~siretart/libav/merge.raring.libav-0.8.6

« back to all changes in this revision

Viewing changes to tools/qt-faststart.c

  • Committer: Package Import Robot
  • Author(s): Reinhard Tartler
  • Date: 2012-01-12 22:30:00 UTC
  • mfrom: (1.4.1)
  • mto: (1.3.11 sid) (26.1.1 quantal-security)
  • mto: This revision was merged to the branch mainline in revision 15.
  • Revision ID: package-import@ubuntu.com-20120112223000-s1reiy1e28hnix42
Tags: upstream-0.8~beta2
ImportĀ upstreamĀ versionĀ 0.8~beta2

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
#include <string.h>
31
31
 
32
32
#ifdef __MINGW32__
33
 
#define fseeko(x,y,z)  fseeko64(x,y,z)
34
 
#define ftello(x)      ftello64(x)
 
33
#define fseeko(x, y, z) fseeko64(x, y, z)
 
34
#define ftello(x)       ftello64(x)
35
35
#endif
36
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) | \
 
37
#define BE_16(x) ((((uint8_t*)(x))[0] <<  8) | ((uint8_t*)(x))[1])
 
38
 
 
39
#define BE_32(x) ((((uint8_t*)(x))[0] << 24) |  \
 
40
                  (((uint8_t*)(x))[1] << 16) |  \
 
41
                  (((uint8_t*)(x))[2] <<  8) |  \
41
42
                   ((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 ) )
 
43
 
 
44
#define BE_64(x) (((uint64_t)(((uint8_t*)(x))[0]) << 56) |  \
 
45
                  ((uint64_t)(((uint8_t*)(x))[1]) << 48) |  \
 
46
                  ((uint64_t)(((uint8_t*)(x))[2]) << 40) |  \
 
47
                  ((uint64_t)(((uint8_t*)(x))[3]) << 32) |  \
 
48
                  ((uint64_t)(((uint8_t*)(x))[4]) << 24) |  \
 
49
                  ((uint64_t)(((uint8_t*)(x))[5]) << 16) |  \
 
50
                  ((uint64_t)(((uint8_t*)(x))[6]) <<  8) |  \
 
51
                  ((uint64_t)( (uint8_t*)(x))[7]))
 
52
 
 
53
#define BE_FOURCC(ch0, ch1, ch2, ch3)           \
 
54
    ( (uint32_t)(unsigned char)(ch3)        |   \
 
55
     ((uint32_t)(unsigned char)(ch2) <<  8) |   \
 
56
     ((uint32_t)(unsigned char)(ch1) << 16) |   \
 
57
     ((uint32_t)(unsigned char)(ch0) << 24) )
56
58
 
57
59
#define QT_ATOM BE_FOURCC
58
60
/* top level atoms */
71
73
#define STCO_ATOM QT_ATOM('s', 't', 'c', 'o')
72
74
#define CO64_ATOM QT_ATOM('c', 'o', '6', '4')
73
75
 
74
 
#define ATOM_PREAMBLE_SIZE 8
75
 
#define COPY_BUFFER_SIZE 1024
 
76
#define ATOM_PREAMBLE_SIZE    8
 
77
#define COPY_BUFFER_SIZE   1024
76
78
 
77
79
int main(int argc, char *argv[])
78
80
{
79
81
    FILE *infile  = NULL;
80
82
    FILE *outfile = NULL;
81
83
    unsigned char atom_bytes[ATOM_PREAMBLE_SIZE];
82
 
    uint32_t atom_type = 0;
83
 
    uint64_t atom_size = 0;
 
84
    uint32_t atom_type   = 0;
 
85
    uint64_t atom_size   = 0;
84
86
    uint64_t atom_offset = 0;
85
87
    uint64_t last_offset;
86
88
    unsigned char *moov_atom = NULL;
95
97
    int bytes_to_copy;
96
98
 
97
99
    if (argc != 3) {
98
 
        printf ("Usage: qt-faststart <infile.mov> <outfile.mov>\n");
 
100
        printf("Usage: qt-faststart <infile.mov> <outfile.mov>\n");
99
101
        return 0;
100
102
    }
101
103
 
116
118
        if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
117
119
            break;
118
120
        }
119
 
        atom_size = (uint32_t)BE_32(&atom_bytes[0]);
 
121
        atom_size = (uint32_t) BE_32(&atom_bytes[0]);
120
122
        atom_type = BE_32(&atom_bytes[4]);
121
123
 
122
124
        /* keep ftyp atom */
125
127
            free(ftyp_atom);
126
128
            ftyp_atom = malloc(ftyp_atom_size);
127
129
            if (!ftyp_atom) {
128
 
                printf ("could not allocate %"PRIu64" byte for ftyp atom\n",
129
 
                        atom_size);
 
130
                printf("could not allocate %"PRIu64" bytes for ftyp atom\n",
 
131
                       atom_size);
130
132
                goto error_out;
131
133
            }
132
134
            fseeko(infile, -ATOM_PREAMBLE_SIZE, SEEK_CUR);
137
139
            start_offset = ftello(infile);
138
140
        } else {
139
141
 
140
 
        /* 64-bit special case */
141
 
        if (atom_size == 1) {
142
 
            if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
143
 
                break;
 
142
            /* 64-bit special case */
 
143
            if (atom_size == 1) {
 
144
                if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
 
145
                    break;
 
146
                }
 
147
                atom_size = BE_64(&atom_bytes[0]);
 
148
                fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE * 2, SEEK_CUR);
 
149
            } else {
 
150
                fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE, SEEK_CUR);
144
151
            }
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
152
        }
150
 
    }
151
153
        printf("%c%c%c%c %10"PRIu64" %"PRIu64"\n",
152
154
               (atom_type >> 24) & 255,
153
155
               (atom_type >> 16) & 255,
165
167
            (atom_type != PICT_ATOM) &&
166
168
            (atom_type != UUID_ATOM) &&
167
169
            (atom_type != FTYP_ATOM)) {
168
 
            printf ("encountered non-QT top-level atom (is this a Quicktime file?)\n");
 
170
            printf("encountered non-QT top-level atom (is this a QuickTime file?)\n");
169
171
            break;
170
172
        }
171
173
        atom_offset += atom_size;
178
180
    }
179
181
 
180
182
    if (atom_type != MOOV_ATOM) {
181
 
        printf ("last atom in file was not a moov atom\n");
 
183
        printf("last atom in file was not a moov atom\n");
182
184
        free(ftyp_atom);
183
185
        fclose(infile);
184
186
        return 0;
187
189
    /* moov atom was, in fact, the last atom in the chunk; load the whole
188
190
     * moov atom */
189
191
    fseeko(infile, -atom_size, SEEK_END);
190
 
    last_offset = ftello(infile);
 
192
    last_offset    = ftello(infile);
191
193
    moov_atom_size = atom_size;
192
 
    moov_atom = malloc(moov_atom_size);
 
194
    moov_atom      = malloc(moov_atom_size);
193
195
    if (!moov_atom) {
194
 
        printf ("could not allocate %"PRIu64" byte for moov atom\n",
195
 
            atom_size);
 
196
        printf("could not allocate %"PRIu64" bytes for moov atom\n", atom_size);
196
197
        goto error_out;
197
198
    }
198
199
    if (fread(moov_atom, atom_size, 1, infile) != 1) {
203
204
    /* this utility does not support compressed atoms yet, so disqualify
204
205
     * files with compressed QT atoms */
205
206
    if (BE_32(&moov_atom[12]) == CMOV_ATOM) {
206
 
        printf ("this utility does not support compressed moov atoms yet\n");
 
207
        printf("this utility does not support compressed moov atoms yet\n");
207
208
        goto error_out;
208
209
    }
209
210
 
215
216
    for (i = 4; i < moov_atom_size - 4; i++) {
216
217
        atom_type = BE_32(&moov_atom[i]);
217
218
        if (atom_type == STCO_ATOM) {
218
 
            printf (" patching stco atom...\n");
 
219
            printf(" patching stco atom...\n");
219
220
            atom_size = BE_32(&moov_atom[i - 4]);
220
221
            if (i + atom_size - 4 > moov_atom_size) {
221
 
                printf (" bad atom size\n");
 
222
                printf(" bad atom size\n");
222
223
                goto error_out;
223
224
            }
224
225
            offset_count = BE_32(&moov_atom[i + 8]);
225
226
            for (j = 0; j < offset_count; j++) {
226
 
                current_offset = BE_32(&moov_atom[i + 12 + j * 4]);
 
227
                current_offset  = BE_32(&moov_atom[i + 12 + j * 4]);
227
228
                current_offset += moov_atom_size;
228
229
                moov_atom[i + 12 + j * 4 + 0] = (current_offset >> 24) & 0xFF;
229
230
                moov_atom[i + 12 + j * 4 + 1] = (current_offset >> 16) & 0xFF;
232
233
            }
233
234
            i += atom_size - 4;
234
235
        } else if (atom_type == CO64_ATOM) {
235
 
            printf (" patching co64 atom...\n");
 
236
            printf(" patching co64 atom...\n");
236
237
            atom_size = BE_32(&moov_atom[i - 4]);
237
238
            if (i + atom_size - 4 > moov_atom_size) {
238
 
                printf (" bad atom size\n");
 
239
                printf(" bad atom size\n");
239
240
                goto error_out;
240
241
            }
241
242
            offset_count = BE_32(&moov_atom[i + 8]);
242
243
            for (j = 0; j < offset_count; j++) {
243
 
                current_offset = BE_64(&moov_atom[i + 12 + j * 8]);
 
244
                current_offset  = BE_64(&moov_atom[i + 12 + j * 8]);
244
245
                current_offset += moov_atom_size;
245
246
                moov_atom[i + 12 + j * 8 + 0] = (current_offset >> 56) & 0xFF;
246
247
                moov_atom[i + 12 + j * 8 + 1] = (current_offset >> 48) & 0xFF;
275
276
 
276
277
    /* dump the same ftyp atom */
277
278
    if (ftyp_atom_size > 0) {
278
 
        printf (" writing ftyp atom...\n");
 
279
        printf(" writing ftyp atom...\n");
279
280
        if (fwrite(ftyp_atom, ftyp_atom_size, 1, outfile) != 1) {
280
281
            perror(argv[2]);
281
282
            goto error_out;
283
284
    }
284
285
 
285
286
    /* dump the new moov atom */
286
 
    printf (" writing moov atom...\n");
 
287
    printf(" writing moov atom...\n");
287
288
    if (fwrite(moov_atom, moov_atom_size, 1, outfile) != 1) {
288
289
        perror(argv[2]);
289
290
        goto error_out;
290
291
    }
291
292
 
292
293
    /* copy the remainder of the infile, from offset 0 -> last_offset - 1 */
293
 
    printf (" copying rest of file...\n");
 
294
    printf(" copying rest of file...\n");
294
295
    while (last_offset) {
295
296
        if (last_offset > COPY_BUFFER_SIZE)
296
297
            bytes_to_copy = COPY_BUFFER_SIZE;
305
306
            perror(argv[2]);
306
307
            goto error_out;
307
308
        }
308
 
 
309
309
        last_offset -= bytes_to_copy;
310
310
    }
311
311