Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Part of my class code (one class used by decompression):
- Define the lockmgr callback function:
- static int ff_lockmgr(void **mutex, enum AVLockOp op)
- // Callback
- { if (NULL == mutex)
- return -1;
- CRITICAL_SECTION **cSec = (CRITICAL_SECTION **)mutex;
- switch (op)
- { case AV_LOCK_CREATE:
- { *cSec = NULL;
- *cSec = new CRITICAL_SECTION();
- if (*cSec == NULL) return 1;
- InitializeCriticalSection(*cSec);
- return 0;
- }
- case AV_LOCK_OBTAIN:
- { if (*cSec == NULL) return 1;
- EnterCriticalSection(*cSec);
- return 0;
- }
- case AV_LOCK_RELEASE:
- { if (*cSec == NULL) return 1;
- LeaveCriticalSection(*cSec);
- return 0;
- }
- case AV_LOCK_DESTROY:
- { if (*cSec == NULL) return 1;
- DeleteCriticalSection(*cSec);
- delete *cSec;
- *cSec = NULL;
- return 0;
- }
- }
- return 0;
- }
- At the class constructor :
- Just call once using a global check value :
- av_register_all();
- av_lockmgr_register(ff_lockmgr);
- decoding part (call each once per frame):
- if (m_lpCodecCtx ==NULL)
- { m_lpCodecCtx = avcodec_alloc_context3(NULL);
- avcodec_get_context_defaults(m_lpCodecCtx);
- }
- // Init CodecContext
- if (m_lpCodecCtx->width == 0)
- { m_lpCodecCtx->pix_fmt = AV_PIX_FMT_YUV420P;
- m_lpCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;
- m_lpCodecCtx->codec_id = CODEC_ID_H264;
- m_lpCodecCtx->coded_width = iImageWidth; // values I already got from my camera stream
- m_lpCodecCtx->coded_height = iImageHeight; // values I already got from my camera stream
- m_lpCodecCtx->width = iImageWidth;
- m_lpCodecCtx->height = iImageHeight;
- if (m_lpCodecCtx->codec_id == AV_CODEC_ID_H264)
- { m_lpCodecCtx->thread_count = 1;
- m_lpCodecCtx->thread_type = FF_THREAD_SLICE|FF_THREAD_FRAME;
- }
- if (m_lpCodec==NULL)
- { m_lpCodec = avcodec_find_decoder(m_lpCodecCtx->codec_id);
- if(m_lpCodec->capabilities&CODEC_CAP_TRUNCATED) m_lpCodecCtx->flags|= CODEC_FLAG_TRUNCATED;
- avcodec_open2(m_lpCodecCtx, m_lpCodec, NULL);
- m_lpFrame = lpfnavcodec_alloc_frame();
- m_lpFrame->width = m_lpCodecCtx->width;
- m_lpFrame->height = m_lpCodecCtx->height;
- }
- // Prepare the packet to decode
- av_init_packet(&m_lpPacket);
- // Set the INPUT BUFFER PADDING
- if (m_lpFFMPEGBuffer == NULL)
- { m_lpFFMPEGBuffer = new BYTE[MAX_MPEG4VIDEO_FRAMESIZE+FF_INPUT_BUFFER_PADDING_SIZE];
- }
- if (m_lpFFMPEGBuffer !=NULL)
- { ZeroMemory( m_lpFFMPEGBuffer, MAX_MPEG4VIDEO_FRAMESIZE+FF_INPUT_BUFFER_PADDING_SIZE);
- CopyMemory( m_lpFFMPEGBuffer, lpImageData, dwImageSize);
- // Set the buffer to packet data
- m_lpPacket.data = m_lpFFMPEGBuffer;
- // Set the size to packet size
- m_lpPacket.size = dwImageSize;
- }
- // Decode the frame
- do
- { iRes = avcodec_decode_video2( m_lpCodecCtx, m_lpFrame, &iFrameFinished, &m_lpPacket);
- if (iRes<0)
- { break;
- }
- if (iRes>0)
- { m_lpPacket.data+=iRes;
- m_lpPacket.size-=iRes;
- }
- }
- while (m_lpPacket.size>0);
- if (iFrameFinished)
- ...
- I display my decoded image using swscale
- ...
- // Free Packet
- av_free_packet(&m_lpPacket);
- When I finish my decompression work :
- // Close the codec context
- avcodec_close(m_lpCodecCtx);
- av_free(m_lpCodecCtx);
- avcodec_free_frame(&m_lpFrame);
- if (m_lpFFMPEGBuffer != NULL)
- { delete [] m_lpFFMPEGBuffer;
- }
- m_lpFrame = NULL;
- m_lpCodec = NULL;
- m_lpCodecCtx = NULL;
- m_lpFFMPEGBuffer = NULL;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement