Qt基于FFmpeg播放本地 H.264(H264)文件(灿哥哥的博客)
最近在弄H264的硬件编解码,基于DM3730,但是为了调试方便,在小红帽上用FFmpeg实现了H264的软件编解码。现在弄了一个Windows的例子,给需要的同学参考一下,如果大家觉得有帮助,可以小手一抖,帮我点个赞
。
这个例子是Qt Mingw版本的,FFmpeg可以去官网下载,也可以自己编译,编译方法可以参考我的博文。
Windows 7(Win7)下MinGW+msys编译ffmpeg,并加入H264编码支持
下面是H264解码类
ch264decoder.h
- #ifndef CH264DECODER_H
- #define CH264DECODER_H
- #include <string.h>
- //C++引用C语言的头文件
- extern "C"
- {
- #include "libavformat/avformat.h"
- #include "libswresample/swresample.h"
- #include "libavutil/opt.h"
- #include "libavutil/channel_layout.h"
- #include "libavutil/parseutils.h"
- #include "libavutil/samplefmt.h"
- #include "libavutil/fifo.h"
- #include "libavutil/intreadwrite.h"
- #include "libavutil/dict.h"
- #include "libavutil/mathematics.h"
- #include "libavutil/pixdesc.h"
- #include "libavutil/avstring.h"
- #include "libavutil/imgutils.h"
- #include "libavcodec/avcodec.h"
- #include "libavfilter/avfilter.h"
- #include "libavfilter/buffersrc.h"
- #include "libavfilter/buffersink.h"
- }
- class CH264Decoder
- {
- public:
- CH264Decoder();
- ~CH264Decoder();
- /*************************************************
- Function:initial
- Description:初始化
- Input:无
- Output:无
- Return:错误代码
- Others:无
- *************************************************/
- int initial();
- /*************************************************
- Function:decode
- Description:解码
- Input:pDataIn-待解码数据,nInSize-待解码数据长度
- Output:pDataOut-解码后的数据,nWidth-解码后的图像宽度,nHeight-解码后的图像高度
- Return:错误代码
- Others:解码后的数据为RGB16格式
- *************************************************/
- int decode(uint8_t *pDataIn, int nInSize, uint8_t *pDataOut, int *nWidth, int *nHeight);
- /*************************************************
- Function:unInitial
- Description:销毁
- Input:无
- Output:无
- Return:无
- Others:无
- *************************************************/
- void unInitial();
- private:
- void deleteYUVTab();
- void createYUVTab_16();
- void displayYUV_16(unsigned int *pdst, unsigned char *y, unsigned char *u,unsigned char *v,
- int width, int height, int src_ystride, int src_uvstride, int dst_ystride);
- private:
- int *colortab;
- int *u_b_tab;
- int *u_g_tab;
- int *v_g_tab;
- int *v_r_tab;
- unsigned int *rgb_2_pix;
- unsigned int *r_2_pix;
- unsigned int *g_2_pix;
- unsigned int *b_2_pix;
- AVCodec *codec;
- AVCodecContext *context;
- AVFrame *frame;
- AVPacket packet;
- };
- #endif // CH264DECODER_H
ch264decoder.cpp
- #include "ch264decoder.h"
- #include <QDebug>
- CH264Decoder::CH264Decoder()
- {
- createYUVTab_16();
- }
- CH264Decoder::~CH264Decoder()
- {
- deleteYUVTab();
- }
- void CH264Decoder::deleteYUVTab()
- {
- av_free(colortab);
- av_free(rgb_2_pix);
- }
- void CH264Decoder::createYUVTab_16()
- {
- int i;
- int u, v;
- colortab = (int *)av_malloc(4*256*sizeof(int));
- u_b_tab = &colortab[0*256];
- u_g_tab = &colortab[1*256];
- v_g_tab = &colortab[2*256];
- v_r_tab = &colortab[3*256];
- for (i=0; i<256; i++)
- {
- u = v = (i-128);
- u_b_tab[i] = (int) ( 1.772 * u);
- u_g_tab[i] = (int) ( 0.34414 * u);
- v_g_tab[i] = (int) ( 0.71414 * v);
- v_r_tab[i] = (int) ( 1.402 * v);
- }
- rgb_2_pix = (unsigned int *)av_malloc(3*768*sizeof(unsigned int));
- r_2_pix = &rgb_2_pix[0*768];
- g_2_pix = &rgb_2_pix[1*768];
- b_2_pix = &rgb_2_pix[2*768];
- for(i=0; i<256; i++)
- {
- r_2_pix[i] = 0;
- g_2_pix[i] = 0;
- b_2_pix[i] = 0;
- }
- for(i=0; i<256; i++)
- {
- r_2_pix[i+256] = (i & 0xF8) << 8;
- g_2_pix[i+256] = (i & 0xFC) << 3;
- b_2_pix[i+256] = (i ) >> 3;
- }
- for(i=0; i<256; i++)
- {
- r_2_pix[i+512] = 0xF8 << 8;
- g_2_pix[i+512] = 0xFC << 3;
- b_2_pix[i+512] = 0x1F;
- }
- r_2_pix += 256;
- g_2_pix += 256;
- b_2_pix += 256;
- }
- void CH264Decoder::displayYUV_16(unsigned int *pdst, unsigned char *y, unsigned char *u, unsigned char *v, int width, int height, int src_ystride, int src_uvstride, int dst_ystride)
- {
- int i, j;
- int r, g, b, rgb;
- int yy, ub, ug, vg, vr;
- unsigned char* yoff;
- unsigned char* uoff;
- unsigned char* voff;
- int width2 = width/2;
- int height2 = height/2;
- for(j=0; j<height2; j++)
- {
- yoff = y + j * 2 * src_ystride;
- uoff = u + j * src_uvstride;
- voff = v + j * src_uvstride;
- for(i=0; i<width2; i++)
- {
- yy = *(yoff+(i<<1));
- ub = u_b_tab[*(uoff+i)];
- ug = u_g_tab[*(uoff+i)];
- vg = v_g_tab[*(voff+i)];
- vr = v_r_tab[*(voff+i)];
- b = yy + ub;
- g = yy - ug - vg;
- r = yy + vr;
- rgb = r_2_pix[r] + g_2_pix[g] + b_2_pix[b];
- yy = *(yoff+(i<<1)+1);
- b = yy + ub;
- g = yy - ug - vg;
- r = yy + vr;
- pdst[(j*dst_ystride+i)] = (rgb)+((r_2_pix[r] + g_2_pix[g] + b_2_pix[b])<<16);
- yy = *(yoff+(i<<1)+src_ystride);
- b = yy + ub;
- g = yy - ug - vg;
- r = yy + vr;
- rgb = r_2_pix[r] + g_2_pix[g] + b_2_pix[b];
- yy = *(yoff+(i<<1)+src_ystride+1);
- b = yy + ub;
- g = yy - ug - vg;
- r = yy + vr;
- pdst [((2*j+1)*dst_ystride+i*2)>>1] = (rgb)+((r_2_pix[r] + g_2_pix[g] + b_2_pix[b])<<16);
- }
- }
- }
- int CH264Decoder::initial()
- {
- avcodec_register_all();
- av_init_packet(&packet);
- codec = avcodec_find_decoder(AV_CODEC_ID_H264);
- if (!codec)
- {
- printf("avcodec_find_encoder failed");
- return -1;
- }
- context = avcodec_alloc_context3(codec);
- if (!context)
- {
- printf("avcodec_alloc_context3 failed");
- return -2;
- }
- context->codec_type = AVMEDIA_TYPE_VIDEO;
- context->pix_fmt = AV_PIX_FMT_YUV420P;
- if (avcodec_open2(context, codec, NULL) < 0)
- {
- printf("avcodec_open2 failed");
- return -3;
- }
- frame = av_frame_alloc();
- if (!frame)
- {
- return -4;
- }
- return 0;
- }
- void CH264Decoder::unInitial()
- {
- avcodec_close(context);
- av_free(context);
- av_frame_free(&frame);
- }
- int CH264Decoder::decode(uint8_t *pDataIn, int nInSize, uint8_t *pDataOut,int *nWidth, int *nHeight)
- {
- av_init_packet(&packet);
- packet.size = nInSize;
- packet.data = pDataIn;
- if (packet.size > 0)
- {
- int got_picture=0;
- int ret= avcodec_decode_video2(context, frame, &got_picture, &packet);
- if (ret < 0)
- {
- printf("avcodec_encode_video2 failed");
- return -2;
- }
- if (got_picture)
- {
- *nWidth = context->width;
- *nHeight = context->height;
- displayYUV_16((unsigned int*)pDataOut, frame->data[0], frame->data[1],frame->data[2],
- *nWidth,*nHeight,frame->linesize[0],frame->linesize[2],*nWidth);
- }
- }
- else
- {
- printf("no data to decode");
- return -1;
- }
- return 0;
- }
使用方法,先调用initial()进行初始化,然后读取本地H264文件,调用decode()函数进行解码,退出时调用unInitial()函数释放资源。
需要注意的是,解码后的数据是RGB16格式,转换为QImage时,最后一个参数要用QImage::Format_RGB16,例如QImage image= QImage(outBuf, width, height, QImage::Format_RGB16);
下图是播放效果:
演示用的H264文件下载链接:http://download.csdn.net/detail/caoshangpa/9492803
源码下载链接:见http://blog.csdn.net/caoshangpa/article/details/51953208的评论
Qt基于FFmpeg播放本地 H.264(H264)文件(灿哥哥的博客)的更多相关文章
- 基于RTP协议的H.264传输
1. 引言 随 着信息产业的发展,人们对信息资源的要求已经逐渐由文字和图片过渡到音频和视频,并越来越强调获取资源的实时性和互动性.但人们又面临着另外一种不可避免 的尴尬,就是在网络上看 ...
- 本地Markdown文件上传到博客
本地Markdown文件上传到博客 参考:https://www.cnblogs.com/ccylhw/p/13954153.html 1.Typora 最漂亮的写作APPhttps://www.ty ...
- 树莓派编译安装 FFmpeg(添加 H.264 硬件编解码器支持)
说明 FFmpeg 是一套开源的音视频编解码库,有非常强大的功能,包括视频采集功能.视频格式转换等.众所周知视频编解码是一个非常消耗系统资源的过程,而树莓派自带了 H.264 的硬件编解码器,因此本文 ...
- iOS中 本地通知/本地通知详解 韩俊强的博客
布局如下:(重点讲本地通知) iOS开发者交流QQ群: 446310206 每日更新关注:http://weibo.com/hanjunqiang 新浪微博 Notification是智能手机应用编 ...
- 基于SAE+CodeIgniter3.0+管理端angularjs+前台amazeui的多用户博客系统V1.0--系统设计(一)
开发环境: 服务器系统:CentOS-6.x web服务器:Apache-2.2.x php版本:PHP-5.3.x 开发工具:sublime text 3 ,谷歌浏览器 数据库查询工具:phpmya ...
- 兼容各个浏览器的H.264播放: H.264+HTML5+FLOWPLAYER+WOWZA+RMTP
一.方案确定 计划做视频播放,要求可以播放H264编码的mp4文件,各个浏览器,各种终端都能播放. 首先查找可行性方案, http://www.cnblogs.com/sink_cup/archive ...
- [转]兼容各个浏览器的H.264播放: H.264+HTML5+FLOWPLAYER+WOWZA+RMTP
一.方案确定 计划做视频播放,要求能够播放H264编码的mp4文件,各个浏览器,各种终端都能播放. 首先查找可行性方案, http://www.cnblogs.com/sink_cup/archive ...
- ffmpeg接收udp输入的h264文件流,推流到rtmp服务器
ffmpeg -re -f h264 -i udp://192.168.5.49:10002 -vcodec libx264 -f flv rtmp://192.168.5.155/live/1
- 3种用组策略将域帐号加入本地管理员组的方法_jinifly_新浪博客
次当前系统域帐号是怎么在第一次登录时,自动加入域客户端本地管理员组的?我猜不外乎就是脚本.计算机策略或虚拟机初始化的自动应答脚本,结果系统的前任同事找到了答案--GPO的用户策略(确切讲是用户首选项) ...
随机推荐
- 自制单片机之十……AT89S51的上拉电阻问题
很多网友都问我AT89S51的P0口为什么要接一个上拉电阻.我就用一个篇幅来说一说 P0口和其它三个口的内部电路是不同的,如下图 P0口是接在两个三极管D0和D1之间的,而P1-P3口的上部是接一个电 ...
- SSH自动部署(转)
我的是windows环境,目前开发的过程中,有些项目需要一下子部署到很多的linux服务器上.写了个脚本能够自动上传文件和执行部署任务.完成这个任务需要的条件包括SSH配置和一个执行脚本. 准备 1. ...
- 利用php unpack读取c struct的二进制数据,struct内存对齐引起的一些问题
c语言代码 #include <stdio.h> struct test{ int a; unsigned char b; int c; }; int main(){ FILE *fp; ...
- 【剑指offer】面试题40:数组中只出现一次的数字
题目: 一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字. 思路: 因为只有两个只出现一次的数字,所以所有数字进行异或之后得到值res一定不是0.这样,res ...
- spoj1812-Longest Common Substring II(后缀自动机)
Description A string is finite sequence of characters over a non-empty finite set Σ. In this problem ...
- Android 百度地图API(01)_开发环境 HelloBaiduMap
转载于:http://blog.csdn.net/lmj623565791/article/details/37729091 转载于:http://blog.csdn.net/crazy1235/ar ...
- Javascript:重用之道
近期写了大量的js,愈发觉得自己的代码过于冗余,所以,利用周末的时间研习代码重用之道,有了这篇博文所得: 重用代码: 1.尽量保证 HTML 代码结构一致,可以通过父级选取子元素 2.把核心主程序实现 ...
- 在Servlet中使用spring注入的bean
package abu.csdn.servlet; import java.io.IOException; import javax.servlet.ServletContext; ...
- HR系统+人脸识别
近期一直在写一套HR系统,这套HR系统和人脸识别相结合.全然杜绝取代刷卡的情况产生.系统的灵活性比較强,开发简洁高速. 例如以下是一些功能上的截图 考勤模块仅仅是列举当中一个请假申请的功能做为展示 ...
- 利用Excel批量高速发送电子邮件
利用Excel批量高速发送电子邮件,分两步: 1. 准备待发送的数据: a.) 打开Excel,新建Book1.xlsx b.) 填入以下的内容, 第一列:接收人,第二列:邮件标题,第三列:正文,第四 ...