utils.c 28.4 KB
Newer Older
Justin Ruggles's avatar
Justin Ruggles committed
1 2 3
/*
 * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
 *
4
 * This file is part of FFmpeg.
Justin Ruggles's avatar
Justin Ruggles committed
5
 *
6
 * FFmpeg is free software; you can redistribute it and/or
Justin Ruggles's avatar
Justin Ruggles committed
7 8 9 10
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
11
 * FFmpeg is distributed in the hope that it will be useful,
Justin Ruggles's avatar
Justin Ruggles committed
12 13 14 15 16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with FFmpeg; if not, write to the Free Software
Justin Ruggles's avatar
Justin Ruggles committed
18 19 20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

21
#include "libavutil/common.h"
Justin Ruggles's avatar
Justin Ruggles committed
22
#include "libavutil/dict.h"
23
// #include "libavutil/error.h"
24
#include "libavutil/frame.h"
Justin Ruggles's avatar
Justin Ruggles committed
25 26 27 28 29 30
#include "libavutil/log.h"
#include "libavutil/mem.h"
#include "libavutil/opt.h"

#include "avresample.h"
#include "internal.h"
31 32 33 34
#include "audio_data.h"
#include "audio_convert.h"
#include "audio_mix.h"
#include "resample.h"
Justin Ruggles's avatar
Justin Ruggles committed
35 36 37 38 39

int avresample_open(AVAudioResampleContext *avr)
{
    int ret;

40 41 42 43 44
    if (avresample_is_open(avr)) {
        av_log(avr, AV_LOG_ERROR, "The resampling context is already open.\n");
        return AVERROR(EINVAL);
    }

Justin Ruggles's avatar
Justin Ruggles committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
    /* set channel mixing parameters */
    avr->in_channels = av_get_channel_layout_nb_channels(avr->in_channel_layout);
    if (avr->in_channels <= 0 || avr->in_channels > AVRESAMPLE_MAX_CHANNELS) {
        av_log(avr, AV_LOG_ERROR, "Invalid input channel layout: %"PRIu64"\n",
               avr->in_channel_layout);
        return AVERROR(EINVAL);
    }
    avr->out_channels = av_get_channel_layout_nb_channels(avr->out_channel_layout);
    if (avr->out_channels <= 0 || avr->out_channels > AVRESAMPLE_MAX_CHANNELS) {
        av_log(avr, AV_LOG_ERROR, "Invalid output channel layout: %"PRIu64"\n",
               avr->out_channel_layout);
        return AVERROR(EINVAL);
    }
    avr->resample_channels = FFMIN(avr->in_channels, avr->out_channels);
    avr->downmix_needed    = avr->in_channels  > avr->out_channels;
    avr->upmix_needed      = avr->out_channels > avr->in_channels ||
61
                             (!avr->downmix_needed && (avr->mix_matrix ||
62
                              avr->in_channel_layout != avr->out_channel_layout));
Justin Ruggles's avatar
Justin Ruggles committed
63 64 65 66 67 68
    avr->mixing_needed     = avr->downmix_needed || avr->upmix_needed;

    /* set resampling parameters */
    avr->resample_needed   = avr->in_sample_rate != avr->out_sample_rate ||
                             avr->force_resampling;

69 70 71 72 73 74 75
    /* select internal sample format if not specified by the user */
    if (avr->internal_sample_fmt == AV_SAMPLE_FMT_NONE &&
        (avr->mixing_needed || avr->resample_needed)) {
        enum AVSampleFormat  in_fmt = av_get_planar_sample_fmt(avr->in_sample_fmt);
        enum AVSampleFormat out_fmt = av_get_planar_sample_fmt(avr->out_sample_fmt);
        int max_bps = FFMAX(av_get_bytes_per_sample(in_fmt),
                            av_get_bytes_per_sample(out_fmt));
76
        if (max_bps <= 2) {
77 78 79
            avr->internal_sample_fmt = AV_SAMPLE_FMT_S16P;
        } else if (avr->mixing_needed) {
            avr->internal_sample_fmt = AV_SAMPLE_FMT_FLTP;
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
        } else {
            if (max_bps <= 4) {
                if (in_fmt  == AV_SAMPLE_FMT_S32P ||
                    out_fmt == AV_SAMPLE_FMT_S32P) {
                    if (in_fmt  == AV_SAMPLE_FMT_FLTP ||
                        out_fmt == AV_SAMPLE_FMT_FLTP) {
                        /* if one is s32 and the other is flt, use dbl */
                        avr->internal_sample_fmt = AV_SAMPLE_FMT_DBLP;
                    } else {
                        /* if one is s32 and the other is s32, s16, or u8, use s32 */
                        avr->internal_sample_fmt = AV_SAMPLE_FMT_S32P;
                    }
                } else {
                    /* if one is flt and the other is flt, s16 or u8, use flt */
                    avr->internal_sample_fmt = AV_SAMPLE_FMT_FLTP;
                }
            } else {
                /* if either is dbl, use dbl */
                avr->internal_sample_fmt = AV_SAMPLE_FMT_DBLP;
            }
100 101 102
        }
        av_log(avr, AV_LOG_DEBUG, "Using %s as internal sample format\n",
               av_get_sample_fmt_name(avr->internal_sample_fmt));
Justin Ruggles's avatar
Justin Ruggles committed
103
    }
104

105 106 107
    /* we may need to add an extra conversion in order to remap channels if
       the output format is not planar */
    if (avr->use_channel_map && !avr->mixing_needed && !avr->resample_needed &&
108
        !ff_sample_fmt_is_planar(avr->out_sample_fmt, avr->out_channels)) {
109 110 111 112
        avr->internal_sample_fmt = av_get_planar_sample_fmt(avr->out_sample_fmt);
    }

    /* set sample format conversion parameters */
Justin Ruggles's avatar
Justin Ruggles committed
113
    if (avr->resample_needed || avr->mixing_needed)
114 115 116
        avr->in_convert_needed = avr->in_sample_fmt != avr->internal_sample_fmt;
    else
        avr->in_convert_needed = avr->use_channel_map &&
117
                                 !ff_sample_fmt_is_planar(avr->out_sample_fmt, avr->out_channels);
118 119

    if (avr->resample_needed || avr->mixing_needed || avr->in_convert_needed)
Justin Ruggles's avatar
Justin Ruggles committed
120 121 122 123
        avr->out_convert_needed = avr->internal_sample_fmt != avr->out_sample_fmt;
    else
        avr->out_convert_needed = avr->in_sample_fmt != avr->out_sample_fmt;

124 125 126 127 128 129
    avr->in_copy_needed = !avr->in_convert_needed && (avr->mixing_needed ||
                          (avr->use_channel_map && avr->resample_needed));

    if (avr->use_channel_map) {
        if (avr->in_copy_needed) {
            avr->remap_point = REMAP_IN_COPY;
130
            av_log(avr, AV_LOG_TRACE, "remap channels during in_copy\n");
131 132
        } else if (avr->in_convert_needed) {
            avr->remap_point = REMAP_IN_CONVERT;
133
            av_log(avr, AV_LOG_TRACE, "remap channels during in_convert\n");
134 135
        } else if (avr->out_convert_needed) {
            avr->remap_point = REMAP_OUT_CONVERT;
136
            av_log(avr, AV_LOG_TRACE, "remap channels during out_convert\n");
137 138
        } else {
            avr->remap_point = REMAP_OUT_COPY;
139
            av_log(avr, AV_LOG_TRACE, "remap channels during out_copy\n");
140 141 142 143 144
        }

#ifdef DEBUG
        {
            int ch;
145
            av_log(avr, AV_LOG_TRACE, "output map: ");
146 147
            if (avr->ch_map_info.do_remap)
                for (ch = 0; ch < avr->in_channels; ch++)
148
                    av_log(avr, AV_LOG_TRACE, " % 2d", avr->ch_map_info.channel_map[ch]);
149
            else
150 151 152
                av_log(avr, AV_LOG_TRACE, "n/a");
            av_log(avr, AV_LOG_TRACE, "\n");
            av_log(avr, AV_LOG_TRACE, "copy map:   ");
153 154
            if (avr->ch_map_info.do_copy)
                for (ch = 0; ch < avr->in_channels; ch++)
155
                    av_log(avr, AV_LOG_TRACE, " % 2d", avr->ch_map_info.channel_copy[ch]);
156
            else
157 158 159
                av_log(avr, AV_LOG_TRACE, "n/a");
            av_log(avr, AV_LOG_TRACE, "\n");
            av_log(avr, AV_LOG_TRACE, "zero map:   ");
160 161
            if (avr->ch_map_info.do_zero)
                for (ch = 0; ch < avr->in_channels; ch++)
162
                    av_log(avr, AV_LOG_TRACE, " % 2d", avr->ch_map_info.channel_zero[ch]);
163
            else
164 165 166
                av_log(avr, AV_LOG_TRACE, "n/a");
            av_log(avr, AV_LOG_TRACE, "\n");
            av_log(avr, AV_LOG_TRACE, "input map:  ");
167
            for (ch = 0; ch < avr->in_channels; ch++)
168 169
                av_log(avr, AV_LOG_TRACE, " % 2d", avr->ch_map_info.input_map[ch]);
            av_log(avr, AV_LOG_TRACE, "\n");
170 171 172 173 174
        }
#endif
    } else
        avr->remap_point = REMAP_NONE;

