30
30
#include <string.h>
33
#define fseeko(x,y,z) fseeko64(x,y,z)
34
#define ftello(x) ftello64(x)
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])
38
#define BE_32(x) ((((uint8_t*)(x))[0] << 24) | \
39
(((uint8_t*)(x))[1] << 16) | \
40
(((uint8_t*)(x))[2] << 8) | \
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) | \
41
42
((uint8_t*)(x))[3])
42
#define BE_64(x) (((uint64_t)(((uint8_t*)(x))[0]) << 56) | \
43
((uint64_t)(((uint8_t*)(x))[1]) << 48) | \
44
((uint64_t)(((uint8_t*)(x))[2]) << 40) | \
45
((uint64_t)(((uint8_t*)(x))[3]) << 32) | \
46
((uint64_t)(((uint8_t*)(x))[4]) << 24) | \
47
((uint64_t)(((uint8_t*)(x))[5]) << 16) | \
48
((uint64_t)(((uint8_t*)(x))[6]) << 8) | \
49
((uint64_t)((uint8_t*)(x))[7]))
51
#define BE_FOURCC( ch0, ch1, ch2, ch3 ) \
52
( (uint32_t)(unsigned char)(ch3) | \
53
( (uint32_t)(unsigned char)(ch2) << 8 ) | \
54
( (uint32_t)(unsigned char)(ch1) << 16 ) | \
55
( (uint32_t)(unsigned char)(ch0) << 24 ) )
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) )
57
59
#define QT_ATOM BE_FOURCC
58
60
/* top level atoms */
71
73
#define STCO_ATOM QT_ATOM('s', 't', 'c', 'o')
72
74
#define CO64_ATOM QT_ATOM('c', 'o', '6', '4')
74
#define ATOM_PREAMBLE_SIZE 8
75
#define COPY_BUFFER_SIZE 1024
76
#define ATOM_PREAMBLE_SIZE 8
77
#define COPY_BUFFER_SIZE 1024
77
79
int main(int argc, char *argv[])
79
81
FILE *infile = NULL;
80
82
FILE *outfile = NULL;
81
83
unsigned char atom_bytes[ATOM_PREAMBLE_SIZE];
82
uint32_t atom_type = 0;
83
uint64_t atom_size = 0;
84
uint32_t atom_type = 0;
85
uint64_t atom_size = 0;
84
86
uint64_t atom_offset = 0;
85
87
uint64_t last_offset;
86
88
unsigned char *moov_atom = NULL;
126
128
ftyp_atom = malloc(ftyp_atom_size);
127
129
if (!ftyp_atom) {
128
printf ("could not allocate %"PRIu64" byte for ftyp atom\n",
130
printf("could not allocate %"PRIu64" bytes for ftyp atom\n",
132
134
fseeko(infile, -ATOM_PREAMBLE_SIZE, SEEK_CUR);
137
139
start_offset = ftello(infile);
140
/* 64-bit special case */
141
if (atom_size == 1) {
142
if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
142
/* 64-bit special case */
143
if (atom_size == 1) {
144
if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
147
atom_size = BE_64(&atom_bytes[0]);
148
fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE * 2, SEEK_CUR);
150
fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE, SEEK_CUR);
145
atom_size = BE_64(&atom_bytes[0]);
146
fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE * 2, SEEK_CUR);
148
fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE, SEEK_CUR);
151
153
printf("%c%c%c%c %10"PRIu64" %"PRIu64"\n",
152
154
(atom_type >> 24) & 255,
153
155
(atom_type >> 16) & 255,
165
167
(atom_type != PICT_ATOM) &&
166
168
(atom_type != UUID_ATOM) &&
167
169
(atom_type != FTYP_ATOM)) {
168
printf ("encountered non-QT top-level atom (is this a Quicktime file?)\n");
170
printf("encountered non-QT top-level atom (is this a QuickTime file?)\n");
171
173
atom_offset += atom_size;
187
189
/* moov atom was, in fact, the last atom in the chunk; load the whole
189
191
fseeko(infile, -atom_size, SEEK_END);
190
last_offset = ftello(infile);
192
last_offset = ftello(infile);
191
193
moov_atom_size = atom_size;
192
moov_atom = malloc(moov_atom_size);
194
moov_atom = malloc(moov_atom_size);
193
195
if (!moov_atom) {
194
printf ("could not allocate %"PRIu64" byte for moov atom\n",
196
printf("could not allocate %"PRIu64" bytes for moov atom\n", atom_size);
198
199
if (fread(moov_atom, atom_size, 1, infile) != 1) {
203
204
/* this utility does not support compressed atoms yet, so disqualify
204
205
* files with compressed QT atoms */
205
206
if (BE_32(&moov_atom[12]) == CMOV_ATOM) {
206
printf ("this utility does not support compressed moov atoms yet\n");
207
printf("this utility does not support compressed moov atoms yet\n");
215
216
for (i = 4; i < moov_atom_size - 4; i++) {
216
217
atom_type = BE_32(&moov_atom[i]);
217
218
if (atom_type == STCO_ATOM) {
218
printf (" patching stco atom...\n");
219
printf(" patching stco atom...\n");
219
220
atom_size = BE_32(&moov_atom[i - 4]);
220
221
if (i + atom_size - 4 > moov_atom_size) {
221
printf (" bad atom size\n");
222
printf(" bad atom size\n");
224
225
offset_count = BE_32(&moov_atom[i + 8]);
225
226
for (j = 0; j < offset_count; j++) {
226
current_offset = BE_32(&moov_atom[i + 12 + j * 4]);
227
current_offset = BE_32(&moov_atom[i + 12 + j * 4]);
227
228
current_offset += moov_atom_size;
228
229
moov_atom[i + 12 + j * 4 + 0] = (current_offset >> 24) & 0xFF;
229
230
moov_atom[i + 12 + j * 4 + 1] = (current_offset >> 16) & 0xFF;
233
234
i += atom_size - 4;
234
235
} else if (atom_type == CO64_ATOM) {
235
printf (" patching co64 atom...\n");
236
printf(" patching co64 atom...\n");
236
237
atom_size = BE_32(&moov_atom[i - 4]);
237
238
if (i + atom_size - 4 > moov_atom_size) {
238
printf (" bad atom size\n");
239
printf(" bad atom size\n");
241
242
offset_count = BE_32(&moov_atom[i + 8]);
242
243
for (j = 0; j < offset_count; j++) {
243
current_offset = BE_64(&moov_atom[i + 12 + j * 8]);
244
current_offset = BE_64(&moov_atom[i + 12 + j * 8]);
244
245
current_offset += moov_atom_size;
245
246
moov_atom[i + 12 + j * 8 + 0] = (current_offset >> 56) & 0xFF;
246
247
moov_atom[i + 12 + j * 8 + 1] = (current_offset >> 48) & 0xFF;
276
277
/* dump the same ftyp atom */
277
278
if (ftyp_atom_size > 0) {
278
printf (" writing ftyp atom...\n");
279
printf(" writing ftyp atom...\n");
279
280
if (fwrite(ftyp_atom, ftyp_atom_size, 1, outfile) != 1) {
285
286
/* dump the new moov atom */
286
printf (" writing moov atom...\n");
287
printf(" writing moov atom...\n");
287
288
if (fwrite(moov_atom, moov_atom_size, 1, outfile) != 1) {
292
293
/* copy the remainder of the infile, from offset 0 -> last_offset - 1 */
293
printf (" copying rest of file...\n");
294
printf(" copying rest of file...\n");
294
295
while (last_offset) {
295
296
if (last_offset > COPY_BUFFER_SIZE)
296
297
bytes_to_copy = COPY_BUFFER_SIZE;