这里也直接给出代码:

h264dec.h:

  1. #pragma once
  2. #include "tdll.h"
  3. #include "avcodec.h"
  4. #include "postprocess.h"
  5. //#include "EMVideoCodec.h"
  6. class h264dec /*: public IH264Decoder*/
  7. {
  8. public:
  9. virtual bool InitH264Deocder(int width,int height);
  10. virtual bool H264Decode(unsigned char * inbuf, const int & inlen,unsigned char * outbuf,int & outlen);
  11. virtual void StopH264Decoder();
  12. virtual void ReleaseConnection();
  13. public:
  14. h264dec(void);
  15. virtual ~h264dec(void);
  16. private:
  17. Tdll *dll;
  18. bool LoadDllFun();
  19. void (*avcodec_init)(void);
  20. void (*avcodec_register_all)(void);
  21. AVCodecContext* (*avcodec_alloc_context)(void);
  22. AVFrame* (*avcodec_alloc_frame)(void);
  23. AVCodec *(*avcodec_find_decoder)(enum CodecID id);
  24. int (*avcodec_decode_video)(AVCodecContext *avctx, AVFrame *picture,int *got_picture_ptr,
  25. uint8_t *buf, int buf_size);
  26. int  (*avcodec_open)(AVCodecContext *avctx, AVCodec *codec);
  27. int  (*avcodec_close)(AVCodecContext *avctx);
  28. void (*av_free)(void *ptr);
  29. bool InitPostproc(int w,int h);
  30. void ClosePostproc();
  31. pp_context_t *(*pp_get_context)(int width, int height, int flags);
  32. void (*pp_free_context)(pp_context_t *ppContext);
  33. void (*pp_free_mode)(pp_mode_t *mode);
  34. pp_mode_t *(*pp_get_mode_by_name_and_quality)(char *name, int quality);
  35. void  (*pp_postprocess)(uint8_t * src[3], int srcStride[3],
  36. uint8_t * dst[3], int dstStride[3],
  37. int horizontalSize, int verticalSize,
  38. QP_STORE_T *QP_store,  int QP_stride,
  39. pp_mode_t *mode, pp_context_t *ppContext, int pict_type);
  40. private:
  41. AVCodec         *pdec;
  42. AVCodecContext  *pdecContext;
  43. AVFrame         *pdecFrame;
  44. int             m_width;
  45. int             m_height;
  46. Tdll* prodll;
  47. pp_context_t *pp_context;
  48. pp_mode_t    *pp_mode;
  49. };

