Statistics
| Branch: | Revision:

ffmpeg / tools / qt-faststart.c @ 565344e7

History | View | Annotate | Download (11.1 KB)

1
/*
2
 * qt-faststart.c, v0.2
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
 * To compile this program, start from the base directory from which you
11
 * are building FFmpeg and type:
12
 *  make tools/qt-faststart
13
 * The qt-faststart program will be built in the tools/ directory. If you
14
 * do not build the program in this manner, correct results are not
15
 * guaranteed, particularly on 64-bit platforms.
16
 * Invoke the program with:
17
 *  qt-faststart <infile.mov> <outfile.mov>
18
 *
19
 * Notes: Quicktime files can come in many configurations of top-level
20
 * atoms. This utility stipulates that the very last atom in the file needs
21
 * to be a moov atom. When given such a file, this utility will rearrange
22
 * the top-level atoms by shifting the moov atom from the back of the file
23
 * to the front, and patch the chunk offsets along the way. This utility
24
 * presently only operates on uncompressed moov atoms.
25
 */
26

    
27
#include <stdio.h>
28
#include <stdlib.h>
29
#include <inttypes.h>
30
#include <string.h>
31

    
32
#ifdef __MINGW32__
33
#define fseeko(x,y,z)  fseeko64(x,y,z)
34
#define ftello(x)      ftello64(x)
35
#endif
36

    
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) | \
41
                   ((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]))
50

    
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 ) )
56

    
57
#define QT_ATOM BE_FOURCC
58
/* top level atoms */
59
#define FREE_ATOM QT_ATOM('f', 'r', 'e', 'e')
60
#define JUNK_ATOM QT_ATOM('j', 'u', 'n', 'k')
61
#define MDAT_ATOM QT_ATOM('m', 'd', 'a', 't')
62
#define MOOV_ATOM QT_ATOM('m', 'o', 'o', 'v')
63
#define PNOT_ATOM QT_ATOM('p', 'n', 'o', 't')
64
#define SKIP_ATOM QT_ATOM('s', 'k', 'i', 'p')
65
#define WIDE_ATOM QT_ATOM('w', 'i', 'd', 'e')
66
#define PICT_ATOM QT_ATOM('P', 'I', 'C', 'T')
67
#define FTYP_ATOM QT_ATOM('f', 't', 'y', 'p')
68
#define UUID_ATOM QT_ATOM('u', 'u', 'i', 'd')
69

    
70
#define CMOV_ATOM QT_ATOM('c', 'm', 'o', 'v')
71
#define STCO_ATOM QT_ATOM('s', 't', 'c', 'o')
72
#define CO64_ATOM QT_ATOM('c', 'o', '6', '4')
73

    
74
#define ATOM_PREAMBLE_SIZE 8
75
#define COPY_BUFFER_SIZE 1024
76

    
77
int main(int argc, char *argv[])
78
{
79
    FILE *infile  = NULL;
80
    FILE *outfile = NULL;
81
    unsigned char atom_bytes[ATOM_PREAMBLE_SIZE];
82
    uint32_t atom_type = 0;
83
    uint64_t atom_size = 0;
84
    uint64_t atom_offset = 0;
85
    uint64_t last_offset;
86
    unsigned char *moov_atom = NULL;
87
    unsigned char *ftyp_atom = NULL;
88
    uint64_t moov_atom_size;
89
    uint64_t ftyp_atom_size = 0;
90
    uint64_t i, j;
91
    uint32_t offset_count;
92
    uint64_t current_offset;
93
    uint64_t start_offset = 0;
94
    unsigned char copy_buffer[COPY_BUFFER_SIZE];
95
    int bytes_to_copy;
96

    
97
    if (argc != 3) {
98
        printf ("Usage: qt-faststart <infile.mov> <outfile.mov>\n");
99
        return 0;
100
    }
101

    
102
    if (!strcmp(argv[1], argv[2])) {
103
        fprintf(stderr, "input and output files need to be different\n");
104
        return 1;
105
    }
106

    
107
    infile = fopen(argv[1], "rb");
108
    if (!infile) {
109
        perror(argv[1]);
110
        goto error_out;
111
    }
112

    
113
    /* traverse through the atoms in the file to make sure that 'moov' is
114
     * at the end */
115
    while (!feof(infile)) {
116
        if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
117
            break;
118
        }
119
        atom_size = (uint32_t)BE_32(&atom_bytes[0]);
120
        atom_type = BE_32(&atom_bytes[4]);
121

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

    
140
        /* 64-bit special case */
141
        if (atom_size == 1) {
142
            if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
143
                break;
144
            }
145
            atom_size = BE_64(&atom_bytes[0]);
146
            fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE * 2, SEEK_CUR);
147
        } else {
148
            fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE, SEEK_CUR);
149
        }
150
    }
151
        printf("%c%c%c%c %10"PRIu64" %"PRIu64"\n",
152
               (atom_type >> 24) & 255,
153
               (atom_type >> 16) & 255,
154
               (atom_type >>  8) & 255,
155
               (atom_type >>  0) & 255,
156
               atom_offset,
157
               atom_size);
158
        if ((atom_type != FREE_ATOM) &&
159
            (atom_type != JUNK_ATOM) &&
160
            (atom_type != MDAT_ATOM) &&
161
            (atom_type != MOOV_ATOM) &&
162
            (atom_type != PNOT_ATOM) &&
163
            (atom_type != SKIP_ATOM) &&
164
            (atom_type != WIDE_ATOM) &&
165
            (atom_type != PICT_ATOM) &&
166
            (atom_type != UUID_ATOM) &&
167
            (atom_type != FTYP_ATOM)) {
168
            printf ("encountered non-QT top-level atom (is this a Quicktime file?)\n");
169
            break;
170
        }
171
        atom_offset += atom_size;
172

    
173
        /* The atom header is 8 (or 16 bytes), if the atom size (which
174
         * includes these 8 or 16 bytes) is less than that, we won't be
175
         * able to continue scanning sensibly after this atom, so break. */
176
        if (atom_size < 8)
177
            break;
178
    }
179

    
180
    if (atom_type != MOOV_ATOM) {
181
        printf ("last atom in file was not a moov atom\n");
182
        free(ftyp_atom);
183
        fclose(infile);
184
        return 0;
185
    }
186

    
187
    /* moov atom was, in fact, the last atom in the chunk; load the whole
188
     * moov atom */
189
    fseeko(infile, -atom_size, SEEK_END);
190
    last_offset = ftello(infile);
191
    moov_atom_size = atom_size;
192
    moov_atom = malloc(moov_atom_size);
193
    if (!moov_atom) {
194
        printf ("could not allocate %"PRIu64" byte for moov atom\n",
195
            atom_size);
196
        goto error_out;
197
    }
198
    if (fread(moov_atom, atom_size, 1, infile) != 1) {
199
        perror(argv[1]);
200
        goto error_out;
201
    }
202

    
203
    /* this utility does not support compressed atoms yet, so disqualify
204
     * files with compressed QT atoms */
205
    if (BE_32(&moov_atom[12]) == CMOV_ATOM) {
206
        printf ("this utility does not support compressed moov atoms yet\n");
207
        goto error_out;
208
    }
209

    
210
    /* close; will be re-opened later */
211
    fclose(infile);
212
    infile = NULL;
213

    
214
    /* crawl through the moov chunk in search of stco or co64 atoms */
