使用ffmpeg库encode和decode视频

上一篇 / 下一篇  2013-02-28 19:35:50 / 个人分类:多媒体开发

FFMPEG库是一个开源的library,在实际的应用中,我们并不需要重新编译所有的代码,而直接使用他库中的功能。一般情况下,我们仅需要包含相应的头文件和之前编译成功的库文件(.so)。

在./ffmpeg/doc/examples目录里面有一个decoding_encoding.c代码,里面讲述了如何调用ffmpeg库进行音频编码、音频解码、视频编码、视频解码的基本使用方法。同时网络上,也有一些说明的详细例子。

1 打开Video Files,The first thing we need to do is to initialize libavformat/libavcodec:

1
av_register_all();

2 打开视频文件

1
2
3
4
5
6
AVFormatContext*pFormatCtx;
constchar     *filename="myvideo.mpg";

// Open video file
if(av_open_input_file(&pFormatCtx,filename,NULL,0,NULL)!=0)
    handle_error();// Couldn't open file

3 如果出错可以输出信息

1
dump_format(pFormatCtx,0,filename,false);

4 这里我们仅仅处理视频数据,而不包含音频数据
[code lang="c"]
int i, videoStream;
AVCodecContext *pCodecCtx;

// Find the first video stream
videoStream=-1;
for(i=0; i<pFormatCtx->nb_streams; i++)
if(pFormatCtx->streams[i]->codec.codec_type==CODEC_TYPE_VIDEO)
{
videoStream=i;
break;
}
if(videoStream==-1)
handle_error(); // Didn't find a video stream

// Get a pointer to the codec context for the video stream
pCodecCtx=&pFormatCtx->streams[videoStream]->codec;
[/code]

5 然后我们要得到真正的编码
[code lang="c"]
AVCodec *pCodec;

// Find the decoder for the video stream
pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
if(pCodec==NULL)
handle_error(); // Codec not found

// Inform. the codec that we can handle truncated bitstreams -- i.e.,
// bitstreams where frame. boundaries can fall in the middle of packets
if(pCodec->capabilities & CODEC_CAP_TRUNCATED)
pCodecCtx->flags|=CODEC_FLAG_TRUNCATED;

// Open codec
if(avcodec_open(pCodecCtx, pCodec)<0)
handle_error(); // Could not open codec
[/code]

6 特殊情况需要处理帧率,加上下面代码
[code lang="c"]
// Hack to correct wrong frame. rates that seem to be generated by some
// codecs
if(pCodecCtx->frame_rate>1000 && pCodecCtx->frame_rate_base==1)
pCodecCtx->frame_rate_base=1000;
[/code]

7 下面我们需要创建一帧数据来存放解码的图像

1
2
3
AVFrame.*pFrame;

pFrame=avcodec_alloc_frame();

8 下面我们封装一个解码每帧的函数

[code lang="c"]
bool GetNextFrame(AVFormatContext *pFormatCtx, AVCodecContext *pCodecCtx,
int videoStream, AVFrame. *pFrame)
{
static AVPacket packet;
static int bytesRemaining=0;
static uint8_t *rawData;
static bool fFirstTime=true;
int bytesDecoded;
int frameFinished;

// First time we're called, set packet.data to NULL to indicate it
// doesn't have to be freed
if(fFirstTime)
{
fFirstTime=false;
packet.data=NULL;
}

// Decode packets until we have decoded a complete frame
while(true)
{
// Work on the current packet until we have decoded all of it
while(bytesRemaining > 0)
{
// Decode the next chunk of data
bytesDecoded=avcodec_decode_video(pCodecCtx, pFrame,
&frameFinished, rawData, bytesRemaining);

// Was there an error?
if(bytesDecoded < 0)
{
fprintf(stderr, "Error while decoding framen");
return false;
}

bytesRemaining-=bytesDecoded;
rawData+=bytesDecoded;

// Did we finish the current frame? Then we can return
if(frameFinished)
return true;
}

// Read the next packet, skipping all packets that aren't for this
// stream
do
{
// Free old packet
if(packet.data!=NULL)
av_free_packet(&packet);

// Read new packet
if(av_read_packet(pFormatCtx, &packet)<0)
goto loop_exit;
} while(packet.stream_index!=videoStream);

bytesRemaining=packet.size;
rawData=packet.data;
}

loop_exit:

// Decode the rest of the last frame
bytesDecoded=avcodec_decode_video(pCodecCtx, pFrame, &frameFinished,
rawData, bytesRemaining);

// Free last packet
if(packet.data!=NULL)
av_free_packet(&packet);

return frameFinished!=0;
}
[/code]

