simple_ffmpeg_decoder(ffmpeg的解码器)

代码

#include <stdio.h>

#define __STDC_CONSTANT_MACROS
//因为ffmpeg是C语言编写的所以要继承C语言的语法格式
extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
};
int main(int argc,char* argv)
{
  AVFormatContext	*pFormatCtx;//音频格式相关信息
  AVCodecContext	*pCodecCtx;//编解码信息
  AVCodec			*pCodec;//编解码器 如H.264编码器和解码器
  AVFrame	*pFrame,*pFrameYUV;//用于存放解码后的音视频帧的数据结构;pFrame用于存储从视频流中解码得到的原始帧数据  pFrameYUV则用于存储将原始帧数据转换为YUV格式后的数据
  AVPacket *packet; //AVPacket结构体用于表示音视频数据包。它包含了编码后的音视频数据以及相关的元数据信息,如数据包大小、时间戳、持续时间等。
  int i, videoindex;
  int y_size;
  int ret, got_picture;
  struct SwsContext *img_convert_ctx;
  //输入文件路径
  char filepath[]="xxxx.ts";
  int frame_cnt ;//frame_cnt 是一个整数变量,用于统计已经解码的视频帧的数量。
  av_register_all();//旧版本中还有这个方法4.x 新版本中弃用了这个
  avformat_network_init();//初始化网络模块 初始化win socket和openssl
  pFormatCtx = avformat_alloc_context();//初始化音视频信息的上下文 返回值为AVFormatContext
//	int avformat_open_input(AVFormatContext * *ps, const char* url, AVInputFormat * fmt, AVDictionary * *options);
	//avformat_open_input函数用于打开指定路径的音频文件,并将文件的信息填充到之前的AVFormatContext结构体中
	//ps是一个指向AVFormatContext* 类型的指针的指针,用于传递要填充的AVFormatContext结构体指针的地址
	//url是要打开的音视频文件的路径
	//fmt是要使用的输入格式,通常为:NULL,表示自动检测输入格式
	//options:是一个指向选项字典的指针
  int avformat_open_input(&pFormatCtx,filepath,NULL,NULL!=2)
  {
    printf("Couldn't open input steram\r\n");
    return  -1;
  }
 	//函数用于获取音视频流的详细信息,比如流的类型、编解码器信息、时长等。它会解析音视频文件中的每个流,并将相关信息填充到AVFormatContext
	// ic是指向已打开 AVFormatContext结构体的指针,其中包含了音视频文件的格式信息。
	//options 是一个指向选项字典的指针,用于设置查找流信息时的选项。这里传入了NULL,表示不设置任何选项。
	if(avformat_find_stream_info(pFormatCtx,NULL)<0)
    {
		printf("Couldn't find stream information.\n");
		return -1;
	}
    videoindex=-1;//videoindex = -1;//初始化videoindex变量为-1这是一个标记,用于记录找到的视频流的索引
    //循环遍历pFormatCtx->nb_streams数组,该数组中存储了音视频文件中的所有流信息
	for(i=0; i<pFormatCtx->nb_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");//在循环中,判断当前流是否为视频流。如果是视频流,则将视频流,程序输出错误信息并返回-1
		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;
	}

  /*
	 * 在此处添加输出视频信息的代码
	 * 取自于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));
	//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;
	while(av_read_frame(pFormatCtx, packet)>=0){
		if(packet->stream_index==videoindex){
				/*
				 * 在此处添加输出H264码流的代码
				 * 取自于packet,使用fwrite()
				 */
			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()
				 */

				frame_cnt++;

			}
		}
		av_free_packet(packet);
	}

	sws_freeContext(img_convert_ctx);

	av_frame_free(&pFrameYUV);
	av_frame_free(&pFrame);
	avcodec_close(pCodecCtx);
	avformat_close_input(&pFormatCtx);
}
posted @ 2024-04-14 09:51  不会笑的孩子  阅读(2)  评论(0编辑  收藏  举报