Browse code

lavf/movenc: add faststart option.

Clément Bœsch authored on 2012/09/20 17:40:10
Showing 3 changed files
... ...
@@ -65,6 +65,7 @@ version next:
65 65
 - sendcmd and asendcmd filters
66 66
 - WebVTT demuxer and decoder (simple tags supported)
67 67
 - RTP packetization of JPEG
68
+- faststart option in the MOV/MP4 muxer
68 69
 
69 70
 
70 71
 version 0.11:
... ...
@@ -52,6 +52,7 @@ static const AVOption options[] = {
52 52
     { "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" },
53 53
     { "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" },
54 54
     { "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" },
55
+    { "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" },
55 56
     FF_RTP_FLAG_OPTS(MOVMuxContext, rtp_flags),
56 57
     { "skip_iods", "Skip writing iods atom.", offsetof(MOVMuxContext, iods_skip), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
57 58
     { "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)
3401 3401
                       FF_MOV_FLAG_FRAG_CUSTOM))
3402 3402
         mov->flags |= FF_MOV_FLAG_FRAGMENT;
3403 3403
 
3404
+    /* faststart: moov at the beginning of the file, if supported */
3405
+    if (mov->flags & FF_MOV_FLAG_FASTSTART) {
3406
+        if (mov->flags & FF_MOV_FLAG_FRAGMENT)
3407
+            mov->flags &= ~FF_MOV_FLAG_FASTSTART;
3408
+        else
3409
+            mov->reserved_moov_size = -1;
3410
+    }
3411
+
3404 3412
     /* Non-seekable output is ok if using fragmentation. If ism_lookahead
3405 3413
      * is enabled, we don't support non-seekable output at all. */
3406 3414
     if (!s->pb->seekable &&
... ...
@@ -3573,7 +3582,8 @@ static int mov_write_header(AVFormatContext *s)
3573 3573
 
3574 3574
     if(mov->reserved_moov_size){
3575 3575
         mov->reserved_moov_pos= avio_tell(pb);
3576
-        avio_skip(pb, mov->reserved_moov_size);
3576
+        if (mov->reserved_moov_size > 0)
3577
+            avio_skip(pb, mov->reserved_moov_size);
3577 3578
     }
3578 3579
 
3579 3580
     if (!(mov->flags & FF_MOV_FLAG_FRAGMENT))
... ...
@@ -3633,6 +3643,100 @@ static int mov_write_header(AVFormatContext *s)
3633 3633
     return -1;
3634 3634
 }
3635 3635
 
3636
+/**
3637
+ * This function gets the moov size if moved to the top of the file: the chunk
3638
+ * offset table can switch between stco (32-bit entries) to co64 (64-bit
3639
+ * entries) when the moov is moved to the top, so the size of the moov would
3640
+ * change. It also updates the chunk offset tables.
3641
+ */
3642
+static int compute_moov_size(AVFormatContext *s)
3643
+{
3644
+    int i, moov_size, moov_size2;
3645
+    MOVMuxContext *mov = s->priv_data;
3646
+
3647
+    moov_size = get_moov_size(s);
3648
+    if (moov_size < 0)
3649
+        return moov_size;
3650
+
3651
+    for (i = 0; i < mov->nb_streams; i++)
3652
+        mov->tracks[i].data_offset += moov_size;
3653
+
3654
+    moov_size2 = get_moov_size(s);
3655
+    if (moov_size2 < 0)
3656
+        return moov_size2;
3657
+
3658
+    /* if the size changed, we just switched from stco to co64 and needs to
3659
+     * update the offsets */
3660
+    if (moov_size2 != moov_size)
3661
+        for (i = 0; i < mov->nb_streams; i++)
3662
+            mov->tracks[i].data_offset += moov_size2 - moov_size;
3663
+
3664
+    return moov_size2;
3665
+}
3666
+
3667
+static int shift_data(AVFormatContext *s)
3668
+{
3669
+    int ret = 0, moov_size;
3670
+    MOVMuxContext *mov = s->priv_data;
3671
+    int64_t pos, pos_end = avio_tell(s->pb);
3672
+    uint8_t *buf, *read_buf[2];
3673
+    int read_buf_id = 0;
3674
+    int read_size[2];
3675
+    AVIOContext *read_pb;
3676
+
3677
+    moov_size = compute_moov_size(s);
3678
+    if (moov_size < 0)
3679
+        return moov_size;
3680
+
3681
+    buf = av_malloc(moov_size * 2);
3682
+    if (!buf)
3683
+        return AVERROR(ENOMEM);
3684
+    read_buf[0] = buf;
3685
+    read_buf[1] = buf + moov_size;
3686
+
3687
+    /* Shift the data: the AVIO context of the output can only be used for
3688
+     * writing, so we re-open the same output, but for reading. It also avoids
3689
+     * a read/seek/write/seek back and forth. */
3690
+    avio_flush(s->pb);
3691
+    ret = avio_open(&read_pb, s->filename, AVIO_FLAG_READ);
3692
+    if (ret < 0) {
3693
+        av_log(s, AV_LOG_ERROR, "Unable to re-open %s output file for "
3694
+               "the second pass (faststart)\n", s->filename);
3695
+        goto end;
3696
+    }
3697
+
3698
+    /* mark the end of the shift to up to the last data we wrote, and get ready
3699
+     * for writing */
3700
+    pos_end = avio_tell(s->pb);
3701
+    avio_seek(s->pb, mov->reserved_moov_pos + moov_size, SEEK_SET);
3702
+
3703
+    /* start reading at where the new moov will be placed */
3704
+    avio_seek(read_pb, mov->reserved_moov_pos, SEEK_SET);
3705
+    pos = avio_tell(read_pb);
3706
+
3707
+#define READ_BLOCK do {                                                             \
3708
+    read_size[read_buf_id] = avio_read(read_pb, read_buf[read_buf_id], moov_size);  \
3709
+    read_buf_id ^= 1;                                                               \
3710
+} while (0)
3711
+
3712
+    /* shift data by chunk of at most moov_size */
3713
+    READ_BLOCK;
3714
+    do {
3715
+        int n;
3716
+        READ_BLOCK;
3717
+        n = read_size[read_buf_id];
3718
+        if (n <= 0)
3719
+            break;
3720
+        avio_write(s->pb, read_buf[read_buf_id], n);
3721
+        pos += n;
3722
+    } while (pos < pos_end);
3723
+    avio_close(read_pb);
3724
+
3725
+end:
3726
+    av_free(buf);
3727
+    return ret;
3728
+}
3729
+
3636 3730
 static int mov_write_trailer(AVFormatContext *s)
