tools/qt-faststart.c
Go to the documentation of this file.
00001 /*
00002  * qt-faststart.c, v0.2
00003  * by Mike Melanson (melanson@pcisys.net)
00004  * This file is placed in the public domain. Use the program however you
00005  * see fit.
00006  *
00007  * This utility rearranges a Quicktime file such that the moov atom
00008  * is in front of the data, thus facilitating network streaming.
00009  *
00010  * To compile this program, start from the base directory from which you
00011  * are building FFmpeg and type:
00012  *  make tools/qt-faststart
00013  * The qt-faststart program will be built in the tools/ directory. If you
00014  * do not build the program in this manner, correct results are not
00015  * guaranteed, particularly on 64-bit platforms.
00016  * Invoke the program with:
00017  *  qt-faststart <infile.mov> <outfile.mov>
00018  *
00019  * Notes: Quicktime files can come in many configurations of top-level
00020  * atoms. This utility stipulates that the very last atom in the file needs
00021  * to be a moov atom. When given such a file, this utility will rearrange
00022  * the top-level atoms by shifting the moov atom from the back of the file
00023  * to the front, and patch the chunk offsets along the way. This utility
00024  * presently only operates on uncompressed moov atoms.
00025  */
00026 
00027 #include <stdio.h>
00028 #include <stdlib.h>
00029 #include <inttypes.h>
00030 #include <string.h>
00031 
00032 #ifdef __MINGW32__
00033 #define fseeko(x, y, z) fseeko64(x, y, z)
00034 #define ftello(x)       ftello64(x)
00035 #endif
00036 
00037 #define BE_16(x) ((((uint8_t*)(x))[0] <<  8) | ((uint8_t*)(x))[1])
00038 
00039 #define BE_32(x) ((((uint8_t*)(x))[0] << 24) |  \
00040                   (((uint8_t*)(x))[1] << 16) |  \
00041                   (((uint8_t*)(x))[2] <<  8) |  \
00042                    ((uint8_t*)(x))[3])
00043 
00044 #define BE_64(x) (((uint64_t)(((uint8_t*)(x))[0]) << 56) |  \
00045                   ((uint64_t)(((uint8_t*)(x))[1]) << 48) |  \
00046                   ((uint64_t)(((uint8_t*)(x))[2]) << 40) |  \
00047                   ((uint64_t)(((uint8_t*)(x))[3]) << 32) |  \
00048                   ((uint64_t)(((uint8_t*)(x))[4]) << 24) |  \
00049                   ((uint64_t)(((uint8_t*)(x))[5]) << 16) |  \
00050                   ((uint64_t)(((uint8_t*)(x))[6]) <<  8) |  \
00051                   ((uint64_t)( (uint8_t*)(x))[7]))
00052 
00053 #define BE_FOURCC(ch0, ch1, ch2, ch3)           \
00054     ( (uint32_t)(unsigned char)(ch3)        |   \
00055      ((uint32_t)(unsigned char)(ch2) <<  8) |   \
00056      ((uint32_t)(unsigned char)(ch1) << 16) |   \
00057      ((uint32_t)(unsigned char)(ch0) << 24) )
00058 
00059 #define QT_ATOM BE_FOURCC
00060 /* top level atoms */
00061 #define FREE_ATOM QT_ATOM('f', 'r', 'e', 'e')
00062 #define JUNK_ATOM QT_ATOM('j', 'u', 'n', 'k')
00063 #define MDAT_ATOM QT_ATOM('m', 'd', 'a', 't')
00064 #define MOOV_ATOM QT_ATOM('m', 'o', 'o', 'v')
00065 #define PNOT_ATOM QT_ATOM('p', 'n', 'o', 't')
00066 #define SKIP_ATOM QT_ATOM('s', 'k', 'i', 'p')
00067 #define WIDE_ATOM QT_ATOM('w', 'i', 'd', 'e')
00068 #define PICT_ATOM QT_ATOM('P', 'I', 'C', 'T')
00069 #define FTYP_ATOM QT_ATOM('f', 't', 'y', 'p')
00070 #define UUID_ATOM QT_ATOM('u', 'u', 'i', 'd')
00071 
00072 #define CMOV_ATOM QT_ATOM('c', 'm', 'o', 'v')
00073 #define STCO_ATOM QT_ATOM('s', 't', 'c', 'o')
00074 #define CO64_ATOM QT_ATOM('c', 'o', '6', '4')
00075 
00076 #define ATOM_PREAMBLE_SIZE    8
00077 #define COPY_BUFFER_SIZE   1024
00078 
00079 int main(int argc, char *argv[])
00080 {
00081     FILE *infile  = NULL;
00082     FILE *outfile = NULL;
00083     unsigned char atom_bytes[ATOM_PREAMBLE_SIZE];
00084     uint32_t atom_type   = 0;
00085     uint64_t atom_size   = 0;
00086     uint64_t atom_offset = 0;
00087     uint64_t last_offset;
00088     unsigned char *moov_atom = NULL;
00089     unsigned char *ftyp_atom = NULL;
00090     uint64_t moov_atom_size;
00091     uint64_t ftyp_atom_size = 0;
00092     uint64_t i, j;
00093     uint32_t offset_count;
00094     uint64_t current_offset;
00095     uint64_t start_offset = 0;
00096     unsigned char copy_buffer[COPY_BUFFER_SIZE];
00097     int bytes_to_copy;
00098 
00099     if (argc != 3) {
00100         printf("Usage: qt-faststart <infile.mov> <outfile.mov>\n");
00101         return 0;
00102     }
00103 
00104     if (!strcmp(argv[1], argv[2])) {
00105         fprintf(stderr, "input and output files need to be different\n");
00106         return 1;
00107     }
00108 
00109     infile = fopen(argv[1], "rb");
00110     if (!infile) {
00111         perror(argv[1]);
00112         goto error_out;
00113     }
00114 
00115     /* traverse through the atoms in the file to make sure that 'moov' is
00116      * at the end */
00117     while (!feof(infile)) {
00118         if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
00119             break;
00120         }
00121         atom_size = (uint32_t) BE_32(&atom_bytes[0]);
00122         atom_type = BE_32(&atom_bytes[4]);
00123 
00124         /* keep ftyp atom */
00125         if (atom_type == FTYP_ATOM) {
00126             ftyp_atom_size = atom_size;
00127             free(ftyp_atom);
00128             ftyp_atom = malloc(ftyp_atom_size);
00129             if (!ftyp_atom) {
00130                 printf("could not allocate %"PRIu64" bytes for ftyp atom\n",
00131                        atom_size);
00132                 goto error_out;
00133             }
00134             fseeko(infile, -ATOM_PREAMBLE_SIZE, SEEK_CUR);
00135             if (fread(ftyp_atom, atom_size, 1, infile) != 1) {
00136                 perror(argv[1]);
00137                 goto error_out;
00138             }
00139             start_offset = ftello(infile);
00140         } else {
00141             /* 64-bit special case */
00142             if (atom_size == 1) {
00143                 if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
00144                     break;
00145                 }
00146                 atom_size = BE_64(&atom_bytes[0]);
00147                 fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE * 2, SEEK_CUR);
00148             } else {
00149                 fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE, SEEK_CUR);
00150             }
00151         }
00152         printf("%c%c%c%c %10"PRIu64" %"PRIu64"\n",
00153                (atom_type >> 24) & 255,
00154                (atom_type >> 16) & 255,
00155                (atom_type >>  8) & 255,
00156                (atom_type >>  0) & 255,
00157                atom_offset,
00158                atom_size);
00159         if ((atom_type != FREE_ATOM) &&
00160             (atom_type != JUNK_ATOM) &&
00161             (atom_type != MDAT_ATOM) &&
00162             (atom_type != MOOV_ATOM) &&
00163             (atom_type != PNOT_ATOM) &&
00164             (atom_type != SKIP_ATOM) &&
00165             (atom_type != WIDE_ATOM) &&
00166             (atom_type != PICT_ATOM) &&
00167             (atom_type != UUID_ATOM) &&
00168             (atom_type != FTYP_ATOM)) {
00169             printf("encountered non-QT top-level atom (is this a QuickTime file?)\n");
00170             break;
00171         }
00172         atom_offset += atom_size;
00173 
00174         /* The atom header is 8 (or 16 bytes), if the atom size (which
00175          * includes these 8 or 16 bytes) is less than that, we won't be
00176          * able to continue scanning sensibly after this atom, so break. */
00177         if (atom_size < 8)
00178             break;
00179     }
00180 
00181     if (atom_type != MOOV_ATOM) {
00182         printf("last atom in file was not a moov atom\n");
00183         free(ftyp_atom);
00184         fclose(infile);
00185         return 0;
00186     }
00187 
00188     /* moov atom was, in fact, the last atom in the chunk; load the whole
00189      * moov atom */
00190     fseeko(infile, -atom_size, SEEK_END);
00191     last_offset    = ftello(infile);
00192     moov_atom_size = atom_size;
00193     moov_atom      = malloc(moov_atom_size);
00194     if (!moov_atom) {
00195         printf("could not allocate %"PRIu64" bytes for moov atom\n", atom_size);
00196         goto error_out;
00197     }
00198     if (fread(moov_atom, atom_size, 1, infile) != 1) {
00199         perror(argv[1]);
00200         goto error_out;
00201     }
00202 
00203     /* this utility does not support compressed atoms yet, so disqualify
00204      * files with compressed QT atoms */
00205     if (BE_32(&moov_atom[12]) == CMOV_ATOM) {
00206         printf("this utility does not support compressed moov atoms yet\n");
00207         goto error_out;
00208     }
00209 
00210     /* close; will be re-opened later */
00211     fclose(infile);
00212     infile = NULL;
00213 
00214     /* crawl through the moov chunk in search of stco or co64 atoms */
00215     for (i = 4; i < moov_atom_size - 4; i++) {
00216         atom_type = BE_32(&moov_atom[i]);
00217         if (atom_type == STCO_ATOM) {
00218             printf(" patching stco atom...\n");
00219             atom_size = BE_32(&moov_atom[i - 4]);
00220             if (i + atom_size - 4 > moov_atom_size) {
00221                 printf(" bad atom size\n");
00222                 goto error_out;
00223             }
00224             offset_count = BE_32(&moov_atom[i + 8]);
00225             for (j = 0; j < offset_count; j++) {
00226                 current_offset  = BE_32(&moov_atom[i + 12 + j * 4]);
00227                 current_offset += moov_atom_size;
00228                 moov_atom[i + 12 + j * 4 + 0] = (current_offset >> 24) & 0xFF;
00229                 moov_atom[i + 12 + j * 4 + 1] = (current_offset >> 16) & 0xFF;
00230                 moov_atom[i + 12 + j * 4 + 2] = (current_offset >>  8) & 0xFF;
00231                 moov_atom[i + 12 + j * 4 + 3] = (current_offset >>  0) & 0xFF;
00232             }
00233             i += atom_size - 4;
00234         } else if (atom_type == CO64_ATOM) {
00235             printf(" patching co64 atom...\n");
00236             atom_size = BE_32(&moov_atom[i - 4]);
00237             if (i + atom_size - 4 > moov_atom_size) {
00238                 printf(" bad atom size\n");
00239                 goto error_out;
00240             }
00241             offset_count = BE_32(&moov_atom[i + 8]);
00242             for (j = 0; j < offset_count; j++) {
00243                 current_offset  = BE_64(&moov_atom[i + 12 + j * 8]);
00244                 current_offset += moov_atom_size;
00245                 moov_atom[i + 12 + j * 8 + 0] = (current_offset >> 56) & 0xFF;
00246                 moov_atom[i + 12 + j * 8 + 1] = (current_offset >> 48) & 0xFF;
00247                 moov_atom[i + 12 + j * 8 + 2] = (current_offset >> 40) & 0xFF;
00248                 moov_atom[i + 12 + j * 8 + 3] = (current_offset >> 32) & 0xFF;
00249                 moov_atom[i + 12 + j * 8 + 4] = (current_offset >> 24) & 0xFF;
00250                 moov_atom[i + 12 + j * 8 + 5] = (current_offset >> 16) & 0xFF;
00251                 moov_atom[i + 12 + j * 8 + 6] = (current_offset >>  8) & 0xFF;
00252                 moov_atom[i + 12 + j * 8 + 7] = (current_offset >>  0) & 0xFF;
00253             }
00254             i += atom_size - 4;
00255         }
00256     }
00257 
00258     /* re-open the input file and open the output file */
00259     infile = fopen(argv[1], "rb");
00260     if (!infile) {
00261         perror(argv[1]);
00262         goto error_out;
00263     }
00264 
00265     if (start_offset > 0) { /* seek after ftyp atom */
00266         fseeko(infile, start_offset, SEEK_SET);
00267         last_offset -= start_offset;
00268     }
00269 
00270     outfile = fopen(argv[2], "wb");
00271     if (!outfile) {
00272         perror(argv[2]);
00273         goto error_out;
00274     }
00275 
00276     /* dump the same ftyp atom */
00277     if (ftyp_atom_size > 0) {
00278         printf(" writing ftyp atom...\n");
00279         if (fwrite(ftyp_atom, ftyp_atom_size, 1, outfile) != 1) {
00280             perror(argv[2]);
00281             goto error_out;
00282         }
00283     }
00284 
00285     /* dump the new moov atom */
00286     printf(" writing moov atom...\n");
00287     if (fwrite(moov_atom, moov_atom_size, 1, outfile) != 1) {
00288         perror(argv[2]);
00289         goto error_out;
00290     }
00291 
00292     /* copy the remainder of the infile, from offset 0 -> last_offset - 1 */
00293     printf(" copying rest of file...\n");
00294     while (last_offset) {
00295         if (last_offset > COPY_BUFFER_SIZE)
00296             bytes_to_copy = COPY_BUFFER_SIZE;
00297         else
00298             bytes_to_copy = last_offset;
00299 
00300         if (fread(copy_buffer, bytes_to_copy, 1, infile) != 1) {
00301             perror(argv[1]);
00302             goto error_out;
00303         }
00304         if (fwrite(copy_buffer, bytes_to_copy, 1, outfile) != 1) {
00305             perror(argv[2]);
00306             goto error_out;
00307         }
00308         last_offset -= bytes_to_copy;
00309     }
00310 
00311     fclose(infile);
00312     fclose(outfile);
00313     free(moov_atom);
00314     free(ftyp_atom);
00315 
00316     return 0;
00317 
00318 error_out:
00319     if (infile)
00320         fclose(infile);
00321     if (outfile)
00322         fclose(outfile);
00323     free(moov_atom);
00324     free(ftyp_atom);
00325     return 1;
00326 }