FFmpeg: Encoding PCM 16 audio data Allocation erro

2019-02-28 19:39发布

问题:

I am currently trying to encode some raw audio data with some video inside an avi container.

The video codec used is mpeg4 and I would like to use the PCM_16LE for the audio codec but I am facing a problem regarding the AVCodec->frame_size parameter for the audio samples.

After doing all the correct allocation, I try allocating the audio frame and for AV_CODEC_ID_PCM_S16LE codec I don't have the codec frame_size needed to get the samples buffer size. Therefore the sample buffer size is huge and I simply can't allocate such quantity of memory. Does someone know how to bypass this issue and how to manually compute the frame_size ?

    frame = av_frame_alloc();
    if(!frame)
    {
        return NULL;
    }

    //Problem is right here with the frame_size
    frame->nb_samples = m_pAudioCodecContext->frame_size;
    frame->format = m_pAudioStream->codec->sample_fmt;
    frame->channel_layout = m_pAudioStream->codec->channel_layout;

    //The codec gives us the frame size, in samples, so we can calculate the size of the samples buffer in bytes
    //This returns a huge value due to a null frame_size
    m_audioSampleBufferSize = av_samples_get_buffer_size(NULL,
                                                         m_pAudioCodecContext->channels,
                                                         m_pAudioCodecContext->frame_size,
                                                         m_pAudioCodecContext->sample_fmt,
                                                         0);

Thank you for your help,

Robert

回答1:

As you can see in pcm_encode_init function in pcm.c

All pcm encoders have frame_size = 0;. Why?

Because in all PCM formats 'De facto' no such thing like frame, there is no compression by nature of PCM.

So you should decide by your own how many samples you wanna to store in buffer