Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //////////////////////////////////////////////////////////////////////////////////////////////////////
- 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)
- //////////////////////////////////////////////////////////////////////////////////////////////////////
- { int result = 0;
- // Init linesize on the output stride
- if (dst->linesize[0] == 0) dst->linesize[0] = dwStride;
- if (lpfnsws_getContext != NULL)
- { SwsContext* img_convert_ctx = lpfnsws_getContext( srcWidth, srcHeight, src_pix_fmt, dstWidth, dstHeight, dst_pix_fmt, SWS_BICUBIC, NULL, NULL, NULL);
- if (img_convert_ctx != NULL)
- { if (lpfnsws_scale!=NULL)
- { result = lpfnsws_scale( img_convert_ctx, (const uint8_t* const*)src->data, src->linesize, 0, srcHeight, dst->data, dst->linesize);
- }
- if (lpfnsws_freeContext != NULL)
- { lpfnsws_freeContext(img_convert_ctx);
- }
- }
- }
- return result;
- }
- MyClass Members:
- AVCodecContext* m_lpCodecCtx;
- AVCodec* m_lpCodec;
- AVFrame* m_lpFrame;
- AVPacket m_lpPacket;
- AVPicture m_lpPict;
- MyClass::Decompress(LPBYTE lpFrameData, DWORD dwFrameDataSize, DWORD dwHeight, DWORD dwWidth)
- {
- INT iFrameFinished = 0;
- INT iResult = 0;
- AVDictionary* optionsDict = NULL;
- // Init decoder if needed
- if (m_lpCodec==NULL)
- if (lpStreamingImageHeader->dwFlags == AVIIF_KEYFRAME)
- { // Find decoder H264
- if (lpfnavcodec_find_decoder != NULL)
- { m_lpCodec = lpfnavcodec_find_decoder(CODEC_ID_H264);
- }
- }
- // If find decoder and no context...
- if ((m_lpCodec != NULL)&&(m_lpCodecCtx == NULL))
- { // Allocate CodecContext
- if (lpfnavcodec_alloc_context3!=NULL)
- { m_lpCodecCtx = lpfnavcodec_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 = lpStreamingImageHeader->iImageWidth;
- m_lpCodecCtx->coded_height = lpStreamingImageHeader->iImageHeight;
- m_lpCodecCtx->width = lpStreamingImageHeader->iImageWidth;
- m_lpCodecCtx->height = lpStreamingImageHeader->iImageHeight;
- }
- }
- }
- // If decoder and context initialized...
- // Open decoder
- 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;
- // Ouvrir le décodeur
- if (lpfnavcodec_open2!=NULL)
- { iResult = lpfnavcodec_open2( m_lpCodecCtx, m_lpCodec, &optionsDict);
- }
- // If error on open
- if (iResult < 0)
- { // On libère le contexte
- if (m_lpCodecCtx != NULL)
- { lpfnavcodec_close(m_lpCodecCtx);
- lpfnav_free(m_lpCodecCtx);
- }
- m_lpCodecCtx = NULL;
- m_lpCodec = NULL;
- }
- }
- }
- // If all OK
- if ((m_lpCodecCtx!=NULL) && (m_lpCodec!=NULL)&&(lpfnavcodec_is_open(m_lpCodecCtx)))
- { // Allocate video frame
- if (m_lpFrame == NULL)
- { if (lpfnavcodec_alloc_frame != NULL)
- { m_lpFrame = lpfnavcodec_alloc_frame();
- }
- }
- // if allocation OK
- if (m_lpFrame!=NULL)
- { // Set Size
- 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
- if (lpfnav_init_packet !=NULL)
- { lpfnav_init_packet(&m_lpPacket);
- }
- // Set Packet data
- m_lpPacket.data = lpImageData;
- m_lpPacket.size = dwImageSize;
- // Decode H264 frame
- if ((m_lpFrame!=NULL)&&(m_lpPacket.size>0)&&(lpfnavcodec_decode_video2!=NULL))
- { 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;
- }
- }
- }
- // if decoded...
- if (iFrameFinished)
- { // Fill buffer
- if (lpfnavpicture_fill!=NULL)
- { lpfnavpicture_fill( &m_lpPict, lpDIBData, AV_PIX_FMT_RGB32, dwWidth, dwHeight);
- }
- // Convert the image into RGB and copy to the surface.
- 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);
- }
- // Free Packet
- if (lpfnav_free_packet!=NULL)
- { lpfnav_free_packet(&m_lpPacket);
- }
- }
- }
- }
- MyClass::FFMpegStopDecompress()
- {
- // Close the codec context
- if (m_lpCodecCtx != NULL)
- { if (lpfnavcodec_flush_buffers!= NULL)
- { lpfnavcodec_flush_buffers(m_lpCodecCtx);
- }
- if (lpfnavcodec_is_open!=NULL)
- { if (lpfnavcodec_is_open(m_lpCodecCtx)>0)
- { if (lpfnavcodec_close != NULL)
- { lpfnavcodec_close(m_lpCodecCtx);
- }
- }
- }
- if (lpfnav_free != NULL)
- { lpfnav_free(m_lpCodecCtx);
- }
- }
- // Free YUV
- if (m_lpFrame != NULL)
- { if (lpfnav_free!=NULL)
- { lpfnav_free(m_lpFrame);
- }
- }
- m_lpFrame = NULL;
- m_lpCodec = NULL;
- m_lpCodecCtx = NULL;
- memset( &m_lpPict, 0, sizeof(AVPicture));
- memset( &m_lpPacket,0, sizeof(AVPacket));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement