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