avconv: Don't split out inline sequence headers when stream copying VC1
[libav.git] / libavformat / movenc.c
CommitLineData
1cb5f7fd 1/*
7fbde343 2 * MOV, 3GP, MP4 muxer
406792e7
DB
3 * Copyright (c) 2003 Thomas Raivio
4 * Copyright (c) 2004 Gildas Bazin <gbazin at videolan dot org>
7c4502c8 5 * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
1cb5f7fd 6 *
2912e87a 7 * This file is part of Libav.
b78e7197 8 *
2912e87a 9 * Libav is free software; you can redistribute it and/or
1cb5f7fd
MN
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
b78e7197 12 * version 2.1 of the License, or (at your option) any later version.
1cb5f7fd 13 *
2912e87a 14 * Libav is distributed in the hope that it will be useful,
1cb5f7fd
MN
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
2912e87a 20 * License along with Libav; if not, write to the Free Software
5509bffa 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1cb5f7fd 22 */
4909e517 23
f72dad41 24#include "movenc.h"
1cb5f7fd 25#include "avformat.h"
0abdb293 26#include "avio_internal.h"
9d9f4119 27#include "riff.h"
1cb5f7fd 28#include "avio.h"
e40ee6a2 29#include "isom.h"
1bd2d763 30#include "avc.h"
9106a698 31#include "libavcodec/get_bits.h"
b2755007 32#include "libavcodec/put_bits.h"
e977af6f
MS
33#include "internal.h"
34#include "libavutil/avstring.h"
3383a53e 35#include "libavutil/intfloat.h"
0ebcdf5c 36#include "libavutil/mathematics.h"
91e3a25e 37#include "libavutil/opt.h"
d2d67e42 38#include "libavutil/dict.h"
d16cccac 39#include "rtpenc.h"
1fdf18f4 40#include "mov_chan.h"
1cb5f7fd 41
6e6d6dc0
MN
42#undef NDEBUG
43#include <assert.h>
44
91e3a25e 45static const AVOption options[] = {
145f741e
AK
46 { "movflags", "MOV muxer flags", offsetof(MOVMuxContext, flags), AV_OPT_TYPE_FLAGS, {.dbl = 0}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
47 { "rtphint", "Add RTP hint tracks", 0, AV_OPT_TYPE_CONST, {.dbl = FF_MOV_FLAG_RTP_HINT}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
83988d58
MS
48 { "empty_moov", "Make the initial moov atom empty (not supported by QuickTime)", 0, AV_OPT_TYPE_CONST, {.dbl = FF_MOV_FLAG_EMPTY_MOOV}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
49 { "frag_keyframe", "Fragment at video keyframes", 0, AV_OPT_TYPE_CONST, {.dbl = FF_MOV_FLAG_FRAG_KEYFRAME}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
50 { "separate_moof", "Write separate moof/mdat atoms for each track", 0, AV_OPT_TYPE_CONST, {.dbl = FF_MOV_FLAG_SEPARATE_MOOF}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
b613ff5e 51 { "frag_custom", "Flush fragments on caller requests", 0, AV_OPT_TYPE_CONST, {.dbl = FF_MOV_FLAG_FRAG_CUSTOM}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
f5322104 52 { "isml", "Create a live smooth streaming feed (for pushing to a publishing point)", 0, AV_OPT_TYPE_CONST, {.dbl = FF_MOV_FLAG_ISML}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
d16cccac 53 FF_RTP_FLAG_OPTS(MOVMuxContext, rtp_flags),
d3b8bde2
AC
54 { "skip_iods", "Skip writing iods atom.", offsetof(MOVMuxContext, iods_skip), AV_OPT_TYPE_INT, {.dbl = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
55 { "iods_audio_profile", "iods audio profile atom.", offsetof(MOVMuxContext, iods_audio_profile), AV_OPT_TYPE_INT, {.dbl = -1}, -1, 255, AV_OPT_FLAG_ENCODING_PARAM},
56 { "iods_video_profile", "iods video profile atom.", offsetof(MOVMuxContext, iods_video_profile), AV_OPT_TYPE_INT, {.dbl = -1}, -1, 255, AV_OPT_FLAG_ENCODING_PARAM},
83988d58
MS
57 { "frag_duration", "Maximum fragment duration", offsetof(MOVMuxContext, max_fragment_duration), AV_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
58 { "frag_size", "Maximum fragment size", offsetof(MOVMuxContext, max_fragment_size), AV_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
4ddd54da 59 { "ism_lookahead", "Number of lookahead entries for ISM files", offsetof(MOVMuxContext, ism_lookahead), AV_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
91e3a25e
MS
60 { NULL },
61};
62
a4ea00d0
AK
63#define MOV_CLASS(flavor)\
64static const AVClass flavor ## _muxer_class = {\
65 .class_name = #flavor " muxer",\
66 .item_name = av_default_item_name,\
67 .option = options,\
68 .version = LIBAVUTIL_VERSION_INT,\
91e3a25e
MS
69};
70
90b5b51e 71//FIXME support 64 bit variant with wide placeholders
b7d3dd90 72static int64_t update_size(AVIOContext *pb, int64_t pos)
1cb5f7fd 73{
a2704c97 74 int64_t curpos = avio_tell(pb);
6b4aa5da 75 avio_seek(pb, pos, SEEK_SET);
77eb5504 76 avio_wb32(pb, curpos - pos); /* rewrite size */
6b4aa5da 77 avio_seek(pb, curpos, SEEK_SET);
6e6d6dc0
MN
78
79 return curpos - pos;
1cb5f7fd
MN
80}
81
e45ccf79 82/* Chunk offset atom */
ae628ec1 83static int mov_write_stco_tag(AVIOContext *pb, MOVTrack *track)
1cb5f7fd
MN
84{
85 int i;
ed06cf6d 86 int mode64 = 0; // use 32 bit size variant if possible
a2704c97 87 int64_t pos = avio_tell(pb);
77eb5504 88 avio_wb32(pb, 0); /* size */
b29af723
MN
89 if (pos > UINT32_MAX) {
90 mode64 = 1;
0abdb293 91 ffio_wfourcc(pb, "co64");
b29af723 92 } else
0abdb293 93 ffio_wfourcc(pb, "stco");
77eb5504
AK
94 avio_wb32(pb, 0); /* version & flags */
95 avio_wb32(pb, track->entry); /* entry count */
1cb5f7fd 96 for (i=0; i<track->entry; i++) {
b29af723 97 if(mode64 == 1)
77eb5504 98 avio_wb64(pb, track->cluster[i].pos);
b29af723 99 else
77eb5504 100 avio_wb32(pb, track->cluster[i].pos);
1cb5f7fd 101 }
b7d3dd90 102 return update_size(pb, pos);
1cb5f7fd
MN
103}
104
e45ccf79 105/* Sample size atom */
ae628ec1 106static int mov_write_stsz_tag(AVIOContext *pb, MOVTrack *track)
1cb5f7fd 107{
f578f938 108 int equalChunks = 1;
e45ccf79 109 int i, j, entries = 0, tst = -1, oldtst = -1;
1cb5f7fd 110
a2704c97 111 int64_t pos = avio_tell(pb);
77eb5504 112 avio_wb32(pb, 0); /* size */
0abdb293 113 ffio_wfourcc(pb, "stsz");
77eb5504 114 avio_wb32(pb, 0); /* version & flags */
1cb5f7fd 115
f578f938 116 for (i=0; i<track->entry; i++) {
0aec3c5c 117 tst = track->cluster[i].size/track->cluster[i].entries;
e45ccf79
GB
118 if(oldtst != -1 && tst != oldtst) {
119 equalChunks = 0;
f578f938
TR
120 }
121 oldtst = tst;
0aec3c5c 122 entries += track->cluster[i].entries;
f578f938 123 }
83988d58
MS
124 if (equalChunks && track->entry) {
125 int sSize = track->entry ? track->cluster[0].size/track->cluster[0].entries : 0;
7fe3207e 126 sSize = FFMAX(1, sSize); // adpcm mono case could make sSize == 0
77eb5504
AK
127 avio_wb32(pb, sSize); // sample size
128 avio_wb32(pb, entries); // sample count
1cb5f7fd 129 }
f578f938 130 else {
77eb5504
AK
131 avio_wb32(pb, 0); // sample size
132 avio_wb32(pb, entries); // sample count
f578f938 133 for (i=0; i<track->entry; i++) {
91208916 134 for (j=0; j<track->cluster[i].entries; j++) {
77eb5504 135 avio_wb32(pb, track->cluster[i].size /
0aec3c5c 136 track->cluster[i].entries);
e45ccf79 137 }
1cb5f7fd
MN
138 }
139 }
b7d3dd90 140 return update_size(pb, pos);
1cb5f7fd
MN
141}
142
e45ccf79 143/* Sample to chunk atom */
ae628ec1 144static int mov_write_stsc_tag(AVIOContext *pb, MOVTrack *track)
1cb5f7fd 145{
b29af723 146 int index = 0, oldval = -1, i;
bc5c918e 147 int64_t entryPos, curpos;
f578f938 148
a2704c97 149 int64_t pos = avio_tell(pb);
77eb5504 150 avio_wb32(pb, 0); /* size */
0abdb293 151 ffio_wfourcc(pb, "stsc");
77eb5504 152 avio_wb32(pb, 0); // version & flags
a2704c97 153 entryPos = avio_tell(pb);
77eb5504 154 avio_wb32(pb, track->entry); // entry count
f578f938 155 for (i=0; i<track->entry; i++) {
b7d3dd90 156 if (oldval != track->cluster[i].samples_in_chunk)
f578f938 157 {
77eb5504 158 avio_wb32(pb, i+1); // first chunk
b7d3dd90 159 avio_wb32(pb, track->cluster[i].samples_in_chunk); // samples per chunk
77eb5504 160 avio_wb32(pb, 0x1); // sample description index
b7d3dd90 161 oldval = track->cluster[i].samples_in_chunk;
f578f938 162 index++;
1cb5f7fd
MN
163 }
164 }
a2704c97 165 curpos = avio_tell(pb);
6b4aa5da 166 avio_seek(pb, entryPos, SEEK_SET);
77eb5504 167 avio_wb32(pb, index); // rewrite size
6b4aa5da 168 avio_seek(pb, curpos, SEEK_SET);
1cb5f7fd 169
b7d3dd90 170 return update_size(pb, pos);
1cb5f7fd
MN
171}
172
e45ccf79 173/* Sync sample atom */
ae628ec1 174static int mov_write_stss_tag(AVIOContext *pb, MOVTrack *track, uint32_t flag)
1cb5f7fd 175{
bc5c918e 176 int64_t curpos, entryPos;
b29af723 177 int i, index = 0;
a2704c97 178 int64_t pos = avio_tell(pb);
77eb5504 179 avio_wb32(pb, 0); // size
0abdb293 180 ffio_wfourcc(pb, flag == MOV_SYNC_SAMPLE ? "stss" : "stps");
77eb5504 181 avio_wb32(pb, 0); // version & flags
a2704c97 182 entryPos = avio_tell(pb);
77eb5504 183 avio_wb32(pb, track->entry); // entry count
f578f938 184 for (i=0; i<track->entry; i++) {
e1316b19 185 if (track->cluster[i].flags & flag) {
77eb5504 186 avio_wb32(pb, i+1);
f578f938
TR
187 index++;
188 }
189 }
a2704c97 190 curpos = avio_tell(pb);
6b4aa5da 191 avio_seek(pb, entryPos, SEEK_SET);
77eb5504 192 avio_wb32(pb, index); // rewrite size
6b4aa5da 193 avio_seek(pb, curpos, SEEK_SET);
b7d3dd90 194 return update_size(pb, pos);
1cb5f7fd
MN
195}
196
ae628ec1 197static int mov_write_amr_tag(AVIOContext *pb, MOVTrack *track)
1cb5f7fd 198{
77eb5504 199 avio_wb32(pb, 0x11); /* size */
0abdb293
AK
200 if (track->mode == MODE_MOV) ffio_wfourcc(pb, "samr");
201 else ffio_wfourcc(pb, "damr");
202 ffio_wfourcc(pb, "FFMP");
77eb5504 203 avio_w8(pb, 0); /* decoder version */
3a72cbd9 204
77eb5504
AK
205 avio_wb16(pb, 0x81FF); /* Mode set (all modes for AMR_NB) */
206 avio_w8(pb, 0x00); /* Mode change period (no restriction) */
207 avio_w8(pb, 0x01); /* Frames per sample */
3a72cbd9
BC
208 return 0x11;
209}
210
ae628ec1 211static int mov_write_ac3_tag(AVIOContext *pb, MOVTrack *track)
d4a240cb
BC
212{
213 GetBitContext gbc;
214 PutBitContext pbc;
215 uint8_t buf[3];
216 int fscod, bsid, bsmod, acmod, lfeon, frmsizecod;
217
b7d3dd90 218 if (track->vos_len < 7)
d4a240cb
BC
219 return -1;
220
77eb5504 221 avio_wb32(pb, 11);
0abdb293 222 ffio_wfourcc(pb, "dac3");
d4a240cb 223
b7d3dd90 224 init_get_bits(&gbc, track->vos_data + 4, (track->vos_len - 4) * 8);
d4a240cb
BC
225 fscod = get_bits(&gbc, 2);
226 frmsizecod = get_bits(&gbc, 6);
227 bsid = get_bits(&gbc, 5);
228 bsmod = get_bits(&gbc, 3);
229 acmod = get_bits(&gbc, 3);
230 if (acmod == 2) {
231 skip_bits(&gbc, 2); // dsurmod
232 } else {
233 if ((acmod & 1) && acmod != 1)
234 skip_bits(&gbc, 2); // cmixlev
235 if (acmod & 4)
236 skip_bits(&gbc, 2); // surmixlev
237 }
238 lfeon = get_bits1(&gbc);
239
240 init_put_bits(&pbc, buf, sizeof(buf));
241 put_bits(&pbc, 2, fscod);
242 put_bits(&pbc, 5, bsid);
243 put_bits(&pbc, 3, bsmod);
244 put_bits(&pbc, 3, acmod);
245 put_bits(&pbc, 1, lfeon);
246 put_bits(&pbc, 5, frmsizecod>>1); // bit_rate_code
247 put_bits(&pbc, 5, 0); // reserved
248
249 flush_put_bits(&pbc);
77eb5504 250 avio_write(pb, buf, sizeof(buf));
d4a240cb
BC
251
252 return 11;
253}
254
463e7afd
BC
255/**
256 * This function writes extradata "as is".
da9cea77 257 * Extradata must be formatted like a valid atom (with size and tag).
463e7afd 258 */
ae628ec1 259static int mov_write_extradata_tag(AVIOContext *pb, MOVTrack *track)
463e7afd 260{
77eb5504 261 avio_write(pb, track->enc->extradata, track->enc->extradata_size);
463e7afd
BC
262 return track->enc->extradata_size;
263}
264
ae628ec1 265static int mov_write_enda_tag(AVIOContext *pb)
fcef991a 266{
77eb5504 267 avio_wb32(pb, 10);
0abdb293 268 ffio_wfourcc(pb, "enda");
77eb5504 269 avio_wb16(pb, 1); /* little endian */
fcef991a
BC
270 return 10;
271}
272
b7d3dd90 273static void put_descr(AVIOContext *pb, int tag, unsigned int size)
039627cf 274{
7e19a6e8 275 int i = 3;
77eb5504 276 avio_w8(pb, tag);
08db8f18 277 for(; i>0; i--)
77eb5504
AK
278 avio_w8(pb, (size>>(7*i)) | 0x80);
279 avio_w8(pb, size & 0x7F);
039627cf
BC
280}
281
ae628ec1 282static int mov_write_esds_tag(AVIOContext *pb, MOVTrack *track) // Basic
039627cf 283{
a2704c97 284 int64_t pos = avio_tell(pb);
b7d3dd90 285 int decoder_specific_info_len = track->vos_len ? 5 + track->vos_len : 0;
039627cf 286
77eb5504 287 avio_wb32(pb, 0); // size
0abdb293 288 ffio_wfourcc(pb, "esds");
77eb5504 289 avio_wb32(pb, 0); // Version
039627cf
BC
290
291 // ES descriptor
b7d3dd90
MS
292 put_descr(pb, 0x03, 3 + 5+13 + decoder_specific_info_len + 5+1);
293 avio_wb16(pb, track->track_id);
77eb5504 294 avio_w8(pb, 0x00); // flags (= no flags)
039627cf
BC
295
296 // DecoderConfig descriptor
b7d3dd90 297 put_descr(pb, 0x04, 13 + decoder_specific_info_len);
039627cf
BC
298
299 // Object type indication
dfce888f
BC
300 if ((track->enc->codec_id == CODEC_ID_MP2 ||
301 track->enc->codec_id == CODEC_ID_MP3) &&
c4e02d70 302 track->enc->sample_rate > 24000)
77eb5504 303 avio_w8(pb, 0x6B); // 11172-3
dfce888f 304 else
77eb5504 305 avio_w8(pb, ff_codec_get_tag(ff_mp4_obj_type, track->enc->codec_id));
039627cf
BC
306
307 // the following fields is made of 6 bits to identify the streamtype (4 for video, 5 for audio)
308 // plus 1 bit to indicate upstream and 1 bit set to 1 (reserved)
72415b2a 309 if(track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
77eb5504 310 avio_w8(pb, 0x15); // flags (= Audiostream)
039627cf 311 else
77eb5504 312 avio_w8(pb, 0x11); // flags (= Visualstream)
039627cf 313
77eb5504
AK
314 avio_w8(pb, track->enc->rc_buffer_size>>(3+16)); // Buffersize DB (24 bits)
315 avio_wb16(pb, (track->enc->rc_buffer_size>>3)&0xFFFF); // Buffersize DB
039627cf 316
77eb5504 317 avio_wb32(pb, FFMAX(track->enc->bit_rate, track->enc->rc_max_rate)); // maxbitrate (FIXME should be max rate in any 1 sec window)
039627cf 318 if(track->enc->rc_max_rate != track->enc->rc_min_rate || track->enc->rc_min_rate==0)
77eb5504 319 avio_wb32(pb, 0); // vbr
039627cf 320 else
77eb5504 321 avio_wb32(pb, track->enc->rc_max_rate); // avg bitrate
039627cf 322
b7d3dd90 323 if (track->vos_len) {
039627cf 324 // DecoderSpecific info descriptor
b7d3dd90
MS
325 put_descr(pb, 0x05, track->vos_len);
326 avio_write(pb, track->vos_data, track->vos_len);
039627cf
BC
327 }
328
039627cf 329 // SL descriptor
b7d3dd90 330 put_descr(pb, 0x06, 1);
77eb5504 331 avio_w8(pb, 0x02);
b7d3dd90 332 return update_size(pb, pos);
039627cf
BC
333}
334
7c4b7d0f
BC
335static int mov_pcm_le_gt16(enum CodecID codec_id)
336{
337 return codec_id == CODEC_ID_PCM_S24LE ||
338 codec_id == CODEC_ID_PCM_S32LE ||
339 codec_id == CODEC_ID_PCM_F32LE ||
340 codec_id == CODEC_ID_PCM_F64LE;
341}
342
ae628ec1 343static int mov_write_ms_tag(AVIOContext *pb, MOVTrack *track)
f2589642 344{
a2704c97 345 int64_t pos = avio_tell(pb);
77eb5504
AK
346 avio_wb32(pb, 0);
347 avio_wl32(pb, track->tag); // store it byteswapped
795ed278 348 track->enc->codec_tag = av_bswap16(track->tag >> 16);
f2589642 349 ff_put_wav_header(pb, track->enc);
b7d3dd90 350 return update_size(pb, pos);
f2589642
BC
351}
352
5d561514
MS
353static int mov_write_wfex_tag(AVIOContext *pb, MOVTrack *track)
354{
355 int64_t pos = avio_tell(pb);
356 avio_wb32(pb, 0);
357 ffio_wfourcc(pb, "wfex");
358 ff_put_wav_header(pb, track->enc);
359 return update_size(pb, pos);
360}
361
1fdf18f4
JR
362static int mov_write_chan_tag(AVIOContext *pb, MOVTrack *track)
363{
364 uint32_t layout_tag, bitmap;
365 int64_t pos = avio_tell(pb);
366
367 layout_tag = ff_mov_get_channel_layout_tag(track->enc->codec_id,
368 track->enc->channel_layout,
369 &bitmap);
370 if (!layout_tag) {
371 av_log(track->enc, AV_LOG_WARNING, "not writing 'chan' tag due to "
372 "lack of channel information\n");
373 return 0;
374 }
375
376 avio_wb32(pb, 0); // Size
377 ffio_wfourcc(pb, "chan"); // Type
378 avio_w8(pb, 0); // Version
379 avio_wb24(pb, 0); // Flags
380 avio_wb32(pb, layout_tag); // mChannelLayoutTag
381 avio_wb32(pb, bitmap); // mChannelBitmap
382 avio_wb32(pb, 0); // mNumberChannelDescriptions
383
b7d3dd90 384 return update_size(pb, pos);
1fdf18f4
JR
385}
386
ae628ec1 387static int mov_write_wave_tag(AVIOContext *pb, MOVTrack *track)
69dde1ad 388{
a2704c97 389 int64_t pos = avio_tell(pb);
69dde1ad 390
77eb5504 391 avio_wb32(pb, 0); /* size */
0abdb293 392 ffio_wfourcc(pb, "wave");
69dde1ad 393
77eb5504 394 avio_wb32(pb, 12); /* size */
0abdb293 395 ffio_wfourcc(pb, "frma");
77eb5504 396 avio_wl32(pb, track->tag);
69dde1ad 397
fcef991a 398 if (track->enc->codec_id == CODEC_ID_AAC) {
aa1c1c61 399 /* useless atom needed by mplayer, ipod, not needed by quicktime */
77eb5504 400 avio_wb32(pb, 12); /* size */
0abdb293 401 ffio_wfourcc(pb, "mp4a");
77eb5504 402 avio_wb32(pb, 0);
fcef991a 403 mov_write_esds_tag(pb, track);
7c4b7d0f 404 } else if (mov_pcm_le_gt16(track->enc->codec_id)) {
fcef991a 405 mov_write_enda_tag(pb);
3a72cbd9 406 } else if (track->enc->codec_id == CODEC_ID_AMR_NB) {
b7d9da10 407 mov_write_amr_tag(pb, track);
d4a240cb 408 } else if (track->enc->codec_id == CODEC_ID_AC3) {
60cd802d 409 mov_write_chan_tag(pb, track);
d4a240cb 410 mov_write_ac3_tag(pb, track);
463e7afd
BC
411 } else if (track->enc->codec_id == CODEC_ID_ALAC) {
412 mov_write_extradata_tag(pb, track);
f2589642
BC
413 } else if (track->enc->codec_id == CODEC_ID_ADPCM_MS ||
414 track->enc->codec_id == CODEC_ID_ADPCM_IMA_WAV) {
415 mov_write_ms_tag(pb, track);
fcef991a 416 }
69dde1ad 417
77eb5504
AK
418 avio_wb32(pb, 8); /* size */
419 avio_wb32(pb, 0); /* null tag */
69dde1ad 420
b7d3dd90 421 return update_size(pb, pos);
69dde1ad
GB
422}
423
ae628ec1 424static int mov_write_glbl_tag(AVIOContext *pb, MOVTrack *track)
79e42311 425{
b7d3dd90 426 avio_wb32(pb, track->vos_len + 8);
0abdb293 427 ffio_wfourcc(pb, "glbl");
b7d3dd90
MS
428 avio_write(pb, track->vos_data, track->vos_len);
429 return 8 + track->vos_len;
79e42311
BC
430}
431
feaa8d11
BC
432/**
433 * Compute flags for 'lpcm' tag.
434 * See CoreAudioTypes and AudioStreamBasicDescription at Apple.
435 */
436static int mov_get_lpcm_flags(enum CodecID codec_id)
437{
438 switch (codec_id) {
439 case CODEC_ID_PCM_F32BE:
440 case CODEC_ID_PCM_F64BE:
441 return 11;
442 case CODEC_ID_PCM_F32LE:
443 case CODEC_ID_PCM_F64LE:
444 return 9;
445 case CODEC_ID_PCM_U8:
446 return 10;
447 case CODEC_ID_PCM_S16BE:
448 case CODEC_ID_PCM_S24BE:
449 case CODEC_ID_PCM_S32BE:
450 return 14;
451 case CODEC_ID_PCM_S8:
452 case CODEC_ID_PCM_S16LE:
453 case CODEC_ID_PCM_S24LE:
454 case CODEC_ID_PCM_S32LE:
455 return 12;
456 default:
457 return 0;
458 }
459}
460
ae628ec1 461static int mov_write_audio_tag(AVIOContext *pb, MOVTrack *track)
1cb5f7fd 462{
a2704c97 463 int64_t pos = avio_tell(pb);
feaa8d11
BC
464 int version = 0;
465 uint32_t tag = track->tag;
466
467 if (track->mode == MODE_MOV) {
8e8c5131
JR
468 if (mov_get_lpcm_flags(track->enc->codec_id))
469 tag = AV_RL32("lpcm");
470 version = 2;
feaa8d11 471 }
115329f1 472
77eb5504
AK
473 avio_wb32(pb, 0); /* size */
474 avio_wl32(pb, tag); // store it byteswapped
475 avio_wb32(pb, 0); /* Reserved */
476 avio_wb16(pb, 0); /* Reserved */
477 avio_wb16(pb, 1); /* Data-reference index, XXX == 1 */
69dde1ad 478
e45ccf79 479 /* SoundDescription */
77eb5504
AK
480 avio_wb16(pb, version); /* Version */
481 avio_wb16(pb, 0); /* Revision level */
482 avio_wb32(pb, 0); /* Reserved */
1cb5f7fd 483
feaa8d11 484 if (version == 2) {
77eb5504
AK
485 avio_wb16(pb, 3);
486 avio_wb16(pb, 16);
487 avio_wb16(pb, 0xfffe);
488 avio_wb16(pb, 0);
489 avio_wb32(pb, 0x00010000);
490 avio_wb32(pb, 72);
3b5d4428 491 avio_wb64(pb, av_double2int(track->enc->sample_rate));
77eb5504
AK
492 avio_wb32(pb, track->enc->channels);
493 avio_wb32(pb, 0x7F000000);
494 avio_wb32(pb, av_get_bits_per_sample(track->enc->codec_id));
495 avio_wb32(pb, mov_get_lpcm_flags(track->enc->codec_id));
b7d3dd90 496 avio_wb32(pb, track->sample_size);
8e8c5131 497 avio_wb32(pb, track->audio_vbr ? track->enc->frame_size : 1);
feaa8d11 498 } else {
8e8c5131
JR
499 /* reserved for mp4/3gp */
500 avio_wb16(pb, 2);
501 avio_wb16(pb, 16);
502 avio_wb16(pb, 0);
ef19c7eb 503
77eb5504 504 avio_wb16(pb, 0); /* packet size (= 0) */
3b5d4428 505 avio_wb16(pb, track->enc->sample_rate);
77eb5504 506 avio_wb16(pb, 0); /* Reserved */
feaa8d11 507 }
1cb5f7fd 508
6dd19fff
BC
509 if(track->mode == MODE_MOV &&
510 (track->enc->codec_id == CODEC_ID_AAC ||
4ed19420 511 track->enc->codec_id == CODEC_ID_AC3 ||
6dd19fff 512 track->enc->codec_id == CODEC_ID_AMR_NB ||
7c4b7d0f 513 track->enc->codec_id == CODEC_ID_ALAC ||
f2589642
BC
514 track->enc->codec_id == CODEC_ID_ADPCM_MS ||
515 track->enc->codec_id == CODEC_ID_ADPCM_IMA_WAV ||
7c4b7d0f 516 mov_pcm_le_gt16(track->enc->codec_id)))
fcef991a 517 mov_write_wave_tag(pb, track);
d0c0a29b 518 else if(track->tag == MKTAG('m','p','4','a'))
3a72cbd9
BC
519 mov_write_esds_tag(pb, track);
520 else if(track->enc->codec_id == CODEC_ID_AMR_NB)
b7d9da10 521 mov_write_amr_tag(pb, track);
4ed19420
K
522 else if(track->enc->codec_id == CODEC_ID_AC3)
523 mov_write_ac3_tag(pb, track);
efa1fb39 524 else if(track->enc->codec_id == CODEC_ID_ALAC)
709c9f8d 525 mov_write_extradata_tag(pb, track);
5d561514
MS
526 else if (track->enc->codec_id == CODEC_ID_WMAPRO)
527 mov_write_wfex_tag(pb, track);
b7d3dd90 528 else if (track->vos_len > 0)
79e42311 529 mov_write_glbl_tag(pb, track);
3a72cbd9 530
b7d3dd90 531 return update_size(pb, pos);
1cb5f7fd
MN
532}
533
ae628ec1 534static int mov_write_d263_tag(AVIOContext *pb)
1cb5f7fd 535{
77eb5504 536 avio_wb32(pb, 0xf); /* size */
0abdb293
AK
537 ffio_wfourcc(pb, "d263");
538 ffio_wfourcc(pb, "FFMP");
77eb5504 539 avio_w8(pb, 0); /* decoder version */
87494ea0 540 /* FIXME use AVCodecContext level/profile, when encoder will set values */
77eb5504
AK
541 avio_w8(pb, 0xa); /* level */
542 avio_w8(pb, 0); /* profile */
1cb5f7fd
MN
543 return 0xf;
544}
545
f578f938 546/* TODO: No idea about these values */
ae628ec1 547static int mov_write_svq3_tag(AVIOContext *pb)
1cb5f7fd 548{
77eb5504 549 avio_wb32(pb, 0x15);
0abdb293
AK
550 ffio_wfourcc(pb, "SMI ");
551 ffio_wfourcc(pb, "SEQH");
77eb5504
AK
552 avio_wb32(pb, 0x5);
553 avio_wb32(pb, 0xe2c0211d);
554 avio_wb32(pb, 0xc0000000);
555 avio_w8(pb, 0);
f578f938 556 return 0x15;
1cb5f7fd
MN
557}
558
ae628ec1 559static int mov_write_avcc_tag(AVIOContext *pb, MOVTrack *track)
6e6eebf9 560{
a2704c97 561 int64_t pos = avio_tell(pb);
6e6eebf9 562
77eb5504 563 avio_wb32(pb, 0);
0abdb293 564 ffio_wfourcc(pb, "avcC");
b7d3dd90
MS
565 ff_isom_write_avcc(pb, track->vos_data, track->vos_len);
566 return update_size(pb, pos);
c1b8e6d8
BC
567}
568
fc4cbc16 569/* also used by all avid codecs (dv, imx, meridien) and their variants */
ae628ec1 570static int mov_write_avid_tag(AVIOContext *pb, MOVTrack *track)
fc4cbc16
BC
571{
572 int i;
77eb5504 573 avio_wb32(pb, 24); /* size */
0abdb293
AK
574 ffio_wfourcc(pb, "ACLR");
575 ffio_wfourcc(pb, "ACLR");
576 ffio_wfourcc(pb, "0001");
304e983d 577 avio_wb32(pb, 2); /* yuv range: full 1 / normal 2 */
77eb5504 578 avio_wb32(pb, 0); /* unknown */
fc4cbc16 579
77eb5504 580 avio_wb32(pb, 24); /* size */
0abdb293
AK
581 ffio_wfourcc(pb, "APRG");
582 ffio_wfourcc(pb, "APRG");
583 ffio_wfourcc(pb, "0001");
77eb5504
AK
584 avio_wb32(pb, 1); /* unknown */
585 avio_wb32(pb, 0); /* unknown */
fc4cbc16 586
77eb5504 587 avio_wb32(pb, 120); /* size */
0abdb293
AK
588 ffio_wfourcc(pb, "ARES");
589 ffio_wfourcc(pb, "ARES");
590 ffio_wfourcc(pb, "0001");
b7d3dd90 591 avio_wb32(pb, AV_RB32(track->vos_data + 0x28)); /* dnxhd cid, some id ? */
77eb5504 592 avio_wb32(pb, track->enc->width);
fc4cbc16 593 /* values below are based on samples created with quicktime and avid codecs */
b7d3dd90 594 if (track->vos_data[5] & 2) { // interlaced
77eb5504
AK
595 avio_wb32(pb, track->enc->height/2);
596 avio_wb32(pb, 2); /* unknown */
597 avio_wb32(pb, 0); /* unknown */
598 avio_wb32(pb, 4); /* unknown */
fc4cbc16 599 } else {
77eb5504
AK
600 avio_wb32(pb, track->enc->height);
601 avio_wb32(pb, 1); /* unknown */
602 avio_wb32(pb, 0); /* unknown */
44fb8ebb 603 if (track->enc->height == 1080)
77eb5504 604 avio_wb32(pb, 5); /* unknown */
44fb8ebb 605 else
77eb5504 606 avio_wb32(pb, 6); /* unknown */
fc4cbc16
BC
607 }
608 /* padding */
609 for (i = 0; i < 10; i++)
77eb5504 610 avio_wb64(pb, 0);
fc4cbc16
BC
611
612 /* extra padding for stsd needed */
77eb5504 613 avio_wb32(pb, 0);
fc4cbc16
BC
614 return 0;
615}
616
6b600285 617static int mp4_get_codec_tag(AVFormatContext *s, MOVTrack *track)
8e321619 618{
039627cf 619 int tag = track->enc->codec_tag;
2ab57c04 620
1a40491e 621 if (!ff_codec_get_tag(ff_mp4_obj_type, track->enc->codec_id))
2ab57c04
BC
622 return 0;
623
624 if (track->enc->codec_id == CODEC_ID_H264) tag = MKTAG('a','v','c','1');
625 else if (track->enc->codec_id == CODEC_ID_AC3) tag = MKTAG('a','c','-','3');
626 else if (track->enc->codec_id == CODEC_ID_DIRAC) tag = MKTAG('d','r','a','c');
627 else if (track->enc->codec_id == CODEC_ID_MOV_TEXT) tag = MKTAG('t','x','3','g');
72415b2a
SS
628 else if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) tag = MKTAG('m','p','4','v');
629 else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) tag = MKTAG('m','p','4','a');
6b600285
BC
630
631 return tag;
632}
633
3c8d1447
BC
634static const AVCodecTag codec_ipod_tags[] = {
635 { CODEC_ID_H264, MKTAG('a','v','c','1') },
636 { CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
637 { CODEC_ID_AAC, MKTAG('m','p','4','a') },
638 { CODEC_ID_ALAC, MKTAG('a','l','a','c') },
639 { CODEC_ID_AC3, MKTAG('a','c','-','3') },
640 { CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
641 { CODEC_ID_MOV_TEXT, MKTAG('t','e','x','t') },
642 { CODEC_ID_NONE, 0 },
643};
644
6b600285
BC
645static int ipod_get_codec_tag(AVFormatContext *s, MOVTrack *track)
646{
647 int tag = track->enc->codec_tag;
648
130ba4b1 649 // keep original tag for subs, ipod supports both formats
72415b2a 650 if (!(track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE &&
2ab57c04 651 (tag == MKTAG('t','x','3','g') ||
b6cb9946 652 tag == MKTAG('t','e','x','t'))))
1a40491e 653 tag = ff_codec_get_tag(codec_ipod_tags, track->enc->codec_id);
0872acc4 654
ade5b91d 655 if (!av_match_ext(s->filename, "m4a") && !av_match_ext(s->filename, "m4v"))
2ab57c04
BC
656 av_log(s, AV_LOG_WARNING, "Warning, extension is not .m4a nor .m4v "
657 "Quicktime/Ipod might not play the file\n");
6b600285
BC
658
659 return tag;
660}
661
662static int mov_get_dv_codec_tag(AVFormatContext *s, MOVTrack *track)
663{
664 int tag;
665
06ed4873
BC
666 if (track->enc->width == 720) /* SD */
667 if (track->enc->height == 480) /* NTSC */
668 if (track->enc->pix_fmt == PIX_FMT_YUV422P) tag = MKTAG('d','v','5','n');
669 else tag = MKTAG('d','v','c',' ');
670 else if (track->enc->pix_fmt == PIX_FMT_YUV422P) tag = MKTAG('d','v','5','p');
671 else if (track->enc->pix_fmt == PIX_FMT_YUV420P) tag = MKTAG('d','v','c','p');
672 else tag = MKTAG('d','v','p','p');
673 else if (track->enc->height == 720) /* HD 720 line */
674 if (track->enc->time_base.den == 50) tag = MKTAG('d','v','h','q');
675 else tag = MKTAG('d','v','h','p');
676 else if (track->enc->height == 1080) /* HD 1080 line */
677 if (track->enc->time_base.den == 25) tag = MKTAG('d','v','h','5');
678 else tag = MKTAG('d','v','h','6');
679 else {
680 av_log(s, AV_LOG_ERROR, "unsupported height for dv codec\n");
681 return 0;
682 }
6b600285
BC
683
684 return tag;
685}
686
3c8d1447
BC
687static const struct {
688 enum PixelFormat pix_fmt;
689 uint32_t tag;
690 unsigned bps;
691} mov_pix_fmt_tags[] = {
692 { PIX_FMT_YUYV422, MKTAG('y','u','v','s'), 0 },
693 { PIX_FMT_UYVY422, MKTAG('2','v','u','y'), 0 },
a7cc89e2 694 { PIX_FMT_RGB555BE,MKTAG('r','a','w',' '), 16 },
f65aad95
BC
695 { PIX_FMT_RGB555LE,MKTAG('L','5','5','5'), 16 },
696 { PIX_FMT_RGB565LE,MKTAG('L','5','6','5'), 16 },
697 { PIX_FMT_RGB565BE,MKTAG('B','5','6','5'), 16 },
9a191b3a 698 { PIX_FMT_GRAY16BE,MKTAG('b','1','6','g'), 16 },
3c8d1447 699 { PIX_FMT_RGB24, MKTAG('r','a','w',' '), 24 },
f65aad95 700 { PIX_FMT_BGR24, MKTAG('2','4','B','G'), 24 },
dfb0471f 701 { PIX_FMT_ARGB, MKTAG('r','a','w',' '), 32 },
5b50b8f5 702 { PIX_FMT_BGRA, MKTAG('B','G','R','A'), 32 },
ed88074a 703 { PIX_FMT_RGBA, MKTAG('R','G','B','A'), 32 },
a7cc89e2 704 { PIX_FMT_ABGR, MKTAG('A','B','G','R'), 32 },
9a191b3a 705 { PIX_FMT_RGB48BE, MKTAG('b','4','8','r'), 48 },
3c8d1447
BC
706};
707
6b600285
BC
708static int mov_get_rawvideo_codec_tag(AVFormatContext *s, MOVTrack *track)
709{
710 int tag = track->enc->codec_tag;
2ab57c04
BC
711 int i;
712
713 for (i = 0; i < FF_ARRAY_ELEMS(mov_pix_fmt_tags); i++) {
714 if (track->enc->pix_fmt == mov_pix_fmt_tags[i].pix_fmt) {
715 tag = mov_pix_fmt_tags[i].tag;
716 track->enc->bits_per_coded_sample = mov_pix_fmt_tags[i].bps;
717 break;
718 }
719 }
6b600285
BC
720
721 return tag;
722}
723
724static int mov_get_codec_tag(AVFormatContext *s, MOVTrack *track)
725{
726 int tag = track->enc->codec_tag;
727
728 if (!tag || (track->enc->strict_std_compliance >= FF_COMPLIANCE_NORMAL &&
a2b7ed32 729 (track->enc->codec_id == CODEC_ID_DVVIDEO ||
6b600285 730 track->enc->codec_id == CODEC_ID_RAWVIDEO ||
10d8eac9 731 track->enc->codec_id == CODEC_ID_H263 ||
6b600285 732 av_get_bits_per_sample(track->enc->codec_id)))) { // pcm audio
0872acc4 733 if (track->enc->codec_id == CODEC_ID_DVVIDEO)
6b600285 734 tag = mov_get_dv_codec_tag(s, track);
0872acc4 735 else if (track->enc->codec_id == CODEC_ID_RAWVIDEO)
6b600285 736 tag = mov_get_rawvideo_codec_tag(s, track);
72415b2a 737 else if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
1a40491e 738 tag = ff_codec_get_tag(codec_movvideo_tags, track->enc->codec_id);
08680ab8 739 if (!tag) { // if no mac fcc found, try with Microsoft tags
1a40491e 740 tag = ff_codec_get_tag(ff_codec_bmp_tags, track->enc->codec_id);
08680ab8 741 if (tag)
eb97dbb0 742 av_log(s, AV_LOG_WARNING, "Using MS style video codec tag, "
08680ab8
BC
743 "the file may be unplayable!\n");
744 }
72415b2a 745 } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
1a40491e 746 tag = ff_codec_get_tag(codec_movaudio_tags, track->enc->codec_id);
08680ab8 747 if (!tag) { // if no mac fcc found, try with Microsoft tags
1a40491e 748 int ms_tag = ff_codec_get_tag(ff_codec_wav_tags, track->enc->codec_id);
08680ab8
BC
749 if (ms_tag) {
750 tag = MKTAG('m', 's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
eb97dbb0 751 av_log(s, AV_LOG_WARNING, "Using MS style audio codec tag, "
08680ab8 752 "the file may be unplayable!\n");
d97f2144 753 }
08680ab8 754 }
72415b2a 755 } else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)
1a40491e 756 tag = ff_codec_get_tag(ff_codec_movsubtitle_tags, track->enc->codec_id);
a23c9c4a 757 }
2ab57c04 758
8e321619
BC
759 return tag;
760}
761
3c8d1447
BC
762static const AVCodecTag codec_3gp_tags[] = {
763 { CODEC_ID_H263, MKTAG('s','2','6','3') },
764 { CODEC_ID_H264, MKTAG('a','v','c','1') },
765 { CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
766 { CODEC_ID_AAC, MKTAG('m','p','4','a') },
767 { CODEC_ID_AMR_NB, MKTAG('s','a','m','r') },
768 { CODEC_ID_AMR_WB, MKTAG('s','a','w','b') },
769 { CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
770 { CODEC_ID_NONE, 0 },
771};
772
6b600285
BC
773static int mov_find_codec_tag(AVFormatContext *s, MOVTrack *track)
774{
775 int tag = track->enc->codec_tag;
776
5d561514
MS
777 if (track->mode == MODE_MP4 || track->mode == MODE_PSP)
778 tag = mp4_get_codec_tag(s, track);
779 else if (track->mode == MODE_ISM) {
6b600285 780 tag = mp4_get_codec_tag(s, track);
5d561514
MS
781 if (!tag && track->enc->codec_id == CODEC_ID_WMAPRO)
782 tag = MKTAG('w', 'm', 'a', ' ');
783 } else if (track->mode == MODE_IPOD)
6b600285
BC
784 tag = ipod_get_codec_tag(s, track);
785 else if (track->mode & MODE_3GP)
1a40491e 786 tag = ff_codec_get_tag(codec_3gp_tags, track->enc->codec_id);
6b600285
BC
787 else
788 tag = mov_get_codec_tag(s, track);
789
790 return tag;
791}
792
aa9f4208
BC
793/** Write uuid atom.
794 * Needed to make file play in iPods running newest firmware
795 * goes after avcC atom in moov.trak.mdia.minf.stbl.stsd.avc1
796 */
ae628ec1 797static int mov_write_uuid_tag_ipod(AVIOContext *pb)
aa9f4208 798{
77eb5504 799 avio_wb32(pb, 28);
0abdb293 800 ffio_wfourcc(pb, "uuid");
77eb5504
AK
801 avio_wb32(pb, 0x6b6840f2);
802 avio_wb32(pb, 0x5f244fc5);
803 avio_wb32(pb, 0xba39a51b);
804 avio_wb32(pb, 0xcf0323f3);
805 avio_wb32(pb, 0x0);
aa9f4208
BC
806 return 28;
807}
808
4bf3c8f2
AC
809static const uint16_t fiel_data[] = {
810 0x0000, 0x0100, 0x0201, 0x0206, 0x0209, 0x020e
811};
812
813static int mov_write_fiel_tag(AVIOContext *pb, MOVTrack *track)
814{
815 unsigned mov_field_order = 0;
816 if (track->enc->field_order < FF_ARRAY_ELEMS(fiel_data))
817 mov_field_order = fiel_data[track->enc->field_order];
818 else
819 return 0;
820 avio_wb32(pb, 10);
821 ffio_wfourcc(pb, "fiel");
822 avio_wb16(pb, mov_field_order);
823 return 10;
824}
825
ae628ec1 826static int mov_write_subtitle_tag(AVIOContext *pb, MOVTrack *track)
f6204886 827{
a2704c97 828 int64_t pos = avio_tell(pb);
77eb5504
AK
829 avio_wb32(pb, 0); /* size */
830 avio_wl32(pb, track->tag); // store it byteswapped
831 avio_wb32(pb, 0); /* Reserved */
832 avio_wb16(pb, 0); /* Reserved */
833 avio_wb16(pb, 1); /* Data-reference index */
f6204886
DC
834
835 if (track->enc->extradata_size)
77eb5504 836 avio_write(pb, track->enc->extradata, track->enc->extradata_size);
f6204886 837
b7d3dd90 838 return update_size(pb, pos);
f6204886
DC
839}
840
ae628ec1 841static int mov_write_pasp_tag(AVIOContext *pb, MOVTrack *track)
b015be21
BC
842{
843 AVRational sar;
844 av_reduce(&sar.num, &sar.den, track->enc->sample_aspect_ratio.num,
845 track->enc->sample_aspect_ratio.den, INT_MAX);
846
77eb5504 847 avio_wb32(pb, 16);
0abdb293 848 ffio_wfourcc(pb, "pasp");
77eb5504
AK
849 avio_wb32(pb, sar.num);
850 avio_wb32(pb, sar.den);
b015be21
BC
851 return 16;
852}
853
ae628ec1 854static int mov_write_video_tag(AVIOContext *pb, MOVTrack *track)
1cb5f7fd 855{
a2704c97 856 int64_t pos = avio_tell(pb);
53ffdd14 857 char compressor_name[32];
8cc7a34d 858
77eb5504
AK
859 avio_wb32(pb, 0); /* size */
860 avio_wl32(pb, track->tag); // store it byteswapped
861 avio_wb32(pb, 0); /* Reserved */
862 avio_wb16(pb, 0); /* Reserved */
863 avio_wb16(pb, 1); /* Data-reference index */
f578f938 864
77eb5504
AK
865 avio_wb16(pb, 0); /* Codec stream version */
866 avio_wb16(pb, 0); /* Codec stream revision (=0) */
5997ed78 867 if (track->mode == MODE_MOV) {
0abdb293 868 ffio_wfourcc(pb, "FFMP"); /* Vendor */
915282e5 869 if(track->enc->codec_id == CODEC_ID_RAWVIDEO) {
77eb5504
AK
870 avio_wb32(pb, 0); /* Temporal Quality */
871 avio_wb32(pb, 0x400); /* Spatial Quality = lossless*/
915282e5 872 } else {
77eb5504
AK
873 avio_wb32(pb, 0x200); /* Temporal Quality = normal */
874 avio_wb32(pb, 0x200); /* Spatial Quality = normal */
915282e5 875 }
5997ed78 876 } else {
77eb5504
AK
877 avio_wb32(pb, 0); /* Reserved */
878 avio_wb32(pb, 0); /* Reserved */
879 avio_wb32(pb, 0); /* Reserved */
5997ed78 880 }
77eb5504
AK
881 avio_wb16(pb, track->enc->width); /* Video width */
882 avio_wb16(pb, track->height); /* Video height */
883 avio_wb32(pb, 0x00480000); /* Horizontal resolution 72dpi */
884 avio_wb32(pb, 0x00480000); /* Vertical resolution 72dpi */
885 avio_wb32(pb, 0); /* Data size (= 0) */
886 avio_wb16(pb, 1); /* Frame count (= 1) */
115329f1 887
53ffdd14 888 memset(compressor_name,0,32);
5997ed78
BC
889 /* FIXME not sure, ISO 14496-1 draft where it shall be set to 0 */
890 if (track->mode == MODE_MOV && track->enc->codec && track->enc->codec->name)
1a5e4fd8 891 av_strlcpy(compressor_name,track->enc->codec->name,32);
77eb5504
AK
892 avio_w8(pb, strlen(compressor_name));
893 avio_write(pb, compressor_name, 31);
115329f1 894
dd1c8f3e 895 if (track->mode == MODE_MOV && track->enc->bits_per_coded_sample)
77eb5504 896 avio_wb16(pb, track->enc->bits_per_coded_sample);
ccac8438 897 else
77eb5504
AK
898 avio_wb16(pb, 0x18); /* Reserved */
899 avio_wb16(pb, 0xffff); /* Reserved */
d0c0a29b 900 if(track->tag == MKTAG('m','p','4','v'))
f578f938
TR
901 mov_write_esds_tag(pb, track);
902 else if(track->enc->codec_id == CODEC_ID_H263)
903 mov_write_d263_tag(pb);
904 else if(track->enc->codec_id == CODEC_ID_SVQ3)
115329f1 905 mov_write_svq3_tag(pb);
7b0f4c9a
BC
906 else if(track->enc->codec_id == CODEC_ID_DNXHD)
907 mov_write_avid_tag(pb, track);
aa9f4208 908 else if(track->enc->codec_id == CODEC_ID_H264) {
c1b8e6d8 909 mov_write_avcc_tag(pb, track);
aa9f4208
BC
910 if(track->mode == MODE_IPOD)
911 mov_write_uuid_tag_ipod(pb);
4bf3c8f2
AC
912 } else if (track->enc->field_order != AV_FIELD_UNKNOWN)
913 mov_write_fiel_tag(pb, track);
b7d3dd90 914 else if (track->vos_len > 0)
79e42311 915 mov_write_glbl_tag(pb, track);
f578f938 916
d184c86c 917 if (track->enc->sample_aspect_ratio.den && track->enc->sample_aspect_ratio.num &&
b015be21
BC
918 track->enc->sample_aspect_ratio.den != track->enc->sample_aspect_ratio.num) {
919 mov_write_pasp_tag(pb, track);
920 }
921
b7d3dd90 922 return update_size(pb, pos);
1cb5f7fd
MN
923}
924
ae628ec1 925static int mov_write_rtp_tag(AVIOContext *pb, MOVTrack *track)
e977af6f 926{
a2704c97 927 int64_t pos = avio_tell(pb);
77eb5504 928 avio_wb32(pb, 0); /* size */
0abdb293 929 ffio_wfourcc(pb, "rtp ");
77eb5504
AK
930 avio_wb32(pb, 0); /* Reserved */
931 avio_wb16(pb, 0); /* Reserved */
932 avio_wb16(pb, 1); /* Data-reference index */
e977af6f 933
77eb5504
AK
934 avio_wb16(pb, 1); /* Hint track version */
935 avio_wb16(pb, 1); /* Highest compatible version */
936 avio_wb32(pb, track->max_packet_size); /* Max packet size */
e977af6f 937
77eb5504 938 avio_wb32(pb, 12); /* size */
0abdb293 939 ffio_wfourcc(pb, "tims");
77eb5504 940 avio_wb32(pb, track->timescale);
e977af6f 941
b7d3dd90 942 return update_size(pb, pos);
e977af6f
MS
943}
944
ae628ec1 945static int mov_write_stsd_tag(AVIOContext *pb, MOVTrack *track)
1cb5f7fd 946{
a2704c97 947 int64_t pos = avio_tell(pb);
77eb5504 948 avio_wb32(pb, 0); /* size */
0abdb293 949 ffio_wfourcc(pb, "stsd");
77eb5504
AK
950 avio_wb32(pb, 0); /* version & flags */
951 avio_wb32(pb, 1); /* entry count */
72415b2a 952 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO)
f578f938 953 mov_write_video_tag(pb, track);
72415b2a 954 else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
f578f938 955 mov_write_audio_tag(pb, track);
72415b2a 956 else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)
f6204886 957 mov_write_subtitle_tag(pb, track);
e977af6f
MS
958 else if (track->enc->codec_tag == MKTAG('r','t','p',' '))
959 mov_write_rtp_tag(pb, track);
b7d3dd90 960 return update_size(pb, pos);
1cb5f7fd
MN
961}
962
ae628ec1 963static int mov_write_ctts_tag(AVIOContext *pb, MOVTrack *track)
b4712e3c 964{
c3e92a6c 965 MOVStts *ctts_entries;
b4712e3c
BC
966 uint32_t entries = 0;
967 uint32_t atom_size;
968 int i;
969
970 ctts_entries = av_malloc((track->entry + 1) * sizeof(*ctts_entries)); /* worst case */
971 ctts_entries[0].count = 1;
0aec3c5c 972 ctts_entries[0].duration = track->cluster[0].cts;
b4712e3c 973 for (i=1; i<track->entry; i++) {
0aec3c5c 974 if (track->cluster[i].cts == ctts_entries[entries].duration) {
b4712e3c
BC
975 ctts_entries[entries].count++; /* compress */
976 } else {
977 entries++;
0aec3c5c 978 ctts_entries[entries].duration = track->cluster[i].cts;
b4712e3c
BC
979 ctts_entries[entries].count = 1;
980 }
981 }
982 entries++; /* last one */
983 atom_size = 16 + (entries * 8);
77eb5504 984 avio_wb32(pb, atom_size); /* size */
0abdb293 985 ffio_wfourcc(pb, "ctts");
77eb5504
AK
986 avio_wb32(pb, 0); /* version & flags */
987 avio_wb32(pb, entries); /* entry count */
b4712e3c 988 for (i=0; i<entries; i++) {
77eb5504
AK
989 avio_wb32(pb, ctts_entries[i].count);
990 avio_wb32(pb, ctts_entries[i].duration);
b4712e3c
BC
991 }
992 av_free(ctts_entries);
993 return atom_size;
994}
995
e45ccf79 996/* Time to sample atom */
ae628ec1 997static int mov_write_stts_tag(AVIOContext *pb, MOVTrack *track)
1cb5f7fd 998{
c3e92a6c 999 MOVStts *stts_entries;
aa90239f
BC
1000 uint32_t entries = -1;
1001 uint32_t atom_size;
1002 int i;
1003
72415b2a 1004 if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO && !track->audio_vbr) {
aa90239f 1005 stts_entries = av_malloc(sizeof(*stts_entries)); /* one entry */
b7d3dd90 1006 stts_entries[0].count = track->sample_count;
aa90239f
BC
1007 stts_entries[0].duration = 1;
1008 entries = 1;
1009 } else {
83988d58
MS
1010 stts_entries = track->entry ?
1011 av_malloc(track->entry * sizeof(*stts_entries)) : /* worst case */
1012 NULL;
aa90239f
BC
1013 for (i=0; i<track->entry; i++) {
1014 int64_t duration = i + 1 == track->entry ?
b7d3dd90 1015 track->track_duration - track->cluster[i].dts + track->start_dts : /* readjusting */
aa90239f
BC
1016 track->cluster[i+1].dts - track->cluster[i].dts;
1017 if (i && duration == stts_entries[entries].duration) {
1018 stts_entries[entries].count++; /* compress */
1019 } else {
1020 entries++;
1021 stts_entries[entries].duration = duration;
1022 stts_entries[entries].count = 1;
1023 }
1024 }
1025 entries++; /* last one */
1026 }
1027 atom_size = 16 + (entries * 8);
77eb5504 1028 avio_wb32(pb, atom_size); /* size */
0abdb293 1029 ffio_wfourcc(pb, "stts");
77eb5504
AK
1030 avio_wb32(pb, 0); /* version & flags */
1031 avio_wb32(pb, entries); /* entry count */
aa90239f 1032 for (i=0; i<entries; i++) {
77eb5504
AK
1033 avio_wb32(pb, stts_entries[i].count);
1034 avio_wb32(pb, stts_entries[i].duration);
aa90239f
BC
1035 }
1036 av_free(stts_entries);
1037 return atom_size;
1cb5f7fd
MN
1038}
1039
ae628ec1 1040static int mov_write_dref_tag(AVIOContext *pb)
1cb5f7fd 1041{
77eb5504 1042 avio_wb32(pb, 28); /* size */
0abdb293 1043 ffio_wfourcc(pb, "dref");
77eb5504
AK
1044 avio_wb32(pb, 0); /* version & flags */
1045 avio_wb32(pb, 1); /* entry count */
1cb5f7fd 1046
77eb5504 1047 avio_wb32(pb, 0xc); /* size */
0abdb293 1048 ffio_wfourcc(pb, "url ");
77eb5504 1049 avio_wb32(pb, 1); /* version & flags */
1cb5f7fd
MN
1050
1051 return 28;
1052}
1053
ae628ec1 1054static int mov_write_stbl_tag(AVIOContext *pb, MOVTrack *track)
1cb5f7fd 1055{
a2704c97 1056 int64_t pos = avio_tell(pb);
77eb5504 1057 avio_wb32(pb, 0); /* size */
0abdb293 1058 ffio_wfourcc(pb, "stbl");
6e6d6dc0
MN
1059 mov_write_stsd_tag(pb, track);
1060 mov_write_stts_tag(pb, track);
e977af6f
MS
1061 if ((track->enc->codec_type == AVMEDIA_TYPE_VIDEO ||
1062 track->enc->codec_tag == MKTAG('r','t','p',' ')) &&
b7d3dd90 1063 track->has_keyframes && track->has_keyframes < track->entry)
e1316b19
BC
1064 mov_write_stss_tag(pb, track, MOV_SYNC_SAMPLE);
1065 if (track->mode == MODE_MOV && track->flags & MOV_TRACK_STPS)
1066 mov_write_stss_tag(pb, track, MOV_PARTIAL_SYNC_SAMPLE);
72415b2a 1067 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO &&
e1316b19 1068 track->flags & MOV_TRACK_CTTS)
b4712e3c 1069 mov_write_ctts_tag(pb, track);
6e6d6dc0
MN
1070 mov_write_stsc_tag(pb, track);
1071 mov_write_stsz_tag(pb, track);
1072 mov_write_stco_tag(pb, track);
b7d3dd90 1073 return update_size(pb, pos);
1cb5f7fd
MN
1074}
1075
ae628ec1 1076static int mov_write_dinf_tag(AVIOContext *pb)
1cb5f7fd 1077{
a2704c97 1078 int64_t pos = avio_tell(pb);
77eb5504 1079 avio_wb32(pb, 0); /* size */
0abdb293 1080 ffio_wfourcc(pb, "dinf");
6e6d6dc0 1081 mov_write_dref_tag(pb);
b7d3dd90 1082 return update_size(pb, pos);
1cb5f7fd
MN
1083}
1084
ae628ec1 1085static int mov_write_nmhd_tag(AVIOContext *pb)
f6204886 1086{
77eb5504 1087 avio_wb32(pb, 12);
0abdb293 1088 ffio_wfourcc(pb, "nmhd");
77eb5504 1089 avio_wb32(pb, 0);
f6204886
DC
1090 return 12;
1091}
1092
ae628ec1 1093static int mov_write_gmhd_tag(AVIOContext *pb)
f6204886 1094{
77eb5504 1095 avio_wb32(pb, 0x20); /* size */
0abdb293 1096 ffio_wfourcc(pb, "gmhd");
77eb5504 1097 avio_wb32(pb, 0x18); /* gmin size */
0abdb293 1098 ffio_wfourcc(pb, "gmin");/* generic media info */
77eb5504
AK
1099 avio_wb32(pb, 0); /* version & flags */
1100 avio_wb16(pb, 0x40); /* graphics mode = */
1101 avio_wb16(pb, 0x8000); /* opColor (r?) */
1102 avio_wb16(pb, 0x8000); /* opColor (g?) */
1103 avio_wb16(pb, 0x8000); /* opColor (b?) */
1104 avio_wb16(pb, 0); /* balance */
1105 avio_wb16(pb, 0); /* reserved */
f6204886
DC
1106 return 0x20;
1107}
1108
ae628ec1 1109static int mov_write_smhd_tag(AVIOContext *pb)
1cb5f7fd 1110{
77eb5504 1111 avio_wb32(pb, 16); /* size */
0abdb293 1112 ffio_wfourcc(pb, "smhd");
77eb5504
AK
1113 avio_wb32(pb, 0); /* version & flags */
1114 avio_wb16(pb, 0); /* reserved (balance, normally = 0) */
1115 avio_wb16(pb, 0); /* reserved */
1cb5f7fd
MN
1116 return 16;
1117}
1118
ae628ec1 1119static int mov_write_vmhd_tag(AVIOContext *pb)
1cb5f7fd 1120{
77eb5504 1121 avio_wb32(pb, 0x14); /* size (always 0x14) */
0abdb293 1122 ffio_wfourcc(pb, "vmhd");
77eb5504
AK
1123 avio_wb32(pb, 0x01); /* version & flags */
1124 avio_wb64(pb, 0); /* reserved (graphics mode = copy) */
1cb5f7fd
MN
1125 return 0x14;
1126}
1127
ae628ec1 1128static int mov_write_hdlr_tag(AVIOContext *pb, MOVTrack *track)
1cb5f7fd 1129{
985688b8 1130 const char *hdlr, *descr = NULL, *hdlr_type = NULL;
a2704c97 1131 int64_t pos = avio_tell(pb);
115329f1 1132
9a4d9388 1133 if (!track) { /* no media --> data handler */
bb270c08
DB
1134 hdlr = "dhlr";
1135 hdlr_type = "url ";
1136 descr = "DataHandler";
9a4d9388 1137 } else {
bb270c08 1138 hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
72415b2a 1139 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
bb270c08
DB
1140 hdlr_type = "vide";
1141 descr = "VideoHandler";
72415b2a 1142 } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
bb270c08
DB
1143 hdlr_type = "soun";
1144 descr = "SoundHandler";
72415b2a 1145 } else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) {
9f520554 1146 if (track->tag == MKTAG('t','x','3','g')) hdlr_type = "sbtl";
80357cfc 1147 else hdlr_type = "text";
f6204886 1148 descr = "SubtitleHandler";
e977af6f
MS
1149 } else if (track->enc->codec_tag == MKTAG('r','t','p',' ')) {
1150 hdlr_type = "hint";
1151 descr = "HintHandler";
bb270c08 1152 }
9a4d9388 1153 }
115329f1 1154
77eb5504 1155 avio_wb32(pb, 0); /* size */
0abdb293 1156 ffio_wfourcc(pb, "hdlr");
77eb5504
AK
1157 avio_wb32(pb, 0); /* Version & flags */
1158 avio_write(pb, hdlr, 4); /* handler */
0abdb293 1159 ffio_wfourcc(pb, hdlr_type); /* handler type */
77eb5504
AK
1160 avio_wb32(pb ,0); /* reserved */
1161 avio_wb32(pb ,0); /* reserved */
1162 avio_wb32(pb ,0); /* reserved */
606d48ce 1163 if (!track || track->mode == MODE_MOV)
77eb5504
AK
1164 avio_w8(pb, strlen(descr)); /* pascal string */
1165 avio_write(pb, descr, strlen(descr)); /* handler description */
606d48ce 1166 if (track && track->mode != MODE_MOV)
77eb5504 1167 avio_w8(pb, 0); /* c string */
b7d3dd90 1168 return update_size(pb, pos);
9a4d9388
RS
1169}
1170
ae628ec1 1171static int mov_write_hmhd_tag(AVIOContext *pb)
e977af6f
MS
1172{
1173 /* This atom must be present, but leaving the values at zero
1174 * seems harmless. */
77eb5504 1175 avio_wb32(pb, 28); /* size */
0abdb293 1176 ffio_wfourcc(pb, "hmhd");
77eb5504
AK
1177 avio_wb32(pb, 0); /* version, flags */
1178 avio_wb16(pb, 0); /* maxPDUsize */
1179 avio_wb16(pb, 0); /* avgPDUsize */
1180 avio_wb32(pb, 0); /* maxbitrate */
1181 avio_wb32(pb, 0); /* avgbitrate */
1182 avio_wb32(pb, 0); /* reserved */
e977af6f
MS
1183 return 28;
1184}
1185
ae628ec1 1186static int mov_write_minf_tag(AVIOContext *pb, MOVTrack *track)
9a4d9388 1187{
a2704c97 1188 int64_t pos = avio_tell(pb);
77eb5504 1189 avio_wb32(pb, 0); /* size */
0abdb293 1190 ffio_wfourcc(pb, "minf");
72415b2a 1191 if(track->enc->codec_type == AVMEDIA_TYPE_VIDEO)
9a4d9388 1192 mov_write_vmhd_tag(pb);
72415b2a 1193 else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
9a4d9388 1194 mov_write_smhd_tag(pb);
72415b2a 1195 else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) {
9f520554 1196 if (track->tag == MKTAG('t','e','x','t')) mov_write_gmhd_tag(pb);
80357cfc 1197 else mov_write_nmhd_tag(pb);
e977af6f
MS
1198 } else if (track->tag == MKTAG('r','t','p',' ')) {
1199 mov_write_hmhd_tag(pb);
f6204886 1200 }
9a4d9388
RS
1201 if (track->mode == MODE_MOV) /* FIXME: Why do it for MODE_MOV only ? */
1202 mov_write_hdlr_tag(pb, NULL);
1203 mov_write_dinf_tag(pb);
1204 mov_write_stbl_tag(pb, track);
b7d3dd90 1205 return update_size(pb, pos);
1cb5f7fd
MN
1206}
1207
ae628ec1 1208static int mov_write_mdhd_tag(AVIOContext *pb, MOVTrack *track)
1cb5f7fd 1209{
b7d3dd90 1210 int version = track->track_duration < INT32_MAX ? 0 : 1;
a3a80ddc 1211
77eb5504 1212 (version == 1) ? avio_wb32(pb, 44) : avio_wb32(pb, 32); /* size */
0abdb293 1213 ffio_wfourcc(pb, "mdhd");
77eb5504
AK
1214 avio_w8(pb, version);
1215 avio_wb24(pb, 0); /* flags */
a3a80ddc 1216 if (version == 1) {
77eb5504
AK
1217 avio_wb64(pb, track->time);
1218 avio_wb64(pb, track->time);
a3a80ddc 1219 } else {
77eb5504
AK
1220 avio_wb32(pb, track->time); /* creation time */
1221 avio_wb32(pb, track->time); /* modification time */
a3a80ddc 1222 }
77eb5504 1223 avio_wb32(pb, track->timescale); /* time scale (sample rate for audio) */
b7d3dd90 1224 (version == 1) ? avio_wb64(pb, track->track_duration) : avio_wb32(pb, track->track_duration); /* duration */
77eb5504
AK
1225 avio_wb16(pb, track->language); /* language */
1226 avio_wb16(pb, 0); /* reserved (quality) */
db8f4a92
MN
1227
1228 if(version!=0 && track->mode == MODE_MOV){
1229 av_log(NULL, AV_LOG_ERROR,
1230 "FATAL error, file duration too long for timebase, this file will not be\n"
b1b64c23 1231 "playable with quicktime. Choose a different timebase or a different\n"
db8f4a92
MN
1232 "container format\n");
1233 }
1234
1cb5f7fd
MN
1235 return 32;
1236}
1237
ae628ec1 1238static int mov_write_mdia_tag(AVIOContext *pb, MOVTrack *track)
1cb5f7fd 1239{
a2704c97 1240 int64_t pos = avio_tell(pb);
77eb5504 1241 avio_wb32(pb, 0); /* size */
0abdb293 1242 ffio_wfourcc(pb, "mdia");
6e6d6dc0
MN
1243 mov_write_mdhd_tag(pb, track);
1244 mov_write_hdlr_tag(pb, track);
1245 mov_write_minf_tag(pb, track);
b7d3dd90 1246 return update_size(pb, pos);
1cb5f7fd
MN
1247}
1248
ae628ec1 1249static int mov_write_tkhd_tag(AVIOContext *pb, MOVTrack *track, AVStream *st)
1cb5f7fd 1250{
b7d3dd90 1251 int64_t duration = av_rescale_rnd(track->track_duration, MOV_TIMESCALE,
729ef3ba 1252 track->timescale, AV_ROUND_UP);
a3a80ddc
BC
1253 int version = duration < INT32_MAX ? 0 : 1;
1254
77eb5504 1255 (version == 1) ? avio_wb32(pb, 104) : avio_wb32(pb, 92); /* size */
0abdb293 1256 ffio_wfourcc(pb, "tkhd");
77eb5504
AK
1257 avio_w8(pb, version);
1258 avio_wb24(pb, 0xf); /* flags (track enabled) */
a3a80ddc 1259 if (version == 1) {
77eb5504
AK
1260 avio_wb64(pb, track->time);
1261 avio_wb64(pb, track->time);
a3a80ddc 1262 } else {
77eb5504
AK
1263 avio_wb32(pb, track->time); /* creation time */
1264 avio_wb32(pb, track->time); /* modification time */
a3a80ddc 1265 }
b7d3dd90 1266 avio_wb32(pb, track->track_id); /* track-id */
77eb5504
AK
1267 avio_wb32(pb, 0); /* reserved */
1268 (version == 1) ? avio_wb64(pb, duration) : avio_wb32(pb, duration);
1cb5f7fd 1269
77eb5504
AK
1270 avio_wb32(pb, 0); /* reserved */
1271 avio_wb32(pb, 0); /* reserved */
7574cacb 1272 avio_wb16(pb, 0); /* layer */
c92a2a4e 1273 avio_wb16(pb, st ? st->codec->codec_type : 0); /* alternate group) */
1cb5f7fd 1274 /* Volume, only for audio */
72415b2a 1275 if(track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
77eb5504 1276 avio_wb16(pb, 0x0100);
1cb5f7fd 1277 else
77eb5504
AK
1278 avio_wb16(pb, 0);
1279 avio_wb16(pb, 0); /* reserved */
1cb5f7fd
MN
1280
1281 /* Matrix structure */
77eb5504
AK
1282 avio_wb32(pb, 0x00010000); /* reserved */
1283 avio_wb32(pb, 0x0); /* reserved */
1284 avio_wb32(pb, 0x0); /* reserved */
1285 avio_wb32(pb, 0x0); /* reserved */
1286 avio_wb32(pb, 0x00010000); /* reserved */
1287 avio_wb32(pb, 0x0); /* reserved */
1288 avio_wb32(pb, 0x0); /* reserved */
1289 avio_wb32(pb, 0x0); /* reserved */
1290 avio_wb32(pb, 0x40000000); /* reserved */
1cb5f7fd
MN
1291
1292 /* Track width and height, for visual only */
ddb63017
DC
1293 if(st && (track->enc->codec_type == AVMEDIA_TYPE_VIDEO ||
1294 track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)) {
ea1afa12
MV
1295 if(track->mode == MODE_MOV) {
1296 avio_wb32(pb, track->enc->width << 16);
fffdee89 1297 avio_wb32(pb, track->height << 16);
ea1afa12 1298 } else {
77d207cb
MV
1299 double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
1300 if(!sample_aspect_ratio || track->height != track->enc->height)
1301 sample_aspect_ratio = 1;
1302 avio_wb32(pb, sample_aspect_ratio * track->enc->width*0x10000);
1303 avio_wb32(pb, track->height*0x10000);
ea1afa12 1304 }
1cb5f7fd
MN
1305 }
1306 else {
77eb5504
AK
1307 avio_wb32(pb, 0);
1308 avio_wb32(pb, 0);
1cb5f7fd
MN
1309 }
1310 return 0x5c;
1311}
1312
13ff92d1 1313static int mov_write_tapt_tag(AVIOContext *pb, MOVTrack *track)
ea1afa12
MV
1314{
1315 int32_t width = av_rescale(track->enc->sample_aspect_ratio.num, track->enc->width,
1316 track->enc->sample_aspect_ratio.den);
1317
a2704c97 1318 int64_t pos = avio_tell(pb);
ea1afa12
MV
1319
1320 avio_wb32(pb, 0); /* size */
13ff92d1 1321 ffio_wfourcc(pb, "tapt");
ea1afa12
MV
1322
1323 avio_wb32(pb, 20);
13ff92d1 1324 ffio_wfourcc(pb, "clef");
ea1afa12
MV
1325 avio_wb32(pb, 0);
1326 avio_wb32(pb, width << 16);
1327 avio_wb32(pb, track->enc->height << 16);
1328
1329 avio_wb32(pb, 20);
13ff92d1 1330 ffio_wfourcc(pb, "enof");
ea1afa12
MV
1331 avio_wb32(pb, 0);
1332 avio_wb32(pb, track->enc->width << 16);
1333 avio_wb32(pb, track->enc->height << 16);
1334
b7d3dd90 1335 return update_size(pb, pos);
373211d8 1336}
ea1afa12 1337
8af18154 1338// This box seems important for the psp playback ... without it the movie seems to hang
ae628ec1 1339static int mov_write_edts_tag(AVIOContext *pb, MOVTrack *track)
8af18154 1340{
b7d3dd90 1341 int64_t duration = av_rescale_rnd(track->track_duration, MOV_TIMESCALE,
c1691948
GP
1342 track->timescale, AV_ROUND_UP);
1343 int version = duration < INT32_MAX ? 0 : 1;
1344 int entry_size, entry_count, size;
1345 int64_t delay, start_ct = track->cluster[0].cts;
1346 delay = av_rescale_rnd(track->cluster[0].dts + start_ct, MOV_TIMESCALE,
1347 track->timescale, AV_ROUND_DOWN);
1348 version |= delay < INT32_MAX ? 0 : 1;
1349
1350 entry_size = (version == 1) ? 20 : 12;
1351 entry_count = 1 + (delay > 0);
1352 size = 24 + entry_count * entry_size;
1353
1354 /* write the atom data */
1355 avio_wb32(pb, size);
0abdb293 1356 ffio_wfourcc(pb, "edts");
c1691948 1357 avio_wb32(pb, size - 8);
0abdb293 1358 ffio_wfourcc(pb, "elst");
c1691948
GP
1359 avio_w8(pb, version);
1360 avio_wb24(pb, 0); /* flags */
8af18154 1361
c1691948
GP
1362 avio_wb32(pb, entry_count);
1363 if (delay > 0) { /* add an empty edit to delay presentation */
1364 if (version == 1) {
1365 avio_wb64(pb, delay);
1366 avio_wb64(pb, -1);
1367 } else {
1368 avio_wb32(pb, delay);
1369 avio_wb32(pb, -1);
1370 }
1371 avio_wb32(pb, 0x00010000);
1372 }
8af18154 1373
c1691948
GP
1374 /* duration */
1375 if (version == 1) {
1376 avio_wb64(pb, duration);
1377 avio_wb64(pb, start_ct);
1378 } else {
1379 avio_wb32(pb, duration);
1380 avio_wb32(pb, start_ct);
1381 }
77eb5504 1382 avio_wb32(pb, 0x00010000);
c1691948 1383 return size;
8af18154 1384}
1385
ae628ec1 1386static int mov_write_tref_tag(AVIOContext *pb, MOVTrack *track)
ddb63017 1387{
77eb5504 1388 avio_wb32(pb, 20); // size
0abdb293 1389 ffio_wfourcc(pb, "tref");
77eb5504
AK
1390 avio_wb32(pb, 12); // size (subatom)
1391 avio_wl32(pb, track->tref_tag);
1392 avio_wb32(pb, track->tref_id);
ddb63017
DC
1393 return 20;
1394}
1395
8af18154 1396// goes at the end of each track! ... Critical for PSP playback ("Incompatible data" without it)
ae628ec1 1397static int mov_write_uuid_tag_psp(AVIOContext *pb, MOVTrack *mov)
8af18154 1398{
77eb5504 1399 avio_wb32(pb, 0x34); /* size ... reports as 28 in mp4box! */
0abdb293
AK
1400 ffio_wfourcc(pb, "uuid");
1401 ffio_wfourcc(pb, "USMT");
77eb5504
AK
1402 avio_wb32(pb, 0x21d24fce);
1403 avio_wb32(pb, 0xbb88695c);
1404 avio_wb32(pb, 0xfac9c740);
1405 avio_wb32(pb, 0x1c); // another size here!
0abdb293 1406 ffio_wfourcc(pb, "MTDT");
77eb5504
AK
1407 avio_wb32(pb, 0x00010012);
1408 avio_wb32(pb, 0x0a);
1409 avio_wb32(pb, 0x55c40000);
1410 avio_wb32(pb, 0x1);
1411 avio_wb32(pb, 0x0);
8af18154 1412 return 0x34;
1413}
1414
f3f82296 1415static int mov_write_udta_sdp(AVIOContext *pb, AVFormatContext *ctx, int index)
e977af6f
MS
1416{
1417 char buf[1000] = "";
1418 int len;
1419
f3f82296 1420 ff_sdp_write_media(buf, sizeof(buf), ctx->streams[0]->codec, NULL, NULL, 0, 0, ctx);
e977af6f
MS
1421 av_strlcatf(buf, sizeof(buf), "a=control:streamid=%d\r\n", index);
1422 len = strlen(buf);
1423
77eb5504 1424 avio_wb32(pb, len + 24);
0abdb293 1425 ffio_wfourcc(pb, "udta");
77eb5504 1426 avio_wb32(pb, len + 16);
0abdb293 1427 ffio_wfourcc(pb, "hnti");
77eb5504 1428 avio_wb32(pb, len + 8);
0abdb293 1429 ffio_wfourcc(pb, "sdp ");
77eb5504 1430 avio_write(pb, buf, len);
e977af6f
MS
1431 return len + 24;
1432}
1433
83988d58
MS
1434static int mov_write_trak_tag(AVIOContext *pb, MOVMuxContext *mov,
1435 MOVTrack *track, AVStream *st)
1cb5f7fd 1436{
a2704c97 1437 int64_t pos = avio_tell(pb);
77eb5504 1438 avio_wb32(pb, 0); /* size */
0abdb293 1439 ffio_wfourcc(pb, "trak");
59729451 1440 mov_write_tkhd_tag(pb, track, st);
83988d58
MS
1441 if (track->mode == MODE_PSP || track->flags & MOV_TRACK_CTTS ||
1442 (track->entry && track->cluster[0].dts)) {
1443 if (!(mov->flags & FF_MOV_FLAG_FRAGMENT))
1444 mov_write_edts_tag(pb, track); // PSP Movies require edts box
1445 }
ddb63017
DC
1446 if (track->tref_tag)
1447 mov_write_tref_tag(pb, track);
6e6d6dc0 1448 mov_write_mdia_tag(pb, track);
115329f1 1449 if (track->mode == MODE_PSP)
8af18154 1450 mov_write_uuid_tag_psp(pb,track); // PSP Movies require this uuid box
e977af6f 1451 if (track->tag == MKTAG('r','t','p',' '))
b7d3dd90 1452 mov_write_udta_sdp(pb, track->rtp_ctx, track->track_id);
ea1afa12
MV
1453 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO && track->mode == MODE_MOV) {
1454 double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
1455 if (0.0 != sample_aspect_ratio && 1.0 != sample_aspect_ratio)
1456 mov_write_tapt_tag(pb, track);
1457 };
b7d3dd90 1458 return update_size(pb, pos);
1cb5f7fd
MN
1459}
1460
ae628ec1 1461static int mov_write_iods_tag(AVIOContext *pb, MOVMuxContext *mov)
1cb5f7fd 1462{
d3b8bde2
AC
1463 int i, has_audio = 0, has_video = 0;
1464 int64_t pos = avio_tell(pb);
1465 int audio_profile = mov->iods_audio_profile;
1466 int video_profile = mov->iods_video_profile;
1467 for (i = 0; i < mov->nb_streams; i++) {
1468 if(mov->tracks[i].entry > 0) {
1469 has_audio |= mov->tracks[i].enc->codec_type == AVMEDIA_TYPE_AUDIO;
1470 has_video |= mov->tracks[i].enc->codec_type == AVMEDIA_TYPE_VIDEO;
1471 }
1472 }
1473 if (audio_profile < 0)
1474 audio_profile = 0xFF - has_audio;
1475 if (video_profile < 0)
1476 video_profile = 0xFF - has_video;
1477 avio_wb32(pb, 0x0); /* size */
0abdb293 1478 ffio_wfourcc(pb, "iods");
77eb5504 1479 avio_wb32(pb, 0); /* version & flags */
b7d3dd90 1480 put_descr(pb, 0x10, 7);
d3b8bde2
AC
1481 avio_wb16(pb, 0x004f);
1482 avio_w8(pb, 0xff);
1483 avio_w8(pb, 0xff);
1484 avio_w8(pb, audio_profile);
1485 avio_w8(pb, video_profile);
1486 avio_w8(pb, 0xff);
b7d3dd90 1487 return update_size(pb, pos);
1cb5f7fd
MN
1488}
1489
83988d58
MS
1490static int mov_write_trex_tag(AVIOContext *pb, MOVTrack *track)
1491{
1492 avio_wb32(pb, 0x20); /* size */
1493 ffio_wfourcc(pb, "trex");
1494 avio_wb32(pb, 0); /* version & flags */
b7d3dd90 1495 avio_wb32(pb, track->track_id); /* track ID */
83988d58
MS
1496 avio_wb32(pb, 1); /* default sample description index */
1497 avio_wb32(pb, 0); /* default sample duration */
1498 avio_wb32(pb, 0); /* default sample size */
1499 avio_wb32(pb, 0); /* default sample flags */
1500 return 0;
1501}
1502
1503static int mov_write_mvex_tag(AVIOContext *pb, MOVMuxContext *mov)
1504{
1505 int64_t pos = avio_tell(pb);
1506 int i;
1507 avio_wb32(pb, 0x0); /* size */
1508 ffio_wfourcc(pb, "mvex");
1509 for (i = 0; i < mov->nb_streams; i++)
1510 mov_write_trex_tag(pb, &mov->tracks[i]);
b7d3dd90 1511 return update_size(pb, pos);
83988d58
MS
1512}
1513
ae628ec1 1514static int mov_write_mvhd_tag(AVIOContext *pb, MOVMuxContext *mov)
1cb5f7fd 1515{
b7d3dd90
MS
1516 int max_track_id = 1, i;
1517 int64_t max_track_len_temp, max_track_len = 0;
a3a80ddc 1518 int version;
1cb5f7fd 1519
42fb4148 1520 for (i=0; i<mov->nb_streams; i++) {
1cb5f7fd 1521 if(mov->tracks[i].entry > 0) {
b7d3dd90
MS
1522 max_track_len_temp = av_rescale_rnd(mov->tracks[i].track_duration,
1523 MOV_TIMESCALE,
1524 mov->tracks[i].timescale,
1525 AV_ROUND_UP);
1526 if (max_track_len < max_track_len_temp)
1527 max_track_len = max_track_len_temp;
1528 if (max_track_id < mov->tracks[i].track_id)
1529 max_track_id = mov->tracks[i].track_id;
1cb5f7fd
MN
1530 }
1531 }
a3a80ddc 1532
b7d3dd90 1533 version = max_track_len < UINT32_MAX ? 0 : 1;
77eb5504 1534 (version == 1) ? avio_wb32(pb, 120) : avio_wb32(pb, 108); /* size */
0abdb293 1535 ffio_wfourcc(pb, "mvhd");
77eb5504
AK
1536 avio_w8(pb, version);
1537 avio_wb24(pb, 0); /* flags */
a3a80ddc 1538 if (version == 1) {
77eb5504
AK
1539 avio_wb64(pb, mov->time);
1540 avio_wb64(pb, mov->time);
a3a80ddc 1541 } else {
77eb5504
AK
1542 avio_wb32(pb, mov->time); /* creation time */
1543 avio_wb32(pb, mov->time); /* modification time */
a3a80ddc 1544 }
77eb5504 1545 avio_wb32(pb, MOV_TIMESCALE);
b7d3dd90 1546 (version == 1) ? avio_wb64(pb, max_track_len) : avio_wb32(pb, max_track_len); /* duration of longest track */
1cb5f7fd 1547
77eb5504
AK
1548 avio_wb32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
1549 avio_wb16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
1550 avio_wb16(pb, 0); /* reserved */
1551 avio_wb32(pb, 0); /* reserved */
1552 avio_wb32(pb, 0); /* reserved */
1cb5f7fd
MN
1553
1554 /* Matrix structure */
77eb5504
AK
1555 avio_wb32(pb, 0x00010000); /* reserved */
1556 avio_wb32(pb, 0x0); /* reserved */
1557 avio_wb32(pb, 0x0); /* reserved */
1558 avio_wb32(pb, 0x0); /* reserved */
1559 avio_wb32(pb, 0x00010000); /* reserved */
1560 avio_wb32(pb, 0x0); /* reserved */
1561 avio_wb32(pb, 0x0); /* reserved */
1562 avio_wb32(pb, 0x0); /* reserved */
1563 avio_wb32(pb, 0x40000000); /* reserved */
1564
1565 avio_wb32(pb, 0); /* reserved (preview time) */
1566 avio_wb32(pb, 0); /* reserved (preview duration) */
1567 avio_wb32(pb, 0); /* reserved (poster time) */
1568 avio_wb32(pb, 0); /* reserved (selection time) */
1569 avio_wb32(pb, 0); /* reserved (selection duration) */
1570 avio_wb32(pb, 0); /* reserved (current time) */
b7d3dd90 1571 avio_wb32(pb, max_track_id + 1); /* Next track id */
1cb5f7fd
MN
1572 return 0x6c;
1573}
1574
ae628ec1 1575static int mov_write_itunes_hdlr_tag(AVIOContext *pb, MOVMuxContext *mov,
b6c50eb1
PB
1576 AVFormatContext *s)
1577{
77eb5504 1578 avio_wb32(pb, 33); /* size */
0abdb293 1579 ffio_wfourcc(pb, "hdlr");
77eb5504
AK
1580 avio_wb32(pb, 0);
1581 avio_wb32(pb, 0);
0abdb293
AK
1582 ffio_wfourcc(pb, "mdir");
1583 ffio_wfourcc(pb, "appl");
77eb5504
AK
1584 avio_wb32(pb, 0);
1585 avio_wb32(pb, 0);
1586 avio_w8(pb, 0);
6b174197 1587 return 33;
b6c50eb1
PB
1588}
1589
1590/* helper function to write a data tag with the specified string as data */
ae628ec1 1591static int mov_write_string_data_tag(AVIOContext *pb, const char *data, int lang, int long_style)
b6c50eb1 1592{
efda3395 1593 if(long_style){
6b174197 1594 int size = 16 + strlen(data);
77eb5504 1595 avio_wb32(pb, size); /* size */
0abdb293 1596 ffio_wfourcc(pb, "data");
77eb5504
AK
1597 avio_wb32(pb, 1);
1598 avio_wb32(pb, 0);
1599 avio_write(pb, data, strlen(data));
6b174197 1600 return size;
efda3395 1601 }else{
fe3ab8ad
BC
1602 if (!lang)
1603 lang = ff_mov_iso639_to_lang("und", 1);
77eb5504
AK
1604 avio_wb16(pb, strlen(data)); /* string length */
1605 avio_wb16(pb, lang);
1606 avio_write(pb, data, strlen(data));
efda3395 1607 return strlen(data) + 4;
b6c50eb1 1608 }
b6c50eb1
PB
1609}
1610
ae628ec1 1611static int mov_write_string_tag(AVIOContext *pb, const char *name, const char *value, int lang, int long_style){
b6c50eb1 1612 int size = 0;
91208916 1613 if (value && value[0]) {
a2704c97 1614 int64_t pos = avio_tell(pb);
77eb5504 1615 avio_wb32(pb, 0); /* size */
0abdb293 1616 ffio_wfourcc(pb, name);
d9fc9ff3 1617 mov_write_string_data_tag(pb, value, lang, long_style);
b7d3dd90 1618 size = update_size(pb, pos);
b6c50eb1
PB
1619 }
1620 return size;
1621}
1622
ae628ec1 1623static int mov_write_string_metadata(AVFormatContext *s, AVIOContext *pb,
1ee2d448
AJ
1624 const char *name, const char *tag,
1625 int long_style)
b6c50eb1 1626{
d9fc9ff3 1627 int l, lang = 0, len, len2;
d2d67e42 1628 AVDictionaryEntry *t, *t2 = NULL;
d9fc9ff3 1629 char tag2[16];
1ee2d448 1630
d2d67e42 1631 if (!(t = av_dict_get(s->metadata, tag, NULL, 0)))
efda3395 1632 return 0;
1ee2d448 1633
d9fc9ff3
AJ
1634 len = strlen(t->key);
1635 snprintf(tag2, sizeof(tag2), "%s-", tag);
d2d67e42 1636 while ((t2 = av_dict_get(s->metadata, tag2, t2, AV_DICT_IGNORE_SUFFIX))) {
d9fc9ff3
AJ
1637 len2 = strlen(t2->key);
1638 if (len2 == len+4 && !strcmp(t->value, t2->value)
cc255afe 1639 && (l=ff_mov_iso639_to_lang(&t2->key[len2-3], 1)) >= 0) {
d9fc9ff3
AJ
1640 lang = l;
1641 break;
1642 }
1643 }
1644 return mov_write_string_tag(pb, name, t->value, lang, long_style);
b6c50eb1
PB
1645}
1646
1647/* iTunes track number */
ae628ec1 1648static int mov_write_trkn_tag(AVIOContext *pb, MOVMuxContext *mov,
b6c50eb1
PB
1649 AVFormatContext *s)
1650{
d2d67e42 1651 AVDictionaryEntry *t = av_dict_get(s->metadata, "track", NULL, 0);
1ee2d448
AJ
1652 int size = 0, track = t ? atoi(t->value) : 0;
1653 if (track) {
77eb5504 1654 avio_wb32(pb, 32); /* size */
0abdb293 1655 ffio_wfourcc(pb, "trkn");
77eb5504 1656 avio_wb32(pb, 24); /* size */
0abdb293 1657 ffio_wfourcc(pb, "data");
77eb5504
AK
1658 avio_wb32(pb, 0); // 8 bytes empty
1659 avio_wb32(pb, 0);
1660 avio_wb16(pb, 0); // empty
1661 avio_wb16(pb, track); // track number
1662 avio_wb16(pb, 0); // total track number
1663 avio_wb16(pb, 0); // empty
6b174197 1664 size = 32;
b6c50eb1
PB
1665 }
1666 return size;
1667}
1668
1669/* iTunes meta data list */
ae628ec1 1670static int mov_write_ilst_tag(AVIOContext *pb, MOVMuxContext *mov,
b6c50eb1
PB
1671 AVFormatContext *s)
1672{
a2704c97 1673 int64_t pos = avio_tell(pb);
77eb5504 1674 avio_wb32(pb, 0); /* size */
0abdb293 1675 ffio_wfourcc(pb, "ilst");
1ee2d448 1676 mov_write_string_metadata(s, pb, "\251nam", "title" , 1);
ea4c4d7f 1677 mov_write_string_metadata(s, pb, "\251ART", "artist" , 1);
2cf9c7ef 1678 mov_write_string_metadata(s, pb, "aART", "album_artist", 1);
620af1a1 1679 mov_write_string_metadata(s, pb, "\251wrt", "composer" , 1);
1ee2d448 1680 mov_write_string_metadata(s, pb, "\251alb", "album" , 1);
ca76a119 1681 mov_write_string_metadata(s, pb, "\251day", "date" , 1);
d9fc9ff3 1682 mov_write_string_tag(pb, "\251too", LIBAVFORMAT_IDENT, 0, 1);
1ee2d448
AJ
1683 mov_write_string_metadata(s, pb, "\251cmt", "comment" , 1);
1684 mov_write_string_metadata(s, pb, "\251gen", "genre" , 1);
1685 mov_write_string_metadata(s, pb, "\251cpy", "copyright", 1);
2cf9c7ef
MM
1686 mov_write_string_metadata(s, pb, "\251grp", "grouping" , 1);
1687 mov_write_string_metadata(s, pb, "\251lyr", "lyrics" , 1);
7382902b
DC
1688 mov_write_string_metadata(s, pb, "desc", "description",1);
1689 mov_write_string_metadata(s, pb, "ldes", "synopsis" , 1);
1690 mov_write_string_metadata(s, pb, "tvsh", "show" , 1);
1691 mov_write_string_metadata(s, pb, "tven", "episode_id",1);
1692 mov_write_string_metadata(s, pb, "tvnn", "network" , 1);
b6c50eb1 1693 mov_write_trkn_tag(pb, mov, s);
b7d3dd90 1694 return update_size(pb, pos);
b6c50eb1
PB
1695}
1696
1697/* iTunes meta data tag */
ae628ec1 1698static int mov_write_meta_tag(AVIOContext *pb, MOVMuxContext *mov,
b6c50eb1
PB
1699 AVFormatContext *s)
1700{
1701 int size = 0;
a2704c97 1702 int64_t pos = avio_tell(pb);
77eb5504 1703 avio_wb32(pb, 0); /* size */
0abdb293 1704 ffio_wfourcc(pb, "meta");
77eb5504 1705 avio_wb32(pb, 0);
b97fb809
AJ
1706 mov_write_itunes_hdlr_tag(pb, mov, s);
1707 mov_write_ilst_tag(pb, mov, s);
b7d3dd90 1708 size = update_size(pb, pos);
b6c50eb1
PB
1709 return size;
1710}
115329f1 1711
07b7b06d
BC
1712static int utf8len(const uint8_t *b)
1713{
1714 int len=0;
1715 int val;
1716 while(*b){
1717 GET_UTF8(val, *b++, return -1;)
1718 len++;
1719 }
1720 return len;
1721}
1722
ae628ec1 1723static int ascii_to_wc(AVIOContext *pb, const uint8_t *b)
07b7b06d
BC
1724{
1725 int val;
1726 while(*b){
1727 GET_UTF8(val, *b++, return -1;)
77eb5504 1728 avio_wb16(pb, val);
07b7b06d 1729 }
77eb5504 1730 avio_wb16(pb, 0x00);
07b7b06d
BC
1731 return 0;
1732}
1733
1734static uint16_t language_code(const char *str)
1735{
1736 return (((str[0]-0x60) & 0x1F) << 10) + (((str[1]-0x60) & 0x1F) << 5) + ((str[2]-0x60) & 0x1F);
1737}
1738
ae628ec1 1739static int mov_write_3gp_udta_tag(AVIOContext *pb, AVFormatContext *s,
c6e2c6c9
BC
1740 const char *tag, const char *str)
1741{
a2704c97 1742 int64_t pos = avio_tell(pb);
d2d67e42 1743 AVDictionaryEntry *t = av_dict_get(s->metadata, str, NULL, 0);
1ee2d448 1744 if (!t || !utf8len(t->value))
c6e2c6c9 1745 return 0;
77eb5504 1746 avio_wb32(pb, 0); /* size */
0abdb293 1747 ffio_wfourcc(pb, tag); /* type */
77eb5504 1748 avio_wb32(pb, 0); /* version + flags */
c6e2c6c9 1749 if (!strcmp(tag, "yrrc"))
77eb5504 1750 avio_wb16(pb, atoi(t->value));
c6e2c6c9 1751 else {
77eb5504
AK
1752 avio_wb16(pb, language_code("eng")); /* language */
1753 avio_write(pb, t->value, strlen(t->value)+1); /* UTF8 string value */
1ee2d448 1754 if (!strcmp(tag, "albm") &&
d2d67e42 1755 (t = av_dict_get(s->metadata, "track", NULL, 0)))
77eb5504 1756 avio_w8(pb, atoi(t->value));
c6e2c6c9 1757 }
b7d3dd90 1758 return update_size(pb, pos);
c6e2c6c9
BC
1759}
1760
ae628ec1 1761static int mov_write_chpl_tag(AVIOContext *pb, AVFormatContext *s)
dc75e4e3 1762{
a2704c97 1763 int64_t pos = avio_tell(pb);
dc75e4e3
DC
1764 int i, nb_chapters = FFMIN(s->nb_chapters, 255);
1765
77eb5504 1766 avio_wb32(pb, 0); // size
0abdb293 1767 ffio_wfourcc(pb, "chpl");
77eb5504
AK
1768 avio_wb32(pb, 0x01000000); // version + flags
1769 avio_wb32(pb, 0); // unknown
1770 avio_w8(pb, nb_chapters);
dc75e4e3
DC
1771
1772 for (i = 0; i < nb_chapters; i++) {
1773 AVChapter *c = s->chapters[i];
d2d67e42 1774 AVDictionaryEntry *t;
77eb5504 1775 avio_wb64(pb, av_rescale_q(c->start, c->time_base, (AVRational){1,10000000}));
dc75e4e3 1776
d2d67e42 1777 if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
dc75e4e3 1778 int len = FFMIN(strlen(t->value), 255);
77eb5504
AK
1779 avio_w8(pb, len);
1780 avio_write(pb, t->value, len);
dc75e4e3 1781 } else
77eb5504 1782 avio_w8(pb, 0);
dc75e4e3 1783 }
b7d3dd90 1784 return update_size(pb, pos);
dc75e4e3
DC
1785}
1786
ae628ec1 1787static int mov_write_udta_tag(AVIOContext *pb, MOVMuxContext *mov,
69dde1ad
GB
1788 AVFormatContext *s)
1789{
ae628ec1 1790 AVIOContext *pb_buf;
1ee2d448
AJ
1791 int i, ret, size;
1792 uint8_t *buf;
89a0d790 1793
d1d87dfb
BC
1794 for (i = 0; i < s->nb_streams; i++)
1795 if (mov->tracks[i].enc->flags & CODEC_FLAG_BITEXACT) {
1ee2d448 1796 return 0;
d1d87dfb
BC
1797 }
1798
b92c5452 1799 ret = avio_open_dyn_buf(&pb_buf);
1ee2d448
AJ
1800 if(ret < 0)
1801 return ret;
69dde1ad 1802
a04aec6d 1803 if (mov->mode & MODE_3GP) {
a4de2b6e 1804 mov_write_3gp_udta_tag(pb_buf, s, "perf", "artist");
1ee2d448
AJ
1805 mov_write_3gp_udta_tag(pb_buf, s, "titl", "title");
1806 mov_write_3gp_udta_tag(pb_buf, s, "auth", "author");
1807 mov_write_3gp_udta_tag(pb_buf, s, "gnre", "genre");
1808 mov_write_3gp_udta_tag(pb_buf, s, "dscp", "comment");
1809 mov_write_3gp_udta_tag(pb_buf, s, "albm", "album");
1810 mov_write_3gp_udta_tag(pb_buf, s, "cprt", "copyright");
ca76a119 1811 mov_write_3gp_udta_tag(pb_buf, s, "yrrc", "date");
ce072b2d 1812 } else if (mov->mode == MODE_MOV) { // the title field breaks gtkpod with mp4 and my suspicion is that stuff is not valid in mp4
96d1e75a 1813 mov_write_string_metadata(s, pb_buf, "\251ART", "artist" , 0);
1ee2d448
AJ
1814 mov_write_string_metadata(s, pb_buf, "\251nam", "title" , 0);
1815 mov_write_string_metadata(s, pb_buf, "\251aut", "author" , 0);
1816 mov_write_string_metadata(s, pb_buf, "\251alb", "album" , 0);
ca76a119 1817 mov_write_string_metadata(s, pb_buf, "\251day", "date" , 0);
edd33cb6 1818 mov_write_string_metadata(s, pb_buf, "\251swr", "encoder" , 0);
1ee2d448
AJ
1819 mov_write_string_metadata(s, pb_buf, "\251des", "comment" , 0);
1820 mov_write_string_metadata(s, pb_buf, "\251gen", "genre" , 0);
1821 mov_write_string_metadata(s, pb_buf, "\251cpy", "copyright" , 0);
c6e2c6c9 1822 } else {
283c9a8e 1823 /* iTunes meta data */
1ee2d448 1824 mov_write_meta_tag(pb_buf, mov, s);
c6e2c6c9 1825 }
1ee2d448 1826
dc75e4e3
DC
1827 if (s->nb_chapters)
1828 mov_write_chpl_tag(pb_buf, s);
1829
6dc7d80d 1830 if ((size = avio_close_dyn_buf(pb_buf, &buf)) > 0) {
77eb5504 1831 avio_wb32(pb, size+8);
0abdb293 1832 ffio_wfourcc(pb, "udta");
77eb5504 1833 avio_write(pb, buf, size);
89a0d790 1834 }
8b6e0aec 1835 av_free(buf);
89a0d790
BF
1836
1837 return 0;
69dde1ad
GB
1838}
1839
ae628ec1 1840static void mov_write_psp_udta_tag(AVIOContext *pb,
20e22af8
BC
1841 const char *str, const char *lang, int type)
1842{
1843 int len = utf8len(str)+1;
1844 if(len<=0)
d32d6def 1845 return;
77eb5504
AK
1846 avio_wb16(pb, len*2+10); /* size */
1847 avio_wb32(pb, type); /* type */
1848 avio_wb16(pb, language_code(lang)); /* language */
1849 avio_wb16(pb, 0x01); /* ? */
20e22af8 1850 ascii_to_wc(pb, str);
20e22af8
BC
1851}
1852
ae628ec1 1853static int mov_write_uuidusmt_tag(AVIOContext *pb, AVFormatContext *s)
dcfdb046 1854{
d2d67e42 1855 AVDictionaryEntry *title = av_dict_get(s->metadata, "title", NULL, 0);
bc5c918e 1856 int64_t pos, pos2;
dcfdb046 1857
1ee2d448 1858 if (title) {
a2704c97 1859 pos = avio_tell(pb);
77eb5504 1860 avio_wb32(pb, 0); /* size placeholder*/
0abdb293
AK
1861 ffio_wfourcc(pb, "uuid");
1862 ffio_wfourcc(pb, "USMT");
77eb5504
AK
1863 avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
1864 avio_wb32(pb, 0xbb88695c);
1865 avio_wb32(pb, 0xfac9c740);
dcfdb046 1866
a2704c97 1867 pos2 = avio_tell(pb);
77eb5504 1868 avio_wb32(pb, 0); /* size placeholder*/
0abdb293 1869 ffio_wfourcc(pb, "MTDT");
77eb5504 1870 avio_wb16(pb, 4);
dcfdb046 1871
7cf0e16f 1872 // ?
77eb5504
AK
1873 avio_wb16(pb, 0x0C); /* size */
1874 avio_wb32(pb, 0x0B); /* type */
1875 avio_wb16(pb, language_code("und")); /* language */
1876 avio_wb16(pb, 0x0); /* ? */
1877 avio_wb16(pb, 0x021C); /* data */
7cf0e16f 1878
d32d6def 1879 mov_write_psp_udta_tag(pb, LIBAVCODEC_IDENT, "eng", 0x04);
1ee2d448 1880 mov_write_psp_udta_tag(pb, title->value, "eng", 0x01);
7cf0e16f 1881// 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);
d32d6def
BC
1882 mov_write_psp_udta_tag(pb, "2006/04/01 11:11:11", "und", 0x03);
1883
b7d3dd90
MS
1884 update_size(pb, pos2);
1885 return update_size(pb, pos);
dcfdb046
BL
1886 }
1887
d32d6def 1888 return 0;
dcfdb046
BL
1889}
1890
ae628ec1 1891static int mov_write_moov_tag(AVIOContext *pb, MOVMuxContext *mov,
69dde1ad 1892 AVFormatContext *s)
1cb5f7fd 1893{
b29af723 1894 int i;
a2704c97 1895 int64_t pos = avio_tell(pb);
77eb5504 1896 avio_wb32(pb, 0); /* size placeholder*/
0abdb293 1897 ffio_wfourcc(pb, "moov");
1cb5f7fd 1898
42fb4148 1899 for (i=0; i<mov->nb_streams; i++) {
83988d58
MS
1900 if (mov->tracks[i].entry <= 0 && !(mov->flags & FF_MOV_FLAG_FRAGMENT))
1901 continue;
e45ccf79 1902
e45ccf79 1903 mov->tracks[i].time = mov->time;
b7d3dd90 1904 mov->tracks[i].track_id = i+1;
1cb5f7fd
MN
1905 }
1906
ddb63017
DC
1907 if (mov->chapter_track)
1908 for (i=0; i<s->nb_streams; i++) {
1909 mov->tracks[i].tref_tag = MKTAG('c','h','a','p');
b7d3dd90 1910 mov->tracks[i].tref_id = mov->tracks[mov->chapter_track].track_id;
ddb63017 1911 }
e977af6f
MS
1912 for (i = 0; i < mov->nb_streams; i++) {
1913 if (mov->tracks[i].tag == MKTAG('r','t','p',' ')) {
1914 mov->tracks[i].tref_tag = MKTAG('h','i','n','t');
1915 mov->tracks[i].tref_id =
b7d3dd90 1916 mov->tracks[mov->tracks[i].src_track].track_id;
e977af6f
MS
1917 }
1918 }
ddb63017 1919
6e6d6dc0 1920 mov_write_mvhd_tag(pb, mov);
d3b8bde2
AC
1921 if (mov->mode != MODE_MOV && !mov->iods_skip)
1922 mov_write_iods_tag(pb, mov);
42fb4148 1923 for (i=0; i<mov->nb_streams; i++) {
83988d58
MS
1924 if (mov->tracks[i].entry > 0 || mov->flags & FF_MOV_FLAG_FRAGMENT) {
1925 mov_write_trak_tag(pb, mov, &(mov->tracks[i]), i < s->nb_streams ? s->streams[i] : NULL);
1cb5f7fd
MN
1926 }
1927 }
83988d58
MS
1928 if (mov->flags & FF_MOV_FLAG_FRAGMENT)
1929 mov_write_mvex_tag(pb, mov); /* QuickTime requires trak to precede this */
1cb5f7fd 1930
dcfdb046
BL
1931 if (mov->mode == MODE_PSP)
1932 mov_write_uuidusmt_tag(pb, s);
c6e2c6c9 1933 else
e344c1ea 1934 mov_write_udta_tag(pb, mov, s);
69dde1ad 1935
b7d3dd90 1936 return update_size(pb, pos);
1cb5f7fd
MN
1937}
1938
f5322104
MS
1939static void param_write_int(AVIOContext *pb, const char *name, int value)
1940{
1941 avio_printf(pb, "<param name=\"%s\" value=\"%d\" valuetype=\"data\"/>\n", name, value);
1942}
1943
1944static void param_write_string(AVIOContext *pb, const char *name, const char *value)
1945{
1946 avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, value);
1947}
1948
1949static void param_write_hex(AVIOContext *pb, const char *name, const uint8_t *value, int len)
1950{
1951 char buf[150];
1952 len = FFMIN(sizeof(buf)/2 - 1, len);
1953 ff_data_to_hex(buf, value, len, 0);
1954 buf[2*len] = '\0';
1955 avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, buf);
1956}
1957
1958static void write_h264_extradata(AVIOContext *pb, AVCodecContext *enc)
1959{
1960 uint16_t sps_size, pps_size, len;
1961 char buf[150];
1962 sps_size = AV_RB16(&enc->extradata[6]);
1963 if (11 + sps_size > enc->extradata_size)
1964 return;
1965 pps_size = AV_RB16(&enc->extradata[9 + sps_size]);
1966 if (11 + sps_size + pps_size > enc->extradata_size)
1967 return;
1968 len = FFMIN(sizeof(buf)/2 - 1, sps_size);
1969 ff_data_to_hex(buf, &enc->extradata[8], len, 0);
1970 buf[2*len] = '\0';
1971 avio_printf(pb, "<param name=\"CodecPrivateData\" value=\"00000001%s", buf);
1972 len = FFMIN(sizeof(buf)/2 - 1, pps_size);
1973 ff_data_to_hex(buf, &enc->extradata[11 + sps_size], len, 0);
1974 buf[2*len] = '\0';
1975 avio_printf(pb, "00000001%s\" valuetype=\"data\"/>\n", buf);
1976}
1977
1978static int mov_write_isml_manifest(AVIOContext *pb, MOVMuxContext *mov)
1979{
1980 int64_t pos = avio_tell(pb);
1981 int i;
1982 const uint8_t uuid[] = {
1983 0xa5, 0xd4, 0x0b, 0x30, 0xe8, 0x14, 0x11, 0xdd,
1984 0xba, 0x2f, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66
1985 };
1986
1987 avio_wb32(pb, 0);
1988 ffio_wfourcc(pb, "uuid");
1989 avio_write(pb, uuid, sizeof(uuid));
1990 avio_wb32(pb, 0);
1991
1992 avio_printf(pb, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
1993 avio_printf(pb, "<smil xmlns=\"http://www.w3.org/2001/SMIL20/Language\">\n");
1994 avio_printf(pb, "<head>\n");
1995 avio_printf(pb, "<meta name=\"creator\" content=\"%s\" />\n",
1996 LIBAVFORMAT_IDENT);
1997 avio_printf(pb, "</head>\n");
1998 avio_printf(pb, "<body>\n");
1999 avio_printf(pb, "<switch>\n");
2000 for (i = 0; i < mov->nb_streams; i++) {
2001 MOVTrack *track = &mov->tracks[i];
2002 const char *type;
b7d3dd90 2003 /* track->track_id is initialized in write_moov, and thus isn't known
f5322104
MS
2004 * here yet */
2005 int track_id = i + 1;
2006
2007 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
2008 type = "video";
2009 } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
2010 type = "audio";
2011 } else {
2012 continue;
2013 }
2014 avio_printf(pb, "<%s systemBitrate=\"%d\">\n", type,
2015 track->enc->bit_rate);
2016 param_write_int(pb, "systemBitrate", track->enc->bit_rate);
2017 param_write_int(pb, "trackID", track_id);
2018 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
2019 if (track->enc->codec_id == CODEC_ID_H264 &&
2020 track->enc->extradata_size >= 11 &&
2021 track->enc->extradata[0] == 1) {
2022 write_h264_extradata(pb, track->enc);
2023 } else {
2024 param_write_hex(pb, "CodecPrivateData", track->enc->extradata,
2025 track->enc->extradata_size);
2026 }
2027 if (track->enc->codec_id == CODEC_ID_H264) {
2028 param_write_string(pb, "FourCC", "H264");
2029 } else if (track->enc->codec_id == CODEC_ID_VC1) {
2030 param_write_string(pb, "FourCC", "WVC1");
2031 }
2032 param_write_int(pb, "MaxWidth", track->enc->width);
2033 param_write_int(pb, "MaxHeight", track->enc->height);
2034 param_write_int(pb, "DisplayWidth", track->enc->width);
2035 param_write_int(pb, "DisplayHeight", track->enc->height);
2036 } else {
2037 if (track->enc->codec_id == CODEC_ID_AAC) {
2038 param_write_string(pb, "FourCC", "AACL");
2039 } else if (track->enc->codec_id == CODEC_ID_WMAPRO) {
2040 param_write_string(pb, "FourCC", "WMAP");
2041 }
2042 param_write_hex(pb, "CodecPrivateData", track->enc->extradata,
2043 track->enc->extradata_size);
2044 param_write_int(pb, "AudioTag", ff_codec_get_tag(ff_codec_wav_tags,
2045 track->enc->codec_id));
2046 param_write_int(pb, "Channels", track->enc->channels);
2047 param_write_int(pb, "SamplingRate", track->enc->sample_rate);
2048 param_write_int(pb, "BitsPerSample", 16);
2049 param_write_int(pb, "PacketSize", track->enc->block_align ?
2050 track->enc->block_align : 4);
2051 }
2052 avio_printf(pb, "</%s>\n", type);
2053 }
2054 avio_printf(pb, "</switch>\n");
2055 avio_printf(pb, "</body>\n");
2056 avio_printf(pb, "</smil>\n");
2057
b7d3dd90 2058 return update_size(pb, pos);
f5322104
MS
2059}
2060
83988d58
MS
2061static int mov_write_mfhd_tag(AVIOContext *pb, MOVMuxContext *mov)
2062{
2063 avio_wb32(pb, 16);
2064 ffio_wfourcc(pb, "mfhd");
2065 avio_wb32(pb, 0);
2066 avio_wb32(pb, mov->fragments);
2067 return 0;
2068}
2069
2070static int mov_write_tfhd_tag(AVIOContext *pb, MOVTrack *track,
2071 int64_t moof_offset)
2072{
2073 int64_t pos = avio_tell(pb);
2074 /* default-sample-size + default-sample-duration + base-data-offset */
2075 uint32_t flags = 0x19;
2076 if (!track->entry) {
2077 flags |= 0x010000; /* duration-is-empty */
2078 } else {
2079 flags |= 0x20; /* default-sample-flags-present */
2080 }
2081
4ddd54da
MS
2082 /* Don't set a default sample size when creating data for silverlight,
2083 * the player refuses to play files with that set. */
2084 if (track->mode == MODE_ISM)
2085 flags &= ~0x10;
2086
83988d58
MS
2087 avio_wb32(pb, 0); /* size placeholder */
2088 ffio_wfourcc(pb, "tfhd");
2089 avio_w8(pb, 0); /* version */
2090 avio_wb24(pb, flags);
2091
b7d3dd90 2092 avio_wb32(pb, track->track_id); /* track-id */
83988d58
MS
2093 if (flags & 0x01)
2094 avio_wb64(pb, moof_offset);
2095 if (flags & 0x08) {
2096 track->default_duration = track->audio_vbr ? track->enc->frame_size : 1;
2097 avio_wb32(pb, track->default_duration);
2098 }
2099 if (flags & 0x10) {
2100 track->default_size = track->entry ? track->cluster[0].size : 1;
2101 avio_wb32(pb, track->default_size);
2102 } else
2103 track->default_size = -1;
2104
2105 if (flags & 0x20) {
2106 track->default_sample_flags =
2107 track->enc->codec_type == AVMEDIA_TYPE_VIDEO ?
2108 0x01010000 : 0x02000000;
2109 avio_wb32(pb, track->default_sample_flags);
2110 }
2111
b7d3dd90 2112 return update_size(pb, pos);
83988d58
MS
2113}
2114
2115static uint32_t get_sample_flags(MOVTrack *track, MOVIentry *entry)
2116{
2117 return entry->flags & MOV_SYNC_SAMPLE ? 0x02000000 : 0x01010000;
2118}
2119
2120static int mov_write_trun_tag(AVIOContext *pb, MOVTrack *track)
2121{
2122 int64_t pos = avio_tell(pb);
2123 uint32_t flags = 1; /* data-offset-present */
2124 int i;
2125
2126 for (i = 0; i < track->entry; i++) {
2127 int64_t duration = i + 1 == track->entry ?
b7d3dd90 2128 track->track_duration - track->cluster[i].dts + track->start_dts :
83988d58
MS
2129 track->cluster[i + 1].dts - track->cluster[i].dts;
2130 if (duration != track->default_duration)
2131 flags |= 0x100; /* sample-duration-present */
2132 if (track->cluster[i].size != track->default_size)
2133 flags |= 0x200; /* sample-size-present */
2134 if (i > 0 && get_sample_flags(track, &track->cluster[i]) != track->default_sample_flags)
2135 flags |= 0x400; /* sample-flags-present */
2136 }
2137 if (!(flags & 0x400))
2138 flags |= 0x4; /* first-sample-flags-present */
2139 if (track->flags & MOV_TRACK_CTTS)
2140 flags |= 0x800; /* sample-composition-time-offsets-present */
2141
2142 avio_wb32(pb, 0); /* size placeholder */
2143 ffio_wfourcc(pb, "trun");
2144 avio_w8(pb, 0); /* version */
2145 avio_wb24(pb, flags);
2146
2147 avio_wb32(pb, track->entry); /* sample count */
2148 track->moof_size_offset = avio_tell(pb);
2149 avio_wb32(pb, 0); /* data offset */
2150 if (flags & 0x4) /* first sample flags */
2151 avio_wb32(pb, get_sample_flags(track, &track->cluster[0]));
2152
2153 for (i = 0; i < track->entry; i++) {
2154 int64_t duration = i + 1 == track->entry ?
b7d3dd90 2155 track->track_duration - track->cluster[i].dts + track->start_dts :
83988d58
MS
2156 track->cluster[i + 1].dts - track->cluster[i].dts;
2157 if (flags & 0x100)
2158 avio_wb32(pb, duration);
2159 if (flags & 0x200)
2160 avio_wb32(pb, track->cluster[i].size);
2161 if (flags & 0x400)
2162 avio_wb32(pb, get_sample_flags(track, &track->cluster[i]));
2163 if (flags & 0x800)
2164 avio_wb32(pb, track->cluster[i].cts);
2165 }
2166
b7d3dd90 2167 return update_size(pb, pos);
83988d58
MS
2168}
2169
4ddd54da
MS
2170static int mov_write_tfxd_tag(AVIOContext *pb, MOVTrack *track)
2171{
2172 int64_t pos = avio_tell(pb);
2173 const uint8_t uuid[] = {
2174 0x6d, 0x1d, 0x9b, 0x05, 0x42, 0xd5, 0x44, 0xe6,
2175 0x80, 0xe2, 0x14, 0x1d, 0xaf, 0xf7, 0x57, 0xb2
2176 };
2177
2178 avio_wb32(pb, 0); /* size placeholder */
2179 ffio_wfourcc(pb, "uuid");
2180 avio_write(pb, uuid, sizeof(uuid));
2181 avio_w8(pb, 1);
2182 avio_wb24(pb, 0);
2183 avio_wb64(pb, track->frag_start);
b7d3dd90 2184 avio_wb64(pb, track->start_dts + track->track_duration -
4ddd54da
MS
2185 track->cluster[0].dts);
2186
b7d3dd90 2187 return update_size(pb, pos);
4ddd54da
MS
2188}
2189
2190static int mov_write_tfrf_tag(AVIOContext *pb, MOVMuxContext *mov,
2191 MOVTrack *track, int entry)
2192{
2193 int n = track->nb_frag_info - 1 - entry, i;
2194 int size = 8 + 16 + 4 + 1 + 16*n;
2195 const uint8_t uuid[] = {
2196 0xd4, 0x80, 0x7e, 0xf2, 0xca, 0x39, 0x46, 0x95,
2197 0x8e, 0x54, 0x26, 0xcb, 0x9e, 0x46, 0xa7, 0x9f
2198 };
2199
2200 if (entry < 0)
2201 return 0;
2202
2203 avio_seek(pb, track->frag_info[entry].tfrf_offset, SEEK_SET);
2204 avio_wb32(pb, size);
2205 ffio_wfourcc(pb, "uuid");
2206 avio_write(pb, uuid, sizeof(uuid));
2207 avio_w8(pb, 1);
2208 avio_wb24(pb, 0);
2209 avio_w8(pb, n);
2210 for (i = 0; i < n; i++) {
2211 int index = entry + 1 + i;
2212 avio_wb64(pb, track->frag_info[index].time);
2213 avio_wb64(pb, track->frag_info[index].duration);
2214 }
2215 if (n < mov->ism_lookahead) {
2216 int free_size = 16*(mov->ism_lookahead - n);
2217 avio_wb32(pb, free_size);
2218 ffio_wfourcc(pb, "free");
2219 for (i = 0; i < free_size - 8; i++)
2220 avio_w8(pb, 0);
2221 }
2222
2223 return 0;
2224}
2225
2226static int mov_write_tfrf_tags(AVIOContext *pb, MOVMuxContext *mov,
2227 MOVTrack *track)
2228{
2229 int64_t pos = avio_tell(pb);
2230 int i;
2231 for (i = 0; i < mov->ism_lookahead; i++) {
2232 /* Update the tfrf tag for the last ism_lookahead fragments,
2233 * nb_frag_info - 1 is the next fragment to be written. */
2234 mov_write_tfrf_tag(pb, mov, track, track->nb_frag_info - 2 - i);
2235 }
2236 avio_seek(pb, pos, SEEK_SET);
2237 return 0;
2238}
2239
2240static int mov_write_traf_tag(AVIOContext *pb, MOVMuxContext *mov,
2241 MOVTrack *track, int64_t moof_offset)
83988d58
MS
2242{
2243 int64_t pos = avio_tell(pb);
2244 avio_wb32(pb, 0); /* size placeholder */
2245 ffio_wfourcc(pb, "traf");
2246
2247 mov_write_tfhd_tag(pb, track, moof_offset);
2248 mov_write_trun_tag(pb, track);
4ddd54da
MS
2249 if (mov->mode == MODE_ISM) {
2250 mov_write_tfxd_tag(pb, track);
2251
2252 if (mov->ism_lookahead) {
2253 int i, size = 16 + 4 + 1 + 16*mov->ism_lookahead;
2254
2255 track->tfrf_offset = avio_tell(pb);
2256 avio_wb32(pb, 8 + size);
2257 ffio_wfourcc(pb, "free");
2258 for (i = 0; i < size; i++)
2259 avio_w8(pb, 0);
2260 }
2261 }
83988d58 2262
b7d3dd90 2263 return update_size(pb, pos);
83988d58
MS
2264}
2265
2266static int mov_write_moof_tag(AVIOContext *pb, MOVMuxContext *mov, int tracks)
2267{
2268 int64_t pos = avio_tell(pb), end;
2269 int i, moof_size;
2270
2271 avio_wb32(pb, 0); /* size placeholder */
2272 ffio_wfourcc(pb, "moof");
2273
2274 mov_write_mfhd_tag(pb, mov);
2275 for (i = 0; i < mov->nb_streams; i++) {
2276 MOVTrack *track = &mov->tracks[i];
2277 if (tracks >= 0 && i != tracks)
2278 continue;
2279 if (!track->entry)
2280 continue;
4ddd54da 2281 mov_write_traf_tag(pb, mov, track, pos);
83988d58
MS
2282 }
2283
2284 end = avio_tell(pb);
2285 moof_size = end - pos;
2286 for (i = 0; i < mov->nb_streams; i++) {
2287 MOVTrack *track = &mov->tracks[i];
2288 if (tracks >= 0 && i != tracks)
2289 continue;
2290 if (!track->entry)
2291 continue;
2292 avio_seek(pb, mov->tracks[i].moof_size_offset, SEEK_SET);
2293 avio_wb32(pb, moof_size + 8 + mov->tracks[i].data_offset);
2294 }
2295 avio_seek(pb, end, SEEK_SET);
2296
b7d3dd90 2297 return update_size(pb, pos);
83988d58
MS
2298}
2299
2300static int mov_write_tfra_tag(AVIOContext *pb, MOVTrack *track)
2301{
2302 int64_t pos = avio_tell(pb);
2303 int i;
2304
2305 avio_wb32(pb, 0); /* size placeholder */
2306 ffio_wfourcc(pb, "tfra");
2307 avio_w8(pb, 1); /* version */
2308 avio_wb24(pb, 0);
2309
b7d3dd90 2310 avio_wb32(pb, track->track_id);
83988d58
MS
2311 avio_wb32(pb, 0); /* length of traf/trun/sample num */
2312 avio_wb32(pb, track->nb_frag_info);
2313 for (i = 0; i < track->nb_frag_info; i++) {
2314 avio_wb64(pb, track->frag_info[i].time);
2315 avio_wb64(pb, track->frag_info[i].offset);
2316 avio_w8(pb, 1); /* traf number */
2317 avio_w8(pb, 1); /* trun number */
2318 avio_w8(pb, 1); /* sample number */
2319 }
2320
b7d3dd90 2321 return update_size(pb, pos);
83988d58
MS
2322}
2323
2324static int mov_write_mfra_tag(AVIOContext *pb, MOVMuxContext *mov)
2325{
2326 int64_t pos = avio_tell(pb);
2327 int i;
2328
2329 avio_wb32(pb, 0); /* size placeholder */
2330 ffio_wfourcc(pb, "mfra");
f5322104
MS
2331 /* An empty mfra atom is enough to indicate to the publishing point that
2332 * the stream has ended. */
2333 if (mov->flags & FF_MOV_FLAG_ISML)
b7d3dd90 2334 return update_size(pb, pos);
83988d58
MS
2335
2336 for (i = 0; i < mov->nb_streams; i++) {
2337 MOVTrack *track = &mov->tracks[i];
2338 if (track->nb_frag_info)
2339 mov_write_tfra_tag(pb, track);
2340 }
2341
2342 avio_wb32(pb, 16);
2343 ffio_wfourcc(pb, "mfro");
2344 avio_wb32(pb, 0); /* version + flags */
2345 avio_wb32(pb, avio_tell(pb) + 4 - pos);
2346
b7d3dd90 2347 return update_size(pb, pos);
83988d58
MS
2348}
2349
ae628ec1 2350static int mov_write_mdat_tag(AVIOContext *pb, MOVMuxContext *mov)
1cb5f7fd 2351{
77eb5504 2352 avio_wb32(pb, 8); // placeholder for extended size field (64 bit)
0abdb293 2353 ffio_wfourcc(pb, mov->mode == MODE_MOV ? "wide" : "free");
b29af723 2354
a2704c97 2355 mov->mdat_pos = avio_tell(pb);
77eb5504 2356 avio_wb32(pb, 0); /* size placeholder*/
0abdb293 2357 ffio_wfourcc(pb, "mdat");
1cb5f7fd
MN
2358 return 0;
2359}
2360
2361/* TODO: This needs to be more general */
ae628ec1 2362static int mov_write_ftyp_tag(AVIOContext *pb, AVFormatContext *s)
1cb5f7fd 2363{
2d243fb3 2364 MOVMuxContext *mov = s->priv_data;
a2704c97 2365 int64_t pos = avio_tell(pb);
cd70d17f 2366 int has_h264 = 0, has_video = 0;
40f8675c 2367 int minor = 0x200;
c55cc074 2368 int i;
69dde1ad 2369
83579084
BC
2370 for (i = 0; i < s->nb_streams; i++) {
2371 AVStream *st = s->streams[i];
72415b2a 2372 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
cd70d17f 2373 has_video = 1;
83579084
BC
2374 if (st->codec->codec_id == CODEC_ID_H264)
2375 has_h264 = 1;
2376 }
2377
77eb5504 2378 avio_wb32(pb, 0); /* size */
0abdb293 2379 ffio_wfourcc(pb, "ftyp");
e45ccf79 2380
40f8675c 2381 if (mov->mode == MODE_3GP) {
0abdb293 2382 ffio_wfourcc(pb, has_h264 ? "3gp6" : "3gp4");
40f8675c
BC
2383 minor = has_h264 ? 0x100 : 0x200;
2384 } else if (mov->mode & MODE_3G2) {
0abdb293 2385 ffio_wfourcc(pb, has_h264 ? "3g2b" : "3g2a");
40f8675c
BC
2386 minor = has_h264 ? 0x20000 : 0x10000;
2387 }else if (mov->mode == MODE_PSP)
0abdb293 2388 ffio_wfourcc(pb, "MSNV");
c55cc074 2389 else if (mov->mode == MODE_MP4)
0abdb293 2390 ffio_wfourcc(pb, "isom");
cd70d17f 2391 else if (mov->mode == MODE_IPOD)
0abdb293 2392 ffio_wfourcc(pb, has_video ? "M4V ":"M4A ");
4ddd54da
MS
2393 else if (mov->mode == MODE_ISM)
2394 ffio_wfourcc(pb, "isml");
cd70d17f 2395 else
0abdb293 2396 ffio_wfourcc(pb, "qt ");
e45ccf79 2397
77eb5504 2398 avio_wb32(pb, minor);
e45ccf79 2399
6ba9ed95 2400 if(mov->mode == MODE_MOV)
0abdb293 2401 ffio_wfourcc(pb, "qt ");
4ddd54da
MS
2402 else if (mov->mode == MODE_ISM) {
2403 ffio_wfourcc(pb, "piff");
2404 ffio_wfourcc(pb, "iso2");
2405 } else {
0abdb293
AK
2406 ffio_wfourcc(pb, "isom");
2407 ffio_wfourcc(pb, "iso2");
83579084 2408 if(has_h264)
0abdb293 2409 ffio_wfourcc(pb, "avc1");
6ba9ed95 2410 }
ed6e47c2 2411
91208916 2412 if (mov->mode == MODE_3GP)
0abdb293 2413 ffio_wfourcc(pb, has_h264 ? "3gp6":"3gp4");
a04aec6d 2414 else if (mov->mode & MODE_3G2)
0abdb293 2415 ffio_wfourcc(pb, has_h264 ? "3g2b":"3g2a");
91208916 2416 else if (mov->mode == MODE_PSP)
0abdb293 2417 ffio_wfourcc(pb, "MSNV");
c55cc074 2418 else if (mov->mode == MODE_MP4)
0abdb293 2419 ffio_wfourcc(pb, "mp41");
b7d3dd90 2420 return update_size(pb, pos);
1cb5f7fd
MN
2421}
2422
ae628ec1 2423static void mov_write_uuidprof_tag(AVIOContext *pb, AVFormatContext *s)
8af18154 2424{
b7d3dd90
MS
2425 AVCodecContext *video_codec = s->streams[0]->codec;
2426 AVCodecContext *audio_codec = s->streams[1]->codec;
2427 int audio_rate = audio_codec->sample_rate;
2428 int frame_rate = ((video_codec->time_base.den) * (0x10000))/ (video_codec->time_base.num);
2429 int audio_kbitrate = audio_codec->bit_rate / 1000;
2430 int video_kbitrate = FFMIN(video_codec->bit_rate / 1000, 800 - audio_kbitrate);
8af18154 2431
77eb5504 2432 avio_wb32(pb, 0x94); /* size */
0abdb293
AK
2433 ffio_wfourcc(pb, "uuid");
2434 ffio_wfourcc(pb, "PROF");
8af18154 2435
77eb5504
AK
2436 avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
2437 avio_wb32(pb, 0xbb88695c);
2438 avio_wb32(pb, 0xfac9c740);
8af18154 2439
77eb5504
AK
2440 avio_wb32(pb, 0x0); /* ? */
2441 avio_wb32(pb, 0x3); /* 3 sections ? */
8af18154 2442
77eb5504 2443 avio_wb32(pb, 0x14); /* size */
0abdb293 2444 ffio_wfourcc(pb, "FPRF");
77eb5504
AK
2445 avio_wb32(pb, 0x0); /* ? */
2446 avio_wb32(pb, 0x0); /* ? */
2447 avio_wb32(pb, 0x0); /* ? */
8af18154 2448
77eb5504 2449 avio_wb32(pb, 0x2c); /* size */
0abdb293 2450 ffio_wfourcc(pb, "APRF");/* audio */
77eb5504
AK
2451 avio_wb32(pb, 0x0);
2452 avio_wb32(pb, 0x2); /* TrackID */
0abdb293 2453 ffio_wfourcc(pb, "mp4a");
77eb5504
AK
2454 avio_wb32(pb, 0x20f);
2455 avio_wb32(pb, 0x0);
2456 avio_wb32(pb, audio_kbitrate);
2457 avio_wb32(pb, audio_kbitrate);
b7d3dd90
MS
2458 avio_wb32(pb, audio_rate);
2459 avio_wb32(pb, audio_codec->channels);
77eb5504
AK
2460
2461 avio_wb32(pb, 0x34); /* size */
0abdb293 2462 ffio_wfourcc(pb, "VPRF"); /* video */
77eb5504
AK
2463 avio_wb32(pb, 0x0);
2464 avio_wb32(pb, 0x1); /* TrackID */
b7d3dd90 2465 if (video_codec->codec_id == CODEC_ID_H264) {
0abdb293 2466 ffio_wfourcc(pb, "avc1");
77eb5504
AK
2467 avio_wb16(pb, 0x014D);
2468 avio_wb16(pb, 0x0015);
87b041e0 2469 } else {
0abdb293 2470 ffio_wfourcc(pb, "mp4v");
77eb5504
AK
2471 avio_wb16(pb, 0x0000);
2472 avio_wb16(pb, 0x0103);
87b041e0 2473 }
77eb5504
AK
2474 avio_wb32(pb, 0x0);
2475 avio_wb32(pb, video_kbitrate);
2476 avio_wb32(pb, video_kbitrate);
b7d3dd90
MS
2477 avio_wb32(pb, frame_rate);
2478 avio_wb32(pb, frame_rate);
2479 avio_wb16(pb, video_codec->width);
2480 avio_wb16(pb, video_codec->height);
77eb5504 2481 avio_wb32(pb, 0x010001); /* ? */
8af18154 2482}
2483
e1316b19
BC
2484static int mov_parse_mpeg2_frame(AVPacket *pkt, uint32_t *flags)
2485{
2486 uint32_t c = -1;
2487 int i, closed_gop = 0;
2488
2489 for (i = 0; i < pkt->size - 4; i++) {
2490 c = (c<<8) + pkt->data[i];
2491 if (c == 0x1b8) { // gop
2492 closed_gop = pkt->data[i+4]>>6 & 0x01;
2493 } else if (c == 0x100) { // pic
2494 int temp_ref = (pkt->data[i+1]<<2) | (pkt->data[i+2]>>6);
2495 if (!temp_ref || closed_gop) // I picture is not reordered
2496 *flags = MOV_SYNC_SAMPLE;
2497 else
2498 *flags = MOV_PARTIAL_SYNC_SAMPLE;
2499 break;
2500 }
2501 }
2502 return 0;
2503}
2504
83988d58
MS
2505static int mov_flush_fragment(AVFormatContext *s)
2506{
2507 MOVMuxContext *mov = s->priv_data;
2508 int i, first_track = -1;
2509 int64_t mdat_size = 0;
2510
2511 if (!(mov->flags & FF_MOV_FLAG_FRAGMENT))
2512 return 0;
2513
2514 if (!(mov->flags & FF_MOV_FLAG_EMPTY_MOOV) && mov->fragments == 0) {
2515 int64_t pos = avio_tell(s->pb);
2516
2517 for (i = 0; i < mov->nb_streams; i++)
2518 if (!mov->tracks[i].entry)
2519 break;
2520 /* Don't write the initial moov unless all tracks have data */
2521 if (i < mov->nb_streams)
2522 return 0;
2523 avio_seek(s->pb, mov->mdat_pos, SEEK_SET);
2524 avio_wb32(s->pb, mov->mdat_size + 8);
2525 avio_seek(s->pb, pos, SEEK_SET);
2526 mov_write_moov_tag(s->pb, mov, s);
2527 mov->fragments++;
2528 mov->mdat_size = 0;
2529 for (i = 0; i < mov->nb_streams; i++) {
2530 if (mov->tracks[i].entry)
2531 mov->tracks[i].frag_start += mov->tracks[i].start_dts +
b7d3dd90 2532 mov->tracks[i].track_duration -
83988d58
MS
2533 mov->tracks[i].cluster[0].dts;
2534 mov->tracks[i].entry = 0;
2535 }
e771e6dd 2536 avio_flush(s->pb);
83988d58
MS
2537 return 0;
2538 }
2539
2540 for (i = 0; i < mov->nb_streams; i++) {
2541 MOVTrack *track = &mov->tracks[i];
2542 if (mov->flags & FF_MOV_FLAG_SEPARATE_MOOF)
2543 track->data_offset = 0;
2544 else
2545 track->data_offset = mdat_size;
2546 if (!track->mdat_buf)
2547 continue;
2548 mdat_size += avio_tell(track->mdat_buf);
2549 if (first_track < 0)
2550 first_track = i;
2551 }
2552
2553 if (!mdat_size)
2554 return 0;
2555
2556 for (i = 0; i < mov->nb_streams; i++) {
2557 MOVTrack *track = &mov->tracks[i];
2558 int buf_size, write_moof = 1, moof_tracks = -1;
2559 uint8_t *buf;
2560 int64_t duration = 0;
2561
2562 if (track->entry)
b7d3dd90 2563 duration = track->start_dts + track->track_duration -
83988d58
MS
2564 track->cluster[0].dts;
2565 if (mov->flags & FF_MOV_FLAG_SEPARATE_MOOF) {
2566 if (!track->mdat_buf)
2567 continue;
2568 mdat_size = avio_tell(track->mdat_buf);
2569 moof_tracks = i;
2570 } else {
2571 write_moof = i == first_track;
2572 }
2573
2574 if (write_moof) {
2575 MOVFragmentInfo *info;
e771e6dd 2576 avio_flush(s->pb);
83988d58
MS
2577 track->nb_frag_info++;
2578 track->frag_info = av_realloc(track->frag_info,
2579 sizeof(*track->frag_info) *
2580 track->nb_frag_info);
2581 info = &track->frag_info[track->nb_frag_info - 1];
2582 info->offset = avio_tell(s->pb);
2583 info->time = mov->tracks[i].frag_start;
4ddd54da
MS
2584 info->duration = duration;
2585 mov_write_tfrf_tags(s->pb, mov, track);
83988d58
MS
2586
2587 mov_write_moof_tag(s->pb, mov, moof_tracks);
4ddd54da 2588 info->tfrf_offset = track->tfrf_offset;
83988d58
MS
2589 mov->fragments++;
2590
2591 avio_wb32(s->pb, mdat_size + 8);
2592 ffio_wfourcc(s->pb, "mdat");
2593 }
2594
2595 if (track->entry)
2596 track->frag_start += duration;
2597 track->entry = 0;
2598 if (!track->mdat_buf)
2599 continue;
2600 buf_size = avio_close_dyn_buf(track->mdat_buf, &buf);
2601 track->mdat_buf = NULL;
2602
2603 avio_write(s->pb, buf, buf_size);
2604 av_free(buf);
2605 }
2606
2607 mov->mdat_size = 0;
2608
e771e6dd 2609 avio_flush(s->pb);
83988d58
MS
2610 return 0;
2611}
2612
b613ff5e 2613static int mov_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
1cb5f7fd 2614{
2d243fb3 2615 MOVMuxContext *mov = s->priv_data;
ae628ec1 2616 AVIOContext *pb = s->pb;
039627cf
BC
2617 MOVTrack *trk = &mov->tracks[pkt->stream_index];
2618 AVCodecContext *enc = trk->enc;
b7d3dd90 2619 unsigned int samples_in_chunk = 0;
e928649b 2620 int size= pkt->size;
e2484fb6 2621 uint8_t *reformatted_data = NULL;
1cb5f7fd 2622
e771e6dd
MS
2623 if (!s->pb->seekable && !(mov->flags & FF_MOV_FLAG_EMPTY_MOOV))
2624 return 0; /* Can't handle that */
2625
e45ccf79 2626 if (!size) return 0; /* Discard 0 sized packets */
1cb5f7fd 2627
83988d58
MS
2628 if ((mov->max_fragment_duration && trk->entry &&
2629 av_rescale_q(pkt->dts - trk->cluster[0].dts,
2630 s->streams[pkt->stream_index]->time_base,
2631 AV_TIME_BASE_Q) >= mov->max_fragment_duration) ||
2632 (mov->max_fragment_size && mov->mdat_size + size >= mov->max_fragment_size) ||
2633 (mov->flags & FF_MOV_FLAG_FRAG_KEYFRAME &&
2634 enc->codec_type == AVMEDIA_TYPE_VIDEO &&
2635 trk->entry && pkt->flags & AV_PKT_FLAG_KEY)) {
2636 mov_flush_fragment(s);
2637 }
2638
2639 if (mov->flags & FF_MOV_FLAG_FRAGMENT && mov->fragments > 0) {
2640 if (!trk->mdat_buf) {
2641 int ret;
2642 if ((ret = avio_open_dyn_buf(&trk->mdat_buf)) < 0)
2643 return ret;
2644 }
2645 pb = trk->mdat_buf;
2646 }
2647
06fcf56c 2648 if (enc->codec_id == CODEC_ID_AMR_NB) {
5cb49ca1
BC
2649 /* We must find out how many AMR blocks there are in one packet */
2650 static uint16_t packed_size[16] =
ed669c9b 2651 {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 1};
5cb49ca1
BC
2652 int len = 0;
2653
b7d3dd90 2654 while (len < size && samples_in_chunk < 100) {
5cb49ca1 2655 len += packed_size[(pkt->data[len] >> 3) & 0x0F];
b7d3dd90 2656 samples_in_chunk++;
1cb5f7fd 2657 }
b7d3dd90 2658 if (samples_in_chunk > 1) {
89938d29 2659 av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, implement a AVParser for it\n");
2111e3f9
MN
2660 return -1;
2661 }
b7d3dd90
MS
2662 } else if (trk->sample_size)
2663 samples_in_chunk = size / trk->sample_size;
5cb49ca1 2664 else
b7d3dd90 2665 samples_in_chunk = 1;
e45ccf79 2666
5616f85d 2667 /* copy extradata if it exists */
b7d3dd90
MS
2668 if (trk->vos_len == 0 && enc->extradata_size > 0) {
2669 trk->vos_len = enc->extradata_size;
2670 trk->vos_data = av_malloc(trk->vos_len);
2671 memcpy(trk->vos_data, enc->extradata, trk->vos_len);
e45ccf79
GB
2672 }
2673
b7d3dd90 2674 if (enc->codec_id == CODEC_ID_H264 && trk->vos_len > 0 && *(uint8_t *)trk->vos_data != 1) {
5d2160a0
BC
2675 /* from x264 or from bytestream h264 */
2676 /* nal reformating needed */
e2484fb6
MS
2677 if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams) {
2678 ff_avc_parse_nal_units_buf(pkt->data, &reformatted_data,
2679 &size);
2680 avio_write(pb, reformatted_data, size);
2681 } else {
2682 size = ff_avc_parse_nal_units(pb, pkt->data, pkt->size);
2683 }
5d2160a0 2684 } else {
77eb5504 2685 avio_write(pb, pkt->data, size);
5d2160a0
BC
2686 }
2687
0a63a676 2688 if ((enc->codec_id == CODEC_ID_DNXHD ||
b7d3dd90 2689 enc->codec_id == CODEC_ID_AC3) && !trk->vos_len) {
db568c07 2690 /* copy frame to create needed atoms */
b7d3dd90
MS
2691 trk->vos_len = size;
2692 trk->vos_data = av_malloc(size);
2693 if (!trk->vos_data)
e8c4df40 2694 return AVERROR(ENOMEM);
b7d3dd90 2695 memcpy(trk->vos_data, pkt->data, size);
c1b8e6d8
BC
2696 }
2697
0aec3c5c
BC
2698 if (!(trk->entry % MOV_INDEX_CLUSTER_SIZE)) {
2699 trk->cluster = av_realloc(trk->cluster, (trk->entry + MOV_INDEX_CLUSTER_SIZE) * sizeof(*trk->cluster));
e45ccf79
GB
2700 if (!trk->cluster)
2701 return -1;
e45ccf79 2702 }
e45ccf79 2703
a2704c97 2704 trk->cluster[trk->entry].pos = avio_tell(pb) - size;
b7d3dd90 2705 trk->cluster[trk->entry].samples_in_chunk = samples_in_chunk;
0aec3c5c 2706 trk->cluster[trk->entry].size = size;
b7d3dd90 2707 trk->cluster[trk->entry].entries = samples_in_chunk;
aa90239f 2708 trk->cluster[trk->entry].dts = pkt->dts;
83988d58
MS
2709 if (!trk->entry && trk->start_dts != AV_NOPTS_VALUE) {
2710 /* First packet of a new fragment. We already wrote the duration
b7d3dd90 2711 * of the last packet of the previous fragment based on track_duration,
83988d58
MS
2712 * which might not exactly match our dts. Therefore adjust the dts
2713 * of this packet to be what the previous packets duration implies. */
b7d3dd90 2714 trk->cluster[trk->entry].dts = trk->start_dts + trk->track_duration;
83988d58 2715 }
6ca38568
MS
2716 if (trk->start_dts == AV_NOPTS_VALUE)
2717 trk->start_dts = pkt->dts;
b7d3dd90 2718 trk->track_duration = pkt->dts - trk->start_dts + pkt->duration;
aa90239f 2719
96f69e0d
BC
2720 if (pkt->pts == AV_NOPTS_VALUE) {
2721 av_log(s, AV_LOG_WARNING, "pts has no value\n");
2722 pkt->pts = pkt->dts;
2723 }
c4f078ff 2724 if (pkt->dts != pkt->pts)
e1316b19 2725 trk->flags |= MOV_TRACK_CTTS;
c4f078ff 2726 trk->cluster[trk->entry].cts = pkt->pts - pkt->dts;
e1316b19 2727 trk->cluster[trk->entry].flags = 0;
cc947f04 2728 if (pkt->flags & AV_PKT_FLAG_KEY) {
44b11207
TM
2729 if (mov->mode == MODE_MOV && enc->codec_id == CODEC_ID_MPEG2VIDEO &&
2730 trk->entry > 0) { // force sync sample for the first key frame
e1316b19
BC
2731 mov_parse_mpeg2_frame(pkt, &trk->cluster[trk->entry].flags);
2732 if (trk->cluster[trk->entry].flags & MOV_PARTIAL_SYNC_SAMPLE)
2733 trk->flags |= MOV_TRACK_STPS;
2734 } else {
2735 trk->cluster[trk->entry].flags = MOV_SYNC_SAMPLE;
2736 }
2737 if (trk->cluster[trk->entry].flags & MOV_SYNC_SAMPLE)
b7d3dd90 2738 trk->has_keyframes++;
ab4752e3 2739 }
e45ccf79 2740 trk->entry++;
b7d3dd90 2741 trk->sample_count += samples_in_chunk;
039627cf 2742 mov->mdat_size += size;
e45ccf79 2743
b7f2fdde 2744 avio_flush(pb);
e977af6f
MS
2745
2746 if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams)
e2484fb6
MS
2747 ff_mov_add_hinted_packet(s, pkt, trk->hint_track, trk->entry,
2748 reformatted_data, size);
2749 av_free(reformatted_data);
1cb5f7fd
MN
2750 return 0;
2751}
2752
b613ff5e
MS
2753int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
2754{
2755 if (!pkt) {
2756 mov_flush_fragment(s);
2757 return 1;
2758 } else {
2759 return mov_write_packet_internal(s, pkt);
2760 }
2761}
2762
ddb63017
DC
2763// QuickTime chapters involve an additional text track with the chapter names
2764// as samples, and a tref pointing from the other tracks to the chapter one.
2765static void mov_create_chapter_track(AVFormatContext *s, int tracknum)
2766{
2767 MOVMuxContext *mov = s->priv_data;
2768 MOVTrack *track = &mov->tracks[tracknum];
2769 AVPacket pkt = { .stream_index = tracknum, .flags = AV_PKT_FLAG_KEY };
2770 int i, len;
2771
2772 track->mode = mov->mode;
2773 track->tag = MKTAG('t','e','x','t');
2774 track->timescale = MOV_TIMESCALE;
71a861cf 2775 track->enc = avcodec_alloc_context3(NULL);
ddb63017
DC
2776 track->enc->codec_type = AVMEDIA_TYPE_SUBTITLE;
2777
2778 for (i = 0; i < s->nb_chapters; i++) {
2779 AVChapter *c = s->chapters[i];
d2d67e42 2780 AVDictionaryEntry *t;
ddb63017
DC
2781
2782 int64_t end = av_rescale_q(c->end, c->time_base, (AVRational){1,MOV_TIMESCALE});
2783 pkt.pts = pkt.dts = av_rescale_q(c->start, c->time_base, (AVRational){1,MOV_TIMESCALE});
2784 pkt.duration = end - pkt.dts;
2785
d2d67e42 2786 if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
ddb63017
DC
2787 len = strlen(t->value);
2788 pkt.size = len+2;
2789 pkt.data = av_malloc(pkt.size);
2790 AV_WB16(pkt.data, len);
2791 memcpy(pkt.data+2, t->value, len);
27a826c9 2792 ff_mov_write_packet(s, &pkt);
ddb63017
DC
2793 av_freep(&pkt.data);
2794 }
2795 }
2796}
2797
1b206f62
DC
2798static int mov_write_header(AVFormatContext *s)
2799{
ae628ec1 2800 AVIOContext *pb = s->pb;
1b206f62 2801 MOVMuxContext *mov = s->priv_data;
5f847bf6 2802 AVDictionaryEntry *t;
e977af6f 2803 int i, hint_track = 0;
1b206f62 2804
e771e6dd
MS
2805 /* Non-seekable output is ok if EMPTY_MOOV is set, or if using the ismv
2806 * format (which sets EMPTY_MOOV later in this function). If ism_lookahead
2807 * is enabled, we don't support non-seekable output at all. */
2808 if (!s->pb->seekable &&
2809 ((!(mov->flags & FF_MOV_FLAG_EMPTY_MOOV) &&
2810 !(s->oformat && !strcmp(s->oformat->name, "ismv")))
2811 || mov->ism_lookahead)) {
1b206f62
DC
2812 av_log(s, AV_LOG_ERROR, "muxer does not support non seekable output\n");
2813 return -1;
2814 }
2815
2816 /* Default mode == MP4 */
2817 mov->mode = MODE_MP4;
2818
2819 if (s->oformat != NULL) {
2820 if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
2821 else if (!strcmp("3g2", s->oformat->name)) mov->mode = MODE_3GP|MODE_3G2;
2822 else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
2823 else if (!strcmp("psp", s->oformat->name)) mov->mode = MODE_PSP;
2824 else if (!strcmp("ipod",s->oformat->name)) mov->mode = MODE_IPOD;
4ddd54da 2825 else if (!strcmp("ismv",s->oformat->name)) mov->mode = MODE_ISM;
1b206f62
DC
2826
2827 mov_write_ftyp_tag(pb,s);
2828 if (mov->mode == MODE_PSP) {
2829 if (s->nb_streams != 2) {
2830 av_log(s, AV_LOG_ERROR, "PSP mode need one video and one audio stream\n");
2831 return -1;
2832 }
2833 mov_write_uuidprof_tag(pb,s);
2834 }
2835 }
2836
ddb63017
DC
2837 mov->nb_streams = s->nb_streams;
2838 if (mov->mode & (MODE_MOV|MODE_IPOD) && s->nb_chapters)
2839 mov->chapter_track = mov->nb_streams++;
2840
28734ac9 2841 if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
e977af6f
MS
2842 /* Add hint tracks for each audio and video stream */
2843 hint_track = mov->nb_streams;
2844 for (i = 0; i < s->nb_streams; i++) {
2845 AVStream *st = s->streams[i];
2846 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
2847 st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
2848 mov->nb_streams++;
2849 }
2850 }
2851 }
2852
ddb63017 2853 mov->tracks = av_mallocz(mov->nb_streams*sizeof(*mov->tracks));
1b206f62
DC
2854 if (!mov->tracks)
2855 return AVERROR(ENOMEM);
2856
2857 for(i=0; i<s->nb_streams; i++){
2858 AVStream *st= s->streams[i];
2859 MOVTrack *track= &mov->tracks[i];
d2d67e42 2860 AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL,0);
1b206f62
DC
2861
2862 track->enc = st->codec;
2863 track->language = ff_mov_iso639_to_lang(lang?lang->value:"und", mov->mode!=MODE_MOV);
2864 if (track->language < 0)
2865 track->language = 0;
2866 track->mode = mov->mode;
2867 track->tag = mov_find_codec_tag(s, track);
2868 if (!track->tag) {
2869 av_log(s, AV_LOG_ERROR, "track %d: could not find tag, "
2870 "codec not currently supported in container\n", i);
2871 goto error;
2872 }
e977af6f
MS
2873 /* If hinting of this track is enabled by a later hint track,
2874 * this is updated. */
2875 track->hint_track = -1;
6ca38568 2876 track->start_dts = AV_NOPTS_VALUE;
1b206f62
DC
2877 if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO){
2878 if (track->tag == MKTAG('m','x','3','p') || track->tag == MKTAG('m','x','3','n') ||
2879 track->tag == MKTAG('m','x','4','p') || track->tag == MKTAG('m','x','4','n') ||
2880 track->tag == MKTAG('m','x','5','p') || track->tag == MKTAG('m','x','5','n')) {
2881 if (st->codec->width != 720 || (st->codec->height != 608 && st->codec->height != 512)) {
2882 av_log(s, AV_LOG_ERROR, "D-10/IMX must use 720x608 or 720x512 video resolution\n");
2883 goto error;
2884 }
2885 track->height = track->tag>>24 == 'n' ? 486 : 576;
2886 }
2887 track->timescale = st->codec->time_base.den;
2888 if (track->mode == MODE_MOV && track->timescale > 100000)
2889 av_log(s, AV_LOG_WARNING,
2890 "WARNING codec timebase is very high. If duration is too long,\n"
2891 "file may not be playable by quicktime. Specify a shorter timebase\n"
2892 "or choose different container.\n");
2893 }else if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO){
2894 track->timescale = st->codec->sample_rate;
b7d3dd90 2895 /* set sample_size for PCM and ADPCM */
8e8c5131 2896 if (av_get_bits_per_sample(st->codec->codec_id)) {
f2589642 2897 if (!st->codec->block_align) {
8e8c5131 2898 av_log(s, AV_LOG_ERROR, "track %d: codec block align is not set\n", i);
f2589642
BC
2899 goto error;
2900 }
b7d3dd90 2901 track->sample_size = st->codec->block_align;
8e8c5131
JR
2902 }
2903 /* set audio_vbr for compressed audio */
2904 if (av_get_bits_per_sample(st->codec->codec_id) < 8) {
9f9c45f4 2905 if (!st->codec->frame_size && track->mode == MODE_MOV) {
8e8c5131
JR
2906 av_log(s, AV_LOG_ERROR, "track %d: codec frame size is not set\n", i);
2907 goto error;
2908 }
1b206f62 2909 track->audio_vbr = 1;
1b206f62
DC
2910 }
2911 if (track->mode != MODE_MOV) {
2912 if (track->timescale > UINT16_MAX) {
2913 av_log(s, AV_LOG_ERROR, "track %d: output format does not support "
2914 "sample rate %dhz\n", i, track->timescale);
2915 goto error;
2916 }
2917 if (track->enc->codec_id == CODEC_ID_MP3 && track->timescale < 16000) {
2918 av_log(s, AV_LOG_ERROR, "track %d: muxing mp3 at %dhz is not supported\n",
2919 i, track->enc->sample_rate);
2920 goto error;
2921 }
2922 }
2923 }else if(st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE){
2924 track->timescale = st->codec->time_base.den;
2925 }
2926 if (!track->height)
2927 track->height = st->codec->height;
4ddd54da
MS
2928 /* The ism specific timescale isn't mandatory, but is assumed by
2929 * some tools, such as mp4split. */
2930 if (mov->mode == MODE_ISM)
2931 track->timescale = 10000000;
1b206f62 2932
c3f9ebf7 2933 avpriv_set_pts_info(st, 64, 1, track->timescale);
83988d58
MS
2934
2935 /* copy extradata if it exists */
2936 if (st->codec->extradata_size) {
b7d3dd90
MS
2937 track->vos_len = st->codec->extradata_size;
2938 track->vos_data = av_malloc(track->vos_len);
2939 memcpy(track->vos_data, st->codec->extradata, track->vos_len);
83988d58 2940 }
1b206f62
DC
2941 }
2942
4ddd54da
MS
2943 if (mov->mode == MODE_ISM) {
2944 /* If no fragmentation options have been set, set a default. */
2945 if (!(mov->flags & (FF_MOV_FLAG_FRAG_KEYFRAME |
2946 FF_MOV_FLAG_FRAG_CUSTOM)) &&
2947 !mov->max_fragment_duration && !mov->max_fragment_size)
2948 mov->max_fragment_duration = 5000000;
2949 mov->flags |= FF_MOV_FLAG_EMPTY_MOOV | FF_MOV_FLAG_SEPARATE_MOOF;
2950 }
2951
83988d58
MS
2952 /* Set the FRAGMENT flag if any of the fragmentation methods are
2953 * enabled. */
2954 if (mov->max_fragment_duration || mov->max_fragment_size ||
2955 mov->flags & (FF_MOV_FLAG_EMPTY_MOOV |
b613ff5e
MS
2956 FF_MOV_FLAG_FRAG_KEYFRAME |
2957 FF_MOV_FLAG_FRAG_CUSTOM))
83988d58
MS
2958 mov->flags |= FF_MOV_FLAG_FRAGMENT;
2959
2960 if (!(mov->flags & FF_MOV_FLAG_EMPTY_MOOV))
2961 mov_write_mdat_tag(pb, mov);
5f847bf6 2962
001d668d
AK
2963 if (t = av_dict_get(s->metadata, "creation_time", NULL, 0))
2964 mov->time = ff_iso8601_to_unix_time(t->value);
a78dbada
MS
2965 if (mov->time)
2966 mov->time += 0x7C25B080; // 1970 based -> 1904 based
ddb63017
DC
2967
2968 if (mov->chapter_track)
2969 mov_create_chapter_track(s, mov->chapter_track);
1b206f62 2970
28734ac9 2971 if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
e977af6f
MS
2972 /* Initialize the hint tracks for each audio and video stream */
2973 for (i = 0; i < s->nb_streams; i++) {
2974 AVStream *st = s->streams[i];
2975 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
2976 st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
2977 ff_mov_init_hinting(s, hint_track, i);
2978 hint_track++;
2979 }
2980 }
2981 }
2982
b7f2fdde 2983 avio_flush(pb);
1b206f62 2984
f5322104
MS
2985 if (mov->flags & FF_MOV_FLAG_ISML)
2986 mov_write_isml_manifest(pb, mov);
2987
83988d58
MS
2988 if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV) {
2989 mov_write_moov_tag(pb, mov, s);
2990 mov->fragments++;
2991 }
2992
1b206f62
DC
2993 return 0;
2994 error:
2995 av_freep(&mov->tracks);
2996 return -1;
2997}
2998
1cb5f7fd
MN
2999static int mov_write_trailer(AVFormatContext *s)
3000{
2d243fb3 3001 MOVMuxContext *mov = s->priv_data;
ae628ec1 3002 AVIOContext *pb = s->pb;
1cb5f7fd 3003 int res = 0;
0aec3c5c 3004 int i;
1cb5f7fd 3005
a2704c97 3006 int64_t moov_pos = avio_tell(pb);
1cb5f7fd 3007
83988d58 3008 if (!(mov->flags & FF_MOV_FLAG_FRAGMENT)) {
990a746c
MS
3009 /* Write size of mdat tag */
3010 if (mov->mdat_size + 8 <= UINT32_MAX) {
3011 avio_seek(pb, mov->mdat_pos, SEEK_SET);
3012 avio_wb32(pb, mov->mdat_size + 8);
3013 } else {
3014 /* overwrite 'wide' placeholder atom */
3015 avio_seek(pb, mov->mdat_pos - 8, SEEK_SET);
3016 /* special value: real atom size will be 64 bit value after
3017 * tag field */
3018 avio_wb32(pb, 1);
3019 ffio_wfourcc(pb, "mdat");
3020 avio_wb64(pb, mov->mdat_size + 16);
3021 }
3022 avio_seek(pb, moov_pos, SEEK_SET);
1cb5f7fd 3023
990a746c 3024 mov_write_moov_tag(pb, mov, s);
83988d58
MS
3025 } else {
3026 mov_flush_fragment(s);
3027 mov_write_mfra_tag(pb, mov);
3028 }
1cb5f7fd 3029
ddb63017
DC
3030 if (mov->chapter_track)
3031 av_freep(&mov->tracks[mov->chapter_track].enc);
3032
42fb4148 3033 for (i=0; i<mov->nb_streams; i++) {
e977af6f
MS
3034 if (mov->tracks[i].tag == MKTAG('r','t','p',' '))
3035 ff_mov_close_hinting(&mov->tracks[i]);
0aec3c5c 3036 av_freep(&mov->tracks[i].cluster);
83988d58 3037 av_freep(&mov->tracks[i].frag_info);
0aec3c5c 3038
b7d3dd90
MS
3039 if (mov->tracks[i].vos_len)
3040 av_free(mov->tracks[i].vos_data);
ec7d0d2e 3041
1cb5f7fd 3042 }
69dde1ad 3043
b7f2fdde 3044 avio_flush(pb);
1cb5f7fd 3045
5c5776e1
BC
3046 av_freep(&mov->tracks);
3047
1cb5f7fd
MN
3048 return res;
3049}
3050
b250f9c6 3051#if CONFIG_MOV_MUXER
a4ea00d0 3052MOV_CLASS(mov)
c6610a21 3053AVOutputFormat ff_mov_muxer = {
dfc2c4d9
AK
3054 .name = "mov",
3055 .long_name = NULL_IF_CONFIG_SMALL("MOV format"),
3056 .extensions = "mov",
3057 .priv_data_size = sizeof(MOVMuxContext),
3058 .audio_codec = CODEC_ID_AAC,
fb42d156
AK
3059#if CONFIG_LIBX264_ENCODER
3060 .video_codec = CODEC_ID_H264,
3061#else
dfc2c4d9 3062 .video_codec = CODEC_ID_MPEG4,
fb42d156 3063#endif
dfc2c4d9
AK
3064 .write_header = mov_write_header,
3065 .write_packet = ff_mov_write_packet,
3066 .write_trailer = mov_write_trailer,
b613ff5e 3067 .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
c1854592 3068 .codec_tag = (const AVCodecTag* const []){codec_movvideo_tags, codec_movaudio_tags, 0},
91e3a25e 3069 .priv_class = &mov_muxer_class,
1cb5f7fd 3070};
ff70e601 3071#endif
b250f9c6 3072#if CONFIG_TGP_MUXER
a4ea00d0 3073MOV_CLASS(tgp)
c6610a21 3074AVOutputFormat ff_tgp_muxer = {
dfc2c4d9
AK
3075 .name = "3gp",
3076 .long_name = NULL_IF_CONFIG_SMALL("3GP format"),
3077 .extensions = "3gp",
3078 .priv_data_size = sizeof(MOVMuxContext),
3079 .audio_codec = CODEC_ID_AMR_NB,
3080 .video_codec = CODEC_ID_H263,
3081 .write_header = mov_write_header,
3082 .write_packet = ff_mov_write_packet,
3083 .write_trailer = mov_write_trailer,
b613ff5e 3084 .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
c1854592 3085 .codec_tag = (const AVCodecTag* const []){codec_3gp_tags, 0},
a4ea00d0 3086 .priv_class = &tgp_muxer_class,
1cb5f7fd 3087};
ff70e601 3088#endif
b250f9c6 3089#if CONFIG_MP4_MUXER
a4ea00d0 3090MOV_CLASS(mp4)
c6610a21 3091AVOutputFormat ff_mp4_muxer = {
dfc2c4d9
AK
3092 .name = "mp4",
3093 .long_name = NULL_IF_CONFIG_SMALL("MP4 format"),
3094 .mime_type = "application/mp4",
3095 .extensions = "mp4",
3096 .priv_data_size = sizeof(MOVMuxContext),
3097 .audio_codec = CODEC_ID_AAC,
fb42d156
AK
3098#if CONFIG_LIBX264_ENCODER
3099 .video_codec = CODEC_ID_H264,