changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext...
[libav.git] / libavformat / movenc.c
1 /*
2 * MOV, 3GP, MP4 encoder.
3 * Copyright (c) 2003 Thomas Raivio.
4 * Copyright (c) 2004 Gildas Bazin <gbazin at videolan dot org>.
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"
21 #include "avi.h"
22 #include "avio.h"
23
24 #undef NDEBUG
25 #include <assert.h>
26
27 #define MOV_INDEX_CLUSTER_SIZE 16384
28 #define globalTimescale 1000
29
30 #define MODE_MP4 0
31 #define MODE_MOV 1
32 #define MODE_3GP 2
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
35 #define MODE_3G2 4
36
37 typedef struct MOVIentry {
38 unsigned int flags, pos, size;
39 unsigned int samplesInChunk;
40 char key_frame;
41 unsigned int entries;
42 } MOVIentry;
43
44 typedef struct MOVIndex {
45 int mode;
46 int entry;
47 int mdat_size;
48 int ents_allocated;
49 long timescale;
50 long time;
51 long trackDuration;
52 long sampleCount;
53 long sampleDuration;
54 int hasKeyframes;
55 int trackID;
56 AVCodecContext *enc;
57
58 int vosLen;
59 uint8_t *vosData;
60 MOVIentry** cluster;
61 } MOVTrack;
62
63 typedef struct MOVContext {
64 int mode;
65 long time;
66 int nb_streams;
67 int mdat_written;
68 offset_t mdat_pos;
69 long timescale;
70 MOVTrack tracks[MAX_STREAMS];
71 } MOVContext;
72
73 static int mov_write_esds_tag(ByteIOContext *pb, MOVTrack* track);
74
75 const 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
82 { CODEC_ID_H264 , 33 },
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
95 //FIXME supprt 64bit varaint with wide placeholders
96 static int updateSize (ByteIOContext *pb, int pos)
97 {
98 long curpos = url_ftell(pb);
99 url_fseek(pb, pos, SEEK_SET);
100 put_be32(pb, curpos - pos); /* rewrite size */
101 url_fseek(pb, curpos, SEEK_SET);
102
103 return curpos - pos;
104 }
105
106 /* Chunk offset atom */
107 static int mov_write_stco_tag(ByteIOContext *pb, MOVTrack* track)
108 {
109 int i;
110 int pos = url_ftell(pb);
111 put_be32(pb, 0); /* size */
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 }
120 return updateSize (pb, pos);
121 }
122
123 /* Sample size atom */
124 static int mov_write_stsz_tag(ByteIOContext *pb, MOVTrack* track)
125 {
126 int equalChunks = 1;
127 int i, j, entries = 0, tst = -1, oldtst = -1;
128
129 int pos = url_ftell(pb);
130 put_be32(pb, 0); /* size */
131 put_tag(pb, "stsz");
132 put_be32(pb, 0); /* version & flags */
133
134 for (i=0; i<track->entry; i++) {
135 int cl = i / MOV_INDEX_CLUSTER_SIZE;
136 int id = i % MOV_INDEX_CLUSTER_SIZE;
137 tst = track->cluster[cl][id].size/track->cluster[cl][id].entries;
138 if(oldtst != -1 && tst != oldtst) {
139 equalChunks = 0;
140 }
141 oldtst = tst;
142 entries += track->cluster[cl][id].entries;
143 }
144 if (equalChunks) {
145 int sSize = track->cluster[0][0].size/track->cluster[0][0].entries;
146 put_be32(pb, sSize); // sample size
147 put_be32(pb, entries); // sample count
148 }
149 else {
150 put_be32(pb, 0); // sample size
151 put_be32(pb, entries); // sample count
152 for (i=0; i<track->entry; i++) {
153 int cl = i / MOV_INDEX_CLUSTER_SIZE;
154 int id = i % MOV_INDEX_CLUSTER_SIZE;
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 }
159 }
160 }
161 return updateSize (pb, pos);
162 }
163
164 /* Sample to chunk atom */
165 static int mov_write_stsc_tag(ByteIOContext *pb, MOVTrack* track)
166 {
167 int index = 0, oldval = -1, i, entryPos, curpos;
168
169 int pos = url_ftell(pb);
170 put_be32(pb, 0); /* size */
171 put_tag(pb, "stsc");
172 put_be32(pb, 0); // version & flags
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;
178 if(oldval != track->cluster[cl][id].samplesInChunk)
179 {
180 put_be32(pb, i+1); // first chunk
181 put_be32(pb, track->cluster[cl][id].samplesInChunk); // samples per chunk
182 put_be32(pb, 0x1); // sample description index
183 oldval = track->cluster[cl][id].samplesInChunk;
184 index++;
185 }
186 }
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);
191
192 return updateSize (pb, pos);
193 }
194
195 /* Sync sample atom */
196 static int mov_write_stss_tag(ByteIOContext *pb, MOVTrack* track)
197 {
198 long curpos;
199 int i, index = 0, entryPos;
200 int pos = url_ftell(pb);
201 put_be32(pb, 0); // size
202 put_tag(pb, "stss");
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);
219 }
220
221 static int mov_write_damr_tag(ByteIOContext *pb)
222 {
223 put_be32(pb, 0x11); /* size */
224 put_tag(pb, "damr");
225 put_tag(pb, "FFMP");
226 put_byte(pb, 0);
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) */
232 return 0x11;
233 }
234
235 static 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
262 static const CodecTag codec_movaudio_tags[] = {
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') },
270 { CODEC_ID_AMR_WB, MKTAG('s', 'a', 'w', 'b') },
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
277 static int mov_write_audio_tag(ByteIOContext *pb, MOVTrack* track)
278 {
279 int pos = url_ftell(pb);
280 int tag;
281
282 put_be32(pb, 0); /* size */
283
284 tag = track->enc->codec_tag;
285 if (!tag)
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);
291 tag = MKTAG('m', 's', ((tmp >> 8) & 0xff), (tmp & 0xff));
292 }
293 put_le32(pb, tag); // store it byteswapped
294
295 put_be32(pb, 0); /* Reserved */
296 put_be16(pb, 0); /* Reserved */
297 put_be16(pb, 1); /* Data-reference index, XXX == 1 */
298
299 /* SoundDescription */
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 */
304 put_be16(pb, 0); /* Revision level */
305 put_be32(pb, 0); /* Reserved */
306
307 put_be16(pb, track->enc->channels); /* Number of channels */
308 /* TODO: Currently hard-coded to 16-bit, there doesn't seem
309 to be a good way to get number of bits of audio */
310 put_be16(pb, 0x10); /* Reserved */
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 }
321 put_be16(pb, 0); /* packet size (= 0) */
322 put_be16(pb, track->timescale); /* Time scale */
323 put_be16(pb, 0); /* Reserved */
324
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 }
338 if(track->enc->codec_id == CODEC_ID_AMR_NB)
339 mov_write_damr_tag(pb);
340 return updateSize (pb, pos);
341 }
342
343 static int mov_write_d263_tag(ByteIOContext *pb)
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
353 /* TODO: No idea about these values */
354 static int mov_write_svq3_tag(ByteIOContext *pb)
355 {
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;
364 }
365
366 static 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
378 static void putDescr(ByteIOContext *pb, int tag, int size)
379 {
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 }
416 }
417
418 static int mov_write_esds_tag(ByteIOContext *pb, MOVTrack* track) // Basic
419 {
420 int decoderSpecificInfoLen;
421 int pos = url_ftell(pb);
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 }
444
445 decoderSpecificInfoLen = track->vosLen ? descrLength(track->vosLen):0;
446
447 put_be32(pb, 0); // size
448 put_tag(pb, "esds");
449 put_be32(pb, 0); // Version
450
451 // ES descriptor
452 putDescr(pb, 0x03, 3 + descrLength(13 + decoderSpecificInfoLen) +
453 descrLength(1));
454 put_be16(pb, track->trackID);
455 put_byte(pb, 0x00); // flags (= no flags)
456
457 // DecoderConfig descriptor
458 putDescr(pb, 0x04, 13 + decoderSpecificInfoLen);
459
460 // Object type indication
461 put_byte(pb, codec_get_tag(ff_mov_obj_type, track->enc->codec_id));
462
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)
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
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
472
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
478
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
486 track->vosData = vosDataBackup;
487 track->vosLen = vosLenBackup;
488
489 // SL descriptor
490 putDescr(pb, 0x06, 1);
491 put_byte(pb, 0x02);
492 return updateSize (pb, pos);
493 }
494
495 static const CodecTag codec_movvideo_tags[] = {
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') },
500 { CODEC_ID_H264, MKTAG('a', 'v', 'c', '1') },
501 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', ' ') },
502 { 0, 0 },
503 };
504
505 static int mov_write_video_tag(ByteIOContext *pb, MOVTrack* track)
506 {
507 int pos = url_ftell(pb);
508 char compressor_name[32];
509 int tag;
510
511 put_be32(pb, 0); /* size */
512
513 tag = track->enc->codec_tag;
514 if (!tag)
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);
519 put_le32(pb, tag); // store it byteswapped
520
521 put_be32(pb, 0); /* Reserved */
522 put_be16(pb, 0); /* Reserved */
523 put_be16(pb, 1); /* Data-reference index */
524
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 }
535 put_be16(pb, track->enc->width); /* Video width */
536 put_be16(pb, track->enc->height); /* Video height */
537 put_be32(pb, 0x00480000); /* Horizontal resolution 72dpi */
538 put_be32(pb, 0x00480000); /* Vertical resolution 72dpi */
539 put_be32(pb, 0); /* Data size (= 0) */
540 put_be16(pb, 1); /* Frame count (= 1) */
541
542 memset(compressor_name,0,32);
543 if (track->enc->codec && track->enc->codec->name)
544 strncpy(compressor_name,track->enc->codec->name,31);
545 put_byte(pb, strlen(compressor_name));
546 put_buffer(pb, compressor_name, 31);
547
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);
558 }
559
560 static int mov_write_stsd_tag(ByteIOContext *pb, MOVTrack* track)
561 {
562 int pos = url_ftell(pb);
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 */
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);
571 return updateSize(pb, pos);
572 }
573
574 /* TODO: */
575 /* Time to sample atom */
576 static int mov_write_stts_tag(ByteIOContext *pb, MOVTrack* track)
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
583 put_be32(pb, track->sampleCount); /* sample count */
584 put_be32(pb, track->sampleDuration); /* sample duration */
585 return 0x18;
586 }
587
588 static int mov_write_dref_tag(ByteIOContext *pb)
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
602 static int mov_write_stbl_tag(ByteIOContext *pb, MOVTrack* track)
603 {
604 int pos = url_ftell(pb);
605 put_be32(pb, 0); /* size */
606 put_tag(pb, "stbl");
607 mov_write_stsd_tag(pb, track);
608 mov_write_stts_tag(pb, track);
609 if (track->enc->codec_type == CODEC_TYPE_VIDEO &&
610 track->hasKeyframes)
611 mov_write_stss_tag(pb, track);
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);
616 }
617
618 static int mov_write_dinf_tag(ByteIOContext *pb)
619 {
620 int pos = url_ftell(pb);
621 put_be32(pb, 0); /* size */
622 put_tag(pb, "dinf");
623 mov_write_dref_tag(pb);
624 return updateSize(pb, pos);
625 }
626
627 static int mov_write_smhd_tag(ByteIOContext *pb)
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
637 static int mov_write_vmhd_tag(ByteIOContext *pb)
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
646 static int mov_write_hdlr_tag(ByteIOContext *pb, MOVTrack* track)
647 {
648 char *descr, *hdlr, *hdlr_type;
649 int pos = url_ftell(pb);
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
666 put_be32(pb, 0); /* size */
667 put_tag(pb, "hdlr");
668 put_be32(pb, 0); /* Version & flags */
669 put_buffer(pb, hdlr, 4); /* handler */
670 put_tag(pb, hdlr_type); /* handler type */
671 put_be32(pb ,0); /* reserved */
672 put_be32(pb ,0); /* reserved */
673 put_be32(pb ,0); /* reserved */
674 put_byte(pb, strlen(descr)); /* string counter */
675 put_buffer(pb, descr, strlen(descr)); /* handler description */
676 return updateSize(pb, pos);
677 }
678
679 static 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");
684 if(track->enc->codec_type == CODEC_TYPE_VIDEO)
685 mov_write_vmhd_tag(pb);
686 else
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);
692 return updateSize(pb, pos);
693 }
694
695 static int mov_write_mdhd_tag(ByteIOContext *pb, MOVTrack* track)
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 */
702 put_be32(pb, track->timescale); /* time scale (sample rate for audio) */
703 put_be32(pb, track->trackDuration); /* duration */
704 put_be16(pb, 0); /* language, 0 = english */
705 put_be16(pb, 0); /* reserved (quality) */
706 return 32;
707 }
708
709 static int mov_write_mdia_tag(ByteIOContext *pb, MOVTrack* track)
710 {
711 int pos = url_ftell(pb);
712 put_be32(pb, 0); /* size */
713 put_tag(pb, "mdia");
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);
718 }
719
720 static int mov_write_tkhd_tag(ByteIOContext *pb, MOVTrack* track)
721 {
722 put_be32(pb, 0x5c); /* size (always 0x5c) */
723 put_tag(pb, "tkhd");
724 put_be32(pb, 0xf); /* version & flags (track enabled) */
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 */
729 put_be32(pb, av_rescale_rnd(track->trackDuration, globalTimescale, track->timescale, AV_ROUND_UP)); /* duration */
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) {
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);
757 put_be32(pb, track->enc->height*0x10000);
758 }
759 else {
760 put_be32(pb, 0);
761 put_be32(pb, 0);
762 }
763 return 0x5c;
764 }
765
766 // This box seems important for the psp playback ... without it the movie seems to hang
767 static int mov_write_edts_tag(ByteIOContext *pb, MOVTrack *track)
768 {
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
776 put_be32(pb, av_rescale_rnd(track->trackDuration, globalTimescale, track->timescale, AV_ROUND_UP)); /* duration ... doesn't seem to effect psp */
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)
784 static 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
802 static int mov_write_trak_tag(ByteIOContext *pb, MOVTrack* track)
803 {
804 int pos = url_ftell(pb);
805 put_be32(pb, 0); /* size */
806 put_tag(pb, "trak");
807 mov_write_tkhd_tag(pb, track);
808 if (track->mode == MODE_PSP)
809 mov_write_edts_tag(pb, track); // PSP Movies require edts box
810 mov_write_mdia_tag(pb, track);
811 if (track->mode == MODE_PSP)
812 mov_write_uuid_tag_psp(pb,track); // PSP Movies require this uuid box
813 return updateSize(pb, pos);
814 }
815
816 #if 0
817 /* TODO: Not sorted out, but not necessary either */
818 static int mov_write_iods_tag(ByteIOContext *pb, MOVContext *mov)
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 }
830 #endif
831
832 static int mov_write_mvhd_tag(ByteIOContext *pb, MOVContext *mov)
833 {
834 int maxTrackID = 1, maxTrackLen = 0, i;
835 int64_t maxTrackLenTemp;
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) {
845 maxTrackLenTemp = av_rescale_rnd(mov->tracks[i].trackDuration, globalTimescale, mov->tracks[i].timescale, AV_ROUND_UP);
846 if(maxTrackLen < maxTrackLenTemp)
847 maxTrackLen = maxTrackLenTemp;
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
881 static 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 */
898 static 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 */
911 static 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 */
926 static 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 */
942 static 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 */
957 static 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 */
972 static 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 */
989 static 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 */
1000 static 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 */
1015 static 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 */
1030 static 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 */
1056 static 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 */
1075 static 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
1094 static 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
1103 /* iTunes meta data */
1104 mov_write_meta_tag(pb, mov, s);
1105
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 */
1124 if(mov->tracks[0].enc && !(mov->tracks[0].enc->flags & CODEC_FLAG_BITEXACT))
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
1171 static int mov_write_moov_tag(ByteIOContext *pb, MOVContext *mov,
1172 AVFormatContext *s)
1173 {
1174 int pos, i;
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++) {
1181 if(mov->tracks[i].entry <= 0) continue;
1182
1183 if(mov->tracks[i].enc->codec_type == CODEC_TYPE_VIDEO) {
1184 mov->tracks[i].timescale = mov->tracks[i].enc->time_base.den;
1185 mov->tracks[i].sampleDuration = mov->tracks[i].enc->time_base.num;
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;
1192 }
1193 else {
1194 mov->tracks[i].timescale = mov->tracks[i].enc->sample_rate;
1195 mov->tracks[i].sampleDuration = mov->tracks[i].enc->frame_size;
1196 }
1197 }
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;
1203 }
1204
1205 mov_write_mvhd_tag(pb, mov);
1206 //mov_write_iods_tag(pb, mov);
1207 for (i=0; i<MAX_STREAMS; i++) {
1208 if(mov->tracks[i].entry > 0) {
1209 mov_write_trak_tag(pb, &(mov->tracks[i]));
1210 }
1211 }
1212
1213 mov_write_udta_tag(pb, mov, s);
1214
1215 return updateSize(pb, pos);
1216 }
1217
1218 int mov_write_mdat_tag(ByteIOContext *pb, MOVContext* mov)
1219 {
1220 mov->mdat_pos = url_ftell(pb);
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 */
1227 int mov_write_ftyp_tag(ByteIOContext *pb, AVFormatContext *s)
1228 {
1229 MOVContext *mov = s->priv_data;
1230
1231 put_be32(pb, 0x14 ); /* size */
1232 put_tag(pb, "ftyp");
1233
1234 if ( mov->mode == MODE_3GP )
1235 put_tag(pb, "3gp4");
1236 else if ( mov->mode == MODE_3G2 )
1237 put_tag(pb, "3g2a");
1238 else if ( mov->mode == MODE_PSP )
1239 put_tag(pb, "MSNV");
1240 else
1241 put_tag(pb, "isom");
1242
1243 put_be32(pb, 0x200 );
1244
1245 if ( mov->mode == MODE_3GP )
1246 put_tag(pb, "3gp4");
1247 else if ( mov->mode == MODE_3G2 )
1248 put_tag(pb, "3g2a");
1249 else if ( mov->mode == MODE_PSP )
1250 put_tag(pb, "MSNV");
1251 else
1252 put_tag(pb, "mp41");
1253
1254 return 0x14;
1255 }
1256
1257 static void mov_write_uuidprof_tag(ByteIOContext *pb, AVFormatContext *s)
1258 {
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);
1261
1262 //printf("audiorate = %d\n",AudioRate);
1263 //printf("framerate = %d / %d = 0x%x\n",s->streams[0]->codec->time_base.den,s->streams[0]->codec->time_base.num,FrameRate);
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
1305 put_be16(pb, s->streams[0]->codec->width);
1306 put_be16(pb, s->streams[0]->codec->height);
1307 put_be32(pb, 0x010001 );
1308 }
1309
1310 static int mov_write_header(AVFormatContext *s)
1311 {
1312 ByteIOContext *pb = &s->pb;
1313 MOVContext *mov = s->priv_data;
1314 int i;
1315
1316 for(i=0; i<s->nb_streams; i++){
1317 AVCodecContext *c= s->streams[i]->codec;
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
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;
1341 else if (!strcmp("3g2", s->oformat->name)) mov->mode = MODE_3G2;
1342 else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
1343 else if (!strcmp("psp", s->oformat->name)) mov->mode = MODE_PSP;
1344
1345 if ( mov->mode == MODE_3GP || mov->mode == MODE_3G2 ||
1346 mov->mode == MODE_MP4 || mov->mode == MODE_PSP )
1347 mov_write_ftyp_tag(pb,s);
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 }
1353 mov_write_uuidprof_tag(pb,s);
1354 }
1355 }
1356
1357 for (i=0; i<MAX_STREAMS; i++) {
1358 mov->tracks[i].mode = mov->mode;
1359 }
1360
1361 put_flush_packet(pb);
1362
1363 return 0;
1364 }
1365
1366 static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
1367 {
1368 MOVContext *mov = s->priv_data;
1369 ByteIOContext *pb = &s->pb;
1370 AVCodecContext *enc = s->streams[pkt->stream_index]->codec;
1371 MOVTrack* trk = &mov->tracks[pkt->stream_index];
1372 int cl, id;
1373 unsigned int samplesInChunk = 0;
1374 int size= pkt->size;
1375
1376 if (url_is_streamed(&s->pb)) return 0; /* Can't handle that */
1377 if (!size) return 0; /* Discard 0 sized packets */
1378
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) {
1384 /* We must find out how many AMR blocks there are in one packet */
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) {
1390 len += packed_size[(pkt->data[len] >> 3) & 0x0F];
1391 samplesInChunk++;
1392 }
1393 }
1394 else if(enc->codec_id == CODEC_ID_PCM_ALAW) {
1395 samplesInChunk = size/enc->channels;
1396 }
1397 else if(enc->codec_id == CODEC_ID_PCM_S16BE || enc->codec_id == CODEC_ID_PCM_S16LE) {
1398 samplesInChunk = size/(2*enc->channels);
1399 }
1400 else {
1401 samplesInChunk = 1;
1402 }
1403 }
1404
1405 if ((enc->codec_id == CODEC_ID_MPEG4 || enc->codec_id == CODEC_ID_AAC)
1406 && trk->vosLen == 0) {
1407 // assert(enc->extradata_size);
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;
1429 mov->time = s->timestamp + 0x7C25B080; //1970 based -> 1904 based
1430 }
1431
1432 trk->cluster[cl][id].pos = url_ftell(pb);
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) {
1437 trk->cluster[cl][id].key_frame = !!(pkt->flags & PKT_FLAG_KEY);
1438 if(trk->cluster[cl][id].key_frame)
1439 trk->hasKeyframes = 1;
1440 }
1441 trk->enc = enc;
1442 trk->entry++;
1443 trk->sampleCount += samplesInChunk;
1444 trk->mdat_size += size;
1445
1446 put_buffer(pb, pkt->data, size);
1447
1448 put_flush_packet(pb);
1449 return 0;
1450 }
1451
1452 static 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;
1458
1459 offset_t moov_pos = url_ftell(pb);
1460
1461 /* Write size of mdat tag */
1462 for (i=0, j=0; i<MAX_STREAMS; i++) {
1463 if(mov->tracks[i].ents_allocated > 0) {
1464 j += mov->tracks[i].mdat_size;
1465 }
1466 }
1467 url_fseek(pb, mov->mdat_pos, SEEK_SET);
1468 put_be32(pb, j+8);
1469 url_fseek(pb, moov_pos, SEEK_SET);
1470
1471 mov_write_moov_tag(pb, mov, s);
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);
1478 if( mov->tracks[i].vosLen ) av_free( mov->tracks[i].vosData );
1479
1480 mov->tracks[i].cluster = NULL;
1481 mov->tracks[i].ents_allocated = mov->tracks[i].entry = 0;
1482 }
1483
1484 put_flush_packet(pb);
1485
1486 return res;
1487 }
1488
1489 static AVOutputFormat mov_oformat = {
1490 "mov",
1491 "mov format",
1492 NULL,
1493 "mov",
1494 sizeof(MOVContext),
1495 CODEC_ID_AAC,
1496 CODEC_ID_MPEG4,
1497 mov_write_header,
1498 mov_write_packet,
1499 mov_write_trailer,
1500 .flags = AVFMT_GLOBALHEADER,
1501 };
1502
1503 static 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,
1514 .flags = AVFMT_GLOBALHEADER,
1515 };
1516
1517 static AVOutputFormat mp4_oformat = {
1518 "mp4",
1519 "mp4 format",
1520 "application/mp4",
1521 "mp4,m4a",
1522 sizeof(MOVContext),
1523 CODEC_ID_AAC,
1524 CODEC_ID_MPEG4,
1525 mov_write_header,
1526 mov_write_packet,
1527 mov_write_trailer,
1528 .flags = AVFMT_GLOBALHEADER,
1529 };
1530
1531 static 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,
1542 // .flags = AVFMT_GLOBALHEADER,
1543 };
1544
1545 static 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,
1556 .flags = AVFMT_GLOBALHEADER,
1557 };
1558
1559 int 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);
1564 av_register_output_format(&psp_oformat);
1565 av_register_output_format(&_3g2_oformat);
1566 return 0;
1567 }