Statistics
| Branch: | Revision:

ffmpeg / libavformat / movenc.c @ 506e4bb8

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 no mac fcc found, try with Microsoft tags
510
                if (!tag) {
511
                    tag = codec_get_tag(codec_bmp_tags, track->enc->codec_id);
512
                    if (tag) {
513
                        av_log(s, AV_LOG_INFO, "Warning, using MS style video codec tag, the file may be unplayable!\n");
514
                    }
515
                }
516
            } else if (track->enc->codec_type == CODEC_TYPE_AUDIO) {
517
                tag = codec_get_tag(codec_movaudio_tags, track->enc->codec_id);
518
                // if no mac fcc found, try with Microsoft tags
519
                if (!tag) {
520
                    int ms_tag = codec_get_tag(codec_wav_tags, track->enc->codec_id);
521
                    if (ms_tag) {
522
                        tag = MKTAG('m', 's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
523
                        av_log(s, AV_LOG_INFO, "Warning, using MS style audio codec tag, the file may be unplayable!\n");
524
                    }
525
                }
526
            }
527
        }
528
    }
529
    return tag;
530
}
531

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

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

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

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

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

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

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

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

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

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

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

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

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

    
691
    return 28;
692
}
693

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

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

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

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

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

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

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

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

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

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

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

    
817
    return 32;
818
}
819

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1179
    return 0;
1180
}
1181

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1348
    put_be32(pb, 0x200 );
1349

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1491
    put_flush_packet(pb);
1492

    
1493
    return 0;
1494
}
1495

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

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

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

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

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

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

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

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

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

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

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

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

    
1589
    offset_t moov_pos = url_ftell(pb);
1590

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

    
1604
    mov_write_moov_tag(pb, mov, s);
1605

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

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

    
1611
    }
1612

    
1613
    put_flush_packet(pb);
1614

    
1615
    return res;
1616
}
1617

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