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