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:
11 * cc qt-faststart.c -o qt-faststart
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.
27 #define BE_16(x) ((((uint8_t*)(x))[0] << 8) | ((uint8_t*)(x))[1])
28 #define BE_32(x) ((((uint8_t*)(x))[0] << 24) | \
29 (((uint8_t*)(x))[1] << 16) | \
30 (((uint8_t*)(x))[2] << 8) | \
32 #define BE_64(x) (((uint64_t)(((uint8_t*)(x))[0]) << 56) | \
33 ((uint64_t)(((uint8_t*)(x))[1]) << 48) | \
34 ((uint64_t)(((uint8_t*)(x))[2]) << 40) | \
35 ((uint64_t)(((uint8_t*)(x))[3]) << 32) | \
36 ((uint64_t)(((uint8_t*)(x))[4]) << 24) | \
37 ((uint64_t)(((uint8_t*)(x))[5]) << 16) | \
38 ((uint64_t)(((uint8_t*)(x))[6]) << 8) | \
39 ((uint64_t)((uint8_t*)(x))[7]))
41 #define BE_FOURCC( ch0, ch1, ch2, ch3 ) \
42 ( (uint32_t)(unsigned char)(ch3) | \
43 ( (uint32_t)(unsigned char)(ch2) << 8 ) | \
44 ( (uint32_t)(unsigned char)(ch1) << 16 ) | \
45 ( (uint32_t)(unsigned char)(ch0) << 24 ) )
47 #define QT_ATOM BE_FOURCC
49 #define FREE_ATOM QT_ATOM('f', 'r', 'e', 'e')
50 #define JUNK_ATOM QT_ATOM('j', 'u', 'n', 'k')
51 #define MDAT_ATOM QT_ATOM('m', 'd', 'a', 't')
52 #define MOOV_ATOM QT_ATOM('m', 'o', 'o', 'v')
53 #define PNOT_ATOM QT_ATOM('p', 'n', 'o', 't')
54 #define SKIP_ATOM QT_ATOM('s', 'k', 'i', 'p')
55 #define WIDE_ATOM QT_ATOM('w', 'i', 'd', 'e')
56 #define PICT_ATOM QT_ATOM('P', 'I', 'C', 'T')
57 #define FTYP_ATOM QT_ATOM('f', 't', 'y', 'p')
59 #define CMOV_ATOM QT_ATOM('c', 'm', 'o', 'v')
60 #define STCO_ATOM QT_ATOM('s', 't', 'c', 'o')
61 #define CO64_ATOM QT_ATOM('c', 'o', '6', '4')
63 #define ATOM_PREAMBLE_SIZE 8
64 #define COPY_BUFFER_SIZE 1024
66 int main(int argc, char *argv[])
70 unsigned char atom_bytes[ATOM_PREAMBLE_SIZE];
71 uint32_t atom_type = 0;
72 uint64_t atom_size = 0;
74 unsigned char *moov_atom;
75 unsigned char *ftyp_atom = 0;
76 uint64_t moov_atom_size;
77 uint64_t ftyp_atom_size = 0;
79 uint32_t offset_count;
80 uint64_t current_offset;
81 uint64_t start_offset = 0;
82 unsigned char copy_buffer[COPY_BUFFER_SIZE];
86 printf ("Usage: qt-faststart <infile.mov> <outfile.mov>\n");
90 infile = fopen(argv[1], "rb");
96 /* traverse through the atoms in the file to make sure that 'moov' is
98 while (!feof(infile)) {
99 if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
102 atom_size = (uint32_t)BE_32(&atom_bytes[0]);
103 atom_type = BE_32(&atom_bytes[4]);
105 if ((atom_type != FREE_ATOM) &&
106 (atom_type != JUNK_ATOM) &&
107 (atom_type != MDAT_ATOM) &&
108 (atom_type != MOOV_ATOM) &&
109 (atom_type != PNOT_ATOM) &&
110 (atom_type != SKIP_ATOM) &&
111 (atom_type != WIDE_ATOM) &&
112 (atom_type != PICT_ATOM) &&
113 (atom_type != FTYP_ATOM)) {
114 printf ("encountered non-QT top-level atom (is this a Quicktime file?)\n");
119 if (atom_type == FTYP_ATOM) {
120 ftyp_atom_size = atom_size;
121 ftyp_atom = malloc(ftyp_atom_size);
123 printf ("could not allocate 0x%llX byte for ftyp atom\n",
128 fseeko(infile, -ATOM_PREAMBLE_SIZE, SEEK_CUR);
129 if (fread(ftyp_atom, atom_size, 1, infile) != 1) {
135 start_offset = ftello(infile);
139 /* 64-bit special case */
140 if (atom_size == 1) {
141 if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
144 atom_size = BE_64(&atom_bytes[0]);
145 fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE * 2, SEEK_CUR);
147 fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE, SEEK_CUR);
151 if (atom_type != MOOV_ATOM) {
152 printf ("last atom in file was not a moov atom\n");
157 /* moov atom was, in fact, the last atom in the chunk; load the whole
159 fseeko(infile, -atom_size, SEEK_END);
160 last_offset = ftello(infile);
161 moov_atom_size = atom_size;
162 moov_atom = malloc(moov_atom_size);
164 printf ("could not allocate 0x%llX byte for moov atom\n",
169 if (fread(moov_atom, atom_size, 1, infile) != 1) {
176 /* this utility does not support compressed atoms yet, so disqualify
177 * files with compressed QT atoms */
178 if (BE_32(&moov_atom[12]) == CMOV_ATOM) {
179 printf ("this utility does not support compressed moov atoms yet\n");
185 /* close; will be re-opened later */
188 /* crawl through the moov chunk in search of stco or co64 atoms */
189 for (i = 4; i < moov_atom_size - 4; i++) {
190 atom_type = BE_32(&moov_atom[i]);
191 if (atom_type == STCO_ATOM) {
192 printf (" patching stco atom...\n");
193 atom_size = BE_32(&moov_atom[i - 4]);
194 if (i + atom_size - 4 > moov_atom_size) {
195 printf (" bad atom size\n");
199 offset_count = BE_32(&moov_atom[i + 8]);
200 for (j = 0; j < offset_count; j++) {
201 current_offset = BE_32(&moov_atom[i + 12 + j * 4]);
202 current_offset += moov_atom_size;
203 moov_atom[i + 12 + j * 4 + 0] = (current_offset >> 24) & 0xFF;
204 moov_atom[i + 12 + j * 4 + 1] = (current_offset >> 16) & 0xFF;
205 moov_atom[i + 12 + j * 4 + 2] = (current_offset >> 8) & 0xFF;
206 moov_atom[i + 12 + j * 4 + 3] = (current_offset >> 0) & 0xFF;
209 } else if (atom_type == CO64_ATOM) {
210 printf (" patching co64 atom...\n");
211 atom_size = BE_32(&moov_atom[i - 4]);
212 if (i + atom_size - 4 > moov_atom_size) {
213 printf (" bad atom size\n");
217 offset_count = BE_32(&moov_atom[i + 8]);
218 for (j = 0; j < offset_count; j++) {
219 current_offset = BE_64(&moov_atom[i + 12 + j * 8]);
220 current_offset += moov_atom_size;
221 moov_atom[i + 12 + j * 8 + 0] = (current_offset >> 56) & 0xFF;
222 moov_atom[i + 12 + j * 8 + 1] = (current_offset >> 48) & 0xFF;
223 moov_atom[i + 12 + j * 8 + 2] = (current_offset >> 40) & 0xFF;
224 moov_atom[i + 12 + j * 8 + 3] = (current_offset >> 32) & 0xFF;
225 moov_atom[i + 12 + j * 8 + 4] = (current_offset >> 24) & 0xFF;
226 moov_atom[i + 12 + j * 8 + 5] = (current_offset >> 16) & 0xFF;
227 moov_atom[i + 12 + j * 8 + 6] = (current_offset >> 8) & 0xFF;
228 moov_atom[i + 12 + j * 8 + 7] = (current_offset >> 0) & 0xFF;
234 /* re-open the input file and open the output file */
235 infile = fopen(argv[1], "rb");
241 /* seek after ftyp atom if needed */
242 fseeko(infile, start_offset, SEEK_SET);
244 outfile = fopen(argv[2], "wb");
252 /* dump the same ftyp atom */
253 if (ftyp_atom_size > 0) {
254 printf (" writing ftyp atom...\n");
255 if (fwrite(ftyp_atom, ftyp_atom_size, 1, outfile) != 1) {
261 /* dump the new moov atom */
262 printf (" writing moov atom...\n");
263 if (fwrite(moov_atom, moov_atom_size, 1, outfile) != 1) {
268 /* copy the remainder of the infile, from offset 0 -> last_offset - 1 */
269 printf (" copying rest of file...\n");
270 while (last_offset) {
271 if (last_offset > COPY_BUFFER_SIZE)
272 bytes_to_copy = COPY_BUFFER_SIZE;
274 bytes_to_copy = last_offset;
276 if (fread(copy_buffer, bytes_to_copy, 1, infile) != 1) {
280 if (fwrite(copy_buffer, bytes_to_copy, 1, outfile) != 1) {
285 last_offset -= bytes_to_copy;