Statistics
| Branch: | Revision:

ffmpeg / libavformat / movenc.c @ 00996920

History | View | Annotate | Download (54.2 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_audio_tag(ByteIOContext *pb, MOVTrack* track)
326
{
327
    offset_t pos = url_ftell(pb);
328
    int version = track->mode == MODE_MOV &&
329
        (track->audio_vbr ||
330
         track->enc->codec_id == CODEC_ID_PCM_S32LE ||
331
         track->enc->codec_id == CODEC_ID_PCM_S24LE);
332

    
333
    put_be32(pb, 0); /* size */
334
    put_le32(pb, track->tag); // store it byteswapped
335
    put_be32(pb, 0); /* Reserved */
336
    put_be16(pb, 0); /* Reserved */
337
    put_be16(pb, 1); /* Data-reference index, XXX  == 1 */
338

    
339
    /* SoundDescription */
340
    put_be16(pb, version); /* Version */
341
    put_be16(pb, 0); /* Revision level */
342
    put_be32(pb, 0); /* Reserved */
343

    
344
    if (track->mode == MODE_MOV) {
345
        put_be16(pb, track->enc->channels);
346
        if (track->enc->codec_id == CODEC_ID_PCM_U8 ||
347
            track->enc->codec_id == CODEC_ID_PCM_S8)
348
            put_be16(pb, 8); /* bits per sample */
349
        else
350
            put_be16(pb, 16);
351
        put_be16(pb, track->audio_vbr ? -2 : 0); /* compression ID */
352
    } else { /* reserved for mp4/3gp */
353
        put_be16(pb, 2);
354
        put_be16(pb, 16);
355
        put_be16(pb, 0);
356
    }
357

    
358
    put_be16(pb, 0); /* packet size (= 0) */
359
    put_be16(pb, track->timescale); /* Time scale */
360
    put_be16(pb, 0); /* Reserved */
361

    
362
    if(version == 1) { /* SoundDescription V1 extended info */
363
        put_be32(pb, track->enc->frame_size); /* Samples per packet */
364
        put_be32(pb, track->sampleSize / track->enc->channels); /* Bytes per packet */
365
        put_be32(pb, track->sampleSize); /* Bytes per frame */
366
        put_be32(pb, 2); /* Bytes per sample */
367
    }
368

    
369
    if(track->mode == MODE_MOV &&
370
       (track->enc->codec_id == CODEC_ID_AAC ||
371
        track->enc->codec_id == CODEC_ID_AMR_NB ||
372
        track->enc->codec_id == CODEC_ID_PCM_S24LE ||
373
        track->enc->codec_id == CODEC_ID_PCM_S32LE))
374
        mov_write_wave_tag(pb, track);
375
    else if(track->enc->codec_id == CODEC_ID_AAC)
376
        mov_write_esds_tag(pb, track);
377
    else if(track->enc->codec_id == CODEC_ID_AMR_NB)
378
        mov_write_amr_tag(pb, track);
379

    
380
    return updateSize (pb, pos);
381
}
382

    
383
static int mov_write_d263_tag(ByteIOContext *pb)
384
{
385
    put_be32(pb, 0xf); /* size */
386
    put_tag(pb, "d263");
387
    put_tag(pb, "FFMP");
388
    put_byte(pb, 0); /* decoder version */
389
    /* FIXME use AVCodecContext level/profile, when encoder will set values */
390
    put_byte(pb, 0xa); /* level */
391
    put_byte(pb, 0); /* profile */
392
    return 0xf;
393
}
394

    
395
/* TODO: No idea about these values */
396
static int mov_write_svq3_tag(ByteIOContext *pb)
397
{
398
    put_be32(pb, 0x15);
399
    put_tag(pb, "SMI ");
400
    put_tag(pb, "SEQH");
401
    put_be32(pb, 0x5);
402
    put_be32(pb, 0xe2c0211d);
403
    put_be32(pb, 0xc0000000);
404
    put_byte(pb, 0);
405
    return 0x15;
406
}
407

    
408
static uint8_t *avc_find_startcode( uint8_t *p, uint8_t *end )
409
{
410
    uint8_t *a = p + 4 - ((long)p & 3);
411

    
412
    for( end -= 3; p < a && p < end; p++ ) {
413
        if( p[0] == 0 && p[1] == 0 && p[2] == 1 )
414
            return p;
415
    }
416

    
417
    for( end -= 3; p < end; p += 4 ) {
418
        uint32_t x = *(uint32_t*)p;
419
//      if( (x - 0x01000100) & (~x) & 0x80008000 ) // little endian
420
//      if( (x - 0x00010001) & (~x) & 0x00800080 ) // big endian
421
        if( (x - 0x01010101) & (~x) & 0x80808080 ) { // generic
422
            if( p[1] == 0 ) {
423
                if( p[0] == 0 && p[2] == 1 )
424
                    return p-1;
425
                if( p[2] == 0 && p[3] == 1 )
426
                    return p;
427
            }
428
            if( p[3] == 0 ) {
429
                if( p[2] == 0 && p[4] == 1 )
430
                    return p+1;
431
                if( p[4] == 0 && p[5] == 1 )
432
                    return p+2;
433
            }
434
        }
435
    }
436

    
437
    for( end += 3; p < end; p++ ) {
438
        if( p[0] == 0 && p[1] == 0 && p[2] == 1 )
439
            return p;
440
    }
441

    
442
    return end + 3;
443
}
444

    
445
static void avc_parse_nal_units(uint8_t **buf, int *size)
446
{
447
    ByteIOContext pb;
448
    uint8_t *p = *buf;
449
    uint8_t *end = p + *size;
450
    uint8_t *nal_start, *nal_end;
451

    
452
    url_open_dyn_buf(&pb);
453
    nal_start = avc_find_startcode(p, end);
454
    while (nal_start < end) {
455
        while(!*(nal_start++));
456
        nal_end = avc_find_startcode(nal_start, end);
457
        put_be32(&pb, nal_end - nal_start);
458
        put_buffer(&pb, nal_start, nal_end - nal_start);
459
        nal_start = nal_end;
460
    }
461
    av_freep(buf);
462
    *size = url_close_dyn_buf(&pb, buf);
463
}
464

    
465
static int mov_write_avcc_tag(ByteIOContext *pb, MOVTrack *track)
466
{
467
    offset_t pos = url_ftell(pb);
468

    
469
    put_be32(pb, 0);
470
    put_tag(pb, "avcC");
471
    if (track->vosLen > 6) {
472
        /* check for h264 start code */
473
        if (AV_RB32(track->vosData) == 0x00000001) {
474
            uint8_t *buf, *end;
475
            uint32_t sps_size=0, pps_size=0;
476
            uint8_t *sps=0, *pps=0;
477

    
478
            avc_parse_nal_units(&track->vosData, &track->vosLen);
479
            buf = track->vosData;
480
            end = track->vosData + track->vosLen;
481

    
482
            /* look for sps and pps */
483
            while (buf < end) {
484
                unsigned int size;
485
                uint8_t nal_type;
486
                size = AV_RB32(buf);
487
                nal_type = buf[4] & 0x1f;
488
                if (nal_type == 7) { /* SPS */
489
                    sps = buf + 4;
490
                    sps_size = size;
491
                } else if (nal_type == 8) { /* PPS */
492
                    pps = buf + 4;
493
                    pps_size = size;
494
                }
495
                buf += size + 4;
496
            }
497
            assert(sps);
498
            assert(pps);
499

    
500
            put_byte(pb, 1); /* version */
501
            put_byte(pb, sps[1]); /* profile */
502
            put_byte(pb, sps[2]); /* profile compat */
503
            put_byte(pb, sps[3]); /* level */
504
            put_byte(pb, 0xff); /* 6 bits reserved (111111) + 2 bits nal size length - 1 (11) */
505
            put_byte(pb, 0xe1); /* 3 bits reserved (111) + 5 bits number of sps (00001) */
506

    
507
            put_be16(pb, sps_size);
508
            put_buffer(pb, sps, sps_size);
509
            put_byte(pb, 1); /* number of pps */
510
            put_be16(pb, pps_size);
511
            put_buffer(pb, pps, pps_size);
512
        } else {
513
            put_buffer(pb, track->vosData, track->vosLen);
514
        }
515
    }
516
    return updateSize(pb, pos);
517
}
518

    
519
/* also used by all avid codecs (dv, imx, meridien) and their variants */
520
static int mov_write_avid_tag(ByteIOContext *pb, MOVTrack *track)
521
{
522
    int i;
523
    put_be32(pb, 24); /* size */
524
    put_tag(pb, "ACLR");
525
    put_tag(pb, "ACLR");
526
    put_tag(pb, "0001");
527
    put_be32(pb, 1); /* yuv 1 / rgb 2 ? */
528
    put_be32(pb, 0); /* unknown */
529

    
530
    put_be32(pb, 24); /* size */
531
    put_tag(pb, "APRG");
532
    put_tag(pb, "APRG");
533
    put_tag(pb, "0001");
534
    put_be32(pb, 1); /* unknown */
535
    put_be32(pb, 0); /* unknown */
536

    
537
    put_be32(pb, 120); /* size */
538
    put_tag(pb, "ARES");
539
    put_tag(pb, "ARES");
540
    put_tag(pb, "0001");
541
    put_be32(pb, AV_RB32(track->vosData + 0x28)); /* dnxhd cid, some id ? */
542
    put_be32(pb, track->enc->width);
543
    /* values below are based on samples created with quicktime and avid codecs */
544
    if (track->vosData[5] & 2) { // interlaced
545
        put_be32(pb, track->enc->height/2);
546
        put_be32(pb, 2); /* unknown */
547
        put_be32(pb, 0); /* unknown */
548
        put_be32(pb, 4); /* unknown */
549
    } else {
550
        put_be32(pb, track->enc->height);
551
        put_be32(pb, 1); /* unknown */
552
        put_be32(pb, 0); /* unknown */
553
        put_be32(pb, 5); /* unknown */
554
    }
555
    /* padding */
556
    for (i = 0; i < 10; i++)
557
        put_be64(pb, 0);
558

    
559
    /* extra padding for stsd needed */
560
    put_be32(pb, 0);
561
    return 0;
562
}
563

    
564
static int mov_find_video_codec_tag(AVFormatContext *s, MOVTrack *track)
565
{
566
    int tag = track->enc->codec_tag;
567
    if (!tag) {
568
        if (track->enc->codec_id == CODEC_ID_DVVIDEO) {
569
            if (track->enc->height == 480) { /* NTSC */
570
                if (track->enc->pix_fmt == PIX_FMT_YUV422P)
571
                    tag = MKTAG('d', 'v', '5', 'n');
572
                else
573
                    tag = MKTAG('d', 'v', 'c', ' ');
574
            } else { /* assume PAL */
575
                if (track->enc->pix_fmt == PIX_FMT_YUV422P)
576
                    tag = MKTAG('d', 'v', '5', 'p');
577
                else if (track->enc->pix_fmt == PIX_FMT_YUV420P)
578
                    tag = MKTAG('d', 'v', 'c', 'p');
579
                else
580
                    tag = MKTAG('d', 'v', 'p', 'p');
581
            }
582
        } else if (track->enc->codec_id == CODEC_ID_H263) {
583
            if (track->mode == MODE_MOV)
584
                tag = MKTAG('h', '2', '6', '3');
585
            else
586
                tag = MKTAG('s', '2', '6', '3');
587
        } else {
588
            tag = codec_get_tag(codec_movvideo_tags, track->enc->codec_id);
589
        }
590
    }
591
    // if no mac fcc found, try with Microsoft tags
592
    if (!tag) {
593
        tag = codec_get_tag(codec_bmp_tags, track->enc->codec_id);
594
        if (tag) {
595
            av_log(s, AV_LOG_INFO, "Warning, using MS style video codec tag, the file may be unplayable!\n");
596
        }
597
    }
598
    assert(tag);
599
    return tag;
600
}
601

    
602
static int mov_find_audio_codec_tag(AVFormatContext *s, MOVTrack *track)
603
{
604
    int tag = track->enc->codec_tag;
605
    if (!tag) {
606
        tag = codec_get_tag(codec_movaudio_tags, track->enc->codec_id);
607
    }
608
    // if no mac fcc found, try with Microsoft tags
609
    if (!tag) {
610
        int ms_tag = codec_get_tag(codec_wav_tags, track->enc->codec_id);
611
        if (ms_tag) {
612
            tag = MKTAG('m', 's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
613
            av_log(s, AV_LOG_INFO, "Warning, using MS style audio codec tag, the file may be unplayable!\n");
614
        }
615
    }
616
    assert(tag);
617
    return tag;
618
}
619

    
620
static int mov_write_video_tag(ByteIOContext *pb, MOVTrack* track)
621
{
622
    offset_t pos = url_ftell(pb);
623
    char compressor_name[32];
624

    
625
    put_be32(pb, 0); /* size */
626
    put_le32(pb, track->tag); // store it byteswapped
627
    put_be32(pb, 0); /* Reserved */
628
    put_be16(pb, 0); /* Reserved */
629
    put_be16(pb, 1); /* Data-reference index */
630

    
631
    put_be16(pb, 0); /* Codec stream version */
632
    put_be16(pb, 0); /* Codec stream revision (=0) */
633
    if (track->mode == MODE_MOV) {
634
        put_tag(pb, "FFMP"); /* Vendor */
635
        if(track->enc->codec_id == CODEC_ID_RAWVIDEO) {
636
            put_be32(pb, 0); /* Temporal Quality */
637
            put_be32(pb, 0x400); /* Spatial Quality = lossless*/
638
        } else {
639
            put_be32(pb, 0x200); /* Temporal Quality = normal */
640
            put_be32(pb, 0x200); /* Spatial Quality = normal */
641
        }
642
    } else {
643
        put_be32(pb, 0); /* Reserved */
644
        put_be32(pb, 0); /* Reserved */
645
        put_be32(pb, 0); /* Reserved */
646
    }
647
    put_be16(pb, track->enc->width); /* Video width */
648
    put_be16(pb, track->enc->height); /* Video height */
649
    put_be32(pb, 0x00480000); /* Horizontal resolution 72dpi */
650
    put_be32(pb, 0x00480000); /* Vertical resolution 72dpi */
651
    put_be32(pb, 0); /* Data size (= 0) */
652
    put_be16(pb, 1); /* Frame count (= 1) */
653

    
654
    memset(compressor_name,0,32);
655
    /* FIXME not sure, ISO 14496-1 draft where it shall be set to 0 */
656
    if (track->mode == MODE_MOV && track->enc->codec && track->enc->codec->name)
657
        strncpy(compressor_name,track->enc->codec->name,31);
658
    put_byte(pb, strlen(compressor_name));
659
    put_buffer(pb, compressor_name, 31);
660

    
661
    put_be16(pb, 0x18); /* Reserved */
662
    put_be16(pb, 0xffff); /* Reserved */
663
    if(track->enc->codec_id == CODEC_ID_MPEG4)
664
        mov_write_esds_tag(pb, track);
665
    else if(track->enc->codec_id == CODEC_ID_H263)
666
        mov_write_d263_tag(pb);
667
    else if(track->enc->codec_id == CODEC_ID_SVQ3)
668
        mov_write_svq3_tag(pb);
669
    else if(track->enc->codec_id == CODEC_ID_H264)
670
        mov_write_avcc_tag(pb, track);
671
    else if(track->enc->codec_id == CODEC_ID_DNXHD)
672
        mov_write_avid_tag(pb, track);
673

    
674
    return updateSize (pb, pos);
675
}
676

    
677
static int mov_write_stsd_tag(ByteIOContext *pb, MOVTrack* track)
678
{
679
    offset_t pos = url_ftell(pb);
680
    put_be32(pb, 0); /* size */
681
    put_tag(pb, "stsd");
682
    put_be32(pb, 0); /* version & flags */
683
    put_be32(pb, 1); /* entry count */
684
    if (track->enc->codec_type == CODEC_TYPE_VIDEO)
685
        mov_write_video_tag(pb, track);
686
    else if (track->enc->codec_type == CODEC_TYPE_AUDIO)
687
        mov_write_audio_tag(pb, track);
688
    return updateSize(pb, pos);
689
}
690

    
691
static int mov_write_ctts_tag(ByteIOContext *pb, MOVTrack* track)
692
{
693
    MOV_stts_t *ctts_entries;
694
    uint32_t entries = 0;
695
    uint32_t atom_size;
696
    int i;
697

    
698
    ctts_entries = av_malloc((track->entry + 1) * sizeof(*ctts_entries)); /* worst case */
699
    ctts_entries[0].count = 1;
700
    ctts_entries[0].duration = track->cluster[0].cts;
701
    for (i=1; i<track->entry; i++) {
702
        if (track->cluster[i].cts == ctts_entries[entries].duration) {
703
            ctts_entries[entries].count++; /* compress */
704
        } else {
705
            entries++;
706
            ctts_entries[entries].duration = track->cluster[i].cts;
707
            ctts_entries[entries].count = 1;
708
        }
709
    }
710
    entries++; /* last one */
711
    atom_size = 16 + (entries * 8);
712
    put_be32(pb, atom_size); /* size */
713
    put_tag(pb, "ctts");
714
    put_be32(pb, 0); /* version & flags */
715
    put_be32(pb, entries); /* entry count */
716
    for (i=0; i<entries; i++) {
717
        put_be32(pb, ctts_entries[i].count);
718
        put_be32(pb, ctts_entries[i].duration);
719
    }
720
    av_free(ctts_entries);
721
    return atom_size;
722
}
723

    
724
/* Time to sample atom */
725
static int mov_write_stts_tag(ByteIOContext *pb, MOVTrack* track)
726
{
727
    MOV_stts_t *stts_entries;
728
    uint32_t entries = -1;
729
    uint32_t atom_size;
730
    int i;
731

    
732
    if (track->enc->codec_type == CODEC_TYPE_AUDIO && !track->audio_vbr) {
733
        stts_entries = av_malloc(sizeof(*stts_entries)); /* one entry */
734
        stts_entries[0].count = track->sampleCount;
735
        stts_entries[0].duration = 1;
736
        entries = 1;
737
    } else {
738
        stts_entries = av_malloc(track->entry * sizeof(*stts_entries)); /* worst case */
739
        for (i=0; i<track->entry; i++) {
740
            int64_t duration = i + 1 == track->entry ?
741
                track->trackDuration - track->cluster[i].dts + track->cluster[0].dts : /* readjusting */
742
                track->cluster[i+1].dts - track->cluster[i].dts;
743
            if (i && duration == stts_entries[entries].duration) {
744
                stts_entries[entries].count++; /* compress */
745
            } else {
746
                entries++;
747
                stts_entries[entries].duration = duration;
748
                stts_entries[entries].count = 1;
749
            }
750
        }
751
        entries++; /* last one */
752
    }
753
    atom_size = 16 + (entries * 8);
754
    put_be32(pb, atom_size); /* size */
755
    put_tag(pb, "stts");
756
    put_be32(pb, 0); /* version & flags */
757
    put_be32(pb, entries); /* entry count */
758
    for (i=0; i<entries; i++) {
759
        put_be32(pb, stts_entries[i].count);
760
        put_be32(pb, stts_entries[i].duration);
761
    }
762
    av_free(stts_entries);
763
    return atom_size;
764
}
765

    
766
static int mov_write_dref_tag(ByteIOContext *pb)
767
{
768
    put_be32(pb, 28); /* size */
769
    put_tag(pb, "dref");
770
    put_be32(pb, 0); /* version & flags */
771
    put_be32(pb, 1); /* entry count */
772

    
773
    put_be32(pb, 0xc); /* size */
774
    put_tag(pb, "url ");
775
    put_be32(pb, 1); /* version & flags */
776

    
777
    return 28;
778
}
779

    
780
static int mov_write_stbl_tag(ByteIOContext *pb, MOVTrack* track)
781
{
782
    offset_t pos = url_ftell(pb);
783
    put_be32(pb, 0); /* size */
784
    put_tag(pb, "stbl");
785
    mov_write_stsd_tag(pb, track);
786
    mov_write_stts_tag(pb, track);
787
    if (track->enc->codec_type == CODEC_TYPE_VIDEO &&
788
        track->hasKeyframes < track->entry)
789
        mov_write_stss_tag(pb, track);
790
    if (track->enc->codec_type == CODEC_TYPE_VIDEO &&
791
        track->hasBframes)
792
        mov_write_ctts_tag(pb, track);
793
    mov_write_stsc_tag(pb, track);
794
    mov_write_stsz_tag(pb, track);
795
    mov_write_stco_tag(pb, track);
796
    return updateSize(pb, pos);
797
}
798

    
799
static int mov_write_dinf_tag(ByteIOContext *pb)
800
{
801
    offset_t pos = url_ftell(pb);
802
    put_be32(pb, 0); /* size */
803
    put_tag(pb, "dinf");
804
    mov_write_dref_tag(pb);
805
    return updateSize(pb, pos);
806
}
807

    
808
static int mov_write_smhd_tag(ByteIOContext *pb)
809
{
810
    put_be32(pb, 16); /* size */
811
    put_tag(pb, "smhd");
812
    put_be32(pb, 0); /* version & flags */
813
    put_be16(pb, 0); /* reserved (balance, normally = 0) */
814
    put_be16(pb, 0); /* reserved */
815
    return 16;
816
}
817

    
818
static int mov_write_vmhd_tag(ByteIOContext *pb)
819
{
820
    put_be32(pb, 0x14); /* size (always 0x14) */
821
    put_tag(pb, "vmhd");
822
    put_be32(pb, 0x01); /* version & flags */
823
    put_be64(pb, 0); /* reserved (graphics mode = copy) */
824
    return 0x14;
825
}
826

    
827
static int mov_write_hdlr_tag(ByteIOContext *pb, MOVTrack* track)
828
{
829
    const char *descr, *hdlr, *hdlr_type;
830
    offset_t pos = url_ftell(pb);
831

    
832
    if (!track) { /* no media --> data handler */
833
        hdlr = "dhlr";
834
        hdlr_type = "url ";
835
        descr = "DataHandler";
836
    } else {
837
        hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
838
        if (track->enc->codec_type == CODEC_TYPE_VIDEO) {
839
            hdlr_type = "vide";
840
            descr = "VideoHandler";
841
        } else {
842
            hdlr_type = "soun";
843
            descr = "SoundHandler";
844
        }
845
    }
846

    
847
    put_be32(pb, 0); /* size */
848
    put_tag(pb, "hdlr");
849
    put_be32(pb, 0); /* Version & flags */
850
    put_buffer(pb, hdlr, 4); /* handler */
851
    put_tag(pb, hdlr_type); /* handler type */
852
    put_be32(pb ,0); /* reserved */
853
    put_be32(pb ,0); /* reserved */
854
    put_be32(pb ,0); /* reserved */
855
    put_byte(pb, strlen(descr)); /* string counter */
856
    put_buffer(pb, descr, strlen(descr)); /* handler description */
857
    return updateSize(pb, pos);
858
}
859

    
860
static int mov_write_minf_tag(ByteIOContext *pb, MOVTrack* track)
861
{
862
    offset_t pos = url_ftell(pb);
863
    put_be32(pb, 0); /* size */
864
    put_tag(pb, "minf");
865
    if(track->enc->codec_type == CODEC_TYPE_VIDEO)
866
        mov_write_vmhd_tag(pb);
867
    else
868
        mov_write_smhd_tag(pb);
869
    if (track->mode == MODE_MOV) /* FIXME: Why do it for MODE_MOV only ? */
870
        mov_write_hdlr_tag(pb, NULL);
871
    mov_write_dinf_tag(pb);
872
    mov_write_stbl_tag(pb, track);
873
    return updateSize(pb, pos);
874
}
875

    
876
static int mov_write_mdhd_tag(ByteIOContext *pb, MOVTrack* track)
877
{
878
    int version = track->trackDuration < INT32_MAX ? 0 : 1;
879

    
880
    (version == 1) ? put_be32(pb, 44) : put_be32(pb, 32); /* size */
881
    put_tag(pb, "mdhd");
882
    put_byte(pb, version);
883
    put_be24(pb, 0); /* flags */
884
    if (version == 1) {
885
        put_be64(pb, track->time);
886
        put_be64(pb, track->time);
887
    } else {
888
        put_be32(pb, track->time); /* creation time */
889
        put_be32(pb, track->time); /* modification time */
890
    }
891
    put_be32(pb, track->timescale); /* time scale (sample rate for audio) */
892
    (version == 1) ? put_be64(pb, track->trackDuration) : put_be32(pb, track->trackDuration); /* duration */
893
    put_be16(pb, track->language); /* language */
894
    put_be16(pb, 0); /* reserved (quality) */
895

    
896
    if(version!=0 && track->mode == MODE_MOV){
897
        av_log(NULL, AV_LOG_ERROR,
898
            "FATAL error, file duration too long for timebase, this file will not be\n"
899
            "playable with quicktime. Choose a different timebase or a different\n"
900
            "container format\n");
901
    }
902

    
903
    return 32;
904
}
905

    
906
static int mov_write_mdia_tag(ByteIOContext *pb, MOVTrack* track)
907
{
908
    offset_t pos = url_ftell(pb);
909
    put_be32(pb, 0); /* size */
910
    put_tag(pb, "mdia");
911
    mov_write_mdhd_tag(pb, track);
912
    mov_write_hdlr_tag(pb, track);
913
    mov_write_minf_tag(pb, track);
914
    return updateSize(pb, pos);
915
}
916

    
917
static int mov_write_tkhd_tag(ByteIOContext *pb, MOVTrack* track)
918
{
919
    int64_t duration = av_rescale_rnd(track->trackDuration, globalTimescale, track->timescale, AV_ROUND_UP);
920
    int version = duration < INT32_MAX ? 0 : 1;
921

    
922
    (version == 1) ? put_be32(pb, 104) : put_be32(pb, 92); /* size */
923
    put_tag(pb, "tkhd");
924
    put_byte(pb, version);
925
    put_be24(pb, 0xf); /* flags (track enabled) */
926
    if (version == 1) {
927
        put_be64(pb, track->time);
928
        put_be64(pb, track->time);
929
    } else {
930
        put_be32(pb, track->time); /* creation time */
931
        put_be32(pb, track->time); /* modification time */
932
    }
933
    put_be32(pb, track->trackID); /* track-id */
934
    put_be32(pb, 0); /* reserved */
935
    (version == 1) ? put_be64(pb, duration) : put_be32(pb, duration);
936

    
937
    put_be32(pb, 0); /* reserved */
938
    put_be32(pb, 0); /* reserved */
939
    put_be32(pb, 0x0); /* reserved (Layer & Alternate group) */
940
    /* Volume, only for audio */
941
    if(track->enc->codec_type == CODEC_TYPE_AUDIO)
942
        put_be16(pb, 0x0100);
943
    else
944
        put_be16(pb, 0);
945
    put_be16(pb, 0); /* reserved */
946

    
947
    /* Matrix structure */
948
    put_be32(pb, 0x00010000); /* reserved */
949
    put_be32(pb, 0x0); /* reserved */
950
    put_be32(pb, 0x0); /* reserved */
951
    put_be32(pb, 0x0); /* reserved */
952
    put_be32(pb, 0x00010000); /* reserved */
953
    put_be32(pb, 0x0); /* reserved */
954
    put_be32(pb, 0x0); /* reserved */
955
    put_be32(pb, 0x0); /* reserved */
956
    put_be32(pb, 0x40000000); /* reserved */
957

    
958
    /* Track width and height, for visual only */
959
    if(track->enc->codec_type == CODEC_TYPE_VIDEO) {
960
        double sample_aspect_ratio = av_q2d(track->enc->sample_aspect_ratio);
961
        if( !sample_aspect_ratio ) sample_aspect_ratio = 1;
962
        put_be32(pb, sample_aspect_ratio * track->enc->width*0x10000);
963
        put_be32(pb, track->enc->height*0x10000);
964
    }
965
    else {
966
        put_be32(pb, 0);
967
        put_be32(pb, 0);
968
    }
969
    return 0x5c;
970
}
971

    
972
// This box seems important for the psp playback ... without it the movie seems to hang
973
static int mov_write_edts_tag(ByteIOContext *pb, MOVTrack *track)
974
{
975
    put_be32(pb, 0x24); /* size  */
976
    put_tag(pb, "edts");
977
    put_be32(pb, 0x1c); /* size  */
978
    put_tag(pb, "elst");
979
    put_be32(pb, 0x0);
980
    put_be32(pb, 0x1);
981

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

    
984
    put_be32(pb, track->cluster[0].cts); /* first pts is cts since dts is 0 */
985
    put_be32(pb, 0x00010000);
986
    return 0x24;
987
}
988

    
989
// goes at the end of each track!  ... Critical for PSP playback ("Incompatible data" without it)
990
static int mov_write_uuid_tag_psp(ByteIOContext *pb, MOVTrack *mov)
991
{
992
    put_be32(pb, 0x34); /* size ... reports as 28 in mp4box! */
993
    put_tag(pb, "uuid");
994
    put_tag(pb, "USMT");
995
    put_be32(pb, 0x21d24fce);
996
    put_be32(pb, 0xbb88695c);
997
    put_be32(pb, 0xfac9c740);
998
    put_be32(pb, 0x1c);     // another size here!
999
    put_tag(pb, "MTDT");
1000
    put_be32(pb, 0x00010012);
1001
    put_be32(pb, 0x0a);
1002
    put_be32(pb, 0x55c40000);
1003
    put_be32(pb, 0x1);
1004
    put_be32(pb, 0x0);
1005
    return 0x34;
1006
}
1007

    
1008
static int mov_write_trak_tag(ByteIOContext *pb, MOVTrack* track)
1009
{
1010
    offset_t pos = url_ftell(pb);
1011
    put_be32(pb, 0); /* size */
1012
    put_tag(pb, "trak");
1013
    mov_write_tkhd_tag(pb, track);
1014
    if (track->mode == MODE_PSP || track->hasBframes)
1015
        mov_write_edts_tag(pb, track);  // PSP Movies require edts box
1016
    mov_write_mdia_tag(pb, track);
1017
    if (track->mode == MODE_PSP)
1018
        mov_write_uuid_tag_psp(pb,track);  // PSP Movies require this uuid box
1019
    return updateSize(pb, pos);
1020
}
1021

    
1022
#if 0
1023
/* TODO: Not sorted out, but not necessary either */
1024
static int mov_write_iods_tag(ByteIOContext *pb, MOVContext *mov)
1025
{
1026
    put_be32(pb, 0x15); /* size */
1027
    put_tag(pb, "iods");
1028
    put_be32(pb, 0);    /* version & flags */
1029
    put_be16(pb, 0x1007);
1030
    put_byte(pb, 0);
1031
    put_be16(pb, 0x4fff);
1032
    put_be16(pb, 0xfffe);
1033
    put_be16(pb, 0x01ff);
1034
    return 0x15;
1035
}
1036
#endif
1037

    
1038
static int mov_write_mvhd_tag(ByteIOContext *pb, MOVContext *mov)
1039
{
1040
    int maxTrackID = 1, i;
1041
    int64_t maxTrackLenTemp, maxTrackLen = 0;
1042
    int version;
1043

    
1044
    for (i=0; i<mov->nb_streams; i++) {
1045
        if(mov->tracks[i].entry > 0) {
1046
            maxTrackLenTemp = av_rescale_rnd(mov->tracks[i].trackDuration, globalTimescale, mov->tracks[i].timescale, AV_ROUND_UP);
1047
            if(maxTrackLen < maxTrackLenTemp)
1048
                maxTrackLen = maxTrackLenTemp;
1049
            if(maxTrackID < mov->tracks[i].trackID)
1050
                maxTrackID = mov->tracks[i].trackID;
1051
        }
1052
    }
1053

    
1054
    version = maxTrackLen < UINT32_MAX ? 0 : 1;
1055
    (version == 1) ? put_be32(pb, 120) : put_be32(pb, 108); /* size */
1056
    put_tag(pb, "mvhd");
1057
    put_byte(pb, version);
1058
    put_be24(pb, 0); /* flags */
1059
    if (version == 1) {
1060
        put_be64(pb, mov->time);
1061
        put_be64(pb, mov->time);
1062
    } else {
1063
        put_be32(pb, mov->time); /* creation time */
1064
        put_be32(pb, mov->time); /* modification time */
1065
    }
1066
    put_be32(pb, mov->timescale); /* timescale */
1067
    (version == 1) ? put_be64(pb, maxTrackLen) : put_be32(pb, maxTrackLen); /* duration of longest track */
1068

    
1069
    put_be32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
1070
    put_be16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
1071
    put_be16(pb, 0); /* reserved */
1072
    put_be32(pb, 0); /* reserved */
1073
    put_be32(pb, 0); /* reserved */
1074

    
1075
    /* Matrix structure */
1076
    put_be32(pb, 0x00010000); /* reserved */
1077
    put_be32(pb, 0x0); /* reserved */
1078
    put_be32(pb, 0x0); /* reserved */
1079
    put_be32(pb, 0x0); /* reserved */
1080
    put_be32(pb, 0x00010000); /* reserved */
1081
    put_be32(pb, 0x0); /* reserved */
1082
    put_be32(pb, 0x0); /* reserved */
1083
    put_be32(pb, 0x0); /* reserved */
1084
    put_be32(pb, 0x40000000); /* reserved */
1085

    
1086
    put_be32(pb, 0); /* reserved (preview time) */
1087
    put_be32(pb, 0); /* reserved (preview duration) */
1088
    put_be32(pb, 0); /* reserved (poster time) */
1089
    put_be32(pb, 0); /* reserved (selection time) */
1090
    put_be32(pb, 0); /* reserved (selection duration) */
1091
    put_be32(pb, 0); /* reserved (current time) */
1092
    put_be32(pb, maxTrackID+1); /* Next track id */
1093
    return 0x6c;
1094
}
1095

    
1096
static int mov_write_itunes_hdlr_tag(ByteIOContext *pb, MOVContext* mov,
1097
                                     AVFormatContext *s)
1098
{
1099
    offset_t pos = url_ftell(pb);
1100
    put_be32(pb, 0); /* size */
1101
    put_tag(pb, "hdlr");
1102
    put_be32(pb, 0);
1103
    put_be32(pb, 0);
1104
    put_tag(pb, "mdir");
1105
    put_tag(pb, "appl");
1106
    put_be32(pb, 0);
1107
    put_be32(pb, 0);
1108
    put_be16(pb, 0);
1109
    return updateSize(pb, pos);
1110
}
1111

    
1112
/* helper function to write a data tag with the specified string as data */
1113
static int mov_write_string_data_tag(ByteIOContext *pb, const char *data, int long_style)
1114
{
1115
    if(long_style){
1116
        offset_t pos = url_ftell(pb);
1117
        put_be32(pb, 0); /* size */
1118
        put_tag(pb, "data");
1119
        put_be32(pb, 1);
1120
        put_be32(pb, 0);
1121
        put_buffer(pb, data, strlen(data));
1122
        return updateSize(pb, pos);
1123
    }else{
1124
        put_be16(pb, strlen(data)); /* string length */
1125
        put_be16(pb, 0);
1126
        put_buffer(pb, data, strlen(data));
1127
        return strlen(data) + 4;
1128
    }
1129
}
1130

    
1131
static int mov_write_string_tag(ByteIOContext *pb, const char *name, const char *value, int long_style){
1132
    int size = 0;
1133
    if ( value && value[0] ) {
1134
        offset_t pos = url_ftell(pb);
1135
        put_be32(pb, 0); /* size */
1136
        put_tag(pb, name);
1137
        mov_write_string_data_tag(pb, value, long_style);
1138
        size= updateSize(pb, pos);
1139
    }
1140
    return size;
1141
}
1142

    
1143
/* iTunes year */
1144
static int mov_write_day_tag(ByteIOContext *pb, int year, int long_style)
1145
{
1146
    if(year){
1147
        char year_str[5];
1148
        snprintf(year_str, sizeof(year_str), "%04d", year);
1149
        return mov_write_string_tag(pb, "\251day", year_str, long_style);
1150
    }else
1151
        return 0;
1152
}
1153

    
1154
/* iTunes track number */
1155
static int mov_write_trkn_tag(ByteIOContext *pb, MOVContext* mov,
1156
                              AVFormatContext *s)
1157
{
1158
    int size = 0;
1159
    if ( s->track ) {
1160
        offset_t pos = url_ftell(pb);
1161
        put_be32(pb, 0); /* size */
1162
        put_tag(pb, "trkn");
1163
        {
1164
            offset_t pos = url_ftell(pb);
1165
            put_be32(pb, 0); /* size */
1166
            put_tag(pb, "data");
1167
            put_be32(pb, 0);        // 8 bytes empty
1168
            put_be32(pb, 0);
1169
            put_be16(pb, 0);        // empty
1170
            put_be16(pb, s->track); // track number
1171
            put_be16(pb, 0);        // total track number
1172
            put_be16(pb, 0);        // empty
1173
            updateSize(pb, pos);
1174
        }
1175
        size = updateSize(pb, pos);
1176
    }
1177
    return size;
1178
}
1179

    
1180
/* iTunes meta data list */
1181
static int mov_write_ilst_tag(ByteIOContext *pb, MOVContext* mov,
1182
                              AVFormatContext *s)
1183
{
1184
    offset_t pos = url_ftell(pb);
1185
    put_be32(pb, 0); /* size */
1186
    put_tag(pb, "ilst");
1187
    mov_write_string_tag(pb, "\251nam", s->title         , 1);
1188
    mov_write_string_tag(pb, "\251ART", s->author        , 1);
1189
    mov_write_string_tag(pb, "\251wrt", s->author        , 1);
1190
    mov_write_string_tag(pb, "\251alb", s->album         , 1);
1191
    mov_write_day_tag(pb, s->year ,1);
1192
    if(mov->tracks[0].enc && !(mov->tracks[0].enc->flags & CODEC_FLAG_BITEXACT))
1193
        mov_write_string_tag(pb, "\251too", LIBAVFORMAT_IDENT, 1);
1194
    mov_write_string_tag(pb, "\251cmt", s->comment       , 1);
1195
    mov_write_string_tag(pb, "\251gen", s->genre         , 1);
1196
    mov_write_trkn_tag(pb, mov, s);
1197
    return updateSize(pb, pos);
1198
}
1199

    
1200
/* iTunes meta data tag */
1201
static int mov_write_meta_tag(ByteIOContext *pb, MOVContext* mov,
1202
                              AVFormatContext *s)
1203
{
1204
    int size = 0;
1205

    
1206
    // only save meta tag if required
1207
    if ( s->title[0] || s->author[0] || s->album[0] || s->year ||
1208
         s->comment[0] || s->genre[0] || s->track ) {
1209
        offset_t pos = url_ftell(pb);
1210
        put_be32(pb, 0); /* size */
1211
        put_tag(pb, "meta");
1212
        put_be32(pb, 0);
1213
        mov_write_itunes_hdlr_tag(pb, mov, s);
1214
        mov_write_ilst_tag(pb, mov, s);
1215
        size = updateSize(pb, pos);
1216
    }
1217
    return size;
1218
}
1219

    
1220
static int mov_write_udta_tag(ByteIOContext *pb, MOVContext* mov,
1221
                              AVFormatContext *s)
1222
{
1223
    offset_t pos = url_ftell(pb);
1224
    int i;
1225

    
1226
    put_be32(pb, 0); /* size */
1227
    put_tag(pb, "udta");
1228

    
1229
    /* iTunes meta data */
1230
    mov_write_meta_tag(pb, mov, s);
1231

    
1232
    if(mov->mode == MODE_MOV){ // the title field breaks gtkpod with mp4 and my suspicion is that stuff is not valid in mp4
1233
        /* Requirements */
1234
        for (i=0; i<mov->nb_streams; i++) {
1235
            if(mov->tracks[i].entry <= 0) continue;
1236
            if (mov->tracks[i].enc->codec_id == CODEC_ID_AAC ||
1237
                mov->tracks[i].enc->codec_id == CODEC_ID_MPEG4) {
1238
                mov_write_string_tag(pb, "\251req", "QuickTime 6.0 or greater", 0);
1239
                break;
1240
            }
1241
        }
1242

    
1243
        mov_write_string_tag(pb, "\251nam", s->title         , 0);
1244
        mov_write_string_tag(pb, "\251aut", s->author        , 0);
1245
        mov_write_string_tag(pb, "\251alb", s->album         , 0);
1246
        mov_write_day_tag(pb, s->year, 0);
1247
        if(mov->tracks[0].enc && !(mov->tracks[0].enc->flags & CODEC_FLAG_BITEXACT))
1248
            mov_write_string_tag(pb, "\251enc", LIBAVFORMAT_IDENT, 0);
1249
        mov_write_string_tag(pb, "\251des", s->comment       , 0);
1250
        mov_write_string_tag(pb, "\251gen", s->genre         , 0);
1251
    }
1252

    
1253
    return updateSize(pb, pos);
1254
}
1255

    
1256
static int utf8len(uint8_t *b){
1257
    int len=0;
1258
    int val;
1259
    while(*b){
1260
        GET_UTF8(val, *b++, return -1;)
1261
        len++;
1262
    }
1263
    return len;
1264
}
1265

    
1266
static int ascii_to_wc (ByteIOContext *pb, uint8_t *b)
1267
{
1268
    int val;
1269
    while(*b){
1270
        GET_UTF8(val, *b++, return -1;)
1271
        put_be16(pb, val);
1272
    }
1273
    put_be16(pb, 0x00);
1274
    return 0;
1275
}
1276

    
1277
static uint16_t language_code (const char *str)
1278
{
1279
    return ((((str[0]-0x60) & 0x1F)<<10) + (((str[1]-0x60) & 0x1F)<<5) + ((str[2]-0x60) & 0x1F));
1280
}
1281

    
1282
static int mov_write_uuidusmt_tag (ByteIOContext *pb, AVFormatContext *s)
1283
{
1284
    size_t len, size;
1285
    offset_t pos, curpos;
1286

    
1287
    size = 0;
1288
    if (s->title[0]) {
1289
        pos = url_ftell(pb);
1290
        put_be32(pb, 0); /* size placeholder*/
1291
        put_tag(pb, "uuid");
1292
        put_tag(pb, "USMT");
1293
        put_be32(pb, 0x21d24fce ); /* 96 bit UUID */
1294
        put_be32(pb, 0xbb88695c );
1295
        put_be32(pb, 0xfac9c740 );
1296
        size += 24;
1297

    
1298
        put_be32(pb, 0); /* size placeholder*/
1299
        put_tag(pb, "MTDT");
1300
        put_be16(pb, 4);
1301
        size += 10;
1302

    
1303
        // ?
1304
        put_be16(pb, 0x0C);                 /* size */
1305
        put_be32(pb, 0x0B);                 /* type */
1306
        put_be16(pb, language_code("und")); /* language */
1307
        put_be16(pb, 0x0);                  /* ? */
1308
        put_be16(pb, 0x021C);               /* data */
1309
        size += 12;
1310

    
1311
        // Encoder
1312
        len = utf8len(LIBAVCODEC_IDENT)+1;
1313
        if(len<=0)
1314
            goto not_utf8;
1315
        put_be16(pb, len*2+10);             /* size */
1316
        put_be32(pb, 0x04);                 /* type */
1317
        put_be16(pb, language_code("eng")); /* language */
1318
        put_be16(pb, 0x01);                 /* ? */
1319
        ascii_to_wc(pb, LIBAVCODEC_IDENT);
1320
        size += len*2+10;
1321

    
1322
        // Title
1323
        len = utf8len(s->title)+1;
1324
        if(len<=0)
1325
            goto not_utf8;
1326
        put_be16(pb, len*2+10);             /* size */
1327
        put_be32(pb, 0x01);                 /* type */
1328
        put_be16(pb, language_code("eng")); /* language */
1329
        put_be16(pb, 0x01);                 /* ? */
1330
        ascii_to_wc (pb, s->title);
1331
        size += len*2+10;
1332

    
1333
        // Date
1334
//        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);
1335
        len = utf8len("2006/04/01 11:11:11")+1;
1336
        if(len<=0)
1337
            goto not_utf8;
1338
        put_be16(pb, len*2+10);    /* size */
1339
        put_be32(pb, 0x03);        /* type */
1340
        put_be16(pb, language_code("und")); /* language */
1341
        put_be16(pb, 0x01);        /* ? */
1342
        ascii_to_wc (pb, "2006/04/01 11:11:11");
1343
        size += len*2+10;
1344

    
1345
        // size
1346
        curpos = url_ftell(pb);
1347
        url_fseek(pb, pos, SEEK_SET);
1348
        put_be32(pb, size);
1349
        url_fseek(pb, pos+24, SEEK_SET);
1350
        put_be32(pb, size-24);
1351
        url_fseek(pb, curpos, SEEK_SET);
1352
    }
1353

    
1354
    return size;
1355
not_utf8:
1356
    av_log(s, AV_LOG_ERROR, "not utf8\n");
1357
    return -1;
1358
}
1359

    
1360
static int mov_write_moov_tag(ByteIOContext *pb, MOVContext *mov,
1361
                              AVFormatContext *s)
1362
{
1363
    int i;
1364
    offset_t pos = url_ftell(pb);
1365
    put_be32(pb, 0); /* size placeholder*/
1366
    put_tag(pb, "moov");
1367
    mov->timescale = globalTimescale;
1368

    
1369
    for (i=0; i<mov->nb_streams; i++) {
1370
        if(mov->tracks[i].entry <= 0) continue;
1371

    
1372
        mov->tracks[i].time = mov->time;
1373
        mov->tracks[i].trackID = i+1;
1374
    }
1375

    
1376
    mov_write_mvhd_tag(pb, mov);
1377
    //mov_write_iods_tag(pb, mov);
1378
    for (i=0; i<mov->nb_streams; i++) {
1379
        if(mov->tracks[i].entry > 0) {
1380
            mov_write_trak_tag(pb, &(mov->tracks[i]));
1381
        }
1382
    }
1383

    
1384
    if (mov->mode == MODE_PSP)
1385
        mov_write_uuidusmt_tag(pb, s);
1386
    else
1387
        mov_write_udta_tag(pb, mov, s);
1388

    
1389
    return updateSize(pb, pos);
1390
}
1391

    
1392
static int mov_write_mdat_tag(ByteIOContext *pb, MOVContext* mov)
1393
{
1394
    put_be32(pb, 8);    // placeholder for extended size field (64 bit)
1395
    put_tag(pb, mov->mode == MODE_MOV ? "wide" : "free");
1396

    
1397
    mov->mdat_pos = url_ftell(pb);
1398
    put_be32(pb, 0); /* size placeholder*/
1399
    put_tag(pb, "mdat");
1400
    return 0;
1401
}
1402

    
1403
/* TODO: This needs to be more general */
1404
static void mov_write_ftyp_tag (ByteIOContext *pb, AVFormatContext *s)
1405
{
1406
    MOVContext *mov = s->priv_data;
1407

    
1408
    put_be32(pb, 0x14 ); /* size */
1409
    put_tag(pb, "ftyp");
1410

    
1411
    if ( mov->mode == MODE_3GP )
1412
        put_tag(pb, "3gp4");
1413
    else if ( mov->mode == MODE_3G2 )
1414
        put_tag(pb, "3g2a");
1415
    else if ( mov->mode == MODE_PSP )
1416
        put_tag(pb, "MSNV");
1417
    else if ( mov->mode == MODE_MP4 )
1418
        put_tag(pb, "isom");
1419
    else
1420
        put_tag(pb, "qt  ");
1421

    
1422
    put_be32(pb, 0x200 );
1423

    
1424
    if ( mov->mode == MODE_3GP )
1425
        put_tag(pb, "3gp4");
1426
    else if ( mov->mode == MODE_3G2 )
1427
        put_tag(pb, "3g2a");
1428
    else if ( mov->mode == MODE_PSP )
1429
        put_tag(pb, "MSNV");
1430
    else if ( mov->mode == MODE_MP4 )
1431
        put_tag(pb, "mp41");
1432
    else
1433
        put_tag(pb, "qt  ");
1434
}
1435

    
1436
static void mov_write_uuidprof_tag(ByteIOContext *pb, AVFormatContext *s)
1437
{
1438
    AVCodecContext *VideoCodec = s->streams[0]->codec;
1439
    AVCodecContext *AudioCodec = s->streams[1]->codec;
1440
    int AudioRate = AudioCodec->sample_rate;
1441
    int FrameRate = ((VideoCodec->time_base.den) * (0x10000))/ (VideoCodec->time_base.num);
1442
    int audio_kbitrate= AudioCodec->bit_rate / 1000;
1443
    int video_kbitrate= FFMIN(VideoCodec->bit_rate / 1000, 800 - audio_kbitrate);
1444

    
1445
    put_be32(pb, 0x94 ); /* size */
1446
    put_tag(pb, "uuid");
1447
    put_tag(pb, "PROF");
1448

    
1449
    put_be32(pb, 0x21d24fce ); /* 96 bit UUID */
1450
    put_be32(pb, 0xbb88695c );
1451
    put_be32(pb, 0xfac9c740 );
1452

    
1453
    put_be32(pb, 0x0 );  /* ? */
1454
    put_be32(pb, 0x3 );  /* 3 sections ? */
1455

    
1456
    put_be32(pb, 0x14 ); /* size */
1457
    put_tag(pb, "FPRF");
1458
    put_be32(pb, 0x0 );  /* ? */
1459
    put_be32(pb, 0x0 );  /* ? */
1460
    put_be32(pb, 0x0 );  /* ? */
1461

    
1462
    put_be32(pb, 0x2c );  /* size */
1463
    put_tag(pb, "APRF");   /* audio */
1464
    put_be32(pb, 0x0 );
1465
    put_be32(pb, 0x2 );   /* TrackID */
1466
    put_tag(pb, "mp4a");
1467
    put_be32(pb, 0x20f );
1468
    put_be32(pb, 0x0 );
1469
    put_be32(pb, audio_kbitrate);
1470
    put_be32(pb, audio_kbitrate);
1471
    put_be32(pb, AudioRate );
1472
    put_be32(pb, AudioCodec->channels );
1473

    
1474
    put_be32(pb, 0x34 );  /* size */
1475
    put_tag(pb, "VPRF");   /* video */
1476
    put_be32(pb, 0x0 );
1477
    put_be32(pb, 0x1 );    /* TrackID */
1478
    if (VideoCodec->codec_id == CODEC_ID_H264) {
1479
        put_tag(pb, "avc1");
1480
        put_be16(pb, 0x014D );
1481
        put_be16(pb, 0x0015 );
1482
    } else {
1483
        put_tag(pb, "mp4v");
1484
        put_be16(pb, 0x0000 );
1485
        put_be16(pb, 0x0103 );
1486
    }
1487
    put_be32(pb, 0x0 );
1488
    put_be32(pb, video_kbitrate);
1489
    put_be32(pb, video_kbitrate);
1490
    put_be32(pb, FrameRate);
1491
    put_be32(pb, FrameRate);
1492
    put_be16(pb, VideoCodec->width);
1493
    put_be16(pb, VideoCodec->height);
1494
    put_be32(pb, 0x010001); /* ? */
1495
}
1496

    
1497
static int mov_write_header(AVFormatContext *s)
1498
{
1499
    ByteIOContext *pb = &s->pb;
1500
    MOVContext *mov = s->priv_data;
1501
    int i;
1502

    
1503
    if (url_is_streamed(&s->pb)) {
1504
        av_log(s, AV_LOG_ERROR, "muxer does not support non seekable output\n");
1505
        return -1;
1506
    }
1507

    
1508
    /* Default mode == MP4 */
1509
    mov->mode = MODE_MP4;
1510

    
1511
    if (s->oformat != NULL) {
1512
        if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
1513
        else if (!strcmp("3g2", s->oformat->name)) mov->mode = MODE_3G2;
1514
        else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
1515
        else if (!strcmp("psp", s->oformat->name)) mov->mode = MODE_PSP;
1516

    
1517
        mov_write_ftyp_tag(pb,s);
1518
        if ( mov->mode == MODE_PSP ) {
1519
            if ( s->nb_streams != 2 ) {
1520
                av_log(s, AV_LOG_ERROR, "PSP mode need one video and one audio stream\n");
1521
                return -1;
1522
            }
1523
            mov_write_uuidprof_tag(pb,s);
1524
        }
1525
    }
1526

    
1527
    for(i=0; i<s->nb_streams; i++){
1528
        AVStream *st= s->streams[i];
1529
        MOVTrack *track= &mov->tracks[i];
1530

    
1531
        track->enc = st->codec;
1532
        track->language = ff_mov_iso639_to_lang(st->language, mov->mode != MODE_MOV);
1533
        track->mode = mov->mode;
1534
        if(st->codec->codec_type == CODEC_TYPE_VIDEO){
1535
            track->tag = mov_find_video_codec_tag(s, track);
1536
            track->timescale = st->codec->time_base.den;
1537
            av_set_pts_info(st, 64, 1, st->codec->time_base.den);
1538
            if (track->timescale > 100000)
1539
                av_log(NULL, AV_LOG_WARNING,
1540
                       "WARNING codec timebase is very high. If duration is too long,\n"
1541
                       "file may not be playable by quicktime. Specify a shorter timebase\n"
1542
                       "or choose different container.\n");
1543
        }else if(st->codec->codec_type == CODEC_TYPE_AUDIO){
1544
            track->tag = mov_find_audio_codec_tag(s, track);
1545
            track->timescale = st->codec->sample_rate;
1546
            av_set_pts_info(st, 64, 1, st->codec->sample_rate);
1547
            if(!st->codec->frame_size){
1548
                av_log(s, AV_LOG_ERROR, "track %d: codec frame size is not set\n", i);
1549
                return -1;
1550
            }else if(st->codec->frame_size > 1){ /* assume compressed audio */
1551
                track->audio_vbr = 1;
1552
            }else{
1553
                track->sampleSize = (av_get_bits_per_sample(st->codec->codec_id) >> 3) * st->codec->channels;
1554
            }
1555
        }
1556
    }
1557

    
1558
    mov_write_mdat_tag(pb, mov);
1559
    mov->time = s->timestamp + 0x7C25B080; //1970 based -> 1904 based
1560
    mov->nb_streams = s->nb_streams;
1561

    
1562
    put_flush_packet(pb);
1563

    
1564
    return 0;
1565
}
1566

    
1567
static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
1568
{
1569
    MOVContext *mov = s->priv_data;
1570
    ByteIOContext *pb = &s->pb;
1571
    MOVTrack *trk = &mov->tracks[pkt->stream_index];
1572
    AVCodecContext *enc = trk->enc;
1573
    unsigned int samplesInChunk = 0;
1574
    int size= pkt->size;
1575

    
1576
    if (url_is_streamed(&s->pb)) return 0; /* Can't handle that */
1577
    if (!size) return 0; /* Discard 0 sized packets */
1578

    
1579
    if (enc->codec_id == CODEC_ID_AMR_NB) {
1580
        /* We must find out how many AMR blocks there are in one packet */
1581
        static uint16_t packed_size[16] =
1582
            {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 0};
1583
        int len = 0;
1584

    
1585
        while (len < size && samplesInChunk < 100) {
1586
            len += packed_size[(pkt->data[len] >> 3) & 0x0F];
1587
            samplesInChunk++;
1588
        }
1589
        if(samplesInChunk > 1){
1590
            av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, implement a AVParser for it\n");
1591
            return -1;
1592
        }
1593
    } else if (trk->sampleSize)
1594
        samplesInChunk = size/trk->sampleSize;
1595
    else
1596
        samplesInChunk = 1;
1597

    
1598
    /* copy extradata if it exists */
1599
    if (trk->vosLen == 0 && enc->extradata_size > 0) {
1600
        trk->vosLen = enc->extradata_size;
1601
        trk->vosData = av_malloc(trk->vosLen);
1602
        memcpy(trk->vosData, enc->extradata, trk->vosLen);
1603
    }
1604

    
1605
    if (enc->codec_id == CODEC_ID_H264 && trk->vosLen > 0 && *(uint8_t *)trk->vosData != 1) {
1606
        /* from x264 or from bytestream h264 */
1607
        /* nal reformating needed */
1608
        avc_parse_nal_units(&pkt->data, &pkt->size);
1609
        assert(pkt->size);
1610
        size = pkt->size;
1611
    } else if (enc->codec_id == CODEC_ID_DNXHD && !trk->vosLen) {
1612
        /* copy frame header to create needed atoms */
1613
        if (size < 640)
1614
            return -1;
1615
        trk->vosLen = 640;
1616
        trk->vosData = av_malloc(trk->vosLen);
1617
        memcpy(trk->vosData, pkt->data, 640);
1618
    }
1619

    
1620
    if (!(trk->entry % MOV_INDEX_CLUSTER_SIZE)) {
1621
        trk->cluster = av_realloc(trk->cluster, (trk->entry + MOV_INDEX_CLUSTER_SIZE) * sizeof(*trk->cluster));
1622
        if (!trk->cluster)
1623
            return -1;
1624
    }
1625

    
1626
    trk->cluster[trk->entry].pos = url_ftell(pb);
1627
    trk->cluster[trk->entry].samplesInChunk = samplesInChunk;
1628
    trk->cluster[trk->entry].size = size;
1629
    trk->cluster[trk->entry].entries = samplesInChunk;
1630
    trk->cluster[trk->entry].dts = pkt->dts;
1631
    trk->trackDuration = pkt->dts - trk->cluster[0].dts + pkt->duration;
1632

    
1633
    if(enc->codec_type == CODEC_TYPE_VIDEO) {
1634
        if (pkt->dts != pkt->pts)
1635
            trk->hasBframes = 1;
1636
        trk->cluster[trk->entry].cts = pkt->pts - pkt->dts;
1637
        trk->cluster[trk->entry].key_frame = !!(pkt->flags & PKT_FLAG_KEY);
1638
        if(trk->cluster[trk->entry].key_frame)
1639
            trk->hasKeyframes++;
1640
    }
1641
    trk->entry++;
1642
    trk->sampleCount += samplesInChunk;
1643
    mov->mdat_size += size;
1644

    
1645
    put_buffer(pb, pkt->data, size);
1646

    
1647
    put_flush_packet(pb);
1648
    return 0;
1649
}
1650

    
1651
static int mov_write_trailer(AVFormatContext *s)
1652
{
1653
    MOVContext *mov = s->priv_data;
1654
    ByteIOContext *pb = &s->pb;
1655
    int res = 0;
1656
    int i;
1657

    
1658
    offset_t moov_pos = url_ftell(pb);
1659

    
1660
    /* Write size of mdat tag */
1661
    if (mov->mdat_size+8 <= UINT32_MAX) {
1662
        url_fseek(pb, mov->mdat_pos, SEEK_SET);
1663
        put_be32(pb, mov->mdat_size+8);
1664
    } else {
1665
        /* overwrite 'wide' placeholder atom */
1666
        url_fseek(pb, mov->mdat_pos - 8, SEEK_SET);
1667
        put_be32(pb, 1); /* special value: real atom size will be 64 bit value after tag field */
1668
        put_tag(pb, "mdat");
1669
        put_be64(pb, mov->mdat_size+16);
1670
    }
1671
    url_fseek(pb, moov_pos, SEEK_SET);
1672

    
1673
    mov_write_moov_tag(pb, mov, s);
1674

    
1675
    for (i=0; i<mov->nb_streams; i++) {
1676
        av_freep(&mov->tracks[i].cluster);
1677

    
1678
        if( mov->tracks[i].vosLen ) av_free( mov->tracks[i].vosData );
1679

    
1680
    }
1681

    
1682
    put_flush_packet(pb);
1683

    
1684
    return res;
1685
}
1686

    
1687
#ifdef CONFIG_MOV_MUXER
1688
AVOutputFormat mov_muxer = {
1689
    "mov",
1690
    "mov format",
1691
    NULL,
1692
    "mov",
1693
    sizeof(MOVContext),
1694
    CODEC_ID_AAC,
1695
    CODEC_ID_MPEG4,
1696
    mov_write_header,
1697
    mov_write_packet,
1698
    mov_write_trailer,
1699
    .flags = AVFMT_GLOBALHEADER,
1700
};
1701
#endif
1702
#ifdef CONFIG_TGP_MUXER
1703
AVOutputFormat tgp_muxer = {
1704
    "3gp",
1705
    "3gp format",
1706
    NULL,
1707
    "3gp",
1708
    sizeof(MOVContext),
1709
    CODEC_ID_AMR_NB,
1710
    CODEC_ID_H263,
1711
    mov_write_header,
1712
    mov_write_packet,
1713
    mov_write_trailer,
1714
    .flags = AVFMT_GLOBALHEADER,
1715
};
1716
#endif
1717
#ifdef CONFIG_MP4_MUXER
1718
AVOutputFormat mp4_muxer = {
1719
    "mp4",
1720
    "mp4 format",
1721
    "application/mp4",
1722
    "mp4,m4a",
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_PSP_MUXER
1733
AVOutputFormat psp_muxer = {
1734
    "psp",
1735
    "psp mp4 format",
1736
    NULL,
1737
    "mp4,psp",
1738
    sizeof(MOVContext),
1739
    CODEC_ID_AAC,
1740
    CODEC_ID_MPEG4,
1741
    mov_write_header,
1742
    mov_write_packet,
1743
    mov_write_trailer,
1744
    .flags = AVFMT_GLOBALHEADER,
1745
};
1746
#endif
1747
#ifdef CONFIG_TG2_MUXER
1748
AVOutputFormat tg2_muxer = {
1749
    "3g2",
1750
    "3gp2 format",
1751
    NULL,
1752
    "3g2",
1753
    sizeof(MOVContext),
1754
    CODEC_ID_AMR_NB,
1755
    CODEC_ID_H263,
1756
    mov_write_header,
1757
    mov_write_packet,
1758
    mov_write_trailer,
1759
    .flags = AVFMT_GLOBALHEADER,
1760
};
1761
#endif