00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "libavutil/common.h"
00022 #include "libavutil/dict.h"
00023
00024 #include "libavutil/log.h"
00025 #include "libavutil/mem.h"
00026 #include "libavutil/opt.h"
00027
00028 #include "avresample.h"
00029 #include "audio_data.h"
00030 #include "internal.h"
00031
00032 int avresample_open(AVAudioResampleContext *avr)
00033 {
00034 int ret;
00035
00036
00037 avr->in_channels = av_get_channel_layout_nb_channels(avr->in_channel_layout);
00038 if (avr->in_channels <= 0 || avr->in_channels > AVRESAMPLE_MAX_CHANNELS) {
00039 av_log(avr, AV_LOG_ERROR, "Invalid input channel layout: %"PRIu64"\n",
00040 avr->in_channel_layout);
00041 return AVERROR(EINVAL);
00042 }
00043 avr->out_channels = av_get_channel_layout_nb_channels(avr->out_channel_layout);
00044 if (avr->out_channels <= 0 || avr->out_channels > AVRESAMPLE_MAX_CHANNELS) {
00045 av_log(avr, AV_LOG_ERROR, "Invalid output channel layout: %"PRIu64"\n",
00046 avr->out_channel_layout);
00047 return AVERROR(EINVAL);
00048 }
00049 avr->resample_channels = FFMIN(avr->in_channels, avr->out_channels);
00050 avr->downmix_needed = avr->in_channels > avr->out_channels;
00051 avr->upmix_needed = avr->out_channels > avr->in_channels ||
00052 (!avr->downmix_needed && (avr->mix_matrix ||
00053 avr->in_channel_layout != avr->out_channel_layout));
00054 avr->mixing_needed = avr->downmix_needed || avr->upmix_needed;
00055
00056
00057 avr->resample_needed = avr->in_sample_rate != avr->out_sample_rate ||
00058 avr->force_resampling;
00059
00060
00061 if (avr->internal_sample_fmt == AV_SAMPLE_FMT_NONE &&
00062 (avr->mixing_needed || avr->resample_needed)) {
00063 enum AVSampleFormat in_fmt = av_get_planar_sample_fmt(avr->in_sample_fmt);
00064 enum AVSampleFormat out_fmt = av_get_planar_sample_fmt(avr->out_sample_fmt);
00065 int max_bps = FFMAX(av_get_bytes_per_sample(in_fmt),
00066 av_get_bytes_per_sample(out_fmt));
00067 if (max_bps <= 2) {
00068 avr->internal_sample_fmt = AV_SAMPLE_FMT_S16P;
00069 } else if (avr->mixing_needed) {
00070 avr->internal_sample_fmt = AV_SAMPLE_FMT_FLTP;
00071 } else {
00072 if (max_bps <= 4) {
00073 if (in_fmt == AV_SAMPLE_FMT_S32P ||
00074 out_fmt == AV_SAMPLE_FMT_S32P) {
00075 if (in_fmt == AV_SAMPLE_FMT_FLTP ||
00076 out_fmt == AV_SAMPLE_FMT_FLTP) {
00077
00078 avr->internal_sample_fmt = AV_SAMPLE_FMT_DBLP;
00079 } else {
00080
00081 avr->internal_sample_fmt = AV_SAMPLE_FMT_S32P;
00082 }
00083 } else {
00084
00085 avr->internal_sample_fmt = AV_SAMPLE_FMT_FLTP;
00086 }
00087 } else {
00088
00089 avr->internal_sample_fmt = AV_SAMPLE_FMT_DBLP;
00090 }
00091 }
00092 av_log(avr, AV_LOG_DEBUG, "Using %s as internal sample format\n",
00093 av_get_sample_fmt_name(avr->internal_sample_fmt));
00094 }
00095
00096
00097 if (avr->in_channels == 1)
00098 avr->in_sample_fmt = av_get_planar_sample_fmt(avr->in_sample_fmt);
00099 if (avr->out_channels == 1)
00100 avr->out_sample_fmt = av_get_planar_sample_fmt(avr->out_sample_fmt);
00101 avr->in_convert_needed = (avr->resample_needed || avr->mixing_needed) &&
00102 avr->in_sample_fmt != avr->internal_sample_fmt;
00103 if (avr->resample_needed || avr->mixing_needed)
00104 avr->out_convert_needed = avr->internal_sample_fmt != avr->out_sample_fmt;
00105 else
00106 avr->out_convert_needed = avr->in_sample_fmt != avr->out_sample_fmt;
00107
00108
00109 if (avr->mixing_needed || avr->in_convert_needed) {
00110 avr->in_buffer = ff_audio_data_alloc(FFMAX(avr->in_channels, avr->out_channels),
00111 0, avr->internal_sample_fmt,
00112 "in_buffer");
00113 if (!avr->in_buffer) {
00114 ret = AVERROR(EINVAL);
00115 goto error;
00116 }
00117 }
00118 if (avr->resample_needed) {
00119 avr->resample_out_buffer = ff_audio_data_alloc(avr->out_channels,
00120 0, avr->internal_sample_fmt,
00121 "resample_out_buffer");
00122 if (!avr->resample_out_buffer) {
00123 ret = AVERROR(EINVAL);
00124 goto error;
00125 }
00126 }
00127 if (avr->out_convert_needed) {
00128 avr->out_buffer = ff_audio_data_alloc(avr->out_channels, 0,
00129 avr->out_sample_fmt, "out_buffer");
00130 if (!avr->out_buffer) {
00131 ret = AVERROR(EINVAL);
00132 goto error;
00133 }
00134 }
00135 avr->out_fifo = av_audio_fifo_alloc(avr->out_sample_fmt, avr->out_channels,
00136 1024);
00137 if (!avr->out_fifo) {
00138 ret = AVERROR(ENOMEM);
00139 goto error;
00140 }
00141
00142
00143 if (avr->in_convert_needed) {
00144 avr->ac_in = ff_audio_convert_alloc(avr, avr->internal_sample_fmt,
00145 avr->in_sample_fmt, avr->in_channels);
00146 if (!avr->ac_in) {
00147 ret = AVERROR(ENOMEM);
00148 goto error;
00149 }
00150 }
00151 if (avr->out_convert_needed) {
00152 enum AVSampleFormat src_fmt;
00153 if (avr->in_convert_needed)
00154 src_fmt = avr->internal_sample_fmt;
00155 else
00156 src_fmt = avr->in_sample_fmt;
00157 avr->ac_out = ff_audio_convert_alloc(avr, avr->out_sample_fmt, src_fmt,
00158 avr->out_channels);
00159 if (!avr->ac_out) {
00160 ret = AVERROR(ENOMEM);
00161 goto error;
00162 }
00163 }
00164 if (avr->resample_needed) {
00165 avr->resample = ff_audio_resample_init(avr);
00166 if (!avr->resample) {
00167 ret = AVERROR(ENOMEM);
00168 goto error;
00169 }
00170 }
00171 if (avr->mixing_needed) {
00172 avr->am = ff_audio_mix_alloc(avr);
00173 if (!avr->am) {
00174 ret = AVERROR(ENOMEM);
00175 goto error;
00176 }
00177 }
00178
00179 return 0;
00180
00181 error:
00182 avresample_close(avr);
00183 return ret;
00184 }
00185
00186 void avresample_close(AVAudioResampleContext *avr)
00187 {
00188 ff_audio_data_free(&avr->in_buffer);
00189 ff_audio_data_free(&avr->resample_out_buffer);
00190 ff_audio_data_free(&avr->out_buffer);
00191 av_audio_fifo_free(avr->out_fifo);
00192 avr->out_fifo = NULL;
00193 av_freep(&avr->ac_in);
00194 av_freep(&avr->ac_out);
00195 ff_audio_resample_free(&avr->resample);
00196 ff_audio_mix_free(&avr->am);
00197 av_freep(&avr->mix_matrix);
00198 }
00199
00200 void avresample_free(AVAudioResampleContext **avr)
00201 {
00202 if (!*avr)
00203 return;
00204 avresample_close(*avr);
00205 av_opt_free(*avr);
00206 av_freep(avr);
00207 }
00208
00209 static int handle_buffered_output(AVAudioResampleContext *avr,
00210 AudioData *output, AudioData *converted)
00211 {
00212 int ret;
00213
00214 if (!output || av_audio_fifo_size(avr->out_fifo) > 0 ||
00215 (converted && output->allocated_samples < converted->nb_samples)) {
00216 if (converted) {
00217
00218
00219
00220 av_dlog(avr, "[FIFO] add %s to out_fifo\n", converted->name);
00221 ret = ff_audio_data_add_to_fifo(avr->out_fifo, converted, 0,
00222 converted->nb_samples);
00223 if (ret < 0)
00224 return ret;
00225 }
00226
00227
00228
00229 if (output && output->allocated_samples > 0) {
00230 av_dlog(avr, "[FIFO] read from out_fifo to output\n");
00231 av_dlog(avr, "[end conversion]\n");
00232 return ff_audio_data_read_from_fifo(avr->out_fifo, output,
00233 output->allocated_samples);
00234 }
00235 } else if (converted) {
00236
00237
00238 av_dlog(avr, "[copy] %s to output\n", converted->name);
00239 output->nb_samples = 0;
00240 ret = ff_audio_data_copy(output, converted);
00241 if (ret < 0)
00242 return ret;
00243 av_dlog(avr, "[end conversion]\n");
00244 return output->nb_samples;
00245 }
00246 av_dlog(avr, "[end conversion]\n");
00247 return 0;
00248 }
00249
00250 int attribute_align_arg avresample_convert(AVAudioResampleContext *avr,
00251 uint8_t **output, int out_plane_size,
00252 int out_samples, uint8_t **input,
00253 int in_plane_size, int in_samples)
00254 {
00255 AudioData input_buffer;
00256 AudioData output_buffer;
00257 AudioData *current_buffer;
00258 int ret, direct_output;
00259
00260
00261 if (avr->in_buffer) {
00262 avr->in_buffer->nb_samples = 0;
00263 ff_audio_data_set_channels(avr->in_buffer,
00264 avr->in_buffer->allocated_channels);
00265 }
00266 if (avr->resample_out_buffer) {
00267 avr->resample_out_buffer->nb_samples = 0;
00268 ff_audio_data_set_channels(avr->resample_out_buffer,
00269 avr->resample_out_buffer->allocated_channels);
00270 }
00271 if (avr->out_buffer) {
00272 avr->out_buffer->nb_samples = 0;
00273 ff_audio_data_set_channels(avr->out_buffer,
00274 avr->out_buffer->allocated_channels);
00275 }
00276
00277 av_dlog(avr, "[start conversion]\n");
00278
00279
00280 direct_output = output && av_audio_fifo_size(avr->out_fifo) == 0;
00281 if (output) {
00282 ret = ff_audio_data_init(&output_buffer, output, out_plane_size,
00283 avr->out_channels, out_samples,
00284 avr->out_sample_fmt, 0, "output");
00285 if (ret < 0)
00286 return ret;
00287 output_buffer.nb_samples = 0;
00288 }
00289
00290 if (input) {
00291
00292 ret = ff_audio_data_init(&input_buffer, input, in_plane_size,
00293 avr->in_channels, in_samples,
00294 avr->in_sample_fmt, 1, "input");
00295 if (ret < 0)
00296 return ret;
00297 current_buffer = &input_buffer;
00298
00299 if (avr->upmix_needed && !avr->in_convert_needed && !avr->resample_needed &&
00300 !avr->out_convert_needed && direct_output && out_samples >= in_samples) {
00301
00302
00303 av_dlog(avr, "[copy] %s to output\n", current_buffer->name);
00304 ret = ff_audio_data_copy(&output_buffer, current_buffer);
00305 if (ret < 0)
00306 return ret;
00307 current_buffer = &output_buffer;
00308 } else if (avr->mixing_needed || avr->in_convert_needed) {
00309
00310
00311 if (avr->in_convert_needed) {
00312 ret = ff_audio_data_realloc(avr->in_buffer,
00313 current_buffer->nb_samples);
00314 if (ret < 0)
00315 return ret;
00316 av_dlog(avr, "[convert] %s to in_buffer\n", current_buffer->name);
00317 ret = ff_audio_convert(avr->ac_in, avr->in_buffer,
00318 current_buffer);
00319 if (ret < 0)
00320 return ret;
00321 } else {
00322 av_dlog(avr, "[copy] %s to in_buffer\n", current_buffer->name);
00323 ret = ff_audio_data_copy(avr->in_buffer, current_buffer);
00324 if (ret < 0)
00325 return ret;
00326 }
00327 ff_audio_data_set_channels(avr->in_buffer, avr->in_channels);
00328 if (avr->downmix_needed) {
00329 av_dlog(avr, "[downmix] in_buffer\n");
00330 ret = ff_audio_mix(avr->am, avr->in_buffer);
00331 if (ret < 0)
00332 return ret;
00333 }
00334 current_buffer = avr->in_buffer;
00335 }
00336 } else {
00337
00338 if (!avr->resample_needed)
00339 return handle_buffered_output(avr, output ? &output_buffer : NULL,
00340 NULL);
00341 current_buffer = NULL;
00342 }
00343
00344 if (avr->resample_needed) {
00345 AudioData *resample_out;
00346
00347 if (!avr->out_convert_needed && direct_output && out_samples > 0)
00348 resample_out = &output_buffer;
00349 else
00350 resample_out = avr->resample_out_buffer;
00351 av_dlog(avr, "[resample] %s to %s\n", current_buffer->name,
00352 resample_out->name);
00353 ret = ff_audio_resample(avr->resample, resample_out,
00354 current_buffer);
00355 if (ret < 0)
00356 return ret;
00357
00358
00359 if (resample_out->nb_samples == 0) {
00360 av_dlog(avr, "[end conversion]\n");
00361 return 0;
00362 }
00363
00364 current_buffer = resample_out;
00365 }
00366
00367 if (avr->upmix_needed) {
00368 av_dlog(avr, "[upmix] %s\n", current_buffer->name);
00369 ret = ff_audio_mix(avr->am, current_buffer);
00370 if (ret < 0)
00371 return ret;
00372 }
00373
00374
00375 if (current_buffer == &output_buffer) {
00376 av_dlog(avr, "[end conversion]\n");
00377 return current_buffer->nb_samples;
00378 }
00379
00380 if (avr->out_convert_needed) {
00381 if (direct_output && out_samples >= current_buffer->nb_samples) {
00382
00383 av_dlog(avr, "[convert] %s to output\n", current_buffer->name);
00384 ret = ff_audio_convert(avr->ac_out, &output_buffer, current_buffer);
00385 if (ret < 0)
00386 return ret;
00387
00388 av_dlog(avr, "[end conversion]\n");
00389 return output_buffer.nb_samples;
00390 } else {
00391 ret = ff_audio_data_realloc(avr->out_buffer,
00392 current_buffer->nb_samples);
00393 if (ret < 0)
00394 return ret;
00395 av_dlog(avr, "[convert] %s to out_buffer\n", current_buffer->name);
00396 ret = ff_audio_convert(avr->ac_out, avr->out_buffer,
00397 current_buffer);
00398 if (ret < 0)
00399 return ret;
00400 current_buffer = avr->out_buffer;
00401 }
00402 }
00403
00404 return handle_buffered_output(avr, output ? &output_buffer : NULL,
00405 current_buffer);
00406 }
00407
00408 int avresample_get_matrix(AVAudioResampleContext *avr, double *matrix,
00409 int stride)
00410 {
00411 int in_channels, out_channels, i, o;
00412
00413 if (avr->am)
00414 return ff_audio_mix_get_matrix(avr->am, matrix, stride);
00415
00416 in_channels = av_get_channel_layout_nb_channels(avr->in_channel_layout);
00417 out_channels = av_get_channel_layout_nb_channels(avr->out_channel_layout);
00418
00419 if ( in_channels <= 0 || in_channels > AVRESAMPLE_MAX_CHANNELS ||
00420 out_channels <= 0 || out_channels > AVRESAMPLE_MAX_CHANNELS) {
00421 av_log(avr, AV_LOG_ERROR, "Invalid channel layouts\n");
00422 return AVERROR(EINVAL);
00423 }
00424
00425 if (!avr->mix_matrix) {
00426 av_log(avr, AV_LOG_ERROR, "matrix is not set\n");
00427 return AVERROR(EINVAL);
00428 }
00429
00430 for (o = 0; o < out_channels; o++)
00431 for (i = 0; i < in_channels; i++)
00432 matrix[o * stride + i] = avr->mix_matrix[o * in_channels + i];
00433
00434 return 0;
00435 }
00436
00437 int avresample_set_matrix(AVAudioResampleContext *avr, const double *matrix,
00438 int stride)
00439 {
00440 int in_channels, out_channels, i, o;
00441
00442 if (avr->am)
00443 return ff_audio_mix_set_matrix(avr->am, matrix, stride);
00444
00445 in_channels = av_get_channel_layout_nb_channels(avr->in_channel_layout);
00446 out_channels = av_get_channel_layout_nb_channels(avr->out_channel_layout);
00447
00448 if ( in_channels <= 0 || in_channels > AVRESAMPLE_MAX_CHANNELS ||
00449 out_channels <= 0 || out_channels > AVRESAMPLE_MAX_CHANNELS) {
00450 av_log(avr, AV_LOG_ERROR, "Invalid channel layouts\n");
00451 return AVERROR(EINVAL);
00452 }
00453
00454 if (avr->mix_matrix)
00455 av_freep(&avr->mix_matrix);
00456 avr->mix_matrix = av_malloc(in_channels * out_channels *
00457 sizeof(*avr->mix_matrix));
00458 if (!avr->mix_matrix)
00459 return AVERROR(ENOMEM);
00460
00461 for (o = 0; o < out_channels; o++)
00462 for (i = 0; i < in_channels; i++)
00463 avr->mix_matrix[o * in_channels + i] = matrix[o * stride + i];
00464
00465 return 0;
00466 }
00467
00468 int avresample_available(AVAudioResampleContext *avr)
00469 {
00470 return av_audio_fifo_size(avr->out_fifo);
00471 }
00472
00473 int avresample_read(AVAudioResampleContext *avr, uint8_t **output, int nb_samples)
00474 {
00475 if (!output)
00476 return av_audio_fifo_drain(avr->out_fifo, nb_samples);
00477 return av_audio_fifo_read(avr->out_fifo, (void**)output, nb_samples);
00478 }
00479
00480 unsigned avresample_version(void)
00481 {
00482 return LIBAVRESAMPLE_VERSION_INT;
00483 }
00484
00485 const char *avresample_license(void)
00486 {
00487 #define LICENSE_PREFIX "libavresample license: "
00488 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
00489 }
00490
00491 const char *avresample_configuration(void)
00492 {
00493 return FFMPEG_CONFIGURATION;
00494 }