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 5C47346CCB for ; Fri, 7 Jul 2023 14:20:51 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 63C1068C7E3; Fri, 7 Jul 2023 17:20:48 +0300 (EEST) Received: from APC01-PSA-obe.outbound.protection.outlook.com (mail-psaapc01olkn2095.outbound.protection.outlook.com [40.92.52.95]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 29E5968C70B for ; Fri, 7 Jul 2023 17:20:40 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=U/fgFefI72KYQbidWTB95ldRVwlc3NCATatLhzvSebbrdqLl9gdHh/Ul8scNEp+NP5+T+Uj+TZKd9pEZlcyZP2v3Exxd3AmWNhdsoslhS3OSZLTw4XkZMQtBa6Z+4S/uyr/0OAnbDRDYr4Wro8rz54k/LcvUqVxNzifDl5HouzRRG30Z9wIpqyPjNHy+wRBcRdUubmOUnEQulLoNqFijBVHDGtk3bRGRQnAcPUoCHGuxBnsPEqu00QLrtLFDSkFRVHbrRs3uMhJswavMFmJvN+bazfDRxmB41PFEKir+o0RPm4szO2bzAVAxMdylXPBCfisuXcFwapskBVvCB2R0pQ== 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=xT9/gJyDs8WjgaowmI1XcG5NWIgA9uBDpErk4zsWx9Y=; b=KknsG/7K7V0zdK5W+g+2jDoqcFiOMOrqbmJlV0gMogz+aLEzwmfRxOWBaUJEucd8Bnbt+uaWePFeZJBgM8ThuGXdDtmlYqs8WvPq1kfSBTzCLaYa4SjdAUqqqf1GJaLplFrSf8HzDG0kQIpTWChJTitYcOl++4iNdWcvpQXzz6eBy7f4M66xu3aHjoQ6uMBuFQEP/tpQIRQyxscw+ATps0hgYhoMggMByGkU5v6QKPcyvQgS09vmdTyfDus+vCYxgP7dDtBvrRhnbdrldgYJfMN7w7Trsn6qiIrNY6bupiy8QA5UXfucAi2946ksHPHD7RV7JhKlkVzS/+alShZ2hQ== 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=xT9/gJyDs8WjgaowmI1XcG5NWIgA9uBDpErk4zsWx9Y=; b=e++VvtxCeLo59K5f6HWX2X2eyPSaB8I2rPD0VHG19192875qCN5hS5NkSq1LFLoFE3mJpJyJMu5SKAOjjjZoRxQCJoi0LQNPRp88Qpi18IKBwxfgz3l+EWv11AdlDdxAdnMA5piShdOp25yJZ/kt5Sj12IamCtUKT8jjw8ArQYFCATeJ+hWaKRbNdWcT61SYew9iQN0vEALkUWGtyf8mH7G7YwSSzcH5e7/ymxu6BI7wM3jumDS09iMTLDe+vGwyfL5DhA4SvqRJYDmTR4I7BrkCq5LXiIa1xrEOTBG1n5KL3bAifOT8fmoNwEICseLZdF6bJ44g9kKVPKbA6SuGvg== Received: from TYSPR06MB6433.apcprd06.prod.outlook.com (2603:1096:400:47a::6) by KL1PR0601MB5445.apcprd06.prod.outlook.com (2603:1096:820:c0::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6565.25; Fri, 7 Jul 2023 14:05:54 +0000 Received: from TYSPR06MB6433.apcprd06.prod.outlook.com ([fe80::7d80:1294:fd54:a7f]) by TYSPR06MB6433.apcprd06.prod.outlook.com ([fe80::7d80:1294:fd54:a7f%7]) with mapi id 15.20.6565.016; Fri, 7 Jul 2023 14:05:54 +0000 From: Nuo Mi To: ffmpeg-devel@ffmpeg.org Date: Fri, 7 Jul 2023 22:05:31 +0800 Message-ID: X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230707140540.10279-1-nuomi2021@gmail.com> References: <20230707140540.10279-1-nuomi2021@gmail.com> X-TMN: [0gXM9DMTUlOMyTZowejYGZBebGq2fPhF] X-ClientProxiedBy: TYCP286CA0251.JPNP286.PROD.OUTLOOK.COM (2603:1096:400:456::20) To TYSPR06MB6433.apcprd06.prod.outlook.com (2603:1096:400:47a::6) X-Microsoft-Original-Message-ID: <20230707140540.10279-6-nuomi2021@gmail.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 2 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: TYSPR06MB6433:EE_|KL1PR0601MB5445:EE_ X-MS-Office365-Filtering-Correlation-Id: a57d8444-be5e-487c-2e6b-08db7ef3473f X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: CSMWsC1DzhjE6j+zC1jhWMUboAX9g9YxzTMKuN4/P/0b+j2PTPRjJG2XV2Bxwc98Ab2qaJlrlyyFgs8Zj3ux0EXDBcJEAQO8BQy+9gqu25AkImYHRzq/Q3mKnIh/5vqj7bL842aItrmFOz2xoAXr0ohjFsiteU+lHLJnPI3osVC0+tqt2ngrUxWfcECv9uz3myKQxfKFUMdZfprVDwW1MY98c2TvxFukkK+vnpWARBhbZdPHBkNkg6202pXTUkQqONSpKTmPl89HIpGrPxf0BsdrB/Uqax8MuAapeY9E1NqziRyTlEpRN/yDeRIqFH/pAB+Ygwv3p5nv+E7cDv7rheZaxQrfhbh4l/X6K2yRfsZyqVxLTR0cIyuR6c48WjvepiCqv2Py1KmkY4/1hXQjpXK5Blui3xAPQjtpSHkYTJxl+y//cKmj4MwtAV1B0S4F+I71togB3XeDda+aY3yWQmPS0JZ1yO6bPvqqNS4jZC5Ic0yF4vLD3DFjqY7n48cUunxxIZ50GlUAUbIlNEPczbwJtW5G5sb5pCaUQC1eaU8LiK6NlbjqT+7UFCfNj9/JhfK4KFXLhTLtCiUS0K85OE6sm8hDbL10iXfBBB2TMOC+jOrxOmu2zM4UZD+pjz+W X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?06/vHxtQ4OqBrAmpJRxzkeqWChXOEsjVhdg4Q0CqAMQ/zo1mnGYRohQ6WKNx?= =?us-ascii?Q?8poahYkdDmsnqaeekVRebzwx8MEfHZHSzwwiTAvbUT6H4ddCpTMpT1TQ9JTv?= =?us-ascii?Q?ByyMtW4iG48KgQRwm2hqN1twcxKADtdmIfoRYd+v4ai6DhklqZIEVw4sKm8e?= =?us-ascii?Q?cUdzPoeUnrr15En3oRzGAEZYod5YX/RkcNTP8Ev0kiUDAFuVSrV+6pL5SWgf?= =?us-ascii?Q?7q1W0nMMH1ONyyBAN0NpNegzL4P72zXa7Z5npnK6smzax4NSFnYBjOWG0c6w?= =?us-ascii?Q?nB0TNMamW/eUAU5BEeO3Wrc93LLhJoUWfbtTYa4D4x62wt8aiEDbl/IjzT6w?= =?us-ascii?Q?/5edYrNyH7BstyZWtwWVtpvLvrineDp1Y9QbemV1hIdtY+KAfS3lJ+GVOK0g?= =?us-ascii?Q?mqSVvrfitLHHQ5AL726XEdkbXNMJxCC0pn6wc8Ra3soQjs3i6ENkzhJBthbA?= =?us-ascii?Q?YiHofGzmb8h61JapJ1cRHKye5bfzdMk6mR7a35Elc0YXA4eA8HtdM/YCr9xW?= =?us-ascii?Q?buWsuUCvgiWY/4PejNCRGky4cjBCJmjvzSaqSR0k5t+wf8dyst/ee4FoZeGN?= =?us-ascii?Q?rdSMVku06xV2gRpYfi0X+ZbYMLu6rM+cwz/qhZ/DAQ36ThdsgcI+yAfCgaZM?= =?us-ascii?Q?GZqIemkLvjn+eHHlw4Buf/qkhwf1nEYmtZd98OsTloQ14OfznspU4y4HfPrs?= =?us-ascii?Q?t6gVFPyxN3pU5T12ogTacT1kB5nSsV7+ejWcGZzfRCfc5Jo3xcKHbK9pHQ/n?= =?us-ascii?Q?qpmmyyfF5RkZBvQjyWObO4BDke/+u+9Y9KT9ZNed28SHZRvrQpYlOen+ZLD5?= =?us-ascii?Q?yfhDWpP5G7+O0fztwR0GwuOrn0Wy8mQkdIdAkNLRXHhpfqBYb/MpPqHQzSVZ?= =?us-ascii?Q?PpSDHEh8Ew++gMjOKeoZ1nGE3lRn+PSlgGEuu52xnVQolq+9zVUW2KOVoFNU?= =?us-ascii?Q?ElbLW3wrWq2t5W41VACvDEiCAvS5/M+FIVmC48479a4iYMcXJnyenJb3YYnd?= =?us-ascii?Q?n65IUgDOUUeoAZacd/xTiChq2cM9sPMkUh8pXm3gpE3KNvt9uwC+gdo/EeD6?= =?us-ascii?Q?9vuHTMNBBhU/xMVELJDuzElONmpSbnSG63YXo2oSxwC8MuRGhmw3okd+6NN6?= =?us-ascii?Q?b8EYlFmrSD7cPRjeU4N/H4LZP5HOz9/B5lb6WGjhMreiyGfiy+cd3X+6gq+8?= =?us-ascii?Q?zAkvY3HC2ETdlztE7yfsRvjfmyWXOk1T522I3yiwSX+NOK7dpQh2NR6HwxFv?= =?us-ascii?Q?YeXNEFNxkCTpKEAePrau?= X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: a57d8444-be5e-487c-2e6b-08db7ef3473f X-MS-Exchange-CrossTenant-AuthSource: TYSPR06MB6433.apcprd06.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Jul 2023 14:05:54.3100 (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: KL1PR0601MB5445 Subject: [FFmpeg-devel] [PATCH v2 05/14] 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 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: --- libavcodec/vvc/Makefile | 3 +- libavcodec/vvc/vvc_refs.c | 502 ++++++++++++++++++++++++++++++++++++++ libavcodec/vvc/vvc_refs.h | 51 ++++ 3 files changed, 555 insertions(+), 1 deletion(-) 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 39f376fddc..9d2e6f60c4 100644 --- a/libavcodec/vvc/Makefile +++ b/libavcodec/vvc/Makefile @@ -6,4 +6,5 @@ OBJS-$(CONFIG_VVC_DECODER) += vvc/vvcdec.o \ vvc/vvc_cabac.o \ vvc/vvc_ctu.o \ vvc/vvc_data.o \ - vvc/vvc_ps.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..4edbf3514f --- /dev/null +++ b/libavcodec/vvc/vvc_refs.c @@ -0,0 +1,502 @@ +/* + * 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 "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]; + pthread_mutex_t lock; + pthread_cond_t 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) { + ff_thread_release_ext_buffer(fc->avctx, &frame->tf); + + av_buffer_unref(&frame->progress_buf); + + av_buffer_unref(&frame->tab_dmvr_mvf_buf); + frame->tab_dmvr_mvf = NULL; + + av_buffer_unref(&frame->rpl_buf); + av_buffer_unref(&frame->rpl_tab_buf); + frame->rpl_tab = NULL; + + frame->collocated_ref = NULL; + } +} + +const RefPicList *ff_vvc_get_ref_list(const VVCFrameContext *fc, const VVCFrame *ref, int x0, int y0) +{ + int x_cb = x0 >> fc->ps.sps->ctb_log2_size_y; + int y_cb = y0 >> fc->ps.sps->ctb_log2_size_y; + int pic_width_cb = fc->ps.pps->ctb_width; + 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) +{ + int i; + for (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(void *opaque, uint8_t *data) +{ + FrameProgress *p = (FrameProgress*)data; + pthread_cond_destroy(&p->cond); + pthread_mutex_destroy(&p->lock); + av_free(data); +} + +static AVBufferRef *alloc_progress(void) +{ + int ret; + AVBufferRef *buf; + FrameProgress *p = av_mallocz(sizeof(FrameProgress)); + + if (!p) + return NULL; + ret = pthread_cond_init(&p->cond, NULL); + if (ret) { + av_free(p); + return NULL; + } + + ret = pthread_mutex_init(&p->lock, NULL); + if (ret) { + pthread_cond_destroy(&p->cond); + av_free(p); + return NULL; + } + buf = av_buffer_create((void*)p, sizeof(*p), free_progress, NULL, 0); + if (!buf) + free_progress(NULL, (void*)p); + return buf; +} + +static VVCFrame *alloc_frame(VVCContext *s, VVCFrameContext *fc) +{ + const VVCPPS *pps = s->ps.pps; + int i, j, ret; + for (i = 0; i < FF_ARRAY_ELEMS(fc->DPB); i++) { + VVCFrame *frame = &fc->DPB[i]; + if (frame->frame->buf[0]) + continue; + + ret = ff_thread_get_ext_buffer(fc->avctx, &frame->tf, + AV_GET_BUFFER_FLAG_REF); + if (ret < 0) + return NULL; + + frame->rpl_buf = av_buffer_allocz(fc->pkt.nb_nals * sizeof(RefPicListTab)); + if (!frame->rpl_buf) + goto fail; + + frame->tab_dmvr_mvf_buf = av_buffer_pool_get(fc->tab_dmvr_mvf_pool); + if (!frame->tab_dmvr_mvf_buf) + goto fail; + frame->tab_dmvr_mvf = (MvField *)frame->tab_dmvr_mvf_buf->data; + //fixme: remove this + memset(frame->tab_dmvr_mvf, 0, frame->tab_dmvr_mvf_buf->size); + + frame->rpl_tab_buf = av_buffer_pool_get(fc->rpl_tab_pool); + if (!frame->rpl_tab_buf) + goto fail; + frame->rpl_tab = (RefPicListTab **)frame->rpl_tab_buf->data; + frame->ctb_count = pps->ctb_width * pps->ctb_height; + for (j = 0; j < frame->ctb_count; j++) + frame->rpl_tab[j] = (RefPicListTab *)frame->rpl_buf->data; + + + frame->progress_buf = alloc_progress(); + if (!frame->progress_buf) + 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; + int i; + + /* check that this POC doesn't already exist */ + for (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->pic_output_flag) + ref->flags = VVC_FRAME_FLAG_OUTPUT; + + if (!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->conf_win.left_offset; + ref->frame->crop_right = fc->ps.pps->conf_win.right_offset; + ref->frame->crop_top = fc->ps.pps->conf_win.top_offset; + ref->frame->crop_bottom = fc->ps.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 i, min_idx, ret; + + if (no_output_of_prior_pics_flag) { + for (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 (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->dpb.max_dec_pic_buffering[sps->max_sublayers - 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; + int i; + + for (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->dpb.max_dec_pic_buffering[sps->max_sublayers - 1]) { + for (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 (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) +{ + int mask = use_msb ? ~0 : fc->ps.sps->max_pic_order_cnt_lsb - 1; + int i; + + for (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; + int i, y; + + frame = alloc_frame(s, fc); + if (!frame) + return NULL; + + if (!s->avctx->hwaccel) { + if (!sps->pixel_shift) { + for (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 (i = 0; frame->frame->data[i]; i++) + for (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->rpl_buf->size / sizeof(RefPicListTab)) + 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] = (RefPicListTab *)frame->rpl_buf->data + sc->slice_idx; + } + + sc->rpl = (RefPicList *)frame->rpl_tab[sh->ctb_addr_in_curr_slice[0]]; + + return 0; +} + +int ff_vvc_slice_rpl(VVCContext *s, VVCFrameContext *fc, SliceContext *sc) +{ + const VVCSH *sh = &sc->sh; + int i, ret = 0; + + init_slice_rpl(fc, sc); + + for (i = 0; i < 2; i++) { + const VVCRefPicListStruct *rpls = sh->rpls + i; + RefPicList *rpl = sc->rpl + i; + int poc_base = fc->ps.ph->poc; + rpl->nb_refs = 0; + for (int j = 0; j < rpls->num_ref_entries; j++) { + const VVCRefPicListStructEntry *ref = &rpls->entries[j]; + int poc; + if (!ref->inter_layer_ref_pic_flag) { + int use_msb = 1; + int ref_flag; + if (ref->st_ref_pic_flag) { + poc = poc_base + ref->delta_poc_val_st; + poc_base = poc; + ref_flag = VVC_FRAME_FLAG_SHORT_REF; + } else { + use_msb = ref->lt_msb_flag; + poc = ref->lt_poc; + ref_flag = VVC_FRAME_FLAG_LONG_REF; + } + ret = add_candidate_ref(s, fc, rpl, poc, ref_flag, use_msb); + if (ret < 0) + goto fail; + } else { + avpriv_request_sample(fc->avctx, "Inter layer ref"); + ret = AVERROR_PATCHWELCOME; + goto fail; + } + } + if (sh->collocated_list == i && + sh->collocated_ref_idx < rpl->nb_refs) + fc->ref->collocated_ref = rpl->ref[sh->collocated_ref_idx]; + } +fail: + return ret; +} + +int ff_vvc_frame_rpl(VVCContext *s, VVCFrameContext *fc, SliceContext *sc) +{ + int i, ret = 0; + + /* clear the reference flags on all frames except the current one */ + for (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 (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); +} + +void ff_vvc_report_progress(VVCFrame *frame, const VVCProgress vp, const int y) +{ + FrameProgress *p = (FrameProgress*)frame->progress_buf->data; + + pthread_mutex_lock(&p->lock); + + av_assert0(p->progress[vp] < y || p->progress[vp] == INT_MAX); + p->progress[vp] = y; + + pthread_cond_broadcast(&p->cond); + pthread_mutex_unlock(&p->lock); +} + +int ff_vvc_check_progress(VVCFrame *frame, const VVCProgress vp, const int y) +{ + int ready ; + FrameProgress *p = (FrameProgress*)frame->progress_buf->data; + + pthread_mutex_lock(&p->lock); + + ready = p->progress[vp] > y + 1; + + pthread_mutex_unlock(&p->lock); + return ready; +} diff --git a/libavcodec/vvc/vvc_refs.h b/libavcodec/vvc/vvc_refs.h new file mode 100644 index 0000000000..c990a3f12d --- /dev/null +++ b/libavcodec/vvc/vvc_refs.h @@ -0,0 +1,51 @@ +/* + * 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_REFS_H +#define AVCODEC_VVC_REFS_H + +#include "vvcdec.h" + +#define INV_POC INT_MIN +#define GDR_IS_RECOVERED(s) (s->gdr_recovery_point_poc == INV_POC) +#define GDR_SET_RECOVERED(s) (s->gdr_recovery_point_poc = INV_POC) + +int ff_vvc_output_frame(VVCContext *s, VVCFrameContext *fc, 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, 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; + +void ff_vvc_report_frame_finished(VVCFrame *frame); +void ff_vvc_report_progress(VVCFrame *frame, VVCProgress vp, int y); +int ff_vvc_check_progress(VVCFrame *frame, VVCProgress vp, int y); + +#endif // AVCODEC_VVC_REFS_H -- 2.25.1 _______________________________________________ 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".