]> git.sesse.net Git - ffmpeg/blob - tools/qt-faststart.c
Merge commit 'c0329748b04e1f175dad8c9c2ebf22a5e2dc5b72'
[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     uint64_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         return 0;
107     }
108
109     if (!strcmp(argv[1], argv[2])) {
110         fprintf(stderr, "input and output files need to be different\n");
111         return 1;
112     }
113
114     infile = fopen(argv[1], "rb");
115     if (!infile) {
116         perror(argv[1]);
117         goto error_out;
118     }
119
120     /* traverse through the atoms in the file to make sure that 'moov' is
121      * at the end */
122     while (!feof(infile)) {
123         if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
124             break;
125         }
126         atom_size = (uint32_t) BE_32(&atom_bytes[0]);
127         atom_type = BE_32(&atom_bytes[4]);
128
129         /* keep ftyp atom */
130         if (atom_type == FTYP_ATOM) {
131             ftyp_atom_size = atom_size;
132             free(ftyp_atom);
133             ftyp_atom = malloc(ftyp_atom_size);
134             if (!ftyp_atom) {
135                 printf("could not allocate %"PRIu64" bytes for ftyp atom\n",
136                        atom_size);
137                 goto error_out;
138             }
139             fseeko(infile, -ATOM_PREAMBLE_SIZE, SEEK_CUR);
140             if (fread(ftyp_atom, atom_size, 1, infile) != 1) {
141                 perror(argv[1]);
142                 goto error_out;
143             }
144             start_offset = ftello(infile);
145         } else {
146             /* 64-bit special case */
147             if (atom_size == 1) {
148                 if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
149                     break;
150                 }
151                 atom_size = BE_64(&atom_bytes[0]);
152                 fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE * 2, SEEK_CUR);
153             } else {
154                 fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE, SEEK_CUR);
155             }
156         }
157         printf("%c%c%c%c %10"PRIu64" %"PRIu64"\n",
158                (atom_type >> 24) & 255,
159                (atom_type >> 16) & 255,
160                (atom_type >>  8) & 255,
161                (atom_type >>  0) & 255,
162                atom_offset,
163                atom_size);
164         if ((atom_type != FREE_ATOM) &&
165             (atom_type != JUNK_ATOM) &&
166             (atom_type != MDAT_ATOM) &&
167             (atom_type != MOOV_ATOM) &&
168             (atom_type != PNOT_ATOM) &&
169             (atom_type != SKIP_ATOM) &&
170             (atom_type != WIDE_ATOM) &&
171             (atom_type != PICT_ATOM) &&
172             (atom_type != UUID_ATOM) &&
173             (atom_type != FTYP_ATOM)) {
174             printf("encountered non-QT top-level atom (is this a QuickTime file?)\n");
175             break;
176         }
177         atom_offset += atom_size;
178
179         /* The atom header is 8 (or 16 bytes), if the atom size (which
180          * includes these 8 or 16 bytes) is less than that, we won't be
181          * able to continue scanning sensibly after this atom, so break. */
182         if (atom_size < 8)
183             break;
184     }
185
186     if (atom_type != MOOV_ATOM) {
187         printf("last atom in file was not a moov atom\n");
188         free(ftyp_atom);
189         fclose(infile);
190         return 0;
191     }
192
193     /* moov atom was, in fact, the last atom in the chunk; load the whole
194      * moov atom */
195     fseeko(infile, -atom_size, SEEK_END);
196     last_offset    = ftello(infile);
197     moov_atom_size = atom_size;
198     moov_atom      = malloc(moov_atom_size);
199     if (!moov_atom) {
200         printf("could not allocate %"PRIu64" bytes for moov atom\n", atom_size);
201         goto error_out;
202     }
203     if (fread(moov_atom, atom_size, 1, infile) != 1) {
204         perror(argv[1]);
205         goto error_out;
206     }
207
208     /* this utility does not support compressed atoms yet, so disqualify
209      * files with compressed QT atoms */
210     if (BE_32(&moov_atom[12]) == CMOV_ATOM) {
211         printf("this utility does not support compressed moov atoms yet\n");
212         goto error_out;
213     }
214
215     /* close; will be re-opened later */
216     fclose(infile);
217     infile = NULL;
218
219     /* crawl through the moov chunk in search of stco or co64 atoms */
220     for (i = 4; i < moov_atom_size - 4; i++) {
221         atom_type = BE_32(&moov_atom[i]);
222         if (atom_type == STCO_ATOM) {
223             printf(" patching stco 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");
227                 goto error_out;
228             }
229             offset_count = BE_32(&moov_atom[i + 8]);
230             for (j = 0; j < offset_count; j++) {
231                 current_offset  = BE_32(&moov_atom[i + 12 + j * 4]);
232                 current_offset += moov_atom_size;
233                 moov_atom[i + 12 + j * 4 + 0] = (current_offset >> 24) & 0xFF;
234                 moov_atom[i + 12 + j * 4 + 1] = (current_offset >> 16) & 0xFF;
235                 moov_atom[i + 12 + j * 4 + 2] = (current_offset >>  8) & 0xFF;
236                 moov_atom[i + 12 + j * 4 + 3] = (current_offset >>  0) & 0xFF;
237             }
238             i += atom_size - 4;
239         } else if (atom_type == CO64_ATOM) {
240             printf(" patching co64 atom...\n");
241             atom_size = BE_32(&moov_atom[i - 4]);
242             if (i + atom_size - 4 > moov_atom_size) {
243                 printf(" bad atom size\n");
244                 goto error_out;
245             }
246             offset_count = BE_32(&moov_atom[i + 8]);
247             for (j = 0; j < offset_count; j++) {
248                 current_offset  = BE_64(&moov_atom[i + 12 + j * 8]);
249                 current_offset += moov_atom_size;
250                 moov_atom[i + 12 + j * 8 + 0] = (current_offset >> 56) & 0xFF;
251                 moov_atom[i + 12 + j * 8 + 1] = (current_offset >> 48) & 0xFF;
252                 moov_atom[i + 12 + j * 8 + 2] = (current_offset >> 40) & 0xFF;
253                 moov_atom[i + 12 + j * 8 + 3] = (current_offset >> 32) & 0xFF;
254                 moov_atom[i + 12 + j * 8 + 4] = (current_offset >> 24) & 0xFF;
255                 moov_atom[i + 12 + j * 8 + 5] = (current_offset >> 16) & 0xFF;
256                 moov_atom[i + 12 + j * 8 + 6] = (current_offset >>  8) & 0xFF;
257                 moov_atom[i + 12 + j * 8 + 7] = (current_offset >>  0) & 0xFF;
258             }
259             i += atom_size - 4;
260         }
261     }
262
263     /* re-open the input file and open the output file */
264     infile = fopen(argv[1], "rb");
265     if (!infile) {
266         perror(argv[1]);
267         goto error_out;
268     }
269
270     if (start_offset > 0) { /* seek after ftyp atom */
271         fseeko(infile, start_offset, SEEK_SET);
272         last_offset -= start_offset;
273     }
274
275     outfile = fopen(argv[2], "wb");
276     if (!outfile) {
277         perror(argv[2]);
278         goto error_out;
279     }
280
281     /* dump the same ftyp atom */
282     if (ftyp_atom_size > 0) {
283         printf(" writing ftyp atom...\n");
284         if (fwrite(ftyp_atom, ftyp_atom_size, 1, outfile) != 1) {
285             perror(argv[2]);
286             goto error_out;
287         }
288     }
289
290     /* dump the new moov atom */
291     printf(" writing moov atom...\n");
292     if (fwrite(moov_atom, moov_atom_size, 1, outfile) != 1) {
293         perror(argv[2]);
294         goto error_out;
295     }
296
297     /* copy the remainder of the infile, from offset 0 -> last_offset - 1 */
298     bytes_to_copy = FFMIN(COPY_BUFFER_SIZE, last_offset);
299     copy_buffer = malloc(bytes_to_copy);
300     if (!copy_buffer) {
301         printf("could not allocate %d bytes for copy_buffer\n", bytes_to_copy);
302         goto error_out;
303     }
304     printf(" copying rest of file...\n");
305     while (last_offset) {
306         bytes_to_copy = FFMIN(bytes_to_copy, last_offset);
307
308         if (fread(copy_buffer, bytes_to_copy, 1, infile) != 1) {
309             perror(argv[1]);
310             goto error_out;
311         }
312         if (fwrite(copy_buffer, bytes_to_copy, 1, outfile) != 1) {
313             perror(argv[2]);
314             goto error_out;
315         }
316         last_offset -= bytes_to_copy;
317     }
318
319     fclose(infile);
320     fclose(outfile);
321     free(moov_atom);
322     free(ftyp_atom);
323     free(copy_buffer);
324
325     return 0;
326
327 error_out:
328     if (infile)
329         fclose(infile);
330     if (outfile)
331         fclose(outfile);
332     free(moov_atom);
333     free(ftyp_atom);
334     free(copy_buffer);
335     return 1;
336 }