FFmpeg  4.1.11
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 + ((34 * (info->num_ind_sub + 1) + 7) >> 3);
545  buf = av_malloc(size);
546  if (!buf) {
547  return AVERROR(ENOMEM);
548  }
549 
550  init_put_bits(&pbc, buf, size);
551  put_bits(&pbc, 13, info->data_rate);
552  put_bits(&pbc, 3, info->num_ind_sub);
553  for (i = 0; i <= info->num_ind_sub; i++) {
554  put_bits(&pbc, 2, info->substream[i].fscod);
555  put_bits(&pbc, 5, info->substream[i].bsid);
556  put_bits(&pbc, 1, 0); /* reserved */
557  put_bits(&pbc, 1, 0); /* asvc */
558  put_bits(&pbc, 3, info->substream[i].bsmod);
559  put_bits(&pbc, 3, info->substream[i].acmod);
560  put_bits(&pbc, 1, info->substream[i].lfeon);
561  put_bits(&pbc, 5, 0); /* reserved */
562  put_bits(&pbc, 4, info->substream[i].num_dep_sub);
563  if (!info->substream[i].num_dep_sub) {
564  put_bits(&pbc, 1, 0); /* reserved */
565  } else {
566  put_bits(&pbc, 9, info->substream[i].chan_loc);
567  }
568  }
569  flush_put_bits(&pbc);
570  size = put_bits_count(&pbc) >> 3;
571 
572  avio_wb32(pb, size + 8);
573  ffio_wfourcc(pb, "dec3");
574  avio_write(pb, buf, size);
575 
576  av_free(buf);
577 
578  return size;
579 }
580 
581 /**
582  * This function writes extradata "as is".
583  * Extradata must be formatted like a valid atom (with size and tag).
584  */
586 {
587  avio_write(pb, track->par->extradata, track->par->extradata_size);
588  return track->par->extradata_size;
589 }
590 
592 {
593  avio_wb32(pb, 10);
594  ffio_wfourcc(pb, "enda");
595  avio_wb16(pb, 1); /* little endian */
596  return 10;
597 }
598 
600 {
601  avio_wb32(pb, 10);
602  ffio_wfourcc(pb, "enda");
603  avio_wb16(pb, 0); /* big endian */
604  return 10;
605 }
606 
607 static void put_descr(AVIOContext *pb, int tag, unsigned int size)
608 {
609  int i = 3;
610  avio_w8(pb, tag);
611  for (; i > 0; i--)
612  avio_w8(pb, (size >> (7 * i)) | 0x80);
613  avio_w8(pb, size & 0x7F);
614 }
615 
616 static unsigned compute_avg_bitrate(MOVTrack *track)
617 {
618  uint64_t size = 0;
619  int i;
620  if (!track->track_duration)
621  return 0;
622  for (i = 0; i < track->entry; i++)
623  size += track->cluster[i].size;
624  return size * 8 * track->timescale / track->track_duration;
625 }
626 
627 static int mov_write_esds_tag(AVIOContext *pb, MOVTrack *track) // Basic
628 {
629  AVCPBProperties *props;
630  int64_t pos = avio_tell(pb);
631  int decoder_specific_info_len = track->vos_len ? 5 + track->vos_len : 0;
632  unsigned avg_bitrate;
633 
634  avio_wb32(pb, 0); // size
635  ffio_wfourcc(pb, "esds");
636  avio_wb32(pb, 0); // Version
637 
638  // ES descriptor
639  put_descr(pb, 0x03, 3 + 5+13 + decoder_specific_info_len + 5+1);
640  avio_wb16(pb, track->track_id);
641  avio_w8(pb, 0x00); // flags (= no flags)
642 
643  // DecoderConfig descriptor
644  put_descr(pb, 0x04, 13 + decoder_specific_info_len);
645 
646  // Object type indication
647  if ((track->par->codec_id == AV_CODEC_ID_MP2 ||
648  track->par->codec_id == AV_CODEC_ID_MP3) &&
649  track->par->sample_rate > 24000)
650  avio_w8(pb, 0x6B); // 11172-3
651  else
653 
654  // the following fields is made of 6 bits to identify the streamtype (4 for video, 5 for audio)
655  // plus 1 bit to indicate upstream and 1 bit set to 1 (reserved)
656  if (track->par->codec_id == AV_CODEC_ID_DVD_SUBTITLE)
657  avio_w8(pb, (0x38 << 2) | 1); // flags (= NeroSubpicStream)
658  else if (track->par->codec_type == AVMEDIA_TYPE_AUDIO)
659  avio_w8(pb, 0x15); // flags (= Audiostream)
660  else
661  avio_w8(pb, 0x11); // flags (= Visualstream)
662 
664  NULL);
665 
666  avio_wb24(pb, props ? props->buffer_size / 8 : 0); // Buffersize DB
667 
668  avg_bitrate = compute_avg_bitrate(track);
669  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)
670  avio_wb32(pb, avg_bitrate);
671 
672  if (track->vos_len) {
673  // DecoderSpecific info descriptor
674  put_descr(pb, 0x05, track->vos_len);
675  avio_write(pb, track->vos_data, track->vos_len);
676  }
677 
678  // SL descriptor
679  put_descr(pb, 0x06, 1);
680  avio_w8(pb, 0x02);
681  return update_size(pb, pos);
682 }
683 
685 {
686  return codec_id == AV_CODEC_ID_PCM_S24LE ||
687  codec_id == AV_CODEC_ID_PCM_S32LE ||
688  codec_id == AV_CODEC_ID_PCM_F32LE ||
689  codec_id == AV_CODEC_ID_PCM_F64LE;
690 }
691 
693 {
694  return codec_id == AV_CODEC_ID_PCM_S24BE ||
695  codec_id == AV_CODEC_ID_PCM_S32BE ||
696  codec_id == AV_CODEC_ID_PCM_F32BE ||
697  codec_id == AV_CODEC_ID_PCM_F64BE;
698 }
699 
701 {
702  int ret;
703  int64_t pos = avio_tell(pb);
704  avio_wb32(pb, 0);
705  avio_wl32(pb, track->tag); // store it byteswapped
706  track->par->codec_tag = av_bswap16(track->tag >> 16);
707  if ((ret = ff_put_wav_header(s, pb, track->par, 0)) < 0)
708  return ret;
709  return update_size(pb, pos);
710 }
711 
713 {
714  int ret;
715  int64_t pos = avio_tell(pb);
716  avio_wb32(pb, 0);
717  ffio_wfourcc(pb, "wfex");
718  if ((ret = ff_put_wav_header(s, pb, track->st->codecpar, FF_PUT_WAV_HEADER_FORCE_WAVEFORMATEX)) < 0)
719  return ret;
720  return update_size(pb, pos);
721 }
722 
723 static int mov_write_dfla_tag(AVIOContext *pb, MOVTrack *track)
724 {
725  int64_t pos = avio_tell(pb);
726  avio_wb32(pb, 0);
727  ffio_wfourcc(pb, "dfLa");
728  avio_w8(pb, 0); /* version */
729  avio_wb24(pb, 0); /* flags */
730 
731  /* Expect the encoder to pass a METADATA_BLOCK_TYPE_STREAMINFO. */
732  if (track->par->extradata_size != FLAC_STREAMINFO_SIZE)
733  return AVERROR_INVALIDDATA;
734 
735  /* TODO: Write other METADATA_BLOCK_TYPEs if the encoder makes them available. */
736  avio_w8(pb, 1 << 7 | FLAC_METADATA_TYPE_STREAMINFO); /* LastMetadataBlockFlag << 7 | BlockType */
737  avio_wb24(pb, track->par->extradata_size); /* Length */
738  avio_write(pb, track->par->extradata, track->par->extradata_size); /* BlockData[Length] */
739 
740  return update_size(pb, pos);
741 }
742 
743 static int mov_write_dops_tag(AVIOContext *pb, MOVTrack *track)
744 {
745  int64_t pos = avio_tell(pb);
746  avio_wb32(pb, 0);
747  ffio_wfourcc(pb, "dOps");
748  avio_w8(pb, 0); /* Version */
749  if (track->par->extradata_size < 19) {
750  av_log(pb, AV_LOG_ERROR, "invalid extradata size\n");
751  return AVERROR_INVALIDDATA;
752  }
753  /* extradata contains an Ogg OpusHead, other than byte-ordering and
754  OpusHead's preceeding magic/version, OpusSpecificBox is currently
755  identical. */
756  avio_w8(pb, AV_RB8(track->par->extradata + 9)); /* OuputChannelCount */
757  avio_wb16(pb, AV_RL16(track->par->extradata + 10)); /* PreSkip */
758  avio_wb32(pb, AV_RL32(track->par->extradata + 12)); /* InputSampleRate */
759  avio_wb16(pb, AV_RL16(track->par->extradata + 16)); /* OutputGain */
760  /* Write the rest of the header out without byte-swapping. */
761  avio_write(pb, track->par->extradata + 18, track->par->extradata_size - 18);
762 
763  return update_size(pb, pos);
764 }
765 
767 {
768  uint32_t layout_tag, bitmap;
769  int64_t pos = avio_tell(pb);
770 
771  layout_tag = ff_mov_get_channel_layout_tag(track->par->codec_id,
772  track->par->channel_layout,
773  &bitmap);
774  if (!layout_tag) {
775  av_log(s, AV_LOG_WARNING, "not writing 'chan' tag due to "
776  "lack of channel information\n");
777  return 0;
778  }
779 
780  if (track->multichannel_as_mono)
781  return 0;
782 
783  avio_wb32(pb, 0); // Size
784  ffio_wfourcc(pb, "chan"); // Type
785  avio_w8(pb, 0); // Version
786  avio_wb24(pb, 0); // Flags
787  avio_wb32(pb, layout_tag); // mChannelLayoutTag
788  avio_wb32(pb, bitmap); // mChannelBitmap
789  avio_wb32(pb, 0); // mNumberChannelDescriptions
790 
791  return update_size(pb, pos);
792 }
793 
795 {
796  int64_t pos = avio_tell(pb);
797 
798  avio_wb32(pb, 0); /* size */
799  ffio_wfourcc(pb, "wave");
800 
801  if (track->par->codec_id != AV_CODEC_ID_QDM2) {
802  avio_wb32(pb, 12); /* size */
803  ffio_wfourcc(pb, "frma");
804  avio_wl32(pb, track->tag);
805  }
806 
807  if (track->par->codec_id == AV_CODEC_ID_AAC) {
808  /* useless atom needed by mplayer, ipod, not needed by quicktime */
809  avio_wb32(pb, 12); /* size */
810  ffio_wfourcc(pb, "mp4a");
811  avio_wb32(pb, 0);
812  mov_write_esds_tag(pb, track);
813  } else if (mov_pcm_le_gt16(track->par->codec_id)) {
814  mov_write_enda_tag(pb);
815  } else if (mov_pcm_be_gt16(track->par->codec_id)) {
817  } else if (track->par->codec_id == AV_CODEC_ID_AMR_NB) {
818  mov_write_amr_tag(pb, track);
819  } else if (track->par->codec_id == AV_CODEC_ID_AC3) {
820  mov_write_ac3_tag(pb, track);
821  } else if (track->par->codec_id == AV_CODEC_ID_EAC3) {
822  mov_write_eac3_tag(pb, track);
823  } else if (track->par->codec_id == AV_CODEC_ID_ALAC ||
824  track->par->codec_id == AV_CODEC_ID_QDM2) {
825  mov_write_extradata_tag(pb, track);
826  } else if (track->par->codec_id == AV_CODEC_ID_ADPCM_MS ||
828  mov_write_ms_tag(s, pb, track);
829  }
830 
831  avio_wb32(pb, 8); /* size */
832  avio_wb32(pb, 0); /* null tag */
833 
834  return update_size(pb, pos);
835 }
836 
838 {
839  uint8_t *unescaped;
840  const uint8_t *start, *next, *end = track->vos_data + track->vos_len;
841  int unescaped_size, seq_found = 0;
842  int level = 0, interlace = 0;
843  int packet_seq = track->vc1_info.packet_seq;
844  int packet_entry = track->vc1_info.packet_entry;
845  int slices = track->vc1_info.slices;
846  PutBitContext pbc;
847 
848  if (track->start_dts == AV_NOPTS_VALUE) {
849  /* No packets written yet, vc1_info isn't authoritative yet. */
850  /* Assume inline sequence and entry headers. */
851  packet_seq = packet_entry = 1;
853  "moov atom written before any packets, unable to write correct "
854  "dvc1 atom. Set the delay_moov flag to fix this.\n");
855  }
856 
857  unescaped = av_mallocz(track->vos_len + AV_INPUT_BUFFER_PADDING_SIZE);
858  if (!unescaped)
859  return AVERROR(ENOMEM);
860  start = find_next_marker(track->vos_data, end);
861  for (next = start; next < end; start = next) {
862  GetBitContext gb;
863  int size;
864  next = find_next_marker(start + 4, end);
865  size = next - start - 4;
866  if (size <= 0)
867  continue;
868  unescaped_size = vc1_unescape_buffer(start + 4, size, unescaped);
869  init_get_bits(&gb, unescaped, 8 * unescaped_size);
870  if (AV_RB32(start) == VC1_CODE_SEQHDR) {
871  int profile = get_bits(&gb, 2);
872  if (profile != PROFILE_ADVANCED) {
873  av_free(unescaped);
874  return AVERROR(ENOSYS);
875  }
876  seq_found = 1;
877  level = get_bits(&gb, 3);
878  /* chromaformat, frmrtq_postproc, bitrtq_postproc, postprocflag,
879  * width, height */
880  skip_bits_long(&gb, 2 + 3 + 5 + 1 + 2*12);
881  skip_bits(&gb, 1); /* broadcast */
882  interlace = get_bits1(&gb);
883  skip_bits(&gb, 4); /* tfcntrflag, finterpflag, reserved, psf */
884  }
885  }
886  if (!seq_found) {
887  av_free(unescaped);
888  return AVERROR(ENOSYS);
889  }
890 
891  init_put_bits(&pbc, buf, 7);
892  /* VC1DecSpecStruc */
893  put_bits(&pbc, 4, 12); /* profile - advanced */
894  put_bits(&pbc, 3, level);
895  put_bits(&pbc, 1, 0); /* reserved */
896  /* VC1AdvDecSpecStruc */
897  put_bits(&pbc, 3, level);
898  put_bits(&pbc, 1, 0); /* cbr */
899  put_bits(&pbc, 6, 0); /* reserved */
900  put_bits(&pbc, 1, !interlace); /* no interlace */
901  put_bits(&pbc, 1, !packet_seq); /* no multiple seq */
902  put_bits(&pbc, 1, !packet_entry); /* no multiple entry */
903  put_bits(&pbc, 1, !slices); /* no slice code */
904  put_bits(&pbc, 1, 0); /* no bframe */
905  put_bits(&pbc, 1, 0); /* reserved */
906 
907  /* framerate */
908  if (track->st->avg_frame_rate.num > 0 && track->st->avg_frame_rate.den > 0)
909  put_bits32(&pbc, track->st->avg_frame_rate.num / track->st->avg_frame_rate.den);
910  else
911  put_bits32(&pbc, 0xffffffff);
912 
913  flush_put_bits(&pbc);
914 
915  av_free(unescaped);
916 
917  return 0;
918 }
919 
920 static int mov_write_dvc1_tag(AVIOContext *pb, MOVTrack *track)
921 {
922  uint8_t buf[7] = { 0 };
923  int ret;
924 
925  if ((ret = mov_write_dvc1_structs(track, buf)) < 0)
926  return ret;
927 
928  avio_wb32(pb, track->vos_len + 8 + sizeof(buf));
929  ffio_wfourcc(pb, "dvc1");
930  avio_write(pb, buf, sizeof(buf));
931  avio_write(pb, track->vos_data, track->vos_len);
932 
933  return 0;
934 }
935 
936 static int mov_write_glbl_tag(AVIOContext *pb, MOVTrack *track)
937 {
938  avio_wb32(pb, track->vos_len + 8);
939  ffio_wfourcc(pb, "glbl");
940  avio_write(pb, track->vos_data, track->vos_len);
941  return 8 + track->vos_len;
942 }
943 
944 /**
945  * Compute flags for 'lpcm' tag.
946  * See CoreAudioTypes and AudioStreamBasicDescription at Apple.
947  */
949 {
950  switch (codec_id) {
953  return 11;
956  return 9;
957  case AV_CODEC_ID_PCM_U8:
958  return 10;
962  return 14;
963  case AV_CODEC_ID_PCM_S8:
967  return 12;
968  default:
969  return 0;
970  }
971 }
972 
973 static int get_cluster_duration(MOVTrack *track, int cluster_idx)
974 {
975  int64_t next_dts;
976 
977  if (cluster_idx >= track->entry)
978  return 0;
979 
980  if (cluster_idx + 1 == track->entry)
981  next_dts = track->track_duration + track->start_dts;
982  else
983  next_dts = track->cluster[cluster_idx + 1].dts;
984 
985  next_dts -= track->cluster[cluster_idx].dts;
986 
987  av_assert0(next_dts >= 0);
988  av_assert0(next_dts <= INT_MAX);
989 
990  return next_dts;
991 }
992 
994 {
995  int i, first_duration;
996 
997 // return track->par->frame_size;
998 
999  /* use 1 for raw PCM */
1000  if (!track->audio_vbr)
1001  return 1;
1002 
1003  /* check to see if duration is constant for all clusters */
1004  if (!track->entry)
1005  return 0;
1006  first_duration = get_cluster_duration(track, 0);
1007  for (i = 1; i < track->entry; i++) {
1008  if (get_cluster_duration(track, i) != first_duration)
1009  return 0;
1010  }
1011  return first_duration;
1012 }
1013 
1015 {
1016  int64_t pos = avio_tell(pb);
1017  int version = 0;
1018  uint32_t tag = track->tag;
1019 
1020  if (track->mode == MODE_MOV) {
1021  if (track->timescale > UINT16_MAX || !track->par->channels) {
1022  if (mov_get_lpcm_flags(track->par->codec_id))
1023  tag = AV_RL32("lpcm");
1024  version = 2;
1025  } else if (track->audio_vbr || mov_pcm_le_gt16(track->par->codec_id) ||
1026  mov_pcm_be_gt16(track->par->codec_id) ||
1027  track->par->codec_id == AV_CODEC_ID_ADPCM_MS ||
1028  track->par->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV ||
1029  track->par->codec_id == AV_CODEC_ID_QDM2) {
1030  version = 1;
1031  }
1032  }
1033 
1034  avio_wb32(pb, 0); /* size */
1035  if (mov->encryption_scheme != MOV_ENC_NONE) {
1036  ffio_wfourcc(pb, "enca");
1037  } else {
1038  avio_wl32(pb, tag); // store it byteswapped
1039  }
1040  avio_wb32(pb, 0); /* Reserved */
1041  avio_wb16(pb, 0); /* Reserved */
1042  avio_wb16(pb, 1); /* Data-reference index, XXX == 1 */
1043 
1044  /* SoundDescription */
1045  avio_wb16(pb, version); /* Version */
1046  avio_wb16(pb, 0); /* Revision level */
1047  avio_wb32(pb, 0); /* Reserved */
1048 
1049  if (version == 2) {
1050  avio_wb16(pb, 3);
1051  avio_wb16(pb, 16);
1052  avio_wb16(pb, 0xfffe);
1053  avio_wb16(pb, 0);
1054  avio_wb32(pb, 0x00010000);
1055  avio_wb32(pb, 72);
1056  avio_wb64(pb, av_double2int(track->par->sample_rate));
1057  avio_wb32(pb, track->par->channels);
1058  avio_wb32(pb, 0x7F000000);
1060  avio_wb32(pb, mov_get_lpcm_flags(track->par->codec_id));
1061  avio_wb32(pb, track->sample_size);
1062  avio_wb32(pb, get_samples_per_packet(track));
1063  } else {
1064  if (track->mode == MODE_MOV) {
1065  avio_wb16(pb, track->par->channels);
1066  if (track->par->codec_id == AV_CODEC_ID_PCM_U8 ||
1067  track->par->codec_id == AV_CODEC_ID_PCM_S8)
1068  avio_wb16(pb, 8); /* bits per sample */
1069  else if (track->par->codec_id == AV_CODEC_ID_ADPCM_G726)
1070  avio_wb16(pb, track->par->bits_per_coded_sample);
1071  else
1072  avio_wb16(pb, 16);
1073  avio_wb16(pb, track->audio_vbr ? -2 : 0); /* compression ID */
1074  } else { /* reserved for mp4/3gp */
1075  if (track->par->codec_id == AV_CODEC_ID_FLAC ||
1076  track->par->codec_id == AV_CODEC_ID_OPUS) {
1077  avio_wb16(pb, track->par->channels);
1078  } else {
1079  avio_wb16(pb, 2);
1080  }
1081  if (track->par->codec_id == AV_CODEC_ID_FLAC) {
1082  avio_wb16(pb, track->par->bits_per_raw_sample);
1083  } else {
1084  avio_wb16(pb, 16);
1085  }
1086  avio_wb16(pb, 0);
1087  }
1088 
1089  avio_wb16(pb, 0); /* packet size (= 0) */
1090  if (track->par->codec_id == AV_CODEC_ID_OPUS)
1091  avio_wb16(pb, 48000);
1092  else
1093  avio_wb16(pb, track->par->sample_rate <= UINT16_MAX ?
1094  track->par->sample_rate : 0);
1095  avio_wb16(pb, 0); /* Reserved */
1096  }
1097 
1098  if (version == 1) { /* SoundDescription V1 extended info */
1099  if (mov_pcm_le_gt16(track->par->codec_id) ||
1100  mov_pcm_be_gt16(track->par->codec_id))
1101  avio_wb32(pb, 1); /* must be 1 for uncompressed formats */
1102  else
1103  avio_wb32(pb, track->par->frame_size); /* Samples per packet */
1104  avio_wb32(pb, track->sample_size / track->par->channels); /* Bytes per packet */
1105  avio_wb32(pb, track->sample_size); /* Bytes per frame */
1106  avio_wb32(pb, 2); /* Bytes per sample */
1107  }
1108 
1109  if (track->mode == MODE_MOV &&
1110  (track->par->codec_id == AV_CODEC_ID_AAC ||
1111  track->par->codec_id == AV_CODEC_ID_AC3 ||
1112  track->par->codec_id == AV_CODEC_ID_EAC3 ||
1113  track->par->codec_id == AV_CODEC_ID_AMR_NB ||
1114  track->par->codec_id == AV_CODEC_ID_ALAC ||
1115  track->par->codec_id == AV_CODEC_ID_ADPCM_MS ||
1116  track->par->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV ||
1117  track->par->codec_id == AV_CODEC_ID_QDM2 ||
1118  (mov_pcm_le_gt16(track->par->codec_id) && version==1) ||
1119  (mov_pcm_be_gt16(track->par->codec_id) && version==1)))
1120  mov_write_wave_tag(s, pb, track);
1121  else if (track->tag == MKTAG('m','p','4','a'))
1122  mov_write_esds_tag(pb, track);
1123  else if (track->par->codec_id == AV_CODEC_ID_AMR_NB)
1124  mov_write_amr_tag(pb, track);
1125  else if (track->par->codec_id == AV_CODEC_ID_AC3)
1126  mov_write_ac3_tag(pb, track);
1127  else if (track->par->codec_id == AV_CODEC_ID_EAC3)
1128  mov_write_eac3_tag(pb, track);
1129  else if (track->par->codec_id == AV_CODEC_ID_ALAC)
1130  mov_write_extradata_tag(pb, track);
1131  else if (track->par->codec_id == AV_CODEC_ID_WMAPRO)
1132  mov_write_wfex_tag(s, pb, track);
1133  else if (track->par->codec_id == AV_CODEC_ID_FLAC)
1134  mov_write_dfla_tag(pb, track);
1135  else if (track->par->codec_id == AV_CODEC_ID_OPUS)
1136  mov_write_dops_tag(pb, track);
1137  else if (track->vos_len > 0)
1138  mov_write_glbl_tag(pb, track);
1139 
1140  if (track->mode == MODE_MOV && track->par->codec_type == AVMEDIA_TYPE_AUDIO)
1141  mov_write_chan_tag(s, pb, track);
1142 
1143  if (mov->encryption_scheme != MOV_ENC_NONE) {
1144  ff_mov_cenc_write_sinf_tag(track, pb, mov->encryption_kid);
1145  }
1146 
1147  return update_size(pb, pos);
1148 }
1149 
1151 {
1152  avio_wb32(pb, 0xf); /* size */
1153  ffio_wfourcc(pb, "d263");
1154  ffio_wfourcc(pb, "FFMP");
1155  avio_w8(pb, 0); /* decoder version */
1156  /* FIXME use AVCodecContext level/profile, when encoder will set values */
1157  avio_w8(pb, 0xa); /* level */
1158  avio_w8(pb, 0); /* profile */
1159  return 0xf;
1160 }
1161 
1162 static int mov_write_av1c_tag(AVIOContext *pb, MOVTrack *track)
1163 {
1164  int64_t pos = avio_tell(pb);
1165 
1166  avio_wb32(pb, 0);
1167  ffio_wfourcc(pb, "av1C");
1168  ff_isom_write_av1c(pb, track->vos_data, track->vos_len);
1169  return update_size(pb, pos);
1170 }
1171 
1172 static int mov_write_avcc_tag(AVIOContext *pb, MOVTrack *track)
1173 {
1174  int64_t pos = avio_tell(pb);
1175 
1176  avio_wb32(pb, 0);
1177  ffio_wfourcc(pb, "avcC");
1178  ff_isom_write_avcc(pb, track->vos_data, track->vos_len);
1179  return update_size(pb, pos);
1180 }
1181 
1183 {
1184  int64_t pos = avio_tell(pb);
1185 
1186  avio_wb32(pb, 0);
1187  ffio_wfourcc(pb, "vpcC");
1188  avio_w8(pb, 1); /* version */
1189  avio_wb24(pb, 0); /* flags */
1190  ff_isom_write_vpcc(s, pb, track->par);
1191  return update_size(pb, pos);
1192 }
1193 
1194 static int mov_write_hvcc_tag(AVIOContext *pb, MOVTrack *track)
1195 {
1196  int64_t pos = avio_tell(pb);
1197 
1198  avio_wb32(pb, 0);
1199  ffio_wfourcc(pb, "hvcC");
1200  if (track->tag == MKTAG('h','v','c','1'))
1201  ff_isom_write_hvcc(pb, track->vos_data, track->vos_len, 1);
1202  else
1203  ff_isom_write_hvcc(pb, track->vos_data, track->vos_len, 0);
1204  return update_size(pb, pos);
1205 }
1206 
1207 /* also used by all avid codecs (dv, imx, meridien) and their variants */
1208 static int mov_write_avid_tag(AVIOContext *pb, MOVTrack *track)
1209 {
1210  int i;
1211  int interlaced;
1212  int cid;
1213  int display_width = track->par->width;
1214 
1215  if (track->vos_data && track->vos_len > 0x29) {
1216  if (ff_dnxhd_parse_header_prefix(track->vos_data) != 0) {
1217  /* looks like a DNxHD bit stream */
1218  interlaced = (track->vos_data[5] & 2);
1219  cid = AV_RB32(track->vos_data + 0x28);
1220  } else {
1221  av_log(NULL, AV_LOG_WARNING, "Could not locate DNxHD bit stream in vos_data\n");
1222  return 0;
1223  }
1224  } else {
1225  av_log(NULL, AV_LOG_WARNING, "Could not locate DNxHD bit stream, vos_data too small\n");
1226  return 0;
1227  }
1228 
1229  avio_wb32(pb, 24); /* size */
1230  ffio_wfourcc(pb, "ACLR");
1231  ffio_wfourcc(pb, "ACLR");
1232  ffio_wfourcc(pb, "0001");
1233  if (track->par->color_range == AVCOL_RANGE_MPEG || /* Legal range (16-235) */
1235  avio_wb32(pb, 1); /* Corresponds to 709 in official encoder */
1236  } else { /* Full range (0-255) */
1237  avio_wb32(pb, 2); /* Corresponds to RGB in official encoder */
1238  }
1239  avio_wb32(pb, 0); /* unknown */
1240 
1241  if (track->tag == MKTAG('A','V','d','h')) {
1242  avio_wb32(pb, 32);
1243  ffio_wfourcc(pb, "ADHR");
1244  ffio_wfourcc(pb, "0001");
1245  avio_wb32(pb, cid);
1246  avio_wb32(pb, 0); /* unknown */
1247  avio_wb32(pb, 1); /* unknown */
1248  avio_wb32(pb, 0); /* unknown */
1249  avio_wb32(pb, 0); /* unknown */
1250  return 0;
1251  }
1252 
1253  avio_wb32(pb, 24); /* size */
1254  ffio_wfourcc(pb, "APRG");
1255  ffio_wfourcc(pb, "APRG");
1256  ffio_wfourcc(pb, "0001");
1257  avio_wb32(pb, 1); /* unknown */
1258  avio_wb32(pb, 0); /* unknown */
1259 
1260  avio_wb32(pb, 120); /* size */
1261  ffio_wfourcc(pb, "ARES");
1262  ffio_wfourcc(pb, "ARES");
1263  ffio_wfourcc(pb, "0001");
1264  avio_wb32(pb, cid); /* dnxhd cid, some id ? */
1265  if ( track->par->sample_aspect_ratio.num > 0
1266  && track->par->sample_aspect_ratio.den > 0)
1267  display_width = display_width * track->par->sample_aspect_ratio.num / track->par->sample_aspect_ratio.den;
1268  avio_wb32(pb, display_width);
1269  /* values below are based on samples created with quicktime and avid codecs */
1270  if (interlaced) {
1271  avio_wb32(pb, track->par->height / 2);
1272  avio_wb32(pb, 2); /* unknown */
1273  avio_wb32(pb, 0); /* unknown */
1274  avio_wb32(pb, 4); /* unknown */
1275  } else {
1276  avio_wb32(pb, track->par->height);
1277  avio_wb32(pb, 1); /* unknown */
1278  avio_wb32(pb, 0); /* unknown */
1279  if (track->par->height == 1080)
1280  avio_wb32(pb, 5); /* unknown */
1281  else
1282  avio_wb32(pb, 6); /* unknown */
1283  }
1284  /* padding */
1285  for (i = 0; i < 10; i++)
1286  avio_wb64(pb, 0);
1287 
1288  return 0;
1289 }
1290 
1291 static int mov_write_dpxe_tag(AVIOContext *pb, MOVTrack *track)
1292 {
1293  avio_wb32(pb, 12);
1294  ffio_wfourcc(pb, "DpxE");
1295  if (track->par->extradata_size >= 12 &&
1296  !memcmp(&track->par->extradata[4], "DpxE", 4)) {
1297  avio_wb32(pb, track->par->extradata[11]);
1298  } else {
1299  avio_wb32(pb, 1);
1300  }
1301  return 0;
1302 }
1303 
1305 {
1306  int tag;
1307 
1308  if (track->par->width == 720) { /* SD */
1309  if (track->par->height == 480) { /* NTSC */
1310  if (track->par->format == AV_PIX_FMT_YUV422P) tag = MKTAG('d','v','5','n');
1311  else tag = MKTAG('d','v','c',' ');
1312  }else if (track->par->format == AV_PIX_FMT_YUV422P) tag = MKTAG('d','v','5','p');
1313  else if (track->par->format == AV_PIX_FMT_YUV420P) tag = MKTAG('d','v','c','p');
1314  else tag = MKTAG('d','v','p','p');
1315  } else if (track->par->height == 720) { /* HD 720 line */
1316  if (track->st->time_base.den == 50) tag = MKTAG('d','v','h','q');
1317  else tag = MKTAG('d','v','h','p');
1318  } else if (track->par->height == 1080) { /* HD 1080 line */
1319  if (track->st->time_base.den == 25) tag = MKTAG('d','v','h','5');
1320  else tag = MKTAG('d','v','h','6');
1321  } else {
1322  av_log(s, AV_LOG_ERROR, "unsupported height for dv codec\n");
1323  return 0;
1324  }
1325 
1326  return tag;
1327 }
1328 
1330 {
1331  AVRational rate = st->avg_frame_rate;
1332 
1333 #if FF_API_LAVF_AVCTX
1335  rate = av_inv_q(st->codec->time_base);
1336  if (av_timecode_check_frame_rate(rate) < 0) {
1337  av_log(s, AV_LOG_DEBUG, "timecode: tbc=%d/%d invalid, fallback on %d/%d\n",
1338  rate.num, rate.den, st->avg_frame_rate.num, st->avg_frame_rate.den);
1339  rate = st->avg_frame_rate;
1340  }
1342 #endif
1343 
1344  return rate;
1345 }
1346 
1348 {
1349  AVRational rational_framerate = find_fps(s, st);
1350  int rate = 0;
1351  if (rational_framerate.den != 0)
1352  rate = av_q2d(rational_framerate);
1353  return rate;
1354 }
1355 
1357 {
1358  int tag = track->par->codec_tag;
1360  AVStream *st = track->st;
1361  int rate = defined_frame_rate(s, st);
1362 
1363  if (!tag)
1364  tag = MKTAG('m', '2', 'v', '1'); //fallback tag
1365 
1366  if (track->par->format == AV_PIX_FMT_YUV420P) {
1367  if (track->par->width == 1280 && track->par->height == 720) {
1368  if (!interlaced) {
1369  if (rate == 24) tag = MKTAG('x','d','v','4');
1370  else if (rate == 25) tag = MKTAG('x','d','v','5');
1371  else if (rate == 30) tag = MKTAG('x','d','v','1');
1372  else if (rate == 50) tag = MKTAG('x','d','v','a');
1373  else if (rate == 60) tag = MKTAG('x','d','v','9');
1374  }
1375  } else if (track->par->width == 1440 && track->par->height == 1080) {
1376  if (!interlaced) {
1377  if (rate == 24) tag = MKTAG('x','d','v','6');
1378  else if (rate == 25) tag = MKTAG('x','d','v','7');
1379  else if (rate == 30) tag = MKTAG('x','d','v','8');
1380  } else {
1381  if (rate == 25) tag = MKTAG('x','d','v','3');
1382  else if (rate == 30) tag = MKTAG('x','d','v','2');
1383  }
1384  } else if (track->par->width == 1920 && track->par->height == 1080) {
1385  if (!interlaced) {
1386  if (rate == 24) tag = MKTAG('x','d','v','d');
1387  else if (rate == 25) tag = MKTAG('x','d','v','e');
1388  else if (rate == 30) tag = MKTAG('x','d','v','f');
1389  } else {
1390  if (rate == 25) tag = MKTAG('x','d','v','c');
1391  else if (rate == 30) tag = MKTAG('x','d','v','b');
1392  }
1393  }
1394  } else if (track->par->format == AV_PIX_FMT_YUV422P) {
1395  if (track->par->width == 1280 && track->par->height == 720) {
1396  if (!interlaced) {
1397  if (rate == 24) tag = MKTAG('x','d','5','4');
1398  else if (rate == 25) tag = MKTAG('x','d','5','5');
1399  else if (rate == 30) tag = MKTAG('x','d','5','1');
1400  else if (rate == 50) tag = MKTAG('x','d','5','a');
1401  else if (rate == 60) tag = MKTAG('x','d','5','9');
1402  }
1403  } else if (track->par->width == 1920 && track->par->height == 1080) {
1404  if (!interlaced) {
1405  if (rate == 24) tag = MKTAG('x','d','5','d');
1406  else if (rate == 25) tag = MKTAG('x','d','5','e');
1407  else if (rate == 30) tag = MKTAG('x','d','5','f');
1408  } else {
1409  if (rate == 25) tag = MKTAG('x','d','5','c');
1410  else if (rate == 30) tag = MKTAG('x','d','5','b');
1411  }
1412  }
1413  }
1414 
1415  return tag;
1416 }
1417 
1419 {
1420  int tag = track->par->codec_tag;
1422  AVStream *st = track->st;
1423  int rate = defined_frame_rate(s, st);
1424 
1425  if (!tag)
1426  tag = MKTAG('a', 'v', 'c', 'i'); //fallback tag
1427 
1428  if (track->par->format == AV_PIX_FMT_YUV420P10) {
1429  if (track->par->width == 960 && track->par->height == 720) {
1430  if (!interlaced) {
1431  if (rate == 24) tag = MKTAG('a','i','5','p');
1432  else if (rate == 25) tag = MKTAG('a','i','5','q');
1433  else if (rate == 30) tag = MKTAG('a','i','5','p');
1434  else if (rate == 50) tag = MKTAG('a','i','5','q');
1435  else if (rate == 60) tag = MKTAG('a','i','5','p');
1436  }
1437  } else if (track->par->width == 1440 && track->par->height == 1080) {
1438  if (!interlaced) {
1439  if (rate == 24) tag = MKTAG('a','i','5','3');
1440  else if (rate == 25) tag = MKTAG('a','i','5','2');
1441  else if (rate == 30) tag = MKTAG('a','i','5','3');
1442  } else {
1443  if (rate == 50) tag = MKTAG('a','i','5','5');
1444  else if (rate == 60) tag = MKTAG('a','i','5','6');
1445  }
1446  }
1447  } else if (track->par->format == AV_PIX_FMT_YUV422P10) {
1448  if (track->par->width == 1280 && track->par->height == 720) {
1449  if (!interlaced) {
1450  if (rate == 24) tag = MKTAG('a','i','1','p');
1451  else if (rate == 25) tag = MKTAG('a','i','1','q');
1452  else if (rate == 30) tag = MKTAG('a','i','1','p');
1453  else if (rate == 50) tag = MKTAG('a','i','1','q');
1454  else if (rate == 60) tag = MKTAG('a','i','1','p');
1455  }
1456  } else if (track->par->width == 1920 && track->par->height == 1080) {
1457  if (!interlaced) {
1458  if (rate == 24) tag = MKTAG('a','i','1','3');
1459  else if (rate == 25) tag = MKTAG('a','i','1','2');
1460  else if (rate == 30) tag = MKTAG('a','i','1','3');
1461  } else {
1462  if (rate == 25) tag = MKTAG('a','i','1','5');
1463  else if (rate == 50) tag = MKTAG('a','i','1','5');
1464  else if (rate == 60) tag = MKTAG('a','i','1','6');
1465  }
1466  } else if ( track->par->width == 4096 && track->par->height == 2160
1467  || track->par->width == 3840 && track->par->height == 2160
1468  || track->par->width == 2048 && track->par->height == 1080) {
1469  tag = MKTAG('a','i','v','x');
1470  }
1471  }
1472 
1473  return tag;
1474 }
1475 
1476 static const struct {
1478  uint32_t tag;
1479  unsigned bps;
1480 } mov_pix_fmt_tags[] = {
1481  { AV_PIX_FMT_YUYV422, MKTAG('y','u','v','2'), 0 },
1482  { AV_PIX_FMT_YUYV422, MKTAG('y','u','v','s'), 0 },
1483  { AV_PIX_FMT_UYVY422, MKTAG('2','v','u','y'), 0 },
1484  { AV_PIX_FMT_RGB555BE,MKTAG('r','a','w',' '), 16 },
1485  { AV_PIX_FMT_RGB555LE,MKTAG('L','5','5','5'), 16 },
1486  { AV_PIX_FMT_RGB565LE,MKTAG('L','5','6','5'), 16 },
1487  { AV_PIX_FMT_RGB565BE,MKTAG('B','5','6','5'), 16 },
1488  { AV_PIX_FMT_GRAY16BE,MKTAG('b','1','6','g'), 16 },
1489  { AV_PIX_FMT_RGB24, MKTAG('r','a','w',' '), 24 },
1490  { AV_PIX_FMT_BGR24, MKTAG('2','4','B','G'), 24 },
1491  { AV_PIX_FMT_ARGB, MKTAG('r','a','w',' '), 32 },
1492  { AV_PIX_FMT_BGRA, MKTAG('B','G','R','A'), 32 },
1493  { AV_PIX_FMT_RGBA, MKTAG('R','G','B','A'), 32 },
1494  { AV_PIX_FMT_ABGR, MKTAG('A','B','G','R'), 32 },
1495  { AV_PIX_FMT_RGB48BE, MKTAG('b','4','8','r'), 48 },
1496 };
1497 
1499 {
1500  int tag = MKTAG('A','V','d','n');
1501  if (track->par->profile != FF_PROFILE_UNKNOWN &&
1502  track->par->profile != FF_PROFILE_DNXHD)
1503  tag = MKTAG('A','V','d','h');
1504  return tag;
1505 }
1506 
1508 {
1509  int tag = track->par->codec_tag;
1510  int i;
1511  enum AVPixelFormat pix_fmt;
1512 
1513  for (i = 0; i < FF_ARRAY_ELEMS(mov_pix_fmt_tags); i++) {
1514  if (track->par->format == mov_pix_fmt_tags[i].pix_fmt) {
1515  tag = mov_pix_fmt_tags[i].tag;
1517  if (track->par->codec_tag == mov_pix_fmt_tags[i].tag)
1518  break;
1519  }
1520  }
1521 
1523  track->par->bits_per_coded_sample);
1524  if (tag == MKTAG('r','a','w',' ') &&
1525  track->par->format != pix_fmt &&
1526  track->par->format != AV_PIX_FMT_GRAY8 &&
1527  track->par->format != AV_PIX_FMT_NONE)
1528  av_log(s, AV_LOG_ERROR, "%s rawvideo cannot be written to mov, output file will be unreadable\n",
1529  av_get_pix_fmt_name(track->par->format));
1530  return tag;
1531 }
1532 
1533 static unsigned int mov_get_codec_tag(AVFormatContext *s, MOVTrack *track)
1534 {
1535  unsigned int tag = track->par->codec_tag;
1536 
1537  // "rtp " is used to distinguish internally created RTP-hint tracks
1538  // (with rtp_ctx) from other tracks.
1539  if (tag == MKTAG('r','t','p',' '))
1540  tag = 0;
1541  if (!tag || (s->strict_std_compliance >= FF_COMPLIANCE_NORMAL &&
1542  (track->par->codec_id == AV_CODEC_ID_DVVIDEO ||
1543  track->par->codec_id == AV_CODEC_ID_RAWVIDEO ||
1544  track->par->codec_id == AV_CODEC_ID_H263 ||
1545  track->par->codec_id == AV_CODEC_ID_H264 ||
1546  track->par->codec_id == AV_CODEC_ID_DNXHD ||
1547  track->par->codec_id == AV_CODEC_ID_MPEG2VIDEO ||
1548  av_get_bits_per_sample(track->par->codec_id)))) { // pcm audio
1549  if (track->par->codec_id == AV_CODEC_ID_DVVIDEO)
1550  tag = mov_get_dv_codec_tag(s, track);
1551  else if (track->par->codec_id == AV_CODEC_ID_RAWVIDEO)
1552  tag = mov_get_rawvideo_codec_tag(s, track);
1553  else if (track->par->codec_id == AV_CODEC_ID_MPEG2VIDEO)
1554  tag = mov_get_mpeg2_xdcam_codec_tag(s, track);
1555  else if (track->par->codec_id == AV_CODEC_ID_H264)
1556  tag = mov_get_h264_codec_tag(s, track);
1557  else if (track->par->codec_id == AV_CODEC_ID_DNXHD)
1558  tag = mov_get_dnxhd_codec_tag(s, track);
1559  else if (track->par->codec_type == AVMEDIA_TYPE_VIDEO) {
1561  if (!tag) { // if no mac fcc found, try with Microsoft tags
1563  if (tag)
1564  av_log(s, AV_LOG_WARNING, "Using MS style video codec tag, "
1565  "the file may be unplayable!\n");
1566  }
1567  } else if (track->par->codec_type == AVMEDIA_TYPE_AUDIO) {
1569  if (!tag) { // if no mac fcc found, try with Microsoft tags
1570  int ms_tag = ff_codec_get_tag(ff_codec_wav_tags, track->par->codec_id);
1571  if (ms_tag) {
1572  tag = MKTAG('m', 's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
1573  av_log(s, AV_LOG_WARNING, "Using MS style audio codec tag, "
1574  "the file may be unplayable!\n");
1575  }
1576  }
1577  } else if (track->par->codec_type == AVMEDIA_TYPE_SUBTITLE)
1579  }
1580 
1581  return tag;
1582 }
1583 
1585  { AV_CODEC_ID_MJPEG, 0xD },
1586  { AV_CODEC_ID_PNG, 0xE },
1587  { AV_CODEC_ID_BMP, 0x1B },
1588  { AV_CODEC_ID_NONE, 0 },
1589 };
1590 
1591 static unsigned int validate_codec_tag(const AVCodecTag *const *tags,
1592  unsigned int tag, int codec_id)
1593 {
1594  int i;
1595 
1596  /**
1597  * Check that tag + id is in the table
1598  */
1599  for (i = 0; tags && tags[i]; i++) {
1600  const AVCodecTag *codec_tags = tags[i];
1601  while (codec_tags->id != AV_CODEC_ID_NONE) {
1602  if (avpriv_toupper4(codec_tags->tag) == avpriv_toupper4(tag) &&
1603  codec_tags->id == codec_id)
1604  return codec_tags->tag;
1605  codec_tags++;
1606  }
1607  }
1608  return 0;
1609 }
1610 
1611 static unsigned int mov_find_codec_tag(AVFormatContext *s, MOVTrack *track)
1612 {
1613  if (is_cover_image(track->st))
1614  return ff_codec_get_tag(codec_cover_image_tags, track->par->codec_id);
1615 
1616  if (track->mode == MODE_IPOD)
1617  if (!av_match_ext(s->url, "m4a") &&
1618  !av_match_ext(s->url, "m4v") &&
1619  !av_match_ext(s->url, "m4b"))
1620  av_log(s, AV_LOG_WARNING, "Warning, extension is not .m4a nor .m4v "
1621  "Quicktime/Ipod might not play the file\n");
1622 
1623  if (track->mode == MODE_MOV) {
1624  return mov_get_codec_tag(s, track);
1625  } else
1626  return validate_codec_tag(s->oformat->codec_tag, track->par->codec_tag,
1627  track->par->codec_id);
1628 }
1629 
1630 /** Write uuid atom.
1631  * Needed to make file play in iPods running newest firmware
1632  * goes after avcC atom in moov.trak.mdia.minf.stbl.stsd.avc1
1633  */
1635 {
1636  avio_wb32(pb, 28);
1637  ffio_wfourcc(pb, "uuid");
1638  avio_wb32(pb, 0x6b6840f2);
1639  avio_wb32(pb, 0x5f244fc5);
1640  avio_wb32(pb, 0xba39a51b);
1641  avio_wb32(pb, 0xcf0323f3);
1642  avio_wb32(pb, 0x0);
1643  return 28;
1644 }
1645 
1646 static const uint16_t fiel_data[] = {
1647  0x0000, 0x0100, 0x0201, 0x0206, 0x0209, 0x020e
1648 };
1649 
1650 static int mov_write_fiel_tag(AVIOContext *pb, MOVTrack *track, int field_order)
1651 {
1652  unsigned mov_field_order = 0;
1653  if (field_order < FF_ARRAY_ELEMS(fiel_data))
1654  mov_field_order = fiel_data[field_order];
1655  else
1656  return 0;
1657  avio_wb32(pb, 10);
1658  ffio_wfourcc(pb, "fiel");
1659  avio_wb16(pb, mov_field_order);
1660  return 10;
1661 }
1662 
1664 {
1665  int64_t pos = avio_tell(pb);
1666  avio_wb32(pb, 0); /* size */
1667  avio_wl32(pb, track->tag); // store it byteswapped
1668  avio_wb32(pb, 0); /* Reserved */
1669  avio_wb16(pb, 0); /* Reserved */
1670  avio_wb16(pb, 1); /* Data-reference index */
1671 
1672  if (track->par->codec_id == AV_CODEC_ID_DVD_SUBTITLE)
1673  mov_write_esds_tag(pb, track);
1674  else if (track->par->extradata_size)
1675  avio_write(pb, track->par->extradata, track->par->extradata_size);
1676 
1677  return update_size(pb, pos);
1678 }
1679 
1680 static int mov_write_st3d_tag(AVIOContext *pb, AVStereo3D *stereo_3d)
1681 {
1682  int8_t stereo_mode;
1683 
1684  if (stereo_3d->flags != 0) {
1685  av_log(pb, AV_LOG_WARNING, "Unsupported stereo_3d flags %x. st3d not written.\n", stereo_3d->flags);
1686  return 0;
1687  }
1688 
1689  switch (stereo_3d->type) {
1690  case AV_STEREO3D_2D:
1691  stereo_mode = 0;
1692  break;
1693  case AV_STEREO3D_TOPBOTTOM:
1694  stereo_mode = 1;
1695  break;
1697  stereo_mode = 2;
1698  break;
1699  default:
1700  av_log(pb, AV_LOG_WARNING, "Unsupported stereo_3d type %s. st3d not written.\n", av_stereo3d_type_name(stereo_3d->type));
1701  return 0;
1702  }
1703  avio_wb32(pb, 13); /* size */
1704  ffio_wfourcc(pb, "st3d");
1705  avio_wb32(pb, 0); /* version = 0 & flags = 0 */
1706  avio_w8(pb, stereo_mode);
1707  return 13;
1708 }
1709 
1711 {
1712  int64_t sv3d_pos, svhd_pos, proj_pos;
1713  const char* metadata_source = s->flags & AVFMT_FLAG_BITEXACT ? "Lavf" : LIBAVFORMAT_IDENT;
1714 
1715  if (spherical_mapping->projection != AV_SPHERICAL_EQUIRECTANGULAR &&
1716  spherical_mapping->projection != AV_SPHERICAL_EQUIRECTANGULAR_TILE &&
1717  spherical_mapping->projection != AV_SPHERICAL_CUBEMAP) {
1718  av_log(pb, AV_LOG_WARNING, "Unsupported projection %d. sv3d not written.\n", spherical_mapping->projection);
1719  return 0;
1720  }
1721 
1722  sv3d_pos = avio_tell(pb);
1723  avio_wb32(pb, 0); /* size */
1724  ffio_wfourcc(pb, "sv3d");
1725 
1726  svhd_pos = avio_tell(pb);
1727  avio_wb32(pb, 0); /* size */
1728  ffio_wfourcc(pb, "svhd");
1729  avio_wb32(pb, 0); /* version = 0 & flags = 0 */
1730  avio_put_str(pb, metadata_source);
1731  update_size(pb, svhd_pos);
1732 
1733  proj_pos = avio_tell(pb);
1734  avio_wb32(pb, 0); /* size */
1735  ffio_wfourcc(pb, "proj");
1736 
1737  avio_wb32(pb, 24); /* size */
1738  ffio_wfourcc(pb, "prhd");
1739  avio_wb32(pb, 0); /* version = 0 & flags = 0 */
1740  avio_wb32(pb, spherical_mapping->yaw);
1741  avio_wb32(pb, spherical_mapping->pitch);
1742  avio_wb32(pb, spherical_mapping->roll);
1743 
1744  switch (spherical_mapping->projection) {
1747  avio_wb32(pb, 28); /* size */
1748  ffio_wfourcc(pb, "equi");
1749  avio_wb32(pb, 0); /* version = 0 & flags = 0 */
1750  avio_wb32(pb, spherical_mapping->bound_top);
1751  avio_wb32(pb, spherical_mapping->bound_bottom);
1752  avio_wb32(pb, spherical_mapping->bound_left);
1753  avio_wb32(pb, spherical_mapping->bound_right);
1754  break;
1755  case AV_SPHERICAL_CUBEMAP:
1756  avio_wb32(pb, 20); /* size */
1757  ffio_wfourcc(pb, "cbmp");
1758  avio_wb32(pb, 0); /* version = 0 & flags = 0 */
1759  avio_wb32(pb, 0); /* layout */
1760  avio_wb32(pb, spherical_mapping->padding); /* padding */
1761  break;
1762  }
1763  update_size(pb, proj_pos);
1764 
1765  return update_size(pb, sv3d_pos);
1766 }
1767 
1768 static int mov_write_clap_tag(AVIOContext *pb, MOVTrack *track)
1769 {
1770  avio_wb32(pb, 40);
1771  ffio_wfourcc(pb, "clap");
1772  avio_wb32(pb, track->par->width); /* apertureWidth_N */
1773  avio_wb32(pb, 1); /* apertureWidth_D (= 1) */
1774  avio_wb32(pb, track->height); /* apertureHeight_N */
1775  avio_wb32(pb, 1); /* apertureHeight_D (= 1) */
1776  avio_wb32(pb, 0); /* horizOff_N (= 0) */
1777  avio_wb32(pb, 1); /* horizOff_D (= 1) */
1778  avio_wb32(pb, 0); /* vertOff_N (= 0) */
1779  avio_wb32(pb, 1); /* vertOff_D (= 1) */
1780  return 40;
1781 }
1782 
1783 static int mov_write_pasp_tag(AVIOContext *pb, MOVTrack *track)
1784 {
1785  AVRational sar;
1786  av_reduce(&sar.num, &sar.den, track->par->sample_aspect_ratio.num,
1787  track->par->sample_aspect_ratio.den, INT_MAX);
1788 
1789  avio_wb32(pb, 16);
1790  ffio_wfourcc(pb, "pasp");
1791  avio_wb32(pb, sar.num);
1792  avio_wb32(pb, sar.den);
1793  return 16;
1794 }
1795 
1796 static int mov_write_gama_tag(AVIOContext *pb, MOVTrack *track, double gamma)
1797 {
1798  uint32_t gama = 0;
1799  if (gamma <= 0.0)
1800  {
1801  gamma = avpriv_get_gamma_from_trc(track->par->color_trc);
1802  }
1803  av_log(pb, AV_LOG_DEBUG, "gamma value %g\n", gamma);
1804 
1805  if (gamma > 1e-6) {
1806  gama = (uint32_t)lrint((double)(1<<16) * gamma);
1807  av_log(pb, AV_LOG_DEBUG, "writing gama value %"PRId32"\n", gama);
1808 
1809  av_assert0(track->mode == MODE_MOV);
1810  avio_wb32(pb, 12);
1811  ffio_wfourcc(pb, "gama");
1812  avio_wb32(pb, gama);
1813  return 12;
1814  }
1815  else {
1816  av_log(pb, AV_LOG_WARNING, "gamma value unknown, unable to write gama atom\n");
1817  }
1818  return 0;
1819 }
1820 
1821 static int mov_write_colr_tag(AVIOContext *pb, MOVTrack *track)
1822 {
1823  // Ref (MOV): https://developer.apple.com/library/mac/technotes/tn2162/_index.html#//apple_ref/doc/uid/DTS40013070-CH1-TNTAG9
1824  // Ref (MP4): ISO/IEC 14496-12:2012
1825 
1826  if (track->par->color_primaries == AVCOL_PRI_UNSPECIFIED &&
1827  track->par->color_trc == AVCOL_TRC_UNSPECIFIED &&
1828  track->par->color_space == AVCOL_SPC_UNSPECIFIED) {
1829  if ((track->par->width >= 1920 && track->par->height >= 1080)
1830  || (track->par->width == 1280 && track->par->height == 720)) {
1831  av_log(NULL, AV_LOG_WARNING, "color primaries unspecified, assuming bt709\n");
1833  } else if (track->par->width == 720 && track->height == 576) {
1834  av_log(NULL, AV_LOG_WARNING, "color primaries unspecified, assuming bt470bg\n");
1836  } else if (track->par->width == 720 &&
1837  (track->height == 486 || track->height == 480)) {
1838  av_log(NULL, AV_LOG_WARNING, "color primaries unspecified, assuming smpte170\n");
1840  } else {
1841  av_log(NULL, AV_LOG_WARNING, "color primaries unspecified, unable to assume anything\n");
1842  }
1843  switch (track->par->color_primaries) {
1844  case AVCOL_PRI_BT709:
1845  track->par->color_trc = AVCOL_TRC_BT709;
1846  track->par->color_space = AVCOL_SPC_BT709;
1847  break;
1848  case AVCOL_PRI_SMPTE170M:
1849  case AVCOL_PRI_BT470BG:
1850  track->par->color_trc = AVCOL_TRC_BT709;
1852  break;
1853  }
1854  }
1855 
1856  /* We should only ever be called by MOV or MP4. */
1857  av_assert0(track->mode == MODE_MOV || track->mode == MODE_MP4);
1858 
1859  avio_wb32(pb, 18 + (track->mode == MODE_MP4));
1860  ffio_wfourcc(pb, "colr");
1861  if (track->mode == MODE_MP4)
1862  ffio_wfourcc(pb, "nclx");
1863  else
1864  ffio_wfourcc(pb, "nclc");
1865  switch (track->par->color_primaries) {
1866  case AVCOL_PRI_BT709: avio_wb16(pb, 1); break;
1867  case AVCOL_PRI_BT470BG: avio_wb16(pb, 5); break;
1868  case AVCOL_PRI_SMPTE170M:
1869  case AVCOL_PRI_SMPTE240M: avio_wb16(pb, 6); break;
1870  case AVCOL_PRI_BT2020: avio_wb16(pb, 9); break;
1871  case AVCOL_PRI_SMPTE431: avio_wb16(pb, 11); break;
1872  case AVCOL_PRI_SMPTE432: avio_wb16(pb, 12); break;
1873  default: avio_wb16(pb, 2);
1874  }
1875  switch (track->par->color_trc) {
1876  case AVCOL_TRC_BT709: avio_wb16(pb, 1); break;
1877  case AVCOL_TRC_SMPTE170M: avio_wb16(pb, 1); break; // remapped
1878  case AVCOL_TRC_SMPTE240M: avio_wb16(pb, 7); break;
1879  case AVCOL_TRC_SMPTEST2084: avio_wb16(pb, 16); break;
1880  case AVCOL_TRC_SMPTE428: avio_wb16(pb, 17); break;
1881  case AVCOL_TRC_ARIB_STD_B67: avio_wb16(pb, 18); break;
1882  default: avio_wb16(pb, 2);
1883  }
1884  switch (track->par->color_space) {
1885  case AVCOL_SPC_BT709: avio_wb16(pb, 1); break;
1886  case AVCOL_SPC_BT470BG:
1887  case AVCOL_SPC_SMPTE170M: avio_wb16(pb, 6); break;
1888  case AVCOL_SPC_SMPTE240M: avio_wb16(pb, 7); break;
1889  case AVCOL_SPC_BT2020_NCL: avio_wb16(pb, 9); break;
1890  default: avio_wb16(pb, 2);
1891  }
1892 
1893  if (track->mode == MODE_MP4) {
1894  int full_range = track->par->color_range == AVCOL_RANGE_JPEG;
1895  avio_w8(pb, full_range << 7);
1896  return 19;
1897  } else {
1898  return 18;
1899  }
1900 }
1901 
1902 static void find_compressor(char * compressor_name, int len, MOVTrack *track)
1903 {
1904  AVDictionaryEntry *encoder;
1905  int xdcam_res = (track->par->width == 1280 && track->par->height == 720)
1906  || (track->par->width == 1440 && track->par->height == 1080)
1907  || (track->par->width == 1920 && track->par->height == 1080);
1908 
1909  if (track->mode == MODE_MOV &&
1910  (encoder = av_dict_get(track->st->metadata, "encoder", NULL, 0))) {
1911  av_strlcpy(compressor_name, encoder->value, 32);
1912  } else if (track->par->codec_id == AV_CODEC_ID_MPEG2VIDEO && xdcam_res) {
1914  AVStream *st = track->st;
1915  int rate = defined_frame_rate(NULL, st);
1916  av_strlcatf(compressor_name, len, "XDCAM");
1917  if (track->par->format == AV_PIX_FMT_YUV422P) {
1918  av_strlcatf(compressor_name, len, " HD422");
1919  } else if(track->par->width == 1440) {
1920  av_strlcatf(compressor_name, len, " HD");
1921  } else
1922  av_strlcatf(compressor_name, len, " EX");
1923 
1924  av_strlcatf(compressor_name, len, " %d%c", track->par->height, interlaced ? 'i' : 'p');
1925 
1926  av_strlcatf(compressor_name, len, "%d", rate * (interlaced + 1));
1927  }
1928 }
1929 
1931 {
1932  int64_t pos = avio_tell(pb);
1933  char compressor_name[32] = { 0 };
1934  int avid = 0;
1935 
1936  int uncompressed_ycbcr = ((track->par->codec_id == AV_CODEC_ID_RAWVIDEO && track->par->format == AV_PIX_FMT_UYVY422)
1937  || (track->par->codec_id == AV_CODEC_ID_RAWVIDEO && track->par->format == AV_PIX_FMT_YUYV422)
1938  || track->par->codec_id == AV_CODEC_ID_V308
1939  || track->par->codec_id == AV_CODEC_ID_V408
1940  || track->par->codec_id == AV_CODEC_ID_V410
1941  || track->par->codec_id == AV_CODEC_ID_V210);
1942 
1943  avio_wb32(pb, 0); /* size */
1944  if (mov->encryption_scheme != MOV_ENC_NONE) {
1945  ffio_wfourcc(pb, "encv");
1946  } else {
1947  avio_wl32(pb, track->tag); // store it byteswapped
1948  }
1949  avio_wb32(pb, 0); /* Reserved */
1950  avio_wb16(pb, 0); /* Reserved */
1951  avio_wb16(pb, 1); /* Data-reference index */
1952 
1953  if (uncompressed_ycbcr) {
1954  avio_wb16(pb, 2); /* Codec stream version */
1955  } else {
1956  avio_wb16(pb, 0); /* Codec stream version */
1957  }
1958  avio_wb16(pb, 0); /* Codec stream revision (=0) */
1959  if (track->mode == MODE_MOV) {
1960  ffio_wfourcc(pb, "FFMP"); /* Vendor */
1961  if (track->par->codec_id == AV_CODEC_ID_RAWVIDEO || uncompressed_ycbcr) {
1962  avio_wb32(pb, 0); /* Temporal Quality */
1963  avio_wb32(pb, 0x400); /* Spatial Quality = lossless*/
1964  } else {
1965  avio_wb32(pb, 0x200); /* Temporal Quality = normal */
1966  avio_wb32(pb, 0x200); /* Spatial Quality = normal */
1967  }
1968  } else {
1969  avio_wb32(pb, 0); /* Reserved */
1970  avio_wb32(pb, 0); /* Reserved */
1971  avio_wb32(pb, 0); /* Reserved */
1972  }
1973  avio_wb16(pb, track->par->width); /* Video width */
1974  avio_wb16(pb, track->height); /* Video height */
1975  avio_wb32(pb, 0x00480000); /* Horizontal resolution 72dpi */
1976  avio_wb32(pb, 0x00480000); /* Vertical resolution 72dpi */
1977  avio_wb32(pb, 0); /* Data size (= 0) */
1978  avio_wb16(pb, 1); /* Frame count (= 1) */
1979 
1980  /* FIXME not sure, ISO 14496-1 draft where it shall be set to 0 */
1981  find_compressor(compressor_name, 32, track);
1982  avio_w8(pb, strlen(compressor_name));
1983  avio_write(pb, compressor_name, 31);
1984 
1985  if (track->mode == MODE_MOV &&
1986  (track->par->codec_id == AV_CODEC_ID_V410 || track->par->codec_id == AV_CODEC_ID_V210))
1987  avio_wb16(pb, 0x18);
1988  else if (track->mode == MODE_MOV && track->par->bits_per_coded_sample)
1989  avio_wb16(pb, track->par->bits_per_coded_sample |
1990  (track->par->format == AV_PIX_FMT_GRAY8 ? 0x20 : 0));
1991  else
1992  avio_wb16(pb, 0x18); /* Reserved */
1993 
1994  if (track->mode == MODE_MOV && track->par->format == AV_PIX_FMT_PAL8) {
1995  int pal_size, i;
1996  avio_wb16(pb, 0); /* Color table ID */
1997  avio_wb32(pb, 0); /* Color table seed */
1998  avio_wb16(pb, 0x8000); /* Color table flags */
1999  if (track->par->bits_per_coded_sample < 0 || track->par->bits_per_coded_sample > 8)
2000  return AVERROR(EINVAL);
2001  pal_size = 1 << track->par->bits_per_coded_sample;
2002  avio_wb16(pb, pal_size - 1); /* Color table size (zero-relative) */
2003  for (i = 0; i < pal_size; i++) {
2004  uint32_t rgb = track->palette[i];
2005  uint16_t r = (rgb >> 16) & 0xff;
2006  uint16_t g = (rgb >> 8) & 0xff;
2007  uint16_t b = rgb & 0xff;
2008  avio_wb16(pb, 0);
2009  avio_wb16(pb, (r << 8) | r);
2010  avio_wb16(pb, (g << 8) | g);
2011  avio_wb16(pb, (b << 8) | b);
2012  }
2013  } else
2014  avio_wb16(pb, 0xffff); /* Reserved */
2015 
2016  if (track->tag == MKTAG('m','p','4','v'))
2017  mov_write_esds_tag(pb, track);
2018  else if (track->par->codec_id == AV_CODEC_ID_H263)
2019  mov_write_d263_tag(pb);
2020  else if (track->par->codec_id == AV_CODEC_ID_AVUI ||
2021  track->par->codec_id == AV_CODEC_ID_SVQ3) {
2022  mov_write_extradata_tag(pb, track);
2023  avio_wb32(pb, 0);
2024  } else if (track->par->codec_id == AV_CODEC_ID_DNXHD) {
2025  mov_write_avid_tag(pb, track);
2026  avid = 1;
2027  } else if (track->par->codec_id == AV_CODEC_ID_HEVC)
2028  mov_write_hvcc_tag(pb, track);
2029  else if (track->par->codec_id == AV_CODEC_ID_H264 && !TAG_IS_AVCI(track->tag)) {
2030  mov_write_avcc_tag(pb, track);
2031  if (track->mode == MODE_IPOD)
2033  } else if (track->par->codec_id == AV_CODEC_ID_VP9) {
2034  mov_write_vpcc_tag(mov->fc, pb, track);
2035  } else if (track->par->codec_id == AV_CODEC_ID_AV1) {
2036  mov_write_av1c_tag(pb, track);
2037  } else if (track->par->codec_id == AV_CODEC_ID_VC1 && track->vos_len > 0)
2038  mov_write_dvc1_tag(pb, track);
2039  else if (track->par->codec_id == AV_CODEC_ID_VP6F ||
2040  track->par->codec_id == AV_CODEC_ID_VP6A) {
2041  /* Don't write any potential extradata here - the cropping
2042  * is signalled via the normal width/height fields. */
2043  } else if (track->par->codec_id == AV_CODEC_ID_R10K) {
2044  if (track->par->codec_tag == MKTAG('R','1','0','k'))
2045  mov_write_dpxe_tag(pb, track);
2046  } else if (track->vos_len > 0)
2047  mov_write_glbl_tag(pb, track);
2048 
2049  if (track->par->codec_id != AV_CODEC_ID_H264 &&
2050  track->par->codec_id != AV_CODEC_ID_MPEG4 &&
2051  track->par->codec_id != AV_CODEC_ID_DNXHD) {
2052  int field_order = track->par->field_order;
2053 
2054 #if FF_API_LAVF_AVCTX
2056  if (field_order != track->st->codec->field_order && track->st->codec->field_order != AV_FIELD_UNKNOWN)
2057  field_order = track->st->codec->field_order;
2059 #endif
2060 
2061  if (field_order != AV_FIELD_UNKNOWN)
2062  mov_write_fiel_tag(pb, track, field_order);
2063  }
2064 
2065  if (mov->flags & FF_MOV_FLAG_WRITE_GAMA) {
2066  if (track->mode == MODE_MOV)
2067  mov_write_gama_tag(pb, track, mov->gamma);
2068  else
2069  av_log(mov->fc, AV_LOG_WARNING, "Not writing 'gama' atom. Format is not MOV.\n");
2070  }
2071  if (mov->flags & FF_MOV_FLAG_WRITE_COLR) {
2072  if (track->mode == MODE_MOV || track->mode == MODE_MP4)
2073  mov_write_colr_tag(pb, track);
2074  else
2075  av_log(mov->fc, AV_LOG_WARNING, "Not writing 'colr' atom. Format is not MOV or MP4.\n");
2076  }
2077 
2078  if (track->mode == MODE_MP4 && mov->fc->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL) {
2081 
2082  if (stereo_3d)
2083  mov_write_st3d_tag(pb, stereo_3d);
2084  if (spherical_mapping)
2085  mov_write_sv3d_tag(mov->fc, pb, spherical_mapping);
2086  }
2087 
2088  if (track->par->sample_aspect_ratio.den && track->par->sample_aspect_ratio.num) {
2089  mov_write_pasp_tag(pb, track);
2090  }
2091 
2092  if (uncompressed_ycbcr){
2093  mov_write_clap_tag(pb, track);
2094  }
2095 
2096  if (mov->encryption_scheme != MOV_ENC_NONE) {
2097  ff_mov_cenc_write_sinf_tag(track, pb, mov->encryption_kid);
2098  }
2099 
2100  /* extra padding for avid stsd */
2101  /* https://developer.apple.com/library/mac/documentation/QuickTime/QTFF/QTFFChap2/qtff2.html#//apple_ref/doc/uid/TP40000939-CH204-61112 */
2102  if (avid)
2103  avio_wb32(pb, 0);
2104 
2105  return update_size(pb, pos);
2106 }
2107 
2108 static int mov_write_rtp_tag(AVIOContext *pb, MOVTrack *track)
2109 {
2110  int64_t pos = avio_tell(pb);
2111  avio_wb32(pb, 0); /* size */
2112  ffio_wfourcc(pb, "rtp ");
2113  avio_wb32(pb, 0); /* Reserved */
2114  avio_wb16(pb, 0); /* Reserved */
2115  avio_wb16(pb, 1); /* Data-reference index */
2116 
2117  avio_wb16(pb, 1); /* Hint track version */
2118  avio_wb16(pb, 1); /* Highest compatible version */
2119  avio_wb32(pb, track->max_packet_size); /* Max packet size */
2120 
2121  avio_wb32(pb, 12); /* size */
2122  ffio_wfourcc(pb, "tims");
2123  avio_wb32(pb, track->timescale);
2124 
2125  return update_size(pb, pos);
2126 }
2127 
2128 static int mov_write_source_reference_tag(AVIOContext *pb, MOVTrack *track, const char *reel_name)
2129 {
2130  uint64_t str_size =strlen(reel_name);
2131  int64_t pos = avio_tell(pb);
2132 
2133  if (str_size >= UINT16_MAX){
2134  av_log(NULL, AV_LOG_ERROR, "reel_name length %"PRIu64" is too large\n", str_size);
2135  avio_wb16(pb, 0);
2136  return AVERROR(EINVAL);
2137  }
2138 
2139  avio_wb32(pb, 0); /* size */
2140  ffio_wfourcc(pb, "name"); /* Data format */
2141  avio_wb16(pb, str_size); /* string size */
2142  avio_wb16(pb, track->language); /* langcode */
2143  avio_write(pb, reel_name, str_size); /* reel name */
2144  return update_size(pb,pos);
2145 }
2146 
2147 static int mov_write_tmcd_tag(AVIOContext *pb, MOVTrack *track)
2148 {
2149  int64_t pos = avio_tell(pb);
2150 #if 1
2151  int frame_duration;
2152  int nb_frames;
2153  AVDictionaryEntry *t = NULL;
2154 
2155  if (!track->st->avg_frame_rate.num || !track->st->avg_frame_rate.den) {
2156 #if FF_API_LAVF_AVCTX
2158  frame_duration = av_rescale(track->timescale, track->st->codec->time_base.num, track->st->codec->time_base.den);
2159  nb_frames = ROUNDED_DIV(track->st->codec->time_base.den, track->st->codec->time_base.num);
2161 #else
2162  av_log(NULL, AV_LOG_ERROR, "avg_frame_rate not set for tmcd track.\n");
2163  return AVERROR(EINVAL);
2164 #endif
2165  } else {
2166  frame_duration = av_rescale(track->timescale, track->st->avg_frame_rate.num, track->st->avg_frame_rate.den);
2167  nb_frames = ROUNDED_DIV(track->st->avg_frame_rate.den, track->st->avg_frame_rate.num);
2168  }
2169 
2170  if (nb_frames > 255) {
2171  av_log(NULL, AV_LOG_ERROR, "fps %d is too large\n", nb_frames);
2172  return AVERROR(EINVAL);
2173  }
2174 
2175  avio_wb32(pb, 0); /* size */
2176  ffio_wfourcc(pb, "tmcd"); /* Data format */
2177  avio_wb32(pb, 0); /* Reserved */
2178  avio_wb32(pb, 1); /* Data reference index */
2179  avio_wb32(pb, 0); /* Flags */
2180  avio_wb32(pb, track->timecode_flags); /* Flags (timecode) */
2181  avio_wb32(pb, track->timescale); /* Timescale */
2182  avio_wb32(pb, frame_duration); /* Frame duration */
2183  avio_w8(pb, nb_frames); /* Number of frames */
2184  avio_w8(pb, 0); /* Reserved */
2185 
2186  t = av_dict_get(track->st->metadata, "reel_name", NULL, 0);
2187  if (t && utf8len(t->value) && track->mode != MODE_MP4)
2188  mov_write_source_reference_tag(pb, track, t->value);
2189  else
2190  avio_wb16(pb, 0); /* zero size */
2191 #else
2192 
2193  avio_wb32(pb, 0); /* size */
2194  ffio_wfourcc(pb, "tmcd"); /* Data format */
2195  avio_wb32(pb, 0); /* Reserved */
2196  avio_wb32(pb, 1); /* Data reference index */
2197  if (track->par->extradata_size)
2198  avio_write(pb, track->par->extradata, track->par->extradata_size);
2199 #endif
2200  return update_size(pb, pos);
2201 }
2202 
2203 static int mov_write_gpmd_tag(AVIOContext *pb, const MOVTrack *track)
2204 {
2205  int64_t pos = avio_tell(pb);
2206  avio_wb32(pb, 0); /* size */
2207  ffio_wfourcc(pb, "gpmd");
2208  avio_wb32(pb, 0); /* Reserved */
2209  avio_wb16(pb, 0); /* Reserved */
2210  avio_wb16(pb, 1); /* Data-reference index */
2211  avio_wb32(pb, 0); /* Reserved */
2212  return update_size(pb, pos);
2213 }
2214 
2216 {
2217  int64_t pos = avio_tell(pb);
2218  avio_wb32(pb, 0); /* size */
2219  ffio_wfourcc(pb, "stsd");
2220  avio_wb32(pb, 0); /* version & flags */
2221  avio_wb32(pb, 1); /* entry count */
2222  if (track->par->codec_type == AVMEDIA_TYPE_VIDEO)
2223  mov_write_video_tag(pb, mov, track);
2224  else if (track->par->codec_type == AVMEDIA_TYPE_AUDIO)
2225  mov_write_audio_tag(s, pb, mov, track);
2226  else if (track->par->codec_type == AVMEDIA_TYPE_SUBTITLE)
2227  mov_write_subtitle_tag(pb, track);
2228  else if (track->par->codec_tag == MKTAG('r','t','p',' '))
2229  mov_write_rtp_tag(pb, track);
2230  else if (track->par->codec_tag == MKTAG('t','m','c','d'))
2231  mov_write_tmcd_tag(pb, track);
2232  else if (track->par->codec_tag == MKTAG('g','p','m','d'))
2233  mov_write_gpmd_tag(pb, track);
2234  return update_size(pb, pos);
2235 }
2236 
2238 {
2239  MOVMuxContext *mov = s->priv_data;
2240  MOVStts *ctts_entries;
2241  uint32_t entries = 0;
2242  uint32_t atom_size;
2243  int i;
2244 
2245  ctts_entries = av_malloc_array((track->entry + 1), sizeof(*ctts_entries)); /* worst case */
2246  if (!ctts_entries)
2247  return AVERROR(ENOMEM);
2248  ctts_entries[0].count = 1;
2249  ctts_entries[0].duration = track->cluster[0].cts;
2250  for (i = 1; i < track->entry; i++) {
2251  if (track->cluster[i].cts == ctts_entries[entries].duration) {
2252  ctts_entries[entries].count++; /* compress */
2253  } else {
2254  entries++;
2255  ctts_entries[entries].duration = track->cluster[i].cts;
2256  ctts_entries[entries].count = 1;
2257  }
2258  }
2259  entries++; /* last one */
2260  atom_size = 16 + (entries * 8);
2261  avio_wb32(pb, atom_size); /* size */
2262  ffio_wfourcc(pb, "ctts");
2264  avio_w8(pb, 1); /* version */
2265  else
2266  avio_w8(pb, 0); /* version */
2267  avio_wb24(pb, 0); /* flags */
2268  avio_wb32(pb, entries); /* entry count */
2269  for (i = 0; i < entries; i++) {
2270  avio_wb32(pb, ctts_entries[i].count);
2271  avio_wb32(pb, ctts_entries[i].duration);
2272  }
2273  av_free(ctts_entries);
2274  return atom_size;
2275 }
2276 
2277 /* Time to sample atom */
2278 static int mov_write_stts_tag(AVIOContext *pb, MOVTrack *track)
2279 {
2280  MOVStts *stts_entries = NULL;
2281  uint32_t entries = -1;
2282  uint32_t atom_size;
2283  int i;
2284 
2285  if (track->par->codec_type == AVMEDIA_TYPE_AUDIO && !track->audio_vbr) {
2286  stts_entries = av_malloc(sizeof(*stts_entries)); /* one entry */
2287  if (!stts_entries)
2288  return AVERROR(ENOMEM);
2289  stts_entries[0].count = track->sample_count;
2290  stts_entries[0].duration = 1;
2291  entries = 1;
2292  } else {
2293  if (track->entry) {
2294  stts_entries = av_malloc_array(track->entry, sizeof(*stts_entries)); /* worst case */
2295  if (!stts_entries)
2296  return AVERROR(ENOMEM);
2297  }
2298  for (i = 0; i < track->entry; i++) {
2299  int duration = get_cluster_duration(track, i);
2300  if (i && duration == stts_entries[entries].duration) {
2301  stts_entries[entries].count++; /* compress */
2302  } else {
2303  entries++;
2304  stts_entries[entries].duration = duration;
2305  stts_entries[entries].count = 1;
2306  }
2307  }
2308  entries++; /* last one */
2309  }
2310  atom_size = 16 + (entries * 8);
2311  avio_wb32(pb, atom_size); /* size */
2312  ffio_wfourcc(pb, "stts");
2313  avio_wb32(pb, 0); /* version & flags */
2314  avio_wb32(pb, entries); /* entry count */
2315  for (i = 0; i < entries; i++) {
2316  avio_wb32(pb, stts_entries[i].count);
2317  avio_wb32(pb, stts_entries[i].duration);
2318  }
2319  av_free(stts_entries);
2320  return atom_size;
2321 }
2322 
2324 {
2325  avio_wb32(pb, 28); /* size */
2326  ffio_wfourcc(pb, "dref");
2327  avio_wb32(pb, 0); /* version & flags */
2328  avio_wb32(pb, 1); /* entry count */
2329 
2330  avio_wb32(pb, 0xc); /* size */
2331  //FIXME add the alis and rsrc atom
2332  ffio_wfourcc(pb, "url ");
2333  avio_wb32(pb, 1); /* version & flags */
2334 
2335  return 28;
2336 }
2337 
2339 {
2340  struct sgpd_entry {
2341  int count;
2342  int16_t roll_distance;
2343  int group_description_index;
2344  };
2345 
2346  struct sgpd_entry *sgpd_entries = NULL;
2347  int entries = -1;
2348  int group = 0;
2349  int i, j;
2350 
2351  const int OPUS_SEEK_PREROLL_MS = 80;
2352  int roll_samples = av_rescale_q(OPUS_SEEK_PREROLL_MS,
2353  (AVRational){1, 1000},
2354  (AVRational){1, 48000});
2355 
2356  if (!track->entry)
2357  return 0;
2358 
2359  sgpd_entries = av_malloc_array(track->entry, sizeof(*sgpd_entries));
2360  if (!sgpd_entries)
2361  return AVERROR(ENOMEM);
2362 
2364 
2365  if (track->par->codec_id == AV_CODEC_ID_OPUS) {
2366  for (i = 0; i < track->entry; i++) {
2367  int roll_samples_remaining = roll_samples;
2368  int distance = 0;
2369  for (j = i - 1; j >= 0; j--) {
2370  roll_samples_remaining -= get_cluster_duration(track, j);
2371  distance++;
2372  if (roll_samples_remaining <= 0)
2373  break;
2374  }
2375  /* We don't have enough preceeding samples to compute a valid
2376  roll_distance here, so this sample can't be independently
2377  decoded. */
2378  if (roll_samples_remaining > 0)
2379  distance = 0;
2380  /* Verify distance is a maximum of 32 (2.5ms) packets. */
2381  if (distance > 32)
2382  return AVERROR_INVALIDDATA;
2383  if (i && distance == sgpd_entries[entries].roll_distance) {
2384  sgpd_entries[entries].count++;
2385  } else {
2386  entries++;
2387  sgpd_entries[entries].count = 1;
2388  sgpd_entries[entries].roll_distance = distance;
2389  sgpd_entries[entries].group_description_index = distance ? ++group : 0;
2390  }
2391  }
2392  } else {
2393  entries++;
2394  sgpd_entries[entries].count = track->sample_count;
2395  sgpd_entries[entries].roll_distance = 1;
2396  sgpd_entries[entries].group_description_index = ++group;
2397  }
2398  entries++;
2399 
2400  if (!group) {
2401  av_free(sgpd_entries);
2402  return 0;
2403  }
2404 
2405  /* Write sgpd tag */
2406  avio_wb32(pb, 24 + (group * 2)); /* size */
2407  ffio_wfourcc(pb, "sgpd");
2408  avio_wb32(pb, 1 << 24); /* fullbox */
2409  ffio_wfourcc(pb, "roll");
2410  avio_wb32(pb, 2); /* default_length */
2411  avio_wb32(pb, group); /* entry_count */
2412  for (i = 0; i < entries; i++) {
2413  if (sgpd_entries[i].group_description_index) {
2414  avio_wb16(pb, -sgpd_entries[i].roll_distance); /* roll_distance */
2415  }
2416  }
2417 
2418  /* Write sbgp tag */
2419  avio_wb32(pb, 20 + (entries * 8)); /* size */
2420  ffio_wfourcc(pb, "sbgp");
2421  avio_wb32(pb, 0); /* fullbox */
2422  ffio_wfourcc(pb, "roll");
2423  avio_wb32(pb, entries); /* entry_count */
2424  for (i = 0; i < entries; i++) {
2425  avio_wb32(pb, sgpd_entries[i].count); /* sample_count */
2426  avio_wb32(pb, sgpd_entries[i].group_description_index); /* group_description_index */
2427  }
2428 
2429  av_free(sgpd_entries);
2430  return 0;
2431 }
2432 
2434 {
2435  int64_t pos = avio_tell(pb);
2436  int ret;
2437 
2438  avio_wb32(pb, 0); /* size */
2439  ffio_wfourcc(pb, "stbl");
2440  mov_write_stsd_tag(s, pb, mov, track);
2441  mov_write_stts_tag(pb, track);
2442  if ((track->par->codec_type == AVMEDIA_TYPE_VIDEO ||
2443  track->par->codec_tag == MKTAG('r','t','p',' ')) &&
2444  track->has_keyframes && track->has_keyframes < track->entry)
2445  mov_write_stss_tag(pb, track, MOV_SYNC_SAMPLE);
2446  if (track->par->codec_type == AVMEDIA_TYPE_VIDEO && track->has_disposable)
2447  mov_write_sdtp_tag(pb, track);
2448  if (track->mode == MODE_MOV && track->flags & MOV_TRACK_STPS)
2450  if (track->par->codec_type == AVMEDIA_TYPE_VIDEO &&
2451  track->flags & MOV_TRACK_CTTS && track->entry) {
2452 
2453  if ((ret = mov_write_ctts_tag(s, pb, track)) < 0)
2454  return ret;
2455  }
2456  mov_write_stsc_tag(pb, track);
2457  mov_write_stsz_tag(pb, track);
2458  mov_write_stco_tag(pb, track);
2459  if (track->cenc.aes_ctr) {
2460  ff_mov_cenc_write_stbl_atoms(&track->cenc, pb);
2461  }
2462  if (track->par->codec_id == AV_CODEC_ID_OPUS || track->par->codec_id == AV_CODEC_ID_AAC) {
2463  mov_preroll_write_stbl_atoms(pb, track);
2464  }
2465  return update_size(pb, pos);
2466 }
2467 
2469 {
2470  int64_t pos = avio_tell(pb);
2471  avio_wb32(pb, 0); /* size */
2472  ffio_wfourcc(pb, "dinf");
2473  mov_write_dref_tag(pb);
2474  return update_size(pb, pos);
2475 }
2476 
2478 {
2479  avio_wb32(pb, 12);
2480  ffio_wfourcc(pb, "nmhd");
2481  avio_wb32(pb, 0);
2482  return 12;
2483 }
2484 
2485 static int mov_write_tcmi_tag(AVIOContext *pb, MOVTrack *track)
2486 {
2487  int64_t pos = avio_tell(pb);
2488  const char *font = "Lucida Grande";
2489  avio_wb32(pb, 0); /* size */
2490  ffio_wfourcc(pb, "tcmi"); /* timecode media information atom */
2491  avio_wb32(pb, 0); /* version & flags */
2492  avio_wb16(pb, 0); /* text font */
2493  avio_wb16(pb, 0); /* text face */
2494  avio_wb16(pb, 12); /* text size */
2495  avio_wb16(pb, 0); /* (unknown, not in the QT specs...) */
2496  avio_wb16(pb, 0x0000); /* text color (red) */
2497  avio_wb16(pb, 0x0000); /* text color (green) */
2498  avio_wb16(pb, 0x0000); /* text color (blue) */
2499  avio_wb16(pb, 0xffff); /* background color (red) */
2500  avio_wb16(pb, 0xffff); /* background color (green) */
2501  avio_wb16(pb, 0xffff); /* background color (blue) */
2502  avio_w8(pb, strlen(font)); /* font len (part of the pascal string) */
2503  avio_write(pb, font, strlen(font)); /* font name */
2504  return update_size(pb, pos);
2505 }
2506 
2507 static int mov_write_gmhd_tag(AVIOContext *pb, MOVTrack *track)
2508 {
2509  int64_t pos = avio_tell(pb);
2510  avio_wb32(pb, 0); /* size */
2511  ffio_wfourcc(pb, "gmhd");
2512  avio_wb32(pb, 0x18); /* gmin size */
2513  ffio_wfourcc(pb, "gmin");/* generic media info */
2514  avio_wb32(pb, 0); /* version & flags */
2515  avio_wb16(pb, 0x40); /* graphics mode = */
2516  avio_wb16(pb, 0x8000); /* opColor (r?) */
2517  avio_wb16(pb, 0x8000); /* opColor (g?) */
2518  avio_wb16(pb, 0x8000); /* opColor (b?) */
2519  avio_wb16(pb, 0); /* balance */
2520  avio_wb16(pb, 0); /* reserved */
2521 
2522  /*
2523  * This special text atom is required for
2524  * Apple Quicktime chapters. The contents
2525  * don't appear to be documented, so the
2526  * bytes are copied verbatim.
2527  */
2528  if (track->tag != MKTAG('c','6','0','8')) {
2529  avio_wb32(pb, 0x2C); /* size */
2530  ffio_wfourcc(pb, "text");
2531  avio_wb16(pb, 0x01);
2532  avio_wb32(pb, 0x00);
2533  avio_wb32(pb, 0x00);
2534  avio_wb32(pb, 0x00);
2535  avio_wb32(pb, 0x01);
2536  avio_wb32(pb, 0x00);
2537  avio_wb32(pb, 0x00);
2538  avio_wb32(pb, 0x00);
2539  avio_wb32(pb, 0x00004000);
2540  avio_wb16(pb, 0x0000);
2541  }
2542 
2543  if (track->par->codec_tag == MKTAG('t','m','c','d')) {
2544  int64_t tmcd_pos = avio_tell(pb);
2545  avio_wb32(pb, 0); /* size */
2546  ffio_wfourcc(pb, "tmcd");
2547  mov_write_tcmi_tag(pb, track);
2548  update_size(pb, tmcd_pos);
2549  } else if (track->par->codec_tag == MKTAG('g','p','m','d')) {
2550  int64_t gpmd_pos = avio_tell(pb);
2551  avio_wb32(pb, 0); /* size */
2552  ffio_wfourcc(pb, "gpmd");
2553  avio_wb32(pb, 0); /* version */
2554  update_size(pb, gpmd_pos);
2555  }
2556  return update_size(pb, pos);
2557 }
2558 
2560 {
2561  avio_wb32(pb, 16); /* size */
2562  ffio_wfourcc(pb, "smhd");
2563  avio_wb32(pb, 0); /* version & flags */
2564  avio_wb16(pb, 0); /* reserved (balance, normally = 0) */
2565  avio_wb16(pb, 0); /* reserved */
2566  return 16;
2567 }
2568 
2570 {
2571  avio_wb32(pb, 0x14); /* size (always 0x14) */
2572  ffio_wfourcc(pb, "vmhd");
2573  avio_wb32(pb, 0x01); /* version & flags */
2574  avio_wb64(pb, 0); /* reserved (graphics mode = copy) */
2575  return 0x14;
2576 }
2577 
2578 static int is_clcp_track(MOVTrack *track)
2579 {
2580  return track->tag == MKTAG('c','7','0','8') ||
2581  track->tag == MKTAG('c','6','0','8');
2582 }
2583 
2585 {
2586  MOVMuxContext *mov = s->priv_data;
2587  const char *hdlr, *descr = NULL, *hdlr_type = NULL;
2588  int64_t pos = avio_tell(pb);
2589 
2590  hdlr = "dhlr";
2591  hdlr_type = "url ";
2592  descr = "DataHandler";
2593 
2594  if (track) {
2595  hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
2596  if (track->par->codec_type == AVMEDIA_TYPE_VIDEO) {
2597  hdlr_type = "vide";
2598  descr = "VideoHandler";
2599  } else if (track->par->codec_type == AVMEDIA_TYPE_AUDIO) {
2600  hdlr_type = "soun";
2601  descr = "SoundHandler";
2602  } else if (track->par->codec_type == AVMEDIA_TYPE_SUBTITLE) {
2603  if (is_clcp_track(track)) {
2604  hdlr_type = "clcp";
2605  descr = "ClosedCaptionHandler";
2606  } else {
2607  if (track->tag == MKTAG('t','x','3','g')) {
2608  hdlr_type = "sbtl";
2609  } else if (track->tag == MKTAG('m','p','4','s')) {
2610  hdlr_type = "subp";
2611  } else {
2612  hdlr_type = "text";
2613  }
2614  descr = "SubtitleHandler";
2615  }
2616  } else if (track->par->codec_tag == MKTAG('r','t','p',' ')) {
2617  hdlr_type = "hint";
2618  descr = "HintHandler";
2619  } else if (track->par->codec_tag == MKTAG('t','m','c','d')) {
2620  hdlr_type = "tmcd";
2621  descr = "TimeCodeHandler";
2622  } else if (track->par->codec_tag == MKTAG('g','p','m','d')) {
2623  hdlr_type = "meta";
2624  descr = "GoPro MET"; // GoPro Metadata
2625  } else {
2627  "Unknown hldr_type for %s, writing dummy values\n",
2628  av_fourcc2str(track->par->codec_tag));
2629  }
2630  if (track->st) {
2631  // hdlr.name is used by some players to identify the content title
2632  // of the track. So if an alternate handler description is
2633  // specified, use it.
2634  AVDictionaryEntry *t;
2635  t = av_dict_get(track->st->metadata, "handler_name", NULL, 0);
2636  if (t && utf8len(t->value))
2637  descr = t->value;
2638  }
2639  }
2640 
2641  if (mov->empty_hdlr_name) /* expressly allowed by QTFF and not prohibited in ISO 14496-12 8.4.3.3 */
2642  descr = "";
2643 
2644  avio_wb32(pb, 0); /* size */
2645  ffio_wfourcc(pb, "hdlr");
2646  avio_wb32(pb, 0); /* Version & flags */
2647  avio_write(pb, hdlr, 4); /* handler */
2648  ffio_wfourcc(pb, hdlr_type); /* handler type */
2649  avio_wb32(pb, 0); /* reserved */
2650  avio_wb32(pb, 0); /* reserved */
2651  avio_wb32(pb, 0); /* reserved */
2652  if (!track || track->mode == MODE_MOV)
2653  avio_w8(pb, strlen(descr)); /* pascal string */
2654  avio_write(pb, descr, strlen(descr)); /* handler description */
2655  if (track && track->mode != MODE_MOV)
2656  avio_w8(pb, 0); /* c string */
2657  return update_size(pb, pos);
2658 }
2659 
2661 {
2662  /* This atom must be present, but leaving the values at zero
2663  * seems harmless. */
2664  avio_wb32(pb, 28); /* size */
2665  ffio_wfourcc(pb, "hmhd");
2666  avio_wb32(pb, 0); /* version, flags */
2667  avio_wb16(pb, 0); /* maxPDUsize */
2668  avio_wb16(pb, 0); /* avgPDUsize */
2669  avio_wb32(pb, 0); /* maxbitrate */
2670  avio_wb32(pb, 0); /* avgbitrate */
2671  avio_wb32(pb, 0); /* reserved */
2672  return 28;
2673 }
2674 
2676 {
2677  int64_t pos = avio_tell(pb);
2678  int ret;
2679 
2680  avio_wb32(pb, 0); /* size */
2681  ffio_wfourcc(pb, "minf");
2682  if (track->par->codec_type == AVMEDIA_TYPE_VIDEO)
2683  mov_write_vmhd_tag(pb);
2684  else if (track->par->codec_type == AVMEDIA_TYPE_AUDIO)
2685  mov_write_smhd_tag(pb);
2686  else if (track->par->codec_type == AVMEDIA_TYPE_SUBTITLE) {
2687  if (track->tag == MKTAG('t','e','x','t') || is_clcp_track(track)) {
2688  mov_write_gmhd_tag(pb, track);
2689  } else {
2690  mov_write_nmhd_tag(pb);
2691  }
2692  } else if (track->tag == MKTAG('r','t','p',' ')) {
2693  mov_write_hmhd_tag(pb);
2694  } else if (track->tag == MKTAG('t','m','c','d')) {
2695  if (track->mode != MODE_MOV)
2696  mov_write_nmhd_tag(pb);
2697  else
2698  mov_write_gmhd_tag(pb, track);
2699  } else if (track->tag == MKTAG('g','p','m','d')) {
2700  mov_write_gmhd_tag(pb, track);
2701  }
2702  if (track->mode == MODE_MOV) /* ISO 14496-12 8.4.3.1 specifies hdlr only within mdia or meta boxes */
2703  mov_write_hdlr_tag(s, pb, NULL);
2704  mov_write_dinf_tag(pb);
2705  if ((ret = mov_write_stbl_tag(s, pb, mov, track)) < 0)
2706  return ret;
2707  return update_size(pb, pos);
2708 }
2709 
2711  MOVTrack *track)
2712 {
2713  int version = track->track_duration < INT32_MAX ? 0 : 1;
2714 
2715  if (track->mode == MODE_ISM)
2716  version = 1;
2717 
2718  (version == 1) ? avio_wb32(pb, 44) : avio_wb32(pb, 32); /* size */
2719  ffio_wfourcc(pb, "mdhd");
2720  avio_w8(pb, version);
2721  avio_wb24(pb, 0); /* flags */
2722  if (version == 1) {
2723  avio_wb64(pb, track->time);
2724  avio_wb64(pb, track->time);
2725  } else {
2726  avio_wb32(pb, track->time); /* creation time */
2727  avio_wb32(pb, track->time); /* modification time */
2728  }
2729  avio_wb32(pb, track->timescale); /* time scale (sample rate for audio) */
2730  if (!track->entry && mov->mode == MODE_ISM)
2731  (version == 1) ? avio_wb64(pb, UINT64_C(0xffffffffffffffff)) : avio_wb32(pb, 0xffffffff);
2732  else if (!track->entry)
2733  (version == 1) ? avio_wb64(pb, 0) : avio_wb32(pb, 0);
2734  else
2735  (version == 1) ? avio_wb64(pb, track->track_duration) : avio_wb32(pb, track->track_duration); /* duration */
2736  avio_wb16(pb, track->language); /* language */
2737  avio_wb16(pb, 0); /* reserved (quality) */
2738 
2739  if (version != 0 && track->mode == MODE_MOV) {
2741  "FATAL error, file duration too long for timebase, this file will not be\n"
2742  "playable with quicktime. Choose a different timebase or a different\n"
2743  "container format\n");
2744  }
2745 
2746  return 32;
2747 }
2748 
2750  MOVMuxContext *mov, MOVTrack *track)
2751 {
2752  int64_t pos = avio_tell(pb);
2753  int ret;
2754 
2755  avio_wb32(pb, 0); /* size */
2756  ffio_wfourcc(pb, "mdia");
2757  mov_write_mdhd_tag(pb, mov, track);
2758  mov_write_hdlr_tag(s, pb, track);
2759  if ((ret = mov_write_minf_tag(s, pb, mov, track)) < 0)
2760  return ret;
2761  return update_size(pb, pos);
2762 }
2763 
2764 /* transformation matrix
2765  |a b u|
2766  |c d v|
2767  |tx ty w| */
2768 static void write_matrix(AVIOContext *pb, int16_t a, int16_t b, int16_t c,
2769  int16_t d, int16_t tx, int16_t ty)
2770 {
2771  avio_wb32(pb, a << 16); /* 16.16 format */
2772  avio_wb32(pb, b << 16); /* 16.16 format */
2773  avio_wb32(pb, 0); /* u in 2.30 format */
2774  avio_wb32(pb, c << 16); /* 16.16 format */
2775  avio_wb32(pb, d << 16); /* 16.16 format */
2776  avio_wb32(pb, 0); /* v in 2.30 format */
2777  avio_wb32(pb, tx << 16); /* 16.16 format */
2778  avio_wb32(pb, ty << 16); /* 16.16 format */
2779  avio_wb32(pb, 1 << 30); /* w in 2.30 format */
2780 }
2781 
2783  MOVTrack *track, AVStream *st)
2784 {
2786  track->timescale, AV_ROUND_UP);
2787  int version = duration < INT32_MAX ? 0 : 1;
2789  int rotation = 0;
2790  int group = 0;
2791 
2792  uint32_t *display_matrix = NULL;
2793  int display_matrix_size, i;
2794 
2795  if (st) {
2796  if (mov->per_stream_grouping)
2797  group = st->index;
2798  else
2799  group = st->codecpar->codec_type;
2800 
2801  display_matrix = (uint32_t*)av_stream_get_side_data(st, AV_PKT_DATA_DISPLAYMATRIX,
2802  &display_matrix_size);
2803  if (display_matrix && display_matrix_size < 9 * sizeof(*display_matrix))
2804  display_matrix = NULL;
2805  }
2806 
2807  if (track->flags & MOV_TRACK_ENABLED)
2808  flags |= MOV_TKHD_FLAG_ENABLED;
2809 
2810  if (track->mode == MODE_ISM)
2811  version = 1;
2812 
2813  (version == 1) ? avio_wb32(pb, 104) : avio_wb32(pb, 92); /* size */
2814  ffio_wfourcc(pb, "tkhd");
2815  avio_w8(pb, version);
2816  avio_wb24(pb, flags);
2817  if (version == 1) {
2818  avio_wb64(pb, track->time);
2819  avio_wb64(pb, track->time);
2820  } else {
2821  avio_wb32(pb, track->time); /* creation time */
2822  avio_wb32(pb, track->time); /* modification time */
2823  }
2824  avio_wb32(pb, track->track_id); /* track-id */
2825  avio_wb32(pb, 0); /* reserved */
2826  if (!track->entry && mov->mode == MODE_ISM)
2827  (version == 1) ? avio_wb64(pb, UINT64_C(0xffffffffffffffff)) : avio_wb32(pb, 0xffffffff);
2828  else if (!track->entry)
2829  (version == 1) ? avio_wb64(pb, 0) : avio_wb32(pb, 0);
2830  else
2831  (version == 1) ? avio_wb64(pb, duration) : avio_wb32(pb, duration);
2832 
2833  avio_wb32(pb, 0); /* reserved */
2834  avio_wb32(pb, 0); /* reserved */
2835  avio_wb16(pb, 0); /* layer */
2836  avio_wb16(pb, group); /* alternate group) */
2837  /* Volume, only for audio */
2838  if (track->par->codec_type == AVMEDIA_TYPE_AUDIO)
2839  avio_wb16(pb, 0x0100);
2840  else
2841  avio_wb16(pb, 0);
2842  avio_wb16(pb, 0); /* reserved */
2843 
2844  /* Matrix structure */
2845 #if FF_API_OLD_ROTATE_API
2846  if (st && st->metadata) {
2847  AVDictionaryEntry *rot = av_dict_get(st->metadata, "rotate", NULL, 0);
2848  rotation = (rot && rot->value) ? atoi(rot->value) : 0;
2849  }
2850 #endif
2851  if (display_matrix) {
2852  for (i = 0; i < 9; i++)
2853  avio_wb32(pb, display_matrix[i]);
2854 #if FF_API_OLD_ROTATE_API
2855  } else if (rotation == 90) {
2856  write_matrix(pb, 0, 1, -1, 0, track->par->height, 0);
2857  } else if (rotation == 180) {
2858  write_matrix(pb, -1, 0, 0, -1, track->par->width, track->par->height);
2859  } else if (rotation == 270) {
2860  write_matrix(pb, 0, -1, 1, 0, 0, track->par->width);
2861 #endif
2862  } else {
2863  write_matrix(pb, 1, 0, 0, 1, 0, 0);
2864  }
2865  /* Track width and height, for visual only */
2866  if (st && (track->par->codec_type == AVMEDIA_TYPE_VIDEO ||
2867  track->par->codec_type == AVMEDIA_TYPE_SUBTITLE)) {
2868  int64_t track_width_1616;
2869  if (track->mode == MODE_MOV) {
2870  track_width_1616 = track->par->width * 0x10000ULL;
2871  } else {
2872  track_width_1616 = av_rescale(st->sample_aspect_ratio.num,
2873  track->par->width * 0x10000LL,
2874  st->sample_aspect_ratio.den);
2875  if (!track_width_1616 ||
2876  track->height != track->par->height ||
2877  track_width_1616 > UINT32_MAX)
2878  track_width_1616 = track->par->width * 0x10000ULL;
2879  }
2880  if (track_width_1616 > UINT32_MAX) {
2881  av_log(mov->fc, AV_LOG_WARNING, "track width is too large\n");
2882  track_width_1616 = 0;
2883  }
2884  avio_wb32(pb, track_width_1616);
2885  if (track->height > 0xFFFF) {
2886  av_log(mov->fc, AV_LOG_WARNING, "track height is too large\n");
2887  avio_wb32(pb, 0);
2888  } else
2889  avio_wb32(pb, track->height * 0x10000U);
2890  } else {
2891  avio_wb32(pb, 0);
2892  avio_wb32(pb, 0);
2893  }
2894  return 0x5c;
2895 }
2896 
2897 static int mov_write_tapt_tag(AVIOContext *pb, MOVTrack *track)
2898 {
2900  track->par->sample_aspect_ratio.den);
2901 
2902  int64_t pos = avio_tell(pb);
2903 
2904  avio_wb32(pb, 0); /* size */
2905  ffio_wfourcc(pb, "tapt");
2906 
2907  avio_wb32(pb, 20);
2908  ffio_wfourcc(pb, "clef");
2909  avio_wb32(pb, 0);
2910  avio_wb32(pb, width << 16);
2911  avio_wb32(pb, track->par->height << 16);
2912 
2913  avio_wb32(pb, 20);
2914  ffio_wfourcc(pb, "prof");
2915  avio_wb32(pb, 0);
2916  avio_wb32(pb, width << 16);
2917  avio_wb32(pb, track->par->height << 16);
2918 
2919  avio_wb32(pb, 20);
2920  ffio_wfourcc(pb, "enof");
2921  avio_wb32(pb, 0);
2922  avio_wb32(pb, track->par->width << 16);
2923  avio_wb32(pb, track->par->height << 16);
2924 
2925  return update_size(pb, pos);
2926 }
2927 
2928 // This box seems important for the psp playback ... without it the movie seems to hang
2930  MOVTrack *track)
2931 {
2933  track->timescale, AV_ROUND_UP);
2934  int version = duration < INT32_MAX ? 0 : 1;
2935  int entry_size, entry_count, size;
2936  int64_t delay, start_ct = track->start_cts;
2937  int64_t start_dts = track->start_dts;
2938 
2939  if (track->entry) {
2940  if (start_dts != track->cluster[0].dts || start_ct != track->cluster[0].cts) {
2941 
2942  av_log(mov->fc, AV_LOG_DEBUG,
2943  "EDTS using dts:%"PRId64" cts:%d instead of dts:%"PRId64" cts:%"PRId64" tid:%d\n",
2944  track->cluster[0].dts, track->cluster[0].cts,
2945  start_dts, start_ct, track->track_id);
2946  start_dts = track->cluster[0].dts;
2947  start_ct = track->cluster[0].cts;
2948  }
2949  }
2950 
2951  delay = av_rescale_rnd(start_dts + start_ct, MOV_TIMESCALE,
2952  track->timescale, AV_ROUND_DOWN);
2953  version |= delay < INT32_MAX ? 0 : 1;
2954 
2955  entry_size = (version == 1) ? 20 : 12;
2956  entry_count = 1 + (delay > 0);
2957  size = 24 + entry_count * entry_size;
2958 
2959  /* write the atom data */
2960  avio_wb32(pb, size);
2961  ffio_wfourcc(pb, "edts");
2962  avio_wb32(pb, size - 8);
2963  ffio_wfourcc(pb, "elst");
2964  avio_w8(pb, version);
2965  avio_wb24(pb, 0); /* flags */
2966 
2967  avio_wb32(pb, entry_count);
2968  if (delay > 0) { /* add an empty edit to delay presentation */
2969  /* In the positive delay case, the delay includes the cts
2970  * offset, and the second edit list entry below trims out
2971  * the same amount from the actual content. This makes sure
2972  * that the offset last sample is included in the edit
2973  * list duration as well. */
2974  if (version == 1) {
2975  avio_wb64(pb, delay);
2976  avio_wb64(pb, -1);
2977  } else {
2978  avio_wb32(pb, delay);
2979  avio_wb32(pb, -1);
2980  }
2981  avio_wb32(pb, 0x00010000);
2982  } else {
2983  /* Avoid accidentally ending up with start_ct = -1 which has got a
2984  * special meaning. Normally start_ct should end up positive or zero
2985  * here, but use FFMIN in case dts is a small positive integer
2986  * rounded to 0 when represented in MOV_TIMESCALE units. */
2987  av_assert0(av_rescale_rnd(start_dts, MOV_TIMESCALE, track->timescale, AV_ROUND_DOWN) <= 0);
2988  start_ct = -FFMIN(start_dts, 0);
2989  /* Note, this delay is calculated from the pts of the first sample,
2990  * ensuring that we don't reduce the duration for cases with
2991  * dts<0 pts=0. */
2992  duration += delay;
2993  }
2994 
2995  /* For fragmented files, we don't know the full length yet. Setting
2996  * duration to 0 allows us to only specify the offset, including
2997  * the rest of the content (from all future fragments) without specifying
2998  * an explicit duration. */
2999  if (mov->flags & FF_MOV_FLAG_FRAGMENT)
3000  duration = 0;
3001 
3002  /* duration */
3003  if (version == 1) {
3004  avio_wb64(pb, duration);
3005  avio_wb64(pb, start_ct);
3006  } else {
3007  avio_wb32(pb, duration);
3008  avio_wb32(pb, start_ct);
3009  }
3010  avio_wb32(pb, 0x00010000);
3011  return size;
3012 }
3013 
3014 static int mov_write_tref_tag(AVIOContext *pb, MOVTrack *track)
3015 {
3016  avio_wb32(pb, 20); // size
3017  ffio_wfourcc(pb, "tref");
3018  avio_wb32(pb, 12); // size (subatom)
3019  avio_wl32(pb, track->tref_tag);
3020  avio_wb32(pb, track->tref_id);
3021  return 20;
3022 }
3023 
3024 // goes at the end of each track! ... Critical for PSP playback ("Incompatible data" without it)
3026 {
3027  avio_wb32(pb, 0x34); /* size ... reports as 28 in mp4box! */
3028  ffio_wfourcc(pb, "uuid");
3029  ffio_wfourcc(pb, "USMT");
3030  avio_wb32(pb, 0x21d24fce);
3031  avio_wb32(pb, 0xbb88695c);
3032  avio_wb32(pb, 0xfac9c740);
3033  avio_wb32(pb, 0x1c); // another size here!
3034  ffio_wfourcc(pb, "MTDT");
3035  avio_wb32(pb, 0x00010012);
3036  avio_wb32(pb, 0x0a);
3037  avio_wb32(pb, 0x55c40000);
3038  avio_wb32(pb, 0x1);
3039  avio_wb32(pb, 0x0);
3040  return 0x34;
3041 }
3042 
3043 static int mov_write_udta_sdp(AVIOContext *pb, MOVTrack *track)
3044 {
3045  AVFormatContext *ctx = track->rtp_ctx;
3046  char buf[1000] = "";
3047  int len;
3048 
3049  ff_sdp_write_media(buf, sizeof(buf), ctx->streams[0], track->src_track,
3050  NULL, NULL, 0, 0, ctx);
3051  av_strlcatf(buf, sizeof(buf), "a=control:streamid=%d\r\n", track->track_id);
3052  len = strlen(buf);
3053 
3054  avio_wb32(pb, len + 24);
3055  ffio_wfourcc(pb, "udta");
3056  avio_wb32(pb, len + 16);
3057  ffio_wfourcc(pb, "hnti");
3058  avio_wb32(pb, len + 8);
3059  ffio_wfourcc(pb, "sdp ");
3060  avio_write(pb, buf, len);
3061  return len + 24;
3062 }
3063 
3065  const char *tag, const char *str)
3066 {
3067  int64_t pos = avio_tell(pb);
3068  AVDictionaryEntry *t = av_dict_get(st->metadata, str, NULL, 0);
3069  if (!t || !utf8len(t->value))
3070  return 0;
3071 
3072  avio_wb32(pb, 0); /* size */
3073  ffio_wfourcc(pb, tag); /* type */
3074  avio_write(pb, t->value, strlen(t->value)); /* UTF8 string value */
3075  return update_size(pb, pos);
3076 }
3077 
3079  AVStream *st)
3080 {
3081  AVIOContext *pb_buf;
3082  int ret, size;
3083  uint8_t *buf;
3084 
3085  if (!st)
3086  return 0;
3087 
3088  ret = avio_open_dyn_buf(&pb_buf);
3089  if (ret < 0)
3090  return ret;
3091 
3092  if (mov->mode & (MODE_MP4|MODE_MOV))
3093  mov_write_track_metadata(pb_buf, st, "name", "title");
3094 
3095  if ((size = avio_close_dyn_buf(pb_buf, &buf)) > 0) {
3096  avio_wb32(pb, size + 8);
3097  ffio_wfourcc(pb, "udta");
3098  avio_write(pb, buf, size);
3099  }
3100  av_free(buf);
3101 
3102  return 0;
3103 }
3104 
3106  MOVTrack *track, AVStream *st)
3107 {
3108  int64_t pos = avio_tell(pb);
3109  int entry_backup = track->entry;
3110  int chunk_backup = track->chunkCount;
3111  int ret;
3112 
3113  /* If we want to have an empty moov, but some samples already have been
3114  * buffered (delay_moov), pretend that no samples have been written yet. */
3115  if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV)
3116  track->chunkCount = track->entry = 0;
3117 
3118  avio_wb32(pb, 0); /* size */
3119  ffio_wfourcc(pb, "trak");
3120  mov_write_tkhd_tag(pb, mov, track, st);
3121 
3122  av_assert2(mov->use_editlist >= 0);
3123 
3124  if (track->start_dts != AV_NOPTS_VALUE) {
3125  if (mov->use_editlist)
3126  mov_write_edts_tag(pb, mov, track); // PSP Movies and several other cases require edts box
3127  else if ((track->entry && track->cluster[0].dts) || track->mode == MODE_PSP || is_clcp_track(track))
3128  av_log(mov->fc, AV_LOG_WARNING,
3129  "Not writing any edit list even though one would have been required\n");
3130  }
3131 
3132  if (track->tref_tag)
3133  mov_write_tref_tag(pb, track);
3134 
3135  if ((ret = mov_write_mdia_tag(s, pb, mov, track)) < 0)
3136  return ret;
3137  if (track->mode == MODE_PSP)
3138  mov_write_uuid_tag_psp(pb, track); // PSP Movies require this uuid box
3139  if (track->tag == MKTAG('r','t','p',' '))
3140  mov_write_udta_sdp(pb, track);
3141  if (track->mode == MODE_MOV) {
3142  if (track->par->codec_type == AVMEDIA_TYPE_VIDEO) {
3143  double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
3144  if (st->sample_aspect_ratio.num && 1.0 != sample_aspect_ratio) {
3145  mov_write_tapt_tag(pb, track);
3146  }
3147  }
3148  if (is_clcp_track(track) && st->sample_aspect_ratio.num) {
3149  mov_write_tapt_tag(pb, track);
3150  }
3151  }
3152  mov_write_track_udta_tag(pb, mov, st);
3153  track->entry = entry_backup;
3154  track->chunkCount = chunk_backup;
3155  return update_size(pb, pos);
3156 }
3157 
3159 {
3160  int i, has_audio = 0, has_video = 0;
3161  int64_t pos = avio_tell(pb);
3162  int audio_profile = mov->iods_audio_profile;
3163  int video_profile = mov->iods_video_profile;
3164  for (i = 0; i < mov->nb_streams; i++) {
3165  if (mov->tracks[i].entry > 0 || mov->flags & FF_MOV_FLAG_EMPTY_MOOV) {
3166  has_audio |= mov->tracks[i].par->codec_type == AVMEDIA_TYPE_AUDIO;
3167  has_video |= mov->tracks[i].par->codec_type == AVMEDIA_TYPE_VIDEO;
3168  }
3169  }
3170  if (audio_profile < 0)
3171  audio_profile = 0xFF - has_audio;
3172  if (video_profile < 0)
3173  video_profile = 0xFF - has_video;
3174  avio_wb32(pb, 0x0); /* size */
3175  ffio_wfourcc(pb, "iods");
3176  avio_wb32(pb, 0); /* version & flags */
3177  put_descr(pb, 0x10, 7);
3178  avio_wb16(pb, 0x004f);
3179  avio_w8(pb, 0xff);
3180  avio_w8(pb, 0xff);
3181  avio_w8(pb, audio_profile);
3182  avio_w8(pb, video_profile);
3183  avio_w8(pb, 0xff);
3184  return update_size(pb, pos);
3185 }
3186 
3187 static int mov_write_trex_tag(AVIOContext *pb, MOVTrack *track)
3188 {
3189  avio_wb32(pb, 0x20); /* size */
3190  ffio_wfourcc(pb, "trex");
3191  avio_wb32(pb, 0); /* version & flags */
3192  avio_wb32(pb, track->track_id); /* track ID */
3193  avio_wb32(pb, 1); /* default sample description index */
3194  avio_wb32(pb, 0); /* default sample duration */
3195  avio_wb32(pb, 0); /* default sample size */
3196  avio_wb32(pb, 0); /* default sample flags */
3197  return 0;
3198 }
3199 
3201 {
3202  int64_t pos = avio_tell(pb);
3203  int i;
3204  avio_wb32(pb, 0x0); /* size */
3205  ffio_wfourcc(pb, "mvex");
3206  for (i = 0; i < mov->nb_streams; i++)
3207  mov_write_trex_tag(pb, &mov->tracks[i]);
3208  return update_size(pb, pos);
3209 }
3210 
3212 {
3213  int max_track_id = 1, i;
3214  int64_t max_track_len = 0;
3215  int version;
3216 
3217  for (i = 0; i < mov->nb_streams; i++) {
3218  if (mov->tracks[i].entry > 0 && mov->tracks[i].timescale) {
3219  int64_t max_track_len_temp = av_rescale_rnd(mov->tracks[i].track_duration,
3220  MOV_TIMESCALE,
3221  mov->tracks[i].timescale,
3222  AV_ROUND_UP);
3223  if (max_track_len < max_track_len_temp)
3224  max_track_len = max_track_len_temp;
3225  if (max_track_id < mov->tracks[i].track_id)
3226  max_track_id = mov->tracks[i].track_id;
3227  }
3228  }
3229  /* If using delay_moov, make sure the output is the same as if no
3230  * samples had been written yet. */
3231  if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV) {
3232  max_track_len = 0;
3233  max_track_id = 1;
3234  }
3235 
3236  version = max_track_len < UINT32_MAX ? 0 : 1;
3237  avio_wb32(pb, version == 1 ? 120 : 108); /* size */
3238 
3239  ffio_wfourcc(pb, "mvhd");
3240  avio_w8(pb, version);
3241  avio_wb24(pb, 0); /* flags */
3242  if (version == 1) {
3243  avio_wb64(pb, mov->time);
3244  avio_wb64(pb, mov->time);
3245  } else {
3246  avio_wb32(pb, mov->time); /* creation time */
3247  avio_wb32(pb, mov->time); /* modification time */
3248  }
3249  avio_wb32(pb, MOV_TIMESCALE);
3250  (version == 1) ? avio_wb64(pb, max_track_len) : avio_wb32(pb, max_track_len); /* duration of longest track */
3251 
3252  avio_wb32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
3253  avio_wb16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
3254  avio_wb16(pb, 0); /* reserved */
3255  avio_wb32(pb, 0); /* reserved */
3256  avio_wb32(pb, 0); /* reserved */
3257 
3258  /* Matrix structure */
3259  write_matrix(pb, 1, 0, 0, 1, 0, 0);
3260 
3261  avio_wb32(pb, 0); /* reserved (preview time) */
3262  avio_wb32(pb, 0); /* reserved (preview duration) */
3263  avio_wb32(pb, 0); /* reserved (poster time) */
3264  avio_wb32(pb, 0); /* reserved (selection time) */
3265  avio_wb32(pb, 0); /* reserved (selection duration) */
3266  avio_wb32(pb, 0); /* reserved (current time) */
3267  avio_wb32(pb, max_track_id + 1); /* Next track id */
3268  return 0x6c;
3269 }
3270 
3272  AVFormatContext *s)
3273 {
3274  avio_wb32(pb, 33); /* size */
3275  ffio_wfourcc(pb, "hdlr");
3276  avio_wb32(pb, 0);
3277  avio_wb32(pb, 0);
3278  ffio_wfourcc(pb, "mdir");
3279  ffio_wfourcc(pb, "appl");
3280  avio_wb32(pb, 0);
3281  avio_wb32(pb, 0);
3282  avio_w8(pb, 0);
3283  return 33;
3284 }
3285 
3286 /* helper function to write a data tag with the specified string as data */
3287 static int mov_write_string_data_tag(AVIOContext *pb, const char *data, int lang, int long_style)
3288 {
3289  if (long_style) {
3290  int size = 16 + strlen(data);
3291  avio_wb32(pb, size); /* size */
3292  ffio_wfourcc(pb, "data");
3293  avio_wb32(pb, 1);
3294  avio_wb32(pb, 0);
3295  avio_write(pb, data, strlen(data));
3296  return size;
3297  } else {
3298  if (!lang)
3299  lang = ff_mov_iso639_to_lang("und", 1);
3300  avio_wb16(pb, strlen(data)); /* string length */
3301  avio_wb16(pb, lang);
3302  avio_write(pb, data, strlen(data));
3303  return strlen(data) + 4;
3304  }
3305 }
3306 
3307 static int mov_write_string_tag(AVIOContext *pb, const char *name,
3308  const char *value, int lang, int long_style)
3309 {
3310  int size = 0;
3311  if (value && value[0]) {
3312  int64_t pos = avio_tell(pb);
3313  avio_wb32(pb, 0); /* size */
3314  ffio_wfourcc(pb, name);
3315  mov_write_string_data_tag(pb, value, lang, long_style);
3316  size = update_size(pb, pos);
3317  }
3318  return size;
3319 }
3320 
3322  const char *tag, int *lang)
3323 {
3324  int l, len, len2;
3325  AVDictionaryEntry *t, *t2 = NULL;
3326  char tag2[16];
3327 
3328  *lang = 0;
3329 
3330  if (!(t = av_dict_get(s->metadata, tag, NULL, 0)))
3331  return NULL;
3332 
3333  len = strlen(t->key);
3334  snprintf(tag2, sizeof(tag2), "%s-", tag);
3335  while ((t2 = av_dict_get(s->metadata, tag2, t2, AV_DICT_IGNORE_SUFFIX))) {
3336  len2 = strlen(t2->key);
3337  if (len2 == len + 4 && !strcmp(t->value, t2->value)
3338  && (l = ff_mov_iso639_to_lang(&t2->key[len2 - 3], 1)) >= 0) {
3339  *lang = l;
3340  return t;
3341  }
3342  }
3343  return t;
3344 }
3345 
3347  const char *name, const char *tag,
3348  int long_style)
3349 {
3350  int lang;
3351  AVDictionaryEntry *t = get_metadata_lang(s, tag, &lang);
3352  if (!t)
3353  return 0;
3354  return mov_write_string_tag(pb, name, t->value, lang, long_style);
3355 }
3356 
3357 /* iTunes bpm number */
3359 {
3360  AVDictionaryEntry *t = av_dict_get(s->metadata, "tmpo", NULL, 0);
3361  int size = 0, tmpo = t ? atoi(t->value) : 0;
3362  if (tmpo) {
3363  size = 26;
3364  avio_wb32(pb, size);
3365  ffio_wfourcc(pb, "tmpo");
3366  avio_wb32(pb, size-8); /* size */
3367  ffio_wfourcc(pb, "data");
3368  avio_wb32(pb, 0x15); //type specifier
3369  avio_wb32(pb, 0);
3370  avio_wb16(pb, tmpo); // data
3371  }
3372  return size;
3373 }
3374 
3375 /* 3GPP TS 26.244 */
3377 {
3378  int lang;
3379  int64_t pos = avio_tell(pb);
3380  double latitude, longitude, altitude;
3381  int32_t latitude_fix, longitude_fix, altitude_fix;
3382  AVDictionaryEntry *t = get_metadata_lang(s, "location", &lang);
3383  const char *ptr, *place = "";
3384  char *end;
3385  static const char *astronomical_body = "earth";
3386  if (!t)
3387  return 0;
3388 
3389  ptr = t->value;
3390  longitude = strtod(ptr, &end);
3391  if (end == ptr) {
3392  av_log(s, AV_LOG_WARNING, "malformed location metadata\n");
3393  return 0;
3394  }
3395  ptr = end;
3396  latitude = strtod(ptr, &end);
3397  if (end == ptr) {
3398  av_log(s, AV_LOG_WARNING, "malformed location metadata\n");
3399  return 0;
3400  }
3401  ptr = end;
3402  altitude = strtod(ptr, &end);
3403  /* If no altitude was present, the default 0 should be fine */
3404  if (*end == '/')
3405  place = end + 1;
3406 
3407  latitude_fix = (int32_t) ((1 << 16) * latitude);
3408  longitude_fix = (int32_t) ((1 << 16) * longitude);
3409  altitude_fix = (int32_t) ((1 << 16) * altitude);
3410 
3411  avio_wb32(pb, 0); /* size */
3412  ffio_wfourcc(pb, "loci"); /* type */
3413  avio_wb32(pb, 0); /* version + flags */
3414  avio_wb16(pb, lang);
3415  avio_write(pb, place, strlen(place) + 1);
3416  avio_w8(pb, 0); /* role of place (0 == shooting location, 1 == real location, 2 == fictional location) */
3417  avio_wb32(pb, latitude_fix);
3418  avio_wb32(pb, longitude_fix);
3419  avio_wb32(pb, altitude_fix);
3420  avio_write(pb, astronomical_body, strlen(astronomical_body) + 1);
3421  avio_w8(pb, 0); /* additional notes, null terminated string */
3422 
3423  return update_size(pb, pos);
3424 }
3425 
3426 /* iTunes track or disc number */
3428  AVFormatContext *s, int disc)
3429 {
3431  disc ? "disc" : "track",
3432  NULL, 0);
3433  int size = 0, track = t ? atoi(t->value) : 0;
3434  if (track) {
3435  int tracks = 0;
3436  char *slash = strchr(t->value, '/');
3437  if (slash)
3438  tracks = atoi(slash + 1);
3439  avio_wb32(pb, 32); /* size */
3440  ffio_wfourcc(pb, disc ? "disk" : "trkn");
3441  avio_wb32(pb, 24); /* size */
3442  ffio_wfourcc(pb, "data");
3443  avio_wb32(pb, 0); // 8 bytes empty
3444  avio_wb32(pb, 0);
3445  avio_wb16(pb, 0); // empty
3446  avio_wb16(pb, track); // track / disc number
3447  avio_wb16(pb, tracks); // total track / disc number
3448  avio_wb16(pb, 0); // empty
3449  size = 32;
3450  }
3451  return size;
3452 }
3453 
3455  const char *name, const char *tag,
3456  int len)
3457 {
3458  AVDictionaryEntry *t = NULL;
3459  uint8_t num;
3460  int size = 24 + len;
3461 
3462  if (len != 1 && len != 4)
3463  return -1;
3464 
3465  if (!(t = av_dict_get(s->metadata, tag, NULL, 0)))
3466  return 0;
3467  num = atoi(t->value);
3468 
3469  avio_wb32(pb, size);
3470  ffio_wfourcc(pb, name);
3471  avio_wb32(pb, size - 8);
3472  ffio_wfourcc(pb, "data");
3473  avio_wb32(pb, 0x15);
3474  avio_wb32(pb, 0);
3475  if (len==4) avio_wb32(pb, num);
3476  else avio_w8 (pb, num);
3477 
3478  return size;
3479 }
3480 
3482 {
3483  MOVMuxContext *mov = s->priv_data;
3484  int64_t pos = 0;
3485  int i;
3486 
3487  for (i = 0; i < s->nb_streams; i++) {
3488  MOVTrack *trk = &mov->tracks[i];
3489 
3490  if (!is_cover_image(trk->st) || trk->cover_image.size <= 0)
3491  continue;
3492 
3493  if (!pos) {
3494  pos = avio_tell(pb);
3495  avio_wb32(pb, 0);
3496  ffio_wfourcc(pb, "covr");
3497  }
3498  avio_wb32(pb, 16 + trk->cover_image.size);
3499  ffio_wfourcc(pb, "data");
3500  avio_wb32(pb, trk->tag);
3501  avio_wb32(pb , 0);
3502  avio_write(pb, trk->cover_image.data, trk->cover_image.size);
3503  }
3504 
3505  return pos ? update_size(pb, pos) : 0;
3506 }
3507 
3508 /* iTunes meta data list */
3510  AVFormatContext *s)
3511 {
3512  int64_t pos = avio_tell(pb);
3513  avio_wb32(pb, 0); /* size */
3514  ffio_wfourcc(pb, "ilst");
3515  mov_write_string_metadata(s, pb, "\251nam", "title" , 1);
3516  mov_write_string_metadata(s, pb, "\251ART", "artist" , 1);
3517  mov_write_string_metadata(s, pb, "aART", "album_artist", 1);
3518  mov_write_string_metadata(s, pb, "\251wrt", "composer" , 1);
3519  mov_write_string_metadata(s, pb, "\251alb", "album" , 1);
3520  mov_write_string_metadata(s, pb, "\251day", "date" , 1);
3521  if (!mov_write_string_metadata(s, pb, "\251too", "encoding_tool", 1)) {
3522  if (!(s->flags & AVFMT_FLAG_BITEXACT))
3523  mov_write_string_tag(pb, "\251too", LIBAVFORMAT_IDENT, 0, 1);
3524  }
3525  mov_write_string_metadata(s, pb, "\251cmt", "comment" , 1);
3526  mov_write_string_metadata(s, pb, "\251gen", "genre" , 1);
3527  mov_write_string_metadata(s, pb, "cprt", "copyright", 1);
3528  mov_write_string_metadata(s, pb, "\251grp", "grouping" , 1);
3529  mov_write_string_metadata(s, pb, "\251lyr", "lyrics" , 1);
3530  mov_write_string_metadata(s, pb, "desc", "description",1);
3531  mov_write_string_metadata(s, pb, "ldes", "synopsis" , 1);
3532  mov_write_string_metadata(s, pb, "tvsh", "show" , 1);
3533  mov_write_string_metadata(s, pb, "tven", "episode_id",1);
3534  mov_write_string_metadata(s, pb, "tvnn", "network" , 1);
3535  mov_write_string_metadata(s, pb, "keyw", "keywords" , 1);
3536  mov_write_int8_metadata (s, pb, "tves", "episode_sort",4);
3537  mov_write_int8_metadata (s, pb, "tvsn", "season_number",4);
3538  mov_write_int8_metadata (s, pb, "stik", "media_type",1);
3539  mov_write_int8_metadata (s, pb, "hdvd", "hd_video", 1);
3540  mov_write_int8_metadata (s, pb, "pgap", "gapless_playback",1);
3541  mov_write_int8_metadata (s, pb, "cpil", "compilation", 1);
3542  mov_write_covr(pb, s);
3543  mov_write_trkn_tag(pb, mov, s, 0); // track number
3544  mov_write_trkn_tag(pb, mov, s, 1); // disc number
3545  mov_write_tmpo_tag(pb, s);
3546  return update_size(pb, pos);
3547 }
3548 
3550  AVFormatContext *s)
3551 {
3552  avio_wb32(pb, 33); /* size */
3553  ffio_wfourcc(pb, "hdlr");
3554  avio_wb32(pb, 0);
3555  avio_wb32(pb, 0);
3556  ffio_wfourcc(pb, "mdta");
3557  avio_wb32(pb, 0);
3558  avio_wb32(pb, 0);
3559  avio_wb32(pb, 0);
3560  avio_w8(pb, 0);
3561  return 33;
3562 }
3563 
3565  AVFormatContext *s)
3566 {
3567  AVDictionaryEntry *t = NULL;
3568  int64_t pos = avio_tell(pb);
3569  int64_t curpos, entry_pos;
3570  int count = 0;
3571 
3572  avio_wb32(pb, 0); /* size */
3573  ffio_wfourcc(pb, "keys");
3574  avio_wb32(pb, 0);
3575  entry_pos = avio_tell(pb);
3576  avio_wb32(pb, 0); /* entry count */
3577 
3578  while (t = av_dict_get(s->metadata, "", t, AV_DICT_IGNORE_SUFFIX)) {
3579  avio_wb32(pb, strlen(t->key) + 8);
3580  ffio_wfourcc(pb, "mdta");
3581  avio_write(pb, t->key, strlen(t->key));
3582  count += 1;
3583  }
3584  curpos = avio_tell(pb);
3585  avio_seek(pb, entry_pos, SEEK_SET);
3586  avio_wb32(pb, count); // rewrite entry count
3587  avio_seek(pb, curpos, SEEK_SET);
3588 
3589  return update_size(pb, pos);
3590 }
3591 
3593  AVFormatContext *s)
3594 {
3595  AVDictionaryEntry *t = NULL;
3596  int64_t pos = avio_tell(pb);
3597  int count = 1; /* keys are 1-index based */
3598 
3599  avio_wb32(pb, 0); /* size */
3600  ffio_wfourcc(pb, "ilst");
3601 
3602  while (t = av_dict_get(s->metadata, "", t, AV_DICT_IGNORE_SUFFIX)) {
3603  int64_t entry_pos = avio_tell(pb);
3604  avio_wb32(pb, 0); /* size */
3605  avio_wb32(pb, count); /* key */
3606  mov_write_string_data_tag(pb, t->value, 0, 1);
3607  update_size(pb, entry_pos);
3608  count += 1;
3609  }
3610  return update_size(pb, pos);
3611 }
3612 
3613 /* meta data tags */
3615  AVFormatContext *s)
3616 {
3617  int size = 0;
3618  int64_t pos = avio_tell(pb);
3619  avio_wb32(pb, 0); /* size */
3620  ffio_wfourcc(pb, "meta");
3621  avio_wb32(pb, 0);
3622  if (mov->flags & FF_MOV_FLAG_USE_MDTA) {
3623  mov_write_mdta_hdlr_tag(pb, mov, s);
3624  mov_write_mdta_keys_tag(pb, mov, s);
3625  mov_write_mdta_ilst_tag(pb, mov, s);
3626  }
3627  else {
3628  /* iTunes metadata tag */
3629  mov_write_itunes_hdlr_tag(pb, mov, s);
3630  mov_write_ilst_tag(pb, mov, s);
3631  }
3632  size = update_size(pb, pos);
3633  return size;
3634 }
3635 
3637  const char *name, const char *key)
3638 {
3639  int len;
3640  AVDictionaryEntry *t;
3641 
3642  if (!(t = av_dict_get(s->metadata, key, NULL, 0)))
3643  return 0;
3644 
3645  len = strlen(t->value);
3646  if (len > 0) {
3647  int size = len + 8;
3648  avio_wb32(pb, size);
3649  ffio_wfourcc(pb, name);
3650  avio_write(pb, t->value, len);
3651  return size;
3652  }
3653  return 0;
3654 }
3655 
3656 static int ascii_to_wc(AVIOContext *pb, const uint8_t *b)
3657 {
3658  int val;
3659  while (*b) {
3660  GET_UTF8(val, *b++, return -1;)
3661  avio_wb16(pb, val);
3662  }
3663  avio_wb16(pb, 0x00);
3664  return 0;
3665 }
3666 
3667 static uint16_t language_code(const char *str)
3668 {
3669  return (((str[0] - 0x60) & 0x1F) << 10) +
3670  (((str[1] - 0x60) & 0x1F) << 5) +
3671  (( str[2] - 0x60) & 0x1F);
3672 }
3673 
3675  const char *tag, const char *str)
3676 {
3677  int64_t pos = avio_tell(pb);
3678  AVDictionaryEntry *t = av_dict_get(s->metadata, str, NULL, 0);
3679  if (!t || !utf8len(t->value))
3680  return 0;
3681  avio_wb32(pb, 0); /* size */
3682  ffio_wfourcc(pb, tag); /* type */
3683  avio_wb32(pb, 0); /* version + flags */
3684  if (!strcmp(tag, "yrrc"))
3685  avio_wb16(pb, atoi(t->value));
3686  else {
3687  avio_wb16(pb, language_code("eng")); /* language */
3688  avio_write(pb, t->value, strlen(t->value) + 1); /* UTF8 string value */
3689  if (!strcmp(tag, "albm") &&
3690  (t = av_dict_get(s->metadata, "track", NULL, 0)))
3691  avio_w8(pb, atoi(t->value));
3692  }
3693  return update_size(pb, pos);
3694 }
3695 
3697 {
3698  int64_t pos = avio_tell(pb);
3699  int i, nb_chapters = FFMIN(s->nb_chapters, 255);
3700 
3701  avio_wb32(pb, 0); // size
3702  ffio_wfourcc(pb, "chpl");
3703  avio_wb32(pb, 0x01000000); // version + flags
3704  avio_wb32(pb, 0); // unknown
3705  avio_w8(pb, nb_chapters);
3706 
3707  for (i = 0; i < nb_chapters; i++) {
3708  AVChapter *c = s->chapters[i];
3709  AVDictionaryEntry *t;
3710  avio_wb64(pb, av_rescale_q(c->start, c->time_base, (AVRational){1,10000000}));
3711 
3712  if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
3713  int len = FFMIN(strlen(t->value), 255);
3714  avio_w8(pb, len);
3715  avio_write(pb, t->value, len);
3716  } else
3717  avio_w8(pb, 0);
3718  }
3719  return update_size(pb, pos);
3720 }
3721 
3723  AVFormatContext *s)
3724 {
3725  AVIOContext *pb_buf;
3726  int ret, size;
3727  uint8_t *buf;
3728 
3729  ret = avio_open_dyn_buf(&pb_buf);
3730  if (ret < 0)
3731  return ret;
3732 
3733  if (mov->mode & MODE_3GP) {
3734  mov_write_3gp_udta_tag(pb_buf, s, "perf", "artist");
3735  mov_write_3gp_udta_tag(pb_buf, s, "titl", "title");
3736  mov_write_3gp_udta_tag(pb_buf, s, "auth", "author");
3737  mov_write_3gp_udta_tag(pb_buf, s, "gnre", "genre");
3738  mov_write_3gp_udta_tag(pb_buf, s, "dscp", "comment");
3739  mov_write_3gp_udta_tag(pb_buf, s, "albm", "album");
3740  mov_write_3gp_udta_tag(pb_buf, s, "cprt", "copyright");
3741  mov_write_3gp_udta_tag(pb_buf, s, "yrrc", "date");
3742  mov_write_loci_tag(s, pb_buf);
3743  } 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
3744  mov_write_string_metadata(s, pb_buf, "\251ART", "artist", 0);
3745  mov_write_string_metadata(s, pb_buf, "\251nam", "title", 0);
3746  mov_write_string_metadata(s, pb_buf, "\251aut", "author", 0);
3747  mov_write_string_metadata(s, pb_buf, "\251alb", "album", 0);
3748  mov_write_string_metadata(s, pb_buf, "\251day", "date", 0);
3749  mov_write_string_metadata(s, pb_buf, "\251swr", "encoder", 0);
3750  // currently ignored by mov.c
3751  mov_write_string_metadata(s, pb_buf, "\251des", "comment", 0);
3752  // add support for libquicktime, this atom is also actually read by mov.c
3753  mov_write_string_metadata(s, pb_buf, "\251cmt", "comment", 0);
3754  mov_write_string_metadata(s, pb_buf, "\251gen", "genre", 0);
3755  mov_write_string_metadata(s, pb_buf, "\251cpy", "copyright", 0);
3756  mov_write_string_metadata(s, pb_buf, "\251mak", "make", 0);
3757  mov_write_string_metadata(s, pb_buf, "\251mod", "model", 0);
3758  mov_write_string_metadata(s, pb_buf, "\251xyz", "location", 0);
3759  mov_write_string_metadata(s, pb_buf, "\251key", "keywords", 0);
3760  mov_write_raw_metadata_tag(s, pb_buf, "XMP_", "xmp");
3761  } else {
3762  /* iTunes meta data */
3763  mov_write_meta_tag(pb_buf, mov, s);
3764  mov_write_loci_tag(s, pb_buf);
3765  }
3766 
3767  if (s->nb_chapters && !(mov->flags & FF_MOV_FLAG_DISABLE_CHPL))
3768  mov_write_chpl_tag(pb_buf, s);
3769 
3770  if ((size = avio_close_dyn_buf(pb_buf, &buf)) > 0) {
3771  avio_wb32(pb, size + 8);
3772  ffio_wfourcc(pb, "udta");
3773  avio_write(pb, buf, size);
3774  }
3775  av_free(buf);
3776 
3777  return 0;
3778 }
3779 
3781  const char *str, const char *lang, int type)
3782 {
3783  int len = utf8len(str) + 1;
3784  if (len <= 0)
3785  return;
3786  avio_wb16(pb, len * 2 + 10); /* size */
3787  avio_wb32(pb, type); /* type */
3788  avio_wb16(pb, language_code(lang)); /* language */
3789  avio_wb16(pb, 0x01); /* ? */
3790  ascii_to_wc(pb, str);
3791 }
3792 
3794 {
3795  AVDictionaryEntry *title = av_dict_get(s->metadata, "title", NULL, 0);
3796  int64_t pos, pos2;
3797 
3798  if (title) {
3799  pos = avio_tell(pb);
3800  avio_wb32(pb, 0); /* size placeholder*/
3801  ffio_wfourcc(pb, "uuid");
3802  ffio_wfourcc(pb, "USMT");
3803  avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
3804  avio_wb32(pb, 0xbb88695c);
3805  avio_wb32(pb, 0xfac9c740);
3806 
3807  pos2 = avio_tell(pb);
3808  avio_wb32(pb, 0); /* size placeholder*/
3809  ffio_wfourcc(pb, "MTDT");
3810  avio_wb16(pb, 4);
3811 
3812  // ?
3813  avio_wb16(pb, 0x0C); /* size */
3814  avio_wb32(pb, 0x0B); /* type */
3815  avio_wb16(pb, language_code("und")); /* language */
3816  avio_wb16(pb, 0x0); /* ? */
3817  avio_wb16(pb, 0x021C); /* data */
3818 
3819  if (!(s->flags & AVFMT_FLAG_BITEXACT))
3820  mov_write_psp_udta_tag(pb, LIBAVCODEC_IDENT, "eng", 0x04);
3821  mov_write_psp_udta_tag(pb, title->value, "eng", 0x01);
3822  mov_write_psp_udta_tag(pb, "2006/04/01 11:11:11", "und", 0x03);
3823 
3824  update_size(pb, pos2);
3825  return update_size(pb, pos);
3826  }
3827 
3828  return 0;
3829 }
3830 
3831 static void build_chunks(MOVTrack *trk)
3832 {
3833  int i;
3834  MOVIentry *chunk = &trk->cluster[0];
3835  uint64_t chunkSize = chunk->size;
3836  chunk->chunkNum = 1;
3837  if (trk->chunkCount)
3838  return;
3839  trk->chunkCount = 1;
3840  for (i = 1; i<trk->entry; i++){
3841  if (chunk->pos + chunkSize == trk->cluster[i].pos &&
3842  chunkSize + trk->cluster[i].size < (1<<20)){
3843  chunkSize += trk->cluster[i].size;
3844  chunk->samples_in_chunk += trk->cluster[i].entries;
3845  } else {
3846  trk->cluster[i].chunkNum = chunk->chunkNum+1;
3847  chunk=&trk->cluster[i];
3848  chunkSize = chunk->size;
3849  trk->chunkCount++;
3850  }
3851  }
3852 }
3853 
3854 /**
3855  * Assign track ids. If option "use_stream_ids_as_track_ids" is set,
3856  * the stream ids are used as track ids.
3857  *
3858  * This assumes mov->tracks and s->streams are in the same order and
3859  * there are no gaps in either of them (so mov->tracks[n] refers to
3860  * s->streams[n]).
3861  *
3862  * As an exception, there can be more entries in
3863  * s->streams than in mov->tracks, in which case new track ids are
3864  * generated (starting after the largest found stream id).
3865  */
3867 {
3868  int i;
3869 
3870  if (mov->track_ids_ok)
3871  return 0;
3872 
3873  if (mov->use_stream_ids_as_track_ids) {
3874  int next_generated_track_id = 0;
3875  for (i = 0; i < s->nb_streams; i++) {
3876  if (s->streams[i]->id > next_generated_track_id)
3877  next_generated_track_id = s->streams[i]->id;
3878  }
3879 
3880  for (i = 0; i < mov->nb_streams; i++) {
3881  if (mov->tracks[i].entry <= 0 && !(mov->flags & FF_MOV_FLAG_FRAGMENT))
3882  continue;
3883 
3884  mov->tracks[i].track_id = i >= s->nb_streams ? ++next_generated_track_id : s->streams[i]->id;
3885  }
3886  } else {
3887  for (i = 0; i < mov->nb_streams; i++) {
3888  if (mov->tracks[i].entry <= 0 && !(mov->flags & FF_MOV_FLAG_FRAGMENT))
3889  continue;
3890 
3891  mov->tracks[i].track_id = i + 1;
3892  }
3893  }
3894 
3895  mov->track_ids_ok = 1;
3896 
3897  return 0;
3898 }
3899 
3901  AVFormatContext *s)
3902 {
3903  int i;
3904  int64_t pos = avio_tell(pb);
3905  avio_wb32(pb, 0); /* size placeholder*/
3906  ffio_wfourcc(pb, "moov");
3907 
3908  mov_setup_track_ids(mov, s);
3909 
3910  for (i = 0; i < mov->nb_streams; i++) {
3911  if (mov->tracks[i].entry <= 0 && !(mov->flags & FF_MOV_FLAG_FRAGMENT))
3912  continue;
3913 
3914  mov->tracks[i].time = mov->time;
3915 
3916  if (mov->tracks[i].entry)
3917  build_chunks(&mov->tracks[i]);
3918  }
3919 
3920  if (mov->chapter_track)
3921  for (i = 0; i < s->nb_streams; i++) {
3922  mov->tracks[i].tref_tag = MKTAG('c','h','a','p');
3923  mov->tracks[i].tref_id = mov->tracks[mov->chapter_track].track_id;
3924  }
3925  for (i = 0; i < mov->nb_streams; i++) {
3926  MOVTrack *track = &mov->tracks[i];
3927  if (track->tag == MKTAG('r','t','p',' ')) {
3928  track->tref_tag = MKTAG('h','i','n','t');
3929  track->tref_id = mov->tracks[track->src_track].track_id;
3930  } else if (track->par->codec_type == AVMEDIA_TYPE_AUDIO) {
3931  int * fallback, size;
3932  fallback = (int*)av_stream_get_side_data(track->st,
3934  &size);
3935  if (fallback != NULL && size == sizeof(int)) {
3936  if (*fallback >= 0 && *fallback < mov->nb_streams) {
3937  track->tref_tag = MKTAG('f','a','l','l');
3938  track->tref_id = mov->tracks[*fallback].track_id;
3939  }
3940  }
3941  }
3942  }
3943  for (i = 0; i < mov->nb_streams; i++) {
3944  if (mov->tracks[i].tag == MKTAG('t','m','c','d')) {
3945  int src_trk = mov->tracks[i].src_track;
3946  mov->tracks[src_trk].tref_tag = mov->tracks[i].tag;
3947  mov->tracks[src_trk].tref_id = mov->tracks[i].track_id;
3948  //src_trk may have a different timescale than the tmcd track
3949  mov->tracks[i].track_duration = av_rescale(mov->tracks[src_trk].track_duration,
3950  mov->tracks[i].timescale,
3951  mov->tracks[src_trk].timescale);
3952  }
3953  }
3954 
3955  mov_write_mvhd_tag(pb, mov);
3956  if (mov->mode != MODE_MOV && !mov->iods_skip)
3957  mov_write_iods_tag(pb, mov);
3958  for (i = 0; i < mov->nb_streams; i++) {
3959  if (mov->tracks[i].entry > 0 || mov->flags & FF_MOV_FLAG_FRAGMENT) {
3960  int ret = mov_write_trak_tag(s, pb, mov, &(mov->tracks[i]), i < s->nb_streams ? s->streams[i] : NULL);
3961  if (ret < 0)
3962  return ret;
3963  }
3964  }
3965  if (mov->flags & FF_MOV_FLAG_FRAGMENT)
3966  mov_write_mvex_tag(pb, mov); /* QuickTime requires trak to precede this */
3967 
3968  if (mov->mode == MODE_PSP)
3969  mov_write_uuidusmt_tag(pb, s);
3970  else
3971  mov_write_udta_tag(pb, mov, s);
3972 
3973  return update_size(pb, pos);
3974 }
3975 
3976 static void param_write_int(AVIOContext *pb, const char *name, int value)
3977 {
3978  avio_printf(pb, "<param name=\"%s\" value=\"%d\" valuetype=\"data\"/>\n", name, value);
3979 }
3980 
3981 static void param_write_string(AVIOContext *pb, const char *name, const char *value)
3982 {
3983  avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, value);
3984 }
3985 
3986 static void param_write_hex(AVIOContext *pb, const char *name, const uint8_t *value, int len)
3987 {
3988  char buf[150];
3989  len = FFMIN(sizeof(buf) / 2 - 1, len);
3990  ff_data_to_hex(buf, value, len, 0);
3991  buf[2 * len] = '\0';
3992  avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, buf);
3993 }
3994 
3996 {
3997  int64_t pos = avio_tell(pb);
3998  int i;
3999  int64_t manifest_bit_rate = 0;
4000  AVCPBProperties *props = NULL;
4001 
4002  static const uint8_t uuid[] = {
4003  0xa5, 0xd4, 0x0b, 0x30, 0xe8, 0x14, 0x11, 0xdd,
4004  0xba, 0x2f, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66
4005  };
4006 
4007  avio_wb32(pb, 0);
4008  ffio_wfourcc(pb, "uuid");
4009  avio_write(pb, uuid, sizeof(uuid));
4010  avio_wb32(pb, 0);
4011 
4012  avio_printf(pb, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
4013  avio_printf(pb, "<smil xmlns=\"http://www.w3.org/2001/SMIL20/Language\">\n");
4014  avio_printf(pb, "<head>\n");
4015  if (!(mov->fc->flags & AVFMT_FLAG_BITEXACT))
4016  avio_printf(pb, "<meta name=\"creator\" content=\"%s\" />\n",
4018  avio_printf(pb, "</head>\n");
4019  avio_printf(pb, "<body>\n");
4020  avio_printf(pb, "<switch>\n");
4021 
4022  mov_setup_track_ids(mov, s);
4023 
4024  for (i = 0; i < mov->nb_streams; i++) {
4025  MOVTrack *track = &mov->tracks[i];
4026  const char *type;
4027  int track_id = track->track_id;
4028  char track_name_buf[32] = { 0 };
4029 
4030  AVStream *st = track->st;
4031  AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL,0);
4032 
4033  if (track->par->codec_type == AVMEDIA_TYPE_VIDEO && !is_cover_image(st)) {
4034  type = "video";
4035  } else if (track->par->codec_type == AVMEDIA_TYPE_AUDIO) {
4036  type = "audio";
4037  } else {
4038  continue;
4039  }
4040 
4042 
4043  if (track->par->bit_rate) {
4044  manifest_bit_rate = track->par->bit_rate;
4045  } else if (props) {
4046  manifest_bit_rate = props->max_bitrate;
4047  }
4048 
4049  avio_printf(pb, "<%s systemBitrate=\"%"PRId64"\">\n", type,
4050  manifest_bit_rate);
4051  param_write_int(pb, "systemBitrate", manifest_bit_rate);
4052  param_write_int(pb, "trackID", track_id);
4053  param_write_string(pb, "systemLanguage", lang ? lang->value : "und");
4054 
4055  /* Build track name piece by piece: */
4056  /* 1. track type */
4057  av_strlcat(track_name_buf, type, sizeof(track_name_buf));
4058  /* 2. track language, if available */
4059  if (lang)
4060  av_strlcatf(track_name_buf, sizeof(track_name_buf),
4061  "_%s", lang->value);
4062  /* 3. special type suffix */
4063  /* "_cc" = closed captions, "_ad" = audio_description */
4065  av_strlcat(track_name_buf, "_cc", sizeof(track_name_buf));
4067  av_strlcat(track_name_buf, "_ad", sizeof(track_name_buf));
4068 
4069  param_write_string(pb, "trackName", track_name_buf);
4070 
4071  if (track->par->codec_type == AVMEDIA_TYPE_VIDEO) {
4072  if (track->par->codec_id == AV_CODEC_ID_H264) {
4073  uint8_t *ptr;
4074  int size = track->par->extradata_size;
4075  if (!ff_avc_write_annexb_extradata(track->par->extradata, &ptr,
4076  &size)) {
4077  param_write_hex(pb, "CodecPrivateData",
4078  ptr ? ptr : track->par->extradata,
4079  size);
4080  av_free(ptr);
4081  }
4082  param_write_string(pb, "FourCC", "H264");
4083  } else if (track->par->codec_id == AV_CODEC_ID_VC1) {
4084  param_write_string(pb, "FourCC", "WVC1");
4085  param_write_hex(pb, "CodecPrivateData", track->par->extradata,
4086  track->par->extradata_size);
4087  }
4088  param_write_int(pb, "MaxWidth", track->par->width);
4089  param_write_int(pb, "MaxHeight", track->par->height);
4090  param_write_int(pb, "DisplayWidth", track->par->width);
4091  param_write_int(pb, "DisplayHeight", track->par->height);
4092  } else {
4093  if (track->par->codec_id == AV_CODEC_ID_AAC) {
4094  switch (track->par->profile)
4095  {
4096  case FF_PROFILE_AAC_HE_V2:
4097  param_write_string(pb, "FourCC", "AACP");
4098  break;
4099  case FF_PROFILE_AAC_HE:
4100  param_write_string(pb, "FourCC", "AACH");
4101  break;
4102  default:
4103  param_write_string(pb, "FourCC", "AACL");
4104  }
4105  } else if (track->par->codec_id == AV_CODEC_ID_WMAPRO) {
4106  param_write_string(pb, "FourCC", "WMAP");
4107  }
4108  param_write_hex(pb, "CodecPrivateData", track->par->extradata,
4109  track->par->extradata_size);
4111  track->par->codec_id));
4112  param_write_int(pb, "Channels", track->par->channels);
4113  param_write_int(pb, "SamplingRate", track->par->sample_rate);
4114  param_write_int(pb, "BitsPerSample", 16);
4115  param_write_int(pb, "PacketSize", track->par->block_align ?
4116  track->par->block_align : 4);
4117  }
4118  avio_printf(pb, "</%s>\n", type);
4119  }
4120  avio_printf(pb, "</switch>\n");
4121  avio_printf(pb, "</body>\n");
4122  avio_printf(pb, "</smil>\n");
4123 
4124  return update_size(pb, pos);
4125 }
4126 
4128 {
4129  avio_wb32(pb, 16);
4130  ffio_wfourcc(pb, "mfhd");
4131  avio_wb32(pb, 0);
4132  avio_wb32(pb, mov->fragments);
4133  return 0;
4134 }
4135 
4136 static uint32_t get_sample_flags(MOVTrack *track, MOVIentry *entry)
4137 {
4140 }
4141 
4143  MOVTrack *track, int64_t moof_offset)
4144 {
4145  int64_t pos = avio_tell(pb);
4148  if (!track->entry) {
4149  flags |= MOV_TFHD_DURATION_IS_EMPTY;
4150  } else {
4151  flags |= MOV_TFHD_DEFAULT_FLAGS;
4152  }
4154  flags &= ~MOV_TFHD_BASE_DATA_OFFSET;
4155  if (mov->flags & FF_MOV_FLAG_DEFAULT_BASE_MOOF) {
4156  flags &= ~MOV_TFHD_BASE_DATA_OFFSET;
4158  }
4159 
4160  /* Don't set a default sample size, the silverlight player refuses
4161  * to play files with that set. Don't set a default sample duration,
4162  * WMP freaks out if it is set. Don't set a base data offset, PIFF
4163  * file format says it MUST NOT be set. */
4164  if (track->mode == MODE_ISM)
4167 
4168  avio_wb32(pb, 0); /* size placeholder */
4169  ffio_wfourcc(pb, "tfhd");
4170  avio_w8(pb, 0); /* version */
4171  avio_wb24(pb, flags);
4172 
4173  avio_wb32(pb, track->track_id); /* track-id */
4174  if (flags & MOV_TFHD_BASE_DATA_OFFSET)
4175  avio_wb64(pb, moof_offset);
4176  if (flags & MOV_TFHD_DEFAULT_DURATION) {
4177  track->default_duration = get_cluster_duration(track, 0);
4178  avio_wb32(pb, track->default_duration);
4179  }
4180  if (flags & MOV_TFHD_DEFAULT_SIZE) {
4181  track->default_size = track->entry ? track->cluster[0].size : 1;
4182  avio_wb32(pb, track->default_size);
4183  } else
4184  track->default_size = -1;
4185 
4186  if (flags & MOV_TFHD_DEFAULT_FLAGS) {
4187  /* Set the default flags based on the second sample, if available.
4188  * If the first sample is different, that can be signaled via a separate field. */
4189  if (track->entry > 1)
4190  track->default_sample_flags = get_sample_flags(track, &track->cluster[1]);
4191  else
4192  track->default_sample_flags =
4193  track->par->codec_type == AVMEDIA_TYPE_VIDEO ?
4196  avio_wb32(pb, track->default_sample_flags);
4197  }
4198 
4199  return update_size(pb, pos);
4200 }
4201 
4203  MOVTrack *track, int moof_size,
4204  int first, int end)
4205 {
4206  int64_t pos = avio_tell(pb);
4207  uint32_t flags = MOV_TRUN_DATA_OFFSET;
4208  int i;
4209 
4210  for (i = first; i < end; i++) {
4211  if (get_cluster_duration(track, i) != track->default_duration)
4212  flags |= MOV_TRUN_SAMPLE_DURATION;
4213  if (track->cluster[i].size != track->default_size)
4214  flags |= MOV_TRUN_SAMPLE_SIZE;
4215  if (i > first && get_sample_flags(track, &track->cluster[i]) != track->default_sample_flags)
4216  flags |= MOV_TRUN_SAMPLE_FLAGS;
4217  }
4218  if (!(flags & MOV_TRUN_SAMPLE_FLAGS) && track->entry > 0 &&
4219  get_sample_flags(track, &track->cluster[0]) != track->default_sample_flags)
4220  flags |= MOV_TRUN_FIRST_SAMPLE_FLAGS;
4221  if (track->flags & MOV_TRACK_CTTS)
4222  flags |= MOV_TRUN_SAMPLE_CTS;
4223 
4224  avio_wb32(pb, 0); /* size placeholder */
4225  ffio_wfourcc(pb, "trun");
4227  avio_w8(pb, 1); /* version */
4228  else
4229  avio_w8(pb, 0); /* version */
4230  avio_wb24(pb, flags);
4231 
4232  avio_wb32(pb, end - first); /* sample count */
4233  if (mov->flags & FF_MOV_FLAG_OMIT_TFHD_OFFSET &&
4235  !mov->first_trun)
4236  avio_wb32(pb, 0); /* Later tracks follow immediately after the previous one */
4237  else
4238  avio_wb32(pb, moof_size + 8 + track->data_offset +
4239  track->cluster[first].pos); /* data offset */
4240  if (flags & MOV_TRUN_FIRST_SAMPLE_FLAGS)
4241  avio_wb32(pb, get_sample_flags(track, &track->cluster[first]));
4242 
4243  for (i = first; i < end; i++) {
4244  if (flags & MOV_TRUN_SAMPLE_DURATION)
4245  avio_wb32(pb, get_cluster_duration(track, i));
4246  if (flags & MOV_TRUN_SAMPLE_SIZE)
4247  avio_wb32(pb, track->cluster[i].size);
4248  if (flags & MOV_TRUN_SAMPLE_FLAGS)
4249  avio_wb32(pb, get_sample_flags(track, &track->cluster[i]));
4250  if (flags & MOV_TRUN_SAMPLE_CTS)
4251  avio_wb32(pb, track->cluster[i].cts);
4252  }
4253 
4254  mov->first_trun = 0;
4255  return update_size(pb, pos);
4256 }
4257 
4258 static int mov_write_tfxd_tag(AVIOContext *pb, MOVTrack *track)
4259 {
4260  int64_t pos = avio_tell(pb);
4261  static const uint8_t uuid[] = {
4262  0x6d, 0x1d, 0x9b, 0x05, 0x42, 0xd5, 0x44, 0xe6,
4263  0x80, 0xe2, 0x14, 0x1d, 0xaf, 0xf7, 0x57, 0xb2
4264  };
4265 
4266  avio_wb32(pb, 0); /* size placeholder */
4267  ffio_wfourcc(pb, "uuid");
4268  avio_write(pb, uuid, sizeof(uuid));
4269  avio_w8(pb, 1);
4270  avio_wb24(pb, 0);
4271  avio_wb64(pb, track->start_dts + track->frag_start +
4272  track->cluster[0].cts);
4273  avio_wb64(pb, track->end_pts -
4274  (track->cluster[0].dts + track->cluster[0].cts));
4275 
4276  return update_size(pb, pos);
4277 }
4278 
4280  MOVTrack *track, int entry)
4281 {
4282  int n = track->nb_frag_info - 1 - entry, i;
4283  int size = 8 + 16 + 4 + 1 + 16*n;
4284  static const uint8_t uuid[] = {
4285  0xd4, 0x80, 0x7e, 0xf2, 0xca, 0x39, 0x46, 0x95,
4286  0x8e, 0x54, 0x26, 0xcb, 0x9e, 0x46, 0xa7, 0x9f
4287  };
4288 
4289  if (entry < 0)
4290  return 0;
4291 
4292  avio_seek(pb, track->frag_info[entry].tfrf_offset, SEEK_SET);
4293  avio_wb32(pb, size);
4294  ffio_wfourcc(pb, "uuid");
4295  avio_write(pb, uuid, sizeof(uuid));
4296  avio_w8(pb, 1);
4297  avio_wb24(pb, 0);
4298  avio_w8(pb, n);
4299  for (i = 0; i < n; i++) {
4300  int index = entry + 1 + i;
4301  avio_wb64(pb, track->frag_info[index].time);
4302  avio_wb64(pb, track->frag_info[index].duration);
4303  }
4304  if (n < mov->ism_lookahead) {
4305  int free_size = 16 * (mov->ism_lookahead - n);
4306  avio_wb32(pb, free_size);
4307  ffio_wfourcc(pb, "free");
4308  ffio_fill(pb, 0, free_size - 8);
4309  }
4310 
4311  return 0;
4312 }
4313 
4315  MOVTrack *track)
4316 {
4317  int64_t pos = avio_tell(pb);
4318  int i;
4319  for (i = 0; i < mov->ism_lookahead; i++) {
4320  /* Update the tfrf tag for the last ism_lookahead fragments,
4321  * nb_frag_info - 1 is the next fragment to be written. */
4322  mov_write_tfrf_tag(pb, mov, track, track->nb_frag_info - 2 - i);
4323  }
4324  avio_seek(pb, pos, SEEK_SET);
4325  return 0;
4326 }
4327 
4328 static int mov_add_tfra_entries(AVIOContext *pb, MOVMuxContext *mov, int tracks,
4329  int size)
4330 {
4331  int i;
4332  for (i = 0; i < mov->nb_streams; i++) {
4333  MOVTrack *track = &mov->tracks[i];
4334  MOVFragmentInfo *info;
4335  if ((tracks >= 0 && i != tracks) || !track->entry)
4336  continue;
4337  track->nb_frag_info++;
4338  if (track->nb_frag_info >= track->frag_info_capacity) {
4339  unsigned new_capacity = track->nb_frag_info + MOV_FRAG_INFO_ALLOC_INCREMENT;
4340  if (av_reallocp_array(&track->frag_info,
4341  new_capacity,
4342  sizeof(*track->frag_info)))
4343  return AVERROR(ENOMEM);
4344  track->frag_info_capacity = new_capacity;
4345  }
4346  info = &track->frag_info[track->nb_frag_info - 1];
4347  info->offset = avio_tell(pb);
4348  info->size = size;
4349  // Try to recreate the original pts for the first packet
4350  // from the fields we have stored
4351  info->time = track->start_dts + track->frag_start +
4352  track->cluster[0].cts;
4353  info->duration = track->end_pts -
4354  (track->cluster[0].dts + track->cluster[0].cts);
4355  // If the pts is less than zero, we will have trimmed
4356  // away parts of the media track using an edit list,
4357  // and the corresponding start presentation time is zero.
4358  if (info->time < 0) {
4359  info->duration += info->time;
4360  info->time = 0;
4361  }
4362  info->tfrf_offset = 0;
4363  mov_write_tfrf_tags(pb, mov, track);
4364  }
4365  return 0;
4366 }
4367 
4368 static void mov_prune_frag_info(MOVMuxContext *mov, int tracks, int max)
4369 {
4370  int i;
4371  for (i = 0; i < mov->nb_streams; i++) {
4372  MOVTrack *track = &mov->tracks[i];
4373  if ((tracks >= 0 && i != tracks) || !track->entry)
4374  continue;
4375  if (track->nb_frag_info > max) {
4376  memmove(track->frag_info, track->frag_info + (track->nb_frag_info - max), max * sizeof(*track->frag_info));
4377  track->nb_frag_info = max;
4378  }
4379  }
4380 }
4381 
4382 static int mov_write_tfdt_tag(AVIOContext *pb, MOVTrack *track)
4383 {
4384  int64_t pos = avio_tell(pb);
4385 
4386  avio_wb32(pb, 0); /* size */
4387  ffio_wfourcc(pb, "tfdt");
4388  avio_w8(pb, 1); /* version */
4389  avio_wb24(pb, 0);
4390  avio_wb64(pb, track->frag_start);
4391  return update_size(pb, pos);
4392 }
4393 
4395  MOVTrack *track, int64_t moof_offset,
4396  int moof_size)
4397 {
4398  int64_t pos = avio_tell(pb);
4399  int i, start = 0;
4400  avio_wb32(pb, 0); /* size placeholder */
4401  ffio_wfourcc(pb, "traf");
4402 
4403  mov_write_tfhd_tag(pb, mov, track, moof_offset);
4404  if (mov->mode != MODE_ISM)
4405  mov_write_tfdt_tag(pb, track);
4406  for (i = 1; i < track->entry; i++) {
4407  if (track->cluster[i].pos != track->cluster[i - 1].pos + track->cluster[i - 1].size) {
4408  mov_write_trun_tag(pb, mov, track, moof_size, start, i);
4409  start = i;
4410  }
4411  }
4412  mov_write_trun_tag(pb, mov, track, moof_size, start, track->entry);
4413  if (mov->mode == MODE_ISM) {
4414  mov_write_tfxd_tag(pb, track);
4415 
4416  if (mov->ism_lookahead) {
4417  int i, size = 16 + 4 + 1 + 16 * mov->ism_lookahead;
4418 
4419  if (track->nb_frag_info > 0) {
4420  MOVFragmentInfo *info = &track->frag_info[track->nb_frag_info - 1];
4421  if (!info->tfrf_offset)
4422  info->tfrf_offset = avio_tell(pb);
4423  }
4424  avio_wb32(pb, 8 + size);
4425  ffio_wfourcc(pb, "free");
4426  for (i = 0; i < size; i++)
4427  avio_w8(pb, 0);
4428  }
4429  }
4430 
4431  return update_size(pb, pos);
4432 }
4433 
4435  int tracks, int moof_size)
4436 {
4437  int64_t pos = avio_tell(pb);
4438  int i;
4439 
4440  avio_wb32(pb, 0); /* size placeholder */
4441  ffio_wfourcc(pb, "moof");
4442  mov->first_trun = 1;
4443 
4444  mov_write_mfhd_tag(pb, mov);
4445  for (i = 0; i < mov->nb_streams; i++) {
4446  MOVTrack *track = &mov->tracks[i];
4447  if (tracks >= 0 && i != tracks)
4448  continue;
4449  if (!track->entry)
4450  continue;
4451  mov_write_traf_tag(pb, mov, track, pos, moof_size);
4452  }
4453 
4454  return update_size(pb, pos);
4455 }
4456 
4458  MOVTrack *track, int ref_size, int total_sidx_size)
4459 {
4460  int64_t pos = avio_tell(pb), offset_pos, end_pos;
4461  int64_t presentation_time, duration, offset;
4462  unsigned starts_with_SAP;
4463  int i, entries;
4464 
4465  if (track->entry) {
4466  entries = 1;
4467  presentation_time = track->start_dts + track->frag_start +
4468  track->cluster[0].cts;
4469  duration = track->end_pts -
4470  (track->cluster[0].dts + track->cluster[0].cts);
4471  starts_with_SAP = track->cluster[0].flags & MOV_SYNC_SAMPLE;
4472 
4473  // pts<0 should be cut away using edts
4474  if (presentation_time < 0) {
4475  duration += presentation_time;
4476  presentation_time = 0;
4477  }
4478  } else {
4479  entries = track->nb_frag_info;
4480  if (entries <= 0)
4481  return 0;
4482  presentation_time = track->frag_info[0].time;
4483  }
4484 
4485  avio_wb32(pb, 0); /* size */
4486  ffio_wfourcc(pb, "sidx");
4487  avio_w8(pb, 1); /* version */
4488  avio_wb24(pb, 0);
4489  avio_wb32(pb, track->track_id); /* reference_ID */
4490  avio_wb32(pb, track->timescale); /* timescale */
4491  avio_wb64(pb, presentation_time); /* earliest_presentation_time */
4492  offset_pos = avio_tell(pb);
4493  avio_wb64(pb, 0); /* first_offset (offset to referenced moof) */
4494  avio_wb16(pb, 0); /* reserved */
4495 
4496  avio_wb16(pb, entries); /* reference_count */
4497  for (i = 0; i < entries; i++) {
4498  if (!track->entry) {
4499  if (i > 1 && track->frag_info[i].offset != track->frag_info[i - 1].offset + track->frag_info[i - 1].size) {
4500  av_log(NULL, AV_LOG_ERROR, "Non-consecutive fragments, writing incorrect sidx\n");
4501  }
4502  duration = track->frag_info[i].duration;
4503  ref_size = track->frag_info[i].size;
4504  starts_with_SAP = 1;
4505  }
4506  avio_wb32(pb, (0 << 31) | (ref_size & 0x7fffffff)); /* reference_type (0 = media) | referenced_size */
4507  avio_wb32(pb, duration); /* subsegment_duration */
4508  avio_wb32(pb, (starts_with_SAP << 31) | (0 << 28) | 0); /* starts_with_SAP | SAP_type | SAP_delta_time */
4509  }
4510 
4511  end_pos = avio_tell(pb);
4512  offset = pos + total_sidx_size - end_pos;
4513  avio_seek(pb, offset_pos, SEEK_SET);
4514  avio_wb64(pb, offset);
4515  avio_seek(pb, end_pos, SEEK_SET);
4516  return update_size(pb, pos);
4517 }
4518 
4520  int tracks, int ref_size)
4521 {
4522  int i, round, ret;
4523  AVIOContext *avio_buf;
4524  int total_size = 0;
4525  for (round = 0; round < 2; round++) {
4526  // First run one round to calculate the total size of all
4527  // sidx atoms.
4528  // This would be much simpler if we'd only write one sidx
4529  // atom, for the first track in the moof.
4530  if (round == 0) {
4531  if ((ret = ffio_open_null_buf(&avio_buf)) < 0)
4532  return ret;
4533  } else {
4534  avio_buf = pb;
4535  }
4536  for (i = 0; i < mov->nb_streams; i++) {
4537  MOVTrack *track = &mov->tracks[i];
4538  if (tracks >= 0 && i != tracks)
4539  continue;
4540  // When writing a sidx for the full file, entry is 0, but
4541  // we want to include all tracks. ref_size is 0 in this case,
4542  // since we read it from frag_info instead.
4543  if (!track->entry && ref_size > 0)
4544  continue;
4545  total_size -= mov_write_sidx_tag(avio_buf, track, ref_size,
4546  total_size);
4547  }
4548  if (round == 0)
4549  total_size = ffio_close_null_buf(avio_buf);
4550  }
4551  return 0;
4552 }
4553 
4554 static int mov_write_prft_tag(AVIOContext *pb, MOVMuxContext *mov, int tracks)
4555 {
4556  int64_t pos = avio_tell(pb), pts_us, ntp_ts;
4557  MOVTrack *first_track;
4558 
4559  /* PRFT should be associated with at most one track. So, choosing only the
4560  * first track. */
4561  if (tracks > 0)
4562  return 0;
4563  first_track = &(mov->tracks[0]);
4564 
4565  if (!first_track->entry) {
4566  av_log(mov->fc, AV_LOG_WARNING, "Unable to write PRFT, no entries in the track\n");
4567  return 0;
4568  }
4569 
4570  if (first_track->cluster[0].pts == AV_NOPTS_VALUE) {
4571  av_log(mov->fc, AV_LOG_WARNING, "Unable to write PRFT, first PTS is invalid\n");
4572  return 0;
4573  }
4574 
4575  if (mov->write_prft == MOV_PRFT_SRC_WALLCLOCK) {
4577  } else if (mov->write_prft == MOV_PRFT_SRC_PTS) {
4578  pts_us = av_rescale_q(first_track->cluster[0].pts,
4579  first_track->st->time_base, AV_TIME_BASE_Q);
4580  ntp_ts = ff_get_formatted_ntp_time(pts_us + NTP_OFFSET_US);
4581  } else {
4582  av_log(mov->fc, AV_LOG_WARNING, "Unsupported PRFT box configuration: %d\n",
4583  mov->write_prft);
4584  return 0;
4585  }
4586 
4587  avio_wb32(pb, 0); // Size place holder
4588  ffio_wfourcc(pb, "prft"); // Type
4589  avio_w8(pb, 1); // Version
4590  avio_wb24(pb, 0); // Flags
4591  avio_wb32(pb, first_track->track_id); // reference track ID
4592  avio_wb64(pb, ntp_ts); // NTP time stamp
4593  avio_wb64(pb, first_track->cluster[0].pts); //media time
4594  return update_size(pb, pos);
4595 }
4596 
4597 static int mov_write_moof_tag(AVIOContext *pb, MOVMuxContext *mov, int tracks,
4598  int64_t mdat_size)
4599 {
4600  AVIOContext *avio_buf;
4601  int ret, moof_size;
4602 
4603  if ((ret = ffio_open_null_buf(&avio_buf)) < 0)
4604  return ret;
4605  mov_write_moof_tag_internal(avio_buf, mov, tracks, 0);
4606  moof_size = ffio_close_null_buf(avio_buf);
4607 
4608  if (mov->flags & FF_MOV_FLAG_DASH && !(mov->flags & FF_MOV_FLAG_GLOBAL_SIDX))
4609  mov_write_sidx_tags(pb, mov, tracks, moof_size + 8 + mdat_size);
4610 
4611  if (mov->write_prft > MOV_PRFT_NONE && mov->write_prft < MOV_PRFT_NB)
4612  mov_write_prft_tag(pb, mov, tracks);
4613 
4614  if (mov->flags & FF_MOV_FLAG_GLOBAL_SIDX ||
4615  !(mov->flags & FF_MOV_FLAG_SKIP_TRAILER) ||
4616  mov->ism_lookahead) {
4617  if ((ret = mov_add_tfra_entries(pb, mov, tracks, moof_size + 8 + mdat_size)) < 0)
4618  return ret;
4619  if (!(mov->flags & FF_MOV_FLAG_GLOBAL_SIDX) &&
4621  mov_prune_frag_info(mov, tracks, mov->ism_lookahead + 1);
4622  }
4623  }
4624 
4625  return mov_write_moof_tag_internal(pb, mov, tracks, moof_size);
4626 }
4627 
4628 static int mov_write_tfra_tag(AVIOContext *pb, MOVTrack *track)
4629 {
4630  int64_t pos = avio_tell(pb);
4631  int i;
4632 
4633  avio_wb32(pb, 0); /* size placeholder */
4634  ffio_wfourcc(pb, "tfra");
4635  avio_w8(pb, 1); /* version */
4636  avio_wb24(pb, 0);
4637 
4638  avio_wb32(pb, track->track_id);
4639  avio_wb32(pb, 0); /* length of traf/trun/sample num */
4640  avio_wb32(pb, track->nb_frag_info);
4641  for (i = 0; i < track->nb_frag_info; i++) {
4642  avio_wb64(pb, track->frag_info[i].time);
4643  avio_wb64(pb, track->frag_info[i].offset + track->data_offset);
4644  avio_w8(pb, 1); /* traf number */
4645  avio_w8(pb, 1); /* trun number */
4646  avio_w8(pb, 1); /* sample number */
4647  }
4648 
4649  return update_size(pb, pos);
4650 }
4651 
4653 {
4654  int64_t pos = avio_tell(pb);
4655  int i;
4656 
4657  avio_wb32(pb, 0); /* size placeholder */
4658  ffio_wfourcc(pb, "mfra");
4659  /* An empty mfra atom is enough to indicate to the publishing point that
4660  * the stream has ended. */
4661  if (mov->flags & FF_MOV_FLAG_ISML)
4662  return update_size(pb, pos);
4663 
4664  for (i = 0; i < mov->nb_streams; i++) {
4665  MOVTrack *track = &mov->tracks[i];
4666  if (track->nb_frag_info)
4667  mov_write_tfra_tag(pb, track);
4668  }
4669 
4670  avio_wb32(pb, 16);
4671  ffio_wfourcc(pb, "mfro");
4672  avio_wb32(pb, 0); /* version + flags */
4673  avio_wb32(pb, avio_tell(pb) + 4 - pos);
4674 
4675  return update_size(pb, pos);
4676 }
4677 
4679 {
4680  avio_wb32(pb, 8); // placeholder for extended size field (64 bit)
4681  ffio_wfourcc(pb, mov->mode == MODE_MOV ? "wide" : "free");
4682 
4683  mov->mdat_pos = avio_tell(pb);
4684  avio_wb32(pb, 0); /* size placeholder*/
4685  ffio_wfourcc(pb, "mdat");
4686  return 0;
4687 }
4688 
4689 /* TODO: This needs to be more general */
4691 {
4692  MOVMuxContext *mov = s->priv_data;
4693  int64_t pos = avio_tell(pb);
4694  int has_h264 = 0, has_video = 0;
4695  int minor = 0x200;
4696  int i;
4697 
4698  for (i = 0; i < s->nb_streams; i++) {
4699  AVStream *st = s->streams[i];
4700  if (is_cover_image(st))
4701  continue;
4703  has_video = 1;
4704  if (st->codecpar->codec_id == AV_CODEC_ID_H264)
4705  has_h264 = 1;
4706  }
4707 
4708  avio_wb32(pb, 0); /* size */
4709  ffio_wfourcc(pb, "ftyp");
4710 
4711  if (mov->major_brand && strlen(mov->major_brand) >= 4)
4712  ffio_wfourcc(pb, mov->major_brand);
4713  else if (mov->mode == MODE_3GP) {
4714  ffio_wfourcc(pb, has_h264 ? "3gp6" : "3gp4");
4715  minor = has_h264 ? 0x100 : 0x200;
4716  } else if (mov->mode & MODE_3G2) {
4717  ffio_wfourcc(pb, has_h264 ? "3g2b" : "3g2a");
4718  minor = has_h264 ? 0x20000 : 0x10000;
4719  } else if (mov->mode == MODE_PSP)
4720  ffio_wfourcc(pb, "MSNV");
4721  else if (mov->mode == MODE_MP4 && mov->flags & FF_MOV_FLAG_DEFAULT_BASE_MOOF)
4722  ffio_wfourcc(pb, "iso5"); // Required when using default-base-is-moof
4723  else if (mov->mode == MODE_MP4 && mov->flags & FF_MOV_FLAG_NEGATIVE_CTS_OFFSETS)
4724  ffio_wfourcc(pb, "iso4");
4725  else if (mov->mode == MODE_MP4)
4726  ffio_wfourcc(pb, "isom");
4727  else if (mov->mode == MODE_IPOD)
4728  ffio_wfourcc(pb, has_video ? "M4V ":"M4A ");
4729  else if (mov->mode == MODE_ISM)
4730  ffio_wfourcc(pb, "isml");
4731  else if (mov->mode == MODE_F4V)
4732  ffio_wfourcc(pb, "f4v ");
4733  else
4734  ffio_wfourcc(pb, "qt ");
4735 
4736  avio_wb32(pb, minor);
4737 
4738  if (mov->mode == MODE_MOV)
4739  ffio_wfourcc(pb, "qt ");
4740  else if (mov->mode == MODE_ISM) {
4741  ffio_wfourcc(pb, "piff");
4742  } else if (!(mov->flags & FF_MOV_FLAG_DEFAULT_BASE_MOOF)) {
4743  ffio_wfourcc(pb, "isom");
4744  ffio_wfourcc(pb, "iso2");
4745  if (has_h264)
4746  ffio_wfourcc(pb, "avc1");
4747  }
4748 
4749  // We add tfdt atoms when fragmenting, signal this with the iso6 compatible
4750  // brand. This is compatible with users that don't understand tfdt.
4751  if (mov->flags & FF_MOV_FLAG_FRAGMENT && mov->mode != MODE_ISM)
4752  ffio_wfourcc(pb, "iso6");
4753 
4754  if (mov->mode == MODE_3GP)
4755  ffio_wfourcc(pb, has_h264 ? "3gp6":"3gp4");
4756  else if (mov->mode & MODE_3G2)
4757  ffio_wfourcc(pb, has_h264 ? "3g2b":"3g2a");
4758  else if (mov->mode == MODE_PSP)
4759  ffio_wfourcc(pb, "MSNV");
4760  else if (mov->mode == MODE_MP4)
4761  ffio_wfourcc(pb, "mp41");
4762 
4763  if (mov->flags & FF_MOV_FLAG_DASH && mov->flags & FF_MOV_FLAG_GLOBAL_SIDX)
4764  ffio_wfourcc(pb, "dash");
4765 
4766  return update_size(pb, pos);
4767 }
4768 
4770 {
4771  AVStream *video_st = s->streams[0];
4772  AVCodecParameters *video_par = s->streams[0]->codecpar;
4773  AVCodecParameters *audio_par = s->streams[1]->codecpar;
4774  int audio_rate = audio_par->sample_rate;
4775  int64_t frame_rate = video_st->avg_frame_rate.den ?
4776  (video_st->avg_frame_rate.num * 0x10000LL) / video_st->avg_frame_rate.den :
4777  0;
4778  int audio_kbitrate = audio_par->bit_rate / 1000;
4779  int video_kbitrate = FFMIN(video_par->bit_rate / 1000, 800 - audio_kbitrate);
4780 
4781  if (frame_rate < 0 || frame_rate > INT32_MAX) {
4782  av_log(s, AV_LOG_ERROR, "Frame rate %f outside supported range\n", frame_rate / (double)0x10000);
4783  return AVERROR(EINVAL);
4784  }
4785 
4786  avio_wb32(pb, 0x94); /* size */
4787  ffio_wfourcc(pb, "uuid");
4788  ffio_wfourcc(pb, "PROF");
4789 
4790  avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
4791  avio_wb32(pb, 0xbb88695c);
4792  avio_wb32(pb, 0xfac9c740);
4793 
4794  avio_wb32(pb, 0x0); /* ? */
4795  avio_wb32(pb, 0x3); /* 3 sections ? */
4796 
4797  avio_wb32(pb, 0x14); /* size */
4798  ffio_wfourcc(pb, "FPRF");
4799  avio_wb32(pb, 0x0); /* ? */
4800  avio_wb32(pb, 0x0); /* ? */
4801  avio_wb32(pb, 0x0); /* ? */
4802 
4803  avio_wb32(pb, 0x2c); /* size */
4804  ffio_wfourcc(pb, "APRF"); /* audio */
4805  avio_wb32(pb, 0x0);
4806  avio_wb32(pb, 0x2); /* TrackID */
4807  ffio_wfourcc(pb, "mp4a");
4808  avio_wb32(pb, 0x20f);
4809  avio_wb32(pb, 0x0);
4810  avio_wb32(pb, audio_kbitrate);
4811  avio_wb32(pb, audio_kbitrate);
4812  avio_wb32(pb, audio_rate);
4813  avio_wb32(pb, audio_par->channels);
4814 
4815  avio_wb32(pb, 0x34); /* size */
4816  ffio_wfourcc(pb, "VPRF"); /* video */
4817  avio_wb32(pb, 0x0);
4818  avio_wb32(pb, 0x1); /* TrackID */
4819  if (video_par->codec_id == AV_CODEC_ID_H264) {
4820  ffio_wfourcc(pb, "avc1");
4821  avio_wb16(pb, 0x014D);
4822  avio_wb16(pb, 0x0015);
4823  } else {
4824  ffio_wfourcc(pb, "mp4v");
4825  avio_wb16(pb, 0x0000);
4826  avio_wb16(pb, 0x0103);
4827  }
4828  avio_wb32(pb, 0x0);
4829  avio_wb32(pb, video_kbitrate);
4830  avio_wb32(pb, video_kbitrate);
4831  avio_wb32(pb, frame_rate);
4832  avio_wb32(pb, frame_rate);
4833  avio_wb16(pb, video_par->width);
4834  avio_wb16(pb, video_par->height);
4835  avio_wb32(pb, 0x010001); /* ? */
4836 
4837  return 0;
4838 }
4839 
4841 {
4842  MOVMuxContext *mov = s->priv_data;
4843  int i;
4844 
4845  mov_write_ftyp_tag(pb,s);
4846  if (mov->mode == MODE_PSP) {
4847  int video_streams_nb = 0, audio_streams_nb = 0, other_streams_nb = 0;
4848  for (i = 0; i < s->nb_streams; i++) {
4849  AVStream *st = s->streams[i];
4850  if (is_cover_image(st))
4851  continue;
4853  video_streams_nb++;
4854  else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
4855  audio_streams_nb++;
4856  else
4857  other_streams_nb++;
4858  }
4859 
4860  if (video_streams_nb != 1 || audio_streams_nb != 1 || other_streams_nb) {
4861  av_log(s, AV_LOG_ERROR, "PSP mode need one video and one audio stream\n");
4862  return AVERROR(EINVAL);
4863  }
4864  return mov_write_uuidprof_tag(pb, s);
4865  }
4866  return 0;
4867 }
4868 
4869 static int mov_parse_mpeg2_frame(AVPacket *pkt, uint32_t *flags)
4870 {
4871  uint32_t c = -1;
4872  int i, closed_gop = 0;
4873 
4874  for (i = 0; i < pkt->size - 4; i++) {
4875  c = (c << 8) + pkt->data[i];
4876  if (c == 0x1b8) { // gop
4877  closed_gop = pkt->data[i + 4] >> 6 & 0x01;
4878  } else if (c == 0x100) { // pic
4879  int temp_ref = (pkt->data[i + 1] << 2) | (pkt->data[i + 2] >> 6);
4880  if (!temp_ref || closed_gop) // I picture is not reordered
4881  *flags = MOV_SYNC_SAMPLE;
4882  else
4883  *flags = MOV_PARTIAL_SYNC_SAMPLE;
4884  break;
4885  }
4886  }
4887  return 0;
4888 }
4889 
4891 {
4892  const uint8_t *start, *next, *end = pkt->data + pkt->size;
4893  int seq = 0, entry = 0;
4894  int key = pkt->flags & AV_PKT_FLAG_KEY;
4895  start = find_next_marker(pkt->data, end);
4896  for (next = start; next < end; start = next) {
4897  next = find_next_marker(start + 4, end);
4898  switch (AV_RB32(start)) {
4899  case VC1_CODE_SEQHDR:
4900  seq = 1;
4901  break;
4902  case VC1_CODE_ENTRYPOINT:
4903  entry = 1;
4904  break;
4905  case VC1_CODE_SLICE:
4906  trk->vc1_info.slices = 1;
4907  break;
4908  }
4909  }
4910  if (!trk->entry && trk->vc1_info.first_packet_seen)
4911  trk->vc1_info.first_frag_written = 1;
4912  if (!trk->entry && !trk->vc1_info.first_frag_written) {
4913  /* First packet in first fragment */
4914  trk->vc1_info.first_packet_seq = seq;
4915  trk->vc1_info.first_packet_entry = entry;
4916  trk->vc1_info.first_packet_seen = 1;
4917  } else if ((seq && !trk->vc1_info.packet_seq) ||
4918  (entry && !trk->vc1_info.packet_entry)) {
4919  int i;
4920  for (i = 0; i < trk->entry; i++)
4921  trk->cluster[i].flags &= ~MOV_SYNC_SAMPLE;
4922  trk->has_keyframes = 0;
4923  if (seq)
4924  trk->vc1_info.packet_seq = 1;
4925  if (entry)
4926  trk->vc1_info.packet_entry = 1;
4927  if (!trk->vc1_info.first_frag_written) {
4928  /* First fragment */
4929  if ((!seq || trk->vc1_info.first_packet_seq) &&
4930  (!entry || trk->vc1_info.first_packet_entry)) {
4931  /* First packet had the same headers as this one, readd the
4932  * sync sample flag. */
4933  trk->cluster[0].flags |= MOV_SYNC_SAMPLE;
4934  trk->has_keyframes = 1;
4935  }
4936  }
4937  }
4938  if (trk->vc1_info.packet_seq && trk->vc1_info.packet_entry)
4939  key = seq && entry;
4940  else if (trk->vc1_info.packet_seq)
4941  key = seq;
4942  else if (trk->vc1_info.packet_entry)
4943  key = entry;
4944  if (key) {
4945  trk->cluster[trk->entry].flags |= MOV_SYNC_SAMPLE;
4946  trk->has_keyframes++;
4947  }
4948 }
4949 
4951 {
4952  MOVMuxContext *mov = s->priv_data;
4953  int ret, buf_size;
4954  uint8_t *buf;
4955  int i, offset;
4956 
4957  if (!track->mdat_buf)
4958  return 0;
4959  if (!mov->mdat_buf) {
4960  if ((ret = avio_open_dyn_buf(&mov->mdat_buf)) < 0)
4961  return ret;
4962  }
4963  buf_size = avio_close_dyn_buf(track->mdat_buf, &buf);
4964  track->mdat_buf = NULL;
4965 
4966  offset = avio_tell(mov->mdat_buf);
4967  avio_write(mov->mdat_buf, buf, buf_size);
4968  av_free(buf);
4969 
4970  for (i = track->entries_flushed; i < track->entry; i++)
4971  track->cluster[i].pos += offset;
4972  track->entries_flushed = track->entry;
4973  return 0;
4974 }
4975 
4976 static int mov_flush_fragment(AVFormatContext *s, int force)
4977 {
4978  MOVMuxContext *mov = s->priv_data;
4979  int i, first_track = -1;
4980  int64_t mdat_size = 0;
4981  int ret;
4982  int has_video = 0, starts_with_key = 0, first_video_track = 1;
4983 
4984  if (!(mov->flags & FF_MOV_FLAG_FRAGMENT))
4985  return 0;
4986 
4987  // Try to fill in the duration of the last packet in each stream
4988  // from queued packets in the interleave queues. If the flushing
4989  // of fragments was triggered automatically by an AVPacket, we
4990  // already have reliable info for the end of that track, but other
4991  // tracks may need to be filled in.
4992  for (i = 0; i < s->nb_streams; i++) {
4993  MOVTrack *track = &mov->tracks[i];
4994  if (!track->end_reliable) {
4995  AVPacket pkt;
4996  if (!ff_interleaved_peek(s, i, &pkt, 1)) {
4997  if (track->dts_shift != AV_NOPTS_VALUE)
4998  pkt.dts += track->dts_shift;
4999  track->track_duration = pkt.dts - track->start_dts;
5000  if (pkt.pts != AV_NOPTS_VALUE)
5001  track->end_pts = pkt.pts;
5002  else
5003  track->end_pts = pkt.dts;
5004  }
5005  }
5006  }
5007 
5008  for (i = 0; i < mov->nb_streams; i++) {
5009  MOVTrack *track = &mov->tracks[i];
5010  if (track->entry <= 1)
5011  continue;
5012  // Sample durations are calculated as the diff of dts values,
5013  // but for the last sample in a fragment, we don't know the dts
5014  // of the first sample in the next fragment, so we have to rely
5015  // on what was set as duration in the AVPacket. Not all callers
5016  // set this though, so we might want to replace it with an
5017  // estimate if it currently is zero.
5018  if (get_cluster_duration(track, track->entry - 1) != 0)
5019  continue;
5020  // Use the duration (i.e. dts diff) of the second last sample for
5021  // the last one. This is a wild guess (and fatal if it turns out
5022  // to be too long), but probably the best we can do - having a zero
5023  // duration is bad as well.
5024  track->track_duration += get_cluster_duration(track, track->entry - 2);
5025  track->end_pts += get_cluster_duration(track, track->entry - 2);
5026  if (!mov->missing_duration_warned) {
5028  "Estimating the duration of the last packet in a "
5029  "fragment, consider setting the duration field in "
5030  "AVPacket instead.\n");
5031  mov->missing_duration_warned = 1;
5032  }
5033  }
5034 
5035  if (!mov->moov_written) {
5036  int64_t pos = avio_tell(s->pb);
5037  uint8_t *buf;
5038  int buf_size, moov_size;
5039 
5040  for (i = 0; i < mov->nb_streams; i++)
5041  if (!mov->tracks[i].entry && !is_cover_image(mov->tracks[i].st))
5042  break;
5043  /* Don't write the initial moov unless all tracks have data */
5044  if (i < mov->nb_streams && !force)
5045  return 0;
5046 
5047  moov_size = get_moov_size(s);
5048  for (i = 0; i < mov->nb_streams; i++)
5049  mov->tracks[i].data_offset = pos + moov_size + 8;
5050 
5052  if (mov->flags & FF_MOV_FLAG_DELAY_MOOV)
5054  if ((ret = mov_write_moov_tag(s->pb, mov, s)) < 0)
5055  return ret;
5056 
5057  if (mov->flags & FF_MOV_FLAG_DELAY_MOOV) {
5058  if (mov->flags & FF_MOV_FLAG_GLOBAL_SIDX)
5059  mov->reserved_header_pos = avio_tell(s->pb);
5060  avio_flush(s->pb);
5061  mov->moov_written = 1;
5062  return 0;
5063  }
5064 
5065  buf_size = avio_close_dyn_buf(mov->mdat_buf, &buf);
5066  mov->mdat_buf = NULL;
5067  avio_wb32(s->pb, buf_size + 8);
5068  ffio_wfourcc(s->pb, "mdat");
5069  avio_write(s->pb, buf, buf_size);
5070  av_free(buf);
5071 
5072  if (mov->flags & FF_MOV_FLAG_GLOBAL_SIDX)
5073  mov->reserved_header_pos = avio_tell(s->pb);
5074 
5075  mov->moov_written = 1;
5076  mov->mdat_size = 0;
5077  for (i = 0; i < mov->nb_streams; i++) {
5078  if (mov->tracks[i].entry)
5079  mov->tracks[i].frag_start += mov->tracks[i].start_dts +
5080  mov->tracks[i].track_duration -
5081  mov->tracks[i].cluster[0].dts;
5082  mov->tracks[i].entry = 0;
5083  mov->tracks[i].end_reliable = 0;
5084  }
5085  avio_flush(s->pb);
5086  return 0;
5087  }
5088 
5089  if (mov->frag_interleave) {
5090  for (i = 0; i < mov->nb_streams; i++) {
5091  MOVTrack *track = &mov->tracks[i];
5092  int ret;
5093  if ((ret = mov_flush_fragment_interleaving(s, track)) < 0)
5094  return ret;
5095  }
5096 
5097  if (!mov->mdat_buf)
5098  return 0;
5099  mdat_size = avio_tell(mov->mdat_buf);
5100  }
5101 
5102  for (i = 0; i < mov->nb_streams; i++) {
5103  MOVTrack *track = &mov->tracks[i];
5104  if (mov->flags & FF_MOV_FLAG_SEPARATE_MOOF || mov->frag_interleave)
5105  track->data_offset = 0;
5106  else
5107  track->data_offset = mdat_size;
5108  if (track->par->codec_type == AVMEDIA_TYPE_VIDEO) {
5109  has_video = 1;
5110  if (first_video_track) {
5111  if (track->entry)
5112  starts_with_key = track->cluster[0].flags & MOV_SYNC_SAMPLE;
5113  first_video_track = 0;
5114  }
5115  }
5116  if (!track->entry)
5117  continue;
5118  if (track->mdat_buf)
5119  mdat_size += avio_tell(track->mdat_buf);
5120  if (first_track < 0)
5121  first_track = i;
5122  }
5123 
5124  if (!mdat_size)
5125  return 0;
5126 
5127  avio_write_marker(s->pb,
5128  av_rescale(mov->tracks[first_track].cluster[0].dts, AV_TIME_BASE, mov->tracks[first_track].timescale),
5129  (has_video ? starts_with_key : mov->tracks[first_track].cluster[0].flags & MOV_SYNC_SAMPLE) ? AVIO_DATA_MARKER_SYNC_POINT : AVIO_DATA_MARKER_BOUNDARY_POINT);
5130 
5131  for (i = 0; i < mov->nb_streams; i++) {
5132  MOVTrack *track = &mov->tracks[i];
5133  int buf_size, write_moof = 1, moof_tracks = -1;
5134  uint8_t *buf;
5135  int64_t duration = 0;
5136 
5137  if (track->entry)
5138  duration = track->start_dts + track->track_duration -
5139  track->cluster[0].dts;
5140  if (mov->flags & FF_MOV_FLAG_SEPARATE_MOOF) {
5141  if (!track->mdat_buf)
5142  continue;
5143  mdat_size = avio_tell(track->mdat_buf);
5144  moof_tracks = i;
5145  } else {
5146  write_moof = i == first_track;
5147  }
5148 
5149  if (write_moof) {
5150  avio_flush(s->pb);
5151 
5152  mov_write_moof_tag(s->pb, mov, moof_tracks, mdat_size);
5153  mov->fragments++;
5154 
5155  avio_wb32(s->pb, mdat_size + 8);
5156  ffio_wfourcc(s->pb, "mdat");
5157  }
5158 
5159  if (track->entry)
5160  track->frag_start += duration;
5161  track->entry = 0;
5162  track->entries_flushed = 0;
5163  track->end_reliable = 0;
5164  if (!mov->frag_interleave) {
5165  if (!track->mdat_buf)
5166  continue;
5167  buf_size = avio_close_dyn_buf(track->mdat_buf, &buf);
5168  track->mdat_buf = NULL;
5169  } else {
5170  if (!mov->mdat_buf)
5171  continue;
5172  buf_size = avio_close_dyn_buf(mov->mdat_buf, &buf);
5173  mov->mdat_buf = NULL;
5174  }
5175 
5176  avio_write(s->pb, buf, buf_size);
5177  av_free(buf);
5178  }
5179 
5180  mov->mdat_size = 0;
5181 
5182  avio_flush(s->pb);
5183  return 0;
5184 }
5185 
5187 {
5188  MOVMuxContext *mov = s->priv_data;
5189  int had_moov = mov->moov_written;
5190  int ret = mov_flush_fragment(s, force);
5191  if (ret < 0)
5192  return ret;
5193  // If using delay_moov, the first flush only wrote the moov,
5194  // not the actual moof+mdat pair, thus flush once again.
5195  if (!had_moov && mov->flags & FF_MOV_FLAG_DELAY_MOOV)
5196  ret = mov_flush_fragment(s, force);
5197  return ret;
5198 }
5199 
5201 {
5202  MOVMuxContext *mov = s->priv_data;
5203  MOVTrack *trk = &mov->tracks[pkt->stream_index];
5204  int64_t ref;
5205  uint64_t duration;
5206 
5207  if (trk->entry) {
5208  ref = trk->cluster[trk->entry - 1].dts;
5209  } else if ( trk->start_dts != AV_NOPTS_VALUE
5210  && !trk->frag_discont) {
5211  ref = trk->start_dts + trk->track_duration;
5212  } else
5213  ref = pkt->dts; // Skip tests for the first packet
5214 
5215  if (trk->dts_shift != AV_NOPTS_VALUE) {
5216  /* With negative CTS offsets we have set an offset to the DTS,
5217  * reverse this for the check. */
5218  ref -= trk->dts_shift;
5219  }
5220 
5221  duration = pkt->dts - ref;
5222  if (pkt->dts < ref || duration >= INT_MAX) {
5223  av_log(s, AV_LOG_ERROR, "Application provided duration: %"PRId64" / timestamp: %"PRId64" is out of range for mov/mp4 format\n",
5224  duration, pkt->dts
5225  );
5226 
5227  pkt->dts = ref + 1;
5228  pkt->pts = AV_NOPTS_VALUE;
5229  }
5230 
5231  if (pkt->duration < 0 || pkt->duration > INT_MAX) {
5232  av_log(s, AV_LOG_ERROR, "Application provided duration: %"PRId64" is invalid\n", pkt->duration);
5233  return AVERROR(EINVAL);
5234  }
5235  return 0;
5236 }
5237 
5239 {
5240  MOVMuxContext *mov = s->priv_data;
5241  AVIOContext *pb = s->pb;
5242  MOVTrack *trk = &mov->tracks[pkt->stream_index];
5243  AVCodecParameters *par = trk->par;
5244  unsigned int samples_in_chunk = 0;
5245  int size = pkt->size, ret = 0;
5246  uint8_t *reformatted_data = NULL;
5247 
5248  ret = check_pkt(s, pkt);
5249  if (ret < 0)
5250  return ret;
5251 
5252  if (mov->flags & FF_MOV_FLAG_FRAGMENT) {
5253  int ret;
5254  if (mov->moov_written || mov->flags & FF_MOV_FLAG_EMPTY_MOOV) {
5255  if (mov->frag_interleave && mov->fragments > 0) {
5256  if (trk->entry - trk->entries_flushed >= mov->frag_interleave) {
5257  if ((ret = mov_flush_fragment_interleaving(s, trk)) < 0)
5258  return ret;
5259  }
5260  }
5261 
5262  if (!trk->mdat_buf) {
5263  if ((ret = avio_open_dyn_buf(&trk->mdat_buf)) < 0)
5264  return ret;
5265  }
5266  pb = trk->mdat_buf;
5267  } else {
5268  if (!mov->mdat_buf) {
5269  if ((ret = avio_open_dyn_buf(&mov->mdat_buf)) < 0)
5270  return ret;
5271  }
5272  pb = mov->mdat_buf;
5273  }
5274  }
5275 
5276  if (par->codec_id == AV_CODEC_ID_AMR_NB) {
5277  /* We must find out how many AMR blocks there are in one packet */
5278  static const uint16_t packed_size[16] =
5279  {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 1};
5280  int len = 0;
5281 
5282  while (len < size && samples_in_chunk < 100) {
5283  len += packed_size[(pkt->data[len] >> 3) & 0x0F];
5284  samples_in_chunk++;
5285  }
5286  if (samples_in_chunk > 1) {
5287  av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, implement a AVParser for it\n");
5288  return -1;
5289  }
5290  } else if (par->codec_id == AV_CODEC_ID_ADPCM_MS ||
5292  samples_in_chunk = trk->par->frame_size;
5293  } else if (trk->sample_size)
5294  samples_in_chunk = size / trk->sample_size;
5295  else
5296  samples_in_chunk = 1;
5297 
5298  if (samples_in_chunk < 1) {
5299  av_log(s, AV_LOG_ERROR, "fatal error, input packet contains no samples\n");
5300  return AVERROR_PATCHWELCOME;
5301  }
5302 
5303  /* copy extradata if it exists */
5304  if (trk->vos_len == 0 && par->extradata_size > 0 &&
5305  !TAG_IS_AVCI(trk->tag) &&
5306  (par->codec_id != AV_CODEC_ID_DNXHD)) {
5307  trk->vos_len = par->extradata_size;
5309  if (!trk->vos_data) {
5310  ret = AVERROR(ENOMEM);
5311  goto err;
5312  }
5313  memcpy(trk->vos_data, par->extradata, trk->vos_len);
5314  memset(trk->vos_data + trk->vos_len, 0, AV_INPUT_BUFFER_PADDING_SIZE);
5315  }
5316 
5317  if (par->codec_id == AV_CODEC_ID_AAC && pkt->size > 2 &&
5318  (AV_RB16(pkt->data) & 0xfff0) == 0xfff0) {
5319  if (!s->streams[pkt->stream_index]->nb_frames) {
5320  av_log(s, AV_LOG_ERROR, "Malformed AAC bitstream detected: "
5321  "use the audio bitstream filter 'aac_adtstoasc' to fix it "
5322  "('-bsf:a aac_adtstoasc' option with ffmpeg)\n");
5323  return -1;
5324  }
5325  av_log(s, AV_LOG_WARNING, "aac bitstream error\n");
5326  }
5327  if (par->codec_id == AV_CODEC_ID_H264 && trk->vos_len > 0 && *(uint8_t *)trk->vos_data != 1 && !TAG_IS_AVCI(trk->tag)) {
5328  /* from x264 or from bytestream H.264 */
5329  /* NAL reformatting needed */
5330  if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams) {
5331  ff_avc_parse_nal_units_buf(pkt->data, &reformatted_data,
5332  &size);
5333  avio_write(pb, reformatted_data, size);
5334  } else {
5335  if (trk->cenc.aes_ctr) {
5336  size = ff_mov_cenc_avc_parse_nal_units(&trk->cenc, pb, pkt->data, size);
5337  if (size < 0) {
5338  ret = size;
5339  goto err;
5340  }
5341  } else {
5342  size = ff_avc_parse_nal_units(pb, pkt->data, pkt->size);
5343  }
5344  }
5345  } else if (par->codec_id == AV_CODEC_ID_HEVC && trk->vos_len > 6 &&
5346  (AV_RB24(trk->vos_data) == 1 || AV_RB32(trk->vos_data) == 1)) {
5347  /* extradata is Annex B, assume the bitstream is too and convert it */
5348  if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams) {
5349  ff_hevc_annexb2mp4_buf(pkt->data, &reformatted_data, &size, 0, NULL);
5350  avio_write(pb, reformatted_data, size);
5351  } else {
5352  size = ff_hevc_annexb2mp4(pb, pkt->data, pkt->size, 0, NULL);
5353  }
5354  } else if (par->codec_id == AV_CODEC_ID_AV1) {
5355  if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams) {
5356  ff_av1_filter_obus_buf(pkt->data, &reformatted_data, &size);
5357  avio_write(pb, reformatted_data, size);
5358  } else {
5359  size = ff_av1_filter_obus(pb, pkt->data, pkt->size);
5360  }
5361 #if CONFIG_AC3_PARSER
5362  } else if (par->codec_id == AV_CODEC_ID_EAC3) {
5363  size = handle_eac3(mov, pkt, trk);
5364  if (size < 0)
5365  return size;
5366  else if (!size)
5367  goto end;
5368  avio_write(pb, pkt->data, size);
5369 #endif
5370  } else {
5371  if (trk->cenc.aes_ctr) {
5372  if (par->codec_id == AV_CODEC_ID_H264 && par->extradata_size > 4) {
5373  int nal_size_length = (par->extradata[4] & 0x3) + 1;
5374  ret = ff_mov_cenc_avc_write_nal_units(s, &trk->cenc, nal_size_length, pb, pkt->data, size);
5375  } else {
5376  ret = ff_mov_cenc_write_packet(&trk->cenc, pb, pkt->data, size);
5377  }
5378 
5379  if (ret) {
5380  goto err;
5381  }
5382  } else {
5383  avio_write(pb, pkt->data, size);
5384  }
5385  }
5386 
5387  if ((par->codec_id == AV_CODEC_ID_DNXHD ||
5388  par->codec_id == AV_CODEC_ID_AC3) && !trk->vos_len) {
5389  /* copy frame to create needed atoms */
5390  trk->vos_len = size;
5392  if (!trk->vos_data) {
5393  ret = AVERROR(ENOMEM);
5394  goto err;
5395  }
5396  memcpy(trk->vos_data, pkt->data, size);
5397  memset(trk->vos_data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
5398  }
5399 
5400  if (trk->entry >= trk->cluster_capacity) {
5401  unsigned new_capacity = 2 * (trk->entry + MOV_INDEX_CLUSTER_SIZE);
5402  void *cluster = av_realloc_array(trk->cluster, new_capacity, sizeof(*trk->cluster));
5403  if (!cluster) {
5404  ret = AVERROR(ENOMEM);
5405  goto err;
5406  }
5407  trk->cluster = cluster;
5408  trk->cluster_capacity = new_capacity;
5409  }
5410 
5411  trk->cluster[trk->entry].pos = avio_tell(pb) - size;
5412  trk->cluster[trk->entry].samples_in_chunk = samples_in_chunk;
5413  trk->cluster[trk->entry].chunkNum = 0;
5414  trk->cluster[trk->entry].size = size;
5415  trk->cluster[trk->entry].entries = samples_in_chunk;
5416  trk->cluster[trk->entry].dts = pkt->dts;
5417  trk->cluster[trk->entry].pts = pkt->pts;
5418  if (!trk->entry && trk->start_dts != AV_NOPTS_VALUE) {
5419  if (!trk->frag_discont) {
5420  /* First packet of a new fragment. We already wrote the duration
5421  * of the last packet of the previous fragment based on track_duration,
5422  * which might not exactly match our dts. Therefore adjust the dts
5423  * of this packet to be what the previous packets duration implies. */
5424  trk->cluster[trk->entry].dts = trk->start_dts + trk->track_duration;
5425  /* We also may have written the pts and the corresponding duration
5426  * in sidx/tfrf/tfxd tags; make sure the sidx pts and duration match up with
5427  * the next fragment. This means the cts of the first sample must
5428  * be the same in all fragments, unless end_pts was updated by
5429  * the packet causing the fragment to be written. */
5430  if ((mov->flags & FF_MOV_FLAG_DASH && !(mov->flags & FF_MOV_FLAG_GLOBAL_SIDX)) ||
5431  mov->mode == MODE_ISM)
5432  pkt->pts = pkt->dts + trk->end_pts - trk->cluster[trk->entry].dts;
5433  } else {
5434  /* New fragment, but discontinuous from previous fragments.
5435  * Pretend the duration sum of the earlier fragments is
5436  * pkt->dts - trk->start_dts. */
5437  trk->frag_start = pkt->dts - trk->start_dts;
5438  trk->end_pts = AV_NOPTS_VALUE;
5439  trk->frag_discont = 0;
5440  }
5441  }
5442 
5443  if (!trk->entry && trk->start_dts == AV_NOPTS_VALUE && !mov->use_editlist &&
5445  /* Not using edit lists and shifting the first track to start from zero.
5446  * If the other streams start from a later timestamp, we won't be able
5447  * to signal the difference in starting time without an edit list.
5448  * Thus move the timestamp for this first sample to 0, increasing
5449  * its duration instead. */
5450  trk->cluster[trk->entry].dts = trk->start_dts = 0;
5451  }
5452  if (trk->start_dts == AV_NOPTS_VALUE) {
5453  trk->start_dts = pkt->dts;
5454  if (trk->frag_discont) {
5455  if (mov->use_editlist) {
5456  /* Pretend the whole stream started at pts=0, with earlier fragments
5457  * already written. If the stream started at pts=0, the duration sum
5458  * of earlier fragments would have been pkt->pts. */
5459  trk->frag_start = pkt->pts;
5460  trk->start_dts = pkt->dts - pkt->pts;
5461  } else {
5462  /* Pretend the whole stream started at dts=0, with earlier fragments
5463  * already written, with a duration summing up to pkt->dts. */
5464  trk->frag_start = pkt->dts;
5465  trk->start_dts = 0;
5466  }
5467  trk->frag_discont = 0;
5468  } else if (pkt->dts && mov->moov_written)
5470  "Track %d starts with a nonzero dts %"PRId64", while the moov "
5471  "already has been written. Set the delay_moov flag to handle "
5472  "this case.\n",
5473  pkt->stream_index, pkt->dts);
5474  }
5475  trk->track_duration = pkt->dts - trk->start_dts + pkt->duration;
5476  trk->last_sample_is_subtitle_end = 0;
5477 
5478  if (pkt->pts == AV_NOPTS_VALUE) {
5479  av_log(s, AV_LOG_WARNING, "pts has no value\n");
5480  pkt->pts = pkt->dts;
5481  }
5482  if (pkt->dts != pkt->pts)
5483  trk->flags |= MOV_TRACK_CTTS;
5484  trk->cluster[trk->entry].cts = pkt->pts - pkt->dts;
5485  trk->cluster[trk->entry].flags = 0;
5486  if (trk->start_cts == AV_NOPTS_VALUE)
5487  trk->start_cts = pkt->pts - pkt->dts;
5488  if (trk->end_pts == AV_NOPTS_VALUE)
5489  trk->end_pts = trk->cluster[trk->entry].dts +
5490  trk->cluster[trk->entry].cts + pkt->duration;
5491  else
5492  trk->end_pts = FFMAX(trk->end_pts, trk->cluster[trk->entry].dts +
5493  trk->cluster[trk->entry].cts +
5494  pkt->duration);
5495 
5496  if (par->codec_id == AV_CODEC_ID_VC1) {
5497  mov_parse_vc1_frame(pkt, trk);
5498  } else if (pkt->flags & AV_PKT_FLAG_KEY) {
5499  if (mov->mode == MODE_MOV && par->codec_id == AV_CODEC_ID_MPEG2VIDEO &&
5500  trk->entry > 0) { // force sync sample for the first key frame
5501  mov_parse_mpeg2_frame(pkt, &trk->cluster[trk->entry].flags);
5502  if (trk->cluster[trk->entry].flags & MOV_PARTIAL_SYNC_SAMPLE)
5503  trk->flags |= MOV_TRACK_STPS;
5504  } else {
5505  trk->cluster[trk->entry].flags = MOV_SYNC_SAMPLE;
5506  }
5507  if (trk->cluster[trk->entry].flags & MOV_SYNC_SAMPLE)
5508  trk->has_keyframes++;
5509  }
5510  if (pkt->flags & AV_PKT_FLAG_DISPOSABLE) {
5511  trk->cluster[trk->entry].flags |= MOV_DISPOSABLE_SAMPLE;
5512  trk->has_disposable++;
5513  }
5514  trk->entry++;
5515  trk->sample_count += samples_in_chunk;
5516  mov->mdat_size += size;
5517 
5518  if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams)
5519  ff_mov_add_hinted_packet(s, pkt, trk->hint_track, trk->entry,
5520  reformatted_data, size);
5521 
5522 end:
5523 err:
5524 
5525  av_free(reformatted_data);
5526  return ret;
5527 }
5528 
5530 {
5531  MOVMuxContext *mov = s->priv_data;
5532  MOVTrack *trk = &mov->tracks[pkt->stream_index];
5533  AVCodecParameters *par = trk->par;
5534  int64_t frag_duration = 0;
5535  int size = pkt->size;
5536 
5537  int ret = check_pkt(s, pkt);
5538  if (ret < 0)
5539  return ret;
5540 
5541  if (mov->flags & FF_MOV_FLAG_FRAG_DISCONT) {
5542  int i;
5543  for (i = 0; i < s->nb_streams; i++)
5544  mov->tracks[i].frag_discont = 1;
5546  }
5547 
5549  if (trk->dts_shift == AV_NOPTS_VALUE)
5550  trk->dts_shift = pkt->pts - pkt->dts;
5551  pkt->dts += trk->dts_shift;
5552  }
5553 
5554  if (trk->par->codec_id == AV_CODEC_ID_MP4ALS ||
5555  trk->par->codec_id == AV_CODEC_ID_AAC ||
5556  trk->par->codec_id == AV_CODEC_ID_AV1 ||
5557  trk->par->codec_id == AV_CODEC_ID_FLAC) {
5558  int side_size = 0;
5559  uint8_t *side = av_packet_get_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA, &side_size);
5560  if (side && side_size > 0 && (side_size != par->extradata_size || memcmp(side, par->extradata, side_size))) {
5561  void *newextra = av_mallocz(side_size + AV_INPUT_BUFFER_PADDING_SIZE);
5562  if (!newextra)
5563  return AVERROR(ENOMEM);
5564  av_free(par->extradata);
5565  par->extradata = newextra;
5566  memcpy(par->extradata, side, side_size);
5567  par->extradata_size = side_size;
5568  if (!pkt->size) // Flush packet
5569  mov->need_rewrite_extradata = 1;
5570  }
5571  }
5572 
5573  if (!pkt->size) {
5574  if (trk->start_dts == AV_NOPTS_VALUE && trk->frag_discont) {
5575  trk->start_dts = pkt->dts;
5576  if (pkt->pts != AV_NOPTS_VALUE)
5577  trk->start_cts = pkt->pts - pkt->dts;
5578  else
5579  trk->start_cts = 0;
5580  }
5581 
5582  return 0; /* Discard 0 sized packets */
5583  }
5584 
5585  if (trk->entry && pkt->stream_index < s->nb_streams)
5586  frag_duration = av_rescale_q(pkt->dts - trk->cluster[0].dts,
5587  s->streams[pkt->stream_index]->time_base,
5588  AV_TIME_BASE_Q);
5589  if ((mov->max_fragment_duration &&
5590  frag_duration >= mov->max_fragment_duration) ||
5591  (mov->max_fragment_size && mov->mdat_size + size >= mov->max_fragment_size) ||
5592  (mov->flags & FF_MOV_FLAG_FRAG_KEYFRAME &&
5593  par->codec_type == AVMEDIA_TYPE_VIDEO &&
5594  trk->entry && pkt->flags & AV_PKT_FLAG_KEY) ||
5596  if (frag_duration >= mov->min_fragment_duration) {
5597  // Set the duration of this track to line up with the next
5598  // sample in this track. This avoids relying on AVPacket
5599  // duration, but only helps for this particular track, not
5600  // for the other ones that are flushed at the same time.
5601  trk->track_duration = pkt->dts - trk->start_dts;
5602  if (pkt->pts != AV_NOPTS_VALUE)
5603  trk->end_pts = pkt->pts;
5604  else
5605  trk->end_pts = pkt->dts;
5606  trk->end_reliable = 1;
5608  }
5609  }
5610 
5611  return ff_mov_write_packet(s, pkt);
5612 }
5613 
5615  int stream_index,
5616  int64_t dts) {
5617  AVPacket end;
5618  uint8_t data[2] = {0};
5619  int ret;
5620 
5621  av_init_packet(&end);
5622  end.size = sizeof(data);
5623  end.data = data;
5624  end.pts = dts;
5625  end.dts = dts;
5626  end.duration = 0;
5627  end.stream_index = stream_index;
5628 
5629  ret = mov_write_single_packet(s, &end);
5630  av_packet_unref(&end);
5631 
5632  return ret;
5633 }
5634 
5636 {
5637  MOVMuxContext *mov = s->priv_data;
5638  MOVTrack *trk;
5639 
5640  if (!pkt) {
5641  mov_flush_fragment(s, 1);
5642  return 1;
5643  }
5644 
5645  trk = &mov->tracks[pkt->stream_index];
5646 
5647  if (is_cover_image(trk->st)) {
5648  int ret;
5649 
5650  if (trk->st->nb_frames >= 1) {
5651  if (trk->st->nb_frames == 1)
5652  av_log(s, AV_LOG_WARNING, "Got more than one picture in stream %d,"
5653  " ignoring.\n", pkt->stream_index);
5654  return 0;
5655  }
5656 
5657  if ((ret = av_packet_ref(&trk->cover_image, pkt)) < 0)
5658  return ret;
5659 
5660  return 0;
5661  } else {
5662  int i;
5663 
5664  if (!pkt->size)
5665  return mov_write_single_packet(s, pkt); /* Passthrough. */
5666 
5667  /*
5668  * Subtitles require special handling.
5669  *
5670  * 1) For full complaince, every track must have a sample at
5671  * dts == 0, which is rarely true for subtitles. So, as soon
5672  * as we see any packet with dts > 0, write an empty subtitle
5673  * at dts == 0 for any subtitle track with no samples in it.
5674  *
5675  * 2) For each subtitle track, check if the current packet's
5676  * dts is past the duration of the last subtitle sample. If
5677  * so, we now need to write an end sample for that subtitle.
5678  *
5679  * This must be done conditionally to allow for subtitles that
5680  * immediately replace each other, in which case an end sample
5681  * is not needed, and is, in fact, actively harmful.
5682  *
5683  * 3) See mov_write_trailer for how the final end sample is
5684  * handled.
5685  */
5686  for (i = 0; i < mov->nb_streams; i++) {
5687  MOVTrack *trk = &mov->tracks[i];
5688  int ret;
5689 
5690  if (trk->par->codec_id == AV_CODEC_ID_MOV_TEXT &&
5691  trk->track_duration < pkt->dts &&
5692  (trk->entry == 0 || !trk->last_sample_is_subtitle_end)) {
5694  if (ret < 0) return ret;
5695  trk->last_sample_is_subtitle_end = 1;
5696  }
5697  }
5698 
5699  if (trk->mode == MODE_MOV && trk->par->codec_type == AVMEDIA_TYPE_VIDEO) {
5700  AVPacket *opkt = pkt;
5701  int reshuffle_ret, ret;
5702  if (trk->is_unaligned_qt_rgb) {
5703  int64_t bpc = trk->par->bits_per_coded_sample != 15 ? trk->par->bits_per_coded_sample : 16;
5704  int expected_stride = ((trk->par->width * bpc + 15) >> 4)*2;
5705  reshuffle_ret = ff_reshuffle_raw_rgb(s, &pkt, trk->par, expected_stride);
5706  if (reshuffle_ret < 0)
5707  return reshuffle_ret;
5708  } else
5709  reshuffle_ret = 0;
5710  if (trk->par->format == AV_PIX_FMT_PAL8 && !trk->pal_done) {
5711  ret = ff_get_packet_palette(s, opkt, reshuffle_ret, trk->palette);
5712  if (ret < 0)
5713  goto fail;
5714  if (ret)
5715  trk->pal_done++;
5716  } else if (trk->par->codec_id == AV_CODEC_ID_RAWVIDEO &&
5717  (trk->par->format == AV_PIX_FMT_GRAY8 ||
5718  trk->par->format == AV_PIX_FMT_MONOBLACK)) {
5719  for (i = 0; i < pkt->size; i++)
5720  pkt->data[i] = ~pkt->data[i];
5721  }
5722  if (reshuffle_ret) {
5723  ret = mov_write_single_packet(s, pkt);
5724 fail:
5725  if (reshuffle_ret)
5726  av_packet_free(&pkt);
5727  return ret;
5728  }
5729  }
5730 
5731  return mov_write_single_packet(s, pkt);
5732  }
5733 }
5734 
5735 // QuickTime chapters involve an additional text track with the chapter names
5736 // as samples, and a tref pointing from the other tracks to the chapter one.
5737 static int mov_create_chapter_track(AVFormatContext *s, int tracknum)
5738 {
5739  AVIOContext *pb;
5740 
5741  MOVMuxContext *mov = s->priv_data;
5742  MOVTrack *track = &mov->tracks[tracknum];
5743  AVPacket pkt = { .stream_index = tracknum, .flags = AV_PKT_FLAG_KEY };
5744  int i, len;
5745 
5746  track->mode = mov->mode;
5747  track->tag = MKTAG('t','e','x','t');
5748  track->timescale = MOV_TIMESCALE;
5749  track->par = avcodec_parameters_alloc();
5750  if (!track->par)
5751  return AVERROR(ENOMEM);
5753 #if 0
5754  // These properties are required to make QT recognize the chapter track
5755  uint8_t chapter_properties[43] = { 0, 0, 0, 0, 0, 0, 0, 1, };
5756  if (ff_alloc_extradata(track->par, sizeof(chapter_properties)))
5757  return AVERROR(ENOMEM);
5758  memcpy(track->par->extradata, chapter_properties, sizeof(chapter_properties));
5759 #else
5760  if (avio_open_dyn_buf(&pb) >= 0) {
5761  int size;
5762  uint8_t *buf;
5763 
5764  /* Stub header (usually for Quicktime chapter track) */
5765  // TextSampleEntry
5766  avio_wb32(pb, 0x01); // displayFlags
5767  avio_w8(pb, 0x00); // horizontal justification
5768  avio_w8(pb, 0x00); // vertical justification
5769  avio_w8(pb, 0x00); // bgColourRed
5770  avio_w8(pb, 0x00); // bgColourGreen
5771  avio_w8(pb, 0x00); // bgColourBlue
5772  avio_w8(pb, 0x00); // bgColourAlpha
5773  // BoxRecord
5774  avio_wb16(pb, 0x00); // defTextBoxTop
5775  avio_wb16(pb, 0x00); // defTextBoxLeft
5776  avio_wb16(pb, 0x00); // defTextBoxBottom
5777  avio_wb16(pb, 0x00); // defTextBoxRight
5778  // StyleRecord
5779  avio_wb16(pb, 0x00); // startChar
5780  avio_wb16(pb, 0x00); // endChar
5781  avio_wb16(pb, 0x01); // fontID
5782  avio_w8(pb, 0x00); // fontStyleFlags
5783  avio_w8(pb, 0x00); // fontSize
5784  avio_w8(pb, 0x00); // fgColourRed
5785  avio_w8(pb, 0x00); // fgColourGreen
5786  avio_w8(pb, 0x00); // fgColourBlue
5787  avio_w8(pb, 0x00); // fgColourAlpha
5788  // FontTableBox
5789  avio_wb32(pb, 0x0D); // box size
5790  ffio_wfourcc(pb, "ftab"); // box atom name
5791  avio_wb16(pb, 0x01); // entry count
5792  // FontRecord
5793  avio_wb16(pb, 0x01); // font ID
5794  avio_w8(pb, 0x00); // font name length
5795 
5796  if ((size = avio_close_dyn_buf(pb, &buf)) > 0) {
5797  track->par->extradata = buf;
5798  track->par->extradata_size = size;
5799  } else {
5800  av_freep(&buf);
5801  }
5802  }
5803 #endif
5804 
5805  for (i = 0; i < s->nb_chapters; i++) {
5806  AVChapter *c = s->chapters[i];
5807  AVDictionaryEntry *t;
5808 
5809  int64_t end = av_rescale_q(c->end, c->time_base, (AVRational){1,MOV_TIMESCALE});
5810  pkt.pts = pkt.dts = av_rescale_q(c->start, c->time_base, (AVRational){1,MOV_TIMESCALE});
5811  pkt.duration = end - pkt.dts;
5812 
5813  if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
5814  static const char encd[12] = {
5815  0x00, 0x00, 0x00, 0x0C,
5816  'e', 'n', 'c', 'd',
5817  0x00, 0x00, 0x01, 0x00 };
5818  len = strlen(t->value);
5819  pkt.size = len + 2 + 12;
5820  pkt.data = av_malloc(pkt.size);
5821  if (!pkt.data)
5822  return AVERROR(ENOMEM);
5823  AV_WB16(pkt.data, len);
5824  memcpy(pkt.data + 2, t->value, len);
5825  memcpy(pkt.data + len + 2, encd, sizeof(encd));
5826  ff_mov_write_packet(s, &pkt);
5827  av_freep(&pkt.data);
5828  }
5829  }
5830 
5831  return 0;
5832 }
5833 
5834 
5835 static int mov_check_timecode_track(AVFormatContext *s, AVTimecode *tc, int src_index, const char *tcstr)
5836 {
5837  int ret;
5838 
5839  /* compute the frame number */
5840  ret = av_timecode_init_from_string(tc, find_fps(s, s->streams[src_index]), tcstr, s);
5841  return ret;
5842 }
5843 
5845 {
5846  int ret;
5847  MOVMuxContext *mov = s->priv_data;
5848  MOVTrack *track = &mov->tracks[index];
5849  AVStream *src_st = s->streams[src_index];
5850  AVPacket pkt = {.stream_index = index, .flags = AV_PKT_FLAG_KEY, .size = 4};
5851  AVRational rate = find_fps(s, src_st);
5852 
5853  /* tmcd track based on video stream */
5854  track->mode = mov->mode;
5855  track->tag = MKTAG('t','m','c','d');
5856  track->src_track = src_index;
5857  track->timescale = mov->tracks[src_index].timescale;
5860 
5861  /* set st to src_st for metadata access*/
5862  track->st = src_st;
5863 
5864  /* encode context: tmcd data stream */
5865  track->par = avcodec_parameters_alloc();
5866  if (!track->par)
5867  return AVERROR(ENOMEM);
5868  track->par->codec_type = AVMEDIA_TYPE_DATA;
5869  track->par->codec_tag = track->tag;
5870  track->st->avg_frame_rate = av_inv_q(rate);
5871 
5872  /* the tmcd track just contains one packet with the frame number */
5873  pkt.data = av_malloc(pkt.size);
5874  if (!pkt.data)
5875  return AVERROR(ENOMEM);
5876  AV_WB32(pkt.data, tc.start);
5877  ret = ff_mov_write_packet(s, &pkt);
5878  av_free(pkt.data);
5879  return ret;
5880 }
5881 
5882 /*
5883  * st->disposition controls the "enabled" flag in the tkhd tag.
5884  * QuickTime will not play a track if it is not enabled. So make sure
5885  * that one track of each type (audio, video, subtitle) is enabled.
5886  *
5887  * Subtitles are special. For audio and video, setting "enabled" also
5888  * makes the track "default" (i.e. it is rendered when played). For
5889  * subtitles, an "enabled" subtitle is not rendered by default, but
5890  * if no subtitle is enabled, the subtitle menu in QuickTime will be
5891  * empty!
5892  */
5894 {
5895  MOVMuxContext *mov = s->priv_data;
5896  int i;
5897  int enabled[AVMEDIA_TYPE_NB];
5898  int first[AVMEDIA_TYPE_NB];
5899 
5900  for (i = 0; i < AVMEDIA_TYPE_NB; i++) {
5901  enabled[i] = 0;
5902  first[i] = -1;
5903  }
5904 
5905  for (i = 0; i < s->nb_streams; i++) {
5906  AVStream *st = s->streams[i];
5907 
5909  st->codecpar->codec_type >= AVMEDIA_TYPE_NB ||
5910  is_cover_image(st))
5911  continue;
5912 
5913  if (first[st->codecpar->codec_type] < 0)
5914  first[st->codecpar->codec_type] = i;
5915  if (st->disposition & AV_DISPOSITION_DEFAULT) {
5916  mov->tracks[i].flags |= MOV_TRACK_ENABLED;
5917  enabled[st->codecpar->codec_type]++;
5918  }
5919  }
5920 
5921  for (i = 0; i < AVMEDIA_TYPE_NB; i++) {
5922  switch (i) {
5923  case AVMEDIA_TYPE_VIDEO:
5924  case AVMEDIA_TYPE_AUDIO:
5925  case AVMEDIA_TYPE_SUBTITLE:
5926  if (enabled[i] > 1)
5927  mov->per_stream_grouping = 1;
5928  if (!enabled[i] && first[i] >= 0)
5929  mov->tracks[first[i]].flags |= MOV_TRACK_ENABLED;
5930  break;
5931  }
5932  }
5933 }
5934 
5936 {
5937  MOVMuxContext *mov = s->priv_data;
5938  int i;
5939 
5940  if (mov->chapter_track) {
5941  if (mov->tracks[mov->chapter_track].par)
5942  av_freep(&mov->tracks[mov->chapter_track].par->extradata);
5943  av_freep(&mov->tracks[mov->chapter_track].par);
5944  }
5945 
5946  for (i = 0; i < mov->nb_streams; i++) {
5947  if (mov->tracks[i].tag == MKTAG('r','t','p',' '))
5948  ff_mov_close_hinting(&mov->tracks[i]);
5949  else if (mov->tracks[i].tag == MKTAG('t','m','c','d') && mov->nb_meta_tmcd)
5950  av_freep(&mov->tracks[i].par);
5951  av_freep(&mov->tracks[i].cluster);
5952  av_freep(&mov->tracks[i].frag_info);
5954 
5955  if (mov->tracks[i].eac3_priv) {
5956  struct eac3_info *info = mov->tracks[i].eac3_priv;
5957  av_packet_unref(&info->pkt);
5958  av_freep(&mov->tracks[i].eac3_priv);
5959  }
5960  if (mov->tracks[i].vos_len)
5961  av_freep(&mov->tracks[i].vos_data);
5962 
5963  ff_mov_cenc_free(&mov->tracks[i].cenc);
5964  }
5965 
5966  av_freep(&mov->tracks);
5967 }
5968 
5969 static uint32_t rgb_to_yuv(uint32_t rgb)
5970 {
5971  uint8_t r, g, b;
5972  int y, cb, cr;
5973 
5974  r = (rgb >> 16) & 0xFF;
5975  g = (rgb >> 8) & 0xFF;
5976  b = (rgb ) & 0xFF;
5977 
5978  y = av_clip_uint8(( 16000 + 257 * r + 504 * g + 98 * b)/1000);
5979  cb = av_clip_uint8((128000 - 148 * r - 291 * g + 439 * b)/1000);
5980  cr = av_clip_uint8((128000 + 439 * r - 368 * g - 71 * b)/1000);
5981 
5982  return (y << 16) | (cr << 8) | cb;
5983 }
5984 
5986  AVStream *st)
5987 {
5988  int i, width = 720, height = 480;
5989  int have_palette = 0, have_size = 0;
5990  uint32_t palette[16];
5991  char *cur = st->codecpar->extradata;
5992 
5993  while (cur && *cur) {
5994  if (strncmp("palette:", cur, 8) == 0) {
5995  int i, count;
5996  count = sscanf(cur + 8,
5997  "%06"PRIx32", %06"PRIx32", %06"PRIx32", %06"PRIx32", "
5998  "%06"PRIx32", %06"PRIx32", %06"PRIx32", %06"PRIx32", "
5999  "%06"PRIx32", %06"PRIx32", %06"PRIx32", %06"PRIx32", "
6000  "%06"PRIx32", %06"PRIx32", %06"PRIx32", %06"PRIx32"",
6001  &palette[ 0], &palette[ 1], &palette[ 2], &palette[ 3],
6002  &palette[ 4], &palette[ 5], &palette[ 6], &palette[ 7],
6003  &palette[ 8], &palette[ 9], &palette[10], &palette[11],
6004  &palette[12], &palette[13], &palette[14], &palette[15]);
6005 
6006  for (i = 0; i < count; i++) {
6007  palette[i] = rgb_to_yuv(palette[i]);
6008  }
6009  have_palette = 1;
6010  } else if (!strncmp("size:", cur, 5)) {
6011  sscanf(cur + 5, "%dx%d", &width, &height);
6012  have_size = 1;
6013  }
6014  if (have_palette && have_size)
6015  break;
6016  cur += strcspn(cur, "\n\r");
6017  cur += strspn(cur, "\n\r");
6018  }
6019  if (have_palette) {
6021  if (!track->vos_data)
6022  return AVERROR(ENOMEM);
6023  for (i = 0; i < 16; i++) {
6024  AV_WB32(track->vos_data + i * 4, palette[i]);
6025  }
6026  memset(track->vos_data + 16*4, 0, AV_INPUT_BUFFER_PADDING_SIZE);
6027  track->vos_len = 16 * 4;
6028  }
6029  st->codecpar->width = width;
6030  st->codecpar->height = track->height = height;
6031 
6032  return 0;
6033 }
6034 
6036 {
6037  MOVMuxContext *mov = s->priv_data;
6038  AVDictionaryEntry *global_tcr = av_dict_get(s->metadata, "timecode", NULL, 0);
6039  int i, ret;
6040 
6041  mov->fc = s;
6042 
6043  /* Default mode == MP4 */
6044  mov->mode = MODE_MP4;
6045 
6046  if (s->oformat) {
6047  if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
6048  else if (!strcmp("3g2", s->oformat->name)) mov->mode = MODE_3GP|MODE_3G2;
6049  else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
6050  else if (!strcmp("psp", s->oformat->name)) mov->mode = MODE_PSP;
6051  else if (!strcmp("ipod",s->oformat->name)) mov->mode = MODE_IPOD;
6052  else if (!strcmp("ismv",s->oformat->name)) mov->mode = MODE_ISM;
6053  else if (!strcmp("f4v", s->oformat->name)) mov->mode = MODE_F4V;
6054  }
6055 
6056  if (mov->flags & FF_MOV_FLAG_DELAY_MOOV)
6057  mov->flags |= FF_MOV_FLAG_EMPTY_MOOV;
6058 
6059  /* Set the FRAGMENT flag if any of the fragmentation methods are
6060  * enabled. */
6061  if (mov->max_fragment_duration || mov->max_fragment_size ||
6062  mov->flags & (FF_MOV_FLAG_EMPTY_MOOV |
6066  mov->flags |= FF_MOV_FLAG_FRAGMENT;
6067 
6068  /* Set other implicit flags immediately */
6069  if (mov->mode == MODE_ISM)
6072  if (mov->flags & FF_MOV_FLAG_DASH)
6075 
6077  av_log(s, AV_LOG_VERBOSE, "Empty MOOV enabled; disabling automatic bitstream filtering\n");
6078  s->flags &= ~AVFMT_FLAG_AUTO_BSF;
6079  }
6080 
6081  if (mov->flags & FF_MOV_FLAG_FASTSTART) {
6082  mov->reserved_moov_size = -1;
6083  }
6084 
6085  if (mov->use_editlist < 0) {
6086  mov->use_editlist = 1;
6087  if (mov->flags & FF_MOV_FLAG_FRAGMENT &&
6088  !(mov->flags & FF_MOV_FLAG_DELAY_MOOV)) {
6089  // If we can avoid needing an edit list by shifting the
6090  // tracks, prefer that over (trying to) write edit lists
6091  // in fragmented output.
6094  mov->use_editlist = 0;
6095  }
6096  }
6097  if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV &&
6098  !(mov->flags & FF_MOV_FLAG_DELAY_MOOV) && mov->use_editlist)
6099  av_log(s, AV_LOG_WARNING, "No meaningful edit list will be written when using empty_moov without delay_moov\n");
6100 
6103 
6104  /* Clear the omit_tfhd_offset flag if default_base_moof is set;
6105  * if the latter is set that's enough and omit_tfhd_offset doesn't
6106  * add anything extra on top of that. */
6107  if (mov->flags & FF_MOV_FLAG_OMIT_TFHD_OFFSET &&
6110 
6111  if (mov->frag_interleave &&
6113  av_log(s, AV_LOG_ERROR,
6114  "Sample interleaving in fragments is mutually exclusive with "
6115  "omit_tfhd_offset and separate_moof\n");
6116  return AVERROR(EINVAL);
6117  }
6118 
6119  /* Non-seekable output is ok if using fragmentation. If ism_lookahead
6120  * is enabled, we don't support non-seekable output at all. */
6121  if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL) &&
6122  (!(mov->flags & FF_MOV_FLAG_FRAGMENT) || mov->ism_lookahead)) {
6123  av_log(s, AV_LOG_ERROR, "muxer does not support non seekable output\n");
6124  return AVERROR(EINVAL);
6125  }
6126 
6127  mov->nb_streams = s->nb_streams;
6128  if (mov->mode & (MODE_MP4|MODE_MOV|MODE_IPOD) && s->nb_chapters)
6129  mov->chapter_track = mov->nb_streams++;
6130 
6131  if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
6132  for (i = 0; i < s->nb_streams; i++)
6133  if (rtp_hinting_needed(s->streams[i]))
6134  mov->nb_streams++;
6135  }
6136 
6137  if ( mov->write_tmcd == -1 && (mov->mode == MODE_MOV || mov->mode == MODE_MP4)
6138  || mov->write_tmcd == 1) {
6139  /* +1 tmcd track for each video stream with a timecode */
6140  for (i = 0; i < s->nb_streams; i++) {
6141  AVStream *st = s->streams[i];
6142  AVDictionaryEntry *t = global_tcr;
6143  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
6144  (t || (t=av_dict_get(st->metadata, "timecode", NULL, 0)))) {
6145  AVTimecode tc;
6146  ret = mov_check_timecode_track(s, &tc, i, t->value);
6147  if (ret >= 0)
6148  mov->nb_meta_tmcd++;
6149  }
6150  }
6151 
6152  /* check if there is already a tmcd track to remux */
6153  if (mov->nb_meta_tmcd) {
6154  for (i = 0; i < s->nb_streams; i++) {
6155  AVStream *st = s->streams[i];
6156  if (st->codecpar->codec_tag == MKTAG('t','m','c','d')) {
6157  av_log(s, AV_LOG_WARNING, "You requested a copy of the original timecode track "
6158  "so timecode metadata are now ignored\n");
6159  mov->nb_meta_tmcd = 0;
6160  }
6161  }
6162  }
6163 
6164  mov->nb_streams += mov->nb_meta_tmcd;
6165  }
6166 
6167  // Reserve an extra stream for chapters for the case where chapters
6168  // are written in the trailer
6169  mov->tracks = av_mallocz_array((mov->nb_streams + 1), sizeof(*mov->tracks));
6170  if (!mov->tracks)
6171  return AVERROR(ENOMEM);
6172 
6173  if (mov->encryption_scheme_str != NULL && strcmp(mov->encryption_scheme_str, "none") != 0) {
6174  if (strcmp(mov->encryption_scheme_str, "cenc-aes-ctr") == 0) {
6176 
6177  if (mov->encryption_key_len != AES_CTR_KEY_SIZE) {
6178  av_log(s, AV_LOG_ERROR, "Invalid encryption key len %d expected %d\n",
6180  return AVERROR(EINVAL);
6181  }
6182 
6183  if (mov->encryption_kid_len != CENC_KID_SIZE) {
6184  av_log(s, AV_LOG_ERROR, "Invalid encryption kid len %d expected %d\n",
6186  return AVERROR(EINVAL);
6187  }
6188  } else {
6189  av_log(s, AV_LOG_ERROR, "unsupported encryption scheme %s\n",
6190  mov->encryption_scheme_str);
6191  return AVERROR(EINVAL);
6192  }
6193  }
6194 
6195  for (i = 0; i < s->nb_streams; i++) {
6196  AVStream *st= s->streams[i];
6197  MOVTrack *track= &mov->tracks[i];
6198  AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL,0);
6199 
6200  track->st = st;
6201  track->par = st->codecpar;
6202  track->language = ff_mov_iso639_to_lang(lang?lang->value:"und", mov->mode!=MODE_MOV);
6203  if (track->language < 0)
6204  track->language = 0;
6205  track->mode = mov->mode;
6206  track->tag = mov_find_codec_tag(s, track);
6207  if (!track->tag) {
6208  av_log(s, AV_LOG_ERROR, "Could not find tag for codec %s in stream #%d, "
6209  "codec not currently supported in container\n",
6211  return AVERROR(EINVAL);
6212  }
6213  /* If hinting of this track is enabled by a later hint track,
6214  * this is updated. */
6215  track->hint_track = -1;
6216  track->start_dts = AV_NOPTS_VALUE;
6217  track->start_cts = AV_NOPTS_VALUE;
6218  track->end_pts = AV_NOPTS_VALUE;
6219  track->dts_shift = AV_NOPTS_VALUE;
6220  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
6221  if (track->tag == MKTAG('m','x','3','p') || track->tag == MKTAG('m','x','3','n') ||
6222  track->tag == MKTAG('m','x','4','p') || track->tag == MKTAG('m','x','4','n') ||
6223  track->tag == MKTAG('m','x','5','p') || track->tag == MKTAG('m','x','5','n')) {
6224  if (st->codecpar->width != 720 || (st->codecpar->height != 608 && st->codecpar->height != 512)) {
6225  av_log(s, AV_LOG_ERROR, "D-10/IMX must use 720x608 or 720x512 video resolution\n");
6226  return AVERROR(EINVAL);
6227  }
6228  track->height = track->tag >> 24 == 'n' ? 486 : 576;
6229  }
6230  if (mov->video_track_timescale) {
6231  track->timescale = mov->video_track_timescale;
6232  } else {
6233  track->timescale = st->time_base.den;
6234  while(track->timescale < 10000)
6235  track->timescale *= 2;
6236  }
6237  if (st->codecpar->width > 65535 || st->codecpar->height > 65535) {
6238  av_log(s, AV_LOG_ERROR, "Resolution %dx%d too large for mov/mp4\n", st->codecpar->width, st->codecpar->height);
6239  return AVERROR(EINVAL);
6240  }
6241  if (track->mode == MODE_MOV && track->timescale > 100000)
6243  "WARNING codec timebase is very high. If duration is too long,\n"
6244  "file may not be playable by quicktime. Specify a shorter timebase\n"
6245  "or choose different container.\n");
6246  if (track->mode == MODE_MOV &&
6247  track->par->codec_id == AV_CODEC_ID_RAWVIDEO &&
6248  track->tag == MKTAG('r','a','w',' ')) {
6249  enum AVPixelFormat pix_fmt = track->par->format;
6250  if (pix_fmt == AV_PIX_FMT_NONE && track->par->bits_per_coded_sample == 1)
6251  pix_fmt = AV_PIX_FMT_MONOWHITE;
6252  track->is_unaligned_qt_rgb =
6253  pix_fmt == AV_PIX_FMT_RGB24 ||
6254  pix_fmt == AV_PIX_FMT_BGR24 ||
6255  pix_fmt == AV_PIX_FMT_PAL8 ||
6256  pix_fmt == AV_PIX_FMT_GRAY8 ||
6257  pix_fmt == AV_PIX_FMT_MONOWHITE ||
6258  pix_fmt == AV_PIX_FMT_MONOBLACK;
6259  }
6260  if (track->par->codec_id == AV_CODEC_ID_VP9 ||
6261  track->par->codec_id == AV_CODEC_ID_AV1) {
6262  if (track->mode != MODE_MP4) {
6263  av_log(s, AV_LOG_ERROR, "%s only supported in MP4.\n", avcodec_get_name(track->par->codec_id));
6264  return AVERROR(EINVAL);
6265  }
6266  } else if (track->par->codec_id == AV_CODEC_ID_VP8) {
6267  /* altref frames handling is not defined in the spec as of version v1.0,
6268  * so just forbid muxing VP8 streams altogether until a new version does */
6269  av_log(s, AV_LOG_ERROR, "VP8 muxing is currently not supported.\n");
6270  return AVERROR_PATCHWELCOME;
6271  }
6272  } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
6273  track->timescale = st->codecpar->sample_rate;
6275  av_log(s, AV_LOG_WARNING, "track %d: codec frame size is not set\n", i);
6276  track->audio_vbr = 1;
6277  }else if (st->codecpar->codec_id == AV_CODEC_ID_ADPCM_MS ||
6280  if (!st->codecpar->block_align) {
6281  av_log(s, AV_LOG_ERROR, "track %d: codec block align is not set for adpcm\n", i);
6282  return AVERROR(EINVAL);
6283  }
6284  track->sample_size = st->codecpar->block_align;
6285  }else if (st->codecpar->frame_size > 1){ /* assume compressed audio */
6286  track->audio_vbr = 1;
6287  }else{
6289  }
6290  if (st->codecpar->codec_id == AV_CODEC_ID_ILBC ||
6292  track->audio_vbr = 1;
6293  }
6294  if (track->mode != MODE_MOV &&
6295  track->par->codec_id == AV_CODEC_ID_MP3 && track->timescale < 16000) {
6297  av_log(s, AV_LOG_ERROR, "track %d: muxing mp3 at %dhz is not standard, to mux anyway set strict to -1\n",
6298  i, track->par->sample_rate);
6299  return AVERROR(EINVAL);
6300  } else {
6301  av_log(s, AV_LOG_WARNING, "track %d: muxing mp3 at %dhz is not standard in MP4\n",
6302  i, track->par->sample_rate);
6303  }
6304  }
6305  if (track->par->codec_id == AV_CODEC_ID_FLAC ||
6306  track->par->codec_id == AV_CODEC_ID_OPUS) {
6307  if (track->mode != MODE_MP4) {
6308  av_log(s, AV_LOG_ERROR, "%s only supported in MP4.\n", avcodec_get_name(track->par->codec_id));
6309  return AVERROR(EINVAL);
6310  }
6312  av_log(s, AV_LOG_ERROR,
6313  "%s in MP4 support is experimental, add "
6314  "'-strict %d' if you want to use it.\n",
6316  return AVERROR_EXPERIMENTAL;
6317  }
6318  }
6319  } else if (st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) {
6320  track->timescale = st->time_base.den;
6321  } else if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA) {
6322  track->timescale = st->time_base.den;
6323  } else {
6324  track->timescale = MOV_TIMESCALE;
6325  }
6326  if (!track->height)
6327  track->height = st->codecpar->height;
6328  /* The ism specific timescale isn't mandatory, but is assumed by
6329  * some tools, such as mp4split. */
6330  if (mov->mode == MODE_ISM)
6331  track->timescale = 10000000;
6332 
6333  avpriv_set_pts_info(st, 64, 1, track->timescale);
6334 
6336  ret = ff_mov_cenc_init(&track->cenc, mov->encryption_key,
6338  if (ret)
6339  return ret;
6340  }
6341  }
6342 
6343  enable_tracks(s);
6344  return 0;
6345 }
6346 
6348 {
6349  AVIOContext *pb = s->pb;
6350  MOVMuxContext *mov = s->priv_data;
6351  AVDictionaryEntry *t, *global_tcr = av_dict_get(s->metadata, "timecode", NULL, 0);
6352  int i, ret, hint_track = 0, tmcd_track = 0, nb_tracks = s->nb_streams;
6353 
6354  if (mov->mode & (MODE_MP4|MODE_MOV|MODE_IPOD) && s->nb_chapters)
6355  nb_tracks++;
6356 
6357  if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
6358  hint_track = nb_tracks;
6359  for (i = 0; i < s->nb_streams; i++)
6360  if (rtp_hinting_needed(s->streams[i]))
6361  nb_tracks++;
6362  }
6363 
6364  if (mov->mode == MODE_MOV || mov->mode == MODE_MP4)
6365  tmcd_track = nb_tracks;
6366 
6367  for (i = 0; i < s->nb_streams; i++) {
6368  int j;
6369  AVStream *st= s->streams[i];
6370  MOVTrack *track= &mov->tracks[i];
6371 
6372  /* copy extradata if it exists */
6373  if (st->codecpar->extradata_size) {
6376  else if (!TAG_IS_AVCI(track->tag) && st->codecpar->codec_id != AV_CODEC_ID_DNXHD) {
6377  track->vos_len = st->codecpar->extradata_size;
6379  if (!track->vos_data) {
6380  return AVERROR(ENOMEM);
6381  }
6382  memcpy(track->vos_data, st->codecpar->extradata, track->vos_len);
6383  memset(track->vos_data + track->vos_len, 0, AV_INPUT_BUFFER_PADDING_SIZE);
6384  }
6385  }
6386 
6387  if (st->codecpar->codec_type != AVMEDIA_TYPE_AUDIO ||
6389  continue;
6390 
6391  for (j = 0; j < s->nb_streams; j++) {
6392  AVStream *stj= s->streams[j];
6393  MOVTrack *trackj= &mov->tracks[j];
6394  if (j == i)
6395  continue;
6396 
6397  if (stj->codecpar->codec_type != AVMEDIA_TYPE_AUDIO ||
6398  trackj->par->channel_layout != AV_CH_LAYOUT_MONO ||
6399  trackj->language != track->language ||
6400  trackj->tag != track->tag
6401  )
6402  continue;
6403  track->multichannel_as_mono++;
6404  }
6405  }
6406 
6407  if (!(mov->flags & FF_MOV_FLAG_DELAY_MOOV)) {
6408  if ((ret = mov_write_identification(pb, s)) < 0)
6409  return ret;
6410  }
6411 
6412  if (mov->reserved_moov_size){
6413  mov->reserved_header_pos = avio_tell(pb);
6414  if (mov->reserved_moov_size > 0)
6415  avio_skip(pb, mov->reserved_moov_size);
6416  }
6417 
6418  if (mov->flags & FF_MOV_FLAG_FRAGMENT) {
6419  /* If no fragmentation options have been set, set a default. */
6420  if (!(mov->flags & (FF_MOV_FLAG_FRAG_KEYFRAME |
6425  } else {
6426  if (mov->flags & FF_MOV_FLAG_FASTSTART)
6427  mov->reserved_header_pos = avio_tell(pb);
6428  mov_write_mdat_tag(pb, mov);
6429  }
6430 
6432  if (mov->time)
6433  mov->time += 0x7C25B080; // 1970 based -> 1904 based
6434 
6435  if (mov->chapter_track)
6436  if ((ret = mov_create_chapter_track(s, mov->chapter_track)) < 0)
6437  return ret;
6438 
6439  if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
6440  for (i = 0; i < s->nb_streams; i++) {
6441  if (rtp_hinting_needed(s->streams[i])) {
6442  if ((ret = ff_mov_init_hinting(s, hint_track, i)) < 0)
6443  return ret;
6444  hint_track++;
6445  }
6446  }
6447  }
6448 
6449  if (mov->nb_meta_tmcd) {
6450  /* Initialize the tmcd tracks */
6451  for (i = 0; i < s->nb_streams; i++) {
6452  AVStream *st = s->streams[i];
6453  t = global_tcr;
6454 
6455  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
6456  AVTimecode tc;
6457  if (!t)
6458  t = av_dict_get(st->metadata, "timecode", NULL, 0);
6459  if (!t)
6460  continue;
6461  if (mov_check_timecode_track(s, &tc, i, t->value) < 0)
6462  continue;
6463  if ((ret = mov_create_timecode_track(s, tmcd_track, i, tc)) < 0)
6464  return ret;
6465  tmcd_track++;
6466  }
6467  }
6468  }
6469 
6470  avio_flush(pb);
6471 
6472  if (mov->flags & FF_MOV_FLAG_ISML)
6473  mov_write_isml_manifest(pb, mov, s);
6474 
6475  if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV &&
6476  !(mov->flags & FF_MOV_FLAG_DELAY_MOOV)) {
6477  if ((ret = mov_write_moov_tag(pb, mov, s)) < 0)
6478  return ret;
6479  avio_flush(pb);
6480  mov->moov_written = 1;
6481  if (mov->flags & FF_MOV_FLAG_GLOBAL_SIDX)
6482  mov->reserved_header_pos = avio_tell(pb);
6483  }
6484 
6485  return 0;
6486 }
6487 
6489 {
6490  int ret;
6491  AVIOContext *moov_buf;
6492  MOVMuxContext *mov = s->priv_data;
6493 
6494  if ((ret = ffio_open_null_buf(&moov_buf)) < 0)
6495  return ret;
6496  if ((ret = mov_write_moov_tag(moov_buf, mov, s)) < 0)
6497  return ret;
6498  return ffio_close_null_buf(moov_buf);
6499 }
6500 
6502 {
6503  int ret;
6504  AVIOContext *buf;
6505  MOVMuxContext *mov = s->priv_data;
6506 
6507  if ((ret = ffio_open_null_buf(&buf)) < 0)
6508  return ret;
6509  mov_write_sidx_tags(buf, mov, -1, 0);
6510  return ffio_close_null_buf(buf);
6511 }
6512 
6513 /*
6514  * This function gets the moov size if moved to the top of the file: the chunk
6515  * offset table can switch between stco (32-bit entries) to co64 (64-bit
6516  * entries) when the moov is moved to the beginning, so the size of the moov
6517  * would change. It also updates the chunk offset tables.
6518  */
6520 {
6521  int i, moov_size, moov_size2;
6522  MOVMuxContext *mov = s->priv_data;
6523 
6524  moov_size = get_moov_size(s);
6525  if (moov_size < 0)
6526  return moov_size;
6527 
6528  for (i = 0; i < mov->nb_streams; i++)
6529  mov->tracks[i].data_offset += moov_size;
6530 
6531  moov_size2 = get_moov_size(s);
6532  if (moov_size2 < 0)
6533  return moov_size2;
6534 
6535  /* if the size changed, we just switched from stco to co64 and need to
6536  * update the offsets */
6537  if (moov_size2 != moov_size)
6538  for (i = 0; i < mov->nb_streams; i++)
6539  mov->tracks[i].data_offset += moov_size2 - moov_size;
6540 
6541  return moov_size2;
6542 }
6543 
6545 {
6546  int i, sidx_size;
6547  MOVMuxContext *mov = s->priv_data;
6548 
6549  sidx_size = get_sidx_size(s);
6550  if (sidx_size < 0)
6551  return sidx_size;
6552 
6553  for (i = 0; i < mov->nb_streams; i++)
6554  mov->tracks[i].data_offset += sidx_size;
6555 
6556  return sidx_size;
6557 }
6558 
6560 {
6561  int ret = 0, moov_size;
6562  MOVMuxContext *mov = s->priv_data;
6563  int64_t pos, pos_end = avio_tell(s->pb);
6564  uint8_t *buf, *read_buf[2];
6565  int read_buf_id = 0;
6566  int read_size[2];
6567  AVIOContext *read_pb;
6568 
6569  if (mov->flags & FF_MOV_FLAG_FRAGMENT)
6570  moov_size = compute_sidx_size(s);
6571  else
6572  moov_size = compute_moov_size(s);
6573  if (moov_size < 0)
6574  return moov_size;
6575 
6576  buf = av_malloc(moov_size * 2);
6577  if (!buf)
6578  return AVERROR(ENOMEM);
6579  read_buf[0] = buf;
6580  read_buf[1] = buf + moov_size;
6581 
6582  /* Shift the data: the AVIO context of the output can only be used for
6583  * writing, so we re-open the same output, but for reading. It also avoids
6584  * a read/seek/write/seek back and forth. */
6585  avio_flush(s->pb);
6586  ret = s->io_open(s, &read_pb, s->url, AVIO_FLAG_READ, NULL);
6587  if (ret < 0) {
6588  av_log(s, AV_LOG_ERROR, "Unable to re-open %s output file for "
6589  "the second pass (faststart)\n", s->url);
6590  goto end;
6591  }
6592 
6593  /* mark the end of the shift to up to the last data we wrote, and get ready
6594  * for writing */
6595  pos_end = avio_tell(s->pb);
6596  avio_seek(s->pb, mov->reserved_header_pos + moov_size, SEEK_SET);
6597 
6598  /* start reading at where the new moov will be placed */
6599  avio_seek(read_pb, mov->reserved_header_pos, SEEK_SET);
6600  pos = avio_tell(read_pb);
6601 
6602 #define READ_BLOCK do { \
6603  read_size[read_buf_id] = avio_read(read_pb, read_buf[read_buf_id], moov_size); \
6604  read_buf_id ^= 1; \
6605 } while (0)
6606 
6607  /* shift data by chunk of at most moov_size */
6608  READ_BLOCK;
6609  do {
6610  int n;
6611  READ_BLOCK;
6612  n = read_size[read_buf_id];
6613  if (n <= 0)
6614  break;
6615  avio_write(s->pb, read_buf[read_buf_id], n);
6616  pos += n;
6617  } while (pos < pos_end);
6618  ff_format_io_close(s, &read_pb);
6619 
6620 end:
6621  av_free(buf);
6622  return ret;
6623 }
6624 
6626 {
6627  MOVMuxContext *mov = s->priv_data;
6628  AVIOContext *pb = s->pb;
6629  int res = 0;
6630  int i;
6631  int64_t moov_pos;
6632 
6633  if (mov->need_rewrite_extradata) {
6634  for (i = 0; i < s->nb_streams; i++) {
6635  MOVTrack *track = &mov->tracks[i];
6636  AVCodecParameters *par = track->par;
6637 
6638  track->vos_len = par->extradata_size;
6640  if (!track->vos_data)
6641  return AVERROR(ENOMEM);
6642  memcpy(track->vos_data, par->extradata, track->vos_len);
6643  memset(track->vos_data + track->vos_len, 0, AV_INPUT_BUFFER_PADDING_SIZE);
6644  }
6645  mov->need_rewrite_extradata = 0;
6646  }
6647 
6648  /*
6649  * Before actually writing the trailer, make sure that there are no
6650  * dangling subtitles, that need a terminating sample.
6651  */
6652  for (i = 0; i < mov->nb_streams; i++) {
6653  MOVTrack *trk = &mov->tracks[i];
6654  if (trk->par->codec_id == AV_CODEC_ID_MOV_TEXT &&
6657  trk->last_sample_is_subtitle_end = 1;
6658  }
6659  }
6660 
6661  // If there were no chapters when the header was written, but there
6662  // are chapters now, write them in the trailer. This only works
6663  // when we are not doing fragments.
6664  if (!mov->chapter_track && !(mov->flags & FF_MOV_FLAG_FRAGMENT)) {
6665  if (mov->mode & (MODE_MP4|MODE_MOV|MODE_IPOD) && s->nb_chapters) {
6666  mov->chapter_track = mov->nb_streams++;
6667  if ((res = mov_create_chapter_track(s, mov->chapter_track)) < 0)
6668  return res;
6669  }
6670  }
6671 
6672  if (!(mov->flags & FF_MOV_FLAG_FRAGMENT)) {
6673  moov_pos = avio_tell(pb);
6674 
6675  /* Write size of mdat tag */
6676  if (mov->mdat_size + 8 <= UINT32_MAX) {
6677  avio_seek(pb, mov->mdat_pos, SEEK_SET);
6678  avio_wb32(pb, mov->mdat_size + 8);
6679  } else {
6680  /* overwrite 'wide' placeholder atom */
6681  avio_seek(pb, mov->mdat_pos - 8, SEEK_SET);
6682  /* special value: real atom size will be 64 bit value after
6683  * tag field */
6684  avio_wb32(pb, 1);
6685  ffio_wfourcc(pb, "mdat");
6686  avio_wb64(pb, mov->mdat_size + 16);
6687  }
6688  avio_seek(pb, mov->reserved_moov_size > 0 ? mov->reserved_header_pos : moov_pos, SEEK_SET);
6689 
6690  if (mov->flags & FF_MOV_FLAG_FASTSTART) {
6691  av_log(s, AV_LOG_INFO, "Starting second pass: moving the moov atom to the beginning of the file\n");
6692  res = shift_data(s);
6693  if (res < 0)
6694  return res;
6695  avio_seek(pb, mov->reserved_header_pos, SEEK_SET);
6696  if ((res = mov_write_moov_tag(pb, mov, s)) < 0)
6697  return res;
6698  } else if (mov->reserved_moov_size > 0) {
6699  int64_t size;
6700  if ((res = mov_write_moov_tag(pb, mov, s)) < 0)
6701  return res;
6702  size = mov->reserved_moov_size - (avio_tell(pb) - mov->reserved_header_pos);
6703  if (size < 8){
6704  av_log(s, AV_LOG_ERROR, "reserved_moov_size is too small, needed %"PRId64" additional\n", 8-size);
6705  return AVERROR(EINVAL);
6706  }
6707  avio_wb32(pb, size);
6708  ffio_wfourcc(pb, "free");
6709  ffio_fill(pb, 0, size - 8);
6710  avio_seek(pb, moov_pos, SEEK_SET);
6711  } else {
6712  if ((res = mov_write_moov_tag(pb, mov, s)) < 0)
6713  return res;
6714  }
6715  res = 0;
6716  } else {
6718  for (i = 0; i < mov->nb_streams; i++)
6719  mov->tracks[i].data_offset = 0;
6720  if (mov->flags & FF_MOV_FLAG_GLOBAL_SIDX) {
6721  int64_t end;
6722  av_log(s, AV_LOG_INFO, "Starting second pass: inserting sidx atoms\n");
6723  res = shift_data(s);
6724  if (res < 0)
6725  return res;
6726  end = avio_tell(pb);
6727  avio_seek(pb, mov->reserved_header_pos, SEEK_SET);
6728  mov_write_sidx_tags(pb, mov, -1, 0);
6729  avio_seek(pb, end, SEEK_SET);
6731  mov_write_mfra_tag(pb, mov);
6732  } else if (!(mov->flags & FF_MOV_FLAG_SKIP_TRAILER)) {
6734  mov_write_mfra_tag(pb, mov);
6735  }
6736  }
6737 
6738  return res;
6739 }
6740 
6741 static int mov_check_bitstream(struct AVFormatContext *s, const AVPacket *pkt)
6742 {
6743  int ret = 1;
6744  AVStream *st = s->streams[pkt->stream_index];
6745 
6746  if (st->codecpar->codec_id == AV_CODEC_ID_AAC) {
6747  if (pkt->size > 2 && (AV_RB16(pkt->data) & 0xfff0) == 0xfff0)
6748  ret = ff_stream_add_bitstream_filter(st, "aac_adtstoasc", NULL);
6749  } else if (st->codecpar->codec_id == AV_CODEC_ID_VP9) {
6750  ret = ff_stream_add_bitstream_filter(st, "vp9_superframe", NULL);
6751  }
6752 
6753  return ret;
6754 }
6755 
6756 static const AVCodecTag codec_3gp_tags[] = {
6757  { AV_CODEC_ID_H263, MKTAG('s','2','6','3') },
6758  { AV_CODEC_ID_H264, MKTAG('a','v','c','1') },
6759  { AV_CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
6760  { AV_CODEC_ID_AAC, MKTAG('m','p','4','a') },
6761  { AV_CODEC_ID_AMR_NB, MKTAG('s','a','m','r') },
6762  { AV_CODEC_ID_AMR_WB, MKTAG('s','a','w','b') },
6763  { AV_CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
6764  { AV_CODEC_ID_NONE, 0 },
6765 };
6766 
6768  { AV_CODEC_ID_MPEG4 , MKTAG('m', 'p', '4', 'v') },
6769  { AV_CODEC_ID_H264 , MKTAG('a', 'v', 'c', '1') },
6770  { AV_CODEC_ID_H264 , MKTAG('a', 'v', 'c', '3') },
6771  { AV_CODEC_ID_HEVC , MKTAG('h', 'e', 'v', '1') },
6772  { AV_CODEC_ID_HEVC , MKTAG('h', 'v', 'c', '1') },
6773  { AV_CODEC_ID_MPEG2VIDEO , MKTAG('m', 'p', '4', 'v') },
6774  { AV_CODEC_ID_MPEG1VIDEO , MKTAG('m', 'p', '4', 'v') },
6775  { AV_CODEC_ID_MJPEG , MKTAG('m', 'p', '4', 'v') },
6776  { AV_CODEC_ID_PNG , MKTAG('m', 'p', '4', 'v') },
6777  { AV_CODEC_ID_JPEG2000 , MKTAG('m', 'p', '4', 'v') },
6778  { AV_CODEC_ID_VC1 , MKTAG('v', 'c', '-', '1') },
6779  { AV_CODEC_ID_DIRAC , MKTAG('d', 'r', 'a', 'c') },
6780  { AV_CODEC_ID_TSCC2 , MKTAG('m', 'p', '4', 'v') },
6781  { AV_CODEC_ID_VP9 , MKTAG('v', 'p', '0', '9') },
6782  { AV_CODEC_ID_AV1 , MKTAG('a', 'v', '0', '1') },
6783  { AV_CODEC_ID_AAC , MKTAG('m', 'p', '4', 'a') },
6784  { AV_CODEC_ID_MP4ALS , MKTAG('m', 'p', '4', 'a') },
6785  { AV_CODEC_ID_MP3 , MKTAG('m', 'p', '4', 'a') },
6786  { AV_CODEC_ID_MP2 , MKTAG('m', 'p', '4', 'a') },
6787  { AV_CODEC_ID_AC3 , MKTAG('a', 'c', '-', '3') },
6788  { AV_CODEC_ID_EAC3 , MKTAG('e', 'c', '-', '3') },
6789  { AV_CODEC_ID_DTS , MKTAG('m', 'p', '4', 'a') },
6790  { AV_CODEC_ID_FLAC , MKTAG('f', 'L', 'a', 'C') },
6791  { AV_CODEC_ID_OPUS , MKTAG('O', 'p', 'u', 's') },
6792  { AV_CODEC_ID_VORBIS , MKTAG('m', 'p', '4', 'a') },
6793  { AV_CODEC_ID_QCELP , MKTAG('m', 'p', '4', 'a') },
6794  { AV_CODEC_ID_EVRC , MKTAG('m', 'p', '4', 'a') },
6795  { AV_CODEC_ID_DVD_SUBTITLE, MKTAG('m', 'p', '4', 's') },
6796  { AV_CODEC_ID_MOV_TEXT , MKTAG('t', 'x', '3', 'g') },
6797  { AV_CODEC_ID_BIN_DATA , MKTAG('g', 'p', 'm', 'd') },
6798  { AV_CODEC_ID_NONE , 0 },
6799 };
6800 
6802  { AV_CODEC_ID_WMAPRO , MKTAG('w', 'm', 'a', ' ') },
6803  { AV_CODEC_ID_NONE , 0 },
6804 };
6805 
6806 static const AVCodecTag codec_ipod_tags[] = {
6807  { AV_CODEC_ID_H264, MKTAG('a','v','c','1') },
6808  { AV_CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
6809  { AV_CODEC_ID_AAC, MKTAG('m','p','4','a') },
6810  { AV_CODEC_ID_ALAC, MKTAG('a','l','a','c') },
6811  { AV_CODEC_ID_AC3, MKTAG('a','c','-','3') },
6812  { AV_CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
6813  { AV_CODEC_ID_MOV_TEXT, MKTAG('t','e','x','t') },
6814  { AV_CODEC_ID_NONE, 0 },
6815 };
6816 
6817 static const AVCodecTag codec_f4v_tags[] = {
6818  { AV_CODEC_ID_MP3, MKTAG('.','m','p','3') },
6819  { AV_CODEC_ID_AAC, MKTAG('m','p','4','a') },
6820  { AV_CODEC_ID_H264, MKTAG('a','v','c','1') },
6821  { AV_CODEC_ID_VP6A, MKTAG('V','P','6','A') },
6822  { AV_CODEC_ID_VP6F, MKTAG('V','P','6','F') },
6823  { AV_CODEC_ID_NONE, 0 },
6824 };
6825 
6826 #if CONFIG_MOV_MUXER
6827 MOV_CLASS(mov)
6829  .name = "mov",
6830  .long_name = NULL_IF_CONFIG_SMALL("QuickTime / MOV"),
6831  .extensions = "mov",
6832  .priv_data_size = sizeof(MOVMuxContext),
6833  .audio_codec = AV_CODEC_ID_AAC,
6834  .video_codec = CONFIG_LIBX264_ENCODER ?
6836  .init = mov_init,
6840  .deinit = mov_free,
6842  .codec_tag = (const AVCodecTag* const []){
6844  },
6845  .check_bitstream = mov_check_bitstream,
6846  .priv_class = &mov_muxer_class,
6847 };
6848 #endif
6849 #if CONFIG_TGP_MUXER
6850 MOV_CLASS(tgp)
6852  .name = "3gp",
6853  .long_name = NULL_IF_CONFIG_SMALL("3GP (3GPP file format)"),
6854  .extensions = "3gp",
6855  .priv_data_size = sizeof(MOVMuxContext),
6856  .audio_codec = AV_CODEC_ID_AMR_NB,
6857  .video_codec = AV_CODEC_ID_H263,
6858  .init = mov_init,
6862  .deinit = mov_free,
6864  .codec_tag = (const AVCodecTag* const []){ codec_3gp_tags, 0 },
6865  .check_bitstream = mov_check_bitstream,
6866  .priv_class = &tgp_muxer_class,
6867 };
6868 #endif
6869 #if CONFIG_MP4_MUXER
6870 MOV_CLASS(mp4)
6872  .name = "mp4",
6873  .long_name = NULL_IF_CONFIG_SMALL("MP4 (MPEG-4 Part 14)"),
6874  .mime_type = "video/mp4",
6875  .extensions = "mp4",
6876  .priv_data_size = sizeof(MOVMuxContext),
6877  .audio_codec = AV_CODEC_ID_AAC,
6878  .video_codec = CONFIG_LIBX264_ENCODER ?
6880  .init = mov_init,
6884  .deinit = mov_free,
6886  .codec_tag = (const AVCodecTag* const []){ codec_mp4_tags, 0 },
6887  .check_bitstream = mov_check_bitstream,
6888  .priv_class = &mp4_muxer_class,
6889 };
6890 #endif
6891 #if CONFIG_PSP_MUXER
6892 MOV_CLASS(psp)
6894  .name = "psp",
6895  .long_name = NULL_IF_CONFIG_SMALL("PSP MP4 (MPEG-4 Part 14)"),
6896  .extensions = "mp4,psp",
6897  .priv_data_size = sizeof(MOVMuxContext),
6898  .audio_codec = AV_CODEC_ID_AAC,
6899  .video_codec = CONFIG_LIBX264_ENCODER ?
6901  .init = mov_init,
6905  .deinit = mov_free,
6907  .codec_tag = (const AVCodecTag* const []){ codec_mp4_tags, 0 },
6908  .check_bitstream = mov_check_bitstream,
6909  .priv_class = &psp_muxer_class,
6910 };
6911 #endif
6912 #if CONFIG_TG2_MUXER
6913 MOV_CLASS(tg2)
6915  .name = "3g2",
6916  .long_name = NULL_IF_CONFIG_SMALL("3GP2 (3GPP2 file format)"),
6917  .extensions = "3g2",
6918  .priv_data_size = sizeof(MOVMuxContext),
6919  .audio_codec = AV_CODEC_ID_AMR_NB,
6920  .video_codec = AV_CODEC_ID_H263,
6921  .init = mov_init,
6925  .deinit = mov_free,
6927  .codec_tag = (const AVCodecTag* const []){ codec_3gp_tags, 0 },
6928  .check_bitstream = mov_check_bitstream,
6929  .priv_class = &tg2_muxer_class,
6930 };
6931 #endif
6932 #if CONFIG_IPOD_MUXER
6933 MOV_CLASS(ipod)
6935  .name = "ipod",
6936  .long_name = NULL_IF_CONFIG_SMALL("iPod H.264 MP4 (MPEG-4 Part 14)"),
6937  .mime_type = "video/mp4",
6938  .extensions = "m4v,m4a,m4b",
6939  .priv_data_size = sizeof(MOVMuxContext),
6940  .audio_codec = AV_CODEC_ID_AAC,
6941  .video_codec = AV_CODEC_ID_H264,
6942  .init = mov_init,
6946  .deinit = mov_free,
6948  .codec_tag = (const AVCodecTag* const []){ codec_ipod_tags, 0 },
6949  .check_bitstream = mov_check_bitstream,
6950  .priv_class = &ipod_muxer_class,
6951 };
6952 #endif
6953 #if CONFIG_ISMV_MUXER
6954 MOV_CLASS(ismv)
6956  .name = "ismv",
6957  .long_name = NULL_IF_CONFIG_SMALL("ISMV/ISMA (Smooth Streaming)"),
6958  .mime_type = "video/mp4",
6959  .extensions = "ismv,isma",
6960  .priv_data_size = sizeof(MOVMuxContext),
6961  .audio_codec = AV_CODEC_ID_AAC,
6962  .video_codec = AV_CODEC_ID_H264,
6963  .init = mov_init,
6967  .deinit = mov_free,
6969  .codec_tag = (const AVCodecTag* const []){
6971  .check_bitstream = mov_check_bitstream,
6972  .priv_class = &ismv_muxer_class,
6973 };
6974 #endif
6975 #if CONFIG_F4V_MUXER
6976 MOV_CLASS(f4v)
6978  .name = "f4v",
6979  .long_name = NULL_IF_CONFIG_SMALL("F4V Adobe Flash Video"),
6980  .mime_type = "application/f4v",
6981  .extensions = "f4v",
6982  .priv_data_size = sizeof(MOVMuxContext),
6983  .audio_codec = AV_CODEC_ID_AAC,
6984  .video_codec = AV_CODEC_ID_H264,
6985  .init = mov_init,
6989  .deinit = mov_free,
6991  .codec_tag = (const AVCodecTag* const []){ codec_f4v_tags, 0 },
6992  .check_bitstream = mov_check_bitstream,
6993  .priv_class = &f4v_muxer_class,
6994 };
6995 #endif
unsigned int nb_chapters
Number of chapters in AVChapter array.
Definition: avformat.h:1580
int32_t pitch
Rotation around the right vector [-90, 90].
Definition: spherical.h:127
#define FF_PROFILE_DNXHD
Definition: avcodec.h:2874
const char * name
Definition: avisynth_c.h:775
#define MOV_TRACK_STPS
Definition: movenc.h:96
static int utf8len(const uint8_t *b)
Definition: movenc.c:119
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / SMPTE RP177 Annex B
Definition: pixfmt.h:488
#define FF_COMPLIANCE_EXPERIMENTAL
Allow nonstandardized experimental things.
Definition: avcodec.h:2597
static int mov_write_extradata_tag(AVIOContext *pb, MOVTrack *track)
This function writes extradata "as is".
Definition: movenc.c:585
packed YUV 4:2:2, 16bpp, Cb Y0 Cr Y1
Definition: pixfmt.h:81
int ff_mov_iso639_to_lang(const char lang[4], int mp4)
Definition: isom.c:408
int cid
Definition: mxfenc.c:1948
uint8_t bitstream_mode
Definition: ac3.h:187
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
Definition: ffmpeg.c:690
internal header for HEVC (de)muxer utilities
#define FF_MOV_FLAG_DELAY_MOOV
Definition: movenc.h:252
int(* io_open)(struct AVFormatContext *s, AVIOContext **pb, const char *url, int flags, AVDictionary **options)
A callback for opening new IO streams.
Definition: avformat.h:1933
void avio_wb64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:463
#define AV_RB8(x)
Definition: intreadwrite.h:395
static void av_unused put_bits32(PutBitContext *s, uint32_t value)
Write exactly 32 bits into a bitstream.
Definition: put_bits.h:250
#define NULL
Definition: coverity.c:32
const char const char void * val
Definition: avisynth_c.h:771
enum AVFieldOrder field_order
Video only.
Definition: avcodec.h:3981
static int mov_write_moof_tag_internal(AVIOContext *pb, MOVMuxContext *mov, int tracks, int moof_size)
Definition: movenc.c:4434
static int ascii_to_wc(AVIOContext *pb, const uint8_t *b)
Definition: movenc.c:3656
Bytestream IO Context.
Definition: avio.h:161
enum AVColorTransferCharacteristic color_trc
Definition: avcodec.h:3988
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int packet_entry
Definition: movenc.h:154
#define FF_MOV_FLAG_FASTSTART
Definition: movenc.h:246
long chunkCount
Definition: movenc.h:92
static int get_cluster_duration(MOVTrack *track, int cluster_idx)
Definition: movenc.c:973
static int mov_write_dinf_tag(AVIOContext *pb)
Definition: movenc.c:2468
int ff_interleaved_peek(AVFormatContext *s, int stream, AVPacket *pkt, int add_offset)
Find the next packet in the interleaving queue for the given stream.
Definition: mux.c:1139
Buffered I/O operations.
enum AVCodecID id
Definition: internal.h:45
int use_editlist
Definition: movenc.h:217
int size
#define GET_UTF8(val, GET_BYTE, ERROR)
Convert a UTF-8 character (up to 4 bytes) to its 32-bit UCS-4 encoded form.
Definition: common.h:385
static int mov_flush_fragment(AVFormatContext *s, int force)
Definition: movenc.c:4976
#define OPUS_SEEK_PREROLL_MS
Definition: oggparseopus.c:35
int tag
stsd fourcc
Definition: movenc.h:105
static AVRational find_fps(AVFormatContext *s, AVStream *st)
Definition: movenc.c:1329
int language
Definition: movenc.h:103
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1419
AVOption.
Definition: opt.h:246
int64_t dts_shift
Definition: movenc.h:122
static int mov_write_tfdt_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:4382
int ffio_close_null_buf(AVIOContext *s)
Close a null buffer.
Definition: aviobuf.c:1479
AVOutputFormat ff_f4v_muxer
static int mov_write_ac3_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:315
int ff_put_wav_header(AVFormatContext *s, AVIOContext *pb, AVCodecParameters *par, int flags)
Write WAVEFORMAT header structure.
Definition: riffenc.c:54
static void mov_write_psp_udta_tag(AVIOContext *pb, const char *str, const char *lang, int type)
Definition: movenc.c:3780
#define MOV_TFHD_DEFAULT_DURATION
Definition: isom.h:307
unsigned int entries
Definition: movenc.h:53
static unsigned int mov_get_codec_tag(AVFormatContext *s, MOVTrack *track)
Definition: movenc.c:1533
AVIOContext * mdat_buf
Definition: movenc.h:204
static int mov_write_tmcd_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:2147
Definition: isom.h:56
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:208
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:381
static void param_write_int(AVIOContext *pb, const char *name, int value)
Definition: movenc.c:3976
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
#define FF_MOV_FLAG_FRAGMENT
Definition: movenc.h:240
static void find_compressor(char *compressor_name, int len, MOVTrack *track)
Definition: movenc.c:1902
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:68
static int mov_write_subtitle_end_packet(AVFormatContext *s, int stream_index, int64_t dts)
Definition: movenc.c:5614
static int mov_write_enda_tag_be(AVIOContext *pb)
Definition: movenc.c:599
int nb_frag_info
Definition: movenc.h:144
int max_fragment_size
Definition: movenc.h:202
struct AVAESCTR * aes_ctr
Definition: movenccenc.h:34
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4899
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:293
const char * g
Definition: vf_curves.c:115
int ff_av1_filter_obus(AVIOContext *pb, const uint8_t *buf, int size)
Filter out AV1 OBUs not meant to be present in ISOBMFF sample data and write the resulting bitstream ...
Definition: av1.c:31
static int mov_write_dref_tag(AVIOContext *pb)
Definition: movenc.c:2323
static int mov_write_gpmd_tag(AVIOContext *pb, const MOVTrack *track)
Definition: movenc.c:2203
#define NTP_OFFSET_US
Definition: internal.h:245
int max_bitrate
Maximum bitrate of the stream, in bits per second.
Definition: avcodec.h:1113
static int mov_write_string_metadata(AVFormatContext *s, AVIOContext *pb, const char *name, const char *tag, int long_style)
Definition: movenc.c:3346
static const AVCodecTag codec_f4v_tags[]
Definition: movenc.c:6817
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM / IEC 61966-2-4 xvYCC601 ...
Definition: pixfmt.h:492
AVRational sample_aspect_ratio
Video only.
Definition: avcodec.h:3976
#define avpriv_request_sample(...)
This side data should be associated with a video stream and contains Stereoscopic 3D information in f...
Definition: avcodec.h:1226
static int mov_pcm_be_gt16(enum AVCodecID codec_id)
Definition: movenc.c:692
int ff_mov_add_hinted_packet(AVFormatContext *s, AVPacket *pkt, int track_index, int sample, uint8_t *sample_data, int sample_size)
Definition: movenchint.c:401
SMPTE ST 432-1 (2010) / P3 D65 / Display P3.
Definition: pixfmt.h:448
Video represents a portion of a sphere mapped on a flat surface using equirectangular projection...
Definition: spherical.h:72
static int mov_check_timecode_track(AVFormatContext *s, AVTimecode *tc, int src_index, const char *tcstr)
Definition: movenc.c:5835
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3900
static int mov_get_rawvideo_codec_tag(AVFormatContext *s, MOVTrack *track)
Definition: movenc.c:1507
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:936
int num
Numerator.
Definition: rational.h:59
packed RGB 5:5:5, 16bpp, (msb)1X 5R 5G 5B(lsb), little-endian, X=unused/undefined ...
Definition: pixfmt.h:108
int index
stream index in AVFormatContext
Definition: avformat.h:875
int size
Definition: avcodec.h:1446
int ff_hevc_annexb2mp4_buf(const uint8_t *buf_in, uint8_t **buf_out, int *size, int filter_ps, int *ps_count)
Writes Annex B formatted HEVC NAL units to a data buffer.
Definition: hevc.c:1081
const char * b
Definition: vf_curves.c:116
#define MOV_TRUN_SAMPLE_CTS
Definition: isom.h:318
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:246
#define AVIO_FLAG_READ
read-only
Definition: avio.h:654
#define av_bswap16
Definition: bswap.h:31
#define AV_RB24
Definition: intreadwrite.h:64
static int mov_write_dpxe_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1291
void ff_mov_cenc_free(MOVMuxCencContext *ctx)
Free a CENC context.
Definition: movenccenc.c:412
This side data contains an integer value representing the stream index of a "fallback" track...
Definition: avcodec.h:1252
also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC
Definition: pixfmt.h:493
#define tc
Definition: regdef.h:69
static av_always_inline uint64_t av_double2int(double f)
Reinterpret a double as a 64-bit integer.
Definition: intfloat.h:70
static int mov_write_trak_tag(AVFormatContext *s, AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track, AVStream *st)
Definition: movenc.c:3105
static int mov_write_sdtp_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:278
#define AV_RL16
Definition: intreadwrite.h:42
#define AV_DISPOSITION_HEARING_IMPAIRED
stream for hearing impaired audiences
Definition: avformat.h:832
long sample_count
Definition: movenc.h:90
Video represents a sphere mapped on a flat surface using equirectangular projection.
Definition: spherical.h:56
int end_reliable
Definition: movenc.h:121
SMPTE ST 431-2 (2011) / DCI P3.
Definition: pixfmt.h:447
static int mov_write_mdta_keys_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
Definition: movenc.c:3564
static int mov_write_pasp_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1783
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:331
unsigned int ff_codec_get_tag(const AVCodecTag *tags, enum AVCodecID id)
Definition: utils.c:3126
char * encryption_scheme_str
Definition: movenc.h:223
const char * key
int version
Definition: avisynth_c.h:766
#define FF_MOV_FLAG_ISML
Definition: movenc.h:245
static int mov_write_tcmi_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:2485
static int mov_write_track_metadata(AVIOContext *pb, AVStream *st, const char *tag, const char *str)
Definition: movenc.c:3064
static int mov_write_dfla_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:723
Views are next to each other.
Definition: stereo3d.h:67
#define MOV_TFHD_DURATION_IS_EMPTY
Definition: isom.h:310
static int mov_write_video_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track)
Definition: movenc.c:1930
AVStream * st
Definition: movenc.h:106
static int mov_write_colr_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1821
static int mov_write_source_reference_tag(AVIOContext *pb, MOVTrack *track, const char *reel_name)
Definition: movenc.c:2128
AVDictionary * metadata
Definition: avformat.h:1312
static uint16_t language_code(const char *str)
Definition: movenc.c:3667
#define FF_PROFILE_AAC_HE_V2
Definition: avcodec.h:2868
#define AVFMT_ALLOW_FLUSH
Format allows flushing.
Definition: avformat.h:478
static int is_cover_image(const AVStream *st)
Definition: movenc.c:148
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1390
static int mov_get_dnxhd_codec_tag(AVFormatContext *s, MOVTrack *track)
Definition: movenc.c:1498
int strict_std_compliance
Allow non-standard and experimental extension.
Definition: avformat.h:1659
uint16_t chan_loc
Definition: movenc.c:386
This struct describes the properties of an encoded stream.
Definition: avcodec.h:3892
#define AV_PKT_FLAG_DISPOSABLE
Flag is used to indicate packets that contain frames that can be discarded by the decoder...
Definition: avcodec.h:1496
static int mov_write_fiel_tag(AVIOContext *pb, MOVTrack *track, int field_order)
Definition: movenc.c:1650
AVCodecParameters * par
Definition: movenc.h:107
static int mov_write_tfrf_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track, int entry)
Definition: movenc.c:4279
int64_t track_duration
Definition: movenc.h:88
functionally identical to above
Definition: pixfmt.h:494
enum AVColorSpace color_space
Definition: avcodec.h:3989
int encryption_kid_len
Definition: movenc.h:228
#define MOV_TFHD_DEFAULT_BASE_IS_MOOF
Definition: isom.h:311
int ff_mov_cenc_write_packet(MOVMuxCencContext *ctx, AVIOContext *pb, const uint8_t *buf_in, int size)
Write a fully encrypted packet.
Definition: movenccenc.c:167
int frame_size
Audio only.
Definition: avcodec.h:4021
int last_sample_is_subtitle_end
Definition: movenc.h:89
static int mov_write_glbl_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:936
int entries_flushed
Definition: movenc.h:142
#define MOV_TKHD_FLAG_ENABLED
Definition: isom.h:330
packed RGB 5:6:5, 16bpp, (msb) 5R 6G 5B(lsb), little-endian
Definition: pixfmt.h:106
static int shift_data(AVFormatContext *s)
Definition: movenc.c:6559
Trailer data, which doesn&#39;t contain actual content, but only for finalizing the output file...
Definition: avio.h:140
Format I/O context.
Definition: avformat.h:1351
int64_t frag_start
Definition: movenc.h:140
static int mov_write_stsc_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:225
static int mov_write_trkn_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s, int disc)
Definition: movenc.c:3427
static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: movenc.c:5635
struct eac3_info::@258 substream[1]
int avpriv_ac3_parse_header(AC3HeaderInfo **phdr, const uint8_t *buf, size_t size)
Definition: ac3_parser.c:247
static int mov_write_st3d_tag(AVIOContext *pb, AVStereo3D *stereo_3d)
Definition: movenc.c:1680
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
static int mov_add_tfra_entries(AVIOContext *pb, MOVMuxContext *mov, int tracks, int size)
Definition: movenc.c:4328
static int mov_write_single_packet(AVFormatContext *s, AVPacket *pkt)
Definition: movenc.c:5529
Public dictionary API.
double avpriv_get_gamma_from_trc(enum AVColorTransferCharacteristic trc)
Determine a suitable &#39;gamma&#39; value to match the supplied AVColorTransferCharacteristic.
Definition: color_utils.c:28
int av_timecode_init_from_string(AVTimecode *tc, AVRational rate, const char *str, void *log_ctx)
Parse timecode representation (hh:mm:ss[:;.
Definition: timecode.c:194
static double cb(void *priv, double x, double y)
Definition: vf_geq.c:112
int first_trun
Definition: movenc.h:205
int empty_hdlr_name
Definition: movenc.h:236
static int mov_write_uuidusmt_tag(AVIOContext *pb, AVFormatContext *s)
Definition: movenc.c:3793
int64_t default_duration
Definition: movenc.h:131
#define READ_BLOCK
uint32_t flags
Definition: movenc.h:98
#define FF_MOV_FLAG_EMPTY_MOOV
Definition: movenc.h:241
#define FF_MOV_FLAG_WRITE_COLR
Definition: movenc.h:254
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition: avpacket.c:62
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:369
uint8_t
Round toward +infinity.
Definition: mathematics.h:83
static int nb_streams
Definition: ffprobe.c:276
static int mov_write_uuid_tag_psp(AVIOContext *pb, MOVTrack *mov)
Definition: movenc.c:3025
static int mov_write_minf_tag(AVFormatContext *s, AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track)
Definition: movenc.c:2675
unsigned frag_info_capacity
Definition: movenc.h:146
#define av_malloc(s)
int ff_hevc_annexb2mp4(AVIOContext *pb, const uint8_t *buf_in, int size, int filter_ps, int *ps_count)
Writes Annex B formatted HEVC NAL units to the provided AVIOContext.
Definition: hevc.c:1033
Opaque data information usually continuous.
Definition: avutil.h:203
int missing_duration_warned
Definition: movenc.h:221
static const struct @257 mov_pix_fmt_tags[]
static int mov_preroll_write_stbl_atoms(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:2338
int width
Video only.
Definition: avcodec.h:3966
uint64_t mdat_size
Definition: movenc.h:188
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:64
AVOutputFormat ff_ipod_muxer
unsigned int samples_in_chunk
Definition: movenc.h:51
#define MOV_CLASS(flavor)
Definition: movenc.c:109
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:77
static int mov_write_ctts_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:2237
AVOptions.
uint8_t lfe_on
Definition: ac3.h:189
AVCodecParameters * avcodec_parameters_alloc(void)
Allocate a new AVCodecParameters and set its fields to default values (unknown/invalid/0).
Definition: utils.c:2065
static int get_samples_per_packet(MOVTrack *track)
Definition: movenc.c:993
Stereo 3D type: this structure describes how two videos are packed within a single video surface...
Definition: stereo3d.h:176
static int mov_get_mpeg2_xdcam_codec_tag(AVFormatContext *s, MOVTrack *track)
Definition: movenc.c:1356
const AVCodecTag ff_codec_movvideo_tags[]
Definition: isom.c:75
int frag_interleave
Definition: movenc.h:220
static int mov_pcm_le_gt16(enum AVCodecID codec_id)
Definition: movenc.c:684
timecode is drop frame
Definition: timecode.h:36
#define AV_RB32
Definition: intreadwrite.h:130
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1463
static int mov_write_isml_manifest(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
Definition: movenc.c:3995
This side data contains a 3x3 transformation matrix describing an affine transformation that needs to...
Definition: avcodec.h:1220
int id
Format-specific stream ID.
Definition: avformat.h:881
void ff_format_io_close(AVFormatContext *s, AVIOContext **pb)
Definition: utils.c:5687
int encryption_key_len
Definition: movenc.h:226
static const AVOption options[]
Definition: movenc.c:60
#define MODE_PSP
Definition: movenc.h:39
static int mov_write_ms_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:700
void avio_write_marker(AVIOContext *s, int64_t time, enum AVIODataMarkerType type)
Mark the written bytestream as a specific type.
Definition: aviobuf.c:493
uint8_t * av_stream_get_side_data(const AVStream *stream, enum AVPacketSideDataType type, int *size)
Get side information from stream.
Definition: utils.c:5493
static int mov_write_tapt_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:2897
static int mov_create_chapter_track(AVFormatContext *s, int tracknum)
Definition: movenc.c:5737
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1419
int64_t duration
Definition: movenc.c:63
static int mov_write_vmhd_tag(AVIOContext *pb)
Definition: movenc.c:2569
#define MODE_MP4
Definition: movenc.h:36
packed RGB 5:6:5, 16bpp, (msb) 5R 6G 5B(lsb), big-endian
Definition: pixfmt.h:105
static int mov_write_prft_tag(AVIOContext *pb, MOVMuxContext *mov, int tracks)
Definition: movenc.c:4554
int pal_done
Definition: movenc.h:163
static int mov_write_amr_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:301
A point in the output bytestream where a demuxer can start parsing (for non self synchronizing bytest...
Definition: avio.h:128
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:94
static int mov_write_sidx_tags(AVIOContext *pb, MOVMuxContext *mov, int tracks, int ref_size)
Definition: movenc.c:4519
static int mov_write_moov_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
Definition: movenc.c:3900
double strtod(const char *, char **)
uint8_t * encryption_key
Definition: movenc.h:225
int ff_mov_cenc_init(MOVMuxCencContext *ctx, uint8_t *encryption_key, int use_subsamples, int bitexact)
Initialize a CENC context.
Definition: movenccenc.c:388
const char data[16]
Definition: mxf.c:91
const char * av_stereo3d_type_name(unsigned int type)
Provide a human-readable name of a given stereo3d type.
Definition: stereo3d.c:57
#define height
int ff_avc_write_annexb_extradata(const uint8_t *in, uint8_t **buf, int *size)
Definition: avc.c:197
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:40
int chapter_track
qt chapter track number
Definition: movenc.h:186
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1482
int need_rewrite_extradata
Definition: movenc.h:230
uint8_t * data
Definition: avcodec.h:1445
#define FF_MOV_FLAG_OMIT_TFHD_OFFSET
Definition: movenc.h:247
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition: rational.h:104
int ff_isom_write_vpcc(AVFormatContext *s, AVIOContext *pb, AVCodecParameters *par)
Writes VP codec configuration to the provided AVIOContext.
Definition: vpcc.c:149
int ff_isom_write_av1c(AVIOContext *pb, const uint8_t *buf, int size)
Writes AV1 extradata (Sequence Header and Metadata OBUs) to the provided AVIOContext.
Definition: av1.c:303
int start
timecode frame start (first base frame number)
Definition: timecode.h:42
void av_packet_move_ref(AVPacket *dst, AVPacket *src)
Move every field in src to dst and reset src.
Definition: avpacket.c:653
int64_t time
Definition: movenc.h:77
static void mov_free(AVFormatContext *s)
Definition: movenc.c:5935
#define MOV_TRUN_SAMPLE_SIZE
Definition: isom.h:316
uint32_t tag
Definition: movenc.c:1478
static int mov_write_covr(AVIOContext *pb, AVFormatContext *s)
Definition: movenc.c:3481
static int mov_write_itunes_hdlr_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
Definition: movenc.c:3271
int fragments
Definition: movenc.h:199
static int mov_write_subtitle_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1663
int iods_audio_profile
Definition: movenc.h:196
bitstream reader API header.
#define MOV_TIMESCALE
Definition: movenc.h:32
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
int max_fragment_duration
Definition: movenc.h:200
int av_match_ext(const char *filename, const char *extensions)
Return a positive value if the given filename has one of the given extensions, 0 otherwise.
Definition: format.c:38
static int mov_write_moof_tag(AVIOContext *pb, MOVMuxContext *mov, int tracks, int64_t mdat_size)
Definition: movenc.c:4597
uint8_t bitstream_id
Definition: ac3.h:186
Video is not stereoscopic (and metadata has to be there).
Definition: stereo3d.h:55
int buffer_size
The size of the buffer to which the ratecontrol is applied, in bits.
Definition: avcodec.h:1129
int64_t mdat_pos
Definition: movenc.h:187
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:35
void ff_mov_close_hinting(MOVTrack *track)
Definition: movenchint.c:459
static int mov_write_uuid_tag_ipod(AVIOContext *pb)
Write uuid atom.
Definition: movenc.c:1634
unsigned int size
Definition: movenc.h:50
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:218
static av_always_inline void ffio_wfourcc(AVIOContext *pb, const uint8_t *s)
Definition: avio_internal.h:58
static unsigned int mov_find_codec_tag(AVFormatContext *s, MOVTrack *track)
Definition: movenc.c:1611
#define AVFMT_FLAG_BITEXACT
When muxing, try to avoid writing any random/volatile data to the output.
Definition: avformat.h:1499
#define FF_MOV_FLAG_DASH
Definition: movenc.h:250
#define AV_WB16(p, v)
Definition: intreadwrite.h:405
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Allocate, reallocate, or free an array.
Definition: mem.c:198
static const uint16_t fiel_data[]
Definition: movenc.c:1646
uint64_t channel_layout
Audio only.
Definition: avcodec.h:4002
#define av_log(a,...)
#define MOV_TFHD_DEFAULT_SIZE
Definition: isom.h:308
static int mov_write_nmhd_tag(AVIOContext *pb)
Definition: movenc.c:2477
#define AV_OPT_FLAG_ENCODING_PARAM
a generic parameter which can be set by the user for muxing or encoding
Definition: opt.h:276
int ism_lookahead
Definition: movenc.h:203
struct AVOutputFormat * oformat
The output container format.
Definition: avformat.h:1370
int av_packet_ref(AVPacket *dst, const AVPacket *src)
Setup a new reference to the data described by a given packet.
Definition: avpacket.c:607
#define ROUNDED_DIV(a, b)
Definition: common.h:56
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: avcodec.h:3929
#define FF_COMPLIANCE_UNOFFICIAL
Allow unofficial extensions.
Definition: avcodec.h:2596
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1477
#define MODE_3G2
Definition: movenc.h:41
attribute_deprecated int av_copy_packet_side_data(AVPacket *dst, const AVPacket *src)
Copy packet side data.
Definition: avpacket.c:226
const AVCodecTag codec_ism_tags[]
Definition: movenc.c:6801
uint64_t ff_ntp_time(void)
Get the current time since NTP epoch in microseconds.
Definition: utils.c:4655
static int compute_sidx_size(AVFormatContext *s)
Definition: movenc.c:6544
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
uint32_t tref_tag
Definition: movenc.h:116
uint8_t lfeon
Definition: movenc.c:381
uint32_t flags
Definition: movenc.h:58
static int mov_write_eac3_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:533
#define MOV_TIMECODE_FLAG_DROPFRAME
Definition: movenc.h:99
static void mov_parse_vc1_frame(AVPacket *pkt, MOVTrack *trk)
Definition: movenc.c:4890
static int mov_write_udta_sdp(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:3043
int ff_parse_creation_time_metadata(AVFormatContext *s, int64_t *timestamp, int return_seconds)
Parse creation_time in AVFormatContext metadata if exists and warn if the parsing fails...
Definition: utils.c:5699
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:258
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:215
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1591
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:1554
int flags
Additional information about the frame packing.
Definition: stereo3d.h:185
static int mov_write_mdta_ilst_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
Definition: movenc.c:3592
static int mov_write_meta_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
Definition: movenc.c:3614
int64_t tfrf_offset
Definition: movenc.h:79
#define MOV_FRAG_SAMPLE_FLAG_DEPENDS_NO
Definition: isom.h:327
static int mov_write_mdat_tag(AVIOContext *pb, MOVMuxContext *mov)
Definition: movenc.c:4678
static int mov_write_stsz_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:189
int64_t duration
Definition: movenc.h:78
uint8_t frame_type
Definition: ac3.h:190
static void put_descr(AVIOContext *pb, int tag, unsigned int size)
Definition: movenc.c:607
FLAC (Free Lossless Audio Codec) decoder/demuxer common functions.
static int mov_write_ilst_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
Definition: movenc.c:3509
static int mov_write_avid_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1208
#define AV_RB16
Definition: intreadwrite.h:53
#define AVERROR(e)
Definition: error.h:43
uint8_t acmod
Definition: movenc.c:379
static int mov_write_int8_metadata(AVFormatContext *s, AVIOContext *pb, const char *name, const char *tag, int len)
Definition: movenc.c:3454
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, int *size)
Get side information from packet.
Definition: avpacket.c:350
AVPacket cover_image
Definition: movenc.h:136
int video_track_timescale
Definition: movenc.h:207
static int mov_write_dvc1_structs(MOVTrack *track, uint8_t *buf)
Definition: movenc.c:837
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:188
#define MODE_3GP
Definition: movenc.h:38
enum AVColorPrimaries color_primaries
Definition: avcodec.h:3987
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:95
char * url
input or output URL.
Definition: avformat.h:1447
uint8_t sr_code
Definition: ac3.h:185
static int mov_write_clap_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1768
const char * r
Definition: vf_curves.c:114
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
int ff_isom_write_hvcc(AVIOContext *pb, const uint8_t *data, int size, int ps_array_completeness)
Writes HEVC extradata (parameter sets, declarative SEI NAL units) to the provided AVIOContext...
Definition: hevc.c:1102
int ff_mov_cenc_avc_parse_nal_units(MOVMuxCencContext *ctx, AVIOContext *pb, const uint8_t *buf_in, int size)
Parse AVC NAL units from annex B format, the nal size and type are written in the clear while the bod...
Definition: movenccenc.c:192
AVStream * video_st
Definition: movenc.c:59
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: avcodec.h:559
#define av_fourcc2str(fourcc)
Definition: avutil.h:348
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3896
AVChapter ** chapters
Definition: avformat.h:1581
also ITU-R BT1361 / IEC 61966-2-4 / SMPTE RP177 Annex B
Definition: pixfmt.h:435
static AVDictionaryEntry * get_metadata_lang(AVFormatContext *s, const char *tag, int *lang)
Definition: movenc.c:3321
static int mov_write_trun_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track, int moof_size, int first, int end)
Definition: movenc.c:4202
#define FF_MOV_FLAG_WRITE_GAMA
Definition: movenc.h:255
static int mov_create_dvd_sub_decoder_specific_info(MOVTrack *track, AVStream *st)
Definition: movenc.c:5985
struct MOVTrack::@259 vc1_info
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:236
Coded AC-3 header values up to the lfeon element, plus derived values.
Definition: ac3.h:179
static av_always_inline av_const double round(double x)
Definition: libm.h:444
int height
active picture (w/o VBI) height for D-10/IMX
Definition: movenc.h:115
#define MOV_SAMPLE_DEPENDENCY_UNKNOWN
Definition: isom.h:335
static int mov_write_track_udta_tag(AVIOContext *pb, MOVMuxContext *mov, AVStream *st)
Definition: movenc.c:3078
#define FF_MOV_FLAG_SEPARATE_MOOF
Definition: movenc.h:243
int is_unaligned_qt_rgb
Definition: movenc.h:165
#define AES_CTR_KEY_SIZE
Definition: aes_ctr.h:30
static int mov_setup_track_ids(MOVMuxContext *mov, AVFormatContext *s)
Assign track ids.
Definition: movenc.c:3866
uint8_t bsmod
Definition: movenc.c:377
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:947
const AVCodecTag ff_codec_wav_tags[]
Definition: riff.c:481
#define FFMAX(a, b)
Definition: common.h:94
int ff_avc_parse_nal_units_buf(const uint8_t *buf_in, uint8_t **buf, int *size)
Definition: avc.c:93
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:92
#define MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES
Definition: isom.h:328
MOVEncryptionScheme encryption_scheme
Definition: movenc.h:224
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
Definition: avstring.c:83
#define fail()
Definition: checkasm.h:117
int mode
Definition: movenc.h:84
const AVCodecTag ff_mp4_obj_type[]
Definition: isom.c:34
static const AVCodecTag codec_cover_image_tags[]
Definition: movenc.c:1584
static av_always_inline uint64_t ff_dnxhd_parse_header_prefix(const uint8_t *buf)
Definition: dnxhddata.h:86
const AVCodecTag ff_codec_movsubtitle_tags[]
Definition: isom.c:370
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:93
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1451
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:85
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:3918
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
Allocate, reallocate, or free an array through a pointer to a pointer.
Definition: mem.c:205
static float distance(float x, float y, int band)
int vos_len
Definition: movenc.h:110
int iods_skip
Definition: movenc.h:194
static int mov_flush_fragment_interleaving(AVFormatContext *s, MOVTrack *track)
Definition: movenc.c:4950
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1407
uint32_t bound_bottom
Distance from the bottom edge.
Definition: spherical.h:170
int ff_avc_parse_nal_units(AVIOContext *pb, const uint8_t *buf_in, int size)
Definition: avc.c:71
int64_t pts
Definition: movenc.h:49
static int mov_write_identification(AVIOContext *pb, AVFormatContext *s)
Definition: movenc.c:4840
static int mov_write_gama_tag(AVIOContext *pb, MOVTrack *track, double gamma)
Definition: movenc.c:1796
#define LIBAVFORMAT_IDENT
Definition: version.h:46
int block_align
Audio only.
Definition: avcodec.h:4017
#define FF_MOV_FLAG_FRAG_DISCONT
Definition: movenc.h:251
static int mov_write_mdia_tag(AVFormatContext *s, AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track)
Definition: movenc.c:2749
uint64_t pos
Definition: movenc.h:47
#define MOV_DISPOSABLE_SAMPLE
Definition: movenc.h:57
int first_frag_written
Definition: movenc.h:152
MOVMuxCencContext cenc
Definition: movenc.h:160
int64_t dts
Definition: movenc.h:48
#define MOV_FRAG_INFO_ALLOC_INCREMENT
Definition: movenc.h:30
#define MOV_TRACK_ENABLED
Definition: movenc.h:97
static int mov_write_stts_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:2278
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:260
Video frame is split into 6 faces of a cube, and arranged on a 3x2 layout.
Definition: spherical.h:65
int void avio_flush(AVIOContext *s)
Force flushing of buffered data.
Definition: aviobuf.c:238
int ff_alloc_extradata(AVCodecParameters *par, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0...
Definition: utils.c:3299
int write_tmcd
Definition: movenc.h:234
int per_stream_grouping
Definition: movenc.h:214
int64_t data_offset
Definition: movenc.h:139
static int mov_write_tfhd_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track, int64_t moof_offset)
Definition: movenc.c:4142
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:129
void ffio_fill(AVIOContext *s, int b, int count)
Definition: aviobuf.c:204
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:254
#define FFMIN(a, b)
Definition: common.h:96
int nb_meta_tmcd
number of new created tmcd track based on metadata (aka not data copy)
Definition: movenc.h:185
Raw Video Codec.
const AVCodecTag ff_codec_bmp_tags[]
Definition: riff.c:32
static int mov_write_mvex_tag(AVIOContext *pb, MOVMuxContext *mov)
Definition: movenc.c:3200
uint8_t interlaced
Definition: mxfenc.c:2093
int audio_vbr
Definition: movenc.h:114
static int mov_check_bitstream(struct AVFormatContext *s, const AVPacket *pkt)
Definition: movenc.c:6741
static int mov_write_chpl_tag(AVIOContext *pb, AVFormatContext *s)
Definition: movenc.c:3696
#define width
uint32_t bit_rate
Definition: ac3.h:204
#define MOV_TKHD_FLAG_IN_MOVIE
Definition: isom.h:331
#define MOV_PARTIAL_SYNC_SAMPLE
Definition: movenc.h:56
int ff_mov_init_hinting(AVFormatContext *s, int index, int src_index)
Definition: movenchint.c:29
AVPacket pkt
Definition: movenc.c:360
#define FF_PROFILE_UNKNOWN
Definition: avcodec.h:2860
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:94
static int mov_write_avcc_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1172
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition: avformat.h:468
static int is_clcp_track(MOVTrack *track)
Definition: movenc.c:2578
ITU-R BT2020 non-constant luminance system.
Definition: pixfmt.h:497
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM
Definition: pixfmt.h:440
void avio_wb24(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:487
This side data should be associated with a video stream and corresponds to the AVSphericalMapping str...
Definition: avcodec.h:1340
uint32_t bound_right
Distance from the right edge.
Definition: spherical.h:169
const char * name
Definition: avformat.h:507
int ff_get_packet_palette(AVFormatContext *s, AVPacket *pkt, int ret, uint32_t *palette)
Retrieves the palette from a packet, either from side data, or appended to the video data in the pack...
Definition: utils.c:5725
internal header for RIFF based (de)muxers do NOT include this in end user applications ...
int32_t
AVFormatContext * ctx
Definition: movenc.c:48
static int mov_get_dv_codec_tag(AVFormatContext *s, MOVTrack *track)
Definition: movenc.c:1304
int ff_mov_cenc_avc_write_nal_units(AVFormatContext *s, MOVMuxCencContext *ctx, int nal_length_size, AVIOContext *pb, const uint8_t *buf_in, int size)
Write AVC NAL units that are in MP4 format, the nal size and type are written in the clear while the ...
Definition: movenccenc.c:232
long sample_size
Definition: movenc.h:91
static int mov_write_string_tag(AVIOContext *pb, const char *name, const char *value, int lang, int long_style)
Definition: movenc.c:3307
#define FLAC_STREAMINFO_SIZE
Definition: flac.h:34
int frag_discont
Definition: movenc.h:141
#define s(width, name)
Definition: cbs_vp9.c:257
static int mov_write_smhd_tag(AVIOContext *pb)
Definition: movenc.c:2559
static int mov_auto_flush_fragment(AVFormatContext *s, int force)
Definition: movenc.c:5186
int avoid_negative_ts
Avoid negative timestamps during muxing.
Definition: avformat.h:1682
int reserved_moov_size
0 for disabled, -1 for automatic, size otherwise
Definition: movenc.h:209
static int mov_write_stco_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:165
#define AV_RL32
Definition: intreadwrite.h:146
static void param_write_string(AVIOContext *pb, const char *name, const char *value)
Definition: movenc.c:3981
int32_t yaw
Rotation around the up vector [-180, 180].
Definition: spherical.h:126
#define MOV_TRUN_SAMPLE_DURATION
Definition: isom.h:315
int n
Definition: avisynth_c.h:684
AVDictionary * metadata
Definition: avformat.h:938
int use_stream_ids_as_track_ids
Definition: movenc.h:232
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:69
enum AVCodecID codec_id
Definition: vaapi_decode.c:364
enum AVColorRange color_range
Video only.
Definition: avcodec.h:3986
static int mov_write_hvcc_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1194
Usually treated as AVMEDIA_TYPE_DATA.
Definition: avutil.h:200
static int mov_write_edts_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track)
Definition: movenc.c:2929
static int mov_init(AVFormatContext *s)
Definition: movenc.c:6035
enum AVPixelFormat pix_fmt
Definition: movenc.c:1477
#define MOV_SAMPLE_DEPENDENCY_YES
Definition: isom.h:336
static int mov_write_trex_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:3187
int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd)
Rescale a 64-bit integer with specified rounding.
Definition: mathematics.c:58
static int mov_write_dops_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:743
if(ret< 0)
Definition: vf_mcdeint.c:279
#define FF_MOV_FLAG_FRAG_EVERY_FRAME
Definition: movenc.h:259
static int mov_get_h264_codec_tag(AVFormatContext *s, MOVTrack *track)
Definition: movenc.c:1418
preferred ID for MPEG-1/2 video decoding
Definition: avcodec.h:220
static int mov_write_raw_metadata_tag(AVFormatContext *s, AVIOContext *pb, const char *name, const char *key)
Definition: movenc.c:3636
#define AVERROR_EXPERIMENTAL
Requested feature is flagged experimental. Set strict_std_compliance if you really want to use it...
Definition: error.h:72
#define FF_ARRAY_ELEMS(a)
#define AV_DISPOSITION_ATTACHED_PIC
The stream is stored in the file as an attached picture/"cover art" (e.g.
Definition: avformat.h:842
#define MOV_TRACK_CTTS
Definition: movenc.h:95
offset must point to a pointer immediately followed by an int for the length
Definition: opt.h:229
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
Definition: utils.c:1185
the normal 2^n-1 "JPEG" YUV ranges
Definition: pixfmt.h:512
static int mov_write_mfhd_tag(AVIOContext *pb, MOVMuxContext *mov)
Definition: movenc.c:4127
int iods_video_profile
Definition: movenc.h:195
AVOutputFormat ff_psp_muxer
#define AV_DISPOSITION_VISUAL_IMPAIRED
stream for visual impaired audiences
Definition: avformat.h:833
uint32_t padding
Number of pixels to pad from the edge of each cube face.
Definition: spherical.h:182
int avio_put_str(AVIOContext *s, const char *str)
Write a NULL-terminated string.
Definition: aviobuf.c:385
Stream structure.
Definition: avformat.h:874
static int get_sidx_size(AVFormatContext *s)
Definition: movenc.c:6501
uint64_t ff_get_formatted_ntp_time(uint64_t ntp_time_us)
Get the NTP time stamp formatted as per the RFC-5905.
Definition: utils.c:4660
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
int64_t end
chapter start/end time in time_base units
Definition: avformat.h:1311
also ITU-R BT1361
Definition: pixfmt.h:459
This structure describes the bitrate properties of an encoded bitstream.
Definition: avcodec.h:1108
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:40
The AV_PKT_DATA_NEW_EXTRADATA is used to notify the codec or the format that the extradata buffer was...
Definition: avcodec.h:1167
static int mov_write_av1c_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1162
#define AV_DISPOSITION_DEFAULT
Definition: avformat.h:819
static unsigned int validate_codec_tag(const AVCodecTag *const *tags, unsigned int tag, int codec_id)
Definition: movenc.c:1591
enum AVStereo3DType type
How views are packed within the video.
Definition: stereo3d.h:180
enum AVPixelFormat avpriv_find_pix_fmt(const PixelFormatTag *tags, unsigned int fourcc)
Definition: utils.c:479
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
static int mov_write_esds_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:627
also ITU-R BT601-6 525 or 625 / ITU-R BT1358 525 or 625 / ITU-R BT1700 NTSC
Definition: pixfmt.h:464
int64_t end_pts
Definition: movenc.h:120
#define MOV_INDEX_CLUSTER_SIZE
Definition: movenc.h:31
int track_ids_ok
Definition: movenc.h:233
static int mov_write_d263_tag(AVIOContext *pb)
Definition: movenc.c:1150
#define FF_MOV_FLAG_NEGATIVE_CTS_OFFSETS
Definition: movenc.h:258
Views are on top of each other.
Definition: stereo3d.h:79
AVOutputFormat ff_tgp_muxer
static av_always_inline int vc1_unescape_buffer(const uint8_t *src, int size, uint8_t *dst)
Definition: vc1_common.h:70
functionally identical to above
Definition: pixfmt.h:442
const AVCodecTag ff_codec_movaudio_tags[]
Definition: isom.c:313
int duration
Definition: isom.h:58
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:260
int cts
Definition: movenc.h:54
Timecode helpers header.
AVIOContext * pb
I/O context.
Definition: avformat.h:1393
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:650
static void mov_prune_frag_info(MOVMuxContext *mov, int tracks, int max)
Definition: movenc.c:4368
const struct AVCodecTag *const * codec_tag
List of supported codec_id-codec_tag pairs, ordered by "better choice first".
Definition: avformat.h:532
#define MOV_SAMPLE_DEPENDENCY_NO
Definition: isom.h:337
#define FF_RTP_FLAG_OPTS(ctx, fieldname)
Definition: rtpenc.h:74
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:196
static uint32_t rgb_to_yuv(uint32_t rgb)
Definition: movenc.c:5969
#define MOV_TRUN_FIRST_SAMPLE_FLAGS
Definition: isom.h:314
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:598
#define FF_MOV_FLAG_FRAG_CUSTOM
Definition: movenc.h:244
uint32_t timecode_flags
Definition: movenc.h:102
int hint_track
the track that hints this track, -1 if no hint track is set
Definition: movenc.h:124
static int mov_write_hmhd_tag(AVIOContext *pb)
Definition: movenc.c:2660
int slices
Definition: movenc.h:155
static int mov_parse_mpeg2_frame(AVPacket *pkt, uint32_t *flags)
Definition: movenc.c:4869
packed YUV 4:2:2, 16bpp, Y0 Cb Y1 Cr
Definition: pixfmt.h:67
#define FF_MOV_FLAG_GLOBAL_SIDX
Definition: movenc.h:253
static int mov_write_dvc1_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:920
static void build_chunks(MOVTrack *trk)
Definition: movenc.c:3831
int ff_av1_filter_obus_buf(const uint8_t *buf, uint8_t **out, int *size)
Filter out AV1 OBUs not meant to be present in ISOBMFF sample data and write the resulting bitstream ...
Definition: av1.c:61
void * buf
Definition: avisynth_c.h:690
Replacements for frequently missing libm functions.
static int mov_write_stbl_tag(AVFormatContext *s, AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track)
Definition: movenc.c:2433
uint32_t default_size
Definition: movenc.h:133
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:487
int ff_mov_cenc_write_sinf_tag(MOVTrack *track, AVIOContext *pb, uint8_t *kid)
Write the sinf atom, contained inside stsd.
Definition: movenccenc.c:364
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:379
unsigned int count
Definition: isom.h:57
double value
Definition: eval.c:98
float gamma
Definition: movenc.h:218
#define FF_MOV_FLAG_RTP_HINT
Definition: movenc.h:239
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:460
#define AV_WB32(p, v)
Definition: intreadwrite.h:419
#define FF_COMPLIANCE_NORMAL
Definition: avcodec.h:2595
AVFormatContext * fc
Definition: movenc.h:215
Y , 16bpp, big-endian.
Definition: pixfmt.h:97
int index
Definition: gxfenc.c:89
static int mov_write_wfex_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:712
unsigned int chunkNum
Chunk number if the current entry is a chunk start otherwise 0.
Definition: movenc.h:52
unsigned int avpriv_toupper4(unsigned int x)
Definition: utils.c:1859
int32_t roll
Rotation around the forward vector [-180, 180].
Definition: spherical.h:128
static int co64_required(const MOVTrack *track)
Definition: movenc.c:141
Rational number (pair of numerator and denominator).
Definition: rational.h:58
uint64_t time
Definition: movenc.h:87
int first_packet_seq
Definition: movenc.h:149
#define FF_MOV_FLAG_SKIP_TRAILER
Definition: movenc.h:257
static void param_write_hex(AVIOContext *pb, const char *name, const uint8_t *value, int len)
Definition: movenc.c:3986
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:615
int64_t time
Definition: movenc.h:183
uint16_t frame_size
Definition: ac3.h:206
int has_keyframes
Definition: movenc.h:93
cl_device_type type
int ffio_open_null_buf(AVIOContext **s)
Open a write-only fake memory stream.
Definition: aviobuf.c:1469
int entry
Definition: movenc.h:85
static int mov_write_chan_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:766
static int mov_write_loci_tag(AVFormatContext *s, AVIOContext *pb)
Definition: movenc.c:3376
size_t av_strlcatf(char *dst, size_t size, const char *fmt,...)
Definition: avstring.c:101
SMPTE ST 428-1.
Definition: pixfmt.h:476
#define MOV_TFHD_DEFAULT_FLAGS
Definition: isom.h:309
#define AVFMT_AVOID_NEG_TS_AUTO
Enabled when required by target format.
Definition: avformat.h:1683
MOVFragmentInfo * frag_info
Definition: movenc.h:145
#define snprintf
Definition: snprintf.h:34
char * major_brand
Definition: movenc.h:212
int64_t start_cts
Definition: movenc.h:119
#define AVFMT_FLAG_AUTO_BSF
Add bitstream filters as requested by the muxer.
Definition: avformat.h:1510
This structure describes how to handle spherical videos, outlining information about projection...
Definition: spherical.h:82
#define MOV_TRUN_SAMPLE_FLAGS
Definition: isom.h:317
uint8_t * vos_data
Definition: movenc.h:111
int first_packet_entry
Definition: movenc.h:150
#define TAG_IS_AVCI(tag)
Definition: isom.h:340
uint32_t palette[AVPALETTE_COUNT]
Definition: movenc.h:162
AVOutputFormat ff_mov_muxer
size_t av_strlcat(char *dst, const char *src, size_t size)
Append the string src to the string dst, but to a total length of no more than size - 1 bytes...
Definition: avstring.c:93
Round toward -infinity.
Definition: mathematics.h:82
static const AVCodecTag codec_3gp_tags[]
Definition: movenc.c:6756
mfxU16 profile
Definition: qsvenc.c:44
static int mov_write_tkhd_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track, AVStream *st)
Definition: movenc.c:2782
static int mov_write_enda_tag(AVIOContext *pb)
Definition: movenc.c:591
static int mov_write_hdlr_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:2584
static int mov_write_gmhd_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:2507
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:475
#define flags(name, subs,...)
Definition: cbs_av1.c:610
AVOutputFormat ff_ismv_muxer
static void write_matrix(AVIOContext *pb, int16_t a, int16_t b, int16_t c, int16_t d, int16_t tx, int16_t ty)
Definition: movenc.c:2768
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:380
uint16_t data_rate
Definition: movenc.c:366
packed RGB 5:5:5, 16bpp, (msb)1X 5R 5G 5B(lsb), big-endian , X=unused/undefined
Definition: pixfmt.h:107
AVFormatContext * rtp_ctx
the format context for the hinting rtp muxer
Definition: movenc.h:126
int bits_per_raw_sample
This is the number of valid bits in each output sample.
Definition: avcodec.h:3955
uint8_t level
Definition: svq3.c:207
uint32_t bound_top
Distance from the top edge.
Definition: spherical.h:168
int track_id
Definition: movenc.h:104
static int mov_write_udta_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
Definition: movenc.c:3722
unsigned int tag
Definition: internal.h:46
static int mov_write_trailer(AVFormatContext *s)
Definition: movenc.c:6625
the normal 219*2^(n-8) "MPEG" YUV ranges
Definition: pixfmt.h:511
int64_t start
Definition: avformat.h:1311
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:159
int sample_rate
Audio only.
Definition: avcodec.h:4010
#define FF_MOV_FLAG_FRAG_KEYFRAME
Definition: movenc.h:242
#define AVFMT_AVOID_NEG_TS_MAKE_ZERO
Shift timestamps so that they start at 0.
Definition: avformat.h:1685
static int mov_write_3gp_udta_tag(AVIOContext *pb, AVFormatContext *s, const char *tag, const char *str)
Definition: movenc.c:3674
static int mov_write_sv3d_tag(AVFormatContext *s, AVIOContext *pb, AVSphericalMapping *spherical_mapping)
Definition: movenc.c:1710
static int mov_write_audio_tag(AVFormatContext *s, AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track)
Definition: movenc.c:1014
static int mov_write_mvhd_tag(AVIOContext *pb, MOVMuxContext *mov)
Definition: movenc.c:3211
Y , 1bpp, 0 is black, 1 is white, in each byte pixels are ordered from the msb to the lsb...
Definition: pixfmt.h:76
static int defined_frame_rate(AVFormatContext *s, AVStream *st)
Definition: movenc.c:1347
Main libavformat public API header.
static int mov_write_tfrf_tags(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track)
Definition: movenc.c:4314
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
Y , 8bpp.
Definition: pixfmt.h:74
void ff_sdp_write_media(char *buff, int size, AVStream *st, int idx, const char *dest_addr, const char *dest_type, int port, int ttl, AVFormatContext *fmt)
Append the media-specific SDP fragment for the media stream c to the buffer buff. ...
Definition: sdp.c:846
int packet_seq
Definition: movenc.h:153
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:84
static int mov_write_tfra_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:4628
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:101
#define MOV_TFHD_BASE_DATA_OFFSET
Definition: isom.h:305
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:107
static int mov_create_timecode_track(AVFormatContext *s, int index, int src_index, AVTimecode tc)
Definition: movenc.c:5844
Y , 1bpp, 0 is white, 1 is black, in each byte pixels are ordered from the msb to the lsb...
Definition: pixfmt.h:75
enum AVSphericalProjection projection
Projection type.
Definition: spherical.h:86
static uint32_t get_sample_flags(MOVTrack *track, MOVIentry *entry)
Definition: movenc.c:4136
#define MOV_SYNC_SAMPLE
Definition: movenc.h:55
static int mov_write_tfxd_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:4258
#define flag(name)
Definition: cbs_av1.c:602
static double c[64]
int profile
Codec-specific bitstream restrictions that the stream conforms to.
Definition: avcodec.h:3960
packed RGB 16:16:16, 48bpp, 16R, 16G, 16B, the 2-byte value for each R/G/B component is stored as big...
Definition: pixfmt.h:102
static int rtp_hinting_needed(const AVStream *st)
Definition: movenc.c:155
int disposition
AV_DISPOSITION_* bit field.
Definition: avformat.h:927
uint32_t max_packet_size
Definition: movenc.h:129
AVOutputFormat ff_mp4_muxer
static int mov_write_sidx_tag(AVIOContext *pb, MOVTrack *track, int ref_size, int total_sidx_size)
Definition: movenc.c:4457
Stereoscopic video.
int src_track
the track that this hint (or tmcd) track describes
Definition: movenc.h:125
static void init(int bf, int audio_preroll)
Definition: movenc.c:242
AVRational time_base
time base in which the start/end timestamps are specified
Definition: avformat.h:1310
int av_grow_packet(AVPacket *pkt, int grow_by)
Increase packet size, correctly zeroing padding.
Definition: avpacket.c:109
int multichannel_as_mono
Definition: movenc.h:108
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:33
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:925
static int mov_write_wave_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:794
static int mov_write_traf_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track, int64_t moof_offset, int moof_size)
Definition: movenc.c:4394
char * key
Definition: dict.h:86
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:48
int den
Denominator.
Definition: rational.h:60
MOVIentry * cluster
Definition: movenc.h:112
unsigned bps
Definition: movenc.c:1479
int av_timecode_check_frame_rate(AVRational rate)
Check if the timecode feature is available for the given frame rate.
Definition: timecode.c:179
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:782
ARIB STD-B67, known as "Hybrid log-gamma".
Definition: pixfmt.h:478
#define MODE_IPOD
Definition: movenc.h:42
static int mov_write_tmpo_tag(AVIOContext *pb, AVFormatContext *s)
Definition: movenc.c:3358
static int mov_write_mdta_hdlr_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
Definition: movenc.c:3549
#define FF_MOV_FLAG_DISABLE_CHPL
Definition: movenc.h:248
static int mov_write_iods_tag(AVIOContext *pb, MOVMuxContext *mov)
Definition: movenc.c:3158
A point in the output bytestream where a decoder can start decoding (i.e.
Definition: avio.h:122
static int mov_write_stss_tag(AVIOContext *pb, MOVTrack *track, uint32_t flag)
Definition: movenc.c:254
#define xf(width, name, var, range_min, range_max, subs,...)
Definition: cbs_av1.c:712
#define av_free(p)
char * value
Definition: dict.h:87
uint32_t default_sample_flags
Definition: movenc.h:132
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:85
unsigned timescale
Definition: movenc.h:86
int len
#define FF_MOV_FLAG_DEFAULT_BASE_MOOF
Definition: movenc.h:249
static int mov_write_mfra_tag(AVIOContext *pb, MOVMuxContext *mov)
Definition: movenc.c:4652
int avg_bitrate
Average bitrate of the stream, in bits per second.
Definition: avcodec.h:1123
int substreamid
substream identification
Definition: ac3.h:191
MOVPrftBox write_prft
Definition: movenc.h:235
static int mov_write_vpcc_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1182
int ff_stream_add_bitstream_filter(AVStream *st, const char *name, const char *args)
Add a bitstream filter to a stream.
Definition: utils.c:5562
uint8_t ec3_done
Definition: movenc.c:361
static int mov_write_string_data_tag(AVIOContext *pb, const char *data, int lang, int long_style)
Definition: movenc.c:3287
void * priv_data
Format private data.
Definition: avformat.h:1379
static int mov_write_header(AVFormatContext *s)
Definition: movenc.c:6347
This side data corresponds to the AVCPBProperties struct.
Definition: avcodec.h:1257
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:337
#define lrint
Definition: tablegen.h:53
uint32_t bound_left
Distance from the left edge.
Definition: spherical.h:167
int64_t reserved_header_pos
Definition: movenc.h:210
int ff_reshuffle_raw_rgb(AVFormatContext *s, AVPacket **ppkt, AVCodecParameters *par, int expected_stride)
Reshuffles the lines to use the user specified stride.
Definition: rawutils.c:25
static int64_t update_size(AVIOContext *pb, int64_t pos)
Definition: movenc.c:131
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: avcodec.h:3942
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:3914
#define FF_PROFILE_AAC_HE
Definition: avcodec.h:2867
#define MOV_TRUN_DATA_OFFSET
Definition: isom.h:313
int64_t start_dts
Definition: movenc.h:118
static int mov_write_mdhd_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track)
Definition: movenc.c:2710
#define CENC_KID_SIZE
Definition: movenccenc.h:29
int channels
Audio only.
Definition: avcodec.h:4006
#define LIBAVCODEC_IDENT
Definition: version.h:42
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1444
int tref_id
trackID of the referenced track
Definition: movenc.h:117
static int mov_write_ftyp_tag(AVIOContext *pb, AVFormatContext *s)
Definition: movenc.c:4690
#define FF_MOV_FLAG_USE_MDTA
Definition: movenc.h:256
also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC
Definition: pixfmt.h:441
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:377
static int check_pkt(AVFormatContext *s, AVPacket *pkt)
Definition: movenc.c:5200
ITU-R BT2020.
Definition: pixfmt.h:444
int first_packet_seen
Definition: movenc.h:151
#define av_freep(p)
internal header for VPx codec configuration utilities.
const PixelFormatTag avpriv_pix_fmt_bps_mov[]
Definition: raw.c:324
void INT64 INT64 count
Definition: avisynth_c.h:690
static const AVCodecTag codec_ipod_tags[]
Definition: movenc.c:6806
static unsigned compute_avg_bitrate(MOVTrack *track)
Definition: movenc.c:616
void INT64 start
Definition: avisynth_c.h:690
#define AV_DICT_IGNORE_SUFFIX
Return first entry in a dictionary whose first part corresponds to the search key, ignoring the suffix of the found key string.
Definition: dict.h:70
static int mov_write_rtp_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:2108
static int mov_write_tref_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:3014
int moov_written
Definition: movenc.h:198
#define MODE_F4V
Definition: movenc.h:44
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1021
static int mov_write_uuidprof_tag(AVIOContext *pb, AVFormatContext *s)
Definition: movenc.c:4769
AVOutputFormat ff_tg2_muxer
#define av_malloc_array(a, b)
uint8_t fscod
Definition: movenc.c:371
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: avcodec.h:3904
unsigned cluster_capacity
Definition: movenc.h:113
int num_blocks
number of audio blocks
Definition: ac3.h:195
int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: movenc.c:5238
uint8_t channel_mode
Definition: ac3.h:188
static int mov_get_lpcm_flags(enum AVCodecID codec_id)
Compute flags for &#39;lpcm&#39; tag.
Definition: movenc.c:948
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition: pixdesc.c:2362
int stream_index
Definition: avcodec.h:1447
#define MODE_MOV
Definition: movenc.h:37
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:903
uint32_t ff_mov_get_channel_layout_tag(enum AVCodecID codec_id, uint64_t channel_layout, uint32_t *bitmap)
Get the channel layout tag for the specified codec id and channel layout.
Definition: mov_chan.c:494
static double cr(void *priv, double x, double y)
Definition: vf_geq.c:113
void ff_mov_cenc_write_stbl_atoms(MOVMuxCencContext *ctx, AVIOContext *pb)
Write the cenc atoms that should reside inside stbl.
Definition: movenccenc.c:339
static int mov_write_stsd_tag(AVFormatContext *s, AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track)
Definition: movenc.c:2215
#define CONFIG_LIBX264_ENCODER
Definition: config.h:1394
#define AV_CH_LAYOUT_MONO
#define MKTAG(a, b, c, d)
Definition: common.h:366
#define AVFMT_TS_NEGATIVE
Format allows muxing negative timestamps.
Definition: avformat.h:484
uint8_t num_blocks
Definition: movenc.c:362
void * eac3_priv
Definition: movenc.h:158
static void enable_tracks(AVFormatContext *s)
Definition: movenc.c:5893
uint32_t flags
flags such as drop frame, +24 hours support, ...
Definition: timecode.h:43
const AVCodecTag codec_mp4_tags[]
Definition: movenc.c:6767
#define MODE_ISM
Definition: movenc.h:43
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
char * ff_data_to_hex(char *buf, const uint8_t *src, int size, int lowercase)
Definition: utils.c:4850
This structure stores compressed data.
Definition: avcodec.h:1422
int64_t offset
Definition: movenc.h:76
int min_fragment_duration
Definition: movenc.h:201
MOVTrack * tracks
Definition: movenc.h:189
uint8_t num_ind_sub
Definition: movenc.c:368
static av_always_inline const uint8_t * find_next_marker(const uint8_t *src, const uint8_t *end)
Find VC-1 marker in buffer.
Definition: vc1_common.h:59
uint8_t num_dep_sub
Definition: movenc.c:384
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1438
uint8_t bsid
Definition: movenc.c:373
AVIOContext * mdat_buf
Definition: movenc.h:138
int ff_isom_write_avcc(AVIOContext *pb, const uint8_t *data, int len)
Definition: avc.c:107
#define t2
Definition: regdef.h:30
Header data; this needs to be present for the stream to be decodeable.
Definition: avio.h:115
#define FFMAX3(a, b, c)
Definition: common.h:95
int nb_streams
Definition: movenc.h:184
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
int has_disposable
Definition: movenc.h:94
int avio_printf(AVIOContext *s, const char *fmt,...) av_printf_format(2
#define MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC
Definition: isom.h:321
#define FF_PUT_WAV_HEADER_FORCE_WAVEFORMATEX
Tell ff_put_wav_header() to use WAVEFORMATEX even for PCM codecs.
Definition: riff.h:54
static int compute_moov_size(AVFormatContext *s)
Definition: movenc.c:6519
uint8_t * encryption_kid
Definition: movenc.h:227
void * av_mallocz_array(size_t nmemb, size_t size)
Allocate a memory block for an array with av_mallocz().
Definition: mem.c:191
static int get_moov_size(AVFormatContext *s)
Definition: movenc.c:6488
bitstream writer API