3 * by Mike Melanson (melanson@pcisys.net)
4 * This file is placed in the public domain. Use the program however you
7 * This utility rearranges a Quicktime file such that the moov atom
8 * is in front of the data, thus facilitating network streaming.
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>
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.
33 #define fseeko(x, y, z) fseeko64(x, y, z)
34 #define ftello(x) ftello64(x)
36 #define fseeko(x, y, z) _fseeki64(x, y, z)
37 #define ftello(x) _ftelli64(x)
40 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
42 #define BE_16(x) ((((uint8_t*)(x))[0] << 8) | ((uint8_t*)(x))[1])
44 #define BE_32(x) ((((uint8_t*)(x))[0] << 24) | \
45 (((uint8_t*)(x))[1] << 16) | \
46 (((uint8_t*)(x))[2] << 8) | \
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]))
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) )
64 #define QT_ATOM BE_FOURCC
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')
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')
81 #define ATOM_PREAMBLE_SIZE 8
82 #define COPY_BUFFER_SIZE 33554432
84 int main(int argc, char *argv[])
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;
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;
98 uint32_t offset_count;
99 uint64_t current_offset;
100 uint64_t start_offset = 0;
101 unsigned char *copy_buffer = NULL;
105 printf("Usage: qt-faststart <infile.mov> <outfile.mov>\n");
109 if (!strcmp(argv[1], argv[2])) {
110 fprintf(stderr, "input and output files need to be different\n");
114 infile = fopen(argv[1], "rb");
120 /* traverse through the atoms in the file to make sure that 'moov' is
122 while (!feof(infile)) {
123 if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
126 atom_size = (uint32_t) BE_32(&atom_bytes[0]);
127 atom_type = BE_32(&atom_bytes[4]);
130 if (atom_type == FTYP_ATOM) {
131 ftyp_atom_size = atom_size;
133 ftyp_atom = malloc(ftyp_atom_size);
135 printf("could not allocate %"PRIu64" bytes for ftyp atom\n",
139 fseeko(infile, -ATOM_PREAMBLE_SIZE, SEEK_CUR);
140 if (fread(ftyp_atom, atom_size, 1, infile) != 1) {
144 start_offset = ftello(infile);
146 /* 64-bit special case */
147 if (atom_size == 1) {
148 if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
151 atom_size = BE_64(&atom_bytes[0]);
152 fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE * 2, SEEK_CUR);
154 fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE, SEEK_CUR);
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,
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");
177 atom_offset += atom_size;
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. */
186 if (atom_type != MOOV_ATOM) {
187 printf("last atom in file was not a moov atom\n");
193 /* moov atom was, in fact, the last atom in the chunk; load the whole
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);
200 printf("could not allocate %"PRIu64" bytes for moov atom\n", atom_size);
203 if (fread(moov_atom, atom_size, 1, infile) != 1) {
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");
215 /* close; will be re-opened later */
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");
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;
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");
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;
263 /* re-open the input file and open the output file */
264 infile = fopen(argv[1], "rb");
270 if (start_offset > 0) { /* seek after ftyp atom */
271 fseeko(infile, start_offset, SEEK_SET);
272 last_offset -= start_offset;
275 outfile = fopen(argv[2], "wb");
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) {
290 /* dump the new moov atom */
291 printf(" writing moov atom...\n");
292 if (fwrite(moov_atom, moov_atom_size, 1, outfile) != 1) {
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);
301 printf("could not allocate %d bytes for copy_buffer\n", bytes_to_copy);
304 printf(" copying rest of file...\n");
305 while (last_offset) {
306 bytes_to_copy = FFMIN(bytes_to_copy, last_offset);
308 if (fread(copy_buffer, bytes_to_copy, 1, infile) != 1) {
312 if (fwrite(copy_buffer, bytes_to_copy, 1, outfile) != 1) {
316 last_offset -= bytes_to_copy;