Advertisement
Guest User

Untitled

a guest
Feb 26th, 2013
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.55 KB | None | 0 0
  1. //////////////////////////////////////////////////////////////////////////////////////////////////////
  2. int img_convert( AVPicture* dst, AVPixelFormat dst_pix_fmt, int dstWidth, int dstHeight, AVPicture* src, AVPixelFormat src_pix_fmt, int srcWidth, int srcHeight, DWORD dwStride)
  3. //////////////////////////////////////////////////////////////////////////////////////////////////////
  4. { int result = 0;
  5.  
  6. // Init linesize on the output stride
  7. if (dst->linesize[0] == 0) dst->linesize[0] = dwStride;
  8.  
  9. if (lpfnsws_getContext != NULL)
  10. { SwsContext* img_convert_ctx = lpfnsws_getContext( srcWidth, srcHeight, src_pix_fmt, dstWidth, dstHeight, dst_pix_fmt, SWS_BICUBIC, NULL, NULL, NULL);
  11. if (img_convert_ctx != NULL)
  12. { if (lpfnsws_scale!=NULL)
  13. { result = lpfnsws_scale( img_convert_ctx, (const uint8_t* const*)src->data, src->linesize, 0, srcHeight, dst->data, dst->linesize);
  14. }
  15.  
  16. if (lpfnsws_freeContext != NULL)
  17. { lpfnsws_freeContext(img_convert_ctx);
  18. }
  19. }
  20. }
  21. return result;
  22. }
  23.  
  24. MyClass Members:
  25.  
  26. AVCodecContext* m_lpCodecCtx;
  27. AVCodec* m_lpCodec;
  28. AVFrame* m_lpFrame;
  29. AVPacket m_lpPacket;
  30. AVPicture m_lpPict;
  31.  
  32.  
  33.  
  34. MyClass::Decompress(LPBYTE lpFrameData, DWORD dwFrameDataSize, DWORD dwHeight, DWORD dwWidth)
  35. {
  36. INT iFrameFinished = 0;
  37. INT iResult = 0;
  38. AVDictionary* optionsDict = NULL;
  39.  
  40. // Init decoder if needed
  41. if (m_lpCodec==NULL)
  42. if (lpStreamingImageHeader->dwFlags == AVIIF_KEYFRAME)
  43. { // Find decoder H264
  44. if (lpfnavcodec_find_decoder != NULL)
  45. { m_lpCodec = lpfnavcodec_find_decoder(CODEC_ID_H264);
  46. }
  47. }
  48. // If find decoder and no context...
  49. if ((m_lpCodec != NULL)&&(m_lpCodecCtx == NULL))
  50. { // Allocate CodecContext
  51. if (lpfnavcodec_alloc_context3!=NULL)
  52. { m_lpCodecCtx = lpfnavcodec_alloc_context3(m_lpCodec);
  53. }
  54. if (m_lpCodecCtx != NULL)
  55. { // Init CodecContext
  56. if (m_lpCodecCtx->width == 0)
  57. { m_lpCodecCtx->pix_fmt = AV_PIX_FMT_YUV420P;
  58. m_lpCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;
  59. m_lpCodecCtx->codec_id = CODEC_ID_H264;
  60. m_lpCodecCtx->coded_width = lpStreamingImageHeader->iImageWidth;
  61. m_lpCodecCtx->coded_height = lpStreamingImageHeader->iImageHeight;
  62. m_lpCodecCtx->width = lpStreamingImageHeader->iImageWidth;
  63. m_lpCodecCtx->height = lpStreamingImageHeader->iImageHeight;
  64. }
  65. }
  66. }
  67.  
  68. // If decoder and context initialized...
  69. // Open decoder
  70. if ((m_lpCodecCtx!=NULL) && (m_lpCodec!=NULL)&&(!lpfnavcodec_is_open(m_lpCodecCtx)))
  71. { if ( m_lpCodec != NULL)
  72. { if(m_lpCodec->capabilities&CODEC_CAP_TRUNCATED)
  73. m_lpCodecCtx->flags|= CODEC_FLAG_TRUNCATED;
  74. // Ouvrir le décodeur
  75. if (lpfnavcodec_open2!=NULL)
  76. { iResult = lpfnavcodec_open2( m_lpCodecCtx, m_lpCodec, &optionsDict);
  77. }
  78. // If error on open
  79. if (iResult < 0)
  80. { // On libère le contexte
  81. if (m_lpCodecCtx != NULL)
  82. { lpfnavcodec_close(m_lpCodecCtx);
  83. lpfnav_free(m_lpCodecCtx);
  84. }
  85. m_lpCodecCtx = NULL;
  86. m_lpCodec = NULL;
  87. }
  88. }
  89. }
  90. // If all OK
  91. if ((m_lpCodecCtx!=NULL) && (m_lpCodec!=NULL)&&(lpfnavcodec_is_open(m_lpCodecCtx)))
  92. { // Allocate video frame
  93. if (m_lpFrame == NULL)
  94. { if (lpfnavcodec_alloc_frame != NULL)
  95. { m_lpFrame = lpfnavcodec_alloc_frame();
  96. }
  97. }
  98. // if allocation OK
  99. if (m_lpFrame!=NULL)
  100. { // Set Size
  101. m_lpFrame->width = m_lpCodecCtx->width;
  102. m_lpFrame->height = m_lpCodecCtx->height;
  103. }
  104. if ((m_lpCodecCtx != NULL) && ( m_lpCodec != NULL) &&(m_lpFrame != NULL) && (pddsDesc != NULL))
  105. if ((dwWidth>0)&&(dwHeight>0))
  106. { int iRes = 0;
  107. // Init packet
  108. if (lpfnav_init_packet !=NULL)
  109. { lpfnav_init_packet(&m_lpPacket);
  110. }
  111. // Set Packet data
  112. m_lpPacket.data = lpImageData;
  113. m_lpPacket.size = dwImageSize;
  114.  
  115. // Decode H264 frame
  116. if ((m_lpFrame!=NULL)&&(m_lpPacket.size>0)&&(lpfnavcodec_decode_video2!=NULL))
  117. { while (m_lpPacket.size>0)
  118. { iRes = lpfnavcodec_decode_video2( m_lpCodecCtx, m_lpFrame, &iFrameFinished, &m_lpPacket);
  119. if (iRes<0)
  120. { break;
  121. }
  122. if (iRes>0)
  123. { m_lpPacket.data+=iRes;
  124. m_lpPacket.size-=iRes;
  125. }
  126. }
  127.  
  128. }
  129.  
  130. // if decoded...
  131. if (iFrameFinished)
  132. { // Fill buffer
  133. if (lpfnavpicture_fill!=NULL)
  134. { lpfnavpicture_fill( &m_lpPict, lpDIBData, AV_PIX_FMT_RGB32, dwWidth, dwHeight);
  135. }
  136. // Convert the image into RGB and copy to the surface.
  137. img_convert( &m_lpPict, AV_PIX_FMT_RGB32, dwWidth, dwHeight, (AVPicture*)m_lpFrame, AV_PIX_FMT_YUV420P, m_lpCodecCtx->width, m_lpCodecCtx->height, pddsDesc->dwLinearSize);
  138. }
  139. // Free Packet
  140. if (lpfnav_free_packet!=NULL)
  141. { lpfnav_free_packet(&m_lpPacket);
  142. }
  143. }
  144. }
  145. }
  146.  
  147.  
  148. MyClass::FFMpegStopDecompress()
  149. {
  150. // Close the codec context
  151. if (m_lpCodecCtx != NULL)
  152. { if (lpfnavcodec_flush_buffers!= NULL)
  153. { lpfnavcodec_flush_buffers(m_lpCodecCtx);
  154. }
  155. if (lpfnavcodec_is_open!=NULL)
  156. { if (lpfnavcodec_is_open(m_lpCodecCtx)>0)
  157. { if (lpfnavcodec_close != NULL)
  158. { lpfnavcodec_close(m_lpCodecCtx);
  159. }
  160. }
  161. }
  162. if (lpfnav_free != NULL)
  163. { lpfnav_free(m_lpCodecCtx);
  164. }
  165. }
  166.  
  167. // Free YUV
  168. if (m_lpFrame != NULL)
  169. { if (lpfnav_free!=NULL)
  170. { lpfnav_free(m_lpFrame);
  171. }
  172. }
  173. m_lpFrame = NULL;
  174. m_lpCodec = NULL;
  175. m_lpCodecCtx = NULL;
  176. memset( &m_lpPict, 0, sizeof(AVPicture));
  177. memset( &m_lpPacket,0, sizeof(AVPacket));
  178.  
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement