[Libav-user] Buffering a specific channel position

Roberto Baibich me at robertobaibich.com
Mon Dec 2 19:22:46 CET 2013


Hi guys,

I'm working on an iOS app that allows people to turn specific channels on
or off while playing videos with surround audio tracks. I'm using kxmovie (
https://github.com/kolyvan/kxmovie) as the base for it.

The render callback on the audio unit is currently calling swresample,
which is remixing sound to match the 2-ch output of the Apple remote I/O.
As you probably know, different audio codecs encode their channels in
different orders, so I can't just always ask for the second channel in an
audio track, for example.

Is there a way for me to get a specific audio channel in mono (e.g. ask for
surround left and let ffmpeg figure out which one it is for the selected
codec) or to get it to set a default channel order and reorder it if the
format doesn't match it (which would then allow me to always point to the
same channel number if I want the surround left channel, for example).

Right now this is the code I have for the buffer:

- (kxMovieError) openAudioStream: (NSInteger) audioStream
{
    AVCodecContext *codecCtx = _formatCtx->streams[audioStream]->codec;
    SwrContext *swrContext = NULL;

    AVCodec *codec = avcodec_find_decoder(codecCtx->codec_id);
    if(!codec)
        return kxMovieErrorCodecNotFound;

    if (avcodec_open2(codecCtx, codec, NULL) < 0)
         return kxMovieErrorOpenCodec;

    if (!audioCodecIsSupported(codecCtx)) {

        id<KxAudioManager> audioManager = [KxAudioManager audioManager];
        swrContext = swr_alloc_set_opts(NULL,

av_get_default_channel_layout(audioManager.numOutputChannels),
                                        AV_SAMPLE_FMT_S16,
                                        audioManager.samplingRate,

av_get_default_channel_layout(codecCtx->channels),
                                        codecCtx->sample_fmt,
                                        codecCtx->sample_rate,
                                        0,
                                        NULL);

        if (!swrContext ||
            swr_init(swrContext)) {

            if (swrContext)
                swr_free(&swrContext);
             avcodec_close(codecCtx);

            return kxMovieErroReSampler;
        }
    }

    _audioFrame = avcodec_alloc_frame();
    if (!_audioFrame) {
        if (swrContext)
            swr_free(&swrContext);
        avcodec_close(codecCtx);
        return kxMovieErrorAllocateFrame;
    }

    _audioStream = audioStream;
    _audioCodecCtx = codecCtx;
    _swrContext = swrContext;

    AVStream *st = _formatCtx->streams[_audioStream];
    avStreamFPSTimeBase(st, 0.025, 0, &_audioTimeBase);

    return kxMovieErrorNone;
}

Can you help me out? Thanks!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20131202/f4dc3609/attachment.html>


More information about the Libav-user mailing list