Cosmetics: reindent
[libav.git] / tools / qt-faststart.c
CommitLineData
38100e8c 1/*
31457d7a 2 * qt-faststart.c, v0.2
38100e8c
MM
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 *
3c16e03d
MM
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.
38100e8c
MM
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
a8fcaf40
SH
31#ifdef __MINGW32__
32#define fseeko(x,y,z) fseeko64(x,y,z)
33#define ftello(x) ftello64(x)
34#endif
35
38100e8c
MM
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')
31457d7a 67#define UUID_ATOM QT_ATOM('u', 'u', 'i', 'd')
38100e8c
MM
68
69#define CMOV_ATOM QT_ATOM('c', 'm', 'o', 'v')
70#define STCO_ATOM QT_ATOM('s', 't', 'c', 'o')
71#define CO64_ATOM QT_ATOM('c', 'o', '6', '4')
72
73#define ATOM_PREAMBLE_SIZE 8
74#define COPY_BUFFER_SIZE 1024
75
76int main(int argc, char *argv[])
77{
c937454d
MS
78 FILE *infile = NULL;
79 FILE *outfile = NULL;
38100e8c
MM
80 unsigned char atom_bytes[ATOM_PREAMBLE_SIZE];
81 uint32_t atom_type = 0;
c0339599 82 uint64_t atom_size = 0;
65b875d8 83 uint64_t atom_offset = 0;
38100e8c 84 uint64_t last_offset;
c937454d 85 unsigned char *moov_atom = NULL;
60a9cc58 86 unsigned char *ftyp_atom = 0;
38100e8c 87 uint64_t moov_atom_size;
60a9cc58 88 uint64_t ftyp_atom_size = 0;
38100e8c
MM
89 uint64_t i, j;
90 uint32_t offset_count;
91 uint64_t current_offset;
60a9cc58 92 uint64_t start_offset = 0;
38100e8c
MM
93 unsigned char copy_buffer[COPY_BUFFER_SIZE];
94 int bytes_to_copy;
95
96 if (argc != 3) {
97 printf ("Usage: qt-faststart <infile.mov> <outfile.mov>\n");
98 return 0;
99 }
100
101 infile = fopen(argv[1], "rb");
102 if (!infile) {
103 perror(argv[1]);
c937454d 104 goto error_out;
38100e8c
MM
105 }
106
107 /* traverse through the atoms in the file to make sure that 'moov' is
108 * at the end */
109 while (!feof(infile)) {
110 if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
111 break;
112 }
7db2245d 113 atom_size = (uint32_t)BE_32(&atom_bytes[0]);
38100e8c
MM
114 atom_type = BE_32(&atom_bytes[4]);
115
60a9cc58
BC
116 /* keep ftyp atom */
117 if (atom_type == FTYP_ATOM) {
118 ftyp_atom_size = atom_size;
6ad533b7 119 free(ftyp_atom);
60a9cc58
BC
120 ftyp_atom = malloc(ftyp_atom_size);
121 if (!ftyp_atom) {
06a497af 122 printf ("could not allocate %"PRIu64" byte for ftyp atom\n",
60a9cc58 123 atom_size);
c937454d 124 goto error_out;
60a9cc58
BC
125 }
126 fseeko(infile, -ATOM_PREAMBLE_SIZE, SEEK_CUR);
127 if (fread(ftyp_atom, atom_size, 1, infile) != 1) {
128 perror(argv[1]);
c937454d 129 goto error_out;
60a9cc58
BC
130 }
131 start_offset = ftello(infile);
65b875d8 132 } else {
60a9cc58 133
38100e8c
MM
134 /* 64-bit special case */
135 if (atom_size == 1) {
136 if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
137 break;
138 }
139 atom_size = BE_64(&atom_bytes[0]);
e2daf554 140 fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE * 2, SEEK_CUR);
38100e8c 141 } else {
e2daf554 142 fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE, SEEK_CUR);
38100e8c
MM
143 }
144 }
65b875d8
FB
145 printf("%c%c%c%c %10"PRIu64" %"PRIu64"\n",
146 (atom_type >> 24) & 255,
147 (atom_type >> 16) & 255,
148 (atom_type >> 8) & 255,
149 (atom_type >> 0) & 255,
150 atom_offset,
151 atom_size);
152 if ((atom_type != FREE_ATOM) &&
153 (atom_type != JUNK_ATOM) &&
154 (atom_type != MDAT_ATOM) &&
155 (atom_type != MOOV_ATOM) &&
156 (atom_type != PNOT_ATOM) &&
157 (atom_type != SKIP_ATOM) &&
158 (atom_type != WIDE_ATOM) &&
159 (atom_type != PICT_ATOM) &&
160 (atom_type != UUID_ATOM) &&
161 (atom_type != FTYP_ATOM)) {
162 printf ("encountered non-QT top-level atom (is this a Quicktime file?)\n");
163 break;
164 }
165 atom_offset += atom_size;
166 }
38100e8c
MM
167
168 if (atom_type != MOOV_ATOM) {
169 printf ("last atom in file was not a moov atom\n");
d296a658 170 free(ftyp_atom);
38100e8c
MM
171 fclose(infile);
172 return 0;
173 }
174
175 /* moov atom was, in fact, the last atom in the chunk; load the whole
176 * moov atom */
e2daf554
BC
177 fseeko(infile, -atom_size, SEEK_END);
178 last_offset = ftello(infile);
38100e8c
MM
179 moov_atom_size = atom_size;
180 moov_atom = malloc(moov_atom_size);
181 if (!moov_atom) {
06a497af 182 printf ("could not allocate %"PRIu64" byte for moov atom\n",
38100e8c 183 atom_size);
c937454d 184 goto error_out;
38100e8c
MM
185 }
186 if (fread(moov_atom, atom_size, 1, infile) != 1) {
187 perror(argv[1]);
c937454d 188 goto error_out;
38100e8c
MM
189 }
190
191 /* this utility does not support compressed atoms yet, so disqualify
192 * files with compressed QT atoms */
193 if (BE_32(&moov_atom[12]) == CMOV_ATOM) {
194 printf ("this utility does not support compressed moov atoms yet\n");
c937454d 195 goto error_out;
38100e8c
MM
196 }
197
198 /* close; will be re-opened later */
199 fclose(infile);
c937454d 200 infile = NULL;
38100e8c
MM
201
202 /* crawl through the moov chunk in search of stco or co64 atoms */
203 for (i = 4; i < moov_atom_size - 4; i++) {
204 atom_type = BE_32(&moov_atom[i]);
205 if (atom_type == STCO_ATOM) {
206 printf (" patching stco atom...\n");
207 atom_size = BE_32(&moov_atom[i - 4]);
208 if (i + atom_size - 4 > moov_atom_size) {
209 printf (" bad atom size\n");
c937454d 210 goto error_out;
38100e8c
MM
211 }
212 offset_count = BE_32(&moov_atom[i + 8]);
213 for (j = 0; j < offset_count; j++) {
214 current_offset = BE_32(&moov_atom[i + 12 + j * 4]);
215 current_offset += moov_atom_size;
216 moov_atom[i + 12 + j * 4 + 0] = (current_offset >> 24) & 0xFF;
217 moov_atom[i + 12 + j * 4 + 1] = (current_offset >> 16) & 0xFF;
218 moov_atom[i + 12 + j * 4 + 2] = (current_offset >> 8) & 0xFF;
219 moov_atom[i + 12 + j * 4 + 3] = (current_offset >> 0) & 0xFF;
220 }
221 i += atom_size - 4;
222 } else if (atom_type == CO64_ATOM) {
223 printf (" patching co64 atom...\n");
224 atom_size = BE_32(&moov_atom[i - 4]);
225 if (i + atom_size - 4 > moov_atom_size) {
226 printf (" bad atom size\n");
c937454d 227 goto error_out;
38100e8c
MM
228 }
229 offset_count = BE_32(&moov_atom[i + 8]);
230 for (j = 0; j < offset_count; j++) {
231 current_offset = BE_64(&moov_atom[i + 12 + j * 8]);
232 current_offset += moov_atom_size;
233 moov_atom[i + 12 + j * 8 + 0] = (current_offset >> 56) & 0xFF;
234 moov_atom[i + 12 + j * 8 + 1] = (current_offset >> 48) & 0xFF;
235 moov_atom[i + 12 + j * 8 + 2] = (current_offset >> 40) & 0xFF;
236 moov_atom[i + 12 + j * 8 + 3] = (current_offset >> 32) & 0xFF;
237 moov_atom[i + 12 + j * 8 + 4] = (current_offset >> 24) & 0xFF;
238 moov_atom[i + 12 + j * 8 + 5] = (current_offset >> 16) & 0xFF;
239 moov_atom[i + 12 + j * 8 + 6] = (current_offset >> 8) & 0xFF;
240 moov_atom[i + 12 + j * 8 + 7] = (current_offset >> 0) & 0xFF;
241 }
242 i += atom_size - 4;
243 }
244 }
245
246 /* re-open the input file and open the output file */
247 infile = fopen(argv[1], "rb");
248 if (!infile) {
249 perror(argv[1]);
c937454d 250 goto error_out;
38100e8c 251 }
fd7789db
BC
252
253 if (start_offset > 0) { /* seek after ftyp atom */
254 fseeko(infile, start_offset, SEEK_SET);
255 last_offset -= start_offset;
256 }
60a9cc58 257
38100e8c
MM
258 outfile = fopen(argv[2], "wb");
259 if (!outfile) {
260 perror(argv[2]);
c937454d 261 goto error_out;
38100e8c
MM
262 }
263
60a9cc58
BC
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
38100e8c 273 /* dump the new moov atom */
d4fcd7c6 274 printf (" writing moov atom...\n");
38100e8c
MM
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);
331cb6c3 303 free(ftyp_atom);
38100e8c
MM
304
305 return 0;
306
307error_out:
c937454d 308 if (infile)
e3d7269f 309 fclose(infile);
c937454d 310 if (outfile)
e3d7269f 311 fclose(outfile);
38100e8c 312 free(moov_atom);
331cb6c3 313 free(ftyp_atom);
38100e8c
MM
314 return 1;
315}