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