9 在项目代码里面,我们就可以在循环里面调用8中的函数
[code lang="c"]
while(GetNextFrame(pFormatCtx, pCodecCtx, videoStream, pFrame))
{
img_convert((AVPicture *)pFrameRGB, PIX_FMT_RGB24, (AVPicture*)pFrame,
pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height);

// Process the video frame. (save to disk etc.)
DoSomethingWithTheImage(pFrameRGB);
}
[/code]

10 对RGB图像,我们的处理方法为
[code lang="c"]
AVFrame. *pFrameRGB;
int numBytes;
uint8_t *buffer;

// Allocate an AVFrame. structure
pFrameRGB=avcodec_alloc_frame();
if(pFrameRGB==NULL)
handle_error();

// Determine required buffer size and allocate buffer
numBytes=avpicture_get_size(PIX_FMT_RGB24, pCodecCtx->width,
pCodecCtx->height);
buffer=new uint8_t[numBytes];

// Assign appropriate parts of buffer to image planes in pFrameRGB
avpicture_fill((AVPicture *)pFrameRGB, buffer, PIX_FMT_RGB24,
pCodecCtx->width, pCodecCtx->height);
[/code]

11 结束了,需要清理一些我们开辟的资源
[code lang="c"]
// Free the RGB image
delete [] buffer;
av_free(pFrameRGB);

// Free the YUV frame
av_free(pFrame);

// Close the codec
avcodec_close(pCodecCtx);

// Close the video file
av_close_input_file(pFormatCtx);
[/code]

12 完整代码
[code lang="c"]
// avcodec_sample.cpp

// A small sample program that shows how to use libavformat and libavcodec to
// read video from a file.
//
// Use
//
// g++ -o avcodec_sample avcodec_sample.cpp -lavformat -lavcodec -lz
//
// to build (assuming libavformat and libavcodec are correctly installed on
// your system).
//
// Run using
//
// avcodec_sample myvideofile.mpg
//
// to write the first five frames from "myvideofile.mpg" to disk in PPM
// format.

#include <avcodec.h>
#include <avformat.h>

#include <stdio.h>

bool GetNextFrame(AVFormatContext *pFormatCtx, AVCodecContext *pCodecCtx,
int videoStream, AVFrame. *pFrame)
{
static AVPacket packet;
static int bytesRemaining=0;
static uint8_t *rawData;
static bool fFirstTime=true;
int bytesDecoded;
int frameFinished;

// First time we're called, set packet.data to NULL to indicate it
// doesn't have to be freed
if(fFirstTime)
{
fFirstTime=false;
packet.data=NULL;
}

// Decode packets until we have decoded a complete frame
while(true)
{
// Work on the current packet until we have decoded all of it
while(bytesRemaining > 0)
{
// Decode the next chunk of data
bytesDecoded=avcodec_decode_video(pCodecCtx, pFrame,
&frameFinished, rawData, bytesRemaining);

// Was there an error?
if(bytesDecoded < 0)
{
fprintf(stderr, "Error while decoding framen");
return false;
}

bytesRemaining-=bytesDecoded;
rawData+=bytesDecoded;

// Did we finish the current frame? Then we can return
if(frameFinished)
return true;
}

// Read the next packet, skipping all packets that aren't for this
// stream
do
{
// Free old packet
if(packet.data!=NULL)
av_free_packet(&packet);

// Read new packet
if(av_read_packet(pFormatCtx, &packet)<0)
goto loop_exit;
} while(packet.stream_index!=videoStream);

bytesRemaining=packet.size;
rawData=packet.data;
}

loop_exit:

// Decode the rest of the last frame
bytesDecoded=avcodec_decode_video(pCodecCtx, pFrame, &frameFinished,
rawData, bytesRemaining);

// Free last packet
if(packet.data!=NULL)
av_free_packet(&packet);

return frameFinished!=0;
}

