简体   繁体   中英

Why does avcodec_fill_audio_frame return -22 when only sample count is different?

My problem is very fast to explain: I have to encode audio samples using FFmpeg (raw PCM to G.711 mu-law). This is the guilty part of my code (I put raw parameters in this example to be explicit):

AVFrame* frame = av_frame_alloc();
frame->nb_samples = 8000;
frame->format = AV_SAMPLE_FMT_S16;
frame->channels = 1;
frame->channel_layout = AV_CH_LAYOUT_MONO;
frame->sample_rate = 8000;
frame->quality = 1;

int res = avcodec_fill_audio_frame(frame, 1, AV_SAMPLE_FMT_S16, /*my samples data*/, 16000, 0);

// If res >= 0, continue with avcodec_encode_audio2

And it works :) ... Well, I mean...

When my input is 8000 audio samples ( S16 format so 16000 bytes), it works. But when I have 6000 audio samples (still S16 format so 12000 bytes), it fails with a -22 (invalid parameters). Any idea?

PRECISION: This sample count is not dynamically changing. I have sessions with data always composed by 8000 sample (and it works), and other sessions with data always composed by 6000 sample (and it fails). Sample count and data size are the only parameters that are not the same between these sessions.

EDIT: If I set or not the frame_size field in AVCodecContext , it returns to 0 after avcodec_open2 but the mu-law encoder selected has the AV_CODEC_CAP_VARIABLE_FRAME_SIZE capability so it sounds normal.

The input to each call to avcodec_encode_audio2() should be exactly N samples, where N is AVCodecContext::frame_size . In some cases this depends on the codec (eg mp3 frame size is constant), but in other cases it can be variable. It should still be constant in an encoding for most encoders. It looks like your value is just whatever the default was and mulaw has no built-in constant, so you can just specify a different frame_size value before calling avcodec_open2() and you should be OK.

If your number of samples chnages dynamically, you need a ring buffer to make sure the input to avcodec_encode_audio2() still has a constant number of samples.

Ok, I solved my own question, this was an alignment issue. There is two ways to solve it:

  • Giving input buffer initialized and filled according to the default alignment desired by libavcodec . You can use av_samples_get_buffer_size with the alignment value to 0 get the proper size.

  • Calling avcodec_fill_audio_frame with the alignment param to 1 to ignore alignment.

Hope this will help someone else :)

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