Justin Ruggles's avatar
Justin Ruggles committed
175
    /* allocate buffers */
176
    if (avr->in_copy_needed || avr->in_convert_needed) {
Justin Ruggles's avatar
Justin Ruggles committed
177 178 179 180 181 182 183 184 185 186
        avr->in_buffer = ff_audio_data_alloc(FFMAX(avr->in_channels, avr->out_channels),
                                             0, avr->internal_sample_fmt,
                                             "in_buffer");
        if (!avr->in_buffer) {
            ret = AVERROR(EINVAL);
            goto error;
        }
    }
    if (avr->resample_needed) {
        avr->resample_out_buffer = ff_audio_data_alloc(avr->out_channels,
187
                                                       1024, avr->internal_sample_fmt,
Justin Ruggles's avatar
Justin Ruggles committed
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
                                                       "resample_out_buffer");
        if (!avr->resample_out_buffer) {
            ret = AVERROR(EINVAL);
            goto error;
        }
    }
    if (avr->out_convert_needed) {
        avr->out_buffer = ff_audio_data_alloc(avr->out_channels, 0,
                                              avr->out_sample_fmt, "out_buffer");
        if (!avr->out_buffer) {
            ret = AVERROR(EINVAL);
            goto error;
        }
    }
    avr->out_fifo = av_audio_fifo_alloc(avr->out_sample_fmt, avr->out_channels,
                                        1024);
    if (!avr->out_fifo) {
        ret = AVERROR(ENOMEM);
        goto error;
    }

    /* setup contexts */
    if (avr->in_convert_needed) {
        avr->ac_in = ff_audio_convert_alloc(avr, avr->internal_sample_fmt,
212
                                            avr->in_sample_fmt, avr->in_channels,
213 214
                                            avr->in_sample_rate,
                                            avr->remap_point == REMAP_IN_CONVERT);
Justin Ruggles's avatar
Justin Ruggles committed
215 216 217 218 219 220 221 222 223 224 225 226
        if (!avr->ac_in) {
            ret = AVERROR(ENOMEM);
            goto error;
        }
    }
    if (avr->out_convert_needed) {
        enum AVSampleFormat src_fmt;
        if (avr->in_convert_needed)
            src_fmt = avr->internal_sample_fmt;
        else
            src_fmt = avr->in_sample_fmt;
        avr->ac_out = ff_audio_convert_alloc(avr, avr->out_sample_fmt, src_fmt,
227
                                             avr->out_channels,
228 229
                                             avr->out_sample_rate,
                                             avr->remap_point == REMAP_OUT_CONVERT);
Justin Ruggles's avatar
Justin Ruggles committed
230 231 232 233 234 235 236 237 238 239 240 241 242
        if (!avr->ac_out) {
            ret = AVERROR(ENOMEM);
            goto error;
        }
    }
    if (avr->resample_needed) {
        avr->resample = ff_audio_resample_init(avr);
        if (!avr->resample) {
            ret = AVERROR(ENOMEM);
            goto error;
        }
    }
    if (avr->mixing_needed) {
243 244 245
        avr->am = ff_audio_mix_alloc(avr);
        if (!avr->am) {
            ret = AVERROR(ENOMEM);
Justin Ruggles's avatar
Justin Ruggles committed
246
            goto error;
247
        }
Justin Ruggles's avatar
Justin Ruggles committed
248 249 250 251 252 253 254 255 256
    }

    return 0;

error:
    avresample_close(avr);
    return ret;
}

257 258 259 260 261
int avresample_is_open(AVAudioResampleContext *avr)
{
    return !!avr->out_fifo;
}

