Use hb_audio_resample_t from HandBrake to simplify a bit of code.
This commit is contained in:
1 parent
7b1c7d0384
commit
8c1980c81c
4 files changed
+461
-111
No files matched your search
+44
-111
@@ -14,15 +14,15 @@
|
||||
|
||||
#include "FFmpegUtils.h"
|
||||
|
||||
#include "audio_resample.h"
|
||||
|
||||
#include <avcodec.h>
|
||||
#include <libavresample/avresample.h>
|
||||
#include <libavutil/channel_layout.h>
|
||||
#include <libavutil/downmix_info.h>
|
||||
#include <libavutil/opt.h>
|
||||
|
||||
struct MP42DecodeContext {
|
||||
AVCodecContext *avctx;
|
||||
AVAudioResampleContext *resampler;
|
||||
hb_audio_resample_t *resampler;
|
||||
|
||||
AudioChannelLayout **inputLayout;
|
||||
UInt32 *inputLayoutSize;
|
||||
@@ -33,10 +33,9 @@ struct MP42DecodeContext {
|
||||
AudioStreamBasicDescription *outputFormat;
|
||||
|
||||
enum AVMatrixEncoding matrix_encoding;
|
||||
enum AVSampleFormat out_sample_format;
|
||||
int out_layout;
|
||||
|
||||
BOOL reconfigured;
|
||||
BOOL configured;
|
||||
};
|
||||
|
||||
typedef struct MP42DecodeContext MP42DecodeContext;
|
||||
@@ -180,19 +179,15 @@ typedef struct MP42DecodeContext MP42DecodeContext;
|
||||
|
||||
// Context used by the decoder
|
||||
_context = malloc(sizeof(MP42DecodeContext));
|
||||
bzero(_context, (sizeof(MP42DecodeContext)));
|
||||
|
||||
_context->avctx = _avctx;
|
||||
_context->resampler = NULL;
|
||||
|
||||
_context->inputFormat = &_inputFormat;
|
||||
|
||||
_context->outputFormat = &_outputFormat;
|
||||
_context->outputLayout = &_outputLayout;
|
||||
_context->outputLayoutSize = &_outputLayoutSize;
|
||||
|
||||
_context->matrix_encoding = matrix_encoding;
|
||||
_context->out_sample_format = AV_SAMPLE_FMT_FLT;
|
||||
|
||||
_context->reconfigured = NO;
|
||||
_context->configured = NO;
|
||||
|
||||
// Init the FIFOs
|
||||
_inputSamplesBuffer = [[MP42Fifo alloc] initWithCapacity:100];
|
||||
@@ -214,8 +209,7 @@ typedef struct MP42DecodeContext MP42DecodeContext;
|
||||
|
||||
if (_context) {
|
||||
if (_context->resampler) {
|
||||
avresample_close(_context->resampler);
|
||||
avresample_free(&_context->resampler);
|
||||
hb_audio_resample_free(_context->resampler);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -271,11 +265,12 @@ static MP42SampleBuffer * sampleBufferFromFrame(uint8_t *output_data, int output
|
||||
return sample;
|
||||
}
|
||||
|
||||
static void reconfigureDescriptors(MP42DecodeContext *context, AVFrame *frame)
|
||||
static void configureDescriptors(MP42DecodeContext *context, AVFrame *frame)
|
||||
{
|
||||
// Get real channels number and layout
|
||||
int nb_channels = av_get_channel_layout_nb_channels(frame->channel_layout);
|
||||
|
||||
// Reset the channels per frame
|
||||
if (context->inputFormat->mChannelsPerFrame == context->outputFormat->mChannelsPerFrame) {
|
||||
context->outputFormat->mChannelsPerFrame = nb_channels;
|
||||
context->inputFormat->mChannelsPerFrame = nb_channels;
|
||||
@@ -297,6 +292,8 @@ static void reconfigureDescriptors(MP42DecodeContext *context, AVFrame *frame)
|
||||
else {
|
||||
context->out_layout = frame->channel_layout;
|
||||
}
|
||||
|
||||
// Create the AudioChannelLayout from the FFmpeg channel layout
|
||||
UInt32 layout_size = sizeof(AudioChannelLayout) +
|
||||
sizeof(AudioChannelDescription) * context->outputFormat->mChannelsPerFrame;
|
||||
AudioChannelLayout *outputLayout = malloc(layout_size);
|
||||
@@ -305,29 +302,32 @@ static void reconfigureDescriptors(MP42DecodeContext *context, AVFrame *frame)
|
||||
|
||||
*context->outputLayoutSize = layout_size;
|
||||
*context->outputLayout = outputLayout;
|
||||
|
||||
// Tell the audio unit loop we need to reconfigure the chain.
|
||||
context->reconfigured = YES;
|
||||
}
|
||||
|
||||
static int initResampler(MP42DecodeContext *context, AVFrame *frame)
|
||||
static int resample(MP42DecodeContext *context, AVFrame *frame, uint8_t **output_data, int *output_data_size)
|
||||
{
|
||||
int ret = 0;
|
||||
AVAudioResampleContext *resampler;
|
||||
if (!context->configured) {
|
||||
configureDescriptors(context, frame);
|
||||
// We want float interleaved.
|
||||
context->resampler = hb_audio_resample_init(AV_SAMPLE_FMT_FLT,
|
||||
context->out_layout,
|
||||
context->matrix_encoding,
|
||||
0);
|
||||
}
|
||||
|
||||
// We want float interleaved, FFmpeg usually prefers
|
||||
// float planar, so we need to convert it.
|
||||
resampler = avresample_alloc_context();
|
||||
if (!resampler) {
|
||||
if (!context->resampler) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
AVFrameSideData *side_data;
|
||||
if ((side_data = av_frame_get_side_data(frame, AV_FRAME_DATA_DOWNMIX_INFO)) != NULL) {
|
||||
double surround_mix_level, center_mix_level, lfe_mix_level;
|
||||
if ((side_data =
|
||||
av_frame_get_side_data(frame, AV_FRAME_DATA_DOWNMIX_INFO)) != NULL)
|
||||
{
|
||||
double surround_mix_level, center_mix_level;
|
||||
AVDownmixInfo * downmix_info;
|
||||
|
||||
downmix_info = (AVDownmixInfo *)side_data->data;
|
||||
downmix_info = (AVDownmixInfo*)side_data->data;
|
||||
if (context->matrix_encoding == AV_MATRIX_ENCODING_DOLBY ||
|
||||
context->matrix_encoding == AV_MATRIX_ENCODING_DPLII)
|
||||
{
|
||||
@@ -339,91 +339,24 @@ static int initResampler(MP42DecodeContext *context, AVFrame *frame)
|
||||
surround_mix_level = downmix_info->surround_mix_level;
|
||||
center_mix_level = downmix_info->center_mix_level;
|
||||
}
|
||||
lfe_mix_level = downmix_info->lfe_mix_level;
|
||||
|
||||
av_opt_set_double(resampler, "lfe_mix_level", lfe_mix_level, 0);
|
||||
av_opt_set_double(resampler, "center_mix_level", center_mix_level, 0);
|
||||
av_opt_set_double(resampler, "surround_mix_level", surround_mix_level, 0);
|
||||
hb_audio_resample_set_mix_levels(context->resampler,
|
||||
surround_mix_level,
|
||||
center_mix_level,
|
||||
downmix_info->lfe_mix_level);
|
||||
}
|
||||
|
||||
av_opt_set_int(resampler, "in_channel_layout", frame->channel_layout, 0);
|
||||
av_opt_set_int(resampler, "out_channel_layout", context->out_layout, 0);
|
||||
|
||||
av_opt_set_int(resampler, "in_sample_rate", context->inputFormat->mSampleRate, 0);
|
||||
av_opt_set_int(resampler, "out_sample_rate", context->inputFormat->mSampleRate, 0);
|
||||
|
||||
av_opt_set_int(resampler, "in_sample_fmt", context->avctx->sample_fmt, 0);
|
||||
av_opt_set_int(resampler, "out_sample_fmt", context->out_sample_format, 0);
|
||||
|
||||
if (context->matrix_encoding != AV_MATRIX_ENCODING_NONE) {
|
||||
av_opt_set_int(resampler, "matrix_encoding", context->matrix_encoding, 0);
|
||||
hb_audio_resample_set_channel_layout(context->resampler,
|
||||
frame->channel_layout);
|
||||
hb_audio_resample_set_sample_fmt(context->resampler,
|
||||
frame->format);
|
||||
if (hb_audio_resample_update(context->resampler))
|
||||
{
|
||||
NSLog(@"decavcodec: hb_audio_resample_update() failed");
|
||||
return 1;
|
||||
}
|
||||
ret = hb_audio_resample(context->resampler,
|
||||
frame->extended_data, frame->nb_samples,
|
||||
output_data, output_data_size);
|
||||
|
||||
ret = avresample_open(resampler);
|
||||
|
||||
if (ret) {
|
||||
avresample_free(&resampler);
|
||||
}
|
||||
else {
|
||||
context->resampler = resampler;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int updateResampler(MP42DecodeContext *context, AVFrame *frame)
|
||||
{
|
||||
int ret = 0, resample_changed = 0;
|
||||
|
||||
int nb_channels = av_get_channel_layout_nb_channels(frame->channel_layout);
|
||||
if (context->inputFormat->mChannelsPerFrame != nb_channels) {
|
||||
resample_changed = 1;
|
||||
}
|
||||
|
||||
if (resample_changed) {
|
||||
if (context->resampler) {
|
||||
avresample_close(context->resampler);
|
||||
avresample_free(&context->resampler);
|
||||
}
|
||||
ret = initResampler(context, frame);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int resample(MP42DecodeContext *context, AVFrame *frame, uint8_t **output_data, int *output_data_size)
|
||||
{
|
||||
int ret = 0;
|
||||
if (!context->resampler) {
|
||||
reconfigureDescriptors(context, frame);
|
||||
ret = initResampler(context, frame);
|
||||
if (ret) {
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
updateResampler(context, frame);
|
||||
|
||||
int out_samples = avresample_get_out_samples(context->resampler, frame->nb_samples);
|
||||
int out_nb_channels = context->outputFormat->mChannelsPerFrame;
|
||||
|
||||
int in_plane_size, out_plane_size = 0;
|
||||
|
||||
*output_data_size = av_samples_get_buffer_size(&in_plane_size, out_nb_channels,
|
||||
out_samples,
|
||||
context->out_sample_format, 1);
|
||||
|
||||
*output_data = malloc(*output_data_size + AV_INPUT_BUFFER_PADDING_SIZE);
|
||||
|
||||
// If resampling is needed, call avresample_convert
|
||||
out_samples = avresample_convert(context->resampler,
|
||||
output_data, out_plane_size, out_samples,
|
||||
frame->extended_data, in_plane_size, frame->nb_samples);
|
||||
|
||||
// Recompute output_data_size following avresample_convert result (number of samples actually converted)
|
||||
*output_data_size = av_samples_get_buffer_size(&out_plane_size, out_nb_channels,
|
||||
out_samples,
|
||||
context->out_sample_format, 1);
|
||||
|
||||
return ret;
|
||||
}
|
||||
@@ -491,9 +424,9 @@ static inline void enqueue(MP42AudioDecoder *self, MP42SampleBuffer *outSample)
|
||||
}
|
||||
else {
|
||||
decode(_context, sampleBuffer, &outSample);
|
||||
if (_context->reconfigured) {
|
||||
if (_context->configured == NO) {
|
||||
[_outputUnit reconfigure];
|
||||
_context->reconfigured = NO;
|
||||
_context->configured = YES;
|
||||
}
|
||||
enqueue(self, outSample);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,295 @@
|
||||
/* audio_resample.c
|
||||
*
|
||||
* Copyright (c) 2003-2016 HandBrake Team
|
||||
* This file is part of the HandBrake source code
|
||||
* Homepage: <http://handbrake.fr/>
|
||||
* It may be used under the terms of the GNU General Public License v2.
|
||||
* For full terms see the file COPYING file or visit http://www.gnu.org/licenses/gpl-2.0.html
|
||||
*/
|
||||
|
||||
#include <avcodec.h>
|
||||
#include "audio_resample.h"
|
||||
#include <libavutil/opt.h>
|
||||
|
||||
/* Default mix level for center and surround channels */
|
||||
#define HB_MIXLEV_DEFAULT ((double)M_SQRT1_2)
|
||||
/* Default mix level for LFE channel */
|
||||
#define HB_MIXLEV_ZERO ((double)0.0)
|
||||
|
||||
hb_audio_resample_t* hb_audio_resample_init(enum AVSampleFormat sample_fmt,
|
||||
uint64_t channel_layout, int matrix_encoding,
|
||||
int normalize_mix)
|
||||
{
|
||||
hb_audio_resample_t *resample = calloc(1, sizeof(hb_audio_resample_t));
|
||||
if (resample == NULL)
|
||||
{
|
||||
//hb_error("hb_audio_resample_init: failed to allocate resample");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
// avresample context, initialized in hb_audio_resample_update()
|
||||
resample->avresample = NULL;
|
||||
|
||||
// we don't support planar output yet
|
||||
if (av_sample_fmt_is_planar(sample_fmt))
|
||||
{
|
||||
//hb_error("hb_audio_resample_init: planar output not supported ('%s')",
|
||||
// av_get_sample_fmt_name(sample_fmt));
|
||||
goto fail;
|
||||
}
|
||||
|
||||
// convert mixdown to channel_layout/matrix_encoding combo
|
||||
//int matrix_encoding;
|
||||
//uint64_t channel_layout = hb_ff_mixdown_xlat(hb_amixdown, &matrix_encoding);
|
||||
|
||||
/*
|
||||
* When downmixing, Dual Mono to Mono is a special case:
|
||||
* the audio must remain 2-channel until all conversions are done.
|
||||
*/
|
||||
/*if (hb_amixdown == HB_AMIXDOWN_LEFT || hb_amixdown == HB_AMIXDOWN_RIGHT)
|
||||
{
|
||||
channel_layout = AV_CH_LAYOUT_STEREO;
|
||||
resample->dual_mono_downmix = 1;
|
||||
resample->dual_mono_right_only = (hb_amixdown == HB_AMIXDOWN_RIGHT);
|
||||
}
|
||||
else
|
||||
{*/
|
||||
resample->dual_mono_downmix = 0;
|
||||
//}
|
||||
|
||||
// requested output channel_layout, sample_fmt
|
||||
resample->out.channels = av_get_channel_layout_nb_channels(channel_layout);
|
||||
resample->out.channel_layout = channel_layout;
|
||||
resample->out.matrix_encoding = matrix_encoding;
|
||||
resample->out.normalize_mix_level = normalize_mix;
|
||||
resample->out.sample_fmt = sample_fmt;
|
||||
resample->out.sample_size = av_get_bytes_per_sample(sample_fmt);
|
||||
|
||||
// set default input characteristics
|
||||
resample->in.sample_fmt = resample->out.sample_fmt;
|
||||
resample->in.channel_layout = resample->out.channel_layout;
|
||||
resample->in.lfe_mix_level = HB_MIXLEV_ZERO;
|
||||
resample->in.center_mix_level = HB_MIXLEV_DEFAULT;
|
||||
resample->in.surround_mix_level = HB_MIXLEV_DEFAULT;
|
||||
|
||||
// by default, no conversion needed
|
||||
resample->resample_needed = 0;
|
||||
return resample;
|
||||
|
||||
fail:
|
||||
hb_audio_resample_free(resample);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void hb_audio_resample_set_channel_layout(hb_audio_resample_t *resample,
|
||||
uint64_t channel_layout)
|
||||
{
|
||||
if (resample != NULL)
|
||||
{
|
||||
if (channel_layout == AV_CH_LAYOUT_STEREO_DOWNMIX)
|
||||
{
|
||||
// Dolby Surround is Stereo when it comes to remixing
|
||||
channel_layout = AV_CH_LAYOUT_STEREO;
|
||||
}
|
||||
resample->in.channel_layout = channel_layout;
|
||||
}
|
||||
}
|
||||
|
||||
void hb_audio_resample_set_mix_levels(hb_audio_resample_t *resample,
|
||||
double surround_mix_level,
|
||||
double center_mix_level,
|
||||
double lfe_mix_level)
|
||||
{
|
||||
if (resample != NULL)
|
||||
{
|
||||
resample->in.lfe_mix_level = lfe_mix_level;
|
||||
resample->in.center_mix_level = center_mix_level;
|
||||
resample->in.surround_mix_level = surround_mix_level;
|
||||
}
|
||||
}
|
||||
|
||||
void hb_audio_resample_set_sample_fmt(hb_audio_resample_t *resample,
|
||||
enum AVSampleFormat sample_fmt)
|
||||
{
|
||||
if (resample != NULL)
|
||||
{
|
||||
resample->in.sample_fmt = sample_fmt;
|
||||
}
|
||||
}
|
||||
|
||||
int hb_audio_resample_update(hb_audio_resample_t *resample)
|
||||
{
|
||||
if (resample == NULL)
|
||||
{
|
||||
//hb_error("hb_audio_resample_update: resample is NULL");
|
||||
return 1;
|
||||
}
|
||||
|
||||
int ret, resample_changed;
|
||||
|
||||
resample->resample_needed =
|
||||
(resample->out.sample_fmt != resample->in.sample_fmt ||
|
||||
resample->out.channel_layout != resample->in.channel_layout);
|
||||
|
||||
resample_changed =
|
||||
(resample->resample_needed &&
|
||||
(resample->resample.sample_fmt != resample->in.sample_fmt ||
|
||||
resample->resample.channel_layout != resample->in.channel_layout ||
|
||||
resample->resample.lfe_mix_level != resample->in.lfe_mix_level ||
|
||||
resample->resample.center_mix_level != resample->in.center_mix_level ||
|
||||
resample->resample.surround_mix_level != resample->in.surround_mix_level));
|
||||
|
||||
if (resample_changed || (resample->resample_needed &&
|
||||
resample->avresample == NULL))
|
||||
{
|
||||
if (resample->avresample == NULL)
|
||||
{
|
||||
resample->avresample = avresample_alloc_context();
|
||||
if (resample->avresample == NULL)
|
||||
{
|
||||
//hb_error("hb_audio_resample_update: avresample_alloc_context() failed");
|
||||
return 1;
|
||||
}
|
||||
|
||||
av_opt_set_int(resample->avresample, "out_sample_fmt",
|
||||
resample->out.sample_fmt, 0);
|
||||
av_opt_set_int(resample->avresample, "out_channel_layout",
|
||||
resample->out.channel_layout, 0);
|
||||
av_opt_set_int(resample->avresample, "matrix_encoding",
|
||||
resample->out.matrix_encoding, 0);
|
||||
av_opt_set_int(resample->avresample, "normalize_mix_level",
|
||||
resample->out.normalize_mix_level, 0);
|
||||
}
|
||||
else if (resample_changed)
|
||||
{
|
||||
avresample_close(resample->avresample);
|
||||
}
|
||||
|
||||
av_opt_set_int(resample->avresample, "in_sample_fmt",
|
||||
resample->in.sample_fmt, 0);
|
||||
av_opt_set_int(resample->avresample, "in_channel_layout",
|
||||
resample->in.channel_layout, 0);
|
||||
av_opt_set_double(resample->avresample, "lfe_mix_level",
|
||||
resample->in.lfe_mix_level, 0);
|
||||
av_opt_set_double(resample->avresample, "center_mix_level",
|
||||
resample->in.center_mix_level, 0);
|
||||
av_opt_set_double(resample->avresample, "surround_mix_level",
|
||||
resample->in.surround_mix_level, 0);
|
||||
|
||||
if ((ret = avresample_open(resample->avresample)))
|
||||
{
|
||||
char err_desc[64];
|
||||
av_strerror(ret, err_desc, 63);
|
||||
//hb_error("hb_audio_resample_update: avresample_open() failed (%s)",
|
||||
// err_desc);
|
||||
// avresample won't open, start over
|
||||
avresample_free(&resample->avresample);
|
||||
return ret;
|
||||
}
|
||||
|
||||
resample->resample.sample_fmt = resample->in.sample_fmt;
|
||||
resample->resample.channel_layout = resample->in.channel_layout;
|
||||
resample->resample.channels =
|
||||
av_get_channel_layout_nb_channels(resample->in.channel_layout);
|
||||
resample->resample.lfe_mix_level = resample->in.lfe_mix_level;
|
||||
resample->resample.center_mix_level = resample->in.center_mix_level;
|
||||
resample->resample.surround_mix_level = resample->in.surround_mix_level;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void hb_audio_resample_free(hb_audio_resample_t *resample)
|
||||
{
|
||||
if (resample != NULL)
|
||||
{
|
||||
if (resample->avresample != NULL)
|
||||
{
|
||||
avresample_free(&resample->avresample);
|
||||
}
|
||||
free(resample);
|
||||
}
|
||||
}
|
||||
|
||||
int hb_audio_resample(hb_audio_resample_t *resample,
|
||||
uint8_t **samples, int nsamples,
|
||||
uint8_t **out_data, int *out_size_external)
|
||||
{
|
||||
if (resample == NULL)
|
||||
{
|
||||
//hb_error("hb_audio_resample: resample is NULL");
|
||||
return 1;
|
||||
}
|
||||
if (resample->resample_needed && resample->avresample == NULL)
|
||||
{
|
||||
//hb_error("hb_audio_resample: resample needed but libavresample context "
|
||||
// "is NULL");
|
||||
return 1;
|
||||
}
|
||||
|
||||
uint8_t *out;
|
||||
int out_size, out_samples;
|
||||
|
||||
if (resample->resample_needed)
|
||||
{
|
||||
int in_linesize, out_linesize;
|
||||
// set in/out linesize and out_size
|
||||
av_samples_get_buffer_size(&in_linesize,
|
||||
resample->resample.channels, nsamples,
|
||||
resample->resample.sample_fmt, 0);
|
||||
out_size = av_samples_get_buffer_size(&out_linesize,
|
||||
resample->out.channels, nsamples,
|
||||
resample->out.sample_fmt, 0);
|
||||
out = malloc(out_size + AV_INPUT_BUFFER_PADDING_SIZE);
|
||||
|
||||
out_samples = avresample_convert(resample->avresample,
|
||||
&out, out_linesize, nsamples,
|
||||
samples, in_linesize, nsamples);
|
||||
|
||||
if (out_samples <= 0)
|
||||
{
|
||||
if (out_samples < 0) {
|
||||
//hb_log("hb_audio_resample: avresample_convert() failed");
|
||||
}
|
||||
// don't send empty buffers downstream (EOF)
|
||||
free(out);
|
||||
return 1;
|
||||
}
|
||||
*out_data = out;
|
||||
*out_size_external = (out_samples *
|
||||
resample->out.sample_size * resample->out.channels);
|
||||
}
|
||||
else
|
||||
{
|
||||
out_samples = nsamples;
|
||||
out_size = (out_samples *
|
||||
resample->out.sample_size * resample->out.channels);
|
||||
out = malloc(out_size + AV_INPUT_BUFFER_PADDING_SIZE);
|
||||
memcpy(out, samples[0], out_size);
|
||||
|
||||
*out_data = out;
|
||||
*out_size_external = out_size;
|
||||
}
|
||||
|
||||
/*
|
||||
* Dual Mono to Mono.
|
||||
*
|
||||
* Copy all left or right samples to the first half of the buffer and halve
|
||||
* the buffer size.
|
||||
*/
|
||||
if (resample->dual_mono_downmix)
|
||||
{
|
||||
int ii, jj = !!resample->dual_mono_right_only;
|
||||
int sample_size = resample->out.sample_size;
|
||||
uint8_t *audio_samples = out;
|
||||
for (ii = 0; ii < out_samples; ii++)
|
||||
{
|
||||
memcpy(audio_samples + (ii * sample_size),
|
||||
audio_samples + (jj * sample_size), sample_size);
|
||||
jj += 2;
|
||||
}
|
||||
*out_size_external = out_samples * sample_size;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
/* audio_resample.h
|
||||
*
|
||||
* Copyright (c) 2003-2016 HandBrake Team
|
||||
* This file is part of the HandBrake source code
|
||||
* Homepage: <http://handbrake.fr/>
|
||||
* It may be used under the terms of the GNU General Public License v2.
|
||||
* For full terms see the file COPYING file or visit http://www.gnu.org/licenses/gpl-2.0.html
|
||||
*/
|
||||
|
||||
/* Implements a libavresample wrapper for convenience.
|
||||
*
|
||||
* Supports sample_fmt and channel_layout conversion.
|
||||
*
|
||||
* sample_rate conversion will come later (libavresample doesn't support
|
||||
* sample_rate conversion with float samples yet). */
|
||||
|
||||
#ifndef AUDIO_RESAMPLE_H
|
||||
#define AUDIO_RESAMPLE_H
|
||||
|
||||
#include <math.h>
|
||||
#include <stdint.h>
|
||||
#include <libavutil/channel_layout.h>
|
||||
#include <libavresample/avresample.h>
|
||||
|
||||
typedef struct
|
||||
{
|
||||
int dual_mono_downmix;
|
||||
int dual_mono_right_only;
|
||||
|
||||
int resample_needed;
|
||||
AVAudioResampleContext *avresample;
|
||||
|
||||
struct
|
||||
{
|
||||
uint64_t channel_layout;
|
||||
double lfe_mix_level;
|
||||
double center_mix_level;
|
||||
double surround_mix_level;
|
||||
enum AVSampleFormat sample_fmt;
|
||||
} in;
|
||||
|
||||
struct
|
||||
{
|
||||
int channels;
|
||||
uint64_t channel_layout;
|
||||
double lfe_mix_level;
|
||||
double center_mix_level;
|
||||
double surround_mix_level;
|
||||
enum AVSampleFormat sample_fmt;
|
||||
} resample;
|
||||
|
||||
struct
|
||||
{
|
||||
int channels;
|
||||
int sample_size;
|
||||
int normalize_mix_level;
|
||||
uint64_t channel_layout;
|
||||
enum AVSampleFormat sample_fmt;
|
||||
enum AVMatrixEncoding matrix_encoding;
|
||||
} out;
|
||||
} hb_audio_resample_t;
|
||||
|
||||
/* Initialize an hb_audio_resample_t for converting audio to the requested
|
||||
* sample_fmt and mixdown.
|
||||
*
|
||||
* Also sets the default audio input characteristics, so that they are the same
|
||||
* as the output characteristics (no conversion needed).
|
||||
*/
|
||||
hb_audio_resample_t* hb_audio_resample_init(enum AVSampleFormat sample_fmt,
|
||||
uint64_t channel_layout, int matrix_encoding,
|
||||
int normalize_mix);
|
||||
|
||||
/* The following functions set the audio input characteristics.
|
||||
*
|
||||
* They should be called whenever the relevant characteristic(s) differ from the
|
||||
* requested output characteristics, or if they may have changed in the source.
|
||||
*/
|
||||
|
||||
void hb_audio_resample_set_channel_layout(hb_audio_resample_t *resample,
|
||||
uint64_t channel_layout);
|
||||
|
||||
void hb_audio_resample_set_mix_levels(hb_audio_resample_t *resample,
|
||||
double surround_mix_level,
|
||||
double center_mix_level,
|
||||
double lfe_mix_level);
|
||||
|
||||
void hb_audio_resample_set_sample_fmt(hb_audio_resample_t *resample,
|
||||
enum AVSampleFormat sample_fmt);
|
||||
|
||||
/* Update an hb_audio_resample_t.
|
||||
*
|
||||
* Must be called after using any of the above functions.
|
||||
*/
|
||||
int hb_audio_resample_update(hb_audio_resample_t *resample);
|
||||
|
||||
/* Free an hb_audio_remsample_t. */
|
||||
void hb_audio_resample_free(hb_audio_resample_t *resample);
|
||||
|
||||
/* Convert input samples to the requested output characteristics
|
||||
* (sample_fmt and channel_layout + matrix_encoding).
|
||||
*
|
||||
* Returns an hb_buffer_t with the converted output.
|
||||
*
|
||||
* resampling is only done when necessary.
|
||||
*/
|
||||
int hb_audio_resample(hb_audio_resample_t *resample,
|
||||
uint8_t **samples, int nsamples,
|
||||
uint8_t **out_date, int *out_size);
|
||||
|
||||
#endif /* AUDIO_RESAMPLE_H */
|
||||
@@ -64,6 +64,10 @@
|
||||
A925AAB018379AF800BE84D4 /* MP42Utilities.m in Sources */ = {isa = PBXBuildFile; fileRef = A925AAAE18379AF800BE84D4 /* MP42Utilities.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
|
||||
A93EE6E91D489FFC003E3B56 /* libavresample.a in Frameworks */ = {isa = PBXBuildFile; fileRef = A99DB6AD1D4648D100C6E56C /* libavresample.a */; };
|
||||
A93EE6EA1D48A01A003E3B56 /* libswresample.a in Frameworks */ = {isa = PBXBuildFile; fileRef = A9D599BA1D4885E0001C22BD /* libswresample.a */; };
|
||||
A9422FC51D4917AF000DB435 /* audio_resample.c in Sources */ = {isa = PBXBuildFile; fileRef = A9422FC31D4917AF000DB435 /* audio_resample.c */; };
|
||||
A9422FC61D4917AF000DB435 /* audio_resample.c in Sources */ = {isa = PBXBuildFile; fileRef = A9422FC31D4917AF000DB435 /* audio_resample.c */; };
|
||||
A9422FC71D4917AF000DB435 /* audio_resample.h in Headers */ = {isa = PBXBuildFile; fileRef = A9422FC41D4917AF000DB435 /* audio_resample.h */; };
|
||||
A9422FC81D4917AF000DB435 /* audio_resample.h in Headers */ = {isa = PBXBuildFile; fileRef = A9422FC41D4917AF000DB435 /* audio_resample.h */; };
|
||||
A947BA9B1824F0B3004C734B /* Ratings.json in Resources */ = {isa = PBXBuildFile; fileRef = A947BA9A1824F0B3004C734B /* Ratings.json */; };
|
||||
A96523451BAC315900E994E0 /* NSString+MP42Additions.h in Headers */ = {isa = PBXBuildFile; fileRef = A96523431BAC315900E994E0 /* NSString+MP42Additions.h */; };
|
||||
A96523461BAC315900E994E0 /* NSString+MP42Additions.h in Headers */ = {isa = PBXBuildFile; fileRef = A96523431BAC315900E994E0 /* NSString+MP42Additions.h */; };
|
||||
@@ -267,6 +271,8 @@
|
||||
A91C3ACD1BF20CF1008BCF87 /* MP42FormatUtilites.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MP42FormatUtilites.h; sourceTree = "<group>"; };
|
||||
A925AAAD18379AF800BE84D4 /* MP42Utilities.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MP42Utilities.h; sourceTree = "<group>"; };
|
||||
A925AAAE18379AF800BE84D4 /* MP42Utilities.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MP42Utilities.m; sourceTree = "<group>"; };
|
||||
A9422FC31D4917AF000DB435 /* audio_resample.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = audio_resample.c; sourceTree = "<group>"; };
|
||||
A9422FC41D4917AF000DB435 /* audio_resample.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = audio_resample.h; sourceTree = "<group>"; };
|
||||
A947BA9A1824F0B3004C734B /* Ratings.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = Ratings.json; sourceTree = "<group>"; };
|
||||
A96523431BAC315900E994E0 /* NSString+MP42Additions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSString+MP42Additions.h"; sourceTree = "<group>"; };
|
||||
A96523441BAC315900E994E0 /* NSString+MP42Additions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSString+MP42Additions.m"; sourceTree = "<group>"; };
|
||||
@@ -568,6 +574,8 @@
|
||||
A9B9C25A1823923200416A4E /* others */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
A9422FC41D4917AF000DB435 /* audio_resample.h */,
|
||||
A9422FC31D4917AF000DB435 /* audio_resample.c */,
|
||||
A9B9C25D1823923200416A4E /* MatroskaFile.c */,
|
||||
A9B9C25E1823923200416A4E /* MatroskaFile.h */,
|
||||
A9B9C25F1823923200416A4E /* MatroskaParser.c */,
|
||||
@@ -703,6 +711,7 @@
|
||||
files = (
|
||||
A91C3AD11BF20CF1008BCF87 /* MP42FormatUtilites.h in Headers */,
|
||||
A96D28941BAD390400403327 /* MP42Metadata+Private.h in Headers */,
|
||||
A9422FC81D4917AF000DB435 /* audio_resample.h in Headers */,
|
||||
A99DB6A11D46480500C6E56C /* MP42AudioUnit.h in Headers */,
|
||||
A917286719FD2BCE00348DF8 /* MP42Logging.h in Headers */,
|
||||
A99DB6991D46480500C6E56C /* MP42AudioDecoder.h in Headers */,
|
||||
@@ -767,6 +776,7 @@
|
||||
A9B9C26A1823923200416A4E /* MP42AudioConverter.h in Headers */,
|
||||
A9B9C2A11823923200416A4E /* MP42VobSubImporter.h in Headers */,
|
||||
A91C3AD01BF20CF1008BCF87 /* MP42FormatUtilites.h in Headers */,
|
||||
A9422FC71D4917AF000DB435 /* audio_resample.h in Headers */,
|
||||
A9B9C2AC1823923200416A4E /* mbs.h in Headers */,
|
||||
A9B9C2891823923200416A4E /* MP42MkvImporter.h in Headers */,
|
||||
A96D28931BAD390400403327 /* MP42Metadata+Private.h in Headers */,
|
||||
@@ -965,6 +975,7 @@
|
||||
A99DB69B1D46480500C6E56C /* MP42AudioDecoder.m in Sources */,
|
||||
A910B61318394EB20064028F /* MP42AACImporter.mm in Sources */,
|
||||
A910B61518394EB20064028F /* MP42AC3Importer.m in Sources */,
|
||||
A9422FC61D4917AF000DB435 /* audio_resample.c in Sources */,
|
||||
A910B61818394EB20064028F /* MatroskaFile.c in Sources */,
|
||||
A910B61A18394EB20064028F /* MatroskaParser.c in Sources */,
|
||||
A910B61C18394EB20064028F /* mbs.cpp in Sources */,
|
||||
@@ -1019,6 +1030,7 @@
|
||||
A925AAB018379AF800BE84D4 /* MP42Utilities.m in Sources */,
|
||||
A9B9C2731823923200416A4E /* MP42CCImporter.m in Sources */,
|
||||
A9B9C29A1823923200416A4E /* MP42SubUtilities.m in Sources */,
|
||||
A9422FC51D4917AF000DB435 /* audio_resample.c in Sources */,
|
||||
A9B9C2981823923200416A4E /* MP42SubtitleTrack.m in Sources */,
|
||||
A9B9C29C1823923200416A4E /* MP42Track.m in Sources */,
|
||||
A9B9C2711823923200416A4E /* MP42BitmapSubConverter.m in Sources */,
|
||||
|
||||
Reference in new issue
Block a user