利用ffmpeg解码h264流的代码
这里也直接给出代码:
h264dec.h:
- #pragma once
- #include "tdll.h"
- #include "avcodec.h"
- #include "postprocess.h"
- //#include "EMVideoCodec.h"
- class h264dec /*: public IH264Decoder*/
- {
- public:
- virtual bool InitH264Deocder(int width,int height);
- virtual bool H264Decode(unsigned char * inbuf, const int & inlen,unsigned char * outbuf,int & outlen);
- virtual void StopH264Decoder();
- virtual void ReleaseConnection();
- public:
- h264dec(void);
- virtual ~h264dec(void);
- private:
- Tdll *dll;
- bool LoadDllFun();
- void (*avcodec_init)(void);
- void (*avcodec_register_all)(void);
- AVCodecContext* (*avcodec_alloc_context)(void);
- AVFrame* (*avcodec_alloc_frame)(void);
- AVCodec *(*avcodec_find_decoder)(enum CodecID id);
- int (*avcodec_decode_video)(AVCodecContext *avctx, AVFrame *picture,int *got_picture_ptr,
- uint8_t *buf, int buf_size);
- int (*avcodec_open)(AVCodecContext *avctx, AVCodec *codec);
- int (*avcodec_close)(AVCodecContext *avctx);
- void (*av_free)(void *ptr);
- bool InitPostproc(int w,int h);
- void ClosePostproc();
- pp_context_t *(*pp_get_context)(int width, int height, int flags);
- void (*pp_free_context)(pp_context_t *ppContext);
- void (*pp_free_mode)(pp_mode_t *mode);
- pp_mode_t *(*pp_get_mode_by_name_and_quality)(char *name, int quality);
- void (*pp_postprocess)(uint8_t * src[3], int srcStride[3],
- uint8_t * dst[3], int dstStride[3],
- int horizontalSize, int verticalSize,
- QP_STORE_T *QP_store, int QP_stride,
- pp_mode_t *mode, pp_context_t *ppContext, int pict_type);
- private:
- AVCodec *pdec;
- AVCodecContext *pdecContext;
- AVFrame *pdecFrame;
- int m_width;
- int m_height;
- Tdll* prodll;
- pp_context_t *pp_context;
- pp_mode_t *pp_mode;
- };
h264dec.cpp:
- #include "StdAfx.h"
- #include ".\h264dec.h"
- h264dec::h264dec(void)
- :dll(NULL)
- ,pdec(NULL)
- ,pdecContext(NULL)
- ,pdecFrame(NULL)
- ,pp_context(NULL)
- ,pp_mode(NULL)
- ,prodll(NULL)
- {
- }
- h264dec::~h264dec(void)
- {
- }
- bool h264dec::LoadDllFun()
- {
- dll=new Tdll(L"libavcodec.dll");
- dll->loadFunction((void**)&avcodec_init,"avcodec_init");
- dll->loadFunction((void**)&avcodec_register_all,"avcodec_register_all");
- dll->loadFunction((void**)&avcodec_alloc_context,"avcodec_alloc_context");
- dll->loadFunction((void**)&avcodec_alloc_frame,"avcodec_alloc_frame");
- dll->loadFunction((void**)&avcodec_find_decoder,"avcodec_find_decoder");
- dll->loadFunction((void**)&avcodec_open,"avcodec_open");
- dll->loadFunction((void**)&avcodec_decode_video,"avcodec_decode_video");
- dll->loadFunction((void**)&avcodec_close,"avcodec_close");
- dll->loadFunction((void**)&av_free,"av_free");
- if (!dll->ok)
- return false;
- prodll = new Tdll(L"postproc.dll");
- prodll->loadFunction((void**)&pp_get_context,"pp_get_context");
- prodll->loadFunction((void**)&pp_free_context,"pp_free_context");
- prodll->loadFunction((void**)&pp_free_mode,"pp_free_mode");
- prodll->loadFunction((void**)&pp_get_mode_by_name_and_quality,"pp_get_mode_by_name_and_quality");
- prodll->loadFunction((void**)&pp_postprocess,"pp_postprocess");
- if(!prodll->ok)
- return false;
- avcodec_init();
- avcodec_register_all();
- return true;
- }
- bool h264dec::InitH264Deocder(int width,int height)
- {
- if(!LoadDllFun())
- return false;
- if(!InitPostproc(width,height))
- return false;
- m_width=width;
- m_height=height;
- pdec = avcodec_find_decoder(CODEC_ID_H264);
- if (pdec == NULL )
- return false;
- pdecContext = avcodec_alloc_context();
- pdecFrame = avcodec_alloc_frame();
- pdecContext->width = width;
- pdecContext->height = height;
- pdecContext->pix_fmt = PIX_FMT_YUV420P;
- /* open it */
- if (avcodec_open(pdecContext, pdec) < 0)
- {
- return false;
- }
- return true;
- }
- bool h264dec::InitPostproc(int w,int h)
- {
- int i_flags = 0;
- i_flags |= PP_CPU_CAPS_MMX | PP_CPU_CAPS_MMX2 | PP_FORMAT_420;
- pp_context = pp_get_context( w, h, i_flags );
- if(!pp_context)
- return false;
- pp_mode = pp_get_mode_by_name_and_quality( "default", 6 );
- if(!pp_mode)
- return false;
- return true;
- }
- bool h264dec::H264Decode(unsigned char * inbuf, const int & inlen,unsigned char * outbuf,int & outlen)
- {
- int got_frame;
- BYTE* showImage[3];
- int showheight[3],showLx[3];
- int len;
- len=avcodec_decode_video(pdecContext, pdecFrame, &got_frame, inbuf, inlen);
- if(len < 0)
- return false;
- if(got_frame)
- {
- showImage[0]=outbuf;
- showImage[1]=showImage[0]+m_width*m_height;
- showImage[2]=showImage[1]+m_width*m_height/4;
- showLx[0]=m_width;showLx[1]=m_width>>1;showLx[2]=m_width>>1;
- showheight[0]=m_height;showheight[1]=m_height>>1;showheight[2]=m_height>>1;
- pp_postprocess(pdecFrame->data,pdecFrame->linesize,showImage,showLx,m_width,m_height,pdecFrame->qscale_table,
- pdecFrame->qstride,pp_mode,pp_context,pdecFrame->pict_type);
- //GetImage( pdecFrame->data,
- // showImage,
- // pdecFrame->linesize,
- // showLx,
- // showheight);
- outlen=m_width*m_height*3/2;
- }
- else
- {
- outlen = 0;
- }
- return true;
- }
- void h264dec::StopH264Decoder()
- {
- if (pdecContext != NULL)
- {
- avcodec_close(pdecContext);
- av_free( pdecContext );
- pdecContext = NULL;
- if(pdecFrame){
- av_free(pdecFrame);
- pdecFrame = NULL;
- }
- }
- if(dll){
- delete dll;
- dll=0;
- }
- ClosePostproc();
- }
- void h264dec::ClosePostproc()
- {
- if(pp_mode){
- pp_free_mode( pp_mode );
- pp_mode=0;
- }
- if(pp_context){
- pp_free_context(pp_context);
- pp_context=0;
- }
- if(prodll){
- delete prodll;
- prodll=0;
- }
- }
- void h264dec::ReleaseConnection()
- {
- delete this;
- }
tdll.h:
- #ifndef _TDLL_
- #define _TDLL_
- class Tdll
- {
- private:
- HMODULE hdll;
- void loadDll(const char *dllName);
- public:
- bool ok;
- Tdll(const TCHAR *dllName1)
- {
- hdll=LoadLibrary(dllName1);
- if (!hdll)
- {
- hdll=NULL;
- }
- ok=(hdll!=NULL);
- };
- ~Tdll()
- {
- if (hdll)
- FreeLibrary(hdll);
- }
- void loadFunction(void **fnc,const char *name)
- {
- *fnc=GetProcAddress(hdll,name);
- ok&=(*fnc!=NULL);
- };
- };
- #endif
main.cpp:
- #include "stdafx.h"
- #include "h264dec.h"
- #include "postprocess.h"
- #define INBUF_SIZE 100 * 1024;
- static int FindStartCode (unsigned char *Buf, int zeros_in_startcode)
- {
- int info;
- int i;
- info = 1;
- for (i = 0; i < zeros_in_startcode; i++)
- {
- if(Buf[i] != 0)
- info = 0;
- }
- if(Buf[i] != 1)
- info = 0;
- return info;
- }
- static bool Check_StartCode(unsigned char *Buf, int pos)
- {
- int info3 = 0;
- info3 = FindStartCode(&Buf[pos-4], 3);
- return info3 == 1;
- }
- static int getNextNal(FILE* inpf, unsigned char* Buf)
- {
- int pos = 0;
- int StartCodeFound = 0;
- int info2 = 0;
- int info3 = 0;
- int nCount = 0;
- while(!feof(inpf) && ++nCount <= 4)
- {
- Buf[pos++]=fgetc(inpf);
- }
- if(!Check_StartCode(Buf, pos))
- {
- return 0;
- }
- while(!feof(inpf) && (Buf[pos++]=fgetc(inpf))==0);
- while (!StartCodeFound)
- {
- if (feof (inpf))
- {
- // return -1;
- return pos-1;
- }
- Buf[pos++] = fgetc (inpf);
- StartCodeFound = Check_StartCode(Buf, pos);
- }
- fseek (inpf, -4, SEEK_CUR);
- return pos - 4;
- }
- int main(int argc, char* argv[])
- {
- if (argc != 5)
- {
- printf("please input: PP_Demo.exe filename1[input] Width Height filename2[output]\n");
- }
- //params set
- unsigned short usWidth = atoi(argv[2]);
- unsigned short usHeight = atoi(argv[3]);
- //create dec&pp
- h264dec *pdec = new h264dec;
- if(!pdec->InitH264Deocder(usWidth, usHeight))
- {
- return false;
- }
- unsigned char *p_In_Frame = new unsigned char[usWidth * usHeight * 3/2];
- unsigned char *p_Out_Frame = new unsigned char[usWidth * usHeight * 3/2];
- FILE* ifp = fopen(argv[1],"rb");
- FILE* ofp = fopen(argv[4],"wb");
- bool b_continue = true;
- int nReadUnit = usWidth * usHeight * 3/2;
- while(!feof(ifp))
- {
- int nCount = getNextNal(ifp, p_In_Frame);
- if(nCount == 0)
- {
- continue;
- }
- unsigned char *ptr = p_In_Frame;
- int n_Outlen = 0;
- pdec->H264Decode(ptr, nCount, p_Out_Frame, n_Outlen);
- if(n_Outlen > 0)
- {
- fwrite(p_Out_Frame, 1, n_Outlen, ofp);
- }
- }
- //realse
- delete []p_In_Frame;
- delete []p_Out_Frame;
- pdec->StopH264Decoder();
- pdec->ReleaseConnection();
- fclose(ifp);
- fclose(ofp);
- return 0;
- }
利用ffmpeg解码h264流的代码的更多相关文章
- 利用ffmpeg将H264流 解码为RGB
利用H264解码分为几个步骤: 注意一点在添加头文件的时候要添加extern "C",不然会出现错误 [cpp] view plaincopy extern "C&quo ...
- FFmpeg解码H264及swscale缩放详解
本文概要: 本文介绍著名开源音视频编解码库ffmpeg如何解码h264码流,比较详细阐述了其h264码流输入过程,解码原理,解码过程.同时,大部分应用环境下,以原始码流视频大小展示并不是最佳方式,因此 ...
- 【图像处理】FFmpeg解码H264及swscale缩放详解
http://blog.csdn.net/gubenpeiyuan/article/details/19548019 主题 FFmpeg 本文概要: 本文介绍著名开源音视频编解码库ffmpeg如何 ...
- 在iOS平台使用ffmpeg解码h264视频流(转)
在iOS平台使用ffmpeg解码h264视频流,有需要的朋友可以参考下. 对于视频文件和rtsp之类的主流视频传输协议,ffmpeg提供avformat_open_input接口,直接将文件路径或UR ...
- 在iOS平台使用ffmpeg解码h264视频流
来源:http://www.aichengxu.com/view/37145 在iOS平台使用ffmpeg解码h264视频流,有需要的朋友可以参考下. 对于视频文件和rtsp之类的主流视频传输协议,f ...
- 使用X264编码yuv格式的视频帧使用ffmpeg解码h264视频帧
前面一篇博客介绍在centos上搭建点击打开链接ffmpeg及x264开发环境.以下就来问个样例: 1.利用x264库将YUV格式视频文件编码为h264格式视频文件 2.利用ffmpeh库将h264格 ...
- FFmpeg开发笔记(九):ffmpeg解码rtsp流并使用SDL同步播放
前言 ffmpeg播放rtsp网络流和摄像头流. Demo 使用ffmpeg播放局域网rtsp1080p海康摄像头:延迟0.2s,存在马赛克 使用ffmpeg播放网络rtsp文件流 ...
- 利用ffmpeg将H264解码为RGB
因为公司买到了一个不提供解码器的设备,我不得已还要做解码的工作.在网上找了一圈,H264解码比較方便的也就是ffmpeg一系列的函数库了,原本设备中也是用这套函数库解码,但厂家不给提供,没办法,仅仅得 ...
- (转)FFMPEG解码H264拼帧简解
http://blog.csdn.net/ikevin/article/details/7649095 H264的I帧通常 0x00 0x00 0x00 0x01 0x67 开始,到下一个帧头开始之前 ...
随机推荐
- 【BZOJ】【1086】 【SCOI2005】王室联邦
树分块 orz vfk && PoPoQQQ http://vfleaking.blog.163.com/blog/static/174807634201231684436977/ h ...
- 20160728noip模拟赛zld
前言:单独对题面描述的评分-> [题解]把相邻长度为2的子串两两连边,跑欧拉路 /*明天再写,先贴一份方老师代码压压惊*/ #include<map> #include<sta ...
- [设计模式] 17 中介者模式 Mediator Pattern
在GOF的<设计模式:可复用面向对象软件的基础>一书中对中介者模式是这样说的:用一个中介对象来封装一系列的对象交互.中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变 ...
- uva 11181
直接枚举计算就行: #include<cstdio> #include<cstring> #include<algorithm> #define maxn 22 u ...
- Unity3D 游戏开发构架篇 —— 动态大场景生成 = 区域加载+对象池管理
项目做一个类似无尽模式的场景,想了一想,其实方法很简单,做一个相关的总结. 主要先谈一谈构架,后期附上代码. 一.区域加载 其实无尽场景的实现很简单,因为屏幕限制,那么不论何时何地,我们只能看到自己的 ...
- Appium环境配置
一.JDK下载.安装及其环境配置 1.下载.安装略过…… 2.环境配置,以jdk-8u45为例,默认安装在 C:\Program Files\Java\jdk1.8.0_45\路径下. 下面设置环境变 ...
- POJ 3280 Cheapest Palindrome(DP)
题目链接 题意 :给你一个字符串,让你删除或添加某些字母让这个字符串变成回文串,删除或添加某个字母要付出相应的代价,问你变成回文所需要的最小的代价是多少. 思路 :DP[i][j]代表的是 i 到 j ...
- Eclipse jetty
下载Eclipse的Jetty插件run-jetty-run http://download.csdn.net/detail/zhwq1216/7995627 当修改文件时,不需要进行服务重启设置 R ...
- lintcode 中等题:permutations 全排列
题目 全排列 给定一个数字列表,返回其所有可能的排列. 您在真实的面试中是否遇到过这个题? Yes 样例 给出一个列表[1,2,3],其全排列为: [ [1,2,3], [1,3,2], [2,1,3 ...
- *[topcoder]BracketExpressions
http://community.topcoder.com/stat?c=problem_statement&pm=13243 就是能否通过把字符串中的'X'替换成"()" ...