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