33 #define fseeko(x, y, z) fseeko64(x, y, z)
34 #define ftello(x) ftello64(x)
37 #define BE_16(x) ((((uint8_t*)(x))[0] << 8) | ((uint8_t*)(x))[1])
39 #define BE_32(x) ((((uint8_t*)(x))[0] << 24) | \
40 (((uint8_t*)(x))[1] << 16) | \
41 (((uint8_t*)(x))[2] << 8) | \
44 #define BE_64(x) (((uint64_t)(((uint8_t*)(x))[0]) << 56) | \
45 ((uint64_t)(((uint8_t*)(x))[1]) << 48) | \
46 ((uint64_t)(((uint8_t*)(x))[2]) << 40) | \
47 ((uint64_t)(((uint8_t*)(x))[3]) << 32) | \
48 ((uint64_t)(((uint8_t*)(x))[4]) << 24) | \
49 ((uint64_t)(((uint8_t*)(x))[5]) << 16) | \
50 ((uint64_t)(((uint8_t*)(x))[6]) << 8) | \
51 ((uint64_t)( (uint8_t*)(x))[7]))
53 #define BE_FOURCC(ch0, ch1, ch2, ch3) \
54 ( (uint32_t)(unsigned char)(ch3) | \
55 ((uint32_t)(unsigned char)(ch2) << 8) | \
56 ((uint32_t)(unsigned char)(ch1) << 16) | \
57 ((uint32_t)(unsigned char)(ch0) << 24) )
59 #define QT_ATOM BE_FOURCC
61 #define FREE_ATOM QT_ATOM('f', 'r', 'e', 'e')
62 #define JUNK_ATOM QT_ATOM('j', 'u', 'n', 'k')
63 #define MDAT_ATOM QT_ATOM('m', 'd', 'a', 't')
64 #define MOOV_ATOM QT_ATOM('m', 'o', 'o', 'v')
65 #define PNOT_ATOM QT_ATOM('p', 'n', 'o', 't')
66 #define SKIP_ATOM QT_ATOM('s', 'k', 'i', 'p')
67 #define WIDE_ATOM QT_ATOM('w', 'i', 'd', 'e')
68 #define PICT_ATOM QT_ATOM('P', 'I', 'C', 'T')
69 #define FTYP_ATOM QT_ATOM('f', 't', 'y', 'p')
70 #define UUID_ATOM QT_ATOM('u', 'u', 'i', 'd')
72 #define CMOV_ATOM QT_ATOM('c', 'm', 'o', 'v')
73 #define STCO_ATOM QT_ATOM('s', 't', 'c', 'o')
74 #define CO64_ATOM QT_ATOM('c', 'o', '6', '4')
76 #define ATOM_PREAMBLE_SIZE 8
77 #define COPY_BUFFER_SIZE 1024
79 int main(
int argc,
char *argv[])
84 uint32_t atom_type = 0;
85 uint64_t atom_size = 0;
86 uint64_t atom_offset = 0;
88 unsigned char *moov_atom =
NULL;
89 unsigned char *ftyp_atom =
NULL;
90 uint64_t moov_atom_size;
91 uint64_t ftyp_atom_size = 0;
93 uint32_t offset_count;
94 uint64_t current_offset;
95 uint64_t start_offset = 0;
100 printf(
"Usage: qt-faststart <infile.mov> <outfile.mov>\n");
104 if (!strcmp(argv[1], argv[2])) {
105 fprintf(stderr,
"input and output files need to be different\n");
109 infile = fopen(argv[1],
"rb");
117 while (!feof(infile)) {
121 atom_size = (uint32_t)
BE_32(&atom_bytes[0]);
122 atom_type =
BE_32(&atom_bytes[4]);
126 ftyp_atom_size = atom_size;
128 ftyp_atom =
malloc(ftyp_atom_size);
130 printf(
"could not allocate %"PRIu64
" bytes for ftyp atom\n",
135 if (fread(ftyp_atom, atom_size, 1, infile) != 1) {
139 start_offset = ftello(infile);
143 if (atom_size == 1) {
147 atom_size =
BE_64(&atom_bytes[0]);
153 printf(
"%c%c%c%c %10"PRIu64
" %"PRIu64
"\n",
154 (atom_type >> 24) & 255,
155 (atom_type >> 16) & 255,
156 (atom_type >> 8) & 255,
157 (atom_type >> 0) & 255,
170 printf(
"encountered non-QT top-level atom (is this a QuickTime file?)\n");
173 atom_offset += atom_size;
183 printf(
"last atom in file was not a moov atom\n");
191 fseeko(infile, -atom_size, SEEK_END);
192 last_offset = ftello(infile);
193 moov_atom_size = atom_size;
194 moov_atom =
malloc(moov_atom_size);
196 printf(
"could not allocate %"PRIu64
" bytes for moov atom\n", atom_size);
199 if (fread(moov_atom, atom_size, 1, infile) != 1) {
207 printf(
"this utility does not support compressed moov atoms yet\n");
216 for (i = 4; i < moov_atom_size - 4; i++) {
217 atom_type =
BE_32(&moov_atom[i]);
219 printf(
" patching stco atom...\n");
220 atom_size =
BE_32(&moov_atom[i - 4]);
221 if (i + atom_size - 4 > moov_atom_size) {
222 printf(
" bad atom size\n");
225 offset_count =
BE_32(&moov_atom[i + 8]);
226 for (j = 0; j < offset_count; j++) {
227 current_offset =
BE_32(&moov_atom[i + 12 + j * 4]);
228 current_offset += moov_atom_size;
229 moov_atom[i + 12 + j * 4 + 0] = (current_offset >> 24) & 0xFF;
230 moov_atom[i + 12 + j * 4 + 1] = (current_offset >> 16) & 0xFF;
231 moov_atom[i + 12 + j * 4 + 2] = (current_offset >> 8) & 0xFF;
232 moov_atom[i + 12 + j * 4 + 3] = (current_offset >> 0) & 0xFF;
236 printf(
" patching co64 atom...\n");
237 atom_size =
BE_32(&moov_atom[i - 4]);
238 if (i + atom_size - 4 > moov_atom_size) {
239 printf(
" bad atom size\n");
242 offset_count =
BE_32(&moov_atom[i + 8]);
243 for (j = 0; j < offset_count; j++) {
244 current_offset =
BE_64(&moov_atom[i + 12 + j * 8]);
245 current_offset += moov_atom_size;
246 moov_atom[i + 12 + j * 8 + 0] = (current_offset >> 56) & 0xFF;
247 moov_atom[i + 12 + j * 8 + 1] = (current_offset >> 48) & 0xFF;
248 moov_atom[i + 12 + j * 8 + 2] = (current_offset >> 40) & 0xFF;
249 moov_atom[i + 12 + j * 8 + 3] = (current_offset >> 32) & 0xFF;
250 moov_atom[i + 12 + j * 8 + 4] = (current_offset >> 24) & 0xFF;
251 moov_atom[i + 12 + j * 8 + 5] = (current_offset >> 16) & 0xFF;
252 moov_atom[i + 12 + j * 8 + 6] = (current_offset >> 8) & 0xFF;
253 moov_atom[i + 12 + j * 8 + 7] = (current_offset >> 0) & 0xFF;
260 infile = fopen(argv[1],
"rb");
266 if (start_offset > 0) {
267 fseeko(infile, start_offset, SEEK_SET);
268 last_offset -= start_offset;
271 outfile = fopen(argv[2],
"wb");
278 if (ftyp_atom_size > 0) {
279 printf(
" writing ftyp atom...\n");
280 if (fwrite(ftyp_atom, ftyp_atom_size, 1, outfile) != 1) {
287 printf(
" writing moov atom...\n");
288 if (fwrite(moov_atom, moov_atom_size, 1, outfile) != 1) {
294 printf(
" copying rest of file...\n");
295 while (last_offset) {
299 bytes_to_copy = last_offset;
301 if (fread(copy_buffer, bytes_to_copy, 1, infile) != 1) {
305 if (fwrite(copy_buffer, bytes_to_copy, 1, outfile) != 1) {
309 last_offset -= bytes_to_copy;