av_frame_xxx系列

本文详细解析了FFmpeg中AVFrame结构体的内存管理机制,包括av_frame_ref和av_frame_unref函数的具体作用。av_frame_ref会复制所有字段,但实际图像数据不会被复制;而av_frame_unref则会释放通过av_frame_ref分配的字段,但不会释放音视频数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

av_frame_ref拷贝所有的字段,包括extern_data这个二维数组也进行了拷贝,所以说真实的图像数据还是没有拷贝的。

av_frame_unref 也就是把上面那个函数分配出来的字段全部重置,分配的全部free, 但是音视频数据他是不会free的

void *decode_thread(void *arg) { AVCodecContext *decoder = init_sw_decoder(); if (!decoder) { fprintf(stderr, "Failed to initialize software decoder\n"); return NULL; } while (1) { VideoFrame frame = frame_queue_pop(&g_decode_queue); // 解码帧 AVPacket pkt; av_init_packet(&pkt); pkt.data = frame.data; pkt.size = frame.size; if (avcodec_send_packet(decoder, &pkt) == 0) { AVFrame *av_frame = av_frame_alloc(); if (avcodec_receive_frame(decoder, av_frame) == 0) { // 创建显示帧 if(av_frame->width!=video_width || av_frame->height!=video_height){ video_width = av_frame->width; video_height = av_frame->height; if (g_rgb_buf) free(g_rgb_buf); if (g_sws_ctx) sws_freeContext(g_sws_ctx); g_rgb_buf = (uint8_t *)malloc(video_width * video_height * 4); g_sws_ctx = sws_getContext( video_width, video_height, av_frame->format, video_width, video_height, AV_PIX_FMT_BGRA, SWS_BILINEAR, NULL, NULL, NULL ); } VideoFrame display_frame; display_frame.data = malloc(BUF_SIZE); display_frame.size = BUF_SIZE; display_frame.pts = frame.pts; uint8_t *dst[1] = { g_rgb_buf }; int dst_linesize[1] = {video_width * 4}; sws_scale(g_sws_ctx, (const uint8_t * const *)av_frame->data, av_frame->linesize, 0, video_height, dst, dst_linesize); memcpy(display_frame.data, av_frame, BUF_SIZE); // YUV数据 // 推入显示队列 if (frame_queue_push(&g_display_queue, display_frame) != 0) { free(display_frame.data); // 队列满时丢弃 } } av_frame_free(&av_frame); } av_packet_free(&pkt); } avcodec_free_context(&decoder); return NULL; } 报错了
最新发布
08-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值