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

帅比萌擦擦* 提交于 2019-12-02 06:31:55

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!