Statistics
| Branch: | Revision:

ffmpeg / qt-faststart.c @ 21c5aca6

History | View | Annotate | Download (10.1 KB)

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 = 0;
73
    uint64_t last_offset;
74
    unsigned char *moov_atom;
75
    unsigned char *ftyp_atom = 0;
76
    uint64_t moov_atom_size;
77
    uint64_t ftyp_atom_size = 0;
78
    uint64_t i, j;
79
    uint32_t offset_count;
80
    uint64_t current_offset;
81
    uint64_t start_offset = 0;
82
    unsigned char copy_buffer[COPY_BUFFER_SIZE];
83
    int bytes_to_copy;
84

    
85
    if (argc != 3) {
86
        printf ("Usage: qt-faststart <infile.mov> <outfile.mov>\n");
87
        return 0;
88
    }
89

    
90
    infile = fopen(argv[1], "rb");
91
    if (!infile) {
92
        perror(argv[1]);
93
        return 1;
94
    }
95

    
96
    /* traverse through the atoms in the file to make sure that 'moov' is
97
     * at the end */
98
    while (!feof(infile)) {
99
        if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
100
            break;
101
        }
102
        atom_size = (uint32_t)BE_32(&atom_bytes[0]);
103
        atom_type = BE_32(&atom_bytes[4]);
104

    
105
        if ((atom_type != FREE_ATOM) &&
106
            (atom_type != JUNK_ATOM) &&
107
            (atom_type != MDAT_ATOM) &&
108
            (atom_type != MOOV_ATOM) &&
109
            (atom_type != PNOT_ATOM) &&
110
            (atom_type != SKIP_ATOM) &&
111
            (atom_type != WIDE_ATOM) &&
112
            (atom_type != PICT_ATOM) &&
113
            (atom_type != FTYP_ATOM)) {
114
            printf ("encountered non-QT top-level atom (is this a Quicktime file?)\n");
115
            break;
116
        }
117

    
118
        /* keep ftyp atom */
119
        if (atom_type == FTYP_ATOM) {
120
            ftyp_atom_size = atom_size;
121
            ftyp_atom = malloc(ftyp_atom_size);
122
            if (!ftyp_atom) {
123
                printf ("could not allocate 0x%llX byte for ftyp atom\n",
124
                        atom_size);
125
                fclose(infile);
126
                return 1;
127
            }
128
            fseeko(infile, -ATOM_PREAMBLE_SIZE, SEEK_CUR);
129
            if (fread(ftyp_atom, atom_size, 1, infile) != 1) {
130
                perror(argv[1]);
131
                free(ftyp_atom);
132
                fclose(infile);
133
                return 1;
134
            }
135
            start_offset = ftello(infile);
136
            continue;
137
        }
138

    
139
        /* 64-bit special case */
140
        if (atom_size == 1) {
141
            if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
142
                break;
143
            }
144
            atom_size = BE_64(&atom_bytes[0]);
145
            fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE * 2, SEEK_CUR);
146
        } else {
147
            fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE, SEEK_CUR);
148
        }
149
    }
150

    
151
    if (atom_type != MOOV_ATOM) {
152
        printf ("last atom in file was not a moov atom\n");
153
        fclose(infile);
154
        return 0;
155
    }
156

    
157
    /* moov atom was, in fact, the last atom in the chunk; load the whole
158
     * moov atom */
159
    fseeko(infile, -atom_size, SEEK_END);
160
    last_offset = ftello(infile);
161
    moov_atom_size = atom_size;
162
    moov_atom = malloc(moov_atom_size);
163
    if (!moov_atom) {
164
        printf ("could not allocate 0x%llX byte for moov atom\n",
165
            atom_size);
166
        fclose(infile);
167
        return 1;
168
    }
169
    if (fread(moov_atom, atom_size, 1, infile) != 1) {
170
        perror(argv[1]);
171
        free(moov_atom);
172
        fclose(infile);
173
        return 1;
174
    }
175

    
176
    /* this utility does not support compressed atoms yet, so disqualify
177
     * files with compressed QT atoms */
178
    if (BE_32(&moov_atom[12]) == CMOV_ATOM) {
179
        printf ("this utility does not support compressed moov atoms yet\n");
180
        free(moov_atom);
181
        fclose(infile);
182
        return 1;
183
    }
184

    
185
    /* close; will be re-opened later */
186
    fclose(infile);
187

    
188
    /* crawl through the moov chunk in search of stco or co64 atoms */