Justin Ruggles's avatar
Justin Ruggles committed
262 263 264 265 266 267 268
void avresample_close(AVAudioResampleContext *avr)
{
    ff_audio_data_free(&avr->in_buffer);
    ff_audio_data_free(&avr->resample_out_buffer);
    ff_audio_data_free(&avr->out_buffer);
    av_audio_fifo_free(avr->out_fifo);
    avr->out_fifo = NULL;
269 270
    ff_audio_convert_free(&avr->ac_in);
    ff_audio_convert_free(&avr->ac_out);
Justin Ruggles's avatar
Justin Ruggles committed
271
    ff_audio_resample_free(&avr->resample);
272 273
    ff_audio_mix_free(&avr->am);
    av_freep(&avr->mix_matrix);
274 275

    avr->use_channel_map = 0;
Justin Ruggles's avatar
Justin Ruggles committed
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
}

void avresample_free(AVAudioResampleContext **avr)
{
    if (!*avr)
        return;
    avresample_close(*avr);
    av_opt_free(*avr);
    av_freep(avr);
}

static int handle_buffered_output(AVAudioResampleContext *avr,
                                  AudioData *output, AudioData *converted)
{
    int ret;

    if (!output || av_audio_fifo_size(avr->out_fifo) > 0 ||
        (converted && output->allocated_samples < converted->nb_samples)) {
        if (converted) {
            /* if there are any samples in the output FIFO or if the
               user-supplied output buffer is not large enough for all samples,
               we add to the output FIFO */
298
            av_log(avr, AV_LOG_TRACE, "[FIFO] add %s to out_fifo\n", converted->name);
Justin Ruggles's avatar
Justin Ruggles committed
299 300 301 302 303 304 305 306 307
            ret = ff_audio_data_add_to_fifo(avr->out_fifo, converted, 0,
                                            converted->nb_samples);
            if (ret < 0)
                return ret;
        }

        /* if the user specified an output buffer, read samples from the output
           FIFO to the user output */
        if (output && output->allocated_samples > 0) {
308 309
            av_log(avr, AV_LOG_TRACE, "[FIFO] read from out_fifo to output\n");
            av_log(avr, AV_LOG_TRACE, "[end conversion]\n");
Justin Ruggles's avatar
Justin Ruggles committed
310 311 312 313 314 315
            return ff_audio_data_read_from_fifo(avr->out_fifo, output,
                                                output->allocated_samples);
        }
    } else if (converted) {
        /* copy directly to output if it is large enough or there is not any
           data in the output FIFO */
316
        av_log(avr, AV_LOG_TRACE, "[copy] %s to output\n", converted->name);
Justin Ruggles's avatar
Justin Ruggles committed
317
        output->nb_samples = 0;
318 319 320
        ret = ff_audio_data_copy(output, converted,
                                 avr->remap_point == REMAP_OUT_COPY ?
                                 &avr->ch_map_info : NULL);
Justin Ruggles's avatar
Justin Ruggles committed
321 322
        if (ret < 0)
            return ret;
323
        av_log(avr, AV_LOG_TRACE, "[end conversion]\n");
Justin Ruggles's avatar
Justin Ruggles committed
324 325
        return output->nb_samples;
    }
326
    av_log(avr, AV_LOG_TRACE, "[end conversion]\n");
Justin Ruggles's avatar
Justin Ruggles committed
327 328 329
    return 0;
}

330
int attribute_align_arg avresample_convert(AVAudioResampleContext *avr,
331
                                           uint8_t **output, int out_plane_size,
332 333
                                           int out_samples,
                                           uint8_t * const *input,
334
                                           int in_plane_size, int in_samples)
