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