swr: replace deprecated av_opt_set_defaults2() with av_opt_set_defaults().
[ffmpeg.git] / libswresample / swresample.c
1 /*
2  * Copyright (C) 2011 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"
25 #include "libavutil/audioconvert.h"
26
27 #define  C30DB  M_SQRT2
28 #define  C15DB  1.189207115
29 #define C__0DB  1.0
30 #define C_15DB  0.840896415
31 #define C_30DB  M_SQRT1_2
32 #define C_45DB  0.594603558
33 #define C_60DB  0.5
34
35
36 //TODO split options array out?
37 #define OFFSET(x) offsetof(SwrContext,x)
38 static const AVOption options[]={
39 {"ich",  "input channel count", OFFSET( in.ch_count   ), AV_OPT_TYPE_INT, {.dbl=2}, 1, SWR_CH_MAX, 0},
40 {"och", "output channel count", OFFSET(out.ch_count   ), AV_OPT_TYPE_INT, {.dbl=2}, 1, SWR_CH_MAX, 0},
41 {"uch",   "used channel count", OFFSET(used_ch_count  ), AV_OPT_TYPE_INT, {.dbl=0}, 0, SWR_CH_MAX, 0},
42 {"isr",  "input sample rate"  , OFFSET( in_sample_rate), AV_OPT_TYPE_INT, {.dbl=48000}, 1, INT_MAX, 0},
43 {"osr", "output sample rate"  , OFFSET(out_sample_rate), AV_OPT_TYPE_INT, {.dbl=48000}, 1, INT_MAX, 0},
44 //{"ip" ,  "input planar"       , OFFSET( in.planar     ), AV_OPT_TYPE_INT, {.dbl=0},    0,       1, 0},
45 //{"op" , "output planar"       , OFFSET(out.planar     ), AV_OPT_TYPE_INT, {.dbl=0},    0,       1, 0},
46 {"isf",  "input sample format", OFFSET( in_sample_fmt ), AV_OPT_TYPE_INT, {.dbl=AV_SAMPLE_FMT_S16}, 0, AV_SAMPLE_FMT_NB-1+256, 0},
47 {"osf", "output sample format", OFFSET(out_sample_fmt ), AV_OPT_TYPE_INT, {.dbl=AV_SAMPLE_FMT_S16}, 0, AV_SAMPLE_FMT_NB-1+256, 0},
48 {"tsf", "internal sample format", OFFSET(int_sample_fmt ), AV_OPT_TYPE_INT, {.dbl=AV_SAMPLE_FMT_NONE}, -1, AV_SAMPLE_FMT_FLT, 0},
49 {"icl",  "input channel layout" , OFFSET( in_ch_layout), AV_OPT_TYPE_INT64, {.dbl=0}, 0, INT64_MAX, 0, "channel_layout"},
50 {"ocl",  "output channel layout", OFFSET(out_ch_layout), AV_OPT_TYPE_INT64, {.dbl=0}, 0, INT64_MAX, 0, "channel_layout"},
51 {"clev", "center mix level"     , OFFSET(clev)         , AV_OPT_TYPE_FLOAT, {.dbl=C_30DB}, 0, 4, 0},
52 {"slev", "sourround mix level"  , OFFSET(slev)         , AV_OPT_TYPE_FLOAT, {.dbl=C_30DB}, 0, 4, 0},
53 {"rmvol", "rematrix volume"     , OFFSET(rematrix_volume), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, -1000, 1000, 0},
54 {"flags", NULL                  , OFFSET(flags)        , AV_OPT_TYPE_FLAGS, {.dbl=0}, 0,  UINT_MAX, 0, "flags"},
55 {"res", "force resampling", 0, AV_OPT_TYPE_CONST, {.dbl=SWR_FLAG_RESAMPLE}, INT_MIN, INT_MAX, 0, "flags"},
56
57 {0}
58 };
59
60 static const char* context_to_name(void* ptr) {
61     return "SWR";
62 }
63
64 static const AVClass av_class = {
65     .class_name                = "SwrContext",
66     .item_name                 = context_to_name,
67     .option                    = options,
68     .version                   = LIBAVUTIL_VERSION_INT,
69     .log_level_offset_offset   = OFFSET(log_level_offset),
70     .parent_log_context_offset = OFFSET(log_ctx),
71 };
72
73 static int resample(SwrContext *s, AudioData *out_param, int out_count,
74                              const AudioData * in_param, int in_count);
75
76 SwrContext *swr_alloc(void){
77     SwrContext *s= av_mallocz(sizeof(SwrContext));
78     if(s){
79         s->av_class= &av_class;
80         av_opt_set_defaults(s);
81     }
82     return s;
83 }
84
85 SwrContext *swr_alloc2(struct SwrContext *s, int64_t out_ch_layout, enum AVSampleFormat out_sample_fmt, int out_sample_rate,
86                        int64_t  in_ch_layout, enum AVSampleFormat  in_sample_fmt, int  in_sample_rate,
87                        const int *channel_map, int log_offset, void *log_ctx){
88     if(!s) s= swr_alloc();
89     if(!s) return NULL;
90
91     s->log_level_offset= log_offset;
92     s->log_ctx= log_ctx;
93
94     av_set_int(s, "ocl", out_ch_layout);
95     av_set_int(s, "osf", out_sample_fmt);
96     av_set_int(s, "osr", out_sample_rate);
97     av_set_int(s, "icl", in_ch_layout);
98     av_set_int(s, "isf", in_sample_fmt);
99     av_set_int(s, "isr", in_sample_rate);
100
101     s->channel_map = channel_map;
102     s-> in.ch_count= av_get_channel_layout_nb_channels(s-> in_ch_layout);
103     s->out.ch_count= av_get_channel_layout_nb_channels(s->out_ch_layout);
104     s->int_sample_fmt = AV_SAMPLE_FMT_S16;
105
106     return s;
107 }
108
109
110 static void free_temp(AudioData *a){
111     av_free(a->data);
112     memset(a, 0, sizeof(*a));
113 }
114
115 void swr_free(SwrContext **ss){
116     SwrContext *s= *ss;
117     if(s){
118         free_temp(&s->postin);
119         free_temp(&s->midbuf);
120         free_temp(&s->preout);
121         free_temp(&s->in_buffer);
122         swr_audio_convert_free(&s-> in_convert);
123         swr_audio_convert_free(&s->out_convert);
124         swr_audio_convert_free(&s->full_convert);
125         swr_resample_free(&s->resample);
126     }
127
128     av_freep(ss);
129 }
130
131 int swr_init(SwrContext *s){
132     s->in_buffer_index= 0;
133     s->in_buffer_count= 0;
134     s->resample_in_constraint= 0;
135     free_temp(&s->postin);
136     free_temp(&s->midbuf);
137     free_temp(&s->preout);
138     free_temp(&s->in_buffer);
139     swr_audio_convert_free(&s-> in_convert);
140     swr_audio_convert_free(&s->out_convert);
141     swr_audio_convert_free(&s->full_convert);
142
143     s-> in.planar= s-> in_sample_fmt >= 0x100;
144     s->out.planar= s->out_sample_fmt >= 0x100;
145     s-> in_sample_fmt &= 0xFF;
146     s->out_sample_fmt &= 0xFF;
147
148     if(s-> in_sample_fmt >= AV_SAMPLE_FMT_NB){
149         av_log(s, AV_LOG_ERROR, "Requested sample format %s is invalid\n", av_get_sample_fmt_name(s->in_sample_fmt));
150         return AVERROR(EINVAL);
151     }
152     if(s->out_sample_fmt >= AV_SAMPLE_FMT_NB){
153         av_log(s, AV_LOG_ERROR, "Requested sample format %s is invalid\n", av_get_sample_fmt_name(s->out_sample_fmt));
154         return AVERROR(EINVAL);
155     }
156
157     if(   s->int_sample_fmt != AV_SAMPLE_FMT_S16
158         &&s->int_sample_fmt != AV_SAMPLE_FMT_FLT){
159         av_log(s, AV_LOG_ERROR, "Requested sample format %s is not supported internally, only float & S16 is supported\n", av_get_sample_fmt_name(s->int_sample_fmt));
160         return AVERROR(EINVAL);
161     }
162
163     //FIXME should we allow/support using FLT on material that doesnt need it ?
164     if(s->in_sample_fmt <= AV_SAMPLE_FMT_S16 || s->int_sample_fmt==AV_SAMPLE_FMT_S16){
165         s->int_sample_fmt= AV_SAMPLE_FMT_S16;
166     }else
167         s->int_sample_fmt= AV_SAMPLE_FMT_FLT;
168
169
170     if (s->out_sample_rate!=s->in_sample_rate || (s->flags & SWR_FLAG_RESAMPLE)){
171         s->resample = swr_resample_init(s->resample, s->out_sample_rate, s->in_sample_rate, 16, 10, 0, 0.8);
172     }else
173         swr_resample_free(&s->resample);
174     if(s->int_sample_fmt != AV_SAMPLE_FMT_S16 && s->resample){
175         av_log(s, AV_LOG_ERROR, "Resampling only supported with internal s16 currently\n"); //FIXME
176         return -1;
177     }
178
179     if(!s->used_ch_count)
180         s->used_ch_count= s->in.ch_count;
181
182     if(s->used_ch_count && s-> in_ch_layout && s->used_ch_count != av_get_channel_layout_nb_channels(s-> in_ch_layout)){
183         av_log(s, AV_LOG_WARNING, "Input channel layout has a different number of channels than the number of used channels, ignoring layout\n");
184         s-> in_ch_layout= 0;
185     }
186
187     if(!s-> in_ch_layout)
188         s-> in_ch_layout= av_get_default_channel_layout(s->used_ch_count);
189     if(!s->out_ch_layout)
190         s->out_ch_layout= av_get_default_channel_layout(s->out.ch_count);
191
192     s->rematrix= s->out_ch_layout  !=s->in_ch_layout || s->rematrix_volume!=1.0;
193
194 #define RSC 1 //FIXME finetune
195     if(!s-> in.ch_count)
196         s-> in.ch_count= av_get_channel_layout_nb_channels(s-> in_ch_layout);
197     if(!s->used_ch_count)
198         s->used_ch_count= s->in.ch_count;
199     if(!s->out.ch_count)
200         s->out.ch_count= av_get_channel_layout_nb_channels(s->out_ch_layout);
201
202 av_assert0(s-> in.ch_count);
203 av_assert0(s->used_ch_count);
204 av_assert0(s->out.ch_count);
205     s->resample_first= RSC*s->out.ch_count/s->in.ch_count - RSC < s->out_sample_rate/(float)s-> in_sample_rate - 1.0;
206
207     s-> in.bps= av_get_bytes_per_sample(s-> in_sample_fmt);
208     s->int_bps= av_get_bytes_per_sample(s->int_sample_fmt);
209     s->out.bps= av_get_bytes_per_sample(s->out_sample_fmt);
210
211     if(!s->resample && !s->rematrix && !s->channel_map){
212         s->full_convert = swr_audio_convert_alloc(s->out_sample_fmt,
213                                                   s-> in_sample_fmt, s-> in.ch_count, NULL, 0);
214         return 0;
215     }
216
217     s->in_convert = swr_audio_convert_alloc(s->int_sample_fmt,
218                                             s-> in_sample_fmt, s->used_ch_count, s->channel_map, 0);
219     s->out_convert= swr_audio_convert_alloc(s->out_sample_fmt,
220                                             s->int_sample_fmt, s->out.ch_count, NULL, 0);
221
222
223     s->postin= s->in;
224     s->preout= s->out;
225     s->midbuf= s->in;
226     s->in_buffer= s->in;
227     if(s->channel_map){
228         s->postin.ch_count=
229         s->midbuf.ch_count=
230         s->in_buffer.ch_count= s->used_ch_count;
231     }
232     if(!s->resample_first){
233         s->midbuf.ch_count= s->out.ch_count;
234         s->in_buffer.ch_count = s->out.ch_count;
235     }
236
237     s->in_buffer.bps = s->postin.bps = s->midbuf.bps = s->preout.bps =  s->int_bps;
238     s->in_buffer.planar = s->postin.planar = s->midbuf.planar = s->preout.planar =  1;
239
240
241     if(s->rematrix && swr_rematrix_init(s)<0)
242         return -1;
243
244     return 0;
245 }
246
247 static int realloc_audio(AudioData *a, int count){
248     int i, countb;
249     AudioData old;
250
251     if(a->count >= count)
252         return 0;
253
254     count*=2;
255
256     countb= FFALIGN(count*a->bps, 32);
257     old= *a;
258
259     av_assert0(a->planar);
260     av_assert0(a->bps);
261     av_assert0(a->ch_count);
262
263     a->data= av_malloc(countb*a->ch_count);
264     if(!a->data)
265         return AVERROR(ENOMEM);
266     for(i=0; i<a->ch_count; i++){
267         a->ch[i]= a->data + i*(a->planar ? countb : a->bps);
268         if(a->planar) memcpy(a->ch[i], old.ch[i], a->count*a->bps);
269     }
270     av_free(old.data);
271     a->count= count;
272
273     return 1;
274 }
275
276 static void copy(AudioData *out, AudioData *in,
277                  int count){
278     av_assert0(out->planar == in->planar);
279     av_assert0(out->bps == in->bps);
280     av_assert0(out->ch_count == in->ch_count);
281     if(out->planar){
282         int ch;
283         for(ch=0; ch<out->ch_count; ch++)
284             memcpy(out->ch[ch], in->ch[ch], count*out->bps);
285     }else
286         memcpy(out->ch[0], in->ch[0], count*out->ch_count*out->bps);
287 }
288
289 static void fill_audiodata(AudioData *out, uint8_t *in_arg [SWR_CH_MAX]){
290     int i;
291     if(out->planar){
292         for(i=0; i<out->ch_count; i++)
293             out->ch[i]= in_arg[i];
294     }else{
295         for(i=0; i<out->ch_count; i++)
296             out->ch[i]= in_arg[0] + i*out->bps;
297     }
298 }
299
300 int swr_convert(struct SwrContext *s, uint8_t *out_arg[SWR_CH_MAX], int out_count,
301                          const uint8_t *in_arg [SWR_CH_MAX], int  in_count){
302     AudioData *postin, *midbuf, *preout;
303     int ret/*, in_max*/;
304     AudioData * in= &s->in;
305     AudioData *out= &s->out;
306     AudioData preout_tmp, midbuf_tmp;
307
308     if(!s->resample){
309         if(in_count > out_count)
310             return -1;
311         out_count = in_count;
312     }
313
314     if(!in_arg){
315         if(s->in_buffer_count){
316             AudioData *a= &s->in_buffer;
317             int i, j, ret;
318             if((ret=realloc_audio(a, s->in_buffer_index + 2*s->in_buffer_count)) < 0)
319                 return ret;
320             av_assert0(a->planar);
321             for(i=0; i<a->ch_count; i++){
322                 for(j=0; j<s->in_buffer_count; j++){
323                     memcpy(a->ch[i] + (s->in_buffer_index+s->in_buffer_count+j  )*a->bps,
324                            a->ch[i] + (s->in_buffer_index+s->in_buffer_count-j-1)*a->bps, a->bps);
325                 }
326             }
327             s->in_buffer_count += (s->in_buffer_count+1)/2;
328             s->resample_in_constraint = 0;
329         }else{
330             return 0;
331         }
332     }else
333         fill_audiodata(in ,  (void*)in_arg);
334     fill_audiodata(out, out_arg);
335
336     if(s->full_convert){
337         av_assert0(!s->resample);
338         swr_audio_convert(s->full_convert, out, in, in_count);
339         return out_count;
340     }
341
342 //     in_max= out_count*(int64_t)s->in_sample_rate / s->out_sample_rate + resample_filter_taps;
343 //     in_count= FFMIN(in_count, in_in + 2 - s->hist_buffer_count);
344
345     if((ret=realloc_audio(&s->postin, in_count))<0)
346         return ret;
347     if(s->resample_first){
348         av_assert0(s->midbuf.ch_count == s->used_ch_count);
349         if((ret=realloc_audio(&s->midbuf, out_count))<0)
350             return ret;
351     }else{
352         av_assert0(s->midbuf.ch_count ==  s->out.ch_count);
353         if((ret=realloc_audio(&s->midbuf,  in_count))<0)
354             return ret;
355     }
356     if((ret=realloc_audio(&s->preout, out_count))<0)
357         return ret;
358
359     postin= &s->postin;
360
361     midbuf_tmp= s->midbuf;
362     midbuf= &midbuf_tmp;
363     preout_tmp= s->preout;
364     preout= &preout_tmp;
365
366     if(s->int_sample_fmt == s-> in_sample_fmt && s->in.planar)
367         postin= in;
368
369     if(s->resample_first ? !s->resample : !s->rematrix)
370         midbuf= postin;
371
372     if(s->resample_first ? !s->rematrix : !s->resample)
373         preout= midbuf;
374
375     if(s->int_sample_fmt == s->out_sample_fmt && s->out.planar){
376         if(preout==in){
377             out_count= FFMIN(out_count, in_count); //TODO check at teh end if this is needed or redundant
378             av_assert0(s->in.planar); //we only support planar internally so it has to be, we support copying non planar though
379             copy(out, in, out_count);
380             return out_count;
381         }
382         else if(preout==postin) preout= midbuf= postin= out;
383         else if(preout==midbuf) preout= midbuf= out;
384         else                    preout= out;
385     }
386
387     if(in != postin){
388         swr_audio_convert(s->in_convert, postin, in, in_count);
389     }
390
391     if(s->resample_first){
392         if(postin != midbuf)
393             out_count= resample(s, midbuf, out_count, postin, in_count);
394         if(midbuf != preout)
395             swr_rematrix(s, preout, midbuf, out_count, preout==out);
396     }else{
397         if(postin != midbuf)
398             swr_rematrix(s, midbuf, postin, in_count, midbuf==out);
399         if(midbuf != preout)
400             out_count= resample(s, preout, out_count, midbuf, in_count);
401     }
402
403     if(preout != out){
404 //FIXME packed doesnt need more than 1 chan here!
405         swr_audio_convert(s->out_convert, out, preout, out_count);
406     }
407     if(!in_arg)
408         s->in_buffer_count = 0;
409     return out_count;
410 }
411
412 /**
413  *
414  * out may be equal in.
415  */
416 static void buf_set(AudioData *out, AudioData *in, int count){
417     if(in->planar){
418         int ch;
419         for(ch=0; ch<out->ch_count; ch++)
420             out->ch[ch]= in->ch[ch] + count*out->bps;
421     }else
422         out->ch[0]= in->ch[0] + count*out->ch_count*out->bps;
423 }
424
425 /**
426  *
427  * @return number of samples output per channel
428  */
429 static int resample(SwrContext *s, AudioData *out_param, int out_count,
430                              const AudioData * in_param, int in_count){
431     AudioData in, out, tmp;
432     int ret_sum=0;
433     int border=0;
434
435     tmp=out=*out_param;
436     in =  *in_param;
437
438     do{
439         int ret, size, consumed;
440         if(!s->resample_in_constraint && s->in_buffer_count){
441             buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
442             ret= swr_multiple_resample(s->resample, &out, out_count, &tmp, s->in_buffer_count, &consumed);
443             out_count -= ret;
444             ret_sum += ret;
445             buf_set(&out, &out, ret);
446             s->in_buffer_count -= consumed;
447             s->in_buffer_index += consumed;
448
449             if(!in_count)
450                 break;
451             if(s->in_buffer_count <= border){
452                 buf_set(&in, &in, -s->in_buffer_count);
453                 in_count += s->in_buffer_count;
454                 s->in_buffer_count=0;
455                 s->in_buffer_index=0;
456                 border = 0;
457             }
458         }
459
460         if(in_count && !s->in_buffer_count){
461             s->in_buffer_index=0;
462             ret= swr_multiple_resample(s->resample, &out, out_count, &in, in_count, &consumed);
463             out_count -= ret;
464             ret_sum += ret;
465             buf_set(&out, &out, ret);
466             in_count -= consumed;
467             buf_set(&in, &in, consumed);
468         }
469
470         //TODO is this check sane considering the advanced copy avoidance below
471         size= s->in_buffer_index + s->in_buffer_count + in_count;
472         if(   size > s->in_buffer.count
473            && s->in_buffer_count + in_count <= s->in_buffer_index){
474             buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
475             copy(&s->in_buffer, &tmp, s->in_buffer_count);
476             s->in_buffer_index=0;
477         }else
478             if((ret=realloc_audio(&s->in_buffer, size)) < 0)
479                 return ret;
480
481         if(in_count){
482             int count= in_count;
483             if(s->in_buffer_count && s->in_buffer_count+2 < count && out_count) count= s->in_buffer_count+2;
484
485             buf_set(&tmp, &s->in_buffer, s->in_buffer_index + s->in_buffer_count);
486             copy(&tmp, &in, /*in_*/count);
487             s->in_buffer_count += count;
488             in_count -= count;
489             border += count;
490             buf_set(&in, &in, count);
491             s->resample_in_constraint= 0;
492             if(s->in_buffer_count != count || in_count)
493                 continue;
494         }
495         break;
496     }while(1);
497
498     s->resample_in_constraint= !!out_count;
499
500     return ret_sum;
501 }