Statistics
| Branch: | Revision:

ffmpeg / libavformat / movenc.c @ f956e129

History | View | Annotate | Download (53.4 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        sampleDuration;
58
    long        sampleSize;
59
    int         hasKeyframes;
60
    int         hasBframes;
61
    int         language;
62
    int         trackID;
63
    int         tag;
64
    AVCodecContext *enc;
65

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

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

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

    
90
    return curpos - pos;
91
}
92

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
289

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

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

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

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

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

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

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

    
326
static const CodecTag codec_movaudio_tags[] = {
327
    { CODEC_ID_PCM_MULAW, MKTAG('u', 'l', 'a', 'w') },
328
    { CODEC_ID_PCM_ALAW, MKTAG('a', 'l', 'a', 'w') },
329
    { CODEC_ID_ADPCM_IMA_QT, MKTAG('i', 'm', 'a', '4') },
330
    { CODEC_ID_MACE3, MKTAG('M', 'A', 'C', '3') },
331
    { CODEC_ID_MACE6, MKTAG('M', 'A', 'C', '6') },
332
    { CODEC_ID_AAC, MKTAG('m', 'p', '4', 'a') },
333
    { CODEC_ID_AMR_NB, MKTAG('s', 'a', 'm', 'r') },
334
    { CODEC_ID_AMR_WB, MKTAG('s', 'a', 'w', 'b') },
335
    { CODEC_ID_PCM_S16BE, MKTAG('t', 'w', 'o', 's') },
336
    { CODEC_ID_PCM_S16LE, MKTAG('s', 'o', 'w', 't') },
337
    { CODEC_ID_PCM_S24BE, MKTAG('i', 'n', '2', '4') },
338
    { CODEC_ID_PCM_S24LE, MKTAG('i', 'n', '2', '4') },
339
    { CODEC_ID_PCM_S32BE, MKTAG('i', 'n', '3', '2') },
340
    { CODEC_ID_PCM_S32LE, MKTAG('i', 'n', '3', '2') },
341
    { CODEC_ID_MP3, MKTAG('.', 'm', 'p', '3') },
342
    { CODEC_ID_NONE, 0 },
343
};
344

    
345
static int mov_write_audio_tag(ByteIOContext *pb, MOVTrack* track)
346
{
347
    offset_t pos = url_ftell(pb);
348
    int version = track->mode == MODE_MOV &&
349
        (track->audio_vbr ||
350
         track->enc->codec_id == CODEC_ID_PCM_S32LE ||
351
         track->enc->codec_id == CODEC_ID_PCM_S24LE);
352

    
353
    put_be32(pb, 0); /* size */
354
    put_le32(pb, track->tag); // store it byteswapped
355
    put_be32(pb, 0); /* Reserved */
356
    put_be16(pb, 0); /* Reserved */
357
    put_be16(pb, 1); /* Data-reference index, XXX  == 1 */
358

    
359
    /* SoundDescription */
360
    put_be16(pb, version); /* Version */
361
    put_be16(pb, 0); /* Revision level */
362
    put_be32(pb, 0); /* Reserved */
363

    
364
    put_be16(pb, track->mode == MODE_MOV ? track->enc->channels : 2); /* Number of channels */
365
    /* FIXME 8 bit for 'raw ' in mov */
366
    put_be16(pb, 16); /* Reserved */
367

    
368
    put_be16(pb, track->mode == MODE_MOV && track->audio_vbr ? -2 : 0); /* compression ID */
369
    put_be16(pb, 0); /* packet size (= 0) */
370
    put_be16(pb, track->timescale); /* Time scale */
371
    put_be16(pb, 0); /* Reserved */
372

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

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

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

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

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

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

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

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

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

    
453
    return end + 3;
454
}
455

    
456
static void avc_parse_nal_units(uint8_t **buf, int *size)
457
{
458
    ByteIOContext pb;
459
    uint8_t *p = *buf;
460
    uint8_t *end = p + *size;
461
    uint8_t *nal_start, *nal_end;
462

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

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

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

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

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

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

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

    
530
static const CodecTag codec_movvideo_tags[] = {
531
    { CODEC_ID_SVQ1, MKTAG('S', 'V', 'Q', '1') },
532
    { CODEC_ID_SVQ3, MKTAG('S', 'V', 'Q', '3') },
533
    { CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 'v') },
534
    { CODEC_ID_H263, MKTAG('h', '2', '6', '3') },
535
    { CODEC_ID_H263, MKTAG('s', '2', '6', '3') },
536
    { CODEC_ID_H264, MKTAG('a', 'v', 'c', '1') },
537
    /* special handling in mov_find_video_codec_tag */
538
    { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', ' ') }, /* DV NTSC */
539
    { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', 'p') }, /* DV PAL */
540
    { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'p', 'p') }, /* DVCPRO PAL */
541
    { CODEC_ID_DVVIDEO, MKTAG('d', 'v', '5', 'n') }, /* DVCPRO50 NTSC */
542
    { CODEC_ID_DVVIDEO, MKTAG('d', 'v', '5', 'p') }, /* DVCPRO50 PAL */
543
    { CODEC_ID_NONE, 0 },
544
};
545

    
546
static int mov_find_video_codec_tag(AVFormatContext *s, MOVTrack *track)
547
{
548
    int tag = track->enc->codec_tag;
549
    if (!tag) {
550
        if (track->enc->codec_id == CODEC_ID_DVVIDEO) {
551
            if (track->enc->height == 480) { /* NTSC */
552
                if (track->enc->pix_fmt == PIX_FMT_YUV422P)
553
                    tag = MKTAG('d', 'v', '5', 'n');
554
                else
555
                    tag = MKTAG('d', 'v', 'c', ' ');
556
            } else { /* assume PAL */
557
                if (track->enc->pix_fmt == PIX_FMT_YUV422P)
558
                    tag = MKTAG('d', 'v', '5', 'p');
559
                else if (track->enc->pix_fmt == PIX_FMT_YUV420P)
560
                    tag = MKTAG('d', 'v', 'c', 'p');
561
                else
562
                    tag = MKTAG('d', 'v', 'p', 'p');
563
            }
564
        } else if (track->enc->codec_id == CODEC_ID_H263) {
565
            if (track->mode == MODE_MOV)
566
                tag = MKTAG('h', '2', '6', '3');
567
            else
568
                tag = MKTAG('s', '2', '6', '3');
569
        } else {
570
            tag = codec_get_tag(codec_movvideo_tags, track->enc->codec_id);
571
        }
572
    }
573
    // if no mac fcc found, try with Microsoft tags
574
    if (!tag) {
575
        tag = codec_get_tag(codec_bmp_tags, track->enc->codec_id);
576
        if (tag) {
577
            av_log(s, AV_LOG_INFO, "Warning, using MS style video codec tag, the file may be unplayable!\n");
578
        }
579
    }
580
    assert(tag);
581
    return tag;
582
}
583

    
584
static int mov_find_audio_codec_tag(AVFormatContext *s, MOVTrack *track)
585
{
586
    int tag = track->enc->codec_tag;
587
    if (!tag) {
588
        tag = codec_get_tag(codec_movaudio_tags, track->enc->codec_id);
589
    }
590
    // if no mac fcc found, try with Microsoft tags
591
    if (!tag) {
592
        int ms_tag = codec_get_tag(codec_wav_tags, track->enc->codec_id);
593
        if (ms_tag) {
594
            tag = MKTAG('m', 's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
595
            av_log(s, AV_LOG_INFO, "Warning, using MS style audio codec tag, the file may be unplayable!\n");
596
        }
597
    }
598
    assert(tag);
599
    return tag;
600
}
601

    
602
static int mov_write_video_tag(ByteIOContext *pb, MOVTrack* track)
603
{
604
    offset_t pos = url_ftell(pb);
605
    char compressor_name[32];
606

    
607
    put_be32(pb, 0); /* size */
608
    put_le32(pb, track->tag); // store it byteswapped
609
    put_be32(pb, 0); /* Reserved */
610
    put_be16(pb, 0); /* Reserved */
611
    put_be16(pb, 1); /* Data-reference index */
612

    
613
    put_be16(pb, 0); /* Codec stream version */
614
    put_be16(pb, 0); /* Codec stream revision (=0) */
615
    if (track->mode == MODE_MOV) {
616
        put_tag(pb, "FFMP"); /* Vendor */
617
        if(track->enc->codec_id == CODEC_ID_RAWVIDEO) {
618
            put_be32(pb, 0); /* Temporal Quality */
619
            put_be32(pb, 0x400); /* Spatial Quality = lossless*/
620
        } else {
621
            put_be32(pb, 0x200); /* Temporal Quality = normal */
622
            put_be32(pb, 0x200); /* Spatial Quality = normal */
623
        }
624
    } else {
625
        put_be32(pb, 0); /* Reserved */
626
        put_be32(pb, 0); /* Reserved */
627
        put_be32(pb, 0); /* Reserved */
628
    }
629
    put_be16(pb, track->enc->width); /* Video width */
630
    put_be16(pb, track->enc->height); /* Video height */
631
    put_be32(pb, 0x00480000); /* Horizontal resolution 72dpi */
632
    put_be32(pb, 0x00480000); /* Vertical resolution 72dpi */
633
    put_be32(pb, 0); /* Data size (= 0) */
634
    put_be16(pb, 1); /* Frame count (= 1) */
635

    
636
    memset(compressor_name,0,32);
637
    /* FIXME not sure, ISO 14496-1 draft where it shall be set to 0 */
638
    if (track->mode == MODE_MOV && track->enc->codec && track->enc->codec->name)
639
        strncpy(compressor_name,track->enc->codec->name,31);
640
    put_byte(pb, strlen(compressor_name));
641
    put_buffer(pb, compressor_name, 31);
642

    
643
    put_be16(pb, 0x18); /* Reserved */
644
    put_be16(pb, 0xffff); /* Reserved */
645
    if(track->enc->codec_id == CODEC_ID_MPEG4)
646
        mov_write_esds_tag(pb, track);
647
    else if(track->enc->codec_id == CODEC_ID_H263)
648
        mov_write_d263_tag(pb);
649
    else if(track->enc->codec_id == CODEC_ID_SVQ3)
650
        mov_write_svq3_tag(pb);
651
    else if(track->enc->codec_id == CODEC_ID_H264)
652
        mov_write_avcc_tag(pb, track);
653

    
654
    return updateSize (pb, pos);
655
}
656

    
657
static int mov_write_stsd_tag(ByteIOContext *pb, MOVTrack* track)
658
{
659
    offset_t pos = url_ftell(pb);
660
    put_be32(pb, 0); /* size */
661
    put_tag(pb, "stsd");
662
    put_be32(pb, 0); /* version & flags */
663
    put_be32(pb, 1); /* entry count */
664
    if (track->enc->codec_type == CODEC_TYPE_VIDEO)
665
        mov_write_video_tag(pb, track);
666
    else if (track->enc->codec_type == CODEC_TYPE_AUDIO)
667
        mov_write_audio_tag(pb, track);
668
    return updateSize(pb, pos);
669
}
670

    
671
static int mov_write_ctts_tag(ByteIOContext *pb, MOVTrack* track)
672
{
673
    Time2Sample *ctts_entries;
674
    uint32_t entries = 0;
675
    uint32_t atom_size;
676
    int i;
677

    
678
    ctts_entries = av_malloc((track->entry + 1) * sizeof(*ctts_entries)); /* worst case */
679
    ctts_entries[0].count = 1;
680
    ctts_entries[0].duration = track->cluster[0].cts;
681
    for (i=1; i<track->entry; i++) {
682
        if (track->cluster[i].cts == ctts_entries[entries].duration) {
683
            ctts_entries[entries].count++; /* compress */
684
        } else {
685
            entries++;
686
            ctts_entries[entries].duration = track->cluster[i].cts;
687
            ctts_entries[entries].count = 1;
688
        }
689
    }
690
    entries++; /* last one */
691
    atom_size = 16 + (entries * 8);
692
    put_be32(pb, atom_size); /* size */
693
    put_tag(pb, "ctts");
694
    put_be32(pb, 0); /* version & flags */
695
    put_be32(pb, entries); /* entry count */
696
    for (i=0; i<entries; i++) {
697
        put_be32(pb, ctts_entries[i].count);
698
        put_be32(pb, ctts_entries[i].duration);
699
    }
700
    av_free(ctts_entries);
701
    return atom_size;
702
}
703

    
704
/* Time to sample atom */
705
static int mov_write_stts_tag(ByteIOContext *pb, MOVTrack* track)
706
{
707
    Time2Sample *stts_entries;
708
    uint32_t entries = -1;
709
    uint32_t atom_size;
710
    int i;
711

    
712
    if (track->enc->codec_type == CODEC_TYPE_AUDIO && !track->audio_vbr) {
713
        stts_entries = av_malloc(sizeof(*stts_entries)); /* one entry */
714
        stts_entries[0].count = track->sampleCount;
715
        stts_entries[0].duration = 1;
716
        entries = 1;
717
    } else {
718
        stts_entries = av_malloc(track->entry * sizeof(*stts_entries)); /* worst case */
719
        for (i=0; i<track->entry; i++) {
720
            int64_t duration = i + 1 == track->entry ?
721
                track->trackDuration - track->cluster[i].dts + track->cluster[0].dts : /* readjusting */
722
                track->cluster[i+1].dts - track->cluster[i].dts;
723
            if (i && duration == stts_entries[entries].duration) {
724
                stts_entries[entries].count++; /* compress */
725
            } else {
726
                entries++;
727
                stts_entries[entries].duration = duration;
728
                stts_entries[entries].count = 1;
729
            }
730
        }
731
        entries++; /* last one */
732
    }
733
    atom_size = 16 + (entries * 8);
734
    put_be32(pb, atom_size); /* size */
735
    put_tag(pb, "stts");
736
    put_be32(pb, 0); /* version & flags */
737
    put_be32(pb, entries); /* entry count */
738
    for (i=0; i<entries; i++) {
739
        put_be32(pb, stts_entries[i].count);
740
        put_be32(pb, stts_entries[i].duration);
741
    }
742
    av_free(stts_entries);
743
    return atom_size;
744
}
745

    
746
static int mov_write_dref_tag(ByteIOContext *pb)
747
{
748
    put_be32(pb, 28); /* size */
749
    put_tag(pb, "dref");
750
    put_be32(pb, 0); /* version & flags */
751
    put_be32(pb, 1); /* entry count */
752

    
753
    put_be32(pb, 0xc); /* size */
754
    put_tag(pb, "url ");
755
    put_be32(pb, 1); /* version & flags */
756

    
757
    return 28;
758
}
759

    
760
static int mov_write_stbl_tag(ByteIOContext *pb, MOVTrack* track)
761
{
762
    offset_t pos = url_ftell(pb);
763
    put_be32(pb, 0); /* size */
764
    put_tag(pb, "stbl");
765
    mov_write_stsd_tag(pb, track);
766
    mov_write_stts_tag(pb, track);
767
    if (track->enc->codec_type == CODEC_TYPE_VIDEO &&
768
        track->hasKeyframes < track->entry)
769
        mov_write_stss_tag(pb, track);
770
    if (track->enc->codec_type == CODEC_TYPE_VIDEO &&
771
        track->hasBframes)
772
        mov_write_ctts_tag(pb, track);
773
    mov_write_stsc_tag(pb, track);
774
    mov_write_stsz_tag(pb, track);
775
    mov_write_stco_tag(pb, track);
776
    return updateSize(pb, pos);
777
}
778

    
779
static int mov_write_dinf_tag(ByteIOContext *pb)
780
{
781
    offset_t pos = url_ftell(pb);
782
    put_be32(pb, 0); /* size */
783
    put_tag(pb, "dinf");
784
    mov_write_dref_tag(pb);
785
    return updateSize(pb, pos);
786
}
787

    
788
static int mov_write_smhd_tag(ByteIOContext *pb)
789
{
790
    put_be32(pb, 16); /* size */
791
    put_tag(pb, "smhd");
792
    put_be32(pb, 0); /* version & flags */
793
    put_be16(pb, 0); /* reserved (balance, normally = 0) */
794
    put_be16(pb, 0); /* reserved */
795
    return 16;
796
}
797

    
798
static int mov_write_vmhd_tag(ByteIOContext *pb)
799
{
800
    put_be32(pb, 0x14); /* size (always 0x14) */
801
    put_tag(pb, "vmhd");
802
    put_be32(pb, 0x01); /* version & flags */
803
    put_be64(pb, 0); /* reserved (graphics mode = copy) */
804
    return 0x14;
805
}
806

    
807
static int mov_write_hdlr_tag(ByteIOContext *pb, MOVTrack* track)
808
{
809
    const char *descr, *hdlr, *hdlr_type;
810
    offset_t pos = url_ftell(pb);
811

    
812
    if (!track) { /* no media --> data handler */
813
        hdlr = "dhlr";
814
        hdlr_type = "url ";
815
        descr = "DataHandler";
816
    } else {
817
        hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
818
        if (track->enc->codec_type == CODEC_TYPE_VIDEO) {
819
            hdlr_type = "vide";
820
            descr = "VideoHandler";
821
        } else {
822
            hdlr_type = "soun";
823
            descr = "SoundHandler";
824
        }
825
    }
826

    
827
    put_be32(pb, 0); /* size */
828
    put_tag(pb, "hdlr");
829
    put_be32(pb, 0); /* Version & flags */
830
    put_buffer(pb, hdlr, 4); /* handler */
831
    put_tag(pb, hdlr_type); /* handler type */
832
    put_be32(pb ,0); /* reserved */
833
    put_be32(pb ,0); /* reserved */
834
    put_be32(pb ,0); /* reserved */
835
    put_byte(pb, strlen(descr)); /* string counter */
836
    put_buffer(pb, descr, strlen(descr)); /* handler description */
837
    return updateSize(pb, pos);
838
}
839

    
840
static int mov_write_minf_tag(ByteIOContext *pb, MOVTrack* track)
841
{
842
    offset_t pos = url_ftell(pb);
843
    put_be32(pb, 0); /* size */
844
    put_tag(pb, "minf");
845
    if(track->enc->codec_type == CODEC_TYPE_VIDEO)
846
        mov_write_vmhd_tag(pb);
847
    else
848
        mov_write_smhd_tag(pb);
849
    if (track->mode == MODE_MOV) /* FIXME: Why do it for MODE_MOV only ? */
850
        mov_write_hdlr_tag(pb, NULL);
851
    mov_write_dinf_tag(pb);
852
    mov_write_stbl_tag(pb, track);
853
    return updateSize(pb, pos);
854
}
855

    
856
static int mov_write_mdhd_tag(ByteIOContext *pb, MOVTrack* track)
857
{
858
    int version = track->trackDuration < INT32_MAX ? 0 : 1;
859

    
860
    (version == 1) ? put_be32(pb, 44) : put_be32(pb, 32); /* size */
861
    put_tag(pb, "mdhd");
862
    put_byte(pb, version);
863
    put_be24(pb, 0); /* flags */
864
    if (version == 1) {
865
        put_be64(pb, track->time);
866
        put_be64(pb, track->time);
867
    } else {
868
        put_be32(pb, track->time); /* creation time */
869
        put_be32(pb, track->time); /* modification time */
870
    }
871
    put_be32(pb, track->timescale); /* time scale (sample rate for audio) */
872
    (version == 1) ? put_be64(pb, track->trackDuration) : put_be32(pb, track->trackDuration); /* duration */
873
    put_be16(pb, track->language); /* language */
874
    put_be16(pb, 0); /* reserved (quality) */
875
    return 32;
876
}
877

    
878
static int mov_write_mdia_tag(ByteIOContext *pb, MOVTrack* track)
879
{
880
    offset_t pos = url_ftell(pb);
881
    put_be32(pb, 0); /* size */
882
    put_tag(pb, "mdia");
883
    mov_write_mdhd_tag(pb, track);
884
    mov_write_hdlr_tag(pb, track);
885
    mov_write_minf_tag(pb, track);
886
    return updateSize(pb, pos);
887
}
888

    
889
static int mov_write_tkhd_tag(ByteIOContext *pb, MOVTrack* track)
890
{
891
    int64_t duration = av_rescale_rnd(track->trackDuration, globalTimescale, track->timescale, AV_ROUND_UP);
892
    int version = duration < INT32_MAX ? 0 : 1;
893

    
894
    (version == 1) ? put_be32(pb, 104) : put_be32(pb, 92); /* size */
895
    put_tag(pb, "tkhd");
896
    put_byte(pb, version);
897
    put_be24(pb, 0xf); /* flags (track enabled) */
898
    if (version == 1) {
899
        put_be64(pb, track->time);
900
        put_be64(pb, track->time);
901
    } else {
902
        put_be32(pb, track->time); /* creation time */
903
        put_be32(pb, track->time); /* modification time */
904
    }
905
    put_be32(pb, track->trackID); /* track-id */
906
    put_be32(pb, 0); /* reserved */
907
    (version == 1) ? put_be64(pb, duration) : put_be32(pb, duration);
908

    
909
    put_be32(pb, 0); /* reserved */
910
    put_be32(pb, 0); /* reserved */
911
    put_be32(pb, 0x0); /* reserved (Layer & Alternate group) */
912
    /* Volume, only for audio */
913
    if(track->enc->codec_type == CODEC_TYPE_AUDIO)
914
        put_be16(pb, 0x0100);
915
    else
916
        put_be16(pb, 0);
917
    put_be16(pb, 0); /* reserved */
918

    
919
    /* Matrix structure */
920
    put_be32(pb, 0x00010000); /* reserved */
921
    put_be32(pb, 0x0); /* reserved */
922
    put_be32(pb, 0x0); /* reserved */
923
    put_be32(pb, 0x0); /* reserved */
924
    put_be32(pb, 0x00010000); /* reserved */
925
    put_be32(pb, 0x0); /* reserved */
926
    put_be32(pb, 0x0); /* reserved */
927
    put_be32(pb, 0x0); /* reserved */
928
    put_be32(pb, 0x40000000); /* reserved */
929

    
930
    /* Track width and height, for visual only */
931
    if(track->enc->codec_type == CODEC_TYPE_VIDEO) {
932
        double sample_aspect_ratio = av_q2d(track->enc->sample_aspect_ratio);
933
        if( !sample_aspect_ratio ) sample_aspect_ratio = 1;
934
        put_be32(pb, sample_aspect_ratio * track->enc->width*0x10000);
935
        put_be32(pb, track->enc->height*0x10000);
936
    }
937
    else {
938
        put_be32(pb, 0);
939
        put_be32(pb, 0);
940
    }
941
    return 0x5c;
942
}
943

    
944
// This box seems important for the psp playback ... without it the movie seems to hang
945
static int mov_write_edts_tag(ByteIOContext *pb, MOVTrack *track)
946
{
947
    put_be32(pb, 0x24); /* size  */
948
    put_tag(pb, "edts");
949
    put_be32(pb, 0x1c); /* size  */
950
    put_tag(pb, "elst");
951
    put_be32(pb, 0x0);
952
    put_be32(pb, 0x1);
953

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

    
956
    put_be32(pb, track->cluster[0].cts); /* first pts is cts since dts is 0 */
957
    put_be32(pb, 0x00010000);
958
    return 0x24;
959
}
960

    
961
// goes at the end of each track!  ... Critical for PSP playback ("Incompatible data" without it)
962
static int mov_write_uuid_tag_psp(ByteIOContext *pb, MOVTrack *mov)
963
{
964
    put_be32(pb, 0x34); /* size ... reports as 28 in mp4box! */
965
    put_tag(pb, "uuid");
966
    put_tag(pb, "USMT");
967
    put_be32(pb, 0x21d24fce);
968
    put_be32(pb, 0xbb88695c);
969
    put_be32(pb, 0xfac9c740);
970
    put_be32(pb, 0x1c);     // another size here!
971
    put_tag(pb, "MTDT");
972
    put_be32(pb, 0x00010012);
973
    put_be32(pb, 0x0a);
974
    put_be32(pb, 0x55c40000);
975
    put_be32(pb, 0x1);
976
    put_be32(pb, 0x0);
977
    return 0x34;
978
}
979

    
980
static int mov_write_trak_tag(ByteIOContext *pb, MOVTrack* track)
981
{
982
    offset_t pos = url_ftell(pb);
983
    put_be32(pb, 0); /* size */
984
    put_tag(pb, "trak");
985
    mov_write_tkhd_tag(pb, track);
986
    if (track->mode == MODE_PSP || track->hasBframes)
987
        mov_write_edts_tag(pb, track);  // PSP Movies require edts box
988
    mov_write_mdia_tag(pb, track);
989
    if (track->mode == MODE_PSP)
990
        mov_write_uuid_tag_psp(pb,track);  // PSP Movies require this uuid box
991
    return updateSize(pb, pos);
992
}
993

    
994
#if 0
995
/* TODO: Not sorted out, but not necessary either */
996
static int mov_write_iods_tag(ByteIOContext *pb, MOVContext *mov)
997
{
998
    put_be32(pb, 0x15); /* size */
999
    put_tag(pb, "iods");
1000
    put_be32(pb, 0);    /* version & flags */
1001
    put_be16(pb, 0x1007);
1002
    put_byte(pb, 0);
1003
    put_be16(pb, 0x4fff);
1004
    put_be16(pb, 0xfffe);
1005
    put_be16(pb, 0x01ff);
1006
    return 0x15;
1007
}
1008
#endif
1009

    
1010
static int mov_write_mvhd_tag(ByteIOContext *pb, MOVContext *mov)
1011
{
1012
    int maxTrackID = 1, i;
1013
    int64_t maxTrackLenTemp, maxTrackLen = 0;
1014
    int version;
1015

    
1016
    for (i=0; i<mov->nb_streams; i++) {
1017
        if(mov->tracks[i].entry > 0) {
1018
            maxTrackLenTemp = av_rescale_rnd(mov->tracks[i].trackDuration, globalTimescale, mov->tracks[i].timescale, AV_ROUND_UP);
1019
            if(maxTrackLen < maxTrackLenTemp)
1020
                maxTrackLen = maxTrackLenTemp;
1021
            if(maxTrackID < mov->tracks[i].trackID)
1022
                maxTrackID = mov->tracks[i].trackID;
1023
        }
1024
    }
1025

    
1026
    version = maxTrackLen < UINT32_MAX ? 0 : 1;
1027
    (version == 1) ? put_be32(pb, 120) : put_be32(pb, 108); /* size */
1028
    put_tag(pb, "mvhd");
1029
    put_byte(pb, version);
1030
    put_be24(pb, 0); /* flags */
1031
    if (version == 1) {
1032
        put_be64(pb, mov->time);
1033
        put_be64(pb, mov->time);
1034
    } else {
1035
        put_be32(pb, mov->time); /* creation time */
1036
        put_be32(pb, mov->time); /* modification time */
1037
    }
1038
    put_be32(pb, mov->timescale); /* timescale */
1039
    (version == 1) ? put_be64(pb, maxTrackLen) : put_be32(pb, maxTrackLen); /* duration of longest track */
1040

    
1041
    put_be32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
1042
    put_be16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
1043
    put_be16(pb, 0); /* reserved */
1044
    put_be32(pb, 0); /* reserved */
1045
    put_be32(pb, 0); /* reserved */
1046

    
1047
    /* Matrix structure */
1048
    put_be32(pb, 0x00010000); /* reserved */
1049
    put_be32(pb, 0x0); /* reserved */
1050
    put_be32(pb, 0x0); /* reserved */
1051
    put_be32(pb, 0x0); /* reserved */
1052
    put_be32(pb, 0x00010000); /* reserved */
1053
    put_be32(pb, 0x0); /* reserved */
1054
    put_be32(pb, 0x0); /* reserved */
1055
    put_be32(pb, 0x0); /* reserved */
1056
    put_be32(pb, 0x40000000); /* reserved */
1057

    
1058
    put_be32(pb, 0); /* reserved (preview time) */
1059
    put_be32(pb, 0); /* reserved (preview duration) */
1060
    put_be32(pb, 0); /* reserved (poster time) */
1061
    put_be32(pb, 0); /* reserved (selection time) */
1062
    put_be32(pb, 0); /* reserved (selection duration) */
1063
    put_be32(pb, 0); /* reserved (current time) */
1064
    put_be32(pb, maxTrackID+1); /* Next track id */
1065
    return 0x6c;
1066
}
1067

    
1068
static int mov_write_itunes_hdlr_tag(ByteIOContext *pb, MOVContext* mov,
1069
                                     AVFormatContext *s)
1070
{
1071
    offset_t pos = url_ftell(pb);
1072
    put_be32(pb, 0); /* size */
1073
    put_tag(pb, "hdlr");
1074
    put_be32(pb, 0);
1075
    put_be32(pb, 0);
1076
    put_tag(pb, "mdir");
1077
    put_tag(pb, "appl");
1078
    put_be32(pb, 0);
1079
    put_be32(pb, 0);
1080
    put_be16(pb, 0);
1081
    return updateSize(pb, pos);
1082
}
1083

    
1084
/* helper function to write a data tag with the specified string as data */
1085
static int mov_write_string_data_tag(ByteIOContext *pb, const char *data, int long_style)
1086
{
1087
    if(long_style){
1088
        offset_t pos = url_ftell(pb);
1089
        put_be32(pb, 0); /* size */
1090
        put_tag(pb, "data");
1091
        put_be32(pb, 1);
1092
        put_be32(pb, 0);
1093
        put_buffer(pb, data, strlen(data));
1094
        return updateSize(pb, pos);
1095
    }else{
1096
        put_be16(pb, strlen(data)); /* string length */
1097
        put_be16(pb, 0);
1098
        put_buffer(pb, data, strlen(data));
1099
        return strlen(data) + 4;
1100
    }
1101
}
1102

    
1103
static int mov_write_string_tag(ByteIOContext *pb, const char *name, const char *value, int long_style){
1104
    int size = 0;
1105
    if ( value && value[0] ) {
1106
        offset_t pos = url_ftell(pb);
1107
        put_be32(pb, 0); /* size */
1108
        put_tag(pb, name);
1109
        mov_write_string_data_tag(pb, value, long_style);
1110
        size= updateSize(pb, pos);
1111
    }
1112
    return size;
1113
}
1114

    
1115
/* iTunes year */
1116
static int mov_write_day_tag(ByteIOContext *pb, int year, int long_style)
1117
{
1118
    if(year){
1119
        char year_str[5];
1120
        snprintf(year_str, sizeof(year_str), "%04d", year);
1121
        return mov_write_string_tag(pb, "\251day", year_str, long_style);
1122
    }else
1123
        return 0;
1124
}
1125

    
1126
/* iTunes track number */
1127
static int mov_write_trkn_tag(ByteIOContext *pb, MOVContext* mov,
1128
                              AVFormatContext *s)
1129
{
1130
    int size = 0;
1131
    if ( s->track ) {
1132
        offset_t pos = url_ftell(pb);
1133
        put_be32(pb, 0); /* size */
1134
        put_tag(pb, "trkn");
1135
        {
1136
            offset_t pos = url_ftell(pb);
1137
            put_be32(pb, 0); /* size */
1138
            put_tag(pb, "data");
1139
            put_be32(pb, 0);        // 8 bytes empty
1140
            put_be32(pb, 0);
1141
            put_be16(pb, 0);        // empty
1142
            put_be16(pb, s->track); // track number
1143
            put_be16(pb, 0);        // total track number
1144
            put_be16(pb, 0);        // empty
1145
            updateSize(pb, pos);
1146
        }
1147
        size = updateSize(pb, pos);
1148
    }
1149
    return size;
1150
}
1151

    
1152
/* iTunes meta data list */
1153
static int mov_write_ilst_tag(ByteIOContext *pb, MOVContext* mov,
1154
                              AVFormatContext *s)
1155
{
1156
    offset_t pos = url_ftell(pb);
1157
    put_be32(pb, 0); /* size */
1158
    put_tag(pb, "ilst");
1159
    mov_write_string_tag(pb, "\251nam", s->title         , 1);
1160
    mov_write_string_tag(pb, "\251ART", s->author        , 1);
1161
    mov_write_string_tag(pb, "\251wrt", s->author        , 1);
1162
    mov_write_string_tag(pb, "\251alb", s->album         , 1);
1163
    mov_write_day_tag(pb, s->year ,1);
1164
    if(mov->tracks[0].enc && !(mov->tracks[0].enc->flags & CODEC_FLAG_BITEXACT))
1165
        mov_write_string_tag(pb, "\251too", LIBAVFORMAT_IDENT, 1);
1166
    mov_write_string_tag(pb, "\251cmt", s->comment       , 1);
1167
    mov_write_string_tag(pb, "\251gen", s->genre         , 1);
1168
    mov_write_trkn_tag(pb, mov, s);
1169
    return updateSize(pb, pos);
1170
}
1171

    
1172
/* iTunes meta data tag */
1173
static int mov_write_meta_tag(ByteIOContext *pb, MOVContext* mov,
1174
                              AVFormatContext *s)
1175
{
1176
    int size = 0;
1177

    
1178
    // only save meta tag if required
1179
    if ( s->title[0] || s->author[0] || s->album[0] || s->year ||
1180
         s->comment[0] || s->genre[0] || s->track ) {
1181
        offset_t pos = url_ftell(pb);
1182
        put_be32(pb, 0); /* size */
1183
        put_tag(pb, "meta");
1184
        put_be32(pb, 0);
1185
        mov_write_itunes_hdlr_tag(pb, mov, s);
1186
        mov_write_ilst_tag(pb, mov, s);
1187
        size = updateSize(pb, pos);
1188
    }
1189
    return size;
1190
}
1191

    
1192
static int mov_write_udta_tag(ByteIOContext *pb, MOVContext* mov,
1193
                              AVFormatContext *s)
1194
{
1195
    offset_t pos = url_ftell(pb);
1196
    int i;
1197

    
1198
    put_be32(pb, 0); /* size */
1199
    put_tag(pb, "udta");
1200

    
1201
    /* iTunes meta data */
1202
    mov_write_meta_tag(pb, mov, s);
1203

    
1204
  if(mov->mode == MODE_MOV){ // the title field breaks gtkpod with mp4 and my suspicion is that stuff isnt valid in mp4
1205
    /* Requirements */
1206
    for (i=0; i<mov->nb_streams; i++) {
1207
        if(mov->tracks[i].entry <= 0) continue;
1208
        if (mov->tracks[i].enc->codec_id == CODEC_ID_AAC ||
1209
            mov->tracks[i].enc->codec_id == CODEC_ID_MPEG4) {
1210
            mov_write_string_tag(pb, "\251req", "QuickTime 6.0 or greater", 0);
1211
            break;
1212
        }
1213
    }
1214

    
1215
    mov_write_string_tag(pb, "\251nam", s->title         , 0);
1216
    mov_write_string_tag(pb, "\251aut", s->author        , 0);
1217
    mov_write_string_tag(pb, "\251alb", s->album         , 0);
1218
    mov_write_day_tag(pb, s->year, 0);
1219
    if(mov->tracks[0].enc && !(mov->tracks[0].enc->flags & CODEC_FLAG_BITEXACT))
1220
        mov_write_string_tag(pb, "\251enc", LIBAVFORMAT_IDENT, 0);
1221
    mov_write_string_tag(pb, "\251des", s->comment       , 0);
1222
    mov_write_string_tag(pb, "\251gen", s->genre         , 0);
1223
  }
1224

    
1225
    return updateSize(pb, pos);
1226
}
1227

    
1228
static int utf8len(uint8_t *b){
1229
    int len=0;
1230
    int val;
1231
    while(*b){
1232
        GET_UTF8(val, *b++, return -1;)
1233
        len++;
1234
    }
1235
    return len;
1236
}
1237

    
1238
static int ascii_to_wc (ByteIOContext *pb, uint8_t *b)
1239
{
1240
    int val;
1241
    while(*b){
1242
        GET_UTF8(val, *b++, return -1;)
1243
        put_be16(pb, val);
1244
    }
1245
    put_be16(pb, 0x00);
1246
    return 0;
1247
}
1248

    
1249
static uint16_t language_code (const char *str)
1250
{
1251
    return ((((str[0]-0x60) & 0x1F)<<10) + (((str[1]-0x60) & 0x1F)<<5) + ((str[2]-0x60) & 0x1F));
1252
}
1253

    
1254
static int mov_write_uuidusmt_tag (ByteIOContext *pb, AVFormatContext *s)
1255
{
1256
    size_t len, size;
1257
    offset_t pos, curpos;
1258

    
1259
    size = 0;
1260
    if (s->title[0]) {
1261
        pos = url_ftell(pb);
1262
        put_be32(pb, 0); /* size placeholder*/
1263
        put_tag(pb, "uuid");
1264
        put_tag(pb, "USMT");
1265
        put_be32(pb, 0x21d24fce ); /* 96 bit UUID */
1266
        put_be32(pb, 0xbb88695c );
1267
        put_be32(pb, 0xfac9c740 );
1268
        size += 24;
1269

    
1270
        put_be32(pb, 0); /* size placeholder*/
1271
        put_tag(pb, "MTDT");
1272
        put_be16(pb, 4);
1273
        size += 10;
1274

    
1275
        // ?
1276
        put_be16(pb, 0x0C);                 /* size */
1277
        put_be32(pb, 0x0B);                 /* type */
1278
        put_be16(pb, language_code("und")); /* language */
1279
        put_be16(pb, 0x0);                  /* ? */
1280
        put_be16(pb, 0x021C);               /* data */
1281
        size += 12;
1282

    
1283
        // Encoder
1284
        len = utf8len(LIBAVCODEC_IDENT)+1;
1285
        if(len<=0)
1286
            goto not_utf8;
1287
        put_be16(pb, len*2+10);             /* size */
1288
        put_be32(pb, 0x04);                 /* type */
1289
        put_be16(pb, language_code("eng")); /* language */
1290
        put_be16(pb, 0x01);                 /* ? */
1291
        ascii_to_wc(pb, LIBAVCODEC_IDENT);
1292
        size += len*2+10;
1293

    
1294
        // Title
1295
        len = utf8len(s->title)+1;
1296
        if(len<=0)
1297
            goto not_utf8;
1298
        put_be16(pb, len*2+10);             /* size */
1299
        put_be32(pb, 0x01);                 /* type */
1300
        put_be16(pb, language_code("eng")); /* language */
1301
        put_be16(pb, 0x01);                 /* ? */
1302
        ascii_to_wc (pb, s->title);
1303
        size += len*2+10;
1304

    
1305
        // Date
1306
//        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);
1307
        len = utf8len("2006/04/01 11:11:11")+1;
1308
        if(len<=0)
1309
            goto not_utf8;
1310
        put_be16(pb, len*2+10);    /* size */
1311
        put_be32(pb, 0x03);        /* type */
1312
        put_be16(pb, language_code("und")); /* language */
1313
        put_be16(pb, 0x01);        /* ? */
1314
        ascii_to_wc (pb, "2006/04/01 11:11:11");
1315
        size += len*2+10;
1316

    
1317
        // size
1318
        curpos = url_ftell(pb);
1319
        url_fseek(pb, pos, SEEK_SET);
1320
        put_be32(pb, size);
1321
        url_fseek(pb, pos+24, SEEK_SET);
1322
        put_be32(pb, size-24);
1323
        url_fseek(pb, curpos, SEEK_SET);
1324
    }
1325

    
1326
    return size;
1327
not_utf8:
1328
    av_log(s, AV_LOG_ERROR, "not utf8\n");
1329
    return -1;
1330
}
1331

    
1332
static int mov_write_moov_tag(ByteIOContext *pb, MOVContext *mov,
1333
                              AVFormatContext *s)
1334
{
1335
    int i;
1336
    offset_t pos = url_ftell(pb);
1337
    put_be32(pb, 0); /* size placeholder*/
1338
    put_tag(pb, "moov");
1339
    mov->timescale = globalTimescale;
1340

    
1341
    for (i=0; i<mov->nb_streams; i++) {
1342
        if(mov->tracks[i].entry <= 0) continue;
1343

    
1344
        mov->tracks[i].time = mov->time;
1345
        mov->tracks[i].trackID = i+1;
1346
    }
1347

    
1348
    mov_write_mvhd_tag(pb, mov);
1349
    //mov_write_iods_tag(pb, mov);
1350
    for (i=0; i<mov->nb_streams; i++) {
1351
        if(mov->tracks[i].entry > 0) {
1352
            mov_write_trak_tag(pb, &(mov->tracks[i]));
1353
        }
1354
    }
1355

    
1356
    if (mov->mode == MODE_PSP)
1357
        mov_write_uuidusmt_tag(pb, s);
1358
    else
1359
        mov_write_udta_tag(pb, mov, s);
1360

    
1361
    return updateSize(pb, pos);
1362
}
1363

    
1364
static int mov_write_mdat_tag(ByteIOContext *pb, MOVContext* mov)
1365
{
1366
    put_be32(pb, 8);    // placeholder for extended size field (64 bit)
1367
    put_tag(pb, mov->mode == MODE_MOV ? "wide" : "free");
1368

    
1369
    mov->mdat_pos = url_ftell(pb);
1370
    put_be32(pb, 0); /* size placeholder*/
1371
    put_tag(pb, "mdat");
1372
    return 0;
1373
}
1374

    
1375
/* TODO: This needs to be more general */
1376
static void mov_write_ftyp_tag (ByteIOContext *pb, AVFormatContext *s)
1377
{
1378
    MOVContext *mov = s->priv_data;
1379

    
1380
    put_be32(pb, 0x14 ); /* size */
1381
    put_tag(pb, "ftyp");
1382

    
1383
    if ( mov->mode == MODE_3GP )
1384
        put_tag(pb, "3gp4");
1385
    else if ( mov->mode == MODE_3G2 )
1386
        put_tag(pb, "3g2a");
1387
    else if ( mov->mode == MODE_PSP )
1388
        put_tag(pb, "MSNV");
1389
    else if ( mov->mode == MODE_MP4 )
1390
        put_tag(pb, "isom");
1391
    else
1392
        put_tag(pb, "qt  ");
1393

    
1394
    put_be32(pb, 0x200 );
1395

    
1396
    if ( mov->mode == MODE_3GP )
1397
        put_tag(pb, "3gp4");
1398
    else if ( mov->mode == MODE_3G2 )
1399
        put_tag(pb, "3g2a");
1400
    else if ( mov->mode == MODE_PSP )
1401
        put_tag(pb, "MSNV");
1402
    else if ( mov->mode == MODE_MP4 )
1403
        put_tag(pb, "mp41");
1404
    else
1405
        put_tag(pb, "qt  ");
1406
}
1407

    
1408
static void mov_write_uuidprof_tag(ByteIOContext *pb, AVFormatContext *s)
1409
{
1410
    AVCodecContext *VideoCodec = s->streams[0]->codec;
1411
    AVCodecContext *AudioCodec = s->streams[1]->codec;
1412
    int AudioRate = AudioCodec->sample_rate;
1413
    int FrameRate = ((VideoCodec->time_base.den) * (0x10000))/ (VideoCodec->time_base.num);
1414
    int audio_kbitrate= AudioCodec->bit_rate / 1000;
1415
    int video_kbitrate= FFMIN(VideoCodec->bit_rate / 1000, 800 - audio_kbitrate);
1416

    
1417
    put_be32(pb, 0x94 ); /* size */
1418
    put_tag(pb, "uuid");
1419
    put_tag(pb, "PROF");
1420

    
1421
    put_be32(pb, 0x21d24fce ); /* 96 bit UUID */
1422
    put_be32(pb, 0xbb88695c );
1423
    put_be32(pb, 0xfac9c740 );
1424

    
1425
    put_be32(pb, 0x0 );  /* ? */
1426
    put_be32(pb, 0x3 );  /* 3 sections ? */
1427

    
1428
    put_be32(pb, 0x14 ); /* size */
1429
    put_tag(pb, "FPRF");
1430
    put_be32(pb, 0x0 );  /* ? */
1431
    put_be32(pb, 0x0 );  /* ? */
1432
    put_be32(pb, 0x0 );  /* ? */
1433

    
1434
    put_be32(pb, 0x2c );  /* size */
1435
    put_tag(pb, "APRF");   /* audio */
1436
    put_be32(pb, 0x0 );
1437
    put_be32(pb, 0x2 );   /* TrackID */
1438
    put_tag(pb, "mp4a");
1439
    put_be32(pb, 0x20f );
1440
    put_be32(pb, 0x0 );
1441
    put_be32(pb, audio_kbitrate);
1442
    put_be32(pb, audio_kbitrate);
1443
    put_be32(pb, AudioRate );
1444
    put_be32(pb, AudioCodec->channels );
1445

    
1446
    put_be32(pb, 0x34 );  /* size */
1447
    put_tag(pb, "VPRF");   /* video */
1448
    put_be32(pb, 0x0 );
1449
    put_be32(pb, 0x1 );    /* TrackID */
1450
    if (VideoCodec->codec_id == CODEC_ID_H264) {
1451
        put_tag(pb, "avc1");
1452
        put_be16(pb, 0x014D );
1453
        put_be16(pb, 0x0015 );
1454
    } else {
1455
        put_tag(pb, "mp4v");
1456
        put_be16(pb, 0x0000 );
1457
        put_be16(pb, 0x0103 );
1458
    }
1459
    put_be32(pb, 0x0 );
1460
    put_be32(pb, video_kbitrate);
1461
    put_be32(pb, video_kbitrate);
1462
    put_be32(pb, FrameRate);
1463
    put_be32(pb, FrameRate);
1464
    put_be16(pb, VideoCodec->width);
1465
    put_be16(pb, VideoCodec->height);
1466
    put_be32(pb, 0x010001); /* ? */
1467
}
1468

    
1469
static int mov_write_header(AVFormatContext *s)
1470
{
1471
    ByteIOContext *pb = &s->pb;
1472
    MOVContext *mov = s->priv_data;
1473
    int i;
1474

    
1475
    /* Default mode == MP4 */
1476
    mov->mode = MODE_MP4;
1477

    
1478
    if (s->oformat != NULL) {
1479
        if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
1480
        else if (!strcmp("3g2", s->oformat->name)) mov->mode = MODE_3G2;
1481
        else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
1482
        else if (!strcmp("psp", s->oformat->name)) mov->mode = MODE_PSP;
1483

    
1484
        mov_write_ftyp_tag(pb,s);
1485
        if ( mov->mode == MODE_PSP ) {
1486
            if ( s->nb_streams != 2 ) {
1487
                av_log(s, AV_LOG_ERROR, "PSP mode need one video and one audio stream\n");
1488
                return -1;
1489
            }
1490
            mov_write_uuidprof_tag(pb,s);
1491
        }
1492
    }
1493

    
1494
    for(i=0; i<s->nb_streams; i++){
1495
        AVStream *st= s->streams[i];
1496
        MOVTrack *track= &mov->tracks[i];
1497

    
1498
        track->enc = st->codec;
1499
        track->language = ff_mov_iso639_to_lang(st->language, mov->mode != MODE_MOV);
1500
        track->mode = mov->mode;
1501
        if(st->codec->codec_type == CODEC_TYPE_VIDEO){
1502
            track->tag = mov_find_video_codec_tag(s, track);
1503
            track->timescale = st->codec->time_base.den;
1504
            track->sampleDuration = st->codec->time_base.num;
1505
            av_set_pts_info(st, 64, 1, st->codec->time_base.den);
1506
        }else if(st->codec->codec_type == CODEC_TYPE_AUDIO){
1507
            track->tag = mov_find_audio_codec_tag(s, track);
1508
            track->timescale = st->codec->sample_rate;
1509
            track->sampleDuration = st->codec->frame_size;
1510
            av_set_pts_info(st, 64, 1, st->codec->sample_rate);
1511
            switch(track->enc->codec_id){
1512
            case CODEC_ID_MP3:
1513
            case CODEC_ID_AAC:
1514
            case CODEC_ID_AMR_NB:
1515
            case CODEC_ID_AMR_WB:
1516
                track->audio_vbr = 1;
1517
                break;
1518
            default:
1519
                track->sampleSize = (av_get_bits_per_sample(st->codec->codec_id) >> 3) * st->codec->channels;
1520
            }
1521
        }
1522
        if (!track->sampleDuration) {
1523
            av_log(s, AV_LOG_ERROR, "track %d: sample duration is not set\n", i);
1524
            return -1;
1525
        }
1526
    }
1527

    
1528
    mov_write_mdat_tag(pb, mov);
1529
    mov->time = s->timestamp + 0x7C25B080; //1970 based -> 1904 based
1530
    mov->nb_streams = s->nb_streams;
1531

    
1532
    put_flush_packet(pb);
1533

    
1534
    return 0;
1535
}
1536

    
1537
static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
1538
{
1539
    MOVContext *mov = s->priv_data;
1540
    ByteIOContext *pb = &s->pb;
1541
    MOVTrack *trk = &mov->tracks[pkt->stream_index];
1542
    AVCodecContext *enc = trk->enc;
1543
    unsigned int samplesInChunk = 0;
1544
    int size= pkt->size;
1545

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

    
1549
    if (enc->codec_id == CODEC_ID_AMR_NB) {
1550
        /* We must find out how many AMR blocks there are in one packet */
1551
        static uint16_t packed_size[16] =
1552
            {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 0};
1553
        int len = 0;
1554

    
1555
        while (len < size && samplesInChunk < 100) {
1556
            len += packed_size[(pkt->data[len] >> 3) & 0x0F];
1557
            samplesInChunk++;
1558
        }
1559
        if(samplesInChunk > 1){
1560
            av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, inplement a AVParser for it\n");
1561
            return -1;
1562
        }
1563
    } else if (trk->sampleSize)
1564
        samplesInChunk = size/trk->sampleSize;
1565
    else
1566
        samplesInChunk = 1;
1567

    
1568
    /* copy extradata if it exists */
1569
    if (trk->vosLen == 0 && enc->extradata_size > 0) {
1570
        trk->vosLen = enc->extradata_size;
1571
        trk->vosData = av_malloc(trk->vosLen);
1572
        memcpy(trk->vosData, enc->extradata, trk->vosLen);
1573
    }
1574

    
1575
    if (enc->codec_id == CODEC_ID_H264 && trk->vosLen > 0 && *(uint8_t *)trk->vosData != 1) {
1576
        /* from x264 or from bytestream h264 */
1577
        /* nal reformating needed */
1578
        avc_parse_nal_units(&pkt->data, &pkt->size);
1579
        assert(pkt->size);
1580
        size = pkt->size;
1581
    }
1582

    
1583
    if (!(trk->entry % MOV_INDEX_CLUSTER_SIZE)) {
1584
        trk->cluster = av_realloc(trk->cluster, (trk->entry + MOV_INDEX_CLUSTER_SIZE) * sizeof(*trk->cluster));
1585
        if (!trk->cluster)
1586
            return -1;
1587
    }
1588

    
1589
    trk->cluster[trk->entry].pos = url_ftell(pb);
1590
    trk->cluster[trk->entry].samplesInChunk = samplesInChunk;
1591
    trk->cluster[trk->entry].size = size;
1592
    trk->cluster[trk->entry].entries = samplesInChunk;
1593
    trk->cluster[trk->entry].dts = pkt->dts;
1594
    trk->trackDuration = pkt->dts - trk->cluster[0].dts + pkt->duration;
1595

    
1596
    if(enc->codec_type == CODEC_TYPE_VIDEO) {
1597
        if (pkt->dts != pkt->pts)
1598
            trk->hasBframes = 1;
1599
        trk->cluster[trk->entry].cts = pkt->pts - pkt->dts;
1600
        trk->cluster[trk->entry].key_frame = !!(pkt->flags & PKT_FLAG_KEY);
1601
        if(trk->cluster[trk->entry].key_frame)
1602
            trk->hasKeyframes++;
1603
    }
1604
    trk->entry++;
1605
    trk->sampleCount += samplesInChunk;
1606
    mov->mdat_size += size;
1607

    
1608
    put_buffer(pb, pkt->data, size);
1609

    
1610
    put_flush_packet(pb);
1611
    return 0;
1612
}
1613

    
1614
static int mov_write_trailer(AVFormatContext *s)
1615
{
1616
    MOVContext *mov = s->priv_data;
1617
    ByteIOContext *pb = &s->pb;
1618
    int res = 0;
1619
    int i;
1620

    
1621
    offset_t moov_pos = url_ftell(pb);
1622

    
1623
    /* Write size of mdat tag */
1624
    if (mov->mdat_size+8 <= UINT32_MAX) {
1625
        url_fseek(pb, mov->mdat_pos, SEEK_SET);
1626
        put_be32(pb, mov->mdat_size+8);
1627
    } else {
1628
        /* overwrite 'wide' placeholder atom */
1629
        url_fseek(pb, mov->mdat_pos - 8, SEEK_SET);
1630
        put_be32(pb, 1); /* special value: real atom size will be 64 bit value after tag field */
1631
        put_tag(pb, "mdat");
1632
        put_be64(pb, mov->mdat_size+16);
1633
    }
1634
    url_fseek(pb, moov_pos, SEEK_SET);
1635

    
1636
    mov_write_moov_tag(pb, mov, s);
1637

    
1638
    for (i=0; i<mov->nb_streams; i++) {
1639
        av_freep(&mov->tracks[i].cluster);
1640

    
1641
        if( mov->tracks[i].vosLen ) av_free( mov->tracks[i].vosData );
1642

    
1643
    }
1644

    
1645
    put_flush_packet(pb);
1646

    
1647
    return res;
1648
}
1649

    
1650
#ifdef CONFIG_MOV_MUXER
1651
AVOutputFormat mov_muxer = {
1652
    "mov",
1653
    "mov format",
1654
    NULL,
1655
    "mov",
1656
    sizeof(MOVContext),
1657
    CODEC_ID_AAC,
1658
    CODEC_ID_MPEG4,
1659
    mov_write_header,
1660
    mov_write_packet,
1661
    mov_write_trailer,
1662
    .flags = AVFMT_GLOBALHEADER,
1663
};
1664
#endif
1665
#ifdef CONFIG_TGP_MUXER
1666
AVOutputFormat tgp_muxer = {
1667
    "3gp",
1668
    "3gp format",
1669
    NULL,
1670
    "3gp",
1671
    sizeof(MOVContext),
1672
    CODEC_ID_AMR_NB,
1673
    CODEC_ID_H263,
1674
    mov_write_header,
1675
    mov_write_packet,
1676
    mov_write_trailer,
1677
    .flags = AVFMT_GLOBALHEADER,
1678
};
1679
#endif
1680
#ifdef CONFIG_MP4_MUXER
1681
AVOutputFormat mp4_muxer = {
1682
    "mp4",
1683
    "mp4 format",
1684
    "application/mp4",
1685
    "mp4,m4a",
1686
    sizeof(MOVContext),
1687
    CODEC_ID_AAC,
1688
    CODEC_ID_MPEG4,
1689
    mov_write_header,
1690
    mov_write_packet,
1691
    mov_write_trailer,
1692
    .flags = AVFMT_GLOBALHEADER,
1693
};
1694
#endif
1695
#ifdef CONFIG_PSP_MUXER
1696
AVOutputFormat psp_muxer = {
1697
    "psp",
1698
    "psp mp4 format",
1699
    NULL,
1700
    "mp4,psp",
1701
    sizeof(MOVContext),
1702
    CODEC_ID_AAC,
1703
    CODEC_ID_MPEG4,
1704
    mov_write_header,
1705
    mov_write_packet,
1706
    mov_write_trailer,
1707
    .flags = AVFMT_GLOBALHEADER,
1708
};
1709
#endif
1710
#ifdef CONFIG_TG2_MUXER
1711
AVOutputFormat tg2_muxer = {
1712
    "3g2",
1713
    "3gp2 format",
1714
    NULL,
1715
    "3g2",
1716
    sizeof(MOVContext),
1717
    CODEC_ID_AMR_NB,
1718
    CODEC_ID_H263,
1719
    mov_write_header,
1720
    mov_write_packet,
1721
    mov_write_trailer,
1722
    .flags = AVFMT_GLOBALHEADER,
1723
};
1724
#endif