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