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; +}