]> git.sesse.net Git - ffmpeg/blob - qt-faststart.c
3a15e22505194d03e567de4bdccb6adc9b0bbc01
[ffmpeg] / qt-faststart.c
1 /*
2  * qt-faststart.c, v0.1
3  * by Mike Melanson (melanson@pcisys.net)
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 #define BE_16(x) ((((uint8_t*)(x))[0] << 8) | ((uint8_t*)(x))[1])
28 #define BE_32(x) ((((uint8_t*)(x))[0] << 24) | \
29                   (((uint8_t*)(x))[1] << 16) | \
30                   (((uint8_t*)(x))[2] << 8) | \
31                    ((uint8_t*)(x))[3])
32 #define BE_64(x) (((uint64_t)(((uint8_t*)(x))[0]) << 56) | \
33                   ((uint64_t)(((uint8_t*)(x))[1]) << 48) | \
34                   ((uint64_t)(((uint8_t*)(x))[2]) << 40) | \
35                   ((uint64_t)(((uint8_t*)(x))[3]) << 32) | \
36                   ((uint64_t)(((uint8_t*)(x))[4]) << 24) | \
37                   ((uint64_t)(((uint8_t*)(x))[5]) << 16) | \
38                   ((uint64_t)(((uint8_t*)(x))[6]) << 8) | \
39                   ((uint64_t)((uint8_t*)(x))[7]))
40
41 #define BE_FOURCC( ch0, ch1, ch2, ch3 )             \
42         ( (uint32_t)(unsigned char)(ch3) |          \
43         ( (uint32_t)(unsigned char)(ch2) << 8 ) |   \
44         ( (uint32_t)(unsigned char)(ch1) << 16 ) |  \
45         ( (uint32_t)(unsigned char)(ch0) << 24 ) )
46
47 #define QT_ATOM BE_FOURCC
48 /* top level atoms */
49 #define FREE_ATOM QT_ATOM('f', 'r', 'e', 'e')
50 #define JUNK_ATOM QT_ATOM('j', 'u', 'n', 'k')
51 #define MDAT_ATOM QT_ATOM('m', 'd', 'a', 't')
52 #define MOOV_ATOM QT_ATOM('m', 'o', 'o', 'v')
53 #define PNOT_ATOM QT_ATOM('p', 'n', 'o', 't')
54 #define SKIP_ATOM QT_ATOM('s', 'k', 'i', 'p')
55 #define WIDE_ATOM QT_ATOM('w', 'i', 'd', 'e')
56 #define PICT_ATOM QT_ATOM('P', 'I', 'C', 'T')
57 #define FTYP_ATOM QT_ATOM('f', 't', 'y', 'p')
58
59 #define CMOV_ATOM QT_ATOM('c', 'm', 'o', 'v')
60 #define STCO_ATOM QT_ATOM('s', 't', 'c', 'o')
61 #define CO64_ATOM QT_ATOM('c', 'o', '6', '4')
62
63 #define ATOM_PREAMBLE_SIZE 8
64 #define COPY_BUFFER_SIZE 1024
65
66 int main(int argc, char *argv[])
67 {
68     FILE *infile;
69     FILE *outfile;
70     unsigned char atom_bytes[ATOM_PREAMBLE_SIZE];
71     uint32_t atom_type = 0;
72     uint64_t atom_size;
73     uint64_t last_offset;
74     unsigned char *moov_atom;
75     uint64_t moov_atom_size;
76     uint64_t i, j;
77     uint32_t offset_count;
78     uint64_t current_offset;
79     unsigned char copy_buffer[COPY_BUFFER_SIZE];
80     int bytes_to_copy;
81
82     if (argc != 3) {
83         printf ("Usage: qt-faststart <infile.mov> <outfile.mov>\n");
84         return 0;
85     }
86
87     infile = fopen(argv[1], "rb");
88     if (!infile) {
89         perror(argv[1]);
90         return 1;
91     }
92
93     /* traverse through the atoms in the file to make sure that 'moov' is
94      * at the end */
95     while (!feof(infile)) {
96         if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
97             break;
98         }
99         atom_size = BE_32(&atom_bytes[0]);
100         atom_type = BE_32(&atom_bytes[4]);
101
102         if ((atom_type != FREE_ATOM) &&
103             (atom_type != JUNK_ATOM) &&
104             (atom_type != MDAT_ATOM) &&
105             (atom_type != MOOV_ATOM) &&
106             (atom_type != PNOT_ATOM) &&
107             (atom_type != SKIP_ATOM) &&
108             (atom_type != WIDE_ATOM) &&
109             (atom_type != PICT_ATOM) &&
110             (atom_type != FTYP_ATOM)) {
111             printf ("encountered non-QT top-level atom (is this a Quicktime file?)\n");
112             break;
113         }
114
115         /* 64-bit special case */
116         if (atom_size == 1) {
117             if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
118                 break;
119             }
120             atom_size = BE_64(&atom_bytes[0]);
121             fseek(infile, atom_size - ATOM_PREAMBLE_SIZE * 2, SEEK_CUR);
122         } else {
123             fseek(infile, atom_size - ATOM_PREAMBLE_SIZE, SEEK_CUR);
124         }
125     }
126
127     if (atom_type != MOOV_ATOM) {
128         printf ("last atom in file was not a moov atom\n");
129         fclose(infile);
130         return 0;
131     }
132
133     /* moov atom was, in fact, the last atom in the chunk; load the whole
134      * moov atom */
135     fseek(infile, -atom_size, SEEK_END);
136     last_offset = (uint64_t)ftell(infile);
137     moov_atom_size = atom_size;
138     moov_atom = malloc(moov_atom_size);
139     if (!moov_atom) {
140         printf ("could not allocate 0x%llX byte for moov atom\n",
141             atom_size);
142         fclose(infile);
143         return 1;
144     }
145     if (fread(moov_atom, atom_size, 1, infile) != 1) {
146         perror(argv[1]);
147         free(moov_atom);
148         fclose(infile);
149         return 1;
150     }
151
152     /* this utility does not support compressed atoms yet, so disqualify
153      * files with compressed QT atoms */
154     if (BE_32(&moov_atom[12]) == CMOV_ATOM) {
155         printf ("this utility does not support compressed moov atoms yet\n");
156         free(moov_atom);
157         fclose(infile);
158         return 1;
159     }
160
161     /* close; will be re-opened later */
162     fclose(infile);
163
164     /* crawl through the moov chunk in search of stco or co64 atoms */
165     for (i = 4; i < moov_atom_size - 4; i++) {
166         atom_type = BE_32(&moov_atom[i]);
167         if (atom_type == STCO_ATOM) {
168             printf (" patching stco atom...\n");
169             atom_size = BE_32(&moov_atom[i - 4]);
170             if (i + atom_size - 4 > moov_atom_size) {
171                 printf (" bad atom size\n");
172                 free(moov_atom);
173                 return 1;
174             }
175             offset_count = BE_32(&moov_atom[i + 8]);
176             for (j = 0; j < offset_count; j++) {
177                 current_offset = BE_32(&moov_atom[i + 12 + j * 4]);
178                 current_offset += moov_atom_size;
179                 moov_atom[i + 12 + j * 4 + 0] = (current_offset >> 24) & 0xFF;
180                 moov_atom[i + 12 + j * 4 + 1] = (current_offset >> 16) & 0xFF;
181                 moov_atom[i + 12 + j * 4 + 2] = (current_offset >>  8) & 0xFF;
182                 moov_atom[i + 12 + j * 4 + 3] = (current_offset >>  0) & 0xFF;
183             }
184             i += atom_size - 4;
185         } else if (atom_type == CO64_ATOM) {
186             printf (" patching co64 atom...\n");
187             atom_size = BE_32(&moov_atom[i - 4]);
188             if (i + atom_size - 4 > moov_atom_size) {
189                 printf (" bad atom size\n");
190                 free(moov_atom);
191                 return 1;
192             }
193             offset_count = BE_32(&moov_atom[i + 8]);
194             for (j = 0; j < offset_count; j++) {
195                 current_offset = BE_64(&moov_atom[i + 12 + j * 8]);
196                 current_offset += moov_atom_size;
197                 moov_atom[i + 12 + j * 8 + 0] = (current_offset >> 56) & 0xFF;
198                 moov_atom[i + 12 + j * 8 + 1] = (current_offset >> 48) & 0xFF;
199                 moov_atom[i + 12 + j * 8 + 2] = (current_offset >> 40) & 0xFF;
200                 moov_atom[i + 12 + j * 8 + 3] = (current_offset >> 32) & 0xFF;
201                 moov_atom[i + 12 + j * 8 + 4] = (current_offset >> 24) & 0xFF;
202                 moov_atom[i + 12 + j * 8 + 5] = (current_offset >> 16) & 0xFF;
203                 moov_atom[i + 12 + j * 8 + 6] = (current_offset >>  8) & 0xFF;
204                 moov_atom[i + 12 + j * 8 + 7] = (current_offset >>  0) & 0xFF;
205             }
206             i += atom_size - 4;
207         }
208     }
209
210     /* re-open the input file and open the output file */
211     infile = fopen(argv[1], "rb");
212     if (!infile) {
213         perror(argv[1]);
214         free(moov_atom);
215         return 1;
216     }
217     outfile = fopen(argv[2], "wb");
218     if (!outfile) {
219         perror(argv[2]);
220         fclose(outfile);
221         free(moov_atom);
222         return 1;
223     }
224
225     /* dump the new moov atom */
226     printf (" writing moov aton...\n");
227     if (fwrite(moov_atom, moov_atom_size, 1, outfile) != 1) {
228         perror(argv[2]);
229         goto error_out;
230     }
231
232     /* copy the remainder of the infile, from offset 0 -> last_offset - 1 */
233     printf (" copying rest of file...\n");
234     while (last_offset) {
235         if (last_offset > COPY_BUFFER_SIZE)
236             bytes_to_copy = COPY_BUFFER_SIZE;
237         else
238             bytes_to_copy = last_offset;
239
240         if (fread(copy_buffer, bytes_to_copy, 1, infile) != 1) {
241             perror(argv[1]);
242             goto error_out;
243         }
244         if (fwrite(copy_buffer, bytes_to_copy, 1, outfile) != 1) {
245             perror(argv[2]);
246             goto error_out;
247         }
248
249         last_offset -= bytes_to_copy;
250     }
251
252     fclose(infile);
253     fclose(outfile);
254     free(moov_atom);
255
256     return 0;
257
258 error_out:
259     fclose(infile);
260     fclose(outfile);
261     free(moov_atom);
262     return 1;
263 }