2017-04-03 251 views
1

我想使用ffmpeg的sws_scale函数将AVFrame从JPEG(YUV像素格式)转换为RGB24格式。我设置了SwsContext如下:ffmpeg的分段错误sws_scale

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

,给了我一个段错误,虽然我不知道为什么。我已经尝试通过打印和高度和线条化检查值,并且所有内容都显示为有效值。

+1

乍一看,一切似乎都正确。如果'pFrameRGB-> linesize [0]'至少比'pCodecCtx-> width'大3倍,并且'pFrameRGB-> data'包含3个带有YUV(420P?)数据的平面, > data'包含至少为'pFrameRGB-> linesize [0] * pCodecCtx-> height'大的1个平面。如果一切正常,那么看看段落错误是在读取值还是写入时发生(至少你会知道它是源帧还是目的帧)。 –

回答

1

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

+1

你碰巧知道'sws'代表什么? – zanbri

+0

@ zanbri恐怕我没有,对不起! –