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