189
    for (i = 4; i < moov_atom_size - 4; i++) {
190
        atom_type = BE_32(&moov_atom[i]);
191
        if (atom_type == STCO_ATOM) {
192
            printf (" patching stco atom...\n");
193
            atom_size = BE_32(&moov_atom[i - 4]);
194
            if (i + atom_size - 4 > moov_atom_size) {
195
                printf (" bad atom size\n");
196
                free(moov_atom);
197
                return 1;
198
            }
199
            offset_count = BE_32(&moov_atom[i + 8]);
200
            for (j = 0; j < offset_count; j++) {
201
                current_offset = BE_32(&moov_atom[i + 12 + j * 4]);
202
                current_offset += moov_atom_size;
203
                moov_atom[i + 12 + j * 4 + 0] = (current_offset >> 24) & 0xFF;
204
                moov_atom[i + 12 + j * 4 + 1] = (current_offset >> 16) & 0xFF;
205
                moov_atom[i + 12 + j * 4 + 2] = (current_offset >>  8) & 0xFF;
206
                moov_atom[i + 12 + j * 4 + 3] = (current_offset >>  0) & 0xFF;
207
            }
208
            i += atom_size - 4;
209
        } else if (atom_type == CO64_ATOM) {
210
            printf (" patching co64 atom...\n");
211
            atom_size = BE_32(&moov_atom[i - 4]);
212
            if (i + atom_size - 4 > moov_atom_size) {
213
                printf (" bad atom size\n");
214
                free(moov_atom);
215
                return 1;
216
            }
217
            offset_count = BE_32(&moov_atom[i + 8]);
218
            for (j = 0; j < offset_count; j++) {
219
                current_offset = BE_64(&moov_atom[i + 12 + j * 8]);
220
                current_offset += moov_atom_size;
221
                moov_atom[i + 12 + j * 8 + 0] = (current_offset >> 56) & 0xFF;
222
                moov_atom[i + 12 + j * 8 + 1] = (current_offset >> 48) & 0xFF;
223
                moov_atom[i + 12 + j * 8 + 2] = (current_offset >> 40) & 0xFF;
224
                moov_atom[i + 12 + j * 8 + 3] = (current_offset >> 32) & 0xFF;
225
                moov_atom[i + 12 + j * 8 + 4] = (current_offset >> 24) & 0xFF;
226
                moov_atom[i + 12 + j * 8 + 5] = (current_offset >> 16) & 0xFF;
227
                moov_atom[i + 12 + j * 8 + 6] = (current_offset >>  8) & 0xFF;
228
                moov_atom[i + 12 + j * 8 + 7] = (current_offset >>  0) & 0xFF;
229
            }
230
            i += atom_size - 4;
231
        }
232
    }
233

    
234
    /* re-open the input file and open the output file */
235
    infile = fopen(argv[1], "rb");
236
    if (!infile) {
237
        perror(argv[1]);
238
        free(moov_atom);
239
        return 1;
240
    }
241
    /* seek after ftyp atom if needed */
242
    fseeko(infile, start_offset, SEEK_SET);
243

    
244
    outfile = fopen(argv[2], "wb");
245
    if (!outfile) {
246
        perror(argv[2]);
247
        fclose(outfile);
248
        free(moov_atom);
249
        return 1;
250
    }
251

    
252
    /* dump the same ftyp atom */
253
    if (ftyp_atom_size > 0) {
254
        printf (" writing ftyp atom...\n");
255
        if (fwrite(ftyp_atom, ftyp_atom_size, 1, outfile) != 1) {
256
            perror(argv[2]);
257
            goto error_out;
258
        }
259
    }
260

    
261
    /* dump the new moov atom */
262
    printf (" writing moov atom...\n");
263
    if (fwrite(moov_atom, moov_atom_size, 1, outfile) != 1) {
264
        perror(argv[2]);
265
        goto error_out;
266
    }
267

    
268
    /* copy the remainder of the infile, from offset 0 -> last_offset - 1 */
269
    printf (" copying rest of file...\n");
270
    while (last_offset) {
271
        if (last_offset > COPY_BUFFER_SIZE)
272
            bytes_to_copy = COPY_BUFFER_SIZE;
273
        else
274
            bytes_to_copy = last_offset;
275

    
276
        if (fread(copy_buffer, bytes_to_copy, 1, infile) != 1) {
277
            perror(argv[1]);
278
            goto error_out;
279
        }
280
        if (fwrite(copy_buffer, bytes_to_copy, 1, outfile) != 1) {
281
            perror(argv[2]);
282
            goto error_out;
283
        }
284

    
285
        last_offset -= bytes_to_copy;
286
    }
287

    
288
    fclose(infile);
289
    fclose(outfile);
290
    free(moov_atom);
291
    if (ftyp_atom_size > 0)
292
        free(ftyp_atom);
293

    
294
    return 0;
295

    
296
error_out:
297
    fclose(infile);
298
    fclose(outfile);
299
    free(moov_atom);
300
    if (ftyp_atom_size > 0)
301
        free(ftyp_atom);
302
    return 1;
303
}