From patchwork Tue Feb 1 13:06:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 34009 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6602:2c4e:0:0:0:0 with SMTP id x14csp607323iov; Tue, 1 Feb 2022 05:15:46 -0800 (PST) X-Google-Smtp-Source: ABdhPJy8pyj6ChtzNPk8Ir7Q3BncnNPvtV4lLpemj7+ya2Jc6nvMgnqPqpU8X9MRrceG9IbJKB1p X-Received: by 2002:a17:907:6297:: with SMTP id nd23mr21798700ejc.56.1643721346383; Tue, 01 Feb 2022 05:15:46 -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 r4si9363799eda.284.2022.02.01.05.15.45; Tue, 01 Feb 2022 05:15:46 -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=CHYbwfJB; 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 7722568B3C4; Tue, 1 Feb 2022 15:08:41 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from EUR04-VI1-obe.outbound.protection.outlook.com (mail-oln040092075035.outbound.protection.outlook.com [40.92.75.35]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 91BED68B317 for ; Tue, 1 Feb 2022 15:08:39 +0200 (EET) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=V03IwuCM5UfRNyT+IOIAS81B9O7ZSQyKJEV8T9L3Nxap0OTk97CrXILXAJVUJQgIBhzx3j+zLgpYexXflRveA8AdHEltLFqu0beKPUlDltVIiVgjfEmvf6jWHAwPeATjStOC9jwy6Evauki4DlX+4k4J9sjzZymYPFrNSPk/y2/0SZ3rKmCyhcqD9xLlTcCzon78rcCrmkI5D9EpQck1btnw1+iHmlWRoeTfTuWRfHZpqTAg01tkTBc1+XIzmxBJ8e9h46WBIlKXMqr9p7l2ZmEjx7Y78FeXox603ATAm/hf2y7kBikvMRvo51pFdLqkPGMIeTYNsqWanjTl1Pr1wA== 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=RJktWIxy7xbtvNKcpNEFHxA2Eng93cl2KJTVP6cVcK8=; b=cUAUXYAEYvWFO1sqJMtTsMFUvum6qEQ8f24EJQ9jnBQBvbj/lhUQiv3guwvslbLynfCRuw74WWV/Tj+wj3NGxanKny4WxwVNLF1/Zt/h23XLM4pLtbDTA3eeC5mEIay9e+i0+/Q+eblOOcN6bPJaQvb+bJKJtCwhfZDJNrZdkWlKEH/F9wWk6TXOE8ldLWoGy3cv6rwY1Cz+GYlQewjk0h8tRDTuEvglcZEDSRMKYpOpPhlauE5lkhO5V+kd1zkJWOa+S1jlDxcgBPkcrZqePkHM3k5TyXyFz7w6sy6rDHtz4mf5+avXh3Fr/0LE8zzcWG/xIL7VSvCL1M/WcdlC2g== 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=RJktWIxy7xbtvNKcpNEFHxA2Eng93cl2KJTVP6cVcK8=; b=CHYbwfJB0ZaEyax1spiN2b+pA0ODT+l19+ILxxp2mqYUtnnBaobW8cAq4Rw2agQadn+uda2LOfWY57AU8zXVthGh7PNxVPozfj0HIJpS+p/StcSrMRUFAcU4tRMm1w9FkEitEiv8LJ+AnJjudLFyAcZI3t0rpWoaN6uhTWoPRs7PC2WNsoK3jAlsmWRNVGWHuq0Vn5daULZ7iEkFW+EdEPt90IxMeyeeXCz7ahKnRsdTxGmgY6hb2PfYO/yHFU5Qf/pQe2lAX+pRX8RuCRnMwLP3jRIQM4uU6b1EWuJdtJ5Fwg5x1Dt4se0oHxUKi0xF7dIH7+ht75riTEeST8Q51A== Received: from AM7PR03MB6660.eurprd03.prod.outlook.com (2603:10a6:20b:1c1::22) by HE1PR0301MB2330.eurprd03.prod.outlook.com (2603:10a6:3:73::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4930.22; Tue, 1 Feb 2022 13:08:33 +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.4951.011; Tue, 1 Feb 2022 13:08:33 +0000 From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Tue, 1 Feb 2022 14:06:33 +0100 Message-ID: X-Mailer: git-send-email 2.32.0 In-Reply-To: References: X-TMN: [pWOcaE+y3ml9U4zl3/n34SIs6pew5Bcj] X-ClientProxiedBy: AM6P195CA0092.EURP195.PROD.OUTLOOK.COM (2603:10a6:209:86::33) To AM7PR03MB6660.eurprd03.prod.outlook.com (2603:10a6:20b:1c1::22) X-Microsoft-Original-Message-ID: <20220201130706.1420875-35-andreas.rheinhardt@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 5fcc9a26-c141-430a-591c-08d9e583e27a X-MS-TrafficTypeDiagnostic: HE1PR0301MB2330:EE_ X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: br0rlAMvGhSytQT0Biurm+6xBxUHRvTVmPUC8ptIAUm9q8sbQ7Lu2FFK+pAfyhbFkhVrqNIV7V5mPqXmZjpWobGhyitFUe/ksxVKYsmAIuvwTamQXhGjAx9oGpsA+1+qIyQ/ZxKWskpc7eJFqfvy6Q9R8U1h4fBBxXLfOdyIQc/xl1MBLYuS/RFXjhyTVl/rEXbDDGDs4t3LrGOBMAezCgYO/cSsFMgam9Bw3oNcRsj+JMlhwDgw9vVTnKSc606K1IbTzd8XGDHX/Pydd5ISZLK7t3X0pn9oMJdbcy0LcqE3cdj3OFkJm5ljUjlmposlWWjGslYEbAuCyhGPOfc7CSVNDrpmbVKxzSm2FvUj53f4IrNF5mpp5CGY+QO68qMVgvA0hxzJflho6bi26VEfRWSmwRmUDVgeKVyW7Q79GRkuBA7li3whW9uROIDHJABO/W2V63MFXuU5u8wy7mMY/YKT/SM52Wmd6eYKhWnf3cR51cwA5qZXYA2Bwuk0XBTBbsmYOK1QbqWTcgHQHaMaRFpQjFzFB6jTudZRaAOcDrCZQ29uRASvqbVppIFo5Ob0 X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: z+M/o1MxEH1lqXbnBUPUw5KFvZiXrN7QuIXbfkGIUsK1SDxNNa5bL1U1JMvTS+tTW4+XvY7c9eTgC/tIfrn216BNh/i/C5PgQZSqt/UOryxUD7V9FPjvcOOyMTk8N1H/Kf7cZRlNCuDEbjr1dObPd+Va9Sl68f2O5rhCDQRsvBZnZh7JiKoqKM++cUkPFDUh0qllPeeTFCdl1XduSta+hNvy2BpA1w4GcHbTKP6qd9ABm1Jh1kYjag4hXvagEgfWdk/jJw9gBf34rTQQOuzsMSpD1251Nbrok6krRDQwgSRMo1zHpX7c9w/vJBS/37Bo8qiS8wlH9L85uVAm512b/TCfvl7rsG214bhyRjAV6IcV/uIPlJp2HILl/fBedCUo5Yheyb+ZKYYWIB6cO4XmFCkE4GeCkIx9HZW4Aebz7cgtkq9H4vftX10zokmd8sg8ErWeYh6IBZjKsXG8cfShSwqcSLSR65DDiQhAh8ge7UhMq5aGKUTX9kO3ttLDKxgi4TIFy7TPnw4TvbVEZaZtQyzERUxfSEqYsnp26rybvQzYgaPhaDjGwQ59Fk2kbaO/tLz3BFLMjevkIijvycvvoyz47spPx2c66z4pBM41I6jLvvgtfgobiR5qd+8NFh9MCVUJF6CaHvHXO6VIMfoYwtkPxc0ZqxeGTITAxBWyi72EszI1DFJCXZwOdofQJE+0lk0FUnYONLElFcGOXxi5Lhq7BvmGuzaDNWnNU0ndbjWqHItkxQxAKoZIUqwbVMd5u35AsrsYhShxpp/W30LBuU/diZPMk8gPWvvY5z/qOR248CqWXUSnJmE/x9NKwLH2Xvc1QZHchradThA3aASpU5nNFFQ+pzbu0bOYeK9tm3oX3uRe4YIk+nNUFtgg1YD38HJsRF7YOyslIGQCU5QmuLHjYGHkpL0eqRbuV19Gt2ZNCWC8Vxvx3t2lfi+mea5zdlehZrbtFKZvQOlO0Nsrrg== X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 5fcc9a26-c141-430a-591c-08d9e583e27a X-MS-Exchange-CrossTenant-AuthSource: AM7PR03MB6660.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Feb 2022 13:08:05.4031 (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: HE1PR0301MB2330 Subject: [FFmpeg-devel] [PATCH v2 36/69] 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: RJI4rnCqaNqv 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 3dd04d6e82..06bb704dc2 100644 --- a/libavcodec/mpegvideo.h +++ b/libavcodec/mpegvideo.h @@ -115,7 +115,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 4f64fd28a7..7bce5c54ca 100644 --- a/libavcodec/mpegvideo_enc.c +++ b/libavcodec/mpegvideo_enc.c @@ -827,7 +827,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); @@ -954,7 +953,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); @@ -1139,9 +1137,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) @@ -1152,9 +1150,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; } @@ -1251,11 +1249,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)); @@ -1287,7 +1285,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); @@ -1379,13 +1377,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); @@ -1395,7 +1393,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++; @@ -1404,38 +1402,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; } @@ -1444,7 +1442,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); @@ -1455,11 +1453,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"); @@ -1472,21 +1470,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 836fa2eaa3..d66462b19a 100644 --- a/libavcodec/mpegvideoenc.h +++ b/libavcodec/mpegvideoenc.h @@ -39,6 +39,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