From patchwork Sun Jan 30 06:27:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 33926 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6602:2c4e:0:0:0:0 with SMTP id x14csp1771430iov; Sat, 29 Jan 2022 22:32:21 -0800 (PST) X-Google-Smtp-Source: ABdhPJwYrHhXH0fEtdfG2gBv325Qc5lh8DxNVu1TURVTs3UCsiHWi3DBKdloplBqYYTqRxNidiL0 X-Received: by 2002:a17:907:7d93:: with SMTP id oz19mr12853842ejc.350.1643524341655; Sat, 29 Jan 2022 22:32:21 -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 m14si6131779edd.534.2022.01.29.22.32.21; Sat, 29 Jan 2022 22:32:21 -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=gFrbKm+P; 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 7E0E168B296; Sun, 30 Jan 2022 08:29:02 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from EUR05-VI1-obe.outbound.protection.outlook.com (mail-vi1eur05olkn2011.outbound.protection.outlook.com [40.92.90.11]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 5D48368B205 for ; Sun, 30 Jan 2022 08:28:59 +0200 (EET) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Xrpmn171P4A2lCn7pCjFU2hn5EeuvoHhvxC+algbxBUH7ZlrKflYBaN07wgdN9+deK1D32M0CUHQbVzK/+qU7+EnybVMWhqxdPWcVVmJqSUQMlwax0oRXXTicpi/Tu66lE6dSFZaJmtFKbU2niTn0nne0nwsQhKIfKoOSyYuPQxEjuQ8UGQwilqbiqseBfEPtJlU5IWur1r6n8KOascHLOOI5dbmuFjxwCBRn7TH6fpCxd1jM9lpimJ6+83J4WcAnY///BaXye4Lgpd59XIVriIUPymjvtBu8wUQJUddeTluJnRidy5Hdsdk3XeWotXtkqqGgmaKS8Ea0NVh4uN8VA== 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=pGT3eHNK0eYIDNhQWfwD4GrOdFcIqvpgbcXYNkOmL4o=; b=e8TncXwrubVs2Zg5SReQ91PZA8vGCFFO7Un/gIW7OKWxYrVJzGXtTB/oLJ/FvCS8SnxiXuJsWaLX876sXPTacdO6/HDaVOONBKtFQDsH019ciPhG+T0D6UNe3EU7wAFmc+Hkb/LN5FDHAte7gx7ZK8S5Vs4qf50pMp20UJpIMF3w6esvP4cZQKsh6uXA/DpecXpzTMHmbzXVj6ad847O9/q66HUo1b4Dzx/kGNIQaQkx8BH7WsgQJorMkZ91LmUkqqo/r1fmib8OYFU8mLrRMMYHXRrV+qqeSSYqUdgoEE4P/OOON2pug2KyMNgK4WhFpNfGBPI/8kB8wI3RCplS4Q== 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=pGT3eHNK0eYIDNhQWfwD4GrOdFcIqvpgbcXYNkOmL4o=; b=gFrbKm+PI3H2syZn/cVtNOaHDNfm5A8ZZztNPejd4uDSpfeRLhqByt06Fh2tH2OF4EtIstfmgYDa1Nxma4LCH5AD59EI7IcA8EeGAeEIYk2rHmf0yUBhpfaXK0qi5amYdkdY0DWfFhxqrzF7nPM+zrbhE85pC6QhlCvnycdG/FPQ5ZQsoTYQlIDvLG9gSvWInpDCHEEQKjrvHgSdg1QQL35Kui56sAsxX+RsHNy67iJMfYHNLHzaab7WXqWF3Iv9vh3sXC5XjTZobxcpdFXu5IgFLlAMxfqcpZ8ibURg7hnpMOx3UsoZVqETJNawJZIacNcbK/asioT4lBwMa5WKMQ== Received: from AM7PR03MB6660.eurprd03.prod.outlook.com (2603:10a6:20b:1c1::22) by PAXPR03MB7666.eurprd03.prod.outlook.com (2603:10a6:102:202::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4930.20; Sun, 30 Jan 2022 06:28:54 +0000 Received: from AM7PR03MB6660.eurprd03.prod.outlook.com ([fe80::ac56:2ff4:d304:ab22]) by AM7PR03MB6660.eurprd03.prod.outlook.com ([fe80::ac56:2ff4:d304:ab22%6]) with mapi id 15.20.4930.021; Sun, 30 Jan 2022 06:28:54 +0000 From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Sun, 30 Jan 2022 07:27:34 +0100 Message-ID: X-Mailer: git-send-email 2.32.0 In-Reply-To: References: X-TMN: [mp1U+z9Rhwey0kmw4Zb7EGkleGLqGJX5] X-ClientProxiedBy: AM6PR10CA0044.EURPRD10.PROD.OUTLOOK.COM (2603:10a6:209:80::21) To AM7PR03MB6660.eurprd03.prod.outlook.com (2603:10a6:20b:1c1::22) X-Microsoft-Original-Message-ID: <20220130062749.936489-25-andreas.rheinhardt@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 4aa928e0-1bf3-45dd-fb9d-08d9e3b9c9c7 X-MS-TrafficTypeDiagnostic: PAXPR03MB7666:EE_ X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 5j5lq6KlV+LkfLwcXh6+uBZoWdynuZBB5RqMuP760V5WmnvlYeMU3sWo1Gle/Qw/047iEYiGhUznVGsRSwYnHDEr0h+c0SSC2c9+lgheAmnncYR8Y2zJu/DRHjdq+pWdpS7f/gCEM8JoKdUJwILpEzmdQzNdCx4AuWNBlb0UcL+kUKCltjqwb3mXwwWfp+PtmQxZZfzYQ+ZdDSR1Acn7r1KbVSHpT9ISIQTRzXE1b1vTAZ6JZdvpx9mNHZEq2IUJrqvu81MWH/F4ksKiFWid0dVoSBh596xOk6KOItA81S+9muuz5roaUh3+BnpoAZ9oE2Gus2pQzpZ7nHVJt96MqiK9a//UHGXZ0KUZ4GMWJ3IA5jvanQ6jJzuq6l7ZZ6d0EXc2vuE3JYcVHOFSMsI6x38GMQZx7M0gmAOurSlqpveF8yVYf9ELPEaiaf11x2GnM09a4u9/jM/h5Xge8PjtxRfyt2VPooOke1f0YSDvZi+yiWe2zyzuAyPNYEq+CoLdsPV0/wW74359Ohh9Av5tQVeG/cvrjlABV15XqVorc+alk/NLbEYMrXly6wRyJsnC X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: ZQihNjQl7vSMpdV4+xj5Vl1id/DblRhFYQ6oHIvqf2h1sDp/NQj5WZL04Ycs21Q8T3uMGGEJFQtMlajy899DjWSSdK5Q9FlAOJoA6/O+9O5BotE38Sp6GH7KFU3+4QHjv7GmYMrZ/ABqsJLUnnH7r37uuBwT+QK53JSZyK8kzKvH4PQAQxJlBCQENcQwj2fX1P0TZ7t9hHpJw8EyYlHdNLAhYamgJx07KyydIdnjX02r1HPdpvqWCScfpUIilImb03C1i1Av3Rbmauok8wkgIzZ/cFZsa7E4DcO4PCgMrqXhHyGvmQ2yjfj9++gmzvljRtqoFCsZN9ytwVvFc12WYiVgokXLE4CAPh3Cr0aVK1LC5sKkJXytgnXe6kpUlwn+bwSgGzWvg9ZW+IceT/h0hBm7hNA4awulGTC9qyLglM+TEpQ7JBouFdHgT5sSYDe3Ne2HP9yZ3QM5wScmgRwG7T5kh4dUNVM/vMCA6j/usuQdXSSWGeu2PH0L+GX6FYeOD/ml+ATsg3WSAQmkA4vIErAQB6tMY9+kJIlO2MnTbtZ8GkAoxFQU2q6uD7/y+ug6J12aOKU4bR3b2lqOS/7vRu8SCgCWwqV6CPN2jwu28SIJPucLV6zipvwxJehYBLwlIiyWOjQLCHNbapNcXvgVjbSVnIKlpuxiEQUNCKgoPCCY0hxJGtT9MJj7kUKla5dorsj+vwkhaS0bEQ7qeYa8XFgXRYDK7z8LwQQCDH2jEJpbR7J0EoK0/oQau/lnsJLeYHPc9BpQD95D8k8f9LE3mD8i/yEUAuh678cuOWq+syq2qHEDUzU6BIN3+LUUf+i6NDA/AiHrx9TCKs2+Bf8QWc/Vp0Wh3YOn0e+9tM+/omk7wKmxLg3yuu3Vka/6A2TnGRBPvIA77T49X7UkxoAt6ZsnU+h/+NADnyVPZcv35lkYZqgFoAOmXY2vDB1+E3DqKryu3WKo3sk31UJHpssqKw== X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 4aa928e0-1bf3-45dd-fb9d-08d9e3b9c9c7 X-MS-Exchange-CrossTenant-AuthSource: AM7PR03MB6660.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Jan 2022 06:28:54.4354 (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: PAXPR03MB7666 Subject: [FFmpeg-devel] [PATCH 26/41] avcodec/mpegvideo: Move input_picture list to MPVMainEncContext 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: +/vTfy3MuDWr Also avoid an allocation while doing so. Signed-off-by: Andreas Rheinhardt --- libavcodec/mpegvideo.h | 1 - libavcodec/mpegvideo_enc.c | 60 ++++++++++++++++++-------------------- libavcodec/mpegvideoenc.h | 2 ++ 3 files changed, 31 insertions(+), 32 deletions(-) diff --git a/libavcodec/mpegvideo.h b/libavcodec/mpegvideo.h index a334acb3aa..98810ea231 100644 --- a/libavcodec/mpegvideo.h +++ b/libavcodec/mpegvideo.h @@ -118,7 +118,6 @@ typedef struct MPVContext { ptrdiff_t linesize; ///< line size, in bytes, may be different from width ptrdiff_t uvlinesize; ///< line size, for chroma in bytes, may be different from width Picture *picture; ///< main picture buffer - Picture **input_picture; ///< next pictures on display order for encoding Picture **reordered_input_picture; ///< pointer to the next pictures in coded order for encoding /** bit output */ diff --git a/libavcodec/mpegvideo_enc.c b/libavcodec/mpegvideo_enc.c index 9b05b44d36..1cf194dcaf 100644 --- a/libavcodec/mpegvideo_enc.c +++ b/libavcodec/mpegvideo_enc.c @@ -825,7 +825,6 @@ av_cold int ff_mpv_encode_init(AVCodecContext *avctx) !FF_ALLOCZ_TYPED_ARRAY(s->q_intra_matrix16, 32) || !FF_ALLOCZ_TYPED_ARRAY(s->q_chroma_intra_matrix16, 32) || !FF_ALLOCZ_TYPED_ARRAY(s->q_inter_matrix16, 32) || - !FF_ALLOCZ_TYPED_ARRAY(s->input_picture, MAX_PICTURE_COUNT) || !FF_ALLOCZ_TYPED_ARRAY(s->reordered_input_picture, MAX_PICTURE_COUNT)) return AVERROR(ENOMEM); @@ -952,7 +951,6 @@ av_cold int ff_mpv_encode_end(AVCodecContext *avctx) av_freep(&s->q_inter_matrix); av_freep(&s->q_intra_matrix16); av_freep(&s->q_inter_matrix16); - av_freep(&s->input_picture); av_freep(&s->reordered_input_picture); av_freep(&s->dct_offset); @@ -1137,9 +1135,9 @@ static int load_input_picture(MPVMainEncContext *m, const AVFrame *pic_arg) pic->f->pts = pts; // we set this here to avoid modifying pic_arg } else { /* Flushing: When we have not received enough input frames, - * ensure s->input_picture[0] contains the first picture */ + * ensure m->input_picture[0] contains the first picture */ for (flush_offset = 0; flush_offset < encoding_delay + 1; flush_offset++) - if (s->input_picture[flush_offset]) + if (m->input_picture[flush_offset]) break; if (flush_offset <= 1) @@ -1150,9 +1148,9 @@ static int load_input_picture(MPVMainEncContext *m, const AVFrame *pic_arg) /* shift buffer entries */ for (i = flush_offset; i < MAX_PICTURE_COUNT /*s->encoding_delay + 1*/; i++) - s->input_picture[i - flush_offset] = s->input_picture[i]; + m->input_picture[i - flush_offset] = m->input_picture[i]; - s->input_picture[encoding_delay] = (Picture*) pic; + m->input_picture[encoding_delay] = (Picture*) pic; return 0; } @@ -1249,11 +1247,11 @@ static int estimate_best_b_count(MPVMainEncContext *m) FF_LAMBDA_SHIFT; for (i = 0; i < s->max_b_frames + 2; i++) { - Picture pre_input, *pre_input_ptr = i ? s->input_picture[i - 1] : + Picture pre_input, *pre_input_ptr = i ? m->input_picture[i - 1] : s->next_picture_ptr; uint8_t *data[4]; - if (pre_input_ptr && (!i || s->input_picture[i - 1])) { + if (pre_input_ptr && (!i || m->input_picture[i - 1])) { pre_input = *pre_input_ptr; memcpy(data, pre_input_ptr->f->data, sizeof(data)); @@ -1285,7 +1283,7 @@ static int estimate_best_b_count(MPVMainEncContext *m) AVCodecContext *c; int64_t rd = 0; - if (!s->input_picture[j]) + if (!m->input_picture[j]) break; c = avcodec_alloc_context3(NULL); @@ -1377,13 +1375,13 @@ static int select_input_picture(MPVMainEncContext *m) s->reordered_input_picture[MAX_PICTURE_COUNT - 1] = NULL; /* set next picture type & ordering */ - if (!s->reordered_input_picture[0] && s->input_picture[0]) { + if (!s->reordered_input_picture[0] && m->input_picture[0]) { if (s->frame_skip_threshold || s->frame_skip_factor) { if (m->picture_in_gop_number < m->gop_size && s->next_picture_ptr && - skip_check(m, s->input_picture[0], s->next_picture_ptr)) { + skip_check(m, m->input_picture[0], s->next_picture_ptr)) { // FIXME check that the gop check above is +-1 correct - av_frame_unref(s->input_picture[0]->f); + av_frame_unref(m->input_picture[0]->f); ff_vbv_update(m, 0); @@ -1393,7 +1391,7 @@ static int select_input_picture(MPVMainEncContext *m) if (/*s->picture_in_gop_number >= s->gop_size ||*/ !s->next_picture_ptr || s->intra_only) { - s->reordered_input_picture[0] = s->input_picture[0]; + s->reordered_input_picture[0] = m->input_picture[0]; s->reordered_input_picture[0]->f->pict_type = AV_PICTURE_TYPE_I; s->reordered_input_picture[0]->f->coded_picture_number = s->coded_picture_number++; @@ -1402,38 +1400,38 @@ static int select_input_picture(MPVMainEncContext *m) if (s->avctx->flags & AV_CODEC_FLAG_PASS2) { for (i = 0; i < s->max_b_frames + 1; i++) { - int pict_num = s->input_picture[0]->f->display_picture_number + i; + int pict_num = m->input_picture[0]->f->display_picture_number + i; if (pict_num >= m->rc_context.num_entries) break; - if (!s->input_picture[i]) { + if (!m->input_picture[i]) { m->rc_context.entry[pict_num - 1].new_pict_type = AV_PICTURE_TYPE_P; break; } - s->input_picture[i]->f->pict_type = + m->input_picture[i]->f->pict_type = m->rc_context.entry[pict_num].new_pict_type; } } if (m->b_frame_strategy == 0) { b_frames = s->max_b_frames; - while (b_frames && !s->input_picture[b_frames]) + while (b_frames && !m->input_picture[b_frames]) b_frames--; } else if (m->b_frame_strategy == 1) { for (i = 1; i < s->max_b_frames + 1; i++) { - if (s->input_picture[i] && - s->input_picture[i]->b_frame_score == 0) { - s->input_picture[i]->b_frame_score = + if (m->input_picture[i] && + m->input_picture[i]->b_frame_score == 0) { + m->input_picture[i]->b_frame_score = get_intra_count(s, - s->input_picture[i ]->f->data[0], - s->input_picture[i - 1]->f->data[0], + m->input_picture[i ]->f->data[0], + m->input_picture[i - 1]->f->data[0], s->linesize) + 1; } } for (i = 0; i < s->max_b_frames + 1; i++) { - if (!s->input_picture[i] || - s->input_picture[i]->b_frame_score - 1 > + if (!m->input_picture[i] || + m->input_picture[i]->b_frame_score - 1 > s->mb_num / m->b_sensitivity) break; } @@ -1442,7 +1440,7 @@ static int select_input_picture(MPVMainEncContext *m) /* reset scores */ for (i = 0; i < b_frames + 1; i++) { - s->input_picture[i]->b_frame_score = 0; + m->input_picture[i]->b_frame_score = 0; } } else if (m->b_frame_strategy == 2) { b_frames = estimate_best_b_count(m); @@ -1453,11 +1451,11 @@ static int select_input_picture(MPVMainEncContext *m) emms_c(); for (i = b_frames - 1; i >= 0; i--) { - int type = s->input_picture[i]->f->pict_type; + int type = m->input_picture[i]->f->pict_type; if (type && type != AV_PICTURE_TYPE_B) b_frames = i; } - if (s->input_picture[b_frames]->f->pict_type == AV_PICTURE_TYPE_B && + if (m->input_picture[b_frames]->f->pict_type == AV_PICTURE_TYPE_B && b_frames == s->max_b_frames) { av_log(s->avctx, AV_LOG_ERROR, "warning, too many B-frames in a row\n"); @@ -1470,21 +1468,21 @@ static int select_input_picture(MPVMainEncContext *m) } else { if (s->avctx->flags & AV_CODEC_FLAG_CLOSED_GOP) b_frames = 0; - s->input_picture[b_frames]->f->pict_type = AV_PICTURE_TYPE_I; + m->input_picture[b_frames]->f->pict_type = AV_PICTURE_TYPE_I; } } if ((s->avctx->flags & AV_CODEC_FLAG_CLOSED_GOP) && b_frames && - s->input_picture[b_frames]->f->pict_type == AV_PICTURE_TYPE_I) + m->input_picture[b_frames]->f->pict_type == AV_PICTURE_TYPE_I) b_frames--; - s->reordered_input_picture[0] = s->input_picture[b_frames]; + s->reordered_input_picture[0] = m->input_picture[b_frames]; if (s->reordered_input_picture[0]->f->pict_type != AV_PICTURE_TYPE_I) s->reordered_input_picture[0]->f->pict_type = AV_PICTURE_TYPE_P; s->reordered_input_picture[0]->f->coded_picture_number = s->coded_picture_number++; for (i = 0; i < b_frames; i++) { - s->reordered_input_picture[i + 1] = s->input_picture[i]; + s->reordered_input_picture[i + 1] = m->input_picture[i]; s->reordered_input_picture[i + 1]->f->pict_type = AV_PICTURE_TYPE_B; s->reordered_input_picture[i + 1]->f->coded_picture_number = diff --git a/libavcodec/mpegvideoenc.h b/libavcodec/mpegvideoenc.h index 227701b3e3..a06e208be8 100644 --- a/libavcodec/mpegvideoenc.h +++ b/libavcodec/mpegvideoenc.h @@ -37,6 +37,8 @@ typedef MPVContext MPVEncContext; typedef struct MPVMainEncContext { MPVMainContext common; + Picture *input_picture[MAX_PICTURE_COUNT]; ///< next pictures in display order + int me_penalty_compensation; int me_pre; ///< prepass for motion estimation