3 * by Mike Melanson (melanson@pcisys.net)
4 * This file is placed in the public domain. Use the program however you
7 * This utility rearranges a Quicktime file such that the moov atom
8 * is in front of the data, thus facilitating network streaming.
10 * To compile this program, start from the base directory from which you
11 * are building FFmpeg and type:
12 * make tools/qt-faststart
13 * The qt-faststart program will be built in the tools/ directory. If you
14 * do not build the program in this manner, correct results are not
15 * guaranteed, particularly on 64-bit platforms.
16 * Invoke the program with:
17 * qt-faststart <infile.mov> <outfile.mov>
19 * Notes: Quicktime files can come in many configurations of top-level
20 * atoms. This utility stipulates that the very last atom in the file needs
21 * to be a moov atom. When given such a file, this utility will rearrange
22 * the top-level atoms by shifting the moov atom from the back of the file
23 * to the front, and patch the chunk offsets along the way. This utility
24 * presently only operates on uncompressed moov atoms.
35 #define fseeko(x, y, z) fseeko64(x, y, z)
37 #define ftello(x) ftello64(x)
40 #define fseeko(x, y, z) _fseeki64(x, y, z)
42 #define ftello(x) _ftelli64(x)
45 #define MIN(a,b) ((a) > (b) ? (b) : (a))
47 #define BE_32(x) (((uint32_t)(((uint8_t*)(x))[0]) << 24) | \
48 (((uint8_t*)(x))[1] << 16) | \
49 (((uint8_t*)(x))[2] << 8) | \
52 #define BE_64(x) (((uint64_t)(((uint8_t*)(x))[0]) << 56) | \
53 ((uint64_t)(((uint8_t*)(x))[1]) << 48) | \
54 ((uint64_t)(((uint8_t*)(x))[2]) << 40) | \
55 ((uint64_t)(((uint8_t*)(x))[3]) << 32) | \
56 ((uint64_t)(((uint8_t*)(x))[4]) << 24) | \
57 ((uint64_t)(((uint8_t*)(x))[5]) << 16) | \
58 ((uint64_t)(((uint8_t*)(x))[6]) << 8) | \
59 ((uint64_t)( (uint8_t*)(x))[7]))
61 #define AV_WB32(p, val) { \
62 ((uint8_t*)(p))[0] = ((val) >> 24) & 0xff; \
63 ((uint8_t*)(p))[1] = ((val) >> 16) & 0xff; \
64 ((uint8_t*)(p))[2] = ((val) >> 8) & 0xff; \
65 ((uint8_t*)(p))[3] = (val) & 0xff; \
68 #define AV_WB64(p, val) { \
69 AV_WB32(p, (val) >> 32) \
73 #define BE_FOURCC(ch0, ch1, ch2, ch3) \
74 ( (uint32_t)(unsigned char)(ch3) | \
75 ((uint32_t)(unsigned char)(ch2) << 8) | \
76 ((uint32_t)(unsigned char)(ch1) << 16) | \
77 ((uint32_t)(unsigned char)(ch0) << 24) )
79 #define QT_ATOM BE_FOURCC
81 #define FREE_ATOM QT_ATOM('f', 'r', 'e', 'e')
82 #define JUNK_ATOM QT_ATOM('j', 'u', 'n', 'k')
83 #define MDAT_ATOM QT_ATOM('m', 'd', 'a', 't')
84 #define MOOV_ATOM QT_ATOM('m', 'o', 'o', 'v')
85 #define PNOT_ATOM QT_ATOM('p', 'n', 'o', 't')
86 #define SKIP_ATOM QT_ATOM('s', 'k', 'i', 'p')
87 #define WIDE_ATOM QT_ATOM('w', 'i', 'd', 'e')
88 #define PICT_ATOM QT_ATOM('P', 'I', 'C', 'T')
89 #define FTYP_ATOM QT_ATOM('f', 't', 'y', 'p')
90 #define UUID_ATOM QT_ATOM('u', 'u', 'i', 'd')
92 #define CMOV_ATOM QT_ATOM('c', 'm', 'o', 'v')
93 #define TRAK_ATOM QT_ATOM('t', 'r', 'a', 'k')
94 #define MDIA_ATOM QT_ATOM('m', 'd', 'i', 'a')
95 #define MINF_ATOM QT_ATOM('m', 'i', 'n', 'f')
96 #define STBL_ATOM QT_ATOM('s', 't', 'b', 'l')
97 #define STCO_ATOM QT_ATOM('s', 't', 'c', 'o')
98 #define CO64_ATOM QT_ATOM('c', 'o', '6', '4')
100 #define ATOM_PREAMBLE_SIZE 8
101 #define COPY_BUFFER_SIZE 33554432
102 #define MAX_FTYP_ATOM_SIZE 1048576
106 uint32_t header_size
;
112 uint64_t moov_atom_size
;
113 uint64_t stco_offset_count
;
114 uint64_t stco_data_size
;
117 } update_chunk_offsets_context_t
;
121 uint64_t original_moov_size
;
122 uint64_t new_moov_size
;
123 } upgrade_stco_context_t
;
125 typedef int (*parse_atoms_callback_t
)(void *context
, atom_t
*atom
);
127 static int parse_atoms(
130 parse_atoms_callback_t callback
,
133 unsigned char *pos
= buf
;
134 unsigned char *end
= pos
+ size
;
138 while (end
- pos
>= ATOM_PREAMBLE_SIZE
) {
139 atom
.size
= BE_32(pos
);
140 atom
.type
= BE_32(pos
+ 4);
141 pos
+= ATOM_PREAMBLE_SIZE
;
142 atom
.header_size
= ATOM_PREAMBLE_SIZE
;
147 fprintf(stderr
, "not enough room for 64 bit atom size\n");
151 atom
.size
= BE_64(pos
);
153 atom
.header_size
= ATOM_PREAMBLE_SIZE
+ 8;
157 atom
.size
= ATOM_PREAMBLE_SIZE
+ end
- pos
;
161 if (atom
.size
< atom
.header_size
) {
162 fprintf(stderr
, "atom size %"PRIu64
" too small\n", atom
.size
);
166 atom
.size
-= atom
.header_size
;
168 if (atom
.size
> end
- pos
) {
169 fprintf(stderr
, "atom size %"PRIu64
" too big\n", atom
.size
);
174 ret
= callback(context
, &atom
);
185 static int update_stco_offsets(update_chunk_offsets_context_t
*context
, atom_t
*atom
)
187 uint32_t current_offset
;
188 uint32_t offset_count
;
192 printf(" patching stco atom...\n");
193 if (atom
->size
< 8) {
194 fprintf(stderr
, "stco atom size %"PRIu64
" too small\n", atom
->size
);
198 offset_count
= BE_32(atom
->data
+ 4);
199 if (offset_count
> (atom
->size
- 8) / 4) {
200 fprintf(stderr
, "stco offset count %"PRIu32
" too big\n", offset_count
);
204 context
->stco_offset_count
+= offset_count
;
205 context
->stco_data_size
+= atom
->size
- 8;
207 for (pos
= atom
->data
+ 8, end
= pos
+ offset_count
* 4;
210 current_offset
= BE_32(pos
);
211 if (current_offset
> UINT_MAX
- context
->moov_atom_size
) {
212 context
->stco_overflow
= 1;
214 current_offset
+= context
->moov_atom_size
;
215 AV_WB32(pos
, current_offset
);
221 static int update_co64_offsets(update_chunk_offsets_context_t
*context
, atom_t
*atom
)
223 uint64_t current_offset
;
224 uint32_t offset_count
;
228 printf(" patching co64 atom...\n");
229 if (atom
->size
< 8) {
230 fprintf(stderr
, "co64 atom size %"PRIu64
" too small\n", atom
->size
);
234 offset_count
= BE_32(atom
->data
+ 4);
235 if (offset_count
> (atom
->size
- 8) / 8) {
236 fprintf(stderr
, "co64 offset count %"PRIu32
" too big\n", offset_count
);
240 for (pos
= atom
->data
+ 8, end
= pos
+ offset_count
* 8;
243 current_offset
= BE_64(pos
);
244 current_offset
+= context
->moov_atom_size
;
245 AV_WB64(pos
, current_offset
);
251 static int update_chunk_offsets_callback(void *ctx
, atom_t
*atom
)
253 update_chunk_offsets_context_t
*context
= ctx
;
256 switch (atom
->type
) {
258 return update_stco_offsets(context
, atom
);
261 return update_co64_offsets(context
, atom
);
269 if (context
->depth
> 10) {
270 fprintf(stderr
, "atoms too deeply nested\n");
277 update_chunk_offsets_callback
,
286 static void set_atom_size(unsigned char *header
, uint32_t header_size
, uint64_t size
)
288 switch (header_size
) {
290 AV_WB32(header
, size
);
294 AV_WB64(header
+ 8, size
);
299 static void upgrade_stco_atom(upgrade_stco_context_t
*context
, atom_t
*atom
)
304 uint32_t offset_count
;
305 uint32_t original_offset
;
307 /* Note: not performing validations since they were performed on the first pass */
309 offset_count
= BE_32(atom
->data
+ 4);
311 /* write the header */
312 memcpy(context
->dest
, atom
->data
- atom
->header_size
, atom
->header_size
+ 8);
313 AV_WB32(context
->dest
+ 4, CO64_ATOM
);
314 set_atom_size(context
->dest
, atom
->header_size
, atom
->header_size
+ 8 + offset_count
* 8);
315 context
->dest
+= atom
->header_size
+ 8;
318 for (pos
= atom
->data
+ 8, end
= pos
+ offset_count
* 4;
321 original_offset
= BE_32(pos
) - context
->original_moov_size
;
322 new_offset
= (uint64_t)original_offset
+ context
->new_moov_size
;
323 AV_WB64(context
->dest
, new_offset
);
328 static int upgrade_stco_callback(void *ctx
, atom_t
*atom
)
330 upgrade_stco_context_t
*context
= ctx
;
331 unsigned char *start_pos
;
334 switch (atom
->type
) {
336 upgrade_stco_atom(context
, atom
);
344 /* write the atom header */
345 memcpy(context
->dest
, atom
->data
- atom
->header_size
, atom
->header_size
);
346 start_pos
= context
->dest
;
347 context
->dest
+= atom
->header_size
;
349 /* parse internal atoms*/
353 upgrade_stco_callback
,
358 /* update the atom size */
359 set_atom_size(start_pos
, atom
->header_size
, context
->dest
- start_pos
);
363 copy_size
= atom
->header_size
+ atom
->size
;
364 memcpy(context
->dest
, atom
->data
- atom
->header_size
, copy_size
);
365 context
->dest
+= copy_size
;
372 static int update_moov_atom(
373 unsigned char **moov_atom
,
374 uint64_t *moov_atom_size
)
376 update_chunk_offsets_context_t update_context
= { 0 };
377 upgrade_stco_context_t upgrade_context
;
378 unsigned char *new_moov_atom
;
380 update_context
.moov_atom_size
= *moov_atom_size
;
385 update_chunk_offsets_callback
,
386 &update_context
) < 0) {
390 if (!update_context
.stco_overflow
) {
394 printf(" upgrading stco atoms to co64...\n");
395 upgrade_context
.new_moov_size
= *moov_atom_size
+
396 update_context
.stco_offset_count
* 8 -
397 update_context
.stco_data_size
;
399 new_moov_atom
= malloc(upgrade_context
.new_moov_size
);
400 if (new_moov_atom
== NULL
) {
401 fprintf(stderr
, "could not allocate %"PRIu64
" bytes for updated moov atom\n",
402 upgrade_context
.new_moov_size
);
406 upgrade_context
.original_moov_size
= *moov_atom_size
;
407 upgrade_context
.dest
= new_moov_atom
;
412 upgrade_stco_callback
,
413 &upgrade_context
) < 0) {
419 *moov_atom
= new_moov_atom
;
420 *moov_atom_size
= upgrade_context
.new_moov_size
;
422 if (upgrade_context
.dest
!= *moov_atom
+ *moov_atom_size
) {
423 fprintf(stderr
, "unexpected - wrong number of moov bytes written\n");
430 int main(int argc
, char *argv
[])
433 FILE *outfile
= NULL
;
434 unsigned char atom_bytes
[ATOM_PREAMBLE_SIZE
];
435 uint32_t atom_type
= 0;
436 uint64_t atom_size
= 0;
437 uint64_t atom_offset
= 0;
439 unsigned char *moov_atom
= NULL
;
440 unsigned char *ftyp_atom
= NULL
;
441 uint64_t moov_atom_size
;
442 uint64_t ftyp_atom_size
= 0;
443 int64_t start_offset
= 0;
444 unsigned char *copy_buffer
= NULL
;
446 uint64_t free_size
= 0;
447 uint64_t moov_size
= 0;
450 printf("Usage: qt-faststart <infile.mov> <outfile.mov>\n"
451 "Note: alternatively you can use -movflags +faststart in ffmpeg\n");
455 if (!strcmp(argv
[1], argv
[2])) {
456 fprintf(stderr
, "input and output files need to be different\n");
460 infile
= fopen(argv
[1], "rb");
466 /* traverse through the atoms in the file to make sure that 'moov' is
468 while (!feof(infile
)) {
469 if (fread(atom_bytes
, ATOM_PREAMBLE_SIZE
, 1, infile
) != 1) {
472 atom_size
= BE_32(&atom_bytes
[0]);
473 atom_type
= BE_32(&atom_bytes
[4]);
476 if (atom_type
== FTYP_ATOM
) {
477 if (atom_size
> MAX_FTYP_ATOM_SIZE
) {
478 fprintf(stderr
, "ftyp atom size %"PRIu64
" too big\n",
482 ftyp_atom_size
= atom_size
;
484 ftyp_atom
= malloc(ftyp_atom_size
);
486 fprintf(stderr
, "could not allocate %"PRIu64
" bytes for ftyp atom\n",
490 if (fseeko(infile
, -ATOM_PREAMBLE_SIZE
, SEEK_CUR
) ||
491 fread(ftyp_atom
, atom_size
, 1, infile
) != 1 ||
492 (start_offset
= ftello(infile
)) < 0) {
498 /* 64-bit special case */
499 if (atom_size
== 1) {
500 if (fread(atom_bytes
, ATOM_PREAMBLE_SIZE
, 1, infile
) != 1) {
503 atom_size
= BE_64(&atom_bytes
[0]);
504 ret
= fseeko(infile
, atom_size
- ATOM_PREAMBLE_SIZE
* 2, SEEK_CUR
);
506 ret
= fseeko(infile
, atom_size
- ATOM_PREAMBLE_SIZE
, SEEK_CUR
);
513 printf("%c%c%c%c %10"PRIu64
" %"PRIu64
"\n",
514 (atom_type
>> 24) & 255,
515 (atom_type
>> 16) & 255,
516 (atom_type
>> 8) & 255,
517 (atom_type
>> 0) & 255,
520 if ((atom_type
!= FREE_ATOM
) &&
521 (atom_type
!= JUNK_ATOM
) &&
522 (atom_type
!= MDAT_ATOM
) &&
523 (atom_type
!= MOOV_ATOM
) &&
524 (atom_type
!= PNOT_ATOM
) &&
525 (atom_type
!= SKIP_ATOM
) &&
526 (atom_type
!= WIDE_ATOM
) &&
527 (atom_type
!= PICT_ATOM
) &&
528 (atom_type
!= UUID_ATOM
) &&
529 (atom_type
!= FTYP_ATOM
)) {
530 fprintf(stderr
, "encountered non-QT top-level atom (is this a QuickTime file?)\n");
533 atom_offset
+= atom_size
;
535 /* The atom header is 8 (or 16 bytes), if the atom size (which
536 * includes these 8 or 16 bytes) is less than that, we won't be
537 * able to continue scanning sensibly after this atom, so break. */
541 if (atom_type
== MOOV_ATOM
)
542 moov_size
= atom_size
;
544 if (moov_size
&& atom_type
== FREE_ATOM
) {
545 free_size
+= atom_size
;
546 atom_type
= MOOV_ATOM
;
547 atom_size
= moov_size
;
551 if (atom_type
!= MOOV_ATOM
) {
552 printf("last atom in file was not a moov atom\n");
558 if (atom_size
< 16) {
559 fprintf(stderr
, "bad moov atom size\n");
563 /* moov atom was, in fact, the last atom in the chunk; load the whole
565 if (fseeko(infile
, -(atom_size
+ free_size
), SEEK_END
)) {
569 last_offset
= ftello(infile
);
570 if (last_offset
< 0) {
574 moov_atom_size
= atom_size
;
575 moov_atom
= malloc(moov_atom_size
);
577 fprintf(stderr
, "could not allocate %"PRIu64
" bytes for moov atom\n", atom_size
);
580 if (fread(moov_atom
, atom_size
, 1, infile
) != 1) {
585 /* this utility does not support compressed atoms yet, so disqualify
586 * files with compressed QT atoms */
587 if (BE_32(&moov_atom
[12]) == CMOV_ATOM
) {
588 fprintf(stderr
, "this utility does not support compressed moov atoms yet\n");
592 /* close; will be re-opened later */
596 if (update_moov_atom(&moov_atom
, &moov_atom_size
) < 0) {
600 /* re-open the input file and open the output file */
601 infile
= fopen(argv
[1], "rb");
607 if (start_offset
> 0) { /* seek after ftyp atom */
608 if (fseeko(infile
, start_offset
, SEEK_SET
)) {
613 last_offset
-= start_offset
;
616 outfile
= fopen(argv
[2], "wb");
622 /* dump the same ftyp atom */
623 if (ftyp_atom_size
> 0) {
624 printf(" writing ftyp atom...\n");
625 if (fwrite(ftyp_atom
, ftyp_atom_size
, 1, outfile
) != 1) {
631 /* dump the new moov atom */
632 printf(" writing moov atom...\n");
633 if (fwrite(moov_atom
, moov_atom_size
, 1, outfile
) != 1) {
638 /* copy the remainder of the infile, from offset 0 -> last_offset - 1 */
639 bytes_to_copy
= MIN(COPY_BUFFER_SIZE
, last_offset
);
640 copy_buffer
= malloc(bytes_to_copy
);
642 fprintf(stderr
, "could not allocate %d bytes for copy_buffer\n", bytes_to_copy
);
645 printf(" copying rest of file...\n");
646 while (last_offset
) {
647 bytes_to_copy
= MIN(bytes_to_copy
, last_offset
);
649 if (fread(copy_buffer
, bytes_to_copy
, 1, infile
) != 1) {
653 if (fwrite(copy_buffer
, bytes_to_copy
, 1, outfile
) != 1) {
657 last_offset
-= bytes_to_copy
;