Statistics
| Branch: | Revision:

ffmpeg / libavformat / movenc.c @ 71e445fc

History | View | Annotate | Download (53.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 supprt 64bit varaint 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_mov_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 const AVCodecTag codec_movaudio_tags[] = {
326
    { CODEC_ID_PCM_MULAW, MKTAG('u', 'l', 'a', 'w') },
327
    { CODEC_ID_PCM_ALAW, MKTAG('a', 'l', 'a', 'w') },
328
    { CODEC_ID_ADPCM_IMA_QT, MKTAG('i', 'm', 'a', '4') },
329
    { CODEC_ID_MACE3, MKTAG('M', 'A', 'C', '3') },
330
    { CODEC_ID_MACE6, MKTAG('M', 'A', 'C', '6') },
331
    { CODEC_ID_AAC, MKTAG('m', 'p', '4', 'a') },
332
    { CODEC_ID_AMR_NB, MKTAG('s', 'a', 'm', 'r') },
333
    { CODEC_ID_AMR_WB, MKTAG('s', 'a', 'w', 'b') },
334
    { CODEC_ID_PCM_S16BE, MKTAG('t', 'w', 'o', 's') },
335
    { CODEC_ID_PCM_S16LE, MKTAG('s', 'o', 'w', 't') },
336
    { CODEC_ID_PCM_S24BE, MKTAG('i', 'n', '2', '4') },
337
    { CODEC_ID_PCM_S24LE, MKTAG('i', 'n', '2', '4') },
338
    { CODEC_ID_PCM_S32BE, MKTAG('i', 'n', '3', '2') },
339
    { CODEC_ID_PCM_S32LE, MKTAG('i', 'n', '3', '2') },
340
    { CODEC_ID_MP3, MKTAG('.', 'm', 'p', '3') },
341
    { CODEC_ID_NONE, 0 },
342
};
343

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
452
    return end + 3;
453
}
454

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
756
    return 28;
757
}
758

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1393
    put_be32(pb, 0x200 );
1394

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1525
    mov_write_mdat_tag(pb, mov);
1526
    mov->time = s->timestamp + 0x7C25B080; //1970 based -> 1904 based
1527
    mov->nb_streams = s->nb_streams;
1528

    
1529
    put_flush_packet(pb);
1530

    
1531
    return 0;
1532
}
1533

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

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

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

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

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

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

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

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

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

    
1605
    put_buffer(pb, pkt->data, size);
1606

    
1607
    put_flush_packet(pb);
1608
    return 0;
1609
}
1610

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

    
1618
    offset_t moov_pos = url_ftell(pb);
1619

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

    
1633
    mov_write_moov_tag(pb, mov, s);
1634

    
1635
    for (i=0; i<mov->nb_streams; i++) {
1636
        av_freep(&mov->tracks[i].cluster);
1637

    
1638
        if( mov->tracks[i].vosLen ) av_free( mov->tracks[i].vosData );
1639

    
1640
    }
1641

    
1642
    put_flush_packet(pb);
1643

    
1644
    return res;
1645
}
1646

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