From patchwork Mon Jan 24 14:46:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 33783 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:e71a:0:0:0:0:0 with SMTP id b26csp388269ioh; Mon, 24 Jan 2022 06:49:50 -0800 (PST) X-Google-Smtp-Source: ABdhPJw4xyEZJ48+OFkbMIsG9PaLzB2VAKqUTh6PqzHEVLeMUXVwXWadCAyhYOX11XtVj3N8H88q X-Received: by 2002:a17:906:305b:: with SMTP id d27mr12231693ejd.316.1643035790235; Mon, 24 Jan 2022 06:49:50 -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 qf21si9427212ejc.896.2022.01.24.06.49.49; Mon, 24 Jan 2022 06:49:50 -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=GppilWBJ; 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 A023468B084; Mon, 24 Jan 2022 16:48:08 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from EUR03-AM5-obe.outbound.protection.outlook.com (mail-oln040092070061.outbound.protection.outlook.com [40.92.70.61]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 2F40A68B148 for ; Mon, 24 Jan 2022 16:48:06 +0200 (EET) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=D253meOhRFznNlFrcoBbPJCqeM77bJV8YK0QuC5swDVi2AGt0RUpLpcmdUymGIVBcnt93KSAhZp0+JxargfPA7lceMdaFS/90aSaIJ8LVfa9wuwQc6OrOtB5gk3QzImRj6350TgZnIwXpO+XadOZsaNgLIdd7IJ0NiCAUAH0iznOOpnZPk8qIZIjvXSpRjw1bOP/UC8XH1I/E9z1EheVX9TKajgtTDtgMrWfOu+75tVwe/XxaEewX8Ie1qLqeMoe5lsQ873r9c6AmDEHVGl2z6H60Qhugx7py3WDtu3sFnI6dj/8xYpndEk81nyigGpT/TbsNYY8tuTIKTXpkW+Qmw== 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=U1OLQLyBJHjvgfboIVFxG982YVH4ux6o3Cfvbsyt4EE=; b=APnykR7e5hOHFn/CwlRtSQLoSJm+/yRuWYSmEFkbLhDqZgXy+RCY0gipF84uTE+nifTBuGxyVYse6eU1f37ylq/sXvT1qCJFa9XG7SJFS3OW3GxerP1/MzyD0E5gU2E2QHfdetnPpcPw2vJCIe0xo0V0C1bH160O/Y6WgrNvBEkcY/JtlS3i4NwAEEgALoxwxyyHIGrQy62yP6/Mz1LiMkPgFp/l8YIRK6Eli2nrpv80b/HXGKUtHEQMtjECFVIJ3SuoOLZswsa+QgZTlbGMhoCc7IfHr2tHabvbKhMn3YIoiBOkFnUi1ssPmEAJ9QQUNrHv3hB/SXv1zGNi0od+Nw== 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=U1OLQLyBJHjvgfboIVFxG982YVH4ux6o3Cfvbsyt4EE=; b=GppilWBJE2FQ4Gf/uk1qwF2agCGsWT/4MTlBxDCE/F3Ry/bIbSg128XpN3HyBVFEGQOQo2oHVum2M3lwNSmgYMXTey87MEmXhr5YaR1Du8/GpFC9nnyY9L7thYwn9M3oAVZOd5vYz7XvTWV5OpzQfAu33/4x8l2AjxDC8YpycGvl1K3FNYjwP4caATUeKFoptCtC2uwj7CMbouf7SqLQcPVSPm62cwJJrT9BJKaetfFTOX6Io2JmgYwDsZvXYhIUlmFa6xVkc5Cy//FSPtiRiFqa7Yf/uC7aaVRbiD7kWeki9eULnZZgdytNHoaiAvoE9AYeUrXNpc8e8phMRm6Asw== Received: from AM7PR03MB6660.eurprd03.prod.outlook.com (2603:10a6:20b:1c1::22) by AM6PR03MB5236.eurprd03.prod.outlook.com (2603:10a6:20b:c4::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4909.17; Mon, 24 Jan 2022 14:48:05 +0000 Received: from AM7PR03MB6660.eurprd03.prod.outlook.com ([fe80::ac56:2ff4:d304:ab22]) by AM7PR03MB6660.eurprd03.prod.outlook.com ([fe80::ac56:2ff4:d304:ab22%4]) with mapi id 15.20.4909.017; Mon, 24 Jan 2022 14:48:05 +0000 From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Mon, 24 Jan 2022 15:46:04 +0100 Message-ID: X-Mailer: git-send-email 2.32.0 In-Reply-To: References: X-TMN: [nHwgjMLKWEgCvY7EiSnxW1eyXZ/yXuJE] X-ClientProxiedBy: AM5PR0201CA0016.eurprd02.prod.outlook.com (2603:10a6:203:3d::26) To AM7PR03MB6660.eurprd03.prod.outlook.com (2603:10a6:20b:1c1::22) X-Microsoft-Original-Message-ID: <20220124144616.559446-19-andreas.rheinhardt@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: cb036a32-44fe-4fe7-8d2b-08d9df4887a6 X-MS-TrafficTypeDiagnostic: AM6PR03MB5236:EE_ X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: /slHKo0mmp4/huTM5NHTMsUgx3S0Liaa7oJ2UWp0g3EN91JWB4YsZs6R57RQ3If4EFdH9XH2qdVVJQFV7h4KiRdbprt56ocLSuWHK1N0669VW6hXKH19YDH+ZCS5w+hi+/rMTmGH6dBphic7ro+8ow5UnlKCWzuTF4+JhNR8BX77yOD3B6hO0Xtj+TxAwLW+FSfx20vOSK7DQBQHTfUAAH02Dvd9TUlT7SJiXAQvjyD+0i4Zh1ZoOQeRUM8eNF1Q8Do5QNLVohRyVwvH8O7Bx8+SQ6LA/MZr8a0q2bxfPqmnMQHI4X9D5iN1eoYApi3OGZZvacHLRni6OG8e0wk1QlshEnIdFTGN9CuuKWLBbjI5CCb4tzJQhnviukC1lTkT0svR7iNfi1FHZGjuIDfUZZvA/WRJl474dzaNaX0zWY6n5IkzNWdaytw+Ek77xL6U5PwPffVSzt4uqIYkjfKF8CJ5JUjJlg19mTS0dMpR5mE9AIKz4V9C8R5rELOkgMUQuLpaIk2KnT4on5iHbtn7Dlf6EEiICOCNB6vZg7E++vy37cjeW1lip5HdEa84kOcE03w30aczSiuwz76V3OYRSA== X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: XN2lqrgGJpvuSgCQ9V8qLdUUnekvpeK4jHl/bS4PQJAI3bR2PyFI3Hif48ot5WKtQxKBg3P9RXxFo3hAsDegEONDxQ701t3kCSKmNqVSFBjkMtw1eULAOj4CLt/JFhlb3hsKCQex2NMeaUpncugLI+OtVpOd8RUzI5MHusFs20Y14y4xhHlGAohFJR5ouqz2ufa6fTEQcsYczPvUgIXR/fZICkiJDXg8CYy5uDbqXLdfzygaSFgs7Jq74Tblm8RLds5z9tCo1iJMZr2Uh1RwU5vn8pYsOv5XtjXD1NEkwvtoUbaXpLE3nNlEt5llU8KMww+/eySUGDTYTYpD1jKEJFGVKj2b/mdKkpPs080kOk9K05PC+9gAiREiw69/iSFMoZro1TzaDok2lZdBKf0+DB0C4dh3RMiWtgPli2NKYM08DrWQsC4L7SO+l54rfiY5bCzDPNJpIv5xJ8YHsD6XO06nNZb1JVaCiC6vBNWe5y6L3f6SyEq/0tjEVEJnrb912EqB6ZGc2TZ/3qHT3J5YrvXTEQCAn6GPLKgl1Nyfu/STPZ75PGbWcwuojmxcfvOuGA4cR7xw485rtVyBkDR+m1k9ybFDOi+n6anLHBDhFEFbebk8xvth02k/hF8n+xUJG6yEz2zcd5K1/ik5R8YSZVG+tU7S5T9Bea60dzGaT8YXEKPYFuWuPL/AS2p+fvTxKYTn6HA+Kz6NfkKkoHAluz5tsMKW5a0hySbIyaF2YLKXDh9jCLf2jB9Qegpe2TG63hwWzz/Apuq6XAfqKQYAiYfSB+rApYxTpnegnmYiT6AQUZp8YNqrKuwird/ba3vxULZQvC1pSKn3dKWcOfiI5vQ3UablNWFacAvZa73VLVgQ7PEN4eIO83DJ5aX+zIP4JZK9SuJ8cOii4mncKaW8p9h1KY8WfVsH7uXJZGc17C6fjUqDMz+uIhiD9M/REDfv9oWhtOhITlioHU8I99gPvw== X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: cb036a32-44fe-4fe7-8d2b-08d9df4887a6 X-MS-Exchange-CrossTenant-AuthSource: AM7PR03MB6660.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Jan 2022 14:48:05.8145 (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: AM6PR03MB5236 Subject: [FFmpeg-devel] [PATCH v2 19/31] lavf/mpegenc: switch to new FIFO API 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: Anton Khirnov Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: ftTA6txOUP68 From: Anton Khirnov --- libavformat/mpegenc.c | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/libavformat/mpegenc.c b/libavformat/mpegenc.c index b1d8bf9c38..64248695bd 100644 --- a/libavformat/mpegenc.c +++ b/libavformat/mpegenc.c @@ -45,7 +45,7 @@ typedef struct PacketDesc { } PacketDesc; typedef struct StreamInfo { - AVFifoBuffer *fifo; + AVFifo *fifo; uint8_t id; int max_buffer_size; /* in bytes */ int buffer_index; @@ -459,7 +459,7 @@ static av_cold int mpeg_mux_init(AVFormatContext *ctx) av_get_media_type_string(st->codecpar->codec_type), i); return AVERROR(EINVAL); } - stream->fifo = av_fifo_alloc(16); + stream->fifo = av_fifo_alloc2(16, 1, 0); if (!stream->fifo) return AVERROR(ENOMEM); } @@ -626,6 +626,12 @@ static int get_nb_frames(AVFormatContext *ctx, StreamInfo *stream, int len) return nb_frames; } +static int fifo_avio_wrapper(void *opaque, void *buf, size_t *nb_elems) +{ + avio_write(opaque, buf, *nb_elems); + return 0; +} + /* flush the packet on stream stream_index */ static int flush_packet(AVFormatContext *ctx, int stream_index, int64_t pts, int64_t dts, int64_t scr, int trailer_size) @@ -741,6 +747,7 @@ static int flush_packet(AVFormatContext *ctx, int stream_index, packet_size -= pad_packet_bytes + zero_trail_bytes; if (packet_size > 0) { + size_t fifo_data; /* packet header size */ packet_size -= 6; @@ -776,7 +783,7 @@ static int flush_packet(AVFormatContext *ctx, int stream_index, startcode = 0x100 + id; } - stuffing_size = payload_size - av_fifo_size(stream->fifo); + stuffing_size = payload_size - av_fifo_can_read(stream->fifo); // first byte does not fit -> reset pts/dts + stuffing if (payload_size <= trailer_size && pts != AV_NOPTS_VALUE) { @@ -814,7 +821,7 @@ static int flush_packet(AVFormatContext *ctx, int stream_index, stuffing_size = 0; if (startcode == PRIVATE_STREAM_1 && id >= 0xa0) { - if (payload_size < av_fifo_size(stream->fifo)) + if (payload_size < av_fifo_can_read(stream->fifo)) stuffing_size += payload_size % stream->lpcm_align; } @@ -907,11 +914,10 @@ static int flush_packet(AVFormatContext *ctx, int stream_index, } /* output data */ - av_assert0(payload_size - stuffing_size <= av_fifo_size(stream->fifo)); - av_fifo_generic_read(stream->fifo, ctx->pb, - payload_size - stuffing_size, - (void (*)(void*, void*, int))avio_write); - stream->bytes_to_iframe -= payload_size - stuffing_size; + fifo_data = payload_size - stuffing_size; + av_assert0(fifo_data <= av_fifo_can_read(stream->fifo)); + av_fifo_read_to_cb(stream->fifo, fifo_avio_wrapper, ctx->pb, &fifo_data); + stream->bytes_to_iframe -= fifo_data; } else { payload_size = stuffing_size = 0; @@ -1005,7 +1011,7 @@ retry: for (i = 0; i < ctx->nb_streams; i++) { AVStream *st = ctx->streams[i]; StreamInfo *stream = st->priv_data; - const int avail_data = av_fifo_size(stream->fifo); + const size_t avail_data = av_fifo_can_read(stream->fifo); const int space = stream->max_buffer_size - stream->buffer_index; int rel_space = 1024LL * space / stream->max_buffer_size; PacketDesc *next_pkt = stream->premux_packet; @@ -1075,7 +1081,7 @@ retry: st = ctx->streams[best_i]; stream = st->priv_data; - av_assert0(av_fifo_size(stream->fifo) > 0); + av_assert0(av_fifo_can_read(stream->fifo) > 0); av_assert0(avail_space >= s->packet_size || ignore_constraints); @@ -1095,7 +1101,7 @@ retry: es_size = flush_packet(ctx, best_i, timestamp_packet->pts, timestamp_packet->dts, scr, trailer_size); } else { - av_assert0(av_fifo_size(stream->fifo) == trailer_size); + av_assert0(av_fifo_can_read(stream->fifo) == trailer_size); es_size = flush_packet(ctx, best_i, AV_NOPTS_VALUE, AV_NOPTS_VALUE, scr, trailer_size); } @@ -1199,7 +1205,7 @@ static int mpeg_mux_write_packet(AVFormatContext *ctx, AVPacket *pkt) pkt_desc->unwritten_size = pkt_desc->size = size; - ret = av_fifo_realloc2(stream->fifo, av_fifo_size(stream->fifo) + size); + ret = av_fifo_grow2(stream->fifo, size); if (ret < 0) return ret; @@ -1208,13 +1214,13 @@ static int mpeg_mux_write_packet(AVFormatContext *ctx, AVPacket *pkt) if (is_iframe && (s->packet_number == 0 || pts != AV_NOPTS_VALUE && (pts - stream->vobu_start_pts >= 36000))) { - stream->bytes_to_iframe = av_fifo_size(stream->fifo); + stream->bytes_to_iframe = av_fifo_can_read(stream->fifo); stream->align_iframe = 1; stream->vobu_start_pts = pts; } } - av_fifo_generic_write(stream->fifo, buf, size, NULL); + av_fifo_write(stream->fifo, buf, size); for (;;) { int ret = output_packet(ctx, 0); @@ -1244,7 +1250,7 @@ static int mpeg_mux_end(AVFormatContext *ctx) for (i = 0; i < ctx->nb_streams; i++) { stream = ctx->streams[i]->priv_data; - av_assert0(av_fifo_size(stream->fifo) == 0); + av_assert0(av_fifo_can_read(stream->fifo) == 0); } return 0; } @@ -1260,7 +1266,7 @@ static void mpeg_mux_deinit(AVFormatContext *ctx) av_free(pkt); pkt = tmp; } - av_fifo_freep(&stream->fifo); + av_fifo_freep2(&stream->fifo); } }