Using libavformat and libavcodec(2)

本文介绍如何使用libavformat和libavcodec从视频文件中逐帧解码视频,并处理跨包的帧边界问题。通过示例代码展示了如何读取并过滤视频流包,以及如何将解码后的YUV420格式转换为RGB格式。
摘要由CSDN通过智能技术生成

Decoding Video Frames

As I've already mentioned, a video file can contain several audio and video streams, and each of those streams is split up into packets of a particular size. Our job is to read these packets one by one using libavformat, filter out all those that aren't part of the video stream we're interested in, and hand them on to libavcodec for decoding. In doing this, we'll have to take care of the fact that the boundary between two frames can occur in the middle of a packet.

 

Sound complicated? Luckily, we can encapsulate this whole process in a routine that simply returns the next video frame:

 

Now, all we have to do is sit in a loop, calling GetNextFrame() until it return false. Just one more thing to take care of: Most codecs return images in YUV420(one luminance and two chrominance channels, with the chrominance channels samples at half the spatial resolution of the luminance channel). Depending on what you want to do with the video data, you may want to convert this to RGB. (Note, though, that this is not necessary if all you want to do is diplay the video data; take a look at the X11 Xvideo extension, which does YUV-to-RGB and scaling in hardware.) Fortunately, libavcodec provides a conversion routine called img_convert, which does conversion between YUV and RGB as well as a variety of other image formats. The loop that decodes the video thus becomes:

 

The RGB image pFrameRGB(of type AVFrame*) is allocated like this:

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值