1 | /*
|
---|
2 | * qt-faststart.c, v0.1
|
---|
3 | * by Mike Melanson ([email protected])
|
---|
4 | * This file is placed in the public domain. Use the program however you
|
---|
5 | * see fit.
|
---|
6 | *
|
---|
7 | * This utility rearranges a Quicktime file such that the moov atom
|
---|
8 | * is in front of the data, thus facilitating network streaming.
|
---|
9 | *
|
---|
10 | * Compile this program using:
|
---|
11 | * cc qt-faststart.c -o qt-faststart
|
---|
12 | * Invoke the program with:
|
---|
13 | * qt-faststart <infile.mov> <outfile.mov>
|
---|
14 | *
|
---|
15 | * Notes: Quicktime files can come in many configurations of top-level
|
---|
16 | * atoms. This utility stipulates that the very last atom in the file needs
|
---|
17 | * to be a moov atom. When given such a file, this utility will rearrange
|
---|
18 | * the top-level atoms by shifting the moov atom from the back of the file
|
---|
19 | * to the front, and patch the chunk offsets along the way. This utility
|
---|
20 | * presently only operates on uncompressed moov atoms.
|
---|
21 | */
|
---|
22 |
|
---|
23 | #include <stdio.h>
|
---|
24 | #include <stdlib.h>
|
---|
25 | #include <inttypes.h>
|
---|
26 |
|
---|
27 | #ifdef __MINGW32__
|
---|
28 | #define fseeko(x,y,z) fseeko64(x,y,z)
|
---|
29 | #define ftello(x) ftello64(x)
|
---|
30 | #endif
|
---|
31 |
|
---|
32 | #define BE_16(x) ((((uint8_t*)(x))[0] << 8) | ((uint8_t*)(x))[1])
|
---|
33 | #define BE_32(x) ((((uint8_t*)(x))[0] << 24) | \
|
---|
34 | (((uint8_t*)(x))[1] << 16) | \
|
---|
35 | (((uint8_t*)(x))[2] << 8) | \
|
---|
36 | ((uint8_t*)(x))[3])
|
---|
37 | #define BE_64(x) (((uint64_t)(((uint8_t*)(x))[0]) << 56) | \
|
---|
38 | ((uint64_t)(((uint8_t*)(x))[1]) << 48) | \
|
---|
39 | ((uint64_t)(((uint8_t*)(x))[2]) << 40) | \
|
---|
40 | ((uint64_t)(((uint8_t*)(x))[3]) << 32) | \
|
---|
41 | ((uint64_t)(((uint8_t*)(x))[4]) << 24) | \
|
---|
42 | ((uint64_t)(((uint8_t*)(x))[5]) << 16) | \
|
---|
43 | ((uint64_t)(((uint8_t*)(x))[6]) << 8) | \
|
---|
44 | ((uint64_t)((uint8_t*)(x))[7]))
|
---|
45 |
|
---|
46 | #define BE_FOURCC( ch0, ch1, ch2, ch3 ) \
|
---|
47 | ( (uint32_t)(unsigned char)(ch3) | \
|
---|
48 | ( (uint32_t)(unsigned char)(ch2) << 8 ) | \
|
---|
49 | ( (uint32_t)(unsigned char)(ch1) << 16 ) | \
|
---|
50 | ( (uint32_t)(unsigned char)(ch0) << 24 ) )
|
---|
51 |
|
---|
52 | #define QT_ATOM BE_FOURCC
|
---|
53 | /* top level atoms */
|
---|
54 | #define FREE_ATOM QT_ATOM('f', 'r', 'e', 'e')
|
---|
55 | #define JUNK_ATOM QT_ATOM('j', 'u', 'n', 'k')
|
---|
56 | #define MDAT_ATOM QT_ATOM('m', 'd', 'a', 't')
|
---|
57 | #define MOOV_ATOM QT_ATOM('m', 'o', 'o', 'v')
|
---|
58 | #define PNOT_ATOM QT_ATOM('p', 'n', 'o', 't')
|
---|
59 | #define SKIP_ATOM QT_ATOM('s', 'k', 'i', 'p')
|
---|
60 | #define WIDE_ATOM QT_ATOM('w', 'i', 'd', 'e')
|
---|
61 | #define PICT_ATOM QT_ATOM('P', 'I', 'C', 'T')
|
---|
62 | #define FTYP_ATOM QT_ATOM('f', 't', 'y', 'p')
|
---|
63 |
|
---|
64 | #define CMOV_ATOM QT_ATOM('c', 'm', 'o', 'v')
|
---|
65 | #define STCO_ATOM QT_ATOM('s', 't', 'c', 'o')
|
---|
66 | #define CO64_ATOM QT_ATOM('c', 'o', '6', '4')
|
---|
67 |
|
---|
68 | #define ATOM_PREAMBLE_SIZE 8
|
---|
69 | #define COPY_BUFFER_SIZE 1024
|
---|
70 |
|
---|
71 | int main(int argc, char *argv[])
|
---|
72 | {
|
---|
73 | FILE *infile;
|
---|
74 | FILE *outfile;
|
---|
75 | unsigned char atom_bytes[ATOM_PREAMBLE_SIZE];
|
---|
76 | uint32_t atom_type = 0;
|
---|
77 | uint64_t atom_size = 0;
|
---|
78 | uint64_t last_offset;
|
---|
79 | unsigned char *moov_atom;
|
---|
80 | unsigned char *ftyp_atom = 0;
|
---|
81 | uint64_t moov_atom_size;
|
---|
82 | uint64_t ftyp_atom_size = 0;
|
---|
83 | uint64_t i, j;
|
---|
84 | uint32_t offset_count;
|
---|
85 | uint64_t current_offset;
|
---|
86 | uint64_t start_offset = 0;
|
---|
87 | unsigned char copy_buffer[COPY_BUFFER_SIZE];
|
---|
88 | int bytes_to_copy;
|
---|
89 |
|
---|
90 | if (argc != 3) {
|
---|
91 | printf ("Usage: qt-faststart <infile.mov> <outfile.mov>\n");
|
---|
92 | return 0;
|
---|
93 | }
|
---|
94 |
|
---|
95 | infile = fopen(argv[1], "rb");
|
---|
96 | if (!infile) {
|
---|
97 | perror(argv[1]);
|
---|
98 | return 1;
|
---|
99 | }
|
---|
100 |
|
---|
101 | /* traverse through the atoms in the file to make sure that 'moov' is
|
---|
102 | * at the end */
|
---|
103 | while (!feof(infile)) {
|
---|
104 | if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
|
---|
105 | break;
|
---|
106 | }
|
---|
107 | atom_size = (uint32_t)BE_32(&atom_bytes[0]);
|
---|
108 | atom_type = BE_32(&atom_bytes[4]);
|
---|
109 |
|
---|
110 | if ((atom_type != FREE_ATOM) &&
|
---|
111 | (atom_type != JUNK_ATOM) &&
|
---|
112 | (atom_type != MDAT_ATOM) &&
|
---|
113 | (atom_type != MOOV_ATOM) &&
|
---|
114 | (atom_type != PNOT_ATOM) &&
|
---|
115 | (atom_type != SKIP_ATOM) &&
|
---|
116 | (atom_type != WIDE_ATOM) &&
|
---|
117 | (atom_type != PICT_ATOM) &&
|
---|
118 | (atom_type != FTYP_ATOM)) {
|
---|
119 | printf ("encountered non-QT top-level atom (is this a Quicktime file?)\n");
|
---|
120 | break;
|
---|
121 | }
|
---|
122 |
|
---|
123 | /* keep ftyp atom */
|
---|
124 | if (atom_type == FTYP_ATOM) {
|
---|
125 | ftyp_atom_size = atom_size;
|
---|
126 | ftyp_atom = malloc(ftyp_atom_size);
|
---|
127 | if (!ftyp_atom) {
|
---|
128 | printf ("could not allocate 0x%llX byte for ftyp atom\n",
|
---|
129 | atom_size);
|
---|
130 | fclose(infile);
|
---|
131 | return 1;
|
---|
132 | }
|
---|
133 | fseeko(infile, -ATOM_PREAMBLE_SIZE, SEEK_CUR);
|
---|
134 | if (fread(ftyp_atom, atom_size, 1, infile) != 1) {
|
---|
135 | perror(argv[1]);
|
---|
136 | free(ftyp_atom);
|
---|
137 | fclose(infile);
|
---|
138 | return 1;
|
---|
139 | }
|
---|
140 | start_offset = ftello(infile);
|
---|
141 | continue;
|
---|
142 | }
|
---|
143 |
|
---|
144 | /* 64-bit special case */
|
---|
145 | if (atom_size == 1) {
|
---|
146 | if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
|
---|
147 | break;
|
---|
148 | }
|
---|
149 | atom_size = BE_64(&atom_bytes[0]);
|
---|
150 | fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE * 2, SEEK_CUR);
|
---|
151 | } else {
|
---|
152 | fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE, SEEK_CUR);
|
---|
153 | }
|
---|
154 | }
|
---|
155 |
|
---|
156 | if (atom_type != MOOV_ATOM) {
|
---|
157 | printf ("last atom in file was not a moov atom\n");
|
---|
158 | fclose(infile);
|
---|
159 | return 0;
|
---|
160 | }
|
---|
161 |
|
---|
162 | /* moov atom was, in fact, the last atom in the chunk; load the whole
|
---|
163 | * moov atom */
|
---|
164 | fseeko(infile, -atom_size, SEEK_END);
|
---|
165 | last_offset = ftello(infile);
|
---|
166 | moov_atom_size = atom_size;
|
---|
167 | moov_atom = malloc(moov_atom_size);
|
---|
168 | if (!moov_atom) {
|
---|
169 | printf ("could not allocate 0x%llX byte for moov atom\n",
|
---|
170 | atom_size);
|
---|
171 | fclose(infile);
|
---|
172 | return 1;
|
---|
173 | }
|
---|
174 | if (fread(moov_atom, atom_size, 1, infile) != 1) {
|
---|
175 | perror(argv[1]);
|
---|
176 | free(moov_atom);
|
---|
177 | fclose(infile);
|
---|
178 | return 1;
|
---|
179 | }
|
---|
180 |
|
---|
181 | /* this utility does not support compressed atoms yet, so disqualify
|
---|
182 | * files with compressed QT atoms */
|
---|
183 | if (BE_32(&moov_atom[12]) == CMOV_ATOM) {
|
---|
184 | printf ("this utility does not support compressed moov atoms yet\n");
|
---|
185 | free(moov_atom);
|
---|
186 | fclose(infile);
|
---|
187 | return 1;
|
---|
188 | }
|
---|
189 |
|
---|
190 | /* close; will be re-opened later */
|
---|
191 | fclose(infile);
|
---|
192 |
|
---|
193 | /* crawl through the moov chunk in search of stco or co64 atoms */
|
---|
194 | for (i = 4; i < moov_atom_size - 4; i++) {
|
---|
195 | atom_type = BE_32(&moov_atom[i]);
|
---|
196 | if (atom_type == STCO_ATOM) {
|
---|
197 | printf (" patching stco atom...\n");
|
---|
198 | atom_size = BE_32(&moov_atom[i - 4]);
|
---|
199 | if (i + atom_size - 4 > moov_atom_size) {
|
---|
200 | printf (" bad atom size\n");
|
---|
201 | free(moov_atom);
|
---|
202 | return 1;
|
---|
203 | }
|
---|
204 | offset_count = BE_32(&moov_atom[i + 8]);
|
---|
205 | for (j = 0; j < offset_count; j++) {
|
---|
206 | current_offset = BE_32(&moov_atom[i + 12 + j * 4]);
|
---|
207 | current_offset += moov_atom_size;
|
---|
208 | moov_atom[i + 12 + j * 4 + 0] = (current_offset >> 24) & 0xFF;
|
---|
209 | moov_atom[i + 12 + j * 4 + 1] = (current_offset >> 16) & 0xFF;
|
---|
210 | moov_atom[i + 12 + j * 4 + 2] = (current_offset >> 8) & 0xFF;
|
---|
211 | moov_atom[i + 12 + j * 4 + 3] = (current_offset >> 0) & 0xFF;
|
---|
212 | }
|
---|
213 | i += atom_size - 4;
|
---|
214 | } else if (atom_type == CO64_ATOM) {
|
---|
215 | printf (" patching co64 atom...\n");
|
---|
216 | atom_size = BE_32(&moov_atom[i - 4]);
|
---|
217 | if (i + atom_size - 4 > moov_atom_size) {
|
---|
218 | printf (" bad atom size\n");
|
---|
219 | free(moov_atom);
|
---|
220 | return 1;
|
---|
221 | }
|
---|
222 | offset_count = BE_32(&moov_atom[i + 8]);
|
---|
223 | for (j = 0; j < offset_count; j++) {
|
---|
224 | current_offset = BE_64(&moov_atom[i + 12 + j * 8]);
|
---|
225 | current_offset += moov_atom_size;
|
---|
226 | moov_atom[i + 12 + j * 8 + 0] = (current_offset >> 56) & 0xFF;
|
---|
227 | moov_atom[i + 12 + j * 8 + 1] = (current_offset >> 48) & 0xFF;
|
---|
228 | moov_atom[i + 12 + j * 8 + 2] = (current_offset >> 40) & 0xFF;
|
---|
229 | moov_atom[i + 12 + j * 8 + 3] = (current_offset >> 32) & 0xFF;
|
---|
230 | moov_atom[i + 12 + j * 8 + 4] = (current_offset >> 24) & 0xFF;
|
---|
231 | moov_atom[i + 12 + j * 8 + 5] = (current_offset >> 16) & 0xFF;
|
---|
232 | moov_atom[i + 12 + j * 8 + 6] = (current_offset >> 8) & 0xFF;
|
---|
233 | moov_atom[i + 12 + j * 8 + 7] = (current_offset >> 0) & 0xFF;
|
---|
234 | }
|
---|
235 | i += atom_size - 4;
|
---|
236 | }
|
---|
237 | }
|
---|
238 |
|
---|
239 | /* re-open the input file and open the output file */
|
---|
240 | infile = fopen(argv[1], "rb");
|
---|
241 | if (!infile) {
|
---|
242 | perror(argv[1]);
|
---|
243 | free(moov_atom);
|
---|
244 | return 1;
|
---|
245 | }
|
---|
246 |
|
---|
247 | if (start_offset > 0) { /* seek after ftyp atom */
|
---|
248 | fseeko(infile, start_offset, SEEK_SET);
|
---|
249 | last_offset -= start_offset;
|
---|
250 | }
|
---|
251 |
|
---|
252 | outfile = fopen(argv[2], "wb");
|
---|
253 | if (!outfile) {
|
---|
254 | perror(argv[2]);
|
---|
255 | fclose(outfile);
|
---|
256 | free(moov_atom);
|
---|
257 | return 1;
|
---|
258 | }
|
---|
259 |
|
---|
260 | /* dump the same ftyp atom */
|
---|
261 | if (ftyp_atom_size > 0) {
|
---|
262 | printf (" writing ftyp atom...\n");
|
---|
263 | if (fwrite(ftyp_atom, ftyp_atom_size, 1, outfile) != 1) {
|
---|
264 | perror(argv[2]);
|
---|
265 | goto error_out;
|
---|
266 | }
|
---|
267 | }
|
---|
268 |
|
---|
269 | /* dump the new moov atom */
|
---|
270 | printf (" writing moov atom...\n");
|
---|
271 | if (fwrite(moov_atom, moov_atom_size, 1, outfile) != 1) {
|
---|
272 | perror(argv[2]);
|
---|
273 | goto error_out;
|
---|
274 | }
|
---|
275 |
|
---|
276 | /* copy the remainder of the infile, from offset 0 -> last_offset - 1 */
|
---|
277 | printf (" copying rest of file...\n");
|
---|
278 | while (last_offset) {
|
---|
279 | if (last_offset > COPY_BUFFER_SIZE)
|
---|
280 | bytes_to_copy = COPY_BUFFER_SIZE;
|
---|
281 | else
|
---|
282 | bytes_to_copy = last_offset;
|
---|
283 |
|
---|
284 | if (fread(copy_buffer, bytes_to_copy, 1, infile) != 1) {
|
---|
285 | perror(argv[1]);
|
---|
286 | goto error_out;
|
---|
287 | }
|
---|
288 | if (fwrite(copy_buffer, bytes_to_copy, 1, outfile) != 1) {
|
---|
289 | perror(argv[2]);
|
---|
290 | goto error_out;
|
---|
291 | }
|
---|
292 |
|
---|
293 | last_offset -= bytes_to_copy;
|
---|
294 | }
|
---|
295 |
|
---|
296 | fclose(infile);
|
---|
297 | fclose(outfile);
|
---|
298 | free(moov_atom);
|
---|
299 | if (ftyp_atom_size > 0)
|
---|
300 | free(ftyp_atom);
|
---|
301 |
|
---|
302 | return 0;
|
---|
303 |
|
---|
304 | error_out:
|
---|
305 | fclose(infile);
|
---|
306 | fclose(outfile);
|
---|
307 | free(moov_atom);
|
---|
308 | if (ftyp_atom_size > 0)
|
---|
309 | free(ftyp_atom);
|
---|
310 | return 1;
|
---|
311 | }
|
---|