您的位置:首页 > 其它

最简单ffmpeg解码

2015-06-19 11:59 357 查看

1、概述

用ffmpeg解码的例子,把解码数据保存为yuv文件。

2、代码

/* 
 *最简单的FFmpeg的解码器
 *
 *缪国凯 Mickel
 *821486004@qq.com
 *
 *本程序实现了视频解码保存为yuv,写yuv是用文件方式写的,没用到muxer
 */

#include "stdafx.h"

#ifdef	__cplusplus
extern "C"
{
#endif
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
//#include "libavfilter/avfiltergraph.h"
//#include "libavfilter/avcodec.h"
//#include "libavfilter/buffersink.h"
//#include "libavfilter/buffersrc.h"
//#include "libavutil/avutil.h"
//#include "libavutil/opt.h"
//#include "libavutil/pixdesc.h"

#pragma comment(lib, "avcodec.lib")
#pragma comment(lib, "avformat.lib")
#pragma comment(lib, "avutil.lib")
//#pragma comment(lib, "avdevice.lib")
//#pragma comment(lib, "avfilter.lib")
//#pragma comment(lib, "postproc.lib")
//#pragma comment(lib, "swresample.lib")
//#pragma comment(lib, "swscale.lib")
#ifdef __cplusplus
};
#endif

***FormatContext *ifmt_ctx = NULL;

#include <stdio.h>

int openinputfile(const char* filename)
{
	int ret = 0;
	//open the input
	if ((ret = avformat_open_input(&ifmt_ctx, filename, NULL, NULL)) < 0)
	{
		printf("can not open input");
		return ret;
	}
	if ((ret = avformat_find_stream_info(ifmt_ctx, NULL)))
	{
		printf("can not find input stream info");
		return ret;
	}

	//open the decoder
	for (int i = 0; i < ifmt_ctx->nb_streams; i++)
	{
		if (ifmt_ctx->streams[i]->codec->codec_type == ***MEDIA_TYPE_VIDEO)
		{
			ret = avcodec_open2(ifmt_ctx->streams[i]->codec, 
				avcodec_find_decoder(ifmt_ctx->streams[i]->codec->codec_id), NULL);

			if (ret < 0)
			{
				printf("can not open decoder");
				return ret;
			}

		}
	}

	return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
	if (argc < 2)
	{
		return -1;
	}
	***Packet pkt_in, pkt_out;
	***Frame *frame = NULL;
	unsigned int stream_index;
	av_register_all();

	if (openinputfile(argv[1]) < 0)
	{
		printf("failed to open input file");
		goto end;
	}

	FILE *p = NULL;
	p = fopen("test.yuv", "a+b");

	while(1)
	{
		if (av_read_frame(ifmt_ctx, &pkt_in) < 0)
		{
			break;
		}
		pkt_out.data = NULL;
		pkt_out.size = 0;
		av_init_packet(&pkt_out);
		stream_index = pkt_in.stream_index;
		frame = av_frame_alloc();
		int got_frame = -1;
		int ret = -1;

		if (ifmt_ctx->streams[stream_index]->codec->codec_type == ***MEDIA_TYPE_VIDEO)
		{
			ret = avcodec_decode_video2(ifmt_ctx->streams[stream_index]->codec, frame, &got_frame, &pkt_in);

			if (ret < 0)
			{
				av_frame_free(&frame);
				printf("decoding video stream failed\n");
				break;
			}

			if (got_frame)
			{
				if (NULL != p)
				{
					int j;
					int height = ifmt_ctx->streams[stream_index]->codec->height;
					int width = ifmt_ctx->streams[stream_index]->codec->width;
					for(j=0; j<height; j++)
						fwrite(frame->data[0] + j * frame->linesize[0], 1, width, p);  
					for(j=0; j<height/2; j++)  
						fwrite(frame->data[1] + j * frame->linesize[1], 1, width/2, p);  
					for(j=0; j<height/2; j++)  
						fwrite(frame->data[2] + j * frame->linesize[2], 1, width/2, p);
				}
			}
		}
	}
	fclose(p);
end:
	avformat_close_input(&ifmt_ctx);

	getchar();
	return 0;
}

3、工程下载地址

http://download.csdn.net/detail/dancing_night/8822057
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: