00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "libavutil/opt.h"
00022 #include "swresample_internal.h"
00023 #include "audioconvert.h"
00024 #include "libavutil/avassert.h"
00025 #include "libavutil/audioconvert.h"
00026
00027 #include <float.h>
00028
00029 #define C30DB M_SQRT2
00030 #define C15DB 1.189207115
00031 #define C__0DB 1.0
00032 #define C_15DB 0.840896415
00033 #define C_30DB M_SQRT1_2
00034 #define C_45DB 0.594603558
00035 #define C_60DB 0.5
00036
00037 #define ALIGN 32
00038
00039
00040 #define OFFSET(x) offsetof(SwrContext,x)
00041 #define PARAM AV_OPT_FLAG_AUDIO_PARAM
00042
00043 static const AVOption options[]={
00044 {"ich" , "Input Channel Count" , OFFSET( in.ch_count ), AV_OPT_TYPE_INT , {.dbl=2 }, 0 , SWR_CH_MAX, PARAM},
00045 {"in_channel_count" , "Input Channel Count" , OFFSET( in.ch_count ), AV_OPT_TYPE_INT , {.dbl=2 }, 0 , SWR_CH_MAX, PARAM},
00046 {"och" , "Output Channel Count" , OFFSET(out.ch_count ), AV_OPT_TYPE_INT , {.dbl=2 }, 0 , SWR_CH_MAX, PARAM},
00047 {"out_channel_count" , "Output Channel Count" , OFFSET(out.ch_count ), AV_OPT_TYPE_INT , {.dbl=2 }, 0 , SWR_CH_MAX, PARAM},
00048 {"uch" , "Used Channel Count" , OFFSET(used_ch_count ), AV_OPT_TYPE_INT , {.dbl=0 }, 0 , SWR_CH_MAX, PARAM},
00049 {"used_channel_count" , "Used Channel Count" , OFFSET(used_ch_count ), AV_OPT_TYPE_INT , {.dbl=0 }, 0 , SWR_CH_MAX, PARAM},
00050 {"isr" , "Input Sample Rate" , OFFSET( in_sample_rate), AV_OPT_TYPE_INT , {.dbl=0 }, 0 , INT_MAX , PARAM},
00051 {"in_sample_rate" , "Input Sample Rate" , OFFSET( in_sample_rate), AV_OPT_TYPE_INT , {.dbl=0 }, 0 , INT_MAX , PARAM},
00052 {"osr" , "Output Sample Rate" , OFFSET(out_sample_rate), AV_OPT_TYPE_INT , {.dbl=0 }, 0 , INT_MAX , PARAM},
00053 {"out_sample_rate" , "Output Sample Rate" , OFFSET(out_sample_rate), AV_OPT_TYPE_INT , {.dbl=0 }, 0 , INT_MAX , PARAM},
00054 {"isf" , "Input Sample Format" , OFFSET( in_sample_fmt ), AV_OPT_TYPE_INT , {.dbl=AV_SAMPLE_FMT_NONE }, -1 , AV_SAMPLE_FMT_NB-1+256, PARAM},
00055 {"in_sample_fmt" , "Input Sample Format" , OFFSET( in_sample_fmt ), AV_OPT_TYPE_INT , {.dbl=AV_SAMPLE_FMT_NONE }, -1 , AV_SAMPLE_FMT_NB-1+256, PARAM},
00056 {"osf" , "Output Sample Format" , OFFSET(out_sample_fmt ), AV_OPT_TYPE_INT , {.dbl=AV_SAMPLE_FMT_NONE }, -1 , AV_SAMPLE_FMT_NB-1+256, PARAM},
00057 {"out_sample_fmt" , "Output Sample Format" , OFFSET(out_sample_fmt ), AV_OPT_TYPE_INT , {.dbl=AV_SAMPLE_FMT_NONE }, -1 , AV_SAMPLE_FMT_NB-1+256, PARAM},
00058 {"tsf" , "Internal Sample Format" , OFFSET(int_sample_fmt ), AV_OPT_TYPE_INT , {.dbl=AV_SAMPLE_FMT_NONE }, -1 , AV_SAMPLE_FMT_FLTP, PARAM},
00059 {"internal_sample_fmt" , "Internal Sample Format" , OFFSET(int_sample_fmt ), AV_OPT_TYPE_INT , {.dbl=AV_SAMPLE_FMT_NONE }, -1 , AV_SAMPLE_FMT_FLTP, PARAM},
00060 {"icl" , "Input Channel Layout" , OFFSET( in_ch_layout ), AV_OPT_TYPE_INT64, {.dbl=0 }, 0 , INT64_MAX , PARAM, "channel_layout"},
00061 {"in_channel_layout" , "Input Channel Layout" , OFFSET( in_ch_layout ), AV_OPT_TYPE_INT64, {.dbl=0 }, 0 , INT64_MAX , PARAM, "channel_layout"},
00062 {"ocl" , "Output Channel Layout" , OFFSET(out_ch_layout ), AV_OPT_TYPE_INT64, {.dbl=0 }, 0 , INT64_MAX , PARAM, "channel_layout"},
00063 {"out_channel_layout" , "Output Channel Layout" , OFFSET(out_ch_layout ), AV_OPT_TYPE_INT64, {.dbl=0 }, 0 , INT64_MAX , PARAM, "channel_layout"},
00064 {"clev" , "Center Mix Level" , OFFSET(clev ), AV_OPT_TYPE_FLOAT, {.dbl=C_30DB }, -32 , 32 , PARAM},
00065 {"center_mix_level" , "Center Mix Level" , OFFSET(clev ), AV_OPT_TYPE_FLOAT, {.dbl=C_30DB }, -32 , 32 , PARAM},
00066 {"slev" , "Sourround Mix Level" , OFFSET(slev ), AV_OPT_TYPE_FLOAT, {.dbl=C_30DB }, -32 , 32 , PARAM},
00067 {"surround_mix_level" , "Sourround Mix Level" , OFFSET(slev ), AV_OPT_TYPE_FLOAT, {.dbl=C_30DB }, -32 , 32 , PARAM},
00068 {"lfe_mix_level" , "LFE Mix Level" , OFFSET(lfe_mix_level ), AV_OPT_TYPE_FLOAT, {.dbl=0 }, -32 , 32 , PARAM},
00069 {"rmvol" , "Rematrix Volume" , OFFSET(rematrix_volume), AV_OPT_TYPE_FLOAT, {.dbl=1.0 }, -1000 , 1000 , PARAM},
00070 {"rematrix_volume" , "Rematrix Volume" , OFFSET(rematrix_volume), AV_OPT_TYPE_FLOAT, {.dbl=1.0 }, -1000 , 1000 , PARAM},
00071 {"flags" , NULL , OFFSET(flags ), AV_OPT_TYPE_FLAGS, {.dbl=0 }, 0 , UINT_MAX , PARAM, "flags"},
00072 {"swr_flags" , NULL , OFFSET(flags ), AV_OPT_TYPE_FLAGS, {.dbl=0 }, 0 , UINT_MAX , PARAM, "flags"},
00073 {"res" , "Force Resampling" , 0 , AV_OPT_TYPE_CONST, {.dbl=SWR_FLAG_RESAMPLE }, INT_MIN, INT_MAX , PARAM, "flags"},
00074 {"dither_scale" , "Dither Scale" , OFFSET(dither_scale ), AV_OPT_TYPE_FLOAT, {.dbl=1 }, 0 , INT_MAX , PARAM},
00075 {"dither_method" , "Dither Method" , OFFSET(dither_method ), AV_OPT_TYPE_INT , {.dbl=0 }, 0 , SWR_DITHER_NB-1, PARAM, "dither_method"},
00076 {"rectangular" , "Rectangular Dither" , 0 , AV_OPT_TYPE_CONST, {.dbl=SWR_DITHER_RECTANGULAR}, INT_MIN, INT_MAX , PARAM, "dither_method"},
00077 {"triangular" , "Triangular Dither" , 0 , AV_OPT_TYPE_CONST, {.dbl=SWR_DITHER_TRIANGULAR }, INT_MIN, INT_MAX , PARAM, "dither_method"},
00078 {"triangular_hp" , "Triangular Dither With High Pass" , 0 , AV_OPT_TYPE_CONST, {.dbl=SWR_DITHER_TRIANGULAR_HIGHPASS }, INT_MIN, INT_MAX, PARAM, "dither_method"},
00079 {"filter_size" , "Resampling Filter Size" , OFFSET(filter_size) , AV_OPT_TYPE_INT , {.dbl=16 }, 0 , INT_MAX , PARAM },
00080 {"phase_shift" , "Resampling Phase Shift" , OFFSET(phase_shift) , AV_OPT_TYPE_INT , {.dbl=10 }, 0 , 30 , PARAM },
00081 {"linear_interp" , "Use Linear Interpolation" , OFFSET(linear_interp) , AV_OPT_TYPE_INT , {.dbl=0 }, 0 , 1 , PARAM },
00082 {"cutoff" , "Cutoff Frequency Ratio" , OFFSET(cutoff) , AV_OPT_TYPE_DOUBLE,{.dbl=0.8 }, 0 , 1 , PARAM },
00083 {"min_comp" , "Minimum difference between timestamps and audio data (in seconds) below which no timestamp compensation of either kind is applied"
00084 , OFFSET(min_compensation),AV_OPT_TYPE_FLOAT ,{.dbl=FLT_MAX }, 0 , FLT_MAX , PARAM },
00085 {"min_hard_comp" , "Minimum difference between timestamps and audio data (in seconds) to trigger padding/trimming the data."
00086 , OFFSET(min_hard_compensation),AV_OPT_TYPE_FLOAT ,{.dbl=0.1 }, 0 , INT_MAX , PARAM },
00087 {"comp_duration" , "Duration (in seconds) over which data is stretched/squeezeed to make it match the timestamps."
00088 , OFFSET(soft_compensation_duration),AV_OPT_TYPE_FLOAT ,{.dbl=1 }, 0 , INT_MAX , PARAM },
00089 {"max_soft_comp" , "Maximum factor by which data is stretched/squeezeed to make it match the timestamps."
00090 , OFFSET(max_soft_compensation),AV_OPT_TYPE_FLOAT ,{.dbl=0 }, 0 , INT_MAX , PARAM },
00091
00092 {0}
00093 };
00094
00095 static const char* context_to_name(void* ptr) {
00096 return "SWR";
00097 }
00098
00099 static const AVClass av_class = {
00100 .class_name = "SwrContext",
00101 .item_name = context_to_name,
00102 .option = options,
00103 .version = LIBAVUTIL_VERSION_INT,
00104 .log_level_offset_offset = OFFSET(log_level_offset),
00105 .parent_log_context_offset = OFFSET(log_ctx),
00106 };
00107
00108 unsigned swresample_version(void)
00109 {
00110 av_assert0(LIBSWRESAMPLE_VERSION_MICRO >= 100);
00111 return LIBSWRESAMPLE_VERSION_INT;
00112 }
00113
00114 const char *swresample_configuration(void)
00115 {
00116 return FFMPEG_CONFIGURATION;
00117 }
00118
00119 const char *swresample_license(void)
00120 {
00121 #define LICENSE_PREFIX "libswresample license: "
00122 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
00123 }
00124
00125 int swr_set_channel_mapping(struct SwrContext *s, const int *channel_map){
00126 if(!s || s->in_convert)
00127 return AVERROR(EINVAL);
00128 s->channel_map = channel_map;
00129 return 0;
00130 }
00131
00132 const AVClass *swr_get_class(void)
00133 {
00134 return &av_class;
00135 }
00136
00137 struct SwrContext *swr_alloc(void){
00138 SwrContext *s= av_mallocz(sizeof(SwrContext));
00139 if(s){
00140 s->av_class= &av_class;
00141 av_opt_set_defaults(s);
00142 }
00143 return s;
00144 }
00145
00146 struct SwrContext *swr_alloc_set_opts(struct SwrContext *s,
00147 int64_t out_ch_layout, enum AVSampleFormat out_sample_fmt, int out_sample_rate,
00148 int64_t in_ch_layout, enum AVSampleFormat in_sample_fmt, int in_sample_rate,
00149 int log_offset, void *log_ctx){
00150 if(!s) s= swr_alloc();
00151 if(!s) return NULL;
00152
00153 s->log_level_offset= log_offset;
00154 s->log_ctx= log_ctx;
00155
00156 av_opt_set_int(s, "ocl", out_ch_layout, 0);
00157 av_opt_set_int(s, "osf", out_sample_fmt, 0);
00158 av_opt_set_int(s, "osr", out_sample_rate, 0);
00159 av_opt_set_int(s, "icl", in_ch_layout, 0);
00160 av_opt_set_int(s, "isf", in_sample_fmt, 0);
00161 av_opt_set_int(s, "isr", in_sample_rate, 0);
00162 av_opt_set_int(s, "tsf", AV_SAMPLE_FMT_NONE, 0);
00163 av_opt_set_int(s, "ich", av_get_channel_layout_nb_channels(s-> in_ch_layout), 0);
00164 av_opt_set_int(s, "och", av_get_channel_layout_nb_channels(s->out_ch_layout), 0);
00165 av_opt_set_int(s, "uch", 0, 0);
00166 return s;
00167 }
00168
00169 static void set_audiodata_fmt(AudioData *a, enum AVSampleFormat fmt){
00170 a->fmt = fmt;
00171 a->bps = av_get_bytes_per_sample(fmt);
00172 a->planar= av_sample_fmt_is_planar(fmt);
00173 }
00174
00175 static void free_temp(AudioData *a){
00176 av_free(a->data);
00177 memset(a, 0, sizeof(*a));
00178 }
00179
00180 void swr_free(SwrContext **ss){
00181 SwrContext *s= *ss;
00182 if(s){
00183 free_temp(&s->postin);
00184 free_temp(&s->midbuf);
00185 free_temp(&s->preout);
00186 free_temp(&s->in_buffer);
00187 free_temp(&s->dither);
00188 swri_audio_convert_free(&s-> in_convert);
00189 swri_audio_convert_free(&s->out_convert);
00190 swri_audio_convert_free(&s->full_convert);
00191 swri_resample_free(&s->resample);
00192 swri_rematrix_free(s);
00193 }
00194
00195 av_freep(ss);
00196 }
00197
00198 int swr_init(struct SwrContext *s){
00199 s->in_buffer_index= 0;
00200 s->in_buffer_count= 0;
00201 s->resample_in_constraint= 0;
00202 free_temp(&s->postin);
00203 free_temp(&s->midbuf);
00204 free_temp(&s->preout);
00205 free_temp(&s->in_buffer);
00206 free_temp(&s->dither);
00207 swri_audio_convert_free(&s-> in_convert);
00208 swri_audio_convert_free(&s->out_convert);
00209 swri_audio_convert_free(&s->full_convert);
00210 swri_rematrix_free(s);
00211
00212 s->flushed = 0;
00213
00214 if(s-> in_sample_fmt >= AV_SAMPLE_FMT_NB){
00215 av_log(s, AV_LOG_ERROR, "Requested input sample format %d is invalid\n", s->in_sample_fmt);
00216 return AVERROR(EINVAL);
00217 }
00218 if(s->out_sample_fmt >= AV_SAMPLE_FMT_NB){
00219 av_log(s, AV_LOG_ERROR, "Requested output sample format %d is invalid\n", s->out_sample_fmt);
00220 return AVERROR(EINVAL);
00221 }
00222
00223 if(s->int_sample_fmt == AV_SAMPLE_FMT_NONE){
00224 if(av_get_planar_sample_fmt(s->in_sample_fmt) <= AV_SAMPLE_FMT_S16P){
00225 s->int_sample_fmt= AV_SAMPLE_FMT_S16P;
00226 }else if(av_get_planar_sample_fmt(s->in_sample_fmt) <= AV_SAMPLE_FMT_FLTP){
00227 s->int_sample_fmt= AV_SAMPLE_FMT_FLTP;
00228 }else{
00229 av_log(s, AV_LOG_DEBUG, "Using double precission mode\n");
00230 s->int_sample_fmt= AV_SAMPLE_FMT_DBLP;
00231 }
00232 }
00233
00234 if( s->int_sample_fmt != AV_SAMPLE_FMT_S16P
00235 &&s->int_sample_fmt != AV_SAMPLE_FMT_S32P
00236 &&s->int_sample_fmt != AV_SAMPLE_FMT_FLTP
00237 &&s->int_sample_fmt != AV_SAMPLE_FMT_DBLP){
00238 av_log(s, AV_LOG_ERROR, "Requested sample format %s is not supported internally, S16/S32/FLT/DBL is supported\n", av_get_sample_fmt_name(s->int_sample_fmt));
00239 return AVERROR(EINVAL);
00240 }
00241
00242 set_audiodata_fmt(&s-> in, s-> in_sample_fmt);
00243 set_audiodata_fmt(&s->out, s->out_sample_fmt);
00244
00245 if (s->out_sample_rate!=s->in_sample_rate || (s->flags & SWR_FLAG_RESAMPLE)){
00246 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);
00247 }else
00248 swri_resample_free(&s->resample);
00249 if( s->int_sample_fmt != AV_SAMPLE_FMT_S16P
00250 && s->int_sample_fmt != AV_SAMPLE_FMT_S32P
00251 && s->int_sample_fmt != AV_SAMPLE_FMT_FLTP
00252 && s->int_sample_fmt != AV_SAMPLE_FMT_DBLP
00253 && s->resample){
00254 av_log(s, AV_LOG_ERROR, "Resampling only supported with internal s16/s32/flt/dbl\n");
00255 return -1;
00256 }
00257
00258 if(!s->used_ch_count)
00259 s->used_ch_count= s->in.ch_count;
00260
00261 if(s->used_ch_count && s-> in_ch_layout && s->used_ch_count != av_get_channel_layout_nb_channels(s-> in_ch_layout)){
00262 av_log(s, AV_LOG_WARNING, "Input channel layout has a different number of channels than the number of used channels, ignoring layout\n");
00263 s-> in_ch_layout= 0;
00264 }
00265
00266 if(!s-> in_ch_layout)
00267 s-> in_ch_layout= av_get_default_channel_layout(s->used_ch_count);
00268 if(!s->out_ch_layout)
00269 s->out_ch_layout= av_get_default_channel_layout(s->out.ch_count);
00270
00271 s->rematrix= s->out_ch_layout !=s->in_ch_layout || s->rematrix_volume!=1.0 ||
00272 s->rematrix_custom;
00273
00274 #define RSC 1 //FIXME finetune
00275 if(!s-> in.ch_count)
00276 s-> in.ch_count= av_get_channel_layout_nb_channels(s-> in_ch_layout);
00277 if(!s->used_ch_count)
00278 s->used_ch_count= s->in.ch_count;
00279 if(!s->out.ch_count)
00280 s->out.ch_count= av_get_channel_layout_nb_channels(s->out_ch_layout);
00281
00282 if(!s-> in.ch_count){
00283 av_assert0(!s->in_ch_layout);
00284 av_log(s, AV_LOG_ERROR, "Input channel count and layout are unset\n");
00285 return -1;
00286 }
00287
00288 if ((!s->out_ch_layout || !s->in_ch_layout) && s->used_ch_count != s->out.ch_count && !s->rematrix_custom) {
00289 av_log(s, AV_LOG_ERROR, "Rematrix is needed but there is not enough information to do it\n");
00290 return -1;
00291 }
00292
00293 av_assert0(s->used_ch_count);
00294 av_assert0(s->out.ch_count);
00295 s->resample_first= RSC*s->out.ch_count/s->in.ch_count - RSC < s->out_sample_rate/(float)s-> in_sample_rate - 1.0;
00296
00297 s->in_buffer= s->in;
00298
00299 if(!s->resample && !s->rematrix && !s->channel_map && !s->dither_method){
00300 s->full_convert = swri_audio_convert_alloc(s->out_sample_fmt,
00301 s-> in_sample_fmt, s-> in.ch_count, NULL, 0);
00302 return 0;
00303 }
00304
00305 s->in_convert = swri_audio_convert_alloc(s->int_sample_fmt,
00306 s-> in_sample_fmt, s->used_ch_count, s->channel_map, 0);
00307 s->out_convert= swri_audio_convert_alloc(s->out_sample_fmt,
00308 s->int_sample_fmt, s->out.ch_count, NULL, 0);
00309
00310
00311 s->postin= s->in;
00312 s->preout= s->out;
00313 s->midbuf= s->in;
00314
00315 if(s->channel_map){
00316 s->postin.ch_count=
00317 s->midbuf.ch_count= s->used_ch_count;
00318 if(s->resample)
00319 s->in_buffer.ch_count= s->used_ch_count;
00320 }
00321 if(!s->resample_first){
00322 s->midbuf.ch_count= s->out.ch_count;
00323 if(s->resample)
00324 s->in_buffer.ch_count = s->out.ch_count;
00325 }
00326
00327 set_audiodata_fmt(&s->postin, s->int_sample_fmt);
00328 set_audiodata_fmt(&s->midbuf, s->int_sample_fmt);
00329 set_audiodata_fmt(&s->preout, s->int_sample_fmt);
00330
00331 if(s->resample){
00332 set_audiodata_fmt(&s->in_buffer, s->int_sample_fmt);
00333 }
00334
00335 s->dither = s->preout;
00336
00337 if(s->rematrix || s->dither_method)
00338 return swri_rematrix_init(s);
00339
00340 return 0;
00341 }
00342
00343 static int realloc_audio(AudioData *a, int count){
00344 int i, countb;
00345 AudioData old;
00346
00347 if(count < 0 || count > INT_MAX/2/a->bps/a->ch_count)
00348 return AVERROR(EINVAL);
00349
00350 if(a->count >= count)
00351 return 0;
00352
00353 count*=2;
00354
00355 countb= FFALIGN(count*a->bps, ALIGN);
00356 old= *a;
00357
00358 av_assert0(a->bps);
00359 av_assert0(a->ch_count);
00360
00361 a->data= av_malloc(countb*a->ch_count);
00362 if(!a->data)
00363 return AVERROR(ENOMEM);
00364 for(i=0; i<a->ch_count; i++){
00365 a->ch[i]= a->data + i*(a->planar ? countb : a->bps);
00366 if(a->planar) memcpy(a->ch[i], old.ch[i], a->count*a->bps);
00367 }
00368 if(!a->planar) memcpy(a->ch[0], old.ch[0], a->count*a->ch_count*a->bps);
00369 av_free(old.data);
00370 a->count= count;
00371
00372 return 1;
00373 }
00374
00375 static void copy(AudioData *out, AudioData *in,
00376 int count){
00377 av_assert0(out->planar == in->planar);
00378 av_assert0(out->bps == in->bps);
00379 av_assert0(out->ch_count == in->ch_count);
00380 if(out->planar){
00381 int ch;
00382 for(ch=0; ch<out->ch_count; ch++)
00383 memcpy(out->ch[ch], in->ch[ch], count*out->bps);
00384 }else
00385 memcpy(out->ch[0], in->ch[0], count*out->ch_count*out->bps);
00386 }
00387
00388 static void fill_audiodata(AudioData *out, uint8_t *in_arg [SWR_CH_MAX]){
00389 int i;
00390 if(!in_arg){
00391 memset(out->ch, 0, sizeof(out->ch));
00392 }else if(out->planar){
00393 for(i=0; i<out->ch_count; i++)
00394 out->ch[i]= in_arg[i];
00395 }else{
00396 for(i=0; i<out->ch_count; i++)
00397 out->ch[i]= in_arg[0] + i*out->bps;
00398 }
00399 }
00400
00401 static void reversefill_audiodata(AudioData *out, uint8_t *in_arg [SWR_CH_MAX]){
00402 int i;
00403 if(out->planar){
00404 for(i=0; i<out->ch_count; i++)
00405 in_arg[i]= out->ch[i];
00406 }else{
00407 in_arg[0]= out->ch[0];
00408 }
00409 }
00410
00415 static void buf_set(AudioData *out, AudioData *in, int count){
00416 int ch;
00417 if(in->planar){
00418 for(ch=0; ch<out->ch_count; ch++)
00419 out->ch[ch]= in->ch[ch] + count*out->bps;
00420 }else{
00421 for(ch=out->ch_count-1; ch>=0; ch--)
00422 out->ch[ch]= in->ch[0] + (ch + count*out->ch_count) * out->bps;
00423 }
00424 }
00425
00430 static int resample(SwrContext *s, AudioData *out_param, int out_count,
00431 const AudioData * in_param, int in_count){
00432 AudioData in, out, tmp;
00433 int ret_sum=0;
00434 int border=0;
00435
00436 av_assert1(s->in_buffer.ch_count == in_param->ch_count);
00437 av_assert1(s->in_buffer.planar == in_param->planar);
00438 av_assert1(s->in_buffer.fmt == in_param->fmt);
00439
00440 tmp=out=*out_param;
00441 in = *in_param;
00442
00443 do{
00444 int ret, size, consumed;
00445 if(!s->resample_in_constraint && s->in_buffer_count){
00446 buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
00447 ret= swri_multiple_resample(s->resample, &out, out_count, &tmp, s->in_buffer_count, &consumed);
00448 out_count -= ret;
00449 ret_sum += ret;
00450 buf_set(&out, &out, ret);
00451 s->in_buffer_count -= consumed;
00452 s->in_buffer_index += consumed;
00453
00454 if(!in_count)
00455 break;
00456 if(s->in_buffer_count <= border){
00457 buf_set(&in, &in, -s->in_buffer_count);
00458 in_count += s->in_buffer_count;
00459 s->in_buffer_count=0;
00460 s->in_buffer_index=0;
00461 border = 0;
00462 }
00463 }
00464
00465 if(in_count && !s->in_buffer_count){
00466 s->in_buffer_index=0;
00467 ret= swri_multiple_resample(s->resample, &out, out_count, &in, in_count, &consumed);
00468 out_count -= ret;
00469 ret_sum += ret;
00470 buf_set(&out, &out, ret);
00471 in_count -= consumed;
00472 buf_set(&in, &in, consumed);
00473 }
00474
00475
00476 size= s->in_buffer_index + s->in_buffer_count + in_count;
00477 if( size > s->in_buffer.count
00478 && s->in_buffer_count + in_count <= s->in_buffer_index){
00479 buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
00480 copy(&s->in_buffer, &tmp, s->in_buffer_count);
00481 s->in_buffer_index=0;
00482 }else
00483 if((ret=realloc_audio(&s->in_buffer, size)) < 0)
00484 return ret;
00485
00486 if(in_count){
00487 int count= in_count;
00488 if(s->in_buffer_count && s->in_buffer_count+2 < count && out_count) count= s->in_buffer_count+2;
00489
00490 buf_set(&tmp, &s->in_buffer, s->in_buffer_index + s->in_buffer_count);
00491 copy(&tmp, &in, count);
00492 s->in_buffer_count += count;
00493 in_count -= count;
00494 border += count;
00495 buf_set(&in, &in, count);
00496 s->resample_in_constraint= 0;
00497 if(s->in_buffer_count != count || in_count)
00498 continue;
00499 }
00500 break;
00501 }while(1);
00502
00503 s->resample_in_constraint= !!out_count;
00504
00505 return ret_sum;
00506 }
00507
00508 static int swr_convert_internal(struct SwrContext *s, AudioData *out, int out_count,
00509 AudioData *in , int in_count){
00510 AudioData *postin, *midbuf, *preout;
00511 int ret;
00512 AudioData preout_tmp, midbuf_tmp;
00513
00514 if(s->full_convert){
00515 av_assert0(!s->resample);
00516 swri_audio_convert(s->full_convert, out, in, in_count);
00517 return out_count;
00518 }
00519
00520
00521
00522
00523 if((ret=realloc_audio(&s->postin, in_count))<0)
00524 return ret;
00525 if(s->resample_first){
00526 av_assert0(s->midbuf.ch_count == s->used_ch_count);
00527 if((ret=realloc_audio(&s->midbuf, out_count))<0)
00528 return ret;
00529 }else{
00530 av_assert0(s->midbuf.ch_count == s->out.ch_count);
00531 if((ret=realloc_audio(&s->midbuf, in_count))<0)
00532 return ret;
00533 }
00534 if((ret=realloc_audio(&s->preout, out_count))<0)
00535 return ret;
00536
00537 postin= &s->postin;
00538
00539 midbuf_tmp= s->midbuf;
00540 midbuf= &midbuf_tmp;
00541 preout_tmp= s->preout;
00542 preout= &preout_tmp;
00543
00544 if(s->int_sample_fmt == s-> in_sample_fmt && s->in.planar)
00545 postin= in;
00546
00547 if(s->resample_first ? !s->resample : !s->rematrix)
00548 midbuf= postin;
00549
00550 if(s->resample_first ? !s->rematrix : !s->resample)
00551 preout= midbuf;
00552
00553 if(s->int_sample_fmt == s->out_sample_fmt && s->out.planar){
00554 if(preout==in){
00555 out_count= FFMIN(out_count, in_count);
00556 av_assert0(s->in.planar);
00557 copy(out, in, out_count);
00558 return out_count;
00559 }
00560 else if(preout==postin) preout= midbuf= postin= out;
00561 else if(preout==midbuf) preout= midbuf= out;
00562 else preout= out;
00563 }
00564
00565 if(in != postin){
00566 swri_audio_convert(s->in_convert, postin, in, in_count);
00567 }
00568
00569 if(s->resample_first){
00570 if(postin != midbuf)
00571 out_count= resample(s, midbuf, out_count, postin, in_count);
00572 if(midbuf != preout)
00573 swri_rematrix(s, preout, midbuf, out_count, preout==out);
00574 }else{
00575 if(postin != midbuf)
00576 swri_rematrix(s, midbuf, postin, in_count, midbuf==out);
00577 if(midbuf != preout)
00578 out_count= resample(s, preout, out_count, midbuf, in_count);
00579 }
00580
00581 if(preout != out && out_count){
00582 if(s->dither_method){
00583 int ch;
00584 int dither_count= FFMAX(out_count, 1<<16);
00585 av_assert0(preout != in);
00586
00587 if((ret=realloc_audio(&s->dither, dither_count))<0)
00588 return ret;
00589 if(ret)
00590 for(ch=0; ch<s->dither.ch_count; ch++)
00591 swri_get_dither(s, s->dither.ch[ch], s->dither.count, 12345678913579<<ch, s->out_sample_fmt, s->int_sample_fmt);
00592 av_assert0(s->dither.ch_count == preout->ch_count);
00593
00594 if(s->dither_pos + out_count > s->dither.count)
00595 s->dither_pos = 0;
00596
00597 for(ch=0; ch<preout->ch_count; ch++)
00598 s->mix_2_1_f(preout->ch[ch], preout->ch[ch], s->dither.ch[ch] + s->dither.bps * s->dither_pos, s->native_one, 0, 0, out_count);
00599
00600 s->dither_pos += out_count;
00601 }
00602
00603 swri_audio_convert(s->out_convert, out, preout, out_count);
00604 }
00605 return out_count;
00606 }
00607
00608 int swr_convert(struct SwrContext *s, uint8_t *out_arg[SWR_CH_MAX], int out_count,
00609 const uint8_t *in_arg [SWR_CH_MAX], int in_count){
00610 AudioData * in= &s->in;
00611 AudioData *out= &s->out;
00612
00613 if(s->drop_output > 0){
00614 int ret;
00615 AudioData tmp = s->out;
00616 uint8_t *tmp_arg[SWR_CH_MAX];
00617 tmp.count = 0;
00618 tmp.data = NULL;
00619 if((ret=realloc_audio(&tmp, s->drop_output))<0)
00620 return ret;
00621
00622 reversefill_audiodata(&tmp, tmp_arg);
00623 s->drop_output *= -1;
00624 ret = swr_convert(s, tmp_arg, -s->drop_output, in_arg, in_count);
00625 s->drop_output *= -1;
00626 if(ret>0)
00627 s->drop_output -= ret;
00628
00629 av_freep(&tmp.data);
00630 if(s->drop_output || !out_arg)
00631 return 0;
00632 in_count = 0;
00633 }
00634
00635 if(!in_arg){
00636 if(s->in_buffer_count){
00637 if (s->resample && !s->flushed) {
00638 AudioData *a= &s->in_buffer;
00639 int i, j, ret;
00640 if((ret=realloc_audio(a, s->in_buffer_index + 2*s->in_buffer_count)) < 0)
00641 return ret;
00642 av_assert0(a->planar);
00643 for(i=0; i<a->ch_count; i++){
00644 for(j=0; j<s->in_buffer_count; j++){
00645 memcpy(a->ch[i] + (s->in_buffer_index+s->in_buffer_count+j )*a->bps,
00646 a->ch[i] + (s->in_buffer_index+s->in_buffer_count-j-1)*a->bps, a->bps);
00647 }
00648 }
00649 s->in_buffer_count += (s->in_buffer_count+1)/2;
00650 s->resample_in_constraint = 0;
00651 s->flushed = 1;
00652 }
00653 }else{
00654 return 0;
00655 }
00656 }else
00657 fill_audiodata(in , (void*)in_arg);
00658
00659 fill_audiodata(out, out_arg);
00660
00661 if(s->resample){
00662 int ret = swr_convert_internal(s, out, out_count, in, in_count);
00663 if(ret>0 && !s->drop_output)
00664 s->outpts += ret * (int64_t)s->in_sample_rate;
00665 return ret;
00666 }else{
00667 AudioData tmp= *in;
00668 int ret2=0;
00669 int ret, size;
00670 size = FFMIN(out_count, s->in_buffer_count);
00671 if(size){
00672 buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
00673 ret= swr_convert_internal(s, out, size, &tmp, size);
00674 if(ret<0)
00675 return ret;
00676 ret2= ret;
00677 s->in_buffer_count -= ret;
00678 s->in_buffer_index += ret;
00679 buf_set(out, out, ret);
00680 out_count -= ret;
00681 if(!s->in_buffer_count)
00682 s->in_buffer_index = 0;
00683 }
00684
00685 if(in_count){
00686 size= s->in_buffer_index + s->in_buffer_count + in_count - out_count;
00687
00688 if(in_count > out_count) {
00689 if( size > s->in_buffer.count
00690 && s->in_buffer_count + in_count - out_count <= s->in_buffer_index){
00691 buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
00692 copy(&s->in_buffer, &tmp, s->in_buffer_count);
00693 s->in_buffer_index=0;
00694 }else
00695 if((ret=realloc_audio(&s->in_buffer, size)) < 0)
00696 return ret;
00697 }
00698
00699 if(out_count){
00700 size = FFMIN(in_count, out_count);
00701 ret= swr_convert_internal(s, out, size, in, size);
00702 if(ret<0)
00703 return ret;
00704 buf_set(in, in, ret);
00705 in_count -= ret;
00706 ret2 += ret;
00707 }
00708 if(in_count){
00709 buf_set(&tmp, &s->in_buffer, s->in_buffer_index + s->in_buffer_count);
00710 copy(&tmp, in, in_count);
00711 s->in_buffer_count += in_count;
00712 }
00713 }
00714 if(ret2>0 && !s->drop_output)
00715 s->outpts += ret2 * (int64_t)s->in_sample_rate;
00716 return ret2;
00717 }
00718 }
00719
00720 int swr_drop_output(struct SwrContext *s, int count){
00721 s->drop_output += count;
00722
00723 if(s->drop_output <= 0)
00724 return 0;
00725
00726 av_log(s, AV_LOG_VERBOSE, "discarding %d audio samples\n", count);
00727 return swr_convert(s, NULL, s->drop_output, NULL, 0);
00728 }
00729
00730 int swr_inject_silence(struct SwrContext *s, int count){
00731 int ret, i;
00732 AudioData silence = s->out;
00733 uint8_t *tmp_arg[SWR_CH_MAX];
00734
00735 if(count <= 0)
00736 return 0;
00737
00738 silence.count = 0;
00739 silence.data = NULL;
00740 if((ret=realloc_audio(&silence, count))<0)
00741 return ret;
00742
00743 if(silence.planar) for(i=0; i<silence.ch_count; i++) {
00744 memset(silence.ch[i], silence.bps==1 ? 0x80 : 0, count*silence.bps);
00745 } else
00746 memset(silence.ch[0], silence.bps==1 ? 0x80 : 0, count*silence.bps*silence.ch_count);
00747
00748 reversefill_audiodata(&silence, tmp_arg);
00749 av_log(s, AV_LOG_VERBOSE, "adding %d audio samples of silence\n", count);
00750 ret = swr_convert(s, NULL, 0, (const uint8_t**)tmp_arg, count);
00751 av_freep(&silence.data);
00752 return ret;
00753 }
00754
00755 int64_t swr_next_pts(struct SwrContext *s, int64_t pts){
00756 if(pts == INT64_MIN)
00757 return s->outpts;
00758 if(s->min_compensation >= FLT_MAX) {
00759 return (s->outpts = pts - swr_get_delay(s, s->in_sample_rate * (int64_t)s->out_sample_rate));
00760 } else {
00761 int64_t delta = pts - swr_get_delay(s, s->in_sample_rate * (int64_t)s->out_sample_rate) - s->outpts;
00762 double fdelta = delta /(double)(s->in_sample_rate * (int64_t)s->out_sample_rate);
00763
00764 if(fabs(fdelta) > s->min_compensation) {
00765 if(!s->outpts || fabs(fdelta) > s->min_hard_compensation){
00766 int ret;
00767 if(delta > 0) ret = swr_inject_silence(s, delta / s->out_sample_rate);
00768 else ret = swr_drop_output (s, -delta / s-> in_sample_rate);
00769 if(ret<0){
00770 av_log(s, AV_LOG_ERROR, "Failed to compensate for timestamp delta of %f\n", fdelta);
00771 }
00772 } else if(s->soft_compensation_duration && s->max_soft_compensation) {
00773 int duration = s->out_sample_rate * s->soft_compensation_duration;
00774 int comp = av_clipf(fdelta, -s->max_soft_compensation, s->max_soft_compensation) * duration ;
00775 av_log(s, AV_LOG_VERBOSE, "compensating audio timestamp drift:%f compensation:%d in:%d\n", fdelta, comp, duration);
00776 swr_set_compensation(s, comp, duration);
00777 }
00778 }
00779
00780 return s->outpts;
00781 }
00782 }