FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
qt-faststart.c
Go to the documentation of this file.
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 #undef fseeko
34 #define fseeko(x, y, z) fseeko64(x, y, z)
35 #undef ftello
36 #define ftello(x) ftello64(x)
37 #elif defined(_WIN32)
38 #undef fseeko
39 #define fseeko(x, y, z) _fseeki64(x, y, z)
40 #undef ftello
41 #define ftello(x) _ftelli64(x)
42 #endif
43 
44 #define MIN(a,b) ((a) > (b) ? (b) : (a))
45 
46 #define BE_16(x) ((((uint8_t*)(x))[0] << 8) | ((uint8_t*)(x))[1])
47 
48 #define BE_32(x) (((uint32_t)(((uint8_t*)(x))[0]) << 24) | \
49  (((uint8_t*)(x))[1] << 16) | \
50  (((uint8_t*)(x))[2] << 8) | \
51  ((uint8_t*)(x))[3])
52 
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]))
61 
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) )
67 
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')
80 
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')
84 
85 #define ATOM_PREAMBLE_SIZE 8
86 #define COPY_BUFFER_SIZE 33554432
87 
88 int main(int argc, char *argv[])
89 {
90  FILE *infile = NULL;
91  FILE *outfile = NULL;
92  unsigned char atom_bytes[ATOM_PREAMBLE_SIZE];
93  uint32_t atom_type = 0;
94  uint64_t atom_size = 0;
95  uint64_t atom_offset = 0;
96  int64_t last_offset;
97  unsigned char *moov_atom = NULL;
98  unsigned char *ftyp_atom = NULL;
99  uint64_t moov_atom_size;
100  uint64_t ftyp_atom_size = 0;
101  uint64_t i, j;
102  uint32_t offset_count;
103  uint64_t current_offset;
104  int64_t start_offset = 0;
105  unsigned char *copy_buffer = NULL;
106  int bytes_to_copy;
107 
108  if (argc != 3) {
109  printf("Usage: qt-faststart <infile.mov> <outfile.mov>\n"
110  "Note: alternatively you can use -movflags +faststart in ffmpeg\n");
111  return 0;
112  }
113 
114  if (!strcmp(argv[1], argv[2])) {
115  fprintf(stderr, "input and output files need to be different\n");
116  return 1;
117  }
118 
119  infile = fopen(argv[1], "rb");
120  if (!infile) {
121  perror(argv[1]);
122  goto error_out;
123  }
124 
125  /* traverse through the atoms in the file to make sure that 'moov' is
126  * at the end */
127  while (!feof(infile)) {
128  if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
129  break;
130  }
131  atom_size = BE_32(&atom_bytes[0]);
132  atom_type = BE_32(&atom_bytes[4]);
133 
134  /* keep ftyp atom */
135  if (atom_type == FTYP_ATOM) {
136  ftyp_atom_size = atom_size;
137  free(ftyp_atom);
138  ftyp_atom = malloc(ftyp_atom_size);
139  if (!ftyp_atom) {
140  printf("could not allocate %"PRIu64" bytes for ftyp atom\n",
141  atom_size);
142  goto error_out;
143  }
144  if (fseeko(infile, -ATOM_PREAMBLE_SIZE, SEEK_CUR) ||
145  fread(ftyp_atom, atom_size, 1, infile) != 1 ||
146  (start_offset = ftello(infile)) < 0) {
147  perror(argv[1]);
148  goto error_out;
149  }
150  } else {
151  int ret;
152  /* 64-bit special case */
153  if (atom_size == 1) {
154  if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
155  break;
156  }
157  atom_size = BE_64(&atom_bytes[0]);
158  ret = fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE * 2, SEEK_CUR);
159  } else {
160  ret = fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE, SEEK_CUR);
161  }
162  if (ret) {
163  perror(argv[1]);
164  goto error_out;
165  }
166  }
167  printf("%c%c%c%c %10"PRIu64" %"PRIu64"\n",
168  (atom_type >> 24) & 255,
169  (atom_type >> 16) & 255,
170  (atom_type >> 8) & 255,
171  (atom_type >> 0) & 255,
172  atom_offset,
173  atom_size);
174  if ((atom_type != FREE_ATOM) &&
175  (atom_type != JUNK_ATOM) &&
176  (atom_type != MDAT_ATOM) &&
177  (atom_type != MOOV_ATOM) &&
178  (atom_type != PNOT_ATOM) &&
179  (atom_type != SKIP_ATOM) &&
180  (atom_type != WIDE_ATOM) &&
181  (atom_type != PICT_ATOM) &&
182  (atom_type != UUID_ATOM) &&
183  (atom_type != FTYP_ATOM)) {
184  printf("encountered non-QT top-level atom (is this a QuickTime file?)\n");
185  break;
186  }
187  atom_offset += atom_size;
188 
189  /* The atom header is 8 (or 16 bytes), if the atom size (which
190  * includes these 8 or 16 bytes) is less than that, we won't be
191  * able to continue scanning sensibly after this atom, so break. */
192  if (atom_size < 8)
193  break;
194  }
195 
196  if (atom_type != MOOV_ATOM) {
197  printf("last atom in file was not a moov atom\n");
198  free(ftyp_atom);
199  fclose(infile);
200  return 0;
201  }
202 
203  /* moov atom was, in fact, the last atom in the chunk; load the whole
204  * moov atom */
205  if (fseeko(infile, -atom_size, SEEK_END)) {
206  perror(argv[1]);
207  goto error_out;
208  }
209  last_offset = ftello(infile);
210  if (last_offset < 0) {
211  perror(argv[1]);
212  goto error_out;
213  }
214  moov_atom_size = atom_size;
215  moov_atom = malloc(moov_atom_size);
216  if (!moov_atom) {
217  printf("could not allocate %"PRIu64" bytes for moov atom\n", atom_size);
218  goto error_out;
219  }
220  if (fread(moov_atom, atom_size, 1, infile) != 1) {
221  perror(argv[1]);
222  goto error_out;
223  }
224 
225  /* this utility does not support compressed atoms yet, so disqualify
226  * files with compressed QT atoms */
227  if (BE_32(&moov_atom[12]) == CMOV_ATOM) {
228  printf("this utility does not support compressed moov atoms yet\n");
229  goto error_out;
230  }
231 
232  /* close; will be re-opened later */
233  fclose(infile);
234  infile = NULL;
235 
236  /* crawl through the moov chunk in search of stco or co64 atoms */
237  for (i = 4; i < moov_atom_size - 4; i++) {
238  atom_type = BE_32(&moov_atom[i]);
239  if (atom_type == STCO_ATOM) {
240  printf(" patching stco 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  if (i + 12 + offset_count * UINT64_C(4) > moov_atom_size) {
248  printf(" bad atom size/element count\n");
249  goto error_out;
250  }
251  for (j = 0; j < offset_count; j++) {
252  current_offset = BE_32(&moov_atom[i + 12 + j * 4]);
253  current_offset += moov_atom_size;
254  moov_atom[i + 12 + j * 4 + 0] = (current_offset >> 24) & 0xFF;
255  moov_atom[i + 12 + j * 4 + 1] = (current_offset >> 16) & 0xFF;
256  moov_atom[i + 12 + j * 4 + 2] = (current_offset >> 8) & 0xFF;
257  moov_atom[i + 12 + j * 4 + 3] = (current_offset >> 0) & 0xFF;
258  }
259  i += atom_size - 4;
260  } else if (atom_type == CO64_ATOM) {
261  printf(" patching co64 atom...\n");
262  atom_size = BE_32(&moov_atom[i - 4]);
263  if (i + atom_size - 4 > moov_atom_size) {
264  printf(" bad atom size\n");
265  goto error_out;
266  }
267  offset_count = BE_32(&moov_atom[i + 8]);
268  if (i + 12 + offset_count * UINT64_C(8) > moov_atom_size) {
269  printf(" bad atom size/element count\n");
270  goto error_out;
271  }
272  for (j = 0; j < offset_count; j++) {
273  current_offset = BE_64(&moov_atom[i + 12 + j * 8]);
274  current_offset += moov_atom_size;
275  moov_atom[i + 12 + j * 8 + 0] = (current_offset >> 56) & 0xFF;
276  moov_atom[i + 12 + j * 8 + 1] = (current_offset >> 48) & 0xFF;
277  moov_atom[i + 12 + j * 8 + 2] = (current_offset >> 40) & 0xFF;
278  moov_atom[i + 12 + j * 8 + 3] = (current_offset >> 32) & 0xFF;
279  moov_atom[i + 12 + j * 8 + 4] = (current_offset >> 24) & 0xFF;
280  moov_atom[i + 12 + j * 8 + 5] = (current_offset >> 16) & 0xFF;
281  moov_atom[i + 12 + j * 8 + 6] = (current_offset >> 8) & 0xFF;
282  moov_atom[i + 12 + j * 8 + 7] = (current_offset >> 0) & 0xFF;
283  }
284  i += atom_size - 4;
285  }
286  }
287 
288  /* re-open the input file and open the output file */
289  infile = fopen(argv[1], "rb");
290  if (!infile) {
291  perror(argv[1]);
292  goto error_out;
293  }
294 
295  if (start_offset > 0) { /* seek after ftyp atom */
296  if (fseeko(infile, start_offset, SEEK_SET)) {
297  perror(argv[1]);
298  goto error_out;
299  }
300 
301  last_offset -= start_offset;
302  }
303 
304  outfile = fopen(argv[2], "wb");
305  if (!outfile) {
306  perror(argv[2]);
307  goto error_out;
308  }
309 
310  /* dump the same ftyp atom */
311  if (ftyp_atom_size > 0) {
312  printf(" writing ftyp atom...\n");
313  if (fwrite(ftyp_atom, ftyp_atom_size, 1, outfile) != 1) {
314  perror(argv[2]);
315  goto error_out;
316  }
317  }
318 
319  /* dump the new moov atom */
320  printf(" writing moov atom...\n");
321  if (fwrite(moov_atom, moov_atom_size, 1, outfile) != 1) {
322  perror(argv[2]);
323  goto error_out;
324  }
325 
326  /* copy the remainder of the infile, from offset 0 -> last_offset - 1 */
327  bytes_to_copy = MIN(COPY_BUFFER_SIZE, last_offset);
328  copy_buffer = malloc(bytes_to_copy);
329  if (!copy_buffer) {
330  printf("could not allocate %d bytes for copy_buffer\n", bytes_to_copy);
331  goto error_out;
332  }
333  printf(" copying rest of file...\n");
334  while (last_offset) {
335  bytes_to_copy = MIN(bytes_to_copy, last_offset);
336 
337  if (fread(copy_buffer, bytes_to_copy, 1, infile) != 1) {
338  perror(argv[1]);
339  goto error_out;
340  }
341  if (fwrite(copy_buffer, bytes_to_copy, 1, outfile) != 1) {
342  perror(argv[2]);
343  goto error_out;
344  }
345  last_offset -= bytes_to_copy;
346  }
347 
348  fclose(infile);
349  fclose(outfile);
350  free(moov_atom);
351  free(ftyp_atom);
352  free(copy_buffer);
353 
354  return 0;
355 
356 error_out:
357  if (infile)
358  fclose(infile);
359  if (outfile)
360  fclose(outfile);
361  free(moov_atom);
362  free(ftyp_atom);
363  free(copy_buffer);
364  return 1;
365 }