FFMPEG - AVFrame to per channel array conversion
<p>I am looking to copy an <code>AVFrame</code> into an array where pixels are stored one channel at a time in a row-major order.</p>

<p>Details:</p>

<p>I am using FFMPEG's api to read frames from a video. I have used <code>avcodec_decode_video2</code> to fetch each frame as an <code>AVFrame</code> as follows:</p>

<pre><code>AVFormatContext* fmt_ctx = NULL;
avformat_open_input(&amp;fmt_ctx, filepath, NULL, NULL);
...
int video_stream_idx;  // stores the stream index for the video
...
AVFrame* vid_frame = NULL;
vid_frame = av_frame_alloc();
AVPacket vid_pckt;
int frame_finish;
...
while (av_read_frame(fmt_ctx, &amp;vid_pckt) &gt;= 0) {
    if (b_vid_pckt.stream_index == video_stream_idx) {
        avcodec_decode_video2(cdc_ctx, vid_frame, &amp;frame_finish, &amp;vid_pckt);
        if (frame_finish) {
            /* perform conversion */
        }
    }
}
</code></pre>

<p>The destination array looks like this:</p>

<pre><code>unsigned char* frame_arr = new unsigned char [cdc_ctx-&gt;width * cdc_ctx-&gt;height * 3];
</code></pre>

<p>I need to copy all of <code>vid_frame</code> into <code>frame_arr</code>, where the range of pixel values should be [0, 255]. The problem is that the array needs to store the frame in row major order, one channel at a time, i.e. R11, R12, ... R21, R22, ... G11, G12, ... G21, G22, ... B11, B12, ... B21, B22, ... (I have used the notation [color channel][row index][column index], i.e. G21 is the green channel value of pixel at row 2, column 1). I have had a look at <code>sws_scale</code>, but I don't understand it enough to figure out whether that function is capable of doing such a conversion. Can somebody help!! :)</p>