[Libav-user] Audio sample format while encoding

praks411 praks411 at gmail.com
Fri Apr 19 11:16:32 CEST 2013


I'm trying to encode decoded audio packets alongwith with video frame into
avi file.
I'm facing some issue while encoding audio specifically with audio sample
format field.
The sample format for my audio packet is AV_SAMPLE_FMT_FLT. So when I add
audio  stream and set various stream parameters I also set the sample format
of code contex class to AV_SAMPLE_FMT_FLT.

I did normal avcodec_find_encoder() with codec id
AV_CODEC_ID_AAC/AV_CODEC_ID_AC3 (tried both). 
Then added audio stream and set various code context params(sample rate,
channel, sample fmt). 
However my avcodec_open2 is failing. I tried to debug it further and found
that codec return by avcodec_find_encoder has available sample format as 
AV_SAMPLE_FMT_FLTP due to this mismatch, the call to open codec is failing.
Is it possible to change  the codec format to AV_SAMPLE_FMT_FLT or some
other desire sample format?

Here is the function which add stream and open codec for audio.

int FfmpegEncoder::InitInputAudioStream(AVFormatContext*  oc)
    int ret = 0;
    char errbuf[128];
    AVCodec *m_out_aud_codec;
    AVStream *m_out_aud_strm;

    if(oc->oformat->audio_codec != AV_CODEC_ID_NONE)
       // oc->oformat->audio_codec = AV_CODEC_ID_AAC; //AV_CODEC_ID_AC3;
        m_out_aud_codec = avcodec_find_encoder(oc->oformat->audio_codec);
      //  m_out_aud_codec->sample_fmts = AV_SAMPLE_FMT_FLT;
        if(NULL == m_out_aud_codec)
            PRINT_MSG("Could Not Find Out Aud Encoder ")
            return -1;
            PRINT_MSG("Found Out Aud Encoder ")
            m_out_aud_strm = avformat_new_stream(oc, m_out_aud_codec);
            if(NULL == m_out_aud_strm)
                PRINT_MSG("Failed to Allocate Out Vid Strm ")
                return -1;
                m_out_aud_strm->index = 1,
                m_out_aud_strm->codec->channels =
                m_out_aud_strm->codec->channel_layout =
                m_out_aud_strm->codec->sample_fmt = AV_SAMPLE_FMT_FLT; 
                m_out_aud_strm->codec->sample_rate =
                m_out_aud_strm->codec->time_base.den =
                m_out_aud_strm->codec->time_base.num = 1;
                m_out_aud_strm->codec->bit_rate = 64000;
                m_out_aud_strm->codec->codec_tag = 0;

                 /* open the codec */
                AVDictionary *opts = NULL; 
                av_dict_set(&opts, "strict", "experimental", 0); 

                int err = 0;
                if((err=avcodec_open2( m_out_aud_strm->codec,
m_out_aud_codec, &opts)) < 0) 
                    return FF_ERR_CODEC_NOT_SUPPORTED;
        return -1;
    return 0;

View this message in context: http://libav-users.943685.n4.nabble.com/Audio-sample-format-while-encoding-tp4657309.html
Sent from the libav-users mailing list archive at Nabble.com.

More information about the Libav-user mailing list