Based on an initial work by Baptiste Coudurier.tags/n0.9
| @@ -72,6 +72,7 @@ easier to use. The changes are: | |||||
| - Prores encoder | - Prores encoder | ||||
| - Video Decoder Acceleration (VDA) HWAccel module. | - Video Decoder Acceleration (VDA) HWAccel module. | ||||
| - replacement Indeo 3 decoder | - replacement Indeo 3 decoder | ||||
| - new ffmpeg option: -map_channel | |||||
| version 0.8: | version 0.8: | ||||
| @@ -721,6 +721,44 @@ ffmpeg -i INPUT -map 0 -map -0:a:1 OUTPUT | |||||
| Note that using this option disables the default mappings for this output file. | Note that using this option disables the default mappings for this output file. | ||||
| @item -map_channel [@var{input_file_id}.@var{stream_specifier}.@var{channel_id}|-1][:@var{output_file_id}.@var{stream_specifier}] | |||||
| Map an audio channel from a given input to an output. If | |||||
| @var{output_file_id}.@var{stream_specifier} are not set, the audio channel will | |||||
| be mapped on all the audio streams. | |||||
| Using "-1" instead of | |||||
| @var{input_file_id}.@var{stream_specifier}.@var{channel_id} will map a muted | |||||
| channel. | |||||
| For example, assuming @var{INPUT} is a stereo audio file, you can switch the | |||||
| two audio channels with the following command: | |||||
| @example | |||||
| ffmpeg -i INPUT -map_channel 0.0.1 -map_channel 0.0.0 OUTPUT | |||||
| @end example | |||||
| If you want to mute the first channel and keep the second: | |||||
| @example | |||||
| ffmpeg -i INPUT -map_channel -1 -map_channel 0.0.1 OUTPUT | |||||
| @end example | |||||
| The order of the "-map_channel" option specifies the order of the channels in | |||||
| the output stream. The output channel layout is guessed from the number of | |||||
| channels mapped (mono if one "-map_channel", stereo if two, etc.). Using "-ac" | |||||
| in combination of "-map_channel" makes the channel gain levels to be updated if | |||||
| channel layouts don't match (for instance two "-map_channel" options and "-ac | |||||
| 6"). | |||||
| You can also extract each channel of an @var{INPUT} to specific outputs; the | |||||
| following command extract each channel of the audio stream (file 0, stream 0) | |||||
| to the respective @var{OUTPUT_CH0} and @var{OUTPUT_CH1}: | |||||
| @example | |||||
| ffmpeg -i INPUT -map_channel 0.0.0 OUTPUT_CH0 -map_channel 0.0.1 OUTPUT_CH1 | |||||
| @end example | |||||
| Note that "-map_channel" is currently limited to the scope of one input for | |||||
| each output; you can't for example use it to pick multiple input audio files | |||||
| and mix them into one single output. | |||||
| @item -map_metadata[:@var{metadata_type}][:@var{index}] @var{infile}[:@var{metadata_type}][:@var{index}] (@emph{output,per-metadata}) | @item -map_metadata[:@var{metadata_type}][:@var{index}] @var{infile}[:@var{metadata_type}][:@var{index}] (@emph{output,per-metadata}) | ||||
| Set metadata information of the next output file from @var{infile}. Note that | Set metadata information of the next output file from @var{infile}. Note that | ||||
| those are file indices (zero-based), not filenames. | those are file indices (zero-based), not filenames. | ||||
| @@ -99,6 +99,11 @@ typedef struct StreamMap { | |||||
| int sync_stream_index; | int sync_stream_index; | ||||
| } StreamMap; | } StreamMap; | ||||
| typedef struct { | |||||
| int file_idx, stream_idx, channel_idx; // input | |||||
| int ofile_idx, ostream_idx; // output | |||||
| } AudioChannelMap; | |||||
| /** | /** | ||||
| * select an input file for an output file | * select an input file for an output file | ||||
| */ | */ | ||||
| @@ -231,6 +236,8 @@ typedef struct OutputStream { | |||||
| /* audio only */ | /* audio only */ | ||||
| int audio_resample; | int audio_resample; | ||||
| int audio_channels_map[SWR_CH_MAX]; ///< list of the channels id to pick from the source stream | |||||
| int audio_channels_mapped; ///< number of channels in audio_channels_map | |||||
| int resample_sample_fmt; | int resample_sample_fmt; | ||||
| int resample_channels; | int resample_channels; | ||||
| int resample_sample_rate; | int resample_sample_rate; | ||||
| @@ -313,6 +320,8 @@ typedef struct OptionsContext { | |||||
| /* output options */ | /* output options */ | ||||
| StreamMap *stream_maps; | StreamMap *stream_maps; | ||||
| int nb_stream_maps; | int nb_stream_maps; | ||||
| AudioChannelMap *audio_channel_maps; ///< one info entry per -map_channel | |||||
| int nb_audio_channel_maps; ///< number of (valid) -map_channel settings | |||||
| /* first item specifies output metadata, second is input */ | /* first item specifies output metadata, second is input */ | ||||
| MetadataMap (*meta_data_maps)[2]; | MetadataMap (*meta_data_maps)[2]; | ||||
| int nb_meta_data_maps; | int nb_meta_data_maps; | ||||
| @@ -409,6 +418,7 @@ static void reset_options(OptionsContext *o, int is_input) | |||||
| } | } | ||||
| av_freep(&o->stream_maps); | av_freep(&o->stream_maps); | ||||
| av_freep(&o->audio_channel_maps); | |||||
| av_freep(&o->meta_data_maps); | av_freep(&o->meta_data_maps); | ||||
| av_freep(&o->streamid_map); | av_freep(&o->streamid_map); | ||||
| @@ -857,7 +867,7 @@ need_realloc: | |||||
| ost->resample_channels != dec->channels || | ost->resample_channels != dec->channels || | ||||
| ost->resample_sample_rate != dec->sample_rate; | ost->resample_sample_rate != dec->sample_rate; | ||||
| if ((ost->audio_resample && !ost->swr) || resample_changed) { | |||||
| if ((ost->audio_resample && !ost->swr) || resample_changed || ost->audio_channels_mapped) { | |||||
| if (resample_changed) { | if (resample_changed) { | ||||
| av_log(NULL, AV_LOG_INFO, "Input stream #%d.%d frame changed from rate:%d fmt:%s ch:%d to rate:%d fmt:%s ch:%d\n", | av_log(NULL, AV_LOG_INFO, "Input stream #%d.%d frame changed from rate:%d fmt:%s ch:%d to rate:%d fmt:%s ch:%d\n", | ||||
| ist->file_index, ist->st->index, | ist->file_index, ist->st->index, | ||||
| @@ -869,7 +879,7 @@ need_realloc: | |||||
| swr_free(&ost->swr); | swr_free(&ost->swr); | ||||
| } | } | ||||
| /* if audio_sync_method is >1 the resampler is needed for audio drift compensation */ | /* if audio_sync_method is >1 the resampler is needed for audio drift compensation */ | ||||
| if (audio_sync_method <= 1 && | |||||
| if (audio_sync_method <= 1 && !ost->audio_channels_mapped && | |||||
| ost->resample_sample_fmt == enc->sample_fmt && | ost->resample_sample_fmt == enc->sample_fmt && | ||||
| ost->resample_channels == enc->channels && | ost->resample_channels == enc->channels && | ||||
| ost->resample_sample_rate == enc->sample_rate) { | ost->resample_sample_rate == enc->sample_rate) { | ||||
| @@ -879,8 +889,13 @@ need_realloc: | |||||
| ost->swr = swr_alloc2(ost->swr, | ost->swr = swr_alloc2(ost->swr, | ||||
| enc->channel_layout, enc->sample_fmt, enc->sample_rate, | enc->channel_layout, enc->sample_fmt, enc->sample_rate, | ||||
| dec->channel_layout, dec->sample_fmt, dec->sample_rate, | dec->channel_layout, dec->sample_fmt, dec->sample_rate, | ||||
| ost->audio_channels_mapped ? ost->audio_channels_map : NULL, | |||||
| 0, NULL); | 0, NULL); | ||||
| av_set_double(ost->swr, "rmvol", ost->rematrix_volume); | av_set_double(ost->swr, "rmvol", ost->rematrix_volume); | ||||
| if (ost->audio_channels_mapped) { | |||||
| av_set_int(ost->swr, "icl", av_get_default_channel_layout(ost->audio_channels_mapped)); | |||||
| av_set_int(ost->swr, "uch", ost->audio_channels_mapped); | |||||
| } | |||||
| av_set_int(ost->swr, "ich", dec->channels); | av_set_int(ost->swr, "ich", dec->channels); | ||||
| av_set_int(ost->swr, "och", enc->channels); | av_set_int(ost->swr, "och", enc->channels); | ||||
| if(audio_sync_method>1) av_set_int(ost->swr, "flags", SWR_FLAG_RESAMPLE); | if(audio_sync_method>1) av_set_int(ost->swr, "flags", SWR_FLAG_RESAMPLE); | ||||
| @@ -2176,7 +2191,23 @@ static int transcode_init(OutputFile *output_files, int nb_output_files, | |||||
| if (codec->sample_fmt == AV_SAMPLE_FMT_NONE) | if (codec->sample_fmt == AV_SAMPLE_FMT_NONE) | ||||
| codec->sample_fmt = icodec->sample_fmt; | codec->sample_fmt = icodec->sample_fmt; | ||||
| choose_sample_fmt(ost->st, ost->enc); | choose_sample_fmt(ost->st, ost->enc); | ||||
| if (!codec->channels) { | |||||
| if (ost->audio_channels_mapped) { | |||||
| /* the requested output channel is set to the number of | |||||
| * -map_channel only if no -ac are specified */ | |||||
| if (!codec->channels) { | |||||
| codec->channels = ost->audio_channels_mapped; | |||||
| codec->channel_layout = av_get_default_channel_layout(codec->channels); | |||||
| if (!codec->channel_layout) { | |||||
| av_log(NULL, AV_LOG_FATAL, "Unable to find an appropriate channel layout for requested number of channel\n"); | |||||
| exit_program(1); | |||||
| } | |||||
| } | |||||
| /* fill unused channel mapping with -1 (which means a muted | |||||
| * channel in case the number of output channels is bigger | |||||
| * than the number of mapped channel) */ | |||||
| for (j = ost->audio_channels_mapped; j < FF_ARRAY_ELEMS(ost->audio_channels_map); j++) | |||||
| ost->audio_channels_map[j] = -1; | |||||
| } else if (!codec->channels) { | |||||
| codec->channels = icodec->channels; | codec->channels = icodec->channels; | ||||
| codec->channel_layout = icodec->channel_layout; | codec->channel_layout = icodec->channel_layout; | ||||
| } | } | ||||
| @@ -2388,6 +2419,15 @@ static int transcode_init(OutputFile *output_files, int nb_output_files, | |||||
| input_streams[ost->source_index].st->index, | input_streams[ost->source_index].st->index, | ||||
| ost->file_index, | ost->file_index, | ||||
| ost->index); | ost->index); | ||||
| if (ost->audio_channels_mapped) { | |||||
| av_log(NULL, AV_LOG_INFO, " [ch:"); | |||||
| for (j = 0; j < ost->audio_channels_mapped; j++) | |||||
| if (ost->audio_channels_map[j] == -1) | |||||
| av_log(NULL, AV_LOG_INFO, " M"); | |||||
| else | |||||
| av_log(NULL, AV_LOG_INFO, " %d", ost->audio_channels_map[j]); | |||||
| av_log(NULL, AV_LOG_INFO, "]"); | |||||
| } | |||||
| if (ost->sync_ist != &input_streams[ost->source_index]) | if (ost->sync_ist != &input_streams[ost->source_index]) | ||||
| av_log(NULL, AV_LOG_INFO, " [sync #%d.%d]", | av_log(NULL, AV_LOG_INFO, " [sync #%d.%d]", | ||||
| ost->sync_ist->file_index, | ost->sync_ist->file_index, | ||||
| @@ -2899,6 +2939,66 @@ static int opt_attach(OptionsContext *o, const char *opt, const char *arg) | |||||
| return 0; | return 0; | ||||
| } | } | ||||
| static int opt_map_channel(OptionsContext *o, const char *opt, const char *arg) | |||||
| { | |||||
| int n; | |||||
| AVStream *st; | |||||
| AudioChannelMap *m; | |||||
| o->audio_channel_maps = | |||||
| grow_array(o->audio_channel_maps, sizeof(*o->audio_channel_maps), | |||||
| &o->nb_audio_channel_maps, o->nb_audio_channel_maps + 1); | |||||
| m = &o->audio_channel_maps[o->nb_audio_channel_maps - 1]; | |||||
| /* muted channel syntax */ | |||||
| n = sscanf(arg, "%d:%d.%d", &m->channel_idx, &m->ofile_idx, &m->ostream_idx); | |||||
| if ((n == 1 || n == 3) && m->channel_idx == -1) { | |||||
| m->file_idx = m->stream_idx = -1; | |||||
| if (n == 1) | |||||
| m->ofile_idx = m->ostream_idx = -1; | |||||
| return 0; | |||||
| } | |||||
| /* normal syntax */ | |||||
| n = sscanf(arg, "%d.%d.%d:%d.%d", | |||||
| &m->file_idx, &m->stream_idx, &m->channel_idx, | |||||
| &m->ofile_idx, &m->ostream_idx); | |||||
| if (n != 3 && n != 5) { | |||||
| av_log(NULL, AV_LOG_FATAL, "Syntax error, mapchan usage: " | |||||
| "[file.stream.channel|-1][:syncfile:syncstream]\n"); | |||||
| exit_program(1); | |||||
| } | |||||
| if (n != 5) // only file.stream.channel specified | |||||
| m->ofile_idx = m->ostream_idx = -1; | |||||
| /* check input */ | |||||
| if (m->file_idx < 0 || m->file_idx >= nb_input_files) { | |||||
| av_log(NULL, AV_LOG_FATAL, "mapchan: invalid input file index: %d\n", | |||||
| m->file_idx); | |||||
| exit_program(1); | |||||
| } | |||||
| if (m->stream_idx < 0 || | |||||
| m->stream_idx >= input_files[m->file_idx].nb_streams) { | |||||
| av_log(NULL, AV_LOG_FATAL, "mapchan: invalid input file stream index #%d.%d\n", | |||||
| m->file_idx, m->stream_idx); | |||||
| exit_program(1); | |||||
| } | |||||
| st = input_files[m->file_idx].ctx->streams[m->stream_idx]; | |||||
| if (st->codec->codec_type != AVMEDIA_TYPE_AUDIO) { | |||||
| av_log(NULL, AV_LOG_FATAL, "mapchan: stream #%d.%d is not an audio stream.\n", | |||||
| m->file_idx, m->stream_idx); | |||||
| exit_program(1); | |||||
| } | |||||
| if (m->channel_idx < 0 || m->channel_idx >= st->codec->channels) { | |||||
| av_log(NULL, AV_LOG_FATAL, "mapchan: invalid audio channel #%d.%d.%d\n", | |||||
| m->file_idx, m->stream_idx, m->channel_idx); | |||||
| exit_program(1); | |||||
| } | |||||
| return 0; | |||||
| } | |||||
| static void parse_meta_type(char *arg, char *type, int *index) | static void parse_meta_type(char *arg, char *type, int *index) | ||||
| { | { | ||||
| if (*arg) { | if (*arg) { | ||||
| @@ -3588,6 +3688,7 @@ static OutputStream *new_video_stream(OptionsContext *o, AVFormatContext *oc) | |||||
| static OutputStream *new_audio_stream(OptionsContext *o, AVFormatContext *oc) | static OutputStream *new_audio_stream(OptionsContext *o, AVFormatContext *oc) | ||||
| { | { | ||||
| int n; | |||||
| AVStream *st; | AVStream *st; | ||||
| OutputStream *ost; | OutputStream *ost; | ||||
| AVCodecContext *audio_enc; | AVCodecContext *audio_enc; | ||||
| @@ -3616,6 +3717,21 @@ static OutputStream *new_audio_stream(OptionsContext *o, AVFormatContext *oc) | |||||
| MATCH_PER_STREAM_OPT(rematrix_volume, f, ost->rematrix_volume, oc, st); | MATCH_PER_STREAM_OPT(rematrix_volume, f, ost->rematrix_volume, oc, st); | ||||
| } | } | ||||
| /* check for channel mapping for this audio stream */ | |||||
| for (n = 0; n < o->nb_audio_channel_maps; n++) { | |||||
| AudioChannelMap *map = &o->audio_channel_maps[n]; | |||||
| InputStream *ist = &input_streams[ost->source_index]; | |||||
| if ((map->channel_idx == -1 || (ist->file_index == map->file_idx && ist->st->index == map->stream_idx)) && | |||||
| (map->ofile_idx == -1 || ost->file_index == map->ofile_idx) && | |||||
| (map->ostream_idx == -1 || ost->st->index == map->ostream_idx)) { | |||||
| if (ost->audio_channels_mapped < FF_ARRAY_ELEMS(ost->audio_channels_map)) | |||||
| ost->audio_channels_map[ost->audio_channels_mapped++] = map->channel_idx; | |||||
| else | |||||
| av_log(NULL, AV_LOG_FATAL, "Max channel mapping for output %d.%d reached\n", | |||||
| ost->file_index, ost->st->index); | |||||
| } | |||||
| } | |||||
| return ost; | return ost; | ||||
| } | } | ||||
| @@ -4430,6 +4546,7 @@ static const OptionDef options[] = { | |||||
| { "codec", HAS_ARG | OPT_STRING | OPT_SPEC, {.off = OFFSET(codec_names)}, "codec name", "codec" }, | { "codec", HAS_ARG | OPT_STRING | OPT_SPEC, {.off = OFFSET(codec_names)}, "codec name", "codec" }, | ||||
| { "pre", HAS_ARG | OPT_STRING | OPT_SPEC, {.off = OFFSET(presets)}, "preset name", "preset" }, | { "pre", HAS_ARG | OPT_STRING | OPT_SPEC, {.off = OFFSET(presets)}, "preset name", "preset" }, | ||||
| { "map", HAS_ARG | OPT_EXPERT | OPT_FUNC2, {(void*)opt_map}, "set input stream mapping", "file.stream[:syncfile.syncstream]" }, | { "map", HAS_ARG | OPT_EXPERT | OPT_FUNC2, {(void*)opt_map}, "set input stream mapping", "file.stream[:syncfile.syncstream]" }, | ||||
| { "map_channel", HAS_ARG | OPT_EXPERT | OPT_FUNC2, {(void*)opt_map_channel}, "map an audio channel from one stream to another", "file.stream.channel[:syncfile.syncstream]" }, | |||||
| { "map_meta_data", HAS_ARG | OPT_EXPERT | OPT_FUNC2, {(void*)opt_map_meta_data}, "DEPRECATED set meta data information of outfile from infile", | { "map_meta_data", HAS_ARG | OPT_EXPERT | OPT_FUNC2, {(void*)opt_map_meta_data}, "DEPRECATED set meta data information of outfile from infile", | ||||
| "outfile[,metadata]:infile[,metadata]" }, | "outfile[,metadata]:infile[,metadata]" }, | ||||
| { "map_metadata", HAS_ARG | OPT_EXPERT | OPT_FUNC2, {(void*)opt_map_metadata}, "set metadata information of outfile from infile", | { "map_metadata", HAS_ARG | OPT_EXPERT | OPT_FUNC2, {(void*)opt_map_metadata}, "set metadata information of outfile from infile", | ||||
| @@ -2077,7 +2077,7 @@ static int audio_decode_frame(VideoState *is, double *pts_ptr) | |||||
| swr_free(&is->swr_ctx); | swr_free(&is->swr_ctx); | ||||
| is->swr_ctx = swr_alloc2(NULL, is->audio_tgt_channel_layout, is->audio_tgt_fmt, is->audio_tgt_freq, | is->swr_ctx = swr_alloc2(NULL, is->audio_tgt_channel_layout, is->audio_tgt_fmt, is->audio_tgt_freq, | ||||
| dec_channel_layout, dec->sample_fmt, dec->sample_rate, | dec_channel_layout, dec->sample_fmt, dec->sample_rate, | ||||
| 0, NULL); | |||||
| NULL, 0, NULL); | |||||
| if (!is->swr_ctx || swr_init(is->swr_ctx) < 0) { | if (!is->swr_ctx || swr_init(is->swr_ctx) < 0) { | ||||
| fprintf(stderr, "Cannot create sample rate converter for conversion of %d Hz %s %d channels to %d Hz %s %d channels!\n", | fprintf(stderr, "Cannot create sample rate converter for conversion of %d Hz %s %d channels to %d Hz %s %d channels!\n", | ||||
| dec->sample_rate, | dec->sample_rate, | ||||
| @@ -35,11 +35,13 @@ | |||||
| struct AVAudioConvert { | struct AVAudioConvert { | ||||
| int channels; | int channels; | ||||
| int fmt_pair; | int fmt_pair; | ||||
| const int *ch_map; | |||||
| }; | }; | ||||
| AVAudioConvert *swr_audio_convert_alloc(enum AVSampleFormat out_fmt, | AVAudioConvert *swr_audio_convert_alloc(enum AVSampleFormat out_fmt, | ||||
| enum AVSampleFormat in_fmt, | enum AVSampleFormat in_fmt, | ||||
| int channels, int flags) | |||||
| int channels, const int *ch_map, | |||||
| int flags) | |||||
| { | { | ||||
| AVAudioConvert *ctx; | AVAudioConvert *ctx; | ||||
| ctx = av_malloc(sizeof(AVAudioConvert)); | ctx = av_malloc(sizeof(AVAudioConvert)); | ||||
| @@ -47,6 +49,7 @@ AVAudioConvert *swr_audio_convert_alloc(enum AVSampleFormat out_fmt, | |||||
| return NULL; | return NULL; | ||||
| ctx->channels = channels; | ctx->channels = channels; | ||||
| ctx->fmt_pair = out_fmt + AV_SAMPLE_FMT_NB*in_fmt; | ctx->fmt_pair = out_fmt + AV_SAMPLE_FMT_NB*in_fmt; | ||||
| ctx->ch_map = ch_map; | |||||
| return ctx; | 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 swr_audio_convert(AVAudioConvert *ctx, AudioData *out, AudioData*in, int len) | ||||
| { | { | ||||
| int ch; | int ch; | ||||
| const uint8_t null_input[8] = {0}; | |||||
| av_assert0(ctx->channels == out->ch_count); | av_assert0(ctx->channels == out->ch_count); | ||||
| //FIXME optimize common cases | //FIXME optimize common cases | ||||
| for(ch=0; ch<ctx->channels; ch++){ | 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 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 *po= out->ch[ch]; | ||||
| uint8_t *end= po + os*len; | uint8_t *end= po + os*len; | ||||
| if(!po) | if(!po) | ||||
| @@ -42,11 +42,14 @@ typedef struct AVAudioConvert AVAudioConvert; | |||||
| * @param in_fmt Input sample format | * @param in_fmt Input sample format | ||||
| * @param channels Number of channels | * @param channels Number of channels | ||||
| * @param flags See AV_CPU_FLAG_xx | * @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 | * @return NULL on error | ||||
| */ | */ | ||||
| AVAudioConvert *swr_audio_convert_alloc(enum AVSampleFormat out_fmt, | AVAudioConvert *swr_audio_convert_alloc(enum AVSampleFormat out_fmt, | ||||
| enum AVSampleFormat in_fmt, | enum AVSampleFormat in_fmt, | ||||
| int channels, int flags); | |||||
| int channels, const int *ch_map, | |||||
| int flags); | |||||
| /** | /** | ||||
| * Free audio sample format converter context. | * Free audio sample format converter context. | ||||
| @@ -38,6 +38,7 @@ | |||||
| static const AVOption options[]={ | static const AVOption options[]={ | ||||
| {"ich", "input channel count", OFFSET( in.ch_count ), AV_OPT_TYPE_INT, {.dbl=2}, 1, SWR_CH_MAX, 0}, | {"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}, | {"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}, | {"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}, | {"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}, | //{"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, | 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, | 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) s= swr_alloc(); | ||||
| if(!s) return NULL; | 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, "isf", in_sample_fmt); | ||||
| av_set_int(s, "isr", in_sample_rate); | 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-> 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->out.ch_count= av_get_channel_layout_nb_channels(s->out_ch_layout); | ||||
| s->int_sample_fmt = AV_SAMPLE_FMT_S16; | s->int_sample_fmt = AV_SAMPLE_FMT_S16; | ||||
| s->used_ch_count= s-> in.ch_count; | |||||
| return s; | return s; | ||||
| } | } | ||||
| @@ -167,13 +170,16 @@ int swr_init(SwrContext *s){ | |||||
| return -1; | 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; | s-> in_ch_layout= 0; | ||||
| } | } | ||||
| if(!s-> in_ch_layout) | 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) | if(!s->out_ch_layout) | ||||
| s->out_ch_layout= av_get_default_channel_layout(s->out.ch_count); | 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 | #define RSC 1 //FIXME finetune | ||||
| if(!s-> in.ch_count) | if(!s-> in.ch_count) | ||||
| s-> in.ch_count= av_get_channel_layout_nb_channels(s-> in_ch_layout); | 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) | if(!s->out.ch_count) | ||||
| s->out.ch_count= av_get_channel_layout_nb_channels(s->out_ch_layout); | s->out.ch_count= av_get_channel_layout_nb_channels(s->out_ch_layout); | ||||
| av_assert0(s-> in.ch_count); | av_assert0(s-> in.ch_count); | ||||
| av_assert0(s->used_ch_count); | |||||
| av_assert0(s->out.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; | 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->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; | 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->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; | return 0; | ||||
| } | } | ||||
| s->in_convert = swr_audio_convert_alloc(s->int_sample_fmt, | 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->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->postin= s->in; | ||||
| s->preout= s->out; | s->preout= s->out; | ||||
| s->midbuf= s->in; | s->midbuf= s->in; | ||||
| s->in_buffer= 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){ | if(!s->resample_first){ | ||||
| s->midbuf.ch_count= s->out.ch_count; | s->midbuf.ch_count= s->out.ch_count; | ||||
| s->in_buffer.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) | if((ret=realloc_audio(&s->postin, in_count))<0) | ||||
| return ret; | return ret; | ||||
| if(s->resample_first){ | 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) | if((ret=realloc_audio(&s->midbuf, out_count))<0) | ||||
| return ret; | return ret; | ||||
| }else{ | }else{ | ||||
| @@ -25,7 +25,7 @@ | |||||
| #include "libavutil/samplefmt.h" | #include "libavutil/samplefmt.h" | ||||
| #define LIBSWRESAMPLE_VERSION_MAJOR 0 | #define LIBSWRESAMPLE_VERSION_MAJOR 0 | ||||
| #define LIBSWRESAMPLE_VERSION_MINOR 1 | |||||
| #define LIBSWRESAMPLE_VERSION_MINOR 2 | |||||
| #define LIBSWRESAMPLE_VERSION_MICRO 0 | #define LIBSWRESAMPLE_VERSION_MICRO 0 | ||||
| #define SWR_CH_MAX 16 | #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, | 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, | 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. | * Free the given SwrContext. | ||||
| @@ -45,6 +45,8 @@ typedef struct SwrContext { //FIXME find unused fields | |||||
| int out_sample_rate; | int out_sample_rate; | ||||
| int flags; | int flags; | ||||
| float slev, clev, rematrix_volume; | 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 | //below are private | ||||
| int int_bps; | int int_bps; | ||||
| @@ -131,9 +131,11 @@ int main(int argc, char **argv){ | |||||
| in_sample_rate, out_sample_rate, | in_sample_rate, out_sample_rate, | ||||
| av_get_sample_fmt_name(in_sample_fmt), av_get_sample_fmt_name(out_sample_fmt)); | 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, | 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) | if(swr_init( forw_ctx) < 0) | ||||
| fprintf(stderr, "swr_init(->) failed\n"); | fprintf(stderr, "swr_init(->) failed\n"); | ||||
| if(swr_init(backw_ctx) < 0) | if(swr_init(backw_ctx) < 0) | ||||
| @@ -30,6 +30,14 @@ tests/data/asynth-16000-1.sw: tests/audiogen$(HOSTEXESUF) | |||||
| @mkdir -p tests/data | @mkdir -p tests/data | ||||
| $(M)./$< $@ 16000 1 | $(M)./$< $@ 16000 1 | ||||
| tests/data/mapchan-6ch.sw: tests/audiogen$(HOSTEXESUF) | |||||
| @mkdir -p tests/data | |||||
| $(M)./$< $@ 22050 6 | |||||
| tests/data/mapchan-mono.sw: tests/audiogen$(HOSTEXESUF) | |||||
| @mkdir -p tests/data | |||||
| $(M)./$< $@ 22050 1 | |||||
| tests/data/asynth%.sw tests/vsynth%/00.pgm: TAG = GEN | tests/data/asynth%.sw tests/vsynth%/00.pgm: TAG = GEN | ||||
| include $(SRC_PATH)/tests/fate.mak | include $(SRC_PATH)/tests/fate.mak | ||||
| @@ -137,6 +137,18 @@ FATE_TESTS += fate-g722enc | |||||
| fate-g722enc: tests/data/asynth-16000-1.sw | fate-g722enc: tests/data/asynth-16000-1.sw | ||||
| fate-g722enc: CMD = md5 -ar 16000 -ac 1 -f s16le -i $(TARGET_PATH)/tests/data/asynth-16000-1.sw -acodec g722 -ac 1 -f g722 | fate-g722enc: CMD = md5 -ar 16000 -ac 1 -f s16le -i $(TARGET_PATH)/tests/data/asynth-16000-1.sw -acodec g722 -ac 1 -f g722 | ||||
| FATE_TESTS += fate-mapchan-6ch-extract-2 | |||||
| fate-mapchan-6ch-extract-2: tests/data/mapchan-6ch.sw | |||||
| fate-mapchan-6ch-extract-2: CMD = avconv -ar 22050 -ac 6 -i $(TARGET_PATH)/tests/data/mapchan-6ch.sw -map_channel 0.0.0 -f wav md5: -map_channel 0.0.1 -f wav md5: | |||||
| FATE_TESTS += fate-mapchan-6ch-extract-2-downmix-mono | |||||
| fate-mapchan-6ch-extract-2-downmix-mono: tests/data/mapchan-6ch.sw | |||||
| fate-mapchan-6ch-extract-2-downmix-mono: CMD = md5 -ar 22050 -ac 6 -i $(TARGET_PATH)/tests/data/mapchan-6ch.sw -map_channel 0.0.1 -map_channel 0.0.0 -ac 1 -f wav | |||||
| FATE_TESTS += fate-mapchan-silent-mono | |||||
| fate-mapchan-silent-mono: tests/data/mapchan-mono.sw | |||||
| fate-mapchan-silent-mono: CMD = md5 -ar 22050 -ac 1 -i $(TARGET_PATH)/tests/data/mapchan-mono.sw -map_channel -1 -map_channel 0.0.0 -f wav | |||||
| FATE_TESTS += fate-msmpeg4v1 | FATE_TESTS += fate-msmpeg4v1 | ||||
| fate-msmpeg4v1: CMD = framecrc -flags +bitexact -dct fastint -idct simple -i $(SAMPLES)/msmpeg4v1/mpg4.avi -an | fate-msmpeg4v1: CMD = framecrc -flags +bitexact -dct fastint -idct simple -i $(SAMPLES)/msmpeg4v1/mpg4.avi -an | ||||
| @@ -0,0 +1,2 @@ | |||||
| 6f091fe8c0be88c75921731dc9f74314 | |||||
| 5c2d162b9024329eb367295d37b8ca0a | |||||
| @@ -0,0 +1 @@ | |||||
| 959645ed73e6d08d8f1e947eac5d0b92 | |||||
| @@ -0,0 +1 @@ | |||||
| 4f5148f08587a4b9794aa52aec7852ac | |||||