From patchwork Mon Dec 27 00:26:09 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marton Balint X-Patchwork-Id: 32921 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp9884634iog; Sun, 26 Dec 2021 16:26:41 -0800 (PST) X-Google-Smtp-Source: ABdhPJzIc9wlWfTWw5kcQg/uqNDHmTM6fkRAVNsvU359ilVG2ylmshl62KQqJ7mlKMN3Nv8yP1Dz X-Received: by 2002:a17:907:6eac:: with SMTP id sh44mr12145937ejc.507.1640564801558; Sun, 26 Dec 2021 16:26:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1640564801; cv=none; d=google.com; s=arc-20160816; b=aNCjbjTkNhrWWYm43bx02fwpLSn0FFe0hBsYQ9I3myzXyHuvWhOiONY+EntiQhE2o1 rS8kFqrQC22X+QNb21DmcciZ+3HddwOVcSxkyJzG/D/9OmlOvP/7MH05s6cNtl5c1V6b k+Imysx+4rQeJRIuxCIhC0V8Cvf/i6IM9iysZVGCajR4eNOwar5yuzLVYPdFcUxjYXew nqzmUk8s8KL0jhRX8KJFZ46HolJ32fGutnmUjIvj0X4UpdM6fF954sJ+6QkiSyorbVGv SOehTr/kdIRUiGGdkaSuXTF7u9CSjk3F++tg8zh3wXlFb8dbse+P0NVsxNWPSsRfnEO1 uOQw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:message-id:date:to:from :delivered-to; bh=9OED8EhOThmM5HmAl2YGL/7/Z/VpsdIu24BWxz+/vwE=; b=MYK4IjxQYBrh8x13/NJ9C5oSZAdodvvkigPg1fO0c+AZr5vsunEYnf557iZHJXWTNu sDXfbFxLKCEnV5YiB4pC9RzrzBTCADURmAQSVWNXczI0k4p3ngR9Ek3h5tCcONEQygtP bih1QBTJS+nOG7++soKGntcKeieDvfs4iL54r3H7CpoZRaFuIAq1u5n3pYtCA/Lg5BGt ay4hW0OGQ5sHJeqrMUGjx3Bt+EkccejWOiMzJzbuq5Fsl1SuNKPbJf9B1szgsRwoJ04F IpJsJ5bmuWtnHRIcC91waPB0AkpjYFhI9yuc+Crw8jIPTagtgJd/jlDQFF/6BXHQCGDj ENBw== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id hw19si5978741ejc.958.2021.12.26.16.26.33; Sun, 26 Dec 2021 16:26:41 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id D4F8668B009; Mon, 27 Dec 2021 02:26:28 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from iq.passwd.hu (iq.passwd.hu [217.27.212.140]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 4F62968A8E7 for ; Mon, 27 Dec 2021 02:26:22 +0200 (EET) Received: from localhost (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 49C72E6634; Mon, 27 Dec 2021 01:26:21 +0100 (CET) X-Virus-Scanned: amavisd-new at passwd.hu Received: from iq.passwd.hu ([127.0.0.1]) by localhost (iq.passwd.hu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id JgX5Bsyq-IQ2; Mon, 27 Dec 2021 01:26:19 +0100 (CET) Received: from bluegene.passwd.hu (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 4DB86E6631; Mon, 27 Dec 2021 01:26:19 +0100 (CET) From: Marton Balint To: ffmpeg-devel@ffmpeg.org Date: Mon, 27 Dec 2021 01:26:09 +0100 Message-Id: <20211227002613.25069-1-cus@passwd.hu> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/5] avformat/aviobuf: set AVIOContext->error on bprint buffer ENOMEM 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: Marton Balint Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: 7Vs+M3bD0DJF This makes sure the error condition is kept in AVIOContext even if the user does not check the return value of avio_read_to_bprint or ff_read_line_to_bprint. Signed-off-by: Marton Balint --- libavformat/aviobuf.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/libavformat/aviobuf.c b/libavformat/aviobuf.c index 29d4bd7510..6f8a822ee3 100644 --- a/libavformat/aviobuf.c +++ b/libavformat/aviobuf.c @@ -875,8 +875,10 @@ static int64_t read_string_to_bprint_overwrite(AVIOContext *s, AVBPrint *bp, if (ret < 0) return ret; - if (!av_bprint_is_complete(bp)) + if (!av_bprint_is_complete(bp)) { + s->error = AVERROR(ENOMEM); return AVERROR(ENOMEM); + } return bp->len; } @@ -1351,8 +1353,10 @@ int avio_read_to_bprint(AVIOContext *h, AVBPrint *pb, size_t max_size) if (ret <= 0) return ret; av_bprint_append_data(pb, buf, ret); - if (!av_bprint_is_complete(pb)) + if (!av_bprint_is_complete(pb)) { + h->error = AVERROR(ENOMEM); return AVERROR(ENOMEM); + } max_size -= ret; } return 0; From patchwork Mon Dec 27 00:26:10 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marton Balint X-Patchwork-Id: 32922 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp9884693iog; Sun, 26 Dec 2021 16:26:50 -0800 (PST) X-Google-Smtp-Source: ABdhPJxvk7pZp8ttODVniXrwG2fRmUvpu6C4G7dKEjazwmSWmo6PV7+HDc764LsR2YGiYLyvt2Uy X-Received: by 2002:a17:906:5fca:: with SMTP id k10mr13027385ejv.722.1640564810175; Sun, 26 Dec 2021 16:26:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1640564810; cv=none; d=google.com; s=arc-20160816; b=CPJTz+2vq9yuguOEDMNKtfpyHLL80/D7wka/ttl3O+hbJAa9MiYBBJTE7SaZS/lkxM JjeLCWc823E7X+3MqwtWphadiLecXK2RXfuv7+vaIZH21BglIBYxeDabd0XGxM4Bc7XF ULr1mSt+6Mf9YtFMAXEYTac5wQ5zgSahq+yy5J3m+cahG1e1ppeKp7n9VMPn14zQwmoE zggzgXegSLzGcK7YfNSOd2yEBlNCvPTZ2GT9bq4pvYMdWIOD0eEiY4uiM5l7gAy5AXMy zy7TM0zqHNFQZuSl22/XQnJ9Erwvb2xYZLMHNDAP0WIll2z8UBtnT5B5It41dDKgohUo V2dg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:references:in-reply-to :message-id:date:to:from:delivered-to; bh=8ZTpjXbj9F24EAVAofG3uBaXpiZsVlVrV+uF2xTkyHc=; b=lqem1tJWLbUv59HjSrHsZIWdnEaFIXkIcsLGWg+wLSp9fCJKMD+DZRhjsoSH54WfJr qRLt//Vc6eOCZ/9RfE2eyEgaupA2v5+eJ/wDDPR8Qhq9BPtFShv9MFCBPNN8D1Kb2jic AEXn+lHmdQkwjpYKaTsC+FelDWfjZmZLBhfZ4DrsV4sPwzYWTGxi+ai08dqpW653LeQo THn5M4A7WPVHMTpS3snUgKPPm7K5LWne4dEc+YQNBfO9hT5uLW5dmC3/MPYM+El0dScZ HJ/V5xdC3jEh7W0o3mCmbq+jrwMCYmLgL2HJOy2dokFky/zIUMrhPpOVmnjChm4PEtWe GlUQ== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id ga28si6486126ejc.20.2021.12.26.16.26.43; Sun, 26 Dec 2021 16:26: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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 1187C68B0B1; Mon, 27 Dec 2021 02:26:31 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from iq.passwd.hu (iq.passwd.hu [217.27.212.140]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id D7C7E68B098 for ; Mon, 27 Dec 2021 02:26:24 +0200 (EET) Received: from localhost (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 6C05FE663D; Mon, 27 Dec 2021 01:26:24 +0100 (CET) X-Virus-Scanned: amavisd-new at passwd.hu Received: from iq.passwd.hu ([127.0.0.1]) by localhost (iq.passwd.hu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id gsoglkqwBeQY; Mon, 27 Dec 2021 01:26:23 +0100 (CET) Received: from bluegene.passwd.hu (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 02695E6631; Mon, 27 Dec 2021 01:26:22 +0100 (CET) From: Marton Balint To: ffmpeg-devel@ffmpeg.org Date: Mon, 27 Dec 2021 01:26:10 +0100 Message-Id: <20211227002613.25069-2-cus@passwd.hu> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20211227002613.25069-1-cus@passwd.hu> References: <20211227002613.25069-1-cus@passwd.hu> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/5] avformat/movenc: factorize data shifting 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: Marton Balint Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: GqHYjDHI3WR1 And move data shift function from movenc to utils. Signed-off-by: Marton Balint --- libavformat/internal.h | 7 ++++++ libavformat/movenc.c | 55 ++--------------------------------------- libavformat/utils.c | 56 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 53 deletions(-) diff --git a/libavformat/internal.h b/libavformat/internal.h index 2ba795d669..63235ce5cf 100644 --- a/libavformat/internal.h +++ b/libavformat/internal.h @@ -1019,4 +1019,11 @@ void ff_format_set_url(AVFormatContext *s, char *url); void avpriv_register_devices(const AVOutputFormat * const o[], const AVInputFormat * const i[]); +/** + * Make shift_size amount of space at read_start by shifting data in the output + * at read_start until the current IO position. The underlying IO context must + * be seekable. + */ +int ff_format_shift_data(AVFormatContext *s, int64_t read_start, int shift_size); + #endif /* AVFORMAT_INTERNAL_H */ diff --git a/libavformat/movenc.c b/libavformat/movenc.c index 0f912dd012..40ad4f8642 100644 --- a/libavformat/movenc.c +++ b/libavformat/movenc.c @@ -7150,13 +7150,8 @@ static int compute_sidx_size(AVFormatContext *s) static int shift_data(AVFormatContext *s) { - int ret = 0, moov_size; + int moov_size; MOVMuxContext *mov = s->priv_data; - int64_t pos, pos_end; - uint8_t *buf, *read_buf[2]; - int read_buf_id = 0; - int read_size[2]; - AVIOContext *read_pb; if (mov->flags & FF_MOV_FLAG_FRAGMENT) moov_size = compute_sidx_size(s); @@ -7165,53 +7160,7 @@ static int shift_data(AVFormatContext *s) if (moov_size < 0) return moov_size; - buf = av_malloc(moov_size * 2); - if (!buf) - return AVERROR(ENOMEM); - read_buf[0] = buf; - read_buf[1] = buf + moov_size; - - /* Shift the data: the AVIO context of the output can only be used for - * writing, so we re-open the same output, but for reading. It also avoids - * a read/seek/write/seek back and forth. */ - avio_flush(s->pb); - ret = s->io_open(s, &read_pb, s->url, AVIO_FLAG_READ, NULL); - if (ret < 0) { - av_log(s, AV_LOG_ERROR, "Unable to re-open %s output file for " - "the second pass (faststart)\n", s->url); - goto end; - } - - /* mark the end of the shift to up to the last data we wrote, and get ready - * for writing */ - pos_end = avio_tell(s->pb); - avio_seek(s->pb, mov->reserved_header_pos + moov_size, SEEK_SET); - - /* start reading at where the new moov will be placed */ - avio_seek(read_pb, mov->reserved_header_pos, SEEK_SET); - pos = avio_tell(read_pb); - -#define READ_BLOCK do { \ - read_size[read_buf_id] = avio_read(read_pb, read_buf[read_buf_id], moov_size); \ - read_buf_id ^= 1; \ -} while (0) - - /* shift data by chunk of at most moov_size */ - READ_BLOCK; - do { - int n; - READ_BLOCK; - n = read_size[read_buf_id]; - if (n <= 0) - break; - avio_write(s->pb, read_buf[read_buf_id], n); - pos += n; - } while (pos < pos_end); - ff_format_io_close(s, &read_pb); - -end: - av_free(buf); - return ret; + return ff_format_shift_data(s, mov->reserved_header_pos, moov_size); } static int mov_write_trailer(AVFormatContext *s) diff --git a/libavformat/utils.c b/libavformat/utils.c index 332ba534d2..a78797ef57 100644 --- a/libavformat/utils.c +++ b/libavformat/utils.c @@ -2035,3 +2035,59 @@ const char *av_disposition_to_string(int disposition) return NULL; } + +int ff_format_shift_data(AVFormatContext *s, int64_t read_start, int shift_size) +{ + int ret; + int64_t pos, pos_end; + uint8_t *buf, *read_buf[2]; + int read_buf_id = 0; + int read_size[2]; + AVIOContext *read_pb; + + buf = av_malloc_array(shift_size, 2); + if (!buf) + return AVERROR(ENOMEM); + read_buf[0] = buf; + read_buf[1] = buf + shift_size; + + /* Shift the data: the AVIO context of the output can only be used for + * writing, so we re-open the same output, but for reading. It also avoids + * a read/seek/write/seek back and forth. */ + avio_flush(s->pb); + ret = s->io_open(s, &read_pb, s->url, AVIO_FLAG_READ, NULL); + if (ret < 0) { + av_log(s, AV_LOG_ERROR, "Unable to re-open %s output file for shifting data\n", s->url); + goto end; + } + + /* mark the end of the shift to up to the last data we wrote, and get ready + * for writing */ + pos_end = avio_tell(s->pb); + avio_seek(s->pb, read_start + shift_size, SEEK_SET); + + avio_seek(read_pb, read_start, SEEK_SET); + pos = avio_tell(read_pb); + +#define READ_BLOCK do { \ + read_size[read_buf_id] = avio_read(read_pb, read_buf[read_buf_id], shift_size); \ + read_buf_id ^= 1; \ +} while (0) + + /* shift data by chunk of at most shift_size */ + READ_BLOCK; + do { + int n; + READ_BLOCK; + n = read_size[read_buf_id]; + if (n <= 0) + break; + avio_write(s->pb, read_buf[read_buf_id], n); + pos += n; + } while (pos < pos_end); + ff_format_io_close(s, &read_pb); + +end: + av_free(buf); + return ret; +} From patchwork Mon Dec 27 00:26:11 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marton Balint X-Patchwork-Id: 32923 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp9884700iog; Sun, 26 Dec 2021 16:26:52 -0800 (PST) X-Google-Smtp-Source: ABdhPJypt+G7KvH0N4jE4HvEvfcFAysTpJ1fBVA0nJK1Qr72s8XWtxjGNbz0Vb1K6tZvLBnbZYE3 X-Received: by 2002:a17:907:9614:: with SMTP id gb20mr12546916ejc.746.1640564811913; Sun, 26 Dec 2021 16:26:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1640564811; cv=none; d=google.com; s=arc-20160816; b=LZQ76Kf4OkEp4U0OI/bQJ08ZipPCcf/OLVbPBJgrgEGOignRC0F3k2ynM5xTGBwcqf 9PxsV6S4MRzm6mDPvveqCtCkGt8yz8EUGkr99J7u7mObYhMXIfkyEcOIgFN99JviSVe7 lHv+gZfrzuE2F0FoqKcYQNkhUUGBH5glIPV67hjHeSbfPneAXYiysPUqukdALqfCvKsu KaHBc0lTdqqxHjqKpMMa+L7CIpWunGi0O7MiaVNBTWBpO8y+89lgZ6qRtY35D7RNUYaX YwGs0nPj9R3gPf2xChT99ynSt1Ll5DVWJAj6pbzxq8xGnN4Z68AbrgM4t2fjPU0yzt72 dNmA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:references:in-reply-to :message-id:date:to:from:delivered-to; bh=mIkhqYnEJRnZpMVrmq6Uyjzn2zCeY4VEuN0o72bbhqk=; b=L9NpRdUnZW6ETV5PKZ9SeDXVY8RhgIKwDHxJqHK7iGnbbuSLmCM78B90IQkbm6YVQX A3UN/iHDfaN7Icaws861rz4oG6YwZcdeNPCAr2a1m40Kb0LXEkvZ1oshxEhlPP22fQvZ stX+b2vT+nWWbQ16byZIGdryKAK1aeeY9Fqn9LDVlV2JQVEDowWGzTuMmTdOLSMKns34 1D7xgoBwAy0VWjGAE3kzcmwkXD/KNQ4ZH2Hl2H/Q7lLH+CXe+WaTU7IhYdk2Hzv0K1ol hFW0VsPJTfwKoPgfXKDUF7GFU7ovTEtkRmaoqeXjoVj8IQIUIaOosyCJkYaeyJLBHXfj u9zA== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id fs4si1538126ejc.95.2021.12.26.16.26.51; Sun, 26 Dec 2021 16:26:51 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 1626F68B0DE; Mon, 27 Dec 2021 02:26:33 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from iq.passwd.hu (iq.passwd.hu [217.27.212.140]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 81A4768B0BB for ; Mon, 27 Dec 2021 02:26:26 +0200 (EET) Received: from localhost (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 0F9B8E663E; Mon, 27 Dec 2021 01:26:26 +0100 (CET) X-Virus-Scanned: amavisd-new at passwd.hu Received: from iq.passwd.hu ([127.0.0.1]) by localhost (iq.passwd.hu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id NBmd4P_PZOTu; Mon, 27 Dec 2021 01:26:25 +0100 (CET) Received: from bluegene.passwd.hu (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id DD7A8E6631; Mon, 27 Dec 2021 01:26:24 +0100 (CET) From: Marton Balint To: ffmpeg-devel@ffmpeg.org Date: Mon, 27 Dec 2021 01:26:11 +0100 Message-Id: <20211227002613.25069-3-cus@passwd.hu> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20211227002613.25069-1-cus@passwd.hu> References: <20211227002613.25069-1-cus@passwd.hu> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 3/5] avformat/flvenc: use ff_format_shift_data for data shifting 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: Marton Balint Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: O0viy+1BwNKB add_keyframe_index seems to generate a corrupted index even before this change. Signed-off-by: Marton Balint --- libavformat/flvenc.c | 59 +++++--------------------------------------- 1 file changed, 6 insertions(+), 53 deletions(-) diff --git a/libavformat/flvenc.c b/libavformat/flvenc.c index 5130d429ad..31d1d93c23 100644 --- a/libavformat/flvenc.c +++ b/libavformat/flvenc.c @@ -576,15 +576,9 @@ static int flv_append_keyframe_info(AVFormatContext *s, FLVContext *flv, double static int shift_data(AVFormatContext *s) { - int ret = 0; - int n = 0; + int ret; int64_t metadata_size = 0; FLVContext *flv = s->priv_data; - int64_t pos, pos_end = avio_tell(s->pb); /* Save the pre-shift size. */ - uint8_t *buf, *read_buf[2]; - int read_buf_id = 0; - int read_size[2]; - AVIOContext *read_pb; metadata_size = flv->filepositions_count * 9 * 2 + 10; /* filepositions and times value */ metadata_size += 2 + 13; /* filepositions String */ @@ -596,58 +590,17 @@ static int shift_data(AVFormatContext *s) if (metadata_size < 0) return metadata_size; - buf = av_malloc_array(metadata_size, 2); - if (!buf) { - return AVERROR(ENOMEM); - } - read_buf[0] = buf; - read_buf[1] = buf + metadata_size; + ret = ff_format_shift_data(s, flv->keyframes_info_offset, metadata_size); + if (ret < 0) + return ret; avio_seek(s->pb, flv->metadata_size_pos, SEEK_SET); avio_wb24(s->pb, flv->metadata_totalsize + metadata_size); - avio_seek(s->pb, flv->metadata_totalsize_pos, SEEK_SET); + avio_seek(s->pb, flv->metadata_totalsize_pos + metadata_size, SEEK_SET); avio_wb32(s->pb, flv->metadata_totalsize + 11 + metadata_size); - /* Shift the data: the AVIO context of the output can only be used for - * writing, so we re-open the same output, but for reading. It also avoids - * a read/seek/write/seek back and forth. */ - avio_flush(s->pb); - ret = s->io_open(s, &read_pb, s->url, AVIO_FLAG_READ, NULL); - if (ret < 0) { - av_log(s, AV_LOG_ERROR, "Unable to re-open %s output file for " - "the second pass (add_keyframe_index)\n", s->url); - goto end; - } - - /* Get ready for writing. */ - avio_seek(s->pb, flv->keyframes_info_offset + metadata_size, SEEK_SET); - - /* start reading at where the keyframe index information will be placed */ - avio_seek(read_pb, flv->keyframes_info_offset, SEEK_SET); - pos = avio_tell(read_pb); - -#define READ_BLOCK do { \ - read_size[read_buf_id] = avio_read(read_pb, read_buf[read_buf_id], metadata_size); \ - read_buf_id ^= 1; \ -} while (0) - - /* shift data by chunk of at most keyframe *filepositions* and *times* size */ - READ_BLOCK; - do { - READ_BLOCK; - n = read_size[read_buf_id]; - if (n < 0) - break; - avio_write(s->pb, read_buf[read_buf_id], n); - pos += n; - } while (pos <= pos_end); - - ff_format_io_close(s, &read_pb); - -end: - av_free(buf); - return ret; + return 0; } static int flv_init(struct AVFormatContext *s) From patchwork Mon Dec 27 00:26:12 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marton Balint X-Patchwork-Id: 32924 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp9884768iog; Sun, 26 Dec 2021 16:27:00 -0800 (PST) X-Google-Smtp-Source: ABdhPJy8zvBp58ZSzNyznpBdPtUk4lgjTVkd6CIxGUutgh4L841JPMXMhqNxWZFPyyPdCQ8kUjW4 X-Received: by 2002:a17:907:2d8b:: with SMTP id gt11mr12925359ejc.659.1640564820737; Sun, 26 Dec 2021 16:27:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1640564820; cv=none; d=google.com; s=arc-20160816; b=KZemYklVKy0gzHspKsHJajgbSjmU/PchT3IAExNZDZ7dw+WAjzgrSau333e00vhU0o O2Ua2U7zxwc1CaiUox9fDTedB/FNrdnfQG16homQye2+Hb+2lcCJa08khQFpA2zb0VUy 29iCQELDz+9AEOqHw+vZBUyOFIRm3hnw+wIZoVioSD7ZnrEcYGKm419zb+A2XOZCs+DA jAuAWEhWW9h6amxFaNwVfwWPK6BN01bzB/IqCBc4ePLTVS72rVg0TZFPAejEGDSlrOUf mQ7v9OWRRi1TQOnF636NRl1g5Hbjsa7KA1ity/E0DXcReicLpxSBj07mJ9O9z8Xs8hCz njqg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:references:in-reply-to :message-id:date:to:from:delivered-to; bh=y/4ASW7165lO4+gWueySfjr/j4lgXVOM+x/MS+TtU7I=; b=MP/WodZsu+1rBtnlgzZOHW8Zw3w9D2vwpCrR5WTElnPy6O+vjLAK2vbaW647Mvhwlp wEHUgAlhMCtjrsBoP2hg+w/cdIQ7B2YMNKUjND7XEsteLKXC9OxYKCXShVuD8pyKM1H4 3b5Yk+Th+9htukQ99d8NueN6qtFutk7yIGTTQJ6u2+XtF/5OLP3oX6Mkisjji4wEttXE Ny2PO65a6IXdJACsUlbO2q/rRj/cPU/Zx4SwCjoCoN/lEWjxjJwBLda/b4imRXdLuuX/ wBQOxwvMg0SAzJ4gIUkwzwsjESZDet6NUALZODK+NY5D2krZh0sUVY8Wiid6PcTPF406 WcLA== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id e1si6946475ejs.857.2021.12.26.16.27.00; Sun, 26 Dec 2021 16:27:00 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id DC07368B109; Mon, 27 Dec 2021 02:26:36 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from iq.passwd.hu (iq.passwd.hu [217.27.212.140]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 1D16168A8E7 for ; Mon, 27 Dec 2021 02:26:31 +0200 (EET) Received: from localhost (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id A4329E6634; Mon, 27 Dec 2021 01:26:30 +0100 (CET) X-Virus-Scanned: amavisd-new at passwd.hu Received: from iq.passwd.hu ([127.0.0.1]) by localhost (iq.passwd.hu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id E29vWV6eJsya; Mon, 27 Dec 2021 01:26:29 +0100 (CET) Received: from bluegene.passwd.hu (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 2078FE6631; Mon, 27 Dec 2021 01:26:29 +0100 (CET) From: Marton Balint To: ffmpeg-devel@ffmpeg.org Date: Mon, 27 Dec 2021 01:26:12 +0100 Message-Id: <20211227002613.25069-4-cus@passwd.hu> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20211227002613.25069-1-cus@passwd.hu> References: <20211227002613.25069-1-cus@passwd.hu> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 4/5] avformat/segafilmenc: use ff_format_shift_data for shifting 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: Marton Balint Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: rxeRSUKl2J3U Signed-off-by: Marton Balint --- libavformat/segafilmenc.c | 51 ++++----------------------------------- 1 file changed, 5 insertions(+), 46 deletions(-) diff --git a/libavformat/segafilmenc.c b/libavformat/segafilmenc.c index ff8cb66aca..737805faa6 100644 --- a/libavformat/segafilmenc.c +++ b/libavformat/segafilmenc.c @@ -170,54 +170,13 @@ static int film_init(AVFormatContext *format_context) static int write_header(AVFormatContext *format_context, uint8_t *header, unsigned header_size) { - int ret = 0; - int64_t pos, pos_end; - uint8_t *buf, *read_buf[2]; - int read_buf_id = 0; - int read_size[2]; - AVIOContext *read_pb; - - buf = av_malloc(header_size); - if (!buf) - return AVERROR(ENOMEM); - read_buf[0] = buf; - read_buf[1] = header; - read_size[1] = header_size; - - /* Write the header at the beginning of the file, shifting all content as necessary; - * based on the approach used by MOV faststart. */ - avio_flush(format_context->pb); - ret = format_context->io_open(format_context, &read_pb, format_context->url, AVIO_FLAG_READ, NULL); - if (ret < 0) { - av_log(format_context, AV_LOG_ERROR, "Unable to re-open %s output file to " - "write the header\n", format_context->url); - av_free(buf); + int ret = ff_format_shift_data(format_context, 0, header_size); + if (ret < 0) return ret; - } - /* Mark the end of the shift to up to the last data we are going to write, - * and get ready for writing */ - pos_end = avio_tell(format_context->pb) + header_size; - pos = avio_seek(format_context->pb, 0, SEEK_SET); - - /* start reading at where the new header will be placed */ - avio_seek(read_pb, 0, SEEK_SET); - - /* shift data by chunk of at most header_size */ - do { - int n; - read_size[read_buf_id] = avio_read(read_pb, read_buf[read_buf_id], - header_size); - read_buf_id ^= 1; - n = read_size[read_buf_id]; - if (n <= 0) - break; - avio_write(format_context->pb, read_buf[read_buf_id], n); - pos += n; - } while (pos < pos_end); - ff_format_io_close(format_context, &read_pb); - - av_free(buf); + avio_seek(format_context->pb, 0, SEEK_SET); + avio_write(format_context->pb, header, header_size); + return 0; } From patchwork Mon Dec 27 00:26:13 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marton Balint X-Patchwork-Id: 32925 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp9884840iog; Sun, 26 Dec 2021 16:27:10 -0800 (PST) X-Google-Smtp-Source: ABdhPJzRgO8Ad2rcOCbZ/okH2kiaRUtNo2H+PtwAvhELtbl/3D9Ts5D2/8XQfUTNnGVoSMuOkhA6 X-Received: by 2002:a17:907:e8c:: with SMTP id ho12mr12169272ejc.689.1640564830234; Sun, 26 Dec 2021 16:27:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1640564830; cv=none; d=google.com; s=arc-20160816; b=qgs8ai0Yv0WLn9wEdGIEffLis7zYLzI7fhtIAmDYfUrBKUyhVF2RU0DdtiAh3hE7tq nSAQ4yEgcVhxLXEKBHEpc+sOnKSkOEmN/WW10K6SKcYOy3ibySEBhTg4d8iO+VgKxtcZ oCkfIhPcKDSiuDAOwI40kb8JKyyGjde7XT/bV3bi7AJcjjD+3ts/4TXjr9wApE1My5Zq caDdo7TQgGhkSLYBiDbW+dM1UFY0Ye1m7/JfnyIx9093P+Qlw9e8YliY1vsmhknUGNGl pHvM7hKkdWtWDvTvaB4fKI+af5hgz619YKZWVz+d6Hd7oBTAcEWechY7smIoxy38KiWx SN9w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:references:in-reply-to :message-id:date:to:from:delivered-to; bh=gkcsZl9ECsStwl6B0vD/xaiDb51uk/cXpxIvgrrUl80=; b=GGchuTf3ucVeGAf9wM6FNWcpRvYq9MGx5VDmk314pSAGcievOqHxgFRV8OD4QchncF RuoafF78YuhKU0RqdqUUG2FzmR9Y5ev5f1Sg34ID8MC2eKNyDfdKkGN6Vh37DR7eIeOJ /O4Tbl7eD10WStNdeNp3uX81jmJRtwZDtcsFB044439zU8hJJPTQ1GXeONKuM9H544Ye MYtJjfFTidLGyNX1Hu4Cejd78wNTUCfemTdqgRY+SNuep18+jW0Mt3NkiPOICy6UpBz9 n7l2RtQAAsGpMqy6LCCiTLwxeLSvaUnGXPsPZWyXMRK/Py9AC9lsp1u4QacP3edymhbv 5XNQ== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id hc14si6406925ejc.365.2021.12.26.16.27.09; Sun, 26 Dec 2021 16:27:10 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id ED49E68B118; Mon, 27 Dec 2021 02:26:41 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from iq.passwd.hu (iq.passwd.hu [217.27.212.140]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 9009468A8E7 for ; Mon, 27 Dec 2021 02:26:35 +0200 (EET) Received: from localhost (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 1FE01E663D; Mon, 27 Dec 2021 01:26:35 +0100 (CET) X-Virus-Scanned: amavisd-new at passwd.hu Received: from iq.passwd.hu ([127.0.0.1]) by localhost (iq.passwd.hu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 1izIfslnbL9V; Mon, 27 Dec 2021 01:26:34 +0100 (CET) Received: from bluegene.passwd.hu (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id ED58DE6631; Mon, 27 Dec 2021 01:26:33 +0100 (CET) From: Marton Balint To: ffmpeg-devel@ffmpeg.org Date: Mon, 27 Dec 2021 01:26:13 +0100 Message-Id: <20211227002613.25069-5-cus@passwd.hu> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20211227002613.25069-1-cus@passwd.hu> References: <20211227002613.25069-1-cus@passwd.hu> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 5/5] avformat/utils: propagate return value of ff_format_io_close in ff_format_shift_data 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: Marton Balint Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: oH+UacuyLBJV Signed-off-by: Marton Balint --- libavformat/utils.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libavformat/utils.c b/libavformat/utils.c index a78797ef57..23e81bda9d 100644 --- a/libavformat/utils.c +++ b/libavformat/utils.c @@ -2085,7 +2085,7 @@ int ff_format_shift_data(AVFormatContext *s, int64_t read_start, int shift_size) avio_write(s->pb, read_buf[read_buf_id], n); pos += n; } while (pos < pos_end); - ff_format_io_close(s, &read_pb); + ret = ff_format_io_close(s, &read_pb); end: av_free(buf);