Question

I am trying to pull out a rectangular area of an AVFrame and have started on a function that will do so. I'm only interested in using AVFrames that are format PIX_FMT_RGB24. I may be reinventing the wheel a bit here too, so please jump in if there is already a function to do this. So far my function looks like this:

AVFrame * getRGBsection(AVFrame *pFrameRGB, const int start_x, const int start_y, const int w, const int h) {

AVFrame *pFrameSect;
int numBytes;
uint8_t *mb_buffer;

pFrameSect = avcodec_alloc_frame();
numBytes = avpicture_get_size(PIX_FMT_RGB24, w, h);
mb_buffer = (uint8_t *) av_malloc(numBytes * sizeof(uint8_t));
avpicture_fill((AVPicture *) pFrameSect, mb_buffer, PIX_FMT_RGB24, w, h);

int curY, curX, i = 0;
for (curY = start_y ; curY < (start_y + h); curY++) {

    for (curX = start_x; curX < (start_x + w); curX++) {

        int curIndex = curX * 3 + curY * pFrameRGB->linesize[0];

        pFrameSect->data[0][i] = pFrameRGB->data[0][curIndex];
        pFrameSect->data[0][i + 1] = pFrameRGB->data[0][curIndex + 1];
        pFrameSect->data[0][i + 2] = pFrameRGB->data[0][curIndex + 2];

        i += 3;

    }

}

return pFrameSect;

}

The function seems to work when I start at (0,0) (I think) but when I move elsewhere in the image it outputs colors similar to what should be there but isn't right. I think I am very close here, can anyone offer guidance?

Was it helpful?

Solution

  • there are 2 options

    1. user video filter (vf_crop). (filtering_video.c provide example for using crop pragmatically)
    2. function av_picture_crop() in imgconvert.c. This function is not complete but you can modify it for your use.

OTHER TIPS

This code works for me (only RGB24)

#include <libavutil/imgutils.h>

// .....
// left, top
const int crop_left = 500;
const int crop_top = 500;

// output width, height
const int crop_width = 300;
const int crop_height = 200;

AVFrame * rgb24picure;
AVFrame * output;
/// .... initialize ....

const int one_pixel = av_image_get_linesize(PIX_FMT_RGB24, 1, 0);
const int src_line_size = av_image_get_linesize(PIX_FMT_RGB24, source_width, 0);
const int copy_line_size = av_image_get_linesize(PIX_FMT_RGB24, crop_width, 0);

for (int h = crop_top; h < crop_top + crop_height; ++h)
{
    unsigned char * src = rgb24picure->data[0] + src_line_size * h + one_pixel * crop_left;
    unsigned char * dst = output->data[0] + copy_line_size * (h - crop_top);
    memcpy(dst, src, copy_line_size);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top