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