Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2013
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1.  
  2.  
  3. Variables:
  4. AVPixelFormat m_AVPixelFormat;
  5. AVCodecContext* m_lpCodecCtx;
  6. AVCodec* m_lpCodec;
  7. AVFrame* m_lpFrame;
  8. AVPacket m_lpPacket;
  9. AVPicture m_lpPict;
  10. SwsContext* m_lpSwsContext;
  11. INT m_iThreadCount;
  12. LPBYTE m_lpFFMPEGBuffer;
  13. INT iImageWidth, iImageHeigh;
  14.  
  15.  
  16. BOOL FFMpegDecompress( LPBYTE lpImageData, DWORD dwImageSize)
  17. { if (m_lpCodec==NULL)
  18. { // find_decoder
  19. m_lpCodec = avcodec_find_decoder(CODEC_ID_H264);
  20. }
  21.  
  22. if ((m_lpCodec != NULL)&&(m_lpCodecCtx == NULL))
  23. { // Alloc codecContext
  24. m_lpCodecCtx = avcodec_alloc_context3(m_lpCodec);
  25. if (m_lpCodecCtx != NULL)
  26. { // Init CodecContext
  27. if (m_lpCodecCtx->width == 0)
  28. { m_lpCodecCtx->pix_fmt = AV_PIX_FMT_YUV420P;
  29. m_lpCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;
  30. m_lpCodecCtx->codec_id = CODEC_ID_H264;
  31. m_lpCodecCtx->coded_width = iImageWidth;
  32. m_lpCodecCtx->coded_height = iImageHeight;
  33. m_lpCodecCtx->width = iImageWidth;
  34. m_lpCodecCtx->height = iImageHeight;
  35. m_lpCodecCtx->thread_count = 3;
  36. m_lpCodecCtx->thread_type = FF_THREAD_SLICE;
  37. m_lpCodecCtx->err_recognition = AV_EF_EXPLODE;
  38. }
  39. }
  40. }
  41. dwWidth = iImageWidth;
  42. dwHeight = iImageHeight;
  43. // open decoder if not...
  44. if ((m_lpCodecCtx!=NULL) && (m_lpCodec!=NULL)&&(!lpfnavcodec_is_open(m_lpCodecCtx)))
  45. { if ( m_lpCodec != NULL)
  46. { if(m_lpCodec->capabilities&CODEC_CAP_TRUNCATED)
  47. m_lpCodecCtx->flags|= CODEC_FLAG_TRUNCATED;
  48.  
  49. iResult = avcodec_open2( m_lpCodecCtx, m_lpCodec, &optionsDict);
  50. // if error
  51. if (iResult < 0)
  52. { avcodec_close(m_lpCodecCtx);
  53. av_free(m_lpCodecCtx);
  54. m_lpCodecCtx = NULL;
  55. m_lpCodec = NULL;
  56. }
  57. }
  58. }
  59.  
  60. if ((m_lpCodecCtx!=NULL) && (m_lpCodec!=NULL))
  61. { // Allouer la frame video
  62. if (m_lpFrame == NULL)
  63. { m_lpFrame = avcodec_alloc_frame();
  64. }
  65. // Si l'allocation à réussi
  66. if (m_lpFrame!=NULL)
  67. { // Affecter les dimensions
  68. m_lpFrame->width = m_lpCodecCtx->width;
  69. m_lpFrame->height = m_lpCodecCtx->height;
  70. }
  71.  
  72. if ((m_lpCodecCtx != NULL) && ( m_lpCodec != NULL) &&(m_lpFrame != NULL) && (pddsDesc != NULL))
  73. if ((dwWidth>0)&&(dwHeight>0))
  74. { int iRes = 0;
  75. // init packet
  76. av_init_packet(&m_lpPacket);
  77. if (m_lpFFMPEGBuffer == NULL)
  78. { m_lpFFMPEGBuffer = new BYTE[MAX_MPEG4VIDEO_FRAMESIZE+FF_INPUT_BUFFER_PADDING_SIZE];
  79. }
  80. if (m_lpFFMPEGBuffer !=NULL)
  81. { ZeroMemory( m_lpFFMPEGBuffer, MAX_MPEG4VIDEO_FRAMESIZE+FF_INPUT_BUFFER_PADDING_SIZE);
  82. CopyMemory(m_lpFFMPEGBuffer, lpImageData, dwImageSize);
  83. }
  84.  
  85. // set data
  86. m_lpPacket.data = m_lpFFMPEGBuffer;
  87. // Set size
  88. m_lpPacket.size = dwImageSize;
  89. // Decode H264
  90. if ((m_lpFrame!=NULL)&&(m_lpPacket.size>0))
  91. { while (m_lpPacket.size>0)
  92. { iRes = lpfnavcodec_decode_video2( m_lpCodecCtx, m_lpFrame, &iFrameFinished, &m_lpPacket);
  93. if (iRes<0)
  94. { break;
  95. }
  96. if (iRes>0)
  97. { m_lpPacket.data+=iRes;
  98. m_lpPacket.size-=iRes;
  99. }
  100. }
  101. }
  102.  
  103. // free Packet
  104. av_free_packet(&m_lpPacket);
  105. }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement