tools/qt-faststart.c
38100e8c
 /*
31457d7a
  * qt-faststart.c, v0.2
38100e8c
  * by Mike Melanson (melanson@pcisys.net)
  * This file is placed in the public domain. Use the program however you
  * see fit.
  *
  * This utility rearranges a Quicktime file such that the moov atom
  * is in front of the data, thus facilitating network streaming.
  *
3c16e03d
  * To compile this program, start from the base directory from which you
  * are building FFmpeg and type:
  *  make tools/qt-faststart
  * The qt-faststart program will be built in the tools/ directory. If you
  * do not build the program in this manner, correct results are not
  * guaranteed, particularly on 64-bit platforms.
38100e8c
  * Invoke the program with:
  *  qt-faststart <infile.mov> <outfile.mov>
  *
  * Notes: Quicktime files can come in many configurations of top-level
  * atoms. This utility stipulates that the very last atom in the file needs
  * to be a moov atom. When given such a file, this utility will rearrange
  * the top-level atoms by shifting the moov atom from the back of the file
  * to the front, and patch the chunk offsets along the way. This utility
  * presently only operates on uncompressed moov atoms.
  */
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <inttypes.h>
91a4abd8
 #include <string.h>
38100e8c
 
a8fcaf40
 #ifdef __MINGW32__
3526ab89
 #undef fseeko
cd8d8457
 #define fseeko(x, y, z) fseeko64(x, y, z)
3526ab89
 #undef ftello
cd8d8457
 #define ftello(x)       ftello64(x)
dd4169ab
 #elif defined(_WIN32)
3526ab89
 #undef fseeko
dd4169ab
 #define fseeko(x, y, z) _fseeki64(x, y, z)
3526ab89
 #undef ftello
dd4169ab
 #define ftello(x)       _ftelli64(x)
a8fcaf40
 #endif
 
ea15a9a5
 #define MIN(a,b) ((a) > (b) ? (b) : (a))
610efb67
 
cd8d8457
 #define BE_16(x) ((((uint8_t*)(x))[0] <<  8) | ((uint8_t*)(x))[1])
 
ea7f79f9
 #define BE_32(x) (((uint32_t)(((uint8_t*)(x))[0]) << 24) |  \
                              (((uint8_t*)(x))[1]  << 16) |  \
                              (((uint8_t*)(x))[2]  <<  8) |  \
                               ((uint8_t*)(x))[3])
cd8d8457
 
 #define BE_64(x) (((uint64_t)(((uint8_t*)(x))[0]) << 56) |  \
                   ((uint64_t)(((uint8_t*)(x))[1]) << 48) |  \
                   ((uint64_t)(((uint8_t*)(x))[2]) << 40) |  \
                   ((uint64_t)(((uint8_t*)(x))[3]) << 32) |  \
                   ((uint64_t)(((uint8_t*)(x))[4]) << 24) |  \
                   ((uint64_t)(((uint8_t*)(x))[5]) << 16) |  \
                   ((uint64_t)(((uint8_t*)(x))[6]) <<  8) |  \
                   ((uint64_t)( (uint8_t*)(x))[7]))
 
 #define BE_FOURCC(ch0, ch1, ch2, ch3)           \
     ( (uint32_t)(unsigned char)(ch3)        |   \
      ((uint32_t)(unsigned char)(ch2) <<  8) |   \
      ((uint32_t)(unsigned char)(ch1) << 16) |   \
      ((uint32_t)(unsigned char)(ch0) << 24) )
38100e8c
 
 #define QT_ATOM BE_FOURCC
 /* top level atoms */
 #define FREE_ATOM QT_ATOM('f', 'r', 'e', 'e')
 #define JUNK_ATOM QT_ATOM('j', 'u', 'n', 'k')
 #define MDAT_ATOM QT_ATOM('m', 'd', 'a', 't')
 #define MOOV_ATOM QT_ATOM('m', 'o', 'o', 'v')
 #define PNOT_ATOM QT_ATOM('p', 'n', 'o', 't')
 #define SKIP_ATOM QT_ATOM('s', 'k', 'i', 'p')
 #define WIDE_ATOM QT_ATOM('w', 'i', 'd', 'e')
 #define PICT_ATOM QT_ATOM('P', 'I', 'C', 'T')
 #define FTYP_ATOM QT_ATOM('f', 't', 'y', 'p')
31457d7a
 #define UUID_ATOM QT_ATOM('u', 'u', 'i', 'd')
38100e8c
 
 #define CMOV_ATOM QT_ATOM('c', 'm', 'o', 'v')
 #define STCO_ATOM QT_ATOM('s', 't', 'c', 'o')
 #define CO64_ATOM QT_ATOM('c', 'o', '6', '4')
 
cd8d8457
 #define ATOM_PREAMBLE_SIZE    8
f4d9148f
 #define COPY_BUFFER_SIZE   33554432
38100e8c
 
 int main(int argc, char *argv[])
 {
c937454d
     FILE *infile  = NULL;
     FILE *outfile = NULL;
38100e8c
     unsigned char atom_bytes[ATOM_PREAMBLE_SIZE];
cd8d8457
     uint32_t atom_type   = 0;
     uint64_t atom_size   = 0;
65b875d8
     uint64_t atom_offset = 0;
03c2a66f
     int64_t last_offset;
c937454d
     unsigned char *moov_atom = NULL;
2ef9fd8e
     unsigned char *ftyp_atom = NULL;
38100e8c
     uint64_t moov_atom_size;
60a9cc58
     uint64_t ftyp_atom_size = 0;
38100e8c
     uint64_t i, j;
     uint32_t offset_count;
     uint64_t current_offset;
18389613
     int64_t start_offset = 0;
f4d9148f
     unsigned char *copy_buffer = NULL;
38100e8c
     int bytes_to_copy;
 
     if (argc != 3) {
2574d62d
         printf("Usage: qt-faststart <infile.mov> <outfile.mov>\n"
                "Note: alternatively you can use -movflags +faststart in ffmpeg\n");
38100e8c
         return 0;
     }
 
91a4abd8
     if (!strcmp(argv[1], argv[2])) {
         fprintf(stderr, "input and output files need to be different\n");
         return 1;
     }
 
38100e8c
     infile = fopen(argv[1], "rb");
     if (!infile) {
         perror(argv[1]);
c937454d
         goto error_out;
38100e8c
     }
 
     /* traverse through the atoms in the file to make sure that 'moov' is
      * at the end */
     while (!feof(infile)) {
         if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
             break;
         }
ea7f79f9
         atom_size = BE_32(&atom_bytes[0]);
38100e8c
         atom_type = BE_32(&atom_bytes[4]);
 
60a9cc58
         /* keep ftyp atom */
         if (atom_type == FTYP_ATOM) {
             ftyp_atom_size = atom_size;
6ad533b7
             free(ftyp_atom);
60a9cc58
             ftyp_atom = malloc(ftyp_atom_size);
             if (!ftyp_atom) {
cd8d8457
                 printf("could not allocate %"PRIu64" bytes for ftyp atom\n",
                        atom_size);
c937454d
                 goto error_out;
60a9cc58
             }
56122443
             if (fseeko(infile, -ATOM_PREAMBLE_SIZE, SEEK_CUR) ||
11c4bc9a
                 fread(ftyp_atom, atom_size, 1, infile) != 1 ||
63848854
                 (start_offset = ftello(infile)) < 0) {
60a9cc58
                 perror(argv[1]);
c937454d
                 goto error_out;
60a9cc58
             }
65b875d8
         } else {
0de41ead
             int ret;
cf4afe0b
             /* 64-bit special case */
             if (atom_size == 1) {
                 if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
                     break;
                 }
                 atom_size = BE_64(&atom_bytes[0]);
0de41ead
                 ret = fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE * 2, SEEK_CUR);
cf4afe0b
             } else {
0de41ead
                 ret = fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE, SEEK_CUR);
             }
56122443
             if (ret) {
0de41ead
                 perror(argv[1]);
                 goto error_out;
38100e8c
             }
         }
