使用ffmpeg 的liavformat 解封装,使用libavcodec 解codec,实现一个简单的解码器。
解码的流程,从数据结构上看就是AVFormatContext ->AVCodecContext -> AVPacket -> AVFrame。 生成AVPacket 为解封装,生成AVFrame 为解codec。其中,AVFormatContext:封装格式上下文结构体,也是统领全局的结构体,保存了视频文件 封装 格式相关信息;AVCodecContext:编码器上下文结构体,保存了视频(音频)编解码相关信息;AVPacket:存储一帧压缩编码数据;AVFrame:存储一帧解码后像素(采样)数据。
生成AVFrame 后,可以使用sws 进行 不同 AVFrame 的转换,比如将yuv420 转成 rgb。其中,sws 的api 主要有3个,分别是:
- sws_getContext()
- sws_scale()
- sws_freeContext()
这里,sws_getContext 对应的是初始化,sws_scale 对应的是转换,sws_freeContext 是释放函数。这里特别强调下sws_scale 函数的用法:
int sws_scale(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY, int srcSliceH, uint8_t* dst[], int dstStride[])
七个参数:
第一个参数即是由 sws_getContext 所取得的参数。
第二个 src 及第六个 dst 分別指向input 和 output 的 buffer。
第三个 srcStride 及第七个 dstStride 分別指向 input 及 output 的 stride;姑且可以先把它看成是每一列的 byte 數。
第四个 srcSliceY,是指第一列要处理的位置。
第五個srcSliceH指的是 source slice 的高度。
知道上面流程后,能容易实现一个简单的解码器,解码生成h264文件和yuv 文件。
下面是c 代码:
#include <stdio.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
//使用libformat & liavcodec 解码
int main(int argc, char* argv[])
{
AVFormatContext *pFormatCtx;
int i, index;
AVCodecContext *pCodecCtx;
AVCodec *pCodec;
AVFrame *pFrame,*pFrameYUV;
uint8_t *out_buffer;
AVPacket *packet;
int y_size;
int ret, got_picture;
struct SwsContext *img_convert_ctx;
// input & output init
char *filepath = "input.flv";
FILE *fp_yuv=fopen("output.yuv","wb+");
FILE *fp_h264=fopen("output.h264","wb+");
// 注册编码解码器
av_register_all();
// 初始化网络组件
avformat_network_init();
// 初始化 format
pFormatCtx = avformat_alloc_context();
//打开视频流
if(avformat_open_input(&pFormatCtx,filepath,NULL,NULL)!=0){
printf("Couldn't open input stream.\n");
return -1;
}
// 寻找视频流
if(avformat_find_stream_info(pFormatCtx,NULL)<0){
printf("Couldn't find stream information.\n");
return -1;
}
index = -1;
for(i=0; i < pFormatCtx->nb_streams; i++){
if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO){
index = i;
break;
}
}
if(index==-1){
printf("Didn't find a video stream.\n");
return -1;
}
// 获取源视频流的 codec
pCodecCtx=pFormatCtx->streams[index]->codec;
//查找解码器
pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
if(pCodec==NULL){
printf("Codec not found decodec.\n");
return -1;
}
//打开解码器
if(avcodec_open2(pCodecCtx, pCodec,NULL)<0){
printf("Could not open codec.\n");
return -1;
}
// 初始化AVFrame
pFrame=av_frame_alloc();
pFrameYUV=av_frame_alloc();
out_buffer=(uint8_t *)av_malloc(avpicture_get_size(AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height));
avpicture_fill((AVPicture *)pFrameYUV, out_buffer, AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height);
packet=(AVPacket *)av_malloc(sizeof(AVPacket));
//Output Info-----------------------------
printf("--------------- File Information ----------------\n");
av_dump_format(pFormatCtx,0,filepath,0);
printf("-------------------------------------------------\n");
// sws 初始化,设置源pixfmt 和目标pixfmt
img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt,
pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);
while(av_read_frame(pFormatCtx, packet)>=0){//读取一帧压缩数据
if(packet->stream_index == index){
fwrite(packet->data,1,packet->size,fp_h264); //把H264数据写入fp_h264文件
ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);//解码一帧压缩数据
if(ret < 0){
printf("Decode Error.\n");
return -1;
}
if(got_picture){
//PixelFormat 转化,转成yuv420
sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height,
pFrameYUV->data, pFrameYUV->linesize);
y_size = pCodecCtx->width * pCodecCtx->height;
fwrite(pFrameYUV->data[0],y_size,1,fp_yuv); //Y
fwrite(pFrameYUV->data[1],y_size/4,1,fp_yuv); //U
fwrite(pFrameYUV->data[2],y_size/4,1,fp_yuv); //V
printf("Succeed to decode 1 frame!\n");
}
}
av_free_packet(packet);
}
sws_freeContext(img_convert_ctx);
//关闭文件,释放内存
fclose(fp_yuv);
fclose(fp_h264);
av_frame_free(&pFrameYUV);
av_frame_free(&pFrame);
avcodec_close(pCodecCtx);
avformat_close_input(&pFormatCtx);
return 0;
}
linux 上安装ffmpeg 到/usr/lib 后,可以直接编译使用:
gcc decoder.c -g -o decoder.out -I /usr/local/include -L /usr/local/lib -lavformat -lavcodec -lavutil -lswscale