• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

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 
00031 #ifdef __MINGW32__
00032 #define fseeko(x,y,z)  fseeko64(x,y,z)
00033 #define ftello(x)      ftello64(x)
00034 #endif
00035 
00036 #define BE_16(x) ((((uint8_t*)(x))[0] << 8) | ((uint8_t*)(x))[1])
00037 #define BE_32(x) ((((uint8_t*)(x))[0] << 24) | \
00038                   (((uint8_t*)(x))[1] << 16) | \
00039                   (((uint8_t*)(x))[2] << 8) | \
00040                    ((uint8_t*)(x))[3])
00041 #define BE_64(x) (((uint64_t)(((uint8_t*)(x))[0]) << 56) | \
00042                   ((uint64_t)(((uint8_t*)(x))[1]) << 48) | \
00043                   ((uint64_t)(((uint8_t*)(x))[2]) << 40) | \
00044                   ((uint64_t)(((uint8_t*)(x))[3]) << 32) | \
00045                   ((uint64_t)(((uint8_t*)(x))[4]) << 24) | \
00046                   ((uint64_t)(((uint8_t*)(x))[5]) << 16) | \
00047                   ((uint64_t)(((uint8_t*)(x))[6]) << 8) | \
00048                   ((uint64_t)((uint8_t*)(x))[7]))
00049 
00050 #define BE_FOURCC( ch0, ch1, ch2, ch3 )             \
00051         ( (uint32_t)(unsigned char)(ch3) |          \
00052         ( (uint32_t)(unsigned char)(ch2) << 8 ) |   \
00053         ( (uint32_t)(unsigned char)(ch1) << 16 ) |  \
00054         ( (uint32_t)(unsigned char)(ch0) << 24 ) )
00055 
00056 #define QT_ATOM BE_FOURCC
00057 /* top level atoms */
00058 #define FREE_ATOM QT_ATOM('f', 'r', 'e', 'e')
00059 #define JUNK_ATOM QT_ATOM('j', 'u', 'n', 'k')
00060 #define MDAT_ATOM QT_ATOM('m', 'd', 'a', 't')
00061 #define MOOV_ATOM QT_ATOM('m', 'o', 'o', 'v')
00062 #define PNOT_ATOM QT_ATOM('p', 'n', 'o', 't')
00063 #define SKIP_ATOM QT_ATOM('s', 'k', 'i', 'p')
00064 #define WIDE_ATOM QT_ATOM('w', 'i', 'd', 'e')
00065 #define PICT_ATOM QT_ATOM('P', 'I', 'C', 'T')
00066 #define FTYP_ATOM QT_ATOM('f', 't', 'y', 'p')
00067 #define UUID_ATOM QT_ATOM('u', 'u', 'i', 'd')
00068 
00069 #define CMOV_ATOM QT_ATOM('c', 'm', 'o', 'v')
00070 #define STCO_ATOM QT_ATOM('s', 't', 'c', 'o')
00071 #define CO64_ATOM QT_ATOM('c', 'o', '6', '4')
00072 
00073 #define ATOM_PREAMBLE_SIZE 8
00074 #define COPY_BUFFER_SIZE 1024
00075 
00076 int main(int argc, char *argv[])
00077 {
00078     FILE *infile;
00079     FILE *outfile;
00080     unsigned char atom_bytes[ATOM_PREAMBLE_SIZE];
00081     uint32_t atom_type = 0;
00082     uint64_t atom_size = 0;
00083     uint64_t atom_offset = 0;
00084     uint64_t last_offset;
00085     unsigned char *moov_atom;
00086     unsigned char *ftyp_atom = 0;
00087     uint64_t moov_atom_size;
00088     uint64_t ftyp_atom_size = 0;
00089     uint64_t i, j;
00090     uint32_t offset_count;
00091     uint64_t current_offset;
00092     uint64_t start_offset = 0;
00093     unsigned char copy_buffer[COPY_BUFFER_SIZE];
00094     int bytes_to_copy;
00095 
00096     if (argc != 3) {
00097         printf ("Usage: qt-faststart <infile.mov> <outfile.mov>\n");
00098         return 0;
00099     }
00100 
00101     infile = fopen(argv[1], "rb");
00102     if (!infile) {
00103         perror(argv[1]);
00104         return 1;
00105     }
00106 
00107     /* traverse through the atoms in the file to make sure that 'moov' is
00108      * at the end */
00109     while (!feof(infile)) {
00110         if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
00111             break;
00112         }
00113         atom_size = (uint32_t)BE_32(&atom_bytes[0]);
00114         atom_type = BE_32(&atom_bytes[4]);
00115 
00116         /* keep ftyp atom */
00117         if (atom_type == FTYP_ATOM) {
00118             ftyp_atom_size = atom_size;
00119             free(ftyp_atom);
00120             ftyp_atom = malloc(ftyp_atom_size);
00121             if (!ftyp_atom) {
00122                 printf ("could not allocate %"PRIu64" byte for ftyp atom\n",
00123                         atom_size);
00124                 fclose(infile);
00125                 return 1;
00126             }
00127             fseeko(infile, -ATOM_PREAMBLE_SIZE, SEEK_CUR);
00128             if (fread(ftyp_atom, atom_size, 1, infile) != 1) {
00129                 perror(argv[1]);
00130                 free(ftyp_atom);
00131                 fclose(infile);
00132                 return 1;
00133             }
00134             start_offset = ftello(infile);
00135         } else {
00136 
00137         /* 64-bit special case */
00138         if (atom_size == 1) {
00139             if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
00140                 break;
00141             }
00142             atom_size = BE_64(&atom_bytes[0]);
00143             fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE * 2, SEEK_CUR);
00144         } else {
00145             fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE, SEEK_CUR);
00146         }
00147     }
00148         printf("%c%c%c%c %10"PRIu64" %"PRIu64"\n",
00149                (atom_type >> 24) & 255,
00150                (atom_type >> 16) & 255,
00151                (atom_type >>  8) & 255,
00152                (atom_type >>  0) & 255,
00153                atom_offset,
00154                atom_size);
00155         if ((atom_type != FREE_ATOM) &&
00156             (atom_type != JUNK_ATOM) &&
00157             (atom_type != MDAT_ATOM) &&
00158             (atom_type != MOOV_ATOM) &&
00159             (atom_type != PNOT_ATOM) &&
00160             (atom_type != SKIP_ATOM) &&
00161             (atom_type != WIDE_ATOM) &&
00162             (atom_type != PICT_ATOM) &&
00163             (atom_type != UUID_ATOM) &&
00164             (atom_type != FTYP_ATOM)) {
00165             printf ("encountered non-QT top-level atom (is this a Quicktime file?)\n");
00166             break;
00167         }
00168         atom_offset += atom_size;
00169     }
00170 
00171     if (atom_type != MOOV_ATOM) {
00172         printf ("last atom in file was not a moov atom\n");
00173         free(ftyp_atom);
00174         fclose(infile);
00175         return 0;
00176     }
00177 
00178     /* moov atom was, in fact, the last atom in the chunk; load the whole
00179      * moov atom */
00180     fseeko(infile, -atom_size, SEEK_END);
00181     last_offset = ftello(infile);
00182     moov_atom_size = atom_size;
00183     moov_atom = malloc(moov_atom_size);
00184     if (!moov_atom) {
00185         printf ("could not allocate %"PRIu64" byte for moov atom\n",
00186             atom_size);
00187         free(ftyp_atom);
00188         fclose(infile);
00189         return 1;
00190     }
00191     if (fread(moov_atom, atom_size, 1, infile) != 1) {
00192         perror(argv[1]);
00193         free(moov_atom);
00194         free(ftyp_atom);
00195         fclose(infile);
00196         return 1;
00197     }
00198 
00199     /* this utility does not support compressed atoms yet, so disqualify
00200      * files with compressed QT atoms */
00201     if (BE_32(&moov_atom[12]) == CMOV_ATOM) {
00202         printf ("this utility does not support compressed moov atoms yet\n");
00203         free(moov_atom);
00204         free(ftyp_atom);
00205         fclose(infile);
00206         return 1;
00207     }
00208 
00209     /* close; will be re-opened later */
00210     fclose(infile);
00211 
00212     /* crawl through the moov chunk in search of stco or co64 atoms */
00213     for (i = 4; i < moov_atom_size - 4; i++) {
00214         atom_type = BE_32(&moov_atom[i]);
00215         if (atom_type == STCO_ATOM) {
00216             printf (" patching stco atom...\n");
00217             atom_size = BE_32(&moov_atom[i - 4]);
00218             if (i + atom_size - 4 > moov_atom_size) {
00219                 printf (" bad atom size\n");
00220                 free(moov_atom);
00221                 free(ftyp_atom);
00222                 return 1;
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                 free(moov_atom);
00240                 free(ftyp_atom);
00241                 return 1;
00242             }
00243             offset_count = BE_32(&moov_atom[i + 8]);
00244             for (j = 0; j < offset_count; j++) {
00245                 current_offset = BE_64(&moov_atom[i + 12 + j * 8]);
00246                 current_offset += moov_atom_size;
00247                 moov_atom[i + 12 + j * 8 + 0] = (current_offset >> 56) & 0xFF;
00248                 moov_atom[i + 12 + j * 8 + 1] = (current_offset >> 48) & 0xFF;
00249                 moov_atom[i + 12 + j * 8 + 2] = (current_offset >> 40) & 0xFF;
00250                 moov_atom[i + 12 + j * 8 + 3] = (current_offset >> 32) & 0xFF;
00251                 moov_atom[i + 12 + j * 8 + 4] = (current_offset >> 24) & 0xFF;
00252                 moov_atom[i + 12 + j * 8 + 5] = (current_offset >> 16) & 0xFF;
00253                 moov_atom[i + 12 + j * 8 + 6] = (current_offset >>  8) & 0xFF;
00254                 moov_atom[i + 12 + j * 8 + 7] = (current_offset >>  0) & 0xFF;
00255             }
00256             i += atom_size - 4;
00257         }
00258     }
00259 
00260     /* re-open the input file and open the output file */
00261     infile = fopen(argv[1], "rb");
00262     if (!infile) {
00263         perror(argv[1]);
00264         free(moov_atom);
00265         free(ftyp_atom);
00266         return 1;
00267     }
00268 
00269     if (start_offset > 0) { /* seek after ftyp atom */
00270         fseeko(infile, start_offset, SEEK_SET);
00271         last_offset -= start_offset;
00272     }
00273 
00274     outfile = fopen(argv[2], "wb");
00275     if (!outfile) {
00276         perror(argv[2]);
00277         fclose(outfile);
00278         free(moov_atom);
00279         free(ftyp_atom);
00280         return 1;
00281     }
00282 
00283     /* dump the same ftyp atom */
00284     if (ftyp_atom_size > 0) {
00285         printf (" writing ftyp atom...\n");
00286         if (fwrite(ftyp_atom, ftyp_atom_size, 1, outfile) != 1) {
00287             perror(argv[2]);
00288             goto error_out;
00289         }
00290     }
00291 
00292     /* dump the new moov atom */
00293     printf (" writing moov atom...\n");
00294     if (fwrite(moov_atom, moov_atom_size, 1, outfile) != 1) {
00295         perror(argv[2]);
00296         goto error_out;
00297     }
00298 
00299     /* copy the remainder of the infile, from offset 0 -> last_offset - 1 */
00300     printf (" copying rest of file...\n");
00301     while (last_offset) {
00302         if (last_offset > COPY_BUFFER_SIZE)
00303             bytes_to_copy = COPY_BUFFER_SIZE;
00304         else
00305             bytes_to_copy = last_offset;
00306 
00307         if (fread(copy_buffer, bytes_to_copy, 1, infile) != 1) {
00308             perror(argv[1]);
00309             goto error_out;
00310         }
00311         if (fwrite(copy_buffer, bytes_to_copy, 1, outfile) != 1) {
00312             perror(argv[2]);
00313             goto error_out;
00314         }
00315 
00316         last_offset -= bytes_to_copy;
00317     }
00318 
00319     fclose(infile);
00320     fclose(outfile);
00321     free(moov_atom);
00322     free(ftyp_atom);
00323 
00324     return 0;
00325 
00326 error_out:
00327     fclose(infile);
00328     fclose(outfile);
00329     free(moov_atom);
00330     free(ftyp_atom);
00331     return 1;
00332 }

Generated on Fri Sep 16 2011 17:17:52 for FFmpeg by  doxygen 1.7.1