1. sws_scale 函数 yuv420P-->yuv422转换时,发现dstData在释放时候会崩溃,查看源码发现在libswscal/rgb2rgb_template.c中,yv12toyuy2_c,有这么一句注释:
Height should be a multiple of 2 and width should be a multiple of 16.
(If this is a problem for anyone then tell me, and I will fix it.)
而源视频素材是856*480的,宽不是16的整数倍,鉴于sws_scale并未出错,所以以扩大dstData的大小来解决。
之前的申请方式为:
m_nVideoBufSize = av_image_get_buffer_size(m_pVideoState->ePixelFormat,
m_pVideoState->nWidth, m_pVideoState->nHeight, 1);
现在改为:
m_nVideoBufSize = av_image_get_buffer_size(m_pVideoState->ePixelFormat,
FFALIGN(m_pVideoState->nWidth, 16), m_pVideoState->nHeight, 1);
改后再次释放发现不会break。
结论:yuv422-->yuv420进行转换时,最终调用的函数为yv12toyuy2_c(){ ...yuvPlanartoyuy2_c()...},里面可能对源内存地址越界访问了,导致释放出错。
最后附上代码段。
/**
* Height should be a multiple of 2 and width should be a multiple of 16.
* (If this is a problem for anyone then tell me, and I will fix it.)
*/
static inline void yv12toyuy2_c(const uint8_t *ysrc, const uint8_t *usrc,
const uint8_t *vsrc, uint8_t *dst,
int width, int height, int lumStride,
int chromStride, int dstStride)
{
//FIXME interpolate chroma
yuvPlanartoyuy2_c(ysrc, usrc, vsrc, dst, width, height, lumStride,
chromStride, dstStride, 2);
}
static inline void yuvPlanartoyuy2_c(const uint8_t *ysrc, const uint8_t *usrc,
const uint8_t *vsrc, uint8_t *dst,
int width, int height,
int lumStride, int chromStride,
int dstStride, int vertLumPerChroma)
{
int y, i;
const int chromWidth = width >> 1;
for (y = 0; y < height; y++) {
#if HAVE_FAST_64BIT
uint64_t *ldst = (uint64_t *)dst;
const uint8_t *yc = ysrc, *uc = usrc, *vc = vsrc;
for (i = 0; i < chromWidth; i += 2) {
uint64_t k = yc[0] + (uc[0] << 8) +
(yc[1] << 16) + ((unsigned) vc[0] << 24);
uint64_t l = yc[2] + (uc[1] << 8) +
(yc[3] << 16) + ((unsigned) vc[1] << 24);
*ldst++ = k + (l << 32);
yc += 4;
uc += 2;
vc += 2;
}
#else
int *idst = (int32_t *)dst;
const uint8_t *yc = ysrc, *uc = usrc, *vc = vsrc;
for (i = 0; i < chromWidth; i++) {
#if HAVE_BIGENDIAN
*idst++ = (yc[0] << 24) + (uc[0] << 16) +
(yc[1] << 8) + (vc[0] << 0);
#else
*idst++ = yc[0] + (uc[0] << 8) +
(yc[1] << 16) + (vc[0] << 24);
#endif
yc += 2;
uc++;
vc++;
}
#endif
if ((y & (vertLumPerChroma - 1)) == vertLumPerChroma - 1) {
usrc += chromStride;
vsrc += chromStride;
}
ysrc += lumStride;
dst += dstStride;
}
}