#if 1
/*** 最简单的基于FFmpeg的视频解码器(纯净版)* Simplest FFmpeg Decoder Pure** 雷霄骅 Lei Xiaohua* leixiaohua1020@126.com* 中国传媒大学/数字电视技术* Communication University of China / Digital TV Technology* http://blog.csdn.net/leixiaohua1020*** 本程序实现了视频码流(支持HEVC,H.264,MPEG2等)解码为YUV数据。* 它仅仅使用了libavcodec(而没有使用libavformat)。* 是最简单的FFmpeg视频解码方面的教程。* 通过学习本例子可以了解FFmpeg的解码流程。* This software is a simplest decoder based on FFmpeg.* It decode bitstreams to YUV pixel data.* It just use libavcodec (do not contains libavformat).* Suitable for beginner of FFmpeg.*/#include <stdio.h>#define __STDC_CONSTANT_MACROS#ifdef _WIN32
//Windows
extern "C"
{
#include "libavcodec/avcodec.h"
};
#else
//Linux...
#ifdef __cplusplus
extern "C"
{
#endif
#include "avcodec.h"
#ifdef __cplusplus
};
#endif
#endif//test different codec
#define TEST_H264  1
#define TEST_HEVC  0int simplest_ffmpeg_decoder_pure(int argc, char* argv[])
{AVCodec *pCodec;AVCodecContext *pCodecCtx= NULL;AVCodecParserContext *pCodecParserCtx=NULL;FILE *fp_in;FILE *fp_out;AVFrame  *pFrame;const int in_buffer_size=4096;unsigned char in_buffer[in_buffer_size + FF_INPUT_BUFFER_PADDING_SIZE];unsigned char *cur_ptr;int cur_size;AVPacket packet;int ret, got_picture;#if TEST_HEVCenum AVCodecID codec_id=AV_CODEC_ID_HEVC;char filepath_in[]="bigbuckbunny_480x272.hevc";
#elif TEST_H264enum AVCodecID codec_id=AV_CODEC_ID_H264;char filepath_in[]="bigbuckbunny_480x272.h264";
#elseenum AVCodecID codec_id=AV_CODEC_ID_MPEG2VIDEO;char filepath_in[]="bigbuckbunny_480x272.m2v";
#endifchar filepath_out[]="bigbuckbunny_480x272.yuv";int first_time=1;//av_log_set_level(AV_LOG_DEBUG);avcodec_register_all();pCodec = avcodec_find_decoder(codec_id);if (!pCodec) {printf("Codec not found\n");return -1;}pCodecCtx = avcodec_alloc_context3(pCodec);if (!pCodecCtx){printf("Could not allocate video codec context\n");return -1;}pCodecParserCtx=av_parser_init(codec_id);if (!pCodecParserCtx){printf("Could not allocate video parser context\n");return -1;}//if(pCodec->capabilities&CODEC_CAP_TRUNCATED)//    pCodecCtx->flags|= CODEC_FLAG_TRUNCATED; if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {printf("Could not open codec\n");return -1;}//Input Filefp_in = fopen(filepath_in, "rb");if (!fp_in) {printf("Could not open input stream\n");return -1;}//Output Filefp_out = fopen(filepath_out, "wb");if (!fp_out) {printf("Could not open output YUV file\n");return -1;}pFrame = av_frame_alloc();av_init_packet(&packet);while (1) {cur_size = fread(in_buffer, 1, in_buffer_size, fp_in);if (cur_size == 0)break;cur_ptr=in_buffer;while (cur_size>0){int len = av_parser_parse2(pCodecParserCtx, pCodecCtx,&packet.data, &packet.size,cur_ptr , cur_size ,AV_NOPTS_VALUE, AV_NOPTS_VALUE, AV_NOPTS_VALUE);cur_ptr += len;cur_size -= len;if(packet.size==0)continue;//Some Info from AVCodecParserContextprintf("[Packet]Size:%6d\t",packet.size);switch(pCodecParserCtx->pict_type){case AV_PICTURE_TYPE_I: printf("Type:I\t");break;case AV_PICTURE_TYPE_P: printf("Type:P\t");break;case AV_PICTURE_TYPE_B: printf("Type:B\t");break;default: printf("Type:Other\t");break;}printf("Number:%4d\n",pCodecParserCtx->output_picture_number);ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, &packet);if (ret < 0) {printf("Decode Error.\n");return ret;}if (got_picture) {if(first_time){printf("\nCodec Full Name:%s\n",pCodecCtx->codec->long_name);printf("width:%d\nheight:%d\n\n",pCodecCtx->width,pCodecCtx->height);first_time=0;}//Y, U, Vfor(int i=0;i<pFrame->height;i++){fwrite(pFrame->data[0]+pFrame->linesize[0]*i,1,pFrame->width,fp_out);}for(int i=0;i<pFrame->height/2;i++){fwrite(pFrame->data[1]+pFrame->linesize[1]*i,1,pFrame->width/2,fp_out);}for(int i=0;i<pFrame->height/2;i++){fwrite(pFrame->data[2]+pFrame->linesize[2]*i,1,pFrame->width/2,fp_out);}printf("Succeed to decode 1 frame!\n");}}}//Flush Decoderpacket.data = NULL;packet.size = 0;while(1){ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, &packet);if (ret < 0) {printf("Decode Error.\n");return ret;}if (!got_picture){break;}else {//Y, U, Vfor(int i=0;i<pFrame->height;i++){fwrite(pFrame->data[0]+pFrame->linesize[0]*i,1,pFrame->width,fp_out);}for(int i=0;i<pFrame->height/2;i++){fwrite(pFrame->data[1]+pFrame->linesize[1]*i,1,pFrame->width/2,fp_out);}for(int i=0;i<pFrame->height/2;i++){fwrite(pFrame->data[2]+pFrame->linesize[2]*i,1,pFrame->width/2,fp_out);}printf("Flush Decoder: Succeed to decode 1 frame!\n");}}fclose(fp_in);fclose(fp_out);av_parser_close(pCodecParserCtx);av_frame_free(&pFrame);avcodec_close(pCodecCtx);av_free(pCodecCtx);return 0;
}#endif

