0ea96604dd288a297c1a27b878d88dffcf6bacf3
[libav.git] / tools / qt-faststart.c
1 /*
2 * qt-faststart.c, v0.1
3 * by Mike Melanson (melanson@pcisys.net)
4 * This file is placed in the public domain. Use the program however you
5 * see fit.
6 *
7 * This utility rearranges a Quicktime file such that the moov atom
8 * is in front of the data, thus facilitating network streaming.
9 *
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>
18 *
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.
25 */
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <inttypes.h>
30
31 #ifdef __MINGW32__
32 #define fseeko(x,y,z) fseeko64(x,y,z)
33 #define ftello(x) ftello64(x)
34 #endif
35
36 #define BE_16(x) ((((uint8_t*)(x))[0] << 8) | ((uint8_t*)(x))[1])
37 #define BE_32(x) ((((uint8_t*)(x))[0] << 24) | \
38 (((uint8_t*)(x))[1] << 16) | \
39 (((uint8_t*)(x))[2] << 8) | \
40 ((uint8_t*)(x))[3])
41 #define BE_64(x) (((uint64_t)(((uint8_t*)(x))[0]) << 56) | \
42 ((uint64_t)(((uint8_t*)(x))[1]) << 48) | \
43 ((uint64_t)(((uint8_t*)(x))[2]) << 40) | \
44 ((uint64_t)(((uint8_t*)(x))[3]) << 32) | \
45 ((uint64_t)(((uint8_t*)(x))[4]) << 24) | \
46 ((uint64_t)(((uint8_t*)(x))[5]) << 16) | \
47 ((uint64_t)(((uint8_t*)(x))[6]) << 8) | \
48 ((uint64_t)((uint8_t*)(x))[7]))
49
50 #define BE_FOURCC( ch0, ch1, ch2, ch3 ) \
51 ( (uint32_t)(unsigned char)(ch3) | \
52 ( (uint32_t)(unsigned char)(ch2) << 8 ) | \
53 ( (uint32_t)(unsigned char)(ch1) << 16 ) | \
54 ( (uint32_t)(unsigned char)(ch0) << 24 ) )
55
56 #define QT_ATOM BE_FOURCC
57 /* top level atoms */
58 #define FREE_ATOM QT_ATOM('f', 'r', 'e', 'e')
59 #define JUNK_ATOM QT_ATOM('j', 'u', 'n', 'k')
60 #define MDAT_ATOM QT_ATOM('m', 'd', 'a', 't')
61 #define MOOV_ATOM QT_ATOM('m', 'o', 'o', 'v')
62 #define PNOT_ATOM QT_ATOM('p', 'n', 'o', 't')
63 #define SKIP_ATOM QT_ATOM('s', 'k', 'i', 'p')
64 #define WIDE_ATOM QT_ATOM('w', 'i', 'd', 'e')
65 #define PICT_ATOM QT_ATOM('P', 'I', 'C', 'T')
66 #define FTYP_ATOM QT_ATOM('f', 't', 'y', 'p')
67
68 #define CMOV_ATOM QT_ATOM('c', 'm', 'o', 'v')
69 #define STCO_ATOM QT_ATOM('s', 't', 'c', 'o')
70 #define CO64_ATOM QT_ATOM('c', 'o', '6', '4')
71
72 #define ATOM_PREAMBLE_SIZE 8
73 #define COPY_BUFFER_SIZE 1024
74
75 int main(int argc, char *argv[])
76 {
77 FILE *infile;
78 FILE *outfile;
79 unsigned char atom_bytes[ATOM_PREAMBLE_SIZE];
80 uint32_t atom_type = 0;
81 uint64_t atom_size = 0;
82 uint64_t last_offset;
83 unsigned char *moov_atom;
84 unsigned char *ftyp_atom = 0;
85 uint64_t moov_atom_size;
86 uint64_t ftyp_atom_size = 0;
87 uint64_t i, j;
88 uint32_t offset_count;
89 uint64_t current_offset;
90 uint64_t start_offset = 0;
91 unsigned char copy_buffer[COPY_BUFFER_SIZE];
92 int bytes_to_copy;
93
94 if (argc != 3) {
95 printf ("Usage: qt-faststart <infile.mov> <outfile.mov>\n");
96 return 0;
97 }
98
99 infile = fopen(argv[1], "rb");
100 if (!infile) {
101 perror(argv[1]);
102 return 1;
103 }
104
105 /* traverse through the atoms in the file to make sure that 'moov' is
106 * at the end */
107 while (!feof(infile)) {
108 if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
109 break;
110 }
111 atom_size = (uint32_t)BE_32(&atom_bytes[0]);
112 atom_type = BE_32(&atom_bytes[4]);
113
114 if ((atom_type != FREE_ATOM) &&
115 (atom_type != JUNK_ATOM) &&
116 (atom_type != MDAT_ATOM) &&
117 (atom_type != MOOV_ATOM) &&
118 (atom_type != PNOT_ATOM) &&
119 (atom_type != SKIP_ATOM) &&
120 (atom_type != WIDE_ATOM) &&
121 (atom_type != PICT_ATOM) &&
122 (atom_type != FTYP_ATOM)) {
123 printf ("encountered non-QT top-level atom (is this a Quicktime file?)\n");
124 break;
125 }
126
127 /* keep ftyp atom */
128 if (atom_type == FTYP_ATOM) {
129 ftyp_atom_size = atom_size;
130 ftyp_atom = malloc(ftyp_atom_size);
131 if (!ftyp_atom) {
132 printf ("could not allocate 0x%llX byte for ftyp atom\n",
133 atom_size);
134 fclose(infile);
135 return 1;
136 }
137 fseeko(infile, -ATOM_PREAMBLE_SIZE, SEEK_CUR);
138 if (fread(ftyp_atom, atom_size, 1, infile) != 1) {
139 perror(argv[1]);
140 free(ftyp_atom);
141 fclose(infile);
142 return 1;
143 }
144 start_offset = ftello(infile);
145 continue;
146 }
147
148 /* 64-bit special case */
149 if (atom_size == 1) {
150 if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
151 break;
152 }
153 atom_size = BE_64(&atom_bytes[0]);
154 fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE * 2, SEEK_CUR);
155 } else {
156 fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE, SEEK_CUR);
157 }
158 }
159
160 if (atom_type != MOOV_ATOM) {
161 printf ("last atom in file was not a moov atom\n");
162 fclose(infile);
163 return 0;
164 }
165
166 /* moov atom was, in fact, the last atom in the chunk; load the whole
167 * moov atom */
168 fseeko(infile, -atom_size, SEEK_END);
169 last_offset = ftello(infile);
170 moov_atom_size = atom_size;
171 moov_atom = malloc(moov_atom_size);
172 if (!moov_atom) {
173 printf ("could not allocate 0x%llX byte for moov atom\n",
174 atom_size);
175 fclose(infile);
176 return 1;
177 }
178 if (fread(moov_atom, atom_size, 1, infile) != 1) {
179 perror(argv[1]);
180 free(moov_atom);
181 fclose(infile);
182 return 1;
183 }
184
185 /* this utility does not support compressed atoms yet, so disqualify
186 * files with compressed QT atoms */
187 if (BE_32(&moov_atom[12]) == CMOV_ATOM) {
188 printf ("this utility does not support compressed moov atoms yet\n");
189 free(moov_atom);
190 fclose(infile);
191 return 1;
192 }
193
194 /* close; will be re-opened later */
195 fclose(infile);
196
197 /* crawl through the moov chunk in search of stco or co64 atoms */
198 for (i = 4; i < moov_atom_size - 4; i++) {
199 atom_type = BE_32(&moov_atom[i]);
200 if (atom_type == STCO_ATOM) {
201 printf (" patching stco atom...\n");
202 atom_size = BE_32(&moov_atom[i - 4]);
203 if (i + atom_size - 4 > moov_atom_size) {
204 printf (" bad atom size\n");
205 free(moov_atom);
206 return 1;
207 }
208 offset_count = BE_32(&moov_atom[i + 8]);
209 for (j = 0; j < offset_count; j++) {
210 current_offset = BE_32(&moov_atom[i + 12 + j * 4]);
211 current_offset += moov_atom_size;
212 moov_atom[i + 12 + j * 4 + 0] = (current_offset >> 24) & 0xFF;
213 moov_atom[i + 12 + j * 4 + 1] = (current_offset >> 16) & 0xFF;
214 moov_atom[i + 12 + j * 4 + 2] = (current_offset >> 8) & 0xFF;
215 moov_atom[i + 12 + j * 4 + 3] = (current_offset >> 0) & 0xFF;
216 }
217 i += atom_size - 4;
218 } else if (atom_type == CO64_ATOM) {
219 printf (" patching co64 atom...\n");
220 atom_size = BE_32(&moov_atom[i - 4]);
221 if (i + atom_size - 4 > moov_atom_size) {
222 printf (" bad atom size\n");
223 free(moov_atom);
224 return 1;
225 }
226 offset_count = BE_32(&moov_atom[i + 8]);
227 for (j = 0; j < offset_count; j++) {
228 current_offset = BE_64(&moov_atom[i + 12 + j * 8]);
229 current_offset += moov_atom_size;
230 moov_atom[i + 12 + j * 8 + 0] = (current_offset >> 56) & 0xFF;
231 moov_atom[i + 12 + j * 8 + 1] = (current_offset >> 48) & 0xFF;
232 moov_atom[i + 12 + j * 8 + 2] = (current_offset >> 40) & 0xFF;
233 moov_atom[i + 12 + j * 8 + 3] = (current_offset >> 32) & 0xFF;
234 moov_atom[i + 12 + j * 8 + 4] = (current_offset >> 24) & 0xFF;
235 moov_atom[i + 12 + j * 8 + 5] = (current_offset >> 16) & 0xFF;
236 moov_atom[i + 12 + j * 8 + 6] = (current_offset >> 8) & 0xFF;
237 moov_atom[i + 12 + j * 8 + 7] = (current_offset >> 0) & 0xFF;
238 }
239 i += atom_size - 4;
240 }
241 }
242
243 /* re-open the input file and open the output file */
244 infile = fopen(argv[1], "rb");
245 if (!infile) {
246 perror(argv[1]);
247 free(moov_atom);
248 return 1;
249 }
250
251 if (start_offset > 0) { /* seek after ftyp atom */
252 fseeko(infile, start_offset, SEEK_SET);
253 last_offset -= start_offset;
254 }
255
256 outfile = fopen(argv[2], "wb");
257 if (!outfile) {
258 perror(argv[2]);
259 fclose(outfile);
260 free(moov_atom);
261 return 1;
262 }
263
264 /* dump the same ftyp atom */
265 if (ftyp_atom_size > 0) {
266 printf (" writing ftyp atom...\n");
267 if (fwrite(ftyp_atom, ftyp_atom_size, 1, outfile) != 1) {
268 perror(argv[2]);
269 goto error_out;
270 }
271 }
272
273 /* dump the new moov atom */
274 printf (" writing moov atom...\n");
275 if (fwrite(moov_atom, moov_atom_size, 1, outfile) != 1) {
276 perror(argv[2]);
277 goto error_out;
278 }
279
280 /* copy the remainder of the infile, from offset 0 -> last_offset - 1 */
281 printf (" copying rest of file...\n");
282 while (last_offset) {
283 if (last_offset > COPY_BUFFER_SIZE)
284 bytes_to_copy = COPY_BUFFER_SIZE;
285 else
286 bytes_to_copy = last_offset;
287
288 if (fread(copy_buffer, bytes_to_copy, 1, infile) != 1) {
289 perror(argv[1]);
290 goto error_out;
291 }
292 if (fwrite(copy_buffer, bytes_to_copy, 1, outfile) != 1) {
293 perror(argv[2]);
294 goto error_out;
295 }
296
297 last_offset -= bytes_to_copy;
298 }
299
300 fclose(infile);
301 fclose(outfile);
302 free(moov_atom);
303 if (ftyp_atom_size > 0)
304 free(ftyp_atom);
305
306 return 0;
307
308 error_out:
309 fclose(infile);
310 fclose(outfile);
311 free(moov_atom);
312 if (ftyp_atom_size > 0)
313 free(ftyp_atom);
314 return 1;
315 }