From patchwork Tue Aug 17 19:25:31 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Niklas Haas X-Patchwork-Id: 29592 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6602:2a4a:0:0:0:0 with SMTP id k10csp3256523iov; Tue, 17 Aug 2021 12:26:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwF4JSGjsqRvsGMLrOD8IANZLTc3hLB9D5S1Mtyt1CzY6uTibvlhCpJ2dNyeXqxXMqLuA31 X-Received: by 2002:a17:906:6dcb:: with SMTP id j11mr5604196ejt.202.1629228360401; Tue, 17 Aug 2021 12:26:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629228360; cv=none; d=google.com; s=arc-20160816; b=NioFl6b85TqXZ5TyoVtQkRHX7vR02edyFmuM8SM1W6rFvskMGCj9eXevDdG8LPdKLE 1+2ItgTrYoElSRJHuBmTBGBqsczjhgGEIE3jGiyaQ2CYFJHdxO9j3UeFyoQGoYcWPL8K Tr80gGeC2vE8BBz50bvPDjeZw9iHMNNSnhxLfJzf/61KZC7QZ8uF+2v+K3aNF0vzLKv1 KiOJhffCMVNdM8LrLXyJBhj+hyoITnKe1mzDSb1q4lBTmBa7NK+Y9jNIBlpbEbyLe9S3 PHyguyQkujy4gcHocsKFSL4p0av16hUkrnXM773LDHGT3Lt7dYSzegTQ6UE+nVM6VKcZ 0DuQ== 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:message-id:date:to:from :dkim-signature:delivered-to; bh=Ei7w0D2Omd6QuiMfqsEC+uoyMTRxJ1JqAPnmywXUM2Q=; b=vgCj1xiNRWBlG+yBEbUobneUP1apyVkfI3OJlB6lmM2U/ICep0hV50SuXZJiRWCgGS uRlH4q6nTs3tTsye5IhZ0rrPVZugd47e3KHyOttZu2pxSnXSC2aCns1EAgPxn9D7fyJU gCkFUon9EfbIlRn5cKMggF8mD74iGJYEdzB92+4bf3zemf3JL71mye51zJ+MNUgMZML9 fh+r4qXjHkuivtEgP3tENxpWvxFPt9S8NuzuRh2GAVUND/GrAemmW09EBzlz4LB5Mq5a W2T67wN2bXQkjYaBLNk6kqJ7HcE5R2kU9JeM2T4e4z2UPokgLsggAuaIcp7mg2kIdquh 6hpA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@haasn.xyz header.s=mail header.b=n7XX4yHT; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id mm25si3100928ejb.521.2021.08.17.12.25.59; Tue, 17 Aug 2021 12:26:00 -0700 (PDT) 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=@haasn.xyz header.s=mail header.b=n7XX4yHT; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 4221068A4A4; Tue, 17 Aug 2021 22:25:57 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from haasn.dev (haasn.dev [78.46.187.166]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id B953D689F8F for ; Tue, 17 Aug 2021 22:25:50 +0300 (EEST) Received: from haasn.dev (unknown [10.30.0.2]) by haasn.dev (Postfix) with ESMTP id 6558147536; Tue, 17 Aug 2021 21:25:50 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=haasn.xyz; s=mail; t=1629228350; bh=Vev/sA/ELg8m4mq50apwWxCv43Irq4yx083ns3P+HS4=; h=From:To:Cc:Subject:Date:From; b=n7XX4yHT2N9k8YiOi7cxuH4SltjZll6BRteTf3nEXHIQf8lfiQscZk8l1qbpQnxQ5 78S2KgP0cmWSb+aTmDmoVyUyloUQc9G4ED56Rxj4fuTwi/oUHzAgEBGgWpaW5QEJ0s ec3ivPoFSFNDxBjT0ZUoS5SMwUbpjeExW0j2/gLI= From: Niklas Haas To: ffmpeg-devel@ffmpeg.org Date: Tue, 17 Aug 2021 21:25:31 +0200 Message-Id: <20210817192533.123050-1-ffmpeg@haasn.xyz> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2 1/3] avcodec/h264_slice: compute and export film grain seed 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: James Almer , Niklas Haas Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: p31ZC3j8sSwZ From: Niklas Haas From SMPTE RDD 5-2006, the grain seed is to be computed from the following definition of `pic_offset`: > When decoding H.264 | MPEG-4 AVC bitstreams, pic_offset is defined as > follows: > - pic_offset = PicOrderCnt(CurrPic) + (PicOrderCnt_offset << 5) > where: > - PicOrderCnt(CurrPic) is the picture order count of the current frame, > which shall be derived from [the video stream]. > > - PicOrderCnt_offset is set to idr_pic_id on IDR frames. idr_pic_id > shall be read from the slice header of [the video stream]. On non-IDR I > frames, PicOrderCnt_offset is set to 0. A frame shall be classified as I > frame when all its slices are I slices, which may be optionally > designated by setting primary_pic_type to 0 in the access delimiter NAL > unit. Otherwise, PicOrderCnt_offset it not changed. PicOrderCnt_offset is > updated in decoding order. Co-authored-by: James Almer Signed-off-by: Niklas Haas --- libavcodec/h264_slice.c | 9 ++++++++- libavcodec/h264dec.c | 14 ++++++++++++++ libavcodec/h264dec.h | 7 +++++++ libavutil/film_grain_params.h | 3 +++ 4 files changed, 32 insertions(+), 1 deletion(-) diff --git a/libavcodec/h264_slice.c b/libavcodec/h264_slice.c index 0d7107d455..9244d2d5dd 100644 --- a/libavcodec/h264_slice.c +++ b/libavcodec/h264_slice.c @@ -406,6 +406,7 @@ int ff_h264_update_thread_context(AVCodecContext *dst, h->next_output_pic = h1->next_output_pic; h->next_outputed_poc = h1->next_outputed_poc; + h->poc_offset = h1->poc_offset; memcpy(h->mmco, h1->mmco, sizeof(h->mmco)); h->nb_mmco = h1->nb_mmco; @@ -1335,6 +1336,7 @@ static int h264_export_frame_props(H264Context *h) return AVERROR(ENOMEM); fgp->type = AV_FILM_GRAIN_PARAMS_H274; + fgp->seed = cur->poc + (h->poc_offset << 5); fgp->codec.h274.model_id = fgc->model_id; if (fgc->separate_colour_description_present_flag) { @@ -1543,6 +1545,11 @@ static int h264_field_start(H264Context *h, const H264SliceContext *sl, h->poc.delta_poc[0] = sl->delta_poc[0]; h->poc.delta_poc[1] = sl->delta_poc[1]; + if (nal->type == H264_NAL_IDR_SLICE) + h->poc_offset = sl->idr_pic_id; + else if (h->picture_intra_only) + h->poc_offset = 0; + /* Shorten frame num gaps so we don't have to allocate reference * frames just to throw them away */ if (h->poc.frame_num != h->poc.prev_frame_num) { @@ -1891,7 +1898,7 @@ static int h264_slice_header_parse(const H264Context *h, H264SliceContext *sl, } if (nal->type == H264_NAL_IDR_SLICE) - get_ue_golomb_long(&sl->gb); /* idr_pic_id */ + sl->idr_pic_id = get_ue_golomb_long(&sl->gb); sl->poc_lsb = 0; sl->delta_poc_bottom = 0; diff --git a/libavcodec/h264dec.c b/libavcodec/h264dec.c index 38f8967265..dc99ee995e 100644 --- a/libavcodec/h264dec.c +++ b/libavcodec/h264dec.c @@ -485,6 +485,8 @@ static void h264_decode_flush(AVCodecContext *avctx) static int get_last_needed_nal(H264Context *h) { int nals_needed = 0; + int slice_type = 0; + int picture_intra_only = 1; int first_slice = 0; int i, ret; @@ -516,11 +518,23 @@ static int get_last_needed_nal(H264Context *h) !first_slice || first_slice != nal->type) nals_needed = i; + slice_type = get_ue_golomb_31(&gb); + if (slice_type > 9) { + if (h->avctx->err_recognition & AV_EF_EXPLODE) + return AVERROR_INVALIDDATA; + } + if (slice_type > 4) + slice_type -= 5; + + slice_type = ff_h264_golomb_to_pict_type[slice_type]; + picture_intra_only &= (slice_type & 3) == AV_PICTURE_TYPE_I; if (!first_slice) first_slice = nal->type; } } + h->picture_intra_only = picture_intra_only; + return nals_needed; } diff --git a/libavcodec/h264dec.h b/libavcodec/h264dec.h index 125966aa04..7c419de051 100644 --- a/libavcodec/h264dec.h +++ b/libavcodec/h264dec.h @@ -331,6 +331,7 @@ typedef struct H264SliceContext { int explicit_ref_marking; int frame_num; + int idr_pic_id; int poc_lsb; int delta_poc_bottom; int delta_poc[2]; @@ -384,6 +385,11 @@ typedef struct H264Context { */ int picture_idr; + /* + * Set to 1 when the current picture contains only I slices, 0 otherwise. + */ + int picture_intra_only; + int crop_left; int crop_right; int crop_top; @@ -473,6 +479,7 @@ typedef struct H264Context { int last_pocs[MAX_DELAYED_PIC_COUNT]; H264Picture *next_output_pic; int next_outputed_poc; + int poc_offset; ///< PicOrderCnt_offset from SMPTE RDD-2006 /** * memory management control operations buffer. diff --git a/libavutil/film_grain_params.h b/libavutil/film_grain_params.h index 7350dfc5b8..f3bd0a4a6a 100644 --- a/libavutil/film_grain_params.h +++ b/libavutil/film_grain_params.h @@ -221,6 +221,9 @@ typedef struct AVFilmGrainParams { /** * Seed to use for the synthesis process, if the codec allows for it. + * + * @note For H.264, this refers to `pic_offset` as defined in + * SMPTE RDD 5-2006. */ uint64_t seed;