3637 3731
 {
3638 3732
     MOVMuxContext *mov = s->priv_data;
... ...
@@ -3670,11 +3774,19 @@ static int mov_write_trailer(AVFormatContext *s)
3670 3670
             ffio_wfourcc(pb, "mdat");
3671 3671
             avio_wb64(pb, mov->mdat_size + 16);
3672 3672
         }
3673
-        avio_seek(pb, mov->reserved_moov_size ? mov->reserved_moov_pos : moov_pos, SEEK_SET);
3674
-
3675
-        mov_write_moov_tag(pb, mov, s);
3676
-        if(mov->reserved_moov_size){
3677
-            int64_t size=  mov->reserved_moov_size - (avio_tell(pb) - mov->reserved_moov_pos);
3673
+        avio_seek(pb, mov->reserved_moov_size > 0 ? mov->reserved_moov_pos : moov_pos, SEEK_SET);
3674
+
3675
+        if (mov->reserved_moov_size == -1) {
3676
+            av_log(s, AV_LOG_INFO, "Starting second pass: moving header on top of the file\n");
3677
+            res = shift_data(s);
3678
+            if (res == 0) {
3679
+                avio_seek(s->pb, mov->reserved_moov_pos, SEEK_SET);
3680
+                mov_write_moov_tag(pb, mov, s);
3681
+            }
3682
+        } else if (mov->reserved_moov_size > 0) {
3683
+            int64_t size;
3684
+            mov_write_moov_tag(pb, mov, s);
3685
+            size = mov->reserved_moov_size - (avio_tell(pb) - mov->reserved_moov_pos);
3678 3686
             if(size < 8){
3679 3687
                 av_log(s, AV_LOG_ERROR, "reserved_moov_size is too small, needed %"PRId64" additional\n", 8-size);
3680 3688
                 return -1;
... ...
@@ -3684,6 +3796,8 @@ static int mov_write_trailer(AVFormatContext *s)
3684 3684
             for(i=0; i<size; i++)
3685 3685
                 avio_w8(pb, 0);
3686 3686
             avio_seek(pb, moov_pos, SEEK_SET);
3687
+        } else {
3688
+            mov_write_moov_tag(pb, mov, s);
3687 3689
         }
3688 3690
     } else {
3689 3691
         mov_flush_fragment(s);
... ...
@@ -152,7 +152,7 @@ typedef struct MOVMuxContext {
152 152
 
153 153
     int flags;
154 154
     int rtp_flags;
155
-    int reserved_moov_size;
155
+    int reserved_moov_size; ///< 0 for disabled, -1 for automatic, size otherwise
156 156
     int64_t reserved_moov_pos;
157 157
 
158 158
     int iods_skip;
... ...
@@ -174,6 +174,7 @@ typedef struct MOVMuxContext {
174 174
 #define FF_MOV_FLAG_SEPARATE_MOOF 16
175 175
 #define FF_MOV_FLAG_FRAG_CUSTOM 32
176 176
 #define FF_MOV_FLAG_ISML 64
177
+#define FF_MOV_FLAG_FASTSTART 128
177 178
 
178 179
 int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt);
179 180