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