25 #include "libavutil/avassert.h"
26 #include "libavutil/avstring.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/samplefmt.h"
64 #define OFFSET(x) offsetof(CompandContext, x)
65 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
68 {
"attacks",
"set time over which increase of volume is determined",
OFFSET(attacks),
AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0,
A },
69 {
"decays",
"set time over which decrease of volume is determined",
OFFSET(decays),
AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0,
A },
74 {
"delay",
"set delay for samples before sending them to volume adjuster",
OFFSET(delay),
AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 20,
A },
135 for (p = item_str; *p; p++) {
155 double in_log, out_log;
158 if (in_lin < s->in_min_lin)
161 in_log = log(in_lin);
164 if (in_log <= s->segments[i + 1].x)
169 out_log = cs->
y + in_log * (cs->
a * in_log + cs->
b);
178 const int channels = inlink->
channels;
192 for (chan = 0; chan < channels; chan++) {
197 for (i = 0; i < nb_samples; i++) {
204 if (frame != out_frame)
210 #define MOD(a, b) (((a) >= (b)) ? (a) - (b) : (a))
216 const int channels = inlink->
channels;
223 for (chan = 0; chan < channels; chan++) {
225 double *dbuf = (
double *)s->
delayptrs[chan];
231 for (i = 0, oindex = 0; i < nb_samples; i++) {
232 const double in = src[i];
267 const int channels = outlink->
channels;
277 for (chan = 0; chan < channels; chan++) {
278 double *dbuf = (
double *)
s->delayptrs[chan];
282 dindex =
s->delay_index;
285 dindex =
MOD(dindex + 1,
s->delay_samples);
289 s->delay_index = dindex;
300 int nb_attacks, nb_decays, nb_points;
301 char *p, *saveptr = NULL;
302 int new_nb_items, num;
310 av_log(ctx,
AV_LOG_ERROR,
"Number of attacks/decays bigger than number of channels.\n");
323 for (i = 0, new_nb_items = 0; i < nb_attacks; i++) {
324 char *tstr =
av_strtok(p,
" ", &saveptr);
330 nb_attacks = new_nb_items;
333 for (i = 0, new_nb_items = 0; i < nb_decays; i++) {
334 char *tstr =
av_strtok(p,
" ", &saveptr);
336 new_nb_items += sscanf(tstr,
"%lf", &s->
channels[i].
decay) == 1;
340 nb_decays = new_nb_items;
342 if (nb_attacks != nb_decays) {
343 av_log(ctx,
AV_LOG_ERROR,
"Number of attacks %d differs from number of decays %d.\n", nb_attacks, nb_decays);
347 #define S(x) s->segments[2 * ((x) + 1)]
349 for (i = 0, new_nb_items = 0; i < nb_points; i++) {
350 char *tstr =
av_strtok(p,
" ", &saveptr);
352 if (sscanf(tstr,
"%lf/%lf", &
S(i).x, &
S(i).
y) != 2) {
356 if (i &&
S(i - 1).x >
S(i).x) {
367 if (num == 0 ||
S(num - 1).x)
371 #define S(x) s->segments[2 * (x)]
378 for (i = 2; i < num; i++) {
379 double g1 = (
S(i - 1).y -
S(i - 2).y) * (
S(i - 0).x -
S(i - 1).x);
380 double g2 = (
S(i - 0).y -
S(i - 1).y) * (
S(i - 1).x -
S(i - 2).x);
386 for (j = --i; j < num; j++)
390 for (i = 0; !i || s->
segments[i - 2].
x; i += 2) {
396 #define L(x) s->segments[i - (x)]
397 for (i = 4; s->
segments[i - 2].
x; i += 2) {
398 double x,
y, cx, cy, in1, in2, out1, out2, theta,
len,
r;
401 L(4).b = (
L(2).y -
L(4).y) / (
L(2).x -
L(4).x);
404 L(2).b = (
L(0).y -
L(2).y) / (
L(0).x -
L(2).x);
406 theta = atan2(
L(2).y -
L(4).y,
L(2).x -
L(4).x);
407 len = sqrt(pow(
L(2).x -
L(4).x, 2.) + pow(
L(2).y -
L(4).y, 2.));
408 r =
FFMIN(radius, len);
409 L(3).x =
L(2).x - r * cos(theta);
410 L(3).y =
L(2).y - r * sin(theta);
412 theta = atan2(
L(0).y -
L(2).y,
L(0).x -
L(2).x);
413 len = sqrt(pow(
L(0).x -
L(2).x, 2.) + pow(
L(0).y -
L(2).y, 2.));
414 r =
FFMIN(radius, len / 2);
415 x =
L(2).x + r * cos(theta);
416 y =
L(2).y + r * sin(theta);
418 cx = (
L(3).x +
L(2).x + x) / 3;
419 cy = (
L(3).y +
L(2).y +
y) / 3;
426 in2 =
L(2).x -
L(3).x;
427 out2 =
L(2).y -
L(3).y;
428 L(3).a = (out2 / in2 - out1 / in1) / (in2-in1);
429 L(3).b = out1 / in1 -
L(3).a * in1;
437 for (i = 0; i < outlink->
channels; i++) {
440 if (cp->
attack > 1.0 / sample_rate)
441 cp->
attack = 1.0 - exp(-1.0 / (sample_rate * cp->
attack));
444 if (cp->
decay > 1.0 / sample_rate)
445 cp->
decay = 1.0 - exp(-1.0 / (sample_rate * cp->
decay));
513 .priv_class = &compand_class,
static const AVFilterPad compand_inputs[]
This structure describes decoded (raw) audio or video data.
static const AVOption compand_options[]
const char * name
Filter name.
void * priv
private data for use by the filter
static const AVFilterPad outputs[]
static double get_volume(CompandContext *s, double in_lin)
AVFilter avfilter_af_compand
uint8_t ** extended_data
pointers to the data planes/channels.
int av_samples_alloc_array_and_samples(uint8_t ***audio_data, int *linesize, int nb_channels, int nb_samples, enum AVSampleFormat sample_fmt, int align)
Allocate a data pointers array, samples buffer for nb_samples samples, and fill data pointers and lin...
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
static enum AVSampleFormat formats[]
static const AVFilterPad compand_outputs[]
static void update_volume(ChanParam *cp, double in)
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
int is_disabled
the enabled state from the last expression evaluation
static av_cold int init(AVFilterContext *ctx)
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
const char * name
Pad name.
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
static int compand_delay(AVFilterContext *ctx, AVFrame *frame)
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
static int query_formats(AVFilterContext *ctx)
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
A filter pad used for either input or output.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq) av_const
Rescale a 64-bit integer by 2 rational numbers.
A link between two filters.
static int compand_nodelay(AVFilterContext *ctx, AVFrame *frame)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);ff_audio_convert_init_arm(ac);ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_dlog(ac->avr,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
unsigned flags
Link processing flags.
Frame requests may need to loop in order to be fulfilled.
int sample_rate
samples per second
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
AVRational time_base
Define the time base used by the PTS of the frames/samples which will pass through this link...
AVFilterContext * src
source filter
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
static int request_frame(AVFilterLink *outlink)
CompandSegment * segments
int format
agreed upon media format
A list of supported channel layouts.
Main libavfilter public API header.
AVFilterLink ** outputs
array of pointers to output links
AVSampleFormat
Audio Sample Formats.
Describe the class of an AVClass context structure.
static const AVFilterPad inputs[]
AVFilterLink ** inputs
array of pointers to input links
rational number numerator/denominator
AVFilterContext * dst
dest filter
enum MovChannelLayoutTag * layouts
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
static av_cold void uninit(AVFilterContext *ctx)
char * av_strtok(char *s, const char *delim, char **saveptr)
Split the string into several tokens which can be accessed by successive calls to av_strtok()...
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
int channels
Number of channels.
static int config_output(AVFilterLink *outlink)
static int compand_drain(AVFilterLink *outlink)
#define AVFILTER_DEFINE_CLASS(fname)
int(* compand)(AVFilterContext *ctx, AVFrame *frame)
static void count_items(char *item_str, int *nb_items)
static void * av_mallocz_array(size_t nmemb, size_t size)
static enum AVSampleFormat sample_fmts[]
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
int nb_samples
number of audio samples (per channel) described by this frame