FFmpeg + SDL的视频播放器的制作(3)
ffmpeg解码的函数和数据结构
实例程序运行:simplest_ffmpeg_decoder.cpp
/**
* 最简单的基于FFmpeg的解码器
* Simplest FFmpeg Decoder
*
* 雷霄骅 Lei Xiaohua
* [email protected]
* 中国传媒大学/数字电视技术
* Communication University of China / Digital TV Technology
* http://blog.csdn.net/leixiaohua1020
*
* 本程序实现了视频文件的解码(支持HEVC,H.264,MPEG2等)。
* 是最简单的FFmpeg视频解码方面的教程。
* 通过学习本例子可以了解FFmpeg的解码流程。
* This software is a simplest video decoder based on FFmpeg.
* Suitable for beginner of FFmpeg.
*
*/
#include <stdio.h>
#define __STDC_CONSTANT_MACROS
extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
};
int main(int argc, char* argv[])
{
AVFormatContext *pFormatCtx;
int i, videoindex;
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;
//输入文件路径
char filepath[]="Titanic.ts";
int frame_cnt;
av_register_all();//注册所有组件
avformat_network_init();
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;
}
videoindex=-1;
for(i=0; i<pFormatCtx->nb_streams; i++)//nb_streams专门记录有多少个stream(音频还是视频),遍历nb_streams
if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO){//判断avstream是音频还是视频
videoindex=i;//视频所在的avstream流的序号
break;
}
if(videoindex==-1){
printf("Didn't find a video stream.\n");
return -1;
}
pCodecCtx=pFormatCtx->streams[videoindex]->codec;
pCodec=avcodec_find_decoder(pCodecCtx->codec_id);//找出 查找对应的解码器
if(pCodec==NULL){
printf("Codec not found.\n");
return -1;
}
if(avcodec_open2(pCodecCtx, pCodec,NULL)<0){//打开解码器
printf("Could not open codec.\n");
return -1;
}
FILE *fp = fopen("info.txt", "wb+");//以读写的方式打开一个文件info.txt二进制文件
fprintf(fp, "shichang: %d\n", pFormatCtx->duration);//获取AVFormatContext里面的视频时长duration(微妙为单位)
fprintf(fp, "fengzhuanggeshi: %s\n", pFormatCtx->iformat->name);//AVFormatContext是所有数据的来源
fprintf(fp, "kuangao:%d*%d\n", pFormatCtx->streams[videoindex]->codec->width, pFormatCtx->streams[videoindex]->codec->height);//AVStream图像的宽高
printf("shichang: %d\n", pFormatCtx->duration);//获取AVFormatContext里面的视频时长duration(微妙为单位)
printf("fengzhuanggeshi: %s\n", pFormatCtx->iformat->name);//AVFormatContext是所有数据的来源
printf("kuangao:%d*%d\n", pFormatCtx->streams[videoindex]->codec->width, pFormatCtx->streams[videoindex]->codec->height);//AVStream图像的宽高
fclose(fp);//关闭文件
/*
* 在此处添加输出视频信息的代码
* 取自于pFormatCtx,使用fprintf()
*/
pFrame=av_frame_alloc();
pFrameYUV=av_frame_alloc();
out_buffer=(uint8_t *)av_malloc(avpicture_get_size(PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height));
avpicture_fill((AVPicture *)pFrameYUV, out_buffer, PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height);
packet=(AVPacket *)av_malloc(sizeof(AVPacket));//AVPacket,里面是H.264。(AVFrame里面是YUV)
//Output Info-----------------------------
printf("--------------- File Information ----------------\n");
av_dump_format(pFormatCtx,0,filepath,0);
printf("-------------------------------------------------\n");
img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt,
pCodecCtx->width, pCodecCtx->height, PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);
FILE *fp_264 = fopen("test264.h264", "wb+");//解码前的H.264码流数据
FILE *fp_yuv = fopen("testyuv.yuv", "wb+");//解码后的YUV像素数据
frame_cnt=0;
while(av_read_frame(pFormatCtx, packet)>=0){//读取一帧的视频的数据
if(packet->stream_index==videoindex){//有没有读取到视频流?是否读取到末尾?
/*
* 在此处添加输出H264码流的代码
* 取自于packet,使用fwrite()
*/
fwrite(packet->data, 1, packet->size, fp_264);//解码前的H.264码流数据
ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);//核心函数,解码一帧压缩数据,会生成AVFrame。(AVFrame里面是YUV)
if(ret < 0){
printf("Decode Error.\n");
return -1;
}
if(got_picture){
//sws_scale裁减右边的那块
sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height,
pFrameYUV->data, pFrameYUV->linesize);
printf("Decoded frame index: %d\n",frame_cnt);
/*
* 在此处添加输出YUV的代码
* 取自于pFrameYUV,使用fwrite()
*/
//YUV420
//pFrameYUV是经过sws_scale裁减后的YUV
fwrite(pFrameYUV->data[0], 1, pCodecCtx->width*pCodecCtx->height, fp_yuv);//Y数据
//U,V数据量只有Y数据的四分之一
//色差信号U,V的取样频率为亮度信号取样频率的四分之一,在水平方向和垂直方向上的取样点数均为Y的一半,因此UV的分辨率是Y的1/4
fwrite(pFrameYUV->data[1], 1, pCodecCtx->width*pCodecCtx->height/4, fp_yuv);//U数据
fwrite(pFrameYUV->data[2], 1, pCodecCtx->width*pCodecCtx->height/4, fp_yuv);//V数据
frame_cnt++;
}
}
av_free_packet(packet);
}
fclose(fp_264);
fclose(fp_yuv);
sws_freeContext(img_convert_ctx);
av_frame_free(&pFrameYUV);
av_frame_free(&pFrame);
avcodec_close(pCodecCtx);//关闭解码器
avformat_close_input(&pFormatCtx);//关闭输入视频文件
return 0;
}