33 #define fseeko(x, y, z) fseeko64(x, y, z)
34 #define ftello(x) ftello64(x)
36 #define fseeko(x, y, z) _fseeki64(x, y, z)
37 #define ftello(x) _ftelli64(x)
40 #define BE_16(x) ((((uint8_t*)(x))[0] << 8) | ((uint8_t*)(x))[1])
42 #define BE_32(x) ((((uint8_t*)(x))[0] << 24) | \
43 (((uint8_t*)(x))[1] << 16) | \
44 (((uint8_t*)(x))[2] << 8) | \
47 #define BE_64(x) (((uint64_t)(((uint8_t*)(x))[0]) << 56) | \
48 ((uint64_t)(((uint8_t*)(x))[1]) << 48) | \
49 ((uint64_t)(((uint8_t*)(x))[2]) << 40) | \
50 ((uint64_t)(((uint8_t*)(x))[3]) << 32) | \
51 ((uint64_t)(((uint8_t*)(x))[4]) << 24) | \
52 ((uint64_t)(((uint8_t*)(x))[5]) << 16) | \
53 ((uint64_t)(((uint8_t*)(x))[6]) << 8) | \
54 ((uint64_t)( (uint8_t*)(x))[7]))
56 #define BE_FOURCC(ch0, ch1, ch2, ch3) \
57 ( (uint32_t)(unsigned char)(ch3) | \
58 ((uint32_t)(unsigned char)(ch2) << 8) | \
59 ((uint32_t)(unsigned char)(ch1) << 16) | \
60 ((uint32_t)(unsigned char)(ch0) << 24) )
62 #define QT_ATOM BE_FOURCC
64 #define FREE_ATOM QT_ATOM('f', 'r', 'e', 'e')
65 #define JUNK_ATOM QT_ATOM('j', 'u', 'n', 'k')
66 #define MDAT_ATOM QT_ATOM('m', 'd', 'a', 't')
67 #define MOOV_ATOM QT_ATOM('m', 'o', 'o', 'v')
68 #define PNOT_ATOM QT_ATOM('p', 'n', 'o', 't')
69 #define SKIP_ATOM QT_ATOM('s', 'k', 'i', 'p')
70 #define WIDE_ATOM QT_ATOM('w', 'i', 'd', 'e')
71 #define PICT_ATOM QT_ATOM('P', 'I', 'C', 'T')
72 #define FTYP_ATOM QT_ATOM('f', 't', 'y', 'p')
73 #define UUID_ATOM QT_ATOM('u', 'u', 'i', 'd')
75 #define CMOV_ATOM QT_ATOM('c', 'm', 'o', 'v')
76 #define STCO_ATOM QT_ATOM('s', 't', 'c', 'o')
77 #define CO64_ATOM QT_ATOM('c', 'o', '6', '4')
79 #define ATOM_PREAMBLE_SIZE 8
80 #define COPY_BUFFER_SIZE 1024
82 int main(
int argc,
char *argv[])
87 uint32_t atom_type = 0;
88 uint64_t atom_size = 0;
89 uint64_t atom_offset = 0;
91 unsigned char *moov_atom =
NULL;
92 unsigned char *ftyp_atom =
NULL;
93 uint64_t moov_atom_size;
94 uint64_t ftyp_atom_size = 0;
96 uint32_t offset_count;
97 uint64_t current_offset;
98 uint64_t start_offset = 0;
103 printf(
"Usage: qt-faststart <infile.mov> <outfile.mov>\n");
107 if (!strcmp(argv[1], argv[2])) {
108 fprintf(stderr,
"input and output files need to be different\n");
112 infile = fopen(argv[1],
"rb");
120 while (!feof(infile)) {
124 atom_size = (uint32_t)
BE_32(&atom_bytes[0]);
125 atom_type =
BE_32(&atom_bytes[4]);
129 ftyp_atom_size = atom_size;
131 ftyp_atom = malloc(ftyp_atom_size);
133 printf(
"could not allocate %"PRIu64
" bytes for ftyp atom\n",
138 if (fread(ftyp_atom, atom_size, 1, infile) != 1) {
142 start_offset = ftello(infile);
145 if (atom_size == 1) {
149 atom_size =
BE_64(&atom_bytes[0]);
155 printf(
"%c%c%c%c %10"PRIu64
" %"PRIu64
"\n",
156 (atom_type >> 24) & 255,
157 (atom_type >> 16) & 255,
158 (atom_type >> 8) & 255,
159 (atom_type >> 0) & 255,
172 printf(
"encountered non-QT top-level atom (is this a QuickTime file?)\n");
175 atom_offset += atom_size;
185 printf(
"last atom in file was not a moov atom\n");
193 fseeko(infile, -atom_size, SEEK_END);
194 last_offset = ftello(infile);
195 moov_atom_size = atom_size;
196 moov_atom = malloc(moov_atom_size);
198 printf(
"could not allocate %"PRIu64
" bytes for moov atom\n", atom_size);
201 if (fread(moov_atom, atom_size, 1, infile) != 1) {
209 printf(
"this utility does not support compressed moov atoms yet\n");
218 for (i = 4; i < moov_atom_size - 4; i++) {
219 atom_type =
BE_32(&moov_atom[i]);
221 printf(
" patching stco atom...\n");
222 atom_size =
BE_32(&moov_atom[i - 4]);
223 if (i + atom_size - 4 > moov_atom_size) {
224 printf(
" bad atom size\n");
227 offset_count =
BE_32(&moov_atom[i + 8]);
228 if (i + 12 + offset_count * UINT64_C(4) > moov_atom_size) {
229 printf(
" bad atom size/element count\n");
232 for (j = 0; j < offset_count; j++) {
233 current_offset =
BE_32(&moov_atom[i + 12 + j * 4]);
234 current_offset += moov_atom_size;
235 moov_atom[i + 12 + j * 4 + 0] = (current_offset >> 24) & 0xFF;
236 moov_atom[i + 12 + j * 4 + 1] = (current_offset >> 16) & 0xFF;
237 moov_atom[i + 12 + j * 4 + 2] = (current_offset >> 8) & 0xFF;
238 moov_atom[i + 12 + j * 4 + 3] = (current_offset >> 0) & 0xFF;
242 printf(
" patching co64 atom...\n");
243 atom_size =
BE_32(&moov_atom[i - 4]);
244 if (i + atom_size - 4 > moov_atom_size) {
245 printf(
" bad atom size\n");
248 offset_count =
BE_32(&moov_atom[i + 8]);
249 if (i + 12 + offset_count * UINT64_C(8) > moov_atom_size) {
250 printf(
" bad atom size/element count\n");
253 for (j = 0; j < offset_count; j++) {
254 current_offset =
BE_64(&moov_atom[i + 12 + j * 8]);
255 current_offset += moov_atom_size;
256 moov_atom[i + 12 + j * 8 + 0] = (current_offset >> 56) & 0xFF;
257 moov_atom[i + 12 + j * 8 + 1] = (current_offset >> 48) & 0xFF;
258 moov_atom[i + 12 + j * 8 + 2] = (current_offset >> 40) & 0xFF;
259 moov_atom[i + 12 + j * 8 + 3] = (current_offset >> 32) & 0xFF;
260 moov_atom[i + 12 + j * 8 + 4] = (current_offset >> 24) & 0xFF;
261 moov_atom[i + 12 + j * 8 + 5] = (current_offset >> 16) & 0xFF;
262 moov_atom[i + 12 + j * 8 + 6] = (current_offset >> 8) & 0xFF;
263 moov_atom[i + 12 + j * 8 + 7] = (current_offset >> 0) & 0xFF;
270 infile = fopen(argv[1],
"rb");
276 if (start_offset > 0) {
277 fseeko(infile, start_offset, SEEK_SET);
278 last_offset -= start_offset;
281 outfile = fopen(argv[2],
"wb");
288 if (ftyp_atom_size > 0) {
289 printf(
" writing ftyp atom...\n");
290 if (fwrite(ftyp_atom, ftyp_atom_size, 1, outfile) != 1) {
297 printf(
" writing moov atom...\n");
298 if (fwrite(moov_atom, moov_atom_size, 1, outfile) != 1) {
304 printf(
" copying rest of file...\n");
305 while (last_offset) {
309 bytes_to_copy = last_offset;
311 if (fread(copy_buffer, bytes_to_copy, 1, infile) != 1) {
315 if (fwrite(copy_buffer, bytes_to_copy, 1, outfile) != 1) {
319 last_offset -= bytes_to_copy;
#define ATOM_PREAMBLE_SIZE
int main(int argc, char *argv[])