今天在做编解码时,遇到一个奇怪的问题,就是av_image_alloc 导致内存泄漏,这点比较纳闷,场景是这样的,在视频传输过程中,需要根据网速来切换码流大小哦,如果带宽不够自动切换低码流传输,于是我将采集的1080p的视频转化成yuv格式发送。
过程如下:
首先初始化编解码环境:
if (_thumbCodecContext != nullptr && _thumbCodec != nullptr)
{
return 0;
}
int ret;
AVCodecID codec_id = AV_CODEC_ID_MJPEG;
int in_w = width, in_h = height;
_width = width;
_height = height;
int framenum = 1;
_thumbCodec = avcodec_find_encoder(codec_id);
if (!_thumbCodec) {
return 40;
}
_thumbCodecContext = avcodec_alloc_context3(_thumbCodec);
if (!_thumbCodecContext) {
return 40;
}
_thumbCodecContext->bit_rate = 1024 *1024 * 10;
_thumbCodecContext->width = scaleWidth;
_thumbCodecContext->height = scaleHeight;
_thumbCodecContext->time_base.num = 1;
_thumbCodecContext->time_base.den = 11;
_thumbCodecContext->gop_size = 10;
_thumbCodecContex