FFmpeg相信流媒体开发者的都不陌生,咬文嚼字就不凑字数了,耽误大家时间。最近在各方资源里听到甚至专业人士说”FFmpeg是做音视频编解码的“,说的太狭隘了,了解FFmpeg的一般也都知道VLC,VLC是一套以FFmpeg为基础的完整流媒体解决方案,行内称:VLC是汽车,FFMpeg就是发动机”,这就很形象的描述了FFmpeg的功能。FFmpeg是集音视频采集、音视频演示数据处理、解复用、编码解码、渲染等完整流媒体框架,内合入ffplay可实现渲染播放。webrtc是功能上与之并驾齐驱的一套架构,它合入一些新的编码标准,如音频的OPUS标准,SIP信令协议等,后续再以webrtc为框架写一些blog。

不说废话,先上代码。程序中将视频流decode成YUV420P格式,合入SDL2渲染播放。虽然是完整代码,目的为学术交流和技术共享,无意培养伸手党。代码中解决ffmpeg中新老接口对接,其它都是按decode的一般流程解码,如有不妥之处,欢迎指正。技术交流群:479245235,欢迎各界精英论道。

#include "stdio.h"
#include "iostream"
#include "stdlib.h"
#include <string.h>
#include <math.h>
#include <chrono> //C++11时间类标准模板库#ifdef _WIN32
//Windows
extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "libavfilter/avfilter.h"
#include "libavutil/imgutils.h"
#include "libavutil/fifo.h"
#include "libavutil/samplefmt.h"
#include "libavutil/time.h"
#include "libavutil/timestamp.h"
#include "libavutil/avassert.h"
#include "libavutil/channel_layout.h"
#include "libavutil/mathematics.h"
#include "libavutil/opt.h"
#include "libswresample/swresample.h"
#include "SDL2\SDL.h"
};
#else
//Linux...
#ifdef __cplusplus
extern "C"
{
#endif
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libavutil/imgutils.h>
#include <libswresample/swresample.h>
#include <SDL2/SDL.h>
#ifdef __cplusplus
};
#endif
#endifusing namespace std;#define  _CRT_SECURE_NO_WARNINGSint main()
{const char *fname;char errbuf[256] = { 0 };int iRes = 0;int vindex = -1;AVFormatContext *fctx = NULL;AVCodecContext *cctx = NULL;AVCodec *c = NULL;AVPacket *pkt = NULL;AVFrame *fr = NULL;AVFrame *yuv = NULL;uint8_t *buf = NULL;int vsize;struct SwsContext *imgCtx = NULL;SDL_Window *sw = NULL;SDL_Renderer *sr = NULL;SDL_Texture *ste = NULL;SDL_Rect srect = { 0 };//   av_register_all();  //ffmpeg 4.0 After noif (SDL_Init(SDL_INIT_VIDEO) != 0){cout << "SDL init failed!" << endl;return -1;}fctx = avformat_alloc_context();if ((iRes = avformat_open_input(&fctx, fname, NULL, NULL)) != 0){cout << "File open failed!" << endl;return -1;}if (avformat_find_stream_info(fctx, NULL) < 0){cout << "Stream find failed!\n";return -1;}av_dump_format(fctx, -1, fname, NULL);for (int i = 0; i < fctx->nb_streams; i++){if (fctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)vindex = i;}if (vindex == -1){cout << "Codec find failed!" << endl;return -1;}cctx = avcodec_alloc_context3(NULL);if (avcodec_parameters_to_context(cctx, fctx->streams[vindex]->codecpar) < 0){cout << "Copy stream failed!" << endl;return -1;}c = avcodec_find_decoder(cctx->codec_id);if (!c) {cout << "Find Decoder failed!" << endl;return -1;}if (avcodec_open2(cctx, c, NULL) != 0) {cout << "Open codec failed!" << endl;return -1;}sw = SDL_CreateWindow("video", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 680, 540, SDL_WINDOW_OPENGL);sr = SDL_CreateRenderer(sw, -1, 0);ste = SDL_CreateTexture(sr, SDL_PIXELFORMAT_IYUV, SDL_TEXTUREACCESS_STREAMING, cctx->width, cctx->height);if (!sw || !sr || !ste) {cout << "Create SDL windows failed!" << endl;return -1;}srect.w = cctx->width;srect.h = cctx->height;imgCtx = sws_getContext(cctx->width, cctx->height, cctx->pix_fmt, cctx->width, cctx->height, AV_PIX_FMT_YUV420P,SWS_BICUBIC, NULL, NULL, NULL);if (!imgCtx) {cout << "Get swscale context failed!" << endl;return -1;}pkt = av_packet_alloc();fr = av_frame_alloc();yuv = av_frame_alloc();vsize = av_image_get_buffer_size(AV_PIX_FMT_YUV420P, cctx->width, cctx->height, 1);buf = (uint8_t *)av_malloc(vsize);av_image_fill_arrays(yuv->data, yuv->linesize,buf, AV_PIX_FMT_YUV420P, cctx->width, cctx->height, 1);while (av_read_frame(fctx, pkt) >= 0) {if (pkt->stream_index == vindex) {if ((iRes = avcodec_send_packet(cctx, pkt)) != 0){cout << "Send video stream packet failed!" << endl;av_strerror(iRes, errbuf, 256);return -5;}if ((iRes = avcodec_receive_frame(cctx, fr)) != 0){cout << "Receive video frame failed!\n";av_strerror(iRes, errbuf, 256);return -6;}sws_scale(imgCtx, fr->data, fr->linesize, 0, cctx->height, yuv->data, yuv->linesize);SDL_UpdateTexture(ste, &srect, yuv->data[0], yuv->linesize[0]);SDL_RenderClear(sr);SDL_RenderCopy(sr, ste, NULL, NULL);SDL_RenderPresent(sr);}}av_free(buf);av_frame_free(&yuv);av_frame_free(&fr);av_packet_free(&pkt);sws_freeContext(imgCtx);SDL_DestroyTexture(ste);SDL_DestroyRenderer(sr);SDL_DestroyWindow(sw);SDL_Quit();avcodec_free_context(&cctx);avformat_close_input(&fctx);avformat_free_context(fctx);return 0;
}

