Use normal check_func test for math functions
[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{
78 FILE *infile;
79 FILE *outfile;
80 unsigned char atom_bytes[ATOM_PREAMBLE_SIZE];
81 uint32_t atom_type = 0;
c0339599 82 uint64_t atom_size = 0;
38100e8c
MM
83 uint64_t last_offset;
84 unsigned char *moov_atom;
60a9cc58 85 unsigned char *ftyp_atom = 0;
38100e8c 86 uint64_t moov_atom_size;
60a9cc58 87 uint64_t ftyp_atom_size = 0;
38100e8c
MM
88 uint64_t i, j;
89 uint32_t offset_count;
90 uint64_t current_offset;
60a9cc58 91 uint64_t start_offset = 0;
38100e8c
MM
92 unsigned char copy_buffer[COPY_BUFFER_SIZE];
93 int bytes_to_copy;
94
95 if (argc != 3) {
96 printf ("Usage: qt-faststart <infile.mov> <outfile.mov>\n");
97 return 0;
98 }
99
100 infile = fopen(argv[1], "rb");
101 if (!infile) {
102 perror(argv[1]);
103 return 1;
104 }
105
106 /* traverse through the atoms in the file to make sure that 'moov' is
107 * at the end */
108 while (!feof(infile)) {
109 if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
110 break;
111 }
7db2245d 112 atom_size = (uint32_t)BE_32(&atom_bytes[0]);
38100e8c
MM
113 atom_type = BE_32(&atom_bytes[4]);
114
115 if ((atom_type != FREE_ATOM) &&
116 (atom_type != JUNK_ATOM) &&
117 (atom_type != MDAT_ATOM) &&
118 (atom_type != MOOV_ATOM) &&
119 (atom_type != PNOT_ATOM) &&
120 (atom_type != SKIP_ATOM) &&
121 (atom_type != WIDE_ATOM) &&
122 (atom_type != PICT_ATOM) &&
31457d7a 123 (atom_type != UUID_ATOM) &&
38100e8c
MM
124 (atom_type != FTYP_ATOM)) {
125 printf ("encountered non-QT top-level atom (is this a Quicktime file?)\n");
126 break;
127 }
128
60a9cc58
BC
129 /* keep ftyp atom */
130 if (atom_type == FTYP_ATOM) {
131 ftyp_atom_size = atom_size;
132 ftyp_atom = malloc(ftyp_atom_size);
133 if (!ftyp_atom) {
06a497af 134 printf ("could not allocate %"PRIu64" byte for ftyp atom\n",
60a9cc58
BC
135 atom_size);
136 fclose(infile);
137 return 1;
138 }
139 fseeko(infile, -ATOM_PREAMBLE_SIZE, SEEK_CUR);
140 if (fread(ftyp_atom, atom_size, 1, infile) != 1) {
141 perror(argv[1]);
142 free(ftyp_atom);
143 fclose(infile);
144 return 1;
145 }
146 start_offset = ftello(infile);
147 continue;
148 }
149
38100e8c
MM
150 /* 64-bit special case */
151 if (atom_size == 1) {
152 if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
153 break;
154 }
155 atom_size = BE_64(&atom_bytes[0]);
e2daf554 156 fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE * 2, SEEK_CUR);
38100e8c 157 } else {
e2daf554 158 fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE, SEEK_CUR);
38100e8c
MM
159 }
160 }
161
162 if (atom_type != MOOV_ATOM) {
163 printf ("last atom in file was not a moov atom\n");
164 fclose(infile);
165 return 0;
166 }
167
168 /* moov atom was, in fact, the last atom in the chunk; load the whole
169 * moov atom */
e2daf554
BC
170 fseeko(infile, -atom_size, SEEK_END);
171 last_offset = ftello(infile);
38100e8c
MM
172 moov_atom_size = atom_size;
173 moov_atom = malloc(moov_atom_size);
174 if (!moov_atom) {
06a497af 175 printf ("could not allocate %"PRIu64" byte for moov atom\n",
38100e8c
MM
176 atom_size);
177 fclose(infile);
178 return 1;
179 }
180 if (fread(moov_atom, atom_size, 1, infile) != 1) {
181 perror(argv[1]);
182 free(moov_atom);
183 fclose(infile);
184 return 1;
185 }
186
187 /* this utility does not support compressed atoms yet, so disqualify
188 * files with compressed QT atoms */
189 if (BE_32(&moov_atom[12]) == CMOV_ATOM) {
190 printf ("this utility does not support compressed moov atoms yet\n");
191 free(moov_atom);
192 fclose(infile);
193 return 1;
194 }
195
196 /* close; will be re-opened later */
197 fclose(infile);
198
199 /* crawl through the moov chunk in search of stco or co64 atoms */
200 for (i = 4; i < moov_atom_size - 4; i++) {
201 atom_type = BE_32(&moov_atom[i]);
202 if (atom_type == STCO_ATOM) {
203 printf (" patching stco atom...\n");
204 atom_size = BE_32(&moov_atom[i - 4]);
205 if (i + atom_size - 4 > moov_atom_size) {
206 printf (" bad atom size\n");
207 free(moov_atom);
208 return 1;
209 }
210 offset_count = BE_32(&moov_atom[i + 8]);
211 for (j = 0; j < offset_count; j++) {
212 current_offset = BE_32(&moov_atom[i + 12 + j * 4]);
213 current_offset += moov_atom_size;
214 moov_atom[i + 12 + j * 4 + 0] = (current_offset >> 24) & 0xFF;
215 moov_atom[i + 12 + j * 4 + 1] = (current_offset >> 16) & 0xFF;
216 moov_atom[i + 12 + j * 4 + 2] = (current_offset >> 8) & 0xFF;
217 moov_atom[i + 12 + j * 4 + 3] = (current_offset >> 0) & 0xFF;
218 }
219 i += atom_size - 4;
220 } else if (atom_type == CO64_ATOM) {
221 printf (" patching co64 atom...\n");
222 atom_size = BE_32(&moov_atom[i - 4]);
223 if (i + atom_size - 4 > moov_atom_size) {
224 printf (" bad atom size\n");
225 free(moov_atom);
226 return 1;
227 }
228 offset_count = BE_32(&moov_atom[i + 8]);
229 for (j = 0; j < offset_count; j++) {
230 current_offset = BE_64(&moov_atom[i + 12 + j * 8]);
231 current_offset += moov_atom_size;
232 moov_atom[i + 12 + j * 8 + 0] = (current_offset >> 56) & 0xFF;
233 moov_atom[i + 12 + j * 8 + 1] = (current_offset >> 48) & 0xFF;
234 moov_atom[i + 12 + j * 8 + 2] = (current_offset >> 40) & 0xFF;
235 moov_atom[i + 12 + j * 8 + 3] = (current_offset >> 32) & 0xFF;
236 moov_atom[i + 12 + j * 8 + 4] = (current_offset >> 24) & 0xFF;
237 moov_atom[i + 12 + j * 8 + 5] = (current_offset >> 16) & 0xFF;
238 moov_atom[i + 12 + j * 8 + 6] = (current_offset >> 8) & 0xFF;
239 moov_atom[i + 12 + j * 8 + 7] = (current_offset >> 0) & 0xFF;
240 }
241 i += atom_size - 4;
242 }
243 }
244
245 /* re-open the input file and open the output file */
246 infile = fopen(argv[1], "rb");
247 if (!infile) {
248 perror(argv[1]);
249 free(moov_atom);
250 return 1;
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]);
261 fclose(outfile);
262 free(moov_atom);
263 return 1;
264 }
265
60a9cc58
BC
266 /* dump the same ftyp atom */
267 if (ftyp_atom_size > 0) {
268 printf (" writing ftyp atom...\n");
269 if (fwrite(ftyp_atom, ftyp_atom_size, 1, outfile) != 1) {
270 perror(argv[2]);
271 goto error_out;
272 }
273 }
274
38100e8c 275 /* dump the new moov atom */
d4fcd7c6 276 printf (" writing moov atom...\n");
38100e8c
MM
277 if (fwrite(moov_atom, moov_atom_size, 1, outfile) != 1) {
278 perror(argv[2]);
279 goto error_out;
280 }
281
282 /* copy the remainder of the infile, from offset 0 -> last_offset - 1 */
283 printf (" copying rest of file...\n");
284 while (last_offset) {
285 if (last_offset > COPY_BUFFER_SIZE)
286 bytes_to_copy = COPY_BUFFER_SIZE;
287 else
288 bytes_to_copy = last_offset;
289
290 if (fread(copy_buffer, bytes_to_copy, 1, infile) != 1) {
291 perror(argv[1]);
292 goto error_out;
293 }
294 if (fwrite(copy_buffer, bytes_to_copy, 1, outfile) != 1) {
295 perror(argv[2]);
296 goto error_out;
297 }
298
299 last_offset -= bytes_to_copy;
300 }
301
302 fclose(infile);
303 fclose(outfile);
304 free(moov_atom);
21c5aca6
BC
305 if (ftyp_atom_size > 0)
306 free(ftyp_atom);
38100e8c
MM
307
308 return 0;
309
310error_out:
311 fclose(infile);
312 fclose(outfile);
313 free(moov_atom);
21c5aca6
BC
314 if (ftyp_atom_size > 0)
315 free(ftyp_atom);
38100e8c
MM
316 return 1;
317}