从AVFrame-ffmpeg复制一个矩形区域



我正在尝试拉出AVFrame的矩形区域,并且已经开始了一个函数。我只对使用格式为PIX_FMT_RGB24的AVFrame感兴趣。我可能也在这里重新发明轮子,所以如果已经有功能可以做到这一点,请加入进来。到目前为止,我的功能如下:

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;
}

当我从(0,0)开始时,这个函数似乎可以工作(我想),但当我在图像的其他地方移动时,它输出的颜色与应该存在的颜色相似,但不正确。我想我离这里很近,有人能提供指导吗?

  • 有两种选择

    1. 用户视频过滤器(vf_crop)。(filtering_video.c提供了实用地使用crop的示例)
    2. imgconvert.c中的函数av_picture_crop()。此函数不完整,但您可以修改它以供使用

此代码适用于我(仅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);
}

最新更新