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