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