Advertisement
Guest User

Untitled

a guest
Feb 13th, 2013
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1.  
  2. Part of my class code (one class used by decompression):
  3.  
  4.  
  5.  
  6. Define the lockmgr callback function:
  7.  
  8.  
  9.  
  10. static int ff_lockmgr(void **mutex, enum AVLockOp op)
  11.  
  12. // Callback
  13.  
  14. { if (NULL == mutex)
  15.  
  16. return -1;
  17.  
  18.  
  19.  
  20. CRITICAL_SECTION **cSec = (CRITICAL_SECTION **)mutex;
  21.  
  22.  
  23.  
  24. switch (op)
  25.  
  26. { case AV_LOCK_CREATE:
  27.  
  28. { *cSec = NULL;
  29.  
  30. *cSec = new CRITICAL_SECTION();
  31.  
  32. if (*cSec == NULL) return 1;
  33.  
  34. InitializeCriticalSection(*cSec);
  35.  
  36. return 0;
  37.  
  38. }
  39.  
  40. case AV_LOCK_OBTAIN:
  41.  
  42. { if (*cSec == NULL) return 1;
  43.  
  44. EnterCriticalSection(*cSec);
  45.  
  46. return 0;
  47.  
  48. }
  49.  
  50. case AV_LOCK_RELEASE:
  51.  
  52. { if (*cSec == NULL) return 1;
  53.  
  54. LeaveCriticalSection(*cSec);
  55.  
  56. return 0;
  57.  
  58. }
  59.  
  60. case AV_LOCK_DESTROY:
  61.  
  62. { if (*cSec == NULL) return 1;
  63.  
  64. DeleteCriticalSection(*cSec);
  65.  
  66. delete *cSec;
  67.  
  68. *cSec = NULL;
  69.  
  70. return 0;
  71.  
  72. }
  73.  
  74. }
  75.  
  76. return 0;
  77.  
  78. }
  79.  
  80.  
  81.  
  82. At the class constructor :
  83.  
  84. Just call once using a global check value :
  85.  
  86.  
  87.  
  88. av_register_all();
  89.  
  90. av_lockmgr_register(ff_lockmgr);
  91.  
  92.  
  93.  
  94. decoding part (call each once per frame):
  95.  
  96.  
  97.  
  98. if (m_lpCodecCtx ==NULL)
  99.  
  100. { m_lpCodecCtx = avcodec_alloc_context3(NULL);
  101.  
  102. avcodec_get_context_defaults(m_lpCodecCtx);
  103.  
  104. }
  105.  
  106. // Init CodecContext
  107.  
  108. if (m_lpCodecCtx->width == 0)
  109.  
  110. { m_lpCodecCtx->pix_fmt = AV_PIX_FMT_YUV420P;
  111.  
  112. m_lpCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;
  113.  
  114. m_lpCodecCtx->codec_id = CODEC_ID_H264;
  115.  
  116. m_lpCodecCtx->coded_width = iImageWidth; // values I already got from my camera stream
  117.  
  118. m_lpCodecCtx->coded_height = iImageHeight; // values I already got from my camera stream
  119.  
  120. m_lpCodecCtx->width = iImageWidth;
  121.  
  122. m_lpCodecCtx->height = iImageHeight;
  123.  
  124.  
  125.  
  126. if (m_lpCodecCtx->codec_id == AV_CODEC_ID_H264)
  127.  
  128. { m_lpCodecCtx->thread_count = 1;
  129.  
  130. m_lpCodecCtx->thread_type = FF_THREAD_SLICE|FF_THREAD_FRAME;
  131.  
  132.  
  133.  
  134. }
  135.  
  136.  
  137.  
  138. if (m_lpCodec==NULL)
  139.  
  140. { m_lpCodec = avcodec_find_decoder(m_lpCodecCtx->codec_id);
  141.  
  142.  
  143.  
  144. if(m_lpCodec->capabilities&CODEC_CAP_TRUNCATED) m_lpCodecCtx->flags|= CODEC_FLAG_TRUNCATED;
  145.  
  146.  
  147.  
  148. avcodec_open2(m_lpCodecCtx, m_lpCodec, NULL);
  149.  
  150. m_lpFrame = lpfnavcodec_alloc_frame();
  151.  
  152.  
  153.  
  154. m_lpFrame->width = m_lpCodecCtx->width;
  155.  
  156. m_lpFrame->height = m_lpCodecCtx->height;
  157.  
  158. }
  159.  
  160.  
  161.  
  162. // Prepare the packet to decode
  163.  
  164. av_init_packet(&m_lpPacket);
  165.  
  166.  
  167.  
  168. // Set the INPUT BUFFER PADDING
  169.  
  170. if (m_lpFFMPEGBuffer == NULL)
  171.  
  172. { m_lpFFMPEGBuffer = new BYTE[MAX_MPEG4VIDEO_FRAMESIZE+FF_INPUT_BUFFER_PADDING_SIZE];
  173.  
  174. }
  175.  
  176. if (m_lpFFMPEGBuffer !=NULL)
  177.  
  178. { ZeroMemory( m_lpFFMPEGBuffer, MAX_MPEG4VIDEO_FRAMESIZE+FF_INPUT_BUFFER_PADDING_SIZE);
  179.  
  180. CopyMemory( m_lpFFMPEGBuffer, lpImageData, dwImageSize);
  181.  
  182. // Set the buffer to packet data
  183.  
  184. m_lpPacket.data = m_lpFFMPEGBuffer;
  185.  
  186. // Set the size to packet size
  187.  
  188. m_lpPacket.size = dwImageSize;
  189.  
  190. }
  191.  
  192.  
  193.  
  194. // Decode the frame
  195.  
  196.  
  197. do
  198.  
  199.  
  200. { iRes = avcodec_decode_video2( m_lpCodecCtx, m_lpFrame, &iFrameFinished, &m_lpPacket);
  201.  
  202. if (iRes<0)
  203.  
  204. { break;
  205.  
  206. }
  207.  
  208. if (iRes>0)
  209.  
  210. { m_lpPacket.data+=iRes;
  211.  
  212. m_lpPacket.size-=iRes;
  213.  
  214. }
  215.  
  216. }
  217.  
  218. while (m_lpPacket.size>0);
  219.  
  220.  
  221.  
  222. if (iFrameFinished)
  223.  
  224. ...
  225.  
  226. I display my decoded image using swscale
  227.  
  228. ...
  229.  
  230. // Free Packet
  231.  
  232. av_free_packet(&m_lpPacket);
  233.  
  234.  
  235.  
  236. When I finish my decompression work :
  237.  
  238.  
  239.  
  240. // Close the codec context
  241.  
  242. avcodec_close(m_lpCodecCtx);
  243.  
  244. av_free(m_lpCodecCtx);
  245.  
  246.  
  247.  
  248. avcodec_free_frame(&m_lpFrame);
  249.  
  250.  
  251.  
  252. if (m_lpFFMPEGBuffer != NULL)
  253.  
  254. { delete [] m_lpFFMPEGBuffer;
  255.  
  256. }
  257.  
  258. m_lpFrame = NULL;
  259.  
  260. m_lpCodec = NULL;
  261.  
  262. m_lpCodecCtx = NULL;
  263.  
  264. m_lpFFMPEGBuffer = NULL;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement