Statistics
| Branch: | Revision:

ffmpeg / libavformat / movenc.c @ 3abe5fbd

History | View | Annotate | Download (55.1 KB)

1
/*
2
 * MOV, 3GP, MP4 muxer
3
 * Copyright (c) 2003 Thomas Raivio.
4
 * Copyright (c) 2004 Gildas Bazin <gbazin at videolan dot org>.
5
 *
6
 * This file is part of FFmpeg.
7
 *
8
 * FFmpeg is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * FFmpeg is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with FFmpeg; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
 */
22
#include "avformat.h"
23
#include "riff.h"
24
#include "avio.h"
25
#include "isom.h"
26

    
27
#undef NDEBUG
28
#include <assert.h>
29

    
30
#define MOV_INDEX_CLUSTER_SIZE 16384
31
#define globalTimescale 1000
32

    
33
#define MODE_MP4 0
34
#define MODE_MOV 1
35
#define MODE_3GP 2
36
#define MODE_PSP 3 // example working PSP command line:
37
// ffmpeg -i testinput.avi  -f psp -r 14.985 -s 320x240 -b 768 -ar 24000 -ab 32 M4V00001.MP4
38
#define MODE_3G2 4
39

    
40
typedef struct MOVIentry {
41
    unsigned int flags, size;
42
    uint64_t     pos;
43
    unsigned int samplesInChunk;
44
    char         key_frame;
45
    unsigned int entries;
46
    int64_t      cts;
47
    int64_t      dts;
48
} MOVIentry;
49

    
50
typedef struct MOVIndex {
51
    int         mode;
52
    int         entry;
53
    long        timescale;
54
    long        time;
55
    int64_t     trackDuration;
56
    long        sampleCount;
57
    long        sampleSize;
58
    int         hasKeyframes;
59
    int         hasBframes;
60
    int         language;
61
    int         trackID;
62
    int         tag;
63
    AVCodecContext *enc;
64

    
65
    int         vosLen;
66
    uint8_t     *vosData;
67
    MOVIentry   *cluster;
68
    int         audio_vbr;
69
} MOVTrack;
70

    
71
typedef struct MOVContext {
72
    int     mode;
73
    int64_t time;
74
    int     nb_streams;
75
    offset_t mdat_pos;
76
    uint64_t mdat_size;
77
    long    timescale;
78
    MOVTrack tracks[MAX_STREAMS];
79
} MOVContext;
80

    
81
//FIXME support 64 bit variant with wide placeholders
82
static offset_t updateSize (ByteIOContext *pb, offset_t pos)
83
{
84
    offset_t curpos = url_ftell(pb);
85
    url_fseek(pb, pos, SEEK_SET);
86
    put_be32(pb, curpos - pos); /* rewrite size */
87
    url_fseek(pb, curpos, SEEK_SET);
88

    
89
    return curpos - pos;
90
}
91

    
92
/* Chunk offset atom */
93
static int mov_write_stco_tag(ByteIOContext *pb, MOVTrack* track)
94
{
95
    int i;
96
    int mode64 = 0; //   use 32 bit size variant if possible
97
    offset_t pos = url_ftell(pb);
98
    put_be32(pb, 0); /* size */
99
    if (pos > UINT32_MAX) {
100
        mode64 = 1;
101
        put_tag(pb, "co64");
102
    } else
103
        put_tag(pb, "stco");
104
    put_be32(pb, 0); /* version & flags */
105
    put_be32(pb, track->entry); /* entry count */
106
    for (i=0; i<track->entry; i++) {
107
        if(mode64 == 1)
108
            put_be64(pb, track->cluster[i].pos);
109
        else
110
            put_be32(pb, track->cluster[i].pos);
111
    }
112
    return updateSize (pb, pos);
113
}
114

    
115
/* Sample size atom */
116
static int mov_write_stsz_tag(ByteIOContext *pb, MOVTrack* track)
117
{
118
    int equalChunks = 1;
119
    int i, j, entries = 0, tst = -1, oldtst = -1;
120

    
121
    offset_t pos = url_ftell(pb);
122
    put_be32(pb, 0); /* size */
123
    put_tag(pb, "stsz");
124
    put_be32(pb, 0); /* version & flags */
125

    
126
    for (i=0; i<track->entry; i++) {
127
        tst = track->cluster[i].size/track->cluster[i].entries;
128
        if(oldtst != -1 && tst != oldtst) {
129
            equalChunks = 0;
130
        }
131
        oldtst = tst;
132
        entries += track->cluster[i].entries;
133
    }
134
    if (equalChunks) {
135
        int sSize = track->cluster[0].size/track->cluster[0].entries;
136
        put_be32(pb, sSize); // sample size
137
        put_be32(pb, entries); // sample count
138
    }
139
    else {
140
        put_be32(pb, 0); // sample size
141
        put_be32(pb, entries); // sample count
142
        for (i=0; i<track->entry; i++) {
143
            for ( j=0; j<track->cluster[i].entries; j++) {
144
                put_be32(pb, track->cluster[i].size /
145
                         track->cluster[i].entries);
146
            }
147
        }
148
    }
149
    return updateSize (pb, pos);
150
}
151

    
152
/* Sample to chunk atom */
153
static int mov_write_stsc_tag(ByteIOContext *pb, MOVTrack* track)
154
{
155
    int index = 0, oldval = -1, i;
156
    offset_t entryPos, curpos;
157

    
158
    offset_t pos = url_ftell(pb);
159
    put_be32(pb, 0); /* size */
160
    put_tag(pb, "stsc");
161
    put_be32(pb, 0); // version & flags
162
    entryPos = url_ftell(pb);
163
    put_be32(pb, track->entry); // entry count
164
    for (i=0; i<track->entry; i++) {
165
        if(oldval != track->cluster[i].samplesInChunk)
166
        {
167
            put_be32(pb, i+1); // first chunk
168
            put_be32(pb, track->cluster[i].samplesInChunk); // samples per chunk
169
            put_be32(pb, 0x1); // sample description index
170
            oldval = track->cluster[i].samplesInChunk;
171
            index++;
172
        }
173
    }
174
    curpos = url_ftell(pb);
175
    url_fseek(pb, entryPos, SEEK_SET);
176
    put_be32(pb, index); // rewrite size
177
    url_fseek(pb, curpos, SEEK_SET);
178

    
179
    return updateSize (pb, pos);
180
}
181

    
182
/* Sync sample atom */
183
static int mov_write_stss_tag(ByteIOContext *pb, MOVTrack* track)
184
{
185
    offset_t curpos, entryPos;
186
    int i, index = 0;
187
    offset_t pos = url_ftell(pb);
188
    put_be32(pb, 0); // size
189
    put_tag(pb, "stss");
190
    put_be32(pb, 0); // version & flags
191
    entryPos = url_ftell(pb);
192
    put_be32(pb, track->entry); // entry count
193
    for (i=0; i<track->entry; i++) {
194
        if(track->cluster[i].key_frame == 1) {
195
            put_be32(pb, i+1);
196
            index++;
197
        }
198
    }
199
    curpos = url_ftell(pb);
200
    url_fseek(pb, entryPos, SEEK_SET);
201
    put_be32(pb, index); // rewrite size
202
    url_fseek(pb, curpos, SEEK_SET);
203
    return updateSize (pb, pos);
204
}
205

    
206
static int mov_write_amr_tag(ByteIOContext *pb, MOVTrack *track)
207
{
208
    put_be32(pb, 0x11); /* size */
209
    if (track->mode == MODE_MOV) put_tag(pb, "samr");
210
    else                         put_tag(pb, "damr");
211
    put_tag(pb, "FFMP");
212
    put_byte(pb, 0); /* decoder version */
213

    
214
    put_be16(pb, 0x81FF); /* Mode set (all modes for AMR_NB) */
215
    put_byte(pb, 0x00); /* Mode change period (no restriction) */
216
    put_byte(pb, 0x01); /* Frames per sample */
217
    return 0x11;
218
}
219

    
220
static int mov_write_enda_tag(ByteIOContext *pb)
221
{
222
    put_be32(pb, 10);
223
    put_tag(pb, "enda");
224
    put_be16(pb, 1); /* little endian */
225
    return 10;
226
}
227

    
228
static unsigned int descrLength(unsigned int len)
229
{
230
    int i;
231
    for(i=1; len>>(7*i); i++);
232
    return len + 1 + i;
233
}
234

    
235
static void putDescr(ByteIOContext *pb, int tag, unsigned int size)
236
{
237
    int i= descrLength(size) - size - 2;
238
    put_byte(pb, tag);
239
    for(; i>0; i--)
240
        put_byte(pb, (size>>(7*i)) | 0x80);
241
    put_byte(pb, size & 0x7F);
242
}
243

    
244
static int mov_write_esds_tag(ByteIOContext *pb, MOVTrack* track) // Basic
245
{
246
    offset_t pos = url_ftell(pb);
247
    int decoderSpecificInfoLen = track->vosLen ? descrLength(track->vosLen):0;
248

    
249
    put_be32(pb, 0);               // size
250
    put_tag(pb, "esds");
251
    put_be32(pb, 0);               // Version
252

    
253
    // ES descriptor
254
    putDescr(pb, 0x03, 3 + descrLength(13 + decoderSpecificInfoLen) +
255
             descrLength(1));
256
    put_be16(pb, track->trackID);
257
    put_byte(pb, 0x00);            // flags (= no flags)
258

    
259
    // DecoderConfig descriptor
260
    putDescr(pb, 0x04, 13 + decoderSpecificInfoLen);
261

    
262
    // Object type indication
263
    put_byte(pb, codec_get_tag(ff_mp4_obj_type, track->enc->codec_id));
264

    
265
    // the following fields is made of 6 bits to identify the streamtype (4 for video, 5 for audio)
266
    // plus 1 bit to indicate upstream and 1 bit set to 1 (reserved)
267
    if(track->enc->codec_type == CODEC_TYPE_AUDIO)
268
        put_byte(pb, 0x15);            // flags (= Audiostream)
269
    else
270
        put_byte(pb, 0x11);            // flags (= Visualstream)
271

    
272
    put_byte(pb,  track->enc->rc_buffer_size>>(3+16));             // Buffersize DB (24 bits)
273
    put_be16(pb, (track->enc->rc_buffer_size>>3)&0xFFFF);          // Buffersize DB
274

    
275
    put_be32(pb, FFMAX(track->enc->bit_rate, track->enc->rc_max_rate));     // maxbitrate  (FIXME should be max rate in any 1 sec window)
276
    if(track->enc->rc_max_rate != track->enc->rc_min_rate || track->enc->rc_min_rate==0)
277
        put_be32(pb, 0);     // vbr
278
    else
279
        put_be32(pb, track->enc->rc_max_rate);     // avg bitrate
280

    
281
    if (track->vosLen)
282
    {
283
        // DecoderSpecific info descriptor
284
        putDescr(pb, 0x05, track->vosLen);
285
        put_buffer(pb, track->vosData, track->vosLen);
286
    }
287

    
288

    
289
    // SL descriptor
290
    putDescr(pb, 0x06, 1);
291
    put_byte(pb, 0x02);
292
    return updateSize (pb, pos);
293
}
294

    
295
static int mov_write_wave_tag(ByteIOContext *pb, MOVTrack* track)
296
{
297
    offset_t pos = url_ftell(pb);
298

    
299
    put_be32(pb, 0);     /* size */
300
    put_tag(pb, "wave");
301

    
302
    put_be32(pb, 12);    /* size */
303
    put_tag(pb, "frma");
304
    put_le32(pb, track->tag);
305

    
306
    if (track->enc->codec_id == CODEC_ID_AAC) {
307
        /* useless atom needed by mplayer, ipod, not needed by quicktime */
308
        put_be32(pb, 12); /* size */
309
        put_tag(pb, "mp4a");
310
        put_be32(pb, 0);
311
        mov_write_esds_tag(pb, track);
312
    } else if (track->enc->codec_id == CODEC_ID_PCM_S24LE ||
313
               track->enc->codec_id == CODEC_ID_PCM_S32LE) {
314
        mov_write_enda_tag(pb);
315
    } else if (track->enc->codec_id == CODEC_ID_AMR_NB) {
316
        mov_write_amr_tag(pb, track);
317
    }
318

    
319
    put_be32(pb, 8);     /* size */
320
    put_be32(pb, 0);     /* null tag */
321

    
322
    return updateSize (pb, pos);
323
}
324

    
325
static int mov_write_glbl_tag(ByteIOContext *pb, MOVTrack* track)
326
{
327
    put_be32(pb, track->vosLen+8);
328
    put_tag(pb, "glbl");
329
    put_buffer(pb, track->vosData, track->vosLen);
330
    return 8+track->vosLen;
331
}
332

    
333
static int mov_write_audio_tag(ByteIOContext *pb, MOVTrack* track)
334
{
335
    offset_t pos = url_ftell(pb);
336
    int version = track->mode == MODE_MOV &&
337
        (track->audio_vbr ||
338
         track->enc->codec_id == CODEC_ID_PCM_S32LE ||
339
         track->enc->codec_id == CODEC_ID_PCM_S24LE);
340

    
341
    put_be32(pb, 0); /* size */
342
    put_le32(pb, track->tag); // store it byteswapped
343
    put_be32(pb, 0); /* Reserved */
344
    put_be16(pb, 0); /* Reserved */
345
    put_be16(pb, 1); /* Data-reference index, XXX  == 1 */
346

    
347
    /* SoundDescription */
348
    put_be16(pb, version); /* Version */
349
    put_be16(pb, 0); /* Revision level */
350
    put_be32(pb, 0); /* Reserved */
351

    
352
    if (track->mode == MODE_MOV) {
353
        put_be16(pb, track->enc->channels);
354
        if (track->enc->codec_id == CODEC_ID_PCM_U8 ||
355
            track->enc->codec_id == CODEC_ID_PCM_S8)
356
            put_be16(pb, 8); /* bits per sample */
357
        else
358
            put_be16(pb, 16);
359
        put_be16(pb, track->audio_vbr ? -2 : 0); /* compression ID */
360
    } else { /* reserved for mp4/3gp */
361
        put_be16(pb, 2);
362
        put_be16(pb, 16);
363
        put_be16(pb, 0);
364
    }
365

    
366
    put_be16(pb, 0); /* packet size (= 0) */
367
    put_be16(pb, track->timescale); /* Time scale */
368
    put_be16(pb, 0); /* Reserved */
369

    
370
    if(version == 1) { /* SoundDescription V1 extended info */
371
        put_be32(pb, track->enc->frame_size); /* Samples per packet */
372
        put_be32(pb, track->sampleSize / track->enc->channels); /* Bytes per packet */
373
        put_be32(pb, track->sampleSize); /* Bytes per frame */
374
        put_be32(pb, 2); /* Bytes per sample */
375
    }
376

    
377
    if(track->mode == MODE_MOV &&
378
       (track->enc->codec_id == CODEC_ID_AAC ||
379
        track->enc->codec_id == CODEC_ID_AMR_NB ||
380
        track->enc->codec_id == CODEC_ID_PCM_S24LE ||
381
        track->enc->codec_id == CODEC_ID_PCM_S32LE))
382
        mov_write_wave_tag(pb, track);
383
    else if(track->enc->codec_id == CODEC_ID_AAC)
384
        mov_write_esds_tag(pb, track);
385
    else if(track->enc->codec_id == CODEC_ID_AMR_NB)
386
        mov_write_amr_tag(pb, track);
387
    else if(track->vosLen > 0)
388
        mov_write_glbl_tag(pb, track);
389

    
390
    return updateSize (pb, pos);
391
}
392

    
393
static int mov_write_d263_tag(ByteIOContext *pb)
394
{
395
    put_be32(pb, 0xf); /* size */
396
    put_tag(pb, "d263");
397
    put_tag(pb, "FFMP");
398
    put_byte(pb, 0); /* decoder version */
399
    /* FIXME use AVCodecContext level/profile, when encoder will set values */
400
    put_byte(pb, 0xa); /* level */
401
    put_byte(pb, 0); /* profile */
402
    return 0xf;
403
}
404

    
405
/* TODO: No idea about these values */
406
static int mov_write_svq3_tag(ByteIOContext *pb)
407
{
408
    put_be32(pb, 0x15);
409
    put_tag(pb, "SMI ");
410
    put_tag(pb, "SEQH");
411
    put_be32(pb, 0x5);
412
    put_be32(pb, 0xe2c0211d);
413
    put_be32(pb, 0xc0000000);
414
    put_byte(pb, 0);
415
    return 0x15;
416
}
417

    
418
static uint8_t *avc_find_startcode( uint8_t *p, uint8_t *end )
419
{
420
    uint8_t *a = p + 4 - ((long)p & 3);
421

    
422
    for( end -= 3; p < a && p < end; p++ ) {
423
        if( p[0] == 0 && p[1] == 0 && p[2] == 1 )
424
            return p;
425
    }
426

    
427
    for( end -= 3; p < end; p += 4 ) {
428
        uint32_t x = *(uint32_t*)p;
429
//      if( (x - 0x01000100) & (~x) & 0x80008000 ) // little endian
430
//      if( (x - 0x00010001) & (~x) & 0x00800080 ) // big endian
431
        if( (x - 0x01010101) & (~x) & 0x80808080 ) { // generic
432
            if( p[1] == 0 ) {
433
                if( p[0] == 0 && p[2] == 1 )
434
                    return p-1;
435
                if( p[2] == 0 && p[3] == 1 )
436
                    return p;
437
            }
438
            if( p[3] == 0 ) {
439
                if( p[2] == 0 && p[4] == 1 )
440
                    return p+1;
441
                if( p[4] == 0 && p[5] == 1 )
442
                    return p+2;
443
            }
444
        }
445
    }
446

    
447
    for( end += 3; p < end; p++ ) {
448
        if( p[0] == 0 && p[1] == 0 && p[2] == 1 )
449
            return p;
450
    }
451

    
452
    return end + 3;
453
}
454

    
455
static int avc_parse_nal_units(uint8_t **buf, int *size)
456
{
457
    ByteIOContext *pb;
458
    uint8_t *p = *buf;
459
    uint8_t *end = p + *size;
460
    uint8_t *nal_start, *nal_end;
461
    int ret = url_open_dyn_buf(&pb);
462
    if(ret < 0)
463
        return ret;
464

    
465
    nal_start = avc_find_startcode(p, end);
466
    while (nal_start < end) {
467
        while(!*(nal_start++));
468
        nal_end = avc_find_startcode(nal_start, end);
469
        put_be32(pb, nal_end - nal_start);
470
        put_buffer(pb, nal_start, nal_end - nal_start);
471
        nal_start = nal_end;
472
    }
473
    av_freep(buf);
474
    *size = url_close_dyn_buf(pb, buf);
475
    return 0;
476
}
477

    
478
static int mov_write_avcc_tag(ByteIOContext *pb, MOVTrack *track)
479
{
480
    offset_t pos = url_ftell(pb);
481

    
482
    put_be32(pb, 0);
483
    put_tag(pb, "avcC");
484
    if (track->vosLen > 6) {
485
        /* check for h264 start code */
486
        if (AV_RB32(track->vosData) == 0x00000001) {
487
            uint8_t *buf, *end;
488
            uint32_t sps_size=0, pps_size=0;
489
            uint8_t *sps=0, *pps=0;
490

    
491
            avc_parse_nal_units(&track->vosData, &track->vosLen);
492
            buf = track->vosData;
493
            end = track->vosData + track->vosLen;
494

    
495
            /* look for sps and pps */
496
            while (buf < end) {
497
                unsigned int size;
498
                uint8_t nal_type;
499
                size = AV_RB32(buf);
500
                nal_type = buf[4] & 0x1f;
501
                if (nal_type == 7) { /* SPS */
502
                    sps = buf + 4;
503
                    sps_size = size;
504
                } else if (nal_type == 8) { /* PPS */
505
                    pps = buf + 4;
506
                    pps_size = size;
507
                }
508
                buf += size + 4;
509
            }
510
            assert(sps);
511
            assert(pps);
512

    
513
            put_byte(pb, 1); /* version */
514
            put_byte(pb, sps[1]); /* profile */
515
            put_byte(pb, sps[2]); /* profile compat */
516
            put_byte(pb, sps[3]); /* level */
517
            put_byte(pb, 0xff); /* 6 bits reserved (111111) + 2 bits nal size length - 1 (11) */
518
            put_byte(pb, 0xe1); /* 3 bits reserved (111) + 5 bits number of sps (00001) */
519

    
520
            put_be16(pb, sps_size);
521
            put_buffer(pb, sps, sps_size);
522
            put_byte(pb, 1); /* number of pps */
523
            put_be16(pb, pps_size);
524
            put_buffer(pb, pps, pps_size);
525
        } else {
526
            put_buffer(pb, track->vosData, track->vosLen);
527
        }
528
    }
529
    return updateSize(pb, pos);
530
}
531

    
532
/* also used by all avid codecs (dv, imx, meridien) and their variants */
533
static int mov_write_avid_tag(ByteIOContext *pb, MOVTrack *track)
534
{
535
    int i;
536
    put_be32(pb, 24); /* size */
537
    put_tag(pb, "ACLR");
538
    put_tag(pb, "ACLR");
539
    put_tag(pb, "0001");
540
    put_be32(pb, 1); /* yuv 1 / rgb 2 ? */
541
    put_be32(pb, 0); /* unknown */
542

    
543
    put_be32(pb, 24); /* size */
544
    put_tag(pb, "APRG");
545
    put_tag(pb, "APRG");
546
    put_tag(pb, "0001");
547
    put_be32(pb, 1); /* unknown */
548
    put_be32(pb, 0); /* unknown */
549

    
550
    put_be32(pb, 120); /* size */
551
    put_tag(pb, "ARES");
552
    put_tag(pb, "ARES");
553
    put_tag(pb, "0001");
554
    put_be32(pb, AV_RB32(track->vosData + 0x28)); /* dnxhd cid, some id ? */
555
    put_be32(pb, track->enc->width);
556
    /* values below are based on samples created with quicktime and avid codecs */
557
    if (track->vosData[5] & 2) { // interlaced
558
        put_be32(pb, track->enc->height/2);
559
        put_be32(pb, 2); /* unknown */
560
        put_be32(pb, 0); /* unknown */
561
        put_be32(pb, 4); /* unknown */
562
    } else {
563
        put_be32(pb, track->enc->height);
564
        put_be32(pb, 1); /* unknown */
565
        put_be32(pb, 0); /* unknown */
566
        if (track->enc->height == 1080)
567
            put_be32(pb, 5); /* unknown */
568
        else
569
            put_be32(pb, 6); /* unknown */
570
    }
571
    /* padding */
572
    for (i = 0; i < 10; i++)
573
        put_be64(pb, 0);
574

    
575
    /* extra padding for stsd needed */
576
    put_be32(pb, 0);
577
    return 0;
578
}
579

    
580
static int mov_find_video_codec_tag(AVFormatContext *s, MOVTrack *track)
581
{
582
    int tag = track->enc->codec_tag;
583
    if (!tag) {
584
        if (track->enc->codec_id == CODEC_ID_DVVIDEO) {
585
            if (track->enc->height == 480) { /* NTSC */
586
                if (track->enc->pix_fmt == PIX_FMT_YUV422P)
587
                    tag = MKTAG('d', 'v', '5', 'n');
588
                else
589
                    tag = MKTAG('d', 'v', 'c', ' ');
590
            } else { /* assume PAL */
591
                if (track->enc->pix_fmt == PIX_FMT_YUV422P)
592
                    tag = MKTAG('d', 'v', '5', 'p');
593
                else if (track->enc->pix_fmt == PIX_FMT_YUV420P)
594
                    tag = MKTAG('d', 'v', 'c', 'p');
595
                else
596
                    tag = MKTAG('d', 'v', 'p', 'p');
597
            }
598
        } else if (track->enc->codec_id == CODEC_ID_H263) {
599
            if (track->mode == MODE_MOV)
600
                tag = MKTAG('h', '2', '6', '3');
601
            else
602
                tag = MKTAG('s', '2', '6', '3');
603
        } else {
604
            tag = codec_get_tag(codec_movvideo_tags, track->enc->codec_id);
605
        }
606
    }
607
    // if no mac fcc found, try with Microsoft tags
608
    if (!tag) {
609
        tag = codec_get_tag(codec_bmp_tags, track->enc->codec_id);
610
        if (tag) {
611
            av_log(s, AV_LOG_INFO, "Warning, using MS style video codec tag, the file may be unplayable!\n");
612
        }
613
    }
614
    assert(tag);
615
    return tag;
616
}
617

    
618
static int mov_find_audio_codec_tag(AVFormatContext *s, MOVTrack *track)
619
{
620
    int tag = track->enc->codec_tag;
621
    if (!tag) {
622
        tag = codec_get_tag(codec_movaudio_tags, track->enc->codec_id);
623
    }
624
    // if no mac fcc found, try with Microsoft tags
625
    if (!tag) {
626
        int ms_tag = codec_get_tag(codec_wav_tags, track->enc->codec_id);
627
        if (ms_tag) {
628
            tag = MKTAG('m', 's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
629
            av_log(s, AV_LOG_INFO, "Warning, using MS style audio codec tag, the file may be unplayable!\n");
630
        }
631
    }
632
    assert(tag);
633
    return tag;
634
}
635

    
636
static int mov_write_video_tag(ByteIOContext *pb, MOVTrack* track)
637
{
638
    offset_t pos = url_ftell(pb);
639
    char compressor_name[32];
640

    
641
    put_be32(pb, 0); /* size */
642
    put_le32(pb, track->tag); // store it byteswapped
643
    put_be32(pb, 0); /* Reserved */
644
    put_be16(pb, 0); /* Reserved */
645
    put_be16(pb, 1); /* Data-reference index */
646

    
647
    put_be16(pb, 0); /* Codec stream version */
648
    put_be16(pb, 0); /* Codec stream revision (=0) */
649
    if (track->mode == MODE_MOV) {
650
        put_tag(pb, "FFMP"); /* Vendor */
651
        if(track->enc->codec_id == CODEC_ID_RAWVIDEO) {
652
            put_be32(pb, 0); /* Temporal Quality */
653
            put_be32(pb, 0x400); /* Spatial Quality = lossless*/
654
        } else {
655
            put_be32(pb, 0x200); /* Temporal Quality = normal */
656
            put_be32(pb, 0x200); /* Spatial Quality = normal */
657
        }
658
    } else {
659
        put_be32(pb, 0); /* Reserved */
660
        put_be32(pb, 0); /* Reserved */
661
        put_be32(pb, 0); /* Reserved */
662
    }
663
    put_be16(pb, track->enc->width); /* Video width */
664
    put_be16(pb, track->enc->height); /* Video height */
665
    put_be32(pb, 0x00480000); /* Horizontal resolution 72dpi */
666
    put_be32(pb, 0x00480000); /* Vertical resolution 72dpi */
667
    put_be32(pb, 0); /* Data size (= 0) */
668
    put_be16(pb, 1); /* Frame count (= 1) */
669

    
670
    memset(compressor_name,0,32);
671
    /* FIXME not sure, ISO 14496-1 draft where it shall be set to 0 */
672
    if (track->mode == MODE_MOV && track->enc->codec && track->enc->codec->name)
673
        strncpy(compressor_name,track->enc->codec->name,31);
674
    put_byte(pb, strlen(compressor_name));
675
    put_buffer(pb, compressor_name, 31);
676

    
677
    put_be16(pb, 0x18); /* Reserved */
678
    put_be16(pb, 0xffff); /* Reserved */
679
    if(track->enc->codec_id == CODEC_ID_MPEG4)
680
        mov_write_esds_tag(pb, track);
681
    else if(track->enc->codec_id == CODEC_ID_H263)
682
        mov_write_d263_tag(pb);
683
    else if(track->enc->codec_id == CODEC_ID_SVQ3)
684
        mov_write_svq3_tag(pb);
685
    else if(track->enc->codec_id == CODEC_ID_H264)
686
        mov_write_avcc_tag(pb, track);
687
    else if(track->enc->codec_id == CODEC_ID_DNXHD)
688
        mov_write_avid_tag(pb, track);
689
    else if(track->vosLen > 0)
690
        mov_write_glbl_tag(pb, track);
691

    
692
    return updateSize (pb, pos);
693
}
694

    
695
static int mov_write_stsd_tag(ByteIOContext *pb, MOVTrack* track)
696
{
697
    offset_t pos = url_ftell(pb);
698
    put_be32(pb, 0); /* size */
699
    put_tag(pb, "stsd");
700
    put_be32(pb, 0); /* version & flags */
701
    put_be32(pb, 1); /* entry count */
702
    if (track->enc->codec_type == CODEC_TYPE_VIDEO)
703
        mov_write_video_tag(pb, track);
704
    else if (track->enc->codec_type == CODEC_TYPE_AUDIO)
705
        mov_write_audio_tag(pb, track);
706
    return updateSize(pb, pos);
707
}
708

    
709
static int mov_write_ctts_tag(ByteIOContext *pb, MOVTrack* track)
710
{
711
    MOV_stts_t *ctts_entries;
712
    uint32_t entries = 0;
713
    uint32_t atom_size;
714
    int i;
715

    
716
    ctts_entries = av_malloc((track->entry + 1) * sizeof(*ctts_entries)); /* worst case */
717
    ctts_entries[0].count = 1;
718
    ctts_entries[0].duration = track->cluster[0].cts;
719
    for (i=1; i<track->entry; i++) {
720
        if (track->cluster[i].cts == ctts_entries[entries].duration) {
721
            ctts_entries[entries].count++; /* compress */
722
        } else {
723
            entries++;
724
            ctts_entries[entries].duration = track->cluster[i].cts;
725
            ctts_entries[entries].count = 1;
726
        }
727
    }
728
    entries++; /* last one */
729
    atom_size = 16 + (entries * 8);
730
    put_be32(pb, atom_size); /* size */
731
    put_tag(pb, "ctts");
732
    put_be32(pb, 0); /* version & flags */
733
    put_be32(pb, entries); /* entry count */
734
    for (i=0; i<entries; i++) {
735
        put_be32(pb, ctts_entries[i].count);
736
        put_be32(pb, ctts_entries[i].duration);
737
    }
738
    av_free(ctts_entries);
739
    return atom_size;
740
}
741

    
742
/* Time to sample atom */
743
static int mov_write_stts_tag(ByteIOContext *pb, MOVTrack* track)
744
{
745
    MOV_stts_t *stts_entries;
746
    uint32_t entries = -1;
747
    uint32_t atom_size;
748
    int i;
749

    
750
    if (track->enc->codec_type == CODEC_TYPE_AUDIO && !track->audio_vbr) {
751
        stts_entries = av_malloc(sizeof(*stts_entries)); /* one entry */
752
        stts_entries[0].count = track->sampleCount;
753
        stts_entries[0].duration = 1;
754
        entries = 1;
755
    } else {
756
        stts_entries = av_malloc(track->entry * sizeof(*stts_entries)); /* worst case */
757
        for (i=0; i<track->entry; i++) {
758
            int64_t duration = i + 1 == track->entry ?
759
                track->trackDuration - track->cluster[i].dts + track->cluster[0].dts : /* readjusting */
760
                track->cluster[i+1].dts - track->cluster[i].dts;
761
            if (i && duration == stts_entries[entries].duration) {
762
                stts_entries[entries].count++; /* compress */
763
            } else {
764
                entries++;
765
                stts_entries[entries].duration = duration;
766
                stts_entries[entries].count = 1;
767
            }
768
        }
769
        entries++; /* last one */
770
    }
771
    atom_size = 16 + (entries * 8);
772
    put_be32(pb, atom_size); /* size */
773
    put_tag(pb, "stts");
774
    put_be32(pb, 0); /* version & flags */
775
    put_be32(pb, entries); /* entry count */
776
    for (i=0; i<entries; i++) {
777
        put_be32(pb, stts_entries[i].count);
778
        put_be32(pb, stts_entries[i].duration);
779
    }
780
    av_free(stts_entries);
781
    return atom_size;
782
}
783

    
784
static int mov_write_dref_tag(ByteIOContext *pb)
785
{
786
    put_be32(pb, 28); /* size */
787
    put_tag(pb, "dref");
788
    put_be32(pb, 0); /* version & flags */
789
    put_be32(pb, 1); /* entry count */
790

    
791
    put_be32(pb, 0xc); /* size */
792
    put_tag(pb, "url ");
793
    put_be32(pb, 1); /* version & flags */
794

    
795
    return 28;
796
}
797

    
798
static int mov_write_stbl_tag(ByteIOContext *pb, MOVTrack* track)
799
{
800
    offset_t pos = url_ftell(pb);
801
    put_be32(pb, 0); /* size */
802
    put_tag(pb, "stbl");
803
    mov_write_stsd_tag(pb, track);
804
    mov_write_stts_tag(pb, track);
805
    if (track->enc->codec_type == CODEC_TYPE_VIDEO &&
806
        track->hasKeyframes < track->entry)
807
        mov_write_stss_tag(pb, track);
808
    if (track->enc->codec_type == CODEC_TYPE_VIDEO &&
809
        track->hasBframes)
810
        mov_write_ctts_tag(pb, track);
811
    mov_write_stsc_tag(pb, track);
812
    mov_write_stsz_tag(pb, track);
813
    mov_write_stco_tag(pb, track);
814
    return updateSize(pb, pos);
815
}
816

    
817
static int mov_write_dinf_tag(ByteIOContext *pb)
818
{
819
    offset_t pos = url_ftell(pb);
820
    put_be32(pb, 0); /* size */
821
    put_tag(pb, "dinf");
822
    mov_write_dref_tag(pb);
823
    return updateSize(pb, pos);
824
}
825

    
826
static int mov_write_smhd_tag(ByteIOContext *pb)
827
{
828
    put_be32(pb, 16); /* size */
829
    put_tag(pb, "smhd");
830
    put_be32(pb, 0); /* version & flags */
831
    put_be16(pb, 0); /* reserved (balance, normally = 0) */
832
    put_be16(pb, 0); /* reserved */
833
    return 16;
834
}
835

    
836
static int mov_write_vmhd_tag(ByteIOContext *pb)
837
{
838
    put_be32(pb, 0x14); /* size (always 0x14) */
839
    put_tag(pb, "vmhd");
840
    put_be32(pb, 0x01); /* version & flags */
841
    put_be64(pb, 0); /* reserved (graphics mode = copy) */
842
    return 0x14;
843
}
844

    
845
static int mov_write_hdlr_tag(ByteIOContext *pb, MOVTrack* track)
846
{
847
    const char *descr, *hdlr, *hdlr_type;
848
    offset_t pos = url_ftell(pb);
849

    
850
    if (!track) { /* no media --> data handler */
851
        hdlr = "dhlr";
852
        hdlr_type = "url ";
853
        descr = "DataHandler";
854
    } else {
855
        hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
856
        if (track->enc->codec_type == CODEC_TYPE_VIDEO) {
857
            hdlr_type = "vide";
858
            descr = "VideoHandler";
859
        } else {
860
            hdlr_type = "soun";
861
            descr = "SoundHandler";
862
        }
863
    }
864

    
865
    put_be32(pb, 0); /* size */
866
    put_tag(pb, "hdlr");
867
    put_be32(pb, 0); /* Version & flags */
868
    put_buffer(pb, hdlr, 4); /* handler */
869
    put_tag(pb, hdlr_type); /* handler type */
870
    put_be32(pb ,0); /* reserved */
871
    put_be32(pb ,0); /* reserved */
872
    put_be32(pb ,0); /* reserved */
873
    put_byte(pb, strlen(descr)); /* string counter */
874
    put_buffer(pb, descr, strlen(descr)); /* handler description */
875
    return updateSize(pb, pos);
876
}
877

    
878
static int mov_write_minf_tag(ByteIOContext *pb, MOVTrack* track)
879
{
880
    offset_t pos = url_ftell(pb);
881
    put_be32(pb, 0); /* size */
882
    put_tag(pb, "minf");
883
    if(track->enc->codec_type == CODEC_TYPE_VIDEO)
884
        mov_write_vmhd_tag(pb);
885
    else
886
        mov_write_smhd_tag(pb);
887
    if (track->mode == MODE_MOV) /* FIXME: Why do it for MODE_MOV only ? */
888
        mov_write_hdlr_tag(pb, NULL);
889
    mov_write_dinf_tag(pb);
890
    mov_write_stbl_tag(pb, track);
891
    return updateSize(pb, pos);
892
}
893

    
894
static int mov_write_mdhd_tag(ByteIOContext *pb, MOVTrack* track)
895
{
896
    int version = track->trackDuration < INT32_MAX ? 0 : 1;
897

    
898
    (version == 1) ? put_be32(pb, 44) : put_be32(pb, 32); /* size */
899
    put_tag(pb, "mdhd");
900
    put_byte(pb, version);
901
    put_be24(pb, 0); /* flags */
902
    if (version == 1) {
903
        put_be64(pb, track->time);
904
        put_be64(pb, track->time);
905
    } else {
906
        put_be32(pb, track->time); /* creation time */
907
        put_be32(pb, track->time); /* modification time */
908
    }
909
    put_be32(pb, track->timescale); /* time scale (sample rate for audio) */
910
    (version == 1) ? put_be64(pb, track->trackDuration) : put_be32(pb, track->trackDuration); /* duration */
911
    put_be16(pb, track->language); /* language */
912
    put_be16(pb, 0); /* reserved (quality) */
913

    
914
    if(version!=0 && track->mode == MODE_MOV){
915
        av_log(NULL, AV_LOG_ERROR,
916
            "FATAL error, file duration too long for timebase, this file will not be\n"
917
            "playable with quicktime. Choose a different timebase or a different\n"
918
            "container format\n");
919
    }
920

    
921
    return 32;
922
}
923

    
924
static int mov_write_mdia_tag(ByteIOContext *pb, MOVTrack* track)
925
{
926
    offset_t pos = url_ftell(pb);
927
    put_be32(pb, 0); /* size */
928
    put_tag(pb, "mdia");
929
    mov_write_mdhd_tag(pb, track);
930
    mov_write_hdlr_tag(pb, track);
931
    mov_write_minf_tag(pb, track);
932
    return updateSize(pb, pos);
933
}
934

    
935
static int mov_write_tkhd_tag(ByteIOContext *pb, MOVTrack* track)
936
{
937
    int64_t duration = av_rescale_rnd(track->trackDuration, globalTimescale, track->timescale, AV_ROUND_UP);
938
    int version = duration < INT32_MAX ? 0 : 1;
939

    
940
    (version == 1) ? put_be32(pb, 104) : put_be32(pb, 92); /* size */
941
    put_tag(pb, "tkhd");
942
    put_byte(pb, version);
943
    put_be24(pb, 0xf); /* flags (track enabled) */
944
    if (version == 1) {
945
        put_be64(pb, track->time);
946
        put_be64(pb, track->time);
947
    } else {
948
        put_be32(pb, track->time); /* creation time */
949
        put_be32(pb, track->time); /* modification time */
950
    }
951
    put_be32(pb, track->trackID); /* track-id */
952
    put_be32(pb, 0); /* reserved */
953
    (version == 1) ? put_be64(pb, duration) : put_be32(pb, duration);
954

    
955
    put_be32(pb, 0); /* reserved */
956
    put_be32(pb, 0); /* reserved */
957
    put_be32(pb, 0x0); /* reserved (Layer & Alternate group) */
958
    /* Volume, only for audio */
959
    if(track->enc->codec_type == CODEC_TYPE_AUDIO)
960
        put_be16(pb, 0x0100);
961
    else
962
        put_be16(pb, 0);
963
    put_be16(pb, 0); /* reserved */
964

    
965
    /* Matrix structure */
966
    put_be32(pb, 0x00010000); /* reserved */
967
    put_be32(pb, 0x0); /* reserved */
968
    put_be32(pb, 0x0); /* reserved */
969
    put_be32(pb, 0x0); /* reserved */
970
    put_be32(pb, 0x00010000); /* reserved */
971
    put_be32(pb, 0x0); /* reserved */
972
    put_be32(pb, 0x0); /* reserved */
973
    put_be32(pb, 0x0); /* reserved */
974
    put_be32(pb, 0x40000000); /* reserved */
975

    
976
    /* Track width and height, for visual only */
977
    if(track->enc->codec_type == CODEC_TYPE_VIDEO) {
978
        double sample_aspect_ratio = av_q2d(track->enc->sample_aspect_ratio);
979
        if( !sample_aspect_ratio ) sample_aspect_ratio = 1;
980
        put_be32(pb, sample_aspect_ratio * track->enc->width*0x10000);
981
        put_be32(pb, track->enc->height*0x10000);
982
    }
983
    else {
984
        put_be32(pb, 0);
985
        put_be32(pb, 0);
986
    }
987
    return 0x5c;
988
}
989

    
990
// This box seems important for the psp playback ... without it the movie seems to hang
991
static int mov_write_edts_tag(ByteIOContext *pb, MOVTrack *track)
992
{
993
    put_be32(pb, 0x24); /* size  */
994
    put_tag(pb, "edts");
995
    put_be32(pb, 0x1c); /* size  */
996
    put_tag(pb, "elst");
997
    put_be32(pb, 0x0);
998
    put_be32(pb, 0x1);
999

    
1000
    put_be32(pb, av_rescale_rnd(track->trackDuration, globalTimescale, track->timescale, AV_ROUND_UP)); /* duration   ... doesn't seem to effect psp */
1001

    
1002
    put_be32(pb, track->cluster[0].cts); /* first pts is cts since dts is 0 */
1003
    put_be32(pb, 0x00010000);
1004
    return 0x24;
1005
}
1006

    
1007
// goes at the end of each track!  ... Critical for PSP playback ("Incompatible data" without it)
1008
static int mov_write_uuid_tag_psp(ByteIOContext *pb, MOVTrack *mov)
1009
{
1010
    put_be32(pb, 0x34); /* size ... reports as 28 in mp4box! */
1011
    put_tag(pb, "uuid");
1012
    put_tag(pb, "USMT");
1013
    put_be32(pb, 0x21d24fce);
1014
    put_be32(pb, 0xbb88695c);
1015
    put_be32(pb, 0xfac9c740);
1016
    put_be32(pb, 0x1c);     // another size here!
1017
    put_tag(pb, "MTDT");
1018
    put_be32(pb, 0x00010012);
1019
    put_be32(pb, 0x0a);
1020
    put_be32(pb, 0x55c40000);
1021
    put_be32(pb, 0x1);
1022
    put_be32(pb, 0x0);
1023
    return 0x34;
1024
}
1025

    
1026
static int mov_write_trak_tag(ByteIOContext *pb, MOVTrack* track)
1027
{
1028
    offset_t pos = url_ftell(pb);
1029
    put_be32(pb, 0); /* size */
1030
    put_tag(pb, "trak");
1031
    mov_write_tkhd_tag(pb, track);
1032
    if (track->mode == MODE_PSP || track->hasBframes)
1033
        mov_write_edts_tag(pb, track);  // PSP Movies require edts box
1034
    mov_write_mdia_tag(pb, track);
1035
    if (track->mode == MODE_PSP)
1036
        mov_write_uuid_tag_psp(pb,track);  // PSP Movies require this uuid box
1037
    return updateSize(pb, pos);
1038
}
1039

    
1040
#if 0
1041
/* TODO: Not sorted out, but not necessary either */
1042
static int mov_write_iods_tag(ByteIOContext *pb, MOVContext *mov)
1043
{
1044
    put_be32(pb, 0x15); /* size */
1045
    put_tag(pb, "iods");
1046
    put_be32(pb, 0);    /* version & flags */
1047
    put_be16(pb, 0x1007);
1048
    put_byte(pb, 0);
1049
    put_be16(pb, 0x4fff);
1050
    put_be16(pb, 0xfffe);
1051
    put_be16(pb, 0x01ff);
1052
    return 0x15;
1053
}
1054
#endif
1055

    
1056
static int mov_write_mvhd_tag(ByteIOContext *pb, MOVContext *mov)
1057
{
1058
    int maxTrackID = 1, i;
1059
    int64_t maxTrackLenTemp, maxTrackLen = 0;
1060
    int version;
1061

    
1062
    for (i=0; i<mov->nb_streams; i++) {
1063
        if(mov->tracks[i].entry > 0) {
1064
            maxTrackLenTemp = av_rescale_rnd(mov->tracks[i].trackDuration, globalTimescale, mov->tracks[i].timescale, AV_ROUND_UP);
1065
            if(maxTrackLen < maxTrackLenTemp)
1066
                maxTrackLen = maxTrackLenTemp;
1067
            if(maxTrackID < mov->tracks[i].trackID)
1068
                maxTrackID = mov->tracks[i].trackID;
1069
        }
1070
    }
1071

    
1072
    version = maxTrackLen < UINT32_MAX ? 0 : 1;
1073
    (version == 1) ? put_be32(pb, 120) : put_be32(pb, 108); /* size */
1074
    put_tag(pb, "mvhd");
1075
    put_byte(pb, version);
1076
    put_be24(pb, 0); /* flags */
1077
    if (version == 1) {
1078
        put_be64(pb, mov->time);
1079
        put_be64(pb, mov->time);
1080
    } else {
1081
        put_be32(pb, mov->time); /* creation time */
1082
        put_be32(pb, mov->time); /* modification time */
1083
    }
1084
    put_be32(pb, mov->timescale); /* timescale */
1085
    (version == 1) ? put_be64(pb, maxTrackLen) : put_be32(pb, maxTrackLen); /* duration of longest track */
1086

    
1087
    put_be32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
1088
    put_be16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
1089
    put_be16(pb, 0); /* reserved */
1090
    put_be32(pb, 0); /* reserved */
1091
    put_be32(pb, 0); /* reserved */
1092

    
1093
    /* Matrix structure */
1094
    put_be32(pb, 0x00010000); /* reserved */
1095
    put_be32(pb, 0x0); /* reserved */
1096
    put_be32(pb, 0x0); /* reserved */
1097
    put_be32(pb, 0x0); /* reserved */
1098
    put_be32(pb, 0x00010000); /* reserved */
1099
    put_be32(pb, 0x0); /* reserved */
1100
    put_be32(pb, 0x0); /* reserved */
1101
    put_be32(pb, 0x0); /* reserved */
1102
    put_be32(pb, 0x40000000); /* reserved */
1103

    
1104
    put_be32(pb, 0); /* reserved (preview time) */
1105
    put_be32(pb, 0); /* reserved (preview duration) */
1106
    put_be32(pb, 0); /* reserved (poster time) */
1107
    put_be32(pb, 0); /* reserved (selection time) */
1108
    put_be32(pb, 0); /* reserved (selection duration) */
1109
    put_be32(pb, 0); /* reserved (current time) */
1110
    put_be32(pb, maxTrackID+1); /* Next track id */
1111
    return 0x6c;
1112
}
1113

    
1114
static int mov_write_itunes_hdlr_tag(ByteIOContext *pb, MOVContext* mov,
1115
                                     AVFormatContext *s)
1116
{
1117
    offset_t pos = url_ftell(pb);
1118
    put_be32(pb, 0); /* size */
1119
    put_tag(pb, "hdlr");
1120
    put_be32(pb, 0);
1121
    put_be32(pb, 0);
1122
    put_tag(pb, "mdir");
1123
    put_tag(pb, "appl");
1124
    put_be32(pb, 0);
1125
    put_be32(pb, 0);
1126
    put_be16(pb, 0);
1127
    return updateSize(pb, pos);
1128
}
1129

    
1130
/* helper function to write a data tag with the specified string as data */
1131
static int mov_write_string_data_tag(ByteIOContext *pb, const char *data, int long_style)
1132
{
1133
    if(long_style){
1134
        offset_t pos = url_ftell(pb);
1135
        put_be32(pb, 0); /* size */
1136
        put_tag(pb, "data");
1137
        put_be32(pb, 1);
1138
        put_be32(pb, 0);
1139
        put_buffer(pb, data, strlen(data));
1140
        return updateSize(pb, pos);
1141
    }else{
1142
        put_be16(pb, strlen(data)); /* string length */
1143
        put_be16(pb, 0);
1144
        put_buffer(pb, data, strlen(data));
1145
        return strlen(data) + 4;
1146
    }
1147
}
1148

    
1149
static int mov_write_string_tag(ByteIOContext *pb, const char *name, const char *value, int long_style){
1150
    int size = 0;
1151
    if ( value && value[0] ) {
1152
        offset_t pos = url_ftell(pb);
1153
        put_be32(pb, 0); /* size */
1154
        put_tag(pb, name);
1155
        mov_write_string_data_tag(pb, value, long_style);
1156
        size= updateSize(pb, pos);
1157
    }
1158
    return size;
1159
}
1160

    
1161
/* iTunes year */
1162
static int mov_write_day_tag(ByteIOContext *pb, int year, int long_style)
1163
{
1164
    if(year){
1165
        char year_str[5];
1166
        snprintf(year_str, sizeof(year_str), "%04d", year);
1167
        return mov_write_string_tag(pb, "\251day", year_str, long_style);
1168
    }else
1169
        return 0;
1170
}
1171

    
1172
/* iTunes track number */
1173
static int mov_write_trkn_tag(ByteIOContext *pb, MOVContext* mov,
1174
                              AVFormatContext *s)
1175
{
1176
    int size = 0;
1177
    if ( s->track ) {
1178
        offset_t pos = url_ftell(pb);
1179
        put_be32(pb, 0); /* size */
1180
        put_tag(pb, "trkn");
1181
        {
1182
            offset_t pos = url_ftell(pb);
1183
            put_be32(pb, 0); /* size */
1184
            put_tag(pb, "data");
1185
            put_be32(pb, 0);        // 8 bytes empty
1186
            put_be32(pb, 0);
1187
            put_be16(pb, 0);        // empty
1188
            put_be16(pb, s->track); // track number
1189
            put_be16(pb, 0);        // total track number
1190
            put_be16(pb, 0);        // empty
1191
            updateSize(pb, pos);
1192
        }
1193
        size = updateSize(pb, pos);
1194
    }
1195
    return size;
1196
}
1197

    
1198
/* iTunes meta data list */
1199
static int mov_write_ilst_tag(ByteIOContext *pb, MOVContext* mov,
1200
                              AVFormatContext *s)
1201
{
1202
    offset_t pos = url_ftell(pb);
1203
    put_be32(pb, 0); /* size */
1204
    put_tag(pb, "ilst");
1205
    mov_write_string_tag(pb, "\251nam", s->title         , 1);
1206
    mov_write_string_tag(pb, "\251ART", s->author        , 1);
1207
    mov_write_string_tag(pb, "\251wrt", s->author        , 1);
1208
    mov_write_string_tag(pb, "\251alb", s->album         , 1);
1209
    mov_write_day_tag(pb, s->year ,1);
1210
    if(mov->tracks[0].enc && !(mov->tracks[0].enc->flags & CODEC_FLAG_BITEXACT))
1211
        mov_write_string_tag(pb, "\251too", LIBAVFORMAT_IDENT, 1);
1212
    mov_write_string_tag(pb, "\251cmt", s->comment       , 1);
1213
    mov_write_string_tag(pb, "\251gen", s->genre         , 1);
1214
    mov_write_trkn_tag(pb, mov, s);
1215
    return updateSize(pb, pos);
1216
}
1217

    
1218
/* iTunes meta data tag */
1219
static int mov_write_meta_tag(ByteIOContext *pb, MOVContext* mov,
1220
                              AVFormatContext *s)
1221
{
1222
    int size = 0;
1223

    
1224
    // only save meta tag if required
1225
    if ( s->title[0] || s->author[0] || s->album[0] || s->year ||
1226
         s->comment[0] || s->genre[0] || s->track ) {
1227
        offset_t pos = url_ftell(pb);
1228
        put_be32(pb, 0); /* size */
1229
        put_tag(pb, "meta");
1230
        put_be32(pb, 0);
1231
        mov_write_itunes_hdlr_tag(pb, mov, s);
1232
        mov_write_ilst_tag(pb, mov, s);
1233
        size = updateSize(pb, pos);
1234
    }
1235
    return size;
1236
}
1237

    
1238
static int mov_write_udta_tag(ByteIOContext *pb, MOVContext* mov,
1239
                              AVFormatContext *s)
1240
{
1241
    int i, req = 0;
1242

    
1243
    /* Requirements */
1244
    for (i=0; i<mov->nb_streams; i++) {
1245
        if(mov->tracks[i].entry <= 0) continue;
1246
        if (mov->tracks[i].enc->codec_id == CODEC_ID_AAC ||
1247
            mov->tracks[i].enc->codec_id == CODEC_ID_MPEG4) {
1248
            req = 1;
1249
            break;
1250
        }
1251
    }
1252

    
1253
    if (s->title[0]   || s->author[0] || s->album[0] || s->year ||
1254
        s->comment[0] || s->genre[0]  || s->track ||
1255
       (mov->mode == MODE_MOV &&
1256
      ((mov->tracks[0].enc && !mov->tracks[0].enc->flags & CODEC_FLAG_BITEXACT) || req))) {
1257
        offset_t pos = url_ftell(pb);
1258

    
1259
        put_be32(pb, 0); /* size */
1260
        put_tag(pb, "udta");
1261

    
1262
        /* iTunes meta data */
1263
        mov_write_meta_tag(pb, mov, s);
1264

    
1265
        if(mov->mode == MODE_MOV){ // the title field breaks gtkpod with mp4 and my suspicion is that stuff is not valid in mp4
1266
            /* Requirements */
1267
            if (req)
1268
                mov_write_string_tag(pb, "\251req", "QuickTime 6.0 or greater", 0);
1269

    
1270
            mov_write_string_tag(pb, "\251nam", s->title         , 0);
1271
            mov_write_string_tag(pb, "\251aut", s->author        , 0);
1272
            mov_write_string_tag(pb, "\251alb", s->album         , 0);
1273
            mov_write_day_tag(pb, s->year, 0);
1274
            if(mov->tracks[0].enc && !(mov->tracks[0].enc->flags & CODEC_FLAG_BITEXACT))
1275
                mov_write_string_tag(pb, "\251enc", LIBAVFORMAT_IDENT, 0);
1276
            mov_write_string_tag(pb, "\251des", s->comment       , 0);
1277
            mov_write_string_tag(pb, "\251gen", s->genre         , 0);
1278
        }
1279

    
1280
        return updateSize(pb, pos);
1281
    }
1282

    
1283
    return 0;
1284
}
1285

    
1286
static int utf8len(uint8_t *b){
1287
    int len=0;
1288
    int val;
1289
    while(*b){
1290
        GET_UTF8(val, *b++, return -1;)
1291
        len++;
1292
    }
1293
    return len;
1294
}
1295

    
1296
static int ascii_to_wc (ByteIOContext *pb, uint8_t *b)
1297
{
1298
    int val;
1299
    while(*b){
1300
        GET_UTF8(val, *b++, return -1;)
1301
        put_be16(pb, val);
1302
    }
1303
    put_be16(pb, 0x00);
1304
    return 0;
1305
}
1306

    
1307
static uint16_t language_code (const char *str)
1308
{
1309
    return ((((str[0]-0x60) & 0x1F)<<10) + (((str[1]-0x60) & 0x1F)<<5) + ((str[2]-0x60) & 0x1F));
1310
}
1311

    
1312
static int mov_write_uuidusmt_tag (ByteIOContext *pb, AVFormatContext *s)
1313
{
1314
    size_t len, size;
1315
    offset_t pos, curpos;
1316

    
1317
    size = 0;
1318
    if (s->title[0]) {
1319
        pos = url_ftell(pb);
1320
        put_be32(pb, 0); /* size placeholder*/
1321
        put_tag(pb, "uuid");
1322
        put_tag(pb, "USMT");
1323
        put_be32(pb, 0x21d24fce ); /* 96 bit UUID */
1324
        put_be32(pb, 0xbb88695c );
1325
        put_be32(pb, 0xfac9c740 );
1326
        size += 24;
1327

    
1328
        put_be32(pb, 0); /* size placeholder*/
1329
        put_tag(pb, "MTDT");
1330
        put_be16(pb, 4);
1331
        size += 10;
1332

    
1333
        // ?
1334
        put_be16(pb, 0x0C);                 /* size */
1335
        put_be32(pb, 0x0B);                 /* type */
1336
        put_be16(pb, language_code("und")); /* language */
1337
        put_be16(pb, 0x0);                  /* ? */
1338
        put_be16(pb, 0x021C);               /* data */
1339
        size += 12;
1340

    
1341
        // Encoder
1342
        len = utf8len(LIBAVCODEC_IDENT)+1;
1343
        if(len<=0)
1344
            goto not_utf8;
1345
        put_be16(pb, len*2+10);             /* size */
1346
        put_be32(pb, 0x04);                 /* type */
1347
        put_be16(pb, language_code("eng")); /* language */
1348
        put_be16(pb, 0x01);                 /* ? */
1349
        ascii_to_wc(pb, LIBAVCODEC_IDENT);
1350
        size += len*2+10;
1351

    
1352
        // Title
1353
        len = utf8len(s->title)+1;
1354
        if(len<=0)
1355
            goto not_utf8;
1356
        put_be16(pb, len*2+10);             /* size */
1357
        put_be32(pb, 0x01);                 /* type */
1358
        put_be16(pb, language_code("eng")); /* language */
1359
        put_be16(pb, 0x01);                 /* ? */
1360
        ascii_to_wc (pb, s->title);
1361
        size += len*2+10;
1362

    
1363
        // Date
1364
//        snprintf(dt,32,"%04d/%02d/%02d %02d:%02d:%02d",t_st->tm_year+1900,t_st->tm_mon+1,t_st->tm_mday,t_st->tm_hour,t_st->tm_min,t_st->tm_sec);
1365
        len = utf8len("2006/04/01 11:11:11")+1;
1366
        if(len<=0)
1367
            goto not_utf8;
1368
        put_be16(pb, len*2+10);    /* size */
1369
        put_be32(pb, 0x03);        /* type */
1370
        put_be16(pb, language_code("und")); /* language */
1371
        put_be16(pb, 0x01);        /* ? */
1372
        ascii_to_wc (pb, "2006/04/01 11:11:11");
1373
        size += len*2+10;
1374

    
1375
        // size
1376
        curpos = url_ftell(pb);
1377
        url_fseek(pb, pos, SEEK_SET);
1378
        put_be32(pb, size);
1379
        url_fseek(pb, pos+24, SEEK_SET);
1380
        put_be32(pb, size-24);
1381
        url_fseek(pb, curpos, SEEK_SET);
1382
    }
1383

    
1384
    return size;
1385
not_utf8:
1386
    av_log(s, AV_LOG_ERROR, "not utf8\n");
1387
    return -1;
1388
}
1389

    
1390
static int mov_write_moov_tag(ByteIOContext *pb, MOVContext *mov,
1391
                              AVFormatContext *s)
1392
{
1393
    int i;
1394
    offset_t pos = url_ftell(pb);
1395
    put_be32(pb, 0); /* size placeholder*/
1396
    put_tag(pb, "moov");
1397
    mov->timescale = globalTimescale;
1398

    
1399
    for (i=0; i<mov->nb_streams; i++) {
1400
        if(mov->tracks[i].entry <= 0) continue;
1401

    
1402
        mov->tracks[i].time = mov->time;
1403
        mov->tracks[i].trackID = i+1;
1404
    }
1405

    
1406
    mov_write_mvhd_tag(pb, mov);
1407
    //mov_write_iods_tag(pb, mov);
1408
    for (i=0; i<mov->nb_streams; i++) {
1409
        if(mov->tracks[i].entry > 0) {
1410
            mov_write_trak_tag(pb, &(mov->tracks[i]));
1411
        }
1412
    }
1413

    
1414
    if (mov->mode == MODE_PSP)
1415
        mov_write_uuidusmt_tag(pb, s);
1416
    else if (mov->mode != MODE_3GP && mov->mode != MODE_3G2)
1417
        mov_write_udta_tag(pb, mov, s);
1418

    
1419
    return updateSize(pb, pos);
1420
}
1421

    
1422
static int mov_write_mdat_tag(ByteIOContext *pb, MOVContext* mov)
1423
{
1424
    put_be32(pb, 8);    // placeholder for extended size field (64 bit)
1425
    put_tag(pb, mov->mode == MODE_MOV ? "wide" : "free");
1426

    
1427
    mov->mdat_pos = url_ftell(pb);
1428
    put_be32(pb, 0); /* size placeholder*/
1429
    put_tag(pb, "mdat");
1430
    return 0;
1431
}
1432

    
1433
/* TODO: This needs to be more general */
1434
static void mov_write_ftyp_tag (ByteIOContext *pb, AVFormatContext *s)
1435
{
1436
    MOVContext *mov = s->priv_data;
1437

    
1438
    put_be32(pb, 0x14 ); /* size */
1439
    put_tag(pb, "ftyp");
1440

    
1441
    if ( mov->mode == MODE_3GP )
1442
        put_tag(pb, "3gp4");
1443
    else if ( mov->mode == MODE_3G2 )
1444
        put_tag(pb, "3g2a");
1445
    else if ( mov->mode == MODE_PSP )
1446
        put_tag(pb, "MSNV");
1447
    else if ( mov->mode == MODE_MP4 )
1448
        put_tag(pb, "isom");
1449
    else
1450
        put_tag(pb, "qt  ");
1451

    
1452
    put_be32(pb, 0x200 );
1453

    
1454
    if ( mov->mode == MODE_3GP )
1455
        put_tag(pb, "3gp4");
1456
    else if ( mov->mode == MODE_3G2 )
1457
        put_tag(pb, "3g2a");
1458
    else if ( mov->mode == MODE_PSP )
1459
        put_tag(pb, "MSNV");
1460
    else if ( mov->mode == MODE_MP4 )
1461
        put_tag(pb, "mp41");
1462
    else
1463
        put_tag(pb, "qt  ");
1464
}
1465

    
1466
static void mov_write_uuidprof_tag(ByteIOContext *pb, AVFormatContext *s)
1467
{
1468
    AVCodecContext *VideoCodec = s->streams[0]->codec;
1469
    AVCodecContext *AudioCodec = s->streams[1]->codec;
1470
    int AudioRate = AudioCodec->sample_rate;
1471
    int FrameRate = ((VideoCodec->time_base.den) * (0x10000))/ (VideoCodec->time_base.num);
1472
    int audio_kbitrate= AudioCodec->bit_rate / 1000;
1473
    int video_kbitrate= FFMIN(VideoCodec->bit_rate / 1000, 800 - audio_kbitrate);
1474

    
1475
    put_be32(pb, 0x94 ); /* size */
1476
    put_tag(pb, "uuid");
1477
    put_tag(pb, "PROF");
1478

    
1479
    put_be32(pb, 0x21d24fce ); /* 96 bit UUID */
1480
    put_be32(pb, 0xbb88695c );
1481
    put_be32(pb, 0xfac9c740 );
1482

    
1483
    put_be32(pb, 0x0 );  /* ? */
1484
    put_be32(pb, 0x3 );  /* 3 sections ? */
1485

    
1486
    put_be32(pb, 0x14 ); /* size */
1487
    put_tag(pb, "FPRF");
1488
    put_be32(pb, 0x0 );  /* ? */
1489
    put_be32(pb, 0x0 );  /* ? */
1490
    put_be32(pb, 0x0 );  /* ? */
1491

    
1492
    put_be32(pb, 0x2c );  /* size */
1493
    put_tag(pb, "APRF");   /* audio */
1494
    put_be32(pb, 0x0 );
1495
    put_be32(pb, 0x2 );   /* TrackID */
1496
    put_tag(pb, "mp4a");
1497
    put_be32(pb, 0x20f );
1498
    put_be32(pb, 0x0 );
1499
    put_be32(pb, audio_kbitrate);
1500
    put_be32(pb, audio_kbitrate);
1501
    put_be32(pb, AudioRate );
1502
    put_be32(pb, AudioCodec->channels );
1503

    
1504
    put_be32(pb, 0x34 );  /* size */
1505
    put_tag(pb, "VPRF");   /* video */
1506
    put_be32(pb, 0x0 );
1507
    put_be32(pb, 0x1 );    /* TrackID */
1508
    if (VideoCodec->codec_id == CODEC_ID_H264) {
1509
        put_tag(pb, "avc1");
1510
        put_be16(pb, 0x014D );
1511
        put_be16(pb, 0x0015 );
1512
    } else {
1513
        put_tag(pb, "mp4v");
1514
        put_be16(pb, 0x0000 );
1515
        put_be16(pb, 0x0103 );
1516
    }
1517
    put_be32(pb, 0x0 );
1518
    put_be32(pb, video_kbitrate);
1519
    put_be32(pb, video_kbitrate);
1520
    put_be32(pb, FrameRate);
1521
    put_be32(pb, FrameRate);
1522
    put_be16(pb, VideoCodec->width);
1523
    put_be16(pb, VideoCodec->height);
1524
    put_be32(pb, 0x010001); /* ? */
1525
}
1526

    
1527
static int mov_write_header(AVFormatContext *s)
1528
{
1529
    ByteIOContext *pb = s->pb;
1530
    MOVContext *mov = s->priv_data;
1531
    int i;
1532

    
1533
    if (url_is_streamed(s->pb)) {
1534
        av_log(s, AV_LOG_ERROR, "muxer does not support non seekable output\n");
1535
        return -1;
1536
    }
1537

    
1538
    /* Default mode == MP4 */
1539
    mov->mode = MODE_MP4;
1540

    
1541
    if (s->oformat != NULL) {
1542
        if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
1543
        else if (!strcmp("3g2", s->oformat->name)) mov->mode = MODE_3G2;
1544
        else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
1545
        else if (!strcmp("psp", s->oformat->name)) mov->mode = MODE_PSP;
1546

    
1547
        mov_write_ftyp_tag(pb,s);
1548
        if ( mov->mode == MODE_PSP ) {
1549
            if ( s->nb_streams != 2 ) {
1550
                av_log(s, AV_LOG_ERROR, "PSP mode need one video and one audio stream\n");
1551
                return -1;
1552
            }
1553
            mov_write_uuidprof_tag(pb,s);
1554
        }
1555
    }
1556

    
1557
    for(i=0; i<s->nb_streams; i++){
1558
        AVStream *st= s->streams[i];
1559
        MOVTrack *track= &mov->tracks[i];
1560

    
1561
        track->enc = st->codec;
1562
        track->language = ff_mov_iso639_to_lang(st->language, mov->mode != MODE_MOV);
1563
        track->mode = mov->mode;
1564
        if(st->codec->codec_type == CODEC_TYPE_VIDEO){
1565
            track->tag = mov_find_video_codec_tag(s, track);
1566
            track->timescale = st->codec->time_base.den;
1567
            av_set_pts_info(st, 64, 1, st->codec->time_base.den);
1568
            if (track->timescale > 100000)
1569
                av_log(NULL, AV_LOG_WARNING,
1570
                       "WARNING codec timebase is very high. If duration is too long,\n"
1571
                       "file may not be playable by quicktime. Specify a shorter timebase\n"
1572
                       "or choose different container.\n");
1573
        }else if(st->codec->codec_type == CODEC_TYPE_AUDIO){
1574
            track->tag = mov_find_audio_codec_tag(s, track);
1575
            track->timescale = st->codec->sample_rate;
1576
            av_set_pts_info(st, 64, 1, st->codec->sample_rate);
1577
            if(!st->codec->frame_size){
1578
                av_log(s, AV_LOG_ERROR, "track %d: codec frame size is not set\n", i);
1579
                return -1;
1580
            }else if(st->codec->frame_size > 1){ /* assume compressed audio */
1581
                track->audio_vbr = 1;
1582
            }else{
1583
                track->sampleSize = (av_get_bits_per_sample(st->codec->codec_id) >> 3) * st->codec->channels;
1584
            }
1585
        }
1586
    }
1587

    
1588
    mov_write_mdat_tag(pb, mov);
1589
    mov->time = s->timestamp + 0x7C25B080; //1970 based -> 1904 based
1590
    mov->nb_streams = s->nb_streams;
1591

    
1592
    put_flush_packet(pb);
1593

    
1594
    return 0;
1595
}
1596

    
1597
static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
1598
{
1599
    MOVContext *mov = s->priv_data;
1600
    ByteIOContext *pb = s->pb;
1601
    MOVTrack *trk = &mov->tracks[pkt->stream_index];
1602
    AVCodecContext *enc = trk->enc;
1603
    unsigned int samplesInChunk = 0;
1604
    int size= pkt->size;
1605

    
1606
    if (url_is_streamed(s->pb)) return 0; /* Can't handle that */
1607
    if (!size) return 0; /* Discard 0 sized packets */
1608

    
1609
    if (enc->codec_id == CODEC_ID_AMR_NB) {
1610
        /* We must find out how many AMR blocks there are in one packet */
1611
        static uint16_t packed_size[16] =
1612
            {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 0};
1613
        int len = 0;
1614

    
1615
        while (len < size && samplesInChunk < 100) {
1616
            len += packed_size[(pkt->data[len] >> 3) & 0x0F];
1617
            samplesInChunk++;
1618
        }
1619
        if(samplesInChunk > 1){
1620
            av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, implement a AVParser for it\n");
1621
            return -1;
1622
        }
1623
    } else if (trk->sampleSize)
1624
        samplesInChunk = size/trk->sampleSize;
1625
    else
1626
        samplesInChunk = 1;
1627

    
1628
    /* copy extradata if it exists */
1629
    if (trk->vosLen == 0 && enc->extradata_size > 0) {
1630
        trk->vosLen = enc->extradata_size;
1631
        trk->vosData = av_malloc(trk->vosLen);
1632
        memcpy(trk->vosData, enc->extradata, trk->vosLen);
1633
    }
1634

    
1635
    if (enc->codec_id == CODEC_ID_H264 && trk->vosLen > 0 && *(uint8_t *)trk->vosData != 1) {
1636
        /* from x264 or from bytestream h264 */
1637
        /* nal reformating needed */
1638
        avc_parse_nal_units(&pkt->data, &pkt->size);
1639
        assert(pkt->size);
1640
        size = pkt->size;
1641
    } else if (enc->codec_id == CODEC_ID_DNXHD && !trk->vosLen) {
1642
        /* copy frame header to create needed atoms */
1643
        if (size < 640)
1644
            return -1;
1645
        trk->vosLen = 640;
1646
        trk->vosData = av_malloc(trk->vosLen);
1647
        memcpy(trk->vosData, pkt->data, 640);
1648
    }
1649

    
1650
    if (!(trk->entry % MOV_INDEX_CLUSTER_SIZE)) {
1651
        trk->cluster = av_realloc(trk->cluster, (trk->entry + MOV_INDEX_CLUSTER_SIZE) * sizeof(*trk->cluster));
1652
        if (!trk->cluster)
1653
            return -1;
1654
    }
1655

    
1656
    trk->cluster[trk->entry].pos = url_ftell(pb);
1657
    trk->cluster[trk->entry].samplesInChunk = samplesInChunk;
1658
    trk->cluster[trk->entry].size = size;
1659
    trk->cluster[trk->entry].entries = samplesInChunk;
1660
    trk->cluster[trk->entry].dts = pkt->dts;
1661
    trk->trackDuration = pkt->dts - trk->cluster[0].dts + pkt->duration;
1662

    
1663
    if(enc->codec_type == CODEC_TYPE_VIDEO) {
1664
        if (pkt->dts != pkt->pts)
1665
            trk->hasBframes = 1;
1666
        trk->cluster[trk->entry].cts = pkt->pts - pkt->dts;
1667
        trk->cluster[trk->entry].key_frame = !!(pkt->flags & PKT_FLAG_KEY);
1668
        if(trk->cluster[trk->entry].key_frame)
1669
            trk->hasKeyframes++;
1670
    }
1671
    trk->entry++;
1672
    trk->sampleCount += samplesInChunk;
1673
    mov->mdat_size += size;
1674

    
1675
    put_buffer(pb, pkt->data, size);
1676

    
1677
    put_flush_packet(pb);
1678
    return 0;
1679
}
1680

    
1681
static int mov_write_trailer(AVFormatContext *s)
1682
{
1683
    MOVContext *mov = s->priv_data;
1684
    ByteIOContext *pb = s->pb;
1685
    int res = 0;
1686
    int i;
1687

    
1688
    offset_t moov_pos = url_ftell(pb);
1689

    
1690
    /* Write size of mdat tag */
1691
    if (mov->mdat_size+8 <= UINT32_MAX) {
1692
        url_fseek(pb, mov->mdat_pos, SEEK_SET);
1693
        put_be32(pb, mov->mdat_size+8);
1694
    } else {
1695
        /* overwrite 'wide' placeholder atom */
1696
        url_fseek(pb, mov->mdat_pos - 8, SEEK_SET);
1697
        put_be32(pb, 1); /* special value: real atom size will be 64 bit value after tag field */
1698
        put_tag(pb, "mdat");
1699
        put_be64(pb, mov->mdat_size+16);
1700
    }
1701
    url_fseek(pb, moov_pos, SEEK_SET);
1702

    
1703
    mov_write_moov_tag(pb, mov, s);
1704

    
1705
    for (i=0; i<mov->nb_streams; i++) {
1706
        av_freep(&mov->tracks[i].cluster);
1707

    
1708
        if( mov->tracks[i].vosLen ) av_free( mov->tracks[i].vosData );
1709

    
1710
    }
1711

    
1712
    put_flush_packet(pb);
1713

    
1714
    return res;
1715
}
1716

    
1717
#ifdef CONFIG_MOV_MUXER
1718
AVOutputFormat mov_muxer = {
1719
    "mov",
1720
    "mov format",
1721
    NULL,
1722
    "mov",
1723
    sizeof(MOVContext),
1724
    CODEC_ID_AAC,
1725
    CODEC_ID_MPEG4,
1726
    mov_write_header,
1727
    mov_write_packet,
1728
    mov_write_trailer,
1729
    .flags = AVFMT_GLOBALHEADER,
1730
};
1731
#endif
1732
#ifdef CONFIG_TGP_MUXER
1733
AVOutputFormat tgp_muxer = {
1734
    "3gp",
1735
    "3gp format",
1736
    NULL,
1737
    "3gp",
1738
    sizeof(MOVContext),
1739
    CODEC_ID_AMR_NB,
1740
    CODEC_ID_H263,
1741
    mov_write_header,
1742
    mov_write_packet,
1743
    mov_write_trailer,
1744
    .flags = AVFMT_GLOBALHEADER,
1745
};
1746
#endif
1747
#ifdef CONFIG_MP4_MUXER
1748
AVOutputFormat mp4_muxer = {
1749
    "mp4",
1750
    "mp4 format",
1751
    "application/mp4",
1752
    "mp4,m4a",
1753
    sizeof(MOVContext),
1754
    CODEC_ID_AAC,
1755
    CODEC_ID_MPEG4,
1756
    mov_write_header,
1757
    mov_write_packet,
1758
    mov_write_trailer,
1759
    .flags = AVFMT_GLOBALHEADER,
1760
};
1761
#endif
1762
#ifdef CONFIG_PSP_MUXER
1763
AVOutputFormat psp_muxer = {
1764
    "psp",
1765
    "psp mp4 format",
1766
    NULL,
1767
    "mp4,psp",
1768
    sizeof(MOVContext),
1769
    CODEC_ID_AAC,
1770
    CODEC_ID_MPEG4,
1771
    mov_write_header,
1772
    mov_write_packet,
1773
    mov_write_trailer,
1774
    .flags = AVFMT_GLOBALHEADER,
1775
};
1776
#endif
1777
#ifdef CONFIG_TG2_MUXER
1778
AVOutputFormat tg2_muxer = {
1779
    "3g2",
1780
    "3gp2 format",
1781
    NULL,
1782
    "3g2",
1783
    sizeof(MOVContext),
1784
    CODEC_ID_AMR_NB,
1785
    CODEC_ID_H263,
1786
    mov_write_header,
1787
    mov_write_packet,
1788
    mov_write_trailer,
1789
    .flags = AVFMT_GLOBALHEADER,
1790
};
1791
#endif