From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id 3BD474386A for ; Sat, 2 Jul 2022 10:40:35 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 467EA68B8A3; Sat, 2 Jul 2022 13:40:33 +0300 (EEST) Received: from EUR04-DB3-obe.outbound.protection.outlook.com (mail-oln040092074051.outbound.protection.outlook.com [40.92.74.51]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 44AC868B842 for ; Sat, 2 Jul 2022 13:40:27 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=lsaTHhDNvV/peY910N64Vel0a7+4/MSdOGt9ho8Cp9TmStvM0HugrohKlu+NIu1vj4qXTShV6AUHAjYCDbALmpM9Q6PfUkJa2G6vjqm1Ama+Uk37yTe7sAS629pTACnFHp9nFKhQSrvSOQByVcrmi7gPSGVv3Hi5svaLPXzcq93j7Mq0JkkfLC7es047DZWOh5kH97jUBaTcmcXNoT5Vo09EgWGArVqDa82blABQapMEaRoV6kPe+tRFwh9Z4DF4qctoyGixMxLtguqUEK6reayW1DP2teDJ+MFlQyAys0tMpaho601X6ZpWV4CqdWK96JLIxXuOUpFrAbQwQdoydA== 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=WvSmhjKYYdW7qfIddj53zkJJllD/Y9NH2a9DRILBHmk=; b=VE/xDl75TxCyiPcm6QQ1/vA6resE0c0ppe61oce/bYmuzFOEBsTEsy7Z5L132L5UKaBxrgx8vq73+NHsXinX+3DVsejpyh2xolya1ZJE3G/0HXEB1zTMo7Bc0CYSMNp3P0qK6A/Guab79guV6UZx+LSuGQ+nf+mRWHx5OOaIVEHgB/xrKM16i7Yhqs6IyleMSJzWCm17F2EMdt3P+Xv6AhHKbUF56Y8tAE0NipFvjJ4cQx9blNGCSPEyNOL0/JQeQpehsRfvTbbhWz7VclsOzVLUDdvQpUi6U+4GREpMznMeqmcjGui0ESqydFbkL2Tfp0qi5A76P/bnorcVzZtBEQ== 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=WvSmhjKYYdW7qfIddj53zkJJllD/Y9NH2a9DRILBHmk=; b=AP1/ZxAdSW8h1qv/Bh+WnckrmKqYRsuAVhWaQ6Z94HJGY2lCDMs9olFnT+xRqgMT1E3TgJ6LW9HlCtS+EKhL6GGyr3k1koKis/etDHSMThg8bbqNnPjKYW0Juc6nbVT91rNP8bpSsaOzpkWjuyESFgoupJWRF0DqzEmWxaDTsqcQLh1aXLGMupcRg83Pet9ynpcTWmYsWf6fQe6QIQB7i8Bj819XzZDL/3UhaRm7XTlNmuwhFUPCgMueb/acRsjqtmrCI57aYt4+6f8YTcnr9TkKGc+T7WfALJu4vGpWEdCe9/lGvF+JkC0335WlTls/GWJxA0LBkgxQf9uoXirn9w== Received: from DB6PR0101MB2214.eurprd01.prod.exchangelabs.com (2603:10a6:4:42::27) by AM0PR01MB3826.eurprd01.prod.exchangelabs.com (2603:10a6:208:b1::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5373.17; Sat, 2 Jul 2022 10:40:25 +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.025; Sat, 2 Jul 2022 10:40:25 +0000 Message-ID: Date: Sat, 2 Jul 2022 12:40:20 +0200 Content-Language: en-US To: FFmpeg development discussions and patches References: <165675088963.31466.628326916294051579@lain.khirnov.net> From: Andreas Rheinhardt In-Reply-To: <165675088963.31466.628326916294051579@lain.khirnov.net> X-TMN: [/N2EOHLnrCq8YBK/tIGMFIGfR6C1rHde] X-ClientProxiedBy: AM5P194CA0014.EURP194.PROD.OUTLOOK.COM (2603:10a6:203:8f::24) To DB6PR0101MB2214.eurprd01.prod.exchangelabs.com (2603:10a6:4:42::27) X-Microsoft-Original-Message-ID: <8e7614fd-6366-1273-3925-10da139c6a5a@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 38ab58fc-c0a6-482c-a366-08da5c17439e X-MS-TrafficTypeDiagnostic: AM0PR01MB3826:EE_ X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: YX6fq+WL3rdin8GUBRbtlpMrTJU65pYHJccavxyDsAsRKdvwHoDoJnCi1bKQ2Ekb6FNoToZT/oEdBx8L/ACR6iT1Iqt2OiURnV00/+7Bt7qONlPmLU7ORNYhNMIJNY6nt9uG+Q5BBuI3HW0ePZW7XFr5XLESQZuTgVcOia1TsTCZB4huMO2QebWNUvSjDSsxKryCleZCxj+L5iqW4Yr7P1PWx7QRlg4UQJ1SEjn1McV6sZXgSVhvC8V151Xy/ZuiGba7bSD12DxWBA8Evbe/b3vwxki0tZB0M6GxisrxYSZH0Y5Hmwy2+KeP6A0cCP/noQu6A49g9xvo5iEOCzuxwQTIHHR3U3KMpxDvF7wR2YczEGvZW+svUwF9ul2oPqnNINrmL6eaTBGm0eF7DIXhXBCMGVDHTvK3ohEEOj2Jw11HznLDjpReLbNr/fFt0vuavGYQnbh5iC7TsvGeAOujIW2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YwSkRiYXZram5Pbk56cHdua1BxVXdyeFV6?= =?utf-8?B?eDRxYVA5YU1TYit5MW40TTVwK3JNSnlJamtBZitoTVVmdXc3M1BZSXpIZmRw?= =?utf-8?B?My8vVDEzL0hkZ01NMjBlTHZhUFl2REUzNGhpMnRoQ09VK1p1S1V2YmlMLzgw?= =?utf-8?B?cVdCTEtRd2tINGJabW84V2t1VDlJYnNCSUNPYXFQVFNkeTBlN2lEVmY5Q3JL?= =?utf-8?Q?sbxhzKC2H+Yk3Wgwkw=3D?= X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 38ab58fc-c0a6-482c-a366-08da5c17439e X-MS-Exchange-CrossTenant-AuthSource: DB6PR0101MB2214.eurprd01.prod.exchangelabs.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Jul 2022 10:40:25.2317 (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: AM0PR01MB3826 Subject: Re: [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 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: Anton Khirnov: > Quoting Andreas Rheinhardt (2022-07-01 00:29:40) >> 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); > > So if I'm reading this right, this copies the per-slice-context state > into the decoder-global state. And it's done from slice threads with no > locks. So how is this not racy? > a) I am not claiming that this is not racy; I am merely claiming that it does not introduce a new race, 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 tsan has never shown a race for me when updating cabac state or the rice parameters in general. b) (i) I readily admit that HEVC is not my forte*. WPP is supposed to be as follows: Given that HEVC needs the upper right and upper block/ctu for prediction, each row can only start decoding after the first two ctus of the row above it have been decoded. And then the cabac state of the row below is initialized from the cabac state of the row above after decoding its first two ctus. Therefore only one cabac state needs to be cached at any given time. (ii) You can see this in ff_hevc_save_states, where it is only saving the state after the second row (this presumes that the initial ctb_addr_ts in hls_decode_entry_wpp is a multiple of ctb_width which seems to be the case when tiles are disabled; I don't know what happens when both tiles and wpp are enabled. According to https://github.com/ultravideo/kvazaar/issues/201#issuecomment-391329526 this is not even allowed in any currently legal profile, but I don't think our decoder checks for that). (iii) After having saved the state, it is signalling that it is done with this ctu via ff_thread_report_progress2. The next thread waits for this event via ff_thread_await_progress2 and initializes its cabac state (if necessary). So there is your synchronization. (iv) Looking at ff_hevc_cabac_init, one can see that the first branch is always true when run from the first job; whereas ctb_addr_ts % s->ps.sps->ctb_width == 0 is true when tiles are disabled and when one is decoding the first ctu of a row (I don't know what happens in case tiles are enabled; probably mayhem. It seems kvazaar can produce such files, see above link.). c) The current state of affairs is btw weird: Given that the secondary HEVCContexts are overwritten by the main HEVCContext in hls_slice_data_wpp, the rice state that every HEVCContext starts with is given by the state of the first HEVCContext. And which row (of the last picture) this corresponds to depends upon the number of slice threads in use. This might cause problems if dependent_slice_segment_flag is enabled. d) See also the comment to patch #2. - Andreas *: I only wanted to share the common SEI parts of H.264 and HEVC due to softworkz's horrible way of sharing it. _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".