33 #define fseeko(x, y, z) fseek(x, y, z)
34 #define ftello(x) ftell(x)
35 #elif defined(__MINGW32__)
37 #define fseeko(x, y, z) fseeko64(x, y, z)
39 #define ftello(x) ftello64(x)
42 #define fseeko(x, y, z) _fseeki64(x, y, z)
44 #define ftello(x) _ftelli64(x)
47 #define MIN(a,b) ((a) > (b) ? (b) : (a))
49 #define BE_16(x) ((((uint8_t*)(x))[0] << 8) | ((uint8_t*)(x))[1])
51 #define BE_32(x) (((uint32_t)(((uint8_t*)(x))[0]) << 24) | \
52 (((uint8_t*)(x))[1] << 16) | \
53 (((uint8_t*)(x))[2] << 8) | \
56 #define BE_64(x) (((uint64_t)(((uint8_t*)(x))[0]) << 56) | \
57 ((uint64_t)(((uint8_t*)(x))[1]) << 48) | \
58 ((uint64_t)(((uint8_t*)(x))[2]) << 40) | \
59 ((uint64_t)(((uint8_t*)(x))[3]) << 32) | \
60 ((uint64_t)(((uint8_t*)(x))[4]) << 24) | \
61 ((uint64_t)(((uint8_t*)(x))[5]) << 16) | \
62 ((uint64_t)(((uint8_t*)(x))[6]) << 8) | \
63 ((uint64_t)( (uint8_t*)(x))[7]))
65 #define BE_FOURCC(ch0, ch1, ch2, ch3) \
66 ( (uint32_t)(unsigned char)(ch3) | \
67 ((uint32_t)(unsigned char)(ch2) << 8) | \
68 ((uint32_t)(unsigned char)(ch1) << 16) | \
69 ((uint32_t)(unsigned char)(ch0) << 24) )
71 #define QT_ATOM BE_FOURCC
73 #define FREE_ATOM QT_ATOM('f', 'r', 'e', 'e')
74 #define JUNK_ATOM QT_ATOM('j', 'u', 'n', 'k')
75 #define MDAT_ATOM QT_ATOM('m', 'd', 'a', 't')
76 #define MOOV_ATOM QT_ATOM('m', 'o', 'o', 'v')
77 #define PNOT_ATOM QT_ATOM('p', 'n', 'o', 't')
78 #define SKIP_ATOM QT_ATOM('s', 'k', 'i', 'p')
79 #define WIDE_ATOM QT_ATOM('w', 'i', 'd', 'e')
80 #define PICT_ATOM QT_ATOM('P', 'I', 'C', 'T')
81 #define FTYP_ATOM QT_ATOM('f', 't', 'y', 'p')
82 #define UUID_ATOM QT_ATOM('u', 'u', 'i', 'd')
84 #define CMOV_ATOM QT_ATOM('c', 'm', 'o', 'v')
85 #define STCO_ATOM QT_ATOM('s', 't', 'c', 'o')
86 #define CO64_ATOM QT_ATOM('c', 'o', '6', '4')
88 #define ATOM_PREAMBLE_SIZE 8
89 #define COPY_BUFFER_SIZE 33554432
91 int main(
int argc,
char *argv[])
96 uint32_t atom_type = 0;
97 uint64_t atom_size = 0;
98 uint64_t atom_offset = 0;
100 unsigned char *moov_atom =
NULL;
101 unsigned char *ftyp_atom =
NULL;
102 uint64_t moov_atom_size;
103 uint64_t ftyp_atom_size = 0;
105 uint32_t offset_count;
106 uint64_t current_offset;
107 int64_t start_offset = 0;
108 unsigned char *copy_buffer =
NULL;
112 printf(
"Usage: qt-faststart <infile.mov> <outfile.mov>\n"
113 "Note: alternatively you can use -movflags +faststart in ffmpeg\n");
117 if (!strcmp(argv[1], argv[2])) {
118 fprintf(stderr,
"input and output files need to be different\n");
122 infile = fopen(argv[1],
"rb");
130 while (!feof(infile)) {
134 atom_size =
BE_32(&atom_bytes[0]);
135 atom_type =
BE_32(&atom_bytes[4]);
139 ftyp_atom_size = atom_size;
141 ftyp_atom = malloc(ftyp_atom_size);
143 printf(
"could not allocate %"PRIu64
" bytes for ftyp atom\n",
148 fread(ftyp_atom, atom_size, 1, infile) != 1 ||
149 (start_offset = ftello(infile)) < 0) {
156 if (atom_size == 1) {
160 atom_size =
BE_64(&atom_bytes[0]);
170 printf(
"%c%c%c%c %10"PRIu64
" %"PRIu64
"\n",
171 (atom_type >> 24) & 255,
172 (atom_type >> 16) & 255,
173 (atom_type >> 8) & 255,
174 (atom_type >> 0) & 255,
187 printf(
"encountered non-QT top-level atom (is this a QuickTime file?)\n");
190 atom_offset += atom_size;
200 printf(
"last atom in file was not a moov atom\n");
208 if (fseeko(infile, -atom_size, SEEK_END)) {
212 last_offset = ftello(infile);
213 if (last_offset < 0) {
217 moov_atom_size = atom_size;
218 moov_atom = malloc(moov_atom_size);
220 printf(
"could not allocate %"PRIu64
" bytes for moov atom\n", atom_size);
223 if (fread(moov_atom, atom_size, 1, infile) != 1) {
231 printf(
"this utility does not support compressed moov atoms yet\n");
240 for (i = 4; i < moov_atom_size - 4; i++) {
241 atom_type =
BE_32(&moov_atom[i]);
243 printf(
" patching stco atom...\n");
244 atom_size =
BE_32(&moov_atom[i - 4]);
245 if (i + atom_size - 4 > moov_atom_size) {
246 printf(
" bad atom size\n");
249 offset_count =
BE_32(&moov_atom[i + 8]);
250 if (i + 12 + offset_count * UINT64_C(4) > moov_atom_size) {
251 printf(
" bad atom size/element count\n");
254 for (j = 0; j < offset_count; j++) {
255 current_offset =
BE_32(&moov_atom[i + 12 + j * 4]);
256 current_offset += moov_atom_size;
257 moov_atom[i + 12 + j * 4 + 0] = (current_offset >> 24) & 0xFF;
258 moov_atom[i + 12 + j * 4 + 1] = (current_offset >> 16) & 0xFF;
259 moov_atom[i + 12 + j * 4 + 2] = (current_offset >> 8) & 0xFF;
260 moov_atom[i + 12 + j * 4 + 3] = (current_offset >> 0) & 0xFF;
264 printf(
" patching co64 atom...\n");
265 atom_size =
BE_32(&moov_atom[i - 4]);
266 if (i + atom_size - 4 > moov_atom_size) {
267 printf(
" bad atom size\n");
270 offset_count =
BE_32(&moov_atom[i + 8]);
271 if (i + 12 + offset_count * UINT64_C(8) > moov_atom_size) {
272 printf(
" bad atom size/element count\n");
275 for (j = 0; j < offset_count; j++) {
276 current_offset =
BE_64(&moov_atom[i + 12 + j * 8]);
277 current_offset += moov_atom_size;
278 moov_atom[i + 12 + j * 8 + 0] = (current_offset >> 56) & 0xFF;
279 moov_atom[i + 12 + j * 8 + 1] = (current_offset >> 48) & 0xFF;
280 moov_atom[i + 12 + j * 8 + 2] = (current_offset >> 40) & 0xFF;
281 moov_atom[i + 12 + j * 8 + 3] = (current_offset >> 32) & 0xFF;
282 moov_atom[i + 12 + j * 8 + 4] = (current_offset >> 24) & 0xFF;
283 moov_atom[i + 12 + j * 8 + 5] = (current_offset >> 16) & 0xFF;
284 moov_atom[i + 12 + j * 8 + 6] = (current_offset >> 8) & 0xFF;
285 moov_atom[i + 12 + j * 8 + 7] = (current_offset >> 0) & 0xFF;
292 infile = fopen(argv[1],
"rb");
298 if (start_offset > 0) {
299 if (fseeko(infile, start_offset, SEEK_SET)) {
304 last_offset -= start_offset;
307 outfile = fopen(argv[2],
"wb");
314 if (ftyp_atom_size > 0) {
315 printf(
" writing ftyp atom...\n");
316 if (fwrite(ftyp_atom, ftyp_atom_size, 1, outfile) != 1) {
323 printf(
" writing moov atom...\n");
324 if (fwrite(moov_atom, moov_atom_size, 1, outfile) != 1) {
331 copy_buffer = malloc(bytes_to_copy);
333 printf(
"could not allocate %d bytes for copy_buffer\n", bytes_to_copy);
336 printf(
" copying rest of file...\n");
337 while (last_offset) {
338 bytes_to_copy =
MIN(bytes_to_copy, last_offset);
340 if (fread(copy_buffer, bytes_to_copy, 1, infile) != 1) {
344 if (fwrite(copy_buffer, bytes_to_copy, 1, outfile) != 1) {
348 last_offset -= bytes_to_copy;
#define ATOM_PREAMBLE_SIZE
int main(int argc, char *argv[])