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>
7
* This file is part of Libav.
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.
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.
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
26
#include "avio_internal.h"
31
#include "libavcodec/get_bits.h"
32
#include "libavcodec/put_bits.h"
34
#include "libavutil/avstring.h"
35
#include "libavutil/opt.h"
36
#include "libavutil/dict.h"
42
static const AVOption options[] = {
43
{ "movflags", "MOV muxer flags", offsetof(MOVMuxContext, flags), FF_OPT_TYPE_FLAGS, {.dbl = 0}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
44
{ "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" },
45
FF_RTP_FLAG_OPTS(MOVMuxContext, rtp_flags),
49
static const AVClass mov_muxer_class = {
50
.class_name = "MOV/3GP/MP4/3G2 muxer",
51
.item_name = av_default_item_name,
53
.version = LIBAVUTIL_VERSION_INT,
56
//FIXME support 64 bit variant with wide placeholders
57
static int64_t updateSize(AVIOContext *pb, int64_t pos)
59
int64_t curpos = avio_tell(pb);
60
avio_seek(pb, pos, SEEK_SET);
61
avio_wb32(pb, curpos - pos); /* rewrite size */
62
avio_seek(pb, curpos, SEEK_SET);
67
/* Chunk offset atom */
68
static int mov_write_stco_tag(AVIOContext *pb, MOVTrack *track)
71
int mode64 = 0; // use 32 bit size variant if possible
72
int64_t pos = avio_tell(pb);
73
avio_wb32(pb, 0); /* size */
74
if (pos > UINT32_MAX) {
76
ffio_wfourcc(pb, "co64");
78
ffio_wfourcc(pb, "stco");
79
avio_wb32(pb, 0); /* version & flags */
80
avio_wb32(pb, track->entry); /* entry count */
81
for (i=0; i<track->entry; i++) {
83
avio_wb64(pb, track->cluster[i].pos);
85
avio_wb32(pb, track->cluster[i].pos);
87
return updateSize(pb, pos);
90
/* Sample size atom */
91
static int mov_write_stsz_tag(AVIOContext *pb, MOVTrack *track)
94
int i, j, entries = 0, tst = -1, oldtst = -1;
96
int64_t pos = avio_tell(pb);
97
avio_wb32(pb, 0); /* size */
98
ffio_wfourcc(pb, "stsz");
99
avio_wb32(pb, 0); /* version & flags */
101
for (i=0; i<track->entry; i++) {
102
tst = track->cluster[i].size/track->cluster[i].entries;
103
if(oldtst != -1 && tst != oldtst) {
107
entries += track->cluster[i].entries;
110
int sSize = track->cluster[0].size/track->cluster[0].entries;
111
sSize = FFMAX(1, sSize); // adpcm mono case could make sSize == 0
112
avio_wb32(pb, sSize); // sample size
113
avio_wb32(pb, entries); // sample count
116
avio_wb32(pb, 0); // sample size
117
avio_wb32(pb, entries); // sample count
118
for (i=0; i<track->entry; i++) {
119
for (j=0; j<track->cluster[i].entries; j++) {
120
avio_wb32(pb, track->cluster[i].size /
121
track->cluster[i].entries);
125
return updateSize(pb, pos);
128
/* Sample to chunk atom */
129
static int mov_write_stsc_tag(AVIOContext *pb, MOVTrack *track)
131
int index = 0, oldval = -1, i;
132
int64_t entryPos, curpos;
134
int64_t pos = avio_tell(pb);
135
avio_wb32(pb, 0); /* size */
136
ffio_wfourcc(pb, "stsc");
137
avio_wb32(pb, 0); // version & flags
138
entryPos = avio_tell(pb);
139
avio_wb32(pb, track->entry); // entry count
140
for (i=0; i<track->entry; i++) {
141
if(oldval != track->cluster[i].samplesInChunk)
143
avio_wb32(pb, i+1); // first chunk
144
avio_wb32(pb, track->cluster[i].samplesInChunk); // samples per chunk
145
avio_wb32(pb, 0x1); // sample description index
146
oldval = track->cluster[i].samplesInChunk;
150
curpos = avio_tell(pb);
151
avio_seek(pb, entryPos, SEEK_SET);
152
avio_wb32(pb, index); // rewrite size
153
avio_seek(pb, curpos, SEEK_SET);
155
return updateSize(pb, pos);
158
/* Sync sample atom */
159
static int mov_write_stss_tag(AVIOContext *pb, MOVTrack *track, uint32_t flag)
161
int64_t curpos, entryPos;
163
int64_t pos = avio_tell(pb);
164
avio_wb32(pb, 0); // size
165
ffio_wfourcc(pb, flag == MOV_SYNC_SAMPLE ? "stss" : "stps");
166
avio_wb32(pb, 0); // version & flags
167
entryPos = avio_tell(pb);
168
avio_wb32(pb, track->entry); // entry count
169
for (i=0; i<track->entry; i++) {
170
if (track->cluster[i].flags & flag) {
175
curpos = avio_tell(pb);
176
avio_seek(pb, entryPos, SEEK_SET);
177
avio_wb32(pb, index); // rewrite size
178
avio_seek(pb, curpos, SEEK_SET);
179
return updateSize(pb, pos);
182
static int mov_write_amr_tag(AVIOContext *pb, MOVTrack *track)
184
avio_wb32(pb, 0x11); /* size */
185
if (track->mode == MODE_MOV) ffio_wfourcc(pb, "samr");
186
else ffio_wfourcc(pb, "damr");
187
ffio_wfourcc(pb, "FFMP");
188
avio_w8(pb, 0); /* decoder version */
190
avio_wb16(pb, 0x81FF); /* Mode set (all modes for AMR_NB) */
191
avio_w8(pb, 0x00); /* Mode change period (no restriction) */
192
avio_w8(pb, 0x01); /* Frames per sample */
196
static int mov_write_ac3_tag(AVIOContext *pb, MOVTrack *track)
201
int fscod, bsid, bsmod, acmod, lfeon, frmsizecod;
203
if (track->vosLen < 7)
207
ffio_wfourcc(pb, "dac3");
209
init_get_bits(&gbc, track->vosData+4, track->vosLen-4);
210
fscod = get_bits(&gbc, 2);
211
frmsizecod = get_bits(&gbc, 6);
212
bsid = get_bits(&gbc, 5);
213
bsmod = get_bits(&gbc, 3);
214
acmod = get_bits(&gbc, 3);
216
skip_bits(&gbc, 2); // dsurmod
218
if ((acmod & 1) && acmod != 1)
219
skip_bits(&gbc, 2); // cmixlev
221
skip_bits(&gbc, 2); // surmixlev
223
lfeon = get_bits1(&gbc);
225
init_put_bits(&pbc, buf, sizeof(buf));
226
put_bits(&pbc, 2, fscod);
227
put_bits(&pbc, 5, bsid);
228
put_bits(&pbc, 3, bsmod);
229
put_bits(&pbc, 3, acmod);
230
put_bits(&pbc, 1, lfeon);
231
put_bits(&pbc, 5, frmsizecod>>1); // bit_rate_code
232
put_bits(&pbc, 5, 0); // reserved
234
flush_put_bits(&pbc);
235
avio_write(pb, buf, sizeof(buf));
241
* This function writes extradata "as is".
242
* Extradata must be formated like a valid atom (with size and tag)
244
static int mov_write_extradata_tag(AVIOContext *pb, MOVTrack *track)
246
avio_write(pb, track->enc->extradata, track->enc->extradata_size);
247
return track->enc->extradata_size;
250
static int mov_write_enda_tag(AVIOContext *pb)
253
ffio_wfourcc(pb, "enda");
254
avio_wb16(pb, 1); /* little endian */
258
static void putDescr(AVIOContext *pb, int tag, unsigned int size)
263
avio_w8(pb, (size>>(7*i)) | 0x80);
264
avio_w8(pb, size & 0x7F);
267
static int mov_write_esds_tag(AVIOContext *pb, MOVTrack *track) // Basic
269
int64_t pos = avio_tell(pb);
270
int decoderSpecificInfoLen = track->vosLen ? 5+track->vosLen : 0;
272
avio_wb32(pb, 0); // size
273
ffio_wfourcc(pb, "esds");
274
avio_wb32(pb, 0); // Version
277
putDescr(pb, 0x03, 3 + 5+13 + decoderSpecificInfoLen + 5+1);
278
avio_wb16(pb, track->trackID);
279
avio_w8(pb, 0x00); // flags (= no flags)
281
// DecoderConfig descriptor
282
putDescr(pb, 0x04, 13 + decoderSpecificInfoLen);
284
// Object type indication
285
if ((track->enc->codec_id == CODEC_ID_MP2 ||
286
track->enc->codec_id == CODEC_ID_MP3) &&
287
track->enc->sample_rate > 24000)
288
avio_w8(pb, 0x6B); // 11172-3
290
avio_w8(pb, ff_codec_get_tag(ff_mp4_obj_type, track->enc->codec_id));
292
// the following fields is made of 6 bits to identify the streamtype (4 for video, 5 for audio)
293
// plus 1 bit to indicate upstream and 1 bit set to 1 (reserved)
294
if(track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
295
avio_w8(pb, 0x15); // flags (= Audiostream)
297
avio_w8(pb, 0x11); // flags (= Visualstream)
299
avio_w8(pb, track->enc->rc_buffer_size>>(3+16)); // Buffersize DB (24 bits)
300
avio_wb16(pb, (track->enc->rc_buffer_size>>3)&0xFFFF); // Buffersize DB
302
avio_wb32(pb, FFMAX(track->enc->bit_rate, track->enc->rc_max_rate)); // maxbitrate (FIXME should be max rate in any 1 sec window)
303
if(track->enc->rc_max_rate != track->enc->rc_min_rate || track->enc->rc_min_rate==0)
304
avio_wb32(pb, 0); // vbr
306
avio_wb32(pb, track->enc->rc_max_rate); // avg bitrate
309
// DecoderSpecific info descriptor
310
putDescr(pb, 0x05, track->vosLen);
311
avio_write(pb, track->vosData, track->vosLen);
315
putDescr(pb, 0x06, 1);
317
return updateSize(pb, pos);
320
static int mov_pcm_le_gt16(enum CodecID codec_id)
322
return codec_id == CODEC_ID_PCM_S24LE ||
323
codec_id == CODEC_ID_PCM_S32LE ||
324
codec_id == CODEC_ID_PCM_F32LE ||
325
codec_id == CODEC_ID_PCM_F64LE;
328
static int mov_write_ms_tag(AVIOContext *pb, MOVTrack *track)
330
int64_t pos = avio_tell(pb);
332
avio_wl32(pb, track->tag); // store it byteswapped
333
track->enc->codec_tag = av_bswap16(track->tag >> 16);
334
ff_put_wav_header(pb, track->enc);
335
return updateSize(pb, pos);
338
static int mov_write_wave_tag(AVIOContext *pb, MOVTrack *track)
340
int64_t pos = avio_tell(pb);
342
avio_wb32(pb, 0); /* size */
343
ffio_wfourcc(pb, "wave");
345
avio_wb32(pb, 12); /* size */
346
ffio_wfourcc(pb, "frma");
347
avio_wl32(pb, track->tag);
349
if (track->enc->codec_id == CODEC_ID_AAC) {
350
/* useless atom needed by mplayer, ipod, not needed by quicktime */
351
avio_wb32(pb, 12); /* size */
352
ffio_wfourcc(pb, "mp4a");
354
mov_write_esds_tag(pb, track);
355
} else if (mov_pcm_le_gt16(track->enc->codec_id)) {
356
mov_write_enda_tag(pb);
357
} else if (track->enc->codec_id == CODEC_ID_AMR_NB) {
358
mov_write_amr_tag(pb, track);
359
} else if (track->enc->codec_id == CODEC_ID_AC3) {
360
mov_write_ac3_tag(pb, track);
361
} else if (track->enc->codec_id == CODEC_ID_ALAC) {
362
mov_write_extradata_tag(pb, track);
363
} else if (track->enc->codec_id == CODEC_ID_ADPCM_MS ||
364
track->enc->codec_id == CODEC_ID_ADPCM_IMA_WAV) {
365
mov_write_ms_tag(pb, track);
368
avio_wb32(pb, 8); /* size */
369
avio_wb32(pb, 0); /* null tag */
371
return updateSize(pb, pos);
374
static int mov_write_glbl_tag(AVIOContext *pb, MOVTrack *track)
376
avio_wb32(pb, track->vosLen+8);
377
ffio_wfourcc(pb, "glbl");
378
avio_write(pb, track->vosData, track->vosLen);
379
return 8+track->vosLen;
383
* Compute flags for 'lpcm' tag.
384
* See CoreAudioTypes and AudioStreamBasicDescription at Apple.
386
static int mov_get_lpcm_flags(enum CodecID codec_id)
389
case CODEC_ID_PCM_F32BE:
390
case CODEC_ID_PCM_F64BE:
392
case CODEC_ID_PCM_F32LE:
393
case CODEC_ID_PCM_F64LE:
395
case CODEC_ID_PCM_U8:
397
case CODEC_ID_PCM_S16BE:
398
case CODEC_ID_PCM_S24BE:
399
case CODEC_ID_PCM_S32BE:
401
case CODEC_ID_PCM_S8:
402
case CODEC_ID_PCM_S16LE:
403
case CODEC_ID_PCM_S24LE:
404
case CODEC_ID_PCM_S32LE:
411
static int mov_write_audio_tag(AVIOContext *pb, MOVTrack *track)
413
int64_t pos = avio_tell(pb);
415
uint32_t tag = track->tag;
417
if (track->mode == MODE_MOV) {
418
if (track->timescale > UINT16_MAX) {
419
if (mov_get_lpcm_flags(track->enc->codec_id))
420
tag = AV_RL32("lpcm");
422
} else if (track->audio_vbr || mov_pcm_le_gt16(track->enc->codec_id) ||
423
track->enc->codec_id == CODEC_ID_ADPCM_MS ||
424
track->enc->codec_id == CODEC_ID_ADPCM_IMA_WAV) {
429
avio_wb32(pb, 0); /* size */
430
avio_wl32(pb, tag); // store it byteswapped
431
avio_wb32(pb, 0); /* Reserved */
432
avio_wb16(pb, 0); /* Reserved */
433
avio_wb16(pb, 1); /* Data-reference index, XXX == 1 */
435
/* SoundDescription */
436
avio_wb16(pb, version); /* Version */
437
avio_wb16(pb, 0); /* Revision level */
438
avio_wb32(pb, 0); /* Reserved */
443
avio_wb16(pb, 0xfffe);
445
avio_wb32(pb, 0x00010000);
447
avio_wb64(pb, av_dbl2int(track->timescale));
448
avio_wb32(pb, track->enc->channels);
449
avio_wb32(pb, 0x7F000000);
450
avio_wb32(pb, av_get_bits_per_sample(track->enc->codec_id));
451
avio_wb32(pb, mov_get_lpcm_flags(track->enc->codec_id));
452
avio_wb32(pb, track->sampleSize);
453
avio_wb32(pb, track->enc->frame_size);
455
if (track->mode == MODE_MOV) {
456
avio_wb16(pb, track->enc->channels);
457
if (track->enc->codec_id == CODEC_ID_PCM_U8 ||
458
track->enc->codec_id == CODEC_ID_PCM_S8)
459
avio_wb16(pb, 8); /* bits per sample */
462
avio_wb16(pb, track->audio_vbr ? -2 : 0); /* compression ID */
463
} else { /* reserved for mp4/3gp */
469
avio_wb16(pb, 0); /* packet size (= 0) */
470
avio_wb16(pb, track->timescale); /* Time scale */
471
avio_wb16(pb, 0); /* Reserved */
474
if(version == 1) { /* SoundDescription V1 extended info */
475
avio_wb32(pb, track->enc->frame_size); /* Samples per packet */
476
avio_wb32(pb, track->sampleSize / track->enc->channels); /* Bytes per packet */
477
avio_wb32(pb, track->sampleSize); /* Bytes per frame */
478
avio_wb32(pb, 2); /* Bytes per sample */
481
if(track->mode == MODE_MOV &&
482
(track->enc->codec_id == CODEC_ID_AAC ||
483
track->enc->codec_id == CODEC_ID_AC3 ||
484
track->enc->codec_id == CODEC_ID_AMR_NB ||
485
track->enc->codec_id == CODEC_ID_ALAC ||
486
track->enc->codec_id == CODEC_ID_ADPCM_MS ||
487
track->enc->codec_id == CODEC_ID_ADPCM_IMA_WAV ||
488
mov_pcm_le_gt16(track->enc->codec_id)))
489
mov_write_wave_tag(pb, track);
490
else if(track->tag == MKTAG('m','p','4','a'))
491
mov_write_esds_tag(pb, track);
492
else if(track->enc->codec_id == CODEC_ID_AMR_NB)
493
mov_write_amr_tag(pb, track);
494
else if(track->enc->codec_id == CODEC_ID_AC3)
495
mov_write_ac3_tag(pb, track);
496
else if(track->enc->codec_id == CODEC_ID_ALAC)
497
mov_write_extradata_tag(pb, track);
498
else if(track->vosLen > 0)
499
mov_write_glbl_tag(pb, track);
501
return updateSize(pb, pos);
504
static int mov_write_d263_tag(AVIOContext *pb)
506
avio_wb32(pb, 0xf); /* size */
507
ffio_wfourcc(pb, "d263");
508
ffio_wfourcc(pb, "FFMP");
509
avio_w8(pb, 0); /* decoder version */
510
/* FIXME use AVCodecContext level/profile, when encoder will set values */
511
avio_w8(pb, 0xa); /* level */
512
avio_w8(pb, 0); /* profile */
516
/* TODO: No idea about these values */
517
static int mov_write_svq3_tag(AVIOContext *pb)
520
ffio_wfourcc(pb, "SMI ");
521
ffio_wfourcc(pb, "SEQH");
523
avio_wb32(pb, 0xe2c0211d);
524
avio_wb32(pb, 0xc0000000);
529
static int mov_write_avcc_tag(AVIOContext *pb, MOVTrack *track)
531
int64_t pos = avio_tell(pb);
534
ffio_wfourcc(pb, "avcC");
535
ff_isom_write_avcc(pb, track->vosData, track->vosLen);
536
return updateSize(pb, pos);
539
/* also used by all avid codecs (dv, imx, meridien) and their variants */
540
static int mov_write_avid_tag(AVIOContext *pb, MOVTrack *track)
543
avio_wb32(pb, 24); /* size */
544
ffio_wfourcc(pb, "ACLR");
545
ffio_wfourcc(pb, "ACLR");
546
ffio_wfourcc(pb, "0001");
547
avio_wb32(pb, 2); /* yuv range: full 1 / normal 2 */
548
avio_wb32(pb, 0); /* unknown */
550
avio_wb32(pb, 24); /* size */
551
ffio_wfourcc(pb, "APRG");
552
ffio_wfourcc(pb, "APRG");
553
ffio_wfourcc(pb, "0001");
554
avio_wb32(pb, 1); /* unknown */
555
avio_wb32(pb, 0); /* unknown */
557
avio_wb32(pb, 120); /* size */
558
ffio_wfourcc(pb, "ARES");
559
ffio_wfourcc(pb, "ARES");
560
ffio_wfourcc(pb, "0001");
561
avio_wb32(pb, AV_RB32(track->vosData + 0x28)); /* dnxhd cid, some id ? */
562
avio_wb32(pb, track->enc->width);
563
/* values below are based on samples created with quicktime and avid codecs */
564
if (track->vosData[5] & 2) { // interlaced
565
avio_wb32(pb, track->enc->height/2);
566
avio_wb32(pb, 2); /* unknown */
567
avio_wb32(pb, 0); /* unknown */
568
avio_wb32(pb, 4); /* unknown */
570
avio_wb32(pb, track->enc->height);
571
avio_wb32(pb, 1); /* unknown */
572
avio_wb32(pb, 0); /* unknown */
573
if (track->enc->height == 1080)
574
avio_wb32(pb, 5); /* unknown */
576
avio_wb32(pb, 6); /* unknown */
579
for (i = 0; i < 10; i++)
582
/* extra padding for stsd needed */
587
static int mp4_get_codec_tag(AVFormatContext *s, MOVTrack *track)
589
int tag = track->enc->codec_tag;
591
if (!ff_codec_get_tag(ff_mp4_obj_type, track->enc->codec_id))
594
if (track->enc->codec_id == CODEC_ID_H264) tag = MKTAG('a','v','c','1');
595
else if (track->enc->codec_id == CODEC_ID_AC3) tag = MKTAG('a','c','-','3');
596
else if (track->enc->codec_id == CODEC_ID_DIRAC) tag = MKTAG('d','r','a','c');
597
else if (track->enc->codec_id == CODEC_ID_MOV_TEXT) tag = MKTAG('t','x','3','g');
598
else if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) tag = MKTAG('m','p','4','v');
599
else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) tag = MKTAG('m','p','4','a');
604
static const AVCodecTag codec_ipod_tags[] = {
605
{ CODEC_ID_H264, MKTAG('a','v','c','1') },
606
{ CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
607
{ CODEC_ID_AAC, MKTAG('m','p','4','a') },
608
{ CODEC_ID_ALAC, MKTAG('a','l','a','c') },
609
{ CODEC_ID_AC3, MKTAG('a','c','-','3') },
610
{ CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
611
{ CODEC_ID_MOV_TEXT, MKTAG('t','e','x','t') },
612
{ CODEC_ID_NONE, 0 },
615
static int ipod_get_codec_tag(AVFormatContext *s, MOVTrack *track)
617
int tag = track->enc->codec_tag;
619
// keep original tag for subs, ipod supports both formats
620
if (!(track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE &&
621
(tag == MKTAG('t','x','3','g') ||
622
tag == MKTAG('t','e','x','t'))))
623
tag = ff_codec_get_tag(codec_ipod_tags, track->enc->codec_id);
625
if (!av_match_ext(s->filename, "m4a") && !av_match_ext(s->filename, "m4v"))
626
av_log(s, AV_LOG_WARNING, "Warning, extension is not .m4a nor .m4v "
627
"Quicktime/Ipod might not play the file\n");
632
static int mov_get_dv_codec_tag(AVFormatContext *s, MOVTrack *track)
636
if (track->enc->width == 720) /* SD */
637
if (track->enc->height == 480) /* NTSC */
638
if (track->enc->pix_fmt == PIX_FMT_YUV422P) tag = MKTAG('d','v','5','n');
639
else tag = MKTAG('d','v','c',' ');
640
else if (track->enc->pix_fmt == PIX_FMT_YUV422P) tag = MKTAG('d','v','5','p');
641
else if (track->enc->pix_fmt == PIX_FMT_YUV420P) tag = MKTAG('d','v','c','p');
642
else tag = MKTAG('d','v','p','p');
643
else if (track->enc->height == 720) /* HD 720 line */
644
if (track->enc->time_base.den == 50) tag = MKTAG('d','v','h','q');
645
else tag = MKTAG('d','v','h','p');
646
else if (track->enc->height == 1080) /* HD 1080 line */
647
if (track->enc->time_base.den == 25) tag = MKTAG('d','v','h','5');
648
else tag = MKTAG('d','v','h','6');
650
av_log(s, AV_LOG_ERROR, "unsupported height for dv codec\n");
657
static const struct {
658
enum PixelFormat pix_fmt;
661
} mov_pix_fmt_tags[] = {
662
{ PIX_FMT_YUYV422, MKTAG('y','u','v','s'), 0 },
663
{ PIX_FMT_UYVY422, MKTAG('2','v','u','y'), 0 },
664
{ PIX_FMT_RGB555BE,MKTAG('r','a','w',' '), 16 },
665
{ PIX_FMT_RGB555LE,MKTAG('L','5','5','5'), 16 },
666
{ PIX_FMT_RGB565LE,MKTAG('L','5','6','5'), 16 },
667
{ PIX_FMT_RGB565BE,MKTAG('B','5','6','5'), 16 },
668
{ PIX_FMT_GRAY16BE,MKTAG('b','1','6','g'), 16 },
669
{ PIX_FMT_RGB24, MKTAG('r','a','w',' '), 24 },
670
{ PIX_FMT_BGR24, MKTAG('2','4','B','G'), 24 },
671
{ PIX_FMT_ARGB, MKTAG('r','a','w',' '), 32 },
672
{ PIX_FMT_BGRA, MKTAG('B','G','R','A'), 32 },
673
{ PIX_FMT_RGBA, MKTAG('R','G','B','A'), 32 },
674
{ PIX_FMT_ABGR, MKTAG('A','B','G','R'), 32 },
675
{ PIX_FMT_RGB48BE, MKTAG('b','4','8','r'), 48 },
678
static int mov_get_rawvideo_codec_tag(AVFormatContext *s, MOVTrack *track)
680
int tag = track->enc->codec_tag;
683
for (i = 0; i < FF_ARRAY_ELEMS(mov_pix_fmt_tags); i++) {
684
if (track->enc->pix_fmt == mov_pix_fmt_tags[i].pix_fmt) {
685
tag = mov_pix_fmt_tags[i].tag;
686
track->enc->bits_per_coded_sample = mov_pix_fmt_tags[i].bps;
694
static int mov_get_codec_tag(AVFormatContext *s, MOVTrack *track)
696
int tag = track->enc->codec_tag;
698
if (!tag || (track->enc->strict_std_compliance >= FF_COMPLIANCE_NORMAL &&
699
(track->enc->codec_id == CODEC_ID_DVVIDEO ||
700
track->enc->codec_id == CODEC_ID_RAWVIDEO ||
701
track->enc->codec_id == CODEC_ID_H263 ||
702
av_get_bits_per_sample(track->enc->codec_id)))) { // pcm audio
703
if (track->enc->codec_id == CODEC_ID_DVVIDEO)
704
tag = mov_get_dv_codec_tag(s, track);
705
else if (track->enc->codec_id == CODEC_ID_RAWVIDEO)
706
tag = mov_get_rawvideo_codec_tag(s, track);
707
else if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
708
tag = ff_codec_get_tag(codec_movvideo_tags, track->enc->codec_id);
709
if (!tag) { // if no mac fcc found, try with Microsoft tags
710
tag = ff_codec_get_tag(ff_codec_bmp_tags, track->enc->codec_id);
712
av_log(s, AV_LOG_INFO, "Warning, using MS style video codec tag, "
713
"the file may be unplayable!\n");
715
} else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
716
tag = ff_codec_get_tag(codec_movaudio_tags, track->enc->codec_id);
717
if (!tag) { // if no mac fcc found, try with Microsoft tags
718
int ms_tag = ff_codec_get_tag(ff_codec_wav_tags, track->enc->codec_id);
720
tag = MKTAG('m', 's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
721
av_log(s, AV_LOG_INFO, "Warning, using MS style audio codec tag, "
722
"the file may be unplayable!\n");
725
} else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)
726
tag = ff_codec_get_tag(ff_codec_movsubtitle_tags, track->enc->codec_id);
732
static const AVCodecTag codec_3gp_tags[] = {
733
{ CODEC_ID_H263, MKTAG('s','2','6','3') },
734
{ CODEC_ID_H264, MKTAG('a','v','c','1') },
735
{ CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
736
{ CODEC_ID_AAC, MKTAG('m','p','4','a') },
737
{ CODEC_ID_AMR_NB, MKTAG('s','a','m','r') },
738
{ CODEC_ID_AMR_WB, MKTAG('s','a','w','b') },
739
{ CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
740
{ CODEC_ID_NONE, 0 },
743
static int mov_find_codec_tag(AVFormatContext *s, MOVTrack *track)
745
int tag = track->enc->codec_tag;
747
if (track->mode == MODE_MP4 || track->mode == MODE_PSP)
748
tag = mp4_get_codec_tag(s, track);
749
else if (track->mode == MODE_IPOD)
750
tag = ipod_get_codec_tag(s, track);
751
else if (track->mode & MODE_3GP)
752
tag = ff_codec_get_tag(codec_3gp_tags, track->enc->codec_id);
754
tag = mov_get_codec_tag(s, track);
760
* Needed to make file play in iPods running newest firmware
761
* goes after avcC atom in moov.trak.mdia.minf.stbl.stsd.avc1
763
static int mov_write_uuid_tag_ipod(AVIOContext *pb)
766
ffio_wfourcc(pb, "uuid");
767
avio_wb32(pb, 0x6b6840f2);
768
avio_wb32(pb, 0x5f244fc5);
769
avio_wb32(pb, 0xba39a51b);
770
avio_wb32(pb, 0xcf0323f3);
775
static int mov_write_subtitle_tag(AVIOContext *pb, MOVTrack *track)
777
int64_t pos = avio_tell(pb);
778
avio_wb32(pb, 0); /* size */
779
avio_wl32(pb, track->tag); // store it byteswapped
780
avio_wb32(pb, 0); /* Reserved */
781
avio_wb16(pb, 0); /* Reserved */
782
avio_wb16(pb, 1); /* Data-reference index */
784
if (track->enc->extradata_size)
785
avio_write(pb, track->enc->extradata, track->enc->extradata_size);
787
return updateSize(pb, pos);
790
static int mov_write_pasp_tag(AVIOContext *pb, MOVTrack *track)
793
av_reduce(&sar.num, &sar.den, track->enc->sample_aspect_ratio.num,
794
track->enc->sample_aspect_ratio.den, INT_MAX);
797
ffio_wfourcc(pb, "pasp");
798
avio_wb32(pb, sar.num);
799
avio_wb32(pb, sar.den);
803
static int mov_write_video_tag(AVIOContext *pb, MOVTrack *track)
805
int64_t pos = avio_tell(pb);
806
char compressor_name[32];
808
avio_wb32(pb, 0); /* size */
809
avio_wl32(pb, track->tag); // store it byteswapped
810
avio_wb32(pb, 0); /* Reserved */
811
avio_wb16(pb, 0); /* Reserved */
812
avio_wb16(pb, 1); /* Data-reference index */
814
avio_wb16(pb, 0); /* Codec stream version */
815
avio_wb16(pb, 0); /* Codec stream revision (=0) */
816
if (track->mode == MODE_MOV) {
817
ffio_wfourcc(pb, "FFMP"); /* Vendor */
818
if(track->enc->codec_id == CODEC_ID_RAWVIDEO) {
819
avio_wb32(pb, 0); /* Temporal Quality */
820
avio_wb32(pb, 0x400); /* Spatial Quality = lossless*/
822
avio_wb32(pb, 0x200); /* Temporal Quality = normal */
823
avio_wb32(pb, 0x200); /* Spatial Quality = normal */
826
avio_wb32(pb, 0); /* Reserved */
827
avio_wb32(pb, 0); /* Reserved */
828
avio_wb32(pb, 0); /* Reserved */
830
avio_wb16(pb, track->enc->width); /* Video width */
831
avio_wb16(pb, track->height); /* Video height */
832
avio_wb32(pb, 0x00480000); /* Horizontal resolution 72dpi */
833
avio_wb32(pb, 0x00480000); /* Vertical resolution 72dpi */
834
avio_wb32(pb, 0); /* Data size (= 0) */
835
avio_wb16(pb, 1); /* Frame count (= 1) */
837
memset(compressor_name,0,32);
838
/* FIXME not sure, ISO 14496-1 draft where it shall be set to 0 */
839
if (track->mode == MODE_MOV && track->enc->codec && track->enc->codec->name)
840
av_strlcpy(compressor_name,track->enc->codec->name,32);
841
avio_w8(pb, strlen(compressor_name));
842
avio_write(pb, compressor_name, 31);
844
if (track->mode == MODE_MOV && track->enc->bits_per_coded_sample)
845
avio_wb16(pb, track->enc->bits_per_coded_sample);
847
avio_wb16(pb, 0x18); /* Reserved */
848
avio_wb16(pb, 0xffff); /* Reserved */
849
if(track->tag == MKTAG('m','p','4','v'))
850
mov_write_esds_tag(pb, track);
851
else if(track->enc->codec_id == CODEC_ID_H263)
852
mov_write_d263_tag(pb);
853
else if(track->enc->codec_id == CODEC_ID_SVQ3)
854
mov_write_svq3_tag(pb);
855
else if(track->enc->codec_id == CODEC_ID_DNXHD)
856
mov_write_avid_tag(pb, track);
857
else if(track->enc->codec_id == CODEC_ID_H264) {
858
mov_write_avcc_tag(pb, track);
859
if(track->mode == MODE_IPOD)
860
mov_write_uuid_tag_ipod(pb);
861
} else if(track->vosLen > 0)
862
mov_write_glbl_tag(pb, track);
864
if (track->enc->sample_aspect_ratio.den && track->enc->sample_aspect_ratio.num &&
865
track->enc->sample_aspect_ratio.den != track->enc->sample_aspect_ratio.num) {
866
mov_write_pasp_tag(pb, track);
869
return updateSize(pb, pos);
872
static int mov_write_rtp_tag(AVIOContext *pb, MOVTrack *track)
874
int64_t pos = avio_tell(pb);
875
avio_wb32(pb, 0); /* size */
876
ffio_wfourcc(pb, "rtp ");
877
avio_wb32(pb, 0); /* Reserved */
878
avio_wb16(pb, 0); /* Reserved */
879
avio_wb16(pb, 1); /* Data-reference index */
881
avio_wb16(pb, 1); /* Hint track version */
882
avio_wb16(pb, 1); /* Highest compatible version */
883
avio_wb32(pb, track->max_packet_size); /* Max packet size */
885
avio_wb32(pb, 12); /* size */
886
ffio_wfourcc(pb, "tims");
887
avio_wb32(pb, track->timescale);
889
return updateSize(pb, pos);
892
static int mov_write_stsd_tag(AVIOContext *pb, MOVTrack *track)
894
int64_t pos = avio_tell(pb);
895
avio_wb32(pb, 0); /* size */
896
ffio_wfourcc(pb, "stsd");
897
avio_wb32(pb, 0); /* version & flags */
898
avio_wb32(pb, 1); /* entry count */
899
if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO)
900
mov_write_video_tag(pb, track);
901
else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
902
mov_write_audio_tag(pb, track);
903
else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)
904
mov_write_subtitle_tag(pb, track);
905
else if (track->enc->codec_tag == MKTAG('r','t','p',' '))
906
mov_write_rtp_tag(pb, track);
907
return updateSize(pb, pos);
910
static int mov_write_ctts_tag(AVIOContext *pb, MOVTrack *track)
912
MOVStts *ctts_entries;
913
uint32_t entries = 0;
917
ctts_entries = av_malloc((track->entry + 1) * sizeof(*ctts_entries)); /* worst case */
918
ctts_entries[0].count = 1;
919
ctts_entries[0].duration = track->cluster[0].cts;
920
for (i=1; i<track->entry; i++) {
921
if (track->cluster[i].cts == ctts_entries[entries].duration) {
922
ctts_entries[entries].count++; /* compress */
925
ctts_entries[entries].duration = track->cluster[i].cts;
926
ctts_entries[entries].count = 1;
929
entries++; /* last one */
930
atom_size = 16 + (entries * 8);
931
avio_wb32(pb, atom_size); /* size */
932
ffio_wfourcc(pb, "ctts");
933
avio_wb32(pb, 0); /* version & flags */
934
avio_wb32(pb, entries); /* entry count */
935
for (i=0; i<entries; i++) {
936
avio_wb32(pb, ctts_entries[i].count);
937
avio_wb32(pb, ctts_entries[i].duration);
939
av_free(ctts_entries);
943
/* Time to sample atom */
944
static int mov_write_stts_tag(AVIOContext *pb, MOVTrack *track)
946
MOVStts *stts_entries;
947
uint32_t entries = -1;
951
if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO && !track->audio_vbr) {
952
stts_entries = av_malloc(sizeof(*stts_entries)); /* one entry */
953
stts_entries[0].count = track->sampleCount;
954
stts_entries[0].duration = 1;
957
stts_entries = av_malloc(track->entry * sizeof(*stts_entries)); /* worst case */
958
for (i=0; i<track->entry; i++) {
959
int64_t duration = i + 1 == track->entry ?
960
track->trackDuration - track->cluster[i].dts + track->cluster[0].dts : /* readjusting */
961
track->cluster[i+1].dts - track->cluster[i].dts;
962
if (i && duration == stts_entries[entries].duration) {
963
stts_entries[entries].count++; /* compress */
966
stts_entries[entries].duration = duration;
967
stts_entries[entries].count = 1;
970
entries++; /* last one */
972
atom_size = 16 + (entries * 8);
973
avio_wb32(pb, atom_size); /* size */
974
ffio_wfourcc(pb, "stts");
975
avio_wb32(pb, 0); /* version & flags */
976
avio_wb32(pb, entries); /* entry count */
977
for (i=0; i<entries; i++) {
978
avio_wb32(pb, stts_entries[i].count);
979
avio_wb32(pb, stts_entries[i].duration);
981
av_free(stts_entries);
985
static int mov_write_dref_tag(AVIOContext *pb)
987
avio_wb32(pb, 28); /* size */
988
ffio_wfourcc(pb, "dref");
989
avio_wb32(pb, 0); /* version & flags */
990
avio_wb32(pb, 1); /* entry count */
992
avio_wb32(pb, 0xc); /* size */
993
ffio_wfourcc(pb, "url ");
994
avio_wb32(pb, 1); /* version & flags */
999
static int mov_write_stbl_tag(AVIOContext *pb, MOVTrack *track)
1001
int64_t pos = avio_tell(pb);
1002
avio_wb32(pb, 0); /* size */
1003
ffio_wfourcc(pb, "stbl");
1004
mov_write_stsd_tag(pb, track);
1005
mov_write_stts_tag(pb, track);
1006
if ((track->enc->codec_type == AVMEDIA_TYPE_VIDEO ||
1007
track->enc->codec_tag == MKTAG('r','t','p',' ')) &&
1008
track->hasKeyframes && track->hasKeyframes < track->entry)
1009
mov_write_stss_tag(pb, track, MOV_SYNC_SAMPLE);
1010
if (track->mode == MODE_MOV && track->flags & MOV_TRACK_STPS)
1011
mov_write_stss_tag(pb, track, MOV_PARTIAL_SYNC_SAMPLE);
1012
if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO &&
1013
track->flags & MOV_TRACK_CTTS)
1014
mov_write_ctts_tag(pb, track);
1015
mov_write_stsc_tag(pb, track);
1016
mov_write_stsz_tag(pb, track);
1017
mov_write_stco_tag(pb, track);
1018
return updateSize(pb, pos);
1021
static int mov_write_dinf_tag(AVIOContext *pb)
1023
int64_t pos = avio_tell(pb);
1024
avio_wb32(pb, 0); /* size */
1025
ffio_wfourcc(pb, "dinf");
1026
mov_write_dref_tag(pb);
1027
return updateSize(pb, pos);
1030
static int mov_write_nmhd_tag(AVIOContext *pb)
1033
ffio_wfourcc(pb, "nmhd");
1038
static int mov_write_gmhd_tag(AVIOContext *pb)
1040
avio_wb32(pb, 0x20); /* size */
1041
ffio_wfourcc(pb, "gmhd");
1042
avio_wb32(pb, 0x18); /* gmin size */
1043
ffio_wfourcc(pb, "gmin");/* generic media info */
1044
avio_wb32(pb, 0); /* version & flags */
1045
avio_wb16(pb, 0x40); /* graphics mode = */
1046
avio_wb16(pb, 0x8000); /* opColor (r?) */
1047
avio_wb16(pb, 0x8000); /* opColor (g?) */
1048
avio_wb16(pb, 0x8000); /* opColor (b?) */
1049
avio_wb16(pb, 0); /* balance */
1050
avio_wb16(pb, 0); /* reserved */
1054
static int mov_write_smhd_tag(AVIOContext *pb)
1056
avio_wb32(pb, 16); /* size */
1057
ffio_wfourcc(pb, "smhd");
1058
avio_wb32(pb, 0); /* version & flags */
1059
avio_wb16(pb, 0); /* reserved (balance, normally = 0) */
1060
avio_wb16(pb, 0); /* reserved */
1064
static int mov_write_vmhd_tag(AVIOContext *pb)
1066
avio_wb32(pb, 0x14); /* size (always 0x14) */
1067
ffio_wfourcc(pb, "vmhd");
1068
avio_wb32(pb, 0x01); /* version & flags */
1069
avio_wb64(pb, 0); /* reserved (graphics mode = copy) */
1073
static int mov_write_hdlr_tag(AVIOContext *pb, MOVTrack *track)
1075
const char *hdlr, *descr = NULL, *hdlr_type = NULL;
1076
int64_t pos = avio_tell(pb);
1078
if (!track) { /* no media --> data handler */
1081
descr = "DataHandler";
1083
hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
1084
if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
1086
descr = "VideoHandler";
1087
} else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
1089
descr = "SoundHandler";
1090
} else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) {
1091
if (track->tag == MKTAG('t','x','3','g')) hdlr_type = "sbtl";
1092
else hdlr_type = "text";
1093
descr = "SubtitleHandler";
1094
} else if (track->enc->codec_tag == MKTAG('r','t','p',' ')) {
1096
descr = "HintHandler";
1100
avio_wb32(pb, 0); /* size */
1101
ffio_wfourcc(pb, "hdlr");
1102
avio_wb32(pb, 0); /* Version & flags */
1103
avio_write(pb, hdlr, 4); /* handler */
1104
ffio_wfourcc(pb, hdlr_type); /* handler type */
1105
avio_wb32(pb ,0); /* reserved */
1106
avio_wb32(pb ,0); /* reserved */
1107
avio_wb32(pb ,0); /* reserved */
1108
if (!track || track->mode == MODE_MOV)
1109
avio_w8(pb, strlen(descr)); /* pascal string */
1110
avio_write(pb, descr, strlen(descr)); /* handler description */
1111
if (track && track->mode != MODE_MOV)
1112
avio_w8(pb, 0); /* c string */
1113
return updateSize(pb, pos);
1116
static int mov_write_hmhd_tag(AVIOContext *pb)
1118
/* This atom must be present, but leaving the values at zero
1119
* seems harmless. */
1120
avio_wb32(pb, 28); /* size */
1121
ffio_wfourcc(pb, "hmhd");
1122
avio_wb32(pb, 0); /* version, flags */
1123
avio_wb16(pb, 0); /* maxPDUsize */
1124
avio_wb16(pb, 0); /* avgPDUsize */
1125
avio_wb32(pb, 0); /* maxbitrate */
1126
avio_wb32(pb, 0); /* avgbitrate */
1127
avio_wb32(pb, 0); /* reserved */
1131
static int mov_write_minf_tag(AVIOContext *pb, MOVTrack *track)
1133
int64_t pos = avio_tell(pb);
1134
avio_wb32(pb, 0); /* size */
1135
ffio_wfourcc(pb, "minf");
1136
if(track->enc->codec_type == AVMEDIA_TYPE_VIDEO)
1137
mov_write_vmhd_tag(pb);
1138
else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
1139
mov_write_smhd_tag(pb);
1140
else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) {
1141
if (track->tag == MKTAG('t','e','x','t')) mov_write_gmhd_tag(pb);
1142
else mov_write_nmhd_tag(pb);
1143
} else if (track->tag == MKTAG('r','t','p',' ')) {
1144
mov_write_hmhd_tag(pb);
1146
if (track->mode == MODE_MOV) /* FIXME: Why do it for MODE_MOV only ? */
1147
mov_write_hdlr_tag(pb, NULL);
1148
mov_write_dinf_tag(pb);
1149
mov_write_stbl_tag(pb, track);
1150
return updateSize(pb, pos);
1153
static int mov_write_mdhd_tag(AVIOContext *pb, MOVTrack *track)
1155
int version = track->trackDuration < INT32_MAX ? 0 : 1;
1157
(version == 1) ? avio_wb32(pb, 44) : avio_wb32(pb, 32); /* size */
1158
ffio_wfourcc(pb, "mdhd");
1159
avio_w8(pb, version);
1160
avio_wb24(pb, 0); /* flags */
1162
avio_wb64(pb, track->time);
1163
avio_wb64(pb, track->time);
1165
avio_wb32(pb, track->time); /* creation time */
1166
avio_wb32(pb, track->time); /* modification time */
1168
avio_wb32(pb, track->timescale); /* time scale (sample rate for audio) */
1169
(version == 1) ? avio_wb64(pb, track->trackDuration) : avio_wb32(pb, track->trackDuration); /* duration */
1170
avio_wb16(pb, track->language); /* language */
1171
avio_wb16(pb, 0); /* reserved (quality) */
1173
if(version!=0 && track->mode == MODE_MOV){
1174
av_log(NULL, AV_LOG_ERROR,
1175
"FATAL error, file duration too long for timebase, this file will not be\n"
1176
"playable with quicktime. Choose a different timebase or a different\n"
1177
"container format\n");
1183
static int mov_write_mdia_tag(AVIOContext *pb, MOVTrack *track)
1185
int64_t pos = avio_tell(pb);
1186
avio_wb32(pb, 0); /* size */
1187
ffio_wfourcc(pb, "mdia");
1188
mov_write_mdhd_tag(pb, track);
1189
mov_write_hdlr_tag(pb, track);
1190
mov_write_minf_tag(pb, track);
1191
return updateSize(pb, pos);
1194
static int mov_write_tkhd_tag(AVIOContext *pb, MOVTrack *track, AVStream *st)
1196
int64_t duration = av_rescale_rnd(track->trackDuration, MOV_TIMESCALE,
1197
track->timescale, AV_ROUND_UP);
1198
int version = duration < INT32_MAX ? 0 : 1;
1200
(version == 1) ? avio_wb32(pb, 104) : avio_wb32(pb, 92); /* size */
1201
ffio_wfourcc(pb, "tkhd");
1202
avio_w8(pb, version);
1203
avio_wb24(pb, 0xf); /* flags (track enabled) */
1205
avio_wb64(pb, track->time);
1206
avio_wb64(pb, track->time);
1208
avio_wb32(pb, track->time); /* creation time */
1209
avio_wb32(pb, track->time); /* modification time */
1211
avio_wb32(pb, track->trackID); /* track-id */
1212
avio_wb32(pb, 0); /* reserved */
1213
(version == 1) ? avio_wb64(pb, duration) : avio_wb32(pb, duration);
1215
avio_wb32(pb, 0); /* reserved */
1216
avio_wb32(pb, 0); /* reserved */
1217
avio_wb32(pb, 0x0); /* reserved (Layer & Alternate group) */
1218
/* Volume, only for audio */
1219
if(track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
1220
avio_wb16(pb, 0x0100);
1223
avio_wb16(pb, 0); /* reserved */
1225
/* Matrix structure */
1226
avio_wb32(pb, 0x00010000); /* reserved */
1227
avio_wb32(pb, 0x0); /* reserved */
1228
avio_wb32(pb, 0x0); /* reserved */
1229
avio_wb32(pb, 0x0); /* reserved */
1230
avio_wb32(pb, 0x00010000); /* reserved */
1231
avio_wb32(pb, 0x0); /* reserved */
1232
avio_wb32(pb, 0x0); /* reserved */
1233
avio_wb32(pb, 0x0); /* reserved */
1234
avio_wb32(pb, 0x40000000); /* reserved */
1236
/* Track width and height, for visual only */
1237
if(st && (track->enc->codec_type == AVMEDIA_TYPE_VIDEO ||
1238
track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)) {
1239
if(track->mode == MODE_MOV) {
1240
avio_wb32(pb, track->enc->width << 16);
1241
avio_wb32(pb, track->height << 16);
1243
double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
1244
if(!sample_aspect_ratio || track->height != track->enc->height)
1245
sample_aspect_ratio = 1;
1246
avio_wb32(pb, sample_aspect_ratio * track->enc->width*0x10000);
1247
avio_wb32(pb, track->height*0x10000);
1257
static int mov_write_tapt_tag(AVIOContext *pb, MOVTrack *track)
1259
int32_t width = av_rescale(track->enc->sample_aspect_ratio.num, track->enc->width,
1260
track->enc->sample_aspect_ratio.den);
1262
int64_t pos = avio_tell(pb);
1264
avio_wb32(pb, 0); /* size */
1265
ffio_wfourcc(pb, "tapt");
1268
ffio_wfourcc(pb, "clef");
1270
avio_wb32(pb, width << 16);
1271
avio_wb32(pb, track->enc->height << 16);
1274
ffio_wfourcc(pb, "enof");
1276
avio_wb32(pb, track->enc->width << 16);
1277
avio_wb32(pb, track->enc->height << 16);
1279
return updateSize(pb, pos);
1282
// This box seems important for the psp playback ... without it the movie seems to hang
1283
static int mov_write_edts_tag(AVIOContext *pb, MOVTrack *track)
1285
int64_t duration = av_rescale_rnd(track->trackDuration, MOV_TIMESCALE,
1286
track->timescale, AV_ROUND_UP);
1287
int version = duration < INT32_MAX ? 0 : 1;
1288
int entry_size, entry_count, size;
1289
int64_t delay, start_ct = track->cluster[0].cts;
1290
delay = av_rescale_rnd(track->cluster[0].dts + start_ct, MOV_TIMESCALE,
1291
track->timescale, AV_ROUND_DOWN);
1292
version |= delay < INT32_MAX ? 0 : 1;
1294
entry_size = (version == 1) ? 20 : 12;
1295
entry_count = 1 + (delay > 0);
1296
size = 24 + entry_count * entry_size;
1298
/* write the atom data */
1299
avio_wb32(pb, size);
1300
ffio_wfourcc(pb, "edts");
1301
avio_wb32(pb, size - 8);
1302
ffio_wfourcc(pb, "elst");
1303
avio_w8(pb, version);
1304
avio_wb24(pb, 0); /* flags */
1306
avio_wb32(pb, entry_count);
1307
if (delay > 0) { /* add an empty edit to delay presentation */
1309
avio_wb64(pb, delay);
1312
avio_wb32(pb, delay);
1315
avio_wb32(pb, 0x00010000);
1320
avio_wb64(pb, duration);
1321
avio_wb64(pb, start_ct);
1323
avio_wb32(pb, duration);
1324
avio_wb32(pb, start_ct);
1326
avio_wb32(pb, 0x00010000);
1330
static int mov_write_tref_tag(AVIOContext *pb, MOVTrack *track)
1332
avio_wb32(pb, 20); // size
1333
ffio_wfourcc(pb, "tref");
1334
avio_wb32(pb, 12); // size (subatom)
1335
avio_wl32(pb, track->tref_tag);
1336
avio_wb32(pb, track->tref_id);
1340
// goes at the end of each track! ... Critical for PSP playback ("Incompatible data" without it)
1341
static int mov_write_uuid_tag_psp(AVIOContext *pb, MOVTrack *mov)
1343
avio_wb32(pb, 0x34); /* size ... reports as 28 in mp4box! */
1344
ffio_wfourcc(pb, "uuid");
1345
ffio_wfourcc(pb, "USMT");
1346
avio_wb32(pb, 0x21d24fce);
1347
avio_wb32(pb, 0xbb88695c);
1348
avio_wb32(pb, 0xfac9c740);
1349
avio_wb32(pb, 0x1c); // another size here!
1350
ffio_wfourcc(pb, "MTDT");
1351
avio_wb32(pb, 0x00010012);
1352
avio_wb32(pb, 0x0a);
1353
avio_wb32(pb, 0x55c40000);
1359
static int mov_write_udta_sdp(AVIOContext *pb, AVFormatContext *ctx, int index)
1361
char buf[1000] = "";
1364
ff_sdp_write_media(buf, sizeof(buf), ctx->streams[0]->codec, NULL, NULL, 0, 0, ctx);
1365
av_strlcatf(buf, sizeof(buf), "a=control:streamid=%d\r\n", index);
1368
avio_wb32(pb, len + 24);
1369
ffio_wfourcc(pb, "udta");
1370
avio_wb32(pb, len + 16);
1371
ffio_wfourcc(pb, "hnti");
1372
avio_wb32(pb, len + 8);
1373
ffio_wfourcc(pb, "sdp ");
1374
avio_write(pb, buf, len);
1378
static int mov_write_trak_tag(AVIOContext *pb, MOVTrack *track, AVStream *st)
1380
int64_t pos = avio_tell(pb);
1381
avio_wb32(pb, 0); /* size */
1382
ffio_wfourcc(pb, "trak");
1383
mov_write_tkhd_tag(pb, track, st);
1384
if (track->mode == MODE_PSP || track->flags & MOV_TRACK_CTTS || track->cluster[0].dts)
1385
mov_write_edts_tag(pb, track); // PSP Movies require edts box
1386
if (track->tref_tag)
1387
mov_write_tref_tag(pb, track);
1388
mov_write_mdia_tag(pb, track);
1389
if (track->mode == MODE_PSP)
1390
mov_write_uuid_tag_psp(pb,track); // PSP Movies require this uuid box
1391
if (track->tag == MKTAG('r','t','p',' '))
1392
mov_write_udta_sdp(pb, track->rtp_ctx, track->trackID);
1393
if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO && track->mode == MODE_MOV) {
1394
double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
1395
if (0.0 != sample_aspect_ratio && 1.0 != sample_aspect_ratio)
1396
mov_write_tapt_tag(pb, track);
1398
return updateSize(pb, pos);
1402
/* TODO: Not sorted out, but not necessary either */
1403
static int mov_write_iods_tag(AVIOContext *pb, MOVMuxContext *mov)
1405
avio_wb32(pb, 0x15); /* size */
1406
ffio_wfourcc(pb, "iods");
1407
avio_wb32(pb, 0); /* version & flags */
1408
avio_wb16(pb, 0x1007);
1410
avio_wb16(pb, 0x4fff);
1411
avio_wb16(pb, 0xfffe);
1412
avio_wb16(pb, 0x01ff);
1417
static int mov_write_mvhd_tag(AVIOContext *pb, MOVMuxContext *mov)
1419
int maxTrackID = 1, i;
1420
int64_t maxTrackLenTemp, maxTrackLen = 0;
1423
for (i=0; i<mov->nb_streams; i++) {
1424
if(mov->tracks[i].entry > 0) {
1425
maxTrackLenTemp = av_rescale_rnd(mov->tracks[i].trackDuration,
1427
mov->tracks[i].timescale,
1429
if(maxTrackLen < maxTrackLenTemp)
1430
maxTrackLen = maxTrackLenTemp;
1431
if(maxTrackID < mov->tracks[i].trackID)
1432
maxTrackID = mov->tracks[i].trackID;
1436
version = maxTrackLen < UINT32_MAX ? 0 : 1;
1437
(version == 1) ? avio_wb32(pb, 120) : avio_wb32(pb, 108); /* size */
1438
ffio_wfourcc(pb, "mvhd");
1439
avio_w8(pb, version);
1440
avio_wb24(pb, 0); /* flags */
1442
avio_wb64(pb, mov->time);
1443
avio_wb64(pb, mov->time);
1445
avio_wb32(pb, mov->time); /* creation time */
1446
avio_wb32(pb, mov->time); /* modification time */
1448
avio_wb32(pb, MOV_TIMESCALE);
1449
(version == 1) ? avio_wb64(pb, maxTrackLen) : avio_wb32(pb, maxTrackLen); /* duration of longest track */
1451
avio_wb32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
1452
avio_wb16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
1453
avio_wb16(pb, 0); /* reserved */
1454
avio_wb32(pb, 0); /* reserved */
1455
avio_wb32(pb, 0); /* reserved */
1457
/* Matrix structure */
1458
avio_wb32(pb, 0x00010000); /* reserved */
1459
avio_wb32(pb, 0x0); /* reserved */
1460
avio_wb32(pb, 0x0); /* reserved */
1461
avio_wb32(pb, 0x0); /* reserved */
1462
avio_wb32(pb, 0x00010000); /* reserved */
1463
avio_wb32(pb, 0x0); /* reserved */
1464
avio_wb32(pb, 0x0); /* reserved */
1465
avio_wb32(pb, 0x0); /* reserved */
1466
avio_wb32(pb, 0x40000000); /* reserved */
1468
avio_wb32(pb, 0); /* reserved (preview time) */
1469
avio_wb32(pb, 0); /* reserved (preview duration) */
1470
avio_wb32(pb, 0); /* reserved (poster time) */
1471
avio_wb32(pb, 0); /* reserved (selection time) */
1472
avio_wb32(pb, 0); /* reserved (selection duration) */
1473
avio_wb32(pb, 0); /* reserved (current time) */
1474
avio_wb32(pb, maxTrackID+1); /* Next track id */
1478
static int mov_write_itunes_hdlr_tag(AVIOContext *pb, MOVMuxContext *mov,
1481
avio_wb32(pb, 33); /* size */
1482
ffio_wfourcc(pb, "hdlr");
1485
ffio_wfourcc(pb, "mdir");
1486
ffio_wfourcc(pb, "appl");
1493
/* helper function to write a data tag with the specified string as data */
1494
static int mov_write_string_data_tag(AVIOContext *pb, const char *data, int lang, int long_style)
1497
int size = 16 + strlen(data);
1498
avio_wb32(pb, size); /* size */
1499
ffio_wfourcc(pb, "data");
1502
avio_write(pb, data, strlen(data));
1506
lang = ff_mov_iso639_to_lang("und", 1);
1507
avio_wb16(pb, strlen(data)); /* string length */
1508
avio_wb16(pb, lang);
1509
avio_write(pb, data, strlen(data));
1510
return strlen(data) + 4;
1514
static int mov_write_string_tag(AVIOContext *pb, const char *name, const char *value, int lang, int long_style){
1516
if (value && value[0]) {
1517
int64_t pos = avio_tell(pb);
1518
avio_wb32(pb, 0); /* size */
1519
ffio_wfourcc(pb, name);
1520
mov_write_string_data_tag(pb, value, lang, long_style);
1521
size= updateSize(pb, pos);
1526
static int mov_write_string_metadata(AVFormatContext *s, AVIOContext *pb,
1527
const char *name, const char *tag,
1530
int l, lang = 0, len, len2;
1531
AVDictionaryEntry *t, *t2 = NULL;
1534
if (!(t = av_dict_get(s->metadata, tag, NULL, 0)))
1537
len = strlen(t->key);
1538
snprintf(tag2, sizeof(tag2), "%s-", tag);
1539
while ((t2 = av_dict_get(s->metadata, tag2, t2, AV_DICT_IGNORE_SUFFIX))) {
1540
len2 = strlen(t2->key);
1541
if (len2 == len+4 && !strcmp(t->value, t2->value)
1542
&& (l=ff_mov_iso639_to_lang(&t2->key[len2-3], 1)) >= 0) {
1547
return mov_write_string_tag(pb, name, t->value, lang, long_style);
1550
/* iTunes track number */
1551
static int mov_write_trkn_tag(AVIOContext *pb, MOVMuxContext *mov,
1554
AVDictionaryEntry *t = av_dict_get(s->metadata, "track", NULL, 0);
1555
int size = 0, track = t ? atoi(t->value) : 0;
1557
avio_wb32(pb, 32); /* size */
1558
ffio_wfourcc(pb, "trkn");
1559
avio_wb32(pb, 24); /* size */
1560
ffio_wfourcc(pb, "data");
1561
avio_wb32(pb, 0); // 8 bytes empty
1563
avio_wb16(pb, 0); // empty
1564
avio_wb16(pb, track); // track number
1565
avio_wb16(pb, 0); // total track number
1566
avio_wb16(pb, 0); // empty
1572
/* iTunes meta data list */
1573
static int mov_write_ilst_tag(AVIOContext *pb, MOVMuxContext *mov,
1576
int64_t pos = avio_tell(pb);
1577
avio_wb32(pb, 0); /* size */
1578
ffio_wfourcc(pb, "ilst");
1579
mov_write_string_metadata(s, pb, "\251nam", "title" , 1);
1580
mov_write_string_metadata(s, pb, "\251ART", "artist" , 1);
1581
mov_write_string_metadata(s, pb, "aART", "album_artist", 1);
1582
mov_write_string_metadata(s, pb, "\251wrt", "composer" , 1);
1583
mov_write_string_metadata(s, pb, "\251alb", "album" , 1);
1584
mov_write_string_metadata(s, pb, "\251day", "date" , 1);
1585
mov_write_string_tag(pb, "\251too", LIBAVFORMAT_IDENT, 0, 1);
1586
mov_write_string_metadata(s, pb, "\251cmt", "comment" , 1);
1587
mov_write_string_metadata(s, pb, "\251gen", "genre" , 1);
1588
mov_write_string_metadata(s, pb, "\251cpy", "copyright", 1);
1589
mov_write_string_metadata(s, pb, "\251grp", "grouping" , 1);
1590
mov_write_string_metadata(s, pb, "\251lyr", "lyrics" , 1);
1591
mov_write_string_metadata(s, pb, "desc", "description",1);
1592
mov_write_string_metadata(s, pb, "ldes", "synopsis" , 1);
1593
mov_write_string_metadata(s, pb, "tvsh", "show" , 1);
1594
mov_write_string_metadata(s, pb, "tven", "episode_id",1);
1595
mov_write_string_metadata(s, pb, "tvnn", "network" , 1);
1596
mov_write_trkn_tag(pb, mov, s);
1597
return updateSize(pb, pos);
1600
/* iTunes meta data tag */
1601
static int mov_write_meta_tag(AVIOContext *pb, MOVMuxContext *mov,
1605
int64_t pos = avio_tell(pb);
1606
avio_wb32(pb, 0); /* size */
1607
ffio_wfourcc(pb, "meta");
1609
mov_write_itunes_hdlr_tag(pb, mov, s);
1610
mov_write_ilst_tag(pb, mov, s);
1611
size = updateSize(pb, pos);
1615
static int utf8len(const uint8_t *b)
1620
GET_UTF8(val, *b++, return -1;)
1626
static int ascii_to_wc(AVIOContext *pb, const uint8_t *b)
1630
GET_UTF8(val, *b++, return -1;)
1633
avio_wb16(pb, 0x00);
1637
static uint16_t language_code(const char *str)
1639
return (((str[0]-0x60) & 0x1F) << 10) + (((str[1]-0x60) & 0x1F) << 5) + ((str[2]-0x60) & 0x1F);
1642
static int mov_write_3gp_udta_tag(AVIOContext *pb, AVFormatContext *s,
1643
const char *tag, const char *str)
1645
int64_t pos = avio_tell(pb);
1646
AVDictionaryEntry *t = av_dict_get(s->metadata, str, NULL, 0);
1647
if (!t || !utf8len(t->value))
1649
avio_wb32(pb, 0); /* size */
1650
ffio_wfourcc(pb, tag); /* type */
1651
avio_wb32(pb, 0); /* version + flags */
1652
if (!strcmp(tag, "yrrc"))
1653
avio_wb16(pb, atoi(t->value));
1655
avio_wb16(pb, language_code("eng")); /* language */
1656
avio_write(pb, t->value, strlen(t->value)+1); /* UTF8 string value */
1657
if (!strcmp(tag, "albm") &&
1658
(t = av_dict_get(s->metadata, "track", NULL, 0)))
1659
avio_w8(pb, atoi(t->value));
1661
return updateSize(pb, pos);
1664
static int mov_write_chpl_tag(AVIOContext *pb, AVFormatContext *s)
1666
int64_t pos = avio_tell(pb);
1667
int i, nb_chapters = FFMIN(s->nb_chapters, 255);
1669
avio_wb32(pb, 0); // size
1670
ffio_wfourcc(pb, "chpl");
1671
avio_wb32(pb, 0x01000000); // version + flags
1672
avio_wb32(pb, 0); // unknown
1673
avio_w8(pb, nb_chapters);
1675
for (i = 0; i < nb_chapters; i++) {
1676
AVChapter *c = s->chapters[i];
1677
AVDictionaryEntry *t;
1678
avio_wb64(pb, av_rescale_q(c->start, c->time_base, (AVRational){1,10000000}));
1680
if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
1681
int len = FFMIN(strlen(t->value), 255);
1683
avio_write(pb, t->value, len);
1687
return updateSize(pb, pos);
1690
static int mov_write_udta_tag(AVIOContext *pb, MOVMuxContext *mov,
1693
AVIOContext *pb_buf;
1697
for (i = 0; i < s->nb_streams; i++)
1698
if (mov->tracks[i].enc->flags & CODEC_FLAG_BITEXACT) {
1702
ret = avio_open_dyn_buf(&pb_buf);
1706
if (mov->mode & MODE_3GP) {
1707
mov_write_3gp_udta_tag(pb_buf, s, "perf", "artist");
1708
mov_write_3gp_udta_tag(pb_buf, s, "titl", "title");
1709
mov_write_3gp_udta_tag(pb_buf, s, "auth", "author");
1710
mov_write_3gp_udta_tag(pb_buf, s, "gnre", "genre");
1711
mov_write_3gp_udta_tag(pb_buf, s, "dscp", "comment");
1712
mov_write_3gp_udta_tag(pb_buf, s, "albm", "album");
1713
mov_write_3gp_udta_tag(pb_buf, s, "cprt", "copyright");
1714
mov_write_3gp_udta_tag(pb_buf, s, "yrrc", "date");
1715
} else if (mov->mode == MODE_MOV) { // the title field breaks gtkpod with mp4 and my suspicion is that stuff is not valid in mp4
1716
mov_write_string_metadata(s, pb_buf, "\251ART", "artist" , 0);
1717
mov_write_string_metadata(s, pb_buf, "\251nam", "title" , 0);
1718
mov_write_string_metadata(s, pb_buf, "\251aut", "author" , 0);
1719
mov_write_string_metadata(s, pb_buf, "\251alb", "album" , 0);
1720
mov_write_string_metadata(s, pb_buf, "\251day", "date" , 0);
1721
mov_write_string_metadata(s, pb_buf, "\251swr", "encoder" , 0);
1722
mov_write_string_metadata(s, pb_buf, "\251des", "comment" , 0);
1723
mov_write_string_metadata(s, pb_buf, "\251gen", "genre" , 0);
1724
mov_write_string_metadata(s, pb_buf, "\251cpy", "copyright" , 0);
1726
/* iTunes meta data */
1727
mov_write_meta_tag(pb_buf, mov, s);
1731
mov_write_chpl_tag(pb_buf, s);
1733
if ((size = avio_close_dyn_buf(pb_buf, &buf)) > 0) {
1734
avio_wb32(pb, size+8);
1735
ffio_wfourcc(pb, "udta");
1736
avio_write(pb, buf, size);
1743
static void mov_write_psp_udta_tag(AVIOContext *pb,
1744
const char *str, const char *lang, int type)
1746
int len = utf8len(str)+1;
1749
avio_wb16(pb, len*2+10); /* size */
1750
avio_wb32(pb, type); /* type */
1751
avio_wb16(pb, language_code(lang)); /* language */
1752
avio_wb16(pb, 0x01); /* ? */
1753
ascii_to_wc(pb, str);
1756
static int mov_write_uuidusmt_tag(AVIOContext *pb, AVFormatContext *s)
1758
AVDictionaryEntry *title = av_dict_get(s->metadata, "title", NULL, 0);
1762
pos = avio_tell(pb);
1763
avio_wb32(pb, 0); /* size placeholder*/
1764
ffio_wfourcc(pb, "uuid");
1765
ffio_wfourcc(pb, "USMT");
1766
avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
1767
avio_wb32(pb, 0xbb88695c);
1768
avio_wb32(pb, 0xfac9c740);
1770
pos2 = avio_tell(pb);
1771
avio_wb32(pb, 0); /* size placeholder*/
1772
ffio_wfourcc(pb, "MTDT");
1776
avio_wb16(pb, 0x0C); /* size */
1777
avio_wb32(pb, 0x0B); /* type */
1778
avio_wb16(pb, language_code("und")); /* language */
1779
avio_wb16(pb, 0x0); /* ? */
1780
avio_wb16(pb, 0x021C); /* data */
1782
mov_write_psp_udta_tag(pb, LIBAVCODEC_IDENT, "eng", 0x04);
1783
mov_write_psp_udta_tag(pb, title->value, "eng", 0x01);
1784
// 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);
1785
mov_write_psp_udta_tag(pb, "2006/04/01 11:11:11", "und", 0x03);
1787
updateSize(pb, pos2);
1788
return updateSize(pb, pos);
1794
static int mov_write_moov_tag(AVIOContext *pb, MOVMuxContext *mov,
1798
int64_t pos = avio_tell(pb);
1799
avio_wb32(pb, 0); /* size placeholder*/
1800
ffio_wfourcc(pb, "moov");
1802
for (i=0; i<mov->nb_streams; i++) {
1803
if(mov->tracks[i].entry <= 0) continue;
1805
mov->tracks[i].time = mov->time;
1806
mov->tracks[i].trackID = i+1;
1809
if (mov->chapter_track)
1810
for (i=0; i<s->nb_streams; i++) {
1811
mov->tracks[i].tref_tag = MKTAG('c','h','a','p');
1812
mov->tracks[i].tref_id = mov->tracks[mov->chapter_track].trackID;
1814
for (i = 0; i < mov->nb_streams; i++) {
1815
if (mov->tracks[i].tag == MKTAG('r','t','p',' ')) {
1816
mov->tracks[i].tref_tag = MKTAG('h','i','n','t');
1817
mov->tracks[i].tref_id =
1818
mov->tracks[mov->tracks[i].src_track].trackID;
1822
mov_write_mvhd_tag(pb, mov);
1823
//mov_write_iods_tag(pb, mov);
1824
for (i=0; i<mov->nb_streams; i++) {
1825
if(mov->tracks[i].entry > 0) {
1826
mov_write_trak_tag(pb, &(mov->tracks[i]), i < s->nb_streams ? s->streams[i] : NULL);
1830
if (mov->mode == MODE_PSP)
1831
mov_write_uuidusmt_tag(pb, s);
1833
mov_write_udta_tag(pb, mov, s);
1835
return updateSize(pb, pos);
1838
static int mov_write_mdat_tag(AVIOContext *pb, MOVMuxContext *mov)
1840
avio_wb32(pb, 8); // placeholder for extended size field (64 bit)
1841
ffio_wfourcc(pb, mov->mode == MODE_MOV ? "wide" : "free");
1843
mov->mdat_pos = avio_tell(pb);
1844
avio_wb32(pb, 0); /* size placeholder*/
1845
ffio_wfourcc(pb, "mdat");
1849
/* TODO: This needs to be more general */
1850
static int mov_write_ftyp_tag(AVIOContext *pb, AVFormatContext *s)
1852
MOVMuxContext *mov = s->priv_data;
1853
int64_t pos = avio_tell(pb);
1854
int has_h264 = 0, has_video = 0;
1858
for (i = 0; i < s->nb_streams; i++) {
1859
AVStream *st = s->streams[i];
1860
if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
1862
if (st->codec->codec_id == CODEC_ID_H264)
1866
avio_wb32(pb, 0); /* size */
1867
ffio_wfourcc(pb, "ftyp");
1869
if (mov->mode == MODE_3GP) {
1870
ffio_wfourcc(pb, has_h264 ? "3gp6" : "3gp4");
1871
minor = has_h264 ? 0x100 : 0x200;
1872
} else if (mov->mode & MODE_3G2) {
1873
ffio_wfourcc(pb, has_h264 ? "3g2b" : "3g2a");
1874
minor = has_h264 ? 0x20000 : 0x10000;
1875
}else if (mov->mode == MODE_PSP)
1876
ffio_wfourcc(pb, "MSNV");
1877
else if (mov->mode == MODE_MP4)
1878
ffio_wfourcc(pb, "isom");
1879
else if (mov->mode == MODE_IPOD)
1880
ffio_wfourcc(pb, has_video ? "M4V ":"M4A ");
1882
ffio_wfourcc(pb, "qt ");
1884
avio_wb32(pb, minor);
1886
if(mov->mode == MODE_MOV)
1887
ffio_wfourcc(pb, "qt ");
1889
ffio_wfourcc(pb, "isom");
1890
ffio_wfourcc(pb, "iso2");
1892
ffio_wfourcc(pb, "avc1");
1895
if (mov->mode == MODE_3GP)
1896
ffio_wfourcc(pb, has_h264 ? "3gp6":"3gp4");
1897
else if (mov->mode & MODE_3G2)
1898
ffio_wfourcc(pb, has_h264 ? "3g2b":"3g2a");
1899
else if (mov->mode == MODE_PSP)
1900
ffio_wfourcc(pb, "MSNV");
1901
else if (mov->mode == MODE_MP4)
1902
ffio_wfourcc(pb, "mp41");
1903
return updateSize(pb, pos);
1906
static void mov_write_uuidprof_tag(AVIOContext *pb, AVFormatContext *s)
1908
AVCodecContext *VideoCodec = s->streams[0]->codec;
1909
AVCodecContext *AudioCodec = s->streams[1]->codec;
1910
int AudioRate = AudioCodec->sample_rate;
1911
int FrameRate = ((VideoCodec->time_base.den) * (0x10000))/ (VideoCodec->time_base.num);
1912
int audio_kbitrate= AudioCodec->bit_rate / 1000;
1913
int video_kbitrate= FFMIN(VideoCodec->bit_rate / 1000, 800 - audio_kbitrate);
1915
avio_wb32(pb, 0x94); /* size */
1916
ffio_wfourcc(pb, "uuid");
1917
ffio_wfourcc(pb, "PROF");
1919
avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
1920
avio_wb32(pb, 0xbb88695c);
1921
avio_wb32(pb, 0xfac9c740);
1923
avio_wb32(pb, 0x0); /* ? */
1924
avio_wb32(pb, 0x3); /* 3 sections ? */
1926
avio_wb32(pb, 0x14); /* size */
1927
ffio_wfourcc(pb, "FPRF");
1928
avio_wb32(pb, 0x0); /* ? */
1929
avio_wb32(pb, 0x0); /* ? */
1930
avio_wb32(pb, 0x0); /* ? */
1932
avio_wb32(pb, 0x2c); /* size */
1933
ffio_wfourcc(pb, "APRF");/* audio */
1935
avio_wb32(pb, 0x2); /* TrackID */
1936
ffio_wfourcc(pb, "mp4a");
1937
avio_wb32(pb, 0x20f);
1939
avio_wb32(pb, audio_kbitrate);
1940
avio_wb32(pb, audio_kbitrate);
1941
avio_wb32(pb, AudioRate);
1942
avio_wb32(pb, AudioCodec->channels);
1944
avio_wb32(pb, 0x34); /* size */
1945
ffio_wfourcc(pb, "VPRF"); /* video */
1947
avio_wb32(pb, 0x1); /* TrackID */
1948
if (VideoCodec->codec_id == CODEC_ID_H264) {
1949
ffio_wfourcc(pb, "avc1");
1950
avio_wb16(pb, 0x014D);
1951
avio_wb16(pb, 0x0015);
1953
ffio_wfourcc(pb, "mp4v");
1954
avio_wb16(pb, 0x0000);
1955
avio_wb16(pb, 0x0103);
1958
avio_wb32(pb, video_kbitrate);
1959
avio_wb32(pb, video_kbitrate);
1960
avio_wb32(pb, FrameRate);
1961
avio_wb32(pb, FrameRate);
1962
avio_wb16(pb, VideoCodec->width);
1963
avio_wb16(pb, VideoCodec->height);
1964
avio_wb32(pb, 0x010001); /* ? */
1967
static int mov_parse_mpeg2_frame(AVPacket *pkt, uint32_t *flags)
1970
int i, closed_gop = 0;
1972
for (i = 0; i < pkt->size - 4; i++) {
1973
c = (c<<8) + pkt->data[i];
1974
if (c == 0x1b8) { // gop
1975
closed_gop = pkt->data[i+4]>>6 & 0x01;
1976
} else if (c == 0x100) { // pic
1977
int temp_ref = (pkt->data[i+1]<<2) | (pkt->data[i+2]>>6);
1978
if (!temp_ref || closed_gop) // I picture is not reordered
1979
*flags = MOV_SYNC_SAMPLE;
1981
*flags = MOV_PARTIAL_SYNC_SAMPLE;
1988
int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
1990
MOVMuxContext *mov = s->priv_data;
1991
AVIOContext *pb = s->pb;
1992
MOVTrack *trk = &mov->tracks[pkt->stream_index];
1993
AVCodecContext *enc = trk->enc;
1994
unsigned int samplesInChunk = 0;
1995
int size= pkt->size;
1997
if (!s->pb->seekable) return 0; /* Can't handle that */
1998
if (!size) return 0; /* Discard 0 sized packets */
2000
if (enc->codec_id == CODEC_ID_AMR_NB) {
2001
/* We must find out how many AMR blocks there are in one packet */
2002
static uint16_t packed_size[16] =
2003
{13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 0};
2006
while (len < size && samplesInChunk < 100) {
2007
len += packed_size[(pkt->data[len] >> 3) & 0x0F];
2010
if(samplesInChunk > 1){
2011
av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, implement a AVParser for it\n");
2014
} else if (enc->codec_id == CODEC_ID_ADPCM_MS ||
2015
enc->codec_id == CODEC_ID_ADPCM_IMA_WAV) {
2016
samplesInChunk = enc->frame_size;
2017
} else if (trk->sampleSize)
2018
samplesInChunk = size/trk->sampleSize;
2022
/* copy extradata if it exists */
2023
if (trk->vosLen == 0 && enc->extradata_size > 0) {
2024
trk->vosLen = enc->extradata_size;
2025
trk->vosData = av_malloc(trk->vosLen);
2026
memcpy(trk->vosData, enc->extradata, trk->vosLen);
2029
if (enc->codec_id == CODEC_ID_H264 && trk->vosLen > 0 && *(uint8_t *)trk->vosData != 1) {
2030
/* from x264 or from bytestream h264 */
2031
/* nal reformating needed */
2032
size = ff_avc_parse_nal_units(pb, pkt->data, pkt->size);
2034
avio_write(pb, pkt->data, size);
2037
if ((enc->codec_id == CODEC_ID_DNXHD ||
2038
enc->codec_id == CODEC_ID_AC3) && !trk->vosLen) {
2039
/* copy frame to create needed atoms */
2041
trk->vosData = av_malloc(size);
2043
return AVERROR(ENOMEM);
2044
memcpy(trk->vosData, pkt->data, size);
2047
if (!(trk->entry % MOV_INDEX_CLUSTER_SIZE)) {
2048
trk->cluster = av_realloc(trk->cluster, (trk->entry + MOV_INDEX_CLUSTER_SIZE) * sizeof(*trk->cluster));
2053
trk->cluster[trk->entry].pos = avio_tell(pb) - size;
2054
trk->cluster[trk->entry].samplesInChunk = samplesInChunk;
2055
trk->cluster[trk->entry].size = size;
2056
trk->cluster[trk->entry].entries = samplesInChunk;
2057
trk->cluster[trk->entry].dts = pkt->dts;
2058
trk->trackDuration = pkt->dts - trk->cluster[0].dts + pkt->duration;
2060
if (pkt->pts == AV_NOPTS_VALUE) {
2061
av_log(s, AV_LOG_WARNING, "pts has no value\n");
2062
pkt->pts = pkt->dts;
2064
if (pkt->dts != pkt->pts)
2065
trk->flags |= MOV_TRACK_CTTS;
2066
trk->cluster[trk->entry].cts = pkt->pts - pkt->dts;
2067
trk->cluster[trk->entry].flags = 0;
2068
if (pkt->flags & AV_PKT_FLAG_KEY) {
2069
if (mov->mode == MODE_MOV && enc->codec_id == CODEC_ID_MPEG2VIDEO &&
2070
trk->entry > 0) { // force sync sample for the first key frame
2071
mov_parse_mpeg2_frame(pkt, &trk->cluster[trk->entry].flags);
2072
if (trk->cluster[trk->entry].flags & MOV_PARTIAL_SYNC_SAMPLE)
2073
trk->flags |= MOV_TRACK_STPS;
2075
trk->cluster[trk->entry].flags = MOV_SYNC_SAMPLE;
2077
if (trk->cluster[trk->entry].flags & MOV_SYNC_SAMPLE)
2078
trk->hasKeyframes++;
2081
trk->sampleCount += samplesInChunk;
2082
mov->mdat_size += size;
2086
if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams)
2087
ff_mov_add_hinted_packet(s, pkt, trk->hint_track, trk->entry);
2091
// QuickTime chapters involve an additional text track with the chapter names
2092
// as samples, and a tref pointing from the other tracks to the chapter one.
2093
static void mov_create_chapter_track(AVFormatContext *s, int tracknum)
2095
MOVMuxContext *mov = s->priv_data;
2096
MOVTrack *track = &mov->tracks[tracknum];
2097
AVPacket pkt = { .stream_index = tracknum, .flags = AV_PKT_FLAG_KEY };
2100
track->mode = mov->mode;
2101
track->tag = MKTAG('t','e','x','t');
2102
track->timescale = MOV_TIMESCALE;
2103
track->enc = avcodec_alloc_context();
2104
track->enc->codec_type = AVMEDIA_TYPE_SUBTITLE;
2106
for (i = 0; i < s->nb_chapters; i++) {
2107
AVChapter *c = s->chapters[i];
2108
AVDictionaryEntry *t;
2110
int64_t end = av_rescale_q(c->end, c->time_base, (AVRational){1,MOV_TIMESCALE});
2111
pkt.pts = pkt.dts = av_rescale_q(c->start, c->time_base, (AVRational){1,MOV_TIMESCALE});
2112
pkt.duration = end - pkt.dts;
2114
if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
2115
len = strlen(t->value);
2117
pkt.data = av_malloc(pkt.size);
2118
AV_WB16(pkt.data, len);
2119
memcpy(pkt.data+2, t->value, len);
2120
ff_mov_write_packet(s, &pkt);
2121
av_freep(&pkt.data);
2126
static int mov_write_header(AVFormatContext *s)
2128
AVIOContext *pb = s->pb;
2129
MOVMuxContext *mov = s->priv_data;
2130
int i, hint_track = 0;
2132
if (!s->pb->seekable) {
2133
av_log(s, AV_LOG_ERROR, "muxer does not support non seekable output\n");
2137
/* Default mode == MP4 */
2138
mov->mode = MODE_MP4;
2140
if (s->oformat != NULL) {
2141
if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
2142
else if (!strcmp("3g2", s->oformat->name)) mov->mode = MODE_3GP|MODE_3G2;
2143
else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
2144
else if (!strcmp("psp", s->oformat->name)) mov->mode = MODE_PSP;
2145
else if (!strcmp("ipod",s->oformat->name)) mov->mode = MODE_IPOD;
2147
mov_write_ftyp_tag(pb,s);
2148
if (mov->mode == MODE_PSP) {
2149
if (s->nb_streams != 2) {
2150
av_log(s, AV_LOG_ERROR, "PSP mode need one video and one audio stream\n");
2153
mov_write_uuidprof_tag(pb,s);
2157
mov->nb_streams = s->nb_streams;
2158
if (mov->mode & (MODE_MOV|MODE_IPOD) && s->nb_chapters)
2159
mov->chapter_track = mov->nb_streams++;
2161
#if FF_API_FLAG_RTP_HINT
2162
if (s->flags & AVFMT_FLAG_RTP_HINT) {
2163
av_log(s, AV_LOG_WARNING, "The RTP_HINT flag is deprecated, enable it "
2164
"via the -movflags rtphint muxer option "
2166
mov->flags |= FF_MOV_FLAG_RTP_HINT;
2169
if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
2170
/* Add hint tracks for each audio and video stream */
2171
hint_track = mov->nb_streams;
2172
for (i = 0; i < s->nb_streams; i++) {
2173
AVStream *st = s->streams[i];
2174
if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
2175
st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
2181
mov->tracks = av_mallocz(mov->nb_streams*sizeof(*mov->tracks));
2183
return AVERROR(ENOMEM);
2185
for(i=0; i<s->nb_streams; i++){
2186
AVStream *st= s->streams[i];
2187
MOVTrack *track= &mov->tracks[i];
2188
AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL,0);
2190
track->enc = st->codec;
2191
track->language = ff_mov_iso639_to_lang(lang?lang->value:"und", mov->mode!=MODE_MOV);
2192
if (track->language < 0)
2193
track->language = 0;
2194
track->mode = mov->mode;
2195
track->tag = mov_find_codec_tag(s, track);
2197
av_log(s, AV_LOG_ERROR, "track %d: could not find tag, "
2198
"codec not currently supported in container\n", i);
2201
/* If hinting of this track is enabled by a later hint track,
2202
* this is updated. */
2203
track->hint_track = -1;
2204
if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO){
2205
if (track->tag == MKTAG('m','x','3','p') || track->tag == MKTAG('m','x','3','n') ||
2206
track->tag == MKTAG('m','x','4','p') || track->tag == MKTAG('m','x','4','n') ||
2207
track->tag == MKTAG('m','x','5','p') || track->tag == MKTAG('m','x','5','n')) {
2208
if (st->codec->width != 720 || (st->codec->height != 608 && st->codec->height != 512)) {
2209
av_log(s, AV_LOG_ERROR, "D-10/IMX must use 720x608 or 720x512 video resolution\n");
2212
track->height = track->tag>>24 == 'n' ? 486 : 576;
2214
track->timescale = st->codec->time_base.den;
2215
if (track->mode == MODE_MOV && track->timescale > 100000)
2216
av_log(s, AV_LOG_WARNING,
2217
"WARNING codec timebase is very high. If duration is too long,\n"
2218
"file may not be playable by quicktime. Specify a shorter timebase\n"
2219
"or choose different container.\n");
2220
}else if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO){
2221
track->timescale = st->codec->sample_rate;
2222
if(!st->codec->frame_size && !av_get_bits_per_sample(st->codec->codec_id)) {
2223
av_log(s, AV_LOG_ERROR, "track %d: codec frame size is not set\n", i);
2225
}else if(st->codec->codec_id == CODEC_ID_ADPCM_MS ||
2226
st->codec->codec_id == CODEC_ID_ADPCM_IMA_WAV){
2227
if (!st->codec->block_align) {
2228
av_log(s, AV_LOG_ERROR, "track %d: codec block align is not set for adpcm\n", i);
2231
track->sampleSize = st->codec->block_align;
2232
}else if(st->codec->frame_size > 1){ /* assume compressed audio */
2233
track->audio_vbr = 1;
2235
st->codec->frame_size = 1;
2236
track->sampleSize = (av_get_bits_per_sample(st->codec->codec_id) >> 3) * st->codec->channels;
2238
if (track->mode != MODE_MOV) {
2239
if (track->timescale > UINT16_MAX) {
2240
av_log(s, AV_LOG_ERROR, "track %d: output format does not support "
2241
"sample rate %dhz\n", i, track->timescale);
2244
if (track->enc->codec_id == CODEC_ID_MP3 && track->timescale < 16000) {
2245
av_log(s, AV_LOG_ERROR, "track %d: muxing mp3 at %dhz is not supported\n",
2246
i, track->enc->sample_rate);
2250
}else if(st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE){
2251
track->timescale = st->codec->time_base.den;
2254
track->height = st->codec->height;
2256
av_set_pts_info(st, 64, 1, track->timescale);
2259
mov_write_mdat_tag(pb, mov);
2260
mov->time = s->timestamp + 0x7C25B080; //1970 based -> 1904 based
2262
if (mov->chapter_track)
2263
mov_create_chapter_track(s, mov->chapter_track);
2265
if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
2266
/* Initialize the hint tracks for each audio and video stream */
2267
for (i = 0; i < s->nb_streams; i++) {
2268
AVStream *st = s->streams[i];
2269
if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
2270
st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
2271
ff_mov_init_hinting(s, hint_track, i);
2281
av_freep(&mov->tracks);
2285
static int mov_write_trailer(AVFormatContext *s)
2287
MOVMuxContext *mov = s->priv_data;
2288
AVIOContext *pb = s->pb;
2292
int64_t moov_pos = avio_tell(pb);
2294
/* Write size of mdat tag */
2295
if (mov->mdat_size+8 <= UINT32_MAX) {
2296
avio_seek(pb, mov->mdat_pos, SEEK_SET);
2297
avio_wb32(pb, mov->mdat_size+8);
2299
/* overwrite 'wide' placeholder atom */
2300
avio_seek(pb, mov->mdat_pos - 8, SEEK_SET);
2301
avio_wb32(pb, 1); /* special value: real atom size will be 64 bit value after tag field */
2302
ffio_wfourcc(pb, "mdat");
2303
avio_wb64(pb, mov->mdat_size+16);
2305
avio_seek(pb, moov_pos, SEEK_SET);
2307
mov_write_moov_tag(pb, mov, s);
2309
if (mov->chapter_track)
2310
av_freep(&mov->tracks[mov->chapter_track].enc);
2312
for (i=0; i<mov->nb_streams; i++) {
2313
if (mov->tracks[i].tag == MKTAG('r','t','p',' '))
2314
ff_mov_close_hinting(&mov->tracks[i]);
2315
av_freep(&mov->tracks[i].cluster);
2317
if(mov->tracks[i].vosLen) av_free(mov->tracks[i].vosData);
2323
av_freep(&mov->tracks);
2328
#if CONFIG_MOV_MUXER
2329
AVOutputFormat ff_mov_muxer = {
2331
NULL_IF_CONFIG_SMALL("MOV format"),
2334
sizeof(MOVMuxContext),
2338
ff_mov_write_packet,
2340
.flags = AVFMT_GLOBALHEADER,
2341
.codec_tag = (const AVCodecTag* const []){codec_movvideo_tags, codec_movaudio_tags, 0},
2342
.priv_class = &mov_muxer_class,
2345
#if CONFIG_TGP_MUXER
2346
AVOutputFormat ff_tgp_muxer = {
2348
NULL_IF_CONFIG_SMALL("3GP format"),
2351
sizeof(MOVMuxContext),
2355
ff_mov_write_packet,
2357
.flags = AVFMT_GLOBALHEADER,
2358
.codec_tag = (const AVCodecTag* const []){codec_3gp_tags, 0},
2359
.priv_class = &mov_muxer_class,
2362
#if CONFIG_MP4_MUXER
2363
AVOutputFormat ff_mp4_muxer = {
2365
NULL_IF_CONFIG_SMALL("MP4 format"),
2368
sizeof(MOVMuxContext),
2372
ff_mov_write_packet,
2374
.flags = AVFMT_GLOBALHEADER,
2375
.codec_tag = (const AVCodecTag* const []){ff_mp4_obj_type, 0},
2376
.priv_class = &mov_muxer_class,
2379
#if CONFIG_PSP_MUXER
2380
AVOutputFormat ff_psp_muxer = {
2382
NULL_IF_CONFIG_SMALL("PSP MP4 format"),
2385
sizeof(MOVMuxContext),
2389
ff_mov_write_packet,
2391
.flags = AVFMT_GLOBALHEADER,
2392
.codec_tag = (const AVCodecTag* const []){ff_mp4_obj_type, 0},
2393
.priv_class = &mov_muxer_class,
2396
#if CONFIG_TG2_MUXER
2397
AVOutputFormat ff_tg2_muxer = {
2399
NULL_IF_CONFIG_SMALL("3GP2 format"),
2402
sizeof(MOVMuxContext),
2406
ff_mov_write_packet,
2408
.flags = AVFMT_GLOBALHEADER,
2409
.codec_tag = (const AVCodecTag* const []){codec_3gp_tags, 0},
2410
.priv_class = &mov_muxer_class,
2413
#if CONFIG_IPOD_MUXER
2414
AVOutputFormat ff_ipod_muxer = {
2416
NULL_IF_CONFIG_SMALL("iPod H.264 MP4 format"),
2419
sizeof(MOVMuxContext),
2423
ff_mov_write_packet,
2425
.flags = AVFMT_GLOBALHEADER,
2426
.codec_tag = (const AVCodecTag* const []){codec_ipod_tags, 0},
2427
.priv_class = &mov_muxer_class,