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