From patchwork Thu Mar 3 07:55:08 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 34590 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6838:d078:0:0:0:0 with SMTP id x24csp362435nkx; Wed, 2 Mar 2022 23:55:31 -0800 (PST) X-Google-Smtp-Source: ABdhPJyqu/F0x3aa2H1BkbI7+KWV3h1f+LKzJbyxuVpeFQEMMKepBHsZiSG1ZGXOQ0vVJRe7QGRU X-Received: by 2002:a50:cc89:0:b0:400:8f82:99e8 with SMTP id q9-20020a50cc89000000b004008f8299e8mr32841139edi.7.1646294131663; Wed, 02 Mar 2022 23:55:31 -0800 (PST) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id q7-20020a170906144700b006d6daebb216si785202ejc.869.2022.03.02.23.55.30; Wed, 02 Mar 2022 23:55:31 -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=@outlook.com header.s=selector1 header.b=FqRALHwl; 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 BE65F68AED0; Thu, 3 Mar 2022 09:55:25 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from EUR05-DB8-obe.outbound.protection.outlook.com (mail-db8eur05olkn2040.outbound.protection.outlook.com [40.92.89.40]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 550AA68AED0 for ; Thu, 3 Mar 2022 09:55:18 +0200 (EET) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ehNbPQrqZ+RzfJ+hYF/blhh7TgzBEzpXx2Bq3M+vk5U9IX6I6Ki72xvznOVH2p8OA5LW1dV3iH+yANCef98SC9dqZoPy/dsd7XhrNzGqvnwEZ6i8ZRAIvUDVM524Z3OFLCODf9Q9iFYaTEpBfBeDjMUlj0d5M+bPVLoxZVu76XGuEN0tYBASeqGWuPMEsUgjaf1kYgmOZUj6zCqT3UGee8JrInaC5hHznKeY+uDQ7EnXgPajvjFyj3Vk3jErWD6cxuYZdfQ0obSafXyYex2cWePTx4tOpFOl/bccqdOza+GRGZhrZma/Tmc4kcOExrvcPnX7LqAHAblyhXmfylOt0g== 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=JIVamsVIhSy4SS/mTjExM1iSlbVZUbxCrmkg2gV+qIk=; b=mUEFCG9CalIQULeIluYRal5JgOifqbcvagO174X1X1I1iNcn2uXx+RKMsiQCgDMrj+MaR4d83X/PeVAp6WZYqjz3lKIekyqVJjdnEtZd7eexdQ0RkrBx0WPAtUWB0T2PjnDsr8BLuOuADBh3Qaars8AFzObMo0wD9i87UP9XgL771qNQudu/yNCcrcMBITC96HfLrmhvAfpUW3gR9q/aWCvk3wBjaCLFdaM8T/oHmA15nIG3wTxASUGwkQDfuM41ExwXj17qjbLiwNJmLGqbtz3d7wGtAqqcAVD3MJztnxsCka2rGwAbrXq9ICVzoWxXJPFWI/rHcih1r9Zf8JQXbA== 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=JIVamsVIhSy4SS/mTjExM1iSlbVZUbxCrmkg2gV+qIk=; b=FqRALHwlIt3tPLbmLvygcerzSSPMa6hn9pxy4tbXtJJwzdSlRFAoNSMo//ZXIC7eUYGLLX6Ru80gikL+z98LUYvWCGD143TBzrVSN8ozsWBbB0zoWg8hyZjJwrAB/92TXwemvdq09lIcji4nUh/6HTVGYw1y7Ez9Zdh9M+a2MEhbTqchN5nNh+ESL2CLvaNll30N2PLp5WzQ796/2PrGXYQWI6rEbewEcyrI8TnjMTOLMUiCI8ixP+DYIT6WyHbsqNIga7RnOM61BsucbIAOno+xyhyvE8ythIyzH4XboBL2tFjy2wrY3sazFMzoawwvQTAXMJlwTrQM0A7vzNn4cQ== Received: from AM7PR03MB6660.eurprd03.prod.outlook.com (2603:10a6:20b:1c1::22) by VI1PR03MB2864.eurprd03.prod.outlook.com (2603:10a6:802:33::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5038.14; Thu, 3 Mar 2022 07:55:16 +0000 Received: from AM7PR03MB6660.eurprd03.prod.outlook.com ([fe80::b8d3:effd:9a3c:4090]) by AM7PR03MB6660.eurprd03.prod.outlook.com ([fe80::b8d3:effd:9a3c:4090%5]) with mapi id 15.20.5038.015; Thu, 3 Mar 2022 07:55:16 +0000 From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Thu, 3 Mar 2022 08:55:08 +0100 Message-ID: X-Mailer: git-send-email 2.32.0 X-TMN: [eceX/ng5JfrSSo8gElgPE2HfopuN3jhs] X-ClientProxiedBy: AM5PR0601CA0043.eurprd06.prod.outlook.com (2603:10a6:203:68::29) To AM7PR03MB6660.eurprd03.prod.outlook.com (2603:10a6:20b:1c1::22) X-Microsoft-Original-Message-ID: <20220303075508.146526-1-andreas.rheinhardt@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 0ac257f5-efb2-4daa-20d9-08d9fceb274f X-MS-Exchange-SLBlob-MailProps: +LiGfBxqLEtfxYSlGQowr6/qUFe6DNq8Ci+d+K5A1wTbelLICyOnh34cphKyAEmP1br552yFiAWQUU4eo7+HHEQ3QLRdUOYRxwVwH41cJdJp9LrTtJNRPlqRsZjR2KUooplVIVtQcwoI9LM6UZzI1joWnlGOeiMT87gZmy+PGkRKzJWXQuTzhW0wxy9LcuGSxO2UVDV2k+ptDylxoNrc3ssiPDrBR0AYJvLMOjtQrkn5epZgGkYQ6xhxgTxeeiLNoagmhfaDhylX/dPYgOTxIShlnsZ+lRWbZr2KOj0+JNLJy+QyXo2b7om5qcCxC8dwUekqElc+6+0Pieh8opdQfwPL1eehn3mR0uGfjbkUuqX46Hs7sB6+CHJ4hXlcSeVBQcWMfPHvLO07wnwC+N6k327wv73rDETC0Sry7V8uwqMfYPPc8aHAC3JiOefB+NcypHBNFpRmF0eGbNpuqfWrlA/ry+mExYGg9sPme6Oex/Dx8/KUiv4r7nBnwapv0TxPZKpKAnhj5pU/BvjWgNFX1PlTmD02PEkrVmjTe037U0KRMVn4+BCDPEIPrT5rZGgOsXROMkdT6lYUbQzswEbFeiIQEtY7leaREATqEnbyDDQo0xDMD4+0Sq+F3bWDTDe79ezMvh8OiA3hXu/TE0hCRRFKRzQ7vsvBylxBFhXb9pO4Hr9xWp5klspMzmvGvMLhY6MQe/oTndI38CprvtQ9DYJZ4yOuf6AU+B4NSkrSyDtTFQV9lsJbvP9XOAiAhjr6kZUNvvp0F1spS6cJUzGb4ONU5xxVgJX8 X-MS-TrafficTypeDiagnostic: VI1PR03MB2864:EE_ X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: tXWXhBKtHe4PuHWILN+Wvm+b8IAwwLRMia834m+eiAR+COl3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1/qR76Ye/g38c+s+NXe/WAM2mGv44PYjxJ6CSqtJHQTyIsq+X+Ngxba+JC/UDsmeyJtC+jT+ZMam70Sbm5dcUo2WGo6pj7pZD8WWmcqAJnYHV5ZohpwIWKFdC4VXZoM9Xhbm/iTe3MhBqVw3nneB+3xti0ysxZGfLbAFLhMNF7rsaWj6JEg86+iIUbnAWSHMmwqVk1tTwrbV2YAZwjyE7tb2CZn5f2R5dPPOMhjJxs+toOQuefOdD9u1D8bqT4EeodAATBTjCQyELcMxzwiIcDulD5y32tfLMSjCjI/Byo8L9rylvNLCytjq5k7sZah1w3TEtJKpmOYfkT9gIjyQH/o9N/iXkCSM1bingCaOE2+lySwc90JbCM4t/ptYBEu+iR8Dx4WGx3HLViJV+BR5kmQ1v/hYiggd91oq/2xEZKd12n/q/CT2Xm8Hgthp4pi6eAX3y/1GKZS65xpJHmerp4+dtReoG+Q/Rmw++VUQ+cSeRSWXzVZN6fPFew9azid+QA== X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 0ac257f5-efb2-4daa-20d9-08d9fceb274f X-MS-Exchange-CrossTenant-AuthSource: AM7PR03MB6660.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Mar 2022 07:55:16.3989 (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: VI1PR03MB2864 Subject: [FFmpeg-devel] [PATCH v2] avcodec/ffv1dec: Don't set ThreadFrame properties, fix race 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: E1U047lQDr/V Each FFV1 slice has its own SAR and picture structure encoded; when a slice header was parsed, the relevant fields of a ThreadFrame's AVFrame were directly set based upon the parsed values. This is a data race in case slice threading is in use because of the concurrent writes. In case of frame threading, it is also a data race because the writes happen after ff_thread_finish_setup(), so that the reads performed by ff_thread_ref_frame() are unsynchronized with the writes performed when parsing the header. This commit fixes these issues by not writing to the ThreadFrame at all; instead the raw data is read into the each SliceContext first; after decoding the current frame and creating the actual output frame these values are compared to each other. If they are valid and coincide, the derived value is written directly to the output frame, not to the ThreadFrame, thereby avoiding data races; in case they are not valid or inconsistent the most commonly used valid value is used instead. This fixes most FFV1 FATE-tests completely when using slice threading; the exceptions are fate-vsynth3-ffv1, vsynth3-ffv1-v3-yuv420p and vsynth3-ffv1-v3-yuv422p10. (In these tests the partitioning into slices does not honour chroma subsampling; as a result, chroma pixels at slice borders get set by more than one thread without any synchronization.) Signed-off-by: Andreas Rheinhardt --- libavcodec/ffv1.h | 4 ++ libavcodec/ffv1dec.c | 119 +++++++++++++++++++++++++++++++++++-------- 2 files changed, 103 insertions(+), 20 deletions(-) diff --git a/libavcodec/ffv1.h b/libavcodec/ffv1.h index ac80fa85ce..f640d5a710 100644 --- a/libavcodec/ffv1.h +++ b/libavcodec/ffv1.h @@ -91,6 +91,8 @@ typedef struct FFV1Context { struct FFV1Context *fsrc; AVFrame *cur; + int picture_structure; + AVRational sample_aspect_ratio; int plane_count; int ac; ///< 1=range coder <-> 0=golomb rice int ac_byte_count; ///< number of bytes used for AC coding @@ -132,6 +134,8 @@ typedef struct FFV1Context { int slice_coding_mode; int slice_rct_by_coef; int slice_rct_ry_coef; + + AVRational slice_sample_aspect_ratios[MAX_SLICES]; } FFV1Context; int ff_ffv1_common_init(AVCodecContext *avctx); diff --git a/libavcodec/ffv1dec.c b/libavcodec/ffv1dec.c index 201630167d..bda8a5a2f1 100644 --- a/libavcodec/ffv1dec.c +++ b/libavcodec/ffv1dec.c @@ -167,7 +167,7 @@ static int decode_slice_header(const FFV1Context *f, FFV1Context *fs) { RangeCoder *c = &fs->c; uint8_t state[CONTEXT_SIZE]; - unsigned ps, i, context_count; + unsigned i, context_count; memset(state, 128, sizeof(state)); av_assert0(f->version > 2); @@ -205,25 +205,17 @@ static int decode_slice_header(const FFV1Context *f, FFV1Context *fs) p->context_count = context_count; } - ps = get_symbol(c, state, 0); - if (ps == 1) { - f->cur->interlaced_frame = 1; - f->cur->top_field_first = 1; - } else if (ps == 2) { - f->cur->interlaced_frame = 1; - f->cur->top_field_first = 0; - } else if (ps == 3) { - f->cur->interlaced_frame = 0; - } - f->cur->sample_aspect_ratio.num = get_symbol(c, state, 0); - f->cur->sample_aspect_ratio.den = get_symbol(c, state, 0); - - if (av_image_check_sar(f->width, f->height, - f->cur->sample_aspect_ratio) < 0) { + fs->picture_structure = get_symbol(c, state, 0); + fs->sample_aspect_ratio.num = get_symbol(c, state, 0); + fs->sample_aspect_ratio.den = get_symbol(c, state, 0); + /* Num or den being zero means unknown for FFV1; our unknown is 0 / 1. */ + if (fs->sample_aspect_ratio.num == 0 || fs->sample_aspect_ratio.den == 0) { + fs->sample_aspect_ratio = (AVRational) { 0, 1 }; + } else if (av_image_check_sar(f->width, f->height, + fs->sample_aspect_ratio) < 0) { av_log(f->avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n", - f->cur->sample_aspect_ratio.num, - f->cur->sample_aspect_ratio.den); - f->cur->sample_aspect_ratio = (AVRational){ 0, 1 }; + fs->sample_aspect_ratio.num, fs->sample_aspect_ratio.den); + fs->sample_aspect_ratio = (AVRational) { 0, 0 }; } if (fs->version > 3) { @@ -251,6 +243,9 @@ static int decode_slice(AVCodecContext *c, void *arg) AVFrame * const p = f->cur; int i, si; + fs->picture_structure = 0; + fs->sample_aspect_ratio = (AVRational){ 0, 0 }; + for( si=0; fs != f->slice_context[si]; si ++) ; @@ -831,6 +826,21 @@ static av_cold int decode_init(AVCodecContext *avctx) return 0; } +static int compare_sar(const void *a, const void *b) +{ + const AVRational x = *(const AVRational*)a; + const AVRational y = *(const AVRational*)b; + int cmp; + + /* 0/0 means invalid and 0/1 means unknown. + * Order the ratios so that these values are at the end. */ + if (x.num == 0 || y.num == 0) + return y.num - x.num; + cmp = av_cmp_q(x, y); + /* For definiteness, order equivalent fractions by "lower terms last". */ + return cmp ? cmp : y.num - x.num; +} + static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt) { uint8_t *buf = avpkt->data; @@ -969,9 +979,78 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPac if (f->last_picture.f) ff_thread_release_ext_buffer(avctx, &f->last_picture); - if ((ret = av_frame_ref(data, f->picture.f)) < 0) + p = data; + if ((ret = av_frame_ref(p, f->picture.f)) < 0) return ret; + if (f->version > 2) { + AVRational sar = f->slice_context[0]->sample_aspect_ratio; + int pic_structures[4] = { 0 }, picture_structure; + int inconsistent_sar = 0, has_sar = 0; + + for (int i = 0; i < f->slice_count; i++) { + const FFV1Context *const fs = f->slice_context[i]; + int slice_picture_structure = (unsigned)fs->picture_structure > 3 ? + 0 : fs->picture_structure; + + pic_structures[slice_picture_structure]++; + + if (fs->sample_aspect_ratio.num != sar.num || + fs->sample_aspect_ratio.den != sar.den) + inconsistent_sar = 1; + sar = fs->sample_aspect_ratio; + if (sar.num == 0) + inconsistent_sar = 1; + else + has_sar = 1; + f->slice_sample_aspect_ratios[i] = sar; + } + if (has_sar) { + if (inconsistent_sar) { + AVRational last_sar = (AVRational){ 0, 0 }; + qsort(f->slice_sample_aspect_ratios, f->slice_count, + sizeof(f->slice_sample_aspect_ratios[0]), compare_sar); + + for (int i = 0, current_run = 0, longest_run = 0; + i < f->slice_count; i++) { + AVRational cur_sar = f->slice_sample_aspect_ratios[i]; + + if (cur_sar.num == 0) + break; + if (av_cmp_q(cur_sar, last_sar)) + current_run = 1; + else + current_run++; + if (current_run > longest_run) { + longest_run = current_run; + sar = cur_sar; + } + last_sar = cur_sar; + } + av_log(avctx, AV_LOG_WARNING, "SAR inconsistent, using %u/%u\n", + sar.num, sar.den); + } + p->sample_aspect_ratio = sar; + } + + for (int i = 0, count = 0; i < FF_ARRAY_ELEMS(pic_structures); i++) { + if (pic_structures[i] > count) { + picture_structure = i; + count = pic_structures[i]; + } + } + /* Set picture structure based upon the most commonly encountered + * picture structure. */ + if (picture_structure == 1) { + p->interlaced_frame = 1; + p->top_field_first = 1; + } else if (picture_structure == 2) { + p->interlaced_frame = 1; + p->top_field_first = 0; + } else if (picture_structure == 3) + p->interlaced_frame = 0; + } + *got_frame = 1; return buf_size;