summaryrefslogtreecommitdiff
path: root/libavformat/utils.c
diff options
context:
space:
mode:
authorMarton Balint <cus@passwd.hu>2021-12-11 16:27:28 +0100
committerMarton Balint <cus@passwd.hu>2022-01-03 22:54:12 +0100
commit76e062322c3527b76b9b180ff0caa52b7af4784b (patch)
tree36c39c89c70a33fa5435e02242baaa39bf7c17bd /libavformat/utils.c
parent4be85c9331ca7c3360dffbbd4ce5055e8c478d2e (diff)
downloadffmpeg-76e062322c3527b76b9b180ff0caa52b7af4784b.tar.gz
avformat/movenc: factorize data shifting
And move data shift function from movenc to utils. Signed-off-by: Marton Balint <cus@passwd.hu>
Diffstat (limited to 'libavformat/utils.c')
-rw-r--r--libavformat/utils.c56
1 files changed, 56 insertions, 0 deletions
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;
+}