Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Variables:
- AVPixelFormat m_AVPixelFormat;
- AVCodecContext* m_lpCodecCtx;
- AVCodec* m_lpCodec;
- AVFrame* m_lpFrame;
- AVPacket m_lpPacket;
- AVPicture m_lpPict;
- SwsContext* m_lpSwsContext;
- INT m_iThreadCount;
- LPBYTE m_lpFFMPEGBuffer;
- INT iImageWidth, iImageHeigh;
- BOOL FFMpegDecompress( LPBYTE lpImageData, DWORD dwImageSize)
- { if (m_lpCodec==NULL)
- { // find_decoder
- m_lpCodec = avcodec_find_decoder(CODEC_ID_H264);
- }
- if ((m_lpCodec != NULL)&&(m_lpCodecCtx == NULL))
- { // Alloc codecContext
- m_lpCodecCtx = avcodec_alloc_context3(m_lpCodec);
- if (m_lpCodecCtx != NULL)
- { // 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;
- m_lpCodecCtx->coded_height = iImageHeight;
- m_lpCodecCtx->width = iImageWidth;
- m_lpCodecCtx->height = iImageHeight;
- m_lpCodecCtx->thread_count = 3;
- m_lpCodecCtx->thread_type = FF_THREAD_SLICE;
- m_lpCodecCtx->err_recognition = AV_EF_EXPLODE;
- }
- }
- }
- dwWidth = iImageWidth;
- dwHeight = iImageHeight;
- // open decoder if not...
- if ((m_lpCodecCtx!=NULL) && (m_lpCodec!=NULL)&&(!lpfnavcodec_is_open(m_lpCodecCtx)))
- { if ( m_lpCodec != NULL)
- { if(m_lpCodec->capabilities&CODEC_CAP_TRUNCATED)
- m_lpCodecCtx->flags|= CODEC_FLAG_TRUNCATED;
- iResult = avcodec_open2( m_lpCodecCtx, m_lpCodec, &optionsDict);
- // if error
- if (iResult < 0)
- { avcodec_close(m_lpCodecCtx);
- av_free(m_lpCodecCtx);
- m_lpCodecCtx = NULL;
- m_lpCodec = NULL;
- }
- }
- }
- if ((m_lpCodecCtx!=NULL) && (m_lpCodec!=NULL))
- { // Allouer la frame video
- if (m_lpFrame == NULL)
- { m_lpFrame = avcodec_alloc_frame();
- }
- // Si l'allocation à réussi
- if (m_lpFrame!=NULL)
- { // Affecter les dimensions
- m_lpFrame->width = m_lpCodecCtx->width;
- m_lpFrame->height = m_lpCodecCtx->height;
- }
- if ((m_lpCodecCtx != NULL) && ( m_lpCodec != NULL) &&(m_lpFrame != NULL) && (pddsDesc != NULL))
- if ((dwWidth>0)&&(dwHeight>0))
- { int iRes = 0;
- // init packet
- av_init_packet(&m_lpPacket);
- 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 data
- m_lpPacket.data = m_lpFFMPEGBuffer;
- // Set size
- m_lpPacket.size = dwImageSize;
- // Decode H264
- if ((m_lpFrame!=NULL)&&(m_lpPacket.size>0))
- { while (m_lpPacket.size>0)
- { iRes = lpfnavcodec_decode_video2( m_lpCodecCtx, m_lpFrame, &iFrameFinished, &m_lpPacket);
- if (iRes<0)
- { break;
- }
- if (iRes>0)
- { m_lpPacket.data+=iRes;
- m_lpPacket.size-=iRes;
- }
- }
- }
- // free Packet
- av_free_packet(&m_lpPacket);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement