简体   繁体   中英

FFmpeg: Encoding PCM 16 audio data Allocation error

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM