Improve performance of registering font preferences
[chromium-blink-merge.git] / media / tools / qt_faststart / qt_faststart.c
blob07b5aa3555762eb43db5e6cf2e39346def919319
1 /*
2 * qt-faststart.c, v0.2
3 * by Mike Melanson (melanson@pcisys.net)
4 * patch by Frank Barchard, to remove last FREE atom.
5 * This file is placed in the public domain. Use the program however you
6 * see fit.
8 * This utility rearranges a Quicktime file such that the moov atom
9 * is in front of the data, thus facilitating network streaming.
11 * To compile this program, start from the base directory from which you
12 * are building FFmpeg and type:
13 * make tools/qt-faststart
14 * The qt-faststart program will be built in the tools/ directory. If you
15 * do not build the program in this manner, correct results are not
16 * guaranteed, particularly on 64-bit platforms.
17 * Invoke the program with:
18 * qt-faststart <infile.mov> <outfile.mov>
20 * Notes: Quicktime files can come in many configurations of top-level
21 * atoms. This utility stipulates that the very last atom in the file needs
22 * to be a moov atom. When given such a file, this utility will rearrange
23 * the top-level atoms by shifting the moov atom from the back of the file
24 * to the front, and patch the chunk offsets along the way. This utility
25 * presently only operates on uncompressed moov atoms.
28 #include <stdio.h>
29 #include <stdlib.h>
30 #ifndef _MSC_VER
31 #include <inttypes.h>
32 #else
33 #pragma warning(push,1)
34 #define PRIu64 "I64u"
35 #define PRIX64 "I64x"
36 #define uint64_t unsigned __int64
37 #define uint32_t unsigned int
38 #define uint8_t unsigned char
39 #define ftello _ftelli64
40 #define fseeko _fseeki64
41 #endif
43 #ifdef __MINGW32__
44 #define fseeko(x,y,z) fseeko64(x,y,z)
45 #define ftello(x) ftello64(x)
46 #endif
48 #define BE_16(x) ((((uint8_t*)(x))[0] << 8) | ((uint8_t*)(x))[1])
49 #define BE_32(x) ((((uint8_t*)(x))[0] << 24) | \
50 (((uint8_t*)(x))[1] << 16) | \
51 (((uint8_t*)(x))[2] << 8) | \
52 ((uint8_t*)(x))[3])
53 #define BE_64(x) (((uint64_t)(((uint8_t*)(x))[0]) << 56) | \
54 ((uint64_t)(((uint8_t*)(x))[1]) << 48) | \
55 ((uint64_t)(((uint8_t*)(x))[2]) << 40) | \
56 ((uint64_t)(((uint8_t*)(x))[3]) << 32) | \
57 ((uint64_t)(((uint8_t*)(x))[4]) << 24) | \
58 ((uint64_t)(((uint8_t*)(x))[5]) << 16) | \
59 ((uint64_t)(((uint8_t*)(x))[6]) << 8) | \
60 ((uint64_t)((uint8_t*)(x))[7]))
62 #define BE_FOURCC( ch0, ch1, ch2, ch3 ) \
63 ( (uint32_t)(unsigned char)(ch3) | \
64 ( (uint32_t)(unsigned char)(ch2) << 8 ) | \
65 ( (uint32_t)(unsigned char)(ch1) << 16 ) | \
66 ( (uint32_t)(unsigned char)(ch0) << 24 ) )
68 #define QT_ATOM BE_FOURCC
69 /* top level atoms */
70 #define FREE_ATOM QT_ATOM('f', 'r', 'e', 'e')
71 #define JUNK_ATOM QT_ATOM('j', 'u', 'n', 'k')
72 #define MDAT_ATOM QT_ATOM('m', 'd', 'a', 't')
73 #define MOOV_ATOM QT_ATOM('m', 'o', 'o', 'v')
74 #define PNOT_ATOM QT_ATOM('p', 'n', 'o', 't')
75 #define SKIP_ATOM QT_ATOM('s', 'k', 'i', 'p')
76 #define WIDE_ATOM QT_ATOM('w', 'i', 'd', 'e')
77 #define PICT_ATOM QT_ATOM('P', 'I', 'C', 'T')
78 #define FTYP_ATOM QT_ATOM('f', 't', 'y', 'p')
79 #define UUID_ATOM QT_ATOM('u', 'u', 'i', 'd')
81 #define CMOV_ATOM QT_ATOM('c', 'm', 'o', 'v')
82 #define STCO_ATOM QT_ATOM('s', 't', 'c', 'o')
83 #define CO64_ATOM QT_ATOM('c', 'o', '6', '4')
85 #define ATOM_PREAMBLE_SIZE 8
86 #define COPY_BUFFER_SIZE 1024
88 int main(int argc, char *argv[])
90 FILE *infile = 0;
91 FILE *outfile = 0;
92 unsigned char atom_bytes[ATOM_PREAMBLE_SIZE];
93 uint32_t atom_type = 0;
94 uint64_t atom_size = 0;
95 uint64_t last_offset = 0; // warning: 'last_offset' may be used uninitialized in this function
96 unsigned char *moov_atom = 0;
97 unsigned char *ftyp_atom = 0;
98 uint64_t moov_atom_size = 0; // warning: 'moov_atom_size' may be used uninitialized in this function
99 uint64_t ftyp_atom_size = 0;
100 uint64_t i, j;
101 uint32_t offset_count;
102 uint64_t current_offset;
103 uint64_t start_offset = 0;
104 unsigned char copy_buffer[COPY_BUFFER_SIZE];
105 int bytes_to_copy;
106 uint32_t last_atom_type = 0;
107 uint64_t last_atom_size = 0;
108 uint64_t last_atom_offset = 0;
109 uint64_t atom_offset;
110 int just_remove_free = 0;
111 int return_code = 1;
113 if (argc != 3) {
114 printf ("Usage: qt_faststart <infile.mov> <outfile.mov>\n");
115 goto exit_cleanup;
118 infile = fopen(argv[1], "rb");
119 if (!infile) {
120 perror(argv[1]);
121 goto exit_cleanup;
124 /* traverse through the atoms in the file to make sure that 'moov' is
125 * at the end */
126 while (!feof(infile)) {
127 if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
128 break;
130 atom_size = (uint32_t)BE_32(&atom_bytes[0]);
131 atom_type = BE_32(&atom_bytes[4]);
132 atom_offset = ftello(infile) - ATOM_PREAMBLE_SIZE;
134 /* keep ftyp atom */
135 if (atom_type == FTYP_ATOM) {
136 ftyp_atom_size = atom_size;
137 ftyp_atom = malloc(ftyp_atom_size);
138 if (!ftyp_atom) {
139 printf ("could not allocate 0x%"PRIX64" byte for ftyp atom\n",
140 atom_size);
141 goto exit_cleanup;
143 fseeko(infile, -ATOM_PREAMBLE_SIZE, SEEK_CUR);
144 if (fread(ftyp_atom, atom_size, 1, infile) != 1) {
145 perror(argv[1]);
146 goto exit_cleanup;
148 start_offset = ftello(infile);
149 } else {
150 /* 64-bit special case */
151 if (atom_size == 1) {
152 if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
153 break;
155 atom_size = BE_64(&atom_bytes[0]);
156 fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE * 2, SEEK_CUR);
157 } else {
158 fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE, SEEK_CUR);
162 printf("%c%c%c%c %10"PRIu64" %"PRIu64"\n",
163 (atom_type >> 24) & 255,
164 (atom_type >> 16) & 255,
165 (atom_type >> 8) & 255,
166 (atom_type >> 0) & 255,
167 atom_offset,
168 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;
183 if ((atom_type != FREE_ATOM)) {
184 last_atom_size = atom_size;
185 last_atom_type = atom_type;
186 last_atom_offset = ftello(infile) - last_atom_size;
188 if ((atom_type != FREE_ATOM) && (atom_type != MOOV_ATOM))
189 last_offset = ftello(infile);
192 if (last_atom_type == MOOV_ATOM) {
194 /* moov atom was, in fact, the last atom in the chunk; load the whole
195 * moov atom */
196 fseeko(infile, last_atom_offset, SEEK_SET);
197 moov_atom_size = last_atom_size;
198 moov_atom = malloc(moov_atom_size);
199 if (!moov_atom) {
200 printf ("could not allocate 0x%"PRIX64" byte for moov atom\n",
201 atom_size);
202 goto exit_cleanup;
204 if (fread(moov_atom, moov_atom_size, 1, infile) != 1) {
205 perror(argv[1]);
206 goto exit_cleanup;
209 /* this utility does not support compressed atoms yet, so disqualify
210 * files with compressed QT atoms */
211 if (BE_32(&moov_atom[12]) == CMOV_ATOM) {
212 printf ("this utility does not support compressed moov atoms yet\n");
213 goto exit_cleanup;
216 /* crawl through the moov chunk in search of stco or co64 atoms */
217 for (i = 4; i < moov_atom_size - 4; i++) {
218 atom_type = BE_32(&moov_atom[i]);
219 if (atom_type == STCO_ATOM) {
220 printf (" patching stco atom...\n");
221 atom_size = BE_32(&moov_atom[i - 4]);
222 if (i + atom_size - 4 > moov_atom_size) {
223 printf (" bad atom size\n");
224 free(moov_atom);
225 return 1;
227 offset_count = BE_32(&moov_atom[i + 8]);
228 for (j = 0; j < offset_count; j++) {
229 current_offset = BE_32(&moov_atom[i + 12 + j * 4]);
230 current_offset += moov_atom_size;
231 moov_atom[i + 12 + j * 4 + 0] = (current_offset >> 24) & 0xFF;
232 moov_atom[i + 12 + j * 4 + 1] = (current_offset >> 16) & 0xFF;
233 moov_atom[i + 12 + j * 4 + 2] = (current_offset >> 8) & 0xFF;
234 moov_atom[i + 12 + j * 4 + 3] = (current_offset >> 0) & 0xFF;
236 i += atom_size - 4;
237 } else if (atom_type == CO64_ATOM) {
238 printf (" patching co64 atom...\n");
239 atom_size = BE_32(&moov_atom[i - 4]);
240 if (i + atom_size - 4 > moov_atom_size) {
241 printf (" bad atom size\n");
242 free(moov_atom);
243 return 1;
245 offset_count = BE_32(&moov_atom[i + 8]);
246 for (j = 0; j < offset_count; j++) {
247 current_offset = BE_64(&moov_atom[i + 12 + j * 8]);
248 current_offset += moov_atom_size;
249 moov_atom[i + 12 + j * 8 + 0] = (current_offset >> 56) & 0xFF;
250 moov_atom[i + 12 + j * 8 + 1] = (current_offset >> 48) & 0xFF;
251 moov_atom[i + 12 + j * 8 + 2] = (current_offset >> 40) & 0xFF;
252 moov_atom[i + 12 + j * 8 + 3] = (current_offset >> 32) & 0xFF;
253 moov_atom[i + 12 + j * 8 + 4] = (current_offset >> 24) & 0xFF;
254 moov_atom[i + 12 + j * 8 + 5] = (current_offset >> 16) & 0xFF;
255 moov_atom[i + 12 + j * 8 + 6] = (current_offset >> 8) & 0xFF;
256 moov_atom[i + 12 + j * 8 + 7] = (current_offset >> 0) & 0xFF;
258 i += atom_size - 4;
261 } else {
262 if (atom_type == FREE_ATOM) {
263 printf ("free atom at %"PRIu64" removed.\n", last_offset);
264 just_remove_free = 1;
265 } else {
266 printf ("Last atom in file was not a moov or free atom. Skipping.\n");
267 goto exit_cleanup;
271 outfile = fopen(argv[2], "wb");
272 if (!outfile) {
273 perror(argv[2]);
274 goto exit_cleanup;
277 /* dump the same ftyp atom */
278 if (ftyp_atom_size > 0) {
279 printf (" writing ftyp atom...\n");
280 if (fwrite(ftyp_atom, ftyp_atom_size, 1, outfile) != 1) {
281 perror(argv[2]);
282 goto exit_cleanup;
286 if (!just_remove_free) {
287 /* dump the new moov atom */
288 printf (" writing moov atom...\n");
289 if (fwrite(moov_atom, moov_atom_size, 1, outfile) != 1) {
290 perror(argv[2]);
291 goto exit_cleanup;
295 /* copy the remainder of the infile, from offset 0 -> last_offset - 1 */
296 printf (" copying rest of file: %"PRIu64"..%"PRIu64"\n", start_offset, last_offset);
297 fseeko(infile, start_offset, SEEK_SET);
298 last_offset -= start_offset; /* Amount to copy */
299 while (last_offset > 0) {
300 if (last_offset > COPY_BUFFER_SIZE)
301 bytes_to_copy = COPY_BUFFER_SIZE;
302 else
303 bytes_to_copy = last_offset;
305 if (fread(copy_buffer, bytes_to_copy, 1, infile) != 1) {
306 perror(argv[1]);
307 goto exit_cleanup;
309 if (fwrite(copy_buffer, bytes_to_copy, 1, outfile) != 1) {
310 perror(argv[2]);
311 goto exit_cleanup;
313 last_offset -= bytes_to_copy;
315 return_code = 0;
317 exit_cleanup:
318 if (infile)
319 fclose(infile);
320 if (outfile)
321 fclose(outfile);
322 if (moov_atom)
323 free(moov_atom);
324 if (ftyp_atom)
325 free(ftyp_atom);
326 return return_code;