65b875d8
         printf("%c%c%c%c %10"PRIu64" %"PRIu64"\n",
                (atom_type >> 24) & 255,
                (atom_type >> 16) & 255,
                (atom_type >>  8) & 255,
                (atom_type >>  0) & 255,
                atom_offset,
                atom_size);
         if ((atom_type != FREE_ATOM) &&
             (atom_type != JUNK_ATOM) &&
             (atom_type != MDAT_ATOM) &&
             (atom_type != MOOV_ATOM) &&
             (atom_type != PNOT_ATOM) &&
             (atom_type != SKIP_ATOM) &&
             (atom_type != WIDE_ATOM) &&
             (atom_type != PICT_ATOM) &&
             (atom_type != UUID_ATOM) &&
             (atom_type != FTYP_ATOM)) {
cd8d8457
             printf("encountered non-QT top-level atom (is this a QuickTime file?)\n");
65b875d8
             break;
         }
         atom_offset += atom_size;
86e09922
 
         /* The atom header is 8 (or 16 bytes), if the atom size (which
          * includes these 8 or 16 bytes) is less than that, we won't be
          * able to continue scanning sensibly after this atom, so break. */
         if (atom_size < 8)
             break;
65b875d8
     }
38100e8c
 
     if (atom_type != MOOV_ATOM) {
cd8d8457
         printf("last atom in file was not a moov atom\n");
d296a658
         free(ftyp_atom);
38100e8c
         fclose(infile);
         return 0;
     }
 
     /* moov atom was, in fact, the last atom in the chunk; load the whole
      * moov atom */
0de41ead
     if (fseeko(infile, -atom_size, SEEK_END)) {
         perror(argv[1]);
         goto error_out;
     }
cd8d8457
     last_offset    = ftello(infile);
63848854
     if (last_offset < 0) {
         perror(argv[1]);
         goto error_out;
     }
38100e8c
     moov_atom_size = atom_size;
cd8d8457
     moov_atom      = malloc(moov_atom_size);
38100e8c
     if (!moov_atom) {
cd8d8457
         printf("could not allocate %"PRIu64" bytes for moov atom\n", atom_size);
c937454d
         goto error_out;
38100e8c
     }
     if (fread(moov_atom, atom_size, 1, infile) != 1) {
         perror(argv[1]);
c937454d
         goto error_out;
38100e8c
     }
 
     /* this utility does not support compressed atoms yet, so disqualify
      * files with compressed QT atoms */
     if (BE_32(&moov_atom[12]) == CMOV_ATOM) {
cd8d8457
         printf("this utility does not support compressed moov atoms yet\n");
c937454d
         goto error_out;
38100e8c
     }
 
     /* close; will be re-opened later */
     fclose(infile);
c937454d
     infile = NULL;
38100e8c
 
     /* crawl through the moov chunk in search of stco or co64 atoms */
     for (i = 4; i < moov_atom_size - 4; i++) {
         atom_type = BE_32(&moov_atom[i]);
         if (atom_type == STCO_ATOM) {
cd8d8457
             printf(" patching stco atom...\n");
38100e8c
             atom_size = BE_32(&moov_atom[i - 4]);
             if (i + atom_size - 4 > moov_atom_size) {
cd8d8457
                 printf(" bad atom size\n");
c937454d
                 goto error_out;
38100e8c
             }
             offset_count = BE_32(&moov_atom[i + 8]);
bb95334c
             if (i + 12 + offset_count * UINT64_C(4) > moov_atom_size) {
                 printf(" bad atom size/element count\n");
0ea47423
                 goto error_out;
             }
38100e8c
             for (j = 0; j < offset_count; j++) {
cd8d8457
                 current_offset  = BE_32(&moov_atom[i + 12 + j * 4]);
38100e8c
                 current_offset += moov_atom_size;
                 moov_atom[i + 12 + j * 4 + 0] = (current_offset >> 24) & 0xFF;
                 moov_atom[i + 12 + j * 4 + 1] = (current_offset >> 16) & 0xFF;
                 moov_atom[i + 12 + j * 4 + 2] = (current_offset >>  8) & 0xFF;
                 moov_atom[i + 12 + j * 4 + 3] = (current_offset >>  0) & 0xFF;
             }
             i += atom_size - 4;
         } else if (atom_type == CO64_ATOM) {
cd8d8457
             printf(" patching co64 atom...\n");
38100e8c
             atom_size = BE_32(&moov_atom[i - 4]);
             if (i + atom_size - 4 > moov_atom_size) {
cd8d8457
                 printf(" bad atom size\n");
c937454d
                 goto error_out;
38100e8c
             }
             offset_count = BE_32(&moov_atom[i + 8]);
bb95334c
             if (i + 12 + offset_count * UINT64_C(8) > moov_atom_size) {
                 printf(" bad atom size/element count\n");
0ea47423
                 goto error_out;
             }
38100e8c
             for (j = 0; j < offset_count; j++) {
cd8d8457
                 current_offset  = BE_64(&moov_atom[i + 12 + j * 8]);
38100e8c
                 current_offset += moov_atom_size;
                 moov_atom[i + 12 + j * 8 + 0] = (current_offset >> 56) & 0xFF;
                 moov_atom[i + 12 + j * 8 + 1] = (current_offset >> 48) & 0xFF;
                 moov_atom[i + 12 + j * 8 + 2] = (current_offset >> 40) & 0xFF;
                 moov_atom[i + 12 + j * 8 + 3] = (current_offset >> 32) & 0xFF;
                 moov_atom[i + 12 + j * 8 + 4] = (current_offset >> 24) & 0xFF;
                 moov_atom[i + 12 + j * 8 + 5] = (current_offset >> 16) & 0xFF;
                 moov_atom[i + 12 + j * 8 + 6] = (current_offset >>  8) & 0xFF;
                 moov_atom[i + 12 + j * 8 + 7] = (current_offset >>  0) & 0xFF;
             }
             i += atom_size - 4;
         }
     }
 
     /* re-open the input file and open the output file */
     infile = fopen(argv[1], "rb");
     if (!infile) {
         perror(argv[1]);
c937454d
         goto error_out;
38100e8c
     }
