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