h264dec.cpp:

  1. #include "StdAfx.h"
  2. #include ".\h264dec.h"
  3. h264dec::h264dec(void)
  4. :dll(NULL)
  5. ,pdec(NULL)
  6. ,pdecContext(NULL)
  7. ,pdecFrame(NULL)
  8. ,pp_context(NULL)
  9. ,pp_mode(NULL)
  10. ,prodll(NULL)
  11. {
  12. }
  13. h264dec::~h264dec(void)
  14. {
  15. }
  16. bool h264dec::LoadDllFun()
  17. {
  18. dll=new Tdll(L"libavcodec.dll");
  19. dll->loadFunction((void**)&avcodec_init,"avcodec_init");
  20. dll->loadFunction((void**)&avcodec_register_all,"avcodec_register_all");
  21. dll->loadFunction((void**)&avcodec_alloc_context,"avcodec_alloc_context");
  22. dll->loadFunction((void**)&avcodec_alloc_frame,"avcodec_alloc_frame");
  23. dll->loadFunction((void**)&avcodec_find_decoder,"avcodec_find_decoder");
  24. dll->loadFunction((void**)&avcodec_open,"avcodec_open");
  25. dll->loadFunction((void**)&avcodec_decode_video,"avcodec_decode_video");
  26. dll->loadFunction((void**)&avcodec_close,"avcodec_close");
  27. dll->loadFunction((void**)&av_free,"av_free");
  28. if (!dll->ok)
  29. return false;
  30. prodll = new Tdll(L"postproc.dll");
  31. prodll->loadFunction((void**)&pp_get_context,"pp_get_context");
  32. prodll->loadFunction((void**)&pp_free_context,"pp_free_context");
  33. prodll->loadFunction((void**)&pp_free_mode,"pp_free_mode");
  34. prodll->loadFunction((void**)&pp_get_mode_by_name_and_quality,"pp_get_mode_by_name_and_quality");
  35. prodll->loadFunction((void**)&pp_postprocess,"pp_postprocess");
  36. if(!prodll->ok)
  37. return false;
  38. avcodec_init();
  39. avcodec_register_all();
  40. return true;
  41. }
  42. bool h264dec::InitH264Deocder(int width,int height)
  43. {
  44. if(!LoadDllFun())
  45. return false;
  46. if(!InitPostproc(width,height))
  47. return false;
  48. m_width=width;
  49. m_height=height;
  50. pdec = avcodec_find_decoder(CODEC_ID_H264);
  51. if (pdec == NULL )
  52. return false;
  53. pdecContext = avcodec_alloc_context();
  54. pdecFrame = avcodec_alloc_frame();
  55. pdecContext->width  = width;
  56. pdecContext->height = height;
  57. pdecContext->pix_fmt = PIX_FMT_YUV420P;
  58. /* open it */
  59. if (avcodec_open(pdecContext, pdec) < 0)
  60. {
  61. return false;
  62. }
  63. return true;
  64. }
  65. bool h264dec::InitPostproc(int w,int h)
  66. {
  67. int i_flags = 0;
  68. i_flags |= PP_CPU_CAPS_MMX | PP_CPU_CAPS_MMX2 | PP_FORMAT_420;
  69. pp_context = pp_get_context( w, h, i_flags );
  70. if(!pp_context)
  71. return false;
  72. pp_mode = pp_get_mode_by_name_and_quality( "default", 6 );
  73. if(!pp_mode)
  74. return false;
  75. return true;
  76. }
  77. bool h264dec::H264Decode(unsigned char * inbuf, const int & inlen,unsigned char * outbuf,int & outlen)
  78. {
  79. int got_frame;
  80. BYTE* showImage[3];
  81. int showheight[3],showLx[3];
  82. int len;
  83. len=avcodec_decode_video(pdecContext, pdecFrame, &got_frame, inbuf, inlen);
  84. if(len < 0)
  85. return false;
  86. if(got_frame)
  87. {
  88. showImage[0]=outbuf;
  89. showImage[1]=showImage[0]+m_width*m_height;
  90. showImage[2]=showImage[1]+m_width*m_height/4;
  91. showLx[0]=m_width;showLx[1]=m_width>>1;showLx[2]=m_width>>1;
  92. showheight[0]=m_height;showheight[1]=m_height>>1;showheight[2]=m_height>>1;
  93. pp_postprocess(pdecFrame->data,pdecFrame->linesize,showImage,showLx,m_width,m_height,pdecFrame->qscale_table,
  94. pdecFrame->qstride,pp_mode,pp_context,pdecFrame->pict_type);
  95. //GetImage( pdecFrame->data,
  96. //          showImage,
  97. //          pdecFrame->linesize,
  98. //          showLx,
  99. //          showheight);
  100. outlen=m_width*m_height*3/2;
  101. }
  102. else
  103. {
  104. outlen = 0;
  105. }
  106. return true;
  107. }
  108. void h264dec::StopH264Decoder()
  109. {
  110. if (pdecContext != NULL)
  111. {
  112. avcodec_close(pdecContext);
  113. av_free( pdecContext );
  114. pdecContext = NULL;
  115. if(pdecFrame){
  116. av_free(pdecFrame);
  117. pdecFrame = NULL;
  118. }
  119. }
  120. if(dll){
  121. delete dll;
  122. dll=0;
  123. }
  124. ClosePostproc();
  125. }
  126. void h264dec::ClosePostproc()
  127. {
  128. if(pp_mode){
  129. pp_free_mode( pp_mode );
  130. pp_mode=0;
  131. }
  132. if(pp_context){
  133. pp_free_context(pp_context);
  134. pp_context=0;
  135. }
  136. if(prodll){
  137. delete prodll;
  138. prodll=0;
  139. }
  140. }
  141. void h264dec::ReleaseConnection()
  142. {
  143. delete this;
  144. }

tdll.h:

  1. #ifndef _TDLL_
  2. #define _TDLL_
  3. class Tdll
  4. {
  5. private:
  6. HMODULE hdll;
  7. void loadDll(const char *dllName);
  8. public:
  9. bool ok;
  10. Tdll(const TCHAR *dllName1)
  11. {
  12. hdll=LoadLibrary(dllName1);
  13. if (!hdll)
  14. {
  15. hdll=NULL;
  16. }
  17. ok=(hdll!=NULL);
  18. };
  19. ~Tdll()
  20. {
  21. if (hdll)
  22. FreeLibrary(hdll);
  23. }
  24. void loadFunction(void **fnc,const char *name)
  25. {
  26. *fnc=GetProcAddress(hdll,name);
  27. ok&=(*fnc!=NULL);
  28. };
  29. };
  30. #endif