Justin Ruggles's avatar
Justin Ruggles committed
335 336 337 338
{
    AudioData input_buffer;
    AudioData output_buffer;
    AudioData *current_buffer;
339
    int ret, direct_output;
Justin Ruggles's avatar
Justin Ruggles committed
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357

    /* reset internal buffers */
    if (avr->in_buffer) {
        avr->in_buffer->nb_samples = 0;
        ff_audio_data_set_channels(avr->in_buffer,
                                   avr->in_buffer->allocated_channels);
    }
    if (avr->resample_out_buffer) {
        avr->resample_out_buffer->nb_samples = 0;
        ff_audio_data_set_channels(avr->resample_out_buffer,
                                   avr->resample_out_buffer->allocated_channels);
    }
    if (avr->out_buffer) {
        avr->out_buffer->nb_samples = 0;
        ff_audio_data_set_channels(avr->out_buffer,
                                   avr->out_buffer->allocated_channels);
    }

358
    av_log(avr, AV_LOG_TRACE, "[start conversion]\n");
Justin Ruggles's avatar
Justin Ruggles committed
359 360

    /* initialize output_buffer with output data */
361
    direct_output = output && av_audio_fifo_size(avr->out_fifo) == 0;
Justin Ruggles's avatar
Justin Ruggles committed
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
    if (output) {
        ret = ff_audio_data_init(&output_buffer, output, out_plane_size,
                                 avr->out_channels, out_samples,
                                 avr->out_sample_fmt, 0, "output");
        if (ret < 0)
            return ret;
        output_buffer.nb_samples = 0;
    }

    if (input) {
        /* initialize input_buffer with input data */
        ret = ff_audio_data_init(&input_buffer, input, in_plane_size,
                                 avr->in_channels, in_samples,
                                 avr->in_sample_fmt, 1, "input");
        if (ret < 0)
            return ret;
        current_buffer = &input_buffer;

        if (avr->upmix_needed && !avr->in_convert_needed && !avr->resample_needed &&
381
            !avr->out_convert_needed && direct_output && out_samples >= in_samples) {
Justin Ruggles's avatar
Justin Ruggles committed
382 383
            /* in some rare cases we can copy input to output and upmix
               directly in the output buffer */
384
            av_log(avr, AV_LOG_TRACE, "[copy] %s to output\n", current_buffer->name);
385 386 387
            ret = ff_audio_data_copy(&output_buffer, current_buffer,
                                     avr->remap_point == REMAP_OUT_COPY ?
                                     &avr->ch_map_info : NULL);
Justin Ruggles's avatar
Justin Ruggles committed
388 389 390
            if (ret < 0)
                return ret;
            current_buffer = &output_buffer;
391 392 393 394 395
        } else if (avr->remap_point == REMAP_OUT_COPY &&
                   (!direct_output || out_samples < in_samples)) {
            /* if remapping channels during output copy, we may need to
             * use an intermediate buffer in order to remap before adding
             * samples to the output fifo */
396
            av_log(avr, AV_LOG_TRACE, "[copy] %s to out_buffer\n", current_buffer->name);
397 398 399 400 401 402
            ret = ff_audio_data_copy(avr->out_buffer, current_buffer,
                                     &avr->ch_map_info);
            if (ret < 0)
                return ret;
            current_buffer = avr->out_buffer;
        } else if (avr->in_copy_needed || avr->in_convert_needed) {
Justin Ruggles's avatar
Justin Ruggles committed
403 404 405 406 407 408 409
            /* if needed, copy or convert input to in_buffer, and downmix if
               applicable */
            if (avr->in_convert_needed) {
                ret = ff_audio_data_realloc(avr->in_buffer,
                                            current_buffer->nb_samples);
                if (ret < 0)
                    return ret;
410
                av_log(avr, AV_LOG_TRACE, "[convert] %s to in_buffer\n", current_buffer->name);
411 412
                ret = ff_audio_convert(avr->ac_in, avr->in_buffer,
                                       current_buffer);
Justin Ruggles's avatar
Justin Ruggles committed
413 414 415
                if (ret < 0)
                    return ret;
            } else {
416
                av_log(avr, AV_LOG_TRACE, "[copy] %s to in_buffer\n", current_buffer->name);
417 418 419
                ret = ff_audio_data_copy(avr->in_buffer, current_buffer,
                                         avr->remap_point == REMAP_IN_COPY ?
                                         &avr->ch_map_info : NULL);
Justin Ruggles's avatar
Justin Ruggles committed
420 421 422 423 424
                if (ret < 0)
                    return ret;
            }
            ff_audio_data_set_channels(avr->in_buffer, avr->in_channels);
            if (avr->downmix_needed) {
425
                av_log(avr, AV_LOG_TRACE, "[downmix] in_buffer\n");
Justin Ruggles's avatar
Justin Ruggles committed
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
                ret = ff_audio_mix(avr->am, avr->in_buffer);
                if (ret < 0)
                    return ret;
            }
            current_buffer = avr->in_buffer;
        }
    } else {
        /* flush resampling buffer and/or output FIFO if input is NULL */
        if (!avr->resample_needed)
            return handle_buffered_output(avr, output ? &output_buffer : NULL,
                                          NULL);
        current_buffer = NULL;
    }

    if (avr->resample_needed) {
        AudioData *resample_out;

443
        if (!avr->out_convert_needed && direct_output && out_samples > 0)
Justin Ruggles's avatar
Justin Ruggles committed
444 445 446
            resample_out = &output_buffer;
        else
            resample_out = avr->resample_out_buffer;
447
        av_log(avr, AV_LOG_TRACE, "[resample] %s to %s\n",
448
                current_buffer ? current_buffer->name : "null",
Justin Ruggles's avatar
Justin Ruggles committed
449 450
                resample_out->name);
        ret = ff_audio_resample(avr->resample, resample_out,
451
                                current_buffer);
Justin Ruggles's avatar
Justin Ruggles committed
452 453 454 455 456
        if (ret < 0)
            return ret;

        /* if resampling did not produce any samples, just return 0 */
        if (resample_out->nb_samples == 0) {
457
            av_log(avr, AV_LOG_TRACE, "[end conversion]\n");
Justin Ruggles's avatar
Justin Ruggles committed
458 459 460 461 462 463 464
            return 0;
        }

        current_buffer = resample_out;
    }

    if (avr->upmix_needed) {
465
        av_log(avr, AV_LOG_TRACE, "[upmix] %s\n", current_buffer->name);
Justin Ruggles's avatar
Justin Ruggles committed
466 467 468 469 470 471 472
        ret = ff_audio_mix(avr->am, current_buffer);
        if (ret < 0)
            return ret;
    }

    /* if we resampled or upmixed directly to output, return here */
    if (current_buffer == &output_buffer) {
473
        av_log(avr, AV_LOG_TRACE, "[end conversion]\n");
Justin Ruggles's avatar
Justin Ruggles committed
474 475 476 477
        return current_buffer->nb_samples;
    }

    if (avr->out_convert_needed) {
478
        if (direct_output && out_samples >= current_buffer->nb_samples) {
Justin Ruggles's avatar
Justin Ruggles committed
479
            /* convert directly to output */
480
            av_log(avr, AV_LOG_TRACE, "[convert] %s to output\n", current_buffer->name);
481
            ret = ff_audio_convert(avr->ac_out, &output_buffer, current_buffer);
Justin Ruggles's avatar
Justin Ruggles committed
482 483 484
            if (ret < 0)
                return ret;

485
            av_log(avr, AV_LOG_TRACE, "[end conversion]\n");
Justin Ruggles's avatar
Justin Ruggles committed
486 487 488 489 490 491
            return output_buffer.nb_samples;
        } else {
            ret = ff_audio_data_realloc(avr->out_buffer,
                                        current_buffer->nb_samples);
            if (ret < 0)
                return ret;
492
            av_log(avr, AV_LOG_TRACE, "[convert] %s to out_buffer\n", current_buffer->name);
Justin Ruggles's avatar
Justin Ruggles committed
493
            ret = ff_audio_convert(avr->ac_out, avr->out_buffer,
494
                                   current_buffer);
Justin Ruggles's avatar
Justin Ruggles committed
495 496 497 498 499 500
            if (ret < 0)
                return ret;
            current_buffer = avr->out_buffer;
        }
    }

501 502
    return handle_buffered_output(avr, output ? &output_buffer : NULL,
                                  current_buffer);
Justin Ruggles's avatar
Justin Ruggles committed
503 504
}

