使用FFMPEG解析H264編碼爲YUV格式

頭文件

#pragma once

#ifndef _VIDEO_DECODING_HEADER_
#define _VIDEO_DECODING_HEADER_

#define INBUF_SIZE 4096
#define AUDIO_INBUF_SIZE 20480
#define AUDIO_REFILL_THRESH 4096

extern "C"
{
#include "libavutil/opt.h"
#include "libavcodec/avcodec.h"
#include "libavutil/channel_layout.h"
#include "libavutil/common.h"
#include "libavutil/imgutils.h"
#include "libavutil/mathematics.h"
#include "libavutil/samplefmt.h"
}

/*************************************************
Struct:			CodecCtx
Description:	FFMpeg編解碼器上下文
*************************************************/
typedef struct
{
	AVCodec			*pCodec;				//編解碼器實例指針
	AVCodecContext	*pCodecContext;			//編解碼器上下文,指定了編解碼的參數
	AVCodecParserContext *pCodecParserCtx;	//編解碼解析器,從碼流中截取完整的一個NAL Unit數據

	AVFrame			*frame;					//封裝圖像對象指針
	AVPacket		pkt;					//封裝碼流對象實例
} CodecCtx;

#endif

cpp文件

/*************
解碼主要步驟:
1.解析輸入參數,獲取待解碼的碼流數據
2.初始化相應的FFMpeg結構
3.循環讀取並解析輸入碼流數據--由2進制碼流解析爲FFMpeg可以處理的包數據
4.解碼解析出的包爲像素數據
5.寫出像素數據
6.收尾工作
*************/

#include<stdio.h>
#include<stdint.h>
#include"VideoDecodingHeader.h"

#define INBUF_SIZE 4096   //接受區域大小


FILE* pFin = NULL;
FILE* pFout = NULL;

AVCodec* pCodec = NULL;
AVCodecContext* pCodecContext = NULL;
AVCodecParserContext* pCodecParserCtx = NULL;   //碼流解碼供解碼器使用的包

AVFrame* frame = NULL;
AVPacket pkt;



static int open_input_output_file(char **argv)
{
	const char* inputFileName = argv[1];
	const char* outputFileName = argv[2];
	//fopen_s 以二進制只讀的方式打開inputFileName,返回FILE指針
	fopen_s(&pFin, inputFileName, "rb+");
	if (!pFin)
	{
		printf("Error: open input file failed.\n");
		return -1;
	}
	fopen_s(&pFout, outputFileName, "wb+");
	if (!pFout)
	{
		printf("Error: open input file failed.\n");
		return -1;
	}
	return 0;
}
//打開解碼器且初始化
static int open_decoder()
{
	//註冊組件
	avcodec_register_all();

	//初始化最後輸出的pkt
	av_init_packet(&pkt);

	//查找解碼器
	pCodec = avcodec_find_decoder(AV_CODEC_ID_H264);
	if (!pCodec)
	{
		return -1;
	}

	pCodecContext = avcodec_alloc_context3(pCodec);
	if (!pCodecContext)
	{
		return -1;
	}

	if (pCodec->capabilities & AV_CODEC_CAP_TRUNCATED)
	{
		pCodecContext->flags |= AV_CODEC_CAP_TRUNCATED;
	}

	//初始化解析器
	pCodecParserCtx = av_parser_init(AV_CODEC_ID_H264);
	if (!pCodecParserCtx)
	{
		printf("Error:alloc parser failed.\n");
		return -1;
	}

	//打開解碼器
	if (avcodec_open2(pCodecContext, pCodec, NULL) < 0)
	{
		printf("Error:open condec failed.\n");
		return -1;
	}

	//開闢frame空間
	frame = av_frame_alloc();
	if (!frame)
	{
		printf("Error:alloc frame failed.\n");
		return -1;
	}

	return 0;

}
//寫出YUV數據
static void write_out_yuv_frame(AVFrame *frame)
{
	uint8_t **pBuf = frame->data;//保存地址
	int*	pStride = frame->linesize;//保存位寬

	for (int color_idx = 0; color_idx < 3; color_idx++)
	{
		int		nWidth = color_idx == 0 ? frame->width : frame->width / 2;
		int		nHeight = color_idx == 0 ? frame->height : frame->height / 2;
		for (int idx = 0; idx < nHeight; idx++)
		{
			fwrite(pBuf[color_idx], 1, nWidth, pFout);
			pBuf[color_idx] += pStride[color_idx];
		}
		fflush(pFout);
	}
}
//收尾工作
static void Close()
{
	fclose(pFin);
	fclose(pFout);

	avcodec_close(pCodecContext);
	av_free(pCodecContext);
	av_frame_free(&frame);

}


int main(int argc, char **argv)
{
	//從輸入文件讀取碼流數據保存到的緩存位置
	uint8_t inbuf[INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
	
	if (open_input_output_file(argv) < 0)
	{
		return -1;
	}
	else
	{
		printf("Open input/ouput file succeed\n");
	}
	if (open_decoder() < 0)
	{
		return -1;
	}
	else
	{
		printf("Open decoder file succeed\n");
	}
	
	//解碼循環
	int uDataSize = 0, len = 0;
	int got_frame = 0;
	uint8_t* pDataPtr = NULL;
	while (true)
	{
		uDataSize = fread_s(inbuf, INBUF_SIZE, 1, INBUF_SIZE, pFin);
		if (uDataSize == 0)
		{
			break;
		}
		pDataPtr = inbuf;
		while (uDataSize>0)
		{
			len = av_parser_parse2(pCodecParserCtx, pCodecContext,
				&pkt.data, &pkt.size,
				pDataPtr, uDataSize, AV_NOPTS_VALUE, AV_NOPTS_VALUE, AV_NOPTS_VALUE);
			pDataPtr += len;
			uDataSize -= len;
			//pkt.size==0沒有解析完
			if (pkt.size == 0)
			{
				continue;
			}
			//!=0成功解析出一個packet的碼流
			printf("Parse 1 packet.\n");

			int ret = avcodec_decode_video2(pCodecContext, frame, &got_frame, &pkt);
			if (ret < 0)
			{
				printf("Error: decode failed.\n");
				return -1;
			}

			if (got_frame)
			{
				printf("Decoded 1 frame OK! Width x Height: (%d x %d)\n", frame->width, frame->height);
				write_out_yuv_frame(frame);
			}
			else
			{
				break;
			}
		 }
	}

	pkt.data = NULL;
	pkt.size = 0;
	while (true)
	{
		int ret = avcodec_decode_video2(pCodecContext, frame, &got_frame, &pkt);
		if (ret < 0)
		{
			printf("Error: decode failed.\n");
			return -1;
		}
		if (got_frame)
		{
			printf("Flush Decoded 1 frame OK! Width x Height: (%d x %d)\n", frame->width, frame->height);
			write_out_yuv_frame(frame);
		}

	 }

	Close();

	
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章