[FFmpeg-devel] [PATCH 3/3] lavf/movenc: add faststart option.

Clément Bœsch ubitux at gmail.com
Mon Sep 24 09:58:07 CEST 2012


On Sun, Sep 23, 2012 at 03:55:10AM +0200, Michael Niedermayer wrote:
> On Thu, Sep 20, 2012 at 10:47:20AM +0200, Clément Bœsch wrote:
> > From: Clément Bœsch <clement.boesch at smartjog.com>
> > 
> > ---
> >  libavformat/movenc.c |  125 +++++++++++++++++++++++++++++++++++++++++++++++---
> >  libavformat/movenc.h |    3 +-
> >  2 files changed, 121 insertions(+), 7 deletions(-)
> 
> this will need a fate test
> 

Added a commit on top of the patch set, see attached.

> 
> [...]
> > +static int shift_data(AVFormatContext *s)
> > +{
> > +    int ret, moov_size;
> > +    MOVMuxContext *mov = s->priv_data;
> > +    int64_t pos, pos_end = avio_tell(s->pb);
> > +    uint8_t *buf, *read_buf[2];
> > +    int read_buf_id = 0;
> > +    int read_size[2];
> > +    AVIOContext *read_pb;
> > +
> > +    moov_size = compute_moov_size(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 = avio_open(&read_pb, s->filename, AVIO_FLAG_READ);
> > +    if (ret < 0) {
> > +        av_log(s, AV_LOG_ERROR, "Unable to re-open %s output file for "
> > +               "the second pass (faststart)\n", s->filename);
> > +        return ret;
> > +    }
> 
> memleak (buf)
> 

Fixed.

> otherwise things look nice from a quick look
> 

I'll give 3 days before pushing unless you want that feature in 0.12.

-- 
Clément B.
-------------- next part --------------
From 605ddc90450724390ad9755e17836d7812e91c6a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Cl=C3=A9ment=20B=C5=93sch?= <clement.boesch at smartjog.com>
Date: Thu, 20 Sep 2012 10:40:10 +0200
Subject: [PATCH 3/4] lavf/movenc: add faststart option.

---
 libavformat/movenc.c |  126 +++++++++++++++++++++++++++++++++++++++++++++++---
 libavformat/movenc.h |    3 +-
 2 files changed, 122 insertions(+), 7 deletions(-)

diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index a514b59..4b0a547 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -52,6 +52,7 @@ static const AVOption options[] = {
     { "separate_moof", "Write separate moof/mdat atoms for each track", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_SEPARATE_MOOF}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
     { "frag_custom", "Flush fragments on caller requests", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_FRAG_CUSTOM}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
     { "isml", "Create a live smooth streaming feed (for pushing to a publishing point)", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_ISML}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
+    { "faststart", "Run a second pass to put the moov at the beginning of the file", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_FASTSTART}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
     FF_RTP_FLAG_OPTS(MOVMuxContext, rtp_flags),
     { "skip_iods", "Skip writing iods atom.", offsetof(MOVMuxContext, iods_skip), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
     { "iods_audio_profile", "iods audio profile atom.", offsetof(MOVMuxContext, iods_audio_profile), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 255, AV_OPT_FLAG_ENCODING_PARAM},
@@ -3401,6 +3402,14 @@ static int mov_write_header(AVFormatContext *s)
                       FF_MOV_FLAG_FRAG_CUSTOM))
         mov->flags |= FF_MOV_FLAG_FRAGMENT;
 
+    /* faststart: moov at the beginning of the file, if supported */
+    if (mov->flags & FF_MOV_FLAG_FASTSTART) {
+        if (mov->flags & FF_MOV_FLAG_FRAGMENT)
+            mov->flags &= ~FF_MOV_FLAG_FASTSTART;
+        else
+            mov->reserved_moov_size = -1;
+    }
+
     /* Non-seekable output is ok if using fragmentation. If ism_lookahead
      * is enabled, we don't support non-seekable output at all. */
     if (!s->pb->seekable &&
@@ -3573,7 +3582,8 @@ static int mov_write_header(AVFormatContext *s)
 
     if(mov->reserved_moov_size){
         mov->reserved_moov_pos= avio_tell(pb);
-        avio_skip(pb, mov->reserved_moov_size);
+        if (mov->reserved_moov_size > 0)
+            avio_skip(pb, mov->reserved_moov_size);
     }
 
     if (!(mov->flags & FF_MOV_FLAG_FRAGMENT))
@@ -3633,6 +3643,100 @@ static int mov_write_header(AVFormatContext *s)
     return -1;
 }
 
+/**
+ * This function gets the moov size if moved to the top of the file: the chunk
+ * offset table can switch between stco (32-bit entries) to co64 (64-bit
+ * entries) when the moov is moved to the top, so the size of the moov would
+ * change. It also updates the chunk offset tables.
+ */
+static int compute_moov_size(AVFormatContext *s)
+{
+    int i, moov_size, moov_size2;
+    MOVMuxContext *mov = s->priv_data;
+
+    moov_size = get_moov_size(s);
+    if (moov_size < 0)
+        return moov_size;
+
+    for (i = 0; i < mov->nb_streams; i++)
+        mov->tracks[i].data_offset += moov_size;
+
+    moov_size2 = get_moov_size(s);
+    if (moov_size2 < 0)
+        return moov_size2;
+
+    /* if the size changed, we just switched from stco to co64 and needs to
+     * update the offsets */
+    if (moov_size2 != moov_size)
+        for (i = 0; i < mov->nb_streams; i++)
+            mov->tracks[i].data_offset += moov_size2 - moov_size;
+
+    return moov_size2;
+}
+
+static int shift_data(AVFormatContext *s)
+{
+    int ret = 0, moov_size;
+    MOVMuxContext *mov = s->priv_data;
+    int64_t pos, pos_end = avio_tell(s->pb);
+    uint8_t *buf, *read_buf[2];
+    int read_buf_id = 0;
+    int read_size[2];
+    AVIOContext *read_pb;
+
+    moov_size = compute_moov_size(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 = avio_open(&read_pb, s->filename, AVIO_FLAG_READ);
+    if (ret < 0) {
+        av_log(s, AV_LOG_ERROR, "Unable to re-open %s output file for "
+               "the second pass (faststart)\n", s->filename);
+        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_moov_pos + moov_size, SEEK_SET);
+
+    /* start reading at where the new moov will be placed */
+    avio_seek(read_pb, mov->reserved_moov_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);
+    avio_close(read_pb);
+
+end:
+    av_free(buf);
+    return ret;
+}
+
 static int mov_write_trailer(AVFormatContext *s)
 {
     MOVMuxContext *mov = s->priv_data;
@@ -3670,11 +3774,19 @@ static int mov_write_trailer(AVFormatContext *s)
             ffio_wfourcc(pb, "mdat");
             avio_wb64(pb, mov->mdat_size + 16);
         }
-        avio_seek(pb, mov->reserved_moov_size ? mov->reserved_moov_pos : moov_pos, SEEK_SET);
-
-        mov_write_moov_tag(pb, mov, s);
-        if(mov->reserved_moov_size){
-            int64_t size=  mov->reserved_moov_size - (avio_tell(pb) - mov->reserved_moov_pos);
+        avio_seek(pb, mov->reserved_moov_size > 0 ? mov->reserved_moov_pos : moov_pos, SEEK_SET);
+
+        if (mov->reserved_moov_size == -1) {
+            av_log(s, AV_LOG_INFO, "Starting second pass: moving header on top of the file\n");
+            res = shift_data(s);
+            if (res == 0) {
+                avio_seek(s->pb, mov->reserved_moov_pos, SEEK_SET);
+                mov_write_moov_tag(pb, mov, s);
+            }
+        } else if (mov->reserved_moov_size > 0) {
+            int64_t size;
+            mov_write_moov_tag(pb, mov, s);
+            size = mov->reserved_moov_size - (avio_tell(pb) - mov->reserved_moov_pos);
             if(size < 8){
                 av_log(s, AV_LOG_ERROR, "reserved_moov_size is too small, needed %"PRId64" additional\n", 8-size);
                 return -1;
@@ -3684,6 +3796,8 @@ static int mov_write_trailer(AVFormatContext *s)
             for(i=0; i<size; i++)
                 avio_w8(pb, 0);
             avio_seek(pb, moov_pos, SEEK_SET);
+        } else {
+            mov_write_moov_tag(pb, mov, s);
         }
     } else {
         mov_flush_fragment(s);
diff --git a/libavformat/movenc.h b/libavformat/movenc.h
index b4b3f1c..da6f19b 100644
--- a/libavformat/movenc.h
+++ b/libavformat/movenc.h
@@ -152,7 +152,7 @@ typedef struct MOVMuxContext {
 
     int flags;
     int rtp_flags;
-    int reserved_moov_size;
+    int reserved_moov_size; ///< 0 for disabled, -1 for automatic, size otherwise
     int64_t reserved_moov_pos;
 
     int iods_skip;
@@ -174,6 +174,7 @@ typedef struct MOVMuxContext {
 #define FF_MOV_FLAG_SEPARATE_MOOF 16
 #define FF_MOV_FLAG_FRAG_CUSTOM 32
 #define FF_MOV_FLAG_ISML 64
+#define FF_MOV_FLAG_FASTSTART 128
 
 int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt);
 
-- 
1.7.10.4

-------------- next part --------------
From 85e182bb8d4a4faf0696e3ede4cbcc45ce36c6af Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Cl=C3=A9ment=20B=C5=93sch?= <clement.boesch at smartjog.com>
Date: Mon, 24 Sep 2012 09:36:09 +0200
Subject: [PATCH 4/4] fate: add faststart regression test.

Also factorize the common options for the different mov-based tests.
---
 tests/lavf-regression.sh |    5 +++--
 tests/ref/lavf/mov       |    6 +++---
 2 files changed, 6 insertions(+), 5 deletions(-)

diff --git a/tests/lavf-regression.sh b/tests/lavf-regression.sh
index 29f9ba1..4a348f5 100755
--- a/tests/lavf-regression.sh
+++ b/tests/lavf-regression.sh
@@ -107,8 +107,9 @@ do_lavf flv "" "-an"
 fi
 
 if [ -n "$do_mov" ] ; then
-do_lavf mov "" "-movflags +rtphint -acodec pcm_alaw -vcodec mpeg4"
-do_lavf_timecode mov "-acodec pcm_alaw -vcodec mpeg4"
+mov_common_opt="-acodec pcm_alaw -vcodec mpeg4"
+do_lavf mov "" "-movflags +rtphint $mov_common_opt"
+do_lavf_timecode mov "-movflags +faststart $mov_common_opt"
 fi
 
 if [ -n "$do_ismv" ] ; then
diff --git a/tests/ref/lavf/mov b/tests/ref/lavf/mov
index a49904e..79e1318 100644
--- a/tests/ref/lavf/mov
+++ b/tests/ref/lavf/mov
@@ -1,12 +1,12 @@
 2c58e6422013d9cd6169f272ffba7a33 *./tests/data/lavf/lavf.mov
 367373 ./tests/data/lavf/lavf.mov
 ./tests/data/lavf/lavf.mov CRC=0x2f6a9b26
-21b992f6a677f971dfd685cc055a2b0a *./tests/data/lavf/lavf.mov
+5e5db229636424e92d21810adb6a7ea2 *./tests/data/lavf/lavf.mov
 358463 ./tests/data/lavf/lavf.mov
 ./tests/data/lavf/lavf.mov CRC=0x2f6a9b26
-f607c5ac4f2668149c064d75a4ddd0dd *./tests/data/lavf/lavf.mov
+91a92fb183da44202ea6f22d163337aa *./tests/data/lavf/lavf.mov
 377609 ./tests/data/lavf/lavf.mov
 ./tests/data/lavf/lavf.mov CRC=0x4e671506
-305a68397e3cdb505704841fedcdc352 *./tests/data/lavf/lavf.mov
+928e6ffcfd5c73c1961fd38c1ccf3f78 *./tests/data/lavf/lavf.mov
 357845 ./tests/data/lavf/lavf.mov
 ./tests/data/lavf/lavf.mov CRC=0x2f6a9b26
-- 
1.7.10.4

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 490 bytes
Desc: not available
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20120924/0d15e3e2/attachment.asc>


More information about the ffmpeg-devel mailing list