From patchwork Thu Jun 30 22:29:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 36566 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:8b27:b0:88:1bbf:7fd2 with SMTP id l39csp620893pzh; Thu, 30 Jun 2022 15:31:35 -0700 (PDT) X-Google-Smtp-Source: AGRyM1v1v16ScfQVnrSqyw/wp7z5S6MyNvKb1cXZS5z4iaTGpAjYYgnhvgc2hRN8l5cxPI3mBKcr X-Received: by 2002:a17:906:7c0c:b0:6f9:1fc:ebf3 with SMTP id t12-20020a1709067c0c00b006f901fcebf3mr10931899ejo.403.1656628295480; Thu, 30 Jun 2022 15:31:35 -0700 (PDT) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id oz37-20020a1709077da500b006f508f07fa1si375379ejc.458.2022.06.30.15.31.35; Thu, 30 Jun 2022 15:31:35 -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=@outlook.com header.s=selector1 header.b=eZVWChU6; arc=fail (body hash mismatch); 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=QUARANTINE dis=NONE) header.from=outlook.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 91EB268B7D4; Fri, 1 Jul 2022 01:30:48 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from EUR05-AM6-obe.outbound.protection.outlook.com (mail-am6eur05olkn2053.outbound.protection.outlook.com [40.92.91.53]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 4922168B7D8 for ; Fri, 1 Jul 2022 01:30:45 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=HawfdOfL5/HJiAtFsjUHPv86RDoKAQxdihUNuWizE7mfgumg0qJI90esecisKgDg1s5GFdp1R/Pzl20SHH8tQM6/gJwwyvacE188wJ9KntFD58AapKUjynYCg8arG8N9pWfBRodLQ1GKEJwuPmfn+xFo8r3D6q+hjmslLWiqSsWpReCgU8VB6DO2dpLeve2i3vE/tb0dASp8XeRlXpQ+Noi4QVT5YBiRljMkmzXo38FGbymvtLFByVuafp4SULXCEbP4MtnL7e0UN4C2prvitCciCUNirKZyqoV2IPjqjZzcYjoosY8fgXM4PoI5TEJdryQpEYpe1UTaE+3qF0vmcA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=9a/DuBPP82LeIRp7n6Kff0SKxUbXixh2toob7OYZhDo=; b=gbyaldJPgxwiypbgUbeHqEYIoIzO66rX25T9TYEGFNRiHmgERSdO9NmjmizTLvb42xNlWuq+WycYFNm1RT1+lSdOlMIeRz8R8DWRs5xqPp9ytZWhlLuHch6Go7WNmLQUM84YBk33glJwc+G2ddqNLe63S43ovEA3QvNu5/pFiGqf2IfEST5DO5S0ZVpo6xsY725S89sp/2gyYKbcYJRqGq6MHtYT/ztaY00sVQb+2MFz62Q9llezg/eU0ethP/5/DbrbpqECOQV5nQZ+QtstYvzNUu7cXs2hcy9mbfm8ogwUp03LLo+P/vGHcBZeLzbcf0nuXrte4f5Md94CldwEBA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=9a/DuBPP82LeIRp7n6Kff0SKxUbXixh2toob7OYZhDo=; b=eZVWChU6aqQmHL8Ay8aC5zm/9yge9ffc6AXiqFvZXepy2epJ3ruC13JOA41WM28yXeZ8KUzG+gPr32upS5JiZPkTLnwIU8IItKOJl50tfT879ToX2Nxw+b+h3FuMlxKRvVkFXio8d+L76GKAUX3iJXsS2CsJUZrJtVS9wofcw2ujp7//CF6o4nvfFrF+xqwt/43qRBTswDGqJJe/u0LgPSZ7LNTzDgR5Sx9YIYbJ0I2LnubxSbuIca0U01BU4A7XzGlE/Jr4F4+bK6V/pnyfeYal8PhINz9j76KNR8cWCwEcJcTHKKGvuMxvU7nYkTN89RvTkzU/Y4w9n0cMYGdtiA== Received: from DB6PR0101MB2214.eurprd01.prod.exchangelabs.com (2603:10a6:4:42::27) by DB3PR0102MB3546.eurprd01.prod.exchangelabs.com (2603:10a6:8:d::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5395.15; Thu, 30 Jun 2022 22:30:44 +0000 Received: from DB6PR0101MB2214.eurprd01.prod.exchangelabs.com ([fe80::60b9:9f29:40cc:f01c]) by DB6PR0101MB2214.eurprd01.prod.exchangelabs.com ([fe80::60b9:9f29:40cc:f01c%10]) with mapi id 15.20.5373.018; Thu, 30 Jun 2022 22:30:44 +0000 From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Fri, 1 Jul 2022 00:29:40 +0200 Message-ID: X-Mailer: git-send-email 2.34.1 In-Reply-To: References: X-TMN: [0TYEETFuvs2serXLAZzlrIr916fMzbwc] X-ClientProxiedBy: FR0P281CA0099.DEUP281.PROD.OUTLOOK.COM (2603:10a6:d10:a9::17) To DB6PR0101MB2214.eurprd01.prod.exchangelabs.com (2603:10a6:4:42::27) X-Microsoft-Original-Message-ID: <20220630222949.1758625-8-andreas.rheinhardt@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: a4938c51-c85e-4ded-5dff-08da5ae82b9b X-MS-Exchange-SLBlob-MailProps: ZTmCor6bjd9NOzJ3eMHB3ZNcMHEQAm90xy2vrh6YM+CcJdQOzhHXZ7JwnCXUzCj7h5weJ09823yZ/5YFvju03+qF1fq0xdq5ZK9CVDZOgkO7N58sqWCrEg+qQZArRGRzxtjlQHDunQpAyJ1AegcZFklEF8RnInrkTWWDY4tfkcRZlKqf6YLg7nbttFCzulCPjzid9VyzQumI5IRqb9FKu+xj2le8E0jBDAfkckIPF4t/rK+/ycn27+O5B9M1lWU3lxkN1pzJiRIrdkA5KxCS5Y7aYRECyMDIrPwmd+7llS6JIGGqxGyJPDRwdz/CPdgOl+EmJ0xC7V1WVAph36iWf5EF8xdZeRhmEZ2ns/wrwVeXPwuuGHvvHmdDr9J5fu7izs1aonQQQPTj77R6hNOuRgVpZSPDLhMmoyTzYbDlyVWO3r3tve/KCQq0DVfaa1PzJbr2kwS/GDpCggkE+IBAaC2ef1pnS8m5SPZhowIMOt1qQ+EGpdrI4G4qdJdDP6TEOCc/ueRzCO5lgX9BnseLz45t5Ny2cQL6AzGAfKCresaAd187RZucvI6SNc7ZGG21ssZvasdrhjzEm1XDgv3tfr8WR0b8v2F6ahIbzF7IVH3ItqvHWZQVk6L52jMHj11f/B4iJGD2QD9lHeMmAz21fshmb1RfV0q2E/oUNaGoVAiwRvewIn3jxea5F4IZJ5Qjys8Q3sAb0qkCJ2Cn+BO6IDHbF3HZz+HgiibllYfNGRUymuJY4f6cLYk+D+cuUKPrFdC76IT8rjdOcZBD5wv9Dqn6YNwVuuYbiFopG1924rKeCh482OcQ+532kZtaP0fe X-MS-TrafficTypeDiagnostic: DB3PR0102MB3546:EE_ X-Microsoft-Antispam: BCL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e-4ded-5dff-08da5ae82b9b X-MS-Exchange-CrossTenant-AuthSource: DB6PR0101MB2214.eurprd01.prod.exchangelabs.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Jun 2022 22:30:43.9673 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB3PR0102MB3546 Subject: [FFmpeg-devel] [PATCH 09/18] avcodec/hevcdec: Add stat_coeffs to HEVCABACState 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: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: hbd80EmI3jaT The HEVC decoder has both HEVCContext and HEVCLocalContext structures. The latter is supposed to be the structure containing the per-slicethread state. Yet that is not how it is handled in practice: Each HEVCLocalContext has a unique HEVCContext allocated for it and each of these coincides with the main HEVCContext except in exactly one field: The corresponding HEVCLocalContext. This makes it possible to pass the HEVCContext everywhere where logically a HEVCLocalContext should be used. This led to confusion in the first version of what eventually became commit c8bc0f66a875bc3708d8dc11b757f2198606ffd7: Before said commit, the initialization of the Rice parameter derivation state was incorrect; the fix for single-threaded as well as frame-threaded decoding was to add backup stats to HEVCContext that are used when the cabac state is updated*, see https://ffmpeg.org/pipermail/ffmpeg-devel/2020-August/268861.html Yet due to what has been said above, this does not work for slice-threading, because the each HEVCLocalContext has its own HEVCContext, so the Rice parameter state would not be transferred between threads. This is fixed in c8bc0f66a875bc3708d8dc11b757f2198606ffd7 by a hack: It rederives what the previous thread was and accesses the corresponding HEVCContext. Fix this by treating the Rice parameter state the same way the ordinary CABAC parameters are shared between threads: Make them part of the same struct that is shared between slice threads. This does not cause races, because the parts of the code that access these Rice parameters are a subset of the parts of code that access the CABAC parameters. *: And if the persistent_rice_adaptation_enabled_flag is set. Signed-off-by: Andreas Rheinhardt --- libavcodec/hevc_cabac.c | 17 ++++++++--------- libavcodec/hevcdec.c | 10 +++++----- libavcodec/hevcdec.h | 10 +++++++--- 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/libavcodec/hevc_cabac.c b/libavcodec/hevc_cabac.c index a194f8a02a..985c97ef2a 100644 --- a/libavcodec/hevc_cabac.c +++ b/libavcodec/hevc_cabac.c @@ -453,19 +453,18 @@ void ff_hevc_save_states(HEVCContext *s, int ctb_addr_ts) (ctb_addr_ts % s->ps.sps->ctb_width == 2 || (s->ps.sps->ctb_width == 2 && ctb_addr_ts % s->ps.sps->ctb_width == 0))) { - memcpy(s->cabac_state, s->HEVClc->cabac_state, HEVC_CONTEXTS); + memcpy(s->cabac->state, s->HEVClc->cabac_state, HEVC_CONTEXTS); if (s->ps.sps->persistent_rice_adaptation_enabled_flag) { - memcpy(s->stat_coeff, s->HEVClc->stat_coeff, HEVC_STAT_COEFFS); + memcpy(s->cabac->stat_coeff, s->HEVClc->stat_coeff, HEVC_STAT_COEFFS); } } } -static void load_states(HEVCContext *s, int thread) +static void load_states(HEVCContext *s) { - memcpy(s->HEVClc->cabac_state, s->cabac_state, HEVC_CONTEXTS); + memcpy(s->HEVClc->cabac_state, s->cabac->state, HEVC_CONTEXTS); if (s->ps.sps->persistent_rice_adaptation_enabled_flag) { - const HEVCContext *prev = s->sList[(thread + s->threads_number - 1) % s->threads_number]; - memcpy(s->HEVClc->stat_coeff, prev->stat_coeff, HEVC_STAT_COEFFS); + memcpy(s->HEVClc->stat_coeff, s->cabac->stat_coeff, HEVC_STAT_COEFFS); } } @@ -508,7 +507,7 @@ static void cabac_init_state(HEVCContext *s) s->HEVClc->stat_coeff[i] = 0; } -int ff_hevc_cabac_init(HEVCContext *s, int ctb_addr_ts, int thread) +int ff_hevc_cabac_init(HEVCContext *s, int ctb_addr_ts) { if (ctb_addr_ts == s->ps.pps->ctb_addr_rs_to_ts[s->sh.slice_ctb_addr_rs]) { int ret = cabac_init_decoder(s); @@ -525,7 +524,7 @@ int ff_hevc_cabac_init(HEVCContext *s, int ctb_addr_ts, int thread) if (s->ps.sps->ctb_width == 1) cabac_init_state(s); else if (s->sh.dependent_slice_segment_flag == 1) - load_states(s, thread); + load_states(s); } } } else { @@ -556,7 +555,7 @@ int ff_hevc_cabac_init(HEVCContext *s, int ctb_addr_ts, int thread) if (s->ps.sps->ctb_width == 1) cabac_init_state(s); else - load_states(s, thread); + load_states(s); } } } diff --git a/libavcodec/hevcdec.c b/libavcodec/hevcdec.c index 33c9d74e5f..7d4de414cd 100644 --- a/libavcodec/hevcdec.c +++ b/libavcodec/hevcdec.c @@ -2501,7 +2501,7 @@ static int hls_decode_entry(AVCodecContext *avctxt, void *isFilterThread) y_ctb = (ctb_addr_rs / ((s->ps.sps->width + ctb_size - 1) >> s->ps.sps->log2_ctb_size)) << s->ps.sps->log2_ctb_size; hls_decode_neighbour(s, x_ctb, y_ctb, ctb_addr_ts); - ret = ff_hevc_cabac_init(s, ctb_addr_ts, 0); + ret = ff_hevc_cabac_init(s, ctb_addr_ts); if (ret < 0) { s->tab_slice_address[ctb_addr_rs] = -1; return ret; @@ -2579,7 +2579,7 @@ static int hls_decode_entry_wpp(AVCodecContext *avctxt, void *input_ctb_row, int return 0; } - ret = ff_hevc_cabac_init(s, ctb_addr_ts, thread); + ret = ff_hevc_cabac_init(s, ctb_addr_ts); if (ret < 0) goto error; hls_sao_param(s, x_ctb >> s->ps.sps->log2_ctb_size, y_ctb >> s->ps.sps->log2_ctb_size); @@ -3601,7 +3601,7 @@ static av_cold int hevc_decode_free(AVCodecContext *avctx) av_freep(&s->md5_ctx); - av_freep(&s->cabac_state); + av_freep(&s->cabac); for (i = 0; i < 3; i++) { av_freep(&s->sao_pixel_buffer_h[i]); @@ -3655,8 +3655,8 @@ static av_cold int hevc_init_context(AVCodecContext *avctx) s->HEVClcList[0] = s->HEVClc; s->sList[0] = s; - s->cabac_state = av_malloc(HEVC_CONTEXTS); - if (!s->cabac_state) + s->cabac = av_malloc(sizeof(*s->cabac)); + if (!s->cabac) return AVERROR(ENOMEM); s->output_frame = av_frame_alloc(); diff --git a/libavcodec/hevcdec.h b/libavcodec/hevcdec.h index e2dba54f26..b97b7d1354 100644 --- a/libavcodec/hevcdec.h +++ b/libavcodec/hevcdec.h @@ -226,6 +226,11 @@ enum ScanType { SCAN_VERT, }; +typedef struct HEVCCABACState { + uint8_t state[HEVC_CONTEXTS]; + uint8_t stat_coeff[HEVC_STAT_COEFFS]; +} HEVCCABACState; + typedef struct LongTermRPS { int poc[32]; uint8_t poc_msb_present[32]; @@ -482,8 +487,7 @@ typedef struct HEVCContext { int width; int height; - uint8_t *cabac_state; - uint8_t stat_coeff[HEVC_STAT_COEFFS]; + HEVCCABACState *cabac; /** 1 if the independent slice segment header was successfully parsed */ uint8_t slice_initialized; @@ -601,7 +605,7 @@ int ff_hevc_frame_rps(HEVCContext *s); int ff_hevc_slice_rpl(HEVCContext *s); void ff_hevc_save_states(HEVCContext *s, int ctb_addr_ts); -int ff_hevc_cabac_init(HEVCContext *s, int ctb_addr_ts, int thread); +int ff_hevc_cabac_init(HEVCContext *s, int ctb_addr_ts); int ff_hevc_sao_merge_flag_decode(HEVCContext *s); int ff_hevc_sao_type_idx_decode(HEVCContext *s); int ff_hevc_sao_band_position_decode(HEVCContext *s);