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