main.cpp:

    1. #include "stdafx.h"
    2. #include "h264dec.h"
    3. #include "postprocess.h"
    4. #define INBUF_SIZE 100 * 1024;
    5. static int FindStartCode (unsigned char *Buf, int zeros_in_startcode)
    6. {
    7. int info;
    8. int i;
    9. info = 1;
    10. for (i = 0; i < zeros_in_startcode; i++)
    11. {
    12. if(Buf[i] != 0)
    13. info = 0;
    14. }
    15. if(Buf[i] != 1)
    16. info = 0;
    17. return info;
    18. }
    19. static bool Check_StartCode(unsigned char *Buf, int pos)
    20. {
    21. int info3 = 0;
    22. info3 = FindStartCode(&Buf[pos-4], 3);
    23. return info3 == 1;
    24. }
    25. static int getNextNal(FILE* inpf, unsigned char* Buf)
    26. {
    27. int pos = 0;
    28. int StartCodeFound = 0;
    29. int info2 = 0;
    30. int info3 = 0;
    31. int nCount = 0;
    32. while(!feof(inpf) && ++nCount <= 4)
    33. {
    34. Buf[pos++]=fgetc(inpf);
    35. }
    36. if(!Check_StartCode(Buf, pos))
    37. {
    38. return 0;
    39. }
    40. while(!feof(inpf) && (Buf[pos++]=fgetc(inpf))==0);
    41. while (!StartCodeFound)
    42. {
    43. if (feof (inpf))
    44. {
    45. //          return -1;
    46. return pos-1;
    47. }
    48. Buf[pos++] = fgetc (inpf);
    49. StartCodeFound = Check_StartCode(Buf, pos);
    50. }
    51. fseek (inpf, -4, SEEK_CUR);
    52. return pos - 4;
    53. }
    54. int main(int argc, char* argv[])
    55. {
    56. if (argc != 5)
    57. {
    58. printf("please input: PP_Demo.exe filename1[input] Width Height filename2[output]\n");
    59. }
    60. //params set
    61. unsigned short usWidth = atoi(argv[2]);
    62. unsigned short usHeight = atoi(argv[3]);
    63. //create dec&pp
    64. h264dec *pdec = new h264dec;
    65. if(!pdec->InitH264Deocder(usWidth, usHeight))
    66. {
    67. return false;
    68. }
    69. unsigned char *p_In_Frame = new unsigned char[usWidth * usHeight * 3/2];
    70. unsigned char *p_Out_Frame = new unsigned char[usWidth * usHeight * 3/2];
    71. FILE* ifp = fopen(argv[1],"rb");
    72. FILE* ofp = fopen(argv[4],"wb");
    73. bool b_continue = true;
    74. int nReadUnit = usWidth * usHeight * 3/2;
    75. while(!feof(ifp))
    76. {
    77. int nCount = getNextNal(ifp, p_In_Frame);
    78. if(nCount == 0)
    79. {
    80. continue;
    81. }
    82. unsigned char *ptr = p_In_Frame;
    83. int n_Outlen = 0;
    84. pdec->H264Decode(ptr, nCount, p_Out_Frame, n_Outlen);
    85. if(n_Outlen > 0)
    86. {
    87. fwrite(p_Out_Frame, 1, n_Outlen, ofp);
    88. }
    89. }
    90. //realse
    91. delete []p_In_Frame;
    92. delete []p_Out_Frame;
    93. pdec->StopH264Decoder();
    94. pdec->ReleaseConnection();
    95. fclose(ifp);
    96. fclose(ofp);
    97. return 0;
    98. }

