Statistics
| Branch: | Revision:

ffmpeg / libavformat / movenc.c @ b973f9ad

History | View | Annotate | Download (52 KB)

1 1cb5f7fd Michael Niedermayer
/*
2 7fbde343 Aurelien Jacobs
 * MOV, 3GP, MP4 muxer
3 1cb5f7fd Michael Niedermayer
 * Copyright (c) 2003 Thomas Raivio.
4 69dde1ad Gildas Bazin
 * Copyright (c) 2004 Gildas Bazin <gbazin at videolan dot org>.
5 1cb5f7fd Michael Niedermayer
 *
6 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
7
 *
8
 * FFmpeg is free software; you can redistribute it and/or
9 1cb5f7fd Michael Niedermayer
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
12 1cb5f7fd Michael Niedermayer
 *
13 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
14 1cb5f7fd Michael Niedermayer
 * 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 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
20 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 1cb5f7fd Michael Niedermayer
 */
22
#include "avformat.h"
23 9d9f4119 Måns Rullgård
#include "riff.h"
24 1cb5f7fd Michael Niedermayer
#include "avio.h"
25 e40ee6a2 Baptiste Coudurier
#include "isom.h"
26 1bd2d763 Aurelien Jacobs
#include "avc.h"
27 1cb5f7fd Michael Niedermayer
28 6e6d6dc0 Michael Niedermayer
#undef NDEBUG
29
#include <assert.h>
30
31 1cb5f7fd Michael Niedermayer
#define MOV_INDEX_CLUSTER_SIZE 16384
32
#define globalTimescale 1000
33
34 69dde1ad Gildas Bazin
#define MODE_MP4 0
35
#define MODE_MOV 1
36
#define MODE_3GP 2
37 115329f1 Diego Biurrun
#define MODE_PSP 3 // example working PSP command line:
38 8af18154 tjcannell@blueyonder.co.uk
// ffmpeg -i testinput.avi  -f psp -r 14.985 -s 320x240 -b 768 -ar 24000 -ab 32 M4V00001.MP4
39 8536ab89 tjraivio@cc.hut.fi
#define MODE_3G2 4
40 69dde1ad Gildas Bazin
41 1cb5f7fd Michael Niedermayer
typedef struct MOVIentry {
42 b29af723 Michael Niedermayer
    unsigned int flags, size;
43
    uint64_t     pos;
44 e45ccf79 Gildas Bazin
    unsigned int samplesInChunk;
45 f578f938 Thomas Raivio
    char         key_frame;
46 1cb5f7fd Michael Niedermayer
    unsigned int entries;
47 b4712e3c Baptiste Coudurier
    int64_t      cts;
48 aa90239f Baptiste Coudurier
    int64_t      dts;
49 1cb5f7fd Michael Niedermayer
} MOVIentry;
50
51
typedef struct MOVIndex {
52 69dde1ad Gildas Bazin
    int         mode;
53 1cb5f7fd Michael Niedermayer
    int         entry;
54
    long        timescale;
55
    long        time;
56 a3a80ddc Baptiste Coudurier
    int64_t     trackDuration;
57 e45ccf79 Gildas Bazin
    long        sampleCount;
58 5cb49ca1 Baptiste Coudurier
    long        sampleSize;
59 f578f938 Thomas Raivio
    int         hasKeyframes;
60 b4712e3c Baptiste Coudurier
    int         hasBframes;
61 ab561df9 François Revol
    int         language;
62 1cb5f7fd Michael Niedermayer
    int         trackID;
63 039627cf Baptiste Coudurier
    int         tag;
64 1cb5f7fd Michael Niedermayer
    AVCodecContext *enc;
65
66
    int         vosLen;
67 6e6d6dc0 Michael Niedermayer
    uint8_t     *vosData;
68 0aec3c5c Baptiste Coudurier
    MOVIentry   *cluster;
69 aa90239f Baptiste Coudurier
    int         audio_vbr;
70 1cb5f7fd Michael Niedermayer
} MOVTrack;
71
72 8af18154 tjcannell@blueyonder.co.uk
typedef struct MOVContext {
73 69dde1ad Gildas Bazin
    int     mode;
74 a3a80ddc Baptiste Coudurier
    int64_t time;
75 1cb5f7fd Michael Niedermayer
    int     nb_streams;
76 f578f938 Thomas Raivio
    offset_t mdat_pos;
77 039627cf Baptiste Coudurier
    uint64_t mdat_size;
78 1cb5f7fd Michael Niedermayer
    long    timescale;
79
    MOVTrack tracks[MAX_STREAMS];
80
} MOVContext;
81
82 90b5b51e Diego Biurrun
//FIXME support 64 bit variant with wide placeholders
83 b29af723 Michael Niedermayer
static offset_t updateSize (ByteIOContext *pb, offset_t pos)
84 1cb5f7fd Michael Niedermayer
{
85 b29af723 Michael Niedermayer
    offset_t curpos = url_ftell(pb);
86 1cb5f7fd Michael Niedermayer
    url_fseek(pb, pos, SEEK_SET);
87 6e6d6dc0 Michael Niedermayer
    put_be32(pb, curpos - pos); /* rewrite size */
88 1cb5f7fd Michael Niedermayer
    url_fseek(pb, curpos, SEEK_SET);
89 6e6d6dc0 Michael Niedermayer
90
    return curpos - pos;
91 1cb5f7fd Michael Niedermayer
}
92
93 e45ccf79 Gildas Bazin
/* Chunk offset atom */
94 6e6d6dc0 Michael Niedermayer
static int mov_write_stco_tag(ByteIOContext *pb, MOVTrack* track)
95 1cb5f7fd Michael Niedermayer
{
96
    int i;
97 ed06cf6d Benjamin Larsson
    int mode64 = 0; //   use 32 bit size variant if possible
98 b29af723 Michael Niedermayer
    offset_t pos = url_ftell(pb);
99 f578f938 Thomas Raivio
    put_be32(pb, 0); /* size */
100 b29af723 Michael Niedermayer
    if (pos > UINT32_MAX) {
101
        mode64 = 1;
102
        put_tag(pb, "co64");
103
    } else
104
        put_tag(pb, "stco");
105 1cb5f7fd Michael Niedermayer
    put_be32(pb, 0); /* version & flags */
106
    put_be32(pb, track->entry); /* entry count */
107
    for (i=0; i<track->entry; i++) {
108 b29af723 Michael Niedermayer
        if(mode64 == 1)
109 0aec3c5c Baptiste Coudurier
            put_be64(pb, track->cluster[i].pos);
110 b29af723 Michael Niedermayer
        else
111 0aec3c5c Baptiste Coudurier
            put_be32(pb, track->cluster[i].pos);
112 1cb5f7fd Michael Niedermayer
    }
113 f578f938 Thomas Raivio
    return updateSize (pb, pos);
114 1cb5f7fd Michael Niedermayer
}
115
116 e45ccf79 Gildas Bazin
/* Sample size atom */
117 6e6d6dc0 Michael Niedermayer
static int mov_write_stsz_tag(ByteIOContext *pb, MOVTrack* track)
118 1cb5f7fd Michael Niedermayer
{
119 f578f938 Thomas Raivio
    int equalChunks = 1;
120 e45ccf79 Gildas Bazin
    int i, j, entries = 0, tst = -1, oldtst = -1;
121 1cb5f7fd Michael Niedermayer
122 b29af723 Michael Niedermayer
    offset_t pos = url_ftell(pb);
123 f578f938 Thomas Raivio
    put_be32(pb, 0); /* size */
124 1cb5f7fd Michael Niedermayer
    put_tag(pb, "stsz");
125
    put_be32(pb, 0); /* version & flags */
126
127 f578f938 Thomas Raivio
    for (i=0; i<track->entry; i++) {
128 0aec3c5c Baptiste Coudurier
        tst = track->cluster[i].size/track->cluster[i].entries;
129 e45ccf79 Gildas Bazin
        if(oldtst != -1 && tst != oldtst) {
130
            equalChunks = 0;
131 f578f938 Thomas Raivio
        }
132
        oldtst = tst;
133 0aec3c5c Baptiste Coudurier
        entries += track->cluster[i].entries;
134 f578f938 Thomas Raivio
    }
135 e45ccf79 Gildas Bazin
    if (equalChunks) {
136 0aec3c5c Baptiste Coudurier
        int sSize = track->cluster[0].size/track->cluster[0].entries;
137 115329f1 Diego Biurrun
        put_be32(pb, sSize); // sample size
138 e45ccf79 Gildas Bazin
        put_be32(pb, entries); // sample count
139 1cb5f7fd Michael Niedermayer
    }
140 f578f938 Thomas Raivio
    else {
141 115329f1 Diego Biurrun
        put_be32(pb, 0); // sample size
142
        put_be32(pb, entries); // sample count
143 f578f938 Thomas Raivio
        for (i=0; i<track->entry; i++) {
144 0aec3c5c Baptiste Coudurier
            for ( j=0; j<track->cluster[i].entries; j++) {
145
                put_be32(pb, track->cluster[i].size /
146
                         track->cluster[i].entries);
147 e45ccf79 Gildas Bazin
            }
148 1cb5f7fd Michael Niedermayer
        }
149
    }
150 f578f938 Thomas Raivio
    return updateSize (pb, pos);
151 1cb5f7fd Michael Niedermayer
}
152
153 e45ccf79 Gildas Bazin
/* Sample to chunk atom */
154 6e6d6dc0 Michael Niedermayer
static int mov_write_stsc_tag(ByteIOContext *pb, MOVTrack* track)
155 1cb5f7fd Michael Niedermayer
{
156 b29af723 Michael Niedermayer
    int index = 0, oldval = -1, i;
157
    offset_t entryPos, curpos;
158 f578f938 Thomas Raivio
159 b29af723 Michael Niedermayer
    offset_t pos = url_ftell(pb);
160 f578f938 Thomas Raivio
    put_be32(pb, 0); /* size */
161 1cb5f7fd Michael Niedermayer
    put_tag(pb, "stsc");
162 115329f1 Diego Biurrun
    put_be32(pb, 0); // version & flags
163 f578f938 Thomas Raivio
    entryPos = url_ftell(pb);
164 115329f1 Diego Biurrun
    put_be32(pb, track->entry); // entry count
165 f578f938 Thomas Raivio
    for (i=0; i<track->entry; i++) {
166 0aec3c5c Baptiste Coudurier
        if(oldval != track->cluster[i].samplesInChunk)
167 f578f938 Thomas Raivio
        {
168 115329f1 Diego Biurrun
            put_be32(pb, i+1); // first chunk
169 0aec3c5c Baptiste Coudurier
            put_be32(pb, track->cluster[i].samplesInChunk); // samples per chunk
170 115329f1 Diego Biurrun
            put_be32(pb, 0x1); // sample description index
171 0aec3c5c Baptiste Coudurier
            oldval = track->cluster[i].samplesInChunk;
172 f578f938 Thomas Raivio
            index++;
173 1cb5f7fd Michael Niedermayer
        }
174
    }
175 f578f938 Thomas Raivio
    curpos = url_ftell(pb);
176
    url_fseek(pb, entryPos, SEEK_SET);
177 115329f1 Diego Biurrun
    put_be32(pb, index); // rewrite size
178 f578f938 Thomas Raivio
    url_fseek(pb, curpos, SEEK_SET);
179 1cb5f7fd Michael Niedermayer
180 f578f938 Thomas Raivio
    return updateSize (pb, pos);
181 1cb5f7fd Michael Niedermayer
}
182
183 e45ccf79 Gildas Bazin
/* Sync sample atom */
184 f578f938 Thomas Raivio
static int mov_write_stss_tag(ByteIOContext *pb, MOVTrack* track)
185 1cb5f7fd Michael Niedermayer
{
186 b29af723 Michael Niedermayer
    offset_t curpos, entryPos;
187
    int i, index = 0;
188
    offset_t pos = url_ftell(pb);
189 115329f1 Diego Biurrun
    put_be32(pb, 0); // size
190 1cb5f7fd Michael Niedermayer
    put_tag(pb, "stss");
191 115329f1 Diego Biurrun
    put_be32(pb, 0); // version & flags
192 f578f938 Thomas Raivio
    entryPos = url_ftell(pb);
193 115329f1 Diego Biurrun
    put_be32(pb, track->entry); // entry count
194 f578f938 Thomas Raivio
    for (i=0; i<track->entry; i++) {
195 0aec3c5c Baptiste Coudurier
        if(track->cluster[i].key_frame == 1) {
196 f578f938 Thomas Raivio
            put_be32(pb, i+1);
197
            index++;
198
        }
199
    }
200
    curpos = url_ftell(pb);
201
    url_fseek(pb, entryPos, SEEK_SET);
202 115329f1 Diego Biurrun
    put_be32(pb, index); // rewrite size
203 f578f938 Thomas Raivio
    url_fseek(pb, curpos, SEEK_SET);
204
    return updateSize (pb, pos);
205 1cb5f7fd Michael Niedermayer
}
206
207 b7d9da10 Baptiste Coudurier
static int mov_write_amr_tag(ByteIOContext *pb, MOVTrack *track)
208 1cb5f7fd Michael Niedermayer
{
209
    put_be32(pb, 0x11); /* size */
210 b7d9da10 Baptiste Coudurier
    if (track->mode == MODE_MOV) put_tag(pb, "samr");
211
    else                         put_tag(pb, "damr");
212 1cb5f7fd Michael Niedermayer
    put_tag(pb, "FFMP");
213 b7d9da10 Baptiste Coudurier
    put_byte(pb, 0); /* decoder version */
214 3a72cbd9 Baptiste Coudurier
215 875f9287 Baptiste Coudurier
    put_be16(pb, 0x81FF); /* Mode set (all modes for AMR_NB) */
216 b7d9da10 Baptiste Coudurier
    put_byte(pb, 0x00); /* Mode change period (no restriction) */
217
    put_byte(pb, 0x01); /* Frames per sample */
218 3a72cbd9 Baptiste Coudurier
    return 0x11;
219
}
220
221 fcef991a Baptiste Coudurier
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 039627cf Baptiste Coudurier
static unsigned int descrLength(unsigned int len)
230
{
231 08db8f18 Michael Niedermayer
    int i;
232
    for(i=1; len>>(7*i); i++);
233
    return len + 1 + i;
234 039627cf Baptiste Coudurier
}
235
236 08db8f18 Michael Niedermayer
static void putDescr(ByteIOContext *pb, int tag, unsigned int size)
237 039627cf Baptiste Coudurier
{
238 08db8f18 Michael Niedermayer
    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 039627cf Baptiste Coudurier
}
244
245
static int mov_write_esds_tag(ByteIOContext *pb, MOVTrack* track) // Basic
246
{
247
    offset_t pos = url_ftell(pb);
248 fcef991a Baptiste Coudurier
    int decoderSpecificInfoLen = track->vosLen ? descrLength(track->vosLen):0;
249 039627cf Baptiste Coudurier
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 ccba88d7 Baptiste Coudurier
    put_byte(pb, codec_get_tag(ff_mp4_obj_type, track->enc->codec_id));
265 039627cf Baptiste Coudurier
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 69dde1ad Gildas Bazin
static int mov_write_wave_tag(ByteIOContext *pb, MOVTrack* track)
297
{
298 b29af723 Michael Niedermayer
    offset_t pos = url_ftell(pb);
299 69dde1ad Gildas Bazin
300
    put_be32(pb, 0);     /* size */
301
    put_tag(pb, "wave");
302
303
    put_be32(pb, 12);    /* size */
304
    put_tag(pb, "frma");
305 fcef991a Baptiste Coudurier
    put_le32(pb, track->tag);
306 69dde1ad Gildas Bazin
307 fcef991a Baptiste Coudurier
    if (track->enc->codec_id == CODEC_ID_AAC) {
308 aa1c1c61 Baptiste Coudurier
        /* 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 fcef991a Baptiste Coudurier
        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 3a72cbd9 Baptiste Coudurier
    } else if (track->enc->codec_id == CODEC_ID_AMR_NB) {
317 b7d9da10 Baptiste Coudurier
        mov_write_amr_tag(pb, track);
318 fcef991a Baptiste Coudurier
    }
319 69dde1ad Gildas Bazin
320
    put_be32(pb, 8);     /* size */
321
    put_be32(pb, 0);     /* null tag */
322
323
    return updateSize (pb, pos);
324
}
325
326 79e42311 Baptiste Coudurier
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 f578f938 Thomas Raivio
static int mov_write_audio_tag(ByteIOContext *pb, MOVTrack* track)
335 1cb5f7fd Michael Niedermayer
{
336 b29af723 Michael Niedermayer
    offset_t pos = url_ftell(pb);
337 f45b744c Baptiste Coudurier
    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 115329f1 Diego Biurrun
342 1cb5f7fd Michael Niedermayer
    put_be32(pb, 0); /* size */
343 039627cf Baptiste Coudurier
    put_le32(pb, track->tag); // store it byteswapped
344 1cb5f7fd Michael Niedermayer
    put_be32(pb, 0); /* Reserved */
345
    put_be16(pb, 0); /* Reserved */
346
    put_be16(pb, 1); /* Data-reference index, XXX  == 1 */
347 69dde1ad Gildas Bazin
348 e45ccf79 Gildas Bazin
    /* SoundDescription */
349 fcef991a Baptiste Coudurier
    put_be16(pb, version); /* Version */
350 e45ccf79 Gildas Bazin
    put_be16(pb, 0); /* Revision level */
351 1cb5f7fd Michael Niedermayer
    put_be32(pb, 0); /* Reserved */
352
353 2260499c Baptiste Coudurier
    if (track->mode == MODE_MOV) {
354
        put_be16(pb, track->enc->channels);
355 10fc840c Baptiste Coudurier
        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 2260499c Baptiste Coudurier
        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 ef19c7eb Gildas Bazin
367 9a4d9388 Roman Shaposhnik
    put_be16(pb, 0); /* packet size (= 0) */
368 1cb5f7fd Michael Niedermayer
    put_be16(pb, track->timescale); /* Time scale */
369
    put_be16(pb, 0); /* Reserved */
370
371 5cb49ca1 Baptiste Coudurier
    if(version == 1) { /* SoundDescription V1 extended info */
372
        put_be32(pb, track->enc->frame_size); /* Samples per packet */
373 9a39cd35 Michael Niedermayer
        put_be32(pb, track->sampleSize / track->enc->channels); /* Bytes per packet */
374 5cb49ca1 Baptiste Coudurier
        put_be32(pb, track->sampleSize); /* Bytes per frame */
375 aa1c1c61 Baptiste Coudurier
        put_be32(pb, 2); /* Bytes per sample */
376 69dde1ad Gildas Bazin
    }
377
378 6dd19fff Baptiste Coudurier
    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 fcef991a Baptiste Coudurier
        mov_write_wave_tag(pb, track);
384 3a72cbd9 Baptiste Coudurier
    else if(track->enc->codec_id == CODEC_ID_AAC)
385
        mov_write_esds_tag(pb, track);
386
    else if(track->enc->codec_id == CODEC_ID_AMR_NB)
387 b7d9da10 Baptiste Coudurier
        mov_write_amr_tag(pb, track);
388 79e42311 Baptiste Coudurier
    else if(track->vosLen > 0)
389
        mov_write_glbl_tag(pb, track);
390 3a72cbd9 Baptiste Coudurier
391 6e6d6dc0 Michael Niedermayer
    return updateSize (pb, pos);
392 1cb5f7fd Michael Niedermayer
}
393
394 6e6d6dc0 Michael Niedermayer
static int mov_write_d263_tag(ByteIOContext *pb)
395 1cb5f7fd Michael Niedermayer
{
396
    put_be32(pb, 0xf); /* size */
397
    put_tag(pb, "d263");
398
    put_tag(pb, "FFMP");
399 87494ea0 Baptiste Coudurier
    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 1cb5f7fd Michael Niedermayer
    return 0xf;
404
}
405
406 f578f938 Thomas Raivio
/* TODO: No idea about these values */
407
static int mov_write_svq3_tag(ByteIOContext *pb)
408 1cb5f7fd Michael Niedermayer
{
409 f578f938 Thomas Raivio
    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 115329f1 Diego Biurrun
    put_byte(pb, 0);
416 f578f938 Thomas Raivio
    return 0x15;
417 1cb5f7fd Michael Niedermayer
}
418
419 6e6eebf9 Aurelien Jacobs
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 9ab3f71b Aurelien Jacobs
    ff_isom_write_avcc(pb, track->vosData, track->vosLen);
426 c1b8e6d8 Baptiste Coudurier
    return updateSize(pb, pos);
427
}
428
429 fc4cbc16 Baptiste Coudurier
/* 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 44fb8ebb Baptiste Coudurier
        if (track->enc->height == 1080)
464
            put_be32(pb, 5); /* unknown */
465
        else
466
            put_be32(pb, 6); /* unknown */
467 fc4cbc16 Baptiste Coudurier
    }
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 039627cf Baptiste Coudurier
static int mov_find_video_codec_tag(AVFormatContext *s, MOVTrack *track)
478 8e321619 Baptiste Coudurier
{
479 039627cf Baptiste Coudurier
    int tag = track->enc->codec_tag;
480 8e321619 Baptiste Coudurier
    if (!tag) {
481
        if (track->enc->codec_id == CODEC_ID_DVVIDEO) {
482
            if (track->enc->height == 480) { /* NTSC */
483
                if (track->enc->pix_fmt == PIX_FMT_YUV422P)
484
                    tag = MKTAG('d', 'v', '5', 'n');
485
                else
486
                    tag = MKTAG('d', 'v', 'c', ' ');
487
            } else { /* assume PAL */
488
                if (track->enc->pix_fmt == PIX_FMT_YUV422P)
489
                    tag = MKTAG('d', 'v', '5', 'p');
490
                else if (track->enc->pix_fmt == PIX_FMT_YUV420P)
491
                    tag = MKTAG('d', 'v', 'c', 'p');
492 3cdc7eb4 Baptiste Coudurier
                else
493
                    tag = MKTAG('d', 'v', 'p', 'p');
494 8e321619 Baptiste Coudurier
            }
495 2c5ad5fd Baptiste Coudurier
        } else if (track->enc->codec_id == CODEC_ID_H263) {
496
            if (track->mode == MODE_MOV)
497
                tag = MKTAG('h', '2', '6', '3');
498
            else
499
                tag = MKTAG('s', '2', '6', '3');
500 8e321619 Baptiste Coudurier
        } else {
501
            tag = codec_get_tag(codec_movvideo_tags, track->enc->codec_id);
502
        }
503
    }
504
    // if no mac fcc found, try with Microsoft tags
505 039627cf Baptiste Coudurier
    if (!tag) {
506 8e321619 Baptiste Coudurier
        tag = codec_get_tag(codec_bmp_tags, track->enc->codec_id);
507 039627cf Baptiste Coudurier
        if (tag) {
508
            av_log(s, AV_LOG_INFO, "Warning, using MS style video codec tag, the file may be unplayable!\n");
509
        }
510
    }
511
    assert(tag);
512
    return tag;
513
}
514
515
static int mov_find_audio_codec_tag(AVFormatContext *s, MOVTrack *track)
516
{
517
    int tag = track->enc->codec_tag;
518
    if (!tag) {
519
        tag = codec_get_tag(codec_movaudio_tags, track->enc->codec_id);
520
    }
521
    // if no mac fcc found, try with Microsoft tags
522
    if (!tag) {
523
        int ms_tag = codec_get_tag(codec_wav_tags, track->enc->codec_id);
524
        if (ms_tag) {
525
            tag = MKTAG('m', 's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
526
            av_log(s, AV_LOG_INFO, "Warning, using MS style audio codec tag, the file may be unplayable!\n");
527
        }
528
    }
529 8e321619 Baptiste Coudurier
    assert(tag);
530
    return tag;
531
}
532
533 f578f938 Thomas Raivio
static int mov_write_video_tag(ByteIOContext *pb, MOVTrack* track)
534 1cb5f7fd Michael Niedermayer
{
535 b29af723 Michael Niedermayer
    offset_t pos = url_ftell(pb);
536 53ffdd14 Roine Gustafsson
    char compressor_name[32];
537 8cc7a34d Alex Beregszaszi
538 f578f938 Thomas Raivio
    put_be32(pb, 0); /* size */
539 039627cf Baptiste Coudurier
    put_le32(pb, track->tag); // store it byteswapped
540 f578f938 Thomas Raivio
    put_be32(pb, 0); /* Reserved */
541
    put_be16(pb, 0); /* Reserved */
542
    put_be16(pb, 1); /* Data-reference index */
543
544 53ffdd14 Roine Gustafsson
    put_be16(pb, 0); /* Codec stream version */
545
    put_be16(pb, 0); /* Codec stream revision (=0) */
546 5997ed78 Baptiste Coudurier
    if (track->mode == MODE_MOV) {
547 915282e5 Baptiste Coudurier
        put_tag(pb, "FFMP"); /* Vendor */
548
        if(track->enc->codec_id == CODEC_ID_RAWVIDEO) {
549
            put_be32(pb, 0); /* Temporal Quality */
550
            put_be32(pb, 0x400); /* Spatial Quality = lossless*/
551
        } else {
552
            put_be32(pb, 0x200); /* Temporal Quality = normal */
553
            put_be32(pb, 0x200); /* Spatial Quality = normal */
554
        }
555 5997ed78 Baptiste Coudurier
    } else {
556
        put_be32(pb, 0); /* Reserved */
557
        put_be32(pb, 0); /* Reserved */
558
        put_be32(pb, 0); /* Reserved */
559
    }
560 f578f938 Thomas Raivio
    put_be16(pb, track->enc->width); /* Video width */
561
    put_be16(pb, track->enc->height); /* Video height */
562 53ffdd14 Roine Gustafsson
    put_be32(pb, 0x00480000); /* Horizontal resolution 72dpi */
563
    put_be32(pb, 0x00480000); /* Vertical resolution 72dpi */
564 9a4d9388 Roman Shaposhnik
    put_be32(pb, 0); /* Data size (= 0) */
565
    put_be16(pb, 1); /* Frame count (= 1) */
566 115329f1 Diego Biurrun
567 53ffdd14 Roine Gustafsson
    memset(compressor_name,0,32);
568 5997ed78 Baptiste Coudurier
    /* FIXME not sure, ISO 14496-1 draft where it shall be set to 0 */
569
    if (track->mode == MODE_MOV && track->enc->codec && track->enc->codec->name)
570 53ffdd14 Roine Gustafsson
        strncpy(compressor_name,track->enc->codec->name,31);
571 25cf9062 François Revol
    put_byte(pb, strlen(compressor_name));
572 53ffdd14 Roine Gustafsson
    put_buffer(pb, compressor_name, 31);
573 115329f1 Diego Biurrun
574 f578f938 Thomas Raivio
    put_be16(pb, 0x18); /* Reserved */
575
    put_be16(pb, 0xffff); /* Reserved */
576
    if(track->enc->codec_id == CODEC_ID_MPEG4)
577
        mov_write_esds_tag(pb, track);
578
    else if(track->enc->codec_id == CODEC_ID_H263)
579
        mov_write_d263_tag(pb);
580
    else if(track->enc->codec_id == CODEC_ID_SVQ3)
581 115329f1 Diego Biurrun
        mov_write_svq3_tag(pb);
582 c1b8e6d8 Baptiste Coudurier
    else if(track->enc->codec_id == CODEC_ID_H264)
583
        mov_write_avcc_tag(pb, track);
584 fc4cbc16 Baptiste Coudurier
    else if(track->enc->codec_id == CODEC_ID_DNXHD)
585
        mov_write_avid_tag(pb, track);
586 b356f75d Baptiste Coudurier
    else if(track->vosLen > 0)
587 79e42311 Baptiste Coudurier
        mov_write_glbl_tag(pb, track);
588 f578f938 Thomas Raivio
589
    return updateSize (pb, pos);
590 1cb5f7fd Michael Niedermayer
}
591
592 6e6d6dc0 Michael Niedermayer
static int mov_write_stsd_tag(ByteIOContext *pb, MOVTrack* track)
593 1cb5f7fd Michael Niedermayer
{
594 b29af723 Michael Niedermayer
    offset_t pos = url_ftell(pb);
595 1cb5f7fd Michael Niedermayer
    put_be32(pb, 0); /* size */
596
    put_tag(pb, "stsd");
597
    put_be32(pb, 0); /* version & flags */
598
    put_be32(pb, 1); /* entry count */
599 f578f938 Thomas Raivio
    if (track->enc->codec_type == CODEC_TYPE_VIDEO)
600
        mov_write_video_tag(pb, track);
601
    else if (track->enc->codec_type == CODEC_TYPE_AUDIO)
602
        mov_write_audio_tag(pb, track);
603 6e6d6dc0 Michael Niedermayer
    return updateSize(pb, pos);
604 1cb5f7fd Michael Niedermayer
}
605
606 b4712e3c Baptiste Coudurier
static int mov_write_ctts_tag(ByteIOContext *pb, MOVTrack* track)
607
{
608 e23848a4 Baptiste Coudurier
    MOV_stts_t *ctts_entries;
609 b4712e3c Baptiste Coudurier
    uint32_t entries = 0;
610
    uint32_t atom_size;
611
    int i;
612
613
    ctts_entries = av_malloc((track->entry + 1) * sizeof(*ctts_entries)); /* worst case */
614
    ctts_entries[0].count = 1;
615 0aec3c5c Baptiste Coudurier
    ctts_entries[0].duration = track->cluster[0].cts;
616 b4712e3c Baptiste Coudurier
    for (i=1; i<track->entry; i++) {
617 0aec3c5c Baptiste Coudurier
        if (track->cluster[i].cts == ctts_entries[entries].duration) {
618 b4712e3c Baptiste Coudurier
            ctts_entries[entries].count++; /* compress */
619
        } else {
620
            entries++;
621 0aec3c5c Baptiste Coudurier
            ctts_entries[entries].duration = track->cluster[i].cts;
622 b4712e3c Baptiste Coudurier
            ctts_entries[entries].count = 1;
623
        }
624
    }
625
    entries++; /* last one */
626
    atom_size = 16 + (entries * 8);
627
    put_be32(pb, atom_size); /* size */
628
    put_tag(pb, "ctts");
629
    put_be32(pb, 0); /* version & flags */
630
    put_be32(pb, entries); /* entry count */
631
    for (i=0; i<entries; i++) {
632
        put_be32(pb, ctts_entries[i].count);
633
        put_be32(pb, ctts_entries[i].duration);
634
    }
635
    av_free(ctts_entries);
636
    return atom_size;
637
}
638
639 e45ccf79 Gildas Bazin
/* Time to sample atom */
640 6e6d6dc0 Michael Niedermayer
static int mov_write_stts_tag(ByteIOContext *pb, MOVTrack* track)
641 1cb5f7fd Michael Niedermayer
{
642 e23848a4 Baptiste Coudurier
    MOV_stts_t *stts_entries;
643 aa90239f Baptiste Coudurier
    uint32_t entries = -1;
644
    uint32_t atom_size;
645
    int i;
646
647
    if (track->enc->codec_type == CODEC_TYPE_AUDIO && !track->audio_vbr) {
648
        stts_entries = av_malloc(sizeof(*stts_entries)); /* one entry */
649
        stts_entries[0].count = track->sampleCount;
650
        stts_entries[0].duration = 1;
651
        entries = 1;
652
    } else {
653
        stts_entries = av_malloc(track->entry * sizeof(*stts_entries)); /* worst case */
654
        for (i=0; i<track->entry; i++) {
655
            int64_t duration = i + 1 == track->entry ?
656
                track->trackDuration - track->cluster[i].dts + track->cluster[0].dts : /* readjusting */
657
                track->cluster[i+1].dts - track->cluster[i].dts;
658
            if (i && duration == stts_entries[entries].duration) {
659
                stts_entries[entries].count++; /* compress */
660
            } else {
661
                entries++;
662
                stts_entries[entries].duration = duration;
663
                stts_entries[entries].count = 1;
664
            }
665
        }
666
        entries++; /* last one */
667
    }
668
    atom_size = 16 + (entries * 8);
669
    put_be32(pb, atom_size); /* size */
670 1cb5f7fd Michael Niedermayer
    put_tag(pb, "stts");
671
    put_be32(pb, 0); /* version & flags */
672 aa90239f Baptiste Coudurier
    put_be32(pb, entries); /* entry count */
673
    for (i=0; i<entries; i++) {
674
        put_be32(pb, stts_entries[i].count);
675
        put_be32(pb, stts_entries[i].duration);
676
    }
677
    av_free(stts_entries);
678
    return atom_size;
679 1cb5f7fd Michael Niedermayer
}
680
681 6e6d6dc0 Michael Niedermayer
static int mov_write_dref_tag(ByteIOContext *pb)
682 1cb5f7fd Michael Niedermayer
{
683
    put_be32(pb, 28); /* size */
684
    put_tag(pb, "dref");
685
    put_be32(pb, 0); /* version & flags */
686
    put_be32(pb, 1); /* entry count */
687
688
    put_be32(pb, 0xc); /* size */
689
    put_tag(pb, "url ");
690
    put_be32(pb, 1); /* version & flags */
691
692
    return 28;
693
}
694
695 6e6d6dc0 Michael Niedermayer
static int mov_write_stbl_tag(ByteIOContext *pb, MOVTrack* track)
696 1cb5f7fd Michael Niedermayer
{
697 b29af723 Michael Niedermayer
    offset_t pos = url_ftell(pb);
698 1cb5f7fd Michael Niedermayer
    put_be32(pb, 0); /* size */
699
    put_tag(pb, "stbl");
700 6e6d6dc0 Michael Niedermayer
    mov_write_stsd_tag(pb, track);
701
    mov_write_stts_tag(pb, track);
702 f578f938 Thomas Raivio
    if (track->enc->codec_type == CODEC_TYPE_VIDEO &&
703 32ba6fb1 Baptiste Coudurier
        track->hasKeyframes < track->entry)
704 f578f938 Thomas Raivio
        mov_write_stss_tag(pb, track);
705 b4712e3c Baptiste Coudurier
    if (track->enc->codec_type == CODEC_TYPE_VIDEO &&
706
        track->hasBframes)
707
        mov_write_ctts_tag(pb, track);
708 6e6d6dc0 Michael Niedermayer
    mov_write_stsc_tag(pb, track);
709
    mov_write_stsz_tag(pb, track);
710
    mov_write_stco_tag(pb, track);
711
    return updateSize(pb, pos);
712 1cb5f7fd Michael Niedermayer
}
713
714 6e6d6dc0 Michael Niedermayer
static int mov_write_dinf_tag(ByteIOContext *pb)
715 1cb5f7fd Michael Niedermayer
{
716 b29af723 Michael Niedermayer
    offset_t pos = url_ftell(pb);
717 1cb5f7fd Michael Niedermayer
    put_be32(pb, 0); /* size */
718
    put_tag(pb, "dinf");
719 6e6d6dc0 Michael Niedermayer
    mov_write_dref_tag(pb);
720
    return updateSize(pb, pos);
721 1cb5f7fd Michael Niedermayer
}
722
723 6e6d6dc0 Michael Niedermayer
static int mov_write_smhd_tag(ByteIOContext *pb)
724 1cb5f7fd Michael Niedermayer
{
725
    put_be32(pb, 16); /* size */
726
    put_tag(pb, "smhd");
727
    put_be32(pb, 0); /* version & flags */
728
    put_be16(pb, 0); /* reserved (balance, normally = 0) */
729
    put_be16(pb, 0); /* reserved */
730
    return 16;
731
}
732
733 6e6d6dc0 Michael Niedermayer
static int mov_write_vmhd_tag(ByteIOContext *pb)
734 1cb5f7fd Michael Niedermayer
{
735
    put_be32(pb, 0x14); /* size (always 0x14) */
736
    put_tag(pb, "vmhd");
737
    put_be32(pb, 0x01); /* version & flags */
738
    put_be64(pb, 0); /* reserved (graphics mode = copy) */
739
    return 0x14;
740
}
741
742 6e6d6dc0 Michael Niedermayer
static int mov_write_hdlr_tag(ByteIOContext *pb, MOVTrack* track)
743 1cb5f7fd Michael Niedermayer
{
744 a97c1e93 Stefan Huehner
    const char *descr, *hdlr, *hdlr_type;
745 b29af723 Michael Niedermayer
    offset_t pos = url_ftell(pb);
746 115329f1 Diego Biurrun
747 9a4d9388 Roman Shaposhnik
    if (!track) { /* no media --> data handler */
748 bb270c08 Diego Biurrun
        hdlr = "dhlr";
749
        hdlr_type = "url ";
750
        descr = "DataHandler";
751 9a4d9388 Roman Shaposhnik
    } else {
752 bb270c08 Diego Biurrun
        hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
753
        if (track->enc->codec_type == CODEC_TYPE_VIDEO) {
754
            hdlr_type = "vide";
755
            descr = "VideoHandler";
756
        } else {
757
            hdlr_type = "soun";
758
            descr = "SoundHandler";
759
        }
760 9a4d9388 Roman Shaposhnik
    }
761 115329f1 Diego Biurrun
762 f578f938 Thomas Raivio
    put_be32(pb, 0); /* size */
763 1cb5f7fd Michael Niedermayer
    put_tag(pb, "hdlr");
764
    put_be32(pb, 0); /* Version & flags */
765 906b578f Roman Shaposhnik
    put_buffer(pb, hdlr, 4); /* handler */
766 9a4d9388 Roman Shaposhnik
    put_tag(pb, hdlr_type); /* handler type */
767 f578f938 Thomas Raivio
    put_be32(pb ,0); /* reserved */
768
    put_be32(pb ,0); /* reserved */
769
    put_be32(pb ,0); /* reserved */
770 9a4d9388 Roman Shaposhnik
    put_byte(pb, strlen(descr)); /* string counter */
771
    put_buffer(pb, descr, strlen(descr)); /* handler description */
772
    return updateSize(pb, pos);
773
}
774
775
static int mov_write_minf_tag(ByteIOContext *pb, MOVTrack* track)
776
{
777 b29af723 Michael Niedermayer
    offset_t pos = url_ftell(pb);
778 9a4d9388 Roman Shaposhnik
    put_be32(pb, 0); /* size */
779
    put_tag(pb, "minf");
780 1cb5f7fd Michael Niedermayer
    if(track->enc->codec_type == CODEC_TYPE_VIDEO)
781 9a4d9388 Roman Shaposhnik
        mov_write_vmhd_tag(pb);
782 1cb5f7fd Michael Niedermayer
    else
783 9a4d9388 Roman Shaposhnik
        mov_write_smhd_tag(pb);
784
    if (track->mode == MODE_MOV) /* FIXME: Why do it for MODE_MOV only ? */
785
        mov_write_hdlr_tag(pb, NULL);
786
    mov_write_dinf_tag(pb);
787
    mov_write_stbl_tag(pb, track);
788 f578f938 Thomas Raivio
    return updateSize(pb, pos);
789 1cb5f7fd Michael Niedermayer
}
790
791 6e6d6dc0 Michael Niedermayer
static int mov_write_mdhd_tag(ByteIOContext *pb, MOVTrack* track)
792 1cb5f7fd Michael Niedermayer
{
793 a3a80ddc Baptiste Coudurier
    int version = track->trackDuration < INT32_MAX ? 0 : 1;
794
795
    (version == 1) ? put_be32(pb, 44) : put_be32(pb, 32); /* size */
796 1cb5f7fd Michael Niedermayer
    put_tag(pb, "mdhd");
797 a3a80ddc Baptiste Coudurier
    put_byte(pb, version);
798
    put_be24(pb, 0); /* flags */
799
    if (version == 1) {
800
        put_be64(pb, track->time);
801
        put_be64(pb, track->time);
802
    } else {
803
        put_be32(pb, track->time); /* creation time */
804
        put_be32(pb, track->time); /* modification time */
805
    }
806 115329f1 Diego Biurrun
    put_be32(pb, track->timescale); /* time scale (sample rate for audio) */
807 a3a80ddc Baptiste Coudurier
    (version == 1) ? put_be64(pb, track->trackDuration) : put_be32(pb, track->trackDuration); /* duration */
808 ab561df9 François Revol
    put_be16(pb, track->language); /* language */
809 1cb5f7fd Michael Niedermayer
    put_be16(pb, 0); /* reserved (quality) */
810 db8f4a92 Michael Niedermayer
811
    if(version!=0 && track->mode == MODE_MOV){
812
        av_log(NULL, AV_LOG_ERROR,
813
            "FATAL error, file duration too long for timebase, this file will not be\n"
814 b1b64c23 Aurelien Jacobs
            "playable with quicktime. Choose a different timebase or a different\n"
815 db8f4a92 Michael Niedermayer
            "container format\n");
816
    }
817
818 1cb5f7fd Michael Niedermayer
    return 32;
819
}
820
821 6e6d6dc0 Michael Niedermayer
static int mov_write_mdia_tag(ByteIOContext *pb, MOVTrack* track)
822 1cb5f7fd Michael Niedermayer
{
823 b29af723 Michael Niedermayer
    offset_t pos = url_ftell(pb);
824 1cb5f7fd Michael Niedermayer
    put_be32(pb, 0); /* size */
825
    put_tag(pb, "mdia");
826 6e6d6dc0 Michael Niedermayer
    mov_write_mdhd_tag(pb, track);
827
    mov_write_hdlr_tag(pb, track);
828
    mov_write_minf_tag(pb, track);
829
    return updateSize(pb, pos);
830 1cb5f7fd Michael Niedermayer
}
831
832 6e6d6dc0 Michael Niedermayer
static int mov_write_tkhd_tag(ByteIOContext *pb, MOVTrack* track)
833 1cb5f7fd Michael Niedermayer
{
834 a3a80ddc Baptiste Coudurier
    int64_t duration = av_rescale_rnd(track->trackDuration, globalTimescale, track->timescale, AV_ROUND_UP);
835
    int version = duration < INT32_MAX ? 0 : 1;
836
837
    (version == 1) ? put_be32(pb, 104) : put_be32(pb, 92); /* size */
838 1cb5f7fd Michael Niedermayer
    put_tag(pb, "tkhd");
839 a3a80ddc Baptiste Coudurier
    put_byte(pb, version);
840
    put_be24(pb, 0xf); /* flags (track enabled) */
841
    if (version == 1) {
842
        put_be64(pb, track->time);
843
        put_be64(pb, track->time);
844
    } else {
845
        put_be32(pb, track->time); /* creation time */
846
        put_be32(pb, track->time); /* modification time */
847
    }
848 1cb5f7fd Michael Niedermayer
    put_be32(pb, track->trackID); /* track-id */
849
    put_be32(pb, 0); /* reserved */
850 a3a80ddc Baptiste Coudurier
    (version == 1) ? put_be64(pb, duration) : put_be32(pb, duration);
851 1cb5f7fd Michael Niedermayer
852
    put_be32(pb, 0); /* reserved */
853
    put_be32(pb, 0); /* reserved */
854
    put_be32(pb, 0x0); /* reserved (Layer & Alternate group) */
855
    /* Volume, only for audio */
856
    if(track->enc->codec_type == CODEC_TYPE_AUDIO)
857
        put_be16(pb, 0x0100);
858
    else
859
        put_be16(pb, 0);
860
    put_be16(pb, 0); /* reserved */
861
862
    /* Matrix structure */
863
    put_be32(pb, 0x00010000); /* reserved */
864
    put_be32(pb, 0x0); /* reserved */
865
    put_be32(pb, 0x0); /* reserved */
866
    put_be32(pb, 0x0); /* reserved */
867
    put_be32(pb, 0x00010000); /* reserved */
868
    put_be32(pb, 0x0); /* reserved */
869
    put_be32(pb, 0x0); /* reserved */
870
    put_be32(pb, 0x0); /* reserved */
871
    put_be32(pb, 0x40000000); /* reserved */
872
873
    /* Track width and height, for visual only */
874
    if(track->enc->codec_type == CODEC_TYPE_VIDEO) {
875 69dde1ad Gildas Bazin
        double sample_aspect_ratio = av_q2d(track->enc->sample_aspect_ratio);
876
        if( !sample_aspect_ratio ) sample_aspect_ratio = 1;
877
        put_be32(pb, sample_aspect_ratio * track->enc->width*0x10000);
878 f578f938 Thomas Raivio
        put_be32(pb, track->enc->height*0x10000);
879 1cb5f7fd Michael Niedermayer
    }
880
    else {
881
        put_be32(pb, 0);
882
        put_be32(pb, 0);
883
    }
884
    return 0x5c;
885
}
886
887 8af18154 tjcannell@blueyonder.co.uk
// This box seems important for the psp playback ... without it the movie seems to hang
888
static int mov_write_edts_tag(ByteIOContext *pb, MOVTrack *track)
889
{
890
    put_be32(pb, 0x24); /* size  */
891
    put_tag(pb, "edts");
892
    put_be32(pb, 0x1c); /* size  */
893
    put_tag(pb, "elst");
894
    put_be32(pb, 0x0);
895
    put_be32(pb, 0x1);
896
897 83fa2ef8 Michael Niedermayer
    put_be32(pb, av_rescale_rnd(track->trackDuration, globalTimescale, track->timescale, AV_ROUND_UP)); /* duration   ... doesn't seem to effect psp */
898 8af18154 tjcannell@blueyonder.co.uk
899 72289140 Baptiste Coudurier
    put_be32(pb, track->cluster[0].cts); /* first pts is cts since dts is 0 */
900 8af18154 tjcannell@blueyonder.co.uk
    put_be32(pb, 0x00010000);
901
    return 0x24;
902
}
903
904
// goes at the end of each track!  ... Critical for PSP playback ("Incompatible data" without it)
905
static int mov_write_uuid_tag_psp(ByteIOContext *pb, MOVTrack *mov)
906
{
907
    put_be32(pb, 0x34); /* size ... reports as 28 in mp4box! */
908
    put_tag(pb, "uuid");
909
    put_tag(pb, "USMT");
910
    put_be32(pb, 0x21d24fce);
911
    put_be32(pb, 0xbb88695c);
912
    put_be32(pb, 0xfac9c740);
913
    put_be32(pb, 0x1c);     // another size here!
914
    put_tag(pb, "MTDT");
915
    put_be32(pb, 0x00010012);
916
    put_be32(pb, 0x0a);
917
    put_be32(pb, 0x55c40000);
918
    put_be32(pb, 0x1);
919
    put_be32(pb, 0x0);
920
    return 0x34;
921
}
922
923 6e6d6dc0 Michael Niedermayer
static int mov_write_trak_tag(ByteIOContext *pb, MOVTrack* track)
924 1cb5f7fd Michael Niedermayer
{
925 b29af723 Michael Niedermayer
    offset_t pos = url_ftell(pb);
926 1cb5f7fd Michael Niedermayer
    put_be32(pb, 0); /* size */
927
    put_tag(pb, "trak");
928 6e6d6dc0 Michael Niedermayer
    mov_write_tkhd_tag(pb, track);
929 c6e722e5 Baptiste Coudurier
    if (track->mode == MODE_PSP || track->hasBframes)
930 8af18154 tjcannell@blueyonder.co.uk
        mov_write_edts_tag(pb, track);  // PSP Movies require edts box
931 6e6d6dc0 Michael Niedermayer
    mov_write_mdia_tag(pb, track);
932 115329f1 Diego Biurrun
    if (track->mode == MODE_PSP)
933 8af18154 tjcannell@blueyonder.co.uk
        mov_write_uuid_tag_psp(pb,track);  // PSP Movies require this uuid box
934 6e6d6dc0 Michael Niedermayer
    return updateSize(pb, pos);
935 1cb5f7fd Michael Niedermayer
}
936
937 88730be6 Måns Rullgård
#if 0
938 1cb5f7fd Michael Niedermayer
/* TODO: Not sorted out, but not necessary either */
939 6e6d6dc0 Michael Niedermayer
static int mov_write_iods_tag(ByteIOContext *pb, MOVContext *mov)
940 1cb5f7fd Michael Niedermayer
{
941
    put_be32(pb, 0x15); /* size */
942
    put_tag(pb, "iods");
943
    put_be32(pb, 0);    /* version & flags */
944
    put_be16(pb, 0x1007);
945
    put_byte(pb, 0);
946
    put_be16(pb, 0x4fff);
947
    put_be16(pb, 0xfffe);
948
    put_be16(pb, 0x01ff);
949
    return 0x15;
950
}
951 88730be6 Måns Rullgård
#endif
952 1cb5f7fd Michael Niedermayer
953 6e6d6dc0 Michael Niedermayer
static int mov_write_mvhd_tag(ByteIOContext *pb, MOVContext *mov)
954 1cb5f7fd Michael Niedermayer
{
955 b29af723 Michael Niedermayer
    int maxTrackID = 1, i;
956
    int64_t maxTrackLenTemp, maxTrackLen = 0;
957 a3a80ddc Baptiste Coudurier
    int version;
958 1cb5f7fd Michael Niedermayer
959 42fb4148 Baptiste Coudurier
    for (i=0; i<mov->nb_streams; i++) {
960 1cb5f7fd Michael Niedermayer
        if(mov->tracks[i].entry > 0) {
961 83fa2ef8 Michael Niedermayer
            maxTrackLenTemp = av_rescale_rnd(mov->tracks[i].trackDuration, globalTimescale, mov->tracks[i].timescale, AV_ROUND_UP);
962 f578f938 Thomas Raivio
            if(maxTrackLen < maxTrackLenTemp)
963
                maxTrackLen = maxTrackLenTemp;
964 1cb5f7fd Michael Niedermayer
            if(maxTrackID < mov->tracks[i].trackID)
965
                maxTrackID = mov->tracks[i].trackID;
966
        }
967
    }
968 a3a80ddc Baptiste Coudurier
969
    version = maxTrackLen < UINT32_MAX ? 0 : 1;
970
    (version == 1) ? put_be32(pb, 120) : put_be32(pb, 108); /* size */
971
    put_tag(pb, "mvhd");
972
    put_byte(pb, version);
973
    put_be24(pb, 0); /* flags */
974
    if (version == 1) {
975
        put_be64(pb, mov->time);
976
        put_be64(pb, mov->time);
977
    } else {
978
        put_be32(pb, mov->time); /* creation time */
979
        put_be32(pb, mov->time); /* modification time */
980
    }
981
    put_be32(pb, mov->timescale); /* timescale */
982
    (version == 1) ? put_be64(pb, maxTrackLen) : put_be32(pb, maxTrackLen); /* duration of longest track */
983 1cb5f7fd Michael Niedermayer
984
    put_be32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
985
    put_be16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
986
    put_be16(pb, 0); /* reserved */
987
    put_be32(pb, 0); /* reserved */
988
    put_be32(pb, 0); /* reserved */
989
990
    /* Matrix structure */
991
    put_be32(pb, 0x00010000); /* reserved */
992
    put_be32(pb, 0x0); /* reserved */
993
    put_be32(pb, 0x0); /* reserved */
994
    put_be32(pb, 0x0); /* reserved */
995
    put_be32(pb, 0x00010000); /* reserved */
996
    put_be32(pb, 0x0); /* reserved */
997
    put_be32(pb, 0x0); /* reserved */
998
    put_be32(pb, 0x0); /* reserved */
999
    put_be32(pb, 0x40000000); /* reserved */
1000
1001
    put_be32(pb, 0); /* reserved (preview time) */
1002
    put_be32(pb, 0); /* reserved (preview duration) */
1003
    put_be32(pb, 0); /* reserved (poster time) */
1004
    put_be32(pb, 0); /* reserved (selection time) */
1005
    put_be32(pb, 0); /* reserved (selection duration) */
1006
    put_be32(pb, 0); /* reserved (current time) */
1007
    put_be32(pb, maxTrackID+1); /* Next track id */
1008
    return 0x6c;
1009
}
1010
1011 b6c50eb1 Patrice Bensoussan
static int mov_write_itunes_hdlr_tag(ByteIOContext *pb, MOVContext* mov,
1012
                                     AVFormatContext *s)
1013
{
1014 b29af723 Michael Niedermayer
    offset_t pos = url_ftell(pb);
1015 b6c50eb1 Patrice Bensoussan
    put_be32(pb, 0); /* size */
1016
    put_tag(pb, "hdlr");
1017
    put_be32(pb, 0);
1018
    put_be32(pb, 0);
1019
    put_tag(pb, "mdir");
1020
    put_tag(pb, "appl");
1021
    put_be32(pb, 0);
1022
    put_be32(pb, 0);
1023
    put_be16(pb, 0);
1024
    return updateSize(pb, pos);
1025
}
1026
1027
/* helper function to write a data tag with the specified string as data */
1028 efda3395 Michael Niedermayer
static int mov_write_string_data_tag(ByteIOContext *pb, const char *data, int long_style)
1029 b6c50eb1 Patrice Bensoussan
{
1030 efda3395 Michael Niedermayer
    if(long_style){
1031 b29af723 Michael Niedermayer
        offset_t pos = url_ftell(pb);
1032 b6c50eb1 Patrice Bensoussan
        put_be32(pb, 0); /* size */
1033 efda3395 Michael Niedermayer
        put_tag(pb, "data");
1034
        put_be32(pb, 1);
1035
        put_be32(pb, 0);
1036
        put_buffer(pb, data, strlen(data));
1037
        return updateSize(pb, pos);
1038
    }else{
1039
        put_be16(pb, strlen(data)); /* string length */
1040
        put_be16(pb, 0);
1041
        put_buffer(pb, data, strlen(data));
1042
        return strlen(data) + 4;
1043 b6c50eb1 Patrice Bensoussan
    }
1044
}
1045
1046 a97c1e93 Stefan Huehner
static int mov_write_string_tag(ByteIOContext *pb, const char *name, const char *value, int long_style){
1047 b6c50eb1 Patrice Bensoussan
    int size = 0;
1048 efda3395 Michael Niedermayer
    if ( value && value[0] ) {
1049 b29af723 Michael Niedermayer
        offset_t pos = url_ftell(pb);
1050 b6c50eb1 Patrice Bensoussan
        put_be32(pb, 0); /* size */
1051 efda3395 Michael Niedermayer
        put_tag(pb, name);
1052
        mov_write_string_data_tag(pb, value, long_style);
1053
        size= updateSize(pb, pos);
1054 b6c50eb1 Patrice Bensoussan
    }
1055
    return size;
1056
}
1057
1058
/* iTunes year */
1059 efda3395 Michael Niedermayer
static int mov_write_day_tag(ByteIOContext *pb, int year, int long_style)
1060 b6c50eb1 Patrice Bensoussan
{
1061 efda3395 Michael Niedermayer
    if(year){
1062
        char year_str[5];
1063
        snprintf(year_str, sizeof(year_str), "%04d", year);
1064
        return mov_write_string_tag(pb, "\251day", year_str, long_style);
1065
    }else
1066
        return 0;
1067 b6c50eb1 Patrice Bensoussan
}
1068
1069
/* iTunes track number */
1070
static int mov_write_trkn_tag(ByteIOContext *pb, MOVContext* mov,
1071
                              AVFormatContext *s)
1072
{
1073
    int size = 0;
1074
    if ( s->track ) {
1075 b29af723 Michael Niedermayer
        offset_t pos = url_ftell(pb);
1076 b6c50eb1 Patrice Bensoussan
        put_be32(pb, 0); /* size */
1077
        put_tag(pb, "trkn");
1078
        {
1079 b29af723 Michael Niedermayer
            offset_t pos = url_ftell(pb);
1080 b6c50eb1 Patrice Bensoussan
            put_be32(pb, 0); /* size */
1081
            put_tag(pb, "data");
1082
            put_be32(pb, 0);        // 8 bytes empty
1083
            put_be32(pb, 0);
1084
            put_be16(pb, 0);        // empty
1085
            put_be16(pb, s->track); // track number
1086
            put_be16(pb, 0);        // total track number
1087
            put_be16(pb, 0);        // empty
1088
            updateSize(pb, pos);
1089
        }
1090
        size = updateSize(pb, pos);
1091
    }
1092
    return size;
1093
}
1094
1095
/* iTunes meta data list */
1096
static int mov_write_ilst_tag(ByteIOContext *pb, MOVContext* mov,
1097
                              AVFormatContext *s)
1098
{
1099 b29af723 Michael Niedermayer
    offset_t pos = url_ftell(pb);
1100 b6c50eb1 Patrice Bensoussan
    put_be32(pb, 0); /* size */
1101
    put_tag(pb, "ilst");
1102 efda3395 Michael Niedermayer
    mov_write_string_tag(pb, "\251nam", s->title         , 1);
1103
    mov_write_string_tag(pb, "\251ART", s->author        , 1);
1104
    mov_write_string_tag(pb, "\251wrt", s->author        , 1);
1105
    mov_write_string_tag(pb, "\251alb", s->album         , 1);
1106
    mov_write_day_tag(pb, s->year ,1);
1107 6f276720 Michael Niedermayer
    if(mov->tracks[0].enc && !(mov->tracks[0].enc->flags & CODEC_FLAG_BITEXACT))
1108
        mov_write_string_tag(pb, "\251too", LIBAVFORMAT_IDENT, 1);
1109 efda3395 Michael Niedermayer
    mov_write_string_tag(pb, "\251cmt", s->comment       , 1);
1110
    mov_write_string_tag(pb, "\251gen", s->genre         , 1);
1111 b6c50eb1 Patrice Bensoussan
    mov_write_trkn_tag(pb, mov, s);
1112
    return updateSize(pb, pos);
1113
}
1114
1115
/* iTunes meta data tag */
1116
static int mov_write_meta_tag(ByteIOContext *pb, MOVContext* mov,
1117
                              AVFormatContext *s)
1118
{
1119
    int size = 0;
1120
1121
    // only save meta tag if required
1122 115329f1 Diego Biurrun
    if ( s->title[0] || s->author[0] || s->album[0] || s->year ||
1123 b6c50eb1 Patrice Bensoussan
         s->comment[0] || s->genre[0] || s->track ) {
1124 b29af723 Michael Niedermayer
        offset_t pos = url_ftell(pb);
1125 b6c50eb1 Patrice Bensoussan
        put_be32(pb, 0); /* size */
1126
        put_tag(pb, "meta");
1127
        put_be32(pb, 0);
1128
        mov_write_itunes_hdlr_tag(pb, mov, s);
1129
        mov_write_ilst_tag(pb, mov, s);
1130
        size = updateSize(pb, pos);
1131
    }
1132
    return size;
1133
}
1134 115329f1 Diego Biurrun
1135 69dde1ad Gildas Bazin
static int mov_write_udta_tag(ByteIOContext *pb, MOVContext* mov,
1136
                              AVFormatContext *s)
1137
{
1138 89a0d790 Benoit Fouet
    int i, req = 0;
1139
1140
    /* Requirements */
1141
    for (i=0; i<mov->nb_streams; i++) {
1142
        if(mov->tracks[i].entry <= 0) continue;
1143
        if (mov->tracks[i].enc->codec_id == CODEC_ID_AAC ||
1144
            mov->tracks[i].enc->codec_id == CODEC_ID_MPEG4) {
1145
            req = 1;
1146
            break;
1147
        }
1148
    }
1149
1150
    if (s->title[0]   || s->author[0] || s->album[0] || s->year ||
1151
        s->comment[0] || s->genre[0]  || s->track ||
1152
       (mov->mode == MODE_MOV &&
1153
      ((mov->tracks[0].enc && !mov->tracks[0].enc->flags & CODEC_FLAG_BITEXACT) || req))) {
1154 cfac9ec7 Benoit Fouet
        offset_t pos = url_ftell(pb);
1155 69dde1ad Gildas Bazin
1156 cfac9ec7 Benoit Fouet
        put_be32(pb, 0); /* size */
1157
        put_tag(pb, "udta");
1158 69dde1ad Gildas Bazin
1159 cfac9ec7 Benoit Fouet
        /* iTunes meta data */
1160
        mov_write_meta_tag(pb, mov, s);
1161 b6c50eb1 Patrice Bensoussan
1162 cfac9ec7 Benoit Fouet
        if(mov->mode == MODE_MOV){ // the title field breaks gtkpod with mp4 and my suspicion is that stuff is not valid in mp4
1163
            /* Requirements */
1164
            if (req)
1165 00996920 Baptiste Coudurier
                mov_write_string_tag(pb, "\251req", "QuickTime 6.0 or greater", 0);
1166 69dde1ad Gildas Bazin
1167 cfac9ec7 Benoit Fouet
            mov_write_string_tag(pb, "\251nam", s->title         , 0);
1168
            mov_write_string_tag(pb, "\251aut", s->author        , 0);
1169
            mov_write_string_tag(pb, "\251alb", s->album         , 0);
1170
            mov_write_day_tag(pb, s->year, 0);
1171
            if(mov->tracks[0].enc && !(mov->tracks[0].enc->flags & CODEC_FLAG_BITEXACT))
1172
                mov_write_string_tag(pb, "\251enc", LIBAVFORMAT_IDENT, 0);
1173
            mov_write_string_tag(pb, "\251des", s->comment       , 0);
1174
            mov_write_string_tag(pb, "\251gen", s->genre         , 0);
1175
        }
1176 69dde1ad Gildas Bazin
1177 cfac9ec7 Benoit Fouet
        return updateSize(pb, pos);
1178 89a0d790 Benoit Fouet
    }
1179
1180
    return 0;
1181 69dde1ad Gildas Bazin
}
1182
1183 79e5a53c Michael Niedermayer
static int utf8len(uint8_t *b){
1184
    int len=0;
1185
    int val;
1186
    while(*b){
1187
        GET_UTF8(val, *b++, return -1;)
1188
        len++;
1189
    }
1190
    return len;
1191
}
1192 dcfdb046 Benjamin Larsson
1193 79e5a53c Michael Niedermayer
static int ascii_to_wc (ByteIOContext *pb, uint8_t *b)
1194 dcfdb046 Benjamin Larsson
{
1195 79e5a53c Michael Niedermayer
    int val;
1196
    while(*b){
1197
        GET_UTF8(val, *b++, return -1;)
1198
        put_be16(pb, val);
1199 dcfdb046 Benjamin Larsson
    }
1200
    put_be16(pb, 0x00);
1201 79e5a53c Michael Niedermayer
    return 0;
1202 dcfdb046 Benjamin Larsson
}
1203
1204 a97c1e93 Stefan Huehner
static uint16_t language_code (const char *str)
1205 dcfdb046 Benjamin Larsson
{
1206 7cf0e16f Michael Niedermayer
    return ((((str[0]-0x60) & 0x1F)<<10) + (((str[1]-0x60) & 0x1F)<<5) + ((str[2]-0x60) & 0x1F));
1207 dcfdb046 Benjamin Larsson
}
1208
1209
static int mov_write_uuidusmt_tag (ByteIOContext *pb, AVFormatContext *s)
1210
{
1211
    size_t len, size;
1212
    offset_t pos, curpos;
1213
1214
    size = 0;
1215
    if (s->title[0]) {
1216
        pos = url_ftell(pb);
1217
        put_be32(pb, 0); /* size placeholder*/
1218
        put_tag(pb, "uuid");
1219
        put_tag(pb, "USMT");
1220
        put_be32(pb, 0x21d24fce ); /* 96 bit UUID */
1221
        put_be32(pb, 0xbb88695c );
1222
        put_be32(pb, 0xfac9c740 );
1223
        size += 24;
1224
1225
        put_be32(pb, 0); /* size placeholder*/
1226
        put_tag(pb, "MTDT");
1227 7cf0e16f Michael Niedermayer
        put_be16(pb, 4);
1228 dcfdb046 Benjamin Larsson
        size += 10;
1229
1230 7cf0e16f Michael Niedermayer
        // ?
1231
        put_be16(pb, 0x0C);                 /* size */
1232
        put_be32(pb, 0x0B);                 /* type */
1233
        put_be16(pb, language_code("und")); /* language */
1234
        put_be16(pb, 0x0);                  /* ? */
1235
        put_be16(pb, 0x021C);               /* data */
1236
        size += 12;
1237
1238
        // Encoder
1239 79e5a53c Michael Niedermayer
        len = utf8len(LIBAVCODEC_IDENT)+1;
1240
        if(len<=0)
1241
            goto not_utf8;
1242 7cf0e16f Michael Niedermayer
        put_be16(pb, len*2+10);             /* size */
1243
        put_be32(pb, 0x04);                 /* type */
1244
        put_be16(pb, language_code("eng")); /* language */
1245
        put_be16(pb, 0x01);                 /* ? */
1246 79e5a53c Michael Niedermayer
        ascii_to_wc(pb, LIBAVCODEC_IDENT);
1247 7cf0e16f Michael Niedermayer
        size += len*2+10;
1248
1249 dcfdb046 Benjamin Larsson
        // Title
1250 79e5a53c Michael Niedermayer
        len = utf8len(s->title)+1;
1251
        if(len<=0)
1252
            goto not_utf8;
1253 dcfdb046 Benjamin Larsson
        put_be16(pb, len*2+10);             /* size */
1254
        put_be32(pb, 0x01);                 /* type */
1255 7cf0e16f Michael Niedermayer
        put_be16(pb, language_code("eng")); /* language */
1256 dcfdb046 Benjamin Larsson
        put_be16(pb, 0x01);                 /* ? */
1257 79e5a53c Michael Niedermayer
        ascii_to_wc (pb, s->title);
1258 dcfdb046 Benjamin Larsson
        size += len*2+10;
1259
1260 7cf0e16f Michael Niedermayer
        // Date
1261
//        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);
1262 79e5a53c Michael Niedermayer
        len = utf8len("2006/04/01 11:11:11")+1;
1263
        if(len<=0)
1264
            goto not_utf8;
1265 7cf0e16f Michael Niedermayer
        put_be16(pb, len*2+10);    /* size */
1266
        put_be32(pb, 0x03);        /* type */
1267
        put_be16(pb, language_code("und")); /* language */
1268
        put_be16(pb, 0x01);        /* ? */
1269 79e5a53c Michael Niedermayer
        ascii_to_wc (pb, "2006/04/01 11:11:11");
1270 7cf0e16f Michael Niedermayer
        size += len*2+10;
1271
1272 dcfdb046 Benjamin Larsson
        // size
1273
        curpos = url_ftell(pb);
1274
        url_fseek(pb, pos, SEEK_SET);
1275
        put_be32(pb, size);
1276
        url_fseek(pb, pos+24, SEEK_SET);
1277
        put_be32(pb, size-24);
1278
        url_fseek(pb, curpos, SEEK_SET);
1279
    }
1280
1281
    return size;
1282 79e5a53c Michael Niedermayer
not_utf8:
1283
    av_log(s, AV_LOG_ERROR, "not utf8\n");
1284
    return -1;
1285 dcfdb046 Benjamin Larsson
}
1286
1287 69dde1ad Gildas Bazin
static int mov_write_moov_tag(ByteIOContext *pb, MOVContext *mov,
1288
                              AVFormatContext *s)
1289 1cb5f7fd Michael Niedermayer
{
1290 b29af723 Michael Niedermayer
    int i;
1291
    offset_t pos = url_ftell(pb);
1292 1cb5f7fd Michael Niedermayer
    put_be32(pb, 0); /* size placeholder*/
1293
    put_tag(pb, "moov");
1294
    mov->timescale = globalTimescale;
1295
1296 42fb4148 Baptiste Coudurier
    for (i=0; i<mov->nb_streams; i++) {
1297 e45ccf79 Gildas Bazin
        if(mov->tracks[i].entry <= 0) continue;
1298
1299
        mov->tracks[i].time = mov->time;
1300
        mov->tracks[i].trackID = i+1;
1301 1cb5f7fd Michael Niedermayer
    }
1302
1303 6e6d6dc0 Michael Niedermayer
    mov_write_mvhd_tag(pb, mov);
1304
    //mov_write_iods_tag(pb, mov);
1305 42fb4148 Baptiste Coudurier
    for (i=0; i<mov->nb_streams; i++) {
1306 1cb5f7fd Michael Niedermayer
        if(mov->tracks[i].entry > 0) {
1307 6e6d6dc0 Michael Niedermayer
            mov_write_trak_tag(pb, &(mov->tracks[i]));
1308 1cb5f7fd Michael Niedermayer
        }
1309
    }
1310
1311 dcfdb046 Benjamin Larsson
    if (mov->mode == MODE_PSP)
1312
        mov_write_uuidusmt_tag(pb, s);
1313 86161cec Baptiste Coudurier
    else if (mov->mode != MODE_3GP && mov->mode != MODE_3G2)
1314 e344c1ea Steve L'Homme
        mov_write_udta_tag(pb, mov, s);
1315 69dde1ad Gildas Bazin
1316 6e6d6dc0 Michael Niedermayer
    return updateSize(pb, pos);
1317 1cb5f7fd Michael Niedermayer
}
1318
1319 7b49ce2e Stefan Huehner
static int mov_write_mdat_tag(ByteIOContext *pb, MOVContext* mov)
1320 1cb5f7fd Michael Niedermayer
{
1321 b29af723 Michael Niedermayer
    put_be32(pb, 8);    // placeholder for extended size field (64 bit)
1322 5340a0bc Baptiste Coudurier
    put_tag(pb, mov->mode == MODE_MOV ? "wide" : "free");
1323 b29af723 Michael Niedermayer
1324 115329f1 Diego Biurrun
    mov->mdat_pos = url_ftell(pb);
1325 1cb5f7fd Michael Niedermayer
    put_be32(pb, 0); /* size placeholder*/
1326
    put_tag(pb, "mdat");
1327
    return 0;
1328
}
1329
1330
/* TODO: This needs to be more general */
1331 dcfdb046 Benjamin Larsson
static void mov_write_ftyp_tag (ByteIOContext *pb, AVFormatContext *s)
1332 1cb5f7fd Michael Niedermayer
{
1333 69dde1ad Gildas Bazin
    MOVContext *mov = s->priv_data;
1334
1335 1cb5f7fd Michael Niedermayer
    put_be32(pb, 0x14 ); /* size */
1336
    put_tag(pb, "ftyp");
1337 e45ccf79 Gildas Bazin
1338 69dde1ad Gildas Bazin
    if ( mov->mode == MODE_3GP )
1339 e45ccf79 Gildas Bazin
        put_tag(pb, "3gp4");
1340 8536ab89 tjraivio@cc.hut.fi
    else if ( mov->mode == MODE_3G2 )
1341
        put_tag(pb, "3g2a");
1342 8af18154 tjcannell@blueyonder.co.uk
    else if ( mov->mode == MODE_PSP )
1343
        put_tag(pb, "MSNV");
1344 26f86eaf Baptiste Coudurier
    else if ( mov->mode == MODE_MP4 )
1345 e45ccf79 Gildas Bazin
        put_tag(pb, "isom");
1346 26f86eaf Baptiste Coudurier
    else
1347
        put_tag(pb, "qt  ");
1348 e45ccf79 Gildas Bazin
1349 1cb5f7fd Michael Niedermayer
    put_be32(pb, 0x200 );
1350 e45ccf79 Gildas Bazin
1351 69dde1ad Gildas Bazin
    if ( mov->mode == MODE_3GP )
1352 e45ccf79 Gildas Bazin
        put_tag(pb, "3gp4");
1353 8536ab89 tjraivio@cc.hut.fi
    else if ( mov->mode == MODE_3G2 )
1354
        put_tag(pb, "3g2a");
1355 8af18154 tjcannell@blueyonder.co.uk
    else if ( mov->mode == MODE_PSP )
1356
        put_tag(pb, "MSNV");
1357 26f86eaf Baptiste Coudurier
    else if ( mov->mode == MODE_MP4 )
1358 e45ccf79 Gildas Bazin
        put_tag(pb, "mp41");
1359 26f86eaf Baptiste Coudurier
    else
1360
        put_tag(pb, "qt  ");
1361 1cb5f7fd Michael Niedermayer
}
1362
1363 0c716ab7 François Revol
static void mov_write_uuidprof_tag(ByteIOContext *pb, AVFormatContext *s)
1364 8af18154 tjcannell@blueyonder.co.uk
{
1365 dcfdb046 Benjamin Larsson
    AVCodecContext *VideoCodec = s->streams[0]->codec;
1366
    AVCodecContext *AudioCodec = s->streams[1]->codec;
1367
    int AudioRate = AudioCodec->sample_rate;
1368
    int FrameRate = ((VideoCodec->time_base.den) * (0x10000))/ (VideoCodec->time_base.num);
1369 9e282ba3 Michael Niedermayer
    int audio_kbitrate= AudioCodec->bit_rate / 1000;
1370
    int video_kbitrate= FFMIN(VideoCodec->bit_rate / 1000, 800 - audio_kbitrate);
1371 8af18154 tjcannell@blueyonder.co.uk
1372
    put_be32(pb, 0x94 ); /* size */
1373
    put_tag(pb, "uuid");
1374
    put_tag(pb, "PROF");
1375
1376
    put_be32(pb, 0x21d24fce ); /* 96 bit UUID */
1377
    put_be32(pb, 0xbb88695c );
1378
    put_be32(pb, 0xfac9c740 );
1379
1380
    put_be32(pb, 0x0 );  /* ? */
1381
    put_be32(pb, 0x3 );  /* 3 sections ? */
1382
1383
    put_be32(pb, 0x14 ); /* size */
1384
    put_tag(pb, "FPRF");
1385
    put_be32(pb, 0x0 );  /* ? */
1386
    put_be32(pb, 0x0 );  /* ? */
1387
    put_be32(pb, 0x0 );  /* ? */
1388
1389
    put_be32(pb, 0x2c );  /* size */
1390
    put_tag(pb, "APRF");   /* audio */
1391
    put_be32(pb, 0x0 );
1392 dcfdb046 Benjamin Larsson
    put_be32(pb, 0x2 );   /* TrackID */
1393 8af18154 tjcannell@blueyonder.co.uk
    put_tag(pb, "mp4a");
1394
    put_be32(pb, 0x20f );
1395
    put_be32(pb, 0x0 );
1396 9e282ba3 Michael Niedermayer
    put_be32(pb, audio_kbitrate);
1397
    put_be32(pb, audio_kbitrate);
1398 dcfdb046 Benjamin Larsson
    put_be32(pb, AudioRate );
1399
    put_be32(pb, AudioCodec->channels );
1400 8af18154 tjcannell@blueyonder.co.uk
1401
    put_be32(pb, 0x34 );  /* size */
1402
    put_tag(pb, "VPRF");   /* video */
1403
    put_be32(pb, 0x0 );
1404 dcfdb046 Benjamin Larsson
    put_be32(pb, 0x1 );    /* TrackID */
1405 87b041e0 Michael Niedermayer
    if (VideoCodec->codec_id == CODEC_ID_H264) {
1406
        put_tag(pb, "avc1");
1407
        put_be16(pb, 0x014D );
1408
        put_be16(pb, 0x0015 );
1409
    } else {
1410
        put_tag(pb, "mp4v");
1411
        put_be16(pb, 0x0000 );
1412
        put_be16(pb, 0x0103 );
1413
    }
1414 8af18154 tjcannell@blueyonder.co.uk
    put_be32(pb, 0x0 );
1415 9e282ba3 Michael Niedermayer
    put_be32(pb, video_kbitrate);
1416
    put_be32(pb, video_kbitrate);
1417 dcfdb046 Benjamin Larsson
    put_be32(pb, FrameRate);
1418
    put_be32(pb, FrameRate);
1419
    put_be16(pb, VideoCodec->width);
1420
    put_be16(pb, VideoCodec->height);
1421
    put_be32(pb, 0x010001); /* ? */
1422 8af18154 tjcannell@blueyonder.co.uk
}
1423
1424 1cb5f7fd Michael Niedermayer
static int mov_write_header(AVFormatContext *s)
1425
{
1426 899681cd Björn Axelsson
    ByteIOContext *pb = s->pb;
1427 69dde1ad Gildas Bazin
    MOVContext *mov = s->priv_data;
1428
    int i;
1429 1cb5f7fd Michael Niedermayer
1430 899681cd Björn Axelsson
    if (url_is_streamed(s->pb)) {
1431 4f60f34a Baptiste Coudurier
        av_log(s, AV_LOG_ERROR, "muxer does not support non seekable output\n");
1432
        return -1;
1433
    }
1434
1435 1522767d Baptiste Coudurier
    /* Default mode == MP4 */
1436
    mov->mode = MODE_MP4;
1437
1438
    if (s->oformat != NULL) {
1439
        if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
1440
        else if (!strcmp("3g2", s->oformat->name)) mov->mode = MODE_3G2;
1441
        else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
1442
        else if (!strcmp("psp", s->oformat->name)) mov->mode = MODE_PSP;
1443
1444 26f86eaf Baptiste Coudurier
        mov_write_ftyp_tag(pb,s);
1445 1522767d Baptiste Coudurier
        if ( mov->mode == MODE_PSP ) {
1446
            if ( s->nb_streams != 2 ) {
1447
                av_log(s, AV_LOG_ERROR, "PSP mode need one video and one audio stream\n");
1448
                return -1;
1449
            }
1450
            mov_write_uuidprof_tag(pb,s);
1451
        }
1452
    }
1453
1454 ffdd57d4 Michael Niedermayer
    for(i=0; i<s->nb_streams; i++){
1455 039627cf Baptiste Coudurier
        AVStream *st= s->streams[i];
1456
        MOVTrack *track= &mov->tracks[i];
1457
1458
        track->enc = st->codec;
1459 2c5ad5fd Baptiste Coudurier
        track->language = ff_mov_iso639_to_lang(st->language, mov->mode != MODE_MOV);
1460
        track->mode = mov->mode;
1461 039627cf Baptiste Coudurier
        if(st->codec->codec_type == CODEC_TYPE_VIDEO){
1462
            track->tag = mov_find_video_codec_tag(s, track);
1463 ffb512e3 Baptiste Coudurier
            track->timescale = st->codec->time_base.den;
1464 039627cf Baptiste Coudurier
            av_set_pts_info(st, 64, 1, st->codec->time_base.den);
1465 911f9b71 Baptiste Coudurier
            if (track->timescale > 100000)
1466
                av_log(NULL, AV_LOG_WARNING,
1467
                       "WARNING codec timebase is very high. If duration is too long,\n"
1468
                       "file may not be playable by quicktime. Specify a shorter timebase\n"
1469
                       "or choose different container.\n");
1470 039627cf Baptiste Coudurier
        }else if(st->codec->codec_type == CODEC_TYPE_AUDIO){
1471
            track->tag = mov_find_audio_codec_tag(s, track);
1472 ffb512e3 Baptiste Coudurier
            track->timescale = st->codec->sample_rate;
1473 039627cf Baptiste Coudurier
            av_set_pts_info(st, 64, 1, st->codec->sample_rate);
1474 4278662d Baptiste Coudurier
            if(!st->codec->frame_size){
1475 b596102e Baptiste Coudurier
                av_log(s, AV_LOG_ERROR, "track %d: codec frame size is not set\n", i);
1476
                return -1;
1477 4278662d Baptiste Coudurier
            }else if(st->codec->frame_size > 1){ /* assume compressed audio */
1478
                track->audio_vbr = 1;
1479
            }else{
1480
                track->sampleSize = (av_get_bits_per_sample(st->codec->codec_id) >> 3) * st->codec->channels;
1481 b596102e Baptiste Coudurier
            }
1482 ffb512e3 Baptiste Coudurier
        }
1483 f578f938 Thomas Raivio
    }
1484
1485 18c697ac Baptiste Coudurier
    mov_write_mdat_tag(pb, mov);
1486
    mov->time = s->timestamp + 0x7C25B080; //1970 based -> 1904 based
1487 42fb4148 Baptiste Coudurier
    mov->nb_streams = s->nb_streams;
1488 18c697ac Baptiste Coudurier
1489 1cb5f7fd Michael Niedermayer
    put_flush_packet(pb);
1490
1491
    return 0;
1492
}
1493
1494 e928649b Michael Niedermayer
static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
1495 1cb5f7fd Michael Niedermayer
{
1496
    MOVContext *mov = s->priv_data;
1497 899681cd Björn Axelsson
    ByteIOContext *pb = s->pb;
1498 039627cf Baptiste Coudurier
    MOVTrack *trk = &mov->tracks[pkt->stream_index];
1499
    AVCodecContext *enc = trk->enc;
1500 e45ccf79 Gildas Bazin
    unsigned int samplesInChunk = 0;
1501 e928649b Michael Niedermayer
    int size= pkt->size;
1502 1cb5f7fd Michael Niedermayer
1503 899681cd Björn Axelsson
    if (url_is_streamed(s->pb)) return 0; /* Can't handle that */
1504 e45ccf79 Gildas Bazin
    if (!size) return 0; /* Discard 0 sized packets */
1505 1cb5f7fd Michael Niedermayer
1506 06fcf56c Baptiste Coudurier
    if (enc->codec_id == CODEC_ID_AMR_NB) {
1507 5cb49ca1 Baptiste Coudurier
        /* We must find out how many AMR blocks there are in one packet */
1508
        static uint16_t packed_size[16] =
1509
            {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 0};
1510
        int len = 0;
1511
1512
        while (len < size && samplesInChunk < 100) {
1513
            len += packed_size[(pkt->data[len] >> 3) & 0x0F];
1514
            samplesInChunk++;
1515 1cb5f7fd Michael Niedermayer
        }
1516 2111e3f9 Michael Niedermayer
        if(samplesInChunk > 1){
1517 89938d29 Ramiro Polla
            av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, implement a AVParser for it\n");
1518 2111e3f9 Michael Niedermayer
            return -1;
1519
        }
1520 5cb49ca1 Baptiste Coudurier
    } else if (trk->sampleSize)
1521
        samplesInChunk = size/trk->sampleSize;
1522
    else
1523 5616f85d Baptiste Coudurier
        samplesInChunk = 1;
1524 e45ccf79 Gildas Bazin
1525 5616f85d Baptiste Coudurier
    /* copy extradata if it exists */
1526
    if (trk->vosLen == 0 && enc->extradata_size > 0) {
1527 e45ccf79 Gildas Bazin
        trk->vosLen = enc->extradata_size;
1528
        trk->vosData = av_malloc(trk->vosLen);
1529
        memcpy(trk->vosData, enc->extradata, trk->vosLen);
1530
    }
1531
1532 5616f85d Baptiste Coudurier
    if (enc->codec_id == CODEC_ID_H264 && trk->vosLen > 0 && *(uint8_t *)trk->vosData != 1) {
1533
        /* from x264 or from bytestream h264 */
1534
        /* nal reformating needed */
1535 9ab3f71b Aurelien Jacobs
        int ret = ff_avc_parse_nal_units(pkt->data, &pkt->data, &pkt->size);
1536 ed7fa39c Aurelien Jacobs
        if (ret < 0)
1537
            return ret;
1538 5616f85d Baptiste Coudurier
        assert(pkt->size);
1539
        size = pkt->size;
1540 fc4cbc16 Baptiste Coudurier
    } else if (enc->codec_id == CODEC_ID_DNXHD && !trk->vosLen) {
1541
        /* copy frame header to create needed atoms */
1542
        if (size < 640)
1543
            return -1;
1544
        trk->vosLen = 640;
1545
        trk->vosData = av_malloc(trk->vosLen);
1546
        memcpy(trk->vosData, pkt->data, 640);
1547 c1b8e6d8 Baptiste Coudurier
    }
1548
1549 0aec3c5c Baptiste Coudurier
    if (!(trk->entry % MOV_INDEX_CLUSTER_SIZE)) {
1550
        trk->cluster = av_realloc(trk->cluster, (trk->entry + MOV_INDEX_CLUSTER_SIZE) * sizeof(*trk->cluster));
1551 e45ccf79 Gildas Bazin
        if (!trk->cluster)
1552
            return -1;
1553
    }
1554
1555 0aec3c5c Baptiste Coudurier
    trk->cluster[trk->entry].pos = url_ftell(pb);
1556
    trk->cluster[trk->entry].samplesInChunk = samplesInChunk;
1557
    trk->cluster[trk->entry].size = size;
1558
    trk->cluster[trk->entry].entries = samplesInChunk;
1559 aa90239f Baptiste Coudurier
    trk->cluster[trk->entry].dts = pkt->dts;
1560
    trk->trackDuration = pkt->dts - trk->cluster[0].dts + pkt->duration;
1561
1562 e45ccf79 Gildas Bazin
    if(enc->codec_type == CODEC_TYPE_VIDEO) {
1563 b4712e3c Baptiste Coudurier
        if (pkt->dts != pkt->pts)
1564
            trk->hasBframes = 1;
1565 0aec3c5c Baptiste Coudurier
        trk->cluster[trk->entry].cts = pkt->pts - pkt->dts;
1566
        trk->cluster[trk->entry].key_frame = !!(pkt->flags & PKT_FLAG_KEY);
1567
        if(trk->cluster[trk->entry].key_frame)
1568 32ba6fb1 Baptiste Coudurier
            trk->hasKeyframes++;
1569 1cb5f7fd Michael Niedermayer
    }
1570 e45ccf79 Gildas Bazin
    trk->entry++;
1571
    trk->sampleCount += samplesInChunk;
1572 039627cf Baptiste Coudurier
    mov->mdat_size += size;
1573 e45ccf79 Gildas Bazin
1574 e928649b Michael Niedermayer
    put_buffer(pb, pkt->data, size);
1575 1cb5f7fd Michael Niedermayer
1576
    put_flush_packet(pb);
1577
    return 0;
1578
}
1579
1580
static int mov_write_trailer(AVFormatContext *s)
1581
{
1582
    MOVContext *mov = s->priv_data;
1583 899681cd Björn Axelsson
    ByteIOContext *pb = s->pb;
1584 1cb5f7fd Michael Niedermayer
    int res = 0;
1585 0aec3c5c Baptiste Coudurier
    int i;
1586 1cb5f7fd Michael Niedermayer
1587 69dde1ad Gildas Bazin
    offset_t moov_pos = url_ftell(pb);
1588 1cb5f7fd Michael Niedermayer
1589
    /* Write size of mdat tag */
1590 039627cf Baptiste Coudurier
    if (mov->mdat_size+8 <= UINT32_MAX) {
1591 b29af723 Michael Niedermayer
        url_fseek(pb, mov->mdat_pos, SEEK_SET);
1592 039627cf Baptiste Coudurier
        put_be32(pb, mov->mdat_size+8);
1593 b29af723 Michael Niedermayer
    } else {
1594
        /* overwrite 'wide' placeholder atom */
1595
        url_fseek(pb, mov->mdat_pos - 8, SEEK_SET);
1596
        put_be32(pb, 1); /* special value: real atom size will be 64 bit value after tag field */
1597
        put_tag(pb, "mdat");
1598 039627cf Baptiste Coudurier
        put_be64(pb, mov->mdat_size+16);
1599 b29af723 Michael Niedermayer
    }
1600 69dde1ad Gildas Bazin
    url_fseek(pb, moov_pos, SEEK_SET);
1601 1cb5f7fd Michael Niedermayer
1602 69dde1ad Gildas Bazin
    mov_write_moov_tag(pb, mov, s);
1603 1cb5f7fd Michael Niedermayer
1604 42fb4148 Baptiste Coudurier
    for (i=0; i<mov->nb_streams; i++) {
1605 0aec3c5c Baptiste Coudurier
        av_freep(&mov->tracks[i].cluster);
1606
1607 ec7d0d2e Gildas Bazin
        if( mov->tracks[i].vosLen ) av_free( mov->tracks[i].vosData );
1608
1609 1cb5f7fd Michael Niedermayer
    }
1610 69dde1ad Gildas Bazin
1611 1cb5f7fd Michael Niedermayer
    put_flush_packet(pb);
1612
1613
    return res;
1614
}
1615
1616 ff70e601 Måns Rullgård
#ifdef CONFIG_MOV_MUXER
1617
AVOutputFormat mov_muxer = {
1618 1cb5f7fd Michael Niedermayer
    "mov",
1619
    "mov format",
1620
    NULL,
1621
    "mov",
1622
    sizeof(MOVContext),
1623 69dde1ad Gildas Bazin
    CODEC_ID_AAC,
1624 2187d948 Michael Niedermayer
    CODEC_ID_MPEG4,
1625 1cb5f7fd Michael Niedermayer
    mov_write_header,
1626
    mov_write_packet,
1627
    mov_write_trailer,
1628 c64d476c Calcium
    .flags = AVFMT_GLOBALHEADER,
1629 1cb5f7fd Michael Niedermayer
};
1630 ff70e601 Måns Rullgård
#endif
1631
#ifdef CONFIG_TGP_MUXER
1632
AVOutputFormat tgp_muxer = {
1633 1cb5f7fd Michael Niedermayer
    "3gp",
1634
    "3gp format",
1635
    NULL,
1636
    "3gp",
1637
    sizeof(MOVContext),
1638
    CODEC_ID_AMR_NB,
1639
    CODEC_ID_H263,
1640
    mov_write_header,
1641
    mov_write_packet,
1642
    mov_write_trailer,
1643 c64d476c Calcium
    .flags = AVFMT_GLOBALHEADER,
1644 1cb5f7fd Michael Niedermayer
};
1645 ff70e601 Måns Rullgård
#endif
1646
#ifdef CONFIG_MP4_MUXER
1647
AVOutputFormat mp4_muxer = {
1648 1cb5f7fd Michael Niedermayer
    "mp4",
1649
    "mp4 format",
1650 4cb3f3b6 Dan Christiansen
    "application/mp4",
1651
    "mp4,m4a",
1652 1cb5f7fd Michael Niedermayer
    sizeof(MOVContext),
1653
    CODEC_ID_AAC,
1654
    CODEC_ID_MPEG4,
1655
    mov_write_header,
1656
    mov_write_packet,
1657
    mov_write_trailer,
1658 c64d476c Calcium
    .flags = AVFMT_GLOBALHEADER,
1659 1cb5f7fd Michael Niedermayer
};
1660 ff70e601 Måns Rullgård
#endif
1661
#ifdef CONFIG_PSP_MUXER
1662
AVOutputFormat psp_muxer = {
1663 8af18154 tjcannell@blueyonder.co.uk
    "psp",
1664
    "psp mp4 format",
1665
    NULL,
1666
    "mp4,psp",
1667
    sizeof(MOVContext),
1668
    CODEC_ID_AAC,
1669
    CODEC_ID_MPEG4,
1670
    mov_write_header,
1671
    mov_write_packet,
1672
    mov_write_trailer,
1673 dcfdb046 Benjamin Larsson
    .flags = AVFMT_GLOBALHEADER,
1674 8af18154 tjcannell@blueyonder.co.uk
};
1675 ff70e601 Måns Rullgård
#endif
1676
#ifdef CONFIG_TG2_MUXER
1677
AVOutputFormat tg2_muxer = {
1678 8536ab89 tjraivio@cc.hut.fi
    "3g2",
1679
    "3gp2 format",
1680
    NULL,
1681
    "3g2",
1682
    sizeof(MOVContext),
1683
    CODEC_ID_AMR_NB,
1684
    CODEC_ID_H263,
1685
    mov_write_header,
1686
    mov_write_packet,
1687
    mov_write_trailer,
1688 c64d476c Calcium
    .flags = AVFMT_GLOBALHEADER,
1689 8536ab89 tjraivio@cc.hut.fi
};
1690 ff70e601 Måns Rullgård
#endif