FFmpeg
movenc.c
Go to the documentation of this file.
1 /*
2  * MOV, 3GP, MP4 muxer
3  * Copyright (c) 2003 Thomas Raivio
4  * Copyright (c) 2004 Gildas Bazin <gbazin at videolan dot org>
5  * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
6  *
7  * This file is part of FFmpeg.
8  *
9  * FFmpeg is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * FFmpeg is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with FFmpeg; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23 
24 #include <stdint.h>
25 #include <inttypes.h>
26 
27 #include "movenc.h"
28 #include "avformat.h"
29 #include "avio_internal.h"
30 #include "riff.h"
31 #include "avio.h"
32 #include "isom.h"
33 #include "av1.h"
34 #include "avc.h"
36 #include "libavcodec/dnxhddata.h"
37 #include "libavcodec/flac.h"
38 #include "libavcodec/get_bits.h"
39 
40 #include "libavcodec/internal.h"
41 #include "libavcodec/put_bits.h"
42 #include "libavcodec/vc1_common.h"
43 #include "libavcodec/raw.h"
44 #include "internal.h"
45 #include "libavutil/avstring.h"
46 #include "libavutil/intfloat.h"
47 #include "libavutil/mathematics.h"
48 #include "libavutil/libm.h"
49 #include "libavutil/opt.h"
50 #include "libavutil/dict.h"
51 #include "libavutil/pixdesc.h"
52 #include "libavutil/stereo3d.h"
53 #include "libavutil/timecode.h"
54 #include "libavutil/color_utils.h"
55 #include "hevc.h"
56 #include "rtpenc.h"
57 #include "mov_chan.h"
58 #include "vpcc.h"
59 
60 static const AVOption options[] = {
61  { "movflags", "MOV muxer flags", offsetof(MOVMuxContext, flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
62  { "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" },
63  { "moov_size", "maximum moov size so it can be placed at the begin", offsetof(MOVMuxContext, reserved_moov_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, 0 },
64  { "empty_moov", "Make the initial moov atom empty", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_EMPTY_MOOV}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
65  { "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" },
66  { "frag_every_frame", "Fragment at every frame", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_FRAG_EVERY_FRAME}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
67  { "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" },
68  { "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" },
69  { "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" },
70  { "faststart", "Run a second pass to put the index (moov atom) at the beginning of the file", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_FASTSTART}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
71  { "omit_tfhd_offset", "Omit the base data offset in tfhd atoms", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_OMIT_TFHD_OFFSET}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
72  { "disable_chpl", "Disable Nero chapter atom", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_DISABLE_CHPL}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
73  { "default_base_moof", "Set the default-base-is-moof flag in tfhd atoms", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_DEFAULT_BASE_MOOF}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
74  { "dash", "Write DASH compatible fragmented MP4", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_DASH}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
75  { "frag_discont", "Signal that the next fragment is discontinuous from earlier ones", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_FRAG_DISCONT}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
76  { "delay_moov", "Delay writing the initial moov until the first fragment is cut, or until the first fragment flush", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_DELAY_MOOV}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
77  { "global_sidx", "Write a global sidx index at the start of the file", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_GLOBAL_SIDX}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
78  { "skip_sidx", "Skip writing of sidx atom", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_SKIP_SIDX}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
79  { "write_colr", "Write colr atom (Experimental, may be renamed or changed, do not use from scripts)", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_WRITE_COLR}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
80  { "write_gama", "Write deprecated gama atom", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_WRITE_GAMA}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
81  { "use_metadata_tags", "Use mdta atom for metadata.", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_USE_MDTA}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
82  { "skip_trailer", "Skip writing the mfra/tfra/mfro trailer for fragmented files", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_SKIP_TRAILER}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
83  { "negative_cts_offsets", "Use negative CTS offsets (reducing the need for edit lists)", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_NEGATIVE_CTS_OFFSETS}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
84  FF_RTP_FLAG_OPTS(MOVMuxContext, rtp_flags),
85  { "skip_iods", "Skip writing iods atom.", offsetof(MOVMuxContext, iods_skip), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
86  { "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},
87  { "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},
88  { "frag_duration", "Maximum fragment duration", offsetof(MOVMuxContext, max_fragment_duration), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
89  { "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},
90  { "frag_size", "Maximum fragment size", offsetof(MOVMuxContext, max_fragment_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
91  { "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},
92  { "video_track_timescale", "set timescale of all video tracks", offsetof(MOVMuxContext, video_track_timescale), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
93  { "brand", "Override major brand", offsetof(MOVMuxContext, major_brand), AV_OPT_TYPE_STRING, {.str = NULL}, .flags = AV_OPT_FLAG_ENCODING_PARAM },
94  { "use_editlist", "use edit list", offsetof(MOVMuxContext, use_editlist), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, AV_OPT_FLAG_ENCODING_PARAM},
95  { "fragment_index", "Fragment number of the next fragment", offsetof(MOVMuxContext, fragments), AV_OPT_TYPE_INT, {.i64 = 1}, 1, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
96  { "mov_gamma", "gamma value for gama atom", offsetof(MOVMuxContext, gamma), AV_OPT_TYPE_FLOAT, {.dbl = 0.0 }, 0.0, 10, AV_OPT_FLAG_ENCODING_PARAM},
97  { "frag_interleave", "Interleave samples within fragments (max number of consecutive samples, lower is tighter interleaving, but with more overhead)", offsetof(MOVMuxContext, frag_interleave), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
98  { "encryption_scheme", "Configures the encryption scheme, allowed values are none, cenc-aes-ctr", offsetof(MOVMuxContext, encryption_scheme_str), AV_OPT_TYPE_STRING, {.str = NULL}, .flags = AV_OPT_FLAG_ENCODING_PARAM },
99  { "encryption_key", "The media encryption key (hex)", offsetof(MOVMuxContext, encryption_key), AV_OPT_TYPE_BINARY, .flags = AV_OPT_FLAG_ENCODING_PARAM },
100  { "encryption_kid", "The media encryption key identifier (hex)", offsetof(MOVMuxContext, encryption_kid), AV_OPT_TYPE_BINARY, .flags = AV_OPT_FLAG_ENCODING_PARAM },
101  { "use_stream_ids_as_track_ids", "use stream ids as track ids", offsetof(MOVMuxContext, use_stream_ids_as_track_ids), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
102  { "write_tmcd", "force or disable writing tmcd", offsetof(MOVMuxContext, write_tmcd), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, AV_OPT_FLAG_ENCODING_PARAM},
103  { "write_prft", "Write producer reference time box with specified time source", offsetof(MOVMuxContext, write_prft), AV_OPT_TYPE_INT, {.i64 = MOV_PRFT_NONE}, 0, MOV_PRFT_NB-1, AV_OPT_FLAG_ENCODING_PARAM, "prft"},
104  { "wallclock", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = MOV_PRFT_SRC_WALLCLOCK}, 0, 0, AV_OPT_FLAG_ENCODING_PARAM, "prft"},
105  { "pts", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = MOV_PRFT_SRC_PTS}, 0, 0, AV_OPT_FLAG_ENCODING_PARAM, "prft"},
106  { "empty_hdlr_name", "write zero-length name string in hdlr atoms within mdia and minf atoms", offsetof(MOVMuxContext, empty_hdlr_name), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
107  { NULL },
108 };
109 
110 #define MOV_CLASS(flavor)\
111 static const AVClass flavor ## _muxer_class = {\
112  .class_name = #flavor " muxer",\
113  .item_name = av_default_item_name,\
114  .option = options,\
115  .version = LIBAVUTIL_VERSION_INT,\
116 };
117 
118 static int get_moov_size(AVFormatContext *s);
119 
120 static int utf8len(const uint8_t *b)
121 {
122  int len = 0;
123  int val;
124  while (*b) {
125  GET_UTF8(val, *b++, return -1;)
126  len++;
127  }
128  return len;
129 }
130 
131 //FIXME support 64 bit variant with wide placeholders
132 static int64_t update_size(AVIOContext *pb, int64_t pos)
133 {
134  int64_t curpos = avio_tell(pb);
135  avio_seek(pb, pos, SEEK_SET);
136  avio_wb32(pb, curpos - pos); /* rewrite size */
137  avio_seek(pb, curpos, SEEK_SET);
138 
139  return curpos - pos;
140 }
141 
142 static int co64_required(const MOVTrack *track)
143 {
144  if (track->entry > 0 && track->cluster[track->entry - 1].pos + track->data_offset > UINT32_MAX)
145  return 1;
146  return 0;
147 }
148 
149 static int is_cover_image(const AVStream *st)
150 {
151  /* Eg. AV_DISPOSITION_ATTACHED_PIC | AV_DISPOSITION_TIMED_THUMBNAILS
152  * is encoded as sparse video track */
153  return st && st->disposition == AV_DISPOSITION_ATTACHED_PIC;
154 }
155 
156 static int rtp_hinting_needed(const AVStream *st)
157 {
158  /* Add hint tracks for each real audio and video stream */
159  if (is_cover_image(st))
160  return 0;
161  return st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ||
163 }
164 
165 /* Chunk offset atom */
166 static int mov_write_stco_tag(AVIOContext *pb, MOVTrack *track)
167 {
168  int i;
169  int mode64 = co64_required(track); // use 32 bit size variant if possible
170  int64_t pos = avio_tell(pb);
171  avio_wb32(pb, 0); /* size */
172  if (mode64)
173  ffio_wfourcc(pb, "co64");
174  else
175  ffio_wfourcc(pb, "stco");
176  avio_wb32(pb, 0); /* version & flags */
177  avio_wb32(pb, track->chunkCount); /* entry count */
178  for (i = 0; i < track->entry; i++) {
179  if (!track->cluster[i].chunkNum)
180  continue;
181  if (mode64 == 1)
182  avio_wb64(pb, track->cluster[i].pos + track->data_offset);
183  else
184  avio_wb32(pb, track->cluster[i].pos + track->data_offset);
185  }
186  return update_size(pb, pos);
187 }
188 
189 /* Sample size atom */
190 static int mov_write_stsz_tag(AVIOContext *pb, MOVTrack *track)
191 {
192  int equalChunks = 1;
193  int i, j, entries = 0, tst = -1, oldtst = -1;
194 
195  int64_t pos = avio_tell(pb);
196  avio_wb32(pb, 0); /* size */
197  ffio_wfourcc(pb, "stsz");
198  avio_wb32(pb, 0); /* version & flags */
199 
200  for (i = 0; i < track->entry; i++) {
201  tst = track->cluster[i].size / track->cluster[i].entries;
202  if (oldtst != -1 && tst != oldtst)
203  equalChunks = 0;
204  oldtst = tst;
205  entries += track->cluster[i].entries;
206  }
207  if (equalChunks && track->entry) {
208  int sSize = track->entry ? track->cluster[0].size / track->cluster[0].entries : 0;
209  sSize = FFMAX(1, sSize); // adpcm mono case could make sSize == 0
210  avio_wb32(pb, sSize); // sample size
211  avio_wb32(pb, entries); // sample count
212  } else {
213  avio_wb32(pb, 0); // sample size
214  avio_wb32(pb, entries); // sample count
215  for (i = 0; i < track->entry; i++) {
216  for (j = 0; j < track->cluster[i].entries; j++) {
217  avio_wb32(pb, track->cluster[i].size /
218  track->cluster[i].entries);
219  }
220  }
221  }
222  return update_size(pb, pos);
223 }
224 
225 /* Sample to chunk atom */
226 static int mov_write_stsc_tag(AVIOContext *pb, MOVTrack *track)
227 {
228  int index = 0, oldval = -1, i;
229  int64_t entryPos, curpos;
230 
231  int64_t pos = avio_tell(pb);
232  avio_wb32(pb, 0); /* size */
233  ffio_wfourcc(pb, "stsc");
234  avio_wb32(pb, 0); // version & flags
235  entryPos = avio_tell(pb);
236  avio_wb32(pb, track->chunkCount); // entry count
237  for (i = 0; i < track->entry; i++) {
238  if (oldval != track->cluster[i].samples_in_chunk && track->cluster[i].chunkNum) {
239  avio_wb32(pb, track->cluster[i].chunkNum); // first chunk
240  avio_wb32(pb, track->cluster[i].samples_in_chunk); // samples per chunk
241  avio_wb32(pb, 0x1); // sample description index
242  oldval = track->cluster[i].samples_in_chunk;
243  index++;
244  }
245  }
246  curpos = avio_tell(pb);
247  avio_seek(pb, entryPos, SEEK_SET);
248  avio_wb32(pb, index); // rewrite size
249  avio_seek(pb, curpos, SEEK_SET);
250 
251  return update_size(pb, pos);
252 }
253 
254 /* Sync sample atom */
255 static int mov_write_stss_tag(AVIOContext *pb, MOVTrack *track, uint32_t flag)
256 {
257  int64_t curpos, entryPos;
258  int i, index = 0;
259  int64_t pos = avio_tell(pb);
260  avio_wb32(pb, 0); // size
261  ffio_wfourcc(pb, flag == MOV_SYNC_SAMPLE ? "stss" : "stps");
262  avio_wb32(pb, 0); // version & flags
263  entryPos = avio_tell(pb);
264  avio_wb32(pb, track->entry); // entry count
265  for (i = 0; i < track->entry; i++) {
266  if (track->cluster[i].flags & flag) {
267  avio_wb32(pb, i + 1);
268  index++;
269  }
270  }
271  curpos = avio_tell(pb);
272  avio_seek(pb, entryPos, SEEK_SET);
273  avio_wb32(pb, index); // rewrite size
274  avio_seek(pb, curpos, SEEK_SET);
275  return update_size(pb, pos);
276 }
277 
278 /* Sample dependency atom */
279 static int mov_write_sdtp_tag(AVIOContext *pb, MOVTrack *track)
280 {
281  int i;
282  uint8_t leading, dependent, reference, redundancy;
283  int64_t pos = avio_tell(pb);
284  avio_wb32(pb, 0); // size
285  ffio_wfourcc(pb, "sdtp");
286  avio_wb32(pb, 0); // version & flags
287  for (i = 0; i < track->entry; i++) {
288  dependent = MOV_SAMPLE_DEPENDENCY_YES;
289  leading = reference = redundancy = MOV_SAMPLE_DEPENDENCY_UNKNOWN;
290  if (track->cluster[i].flags & MOV_DISPOSABLE_SAMPLE) {
291  reference = MOV_SAMPLE_DEPENDENCY_NO;
292  }
293  if (track->cluster[i].flags & MOV_SYNC_SAMPLE) {
294  dependent = MOV_SAMPLE_DEPENDENCY_NO;
295  }
296  avio_w8(pb, (leading << 6) | (dependent << 4) |
297  (reference << 2) | redundancy);
298  }
299  return update_size(pb, pos);
300 }
301 
302 static int mov_write_amr_tag(AVIOContext *pb, MOVTrack *track)
303 {
304  avio_wb32(pb, 0x11); /* size */
305  if (track->mode == MODE_MOV) ffio_wfourcc(pb, "samr");
306  else ffio_wfourcc(pb, "damr");
307  ffio_wfourcc(pb, "FFMP");
308  avio_w8(pb, 0); /* decoder version */
309 
310  avio_wb16(pb, 0x81FF); /* Mode set (all modes for AMR_NB) */
311  avio_w8(pb, 0x00); /* Mode change period (no restriction) */
312  avio_w8(pb, 0x01); /* Frames per sample */
313  return 0x11;
314 }
315 
317 {
318  GetBitContext gbc;
319  PutBitContext pbc;
320  uint8_t buf[3];
321  int fscod, bsid, bsmod, acmod, lfeon, frmsizecod;
322 
323  if (track->vos_len < 7) {
324  av_log(s, AV_LOG_ERROR,
325  "Cannot write moov atom before AC3 packets."
326  " Set the delay_moov flag to fix this.\n");
327  return AVERROR(EINVAL);
328  }
329 
330  avio_wb32(pb, 11);
331  ffio_wfourcc(pb, "dac3");
332 
333  init_get_bits(&gbc, track->vos_data + 4, (track->vos_len - 4) * 8);
334  fscod = get_bits(&gbc, 2);
335  frmsizecod = get_bits(&gbc, 6);
336  bsid = get_bits(&gbc, 5);
337  bsmod = get_bits(&gbc, 3);
338  acmod = get_bits(&gbc, 3);
339  if (acmod == 2) {
340  skip_bits(&gbc, 2); // dsurmod
341  } else {
342  if ((acmod & 1) && acmod != 1)
343  skip_bits(&gbc, 2); // cmixlev
344  if (acmod & 4)
345  skip_bits(&gbc, 2); // surmixlev
346  }
347  lfeon = get_bits1(&gbc);
348 
349  init_put_bits(&pbc, buf, sizeof(buf));
350  put_bits(&pbc, 2, fscod);
351  put_bits(&pbc, 5, bsid);
352  put_bits(&pbc, 3, bsmod);
353  put_bits(&pbc, 3, acmod);
354  put_bits(&pbc, 1, lfeon);
355  put_bits(&pbc, 5, frmsizecod >> 1); // bit_rate_code
356  put_bits(&pbc, 5, 0); // reserved
357 
358  flush_put_bits(&pbc);
359  avio_write(pb, buf, sizeof(buf));
360 
361  return 11;
362 }
363 
364 struct eac3_info {
368 
369  /* Layout of the EC3SpecificBox */
370  /* maximum bitrate */
371  uint16_t data_rate;
372  /* number of independent substreams */
374  struct {
375  /* sample rate code (see ff_ac3_sample_rate_tab) 2 bits */
377  /* bit stream identification 5 bits */
379  /* one bit reserved */
380  /* audio service mixing (not supported yet) 1 bit */
381  /* bit stream mode 3 bits */
383  /* audio coding mode 3 bits */
385  /* sub woofer on 1 bit */
387  /* 3 bits reserved */
388  /* number of dependent substreams associated with this substream 4 bits */
390  /* channel locations of the dependent substream(s), if any, 9 bits */
391  uint16_t chan_loc;
392  /* if there is no dependent substream, then one bit reserved instead */
393  } substream[1]; /* TODO: support 8 independent substreams */
394 };
395 
396 #if CONFIG_AC3_PARSER
397 static int handle_eac3(MOVMuxContext *mov, AVPacket *pkt, MOVTrack *track)
398 {
399  AC3HeaderInfo *hdr = NULL;
400  struct eac3_info *info;
401  int num_blocks, ret;
402 
403  if (!track->eac3_priv && !(track->eac3_priv = av_mallocz(sizeof(*info))))
404  return AVERROR(ENOMEM);
405  info = track->eac3_priv;
406 
407  if (avpriv_ac3_parse_header(&hdr, pkt->data, pkt->size) < 0) {
408  /* drop the packets until we see a good one */
409  if (!track->entry) {
410  av_log(mov->fc, AV_LOG_WARNING, "Dropping invalid packet from start of the stream\n");
411  ret = 0;
412  } else
413  ret = AVERROR_INVALIDDATA;
414  goto end;
415  }
416 
417  info->data_rate = FFMAX(info->data_rate, hdr->bit_rate / 1000);
418  num_blocks = hdr->num_blocks;
419 
420  if (!info->ec3_done) {
421  /* AC-3 substream must be the first one */
422  if (hdr->bitstream_id <= 10 && hdr->substreamid != 0) {
423  ret = AVERROR(EINVAL);
424  goto end;
425  }
426 
427  /* this should always be the case, given that our AC-3 parser
428  * concatenates dependent frames to their independent parent */
430  /* substream ids must be incremental */
431  if (hdr->substreamid > info->num_ind_sub + 1) {
432  ret = AVERROR(EINVAL);
433  goto end;
434  }
435 
436  if (hdr->substreamid == info->num_ind_sub + 1) {
437  //info->num_ind_sub++;
438  avpriv_request_sample(mov->fc, "Multiple independent substreams");
439  ret = AVERROR_PATCHWELCOME;
440  goto end;
441  } else if (hdr->substreamid < info->num_ind_sub ||
442  hdr->substreamid == 0 && info->substream[0].bsid) {
443  info->ec3_done = 1;
444  goto concatenate;
445  }
446  } else {
447  if (hdr->substreamid != 0) {
448  avpriv_request_sample(mov->fc, "Multiple non EAC3 independent substreams");
449  ret = AVERROR_PATCHWELCOME;
450  goto end;
451  }
452  }
453 
454  /* fill the info needed for the "dec3" atom */
455  info->substream[hdr->substreamid].fscod = hdr->sr_code;
456  info->substream[hdr->substreamid].bsid = hdr->bitstream_id;
457  info->substream[hdr->substreamid].bsmod = hdr->bitstream_mode;
458  info->substream[hdr->substreamid].acmod = hdr->channel_mode;
459  info->substream[hdr->substreamid].lfeon = hdr->lfe_on;
460 
461  /* Parse dependent substream(s), if any */
462  if (pkt->size != hdr->frame_size) {
463  int cumul_size = hdr->frame_size;
464  int parent = hdr->substreamid;
465 
466  while (cumul_size != pkt->size) {
467  GetBitContext gbc;
468  int i;
469  ret = avpriv_ac3_parse_header(&hdr, pkt->data + cumul_size, pkt->size - cumul_size);
470  if (ret < 0)
471  goto end;
473  ret = AVERROR(EINVAL);
474  goto end;
475  }
476  info->substream[parent].num_dep_sub++;
477  ret /= 8;
478 
479  /* header is parsed up to lfeon, but custom channel map may be needed */
480  init_get_bits8(&gbc, pkt->data + cumul_size + ret, pkt->size - cumul_size - ret);
481  /* skip bsid */
482  skip_bits(&gbc, 5);
483  /* skip volume control params */
484  for (i = 0; i < (hdr->channel_mode ? 1 : 2); i++) {
485  skip_bits(&gbc, 5); // skip dialog normalization
486  if (get_bits1(&gbc)) {
487  skip_bits(&gbc, 8); // skip compression gain word
488  }
489  }
490  /* get the dependent stream channel map, if exists */
491  if (get_bits1(&gbc))
492  info->substream[parent].chan_loc |= (get_bits(&gbc, 16) >> 5) & 0x1f;
493  else
494  info->substream[parent].chan_loc |= hdr->channel_mode;
495  cumul_size += hdr->frame_size;
496  }
497  }
498  }
499 
500 concatenate:
501  if (!info->num_blocks && num_blocks == 6) {
502  ret = pkt->size;
503  goto end;
504  }
505  else if (info->num_blocks + num_blocks > 6) {
506  ret = AVERROR_INVALIDDATA;
507  goto end;
508  }
509 
510  if (!info->num_blocks) {
511  ret = av_packet_ref(&info->pkt, pkt);
512  if (!ret)
513  info->num_blocks = num_blocks;
514  goto end;
515  } else {
516  if ((ret = av_grow_packet(&info->pkt, pkt->size)) < 0)
517  goto end;
518  memcpy(info->pkt.data + info->pkt.size - pkt->size, pkt->data, pkt->size);
519  info->num_blocks += num_blocks;
520  info->pkt.duration += pkt->duration;
521  if ((ret = av_copy_packet_side_data(&info->pkt, pkt)) < 0)
522  goto end;
523  if (info->num_blocks != 6)
524  goto end;
525  av_packet_unref(pkt);
526  av_packet_move_ref(pkt, &info->pkt);
527  info->num_blocks = 0;
528  }
529  ret = pkt->size;
530 
531 end:
532  av_free(hdr);
533 
534  return ret;
535 }
536 #endif
537 
539 {
540  PutBitContext pbc;
541  uint8_t *buf;
542  struct eac3_info *info;
543  int size, i;
544 
545  if (!track->eac3_priv) {
546  av_log(s, AV_LOG_ERROR,
547  "Cannot write moov atom before EAC3 packets parsed.\n");
548  return AVERROR(EINVAL);
549  }
550 
551  info = track->eac3_priv;
552  size = 2 + ((34 * (info->num_ind_sub + 1) + 7) >> 3);
553  buf = av_malloc(size);
554  if (!buf) {
555  return AVERROR(ENOMEM);
556  }
557 
558  init_put_bits(&pbc, buf, size);
559  put_bits(&pbc, 13, info->data_rate);
560  put_bits(&pbc, 3, info->num_ind_sub);
561  for (i = 0; i <= info->num_ind_sub; i++) {
562  put_bits(&pbc, 2, info->substream[i].fscod);
563  put_bits(&pbc, 5, info->substream[i].bsid);
564  put_bits(&pbc, 1, 0); /* reserved */
565  put_bits(&pbc, 1, 0); /* asvc */
566  put_bits(&pbc, 3, info->substream[i].bsmod);
567  put_bits(&pbc, 3, info->substream[i].acmod);
568  put_bits(&pbc, 1, info->substream[i].lfeon);
569  put_bits(&pbc, 5, 0); /* reserved */
570  put_bits(&pbc, 4, info->substream[i].num_dep_sub);
571  if (!info->substream[i].num_dep_sub) {
572  put_bits(&pbc, 1, 0); /* reserved */
573  } else {
574  put_bits(&pbc, 9, info->substream[i].chan_loc);
575  }
576  }
577  flush_put_bits(&pbc);
578  size = put_bits_count(&pbc) >> 3;
579 
580  avio_wb32(pb, size + 8);
581  ffio_wfourcc(pb, "dec3");
582  avio_write(pb, buf, size);
583 
584  av_free(buf);
585 
586  return size;
587 }
588 
589 /**
590  * This function writes extradata "as is".
591  * Extradata must be formatted like a valid atom (with size and tag).
592  */
594 {
595  avio_write(pb, track->par->extradata, track->par->extradata_size);
596  return track->par->extradata_size;
597 }
598 
600 {
601  avio_wb32(pb, 10);
602  ffio_wfourcc(pb, "enda");
603  avio_wb16(pb, 1); /* little endian */
604  return 10;
605 }
606 
608 {
609  avio_wb32(pb, 10);
610  ffio_wfourcc(pb, "enda");
611  avio_wb16(pb, 0); /* big endian */
612  return 10;
613 }
614 
615 static void put_descr(AVIOContext *pb, int tag, unsigned int size)
616 {
617  int i = 3;
618  avio_w8(pb, tag);
619  for (; i > 0; i--)
620  avio_w8(pb, (size >> (7 * i)) | 0x80);
621  avio_w8(pb, size & 0x7F);
622 }
623 
624 static unsigned compute_avg_bitrate(MOVTrack *track)
625 {
626  uint64_t size = 0;
627  int i;
628  if (!track->track_duration)
629  return 0;
630  for (i = 0; i < track->entry; i++)
631  size += track->cluster[i].size;
632  return size * 8 * track->timescale / track->track_duration;
633 }
634 
635 static int mov_write_esds_tag(AVIOContext *pb, MOVTrack *track) // Basic
636 {
637  AVCPBProperties *props;
638  int64_t pos = avio_tell(pb);
639  int decoder_specific_info_len = track->vos_len ? 5 + track->vos_len : 0;
640  unsigned avg_bitrate;
641 
642  avio_wb32(pb, 0); // size
643  ffio_wfourcc(pb, "esds");
644  avio_wb32(pb, 0); // Version
645 
646  // ES descriptor
647  put_descr(pb, 0x03, 3 + 5+13 + decoder_specific_info_len + 5+1);
648  avio_wb16(pb, track->track_id);
649  avio_w8(pb, 0x00); // flags (= no flags)
650 
651  // DecoderConfig descriptor
652  put_descr(pb, 0x04, 13 + decoder_specific_info_len);
653 
654  // Object type indication
655  if ((track->par->codec_id == AV_CODEC_ID_MP2 ||
656  track->par->codec_id == AV_CODEC_ID_MP3) &&
657  track->par->sample_rate > 24000)
658  avio_w8(pb, 0x6B); // 11172-3
659  else
661 
662  // the following fields is made of 6 bits to identify the streamtype (4 for video, 5 for audio)
663  // plus 1 bit to indicate upstream and 1 bit set to 1 (reserved)
664  if (track->par->codec_id == AV_CODEC_ID_DVD_SUBTITLE)
665  avio_w8(pb, (0x38 << 2) | 1); // flags (= NeroSubpicStream)
666  else if (track->par->codec_type == AVMEDIA_TYPE_AUDIO)
667  avio_w8(pb, 0x15); // flags (= Audiostream)
668  else
669  avio_w8(pb, 0x11); // flags (= Visualstream)
670 
672  NULL);
673 
674  avio_wb24(pb, props ? props->buffer_size / 8 : 0); // Buffersize DB
675 
676  avg_bitrate = compute_avg_bitrate(track);
677  avio_wb32(pb, props ? FFMAX3(props->max_bitrate, props->avg_bitrate, avg_bitrate) : FFMAX(track->par->bit_rate, avg_bitrate)); // maxbitrate (FIXME should be max rate in any 1 sec window)
678  avio_wb32(pb, avg_bitrate);
679 
680  if (track->vos_len) {
681  // DecoderSpecific info descriptor
682  put_descr(pb, 0x05, track->vos_len);
683  avio_write(pb, track->vos_data, track->vos_len);
684  }
685 
686  // SL descriptor
687  put_descr(pb, 0x06, 1);
688  avio_w8(pb, 0x02);
689  return update_size(pb, pos);
690 }
691 
693 {
694  return codec_id == AV_CODEC_ID_PCM_S24LE ||
695  codec_id == AV_CODEC_ID_PCM_S32LE ||
696  codec_id == AV_CODEC_ID_PCM_F32LE ||
697  codec_id == AV_CODEC_ID_PCM_F64LE;
698 }
699 
701 {
702  return codec_id == AV_CODEC_ID_PCM_S24BE ||
703  codec_id == AV_CODEC_ID_PCM_S32BE ||
704  codec_id == AV_CODEC_ID_PCM_F32BE ||
705  codec_id == AV_CODEC_ID_PCM_F64BE;
706 }
707 
709 {
710  int ret;
711  int64_t pos = avio_tell(pb);
712  avio_wb32(pb, 0);
713  avio_wl32(pb, track->tag); // store it byteswapped
714  track->par->codec_tag = av_bswap16(track->tag >> 16);
715  if ((ret = ff_put_wav_header(s, pb, track->par, 0)) < 0)
716  return ret;
717  return update_size(pb, pos);
718 }
719 
721 {
722  int ret;
723  int64_t pos = avio_tell(pb);
724  avio_wb32(pb, 0);
725  ffio_wfourcc(pb, "wfex");
726  if ((ret = ff_put_wav_header(s, pb, track->st->codecpar, FF_PUT_WAV_HEADER_FORCE_WAVEFORMATEX)) < 0)
727  return ret;
728  return update_size(pb, pos);
729 }
730 
731 static int mov_write_dfla_tag(AVIOContext *pb, MOVTrack *track)
732 {
733  int64_t pos = avio_tell(pb);
734  avio_wb32(pb, 0);
735  ffio_wfourcc(pb, "dfLa");
736  avio_w8(pb, 0); /* version */
737  avio_wb24(pb, 0); /* flags */
738 
739  /* Expect the encoder to pass a METADATA_BLOCK_TYPE_STREAMINFO. */
740  if (track->par->extradata_size != FLAC_STREAMINFO_SIZE)
741  return AVERROR_INVALIDDATA;
742 
743  /* TODO: Write other METADATA_BLOCK_TYPEs if the encoder makes them available. */
744  avio_w8(pb, 1 << 7 | FLAC_METADATA_TYPE_STREAMINFO); /* LastMetadataBlockFlag << 7 | BlockType */
745  avio_wb24(pb, track->par->extradata_size); /* Length */
746  avio_write(pb, track->par->extradata, track->par->extradata_size); /* BlockData[Length] */
747 
748  return update_size(pb, pos);
749 }
750 
752 {
753  int64_t pos = avio_tell(pb);
754  avio_wb32(pb, 0);
755  ffio_wfourcc(pb, "dOps");
756  avio_w8(pb, 0); /* Version */
757  if (track->par->extradata_size < 19) {
758  av_log(s, AV_LOG_ERROR, "invalid extradata size\n");
759  return AVERROR_INVALIDDATA;
760  }
761  /* extradata contains an Ogg OpusHead, other than byte-ordering and
762  OpusHead's preceeding magic/version, OpusSpecificBox is currently
763  identical. */
764  avio_w8(pb, AV_RB8(track->par->extradata + 9)); /* OuputChannelCount */
765  avio_wb16(pb, AV_RL16(track->par->extradata + 10)); /* PreSkip */
766  avio_wb32(pb, AV_RL32(track->par->extradata + 12)); /* InputSampleRate */
767  avio_wb16(pb, AV_RL16(track->par->extradata + 16)); /* OutputGain */
768  /* Write the rest of the header out without byte-swapping. */
769  avio_write(pb, track->par->extradata + 18, track->par->extradata_size - 18);
770 
771  return update_size(pb, pos);
772 }
773 
775 {
776  int64_t pos = avio_tell(pb);
777  int length;
778  avio_wb32(pb, 0);
779  ffio_wfourcc(pb, "dmlp");
780 
781  if (track->vos_len < 20) {
782  av_log(s, AV_LOG_ERROR,
783  "Cannot write moov atom before TrueHD packets."
784  " Set the delay_moov flag to fix this.\n");
785  return AVERROR(EINVAL);
786  }
787 
788  length = (AV_RB16(track->vos_data) & 0xFFF) * 2;
789  if (length < 20 || length > track->vos_len)
790  return AVERROR_INVALIDDATA;
791 
792  // Only TrueHD is supported
793  if (AV_RB32(track->vos_data + 4) != 0xF8726FBA)
794  return AVERROR_INVALIDDATA;
795 
796  avio_wb32(pb, AV_RB32(track->vos_data + 8)); /* format_info */
797  avio_wb16(pb, AV_RB16(track->vos_data + 18) << 1); /* peak_data_rate */
798  avio_wb32(pb, 0); /* reserved */
799 
800  return update_size(pb, pos);
801 }
802 
804 {
805  uint32_t layout_tag, bitmap;
806  int64_t pos = avio_tell(pb);
807 
808  layout_tag = ff_mov_get_channel_layout_tag(track->par->codec_id,
809  track->par->channel_layout,
810  &bitmap);
811  if (!layout_tag) {
812  av_log(s, AV_LOG_WARNING, "not writing 'chan' tag due to "
813  "lack of channel information\n");
814  return 0;
815  }
816 
817  if (track->multichannel_as_mono)
818  return 0;
819 
820  avio_wb32(pb, 0); // Size
821  ffio_wfourcc(pb, "chan"); // Type
822  avio_w8(pb, 0); // Version
823  avio_wb24(pb, 0); // Flags
824  avio_wb32(pb, layout_tag); // mChannelLayoutTag
825  avio_wb32(pb, bitmap); // mChannelBitmap
826  avio_wb32(pb, 0); // mNumberChannelDescriptions
827 
828  return update_size(pb, pos);
829 }
830 
832 {
833  int64_t pos = avio_tell(pb);
834 
835  avio_wb32(pb, 0); /* size */
836  ffio_wfourcc(pb, "wave");
837 
838  if (track->par->codec_id != AV_CODEC_ID_QDM2) {
839  avio_wb32(pb, 12); /* size */
840  ffio_wfourcc(pb, "frma");
841  avio_wl32(pb, track->tag);
842  }
843 
844  if (track->par->codec_id == AV_CODEC_ID_AAC) {
845  /* useless atom needed by mplayer, ipod, not needed by quicktime */
846  avio_wb32(pb, 12); /* size */
847  ffio_wfourcc(pb, "mp4a");
848  avio_wb32(pb, 0);
849  mov_write_esds_tag(pb, track);
850  } else if (mov_pcm_le_gt16(track->par->codec_id)) {
851  mov_write_enda_tag(pb);
852  } else if (mov_pcm_be_gt16(track->par->codec_id)) {
854  } else if (track->par->codec_id == AV_CODEC_ID_AMR_NB) {
855  mov_write_amr_tag(pb, track);
856  } else if (track->par->codec_id == AV_CODEC_ID_AC3) {
857  mov_write_ac3_tag(s, pb, track);
858  } else if (track->par->codec_id == AV_CODEC_ID_EAC3) {
859  mov_write_eac3_tag(s, pb, track);
860  } else if (track->par->codec_id == AV_CODEC_ID_ALAC ||
861  track->par->codec_id == AV_CODEC_ID_QDM2) {
862  mov_write_extradata_tag(pb, track);
863  } else if (track->par->codec_id == AV_CODEC_ID_ADPCM_MS ||
865  mov_write_ms_tag(s, pb, track);
866  }
867 
868  avio_wb32(pb, 8); /* size */
869  avio_wb32(pb, 0); /* null tag */
870 
871  return update_size(pb, pos);
872 }
873 
875 {
876  uint8_t *unescaped;
877  const uint8_t *start, *next, *end = track->vos_data + track->vos_len;
878  int unescaped_size, seq_found = 0;
879  int level = 0, interlace = 0;
880  int packet_seq = track->vc1_info.packet_seq;
881  int packet_entry = track->vc1_info.packet_entry;
882  int slices = track->vc1_info.slices;
883  PutBitContext pbc;
884 
885  if (track->start_dts == AV_NOPTS_VALUE) {
886  /* No packets written yet, vc1_info isn't authoritative yet. */
887  /* Assume inline sequence and entry headers. */
888  packet_seq = packet_entry = 1;
890  "moov atom written before any packets, unable to write correct "
891  "dvc1 atom. Set the delay_moov flag to fix this.\n");
892  }
893 
894  unescaped = av_mallocz(track->vos_len + AV_INPUT_BUFFER_PADDING_SIZE);
895  if (!unescaped)
896  return AVERROR(ENOMEM);
897  start = find_next_marker(track->vos_data, end);
898  for (next = start; next < end; start = next) {
899  GetBitContext gb;
900  int size;
901  next = find_next_marker(start + 4, end);
902  size = next - start - 4;
903  if (size <= 0)
904  continue;
905  unescaped_size = vc1_unescape_buffer(start + 4, size, unescaped);
906  init_get_bits(&gb, unescaped, 8 * unescaped_size);
907  if (AV_RB32(start) == VC1_CODE_SEQHDR) {
908  int profile = get_bits(&gb, 2);
909  if (profile != PROFILE_ADVANCED) {
910  av_free(unescaped);
911  return AVERROR(ENOSYS);
912  }
913  seq_found = 1;
914  level = get_bits(&gb, 3);
915  /* chromaformat, frmrtq_postproc, bitrtq_postproc, postprocflag,
916  * width, height */
917  skip_bits_long(&gb, 2 + 3 + 5 + 1 + 2*12);
918  skip_bits(&gb, 1); /* broadcast */
919  interlace = get_bits1(&gb);
920  skip_bits(&gb, 4); /* tfcntrflag, finterpflag, reserved, psf */
921  }
922  }
923  if (!seq_found) {
924  av_free(unescaped);
925  return AVERROR(ENOSYS);
926  }
927 
928  init_put_bits(&pbc, buf, 7);
929  /* VC1DecSpecStruc */
930  put_bits(&pbc, 4, 12); /* profile - advanced */
931  put_bits(&pbc, 3, level);
932  put_bits(&pbc, 1, 0); /* reserved */
933  /* VC1AdvDecSpecStruc */
934  put_bits(&pbc, 3, level);
935  put_bits(&pbc, 1, 0); /* cbr */
936  put_bits(&pbc, 6, 0); /* reserved */
937  put_bits(&pbc, 1, !interlace); /* no interlace */
938  put_bits(&pbc, 1, !packet_seq); /* no multiple seq */
939  put_bits(&pbc, 1, !packet_entry); /* no multiple entry */
940  put_bits(&pbc, 1, !slices); /* no slice code */
941  put_bits(&pbc, 1, 0); /* no bframe */
942  put_bits(&pbc, 1, 0); /* reserved */
943 
944  /* framerate */
945  if (track->st->avg_frame_rate.num > 0 && track->st->avg_frame_rate.den > 0)
946  put_bits32(&pbc, track->st->avg_frame_rate.num / track->st->avg_frame_rate.den);
947  else
948  put_bits32(&pbc, 0xffffffff);
949 
950  flush_put_bits(&pbc);
951 
952  av_free(unescaped);
953 
954  return 0;
955 }
956 
957 static int mov_write_dvc1_tag(AVIOContext *pb, MOVTrack *track)
958 {
959  uint8_t buf[7] = { 0 };
960  int ret;
961 
962  if ((ret = mov_write_dvc1_structs(track, buf)) < 0)
963  return ret;
964 
965  avio_wb32(pb, track->vos_len + 8 + sizeof(buf));
966  ffio_wfourcc(pb, "dvc1");
967  avio_write(pb, buf, sizeof(buf));
968  avio_write(pb, track->vos_data, track->vos_len);
969 
970  return 0;
971 }
972 
973 static int mov_write_glbl_tag(AVIOContext *pb, MOVTrack *track)
974 {
975  avio_wb32(pb, track->vos_len + 8);
976  ffio_wfourcc(pb, "glbl");
977  avio_write(pb, track->vos_data, track->vos_len);
978  return 8 + track->vos_len;
979 }
980 
981 /**
982  * Compute flags for 'lpcm' tag.
983  * See CoreAudioTypes and AudioStreamBasicDescription at Apple.
984  */
986 {
987  switch (codec_id) {
990  return 11;
993  return 9;
994  case AV_CODEC_ID_PCM_U8:
995  return 10;
999  return 14;
1000  case AV_CODEC_ID_PCM_S8:
1001  case AV_CODEC_ID_PCM_S16LE:
1002  case AV_CODEC_ID_PCM_S24LE:
1003  case AV_CODEC_ID_PCM_S32LE:
1004  return 12;
1005  default:
1006  return 0;
1007  }
1008 }
1009 
1010 static int get_cluster_duration(MOVTrack *track, int cluster_idx)
1011 {
1012  int64_t next_dts;
1013 
1014  if (cluster_idx >= track->entry)
1015  return 0;
1016 
1017  if (cluster_idx + 1 == track->entry)
1018  next_dts = track->track_duration + track->start_dts;
1019  else
1020  next_dts = track->cluster[cluster_idx + 1].dts;
1021 
1022  next_dts -= track->cluster[cluster_idx].dts;
1023 
1024  av_assert0(next_dts >= 0);
1025  av_assert0(next_dts <= INT_MAX);
1026 
1027  return next_dts;
1028 }
1029 
1031 {
1032  int i, first_duration;
1033 
1034 // return track->par->frame_size;
1035 
1036  /* use 1 for raw PCM */
1037  if (!track->audio_vbr)
1038  return 1;
1039 
1040  /* check to see if duration is constant for all clusters */
1041  if (!track->entry)
1042  return 0;
1043  first_duration = get_cluster_duration(track, 0);
1044  for (i = 1; i < track->entry; i++) {
1045  if (get_cluster_duration(track, i) != first_duration)
1046  return 0;
1047  }
1048  return first_duration;
1049 }
1050 
1052 {
1053  int64_t pos = avio_tell(pb);
1054  int version = 0;
1055  uint32_t tag = track->tag;
1056  int ret = 0;
1057 
1058  if (track->mode == MODE_MOV) {
1059  if (track->timescale > UINT16_MAX || !track->par->channels) {
1060  if (mov_get_lpcm_flags(track->par->codec_id))
1061  tag = AV_RL32("lpcm");
1062  version = 2;
1063  } else if (track->audio_vbr || mov_pcm_le_gt16(track->par->codec_id) ||
1064  mov_pcm_be_gt16(track->par->codec_id) ||
1065  track->par->codec_id == AV_CODEC_ID_ADPCM_MS ||
1066  track->par->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV ||
1067  track->par->codec_id == AV_CODEC_ID_QDM2) {
1068  version = 1;
1069  }
1070  }
1071 
1072  avio_wb32(pb, 0); /* size */
1073  if (mov->encryption_scheme != MOV_ENC_NONE) {
1074  ffio_wfourcc(pb, "enca");
1075  } else {
1076  avio_wl32(pb, tag); // store it byteswapped
1077  }
1078  avio_wb32(pb, 0); /* Reserved */
1079  avio_wb16(pb, 0); /* Reserved */
1080  avio_wb16(pb, 1); /* Data-reference index, XXX == 1 */
1081 
1082  /* SoundDescription */
1083  avio_wb16(pb, version); /* Version */
1084  avio_wb16(pb, 0); /* Revision level */
1085  avio_wb32(pb, 0); /* Reserved */
1086 
1087  if (version == 2) {
1088  avio_wb16(pb, 3);
1089  avio_wb16(pb, 16);
1090  avio_wb16(pb, 0xfffe);
1091  avio_wb16(pb, 0);
1092  avio_wb32(pb, 0x00010000);
1093  avio_wb32(pb, 72);
1094  avio_wb64(pb, av_double2int(track->par->sample_rate));
1095  avio_wb32(pb, track->par->channels);
1096  avio_wb32(pb, 0x7F000000);
1098  avio_wb32(pb, mov_get_lpcm_flags(track->par->codec_id));
1099  avio_wb32(pb, track->sample_size);
1100  avio_wb32(pb, get_samples_per_packet(track));
1101  } else {
1102  if (track->mode == MODE_MOV) {
1103  avio_wb16(pb, track->par->channels);
1104  if (track->par->codec_id == AV_CODEC_ID_PCM_U8 ||
1105  track->par->codec_id == AV_CODEC_ID_PCM_S8)
1106  avio_wb16(pb, 8); /* bits per sample */
1107  else if (track->par->codec_id == AV_CODEC_ID_ADPCM_G726)
1108  avio_wb16(pb, track->par->bits_per_coded_sample);
1109  else
1110  avio_wb16(pb, 16);
1111  avio_wb16(pb, track->audio_vbr ? -2 : 0); /* compression ID */
1112  } else { /* reserved for mp4/3gp */
1113  if (track->par->codec_id == AV_CODEC_ID_FLAC ||
1114  track->par->codec_id == AV_CODEC_ID_ALAC ||
1115  track->par->codec_id == AV_CODEC_ID_OPUS) {
1116  avio_wb16(pb, track->par->channels);
1117  } else {
1118  avio_wb16(pb, 2);
1119  }
1120  if (track->par->codec_id == AV_CODEC_ID_FLAC ||
1121  track->par->codec_id == AV_CODEC_ID_ALAC) {
1122  avio_wb16(pb, track->par->bits_per_raw_sample);
1123  } else {
1124  avio_wb16(pb, 16);
1125  }
1126  avio_wb16(pb, 0);
1127  }
1128 
1129  avio_wb16(pb, 0); /* packet size (= 0) */
1130  if (track->par->codec_id == AV_CODEC_ID_OPUS)
1131  avio_wb16(pb, 48000);
1132  else if (track->par->codec_id == AV_CODEC_ID_TRUEHD)
1133  avio_wb32(pb, track->par->sample_rate);
1134  else
1135  avio_wb16(pb, track->par->sample_rate <= UINT16_MAX ?
1136  track->par->sample_rate : 0);
1137 
1138  if (track->par->codec_id != AV_CODEC_ID_TRUEHD)
1139  avio_wb16(pb, 0); /* Reserved */
1140  }
1141 
1142  if (version == 1) { /* SoundDescription V1 extended info */
1143  if (mov_pcm_le_gt16(track->par->codec_id) ||
1144  mov_pcm_be_gt16(track->par->codec_id))
1145  avio_wb32(pb, 1); /* must be 1 for uncompressed formats */
1146  else
1147  avio_wb32(pb, track->par->frame_size); /* Samples per packet */
1148  avio_wb32(pb, track->sample_size / track->par->channels); /* Bytes per packet */
1149  avio_wb32(pb, track->sample_size); /* Bytes per frame */
1150  avio_wb32(pb, 2); /* Bytes per sample */
1151  }
1152 
1153  if (track->mode == MODE_MOV &&
1154  (track->par->codec_id == AV_CODEC_ID_AAC ||
1155  track->par->codec_id == AV_CODEC_ID_AC3 ||
1156  track->par->codec_id == AV_CODEC_ID_EAC3 ||
1157  track->par->codec_id == AV_CODEC_ID_AMR_NB ||
1158  track->par->codec_id == AV_CODEC_ID_ALAC ||
1159  track->par->codec_id == AV_CODEC_ID_ADPCM_MS ||
1160  track->par->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV ||
1161  track->par->codec_id == AV_CODEC_ID_QDM2 ||
1162  (mov_pcm_le_gt16(track->par->codec_id) && version==1) ||
1163  (mov_pcm_be_gt16(track->par->codec_id) && version==1)))
1164  ret = mov_write_wave_tag(s, pb, track);
1165  else if (track->tag == MKTAG('m','p','4','a'))
1166  ret = mov_write_esds_tag(pb, track);
1167  else if (track->par->codec_id == AV_CODEC_ID_AMR_NB)
1168  ret = mov_write_amr_tag(pb, track);
1169  else if (track->par->codec_id == AV_CODEC_ID_AC3)
1170  ret = mov_write_ac3_tag(s, pb, track);
1171  else if (track->par->codec_id == AV_CODEC_ID_EAC3)
1172  ret = mov_write_eac3_tag(s, pb, track);
1173  else if (track->par->codec_id == AV_CODEC_ID_ALAC)
1174  ret = mov_write_extradata_tag(pb, track);
1175  else if (track->par->codec_id == AV_CODEC_ID_WMAPRO)
1176  ret = mov_write_wfex_tag(s, pb, track);
1177  else if (track->par->codec_id == AV_CODEC_ID_FLAC)
1178  ret = mov_write_dfla_tag(pb, track);
1179  else if (track->par->codec_id == AV_CODEC_ID_OPUS)
1180  ret = mov_write_dops_tag(s, pb, track);
1181  else if (track->par->codec_id == AV_CODEC_ID_TRUEHD)
1182  ret = mov_write_dmlp_tag(s, pb, track);
1183  else if (track->vos_len > 0)
1184  ret = mov_write_glbl_tag(pb, track);
1185 
1186  if (ret < 0)
1187  return ret;
1188 
1189  if (track->mode == MODE_MOV && track->par->codec_type == AVMEDIA_TYPE_AUDIO
1190  && ((ret = mov_write_chan_tag(s, pb, track)) < 0)) {
1191  return ret;
1192  }
1193 
1194  if (mov->encryption_scheme != MOV_ENC_NONE
1195  && ((ret = ff_mov_cenc_write_sinf_tag(track, pb, mov->encryption_kid)) < 0)) {
1196  return ret;
1197  }
1198 
1199  ret = update_size(pb, pos);
1200  return ret;
1201 }
1202 
1204 {
1205  avio_wb32(pb, 0xf); /* size */
1206  ffio_wfourcc(pb, "d263");
1207  ffio_wfourcc(pb, "FFMP");
1208  avio_w8(pb, 0); /* decoder version */
1209  /* FIXME use AVCodecContext level/profile, when encoder will set values */
1210  avio_w8(pb, 0xa); /* level */
1211  avio_w8(pb, 0); /* profile */
1212  return 0xf;
1213 }
1214 
1215 static int mov_write_av1c_tag(AVIOContext *pb, MOVTrack *track)
1216 {
1217  int64_t pos = avio_tell(pb);
1218 
1219  avio_wb32(pb, 0);
1220  ffio_wfourcc(pb, "av1C");
1221  ff_isom_write_av1c(pb, track->vos_data, track->vos_len);
1222  return update_size(pb, pos);
1223 }
1224 
1225 static int mov_write_avcc_tag(AVIOContext *pb, MOVTrack *track)
1226 {
1227  int64_t pos = avio_tell(pb);
1228 
1229  avio_wb32(pb, 0);
1230  ffio_wfourcc(pb, "avcC");
1231  ff_isom_write_avcc(pb, track->vos_data, track->vos_len);
1232  return update_size(pb, pos);
1233 }
1234 
1236 {
1237  int64_t pos = avio_tell(pb);
1238 
1239  avio_wb32(pb, 0);
1240  ffio_wfourcc(pb, "vpcC");
1241  avio_w8(pb, 1); /* version */
1242  avio_wb24(pb, 0); /* flags */
1243  ff_isom_write_vpcc(s, pb, track->par);
1244  return update_size(pb, pos);
1245 }
1246 
1247 static int mov_write_hvcc_tag(AVIOContext *pb, MOVTrack *track)
1248 {
1249  int64_t pos = avio_tell(pb);
1250 
1251  avio_wb32(pb, 0);
1252  ffio_wfourcc(pb, "hvcC");
1253  if (track->tag == MKTAG('h','v','c','1'))
1254  ff_isom_write_hvcc(pb, track->vos_data, track->vos_len, 1);
1255  else
1256  ff_isom_write_hvcc(pb, track->vos_data, track->vos_len, 0);
1257  return update_size(pb, pos);
1258 }
1259 
1260 /* also used by all avid codecs (dv, imx, meridien) and their variants */
1261 static int mov_write_avid_tag(AVIOContext *pb, MOVTrack *track)
1262 {
1263  int i;
1264  int interlaced;
1265  int cid;
1266  int display_width = track->par->width;
1267 
1268  if (track->vos_data && track->vos_len > 0x29) {
1269  if (ff_dnxhd_parse_header_prefix(track->vos_data) != 0) {
1270  /* looks like a DNxHD bit stream */
1271  interlaced = (track->vos_data[5] & 2);
1272  cid = AV_RB32(track->vos_data + 0x28);
1273  } else {
1274  av_log(NULL, AV_LOG_WARNING, "Could not locate DNxHD bit stream in vos_data\n");
1275  return 0;
1276  }
1277  } else {
1278  av_log(NULL, AV_LOG_WARNING, "Could not locate DNxHD bit stream, vos_data too small\n");
1279  return 0;
1280  }
1281 
1282  avio_wb32(pb, 24); /* size */
1283  ffio_wfourcc(pb, "ACLR");
1284  ffio_wfourcc(pb, "ACLR");
1285  ffio_wfourcc(pb, "0001");
1286  if (track->par->color_range == AVCOL_RANGE_MPEG || /* Legal range (16-235) */
1288  avio_wb32(pb, 1); /* Corresponds to 709 in official encoder */
1289  } else { /* Full range (0-255) */
1290  avio_wb32(pb, 2); /* Corresponds to RGB in official encoder */
1291  }
1292  avio_wb32(pb, 0); /* unknown */
1293 
1294  if (track->tag == MKTAG('A','V','d','h')) {
1295  avio_wb32(pb, 32);
1296  ffio_wfourcc(pb, "ADHR");
1297  ffio_wfourcc(pb, "0001");
1298  avio_wb32(pb, cid);
1299  avio_wb32(pb, 0); /* unknown */
1300  avio_wb32(pb, 1); /* unknown */
1301  avio_wb32(pb, 0); /* unknown */
1302  avio_wb32(pb, 0); /* unknown */
1303  return 0;
1304  }
1305 
1306  avio_wb32(pb, 24); /* size */
1307  ffio_wfourcc(pb, "APRG");
1308  ffio_wfourcc(pb, "APRG");
1309  ffio_wfourcc(pb, "0001");
1310  avio_wb32(pb, 1); /* unknown */
1311  avio_wb32(pb, 0); /* unknown */
1312 
1313  avio_wb32(pb, 120); /* size */
1314  ffio_wfourcc(pb, "ARES");
1315  ffio_wfourcc(pb, "ARES");
1316  ffio_wfourcc(pb, "0001");
1317  avio_wb32(pb, cid); /* dnxhd cid, some id ? */
1318  if ( track->par->sample_aspect_ratio.num > 0
1319  && track->par->sample_aspect_ratio.den > 0)
1320  display_width = display_width * track->par->sample_aspect_ratio.num / track->par->sample_aspect_ratio.den;
1321  avio_wb32(pb, display_width);
1322  /* values below are based on samples created with quicktime and avid codecs */
1323  if (interlaced) {
1324  avio_wb32(pb, track->par->height / 2);
1325  avio_wb32(pb, 2); /* unknown */
1326  avio_wb32(pb, 0); /* unknown */
1327  avio_wb32(pb, 4); /* unknown */
1328  } else {
1329  avio_wb32(pb, track->par->height);
1330  avio_wb32(pb, 1); /* unknown */
1331  avio_wb32(pb, 0); /* unknown */
1332  if (track->par->height == 1080)
1333  avio_wb32(pb, 5); /* unknown */
1334  else
1335  avio_wb32(pb, 6); /* unknown */
1336  }
1337  /* padding */
1338  for (i = 0; i < 10; i++)
1339  avio_wb64(pb, 0);
1340 
1341  return 0;
1342 }
1343 
1344 static int mov_write_dpxe_tag(AVIOContext *pb, MOVTrack *track)
1345 {
1346  avio_wb32(pb, 12);
1347  ffio_wfourcc(pb, "DpxE");
1348  if (track->par->extradata_size >= 12 &&
1349  !memcmp(&track->par->extradata[4], "DpxE", 4)) {
1350  avio_wb32(pb, track->par->extradata[11]);
1351  } else {
1352  avio_wb32(pb, 1);
1353  }
1354  return 0;
1355 }
1356 
1358 {
1359  int tag;
1360 
1361  if (track->par->width == 720) { /* SD */
1362  if (track->par->height == 480) { /* NTSC */
1363  if (track->par->format == AV_PIX_FMT_YUV422P) tag = MKTAG('d','v','5','n');
1364  else tag = MKTAG('d','v','c',' ');
1365  }else if (track->par->format == AV_PIX_FMT_YUV422P) tag = MKTAG('d','v','5','p');
1366  else if (track->par->format == AV_PIX_FMT_YUV420P) tag = MKTAG('d','v','c','p');
1367  else tag = MKTAG('d','v','p','p');
1368  } else if (track->par->height == 720) { /* HD 720 line */
1369  if (track->st->time_base.den == 50) tag = MKTAG('d','v','h','q');
1370  else tag = MKTAG('d','v','h','p');
1371  } else if (track->par->height == 1080) { /* HD 1080 line */
1372  if (track->st->time_base.den == 25) tag = MKTAG('d','v','h','5');
1373  else tag = MKTAG('d','v','h','6');
1374  } else {
1375  av_log(s, AV_LOG_ERROR, "unsupported height for dv codec\n");
1376  return 0;
1377  }
1378 
1379  return tag;
1380 }
1381 
1383 {
1384  AVRational rate = st->avg_frame_rate;
1385 
1386 #if FF_API_LAVF_AVCTX
1388  rate = av_inv_q(st->codec->time_base);
1389  if (av_timecode_check_frame_rate(rate) < 0) {
1390  av_log(s, AV_LOG_DEBUG, "timecode: tbc=%d/%d invalid, fallback on %d/%d\n",
1391  rate.num, rate.den, st->avg_frame_rate.num, st->avg_frame_rate.den);
1392  rate = st->avg_frame_rate;
1393  }
1395 #endif
1396 
1397  return rate;
1398 }
1399 
1401 {
1402  AVRational rational_framerate = find_fps(s, st);
1403  int rate = 0;
1404  if (rational_framerate.den != 0)
1405  rate = av_q2d(rational_framerate);
1406  return rate;
1407 }
1408 
1410 {
1411  int tag = track->par->codec_tag;
1413  AVStream *st = track->st;
1414  int rate = defined_frame_rate(s, st);
1415 
1416  if (!tag)
1417  tag = MKTAG('m', '2', 'v', '1'); //fallback tag
1418 
1419  if (track->par->format == AV_PIX_FMT_YUV420P) {
1420  if (track->par->width == 1280 && track->par->height == 720) {
1421  if (!interlaced) {
1422  if (rate == 24) tag = MKTAG('x','d','v','4');
1423  else if (rate == 25) tag = MKTAG('x','d','v','5');
1424  else if (rate == 30) tag = MKTAG('x','d','v','1');
1425  else if (rate == 50) tag = MKTAG('x','d','v','a');
1426  else if (rate == 60) tag = MKTAG('x','d','v','9');
1427  }
1428  } else if (track->par->width == 1440 && track->par->height == 1080) {
1429  if (!interlaced) {
1430  if (rate == 24) tag = MKTAG('x','d','v','6');
1431  else if (rate == 25) tag = MKTAG('x','d','v','7');
1432  else if (rate == 30) tag = MKTAG('x','d','v','8');
1433  } else {
1434  if (rate == 25) tag = MKTAG('x','d','v','3');
1435  else if (rate == 30) tag = MKTAG('x','d','v','2');
1436  }
1437  } else if (track->par->width == 1920 && track->par->height == 1080) {
1438  if (!interlaced) {
1439  if (rate == 24) tag = MKTAG('x','d','v','d');
1440  else if (rate == 25) tag = MKTAG('x','d','v','e');
1441  else if (rate == 30) tag = MKTAG('x','d','v','f');
1442  } else {
1443  if (rate == 25) tag = MKTAG('x','d','v','c');
1444  else if (rate == 30) tag = MKTAG('x','d','v','b');
1445  }
1446  }
1447  } else if (track->par->format == AV_PIX_FMT_YUV422P) {
1448  if (track->par->width == 1280 && track->par->height == 720) {
1449  if (!interlaced) {
1450  if (rate == 24) tag = MKTAG('x','d','5','4');
1451  else if (rate == 25) tag = MKTAG('x','d','5','5');
1452  else if (rate == 30) tag = MKTAG('x','d','5','1');
1453  else if (rate == 50) tag = MKTAG('x','d','5','a');
1454  else if (rate == 60) tag = MKTAG('x','d','5','9');
1455  }
1456  } else if (track->par->width == 1920 && track->par->height == 1080) {
1457  if (!interlaced) {
1458  if (rate == 24) tag = MKTAG('x','d','5','d');
1459  else if (rate == 25) tag = MKTAG('x','d','5','e');
1460  else if (rate == 30) tag = MKTAG('x','d','5','f');
1461  } else {
1462  if (rate == 25) tag = MKTAG('x','d','5','c');
1463  else if (rate == 30) tag = MKTAG('x','d','5','b');
1464  }
1465  }
1466  }
1467 
1468  return tag;
1469 }
1470 
1472 {
1473  int tag = track->par->codec_tag;
1475  AVStream *st = track->st;
1476  int rate = defined_frame_rate(s, st);
1477 
1478  if (!tag)
1479  tag = MKTAG('a', 'v', 'c', 'i'); //fallback tag
1480 
1481  if (track->par->format == AV_PIX_FMT_YUV420P10) {
1482  if (track->par->width == 960 && track->par->height == 720) {
1483  if (!interlaced) {
1484  if (rate == 24) tag = MKTAG('a','i','5','p');
1485  else if (rate == 25) tag = MKTAG('a','i','5','q');
1486  else if (rate == 30) tag = MKTAG('a','i','5','p');
1487  else if (rate == 50) tag = MKTAG('a','i','5','q');
1488  else if (rate == 60) tag = MKTAG('a','i','5','p');
1489  }
1490  } else if (track->par->width == 1440 && track->par->height == 1080) {
1491  if (!interlaced) {
1492  if (rate == 24) tag = MKTAG('a','i','5','3');
1493  else if (rate == 25) tag = MKTAG('a','i','5','2');
1494  else if (rate == 30) tag = MKTAG('a','i','5','3');
1495  } else {
1496  if (rate == 50) tag = MKTAG('a','i','5','5');
1497  else if (rate == 60) tag = MKTAG('a','i','5','6');
1498  }
1499  }
1500  } else if (track->par->format == AV_PIX_FMT_YUV422P10) {
1501  if (track->par->width == 1280 && track->par->height == 720) {
1502  if (!interlaced) {
1503  if (rate == 24) tag = MKTAG('a','i','1','p');
1504  else if (rate == 25) tag = MKTAG('a','i','1','q');
1505  else if (rate == 30) tag = MKTAG('a','i','1','p');
1506  else if (rate == 50) tag = MKTAG('a','i','1','q');
1507  else if (rate == 60) tag = MKTAG('a','i','1','p');
1508  }
1509  } else if (track->par->width == 1920 && track->par->height == 1080) {
1510  if (!interlaced) {
1511  if (rate == 24) tag = MKTAG('a','i','1','3');
1512  else if (rate == 25) tag = MKTAG('a','i','1','2');
1513  else if (rate == 30) tag = MKTAG('a','i','1','3');
1514  } else {
1515  if (rate == 25) tag = MKTAG('a','i','1','5');
1516  else if (rate == 50) tag = MKTAG('a','i','1','5');
1517  else if (rate == 60) tag = MKTAG('a','i','1','6');
1518  }
1519  } else if ( track->par->width == 4096 && track->par->height == 2160
1520  || track->par->width == 3840 && track->par->height == 2160
1521  || track->par->width == 2048 && track->par->height == 1080) {
1522  tag = MKTAG('a','i','v','x');
1523  }
1524  }
1525 
1526  return tag;
1527 }
1528 
1529 static const struct {
1531  uint32_t tag;
1532  unsigned bps;
1533 } mov_pix_fmt_tags[] = {
1534  { AV_PIX_FMT_YUYV422, MKTAG('y','u','v','2'), 0 },
1535  { AV_PIX_FMT_YUYV422, MKTAG('y','u','v','s'), 0 },
1536  { AV_PIX_FMT_UYVY422, MKTAG('2','v','u','y'), 0 },
1537  { AV_PIX_FMT_RGB555BE,MKTAG('r','a','w',' '), 16 },
1538  { AV_PIX_FMT_RGB555LE,MKTAG('L','5','5','5'), 16 },
1539  { AV_PIX_FMT_RGB565LE,MKTAG('L','5','6','5'), 16 },
1540  { AV_PIX_FMT_RGB565BE,MKTAG('B','5','6','5'), 16 },
1541  { AV_PIX_FMT_GRAY16BE,MKTAG('b','1','6','g'), 16 },
1542  { AV_PIX_FMT_RGB24, MKTAG('r','a','w',' '), 24 },
1543  { AV_PIX_FMT_BGR24, MKTAG('2','4','B','G'), 24 },
1544  { AV_PIX_FMT_ARGB, MKTAG('r','a','w',' '), 32 },
1545  { AV_PIX_FMT_BGRA, MKTAG('B','G','R','A'), 32 },
1546  { AV_PIX_FMT_RGBA, MKTAG('R','G','B','A'), 32 },
1547  { AV_PIX_FMT_ABGR, MKTAG('A','B','G','R'), 32 },
1548  { AV_PIX_FMT_RGB48BE, MKTAG('b','4','8','r'), 48 },
1549 };
1550 
1552 {
1553  int tag = MKTAG('A','V','d','n');
1554  if (track->par->profile != FF_PROFILE_UNKNOWN &&
1555  track->par->profile != FF_PROFILE_DNXHD)
1556  tag = MKTAG('A','V','d','h');
1557  return tag;
1558 }
1559 
1561 {
1562  int tag = track->par->codec_tag;
1563  int i;
1564  enum AVPixelFormat pix_fmt;
1565 
1566  for (i = 0; i < FF_ARRAY_ELEMS(mov_pix_fmt_tags); i++) {
1567  if (track->par->format == mov_pix_fmt_tags[i].pix_fmt) {
1568  tag = mov_pix_fmt_tags[i].tag;
1570  if (track->par->codec_tag == mov_pix_fmt_tags[i].tag)
1571  break;
1572  }
1573  }
1574 
1576  track->par->bits_per_coded_sample);
1577  if (tag == MKTAG('r','a','w',' ') &&
1578  track->par->format != pix_fmt &&
1579  track->par->format != AV_PIX_FMT_GRAY8 &&
1580  track->par->format != AV_PIX_FMT_NONE)
1581  av_log(s, AV_LOG_ERROR, "%s rawvideo cannot be written to mov, output file will be unreadable\n",
1582  av_get_pix_fmt_name(track->par->format));
1583  return tag;
1584 }
1585 
1586 static unsigned int mov_get_codec_tag(AVFormatContext *s, MOVTrack *track)
1587 {
1588  unsigned int tag = track->par->codec_tag;
1589 
1590  if (!tag || (s->strict_std_compliance >= FF_COMPLIANCE_NORMAL &&
1591  (track->par->codec_id == AV_CODEC_ID_DVVIDEO ||
1592  track->par->codec_id == AV_CODEC_ID_RAWVIDEO ||
1593  track->par->codec_id == AV_CODEC_ID_H263 ||
1594  track->par->codec_id == AV_CODEC_ID_H264 ||
1595  track->par->codec_id == AV_CODEC_ID_DNXHD ||
1596  track->par->codec_id == AV_CODEC_ID_MPEG2VIDEO ||
1597  av_get_bits_per_sample(track->par->codec_id)))) { // pcm audio
1598  if (track->par->codec_id == AV_CODEC_ID_DVVIDEO)
1599  tag = mov_get_dv_codec_tag(s, track);
1600  else if (track->par->codec_id == AV_CODEC_ID_RAWVIDEO)
1601  tag = mov_get_rawvideo_codec_tag(s, track);
1602  else if (track->par->codec_id == AV_CODEC_ID_MPEG2VIDEO)
1603  tag = mov_get_mpeg2_xdcam_codec_tag(s, track);
1604  else if (track->par->codec_id == AV_CODEC_ID_H264)
1605  tag = mov_get_h264_codec_tag(s, track);
1606  else if (track->par->codec_id == AV_CODEC_ID_DNXHD)
1607  tag = mov_get_dnxhd_codec_tag(s, track);
1608  else if (track->par->codec_type == AVMEDIA_TYPE_VIDEO) {
1610  if (!tag) { // if no mac fcc found, try with Microsoft tags
1612  if (tag)
1613  av_log(s, AV_LOG_WARNING, "Using MS style video codec tag, "
1614  "the file may be unplayable!\n");
1615  }
1616  } else if (track->par->codec_type == AVMEDIA_TYPE_AUDIO) {
1618  if (!tag) { // if no mac fcc found, try with Microsoft tags
1619  int ms_tag = ff_codec_get_tag(ff_codec_wav_tags, track->par->codec_id);
1620  if (ms_tag) {
1621  tag = MKTAG('m', 's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
1622  av_log(s, AV_LOG_WARNING, "Using MS style audio codec tag, "
1623  "the file may be unplayable!\n");
1624  }
1625  }
1626  } else if (track->par->codec_type == AVMEDIA_TYPE_SUBTITLE)
1628  }
1629 
1630  return tag;
1631 }
1632 
1634  { AV_CODEC_ID_MJPEG, 0xD },
1635  { AV_CODEC_ID_PNG, 0xE },
1636  { AV_CODEC_ID_BMP, 0x1B },
1637  { AV_CODEC_ID_NONE, 0 },
1638 };
1639 
1640 static unsigned int validate_codec_tag(const AVCodecTag *const *tags,
1641  unsigned int tag, int codec_id)
1642 {
1643  int i;
1644 
1645  /**
1646  * Check that tag + id is in the table
1647  */
1648  for (i = 0; tags && tags[i]; i++) {
1649  const AVCodecTag *codec_tags = tags[i];
1650  while (codec_tags->id != AV_CODEC_ID_NONE) {
1651  if (avpriv_toupper4(codec_tags->tag) == avpriv_toupper4(tag) &&
1652  codec_tags->id == codec_id)
1653  return codec_tags->tag;
1654  codec_tags++;
1655  }
1656  }
1657  return 0;
1658 }
1659 
1660 static unsigned int mov_find_codec_tag(AVFormatContext *s, MOVTrack *track)
1661 {
1662  if (is_cover_image(track->st))
1663  return ff_codec_get_tag(codec_cover_image_tags, track->par->codec_id);
1664 
1665  if (track->mode == MODE_IPOD)
1666  if (!av_match_ext(s->url, "m4a") &&
1667  !av_match_ext(s->url, "m4v") &&
1668  !av_match_ext(s->url, "m4b"))
1669  av_log(s, AV_LOG_WARNING, "Warning, extension is not .m4a nor .m4v "
1670  "Quicktime/Ipod might not play the file\n");
1671 
1672  if (track->mode == MODE_MOV) {
1673  return mov_get_codec_tag(s, track);
1674  } else
1675  return validate_codec_tag(s->oformat->codec_tag, track->par->codec_tag,
1676  track->par->codec_id);
1677 }
1678 
1679 /** Write uuid atom.
1680  * Needed to make file play in iPods running newest firmware
1681  * goes after avcC atom in moov.trak.mdia.minf.stbl.stsd.avc1
1682  */
1684 {
1685  avio_wb32(pb, 28);
1686  ffio_wfourcc(pb, "uuid");
1687  avio_wb32(pb, 0x6b6840f2);
1688  avio_wb32(pb, 0x5f244fc5);
1689  avio_wb32(pb, 0xba39a51b);
1690  avio_wb32(pb, 0xcf0323f3);
1691  avio_wb32(pb, 0x0);
1692  return 28;
1693 }
1694 
1695 static const uint16_t fiel_data[] = {
1696  0x0000, 0x0100, 0x0201, 0x0206, 0x0209, 0x020e
1697 };
1698 
1699 static int mov_write_fiel_tag(AVIOContext *pb, MOVTrack *track, int field_order)
1700 {
1701  unsigned mov_field_order = 0;
1702  if (field_order < FF_ARRAY_ELEMS(fiel_data))
1703  mov_field_order = fiel_data[field_order];
1704  else
1705  return 0;
1706  avio_wb32(pb, 10);
1707  ffio_wfourcc(pb, "fiel");
1708  avio_wb16(pb, mov_field_order);
1709  return 10;
1710 }
1711 
1713 {
1714  int64_t pos = avio_tell(pb);
1715  avio_wb32(pb, 0); /* size */
1716  avio_wl32(pb, track->tag); // store it byteswapped
1717  avio_wb32(pb, 0); /* Reserved */
1718  avio_wb16(pb, 0); /* Reserved */
1719  avio_wb16(pb, 1); /* Data-reference index */
1720 
1721  if (track->par->codec_id == AV_CODEC_ID_DVD_SUBTITLE)
1722  mov_write_esds_tag(pb, track);
1723  else if (track->par->extradata_size)
1724  avio_write(pb, track->par->extradata, track->par->extradata_size);
1725 
1726  return update_size(pb, pos);
1727 }
1728 
1730 {
1731  int8_t stereo_mode;
1732 
1733  if (stereo_3d->flags != 0) {
1734  av_log(s, AV_LOG_WARNING, "Unsupported stereo_3d flags %x. st3d not written.\n", stereo_3d->flags);
1735  return 0;
1736  }
1737 
1738  switch (stereo_3d->type) {
1739  case AV_STEREO3D_2D:
1740  stereo_mode = 0;
1741  break;
1742  case AV_STEREO3D_TOPBOTTOM:
1743  stereo_mode = 1;
1744  break;
1746  stereo_mode = 2;
1747  break;
1748  default:
1749  av_log(s, AV_LOG_WARNING, "Unsupported stereo_3d type %s. st3d not written.\n", av_stereo3d_type_name(stereo_3d->type));
1750  return 0;
1751  }
1752  avio_wb32(pb, 13); /* size */
1753  ffio_wfourcc(pb, "st3d");
1754  avio_wb32(pb, 0); /* version = 0 & flags = 0 */
1755  avio_w8(pb, stereo_mode);
1756  return 13;
1757 }
1758 
1760 {
1761  int64_t sv3d_pos, svhd_pos, proj_pos;
1762  const char* metadata_source = s->flags & AVFMT_FLAG_BITEXACT ? "Lavf" : LIBAVFORMAT_IDENT;
1763 
1764  if (spherical_mapping->projection != AV_SPHERICAL_EQUIRECTANGULAR &&
1765  spherical_mapping->projection != AV_SPHERICAL_EQUIRECTANGULAR_TILE &&
1766  spherical_mapping->projection != AV_SPHERICAL_CUBEMAP) {
1767  av_log(s, AV_LOG_WARNING, "Unsupported projection %d. sv3d not written.\n", spherical_mapping->projection);
1768  return 0;
1769  }
1770 
1771  sv3d_pos = avio_tell(pb);
1772  avio_wb32(pb, 0); /* size */
1773  ffio_wfourcc(pb, "sv3d");
1774 
1775  svhd_pos = avio_tell(pb);
1776  avio_wb32(pb, 0); /* size */
1777  ffio_wfourcc(pb, "svhd");
1778  avio_wb32(pb, 0); /* version = 0 & flags = 0 */
1779  avio_put_str(pb, metadata_source);
1780  update_size(pb, svhd_pos);
1781 
1782  proj_pos = avio_tell(pb);
1783  avio_wb32(pb, 0); /* size */
1784  ffio_wfourcc(pb, "proj");
1785 
1786  avio_wb32(pb, 24); /* size */
1787  ffio_wfourcc(pb, "prhd");
1788  avio_wb32(pb, 0); /* version = 0 & flags = 0 */
1789  avio_wb32(pb, spherical_mapping->yaw);
1790  avio_wb32(pb, spherical_mapping->pitch);
1791  avio_wb32(pb, spherical_mapping->roll);
1792 
1793  switch (spherical_mapping->projection) {
1796  avio_wb32(pb, 28); /* size */
1797  ffio_wfourcc(pb, "equi");
1798  avio_wb32(pb, 0); /* version = 0 & flags = 0 */
1799  avio_wb32(pb, spherical_mapping->bound_top);
1800  avio_wb32(pb, spherical_mapping->bound_bottom);
1801  avio_wb32(pb, spherical_mapping->bound_left);
1802  avio_wb32(pb, spherical_mapping->bound_right);
1803  break;
1804  case AV_SPHERICAL_CUBEMAP:
1805  avio_wb32(pb, 20); /* size */
1806  ffio_wfourcc(pb, "cbmp");
1807  avio_wb32(pb, 0); /* version = 0 & flags = 0 */
1808  avio_wb32(pb, 0); /* layout */
1809  avio_wb32(pb, spherical_mapping->padding); /* padding */
1810  break;
1811  }
1812  update_size(pb, proj_pos);
1813 
1814  return update_size(pb, sv3d_pos);
1815 }
1816 
1817 static int mov_write_clap_tag(AVIOContext *pb, MOVTrack *track)
1818 {
1819  avio_wb32(pb, 40);
1820  ffio_wfourcc(pb, "clap");
1821  avio_wb32(pb, track->par->width); /* apertureWidth_N */
1822  avio_wb32(pb, 1); /* apertureWidth_D (= 1) */
1823  avio_wb32(pb, track->height); /* apertureHeight_N */
1824  avio_wb32(pb, 1); /* apertureHeight_D (= 1) */
1825  avio_wb32(pb, 0); /* horizOff_N (= 0) */
1826  avio_wb32(pb, 1); /* horizOff_D (= 1) */
1827  avio_wb32(pb, 0); /* vertOff_N (= 0) */
1828  avio_wb32(pb, 1); /* vertOff_D (= 1) */
1829  return 40;
1830 }
1831 
1832 static int mov_write_pasp_tag(AVIOContext *pb, MOVTrack *track)
1833 {
1834  AVRational sar;
1835  av_reduce(&sar.num, &sar.den, track->par->sample_aspect_ratio.num,
1836  track->par->sample_aspect_ratio.den, INT_MAX);
1837 
1838  avio_wb32(pb, 16);
1839  ffio_wfourcc(pb, "pasp");
1840  avio_wb32(pb, sar.num);
1841  avio_wb32(pb, sar.den);
1842  return 16;
1843 }
1844 
1845 static int mov_write_gama_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track, double gamma)
1846 {
1847  uint32_t gama = 0;
1848  if (gamma <= 0.0) {
1849  gamma = avpriv_get_gamma_from_trc(track->par->color_trc);
1850  }
1851  av_log(s, AV_LOG_DEBUG, "gamma value %g\n", gamma);
1852 
1853  if (gamma > 1e-6) {
1854  gama = (uint32_t)lrint((double)(1<<16) * gamma);
1855  av_log(s, AV_LOG_DEBUG, "writing gama value %"PRId32"\n", gama);
1856 
1857  av_assert0(track->mode == MODE_MOV);
1858  avio_wb32(pb, 12);
1859  ffio_wfourcc(pb, "gama");
1860  avio_wb32(pb, gama);
1861  return 12;
1862  } else {
1863  av_log(s, AV_LOG_WARNING, "gamma value unknown, unable to write gama atom\n");
1864  }
1865  return 0;
1866 }
1867 
1868 static int mov_write_colr_tag(AVIOContext *pb, MOVTrack *track)
1869 {
1870  int64_t pos = avio_tell(pb);
1871 
1872  // Ref (MOV): https://developer.apple.com/library/mac/technotes/tn2162/_index.html#//apple_ref/doc/uid/DTS40013070-CH1-TNTAG9
1873  // Ref (MP4): ISO/IEC 14496-12:2012
1874 
1875  if (track->par->color_primaries == AVCOL_PRI_UNSPECIFIED &&
1876  track->par->color_trc == AVCOL_TRC_UNSPECIFIED &&
1877  track->par->color_space == AVCOL_SPC_UNSPECIFIED) {
1878  if ((track->par->width >= 1920 && track->par->height >= 1080)
1879  || (track->par->width == 1280 && track->par->height == 720)) {
1880  av_log(NULL, AV_LOG_WARNING, "color primaries unspecified, assuming bt709\n");
1882  } else if (track->par->width == 720 && track->height == 576) {
1883  av_log(NULL, AV_LOG_WARNING, "color primaries unspecified, assuming bt470bg\n");
1885  } else if (track->par->width == 720 &&
1886  (track->height == 486 || track->height == 480)) {
1887  av_log(NULL, AV_LOG_WARNING, "color primaries unspecified, assuming smpte170\n");
1889  } else {
1890  av_log(NULL, AV_LOG_WARNING, "color primaries unspecified, unable to assume anything\n");
1891  }
1892  switch (track->par->color_primaries) {
1893  case AVCOL_PRI_BT709:
1894  track->par->color_trc = AVCOL_TRC_BT709;
1895  track->par->color_space = AVCOL_SPC_BT709;
1896  break;
1897  case AVCOL_PRI_SMPTE170M:
1898  case AVCOL_PRI_BT470BG:
1899  track->par->color_trc = AVCOL_TRC_BT709;
1901  break;
1902  }
1903  }
1904 
1905  /* We should only ever be called by MOV or MP4. */
1906  av_assert0(track->mode == MODE_MOV || track->mode == MODE_MP4);
1907 
1908  avio_wb32(pb, 0); /* size */
1909  ffio_wfourcc(pb, "colr");
1910  if (track->mode == MODE_MP4)
1911  ffio_wfourcc(pb, "nclx");
1912  else
1913  ffio_wfourcc(pb, "nclc");
1914  switch (track->par->color_primaries) {
1915  case AVCOL_PRI_BT709: avio_wb16(pb, 1); break;
1916  case AVCOL_PRI_BT470BG: avio_wb16(pb, 5); break;
1917  case AVCOL_PRI_SMPTE170M:
1918  case AVCOL_PRI_SMPTE240M: avio_wb16(pb, 6); break;
1919  case AVCOL_PRI_BT2020: avio_wb16(pb, 9); break;
1920  case AVCOL_PRI_SMPTE431: avio_wb16(pb, 11); break;
1921  case AVCOL_PRI_SMPTE432: avio_wb16(pb, 12); break;
1922  default: avio_wb16(pb, 2);
1923  }
1924  switch (track->par->color_trc) {
1925  case AVCOL_TRC_BT709: avio_wb16(pb, 1); break;
1926  case AVCOL_TRC_SMPTE170M: avio_wb16(pb, 1); break; // remapped
1927  case AVCOL_TRC_SMPTE240M: avio_wb16(pb, 7); break;
1928  case AVCOL_TRC_SMPTEST2084: avio_wb16(pb, 16); break;
1929  case AVCOL_TRC_SMPTE428: avio_wb16(pb, 17); break;
1930  case AVCOL_TRC_ARIB_STD_B67: avio_wb16(pb, 18); break;
1931  default: avio_wb16(pb, 2);
1932  }
1933  switch (track->par->color_space) {
1934  case AVCOL_SPC_BT709: avio_wb16(pb, 1); break;
1935  case AVCOL_SPC_BT470BG:
1936  case AVCOL_SPC_SMPTE170M: avio_wb16(pb, 6); break;
1937  case AVCOL_SPC_SMPTE240M: avio_wb16(pb, 7); break;
1938  case AVCOL_SPC_BT2020_NCL: avio_wb16(pb, 9); break;
1939  default: avio_wb16(pb, 2);
1940  }
1941 
1942  if (track->mode == MODE_MP4) {
1943  int full_range = track->par->color_range == AVCOL_RANGE_JPEG;
1944  avio_w8(pb, full_range << 7);
1945  }
1946 
1947  return update_size(pb, pos);
1948 }
1949 
1950 static void find_compressor(char * compressor_name, int len, MOVTrack *track)
1951 {
1952  AVDictionaryEntry *encoder;
1953  int xdcam_res = (track->par->width == 1280 && track->par->height == 720)
1954  || (track->par->width == 1440 && track->par->height == 1080)
1955  || (track->par->width == 1920 && track->par->height == 1080);
1956 
1957  if (track->mode == MODE_MOV &&
1958  (encoder = av_dict_get(track->st->metadata, "encoder", NULL, 0))) {
1959  av_strlcpy(compressor_name, encoder->value, 32);
1960  } else if (track->par->codec_id == AV_CODEC_ID_MPEG2VIDEO && xdcam_res) {
1962  AVStream *st = track->st;
1963  int rate = defined_frame_rate(NULL, st);
1964  av_strlcatf(compressor_name, len, "XDCAM");
1965  if (track->par->format == AV_PIX_FMT_YUV422P) {
1966  av_strlcatf(compressor_name, len, " HD422");
1967  } else if(track->par->width == 1440) {
1968  av_strlcatf(compressor_name, len, " HD");
1969  } else
1970  av_strlcatf(compressor_name, len, " EX");
1971 
1972  av_strlcatf(compressor_name, len, " %d%c", track->par->height, interlaced ? 'i' : 'p');
1973 
1974  av_strlcatf(compressor_name, len, "%d", rate * (interlaced + 1));
1975  }
1976 }
1977 
1979 {
1980  int64_t pos = avio_tell(pb);
1981  char compressor_name[32] = { 0 };
1982  int avid = 0;
1983 
1984  int uncompressed_ycbcr = ((track->par->codec_id == AV_CODEC_ID_RAWVIDEO && track->par->format == AV_PIX_FMT_UYVY422)
1985  || (track->par->codec_id == AV_CODEC_ID_RAWVIDEO && track->par->format == AV_PIX_FMT_YUYV422)
1986  || track->par->codec_id == AV_CODEC_ID_V308
1987  || track->par->codec_id == AV_CODEC_ID_V408
1988  || track->par->codec_id == AV_CODEC_ID_V410
1989  || track->par->codec_id == AV_CODEC_ID_V210);
1990 
1991  avio_wb32(pb, 0); /* size */
1992  if (mov->encryption_scheme != MOV_ENC_NONE) {
1993  ffio_wfourcc(pb, "encv");
1994  } else {
1995  avio_wl32(pb, track->tag); // store it byteswapped
1996  }
1997  avio_wb32(pb, 0); /* Reserved */
1998  avio_wb16(pb, 0); /* Reserved */
1999  avio_wb16(pb, 1); /* Data-reference index */
2000 
2001  if (uncompressed_ycbcr) {
2002  avio_wb16(pb, 2); /* Codec stream version */
2003  } else {
2004  avio_wb16(pb, 0); /* Codec stream version */
2005  }
2006  avio_wb16(pb, 0); /* Codec stream revision (=0) */
2007  if (track->mode == MODE_MOV) {
2008  ffio_wfourcc(pb, "FFMP"); /* Vendor */
2009  if (track->par->codec_id == AV_CODEC_ID_RAWVIDEO || uncompressed_ycbcr) {
2010  avio_wb32(pb, 0); /* Temporal Quality */
2011  avio_wb32(pb, 0x400); /* Spatial Quality = lossless*/
2012  } else {
2013  avio_wb32(pb, 0x200); /* Temporal Quality = normal */
2014  avio_wb32(pb, 0x200); /* Spatial Quality = normal */
2015  }
2016  } else {
2017  avio_wb32(pb, 0); /* Reserved */
2018  avio_wb32(pb, 0); /* Reserved */
2019  avio_wb32(pb, 0); /* Reserved */
2020  }
2021  avio_wb16(pb, track->par->width); /* Video width */
2022  avio_wb16(pb, track->height); /* Video height */
2023  avio_wb32(pb, 0x00480000); /* Horizontal resolution 72dpi */
2024  avio_wb32(pb, 0x00480000); /* Vertical resolution 72dpi */
2025  avio_wb32(pb, 0); /* Data size (= 0) */
2026  avio_wb16(pb, 1); /* Frame count (= 1) */
2027 
2028  /* FIXME not sure, ISO 14496-1 draft where it shall be set to 0 */
2029  find_compressor(compressor_name, 32, track);
2030  avio_w8(pb, strlen(compressor_name));
2031  avio_write(pb, compressor_name, 31);
2032 
2033  if (track->mode == MODE_MOV &&
2034  (track->par->codec_id == AV_CODEC_ID_V410 || track->par->codec_id == AV_CODEC_ID_V210))
2035  avio_wb16(pb, 0x18);
2036  else if (track->mode == MODE_MOV && track->par->bits_per_coded_sample)
2037  avio_wb16(pb, track->par->bits_per_coded_sample |
2038  (track->par->format == AV_PIX_FMT_GRAY8 ? 0x20 : 0));
2039  else
2040  avio_wb16(pb, 0x18); /* Reserved */
2041 
2042  if (track->mode == MODE_MOV && track->par->format == AV_PIX_FMT_PAL8) {
2043  int pal_size = 1 << track->par->bits_per_coded_sample;
2044  int i;
2045  avio_wb16(pb, 0); /* Color table ID */
2046  avio_wb32(pb, 0); /* Color table seed */
2047  avio_wb16(pb, 0x8000); /* Color table flags */
2048  avio_wb16(pb, pal_size - 1); /* Color table size (zero-relative) */
2049  for (i = 0; i < pal_size; i++) {
2050  uint32_t rgb = track->palette[i];
2051  uint16_t r = (rgb >> 16) & 0xff;
2052  uint16_t g = (rgb >> 8) & 0xff;
2053  uint16_t b = rgb & 0xff;
2054  avio_wb16(pb, 0);
2055  avio_wb16(pb, (r << 8) | r);
2056  avio_wb16(pb, (g << 8) | g);
2057  avio_wb16(pb, (b << 8) | b);
2058  }
2059  } else
2060  avio_wb16(pb, 0xffff); /* Reserved */
2061 
2062  if (track->tag == MKTAG('m','p','4','v'))
2063  mov_write_esds_tag(pb, track);
2064  else if (track->par->codec_id == AV_CODEC_ID_H263)
2065  mov_write_d263_tag(pb);
2066  else if (track->par->codec_id == AV_CODEC_ID_AVUI ||
2067  track->par->codec_id == AV_CODEC_ID_SVQ3) {
2068  mov_write_extradata_tag(pb, track);
2069  avio_wb32(pb, 0);
2070  } else if (track->par->codec_id == AV_CODEC_ID_DNXHD) {
2071  mov_write_avid_tag(pb, track);
2072  avid = 1;
2073  } else if (track->par->codec_id == AV_CODEC_ID_HEVC)
2074  mov_write_hvcc_tag(pb, track);
2075  else if (track->par->codec_id == AV_CODEC_ID_H264 && !TAG_IS_AVCI(track->tag)) {
2076  mov_write_avcc_tag(pb, track);
2077  if (track->mode == MODE_IPOD)
2079  } else if (track->par->codec_id == AV_CODEC_ID_VP9) {
2080  mov_write_vpcc_tag(mov->fc, pb, track);
2081  } else if (track->par->codec_id == AV_CODEC_ID_AV1) {
2082  mov_write_av1c_tag(pb, track);
2083  } else if (track->par->codec_id == AV_CODEC_ID_VC1 && track->vos_len > 0)
2084  mov_write_dvc1_tag(pb, track);
2085  else if (track->par->codec_id == AV_CODEC_ID_VP6F ||
2086  track->par->codec_id == AV_CODEC_ID_VP6A) {
2087  /* Don't write any potential extradata here - the cropping
2088  * is signalled via the normal width/height fields. */
2089  } else if (track->par->codec_id == AV_CODEC_ID_R10K) {
2090  if (track->par->codec_tag == MKTAG('R','1','0','k'))
2091  mov_write_dpxe_tag(pb, track);
2092  } else if (track->vos_len > 0)
2093  mov_write_glbl_tag(pb, track);
2094 
2095  if (track->par->codec_id != AV_CODEC_ID_H264 &&
2096  track->par->codec_id != AV_CODEC_ID_MPEG4 &&
2097  track->par->codec_id != AV_CODEC_ID_DNXHD) {
2098  int field_order = track->par->field_order;
2099 
2100 #if FF_API_LAVF_AVCTX
2102  if (field_order != track->st->codec->field_order && track->st->codec->field_order != AV_FIELD_UNKNOWN)
2103  field_order = track->st->codec->field_order;
2105 #endif
2106 
2107  if (field_order != AV_FIELD_UNKNOWN)
2108  mov_write_fiel_tag(pb, track, field_order);
2109  }
2110 
2111  if (mov->flags & FF_MOV_FLAG_WRITE_GAMA) {
2112  if (track->mode == MODE_MOV)
2113  mov_write_gama_tag(s, pb, track, mov->gamma);
2114  else
2115  av_log(mov->fc, AV_LOG_WARNING, "Not writing 'gama' atom. Format is not MOV.\n");
2116  }
2117  if (mov->flags & FF_MOV_FLAG_WRITE_COLR) {
2118  if (track->mode == MODE_MOV || track->mode == MODE_MP4)
2119  mov_write_colr_tag(pb, track);
2120  else
2121  av_log(mov->fc, AV_LOG_WARNING, "Not writing 'colr' atom. Format is not MOV or MP4.\n");
2122  }
2123 
2124  if (track->mode == MODE_MP4 && mov->fc->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL) {
2127 
2128  if (stereo_3d)
2129  mov_write_st3d_tag(s, pb, stereo_3d);
2130  if (spherical_mapping)
2131  mov_write_sv3d_tag(mov->fc, pb, spherical_mapping);
2132  }
2133 
2134  if (track->par->sample_aspect_ratio.den && track->par->sample_aspect_ratio.num) {
2135  mov_write_pasp_tag(pb, track);
2136  }
2137 
2138  if (uncompressed_ycbcr){
2139  mov_write_clap_tag(pb, track);
2140  }
2141 
2142  if (mov->encryption_scheme != MOV_ENC_NONE) {
2143  ff_mov_cenc_write_sinf_tag(track, pb, mov->encryption_kid);
2144  }
2145 
2146  /* extra padding for avid stsd */
2147  /* https://developer.apple.com/library/mac/documentation/QuickTime/QTFF/QTFFChap2/qtff2.html#//apple_ref/doc/uid/TP40000939-CH204-61112 */
2148  if (avid)
2149  avio_wb32(pb, 0);
2150 
2151  return update_size(pb, pos);
2152 }
2153 
2154 static int mov_write_rtp_tag(AVIOContext *pb, MOVTrack *track)
2155 {
2156  int64_t pos = avio_tell(pb);
2157  avio_wb32(pb, 0); /* size */
2158  ffio_wfourcc(pb, "rtp ");
2159  avio_wb32(pb, 0); /* Reserved */
2160  avio_wb16(pb, 0); /* Reserved */
2161  avio_wb16(pb, 1); /* Data-reference index */
2162 
2163  avio_wb16(pb, 1); /* Hint track version */
2164  avio_wb16(pb, 1); /* Highest compatible version */
2165  avio_wb32(pb, track->max_packet_size); /* Max packet size */
2166 
2167  avio_wb32(pb, 12); /* size */
2168  ffio_wfourcc(pb, "tims");
2169  avio_wb32(pb, track->timescale);
2170 
2171  return update_size(pb, pos);
2172 }
2173 
2174 static int mov_write_source_reference_tag(AVIOContext *pb, MOVTrack *track, const char *reel_name)
2175 {
2176  uint64_t str_size =strlen(reel_name);
2177  int64_t pos = avio_tell(pb);
2178 
2179  if (str_size >= UINT16_MAX){
2180  av_log(NULL, AV_LOG_ERROR, "reel_name length %"PRIu64" is too large\n", str_size);
2181  avio_wb16(pb, 0);
2182  return AVERROR(EINVAL);
2183  }
2184 
2185  avio_wb32(pb, 0); /* size */
2186  ffio_wfourcc(pb, "name"); /* Data format */
2187  avio_wb16(pb, str_size); /* string size */
2188  avio_wb16(pb, track->language); /* langcode */
2189  avio_write(pb, reel_name, str_size); /* reel name */
2190  return update_size(pb,pos);
2191 }
2192 
2193 static int mov_write_tmcd_tag(AVIOContext *pb, MOVTrack *track)
2194 {
2195  int64_t pos = avio_tell(pb);
2196 #if 1
2197  int frame_duration;
2198  int nb_frames;
2199  AVDictionaryEntry *t = NULL;
2200 
2201  if (!track->st->avg_frame_rate.num || !track->st->avg_frame_rate.den) {
2202 #if FF_API_LAVF_AVCTX
2204  frame_duration = av_rescale(track->timescale, track->st->codec->time_base.num, track->st->codec->time_base.den);
2205  nb_frames = ROUNDED_DIV(track->st->codec->time_base.den, track->st->codec->time_base.num);
2207 #else
2208  av_log(NULL, AV_LOG_ERROR, "avg_frame_rate not set for tmcd track.\n");
2209  return AVERROR(EINVAL);
2210 #endif
2211  } else {
2212  frame_duration = av_rescale(track->timescale, track->st->avg_frame_rate.num, track->st->avg_frame_rate.den);
2213  nb_frames = ROUNDED_DIV(track->st->avg_frame_rate.den, track->st->avg_frame_rate.num);
2214  }
2215 
2216  if (nb_frames > 255) {
2217  av_log(NULL, AV_LOG_ERROR, "fps %d is too large\n", nb_frames);
2218  return AVERROR(EINVAL);
2219  }
2220 
2221  avio_wb32(pb, 0); /* size */
2222  ffio_wfourcc(pb, "tmcd"); /* Data format */
2223  avio_wb32(pb, 0); /* Reserved */
2224  avio_wb32(pb, 1); /* Data reference index */
2225  avio_wb32(pb, 0); /* Flags */
2226  avio_wb32(pb, track->timecode_flags); /* Flags (timecode) */
2227  avio_wb32(pb, track->timescale); /* Timescale */
2228  avio_wb32(pb, frame_duration); /* Frame duration */
2229  avio_w8(pb, nb_frames); /* Number of frames */
2230  avio_w8(pb, 0); /* Reserved */
2231 
2232  t = av_dict_get(track->st->metadata, "reel_name", NULL, 0);
2233  if (t && utf8len(t->value) && track->mode != MODE_MP4)
2234  mov_write_source_reference_tag(pb, track, t->value);
2235  else
2236  avio_wb16(pb, 0); /* zero size */
2237 #else
2238 
2239  avio_wb32(pb, 0); /* size */
2240  ffio_wfourcc(pb, "tmcd"); /* Data format */
2241  avio_wb32(pb, 0); /* Reserved */
2242  avio_wb32(pb, 1); /* Data reference index */
2243  if (track->par->extradata_size)
2244  avio_write(pb, track->par->extradata, track->par->extradata_size);
2245 #endif
2246  return update_size(pb, pos);
2247 }
2248 
2249 static int mov_write_gpmd_tag(AVIOContext *pb, const MOVTrack *track)
2250 {
2251  int64_t pos = avio_tell(pb);
2252  avio_wb32(pb, 0); /* size */
2253  ffio_wfourcc(pb, "gpmd");
2254  avio_wb32(pb, 0); /* Reserved */
2255  avio_wb16(pb, 0); /* Reserved */
2256  avio_wb16(pb, 1); /* Data-reference index */
2257  avio_wb32(pb, 0); /* Reserved */
2258  return update_size(pb, pos);
2259 }
2260 
2262 {
2263  int64_t pos = avio_tell(pb);
2264  int ret = 0;
2265  avio_wb32(pb, 0); /* size */
2266  ffio_wfourcc(pb, "stsd");
2267  avio_wb32(pb, 0); /* version & flags */
2268  avio_wb32(pb, 1); /* entry count */
2269  if (track->par->codec_type == AVMEDIA_TYPE_VIDEO)
2270  ret = mov_write_video_tag(s, pb, mov, track);
2271  else if (track->par->codec_type == AVMEDIA_TYPE_AUDIO)
2272  ret = mov_write_audio_tag(s, pb, mov, track);
2273  else if (track->par->codec_type == AVMEDIA_TYPE_SUBTITLE)
2274  ret = mov_write_subtitle_tag(pb, track);
2275  else if (track->par->codec_tag == MKTAG('r','t','p',' '))
2276  ret = mov_write_rtp_tag(pb, track);
2277  else if (track->par->codec_tag == MKTAG('t','m','c','d'))
2278  ret = mov_write_tmcd_tag(pb, track);
2279  else if (track->par->codec_tag == MKTAG('g','p','m','d'))
2280  ret = mov_write_gpmd_tag(pb, track);
2281 
2282  if (ret < 0)
2283  return ret;
2284 
2285  return update_size(pb, pos);
2286 }
2287 
2289 {
2290  MOVMuxContext *mov = s->priv_data;
2291  MOVStts *ctts_entries;
2292  uint32_t entries = 0;
2293  uint32_t atom_size;
2294  int i;
2295 
2296  ctts_entries = av_malloc_array((track->entry + 1), sizeof(*ctts_entries)); /* worst case */
2297  if (!ctts_entries)
2298  return AVERROR(ENOMEM);
2299  ctts_entries[0].count = 1;
2300  ctts_entries[0].duration = track->cluster[0].cts;
2301  for (i = 1; i < track->entry; i++) {
2302  if (track->cluster[i].cts == ctts_entries[entries].duration) {
2303  ctts_entries[entries].count++; /* compress */
2304  } else {
2305  entries++;
2306  ctts_entries[entries].duration = track->cluster[i].cts;
2307  ctts_entries[entries].count = 1;
2308  }
2309  }
2310  entries++; /* last one */
2311  atom_size = 16 + (entries * 8);
2312  avio_wb32(pb, atom_size); /* size */
2313  ffio_wfourcc(pb, "ctts");
2315  avio_w8(pb, 1); /* version */
2316  else
2317  avio_w8(pb, 0); /* version */
2318  avio_wb24(pb, 0); /* flags */
2319  avio_wb32(pb, entries); /* entry count */
2320  for (i = 0; i < entries; i++) {
2321  avio_wb32(pb, ctts_entries[i].count);
2322  avio_wb32(pb, ctts_entries[i].duration);
2323  }
2324  av_free(ctts_entries);
2325  return atom_size;
2326 }
2327 
2328 /* Time to sample atom */
2329 static int mov_write_stts_tag(AVIOContext *pb, MOVTrack *track)
2330 {
2331  MOVStts *stts_entries = NULL;
2332  uint32_t entries = -1;
2333  uint32_t atom_size;
2334  int i;
2335 
2336  if (track->par->codec_type == AVMEDIA_TYPE_AUDIO && !track->audio_vbr) {
2337  stts_entries = av_malloc(sizeof(*stts_entries)); /* one entry */
2338  if (!stts_entries)
2339  return AVERROR(ENOMEM);
2340  stts_entries[0].count = track->sample_count;
2341  stts_entries[0].duration = 1;
2342  entries = 1;
2343  } else {
2344  if (track->entry) {
2345  stts_entries = av_malloc_array(track->entry, sizeof(*stts_entries)); /* worst case */
2346  if (!stts_entries)
2347  return AVERROR(ENOMEM);
2348  }
2349  for (i = 0; i < track->entry; i++) {
2350  int duration = get_cluster_duration(track, i);
2351  if (i && duration == stts_entries[entries].duration) {
2352  stts_entries[entries].count++; /* compress */
2353  } else {
2354  entries++;
2355  stts_entries[entries].duration = duration;
2356  stts_entries[entries].count = 1;
2357  }
2358  }
2359  entries++; /* last one */
2360  }
2361  atom_size = 16 + (entries * 8);
2362  avio_wb32(pb, atom_size); /* size */
2363  ffio_wfourcc(pb, "stts");
2364  avio_wb32(pb, 0); /* version & flags */
2365  avio_wb32(pb, entries); /* entry count */
2366  for (i = 0; i < entries; i++) {
2367  avio_wb32(pb, stts_entries[i].count);
2368  avio_wb32(pb, stts_entries[i].duration);
2369  }
2370  av_free(stts_entries);
2371  return atom_size;
2372 }
2373 
2375 {
2376  avio_wb32(pb, 28); /* size */
2377  ffio_wfourcc(pb, "dref");
2378  avio_wb32(pb, 0); /* version & flags */
2379  avio_wb32(pb, 1); /* entry count */
2380 
2381  avio_wb32(pb, 0xc); /* size */
2382  //FIXME add the alis and rsrc atom
2383  ffio_wfourcc(pb, "url ");
2384  avio_wb32(pb, 1); /* version & flags */
2385 
2386  return 28;
2387 }
2388 
2390 {
2391  struct sgpd_entry {
2392  int count;
2393  int16_t roll_distance;
2394  int group_description_index;
2395  };
2396 
2397  struct sgpd_entry *sgpd_entries = NULL;
2398  int entries = -1;
2399  int group = 0;
2400  int i, j;
2401 
2402  const int OPUS_SEEK_PREROLL_MS = 80;
2403  int roll_samples = av_rescale_q(OPUS_SEEK_PREROLL_MS,
2404  (AVRational){1, 1000},
2405  (AVRational){1, 48000});
2406 
2407  if (!track->entry)
2408  return 0;
2409 
2410  sgpd_entries = av_malloc_array(track->entry, sizeof(*sgpd_entries));
2411  if (!sgpd_entries)
2412  return AVERROR(ENOMEM);
2413 
2415 
2416  if (track->par->codec_id == AV_CODEC_ID_OPUS) {
2417  for (i = 0; i < track->entry; i++) {
2418  int roll_samples_remaining = roll_samples;
2419  int distance = 0;
2420  for (j = i - 1; j >= 0; j--) {
2421  roll_samples_remaining -= get_cluster_duration(track, j);
2422  distance++;
2423  if (roll_samples_remaining <= 0)
2424  break;
2425  }
2426  /* We don't have enough preceeding samples to compute a valid
2427  roll_distance here, so this sample can't be independently
2428  decoded. */
2429  if (roll_samples_remaining > 0)
2430  distance = 0;
2431  /* Verify distance is a maximum of 32 (2.5ms) packets. */
2432  if (distance > 32)
2433  return AVERROR_INVALIDDATA;
2434  if (i && distance == sgpd_entries[entries].roll_distance) {
2435  sgpd_entries[entries].count++;
2436  } else {
2437  entries++;
2438  sgpd_entries[entries].count = 1;
2439  sgpd_entries[entries].roll_distance = distance;
2440  sgpd_entries[entries].group_description_index = distance ? ++group : 0;
2441  }
2442  }
2443  } else {
2444  entries++;
2445  sgpd_entries[entries].count = track->sample_count;
2446  sgpd_entries[entries].roll_distance = 1;
2447  sgpd_entries[entries].group_description_index = ++group;
2448  }
2449  entries++;
2450 
2451  if (!group) {
2452  av_free(sgpd_entries);
2453  return 0;
2454  }
2455 
2456  /* Write sgpd tag */
2457  avio_wb32(pb, 24 + (group * 2)); /* size */
2458  ffio_wfourcc(pb, "sgpd");
2459  avio_wb32(pb, 1 << 24); /* fullbox */
2460  ffio_wfourcc(pb, "roll");
2461  avio_wb32(pb, 2); /* default_length */
2462  avio_wb32(pb, group); /* entry_count */
2463  for (i = 0; i < entries; i++) {
2464  if (sgpd_entries[i].group_description_index) {
2465  avio_wb16(pb, -sgpd_entries[i].roll_distance); /* roll_distance */
2466  }
2467  }
2468 
2469  /* Write sbgp tag */
2470  avio_wb32(pb, 20 + (entries * 8)); /* size */
2471  ffio_wfourcc(pb, "sbgp");
2472  avio_wb32(pb, 0); /* fullbox */
2473  ffio_wfourcc(pb, "roll");
2474  avio_wb32(pb, entries); /* entry_count */
2475  for (i = 0; i < entries; i++) {
2476  avio_wb32(pb, sgpd_entries[i].count); /* sample_count */
2477  avio_wb32(pb, sgpd_entries[i].group_description_index); /* group_description_index */
2478  }
2479 
2480  av_free(sgpd_entries);
2481  return 0;
2482 }
2483 
2485 {
2486  int64_t pos = avio_tell(pb);
2487  int ret = 0;
2488 
2489  avio_wb32(pb, 0); /* size */
2490  ffio_wfourcc(pb, "stbl");
2491  if ((ret = mov_write_stsd_tag(s, pb, mov, track)) < 0)
2492  return ret;
2493  mov_write_stts_tag(pb, track);
2494  if ((track->par->codec_type == AVMEDIA_TYPE_VIDEO ||
2495  track->par->codec_id == AV_CODEC_ID_TRUEHD ||
2496  track->par->codec_tag == MKTAG('r','t','p',' ')) &&
2497  track->has_keyframes && track->has_keyframes < track->entry)
2498  mov_write_stss_tag(pb, track, MOV_SYNC_SAMPLE);
2499  if (track->par->codec_type == AVMEDIA_TYPE_VIDEO && track->has_disposable)
2500  mov_write_sdtp_tag(pb, track);
2501  if (track->mode == MODE_MOV && track->flags & MOV_TRACK_STPS)
2503  if (track->par->codec_type == AVMEDIA_TYPE_VIDEO &&
2504  track->flags & MOV_TRACK_CTTS && track->entry) {
2505 
2506  if ((ret = mov_write_ctts_tag(s, pb, track)) < 0)
2507  return ret;
2508  }
2509  mov_write_stsc_tag(pb, track);
2510  mov_write_stsz_tag(pb, track);
2511  mov_write_stco_tag(pb, track);
2512  if (track->cenc.aes_ctr) {
2513  ff_mov_cenc_write_stbl_atoms(&track->cenc, pb);
2514  }
2515  if (track->par->codec_id == AV_CODEC_ID_OPUS || track->par->codec_id == AV_CODEC_ID_AAC) {
2516  mov_preroll_write_stbl_atoms(pb, track);
2517  }
2518  return update_size(pb, pos);
2519 }
2520 
2522 {
2523  int64_t pos = avio_tell(pb);
2524  avio_wb32(pb, 0); /* size */
2525  ffio_wfourcc(pb, "dinf");
2526  mov_write_dref_tag(pb);
2527  return update_size(pb, pos);
2528 }
2529 
2531 {
2532  avio_wb32(pb, 12);
2533  ffio_wfourcc(pb, "nmhd");
2534  avio_wb32(pb, 0);
2535  return 12;
2536 }
2537 
2538 static int mov_write_tcmi_tag(AVIOContext *pb, MOVTrack *track)
2539 {
2540  int64_t pos = avio_tell(pb);
2541  const char *font = "Lucida Grande";
2542  avio_wb32(pb, 0); /* size */
2543  ffio_wfourcc(pb, "tcmi"); /* timecode media information atom */
2544  avio_wb32(pb, 0); /* version & flags */
2545  avio_wb16(pb, 0); /* text font */
2546  avio_wb16(pb, 0); /* text face */
2547  avio_wb16(pb, 12); /* text size */
2548  avio_wb16(pb, 0); /* (unknown, not in the QT specs...) */
2549  avio_wb16(pb, 0x0000); /* text color (red) */
2550  avio_wb16(pb, 0x0000); /* text color (green) */
2551  avio_wb16(pb, 0x0000); /* text color (blue) */
2552  avio_wb16(pb, 0xffff); /* background color (red) */
2553  avio_wb16(pb, 0xffff); /* background color (green) */
2554  avio_wb16(pb, 0xffff); /* background color (blue) */
2555  avio_w8(pb, strlen(font)); /* font len (part of the pascal string) */
2556  avio_write(pb, font, strlen(font)); /* font name */
2557  return update_size(pb, pos);
2558 }
2559 
2560 static int mov_write_gmhd_tag(AVIOContext *pb, MOVTrack *track)
2561 {
2562  int64_t pos = avio_tell(pb);
2563  avio_wb32(pb, 0); /* size */
2564  ffio_wfourcc(pb, "gmhd");
2565  avio_wb32(pb, 0x18); /* gmin size */
2566  ffio_wfourcc(pb, "gmin");/* generic media info */
2567  avio_wb32(pb, 0); /* version & flags */
2568  avio_wb16(pb, 0x40); /* graphics mode = */
2569  avio_wb16(pb, 0x8000); /* opColor (r?) */
2570  avio_wb16(pb, 0x8000); /* opColor (g?) */
2571  avio_wb16(pb, 0x8000); /* opColor (b?) */
2572  avio_wb16(pb, 0); /* balance */
2573  avio_wb16(pb, 0); /* reserved */
2574 
2575  /*
2576  * This special text atom is required for
2577  * Apple Quicktime chapters. The contents
2578  * don't appear to be documented, so the
2579  * bytes are copied verbatim.
2580  */
2581  if (track->tag != MKTAG('c','6','0','8')) {
2582  avio_wb32(pb, 0x2C); /* size */
2583  ffio_wfourcc(pb, "text");
2584  avio_wb16(pb, 0x01);
2585  avio_wb32(pb, 0x00);
2586  avio_wb32(pb, 0x00);
2587  avio_wb32(pb, 0x00);
2588  avio_wb32(pb, 0x01);
2589  avio_wb32(pb, 0x00);
2590  avio_wb32(pb, 0x00);
2591  avio_wb32(pb, 0x00);
2592  avio_wb32(pb, 0x00004000);
2593  avio_wb16(pb, 0x0000);
2594  }
2595 
2596  if (track->par->codec_tag == MKTAG('t','m','c','d')) {
2597  int64_t tmcd_pos = avio_tell(pb);
2598  avio_wb32(pb, 0); /* size */
2599  ffio_wfourcc(pb, "tmcd");
2600  mov_write_tcmi_tag(pb, track);
2601  update_size(pb, tmcd_pos);
2602  } else if (track->par->codec_tag == MKTAG('g','p','m','d')) {
2603  int64_t gpmd_pos = avio_tell(pb);
2604  avio_wb32(pb, 0); /* size */
2605  ffio_wfourcc(pb, "gpmd");
2606  avio_wb32(pb, 0); /* version */
2607  update_size(pb, gpmd_pos);
2608  }
2609  return update_size(pb, pos);
2610 }
2611 
2613 {
2614  avio_wb32(pb, 16); /* size */
2615  ffio_wfourcc(pb, "smhd");
2616  avio_wb32(pb, 0); /* version & flags */
2617  avio_wb16(pb, 0); /* reserved (balance, normally = 0) */
2618  avio_wb16(pb, 0); /* reserved */
2619  return 16;
2620 }
2621 
2623 {
2624  avio_wb32(pb, 0x14); /* size (always 0x14) */
2625  ffio_wfourcc(pb, "vmhd");
2626  avio_wb32(pb, 0x01); /* version & flags */
2627  avio_wb64(pb, 0); /* reserved (graphics mode = copy) */
2628  return 0x14;
2629 }
2630 
2631 static int is_clcp_track(MOVTrack *track)
2632 {
2633  return track->tag == MKTAG('c','7','0','8') ||
2634  track->tag == MKTAG('c','6','0','8');
2635 }
2636 
2638 {
2639  MOVMuxContext *mov = s->priv_data;
2640  const char *hdlr, *descr = NULL, *hdlr_type = NULL;
2641  int64_t pos = avio_tell(pb);
2642 
2643  hdlr = "dhlr";
2644  hdlr_type = "url ";
2645  descr = "DataHandler";
2646 
2647  if (track) {
2648  hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
2649  if (track->par->codec_type == AVMEDIA_TYPE_VIDEO) {
2650  hdlr_type = "vide";
2651  descr = "VideoHandler";
2652  } else if (track->par->codec_type == AVMEDIA_TYPE_AUDIO) {
2653  hdlr_type = "soun";
2654  descr = "SoundHandler";
2655  } else if (track->par->codec_type == AVMEDIA_TYPE_SUBTITLE) {
2656  if (is_clcp_track(track)) {
2657  hdlr_type = "clcp";
2658  descr = "ClosedCaptionHandler";
2659  } else {
2660  if (track->tag == MKTAG('t','x','3','g')) {
2661  hdlr_type = "sbtl";
2662  } else if (track->tag == MKTAG('m','p','4','s')) {
2663  hdlr_type = "subp";
2664  } else {
2665  hdlr_type = "text";
2666  }
2667  descr = "SubtitleHandler";
2668  }
2669  } else if (track->par->codec_tag == MKTAG('r','t','p',' ')) {
2670  hdlr_type = "hint";
2671  descr = "HintHandler";
2672  } else if (track->par->codec_tag == MKTAG('t','m','c','d')) {
2673  hdlr_type = "tmcd";
2674  descr = "TimeCodeHandler";
2675  } else if (track->par->codec_tag == MKTAG('g','p','m','d')) {
2676  hdlr_type = "meta";
2677  descr = "GoPro MET"; // GoPro Metadata
2678  } else {
2680  "Unknown hldr_type for %s, writing dummy values\n",
2681  av_fourcc2str(track->par->codec_tag));
2682  }
2683  if (track->st) {
2684  // hdlr.name is used by some players to identify the content title
2685  // of the track. So if an alternate handler description is
2686  // specified, use it.
2687  AVDictionaryEntry *t;
2688  t = av_dict_get(track->st->metadata, "handler_name", NULL, 0);
2689  if (t && utf8len(t->value))
2690  descr = t->value;
2691  }
2692  }
2693 
2694  if (mov->empty_hdlr_name) /* expressly allowed by QTFF and not prohibited in ISO 14496-12 8.4.3.3 */
2695  descr = "";
2696 
2697  avio_wb32(pb, 0); /* size */
2698  ffio_wfourcc(pb, "hdlr");
2699  avio_wb32(pb, 0); /* Version & flags */
2700  avio_write(pb, hdlr, 4); /* handler */
2701  ffio_wfourcc(pb, hdlr_type); /* handler type */
2702  avio_wb32(pb, 0); /* reserved */
2703  avio_wb32(pb, 0); /* reserved */
2704  avio_wb32(pb, 0); /* reserved */
2705  if (!track || track->mode == MODE_MOV)
2706  avio_w8(pb, strlen(descr)); /* pascal string */
2707  avio_write(pb, descr, strlen(descr)); /* handler description */
2708  if (track && track->mode != MODE_MOV)
2709  avio_w8(pb, 0); /* c string */
2710  return update_size(pb, pos);
2711 }
2712 
2714 {
2715  /* This atom must be present, but leaving the values at zero
2716  * seems harmless. */
2717  avio_wb32(pb, 28); /* size */
2718  ffio_wfourcc(pb, "hmhd");
2719  avio_wb32(pb, 0); /* version, flags */
2720  avio_wb16(pb, 0); /* maxPDUsize */
2721  avio_wb16(pb, 0); /* avgPDUsize */
2722  avio_wb32(pb, 0); /* maxbitrate */
2723  avio_wb32(pb, 0); /* avgbitrate */
2724  avio_wb32(pb, 0); /* reserved */
2725  return 28;
2726 }
2727 
2729 {
2730  int64_t pos = avio_tell(pb);
2731  int ret;
2732 
2733  avio_wb32(pb, 0); /* size */
2734  ffio_wfourcc(pb, "minf");
2735  if (track->par->codec_type == AVMEDIA_TYPE_VIDEO)
2736  mov_write_vmhd_tag(pb);
2737  else if (track->par->codec_type == AVMEDIA_TYPE_AUDIO)
2738  mov_write_smhd_tag(pb);
2739  else if (track->par->codec_type == AVMEDIA_TYPE_SUBTITLE) {
2740  if (track->tag == MKTAG('t','e','x','t') || is_clcp_track(track)) {
2741  mov_write_gmhd_tag(pb, track);
2742  } else {
2743  mov_write_nmhd_tag(pb);
2744  }
2745  } else if (track->tag == MKTAG('r','t','p',' ')) {
2746  mov_write_hmhd_tag(pb);
2747  } else if (track->tag == MKTAG('t','m','c','d')) {
2748  if (track->mode != MODE_MOV)
2749  mov_write_nmhd_tag(pb);
2750  else
2751  mov_write_gmhd_tag(pb, track);
2752  } else if (track->tag == MKTAG('g','p','m','d')) {
2753  mov_write_gmhd_tag(pb, track);
2754  }
2755  if (track->mode == MODE_MOV) /* ISO 14496-12 8.4.3.1 specifies hdlr only within mdia or meta boxes */
2756  mov_write_hdlr_tag(s, pb, NULL);
2757  mov_write_dinf_tag(pb);
2758  if ((ret = mov_write_stbl_tag(s, pb, mov, track)) < 0)
2759  return ret;
2760  return update_size(pb, pos);
2761 }
2762 
2764  MOVTrack *track)
2765 {
2766  int version = track->track_duration < INT32_MAX ? 0 : 1;
2767 
2768  if (track->mode == MODE_ISM)
2769  version = 1;
2770 
2771  (version == 1) ? avio_wb32(pb, 44) : avio_wb32(pb, 32); /* size */
2772  ffio_wfourcc(pb, "mdhd");
2773  avio_w8(pb, version);
2774  avio_wb24(pb, 0); /* flags */
2775  if (version == 1) {
2776  avio_wb64(pb, track->time);
2777  avio_wb64(pb, track->time);
2778  } else {
2779  avio_wb32(pb, track->time); /* creation time */
2780  avio_wb32(pb, track->time); /* modification time */
2781  }
2782  avio_wb32(pb, track->timescale); /* time scale (sample rate for audio) */
2783  if (!track->entry && mov->mode == MODE_ISM)
2784  (version == 1) ? avio_wb64(pb, UINT64_C(0xffffffffffffffff)) : avio_wb32(pb, 0xffffffff);
2785  else if (!track->entry)
2786  (version == 1) ? avio_wb64(pb, 0) : avio_wb32(pb, 0);
2787  else
2788  (version == 1) ? avio_wb64(pb, track->track_duration) : avio_wb32(pb, track->track_duration); /* duration */
2789  avio_wb16(pb, track->language); /* language */
2790  avio_wb16(pb, 0); /* reserved (quality) */
2791 
2792  if (version != 0 && track->mode == MODE_MOV) {
2794  "FATAL error, file duration too long for timebase, this file will not be\n"
2795  "playable with QuickTime. Choose a different timebase with "
2796  "-video_track_timescale or a different container format\n");
2797  }
2798 
2799  return 32;
2800 }
2801 
2803  MOVMuxContext *mov, MOVTrack *track)
2804 {
2805  int64_t pos = avio_tell(pb);
2806  int ret;
2807 
2808  avio_wb32(pb, 0); /* size */
2809  ffio_wfourcc(pb, "mdia");
2810  mov_write_mdhd_tag(pb, mov, track);
2811  mov_write_hdlr_tag(s, pb, track);
2812  if ((ret = mov_write_minf_tag(s, pb, mov, track)) < 0)
2813  return ret;
2814  return update_size(pb, pos);
2815 }
2816 
2817 /* transformation matrix
2818  |a b u|
2819  |c d v|
2820  |tx ty w| */
2821 static void write_matrix(AVIOContext *pb, int16_t a, int16_t b, int16_t c,
2822  int16_t d, int16_t tx, int16_t ty)
2823 {
2824  avio_wb32(pb, a << 16); /* 16.16 format */
2825  avio_wb32(pb, b << 16); /* 16.16 format */
2826  avio_wb32(pb, 0); /* u in 2.30 format */
2827  avio_wb32(pb, c << 16); /* 16.16 format */
2828  avio_wb32(pb, d << 16); /* 16.16 format */
2829  avio_wb32(pb, 0); /* v in 2.30 format */
2830  avio_wb32(pb, tx << 16); /* 16.16 format */
2831  avio_wb32(pb, ty << 16); /* 16.16 format */
2832  avio_wb32(pb, 1 << 30); /* w in 2.30 format */
2833 }
2834 
2836  MOVTrack *track, AVStream *st)
2837 {
2839  track->timescale, AV_ROUND_UP);
2840  int version = duration < INT32_MAX ? 0 : 1;
2842  int rotation = 0;
2843  int group = 0;
2844 
2845  uint32_t *display_matrix = NULL;
2846  int display_matrix_size, i;
2847 
2848  if (st) {
2849  if (mov->per_stream_grouping)
2850  group = st->index;
2851  else
2852  group = st->codecpar->codec_type;
2853 
2854  display_matrix = (uint32_t*)av_stream_get_side_data(st, AV_PKT_DATA_DISPLAYMATRIX,
2855  &display_matrix_size);
2856  if (display_matrix && display_matrix_size < 9 * sizeof(*display_matrix))
2857  display_matrix = NULL;
2858  }
2859 
2860  if (track->flags & MOV_TRACK_ENABLED)
2861  flags |= MOV_TKHD_FLAG_ENABLED;
2862 
2863  if (track->mode == MODE_ISM)
2864  version = 1;
2865 
2866  (version == 1) ? avio_wb32(pb, 104) : avio_wb32(pb, 92); /* size */
2867  ffio_wfourcc(pb, "tkhd");
2868  avio_w8(pb, version);
2869  avio_wb24(pb, flags);
2870  if (version == 1) {
2871  avio_wb64(pb, track->time);
2872  avio_wb64(pb, track->time);
2873  } else {
2874  avio_wb32(pb, track->time); /* creation time */
2875  avio_wb32(pb, track->time); /* modification time */
2876  }
2877  avio_wb32(pb, track->track_id); /* track-id */
2878  avio_wb32(pb, 0); /* reserved */
2879  if (!track->entry && mov->mode == MODE_ISM)
2880  (version == 1) ? avio_wb64(pb, UINT64_C(0xffffffffffffffff)) : avio_wb32(pb, 0xffffffff);
2881  else if (!track->entry)
2882  (version == 1) ? avio_wb64(pb, 0) : avio_wb32(pb, 0);
2883  else
2884  (version == 1) ? avio_wb64(pb, duration) : avio_wb32(pb, duration);
2885 
2886  avio_wb32(pb, 0); /* reserved */
2887  avio_wb32(pb, 0); /* reserved */
2888  avio_wb16(pb, 0); /* layer */
2889  avio_wb16(pb, group); /* alternate group) */
2890  /* Volume, only for audio */
2891  if (track->par->codec_type == AVMEDIA_TYPE_AUDIO)
2892  avio_wb16(pb, 0x0100);
2893  else
2894  avio_wb16(pb, 0);
2895  avio_wb16(pb, 0); /* reserved */
2896 
2897  /* Matrix structure */
2898 #if FF_API_OLD_ROTATE_API
2899  if (st && st->metadata) {
2900  AVDictionaryEntry *rot = av_dict_get(st->metadata, "rotate", NULL, 0);
2901  rotation = (rot && rot->value) ? atoi(rot->value) : 0;
2902  }
2903 #endif
2904  if (display_matrix) {
2905  for (i = 0; i < 9; i++)
2906  avio_wb32(pb, display_matrix[i]);
2907 #if FF_API_OLD_ROTATE_API
2908  } else if (rotation == 90) {
2909  write_matrix(pb, 0, 1, -1, 0, track->par->height, 0);
2910  } else if (rotation == 180) {
2911  write_matrix(pb, -1, 0, 0, -1, track->par->width, track->par->height);
2912  } else if (rotation == 270) {
2913  write_matrix(pb, 0, -1, 1, 0, 0, track->par->width);
2914 #endif
2915  } else {
2916  write_matrix(pb, 1, 0, 0, 1, 0, 0);
2917  }
2918  /* Track width and height, for visual only */
2919  if (st && (track->par->codec_type == AVMEDIA_TYPE_VIDEO ||
2920  track->par->codec_type == AVMEDIA_TYPE_SUBTITLE)) {
2921  int64_t track_width_1616;
2922  if (track->mode == MODE_MOV) {
2923  track_width_1616 = track->par->width * 0x10000ULL;
2924  } else {
2925  track_width_1616 = av_rescale(st->sample_aspect_ratio.num,
2926  track->par->width * 0x10000LL,
2927  st->sample_aspect_ratio.den);
2928  if (!track_width_1616 ||
2929  track->height != track->par->height ||
2930  track_width_1616 > UINT32_MAX)
2931  track_width_1616 = track->par->width * 0x10000ULL;
2932  }
2933  if (track_width_1616 > UINT32_MAX) {
2934  av_log(mov->fc, AV_LOG_WARNING, "track width is too large\n");
2935  track_width_1616 = 0;
2936  }
2937  avio_wb32(pb, track_width_1616);
2938  if (track->height > 0xFFFF) {
2939  av_log(mov->fc, AV_LOG_WARNING, "track height is too large\n");
2940  avio_wb32(pb, 0);
2941  } else
2942  avio_wb32(pb, track->height * 0x10000U);
2943  } else {
2944  avio_wb32(pb, 0);
2945  avio_wb32(pb, 0);
2946  }
2947  return 0x5c;
2948 }
2949 
2950 static int mov_write_tapt_tag(AVIOContext *pb, MOVTrack *track)
2951 {
2953  track->par->sample_aspect_ratio.den);
2954 
2955  int64_t pos = avio_tell(pb);
2956 
2957  avio_wb32(pb, 0); /* size */
2958  ffio_wfourcc(pb, "tapt");
2959 
2960  avio_wb32(pb, 20);
2961  ffio_wfourcc(pb, "clef");
2962  avio_wb32(pb, 0);
2963  avio_wb32(pb, width << 16);
2964  avio_wb32(pb, track->par->height << 16);
2965 
2966  avio_wb32(pb, 20);
2967  ffio_wfourcc(pb, "prof");
2968  avio_wb32(pb, 0);
2969  avio_wb32(pb, width << 16);
2970  avio_wb32(pb, track->par->height << 16);
2971 
2972  avio_wb32(pb, 20);
2973  ffio_wfourcc(pb, "enof");
2974  avio_wb32(pb, 0);
2975  avio_wb32(pb, track->par->width << 16);
2976  avio_wb32(pb, track->par->height << 16);
2977 
2978  return update_size(pb, pos);
2979 }
2980 
2981 // This box seems important for the psp playback ... without it the movie seems to hang
2983  MOVTrack *track)
2984 {
2986  track->timescale, AV_ROUND_UP);
2987  int version = duration < INT32_MAX ? 0 : 1;
2988  int entry_size, entry_count, size;
2989  int64_t delay, start_ct = track->start_cts;
2990  int64_t start_dts = track->start_dts;
2991 
2992  if (track->entry) {
2993  if (start_dts != track->cluster[0].dts || start_ct != track->cluster[0].cts) {
2994 
2995  av_log(mov->fc, AV_LOG_DEBUG,
2996  "EDTS using dts:%"PRId64" cts:%d instead of dts:%"PRId64" cts:%"PRId64" tid:%d\n",
2997  track->cluster[0].dts, track->cluster[0].cts,
2998  start_dts, start_ct, track->track_id);
2999  start_dts = track->cluster[0].dts;
3000  start_ct = track->cluster[0].cts;
3001  }
3002  }
3003 
3004  delay = av_rescale_rnd(start_dts + start_ct, MOV_TIMESCALE,
3005  track->timescale, AV_ROUND_DOWN);
3006  version |= delay < INT32_MAX ? 0 : 1;
3007 
3008  entry_size = (version == 1) ? 20 : 12;
3009  entry_count = 1 + (delay > 0);
3010  size = 24 + entry_count * entry_size;
3011 
3012  /* write the atom data */
3013  avio_wb32(pb, size);
3014  ffio_wfourcc(pb, "edts");
3015  avio_wb32(pb, size - 8);
3016  ffio_wfourcc(pb, "elst");
3017  avio_w8(pb, version);
3018  avio_wb24(pb, 0); /* flags */
3019 
3020  avio_wb32(pb, entry_count);
3021  if (delay > 0) { /* add an empty edit to delay presentation */
3022  /* In the positive delay case, the delay includes the cts
3023  * offset, and the second edit list entry below trims out
3024  * the same amount from the actual content. This makes sure
3025  * that the offset last sample is included in the edit
3026  * list duration as well. */
3027  if (version == 1) {
3028  avio_wb64(pb, delay);
3029  avio_wb64(pb, -1);
3030  } else {
3031  avio_wb32(pb, delay);
3032  avio_wb32(pb, -1);
3033  }
3034  avio_wb32(pb, 0x00010000);
3035  } else {
3036  /* Avoid accidentally ending up with start_ct = -1 which has got a
3037  * special meaning. Normally start_ct should end up positive or zero
3038  * here, but use FFMIN in case dts is a small positive integer
3039  * rounded to 0 when represented in MOV_TIMESCALE units. */
3040  av_assert0(av_rescale_rnd(start_dts, MOV_TIMESCALE, track->timescale, AV_ROUND_DOWN) <= 0);
3041  start_ct = -FFMIN(start_dts, 0);
3042  /* Note, this delay is calculated from the pts of the first sample,
3043  * ensuring that we don't reduce the duration for cases with
3044  * dts<0 pts=0. */
3045  duration += delay;
3046  }
3047 
3048  /* For fragmented files, we don't know the full length yet. Setting
3049  * duration to 0 allows us to only specify the offset, including
3050  * the rest of the content (from all future fragments) without specifying
3051  * an explicit duration. */
3052  if (mov->flags & FF_MOV_FLAG_FRAGMENT)
3053  duration = 0;
3054 
3055  /* duration */
3056  if (version == 1) {
3057  avio_wb64(pb, duration);
3058  avio_wb64(pb, start_ct);
3059  } else {
3060  avio_wb32(pb, duration);
3061  avio_wb32(pb, start_ct);
3062  }
3063  avio_wb32(pb, 0x00010000);
3064  return size;
3065 }
3066 
3067 static int mov_write_tref_tag(AVIOContext *pb, MOVTrack *track)
3068 {
3069  avio_wb32(pb, 20); // size
3070  ffio_wfourcc(pb, "tref");
3071  avio_wb32(pb, 12); // size (subatom)
3072  avio_wl32(pb, track->tref_tag);
3073  avio_wb32(pb, track->tref_id);
3074  return 20;
3075 }
3076 
3077 // goes at the end of each track! ... Critical for PSP playback ("Incompatible data" without it)
3079 {
3080  avio_wb32(pb, 0x34); /* size ... reports as 28 in mp4box! */
3081  ffio_wfourcc(pb, "uuid");
3082  ffio_wfourcc(pb, "USMT");
3083  avio_wb32(pb, 0x21d24fce);
3084  avio_wb32(pb, 0xbb88695c);
3085  avio_wb32(pb, 0xfac9c740);
3086  avio_wb32(pb, 0x1c); // another size here!
3087  ffio_wfourcc(pb, "MTDT");
3088  avio_wb32(pb, 0x00010012);
3089  avio_wb32(pb, 0x0a);
3090  avio_wb32(pb, 0x55c40000);
3091  avio_wb32(pb, 0x1);
3092  avio_wb32(pb, 0x0);
3093  return 0x34;
3094 }
3095 
3096 static int mov_write_udta_sdp(AVIOContext *pb, MOVTrack *track)
3097 {
3098  AVFormatContext *ctx = track->rtp_ctx;
3099  char buf[1000] = "";
3100  int len;
3101 
3102  ff_sdp_write_media(buf, sizeof(buf), ctx->streams[0], track->src_track,
3103  NULL, NULL, 0, 0, ctx);
3104  av_strlcatf(buf, sizeof(buf), "a=control:streamid=%d\r\n", track->track_id);
3105  len = strlen(buf);
3106 
3107  avio_wb32(pb, len + 24);
3108  ffio_wfourcc(pb, "udta");
3109  avio_wb32(pb, len + 16);
3110  ffio_wfourcc(pb, "hnti");
3111  avio_wb32(pb, len + 8);
3112  ffio_wfourcc(pb, "sdp ");
3113  avio_write(pb, buf, len);
3114  return len + 24;
3115 }
3116 
3118  const char *tag, const char *str)
3119 {
3120  int64_t pos = avio_tell(pb);
3121  AVDictionaryEntry *t = av_dict_get(st->metadata, str, NULL, 0);
3122  if (!t || !utf8len(t->value))
3123  return 0;
3124 
3125  avio_wb32(pb, 0); /* size */
3126  ffio_wfourcc(pb, tag); /* type */
3127  avio_write(pb, t->value, strlen(t->value)); /* UTF8 string value */
3128  return update_size(pb, pos);
3129 }
3130 
3132  AVStream *st)
3133 {
3134  AVIOContext *pb_buf;
3135  int ret, size;
3136  uint8_t *buf;
3137 
3138  if (!st)
3139  return 0;
3140 
3141  ret = avio_open_dyn_buf(&pb_buf);
3142  if (ret < 0)
3143  return ret;
3144 
3145  if (mov->mode & (MODE_MP4|MODE_MOV))
3146  mov_write_track_metadata(pb_buf, st, "name", "title");
3147 
3148  if ((size = avio_close_dyn_buf(pb_buf, &buf)) > 0) {
3149  avio_wb32(pb, size + 8);
3150  ffio_wfourcc(pb, "udta");
3151  avio_write(pb, buf, size);
3152  }
3153  av_free(buf);
3154 
3155  return 0;
3156 }
3157 
3159  MOVTrack *track, AVStream *st)
3160 {
3161  int64_t pos = avio_tell(pb);
3162  int entry_backup = track->entry;
3163  int chunk_backup = track->chunkCount;
3164  int ret;
3165 
3166  /* If we want to have an empty moov, but some samples already have been
3167  * buffered (delay_moov), pretend that no samples have been written yet. */
3168  if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV)
3169  track->chunkCount = track->entry = 0;
3170 
3171  avio_wb32(pb, 0); /* size */
3172  ffio_wfourcc(pb, "trak");
3173  mov_write_tkhd_tag(pb, mov, track, st);
3174 
3175  av_assert2(mov->use_editlist >= 0);
3176 
3177  if (track->start_dts != AV_NOPTS_VALUE) {
3178  if (mov->use_editlist)
3179  mov_write_edts_tag(pb, mov, track); // PSP Movies and several other cases require edts box
3180  else if ((track->entry && track->cluster[0].dts) || track->mode == MODE_PSP || is_clcp_track(track))
3181  av_log(mov->fc, AV_LOG_WARNING,
3182  "Not writing any edit list even though one would have been required\n");
3183  }
3184 
3185  if (track->tref_tag)
3186  mov_write_tref_tag(pb, track);
3187 
3188  if ((ret = mov_write_mdia_tag(s, pb, mov, track)) < 0)
3189  return ret;
3190  if (track->mode == MODE_PSP)
3191  mov_write_uuid_tag_psp(pb, track); // PSP Movies require this uuid box
3192  if (track->tag == MKTAG('r','t','p',' '))
3193  mov_write_udta_sdp(pb, track);
3194  if (track->mode == MODE_MOV) {
3195  if (track->par->codec_type == AVMEDIA_TYPE_VIDEO) {
3196  double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
3197  if (st->sample_aspect_ratio.num && 1.0 != sample_aspect_ratio) {
3198  mov_write_tapt_tag(pb, track);
3199  }
3200  }
3201  if (is_clcp_track(track) && st->sample_aspect_ratio.num) {
3202  mov_write_tapt_tag(pb, track);
3203  }
3204  }
3205  mov_write_track_udta_tag(pb, mov, st);
3206  track->entry = entry_backup;
3207  track->chunkCount = chunk_backup;
3208  return update_size(pb, pos);
3209 }
3210 
3212 {
3213  int i, has_audio = 0, has_video = 0;
3214  int64_t pos = avio_tell(pb);
3215  int audio_profile = mov->iods_audio_profile;
3216  int video_profile = mov->iods_video_profile;
3217  for (i = 0; i < mov->nb_streams; i++) {
3218  if (mov->tracks[i].entry > 0 || mov->flags & FF_MOV_FLAG_EMPTY_MOOV) {
3219  has_audio |= mov->tracks[i].par->codec_type == AVMEDIA_TYPE_AUDIO;
3220  has_video |= mov->tracks[i].par->codec_type == AVMEDIA_TYPE_VIDEO;
3221  }
3222  }
3223  if (audio_profile < 0)
3224  audio_profile = 0xFF - has_audio;
3225  if (video_profile < 0)
3226  video_profile = 0xFF - has_video;
3227  avio_wb32(pb, 0x0); /* size */
3228  ffio_wfourcc(pb, "iods");
3229  avio_wb32(pb, 0); /* version & flags */
3230  put_descr(pb, 0x10, 7);
3231  avio_wb16(pb, 0x004f);
3232  avio_w8(pb, 0xff);
3233  avio_w8(pb, 0xff);
3234  avio_w8(pb, audio_profile);
3235  avio_w8(pb, video_profile);
3236  avio_w8(pb, 0xff);
3237  return update_size(pb, pos);
3238 }
3239 
3240 static int mov_write_trex_tag(AVIOContext *pb, MOVTrack *track)
3241 {
3242  avio_wb32(pb, 0x20); /* size */
3243  ffio_wfourcc(pb, "trex");
3244  avio_wb32(pb, 0); /* version & flags */
3245  avio_wb32(pb, track->track_id); /* track ID */
3246  avio_wb32(pb, 1); /* default sample description index */
3247  avio_wb32(pb, 0); /* default sample duration */
3248  avio_wb32(pb, 0); /* default sample size */
3249  avio_wb32(pb, 0); /* default sample flags */
3250  return 0;
3251 }
3252 
3254 {
3255  int64_t pos = avio_tell(pb);
3256  int i;
3257  avio_wb32(pb, 0x0); /* size */
3258  ffio_wfourcc(pb, "mvex");
3259  for (i = 0; i < mov->nb_streams; i++)
3260  mov_write_trex_tag(pb, &mov->tracks[i]);
3261  return update_size(pb, pos);
3262 }
3263 
3265 {
3266  int max_track_id = 1, i;
3267  int64_t max_track_len = 0;
3268  int version;
3269 
3270  for (i = 0; i < mov->nb_streams; i++) {
3271  if (mov->tracks[i].entry > 0 && mov->tracks[i].timescale) {
3272  int64_t max_track_len_temp = av_rescale_rnd(mov->tracks[i].track_duration,
3273  MOV_TIMESCALE,
3274  mov->tracks[i].timescale,
3275  AV_ROUND_UP);
3276  if (max_track_len < max_track_len_temp)
3277  max_track_len = max_track_len_temp;
3278  if (max_track_id < mov->tracks[i].track_id)
3279  max_track_id = mov->tracks[i].track_id;
3280  }
3281  }
3282  /* If using delay_moov, make sure the output is the same as if no
3283  * samples had been written yet. */
3284  if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV) {
3285  max_track_len = 0;
3286  max_track_id = 1;
3287  }
3288 
3289  version = max_track_len < UINT32_MAX ? 0 : 1;
3290  avio_wb32(pb, version == 1 ? 120 : 108); /* size */
3291 
3292  ffio_wfourcc(pb, "mvhd");
3293  avio_w8(pb, version);
3294  avio_wb24(pb, 0); /* flags */
3295  if (version == 1) {
3296  avio_wb64(pb, mov->time);
3297  avio_wb64(pb, mov->time);
3298  } else {
3299  avio_wb32(pb, mov->time); /* creation time */
3300  avio_wb32(pb, mov->time); /* modification time */
3301  }
3302  avio_wb32(pb, MOV_TIMESCALE);
3303  (version == 1) ? avio_wb64(pb, max_track_len) : avio_wb32(pb, max_track_len); /* duration of longest track */
3304 
3305  avio_wb32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
3306  avio_wb16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
3307  avio_wb16(pb, 0); /* reserved */
3308  avio_wb32(pb, 0); /* reserved */
3309  avio_wb32(pb, 0); /* reserved */
3310 
3311  /* Matrix structure */
3312  write_matrix(pb, 1, 0, 0, 1, 0, 0);
3313 
3314  avio_wb32(pb, 0); /* reserved (preview time) */
3315  avio_wb32(pb, 0); /* reserved (preview duration) */
3316  avio_wb32(pb, 0); /* reserved (poster time) */
3317  avio_wb32(pb, 0); /* reserved (selection time) */
3318  avio_wb32(pb, 0); /* reserved (selection duration) */
3319  avio_wb32(pb, 0); /* reserved (current time) */
3320  avio_wb32(pb, max_track_id + 1); /* Next track id */
3321  return 0x6c;
3322 }
3323 
3325  AVFormatContext *s)
3326 {
3327  avio_wb32(pb, 33); /* size */
3328  ffio_wfourcc(pb, "hdlr");
3329  avio_wb32(pb, 0);
3330  avio_wb32(pb, 0);
3331  ffio_wfourcc(pb, "mdir");
3332  ffio_wfourcc(pb, "appl");
3333  avio_wb32(pb, 0);
3334  avio_wb32(pb, 0);
3335  avio_w8(pb, 0);
3336  return 33;
3337 }
3338 
3339 /* helper function to write a data tag with the specified string as data */
3340 static int mov_write_string_data_tag(AVIOContext *pb, const char *data, int lang, int long_style)
3341 {
3342  if (long_style) {
3343  int size = 16 + strlen(data);
3344  avio_wb32(pb, size); /* size */
3345  ffio_wfourcc(pb, "data");
3346  avio_wb32(pb, 1);
3347  avio_wb32(pb, 0);
3348  avio_write(pb, data, strlen(data));
3349  return size;
3350  } else {
3351  if (!lang)
3352  lang = ff_mov_iso639_to_lang("und", 1);
3353  avio_wb16(pb, strlen(data)); /* string length */
3354  avio_wb16(pb, lang);
3355  avio_write(pb, data, strlen(data));
3356  return strlen(data) + 4;
3357  }
3358 }
3359 
3360 static int mov_write_string_tag(AVIOContext *pb, const char *name,
3361  const char *value, int lang, int long_style)
3362 {
3363  int size = 0;
3364  if (value && value[0]) {
3365  int64_t pos = avio_tell(pb);
3366  avio_wb32(pb, 0); /* size */
3367  ffio_wfourcc(pb, name);
3368  mov_write_string_data_tag(pb, value, lang, long_style);
3369  size = update_size(pb, pos);
3370  }
3371  return size;
3372 }
3373 
3375  const char *tag, int *lang)
3376 {
3377  int l, len, len2;
3378  AVDictionaryEntry *t, *t2 = NULL;
3379  char tag2[16];
3380 
3381  *lang = 0;
3382 
3383  if (!(t = av_dict_get(s->metadata, tag, NULL, 0)))
3384  return NULL;
3385 
3386  len = strlen(t->key);
3387  snprintf(tag2, sizeof(tag2), "%s-", tag);
3388  while ((t2 = av_dict_get(s->metadata, tag2, t2, AV_DICT_IGNORE_SUFFIX))) {
3389  len2 = strlen(t2->key);
3390  if (len2 == len + 4 && !strcmp(t->value, t2->value)
3391  && (l = ff_mov_iso639_to_lang(&t2->key[len2 - 3], 1)) >= 0) {
3392  *lang = l;
3393  return t;
3394  }
3395  }
3396  return t;
3397 }
3398 
3400  const char *name, const char *tag,
3401  int long_style)
3402 {
3403  int lang;
3404  AVDictionaryEntry *t = get_metadata_lang(s, tag, &lang);
3405  if (!t)
3406  return 0;
3407  return mov_write_string_tag(pb, name, t->value, lang, long_style);
3408 }
3409 
3410 /* iTunes bpm number */
3412 {
3413  AVDictionaryEntry *t = av_dict_get(s->metadata, "tmpo", NULL, 0);
3414  int size = 0, tmpo = t ? atoi(t->value) : 0;
3415  if (tmpo) {
3416  size = 26;
3417  avio_wb32(pb, size);
3418  ffio_wfourcc(pb, "tmpo");
3419  avio_wb32(pb, size-8); /* size */
3420  ffio_wfourcc(pb, "data");
3421  avio_wb32(pb, 0x15); //type specifier
3422  avio_wb32(pb, 0);
3423  avio_wb16(pb, tmpo); // data
3424  }
3425  return size;
3426 }
3427 
3428 /* 3GPP TS 26.244 */
3430 {
3431  int lang;
3432  int64_t pos = avio_tell(pb);
3433  double latitude, longitude, altitude;
3434  int32_t latitude_fix, longitude_fix, altitude_fix;
3435  AVDictionaryEntry *t = get_metadata_lang(s, "location", &lang);
3436  const char *ptr, *place = "";
3437  char *end;
3438  static const char *astronomical_body = "earth";
3439  if (!t)
3440  return 0;
3441 
3442  ptr = t->value;
3443  longitude = strtod(ptr, &end);
3444  if (end == ptr) {
3445  av_log(s, AV_LOG_WARNING, "malformed location metadata\n");
3446  return 0;
3447  }
3448  ptr = end;
3449  latitude = strtod(ptr, &end);
3450  if (end == ptr) {
3451  av_log(s, AV_LOG_WARNING, "malformed location metadata\n");
3452  return 0;
3453  }
3454  ptr = end;
3455  altitude = strtod(ptr, &end);
3456  /* If no altitude was present, the default 0 should be fine */
3457  if (*end == '/')
3458  place = end + 1;
3459 
3460  latitude_fix = (int32_t) ((1 << 16) * latitude);
3461  longitude_fix = (int32_t) ((1 << 16) * longitude);
3462  altitude_fix = (int32_t) ((1 << 16) * altitude);
3463 
3464  avio_wb32(pb, 0); /* size */
3465  ffio_wfourcc(pb, "loci"); /* type */
3466  avio_wb32(pb, 0); /* version + flags */
3467  avio_wb16(pb, lang);
3468  avio_write(pb, place, strlen(place) + 1);
3469  avio_w8(pb, 0); /* role of place (0 == shooting location, 1 == real location, 2 == fictional location) */
3470  avio_wb32(pb, latitude_fix);
3471  avio_wb32(pb, longitude_fix);
3472  avio_wb32(pb, altitude_fix);
3473  avio_write(pb, astronomical_body, strlen(astronomical_body) + 1);
3474  avio_w8(pb, 0); /* additional notes, null terminated string */
3475 
3476  return update_size(pb, pos);
3477 }
3478 
3479 /* iTunes track or disc number */
3481  AVFormatContext *s, int disc)
3482 {
3484  disc ? "disc" : "track",
3485  NULL, 0);
3486  int size = 0, track = t ? atoi(t->value) : 0;
3487  if (track) {
3488  int tracks = 0;
3489  char *slash = strchr(t->value, '/');
3490  if (slash)
3491  tracks = atoi(slash + 1);
3492  avio_wb32(pb, 32); /* size */
3493  ffio_wfourcc(pb, disc ? "disk" : "trkn");
3494  avio_wb32(pb, 24); /* size */
3495  ffio_wfourcc(pb, "data");
3496  avio_wb32(pb, 0); // 8 bytes empty
3497  avio_wb32(pb, 0);
3498  avio_wb16(pb, 0); // empty
3499  avio_wb16(pb, track); // track / disc number
3500  avio_wb16(pb, tracks); // total track / disc number
3501  avio_wb16(pb, 0); // empty
3502  size = 32;
3503  }
3504  return size;
3505 }
3506 
3508  const char *name, const char *tag,
3509  int len)
3510 {
3511  AVDictionaryEntry *t = NULL;
3512  uint8_t num;
3513  int size = 24 + len;
3514 
3515  if (len != 1 && len != 4)
3516  return -1;
3517 
3518  if (!(t = av_dict_get(s->metadata, tag, NULL, 0)))
3519  return 0;
3520  num = atoi(t->value);
3521 
3522  avio_wb32(pb, size);
3523  ffio_wfourcc(pb, name);
3524  avio_wb32(pb, size - 8);
3525  ffio_wfourcc(pb, "data");
3526  avio_wb32(pb, 0x15);
3527  avio_wb32(pb, 0);
3528  if (len==4) avio_wb32(pb, num);
3529  else avio_w8 (pb, num);
3530 
3531  return size;
3532 }
3533 
3535 {
3536  MOVMuxContext *mov = s->priv_data;
3537  int64_t pos = 0;
3538  int i;
3539 
3540  for (i = 0; i < s->nb_streams; i++) {
3541  MOVTrack *trk = &mov->tracks[i];
3542 
3543  if (!is_cover_image(trk->st) || trk->cover_image.size <= 0)
3544  continue;
3545 
3546  if (!pos) {
3547  pos = avio_tell(pb);
3548  avio_wb32(pb, 0);
3549  ffio_wfourcc(pb, "covr");
3550  }
3551  avio_wb32(pb, 16 + trk->cover_image.size);
3552  ffio_wfourcc(pb, "data");
3553  avio_wb32(pb, trk->tag);
3554  avio_wb32(pb , 0);
3555  avio_write(pb, trk->cover_image.data, trk->cover_image.size);
3556  }
3557 
3558  return pos ? update_size(pb, pos) : 0;
3559 }
3560 
3561 /* iTunes meta data list */
3563  AVFormatContext *s)
3564 {
3565  int64_t pos = avio_tell(pb);
3566  avio_wb32(pb, 0); /* size */
3567  ffio_wfourcc(pb, "ilst");
3568  mov_write_string_metadata(s, pb, "\251nam", "title" , 1);
3569  mov_write_string_metadata(s, pb, "\251ART", "artist" , 1);
3570  mov_write_string_metadata(s, pb, "aART", "album_artist", 1);
3571  mov_write_string_metadata(s, pb, "\251wrt", "composer" , 1);
3572  mov_write_string_metadata(s, pb, "\251alb", "album" , 1);
3573  mov_write_string_metadata(s, pb, "\251day", "date" , 1);
3574  if (!mov_write_string_metadata(s, pb, "\251too", "encoding_tool", 1)) {
3575  if (!(s->flags & AVFMT_FLAG_BITEXACT))
3576  mov_write_string_tag(pb, "\251too", LIBAVFORMAT_IDENT, 0, 1);
3577  }
3578  mov_write_string_metadata(s, pb, "\251cmt", "comment" , 1);
3579  mov_write_string_metadata(s, pb, "\251gen", "genre" , 1);
3580  mov_write_string_metadata(s, pb, "cprt", "copyright", 1);
3581  mov_write_string_metadata(s, pb, "\251grp", "grouping" , 1);
3582  mov_write_string_metadata(s, pb, "\251lyr", "lyrics" , 1);
3583  mov_write_string_metadata(s, pb, "desc", "description",1);
3584  mov_write_string_metadata(s, pb, "ldes", "synopsis" , 1);
3585  mov_write_string_metadata(s, pb, "tvsh", "show" , 1);
3586  mov_write_string_metadata(s, pb, "tven", "episode_id",1);
3587  mov_write_string_metadata(s, pb, "tvnn", "network" , 1);
3588  mov_write_string_metadata(s, pb, "keyw", "keywords" , 1);
3589  mov_write_int8_metadata (s, pb, "tves", "episode_sort",4);
3590  mov_write_int8_metadata (s, pb, "tvsn", "season_number",4);
3591  mov_write_int8_metadata (s, pb, "stik", "media_type",1);
3592  mov_write_int8_metadata (s, pb, "hdvd", "hd_video", 1);
3593  mov_write_int8_metadata (s, pb, "pgap", "gapless_playback",1);
3594  mov_write_int8_metadata (s, pb, "cpil", "compilation", 1);
3595  mov_write_covr(pb, s);
3596  mov_write_trkn_tag(pb, mov, s, 0); // track number
3597  mov_write_trkn_tag(pb, mov, s, 1); // disc number
3598  mov_write_tmpo_tag(pb, s);
3599  return update_size(pb, pos);
3600 }
3601 
3603  AVFormatContext *s)
3604 {
3605  avio_wb32(pb, 33); /* size */
3606  ffio_wfourcc(pb, "hdlr");
3607  avio_wb32(pb, 0);
3608  avio_wb32(pb, 0);
3609  ffio_wfourcc(pb, "mdta");
3610  avio_wb32(pb, 0);
3611  avio_wb32(pb, 0);
3612  avio_wb32(pb, 0);
3613  avio_w8(pb, 0);
3614  return 33;
3615 }
3616 
3618  AVFormatContext *s)
3619 {
3620  AVDictionaryEntry *t = NULL;
3621  int64_t pos = avio_tell(pb);
3622  int64_t curpos, entry_pos;
3623  int count = 0;
3624 
3625  avio_wb32(pb, 0); /* size */
3626  ffio_wfourcc(pb, "keys");
3627  avio_wb32(pb, 0);
3628  entry_pos = avio_tell(pb);
3629  avio_wb32(pb, 0); /* entry count */
3630 
3631  while (t = av_dict_get(s->metadata, "", t, AV_DICT_IGNORE_SUFFIX)) {
3632  avio_wb32(pb, strlen(t->key) + 8);
3633  ffio_wfourcc(pb, "mdta");
3634  avio_write(pb, t->key, strlen(t->key));
3635  count += 1;
3636  }
3637  curpos = avio_tell(pb);
3638  avio_seek(pb, entry_pos, SEEK_SET);
3639  avio_wb32(pb, count); // rewrite entry count
3640  avio_seek(pb, curpos, SEEK_SET);
3641 
3642  return update_size(pb, pos);
3643 }
3644 
3646  AVFormatContext *s)
3647 {
3648  AVDictionaryEntry *t = NULL;
3649  int64_t pos = avio_tell(pb);
3650  int count = 1; /* keys are 1-index based */
3651 
3652  avio_wb32(pb, 0); /* size */
3653  ffio_wfourcc(pb, "ilst");
3654 
3655  while (t = av_dict_get(s->metadata, "", t, AV_DICT_IGNORE_SUFFIX)) {
3656  int64_t entry_pos = avio_tell(pb);
3657  avio_wb32(pb, 0); /* size */
3658  avio_wb32(pb, count); /* key */
3659  mov_write_string_data_tag(pb, t->value, 0, 1);
3660  update_size(pb, entry_pos);
3661  count += 1;
3662  }
3663  return update_size(pb, pos);
3664 }
3665 
3666 /* meta data tags */
3668  AVFormatContext *s)
3669 {
3670  int size = 0;
3671  int64_t pos = avio_tell(pb);
3672  avio_wb32(pb, 0); /* size */
3673  ffio_wfourcc(pb, "meta");
3674  avio_wb32(pb, 0);
3675  if (mov->flags & FF_MOV_FLAG_USE_MDTA) {
3676  mov_write_mdta_hdlr_tag(pb, mov, s);
3677  mov_write_mdta_keys_tag(pb, mov, s);
3678  mov_write_mdta_ilst_tag(pb, mov, s);
3679  }
3680  else {
3681  /* iTunes metadata tag */
3682  mov_write_itunes_hdlr_tag(pb, mov, s);
3683  mov_write_ilst_tag(pb, mov, s);
3684  }
3685  size = update_size(pb, pos);
3686  return size;
3687 }
3688 
3690  const char *name, const char *key)
3691 {
3692  int len;
3693  AVDictionaryEntry *t;
3694 
3695  if (!(t = av_dict_get(s->metadata, key, NULL, 0)))
3696  return 0;
3697 
3698  len = strlen(t->value);
3699  if (len > 0) {
3700  int size = len + 8;
3701  avio_wb32(pb, size);
3702  ffio_wfourcc(pb, name);
3703  avio_write(pb, t->value, len);
3704  return size;
3705  }
3706  return 0;
3707 }
3708 
3709 static int ascii_to_wc(AVIOContext *pb, const uint8_t *b)
3710 {
3711  int val;
3712  while (*b) {
3713  GET_UTF8(val, *b++, return -1;)
3714  avio_wb16(pb, val);
3715  }
3716  avio_wb16(pb, 0x00);
3717  return 0;
3718 }
3719 
3720 static uint16_t language_code(const char *str)
3721 {
3722  return (((str[0] - 0x60) & 0x1F) << 10) +
3723  (((str[1] - 0x60) & 0x1F) << 5) +
3724  (( str[2] - 0x60) & 0x1F);
3725 }
3726 
3728  const char *tag, const char *str)
3729 {
3730  int64_t pos = avio_tell(pb);
3731  AVDictionaryEntry *t = av_dict_get(s->metadata, str, NULL, 0);
3732  if (!t || !utf8len(t->value))
3733  return 0;
3734  avio_wb32(pb, 0); /* size */
3735  ffio_wfourcc(pb, tag); /* type */
3736  avio_wb32(pb, 0); /* version + flags */
3737  if (!strcmp(tag, "yrrc"))
3738  avio_wb16(pb, atoi(t->value));
3739  else {
3740  avio_wb16(pb, language_code("eng")); /* language */
3741  avio_write(pb, t->value, strlen(t->value) + 1); /* UTF8 string value */
3742  if (!strcmp(tag, "albm") &&
3743  (t = av_dict_get(s->metadata, "track", NULL, 0)))
3744  avio_w8(pb, atoi(t->value));
3745  }
3746  return update_size(pb, pos);
3747 }
3748 
3750 {
3751  int64_t pos = avio_tell(pb);
3752  int i, nb_chapters = FFMIN(s->nb_chapters, 255);
3753 
3754  avio_wb32(pb, 0); // size
3755  ffio_wfourcc(pb, "chpl");
3756  avio_wb32(pb, 0x01000000); // version + flags
3757  avio_wb32(pb, 0); // unknown
3758  avio_w8(pb, nb_chapters);
3759 
3760  for (i = 0; i < nb_chapters; i++) {
3761  AVChapter *c = s->chapters[i];
3762  AVDictionaryEntry *t;
3763  avio_wb64(pb, av_rescale_q(c->start, c->time_base, (AVRational){1,10000000}));
3764 
3765  if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
3766  int len = FFMIN(strlen(t->value), 255);
3767  avio_w8(pb, len);
3768  avio_write(pb, t->value, len);
3769  } else
3770  avio_w8(pb, 0);
3771  }
3772  return update_size(pb, pos);
3773 }
3774 
3776  AVFormatContext *s)
3777 {
3778  AVIOContext *pb_buf;
3779  int ret, size;
3780  uint8_t *buf;
3781 
3782  ret = avio_open_dyn_buf(&pb_buf);
3783  if (ret < 0)
3784  return ret;
3785 
3786  if (mov->mode & MODE_3GP) {
3787  mov_write_3gp_udta_tag(pb_buf, s, "perf", "artist");
3788  mov_write_3gp_udta_tag(pb_buf, s, "titl", "title");
3789  mov_write_3gp_udta_tag(pb_buf, s, "auth", "author");
3790  mov_write_3gp_udta_tag(pb_buf, s, "gnre", "genre");
3791  mov_write_3gp_udta_tag(pb_buf, s, "dscp", "comment");
3792  mov_write_3gp_udta_tag(pb_buf, s, "albm", "album");
3793  mov_write_3gp_udta_tag(pb_buf, s, "cprt", "copyright");
3794  mov_write_3gp_udta_tag(pb_buf, s, "yrrc", "date");
3795  mov_write_loci_tag(s, pb_buf);
3796  } else if (mov->mode == MODE_MOV && !(mov->flags & FF_MOV_FLAG_USE_MDTA)) { // the title field breaks gtkpod with mp4 and my suspicion is that stuff is not valid in mp4
3797  mov_write_string_metadata(s, pb_buf, "\251ART", "artist", 0);
3798  mov_write_string_metadata(s, pb_buf, "\251nam", "title", 0);
3799  mov_write_string_metadata(s, pb_buf, "\251aut", "author", 0);
3800  mov_write_string_metadata(s, pb_buf, "\251alb", "album", 0);
3801  mov_write_string_metadata(s, pb_buf, "\251day", "date", 0);
3802  mov_write_string_metadata(s, pb_buf, "\251swr", "encoder", 0);
3803  // currently ignored by mov.c
3804  mov_write_string_metadata(s, pb_buf, "\251des", "comment", 0);
3805  // add support for libquicktime, this atom is also actually read by mov.c
3806  mov_write_string_metadata(s, pb_buf, "\251cmt", "comment", 0);
3807  mov_write_string_metadata(s, pb_buf, "\251gen", "genre", 0);
3808  mov_write_string_metadata(s, pb_buf, "\251cpy", "copyright", 0);
3809  mov_write_string_metadata(s, pb_buf, "\251mak", "make", 0);
3810  mov_write_string_metadata(s, pb_buf, "\251mod", "model", 0);
3811  mov_write_string_metadata(s, pb_buf, "\251xyz", "location", 0);
3812  mov_write_string_metadata(s, pb_buf, "\251key", "keywords", 0);
3813  mov_write_raw_metadata_tag(s, pb_buf, "XMP_", "xmp");
3814  } else {
3815  /* iTunes meta data */
3816  mov_write_meta_tag(pb_buf, mov, s);
3817  mov_write_loci_tag(s, pb_buf);
3818  }
3819 
3820  if (s->nb_chapters && !(mov->flags & FF_MOV_FLAG_DISABLE_CHPL))
3821  mov_write_chpl_tag(pb_buf, s);
3822 
3823  if ((size = avio_close_dyn_buf(pb_buf, &buf)) > 0) {
3824  avio_wb32(pb, size + 8);
3825  ffio_wfourcc(pb, "udta");
3826  avio_write(pb, buf, size);
3827  }
3828  av_free(buf);
3829 
3830  return 0;
3831 }
3832 
3834  const char *str, const char *lang, int type)
3835 {
3836  int len = utf8len(str) + 1;
3837  if (len <= 0)
3838  return;
3839  avio_wb16(pb, len * 2 + 10); /* size */
3840  avio_wb32(pb, type); /* type */
3841  avio_wb16(pb, language_code(lang)); /* language */
3842  avio_wb16(pb, 0x01); /* ? */
3843  ascii_to_wc(pb, str);
3844 }
3845 
3847 {
3848  AVDictionaryEntry *title = av_dict_get(s->metadata, "title", NULL, 0);
3849  int64_t pos, pos2;
3850 
3851  if (title) {
3852  pos = avio_tell(pb);
3853  avio_wb32(pb, 0); /* size placeholder*/
3854  ffio_wfourcc(pb, "uuid");
3855  ffio_wfourcc(pb, "USMT");
3856  avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
3857  avio_wb32(pb, 0xbb88695c);
3858  avio_wb32(pb, 0xfac9c740);
3859 
3860  pos2 = avio_tell(pb);
3861  avio_wb32(pb, 0); /* size placeholder*/
3862  ffio_wfourcc(pb, "MTDT");
3863  avio_wb16(pb, 4);
3864 
3865  // ?
3866  avio_wb16(pb, 0x0C); /* size */
3867  avio_wb32(pb, 0x0B); /* type */
3868  avio_wb16(pb, language_code("und")); /* language */
3869  avio_wb16(pb, 0x0); /* ? */
3870  avio_wb16(pb, 0x021C); /* data */
3871 
3872  if (!(s->flags & AVFMT_FLAG_BITEXACT))
3873  mov_write_psp_udta_tag(pb, LIBAVCODEC_IDENT, "eng", 0x04);
3874  mov_write_psp_udta_tag(pb, title->value, "eng", 0x01);
3875  mov_write_psp_udta_tag(pb, "2006/04/01 11:11:11", "und", 0x03);
3876 
3877  update_size(pb, pos2);
3878  return update_size(pb, pos);
3879  }
3880 
3881  return 0;
3882 }
3883 
3884 static void build_chunks(MOVTrack *trk)
3885 {
3886  int i;
3887  MOVIentry *chunk = &trk->cluster[0];
3888  uint64_t chunkSize = chunk->size;
3889  chunk->chunkNum = 1;
3890  if (trk->chunkCount)
3891  return;
3892  trk->chunkCount = 1;
3893  for (i = 1; i<trk->entry; i++){
3894  if (chunk->pos + chunkSize == trk->cluster[i].pos &&
3895  chunkSize + trk->cluster[i].size < (1<<20)){
3896  chunkSize += trk->cluster[i].size;
3897  chunk->samples_in_chunk += trk->cluster[i].entries;
3898  } else {
3899  trk->cluster[i].chunkNum = chunk->chunkNum+1;
3900  chunk=&trk->cluster[i];
3901  chunkSize = chunk->size;
3902  trk->chunkCount++;
3903  }
3904  }
3905 }
3906 
3907 /**
3908  * Assign track ids. If option "use_stream_ids_as_track_ids" is set,
3909  * the stream ids are used as track ids.
3910  *
3911  * This assumes mov->tracks and s->streams are in the same order and
3912  * there are no gaps in either of them (so mov->tracks[n] refers to
3913  * s->streams[n]).
3914  *
3915  * As an exception, there can be more entries in
3916  * s->streams than in mov->tracks, in which case new track ids are
3917  * generated (starting after the largest found stream id).
3918  */
3920 {
3921  int i;
3922 
3923  if (mov->track_ids_ok)
3924  return 0;
3925 
3926  if (mov->use_stream_ids_as_track_ids) {
3927  int next_generated_track_id = 0;
3928  for (i = 0; i < s->nb_streams; i++) {
3929  if (s->streams[i]->id > next_generated_track_id)
3930  next_generated_track_id = s->streams[i]->id;
3931  }
3932 
3933  for (i = 0; i < mov->nb_streams; i++) {
3934  if (mov->tracks[i].entry <= 0 && !(mov->flags & FF_MOV_FLAG_FRAGMENT))
3935  continue;
3936 
3937  mov->tracks[i].track_id = i >= s->nb_streams ? ++next_generated_track_id : s->streams[i]->id;
3938  }
3939  } else {
3940  for (i = 0; i < mov->nb_streams; i++) {
3941  if (mov->tracks[i].entry <= 0 && !(mov->flags & FF_MOV_FLAG_FRAGMENT))
3942  continue;
3943 
3944  mov->tracks[i].track_id = i + 1;
3945  }
3946  }
3947 
3948  mov->track_ids_ok = 1;
3949 
3950  return 0;
3951 }
3952 
3954  AVFormatContext *s)
3955 {
3956  int i;
3957  int64_t pos = avio_tell(pb);
3958  avio_wb32(pb, 0); /* size placeholder*/
3959  ffio_wfourcc(pb, "moov");
3960 
3961  mov_setup_track_ids(mov, s);
3962 
3963  for (i = 0; i < mov->nb_streams; i++) {
3964  if (mov->tracks[i].entry <= 0 && !(mov->flags & FF_MOV_FLAG_FRAGMENT))
3965  continue;
3966 
3967  mov->tracks[i].time = mov->time;
3968 
3969  if (mov->tracks[i].entry)
3970  build_chunks(&mov->tracks[i]);
3971  }
3972 
3973  if (mov->chapter_track)
3974  for (i = 0; i < s->nb_streams; i++) {
3975  mov->tracks[i].tref_tag = MKTAG('c','h','a','p');
3976  mov->tracks[i].tref_id = mov->tracks[mov->chapter_track].track_id;
3977  }
3978  for (i = 0; i < mov->nb_streams; i++) {
3979  MOVTrack *track = &mov->tracks[i];
3980  if (track->tag == MKTAG('r','t','p',' ')) {
3981  track->tref_tag = MKTAG('h','i','n','t');
3982  track->tref_id = mov->tracks[track->src_track].track_id;
3983  } else if (track->par->codec_type == AVMEDIA_TYPE_AUDIO) {
3984  int * fallback, size;
3985  fallback = (int*)av_stream_get_side_data(track->st,
3987  &size);
3988  if (fallback != NULL && size == sizeof(int)) {
3989  if (*fallback >= 0 && *fallback < mov->nb_streams) {
3990  track->tref_tag = MKTAG('f','a','l','l');
3991  track->tref_id = mov->tracks[*fallback].track_id;
3992  }
3993  }
3994  }
3995  }
3996  for (i = 0; i < mov->nb_streams; i++) {
3997  if (mov->tracks[i].tag == MKTAG('t','m','c','d')) {
3998  int src_trk = mov->tracks[i].src_track;
3999  mov->tracks[src_trk].tref_tag = mov->tracks[i].tag;
4000  mov->tracks[src_trk].tref_id = mov->tracks[i].track_id;
4001  //src_trk may have a different timescale than the tmcd track
4002  mov->tracks[i].track_duration = av_rescale(mov->tracks[src_trk].track_duration,
4003  mov->tracks[i].timescale,
4004  mov->tracks[src_trk].timescale);
4005  }
4006  }
4007 
4008  mov_write_mvhd_tag(pb, mov);
4009  if (mov->mode != MODE_MOV && !mov->iods_skip)
4010  mov_write_iods_tag(pb, mov);
4011  for (i = 0; i < mov->nb_streams; i++) {
4012  if (mov->tracks[i].entry > 0 || mov->flags & FF_MOV_FLAG_FRAGMENT) {
4013  int ret = mov_write_trak_tag(s, pb, mov, &(mov->tracks[i]), i < s->nb_streams ? s->streams[i] : NULL);
4014  if (ret < 0)
4015  return ret;
4016  }
4017  }
4018  if (mov->flags & FF_MOV_FLAG_FRAGMENT)
4019  mov_write_mvex_tag(pb, mov); /* QuickTime requires trak to precede this */
4020 
4021  if (mov->mode == MODE_PSP)
4022  mov_write_uuidusmt_tag(pb, s);
4023  else
4024  mov_write_udta_tag(pb, mov, s);
4025 
4026  return update_size(pb, pos);
4027 }
4028 
4029 static void param_write_int(AVIOContext *pb, const char *name, int value)
4030 {
4031  avio_printf(pb, "<param name=\"%s\" value=\"%d\" valuetype=\"data\"/>\n", name, value);
4032 }
4033 
4034 static void param_write_string(AVIOContext *pb, const char *name, const char *value)
4035 {
4036  avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, value);
4037 }
4038 
4039 static void param_write_hex(AVIOContext *pb, const char *name, const uint8_t *value, int len)
4040 {
4041  char buf[150];
4042  len = FFMIN(sizeof(buf) / 2 - 1, len);
4043  ff_data_to_hex(buf, value, len, 0);
4044  buf[2 * len] = '\0';
4045  avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, buf);
4046 }
4047 
4049 {
4050  int64_t pos = avio_tell(pb);
4051  int i;
4052  int64_t manifest_bit_rate = 0;
4053  AVCPBProperties *props = NULL;
4054 
4055  static const uint8_t uuid[] = {
4056  0xa5, 0xd4, 0x0b, 0x30, 0xe8, 0x14, 0x11, 0xdd,
4057  0xba, 0x2f, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66
4058  };
4059 
4060  avio_wb32(pb, 0);
4061  ffio_wfourcc(pb, "uuid");
4062  avio_write(pb, uuid, sizeof(uuid));
4063  avio_wb32(pb, 0);
4064 
4065  avio_printf(pb, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
4066  avio_printf(pb, "<smil xmlns=\"http://www.w3.org/2001/SMIL20/Language\">\n");
4067  avio_printf(pb, "<head>\n");
4068  if (!(mov->fc->flags & AVFMT_FLAG_BITEXACT))
4069  avio_printf(pb, "<meta name=\"creator\" content=\"%s\" />\n",
4071  avio_printf(pb, "</head>\n");
4072  avio_printf(pb, "<body>\n");
4073  avio_printf(pb, "<switch>\n");
4074 
4075  mov_setup_track_ids(mov, s);
4076 
4077  for (i = 0; i < mov->nb_streams; i++) {
4078  MOVTrack *track = &mov->tracks[i];
4079  const char *type;
4080  int track_id = track->track_id;
4081  char track_name_buf[32] = { 0 };
4082 
4083  AVStream *st = track->st;
4084  AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL,0);
4085 
4086  if (track->par->codec_type == AVMEDIA_TYPE_VIDEO && !is_cover_image(st)) {
4087  type = "video";
4088  } else if (track->par->codec_type == AVMEDIA_TYPE_AUDIO) {
4089  type = "audio";
4090  } else {
4091  continue;
4092  }
4093 
4095 
4096  if (track->par->bit_rate) {
4097  manifest_bit_rate = track->par->bit_rate;
4098  } else if (props) {
4099  manifest_bit_rate = props->max_bitrate;
4100  }
4101 
4102  avio_printf(pb, "<%s systemBitrate=\"%"PRId64"\">\n", type,
4103  manifest_bit_rate);
4104  param_write_int(pb, "systemBitrate", manifest_bit_rate);
4105  param_write_int(pb, "trackID", track_id);
4106  param_write_string(pb, "systemLanguage", lang ? lang->value : "und");
4107 
4108  /* Build track name piece by piece: */
4109  /* 1. track type */
4110  av_strlcat(track_name_buf, type, sizeof(track_name_buf));
4111  /* 2. track language, if available */
4112  if (lang)
4113  av_strlcatf(track_name_buf, sizeof(track_name_buf),
4114  "_%s", lang->value);
4115  /* 3. special type suffix */
4116  /* "_cc" = closed captions, "_ad" = audio_description */
4118  av_strlcat(track_name_buf, "_cc", sizeof(track_name_buf));
4120  av_strlcat(track_name_buf, "_ad", sizeof(track_name_buf));
4121 
4122  param_write_string(pb, "trackName", track_name_buf);
4123 
4124  if (track->par->codec_type == AVMEDIA_TYPE_VIDEO) {
4125  if (track->par->codec_id == AV_CODEC_ID_H264) {
4126  uint8_t *ptr;
4127  int size = track->par->extradata_size;
4128  if (!ff_avc_write_annexb_extradata(track->par->extradata, &ptr,
4129  &size)) {
4130  param_write_hex(pb, "CodecPrivateData",
4131  ptr ? ptr : track->par->extradata,
4132  size);
4133  av_free(ptr);
4134  }
4135  param_write_string(pb, "FourCC", "H264");
4136  } else if (track->par->codec_id == AV_CODEC_ID_VC1) {
4137  param_write_string(pb, "FourCC", "WVC1");
4138  param_write_hex(pb, "CodecPrivateData", track->par->extradata,
4139  track->par->extradata_size);
4140  }
4141  param_write_int(pb, "MaxWidth", track->par->width);
4142  param_write_int(pb, "MaxHeight", track->par->height);
4143  param_write_int(pb, "DisplayWidth", track->par->width);
4144  param_write_int(pb, "DisplayHeight", track->par->height);
4145  } else {
4146  if (track->par->codec_id == AV_CODEC_ID_AAC) {
4147  switch (track->par->profile)
4148  {
4149  case FF_PROFILE_AAC_HE_V2:
4150  param_write_string(pb, "FourCC", "AACP");
4151  break;
4152  case FF_PROFILE_AAC_HE:
4153  param_write_string(pb, "FourCC", "AACH");
4154  break;
4155  default:
4156  param_write_string(pb, "FourCC", "AACL");
4157  }
4158  } else if (track->par->codec_id == AV_CODEC_ID_WMAPRO) {
4159  param_write_string(pb, "FourCC", "WMAP");
4160  }
4161  param_write_hex(pb, "CodecPrivateData", track->par->extradata,
4162  track->par->extradata_size);
4164  track->par->codec_id));
4165  param_write_int(pb, "Channels", track->par->channels);
4166  param_write_int(pb, "SamplingRate", track->par->sample_rate);
4167  param_write_int(pb, "BitsPerSample", 16);
4168  param_write_int(pb, "PacketSize", track->par->block_align ?
4169  track->par->block_align : 4);
4170  }
4171  avio_printf(pb, "</%s>\n", type);
4172  }
4173  avio_printf(pb, "</switch>\n");
4174  avio_printf(pb, "</body>\n");
4175  avio_printf(pb, "</smil>\n");
4176 
4177  return update_size(pb, pos);
4178 }
4179 
4181 {
4182  avio_wb32(pb, 16);
4183  ffio_wfourcc(pb, "mfhd");
4184  avio_wb32(pb, 0);
4185  avio_wb32(pb, mov->fragments);
4186  return 0;
4187 }
4188 
4189 static uint32_t get_sample_flags(MOVTrack *track, MOVIentry *entry)
4190 {
4193 }
4194 
4196  MOVTrack *track, int64_t moof_offset)
4197 {
4198  int64_t pos = avio_tell(pb);
4201  if (!track->entry) {
4202  flags |= MOV_TFHD_DURATION_IS_EMPTY;
4203  } else {
4204  flags |= MOV_TFHD_DEFAULT_FLAGS;
4205  }
4207  flags &= ~MOV_TFHD_BASE_DATA_OFFSET;
4208  if (mov->flags & FF_MOV_FLAG_DEFAULT_BASE_MOOF) {
4209  flags &= ~MOV_TFHD_BASE_DATA_OFFSET;
4211  }
4212 
4213  /* Don't set a default sample size, the silverlight player refuses
4214  * to play files with that set. Don't set a default sample duration,
4215  * WMP freaks out if it is set. Don't set a base data offset, PIFF
4216  * file format says it MUST NOT be set. */
4217  if (track->mode == MODE_ISM)
4220 
4221  avio_wb32(pb, 0); /* size placeholder */
4222  ffio_wfourcc(pb, "tfhd");
4223  avio_w8(pb, 0); /* version */
4224  avio_wb24(pb, flags);
4225 
4226  avio_wb32(pb, track->track_id); /* track-id */
4227  if (flags & MOV_TFHD_BASE_DATA_OFFSET)
4228  avio_wb64(pb, moof_offset);
4229  if (flags & MOV_TFHD_DEFAULT_DURATION) {
4230  track->default_duration = get_cluster_duration(track, 0);
4231  avio_wb32(pb, track->default_duration);
4232  }
4233  if (flags & MOV_TFHD_DEFAULT_SIZE) {
4234  track->default_size = track->entry ? track->cluster[0].size : 1;
4235  avio_wb32(pb, track->default_size);
4236  } else
4237  track->default_size = -1;
4238 
4239  if (flags & MOV_TFHD_DEFAULT_FLAGS) {
4240  /* Set the default flags based on the second sample, if available.
4241  * If the first sample is different, that can be signaled via a separate field. */
4242  if (track->entry > 1)
4243  track->default_sample_flags = get_sample_flags(track, &track->cluster[1]);
4244  else
4245  track->default_sample_flags =
4246  track->par->codec_type == AVMEDIA_TYPE_VIDEO ?
4249  avio_wb32(pb, track->default_sample_flags);
4250  }
4251 
4252  return update_size(pb, pos);
4253 }
4254 
4256  MOVTrack *track, int moof_size,
4257  int first, int end)
4258 {
4259  int64_t pos = avio_tell(pb);
4260  uint32_t flags = MOV_TRUN_DATA_OFFSET;
4261  int i;
4262 
4263  for (i = first; i < end; i++) {
4264  if (get_cluster_duration(track, i) != track->default_duration)
4265  flags |= MOV_TRUN_SAMPLE_DURATION;
4266  if (track->cluster[i].size != track->default_size)
4267  flags |= MOV_TRUN_SAMPLE_SIZE;
4268  if (i > first && get_sample_flags(track, &track->cluster[i]) != track->default_sample_flags)
4269  flags |= MOV_TRUN_SAMPLE_FLAGS;
4270  }
4271  if (!(flags & MOV_TRUN_SAMPLE_FLAGS) && track->entry > 0 &&
4272  get_sample_flags(track, &track->cluster[0]) != track->default_sample_flags)
4273  flags |= MOV_TRUN_FIRST_SAMPLE_FLAGS;
4274  if (track->flags & MOV_TRACK_CTTS)
4275  flags |= MOV_TRUN_SAMPLE_CTS;
4276 
4277  avio_wb32(pb, 0); /* size placeholder */
4278  ffio_wfourcc(pb, "trun");
4280  avio_w8(pb, 1); /* version */
4281  else
4282  avio_w8(pb, 0); /* version */
4283  avio_wb24(pb, flags);
4284 
4285  avio_wb32(pb, end - first); /* sample count */
4286  if (mov->flags & FF_MOV_FLAG_OMIT_TFHD_OFFSET &&
4288  !mov->first_trun)
4289  avio_wb32(pb, 0); /* Later tracks follow immediately after the previous one */
4290  else
4291  avio_wb32(pb, moof_size + 8 + track->data_offset +
4292  track->cluster[first].pos); /* data offset */
4293  if (flags & MOV_TRUN_FIRST_SAMPLE_FLAGS)
4294  avio_wb32(pb, get_sample_flags(track, &track->cluster[first]));
4295 
4296  for (i = first; i < end; i++) {
4297  if (flags & MOV_TRUN_SAMPLE_DURATION)
4298  avio_wb32(pb, get_cluster_duration(track, i));
4299  if (flags & MOV_TRUN_SAMPLE_SIZE)
4300  avio_wb32(pb, track->cluster[i].size);
4301  if (flags & MOV_TRUN_SAMPLE_FLAGS)
4302  avio_wb32(pb, get_sample_flags(track, &track->cluster[i]));
4303  if (flags & MOV_TRUN_SAMPLE_CTS)
4304  avio_wb32(pb, track->cluster[i].cts);
4305  }
4306 
4307  mov->first_trun = 0;
4308  return update_size(pb, pos);
4309 }
4310 
4311 static int mov_write_tfxd_tag(AVIOContext *pb, MOVTrack *track)
4312 {
4313  int64_t pos = avio_tell(pb);
4314  static const uint8_t uuid[] = {
4315  0x6d, 0x1d, 0x9b, 0x05, 0x42, 0xd5, 0x44, 0xe6,
4316  0x80, 0xe2, 0x14, 0x1d, 0xaf, 0xf7, 0x57, 0xb2
4317  };
4318 
4319  avio_wb32(pb, 0); /* size placeholder */
4320  ffio_wfourcc(pb, "uuid");
4321  avio_write(pb, uuid, sizeof(uuid));
4322  avio_w8(pb, 1);
4323  avio_wb24(pb, 0);
4324  avio_wb64(pb, track->start_dts + track->frag_start +
4325  track->cluster[0].cts);
4326  avio_wb64(pb, track->end_pts -
4327  (track->cluster[0].dts + track->cluster[0].cts));
4328 
4329  return update_size(pb, pos);
4330 }
4331 
4333  MOVTrack *track, int entry)
4334 {
4335  int n = track->nb_frag_info - 1 - entry, i;
4336  int size = 8 + 16 + 4 + 1 + 16*n;
4337  static const uint8_t uuid[] = {
4338  0xd4, 0x80, 0x7e, 0xf2, 0xca, 0x39, 0x46, 0x95,
4339  0x8e, 0x54, 0x26, 0xcb, 0x9e, 0x46, 0xa7, 0x9f
4340  };
4341 
4342  if (entry < 0)
4343  return 0;
4344 
4345  avio_seek(pb, track->frag_info[entry].tfrf_offset, SEEK_SET);
4346  avio_wb32(pb, size);
4347  ffio_wfourcc(pb, "uuid");
4348  avio_write(pb, uuid, sizeof(uuid));
4349  avio_w8(pb, 1);
4350  avio_wb24(pb, 0);
4351  avio_w8(pb, n);
4352  for (i = 0; i < n; i++) {
4353  int index = entry + 1 + i;
4354  avio_wb64(pb, track->frag_info[index].time);
4355  avio_wb64(pb, track->frag_info[index].duration);
4356  }
4357  if (n < mov->ism_lookahead) {
4358  int free_size = 16 * (mov->ism_lookahead - n);
4359  avio_wb32(pb, free_size);
4360  ffio_wfourcc(pb, "free");
4361  ffio_fill(pb, 0, free_size - 8);
4362  }
4363 
4364  return 0;
4365 }
4366 
4368  MOVTrack *track)
4369 {
4370  int64_t pos = avio_tell(pb);
4371  int i;
4372  for (i = 0; i < mov->ism_lookahead; i++) {
4373  /* Update the tfrf tag for the last ism_lookahead fragments,
4374  * nb_frag_info - 1 is the next fragment to be written. */
4375  mov_write_tfrf_tag(pb, mov, track, track->nb_frag_info - 2 - i);
4376  }
4377  avio_seek(pb, pos, SEEK_SET);
4378  return 0;
4379 }
4380 
4381 static int mov_add_tfra_entries(AVIOContext *pb, MOVMuxContext *mov, int tracks,
4382  int size)
4383 {
4384  int i;
4385  for (i = 0; i < mov->nb_streams; i++) {
4386  MOVTrack *track = &mov->tracks[i];
4388  if ((tracks >= 0 && i != tracks) || !track->entry)
4389  continue;
4390  track->nb_frag_info++;
4391  if (track->nb_frag_info >= track->frag_info_capacity) {
4392  unsigned new_capacity = track->nb_frag_info + MOV_FRAG_INFO_ALLOC_INCREMENT;
4393  if (av_reallocp_array(&track->frag_info,
4394  new_capacity,
4395  sizeof(*track->frag_info)))
4396  return AVERROR(ENOMEM);
4397  track->frag_info_capacity = new_capacity;
4398  }
4399  info = &track->frag_info[track->nb_frag_info - 1];
4400  info->offset = avio_tell(pb);
4401  info->size = size;
4402  // Try to recreate the original pts for the first packet
4403  // from the fields we have stored
4404  info->time = track->start_dts + track->frag_start +
4405  track->cluster[0].cts;
4406  info->duration = track->end_pts -
4407  (track->cluster[0].dts + track->cluster[0].cts);
4408  // If the pts is less than zero, we will have trimmed
4409  // away parts of the media track using an edit list,
4410  // and the corresponding start presentation time is zero.
4411  if (info->time < 0) {