ffmpeg sws_scale上的分段错误

Segmentation fault on ffmpeg sws_scale

本文关键字:分段 错误 scale sws ffmpeg      更新时间:2023-10-16

我正在尝试使用ffmpeg的sws_scale功能将AVFrame从JPEG(YUV像素格式(转换为RGB24格式。我按如下方式设置了 SwsContext:

struct SwsContext *sws_ctx = NULL;
    int frameFinished;
    AVPacket packet;
    // initialize SWS context for software scaling
    sws_ctx = sws_getContext(pCodecCtx->width,
        pCodecCtx->height,
        pCodecCtx->pix_fmt,
        pCodecCtx->width,
        pCodecCtx->height,
        AV_PIX_FMT_RGB24,
        SWS_BICUBIC,
        NULL, NULL, NULL
        );

然后,我使用以下命令执行sws_scale

sws_scale(sws_ctx,
          (uint8_t const * const *)pFrame->data,
          pFrame->linesize,
          0,
          pCodecCtx->height,
          pFrameRGB->data,
          pFrameRGB->linesize);

这给了我一个段错误,虽然我不知道为什么。我尝试通过打印、高度和线条大小来检查值,所有内容似乎都有有效的值。

对于将来遇到此问题的任何人,我的问题是我没有正确启动pFramepFrameRGB。首先必须使用 av_frame_alloc() 为帧结构分配内存,然后必须使用 av_image_alloc() 分配数据缓冲区。