FFmpeg
swresample.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2011-2013 Michael Niedermayer (michaelni@gmx.at)
3  *
4  * This file is part of libswresample
5  *
6  * libswresample is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * libswresample is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with libswresample; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include "libavutil/opt.h"
22 #include "swresample_internal.h"
23 #include "audioconvert.h"
24 #include "libavutil/avassert.h"
26 #include "libavutil/internal.h"
27 
28 #include <float.h>
29 
30 #define ALIGN 32
31 
33  if(!s || s->in_convert) // s needs to be allocated but not initialized
34  return AVERROR(EINVAL);
35  s->channel_map = channel_map;
36  return 0;
37 }
38 
39 #if FF_API_OLD_CHANNEL_LAYOUT
41 struct SwrContext *swr_alloc_set_opts(struct SwrContext *s,
44  int log_offset, void *log_ctx){
45  if(!s) s= swr_alloc();
46  if(!s) return NULL;
47 
48  s->log_level_offset= log_offset;
49  s->log_ctx= log_ctx;
50 
51  if (av_opt_set_int(s, "ocl", out_ch_layout, 0) < 0)
52  goto fail;
53 
54  if (av_opt_set_int(s, "osf", out_sample_fmt, 0) < 0)
55  goto fail;
56 
57  if (av_opt_set_int(s, "osr", out_sample_rate, 0) < 0)
58  goto fail;
59 
60  if (av_opt_set_int(s, "icl", in_ch_layout, 0) < 0)
61  goto fail;
62 
63  if (av_opt_set_int(s, "isf", in_sample_fmt, 0) < 0)
64  goto fail;
65 
66  if (av_opt_set_int(s, "isr", in_sample_rate, 0) < 0)
67  goto fail;
68 
69  if (av_opt_set_int(s, "ich", av_get_channel_layout_nb_channels(s-> user_in_ch_layout), 0) < 0)
70  goto fail;
71 
72  if (av_opt_set_int(s, "och", av_get_channel_layout_nb_channels(s->user_out_ch_layout), 0) < 0)
73  goto fail;
74 
75  av_opt_set_int(s, "uch", 0, 0);
76  return s;
77 fail:
78  av_log(s, AV_LOG_ERROR, "Failed to set option\n");
79  swr_free(&s);
80  return NULL;
81 }
83 #endif
84 
88  int log_offset, void *log_ctx) {
89  struct SwrContext *s = *ps;
90  int ret;
91 
92  if (!s) s = swr_alloc();
93  if (!s) return AVERROR(ENOMEM);
94 
95  *ps = s;
96 
97  s->log_level_offset = log_offset;
98  s->log_ctx = log_ctx;
99 
100  if ((ret = av_opt_set_chlayout(s, "ochl", out_ch_layout, 0)) < 0)
101  goto fail;
102 
103  if ((ret = av_opt_set_int(s, "osf", out_sample_fmt, 0)) < 0)
104  goto fail;
105 
106  if ((ret = av_opt_set_int(s, "osr", out_sample_rate, 0)) < 0)
107  goto fail;
108 
109  if ((ret = av_opt_set_chlayout(s, "ichl", in_ch_layout, 0)) < 0)
110  goto fail;
111 
112  if ((ret = av_opt_set_int(s, "isf", in_sample_fmt, 0)) < 0)
113  goto fail;
114 
115  if ((ret = av_opt_set_int(s, "isr", in_sample_rate, 0)) < 0)
116  goto fail;
117 
118  av_opt_set_int(s, "uch", 0, 0);
119 
120 #if FF_API_OLD_CHANNEL_LAYOUT
121  // Clear old API values so they don't take precedence in swr_init()
122  av_opt_set_int(s, "icl", 0, 0);
123  av_opt_set_int(s, "ocl", 0, 0);
124  av_opt_set_int(s, "ich", 0, 0);
125  av_opt_set_int(s, "och", 0, 0);
126 #endif
127 
128  return 0;
129 fail:
130  av_log(s, AV_LOG_ERROR, "Failed to set option\n");
131  swr_free(ps);
132  return ret;
133 }
134 
135 static void set_audiodata_fmt(AudioData *a, enum AVSampleFormat fmt){
136  a->fmt = fmt;
137  a->bps = av_get_bytes_per_sample(fmt);
138  a->planar= av_sample_fmt_is_planar(fmt);
139  if (a->ch_count == 1)
140  a->planar = 1;
141 }
142 
143 static void free_temp(AudioData *a){
144  av_free(a->data);
145  memset(a, 0, sizeof(*a));
146 }
147 
148 static void clear_context(SwrContext *s){
149  s->in_buffer_index= 0;
150  s->in_buffer_count= 0;
151  s->resample_in_constraint= 0;
152  memset(s->in.ch, 0, sizeof(s->in.ch));
153  memset(s->out.ch, 0, sizeof(s->out.ch));
154  free_temp(&s->postin);
155  free_temp(&s->midbuf);
156  free_temp(&s->preout);
157  free_temp(&s->in_buffer);
158  free_temp(&s->silence);
159  free_temp(&s->drop_temp);
160  free_temp(&s->dither.noise);
161  free_temp(&s->dither.temp);
162  av_channel_layout_uninit(&s->in_ch_layout);
163  av_channel_layout_uninit(&s->out_ch_layout);
164  av_channel_layout_uninit(&s->used_ch_layout);
166  swri_audio_convert_free(&s->out_convert);
167  swri_audio_convert_free(&s->full_convert);
169 
170  s->delayed_samples_fixup = 0;
171  s->flushed = 0;
172 }
173 
175  SwrContext *s= *ss;
176  if(s){
177  clear_context(s);
178  av_channel_layout_uninit(&s->user_in_chlayout);
179  av_channel_layout_uninit(&s->user_out_chlayout);
180  av_channel_layout_uninit(&s->user_used_chlayout);
181 
182  if (s->resampler)
183  s->resampler->free(&s->resample);
184  }
185 
186  av_freep(ss);
187 }
188 
190  clear_context(s);
191 }
192 
194  int ret;
195  char l1[1024], l2[1024];
196 
197  clear_context(s);
198 
199  if((unsigned) s-> in_sample_fmt >= AV_SAMPLE_FMT_NB){
200  av_log(s, AV_LOG_ERROR, "Requested input sample format %d is invalid\n", s->in_sample_fmt);
201  return AVERROR(EINVAL);
202  }
203  if((unsigned) s->out_sample_fmt >= AV_SAMPLE_FMT_NB){
204  av_log(s, AV_LOG_ERROR, "Requested output sample format %d is invalid\n", s->out_sample_fmt);
205  return AVERROR(EINVAL);
206  }
207 
208  if(s-> in_sample_rate <= 0){
209  av_log(s, AV_LOG_ERROR, "Requested input sample rate %d is invalid\n", s->in_sample_rate);
210  return AVERROR(EINVAL);
211  }
212  if(s->out_sample_rate <= 0){
213  av_log(s, AV_LOG_ERROR, "Requested output sample rate %d is invalid\n", s->out_sample_rate);
214  return AVERROR(EINVAL);
215  }
216 #if FF_API_OLD_CHANNEL_LAYOUT
217  s->out.ch_count = s-> user_out_ch_count;
218  s-> in.ch_count = s-> user_in_ch_count;
219 
220  // if the old/new fields are set inconsistently, prefer the old ones
221  if (s->user_used_ch_count && s->user_used_ch_count != s->user_used_chlayout.nb_channels) {
222  av_channel_layout_uninit(&s->used_ch_layout);
223  s->used_ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;
224  s->used_ch_layout.nb_channels = s->user_used_ch_count;
225  } else if (av_channel_layout_check(&s->user_used_chlayout)) {
226  ret = av_channel_layout_copy(&s->used_ch_layout, &s->user_used_chlayout);
227  if (ret < 0)
228  return ret;
229  }
230  if ((s->user_in_ch_count && s->user_in_ch_count != s->user_in_chlayout.nb_channels) ||
231  (s->user_in_ch_layout && (s->user_in_chlayout.order != AV_CHANNEL_ORDER_NATIVE ||
232  s->user_in_chlayout.u.mask != s->user_in_ch_layout))) {
233  av_channel_layout_uninit(&s->in_ch_layout);
234  if (s->user_in_ch_layout)
235  av_channel_layout_from_mask(&s->in_ch_layout, s->user_in_ch_layout);
236  else {
237  s->in_ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;
238  s->in_ch_layout.nb_channels = s->user_in_ch_count;
239  }
240  } else if (av_channel_layout_check(&s->user_in_chlayout))
241  av_channel_layout_copy(&s->in_ch_layout, &s->user_in_chlayout);
242 
243  if ((s->user_out_ch_count && s->user_out_ch_count != s->user_out_chlayout.nb_channels) ||
244  (s->user_out_ch_layout && (s->user_out_chlayout.order != AV_CHANNEL_ORDER_NATIVE ||
245  s->user_out_chlayout.u.mask != s->user_out_ch_layout))) {
246  av_channel_layout_uninit(&s->out_ch_layout);
247  if (s->user_out_ch_layout)
248  av_channel_layout_from_mask(&s->out_ch_layout, s->user_out_ch_layout);
249  else {
250  s->out_ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;
251  s->out_ch_layout.nb_channels = s->user_out_ch_count;
252  }
253  } else if (av_channel_layout_check(&s->user_out_chlayout))
254  av_channel_layout_copy(&s->out_ch_layout, &s->user_out_chlayout);
255 
256  if (!s->out.ch_count)
257  s->out.ch_count = s->out_ch_layout.nb_channels;
258  if (!s-> in.ch_count)
259  s-> in.ch_count = s->in_ch_layout.nb_channels;
260 
261  if (!(ret = av_channel_layout_check(&s->in_ch_layout)) || s->in_ch_layout.nb_channels > SWR_CH_MAX) {
262  if (ret)
263  av_channel_layout_describe(&s->in_ch_layout, l1, sizeof(l1));
264  av_log(s, AV_LOG_WARNING, "Input channel layout \"%s\" is invalid or unsupported.\n", ret ? l1 : "");
265  return AVERROR(EINVAL);
266  }
267 
268  if (!(ret = av_channel_layout_check(&s->out_ch_layout)) || s->out_ch_layout.nb_channels > SWR_CH_MAX) {
269  if (ret)
270  av_channel_layout_describe(&s->out_ch_layout, l2, sizeof(l2));
271  av_log(s, AV_LOG_WARNING, "Output channel layout \"%s\" is invalid or unsupported.\n", ret ? l2 : "");
272  return AVERROR(EINVAL);
273  }
274 #else
275  s->out.ch_count = s-> user_out_chlayout.nb_channels;
277 
278  if (!(ret = av_channel_layout_check(&s->user_in_chlayout)) || s->user_in_chlayout.nb_channels > SWR_CH_MAX) {
279  if (ret)
280  av_channel_layout_describe(&s->user_in_chlayout, l1, sizeof(l1));
281  av_log(s, AV_LOG_WARNING, "Input channel layout \"%s\" is invalid or unsupported.\n", ret ? l1 : "");
282  return AVERROR(EINVAL);
283  }
284 
285  if (!(ret = av_channel_layout_check(&s->user_out_chlayout)) || s->user_out_chlayout.nb_channels > SWR_CH_MAX) {
286  if (ret)
287  av_channel_layout_describe(&s->user_out_chlayout, l2, sizeof(l2));
288  av_log(s, AV_LOG_WARNING, "Output channel layout \"%s\" is invalid or unsupported.\n", ret ? l2 : "");
289  return AVERROR(EINVAL);
290  }
291 
292  ret = av_channel_layout_copy(&s->in_ch_layout, &s->user_in_chlayout);
293  ret |= av_channel_layout_copy(&s->out_ch_layout, &s->user_out_chlayout);
294  ret |= av_channel_layout_copy(&s->used_ch_layout, &s->user_used_chlayout);
295  if (ret < 0)
296  return ret;
297 #endif
298 
299  s->int_sample_fmt= s->user_int_sample_fmt;
300 
301  s->dither.method = s->user_dither_method;
302 
303  switch(s->engine){
304 #if CONFIG_LIBSOXR
305  case SWR_ENGINE_SOXR: s->resampler = &swri_soxr_resampler; break;
306 #endif
307  case SWR_ENGINE_SWR : s->resampler = &swri_resampler; break;
308  default:
309  av_log(s, AV_LOG_ERROR, "Requested resampling engine is unavailable\n");
310  return AVERROR(EINVAL);
311  }
312 
313  if (!av_channel_layout_check(&s->used_ch_layout))
314  av_channel_layout_default(&s->used_ch_layout, s->in.ch_count);
315 
316  if (s->used_ch_layout.nb_channels != s->in_ch_layout.nb_channels)
317  av_channel_layout_uninit(&s->in_ch_layout);
318 
319  if (s->used_ch_layout.order == AV_CHANNEL_ORDER_UNSPEC)
320  av_channel_layout_default(&s->used_ch_layout, s->used_ch_layout.nb_channels);
321  if (s->in_ch_layout.order == AV_CHANNEL_ORDER_UNSPEC) {
322  ret = av_channel_layout_copy(&s->in_ch_layout, &s->used_ch_layout);
323  if (ret < 0)
324  return ret;
325  }
326  if (s->out_ch_layout.order == AV_CHANNEL_ORDER_UNSPEC)
327  av_channel_layout_default(&s->out_ch_layout, s->out.ch_count);
328 
329  s->rematrix = av_channel_layout_compare(&s->out_ch_layout, &s->in_ch_layout) ||
330  s->rematrix_volume!=1.0 ||
331  s->rematrix_custom;
332 
333  if(s->int_sample_fmt == AV_SAMPLE_FMT_NONE){
334  // 16bit or less to 16bit or less with the same sample rate
336  && av_get_bytes_per_sample(s->out_sample_fmt) <= 2
337  && s->out_sample_rate==s->in_sample_rate) {
338  s->int_sample_fmt= AV_SAMPLE_FMT_S16P;
339  // 8 -> 8, 16->8, 8->16bit
341  +av_get_bytes_per_sample(s->out_sample_fmt) <= 3 ) {
342  s->int_sample_fmt= AV_SAMPLE_FMT_S16P;
343  }else if( av_get_bytes_per_sample(s-> in_sample_fmt) <= 2
344  && !s->rematrix
345  && s->out_sample_rate==s->in_sample_rate
346  && !(s->flags & SWR_FLAG_RESAMPLE)){
347  s->int_sample_fmt= AV_SAMPLE_FMT_S16P;
349  && av_get_planar_sample_fmt(s->out_sample_fmt) == AV_SAMPLE_FMT_S32P
350  && !s->rematrix
351  && s->out_sample_rate == s->in_sample_rate
352  && !(s->flags & SWR_FLAG_RESAMPLE)
353  && s->engine != SWR_ENGINE_SOXR){
354  s->int_sample_fmt= AV_SAMPLE_FMT_S32P;
355  }else if(av_get_bytes_per_sample(s->in_sample_fmt) <= 4){
356  s->int_sample_fmt= AV_SAMPLE_FMT_FLTP;
357  }else{
358  s->int_sample_fmt= AV_SAMPLE_FMT_DBLP;
359  }
360  }
361  av_log(s, AV_LOG_DEBUG, "Using %s internally between filters\n", av_get_sample_fmt_name(s->int_sample_fmt));
362 
363  if( s->int_sample_fmt != AV_SAMPLE_FMT_S16P
364  &&s->int_sample_fmt != AV_SAMPLE_FMT_S32P
365  &&s->int_sample_fmt != AV_SAMPLE_FMT_S64P
366  &&s->int_sample_fmt != AV_SAMPLE_FMT_FLTP
367  &&s->int_sample_fmt != AV_SAMPLE_FMT_DBLP){
368  av_log(s, AV_LOG_ERROR, "Requested sample format %s is not supported internally, s16p/s32p/s64p/fltp/dblp are supported\n", av_get_sample_fmt_name(s->int_sample_fmt));
369  return AVERROR(EINVAL);
370  }
371 
373  set_audiodata_fmt(&s->out, s->out_sample_fmt);
374 
375  if (s->firstpts_in_samples != AV_NOPTS_VALUE) {
376  if (!s->async && s->min_compensation >= FLT_MAX/2)
377  s->async = 1;
378  if (s->firstpts == AV_NOPTS_VALUE)
379  s->firstpts =
380  s->outpts = s->firstpts_in_samples * s->out_sample_rate;
381  } else
382  s->firstpts = AV_NOPTS_VALUE;
383 
384  if (s->async) {
385  if (s->min_compensation >= FLT_MAX/2)
386  s->min_compensation = 0.001;
387  if (s->async > 1.0001) {
388  s->max_soft_compensation = s->async / (double) s->in_sample_rate;
389  }
390  }
391 
392  if (s->out_sample_rate!=s->in_sample_rate || (s->flags & SWR_FLAG_RESAMPLE)){
393  s->resample = s->resampler->init(s->resample, s->out_sample_rate, s->in_sample_rate, s->filter_size, s->phase_shift, s->linear_interp, s->cutoff, s->int_sample_fmt, s->filter_type, s->kaiser_beta, s->precision, s->cheby, s->exact_rational);
394  if (!s->resample) {
395  av_log(s, AV_LOG_ERROR, "Failed to initialize resampler\n");
396  return AVERROR(ENOMEM);
397  }
398  }else
399  s->resampler->free(&s->resample);
400  if( s->int_sample_fmt != AV_SAMPLE_FMT_S16P
401  && s->int_sample_fmt != AV_SAMPLE_FMT_S32P
402  && s->int_sample_fmt != AV_SAMPLE_FMT_FLTP
403  && s->int_sample_fmt != AV_SAMPLE_FMT_DBLP
404  && s->resample){
405  av_log(s, AV_LOG_ERROR, "Resampling only supported with internal s16p/s32p/fltp/dblp\n");
406  ret = AVERROR(EINVAL);
407  goto fail;
408  }
409 
410 #define RSC 1 //FIXME finetune
411  if(!s-> in.ch_count)
412  s-> in.ch_count = s->in_ch_layout.nb_channels;
413  if (!av_channel_layout_check(&s->used_ch_layout))
414  av_channel_layout_default(&s->used_ch_layout, s->in.ch_count);
415  if(!s->out.ch_count)
416  s->out.ch_count = s->out_ch_layout.nb_channels;
417 
418  if(!s-> in.ch_count){
419  av_assert0(s->in_ch_layout.order == AV_CHANNEL_ORDER_UNSPEC);
420  av_log(s, AV_LOG_ERROR, "Input channel count and layout are unset\n");
421  ret = AVERROR(EINVAL);
422  goto fail;
423  }
424 
425  av_channel_layout_describe(&s->out_ch_layout, l2, sizeof(l2));
426 #if FF_API_OLD_CHANNEL_LAYOUT
427  if (s->out_ch_layout.order != AV_CHANNEL_ORDER_UNSPEC && s->out.ch_count != s->out_ch_layout.nb_channels) {
428  av_log(s, AV_LOG_ERROR, "Output channel layout %s mismatches specified channel count %d\n", l2, s->out.ch_count);
429  ret = AVERROR(EINVAL);
430  goto fail;
431  }
432 #endif
433  av_channel_layout_describe(&s->in_ch_layout, l1, sizeof(l1));
434  if (s->in_ch_layout.order != AV_CHANNEL_ORDER_UNSPEC && s->used_ch_layout.nb_channels != s->in_ch_layout.nb_channels) {
435  av_log(s, AV_LOG_ERROR, "Input channel layout %s mismatches specified channel count %d\n", l1, s->used_ch_layout.nb_channels);
436  ret = AVERROR(EINVAL);
437  goto fail;
438  }
439 
440  if (( s->out_ch_layout.order == AV_CHANNEL_ORDER_UNSPEC
441  || s-> in_ch_layout.order == AV_CHANNEL_ORDER_UNSPEC) && s->used_ch_layout.nb_channels != s->out.ch_count && !s->rematrix_custom) {
442  av_log(s, AV_LOG_ERROR, "Rematrix is needed between %s and %s "
443  "but there is not enough information to do it\n", l1, l2);
444  ret = AVERROR(EINVAL);
445  goto fail;
446  }
447 
448 av_assert0(s->used_ch_layout.nb_channels);
449 av_assert0(s->out.ch_count);
450  s->resample_first= RSC*s->out.ch_count/s->used_ch_layout.nb_channels - RSC < s->out_sample_rate/(float)s-> in_sample_rate - 1.0;
451 
452  s->in_buffer= s->in;
453  s->silence = s->in;
454  s->drop_temp= s->out;
455 
456  if ((ret = swri_dither_init(s, s->out_sample_fmt, s->int_sample_fmt)) < 0)
457  goto fail;
458 
459  if(!s->resample && !s->rematrix && !s->channel_map && !s->dither.method){
460  s->full_convert = swri_audio_convert_alloc(s->out_sample_fmt,
461  s-> in_sample_fmt, s-> in.ch_count, NULL, 0);
462  return 0;
463  }
464 
465  s->in_convert = swri_audio_convert_alloc(s->int_sample_fmt,
466  s-> in_sample_fmt, s->used_ch_layout.nb_channels, s->channel_map, 0);
467  s->out_convert= swri_audio_convert_alloc(s->out_sample_fmt,
468  s->int_sample_fmt, s->out.ch_count, NULL, 0);
469 
470  if (!s->in_convert || !s->out_convert) {
471  ret = AVERROR(ENOMEM);
472  goto fail;
473  }
474 
475  s->postin= s->in;
476  s->preout= s->out;
477  s->midbuf= s->in;
478 
479  if(s->channel_map){
480  s->postin.ch_count=
481  s->midbuf.ch_count= s->used_ch_layout.nb_channels;
482  if(s->resample)
483  s->in_buffer.ch_count= s->used_ch_layout.nb_channels;
484  }
485  if(!s->resample_first){
486  s->midbuf.ch_count= s->out.ch_count;
487  if(s->resample)
488  s->in_buffer.ch_count = s->out.ch_count;
489  }
490 
491  set_audiodata_fmt(&s->postin, s->int_sample_fmt);
492  set_audiodata_fmt(&s->midbuf, s->int_sample_fmt);
493  set_audiodata_fmt(&s->preout, s->int_sample_fmt);
494 
495  if(s->resample){
496  set_audiodata_fmt(&s->in_buffer, s->int_sample_fmt);
497  }
498 
499  av_assert0(!s->preout.count);
500  s->dither.noise = s->preout;
501  s->dither.temp = s->preout;
502  if (s->dither.method > SWR_DITHER_NS) {
503  s->dither.noise.bps = 4;
504  s->dither.noise.fmt = AV_SAMPLE_FMT_FLTP;
505  s->dither.noise_scale = 1;
506  }
507 
508  if(s->rematrix || s->dither.method) {
510  if (ret < 0)
511  goto fail;
512  }
513 
514  return 0;
515 fail:
516  swr_close(s);
517  return ret;
518 
519 }
520 
521 int swri_realloc_audio(AudioData *a, int count){
522  int i, countb;
523  AudioData old;
524 
525  if(count < 0 || count > INT_MAX/2/a->bps/a->ch_count)
526  return AVERROR(EINVAL);
527 
528  if(a->count >= count)
529  return 0;
530 
531  count*=2;
532 
533  countb= FFALIGN(count*a->bps, ALIGN);
534  old= *a;
535 
536  av_assert0(a->bps);
537  av_assert0(a->ch_count);
538 
539  a->data = av_calloc(countb, a->ch_count);
540  if(!a->data)
541  return AVERROR(ENOMEM);
542  for(i=0; i<a->ch_count; i++){
543  a->ch[i]= a->data + i*(a->planar ? countb : a->bps);
544  if(a->count && a->planar) memcpy(a->ch[i], old.ch[i], a->count*a->bps);
545  }
546  if(a->count && !a->planar) memcpy(a->ch[0], old.ch[0], a->count*a->ch_count*a->bps);
547  av_freep(&old.data);
548  a->count= count;
549 
550  return 1;
551 }
552 
553 static void copy(AudioData *out, AudioData *in,
554  int count){
555  av_assert0(out->planar == in->planar);
556  av_assert0(out->bps == in->bps);
557  av_assert0(out->ch_count == in->ch_count);
558  if(out->planar){
559  int ch;
560  for(ch=0; ch<out->ch_count; ch++)
561  memcpy(out->ch[ch], in->ch[ch], count*out->bps);
562  }else
563  memcpy(out->ch[0], in->ch[0], count*out->ch_count*out->bps);
564 }
565 
566 static void fill_audiodata(AudioData *out, uint8_t *in_arg [SWR_CH_MAX]){
567  int i;
568  if(!in_arg){
569  memset(out->ch, 0, sizeof(out->ch));
570  }else if(out->planar){
571  for(i=0; i<out->ch_count; i++)
572  out->ch[i]= in_arg[i];
573  }else{
574  for(i=0; i<out->ch_count; i++)
575  out->ch[i]= in_arg[0] + i*out->bps;
576  }
577 }
578 
579 static void reversefill_audiodata(AudioData *out, uint8_t *in_arg [SWR_CH_MAX]){
580  int i;
581  if(out->planar){
582  for(i=0; i<out->ch_count; i++)
583  in_arg[i]= out->ch[i];
584  }else{
585  in_arg[0]= out->ch[0];
586  }
587 }
588 
589 /**
590  *
591  * out may be equal in.
592  */
593 static void buf_set(AudioData *out, AudioData *in, int count){
594  int ch;
595  if(in->planar){
596  for(ch=0; ch<out->ch_count; ch++)
597  out->ch[ch]= in->ch[ch] + count*out->bps;
598  }else{
599  for(ch=out->ch_count-1; ch>=0; ch--)
600  out->ch[ch]= in->ch[0] + (ch + count*out->ch_count) * out->bps;
601  }
602 }
603 
604 /**
605  *
606  * @return number of samples output per channel
607  */
608 static int resample(SwrContext *s, AudioData *out_param, int out_count,
609  const AudioData * in_param, int in_count){
610  AudioData in, out, tmp;
611  int ret_sum=0;
612  int border=0;
613  int padless = ARCH_X86 && s->engine == SWR_ENGINE_SWR ? 7 : 0;
614 
615  av_assert1(s->in_buffer.ch_count == in_param->ch_count);
616  av_assert1(s->in_buffer.planar == in_param->planar);
617  av_assert1(s->in_buffer.fmt == in_param->fmt);
618 
619  tmp=out=*out_param;
620  in = *in_param;
621 
622  border = s->resampler->invert_initial_buffer(s->resample, &s->in_buffer,
623  &in, in_count, &s->in_buffer_index, &s->in_buffer_count);
624  if (border == INT_MAX) {
625  return 0;
626  } else if (border < 0) {
627  return border;
628  } else if (border) {
629  buf_set(&in, &in, border);
630  in_count -= border;
631  s->resample_in_constraint = 0;
632  }
633 
634  do{
635  int ret, size, consumed;
636  if(!s->resample_in_constraint && s->in_buffer_count){
637  buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
638  ret= s->resampler->multiple_resample(s->resample, &out, out_count, &tmp, s->in_buffer_count, &consumed);
639  out_count -= ret;
640  ret_sum += ret;
641  buf_set(&out, &out, ret);
642  s->in_buffer_count -= consumed;
643  s->in_buffer_index += consumed;
644 
645  if(!in_count)
646  break;
647  if(s->in_buffer_count <= border){
648  buf_set(&in, &in, -s->in_buffer_count);
649  in_count += s->in_buffer_count;
650  s->in_buffer_count=0;
651  s->in_buffer_index=0;
652  border = 0;
653  }
654  }
655 
656  if((s->flushed || in_count > padless) && !s->in_buffer_count){
657  s->in_buffer_index=0;
658  ret= s->resampler->multiple_resample(s->resample, &out, out_count, &in, FFMAX(in_count-padless, 0), &consumed);
659  out_count -= ret;
660  ret_sum += ret;
661  buf_set(&out, &out, ret);
662  in_count -= consumed;
663  buf_set(&in, &in, consumed);
664  }
665 
666  //TODO is this check sane considering the advanced copy avoidance below
667  size= s->in_buffer_index + s->in_buffer_count + in_count;
668  if( size > s->in_buffer.count
669  && s->in_buffer_count + in_count <= s->in_buffer_index){
670  buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
671  copy(&s->in_buffer, &tmp, s->in_buffer_count);
672  s->in_buffer_index=0;
673  }else
674  if((ret=swri_realloc_audio(&s->in_buffer, size)) < 0)
675  return ret;
676 
677  if(in_count){
678  int count= in_count;
679  if(s->in_buffer_count && s->in_buffer_count+2 < count && out_count) count= s->in_buffer_count+2;
680 
681  buf_set(&tmp, &s->in_buffer, s->in_buffer_index + s->in_buffer_count);
682  copy(&tmp, &in, /*in_*/count);
683  s->in_buffer_count += count;
684  in_count -= count;
685  border += count;
686  buf_set(&in, &in, count);
687  s->resample_in_constraint= 0;
688  if(s->in_buffer_count != count || in_count)
689  continue;
690  if (padless) {
691  padless = 0;
692  continue;
693  }
694  }
695  break;
696  }while(1);
697 
698  s->resample_in_constraint= !!out_count;
699 
700  return ret_sum;
701 }
702 
703 static int swr_convert_internal(struct SwrContext *s, AudioData *out, int out_count,
704  AudioData *in , int in_count){
706  int ret/*, in_max*/;
707  AudioData preout_tmp, midbuf_tmp;
708 
709  if(s->full_convert){
710  av_assert0(!s->resample);
711  swri_audio_convert(s->full_convert, out, in, in_count);
712  return out_count;
713  }
714 
715 // in_max= out_count*(int64_t)s->in_sample_rate / s->out_sample_rate + resample_filter_taps;
716 // in_count= FFMIN(in_count, in_in + 2 - s->hist_buffer_count);
717 
718  if((ret=swri_realloc_audio(&s->postin, in_count))<0)
719  return ret;
720  if(s->resample_first){
721  av_assert0(s->midbuf.ch_count == s->used_ch_layout.nb_channels);
722  if((ret=swri_realloc_audio(&s->midbuf, out_count))<0)
723  return ret;
724  }else{
725  av_assert0(s->midbuf.ch_count == s->out.ch_count);
726  if((ret=swri_realloc_audio(&s->midbuf, in_count))<0)
727  return ret;
728  }
729  if((ret=swri_realloc_audio(&s->preout, out_count))<0)
730  return ret;
731 
732  postin= &s->postin;
733 
734  midbuf_tmp= s->midbuf;
735  midbuf= &midbuf_tmp;
736  preout_tmp= s->preout;
737  preout= &preout_tmp;
738 
739  if(s->int_sample_fmt == s-> in_sample_fmt && s->in.planar && !s->channel_map)
740  postin= in;
741 
742  if(s->resample_first ? !s->resample : !s->rematrix)
743  midbuf= postin;
744 
745  if(s->resample_first ? !s->rematrix : !s->resample)
746  preout= midbuf;
747 
748  if(s->int_sample_fmt == s->out_sample_fmt && s->out.planar
749  && !(s->out_sample_fmt==AV_SAMPLE_FMT_S32P && (s->dither.output_sample_bits&31))){
750  if(preout==in){
751  out_count= FFMIN(out_count, in_count); //TODO check at the end if this is needed or redundant
752  av_assert0(s->in.planar); //we only support planar internally so it has to be, we support copying non planar though
753  copy(out, in, out_count);
754  return out_count;
755  }
756  else if(preout==postin) preout= midbuf= postin= out;
757  else if(preout==midbuf) preout= midbuf= out;
758  else preout= out;
759  }
760 
761  if(in != postin){
762  swri_audio_convert(s->in_convert, postin, in, in_count);
763  }
764 
765  if(s->resample_first){
766  if(postin != midbuf)
767  if ((out_count = resample(s, midbuf, out_count, postin, in_count)) < 0)
768  return out_count;
769  if(midbuf != preout)
770  swri_rematrix(s, preout, midbuf, out_count, preout==out);
771  }else{
772  if(postin != midbuf)
773  swri_rematrix(s, midbuf, postin, in_count, midbuf==out);
774  if(midbuf != preout)
775  if ((out_count = resample(s, preout, out_count, midbuf, in_count)) < 0)
776  return out_count;
777  }
778 
779  if(preout != out && out_count){
780  AudioData *conv_src = preout;
781  if(s->dither.method){
782  int ch;
783  int dither_count= FFMAX(out_count, 1<<16);
784 
785  if (preout == in) {
786  conv_src = &s->dither.temp;
787  if((ret=swri_realloc_audio(&s->dither.temp, dither_count))<0)
788  return ret;
789  }
790 
791  if((ret=swri_realloc_audio(&s->dither.noise, dither_count))<0)
792  return ret;
793  if(ret)
794  for(ch=0; ch<s->dither.noise.ch_count; ch++)
795  if((ret=swri_get_dither(s, s->dither.noise.ch[ch], s->dither.noise.count, (12345678913579ULL*ch + 3141592) % 2718281828U, s->dither.noise.fmt))<0)
796  return ret;
797  av_assert0(s->dither.noise.ch_count == preout->ch_count);
798 
799  if(s->dither.noise_pos + out_count > s->dither.noise.count)
800  s->dither.noise_pos = 0;
801 
802  if (s->dither.method < SWR_DITHER_NS){
803  if (s->mix_2_1_simd) {
804  int len1= out_count&~15;
805  int off = len1 * preout->bps;
806 
807  if(len1)
808  for(ch=0; ch<preout->ch_count; ch++)
809  s->mix_2_1_simd(conv_src->ch[ch], preout->ch[ch], s->dither.noise.ch[ch] + s->dither.noise.bps * s->dither.noise_pos, s->native_simd_one, 0, 0, len1);
810  if(out_count != len1)
811  for(ch=0; ch<preout->ch_count; ch++)
812  s->mix_2_1_f(conv_src->ch[ch] + off, preout->ch[ch] + off, s->dither.noise.ch[ch] + s->dither.noise.bps * s->dither.noise_pos + off, s->native_one, 0, 0, out_count - len1);
813  } else {
814  for(ch=0; ch<preout->ch_count; ch++)
815  s->mix_2_1_f(conv_src->ch[ch], preout->ch[ch], s->dither.noise.ch[ch] + s->dither.noise.bps * s->dither.noise_pos, s->native_one, 0, 0, out_count);
816  }
817  } else {
818  switch(s->int_sample_fmt) {
819  case AV_SAMPLE_FMT_S16P :swri_noise_shaping_int16(s, conv_src, preout, &s->dither.noise, out_count); break;
820  case AV_SAMPLE_FMT_S32P :swri_noise_shaping_int32(s, conv_src, preout, &s->dither.noise, out_count); break;
821  case AV_SAMPLE_FMT_FLTP :swri_noise_shaping_float(s, conv_src, preout, &s->dither.noise, out_count); break;
822  case AV_SAMPLE_FMT_DBLP :swri_noise_shaping_double(s,conv_src, preout, &s->dither.noise, out_count); break;
823  }
824  }
825  s->dither.noise_pos += out_count;
826  }
827 //FIXME packed doesn't need more than 1 chan here!
828  swri_audio_convert(s->out_convert, out, conv_src, out_count);
829  }
830  return out_count;
831 }
832 
834  return !!s->in_buffer.ch_count;
835 }
836 
838  uint8_t **out_arg, int out_count,
839  const uint8_t **in_arg, int in_count)
840 {
841  AudioData * in= &s->in;
842  AudioData *out= &s->out;
843  int av_unused max_output;
844 
845  if (!swr_is_initialized(s)) {
846  av_log(s, AV_LOG_ERROR, "Context has not been initialized\n");
847  return AVERROR(EINVAL);
848  }
849 #if defined(ASSERT_LEVEL) && ASSERT_LEVEL >1
850  max_output = swr_get_out_samples(s, in_count);
851 #endif
852 
853  while(s->drop_output > 0){
854  int ret;
855  uint8_t *tmp_arg[SWR_CH_MAX];
856 #define MAX_DROP_STEP 16384
857  if((ret=swri_realloc_audio(&s->drop_temp, FFMIN(s->drop_output, MAX_DROP_STEP)))<0)
858  return ret;
859 
860  reversefill_audiodata(&s->drop_temp, tmp_arg);
861  s->drop_output *= -1; //FIXME find a less hackish solution
862  ret = swr_convert(s, tmp_arg, FFMIN(-s->drop_output, MAX_DROP_STEP), in_arg, in_count); //FIXME optimize but this is as good as never called so maybe it doesn't matter
863  s->drop_output *= -1;
864  in_count = 0;
865  if(ret>0) {
866  s->drop_output -= ret;
867  if (!s->drop_output && !out_arg)
868  return 0;
869  continue;
870  }
871 
872  av_assert0(s->drop_output);
873  return 0;
874  }
875 
876  if(!in_arg){
877  if(s->resample){
878  if (!s->flushed)
879  s->resampler->flush(s);
880  s->resample_in_constraint = 0;
881  s->flushed = 1;
882  }else if(!s->in_buffer_count){
883  return 0;
884  }
885  }else
886  fill_audiodata(in , (void*)in_arg);
887 
888  fill_audiodata(out, out_arg);
889 
890  if(s->resample){
891  int ret = swr_convert_internal(s, out, out_count, in, in_count);
892  if(ret>0 && !s->drop_output)
893  s->outpts += ret * (int64_t)s->in_sample_rate;
894 
895  av_assert2(max_output < 0 || ret <= max_output);
896 
897  return ret;
898  }else{
899  AudioData tmp= *in;
900  int ret2=0;
901  int ret, size;
902  size = FFMIN(out_count, s->in_buffer_count);
903  if(size){
904  buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
906  if(ret<0)
907  return ret;
908  ret2= ret;
909  s->in_buffer_count -= ret;
910  s->in_buffer_index += ret;
911  buf_set(out, out, ret);
912  out_count -= ret;
913  if(!s->in_buffer_count)
914  s->in_buffer_index = 0;
915  }
916 
917  if(in_count){
918  size= s->in_buffer_index + s->in_buffer_count + in_count - out_count;
919 
920  if(in_count > out_count) { //FIXME move after swr_convert_internal
921  if( size > s->in_buffer.count
922  && s->in_buffer_count + in_count - out_count <= s->in_buffer_index){
923  buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
924  copy(&s->in_buffer, &tmp, s->in_buffer_count);
925  s->in_buffer_index=0;
926  }else
927  if((ret=swri_realloc_audio(&s->in_buffer, size)) < 0)
928  return ret;
929  }
930 
931  if(out_count){
932  size = FFMIN(in_count, out_count);
934  if(ret<0)
935  return ret;
936  buf_set(in, in, ret);
937  in_count -= ret;
938  ret2 += ret;
939  }
940  if(in_count){
941  buf_set(&tmp, &s->in_buffer, s->in_buffer_index + s->in_buffer_count);
942  copy(&tmp, in, in_count);
943  s->in_buffer_count += in_count;
944  }
945  }
946  if(ret2>0 && !s->drop_output)
947  s->outpts += ret2 * (int64_t)s->in_sample_rate;
948  av_assert2(max_output < 0 || ret2 < 0 || ret2 <= max_output);
949  return ret2;
950  }
951 }
952 
953 int swr_drop_output(struct SwrContext *s, int count){
954  const uint8_t *tmp_arg[SWR_CH_MAX];
955  s->drop_output += count;
956 
957  if(s->drop_output <= 0)
958  return 0;
959 
960  av_log(s, AV_LOG_VERBOSE, "discarding %d audio samples\n", count);
961  return swr_convert(s, NULL, s->drop_output, tmp_arg, 0);
962 }
963 
964 int swr_inject_silence(struct SwrContext *s, int count){
965  int ret, i;
966  uint8_t *tmp_arg[SWR_CH_MAX];
967 
968  if(count <= 0)
969  return 0;
970 
971 #define MAX_SILENCE_STEP 16384
972  while (count > MAX_SILENCE_STEP) {
974  return ret;
975  count -= MAX_SILENCE_STEP;
976  }
977 
978  if((ret=swri_realloc_audio(&s->silence, count))<0)
979  return ret;
980 
981  if(s->silence.planar) for(i=0; i<s->silence.ch_count; i++) {
982  memset(s->silence.ch[i], s->silence.bps==1 ? 0x80 : 0, count*s->silence.bps);
983  } else
984  memset(s->silence.ch[0], s->silence.bps==1 ? 0x80 : 0, count*s->silence.bps*s->silence.ch_count);
985 
986  reversefill_audiodata(&s->silence, tmp_arg);
987  av_log(s, AV_LOG_VERBOSE, "adding %d audio samples of silence\n", count);
988  ret = swr_convert(s, NULL, 0, (const uint8_t**)tmp_arg, count);
989  return ret;
990 }
991 
992 int64_t swr_get_delay(struct SwrContext *s, int64_t base){
993  if (s->resampler && s->resample){
994  return s->resampler->get_delay(s, base);
995  }else{
996  return (s->in_buffer_count*base + (s->in_sample_rate>>1))/ s->in_sample_rate;
997  }
998 }
999 
1000 int swr_get_out_samples(struct SwrContext *s, int in_samples)
1001 {
1002  int64_t out_samples;
1003 
1004  if (in_samples < 0)
1005  return AVERROR(EINVAL);
1006 
1007  if (s->resampler && s->resample) {
1008  if (!s->resampler->get_out_samples)
1009  return AVERROR(ENOSYS);
1010  out_samples = s->resampler->get_out_samples(s, in_samples);
1011  } else {
1012  out_samples = s->in_buffer_count + in_samples;
1013  av_assert0(s->out_sample_rate == s->in_sample_rate);
1014  }
1015 
1016  if (out_samples > INT_MAX)
1017  return AVERROR(EINVAL);
1018 
1019  return out_samples;
1020 }
1021 
1022 int swr_set_compensation(struct SwrContext *s, int sample_delta, int compensation_distance){
1023  int ret;
1024 
1025  if (!s || compensation_distance < 0)
1026  return AVERROR(EINVAL);
1027  if (!compensation_distance && sample_delta)
1028  return AVERROR(EINVAL);
1029  if (!s->resample) {
1030  s->flags |= SWR_FLAG_RESAMPLE;
1031  ret = swr_init(s);
1032  if (ret < 0)
1033  return ret;
1034  }
1035  if (!s->resampler->set_compensation){
1036  return AVERROR(EINVAL);
1037  }else{
1038  return s->resampler->set_compensation(s->resample, sample_delta, compensation_distance);
1039  }
1040 }
1041 
1042 int64_t swr_next_pts(struct SwrContext *s, int64_t pts){
1043  if(pts == INT64_MIN)
1044  return s->outpts;
1045 
1046  if (s->firstpts == AV_NOPTS_VALUE)
1047  s->outpts = s->firstpts = pts;
1048 
1049  if(s->min_compensation >= FLT_MAX) {
1050  return (s->outpts = pts - swr_get_delay(s, s->in_sample_rate * (int64_t)s->out_sample_rate));
1051  } else {
1052  int64_t delta = pts - swr_get_delay(s, s->in_sample_rate * (int64_t)s->out_sample_rate) - s->outpts + s->drop_output*(int64_t)s->in_sample_rate;
1053  double fdelta = delta /(double)(s->in_sample_rate * (int64_t)s->out_sample_rate);
1054 
1055  if(fabs(fdelta) > s->min_compensation) {
1056  if(s->outpts == s->firstpts || fabs(fdelta) > s->min_hard_compensation){
1057  int ret;
1058  if(delta > 0) ret = swr_inject_silence(s, delta / s->out_sample_rate);
1059  else ret = swr_drop_output (s, -delta / s-> in_sample_rate);
1060  if(ret<0){
1061  av_log(s, AV_LOG_ERROR, "Failed to compensate for timestamp delta of %f\n", fdelta);
1062  }
1063  } else if(s->soft_compensation_duration && s->max_soft_compensation) {
1064  int duration = s->out_sample_rate * s->soft_compensation_duration;
1065  double max_soft_compensation = s->max_soft_compensation / (s->max_soft_compensation < 0 ? -s->in_sample_rate : 1);
1067  av_log(s, AV_LOG_VERBOSE, "compensating audio timestamp drift:%f compensation:%d in:%d\n", fdelta, comp, duration);
1069  }
1070  }
1071 
1072  return s->outpts;
1073  }
1074 }
FF_ENABLE_DEPRECATION_WARNINGS
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:73
AV_SAMPLE_FMT_FLTP
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition: samplefmt.h:66
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
swr_convert_internal
static int swr_convert_internal(struct SwrContext *s, AudioData *out, int out_count, AudioData *in, int in_count)
Definition: swresample.c:703
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
fill_audiodata
static void fill_audiodata(AudioData *out, uint8_t *in_arg[SWR_CH_MAX])
Definition: swresample.c:566
swri_noise_shaping_int16
void swri_noise_shaping_int16(SwrContext *s, AudioData *dsts, const AudioData *srcs, const AudioData *noises, int count)
out
FILE * out
Definition: movenc.c:54
SwrContext::in_sample_rate
int in_sample_rate
input sample rate
Definition: swresample_internal.h:105
free_temp
static void free_temp(AudioData *a)
Definition: swresample.c:143
comp
static void comp(unsigned char *dst, ptrdiff_t dst_stride, unsigned char *src, ptrdiff_t src_stride, int add)
Definition: eamad.c:80
swr_set_compensation
int swr_set_compensation(struct SwrContext *s, int sample_delta, int compensation_distance)
Activate resampling compensation ("soft" compensation).
Definition: swresample.c:1022
AudioData::bps
int bps
bytes per sample
Definition: swresample_internal.h:49
av_unused
#define av_unused
Definition: attributes.h:131
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
SwrContext::out_sample_rate
int out_sample_rate
output sample rate
Definition: swresample_internal.h:106
AV_SAMPLE_FMT_S32P
@ AV_SAMPLE_FMT_S32P
signed 32 bits, planar
Definition: samplefmt.h:65
SwrContext::out_ch_layout
AVChannelLayout out_ch_layout
output channel layout
Definition: swresample_internal.h:104
SwrContext::user_in_chlayout
AVChannelLayout user_in_chlayout
User set input channel layout.
Definition: swresample_internal.h:125
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:196
base
uint8_t base
Definition: vp3data.h:128
float.h
swr_set_channel_mapping
int swr_set_channel_mapping(struct SwrContext *s, const int *channel_map)
Set a customized input channel mapping.
Definition: swresample.c:32
AVChannelLayout::order
enum AVChannelOrder order
Channel order used in this layout.
Definition: channel_layout.h:318
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
copy
static void copy(AudioData *out, AudioData *in, int count)
Definition: swresample.c:553
av_get_channel_layout_nb_channels
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
Definition: channel_layout.c:330
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:323
SwrContext::in_buffer_index
int in_buffer_index
cached buffer position
Definition: swresample_internal.h:161
swri_noise_shaping_float
void swri_noise_shaping_float(SwrContext *s, AudioData *dsts, const AudioData *srcs, const AudioData *noises, int count)
SWR_ENGINE_SOXR
@ SWR_ENGINE_SOXR
SoX Resampler.
Definition: swresample.h:168
buf_set
static void buf_set(AudioData *out, AudioData *in, int count)
out may be equal in.
Definition: swresample.c:593
AudioData
Definition: swresample_internal.h:45
SwrContext::out_sample_fmt
enum AVSampleFormat out_sample_fmt
output sample format
Definition: swresample_internal.h:101
fail
#define fail()
Definition: checkasm.h:179
swri_realloc_audio
int swri_realloc_audio(AudioData *a, int count)
Definition: swresample.c:521
MAX_SILENCE_STEP
#define MAX_SILENCE_STEP
swr_is_initialized
int swr_is_initialized(struct SwrContext *s)
Check whether an swr context has been initialized or not.
Definition: swresample.c:833
AV_SAMPLE_FMT_S64P
@ AV_SAMPLE_FMT_S64P
signed 64 bits, planar
Definition: samplefmt.h:69
pts
static int64_t pts
Definition: transcode_aac.c:643
ss
#define ss(width, name, subs,...)
Definition: cbs_vp9.c:202
swr_next_pts
int64_t swr_next_pts(struct SwrContext *s, int64_t pts)
Convert the next timestamp from input to output timestamps are in 1/(in_sample_rate * out_sample_rate...
Definition: swresample.c:1042
swr_get_delay
int64_t swr_get_delay(struct SwrContext *s, int64_t base)
Gets the delay the next input sample will experience relative to the next output sample.
Definition: swresample.c:992
av_get_planar_sample_fmt
enum AVSampleFormat av_get_planar_sample_fmt(enum AVSampleFormat sample_fmt)
Get the planar alternative form of the given sample format.
Definition: samplefmt.c:86
SwrContext::postin
AudioData postin
post-input audio data: used for rematrix/resample
Definition: swresample_internal.h:154
avassert.h
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
swr_inject_silence
int swr_inject_silence(struct SwrContext *s, int count)
Injects the specified number of silence samples.
Definition: swresample.c:964
swr_init
av_cold int swr_init(struct SwrContext *s)
Initialize context after user parameters have been set.
Definition: swresample.c:193
duration
int64_t duration
Definition: movenc.c:64
av_channel_layout_describe
int av_channel_layout_describe(const AVChannelLayout *channel_layout, char *buf, size_t buf_size)
Get a human-readable string describing the channel layout properties.
Definition: channel_layout.c:847
float
float
Definition: af_crystalizer.c:121
s
#define s(width, name)
Definition: cbs_vp9.c:198
SwrContext::user_out_chlayout
AVChannelLayout user_out_chlayout
User set output channel layout.
Definition: swresample_internal.h:126
AV_CHANNEL_ORDER_UNSPEC
@ AV_CHANNEL_ORDER_UNSPEC
Only the channel count is specified, without any further information about the channel order.
Definition: channel_layout.h:112
av_channel_layout_from_mask
int av_channel_layout_from_mask(AVChannelLayout *channel_layout, uint64_t mask)
Initialize a native channel layout from a bitmask indicating which channels are present.
Definition: channel_layout.c:421
swr_alloc
av_cold struct SwrContext * swr_alloc(void)
Allocate SwrContext.
Definition: options.c:169
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
av_sample_fmt_is_planar
int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt)
Check if the sample format is planar.
Definition: samplefmt.c:114
swri_rematrix
int swri_rematrix(SwrContext *s, AudioData *out, AudioData *in, int len, int mustcopy)
Definition: rematrix.c:606
SWR_DITHER_NS
@ SWR_DITHER_NS
not part of API/ABI
Definition: swresample.h:154
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
AudioData::planar
int planar
1 if planar audio, 0 otherwise
Definition: swresample_internal.h:51
SwrContext::in_convert
struct AudioConvert * in_convert
input conversion context
Definition: swresample_internal.h:170
channel_map
static const uint8_t channel_map[8][8]
Definition: atrac3plusdec.c:51
swri_audio_convert
int swri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, int len)
Convert between audio sample formats.
av_get_sample_fmt_name
const char * av_get_sample_fmt_name(enum AVSampleFormat sample_fmt)
Return the name of sample_fmt, or NULL if sample_fmt is not recognized.
Definition: samplefmt.c:51
SwrContext
The libswresample context.
Definition: swresample_internal.h:95
AudioData::data
uint8_t * data
samples buffer
Definition: swresample_internal.h:47
AudioData::ch
uint8_t * ch[SWR_CH_MAX]
samples buffer per channel
Definition: swresample_internal.h:46
if
if(ret)
Definition: filter_design.txt:179
SwrContext::in_ch_layout
AVChannelLayout in_ch_layout
input channel layout
Definition: swresample_internal.h:103
swri_noise_shaping_int32
void swri_noise_shaping_int32(SwrContext *s, AudioData *dsts, const AudioData *srcs, const AudioData *noises, int count)
SwrContext::midbuf
AudioData midbuf
intermediate audio data (postin/preout)
Definition: swresample_internal.h:155
fabs
static __device__ float fabs(float a)
Definition: cuda_runtime.h:182
NULL
#define NULL
Definition: coverity.c:32
reversefill_audiodata
static void reversefill_audiodata(AudioData *out, uint8_t *in_arg[SWR_CH_MAX])
Definition: swresample.c:579
SwrContext::log_ctx
void * log_ctx
parent logging context
Definition: swresample_internal.h:98
double
double
Definition: af_crystalizer.c:131
av_clipf
av_clipf
Definition: af_crystalizer.c:121
resample
static int resample(SwrContext *s, AudioData *out_param, int out_count, const AudioData *in_param, int in_count)
Definition: swresample.c:608
swri_soxr_resampler
struct Resampler const swri_soxr_resampler
Definition: soxr_resample.c:126
ALIGN
#define ALIGN
Definition: swresample.c:30
av_opt_set_int
int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
Definition: opt.c:624
AudioData::ch_count
int ch_count
number of channels
Definition: swresample_internal.h:48
attribute_align_arg
#define attribute_align_arg
Definition: internal.h:50
swr_alloc_set_opts2
int swr_alloc_set_opts2(struct SwrContext **ps, const AVChannelLayout *out_ch_layout, enum AVSampleFormat out_sample_fmt, int out_sample_rate, const AVChannelLayout *in_ch_layout, enum AVSampleFormat in_sample_fmt, int in_sample_rate, int log_offset, void *log_ctx)
Allocate SwrContext if needed and set/reset common parameters.
Definition: swresample.c:85
AV_SAMPLE_FMT_NB
@ AV_SAMPLE_FMT_NB
Number of sample formats. DO NOT USE if linking dynamically.
Definition: samplefmt.h:71
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:313
SwrContext::preout
AudioData preout
pre-output audio data: used for rematrix/resample
Definition: swresample_internal.h:156
av_opt_set_chlayout
int av_opt_set_chlayout(void *obj, const char *name, const AVChannelLayout *channel_layout, int search_flags)
Definition: opt.c:786
SWR_FLAG_RESAMPLE
#define SWR_FLAG_RESAMPLE
Force resampling even if equal sample rate.
Definition: swresample.h:143
swr_drop_output
int swr_drop_output(struct SwrContext *s, int count)
Drops the specified number of output samples.
Definition: swresample.c:953
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:56
size
int size
Definition: twinvq_data.h:10344
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
swr_free
av_cold void swr_free(SwrContext **ss)
Free the given SwrContext and set the pointer to NULL.
Definition: swresample.c:174
swr_convert
int attribute_align_arg swr_convert(struct SwrContext *s, uint8_t **out_arg, int out_count, const uint8_t **in_arg, int in_count)
Convert audio.
Definition: swresample.c:837
swri_rematrix_free
av_cold void swri_rematrix_free(SwrContext *s)
Definition: rematrix.c:599
SWR_ENGINE_SWR
@ SWR_ENGINE_SWR
SW Resampler.
Definition: swresample.h:167
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
swresample_internal.h
swri_audio_convert_alloc
AudioConvert * swri_audio_convert_alloc(enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, const int *ch_map, int flags)
Create an audio sample format converter context.
AV_CHANNEL_ORDER_NATIVE
@ AV_CHANNEL_ORDER_NATIVE
The native channel order, i.e.
Definition: channel_layout.h:118
AV_SAMPLE_FMT_S16P
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
Definition: samplefmt.h:64
av_channel_layout_compare
int av_channel_layout_compare(const AVChannelLayout *chl, const AVChannelLayout *chl1)
Check whether two channel layouts are semantically the same, i.e.
Definition: channel_layout.c:1003
av_channel_layout_default
void av_channel_layout_default(AVChannelLayout *ch_layout, int nb_channels)
Get the default channel layout for a given number of channels.
Definition: channel_layout.c:1033
av_assert2
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:67
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:245
av_get_bytes_per_sample
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition: samplefmt.c:108
internal.h
swri_get_dither
int swri_get_dither(SwrContext *s, void *dst, int len, unsigned seed, enum AVSampleFormat noise_fmt)
Definition: dither.c:26
av_assert1
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:56
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:55
SwrContext::max_soft_compensation
float max_soft_compensation
swr maximum soft compensation in seconds over soft_compensation_duration
Definition: swresample_internal.h:145
delta
float delta
Definition: vorbis_enc_data.h:430
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
swri_dither_init
av_cold int swri_dither_init(SwrContext *s, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt)
Definition: dither.c:79
swri_noise_shaping_double
void swri_noise_shaping_double(SwrContext *s, AudioData *dsts, const AudioData *srcs, const AudioData *noises, int count)
clear_context
static void clear_context(SwrContext *s)
Definition: swresample.c:148
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:262
ret
ret
Definition: filter_design.txt:187
RSC
#define RSC
SwrContext::in_sample_fmt
enum AVSampleFormat in_sample_fmt
input sample format
Definition: swresample_internal.h:99
set_audiodata_fmt
static void set_audiodata_fmt(AudioData *a, enum AVSampleFormat fmt)
Definition: swresample.c:135
av_channel_layout_check
int av_channel_layout_check(const AVChannelLayout *channel_layout)
Check whether a channel layout is valid, i.e.
Definition: channel_layout.c:977
U
#define U(x)
Definition: vpx_arith.h:37
AudioData::fmt
enum AVSampleFormat fmt
sample format
Definition: swresample_internal.h:52
channel_layout.h
MAX_DROP_STEP
#define MAX_DROP_STEP
av_channel_layout_uninit
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
Definition: channel_layout.c:662
swri_audio_convert_free
void swri_audio_convert_free(AudioConvert **ctx)
Free audio sample format converter context.
AV_SAMPLE_FMT_DBLP
@ AV_SAMPLE_FMT_DBLP
double, planar
Definition: samplefmt.h:67
swri_rematrix_init
av_cold int swri_rematrix_init(SwrContext *s)
Definition: rematrix.c:493
av_channel_layout_copy
int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
Make a copy of a channel layout.
Definition: channel_layout.c:669
FF_DISABLE_DEPRECATION_WARNINGS
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:72
swr_get_out_samples
int swr_get_out_samples(struct SwrContext *s, int in_samples)
Find an upper bound on the number of samples that the next swr_convert call will output,...
Definition: swresample.c:1000
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
SwrContext::in
AudioData in
input audio data
Definition: swresample_internal.h:153
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
swr_close
av_cold void swr_close(SwrContext *s)
Closes the context so that swr_is_initialized() returns 0.
Definition: swresample.c:189
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
SWR_CH_MAX
#define SWR_CH_MAX
Definition: swresample.c:35
swri_resampler
struct Resampler const swri_resampler
Definition: resample.c:504
audioconvert.h