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