]> git.sesse.net Git - ffmpeg/blob - libswresample/swresample.c
414d84cafb2cdd8099d504e8d946b606fba7e105
[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 #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 #define PARAM AV_OPT_FLAG_AUDIO_PARAM
39
40 static const AVOption options[]={
41 {"ich"                  ,  "Input Channel Count"        , OFFSET( in.ch_count   ), AV_OPT_TYPE_INT  , {.dbl=2                     }, 0      , SWR_CH_MAX, PARAM},
42 {"in_channel_count"     ,  "Input Channel Count"        , OFFSET( in.ch_count   ), AV_OPT_TYPE_INT  , {.dbl=2                     }, 0      , SWR_CH_MAX, PARAM},
43 {"och"                  , "Output Channel Count"        , OFFSET(out.ch_count   ), AV_OPT_TYPE_INT  , {.dbl=2                     }, 0      , SWR_CH_MAX, PARAM},
44 {"out_channel_count"    , "Output Channel Count"        , OFFSET(out.ch_count   ), AV_OPT_TYPE_INT  , {.dbl=2                     }, 0      , SWR_CH_MAX, PARAM},
45 {"uch"                  ,   "Used Channel Count"        , OFFSET(used_ch_count  ), AV_OPT_TYPE_INT  , {.dbl=0                     }, 0      , SWR_CH_MAX, PARAM},
46 {"used_channel_count"   ,   "Used Channel Count"        , OFFSET(used_ch_count  ), AV_OPT_TYPE_INT  , {.dbl=0                     }, 0      , SWR_CH_MAX, PARAM},
47 {"isr"                  ,  "Input Sample Rate"          , OFFSET( in_sample_rate), AV_OPT_TYPE_INT  , {.dbl=48000                 }, 1      , INT_MAX   , PARAM},
48 {"in_sample_rate"       ,  "Input Sample Rate"          , OFFSET( in_sample_rate), AV_OPT_TYPE_INT  , {.dbl=48000                 }, 1      , INT_MAX   , PARAM},
49 {"osr"                  , "Output Sample Rate"          , OFFSET(out_sample_rate), AV_OPT_TYPE_INT  , {.dbl=48000                 }, 1      , INT_MAX   , PARAM},
50 {"out_sample_rate"      , "Output Sample Rate"          , OFFSET(out_sample_rate), AV_OPT_TYPE_INT  , {.dbl=48000                 }, 1      , INT_MAX   , PARAM},
51 {"isf"                  ,    "Input Sample Format"      , OFFSET( in_sample_fmt ), AV_OPT_TYPE_INT  , {.dbl=AV_SAMPLE_FMT_S16     }, 0      , AV_SAMPLE_FMT_NB-1+256, PARAM},
52 {"in_sample_fmt"        ,    "Input Sample Format"      , OFFSET( in_sample_fmt ), AV_OPT_TYPE_INT  , {.dbl=AV_SAMPLE_FMT_S16     }, 0      , AV_SAMPLE_FMT_NB-1+256, PARAM},
53 {"osf"                  ,   "Output Sample Format"      , OFFSET(out_sample_fmt ), AV_OPT_TYPE_INT  , {.dbl=AV_SAMPLE_FMT_S16     }, 0      , AV_SAMPLE_FMT_NB-1+256, PARAM},
54 {"out_sample_fmt"       ,   "Output Sample Format"      , OFFSET(out_sample_fmt ), AV_OPT_TYPE_INT  , {.dbl=AV_SAMPLE_FMT_S16     }, 0      , AV_SAMPLE_FMT_NB-1+256, PARAM},
55 {"tsf"                  , "Internal Sample Format"      , OFFSET(int_sample_fmt ), AV_OPT_TYPE_INT  , {.dbl=AV_SAMPLE_FMT_NONE    }, -1     , AV_SAMPLE_FMT_FLT, PARAM},
56 {"internal_sample_fmt"  , "Internal Sample Format"      , OFFSET(int_sample_fmt ), AV_OPT_TYPE_INT  , {.dbl=AV_SAMPLE_FMT_NONE    }, -1     , AV_SAMPLE_FMT_FLT, PARAM},
57 {"icl"                  ,   "Input Channel Layout"      , OFFSET( in_ch_layout  ), AV_OPT_TYPE_INT64, {.dbl=0                     }, 0      , INT64_MAX , PARAM, "channel_layout"},
58 {"in_channel_layout"    ,   "Input Channel Layout"      , OFFSET( in_ch_layout  ), AV_OPT_TYPE_INT64, {.dbl=0                     }, 0      , INT64_MAX , PARAM, "channel_layout"},
59 {"ocl"                  ,  "Output Channel Layout"      , OFFSET(out_ch_layout  ), AV_OPT_TYPE_INT64, {.dbl=0                     }, 0      , INT64_MAX , PARAM, "channel_layout"},
60 {"out_channel_layout"   ,  "Output Channel Layout"      , OFFSET(out_ch_layout  ), AV_OPT_TYPE_INT64, {.dbl=0                     }, 0      , INT64_MAX , PARAM, "channel_layout"},
61 {"clev"                 ,    "Center Mix Level"         , OFFSET(clev           ), AV_OPT_TYPE_FLOAT, {.dbl=C_30DB                }, -32    , 32        , PARAM},
62 {"center_mix_level"     ,    "Center Mix Level"         , OFFSET(clev           ), AV_OPT_TYPE_FLOAT, {.dbl=C_30DB                }, -32    , 32        , PARAM},
63 {"slev"                 , "Sourround Mix Level"         , OFFSET(slev           ), AV_OPT_TYPE_FLOAT, {.dbl=C_30DB                }, -32    , 32        , PARAM},
64 {"surround_mix_level"   , "Sourround Mix Level"         , OFFSET(slev           ), AV_OPT_TYPE_FLOAT, {.dbl=C_30DB                }, -32    , 32        , PARAM},
65 {"rmvol"                , "Rematrix Volume"             , OFFSET(rematrix_volume), AV_OPT_TYPE_FLOAT, {.dbl=1.0                   }, -1000  , 1000      , PARAM},
66 {"rematrix_volume"      , "Rematrix Volume"             , OFFSET(rematrix_volume), AV_OPT_TYPE_FLOAT, {.dbl=1.0                   }, -1000  , 1000      , PARAM},
67 {"flags"                , NULL                          , OFFSET(flags          ), AV_OPT_TYPE_FLAGS, {.dbl=0                     }, 0      , UINT_MAX  , PARAM, "flags"},
68 {"swr_flags"            , NULL                          , OFFSET(flags          ), AV_OPT_TYPE_FLAGS, {.dbl=0                     }, 0      , UINT_MAX  , PARAM, "flags"},
69 {"res"                  , "Force Resampling"            , 0                      , AV_OPT_TYPE_CONST, {.dbl=SWR_FLAG_RESAMPLE     }, INT_MIN, INT_MAX   , PARAM, "flags"},
70 {"dither_scale"         , "Dither Scale"                , OFFSET(dither_scale   ), AV_OPT_TYPE_FLOAT, {.dbl=1                     }, 0      , INT_MAX   , PARAM},
71 {"dither_method"        , "Dither Method"               , OFFSET(dither_method  ), AV_OPT_TYPE_INT  , {.dbl=0                     }, 0      , SWR_DITHER_NB-1, PARAM, "dither_method"},
72 {"rectangular"          , "Rectangular Dither"          , 0                      , AV_OPT_TYPE_CONST, {.dbl=SWR_DITHER_RECTANGULAR}, INT_MIN, INT_MAX   , PARAM, "dither_method"},
73 {"triangular"           ,  "Triangular Dither"          , 0                      , AV_OPT_TYPE_CONST, {.dbl=SWR_DITHER_TRIANGULAR }, INT_MIN, INT_MAX   , PARAM, "dither_method"},
74 {"triangular_hp"        , "Triangular Dither With High Pass" , 0                 , AV_OPT_TYPE_CONST, {.dbl=SWR_DITHER_TRIANGULAR_HIGHPASS }, INT_MIN, INT_MAX, PARAM, "dither_method"},
75 {"filter_size"          , "Resampling Filter Size"      , OFFSET(filter_size)    , AV_OPT_TYPE_INT  , {.dbl=16                    }, 0      , INT_MAX   , PARAM },
76 {"phase_shift"          , "Resampling Phase Shift"      , OFFSET(phase_shift)    , AV_OPT_TYPE_INT  , {.dbl=10                    }, 0      , 30        , PARAM },
77 {"linear_interp"        , "Use Linear Interpolation"    , OFFSET(linear_interp)  , AV_OPT_TYPE_INT  , {.dbl=0                     }, 0      , 1         , PARAM },
78 {"cutoff"               , "Cutoff Frequency Ratio"      , OFFSET(cutoff)         , AV_OPT_TYPE_DOUBLE,{.dbl=0.8                   }, 0      , 1         , PARAM },
79 {0}
80 };
81
82 static const char* context_to_name(void* ptr) {
83     return "SWR";
84 }
85
86 static const AVClass av_class = {
87     .class_name                = "SwrContext",
88     .item_name                 = context_to_name,
89     .option                    = options,
90     .version                   = LIBAVUTIL_VERSION_INT,
91     .log_level_offset_offset   = OFFSET(log_level_offset),
92     .parent_log_context_offset = OFFSET(log_ctx),
93 };
94
95 unsigned swresample_version(void)
96 {
97     av_assert0(LIBSWRESAMPLE_VERSION_MICRO >= 100);
98     return LIBSWRESAMPLE_VERSION_INT;
99 }
100
101 const char *swresample_configuration(void)
102 {
103     return FFMPEG_CONFIGURATION;
104 }
105
106 const char *swresample_license(void)
107 {
108 #define LICENSE_PREFIX "libswresample license: "
109     return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
110 }
111
112 int swr_set_channel_mapping(struct SwrContext *s, const int *channel_map){
113     if(!s || s->in_convert) // s needs to be allocated but not initialized
114         return AVERROR(EINVAL);
115     s->channel_map = channel_map;
116     return 0;
117 }
118
119 const AVClass *swr_get_class(void)
120 {
121     return &av_class;
122 }
123
124 struct SwrContext *swr_alloc(void){
125     SwrContext *s= av_mallocz(sizeof(SwrContext));
126     if(s){
127         s->av_class= &av_class;
128         av_opt_set_defaults(s);
129     }
130     return s;
131 }
132
133 struct SwrContext *swr_alloc_set_opts(struct SwrContext *s,
134                                       int64_t out_ch_layout, enum AVSampleFormat out_sample_fmt, int out_sample_rate,
135                                       int64_t  in_ch_layout, enum AVSampleFormat  in_sample_fmt, int  in_sample_rate,
136                                       int log_offset, void *log_ctx){
137     if(!s) s= swr_alloc();
138     if(!s) return NULL;
139
140     s->log_level_offset= log_offset;
141     s->log_ctx= log_ctx;
142
143     av_opt_set_int(s, "ocl", out_ch_layout,   0);
144     av_opt_set_int(s, "osf", out_sample_fmt,  0);
145     av_opt_set_int(s, "osr", out_sample_rate, 0);
146     av_opt_set_int(s, "icl", in_ch_layout,    0);
147     av_opt_set_int(s, "isf", in_sample_fmt,   0);
148     av_opt_set_int(s, "isr", in_sample_rate,  0);
149     av_opt_set_int(s, "tsf", AV_SAMPLE_FMT_NONE,   0);
150     av_opt_set_int(s, "ich", av_get_channel_layout_nb_channels(s-> in_ch_layout), 0);
151     av_opt_set_int(s, "och", av_get_channel_layout_nb_channels(s->out_ch_layout), 0);
152     av_opt_set_int(s, "uch", 0, 0);
153     return s;
154 }
155
156
157 static void free_temp(AudioData *a){
158     av_free(a->data);
159     memset(a, 0, sizeof(*a));
160 }
161
162 void swr_free(SwrContext **ss){
163     SwrContext *s= *ss;
164     if(s){
165         free_temp(&s->postin);
166         free_temp(&s->midbuf);
167         free_temp(&s->preout);
168         free_temp(&s->in_buffer);
169         free_temp(&s->dither);
170         swri_audio_convert_free(&s-> in_convert);
171         swri_audio_convert_free(&s->out_convert);
172         swri_audio_convert_free(&s->full_convert);
173         swri_resample_free(&s->resample);
174     }
175
176     av_freep(ss);
177 }
178
179 int swr_init(struct SwrContext *s){
180     s->in_buffer_index= 0;
181     s->in_buffer_count= 0;
182     s->resample_in_constraint= 0;
183     free_temp(&s->postin);
184     free_temp(&s->midbuf);
185     free_temp(&s->preout);
186     free_temp(&s->in_buffer);
187     free_temp(&s->dither);
188     swri_audio_convert_free(&s-> in_convert);
189     swri_audio_convert_free(&s->out_convert);
190     swri_audio_convert_free(&s->full_convert);
191
192     s->flushed = 0;
193
194     s-> in.planar= av_sample_fmt_is_planar(s-> in_sample_fmt);
195     s->out.planar= av_sample_fmt_is_planar(s->out_sample_fmt);
196     s-> in_sample_fmt= av_get_alt_sample_fmt(s-> in_sample_fmt, 0);
197     s->out_sample_fmt= av_get_alt_sample_fmt(s->out_sample_fmt, 0);
198
199     if(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(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     //FIXME should we allow/support using FLT on material that doesnt need it ?
209     if(s->in_sample_fmt <= AV_SAMPLE_FMT_S16 || s->int_sample_fmt==AV_SAMPLE_FMT_S16){
210         s->int_sample_fmt= AV_SAMPLE_FMT_S16;
211     }else
212         s->int_sample_fmt= AV_SAMPLE_FMT_FLT;
213
214     if(   s->int_sample_fmt != AV_SAMPLE_FMT_S16
215         &&s->int_sample_fmt != AV_SAMPLE_FMT_S32
216         &&s->int_sample_fmt != AV_SAMPLE_FMT_FLT){
217         av_log(s, AV_LOG_ERROR, "Requested sample format %s is not supported internally, S16/S32/FLT is supported\n", av_get_sample_fmt_name(s->int_sample_fmt));
218         return AVERROR(EINVAL);
219     }
220
221     if (s->out_sample_rate!=s->in_sample_rate || (s->flags & SWR_FLAG_RESAMPLE)){
222         s->resample = swri_resample_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);
223     }else
224         swri_resample_free(&s->resample);
225     if(    s->int_sample_fmt != AV_SAMPLE_FMT_S16
226         && s->int_sample_fmt != AV_SAMPLE_FMT_S32
227         && s->int_sample_fmt != AV_SAMPLE_FMT_FLT
228         && s->resample){
229         av_log(s, AV_LOG_ERROR, "Resampling only supported with internal s16/s32/flt\n");
230         return -1;
231     }
232
233     if(!s->used_ch_count)
234         s->used_ch_count= s->in.ch_count;
235
236     if(s->used_ch_count && s-> in_ch_layout && s->used_ch_count != av_get_channel_layout_nb_channels(s-> in_ch_layout)){
237         av_log(s, AV_LOG_WARNING, "Input channel layout has a different number of channels than the number of used channels, ignoring layout\n");
238         s-> in_ch_layout= 0;
239     }
240
241     if(!s-> in_ch_layout)
242         s-> in_ch_layout= av_get_default_channel_layout(s->used_ch_count);
243     if(!s->out_ch_layout)
244         s->out_ch_layout= av_get_default_channel_layout(s->out.ch_count);
245
246     s->rematrix= s->out_ch_layout  !=s->in_ch_layout || s->rematrix_volume!=1.0 ||
247                  s->rematrix_custom;
248
249 #define RSC 1 //FIXME finetune
250     if(!s-> in.ch_count)
251         s-> in.ch_count= av_get_channel_layout_nb_channels(s-> in_ch_layout);
252     if(!s->used_ch_count)
253         s->used_ch_count= s->in.ch_count;
254     if(!s->out.ch_count)
255         s->out.ch_count= av_get_channel_layout_nb_channels(s->out_ch_layout);
256
257     if(!s-> in.ch_count){
258         av_assert0(!s->in_ch_layout);
259         av_log(s, AV_LOG_ERROR, "Input channel count and layout are unset\n");
260         return -1;
261     }
262
263     if ((!s->out_ch_layout || !s->in_ch_layout) && s->used_ch_count != s->out.ch_count && !s->rematrix_custom) {
264         av_log(s, AV_LOG_ERROR, "Rematrix is needed but there is not enough information to do it\n");
265         return -1;
266     }
267
268 av_assert0(s->used_ch_count);
269 av_assert0(s->out.ch_count);
270     s->resample_first= RSC*s->out.ch_count/s->in.ch_count - RSC < s->out_sample_rate/(float)s-> in_sample_rate - 1.0;
271
272     s-> in.bps= av_get_bytes_per_sample(s-> in_sample_fmt);
273     s->int_bps= av_get_bytes_per_sample(s->int_sample_fmt);
274     s->out.bps= av_get_bytes_per_sample(s->out_sample_fmt);
275     s->in_buffer= s->in;
276
277     if(!s->resample && !s->rematrix && !s->channel_map){
278         s->full_convert = swri_audio_convert_alloc(s->out_sample_fmt,
279                                                    s-> in_sample_fmt, s-> in.ch_count, NULL, 0);
280         return 0;
281     }
282
283     s->in_convert = swri_audio_convert_alloc(s->int_sample_fmt,
284                                              s-> in_sample_fmt, s->used_ch_count, s->channel_map, 0);
285     s->out_convert= swri_audio_convert_alloc(s->out_sample_fmt,
286                                              s->int_sample_fmt, s->out.ch_count, NULL, 0);
287
288
289     s->postin= s->in;
290     s->preout= s->out;
291     s->midbuf= s->in;
292
293     if(s->channel_map){
294         s->postin.ch_count=
295         s->midbuf.ch_count= s->used_ch_count;
296         if(s->resample)
297             s->in_buffer.ch_count= s->used_ch_count;
298     }
299     if(!s->resample_first){
300         s->midbuf.ch_count= s->out.ch_count;
301         if(s->resample)
302             s->in_buffer.ch_count = s->out.ch_count;
303     }
304
305     s->postin.bps    = s->midbuf.bps    = s->preout.bps    =  s->int_bps;
306     s->postin.planar = s->midbuf.planar = s->preout.planar =  1;
307
308     if(s->resample){
309         s->in_buffer.bps    = s->int_bps;
310         s->in_buffer.planar = 1;
311     }
312
313     s->dither = s->preout;
314
315     if(s->rematrix)
316         return swri_rematrix_init(s);
317
318     return 0;
319 }
320
321 static int realloc_audio(AudioData *a, int count){
322     int i, countb;
323     AudioData old;
324
325     if(a->count >= count)
326         return 0;
327
328     count*=2;
329
330     countb= FFALIGN(count*a->bps, 32);
331     old= *a;
332
333     av_assert0(a->bps);
334     av_assert0(a->ch_count);
335
336     a->data= av_malloc(countb*a->ch_count);
337     if(!a->data)
338         return AVERROR(ENOMEM);
339     for(i=0; i<a->ch_count; i++){
340         a->ch[i]= a->data + i*(a->planar ? countb : a->bps);
341         if(a->planar) memcpy(a->ch[i], old.ch[i], a->count*a->bps);
342     }
343     if(!a->planar) memcpy(a->ch[0], old.ch[0], a->count*a->ch_count*a->bps);
344     av_free(old.data);
345     a->count= count;
346
347     return 1;
348 }
349
350 static void copy(AudioData *out, AudioData *in,
351                  int count){
352     av_assert0(out->planar == in->planar);
353     av_assert0(out->bps == in->bps);
354     av_assert0(out->ch_count == in->ch_count);
355     if(out->planar){
356         int ch;
357         for(ch=0; ch<out->ch_count; ch++)
358             memcpy(out->ch[ch], in->ch[ch], count*out->bps);
359     }else
360         memcpy(out->ch[0], in->ch[0], count*out->ch_count*out->bps);
361 }
362
363 static void fill_audiodata(AudioData *out, uint8_t *in_arg [SWR_CH_MAX]){
364     int i;
365     if(out->planar){
366         for(i=0; i<out->ch_count; i++)
367             out->ch[i]= in_arg[i];
368     }else{
369         for(i=0; i<out->ch_count; i++)
370             out->ch[i]= in_arg[0] + i*out->bps;
371     }
372 }
373
374 /**
375  *
376  * out may be equal in.
377  */
378 static void buf_set(AudioData *out, AudioData *in, int count){
379     int ch;
380     if(in->planar){
381         for(ch=0; ch<out->ch_count; ch++)
382             out->ch[ch]= in->ch[ch] + count*out->bps;
383     }else{
384         for(ch=0; ch<out->ch_count; ch++)
385             out->ch[ch]= in->ch[0] + (ch + count*out->ch_count) * out->bps;
386     }
387 }
388
389 /**
390  *
391  * @return number of samples output per channel
392  */
393 static int resample(SwrContext *s, AudioData *out_param, int out_count,
394                              const AudioData * in_param, int in_count){
395     AudioData in, out, tmp;
396     int ret_sum=0;
397     int border=0;
398
399     tmp=out=*out_param;
400     in =  *in_param;
401
402     do{
403         int ret, size, consumed;
404         if(!s->resample_in_constraint && s->in_buffer_count){
405             buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
406             ret= swri_multiple_resample(s->resample, &out, out_count, &tmp, s->in_buffer_count, &consumed);
407             out_count -= ret;
408             ret_sum += ret;
409             buf_set(&out, &out, ret);
410             s->in_buffer_count -= consumed;
411             s->in_buffer_index += consumed;
412
413             if(!in_count)
414                 break;
415             if(s->in_buffer_count <= border){
416                 buf_set(&in, &in, -s->in_buffer_count);
417                 in_count += s->in_buffer_count;
418                 s->in_buffer_count=0;
419                 s->in_buffer_index=0;
420                 border = 0;
421             }
422         }
423
424         if(in_count && !s->in_buffer_count){
425             s->in_buffer_index=0;
426             ret= swri_multiple_resample(s->resample, &out, out_count, &in, in_count, &consumed);
427             out_count -= ret;
428             ret_sum += ret;
429             buf_set(&out, &out, ret);
430             in_count -= consumed;
431             buf_set(&in, &in, consumed);
432         }
433
434         //TODO is this check sane considering the advanced copy avoidance below
435         size= s->in_buffer_index + s->in_buffer_count + in_count;
436         if(   size > s->in_buffer.count
437            && s->in_buffer_count + in_count <= s->in_buffer_index){
438             buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
439             copy(&s->in_buffer, &tmp, s->in_buffer_count);
440             s->in_buffer_index=0;
441         }else
442             if((ret=realloc_audio(&s->in_buffer, size)) < 0)
443                 return ret;
444
445         if(in_count){
446             int count= in_count;
447             if(s->in_buffer_count && s->in_buffer_count+2 < count && out_count) count= s->in_buffer_count+2;
448
449             buf_set(&tmp, &s->in_buffer, s->in_buffer_index + s->in_buffer_count);
450             copy(&tmp, &in, /*in_*/count);
451             s->in_buffer_count += count;
452             in_count -= count;
453             border += count;
454             buf_set(&in, &in, count);
455             s->resample_in_constraint= 0;
456             if(s->in_buffer_count != count || in_count)
457                 continue;
458         }
459         break;
460     }while(1);
461
462     s->resample_in_constraint= !!out_count;
463
464     return ret_sum;
465 }
466
467 static int swr_convert_internal(struct SwrContext *s, AudioData *out, int out_count,
468                                                       AudioData *in , int  in_count){
469     AudioData *postin, *midbuf, *preout;
470     int ret/*, in_max*/;
471     AudioData preout_tmp, midbuf_tmp;
472
473     if(s->full_convert){
474         av_assert0(!s->resample);
475         swri_audio_convert(s->full_convert, out, in, in_count);
476         return out_count;
477     }
478
479 //     in_max= out_count*(int64_t)s->in_sample_rate / s->out_sample_rate + resample_filter_taps;
480 //     in_count= FFMIN(in_count, in_in + 2 - s->hist_buffer_count);
481
482     if((ret=realloc_audio(&s->postin, in_count))<0)
483         return ret;
484     if(s->resample_first){
485         av_assert0(s->midbuf.ch_count == s->used_ch_count);
486         if((ret=realloc_audio(&s->midbuf, out_count))<0)
487             return ret;
488     }else{
489         av_assert0(s->midbuf.ch_count ==  s->out.ch_count);
490         if((ret=realloc_audio(&s->midbuf,  in_count))<0)
491             return ret;
492     }
493     if((ret=realloc_audio(&s->preout, out_count))<0)
494         return ret;
495
496     postin= &s->postin;
497
498     midbuf_tmp= s->midbuf;
499     midbuf= &midbuf_tmp;
500     preout_tmp= s->preout;
501     preout= &preout_tmp;
502
503     if(s->int_sample_fmt == s-> in_sample_fmt && s->in.planar)
504         postin= in;
505
506     if(s->resample_first ? !s->resample : !s->rematrix)
507         midbuf= postin;
508
509     if(s->resample_first ? !s->rematrix : !s->resample)
510         preout= midbuf;
511
512     if(s->int_sample_fmt == s->out_sample_fmt && s->out.planar){
513         if(preout==in){
514             out_count= FFMIN(out_count, in_count); //TODO check at the end if this is needed or redundant
515             av_assert0(s->in.planar); //we only support planar internally so it has to be, we support copying non planar though
516             copy(out, in, out_count);
517             return out_count;
518         }
519         else if(preout==postin) preout= midbuf= postin= out;
520         else if(preout==midbuf) preout= midbuf= out;
521         else                    preout= out;
522     }
523
524     if(in != postin){
525         swri_audio_convert(s->in_convert, postin, in, in_count);
526     }
527
528     if(s->resample_first){
529         if(postin != midbuf)
530             out_count= resample(s, midbuf, out_count, postin, in_count);
531         if(midbuf != preout)
532             swri_rematrix(s, preout, midbuf, out_count, preout==out);
533     }else{
534         if(postin != midbuf)
535             swri_rematrix(s, midbuf, postin, in_count, midbuf==out);
536         if(midbuf != preout)
537             out_count= resample(s, preout, out_count, midbuf, in_count);
538     }
539
540     if(preout != out && out_count){
541         if(s->dither_method){
542             int ch;
543             int dither_count= FFMAX(out_count, 1<<16);
544             av_assert0(preout != in);
545
546             if((ret=realloc_audio(&s->dither, dither_count))<0)
547                 return ret;
548             if(ret)
549                 for(ch=0; ch<s->dither.ch_count; ch++)
550                     swri_get_dither(s, s->dither.ch[ch], s->dither.count, 12345678913579<<ch, s->out_sample_fmt, s->int_sample_fmt);
551             av_assert0(s->dither.ch_count == preout->ch_count);
552
553             if(s->dither_pos + out_count > s->dither.count)
554                 s->dither_pos = 0;
555             for(ch=0; ch<preout->ch_count; ch++)
556                 swri_sum2(s->int_sample_fmt, preout->ch[ch], preout->ch[ch], s->dither.ch[ch] + s->dither.bps * s->dither_pos, 1, 1, out_count);
557
558             s->dither_pos += out_count;
559         }
560 //FIXME packed doesnt need more than 1 chan here!
561         swri_audio_convert(s->out_convert, out, preout, out_count);
562     }
563     return out_count;
564 }
565
566 int swr_convert(struct SwrContext *s, uint8_t *out_arg[SWR_CH_MAX], int out_count,
567                                 const uint8_t *in_arg [SWR_CH_MAX], int  in_count){
568     AudioData * in= &s->in;
569     AudioData *out= &s->out;
570
571     if(!in_arg){
572         if(s->in_buffer_count){
573             if (s->resample && !s->flushed) {
574                 AudioData *a= &s->in_buffer;
575                 int i, j, ret;
576                 if((ret=realloc_audio(a, s->in_buffer_index + 2*s->in_buffer_count)) < 0)
577                     return ret;
578                 av_assert0(a->planar);
579                 for(i=0; i<a->ch_count; i++){
580                     for(j=0; j<s->in_buffer_count; j++){
581                         memcpy(a->ch[i] + (s->in_buffer_index+s->in_buffer_count+j  )*a->bps,
582                             a->ch[i] + (s->in_buffer_index+s->in_buffer_count-j-1)*a->bps, a->bps);
583                     }
584                 }
585                 s->in_buffer_count += (s->in_buffer_count+1)/2;
586                 s->resample_in_constraint = 0;
587                 s->flushed = 1;
588             }
589         }else{
590             return 0;
591         }
592     }else
593         fill_audiodata(in ,  (void*)in_arg);
594
595     fill_audiodata(out, out_arg);
596
597     if(s->resample){
598         return swr_convert_internal(s, out, out_count, in, in_count);
599     }else{
600         AudioData tmp= *in;
601         int ret2=0;
602         int ret, size;
603         size = FFMIN(out_count, s->in_buffer_count);
604         if(size){
605             buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
606             ret= swr_convert_internal(s, out, size, &tmp, size);
607             if(ret<0)
608                 return ret;
609             ret2= ret;
610             s->in_buffer_count -= ret;
611             s->in_buffer_index += ret;
612             buf_set(out, out, ret);
613             out_count -= ret;
614             if(!s->in_buffer_count)
615                 s->in_buffer_index = 0;
616         }
617
618         if(in_count){
619             size= s->in_buffer_index + s->in_buffer_count + in_count - out_count;
620
621             if(in_count > out_count) { //FIXME move after swr_convert_internal
622                 if(   size > s->in_buffer.count
623                 && s->in_buffer_count + in_count - out_count <= s->in_buffer_index){
624                     buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
625                     copy(&s->in_buffer, &tmp, s->in_buffer_count);
626                     s->in_buffer_index=0;
627                 }else
628                     if((ret=realloc_audio(&s->in_buffer, size)) < 0)
629                         return ret;
630             }
631
632             if(out_count){
633                 size = FFMIN(in_count, out_count);
634                 ret= swr_convert_internal(s, out, size, in, size);
635                 if(ret<0)
636                     return ret;
637                 buf_set(in, in, ret);
638                 in_count -= ret;
639                 ret2 += ret;
640             }
641             if(in_count){
642                 buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
643                 copy(&tmp, in, in_count);
644                 s->in_buffer_count += in_count;
645             }
646         }
647         return ret2;
648     }
649 }
650