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/channel_layout.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" , "set input channel count" , OFFSET( in.ch_count ), AV_OPT_TYPE_INT , {.i64=0 }, 0 , SWR_CH_MAX, PARAM},
00045 {"in_channel_count" , "set input channel count" , OFFSET( in.ch_count ), AV_OPT_TYPE_INT , {.i64=0 }, 0 , SWR_CH_MAX, PARAM},
00046 {"och" , "set output channel count" , OFFSET(out.ch_count ), AV_OPT_TYPE_INT , {.i64=0 }, 0 , SWR_CH_MAX, PARAM},
00047 {"out_channel_count" , "set output channel count" , OFFSET(out.ch_count ), AV_OPT_TYPE_INT , {.i64=0 }, 0 , SWR_CH_MAX, PARAM},
00048 {"uch" , "set used channel count" , OFFSET(used_ch_count ), AV_OPT_TYPE_INT , {.i64=0 }, 0 , SWR_CH_MAX, PARAM},
00049 {"used_channel_count" , "set used channel count" , OFFSET(used_ch_count ), AV_OPT_TYPE_INT , {.i64=0 }, 0 , SWR_CH_MAX, PARAM},
00050 {"isr" , "set input sample rate" , OFFSET( in_sample_rate), AV_OPT_TYPE_INT , {.i64=0 }, 0 , INT_MAX , PARAM},
00051 {"in_sample_rate" , "set input sample rate" , OFFSET( in_sample_rate), AV_OPT_TYPE_INT , {.i64=0 }, 0 , INT_MAX , PARAM},
00052 {"osr" , "set output sample rate" , OFFSET(out_sample_rate), AV_OPT_TYPE_INT , {.i64=0 }, 0 , INT_MAX , PARAM},
00053 {"out_sample_rate" , "set output sample rate" , OFFSET(out_sample_rate), AV_OPT_TYPE_INT , {.i64=0 }, 0 , INT_MAX , PARAM},
00054 {"isf" , "set input sample format" , OFFSET( in_sample_fmt ), AV_OPT_TYPE_SAMPLE_FMT , {.i64=AV_SAMPLE_FMT_NONE}, -1 , AV_SAMPLE_FMT_NB-1, PARAM},
00055 {"in_sample_fmt" , "set input sample format" , OFFSET( in_sample_fmt ), AV_OPT_TYPE_SAMPLE_FMT , {.i64=AV_SAMPLE_FMT_NONE}, -1 , AV_SAMPLE_FMT_NB-1, PARAM},
00056 {"osf" , "set output sample format" , OFFSET(out_sample_fmt ), AV_OPT_TYPE_SAMPLE_FMT , {.i64=AV_SAMPLE_FMT_NONE}, -1 , AV_SAMPLE_FMT_NB-1, PARAM},
00057 {"out_sample_fmt" , "set output sample format" , OFFSET(out_sample_fmt ), AV_OPT_TYPE_SAMPLE_FMT , {.i64=AV_SAMPLE_FMT_NONE}, -1 , AV_SAMPLE_FMT_NB-1, PARAM},
00058 {"tsf" , "set internal sample format" , OFFSET(int_sample_fmt ), AV_OPT_TYPE_SAMPLE_FMT , {.i64=AV_SAMPLE_FMT_NONE}, -1 , AV_SAMPLE_FMT_NB-1, PARAM},
00059 {"internal_sample_fmt" , "set internal sample format" , OFFSET(int_sample_fmt ), AV_OPT_TYPE_SAMPLE_FMT , {.i64=AV_SAMPLE_FMT_NONE}, -1 , AV_SAMPLE_FMT_NB-1, PARAM},
00060 {"icl" , "set input channel layout" , OFFSET( in_ch_layout ), AV_OPT_TYPE_INT64, {.i64=0 }, 0 , INT64_MAX , PARAM, "channel_layout"},
00061 {"in_channel_layout" , "set input channel layout" , OFFSET( in_ch_layout ), AV_OPT_TYPE_INT64, {.i64=0 }, 0 , INT64_MAX , PARAM, "channel_layout"},
00062 {"ocl" , "set output channel layout" , OFFSET(out_ch_layout ), AV_OPT_TYPE_INT64, {.i64=0 }, 0 , INT64_MAX , PARAM, "channel_layout"},
00063 {"out_channel_layout" , "set output channel layout" , OFFSET(out_ch_layout ), AV_OPT_TYPE_INT64, {.i64=0 }, 0 , INT64_MAX , PARAM, "channel_layout"},
00064 {"clev" , "set center mix level" , OFFSET(clev ), AV_OPT_TYPE_FLOAT, {.dbl=C_30DB }, -32 , 32 , PARAM},
00065 {"center_mix_level" , "set center mix level" , OFFSET(clev ), AV_OPT_TYPE_FLOAT, {.dbl=C_30DB }, -32 , 32 , PARAM},
00066 {"slev" , "set surround mix level" , OFFSET(slev ), AV_OPT_TYPE_FLOAT, {.dbl=C_30DB }, -32 , 32 , PARAM},
00067 {"surround_mix_level" , "set surround mix Level" , OFFSET(slev ), AV_OPT_TYPE_FLOAT, {.dbl=C_30DB }, -32 , 32 , PARAM},
00068 {"lfe_mix_level" , "set LFE mix level" , OFFSET(lfe_mix_level ), AV_OPT_TYPE_FLOAT, {.dbl=0 }, -32 , 32 , PARAM},
00069 {"rmvol" , "set rematrix volume" , OFFSET(rematrix_volume), AV_OPT_TYPE_FLOAT, {.dbl=1.0 }, -1000 , 1000 , PARAM},
00070 {"rematrix_volume" , "set rematrix volume" , OFFSET(rematrix_volume), AV_OPT_TYPE_FLOAT, {.dbl=1.0 }, -1000 , 1000 , PARAM},
00071
00072 {"flags" , "set flags" , OFFSET(flags ), AV_OPT_TYPE_FLAGS, {.i64=0 }, 0 , UINT_MAX , PARAM, "flags"},
00073 {"swr_flags" , "set flags" , OFFSET(flags ), AV_OPT_TYPE_FLAGS, {.i64=0 }, 0 , UINT_MAX , PARAM, "flags"},
00074 {"res" , "force resampling" , 0 , AV_OPT_TYPE_CONST, {.i64=SWR_FLAG_RESAMPLE }, INT_MIN, INT_MAX , PARAM, "flags"},
00075
00076 {"dither_scale" , "set dither scale" , OFFSET(dither_scale ), AV_OPT_TYPE_FLOAT, {.dbl=1 }, 0 , INT_MAX , PARAM},
00077
00078 {"dither_method" , "set dither method" , OFFSET(dither_method ), AV_OPT_TYPE_INT , {.i64=0 }, 0 , SWR_DITHER_NB-1, PARAM, "dither_method"},
00079 {"rectangular" , "select rectangular dither" , 0 , AV_OPT_TYPE_CONST, {.i64=SWR_DITHER_RECTANGULAR}, INT_MIN, INT_MAX , PARAM, "dither_method"},
00080 {"triangular" , "select triangular dither" , 0 , AV_OPT_TYPE_CONST, {.i64=SWR_DITHER_TRIANGULAR }, INT_MIN, INT_MAX , PARAM, "dither_method"},
00081 {"triangular_hp" , "select triangular dither with high pass" , 0 , AV_OPT_TYPE_CONST, {.i64=SWR_DITHER_TRIANGULAR_HIGHPASS }, INT_MIN, INT_MAX, PARAM, "dither_method"},
00082
00083 {"filter_size" , "set resampling filter size" , OFFSET(filter_size) , AV_OPT_TYPE_INT , {.i64=16 }, 0 , INT_MAX , PARAM },
00084 {"phase_shift" , "set resampling phase shift" , OFFSET(phase_shift) , AV_OPT_TYPE_INT , {.i64=10 }, 0 , 30 , PARAM },
00085 {"linear_interp" , "enable linear interpolation" , OFFSET(linear_interp) , AV_OPT_TYPE_INT , {.i64=0 }, 0 , 1 , PARAM },
00086 {"cutoff" , "set cutoff frequency ratio" , OFFSET(cutoff) , AV_OPT_TYPE_DOUBLE,{.dbl=0. }, 0 , 1 , PARAM },
00087 {"resampler" , "set resampling Engine" , OFFSET(engine) , AV_OPT_TYPE_INT , {.i64=0 }, 0 , SWR_ENGINE_NB-1, PARAM, "resampler"},
00088 {"swr" , "select SW Resampler" , 0 , AV_OPT_TYPE_CONST, {.i64=SWR_ENGINE_SWR }, INT_MIN, INT_MAX , PARAM, "resampler"},
00089 {"soxr" , "select SoX Resampler" , 0 , AV_OPT_TYPE_CONST, {.i64=SWR_ENGINE_SOXR }, INT_MIN, INT_MAX , PARAM, "resampler"},
00090 {"precision" , "set resampling precision" , OFFSET(precision) , AV_OPT_TYPE_DOUBLE,{.dbl=20.0 }, 15.0 , 33.0 , PARAM },
00091 {"cheby" , "enable Chebyshev passband" , OFFSET(cheby) , AV_OPT_TYPE_INT , {.i64=0 }, 0 , 1 , PARAM },
00092 {"min_comp" , "set minimum difference between timestamps and audio data (in seconds) below which no timestamp compensation of either kind is applied"
00093 , OFFSET(min_compensation),AV_OPT_TYPE_FLOAT ,{.dbl=FLT_MAX }, 0 , FLT_MAX , PARAM },
00094 {"min_hard_comp" , "set minimum difference between timestamps and audio data (in seconds) to trigger padding/trimming the data."
00095 , OFFSET(min_hard_compensation),AV_OPT_TYPE_FLOAT ,{.dbl=0.1 }, 0 , INT_MAX , PARAM },
00096 {"comp_duration" , "set duration (in seconds) over which data is stretched/squeezed to make it match the timestamps."
00097 , OFFSET(soft_compensation_duration),AV_OPT_TYPE_FLOAT ,{.dbl=1 }, 0 , INT_MAX , PARAM },
00098 {"max_soft_comp" , "set maximum factor by which data is stretched/squeezed to make it match the timestamps."
00099 , OFFSET(max_soft_compensation),AV_OPT_TYPE_FLOAT ,{.dbl=0 }, INT_MIN, INT_MAX , PARAM },
00100
00101 { "matrix_encoding" , "set matrixed stereo encoding" , OFFSET(matrix_encoding), AV_OPT_TYPE_INT ,{.i64 = AV_MATRIX_ENCODING_NONE}, AV_MATRIX_ENCODING_NONE, AV_MATRIX_ENCODING_NB-1, PARAM, "matrix_encoding" },
00102 { "none", "select none", 0, AV_OPT_TYPE_CONST, { .i64 = AV_MATRIX_ENCODING_NONE }, INT_MIN, INT_MAX, PARAM, "matrix_encoding" },
00103 { "dolby", "select Dolby", 0, AV_OPT_TYPE_CONST, { .i64 = AV_MATRIX_ENCODING_DOLBY }, INT_MIN, INT_MAX, PARAM, "matrix_encoding" },
00104 { "dplii", "select Dolby Pro Logic II", 0, AV_OPT_TYPE_CONST, { .i64 = AV_MATRIX_ENCODING_DPLII }, INT_MIN, INT_MAX, PARAM, "matrix_encoding" },
00105
00106 { "filter_type" , "select filter type" , OFFSET(filter_type) , AV_OPT_TYPE_INT , { .i64 = SWR_FILTER_TYPE_KAISER }, SWR_FILTER_TYPE_CUBIC, SWR_FILTER_TYPE_KAISER, PARAM, "filter_type" },
00107 { "cubic" , "select cubic" , 0 , AV_OPT_TYPE_CONST, { .i64 = SWR_FILTER_TYPE_CUBIC }, INT_MIN, INT_MAX, PARAM, "filter_type" },
00108 { "blackman_nuttall", "select Blackman Nuttall Windowed Sinc", 0 , AV_OPT_TYPE_CONST, { .i64 = SWR_FILTER_TYPE_BLACKMAN_NUTTALL }, INT_MIN, INT_MAX, PARAM, "filter_type" },
00109 { "kaiser" , "select Kaiser Windowed Sinc" , 0 , AV_OPT_TYPE_CONST, { .i64 = SWR_FILTER_TYPE_KAISER }, INT_MIN, INT_MAX, PARAM, "filter_type" },
00110
00111 { "kaiser_beta" , "set Kaiser Window Beta" , OFFSET(kaiser_beta) , AV_OPT_TYPE_INT , {.i64=9 }, 2 , 16 , PARAM },
00112
00113 {0}
00114 };
00115
00116 static const char* context_to_name(void* ptr) {
00117 return "SWR";
00118 }
00119
00120 static const AVClass av_class = {
00121 .class_name = "SWResampler",
00122 .item_name = context_to_name,
00123 .option = options,
00124 .version = LIBAVUTIL_VERSION_INT,
00125 .log_level_offset_offset = OFFSET(log_level_offset),
00126 .parent_log_context_offset = OFFSET(log_ctx),
00127 .category = AV_CLASS_CATEGORY_SWRESAMPLER,
00128 };
00129
00130 unsigned swresample_version(void)
00131 {
00132 av_assert0(LIBSWRESAMPLE_VERSION_MICRO >= 100);
00133 return LIBSWRESAMPLE_VERSION_INT;
00134 }
00135
00136 const char *swresample_configuration(void)
00137 {
00138 return FFMPEG_CONFIGURATION;
00139 }
00140
00141 const char *swresample_license(void)
00142 {
00143 #define LICENSE_PREFIX "libswresample license: "
00144 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
00145 }
00146
00147 int swr_set_channel_mapping(struct SwrContext *s, const int *channel_map){
00148 if(!s || s->in_convert)
00149 return AVERROR(EINVAL);
00150 s->channel_map = channel_map;
00151 return 0;
00152 }
00153
00154 const AVClass *swr_get_class(void)
00155 {
00156 return &av_class;
00157 }
00158
00159 av_cold struct SwrContext *swr_alloc(void){
00160 SwrContext *s= av_mallocz(sizeof(SwrContext));
00161 if(s){
00162 s->av_class= &av_class;
00163 av_opt_set_defaults(s);
00164 }
00165 return s;
00166 }
00167
00168 struct SwrContext *swr_alloc_set_opts(struct SwrContext *s,
00169 int64_t out_ch_layout, enum AVSampleFormat out_sample_fmt, int out_sample_rate,
00170 int64_t in_ch_layout, enum AVSampleFormat in_sample_fmt, int in_sample_rate,
00171 int log_offset, void *log_ctx){
00172 if(!s) s= swr_alloc();
00173 if(!s) return NULL;
00174
00175 s->log_level_offset= log_offset;
00176 s->log_ctx= log_ctx;
00177
00178 av_opt_set_int(s, "ocl", out_ch_layout, 0);
00179 av_opt_set_int(s, "osf", out_sample_fmt, 0);
00180 av_opt_set_int(s, "osr", out_sample_rate, 0);
00181 av_opt_set_int(s, "icl", in_ch_layout, 0);
00182 av_opt_set_int(s, "isf", in_sample_fmt, 0);
00183 av_opt_set_int(s, "isr", in_sample_rate, 0);
00184 av_opt_set_int(s, "tsf", AV_SAMPLE_FMT_NONE, 0);
00185 av_opt_set_int(s, "ich", av_get_channel_layout_nb_channels(s-> in_ch_layout), 0);
00186 av_opt_set_int(s, "och", av_get_channel_layout_nb_channels(s->out_ch_layout), 0);
00187 av_opt_set_int(s, "uch", 0, 0);
00188 return s;
00189 }
00190
00191 static void set_audiodata_fmt(AudioData *a, enum AVSampleFormat fmt){
00192 a->fmt = fmt;
00193 a->bps = av_get_bytes_per_sample(fmt);
00194 a->planar= av_sample_fmt_is_planar(fmt);
00195 }
00196
00197 static void free_temp(AudioData *a){
00198 av_free(a->data);
00199 memset(a, 0, sizeof(*a));
00200 }
00201
00202 av_cold void swr_free(SwrContext **ss){
00203 SwrContext *s= *ss;
00204 if(s){
00205 free_temp(&s->postin);
00206 free_temp(&s->midbuf);
00207 free_temp(&s->preout);
00208 free_temp(&s->in_buffer);
00209 free_temp(&s->dither);
00210 swri_audio_convert_free(&s-> in_convert);
00211 swri_audio_convert_free(&s->out_convert);
00212 swri_audio_convert_free(&s->full_convert);
00213 if (s->resampler)
00214 s->resampler->free(&s->resample);
00215 swri_rematrix_free(s);
00216 }
00217
00218 av_freep(ss);
00219 }
00220
00221 av_cold int swr_init(struct SwrContext *s){
00222 s->in_buffer_index= 0;
00223 s->in_buffer_count= 0;
00224 s->resample_in_constraint= 0;
00225 free_temp(&s->postin);
00226 free_temp(&s->midbuf);
00227 free_temp(&s->preout);
00228 free_temp(&s->in_buffer);
00229 free_temp(&s->dither);
00230 memset(s->in.ch, 0, sizeof(s->in.ch));
00231 memset(s->out.ch, 0, sizeof(s->out.ch));
00232 swri_audio_convert_free(&s-> in_convert);
00233 swri_audio_convert_free(&s->out_convert);
00234 swri_audio_convert_free(&s->full_convert);
00235 swri_rematrix_free(s);
00236
00237 s->flushed = 0;
00238
00239 if(s-> in_sample_fmt >= AV_SAMPLE_FMT_NB){
00240 av_log(s, AV_LOG_ERROR, "Requested input sample format %d is invalid\n", s->in_sample_fmt);
00241 return AVERROR(EINVAL);
00242 }
00243 if(s->out_sample_fmt >= AV_SAMPLE_FMT_NB){
00244 av_log(s, AV_LOG_ERROR, "Requested output sample format %d is invalid\n", s->out_sample_fmt);
00245 return AVERROR(EINVAL);
00246 }
00247
00248 if(s->int_sample_fmt == AV_SAMPLE_FMT_NONE){
00249 if(av_get_planar_sample_fmt(s->in_sample_fmt) <= AV_SAMPLE_FMT_S16P){
00250 s->int_sample_fmt= AV_SAMPLE_FMT_S16P;
00251 }else if(av_get_planar_sample_fmt(s->in_sample_fmt) <= AV_SAMPLE_FMT_FLTP){
00252 s->int_sample_fmt= AV_SAMPLE_FMT_FLTP;
00253 }else{
00254 av_log(s, AV_LOG_DEBUG, "Using double precision mode\n");
00255 s->int_sample_fmt= AV_SAMPLE_FMT_DBLP;
00256 }
00257 }
00258
00259 if( s->int_sample_fmt != AV_SAMPLE_FMT_S16P
00260 &&s->int_sample_fmt != AV_SAMPLE_FMT_S32P
00261 &&s->int_sample_fmt != AV_SAMPLE_FMT_FLTP
00262 &&s->int_sample_fmt != AV_SAMPLE_FMT_DBLP){
00263 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));
00264 return AVERROR(EINVAL);
00265 }
00266
00267 switch(s->engine){
00268 #if CONFIG_LIBSOXR
00269 extern struct Resampler const soxr_resampler;
00270 case SWR_ENGINE_SOXR: s->resampler = &soxr_resampler; break;
00271 #endif
00272 case SWR_ENGINE_SWR : s->resampler = &swri_resampler; break;
00273 default:
00274 av_log(s, AV_LOG_ERROR, "Requested resampling engine is unavailable\n");
00275 return AVERROR(EINVAL);
00276 }
00277
00278 set_audiodata_fmt(&s-> in, s-> in_sample_fmt);
00279 set_audiodata_fmt(&s->out, s->out_sample_fmt);
00280
00281 if (s->out_sample_rate!=s->in_sample_rate || (s->flags & SWR_FLAG_RESAMPLE)){
00282 s->resample = s->resampler->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, s->filter_type, s->kaiser_beta, s->precision, s->cheby);
00283 }else
00284 s->resampler->free(&s->resample);
00285 if( s->int_sample_fmt != AV_SAMPLE_FMT_S16P
00286 && s->int_sample_fmt != AV_SAMPLE_FMT_S32P
00287 && s->int_sample_fmt != AV_SAMPLE_FMT_FLTP
00288 && s->int_sample_fmt != AV_SAMPLE_FMT_DBLP
00289 && s->resample){
00290 av_log(s, AV_LOG_ERROR, "Resampling only supported with internal s16/s32/flt/dbl\n");
00291 return -1;
00292 }
00293
00294 if(!s->used_ch_count)
00295 s->used_ch_count= s->in.ch_count;
00296
00297 if(s->used_ch_count && s-> in_ch_layout && s->used_ch_count != av_get_channel_layout_nb_channels(s-> in_ch_layout)){
00298 av_log(s, AV_LOG_WARNING, "Input channel layout has a different number of channels than the number of used channels, ignoring layout\n");
00299 s-> in_ch_layout= 0;
00300 }
00301
00302 if(!s-> in_ch_layout)
00303 s-> in_ch_layout= av_get_default_channel_layout(s->used_ch_count);
00304 if(!s->out_ch_layout)
00305 s->out_ch_layout= av_get_default_channel_layout(s->out.ch_count);
00306
00307 s->rematrix= s->out_ch_layout !=s->in_ch_layout || s->rematrix_volume!=1.0 ||
00308 s->rematrix_custom;
00309
00310 #define RSC 1 //FIXME finetune
00311 if(!s-> in.ch_count)
00312 s-> in.ch_count= av_get_channel_layout_nb_channels(s-> in_ch_layout);
00313 if(!s->used_ch_count)
00314 s->used_ch_count= s->in.ch_count;
00315 if(!s->out.ch_count)
00316 s->out.ch_count= av_get_channel_layout_nb_channels(s->out_ch_layout);
00317
00318 if(!s-> in.ch_count){
00319 av_assert0(!s->in_ch_layout);
00320 av_log(s, AV_LOG_ERROR, "Input channel count and layout are unset\n");
00321 return -1;
00322 }
00323
00324 if ((!s->out_ch_layout || !s->in_ch_layout) && s->used_ch_count != s->out.ch_count && !s->rematrix_custom) {
00325 av_log(s, AV_LOG_ERROR, "Rematrix is needed but there is not enough information to do it\n");
00326 return -1;
00327 }
00328
00329 av_assert0(s->used_ch_count);
00330 av_assert0(s->out.ch_count);
00331 s->resample_first= RSC*s->out.ch_count/s->in.ch_count - RSC < s->out_sample_rate/(float)s-> in_sample_rate - 1.0;
00332
00333 s->in_buffer= s->in;
00334
00335 if(!s->resample && !s->rematrix && !s->channel_map && !s->dither_method){
00336 s->full_convert = swri_audio_convert_alloc(s->out_sample_fmt,
00337 s-> in_sample_fmt, s-> in.ch_count, NULL, 0);
00338 return 0;
00339 }
00340
00341 s->in_convert = swri_audio_convert_alloc(s->int_sample_fmt,
00342 s-> in_sample_fmt, s->used_ch_count, s->channel_map, 0);
00343 s->out_convert= swri_audio_convert_alloc(s->out_sample_fmt,
00344 s->int_sample_fmt, s->out.ch_count, NULL, 0);
00345
00346
00347 s->postin= s->in;
00348 s->preout= s->out;
00349 s->midbuf= s->in;
00350
00351 if(s->channel_map){
00352 s->postin.ch_count=
00353 s->midbuf.ch_count= s->used_ch_count;
00354 if(s->resample)
00355 s->in_buffer.ch_count= s->used_ch_count;
00356 }
00357 if(!s->resample_first){
00358 s->midbuf.ch_count= s->out.ch_count;
00359 if(s->resample)
00360 s->in_buffer.ch_count = s->out.ch_count;
00361 }
00362
00363 set_audiodata_fmt(&s->postin, s->int_sample_fmt);
00364 set_audiodata_fmt(&s->midbuf, s->int_sample_fmt);
00365 set_audiodata_fmt(&s->preout, s->int_sample_fmt);
00366
00367 if(s->resample){
00368 set_audiodata_fmt(&s->in_buffer, s->int_sample_fmt);
00369 }
00370
00371 s->dither = s->preout;
00372
00373 if(s->rematrix || s->dither_method)
00374 return swri_rematrix_init(s);
00375
00376 return 0;
00377 }
00378
00379 int swri_realloc_audio(AudioData *a, int count){
00380 int i, countb;
00381 AudioData old;
00382
00383 if(count < 0 || count > INT_MAX/2/a->bps/a->ch_count)
00384 return AVERROR(EINVAL);
00385
00386 if(a->count >= count)
00387 return 0;
00388
00389 count*=2;
00390
00391 countb= FFALIGN(count*a->bps, ALIGN);
00392 old= *a;
00393
00394 av_assert0(a->bps);
00395 av_assert0(a->ch_count);
00396
00397 a->data= av_mallocz(countb*a->ch_count);
00398 if(!a->data)
00399 return AVERROR(ENOMEM);
00400 for(i=0; i<a->ch_count; i++){
00401 a->ch[i]= a->data + i*(a->planar ? countb : a->bps);
00402 if(a->planar) memcpy(a->ch[i], old.ch[i], a->count*a->bps);
00403 }
00404 if(!a->planar) memcpy(a->ch[0], old.ch[0], a->count*a->ch_count*a->bps);
00405 av_free(old.data);
00406 a->count= count;
00407
00408 return 1;
00409 }
00410
00411 static void copy(AudioData *out, AudioData *in,
00412 int count){
00413 av_assert0(out->planar == in->planar);
00414 av_assert0(out->bps == in->bps);
00415 av_assert0(out->ch_count == in->ch_count);
00416 if(out->planar){
00417 int ch;
00418 for(ch=0; ch<out->ch_count; ch++)
00419 memcpy(out->ch[ch], in->ch[ch], count*out->bps);
00420 }else
00421 memcpy(out->ch[0], in->ch[0], count*out->ch_count*out->bps);
00422 }
00423
00424 static void fill_audiodata(AudioData *out, uint8_t *in_arg [SWR_CH_MAX]){
00425 int i;
00426 if(!in_arg){
00427 memset(out->ch, 0, sizeof(out->ch));
00428 }else if(out->planar){
00429 for(i=0; i<out->ch_count; i++)
00430 out->ch[i]= in_arg[i];
00431 }else{
00432 for(i=0; i<out->ch_count; i++)
00433 out->ch[i]= in_arg[0] + i*out->bps;
00434 }
00435 }
00436
00437 static void reversefill_audiodata(AudioData *out, uint8_t *in_arg [SWR_CH_MAX]){
00438 int i;
00439 if(out->planar){
00440 for(i=0; i<out->ch_count; i++)
00441 in_arg[i]= out->ch[i];
00442 }else{
00443 in_arg[0]= out->ch[0];
00444 }
00445 }
00446
00451 static void buf_set(AudioData *out, AudioData *in, int count){
00452 int ch;
00453 if(in->planar){
00454 for(ch=0; ch<out->ch_count; ch++)
00455 out->ch[ch]= in->ch[ch] + count*out->bps;
00456 }else{
00457 for(ch=out->ch_count-1; ch>=0; ch--)
00458 out->ch[ch]= in->ch[0] + (ch + count*out->ch_count) * out->bps;
00459 }
00460 }
00461
00466 static int resample(SwrContext *s, AudioData *out_param, int out_count,
00467 const AudioData * in_param, int in_count){
00468 AudioData in, out, tmp;
00469 int ret_sum=0;
00470 int border=0;
00471
00472 av_assert1(s->in_buffer.ch_count == in_param->ch_count);
00473 av_assert1(s->in_buffer.planar == in_param->planar);
00474 av_assert1(s->in_buffer.fmt == in_param->fmt);
00475
00476 tmp=out=*out_param;
00477 in = *in_param;
00478
00479 do{
00480 int ret, size, consumed;
00481 if(!s->resample_in_constraint && s->in_buffer_count){
00482 buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
00483 ret= s->resampler->multiple_resample(s->resample, &out, out_count, &tmp, s->in_buffer_count, &consumed);
00484 out_count -= ret;
00485 ret_sum += ret;
00486 buf_set(&out, &out, ret);
00487 s->in_buffer_count -= consumed;
00488 s->in_buffer_index += consumed;
00489
00490 if(!in_count)
00491 break;
00492 if(s->in_buffer_count <= border){
00493 buf_set(&in, &in, -s->in_buffer_count);
00494 in_count += s->in_buffer_count;
00495 s->in_buffer_count=0;
00496 s->in_buffer_index=0;
00497 border = 0;
00498 }
00499 }
00500
00501 if((s->flushed || in_count) && !s->in_buffer_count){
00502 s->in_buffer_index=0;
00503 ret= s->resampler->multiple_resample(s->resample, &out, out_count, &in, in_count, &consumed);
00504 out_count -= ret;
00505 ret_sum += ret;
00506 buf_set(&out, &out, ret);
00507 in_count -= consumed;
00508 buf_set(&in, &in, consumed);
00509 }
00510
00511
00512 size= s->in_buffer_index + s->in_buffer_count + in_count;
00513 if( size > s->in_buffer.count
00514 && s->in_buffer_count + in_count <= s->in_buffer_index){
00515 buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
00516 copy(&s->in_buffer, &tmp, s->in_buffer_count);
00517 s->in_buffer_index=0;
00518 }else
00519 if((ret=swri_realloc_audio(&s->in_buffer, size)) < 0)
00520 return ret;
00521
00522 if(in_count){
00523 int count= in_count;
00524 if(s->in_buffer_count && s->in_buffer_count+2 < count && out_count) count= s->in_buffer_count+2;
00525
00526 buf_set(&tmp, &s->in_buffer, s->in_buffer_index + s->in_buffer_count);
00527 copy(&tmp, &in, count);
00528 s->in_buffer_count += count;
00529 in_count -= count;
00530 border += count;
00531 buf_set(&in, &in, count);
00532 s->resample_in_constraint= 0;
00533 if(s->in_buffer_count != count || in_count)
00534 continue;
00535 }
00536 break;
00537 }while(1);
00538
00539 s->resample_in_constraint= !!out_count;
00540
00541 return ret_sum;
00542 }
00543
00544 static int swr_convert_internal(struct SwrContext *s, AudioData *out, int out_count,
00545 AudioData *in , int in_count){
00546 AudioData *postin, *midbuf, *preout;
00547 int ret;
00548 AudioData preout_tmp, midbuf_tmp;
00549
00550 if(s->full_convert){
00551 av_assert0(!s->resample);
00552 swri_audio_convert(s->full_convert, out, in, in_count);
00553 return out_count;
00554 }
00555
00556
00557
00558
00559 if((ret=swri_realloc_audio(&s->postin, in_count))<0)
00560 return ret;
00561 if(s->resample_first){
00562 av_assert0(s->midbuf.ch_count == s->used_ch_count);
00563 if((ret=swri_realloc_audio(&s->midbuf, out_count))<0)
00564 return ret;
00565 }else{
00566 av_assert0(s->midbuf.ch_count == s->out.ch_count);
00567 if((ret=swri_realloc_audio(&s->midbuf, in_count))<0)
00568 return ret;
00569 }
00570 if((ret=swri_realloc_audio(&s->preout, out_count))<0)
00571 return ret;
00572
00573 postin= &s->postin;
00574
00575 midbuf_tmp= s->midbuf;
00576 midbuf= &midbuf_tmp;
00577 preout_tmp= s->preout;
00578 preout= &preout_tmp;
00579
00580 if(s->int_sample_fmt == s-> in_sample_fmt && s->in.planar && !s->channel_map)
00581 postin= in;
00582
00583 if(s->resample_first ? !s->resample : !s->rematrix)
00584 midbuf= postin;
00585
00586 if(s->resample_first ? !s->rematrix : !s->resample)
00587 preout= midbuf;
00588
00589 if(s->int_sample_fmt == s->out_sample_fmt && s->out.planar){
00590 if(preout==in){
00591 out_count= FFMIN(out_count, in_count);
00592 av_assert0(s->in.planar);
00593 copy(out, in, out_count);
00594 return out_count;
00595 }
00596 else if(preout==postin) preout= midbuf= postin= out;
00597 else if(preout==midbuf) preout= midbuf= out;
00598 else preout= out;
00599 }
00600
00601 if(in != postin){
00602 swri_audio_convert(s->in_convert, postin, in, in_count);
00603 }
00604
00605 if(s->resample_first){
00606 if(postin != midbuf)
00607 out_count= resample(s, midbuf, out_count, postin, in_count);
00608 if(midbuf != preout)
00609 swri_rematrix(s, preout, midbuf, out_count, preout==out);
00610 }else{
00611 if(postin != midbuf)
00612 swri_rematrix(s, midbuf, postin, in_count, midbuf==out);
00613 if(midbuf != preout)
00614 out_count= resample(s, preout, out_count, midbuf, in_count);
00615 }
00616
00617 if(preout != out && out_count){
00618 if(s->dither_method){
00619 int ch;
00620 int dither_count= FFMAX(out_count, 1<<16);
00621 av_assert0(preout != in);
00622
00623 if((ret=swri_realloc_audio(&s->dither, dither_count))<0)
00624 return ret;
00625 if(ret)
00626 for(ch=0; ch<s->dither.ch_count; ch++)
00627 swri_get_dither(s, s->dither.ch[ch], s->dither.count, 12345678913579<<ch, s->out_sample_fmt, s->int_sample_fmt);
00628 av_assert0(s->dither.ch_count == preout->ch_count);
00629
00630 if(s->dither_pos + out_count > s->dither.count)
00631 s->dither_pos = 0;
00632
00633 for(ch=0; ch<preout->ch_count; ch++)
00634 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);
00635
00636 s->dither_pos += out_count;
00637 }
00638
00639 swri_audio_convert(s->out_convert, out, preout, out_count);
00640 }
00641 return out_count;
00642 }
00643
00644 int swr_convert(struct SwrContext *s, uint8_t *out_arg[SWR_CH_MAX], int out_count,
00645 const uint8_t *in_arg [SWR_CH_MAX], int in_count){
00646 AudioData * in= &s->in;
00647 AudioData *out= &s->out;
00648
00649 if(s->drop_output > 0){
00650 int ret;
00651 AudioData tmp = s->out;
00652 uint8_t *tmp_arg[SWR_CH_MAX];
00653 tmp.count = 0;
00654 tmp.data = NULL;
00655 if((ret=swri_realloc_audio(&tmp, s->drop_output))<0)
00656 return ret;
00657
00658 reversefill_audiodata(&tmp, tmp_arg);
00659 s->drop_output *= -1;
00660 ret = swr_convert(s, tmp_arg, -s->drop_output, in_arg, in_count);
00661 s->drop_output *= -1;
00662 if(ret>0)
00663 s->drop_output -= ret;
00664
00665 av_freep(&tmp.data);
00666 if(s->drop_output || !out_arg)
00667 return 0;
00668 in_count = 0;
00669 }
00670
00671 if(!in_arg){
00672 if(s->resample){
00673 if (!s->flushed)
00674 s->resampler->flush(s);
00675 s->resample_in_constraint = 0;
00676 s->flushed = 1;
00677 }else if(!s->in_buffer_count){
00678 return 0;
00679 }
00680 }else
00681 fill_audiodata(in , (void*)in_arg);
00682
00683 fill_audiodata(out, out_arg);
00684
00685 if(s->resample){
00686 int ret = swr_convert_internal(s, out, out_count, in, in_count);
00687 if(ret>0 && !s->drop_output)
00688 s->outpts += ret * (int64_t)s->in_sample_rate;
00689 return ret;
00690 }else{
00691 AudioData tmp= *in;
00692 int ret2=0;
00693 int ret, size;
00694 size = FFMIN(out_count, s->in_buffer_count);
00695 if(size){
00696 buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
00697 ret= swr_convert_internal(s, out, size, &tmp, size);
00698 if(ret<0)
00699 return ret;
00700 ret2= ret;
00701 s->in_buffer_count -= ret;
00702 s->in_buffer_index += ret;
00703 buf_set(out, out, ret);
00704 out_count -= ret;
00705 if(!s->in_buffer_count)
00706 s->in_buffer_index = 0;
00707 }
00708
00709 if(in_count){
00710 size= s->in_buffer_index + s->in_buffer_count + in_count - out_count;
00711
00712 if(in_count > out_count) {
00713 if( size > s->in_buffer.count
00714 && s->in_buffer_count + in_count - out_count <= s->in_buffer_index){
00715 buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
00716 copy(&s->in_buffer, &tmp, s->in_buffer_count);
00717 s->in_buffer_index=0;
00718 }else
00719 if((ret=swri_realloc_audio(&s->in_buffer, size)) < 0)
00720 return ret;
00721 }
00722
00723 if(out_count){
00724 size = FFMIN(in_count, out_count);
00725 ret= swr_convert_internal(s, out, size, in, size);
00726 if(ret<0)
00727 return ret;
00728 buf_set(in, in, ret);
00729 in_count -= ret;
00730 ret2 += ret;
00731 }
00732 if(in_count){
00733 buf_set(&tmp, &s->in_buffer, s->in_buffer_index + s->in_buffer_count);
00734 copy(&tmp, in, in_count);
00735 s->in_buffer_count += in_count;
00736 }
00737 }
00738 if(ret2>0 && !s->drop_output)
00739 s->outpts += ret2 * (int64_t)s->in_sample_rate;
00740 return ret2;
00741 }
00742 }
00743
00744 int swr_drop_output(struct SwrContext *s, int count){
00745 s->drop_output += count;
00746
00747 if(s->drop_output <= 0)
00748 return 0;
00749
00750 av_log(s, AV_LOG_VERBOSE, "discarding %d audio samples\n", count);
00751 return swr_convert(s, NULL, s->drop_output, NULL, 0);
00752 }
00753
00754 int swr_inject_silence(struct SwrContext *s, int count){
00755 int ret, i;
00756 AudioData silence = s->in;
00757 uint8_t *tmp_arg[SWR_CH_MAX];
00758
00759 if(count <= 0)
00760 return 0;
00761
00762 silence.count = 0;
00763 silence.data = NULL;
00764 if((ret=swri_realloc_audio(&silence, count))<0)
00765 return ret;
00766
00767 if(silence.planar) for(i=0; i<silence.ch_count; i++) {
00768 memset(silence.ch[i], silence.bps==1 ? 0x80 : 0, count*silence.bps);
00769 } else
00770 memset(silence.ch[0], silence.bps==1 ? 0x80 : 0, count*silence.bps*silence.ch_count);
00771
00772 reversefill_audiodata(&silence, tmp_arg);
00773 av_log(s, AV_LOG_VERBOSE, "adding %d audio samples of silence\n", count);
00774 ret = swr_convert(s, NULL, 0, (const uint8_t**)tmp_arg, count);
00775 av_freep(&silence.data);
00776 return ret;
00777 }
00778
00779 int64_t swr_get_delay(struct SwrContext *s, int64_t base){
00780 if (s->resampler && s->resample){
00781 return s->resampler->get_delay(s, base);
00782 }else{
00783 return (s->in_buffer_count*base + (s->in_sample_rate>>1))/ s->in_sample_rate;
00784 }
00785 }
00786
00787 int swr_set_compensation(struct SwrContext *s, int sample_delta, int compensation_distance){
00788 int ret;
00789
00790 if (!s || compensation_distance < 0)
00791 return AVERROR(EINVAL);
00792 if (!compensation_distance && sample_delta)
00793 return AVERROR(EINVAL);
00794 if (!s->resample) {
00795 s->flags |= SWR_FLAG_RESAMPLE;
00796 ret = swr_init(s);
00797 if (ret < 0)
00798 return ret;
00799 }
00800 if (!s->resampler->set_compensation){
00801 return AVERROR(EINVAL);
00802 }else{
00803 return s->resampler->set_compensation(s->resample, sample_delta, compensation_distance);
00804 }
00805 }
00806
00807 int64_t swr_next_pts(struct SwrContext *s, int64_t pts){
00808 if(pts == INT64_MIN)
00809 return s->outpts;
00810 if(s->min_compensation >= FLT_MAX) {
00811 return (s->outpts = pts - swr_get_delay(s, s->in_sample_rate * (int64_t)s->out_sample_rate));
00812 } else {
00813 int64_t delta = pts - swr_get_delay(s, s->in_sample_rate * (int64_t)s->out_sample_rate) - s->outpts;
00814 double fdelta = delta /(double)(s->in_sample_rate * (int64_t)s->out_sample_rate);
00815
00816 if(fabs(fdelta) > s->min_compensation) {
00817 if(!s->outpts || fabs(fdelta) > s->min_hard_compensation){
00818 int ret;
00819 if(delta > 0) ret = swr_inject_silence(s, delta / s->out_sample_rate);
00820 else ret = swr_drop_output (s, -delta / s-> in_sample_rate);
00821 if(ret<0){
00822 av_log(s, AV_LOG_ERROR, "Failed to compensate for timestamp delta of %f\n", fdelta);
00823 }
00824 } else if(s->soft_compensation_duration && s->max_soft_compensation) {
00825 int duration = s->out_sample_rate * s->soft_compensation_duration;
00826 double max_soft_compensation = s->max_soft_compensation / (s->max_soft_compensation < 0 ? -s->in_sample_rate : 1);
00827 int comp = av_clipf(fdelta, -max_soft_compensation, max_soft_compensation) * duration ;
00828 av_log(s, AV_LOG_VERBOSE, "compensating audio timestamp drift:%f compensation:%d in:%d\n", fdelta, comp, duration);
00829 swr_set_compensation(s, comp, duration);
00830 }
00831 }
00832
00833 return s->outpts;
00834 }
00835 }