2017-08-28 35 views
0

我在FFmpeg的avframe和OpenCV的mat之間進行了轉換。但下面的代碼不會正確地將mat格式轉換爲avframe格式。第一部分將avframe轉換爲mat格式,第二部分將mat轉換爲avframe格式。如何解決在FFmpeg的avframe和OpenCV的mat之間轉換過程中的錯誤?

這裏是我的源代碼:

AVFrame* ProcessFrame(AVFrame *frame, int stream_index) 
{ 
//first part 
    AVStream *in_stream = ifmt_ctx->streams[stream_index]; 
    AVCodecContext *pCodecCtx = in_stream->codec; 

    AVFrame *pFrameRGB = NULL; 

    struct SwsContext * img_convert_ctx = NULL; 
    if(img_convert_ctx == NULL){ 
     img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, 
             pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, 
             AV_PIX_FMT_BGR24, SWS_BICUBIC, NULL, NULL, NULL); 
    } 
    pFrameRGB = av_frame_alloc(); 
    int size = avpicture_get_size(AV_PIX_FMT_BGR24, pCodecCtx->width, pCodecCtx->height); 

    uint8_t *out_bufferRGB = (uint8_t *)av_malloc(size); 

    avpicture_fill((AVPicture *)pFrameRGB, out_bufferRGB, AV_PIX_FMT_BGR24, pCodecCtx->width, pCodecCtx->height); 

    sws_scale(img_convert_ctx, frame->data, frame->linesize, 0, pCodecCtx->height, pFrameRGB->data, pFrameRGB->linesize); 

    Mat imageFrame = Mat(pCodecCtx->height, pCodecCtx->width, CV_8UC3, out_bufferRGB);  

    delete[] out_bufferRGB; 

    /////////////////////////////////////////////////////////// 
    //second part starts 

    avpicture_fill((AVPicture *)pFrameRGB, imageFrame.data,AV_PIX_FMT_BGR24, pCodecCtx->width, pCodecCtx->height); 

    struct SwsContext * convert_ctx = NULL; 
    if(convert_ctx == NULL){ 
     convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, 
             AV_PIX_FMT_BGR24, pCodecCtx->width, pCodecCtx->height, 
             pCodecCtx->pix_fmt, SWS_BICUBIC, NULL, NULL, NULL); 
    } 

    AVFrame *srcFrame = av_frame_alloc(); 
    size = avpicture_get_size(pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height); 

    uint8_t *out_buffer = (uint8_t *)av_malloc(size); 

    avpicture_fill((AVPicture *)srcFrame, out_buffer, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height); 

    sws_scale(convert_ctx, pFrameRGB->data, pFrameRGB->linesize, 0, pCodecCtx->height, srcFrame->data, srcFrame->linesize); 

    delete[] out_buffer; 
    av_free(pFrameRGB); 

    srcFrame->width = frame->width; 
    srcFrame->height = frame->height; 
    srcFrame->format = frame->format; 

    av_frame_copy_props(srcFrame, frame); 

    return srcFrame; 
} 
+1

請更具體地說明什麼不起作用。是轉換還是轉換不起作用?什麼是錯誤?請提供可編譯和運行的最小完整代碼。 –

+1

*「不一樣」*?黑色?錯誤的顏色?扭曲的形狀?件丟失了?完全無法辨認的垃圾?不能播放? –

+0

製作的視頻與原始視頻不一樣。 – md612

回答

0

最後,我刪除del語句,使其工作。

AVFrame* ProcessFrame(AVFrame *frame, int stream_index) 
{ 
//first part 
    AVStream *in_stream = ifmt_ctx->streams[stream_index]; 
    AVCodecContext *pCodecCtx = in_stream->codec; 

    AVFrame *pFrameRGB = NULL; 

    struct SwsContext * img_convert_ctx = NULL; 
    if(img_convert_ctx == NULL){ 
     img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, 
             pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, 
             AV_PIX_FMT_BGR24, SWS_BICUBIC, NULL, NULL, NULL); 
    } 
    pFrameRGB = av_frame_alloc(); 
    int size = avpicture_get_size(AV_PIX_FMT_BGR24, pCodecCtx->width, pCodecCtx->height); 

    uint8_t *out_bufferRGB = (uint8_t *)av_malloc(size); 

    avpicture_fill((AVPicture *)pFrameRGB, out_bufferRGB, AV_PIX_FMT_BGR24, pCodecCtx->width, pCodecCtx->height); 

    sws_scale(img_convert_ctx, frame->data, frame->linesize, 0, pCodecCtx->height, pFrameRGB->data, pFrameRGB->linesize); 

    Mat imageFrame = Mat(pCodecCtx->height, pCodecCtx->width, CV_8UC3, out_bufferRGB); 

    /////////////////////////////////////////////////////////// 
    //second part starts 

    avpicture_fill((AVPicture *)pFrameRGB, imageFrame.data,AV_PIX_FMT_BGR24, pCodecCtx->width, pCodecCtx->height); 

    struct SwsContext * convert_ctx = NULL; 
    if(convert_ctx == NULL){ 
     convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, 
             AV_PIX_FMT_BGR24, pCodecCtx->width, pCodecCtx->height, 
             pCodecCtx->pix_fmt, SWS_BICUBIC, NULL, NULL, NULL); 
    } 

    AVFrame *srcFrame = av_frame_alloc(); 
    size = avpicture_get_size(pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height); 

    uint8_t *out_buffer = (uint8_t *)av_malloc(size); 

    avpicture_fill((AVPicture *)srcFrame, out_buffer, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height); 

    sws_scale(convert_ctx, pFrameRGB->data, pFrameRGB->linesize, 0, pCodecCtx->height, srcFrame->data, srcFrame->linesize); 

    av_free(pFrameRGB); 

    srcFrame->width = frame->width; 
    srcFrame->height = frame->height; 
    srcFrame->format = frame->format; 

    av_frame_copy_props(srcFrame, frame); 

    return srcFrame; 
}