00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include <inttypes.h>
00026
00027 #ifdef __MINGW32__
00028 #define fseeko(x,y,z) fseeko64(x,y,z)
00029 #define ftello(x) ftello64(x)
00030 #endif
00031
00032 #define BE_16(x) ((((uint8_t*)(x))[0] << 8) | ((uint8_t*)(x))[1])
00033 #define BE_32(x) ((((uint8_t*)(x))[0] << 24) | \
00034 (((uint8_t*)(x))[1] << 16) | \
00035 (((uint8_t*)(x))[2] << 8) | \
00036 ((uint8_t*)(x))[3])
00037 #define BE_64(x) (((uint64_t)(((uint8_t*)(x))[0]) << 56) | \
00038 ((uint64_t)(((uint8_t*)(x))[1]) << 48) | \
00039 ((uint64_t)(((uint8_t*)(x))[2]) << 40) | \
00040 ((uint64_t)(((uint8_t*)(x))[3]) << 32) | \
00041 ((uint64_t)(((uint8_t*)(x))[4]) << 24) | \
00042 ((uint64_t)(((uint8_t*)(x))[5]) << 16) | \
00043 ((uint64_t)(((uint8_t*)(x))[6]) << 8) | \
00044 ((uint64_t)((uint8_t*)(x))[7]))
00045
00046 #define BE_FOURCC( ch0, ch1, ch2, ch3 ) \
00047 ( (uint32_t)(unsigned char)(ch3) | \
00048 ( (uint32_t)(unsigned char)(ch2) << 8 ) | \
00049 ( (uint32_t)(unsigned char)(ch1) << 16 ) | \
00050 ( (uint32_t)(unsigned char)(ch0) << 24 ) )
00051
00052 #define QT_ATOM BE_FOURCC
00053
00054 #define FREE_ATOM QT_ATOM('f', 'r', 'e', 'e')
00055 #define JUNK_ATOM QT_ATOM('j', 'u', 'n', 'k')
00056 #define MDAT_ATOM QT_ATOM('m', 'd', 'a', 't')
00057 #define MOOV_ATOM QT_ATOM('m', 'o', 'o', 'v')
00058 #define PNOT_ATOM QT_ATOM('p', 'n', 'o', 't')
00059 #define SKIP_ATOM QT_ATOM('s', 'k', 'i', 'p')
00060 #define WIDE_ATOM QT_ATOM('w', 'i', 'd', 'e')
00061 #define PICT_ATOM QT_ATOM('P', 'I', 'C', 'T')
00062 #define FTYP_ATOM QT_ATOM('f', 't', 'y', 'p')
00063
00064 #define CMOV_ATOM QT_ATOM('c', 'm', 'o', 'v')
00065 #define STCO_ATOM QT_ATOM('s', 't', 'c', 'o')
00066 #define CO64_ATOM QT_ATOM('c', 'o', '6', '4')
00067
00068 #define ATOM_PREAMBLE_SIZE 8
00069 #define COPY_BUFFER_SIZE 1024
00070
00071 int main(int argc, char *argv[])
00072 {
00073 FILE *infile;
00074 FILE *outfile;
00075 unsigned char atom_bytes[ATOM_PREAMBLE_SIZE];
00076 uint32_t atom_type = 0;
00077 uint64_t atom_size = 0;
00078 uint64_t last_offset;
00079 unsigned char *moov_atom;
00080 unsigned char *ftyp_atom = 0;
00081 uint64_t moov_atom_size;
00082 uint64_t ftyp_atom_size = 0;
00083 uint64_t i, j;
00084 uint32_t offset_count;
00085 uint64_t current_offset;
00086 uint64_t start_offset = 0;
00087 unsigned char copy_buffer[COPY_BUFFER_SIZE];
00088 int bytes_to_copy;
00089
00090 if (argc != 3) {
00091 printf ("Usage: qt-faststart <infile.mov> <outfile.mov>\n");
00092 return 0;
00093 }
00094
00095 infile = fopen(argv[1], "rb");
00096 if (!infile) {
00097 perror(argv[1]);
00098 return 1;
00099 }
00100
00101
00102
00103 while (!feof(infile)) {
00104 if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
00105 break;
00106 }
00107 atom_size = (uint32_t)BE_32(&atom_bytes[0]);
00108 atom_type = BE_32(&atom_bytes[4]);
00109
00110 if ((atom_type != FREE_ATOM) &&
00111 (atom_type != JUNK_ATOM) &&
00112 (atom_type != MDAT_ATOM) &&
00113 (atom_type != MOOV_ATOM) &&
00114 (atom_type != PNOT_ATOM) &&
00115 (atom_type != SKIP_ATOM) &&
00116 (atom_type != WIDE_ATOM) &&
00117 (atom_type != PICT_ATOM) &&
00118 (atom_type != FTYP_ATOM)) {
00119 printf ("encountered non-QT top-level atom (is this a Quicktime file?)\n");
00120 break;
00121 }
00122
00123
00124 if (atom_type == FTYP_ATOM) {
00125 ftyp_atom_size = atom_size;
00126 ftyp_atom = malloc(ftyp_atom_size);
00127 if (!ftyp_atom) {
00128 printf ("could not allocate 0x%llX byte for ftyp atom\n",
00129 atom_size);
00130 fclose(infile);
00131 return 1;
00132 }
00133 fseeko(infile, -ATOM_PREAMBLE_SIZE, SEEK_CUR);
00134 if (fread(ftyp_atom, atom_size, 1, infile) != 1) {
00135 perror(argv[1]);
00136 free(ftyp_atom);
00137 fclose(infile);
00138 return 1;
00139 }
00140 start_offset = ftello(infile);
00141 continue;
00142 }
00143
00144
00145 if (atom_size == 1) {
00146 if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
00147 break;
00148 }
00149 atom_size = BE_64(&atom_bytes[0]);
00150 fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE * 2, SEEK_CUR);
00151 } else {
00152 fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE, SEEK_CUR);
00153 }
00154 }
00155
00156 if (atom_type != MOOV_ATOM) {
00157 printf ("last atom in file was not a moov atom\n");
00158 fclose(infile);
00159 return 0;
00160 }
00161
00162
00163
00164 fseeko(infile, -atom_size, SEEK_END);
00165 last_offset = ftello(infile);
00166 moov_atom_size = atom_size;
00167 moov_atom = malloc(moov_atom_size);
00168 if (!moov_atom) {
00169 printf ("could not allocate 0x%llX byte for moov atom\n",
00170 atom_size);
00171 fclose(infile);
00172 return 1;
00173 }
00174 if (fread(moov_atom, atom_size, 1, infile) != 1) {
00175 perror(argv[1]);
00176 free(moov_atom);
00177 fclose(infile);
00178 return 1;
00179 }
00180
00181
00182
00183 if (BE_32(&moov_atom[12]) == CMOV_ATOM) {
00184 printf ("this utility does not support compressed moov atoms yet\n");
00185 free(moov_atom);
00186 fclose(infile);
00187 return 1;
00188 }
00189
00190
00191 fclose(infile);
00192
00193
00194 for (i = 4; i < moov_atom_size - 4; i++) {
00195 atom_type = BE_32(&moov_atom[i]);
00196 if (atom_type == STCO_ATOM) {
00197 printf (" patching stco atom...\n");
00198 atom_size = BE_32(&moov_atom[i - 4]);
00199 if (i + atom_size - 4 > moov_atom_size) {
00200 printf (" bad atom size\n");
00201 free(moov_atom);
00202 return 1;
00203 }
00204 offset_count = BE_32(&moov_atom[i + 8]);
00205 for (j = 0; j < offset_count; j++) {
00206 current_offset = BE_32(&moov_atom[i + 12 + j * 4]);
00207 current_offset += moov_atom_size;
00208 moov_atom[i + 12 + j * 4 + 0] = (current_offset >> 24) & 0xFF;
00209 moov_atom[i + 12 + j * 4 + 1] = (current_offset >> 16) & 0xFF;
00210 moov_atom[i + 12 + j * 4 + 2] = (current_offset >> 8) & 0xFF;
00211 moov_atom[i + 12 + j * 4 + 3] = (current_offset >> 0) & 0xFF;
00212 }
00213 i += atom_size - 4;
00214 } else if (atom_type == CO64_ATOM) {
00215 printf (" patching co64 atom...\n");
00216 atom_size = BE_32(&moov_atom[i - 4]);
00217 if (i + atom_size - 4 > moov_atom_size) {
00218 printf (" bad atom size\n");
00219 free(moov_atom);
00220 return 1;
00221 }
00222 offset_count = BE_32(&moov_atom[i + 8]);
00223 for (j = 0; j < offset_count; j++) {
00224 current_offset = BE_64(&moov_atom[i + 12 + j * 8]);
00225 current_offset += moov_atom_size;
00226 moov_atom[i + 12 + j * 8 + 0] = (current_offset >> 56) & 0xFF;
00227 moov_atom[i + 12 + j * 8 + 1] = (current_offset >> 48) & 0xFF;
00228 moov_atom[i + 12 + j * 8 + 2] = (current_offset >> 40) & 0xFF;
00229 moov_atom[i + 12 + j * 8 + 3] = (current_offset >> 32) & 0xFF;
00230 moov_atom[i + 12 + j * 8 + 4] = (current_offset >> 24) & 0xFF;
00231 moov_atom[i + 12 + j * 8 + 5] = (current_offset >> 16) & 0xFF;
00232 moov_atom[i + 12 + j * 8 + 6] = (current_offset >> 8) & 0xFF;
00233 moov_atom[i + 12 + j * 8 + 7] = (current_offset >> 0) & 0xFF;
00234 }
00235 i += atom_size - 4;
00236 }
00237 }
00238
00239
00240 infile = fopen(argv[1], "rb");
00241 if (!infile) {
00242 perror(argv[1]);
00243 free(moov_atom);
00244 return 1;
00245 }
00246
00247 if (start_offset > 0) {
00248 fseeko(infile, start_offset, SEEK_SET);
00249 last_offset -= start_offset;
00250 }
00251
00252 outfile = fopen(argv[2], "wb");
00253 if (!outfile) {
00254 perror(argv[2]);
00255 fclose(outfile);
00256 free(moov_atom);
00257 return 1;
00258 }
00259
00260
00261 if (ftyp_atom_size > 0) {
00262 printf (" writing ftyp atom...\n");
00263 if (fwrite(ftyp_atom, ftyp_atom_size, 1, outfile) != 1) {
00264 perror(argv[2]);
00265 goto error_out;
00266 }
00267 }
00268
00269
00270 printf (" writing moov atom...\n");
00271 if (fwrite(moov_atom, moov_atom_size, 1, outfile) != 1) {
00272 perror(argv[2]);
00273 goto error_out;
00274 }
00275
00276
00277 printf (" copying rest of file...\n");
00278 while (last_offset) {
00279 if (last_offset > COPY_BUFFER_SIZE)
00280 bytes_to_copy = COPY_BUFFER_SIZE;
00281 else
00282 bytes_to_copy = last_offset;
00283
00284 if (fread(copy_buffer, bytes_to_copy, 1, infile) != 1) {
00285 perror(argv[1]);
00286 goto error_out;
00287 }
00288 if (fwrite(copy_buffer, bytes_to_copy, 1, outfile) != 1) {
00289 perror(argv[2]);
00290 goto error_out;
00291 }
00292
00293 last_offset -= bytes_to_copy;
00294 }
00295
00296 fclose(infile);
00297 fclose(outfile);
00298 free(moov_atom);
00299 if (ftyp_atom_size > 0)
00300 free(ftyp_atom);
00301
00302 return 0;
00303
00304 error_out:
00305 fclose(infile);
00306 fclose(outfile);
00307 free(moov_atom);
00308 if (ftyp_atom_size > 0)
00309 free(ftyp_atom);
00310 return 1;
00311 }