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