Commit 074a00d1 authored by Justin Ruggles's avatar Justin Ruggles

lavr: add a public function for setting a custom channel map

This allows reordering, duplication, and silencing of input channels.
parent 4d68269d
......@@ -13,6 +13,10 @@ libavutil: 2012-10-22
API changes, most recent first:
2013-xx-xx - xxxxxxx - lavr 1.1.0
Add avresample_set_channel_mapping() for input channel reordering,
duplication, and silencing.
2012-xx-xx - xxxxxxx - lavu 52.2.1 - avstring.h
Add av_basename() and av_dirname().
......
......@@ -50,6 +50,7 @@ struct AudioConvert {
DitherContext *dc;
enum AVSampleFormat in_fmt;
enum AVSampleFormat out_fmt;
int apply_map;
int channels;
int planes;
int ptr_align;
......@@ -259,7 +260,8 @@ void ff_audio_convert_free(AudioConvert **ac)
AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr,
enum AVSampleFormat out_fmt,
enum AVSampleFormat in_fmt,
int channels, int sample_rate)
int channels, int sample_rate,
int apply_map)
{
AudioConvert *ac;
int in_planar, out_planar;
......@@ -272,11 +274,13 @@ AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr,
ac->out_fmt = out_fmt;
ac->in_fmt = in_fmt;
ac->channels = channels;
ac->apply_map = apply_map;
if (avr->dither_method != AV_RESAMPLE_DITHER_NONE &&
av_get_packed_sample_fmt(out_fmt) == AV_SAMPLE_FMT_S16 &&
av_get_bytes_per_sample(in_fmt) > 2) {
ac->dc = ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate);
ac->dc = ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate,
apply_map);
if (!ac->dc) {
av_free(ac);
return NULL;
......@@ -309,6 +313,7 @@ int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in)
{
int use_generic = 1;
int len = in->nb_samples;
int p;
if (ac->dc) {
/* dithered conversion */
......@@ -335,9 +340,46 @@ int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in)
av_get_sample_fmt_name(ac->out_fmt),
use_generic ? ac->func_descr_generic : ac->func_descr);
if (ac->apply_map) {
ChannelMapInfo *map = &ac->avr->ch_map_info;
if (!av_sample_fmt_is_planar(ac->out_fmt)) {
av_log(ac->avr, AV_LOG_ERROR, "cannot remap packed format during conversion\n");
return AVERROR(EINVAL);
}
if (map->do_remap) {
if (av_sample_fmt_is_planar(ac->in_fmt)) {
conv_func_flat *convert = use_generic ? ac->conv_flat_generic :
ac->conv_flat;
for (p = 0; p < ac->planes; p++)
if (map->channel_map[p] >= 0)
convert(out->data[p], in->data[map->channel_map[p]], len);
} else {
uint8_t *data[AVRESAMPLE_MAX_CHANNELS];
conv_func_deinterleave *convert = use_generic ?
ac->conv_deinterleave_generic :
ac->conv_deinterleave;
for (p = 0; p < ac->channels; p++)
data[map->input_map[p]] = out->data[p];
convert(data, in->data[0], len, ac->channels);
}
}
if (map->do_copy || map->do_zero) {
for (p = 0; p < ac->planes; p++) {
if (map->channel_copy[p])
memcpy(out->data[p], out->data[map->channel_copy[p]],
len * out->stride);
else if (map->channel_zero[p])
av_samples_set_silence(&out->data[p], 0, len, 1, ac->out_fmt);
}
}
} else {
switch (ac->func_type) {
case CONV_FUNC_TYPE_FLAT: {
int p;
if (!in->is_planar)
len *= in->channels;
if (use_generic) {
......@@ -362,6 +404,7 @@ int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in)
ac->conv_deinterleave(out->data, in->data[0], len, ac->channels);
break;
}
}
out->nb_samples = in->nb_samples;
return 0;
......
......@@ -58,12 +58,14 @@ void ff_audio_convert_set_func(AudioConvert *ac, enum AVSampleFormat out_fmt,
* @param in_fmt input sample format
* @param channels number of channels
* @param sample_rate sample rate (used for dithering)
* @param apply_map apply channel map during conversion
* @return newly-allocated AudioConvert context
*/
AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr,
enum AVSampleFormat out_fmt,
enum AVSampleFormat in_fmt,
int channels, int sample_rate);
int channels, int sample_rate,
int apply_map);
/**
* Free AudioConvert.
......
......@@ -213,7 +213,7 @@ void ff_audio_data_free(AudioData **a)
av_freep(a);
}
int ff_audio_data_copy(AudioData *dst, AudioData *src)
int ff_audio_data_copy(AudioData *dst, AudioData *src, ChannelMapInfo *map)
{
int ret, p;
......@@ -221,6 +221,11 @@ int ff_audio_data_copy(AudioData *dst, AudioData *src)
if (dst->sample_fmt != src->sample_fmt || dst->channels < src->channels)
return AVERROR(EINVAL);
if (map && !src->is_planar) {
av_log(src, AV_LOG_ERROR, "cannot remap packed format during copy\n");
return AVERROR(EINVAL);
}
/* if the input is empty, just empty the output */
if (!src->nb_samples) {
dst->nb_samples = 0;
......@@ -233,8 +238,29 @@ int ff_audio_data_copy(AudioData *dst, AudioData *src)
return ret;
/* copy data */
for (p = 0; p < src->planes; p++)
memcpy(dst->data[p], src->data[p], src->nb_samples * src->stride);
if (map) {
if (map->do_remap) {
for (p = 0; p < src->planes; p++) {
if (map->channel_map[p] >= 0)
memcpy(dst->data[p], src->data[map->channel_map[p]],
src->nb_samples * src->stride);
}
}
if (map->do_copy || map->do_zero) {
for (p = 0; p < src->planes; p++) {
if (map->channel_copy[p])
memcpy(dst->data[p], dst->data[map->channel_copy[p]],
src->nb_samples * src->stride);
else if (map->channel_zero[p])
av_samples_set_silence(&dst->data[p], 0, src->nb_samples,
1, dst->sample_fmt);
}
}
} else {
for (p = 0; p < src->planes; p++)
memcpy(dst->data[p], src->data[p], src->nb_samples * src->stride);
}
dst->nb_samples = src->nb_samples;
return 0;
......
......@@ -118,9 +118,10 @@ void ff_audio_data_free(AudioData **a);
*
* @param out output AudioData
* @param in input AudioData
* @param map channel map, NULL if not remapping
* @return 0 on success, negative AVERROR value on error
*/
int ff_audio_data_copy(AudioData *out, AudioData *in);
int ff_audio_data_copy(AudioData *out, AudioData *in, ChannelMapInfo *map);
/**
* Append data from one AudioData to the end of another.
......
......@@ -258,6 +258,36 @@ int avresample_get_matrix(AVAudioResampleContext *avr, double *matrix,
int avresample_set_matrix(AVAudioResampleContext *avr, const double *matrix,
int stride);
/**
* Set a customized input channel mapping.
*
* This function can only be called when the allocated context is not open.
* Also, the input channel layout must have already been set.
*
* Calling avresample_close() on the context will clear the channel mapping.
*
* The map for each input channel specifies the channel index in the source to
* use for that particular channel, or -1 to mute the channel. Source channels
* can be duplicated by using the same index for multiple input channels.
*
* Examples:
*
* Reordering 5.1 AAC order (C,L,R,Ls,Rs,LFE) to Libav order (L,R,C,LFE,Ls,Rs):
* { 1, 2, 0, 5, 3, 4 }
*
* Muting the 3rd channel in 4-channel input:
* { 0, 1, -1, 3 }
*
* Duplicating the left channel of stereo input:
* { 0, 0 }
*
* @param avr audio resample context
* @param channel_map customized input channel mapping
* @return 0 on success, negative AVERROR code on failure
*/
int avresample_set_channel_mapping(AVAudioResampleContext *avr,
const int *channel_map);
/**
* Set compensation for resampling.
*
......
......@@ -53,6 +53,8 @@ typedef struct DitherState {
struct DitherContext {
DitherDSPContext ddsp;
enum AVResampleDitherMethod method;
int apply_map;
ChannelMapInfo *ch_map_info;
int mute_dither_threshold; // threshold for disabling dither
int mute_reset_threshold; // threshold for resetting noise shaping
......@@ -251,17 +253,23 @@ int ff_convert_dither(DitherContext *c, AudioData *dst, AudioData *src)
return ret;
}
if (src->sample_fmt != AV_SAMPLE_FMT_FLTP) {
if (src->sample_fmt != AV_SAMPLE_FMT_FLTP || c->apply_map) {
/* make sure flt_data is large enough for the input */
ret = ff_audio_data_realloc(c->flt_data, src->nb_samples);
if (ret < 0)
return ret;
flt_data = c->flt_data;
}
if (src->sample_fmt != AV_SAMPLE_FMT_FLTP) {
/* convert input samples to fltp and scale to s16 range */
ret = ff_audio_convert(c->ac_in, flt_data, src);
if (ret < 0)
return ret;
} else if (c->apply_map) {
ret = ff_audio_data_copy(flt_data, src, c->ch_map_info);
if (ret < 0)
return ret;
} else {
flt_data = src;
}
......@@ -333,7 +341,7 @@ static void dither_init(DitherDSPContext *ddsp,
DitherContext *ff_dither_alloc(AVAudioResampleContext *avr,
enum AVSampleFormat out_fmt,
enum AVSampleFormat in_fmt,
int channels, int sample_rate)
int channels, int sample_rate, int apply_map)
{
AVLFG seed_gen;
DitherContext *c;
......@@ -350,6 +358,10 @@ DitherContext *ff_dither_alloc(AVAudioResampleContext *avr,
if (!c)
return NULL;
c->apply_map = apply_map;
if (apply_map)
c->ch_map_info = &avr->ch_map_info;
if (avr->dither_method == AV_RESAMPLE_DITHER_TRIANGULAR_NS &&
sample_rate != 48000 && sample_rate != 44100) {
av_log(avr, AV_LOG_WARNING, "sample rate must be 48000 or 44100 Hz "
......@@ -379,19 +391,20 @@ DitherContext *ff_dither_alloc(AVAudioResampleContext *avr,
goto fail;
c->ac_out = ff_audio_convert_alloc(avr, out_fmt, AV_SAMPLE_FMT_S16P,
channels, sample_rate);
channels, sample_rate, 0);
if (!c->ac_out)
goto fail;
}
if (in_fmt != AV_SAMPLE_FMT_FLTP) {
if (in_fmt != AV_SAMPLE_FMT_FLTP || c->apply_map) {
c->flt_data = ff_audio_data_alloc(channels, 1024, AV_SAMPLE_FMT_FLTP,
"dither flt buffer");
if (!c->flt_data)
goto fail;
}
if (in_fmt != AV_SAMPLE_FMT_FLTP) {
c->ac_in = ff_audio_convert_alloc(avr, AV_SAMPLE_FMT_FLTP, in_fmt,
channels, sample_rate);
channels, sample_rate, c->apply_map);
if (!c->ac_in)
goto fail;
}
......
......@@ -66,7 +66,7 @@ typedef struct DitherDSPContext {
DitherContext *ff_dither_alloc(AVAudioResampleContext *avr,
enum AVSampleFormat out_fmt,
enum AVSampleFormat in_fmt,
int channels, int sample_rate);
int channels, int sample_rate, int apply_map);
/**
* Free a DitherContext.
......
......@@ -32,6 +32,24 @@ typedef struct AudioConvert AudioConvert;
typedef struct AudioMix AudioMix;
typedef struct ResampleContext ResampleContext;
enum RemapPoint {
REMAP_NONE,
REMAP_IN_COPY,
REMAP_IN_CONVERT,
REMAP_OUT_COPY,
REMAP_OUT_CONVERT,
};
typedef struct ChannelMapInfo {
int channel_map[AVRESAMPLE_MAX_CHANNELS]; /**< source index of each output channel, -1 if not remapped */
int do_remap; /**< remap needed */
int channel_copy[AVRESAMPLE_MAX_CHANNELS]; /**< dest index to copy from */
int do_copy; /**< copy needed */
int channel_zero[AVRESAMPLE_MAX_CHANNELS]; /**< dest index to zero */
int do_zero; /**< zeroing needed */
int input_map[AVRESAMPLE_MAX_CHANNELS]; /**< dest index of each input channel */
} ChannelMapInfo;
struct AVAudioResampleContext {
const AVClass *av_class; /**< AVClass for logging and AVOptions */
......@@ -65,6 +83,7 @@ struct AVAudioResampleContext {
int resample_needed; /**< resampling is needed */
int in_convert_needed; /**< input sample format conversion is needed */
int out_convert_needed; /**< output sample format conversion is needed */
int in_copy_needed; /**< input data copy is needed */
AudioData *in_buffer; /**< buffer for converted input */
AudioData *resample_out_buffer; /**< buffer for output from resampler */
......@@ -82,6 +101,10 @@ struct AVAudioResampleContext {
* only used if avresample_set_matrix() is called before avresample_open()
*/
double *mix_matrix;
int use_channel_map;
enum RemapPoint remap_point;
ChannelMapInfo ch_map_info;
};
#endif /* AVRESAMPLE_INTERNAL_H */
This diff is collapsed.
......@@ -20,8 +20,8 @@
#define AVRESAMPLE_VERSION_H
#define LIBAVRESAMPLE_VERSION_MAJOR 1
#define LIBAVRESAMPLE_VERSION_MINOR 0
#define LIBAVRESAMPLE_VERSION_MICRO 1
#define LIBAVRESAMPLE_VERSION_MINOR 1
#define LIBAVRESAMPLE_VERSION_MICRO 0
#define LIBAVRESAMPLE_VERSION_INT AV_VERSION_INT(LIBAVRESAMPLE_VERSION_MAJOR, \
LIBAVRESAMPLE_VERSION_MINOR, \
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment