mirror of
https://git.ffmpeg.org/ffmpeg.git
synced 2026-06-16 04:32:47 +02:00
ffmpeg: add -map_channel option.
Based on an initial work by Baptiste Coudurier.
This commit is contained in:
@@ -35,11 +35,13 @@
|
||||
struct AVAudioConvert {
|
||||
int channels;
|
||||
int fmt_pair;
|
||||
const int *ch_map;
|
||||
};
|
||||
|
||||
AVAudioConvert *swr_audio_convert_alloc(enum AVSampleFormat out_fmt,
|
||||
enum AVSampleFormat in_fmt,
|
||||
int channels, int flags)
|
||||
int channels, const int *ch_map,
|
||||
int flags)
|
||||
{
|
||||
AVAudioConvert *ctx;
|
||||
ctx = av_malloc(sizeof(AVAudioConvert));
|
||||
@@ -47,6 +49,7 @@ AVAudioConvert *swr_audio_convert_alloc(enum AVSampleFormat out_fmt,
|
||||
return NULL;
|
||||
ctx->channels = channels;
|
||||
ctx->fmt_pair = out_fmt + AV_SAMPLE_FMT_NB*in_fmt;
|
||||
ctx->ch_map = ch_map;
|
||||
return ctx;
|
||||
}
|
||||
|
||||
@@ -58,15 +61,17 @@ void swr_audio_convert_free(AVAudioConvert **ctx)
|
||||
int swr_audio_convert(AVAudioConvert *ctx, AudioData *out, AudioData*in, int len)
|
||||
{
|
||||
int ch;
|
||||
const uint8_t null_input[8] = {0};
|
||||
|
||||
av_assert0(ctx->channels == out->ch_count);
|
||||
|
||||
//FIXME optimize common cases
|
||||
|
||||
for(ch=0; ch<ctx->channels; ch++){
|
||||
const int is= (in ->planar ? 1 : in->ch_count) * in->bps;
|
||||
const int ich= ctx->ch_map ? ctx->ch_map[ch] : ch;
|
||||
const int is= ich < 0 ? 0 : (in->planar ? 1 : in->ch_count) * in->bps;
|
||||
const int os= (out->planar ? 1 :out->ch_count) *out->bps;
|
||||
const uint8_t *pi= in ->ch[ch];
|
||||
const uint8_t *pi= ich < 0 ? null_input : in->ch[ich];
|
||||
uint8_t *po= out->ch[ch];
|
||||
uint8_t *end= po + os*len;
|
||||
if(!po)
|
||||
|
||||
@@ -42,11 +42,14 @@ typedef struct AVAudioConvert AVAudioConvert;
|
||||
* @param in_fmt Input sample format
|
||||
* @param channels Number of channels
|
||||
* @param flags See AV_CPU_FLAG_xx
|
||||
* @param ch_map list of the channels id to pick from the source stream, NULL
|
||||
* if all channels must be selected
|
||||
* @return NULL on error
|
||||
*/
|
||||
AVAudioConvert *swr_audio_convert_alloc(enum AVSampleFormat out_fmt,
|
||||
enum AVSampleFormat in_fmt,
|
||||
int channels, int flags);
|
||||
int channels, const int *ch_map,
|
||||
int flags);
|
||||
|
||||
/**
|
||||
* Free audio sample format converter context.
|
||||
|
||||
@@ -38,6 +38,7 @@
|
||||
static const AVOption options[]={
|
||||
{"ich", "input channel count", OFFSET( in.ch_count ), AV_OPT_TYPE_INT, {.dbl=2}, 1, SWR_CH_MAX, 0},
|
||||
{"och", "output channel count", OFFSET(out.ch_count ), AV_OPT_TYPE_INT, {.dbl=2}, 1, SWR_CH_MAX, 0},
|
||||
{"uch", "used channel count", OFFSET(used_ch_count ), AV_OPT_TYPE_INT, {.dbl=2}, 1, SWR_CH_MAX, 0},
|
||||
{"isr", "input sample rate" , OFFSET( in_sample_rate), AV_OPT_TYPE_INT, {.dbl=48000}, 1, INT_MAX, 0},
|
||||
{"osr", "output sample rate" , OFFSET(out_sample_rate), AV_OPT_TYPE_INT, {.dbl=48000}, 1, INT_MAX, 0},
|
||||
//{"ip" , "input planar" , OFFSET( in.planar ), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1, 0},
|
||||
@@ -76,7 +77,7 @@ SwrContext *swr_alloc(void){
|
||||
|
||||
SwrContext *swr_alloc2(struct SwrContext *s, int64_t out_ch_layout, enum AVSampleFormat out_sample_fmt, int out_sample_rate,
|
||||
int64_t in_ch_layout, enum AVSampleFormat in_sample_fmt, int in_sample_rate,
|
||||
int log_offset, void *log_ctx){
|
||||
const int *channel_map, int log_offset, void *log_ctx){
|
||||
if(!s) s= swr_alloc();
|
||||
if(!s) return NULL;
|
||||
|
||||
@@ -90,9 +91,11 @@ SwrContext *swr_alloc2(struct SwrContext *s, int64_t out_ch_layout, enum AVSampl
|
||||
av_set_int(s, "isf", in_sample_fmt);
|
||||
av_set_int(s, "isr", in_sample_rate);
|
||||
|
||||
s->channel_map = channel_map;
|
||||
s-> in.ch_count= av_get_channel_layout_nb_channels(s-> in_ch_layout);
|
||||
s->out.ch_count= av_get_channel_layout_nb_channels(s->out_ch_layout);
|
||||
s->int_sample_fmt = AV_SAMPLE_FMT_S16;
|
||||
s->used_ch_count= s-> in.ch_count;
|
||||
|
||||
return s;
|
||||
}
|
||||
@@ -167,13 +170,16 @@ int swr_init(SwrContext *s){
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(s-> in.ch_count && s-> in_ch_layout && s->in.ch_count != av_get_channel_layout_nb_channels(s-> in_ch_layout)){
|
||||
av_log(s, AV_LOG_WARNING, "Input channel layout has a different number of channels than there actually is, ignoring layout\n");
|
||||
if(!s->used_ch_count)
|
||||
s->used_ch_count= s->in.ch_count;
|
||||
|
||||
if(s->used_ch_count && s-> in_ch_layout && s->used_ch_count != av_get_channel_layout_nb_channels(s-> in_ch_layout)){
|
||||
av_log(s, AV_LOG_WARNING, "Input channel layout has a different number of channels than the number of used channels, ignoring layout\n");
|
||||
s-> in_ch_layout= 0;
|
||||
}
|
||||
|
||||
if(!s-> in_ch_layout)
|
||||
s-> in_ch_layout= av_get_default_channel_layout(s->in.ch_count);
|
||||
s-> in_ch_layout= av_get_default_channel_layout(s->used_ch_count);
|
||||
if(!s->out_ch_layout)
|
||||
s->out_ch_layout= av_get_default_channel_layout(s->out.ch_count);
|
||||
|
||||
@@ -182,10 +188,13 @@ int swr_init(SwrContext *s){
|
||||
#define RSC 1 //FIXME finetune
|
||||
if(!s-> in.ch_count)
|
||||
s-> in.ch_count= av_get_channel_layout_nb_channels(s-> in_ch_layout);
|
||||
if(!s->used_ch_count)
|
||||
s->used_ch_count= s->in.ch_count;
|
||||
if(!s->out.ch_count)
|
||||
s->out.ch_count= av_get_channel_layout_nb_channels(s->out_ch_layout);
|
||||
|
||||
av_assert0(s-> in.ch_count);
|
||||
av_assert0(s->used_ch_count);
|
||||
av_assert0(s->out.ch_count);
|
||||
s->resample_first= RSC*s->out.ch_count/s->in.ch_count - RSC < s->out_sample_rate/(float)s-> in_sample_rate - 1.0;
|
||||
|
||||
@@ -193,22 +202,27 @@ av_assert0(s->out.ch_count);
|
||||
s->int_bps= av_get_bits_per_sample_fmt(s->int_sample_fmt)/8;
|
||||
s->out.bps= av_get_bits_per_sample_fmt(s->out_sample_fmt)/8;
|
||||
|
||||
if(!s->resample && !s->rematrix){
|
||||
if(!s->resample && !s->rematrix && !s->channel_map){
|
||||
s->full_convert = swr_audio_convert_alloc(s->out_sample_fmt,
|
||||
s-> in_sample_fmt, s-> in.ch_count, 0);
|
||||
s-> in_sample_fmt, s-> in.ch_count, NULL, 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
s->in_convert = swr_audio_convert_alloc(s->int_sample_fmt,
|
||||
s-> in_sample_fmt, s-> in.ch_count, 0);
|
||||
s-> in_sample_fmt, s->used_ch_count, s->channel_map, 0);
|
||||
s->out_convert= swr_audio_convert_alloc(s->out_sample_fmt,
|
||||
s->int_sample_fmt, s->out.ch_count, 0);
|
||||
s->int_sample_fmt, s->out.ch_count, NULL, 0);
|
||||
|
||||
|
||||
s->postin= s->in;
|
||||
s->preout= s->out;
|
||||
s->midbuf= s->in;
|
||||
s->in_buffer= s->in;
|
||||
if(s->channel_map){
|
||||
s->postin.ch_count=
|
||||
s->midbuf.ch_count=
|
||||
s->in_buffer.ch_count= s->used_ch_count;
|
||||
}
|
||||
if(!s->resample_first){
|
||||
s->midbuf.ch_count= s->out.ch_count;
|
||||
s->in_buffer.ch_count = s->out.ch_count;
|
||||
@@ -325,7 +339,7 @@ int swr_convert(struct SwrContext *s, uint8_t *out_arg[SWR_CH_MAX], int out_coun
|
||||
if((ret=realloc_audio(&s->postin, in_count))<0)
|
||||
return ret;
|
||||
if(s->resample_first){
|
||||
av_assert0(s->midbuf.ch_count == s-> in.ch_count);
|
||||
av_assert0(s->midbuf.ch_count == s->used_ch_count);
|
||||
if((ret=realloc_audio(&s->midbuf, out_count))<0)
|
||||
return ret;
|
||||
}else{
|
||||
|
||||
@@ -25,7 +25,7 @@
|
||||
#include "libavutil/samplefmt.h"
|
||||
|
||||
#define LIBSWRESAMPLE_VERSION_MAJOR 0
|
||||
#define LIBSWRESAMPLE_VERSION_MINOR 1
|
||||
#define LIBSWRESAMPLE_VERSION_MINOR 2
|
||||
#define LIBSWRESAMPLE_VERSION_MICRO 0
|
||||
|
||||
#define SWR_CH_MAX 16
|
||||
@@ -57,7 +57,7 @@ int swr_init(struct SwrContext *s);
|
||||
*/
|
||||
struct SwrContext *swr_alloc2(struct SwrContext *s, int64_t out_ch_layout, enum AVSampleFormat out_sample_fmt, int out_sample_rate,
|
||||
int64_t in_ch_layout, enum AVSampleFormat in_sample_fmt, int in_sample_rate,
|
||||
int log_offset, void *log_ctx);
|
||||
const int *channel_map, int log_offset, void *log_ctx);
|
||||
|
||||
/**
|
||||
* Free the given SwrContext.
|
||||
|
||||
@@ -45,6 +45,8 @@ typedef struct SwrContext { //FIXME find unused fields
|
||||
int out_sample_rate;
|
||||
int flags;
|
||||
float slev, clev, rematrix_volume;
|
||||
const int *channel_map; ///< channel index (or -1 if muted channel) map
|
||||
int used_ch_count; ///< number of used channels (mapped channel count if channel_map, otherwise in.ch_count)
|
||||
|
||||
//below are private
|
||||
int int_bps;
|
||||
|
||||
@@ -131,9 +131,11 @@ int main(int argc, char **argv){
|
||||
in_sample_rate, out_sample_rate,
|
||||
av_get_sample_fmt_name(in_sample_fmt), av_get_sample_fmt_name(out_sample_fmt));
|
||||
forw_ctx = swr_alloc2(forw_ctx, out_ch_layout, out_sample_fmt+planar_out, out_sample_rate,
|
||||
in_ch_layout, in_sample_fmt+planar_in , in_sample_rate, 0, 0);
|
||||
backw_ctx = swr_alloc2(backw_ctx,in_ch_layout, in_sample_fmt, in_sample_rate,
|
||||
out_ch_layout, out_sample_fmt+planar_out, out_sample_rate, 0, 0);
|
||||
in_ch_layout, in_sample_fmt+planar_in , in_sample_rate,
|
||||
NULL, 0, 0);
|
||||
backw_ctx = swr_alloc2(backw_ctx,in_ch_layout, in_sample_fmt, in_sample_rate,
|
||||
out_ch_layout, out_sample_fmt+planar_out, out_sample_rate,
|
||||
NULL, 0, 0);
|
||||
if(swr_init( forw_ctx) < 0)
|
||||
fprintf(stderr, "swr_init(->) failed\n");
|
||||
if(swr_init(backw_ctx) < 0)
|
||||
|
||||
Reference in New Issue
Block a user