Build an AVFrame manually

For the developers that use FFmpeg in their software.
Post Reply
Posts: 1
Joined: Fri Oct 26, 2018 8:51 am

Build an AVFrame manually

Post by skident »

Hello everyone,

I have a WAV file (Stereo, 11025 Hz, 32 bit float) and I want to apply some audio filters on the fly.
So, I open the file (skip a header) and read, for example, 1000 bytes into a buffer.

Then I try to convert such buffer into an AVFrame, but I haven't found any appropriate description which provides a clear instructions how to do it.

Here is my last attempting to do it (I used a similar approach as in function get_input() in this example ... er_audio.c)

Code: Select all

void buffer_to_frame(const std::vector<char>& in_buffer, AVFrame* frame)
    float* src = (float*);
    float* dst_channel1 = (float*)frame->data[0];
    float* dst_channel2 = (float*)frame->data[1];

    for (int i = 0, j = 0; i < frame->nb_samples; i++, j += 2)
        dst_channel1[i] = src[j];
        dst_channel2[i] = src[j+1];
After filtering I get a quite different AVFrame (with only one plane 1) and after conversion such frame back into a buffer I got a noise into my headphones.

So, my question is next:
Does anybody know how to convert a raw audio buffer into a AVFrame and vice-versa without using AVPacket and Codecs.

Posts: 3
Joined: Fri Oct 26, 2018 11:33 am

Re: Build an AVFrame manually

Post by Hellopoplar »

It is ok of your code. But after put raw pcm to AVFrame, you need set frame's properties:
1. you should set AVFrame's format field, in your case, it should be AV_SAMPLE_FMT_FLTP (float planar data);
2. channel layout, channel number will be calculated via channel layout, and if you don't know channel layout, just leave it default and set channel number, in your case 2;
3. samplerate;

Posts: 61
Joined: Sat Aug 01, 2015 6:35 pm

Re: Build an AVFrame manually

Post by richardpl »

If you use libavfilter, you can call av_frame_get_buffer()
You first need to allocate AVFrame with function from libavutil and set some parameters in newly allocated AVFrame, as explained in doxy.
The function to allocate AVFrame is av_frame_alloc().

Post Reply