3abe388588aa44ada0a36df2bdc460564e0abcf4
[libav.git] / libavformat / movenc.c
1 /*
2 * MOV, 3GP, MP4 muxer
3 * Copyright (c) 2003 Thomas Raivio
4 * Copyright (c) 2004 Gildas Bazin <gbazin at videolan dot org>
5 * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
6 *
7 * This file is part of FFmpeg.
8 *
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #include "avformat.h"
25 #include "riff.h"
26 #include "avio.h"
27 #include "isom.h"
28 #include "avc.h"
29 #include "libavcodec/get_bits.h"
30 #include "libavcodec/put_bits.h"
31
32 #undef NDEBUG
33 #include <assert.h>
34
35 #define MOV_INDEX_CLUSTER_SIZE 16384
36 #define globalTimescale 1000
37
38 #define MODE_MP4 0x01
39 #define MODE_MOV 0x02
40 #define MODE_3GP 0x04
41 #define MODE_PSP 0x08 // example working PSP command line:
42 // ffmpeg -i testinput.avi -f psp -r 14.985 -s 320x240 -b 768 -ar 24000 -ab 32 M4V00001.MP4
43 #define MODE_3G2 0x10
44 #define MODE_IPOD 0x20
45
46 typedef struct MOVIentry {
47 unsigned int size;
48 uint64_t pos;
49 unsigned int samplesInChunk;
50 unsigned int entries;
51 int cts;
52 int64_t dts;
53 #define MOV_SYNC_SAMPLE 0x0001
54 #define MOV_PARTIAL_SYNC_SAMPLE 0x0002
55 uint32_t flags;
56 } MOVIentry;
57
58 typedef struct MOVIndex {
59 int mode;
60 int entry;
61 long timescale;
62 long time;
63 int64_t trackDuration;
64 long sampleCount;
65 long sampleSize;
66 int hasKeyframes;
67 #define MOV_TRACK_CTTS 0x0001
68 #define MOV_TRACK_STPS 0x0002
69 uint32_t flags;
70 int language;
71 int trackID;
72 int tag; ///< stsd fourcc
73 AVCodecContext *enc;
74
75 int vosLen;
76 uint8_t *vosData;
77 MOVIentry *cluster;
78 int audio_vbr;
79 int height; ///< active picture (w/o VBI) height for D-10/IMX
80 } MOVTrack;
81
82 typedef struct MOVMuxContext {
83 int mode;
84 int64_t time;
85 int nb_streams;
86 int64_t mdat_pos;
87 uint64_t mdat_size;
88 long timescale;
89 MOVTrack *tracks;
90 } MOVMuxContext;
91
92 //FIXME support 64 bit variant with wide placeholders
93 static int64_t updateSize(ByteIOContext *pb, int64_t pos)
94 {
95 int64_t curpos = url_ftell(pb);
96 url_fseek(pb, pos, SEEK_SET);
97 put_be32(pb, curpos - pos); /* rewrite size */
98 url_fseek(pb, curpos, SEEK_SET);
99
100 return curpos - pos;
101 }
102
103 /* Chunk offset atom */
104 static int mov_write_stco_tag(ByteIOContext *pb, MOVTrack *track)
105 {
106 int i;
107 int mode64 = 0; // use 32 bit size variant if possible
108 int64_t pos = url_ftell(pb);
109 put_be32(pb, 0); /* size */
110 if (pos > UINT32_MAX) {
111 mode64 = 1;
112 put_tag(pb, "co64");
113 } else
114 put_tag(pb, "stco");
115 put_be32(pb, 0); /* version & flags */
116 put_be32(pb, track->entry); /* entry count */
117 for (i=0; i<track->entry; i++) {
118 if(mode64 == 1)
119 put_be64(pb, track->cluster[i].pos);
120 else
121 put_be32(pb, track->cluster[i].pos);
122 }
123 return updateSize(pb, pos);
124 }
125
126 /* Sample size atom */
127 static int mov_write_stsz_tag(ByteIOContext *pb, MOVTrack *track)
128 {
129 int equalChunks = 1;
130 int i, j, entries = 0, tst = -1, oldtst = -1;
131
132 int64_t pos = url_ftell(pb);
133 put_be32(pb, 0); /* size */
134 put_tag(pb, "stsz");
135 put_be32(pb, 0); /* version & flags */
136
137 for (i=0; i<track->entry; i++) {
138 tst = track->cluster[i].size/track->cluster[i].entries;
139 if(oldtst != -1 && tst != oldtst) {
140 equalChunks = 0;
141 }
142 oldtst = tst;
143 entries += track->cluster[i].entries;
144 }
145 if (equalChunks) {
146 int sSize = track->cluster[0].size/track->cluster[0].entries;
147 put_be32(pb, sSize); // sample size
148 put_be32(pb, entries); // sample count
149 }
150 else {
151 put_be32(pb, 0); // sample size
152 put_be32(pb, entries); // sample count
153 for (i=0; i<track->entry; i++) {
154 for (j=0; j<track->cluster[i].entries; j++) {
155 put_be32(pb, track->cluster[i].size /
156 track->cluster[i].entries);
157 }
158 }
159 }
160 return updateSize(pb, pos);
161 }
162
163 /* Sample to chunk atom */
164 static int mov_write_stsc_tag(ByteIOContext *pb, MOVTrack *track)
165 {
166 int index = 0, oldval = -1, i;
167 int64_t entryPos, curpos;
168
169 int64_t 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 if(oldval != track->cluster[i].samplesInChunk)
177 {
178 put_be32(pb, i+1); // first chunk
179 put_be32(pb, track->cluster[i].samplesInChunk); // samples per chunk
180 put_be32(pb, 0x1); // sample description index
181 oldval = track->cluster[i].samplesInChunk;
182 index++;
183 }
184 }
185 curpos = url_ftell(pb);
186 url_fseek(pb, entryPos, SEEK_SET);
187 put_be32(pb, index); // rewrite size
188 url_fseek(pb, curpos, SEEK_SET);
189
190 return updateSize(pb, pos);
191 }
192
193 /* Sync sample atom */
194 static int mov_write_stss_tag(ByteIOContext *pb, MOVTrack *track, uint32_t flag)
195 {
196 int64_t curpos, entryPos;
197 int i, index = 0;
198 int64_t pos = url_ftell(pb);
199 put_be32(pb, 0); // size
200 put_tag(pb, flag == MOV_SYNC_SAMPLE ? "stss" : "stps");
201 put_be32(pb, 0); // version & flags
202 entryPos = url_ftell(pb);
203 put_be32(pb, track->entry); // entry count
204 for (i=0; i<track->entry; i++) {
205 if (track->cluster[i].flags & flag) {
206 put_be32(pb, i+1);
207 index++;
208 }
209 }
210 curpos = url_ftell(pb);
211 url_fseek(pb, entryPos, SEEK_SET);
212 put_be32(pb, index); // rewrite size
213 url_fseek(pb, curpos, SEEK_SET);
214 return updateSize(pb, pos);
215 }
216
217 static int mov_write_amr_tag(ByteIOContext *pb, MOVTrack *track)
218 {
219 put_be32(pb, 0x11); /* size */
220 if (track->mode == MODE_MOV) put_tag(pb, "samr");
221 else put_tag(pb, "damr");
222 put_tag(pb, "FFMP");
223 put_byte(pb, 0); /* decoder version */
224
225 put_be16(pb, 0x81FF); /* Mode set (all modes for AMR_NB) */
226 put_byte(pb, 0x00); /* Mode change period (no restriction) */
227 put_byte(pb, 0x01); /* Frames per sample */
228 return 0x11;
229 }
230
231 static int mov_write_ac3_tag(ByteIOContext *pb, MOVTrack *track)
232 {
233 GetBitContext gbc;
234 PutBitContext pbc;
235 uint8_t buf[3];
236 int fscod, bsid, bsmod, acmod, lfeon, frmsizecod;
237
238 if (track->vosLen < 7)
239 return -1;
240
241 put_be32(pb, 11);
242 put_tag(pb, "dac3");
243
244 init_get_bits(&gbc, track->vosData+4, track->vosLen-4);
245 fscod = get_bits(&gbc, 2);
246 frmsizecod = get_bits(&gbc, 6);
247 bsid = get_bits(&gbc, 5);
248 bsmod = get_bits(&gbc, 3);
249 acmod = get_bits(&gbc, 3);
250 if (acmod == 2) {
251 skip_bits(&gbc, 2); // dsurmod
252 } else {
253 if ((acmod & 1) && acmod != 1)
254 skip_bits(&gbc, 2); // cmixlev
255 if (acmod & 4)
256 skip_bits(&gbc, 2); // surmixlev
257 }
258 lfeon = get_bits1(&gbc);
259
260 init_put_bits(&pbc, buf, sizeof(buf));
261 put_bits(&pbc, 2, fscod);
262 put_bits(&pbc, 5, bsid);
263 put_bits(&pbc, 3, bsmod);
264 put_bits(&pbc, 3, acmod);
265 put_bits(&pbc, 1, lfeon);
266 put_bits(&pbc, 5, frmsizecod>>1); // bit_rate_code
267 put_bits(&pbc, 5, 0); // reserved
268
269 flush_put_bits(&pbc);
270 put_buffer(pb, buf, sizeof(buf));
271
272 return 11;
273 }
274
275 /**
276 * This function writes extradata "as is".
277 * Extradata must be formated like a valid atom (with size and tag)
278 */
279 static int mov_write_extradata_tag(ByteIOContext *pb, MOVTrack *track)
280 {
281 put_buffer(pb, track->enc->extradata, track->enc->extradata_size);
282 return track->enc->extradata_size;
283 }
284
285 static int mov_write_enda_tag(ByteIOContext *pb)
286 {
287 put_be32(pb, 10);
288 put_tag(pb, "enda");
289 put_be16(pb, 1); /* little endian */
290 return 10;
291 }
292
293 static unsigned int descrLength(unsigned int len)
294 {
295 int i;
296 for(i=1; len>>(7*i); i++);
297 return len + 1 + i;
298 }
299
300 static void putDescr(ByteIOContext *pb, int tag, unsigned int size)
301 {
302 int i= descrLength(size) - size - 2;
303 put_byte(pb, tag);
304 for(; i>0; i--)
305 put_byte(pb, (size>>(7*i)) | 0x80);
306 put_byte(pb, size & 0x7F);
307 }
308
309 static int mov_write_esds_tag(ByteIOContext *pb, MOVTrack *track) // Basic
310 {
311 int64_t pos = url_ftell(pb);
312 int decoderSpecificInfoLen = track->vosLen ? descrLength(track->vosLen):0;
313
314 put_be32(pb, 0); // size
315 put_tag(pb, "esds");
316 put_be32(pb, 0); // Version
317
318 // ES descriptor
319 putDescr(pb, 0x03, 3 + descrLength(13 + decoderSpecificInfoLen) +
320 descrLength(1));
321 put_be16(pb, track->trackID);
322 put_byte(pb, 0x00); // flags (= no flags)
323
324 // DecoderConfig descriptor
325 putDescr(pb, 0x04, 13 + decoderSpecificInfoLen);
326
327 // Object type indication
328 if ((track->enc->codec_id == CODEC_ID_MP2 ||
329 track->enc->codec_id == CODEC_ID_MP3) &&
330 track->enc->sample_rate > 24000)
331 put_byte(pb, 0x6B); // 11172-3
332 else
333 put_byte(pb, codec_get_tag(ff_mp4_obj_type, track->enc->codec_id));
334
335 // the following fields is made of 6 bits to identify the streamtype (4 for video, 5 for audio)
336 // plus 1 bit to indicate upstream and 1 bit set to 1 (reserved)
337 if(track->enc->codec_type == CODEC_TYPE_AUDIO)
338 put_byte(pb, 0x15); // flags (= Audiostream)
339 else
340 put_byte(pb, 0x11); // flags (= Visualstream)
341
342 put_byte(pb, track->enc->rc_buffer_size>>(3+16)); // Buffersize DB (24 bits)
343 put_be16(pb, (track->enc->rc_buffer_size>>3)&0xFFFF); // Buffersize DB
344
345 put_be32(pb, FFMAX(track->enc->bit_rate, track->enc->rc_max_rate)); // maxbitrate (FIXME should be max rate in any 1 sec window)
346 if(track->enc->rc_max_rate != track->enc->rc_min_rate || track->enc->rc_min_rate==0)
347 put_be32(pb, 0); // vbr
348 else
349 put_be32(pb, track->enc->rc_max_rate); // avg bitrate
350
351 if (track->vosLen) {
352 // DecoderSpecific info descriptor
353 putDescr(pb, 0x05, track->vosLen);
354 put_buffer(pb, track->vosData, track->vosLen);
355 }
356
357 // SL descriptor
358 putDescr(pb, 0x06, 1);
359 put_byte(pb, 0x02);
360 return updateSize(pb, pos);
361 }
362
363 static int mov_write_wave_tag(ByteIOContext *pb, MOVTrack *track)
364 {
365 int64_t pos = url_ftell(pb);
366
367 put_be32(pb, 0); /* size */
368 put_tag(pb, "wave");
369
370 put_be32(pb, 12); /* size */
371 put_tag(pb, "frma");
372 put_le32(pb, track->tag);
373
374 if (track->enc->codec_id == CODEC_ID_AAC) {
375 /* useless atom needed by mplayer, ipod, not needed by quicktime */
376 put_be32(pb, 12); /* size */
377 put_tag(pb, "mp4a");
378 put_be32(pb, 0);
379 mov_write_esds_tag(pb, track);
380 } else if (track->enc->codec_id == CODEC_ID_PCM_S24LE ||
381 track->enc->codec_id == CODEC_ID_PCM_S32LE) {
382 mov_write_enda_tag(pb);
383 } else if (track->enc->codec_id == CODEC_ID_AMR_NB) {
384 mov_write_amr_tag(pb, track);
385 } else if (track->enc->codec_id == CODEC_ID_AC3) {
386 mov_write_ac3_tag(pb, track);
387 } else if (track->enc->codec_id == CODEC_ID_ALAC) {
388 mov_write_extradata_tag(pb, track);
389 }
390
391 put_be32(pb, 8); /* size */
392 put_be32(pb, 0); /* null tag */
393
394 return updateSize(pb, pos);
395 }
396
397 static int mov_write_glbl_tag(ByteIOContext *pb, MOVTrack *track)
398 {
399 put_be32(pb, track->vosLen+8);
400 put_tag(pb, "glbl");
401 put_buffer(pb, track->vosData, track->vosLen);
402 return 8+track->vosLen;
403 }
404
405 static int mov_write_audio_tag(ByteIOContext *pb, MOVTrack *track)
406 {
407 int64_t pos = url_ftell(pb);
408 int version = track->mode == MODE_MOV &&
409 (track->audio_vbr ||
410 track->enc->codec_id == CODEC_ID_PCM_S32LE ||
411 track->enc->codec_id == CODEC_ID_PCM_S24LE);
412
413 put_be32(pb, 0); /* size */
414 put_le32(pb, track->tag); // store it byteswapped
415 put_be32(pb, 0); /* Reserved */
416 put_be16(pb, 0); /* Reserved */
417 put_be16(pb, 1); /* Data-reference index, XXX == 1 */
418
419 /* SoundDescription */
420 put_be16(pb, version); /* Version */
421 put_be16(pb, 0); /* Revision level */
422 put_be32(pb, 0); /* Reserved */
423
424 if (track->mode == MODE_MOV) {
425 put_be16(pb, track->enc->channels);
426 if (track->enc->codec_id == CODEC_ID_PCM_U8 ||
427 track->enc->codec_id == CODEC_ID_PCM_S8)
428 put_be16(pb, 8); /* bits per sample */
429 else
430 put_be16(pb, 16);
431 put_be16(pb, track->audio_vbr ? -2 : 0); /* compression ID */
432 } else { /* reserved for mp4/3gp */
433 put_be16(pb, 2);
434 put_be16(pb, 16);
435 put_be16(pb, 0);
436 }
437
438 put_be16(pb, 0); /* packet size (= 0) */
439 put_be16(pb, track->timescale); /* Time scale */
440 put_be16(pb, 0); /* Reserved */
441
442 if(version == 1) { /* SoundDescription V1 extended info */
443 put_be32(pb, track->enc->frame_size); /* Samples per packet */
444 put_be32(pb, track->sampleSize / track->enc->channels); /* Bytes per packet */
445 put_be32(pb, track->sampleSize); /* Bytes per frame */
446 put_be32(pb, 2); /* Bytes per sample */
447 }
448
449 if(track->mode == MODE_MOV &&
450 (track->enc->codec_id == CODEC_ID_AAC ||
451 track->enc->codec_id == CODEC_ID_AC3 ||
452 track->enc->codec_id == CODEC_ID_AMR_NB ||
453 track->enc->codec_id == CODEC_ID_PCM_S24LE ||
454 track->enc->codec_id == CODEC_ID_PCM_S32LE ||
455 track->enc->codec_id == CODEC_ID_ALAC))
456 mov_write_wave_tag(pb, track);
457 else if(track->tag == MKTAG('m','p','4','a'))
458 mov_write_esds_tag(pb, track);
459 else if(track->enc->codec_id == CODEC_ID_AMR_NB)
460 mov_write_amr_tag(pb, track);
461 else if(track->enc->codec_id == CODEC_ID_AC3)
462 mov_write_ac3_tag(pb, track);
463 else if(track->enc->codec_id == CODEC_ID_ALAC)
464 mov_write_extradata_tag(pb, track);
465 else if(track->vosLen > 0)
466 mov_write_glbl_tag(pb, track);
467
468 return updateSize(pb, pos);
469 }
470
471 static int mov_write_d263_tag(ByteIOContext *pb)
472 {
473 put_be32(pb, 0xf); /* size */
474 put_tag(pb, "d263");
475 put_tag(pb, "FFMP");
476 put_byte(pb, 0); /* decoder version */
477 /* FIXME use AVCodecContext level/profile, when encoder will set values */
478 put_byte(pb, 0xa); /* level */
479 put_byte(pb, 0); /* profile */
480 return 0xf;
481 }
482
483 /* TODO: No idea about these values */
484 static int mov_write_svq3_tag(ByteIOContext *pb)
485 {
486 put_be32(pb, 0x15);
487 put_tag(pb, "SMI ");
488 put_tag(pb, "SEQH");
489 put_be32(pb, 0x5);
490 put_be32(pb, 0xe2c0211d);
491 put_be32(pb, 0xc0000000);
492 put_byte(pb, 0);
493 return 0x15;
494 }
495
496 static int mov_write_avcc_tag(ByteIOContext *pb, MOVTrack *track)
497 {
498 int64_t pos = url_ftell(pb);
499
500 put_be32(pb, 0);
501 put_tag(pb, "avcC");
502 ff_isom_write_avcc(pb, track->vosData, track->vosLen);
503 return updateSize(pb, pos);
504 }
505
506 /* also used by all avid codecs (dv, imx, meridien) and their variants */
507 static int mov_write_avid_tag(ByteIOContext *pb, MOVTrack *track)
508 {
509 int i;
510 put_be32(pb, 24); /* size */
511 put_tag(pb, "ACLR");
512 put_tag(pb, "ACLR");
513 put_tag(pb, "0001");
514 put_be32(pb, 1); /* yuv 1 / rgb 2 ? */
515 put_be32(pb, 0); /* unknown */
516
517 put_be32(pb, 24); /* size */
518 put_tag(pb, "APRG");
519 put_tag(pb, "APRG");
520 put_tag(pb, "0001");
521 put_be32(pb, 1); /* unknown */
522 put_be32(pb, 0); /* unknown */
523
524 put_be32(pb, 120); /* size */
525 put_tag(pb, "ARES");
526 put_tag(pb, "ARES");
527 put_tag(pb, "0001");
528 put_be32(pb, AV_RB32(track->vosData + 0x28)); /* dnxhd cid, some id ? */
529 put_be32(pb, track->enc->width);
530 /* values below are based on samples created with quicktime and avid codecs */
531 if (track->vosData[5] & 2) { // interlaced
532 put_be32(pb, track->enc->height/2);
533 put_be32(pb, 2); /* unknown */
534 put_be32(pb, 0); /* unknown */
535 put_be32(pb, 4); /* unknown */
536 } else {
537 put_be32(pb, track->enc->height);
538 put_be32(pb, 1); /* unknown */
539 put_be32(pb, 0); /* unknown */
540 if (track->enc->height == 1080)
541 put_be32(pb, 5); /* unknown */
542 else
543 put_be32(pb, 6); /* unknown */
544 }
545 /* padding */
546 for (i = 0; i < 10; i++)
547 put_be64(pb, 0);
548
549 /* extra padding for stsd needed */
550 put_be32(pb, 0);
551 return 0;
552 }
553
554 static int mp4_get_codec_tag(AVFormatContext *s, MOVTrack *track)
555 {
556 int tag = track->enc->codec_tag;
557
558 if (!codec_get_tag(ff_mp4_obj_type, track->enc->codec_id))
559 return 0;
560
561 if (track->enc->codec_id == CODEC_ID_H264) tag = MKTAG('a','v','c','1');
562 else if (track->enc->codec_id == CODEC_ID_AC3) tag = MKTAG('a','c','-','3');
563 else if (track->enc->codec_id == CODEC_ID_DIRAC) tag = MKTAG('d','r','a','c');
564 else if (track->enc->codec_id == CODEC_ID_MOV_TEXT) tag = MKTAG('t','x','3','g');
565 else if (track->enc->codec_type == CODEC_TYPE_VIDEO) tag = MKTAG('m','p','4','v');
566 else if (track->enc->codec_type == CODEC_TYPE_AUDIO) tag = MKTAG('m','p','4','a');
567
568 return tag;
569 }
570
571 static const AVCodecTag codec_ipod_tags[] = {
572 { CODEC_ID_H264, MKTAG('a','v','c','1') },
573 { CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
574 { CODEC_ID_AAC, MKTAG('m','p','4','a') },
575 { CODEC_ID_ALAC, MKTAG('a','l','a','c') },
576 { CODEC_ID_AC3, MKTAG('a','c','-','3') },
577 { CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
578 { CODEC_ID_MOV_TEXT, MKTAG('t','e','x','t') },
579 { CODEC_ID_NONE, 0 },
580 };
581
582 static int ipod_get_codec_tag(AVFormatContext *s, MOVTrack *track)
583 {
584 int tag = track->enc->codec_tag;
585
586 // keep original tag for subs, ipod supports both formats
587 if (!(track->enc->codec_type == CODEC_TYPE_SUBTITLE &&
588 (tag == MKTAG('t','x','3','g') ||
589 tag == MKTAG('t','e','x','t'))))
590 tag = codec_get_tag(codec_ipod_tags, track->enc->codec_id);
591
592 if (!match_ext(s->filename, "m4a") && !match_ext(s->filename, "m4v"))
593 av_log(s, AV_LOG_WARNING, "Warning, extension is not .m4a nor .m4v "
594 "Quicktime/Ipod might not play the file\n");
595
596 return tag;
597 }
598
599 static int mov_get_dv_codec_tag(AVFormatContext *s, MOVTrack *track)
600 {
601 int tag;
602
603 if (track->enc->height == 480) /* NTSC */
604 if (track->enc->pix_fmt == PIX_FMT_YUV422P) tag = MKTAG('d','v','5','n');
605 else tag = MKTAG('d','v','c',' ');
606 else if (track->enc->pix_fmt == PIX_FMT_YUV422P) tag = MKTAG('d','v','5','p');
607 else if (track->enc->pix_fmt == PIX_FMT_YUV420P) tag = MKTAG('d','v','c','p');
608 else tag = MKTAG('d','v','p','p');
609
610 return tag;
611 }
612
613 static const struct {
614 enum PixelFormat pix_fmt;
615 uint32_t tag;
616 unsigned bps;
617 } mov_pix_fmt_tags[] = {
618 { PIX_FMT_YUYV422, MKTAG('y','u','v','s'), 0 },
619 { PIX_FMT_UYVY422, MKTAG('2','v','u','y'), 0 },
620 { PIX_FMT_BGR555, MKTAG('r','a','w',' '), 16 },
621 { PIX_FMT_RGB555LE,MKTAG('L','5','5','5'), 16 },
622 { PIX_FMT_RGB565LE,MKTAG('L','5','6','5'), 16 },
623 { PIX_FMT_RGB565BE,MKTAG('B','5','6','5'), 16 },
624 { PIX_FMT_RGB24, MKTAG('r','a','w',' '), 24 },
625 { PIX_FMT_BGR24, MKTAG('2','4','B','G'), 24 },
626 { PIX_FMT_ARGB, MKTAG('r','a','w',' '), 32 },
627 { PIX_FMT_BGRA, MKTAG('B','G','R','A'), 32 },
628 { PIX_FMT_RGBA, MKTAG('R','G','B','A'), 32 },
629 };
630
631 static int mov_get_rawvideo_codec_tag(AVFormatContext *s, MOVTrack *track)
632 {
633 int tag = track->enc->codec_tag;
634 int i;
635
636 for (i = 0; i < FF_ARRAY_ELEMS(mov_pix_fmt_tags); i++) {
637 if (track->enc->pix_fmt == mov_pix_fmt_tags[i].pix_fmt) {
638 tag = mov_pix_fmt_tags[i].tag;
639 track->enc->bits_per_coded_sample = mov_pix_fmt_tags[i].bps;
640 break;
641 }
642 }
643
644 return tag;
645 }
646
647 static int mov_get_codec_tag(AVFormatContext *s, MOVTrack *track)
648 {
649 int tag = track->enc->codec_tag;
650
651 if (!tag || (track->enc->strict_std_compliance >= FF_COMPLIANCE_NORMAL &&
652 (tag == MKTAG('d','v','c','p') ||
653 track->enc->codec_id == CODEC_ID_RAWVIDEO ||
654 av_get_bits_per_sample(track->enc->codec_id)))) { // pcm audio
655 if (track->enc->codec_id == CODEC_ID_DVVIDEO)
656 tag = mov_get_dv_codec_tag(s, track);
657 else if (track->enc->codec_id == CODEC_ID_RAWVIDEO)
658 tag = mov_get_rawvideo_codec_tag(s, track);
659 else if (track->enc->codec_type == CODEC_TYPE_VIDEO) {
660 tag = codec_get_tag(codec_movvideo_tags, track->enc->codec_id);
661 if (!tag) { // if no mac fcc found, try with Microsoft tags
662 tag = codec_get_tag(codec_bmp_tags, track->enc->codec_id);
663 if (tag)
664 av_log(s, AV_LOG_INFO, "Warning, using MS style video codec tag, "
665 "the file may be unplayable!\n");
666 }
667 } else if (track->enc->codec_type == CODEC_TYPE_AUDIO) {
668 tag = codec_get_tag(codec_movaudio_tags, track->enc->codec_id);
669 if (!tag) { // if no mac fcc found, try with Microsoft tags
670 int ms_tag = codec_get_tag(codec_wav_tags, track->enc->codec_id);
671 if (ms_tag) {
672 tag = MKTAG('m', 's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
673 av_log(s, AV_LOG_INFO, "Warning, using MS style audio codec tag, "
674 "the file may be unplayable!\n");
675 }
676 }
677 } else if (track->enc->codec_type == CODEC_TYPE_SUBTITLE)
678 tag = codec_get_tag(ff_codec_movsubtitle_tags, track->enc->codec_id);
679 }
680
681 return tag;
682 }
683
684 static const AVCodecTag codec_3gp_tags[] = {
685 { CODEC_ID_H263, MKTAG('s','2','6','3') },
686 { CODEC_ID_H264, MKTAG('a','v','c','1') },
687 { CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
688 { CODEC_ID_AAC, MKTAG('m','p','4','a') },
689 { CODEC_ID_AMR_NB, MKTAG('s','a','m','r') },
690 { CODEC_ID_AMR_WB, MKTAG('s','a','w','b') },
691 { CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
692 { CODEC_ID_NONE, 0 },
693 };
694
695 static int mov_find_codec_tag(AVFormatContext *s, MOVTrack *track)
696 {
697 int tag = track->enc->codec_tag;
698
699 if (track->mode == MODE_MP4 || track->mode == MODE_PSP)
700 tag = mp4_get_codec_tag(s, track);
701 else if (track->mode == MODE_IPOD)
702 tag = ipod_get_codec_tag(s, track);
703 else if (track->mode & MODE_3GP)
704 tag = codec_get_tag(codec_3gp_tags, track->enc->codec_id);
705 else
706 tag = mov_get_codec_tag(s, track);
707
708 return tag;
709 }
710
711 /** Write uuid atom.
712 * Needed to make file play in iPods running newest firmware
713 * goes after avcC atom in moov.trak.mdia.minf.stbl.stsd.avc1
714 */
715 static int mov_write_uuid_tag_ipod(ByteIOContext *pb)
716 {
717 put_be32(pb, 28);
718 put_tag(pb, "uuid");
719 put_be32(pb, 0x6b6840f2);
720 put_be32(pb, 0x5f244fc5);
721 put_be32(pb, 0xba39a51b);
722 put_be32(pb, 0xcf0323f3);
723 put_be32(pb, 0x0);
724 return 28;
725 }
726
727 static int mov_write_subtitle_tag(ByteIOContext *pb, MOVTrack *track)
728 {
729 int64_t pos = url_ftell(pb);
730 put_be32(pb, 0); /* size */
731 put_le32(pb, track->tag); // store it byteswapped
732 put_be32(pb, 0); /* Reserved */
733 put_be16(pb, 0); /* Reserved */
734 put_be16(pb, 1); /* Data-reference index */
735
736 if (track->enc->extradata_size)
737 put_buffer(pb, track->enc->extradata, track->enc->extradata_size);
738
739 return updateSize(pb, pos);
740 }
741
742 static int mov_write_video_tag(ByteIOContext *pb, MOVTrack *track)
743 {
744 int64_t pos = url_ftell(pb);
745 char compressor_name[32];
746
747 put_be32(pb, 0); /* size */
748 put_le32(pb, track->tag); // store it byteswapped
749 put_be32(pb, 0); /* Reserved */
750 put_be16(pb, 0); /* Reserved */
751 put_be16(pb, 1); /* Data-reference index */
752
753 put_be16(pb, 0); /* Codec stream version */
754 put_be16(pb, 0); /* Codec stream revision (=0) */
755 if (track->mode == MODE_MOV) {
756 put_tag(pb, "FFMP"); /* Vendor */
757 if(track->enc->codec_id == CODEC_ID_RAWVIDEO) {
758 put_be32(pb, 0); /* Temporal Quality */
759 put_be32(pb, 0x400); /* Spatial Quality = lossless*/
760 } else {
761 put_be32(pb, 0x200); /* Temporal Quality = normal */
762 put_be32(pb, 0x200); /* Spatial Quality = normal */
763 }
764 } else {
765 put_be32(pb, 0); /* Reserved */
766 put_be32(pb, 0); /* Reserved */
767 put_be32(pb, 0); /* Reserved */
768 }
769 put_be16(pb, track->enc->width); /* Video width */
770 put_be16(pb, track->height); /* Video height */
771 put_be32(pb, 0x00480000); /* Horizontal resolution 72dpi */
772 put_be32(pb, 0x00480000); /* Vertical resolution 72dpi */
773 put_be32(pb, 0); /* Data size (= 0) */
774 put_be16(pb, 1); /* Frame count (= 1) */
775
776 memset(compressor_name,0,32);
777 /* FIXME not sure, ISO 14496-1 draft where it shall be set to 0 */
778 if (track->mode == MODE_MOV && track->enc->codec && track->enc->codec->name)
779 strncpy(compressor_name,track->enc->codec->name,31);
780 put_byte(pb, strlen(compressor_name));
781 put_buffer(pb, compressor_name, 31);
782
783 if (track->mode == MODE_MOV && track->enc->bits_per_coded_sample)
784 put_be16(pb, track->enc->bits_per_coded_sample);
785 else
786 put_be16(pb, 0x18); /* Reserved */
787 put_be16(pb, 0xffff); /* Reserved */
788 if(track->tag == MKTAG('m','p','4','v'))
789 mov_write_esds_tag(pb, track);
790 else if(track->enc->codec_id == CODEC_ID_H263)
791 mov_write_d263_tag(pb);
792 else if(track->enc->codec_id == CODEC_ID_SVQ3)
793 mov_write_svq3_tag(pb);
794 else if(track->enc->codec_id == CODEC_ID_DNXHD)
795 mov_write_avid_tag(pb, track);
796 else if(track->enc->codec_id == CODEC_ID_H264) {
797 mov_write_avcc_tag(pb, track);
798 if(track->mode == MODE_IPOD)
799 mov_write_uuid_tag_ipod(pb);
800 } else if(track->vosLen > 0)
801 mov_write_glbl_tag(pb, track);
802
803 return updateSize(pb, pos);
804 }
805
806 static int mov_write_stsd_tag(ByteIOContext *pb, MOVTrack *track)
807 {
808 int64_t pos = url_ftell(pb);
809 put_be32(pb, 0); /* size */
810 put_tag(pb, "stsd");
811 put_be32(pb, 0); /* version & flags */
812 put_be32(pb, 1); /* entry count */
813 if (track->enc->codec_type == CODEC_TYPE_VIDEO)
814 mov_write_video_tag(pb, track);
815 else if (track->enc->codec_type == CODEC_TYPE_AUDIO)
816 mov_write_audio_tag(pb, track);
817 else if (track->enc->codec_type == CODEC_TYPE_SUBTITLE)
818 mov_write_subtitle_tag(pb, track);
819 return updateSize(pb, pos);
820 }
821
822 static int mov_write_ctts_tag(ByteIOContext *pb, MOVTrack *track)
823 {
824 MOVStts *ctts_entries;
825 uint32_t entries = 0;
826 uint32_t atom_size;
827 int i;
828
829 ctts_entries = av_malloc((track->entry + 1) * sizeof(*ctts_entries)); /* worst case */
830 ctts_entries[0].count = 1;
831 ctts_entries[0].duration = track->cluster[0].cts;
832 for (i=1; i<track->entry; i++) {
833 if (track->cluster[i].cts == ctts_entries[entries].duration) {
834 ctts_entries[entries].count++; /* compress */
835 } else {
836 entries++;
837 ctts_entries[entries].duration = track->cluster[i].cts;
838 ctts_entries[entries].count = 1;
839 }
840 }
841 entries++; /* last one */
842 atom_size = 16 + (entries * 8);
843 put_be32(pb, atom_size); /* size */
844 put_tag(pb, "ctts");
845 put_be32(pb, 0); /* version & flags */
846 put_be32(pb, entries); /* entry count */
847 for (i=0; i<entries; i++) {
848 put_be32(pb, ctts_entries[i].count);
849 put_be32(pb, ctts_entries[i].duration);
850 }
851 av_free(ctts_entries);
852 return atom_size;
853 }
854
855 /* Time to sample atom */
856 static int mov_write_stts_tag(ByteIOContext *pb, MOVTrack *track)
857 {
858 MOVStts *stts_entries;
859 uint32_t entries = -1;
860 uint32_t atom_size;
861 int i;
862
863 if (track->enc->codec_type == CODEC_TYPE_AUDIO && !track->audio_vbr) {
864 stts_entries = av_malloc(sizeof(*stts_entries)); /* one entry */
865 stts_entries[0].count = track->sampleCount;
866 stts_entries[0].duration = 1;
867 entries = 1;
868 } else {
869 stts_entries = av_malloc(track->entry * sizeof(*stts_entries)); /* worst case */
870 for (i=0; i<track->entry; i++) {
871 int64_t duration = i + 1 == track->entry ?
872 track->trackDuration - track->cluster[i].dts + track->cluster[0].dts : /* readjusting */
873 track->cluster[i+1].dts - track->cluster[i].dts;
874 if (i && duration == stts_entries[entries].duration) {
875 stts_entries[entries].count++; /* compress */
876 } else {
877 entries++;
878 stts_entries[entries].duration = duration;
879 stts_entries[entries].count = 1;
880 }
881 }
882 entries++; /* last one */
883 }
884 atom_size = 16 + (entries * 8);
885 put_be32(pb, atom_size); /* size */
886 put_tag(pb, "stts");
887 put_be32(pb, 0); /* version & flags */
888 put_be32(pb, entries); /* entry count */
889 for (i=0; i<entries; i++) {
890 put_be32(pb, stts_entries[i].count);
891 put_be32(pb, stts_entries[i].duration);
892 }
893 av_free(stts_entries);
894 return atom_size;
895 }
896
897 static int mov_write_dref_tag(ByteIOContext *pb)
898 {
899 put_be32(pb, 28); /* size */
900 put_tag(pb, "dref");
901 put_be32(pb, 0); /* version & flags */
902 put_be32(pb, 1); /* entry count */
903
904 put_be32(pb, 0xc); /* size */
905 put_tag(pb, "url ");
906 put_be32(pb, 1); /* version & flags */
907
908 return 28;
909 }
910
911 static int mov_write_stbl_tag(ByteIOContext *pb, MOVTrack *track)
912 {
913 int64_t pos = url_ftell(pb);
914 put_be32(pb, 0); /* size */
915 put_tag(pb, "stbl");
916 mov_write_stsd_tag(pb, track);
917 mov_write_stts_tag(pb, track);
918 if (track->enc->codec_type == CODEC_TYPE_VIDEO &&
919 track->hasKeyframes && track->hasKeyframes < track->entry)
920 mov_write_stss_tag(pb, track, MOV_SYNC_SAMPLE);
921 if (track->mode == MODE_MOV && track->flags & MOV_TRACK_STPS)
922 mov_write_stss_tag(pb, track, MOV_PARTIAL_SYNC_SAMPLE);
923 if (track->enc->codec_type == CODEC_TYPE_VIDEO &&
924 track->flags & MOV_TRACK_CTTS)
925 mov_write_ctts_tag(pb, track);
926 mov_write_stsc_tag(pb, track);
927 mov_write_stsz_tag(pb, track);
928 mov_write_stco_tag(pb, track);
929 return updateSize(pb, pos);
930 }
931
932 static int mov_write_dinf_tag(ByteIOContext *pb)
933 {
934 int64_t pos = url_ftell(pb);
935 put_be32(pb, 0); /* size */
936 put_tag(pb, "dinf");
937 mov_write_dref_tag(pb);
938 return updateSize(pb, pos);
939 }
940
941 static int mov_write_nmhd_tag(ByteIOContext *pb)
942 {
943 put_be32(pb, 12);
944 put_tag(pb, "nmhd");
945 put_be32(pb, 0);
946 return 12;
947 }
948
949 static int mov_write_gmhd_tag(ByteIOContext *pb)
950 {
951 put_be32(pb, 0x20); /* size */
952 put_tag(pb, "gmhd");
953 put_be32(pb, 0x18); /* gmin size */
954 put_tag(pb, "gmin"); /* generic media info */
955 put_be32(pb, 0); /* version & flags */
956 put_be16(pb, 0x40); /* graphics mode = */
957 put_be16(pb, 0x8000); /* opColor (r?) */
958 put_be16(pb, 0x8000); /* opColor (g?) */
959 put_be16(pb, 0x8000); /* opColor (b?) */
960 put_be16(pb, 0); /* balance */
961 put_be16(pb, 0); /* reserved */
962 return 0x20;
963 }
964
965 static int mov_write_smhd_tag(ByteIOContext *pb)
966 {
967 put_be32(pb, 16); /* size */
968 put_tag(pb, "smhd");
969 put_be32(pb, 0); /* version & flags */
970 put_be16(pb, 0); /* reserved (balance, normally = 0) */
971 put_be16(pb, 0); /* reserved */
972 return 16;
973 }
974
975 static int mov_write_vmhd_tag(ByteIOContext *pb)
976 {
977 put_be32(pb, 0x14); /* size (always 0x14) */
978 put_tag(pb, "vmhd");
979 put_be32(pb, 0x01); /* version & flags */
980 put_be64(pb, 0); /* reserved (graphics mode = copy) */
981 return 0x14;
982 }
983
984 static int mov_write_hdlr_tag(ByteIOContext *pb, MOVTrack *track)
985 {
986 const char *hdlr, *descr = NULL, *hdlr_type = NULL;
987 int64_t pos = url_ftell(pb);
988
989 if (!track) { /* no media --> data handler */
990 hdlr = "dhlr";
991 hdlr_type = "url ";
992 descr = "DataHandler";
993 } else {
994 hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
995 if (track->enc->codec_type == CODEC_TYPE_VIDEO) {
996 hdlr_type = "vide";
997 descr = "VideoHandler";
998 } else if (track->enc->codec_type == CODEC_TYPE_AUDIO) {
999 hdlr_type = "soun";
1000 descr = "SoundHandler";
1001 } else if (track->enc->codec_type == CODEC_TYPE_SUBTITLE) {
1002 if (track->tag == MKTAG('t','x','3','g')) hdlr_type = "sbtl";
1003 else hdlr_type = "text";
1004 descr = "SubtitleHandler";
1005 }
1006 }
1007
1008 put_be32(pb, 0); /* size */
1009 put_tag(pb, "hdlr");
1010 put_be32(pb, 0); /* Version & flags */
1011 put_buffer(pb, hdlr, 4); /* handler */
1012 put_tag(pb, hdlr_type); /* handler type */
1013 put_be32(pb ,0); /* reserved */
1014 put_be32(pb ,0); /* reserved */
1015 put_be32(pb ,0); /* reserved */
1016 put_byte(pb, strlen(descr)); /* string counter */
1017 put_buffer(pb, descr, strlen(descr)); /* handler description */
1018 return updateSize(pb, pos);
1019 }
1020
1021 static int mov_write_minf_tag(ByteIOContext *pb, MOVTrack *track)
1022 {
1023 int64_t pos = url_ftell(pb);
1024 put_be32(pb, 0); /* size */
1025 put_tag(pb, "minf");
1026 if(track->enc->codec_type == CODEC_TYPE_VIDEO)
1027 mov_write_vmhd_tag(pb);
1028 else if (track->enc->codec_type == CODEC_TYPE_AUDIO)
1029 mov_write_smhd_tag(pb);
1030 else if (track->enc->codec_type == CODEC_TYPE_SUBTITLE) {
1031 if (track->tag == MKTAG('t','e','x','t')) mov_write_gmhd_tag(pb);
1032 else mov_write_nmhd_tag(pb);
1033 }
1034 if (track->mode == MODE_MOV) /* FIXME: Why do it for MODE_MOV only ? */
1035 mov_write_hdlr_tag(pb, NULL);
1036 mov_write_dinf_tag(pb);
1037 mov_write_stbl_tag(pb, track);
1038 return updateSize(pb, pos);
1039 }
1040
1041 static int mov_write_mdhd_tag(ByteIOContext *pb, MOVTrack *track)
1042 {
1043 int version = track->trackDuration < INT32_MAX ? 0 : 1;
1044
1045 (version == 1) ? put_be32(pb, 44) : put_be32(pb, 32); /* size */
1046 put_tag(pb, "mdhd");
1047 put_byte(pb, version);
1048 put_be24(pb, 0); /* flags */
1049 if (version == 1) {
1050 put_be64(pb, track->time);
1051 put_be64(pb, track->time);
1052 } else {
1053 put_be32(pb, track->time); /* creation time */
1054 put_be32(pb, track->time); /* modification time */
1055 }
1056 put_be32(pb, track->timescale); /* time scale (sample rate for audio) */
1057 (version == 1) ? put_be64(pb, track->trackDuration) : put_be32(pb, track->trackDuration); /* duration */
1058 put_be16(pb, track->language); /* language */
1059 put_be16(pb, 0); /* reserved (quality) */
1060
1061 if(version!=0 && track->mode == MODE_MOV){
1062 av_log(NULL, AV_LOG_ERROR,
1063 "FATAL error, file duration too long for timebase, this file will not be\n"
1064 "playable with quicktime. Choose a different timebase or a different\n"
1065 "container format\n");
1066 }
1067
1068 return 32;
1069 }
1070
1071 static int mov_write_mdia_tag(ByteIOContext *pb, MOVTrack *track)
1072 {
1073 int64_t pos = url_ftell(pb);
1074 put_be32(pb, 0); /* size */
1075 put_tag(pb, "mdia");
1076 mov_write_mdhd_tag(pb, track);
1077 mov_write_hdlr_tag(pb, track);
1078 mov_write_minf_tag(pb, track);
1079 return updateSize(pb, pos);
1080 }
1081
1082 static int mov_write_tkhd_tag(ByteIOContext *pb, MOVTrack *track, AVStream *st)
1083 {
1084 int64_t duration = av_rescale_rnd(track->trackDuration, globalTimescale, track->timescale, AV_ROUND_UP);
1085 int version = duration < INT32_MAX ? 0 : 1;
1086
1087 (version == 1) ? put_be32(pb, 104) : put_be32(pb, 92); /* size */
1088 put_tag(pb, "tkhd");
1089 put_byte(pb, version);
1090 put_be24(pb, 0xf); /* flags (track enabled) */
1091 if (version == 1) {
1092 put_be64(pb, track->time);
1093 put_be64(pb, track->time);
1094 } else {
1095 put_be32(pb, track->time); /* creation time */
1096 put_be32(pb, track->time); /* modification time */
1097 }
1098 put_be32(pb, track->trackID); /* track-id */
1099 put_be32(pb, 0); /* reserved */
1100 (version == 1) ? put_be64(pb, duration) : put_be32(pb, duration);
1101
1102 put_be32(pb, 0); /* reserved */
1103 put_be32(pb, 0); /* reserved */
1104 put_be32(pb, 0x0); /* reserved (Layer & Alternate group) */
1105 /* Volume, only for audio */
1106 if(track->enc->codec_type == CODEC_TYPE_AUDIO)
1107 put_be16(pb, 0x0100);
1108 else
1109 put_be16(pb, 0);
1110 put_be16(pb, 0); /* reserved */
1111
1112 /* Matrix structure */
1113 put_be32(pb, 0x00010000); /* reserved */
1114 put_be32(pb, 0x0); /* reserved */
1115 put_be32(pb, 0x0); /* reserved */
1116 put_be32(pb, 0x0); /* reserved */
1117 put_be32(pb, 0x00010000); /* reserved */
1118 put_be32(pb, 0x0); /* reserved */
1119 put_be32(pb, 0x0); /* reserved */
1120 put_be32(pb, 0x0); /* reserved */
1121 put_be32(pb, 0x40000000); /* reserved */
1122
1123 /* Track width and height, for visual only */
1124 if(track->enc->codec_type == CODEC_TYPE_VIDEO ||
1125 track->enc->codec_type == CODEC_TYPE_SUBTITLE) {
1126 double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
1127 if(!sample_aspect_ratio || track->height != track->enc->height)
1128 sample_aspect_ratio = 1;
1129 put_be32(pb, sample_aspect_ratio * track->enc->width*0x10000);
1130 put_be32(pb, track->height*0x10000);
1131 }
1132 else {
1133 put_be32(pb, 0);
1134 put_be32(pb, 0);
1135 }
1136 return 0x5c;
1137 }
1138
1139 // This box seems important for the psp playback ... without it the movie seems to hang
1140 static int mov_write_edts_tag(ByteIOContext *pb, MOVTrack *track)
1141 {
1142 put_be32(pb, 0x24); /* size */
1143 put_tag(pb, "edts");
1144 put_be32(pb, 0x1c); /* size */
1145 put_tag(pb, "elst");
1146 put_be32(pb, 0x0);
1147 put_be32(pb, 0x1);
1148
1149 put_be32(pb, av_rescale_rnd(track->trackDuration, globalTimescale, track->timescale, AV_ROUND_UP)); /* duration ... doesn't seem to effect psp */
1150
1151 put_be32(pb, track->cluster[0].cts); /* first pts is cts since dts is 0 */
1152 put_be32(pb, 0x00010000);
1153 return 0x24;
1154 }
1155
1156 // goes at the end of each track! ... Critical for PSP playback ("Incompatible data" without it)
1157 static int mov_write_uuid_tag_psp(ByteIOContext *pb, MOVTrack *mov)
1158 {
1159 put_be32(pb, 0x34); /* size ... reports as 28 in mp4box! */
1160 put_tag(pb, "uuid");
1161 put_tag(pb, "USMT");
1162 put_be32(pb, 0x21d24fce);
1163 put_be32(pb, 0xbb88695c);
1164 put_be32(pb, 0xfac9c740);
1165 put_be32(pb, 0x1c); // another size here!
1166 put_tag(pb, "MTDT");
1167 put_be32(pb, 0x00010012);
1168 put_be32(pb, 0x0a);
1169 put_be32(pb, 0x55c40000);
1170 put_be32(pb, 0x1);
1171 put_be32(pb, 0x0);
1172 return 0x34;
1173 }
1174
1175 static int mov_write_trak_tag(ByteIOContext *pb, MOVTrack *track, AVStream *st)
1176 {
1177 int64_t pos = url_ftell(pb);
1178 put_be32(pb, 0); /* size */
1179 put_tag(pb, "trak");
1180 mov_write_tkhd_tag(pb, track, st);
1181 if (track->mode == MODE_PSP || track->flags & MOV_TRACK_CTTS)
1182 mov_write_edts_tag(pb, track); // PSP Movies require edts box
1183 mov_write_mdia_tag(pb, track);
1184 if (track->mode == MODE_PSP)
1185 mov_write_uuid_tag_psp(pb,track); // PSP Movies require this uuid box
1186 return updateSize(pb, pos);
1187 }
1188
1189 #if 0
1190 /* TODO: Not sorted out, but not necessary either */
1191 static int mov_write_iods_tag(ByteIOContext *pb, MOVMuxContext *mov)
1192 {
1193 put_be32(pb, 0x15); /* size */
1194 put_tag(pb, "iods");
1195 put_be32(pb, 0); /* version & flags */
1196 put_be16(pb, 0x1007);
1197 put_byte(pb, 0);
1198 put_be16(pb, 0x4fff);
1199 put_be16(pb, 0xfffe);
1200 put_be16(pb, 0x01ff);
1201 return 0x15;
1202 }
1203 #endif
1204
1205 static int mov_write_mvhd_tag(ByteIOContext *pb, MOVMuxContext *mov)
1206 {
1207 int maxTrackID = 1, i;
1208 int64_t maxTrackLenTemp, maxTrackLen = 0;
1209 int version;
1210
1211 for (i=0; i<mov->nb_streams; i++) {
1212 if(mov->tracks[i].entry > 0) {
1213 maxTrackLenTemp = av_rescale_rnd(mov->tracks[i].trackDuration, globalTimescale, mov->tracks[i].timescale, AV_ROUND_UP);
1214 if(maxTrackLen < maxTrackLenTemp)
1215 maxTrackLen = maxTrackLenTemp;
1216 if(maxTrackID < mov->tracks[i].trackID)
1217 maxTrackID = mov->tracks[i].trackID;
1218 }
1219 }
1220
1221 version = maxTrackLen < UINT32_MAX ? 0 : 1;
1222 (version == 1) ? put_be32(pb, 120) : put_be32(pb, 108); /* size */
1223 put_tag(pb, "mvhd");
1224 put_byte(pb, version);
1225 put_be24(pb, 0); /* flags */
1226 if (version == 1) {
1227 put_be64(pb, mov->time);
1228 put_be64(pb, mov->time);
1229 } else {
1230 put_be32(pb, mov->time); /* creation time */
1231 put_be32(pb, mov->time); /* modification time */
1232 }
1233 put_be32(pb, mov->timescale); /* timescale */
1234 (version == 1) ? put_be64(pb, maxTrackLen) : put_be32(pb, maxTrackLen); /* duration of longest track */
1235
1236 put_be32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
1237 put_be16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
1238 put_be16(pb, 0); /* reserved */
1239 put_be32(pb, 0); /* reserved */
1240 put_be32(pb, 0); /* reserved */
1241
1242 /* Matrix structure */
1243 put_be32(pb, 0x00010000); /* reserved */
1244 put_be32(pb, 0x0); /* reserved */
1245 put_be32(pb, 0x0); /* reserved */
1246 put_be32(pb, 0x0); /* reserved */
1247 put_be32(pb, 0x00010000); /* reserved */
1248 put_be32(pb, 0x0); /* reserved */
1249 put_be32(pb, 0x0); /* reserved */
1250 put_be32(pb, 0x0); /* reserved */
1251 put_be32(pb, 0x40000000); /* reserved */
1252
1253 put_be32(pb, 0); /* reserved (preview time) */
1254 put_be32(pb, 0); /* reserved (preview duration) */
1255 put_be32(pb, 0); /* reserved (poster time) */
1256 put_be32(pb, 0); /* reserved (selection time) */
1257 put_be32(pb, 0); /* reserved (selection duration) */
1258 put_be32(pb, 0); /* reserved (current time) */
1259 put_be32(pb, maxTrackID+1); /* Next track id */
1260 return 0x6c;
1261 }
1262
1263 static int mov_write_itunes_hdlr_tag(ByteIOContext *pb, MOVMuxContext *mov,
1264 AVFormatContext *s)
1265 {
1266 int64_t pos = url_ftell(pb);
1267 put_be32(pb, 0); /* size */
1268 put_tag(pb, "hdlr");
1269 put_be32(pb, 0);
1270 put_be32(pb, 0);
1271 put_tag(pb, "mdir");
1272 put_tag(pb, "appl");
1273 put_be32(pb, 0);
1274 put_be32(pb, 0);
1275 put_be16(pb, 0);
1276 return updateSize(pb, pos);
1277 }
1278
1279 /* helper function to write a data tag with the specified string as data */
1280 static int mov_write_string_data_tag(ByteIOContext *pb, const char *data, int lang, int long_style)
1281 {
1282 if(long_style){
1283 int64_t pos = url_ftell(pb);
1284 put_be32(pb, 0); /* size */
1285 put_tag(pb, "data");
1286 put_be32(pb, 1);
1287 put_be32(pb, 0);
1288 put_buffer(pb, data, strlen(data));
1289 return updateSize(pb, pos);
1290 }else{
1291 put_be16(pb, strlen(data)); /* string length */
1292 put_be16(pb, lang);
1293 put_buffer(pb, data, strlen(data));
1294 return strlen(data) + 4;
1295 }
1296 }
1297
1298 static int mov_write_string_tag(ByteIOContext *pb, const char *name, const char *value, int lang, int long_style){
1299 int size = 0;
1300 if (value && value[0]) {
1301 int64_t pos = url_ftell(pb);
1302 put_be32(pb, 0); /* size */
1303 put_tag(pb, name);
1304 mov_write_string_data_tag(pb, value, lang, long_style);
1305 size= updateSize(pb, pos);
1306 }
1307 return size;
1308 }
1309
1310 static int mov_write_string_metadata(AVFormatContext *s, ByteIOContext *pb,
1311 const char *name, const char *tag,
1312 int long_style)
1313 {
1314 int l, lang = 0, len, len2;
1315 AVMetadataTag *t, *t2 = NULL;
1316 char tag2[16];
1317
1318 if (!(t = av_metadata_get(s->metadata, tag, NULL, 0)))
1319 return 0;
1320
1321 len = strlen(t->key);
1322 snprintf(tag2, sizeof(tag2), "%s-", tag);
1323 while ((t2 = av_metadata_get(s->metadata, tag2, t2, AV_METADATA_IGNORE_SUFFIX))) {
1324 len2 = strlen(t2->key);
1325 if (len2 == len+4 && !strcmp(t->value, t2->value)
1326 && (l=ff_mov_iso639_to_lang(&t2->key[len2-3], 0)) >= 0) {
1327 lang = l;
1328 break;
1329 }
1330 }
1331 return mov_write_string_tag(pb, name, t->value, lang, long_style);
1332 }
1333
1334 /* iTunes track number */
1335 static int mov_write_trkn_tag(ByteIOContext *pb, MOVMuxContext *mov,
1336 AVFormatContext *s)
1337 {
1338 AVMetadataTag *t = av_metadata_get(s->metadata, "track", NULL, 0);
1339 int size = 0, track = t ? atoi(t->value) : 0;
1340 if (track) {
1341 int64_t pos = url_ftell(pb);
1342 put_be32(pb, 0); /* size */
1343 put_tag(pb, "trkn");
1344 {
1345 int64_t pos = url_ftell(pb);
1346 put_be32(pb, 0); /* size */
1347 put_tag(pb, "data");
1348 put_be32(pb, 0); // 8 bytes empty
1349 put_be32(pb, 0);
1350 put_be16(pb, 0); // empty
1351 put_be16(pb, track); // track number
1352 put_be16(pb, 0); // total track number
1353 put_be16(pb, 0); // empty
1354 updateSize(pb, pos);
1355 }
1356 size = updateSize(pb, pos);
1357 }
1358 return size;
1359 }
1360
1361 /* iTunes meta data list */
1362 static int mov_write_ilst_tag(ByteIOContext *pb, MOVMuxContext *mov,
1363 AVFormatContext *s)
1364 {
1365 int64_t pos = url_ftell(pb);
1366 put_be32(pb, 0); /* size */
1367 put_tag(pb, "ilst");
1368 mov_write_string_metadata(s, pb, "\251nam", "title" , 1);
1369 mov_write_string_metadata(s, pb, "\251ART", "author" , 1);
1370 mov_write_string_metadata(s, pb, "\251wrt", "author" , 1);
1371 mov_write_string_metadata(s, pb, "\251alb", "album" , 1);
1372 mov_write_string_metadata(s, pb, "\251day", "year" , 1);
1373 mov_write_string_tag(pb, "\251too", LIBAVFORMAT_IDENT, 0, 1);
1374 mov_write_string_metadata(s, pb, "\251cmt", "comment" , 1);
1375 mov_write_string_metadata(s, pb, "\251gen", "genre" , 1);
1376 mov_write_string_metadata(s, pb, "\251cpy", "copyright", 1);
1377 mov_write_trkn_tag(pb, mov, s);
1378 return updateSize(pb, pos);
1379 }
1380
1381 /* iTunes meta data tag */
1382 static int mov_write_meta_tag(ByteIOContext *pb, MOVMuxContext *mov,
1383 AVFormatContext *s)
1384 {
1385 int size = 0;
1386 int64_t pos = url_ftell(pb);
1387 put_be32(pb, 0); /* size */
1388 put_tag(pb, "meta");
1389 put_be32(pb, 0);
1390 mov_write_itunes_hdlr_tag(pb, mov, s);
1391 mov_write_ilst_tag(pb, mov, s);
1392 size = updateSize(pb, pos);
1393 return size;
1394 }
1395
1396 static int utf8len(const uint8_t *b)
1397 {
1398 int len=0;
1399 int val;
1400 while(*b){
1401 GET_UTF8(val, *b++, return -1;)
1402 len++;
1403 }
1404 return len;
1405 }
1406
1407 static int ascii_to_wc(ByteIOContext *pb, const uint8_t *b)
1408 {
1409 int val;
1410 while(*b){
1411 GET_UTF8(val, *b++, return -1;)
1412 put_be16(pb, val);
1413 }
1414 put_be16(pb, 0x00);
1415 return 0;
1416 }
1417
1418 static uint16_t language_code(const char *str)
1419 {
1420 return (((str[0]-0x60) & 0x1F) << 10) + (((str[1]-0x60) & 0x1F) << 5) + ((str[2]-0x60) & 0x1F);
1421 }
1422
1423 static int mov_write_3gp_udta_tag(ByteIOContext *pb, AVFormatContext *s,
1424 const char *tag, const char *str)
1425 {
1426 int64_t pos = url_ftell(pb);
1427 AVMetadataTag *t = av_metadata_get(s->metadata, str, NULL, 0);
1428 if (!t || !utf8len(t->value))
1429 return 0;
1430 put_be32(pb, 0); /* size */
1431 put_tag (pb, tag); /* type */
1432 put_be32(pb, 0); /* version + flags */
1433 if (!strcmp(tag, "yrrc"))
1434 put_be16(pb, atoi(t->value));
1435 else {
1436 put_be16(pb, language_code("eng")); /* language */
1437 ascii_to_wc(pb, t->value);
1438 if (!strcmp(tag, "albm") &&
1439 (t = av_metadata_get(s->metadata, "year", NULL, 0)))
1440 put_byte(pb, atoi(t->value));
1441 }
1442 return updateSize(pb, pos);
1443 }
1444
1445 static int mov_write_udta_tag(ByteIOContext *pb, MOVMuxContext *mov,
1446 AVFormatContext *s)
1447 {
1448 ByteIOContext *pb_buf;
1449 int i, ret, size;
1450 uint8_t *buf;
1451
1452 for (i = 0; i < s->nb_streams; i++)
1453 if (mov->tracks[i].enc->flags & CODEC_FLAG_BITEXACT) {
1454 return 0;
1455 }
1456
1457 ret = url_open_dyn_buf(&pb_buf);
1458 if(ret < 0)
1459 return ret;
1460
1461 if (mov->mode & MODE_3GP) {
1462 mov_write_3gp_udta_tag(pb_buf, s, "titl", "title");
1463 mov_write_3gp_udta_tag(pb_buf, s, "auth", "author");
1464 mov_write_3gp_udta_tag(pb_buf, s, "gnre", "genre");
1465 mov_write_3gp_udta_tag(pb_buf, s, "dscp", "comment");
1466 mov_write_3gp_udta_tag(pb_buf, s, "albm", "album");
1467 mov_write_3gp_udta_tag(pb_buf, s, "cprt", "copyright");
1468 mov_write_3gp_udta_tag(pb_buf, s, "yrrc", "year");
1469 } else if (mov->mode == MODE_MOV) { // the title field breaks gtkpod with mp4 and my suspicion is that stuff is not valid in mp4
1470 mov_write_string_metadata(s, pb_buf, "\251nam", "title" , 0);
1471 mov_write_string_metadata(s, pb_buf, "\251aut", "author" , 0);
1472 mov_write_string_metadata(s, pb_buf, "\251alb", "album" , 0);
1473 mov_write_string_metadata(s, pb_buf, "\251day", "year" , 0);
1474 mov_write_string_tag(pb_buf, "\251enc", LIBAVFORMAT_IDENT, 0, 0);
1475 mov_write_string_metadata(s, pb_buf, "\251des", "comment" , 0);
1476 mov_write_string_metadata(s, pb_buf, "\251gen", "genre" , 0);
1477 mov_write_string_metadata(s, pb_buf, "\251cpy", "copyright" , 0);
1478 } else {
1479 /* iTunes meta data */
1480 mov_write_meta_tag(pb_buf, mov, s);
1481 }
1482
1483 if ((size = url_close_dyn_buf(pb_buf, &buf)) > 0) {
1484 put_be32(pb, size+8);
1485 put_tag(pb, "udta");
1486 put_buffer(pb, buf, size);
1487 av_free(buf);
1488 }
1489
1490 return 0;
1491 }
1492
1493 static void mov_write_psp_udta_tag(ByteIOContext *pb,
1494 const char *str, const char *lang, int type)
1495 {
1496 int len = utf8len(str)+1;
1497 if(len<=0)
1498 return;
1499 put_be16(pb, len*2+10); /* size */
1500 put_be32(pb, type); /* type */
1501 put_be16(pb, language_code(lang)); /* language */
1502 put_be16(pb, 0x01); /* ? */
1503 ascii_to_wc(pb, str);
1504 }
1505
1506 static int mov_write_uuidusmt_tag(ByteIOContext *pb, AVFormatContext *s)
1507 {
1508 AVMetadataTag *title = av_metadata_get(s->metadata, "title", NULL, 0);
1509 int64_t pos, pos2;
1510
1511 if (title) {
1512 pos = url_ftell(pb);
1513 put_be32(pb, 0); /* size placeholder*/
1514 put_tag(pb, "uuid");
1515 put_tag(pb, "USMT");
1516 put_be32(pb, 0x21d24fce); /* 96 bit UUID */
1517 put_be32(pb, 0xbb88695c);
1518 put_be32(pb, 0xfac9c740);
1519
1520 pos2 = url_ftell(pb);
1521 put_be32(pb, 0); /* size placeholder*/
1522 put_tag(pb, "MTDT");
1523 put_be16(pb, 4);
1524
1525 // ?
1526 put_be16(pb, 0x0C); /* size */
1527 put_be32(pb, 0x0B); /* type */
1528 put_be16(pb, language_code("und")); /* language */
1529 put_be16(pb, 0x0); /* ? */
1530 put_be16(pb, 0x021C); /* data */
1531
1532 mov_write_psp_udta_tag(pb, LIBAVCODEC_IDENT, "eng", 0x04);
1533 mov_write_psp_udta_tag(pb, title->value, "eng", 0x01);
1534 // snprintf(dt,32,"%04d/%02d/%02d %02d:%02d:%02d",t_st->tm_year+1900,t_st->tm_mon+1,t_st->tm_mday,t_st->tm_hour,t_st->tm_min,t_st->tm_sec);
1535 mov_write_psp_udta_tag(pb, "2006/04/01 11:11:11", "und", 0x03);
1536
1537 updateSize(pb, pos2);
1538 return updateSize(pb, pos);
1539 }
1540
1541 return 0;
1542 }
1543
1544 static int mov_write_moov_tag(ByteIOContext *pb, MOVMuxContext *mov,
1545 AVFormatContext *s)
1546 {
1547 int i;
1548 int64_t pos = url_ftell(pb);
1549 put_be32(pb, 0); /* size placeholder*/
1550 put_tag(pb, "moov");
1551 mov->timescale = globalTimescale;
1552
1553 for (i=0; i<mov->nb_streams; i++) {
1554 if(mov->tracks[i].entry <= 0) continue;
1555
1556 mov->tracks[i].time = mov->time;
1557 mov->tracks[i].trackID = i+1;
1558 }
1559
1560 mov_write_mvhd_tag(pb, mov);
1561 //mov_write_iods_tag(pb, mov);
1562 for (i=0; i<mov->nb_streams; i++) {
1563 if(mov->tracks[i].entry > 0) {
1564 mov_write_trak_tag(pb, &(mov->tracks[i]), s->streams[i]);
1565 }
1566 }
1567
1568 if (mov->mode == MODE_PSP)
1569 mov_write_uuidusmt_tag(pb, s);
1570 else
1571 mov_write_udta_tag(pb, mov, s);
1572
1573 return updateSize(pb, pos);
1574 }
1575
1576 static int mov_write_mdat_tag(ByteIOContext *pb, MOVMuxContext *mov)
1577 {
1578 put_be32(pb, 8); // placeholder for extended size field (64 bit)
1579 put_tag(pb, mov->mode == MODE_MOV ? "wide" : "free");
1580
1581 mov->mdat_pos = url_ftell(pb);
1582 put_be32(pb, 0); /* size placeholder*/
1583 put_tag(pb, "mdat");
1584 return 0;
1585 }
1586
1587 /* TODO: This needs to be more general */
1588 static int mov_write_ftyp_tag(ByteIOContext *pb, AVFormatContext *s)
1589 {
1590 MOVMuxContext *mov = s->priv_data;
1591 int64_t pos = url_ftell(pb);
1592 int has_h264 = 0, has_video = 0;
1593 int minor = 0x200;
1594 int i;
1595
1596 for (i = 0; i < s->nb_streams; i++) {
1597 AVStream *st = s->streams[i];
1598 if (st->codec->codec_type == CODEC_TYPE_VIDEO)
1599 has_video = 1;
1600 if (st->codec->codec_id == CODEC_ID_H264)
1601 has_h264 = 1;
1602 }
1603
1604 put_be32(pb, 0); /* size */
1605 put_tag(pb, "ftyp");
1606
1607 if (mov->mode == MODE_3GP) {
1608 put_tag(pb, has_h264 ? "3gp6" : "3gp4");
1609 minor = has_h264 ? 0x100 : 0x200;
1610 } else if (mov->mode & MODE_3G2) {
1611 put_tag(pb, has_h264 ? "3g2b" : "3g2a");
1612 minor = has_h264 ? 0x20000 : 0x10000;
1613 }else if (mov->mode == MODE_PSP)
1614 put_tag(pb, "MSNV");
1615 else if (mov->mode == MODE_MP4)
1616 put_tag(pb, "isom");
1617 else if (mov->mode == MODE_IPOD)
1618 put_tag(pb, has_video ? "M4V ":"M4A ");
1619 else
1620 put_tag(pb, "qt ");
1621
1622 put_be32(pb, minor);
1623
1624 if(mov->mode == MODE_MOV)
1625 put_tag(pb, "qt ");
1626 else{
1627 put_tag(pb, "isom");
1628 put_tag(pb, "iso2");
1629 if(has_h264)
1630 put_tag(pb, "avc1");
1631 }
1632
1633 if (mov->mode == MODE_3GP)
1634 put_tag(pb, has_h264 ? "3gp6":"3gp4");
1635 else if (mov->mode & MODE_3G2)
1636 put_tag(pb, has_h264 ? "3g2b":"3g2a");
1637 else if (mov->mode == MODE_PSP)
1638 put_tag(pb, "MSNV");
1639 else if (mov->mode == MODE_MP4)
1640 put_tag(pb, "mp41");
1641 return updateSize(pb, pos);
1642 }
1643
1644 static void mov_write_uuidprof_tag(ByteIOContext *pb, AVFormatContext *s)
1645 {
1646 AVCodecContext *VideoCodec = s->streams[0]->codec;
1647 AVCodecContext *AudioCodec = s->streams[1]->codec;
1648 int AudioRate = AudioCodec->sample_rate;
1649 int FrameRate = ((VideoCodec->time_base.den) * (0x10000))/ (VideoCodec->time_base.num);
1650 int audio_kbitrate= AudioCodec->bit_rate / 1000;
1651 int video_kbitrate= FFMIN(VideoCodec->bit_rate / 1000, 800 - audio_kbitrate);
1652
1653 put_be32(pb, 0x94); /* size */
1654 put_tag(pb, "uuid");
1655 put_tag(pb, "PROF");
1656
1657 put_be32(pb, 0x21d24fce); /* 96 bit UUID */
1658 put_be32(pb, 0xbb88695c);
1659 put_be32(pb, 0xfac9c740);
1660
1661 put_be32(pb, 0x0); /* ? */
1662 put_be32(pb, 0x3); /* 3 sections ? */
1663
1664 put_be32(pb, 0x14); /* size */
1665 put_tag(pb, "FPRF");
1666 put_be32(pb, 0x0); /* ? */
1667 put_be32(pb, 0x0); /* ? */
1668 put_be32(pb, 0x0); /* ? */
1669
1670 put_be32(pb, 0x2c); /* size */
1671 put_tag(pb, "APRF"); /* audio */
1672 put_be32(pb, 0x0);
1673 put_be32(pb, 0x2); /* TrackID */
1674 put_tag(pb, "mp4a");
1675 put_be32(pb, 0x20f);
1676 put_be32(pb, 0x0);
1677 put_be32(pb, audio_kbitrate);
1678 put_be32(pb, audio_kbitrate);
1679 put_be32(pb, AudioRate);
1680 put_be32(pb, AudioCodec->channels);
1681
1682 put_be32(pb, 0x34); /* size */
1683 put_tag(pb, "VPRF"); /* video */
1684 put_be32(pb, 0x0);
1685 put_be32(pb, 0x1); /* TrackID */
1686 if (VideoCodec->codec_id == CODEC_ID_H264) {
1687 put_tag(pb, "avc1");
1688 put_be16(pb, 0x014D);
1689 put_be16(pb, 0x0015);
1690 } else {
1691 put_tag(pb, "mp4v");
1692 put_be16(pb, 0x0000);
1693 put_be16(pb, 0x0103);
1694 }
1695 put_be32(pb, 0x0);
1696 put_be32(pb, video_kbitrate);
1697 put_be32(pb, video_kbitrate);
1698 put_be32(pb, FrameRate);
1699 put_be32(pb, FrameRate);
1700 put_be16(pb, VideoCodec->width);
1701 put_be16(pb, VideoCodec->height);
1702 put_be32(pb, 0x010001); /* ? */
1703 }
1704
1705 static int mov_write_header(AVFormatContext *s)
1706 {
1707 ByteIOContext *pb = s->pb;
1708 MOVMuxContext *mov = s->priv_data;
1709 int i;
1710
1711 if (url_is_streamed(s->pb)) {
1712 av_log(s, AV_LOG_ERROR, "muxer does not support non seekable output\n");
1713 return -1;
1714 }
1715
1716 /* Default mode == MP4 */
1717 mov->mode = MODE_MP4;
1718
1719 if (s->oformat != NULL) {
1720 if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
1721 else if (!strcmp("3g2", s->oformat->name)) mov->mode = MODE_3GP|MODE_3G2;
1722 else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
1723 else if (!strcmp("psp", s->oformat->name)) mov->mode = MODE_PSP;
1724 else if (!strcmp("ipod",s->oformat->name)) mov->mode = MODE_IPOD;
1725
1726 mov_write_ftyp_tag(pb,s);
1727 if (mov->mode == MODE_PSP) {
1728 if (s->nb_streams != 2) {
1729 av_log(s, AV_LOG_ERROR, "PSP mode need one video and one audio stream\n");
1730 return -1;
1731 }
1732 mov_write_uuidprof_tag(pb,s);
1733 }
1734 }
1735
1736 mov->tracks = av_mallocz(s->nb_streams*sizeof(*mov->tracks));
1737 if (!mov->tracks)
1738 return AVERROR(ENOMEM);
1739
1740 for(i=0; i<s->nb_streams; i++){
1741 AVStream *st= s->streams[i];
1742 MOVTrack *track= &mov->tracks[i];
1743 AVMetadataTag *lang = av_metadata_get(st->metadata, "language", NULL,0);
1744
1745 track->enc = st->codec;
1746 track->language = ff_mov_iso639_to_lang(lang?lang->value:"und", mov->mode!=MODE_MOV);
1747 if (track->language < 0)
1748 track->language = 0;
1749 track->mode = mov->mode;
1750 track->tag = mov_find_codec_tag(s, track);
1751 if (!track->tag) {
1752 av_log(s, AV_LOG_ERROR, "track %d: could not find tag, "
1753 "codec not currently supported in container\n", i);
1754 goto error;
1755 }
1756 if(st->codec->codec_type == CODEC_TYPE_VIDEO){
1757 if (track->tag == MKTAG('m','x','3','p') || track->tag == MKTAG('m','x','3','n') ||
1758 track->tag == MKTAG('m','x','4','p') || track->tag == MKTAG('m','x','4','n') ||
1759 track->tag == MKTAG('m','x','5','p') || track->tag == MKTAG('m','x','5','n')) {
1760 if (st->codec->width != 720 || (st->codec->height != 608 && st->codec->height != 512)) {
1761 av_log(s, AV_LOG_ERROR, "D-10/IMX must use 720x608 or 720x512 video resolution\n");
1762 goto error;
1763 }
1764 track->height = track->tag>>24 == 'n' ? 486 : 576;
1765 }
1766 track->timescale = st->codec->time_base.den;
1767 if (track->mode == MODE_MOV && track->timescale > 100000)
1768 av_log(s, AV_LOG_WARNING,
1769 "WARNING codec timebase is very high. If duration is too long,\n"
1770 "file may not be playable by quicktime. Specify a shorter timebase\n"
1771 "or choose different container.\n");
1772 }else if(st->codec->codec_type == CODEC_TYPE_AUDIO){
1773 track->timescale = st->codec->sample_rate;
1774 if(!st->codec->frame_size && !av_get_bits_per_sample(st->codec->codec_id)) {
1775 av_log(s, AV_LOG_ERROR, "track %d: codec frame size is not set\n", i);
1776 goto error;
1777 }else if(st->codec->frame_size > 1){ /* assume compressed audio */
1778 track->audio_vbr = 1;
1779 }else{
1780 st->codec->frame_size = 1;
1781 track->sampleSize = (av_get_bits_per_sample(st->codec->codec_id) >> 3) * st->codec->channels;
1782 }
1783 if(track->mode != MODE_MOV &&
1784 track->enc->codec_id == CODEC_ID_MP3 && track->enc->sample_rate < 16000){
1785 av_log(s, AV_LOG_ERROR, "track %d: muxing mp3 at %dhz is not supported\n",
1786 i, track->enc->sample_rate);
1787 goto error;
1788 }
1789 }else if(st->codec->codec_type == CODEC_TYPE_SUBTITLE){
1790 track->timescale = st->codec->time_base.den;
1791 }
1792 if (!track->height)
1793 track->height = st->codec->height;
1794
1795 av_set_pts_info(st, 64, 1, track->timescale);
1796 }
1797
1798 mov_write_mdat_tag(pb, mov);
1799 mov->time = s->timestamp + 0x7C25B080; //1970 based -> 1904 based
1800 mov->nb_streams = s->nb_streams;
1801
1802 put_flush_packet(pb);
1803
1804 return 0;
1805 error:
1806 av_freep(&mov->tracks);
1807 return -1;
1808 }
1809
1810 static int mov_parse_mpeg2_frame(AVPacket *pkt, uint32_t *flags)
1811 {
1812 uint32_t c = -1;
1813 int i, closed_gop = 0;
1814
1815 for (i = 0; i < pkt->size - 4; i++) {
1816 c = (c<<8) + pkt->data[i];
1817 if (c == 0x1b8) { // gop
1818 closed_gop = pkt->data[i+4]>>6 & 0x01;
1819 } else if (c == 0x100) { // pic
1820 int temp_ref = (pkt->data[i+1]<<2) | (pkt->data[i+2]>>6);
1821 if (!temp_ref || closed_gop) // I picture is not reordered
1822 *flags = MOV_SYNC_SAMPLE;
1823 else
1824 *flags = MOV_PARTIAL_SYNC_SAMPLE;
1825 break;
1826 }
1827 }
1828 return 0;
1829 }
1830
1831 static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
1832 {
1833 MOVMuxContext *mov = s->priv_data;
1834 ByteIOContext *pb = s->pb;
1835 MOVTrack *trk = &mov->tracks[pkt->stream_index];
1836 AVCodecContext *enc = trk->enc;
1837 unsigned int samplesInChunk = 0;
1838 int size= pkt->size;
1839
1840 if (url_is_streamed(s->pb)) return 0; /* Can't handle that */
1841 if (!size) return 0; /* Discard 0 sized packets */
1842
1843 if (enc->codec_id == CODEC_ID_AMR_NB) {
1844 /* We must find out how many AMR blocks there are in one packet */
1845 static uint16_t packed_size[16] =
1846 {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 0};
1847 int len = 0;
1848
1849 while (len < size && samplesInChunk < 100) {
1850 len += packed_size[(pkt->data[len] >> 3) & 0x0F];
1851 samplesInChunk++;
1852 }
1853 if(samplesInChunk > 1){
1854 av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, implement a AVParser for it\n");
1855 return -1;
1856 }
1857 } else if (trk->sampleSize)
1858 samplesInChunk = size/trk->sampleSize;
1859 else
1860 samplesInChunk = 1;
1861
1862 /* copy extradata if it exists */
1863 if (trk->vosLen == 0 && enc->extradata_size > 0) {
1864 trk->vosLen = enc->extradata_size;
1865 trk->vosData = av_malloc(trk->vosLen);
1866 memcpy(trk->vosData, enc->extradata, trk->vosLen);
1867 }
1868
1869 if (enc->codec_id == CODEC_ID_H264 && trk->vosLen > 0 && *(uint8_t *)trk->vosData != 1) {
1870 /* from x264 or from bytestream h264 */
1871 /* nal reformating needed */
1872 size = ff_avc_parse_nal_units(pb, pkt->data, pkt->size);
1873 } else {
1874 put_buffer(pb, pkt->data, size);
1875 }
1876
1877 if ((enc->codec_id == CODEC_ID_DNXHD ||
1878 enc->codec_id == CODEC_ID_AC3) && !trk->vosLen) {
1879 /* copy frame to create needed atoms */
1880 trk->vosLen = size;
1881 trk->vosData = av_malloc(size);
1882 if (!trk->vosData)
1883 return AVERROR(ENOMEM);
1884 memcpy(trk->vosData, pkt->data, size);
1885 }
1886
1887 if (!(trk->entry % MOV_INDEX_CLUSTER_SIZE)) {
1888 trk->cluster = av_realloc(trk->cluster, (trk->entry + MOV_INDEX_CLUSTER_SIZE) * sizeof(*trk->cluster));
1889 if (!trk->cluster)
1890 return -1;
1891 }
1892
1893 trk->cluster[trk->entry].pos = url_ftell(pb) - size;
1894 trk->cluster[trk->entry].samplesInChunk = samplesInChunk;
1895 trk->cluster[trk->entry].size = size;
1896 trk->cluster[trk->entry].entries = samplesInChunk;
1897 trk->cluster[trk->entry].dts = pkt->dts;
1898 trk->trackDuration = pkt->dts - trk->cluster[0].dts + pkt->duration;
1899
1900 if (pkt->pts == AV_NOPTS_VALUE) {
1901 av_log(s, AV_LOG_WARNING, "pts has no value\n");
1902 pkt->pts = pkt->dts;
1903 }
1904 if (pkt->dts != pkt->pts)
1905 trk->flags |= MOV_TRACK_CTTS;
1906 trk->cluster[trk->entry].cts = pkt->pts - pkt->dts;
1907 trk->cluster[trk->entry].flags = 0;
1908 if (pkt->flags & PKT_FLAG_KEY) {
1909 if (mov->mode == MODE_MOV && enc->codec_id == CODEC_ID_MPEG2VIDEO) {
1910 mov_parse_mpeg2_frame(pkt, &trk->cluster[trk->entry].flags);
1911 if (trk->cluster[trk->entry].flags & MOV_PARTIAL_SYNC_SAMPLE)
1912 trk->flags |= MOV_TRACK_STPS;
1913 } else {
1914 trk->cluster[trk->entry].flags = MOV_SYNC_SAMPLE;
1915 }
1916 if (trk->cluster[trk->entry].flags & MOV_SYNC_SAMPLE)
1917 trk->hasKeyframes++;
1918 }
1919 trk->entry++;
1920 trk->sampleCount += samplesInChunk;
1921 mov->mdat_size += size;
1922
1923 put_flush_packet(pb);
1924 return 0;
1925 }
1926
1927 static int mov_write_trailer(AVFormatContext *s)
1928 {
1929 MOVMuxContext *mov = s->priv_data;
1930 ByteIOContext *pb = s->pb;
1931 int res = 0;
1932 int i;
1933
1934 int64_t moov_pos = url_ftell(pb);
1935
1936 /* Write size of mdat tag */
1937 if (mov->mdat_size+8 <= UINT32_MAX) {
1938 url_fseek(pb, mov->mdat_pos, SEEK_SET);
1939 put_be32(pb, mov->mdat_size+8);
1940 } else {
1941 /* overwrite 'wide' placeholder atom */
1942 url_fseek(pb, mov->mdat_pos - 8, SEEK_SET);
1943 put_be32(pb, 1); /* special value: real atom size will be 64 bit value after tag field */
1944 put_tag(pb, "mdat");
1945 put_be64(pb, mov->mdat_size+16);
1946 }
1947 url_fseek(pb, moov_pos, SEEK_SET);
1948
1949 mov_write_moov_tag(pb, mov, s);
1950
1951 for (i=0; i<mov->nb_streams; i++) {
1952 av_freep(&mov->tracks[i].cluster);
1953
1954 if(mov->tracks[i].vosLen) av_free(mov->tracks[i].vosData);
1955
1956 }
1957
1958 put_flush_packet(pb);
1959
1960 av_freep(&mov->tracks);
1961
1962 return res;
1963 }
1964
1965 #if CONFIG_MOV_MUXER
1966 AVOutputFormat mov_muxer = {
1967 "mov",
1968 NULL_IF_CONFIG_SMALL("MOV format"),
1969 NULL,
1970 "mov",
1971 sizeof(MOVMuxContext),
1972 CODEC_ID_AAC,
1973 CODEC_ID_MPEG4,
1974 mov_write_header,
1975 mov_write_packet,
1976 mov_write_trailer,
1977 .flags = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS,
1978 .codec_tag = (const AVCodecTag* const []){codec_movvideo_tags, codec_movaudio_tags, 0},
1979 };
1980 #endif
1981 #if CONFIG_TGP_MUXER
1982 AVOutputFormat tgp_muxer = {
1983 "3gp",
1984 NULL_IF_CONFIG_SMALL("3GP format"),
1985 NULL,
1986 "3gp",
1987 sizeof(MOVMuxContext),
1988 CODEC_ID_AMR_NB,
1989 CODEC_ID_H263,
1990 mov_write_header,
1991 mov_write_packet,
1992 mov_write_trailer,
1993 .flags = AVFMT_GLOBALHEADER,
1994 .codec_tag = (const AVCodecTag* const []){codec_3gp_tags, 0},
1995 };
1996 #endif
1997 #if CONFIG_MP4_MUXER
1998 AVOutputFormat mp4_muxer = {
1999 "mp4",
2000 NULL_IF_CONFIG_SMALL("MP4 format"),
2001 "application/mp4",
2002 "mp4",
2003 sizeof(MOVMuxContext),
2004 CODEC_ID_AAC,
2005 CODEC_ID_MPEG4,
2006 mov_write_header,
2007 mov_write_packet,
2008 mov_write_trailer,
2009 .flags = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS,
2010 .codec_tag = (const AVCodecTag* const []){ff_mp4_obj_type, 0},
2011 };
2012 #endif
2013 #if CONFIG_PSP_MUXER
2014 AVOutputFormat psp_muxer = {
2015 "psp",
2016 NULL_IF_CONFIG_SMALL("PSP MP4 format"),
2017 NULL,
2018 "mp4,psp",
2019 sizeof(MOVMuxContext),
2020 CODEC_ID_AAC,
2021 CODEC_ID_MPEG4,
2022 mov_write_header,
2023 mov_write_packet,
2024 mov_write_trailer,
2025 .flags = AVFMT_GLOBALHEADER,
2026 .codec_tag = (const AVCodecTag* const []){ff_mp4_obj_type, 0},
2027 };
2028 #endif
2029 #if CONFIG_TG2_MUXER
2030 AVOutputFormat tg2_muxer = {
2031 "3g2",
2032 NULL_IF_CONFIG_SMALL("3GP2 format"),
2033 NULL,
2034 "3g2",
2035 sizeof(MOVMuxContext),
2036 CODEC_ID_AMR_NB,
2037 CODEC_ID_H263,
2038 mov_write_header,
2039 mov_write_packet,
2040 mov_write_trailer,
2041 .flags = AVFMT_GLOBALHEADER,
2042 .codec_tag = (const AVCodecTag* const []){codec_3gp_tags, 0},
2043 };
2044 #endif
2045 #if CONFIG_IPOD_MUXER
2046 AVOutputFormat ipod_muxer = {
2047 "ipod",
2048 NULL_IF_CONFIG_SMALL("iPod H.264 MP4 format"),
2049 "application/mp4",
2050 "m4v,m4a",
2051 sizeof(MOVMuxContext),
2052 CODEC_ID_AAC,
2053 CODEC_ID_H264,
2054 mov_write_header,
2055 mov_write_packet,
2056 mov_write_trailer,
2057 .flags = AVFMT_GLOBALHEADER,
2058 .codec_tag = (const AVCodecTag* const []){codec_ipod_tags, 0},
2059 };
2060 #endif