Statistics
| Branch: | Revision:

ffmpeg / libavformat / movenc.c @ c8134d71

History | View | Annotate | Download (53.3 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
#include "avc.h"
27

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

    
31
#define MOV_INDEX_CLUSTER_SIZE 16384
32
#define globalTimescale 1000
33

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

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

    
51
typedef struct MOVIndex {
52
    int         mode;
53
    int         entry;
54
    long        timescale;
55
    long        time;
56
    int64_t     trackDuration;
57
    long        sampleCount;
58
    long        sampleSize;
59
    int         hasKeyframes;
60
    int         hasBframes;
61
    int         language;
62
    int         trackID;
63
    int         tag; ///< stsd fourcc
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 support 64 bit variant 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_mp4_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 int mov_write_glbl_tag(ByteIOContext *pb, MOVTrack* track)
327
{
328
    put_be32(pb, track->vosLen+8);
329
    put_tag(pb, "glbl");
330
    put_buffer(pb, track->vosData, track->vosLen);
331
    return 8+track->vosLen;
332
}
333

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

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

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

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

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

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

    
378
    if(track->mode == MODE_MOV &&
379
       (track->enc->codec_id == CODEC_ID_AAC ||
380
        track->enc->codec_id == CODEC_ID_AMR_NB ||
381
        track->enc->codec_id == CODEC_ID_PCM_S24LE ||
382
        track->enc->codec_id == CODEC_ID_PCM_S32LE))
383
        mov_write_wave_tag(pb, track);
384
    else if(track->tag == MKTAG('m','p','4','a'))
385
        mov_write_esds_tag(pb, track);
386
    else if(track->enc->codec_id == CODEC_ID_AMR_NB)
387
        mov_write_amr_tag(pb, track);
388
    else if(track->vosLen > 0)
389
        mov_write_glbl_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 int mov_write_avcc_tag(ByteIOContext *pb, MOVTrack *track)
420
{
421
    offset_t pos = url_ftell(pb);
422

    
423
    put_be32(pb, 0);
424
    put_tag(pb, "avcC");
425
    ff_isom_write_avcc(pb, track->vosData, track->vosLen);
426
    return updateSize(pb, pos);
427
}
428

    
429
/* also used by all avid codecs (dv, imx, meridien) and their variants */
430
static int mov_write_avid_tag(ByteIOContext *pb, MOVTrack *track)
431
{
432
    int i;
433
    put_be32(pb, 24); /* size */
434
    put_tag(pb, "ACLR");
435
    put_tag(pb, "ACLR");
436
    put_tag(pb, "0001");
437
    put_be32(pb, 1); /* yuv 1 / rgb 2 ? */
438
    put_be32(pb, 0); /* unknown */
439

    
440
    put_be32(pb, 24); /* size */
441
    put_tag(pb, "APRG");
442
    put_tag(pb, "APRG");
443
    put_tag(pb, "0001");
444
    put_be32(pb, 1); /* unknown */
445
    put_be32(pb, 0); /* unknown */
446

    
447
    put_be32(pb, 120); /* size */
448
    put_tag(pb, "ARES");
449
    put_tag(pb, "ARES");
450
    put_tag(pb, "0001");
451
    put_be32(pb, AV_RB32(track->vosData + 0x28)); /* dnxhd cid, some id ? */
452
    put_be32(pb, track->enc->width);
453
    /* values below are based on samples created with quicktime and avid codecs */
454
    if (track->vosData[5] & 2) { // interlaced
455
        put_be32(pb, track->enc->height/2);
456
        put_be32(pb, 2); /* unknown */
457
        put_be32(pb, 0); /* unknown */
458
        put_be32(pb, 4); /* unknown */
459
    } else {
460
        put_be32(pb, track->enc->height);
461
        put_be32(pb, 1); /* unknown */
462
        put_be32(pb, 0); /* unknown */
463
        if (track->enc->height == 1080)
464
            put_be32(pb, 5); /* unknown */
465
        else
466
            put_be32(pb, 6); /* unknown */
467
    }
468
    /* padding */
469
    for (i = 0; i < 10; i++)
470
        put_be64(pb, 0);
471

    
472
    /* extra padding for stsd needed */
473
    put_be32(pb, 0);
474
    return 0;
475
}
476

    
477
static const AVCodecTag codec_3gp_tags[] = {
478
    { CODEC_ID_H263,   MKTAG('s','2','6','3') },
479
    { CODEC_ID_H264,   MKTAG('a','v','c','1') },
480
    { CODEC_ID_MPEG4,  MKTAG('m','p','4','v') },
481
    { CODEC_ID_AAC,    MKTAG('m','p','4','a') },
482
    { CODEC_ID_AMR_NB, MKTAG('s','a','m','r') },
483
    { CODEC_ID_AMR_WB, MKTAG('s','a','w','b') },
484
};
485

    
486
static int mov_find_codec_tag(AVFormatContext *s, MOVTrack *track)
487
{
488
    int tag = track->enc->codec_tag;
489
    if (track->mode == MODE_MP4 || track->mode == MODE_PSP) {
490
        if (!codec_get_tag(ff_mp4_obj_type, track->enc->codec_id))
491
            return 0;
492
        if (track->enc->codec_id == CODEC_ID_H264)           tag = MKTAG('a','v','c','1');
493
        else if (track->enc->codec_type == CODEC_TYPE_VIDEO) tag = MKTAG('m','p','4','v');
494
        else if (track->enc->codec_type == CODEC_TYPE_AUDIO) tag = MKTAG('m','p','4','a');
495
    } else if (track->mode == MODE_3GP || track->mode == MODE_3G2) {
496
        tag = codec_get_tag(codec_3gp_tags, track->enc->codec_id);
497
    } else if (!tag || (track->enc->strict_std_compliance >= FF_COMPLIANCE_NORMAL &&
498
                        tag == MKTAG('d','v','c','p'))) {
499
        if (track->enc->codec_id == CODEC_ID_DVVIDEO) {
500
            if (track->enc->height == 480) /* NTSC */
501
                if  (track->enc->pix_fmt == PIX_FMT_YUV422P) tag = MKTAG('d','v','5','n');
502
                else                                         tag = MKTAG('d','v','c',' ');
503
            else if (track->enc->pix_fmt == PIX_FMT_YUV422P) tag = MKTAG('d','v','5','p');
504
            else if (track->enc->pix_fmt == PIX_FMT_YUV420P) tag = MKTAG('d','v','c','p');
505
            else                                             tag = MKTAG('d','v','p','p');
506
        } else {
507
            if (track->enc->codec_type == CODEC_TYPE_VIDEO) {
508
                tag = codec_get_tag(codec_movvideo_tags, track->enc->codec_id);
509
                if (!tag) { // if no mac fcc found, try with Microsoft tags
510
                    tag = codec_get_tag(codec_bmp_tags, track->enc->codec_id);
511
                    if (tag)
512
                        av_log(s, AV_LOG_INFO, "Warning, using MS style video codec tag, "
513
                               "the file may be unplayable!\n");
514
                }
515
            } else if (track->enc->codec_type == CODEC_TYPE_AUDIO) {
516
                tag = codec_get_tag(codec_movaudio_tags, track->enc->codec_id);
517
                if (!tag) { // if no mac fcc found, try with Microsoft tags
518
                    int ms_tag = codec_get_tag(codec_wav_tags, track->enc->codec_id);
519
                    if (ms_tag) {
520
                        tag = MKTAG('m', 's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
521
                        av_log(s, AV_LOG_INFO, "Warning, using MS style audio codec tag, "
522
                               "the file may be unplayable!\n");
523
                    }
524
                }
525
            }
526
        }
527
    }
528
    return tag;
529
}
530

    
531
static int mov_write_video_tag(ByteIOContext *pb, MOVTrack* track)
532
{
533
    offset_t pos = url_ftell(pb);
534
    char compressor_name[32];
535

    
536
    put_be32(pb, 0); /* size */
537
    put_le32(pb, track->tag); // store it byteswapped
538
    put_be32(pb, 0); /* Reserved */
539
    put_be16(pb, 0); /* Reserved */
540
    put_be16(pb, 1); /* Data-reference index */
541

    
542
    put_be16(pb, 0); /* Codec stream version */
543
    put_be16(pb, 0); /* Codec stream revision (=0) */
544
    if (track->mode == MODE_MOV) {
545
        put_tag(pb, "FFMP"); /* Vendor */
546
        if(track->enc->codec_id == CODEC_ID_RAWVIDEO) {
547
            put_be32(pb, 0); /* Temporal Quality */
548
            put_be32(pb, 0x400); /* Spatial Quality = lossless*/
549
        } else {
550
            put_be32(pb, 0x200); /* Temporal Quality = normal */
551
            put_be32(pb, 0x200); /* Spatial Quality = normal */
552
        }
553
    } else {
554
        put_be32(pb, 0); /* Reserved */
555
        put_be32(pb, 0); /* Reserved */
556
        put_be32(pb, 0); /* Reserved */
557
    }
558
    put_be16(pb, track->enc->width); /* Video width */
559
    put_be16(pb, track->enc->height); /* Video height */
560
    put_be32(pb, 0x00480000); /* Horizontal resolution 72dpi */
561
    put_be32(pb, 0x00480000); /* Vertical resolution 72dpi */
562
    put_be32(pb, 0); /* Data size (= 0) */
563
    put_be16(pb, 1); /* Frame count (= 1) */
564

    
565
    memset(compressor_name,0,32);
566
    /* FIXME not sure, ISO 14496-1 draft where it shall be set to 0 */
567
    if (track->mode == MODE_MOV && track->enc->codec && track->enc->codec->name)
568
        strncpy(compressor_name,track->enc->codec->name,31);
569
    put_byte(pb, strlen(compressor_name));
570
    put_buffer(pb, compressor_name, 31);
571

    
572
    put_be16(pb, 0x18); /* Reserved */
573
    put_be16(pb, 0xffff); /* Reserved */
574
    if(track->tag == MKTAG('m','p','4','v'))
575
        mov_write_esds_tag(pb, track);
576
    else if(track->enc->codec_id == CODEC_ID_H263)
577
        mov_write_d263_tag(pb);
578
    else if(track->enc->codec_id == CODEC_ID_SVQ3)
579
        mov_write_svq3_tag(pb);
580
    else if(track->enc->codec_id == CODEC_ID_H264)
581
        mov_write_avcc_tag(pb, track);
582
    else if(track->enc->codec_id == CODEC_ID_DNXHD)
583
        mov_write_avid_tag(pb, track);
584
    else if(track->vosLen > 0)
585
        mov_write_glbl_tag(pb, track);
586

    
587
    return updateSize (pb, pos);
588
}
589

    
590
static int mov_write_stsd_tag(ByteIOContext *pb, MOVTrack* track)
591
{
592
    offset_t pos = url_ftell(pb);
593
    put_be32(pb, 0); /* size */
594
    put_tag(pb, "stsd");
595
    put_be32(pb, 0); /* version & flags */
596
    put_be32(pb, 1); /* entry count */
597
    if (track->enc->codec_type == CODEC_TYPE_VIDEO)
598
        mov_write_video_tag(pb, track);
599
    else if (track->enc->codec_type == CODEC_TYPE_AUDIO)
600
        mov_write_audio_tag(pb, track);
601
    return updateSize(pb, pos);
602
}
603

    
604
static int mov_write_ctts_tag(ByteIOContext *pb, MOVTrack* track)
605
{
606
    MOV_stts_t *ctts_entries;
607
    uint32_t entries = 0;
608
    uint32_t atom_size;
609
    int i;
610

    
611
    ctts_entries = av_malloc((track->entry + 1) * sizeof(*ctts_entries)); /* worst case */
612
    ctts_entries[0].count = 1;
613
    ctts_entries[0].duration = track->cluster[0].cts;
614
    for (i=1; i<track->entry; i++) {
615
        if (track->cluster[i].cts == ctts_entries[entries].duration) {
616
            ctts_entries[entries].count++; /* compress */
617
        } else {
618
            entries++;
619
            ctts_entries[entries].duration = track->cluster[i].cts;
620
            ctts_entries[entries].count = 1;
621
        }
622
    }
623
    entries++; /* last one */
624
    atom_size = 16 + (entries * 8);
625
    put_be32(pb, atom_size); /* size */
626
    put_tag(pb, "ctts");
627
    put_be32(pb, 0); /* version & flags */
628
    put_be32(pb, entries); /* entry count */
629
    for (i=0; i<entries; i++) {
630
        put_be32(pb, ctts_entries[i].count);
631
        put_be32(pb, ctts_entries[i].duration);
632
    }
633
    av_free(ctts_entries);
634
    return atom_size;
635
}
636

    
637
/* Time to sample atom */
638
static int mov_write_stts_tag(ByteIOContext *pb, MOVTrack* track)
639
{
640
    MOV_stts_t *stts_entries;
641
    uint32_t entries = -1;
642
    uint32_t atom_size;
643
    int i;
644

    
645
    if (track->enc->codec_type == CODEC_TYPE_AUDIO && !track->audio_vbr) {
646
        stts_entries = av_malloc(sizeof(*stts_entries)); /* one entry */
647
        stts_entries[0].count = track->sampleCount;
648
        stts_entries[0].duration = 1;
649
        entries = 1;
650
    } else {
651
        stts_entries = av_malloc(track->entry * sizeof(*stts_entries)); /* worst case */
652
        for (i=0; i<track->entry; i++) {
653
            int64_t duration = i + 1 == track->entry ?
654
                track->trackDuration - track->cluster[i].dts + track->cluster[0].dts : /* readjusting */
655
                track->cluster[i+1].dts - track->cluster[i].dts;
656
            if (i && duration == stts_entries[entries].duration) {
657
                stts_entries[entries].count++; /* compress */
658
            } else {
659
                entries++;
660
                stts_entries[entries].duration = duration;
661
                stts_entries[entries].count = 1;
662
            }
663
        }
664
        entries++; /* last one */
665
    }
666
    atom_size = 16 + (entries * 8);
667
    put_be32(pb, atom_size); /* size */
668
    put_tag(pb, "stts");
669
    put_be32(pb, 0); /* version & flags */
670
    put_be32(pb, entries); /* entry count */
671
    for (i=0; i<entries; i++) {
672
        put_be32(pb, stts_entries[i].count);
673
        put_be32(pb, stts_entries[i].duration);
674
    }
675
    av_free(stts_entries);
676
    return atom_size;
677
}
678

    
679
static int mov_write_dref_tag(ByteIOContext *pb)
680
{
681
    put_be32(pb, 28); /* size */
682
    put_tag(pb, "dref");
683
    put_be32(pb, 0); /* version & flags */
684
    put_be32(pb, 1); /* entry count */
685

    
686
    put_be32(pb, 0xc); /* size */
687
    put_tag(pb, "url ");
688
    put_be32(pb, 1); /* version & flags */
689

    
690
    return 28;
691
}
692

    
693
static int mov_write_stbl_tag(ByteIOContext *pb, MOVTrack* track)
694
{
695
    offset_t pos = url_ftell(pb);
696
    put_be32(pb, 0); /* size */
697
    put_tag(pb, "stbl");
698
    mov_write_stsd_tag(pb, track);
699
    mov_write_stts_tag(pb, track);
700
    if (track->enc->codec_type == CODEC_TYPE_VIDEO &&
701
        track->hasKeyframes < track->entry)
702
        mov_write_stss_tag(pb, track);
703
    if (track->enc->codec_type == CODEC_TYPE_VIDEO &&
704
        track->hasBframes)
705
        mov_write_ctts_tag(pb, track);
706
    mov_write_stsc_tag(pb, track);
707
    mov_write_stsz_tag(pb, track);
708
    mov_write_stco_tag(pb, track);
709
    return updateSize(pb, pos);
710
}
711

    
712
static int mov_write_dinf_tag(ByteIOContext *pb)
713
{
714
    offset_t pos = url_ftell(pb);
715
    put_be32(pb, 0); /* size */
716
    put_tag(pb, "dinf");
717
    mov_write_dref_tag(pb);
718
    return updateSize(pb, pos);
719
}
720

    
721
static int mov_write_smhd_tag(ByteIOContext *pb)
722
{
723
    put_be32(pb, 16); /* size */
724
    put_tag(pb, "smhd");
725
    put_be32(pb, 0); /* version & flags */
726
    put_be16(pb, 0); /* reserved (balance, normally = 0) */
727
    put_be16(pb, 0); /* reserved */
728
    return 16;
729
}
730

    
731
static int mov_write_vmhd_tag(ByteIOContext *pb)
732
{
733
    put_be32(pb, 0x14); /* size (always 0x14) */
734
    put_tag(pb, "vmhd");
735
    put_be32(pb, 0x01); /* version & flags */
736
    put_be64(pb, 0); /* reserved (graphics mode = copy) */
737
    return 0x14;
738
}
739

    
740
static int mov_write_hdlr_tag(ByteIOContext *pb, MOVTrack* track)
741
{
742
    const char *descr, *hdlr, *hdlr_type;
743
    offset_t pos = url_ftell(pb);
744

    
745
    if (!track) { /* no media --> data handler */
746
        hdlr = "dhlr";
747
        hdlr_type = "url ";
748
        descr = "DataHandler";
749
    } else {
750
        hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
751
        if (track->enc->codec_type == CODEC_TYPE_VIDEO) {
752
            hdlr_type = "vide";
753
            descr = "VideoHandler";
754
        } else {
755
            hdlr_type = "soun";
756
            descr = "SoundHandler";
757
        }
758
    }
759

    
760
    put_be32(pb, 0); /* size */
761
    put_tag(pb, "hdlr");
762
    put_be32(pb, 0); /* Version & flags */
763
    put_buffer(pb, hdlr, 4); /* handler */
764
    put_tag(pb, hdlr_type); /* handler type */
765
    put_be32(pb ,0); /* reserved */
766
    put_be32(pb ,0); /* reserved */
767
    put_be32(pb ,0); /* reserved */
768
    put_byte(pb, strlen(descr)); /* string counter */
769
    put_buffer(pb, descr, strlen(descr)); /* handler description */
770
    return updateSize(pb, pos);
771
}
772

    
773
static int mov_write_minf_tag(ByteIOContext *pb, MOVTrack* track)
774
{
775
    offset_t pos = url_ftell(pb);
776
    put_be32(pb, 0); /* size */
777
    put_tag(pb, "minf");
778
    if(track->enc->codec_type == CODEC_TYPE_VIDEO)
779
        mov_write_vmhd_tag(pb);
780
    else
781
        mov_write_smhd_tag(pb);
782
    if (track->mode == MODE_MOV) /* FIXME: Why do it for MODE_MOV only ? */
783
        mov_write_hdlr_tag(pb, NULL);
784
    mov_write_dinf_tag(pb);
785
    mov_write_stbl_tag(pb, track);
786
    return updateSize(pb, pos);
787
}
788

    
789
static int mov_write_mdhd_tag(ByteIOContext *pb, MOVTrack* track)
790
{
791
    int version = track->trackDuration < INT32_MAX ? 0 : 1;
792

    
793
    (version == 1) ? put_be32(pb, 44) : put_be32(pb, 32); /* size */
794
    put_tag(pb, "mdhd");
795
    put_byte(pb, version);
796
    put_be24(pb, 0); /* flags */
797
    if (version == 1) {
798
        put_be64(pb, track->time);
799
        put_be64(pb, track->time);
800
    } else {
801
        put_be32(pb, track->time); /* creation time */
802
        put_be32(pb, track->time); /* modification time */
803
    }
804
    put_be32(pb, track->timescale); /* time scale (sample rate for audio) */
805
    (version == 1) ? put_be64(pb, track->trackDuration) : put_be32(pb, track->trackDuration); /* duration */
806
    put_be16(pb, track->language); /* language */
807
    put_be16(pb, 0); /* reserved (quality) */
808

    
809
    if(version!=0 && track->mode == MODE_MOV){
810
        av_log(NULL, AV_LOG_ERROR,
811
            "FATAL error, file duration too long for timebase, this file will not be\n"
812
            "playable with quicktime. Choose a different timebase or a different\n"
813
            "container format\n");
814
    }
815

    
816
    return 32;
817
}
818

    
819
static int mov_write_mdia_tag(ByteIOContext *pb, MOVTrack* track)
820
{
821
    offset_t pos = url_ftell(pb);
822
    put_be32(pb, 0); /* size */
823
    put_tag(pb, "mdia");
824
    mov_write_mdhd_tag(pb, track);
825
    mov_write_hdlr_tag(pb, track);
826
    mov_write_minf_tag(pb, track);
827
    return updateSize(pb, pos);
828
}
829

    
830
static int mov_write_tkhd_tag(ByteIOContext *pb, MOVTrack* track)
831
{
832
    int64_t duration = av_rescale_rnd(track->trackDuration, globalTimescale, track->timescale, AV_ROUND_UP);
833
    int version = duration < INT32_MAX ? 0 : 1;
834

    
835
    (version == 1) ? put_be32(pb, 104) : put_be32(pb, 92); /* size */
836
    put_tag(pb, "tkhd");
837
    put_byte(pb, version);
838
    put_be24(pb, 0xf); /* flags (track enabled) */
839
    if (version == 1) {
840
        put_be64(pb, track->time);
841
        put_be64(pb, track->time);
842
    } else {
843
        put_be32(pb, track->time); /* creation time */
844
        put_be32(pb, track->time); /* modification time */
845
    }
846
    put_be32(pb, track->trackID); /* track-id */
847
    put_be32(pb, 0); /* reserved */
848
    (version == 1) ? put_be64(pb, duration) : put_be32(pb, duration);
849

    
850
    put_be32(pb, 0); /* reserved */
851
    put_be32(pb, 0); /* reserved */
852
    put_be32(pb, 0x0); /* reserved (Layer & Alternate group) */
853
    /* Volume, only for audio */
854
    if(track->enc->codec_type == CODEC_TYPE_AUDIO)
855
        put_be16(pb, 0x0100);
856
    else
857
        put_be16(pb, 0);
858
    put_be16(pb, 0); /* reserved */
859

    
860
    /* Matrix structure */
861
    put_be32(pb, 0x00010000); /* reserved */
862
    put_be32(pb, 0x0); /* reserved */
863
    put_be32(pb, 0x0); /* reserved */
864
    put_be32(pb, 0x0); /* reserved */
865
    put_be32(pb, 0x00010000); /* reserved */
866
    put_be32(pb, 0x0); /* reserved */
867
    put_be32(pb, 0x0); /* reserved */
868
    put_be32(pb, 0x0); /* reserved */
869
    put_be32(pb, 0x40000000); /* reserved */
870

    
871
    /* Track width and height, for visual only */
872
    if(track->enc->codec_type == CODEC_TYPE_VIDEO) {
873
        double sample_aspect_ratio = av_q2d(track->enc->sample_aspect_ratio);
874
        if( !sample_aspect_ratio ) sample_aspect_ratio = 1;
875
        put_be32(pb, sample_aspect_ratio * track->enc->width*0x10000);
876
        put_be32(pb, track->enc->height*0x10000);
877
    }
878
    else {
879
        put_be32(pb, 0);
880
        put_be32(pb, 0);
881
    }
882
    return 0x5c;
883
}
884

    
885
// This box seems important for the psp playback ... without it the movie seems to hang
886
static int mov_write_edts_tag(ByteIOContext *pb, MOVTrack *track)
887
{
888
    put_be32(pb, 0x24); /* size  */
889
    put_tag(pb, "edts");
890
    put_be32(pb, 0x1c); /* size  */
891
    put_tag(pb, "elst");
892
    put_be32(pb, 0x0);
893
    put_be32(pb, 0x1);
894

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

    
897
    put_be32(pb, track->cluster[0].cts); /* first pts is cts since dts is 0 */
898
    put_be32(pb, 0x00010000);
899
    return 0x24;
900
}
901

    
902
// goes at the end of each track!  ... Critical for PSP playback ("Incompatible data" without it)
903
static int mov_write_uuid_tag_psp(ByteIOContext *pb, MOVTrack *mov)
904
{
905
    put_be32(pb, 0x34); /* size ... reports as 28 in mp4box! */
906
    put_tag(pb, "uuid");
907
    put_tag(pb, "USMT");
908
    put_be32(pb, 0x21d24fce);
909
    put_be32(pb, 0xbb88695c);
910
    put_be32(pb, 0xfac9c740);
911
    put_be32(pb, 0x1c);     // another size here!
912
    put_tag(pb, "MTDT");
913
    put_be32(pb, 0x00010012);
914
    put_be32(pb, 0x0a);
915
    put_be32(pb, 0x55c40000);
916
    put_be32(pb, 0x1);
917
    put_be32(pb, 0x0);
918
    return 0x34;
919
}
920

    
921
static int mov_write_trak_tag(ByteIOContext *pb, MOVTrack* track)
922
{
923
    offset_t pos = url_ftell(pb);
924
    put_be32(pb, 0); /* size */
925
    put_tag(pb, "trak");
926
    mov_write_tkhd_tag(pb, track);
927
    if (track->mode == MODE_PSP || track->hasBframes)
928
        mov_write_edts_tag(pb, track);  // PSP Movies require edts box
929
    mov_write_mdia_tag(pb, track);
930
    if (track->mode == MODE_PSP)
931
        mov_write_uuid_tag_psp(pb,track);  // PSP Movies require this uuid box
932
    return updateSize(pb, pos);
933
}
934

    
935
#if 0
936
/* TODO: Not sorted out, but not necessary either */
937
static int mov_write_iods_tag(ByteIOContext *pb, MOVContext *mov)
938
{
939
    put_be32(pb, 0x15); /* size */
940
    put_tag(pb, "iods");
941
    put_be32(pb, 0);    /* version & flags */
942
    put_be16(pb, 0x1007);
943
    put_byte(pb, 0);
944
    put_be16(pb, 0x4fff);
945
    put_be16(pb, 0xfffe);
946
    put_be16(pb, 0x01ff);
947
    return 0x15;
948
}
949
#endif
950

    
951
static int mov_write_mvhd_tag(ByteIOContext *pb, MOVContext *mov)
952
{
953
    int maxTrackID = 1, i;
954
    int64_t maxTrackLenTemp, maxTrackLen = 0;
955
    int version;
956

    
957
    for (i=0; i<mov->nb_streams; i++) {
958
        if(mov->tracks[i].entry > 0) {
959
            maxTrackLenTemp = av_rescale_rnd(mov->tracks[i].trackDuration, globalTimescale, mov->tracks[i].timescale, AV_ROUND_UP);
960
            if(maxTrackLen < maxTrackLenTemp)
961
                maxTrackLen = maxTrackLenTemp;
962
            if(maxTrackID < mov->tracks[i].trackID)
963
                maxTrackID = mov->tracks[i].trackID;
964
        }
965
    }
966

    
967
    version = maxTrackLen < UINT32_MAX ? 0 : 1;
968
    (version == 1) ? put_be32(pb, 120) : put_be32(pb, 108); /* size */
969
    put_tag(pb, "mvhd");
970
    put_byte(pb, version);
971
    put_be24(pb, 0); /* flags */
972
    if (version == 1) {
973
        put_be64(pb, mov->time);
974
        put_be64(pb, mov->time);
975
    } else {
976
        put_be32(pb, mov->time); /* creation time */
977
        put_be32(pb, mov->time); /* modification time */
978
    }
979
    put_be32(pb, mov->timescale); /* timescale */
980
    (version == 1) ? put_be64(pb, maxTrackLen) : put_be32(pb, maxTrackLen); /* duration of longest track */
981

    
982
    put_be32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
983
    put_be16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
984
    put_be16(pb, 0); /* reserved */
985
    put_be32(pb, 0); /* reserved */
986
    put_be32(pb, 0); /* reserved */
987

    
988
    /* Matrix structure */
989
    put_be32(pb, 0x00010000); /* reserved */
990
    put_be32(pb, 0x0); /* reserved */
991
    put_be32(pb, 0x0); /* reserved */
992
    put_be32(pb, 0x0); /* reserved */
993
    put_be32(pb, 0x00010000); /* reserved */
994
    put_be32(pb, 0x0); /* reserved */
995
    put_be32(pb, 0x0); /* reserved */
996
    put_be32(pb, 0x0); /* reserved */
997
    put_be32(pb, 0x40000000); /* reserved */
998

    
999
    put_be32(pb, 0); /* reserved (preview time) */
1000
    put_be32(pb, 0); /* reserved (preview duration) */
1001
    put_be32(pb, 0); /* reserved (poster time) */
1002
    put_be32(pb, 0); /* reserved (selection time) */
1003
    put_be32(pb, 0); /* reserved (selection duration) */
1004
    put_be32(pb, 0); /* reserved (current time) */
1005
    put_be32(pb, maxTrackID+1); /* Next track id */
1006
    return 0x6c;
1007
}
1008

    
1009
static int mov_write_itunes_hdlr_tag(ByteIOContext *pb, MOVContext* mov,
1010
                                     AVFormatContext *s)
1011
{
1012
    offset_t pos = url_ftell(pb);
1013
    put_be32(pb, 0); /* size */
1014
    put_tag(pb, "hdlr");
1015
    put_be32(pb, 0);
1016
    put_be32(pb, 0);
1017
    put_tag(pb, "mdir");
1018
    put_tag(pb, "appl");
1019
    put_be32(pb, 0);
1020
    put_be32(pb, 0);
1021
    put_be16(pb, 0);
1022
    return updateSize(pb, pos);
1023
}
1024

    
1025
/* helper function to write a data tag with the specified string as data */
1026
static int mov_write_string_data_tag(ByteIOContext *pb, const char *data, int long_style)
1027
{
1028
    if(long_style){
1029
        offset_t pos = url_ftell(pb);
1030
        put_be32(pb, 0); /* size */
1031
        put_tag(pb, "data");
1032
        put_be32(pb, 1);
1033
        put_be32(pb, 0);
1034
        put_buffer(pb, data, strlen(data));
1035
        return updateSize(pb, pos);
1036
    }else{
1037
        put_be16(pb, strlen(data)); /* string length */
1038
        put_be16(pb, 0);
1039
        put_buffer(pb, data, strlen(data));
1040
        return strlen(data) + 4;
1041
    }
1042
}
1043

    
1044
static int mov_write_string_tag(ByteIOContext *pb, const char *name, const char *value, int long_style){
1045
    int size = 0;
1046
    if ( value && value[0] ) {
1047
        offset_t pos = url_ftell(pb);
1048
        put_be32(pb, 0); /* size */
1049
        put_tag(pb, name);
1050
        mov_write_string_data_tag(pb, value, long_style);
1051
        size= updateSize(pb, pos);
1052
    }
1053
    return size;
1054
}
1055

    
1056
/* iTunes year */
1057
static int mov_write_day_tag(ByteIOContext *pb, int year, int long_style)
1058
{
1059
    if(year){
1060
        char year_str[5];
1061
        snprintf(year_str, sizeof(year_str), "%04d", year);
1062
        return mov_write_string_tag(pb, "\251day", year_str, long_style);
1063
    }else
1064
        return 0;
1065
}
1066

    
1067
/* iTunes track number */
1068
static int mov_write_trkn_tag(ByteIOContext *pb, MOVContext* mov,
1069
                              AVFormatContext *s)
1070
{
1071
    int size = 0;
1072
    if ( s->track ) {
1073
        offset_t pos = url_ftell(pb);
1074
        put_be32(pb, 0); /* size */
1075
        put_tag(pb, "trkn");
1076
        {
1077
            offset_t pos = url_ftell(pb);
1078
            put_be32(pb, 0); /* size */
1079
            put_tag(pb, "data");
1080
            put_be32(pb, 0);        // 8 bytes empty
1081
            put_be32(pb, 0);
1082
            put_be16(pb, 0);        // empty
1083
            put_be16(pb, s->track); // track number
1084
            put_be16(pb, 0);        // total track number
1085
            put_be16(pb, 0);        // empty
1086
            updateSize(pb, pos);
1087
        }
1088
        size = updateSize(pb, pos);
1089
    }
1090
    return size;
1091
}
1092

    
1093
/* iTunes meta data list */
1094
static int mov_write_ilst_tag(ByteIOContext *pb, MOVContext* mov,
1095
                              AVFormatContext *s)
1096
{
1097
    offset_t pos = url_ftell(pb);
1098
    put_be32(pb, 0); /* size */
1099
    put_tag(pb, "ilst");
1100
    mov_write_string_tag(pb, "\251nam", s->title         , 1);
1101
    mov_write_string_tag(pb, "\251ART", s->author        , 1);
1102
    mov_write_string_tag(pb, "\251wrt", s->author        , 1);
1103
    mov_write_string_tag(pb, "\251alb", s->album         , 1);
1104
    mov_write_day_tag(pb, s->year ,1);
1105
    if(mov->tracks[0].enc && !(mov->tracks[0].enc->flags & CODEC_FLAG_BITEXACT))
1106
        mov_write_string_tag(pb, "\251too", LIBAVFORMAT_IDENT, 1);
1107
    mov_write_string_tag(pb, "\251cmt", s->comment       , 1);
1108
    mov_write_string_tag(pb, "\251gen", s->genre         , 1);
1109
    mov_write_trkn_tag(pb, mov, s);
1110
    return updateSize(pb, pos);
1111
}
1112

    
1113
/* iTunes meta data tag */
1114
static int mov_write_meta_tag(ByteIOContext *pb, MOVContext* mov,
1115
                              AVFormatContext *s)
1116
{
1117
    int size = 0;
1118

    
1119
    // only save meta tag if required
1120
    if ( s->title[0] || s->author[0] || s->album[0] || s->year ||
1121
         s->comment[0] || s->genre[0] || s->track ) {
1122
        offset_t pos = url_ftell(pb);
1123
        put_be32(pb, 0); /* size */
1124
        put_tag(pb, "meta");
1125
        put_be32(pb, 0);
1126
        mov_write_itunes_hdlr_tag(pb, mov, s);
1127
        mov_write_ilst_tag(pb, mov, s);
1128
        size = updateSize(pb, pos);
1129
    }
1130
    return size;
1131
}
1132

    
1133
static int mov_write_udta_tag(ByteIOContext *pb, MOVContext* mov,
1134
                              AVFormatContext *s)
1135
{
1136
    int i, req = 0;
1137

    
1138
    /* Requirements */
1139
    for (i=0; i<mov->nb_streams; i++) {
1140
        if(mov->tracks[i].entry <= 0) continue;
1141
        if (mov->tracks[i].enc->codec_id == CODEC_ID_AAC ||
1142
            mov->tracks[i].enc->codec_id == CODEC_ID_MPEG4) {
1143
            req = 1;
1144
            break;
1145
        }
1146
    }
1147

    
1148
    if (s->title[0]   || s->author[0] || s->album[0] || s->year ||
1149
        s->comment[0] || s->genre[0]  || s->track ||
1150
       (mov->mode == MODE_MOV &&
1151
      ((mov->tracks[0].enc && !mov->tracks[0].enc->flags & CODEC_FLAG_BITEXACT) || req))) {
1152
        offset_t pos = url_ftell(pb);
1153

    
1154
        put_be32(pb, 0); /* size */
1155
        put_tag(pb, "udta");
1156

    
1157
        /* iTunes meta data */
1158
        mov_write_meta_tag(pb, mov, s);
1159

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

    
1165
            mov_write_string_tag(pb, "\251nam", s->title         , 0);
1166
            mov_write_string_tag(pb, "\251aut", s->author        , 0);
1167
            mov_write_string_tag(pb, "\251alb", s->album         , 0);
1168
            mov_write_day_tag(pb, s->year, 0);
1169
            if(mov->tracks[0].enc && !(mov->tracks[0].enc->flags & CODEC_FLAG_BITEXACT))
1170
                mov_write_string_tag(pb, "\251enc", LIBAVFORMAT_IDENT, 0);
1171
            mov_write_string_tag(pb, "\251des", s->comment       , 0);
1172
            mov_write_string_tag(pb, "\251gen", s->genre         , 0);
1173
        }
1174

    
1175
        return updateSize(pb, pos);
1176
    }
1177

    
1178
    return 0;
1179
}
1180

    
1181
static int utf8len(uint8_t *b){
1182
    int len=0;
1183
    int val;
1184
    while(*b){
1185
        GET_UTF8(val, *b++, return -1;)
1186
        len++;
1187
    }
1188
    return len;
1189
}
1190

    
1191
static int ascii_to_wc (ByteIOContext *pb, uint8_t *b)
1192
{
1193
    int val;
1194
    while(*b){
1195
        GET_UTF8(val, *b++, return -1;)
1196
        put_be16(pb, val);
1197
    }
1198
    put_be16(pb, 0x00);
1199
    return 0;
1200
}
1201

    
1202
static uint16_t language_code (const char *str)
1203
{
1204
    return ((((str[0]-0x60) & 0x1F)<<10) + (((str[1]-0x60) & 0x1F)<<5) + ((str[2]-0x60) & 0x1F));
1205
}
1206

    
1207
static int mov_write_uuidusmt_tag (ByteIOContext *pb, AVFormatContext *s)
1208
{
1209
    size_t len, size;
1210
    offset_t pos, curpos;
1211

    
1212
    size = 0;
1213
    if (s->title[0]) {
1214
        pos = url_ftell(pb);
1215
        put_be32(pb, 0); /* size placeholder*/
1216
        put_tag(pb, "uuid");
1217
        put_tag(pb, "USMT");
1218
        put_be32(pb, 0x21d24fce ); /* 96 bit UUID */
1219
        put_be32(pb, 0xbb88695c );
1220
        put_be32(pb, 0xfac9c740 );
1221
        size += 24;
1222

    
1223
        put_be32(pb, 0); /* size placeholder*/
1224
        put_tag(pb, "MTDT");
1225
        put_be16(pb, 4);
1226
        size += 10;
1227

    
1228
        // ?
1229
        put_be16(pb, 0x0C);                 /* size */
1230
        put_be32(pb, 0x0B);                 /* type */
1231
        put_be16(pb, language_code("und")); /* language */
1232
        put_be16(pb, 0x0);                  /* ? */
1233
        put_be16(pb, 0x021C);               /* data */
1234
        size += 12;
1235

    
1236
        // Encoder
1237
        len = utf8len(LIBAVCODEC_IDENT)+1;
1238
        if(len<=0)
1239
            goto not_utf8;
1240
        put_be16(pb, len*2+10);             /* size */
1241
        put_be32(pb, 0x04);                 /* type */
1242
        put_be16(pb, language_code("eng")); /* language */
1243
        put_be16(pb, 0x01);                 /* ? */
1244
        ascii_to_wc(pb, LIBAVCODEC_IDENT);
1245
        size += len*2+10;
1246

    
1247
        // Title
1248
        len = utf8len(s->title)+1;
1249
        if(len<=0)
1250
            goto not_utf8;
1251
        put_be16(pb, len*2+10);             /* size */
1252
        put_be32(pb, 0x01);                 /* type */
1253
        put_be16(pb, language_code("eng")); /* language */
1254
        put_be16(pb, 0x01);                 /* ? */
1255
        ascii_to_wc (pb, s->title);
1256
        size += len*2+10;
1257

    
1258
        // Date
1259
//        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);
1260
        len = utf8len("2006/04/01 11:11:11")+1;
1261
        if(len<=0)
1262
            goto not_utf8;
1263
        put_be16(pb, len*2+10);    /* size */
1264
        put_be32(pb, 0x03);        /* type */
1265
        put_be16(pb, language_code("und")); /* language */
1266
        put_be16(pb, 0x01);        /* ? */
1267
        ascii_to_wc (pb, "2006/04/01 11:11:11");
1268
        size += len*2+10;
1269

    
1270
        // size
1271
        curpos = url_ftell(pb);
1272
        url_fseek(pb, pos, SEEK_SET);
1273
        put_be32(pb, size);
1274
        url_fseek(pb, pos+24, SEEK_SET);
1275
        put_be32(pb, size-24);
1276
        url_fseek(pb, curpos, SEEK_SET);
1277
    }
1278

    
1279
    return size;
1280
not_utf8:
1281
    av_log(s, AV_LOG_ERROR, "not utf8\n");
1282
    return -1;
1283
}
1284

    
1285
static int mov_write_moov_tag(ByteIOContext *pb, MOVContext *mov,
1286
                              AVFormatContext *s)
1287
{
1288
    int i;
1289
    offset_t pos = url_ftell(pb);
1290
    put_be32(pb, 0); /* size placeholder*/
1291
    put_tag(pb, "moov");
1292
    mov->timescale = globalTimescale;
1293

    
1294
    for (i=0; i<mov->nb_streams; i++) {
1295
        if(mov->tracks[i].entry <= 0) continue;
1296

    
1297
        mov->tracks[i].time = mov->time;
1298
        mov->tracks[i].trackID = i+1;
1299
    }
1300

    
1301
    mov_write_mvhd_tag(pb, mov);
1302
    //mov_write_iods_tag(pb, mov);
1303
    for (i=0; i<mov->nb_streams; i++) {
1304
        if(mov->tracks[i].entry > 0) {
1305
            mov_write_trak_tag(pb, &(mov->tracks[i]));
1306
        }
1307
    }
1308

    
1309
    if (mov->mode == MODE_PSP)
1310
        mov_write_uuidusmt_tag(pb, s);
1311
    else if (mov->mode != MODE_3GP && mov->mode != MODE_3G2)
1312
        mov_write_udta_tag(pb, mov, s);
1313

    
1314
    return updateSize(pb, pos);
1315
}
1316

    
1317
static int mov_write_mdat_tag(ByteIOContext *pb, MOVContext* mov)
1318
{
1319
    put_be32(pb, 8);    // placeholder for extended size field (64 bit)
1320
    put_tag(pb, mov->mode == MODE_MOV ? "wide" : "free");
1321

    
1322
    mov->mdat_pos = url_ftell(pb);
1323
    put_be32(pb, 0); /* size placeholder*/
1324
    put_tag(pb, "mdat");
1325
    return 0;
1326
}
1327

    
1328
/* TODO: This needs to be more general */
1329
static void mov_write_ftyp_tag (ByteIOContext *pb, AVFormatContext *s)
1330
{
1331
    MOVContext *mov = s->priv_data;
1332

    
1333
    put_be32(pb, 0x14 ); /* size */
1334
    put_tag(pb, "ftyp");
1335

    
1336
    if ( mov->mode == MODE_3GP )
1337
        put_tag(pb, "3gp4");
1338
    else if ( mov->mode == MODE_3G2 )
1339
        put_tag(pb, "3g2a");
1340
    else if ( mov->mode == MODE_PSP )
1341
        put_tag(pb, "MSNV");
1342
    else if ( mov->mode == MODE_MP4 )
1343
        put_tag(pb, "isom");
1344
    else
1345
        put_tag(pb, "qt  ");
1346

    
1347
    put_be32(pb, 0x200 );
1348

    
1349
    if ( mov->mode == MODE_3GP )
1350
        put_tag(pb, "3gp4");
1351
    else if ( mov->mode == MODE_3G2 )
1352
        put_tag(pb, "3g2a");
1353
    else if ( mov->mode == MODE_PSP )
1354
        put_tag(pb, "MSNV");
1355
    else if ( mov->mode == MODE_MP4 )
1356
        put_tag(pb, "mp41");
1357
    else
1358
        put_tag(pb, "qt  ");
1359
}
1360

    
1361
static void mov_write_uuidprof_tag(ByteIOContext *pb, AVFormatContext *s)
1362
{
1363
    AVCodecContext *VideoCodec = s->streams[0]->codec;
1364
    AVCodecContext *AudioCodec = s->streams[1]->codec;
1365
    int AudioRate = AudioCodec->sample_rate;
1366
    int FrameRate = ((VideoCodec->time_base.den) * (0x10000))/ (VideoCodec->time_base.num);
1367
    int audio_kbitrate= AudioCodec->bit_rate / 1000;
1368
    int video_kbitrate= FFMIN(VideoCodec->bit_rate / 1000, 800 - audio_kbitrate);
1369

    
1370
    put_be32(pb, 0x94 ); /* size */
1371
    put_tag(pb, "uuid");
1372
    put_tag(pb, "PROF");
1373

    
1374
    put_be32(pb, 0x21d24fce ); /* 96 bit UUID */
1375
    put_be32(pb, 0xbb88695c );
1376
    put_be32(pb, 0xfac9c740 );
1377

    
1378
    put_be32(pb, 0x0 );  /* ? */
1379
    put_be32(pb, 0x3 );  /* 3 sections ? */
1380

    
1381
    put_be32(pb, 0x14 ); /* size */
1382
    put_tag(pb, "FPRF");
1383
    put_be32(pb, 0x0 );  /* ? */
1384
    put_be32(pb, 0x0 );  /* ? */
1385
    put_be32(pb, 0x0 );  /* ? */
1386

    
1387
    put_be32(pb, 0x2c );  /* size */
1388
    put_tag(pb, "APRF");   /* audio */
1389
    put_be32(pb, 0x0 );
1390
    put_be32(pb, 0x2 );   /* TrackID */
1391
    put_tag(pb, "mp4a");
1392
    put_be32(pb, 0x20f );
1393
    put_be32(pb, 0x0 );
1394
    put_be32(pb, audio_kbitrate);
1395
    put_be32(pb, audio_kbitrate);
1396
    put_be32(pb, AudioRate );
1397
    put_be32(pb, AudioCodec->channels );
1398

    
1399
    put_be32(pb, 0x34 );  /* size */
1400
    put_tag(pb, "VPRF");   /* video */
1401
    put_be32(pb, 0x0 );
1402
    put_be32(pb, 0x1 );    /* TrackID */
1403
    if (VideoCodec->codec_id == CODEC_ID_H264) {
1404
        put_tag(pb, "avc1");
1405
        put_be16(pb, 0x014D );
1406
        put_be16(pb, 0x0015 );
1407
    } else {
1408
        put_tag(pb, "mp4v");
1409
        put_be16(pb, 0x0000 );
1410
        put_be16(pb, 0x0103 );
1411
    }
1412
    put_be32(pb, 0x0 );
1413
    put_be32(pb, video_kbitrate);
1414
    put_be32(pb, video_kbitrate);
1415
    put_be32(pb, FrameRate);
1416
    put_be32(pb, FrameRate);
1417
    put_be16(pb, VideoCodec->width);
1418
    put_be16(pb, VideoCodec->height);
1419
    put_be32(pb, 0x010001); /* ? */
1420
}
1421

    
1422
static int mov_write_header(AVFormatContext *s)
1423
{
1424
    ByteIOContext *pb = s->pb;
1425
    MOVContext *mov = s->priv_data;
1426
    int i;
1427

    
1428
    if (url_is_streamed(s->pb)) {
1429
        av_log(s, AV_LOG_ERROR, "muxer does not support non seekable output\n");
1430
        return -1;
1431
    }
1432

    
1433
    /* Default mode == MP4 */
1434
    mov->mode = MODE_MP4;
1435

    
1436
    if (s->oformat != NULL) {
1437
        if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
1438
        else if (!strcmp("3g2", s->oformat->name)) mov->mode = MODE_3G2;
1439
        else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
1440
        else if (!strcmp("psp", s->oformat->name)) mov->mode = MODE_PSP;
1441

    
1442
        mov_write_ftyp_tag(pb,s);
1443
        if ( mov->mode == MODE_PSP ) {
1444
            if ( s->nb_streams != 2 ) {
1445
                av_log(s, AV_LOG_ERROR, "PSP mode need one video and one audio stream\n");
1446
                return -1;
1447
            }
1448
            mov_write_uuidprof_tag(pb,s);
1449
        }
1450
    }
1451

    
1452
    for(i=0; i<s->nb_streams; i++){
1453
        AVStream *st= s->streams[i];
1454
        MOVTrack *track= &mov->tracks[i];
1455

    
1456
        track->enc = st->codec;
1457
        track->language = ff_mov_iso639_to_lang(st->language, mov->mode != MODE_MOV);
1458
        track->mode = mov->mode;
1459
        track->tag = mov_find_codec_tag(s, track);
1460
        if (!track->tag) {
1461
            av_log(s, AV_LOG_ERROR, "track %d: could not find tag for codec\n", i);
1462
            return -1;
1463
        }
1464
        if(st->codec->codec_type == CODEC_TYPE_VIDEO){
1465
            track->timescale = st->codec->time_base.den;
1466
            av_set_pts_info(st, 64, 1, st->codec->time_base.den);
1467
            if (track->mode == MODE_MOV && track->timescale > 100000)
1468
                av_log(NULL, AV_LOG_WARNING,
1469
                       "WARNING codec timebase is very high. If duration is too long,\n"
1470
                       "file may not be playable by quicktime. Specify a shorter timebase\n"
1471
                       "or choose different container.\n");
1472
        }else if(st->codec->codec_type == CODEC_TYPE_AUDIO){
1473
            track->timescale = st->codec->sample_rate;
1474
            av_set_pts_info(st, 64, 1, st->codec->sample_rate);
1475
            if(!st->codec->frame_size){
1476
                av_log(s, AV_LOG_ERROR, "track %d: codec frame size is not set\n", i);
1477
                return -1;
1478
            }else if(st->codec->frame_size > 1){ /* assume compressed audio */
1479
                track->audio_vbr = 1;
1480
            }else{
1481
                track->sampleSize = (av_get_bits_per_sample(st->codec->codec_id) >> 3) * st->codec->channels;
1482
            }
1483
        }
1484
    }
1485

    
1486
    mov_write_mdat_tag(pb, mov);
1487
    mov->time = s->timestamp + 0x7C25B080; //1970 based -> 1904 based
1488
    mov->nb_streams = s->nb_streams;
1489

    
1490
    put_flush_packet(pb);
1491

    
1492
    return 0;
1493
}
1494

    
1495
static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
1496
{
1497
    MOVContext *mov = s->priv_data;
1498
    ByteIOContext *pb = s->pb;
1499
    MOVTrack *trk = &mov->tracks[pkt->stream_index];
1500
    AVCodecContext *enc = trk->enc;
1501
    unsigned int samplesInChunk = 0;
1502
    int size= pkt->size;
1503

    
1504
    if (url_is_streamed(s->pb)) return 0; /* Can't handle that */
1505
    if (!size) return 0; /* Discard 0 sized packets */
1506

    
1507
    if (enc->codec_id == CODEC_ID_AMR_NB) {
1508
        /* We must find out how many AMR blocks there are in one packet */
1509
        static uint16_t packed_size[16] =
1510
            {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 0};
1511
        int len = 0;
1512

    
1513
        while (len < size && samplesInChunk < 100) {
1514
            len += packed_size[(pkt->data[len] >> 3) & 0x0F];
1515
            samplesInChunk++;
1516
        }
1517
        if(samplesInChunk > 1){
1518
            av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, implement a AVParser for it\n");
1519
            return -1;
1520
        }
1521
    } else if (trk->sampleSize)
1522
        samplesInChunk = size/trk->sampleSize;
1523
    else
1524
        samplesInChunk = 1;
1525

    
1526
    /* copy extradata if it exists */
1527
    if (trk->vosLen == 0 && enc->extradata_size > 0) {
1528
        trk->vosLen = enc->extradata_size;
1529
        trk->vosData = av_malloc(trk->vosLen);
1530
        memcpy(trk->vosData, enc->extradata, trk->vosLen);
1531
    }
1532

    
1533
    if (enc->codec_id == CODEC_ID_H264 && trk->vosLen > 0 && *(uint8_t *)trk->vosData != 1) {
1534
        /* from x264 or from bytestream h264 */
1535
        /* nal reformating needed */
1536
        int ret = ff_avc_parse_nal_units(pkt->data, &pkt->data, &pkt->size);
1537
        if (ret < 0)
1538
            return ret;
1539
        assert(pkt->size);
1540
        size = pkt->size;
1541
    } else if (enc->codec_id == CODEC_ID_DNXHD && !trk->vosLen) {
1542
        /* copy frame header to create needed atoms */
1543
        if (size < 640)
1544
            return -1;
1545
        trk->vosLen = 640;
1546
        trk->vosData = av_malloc(trk->vosLen);
1547
        memcpy(trk->vosData, pkt->data, 640);
1548
    }
1549

    
1550
    if (!(trk->entry % MOV_INDEX_CLUSTER_SIZE)) {
1551
        trk->cluster = av_realloc(trk->cluster, (trk->entry + MOV_INDEX_CLUSTER_SIZE) * sizeof(*trk->cluster));
1552
        if (!trk->cluster)
1553
            return -1;
1554
    }
1555

    
1556
    trk->cluster[trk->entry].pos = url_ftell(pb);
1557
    trk->cluster[trk->entry].samplesInChunk = samplesInChunk;
1558
    trk->cluster[trk->entry].size = size;
1559
    trk->cluster[trk->entry].entries = samplesInChunk;
1560
    trk->cluster[trk->entry].dts = pkt->dts;
1561
    trk->trackDuration = pkt->dts - trk->cluster[0].dts + pkt->duration;
1562

    
1563
    if(enc->codec_type == CODEC_TYPE_VIDEO) {
1564
        if (pkt->dts != pkt->pts)
1565
            trk->hasBframes = 1;
1566
        trk->cluster[trk->entry].cts = pkt->pts - pkt->dts;
1567
        trk->cluster[trk->entry].key_frame = !!(pkt->flags & PKT_FLAG_KEY);
1568
        if(trk->cluster[trk->entry].key_frame)
1569
            trk->hasKeyframes++;
1570
    }
1571
    trk->entry++;
1572
    trk->sampleCount += samplesInChunk;
1573
    mov->mdat_size += size;
1574

    
1575
    put_buffer(pb, pkt->data, size);
1576

    
1577
    put_flush_packet(pb);
1578
    return 0;
1579
}
1580

    
1581
static int mov_write_trailer(AVFormatContext *s)
1582
{
1583
    MOVContext *mov = s->priv_data;
1584
    ByteIOContext *pb = s->pb;
1585
    int res = 0;
1586
    int i;
1587

    
1588
    offset_t moov_pos = url_ftell(pb);
1589

    
1590
    /* Write size of mdat tag */
1591
    if (mov->mdat_size+8 <= UINT32_MAX) {
1592
        url_fseek(pb, mov->mdat_pos, SEEK_SET);
1593
        put_be32(pb, mov->mdat_size+8);
1594
    } else {
1595
        /* overwrite 'wide' placeholder atom */
1596
        url_fseek(pb, mov->mdat_pos - 8, SEEK_SET);
1597
        put_be32(pb, 1); /* special value: real atom size will be 64 bit value after tag field */
1598
        put_tag(pb, "mdat");
1599
        put_be64(pb, mov->mdat_size+16);
1600
    }
1601
    url_fseek(pb, moov_pos, SEEK_SET);
1602

    
1603
    mov_write_moov_tag(pb, mov, s);
1604

    
1605
    for (i=0; i<mov->nb_streams; i++) {
1606
        av_freep(&mov->tracks[i].cluster);
1607

    
1608
        if( mov->tracks[i].vosLen ) av_free( mov->tracks[i].vosData );
1609

    
1610
    }
1611

    
1612
    put_flush_packet(pb);
1613

    
1614
    return res;
1615
}
1616

    
1617
#ifdef CONFIG_MOV_MUXER
1618
AVOutputFormat mov_muxer = {
1619
    "mov",
1620
    "mov format",
1621
    NULL,
1622
    "mov",
1623
    sizeof(MOVContext),
1624
    CODEC_ID_AAC,
1625
    CODEC_ID_MPEG4,
1626
    mov_write_header,
1627
    mov_write_packet,
1628
    mov_write_trailer,
1629
    .flags = AVFMT_GLOBALHEADER,
1630
    .codec_tag = (const AVCodecTag*[]){codec_movvideo_tags, codec_movaudio_tags, 0},
1631
};
1632
#endif
1633
#ifdef CONFIG_TGP_MUXER
1634
AVOutputFormat tgp_muxer = {
1635
    "3gp",
1636
    "3gp format",
1637
    NULL,
1638
    "3gp",
1639
    sizeof(MOVContext),
1640
    CODEC_ID_AMR_NB,
1641
    CODEC_ID_H263,
1642
    mov_write_header,
1643
    mov_write_packet,
1644
    mov_write_trailer,
1645
    .flags = AVFMT_GLOBALHEADER,
1646
    .codec_tag = (const AVCodecTag*[]){codec_3gp_tags, 0},
1647
};
1648
#endif
1649
#ifdef CONFIG_MP4_MUXER
1650
AVOutputFormat mp4_muxer = {
1651
    "mp4",
1652
    "mp4 format",
1653
    "application/mp4",
1654
    "mp4,m4a",
1655
    sizeof(MOVContext),
1656
    CODEC_ID_AAC,
1657
    CODEC_ID_MPEG4,
1658
    mov_write_header,
1659
    mov_write_packet,
1660
    mov_write_trailer,
1661
    .flags = AVFMT_GLOBALHEADER,
1662
    .codec_tag = (const AVCodecTag*[]){ff_mp4_obj_type, 0},
1663
};
1664
#endif
1665
#ifdef CONFIG_PSP_MUXER
1666
AVOutputFormat psp_muxer = {
1667
    "psp",
1668
    "psp mp4 format",
1669
    NULL,
1670
    "mp4,psp",
1671
    sizeof(MOVContext),
1672
    CODEC_ID_AAC,
1673
    CODEC_ID_MPEG4,
1674
    mov_write_header,
1675
    mov_write_packet,
1676
    mov_write_trailer,
1677
    .flags = AVFMT_GLOBALHEADER,
1678
    .codec_tag = (const AVCodecTag*[]){ff_mp4_obj_type, 0},
1679
};
1680
#endif
1681
#ifdef CONFIG_TG2_MUXER
1682
AVOutputFormat tg2_muxer = {
1683
    "3g2",
1684
    "3gp2 format",
1685
    NULL,
1686
    "3g2",
1687
    sizeof(MOVContext),
1688
    CODEC_ID_AMR_NB,
1689
    CODEC_ID_H263,
1690
    mov_write_header,
1691
    mov_write_packet,
1692
    mov_write_trailer,
1693
    .flags = AVFMT_GLOBALHEADER,
1694
    .codec_tag = (const AVCodecTag*[]){codec_3gp_tags, 0},
1695
};
1696
#endif