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