2017-08-28 1 views
0

J'ai fait la conversion entre l'avframe de FFmpeg et le mat d'OpenCV. Mais le code suivant ne convertit pas correctement le format mat en format avframe. La première partie convertit le format avframe en format mat et la seconde partie convertit le format mat en format avframe.Comment résoudre l'erreur lors de la conversion entre l'avframe de FFmpeg et le mat d'OpenCV?

Voici mon code source:

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

Veuillez préciser ce qui ne fonctionne pas. Est-ce que la conversion vers ou à partir de cela ne fonctionne pas? Quelle est l'erreur exactement? S'il vous plaît fournir un morceau de code minimal et complet qui peut être compilé et exécuté. –

+1

* "pas pareil" *? Noir? Mauvaises couleurs? Des formes déformées? Des pièces manquantes? Junk totalement méconnaissable? Injouable? –

+0

La vidéo produite n'est pas la même que la vidéo d'origine. – md612

Répondre

0

Enfin, je retire la déclaration suppr pour le faire fonctionner.

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; 
}