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