Libav
af_compand.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 1999 Chris Bagwell
3  * Copyright (c) 1999 Nick Bailey
4  * Copyright (c) 2007 Rob Sykes <robs@users.sourceforge.net>
5  * Copyright (c) 2013 Paul B Mahol
6  * Copyright (c) 2014 Andrew Kelley
7  *
8  * This file is part of libav.
9  *
10  * Libav is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * Libav is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with Libav; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  */
24 
30 #include <string.h>
31 
32 #include "libavutil/avstring.h"
34 #include "libavutil/common.h"
35 #include "libavutil/mathematics.h"
36 #include "libavutil/mem.h"
37 #include "libavutil/opt.h"
38 #include "audio.h"
39 #include "avfilter.h"
40 #include "formats.h"
41 #include "internal.h"
42 
43 typedef struct ChanParam {
44  float attack;
45  float decay;
46  float volume;
47 } ChanParam;
48 
49 typedef struct CompandSegment {
50  float x, y;
51  float a, b;
53 
54 typedef struct CompandContext {
55  const AVClass *class;
58  char *attacks, *decays, *points;
61  float in_min_lin;
62  float out_min_lin;
63  double curve_dB;
64  double gain_dB;
66  double delay;
71  int64_t pts;
72 
73  int (*compand)(AVFilterContext *ctx, AVFrame *frame);
75 
76 #define OFFSET(x) offsetof(CompandContext, x)
77 #define A AV_OPT_FLAG_AUDIO_PARAM
78 
79 static const AVOption compand_options[] = {
80  { "attacks", "set time over which increase of volume is determined", OFFSET(attacks), AV_OPT_TYPE_STRING, { .str = "0.3" }, 0, 0, A },
81  { "decays", "set time over which decrease of volume is determined", OFFSET(decays), AV_OPT_TYPE_STRING, { .str = "0.8" }, 0, 0, A },
82  { "points", "set points of transfer function", OFFSET(points), AV_OPT_TYPE_STRING, { .str = "-70/-70|-60/-20" }, 0, 0, A },
83  { "soft-knee", "set soft-knee", OFFSET(curve_dB), AV_OPT_TYPE_DOUBLE, { .dbl = 0.01 }, 0.01, 900, A },
84  { "gain", "set output gain", OFFSET(gain_dB), AV_OPT_TYPE_DOUBLE, { .dbl = 0 }, -900, 900, A },
85  { "volume", "set initial volume", OFFSET(initial_volume), AV_OPT_TYPE_DOUBLE, { .dbl = 0 }, -900, 0, A },
86  { "delay", "set delay for samples before sending them to volume adjuster", OFFSET(delay), AV_OPT_TYPE_DOUBLE, { .dbl = 0 }, 0, 20, A },
87  { NULL }
88 };
89 
90 static const AVClass compand_class = {
91  .class_name = "compand filter",
92  .item_name = av_default_item_name,
93  .option = compand_options,
94  .version = LIBAVUTIL_VERSION_INT,
95 };
96 
97 static av_cold int init(AVFilterContext *ctx)
98 {
99  CompandContext *s = ctx->priv;
100  s->pts = AV_NOPTS_VALUE;
101  return 0;
102 }
103 
104 static av_cold void uninit(AVFilterContext *ctx)
105 {
106  CompandContext *s = ctx->priv;
107 
108  av_freep(&s->channels);
109  av_freep(&s->segments);
111 }
112 
114 {
117  static const enum AVSampleFormat sample_fmts[] = {
120  };
121 
122  layouts = ff_all_channel_layouts();
123  if (!layouts)
124  return AVERROR(ENOMEM);
125  ff_set_common_channel_layouts(ctx, layouts);
126 
127  formats = ff_make_format_list(sample_fmts);
128  if (!formats)
129  return AVERROR(ENOMEM);
130  ff_set_common_formats(ctx, formats);
131 
132  formats = ff_all_samplerates();
133  if (!formats)
134  return AVERROR(ENOMEM);
135  ff_set_common_samplerates(ctx, formats);
136 
137  return 0;
138 }
139 
140 static void count_items(char *item_str, int *nb_items)
141 {
142  char *p;
143 
144  *nb_items = 1;
145  for (p = item_str; *p; p++) {
146  if (*p == '|')
147  (*nb_items)++;
148  }
149 }
150 
151 static void update_volume(ChanParam *cp, float in)
152 {
153  float delta = in - cp->volume;
154 
155  if (delta > 0.0)
156  cp->volume += delta * cp->attack;
157  else
158  cp->volume += delta * cp->decay;
159 }
160 
161 static float get_volume(CompandContext *s, float in_lin)
162 {
163  CompandSegment *cs;
164  float in_log, out_log;
165  int i;
166 
167  if (in_lin < s->in_min_lin)
168  return s->out_min_lin;
169 
170  in_log = logf(in_lin);
171 
172  for (i = 1; i < s->nb_segments; i++)
173  if (in_log <= s->segments[i].x)
174  break;
175  cs = &s->segments[i - 1];
176  in_log -= cs->x;
177  out_log = cs->y + in_log * (cs->a * in_log + cs->b);
178 
179  return expf(out_log);
180 }
181 
182 static int compand_nodelay(AVFilterContext *ctx, AVFrame *frame)
183 {
184  CompandContext *s = ctx->priv;
185  AVFilterLink *inlink = ctx->inputs[0];
186  const int channels = s->nb_channels;
187  const int nb_samples = frame->nb_samples;
188  AVFrame *out_frame;
189  int chan, i;
190  int err;
191 
192  if (av_frame_is_writable(frame)) {
193  out_frame = frame;
194  } else {
195  out_frame = ff_get_audio_buffer(inlink, nb_samples);
196  if (!out_frame) {
197  av_frame_free(&frame);
198  return AVERROR(ENOMEM);
199  }
200  err = av_frame_copy_props(out_frame, frame);
201  if (err < 0) {
202  av_frame_free(&out_frame);
203  av_frame_free(&frame);
204  return err;
205  }
206  }
207 
208  for (chan = 0; chan < channels; chan++) {
209  const float *src = (float *)frame->extended_data[chan];
210  float *dst = (float *)out_frame->extended_data[chan];
211  ChanParam *cp = &s->channels[chan];
212 
213  for (i = 0; i < nb_samples; i++) {
214  update_volume(cp, fabs(src[i]));
215 
216  dst[i] = av_clipf(src[i] * get_volume(s, cp->volume), -1.0f, 1.0f);
217  }
218  }
219 
220  if (frame != out_frame)
221  av_frame_free(&frame);
222 
223  return ff_filter_frame(ctx->outputs[0], out_frame);
224 }
225 
226 #define MOD(a, b) (((a) >= (b)) ? (a) - (b) : (a))
227 
228 static int compand_delay(AVFilterContext *ctx, AVFrame *frame)
229 {
230  CompandContext *s = ctx->priv;
231  AVFilterLink *inlink = ctx->inputs[0];
232  const int channels = s->nb_channels;
233  const int nb_samples = frame->nb_samples;
234  int chan, i, dindex = 0, oindex, count = 0;
235  AVFrame *out_frame = NULL;
236  int err;
237 
238  if (s->pts == AV_NOPTS_VALUE) {
239  s->pts = (frame->pts == AV_NOPTS_VALUE) ? 0 : frame->pts;
240  }
241 
242  for (chan = 0; chan < channels; chan++) {
243  AVFrame *delay_frame = s->delay_frame;
244  const float *src = (float *)frame->extended_data[chan];
245  float *dbuf = (float *)delay_frame->extended_data[chan];
246  ChanParam *cp = &s->channels[chan];
247  float *dst;
248 
249  count = s->delay_count;
250  dindex = s->delay_index;
251  for (i = 0, oindex = 0; i < nb_samples; i++) {
252  const float in = src[i];
253  update_volume(cp, fabs(in));
254 
255  if (count >= s->delay_samples) {
256  if (!out_frame) {
257  out_frame = ff_get_audio_buffer(inlink, nb_samples - i);
258  if (!out_frame) {
259  av_frame_free(&frame);
260  return AVERROR(ENOMEM);
261  }
262  err = av_frame_copy_props(out_frame, frame);
263  if (err < 0) {
264  av_frame_free(&out_frame);
265  av_frame_free(&frame);
266  return err;
267  }
268  out_frame->pts = s->pts;
269  s->pts += av_rescale_q(nb_samples - i,
270  (AVRational){ 1, inlink->sample_rate },
271  inlink->time_base);
272  }
273 
274  dst = (float *)out_frame->extended_data[chan];
275  dst[oindex++] = av_clipf(dbuf[dindex] *
276  get_volume(s, cp->volume), -1.0f, 1.0f);
277  } else {
278  count++;
279  }
280 
281  dbuf[dindex] = in;
282  dindex = MOD(dindex + 1, s->delay_samples);
283  }
284  }
285 
286  s->delay_count = count;
287  s->delay_index = dindex;
288 
289  av_frame_free(&frame);
290  return out_frame ? ff_filter_frame(ctx->outputs[0], out_frame) : 0;
291 }
292 
293 static int compand_drain(AVFilterLink *outlink)
294 {
295  AVFilterContext *ctx = outlink->src;
296  CompandContext *s = ctx->priv;
297  const int channels = s->nb_channels;
298  AVFrame *frame = NULL;
299  int chan, i, dindex;
300 
301  /* 2048 is to limit output frame size during drain */
302  frame = ff_get_audio_buffer(outlink, FFMIN(2048, s->delay_count));
303  if (!frame)
304  return AVERROR(ENOMEM);
305  frame->pts = s->pts;
306  s->pts += av_rescale_q(frame->nb_samples,
307  (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
308 
309  for (chan = 0; chan < channels; chan++) {
310  AVFrame *delay_frame = s->delay_frame;
311  float *dbuf = (float *)delay_frame->extended_data[chan];
312  float *dst = (float *)frame->extended_data[chan];
313  ChanParam *cp = &s->channels[chan];
314 
315  dindex = s->delay_index;
316  for (i = 0; i < frame->nb_samples; i++) {
317  dst[i] = av_clipf(dbuf[dindex] * get_volume(s, cp->volume),
318  -1.0f, 1.0f);
319  dindex = MOD(dindex + 1, s->delay_samples);
320  }
321  }
322  s->delay_count -= frame->nb_samples;
323  s->delay_index = dindex;
324 
325  return ff_filter_frame(outlink, frame);
326 }
327 
328 static int config_output(AVFilterLink *outlink)
329 {
330  AVFilterContext *ctx = outlink->src;
331  CompandContext *s = ctx->priv;
332  const int sample_rate = outlink->sample_rate;
333  double radius = s->curve_dB * M_LN10 / 20.0;
334  const char *p;
335  const int channels =
337  int nb_attacks, nb_decays, nb_points;
338  int new_nb_items, num;
339  int i;
340  int err;
341 
342 
343  count_items(s->attacks, &nb_attacks);
344  count_items(s->decays, &nb_decays);
345  count_items(s->points, &nb_points);
346 
347  if (channels <= 0) {
348  av_log(ctx, AV_LOG_ERROR, "Invalid number of channels: %d\n", channels);
349  return AVERROR(EINVAL);
350  }
351 
352  if (nb_attacks > channels || nb_decays > channels) {
353  av_log(ctx, AV_LOG_ERROR,
354  "Number of attacks/decays bigger than number of channels.\n");
355  return AVERROR(EINVAL);
356  }
357 
358  uninit(ctx);
359 
360  s->nb_channels = channels;
361  s->channels = av_mallocz_array(channels, sizeof(*s->channels));
362  s->nb_segments = (nb_points + 4) * 2;
363  s->segments = av_mallocz_array(s->nb_segments, sizeof(*s->segments));
364 
365  if (!s->channels || !s->segments) {
366  uninit(ctx);
367  return AVERROR(ENOMEM);
368  }
369 
370  p = s->attacks;
371  for (i = 0, new_nb_items = 0; i < nb_attacks; i++) {
372  char *tstr = av_get_token(&p, "|");
373  if (!tstr)
374  return AVERROR(ENOMEM);
375 
376  new_nb_items += sscanf(tstr, "%f", &s->channels[i].attack) == 1;
377  av_freep(&tstr);
378  if (s->channels[i].attack < 0) {
379  uninit(ctx);
380  return AVERROR(EINVAL);
381  }
382  if (*p)
383  p++;
384  }
385  nb_attacks = new_nb_items;
386 
387  p = s->decays;
388  for (i = 0, new_nb_items = 0; i < nb_decays; i++) {
389  char *tstr = av_get_token(&p, "|");
390  if (!tstr)
391  return AVERROR(ENOMEM);
392  new_nb_items += sscanf(tstr, "%f", &s->channels[i].decay) == 1;
393  av_freep(&tstr);
394  if (s->channels[i].decay < 0) {
395  uninit(ctx);
396  return AVERROR(EINVAL);
397  }
398  if (*p)
399  p++;
400  }
401  nb_decays = new_nb_items;
402 
403  if (nb_attacks != nb_decays) {
404  av_log(ctx, AV_LOG_ERROR,
405  "Number of attacks %d differs from number of decays %d.\n",
406  nb_attacks, nb_decays);
407  uninit(ctx);
408  return AVERROR(EINVAL);
409  }
410 
411 #define S(x) s->segments[2 * ((x) + 1)]
412  p = s->points;
413  for (i = 0, new_nb_items = 0; i < nb_points; i++) {
414  char *tstr = av_get_token(&p, "|");
415  if (!tstr)
416  return AVERROR(ENOMEM);
417 
418  err = sscanf(tstr, "%f/%f", &S(i).x, &S(i).y);
419  av_freep(&tstr);
420  if (err != 2) {
421  av_log(ctx, AV_LOG_ERROR,
422  "Invalid and/or missing input/output value.\n");
423  uninit(ctx);
424  return AVERROR(EINVAL);
425  }
426  if (i && S(i - 1).x > S(i).x) {
427  av_log(ctx, AV_LOG_ERROR,
428  "Transfer function input values must be increasing.\n");
429  uninit(ctx);
430  return AVERROR(EINVAL);
431  }
432  S(i).y -= S(i).x;
433  av_log(ctx, AV_LOG_DEBUG, "%d: x=%f y=%f\n", i, S(i).x, S(i).y);
434  new_nb_items++;
435  if (*p)
436  p++;
437  }
438  num = new_nb_items;
439 
440  /* Add 0,0 if necessary */
441  if (num == 0 || S(num - 1).x)
442  num++;
443 
444 #undef S
445 #define S(x) s->segments[2 * (x)]
446  /* Add a tail off segment at the start */
447  S(0).x = S(1).x - 2 * s->curve_dB;
448  S(0).y = S(1).y;
449  num++;
450 
451  /* Join adjacent colinear segments */
452  for (i = 2; i < num; i++) {
453  double g1 = (S(i - 1).y - S(i - 2).y) * (S(i - 0).x - S(i - 1).x);
454  double g2 = (S(i - 0).y - S(i - 1).y) * (S(i - 1).x - S(i - 2).x);
455  int j;
456 
457  /* here we purposefully lose precision so that we can compare floats */
458  if (fabs(g1 - g2))
459  continue;
460  num--;
461  for (j = --i; j < num; j++)
462  S(j) = S(j + 1);
463  }
464 
465  for (i = 0; !i || s->segments[i - 2].x; i += 2) {
466  s->segments[i].y += s->gain_dB;
467  s->segments[i].x *= M_LN10 / 20;
468  s->segments[i].y *= M_LN10 / 20;
469  }
470 
471 #define L(x) s->segments[i - (x)]
472  for (i = 4; s->segments[i - 2].x; i += 2) {
473  double x, y, cx, cy, in1, in2, out1, out2, theta, len, r;
474 
475  L(4).a = 0;
476  L(4).b = (L(2).y - L(4).y) / (L(2).x - L(4).x);
477 
478  L(2).a = 0;
479  L(2).b = (L(0).y - L(2).y) / (L(0).x - L(2).x);
480 
481  theta = atan2(L(2).y - L(4).y, L(2).x - L(4).x);
482  len = sqrt(pow(L(2).x - L(4).x, 2.) + pow(L(2).y - L(4).y, 2.));
483  r = FFMIN(radius, len);
484  L(3).x = L(2).x - r * cos(theta);
485  L(3).y = L(2).y - r * sin(theta);
486 
487  theta = atan2(L(0).y - L(2).y, L(0).x - L(2).x);
488  len = sqrt(pow(L(0).x - L(2).x, 2.) + pow(L(0).y - L(2).y, 2.));
489  r = FFMIN(radius, len / 2);
490  x = L(2).x + r * cos(theta);
491  y = L(2).y + r * sin(theta);
492 
493  cx = (L(3).x + L(2).x + x) / 3;
494  cy = (L(3).y + L(2).y + y) / 3;
495 
496  L(2).x = x;
497  L(2).y = y;
498 
499  in1 = cx - L(3).x;
500  out1 = cy - L(3).y;
501  in2 = L(2).x - L(3).x;
502  out2 = L(2).y - L(3).y;
503  L(3).a = (out2 / in2 - out1 / in1) / (in2 - in1);
504  L(3).b = out1 / in1 - L(3).a * in1;
505  }
506  L(3).x = 0;
507  L(3).y = L(2).y;
508 
509  s->in_min_lin = exp(s->segments[1].x);
510  s->out_min_lin = exp(s->segments[1].y);
511 
512  for (i = 0; i < channels; i++) {
513  ChanParam *cp = &s->channels[i];
514 
515  if (cp->attack > 1.0 / sample_rate)
516  cp->attack = 1.0 - exp(-1.0 / (sample_rate * cp->attack));
517  else
518  cp->attack = 1.0;
519  if (cp->decay > 1.0 / sample_rate)
520  cp->decay = 1.0 - exp(-1.0 / (sample_rate * cp->decay));
521  else
522  cp->decay = 1.0;
523  cp->volume = pow(10.0, s->initial_volume / 20);
524  }
525 
526  s->delay_samples = s->delay * sample_rate;
527  if (s->delay_samples <= 0) {
529  return 0;
530  }
531 
533  if (!s->delay_frame) {
534  uninit(ctx);
535  return AVERROR(ENOMEM);
536  }
537 
538  s->delay_frame->format = outlink->format;
541 
542  err = av_frame_get_buffer(s->delay_frame, 32);
543  if (err)
544  return err;
545 
546  s->compand = compand_delay;
547  return 0;
548 }
549 
550 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
551 {
552  AVFilterContext *ctx = inlink->dst;
553  CompandContext *s = ctx->priv;
554 
555  return s->compand(ctx, frame);
556 }
557 
558 static int request_frame(AVFilterLink *outlink)
559 {
560  AVFilterContext *ctx = outlink->src;
561  CompandContext *s = ctx->priv;
562  int ret;
563 
564  ret = ff_request_frame(ctx->inputs[0]);
565 
566  if (ret == AVERROR_EOF && s->delay_count)
567  ret = compand_drain(outlink);
568 
569  return ret;
570 }
571 
572 static const AVFilterPad compand_inputs[] = {
573  {
574  .name = "default",
575  .type = AVMEDIA_TYPE_AUDIO,
576  .filter_frame = filter_frame,
577  },
578  { NULL }
579 };
580 
581 static const AVFilterPad compand_outputs[] = {
582  {
583  .name = "default",
584  .request_frame = request_frame,
585  .config_props = config_output,
586  .type = AVMEDIA_TYPE_AUDIO,
587  },
588  { NULL }
589 };
590 
591 
593  .name = "compand",
594  .description = NULL_IF_CONFIG_SMALL(
595  "Compress or expand audio dynamic range."),
596  .query_formats = query_formats,
597  .priv_size = sizeof(CompandContext),
598  .priv_class = &compand_class,
599  .init = init,
600  .uninit = uninit,
601  .inputs = compand_inputs,
602  .outputs = compand_outputs,
603 };
#define L(x)
static const AVFilterPad compand_inputs[]
Definition: af_compand.c:572
char * points
Definition: af_compand.c:58
static void update_volume(ChanParam *cp, float in)
Definition: af_compand.c:151
This structure describes decoded (raw) audio or video data.
Definition: frame.h:107
static const AVOption compand_options[]
Definition: af_compand.c:79
AVOption.
Definition: opt.h:233
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:122
Main libavfilter public API header.
memory handling functions
int64_t pts
Definition: af_compand.c:71
#define OFFSET(x)
Definition: af_compand.c:76
static enum AVSampleFormat formats[]
static const AVFilterPad compand_outputs[]
Definition: af_compand.c:581
static av_cold int init(AVFilterContext *ctx)
Definition: af_compand.c:97
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:165
const char * name
Pad name.
Definition: internal.h:42
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:38
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:571
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:728
#define av_cold
Definition: attributes.h:66
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:43
float delta
AVOptions.
static int compand_delay(AVFilterContext *ctx, AVFrame *frame)
Definition: af_compand.c:228
static int query_formats(AVFilterContext *ctx)
Definition: af_compand.c:113
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:183
static float get_volume(CompandContext *s, float in_lin)
Definition: af_compand.c:161
#define AVERROR_EOF
End of file.
Definition: error.h:51
void ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:375
float in_min_lin
Definition: af_compand.c:61
float, planar
Definition: samplefmt.h:60
#define r
Definition: input.c:51
A filter pad used for either input or output.
Definition: internal.h:36
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:122
#define expf(x)
Definition: libm.h:61
static int compand_nodelay(AVFilterContext *ctx, AVFrame *frame)
Definition: af_compand.c:182
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition: audio.c:57
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:55
sample_fmts
Definition: avconv_filter.c:68
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:142
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){if(!*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));if(!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);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) 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
void * priv
private data for use by the filter
Definition: avfilter.h:584
AVFilter ff_af_compand
Definition: af_compand.c:592
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:144
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:148
char * av_get_token(const char **buf, const char *term)
Unescape the given string until a non escaped terminating char, and return the token corresponding to...
Definition: avstring.c:121
uint64_t channel_layout
Channel layout of the audio data.
Definition: frame.h:353
static int request_frame(AVFilterLink *outlink)
Definition: af_compand.c:558
float decay
Definition: af_compand.c:45
ChanParam * channels
Definition: af_compand.c:60
float out_min_lin
Definition: af_compand.c:62
audio channel layout utility functions
#define FFMIN(a, b)
Definition: common.h:57
static const AVClass compand_class
Definition: af_compand.c:90
char * decays
Definition: af_compand.c:58
CompandSegment * segments
Definition: af_compand.c:59
double delay
Definition: af_compand.c:66
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
AVFilterChannelLayouts * ff_all_channel_layouts(void)
Construct an empty AVFilterChannelLayouts/AVFilterFormats struct – representing any channel layout/...
Definition: formats.c:244
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:158
NULL
Definition: eval.c:55
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition: frame.c:301
AV_SAMPLE_FMT_NONE
Definition: avconv_filter.c:68
av_default_item_name
Definition: dnxhdenc.c:45
Describe the class of an AVClass context structure.
Definition: log.h:33
#define A
Definition: af_compand.c:77
Filter definition.
Definition: avfilter.h:421
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:111
rational number numerator/denominator
Definition: rational.h:43
double curve_dB
Definition: af_compand.c:63
#define S(x)
const char * name
Filter name.
Definition: avfilter.h:425
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:578
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:433
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:238
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
Definition: frame.c:161
void ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:363
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_compand.c:104
double gain_dB
Definition: af_compand.c:64
common internal and external API header
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:49
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: af_compand.c:550
static int config_output(AVFilterLink *outlink)
Definition: af_compand.c:328
float volume
Definition: af_compand.c:46
static int compand_drain(AVFilterLink *outlink)
Definition: af_compand.c:293
int len
int(* compand)(AVFilterContext *ctx, AVFrame *frame)
Definition: af_compand.c:73
static void count_items(char *item_str, int *nb_items)
Definition: af_compand.c:140
char * attacks
Definition: af_compand.c:58
A list of supported formats for one end of a filter link.
Definition: formats.h:64
An instance of a filter.
Definition: avfilter.h:563
AVFrame * delay_frame
Definition: af_compand.c:67
static void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.h:205
void ff_set_common_channel_layouts(AVFilterContext *ctx, AVFilterChannelLayouts *layouts)
A helper for query_formats() which sets all links to the same list of channel layouts/sample rates...
Definition: formats.c:356
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition: avfilter.c:244
double initial_volume
Definition: af_compand.c:65
float attack
Definition: af_compand.c:44
internal API functions
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:141
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:151
#define MOD(a, b)
Definition: af_compand.c:226
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:362
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:228