Browse code

ffmpeg: add -map_channel option.

Based on an initial work by Baptiste Coudurier.

Clément Bœsch authored on 2011/11/05 02:54:01
Showing 15 changed files
... ...
@@ -72,6 +72,7 @@ easier to use. The changes are:
72 72
 - Prores encoder
73 73
 - Video Decoder Acceleration (VDA) HWAccel module.
74 74
 - replacement Indeo 3 decoder
75
+- new ffmpeg option: -map_channel
75 76
 
76 77
 version 0.8:
77 78
 
... ...
@@ -721,6 +721,44 @@ ffmpeg -i INPUT -map 0 -map -0:a:1 OUTPUT
721 721
 
722 722
 Note that using this option disables the default mappings for this output file.
723 723
 
724
+@item -map_channel [@var{input_file_id}.@var{stream_specifier}.@var{channel_id}|-1][:@var{output_file_id}.@var{stream_specifier}]
725
+Map an audio channel from a given input to an output. If
726
+@var{output_file_id}.@var{stream_specifier} are not set, the audio channel will
727
+be mapped on all the audio streams.
728
+
729
+Using "-1" instead of
730
+@var{input_file_id}.@var{stream_specifier}.@var{channel_id} will map a muted
731
+channel.
732
+
733
+For example, assuming @var{INPUT} is a stereo audio file, you can switch the
734
+two audio channels with the following command:
735
+@example
736
+ffmpeg -i INPUT -map_channel 0.0.1 -map_channel 0.0.0 OUTPUT
737
+@end example
738
+
739
+If you want to mute the first channel and keep the second:
740
+@example
741
+ffmpeg -i INPUT -map_channel -1 -map_channel 0.0.1 OUTPUT
742
+@end example
743
+
744
+The order of the "-map_channel" option specifies the order of the channels in
745
+the output stream. The output channel layout is guessed from the number of
746
+channels mapped (mono if one "-map_channel", stereo if two, etc.). Using "-ac"
747
+in combination of "-map_channel" makes the channel gain levels to be updated if
748
+channel layouts don't match (for instance two "-map_channel" options and "-ac
749
+6").
750
+
751
+You can also extract each channel of an @var{INPUT} to specific outputs; the
752
+following command extract each channel of the audio stream (file 0, stream 0)
753
+to the respective @var{OUTPUT_CH0} and @var{OUTPUT_CH1}:
754
+@example
755
+ffmpeg -i INPUT -map_channel 0.0.0 OUTPUT_CH0 -map_channel 0.0.1 OUTPUT_CH1
756
+@end example
757
+
758
+Note that "-map_channel" is currently limited to the scope of one input for
759
+each output; you can't for example use it to pick multiple input audio files
760
+and mix them into one single output.
761
+
724 762
 @item -map_metadata[:@var{metadata_type}][:@var{index}] @var{infile}[:@var{metadata_type}][:@var{index}] (@emph{output,per-metadata})
725 763
 Set metadata information of the next output file from @var{infile}. Note that
726 764
 those are file indices (zero-based), not filenames.
... ...
@@ -99,6 +99,11 @@ typedef struct StreamMap {
99 99
     int sync_stream_index;
100 100
 } StreamMap;
101 101
 
102
+typedef struct {
103
+    int  file_idx,  stream_idx,  channel_idx; // input
104
+    int ofile_idx, ostream_idx;               // output
105
+} AudioChannelMap;
106
+
102 107
 /**
103 108
  * select an input file for an output file
104 109
  */
