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 * Compile this program using:
12 * Invoke the program with:
13 * qt-faststart <infile.mov> <outfile.mov>
15 * Notes: Quicktime files can come in many configurations of top-level
16 * atoms. This utility stipulates that the very last atom in the file needs
17 * to be a moov atom. When given such a file, this utility will rearrange
18 * the top-level atoms by shifting the moov atom from the back of the file
19 * to the front, and patch the chunk offsets along the way. This utility
20 * presently only operates on uncompressed moov atoms.
28 #define fseeko(x,y,z) fseeko64(x,y,z)
29 #define ftello(x) ftello64(x)
32 #define BE_16(x) ((((uint8_t*)(x))[0] << 8) | ((uint8_t*)(x))[1])
33 #define BE_32(x) ((((uint8_t*)(x))[0] << 24) | \
34 (((uint8_t*)(x))[1] << 16) | \
35 (((uint8_t*)(x))[2] << 8) | \
37 #define BE_64(x) (((uint64_t)(((uint8_t*)(x))[0]) << 56) | \
38 ((uint64_t)(((uint8_t*)(x))[1]) << 48) | \
39 ((uint64_t)(((uint8_t*)(x))[2]) << 40) | \
40 ((uint64_t)(((uint8_t*)(x))[3]) << 32) | \
41 ((uint64_t)(((uint8_t*)(x))[4]) << 24) | \
42 ((uint64_t)(((uint8_t*)(x))[5]) << 16) | \
43 ((uint64_t)(((uint8_t*)(x))[6]) << 8) | \
44 ((uint64_t)((uint8_t*)(x))[7]))
46 #define BE_FOURCC( ch0, ch1, ch2, ch3 ) \
47 ( (uint32_t)(unsigned char)(ch3) | \
48 ( (uint32_t)(unsigned char)(ch2) << 8 ) | \
49 ( (uint32_t)(unsigned char)(ch1) << 16 ) | \
50 ( (uint32_t)(unsigned char)(ch0) << 24 ) )
52 #define QT_ATOM BE_FOURCC
54 #define FREE_ATOM QT_ATOM('f', 'r', 'e', 'e')
55 #define JUNK_ATOM QT_ATOM('j', 'u', 'n', 'k')
56 #define MDAT_ATOM QT_ATOM('m', 'd', 'a', 't')
57 #define MOOV_ATOM QT_ATOM('m', 'o', 'o', 'v')
58 #define PNOT_ATOM QT_ATOM('p', 'n', 'o', 't')
59 #define SKIP_ATOM QT_ATOM('s', 'k', 'i', 'p')
60 #define WIDE_ATOM QT_ATOM('w', 'i', 'd', 'e')
61 #define PICT_ATOM QT_ATOM('P', 'I', 'C', 'T')
62 #define FTYP_ATOM QT_ATOM('f', 't', 'y', 'p')
64 #define CMOV_ATOM QT_ATOM('c', 'm', 'o', 'v')
65 #define STCO_ATOM QT_ATOM('s', 't', 'c', 'o')
66 #define CO64_ATOM QT_ATOM('c', 'o', '6', '4')
68 #define ATOM_PREAMBLE_SIZE 8
69 #define COPY_BUFFER_SIZE 1024
71 int main(int argc
, char *argv
[])
75 unsigned char atom_bytes
[ATOM_PREAMBLE_SIZE
];
76 uint32_t atom_type
= 0;
77 uint64_t atom_size
= 0;
79 unsigned char *moov_atom
;
80 unsigned char *ftyp_atom
= 0;
81 uint64_t moov_atom_size
;
82 uint64_t ftyp_atom_size
= 0;
84 uint32_t offset_count
;
85 uint64_t current_offset
;
86 uint64_t start_offset
= 0;
87 unsigned char copy_buffer
[COPY_BUFFER_SIZE
];
91 printf ("Usage: qt-faststart <infile.mov> <outfile.mov>\n");
95 infile
= fopen(argv
[1], "rb");
101 /* traverse through the atoms in the file to make sure that 'moov' is
103 while (!feof(infile
)) {
104 if (fread(atom_bytes
, ATOM_PREAMBLE_SIZE
, 1, infile
) != 1) {
107 atom_size
= (uint32_t)BE_32(&atom_bytes
[0]);
108 atom_type
= BE_32(&atom_bytes
[4]);
110 if ((atom_type
!= FREE_ATOM
) &&
111 (atom_type
!= JUNK_ATOM
) &&
112 (atom_type
!= MDAT_ATOM
) &&
113 (atom_type
!= MOOV_ATOM
) &&
114 (atom_type
!= PNOT_ATOM
) &&
115 (atom_type
!= SKIP_ATOM
) &&
116 (atom_type
!= WIDE_ATOM
) &&
117 (atom_type
!= PICT_ATOM
) &&
118 (atom_type
!= FTYP_ATOM
)) {
119 printf ("encountered non-QT top-level atom (is this a Quicktime file?)\n");
124 if (atom_type
== FTYP_ATOM
) {
125 ftyp_atom_size
= atom_size
;
126 ftyp_atom
= malloc(ftyp_atom_size
);
128 printf ("could not allocate 0x%llX byte for ftyp atom\n",
133 fseeko(infile
, -ATOM_PREAMBLE_SIZE
, SEEK_CUR
);
134 if (fread(ftyp_atom
, atom_size
, 1, infile
) != 1) {
140 start_offset
= ftello(infile
);
144 /* 64-bit special case */
145 if (atom_size
== 1) {
146 if (fread(atom_bytes
, ATOM_PREAMBLE_SIZE
, 1, infile
) != 1) {
149 atom_size
= BE_64(&atom_bytes
[0]);
150 fseeko(infile
, atom_size
- ATOM_PREAMBLE_SIZE
* 2, SEEK_CUR
);
152 fseeko(infile
, atom_size
- ATOM_PREAMBLE_SIZE
, SEEK_CUR
);
156 if (atom_type
!= MOOV_ATOM
) {
157 printf ("last atom in file was not a moov atom\n");
162 /* moov atom was, in fact, the last atom in the chunk; load the whole
164 fseeko(infile
, -atom_size
, SEEK_END
);
165 last_offset
= ftello(infile
);
166 moov_atom_size
= atom_size
;
167 moov_atom
= malloc(moov_atom_size
);
169 printf ("could not allocate 0x%llX byte for moov atom\n",
174 if (fread(moov_atom
, atom_size
, 1, infile
) != 1) {
181 /* this utility does not support compressed atoms yet, so disqualify
182 * files with compressed QT atoms */
183 if (BE_32(&moov_atom
[12]) == CMOV_ATOM
) {
184 printf ("this utility does not support compressed moov atoms yet\n");
190 /* close; will be re-opened later */
193 /* crawl through the moov chunk in search of stco or co64 atoms */
194 for (i
= 4; i
< moov_atom_size
- 4; i
++) {
195 atom_type
= BE_32(&moov_atom
[i
]);
196 if (atom_type
== STCO_ATOM
) {
197 printf (" patching stco atom...\n");
198 atom_size
= BE_32(&moov_atom
[i
- 4]);
199 if (i
+ atom_size
- 4 > moov_atom_size
) {
200 printf (" bad atom size\n");
204 offset_count
= BE_32(&moov_atom
[i
+ 8]);
205 for (j
= 0; j
< offset_count
; j
++) {
206 current_offset
= BE_32(&moov_atom
[i
+ 12 + j
* 4]);
207 current_offset
+= moov_atom_size
;
208 moov_atom
[i
+ 12 + j
* 4 + 0] = (current_offset
>> 24) & 0xFF;
209 moov_atom
[i
+ 12 + j
* 4 + 1] = (current_offset
>> 16) & 0xFF;
210 moov_atom
[i
+ 12 + j
* 4 + 2] = (current_offset
>> 8) & 0xFF;
211 moov_atom
[i
+ 12 + j
* 4 + 3] = (current_offset
>> 0) & 0xFF;
214 } else if (atom_type
== CO64_ATOM
) {
215 printf (" patching co64 atom...\n");
216 atom_size
= BE_32(&moov_atom
[i
- 4]);
217 if (i
+ atom_size
- 4 > moov_atom_size
) {
218 printf (" bad atom size\n");
222 offset_count
= BE_32(&moov_atom
[i
+ 8]);
223 for (j
= 0; j
< offset_count
; j
++) {
224 current_offset
= BE_64(&moov_atom
[i
+ 12 + j
* 8]);
225 current_offset
+= moov_atom_size
;
226 moov_atom
[i
+ 12 + j
* 8 + 0] = (current_offset
>> 56) & 0xFF;
227 moov_atom
[i
+ 12 + j
* 8 + 1] = (current_offset
>> 48) & 0xFF;
228 moov_atom
[i
+ 12 + j
* 8 + 2] = (current_offset
>> 40) & 0xFF;
229 moov_atom
[i
+ 12 + j
* 8 + 3] = (current_offset
>> 32) & 0xFF;
230 moov_atom
[i
+ 12 + j
* 8 + 4] = (current_offset
>> 24) & 0xFF;
231 moov_atom
[i
+ 12 + j
* 8 + 5] = (current_offset
>> 16) & 0xFF;
232 moov_atom
[i
+ 12 + j
* 8 + 6] = (current_offset
>> 8) & 0xFF;
233 moov_atom
[i
+ 12 + j
* 8 + 7] = (current_offset
>> 0) & 0xFF;
239 /* re-open the input file and open the output file */
240 infile
= fopen(argv
[1], "rb");
247 if (start_offset
> 0) { /* seek after ftyp atom */
248 fseeko(infile
, start_offset
, SEEK_SET
);
249 last_offset
-= start_offset
;
252 outfile
= fopen(argv
[2], "wb");
260 /* dump the same ftyp atom */
261 if (ftyp_atom_size
> 0) {
262 printf (" writing ftyp atom...\n");
263 if (fwrite(ftyp_atom
, ftyp_atom_size
, 1, outfile
) != 1) {
269 /* dump the new moov atom */
270 printf (" writing moov atom...\n");
271 if (fwrite(moov_atom
, moov_atom_size
, 1, outfile
) != 1) {
276 /* copy the remainder of the infile, from offset 0 -> last_offset - 1 */
277 printf (" copying rest of file...\n");
278 while (last_offset
) {
279 if (last_offset
> COPY_BUFFER_SIZE
)
280 bytes_to_copy
= COPY_BUFFER_SIZE
;
282 bytes_to_copy
= last_offset
;
284 if (fread(copy_buffer
, bytes_to_copy
, 1, infile
) != 1) {
288 if (fwrite(copy_buffer
, bytes_to_copy
, 1, outfile
) != 1) {
293 last_offset
-= bytes_to_copy
;
299 if (ftyp_atom_size
> 0)
308 if (ftyp_atom_size
> 0)