From patchwork Fri Mar 10 20:48:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhao Zhili X-Patchwork-Id: 40642 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:d046:b0:cd:afd7:272c with SMTP id hv6csp1153721pzb; Fri, 10 Mar 2023 04:49:36 -0800 (PST) X-Google-Smtp-Source: AK7set+Ry7C4ny+whBsB9DuNnLoWshVb2FLF/NAcd7LvQQcthF2JDm4FrujIg63r7hGQEbRV+Ih9 X-Received: by 2002:a05:6402:289:b0:4ad:7c6c:5386 with SMTP id l9-20020a056402028900b004ad7c6c5386mr22955389edv.30.1678452576798; Fri, 10 Mar 2023 04:49:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1678452576; cv=none; d=google.com; s=arc-20160816; b=dk+0yRyd16xkUTw5TRLG7Es0ifIKfmiQywj7uig3heJsLi3mhTHHC+/SABm1TYtpmz uxbqvNe1A2DjYmpyGs6a2AjCVlA6bOAIS29VzUFDPGWBnwQIQuzBeFeHEf9iQueJOcjw vpEcPK0PkevhIQULw9ukH/4NUvYXVjv/aHzKgvHDt7o9OiKZitV/E9mcWED0jmmEfmsk cmvlKZrDiD5ANNCJH2LQnyVtpacIVzhQYKYNDM01Sj9oqgVVTC8DZKIvMBHd3o/Pzic3 w1YbJEOgCheatOTelH9AEFApV0EUhqa3Iw5mV44NG2n1AWLAgcxNTNT0E4a40jtv3Y8z spEA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:date:to:from:message-id :dkim-signature:delivered-to; bh=LJ1ti+rOzWYkgGJfx7cs6fpTOkvXPo+obqY0oI6k0gM=; b=lDCQJreuhGQeoW0n+jjy0pg4QVRvdCyEYJSJHQ2AUf52RBTssLQUVf8hQBnOdA0e9Q Nk6iA9USiCemYZcPqgSDEz1AEgsu29z9dflKC3mmGmNzDT1WY4MAPdIq+lJF2vkjRFlp PPuuSeBMD59nxX7pOWddUw6eAreYUquERmrbtJWeItipbAU99nv8+4igPu6XNHzR0kCT c1MavGxIrT7bXt663BcLM3nxev3YP6gppM8KY3GO3fs1p7E+CFUqJ8ZZcSnHQF4Idm3k UsfVMbU8eHKnWXgKpndGSvl80E6iH3tanXbOrSTfXyspRP+waE7/g5u6u/6K0yrVYkam dlxg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@foxmail.com header.s=s201512 header.b="c/bteL/x"; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=foxmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id bm10-20020a0564020b0a00b004c30cb1f8cdsi2306067edb.224.2023.03.10.04.49.12; Fri, 10 Mar 2023 04:49:36 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@foxmail.com header.s=s201512 header.b="c/bteL/x"; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=foxmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 209246800AC; Fri, 10 Mar 2023 14:49:09 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from out162-62-57-87.mail.qq.com (out162-62-57-87.mail.qq.com [162.62.57.87]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 12D7968BC3C for ; Fri, 10 Mar 2023 14:48:59 +0200 (EET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foxmail.com; s=s201512; t=1678452531; bh=EjHToqiNypTCipF0BC1k2E3q84Q44TXm+Lj3DIxfBR4=; h=From:To:Cc:Subject:Date; b=c/bteL/xA0aRMrKXVccB9/9mtQFgiWyPrMRqOTBXFMuXbXm3w1kqh7RTBEskXk5kC jcVncR7Sg4Pu6q7576zrlvdAk6rk54ZqSMT3a9Flm4q8wfaUB+x4jgcMhVzTSZ2bAO 2XjIemLPE7EFVfTU2SueH4J2QOqEPlVNJHLJgncc= Received: from localhost.localdomain ([113.108.77.66]) by newxmesmtplogicsvrszc1-0.qq.com (NewEsmtp) with SMTP id C322E8CF; Fri, 10 Mar 2023 20:48:50 +0800 X-QQ-mid: xmsmtpt1678452530tpm902118 Message-ID: X-QQ-XMAILINFO: Mu4fz8q1FCY3u0PZ352LMv8M0YIszD7/IBgoV1+eTozyhfCcBl1spFZI8GYRSS gU+CxxDcVC7WAThPkFa0bYWRsIikIzY5Jk9KyHMLNePfpDEVejuudN4HaYujW5oGDoAPeMYumdWq icH4E5Q9bPhz7ALYLnU2svGZkBB1jbbkUIg5mObNocjpAHCTtzP8uxmomR1OUoCacl77U+iWsumX 7jmYo7fhrm8+mlQAfazocpE/YUNjVlQctHW8blriExXVnAOmVsEAfWc26GM+zDkr4Sd45cgTy9Ia abSZiZxmCqD+kkxDNFUjQkC8bhEmhxb/Zoxs94rBfQ7M7QqXjGe9WWyPvbPlm5c/kYfCIehGMm0e ITjZJkMABi2qrjiW6UnHwdVXez5lHO3zwbj8foxo6ldVWSTXjC3wTDDFz/yaigKeThlABDprJwqS PPbBKgOFBtw2J1skAJrd+Hk5zYPcc9lnFmaPNCotSrlbH8QbCcXOfqLWSzKNQmsSyu5dU7CH4Dv0 bTizyRB28QT6u/Btp/4xhfMUPBrJ4TqiaqsFEL6f5KmPR8JGEXee0B/+5VZ3JlvzW59k2sBV3FfT LatnWcg63S7dfWeVrROdEw1t2iu3OS08M29CWFITrm8UbE2CprHCmE4tFraeepDqLaje7fukqkcK gkbeGKbvwO46fblExXoH2kY3wNQ3jV+qhvwgSXgW4jQ1dBLCfR7rcXiVPKbkpmWDJ4BdhwKDOLq1 0Mq44dXyVcUIZFa7v+i9rI3imcXSlUi7qxtFYVRYriBYkDGX1huMflxPDrtgJagyVXSO1dn06jqz EhwT9ICK6g8D4vsIvi6gx0Yw5VnEoGgaP3/cHaQQfGsh+0cFyiM6SAjmKd2EE3Q630sOwFEG/OSt yagXE4XFcGz17BHwyY5nd8Yw6IFPC3JOyXzN31XSGrm/WVxfYvtzeMIDU9I3dRCcctYUEJU5smmQ fEoNFPuWTodm1mGWsP22N6MyiFv6lf From: Zhao Zhili To: ffmpeg-devel@ffmpeg.org Date: Sat, 11 Mar 2023 04:48:44 +0800 X-OQ-MSGID: <20230310204845.49878-1-quinkblack@foxmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/2] avcodec/mediacodecdec: refactor color space utils X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Zhao Zhili Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: oE2cRi9qY9hj From: Zhao Zhili So it can be shared with encoder. Signed-off-by: Zhao Zhili --- libavcodec/mediacodec_wrapper.c | 102 ++++++++++++++++++++++++++++++ libavcodec/mediacodec_wrapper.h | 73 +++++++++++++++++++++ libavcodec/mediacodecdec_common.c | 87 ++----------------------- 3 files changed, 179 insertions(+), 83 deletions(-) diff --git a/libavcodec/mediacodec_wrapper.c b/libavcodec/mediacodec_wrapper.c index 34ec2134aa..d1fb640ec2 100644 --- a/libavcodec/mediacodec_wrapper.c +++ b/libavcodec/mediacodec_wrapper.c @@ -2542,3 +2542,105 @@ int ff_Build_SDK_INT(AVCodecContext *avctx) return ret; } + +static struct { + enum FFAMediaFormatColorRange mf_range; + enum AVColorRange range; +} color_range_map[] = { + { COLOR_RANGE_FULL, AVCOL_RANGE_JPEG }, + { COLOR_RANGE_LIMITED, AVCOL_RANGE_MPEG }, +}; + +static struct { + enum FFAMediaFormatColorStandard mf_standard; + enum AVColorSpace space; +} color_space_map[] = { + { COLOR_STANDARD_BT709, AVCOL_SPC_BT709 }, + { COLOR_STANDARD_BT601_PAL, AVCOL_SPC_BT470BG }, + { COLOR_STANDARD_BT601_NTSC, AVCOL_SPC_SMPTE170M }, + { COLOR_STANDARD_BT2020, AVCOL_SPC_BT2020_NCL }, +}; + +static struct { + enum FFAMediaFormatColorStandard mf_standard; + enum AVColorPrimaries primaries; +} color_primaries_map[] = { + { COLOR_STANDARD_BT709, AVCOL_PRI_BT709 }, + { COLOR_STANDARD_BT601_PAL, AVCOL_PRI_BT470BG }, + { COLOR_STANDARD_BT601_NTSC, AVCOL_PRI_SMPTE170M }, + { COLOR_STANDARD_BT2020, AVCOL_PRI_BT2020 }, +}; + +static struct { + enum FFAMediaFormatColorTransfer mf_transfer; + enum AVColorTransferCharacteristic transfer; +} color_transfer_map[] = { + { COLOR_TRANSFER_LINEAR, AVCOL_TRC_LINEAR }, + { COLOR_TRANSFER_SDR_VIDEO, AVCOL_TRC_SMPTE170M }, + { COLOR_TRANSFER_ST2084, AVCOL_TRC_SMPTEST2084 }, + { COLOR_TRANSFER_HLG, AVCOL_TRC_ARIB_STD_B67 }, +}; + +enum AVColorRange ff_AMediaFormatColorRange_to_AVColorRange(int color_range) +{ + for (int i = 0; i < FF_ARRAY_ELEMS(color_range_map); i++) + if (color_range_map[i].mf_range == color_range) + return color_range_map[i].range; + + return AVCOL_RANGE_UNSPECIFIED; +} + +int ff_AMediaFormatColorRange_from_AVColorRange(enum AVColorRange color_range) +{ + for (int i = 0; i < FF_ARRAY_ELEMS(color_range_map); i++) + if (color_range_map[i].range == color_range) + return color_range_map[i].mf_range; + return COLOR_RANGE_UNSPECIFIED; +} + +enum AVColorSpace ff_AMediaFormatColorStandard_to_AVColorSpace(int color_standard) +{ + for (int i = 0; i < FF_ARRAY_ELEMS(color_space_map); i++) + if (color_space_map[i].mf_standard == color_standard) + return color_space_map[i].space; + + return AVCOL_SPC_UNSPECIFIED; +} + +int ff_AMediaFormatColorStandard_from_AVColorSpace(enum AVColorSpace color_space) +{ + for (int i = 0; i < FF_ARRAY_ELEMS(color_space_map); i++) + if (color_space_map[i].space == color_space) + return color_space_map[i].mf_standard; + + return COLOR_STANDARD_UNSPECIFIED; +} + +enum AVColorPrimaries ff_AMediaFormatColorStandard_to_AVColorPrimaries(int color_standard) +{ + for (int i = 0; i < FF_ARRAY_ELEMS(color_primaries_map); i++) + if (color_primaries_map[i].mf_standard == color_standard) + return color_primaries_map[i].primaries; + + return AVCOL_PRI_UNSPECIFIED; +} + +enum AVColorTransferCharacteristic +ff_AMediaFormatColorTransfer_to_AVColorTransfer(int color_transfer) +{ + for (int i = 0; i < FF_ARRAY_ELEMS(color_transfer_map); i++) + if (color_transfer_map[i].mf_transfer == color_transfer) + return color_transfer_map[i].transfer; + + return AVCOL_TRC_UNSPECIFIED; +} + +int ff_AMediaFormatColorTransfer_from_AVColorTransfer( + enum AVColorTransferCharacteristic color_transfer) +{ + for (int i = 0; i < FF_ARRAY_ELEMS(color_transfer_map); i++) + if (color_transfer_map[i].transfer == color_transfer) + return color_transfer_map[i].mf_transfer; + + return COLOR_TRANSFER_UNSPECIFIED; +} diff --git a/libavcodec/mediacodec_wrapper.h b/libavcodec/mediacodec_wrapper.h index 1b81e6db84..11a4260497 100644 --- a/libavcodec/mediacodec_wrapper.h +++ b/libavcodec/mediacodec_wrapper.h @@ -345,4 +345,77 @@ static inline int ff_AMediaCodec_signalEndOfInputStream(FFAMediaCodec *codec) int ff_Build_SDK_INT(AVCodecContext *avctx); +enum FFAMediaFormatColorRange { + COLOR_RANGE_UNSPECIFIED = 0x0, + COLOR_RANGE_FULL = 0x1, + COLOR_RANGE_LIMITED = 0x2, +}; + +enum FFAMediaFormatColorStandard { + COLOR_STANDARD_UNSPECIFIED = 0x0, + COLOR_STANDARD_BT709 = 0x1, + COLOR_STANDARD_BT601_PAL = 0x2, + COLOR_STANDARD_BT601_NTSC = 0x4, + COLOR_STANDARD_BT2020 = 0x6, +}; + +enum FFAMediaFormatColorTransfer { + COLOR_TRANSFER_UNSPECIFIED = 0x0, + COLOR_TRANSFER_LINEAR = 0x1, + COLOR_TRANSFER_SDR_VIDEO = 0x3, + COLOR_TRANSFER_ST2084 = 0x6, + COLOR_TRANSFER_HLG = 0x7, +}; + +/** + * Map MediaFormat color range to AVColorRange. + * + * return AVCOL_RANGE_UNSPECIFIED when failed. + */ +enum AVColorRange ff_AMediaFormatColorRange_to_AVColorRange(int color_range); + +/** + * Map AVColorRange to MediaFormat color range. + * + * return COLOR_RANGE_UNSPECIFIED when failed. + */ +int ff_AMediaFormatColorRange_from_AVColorRange(enum AVColorRange color_range); + +/** + * Map MediaFormat color standard to AVColorSpace. + * + * return AVCOL_SPC_UNSPECIFIED when failed. + */ +enum AVColorSpace ff_AMediaFormatColorStandard_to_AVColorSpace(int color_standard); + +/** + * Map AVColorSpace to MediaFormat color standard. + * + * return COLOR_STANDARD_UNSPECIFIED when failed. + */ +int ff_AMediaFormatColorStandard_from_AVColorSpace(enum AVColorSpace color_space); + +/** + * Map MediaFormat color standard to AVColorPrimaries. + * + * return AVCOL_PRI_UNSPECIFIED when failed. + */ +enum AVColorPrimaries ff_AMediaFormatColorStandard_to_AVColorPrimaries(int color_standard); + +/** + * Map MediaFormat color transfer to AVColorTransferCharacteristic. + * + * return AVCOL_TRC_UNSPECIFIED when failed. + */ +enum AVColorTransferCharacteristic +ff_AMediaFormatColorTransfer_to_AVColorTransfer(int color_transfer); + +/** + * Map AVColorTransferCharacteristic to MediaFormat color transfer. + * + * return COLOR_TRANSFER_UNSPECIFIED when failed. + */ +int ff_AMediaFormatColorTransfer_from_AVColorTransfer( + enum AVColorTransferCharacteristic color_transfer); + #endif /* AVCODEC_MEDIACODEC_WRAPPER_H */ diff --git a/libavcodec/mediacodecdec_common.c b/libavcodec/mediacodecdec_common.c index 03bee11918..1151bb71f9 100644 --- a/libavcodec/mediacodecdec_common.c +++ b/libavcodec/mediacodecdec_common.c @@ -85,85 +85,6 @@ #define OUTPUT_DEQUEUE_TIMEOUT_US 8000 #define OUTPUT_DEQUEUE_BLOCK_TIMEOUT_US 1000000 -enum { - COLOR_RANGE_FULL = 0x1, - COLOR_RANGE_LIMITED = 0x2, -}; - -static enum AVColorRange mcdec_get_color_range(int color_range) -{ - switch (color_range) { - case COLOR_RANGE_FULL: - return AVCOL_RANGE_JPEG; - case COLOR_RANGE_LIMITED: - return AVCOL_RANGE_MPEG; - default: - return AVCOL_RANGE_UNSPECIFIED; - } -} - -enum { - COLOR_STANDARD_BT709 = 0x1, - COLOR_STANDARD_BT601_PAL = 0x2, - COLOR_STANDARD_BT601_NTSC = 0x4, - COLOR_STANDARD_BT2020 = 0x6, -}; - -static enum AVColorSpace mcdec_get_color_space(int color_standard) -{ - switch (color_standard) { - case COLOR_STANDARD_BT709: - return AVCOL_SPC_BT709; - case COLOR_STANDARD_BT601_PAL: - return AVCOL_SPC_BT470BG; - case COLOR_STANDARD_BT601_NTSC: - return AVCOL_SPC_SMPTE170M; - case COLOR_STANDARD_BT2020: - return AVCOL_SPC_BT2020_NCL; - default: - return AVCOL_SPC_UNSPECIFIED; - } -} - -static enum AVColorPrimaries mcdec_get_color_pri(int color_standard) -{ - switch (color_standard) { - case COLOR_STANDARD_BT709: - return AVCOL_PRI_BT709; - case COLOR_STANDARD_BT601_PAL: - return AVCOL_PRI_BT470BG; - case COLOR_STANDARD_BT601_NTSC: - return AVCOL_PRI_SMPTE170M; - case COLOR_STANDARD_BT2020: - return AVCOL_PRI_BT2020; - default: - return AVCOL_PRI_UNSPECIFIED; - } -} - -enum { - COLOR_TRANSFER_LINEAR = 0x1, - COLOR_TRANSFER_SDR_VIDEO = 0x3, - COLOR_TRANSFER_ST2084 = 0x6, - COLOR_TRANSFER_HLG = 0x7, -}; - -static enum AVColorTransferCharacteristic mcdec_get_color_trc(int color_transfer) -{ - switch (color_transfer) { - case COLOR_TRANSFER_LINEAR: - return AVCOL_TRC_LINEAR; - case COLOR_TRANSFER_SDR_VIDEO: - return AVCOL_TRC_SMPTE170M; - case COLOR_TRANSFER_ST2084: - return AVCOL_TRC_SMPTEST2084; - case COLOR_TRANSFER_HLG: - return AVCOL_TRC_ARIB_STD_B67; - default: - return AVCOL_TRC_UNSPECIFIED; - } -} - enum { COLOR_FormatYUV420Planar = 0x13, COLOR_FormatYUV420SemiPlanar = 0x15, @@ -517,17 +438,17 @@ static int mediacodec_dec_parse_format(AVCodecContext *avctx, MediaCodecDecConte AMEDIAFORMAT_GET_INT32(color_range, "color-range", 0); if (color_range) - avctx->color_range = mcdec_get_color_range(color_range); + avctx->color_range = ff_AMediaFormatColorRange_to_AVColorRange(color_range); AMEDIAFORMAT_GET_INT32(color_standard, "color-standard", 0); if (color_standard) { - avctx->colorspace = mcdec_get_color_space(color_standard); - avctx->color_primaries = mcdec_get_color_pri(color_standard); + avctx->colorspace = ff_AMediaFormatColorStandard_to_AVColorSpace(color_standard); + avctx->color_primaries = ff_AMediaFormatColorStandard_to_AVColorPrimaries(color_standard); } AMEDIAFORMAT_GET_INT32(color_transfer, "color-transfer", 0); if (color_transfer) - avctx->color_trc = mcdec_get_color_trc(color_transfer); + avctx->color_trc = ff_AMediaFormatColorTransfer_to_AVColorTransfer(color_transfer); av_log(avctx, AV_LOG_INFO, "Output crop parameters top=%d bottom=%d left=%d right=%d, "