fd7789db
 
     if (start_offset > 0) { /* seek after ftyp atom */
0de41ead
         if (fseeko(infile, start_offset, SEEK_SET)) {
             perror(argv[1]);
             goto error_out;
         }
 
fd7789db
         last_offset -= start_offset;
     }
60a9cc58
 
38100e8c
     outfile = fopen(argv[2], "wb");
     if (!outfile) {
         perror(argv[2]);
c937454d
         goto error_out;
38100e8c
     }
 
60a9cc58
     /* dump the same ftyp atom */
     if (ftyp_atom_size > 0) {
cd8d8457
         printf(" writing ftyp atom...\n");
60a9cc58
         if (fwrite(ftyp_atom, ftyp_atom_size, 1, outfile) != 1) {
             perror(argv[2]);
             goto error_out;
         }
     }
 
38100e8c
     /* dump the new moov atom */
cd8d8457
     printf(" writing moov atom...\n");
38100e8c
     if (fwrite(moov_atom, moov_atom_size, 1, outfile) != 1) {
         perror(argv[2]);
         goto error_out;
     }
 
     /* copy the remainder of the infile, from offset 0 -> last_offset - 1 */
0bb474f6
     bytes_to_copy = MIN(COPY_BUFFER_SIZE, last_offset);
610efb67
     copy_buffer = malloc(bytes_to_copy);
f4d9148f
     if (!copy_buffer) {
92c3173c
         printf("could not allocate %d bytes for copy_buffer\n", bytes_to_copy);
f4d9148f
         goto error_out;
     }
cd8d8457
     printf(" copying rest of file...\n");
38100e8c
     while (last_offset) {
0bb474f6
         bytes_to_copy = MIN(bytes_to_copy, last_offset);
38100e8c
 
         if (fread(copy_buffer, bytes_to_copy, 1, infile) != 1) {
             perror(argv[1]);
             goto error_out;
         }
         if (fwrite(copy_buffer, bytes_to_copy, 1, outfile) != 1) {
             perror(argv[2]);
             goto error_out;
         }
         last_offset -= bytes_to_copy;
     }
 
     fclose(infile);
     fclose(outfile);
     free(moov_atom);
331cb6c3
     free(ftyp_atom);
f4d9148f
     free(copy_buffer);
38100e8c
 
     return 0;
 
 error_out:
c937454d
     if (infile)
e3d7269f
         fclose(infile);
c937454d
     if (outfile)
e3d7269f
         fclose(outfile);
38100e8c
     free(moov_atom);
331cb6c3
     free(ftyp_atom);
f4d9148f
     free(copy_buffer);
38100e8c
     return 1;
 }