利用ffmpeg解码h264流的代码的更多相关文章

  1. 利用ffmpeg将H264流 解码为RGB

    利用H264解码分为几个步骤: 注意一点在添加头文件的时候要添加extern "C",不然会出现错误 [cpp] view plaincopy extern "C&quo ...

  2. FFmpeg解码H264及swscale缩放详解

    本文概要: 本文介绍著名开源音视频编解码库ffmpeg如何解码h264码流,比较详细阐述了其h264码流输入过程,解码原理,解码过程.同时,大部分应用环境下,以原始码流视频大小展示并不是最佳方式,因此 ...

  3. 【图像处理】FFmpeg解码H264及swscale缩放详解

      http://blog.csdn.net/gubenpeiyuan/article/details/19548019 主题 FFmpeg 本文概要: 本文介绍著名开源音视频编解码库ffmpeg如何 ...

  4. 在iOS平台使用ffmpeg解码h264视频流(转)

    在iOS平台使用ffmpeg解码h264视频流,有需要的朋友可以参考下. 对于视频文件和rtsp之类的主流视频传输协议,ffmpeg提供avformat_open_input接口,直接将文件路径或UR ...

  5. 在iOS平台使用ffmpeg解码h264视频流

    来源:http://www.aichengxu.com/view/37145 在iOS平台使用ffmpeg解码h264视频流,有需要的朋友可以参考下. 对于视频文件和rtsp之类的主流视频传输协议,f ...

  6. 使用X264编码yuv格式的视频帧使用ffmpeg解码h264视频帧

    前面一篇博客介绍在centos上搭建点击打开链接ffmpeg及x264开发环境.以下就来问个样例: 1.利用x264库将YUV格式视频文件编码为h264格式视频文件 2.利用ffmpeh库将h264格 ...

  7. FFmpeg开发笔记(九):ffmpeg解码rtsp流并使用SDL同步播放

    前言   ffmpeg播放rtsp网络流和摄像头流.   Demo   使用ffmpeg播放局域网rtsp1080p海康摄像头:延迟0.2s,存在马赛克     使用ffmpeg播放网络rtsp文件流 ...

  8. 利用ffmpeg将H264解码为RGB

    因为公司买到了一个不提供解码器的设备,我不得已还要做解码的工作.在网上找了一圈,H264解码比較方便的也就是ffmpeg一系列的函数库了,原本设备中也是用这套函数库解码,但厂家不给提供,没办法,仅仅得 ...

  9. (转)FFMPEG解码H264拼帧简解

    http://blog.csdn.net/ikevin/article/details/7649095 H264的I帧通常 0x00 0x00 0x00 0x01 0x67 开始,到下一个帧头开始之前 ...

随机推荐

  1. Eclipse插件开发 swt ComboBoxCellEditor CCombo 下拉框高度

    效果图:     代码如下 bindingPageTableViewer.setCellModifier(new ICellModifier() { public boolean canModify( ...

  2. PHP之list()函数讲解

    定义和用法 list() 函数用数组中的元素为一组变量赋值. 注意,与 array() 类似,list() 实际上是一种语言结构,不是函数. 语法 list(var1,var2...) 参数 描述 v ...

  3. sgu 138

    自己猜测了一下  按比赛次数 从大到小排  然后类似于模拟 先排胜的场次 当只剩一场 将它定义为败 #include <cstdio> #include <cstdlib> # ...

  4. model对象之setter方法使用,解决去除空格和将数字转成字符串展示方法

    1.系统中手机号注册的时候,不能含有前后空格.在model对象中过滤~! private String mobile; public String getMobile() { return mobil ...

  5. [转] ADO.NET实体框架引发争论

    转自:http://developer.51cto.com/art/200811/76356.htm 2008-11-11 14:00 朱永光译 infoq 我要评论(0) 一个在ADO.NET实体框 ...

  6. POJ2503Babelfish

    http://poj.org/problem?id=2503 这个题一开始是想用字典树,发现太麻烦..... #include<cstdio> #include<cstring> ...

  7. Android ActionBar 关于tab的应用 以及 TabListener的方法详解

    actionBar的tab标签应用以及TabListener的方法详解 package com.example.actionBarTest.actionBarTab; import android.a ...

  8. lintcode :Partition List 链表划分

    题目: 链表划分 给定一个单链表和数值x,划分链表使得所有小于x的节点排在大于等于x的节点之前. 你应该保留两部分内链表节点原有的相对顺序. 样例 给定链表 1->4->3->2-& ...

  9. NSDate & NSDateFormatter

    #import <Foundation/Foundation.h>   int main(int argc, const char * argv[]) {    @autoreleasep ...

  10. C++:纯虚函数与抽象类

    5.4.3 纯虚函数和抽象类 纯虚函数是一个在基类中说明的虚函数,它在该基类中没有定义,但是要求在派生类中根据需要对它进行定义,或仍然说明为纯虚函数. 声明纯虚函数的一般格式是: virtual 函数 ...