【问题标题】:FFMPEG scaling error-invalid dimensionFFMPEG 缩放错误-无效维度
【发布时间】:2016-10-17 12:59:29
【问题描述】:

我正在开发一个 FFMPEG 项目,现在我遇到了一个问题。

我想要做的是,将 png pic 转换为 mpeg 视频文件。我已经设法从图片中获取信息,但不知何故我无法将图片转换为 YUV 格式。它返回“0x0->0x0 是无效的缩放尺寸”。

这是我的代码:

 AVFrame *pFrame;
AVFrame *pFrameYUV;
pFrame = av_frame_alloc();
pFrameYUV = av_frame_alloc();
int numBytes;//Groesse des Bildes
uint8_t *buffer= NULL;
numBytes=avpicture_get_size(AV_PIX_FMT_RGB24, pCodecCtx->width,pCodecCtx->height); 
buffer=(uint8_t *)av_malloc(numBytes*sizeof(uint8_t)); 

/*avpicture_fill((AVPicture *)pFrameYUV, buffer, AV_PIX_FMT_YUV420P,
                pCodecCtx->width, pCodecCtx->height);*/
av_image_fill_arrays(pFrameYUV->data,pFrameYUV->linesize,buffer,AV_PIX_FMT_YUV420P,pCodecCtx->width,pCodecCtx->height,32);


struct SwsContext *sws_ctx = NULL;

AVPacket packet;
// initialize SWS context for software scaling
sws_ctx=sws_getCachedContext(NULL,pFrame->width,pFrame->height,AV_PIX_FMT_RGB24,pFrameYUV->width,pFrameYUV->height,AV_PIX_FMT_YUV420P,0,0,0,0);
pFrameYUV->height= pFrame->height;
pFrameYUV->width= pFrame->width;

while (av_read_frame(pFormatCtx,&packet)>=0)
{
    if(packet.stream_index == videoStream)
    {
        avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet); 

        if(frameFinished)
        {

        sws_scale(sws_ctx, (uint8_t const * const *)pFrame->data,
          pFrame->linesize, 0, pCodecCtx->height,
          pFrameYUV->data, pFrameYUV->linesize);
        printf("%d",pFrameYUV->height);

    }
    }
    av_free_packet(&packet);
}

编辑:

转换后,我尝试将帧编码为一个数据包,但数据包的大小为 0。 代码

AVPacket pkt;
av_init_packet(&pkt);
pkt.stream_index= st->index;
pkt.data= buffer;
pkt.size=numBytes;
int got_pkt;
test=avcodec_encode_video2(st->codec,&pkt,pFrameYUV,&got_pkt);

printf("%d",got_pkt);

【问题讨论】:

    标签: c++ ffmpeg libavcodec swscale


    【解决方案1】:

    这些值pFrameYUV->height, pFrame->height, pFrameYUV->width, pFrame->width 没有在调用sws_getCachedContext 时设置。

    你的意思是维度没有变化吗?如果是这样,请将它们设置在 sws_getCachedContext 之前。

    pFrameYUV->height = pFrame->height = pCodecCtx->height;
    pFrameYUV->width = pFrame->width = pCodecCtx->width;
    sws_ctx=sws_getCachedContext(NULL,pFrame->width,pFrame->height,AV_PIX_FMT_RGB24,pFrameYUV->width,pFrameYUV->height,AV_PIX_FMT_YUV420P,0,0,0,0);
    

    【讨论】:

    • 我以为那些是用 av_image_fill_Arrays 设置的?我还必须设置 Frame 的 pix fmt 吗?
    • 是的,为了安全起见,您也应该设置pix_fmt。
    • 嘿,它有效。但是 pFrameYUV 的数据字段在缩放后仍然是空的。如何设置?
    • sws_scale 将为您填写。您应该检查返回值avcodec_decode_video2 和frameFinished 以确保帧解码成功。
    • 解码成功,Frame结束但pFrameYUV的data字段还是空
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-17
    • 2018-06-21
    • 1970-01-01
    • 1970-01-01
    • 2021-11-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多