simplest_ffmpeg_decoder_pure.c相关推荐

  1. 雷霄骅开源视音频项目汇总

    from: http://blog.csdn.net/leixiaohua1020/article/details/42658139# 作者:雷霄骅, 一个值得怀念的人 本文汇总一下自己视音频编解码学 ...

  2. FFmpeg示例程序合集-批量编译脚本

    此前做了一系列有关FFmpeg的示例程序,组成了< 最简单的FFmpeg示例程序合集>,其中包含了如下项目: simplest ffmpeg player:                 ...

  3. 最简单的基于FFMPEG+SDL的视频播放器:拆分-解码器和播放器

    ===================================================== 最简单的基于FFmpeg的视频播放器系列文章列表: 100行代码实现最简单的基于FFMPEG ...

  4. FFMPEG开源音视频项目学习汇总

    ~非常感谢雷霄骅老师的无私帮助,本文转载自:http://blog.csdn.net/leixiaohua1020/article/details/42658139~       本文汇总一下自己视音 ...

  5. 最简单的基于FFmpeg的解码器-纯净版(不包含libavformat)

    ===================================================== 最简单的基于FFmpeg的视频播放器系列文章列表: 100行代码实现最简单的基于FFMPEG ...

  6. 音视频相关研究-英年早逝的雷宵骅在读博士的项目

    CSDN的老朋友,博主雷霄骅,于2016年7月17日去世,时年26岁,CSDN博客排名第52位.两届CSDN博客之星.微软MVP.中国传媒大学通信与信息系统专业在读博士.主要从事与广播电视有关的视音频 ...

  7. 我的开源视音频项目汇总

    本文汇总一下自己视音频编解码学习方面的开源项目.这些开源项目大体上可以分成专业领域程序,FFmpeg示例程序,FFmpeg移植程序,多媒体项目示例程序,视音频编解码原理学习工程几个类别.这些类别的详细 ...

最新文章

  1. 设置显示VSCode的修改历史Local History,方便多人开发的时候快速查看谁修改了代码(也可以防止误删代码文件)
  2. 区块链技术产生数字货币时代
  3. 基于DL的计算机视觉方法热度高涨,传统方法就可以偏废?
  4. 大数据开发套件—调度运维常见问题
  5. redis3.2的安装和使用
  6. C# Linq 查询数据库(DataSet)生成 Tree
  7. KFold、StratifiedKFold、GroupKFold的区别
  8. 为员工长工资。从最低工资调起每人长10%,请计算长工资的人数和长工资后的工资总额,并输出输出长工资人数及工资总额。
  9. 【OpenCV 例程200篇】47. 图像增强—直方图匹配
  10. 基于JAVA+SpringMVC+Mybatis+MYSQL的车库管理系统
  11. autocad支持python吗_利用python控制Autocad:pyautocad方式
  12. 常用的数据库维护语句
  13. Windows Server 2012/2012R2 中配置 MSDTC,令其使用特定端口
  14. 中国气象站点原数据集(1942-2022年3月)
  15. 转:C# 中 MSCHART 饼状图显示百分比
  16. PyTorch 1.x 常用知识
  17. QT自定义控件设置文本过长时显示点点点……
  18. 以计算机的发展写一篇英语作文,写一篇关于网络的英语作文
  19. PMP考试资料考试重点,不看可惜了
  20. 亿美软通 短信接口整合(JAVA)

热门文章

  1. java403forbidden_Spring Cloud出现Options Forbidden 403问题解决方法
  2. zmud之自动解谜:不用数据库实现自动解谜的原理。
  3. 51单片机——LCD1602液晶屏 C语言入门编程
  4. AD高级培训PPT总结
  5. 使用-OB-ODC连接OceanBase数据库与模拟数据功能使用
  6. lamp mysql 密码_lamp 如何修改mysql密码
  7. python爬虫爬取下厨房食谱,周末聚餐真的停不下来
  8. 分享一个商品历史价格查询的网站
  9. linux终端如何分栏,动态分栏布局实现
  10. 笔记本计算机无法启动怎么解决,笔记本开机进不了系统,教您笔记本开机无法进入系统怎么办...