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 #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
00108
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
00117 if (atom_type == FTYP_ATOM) {
00118 ftyp_atom_size = atom_size;
00119 ftyp_atom = malloc(ftyp_atom_size);
00120 if (!ftyp_atom) {
00121 printf ("could not allocate %"PRIu64" byte for ftyp atom\n",
00122 atom_size);
00123 fclose(infile);
00124 return 1;
00125 }
00126 fseeko(infile, -ATOM_PREAMBLE_SIZE, SEEK_CUR);
00127 if (fread(ftyp_atom, atom_size, 1, infile) != 1) {
00128 perror(argv[1]);
00129 free(ftyp_atom);
00130 fclose(infile);
00131 return 1;
00132 }
00133 start_offset = ftello(infile);
00134 } else {
00135
00136
00137 if (atom_size == 1) {
00138 if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
00139 break;
00140 }
00141 atom_size = BE_64(&atom_bytes[0]);
00142 fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE * 2, SEEK_CUR);
00143 } else {
00144 fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE, SEEK_CUR);
00145 }
00146 }
00147 printf("%c%c%c%c %10"PRIu64" %"PRIu64"\n",
00148 (atom_type >> 24) & 255,
00149 (atom_type >> 16) & 255,
00150 (atom_type >> 8) & 255,
00151 (atom_type >> 0) & 255,
00152 atom_offset,
00153 atom_size);
00154 if ((atom_type != FREE_ATOM) &&
00155 (atom_type != JUNK_ATOM) &&
00156 (atom_type != MDAT_ATOM) &&
00157 (atom_type != MOOV_ATOM) &&
00158 (atom_type != PNOT_ATOM) &&
00159 (atom_type != SKIP_ATOM) &&
00160 (atom_type != WIDE_ATOM) &&
00161 (atom_type != PICT_ATOM) &&
00162 (atom_type != UUID_ATOM) &&
00163 (atom_type != FTYP_ATOM)) {
00164 printf ("encountered non-QT top-level atom (is this a Quicktime file?)\n");
00165 break;
00166 }
00167 atom_offset += atom_size;
00168 }
00169
00170 if (atom_type != MOOV_ATOM) {
00171 printf ("last atom in file was not a moov atom\n");
00172 fclose(infile);
00173 return 0;
00174 }
00175
00176
00177
00178 fseeko(infile, -atom_size, SEEK_END);
00179 last_offset = ftello(infile);
00180 moov_atom_size = atom_size;
00181 moov_atom = malloc(moov_atom_size);
00182 if (!moov_atom) {
00183 printf ("could not allocate %"PRIu64" byte for moov atom\n",
00184 atom_size);
00185 fclose(infile);
00186 return 1;
00187 }
00188 if (fread(moov_atom, atom_size, 1, infile) != 1) {
00189 perror(argv[1]);
00190 free(moov_atom);
00191 fclose(infile);
00192 return 1;
00193 }
00194
00195
00196
00197 if (BE_32(&moov_atom[12]) == CMOV_ATOM) {
00198 printf ("this utility does not support compressed moov atoms yet\n");
00199 free(moov_atom);
00200 fclose(infile);
00201 return 1;
00202 }
00203
00204
00205 fclose(infile);
00206
00207
00208 for (i = 4; i < moov_atom_size - 4; i++) {
00209 atom_type = BE_32(&moov_atom[i]);
00210 if (atom_type == STCO_ATOM) {
00211 printf (" patching stco atom...\n");
00212 atom_size = BE_32(&moov_atom[i - 4]);
00213 if (i + atom_size - 4 > moov_atom_size) {
00214 printf (" bad atom size\n");
00215 free(moov_atom);
00216 return 1;
00217 }
00218 offset_count = BE_32(&moov_atom[i + 8]);
00219 for (j = 0; j < offset_count; j++) {
00220 current_offset = BE_32(&moov_atom[i + 12 + j * 4]);
00221 current_offset += moov_atom_size;
00222 moov_atom[i + 12 + j * 4 + 0] = (current_offset >> 24) & 0xFF;
00223 moov_atom[i + 12 + j * 4 + 1] = (current_offset >> 16) & 0xFF;
00224 moov_atom[i + 12 + j * 4 + 2] = (current_offset >> 8) & 0xFF;
00225 moov_atom[i + 12 + j * 4 + 3] = (current_offset >> 0) & 0xFF;
00226 }
00227 i += atom_size - 4;
00228 } else if (atom_type == CO64_ATOM) {
00229 printf (" patching co64 atom...\n");
00230 atom_size = BE_32(&moov_atom[i - 4]);
00231 if (i + atom_size - 4 > moov_atom_size) {
00232 printf (" bad atom size\n");
00233 free(moov_atom);
00234 return 1;
00235 }
00236 offset_count = BE_32(&moov_atom[i + 8]);
00237 for (j = 0; j < offset_count; j++) {
00238 current_offset = BE_64(&moov_atom[i + 12 + j * 8]);
00239 current_offset += moov_atom_size;
00240 moov_atom[i + 12 + j * 8 + 0] = (current_offset >> 56) & 0xFF;
00241 moov_atom[i + 12 + j * 8 + 1] = (current_offset >> 48) & 0xFF;
00242 moov_atom[i + 12 + j * 8 + 2] = (current_offset >> 40) & 0xFF;
00243 moov_atom[i + 12 + j * 8 + 3] = (current_offset >> 32) & 0xFF;
00244 moov_atom[i + 12 + j * 8 + 4] = (current_offset >> 24) & 0xFF;
00245 moov_atom[i + 12 + j * 8 + 5] = (current_offset >> 16) & 0xFF;
00246 moov_atom[i + 12 + j * 8 + 6] = (current_offset >> 8) & 0xFF;
00247 moov_atom[i + 12 + j * 8 + 7] = (current_offset >> 0) & 0xFF;
00248 }
00249 i += atom_size - 4;
00250 }
00251 }
00252
00253
00254 infile = fopen(argv[1], "rb");
00255 if (!infile) {
00256 perror(argv[1]);
00257 free(moov_atom);
00258 return 1;
00259 }
00260
00261 if (start_offset > 0) {
00262 fseeko(infile, start_offset, SEEK_SET);
00263 last_offset -= start_offset;
00264 }
00265
00266 outfile = fopen(argv[2], "wb");
00267 if (!outfile) {
00268 perror(argv[2]);
00269 fclose(outfile);
00270 free(moov_atom);
00271 return 1;
00272 }
00273
00274
00275 if (ftyp_atom_size > 0) {
00276 printf (" writing ftyp atom...\n");
00277 if (fwrite(ftyp_atom, ftyp_atom_size, 1, outfile) != 1) {
00278 perror(argv[2]);
00279 goto error_out;
00280 }
00281 }
00282
00283
00284 printf (" writing moov atom...\n");
00285 if (fwrite(moov_atom, moov_atom_size, 1, outfile) != 1) {
00286 perror(argv[2]);
00287 goto error_out;
00288 }
00289
00290
00291 printf (" copying rest of file...\n");
00292 while (last_offset) {
00293 if (last_offset > COPY_BUFFER_SIZE)
00294 bytes_to_copy = COPY_BUFFER_SIZE;
00295 else
00296 bytes_to_copy = last_offset;
00297
00298 if (fread(copy_buffer, bytes_to_copy, 1, infile) != 1) {
00299 perror(argv[1]);
00300 goto error_out;
00301 }
00302 if (fwrite(copy_buffer, bytes_to_copy, 1, outfile) != 1) {
00303 perror(argv[2]);
00304 goto error_out;
00305 }
00306
00307 last_offset -= bytes_to_copy;
00308 }
00309
00310 fclose(infile);
00311 fclose(outfile);
00312 free(moov_atom);
00313 if (ftyp_atom_size > 0)
00314 free(ftyp_atom);
00315
00316 return 0;
00317
00318 error_out:
00319 fclose(infile);
00320 fclose(outfile);
00321 free(moov_atom);
00322 if (ftyp_atom_size > 0)
00323 free(ftyp_atom);
00324 return 1;
00325 }