... ...
@@ -231,6 +236,8 @@ typedef struct OutputStream {
231 231
 
232 232
     /* audio only */
233 233
     int audio_resample;
234
+    int audio_channels_map[SWR_CH_MAX];  ///< list of the channels id to pick from the source stream
235
+    int audio_channels_mapped;           ///< number of channels in audio_channels_map
234 236
     int resample_sample_fmt;
235 237
     int resample_channels;
236 238
     int resample_sample_rate;
... ...
@@ -313,6 +320,8 @@ typedef struct OptionsContext {
313 313
     /* output options */
314 314
     StreamMap *stream_maps;
315 315
     int     nb_stream_maps;
316
+    AudioChannelMap *audio_channel_maps; ///< one info entry per -map_channel
317
+    int           nb_audio_channel_maps; ///< number of (valid) -map_channel settings
316 318
     /* first item specifies output metadata, second is input */
317 319
     MetadataMap (*meta_data_maps)[2];
318 320
     int nb_meta_data_maps;
... ...
@@ -409,6 +418,7 @@ static void reset_options(OptionsContext *o, int is_input)
409 409
     }
410 410
 
411 411
     av_freep(&o->stream_maps);
412
+    av_freep(&o->audio_channel_maps);
412 413
     av_freep(&o->meta_data_maps);
413 414
     av_freep(&o->streamid_map);
414 415
 
... ...
@@ -857,7 +867,7 @@ need_realloc:
857 857
                        ost->resample_channels    != dec->channels   ||
858 858
                        ost->resample_sample_rate != dec->sample_rate;
859 859
 
860
-    if ((ost->audio_resample && !ost->swr) || resample_changed) {
860
+    if ((ost->audio_resample && !ost->swr) || resample_changed || ost->audio_channels_mapped) {
861 861
         if (resample_changed) {
862 862
             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",
863 863
                    ist->file_index, ist->st->index,
... ...
@@ -869,7 +879,7 @@ need_realloc:
869 869
             swr_free(&ost->swr);
870 870
         }
871 871
         /* if audio_sync_method is >1 the resampler is needed for audio drift compensation */
872
-        if (audio_sync_method <= 1 &&
872
+        if (audio_sync_method <= 1 && !ost->audio_channels_mapped &&
873 873
             ost->resample_sample_fmt  == enc->sample_fmt &&
874 874
             ost->resample_channels    == enc->channels   &&
875 875
             ost->resample_sample_rate == enc->sample_rate) {
... ...
@@ -879,8 +889,13 @@ need_realloc:
879 879
             ost->swr = swr_alloc2(ost->swr,
880 880
                                   enc->channel_layout, enc->sample_fmt, enc->sample_rate,
881 881
                                   dec->channel_layout, dec->sample_fmt, dec->sample_rate,
882
+                                  ost->audio_channels_mapped ? ost->audio_channels_map : NULL,
882 883
                                   0, NULL);
883 884
             av_set_double(ost->swr, "rmvol", ost->rematrix_volume);
885
+            if (ost->audio_channels_mapped) {
886
+                av_set_int(ost->swr, "icl", av_get_default_channel_layout(ost->audio_channels_mapped));
887
+                av_set_int(ost->swr, "uch", ost->audio_channels_mapped);
888
+            }
884 889
             av_set_int(ost->swr, "ich", dec->channels);
885 890
             av_set_int(ost->swr, "och", enc->channels);
886 891
             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,
2176 2176
                 if (codec->sample_fmt == AV_SAMPLE_FMT_NONE)
2177 2177
                     codec->sample_fmt = icodec->sample_fmt;
2178 2178
                 choose_sample_fmt(ost->st, ost->enc);
2179
-                if (!codec->channels) {
2179
+                if (ost->audio_channels_mapped) {
2180
+                    /* the requested output channel is set to the number of
2181
+                     * -map_channel only if no -ac are specified */
2182
+                    if (!codec->channels) {
2183
+                        codec->channels       = ost->audio_channels_mapped;
2184
+                        codec->channel_layout = av_get_default_channel_layout(codec->channels);
2185
+                        if (!codec->channel_layout) {
2186
+                            av_log(NULL, AV_LOG_FATAL, "Unable to find an appropriate channel layout for requested number of channel\n");
2187
+                            exit_program(1);
2188
+                        }
2189
+                    }
2190
+                    /* fill unused channel mapping with -1 (which means a muted
2191
+                     * channel in case the number of output channels is bigger
2192
+                     * than the number of mapped channel) */
2193
+                    for (j = ost->audio_channels_mapped; j < FF_ARRAY_ELEMS(ost->audio_channels_map); j++)
2194
+                        ost->audio_channels_map[j] = -1;
2195
+                } else if (!codec->channels) {
2180 2196
                     codec->channels = icodec->channels;
2181 2197
                     codec->channel_layout = icodec->channel_layout;
2182 2198
                 }
... ...
@@ -2388,6 +2419,15 @@ static int transcode_init(OutputFile *output_files, int nb_output_files,
2388 2388
                input_streams[ost->source_index].st->index,
2389 2389
                ost->file_index,
2390 2390
                ost->index);
2391
+        if (ost->audio_channels_mapped) {
2392
+            av_log(NULL, AV_LOG_INFO, " [ch:");
2393
+            for (j = 0; j < ost->audio_channels_mapped; j++)
2394
+                if (ost->audio_channels_map[j] == -1)
2395
+                    av_log(NULL, AV_LOG_INFO, " M");
2396
+                else
2397
+                    av_log(NULL, AV_LOG_INFO, " %d", ost->audio_channels_map[j]);
2398
+            av_log(NULL, AV_LOG_INFO, "]");
2399
+        }
2391 2400
         if (ost->sync_ist != &input_streams[ost->source_index])
2392 2401
             av_log(NULL, AV_LOG_INFO, " [sync #%d.%d]",
2393 2402
                    ost->sync_ist->file_index,
... ...
@@ -2899,6 +2939,66 @@ static int opt_attach(OptionsContext *o, const char *opt, const char *arg)
2899 2899
     return 0;
2900 2900
 }
2901 2901
 
2902
+static int opt_map_channel(OptionsContext *o, const char *opt, const char *arg)
2903
+{
2904
+    int n;
2905
+    AVStream *st;
2906
+    AudioChannelMap *m;
2907
+
2908
+    o->audio_channel_maps =
2909
+        grow_array(o->audio_channel_maps, sizeof(*o->audio_channel_maps),
2910
+                   &o->nb_audio_channel_maps, o->nb_audio_channel_maps + 1);
2911
+    m = &o->audio_channel_maps[o->nb_audio_channel_maps - 1];
2912
+
2913
+    /* muted channel syntax */
2914
+    n = sscanf(arg, "%d:%d.%d", &m->channel_idx, &m->ofile_idx, &m->ostream_idx);
2915
+    if ((n == 1 || n == 3) && m->channel_idx == -1) {
2916
+        m->file_idx = m->stream_idx = -1;
2917
+        if (n == 1)
2918
+            m->ofile_idx = m->ostream_idx = -1;
2919
+        return 0;
2920
+    }
2921
+
2922
+    /* normal syntax */
2923
+    n = sscanf(arg, "%d.%d.%d:%d.%d",
2924
+               &m->file_idx,  &m->stream_idx, &m->channel_idx,
2925
+               &m->ofile_idx, &m->ostream_idx);
2926
+
2927
+    if (n != 3 && n != 5) {
2928
+        av_log(NULL, AV_LOG_FATAL, "Syntax error, mapchan usage: "
2929
+               "[file.stream.channel|-1][:syncfile:syncstream]\n");
2930
+        exit_program(1);
2931
+    }
2932
+
2933
+    if (n != 5) // only file.stream.channel specified
2934
+        m->ofile_idx = m->ostream_idx = -1;
2935
+
2936
+    /* check input */
2937
+    if (m->file_idx < 0 || m->file_idx >= nb_input_files) {
2938
+        av_log(NULL, AV_LOG_FATAL, "mapchan: invalid input file index: %d\n",
2939
+               m->file_idx);
2940
+        exit_program(1);
2941
+    }
2942
+    if (m->stream_idx < 0 ||
2943
+        m->stream_idx >= input_files[m->file_idx].nb_streams) {
2944
+        av_log(NULL, AV_LOG_FATAL, "mapchan: invalid input file stream index #%d.%d\n",
2945
+               m->file_idx, m->stream_idx);
2946
+        exit_program(1);
2947
+    }
2948
+    st = input_files[m->file_idx].ctx->streams[m->stream_idx];
2949
+    if (st->codec->codec_type != AVMEDIA_TYPE_AUDIO) {
2950
+        av_log(NULL, AV_LOG_FATAL, "mapchan: stream #%d.%d is not an audio stream.\n",
2951
+               m->file_idx, m->stream_idx);
2952
+        exit_program(1);
2953
+    }
2954
+    if (m->channel_idx < 0 || m->channel_idx >= st->codec->channels) {
2955
+        av_log(NULL, AV_LOG_FATAL, "mapchan: invalid audio channel #%d.%d.%d\n",
2956
+               m->file_idx, m->stream_idx, m->channel_idx);
2957
+        exit_program(1);
2958
+    }
2959
+    return 0;
2960
+}
2961
+
2902 2962
 static void parse_meta_type(char *arg, char *type, int *index)
2903 2963
 {
2904 2964
     if (*arg) {
... ...
@@ -3588,6 +3688,7 @@ static OutputStream *new_video_stream(OptionsContext *o, AVFormatContext *oc)
3588 3588
 
3589 3589
 static OutputStream *new_audio_stream(OptionsContext *o, AVFormatContext *oc)
3590 3590
 {
3591
+    int n;
3591 3592
     AVStream *st;
3592 3593
     OutputStream *ost;
3593 3594
     AVCodecContext *audio_enc;
... ...
@@ -3616,6 +3717,21 @@ static OutputStream *new_audio_stream(OptionsContext *o, AVFormatContext *oc)
3616 3616
         MATCH_PER_STREAM_OPT(rematrix_volume, f, ost->rematrix_volume, oc, st);
3617 3617
     }
3618 3618
 
3619
+    /* check for channel mapping for this audio stream */
3620
+    for (n = 0; n < o->nb_audio_channel_maps; n++) {
3621
+        AudioChannelMap *map = &o->audio_channel_maps[n];
3622
+        InputStream *ist = &input_streams[ost->source_index];
3623
+        if ((map->channel_idx == -1 || (ist->file_index == map->file_idx && ist->st->index == map->stream_idx)) &&
3624
+            (map->ofile_idx   == -1 || ost->file_index == map->ofile_idx) &&
3625
+            (map->ostream_idx == -1 || ost->st->index  == map->ostream_idx)) {
3626
+            if (ost->audio_channels_mapped < FF_ARRAY_ELEMS(ost->audio_channels_map))
3627
+                ost->audio_channels_map[ost->audio_channels_mapped++] = map->channel_idx;
3628
+            else
3629
+                av_log(NULL, AV_LOG_FATAL, "Max channel mapping for output %d.%d reached\n",
3630
+                       ost->file_index, ost->st->index);
3631
+        }
3632
+    }
3633
+
3619 3634
     return ost;
3620 3635
 }
3621 3636
 
... ...
@@ -4430,6 +4546,7 @@ static const OptionDef options[] = {
4430 4430
     { "codec", HAS_ARG | OPT_STRING | OPT_SPEC, {.off = OFFSET(codec_names)}, "codec name", "codec" },
4431 4431
     { "pre", HAS_ARG | OPT_STRING | OPT_SPEC, {.off = OFFSET(presets)}, "preset name", "preset" },
4432 4432
     { "map", HAS_ARG | OPT_EXPERT | OPT_FUNC2, {(void*)opt_map}, "set input stream mapping", "file.stream[:syncfile.syncstream]" },
4433
+    { "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]" },
4433 4434
     { "map_meta_data", HAS_ARG | OPT_EXPERT | OPT_FUNC2, {(void*)opt_map_meta_data}, "DEPRECATED set meta data information of outfile from infile",
4434 4435
       "outfile[,metadata]:infile[,metadata]" },
4435 4436
     { "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)
2077 2077
                     swr_free(&is->swr_ctx);
2078 2078
                 is->swr_ctx = swr_alloc2(NULL, is->audio_tgt_channel_layout, is->audio_tgt_fmt, is->audio_tgt_freq,
2079 2079
                                                dec_channel_layout,          dec->sample_fmt,   dec->sample_rate,
2080
-                                               0, NULL);
2080
+                                               NULL, 0, NULL);
2081 2081
                 if (!is->swr_ctx || swr_init(is->swr_ctx) < 0) {
2082 2082
                     fprintf(stderr, "Cannot create sample rate converter for conversion of %d Hz %s %d channels to %d Hz %s %d channels!\n",
2083 2083
                         dec->sample_rate,
... ...
@@ -35,11 +35,13 @@
35 35
 struct AVAudioConvert {
36 36
     int channels;
37 37
     int fmt_pair;
38
+    const int *ch_map;
38 39
 };
39 40
 
40 41
 AVAudioConvert *swr_audio_convert_alloc(enum AVSampleFormat out_fmt,
41 42
                                         enum AVSampleFormat in_fmt,
42
-                                        int channels, int flags)
43
+                                        int channels, const int *ch_map,
44
+                                        int flags)
43 45
 {
44 46
     AVAudioConvert *ctx;
45 47
     ctx = av_malloc(sizeof(AVAudioConvert));
... ...
@@ -47,6 +49,7 @@ AVAudioConvert *swr_audio_convert_alloc(enum AVSampleFormat out_fmt,
47 47
         return NULL;
48 48
     ctx->channels = channels;
49 49
     ctx->fmt_pair = out_fmt + AV_SAMPLE_FMT_NB*in_fmt;
50
+    ctx->ch_map   = ch_map;
50 51
     return ctx;
51 52
 }
52 53
 
... ...
@@ -58,15 +61,17 @@ void swr_audio_convert_free(AVAudioConvert **ctx)
58 58
 int swr_audio_convert(AVAudioConvert *ctx, AudioData *out, AudioData*in, int len)
59 59
 {
60 60
     int ch;
61
+    const uint8_t null_input[8] = {0};
61 62
 
62 63
     av_assert0(ctx->channels == out->ch_count);
63 64
 
64 65
     //FIXME optimize common cases
65 66
 
66 67
     for(ch=0; ch<ctx->channels; ch++){
67
-        const int is= (in ->planar ? 1 : in->ch_count) * in->bps;
68
+        const int ich= ctx->ch_map ? ctx->ch_map[ch] : ch;
69
+        const int is= ich < 0 ? 0 : (in->planar ? 1 : in->ch_count) * in->bps;
68 70
         const int os= (out->planar ? 1 :out->ch_count) *out->bps;
69
-        const uint8_t *pi= in ->ch[ch];
71
+        const uint8_t *pi= ich < 0 ? null_input : in->ch[ich];
70 72
         uint8_t       *po= out->ch[ch];
71 73
         uint8_t *end= po + os*len;
72 74
         if(!po)
... ...
@@ -42,11 +42,14 @@ typedef struct AVAudioConvert AVAudioConvert;
42 42
  * @param in_fmt Input sample format
43 43
  * @param channels Number of channels
44 44
  * @param flags See AV_CPU_FLAG_xx
45
+ * @param ch_map list of the channels id to pick from the source stream, NULL
46
+ *               if all channels must be selected
45 47
  * @return NULL on error
46 48
  */
47 49
 AVAudioConvert *swr_audio_convert_alloc(enum AVSampleFormat out_fmt,
48 50
                                         enum AVSampleFormat in_fmt,
49
-                                        int channels, int flags);
51
+                                        int channels, const int *ch_map,
52
+                                        int flags);
50 53
 
51 54
 /**
52 55
  * Free audio sample format converter context.
... ...
@@ -38,6 +38,7 @@
38 38
 static const AVOption options[]={
39 39
 {"ich",  "input channel count", OFFSET( in.ch_count   ), AV_OPT_TYPE_INT, {.dbl=2}, 1, SWR_CH_MAX, 0},
40 40
 {"och", "output channel count", OFFSET(out.ch_count   ), AV_OPT_TYPE_INT, {.dbl=2}, 1, SWR_CH_MAX, 0},
41
+{"uch",   "used channel count", OFFSET(used_ch_count  ), AV_OPT_TYPE_INT, {.dbl=2}, 1, SWR_CH_MAX, 0},
41 42
 {"isr",  "input sample rate"  , OFFSET( in_sample_rate), AV_OPT_TYPE_INT, {.dbl=48000}, 1, INT_MAX, 0},
42 43
 {"osr", "output sample rate"  , OFFSET(out_sample_rate), AV_OPT_TYPE_INT, {.dbl=48000}, 1, INT_MAX, 0},
43 44
 //{"ip" ,  "input planar"       , OFFSET( in.planar     ), AV_OPT_TYPE_INT, {.dbl=0},    0,       1, 0},
... ...
@@ -76,7 +77,7 @@ SwrContext *swr_alloc(void){
76 76
 
77 77
 SwrContext *swr_alloc2(struct SwrContext *s, int64_t out_ch_layout, enum AVSampleFormat out_sample_fmt, int out_sample_rate,
78 78
                        int64_t  in_ch_layout, enum AVSampleFormat  in_sample_fmt, int  in_sample_rate,
79
-                       int log_offset, void *log_ctx){
79
+                       const int *channel_map, int log_offset, void *log_ctx){
80 80
     if(!s) s= swr_alloc();
81 81
     if(!s) return NULL;
82 82
 
... ...
@@ -90,9 +91,11 @@ SwrContext *swr_alloc2(struct SwrContext *s, int64_t out_ch_layout, enum AVSampl
90 90
     av_set_int(s, "isf", in_sample_fmt);
91 91
     av_set_int(s, "isr", in_sample_rate);
92 92
 
93
+    s->channel_map = channel_map;
93 94
     s-> in.ch_count= av_get_channel_layout_nb_channels(s-> in_ch_layout);
94 95
     s->out.ch_count= av_get_channel_layout_nb_channels(s->out_ch_layout);
95 96
     s->int_sample_fmt = AV_SAMPLE_FMT_S16;
97
+    s->used_ch_count= s-> in.ch_count;
96 98
 
97 99
     return s;
98 100
 }
... ...
@@ -167,13 +170,16 @@ int swr_init(SwrContext *s){
167 167
         return -1;
168 168
     }
169 169
 
170
-    if(s-> in.ch_count && s-> in_ch_layout && s->in.ch_count != av_get_channel_layout_nb_channels(s-> in_ch_layout)){
171
-        av_log(s, AV_LOG_WARNING, "Input channel layout has a different number of channels than there actually is, ignoring layout\n");
170
+    if(!s->used_ch_count)
171
+        s->used_ch_count= s->in.ch_count;
172
+
173
+    if(s->used_ch_count && s-> in_ch_layout && s->used_ch_count != av_get_channel_layout_nb_channels(s-> in_ch_layout)){
174
+        av_log(s, AV_LOG_WARNING, "Input channel layout has a different number of channels than the number of used channels, ignoring layout\n");
172 175
         s-> in_ch_layout= 0;
173 176
     }
174 177
 
175 178
     if(!s-> in_ch_layout)
176
-        s-> in_ch_layout= av_get_default_channel_layout(s->in.ch_count);
179
+        s-> in_ch_layout= av_get_default_channel_layout(s->used_ch_count);
177 180
     if(!s->out_ch_layout)
178 181
         s->out_ch_layout= av_get_default_channel_layout(s->out.ch_count);
179 182
 
... ...
@@ -182,10 +188,13 @@ int swr_init(SwrContext *s){
182 182
 #define RSC 1 //FIXME finetune
183 183
     if(!s-> in.ch_count)
184 184
         s-> in.ch_count= av_get_channel_layout_nb_channels(s-> in_ch_layout);
185
+    if(!s->used_ch_count)
186
+        s->used_ch_count= s->in.ch_count;
185 187
     if(!s->out.ch_count)
186 188
         s->out.ch_count= av_get_channel_layout_nb_channels(s->out_ch_layout);
187 189
 
188 190
 av_assert0(s-> in.ch_count);
191
+av_assert0(s->used_ch_count);
189 192
 av_assert0(s->out.ch_count);
190 193
     s->resample_first= RSC*s->out.ch_count/s->in.ch_count - RSC < s->out_sample_rate/(float)s-> in_sample_rate - 1.0;
191 194
 
... ...
@@ -193,22 +202,27 @@ av_assert0(s->out.ch_count);
193 193
     s->int_bps= av_get_bits_per_sample_fmt(s->int_sample_fmt)/8;
194 194
     s->out.bps= av_get_bits_per_sample_fmt(s->out_sample_fmt)/8;
195 195
 
196
-    if(!s->resample && !s->rematrix){
196
+    if(!s->resample && !s->rematrix && !s->channel_map){
197 197
         s->full_convert = swr_audio_convert_alloc(s->out_sample_fmt,
198
-                                                  s-> in_sample_fmt, s-> in.ch_count, 0);
198
+                                                  s-> in_sample_fmt, s-> in.ch_count, NULL, 0);
199 199
         return 0;
200 200
     }
201 201
 
202 202
     s->in_convert = swr_audio_convert_alloc(s->int_sample_fmt,
203
-                                            s-> in_sample_fmt, s-> in.ch_count, 0);
203
+                                            s-> in_sample_fmt, s->used_ch_count, s->channel_map, 0);
204 204
     s->out_convert= swr_audio_convert_alloc(s->out_sample_fmt,
205
-                                            s->int_sample_fmt, s->out.ch_count, 0);
205
+                                            s->int_sample_fmt, s->out.ch_count, NULL, 0);
206 206
 
207 207
 
208 208
     s->postin= s->in;
209 209
     s->preout= s->out;
210 210
     s->midbuf= s->in;
211 211
     s->in_buffer= s->in;
212
+    if(s->channel_map){
213
+        s->postin.ch_count=
214
+        s->midbuf.ch_count=
215
+        s->in_buffer.ch_count= s->used_ch_count;
216
+    }
212 217
     if(!s->resample_first){
213 218
         s->midbuf.ch_count= s->out.ch_count;
214 219
         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
325 325
     if((ret=realloc_audio(&s->postin, in_count))<0)
326 326
         return ret;
327 327
     if(s->resample_first){
328
-        av_assert0(s->midbuf.ch_count ==  s-> in.ch_count);
328
+        av_assert0(s->midbuf.ch_count == s->used_ch_count);
329 329
         if((ret=realloc_audio(&s->midbuf, out_count))<0)
330 330
             return ret;
331 331
     }else{
... ...
@@ -25,7 +25,7 @@
25 25
 #include "libavutil/samplefmt.h"
26 26
 
27 27
 #define LIBSWRESAMPLE_VERSION_MAJOR 0
28
-#define LIBSWRESAMPLE_VERSION_MINOR 1
28
+#define LIBSWRESAMPLE_VERSION_MINOR 2
29 29
 #define LIBSWRESAMPLE_VERSION_MICRO 0
30 30
 
31 31
 #define SWR_CH_MAX 16
... ...
@@ -57,7 +57,7 @@ int swr_init(struct SwrContext *s);
57 57
  */
58 58
 struct SwrContext *swr_alloc2(struct SwrContext *s, int64_t out_ch_layout, enum AVSampleFormat out_sample_fmt, int out_sample_rate,
59 59
                               int64_t  in_ch_layout, enum AVSampleFormat  in_sample_fmt, int  in_sample_rate,
60
-                              int log_offset, void *log_ctx);
60
+                              const int *channel_map, int log_offset, void *log_ctx);
61 61
 
62 62
 /**
63 63
  * Free the given SwrContext.
... ...
@@ -45,6 +45,8 @@ typedef struct SwrContext {          //FIXME find unused fields
45 45
     int     out_sample_rate;
46 46
     int flags;
47 47
     float slev, clev, rematrix_volume;
48
+    const int *channel_map;             ///< channel index (or -1 if muted channel) map
49
+    int used_ch_count;                  ///< number of used channels (mapped channel count if channel_map, otherwise in.ch_count)
48 50
 
49 51
     //below are private
50 52
     int int_bps;
... ...
@@ -131,9 +131,11 @@ int main(int argc, char **argv){
131 131
                                in_sample_rate, out_sample_rate,
132 132
                                av_get_sample_fmt_name(in_sample_fmt), av_get_sample_fmt_name(out_sample_fmt));
133 133
                         forw_ctx  = swr_alloc2(forw_ctx, out_ch_layout, out_sample_fmt+planar_out, out_sample_rate,
134
-                                                                  in_ch_layout,  in_sample_fmt+planar_in ,  in_sample_rate, 0, 0);
135
-                        backw_ctx = swr_alloc2(backw_ctx,in_ch_layout,  in_sample_fmt,  in_sample_rate,
136
-                                                                 out_ch_layout, out_sample_fmt+planar_out, out_sample_rate, 0, 0);
134
+                                                          in_ch_layout,  in_sample_fmt+planar_in ,  in_sample_rate,
135
+                                                          NULL, 0, 0);
136
+                        backw_ctx = swr_alloc2(backw_ctx,in_ch_layout,  in_sample_fmt,             in_sample_rate,
137
+                                                        out_ch_layout, out_sample_fmt+planar_out, out_sample_rate,
138
+                                                        NULL, 0, 0);
137 139
                         if(swr_init( forw_ctx) < 0)
138 140
                             fprintf(stderr, "swr_init(->) failed\n");
139 141
                         if(swr_init(backw_ctx) < 0)
... ...
@@ -30,6 +30,14 @@ tests/data/asynth-16000-1.sw: tests/audiogen$(HOSTEXESUF)
30 30
 	@mkdir -p tests/data
31 31
 	$(M)./$< $@ 16000 1
32 32
 
33
+tests/data/mapchan-6ch.sw: tests/audiogen$(HOSTEXESUF)
34
+	@mkdir -p tests/data
35
+	$(M)./$< $@ 22050 6
36
+
37
+tests/data/mapchan-mono.sw: tests/audiogen$(HOSTEXESUF)
38
+	@mkdir -p tests/data
39
+	$(M)./$< $@ 22050 1
40
+
33 41
 tests/data/asynth%.sw tests/vsynth%/00.pgm: TAG = GEN
34 42
 
35 43
 include $(SRC_PATH)/tests/fate.mak
... ...
@@ -137,6 +137,18 @@ FATE_TESTS += fate-g722enc
137 137
 fate-g722enc: tests/data/asynth-16000-1.sw
138 138
 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
139 139
 
140
+FATE_TESTS += fate-mapchan-6ch-extract-2
141
+fate-mapchan-6ch-extract-2: tests/data/mapchan-6ch.sw
142
+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:
143
+
144
+FATE_TESTS += fate-mapchan-6ch-extract-2-downmix-mono
145
+fate-mapchan-6ch-extract-2-downmix-mono: tests/data/mapchan-6ch.sw
146
+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
147
+
148
+FATE_TESTS += fate-mapchan-silent-mono
149
+fate-mapchan-silent-mono: tests/data/mapchan-mono.sw
150
+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
151
+
140 152
 FATE_TESTS += fate-msmpeg4v1
141 153
 fate-msmpeg4v1: CMD = framecrc -flags +bitexact -dct fastint -idct simple -i $(SAMPLES)/msmpeg4v1/mpg4.avi -an
142 154
 
143 155
new file mode 100644
... ...
@@ -0,0 +1,2 @@
0
+6f091fe8c0be88c75921731dc9f74314
1
+5c2d162b9024329eb367295d37b8ca0a
0 2
new file mode 100644
... ...
@@ -0,0 +1 @@
0
+959645ed73e6d08d8f1e947eac5d0b92
0 1
new file mode 100644
... ...
@@ -0,0 +1 @@
0
+4f5148f08587a4b9794aa52aec7852ac