diff mbox series

[FFmpeg-devel,4/5] avformat/segafilmenc: use ff_format_shift_data for shifting

Message ID 20211227002613.25069-4-cus@passwd.hu
State Accepted
Commit 26d6c81dc6ed205005dd34f837f52569c1cb3903
Headers show
Series [FFmpeg-devel,1/5] avformat/aviobuf: set AVIOContext->error on bprint buffer ENOMEM | expand

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished
andriy/make_ppc success Make finished
andriy/make_fate_ppc fail Make fate failed

Commit Message

Marton Balint Dec. 27, 2021, 12:26 a.m. UTC
Signed-off-by: Marton Balint <cus@passwd.hu>
---
 libavformat/segafilmenc.c | 51 ++++-----------------------------------
 1 file changed, 5 insertions(+), 46 deletions(-)

Comments

Andreas Rheinhardt Dec. 31, 2021, 12:30 p.m. UTC | #1
Marton Balint:
> Signed-off-by: Marton Balint <cus@passwd.hu>
> ---
>  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);

This adds a new seek; this could be easily avoided if
ff_format_shift_data() would be changed to add a buffer parameter which
if != NULL would need to point to a buffer of size equal to shift_size
that is directly written at read_start.

> +    avio_write(format_context->pb, header, header_size);
> +
>      return 0;
>  }
>  
>
diff mbox series

Patch

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