92a6f3016ee6d323fd1a6975a44c7158fa47034c
[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, ff_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 (!ff_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 = ff_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 = ff_codec_get_tag(codec_movvideo_tags, track->enc->codec_id);
661 if (!tag) { // if no mac fcc found, try with Microsoft tags
662 tag = ff_codec_get_tag(ff_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 = ff_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 = ff_codec_get_tag(ff_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 = ff_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 = ff_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 if (!track || track->mode == MODE_MOV)
1017 put_byte(pb, strlen(descr)); /* pascal string */
1018 put_buffer(pb, descr, strlen(descr)); /* handler description */
1019 if (track && track->mode != MODE_MOV)
1020 put_byte(pb, 0); /* c string */
1021 return updateSize(pb, pos);
1022 }
1023
1024 static int mov_write_minf_tag(ByteIOContext *pb, MOVTrack *track)
1025 {
1026 int64_t pos = url_ftell(pb);
1027 put_be32(pb, 0); /* size */
1028 put_tag(pb, "minf");
1029 if(track->enc->codec_type == CODEC_TYPE_VIDEO)
1030 mov_write_vmhd_tag(pb);
1031 else if (track->enc->codec_type == CODEC_TYPE_AUDIO)
1032 mov_write_smhd_tag(pb);
1033 else if (track->enc->codec_type == CODEC_TYPE_SUBTITLE) {
1034 if (track->tag == MKTAG('t','e','x','t')) mov_write_gmhd_tag(pb);
1035 else mov_write_nmhd_tag(pb);
1036 }
1037 if (track->mode == MODE_MOV) /* FIXME: Why do it for MODE_MOV only ? */
1038 mov_write_hdlr_tag(pb, NULL);
1039 mov_write_dinf_tag(pb);
1040 mov_write_stbl_tag(pb, track);
1041 return updateSize(pb, pos);
1042 }
1043
1044 static int mov_write_mdhd_tag(ByteIOContext *pb, MOVTrack *track)
1045 {
1046 int version = track->trackDuration < INT32_MAX ? 0 : 1;
1047
1048 (version == 1) ? put_be32(pb, 44) : put_be32(pb, 32); /* size */
1049 put_tag(pb, "mdhd");
1050 put_byte(pb, version);
1051 put_be24(pb, 0); /* flags */
1052 if (version == 1) {
1053 put_be64(pb, track->time);
1054 put_be64(pb, track->time);
1055 } else {
1056 put_be32(pb, track->time); /* creation time */
1057 put_be32(pb, track->time); /* modification time */
1058 }
1059 put_be32(pb, track->timescale); /* time scale (sample rate for audio) */
1060 (version == 1) ? put_be64(pb, track->trackDuration) : put_be32(pb, track->trackDuration); /* duration */
1061 put_be16(pb, track->language); /* language */
1062 put_be16(pb, 0); /* reserved (quality) */
1063
1064 if(version!=0 && track->mode == MODE_MOV){
1065 av_log(NULL, AV_LOG_ERROR,
1066 "FATAL error, file duration too long for timebase, this file will not be\n"
1067 "playable with quicktime. Choose a different timebase or a different\n"
1068 "container format\n");
1069 }
1070
1071 return 32;
1072 }
1073
1074 static int mov_write_mdia_tag(ByteIOContext *pb, MOVTrack *track)
1075 {
1076 int64_t pos = url_ftell(pb);
1077 put_be32(pb, 0); /* size */
1078 put_tag(pb, "mdia");
1079 mov_write_mdhd_tag(pb, track);
1080 mov_write_hdlr_tag(pb, track);
1081 mov_write_minf_tag(pb, track);
1082 return updateSize(pb, pos);
1083 }
1084
1085 static int mov_write_tkhd_tag(ByteIOContext *pb, MOVTrack *track, AVStream *st)
1086 {
1087 int64_t duration = av_rescale_rnd(track->trackDuration, globalTimescale, track->timescale, AV_ROUND_UP);
1088 int version = duration < INT32_MAX ? 0 : 1;
1089
1090 (version == 1) ? put_be32(pb, 104) : put_be32(pb, 92); /* size */
1091 put_tag(pb, "tkhd");
1092 put_byte(pb, version);
1093 put_be24(pb, 0xf); /* flags (track enabled) */
1094 if (version == 1) {
1095 put_be64(pb, track->time);
1096 put_be64(pb, track->time);
1097 } else {
1098 put_be32(pb, track->time); /* creation time */
1099 put_be32(pb, track->time); /* modification time */
1100 }
1101 put_be32(pb, track->trackID); /* track-id */
1102 put_be32(pb, 0); /* reserved */
1103 (version == 1) ? put_be64(pb, duration) : put_be32(pb, duration);
1104
1105 put_be32(pb, 0); /* reserved */
1106 put_be32(pb, 0); /* reserved */
1107 put_be32(pb, 0x0); /* reserved (Layer & Alternate group) */
1108 /* Volume, only for audio */
1109 if(track->enc->codec_type == CODEC_TYPE_AUDIO)
1110 put_be16(pb, 0x0100);
1111 else
1112 put_be16(pb, 0);
1113 put_be16(pb, 0); /* reserved */
1114
1115 /* Matrix structure */
1116 put_be32(pb, 0x00010000); /* reserved */
1117 put_be32(pb, 0x0); /* reserved */
1118 put_be32(pb, 0x0); /* reserved */
1119 put_be32(pb, 0x0); /* reserved */
1120 put_be32(pb, 0x00010000); /* reserved */
1121 put_be32(pb, 0x0); /* reserved */
1122 put_be32(pb, 0x0); /* reserved */
1123 put_be32(pb, 0x0); /* reserved */
1124 put_be32(pb, 0x40000000); /* reserved */
1125
1126 /* Track width and height, for visual only */
1127 if(track->enc->codec_type == CODEC_TYPE_VIDEO ||
1128 track->enc->codec_type == CODEC_TYPE_SUBTITLE) {
1129 double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
1130 if(!sample_aspect_ratio || track->height != track->enc->height)
1131 sample_aspect_ratio = 1;
1132 put_be32(pb, sample_aspect_ratio * track->enc->width*0x10000);
1133 put_be32(pb, track->height*0x10000);
1134 }
1135 else {
1136 put_be32(pb, 0);
1137 put_be32(pb, 0);
1138 }
1139 return 0x5c;
1140 }
1141
1142 // This box seems important for the psp playback ... without it the movie seems to hang
1143 static int mov_write_edts_tag(ByteIOContext *pb, MOVTrack *track)
1144 {
1145 put_be32(pb, 0x24); /* size */
1146 put_tag(pb, "edts");
1147 put_be32(pb, 0x1c); /* size */
1148 put_tag(pb, "elst");
1149 put_be32(pb, 0x0);
1150 put_be32(pb, 0x1);
1151
1152 put_be32(pb, av_rescale_rnd(track->trackDuration, globalTimescale, track->timescale, AV_ROUND_UP)); /* duration ... doesn't seem to effect psp */
1153
1154 put_be32(pb, track->cluster[0].cts); /* first pts is cts since dts is 0 */
1155 put_be32(pb, 0x00010000);
1156 return 0x24;
1157 }
1158
1159 // goes at the end of each track! ... Critical for PSP playback ("Incompatible data" without it)
1160 static int mov_write_uuid_tag_psp(ByteIOContext *pb, MOVTrack *mov)
1161 {
1162 put_be32(pb, 0x34); /* size ... reports as 28 in mp4box! */
1163 put_tag(pb, "uuid");
1164 put_tag(pb, "USMT");
1165 put_be32(pb, 0x21d24fce);
1166 put_be32(pb, 0xbb88695c);
1167 put_be32(pb, 0xfac9c740);
1168 put_be32(pb, 0x1c); // another size here!
1169 put_tag(pb, "MTDT");
1170 put_be32(pb, 0x00010012);
1171 put_be32(pb, 0x0a);
1172 put_be32(pb, 0x55c40000);
1173 put_be32(pb, 0x1);
1174 put_be32(pb, 0x0);
1175 return 0x34;
1176 }
1177
1178 static int mov_write_trak_tag(ByteIOContext *pb, MOVTrack *track, AVStream *st)
1179 {
1180 int64_t pos = url_ftell(pb);
1181 put_be32(pb, 0); /* size */
1182 put_tag(pb, "trak");
1183 mov_write_tkhd_tag(pb, track, st);
1184 if (track->mode == MODE_PSP || track->flags & MOV_TRACK_CTTS)
1185 mov_write_edts_tag(pb, track); // PSP Movies require edts box
1186 mov_write_mdia_tag(pb, track);
1187 if (track->mode == MODE_PSP)
1188 mov_write_uuid_tag_psp(pb,track); // PSP Movies require this uuid box
1189 return updateSize(pb, pos);
1190 }
1191
1192 #if 0
1193 /* TODO: Not sorted out, but not necessary either */
1194 static int mov_write_iods_tag(ByteIOContext *pb, MOVMuxContext *mov)
1195 {
1196 put_be32(pb, 0x15); /* size */
1197 put_tag(pb, "iods");
1198 put_be32(pb, 0); /* version & flags */
1199 put_be16(pb, 0x1007);
1200 put_byte(pb, 0);
1201 put_be16(pb, 0x4fff);
1202 put_be16(pb, 0xfffe);
1203 put_be16(pb, 0x01ff);
1204 return 0x15;
1205 }
1206 #endif
1207
1208 static int mov_write_mvhd_tag(ByteIOContext *pb, MOVMuxContext *mov)
1209 {
1210 int maxTrackID = 1, i;
1211 int64_t maxTrackLenTemp, maxTrackLen = 0;
1212 int version;
1213
1214 for (i=0; i<mov->nb_streams; i++) {
1215 if(mov->tracks[i].entry > 0) {
1216 maxTrackLenTemp = av_rescale_rnd(mov->tracks[i].trackDuration, globalTimescale, mov->tracks[i].timescale, AV_ROUND_UP);
1217 if(maxTrackLen < maxTrackLenTemp)
1218 maxTrackLen = maxTrackLenTemp;
1219 if(maxTrackID < mov->tracks[i].trackID)
1220 maxTrackID = mov->tracks[i].trackID;
1221 }
1222 }
1223
1224 version = maxTrackLen < UINT32_MAX ? 0 : 1;
1225 (version == 1) ? put_be32(pb, 120) : put_be32(pb, 108); /* size */
1226 put_tag(pb, "mvhd");
1227 put_byte(pb, version);
1228 put_be24(pb, 0); /* flags */
1229 if (version == 1) {
1230 put_be64(pb, mov->time);
1231 put_be64(pb, mov->time);
1232 } else {
1233 put_be32(pb, mov->time); /* creation time */
1234 put_be32(pb, mov->time); /* modification time */
1235 }
1236 put_be32(pb, mov->timescale); /* timescale */
1237 (version == 1) ? put_be64(pb, maxTrackLen) : put_be32(pb, maxTrackLen); /* duration of longest track */
1238
1239 put_be32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
1240 put_be16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
1241 put_be16(pb, 0); /* reserved */
1242 put_be32(pb, 0); /* reserved */
1243 put_be32(pb, 0); /* reserved */
1244
1245 /* Matrix structure */
1246 put_be32(pb, 0x00010000); /* reserved */
1247 put_be32(pb, 0x0); /* reserved */
1248 put_be32(pb, 0x0); /* reserved */
1249 put_be32(pb, 0x0); /* reserved */
1250 put_be32(pb, 0x00010000); /* reserved */
1251 put_be32(pb, 0x0); /* reserved */
1252 put_be32(pb, 0x0); /* reserved */
1253 put_be32(pb, 0x0); /* reserved */
1254 put_be32(pb, 0x40000000); /* reserved */
1255
1256 put_be32(pb, 0); /* reserved (preview time) */
1257 put_be32(pb, 0); /* reserved (preview duration) */
1258 put_be32(pb, 0); /* reserved (poster time) */
1259 put_be32(pb, 0); /* reserved (selection time) */
1260 put_be32(pb, 0); /* reserved (selection duration) */
1261 put_be32(pb, 0); /* reserved (current time) */
1262 put_be32(pb, maxTrackID+1); /* Next track id */
1263 return 0x6c;
1264 }
1265
1266 static int mov_write_itunes_hdlr_tag(ByteIOContext *pb, MOVMuxContext *mov,
1267 AVFormatContext *s)
1268 {
1269 int64_t pos = url_ftell(pb);
1270 put_be32(pb, 0); /* size */
1271 put_tag(pb, "hdlr");
1272 put_be32(pb, 0);
1273 put_be32(pb, 0);
1274 put_tag(pb, "mdir");
1275 put_tag(pb, "appl");
1276 put_be32(pb, 0);
1277 put_be32(pb, 0);
1278 put_byte(pb, 0);
1279 return updateSize(pb, pos);
1280 }
1281
1282 /* helper function to write a data tag with the specified string as data */
1283 static int mov_write_string_data_tag(ByteIOContext *pb, const char *data, int lang, int long_style)
1284 {
1285 if(long_style){
1286 int64_t pos = url_ftell(pb);
1287 put_be32(pb, 0); /* size */
1288 put_tag(pb, "data");
1289 put_be32(pb, 1);
1290 put_be32(pb, 0);
1291 put_buffer(pb, data, strlen(data));
1292 return updateSize(pb, pos);
1293 }else{
1294 put_be16(pb, strlen(data)); /* string length */
1295 put_be16(pb, lang);
1296 put_buffer(pb, data, strlen(data));
1297 return strlen(data) + 4;
1298 }
1299 }
1300
1301 static int mov_write_string_tag(ByteIOContext *pb, const char *name, const char *value, int lang, int long_style){
1302 int size = 0;
1303 if (value && value[0]) {
1304 int64_t pos = url_ftell(pb);
1305 put_be32(pb, 0); /* size */
1306 put_tag(pb, name);
1307 mov_write_string_data_tag(pb, value, lang, long_style);
1308 size= updateSize(pb, pos);
1309 }
1310 return size;
1311 }
1312
1313 static int mov_write_string_metadata(AVFormatContext *s, ByteIOContext *pb,
1314 const char *name, const char *tag,
1315 int long_style)
1316 {
1317 int l, lang = 0, len, len2;
1318 AVMetadataTag *t, *t2 = NULL;
1319 char tag2[16];
1320
1321 if (!(t = av_metadata_get(s->metadata, tag, NULL, 0)))
1322 return 0;
1323
1324 len = strlen(t->key);
1325 snprintf(tag2, sizeof(tag2), "%s-", tag);
1326 while ((t2 = av_metadata_get(s->metadata, tag2, t2, AV_METADATA_IGNORE_SUFFIX))) {
1327 len2 = strlen(t2->key);
1328 if (len2 == len+4 && !strcmp(t->value, t2->value)
1329 && (l=ff_mov_iso639_to_lang(&t2->key[len2-3], 0)) >= 0) {
1330 lang = l;
1331 break;
1332 }
1333 }
1334 return mov_write_string_tag(pb, name, t->value, lang, long_style);
1335 }
1336
1337 /* iTunes track number */
1338 static int mov_write_trkn_tag(ByteIOContext *pb, MOVMuxContext *mov,
1339 AVFormatContext *s)
1340 {
1341 AVMetadataTag *t = av_metadata_get(s->metadata, "track", NULL, 0);
1342 int size = 0, track = t ? atoi(t->value) : 0;
1343 if (track) {
1344 int64_t pos = url_ftell(pb);
1345 put_be32(pb, 0); /* size */
1346 put_tag(pb, "trkn");
1347 {
1348 int64_t pos = url_ftell(pb);
1349 put_be32(pb, 0); /* size */
1350 put_tag(pb, "data");
1351 put_be32(pb, 0); // 8 bytes empty
1352 put_be32(pb, 0);
1353 put_be16(pb, 0); // empty
1354 put_be16(pb, track); // track number
1355 put_be16(pb, 0); // total track number
1356 put_be16(pb, 0); // empty
1357 updateSize(pb, pos);
1358 }
1359 size = updateSize(pb, pos);
1360 }
1361 return size;
1362 }
1363
1364 /* iTunes meta data list */
1365 static int mov_write_ilst_tag(ByteIOContext *pb, MOVMuxContext *mov,
1366 AVFormatContext *s)
1367 {
1368 int64_t pos = url_ftell(pb);
1369 put_be32(pb, 0); /* size */
1370 put_tag(pb, "ilst");
1371 mov_write_string_metadata(s, pb, "\251nam", "title" , 1);
1372 mov_write_string_metadata(s, pb, "\251ART", "author" , 1);
1373 mov_write_string_metadata(s, pb, "\251wrt", "author" , 1);
1374 mov_write_string_metadata(s, pb, "\251alb", "album" , 1);
1375 mov_write_string_metadata(s, pb, "\251day", "year" , 1);
1376 mov_write_string_tag(pb, "\251too", LIBAVFORMAT_IDENT, 0, 1);
1377 mov_write_string_metadata(s, pb, "\251cmt", "comment" , 1);
1378 mov_write_string_metadata(s, pb, "\251gen", "genre" , 1);
1379 mov_write_string_metadata(s, pb, "\251cpy", "copyright", 1);
1380 mov_write_trkn_tag(pb, mov, s);
1381 return updateSize(pb, pos);
1382 }
1383
1384 /* iTunes meta data tag */
1385 static int mov_write_meta_tag(ByteIOContext *pb, MOVMuxContext *mov,
1386 AVFormatContext *s)
1387 {
1388 int size = 0;
1389 int64_t pos = url_ftell(pb);
1390 put_be32(pb, 0); /* size */
1391 put_tag(pb, "meta");
1392 put_be32(pb, 0);
1393 mov_write_itunes_hdlr_tag(pb, mov, s);
1394 mov_write_ilst_tag(pb, mov, s);
1395 size = updateSize(pb, pos);
1396 return size;
1397 }
1398
1399 static int utf8len(const uint8_t *b)
1400 {
1401 int len=0;
1402 int val;
1403 while(*b){
1404 GET_UTF8(val, *b++, return -1;)
1405 len++;
1406 }
1407 return len;
1408 }
1409
1410 static int ascii_to_wc(ByteIOContext *pb, const uint8_t *b)
1411 {
1412 int val;
1413 while(*b){
1414 GET_UTF8(val, *b++, return -1;)
1415 put_be16(pb, val);
1416 }
1417 put_be16(pb, 0x00);
1418 return 0;
1419 }
1420
1421 static uint16_t language_code(const char *str)
1422 {
1423 return (((str[0]-0x60) & 0x1F) << 10) + (((str[1]-0x60) & 0x1F) << 5) + ((str[2]-0x60) & 0x1F);
1424 }
1425
1426 static int mov_write_3gp_udta_tag(ByteIOContext *pb, AVFormatContext *s,
1427 const char *tag, const char *str)
1428 {
1429 int64_t pos = url_ftell(pb);
1430 AVMetadataTag *t = av_metadata_get(s->metadata, str, NULL, 0);
1431 if (!t || !utf8len(t->value))
1432 return 0;
1433 put_be32(pb, 0); /* size */
1434 put_tag (pb, tag); /* type */
1435 put_be32(pb, 0); /* version + flags */
1436 if (!strcmp(tag, "yrrc"))
1437 put_be16(pb, atoi(t->value));
1438 else {
1439 put_be16(pb, language_code("eng")); /* language */
1440 put_buffer(pb, t->value, strlen(t->value)+1); /* UTF8 string value */
1441 if (!strcmp(tag, "albm") &&
1442 (t = av_metadata_get(s->metadata, "year", NULL, 0)))
1443 put_byte(pb, atoi(t->value));
1444 }
1445 return updateSize(pb, pos);
1446 }
1447
1448 static int mov_write_udta_tag(ByteIOContext *pb, MOVMuxContext *mov,
1449 AVFormatContext *s)
1450 {
1451 ByteIOContext *pb_buf;
1452 int i, ret, size;
1453 uint8_t *buf;
1454
1455 for (i = 0; i < s->nb_streams; i++)
1456 if (mov->tracks[i].enc->flags & CODEC_FLAG_BITEXACT) {
1457 return 0;
1458 }
1459
1460 ret = url_open_dyn_buf(&pb_buf);
1461 if(ret < 0)
1462 return ret;
1463
1464 if (mov->mode & MODE_3GP) {
1465 mov_write_3gp_udta_tag(pb_buf, s, "titl", "title");
1466 mov_write_3gp_udta_tag(pb_buf, s, "auth", "author");
1467 mov_write_3gp_udta_tag(pb_buf, s, "gnre", "genre");
1468 mov_write_3gp_udta_tag(pb_buf, s, "dscp", "comment");
1469 mov_write_3gp_udta_tag(pb_buf, s, "albm", "album");
1470 mov_write_3gp_udta_tag(pb_buf, s, "cprt", "copyright");
1471 mov_write_3gp_udta_tag(pb_buf, s, "yrrc", "year");
1472 } else if (mov->mode == MODE_MOV) { // the title field breaks gtkpod with mp4 and my suspicion is that stuff is not valid in mp4
1473 mov_write_string_metadata(s, pb_buf, "\251nam", "title" , 0);
1474 mov_write_string_metadata(s, pb_buf, "\251aut", "author" , 0);
1475 mov_write_string_metadata(s, pb_buf, "\251alb", "album" , 0);
1476 mov_write_string_metadata(s, pb_buf, "\251day", "year" , 0);
1477 mov_write_string_tag(pb_buf, "\251enc", LIBAVFORMAT_IDENT, 0, 0);
1478 mov_write_string_metadata(s, pb_buf, "\251des", "comment" , 0);
1479 mov_write_string_metadata(s, pb_buf, "\251gen", "genre" , 0);
1480 mov_write_string_metadata(s, pb_buf, "\251cpy", "copyright" , 0);
1481 } else {
1482 /* iTunes meta data */
1483 mov_write_meta_tag(pb_buf, mov, s);
1484 }
1485
1486 if ((size = url_close_dyn_buf(pb_buf, &buf)) > 0) {
1487 put_be32(pb, size+8);
1488 put_tag(pb, "udta");
1489 put_buffer(pb, buf, size);
1490 av_free(buf);
1491 }
1492
1493 return 0;
1494 }
1495
1496 static void mov_write_psp_udta_tag(ByteIOContext *pb,
1497 const char *str, const char *lang, int type)
1498 {
1499 int len = utf8len(str)+1;
1500 if(len<=0)
1501 return;
1502 put_be16(pb, len*2+10); /* size */
1503 put_be32(pb, type); /* type */
1504 put_be16(pb, language_code(lang)); /* language */
1505 put_be16(pb, 0x01); /* ? */
1506 ascii_to_wc(pb, str);
1507 }
1508
1509 static int mov_write_uuidusmt_tag(ByteIOContext *pb, AVFormatContext *s)
1510 {
1511 AVMetadataTag *title = av_metadata_get(s->metadata, "title", NULL, 0);
1512 int64_t pos, pos2;
1513
1514 if (title) {
1515 pos = url_ftell(pb);
1516 put_be32(pb, 0); /* size placeholder*/
1517 put_tag(pb, "uuid");
1518 put_tag(pb, "USMT");
1519 put_be32(pb, 0x21d24fce); /* 96 bit UUID */
1520 put_be32(pb, 0xbb88695c);
1521 put_be32(pb, 0xfac9c740);
1522
1523 pos2 = url_ftell(pb);
1524 put_be32(pb, 0); /* size placeholder*/
1525 put_tag(pb, "MTDT");
1526 put_be16(pb, 4);
1527
1528 // ?
1529 put_be16(pb, 0x0C); /* size */
1530 put_be32(pb, 0x0B); /* type */
1531 put_be16(pb, language_code("und")); /* language */
1532 put_be16(pb, 0x0); /* ? */
1533 put_be16(pb, 0x021C); /* data */
1534
1535 mov_write_psp_udta_tag(pb, LIBAVCODEC_IDENT, "eng", 0x04);
1536 mov_write_psp_udta_tag(pb, title->value, "eng", 0x01);
1537 // 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);
1538 mov_write_psp_udta_tag(pb, "2006/04/01 11:11:11", "und", 0x03);
1539
1540 updateSize(pb, pos2);
1541 return updateSize(pb, pos);
1542 }
1543
1544 return 0;
1545 }
1546
1547 static int mov_write_moov_tag(ByteIOContext *pb, MOVMuxContext *mov,
1548 AVFormatContext *s)
1549 {
1550 int i;
1551 int64_t pos = url_ftell(pb);
1552 put_be32(pb, 0); /* size placeholder*/
1553 put_tag(pb, "moov");
1554 mov->timescale = globalTimescale;
1555
1556 for (i=0; i<mov->nb_streams; i++) {
1557 if(mov->tracks[i].entry <= 0) continue;
1558
1559 mov->tracks[i].time = mov->time;
1560 mov->tracks[i].trackID = i+1;
1561 }
1562
1563 mov_write_mvhd_tag(pb, mov);
1564 //mov_write_iods_tag(pb, mov);
1565 for (i=0; i<mov->nb_streams; i++) {
1566 if(mov->tracks[i].entry > 0) {
1567 mov_write_trak_tag(pb, &(mov->tracks[i]), s->streams[i]);
1568 }
1569 }
1570
1571 if (mov->mode == MODE_PSP)
1572 mov_write_uuidusmt_tag(pb, s);
1573 else
1574 mov_write_udta_tag(pb, mov, s);
1575
1576 return updateSize(pb, pos);
1577 }
1578
1579 static int mov_write_mdat_tag(ByteIOContext *pb, MOVMuxContext *mov)
1580 {
1581 put_be32(pb, 8); // placeholder for extended size field (64 bit)
1582 put_tag(pb, mov->mode == MODE_MOV ? "wide" : "free");
1583
1584 mov->mdat_pos = url_ftell(pb);
1585 put_be32(pb, 0); /* size placeholder*/
1586 put_tag(pb, "mdat");
1587 return 0;
1588 }
1589
1590 /* TODO: This needs to be more general */
1591 static int mov_write_ftyp_tag(ByteIOContext *pb, AVFormatContext *s)
1592 {
1593 MOVMuxContext *mov = s->priv_data;
1594 int64_t pos = url_ftell(pb);
1595 int has_h264 = 0, has_video = 0;
1596 int minor = 0x200;
1597 int i;
1598
1599 for (i = 0; i < s->nb_streams; i++) {
1600 AVStream *st = s->streams[i];
1601 if (st->codec->codec_type == CODEC_TYPE_VIDEO)
1602 has_video = 1;
1603 if (st->codec->codec_id == CODEC_ID_H264)
1604 has_h264 = 1;
1605 }
1606
1607 put_be32(pb, 0); /* size */
1608 put_tag(pb, "ftyp");
1609
1610 if (mov->mode == MODE_3GP) {
1611 put_tag(pb, has_h264 ? "3gp6" : "3gp4");
1612 minor = has_h264 ? 0x100 : 0x200;
1613 } else if (mov->mode & MODE_3G2) {
1614 put_tag(pb, has_h264 ? "3g2b" : "3g2a");
1615 minor = has_h264 ? 0x20000 : 0x10000;
1616 }else if (mov->mode == MODE_PSP)
1617 put_tag(pb, "MSNV");
1618 else if (mov->mode == MODE_MP4)
1619 put_tag(pb, "isom");
1620 else if (mov->mode == MODE_IPOD)
1621 put_tag(pb, has_video ? "M4V ":"M4A ");
1622 else
1623 put_tag(pb, "qt ");
1624
1625 put_be32(pb, minor);
1626
1627 if(mov->mode == MODE_MOV)
1628 put_tag(pb, "qt ");
1629 else{
1630 put_tag(pb, "isom");
1631 put_tag(pb, "iso2");
1632 if(has_h264)
1633 put_tag(pb, "avc1");
1634 }
1635
1636 if (mov->mode == MODE_3GP)
1637 put_tag(pb, has_h264 ? "3gp6":"3gp4");
1638 else if (mov->mode & MODE_3G2)
1639 put_tag(pb, has_h264 ? "3g2b":"3g2a");
1640 else if (mov->mode == MODE_PSP)
1641 put_tag(pb, "MSNV");
1642 else if (mov->mode == MODE_MP4)
1643 put_tag(pb, "mp41");
1644 return updateSize(pb, pos);
1645 }
1646
1647 static void mov_write_uuidprof_tag(ByteIOContext *pb, AVFormatContext *s)
1648 {
1649 AVCodecContext *VideoCodec = s->streams[0]->codec;
1650 AVCodecContext *AudioCodec = s->streams[1]->codec;
1651 int AudioRate = AudioCodec->sample_rate;
1652 int FrameRate = ((VideoCodec->time_base.den) * (0x10000))/ (VideoCodec->time_base.num);
1653 int audio_kbitrate= AudioCodec->bit_rate / 1000;
1654 int video_kbitrate= FFMIN(VideoCodec->bit_rate / 1000, 800 - audio_kbitrate);
1655
1656 put_be32(pb, 0x94); /* size */
1657 put_tag(pb, "uuid");
1658 put_tag(pb, "PROF");
1659
1660 put_be32(pb, 0x21d24fce); /* 96 bit UUID */
1661 put_be32(pb, 0xbb88695c);
1662 put_be32(pb, 0xfac9c740);
1663
1664 put_be32(pb, 0x0); /* ? */
1665 put_be32(pb, 0x3); /* 3 sections ? */
1666
1667 put_be32(pb, 0x14); /* size */
1668 put_tag(pb, "FPRF");
1669 put_be32(pb, 0x0); /* ? */
1670 put_be32(pb, 0x0); /* ? */
1671 put_be32(pb, 0x0); /* ? */
1672
1673 put_be32(pb, 0x2c); /* size */
1674 put_tag(pb, "APRF"); /* audio */
1675 put_be32(pb, 0x0);
1676 put_be32(pb, 0x2); /* TrackID */
1677 put_tag(pb, "mp4a");
1678 put_be32(pb, 0x20f);
1679 put_be32(pb, 0x0);
1680 put_be32(pb, audio_kbitrate);
1681 put_be32(pb, audio_kbitrate);
1682 put_be32(pb, AudioRate);
1683 put_be32(pb, AudioCodec->channels);
1684
1685 put_be32(pb, 0x34); /* size */
1686 put_tag(pb, "VPRF"); /* video */
1687 put_be32(pb, 0x0);
1688 put_be32(pb, 0x1); /* TrackID */
1689 if (VideoCodec->codec_id == CODEC_ID_H264) {
1690 put_tag(pb, "avc1");
1691 put_be16(pb, 0x014D);
1692 put_be16(pb, 0x0015);
1693 } else {
1694 put_tag(pb, "mp4v");
1695 put_be16(pb, 0x0000);
1696 put_be16(pb, 0x0103);
1697 }
1698 put_be32(pb, 0x0);
1699 put_be32(pb, video_kbitrate);
1700 put_be32(pb, video_kbitrate);
1701 put_be32(pb, FrameRate);
1702 put_be32(pb, FrameRate);
1703 put_be16(pb, VideoCodec->width);
1704 put_be16(pb, VideoCodec->height);
1705 put_be32(pb, 0x010001); /* ? */
1706 }
1707
1708 static int mov_write_header(AVFormatContext *s)
1709 {
1710 ByteIOContext *pb = s->pb;
1711 MOVMuxContext *mov = s->priv_data;
1712 int i;
1713
1714 if (url_is_streamed(s->pb)) {
1715 av_log(s, AV_LOG_ERROR, "muxer does not support non seekable output\n");
1716 return -1;
1717 }
1718
1719 /* Default mode == MP4 */
1720 mov->mode = MODE_MP4;
1721
1722 if (s->oformat != NULL) {
1723 if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
1724 else if (!strcmp("3g2", s->oformat->name)) mov->mode = MODE_3GP|MODE_3G2;
1725 else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
1726 else if (!strcmp("psp", s->oformat->name)) mov->mode = MODE_PSP;
1727 else if (!strcmp("ipod",s->oformat->name)) mov->mode = MODE_IPOD;
1728
1729 mov_write_ftyp_tag(pb,s);
1730 if (mov->mode == MODE_PSP) {
1731 if (s->nb_streams != 2) {
1732 av_log(s, AV_LOG_ERROR, "PSP mode need one video and one audio stream\n");
1733 return -1;
1734 }
1735 mov_write_uuidprof_tag(pb,s);
1736 }
1737 }
1738
1739 mov->tracks = av_mallocz(s->nb_streams*sizeof(*mov->tracks));
1740 if (!mov->tracks)
1741 return AVERROR(ENOMEM);
1742
1743 for(i=0; i<s->nb_streams; i++){
1744 AVStream *st= s->streams[i];
1745 MOVTrack *track= &mov->tracks[i];
1746 AVMetadataTag *lang = av_metadata_get(st->metadata, "language", NULL,0);
1747
1748 track->enc = st->codec;
1749 track->language = ff_mov_iso639_to_lang(lang?lang->value:"und", mov->mode!=MODE_MOV);
1750 if (track->language < 0)
1751 track->language = 0;
1752 track->mode = mov->mode;
1753 track->tag = mov_find_codec_tag(s, track);
1754 if (!track->tag) {
1755 av_log(s, AV_LOG_ERROR, "track %d: could not find tag, "
1756 "codec not currently supported in container\n", i);
1757 goto error;
1758 }
1759 if(st->codec->codec_type == CODEC_TYPE_VIDEO){
1760 if (track->tag == MKTAG('m','x','3','p') || track->tag == MKTAG('m','x','3','n') ||
1761 track->tag == MKTAG('m','x','4','p') || track->tag == MKTAG('m','x','4','n') ||
1762 track->tag == MKTAG('m','x','5','p') || track->tag == MKTAG('m','x','5','n')) {
1763 if (st->codec->width != 720 || (st->codec->height != 608 && st->codec->height != 512)) {
1764 av_log(s, AV_LOG_ERROR, "D-10/IMX must use 720x608 or 720x512 video resolution\n");
1765 goto error;
1766 }
1767 track->height = track->tag>>24 == 'n' ? 486 : 576;
1768 }
1769 track->timescale = st->codec->time_base.den;
1770 if (track->mode == MODE_MOV && track->timescale > 100000)
1771 av_log(s, AV_LOG_WARNING,
1772 "WARNING codec timebase is very high. If duration is too long,\n"
1773 "file may not be playable by quicktime. Specify a shorter timebase\n"
1774 "or choose different container.\n");
1775 }else if(st->codec->codec_type == CODEC_TYPE_AUDIO){
1776 track->timescale = st->codec->sample_rate;
1777 if(!st->codec->frame_size && !av_get_bits_per_sample(st->codec->codec_id)) {
1778 av_log(s, AV_LOG_ERROR, "track %d: codec frame size is not set\n", i);
1779 goto error;
1780 }else if(st->codec->frame_size > 1){ /* assume compressed audio */
1781 track->audio_vbr = 1;
1782 }else{
1783 st->codec->frame_size = 1;
1784 track->sampleSize = (av_get_bits_per_sample(st->codec->codec_id) >> 3) * st->codec->channels;
1785 }
1786 if(track->mode != MODE_MOV &&
1787 track->enc->codec_id == CODEC_ID_MP3 && track->enc->sample_rate < 16000){
1788 av_log(s, AV_LOG_ERROR, "track %d: muxing mp3 at %dhz is not supported\n",
1789 i, track->enc->sample_rate);
1790 goto error;
1791 }
1792 }else if(st->codec->codec_type == CODEC_TYPE_SUBTITLE){
1793 track->timescale = st->codec->time_base.den;
1794 }
1795 if (!track->height)
1796 track->height = st->codec->height;
1797
1798 av_set_pts_info(st, 64, 1, track->timescale);
1799 }
1800
1801 mov_write_mdat_tag(pb, mov);
1802 mov->time = s->timestamp + 0x7C25B080; //1970 based -> 1904 based
1803 mov->nb_streams = s->nb_streams;
1804
1805 put_flush_packet(pb);
1806
1807 return 0;
1808 error:
1809 av_freep(&mov->tracks);
1810 return -1;
1811 }
1812
1813 static int mov_parse_mpeg2_frame(AVPacket *pkt, uint32_t *flags)
1814 {
1815 uint32_t c = -1;
1816 int i, closed_gop = 0;
1817
1818 for (i = 0; i < pkt->size - 4; i++) {
1819 c = (c<<8) + pkt->data[i];
1820 if (c == 0x1b8) { // gop
1821 closed_gop = pkt->data[i+4]>>6 & 0x01;
1822 } else if (c == 0x100) { // pic
1823 int temp_ref = (pkt->data[i+1]<<2) | (pkt->data[i+2]>>6);
1824 if (!temp_ref || closed_gop) // I picture is not reordered
1825 *flags = MOV_SYNC_SAMPLE;
1826 else
1827 *flags = MOV_PARTIAL_SYNC_SAMPLE;
1828 break;
1829 }
1830 }
1831 return 0;
1832 }
1833
1834 static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
1835 {
1836 MOVMuxContext *mov = s->priv_data;
1837 ByteIOContext *pb = s->pb;
1838 MOVTrack *trk = &mov->tracks[pkt->stream_index];
1839 AVCodecContext *enc = trk->enc;
1840 unsigned int samplesInChunk = 0;
1841 int size= pkt->size;
1842
1843 if (url_is_streamed(s->pb)) return 0; /* Can't handle that */
1844 if (!size) return 0; /* Discard 0 sized packets */
1845
1846 if (enc->codec_id == CODEC_ID_AMR_NB) {
1847 /* We must find out how many AMR blocks there are in one packet */
1848 static uint16_t packed_size[16] =
1849 {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 0};
1850 int len = 0;
1851
1852 while (len < size && samplesInChunk < 100) {
1853 len += packed_size[(pkt->data[len] >> 3) & 0x0F];
1854 samplesInChunk++;
1855 }
1856 if(samplesInChunk > 1){
1857 av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, implement a AVParser for it\n");
1858 return -1;
1859 }
1860 } else if (trk->sampleSize)
1861 samplesInChunk = size/trk->sampleSize;
1862 else
1863 samplesInChunk = 1;
1864
1865 /* copy extradata if it exists */
1866 if (trk->vosLen == 0 && enc->extradata_size > 0) {
1867 trk->vosLen = enc->extradata_size;
1868 trk->vosData = av_malloc(trk->vosLen);
1869 memcpy(trk->vosData, enc->extradata, trk->vosLen);
1870 }
1871
1872 if (enc->codec_id == CODEC_ID_H264 && trk->vosLen > 0 && *(uint8_t *)trk->vosData != 1) {
1873 /* from x264 or from bytestream h264 */
1874 /* nal reformating needed */
1875 size = ff_avc_parse_nal_units(pb, pkt->data, pkt->size);
1876 } else {
1877 put_buffer(pb, pkt->data, size);
1878 }
1879
1880 if ((enc->codec_id == CODEC_ID_DNXHD ||
1881 enc->codec_id == CODEC_ID_AC3) && !trk->vosLen) {
1882 /* copy frame to create needed atoms */
1883 trk->vosLen = size;
1884 trk->vosData = av_malloc(size);
1885 if (!trk->vosData)
1886 return AVERROR(ENOMEM);
1887 memcpy(trk->vosData, pkt->data, size);
1888 }
1889
1890 if (!(trk->entry % MOV_INDEX_CLUSTER_SIZE)) {
1891 trk->cluster = av_realloc(trk->cluster, (trk->entry + MOV_INDEX_CLUSTER_SIZE) * sizeof(*trk->cluster));
1892 if (!trk->cluster)
1893 return -1;
1894 }
1895
1896 trk->cluster[trk->entry].pos = url_ftell(pb) - size;
1897 trk->cluster[trk->entry].samplesInChunk = samplesInChunk;
1898 trk->cluster[trk->entry].size = size;
1899 trk->cluster[trk->entry].entries = samplesInChunk;
1900 trk->cluster[trk->entry].dts = pkt->dts;
1901 trk->trackDuration = pkt->dts - trk->cluster[0].dts + pkt->duration;
1902
1903 if (pkt->pts == AV_NOPTS_VALUE) {
1904 av_log(s, AV_LOG_WARNING, "pts has no value\n");
1905 pkt->pts = pkt->dts;
1906 }
1907 if (pkt->dts != pkt->pts)
1908 trk->flags |= MOV_TRACK_CTTS;
1909 trk->cluster[trk->entry].cts = pkt->pts - pkt->dts;
1910 trk->cluster[trk->entry].flags = 0;
1911 if (pkt->flags & PKT_FLAG_KEY) {
1912 if (mov->mode == MODE_MOV && enc->codec_id == CODEC_ID_MPEG2VIDEO) {
1913 mov_parse_mpeg2_frame(pkt, &trk->cluster[trk->entry].flags);
1914 if (trk->cluster[trk->entry].flags & MOV_PARTIAL_SYNC_SAMPLE)
1915 trk->flags |= MOV_TRACK_STPS;
1916 } else {
1917 trk->cluster[trk->entry].flags = MOV_SYNC_SAMPLE;
1918 }
1919 if (trk->cluster[trk->entry].flags & MOV_SYNC_SAMPLE)
1920 trk->hasKeyframes++;
1921 }
1922 trk->entry++;
1923 trk->sampleCount += samplesInChunk;
1924 mov->mdat_size += size;
1925
1926 put_flush_packet(pb);
1927 return 0;
1928 }
1929
1930 static int mov_write_trailer(AVFormatContext *s)
1931 {
1932 MOVMuxContext *mov = s->priv_data;
1933 ByteIOContext *pb = s->pb;
1934 int res = 0;
1935 int i;
1936
1937 int64_t moov_pos = url_ftell(pb);
1938
1939 /* Write size of mdat tag */
1940 if (mov->mdat_size+8 <= UINT32_MAX) {
1941 url_fseek(pb, mov->mdat_pos, SEEK_SET);
1942 put_be32(pb, mov->mdat_size+8);
1943 } else {
1944 /* overwrite 'wide' placeholder atom */
1945 url_fseek(pb, mov->mdat_pos - 8, SEEK_SET);
1946 put_be32(pb, 1); /* special value: real atom size will be 64 bit value after tag field */
1947 put_tag(pb, "mdat");
1948 put_be64(pb, mov->mdat_size+16);
1949 }
1950 url_fseek(pb, moov_pos, SEEK_SET);
1951
1952 mov_write_moov_tag(pb, mov, s);
1953
1954 for (i=0; i<mov->nb_streams; i++) {
1955 av_freep(&mov->tracks[i].cluster);
1956
1957 if(mov->tracks[i].vosLen) av_free(mov->tracks[i].vosData);
1958
1959 }
1960
1961 put_flush_packet(pb);
1962
1963 av_freep(&mov->tracks);
1964
1965 return res;
1966 }
1967
1968 #if CONFIG_MOV_MUXER
1969 AVOutputFormat mov_muxer = {
1970 "mov",
1971 NULL_IF_CONFIG_SMALL("MOV format"),
1972 NULL,
1973 "mov",
1974 sizeof(MOVMuxContext),
1975 CODEC_ID_AAC,
1976 CODEC_ID_MPEG4,
1977 mov_write_header,
1978 mov_write_packet,
1979 mov_write_trailer,
1980 .flags = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS,
1981 .codec_tag = (const AVCodecTag* const []){codec_movvideo_tags, codec_movaudio_tags, 0},
1982 };
1983 #endif
1984 #if CONFIG_TGP_MUXER
1985 AVOutputFormat tgp_muxer = {
1986 "3gp",
1987 NULL_IF_CONFIG_SMALL("3GP format"),
1988 NULL,
1989 "3gp",
1990 sizeof(MOVMuxContext),
1991 CODEC_ID_AMR_NB,
1992 CODEC_ID_H263,
1993 mov_write_header,
1994 mov_write_packet,
1995 mov_write_trailer,
1996 .flags = AVFMT_GLOBALHEADER,
1997 .codec_tag = (const AVCodecTag* const []){codec_3gp_tags, 0},
1998 };
1999 #endif
2000 #if CONFIG_MP4_MUXER
2001 AVOutputFormat mp4_muxer = {
2002 "mp4",
2003 NULL_IF_CONFIG_SMALL("MP4 format"),
2004 "application/mp4",
2005 "mp4",
2006 sizeof(MOVMuxContext),
2007 CODEC_ID_AAC,
2008 CODEC_ID_MPEG4,
2009 mov_write_header,
2010 mov_write_packet,
2011 mov_write_trailer,
2012 .flags = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS,
2013 .codec_tag = (const AVCodecTag* const []){ff_mp4_obj_type, 0},
2014 };
2015 #endif
2016 #if CONFIG_PSP_MUXER
2017 AVOutputFormat psp_muxer = {
2018 "psp",
2019 NULL_IF_CONFIG_SMALL("PSP MP4 format"),
2020 NULL,
2021 "mp4,psp",
2022 sizeof(MOVMuxContext),
2023 CODEC_ID_AAC,
2024 CODEC_ID_MPEG4,
2025 mov_write_header,
2026 mov_write_packet,
2027 mov_write_trailer,
2028 .flags = AVFMT_GLOBALHEADER,
2029 .codec_tag = (const AVCodecTag* const []){ff_mp4_obj_type, 0},
2030 };
2031 #endif
2032 #if CONFIG_TG2_MUXER
2033 AVOutputFormat tg2_muxer = {
2034 "3g2",
2035 NULL_IF_CONFIG_SMALL("3GP2 format"),
2036 NULL,
2037 "3g2",
2038 sizeof(MOVMuxContext),
2039 CODEC_ID_AMR_NB,
2040 CODEC_ID_H263,
2041 mov_write_header,
2042 mov_write_packet,
2043 mov_write_trailer,
2044 .flags = AVFMT_GLOBALHEADER,
2045 .codec_tag = (const AVCodecTag* const []){codec_3gp_tags, 0},
2046 };
2047 #endif
2048 #if CONFIG_IPOD_MUXER
2049 AVOutputFormat ipod_muxer = {
2050 "ipod",
2051 NULL_IF_CONFIG_SMALL("iPod H.264 MP4 format"),
2052 "application/mp4",
2053 "m4v,m4a",
2054 sizeof(MOVMuxContext),
2055 CODEC_ID_AAC,
2056 CODEC_ID_H264,
2057 mov_write_header,
2058 mov_write_packet,
2059 mov_write_trailer,
2060 .flags = AVFMT_GLOBALHEADER,
2061 .codec_tag = (const AVCodecTag* const []){codec_ipod_tags, 0},
2062 };
2063 #endif