215
    for (i = 4; i < moov_atom_size - 4; i++) {
216
        atom_type = BE_32(&moov_atom[i]);
217
        if (atom_type == STCO_ATOM) {
218
            printf (" patching stco atom...\n");
219
            atom_size = BE_32(&moov_atom[i - 4]);
220
            if (i + atom_size - 4 > moov_atom_size) {
221
                printf (" bad atom size\n");
222
                goto error_out;
223
            }
224
            offset_count = BE_32(&moov_atom[i + 8]);
225
            for (j = 0; j < offset_count; j++) {
226
                current_offset = BE_32(&moov_atom[i + 12 + j * 4]);
227
                current_offset += moov_atom_size;
228
                moov_atom[i + 12 + j * 4 + 0] = (current_offset >> 24) & 0xFF;
229
                moov_atom[i + 12 + j * 4 + 1] = (current_offset >> 16) & 0xFF;
230
                moov_atom[i + 12 + j * 4 + 2] = (current_offset >>  8) & 0xFF;
231
                moov_atom[i + 12 + j * 4 + 3] = (current_offset >>  0) & 0xFF;
232
            }
233
            i += atom_size - 4;
234
        } else if (atom_type == CO64_ATOM) {
235
            printf (" patching co64 atom...\n");
236
            atom_size = BE_32(&moov_atom[i - 4]);
237
            if (i + atom_size - 4 > moov_atom_size) {
238
                printf (" bad atom size\n");
239
                goto error_out;
240
            }
241
            offset_count = BE_32(&moov_atom[i + 8]);
242
            for (j = 0; j < offset_count; j++) {
243
                current_offset = BE_64(&moov_atom[i + 12 + j * 8]);
244
                current_offset += moov_atom_size;
245
                moov_atom[i + 12 + j * 8 + 0] = (current_offset >> 56) & 0xFF;
246
                moov_atom[i + 12 + j * 8 + 1] = (current_offset >> 48) & 0xFF;
247
                moov_atom[i + 12 + j * 8 + 2] = (current_offset >> 40) & 0xFF;
248
                moov_atom[i + 12 + j * 8 + 3] = (current_offset >> 32) & 0xFF;
249
                moov_atom[i + 12 + j * 8 + 4] = (current_offset >> 24) & 0xFF;
250
                moov_atom[i + 12 + j * 8 + 5] = (current_offset >> 16) & 0xFF;
251
                moov_atom[i + 12 + j * 8 + 6] = (current_offset >>  8) & 0xFF;
252
                moov_atom[i + 12 + j * 8 + 7] = (current_offset >>  0) & 0xFF;
253
            }
254
            i += atom_size - 4;
255
        }
256
    }
257

    
258
    /* re-open the input file and open the output file */
259
    infile = fopen(argv[1], "rb");
260
    if (!infile) {
261
        perror(argv[1]);
262
        goto error_out;
263
    }
264

    
265
    if (start_offset > 0) { /* seek after ftyp atom */
266
        fseeko(infile, start_offset, SEEK_SET);
267
        last_offset -= start_offset;
268
    }
269

    
270
    outfile = fopen(argv[2], "wb");
271
    if (!outfile) {
272
        perror(argv[2]);
273
        goto error_out;
274
    }
275

    
276
    /* dump the same ftyp atom */
277
    if (ftyp_atom_size > 0) {
278
        printf (" writing ftyp atom...\n");
279
        if (fwrite(ftyp_atom, ftyp_atom_size, 1, outfile) != 1) {
280
            perror(argv[2]);
281
            goto error_out;
282
        }
283
    }
284

    
285
    /* dump the new moov atom */
286
    printf (" writing moov atom...\n");
287
    if (fwrite(moov_atom, moov_atom_size, 1, outfile) != 1) {
288
        perror(argv[2]);
289
        goto error_out;
290
    }
291

    
292
    /* copy the remainder of the infile, from offset 0 -> last_offset - 1 */
293
    printf (" copying rest of file...\n");
294
    while (last_offset) {
295
        if (last_offset > COPY_BUFFER_SIZE)
296
            bytes_to_copy = COPY_BUFFER_SIZE;
297
        else
298
            bytes_to_copy = last_offset;
299

    
300
        if (fread(copy_buffer, bytes_to_copy, 1, infile) != 1) {
301
            perror(argv[1]);
302
            goto error_out;
303
        }
304
        if (fwrite(copy_buffer, bytes_to_copy, 1, outfile) != 1) {
305
            perror(argv[2]);
306
            goto error_out;
307
        }
308

    
309
        last_offset -= bytes_to_copy;
310
    }
311

    
312
    fclose(infile);
313
    fclose(outfile);
314
    free(moov_atom);
315
    free(ftyp_atom);
316

    
317
    return 0;
318

    
319
error_out:
320
    if (infile)
321
        fclose(infile);
322
    if (outfile)
323
        fclose(outfile);
324
    free(moov_atom);
325
    free(ftyp_atom);
326
    return 1;
327
}