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 FFMIN(a,b) ((a) > (b) ? (b) : (a))
42 #define BE_16(x) ((((uint8_t*)(x))[0] << 8) | ((uint8_t*)(x))[1])
44 #define BE_32(x) ((((uint8_t*)(x))[0] << 24) | \
45 (((uint8_t*)(x))[1] << 16) | \
46 (((uint8_t*)(x))[2] << 8) | \
49 #define BE_64(x) (((uint64_t)(((uint8_t*)(x))[0]) << 56) | \
50 ((uint64_t)(((uint8_t*)(x))[1]) << 48) | \
51 ((uint64_t)(((uint8_t*)(x))[2]) << 40) | \
52 ((uint64_t)(((uint8_t*)(x))[3]) << 32) | \
53 ((uint64_t)(((uint8_t*)(x))[4]) << 24) | \
54 ((uint64_t)(((uint8_t*)(x))[5]) << 16) | \
55 ((uint64_t)(((uint8_t*)(x))[6]) << 8) | \
56 ((uint64_t)( (uint8_t*)(x))[7]))
58 #define BE_FOURCC(ch0, ch1, ch2, ch3) \
59 ( (uint32_t)(unsigned char)(ch3) | \
60 ((uint32_t)(unsigned char)(ch2) << 8) | \
61 ((uint32_t)(unsigned char)(ch1) << 16) | \
62 ((uint32_t)(unsigned char)(ch0) << 24) )
64 #define QT_ATOM BE_FOURCC
66 #define FREE_ATOM QT_ATOM('f', 'r', 'e', 'e')
67 #define JUNK_ATOM QT_ATOM('j', 'u', 'n', 'k')
68 #define MDAT_ATOM QT_ATOM('m', 'd', 'a', 't')
69 #define MOOV_ATOM QT_ATOM('m', 'o', 'o', 'v')
70 #define PNOT_ATOM QT_ATOM('p', 'n', 'o', 't')
71 #define SKIP_ATOM QT_ATOM('s', 'k', 'i', 'p')
72 #define WIDE_ATOM QT_ATOM('w', 'i', 'd', 'e')
73 #define PICT_ATOM QT_ATOM('P', 'I', 'C', 'T')
74 #define FTYP_ATOM QT_ATOM('f', 't', 'y', 'p')
75 #define UUID_ATOM QT_ATOM('u', 'u', 'i', 'd')
77 #define CMOV_ATOM QT_ATOM('c', 'm', 'o', 'v')
78 #define STCO_ATOM QT_ATOM('s', 't', 'c', 'o')
79 #define CO64_ATOM QT_ATOM('c', 'o', '6', '4')
81 #define ATOM_PREAMBLE_SIZE 8
82 #define COPY_BUFFER_SIZE 33554432
84 int main(
int argc,
char *argv[])
89 uint32_t atom_type = 0;
90 uint64_t atom_size = 0;
91 uint64_t atom_offset = 0;
93 unsigned char *moov_atom =
NULL;
94 unsigned char *ftyp_atom =
NULL;
95 uint64_t moov_atom_size;
96 uint64_t ftyp_atom_size = 0;
98 uint32_t offset_count;
99 uint64_t current_offset;
100 int64_t start_offset = 0;
101 unsigned char *copy_buffer =
NULL;
105 printf(
"Usage: qt-faststart <infile.mov> <outfile.mov>\n");
109 if (!strcmp(argv[1], argv[2])) {
110 fprintf(stderr,
"input and output files need to be different\n");
114 infile = fopen(argv[1],
"rb");
122 while (!feof(infile)) {
126 atom_size = (uint32_t)
BE_32(&atom_bytes[0]);
127 atom_type =
BE_32(&atom_bytes[4]);
131 ftyp_atom_size = atom_size;
133 ftyp_atom = malloc(ftyp_atom_size);
135 printf(
"could not allocate %"PRIu64
" bytes for ftyp atom\n",
140 || fread(ftyp_atom, atom_size, 1, infile) != 1
141 || (start_offset = ftello(infile))<0) {
148 if (atom_size == 1) {
152 atom_size =
BE_64(&atom_bytes[0]);
162 printf(
"%c%c%c%c %10"PRIu64
" %"PRIu64
"\n",
163 (atom_type >> 24) & 255,
164 (atom_type >> 16) & 255,
165 (atom_type >> 8) & 255,
166 (atom_type >> 0) & 255,
179 printf(
"encountered non-QT top-level atom (is this a QuickTime file?)\n");
182 atom_offset += atom_size;
192 printf(
"last atom in file was not a moov atom\n");
200 if (fseeko(infile, -atom_size, SEEK_END)) {
204 last_offset = ftello(infile);
205 moov_atom_size = atom_size;
206 moov_atom = malloc(moov_atom_size);
208 printf(
"could not allocate %"PRIu64
" bytes for moov atom\n", atom_size);
211 if (fread(moov_atom, atom_size, 1, infile) != 1) {
219 printf(
"this utility does not support compressed moov atoms yet\n");
228 for (i = 4; i < moov_atom_size - 4; i++) {
229 atom_type =
BE_32(&moov_atom[i]);
231 printf(
" patching stco atom...\n");
232 atom_size =
BE_32(&moov_atom[i - 4]);
233 if (i + atom_size - 4 > moov_atom_size) {
234 printf(
" bad atom size\n");
237 offset_count =
BE_32(&moov_atom[i + 8]);
238 if (i + 12LL + offset_count * 4LL > moov_atom_size) {
239 printf(
" bad atom size\n");
242 for (j = 0; j < offset_count; j++) {
243 current_offset =
BE_32(&moov_atom[i + 12 + j * 4]);
244 current_offset += moov_atom_size;
245 moov_atom[i + 12 + j * 4 + 0] = (current_offset >> 24) & 0xFF;
246 moov_atom[i + 12 + j * 4 + 1] = (current_offset >> 16) & 0xFF;
247 moov_atom[i + 12 + j * 4 + 2] = (current_offset >> 8) & 0xFF;
248 moov_atom[i + 12 + j * 4 + 3] = (current_offset >> 0) & 0xFF;
252 printf(
" patching co64 atom...\n");
253 atom_size =
BE_32(&moov_atom[i - 4]);
254 if (i + atom_size - 4 > moov_atom_size) {
255 printf(
" bad atom size\n");
258 offset_count =
BE_32(&moov_atom[i + 8]);
259 if (i + 12LL + offset_count * 8LL > moov_atom_size) {
260 printf(
" bad atom size\n");
263 for (j = 0; j < offset_count; j++) {
264 current_offset =
BE_64(&moov_atom[i + 12 + j * 8]);
265 current_offset += moov_atom_size;
266 moov_atom[i + 12 + j * 8 + 0] = (current_offset >> 56) & 0xFF;
267 moov_atom[i + 12 + j * 8 + 1] = (current_offset >> 48) & 0xFF;
268 moov_atom[i + 12 + j * 8 + 2] = (current_offset >> 40) & 0xFF;
269 moov_atom[i + 12 + j * 8 + 3] = (current_offset >> 32) & 0xFF;
270 moov_atom[i + 12 + j * 8 + 4] = (current_offset >> 24) & 0xFF;
271 moov_atom[i + 12 + j * 8 + 5] = (current_offset >> 16) & 0xFF;
272 moov_atom[i + 12 + j * 8 + 6] = (current_offset >> 8) & 0xFF;
273 moov_atom[i + 12 + j * 8 + 7] = (current_offset >> 0) & 0xFF;
280 infile = fopen(argv[1],
"rb");
286 if (start_offset > 0) {
287 if (fseeko(infile, start_offset, SEEK_SET)) {
292 last_offset -= start_offset;
295 outfile = fopen(argv[2],
"wb");
302 if (ftyp_atom_size > 0) {
303 printf(
" writing ftyp atom...\n");
304 if (fwrite(ftyp_atom, ftyp_atom_size, 1, outfile) != 1) {
311 printf(
" writing moov atom...\n");
312 if (fwrite(moov_atom, moov_atom_size, 1, outfile) != 1) {
319 copy_buffer = malloc(bytes_to_copy);
321 printf(
"could not allocate %d bytes for copy_buffer\n", bytes_to_copy);
324 printf(
" copying rest of file...\n");
325 while (last_offset) {
326 bytes_to_copy =
FFMIN(bytes_to_copy, last_offset);
328 if (fread(copy_buffer, bytes_to_copy, 1, infile) != 1) {
332 if (fwrite(copy_buffer, bytes_to_copy, 1, outfile) != 1) {
336 last_offset -= bytes_to_copy;