qt-faststart.c 11.9 KB
Newer Older
1
/*
2
 * qt-faststart.c, v0.2
3 4 5 6 7 8 9
 * by Mike Melanson (melanson@pcisys.net)
 * This file is placed in the public domain. Use the program however you
 * see fit.
 *
 * This utility rearranges a Quicktime file such that the moov atom
 * is in front of the data, thus facilitating network streaming.
 *
10
 * To compile this program, start from the base directory from which you
11
 * are building Libav and type:
12 13 14 15
 *  make tools/qt-faststart
 * The qt-faststart program will be built in the tools/ directory. If you
 * do not build the program in this manner, correct results are not
 * guaranteed, particularly on 64-bit platforms.
16 17 18 19 20 21 22 23 24 25 26 27 28 29
 * Invoke the program with:
 *  qt-faststart <infile.mov> <outfile.mov>
 *
 * Notes: Quicktime files can come in many configurations of top-level
 * atoms. This utility stipulates that the very last atom in the file needs
 * to be a moov atom. When given such a file, this utility will rearrange
 * the top-level atoms by shifting the moov atom from the back of the file
 * to the front, and patch the chunk offsets along the way. This utility
 * presently only operates on uncompressed moov atoms.
 */

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

32
#ifdef __MINGW32__
33 34
#define fseeko(x, y, z) fseeko64(x, y, z)
#define ftello(x)       ftello64(x)
35 36 37
#elif defined(_WIN32)
#define fseeko(x, y, z) _fseeki64(x, y, z)
#define ftello(x)       _ftelli64(x)
38 39
#endif

40 41
#define MIN(a,b) ((a) > (b) ? (b) : (a))

42 43 44 45 46
#define BE_16(x) ((((uint8_t*)(x))[0] <<  8) | ((uint8_t*)(x))[1])

#define BE_32(x) ((((uint8_t*)(x))[0] << 24) |  \
                  (((uint8_t*)(x))[1] << 16) |  \
                  (((uint8_t*)(x))[2] <<  8) |  \
47
                   ((uint8_t*)(x))[3])
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

#define BE_64(x) (((uint64_t)(((uint8_t*)(x))[0]) << 56) |  \
                  ((uint64_t)(((uint8_t*)(x))[1]) << 48) |  \
                  ((uint64_t)(((uint8_t*)(x))[2]) << 40) |  \
                  ((uint64_t)(((uint8_t*)(x))[3]) << 32) |  \
                  ((uint64_t)(((uint8_t*)(x))[4]) << 24) |  \
                  ((uint64_t)(((uint8_t*)(x))[5]) << 16) |  \
                  ((uint64_t)(((uint8_t*)(x))[6]) <<  8) |  \
                  ((uint64_t)( (uint8_t*)(x))[7]))

#define BE_FOURCC(ch0, ch1, ch2, ch3)           \
    ( (uint32_t)(unsigned char)(ch3)        |   \
     ((uint32_t)(unsigned char)(ch2) <<  8) |   \
     ((uint32_t)(unsigned char)(ch1) << 16) |   \
     ((uint32_t)(unsigned char)(ch0) << 24) )
63 64 65 66 67 68 69 70 71 72 73 74

#define QT_ATOM BE_FOURCC
/* top level atoms */
#define FREE_ATOM QT_ATOM('f', 'r', 'e', 'e')
#define JUNK_ATOM QT_ATOM('j', 'u', 'n', 'k')
#define MDAT_ATOM QT_ATOM('m', 'd', 'a', 't')
#define MOOV_ATOM QT_ATOM('m', 'o', 'o', 'v')
#define PNOT_ATOM QT_ATOM('p', 'n', 'o', 't')
#define SKIP_ATOM QT_ATOM('s', 'k', 'i', 'p')
#define WIDE_ATOM QT_ATOM('w', 'i', 'd', 'e')
#define PICT_ATOM QT_ATOM('P', 'I', 'C', 'T')
#define FTYP_ATOM QT_ATOM('f', 't', 'y', 'p')
75
#define UUID_ATOM QT_ATOM('u', 'u', 'i', 'd')
76 77 78 79 80

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

81
#define ATOM_PREAMBLE_SIZE    8
82
#define COPY_BUFFER_SIZE  65536
83 84 85

int main(int argc, char *argv[])
{
86 87
    FILE *infile  = NULL;
    FILE *outfile = NULL;
88
    unsigned char atom_bytes[ATOM_PREAMBLE_SIZE];
89 90
    uint32_t atom_type   = 0;
    uint64_t atom_size   = 0;
91
    uint64_t atom_offset = 0;
92
    int64_t last_offset;
93
    unsigned char *moov_atom = NULL;
94
    unsigned char *ftyp_atom = NULL;
95
    uint64_t moov_atom_size;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
96
    uint64_t ftyp_atom_size = 0;
97 98 99
    uint64_t i, j;
    uint32_t offset_count;
    uint64_t current_offset;
100
    int64_t start_offset = 0;
101 102 103 104
    unsigned char copy_buffer[COPY_BUFFER_SIZE];
    int bytes_to_copy;

    if (argc != 3) {
105
        printf("Usage: qt-faststart <infile.mov> <outfile.mov>\n");
106 107 108
        return 0;
    }

109 110 111 112 113
    if (!strcmp(argv[1], argv[2])) {
        fprintf(stderr, "input and output files need to be different\n");
        return 1;
    }

114 115 116
    infile = fopen(argv[1], "rb");
    if (!infile) {
        perror(argv[1]);
117
        goto error_out;
118 119 120 121 122 123 124 125
    }

    /* traverse through the atoms in the file to make sure that 'moov' is
     * at the end */
    while (!feof(infile)) {
        if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
            break;
        }
126
        atom_size = (uint32_t) BE_32(&atom_bytes[0]);
127 128
        atom_type = BE_32(&atom_bytes[4]);

Baptiste Coudurier's avatar
Baptiste Coudurier committed
129 130 131
        /* keep ftyp atom */
        if (atom_type == FTYP_ATOM) {
            ftyp_atom_size = atom_size;
132
            free(ftyp_atom);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
133 134
            ftyp_atom = malloc(ftyp_atom_size);
            if (!ftyp_atom) {
135 136
                printf("could not allocate %"PRIu64" bytes for ftyp atom\n",
                       atom_size);
137
                goto error_out;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
138
            }
139
            if (fseeko(infile, -ATOM_PREAMBLE_SIZE, SEEK_CUR) ||
140 141
                fread(ftyp_atom, atom_size, 1, infile) != 1 ||
                (start_offset = ftello(infile)) < 0) {
Baptiste Coudurier's avatar
Baptiste Coudurier committed
142
                perror(argv[1]);
143
                goto error_out;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
144
            }
145
        } else {
146
            int ret;
147 148 149 150 151 152
            /* 64-bit special case */
            if (atom_size == 1) {
                if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
                    break;
                }
                atom_size = BE_64(&atom_bytes[0]);
153
                ret = fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE * 2, SEEK_CUR);
154
            } else {
155 156 157 158 159
                ret = fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE, SEEK_CUR);
            }
            if (ret) {
                perror(argv[1]);
                goto error_out;
160 161
            }
        }
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
        printf("%c%c%c%c %10"PRIu64" %"PRIu64"\n",
               (atom_type >> 24) & 255,
               (atom_type >> 16) & 255,
               (atom_type >>  8) & 255,
               (atom_type >>  0) & 255,
               atom_offset,
               atom_size);
        if ((atom_type != FREE_ATOM) &&
            (atom_type != JUNK_ATOM) &&
            (atom_type != MDAT_ATOM) &&
            (atom_type != MOOV_ATOM) &&
            (atom_type != PNOT_ATOM) &&
            (atom_type != SKIP_ATOM) &&
            (atom_type != WIDE_ATOM) &&
            (atom_type != PICT_ATOM) &&
            (atom_type != UUID_ATOM) &&
            (atom_type != FTYP_ATOM)) {
179
            printf("encountered non-QT top-level atom (is this a QuickTime file?)\n");
180 181 182
            break;
        }
        atom_offset += atom_size;
183 184 185 186 187 188

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

    if (atom_type != MOOV_ATOM) {
192
        printf("last atom in file was not a moov atom\n");
193
        free(ftyp_atom);
194 195 196 197 198 199
        fclose(infile);
        return 0;
    }

    /* moov atom was, in fact, the last atom in the chunk; load the whole
     * moov atom */
200 201 202 203
    if (fseeko(infile, -atom_size, SEEK_END)) {
        perror(argv[1]);
        goto error_out;
    }
204
    last_offset    = ftello(infile);
205 206 207 208
    if (last_offset < 0) {
        perror(argv[1]);
        goto error_out;
    }
209
    moov_atom_size = atom_size;
210
    moov_atom      = malloc(moov_atom_size);
211
    if (!moov_atom) {
212
        printf("could not allocate %"PRIu64" bytes for moov atom\n", atom_size);
213
        goto error_out;
214 215 216
    }
    if (fread(moov_atom, atom_size, 1, infile) != 1) {
        perror(argv[1]);
217
        goto error_out;
218 219 220 221 222
    }

    /* this utility does not support compressed atoms yet, so disqualify
     * files with compressed QT atoms */
    if (BE_32(&moov_atom[12]) == CMOV_ATOM) {
223
        printf("this utility does not support compressed moov atoms yet\n");
224
        goto error_out;
225 226 227 228
    }

    /* close; will be re-opened later */
    fclose(infile);
229
    infile = NULL;
230 231 232 233 234

    /* crawl through the moov chunk in search of stco or co64 atoms */
    for (i = 4; i < moov_atom_size - 4; i++) {
        atom_type = BE_32(&moov_atom[i]);
        if (atom_type == STCO_ATOM) {
235
            printf(" patching stco atom...\n");
236 237
            atom_size = BE_32(&moov_atom[i - 4]);
            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 243 244 245
            if (i + 12 + offset_count * UINT64_C(4) > moov_atom_size) {
                printf(" bad atom size/element count\n");
                goto error_out;
            }
246
            for (j = 0; j < offset_count; j++) {
247
                current_offset  = BE_32(&moov_atom[i + 12 + j * 4]);
248 249 250 251 252 253 254 255
                current_offset += moov_atom_size;
                moov_atom[i + 12 + j * 4 + 0] = (current_offset >> 24) & 0xFF;
                moov_atom[i + 12 + j * 4 + 1] = (current_offset >> 16) & 0xFF;
                moov_atom[i + 12 + j * 4 + 2] = (current_offset >>  8) & 0xFF;
                moov_atom[i + 12 + j * 4 + 3] = (current_offset >>  0) & 0xFF;
            }
            i += atom_size - 4;
        } else if (atom_type == CO64_ATOM) {
256
            printf(" patching co64 atom...\n");
257 258
            atom_size = BE_32(&moov_atom[i - 4]);
            if (i + atom_size - 4 > moov_atom_size) {
259
                printf(" bad atom size\n");
260
                goto error_out;
261 262
            }
            offset_count = BE_32(&moov_atom[i + 8]);
263 264 265 266
            if (i + 12 + offset_count * UINT64_C(8) > moov_atom_size) {
                printf(" bad atom size/element count\n");
                goto error_out;
            }
267
            for (j = 0; j < offset_count; j++) {
268
                current_offset  = BE_64(&moov_atom[i + 12 + j * 8]);
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
                current_offset += moov_atom_size;
                moov_atom[i + 12 + j * 8 + 0] = (current_offset >> 56) & 0xFF;
                moov_atom[i + 12 + j * 8 + 1] = (current_offset >> 48) & 0xFF;
                moov_atom[i + 12 + j * 8 + 2] = (current_offset >> 40) & 0xFF;
                moov_atom[i + 12 + j * 8 + 3] = (current_offset >> 32) & 0xFF;
                moov_atom[i + 12 + j * 8 + 4] = (current_offset >> 24) & 0xFF;
                moov_atom[i + 12 + j * 8 + 5] = (current_offset >> 16) & 0xFF;
                moov_atom[i + 12 + j * 8 + 6] = (current_offset >>  8) & 0xFF;
                moov_atom[i + 12 + j * 8 + 7] = (current_offset >>  0) & 0xFF;
            }
            i += atom_size - 4;
        }
    }

    /* re-open the input file and open the output file */
    infile = fopen(argv[1], "rb");
    if (!infile) {
        perror(argv[1]);
287
        goto error_out;
288
    }
289 290

    if (start_offset > 0) { /* seek after ftyp atom */
291 292 293 294 295
        if (fseeko(infile, start_offset, SEEK_SET)) {
            perror(argv[1]);
            goto error_out;
        }

296 297
        last_offset -= start_offset;
    }
Baptiste Coudurier's avatar
Baptiste Coudurier committed
298

299 300 301
    outfile = fopen(argv[2], "wb");
    if (!outfile) {
        perror(argv[2]);
302
        goto error_out;
303 304
    }

Baptiste Coudurier's avatar
Baptiste Coudurier committed
305 306
    /* dump the same ftyp atom */
    if (ftyp_atom_size > 0) {
307
        printf(" writing ftyp atom...\n");
Baptiste Coudurier's avatar
Baptiste Coudurier committed
308 309 310 311 312 313
        if (fwrite(ftyp_atom, ftyp_atom_size, 1, outfile) != 1) {
            perror(argv[2]);
            goto error_out;
        }
    }

314
    /* dump the new moov atom */
315
    printf(" writing moov atom...\n");
316 317 318 319 320 321
    if (fwrite(moov_atom, moov_atom_size, 1, outfile) != 1) {
        perror(argv[2]);
        goto error_out;
    }

    /* copy the remainder of the infile, from offset 0 -> last_offset - 1 */
322
    printf(" copying rest of file...\n");
323
    while (last_offset) {
324
        bytes_to_copy = MIN(COPY_BUFFER_SIZE, last_offset);
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339

        if (fread(copy_buffer, bytes_to_copy, 1, infile) != 1) {
            perror(argv[1]);
            goto error_out;
        }
        if (fwrite(copy_buffer, bytes_to_copy, 1, outfile) != 1) {
            perror(argv[2]);
            goto error_out;
        }
        last_offset -= bytes_to_copy;
    }

    fclose(infile);
    fclose(outfile);
    free(moov_atom);
340
    free(ftyp_atom);
341 342 343 344

    return 0;

error_out:
345
    if (infile)
Martin Storsjö's avatar
Martin Storsjö committed
346
        fclose(infile);
347
    if (outfile)
Martin Storsjö's avatar
Martin Storsjö committed
348
        fclose(outfile);
349
    free(moov_atom);
350
    free(ftyp_atom);
351 352
    return 1;
}