学习的第一个FFmpeg-demo---解封装且解码视频后存储为yuv和h.264文件

在大四保研时,接到了一个任务,从最底层书写AVI格式的解封装,并提取YUV数据,再由公式计算对应值。

依稀记得那是一个10bit的avi超大视频,编码是m102和另一个种(保密),因为是从底层写起,所以并没有想太多,就用最基本的的c语言一句一句完成,后来还遇到很多Bug.

听雷博士(http://blog.csdn.net/leixiaohua1020)介绍了关于他关于ffmpeg的研究后,打算尝试着玩玩ffmpeg这个东东。以下代码是来之雷博士小学期课程的公开代码。我对其作了部分注释,如有错误,请指出~


#include 

#define __STDC_CONSTANT_MACROS

extern "C"  //由于ffmpeng是c为底层语言,所以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;//h.264由多个packet包组成
	int y_size;
	int ret, got_picture;
	struct SwsContext *img_convert_ctx;//为视频缩放作准备的变量
	//输入文件路径
	//char filepath[]="Titanic.ts";
	char filepath[]="Titanic.ts";
	
	int frame_cnt;

	av_register_all();// 初始化 libavformat和注册所有的muxers、demuxers和protocols
	avformat_network_init();
	pFormatCtx = avformat_alloc_context();

	//打开文件,1:格式内容(最主要的格式)2:打开视频路径
	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; inb_streams; i++) 
		if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO){
			videoindex=i;
			break;
		}
	if(videoindex==-1){
		printf("Didn't find a video stream.\n");
		return -1;
	}
	
	/*
	* 根据视频索引号查找解码
	* 描述编解码器上下文的数据结构
	*/
	pCodecCtx=pFormatCtx->streams[videoindex]->codec;
	/*
	* 根据解码器id,找到解码器数据
	*/
	pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
	if(pCodec==NULL){
		printf("Codec not found.\n");
		return -1;
	}
	/*
	* 使用给的AVCode去初始化一个视音频编解码器的AVCodecContext
	*/
	if(avcodec_open2(pCodecCtx, pCodec,NULL)<0){
		printf("Could not open codec.\n");
		return -1;
	}
	/*
	 * 在此处添加输出视频信息的代码
	 * 取自于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));
	//建立picture数据
	avpicture_fill((AVPicture *)pFrameYUV, out_buffer, 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");
	img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, 
		pCodecCtx->width, pCodecCtx->height, PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL); 

	frame_cnt=0;	
	//FILE *f_h264;
	//f_h264=fopen("test_264.h264","wb+");
	FILE * f_hyuv=fopen("test_yuv.yuv","wb+");
	//循环读取h.264的每一个包
	while(av_read_frame(pFormatCtx, packet)>=0){
		//每个packet包的标志符或视频或音频
		if(packet->stream_index==videoindex){
				/*
				 * 在此处添加输出H264码流的代码
				 * 取自于packet,使用fwrite()
				 */

			//fwrite(packet->data,1,packet->size,f_h264);
			//从包中解码出数据,存储到帧变量中,如果由视频画面数据,got_picture就会非0
			ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);
			if(ret < 0){
				printf("Decode Error.\n");
				return -1;
			}
			if(got_picture){
				//由于解码出的画面宽比实际多出一些,需要裁剪掉
				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()
				 */
					fwrite(pFrameYUV->data[0],pCodecCtx->width*pCodecCtx->height,1,f_hyuv);
					fwrite(pFrameYUV->data[1],pCodecCtx->width*pCodecCtx->height/4,1,f_hyuv);
					fwrite(pFrameYUV->data[2],pCodecCtx->width*pCodecCtx->height/4,1,f_hyuv);
				frame_cnt++;

			}
		}
		av_free_packet(packet);
	}
	//fclose(f_h264);
	fclose(f_hyuv);
	sws_freeContext(img_convert_ctx);

	av_frame_free(&pFrameYUV);
	av_frame_free(&pFrame);
	avcodec_close(pCodecCtx);
	avformat_close_input(&pFormatCtx);

	return 0;
}


你可能感兴趣的:(c++常用总结小代码,FFmpeg)