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