link fixes
[libav.git] / libavformat / movenc.c
CommitLineData
1cb5f7fd
MN
1/*
2 * MOV, 3GP, MP4 encoder.
3 * Copyright (c) 2003 Thomas Raivio.
69dde1ad 4 * Copyright (c) 2004 Gildas Bazin <gbazin at videolan dot org>.
1cb5f7fd
MN
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
5509bffa 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1cb5f7fd
MN
19 */
20#include "avformat.h"
f578f938 21#include "avi.h"
1cb5f7fd 22#include "avio.h"
e60b4ced 23#include "mov.h"
1cb5f7fd 24
6e6d6dc0
MN
25#undef NDEBUG
26#include <assert.h>
27
1cb5f7fd
MN
28#define MOV_INDEX_CLUSTER_SIZE 16384
29#define globalTimescale 1000
30
69dde1ad
GB
31#define MODE_MP4 0
32#define MODE_MOV 1
33#define MODE_3GP 2
115329f1 34#define MODE_PSP 3 // example working PSP command line:
8af18154 35// ffmpeg -i testinput.avi -f psp -r 14.985 -s 320x240 -b 768 -ar 24000 -ab 32 M4V00001.MP4
8536ab89 36#define MODE_3G2 4
69dde1ad 37
1cb5f7fd 38typedef struct MOVIentry {
b29af723
MN
39 unsigned int flags, size;
40 uint64_t pos;
e45ccf79 41 unsigned int samplesInChunk;
f578f938 42 char key_frame;
1cb5f7fd
MN
43 unsigned int entries;
44} MOVIentry;
45
46typedef struct MOVIndex {
69dde1ad 47 int mode;
1cb5f7fd 48 int entry;
b29af723 49 uint64_t mdat_size;
1cb5f7fd
MN
50 int ents_allocated;
51 long timescale;
52 long time;
f578f938 53 long trackDuration;
e45ccf79
GB
54 long sampleCount;
55 long sampleDuration;
f578f938 56 int hasKeyframes;
ab561df9 57 int language;
1cb5f7fd
MN
58 int trackID;
59 AVCodecContext *enc;
60
61 int vosLen;
6e6d6dc0 62 uint8_t *vosData;
1cb5f7fd
MN
63 MOVIentry** cluster;
64} MOVTrack;
65
8af18154 66typedef struct MOVContext {
69dde1ad 67 int mode;
1cb5f7fd
MN
68 long time;
69 int nb_streams;
f578f938
TR
70 int mdat_written;
71 offset_t mdat_pos;
1cb5f7fd
MN
72 long timescale;
73 MOVTrack tracks[MAX_STREAMS];
74} MOVContext;
75
e45ccf79
GB
76static int mov_write_esds_tag(ByteIOContext *pb, MOVTrack* track);
77
ab561df9
FR
78/* output language code from iso639 language name */
79extern int ff_mov_iso639_to_lang(const char *lang, int mp4);
80
6e6d6dc0 81//FIXME supprt 64bit varaint with wide placeholders
b29af723 82static offset_t updateSize (ByteIOContext *pb, offset_t pos)
1cb5f7fd 83{
b29af723 84 offset_t curpos = url_ftell(pb);
1cb5f7fd 85 url_fseek(pb, pos, SEEK_SET);
6e6d6dc0 86 put_be32(pb, curpos - pos); /* rewrite size */
1cb5f7fd 87 url_fseek(pb, curpos, SEEK_SET);
6e6d6dc0
MN
88
89 return curpos - pos;
1cb5f7fd
MN
90}
91
e45ccf79 92/* Chunk offset atom */
6e6d6dc0 93static int mov_write_stco_tag(ByteIOContext *pb, MOVTrack* track)
1cb5f7fd
MN
94{
95 int i;
ed06cf6d 96 int mode64 = 0; // use 32 bit size variant if possible
b29af723 97 offset_t pos = url_ftell(pb);
f578f938 98 put_be32(pb, 0); /* size */
b29af723
MN
99 if (pos > UINT32_MAX) {
100 mode64 = 1;
101 put_tag(pb, "co64");
102 } else
103 put_tag(pb, "stco");
1cb5f7fd
MN
104 put_be32(pb, 0); /* version & flags */
105 put_be32(pb, track->entry); /* entry count */
106 for (i=0; i<track->entry; i++) {
107 int cl = i / MOV_INDEX_CLUSTER_SIZE;
108 int id = i % MOV_INDEX_CLUSTER_SIZE;
b29af723
MN
109 if(mode64 == 1)
110 put_be64(pb, track->cluster[cl][id].pos);
111 else
112 put_be32(pb, track->cluster[cl][id].pos);
1cb5f7fd 113 }
f578f938 114 return updateSize (pb, pos);
1cb5f7fd
MN
115}
116
e45ccf79 117/* Sample size atom */
6e6d6dc0 118static int mov_write_stsz_tag(ByteIOContext *pb, MOVTrack* track)
1cb5f7fd 119{
f578f938 120 int equalChunks = 1;
e45ccf79 121 int i, j, entries = 0, tst = -1, oldtst = -1;
1cb5f7fd 122
b29af723 123 offset_t pos = url_ftell(pb);
f578f938 124 put_be32(pb, 0); /* size */
1cb5f7fd
MN
125 put_tag(pb, "stsz");
126 put_be32(pb, 0); /* version & flags */
127
f578f938
TR
128 for (i=0; i<track->entry; i++) {
129 int cl = i / MOV_INDEX_CLUSTER_SIZE;
130 int id = i % MOV_INDEX_CLUSTER_SIZE;
e45ccf79
GB
131 tst = track->cluster[cl][id].size/track->cluster[cl][id].entries;
132 if(oldtst != -1 && tst != oldtst) {
133 equalChunks = 0;
f578f938
TR
134 }
135 oldtst = tst;
e45ccf79 136 entries += track->cluster[cl][id].entries;
f578f938 137 }
e45ccf79
GB
138 if (equalChunks) {
139 int sSize = track->cluster[0][0].size/track->cluster[0][0].entries;
115329f1 140 put_be32(pb, sSize); // sample size
e45ccf79 141 put_be32(pb, entries); // sample count
1cb5f7fd 142 }
f578f938 143 else {
115329f1
DB
144 put_be32(pb, 0); // sample size
145 put_be32(pb, entries); // sample count
f578f938 146 for (i=0; i<track->entry; i++) {
1cb5f7fd
MN
147 int cl = i / MOV_INDEX_CLUSTER_SIZE;
148 int id = i % MOV_INDEX_CLUSTER_SIZE;
e45ccf79
GB
149 for ( j=0; j<track->cluster[cl][id].entries; j++) {
150 put_be32(pb, track->cluster[cl][id].size /
151 track->cluster[cl][id].entries);
152 }
1cb5f7fd
MN
153 }
154 }
f578f938 155 return updateSize (pb, pos);
1cb5f7fd
MN
156}
157
e45ccf79 158/* Sample to chunk atom */
6e6d6dc0 159static int mov_write_stsc_tag(ByteIOContext *pb, MOVTrack* track)
1cb5f7fd 160{
b29af723
MN
161 int index = 0, oldval = -1, i;
162 offset_t entryPos, curpos;
f578f938 163
b29af723 164 offset_t pos = url_ftell(pb);
f578f938 165 put_be32(pb, 0); /* size */
1cb5f7fd 166 put_tag(pb, "stsc");
115329f1 167 put_be32(pb, 0); // version & flags
f578f938 168 entryPos = url_ftell(pb);
115329f1 169 put_be32(pb, track->entry); // entry count
f578f938
TR
170 for (i=0; i<track->entry; i++) {
171 int cl = i / MOV_INDEX_CLUSTER_SIZE;
172 int id = i % MOV_INDEX_CLUSTER_SIZE;
e45ccf79 173 if(oldval != track->cluster[cl][id].samplesInChunk)
f578f938 174 {
115329f1 175 put_be32(pb, i+1); // first chunk
e45ccf79 176 put_be32(pb, track->cluster[cl][id].samplesInChunk); // samples per chunk
115329f1 177 put_be32(pb, 0x1); // sample description index
e45ccf79 178 oldval = track->cluster[cl][id].samplesInChunk;
f578f938 179 index++;
1cb5f7fd
MN
180 }
181 }
f578f938
TR
182 curpos = url_ftell(pb);
183 url_fseek(pb, entryPos, SEEK_SET);
115329f1 184 put_be32(pb, index); // rewrite size
f578f938 185 url_fseek(pb, curpos, SEEK_SET);
1cb5f7fd 186
f578f938 187 return updateSize (pb, pos);
1cb5f7fd
MN
188}
189
e45ccf79 190/* Sync sample atom */
f578f938 191static int mov_write_stss_tag(ByteIOContext *pb, MOVTrack* track)
1cb5f7fd 192{
b29af723
MN
193 offset_t curpos, entryPos;
194 int i, index = 0;
195 offset_t pos = url_ftell(pb);
115329f1 196 put_be32(pb, 0); // size
1cb5f7fd 197 put_tag(pb, "stss");
115329f1 198 put_be32(pb, 0); // version & flags
f578f938 199 entryPos = url_ftell(pb);
115329f1 200 put_be32(pb, track->entry); // entry count
f578f938
TR
201 for (i=0; i<track->entry; i++) {
202 int cl = i / MOV_INDEX_CLUSTER_SIZE;
203 int id = i % MOV_INDEX_CLUSTER_SIZE;
204 if(track->cluster[cl][id].key_frame == 1) {
205 put_be32(pb, i+1);
206 index++;
207 }
208 }
209 curpos = url_ftell(pb);
210 url_fseek(pb, entryPos, SEEK_SET);
115329f1 211 put_be32(pb, index); // rewrite size
f578f938
TR
212 url_fseek(pb, curpos, SEEK_SET);
213 return updateSize (pb, pos);
1cb5f7fd
MN
214}
215
6e6d6dc0 216static int mov_write_damr_tag(ByteIOContext *pb)
1cb5f7fd
MN
217{
218 put_be32(pb, 0x11); /* size */
219 put_tag(pb, "damr");
220 put_tag(pb, "FFMP");
221 put_byte(pb, 0);
f578f938
TR
222
223 put_be16(pb, 0x80); /* Mode set (all modes for AMR_NB) */
224 put_be16(pb, 0xa); /* Mode change period (no restriction) */
225 //put_be16(pb, 0x81ff); /* Mode set (all modes for AMR_NB) */
226 //put_be16(pb, 1); /* Mode change period (no restriction) */
1cb5f7fd
MN
227 return 0x11;
228}
229
69dde1ad
GB
230static int mov_write_wave_tag(ByteIOContext *pb, MOVTrack* track)
231{
b29af723 232 offset_t pos = url_ftell(pb);
69dde1ad
GB
233
234 put_be32(pb, 0); /* size */
235 put_tag(pb, "wave");
236
237 put_be32(pb, 12); /* size */
238 put_tag(pb, "frma");
239 put_tag(pb, "mp4a");
240
241 put_be32(pb, 12); /* size */
242 put_tag(pb, "mp4a");
243 put_be32(pb, 0);
244
245 mov_write_esds_tag(pb, track);
246
247 put_be32(pb, 12); /* size */
248 put_tag(pb, "srcq");
249 put_be32(pb, 0x40);
250
251 put_be32(pb, 8); /* size */
252 put_be32(pb, 0); /* null tag */
253
254 return updateSize (pb, pos);
255}
256
0c1e0bab 257static const CodecTag codec_movaudio_tags[] = {
8cc7a34d
AB
258 { CODEC_ID_PCM_MULAW, MKTAG('u', 'l', 'a', 'w') },
259 { CODEC_ID_PCM_ALAW, MKTAG('a', 'l', 'a', 'w') },
260 { CODEC_ID_ADPCM_IMA_QT, MKTAG('i', 'm', 'a', '4') },
261 { CODEC_ID_MACE3, MKTAG('M', 'A', 'C', '3') },
262 { CODEC_ID_MACE6, MKTAG('M', 'A', 'C', '6') },
263 { CODEC_ID_AAC, MKTAG('m', 'p', '4', 'a') },
264 { CODEC_ID_AMR_NB, MKTAG('s', 'a', 'm', 'r') },
11bec294 265 { CODEC_ID_AMR_WB, MKTAG('s', 'a', 'w', 'b') },
8cc7a34d
AB
266 { CODEC_ID_PCM_S16BE, MKTAG('t', 'w', 'o', 's') },
267 { CODEC_ID_PCM_S16LE, MKTAG('s', 'o', 'w', 't') },
268 { CODEC_ID_MP3, MKTAG('.', 'm', 'p', '3') },
269 { 0, 0 },
270};
271
f578f938 272static int mov_write_audio_tag(ByteIOContext *pb, MOVTrack* track)
1cb5f7fd 273{
b29af723 274 offset_t pos = url_ftell(pb);
ffdd57d4 275 int tag;
115329f1 276
1cb5f7fd 277 put_be32(pb, 0); /* size */
f578f938 278
caacd4de
RG
279 tag = track->enc->codec_tag;
280 if (!tag)
8cc7a34d
AB
281 tag = codec_get_tag(codec_movaudio_tags, track->enc->codec_id);
282 // if no mac fcc found, try with Microsoft tags
283 if (!tag)
284 {
bb270c08 285 int tmp = codec_get_tag(codec_wav_tags, track->enc->codec_id);
ffdd57d4 286 tag = MKTAG('m', 's', ((tmp >> 8) & 0xff), (tmp & 0xff));
8cc7a34d 287 }
ffdd57d4 288 put_le32(pb, tag); // store it byteswapped
f578f938 289
1cb5f7fd
MN
290 put_be32(pb, 0); /* Reserved */
291 put_be16(pb, 0); /* Reserved */
292 put_be16(pb, 1); /* Data-reference index, XXX == 1 */
69dde1ad 293
e45ccf79 294 /* SoundDescription */
69dde1ad
GB
295 if(track->mode == MODE_MOV && track->enc->codec_id == CODEC_ID_AAC)
296 put_be16(pb, 1); /* Version 1 */
297 else
298 put_be16(pb, 0); /* Version 0 */
e45ccf79 299 put_be16(pb, 0); /* Revision level */
1cb5f7fd
MN
300 put_be32(pb, 0); /* Reserved */
301
f578f938
TR
302 put_be16(pb, track->enc->channels); /* Number of channels */
303 /* TODO: Currently hard-coded to 16-bit, there doesn't seem
e45ccf79 304 to be a good way to get number of bits of audio */
1cb5f7fd 305 put_be16(pb, 0x10); /* Reserved */
ef19c7eb
GB
306
307 if(track->enc->codec_id == CODEC_ID_AAC ||
308 track->enc->codec_id == CODEC_ID_MP3)
309 {
310 put_be16(pb, 0xfffe); /* compression ID (vbr)*/
311 }
312 else
313 {
314 put_be16(pb, 0); /* compression ID (= 0) */
315 }
9a4d9388 316 put_be16(pb, 0); /* packet size (= 0) */
1cb5f7fd
MN
317 put_be16(pb, track->timescale); /* Time scale */
318 put_be16(pb, 0); /* Reserved */
319
69dde1ad
GB
320 if(track->mode == MODE_MOV && track->enc->codec_id == CODEC_ID_AAC)
321 {
322 /* SoundDescription V1 extended info */
323 put_be32(pb, track->enc->frame_size); /* Samples per packet */
324 put_be32(pb, 1536); /* Bytes per packet */
325 put_be32(pb, 2); /* Bytes per frame */
326 put_be32(pb, 2); /* Bytes per sample */
327 }
328
329 if(track->enc->codec_id == CODEC_ID_AAC) {
330 if( track->mode == MODE_MOV ) mov_write_wave_tag(pb, track);
331 else mov_write_esds_tag(pb, track);
332 }
f578f938
TR
333 if(track->enc->codec_id == CODEC_ID_AMR_NB)
334 mov_write_damr_tag(pb);
6e6d6dc0 335 return updateSize (pb, pos);
1cb5f7fd
MN
336}
337
6e6d6dc0 338static int mov_write_d263_tag(ByteIOContext *pb)
1cb5f7fd
MN
339{
340 put_be32(pb, 0xf); /* size */
341 put_tag(pb, "d263");
342 put_tag(pb, "FFMP");
343 put_be16(pb, 0x0a);
344 put_byte(pb, 0);
345 return 0xf;
346}
347
f578f938
TR
348/* TODO: No idea about these values */
349static int mov_write_svq3_tag(ByteIOContext *pb)
1cb5f7fd 350{
f578f938
TR
351 put_be32(pb, 0x15);
352 put_tag(pb, "SMI ");
353 put_tag(pb, "SEQH");
354 put_be32(pb, 0x5);
355 put_be32(pb, 0xe2c0211d);
356 put_be32(pb, 0xc0000000);
115329f1 357 put_byte(pb, 0);
f578f938 358 return 0x15;
1cb5f7fd
MN
359}
360
e45ccf79
GB
361static unsigned int descrLength(unsigned int len)
362{
363 if (len < 0x00000080)
364 return 2 + len;
365 else if (len < 0x00004000)
366 return 3 + len;
367 else if(len < 0x00200000)
368 return 4 + len;
369 else
370 return 5 + len;
371}
372
373static void putDescr(ByteIOContext *pb, int tag, int size)
1cb5f7fd 374{
e45ccf79
GB
375 uint32_t len;
376 uint8_t vals[4];
377
378 len = size;
379 vals[3] = (uint8_t)(len & 0x7f);
380 len >>= 7;
115329f1 381 vals[2] = (uint8_t)((len & 0x7f) | 0x80);
e45ccf79 382 len >>= 7;
115329f1 383 vals[1] = (uint8_t)((len & 0x7f) | 0x80);
e45ccf79
GB
384 len >>= 7;
385 vals[0] = (uint8_t)((len & 0x7f) | 0x80);
386
387 put_byte(pb, tag); // DescriptorTag
388
389 if (size < 0x00000080)
390 {
391 put_byte(pb, vals[3]);
392 }
393 else if (size < 0x00004000)
394 {
395 put_byte(pb, vals[2]);
396 put_byte(pb, vals[3]);
397 }
398 else if (size < 0x00200000)
399 {
400 put_byte(pb, vals[1]);
401 put_byte(pb, vals[2]);
402 put_byte(pb, vals[3]);
403 }
404 else if (size < 0x10000000)
405 {
406 put_byte(pb, vals[0]);
407 put_byte(pb, vals[1]);
408 put_byte(pb, vals[2]);
409 put_byte(pb, vals[3]);
410 }
1cb5f7fd
MN
411}
412
6e6d6dc0 413static int mov_write_esds_tag(ByteIOContext *pb, MOVTrack* track) // Basic
1cb5f7fd 414{
46103f6b 415 int decoderSpecificInfoLen;
b29af723 416 offset_t pos = url_ftell(pb);
e45ccf79 417
46103f6b
FR
418 decoderSpecificInfoLen = track->vosLen ? descrLength(track->vosLen):0;
419
e45ccf79 420 put_be32(pb, 0); // size
1cb5f7fd 421 put_tag(pb, "esds");
e45ccf79 422 put_be32(pb, 0); // Version
1cb5f7fd 423
e45ccf79
GB
424 // ES descriptor
425 putDescr(pb, 0x03, 3 + descrLength(13 + decoderSpecificInfoLen) +
426 descrLength(1));
6c13c880 427 put_be16(pb, track->trackID);
1cb5f7fd
MN
428 put_byte(pb, 0x00); // flags (= no flags)
429
e45ccf79
GB
430 // DecoderConfig descriptor
431 putDescr(pb, 0x04, 13 + decoderSpecificInfoLen);
432
6c13c880
MN
433 // Object type indication
434 put_byte(pb, codec_get_tag(ff_mov_obj_type, track->enc->codec_id));
e45ccf79 435
5cad192d
NS
436 // the following fields is made of 6 bits to identify the streamtype (4 for video, 5 for audio)
437 // plus 1 bit to indicate upstream and 1 bit set to 1 (reserved)
e45ccf79
GB
438 if(track->enc->codec_type == CODEC_TYPE_AUDIO)
439 put_byte(pb, 0x15); // flags (= Audiostream)
440 else
441 put_byte(pb, 0x11); // flags (= Visualstream)
442
6c13c880
MN
443 put_byte(pb, track->enc->rc_buffer_size>>(3+16)); // Buffersize DB (24 bits)
444 put_be16(pb, (track->enc->rc_buffer_size>>3)&0xFFFF); // Buffersize DB
1cb5f7fd 445
6c13c880
MN
446 put_be32(pb, FFMAX(track->enc->bit_rate, track->enc->rc_max_rate)); // maxbitrate (FIXME should be max rate in any 1 sec window)
447 if(track->enc->rc_max_rate != track->enc->rc_min_rate || track->enc->rc_min_rate==0)
448 put_be32(pb, 0); // vbr
449 else
450 put_be32(pb, track->enc->rc_max_rate); // avg bitrate
1cb5f7fd 451
e45ccf79
GB
452 if (track->vosLen)
453 {
454 // DecoderSpecific info descriptor
455 putDescr(pb, 0x05, track->vosLen);
456 put_buffer(pb, track->vosData, track->vosLen);
457 }
458
8af18154 459
e45ccf79 460 // SL descriptor
4bfc029f 461 putDescr(pb, 0x06, 1);
1cb5f7fd 462 put_byte(pb, 0x02);
e45ccf79 463 return updateSize (pb, pos);
1cb5f7fd
MN
464}
465
0c1e0bab 466static const CodecTag codec_movvideo_tags[] = {
8cc7a34d
AB
467 { CODEC_ID_SVQ1, MKTAG('S', 'V', 'Q', '1') },
468 { CODEC_ID_SVQ3, MKTAG('S', 'V', 'Q', '3') },
469 { CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 'v') },
470 { CODEC_ID_H263, MKTAG('s', '2', '6', '3') },
0c1e0bab 471 { CODEC_ID_H264, MKTAG('a', 'v', 'c', '1') },
8cc7a34d
AB
472 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', ' ') },
473 { 0, 0 },
474};
475
f578f938 476static int mov_write_video_tag(ByteIOContext *pb, MOVTrack* track)
1cb5f7fd 477{
b29af723 478 offset_t pos = url_ftell(pb);
53ffdd14 479 char compressor_name[32];
ffdd57d4 480 int tag;
8cc7a34d 481
f578f938 482 put_be32(pb, 0); /* size */
8cc7a34d 483
caacd4de
RG
484 tag = track->enc->codec_tag;
485 if (!tag)
8cc7a34d
AB
486 tag = codec_get_tag(codec_movvideo_tags, track->enc->codec_id);
487 // if no mac fcc found, try with Microsoft tags
488 if (!tag)
bb270c08 489 tag = codec_get_tag(codec_bmp_tags, track->enc->codec_id);
ffdd57d4 490 put_le32(pb, tag); // store it byteswapped
6e6d6dc0 491
f578f938
TR
492 put_be32(pb, 0); /* Reserved */
493 put_be16(pb, 0); /* Reserved */
494 put_be16(pb, 1); /* Data-reference index */
495
53ffdd14
RG
496 put_be16(pb, 0); /* Codec stream version */
497 put_be16(pb, 0); /* Codec stream revision (=0) */
498 put_tag(pb, "FFMP"); /* Vendor */
499 if(track->enc->codec_id == CODEC_ID_RAWVIDEO) {
500 put_be32(pb, 0); /* Temporal Quality */
501 put_be32(pb, 0x400); /* Spatial Quality = lossless*/
502 } else {
503 put_be32(pb, 0x200); /* Temporal Quality = normal */
504 put_be32(pb, 0x200); /* Spatial Quality = normal */
505 }
f578f938
TR
506 put_be16(pb, track->enc->width); /* Video width */
507 put_be16(pb, track->enc->height); /* Video height */
53ffdd14
RG
508 put_be32(pb, 0x00480000); /* Horizontal resolution 72dpi */
509 put_be32(pb, 0x00480000); /* Vertical resolution 72dpi */
9a4d9388
RS
510 put_be32(pb, 0); /* Data size (= 0) */
511 put_be16(pb, 1); /* Frame count (= 1) */
115329f1 512
53ffdd14 513 memset(compressor_name,0,32);
25cf9062 514 if (track->enc->codec && track->enc->codec->name)
53ffdd14 515 strncpy(compressor_name,track->enc->codec->name,31);
25cf9062 516 put_byte(pb, strlen(compressor_name));
53ffdd14 517 put_buffer(pb, compressor_name, 31);
115329f1 518
f578f938
TR
519 put_be16(pb, 0x18); /* Reserved */
520 put_be16(pb, 0xffff); /* Reserved */
521 if(track->enc->codec_id == CODEC_ID_MPEG4)
522 mov_write_esds_tag(pb, track);
523 else if(track->enc->codec_id == CODEC_ID_H263)
524 mov_write_d263_tag(pb);
525 else if(track->enc->codec_id == CODEC_ID_SVQ3)
115329f1 526 mov_write_svq3_tag(pb);
f578f938
TR
527
528 return updateSize (pb, pos);
1cb5f7fd
MN
529}
530
6e6d6dc0 531static int mov_write_stsd_tag(ByteIOContext *pb, MOVTrack* track)
1cb5f7fd 532{
b29af723 533 offset_t pos = url_ftell(pb);
1cb5f7fd
MN
534 put_be32(pb, 0); /* size */
535 put_tag(pb, "stsd");
536 put_be32(pb, 0); /* version & flags */
537 put_be32(pb, 1); /* entry count */
f578f938
TR
538 if (track->enc->codec_type == CODEC_TYPE_VIDEO)
539 mov_write_video_tag(pb, track);
540 else if (track->enc->codec_type == CODEC_TYPE_AUDIO)
541 mov_write_audio_tag(pb, track);
6e6d6dc0 542 return updateSize(pb, pos);
1cb5f7fd
MN
543}
544
0078e9b9 545/* TODO: */
e45ccf79 546/* Time to sample atom */
6e6d6dc0 547static int mov_write_stts_tag(ByteIOContext *pb, MOVTrack* track)
1cb5f7fd
MN
548{
549 put_be32(pb, 0x18); /* size */
550 put_tag(pb, "stts");
551 put_be32(pb, 0); /* version & flags */
552 put_be32(pb, 1); /* entry count */
553
e45ccf79
GB
554 put_be32(pb, track->sampleCount); /* sample count */
555 put_be32(pb, track->sampleDuration); /* sample duration */
1cb5f7fd
MN
556 return 0x18;
557}
558
6e6d6dc0 559static int mov_write_dref_tag(ByteIOContext *pb)
1cb5f7fd
MN
560{
561 put_be32(pb, 28); /* size */
562 put_tag(pb, "dref");
563 put_be32(pb, 0); /* version & flags */
564 put_be32(pb, 1); /* entry count */
565
566 put_be32(pb, 0xc); /* size */
567 put_tag(pb, "url ");
568 put_be32(pb, 1); /* version & flags */
569
570 return 28;
571}
572
6e6d6dc0 573static int mov_write_stbl_tag(ByteIOContext *pb, MOVTrack* track)
1cb5f7fd 574{
b29af723 575 offset_t pos = url_ftell(pb);
1cb5f7fd
MN
576 put_be32(pb, 0); /* size */
577 put_tag(pb, "stbl");
6e6d6dc0
MN
578 mov_write_stsd_tag(pb, track);
579 mov_write_stts_tag(pb, track);
f578f938
TR
580 if (track->enc->codec_type == CODEC_TYPE_VIDEO &&
581 track->hasKeyframes)
582 mov_write_stss_tag(pb, track);
6e6d6dc0
MN
583 mov_write_stsc_tag(pb, track);
584 mov_write_stsz_tag(pb, track);
585 mov_write_stco_tag(pb, track);
586 return updateSize(pb, pos);
1cb5f7fd
MN
587}
588
6e6d6dc0 589static int mov_write_dinf_tag(ByteIOContext *pb)
1cb5f7fd 590{
b29af723 591 offset_t pos = url_ftell(pb);
1cb5f7fd
MN
592 put_be32(pb, 0); /* size */
593 put_tag(pb, "dinf");
6e6d6dc0
MN
594 mov_write_dref_tag(pb);
595 return updateSize(pb, pos);
1cb5f7fd
MN
596}
597
6e6d6dc0 598static int mov_write_smhd_tag(ByteIOContext *pb)
1cb5f7fd
MN
599{
600 put_be32(pb, 16); /* size */
601 put_tag(pb, "smhd");
602 put_be32(pb, 0); /* version & flags */
603 put_be16(pb, 0); /* reserved (balance, normally = 0) */
604 put_be16(pb, 0); /* reserved */
605 return 16;
606}
607
6e6d6dc0 608static int mov_write_vmhd_tag(ByteIOContext *pb)
1cb5f7fd
MN
609{
610 put_be32(pb, 0x14); /* size (always 0x14) */
611 put_tag(pb, "vmhd");
612 put_be32(pb, 0x01); /* version & flags */
613 put_be64(pb, 0); /* reserved (graphics mode = copy) */
614 return 0x14;
615}
616
6e6d6dc0 617static int mov_write_hdlr_tag(ByteIOContext *pb, MOVTrack* track)
1cb5f7fd 618{
9a4d9388 619 char *descr, *hdlr, *hdlr_type;
b29af723 620 offset_t pos = url_ftell(pb);
115329f1 621
9a4d9388 622 if (!track) { /* no media --> data handler */
bb270c08
DB
623 hdlr = "dhlr";
624 hdlr_type = "url ";
625 descr = "DataHandler";
9a4d9388 626 } else {
bb270c08
DB
627 hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
628 if (track->enc->codec_type == CODEC_TYPE_VIDEO) {
629 hdlr_type = "vide";
630 descr = "VideoHandler";
631 } else {
632 hdlr_type = "soun";
633 descr = "SoundHandler";
634 }
9a4d9388 635 }
115329f1 636
f578f938 637 put_be32(pb, 0); /* size */
1cb5f7fd
MN
638 put_tag(pb, "hdlr");
639 put_be32(pb, 0); /* Version & flags */
906b578f 640 put_buffer(pb, hdlr, 4); /* handler */
9a4d9388 641 put_tag(pb, hdlr_type); /* handler type */
f578f938
TR
642 put_be32(pb ,0); /* reserved */
643 put_be32(pb ,0); /* reserved */
644 put_be32(pb ,0); /* reserved */
9a4d9388
RS
645 put_byte(pb, strlen(descr)); /* string counter */
646 put_buffer(pb, descr, strlen(descr)); /* handler description */
647 return updateSize(pb, pos);
648}
649
650static int mov_write_minf_tag(ByteIOContext *pb, MOVTrack* track)
651{
b29af723 652 offset_t pos = url_ftell(pb);
9a4d9388
RS
653 put_be32(pb, 0); /* size */
654 put_tag(pb, "minf");
1cb5f7fd 655 if(track->enc->codec_type == CODEC_TYPE_VIDEO)
9a4d9388 656 mov_write_vmhd_tag(pb);
1cb5f7fd 657 else
9a4d9388
RS
658 mov_write_smhd_tag(pb);
659 if (track->mode == MODE_MOV) /* FIXME: Why do it for MODE_MOV only ? */
660 mov_write_hdlr_tag(pb, NULL);
661 mov_write_dinf_tag(pb);
662 mov_write_stbl_tag(pb, track);
f578f938 663 return updateSize(pb, pos);
1cb5f7fd
MN
664}
665
6e6d6dc0 666static int mov_write_mdhd_tag(ByteIOContext *pb, MOVTrack* track)
1cb5f7fd
MN
667{
668 put_be32(pb, 32); /* size */
669 put_tag(pb, "mdhd");
670 put_be32(pb, 0); /* Version & flags */
671 put_be32(pb, track->time); /* creation time */
672 put_be32(pb, track->time); /* modification time */
115329f1 673 put_be32(pb, track->timescale); /* time scale (sample rate for audio) */
e45ccf79 674 put_be32(pb, track->trackDuration); /* duration */
ab561df9 675 put_be16(pb, track->language); /* language */
1cb5f7fd
MN
676 put_be16(pb, 0); /* reserved (quality) */
677 return 32;
678}
679
6e6d6dc0 680static int mov_write_mdia_tag(ByteIOContext *pb, MOVTrack* track)
1cb5f7fd 681{
b29af723 682 offset_t pos = url_ftell(pb);
1cb5f7fd
MN
683 put_be32(pb, 0); /* size */
684 put_tag(pb, "mdia");
6e6d6dc0
MN
685 mov_write_mdhd_tag(pb, track);
686 mov_write_hdlr_tag(pb, track);
687 mov_write_minf_tag(pb, track);
688 return updateSize(pb, pos);
1cb5f7fd
MN
689}
690
6e6d6dc0 691static int mov_write_tkhd_tag(ByteIOContext *pb, MOVTrack* track)
1cb5f7fd
MN
692{
693 put_be32(pb, 0x5c); /* size (always 0x5c) */
694 put_tag(pb, "tkhd");
f578f938 695 put_be32(pb, 0xf); /* version & flags (track enabled) */
1cb5f7fd
MN
696 put_be32(pb, track->time); /* creation time */
697 put_be32(pb, track->time); /* modification time */
698 put_be32(pb, track->trackID); /* track-id */
699 put_be32(pb, 0); /* reserved */
83fa2ef8 700 put_be32(pb, av_rescale_rnd(track->trackDuration, globalTimescale, track->timescale, AV_ROUND_UP)); /* duration */
1cb5f7fd
MN
701
702 put_be32(pb, 0); /* reserved */
703 put_be32(pb, 0); /* reserved */
704 put_be32(pb, 0x0); /* reserved (Layer & Alternate group) */
705 /* Volume, only for audio */
706 if(track->enc->codec_type == CODEC_TYPE_AUDIO)
707 put_be16(pb, 0x0100);
708 else
709 put_be16(pb, 0);
710 put_be16(pb, 0); /* reserved */
711
712 /* Matrix structure */
713 put_be32(pb, 0x00010000); /* reserved */
714 put_be32(pb, 0x0); /* reserved */
715 put_be32(pb, 0x0); /* reserved */
716 put_be32(pb, 0x0); /* reserved */
717 put_be32(pb, 0x00010000); /* reserved */
718 put_be32(pb, 0x0); /* reserved */
719 put_be32(pb, 0x0); /* reserved */
720 put_be32(pb, 0x0); /* reserved */
721 put_be32(pb, 0x40000000); /* reserved */
722
723 /* Track width and height, for visual only */
724 if(track->enc->codec_type == CODEC_TYPE_VIDEO) {
69dde1ad
GB
725 double sample_aspect_ratio = av_q2d(track->enc->sample_aspect_ratio);
726 if( !sample_aspect_ratio ) sample_aspect_ratio = 1;
727 put_be32(pb, sample_aspect_ratio * track->enc->width*0x10000);
f578f938 728 put_be32(pb, track->enc->height*0x10000);
1cb5f7fd
MN
729 }
730 else {
731 put_be32(pb, 0);
732 put_be32(pb, 0);
733 }
734 return 0x5c;
735}
736
8af18154 737// This box seems important for the psp playback ... without it the movie seems to hang
738static int mov_write_edts_tag(ByteIOContext *pb, MOVTrack *track)
739{
8af18154 740 put_be32(pb, 0x24); /* size */
741 put_tag(pb, "edts");
742 put_be32(pb, 0x1c); /* size */
743 put_tag(pb, "elst");
744 put_be32(pb, 0x0);
745 put_be32(pb, 0x1);
746
83fa2ef8 747 put_be32(pb, av_rescale_rnd(track->trackDuration, globalTimescale, track->timescale, AV_ROUND_UP)); /* duration ... doesn't seem to effect psp */
8af18154 748
749 put_be32(pb, 0x0);
750 put_be32(pb, 0x00010000);
751 return 0x24;
752}
753
754// goes at the end of each track! ... Critical for PSP playback ("Incompatible data" without it)
755static int mov_write_uuid_tag_psp(ByteIOContext *pb, MOVTrack *mov)
756{
757 put_be32(pb, 0x34); /* size ... reports as 28 in mp4box! */
758 put_tag(pb, "uuid");
759 put_tag(pb, "USMT");
760 put_be32(pb, 0x21d24fce);
761 put_be32(pb, 0xbb88695c);
762 put_be32(pb, 0xfac9c740);
763 put_be32(pb, 0x1c); // another size here!
764 put_tag(pb, "MTDT");
765 put_be32(pb, 0x00010012);
766 put_be32(pb, 0x0a);
767 put_be32(pb, 0x55c40000);
768 put_be32(pb, 0x1);
769 put_be32(pb, 0x0);
770 return 0x34;
771}
772
6e6d6dc0 773static int mov_write_trak_tag(ByteIOContext *pb, MOVTrack* track)
1cb5f7fd 774{
b29af723 775 offset_t pos = url_ftell(pb);
1cb5f7fd
MN
776 put_be32(pb, 0); /* size */
777 put_tag(pb, "trak");
6e6d6dc0 778 mov_write_tkhd_tag(pb, track);
115329f1 779 if (track->mode == MODE_PSP)
8af18154 780 mov_write_edts_tag(pb, track); // PSP Movies require edts box
6e6d6dc0 781 mov_write_mdia_tag(pb, track);
115329f1 782 if (track->mode == MODE_PSP)
8af18154 783 mov_write_uuid_tag_psp(pb,track); // PSP Movies require this uuid box
6e6d6dc0 784 return updateSize(pb, pos);
1cb5f7fd
MN
785}
786
88730be6 787#if 0
1cb5f7fd 788/* TODO: Not sorted out, but not necessary either */
6e6d6dc0 789static int mov_write_iods_tag(ByteIOContext *pb, MOVContext *mov)
1cb5f7fd
MN
790{
791 put_be32(pb, 0x15); /* size */
792 put_tag(pb, "iods");
793 put_be32(pb, 0); /* version & flags */
794 put_be16(pb, 0x1007);
795 put_byte(pb, 0);
796 put_be16(pb, 0x4fff);
797 put_be16(pb, 0xfffe);
798 put_be16(pb, 0x01ff);
799 return 0x15;
800}
88730be6 801#endif
1cb5f7fd 802
6e6d6dc0 803static int mov_write_mvhd_tag(ByteIOContext *pb, MOVContext *mov)
1cb5f7fd 804{
b29af723
MN
805 int maxTrackID = 1, i;
806 int64_t maxTrackLenTemp, maxTrackLen = 0;
1cb5f7fd
MN
807
808 put_be32(pb, 0x6c); /* size (always 0x6c) */
809 put_tag(pb, "mvhd");
810 put_be32(pb, 0); /* version & flags */
811 put_be32(pb, mov->time); /* creation time */
812 put_be32(pb, mov->time); /* modification time */
813 put_be32(pb, mov->timescale); /* timescale */
814 for (i=0; i<MAX_STREAMS; i++) {
815 if(mov->tracks[i].entry > 0) {
83fa2ef8 816 maxTrackLenTemp = av_rescale_rnd(mov->tracks[i].trackDuration, globalTimescale, mov->tracks[i].timescale, AV_ROUND_UP);
f578f938
TR
817 if(maxTrackLen < maxTrackLenTemp)
818 maxTrackLen = maxTrackLenTemp;
1cb5f7fd
MN
819 if(maxTrackID < mov->tracks[i].trackID)
820 maxTrackID = mov->tracks[i].trackID;
821 }
822 }
823 put_be32(pb, maxTrackLen); /* duration of longest track */
824
825 put_be32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
826 put_be16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
827 put_be16(pb, 0); /* reserved */
828 put_be32(pb, 0); /* reserved */
829 put_be32(pb, 0); /* reserved */
830
831 /* Matrix structure */
832 put_be32(pb, 0x00010000); /* reserved */
833 put_be32(pb, 0x0); /* reserved */
834 put_be32(pb, 0x0); /* reserved */
835 put_be32(pb, 0x0); /* reserved */
836 put_be32(pb, 0x00010000); /* reserved */
837 put_be32(pb, 0x0); /* reserved */
838 put_be32(pb, 0x0); /* reserved */
839 put_be32(pb, 0x0); /* reserved */
840 put_be32(pb, 0x40000000); /* reserved */
841
842 put_be32(pb, 0); /* reserved (preview time) */
843 put_be32(pb, 0); /* reserved (preview duration) */
844 put_be32(pb, 0); /* reserved (poster time) */
845 put_be32(pb, 0); /* reserved (selection time) */
846 put_be32(pb, 0); /* reserved (selection duration) */
847 put_be32(pb, 0); /* reserved (current time) */
848 put_be32(pb, maxTrackID+1); /* Next track id */
849 return 0x6c;
850}
851
b6c50eb1
PB
852static int mov_write_itunes_hdlr_tag(ByteIOContext *pb, MOVContext* mov,
853 AVFormatContext *s)
854{
b29af723 855 offset_t pos = url_ftell(pb);
b6c50eb1
PB
856 put_be32(pb, 0); /* size */
857 put_tag(pb, "hdlr");
858 put_be32(pb, 0);
859 put_be32(pb, 0);
860 put_tag(pb, "mdir");
861 put_tag(pb, "appl");
862 put_be32(pb, 0);
863 put_be32(pb, 0);
864 put_be16(pb, 0);
865 return updateSize(pb, pos);
866}
867
868/* helper function to write a data tag with the specified string as data */
869static int mov_write_string_data_tag(ByteIOContext *pb, MOVContext* mov,
870 AVFormatContext *s, const char *data)
871{
b29af723 872 offset_t pos = url_ftell(pb);
b6c50eb1
PB
873 put_be32(pb, 0); /* size */
874 put_tag(pb, "data");
875 put_be32(pb, 1);
876 put_be32(pb, 0);
877 put_buffer(pb, data, strlen(data));
878 return updateSize(pb, pos);
879}
880
881/* iTunes name of the song/movie */
882static int mov_write_nam_tag(ByteIOContext *pb, MOVContext* mov,
883 AVFormatContext *s)
884{
885 int size = 0;
886 if ( s->title[0] ) {
b29af723 887 offset_t pos = url_ftell(pb);
b6c50eb1
PB
888 put_be32(pb, 0); /* size */
889 put_tag(pb, "\251nam");
890 mov_write_string_data_tag(pb, mov, s, s->title);
891 size = updateSize(pb, pos);
892 }
893 return size;
894}
895
896/* iTunes name of the artist/performer */
897static int mov_write_ART_tag(ByteIOContext *pb, MOVContext* mov,
898 AVFormatContext *s)
899{
900 int size = 0;
901 if ( s->author[0] ) {
b29af723 902 offset_t pos = url_ftell(pb);
b6c50eb1
PB
903 put_be32(pb, 0); /* size */
904 put_tag(pb, "\251ART");
905 // we use the author here as this is the only thing that we have...
906 mov_write_string_data_tag(pb, mov, s, s->author);
907 size = updateSize(pb, pos);
908 }
909 return size;
910}
911
912/* iTunes name of the writer */
913static int mov_write_wrt_tag(ByteIOContext *pb, MOVContext* mov,
914 AVFormatContext *s)
915{
916 int size = 0;
917 if ( s->author[0] ) {
b29af723 918 offset_t pos = url_ftell(pb);
b6c50eb1
PB
919 put_be32(pb, 0); /* size */
920 put_tag(pb, "\251wrt");
921 mov_write_string_data_tag(pb, mov, s, s->author);
922 size = updateSize(pb, pos);
923 }
924 return size;
925}
926
927/* iTunes name of the album */
928static int mov_write_alb_tag(ByteIOContext *pb, MOVContext* mov,
929 AVFormatContext *s)
930{
931 int size = 0;
932 if ( s->album[0] ) {
b29af723 933 offset_t pos = url_ftell(pb);
b6c50eb1
PB
934 put_be32(pb, 0); /* size */
935 put_tag(pb, "\251alb");
936 mov_write_string_data_tag(pb, mov, s, s->album);
937 size = updateSize(pb, pos);
938 }
939 return size;
940}
941
942/* iTunes year */
943static int mov_write_day_tag(ByteIOContext *pb, MOVContext* mov,
944 AVFormatContext *s)
945{
946 char year[5];
947 int size = 0;
948 if ( s->year ) {
b29af723 949 offset_t pos = url_ftell(pb);
b6c50eb1
PB
950 put_be32(pb, 0); /* size */
951 put_tag(pb, "\251day");
952 snprintf(year, 5, "%04d", s->year);
953 mov_write_string_data_tag(pb, mov, s, year);
954 size = updateSize(pb, pos);
955 }
956 return size;
957}
958
959/* iTunes tool used to create the file */
960static int mov_write_too_tag(ByteIOContext *pb, MOVContext* mov,
961 AVFormatContext *s)
962{
b29af723 963 offset_t pos = url_ftell(pb);
b6c50eb1
PB
964 put_be32(pb, 0); /* size */
965 put_tag(pb, "\251too");
966 mov_write_string_data_tag(pb, mov, s, LIBAVFORMAT_IDENT);
967 return updateSize(pb, pos);
968}
969
970/* iTunes comment */
971static int mov_write_cmt_tag(ByteIOContext *pb, MOVContext* mov,
972 AVFormatContext *s)
973{
974 int size = 0;
975 if ( s->comment[0] ) {
b29af723 976 offset_t pos = url_ftell(pb);
b6c50eb1
PB
977 put_be32(pb, 0); /* size */
978 put_tag(pb, "\251cmt");
979 mov_write_string_data_tag(pb, mov, s, s->comment);
980 size = updateSize(pb, pos);
981 }
982 return size;
983}
984
985/* iTunes custom genre */
986static int mov_write_gen_tag(ByteIOContext *pb, MOVContext* mov,
987 AVFormatContext *s)
988{
989 int size = 0;
990 if ( s->genre[0] ) {
b29af723 991 offset_t pos = url_ftell(pb);
b6c50eb1
PB
992 put_be32(pb, 0); /* size */
993 put_tag(pb, "\251gen");
994 mov_write_string_data_tag(pb, mov, s, s->genre);
995 size = updateSize(pb, pos);
996 }
997 return size;
998}
999
1000/* iTunes track number */
1001static int mov_write_trkn_tag(ByteIOContext *pb, MOVContext* mov,
1002 AVFormatContext *s)
1003{
1004 int size = 0;
1005 if ( s->track ) {
b29af723 1006 offset_t pos = url_ftell(pb);
b6c50eb1
PB
1007 put_be32(pb, 0); /* size */
1008 put_tag(pb, "trkn");
1009 {
b29af723 1010 offset_t pos = url_ftell(pb);
b6c50eb1
PB
1011 put_be32(pb, 0); /* size */
1012 put_tag(pb, "data");
1013 put_be32(pb, 0); // 8 bytes empty
1014 put_be32(pb, 0);
1015 put_be16(pb, 0); // empty
1016 put_be16(pb, s->track); // track number
1017 put_be16(pb, 0); // total track number
1018 put_be16(pb, 0); // empty
1019 updateSize(pb, pos);
1020 }
1021 size = updateSize(pb, pos);
1022 }
1023 return size;
1024}
1025
1026/* iTunes meta data list */
1027static int mov_write_ilst_tag(ByteIOContext *pb, MOVContext* mov,
1028 AVFormatContext *s)
1029{
b29af723 1030 offset_t pos = url_ftell(pb);
b6c50eb1
PB
1031 put_be32(pb, 0); /* size */
1032 put_tag(pb, "ilst");
1033 mov_write_nam_tag(pb, mov, s);
1034 mov_write_ART_tag(pb, mov, s);
1035 mov_write_wrt_tag(pb, mov, s);
1036 mov_write_alb_tag(pb, mov, s);
1037 mov_write_day_tag(pb, mov, s);
1038 mov_write_too_tag(pb, mov, s);
1039 mov_write_cmt_tag(pb, mov, s);
1040 mov_write_gen_tag(pb, mov, s);
1041 mov_write_trkn_tag(pb, mov, s);
1042 return updateSize(pb, pos);
1043}
1044
1045/* iTunes meta data tag */
1046static int mov_write_meta_tag(ByteIOContext *pb, MOVContext* mov,
1047 AVFormatContext *s)
1048{
1049 int size = 0;
1050
1051 // only save meta tag if required
115329f1 1052 if ( s->title[0] || s->author[0] || s->album[0] || s->year ||
b6c50eb1 1053 s->comment[0] || s->genre[0] || s->track ) {
b29af723 1054 offset_t pos = url_ftell(pb);
b6c50eb1
PB
1055 put_be32(pb, 0); /* size */
1056 put_tag(pb, "meta");
1057 put_be32(pb, 0);
1058 mov_write_itunes_hdlr_tag(pb, mov, s);
1059 mov_write_ilst_tag(pb, mov, s);
1060 size = updateSize(pb, pos);
1061 }
1062 return size;
1063}
115329f1 1064
69dde1ad
GB
1065static int mov_write_udta_tag(ByteIOContext *pb, MOVContext* mov,
1066 AVFormatContext *s)
1067{
b29af723 1068 offset_t pos = url_ftell(pb);
69dde1ad
GB
1069 int i;
1070
1071 put_be32(pb, 0); /* size */
1072 put_tag(pb, "udta");
1073
b6c50eb1
PB
1074 /* iTunes meta data */
1075 mov_write_meta_tag(pb, mov, s);
1076
69dde1ad
GB
1077 /* Requirements */
1078 for (i=0; i<MAX_STREAMS; i++) {
1079 if(mov->tracks[i].entry <= 0) continue;
1080 if (mov->tracks[i].enc->codec_id == CODEC_ID_AAC ||
1081 mov->tracks[i].enc->codec_id == CODEC_ID_MPEG4) {
b29af723 1082 offset_t pos = url_ftell(pb);
69dde1ad
GB
1083 put_be32(pb, 0); /* size */
1084 put_tag(pb, "\251req");
1085 put_be16(pb, sizeof("QuickTime 6.0 or greater") - 1);
1086 put_be16(pb, 0);
1087 put_buffer(pb, "QuickTime 6.0 or greater",
1088 sizeof("QuickTime 6.0 or greater") - 1);
1089 updateSize(pb, pos);
1090 break;
1091 }
1092 }
1093
1094 /* Encoder */
501866a1 1095 if(mov->tracks[0].enc && !(mov->tracks[0].enc->flags & CODEC_FLAG_BITEXACT))
69dde1ad 1096 {
b29af723 1097 offset_t pos = url_ftell(pb);
69dde1ad
GB
1098 put_be32(pb, 0); /* size */
1099 put_tag(pb, "\251enc");
1100 put_be16(pb, sizeof(LIBAVFORMAT_IDENT) - 1); /* string length */
1101 put_be16(pb, 0);
1102 put_buffer(pb, LIBAVFORMAT_IDENT, sizeof(LIBAVFORMAT_IDENT) - 1);
1103 updateSize(pb, pos);
1104 }
1105
1106 if( s->title[0] )
1107 {
b29af723 1108 offset_t pos = url_ftell(pb);
69dde1ad
GB
1109 put_be32(pb, 0); /* size */
1110 put_tag(pb, "\251nam");
1111 put_be16(pb, strlen(s->title)); /* string length */
1112 put_be16(pb, 0);
1113 put_buffer(pb, s->title, strlen(s->title));
1114 updateSize(pb, pos);
1115 }
1116
1117 if( s->author[0] )
1118 {
b29af723 1119 offset_t pos = url_ftell(pb);
69dde1ad
GB
1120 put_be32(pb, 0); /* size */
1121 put_tag(pb, /*"\251aut"*/ "\251day" );
1122 put_be16(pb, strlen(s->author)); /* string length */
1123 put_be16(pb, 0);
1124 put_buffer(pb, s->author, strlen(s->author));
1125 updateSize(pb, pos);
1126 }
1127
1128 if( s->comment[0] )
1129 {
b29af723 1130 offset_t pos = url_ftell(pb);
69dde1ad
GB
1131 put_be32(pb, 0); /* size */
1132 put_tag(pb, "\251des");
1133 put_be16(pb, strlen(s->comment)); /* string length */
1134 put_be16(pb, 0);
1135 put_buffer(pb, s->comment, strlen(s->comment));
1136 updateSize(pb, pos);
1137 }
1138
1139 return updateSize(pb, pos);
1140}
1141
dcfdb046
BL
1142
1143static size_t ascii_to_wc (ByteIOContext *pb, char *b, size_t n)
1144{
1145 size_t i;
1146 unsigned char c;
1147 for (i = 0; i < n - 1; i++) {
1148 c = b[i];
1149 if (! (0x20 <= c && c <= 0x7f ))
1150 c = 0x3f; /* '?' */
1151 put_be16(pb, c);
1152 }
1153 put_be16(pb, 0x00);
1154 return 2*n;
1155}
1156
1157static uint16_t language_code (char *str)
1158{
1159 return ((((str[0]-'a') & 0x1F)<<10) + (((str[1]-'a') & 0x1F)<<5) + ((str[2]-'a') & 0x1F));
1160}
1161
1162static int mov_write_uuidusmt_tag (ByteIOContext *pb, AVFormatContext *s)
1163{
1164 size_t len, size;
1165 offset_t pos, curpos;
1166
1167 size = 0;
1168 if (s->title[0]) {
1169 pos = url_ftell(pb);
1170 put_be32(pb, 0); /* size placeholder*/
1171 put_tag(pb, "uuid");
1172 put_tag(pb, "USMT");
1173 put_be32(pb, 0x21d24fce ); /* 96 bit UUID */
1174 put_be32(pb, 0xbb88695c );
1175 put_be32(pb, 0xfac9c740 );
1176 size += 24;
1177
1178 put_be32(pb, 0); /* size placeholder*/
1179 put_tag(pb, "MTDT");
1180 put_be16(pb, 1);
1181 size += 10;
1182
1183 // Title
1184 len = strlen(s->title)+1;
1185 put_be16(pb, len*2+10); /* size */
1186 put_be32(pb, 0x01); /* type */
1187 put_be16(pb, language_code("und")); /* language */
1188 put_be16(pb, 0x01); /* ? */
1189 ascii_to_wc (pb, s->title, len);
1190 size += len*2+10;
1191
1192 // size
1193 curpos = url_ftell(pb);
1194 url_fseek(pb, pos, SEEK_SET);
1195 put_be32(pb, size);
1196 url_fseek(pb, pos+24, SEEK_SET);
1197 put_be32(pb, size-24);
1198 url_fseek(pb, curpos, SEEK_SET);
1199 }
1200
1201 return size;
1202}
1203
69dde1ad
GB
1204static int mov_write_moov_tag(ByteIOContext *pb, MOVContext *mov,
1205 AVFormatContext *s)
1cb5f7fd 1206{
b29af723
MN
1207 int i;
1208 offset_t pos = url_ftell(pb);
1cb5f7fd
MN
1209 put_be32(pb, 0); /* size placeholder*/
1210 put_tag(pb, "moov");
1211 mov->timescale = globalTimescale;
1212
1213 for (i=0; i<MAX_STREAMS; i++) {
e45ccf79
GB
1214 if(mov->tracks[i].entry <= 0) continue;
1215
1216 if(mov->tracks[i].enc->codec_type == CODEC_TYPE_VIDEO) {
c0df9d75
MN
1217 mov->tracks[i].timescale = mov->tracks[i].enc->time_base.den;
1218 mov->tracks[i].sampleDuration = mov->tracks[i].enc->time_base.num;
e45ccf79
GB
1219 }
1220 else if(mov->tracks[i].enc->codec_type == CODEC_TYPE_AUDIO) {
1221 /* If AMR, track timescale = 8000, AMR_WB = 16000 */
1222 if(mov->tracks[i].enc->codec_id == CODEC_ID_AMR_NB) {
1223 mov->tracks[i].sampleDuration = 160; // Bytes per chunk
1224 mov->tracks[i].timescale = 8000;
1cb5f7fd 1225 }
e45ccf79
GB
1226 else {
1227 mov->tracks[i].timescale = mov->tracks[i].enc->sample_rate;
1228 mov->tracks[i].sampleDuration = mov->tracks[i].enc->frame_size;
1cb5f7fd 1229 }
1cb5f7fd 1230 }
e45ccf79 1231
115329f1 1232 mov->tracks[i].trackDuration =
e45ccf79
GB
1233 mov->tracks[i].sampleCount * mov->tracks[i].sampleDuration;
1234 mov->tracks[i].time = mov->time;
1235 mov->tracks[i].trackID = i+1;
1cb5f7fd
MN
1236 }
1237
6e6d6dc0
MN
1238 mov_write_mvhd_tag(pb, mov);
1239 //mov_write_iods_tag(pb, mov);
1cb5f7fd
MN
1240 for (i=0; i<MAX_STREAMS; i++) {
1241 if(mov->tracks[i].entry > 0) {
6e6d6dc0 1242 mov_write_trak_tag(pb, &(mov->tracks[i]));
1cb5f7fd
MN
1243 }
1244 }
1245
dcfdb046
BL
1246 if (mov->mode == MODE_PSP)
1247 mov_write_uuidusmt_tag(pb, s);
1248 else
69dde1ad
GB
1249 mov_write_udta_tag(pb, mov, s);
1250
6e6d6dc0 1251 return updateSize(pb, pos);
1cb5f7fd
MN
1252}
1253
f578f938 1254int mov_write_mdat_tag(ByteIOContext *pb, MOVContext* mov)
1cb5f7fd 1255{
b29af723
MN
1256 put_be32(pb, 8); // placeholder for extended size field (64 bit)
1257 put_tag(pb, "wide");
1258
115329f1 1259 mov->mdat_pos = url_ftell(pb);
1cb5f7fd
MN
1260 put_be32(pb, 0); /* size placeholder*/
1261 put_tag(pb, "mdat");
1262 return 0;
1263}
1264
1265/* TODO: This needs to be more general */
dcfdb046 1266static void mov_write_ftyp_tag (ByteIOContext *pb, AVFormatContext *s)
1cb5f7fd 1267{
69dde1ad
GB
1268 MOVContext *mov = s->priv_data;
1269
1cb5f7fd
MN
1270 put_be32(pb, 0x14 ); /* size */
1271 put_tag(pb, "ftyp");
e45ccf79 1272
69dde1ad 1273 if ( mov->mode == MODE_3GP )
e45ccf79 1274 put_tag(pb, "3gp4");
8536ab89 1275 else if ( mov->mode == MODE_3G2 )
1276 put_tag(pb, "3g2a");
8af18154 1277 else if ( mov->mode == MODE_PSP )
1278 put_tag(pb, "MSNV");
e45ccf79
GB
1279 else
1280 put_tag(pb, "isom");
1281
1cb5f7fd 1282 put_be32(pb, 0x200 );
e45ccf79 1283
69dde1ad 1284 if ( mov->mode == MODE_3GP )
e45ccf79 1285 put_tag(pb, "3gp4");
8536ab89 1286 else if ( mov->mode == MODE_3G2 )
1287 put_tag(pb, "3g2a");
8af18154 1288 else if ( mov->mode == MODE_PSP )
1289 put_tag(pb, "MSNV");
e45ccf79
GB
1290 else
1291 put_tag(pb, "mp41");
1cb5f7fd
MN
1292}
1293
0c716ab7 1294static void mov_write_uuidprof_tag(ByteIOContext *pb, AVFormatContext *s)
8af18154 1295{
dcfdb046
BL
1296 AVCodecContext *VideoCodec = s->streams[0]->codec;
1297 AVCodecContext *AudioCodec = s->streams[1]->codec;
1298 int AudioRate = AudioCodec->sample_rate;
1299 int FrameRate = ((VideoCodec->time_base.den) * (0x10000))/ (VideoCodec->time_base.num);
8af18154 1300
1301 put_be32(pb, 0x94 ); /* size */
1302 put_tag(pb, "uuid");
1303 put_tag(pb, "PROF");
1304
1305 put_be32(pb, 0x21d24fce ); /* 96 bit UUID */
1306 put_be32(pb, 0xbb88695c );
1307 put_be32(pb, 0xfac9c740 );
1308
1309 put_be32(pb, 0x0 ); /* ? */
1310 put_be32(pb, 0x3 ); /* 3 sections ? */
1311
1312 put_be32(pb, 0x14 ); /* size */
1313 put_tag(pb, "FPRF");
1314 put_be32(pb, 0x0 ); /* ? */
1315 put_be32(pb, 0x0 ); /* ? */
1316 put_be32(pb, 0x0 ); /* ? */
1317
1318 put_be32(pb, 0x2c ); /* size */
1319 put_tag(pb, "APRF"); /* audio */
1320 put_be32(pb, 0x0 );
dcfdb046 1321 put_be32(pb, 0x2 ); /* TrackID */
8af18154 1322 put_tag(pb, "mp4a");
1323 put_be32(pb, 0x20f );
1324 put_be32(pb, 0x0 );
dcfdb046
BL
1325 put_be32(pb, AudioCodec->bit_rate / 1000);
1326 put_be32(pb, AudioCodec->bit_rate / 1000);
1327 put_be32(pb, AudioRate );
1328 put_be32(pb, AudioCodec->channels );
8af18154 1329
1330 put_be32(pb, 0x34 ); /* size */
1331 put_tag(pb, "VPRF"); /* video */
1332 put_be32(pb, 0x0 );
dcfdb046 1333 put_be32(pb, 0x1 ); /* TrackID */
8af18154 1334 put_tag(pb, "mp4v");
1335 put_be32(pb, 0x103 );
1336 put_be32(pb, 0x0 );
dcfdb046
BL
1337 put_be32(pb, VideoCodec->bit_rate / 1000);
1338 put_be32(pb, VideoCodec->bit_rate / 1000);
1339 put_be32(pb, FrameRate);
1340 put_be32(pb, FrameRate);
1341 put_be16(pb, VideoCodec->width);
1342 put_be16(pb, VideoCodec->height);
1343 put_be32(pb, 0x010001); /* ? */
8af18154 1344}
1345
1cb5f7fd
MN
1346static int mov_write_header(AVFormatContext *s)
1347{
1348 ByteIOContext *pb = &s->pb;
69dde1ad
GB
1349 MOVContext *mov = s->priv_data;
1350 int i;
1cb5f7fd 1351
ffdd57d4 1352 for(i=0; i<s->nb_streams; i++){
01f4895c 1353 AVCodecContext *c= s->streams[i]->codec;
ffdd57d4
MN
1354
1355 if (c->codec_type == CODEC_TYPE_VIDEO){
1356 if (!codec_get_tag(codec_movvideo_tags, c->codec_id)){
1357 if(!codec_get_tag(codec_bmp_tags, c->codec_id))
1358 return -1;
1359 else
1360 av_log(s, AV_LOG_INFO, "Warning, using MS style video codec tag, the file may be unplayable!\n");
1361 }
1362 }else if(c->codec_type == CODEC_TYPE_AUDIO){
1363 if (!codec_get_tag(codec_movaudio_tags, c->codec_id)){
1364 if(!codec_get_tag(codec_wav_tags, c->codec_id))
1365 return -1;
1366 else
1367 av_log(s, AV_LOG_INFO, "Warning, using MS style audio codec tag, the file may be unplayable!\n");
1368 }
1369 }
ab561df9
FR
1370 /* don't know yet if mp4 or not */
1371 mov->tracks[i].language = ff_mov_iso639_to_lang(s->streams[i]->language, 1);
ffdd57d4
MN
1372 }
1373
69dde1ad
GB
1374 /* Default mode == MP4 */
1375 mov->mode = MODE_MP4;
1376
1377 if (s->oformat != NULL) {
1378 if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
8536ab89 1379 else if (!strcmp("3g2", s->oformat->name)) mov->mode = MODE_3G2;
69dde1ad 1380 else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
8af18154 1381 else if (!strcmp("psp", s->oformat->name)) mov->mode = MODE_PSP;
69dde1ad 1382
8536ab89 1383 if ( mov->mode == MODE_3GP || mov->mode == MODE_3G2 ||
1384 mov->mode == MODE_MP4 || mov->mode == MODE_PSP )
69dde1ad 1385 mov_write_ftyp_tag(pb,s);
0c716ab7
FR
1386 if ( mov->mode == MODE_PSP ) {
1387 if ( s->nb_streams != 2 ) {
1388 av_log(s, AV_LOG_ERROR, "PSP mode need one video and one audio stream\n");
1389 return -1;
1390 }
8af18154 1391 mov_write_uuidprof_tag(pb,s);
0c716ab7 1392 }
69dde1ad
GB
1393 }
1394
1395 for (i=0; i<MAX_STREAMS; i++) {
1396 mov->tracks[i].mode = mov->mode;
f578f938
TR
1397 }
1398
1cb5f7fd
MN
1399 put_flush_packet(pb);
1400
1401 return 0;
1402}
1403
e928649b 1404static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
1cb5f7fd
MN
1405{
1406 MOVContext *mov = s->priv_data;
1407 ByteIOContext *pb = &s->pb;
01f4895c 1408 AVCodecContext *enc = s->streams[pkt->stream_index]->codec;
e928649b 1409 MOVTrack* trk = &mov->tracks[pkt->stream_index];
1a31840c 1410 int cl, id;
e45ccf79 1411 unsigned int samplesInChunk = 0;
e928649b 1412 int size= pkt->size;
1cb5f7fd 1413
e45ccf79
GB
1414 if (url_is_streamed(&s->pb)) return 0; /* Can't handle that */
1415 if (!size) return 0; /* Discard 0 sized packets */
1cb5f7fd 1416
e45ccf79
GB
1417 if (enc->codec_type == CODEC_TYPE_VIDEO ) {
1418 samplesInChunk = 1;
1419 }
1420 else if (enc->codec_type == CODEC_TYPE_AUDIO ) {
1421 if( enc->codec_id == CODEC_ID_AMR_NB) {
f578f938 1422 /* We must find out how many AMR blocks there are in one packet */
e45ccf79
GB
1423 static uint16_t packed_size[16] =
1424 {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 0};
1425 int len = 0;
1426
1427 while (len < size && samplesInChunk < 100) {
e928649b 1428 len += packed_size[(pkt->data[len] >> 3) & 0x0F];
e45ccf79 1429 samplesInChunk++;
f578f938 1430 }
1cb5f7fd 1431 }
e45ccf79
GB
1432 else if(enc->codec_id == CODEC_ID_PCM_ALAW) {
1433 samplesInChunk = size/enc->channels;
1cb5f7fd 1434 }
bb270c08
DB
1435 else if(enc->codec_id == CODEC_ID_PCM_S16BE || enc->codec_id == CODEC_ID_PCM_S16LE) {
1436 samplesInChunk = size/(2*enc->channels);
115329f1 1437 }
e45ccf79
GB
1438 else {
1439 samplesInChunk = 1;
1cb5f7fd 1440 }
e45ccf79
GB
1441 }
1442
1443 if ((enc->codec_id == CODEC_ID_MPEG4 || enc->codec_id == CODEC_ID_AAC)
1444 && trk->vosLen == 0) {
21f52609 1445// assert(enc->extradata_size);
e45ccf79
GB
1446
1447 trk->vosLen = enc->extradata_size;
1448 trk->vosData = av_malloc(trk->vosLen);
1449 memcpy(trk->vosData, enc->extradata, trk->vosLen);
1450 }
1451
1452 cl = trk->entry / MOV_INDEX_CLUSTER_SIZE;
1453 id = trk->entry % MOV_INDEX_CLUSTER_SIZE;
1454
1455 if (trk->ents_allocated <= trk->entry) {
115329f1 1456 trk->cluster = av_realloc(trk->cluster, (cl+1)*sizeof(void*));
e45ccf79
GB
1457 if (!trk->cluster)
1458 return -1;
1459 trk->cluster[cl] = av_malloc(MOV_INDEX_CLUSTER_SIZE*sizeof(MOVIentry));
1460 if (!trk->cluster[cl])
1461 return -1;
1462 trk->ents_allocated += MOV_INDEX_CLUSTER_SIZE;
1463 }
1464 if (mov->mdat_written == 0) {
1465 mov_write_mdat_tag(pb, mov);
1466 mov->mdat_written = 1;
8272de4e 1467 mov->time = s->timestamp + 0x7C25B080; //1970 based -> 1904 based
e45ccf79
GB
1468 }
1469
69dde1ad 1470 trk->cluster[cl][id].pos = url_ftell(pb);
e45ccf79
GB
1471 trk->cluster[cl][id].samplesInChunk = samplesInChunk;
1472 trk->cluster[cl][id].size = size;
1473 trk->cluster[cl][id].entries = samplesInChunk;
1474 if(enc->codec_type == CODEC_TYPE_VIDEO) {
e928649b
MN
1475 trk->cluster[cl][id].key_frame = !!(pkt->flags & PKT_FLAG_KEY);
1476 if(trk->cluster[cl][id].key_frame)
f578f938 1477 trk->hasKeyframes = 1;
1cb5f7fd 1478 }
e45ccf79
GB
1479 trk->enc = enc;
1480 trk->entry++;
1481 trk->sampleCount += samplesInChunk;
1482 trk->mdat_size += size;
1483
e928649b 1484 put_buffer(pb, pkt->data, size);
1cb5f7fd
MN
1485
1486 put_flush_packet(pb);
1487 return 0;
1488}
1489
1490static int mov_write_trailer(AVFormatContext *s)
1491{
1492 MOVContext *mov = s->priv_data;
1493 ByteIOContext *pb = &s->pb;
1494 int res = 0;
b29af723
MN
1495 int i;
1496 uint64_t j;
1cb5f7fd 1497
69dde1ad 1498 offset_t moov_pos = url_ftell(pb);
1cb5f7fd
MN
1499
1500 /* Write size of mdat tag */
69dde1ad 1501 for (i=0, j=0; i<MAX_STREAMS; i++) {
1cb5f7fd
MN
1502 if(mov->tracks[i].ents_allocated > 0) {
1503 j += mov->tracks[i].mdat_size;
1504 }
1505 }
b29af723
MN
1506 if (j+8 <= UINT32_MAX) {
1507 url_fseek(pb, mov->mdat_pos, SEEK_SET);
1508 put_be32(pb, j+8);
1509 } else {
1510 /* overwrite 'wide' placeholder atom */
1511 url_fseek(pb, mov->mdat_pos - 8, SEEK_SET);
1512 put_be32(pb, 1); /* special value: real atom size will be 64 bit value after tag field */
1513 put_tag(pb, "mdat");
1514 put_be64(pb, j+16);
1515 }
69dde1ad 1516 url_fseek(pb, moov_pos, SEEK_SET);
1cb5f7fd 1517
69dde1ad 1518 mov_write_moov_tag(pb, mov, s);
1cb5f7fd
MN
1519
1520 for (i=0; i<MAX_STREAMS; i++) {
1521 for (j=0; j<mov->tracks[i].ents_allocated/MOV_INDEX_CLUSTER_SIZE; j++) {
1522 av_free(mov->tracks[i].cluster[j]);
1523 }
1524 av_free(mov->tracks[i].cluster);
ec7d0d2e
GB
1525 if( mov->tracks[i].vosLen ) av_free( mov->tracks[i].vosData );
1526
1cb5f7fd
MN
1527 mov->tracks[i].cluster = NULL;
1528 mov->tracks[i].ents_allocated = mov->tracks[i].entry = 0;
1529 }
69dde1ad 1530
1cb5f7fd
MN
1531 put_flush_packet(pb);
1532
1533 return res;
1534}
1535
1536static AVOutputFormat mov_oformat = {
1537 "mov",
1538 "mov format",
1539 NULL,
1540 "mov",
1541 sizeof(MOVContext),
69dde1ad 1542 CODEC_ID_AAC,
2187d948 1543 CODEC_ID_MPEG4,
1cb5f7fd
MN
1544 mov_write_header,
1545 mov_write_packet,
1546 mov_write_trailer,
c64d476c 1547 .flags = AVFMT_GLOBALHEADER,
1cb5f7fd
MN
1548};
1549
1550static AVOutputFormat _3gp_oformat = {
1551 "3gp",
1552 "3gp format",
1553 NULL,
1554 "3gp",
1555 sizeof(MOVContext),
1556 CODEC_ID_AMR_NB,
1557 CODEC_ID_H263,
1558 mov_write_header,
1559 mov_write_packet,
1560 mov_write_trailer,
c64d476c 1561 .flags = AVFMT_GLOBALHEADER,
1cb5f7fd
MN
1562};
1563
1564static AVOutputFormat mp4_oformat = {
1565 "mp4",
1566 "mp4 format",
4cb3f3b6
DC
1567 "application/mp4",
1568 "mp4,m4a",
1cb5f7fd
MN
1569 sizeof(MOVContext),
1570 CODEC_ID_AAC,
1571 CODEC_ID_MPEG4,
1572 mov_write_header,
1573 mov_write_packet,
1574 mov_write_trailer,
c64d476c 1575 .flags = AVFMT_GLOBALHEADER,
1cb5f7fd
MN
1576};
1577
8af18154 1578static AVOutputFormat psp_oformat = {
1579 "psp",
1580 "psp mp4 format",
1581 NULL,
1582 "mp4,psp",
1583 sizeof(MOVContext),
1584 CODEC_ID_AAC,
1585 CODEC_ID_MPEG4,
1586 mov_write_header,
1587 mov_write_packet,
1588 mov_write_trailer,
dcfdb046 1589 .flags = AVFMT_GLOBALHEADER,
8af18154 1590};
1591
8536ab89 1592static AVOutputFormat _3g2_oformat = {
1593 "3g2",
1594 "3gp2 format",
1595 NULL,
1596 "3g2",
1597 sizeof(MOVContext),
1598 CODEC_ID_AMR_NB,
1599 CODEC_ID_H263,
1600 mov_write_header,
1601 mov_write_packet,
1602 mov_write_trailer,
c64d476c 1603 .flags = AVFMT_GLOBALHEADER,
8536ab89 1604};
1605
1cb5f7fd
MN
1606int movenc_init(void)
1607{
1608 av_register_output_format(&mov_oformat);
1609 av_register_output_format(&_3gp_oformat);
1610 av_register_output_format(&mp4_oformat);
8af18154 1611 av_register_output_format(&psp_oformat);
8536ab89 1612 av_register_output_format(&_3g2_oformat);
1cb5f7fd
MN
1613 return 0;
1614}