You've already forked FFmpeg
mirror of
https://github.com/FFmpeg/FFmpeg.git
synced 2025-08-15 14:13:16 +02:00
avformat/movenc: factorize data shifting
And move data shift function from movenc to utils. Signed-off-by: Marton Balint <cus@passwd.hu>
This commit is contained in:
@@ -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[]);
|
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 */
|
#endif /* AVFORMAT_INTERNAL_H */
|
||||||
|
@@ -7150,13 +7150,8 @@ static int compute_sidx_size(AVFormatContext *s)
|
|||||||
|
|
||||||
static int shift_data(AVFormatContext *s)
|
static int shift_data(AVFormatContext *s)
|
||||||
{
|
{
|
||||||
int ret = 0, moov_size;
|
int moov_size;
|
||||||
MOVMuxContext *mov = s->priv_data;
|
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)
|
if (mov->flags & FF_MOV_FLAG_FRAGMENT)
|
||||||
moov_size = compute_sidx_size(s);
|
moov_size = compute_sidx_size(s);
|
||||||
@@ -7165,53 +7160,7 @@ static int shift_data(AVFormatContext *s)
|
|||||||
if (moov_size < 0)
|
if (moov_size < 0)
|
||||||
return moov_size;
|
return moov_size;
|
||||||
|
|
||||||
buf = av_malloc(moov_size * 2);
|
return ff_format_shift_data(s, mov->reserved_header_pos, moov_size);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int mov_write_trailer(AVFormatContext *s)
|
static int mov_write_trailer(AVFormatContext *s)
|
||||||
|
@@ -2035,3 +2035,59 @@ const char *av_disposition_to_string(int disposition)
|
|||||||
|
|
||||||
return NULL;
|
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;
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user