void SaveFrame(AVFrame. *pFrame, int width, int height, int iFrame)
{
FILE *pFile;
char szFilename[32];
int y;

// Open file
sprintf(szFilename, "frame%d.ppm", iFrame);
pFile=fopen(szFilename, "wb");
if(pFile==NULL)
return;

// Write header
fprintf(pFile, "P6n%d %dn255n", width, height);

// Write pixel data
for(y=0; y<height; y++)
fwrite(pFrame->data[0]+y*pFrame->linesize[0], 1, width*3, pFile);

// Close file
fclose(pFile);
}

int main(int argc, char *argv[])
{
AVFormatContext *pFormatCtx;
int i, videoStream;
AVCodecContext *pCodecCtx;
AVCodec *pCodec;
AVFrame. *pFrame;
AVFrame. *pFrameRGB;
int numBytes;
uint8_t *buffer;

// Register all formats and codecs
av_register_all();

// Open video file
if(av_open_input_file(&pFormatCtx, argv[1], NULL, 0, NULL)!=0)
return -1; // Couldn't open file

// Retrieve stream information
if(av_find_stream_info(pFormatCtx)<0)
return -1; // Couldn't find stream information

// Dump information about file onto standard error
dump_format(pFormatCtx, 0, argv[1], false);

// Find the first video stream
videoStream=-1;
for(i=0; i<pFormatCtx->nb_streams; i++)
if(pFormatCtx->streams[i]->codec.codec_type==CODEC_TYPE_VIDEO)
{
videoStream=i;
break;
}
if(videoStream==-1)
return -1; // Didn't find a video stream

// Get a pointer to the codec context for the video stream
pCodecCtx=&pFormatCtx->streams[videoStream]->codec;

// Find the decoder for the video stream
pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
if(pCodec==NULL)
return -1; // Codec not found

// Inform. the codec that we can handle truncated bitstreams -- i.e.,
// bitstreams where frame. boundaries can fall in the middle of packets
if(pCodec->capabilities & CODEC_CAP_TRUNCATED)
pCodecCtx->flags|=CODEC_FLAG_TRUNCATED;

// Open codec
if(avcodec_open(pCodecCtx, pCodec)<0)
return -1; // Could not open codec

// Hack to correct wrong frame. rates that seem to be generated by some
// codecs
if(pCodecCtx->frame_rate>1000 && pCodecCtx->frame_rate_base==1)
pCodecCtx->frame_rate_base=1000;

// Allocate video frame
pFrame=avcodec_alloc_frame();

// Allocate an AVFrame. structure
pFrameRGB=avcodec_alloc_frame();
if(pFrameRGB==NULL)
return -1;

// Determine required buffer size and allocate buffer
numBytes=avpicture_get_size(PIX_FMT_RGB24, pCodecCtx->width,
pCodecCtx->height);
buffer=new uint8_t[numBytes];

// Assign appropriate parts of buffer to image planes in pFrameRGB
avpicture_fill((AVPicture *)pFrameRGB, buffer, PIX_FMT_RGB24,
pCodecCtx->width, pCodecCtx->height);

// Read frames and save first five frames to disk
i=0;
while(GetNextFrame(pFormatCtx, pCodecCtx, videoStream, pFrame))
{
img_convert((AVPicture *)pFrameRGB, PIX_FMT_RGB24, (AVPicture*)pFrame,
pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height);

// Save the frame. to disk
if(++i<=5)
SaveFrame(pFrameRGB, pCodecCtx->width, pCodecCtx->height, i);
}

// Free the RGB image
delete [] buffer;
av_free(pFrameRGB);

// Free the YUV frame
av_free(pFrame);

// Close the codec
avcodec_close(pCodecCtx);

// Close the video file
av_close_input_file(pFormatCtx);

return 0;
}
[/code]

13 几个相关及修订代码下载:
avcodec_sample.cpp.tar
avcodec_sample.0.5.0.c.tar
avcodec_sample.0.4.9-fixed.cpp.tar

14 参考文章
1 http://www.inb.uni-luebeck.de/~boehme/using_libavcodec.html
2 http://web.me.com/dhoerl/Home/Tech_Blog/Entries/2009/1/22_Revised_avcodec_sample.c.html
3 http://www.cryptosystem.org/archives/2006/03/libavcodec-libavformat-sample-code/


TAG:

 

评分:0

我来说两句

Open Toolbar