]> git.sesse.net Git - ffmpeg/blob - tools/qt-faststart.c
Merge commit '93632a70f9ac2cb2ebf0e69d21fdfaae68ff02fd'
[ffmpeg] / tools / qt-faststart.c
1 /*
2  * qt-faststart.c, v0.2
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 #include <string.h>
31
32 #ifdef __MINGW32__
33 #define fseeko(x, y, z) fseeko64(x, y, z)
34 #define ftello(x)       ftello64(x)
35 #elif defined(_WIN32)
36 #define fseeko(x, y, z) _fseeki64(x, y, z)
37 #define ftello(x)       _ftelli64(x)
38 #endif
39
40 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
41
42 #define BE_16(x) ((((uint8_t*)(x))[0] <<  8) | ((uint8_t*)(x))[1])
43
44 #define BE_32(x) ((((uint8_t*)(x))[0] << 24) |  \
45                   (((uint8_t*)(x))[1] << 16) |  \
46                   (((uint8_t*)(x))[2] <<  8) |  \
47                    ((uint8_t*)(x))[3])
48
49 #define BE_64(x) (((uint64_t)(((uint8_t*)(x))[0]) << 56) |  \
50                   ((uint64_t)(((uint8_t*)(x))[1]) << 48) |  \
51                   ((uint64_t)(((uint8_t*)(x))[2]) << 40) |  \
52                   ((uint64_t)(((uint8_t*)(x))[3]) << 32) |  \
53                   ((uint64_t)(((uint8_t*)(x))[4]) << 24) |  \
54                   ((uint64_t)(((uint8_t*)(x))[5]) << 16) |  \
55                   ((uint64_t)(((uint8_t*)(x))[6]) <<  8) |  \
56                   ((uint64_t)( (uint8_t*)(x))[7]))
57
58 #define BE_FOURCC(ch0, ch1, ch2, ch3)           \
59     ( (uint32_t)(unsigned char)(ch3)        |   \
60      ((uint32_t)(unsigned char)(ch2) <<  8) |   \
61      ((uint32_t)(unsigned char)(ch1) << 16) |   \
62      ((uint32_t)(unsigned char)(ch0) << 24) )
63
64 #define QT_ATOM BE_FOURCC
65 /* top level atoms */
66 #define FREE_ATOM QT_ATOM('f', 'r', 'e', 'e')
67 #define JUNK_ATOM QT_ATOM('j', 'u', 'n', 'k')
68 #define MDAT_ATOM QT_ATOM('m', 'd', 'a', 't')
69 #define MOOV_ATOM QT_ATOM('m', 'o', 'o', 'v')
70 #define PNOT_ATOM QT_ATOM('p', 'n', 'o', 't')
71 #define SKIP_ATOM QT_ATOM('s', 'k', 'i', 'p')
72 #define WIDE_ATOM QT_ATOM('w', 'i', 'd', 'e')
73 #define PICT_ATOM QT_ATOM('P', 'I', 'C', 'T')
74 #define FTYP_ATOM QT_ATOM('f', 't', 'y', 'p')
75 #define UUID_ATOM QT_ATOM('u', 'u', 'i', 'd')
76
77 #define CMOV_ATOM QT_ATOM('c', 'm', 'o', 'v')
78 #define STCO_ATOM QT_ATOM('s', 't', 'c', 'o')
79 #define CO64_ATOM QT_ATOM('c', 'o', '6', '4')
80
81 #define ATOM_PREAMBLE_SIZE    8
82 #define COPY_BUFFER_SIZE   33554432
83
84 int main(int argc, char *argv[])
85 {
86     FILE *infile  = NULL;
87     FILE *outfile = NULL;
88     unsigned char atom_bytes[ATOM_PREAMBLE_SIZE];
89     uint32_t atom_type   = 0;
90     uint64_t atom_size   = 0;
91     uint64_t atom_offset = 0;
92     uint64_t last_offset;
93     unsigned char *moov_atom = NULL;
94     unsigned char *ftyp_atom = NULL;
95     uint64_t moov_atom_size;
96     uint64_t ftyp_atom_size = 0;
97     uint64_t i, j;
98     uint32_t offset_count;
99     uint64_t current_offset;
100     int64_t start_offset = 0;
101     unsigned char *copy_buffer = NULL;
102     int bytes_to_copy;
103
104     if (argc != 3) {
105         printf("Usage: qt-faststart <infile.mov> <outfile.mov>\n"
106                "Note: alternatively you can use -movflags +faststart in ffmpeg\n");
107         return 0;
108     }
109
110     if (!strcmp(argv[1], argv[2])) {
111         fprintf(stderr, "input and output files need to be different\n");
112         return 1;
113     }
114
115     infile = fopen(argv[1], "rb");
116     if (!infile) {
117         perror(argv[1]);
118         goto error_out;
119     }
120
121     /* traverse through the atoms in the file to make sure that 'moov' is
122      * at the end */
123     while (!feof(infile)) {
124         if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
125             break;
126         }
127         atom_size = (uint32_t) BE_32(&atom_bytes[0]);
128         atom_type = BE_32(&atom_bytes[4]);
129
130         /* keep ftyp atom */
131         if (atom_type == FTYP_ATOM) {
132             ftyp_atom_size = atom_size;
133             free(ftyp_atom);
134             ftyp_atom = malloc(ftyp_atom_size);
135             if (!ftyp_atom) {
136                 printf("could not allocate %"PRIu64" bytes for ftyp atom\n",
137                        atom_size);
138                 goto error_out;
139             }
140             if (   fseeko(infile, -ATOM_PREAMBLE_SIZE, SEEK_CUR)
141                 || fread(ftyp_atom, atom_size, 1, infile) != 1
142                 || (start_offset = ftello(infile))<0) {
143                 perror(argv[1]);
144                 goto error_out;
145             }
146         } else {
147             int ret;
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                 ret = fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE * 2, SEEK_CUR);
155             } else {
156                 ret = fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE, SEEK_CUR);
157             }
158             if(ret) {
159                 perror(argv[1]);
160                 goto error_out;
161             }
162         }
163         printf("%c%c%c%c %10"PRIu64" %"PRIu64"\n",
164                (atom_type >> 24) & 255,
165                (atom_type >> 16) & 255,
166                (atom_type >>  8) & 255,
167                (atom_type >>  0) & 255,
168                atom_offset,
169                atom_size);
170         if ((atom_type != FREE_ATOM) &&
171             (atom_type != JUNK_ATOM) &&
172             (atom_type != MDAT_ATOM) &&
173             (atom_type != MOOV_ATOM) &&
174             (atom_type != PNOT_ATOM) &&
175             (atom_type != SKIP_ATOM) &&
176             (atom_type != WIDE_ATOM) &&
177             (atom_type != PICT_ATOM) &&
178             (atom_type != UUID_ATOM) &&
179             (atom_type != FTYP_ATOM)) {
180             printf("encountered non-QT top-level atom (is this a QuickTime file?)\n");
181             break;
182         }
183         atom_offset += atom_size;
184
185         /* The atom header is 8 (or 16 bytes), if the atom size (which
186          * includes these 8 or 16 bytes) is less than that, we won't be
187          * able to continue scanning sensibly after this atom, so break. */
188         if (atom_size < 8)
189             break;
190     }
191
192     if (atom_type != MOOV_ATOM) {
193         printf("last atom in file was not a moov atom\n");
194         free(ftyp_atom);
195         fclose(infile);
196         return 0;
197     }
198
199     /* moov atom was, in fact, the last atom in the chunk; load the whole
200      * moov atom */
201     if (fseeko(infile, -atom_size, SEEK_END)) {
202         perror(argv[1]);
203         goto error_out;
204     }
205     last_offset    = ftello(infile);
206     moov_atom_size = atom_size;
207     moov_atom      = malloc(moov_atom_size);
208     if (!moov_atom) {
209         printf("could not allocate %"PRIu64" bytes for moov atom\n", atom_size);
210         goto error_out;
211     }
212     if (fread(moov_atom, atom_size, 1, infile) != 1) {
213         perror(argv[1]);
214         goto error_out;
215     }
216
217     /* this utility does not support compressed atoms yet, so disqualify
218      * files with compressed QT atoms */
219     if (BE_32(&moov_atom[12]) == CMOV_ATOM) {
220         printf("this utility does not support compressed moov atoms yet\n");
221         goto error_out;
222     }
223
224     /* close; will be re-opened later */
225     fclose(infile);
226     infile = NULL;
227
228     /* crawl through the moov chunk in search of stco or co64 atoms */
229     for (i = 4; i < moov_atom_size - 4; i++) {
230         atom_type = BE_32(&moov_atom[i]);
231         if (atom_type == STCO_ATOM) {
232             printf(" patching stco atom...\n");
233             atom_size = (uint32_t)BE_32(&moov_atom[i - 4]);
234             if (i + atom_size - 4 > moov_atom_size) {
235                 printf(" bad atom size\n");
236                 goto error_out;
237             }
238             offset_count = BE_32(&moov_atom[i + 8]);
239             if (i + 12LL + offset_count * 4LL > moov_atom_size) {
240                 printf(" bad atom size\n");
241                 goto error_out;
242             }
243             for (j = 0; j < offset_count; j++) {
244                 current_offset  = (uint32_t)BE_32(&moov_atom[i + 12 + j * 4]);
245                 current_offset += moov_atom_size;
246                 moov_atom[i + 12 + j * 4 + 0] = (current_offset >> 24) & 0xFF;
247                 moov_atom[i + 12 + j * 4 + 1] = (current_offset >> 16) & 0xFF;
248                 moov_atom[i + 12 + j * 4 + 2] = (current_offset >>  8) & 0xFF;
249                 moov_atom[i + 12 + j * 4 + 3] = (current_offset >>  0) & 0xFF;
250             }
251             i += atom_size - 4;
252         } else if (atom_type == CO64_ATOM) {
253             printf(" patching co64 atom...\n");
254             atom_size = (uint32_t)BE_32(&moov_atom[i - 4]);
255             if (i + atom_size - 4 > moov_atom_size) {
256                 printf(" bad atom size\n");
257                 goto error_out;
258             }
259             offset_count = BE_32(&moov_atom[i + 8]);
260             if (i + 12LL + offset_count * 8LL > moov_atom_size) {
261                 printf(" bad atom size\n");
262                 goto error_out;
263             }
264             for (j = 0; j < offset_count; j++) {
265                 current_offset  = BE_64(&moov_atom[i + 12 + j * 8]);
266                 current_offset += moov_atom_size;
267                 moov_atom[i + 12 + j * 8 + 0] = (current_offset >> 56) & 0xFF;
268                 moov_atom[i + 12 + j * 8 + 1] = (current_offset >> 48) & 0xFF;
269                 moov_atom[i + 12 + j * 8 + 2] = (current_offset >> 40) & 0xFF;
270                 moov_atom[i + 12 + j * 8 + 3] = (current_offset >> 32) & 0xFF;
271                 moov_atom[i + 12 + j * 8 + 4] = (current_offset >> 24) & 0xFF;
272                 moov_atom[i + 12 + j * 8 + 5] = (current_offset >> 16) & 0xFF;
273                 moov_atom[i + 12 + j * 8 + 6] = (current_offset >>  8) & 0xFF;
274                 moov_atom[i + 12 + j * 8 + 7] = (current_offset >>  0) & 0xFF;
275             }
276             i += atom_size - 4;
277         }
278     }
279
280     /* re-open the input file and open the output file */
281     infile = fopen(argv[1], "rb");
282     if (!infile) {
283         perror(argv[1]);
284         goto error_out;
285     }
286
287     if (start_offset > 0) { /* seek after ftyp atom */
288         if (fseeko(infile, start_offset, SEEK_SET)) {
289             perror(argv[1]);
290             goto error_out;
291         }
292
293         last_offset -= start_offset;
294     }
295
296     outfile = fopen(argv[2], "wb");
297     if (!outfile) {
298         perror(argv[2]);
299         goto error_out;
300     }
301
302     /* dump the same ftyp atom */
303     if (ftyp_atom_size > 0) {
304         printf(" writing ftyp atom...\n");
305         if (fwrite(ftyp_atom, ftyp_atom_size, 1, outfile) != 1) {
306             perror(argv[2]);
307             goto error_out;
308         }
309     }
310
311     /* dump the new moov atom */
312     printf(" writing moov atom...\n");
313     if (fwrite(moov_atom, moov_atom_size, 1, outfile) != 1) {
314         perror(argv[2]);
315         goto error_out;
316     }
317
318     /* copy the remainder of the infile, from offset 0 -> last_offset - 1 */
319     bytes_to_copy = FFMIN(COPY_BUFFER_SIZE, last_offset);
320     copy_buffer = malloc(bytes_to_copy);
321     if (!copy_buffer) {
322         printf("could not allocate %d bytes for copy_buffer\n", bytes_to_copy);
323         goto error_out;
324     }
325     printf(" copying rest of file...\n");
326     while (last_offset) {
327         bytes_to_copy = FFMIN(bytes_to_copy, last_offset);
328
329         if (fread(copy_buffer, bytes_to_copy, 1, infile) != 1) {
330             perror(argv[1]);
331             goto error_out;
332         }
333         if (fwrite(copy_buffer, bytes_to_copy, 1, outfile) != 1) {
334             perror(argv[2]);
335             goto error_out;
336         }
337         last_offset -= bytes_to_copy;
338     }
339
340     fclose(infile);
341     fclose(outfile);
342     free(moov_atom);
343     free(ftyp_atom);
344     free(copy_buffer);
345
346     return 0;
347
348 error_out:
349     if (infile)
350         fclose(infile);
351     if (outfile)
352         fclose(outfile);
353     free(moov_atom);
354     free(ftyp_atom);
355     free(copy_buffer);
356     return 1;
357 }