505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
int avresample_config(AVAudioResampleContext *avr, AVFrame *out, AVFrame *in)
{
    if (avresample_is_open(avr)) {
        avresample_close(avr);
    }

    if (in) {
        avr->in_channel_layout  = in->channel_layout;
        avr->in_sample_rate     = in->sample_rate;
        avr->in_sample_fmt      = in->format;
    }

    if (out) {
        avr->out_channel_layout = out->channel_layout;
        avr->out_sample_rate    = out->sample_rate;
        avr->out_sample_fmt     = out->format;
    }

    return 0;
}

static int config_changed(AVAudioResampleContext *avr,
                          AVFrame *out, AVFrame *in)
{
    int ret = 0;

    if (in) {
        if (avr->in_channel_layout != in->channel_layout ||
            avr->in_sample_rate    != in->sample_rate ||
            avr->in_sample_fmt     != in->format) {
            ret |= AVERROR_INPUT_CHANGED;
        }
    }

    if (out) {
        if (avr->out_channel_layout != out->channel_layout ||
            avr->out_sample_rate    != out->sample_rate ||
            avr->out_sample_fmt     != out->format) {
            ret |= AVERROR_OUTPUT_CHANGED;
        }
    }

    return ret;
}

static inline int convert_frame(AVAudioResampleContext *avr,
                                AVFrame *out, AVFrame *in)
{
    int ret;
    uint8_t **out_data = NULL, **in_data = NULL;
    int out_linesize = 0, in_linesize = 0;
    int out_nb_samples = 0, in_nb_samples = 0;

    if (out) {
        out_data       = out->extended_data;
        out_linesize   = out->linesize[0];
        out_nb_samples = out->nb_samples;
    }

    if (in) {
        in_data       = in->extended_data;
        in_linesize   = in->linesize[0];
        in_nb_samples = in->nb_samples;
    }

    ret = avresample_convert(avr, out_data, out_linesize,
                             out_nb_samples,
                             in_data, in_linesize,
                             in_nb_samples);

    if (ret < 0) {
        if (out)
            out->nb_samples = 0;
        return ret;
    }

    if (out)
        out->nb_samples = ret;

    return 0;
}

