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