最后,我司专注于流媒体和AI技术的研发与拓展,欢迎各界朋友莅临指导或技术交流,QQ:2470425326。

FFmpeg4.0 实现一个H264视频解码器相关推荐

  1. Android使用MediaCodec解码H264视频解码器

    前些日子有写了一篇博客[Android使用MediaCodec硬解码播放H264格式视频文件](http://blog.csdn.net/true100/article/details/5399293 ...

  2. ffmpeg音视频解码器

    在获取到视频文件的视频流与音频流之后,需要进行解码操作以还原其原本格式进行播放. 先上代码: #include<iostream>extern "C" { #inclu ...

  3. FFmpeg 4.0.2编码YUV序列为H264视频文件

    /****************************** 功能:编码YUV序列为h264视频文件 FFmpeg:4.0.2 ******************************/ #in ...

  4. H264 视频文件 帧格式 传输封装等 杂碎

    H264 视频文件 帧格式 传输封装等 杂碎 rfc3984 Standards Track [Page 2] RFC 3984 RTP Payload Format for H.264 Video ...

  5. 音视频开发之旅(34) - 基于FFmpeg实现简单的视频解码器

    目录 FFmpeg解码过程流程图和关键的数据结构 mp4通过FFmpeg解码YUV裸视频数据 遇到的问题 资料 收获 一.FFmpeg解码过程流程图和关键的数据结构 FFmpeg解码涉及的知识点比较多 ...

  6. FFmpeg4入门07:解码视频并保存为YUV格式文件

    上一篇我们解码并保存了其中的几帧确保解码过程和结果是对的.本篇我们将解码整个视频并保存为标准的YUV格式(YUV格式具体信息详见YUV格式介绍),我们就选YUV420P(I420)作为输出格式. 保存 ...

  7. FFmpeg4入门13:h264编码为mp4

    上一篇将yuv源视频文件编码为*.h264的由libx264实现压缩的文件,将源文件从55M编码为620KB,但是h264文件只有视频数据,而且使用范围不太广.那么就需要进一步的封装,在此选用最常用的 ...

  8. flutter 视频解码器fijkplayer使用

    本人做视频监控项目的时候,需要去展示视频流到用户端,一开始使用flutter自带的VideoPlayer播放监控视频,一开始没有发现有什么问题,因为使用多的是Android模拟器,一直没有使用iso模 ...

  9. 音视频开发系列(62)基于FFmpeg实现简单的视频解码器

    一.FFmpeg解码过程流程图和关键的数据结构 FFmpeg解码涉及的知识点比较多,很容易被函数和结构体搞定不知所错,我们先从整体上对解码流程有个认知,画了张解码流程图,如下 1.1 解码流程如下 a ...

最新文章

  1. 使用antd UI组件有感
  2. svn版本控制git(github)
  3. SpringBoot入门教程(十)应用监控Actuator
  4. centos 7 备份代码,然后回滚
  5. pandas 如何把时间转成index_pandas将字段中的字符类型转化为时间类型,并设置为索引...
  6. P3914-染色计数【树形dp】
  7. spring的基本知识
  8. [转]js实现简单的双向数据绑定
  9. 安全威胁建模综述_如何使用威胁建模分析应用程序的安全性
  10. mongodb mysql json数据类型_mongodb 数据格式补充
  11. unity天空盒渐变_Unity 制作天空盒
  12. abb变频器580系列改中文,ACS580变频器参数设置.pdf
  13. 【软件工程】软件需求说明书
  14. 进击的Objective-C -------------------NSSting,NSSMutableString和NSArray,NSMutableArray
  15. Flutter Tabbar 自定义选中下标 自定义Indicator
  16. 幻14 连不上无线网 网卡掉驱动
  17. 5000字 大数据时代读书笔记_大数据时代读书笔记
  18. 电子计算机常用面试题,计算机常用面试题文档.doc
  19. 关于如何构建数字资产量化的投资组合的思考
  20. MFC中TeeChart插件绘图

热门文章

  1. RFID物资管理系统解决方案-RFID智慧物资管理-杭州东识科技
  2. docker-compose命令通过指定文件运行
  3. 不到两年时间,半导体界发生了如此多收购兼并案---ESM
  4. 个人知识变现者必备基本功:造课卖课能力
  5. 获取iWatch的UDID
  6. 脊髓损伤的现场急救法
  7. 民宿管理系统课程设计_基于jsp的民宿网站预定管理-JavaEE实现民宿网站预定管理 - java项目源码...
  8. Chapter5:Octave教程:AndrewNg吴恩达《机器学习》笔记
  9. vscode如何配置中文版
  10. WordPress 百度熊掌号快速开发改造教程