From b2d8aa7d8ad754f9e4a203c6931476335f0e15b6 Mon Sep 17 00:00:00 2001 From: sw.multimedia Date: Wed, 26 Jan 2022 16:40:10 +0800 Subject: [PATCH] ffmpeg: fix omx decode setting pix_fmt bug --- libavcodec/omxdec.c | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/libavcodec/omxdec.c b/libavcodec/omxdec.c index 27a7949..f5da60d 100755 --- a/libavcodec/omxdec.c +++ b/libavcodec/omxdec.c @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -79,6 +80,8 @@ static int64_t from_omx_ticks(OMX_TICKS value) return AVERROR_UNKNOWN; \ } \ } while (0) + +#define FF_ARRAY_ELEMS(a) (sizeof(a) / sizeof((a)[0])) typedef struct OMXContext { void *lib; @@ -93,6 +96,37 @@ typedef struct OMXContext { void (*host_init)(void); } OMXContext; + +static const struct { + + int color_format; + enum AVPixelFormat pix_fmt; + +} color_formats[] = { + + { OMX_COLOR_FormatYUV420Planar, AV_PIX_FMT_YUV420P }, + { OMX_COLOR_FormatYUV420SemiPlanar, AV_PIX_FMT_NV12 }, + { OMX_COLOR_FormatYUV420PackedSemiPlanar, AV_PIX_FMT_NV21 }, + { 0 } +}; + +static enum AVPixelFormat omx_map_color_format(AVCodecContext *avctx, int color_format) +{ + int i; + enum AVPixelFormat ret = AV_PIX_FMT_NONE; + + for (i = 0; i < FF_ARRAY_ELEMS(color_formats); i++) { + if (color_formats[i].color_format == color_format) { + return color_formats[i].pix_fmt; + } + } + + av_log(avctx, AV_LOG_ERROR, "Output color format 0x%x (value=%d) is not supported\n", + color_format, color_format); + + return ret; +} + static av_cold void *dlsym_prefixed(void *handle, const char *symbol, const char *prefix) { char buf[50]; @@ -635,6 +669,7 @@ static av_cold void cleanup(OMXCodecContext *s) av_freep(&s->output_buf); } + static av_cold int omx_decode_init(AVCodecContext *avctx) { OMXCodecContext *s = avctx->priv_data; @@ -782,10 +817,9 @@ static int omx_decode_frame(AVCodecContext *avctx, void *data, } avctx->width = dec_out_width; avctx->height = dec_out_height; - avctx->pix_fmt = AV_PIX_FMT_YUV420P; + avctx->pix_fmt = omx_map_color_format(avctx, dec_pix_fmt); s->stride = avctx->width; s->plane_size = avctx->height; - //avctx->pix_fmt = dec_pix_fmt; if (buffer->nFlags & OMX_BUFFERFLAG_EOS) s->got_eos = 1; -- 2.17.1