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