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 #define C30DB M_SQRT2
00028 #define C15DB 1.189207115
00029 #define C__0DB 1.0
00030 #define C_15DB 0.840896415
00031 #define C_30DB M_SQRT1_2
00032 #define C_45DB 0.594603558
00033 #define C_60DB 0.5
00034
00035
00036
00037 #define OFFSET(x) offsetof(SwrContext,x)
00038 static const AVOption options[]={
00039 {"ich", "input channel count", OFFSET( in.ch_count ), AV_OPT_TYPE_INT, {.dbl=2}, 0, SWR_CH_MAX, 0},
00040 {"och", "output channel count", OFFSET(out.ch_count ), AV_OPT_TYPE_INT, {.dbl=2}, 0, SWR_CH_MAX, 0},
00041 {"uch", "used channel count", OFFSET(used_ch_count ), AV_OPT_TYPE_INT, {.dbl=0}, 0, SWR_CH_MAX, 0},
00042 {"isr", "input sample rate" , OFFSET( in_sample_rate), AV_OPT_TYPE_INT, {.dbl=48000}, 1, INT_MAX, 0},
00043 {"osr", "output sample rate" , OFFSET(out_sample_rate), AV_OPT_TYPE_INT, {.dbl=48000}, 1, INT_MAX, 0},
00044
00045
00046 {"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},
00047 {"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},
00048 {"tsf", "internal sample format", OFFSET(int_sample_fmt ), AV_OPT_TYPE_INT, {.dbl=AV_SAMPLE_FMT_NONE}, -1, AV_SAMPLE_FMT_FLT, 0},
00049 {"icl", "input channel layout" , OFFSET( in_ch_layout), AV_OPT_TYPE_INT64, {.dbl=0}, 0, INT64_MAX, 0, "channel_layout"},
00050 {"ocl", "output channel layout", OFFSET(out_ch_layout), AV_OPT_TYPE_INT64, {.dbl=0}, 0, INT64_MAX, 0, "channel_layout"},
00051 {"clev", "center mix level" , OFFSET(clev) , AV_OPT_TYPE_FLOAT, {.dbl=C_30DB}, 0, 4, 0},
00052 {"slev", "sourround mix level" , OFFSET(slev) , AV_OPT_TYPE_FLOAT, {.dbl=C_30DB}, 0, 4, 0},
00053 {"rmvol", "rematrix volume" , OFFSET(rematrix_volume), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, -1000, 1000, 0},
00054 {"flags", NULL , OFFSET(flags) , AV_OPT_TYPE_FLAGS, {.dbl=0}, 0, UINT_MAX, 0, "flags"},
00055 {"res", "force resampling", 0, AV_OPT_TYPE_CONST, {.dbl=SWR_FLAG_RESAMPLE}, INT_MIN, INT_MAX, 0, "flags"},
00056
00057 {0}
00058 };
00059
00060 static const char* context_to_name(void* ptr) {
00061 return "SWR";
00062 }
00063
00064 static const AVClass av_class = {
00065 .class_name = "SwrContext",
00066 .item_name = context_to_name,
00067 .option = options,
00068 .version = LIBAVUTIL_VERSION_INT,
00069 .log_level_offset_offset = OFFSET(log_level_offset),
00070 .parent_log_context_offset = OFFSET(log_ctx),
00071 };
00072
00073 unsigned swresample_version(void)
00074 {
00075 av_assert0(LIBSWRESAMPLE_VERSION_MICRO >= 100);
00076 return LIBSWRESAMPLE_VERSION_INT;
00077 }
00078
00079 const char *swresample_configuration(void)
00080 {
00081 return FFMPEG_CONFIGURATION;
00082 }
00083
00084 const char *swresample_license(void)
00085 {
00086 #define LICENSE_PREFIX "libswresample license: "
00087 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
00088 }
00089
00090 int swr_set_channel_mapping(struct SwrContext *s, const int *channel_map){
00091 if(!s || s->in_convert)
00092 return AVERROR(EINVAL);
00093 s->channel_map = channel_map;
00094 return 0;
00095 }
00096
00097 struct SwrContext *swr_alloc(void){
00098 SwrContext *s= av_mallocz(sizeof(SwrContext));
00099 if(s){
00100 s->av_class= &av_class;
00101 av_opt_set_defaults(s);
00102 }
00103 return s;
00104 }
00105
00106 struct SwrContext *swr_alloc_set_opts(struct SwrContext *s,
00107 int64_t out_ch_layout, enum AVSampleFormat out_sample_fmt, int out_sample_rate,
00108 int64_t in_ch_layout, enum AVSampleFormat in_sample_fmt, int in_sample_rate,
00109 int log_offset, void *log_ctx){
00110 if(!s) s= swr_alloc();
00111 if(!s) return NULL;
00112
00113 s->log_level_offset= log_offset;
00114 s->log_ctx= log_ctx;
00115
00116 av_opt_set_int(s, "ocl", out_ch_layout, 0);
00117 av_opt_set_int(s, "osf", out_sample_fmt, 0);
00118 av_opt_set_int(s, "osr", out_sample_rate, 0);
00119 av_opt_set_int(s, "icl", in_ch_layout, 0);
00120 av_opt_set_int(s, "isf", in_sample_fmt, 0);
00121 av_opt_set_int(s, "isr", in_sample_rate, 0);
00122 av_opt_set_int(s, "tsf", AV_SAMPLE_FMT_S16, 0);
00123 av_opt_set_int(s, "ich", av_get_channel_layout_nb_channels(s-> in_ch_layout), 0);
00124 av_opt_set_int(s, "och", av_get_channel_layout_nb_channels(s->out_ch_layout), 0);
00125 av_opt_set_int(s, "uch", 0, 0);
00126 return s;
00127 }
00128
00129
00130 static void free_temp(AudioData *a){
00131 av_free(a->data);
00132 memset(a, 0, sizeof(*a));
00133 }
00134
00135 void swr_free(SwrContext **ss){
00136 SwrContext *s= *ss;
00137 if(s){
00138 free_temp(&s->postin);
00139 free_temp(&s->midbuf);
00140 free_temp(&s->preout);
00141 free_temp(&s->in_buffer);
00142 swri_audio_convert_free(&s-> in_convert);
00143 swri_audio_convert_free(&s->out_convert);
00144 swri_audio_convert_free(&s->full_convert);
00145 swri_resample_free(&s->resample);
00146 }
00147
00148 av_freep(ss);
00149 }
00150
00151 int swr_init(struct SwrContext *s){
00152 s->in_buffer_index= 0;
00153 s->in_buffer_count= 0;
00154 s->resample_in_constraint= 0;
00155 free_temp(&s->postin);
00156 free_temp(&s->midbuf);
00157 free_temp(&s->preout);
00158 free_temp(&s->in_buffer);
00159 swri_audio_convert_free(&s-> in_convert);
00160 swri_audio_convert_free(&s->out_convert);
00161 swri_audio_convert_free(&s->full_convert);
00162
00163 s-> in.planar= av_sample_fmt_is_planar(s-> in_sample_fmt);
00164 s->out.planar= av_sample_fmt_is_planar(s->out_sample_fmt);
00165 s-> in_sample_fmt= av_get_alt_sample_fmt(s-> in_sample_fmt, 0);
00166 s->out_sample_fmt= av_get_alt_sample_fmt(s->out_sample_fmt, 0);
00167
00168 if(s-> in_sample_fmt >= AV_SAMPLE_FMT_NB){
00169 av_log(s, AV_LOG_ERROR, "Requested input sample format %d is invalid\n", s->in_sample_fmt);
00170 return AVERROR(EINVAL);
00171 }
00172 if(s->out_sample_fmt >= AV_SAMPLE_FMT_NB){
00173 av_log(s, AV_LOG_ERROR, "Requested output sample format %d is invalid\n", s->out_sample_fmt);
00174 return AVERROR(EINVAL);
00175 }
00176
00177 if( s->int_sample_fmt != AV_SAMPLE_FMT_S16
00178 &&s->int_sample_fmt != AV_SAMPLE_FMT_FLT){
00179 av_log(s, AV_LOG_ERROR, "Requested sample format %s is not supported internally, only float & S16 is supported\n", av_get_sample_fmt_name(s->int_sample_fmt));
00180 return AVERROR(EINVAL);
00181 }
00182
00183
00184 if(s->in_sample_fmt <= AV_SAMPLE_FMT_S16 || s->int_sample_fmt==AV_SAMPLE_FMT_S16){
00185 s->int_sample_fmt= AV_SAMPLE_FMT_S16;
00186 }else
00187 s->int_sample_fmt= AV_SAMPLE_FMT_FLT;
00188
00189
00190 if (s->out_sample_rate!=s->in_sample_rate || (s->flags & SWR_FLAG_RESAMPLE)){
00191 s->resample = swri_resample_init(s->resample, s->out_sample_rate, s->in_sample_rate, 16, 10, 0, 0.8);
00192 }else
00193 swri_resample_free(&s->resample);
00194 if(s->int_sample_fmt != AV_SAMPLE_FMT_S16 && s->resample){
00195 av_log(s, AV_LOG_ERROR, "Resampling only supported with internal s16 currently\n");
00196 return -1;
00197 }
00198
00199 if(!s->used_ch_count)
00200 s->used_ch_count= s->in.ch_count;
00201
00202 if(s->used_ch_count && s-> in_ch_layout && s->used_ch_count != av_get_channel_layout_nb_channels(s-> in_ch_layout)){
00203 av_log(s, AV_LOG_WARNING, "Input channel layout has a different number of channels than the number of used channels, ignoring layout\n");
00204 s-> in_ch_layout= 0;
00205 }
00206
00207 if(!s-> in_ch_layout)
00208 s-> in_ch_layout= av_get_default_channel_layout(s->used_ch_count);
00209 if(!s->out_ch_layout)
00210 s->out_ch_layout= av_get_default_channel_layout(s->out.ch_count);
00211
00212 s->rematrix= s->out_ch_layout !=s->in_ch_layout || s->rematrix_volume!=1.0;
00213
00214 #define RSC 1 //FIXME finetune
00215 if(!s-> in.ch_count)
00216 s-> in.ch_count= av_get_channel_layout_nb_channels(s-> in_ch_layout);
00217 if(!s->used_ch_count)
00218 s->used_ch_count= s->in.ch_count;
00219 if(!s->out.ch_count)
00220 s->out.ch_count= av_get_channel_layout_nb_channels(s->out_ch_layout);
00221
00222 if(!s-> in.ch_count){
00223 av_assert0(!s->in_ch_layout);
00224 av_log(s, AV_LOG_ERROR, "Input channel count and layout are unset\n");
00225 return -1;
00226 }
00227
00228 av_assert0(s->used_ch_count);
00229 av_assert0(s->out.ch_count);
00230 s->resample_first= RSC*s->out.ch_count/s->in.ch_count - RSC < s->out_sample_rate/(float)s-> in_sample_rate - 1.0;
00231
00232 s-> in.bps= av_get_bytes_per_sample(s-> in_sample_fmt);
00233 s->int_bps= av_get_bytes_per_sample(s->int_sample_fmt);
00234 s->out.bps= av_get_bytes_per_sample(s->out_sample_fmt);
00235
00236 if(!s->resample && !s->rematrix && !s->channel_map){
00237 s->full_convert = swri_audio_convert_alloc(s->out_sample_fmt,
00238 s-> in_sample_fmt, s-> in.ch_count, NULL, 0);
00239 return 0;
00240 }
00241
00242 s->in_convert = swri_audio_convert_alloc(s->int_sample_fmt,
00243 s-> in_sample_fmt, s->used_ch_count, s->channel_map, 0);
00244 s->out_convert= swri_audio_convert_alloc(s->out_sample_fmt,
00245 s->int_sample_fmt, s->out.ch_count, NULL, 0);
00246
00247
00248 s->postin= s->in;
00249 s->preout= s->out;
00250 s->midbuf= s->in;
00251 s->in_buffer= s->in;
00252 if(s->channel_map){
00253 s->postin.ch_count=
00254 s->midbuf.ch_count=
00255 s->in_buffer.ch_count= s->used_ch_count;
00256 }
00257 if(!s->resample_first){
00258 s->midbuf.ch_count= s->out.ch_count;
00259 s->in_buffer.ch_count = s->out.ch_count;
00260 }
00261
00262 s->in_buffer.bps = s->postin.bps = s->midbuf.bps = s->preout.bps = s->int_bps;
00263 s->in_buffer.planar = s->postin.planar = s->midbuf.planar = s->preout.planar = 1;
00264
00265
00266 if(s->rematrix)
00267 return swri_rematrix_init(s);
00268
00269 return 0;
00270 }
00271
00272 static int realloc_audio(AudioData *a, int count){
00273 int i, countb;
00274 AudioData old;
00275
00276 if(a->count >= count)
00277 return 0;
00278
00279 count*=2;
00280
00281 countb= FFALIGN(count*a->bps, 32);
00282 old= *a;
00283
00284 av_assert0(a->planar);
00285 av_assert0(a->bps);
00286 av_assert0(a->ch_count);
00287
00288 a->data= av_malloc(countb*a->ch_count);
00289 if(!a->data)
00290 return AVERROR(ENOMEM);
00291 for(i=0; i<a->ch_count; i++){
00292 a->ch[i]= a->data + i*(a->planar ? countb : a->bps);
00293 if(a->planar) memcpy(a->ch[i], old.ch[i], a->count*a->bps);
00294 }
00295 av_free(old.data);
00296 a->count= count;
00297
00298 return 1;
00299 }
00300
00301 static void copy(AudioData *out, AudioData *in,
00302 int count){
00303 av_assert0(out->planar == in->planar);
00304 av_assert0(out->bps == in->bps);
00305 av_assert0(out->ch_count == in->ch_count);
00306 if(out->planar){
00307 int ch;
00308 for(ch=0; ch<out->ch_count; ch++)
00309 memcpy(out->ch[ch], in->ch[ch], count*out->bps);
00310 }else
00311 memcpy(out->ch[0], in->ch[0], count*out->ch_count*out->bps);
00312 }
00313
00314 static void fill_audiodata(AudioData *out, uint8_t *in_arg [SWR_CH_MAX]){
00315 int i;
00316 if(out->planar){
00317 for(i=0; i<out->ch_count; i++)
00318 out->ch[i]= in_arg[i];
00319 }else{
00320 for(i=0; i<out->ch_count; i++)
00321 out->ch[i]= in_arg[0] + i*out->bps;
00322 }
00323 }
00324
00329 static void buf_set(AudioData *out, AudioData *in, int count){
00330 if(in->planar){
00331 int ch;
00332 for(ch=0; ch<out->ch_count; ch++)
00333 out->ch[ch]= in->ch[ch] + count*out->bps;
00334 }else
00335 out->ch[0]= in->ch[0] + count*out->ch_count*out->bps;
00336 }
00337
00342 static int resample(SwrContext *s, AudioData *out_param, int out_count,
00343 const AudioData * in_param, int in_count){
00344 AudioData in, out, tmp;
00345 int ret_sum=0;
00346 int border=0;
00347
00348 tmp=out=*out_param;
00349 in = *in_param;
00350
00351 do{
00352 int ret, size, consumed;
00353 if(!s->resample_in_constraint && s->in_buffer_count){
00354 buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
00355 ret= swri_multiple_resample(s->resample, &out, out_count, &tmp, s->in_buffer_count, &consumed);
00356 out_count -= ret;
00357 ret_sum += ret;
00358 buf_set(&out, &out, ret);
00359 s->in_buffer_count -= consumed;
00360 s->in_buffer_index += consumed;
00361
00362 if(!in_count)
00363 break;
00364 if(s->in_buffer_count <= border){
00365 buf_set(&in, &in, -s->in_buffer_count);
00366 in_count += s->in_buffer_count;
00367 s->in_buffer_count=0;
00368 s->in_buffer_index=0;
00369 border = 0;
00370 }
00371 }
00372
00373 if(in_count && !s->in_buffer_count){
00374 s->in_buffer_index=0;
00375 ret= swri_multiple_resample(s->resample, &out, out_count, &in, in_count, &consumed);
00376 out_count -= ret;
00377 ret_sum += ret;
00378 buf_set(&out, &out, ret);
00379 in_count -= consumed;
00380 buf_set(&in, &in, consumed);
00381 }
00382
00383
00384 size= s->in_buffer_index + s->in_buffer_count + in_count;
00385 if( size > s->in_buffer.count
00386 && s->in_buffer_count + in_count <= s->in_buffer_index){
00387 buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
00388 copy(&s->in_buffer, &tmp, s->in_buffer_count);
00389 s->in_buffer_index=0;
00390 }else
00391 if((ret=realloc_audio(&s->in_buffer, size)) < 0)
00392 return ret;
00393
00394 if(in_count){
00395 int count= in_count;
00396 if(s->in_buffer_count && s->in_buffer_count+2 < count && out_count) count= s->in_buffer_count+2;
00397
00398 buf_set(&tmp, &s->in_buffer, s->in_buffer_index + s->in_buffer_count);
00399 copy(&tmp, &in, count);
00400 s->in_buffer_count += count;
00401 in_count -= count;
00402 border += count;
00403 buf_set(&in, &in, count);
00404 s->resample_in_constraint= 0;
00405 if(s->in_buffer_count != count || in_count)
00406 continue;
00407 }
00408 break;
00409 }while(1);
00410
00411 s->resample_in_constraint= !!out_count;
00412
00413 return ret_sum;
00414 }
00415
00416 int swr_convert(struct SwrContext *s, uint8_t *out_arg[SWR_CH_MAX], int out_count,
00417 const uint8_t *in_arg [SWR_CH_MAX], int in_count){
00418 AudioData *postin, *midbuf, *preout;
00419 int ret;
00420 AudioData * in= &s->in;
00421 AudioData *out= &s->out;
00422 AudioData preout_tmp, midbuf_tmp;
00423
00424 if(!s->resample){
00425 if(in_count > out_count)
00426 return -1;
00427 out_count = in_count;
00428 }
00429
00430 if(!in_arg){
00431 if(s->in_buffer_count){
00432 AudioData *a= &s->in_buffer;
00433 int i, j, ret;
00434 if((ret=realloc_audio(a, s->in_buffer_index + 2*s->in_buffer_count)) < 0)
00435 return ret;
00436 av_assert0(a->planar);
00437 for(i=0; i<a->ch_count; i++){
00438 for(j=0; j<s->in_buffer_count; j++){
00439 memcpy(a->ch[i] + (s->in_buffer_index+s->in_buffer_count+j )*a->bps,
00440 a->ch[i] + (s->in_buffer_index+s->in_buffer_count-j-1)*a->bps, a->bps);
00441 }
00442 }
00443 s->in_buffer_count += (s->in_buffer_count+1)/2;
00444 s->resample_in_constraint = 0;
00445 }else{
00446 return 0;
00447 }
00448 }else
00449 fill_audiodata(in , (void*)in_arg);
00450 fill_audiodata(out, out_arg);
00451
00452 if(s->full_convert){
00453 av_assert0(!s->resample);
00454 swri_audio_convert(s->full_convert, out, in, in_count);
00455 return out_count;
00456 }
00457
00458
00459
00460
00461 if((ret=realloc_audio(&s->postin, in_count))<0)
00462 return ret;
00463 if(s->resample_first){
00464 av_assert0(s->midbuf.ch_count == s->used_ch_count);
00465 if((ret=realloc_audio(&s->midbuf, out_count))<0)
00466 return ret;
00467 }else{
00468 av_assert0(s->midbuf.ch_count == s->out.ch_count);
00469 if((ret=realloc_audio(&s->midbuf, in_count))<0)
00470 return ret;
00471 }
00472 if((ret=realloc_audio(&s->preout, out_count))<0)
00473 return ret;
00474
00475 postin= &s->postin;
00476
00477 midbuf_tmp= s->midbuf;
00478 midbuf= &midbuf_tmp;
00479 preout_tmp= s->preout;
00480 preout= &preout_tmp;
00481
00482 if(s->int_sample_fmt == s-> in_sample_fmt && s->in.planar)
00483 postin= in;
00484
00485 if(s->resample_first ? !s->resample : !s->rematrix)
00486 midbuf= postin;
00487
00488 if(s->resample_first ? !s->rematrix : !s->resample)
00489 preout= midbuf;
00490
00491 if(s->int_sample_fmt == s->out_sample_fmt && s->out.planar){
00492 if(preout==in){
00493 out_count= FFMIN(out_count, in_count);
00494 av_assert0(s->in.planar);
00495 copy(out, in, out_count);
00496 return out_count;
00497 }
00498 else if(preout==postin) preout= midbuf= postin= out;
00499 else if(preout==midbuf) preout= midbuf= out;
00500 else preout= out;
00501 }
00502
00503 if(in != postin){
00504 swri_audio_convert(s->in_convert, postin, in, in_count);
00505 }
00506
00507 if(s->resample_first){
00508 if(postin != midbuf)
00509 out_count= resample(s, midbuf, out_count, postin, in_count);
00510 if(midbuf != preout)
00511 swri_rematrix(s, preout, midbuf, out_count, preout==out);
00512 }else{
00513 if(postin != midbuf)
00514 swri_rematrix(s, midbuf, postin, in_count, midbuf==out);
00515 if(midbuf != preout)
00516 out_count= resample(s, preout, out_count, midbuf, in_count);
00517 }
00518
00519 if(preout != out){
00520
00521 swri_audio_convert(s->out_convert, out, preout, out_count);
00522 }
00523 if(!in_arg)
00524 s->in_buffer_count = 0;
00525 return out_count;
00526 }
00527