static inline int available_samples(AVFrame *out)
{
589
    int samples;
590
    int bytes_per_sample = av_get_bytes_per_sample(out->format);
591 592
    if (!bytes_per_sample)
        return AVERROR(EINVAL);
593

594
    samples = out->linesize[0] / bytes_per_sample;
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
    if (av_sample_fmt_is_planar(out->format)) {
        return samples;
    } else {
        int channels = av_get_channel_layout_nb_channels(out->channel_layout);
        return samples / channels;
    }
}

int avresample_convert_frame(AVAudioResampleContext *avr,
                             AVFrame *out, AVFrame *in)
{
    int ret, setup = 0;

    if (!avresample_is_open(avr)) {
        if ((ret = avresample_config(avr, out, in)) < 0)
            return ret;
        if ((ret = avresample_open(avr)) < 0)
            return ret;
        setup = 1;
    } else {
        // return as is or reconfigure for input changes?
        if ((ret = config_changed(avr, out, in)))
            return ret;
    }

    if (out) {
        if (!out->linesize[0]) {
            out->nb_samples = avresample_get_out_samples(avr, in->nb_samples);
            if ((ret = av_frame_get_buffer(out, 0)) < 0) {
                if (setup)
                    avresample_close(avr);
                return ret;
            }
        } else {
            if (!out->nb_samples)
                out->nb_samples = available_samples(out);
        }
    }

    return convert_frame(avr, out, in);
}

637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
int avresample_get_matrix(AVAudioResampleContext *avr, double *matrix,
                          int stride)
{
    int in_channels, out_channels, i, o;

    if (avr->am)
        return ff_audio_mix_get_matrix(avr->am, matrix, stride);

    in_channels  = av_get_channel_layout_nb_channels(avr->in_channel_layout);
    out_channels = av_get_channel_layout_nb_channels(avr->out_channel_layout);

    if ( in_channels <= 0 ||  in_channels > AVRESAMPLE_MAX_CHANNELS ||
        out_channels <= 0 || out_channels > AVRESAMPLE_MAX_CHANNELS) {
        av_log(avr, AV_LOG_ERROR, "Invalid channel layouts\n");
        return AVERROR(EINVAL);
    }

    if (!avr->mix_matrix) {
        av_log(avr, AV_LOG_ERROR, "matrix is not set\n");
        return AVERROR(EINVAL);
    }

    for (o = 0; o < out_channels; o++)
        for (i = 0; i < in_channels; i++)
            matrix[o * stride + i] = avr->mix_matrix[o * in_channels + i];

    return 0;
}

