How to convert YUV420P image to JPEG using ffmpeg's libraries?

后端 未结 1 2011
走了就别回头了
走了就别回头了 2021-01-22 10:33

I\'m trying to convert a YUV420P image (AV_PIX_FMT_YUV420P) to a JPEG using ffmpeg\'s libavformat and libavcodec. This is my code so far:<

相关标签:
1条回答
  • 2021-01-22 10:57

    The following code is your issue:

    .pix_fmts       = (const enum AVPixelFormat[]){
        AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_NONE
    },
    

    As you can see, it wants AV_PIX_FMT_YUVJ420P, not YUV420P. This is an old leftover and is largely considered deprecated, but it has not yet been removed or marked for removal, and is still used in some places. The literal meaning of YUVJ420P is "YUV420P with JPEG color-range" (i.e. pixels use 0-255 range instead of 16-235 range, where 0 instead of 16 is black, and 255 instead of 235 is white). You can alternatively also do that using AV_PIX_FMT_YUV420P and then setting e.g. avctx->color_range to AVCOL_RANGE_JPEG.

    You probably don't care about that, you want to know, how can I convert AV_PIX_FMT_YUV420P to AV_PIX_FMT_YUVJ420P? Well, in this case, your input is JPEG so it probably uses JPEG range data (check that by checking avctx->color_range or avframe->color_range). Then you can simply change avframe->pix_fmt from AV_PIX_FMT_YUV420P to AV_PIX_FMT_YUVJ420P. If for whatever reason color_range is AVCOL_RANGE_MPEG, you can probably use libswscale to convert between the AV_PIX_FMT_YUV420P with MPEG-range and AV_PIX_FMT_YUVJ420P, see e.g. this example.

    0 讨论(0)
提交回复
热议问题