From patchwork Sat Mar 20 08:09:55 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: sharpbai X-Patchwork-Id: 26501 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 13B384486A9 for ; Sat, 20 Mar 2021 10:39:37 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id E289668A7DE; Sat, 20 Mar 2021 10:39:36 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-oi1-f171.google.com (mail-oi1-f171.google.com [209.85.167.171]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id DEA8F68A2B9 for ; Sat, 20 Mar 2021 10:39:30 +0200 (EET) Received: by mail-oi1-f171.google.com with SMTP id w70so7391975oie.0 for ; Sat, 20 Mar 2021 01:39:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=K3wy292c+2GP1B0px1arhOtOfwHd2cDl0bdBP+qhhp0=; b=E9Jshq1AmzXwtNaTqJMzknqQhHaVv2sQOzvcWAPf1OEXRRAq1x9t1GB5Ev05GhHmcP BJj73rnj+RRMQEMn1ikM6Zlwbut7PYakwhRAfadwXRKemlJyzru1ozYF3E9gyQhO2BP7 FEu9UkQzybRC/jYr7MUm0Q3l86oOdmPP+dIL4BOcZ5VvkDbYKR+aqmMl4LNRu2IApVaC orCN+sBp3bs+2/4fmr9RgzUkgGFUOBlmC35778TzUVgTwPecALZmeM//6LOGiHeSOrgs LCw1lc5HOPxKBMLikLW+J0TnH0wJZ6ARn5drDImDLSz3APLwvXrIaAfub08KTcbVBPz5 m9oA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=K3wy292c+2GP1B0px1arhOtOfwHd2cDl0bdBP+qhhp0=; b=dQA5BVwf9e8q4L02pKCZSFR2Y4AlPvwa/OYRkCSGyGZXWvq7iIpLwiskKqeopks2WR LNltgnuSdKD3VLXabj7HTavXSEvMuVuBe9+9NHTRUVqz7w8ta5+R0UosP+ivtkpGMcBi t1u6Id3dNx6xq9pBbJJr1i6aNX0zKlCpddC+KTmjsQpscap99VIxzGUKANayUNtq920o HcqIH+ajonxWmklLEXYvaw5PAULtd+EBf6NoAxBWZ1JIZvZSWc1gzI07fCs4kV/tfZ24 8rP6/NKASUp7AWcCTHx7CAZ7KURz4DmhiXGxgGuoJaL7iOX+aRnTNeNNFF+cZBff88Gh vl2w== X-Gm-Message-State: AOAM533dd/hiJVAVDbgqIguSQbSOuUyhCNai9XFyVTtexYUvYOgA1Vdn 2nbz7qfHAzU8LM+6gizvHLTONPatVwr72w== X-Google-Smtp-Source: ABdhPJzbHC9WZ18GjChfTD190rl53F7tDlGx8otL+zGtTo676s2zrwFAz5xCdUuSxp7v5v1BUcuXUw== X-Received: by 2002:a17:90b:686:: with SMTP id m6mr2525255pjz.26.1616227817251; Sat, 20 Mar 2021 01:10:17 -0700 (PDT) Received: from localhost.localdomain (ec2-18-183-238-72.ap-northeast-1.compute.amazonaws.com. [18.183.238.72]) by smtp.gmail.com with ESMTPSA id d20sm7149179pjv.47.2021.03.20.01.10.15 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 20 Mar 2021 01:10:16 -0700 (PDT) From: sharpbai To: ffmpeg-devel@ffmpeg.org Date: Sat, 20 Mar 2021 16:09:55 +0800 Message-Id: <20210320080955.97660-1-sharpbai@gmail.com> X-Mailer: git-send-email 2.31.0 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] avcodec/h264_metadata: add change pic_order_cnt_type option X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 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: sharpbai Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Some encoder set pic_order_cnt_type=0 when not using bframe, such as h264_videotoolbox. It may cause that some hardware decoder delays output frames (buffer up to 18frames), such as MediaCodec decoder before Android 11. Setting pic_order_cnt_type=2 indicates that the picture order could not be reversed, it will minimize the decoding delay on some decoder implementations. Signed-off-by: sharpbai --- libavcodec/cbs_h264.h | 1 + libavcodec/cbs_h264_syntax_template.c | 16 ++++++++++++++++ libavcodec/h264_metadata_bsf.c | 9 +++++++++ 3 files changed, 26 insertions(+) diff --git a/libavcodec/cbs_h264.h b/libavcodec/cbs_h264.h index 9f7c2a0d30..00fe5178ea 100644 --- a/libavcodec/cbs_h264.h +++ b/libavcodec/cbs_h264.h @@ -136,6 +136,7 @@ typedef struct H264RawSPS { uint8_t log2_max_frame_num_minus4; uint8_t pic_order_cnt_type; + uint8_t pic_order_cnt_type_write; uint8_t log2_max_pic_order_cnt_lsb_minus4; uint8_t delta_pic_order_always_zero_flag; int32_t offset_for_non_ref_pic; diff --git a/libavcodec/cbs_h264_syntax_template.c b/libavcodec/cbs_h264_syntax_template.c index b65460996b..4a8a8442dd 100644 --- a/libavcodec/cbs_h264_syntax_template.c +++ b/libavcodec/cbs_h264_syntax_template.c @@ -324,9 +324,17 @@ static int FUNC(sps)(CodedBitstreamContext *ctx, RWContext *rw, } ue(log2_max_frame_num_minus4, 0, 12); + #ifdef READ ue(pic_order_cnt_type, 0, 2); + #else + ue(pic_order_cnt_type_write, 0, 2); + #endif + #ifdef READ if (current->pic_order_cnt_type == 0) { + #else + if (current->pic_order_cnt_type_write == 0) { + #endif ue(log2_max_pic_order_cnt_lsb_minus4, 0, 12); } else if (current->pic_order_cnt_type == 1) { flag(delta_pic_order_always_zero_flag); @@ -1265,13 +1273,21 @@ static int FUNC(slice_header)(CodedBitstreamContext *ctx, RWContext *rw, if (idr_pic_flag) ue(idr_pic_id, 0, 65535); + #ifdef READ if (sps->pic_order_cnt_type == 0) { + #else + if (sps->pic_order_cnt_type_write == 0) { + #endif ub(sps->log2_max_pic_order_cnt_lsb_minus4 + 4, pic_order_cnt_lsb); if (pps->bottom_field_pic_order_in_frame_present_flag && !current->field_pic_flag) se(delta_pic_order_cnt_bottom, INT32_MIN + 1, INT32_MAX); + #ifdef READ } else if (sps->pic_order_cnt_type == 1) { + #else + } else if (sps->pic_order_cnt_type_write == 1) { + #endif if (!sps->delta_pic_order_always_zero_flag) { se(delta_pic_order_cnt[0], INT32_MIN + 1, INT32_MAX); if (pps->bottom_field_pic_order_in_frame_present_flag && diff --git a/libavcodec/h264_metadata_bsf.c b/libavcodec/h264_metadata_bsf.c index cef054bd65..c21e477841 100644 --- a/libavcodec/h264_metadata_bsf.c +++ b/libavcodec/h264_metadata_bsf.c @@ -59,6 +59,7 @@ typedef struct H264MetadataContext { AVRational sample_aspect_ratio; int overscan_appropriate_flag; + int pic_order_cnt_type; int video_format; int video_full_range_flag; @@ -95,6 +96,11 @@ static int h264_metadata_update_sps(AVBSFContext *bsf, int need_vui = 0; int crop_unit_x, crop_unit_y; + if (ctx->pic_order_cnt_type != -1) { + sps->pic_order_cnt_type_write = ctx->pic_order_cnt_type; + } else { + sps->pic_order_cnt_type_write = sps->pic_order_cnt_type; + } if (ctx->sample_aspect_ratio.num && ctx->sample_aspect_ratio.den) { // Table E-1. static const AVRational sar_idc[] = { @@ -689,6 +695,9 @@ static const AVOption h264_metadata_options[] = { OFFSET(overscan_appropriate_flag), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, FLAGS }, + { "pic_order_cnt_type", "Set pic_order_cnt_type", + OFFSET(pic_order_cnt_type), AV_OPT_TYPE_INT, + { .i64 = -1 }, -1, 2, FLAGS }, { "video_format", "Set video format (table E-2)", OFFSET(video_format), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 7, FLAGS},