int avresample_set_matrix(AVAudioResampleContext *avr, const double *matrix,
                          int stride)
{
    int in_channels, out_channels, i, o;

    if (avr->am)
        return ff_audio_mix_set_matrix(avr->am, matrix, stride);

    in_channels  = av_get_channel_layout_nb_channels(avr->in_channel_layout);
    out_channels = av_get_channel_layout_nb_channels(avr->out_channel_layout);

    if ( in_channels <= 0 ||  in_channels > AVRESAMPLE_MAX_CHANNELS ||
        out_channels <= 0 || out_channels > AVRESAMPLE_MAX_CHANNELS) {
        av_log(avr, AV_LOG_ERROR, "Invalid channel layouts\n");
        return AVERROR(EINVAL);
    }

    if (avr->mix_matrix)
        av_freep(&avr->mix_matrix);
    avr->mix_matrix = av_malloc(in_channels * out_channels *
                                sizeof(*avr->mix_matrix));
    if (!avr->mix_matrix)
        return AVERROR(ENOMEM);

    for (o = 0; o < out_channels; o++)
        for (i = 0; i < in_channels; i++)
            avr->mix_matrix[o * in_channels + i] = matrix[o * stride + i];

    return 0;
}

697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
int avresample_set_channel_mapping(AVAudioResampleContext *avr,
                                   const int *channel_map)
{
    ChannelMapInfo *info = &avr->ch_map_info;
    int in_channels, ch, i;

    in_channels = av_get_channel_layout_nb_channels(avr->in_channel_layout);
    if (in_channels <= 0 ||  in_channels > AVRESAMPLE_MAX_CHANNELS) {
        av_log(avr, AV_LOG_ERROR, "Invalid input channel layout\n");
        return AVERROR(EINVAL);
    }

    memset(info, 0, sizeof(*info));
    memset(info->input_map, -1, sizeof(info->input_map));

    for (ch = 0; ch < in_channels; ch++) {
        if (channel_map[ch] >= in_channels) {
            av_log(avr, AV_LOG_ERROR, "Invalid channel map\n");
            return AVERROR(EINVAL);
        }
        if (channel_map[ch] < 0) {
            info->channel_zero[ch] =  1;
            info->channel_map[ch]  = -1;
            info->do_zero          =  1;
        } else if (info->input_map[channel_map[ch]] >= 0) {
            info->channel_copy[ch] = info->input_map[channel_map[ch]];
            info->channel_map[ch]  = -1;
            info->do_copy          =  1;
        } else {
            info->channel_map[ch]            = channel_map[ch];
            info->input_map[channel_map[ch]] = ch;
            info->do_remap                   =  1;
        }
    }
    /* Fill-in unmapped input channels with unmapped output channels.
       This is used when remapping during conversion from interleaved to
       planar format. */
    for (ch = 0, i = 0; ch < in_channels && i < in_channels; ch++, i++) {
        while (ch < in_channels && info->input_map[ch] >= 0)
            ch++;
        while (i < in_channels && info->channel_map[i] >= 0)
            i++;
        if (ch >= in_channels || i >= in_channels)
            break;
        info->input_map[ch] = i;
    }

    avr->use_channel_map = 1;
    return 0;
}

Justin Ruggles's avatar
Justin Ruggles committed
748 749 750 751 752
int avresample_available(AVAudioResampleContext *avr)
{
    return av_audio_fifo_size(avr->out_fifo);
}

753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
int avresample_get_out_samples(AVAudioResampleContext *avr, int in_nb_samples)
{
    int64_t samples = avresample_get_delay(avr) + (int64_t)in_nb_samples;

    if (avr->resample_needed) {
        samples = av_rescale_rnd(samples,
                                 avr->out_sample_rate,
                                 avr->in_sample_rate,
                                 AV_ROUND_UP);
    }

    samples += avresample_available(avr);

    if (samples > INT_MAX)
        return AVERROR(EINVAL);

    return samples;
}

772
int avresample_read(AVAudioResampleContext *avr, uint8_t **output, int nb_samples)
Justin Ruggles's avatar
Justin Ruggles committed
773
{
774 775
    if (!output)
        return av_audio_fifo_drain(avr->out_fifo, nb_samples);
776
    return av_audio_fifo_read(avr->out_fifo, (void**)output, nb_samples);
Justin Ruggles's avatar
Justin Ruggles committed
777 778 779 780 781 782 783 784 785 786
}

unsigned avresample_version(void)
{
    return LIBAVRESAMPLE_VERSION_INT;
}

const char *avresample_license(void)
{
#define LICENSE_PREFIX "libavresample license: "
787
    return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
Justin Ruggles's avatar
Justin Ruggles committed
788 789 790 791
}

const char *avresample_configuration(void)
{
792
    return FFMPEG_CONFIGURATION;
Justin Ruggles's avatar
Justin Ruggles committed
793
}