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