Libav 0.7.1
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 Libav 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 #define BE_32(x) ((((uint8_t*)(x))[0] << 24) | \
00039                   (((uint8_t*)(x))[1] << 16) | \
00040                   (((uint8_t*)(x))[2] << 8) | \
00041                    ((uint8_t*)(x))[3])
00042 #define BE_64(x) (((uint64_t)(((uint8_t*)(x))[0]) << 56) | \
00043                   ((uint64_t)(((uint8_t*)(x))[1]) << 48) | \
00044                   ((uint64_t)(((uint8_t*)(x))[2]) << 40) | \
00045                   ((uint64_t)(((uint8_t*)(x))[3]) << 32) | \
00046                   ((uint64_t)(((uint8_t*)(x))[4]) << 24) | \
00047                   ((uint64_t)(((uint8_t*)(x))[5]) << 16) | \
00048                   ((uint64_t)(((uint8_t*)(x))[6]) << 8) | \
00049                   ((uint64_t)((uint8_t*)(x))[7]))
00050 
00051 #define BE_FOURCC( ch0, ch1, ch2, ch3 )             \
00052         ( (uint32_t)(unsigned char)(ch3) |          \
00053         ( (uint32_t)(unsigned char)(ch2) << 8 ) |   \
00054         ( (uint32_t)(unsigned char)(ch1) << 16 ) |  \
00055         ( (uint32_t)(unsigned char)(ch0) << 24 ) )
00056 
00057 #define QT_ATOM BE_FOURCC
00058 /* top level atoms */
00059 #define FREE_ATOM QT_ATOM('f', 'r', 'e', 'e')
00060 #define JUNK_ATOM QT_ATOM('j', 'u', 'n', 'k')
00061 #define MDAT_ATOM QT_ATOM('m', 'd', 'a', 't')
00062 #define MOOV_ATOM QT_ATOM('m', 'o', 'o', 'v')
00063 #define PNOT_ATOM QT_ATOM('p', 'n', 'o', 't')
00064 #define SKIP_ATOM QT_ATOM('s', 'k', 'i', 'p')
00065 #define WIDE_ATOM QT_ATOM('w', 'i', 'd', 'e')
00066 #define PICT_ATOM QT_ATOM('P', 'I', 'C', 'T')
00067 #define FTYP_ATOM QT_ATOM('f', 't', 'y', 'p')
00068 #define UUID_ATOM QT_ATOM('u', 'u', 'i', 'd')
00069 
00070 #define CMOV_ATOM QT_ATOM('c', 'm', 'o', 'v')
00071 #define STCO_ATOM QT_ATOM('s', 't', 'c', 'o')
00072 #define CO64_ATOM QT_ATOM('c', 'o', '6', '4')
00073 
00074 #define ATOM_PREAMBLE_SIZE 8
00075 #define COPY_BUFFER_SIZE 1024
00076 
00077 int main(int argc, char *argv[])
00078 {
00079     FILE *infile  = NULL;
00080     FILE *outfile = NULL;
00081     unsigned char atom_bytes[ATOM_PREAMBLE_SIZE];
00082     uint32_t atom_type = 0;
00083     uint64_t atom_size = 0;
00084     uint64_t atom_offset = 0;
00085     uint64_t last_offset;
00086     unsigned char *moov_atom = NULL;
00087     unsigned char *ftyp_atom = NULL;
00088     uint64_t moov_atom_size;
00089     uint64_t ftyp_atom_size = 0;
00090     uint64_t i, j;
00091     uint32_t offset_count;
00092     uint64_t current_offset;
00093     uint64_t start_offset = 0;
00094     unsigned char copy_buffer[COPY_BUFFER_SIZE];
00095     int bytes_to_copy;
00096 
00097     if (argc != 3) {
00098         printf ("Usage: qt-faststart <infile.mov> <outfile.mov>\n");
00099         return 0;
00100     }
00101 
00102     if (!strcmp(argv[1], argv[2])) {
00103         fprintf(stderr, "input and output files need to be different\n");
00104         return 1;
00105     }
00106 
00107     infile = fopen(argv[1], "rb");
00108     if (!infile) {
00109         perror(argv[1]);
00110         goto error_out;
00111     }
00112 
00113     /* traverse through the atoms in the file to make sure that 'moov' is
00114      * at the end */
00115     while (!feof(infile)) {
00116         if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
00117             break;
00118         }
00119         atom_size = (uint32_t)BE_32(&atom_bytes[0]);
00120         atom_type = BE_32(&atom_bytes[4]);
00121 
00122         /* keep ftyp atom */
00123         if (atom_type == FTYP_ATOM) {
00124             ftyp_atom_size = atom_size;
00125             free(ftyp_atom);
00126             ftyp_atom = malloc(ftyp_atom_size);
00127             if (!ftyp_atom) {
00128                 printf ("could not allocate %"PRIu64" byte for ftyp atom\n",
00129                         atom_size);
00130                 goto error_out;
00131             }
00132             fseeko(infile, -ATOM_PREAMBLE_SIZE, SEEK_CUR);
00133             if (fread(ftyp_atom, atom_size, 1, infile) != 1) {
00134                 perror(argv[1]);
00135                 goto error_out;
00136             }
00137             start_offset = ftello(infile);
00138         } else {
00139 
00140         /* 64-bit special case */
00141         if (atom_size == 1) {
00142             if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
00143                 break;
00144             }
00145             atom_size = BE_64(&atom_bytes[0]);
00146             fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE * 2, SEEK_CUR);
00147         } else {
00148             fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE, SEEK_CUR);
00149         }
00150     }
00151         printf("%c%c%c%c %10"PRIu64" %"PRIu64"\n",
00152                (atom_type >> 24) & 255,
00153                (atom_type >> 16) & 255,
00154                (atom_type >>  8) & 255,
00155                (atom_type >>  0) & 255,
00156                atom_offset,
00157                atom_size);
00158         if ((atom_type != FREE_ATOM) &&
00159             (atom_type != JUNK_ATOM) &&
00160             (atom_type != MDAT_ATOM) &&
00161             (atom_type != MOOV_ATOM) &&
00162             (atom_type != PNOT_ATOM) &&
00163             (atom_type != SKIP_ATOM) &&
00164             (atom_type != WIDE_ATOM) &&
00165             (atom_type != PICT_ATOM) &&
00166             (atom_type != UUID_ATOM) &&
00167             (atom_type != FTYP_ATOM)) {
00168             printf ("encountered non-QT top-level atom (is this a Quicktime file?)\n");
00169             break;
00170         }
00171         atom_offset += atom_size;
00172 
00173         /* The atom header is 8 (or 16 bytes), if the atom size (which
00174          * includes these 8 or 16 bytes) is less than that, we won't be
00175          * able to continue scanning sensibly after this atom, so break. */
00176         if (atom_size < 8)
00177             break;
00178     }
00179 
00180     if (atom_type != MOOV_ATOM) {
00181         printf ("last atom in file was not a moov atom\n");
00182         free(ftyp_atom);
00183         fclose(infile);
00184         return 0;
00185     }
00186 
00187     /* moov atom was, in fact, the last atom in the chunk; load the whole
00188      * moov atom */
00189     fseeko(infile, -atom_size, SEEK_END);
00190     last_offset = ftello(infile);
00191     moov_atom_size = atom_size;
00192     moov_atom = malloc(moov_atom_size);
00193     if (!moov_atom) {
00194         printf ("could not allocate %"PRIu64" byte for moov atom\n",
00195             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 
00309         last_offset -= bytes_to_copy;
00310     }
00311 
00312     fclose(infile);
00313     fclose(outfile);
00314     free(moov_atom);
00315     free(ftyp_atom);
00316 
00317     return 0;
00318 
00319 error_out:
00320     if (infile)
00321         fclose(infile);
00322     if (outfile)
00323         fclose(outfile);
00324     free(moov_atom);
00325     free(ftyp_atom);
00326     return 1;
00327 }