一、问题
在项目中有一个逻辑是当数据帧的分辨率是奇数的时候,会使用 crop 进行裁切 yuv 数据,进行 2 对齐,在项目中使用的是 av_picture_crop 进行裁切的,具体代码类似于下面这样
void Crop() {
int32_t src_width = 1234;
int32_t src_height = 567;
uint8_t* src_buffer = new uint8_t[src_width * src_height * 3 / 2];
// 填充 srcBuffer
int32_t dst_width = 1234;
int32_t dst_height = 566;
uint8_t* dst_buffer = new uint8_t[dst_width * dst_height * 3 / 2];
AVPixelFormat format = AV_PIX_FMT_NV12;
auto* src_picture = static_cast<AVPicture*>(malloc(sizeof(AVPicture)));
auto* dst_picture = static_cast<AVPicture*>(malloc(sizeof(AVPicture)));
avpicture_alloc(src_picture, format, src_width, src_height);
avpicture_fill(src_picture, src_buffer, format, src_width, src_height);
avpicture_alloc(dst_picture, format, dst_width, dst_height);
av_picture_crop(dst_picture, src_picture, format, 0, 0);
avpicture_layout(dst_picture, format, dst_width, dst_height, dst_buffer, avpicture_get_size(format, dst_width, dst_height));
avpicture_free(src_picture);
avpicture_free(dst_picture);
free(src_picture);
free(dst_picture);
delete[] src_buffer;
delete[] dst_buffer;
}
上面这段代码,造成的问题要么是内存泄漏,要么是 avpicture_free 的时候 crash
二、解决办法
1、去掉 avpicture_alloc ,全部使用 avpicture_fill 来填充数据
2、去掉 avpicture_layout
修改过后的代码如下
void Crop() {
int32_t src_width = 1234;
int32_t src_height = 567;
uint8_t* src_buffer = new uint8_t[src_width * src_height * 3 / 2];
// 填充 srcBuffer
int32_t dst_width = 1234;
int32_t dst_height = 566;
uint8_t* dst_buffer = new uint8_t[dst_width * dst_height * 3 / 2];
AVPixelFormat format = AV_PIX_FMT_NV12;
auto* src_picture = static_cast<AVPicture*>(malloc(sizeof(AVPicture)));
auto* dst_picture = static_cast<AVPicture*>(malloc(sizeof(AVPicture)));
avpicture_fill(src_picture, src_buffer, format, src_width, src_height);
avpicture_fill(dst_picture, dst_buffer, format, dst_width, dst_height);
avpicture_alloc(dst_picture, format, dst_width, dst_height);
av_picture_crop(dst_picture, src_picture, format, 0, 0);
memcpy(dst_buffer, dst_picture->data[0], dst_width * dst_height * 3 / 2);
free(src_picture );
free(dst_picture );
// 进行某些操作
// 最后将之前开辟的 buffer 全部释放了
delete[] src_buffer;
delete[] dst_buffer;
}
代码也一下子清晰了很多
三、分析
下面仔细分析一下出问题的代码,很多分析过程是深入 ffmpeg 源码进行的分析,关于 ffmpeg 源码编译和下载的可以参考我的这一篇文章https://blog.csdn.net/yp18792574062/article/details/108962638
1、首先开辟了两个 buffer,src_buffer 和 dst buffer,然后再最后使用 delete[] 去释放,这一步没有问题
2、第二步使用了 avpicture_alloc(src_picture, format, src_width, src_height) 给 AVPicture 分配空间,AVPicture 是一个结构体
typedef struct AVPicture {
attribute_deprecated
uint8_t *data[AV_NUM_DATA_POINTERS]; ///< pointers to the image data planes
attribute_deprecated
int linesize[AV_NUM_DATA_POINTERS]; ///< number of bytes per line
} AVPicture;
因为我们传进去的是 NV12 格式的数据,那么 linesize 只有 0 和 1,看源码发现 avpicture_alloc 的逻辑是这样的
uint8_t* buffer = (uint8_t*)malloc(width * height * 3 / 2);
picture->data[0] = buffer;
picture->data[1] = buffer + width * height;
相当于分配了一段内存给到 AVPicture 的 data
然后使用 avpicture_fill 填充数据,avpicture_fille 的内部逻辑是这样的
picture->data[0] = src_buffer;
picture->data[1] = picture->data[0] + width * height;
这个时候我们发现,这里 data[0] 的指针已经改变了,那么之前 data[0] 指向的那段数据却没有去释放,这里就是问题点了,那么我们可以直接使用 avpicture_fill,不使用 avpicture_alloc 了,使用外面的 buffer,那么 dst_picture 也可以使用 fill 填充,这样数据内存都是外面自己去做管理
然后接下来进行 av_picture_crop 没有问题,到这里就完成了,avpicture_layout 也不需要要了,因为 dst_picture->data[0] 指向的数据也就是 dst_buffer 的区域了,指向的是同一段内存区域,至于 avpicture_layout 它内部的逻辑也就是进行 memcpy,将 dst_picture->data 的数据拷贝到 dst_buffer 里面
另外需要提一点的就是 av_picture_crop,avpicture_alloc 这一套东西都已经被弃用了,如果要进行 crop 可以参考我的这一篇文章https://blog.csdn.net/yp18792574062/article/details/109148041