From patchwork Sun Dec 31 08:18:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nuo Mi X-Patchwork-Id: 45423 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:6623:b0:194:e134:edd4 with SMTP id n35csp5663105pzh; Sun, 31 Dec 2023 00:35:26 -0800 (PST) X-Google-Smtp-Source: AGHT+IHbgGUwtUQXk/ZVnHlOegE2VUeiNJIUkUvW4OLnyqJjkesamkREMj/+uN0liiJaJohohrVC X-Received: by 2002:a17:907:930b:b0:a27:cf4a:607b with SMTP id bu11-20020a170907930b00b00a27cf4a607bmr723017ejc.52.1704011725804; Sun, 31 Dec 2023 00:35:25 -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 js18-20020a170906ca9200b00a27e2b27455si399358ejb.1024.2023.12.31.00.35.25; Sun, 31 Dec 2023 00:35:25 -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=qbBTfnYg; 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=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id C0B9168CCCA; Sun, 31 Dec 2023 10:35:21 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from APC01-TYZ-obe.outbound.protection.outlook.com (mail-tyzapc01olkn2085.outbound.protection.outlook.com [40.92.107.85]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 187B068C455 for ; Sun, 31 Dec 2023 10:35:14 +0200 (EET) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=AuA1MmOSEv7+LwC7gsoATNs5CMXqjX4bQg/Vsl3d/9QA1o8VDhDhWFF3x9uvty5K6AXGAg4Fv+rC6aUS+SL0pfbxDGXoRy+ihEQ1NEudmoTKEyASQ3Ylx23vWxIF4Lk58io/c75+/p2ISXIHmapQEvNEmKrqFG8QuVeY8+sjbFDTeDKtH7f/ontzZQJyj6HeFje/3DNC2efeXCA0R4mFU8mXOSIYCaW9JasG+52zjJjSsGTy8zkDxuN/JCa/0kdrhGa1g4nCoYoWr6JDqiRq1Nhn8+fQa5/MnjSkRaxis4uLXWsWQnPLe20I4mQx43/rm6m+aqFqO6gqOsxOk261TA== 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=bk6bJG4xFgEoPBxmaYpKFppfEL6gw/DRsUn8U4qStMU=; b=cx3PEgEmrjb+q5VpRnDFE+KYsRm9JEtsNO0RPXZ5rhKepwdnRT7eROAGOMlnpRoZmAsLeNTRIFiUqUu/rpkC/8ZVWAodsi99ajpBs7b8LBWT3BjVulfo3XOAZ8ORrlB4z+BnnOgSBZt+0D9XwXBTaltx6LjxaIXFBlDxbdmuFYqrdmjOaONyZDGpXHDLlaOaVgprroNsIb/yzdX98LCXIs3C+PYs6ja03ee0Z1O3C3mK9ZtprcNfU/rcxsmvWZoNRfoqhgRuybPk4y19jdJzHYeg+SVZWnCXFMsl799V5T9KQOaXseQBuU1FQDckThiipyI9J5blcz+yuewyjZsUTg== 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=bk6bJG4xFgEoPBxmaYpKFppfEL6gw/DRsUn8U4qStMU=; b=qbBTfnYg0xF++ybuEr5BgU5OG6LOoyGPo0yUq2R4mNMnwyy2/Fa4lzTr1uY7MNc0bfXatPiLKeAbFXxw1ngSXjDqgUknzrZbuzeP7Mc6TfUjmfDR+kdt5iw5MTtJ2mN3j2fj2xJlsBCNeWiV265MmUeVMmSBZPb7rcMVbufmE3IUTkyd3LLWUfaZlY+BS5KeoVkxrKgltXtah0LIj6NOQ1Wez8wT95O2Jloq0ipwR78VwoQpS+tpJSy/hjvubFvbRO4uLV7eUnER+lfXsZO1nhsPm5eJ946Sjvhtsjaj/4eyzE+GbSRRl3SQZCOocmjFITAJru+3y1V3XfXNzVhpJw== Received: from TYSPR06MB6433.apcprd06.prod.outlook.com (2603:1096:400:47a::6) by JH0PR06MB6367.apcprd06.prod.outlook.com (2603:1096:990:c::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7135.21; Sun, 31 Dec 2023 08:19:47 +0000 Received: from TYSPR06MB6433.apcprd06.prod.outlook.com ([fe80::e139:fd7a:1b66:a3f7]) by TYSPR06MB6433.apcprd06.prod.outlook.com ([fe80::e139:fd7a:1b66:a3f7%6]) with mapi id 15.20.7135.023; Sun, 31 Dec 2023 08:19:47 +0000 From: Nuo Mi To: ffmpeg-devel@ffmpeg.org Date: Sun, 31 Dec 2023 16:18:49 +0800 Message-ID: X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231231081858.1704-1-nuomi2021@gmail.com> References: <20231231081858.1704-1-nuomi2021@gmail.com> X-TMN: [5vmFQECBRMeB7cj0nmAnhcDT5srT8D3V] X-ClientProxiedBy: SI2PR02CA0039.apcprd02.prod.outlook.com (2603:1096:4:196::9) To TYSPR06MB6433.apcprd06.prod.outlook.com (2603:1096:400:47a::6) X-Microsoft-Original-Message-ID: <20231231081858.1704-5-nuomi2021@gmail.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 2 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: TYSPR06MB6433:EE_|JH0PR06MB6367:EE_ X-MS-Office365-Filtering-Correlation-Id: 9fb06487-994f-4ed1-92f9-08dc09d94011 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: BAo77/bJaGFl09moFVNQpuNibfnc1eiq2IGGT1gb8xcjdNMLeggQdfFNL0LGcEqh7D/pAHd5y1xtedVZOs4XqpsitoIVO2PAleP2qY+1wBWUxGNYx8Ku1yXdf/KxSxoFYFt3psa1iXCecaGOVj5rspTAdjKp5DZD9gjCWTplXYtsmxJU8JvQ34aAW72heAAPLMHw5+7+xWTdJ29E86QUQGQRxUC3WeyI8659ti6ik8eePiIUMAQ0C/QmoHBpgNwHJUwYrov0pUTthjDQm1Dqf98G5swYsA7L+Q1NgwY21w/y2ymm42+/q2eNiv3nUWabWwcojJryqG87q/TN0HO17s+ygtErU2W9xeTkF1+7mnmSnMTQ/ztfZwDS5t+EKQ0RXkfFwy0CvcVTwG4DNpi4e659m3SUBYnAqxT/0CUqpTctsQc1Ids0vuEcLCZL9I1yla2YYL4Zy5Cc4+Apigdq1lcMKtn6b7QBW7WCNs3O++9oAuzb5MqHA2EsR+vLefp558c8gtr98Xh3LnzdajJZntXxRxqvpg4mgmFaqqp2MGkYHzL1Q4/VeWsRQ6CYja9qMHgWZz0URFvkctITE2QO1PjvF40VVaRAuozqQaUiks3I6RmHMs7E+6fnBSimSLHC X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: OIV+ju0tL7zpq8V8BuV67QUjYpUy8ZKICjDpFQ0bXcOE03nNjtwO9xFVLxPTg52JYaQ4ia/lnGjgP5KafBHWyky1pxU7MRoZv6Ehh1PZHVkLVzAvDgC9wAejGDdxRfd24QsJZWIdwIZsCi+msQAb8h5gcsJW6vZfELp9vM+bteBApUZs0AYW94VTcEVmz7/QVprM2kBmD1SFenfQJIn+g83DPY2KdBYbkEBB+PWd4u1oDMVP/55VHl+FP6GWmDNab40d55W6ePChHigAl3ESBDelnlQb7YxHvPYYBAYLbUrOgYnVps6WzdR6G3mbXhfuTlB0TBI6p9gcW2RUTLPt1eiPINZNKP88ZxzOp6Ogo9iyDWcKFb5XdNHYct96xvPns62hAMEIcaa7HFqhC+RN+KIR55KqxMVQ36SakM5LIADVdcoF0jzjFWxwM/scBPg/lycL4xCLltlAnfnAMNSgA56YJIzB4vO+go5zJX6Kf7NFKuLuzbTbmlyVMaodymqa3kn/KIH8ZoNjCgIswZgD9hMSzGIJ4n4dvdaXAgx4Yxe1CZ9FdhgbeDYv/EBbDjt3m4yZ+dPEhaFeQCE/tbwT9/NokC1cTgyHHdzIWtPxZQvLeIQiyBeWCnmFsDz5o3X0NfGK/3TAazfb5mzzIEYEm9LOSY1/h3/Jd4vbeoXd7Nf0jf9ZTWy+6wFOunWzuFlS7Qxycde6RfZRsvBXDoiBifpB4L010TKxSnXnPeYh3j9NJLaWHRub9/mFu/f6hJrR45wxbR052Qm5941ekPXZ4nH30Ujk9aiu3O0Vc2W4OOT7HE+49Do8JmC/vQTcA9s/XtRR279UtyM8fcXVa7bidk2exAyBBy5mYjakCDlKFxusN7+P/Jv2fUUUiN+AzFL/BkDGFuSYbeNqp+zr2CY+8Agp6J2+zcjcBvAk2HJD35brIZXVAWh749GSYYjzCbraUu9nu2piNAeE4pmRFpeB35e3Fgt5C0l4CG4QCAwPyOOLJ4kuNfOiXbEJeFbPY3O3EP1V6b0aqRKsB8KdYAbhF0Er0WbNF7qP5xlKEKvXxA9Jtpij6kH19ycYoGt0gC1/qwpjo/27rzGKHtgHyJojFYx/8p0NQNp4iO9ZqI2o8Qgj9zMWEO6B3lTI8yBwv6aRLxxJTuEc1qwBWtGE/YxdxDkv8CQ+QsLHTwU2n4ZGq+069+3qfjhGWcHORmmwveTJtatTRMnk1Iwytl0cQcukz0kvHwoSj/r7Qx2Kzn8J16zhsbYTHW9dHW4BiuJ/qcFx X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 9fb06487-994f-4ed1-92f9-08dc09d94011 X-MS-Exchange-CrossTenant-AuthSource: TYSPR06MB6433.apcprd06.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 31 Dec 2023 08:19:46.9636 (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: JH0PR06MB6367 Subject: [FFmpeg-devel] [PATCH v8 04/13] vvcdec: add reference management 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: Nuo Mi Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: eO9+uGhOT50o --- libavcodec/vvc/Makefile | 1 + libavcodec/vvc/vvc_refs.c | 559 ++++++++++++++++++++++++++++++++++++++ libavcodec/vvc/vvc_refs.h | 57 ++++ 3 files changed, 617 insertions(+) create mode 100644 libavcodec/vvc/vvc_refs.c create mode 100644 libavcodec/vvc/vvc_refs.h diff --git a/libavcodec/vvc/Makefile b/libavcodec/vvc/Makefile index 20cbe8eae1..ae05f54752 100644 --- a/libavcodec/vvc/Makefile +++ b/libavcodec/vvc/Makefile @@ -4,3 +4,4 @@ OBJS-$(CONFIG_VVC_DECODER) += vvc/vvc_cabac.o \ vvc/vvc_ctu.o \ vvc/vvc_data.o \ vvc/vvc_ps.o \ + vvc/vvc_refs.o \ diff --git a/libavcodec/vvc/vvc_refs.c b/libavcodec/vvc/vvc_refs.c new file mode 100644 index 0000000000..bf503e429e --- /dev/null +++ b/libavcodec/vvc/vvc_refs.c @@ -0,0 +1,559 @@ +/* + * VVC reference management + * + * Copyright (C) 2023 Nuo Mi + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include + +#include "libavutil/thread.h" +#include "libavcodec/refstruct.h" +#include "libavcodec/thread.h" + +#include "vvc_refs.h" + +#define VVC_FRAME_FLAG_OUTPUT (1 << 0) +#define VVC_FRAME_FLAG_SHORT_REF (1 << 1) +#define VVC_FRAME_FLAG_LONG_REF (1 << 2) +#define VVC_FRAME_FLAG_BUMPING (1 << 3) + +typedef struct FrameProgress { + atomic_int progress[VVC_PROGRESS_LAST]; + VVCProgressListener *listener[VVC_PROGRESS_LAST]; + AVMutex lock; + AVCond cond; + uint8_t has_lock; + uint8_t has_cond; +} FrameProgress; + +void ff_vvc_unref_frame(VVCFrameContext *fc, VVCFrame *frame, int flags) +{ + /* frame->frame can be NULL if context init failed */ + if (!frame->frame || !frame->frame->buf[0]) + return; + + frame->flags &= ~flags; + if (!frame->flags) { + av_frame_unref(frame->frame); + ff_refstruct_unref(&frame->progress); + + ff_refstruct_unref(&frame->tab_dmvr_mvf); + + ff_refstruct_unref(&frame->rpl); + frame->nb_rpl_elems = 0; + ff_refstruct_unref(&frame->rpl_tab); + + frame->collocated_ref = NULL; + } +} + +const RefPicList *ff_vvc_get_ref_list(const VVCFrameContext *fc, const VVCFrame *ref, int x0, int y0) +{ + const int x_cb = x0 >> fc->ps.sps->ctb_log2_size_y; + const int y_cb = y0 >> fc->ps.sps->ctb_log2_size_y; + const int pic_width_cb = fc->ps.pps->ctb_width; + const int ctb_addr_rs = y_cb * pic_width_cb + x_cb; + + return (const RefPicList *)ref->rpl_tab[ctb_addr_rs]; +} + +void ff_vvc_clear_refs(VVCFrameContext *fc) +{ + for (int i = 0; i < FF_ARRAY_ELEMS(fc->DPB); i++) + ff_vvc_unref_frame(fc, &fc->DPB[i], + VVC_FRAME_FLAG_SHORT_REF | VVC_FRAME_FLAG_LONG_REF); +} + +static void free_progress(FFRefStructOpaque unused, void *obj) +{ + FrameProgress *p = (FrameProgress *)obj; + + if (p->has_cond) + ff_cond_destroy(&p->cond); + if (p->has_lock) + ff_mutex_destroy(&p->lock); +} + +static FrameProgress *alloc_progress(void) +{ + FrameProgress *p = ff_refstruct_alloc_ext(sizeof(*p), 0, NULL, free_progress); + + if (p) { + p->has_lock = !ff_mutex_init(&p->lock, NULL); + p->has_cond = !ff_cond_init(&p->cond, NULL); + if (!p->has_lock || !p->has_cond) + ff_refstruct_unref(&p); + } + return p; +} + +static VVCFrame *alloc_frame(VVCContext *s, VVCFrameContext *fc) +{ + const VVCPPS *pps = fc->ps.pps; + for (int i = 0; i < FF_ARRAY_ELEMS(fc->DPB); i++) { + int ret; + VVCFrame *frame = &fc->DPB[i]; + if (frame->frame->buf[0]) + continue; + + ret = ff_thread_get_buffer(s->avctx, frame->frame, AV_GET_BUFFER_FLAG_REF); + if (ret < 0) + return NULL; + + frame->rpl = ff_refstruct_allocz(s->current_frame.nb_units * sizeof(RefPicListTab)); + if (!frame->rpl) + goto fail; + frame->nb_rpl_elems = s->current_frame.nb_units; + + frame->tab_dmvr_mvf = ff_refstruct_pool_get(fc->tab_dmvr_mvf_pool); + if (!frame->tab_dmvr_mvf) + goto fail; + + frame->rpl_tab = ff_refstruct_pool_get(fc->rpl_tab_pool); + if (!frame->rpl_tab) + goto fail; + frame->ctb_count = pps->ctb_width * pps->ctb_height; + for (int j = 0; j < frame->ctb_count; j++) + frame->rpl_tab[j] = frame->rpl; + + frame->progress = alloc_progress(); + if (!frame->progress) + goto fail; + + return frame; +fail: + ff_vvc_unref_frame(fc, frame, ~0); + return NULL; + } + av_log(s->avctx, AV_LOG_ERROR, "Error allocating frame, DPB full.\n"); + return NULL; +} + +int ff_vvc_set_new_ref(VVCContext *s, VVCFrameContext *fc, AVFrame **frame) +{ + const VVCPH *ph= &fc->ps.ph; + const int poc = ph->poc; + VVCFrame *ref; + + /* check that this POC doesn't already exist */ + for (int i = 0; i < FF_ARRAY_ELEMS(fc->DPB); i++) { + VVCFrame *frame = &fc->DPB[i]; + + if (frame->frame->buf[0] && frame->sequence == s->seq_decode && + frame->poc == poc) { + av_log(s->avctx, AV_LOG_ERROR, "Duplicate POC in a sequence: %d.\n", poc); + return AVERROR_INVALIDDATA; + } + } + + ref = alloc_frame(s, fc); + if (!ref) + return AVERROR(ENOMEM); + + *frame = ref->frame; + fc->ref = ref; + + if (s->no_output_before_recovery_flag && (IS_RASL(s) || !GDR_IS_RECOVERED(s))) + ref->flags = 0; + else if (ph->r->ph_pic_output_flag) + ref->flags = VVC_FRAME_FLAG_OUTPUT; + + if (!ph->r->ph_non_ref_pic_flag) + ref->flags |= VVC_FRAME_FLAG_SHORT_REF; + + ref->poc = poc; + ref->sequence = s->seq_decode; + ref->frame->crop_left = fc->ps.pps->r->pps_conf_win_left_offset; + ref->frame->crop_right = fc->ps.pps->r->pps_conf_win_right_offset; + ref->frame->crop_top = fc->ps.pps->r->pps_conf_win_top_offset; + ref->frame->crop_bottom = fc->ps.pps->r->pps_conf_win_bottom_offset; + + return 0; +} + +int ff_vvc_output_frame(VVCContext *s, VVCFrameContext *fc, AVFrame *out, const int no_output_of_prior_pics_flag, int flush) +{ + const VVCSPS *sps = fc->ps.sps; + do { + int nb_output = 0; + int min_poc = INT_MAX; + int min_idx, ret; + + if (no_output_of_prior_pics_flag) { + for (int i = 0; i < FF_ARRAY_ELEMS(fc->DPB); i++) { + VVCFrame *frame = &fc->DPB[i]; + if (!(frame->flags & VVC_FRAME_FLAG_BUMPING) && frame->poc != fc->ps.ph.poc && + frame->sequence == s->seq_output) { + ff_vvc_unref_frame(fc, frame, VVC_FRAME_FLAG_OUTPUT); + } + } + } + + for (int i = 0; i < FF_ARRAY_ELEMS(fc->DPB); i++) { + VVCFrame *frame = &fc->DPB[i]; + if ((frame->flags & VVC_FRAME_FLAG_OUTPUT) && + frame->sequence == s->seq_output) { + nb_output++; + if (frame->poc < min_poc || nb_output == 1) { + min_poc = frame->poc; + min_idx = i; + } + } + } + + /* wait for more frames before output */ + if (!flush && s->seq_output == s->seq_decode && sps && + nb_output <= sps->r->sps_dpb_params.dpb_max_dec_pic_buffering_minus1[sps->r->sps_max_sublayers_minus1] + 1) + return 0; + + if (nb_output) { + VVCFrame *frame = &fc->DPB[min_idx]; + + ret = av_frame_ref(out, frame->frame); + if (frame->flags & VVC_FRAME_FLAG_BUMPING) + ff_vvc_unref_frame(fc, frame, VVC_FRAME_FLAG_OUTPUT | VVC_FRAME_FLAG_BUMPING); + else + ff_vvc_unref_frame(fc, frame, VVC_FRAME_FLAG_OUTPUT); + if (ret < 0) + return ret; + + av_log(s->avctx, AV_LOG_DEBUG, + "Output frame with POC %d.\n", frame->poc); + return 1; + } + + if (s->seq_output != s->seq_decode) + s->seq_output = (s->seq_output + 1) & 0xff; + else + break; + } while (1); + return 0; +} + +void ff_vvc_bump_frame(VVCContext *s, VVCFrameContext *fc) +{ + const VVCSPS *sps = fc->ps.sps; + const int poc = fc->ps.ph.poc; + int dpb = 0; + int min_poc = INT_MAX; + + for (int i = 0; i < FF_ARRAY_ELEMS(fc->DPB); i++) { + VVCFrame *frame = &fc->DPB[i]; + if ((frame->flags) && + frame->sequence == s->seq_output && + frame->poc != poc) { + dpb++; + } + } + + if (sps && dpb >= sps->r->sps_dpb_params.dpb_max_dec_pic_buffering_minus1[sps->r->sps_max_sublayers_minus1] + 1) { + for (int i = 0; i < FF_ARRAY_ELEMS(fc->DPB); i++) { + VVCFrame *frame = &fc->DPB[i]; + if ((frame->flags) && + frame->sequence == s->seq_output && + frame->poc != poc) { + if (frame->flags == VVC_FRAME_FLAG_OUTPUT && frame->poc < min_poc) { + min_poc = frame->poc; + } + } + } + + for (int i = 0; i < FF_ARRAY_ELEMS(fc->DPB); i++) { + VVCFrame *frame = &fc->DPB[i]; + if (frame->flags & VVC_FRAME_FLAG_OUTPUT && + frame->sequence == s->seq_output && + frame->poc <= min_poc) { + frame->flags |= VVC_FRAME_FLAG_BUMPING; + } + } + + dpb--; + } +} + +static VVCFrame *find_ref_idx(VVCContext *s, VVCFrameContext *fc, int poc, uint8_t use_msb) +{ + const int mask = use_msb ? ~0 : fc->ps.sps->max_pic_order_cnt_lsb - 1; + + for (int i = 0; i < FF_ARRAY_ELEMS(fc->DPB); i++) { + VVCFrame *ref = &fc->DPB[i]; + if (ref->frame->buf[0] && ref->sequence == s->seq_decode) { + if ((ref->poc & mask) == poc) + return ref; + } + } + return NULL; +} + +static void mark_ref(VVCFrame *frame, int flag) +{ + frame->flags &= ~(VVC_FRAME_FLAG_LONG_REF | VVC_FRAME_FLAG_SHORT_REF); + frame->flags |= flag; +} + +static VVCFrame *generate_missing_ref(VVCContext *s, VVCFrameContext *fc, int poc) +{ + const VVCSPS *sps = fc->ps.sps; + VVCFrame *frame; + + frame = alloc_frame(s, fc); + if (!frame) + return NULL; + + if (!s->avctx->hwaccel) { + if (!sps->pixel_shift) { + for (int i = 0; frame->frame->buf[i]; i++) + memset(frame->frame->buf[i]->data, 1 << (sps->bit_depth - 1), + frame->frame->buf[i]->size); + } else { + for (int i = 0; frame->frame->data[i]; i++) + for (int y = 0; y < (sps->height >> sps->vshift[i]); y++) { + uint8_t *dst = frame->frame->data[i] + y * frame->frame->linesize[i]; + AV_WN16(dst, 1 << (sps->bit_depth - 1)); + av_memcpy_backptr(dst + 2, 2, 2*(sps->width >> sps->hshift[i]) - 2); + } + } + } + + frame->poc = poc; + frame->sequence = s->seq_decode; + frame->flags = 0; + + ff_vvc_report_frame_finished(frame); + + return frame; +} + +/* add a reference with the given poc to the list and mark it as used in DPB */ +static int add_candidate_ref(VVCContext *s, VVCFrameContext *fc, RefPicList *list, + int poc, int ref_flag, uint8_t use_msb) +{ + VVCFrame *ref = find_ref_idx(s, fc, poc, use_msb); + + if (ref == fc->ref || list->nb_refs >= VVC_MAX_REF_ENTRIES) + return AVERROR_INVALIDDATA; + + if (!ref) { + ref = generate_missing_ref(s, fc, poc); + if (!ref) + return AVERROR(ENOMEM); + } + + list->list[list->nb_refs] = poc; + list->ref[list->nb_refs] = ref; + list->isLongTerm[list->nb_refs] = ref_flag & VVC_FRAME_FLAG_LONG_REF; + list->nb_refs++; + + mark_ref(ref, ref_flag); + return 0; +} + +static int init_slice_rpl(const VVCFrameContext *fc, SliceContext *sc) +{ + VVCFrame *frame = fc->ref; + const VVCSH *sh = &sc->sh; + + if (sc->slice_idx >= frame->nb_rpl_elems) + return AVERROR_INVALIDDATA; + + for (int i = 0; i < sh->num_ctus_in_curr_slice; i++) { + const int rs = sh->ctb_addr_in_curr_slice[i]; + frame->rpl_tab[rs] = frame->rpl + sc->slice_idx; + } + + sc->rpl = frame->rpl_tab[sh->ctb_addr_in_curr_slice[0]]->refPicList; + + return 0; +} + +static int delta_poc_st(const H266RefPicListStruct *rpls, + const int lx, const int i, const VVCSPS *sps) +{ + int abs_delta_poc_st = rpls->abs_delta_poc_st[i]; + if (!((sps->r->sps_weighted_pred_flag || + sps->r->sps_weighted_bipred_flag) && i != 0)) + abs_delta_poc_st++; + return (1 - 2 * rpls->strp_entry_sign_flag[i]) * abs_delta_poc_st; +} + +static int poc_lt(int *prev_delta_poc_msb, const int poc, const H266RefPicLists *ref_lists, + const int lx, const int j, const int max_poc_lsb) +{ + const H266RefPicListStruct *rpls = ref_lists->rpl_ref_list + lx; + int lt_poc = rpls->ltrp_in_header_flag ? ref_lists->poc_lsb_lt[lx][j] : rpls->rpls_poc_lsb_lt[j]; + + if (ref_lists->delta_poc_msb_cycle_present_flag[lx][j]) { + const uint32_t delta = ref_lists->delta_poc_msb_cycle_lt[lx][j] + *prev_delta_poc_msb; + lt_poc += poc - delta * max_poc_lsb - (poc & (max_poc_lsb - 1)); + *prev_delta_poc_msb = delta; + } + return lt_poc; +} + +int ff_vvc_slice_rpl(VVCContext *s, VVCFrameContext *fc, SliceContext *sc) +{ + const VVCSPS *sps = fc->ps.sps; + const H266RawPPS *pps = fc->ps.pps->r; + const VVCPH *ph = &fc->ps.ph; + const H266RawSliceHeader *rsh = sc->sh.r; + const int max_poc_lsb = sps->max_pic_order_cnt_lsb; + const H266RefPicLists *ref_lists = + pps->pps_rpl_info_in_ph_flag ? &ph->r->ph_ref_pic_lists : &rsh->sh_ref_pic_lists; + int ret = 0; + + ret = init_slice_rpl(fc, sc); + if (ret < 0) + return ret; + + for (int lx = L0; lx <= L1; lx++) { + const H266RefPicListStruct *rpls = ref_lists->rpl_ref_list + lx; + RefPicList *rpl = sc->rpl + lx; + int poc_base = ph->poc; + int prev_delta_poc_msb = 0; + + rpl->nb_refs = 0; + for (int i = 0, j = 0; i < rpls->num_ref_entries; i++) { + int poc; + if (!rpls->inter_layer_ref_pic_flag[i]) { + int use_msb = 1; + int ref_flag; + if (rpls->st_ref_pic_flag[i]) { + poc = poc_base + delta_poc_st(rpls, lx, i, sps); + poc_base = poc; + ref_flag = VVC_FRAME_FLAG_SHORT_REF; + } else { + use_msb = ref_lists->delta_poc_msb_cycle_present_flag[lx][j]; + poc = poc_lt(&prev_delta_poc_msb, ph->poc, ref_lists, lx, j, max_poc_lsb); + ref_flag = VVC_FRAME_FLAG_LONG_REF; + j++; + } + ret = add_candidate_ref(s, fc, rpl, poc, ref_flag, use_msb); + if (ret < 0) + return ret; + } else { + avpriv_request_sample(fc->log_ctx, "Inter layer ref"); + ret = AVERROR_PATCHWELCOME; + return ret; + } + } + if ((!rsh->sh_collocated_from_l0_flag) == lx && + rsh->sh_collocated_ref_idx < rpl->nb_refs) + fc->ref->collocated_ref = rpl->ref[rsh->sh_collocated_ref_idx]; + } + return 0; +} + +int ff_vvc_frame_rpl(VVCContext *s, VVCFrameContext *fc, SliceContext *sc) +{ + int ret = 0; + + /* clear the reference flags on all frames except the current one */ + for (int i = 0; i < FF_ARRAY_ELEMS(fc->DPB); i++) { + VVCFrame *frame = &fc->DPB[i]; + + if (frame == fc->ref) + continue; + + mark_ref(frame, 0); + } + + if ((ret = ff_vvc_slice_rpl(s, fc, sc)) < 0) + goto fail; + +fail: + /* release any frames that are now unused */ + for (int i = 0; i < FF_ARRAY_ELEMS(fc->DPB); i++) + ff_vvc_unref_frame(fc, &fc->DPB[i], 0); + return ret; +} + +void ff_vvc_report_frame_finished(VVCFrame *frame) +{ + ff_vvc_report_progress(frame, VVC_PROGRESS_MV, INT_MAX); + ff_vvc_report_progress(frame, VVC_PROGRESS_PIXEL, INT_MAX); +} + +static int is_progress_done(const FrameProgress *p, const VVCProgressListener *l) +{ + return p->progress[l->vp] > l->y; +} + +static void add_listener(VVCProgressListener **prev, VVCProgressListener *l) +{ + l->next = *prev; + *prev = l; +} + +static VVCProgressListener* remove_listener(VVCProgressListener **prev, VVCProgressListener *l) +{ + *prev = l->next; + l->next = NULL; + return l; +} + +static VVCProgressListener* get_done_listener(FrameProgress *p, const VVCProgress vp) +{ + VVCProgressListener *list = NULL; + VVCProgressListener **prev = &p->listener[vp]; + + while (*prev) { + if (is_progress_done(p, *prev)) { + VVCProgressListener *l = remove_listener(prev, *prev); + add_listener(&list, l); + } else { + prev = &(*prev)->next; + } + } + return list; +} + +void ff_vvc_report_progress(VVCFrame *frame, const VVCProgress vp, const int y) +{ + FrameProgress *p = frame->progress; + VVCProgressListener *l = NULL; + + ff_mutex_lock(&p->lock); + + av_assert0(p->progress[vp] < y || p->progress[vp] == INT_MAX); + p->progress[vp] = y; + l = get_done_listener(p, vp); + ff_cond_signal(&p->cond); + + ff_mutex_unlock(&p->lock); + + while (l) { + l->progress_done(l); + l = l->next; + } +} + +void ff_vvc_add_progress_listener(VVCFrame *frame, VVCProgressListener *l) +{ + FrameProgress *p = frame->progress; + + ff_mutex_lock(&p->lock); + + if (is_progress_done(p, l)) { + ff_mutex_unlock(&p->lock); + l->progress_done(l); + } else { + add_listener(p->listener + l->vp, l); + ff_mutex_unlock(&p->lock); + } +} diff --git a/libavcodec/vvc/vvc_refs.h b/libavcodec/vvc/vvc_refs.h new file mode 100644 index 0000000000..cd3b5f5632 --- /dev/null +++ b/libavcodec/vvc/vvc_refs.h @@ -0,0 +1,57 @@ +/* + * VVC reference management + * + * Copyright (C) 2023 Nuo Mi + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef AVCODEC_VVC_VVC_REFS_H +#define AVCODEC_VVC_VVC_REFS_H + +#include "vvcdec.h" + +int ff_vvc_output_frame(VVCContext *s, VVCFrameContext *fc, struct AVFrame *out, int no_output_of_prior_pics_flag, int flush); +void ff_vvc_bump_frame(VVCContext *s, VVCFrameContext *fc); +int ff_vvc_set_new_ref(VVCContext *s, VVCFrameContext *fc, struct AVFrame **frame); +const RefPicList *ff_vvc_get_ref_list(const VVCFrameContext *fc, const VVCFrame *ref, int x0, int y0); +int ff_vvc_frame_rpl(VVCContext *s, VVCFrameContext *fc, SliceContext *sc); +int ff_vvc_slice_rpl(VVCContext *s, VVCFrameContext *fc, SliceContext *sc); +void ff_vvc_unref_frame(VVCFrameContext *fc, VVCFrame *frame, int flags); +void ff_vvc_clear_refs(VVCFrameContext *fc); + +typedef enum VVCProgress { + VVC_PROGRESS_MV, + VVC_PROGRESS_PIXEL, + VVC_PROGRESS_LAST, +} VVCProgress; + +typedef struct VVCProgressListener VVCProgressListener; +typedef void (*progress_done_fn)(VVCProgressListener *l); + +struct VVCProgressListener { + VVCProgress vp; + int y; + progress_done_fn progress_done; + VVCProgressListener *next; //used by ff_vvc_add_progress_listener only +}; + +void ff_vvc_report_frame_finished(VVCFrame *frame); +void ff_vvc_report_progress(VVCFrame *frame, VVCProgress vp, int y); +void ff_vvc_add_progress_listener(VVCFrame *frame, VVCProgressListener *l); + +#endif // AVCODEC_VVC_VVC_REFS_H