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