FFmpeg  4.1.4
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;
1516  track->par->bits_per_coded_sample = mov_pix_fmt_tags[i].bps;
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  if (!tag || (s->strict_std_compliance >= FF_COMPLIANCE_NORMAL &&
1538  (track->par->codec_id == AV_CODEC_ID_DVVIDEO ||
1539  track->par->codec_id == AV_CODEC_ID_RAWVIDEO ||
1540  track->par->codec_id == AV_CODEC_ID_H263 ||
1541  track->par->codec_id == AV_CODEC_ID_H264 ||
1542  track->par->codec_id == AV_CODEC_ID_DNXHD ||
1543  track->par->codec_id == AV_CODEC_ID_MPEG2VIDEO ||
1544  av_get_bits_per_sample(track->par->codec_id)))) { // pcm audio
1545  if (track->par->codec_id == AV_CODEC_ID_DVVIDEO)
1546  tag = mov_get_dv_codec_tag(s, track);
1547  else if (track->par->codec_id == AV_CODEC_ID_RAWVIDEO)
1548  tag = mov_get_rawvideo_codec_tag(s, track);
1549  else if (track->par->codec_id == AV_CODEC_ID_MPEG2VIDEO)
1550  tag = mov_get_mpeg2_xdcam_codec_tag(s, track);
1551  else if (track->par->codec_id == AV_CODEC_ID_H264)
1552  tag = mov_get_h264_codec_tag(s, track);
1553  else if (track->par->codec_id == AV_CODEC_ID_DNXHD)
1554  tag = mov_get_dnxhd_codec_tag(s, track);
1555  else if (track->par->codec_type == AVMEDIA_TYPE_VIDEO) {
1557  if (!tag) { // if no mac fcc found, try with Microsoft tags
1559  if (tag)
1560  av_log(s, AV_LOG_WARNING, "Using MS style video codec tag, "
1561  "the file may be unplayable!\n");
1562  }
1563  } else if (track->par->codec_type == AVMEDIA_TYPE_AUDIO) {
1565  if (!tag) { // if no mac fcc found, try with Microsoft tags
1566  int ms_tag = ff_codec_get_tag(ff_codec_wav_tags, track->par->codec_id);
1567  if (ms_tag) {
1568  tag = MKTAG('m', 's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
1569  av_log(s, AV_LOG_WARNING, "Using MS style audio codec tag, "
1570  "the file may be unplayable!\n");
1571  }
1572  }
1573  } else if (track->par->codec_type == AVMEDIA_TYPE_SUBTITLE)
1575  }
1576 
1577  return tag;
1578 }
1579 
1581  { AV_CODEC_ID_MJPEG, 0xD },
1582  { AV_CODEC_ID_PNG, 0xE },
1583  { AV_CODEC_ID_BMP, 0x1B },
1584  { AV_CODEC_ID_NONE, 0 },
1585 };
1586 
1587 static unsigned int validate_codec_tag(const AVCodecTag *const *tags,
1588  unsigned int tag, int codec_id)
1589 {
1590  int i;
1591 
1592  /**
1593  * Check that tag + id is in the table
1594  */
1595  for (i = 0; tags && tags[i]; i++) {
1596  const AVCodecTag *codec_tags = tags[i];
1597  while (codec_tags->id != AV_CODEC_ID_NONE) {
1598  if (avpriv_toupper4(codec_tags->tag) == avpriv_toupper4(tag) &&
1599  codec_tags->id == codec_id)
1600  return codec_tags->tag;
1601  codec_tags++;
1602  }
1603  }
1604  return 0;
1605 }
1606 
1607 static unsigned int mov_find_codec_tag(AVFormatContext *s, MOVTrack *track)
1608 {
1609  if (is_cover_image(track->st))
1610  return ff_codec_get_tag(codec_cover_image_tags, track->par->codec_id);
1611 
1612  if (track->mode == MODE_IPOD)
1613  if (!av_match_ext(s->url, "m4a") &&
1614  !av_match_ext(s->url, "m4v") &&
1615  !av_match_ext(s->url, "m4b"))
1616  av_log(s, AV_LOG_WARNING, "Warning, extension is not .m4a nor .m4v "
1617  "Quicktime/Ipod might not play the file\n");
1618 
1619  if (track->mode == MODE_MOV) {
1620  return mov_get_codec_tag(s, track);
1621  } else
1622  return validate_codec_tag(s->oformat->codec_tag, track->par->codec_tag,
1623  track->par->codec_id);
1624 }
1625 
1626 /** Write uuid atom.
1627  * Needed to make file play in iPods running newest firmware
1628  * goes after avcC atom in moov.trak.mdia.minf.stbl.stsd.avc1
1629  */
1631 {
1632  avio_wb32(pb, 28);
1633  ffio_wfourcc(pb, "uuid");
1634  avio_wb32(pb, 0x6b6840f2);
1635  avio_wb32(pb, 0x5f244fc5);
1636  avio_wb32(pb, 0xba39a51b);
1637  avio_wb32(pb, 0xcf0323f3);
1638  avio_wb32(pb, 0x0);
1639  return 28;
1640 }
1641 
1642 static const uint16_t fiel_data[] = {
1643  0x0000, 0x0100, 0x0201, 0x0206, 0x0209, 0x020e
1644 };
1645 
1646 static int mov_write_fiel_tag(AVIOContext *pb, MOVTrack *track, int field_order)
1647 {
1648  unsigned mov_field_order = 0;
1649  if (field_order < FF_ARRAY_ELEMS(fiel_data))
1650  mov_field_order = fiel_data[field_order];
1651  else
1652  return 0;
1653  avio_wb32(pb, 10);
1654  ffio_wfourcc(pb, "fiel");
1655  avio_wb16(pb, mov_field_order);
1656  return 10;
1657 }
1658 
1660 {
1661  int64_t pos = avio_tell(pb);
1662  avio_wb32(pb, 0); /* size */
1663  avio_wl32(pb, track->tag); // store it byteswapped
1664  avio_wb32(pb, 0); /* Reserved */
1665  avio_wb16(pb, 0); /* Reserved */
1666  avio_wb16(pb, 1); /* Data-reference index */
1667 
1668  if (track->par->codec_id == AV_CODEC_ID_DVD_SUBTITLE)
1669  mov_write_esds_tag(pb, track);
1670  else if (track->par->extradata_size)
1671  avio_write(pb, track->par->extradata, track->par->extradata_size);
1672 
1673  return update_size(pb, pos);
1674 }
1675 
1676 static int mov_write_st3d_tag(AVIOContext *pb, AVStereo3D *stereo_3d)
1677 {
1678  int8_t stereo_mode;
1679 
1680  if (stereo_3d->flags != 0) {
1681  av_log(pb, AV_LOG_WARNING, "Unsupported stereo_3d flags %x. st3d not written.\n", stereo_3d->flags);
1682  return 0;
1683  }
1684 
1685  switch (stereo_3d->type) {
1686  case AV_STEREO3D_2D:
1687  stereo_mode = 0;
1688  break;
1689  case AV_STEREO3D_TOPBOTTOM:
1690  stereo_mode = 1;
1691  break;
1693  stereo_mode = 2;
1694  break;
1695  default:
1696  av_log(pb, AV_LOG_WARNING, "Unsupported stereo_3d type %s. st3d not written.\n", av_stereo3d_type_name(stereo_3d->type));
1697  return 0;
1698  }
1699  avio_wb32(pb, 13); /* size */
1700  ffio_wfourcc(pb, "st3d");
1701  avio_wb32(pb, 0); /* version = 0 & flags = 0 */
1702  avio_w8(pb, stereo_mode);
1703  return 13;
1704 }
1705 
1707 {
1708  int64_t sv3d_pos, svhd_pos, proj_pos;
1709  const char* metadata_source = s->flags & AVFMT_FLAG_BITEXACT ? "Lavf" : LIBAVFORMAT_IDENT;
1710 
1711  if (spherical_mapping->projection != AV_SPHERICAL_EQUIRECTANGULAR &&
1712  spherical_mapping->projection != AV_SPHERICAL_EQUIRECTANGULAR_TILE &&
1713  spherical_mapping->projection != AV_SPHERICAL_CUBEMAP) {
1714  av_log(pb, AV_LOG_WARNING, "Unsupported projection %d. sv3d not written.\n", spherical_mapping->projection);
1715  return 0;
1716  }
1717 
1718  sv3d_pos = avio_tell(pb);
1719  avio_wb32(pb, 0); /* size */
1720  ffio_wfourcc(pb, "sv3d");
1721 
1722  svhd_pos = avio_tell(pb);
1723  avio_wb32(pb, 0); /* size */
1724  ffio_wfourcc(pb, "svhd");
1725  avio_wb32(pb, 0); /* version = 0 & flags = 0 */
1726  avio_put_str(pb, metadata_source);
1727  update_size(pb, svhd_pos);
1728 
1729  proj_pos = avio_tell(pb);
1730  avio_wb32(pb, 0); /* size */
1731  ffio_wfourcc(pb, "proj");
1732 
1733  avio_wb32(pb, 24); /* size */
1734  ffio_wfourcc(pb, "prhd");
1735  avio_wb32(pb, 0); /* version = 0 & flags = 0 */
1736  avio_wb32(pb, spherical_mapping->yaw);
1737  avio_wb32(pb, spherical_mapping->pitch);
1738  avio_wb32(pb, spherical_mapping->roll);
1739 
1740  switch (spherical_mapping->projection) {
1743  avio_wb32(pb, 28); /* size */
1744  ffio_wfourcc(pb, "equi");
1745  avio_wb32(pb, 0); /* version = 0 & flags = 0 */
1746  avio_wb32(pb, spherical_mapping->bound_top);
1747  avio_wb32(pb, spherical_mapping->bound_bottom);
1748  avio_wb32(pb, spherical_mapping->bound_left);
1749  avio_wb32(pb, spherical_mapping->bound_right);
1750  break;
1751  case AV_SPHERICAL_CUBEMAP:
1752  avio_wb32(pb, 20); /* size */
1753  ffio_wfourcc(pb, "cbmp");
1754  avio_wb32(pb, 0); /* version = 0 & flags = 0 */
1755  avio_wb32(pb, 0); /* layout */
1756  avio_wb32(pb, spherical_mapping->padding); /* padding */
1757  break;
1758  }
1759  update_size(pb, proj_pos);
1760 
1761  return update_size(pb, sv3d_pos);
1762 }
1763 
1764 static int mov_write_clap_tag(AVIOContext *pb, MOVTrack *track)
1765 {
1766  avio_wb32(pb, 40);
1767  ffio_wfourcc(pb, "clap");
1768  avio_wb32(pb, track->par->width); /* apertureWidth_N */
1769  avio_wb32(pb, 1); /* apertureWidth_D (= 1) */
1770  avio_wb32(pb, track->height); /* apertureHeight_N */
1771  avio_wb32(pb, 1); /* apertureHeight_D (= 1) */
1772  avio_wb32(pb, 0); /* horizOff_N (= 0) */
1773  avio_wb32(pb, 1); /* horizOff_D (= 1) */
1774  avio_wb32(pb, 0); /* vertOff_N (= 0) */
1775  avio_wb32(pb, 1); /* vertOff_D (= 1) */
1776  return 40;
1777 }
1778 
1779 static int mov_write_pasp_tag(AVIOContext *pb, MOVTrack *track)
1780 {
1781  AVRational sar;
1782  av_reduce(&sar.num, &sar.den, track->par->sample_aspect_ratio.num,
1783  track->par->sample_aspect_ratio.den, INT_MAX);
1784 
1785  avio_wb32(pb, 16);
1786  ffio_wfourcc(pb, "pasp");
1787  avio_wb32(pb, sar.num);
1788  avio_wb32(pb, sar.den);
1789  return 16;
1790 }
1791 
1792 static int mov_write_gama_tag(AVIOContext *pb, MOVTrack *track, double gamma)
1793 {
1794  uint32_t gama = 0;
1795  if (gamma <= 0.0)
1796  {
1797  gamma = avpriv_get_gamma_from_trc(track->par->color_trc);
1798  }
1799  av_log(pb, AV_LOG_DEBUG, "gamma value %g\n", gamma);
1800 
1801  if (gamma > 1e-6) {
1802  gama = (uint32_t)lrint((double)(1<<16) * gamma);
1803  av_log(pb, AV_LOG_DEBUG, "writing gama value %"PRId32"\n", gama);
1804 
1805  av_assert0(track->mode == MODE_MOV);
1806  avio_wb32(pb, 12);
1807  ffio_wfourcc(pb, "gama");
1808  avio_wb32(pb, gama);
1809  return 12;
1810  }
1811  else {
1812  av_log(pb, AV_LOG_WARNING, "gamma value unknown, unable to write gama atom\n");
1813  }
1814  return 0;
1815 }
1816 
1817 static int mov_write_colr_tag(AVIOContext *pb, MOVTrack *track)
1818 {
1819  // Ref (MOV): https://developer.apple.com/library/mac/technotes/tn2162/_index.html#//apple_ref/doc/uid/DTS40013070-CH1-TNTAG9
1820  // Ref (MP4): ISO/IEC 14496-12:2012
1821 
1822  if (track->par->color_primaries == AVCOL_PRI_UNSPECIFIED &&
1823  track->par->color_trc == AVCOL_TRC_UNSPECIFIED &&
1824  track->par->color_space == AVCOL_SPC_UNSPECIFIED) {
1825  if ((track->par->width >= 1920 && track->par->height >= 1080)
1826  || (track->par->width == 1280 && track->par->height == 720)) {
1827  av_log(NULL, AV_LOG_WARNING, "color primaries unspecified, assuming bt709\n");
1829  } else if (track->par->width == 720 && track->height == 576) {
1830  av_log(NULL, AV_LOG_WARNING, "color primaries unspecified, assuming bt470bg\n");
1832  } else if (track->par->width == 720 &&
1833  (track->height == 486 || track->height == 480)) {
1834  av_log(NULL, AV_LOG_WARNING, "color primaries unspecified, assuming smpte170\n");
1836  } else {
1837  av_log(NULL, AV_LOG_WARNING, "color primaries unspecified, unable to assume anything\n");
1838  }
1839  switch (track->par->color_primaries) {
1840  case AVCOL_PRI_BT709:
1841  track->par->color_trc = AVCOL_TRC_BT709;
1842  track->par->color_space = AVCOL_SPC_BT709;
1843  break;
1844  case AVCOL_PRI_SMPTE170M:
1845  case AVCOL_PRI_BT470BG:
1846  track->par->color_trc = AVCOL_TRC_BT709;
1848  break;
1849  }
1850  }
1851 
1852  /* We should only ever be called by MOV or MP4. */
1853  av_assert0(track->mode == MODE_MOV || track->mode == MODE_MP4);
1854 
1855  avio_wb32(pb, 18 + (track->mode == MODE_MP4));
1856  ffio_wfourcc(pb, "colr");
1857  if (track->mode == MODE_MP4)
1858  ffio_wfourcc(pb, "nclx");
1859  else
1860  ffio_wfourcc(pb, "nclc");
1861  switch (track->par->color_primaries) {
1862  case AVCOL_PRI_BT709: avio_wb16(pb, 1); break;
1863  case AVCOL_PRI_BT470BG: avio_wb16(pb, 5); break;
1864  case AVCOL_PRI_SMPTE170M:
1865  case AVCOL_PRI_SMPTE240M: avio_wb16(pb, 6); break;
1866  case AVCOL_PRI_BT2020: avio_wb16(pb, 9); break;
1867  case AVCOL_PRI_SMPTE431: avio_wb16(pb, 11); break;
1868  case AVCOL_PRI_SMPTE432: avio_wb16(pb, 12); break;
1869  default: avio_wb16(pb, 2);
1870  }
1871  switch (track->par->color_trc) {
1872  case AVCOL_TRC_BT709: avio_wb16(pb, 1); break;
1873  case AVCOL_TRC_SMPTE170M: avio_wb16(pb, 1); break; // remapped
1874  case AVCOL_TRC_SMPTE240M: avio_wb16(pb, 7); break;
1875  case AVCOL_TRC_SMPTEST2084: avio_wb16(pb, 16); break;
1876  case AVCOL_TRC_SMPTE428: avio_wb16(pb, 17); break;
1877  case AVCOL_TRC_ARIB_STD_B67: avio_wb16(pb, 18); break;
1878  default: avio_wb16(pb, 2);
1879  }
1880  switch (track->par->color_space) {
1881  case AVCOL_SPC_BT709: avio_wb16(pb, 1); break;
1882  case AVCOL_SPC_BT470BG:
1883  case AVCOL_SPC_SMPTE170M: avio_wb16(pb, 6); break;
1884  case AVCOL_SPC_SMPTE240M: avio_wb16(pb, 7); break;
1885  case AVCOL_SPC_BT2020_NCL: avio_wb16(pb, 9); break;
1886  default: avio_wb16(pb, 2);
1887  }
1888 
1889  if (track->mode == MODE_MP4) {
1890  int full_range = track->par->color_range == AVCOL_RANGE_JPEG;
1891  avio_w8(pb, full_range << 7);
1892  return 19;
1893  } else {
1894  return 18;
1895  }
1896 }
1897 
1898 static void find_compressor(char * compressor_name, int len, MOVTrack *track)
1899 {
1900  AVDictionaryEntry *encoder;
1901  int xdcam_res = (track->par->width == 1280 && track->par->height == 720)
1902  || (track->par->width == 1440 && track->par->height == 1080)
1903  || (track->par->width == 1920 && track->par->height == 1080);
1904 
1905  if (track->mode == MODE_MOV &&
1906  (encoder = av_dict_get(track->st->metadata, "encoder", NULL, 0))) {
1907  av_strlcpy(compressor_name, encoder->value, 32);
1908  } else if (track->par->codec_id == AV_CODEC_ID_MPEG2VIDEO && xdcam_res) {
1910  AVStream *st = track->st;
1911  int rate = defined_frame_rate(NULL, st);
1912  av_strlcatf(compressor_name, len, "XDCAM");
1913  if (track->par->format == AV_PIX_FMT_YUV422P) {
1914  av_strlcatf(compressor_name, len, " HD422");
1915  } else if(track->par->width == 1440) {
1916  av_strlcatf(compressor_name, len, " HD");
1917  } else
1918  av_strlcatf(compressor_name, len, " EX");
1919 
1920  av_strlcatf(compressor_name, len, " %d%c", track->par->height, interlaced ? 'i' : 'p');
1921 
1922  av_strlcatf(compressor_name, len, "%d", rate * (interlaced + 1));
1923  }
1924 }
1925 
1927 {
1928  int64_t pos = avio_tell(pb);
1929  char compressor_name[32] = { 0 };
1930  int avid = 0;
1931 
1932  int uncompressed_ycbcr = ((track->par->codec_id == AV_CODEC_ID_RAWVIDEO && track->par->format == AV_PIX_FMT_UYVY422)
1933  || (track->par->codec_id == AV_CODEC_ID_RAWVIDEO && track->par->format == AV_PIX_FMT_YUYV422)
1934  || track->par->codec_id == AV_CODEC_ID_V308
1935  || track->par->codec_id == AV_CODEC_ID_V408
1936  || track->par->codec_id == AV_CODEC_ID_V410
1937  || track->par->codec_id == AV_CODEC_ID_V210);
1938 
1939  avio_wb32(pb, 0); /* size */
1940  if (mov->encryption_scheme != MOV_ENC_NONE) {
1941  ffio_wfourcc(pb, "encv");
1942  } else {
1943  avio_wl32(pb, track->tag); // store it byteswapped
1944  }
1945  avio_wb32(pb, 0); /* Reserved */
1946  avio_wb16(pb, 0); /* Reserved */
1947  avio_wb16(pb, 1); /* Data-reference index */
1948 
1949  if (uncompressed_ycbcr) {
1950  avio_wb16(pb, 2); /* Codec stream version */
1951  } else {
1952  avio_wb16(pb, 0); /* Codec stream version */
1953  }
1954  avio_wb16(pb, 0); /* Codec stream revision (=0) */
1955  if (track->mode == MODE_MOV) {
1956  ffio_wfourcc(pb, "FFMP"); /* Vendor */
1957  if (track->par->codec_id == AV_CODEC_ID_RAWVIDEO || uncompressed_ycbcr) {
1958  avio_wb32(pb, 0); /* Temporal Quality */
1959  avio_wb32(pb, 0x400); /* Spatial Quality = lossless*/
1960  } else {
1961  avio_wb32(pb, 0x200); /* Temporal Quality = normal */
1962  avio_wb32(pb, 0x200); /* Spatial Quality = normal */
1963  }
1964  } else {
1965  avio_wb32(pb, 0); /* Reserved */
1966  avio_wb32(pb, 0); /* Reserved */
1967  avio_wb32(pb, 0); /* Reserved */
1968  }
1969  avio_wb16(pb, track->par->width); /* Video width */
1970  avio_wb16(pb, track->height); /* Video height */
1971  avio_wb32(pb, 0x00480000); /* Horizontal resolution 72dpi */
1972  avio_wb32(pb, 0x00480000); /* Vertical resolution 72dpi */
1973  avio_wb32(pb, 0); /* Data size (= 0) */
1974  avio_wb16(pb, 1); /* Frame count (= 1) */
1975 
1976  /* FIXME not sure, ISO 14496-1 draft where it shall be set to 0 */
1977  find_compressor(compressor_name, 32, track);
1978  avio_w8(pb, strlen(compressor_name));
1979  avio_write(pb, compressor_name, 31);
1980 
1981  if (track->mode == MODE_MOV &&
1982  (track->par->codec_id == AV_CODEC_ID_V410 || track->par->codec_id == AV_CODEC_ID_V210))
1983  avio_wb16(pb, 0x18);
1984  else if (track->mode == MODE_MOV && track->par->bits_per_coded_sample)
1985  avio_wb16(pb, track->par->bits_per_coded_sample |
1986  (track->par->format == AV_PIX_FMT_GRAY8 ? 0x20 : 0));
1987  else
1988  avio_wb16(pb, 0x18); /* Reserved */
1989 
1990  if (track->mode == MODE_MOV && track->par->format == AV_PIX_FMT_PAL8) {
1991  int pal_size = 1 << track->par->bits_per_coded_sample;
1992  int i;
1993  avio_wb16(pb, 0); /* Color table ID */
1994  avio_wb32(pb, 0); /* Color table seed */
1995  avio_wb16(pb, 0x8000); /* Color table flags */
1996  avio_wb16(pb, pal_size - 1); /* Color table size (zero-relative) */
1997  for (i = 0; i < pal_size; i++) {
1998  uint32_t rgb = track->palette[i];
1999  uint16_t r = (rgb >> 16) & 0xff;
2000  uint16_t g = (rgb >> 8) & 0xff;
2001  uint16_t b = rgb & 0xff;
2002  avio_wb16(pb, 0);
2003  avio_wb16(pb, (r << 8) | r);
2004  avio_wb16(pb, (g << 8) | g);
2005  avio_wb16(pb, (b << 8) | b);
2006  }
2007  } else
2008  avio_wb16(pb, 0xffff); /* Reserved */
2009 
2010  if (track->tag == MKTAG('m','p','4','v'))
2011  mov_write_esds_tag(pb, track);
2012  else if (track->par->codec_id == AV_CODEC_ID_H263)
2013  mov_write_d263_tag(pb);
2014  else if (track->par->codec_id == AV_CODEC_ID_AVUI ||
2015  track->par->codec_id == AV_CODEC_ID_SVQ3) {
2016  mov_write_extradata_tag(pb, track);
2017  avio_wb32(pb, 0);
2018  } else if (track->par->codec_id == AV_CODEC_ID_DNXHD) {
2019  mov_write_avid_tag(pb, track);
2020  avid = 1;
2021  } else if (track->par->codec_id == AV_CODEC_ID_HEVC)
2022  mov_write_hvcc_tag(pb, track);
2023  else if (track->par->codec_id == AV_CODEC_ID_H264 && !TAG_IS_AVCI(track->tag)) {
2024  mov_write_avcc_tag(pb, track);
2025  if (track->mode == MODE_IPOD)
2027  } else if (track->par->codec_id == AV_CODEC_ID_VP9) {
2028  mov_write_vpcc_tag(mov->fc, pb, track);
2029  } else if (track->par->codec_id == AV_CODEC_ID_AV1) {
2030  mov_write_av1c_tag(pb, track);
2031  } else if (track->par->codec_id == AV_CODEC_ID_VC1 && track->vos_len > 0)
2032  mov_write_dvc1_tag(pb, track);
2033  else if (track->par->codec_id == AV_CODEC_ID_VP6F ||
2034  track->par->codec_id == AV_CODEC_ID_VP6A) {
2035  /* Don't write any potential extradata here - the cropping
2036  * is signalled via the normal width/height fields. */
2037  } else if (track->par->codec_id == AV_CODEC_ID_R10K) {
2038  if (track->par->codec_tag == MKTAG('R','1','0','k'))
2039  mov_write_dpxe_tag(pb, track);
2040  } else if (track->vos_len > 0)
2041  mov_write_glbl_tag(pb, track);
2042 
2043  if (track->par->codec_id != AV_CODEC_ID_H264 &&
2044  track->par->codec_id != AV_CODEC_ID_MPEG4 &&
2045  track->par->codec_id != AV_CODEC_ID_DNXHD) {
2046  int field_order = track->par->field_order;
2047 
2048 #if FF_API_LAVF_AVCTX
2050  if (field_order != track->st->codec->field_order && track->st->codec->field_order != AV_FIELD_UNKNOWN)
2051  field_order = track->st->codec->field_order;
2053 #endif
2054 
2055  if (field_order != AV_FIELD_UNKNOWN)
2056  mov_write_fiel_tag(pb, track, field_order);
2057  }
2058 
2059  if (mov->flags & FF_MOV_FLAG_WRITE_GAMA) {
2060  if (track->mode == MODE_MOV)
2061  mov_write_gama_tag(pb, track, mov->gamma);
2062  else
2063  av_log(mov->fc, AV_LOG_WARNING, "Not writing 'gama' atom. Format is not MOV.\n");
2064  }
2065  if (mov->flags & FF_MOV_FLAG_WRITE_COLR) {
2066  if (track->mode == MODE_MOV || track->mode == MODE_MP4)
2067  mov_write_colr_tag(pb, track);
2068  else
2069  av_log(mov->fc, AV_LOG_WARNING, "Not writing 'colr' atom. Format is not MOV or MP4.\n");
2070  }
2071 
2072  if (track->mode == MODE_MP4 && mov->fc->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL) {
2075 
2076  if (stereo_3d)
2077  mov_write_st3d_tag(pb, stereo_3d);
2078  if (spherical_mapping)
2079  mov_write_sv3d_tag(mov->fc, pb, spherical_mapping);
2080  }
2081 
2082  if (track->par->sample_aspect_ratio.den && track->par->sample_aspect_ratio.num) {
2083  mov_write_pasp_tag(pb, track);
2084  }
2085 
2086  if (uncompressed_ycbcr){
2087  mov_write_clap_tag(pb, track);
2088  }
2089 
2090  if (mov->encryption_scheme != MOV_ENC_NONE) {
2091  ff_mov_cenc_write_sinf_tag(track, pb, mov->encryption_kid);
2092  }
2093 
2094  /* extra padding for avid stsd */
2095  /* https://developer.apple.com/library/mac/documentation/QuickTime/QTFF/QTFFChap2/qtff2.html#//apple_ref/doc/uid/TP40000939-CH204-61112 */
2096  if (avid)
2097  avio_wb32(pb, 0);
2098 
2099  return update_size(pb, pos);
2100 }
2101 
2102 static int mov_write_rtp_tag(AVIOContext *pb, MOVTrack *track)
2103 {
2104  int64_t pos = avio_tell(pb);
2105  avio_wb32(pb, 0); /* size */
2106  ffio_wfourcc(pb, "rtp ");
2107  avio_wb32(pb, 0); /* Reserved */
2108  avio_wb16(pb, 0); /* Reserved */
2109  avio_wb16(pb, 1); /* Data-reference index */
2110 
2111  avio_wb16(pb, 1); /* Hint track version */
2112  avio_wb16(pb, 1); /* Highest compatible version */
2113  avio_wb32(pb, track->max_packet_size); /* Max packet size */
2114 
2115  avio_wb32(pb, 12); /* size */
2116  ffio_wfourcc(pb, "tims");
2117  avio_wb32(pb, track->timescale);
2118 
2119  return update_size(pb, pos);
2120 }
2121 
2122 static int mov_write_source_reference_tag(AVIOContext *pb, MOVTrack *track, const char *reel_name)
2123 {
2124  uint64_t str_size =strlen(reel_name);
2125  int64_t pos = avio_tell(pb);
2126 
2127  if (str_size >= UINT16_MAX){
2128  av_log(NULL, AV_LOG_ERROR, "reel_name length %"PRIu64" is too large\n", str_size);
2129  avio_wb16(pb, 0);
2130  return AVERROR(EINVAL);
2131  }
2132 
2133  avio_wb32(pb, 0); /* size */
2134  ffio_wfourcc(pb, "name"); /* Data format */
2135  avio_wb16(pb, str_size); /* string size */
2136  avio_wb16(pb, track->language); /* langcode */
2137  avio_write(pb, reel_name, str_size); /* reel name */
2138  return update_size(pb,pos);
2139 }
2140 
2141 static int mov_write_tmcd_tag(AVIOContext *pb, MOVTrack *track)
2142 {
2143  int64_t pos = avio_tell(pb);
2144 #if 1
2145  int frame_duration;
2146  int nb_frames;
2147  AVDictionaryEntry *t = NULL;
2148 
2149  if (!track->st->avg_frame_rate.num || !track->st->avg_frame_rate.den) {
2150 #if FF_API_LAVF_AVCTX
2152  frame_duration = av_rescale(track->timescale, track->st->codec->time_base.num, track->st->codec->time_base.den);
2153  nb_frames = ROUNDED_DIV(track->st->codec->time_base.den, track->st->codec->time_base.num);
2155 #else
2156  av_log(NULL, AV_LOG_ERROR, "avg_frame_rate not set for tmcd track.\n");
2157  return AVERROR(EINVAL);
2158 #endif
2159  } else {
2160  frame_duration = av_rescale(track->timescale, track->st->avg_frame_rate.num, track->st->avg_frame_rate.den);
2161  nb_frames = ROUNDED_DIV(track->st->avg_frame_rate.den, track->st->avg_frame_rate.num);
2162  }
2163 
2164  if (nb_frames > 255) {
2165  av_log(NULL, AV_LOG_ERROR, "fps %d is too large\n", nb_frames);
2166  return AVERROR(EINVAL);
2167  }
2168 
2169  avio_wb32(pb, 0); /* size */
2170  ffio_wfourcc(pb, "tmcd"); /* Data format */
2171  avio_wb32(pb, 0); /* Reserved */
2172  avio_wb32(pb, 1); /* Data reference index */
2173  avio_wb32(pb, 0); /* Flags */
2174  avio_wb32(pb, track->timecode_flags); /* Flags (timecode) */
2175  avio_wb32(pb, track->timescale); /* Timescale */
2176  avio_wb32(pb, frame_duration); /* Frame duration */
2177  avio_w8(pb, nb_frames); /* Number of frames */
2178  avio_w8(pb, 0); /* Reserved */
2179 
2180  t = av_dict_get(track->st->metadata, "reel_name", NULL, 0);
2181  if (t && utf8len(t->value) && track->mode != MODE_MP4)
2182  mov_write_source_reference_tag(pb, track, t->value);
2183  else
2184  avio_wb16(pb, 0); /* zero size */
2185 #else
2186 
2187  avio_wb32(pb, 0); /* size */
2188  ffio_wfourcc(pb, "tmcd"); /* Data format */
2189  avio_wb32(pb, 0); /* Reserved */
2190  avio_wb32(pb, 1); /* Data reference index */
2191  if (track->par->extradata_size)
2192  avio_write(pb, track->par->extradata, track->par->extradata_size);
2193 #endif
2194  return update_size(pb, pos);
2195 }
2196 
2197 static int mov_write_gpmd_tag(AVIOContext *pb, const MOVTrack *track)
2198 {
2199  int64_t pos = avio_tell(pb);
2200  avio_wb32(pb, 0); /* size */
2201  ffio_wfourcc(pb, "gpmd");
2202  avio_wb32(pb, 0); /* Reserved */
2203  avio_wb16(pb, 0); /* Reserved */
2204  avio_wb16(pb, 1); /* Data-reference index */
2205  avio_wb32(pb, 0); /* Reserved */
2206  return update_size(pb, pos);
2207 }
2208 
2210 {
2211  int64_t pos = avio_tell(pb);
2212  avio_wb32(pb, 0); /* size */
2213  ffio_wfourcc(pb, "stsd");
2214  avio_wb32(pb, 0); /* version & flags */
2215  avio_wb32(pb, 1); /* entry count */
2216  if (track->par->codec_type == AVMEDIA_TYPE_VIDEO)
2217  mov_write_video_tag(pb, mov, track);
2218  else if (track->par->codec_type == AVMEDIA_TYPE_AUDIO)
2219  mov_write_audio_tag(s, pb, mov, track);
2220  else if (track->par->codec_type == AVMEDIA_TYPE_SUBTITLE)
2221  mov_write_subtitle_tag(pb, track);
2222  else if (track->par->codec_tag == MKTAG('r','t','p',' '))
2223  mov_write_rtp_tag(pb, track);
2224  else if (track->par->codec_tag == MKTAG('t','m','c','d'))
2225  mov_write_tmcd_tag(pb, track);
2226  else if (track->par->codec_tag == MKTAG('g','p','m','d'))
2227  mov_write_gpmd_tag(pb, track);
2228  return update_size(pb, pos);
2229 }
2230 
2232 {
2233  MOVMuxContext *mov = s->priv_data;
2234  MOVStts *ctts_entries;
2235  uint32_t entries = 0;
2236  uint32_t atom_size;
2237  int i;
2238 
2239  ctts_entries = av_malloc_array((track->entry + 1), sizeof(*ctts_entries)); /* worst case */
2240  if (!ctts_entries)
2241  return AVERROR(ENOMEM);
2242  ctts_entries[0].count = 1;
2243  ctts_entries[0].duration = track->cluster[0].cts;
2244  for (i = 1; i < track->entry; i++) {
2245  if (track->cluster[i].cts == ctts_entries[entries].duration) {
2246  ctts_entries[entries].count++; /* compress */
2247  } else {
2248  entries++;
2249  ctts_entries[entries].duration = track->cluster[i].cts;
2250  ctts_entries[entries].count = 1;
2251  }
2252  }
2253  entries++; /* last one */
2254  atom_size = 16 + (entries * 8);
2255  avio_wb32(pb, atom_size); /* size */
2256  ffio_wfourcc(pb, "ctts");
2258  avio_w8(pb, 1); /* version */
2259  else
2260  avio_w8(pb, 0); /* version */
2261  avio_wb24(pb, 0); /* flags */
2262  avio_wb32(pb, entries); /* entry count */
2263  for (i = 0; i < entries; i++) {
2264  avio_wb32(pb, ctts_entries[i].count);
2265  avio_wb32(pb, ctts_entries[i].duration);
2266  }
2267  av_free(ctts_entries);
2268  return atom_size;
2269 }
2270 
2271 /* Time to sample atom */
2272 static int mov_write_stts_tag(AVIOContext *pb, MOVTrack *track)
2273 {
2274  MOVStts *stts_entries = NULL;
2275  uint32_t entries = -1;
2276  uint32_t atom_size;
2277  int i;
2278 
2279  if (track->par->codec_type == AVMEDIA_TYPE_AUDIO && !track->audio_vbr) {
2280  stts_entries = av_malloc(sizeof(*stts_entries)); /* one entry */
2281  if (!stts_entries)
2282  return AVERROR(ENOMEM);
2283  stts_entries[0].count = track->sample_count;
2284  stts_entries[0].duration = 1;
2285  entries = 1;
2286  } else {
2287  if (track->entry) {
2288  stts_entries = av_malloc_array(track->entry, sizeof(*stts_entries)); /* worst case */
2289  if (!stts_entries)
2290  return AVERROR(ENOMEM);
2291  }
2292  for (i = 0; i < track->entry; i++) {
2293  int duration = get_cluster_duration(track, i);
2294  if (i && duration == stts_entries[entries].duration) {
2295  stts_entries[entries].count++; /* compress */
2296  } else {
2297  entries++;
2298  stts_entries[entries].duration = duration;
2299  stts_entries[entries].count = 1;
2300  }
2301  }
2302  entries++; /* last one */
2303  }
2304  atom_size = 16 + (entries * 8);
2305  avio_wb32(pb, atom_size); /* size */
2306  ffio_wfourcc(pb, "stts");
2307  avio_wb32(pb, 0); /* version & flags */
2308  avio_wb32(pb, entries); /* entry count */
2309  for (i = 0; i < entries; i++) {
2310  avio_wb32(pb, stts_entries[i].count);
2311  avio_wb32(pb, stts_entries[i].duration);
2312  }
2313  av_free(stts_entries);
2314  return atom_size;
2315 }
2316 
2318 {
2319  avio_wb32(pb, 28); /* size */
2320  ffio_wfourcc(pb, "dref");
2321  avio_wb32(pb, 0); /* version & flags */
2322  avio_wb32(pb, 1); /* entry count */
2323 
2324  avio_wb32(pb, 0xc); /* size */
2325  //FIXME add the alis and rsrc atom
2326  ffio_wfourcc(pb, "url ");
2327  avio_wb32(pb, 1); /* version & flags */
2328 
2329  return 28;
2330 }
2331 
2333 {
2334  struct sgpd_entry {
2335  int count;
2336  int16_t roll_distance;
2337  int group_description_index;
2338  };
2339 
2340  struct sgpd_entry *sgpd_entries = NULL;
2341  int entries = -1;
2342  int group = 0;
2343  int i, j;
2344 
2345  const int OPUS_SEEK_PREROLL_MS = 80;
2346  int roll_samples = av_rescale_q(OPUS_SEEK_PREROLL_MS,
2347  (AVRational){1, 1000},
2348  (AVRational){1, 48000});
2349 
2350  if (!track->entry)
2351  return 0;
2352 
2353  sgpd_entries = av_malloc_array(track->entry, sizeof(*sgpd_entries));
2354  if (!sgpd_entries)
2355  return AVERROR(ENOMEM);
2356 
2358 
2359  if (track->par->codec_id == AV_CODEC_ID_OPUS) {
2360  for (i = 0; i < track->entry; i++) {
2361  int roll_samples_remaining = roll_samples;
2362  int distance = 0;
2363  for (j = i - 1; j >= 0; j--) {
2364  roll_samples_remaining -= get_cluster_duration(track, j);
2365  distance++;
2366  if (roll_samples_remaining <= 0)
2367  break;
2368  }
2369  /* We don't have enough preceeding samples to compute a valid
2370  roll_distance here, so this sample can't be independently
2371  decoded. */
2372  if (roll_samples_remaining > 0)
2373  distance = 0;
2374  /* Verify distance is a maximum of 32 (2.5ms) packets. */
2375  if (distance > 32)
2376  return AVERROR_INVALIDDATA;
2377  if (i && distance == sgpd_entries[entries].roll_distance) {
2378  sgpd_entries[entries].count++;
2379  } else {
2380  entries++;
2381  sgpd_entries[entries].count = 1;
2382  sgpd_entries[entries].roll_distance = distance;
2383  sgpd_entries[entries].group_description_index = distance ? ++group : 0;
2384  }
2385  }
2386  } else {
2387  entries++;
2388  sgpd_entries[entries].count = track->sample_count;
2389  sgpd_entries[entries].roll_distance = 1;
2390  sgpd_entries[entries].group_description_index = ++group;
2391  }
2392  entries++;
2393 
2394  if (!group) {
2395  av_free(sgpd_entries);
2396  return 0;
2397  }
2398 
2399  /* Write sgpd tag */
2400  avio_wb32(pb, 24 + (group * 2)); /* size */
2401  ffio_wfourcc(pb, "sgpd");
2402  avio_wb32(pb, 1 << 24); /* fullbox */
2403  ffio_wfourcc(pb, "roll");
2404  avio_wb32(pb, 2); /* default_length */
2405  avio_wb32(pb, group); /* entry_count */
2406  for (i = 0; i < entries; i++) {
2407  if (sgpd_entries[i].group_description_index) {
2408  avio_wb16(pb, -sgpd_entries[i].roll_distance); /* roll_distance */
2409  }
2410  }
2411 
2412  /* Write sbgp tag */
2413  avio_wb32(pb, 20 + (entries * 8)); /* size */
2414  ffio_wfourcc(pb, "sbgp");
2415  avio_wb32(pb, 0); /* fullbox */
2416  ffio_wfourcc(pb, "roll");
2417  avio_wb32(pb, entries); /* entry_count */
2418  for (i = 0; i < entries; i++) {
2419  avio_wb32(pb, sgpd_entries[i].count); /* sample_count */
2420  avio_wb32(pb, sgpd_entries[i].group_description_index); /* group_description_index */
2421  }
2422 
2423  av_free(sgpd_entries);
2424  return 0;
2425 }
2426 
2428 {
2429  int64_t pos = avio_tell(pb);
2430  int ret;
2431 
2432  avio_wb32(pb, 0); /* size */
2433  ffio_wfourcc(pb, "stbl");
2434  mov_write_stsd_tag(s, pb, mov, track);
2435  mov_write_stts_tag(pb, track);
2436  if ((track->par->codec_type == AVMEDIA_TYPE_VIDEO ||
2437  track->par->codec_tag == MKTAG('r','t','p',' ')) &&
2438  track->has_keyframes && track->has_keyframes < track->entry)
2439  mov_write_stss_tag(pb, track, MOV_SYNC_SAMPLE);
2440  if (track->par->codec_type == AVMEDIA_TYPE_VIDEO && track->has_disposable)
2441  mov_write_sdtp_tag(pb, track);
2442  if (track->mode == MODE_MOV && track->flags & MOV_TRACK_STPS)
2444  if (track->par->codec_type == AVMEDIA_TYPE_VIDEO &&
2445  track->flags & MOV_TRACK_CTTS && track->entry) {
2446 
2447  if ((ret = mov_write_ctts_tag(s, pb, track)) < 0)
2448  return ret;
2449  }
2450  mov_write_stsc_tag(pb, track);
2451  mov_write_stsz_tag(pb, track);
2452  mov_write_stco_tag(pb, track);
2453  if (track->cenc.aes_ctr) {
2454  ff_mov_cenc_write_stbl_atoms(&track->cenc, pb);
2455  }
2456  if (track->par->codec_id == AV_CODEC_ID_OPUS || track->par->codec_id == AV_CODEC_ID_AAC) {
2457  mov_preroll_write_stbl_atoms(pb, track);
2458  }
2459  return update_size(pb, pos);
2460 }
2461 
2463 {
2464  int64_t pos = avio_tell(pb);
2465  avio_wb32(pb, 0); /* size */
2466  ffio_wfourcc(pb, "dinf");
2467  mov_write_dref_tag(pb);
2468  return update_size(pb, pos);
2469 }
2470 
2472 {
2473  avio_wb32(pb, 12);
2474  ffio_wfourcc(pb, "nmhd");
2475  avio_wb32(pb, 0);
2476  return 12;
2477 }
2478 
2479 static int mov_write_tcmi_tag(AVIOContext *pb, MOVTrack *track)
2480 {
2481  int64_t pos = avio_tell(pb);
2482  const char *font = "Lucida Grande";
2483  avio_wb32(pb, 0); /* size */
2484  ffio_wfourcc(pb, "tcmi"); /* timecode media information atom */
2485  avio_wb32(pb, 0); /* version & flags */
2486  avio_wb16(pb, 0); /* text font */
2487  avio_wb16(pb, 0); /* text face */
2488  avio_wb16(pb, 12); /* text size */
2489  avio_wb16(pb, 0); /* (unknown, not in the QT specs...) */
2490  avio_wb16(pb, 0x0000); /* text color (red) */
2491  avio_wb16(pb, 0x0000); /* text color (green) */
2492  avio_wb16(pb, 0x0000); /* text color (blue) */
2493  avio_wb16(pb, 0xffff); /* background color (red) */
2494  avio_wb16(pb, 0xffff); /* background color (green) */
2495  avio_wb16(pb, 0xffff); /* background color (blue) */
2496  avio_w8(pb, strlen(font)); /* font len (part of the pascal string) */
2497  avio_write(pb, font, strlen(font)); /* font name */
2498  return update_size(pb, pos);
2499 }
2500 
2501 static int mov_write_gmhd_tag(AVIOContext *pb, MOVTrack *track)
2502 {
2503  int64_t pos = avio_tell(pb);
2504  avio_wb32(pb, 0); /* size */
2505  ffio_wfourcc(pb, "gmhd");
2506  avio_wb32(pb, 0x18); /* gmin size */
2507  ffio_wfourcc(pb, "gmin");/* generic media info */
2508  avio_wb32(pb, 0); /* version & flags */
2509  avio_wb16(pb, 0x40); /* graphics mode = */
2510  avio_wb16(pb, 0x8000); /* opColor (r?) */
2511  avio_wb16(pb, 0x8000); /* opColor (g?) */
2512  avio_wb16(pb, 0x8000); /* opColor (b?) */
2513  avio_wb16(pb, 0); /* balance */
2514  avio_wb16(pb, 0); /* reserved */
2515 
2516  /*
2517  * This special text atom is required for
2518  * Apple Quicktime chapters. The contents
2519  * don't appear to be documented, so the
2520  * bytes are copied verbatim.
2521  */
2522  if (track->tag != MKTAG('c','6','0','8')) {
2523  avio_wb32(pb, 0x2C); /* size */
2524  ffio_wfourcc(pb, "text");
2525  avio_wb16(pb, 0x01);
2526  avio_wb32(pb, 0x00);
2527  avio_wb32(pb, 0x00);
2528  avio_wb32(pb, 0x00);
2529  avio_wb32(pb, 0x01);
2530  avio_wb32(pb, 0x00);
2531  avio_wb32(pb, 0x00);
2532  avio_wb32(pb, 0x00);
2533  avio_wb32(pb, 0x00004000);
2534  avio_wb16(pb, 0x0000);
2535  }
2536 
2537  if (track->par->codec_tag == MKTAG('t','m','c','d')) {
2538  int64_t tmcd_pos = avio_tell(pb);
2539  avio_wb32(pb, 0); /* size */
2540  ffio_wfourcc(pb, "tmcd");
2541  mov_write_tcmi_tag(pb, track);
2542  update_size(pb, tmcd_pos);
2543  } else if (track->par->codec_tag == MKTAG('g','p','m','d')) {
2544  int64_t gpmd_pos = avio_tell(pb);
2545  avio_wb32(pb, 0); /* size */
2546  ffio_wfourcc(pb, "gpmd");
2547  avio_wb32(pb, 0); /* version */
2548  update_size(pb, gpmd_pos);
2549  }
2550  return update_size(pb, pos);
2551 }
2552 
2554 {
2555  avio_wb32(pb, 16); /* size */
2556  ffio_wfourcc(pb, "smhd");
2557  avio_wb32(pb, 0); /* version & flags */
2558  avio_wb16(pb, 0); /* reserved (balance, normally = 0) */
2559  avio_wb16(pb, 0); /* reserved */
2560  return 16;
2561 }
2562 
2564 {
2565  avio_wb32(pb, 0x14); /* size (always 0x14) */
2566  ffio_wfourcc(pb, "vmhd");
2567  avio_wb32(pb, 0x01); /* version & flags */
2568  avio_wb64(pb, 0); /* reserved (graphics mode = copy) */
2569  return 0x14;
2570 }
2571 
2572 static int is_clcp_track(MOVTrack *track)
2573 {
2574  return track->tag == MKTAG('c','7','0','8') ||
2575  track->tag == MKTAG('c','6','0','8');
2576 }
2577 
2579 {
2580  MOVMuxContext *mov = s->priv_data;
2581  const char *hdlr, *descr = NULL, *hdlr_type = NULL;
2582  int64_t pos = avio_tell(pb);
2583 
2584  hdlr = "dhlr";
2585  hdlr_type = "url ";
2586  descr = "DataHandler";
2587 
2588  if (track) {
2589  hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
2590  if (track->par->codec_type == AVMEDIA_TYPE_VIDEO) {
2591  hdlr_type = "vide";
2592  descr = "VideoHandler";
2593  } else if (track->par->codec_type == AVMEDIA_TYPE_AUDIO) {
2594  hdlr_type = "soun";
2595  descr = "SoundHandler";
2596  } else if (track->par->codec_type == AVMEDIA_TYPE_SUBTITLE) {
2597  if (is_clcp_track(track)) {
2598  hdlr_type = "clcp";
2599  descr = "ClosedCaptionHandler";
2600  } else {
2601  if (track->tag == MKTAG('t','x','3','g')) {
2602  hdlr_type = "sbtl";
2603  } else if (track->tag == MKTAG('m','p','4','s')) {
2604  hdlr_type = "subp";
2605  } else {
2606  hdlr_type = "text";
2607  }
2608  descr = "SubtitleHandler";
2609  }
2610  } else if (track->par->codec_tag == MKTAG('r','t','p',' ')) {
2611  hdlr_type = "hint";
2612  descr = "HintHandler";
2613  } else if (track->par->codec_tag == MKTAG('t','m','c','d')) {
2614  hdlr_type = "tmcd";
2615  descr = "TimeCodeHandler";
2616  } else if (track->par->codec_tag == MKTAG('g','p','m','d')) {
2617  hdlr_type = "meta";
2618  descr = "GoPro MET"; // GoPro Metadata
2619  } else {
2621  "Unknown hldr_type for %s, writing dummy values\n",
2622  av_fourcc2str(track->par->codec_tag));
2623  }
2624  if (track->st) {
2625  // hdlr.name is used by some players to identify the content title
2626  // of the track. So if an alternate handler description is
2627  // specified, use it.
2628  AVDictionaryEntry *t;
2629  t = av_dict_get(track->st->metadata, "handler_name", NULL, 0);
2630  if (t && utf8len(t->value))
2631  descr = t->value;
2632  }
2633  }
2634 
2635  if (mov->empty_hdlr_name) /* expressly allowed by QTFF and not prohibited in ISO 14496-12 8.4.3.3 */
2636  descr = "";
2637 
2638  avio_wb32(pb, 0); /* size */
2639  ffio_wfourcc(pb, "hdlr");
2640  avio_wb32(pb, 0); /* Version & flags */
2641  avio_write(pb, hdlr, 4); /* handler */
2642  ffio_wfourcc(pb, hdlr_type); /* handler type */
2643  avio_wb32(pb, 0); /* reserved */
2644  avio_wb32(pb, 0); /* reserved */
2645  avio_wb32(pb, 0); /* reserved */
2646  if (!track || track->mode == MODE_MOV)
2647  avio_w8(pb, strlen(descr)); /* pascal string */
2648  avio_write(pb, descr, strlen(descr)); /* handler description */
2649  if (track && track->mode != MODE_MOV)
2650  avio_w8(pb, 0); /* c string */
2651  return update_size(pb, pos);
2652 }
2653 
2655 {
2656  /* This atom must be present, but leaving the values at zero
2657  * seems harmless. */
2658  avio_wb32(pb, 28); /* size */
2659  ffio_wfourcc(pb, "hmhd");
2660  avio_wb32(pb, 0); /* version, flags */
2661  avio_wb16(pb, 0); /* maxPDUsize */
2662  avio_wb16(pb, 0); /* avgPDUsize */
2663  avio_wb32(pb, 0); /* maxbitrate */
2664  avio_wb32(pb, 0); /* avgbitrate */
2665  avio_wb32(pb, 0); /* reserved */
2666  return 28;
2667 }
2668 
2670 {
2671  int64_t pos = avio_tell(pb);
2672  int ret;
2673 
2674  avio_wb32(pb, 0); /* size */
2675  ffio_wfourcc(pb, "minf");
2676  if (track->par->codec_type == AVMEDIA_TYPE_VIDEO)
2677  mov_write_vmhd_tag(pb);
2678  else if (track->par->codec_type == AVMEDIA_TYPE_AUDIO)
2679  mov_write_smhd_tag(pb);
2680  else if (track->par->codec_type == AVMEDIA_TYPE_SUBTITLE) {
2681  if (track->tag == MKTAG('t','e','x','t') || is_clcp_track(track)) {
2682  mov_write_gmhd_tag(pb, track);
2683  } else {
2684  mov_write_nmhd_tag(pb);
2685  }
2686  } else if (track->tag == MKTAG('r','t','p',' ')) {
2687  mov_write_hmhd_tag(pb);
2688  } else if (track->tag == MKTAG('t','m','c','d')) {
2689  if (track->mode != MODE_MOV)
2690  mov_write_nmhd_tag(pb);
2691  else
2692  mov_write_gmhd_tag(pb, track);
2693  } else if (track->tag == MKTAG('g','p','m','d')) {
2694  mov_write_gmhd_tag(pb, track);
2695  }
2696  if (track->mode == MODE_MOV) /* ISO 14496-12 8.4.3.1 specifies hdlr only within mdia or meta boxes */
2697  mov_write_hdlr_tag(s, pb, NULL);
2698  mov_write_dinf_tag(pb);
2699  if ((ret = mov_write_stbl_tag(s, pb, mov, track)) < 0)
2700  return ret;
2701  return update_size(pb, pos);
2702 }
2703 
2705  MOVTrack *track)
2706 {
2707  int version = track->track_duration < INT32_MAX ? 0 : 1;
2708 
2709  if (track->mode == MODE_ISM)
2710  version = 1;
2711 
2712  (version == 1) ? avio_wb32(pb, 44) : avio_wb32(pb, 32); /* size */
2713  ffio_wfourcc(pb, "mdhd");
2714  avio_w8(pb, version);
2715  avio_wb24(pb, 0); /* flags */
2716  if (version == 1) {
2717  avio_wb64(pb, track->time);
2718  avio_wb64(pb, track->time);
2719  } else {
2720  avio_wb32(pb, track->time); /* creation time */
2721  avio_wb32(pb, track->time); /* modification time */
2722  }
2723  avio_wb32(pb, track->timescale); /* time scale (sample rate for audio) */
2724  if (!track->entry && mov->mode == MODE_ISM)
2725  (version == 1) ? avio_wb64(pb, UINT64_C(0xffffffffffffffff)) : avio_wb32(pb, 0xffffffff);
2726  else if (!track->entry)
2727  (version == 1) ? avio_wb64(pb, 0) : avio_wb32(pb, 0);
2728  else
2729  (version == 1) ? avio_wb64(pb, track->track_duration) : avio_wb32(pb, track->track_duration); /* duration */
2730  avio_wb16(pb, track->language); /* language */
2731  avio_wb16(pb, 0); /* reserved (quality) */
2732 
2733  if (version != 0 && track->mode == MODE_MOV) {
2735  "FATAL error, file duration too long for timebase, this file will not be\n"
2736  "playable with quicktime. Choose a different timebase or a different\n"
2737  "container format\n");
2738  }
2739 
2740  return 32;
2741 }
2742 
2744  MOVMuxContext *mov, MOVTrack *track)
2745 {
2746  int64_t pos = avio_tell(pb);
2747  int ret;
2748 
2749  avio_wb32(pb, 0); /* size */
2750  ffio_wfourcc(pb, "mdia");
2751  mov_write_mdhd_tag(pb, mov, track);
2752  mov_write_hdlr_tag(s, pb, track);
2753  if ((ret = mov_write_minf_tag(s, pb, mov, track)) < 0)
2754  return ret;
2755  return update_size(pb, pos);
2756 }
2757 
2758 /* transformation matrix
2759  |a b u|
2760  |c d v|
2761  |tx ty w| */
2762 static void write_matrix(AVIOContext *pb, int16_t a, int16_t b, int16_t c,
2763  int16_t d, int16_t tx, int16_t ty)
2764 {
2765  avio_wb32(pb, a << 16); /* 16.16 format */
2766  avio_wb32(pb, b << 16); /* 16.16 format */
2767  avio_wb32(pb, 0); /* u in 2.30 format */
2768  avio_wb32(pb, c << 16); /* 16.16 format */
2769  avio_wb32(pb, d << 16); /* 16.16 format */
2770  avio_wb32(pb, 0); /* v in 2.30 format */
2771  avio_wb32(pb, tx << 16); /* 16.16 format */
2772  avio_wb32(pb, ty << 16); /* 16.16 format */
2773  avio_wb32(pb, 1 << 30); /* w in 2.30 format */
2774 }
2775 
2777  MOVTrack *track, AVStream *st)
2778 {
2780  track->timescale, AV_ROUND_UP);
2781  int version = duration < INT32_MAX ? 0 : 1;
2783  int rotation = 0;
2784  int group = 0;
2785 
2786  uint32_t *display_matrix = NULL;
2787  int display_matrix_size, i;
2788 
2789  if (st) {
2790  if (mov->per_stream_grouping)
2791  group = st->index;
2792  else
2793  group = st->codecpar->codec_type;
2794 
2795  display_matrix = (uint32_t*)av_stream_get_side_data(st, AV_PKT_DATA_DISPLAYMATRIX,
2796  &display_matrix_size);
2797  if (display_matrix && display_matrix_size < 9 * sizeof(*display_matrix))
2798  display_matrix = NULL;
2799  }
2800 
2801  if (track->flags & MOV_TRACK_ENABLED)
2802  flags |= MOV_TKHD_FLAG_ENABLED;
2803 
2804  if (track->mode == MODE_ISM)
2805  version = 1;
2806 
2807  (version == 1) ? avio_wb32(pb, 104) : avio_wb32(pb, 92); /* size */
2808  ffio_wfourcc(pb, "tkhd");
2809  avio_w8(pb, version);
2810  avio_wb24(pb, flags);
2811  if (version == 1) {
2812  avio_wb64(pb, track->time);
2813  avio_wb64(pb, track->time);
2814  } else {
2815  avio_wb32(pb, track->time); /* creation time */
2816  avio_wb32(pb, track->time); /* modification time */
2817  }
2818  avio_wb32(pb, track->track_id); /* track-id */
2819  avio_wb32(pb, 0); /* reserved */
2820  if (!track->entry && mov->mode == MODE_ISM)
2821  (version == 1) ? avio_wb64(pb, UINT64_C(0xffffffffffffffff)) : avio_wb32(pb, 0xffffffff);
2822  else if (!track->entry)
2823  (version == 1) ? avio_wb64(pb, 0) : avio_wb32(pb, 0);
2824  else
2825  (version == 1) ? avio_wb64(pb, duration) : avio_wb32(pb, duration);
2826 
2827  avio_wb32(pb, 0); /* reserved */
2828  avio_wb32(pb, 0); /* reserved */
2829  avio_wb16(pb, 0); /* layer */
2830  avio_wb16(pb, group); /* alternate group) */
2831  /* Volume, only for audio */
2832  if (track->par->codec_type == AVMEDIA_TYPE_AUDIO)
2833  avio_wb16(pb, 0x0100);
2834  else
2835  avio_wb16(pb, 0);
2836  avio_wb16(pb, 0); /* reserved */
2837 
2838  /* Matrix structure */
2839 #if FF_API_OLD_ROTATE_API
2840  if (st && st->metadata) {
2841  AVDictionaryEntry *rot = av_dict_get(st->metadata, "rotate", NULL, 0);
2842  rotation = (rot && rot->value) ? atoi(rot->value) : 0;
2843  }
2844 #endif
2845  if (display_matrix) {
2846  for (i = 0; i < 9; i++)
2847  avio_wb32(pb, display_matrix[i]);
2848 #if FF_API_OLD_ROTATE_API
2849  } else if (rotation == 90) {
2850  write_matrix(pb, 0, 1, -1, 0, track->par->height, 0);
2851  } else if (rotation == 180) {
2852  write_matrix(pb, -1, 0, 0, -1, track->par->width, track->par->height);
2853  } else if (rotation == 270) {
2854  write_matrix(pb, 0, -1, 1, 0, 0, track->par->width);
2855 #endif
2856  } else {
2857  write_matrix(pb, 1, 0, 0, 1, 0, 0);
2858  }
2859  /* Track width and height, for visual only */
2860  if (st && (track->par->codec_type == AVMEDIA_TYPE_VIDEO ||
2861  track->par->codec_type == AVMEDIA_TYPE_SUBTITLE)) {
2862  int64_t track_width_1616;
2863  if (track->mode == MODE_MOV) {
2864  track_width_1616 = track->par->width * 0x10000ULL;
2865  } else {
2866  track_width_1616 = av_rescale(st->sample_aspect_ratio.num,
2867  track->par->width * 0x10000LL,
2868  st->sample_aspect_ratio.den);
2869  if (!track_width_1616 ||
2870  track->height != track->par->height ||
2871  track_width_1616 > UINT32_MAX)
2872  track_width_1616 = track->par->width * 0x10000ULL;
2873  }
2874  if (track_width_1616 > UINT32_MAX) {
2875  av_log(mov->fc, AV_LOG_WARNING, "track width is too large\n");
2876  track_width_1616 = 0;
2877  }
2878  avio_wb32(pb, track_width_1616);
2879  if (track->height > 0xFFFF) {
2880  av_log(mov->fc, AV_LOG_WARNING, "track height is too large\n");
2881  avio_wb32(pb, 0);
2882  } else
2883  avio_wb32(pb, track->height * 0x10000U);
2884  } else {
2885  avio_wb32(pb, 0);
2886  avio_wb32(pb, 0);
2887  }
2888  return 0x5c;
2889 }
2890 
2891 static int mov_write_tapt_tag(AVIOContext *pb, MOVTrack *track)
2892 {
2894  track->par->sample_aspect_ratio.den);
2895 
2896  int64_t pos = avio_tell(pb);
2897 
2898  avio_wb32(pb, 0); /* size */
2899  ffio_wfourcc(pb, "tapt");
2900 
2901  avio_wb32(pb, 20);
2902  ffio_wfourcc(pb, "clef");
2903  avio_wb32(pb, 0);
2904  avio_wb32(pb, width << 16);
2905  avio_wb32(pb, track->par->height << 16);
2906 
2907  avio_wb32(pb, 20);
2908  ffio_wfourcc(pb, "prof");
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, "enof");
2915  avio_wb32(pb, 0);
2916  avio_wb32(pb, track->par->width << 16);
2917  avio_wb32(pb, track->par->height << 16);
2918 
2919  return update_size(pb, pos);
2920 }
2921 
2922 // This box seems important for the psp playback ... without it the movie seems to hang
2924  MOVTrack *track)
2925 {
2927  track->timescale, AV_ROUND_UP);
2928  int version = duration < INT32_MAX ? 0 : 1;
2929  int entry_size, entry_count, size;
2930  int64_t delay, start_ct = track->start_cts;
2931  int64_t start_dts = track->start_dts;
2932 
2933  if (track->entry) {
2934  if (start_dts != track->cluster[0].dts || start_ct != track->cluster[0].cts) {
2935 
2936  av_log(mov->fc, AV_LOG_DEBUG,
2937  "EDTS using dts:%"PRId64" cts:%d instead of dts:%"PRId64" cts:%"PRId64" tid:%d\n",
2938  track->cluster[0].dts, track->cluster[0].cts,
2939  start_dts, start_ct, track->track_id);
2940  start_dts = track->cluster[0].dts;
2941  start_ct = track->cluster[0].cts;
2942  }
2943  }
2944 
2945  delay = av_rescale_rnd(start_dts + start_ct, MOV_TIMESCALE,
2946  track->timescale, AV_ROUND_DOWN);
2947  version |= delay < INT32_MAX ? 0 : 1;
2948 
2949  entry_size = (version == 1) ? 20 : 12;
2950  entry_count = 1 + (delay > 0);
2951  size = 24 + entry_count * entry_size;
2952 
2953  /* write the atom data */
2954  avio_wb32(pb, size);
2955  ffio_wfourcc(pb, "edts");
2956  avio_wb32(pb, size - 8);
2957  ffio_wfourcc(pb, "elst");
2958  avio_w8(pb, version);
2959  avio_wb24(pb, 0); /* flags */
2960 
2961  avio_wb32(pb, entry_count);
2962  if (delay > 0) { /* add an empty edit to delay presentation */
2963  /* In the positive delay case, the delay includes the cts
2964  * offset, and the second edit list entry below trims out
2965  * the same amount from the actual content. This makes sure
2966  * that the offset last sample is included in the edit
2967  * list duration as well. */
2968  if (version == 1) {
2969  avio_wb64(pb, delay);
2970  avio_wb64(pb, -1);
2971  } else {
2972  avio_wb32(pb, delay);
2973  avio_wb32(pb, -1);
2974  }
2975  avio_wb32(pb, 0x00010000);
2976  } else {
2977  /* Avoid accidentally ending up with start_ct = -1 which has got a
2978  * special meaning. Normally start_ct should end up positive or zero
2979  * here, but use FFMIN in case dts is a small positive integer
2980  * rounded to 0 when represented in MOV_TIMESCALE units. */
2981  av_assert0(av_rescale_rnd(start_dts, MOV_TIMESCALE, track->timescale, AV_ROUND_DOWN) <= 0);
2982  start_ct = -FFMIN(start_dts, 0);
2983  /* Note, this delay is calculated from the pts of the first sample,
2984  * ensuring that we don't reduce the duration for cases with
2985  * dts<0 pts=0. */
2986  duration += delay;
2987  }
2988 
2989  /* For fragmented files, we don't know the full length yet. Setting
2990  * duration to 0 allows us to only specify the offset, including
2991  * the rest of the content (from all future fragments) without specifying
2992  * an explicit duration. */
2993  if (mov->flags & FF_MOV_FLAG_FRAGMENT)
2994  duration = 0;
2995 
2996  /* duration */
2997  if (version == 1) {
2998  avio_wb64(pb, duration);
2999  avio_wb64(pb, start_ct);
3000  } else {
3001  avio_wb32(pb, duration);
3002  avio_wb32(pb, start_ct);
3003  }
3004  avio_wb32(pb, 0x00010000);
3005  return size;
3006 }
3007 
3008 static int mov_write_tref_tag(AVIOContext *pb, MOVTrack *track)
3009 {
3010  avio_wb32(pb, 20); // size
3011  ffio_wfourcc(pb, "tref");
3012  avio_wb32(pb, 12); // size (subatom)
3013  avio_wl32(pb, track->tref_tag);
3014  avio_wb32(pb, track->tref_id);
3015  return 20;
3016 }
3017 
3018 // goes at the end of each track! ... Critical for PSP playback ("Incompatible data" without it)
3020 {
3021  avio_wb32(pb, 0x34); /* size ... reports as 28 in mp4box! */
3022  ffio_wfourcc(pb, "uuid");
3023  ffio_wfourcc(pb, "USMT");
3024  avio_wb32(pb, 0x21d24fce);
3025  avio_wb32(pb, 0xbb88695c);
3026  avio_wb32(pb, 0xfac9c740);
3027  avio_wb32(pb, 0x1c); // another size here!
3028  ffio_wfourcc(pb, "MTDT");
3029  avio_wb32(pb, 0x00010012);
3030  avio_wb32(pb, 0x0a);
3031  avio_wb32(pb, 0x55c40000);
3032  avio_wb32(pb, 0x1);
3033  avio_wb32(pb, 0x0);
3034  return 0x34;
3035 }
3036 
3037 static int mov_write_udta_sdp(AVIOContext *pb, MOVTrack *track)
3038 {
3039  AVFormatContext *ctx = track->rtp_ctx;
3040  char buf[1000] = "";
3041  int len;
3042 
3043  ff_sdp_write_media(buf, sizeof(buf), ctx->streams[0], track->src_track,
3044  NULL, NULL, 0, 0, ctx);
3045  av_strlcatf(buf, sizeof(buf), "a=control:streamid=%d\r\n", track->track_id);
3046  len = strlen(buf);
3047 
3048  avio_wb32(pb, len + 24);
3049  ffio_wfourcc(pb, "udta");
3050  avio_wb32(pb, len + 16);
3051  ffio_wfourcc(pb, "hnti");
3052  avio_wb32(pb, len + 8);
3053  ffio_wfourcc(pb, "sdp ");
3054  avio_write(pb, buf, len);
3055  return len + 24;
3056 }
3057 
3059  const char *tag, const char *str)
3060 {
3061  int64_t pos = avio_tell(pb);
3062  AVDictionaryEntry *t = av_dict_get(st->metadata, str, NULL, 0);
3063  if (!t || !utf8len(t->value))
3064  return 0;
3065 
3066  avio_wb32(pb, 0); /* size */
3067  ffio_wfourcc(pb, tag); /* type */
3068  avio_write(pb, t->value, strlen(t->value)); /* UTF8 string value */
3069  return update_size(pb, pos);
3070 }
3071 
3073  AVStream *st)
3074 {
3075  AVIOContext *pb_buf;
3076  int ret, size;
3077  uint8_t *buf;
3078 
3079  if (!st)
3080  return 0;
3081 
3082  ret = avio_open_dyn_buf(&pb_buf);
3083  if (ret < 0)
3084  return ret;
3085 
3086  if (mov->mode & (MODE_MP4|MODE_MOV))
3087  mov_write_track_metadata(pb_buf, st, "name", "title");
3088 
3089  if ((size = avio_close_dyn_buf(pb_buf, &buf)) > 0) {
3090  avio_wb32(pb, size + 8);
3091  ffio_wfourcc(pb, "udta");
3092  avio_write(pb, buf, size);
3093  }
3094  av_free(buf);
3095 
3096  return 0;
3097 }
3098 
3100  MOVTrack *track, AVStream *st)
3101 {
3102  int64_t pos = avio_tell(pb);
3103  int entry_backup = track->entry;
3104  int chunk_backup = track->chunkCount;
3105  int ret;
3106 
3107  /* If we want to have an empty moov, but some samples already have been
3108  * buffered (delay_moov), pretend that no samples have been written yet. */
3109  if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV)
3110  track->chunkCount = track->entry = 0;
3111 
3112  avio_wb32(pb, 0); /* size */
3113  ffio_wfourcc(pb, "trak");
3114  mov_write_tkhd_tag(pb, mov, track, st);
3115 
3116  av_assert2(mov->use_editlist >= 0);
3117 
3118  if (track->start_dts != AV_NOPTS_VALUE) {
3119  if (mov->use_editlist)
3120  mov_write_edts_tag(pb, mov, track); // PSP Movies and several other cases require edts box
3121  else if ((track->entry && track->cluster[0].dts) || track->mode == MODE_PSP || is_clcp_track(track))
3122  av_log(mov->fc, AV_LOG_WARNING,
3123  "Not writing any edit list even though one would have been required\n");
3124  }
3125 
3126  if (track->tref_tag)
3127  mov_write_tref_tag(pb, track);
3128 
3129  if ((ret = mov_write_mdia_tag(s, pb, mov, track)) < 0)
3130  return ret;
3131  if (track->mode == MODE_PSP)
3132  mov_write_uuid_tag_psp(pb, track); // PSP Movies require this uuid box
3133  if (track->tag == MKTAG('r','t','p',' '))
3134  mov_write_udta_sdp(pb, track);
3135  if (track->mode == MODE_MOV) {
3136  if (track->par->codec_type == AVMEDIA_TYPE_VIDEO) {
3137  double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
3138  if (st->sample_aspect_ratio.num && 1.0 != sample_aspect_ratio) {
3139  mov_write_tapt_tag(pb, track);
3140  }
3141  }
3142  if (is_clcp_track(track) && st->sample_aspect_ratio.num) {
3143  mov_write_tapt_tag(pb, track);
3144  }
3145  }
3146  mov_write_track_udta_tag(pb, mov, st);
3147  track->entry = entry_backup;
3148  track->chunkCount = chunk_backup;
3149  return update_size(pb, pos);
3150 }
3151 
3153 {
3154  int i, has_audio = 0, has_video = 0;
3155  int64_t pos = avio_tell(pb);
3156  int audio_profile = mov->iods_audio_profile;
3157  int video_profile = mov->iods_video_profile;
3158  for (i = 0; i < mov->nb_streams; i++) {
3159  if (mov->tracks[i].entry > 0 || mov->flags & FF_MOV_FLAG_EMPTY_MOOV) {
3160  has_audio |= mov->tracks[i].par->codec_type == AVMEDIA_TYPE_AUDIO;
3161  has_video |= mov->tracks[i].par->codec_type == AVMEDIA_TYPE_VIDEO;
3162  }
3163  }
3164  if (audio_profile < 0)
3165  audio_profile = 0xFF - has_audio;
3166  if (video_profile < 0)
3167  video_profile = 0xFF - has_video;
3168  avio_wb32(pb, 0x0); /* size */
3169  ffio_wfourcc(pb, "iods");
3170  avio_wb32(pb, 0); /* version & flags */
3171  put_descr(pb, 0x10, 7);
3172  avio_wb16(pb, 0x004f);
3173  avio_w8(pb, 0xff);
3174  avio_w8(pb, 0xff);
3175  avio_w8(pb, audio_profile);
3176  avio_w8(pb, video_profile);
3177  avio_w8(pb, 0xff);
3178  return update_size(pb, pos);
3179 }
3180 
3181 static int mov_write_trex_tag(AVIOContext *pb, MOVTrack *track)
3182 {
3183  avio_wb32(pb, 0x20); /* size */
3184  ffio_wfourcc(pb, "trex");
3185  avio_wb32(pb, 0); /* version & flags */
3186  avio_wb32(pb, track->track_id); /* track ID */
3187  avio_wb32(pb, 1); /* default sample description index */
3188  avio_wb32(pb, 0); /* default sample duration */
3189  avio_wb32(pb, 0); /* default sample size */
3190  avio_wb32(pb, 0); /* default sample flags */
3191  return 0;
3192 }
3193 
3195 {
3196  int64_t pos = avio_tell(pb);
3197  int i;
3198  avio_wb32(pb, 0x0); /* size */
3199  ffio_wfourcc(pb, "mvex");
3200  for (i = 0; i < mov->nb_streams; i++)
3201  mov_write_trex_tag(pb, &mov->tracks[i]);
3202  return update_size(pb, pos);
3203 }
3204 
3206 {
3207  int max_track_id = 1, i;
3208  int64_t max_track_len = 0;
3209  int version;
3210 
3211  for (i = 0; i < mov->nb_streams; i++) {
3212  if (mov->tracks[i].entry > 0 && mov->tracks[i].timescale) {
3213  int64_t max_track_len_temp = av_rescale_rnd(mov->tracks[i].track_duration,
3214  MOV_TIMESCALE,
3215  mov->tracks[i].timescale,
3216  AV_ROUND_UP);
3217  if (max_track_len < max_track_len_temp)
3218  max_track_len = max_track_len_temp;
3219  if (max_track_id < mov->tracks[i].track_id)
3220  max_track_id = mov->tracks[i].track_id;
3221  }
3222  }
3223  /* If using delay_moov, make sure the output is the same as if no
3224  * samples had been written yet. */
3225  if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV) {
3226  max_track_len = 0;
3227  max_track_id = 1;
3228  }
3229 
3230  version = max_track_len < UINT32_MAX ? 0 : 1;
3231  avio_wb32(pb, version == 1 ? 120 : 108); /* size */
3232 
3233  ffio_wfourcc(pb, "mvhd");
3234  avio_w8(pb, version);
3235  avio_wb24(pb, 0); /* flags */
3236  if (version == 1) {
3237  avio_wb64(pb, mov->time);
3238  avio_wb64(pb, mov->time);
3239  } else {
3240  avio_wb32(pb, mov->time); /* creation time */
3241  avio_wb32(pb, mov->time); /* modification time */
3242  }
3243  avio_wb32(pb, MOV_TIMESCALE);
3244  (version == 1) ? avio_wb64(pb, max_track_len) : avio_wb32(pb, max_track_len); /* duration of longest track */
3245 
3246  avio_wb32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
3247  avio_wb16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
3248  avio_wb16(pb, 0); /* reserved */
3249  avio_wb32(pb, 0); /* reserved */
3250  avio_wb32(pb, 0); /* reserved */
3251 
3252  /* Matrix structure */
3253  write_matrix(pb, 1, 0, 0, 1, 0, 0);
3254 
3255  avio_wb32(pb, 0); /* reserved (preview time) */
3256  avio_wb32(pb, 0); /* reserved (preview duration) */
3257  avio_wb32(pb, 0); /* reserved (poster time) */
3258  avio_wb32(pb, 0); /* reserved (selection time) */
3259  avio_wb32(pb, 0); /* reserved (selection duration) */
3260  avio_wb32(pb, 0); /* reserved (current time) */
3261  avio_wb32(pb, max_track_id + 1); /* Next track id */
3262  return 0x6c;
3263 }
3264 
3266  AVFormatContext *s)
3267 {
3268  avio_wb32(pb, 33); /* size */
3269  ffio_wfourcc(pb, "hdlr");
3270  avio_wb32(pb, 0);
3271  avio_wb32(pb, 0);
3272  ffio_wfourcc(pb, "mdir");
3273  ffio_wfourcc(pb, "appl");
3274  avio_wb32(pb, 0);
3275  avio_wb32(pb, 0);
3276  avio_w8(pb, 0);
3277  return 33;
3278 }
3279 
3280 /* helper function to write a data tag with the specified string as data */
3281 static int mov_write_string_data_tag(AVIOContext *pb, const char *data, int lang, int long_style)
3282 {
3283  if (long_style) {
3284  int size = 16 + strlen(data);
3285  avio_wb32(pb, size); /* size */
3286  ffio_wfourcc(pb, "data");
3287  avio_wb32(pb, 1);
3288  avio_wb32(pb, 0);
3289  avio_write(pb, data, strlen(data));
3290  return size;
3291  } else {
3292  if (!lang)
3293  lang = ff_mov_iso639_to_lang("und", 1);
3294  avio_wb16(pb, strlen(data)); /* string length */
3295  avio_wb16(pb, lang);
3296  avio_write(pb, data, strlen(data));
3297  return strlen(data) + 4;
3298  }
3299 }
3300 
3301 static int mov_write_string_tag(AVIOContext *pb, const char *name,
3302  const char *value, int lang, int long_style)
3303 {
3304  int size = 0;
3305  if (value && value[0]) {
3306  int64_t pos = avio_tell(pb);
3307  avio_wb32(pb, 0); /* size */
3308  ffio_wfourcc(pb, name);
3309  mov_write_string_data_tag(pb, value, lang, long_style);
3310  size = update_size(pb, pos);
3311  }
3312  return size;
3313 }
3314 
3316  const char *tag, int *lang)
3317 {
3318  int l, len, len2;
3319  AVDictionaryEntry *t, *t2 = NULL;
3320  char tag2[16];
3321 
3322  *lang = 0;
3323 
3324  if (!(t = av_dict_get(s->metadata, tag, NULL, 0)))
3325  return NULL;
3326 
3327  len = strlen(t->key);
3328  snprintf(tag2, sizeof(tag2), "%s-", tag);
3329  while ((t2 = av_dict_get(s->metadata, tag2, t2, AV_DICT_IGNORE_SUFFIX))) {
3330  len2 = strlen(t2->key);
3331  if (len2 == len + 4 && !strcmp(t->value, t2->value)
3332  && (l = ff_mov_iso639_to_lang(&t2->key[len2 - 3], 1)) >= 0) {
3333  *lang = l;
3334  return t;
3335  }
3336  }
3337  return t;
3338 }
3339 
3341  const char *name, const char *tag,
3342  int long_style)
3343 {
3344  int lang;
3345  AVDictionaryEntry *t = get_metadata_lang(s, tag, &lang);
3346  if (!t)
3347  return 0;
3348  return mov_write_string_tag(pb, name, t->value, lang, long_style);
3349 }
3350 
3351 /* iTunes bpm number */
3353 {
3354  AVDictionaryEntry *t = av_dict_get(s->metadata, "tmpo", NULL, 0);
3355  int size = 0, tmpo = t ? atoi(t->value) : 0;
3356  if (tmpo) {
3357  size = 26;
3358  avio_wb32(pb, size);
3359  ffio_wfourcc(pb, "tmpo");
3360  avio_wb32(pb, size-8); /* size */
3361  ffio_wfourcc(pb, "data");
3362  avio_wb32(pb, 0x15); //type specifier
3363  avio_wb32(pb, 0);
3364  avio_wb16(pb, tmpo); // data
3365  }
3366  return size;
3367 }
3368 
3369 /* 3GPP TS 26.244 */
3371 {
3372  int lang;
3373  int64_t pos = avio_tell(pb);
3374  double latitude, longitude, altitude;
3375  int32_t latitude_fix, longitude_fix, altitude_fix;
3376  AVDictionaryEntry *t = get_metadata_lang(s, "location", &lang);
3377  const char *ptr, *place = "";
3378  char *end;
3379  static const char *astronomical_body = "earth";
3380  if (!t)
3381  return 0;
3382 
3383  ptr = t->value;
3384  longitude = strtod(ptr, &end);
3385  if (end == ptr) {
3386  av_log(s, AV_LOG_WARNING, "malformed location metadata\n");
3387  return 0;
3388  }
3389  ptr = end;
3390  latitude = 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  altitude = strtod(ptr, &end);
3397  /* If no altitude was present, the default 0 should be fine */
3398  if (*end == '/')
3399  place = end + 1;
3400 
3401  latitude_fix = (int32_t) ((1 << 16) * latitude);
3402  longitude_fix = (int32_t) ((1 << 16) * longitude);
3403  altitude_fix = (int32_t) ((1 << 16) * altitude);
3404 
3405  avio_wb32(pb, 0); /* size */
3406  ffio_wfourcc(pb, "loci"); /* type */
3407  avio_wb32(pb, 0); /* version + flags */
3408  avio_wb16(pb, lang);
3409  avio_write(pb, place, strlen(place) + 1);
3410  avio_w8(pb, 0); /* role of place (0 == shooting location, 1 == real location, 2 == fictional location) */
3411  avio_wb32(pb, latitude_fix);
3412  avio_wb32(pb, longitude_fix);
3413  avio_wb32(pb, altitude_fix);
3414  avio_write(pb, astronomical_body, strlen(astronomical_body) + 1);
3415  avio_w8(pb, 0); /* additional notes, null terminated string */
3416 
3417  return update_size(pb, pos);
3418 }
3419 
3420 /* iTunes track or disc number */
3422  AVFormatContext *s, int disc)
3423 {
3425  disc ? "disc" : "track",
3426  NULL, 0);
3427  int size = 0, track = t ? atoi(t->value) : 0;
3428  if (track) {
3429  int tracks = 0;
3430  char *slash = strchr(t->value, '/');
3431  if (slash)
3432  tracks = atoi(slash + 1);
3433  avio_wb32(pb, 32); /* size */
3434  ffio_wfourcc(pb, disc ? "disk" : "trkn");
3435  avio_wb32(pb, 24); /* size */
3436  ffio_wfourcc(pb, "data");
3437  avio_wb32(pb, 0); // 8 bytes empty
3438  avio_wb32(pb, 0);
3439  avio_wb16(pb, 0); // empty
3440  avio_wb16(pb, track); // track / disc number
3441  avio_wb16(pb, tracks); // total track / disc number
3442  avio_wb16(pb, 0); // empty
3443  size = 32;
3444  }
3445  return size;
3446 }
3447 
3449  const char *name, const char *tag,
3450  int len)
3451 {
3452  AVDictionaryEntry *t = NULL;
3453  uint8_t num;
3454  int size = 24 + len;
3455 
3456  if (len != 1 && len != 4)
3457  return -1;
3458 
3459  if (!(t = av_dict_get(s->metadata, tag, NULL, 0)))
3460  return 0;
3461  num = atoi(t->value);
3462 
3463  avio_wb32(pb, size);
3464  ffio_wfourcc(pb, name);
3465  avio_wb32(pb, size - 8);
3466  ffio_wfourcc(pb, "data");
3467  avio_wb32(pb, 0x15);
3468  avio_wb32(pb, 0);
3469  if (len==4) avio_wb32(pb, num);
3470  else avio_w8 (pb, num);
3471 
3472  return size;
3473 }
3474 
3476 {
3477  MOVMuxContext *mov = s->priv_data;
3478  int64_t pos = 0;
3479  int i;
3480 
3481  for (i = 0; i < s->nb_streams; i++) {
3482  MOVTrack *trk = &mov->tracks[i];
3483 
3484  if (!is_cover_image(trk->st) || trk->cover_image.size <= 0)
3485  continue;
3486 
3487  if (!pos) {
3488  pos = avio_tell(pb);
3489  avio_wb32(pb, 0);
3490  ffio_wfourcc(pb, "covr");
3491  }
3492  avio_wb32(pb, 16 + trk->cover_image.size);
3493  ffio_wfourcc(pb, "data");
3494  avio_wb32(pb, trk->tag);
3495  avio_wb32(pb , 0);
3496  avio_write(pb, trk->cover_image.data, trk->cover_image.size);
3497  }
3498 
3499  return pos ? update_size(pb, pos) : 0;
3500 }
3501 
3502 /* iTunes meta data list */
3504  AVFormatContext *s)
3505 {
3506  int64_t pos = avio_tell(pb);
3507  avio_wb32(pb, 0); /* size */
3508  ffio_wfourcc(pb, "ilst");
3509  mov_write_string_metadata(s, pb, "\251nam", "title" , 1);
3510  mov_write_string_metadata(s, pb, "\251ART", "artist" , 1);
3511  mov_write_string_metadata(s, pb, "aART", "album_artist", 1);
3512  mov_write_string_metadata(s, pb, "\251wrt", "composer" , 1);
3513  mov_write_string_metadata(s, pb, "\251alb", "album" , 1);
3514  mov_write_string_metadata(s, pb, "\251day", "date" , 1);
3515  if (!mov_write_string_metadata(s, pb, "\251too", "encoding_tool", 1)) {
3516  if (!(s->flags & AVFMT_FLAG_BITEXACT))
3517  mov_write_string_tag(pb, "\251too", LIBAVFORMAT_IDENT, 0, 1);
3518  }
3519  mov_write_string_metadata(s, pb, "\251cmt", "comment" , 1);
3520  mov_write_string_metadata(s, pb, "\251gen", "genre" , 1);
3521  mov_write_string_metadata(s, pb, "cprt", "copyright", 1);
3522  mov_write_string_metadata(s, pb, "\251grp", "grouping" , 1);
3523  mov_write_string_metadata(s, pb, "\251lyr", "lyrics" , 1);
3524  mov_write_string_metadata(s, pb, "desc", "description",1);
3525  mov_write_string_metadata(s, pb, "ldes", "synopsis" , 1);
3526  mov_write_string_metadata(s, pb, "tvsh", "show" , 1);
3527  mov_write_string_metadata(s, pb, "tven", "episode_id",1);
3528  mov_write_string_metadata(s, pb, "tvnn", "network" , 1);
3529  mov_write_string_metadata(s, pb, "keyw", "keywords" , 1);
3530  mov_write_int8_metadata (s, pb, "tves", "episode_sort",4);
3531  mov_write_int8_metadata (s, pb, "tvsn", "season_number",4);
3532  mov_write_int8_metadata (s, pb, "stik", "media_type",1);
3533  mov_write_int8_metadata (s, pb, "hdvd", "hd_video", 1);
3534  mov_write_int8_metadata (s, pb, "pgap", "gapless_playback",1);
3535  mov_write_int8_metadata (s, pb, "cpil", "compilation", 1);
3536  mov_write_covr(pb, s);
3537  mov_write_trkn_tag(pb, mov, s, 0); // track number
3538  mov_write_trkn_tag(pb, mov, s, 1); // disc number
3539  mov_write_tmpo_tag(pb, s);
3540  return update_size(pb, pos);
3541 }
3542 
3544  AVFormatContext *s)
3545 {
3546  avio_wb32(pb, 33); /* size */
3547  ffio_wfourcc(pb, "hdlr");
3548  avio_wb32(pb, 0);
3549  avio_wb32(pb, 0);
3550  ffio_wfourcc(pb, "mdta");
3551  avio_wb32(pb, 0);
3552  avio_wb32(pb, 0);
3553  avio_wb32(pb, 0);
3554  avio_w8(pb, 0);
3555  return 33;
3556 }
3557 
3559  AVFormatContext *s)
3560 {
3561  AVDictionaryEntry *t = NULL;
3562  int64_t pos = avio_tell(pb);
3563  int64_t curpos, entry_pos;
3564  int count = 0;
3565 
3566  avio_wb32(pb, 0); /* size */
3567  ffio_wfourcc(pb, "keys");
3568  avio_wb32(pb, 0);
3569  entry_pos = avio_tell(pb);
3570  avio_wb32(pb, 0); /* entry count */
3571 
3572  while (t = av_dict_get(s->metadata, "", t, AV_DICT_IGNORE_SUFFIX)) {
3573  avio_wb32(pb, strlen(t->key) + 8);
3574  ffio_wfourcc(pb, "mdta");
3575  avio_write(pb, t->key, strlen(t->key));
3576  count += 1;
3577  }
3578  curpos = avio_tell(pb);
3579  avio_seek(pb, entry_pos, SEEK_SET);
3580  avio_wb32(pb, count); // rewrite entry count
3581  avio_seek(pb, curpos, SEEK_SET);
3582 
3583  return update_size(pb, pos);
3584 }
3585 
3587  AVFormatContext *s)
3588 {
3589  AVDictionaryEntry *t = NULL;
3590  int64_t pos = avio_tell(pb);
3591  int count = 1; /* keys are 1-index based */
3592 
3593  avio_wb32(pb, 0); /* size */
3594  ffio_wfourcc(pb, "ilst");
3595 
3596  while (t = av_dict_get(s->metadata, "", t, AV_DICT_IGNORE_SUFFIX)) {
3597  int64_t entry_pos = avio_tell(pb);
3598  avio_wb32(pb, 0); /* size */
3599  avio_wb32(pb, count); /* key */
3600  mov_write_string_data_tag(pb, t->value, 0, 1);
3601  update_size(pb, entry_pos);
3602  count += 1;
3603  }
3604  return update_size(pb, pos);
3605 }
3606 
3607 /* meta data tags */
3609  AVFormatContext *s)
3610 {
3611  int size = 0;
3612  int64_t pos = avio_tell(pb);
3613  avio_wb32(pb, 0); /* size */
3614  ffio_wfourcc(pb, "meta");
3615  avio_wb32(pb, 0);
3616  if (mov->flags & FF_MOV_FLAG_USE_MDTA) {
3617  mov_write_mdta_hdlr_tag(pb, mov, s);
3618  mov_write_mdta_keys_tag(pb, mov, s);
3619  mov_write_mdta_ilst_tag(pb, mov, s);
3620  }
3621  else {
3622  /* iTunes metadata tag */
3623  mov_write_itunes_hdlr_tag(pb, mov, s);
3624  mov_write_ilst_tag(pb, mov, s);
3625  }
3626  size = update_size(pb, pos);
3627  return size;
3628 }
3629 
3631  const char *name, const char *key)
3632 {
3633  int len;
3634  AVDictionaryEntry *t;
3635 
3636  if (!(t = av_dict_get(s->metadata, key, NULL, 0)))
3637  return 0;
3638 
3639  len = strlen(t->value);
3640  if (len > 0) {
3641  int size = len + 8;
3642  avio_wb32(pb, size);
3643  ffio_wfourcc(pb, name);
3644  avio_write(pb, t->value, len);
3645  return size;
3646  }
3647  return 0;
3648 }
3649 
3650 static int ascii_to_wc(AVIOContext *pb, const uint8_t *b)
3651 {
3652  int val;
3653  while (*b) {
3654  GET_UTF8(val, *b++, return -1;)
3655  avio_wb16(pb, val);
3656  }
3657  avio_wb16(pb, 0x00);
3658  return 0;
3659 }
3660 
3661 static uint16_t language_code(const char *str)
3662 {
3663  return (((str[0] - 0x60) & 0x1F) << 10) +
3664  (((str[1] - 0x60) & 0x1F) << 5) +
3665  (( str[2] - 0x60) & 0x1F);
3666 }
3667 
3669  const char *tag, const char *str)
3670 {
3671  int64_t pos = avio_tell(pb);
3672  AVDictionaryEntry *t = av_dict_get(s->metadata, str, NULL, 0);
3673  if (!t || !utf8len(t->value))
3674  return 0;
3675  avio_wb32(pb, 0); /* size */
3676  ffio_wfourcc(pb, tag); /* type */
3677  avio_wb32(pb, 0); /* version + flags */
3678  if (!strcmp(tag, "yrrc"))
3679  avio_wb16(pb, atoi(t->value));
3680  else {
3681  avio_wb16(pb, language_code("eng")); /* language */
3682  avio_write(pb, t->value, strlen(t->value) + 1); /* UTF8 string value */
3683  if (!strcmp(tag, "albm") &&
3684  (t = av_dict_get(s->metadata, "track", NULL, 0)))
3685  avio_w8(pb, atoi(t->value));
3686  }
3687  return update_size(pb, pos);
3688 }
3689 
3691 {
3692  int64_t pos = avio_tell(pb);
3693  int i, nb_chapters = FFMIN(s->nb_chapters, 255);
3694 
3695  avio_wb32(pb, 0); // size
3696  ffio_wfourcc(pb, "chpl");
3697  avio_wb32(pb, 0x01000000); // version + flags
3698  avio_wb32(pb, 0); // unknown
3699  avio_w8(pb, nb_chapters);
3700 
3701  for (i = 0; i < nb_chapters; i++) {
3702  AVChapter *c = s->chapters[i];
3703  AVDictionaryEntry *t;
3704  avio_wb64(pb, av_rescale_q(c->start, c->time_base, (AVRational){1,10000000}));
3705 
3706  if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
3707  int len = FFMIN(strlen(t->value), 255);
3708  avio_w8(pb, len);
3709  avio_write(pb, t->value, len);
3710  } else
3711  avio_w8(pb, 0);
3712  }
3713  return update_size(pb, pos);
3714 }
3715 
3717  AVFormatContext *s)
3718 {
3719  AVIOContext *pb_buf;
3720  int ret, size;
3721  uint8_t *buf;
3722 
3723  ret = avio_open_dyn_buf(&pb_buf);
3724  if (ret < 0)
3725  return ret;
3726 
3727  if (mov->mode & MODE_3GP) {
3728  mov_write_3gp_udta_tag(pb_buf, s, "perf", "artist");
3729  mov_write_3gp_udta_tag(pb_buf, s, "titl", "title");
3730  mov_write_3gp_udta_tag(pb_buf, s, "auth", "author");
3731  mov_write_3gp_udta_tag(pb_buf, s, "gnre", "genre");
3732  mov_write_3gp_udta_tag(pb_buf, s, "dscp", "comment");
3733  mov_write_3gp_udta_tag(pb_buf, s, "albm", "album");
3734  mov_write_3gp_udta_tag(pb_buf, s, "cprt", "copyright");
3735  mov_write_3gp_udta_tag(pb_buf, s, "yrrc", "date");
3736  mov_write_loci_tag(s, pb_buf);
3737  } 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
3738  mov_write_string_metadata(s, pb_buf, "\251ART", "artist", 0);
3739  mov_write_string_metadata(s, pb_buf, "\251nam", "title", 0);
3740  mov_write_string_metadata(s, pb_buf, "\251aut", "author", 0);
3741  mov_write_string_metadata(s, pb_buf, "\251alb", "album", 0);
3742  mov_write_string_metadata(s, pb_buf, "\251day", "date", 0);
3743  mov_write_string_metadata(s, pb_buf, "\251swr", "encoder", 0);
3744  // currently ignored by mov.c
3745  mov_write_string_metadata(s, pb_buf, "\251des", "comment", 0);
3746  // add support for libquicktime, this atom is also actually read by mov.c
3747  mov_write_string_metadata(s, pb_buf, "\251cmt", "comment", 0);
3748  mov_write_string_metadata(s, pb_buf, "\251gen", "genre", 0);
3749  mov_write_string_metadata(s, pb_buf, "\251cpy", "copyright", 0);
3750  mov_write_string_metadata(s, pb_buf, "\251mak", "make", 0);
3751  mov_write_string_metadata(s, pb_buf, "\251mod", "model", 0);
3752  mov_write_string_metadata(s, pb_buf, "\251xyz", "location", 0);
3753  mov_write_string_metadata(s, pb_buf, "\251key", "keywords", 0);
3754  mov_write_raw_metadata_tag(s, pb_buf, "XMP_", "xmp");
3755  } else {
3756  /* iTunes meta data */
3757  mov_write_meta_tag(pb_buf, mov, s);
3758  mov_write_loci_tag(s, pb_buf);
3759  }
3760 
3761  if (s->nb_chapters && !(mov->flags & FF_MOV_FLAG_DISABLE_CHPL))
3762  mov_write_chpl_tag(pb_buf, s);
3763 
3764  if ((size = avio_close_dyn_buf(pb_buf, &buf)) > 0) {
3765  avio_wb32(pb, size + 8);
3766  ffio_wfourcc(pb, "udta");
3767  avio_write(pb, buf, size);
3768  }
3769  av_free(buf);
3770 
3771  return 0;
3772 }
3773 
3775  const char *str, const char *lang, int type)
3776 {
3777  int len = utf8len(str) + 1;
3778  if (len <= 0)
3779  return;
3780  avio_wb16(pb, len * 2 + 10); /* size */
3781  avio_wb32(pb, type); /* type */
3782  avio_wb16(pb, language_code(lang)); /* language */
3783  avio_wb16(pb, 0x01); /* ? */
3784  ascii_to_wc(pb, str);
3785 }
3786 
3788 {
3789  AVDictionaryEntry *title = av_dict_get(s->metadata, "title", NULL, 0);
3790  int64_t pos, pos2;
3791 
3792  if (title) {
3793  pos = avio_tell(pb);
3794  avio_wb32(pb, 0); /* size placeholder*/
3795  ffio_wfourcc(pb, "uuid");
3796  ffio_wfourcc(pb, "USMT");
3797  avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
3798  avio_wb32(pb, 0xbb88695c);
3799  avio_wb32(pb, 0xfac9c740);
3800 
3801  pos2 = avio_tell(pb);
3802  avio_wb32(pb, 0); /* size placeholder*/
3803  ffio_wfourcc(pb, "MTDT");
3804  avio_wb16(pb, 4);
3805 
3806  // ?
3807  avio_wb16(pb, 0x0C); /* size */
3808  avio_wb32(pb, 0x0B); /* type */
3809  avio_wb16(pb, language_code("und")); /* language */
3810  avio_wb16(pb, 0x0); /* ? */
3811  avio_wb16(pb, 0x021C); /* data */
3812 
3813  if (!(s->flags & AVFMT_FLAG_BITEXACT))
3814  mov_write_psp_udta_tag(pb, LIBAVCODEC_IDENT, "eng", 0x04);
3815  mov_write_psp_udta_tag(pb, title->value, "eng", 0x01);
3816  mov_write_psp_udta_tag(pb, "2006/04/01 11:11:11", "und", 0x03);
3817 
3818  update_size(pb, pos2);
3819  return update_size(pb, pos);
3820  }
3821 
3822  return 0;
3823 }
3824 
3825 static void build_chunks(MOVTrack *trk)
3826 {
3827  int i;
3828  MOVIentry *chunk = &trk->cluster[0];
3829  uint64_t chunkSize = chunk->size;
3830  chunk->chunkNum = 1;
3831  if (trk->chunkCount)
3832  return;
3833  trk->chunkCount = 1;
3834  for (i = 1; i<trk->entry; i++){
3835  if (chunk->pos + chunkSize == trk->cluster[i].pos &&
3836  chunkSize + trk->cluster[i].size < (1<<20)){
3837  chunkSize += trk->cluster[i].size;
3838  chunk->samples_in_chunk += trk->cluster[i].entries;
3839  } else {
3840  trk->cluster[i].chunkNum = chunk->chunkNum+1;
3841  chunk=&trk->cluster[i];
3842  chunkSize = chunk->size;
3843  trk->chunkCount++;
3844  }
3845  }
3846 }
3847 
3848 /**
3849  * Assign track ids. If option "use_stream_ids_as_track_ids" is set,
3850  * the stream ids are used as track ids.
3851  *
3852  * This assumes mov->tracks and s->streams are in the same order and
3853  * there are no gaps in either of them (so mov->tracks[n] refers to
3854  * s->streams[n]).
3855  *
3856  * As an exception, there can be more entries in
3857  * s->streams than in mov->tracks, in which case new track ids are
3858  * generated (starting after the largest found stream id).
3859  */
3861 {
3862  int i;
3863 
3864  if (mov->track_ids_ok)
3865  return 0;
3866 
3867  if (mov->use_stream_ids_as_track_ids) {
3868  int next_generated_track_id = 0;
3869  for (i = 0; i < s->nb_streams; i++) {
3870  if (s->streams[i]->id > next_generated_track_id)
3871  next_generated_track_id = s->streams[i]->id;
3872  }
3873 
3874  for (i = 0; i < mov->nb_streams; i++) {
3875  if (mov->tracks[i].entry <= 0 && !(mov->flags & FF_MOV_FLAG_FRAGMENT))
3876  continue;
3877 
3878  mov->tracks[i].track_id = i >= s->nb_streams ? ++next_generated_track_id : s->streams[i]->id;
3879  }
3880  } else {
3881  for (i = 0; i < mov->nb_streams; i++) {
3882  if (mov->tracks[i].entry <= 0 && !(mov->flags & FF_MOV_FLAG_FRAGMENT))
3883  continue;
3884 
3885  mov->tracks[i].track_id = i + 1;
3886  }
3887  }
3888 
3889  mov->track_ids_ok = 1;
3890 
3891  return 0;
3892 }
3893 
3895  AVFormatContext *s)
3896 {
3897  int i;
3898  int64_t pos = avio_tell(pb);
3899  avio_wb32(pb, 0); /* size placeholder*/
3900  ffio_wfourcc(pb, "moov");
3901 
3902  mov_setup_track_ids(mov, s);
3903 
3904  for (i = 0; i < mov->nb_streams; i++) {
3905  if (mov->tracks[i].entry <= 0 && !(mov->flags & FF_MOV_FLAG_FRAGMENT))
3906  continue;
3907 
3908  mov->tracks[i].time = mov->time;
3909 
3910  if (mov->tracks[i].entry)
3911  build_chunks(&mov->tracks[i]);
3912  }
3913 
3914  if (mov->chapter_track)
3915  for (i = 0; i < s->nb_streams; i++) {
3916  mov->tracks[i].tref_tag = MKTAG('c','h','a','p');
3917  mov->tracks[i].tref_id = mov->tracks[mov->chapter_track].track_id;
3918  }
3919  for (i = 0; i < mov->nb_streams; i++) {
3920  MOVTrack *track = &mov->tracks[i];
3921  if (track->tag == MKTAG('r','t','p',' ')) {
3922  track->tref_tag = MKTAG('h','i','n','t');
3923  track->tref_id = mov->tracks[track->src_track].track_id;
3924  } else if (track->par->codec_type == AVMEDIA_TYPE_AUDIO) {
3925  int * fallback, size;
3926  fallback = (int*)av_stream_get_side_data(track->st,
3928  &size);
3929  if (fallback != NULL && size == sizeof(int)) {
3930  if (*fallback >= 0 && *fallback < mov->nb_streams) {
3931  track->tref_tag = MKTAG('f','a','l','l');
3932  track->tref_id = mov->tracks[*fallback].track_id;
3933  }
3934  }
3935  }
3936  }
3937  for (i = 0; i < mov->nb_streams; i++) {
3938  if (mov->tracks[i].tag == MKTAG('t','m','c','d')) {
3939  int src_trk = mov->tracks[i].src_track;
3940  mov->tracks[src_trk].tref_tag = mov->tracks[i].tag;
3941  mov->tracks[src_trk].tref_id = mov->tracks[i].track_id;
3942  //src_trk may have a different timescale than the tmcd track
3943  mov->tracks[i].track_duration = av_rescale(mov->tracks[src_trk].track_duration,
3944  mov->tracks[i].timescale,
3945  mov->tracks[src_trk].timescale);
3946  }
3947  }
3948 
3949  mov_write_mvhd_tag(pb, mov);
3950  if (mov->mode != MODE_MOV && !mov->iods_skip)
3951  mov_write_iods_tag(pb, mov);
3952  for (i = 0; i < mov->nb_streams; i++) {
3953  if (mov->tracks[i].entry > 0 || mov->flags & FF_MOV_FLAG_FRAGMENT) {
3954  int ret = mov_write_trak_tag(s, pb, mov, &(mov->tracks[i]), i < s->nb_streams ? s->streams[i] : NULL);
3955  if (ret < 0)
3956  return ret;
3957  }
3958  }
3959  if (mov->flags & FF_MOV_FLAG_FRAGMENT)
3960  mov_write_mvex_tag(pb, mov); /* QuickTime requires trak to precede this */
3961 
3962  if (mov->mode == MODE_PSP)
3963  mov_write_uuidusmt_tag(pb, s);
3964  else
3965  mov_write_udta_tag(pb, mov, s);
3966 
3967  return update_size(pb, pos);
3968 }
3969 
3970 static void param_write_int(AVIOContext *pb, const char *name, int value)
3971 {
3972  avio_printf(pb, "<param name=\"%s\" value=\"%d\" valuetype=\"data\"/>\n", name, value);
3973 }
3974 
3975 static void param_write_string(AVIOContext *pb, const char *name, const char *value)
3976 {
3977  avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, value);
3978 }
3979 
3980 static void param_write_hex(AVIOContext *pb, const char *name, const uint8_t *value, int len)
3981 {
3982  char buf[150];
3983  len = FFMIN(sizeof(buf) / 2 - 1, len);
3984  ff_data_to_hex(buf, value, len, 0);
3985  buf[2 * len] = '\0';
3986  avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, buf);
3987 }
3988 
3990 {
3991  int64_t pos = avio_tell(pb);
3992  int i;
3993  int64_t manifest_bit_rate = 0;
3994  AVCPBProperties *props = NULL;
3995 
3996  static const uint8_t uuid[] = {
3997  0xa5, 0xd4, 0x0b, 0x30, 0xe8, 0x14, 0x11, 0xdd,
3998  0xba, 0x2f, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66
3999  };
4000 
4001  avio_wb32(pb, 0);
4002  ffio_wfourcc(pb, "uuid");
4003  avio_write(pb, uuid, sizeof(uuid));
4004  avio_wb32(pb, 0);
4005 
4006  avio_printf(pb, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
4007  avio_printf(pb, "<smil xmlns=\"http://www.w3.org/2001/SMIL20/Language\">\n");
4008  avio_printf(pb, "<head>\n");
4009  if (!(mov->fc->flags & AVFMT_FLAG_BITEXACT))
4010  avio_printf(pb, "<meta name=\"creator\" content=\"%s\" />\n",
4012  avio_printf(pb, "</head>\n");
4013  avio_printf(pb, "<body>\n");
4014  avio_printf(pb, "<switch>\n");
4015 
4016  mov_setup_track_ids(mov, s);
4017 
4018  for (i = 0; i < mov->nb_streams; i++) {
4019  MOVTrack *track = &mov->tracks[i];
4020  const char *type;
4021  int track_id = track->track_id;
4022  char track_name_buf[32] = { 0 };
4023 
4024  AVStream *st = track->st;
4025  AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL,0);
4026 
4027  if (track->par->codec_type == AVMEDIA_TYPE_VIDEO && !is_cover_image(st)) {
4028  type = "video";
4029  } else if (track->par->codec_type == AVMEDIA_TYPE_AUDIO) {
4030  type = "audio";
4031  } else {
4032  continue;
4033  }
4034 
4036 
4037  if (track->par->bit_rate) {
4038  manifest_bit_rate = track->par->bit_rate;
4039  } else if (props) {
4040  manifest_bit_rate = props->max_bitrate;
4041  }
4042 
4043  avio_printf(pb, "<%s systemBitrate=\"%"PRId64"\">\n", type,
4044  manifest_bit_rate);
4045  param_write_int(pb, "systemBitrate", manifest_bit_rate);
4046  param_write_int(pb, "trackID", track_id);
4047  param_write_string(pb, "systemLanguage", lang ? lang->value : "und");
4048 
4049  /* Build track name piece by piece: */
4050  /* 1. track type */
4051  av_strlcat(track_name_buf, type, sizeof(track_name_buf));
4052  /* 2. track language, if available */
4053  if (lang)
4054  av_strlcatf(track_name_buf, sizeof(track_name_buf),
4055  "_%s", lang->value);
4056  /* 3. special type suffix */
4057  /* "_cc" = closed captions, "_ad" = audio_description */
4059  av_strlcat(track_name_buf, "_cc", sizeof(track_name_buf));
4061  av_strlcat(track_name_buf, "_ad", sizeof(track_name_buf));
4062 
4063  param_write_string(pb, "trackName", track_name_buf);
4064 
4065  if (track->par->codec_type == AVMEDIA_TYPE_VIDEO) {
4066  if (track->par->codec_id == AV_CODEC_ID_H264) {
4067  uint8_t *ptr;
4068  int size = track->par->extradata_size;
4069  if (!ff_avc_write_annexb_extradata(track->par->extradata, &ptr,
4070  &size)) {
4071  param_write_hex(pb, "CodecPrivateData",
4072  ptr ? ptr : track->par->extradata,
4073  size);
4074  av_free(ptr);
4075  }
4076  param_write_string(pb, "FourCC", "H264");
4077  } else if (track->par->codec_id == AV_CODEC_ID_VC1) {
4078  param_write_string(pb, "FourCC", "WVC1");
4079  param_write_hex(pb, "CodecPrivateData", track->par->extradata,
4080  track->par->extradata_size);
4081  }
4082  param_write_int(pb, "MaxWidth", track->par->width);
4083  param_write_int(pb, "MaxHeight", track->par->height);
4084  param_write_int(pb, "DisplayWidth", track->par->width);
4085  param_write_int(pb, "DisplayHeight", track->par->height);
4086  } else {
4087  if (track->par->codec_id == AV_CODEC_ID_AAC) {
4088  switch (track->par->profile)
4089  {
4090  case FF_PROFILE_AAC_HE_V2:
4091  param_write_string(pb, "FourCC", "AACP");
4092  break;
4093  case FF_PROFILE_AAC_HE:
4094  param_write_string(pb, "FourCC", "AACH");
4095  break;
4096  default:
4097  param_write_string(pb, "FourCC", "AACL");
4098  }
4099  } else if (track->par->codec_id == AV_CODEC_ID_WMAPRO) {
4100  param_write_string(pb, "FourCC", "WMAP");
4101  }
4102  param_write_hex(pb, "CodecPrivateData", track->par->extradata,
4103  track->par->extradata_size);
4105  track->par->codec_id));
4106  param_write_int(pb, "Channels", track->par->channels);
4107  param_write_int(pb, "SamplingRate", track->par->sample_rate);
4108  param_write_int(pb, "BitsPerSample", 16);
4109  param_write_int(pb, "PacketSize", track->par->block_align ?
4110  track->par->block_align : 4);
4111  }
4112  avio_printf(pb, "</%s>\n", type);
4113  }
4114  avio_printf(pb, "</switch>\n");
4115  avio_printf(pb, "</body>\n");
4116  avio_printf(pb, "</smil>\n");
4117 
4118  return update_size(pb, pos);
4119 }
4120 
4122 {
4123  avio_wb32(pb, 16);
4124  ffio_wfourcc(pb, "mfhd");
4125  avio_wb32(pb, 0);
4126  avio_wb32(pb, mov->fragments);
4127  return 0;
4128 }
4129 
4130 static uint32_t get_sample_flags(MOVTrack *track, MOVIentry *entry)
4131 {
4134 }
4135 
4137  MOVTrack *track, int64_t moof_offset)
4138 {
4139  int64_t pos = avio_tell(pb);
4142  if (!track->entry) {
4143  flags |= MOV_TFHD_DURATION_IS_EMPTY;
4144  } else {
4145  flags |= MOV_TFHD_DEFAULT_FLAGS;
4146  }
4148  flags &= ~MOV_TFHD_BASE_DATA_OFFSET;
4149  if (mov->flags & FF_MOV_FLAG_DEFAULT_BASE_MOOF) {
4150  flags &= ~MOV_TFHD_BASE_DATA_OFFSET;
4152  }
4153 
4154  /* Don't set a default sample size, the silverlight player refuses
4155  * to play files with that set. Don't set a default sample duration,
4156  * WMP freaks out if it is set. Don't set a base data offset, PIFF
4157  * file format says it MUST NOT be set. */
4158  if (track->mode == MODE_ISM)
4161 
4162  avio_wb32(pb, 0); /* size placeholder */
4163  ffio_wfourcc(pb, "tfhd");
4164  avio_w8(pb, 0); /* version */
4165  avio_wb24(pb, flags);
4166 
4167  avio_wb32(pb, track->track_id); /* track-id */
4168  if (flags & MOV_TFHD_BASE_DATA_OFFSET)
4169  avio_wb64(pb, moof_offset);
4170  if (flags & MOV_TFHD_DEFAULT_DURATION) {
4171  track->default_duration = get_cluster_duration(track, 0);
4172  avio_wb32(pb, track->default_duration);
4173  }
4174  if (flags & MOV_TFHD_DEFAULT_SIZE) {
4175  track->default_size = track->entry ? track->cluster[0].size : 1;
4176  avio_wb32(pb, track->default_size);
4177  } else
4178  track->default_size = -1;
4179 
4180  if (flags & MOV_TFHD_DEFAULT_FLAGS) {
4181  /* Set the default flags based on the second sample, if available.
4182  * If the first sample is different, that can be signaled via a separate field. */
4183  if (track->entry > 1)
4184  track->default_sample_flags = get_sample_flags(track, &track->cluster[1]);
4185  else
4186  track->default_sample_flags =
4187  track->par->codec_type == AVMEDIA_TYPE_VIDEO ?
4190  avio_wb32(pb, track->default_sample_flags);
4191  }
4192 
4193  return update_size(pb, pos);
4194 }
4195 
4197  MOVTrack *track, int moof_size,
4198  int first, int end)
4199 {
4200  int64_t pos = avio_tell(pb);
4201  uint32_t flags = MOV_TRUN_DATA_OFFSET;
4202  int i;
4203 
4204  for (i = first; i < end; i++) {
4205  if (get_cluster_duration(track, i) != track->default_duration)
4206  flags |= MOV_TRUN_SAMPLE_DURATION;
4207  if (track->cluster[i].size != track->default_size)
4208  flags |= MOV_TRUN_SAMPLE_SIZE;
4209  if (i > first && get_sample_flags(track, &track->cluster[i]) != track->default_sample_flags)
4210  flags |= MOV_TRUN_SAMPLE_FLAGS;
4211  }
4212  if (!(flags & MOV_TRUN_SAMPLE_FLAGS) && track->entry > 0 &&
4213  get_sample_flags(track, &track->cluster[0]) != track->default_sample_flags)
4214  flags |= MOV_TRUN_FIRST_SAMPLE_FLAGS;
4215  if (track->flags & MOV_TRACK_CTTS)
4216  flags |= MOV_TRUN_SAMPLE_CTS;
4217 
4218  avio_wb32(pb, 0); /* size placeholder */
4219  ffio_wfourcc(pb, "trun");
4221  avio_w8(pb, 1); /* version */
4222  else
4223  avio_w8(pb, 0); /* version */
4224  avio_wb24(pb, flags);
4225 
4226  avio_wb32(pb, end - first); /* sample count */
4227  if (mov->flags & FF_MOV_FLAG_OMIT_TFHD_OFFSET &&
4229  !mov->first_trun)
4230  avio_wb32(pb, 0); /* Later tracks follow immediately after the previous one */
4231  else
4232  avio_wb32(pb, moof_size + 8 + track->data_offset +
4233  track->cluster[first].pos); /* data offset */
4234  if (flags & MOV_TRUN_FIRST_SAMPLE_FLAGS)
4235  avio_wb32(pb, get_sample_flags(track, &track->cluster[first]));
4236 
4237  for (i = first; i < end; i++) {
4238  if (flags & MOV_TRUN_SAMPLE_DURATION)
4239  avio_wb32(pb, get_cluster_duration(track, i));
4240  if (flags & MOV_TRUN_SAMPLE_SIZE)
4241  avio_wb32(pb, track->cluster[i].size);
4242  if (flags & MOV_TRUN_SAMPLE_FLAGS)
4243  avio_wb32(pb, get_sample_flags(track, &track->cluster[i]));
4244  if (flags & MOV_TRUN_SAMPLE_CTS)
4245  avio_wb32(pb, track->cluster[i].cts);
4246  }
4247 
4248  mov->first_trun = 0;
4249  return update_size(pb, pos);
4250 }
4251 
4252 static int mov_write_tfxd_tag(AVIOContext *pb, MOVTrack *track)
4253 {
4254  int64_t pos = avio_tell(pb);
4255  static const uint8_t uuid[] = {
4256  0x6d, 0x1d, 0x9b, 0x05, 0x42, 0xd5, 0x44, 0xe6,
4257  0x80, 0xe2, 0x14, 0x1d, 0xaf, 0xf7, 0x57, 0xb2
4258  };
4259 
4260  avio_wb32(pb, 0); /* size placeholder */
4261  ffio_wfourcc(pb, "uuid");
4262  avio_write(pb, uuid, sizeof(uuid));
4263  avio_w8(pb, 1);
4264  avio_wb24(pb, 0);
4265  avio_wb64(pb, track->start_dts + track->frag_start +
4266  track->cluster[0].cts);
4267  avio_wb64(pb, track->end_pts -
4268  (track->cluster[0].dts + track->cluster[0].cts));
4269 
4270  return update_size(pb, pos);
4271 }
4272 
4274  MOVTrack *track, int entry)
4275 {
4276  int n = track->nb_frag_info - 1 - entry, i;
4277  int size = 8 + 16 + 4 + 1 + 16*n;
4278  static const uint8_t uuid[] = {
4279  0xd4, 0x80, 0x7e, 0xf2, 0xca, 0x39, 0x46, 0x95,
4280  0x8e, 0x54, 0x26, 0xcb, 0x9e, 0x46, 0xa7, 0x9f
4281  };
4282 
4283  if (entry < 0)
4284  return 0;
4285 
4286  avio_seek(pb, track->frag_info[entry].tfrf_offset, SEEK_SET);
4287  avio_wb32(pb, size);
4288  ffio_wfourcc(pb, "uuid");
4289  avio_write(pb, uuid, sizeof(uuid));
4290  avio_w8(pb, 1);
4291  avio_wb24(pb, 0);
4292  avio_w8(pb, n);
4293  for (i = 0; i < n; i++) {
4294  int index = entry + 1 + i;
4295  avio_wb64(pb, track->frag_info[index].time);
4296  avio_wb64(pb, track->frag_info[index].duration);
4297  }
4298  if (n < mov->ism_lookahead) {
4299  int free_size = 16 * (mov->ism_lookahead - n);
4300  avio_wb32(pb, free_size);
4301  ffio_wfourcc(pb, "free");
4302  ffio_fill(pb, 0, free_size - 8);
4303  }
4304 
4305  return 0;
4306 }
4307 
4309  MOVTrack *track)
4310 {
4311  int64_t pos = avio_tell(pb);
4312  int i;
4313  for (i = 0; i < mov->ism_lookahead; i++) {
4314  /* Update the tfrf tag for the last ism_lookahead fragments,
4315  * nb_frag_info - 1 is the next fragment to be written. */
4316  mov_write_tfrf_tag(pb, mov, track, track->nb_frag_info - 2 - i);
4317  }
4318  avio_seek(pb, pos, SEEK_SET);
4319  return 0;
4320 }
4321 
4322 static int mov_add_tfra_entries(AVIOContext *pb, MOVMuxContext *mov, int tracks,
4323  int size)
4324 {
4325  int i;
4326  for (i = 0; i < mov->nb_streams; i++) {
4327  MOVTrack *track = &mov->tracks[i];
4328  MOVFragmentInfo *info;
4329  if ((tracks >= 0 && i != tracks) || !track->entry)
4330  continue;
4331  track->nb_frag_info++;
4332  if (track->nb_frag_info >= track->frag_info_capacity) {
4333  unsigned new_capacity = track->nb_frag_info + MOV_FRAG_INFO_ALLOC_INCREMENT;
4334  if (av_reallocp_array(&track->frag_info,
4335  new_capacity,
4336  sizeof(*track->frag_info)))
4337  return AVERROR(ENOMEM);
4338  track->frag_info_capacity = new_capacity;
4339  }
4340  info = &track->frag_info[track->nb_frag_info - 1];
4341  info->offset = avio_tell(pb);
4342  info->size = size;
4343  // Try to recreate the original pts for the first packet
4344  // from the fields we have stored
4345  info->time = track->start_dts + track->frag_start +
4346  track->cluster[0].cts;
4347  info->duration = track->end_pts -
4348  (track->cluster[0].dts + track->cluster[0].cts);
4349  // If the pts is less than zero, we will have trimmed
4350  // away parts of the media track using an edit list,
4351  // and the corresponding start presentation time is zero.
4352  if (info->time < 0) {
4353  info->duration += info->time;
4354  info->time = 0;
4355  }
4356  info->tfrf_offset = 0;
4357  mov_write_tfrf_tags(pb, mov, track);
4358  }
4359  return 0;
4360 }
4361 
4362 static void mov_prune_frag_info(MOVMuxContext *mov, int tracks, int max)
4363 {
4364  int i;
4365  for (i = 0; i < mov->nb_streams; i++) {
4366  MOVTrack *track = &mov->tracks[i];
4367  if ((tracks >= 0 && i != tracks) || !track->entry)
4368  continue;
4369  if (track->nb_frag_info > max) {
4370  memmove(track->frag_info, track->frag_info + (track->nb_frag_info - max), max * sizeof(*track->frag_info));
4371  track->nb_frag_info = max;
4372  }
4373  }
4374 }
4375 
4376 static int mov_write_tfdt_tag(AVIOContext *pb, MOVTrack *track)
4377 {
4378  int64_t pos = avio_tell(pb);
4379 
4380  avio_wb32(pb, 0); /* size */
4381  ffio_wfourcc(pb, "tfdt");
4382  avio_w8(pb, 1); /* version */
4383  avio_wb24(pb, 0);
4384  avio_wb64(pb, track->frag_start);
4385  return update_size(pb, pos);
4386 }
4387 
4389  MOVTrack *track, int64_t moof_offset,
4390  int moof_size)
4391 {
4392  int64_t pos = avio_tell(pb);
4393  int i, start = 0;
4394  avio_wb32(pb, 0); /* size placeholder */
4395  ffio_wfourcc(pb, "traf");
4396 
4397  mov_write_tfhd_tag(pb, mov, track, moof_offset);
4398  if (mov->mode != MODE_ISM)
4399  mov_write_tfdt_tag(pb, track);
4400  for (i = 1; i < track->entry; i++) {
4401  if (track->cluster[i].pos != track->cluster[i - 1].pos + track->cluster[i - 1].size) {
4402  mov_write_trun_tag(pb, mov, track, moof_size, start, i);
4403  start = i;
4404  }
4405  }
4406  mov_write_trun_tag(pb, mov, track, moof_size, start, track->entry);
4407  if (mov->mode == MODE_ISM) {
4408  mov_write_tfxd_tag(pb, track);
4409 
4410  if (mov->ism_lookahead) {
4411  int i, size = 16 + 4 + 1 + 16 * mov->ism_lookahead;
4412 
4413  if (track->nb_frag_info > 0) {
4414  MOVFragmentInfo *info = &track->frag_info[track->nb_frag_info - 1];
4415  if (!info->tfrf_offset)
4416  info->tfrf_offset = avio_tell(pb);
4417  }
4418  avio_wb32(pb, 8 + size);
4419  ffio_wfourcc(pb, "free");
4420  for (i = 0; i < size; i++)
4421  avio_w8(pb, 0);
4422  }
4423  }
4424 
4425  return update_size(pb, pos);
4426 }
4427 
4429  int tracks, int moof_size)
4430 {
4431  int64_t pos = avio_tell(pb);
4432  int i;
4433 
4434  avio_wb32(pb, 0); /* size placeholder */
4435  ffio_wfourcc(pb, "moof");
4436  mov->first_trun = 1;
4437 
4438  mov_write_mfhd_tag(pb, mov);
4439  for (i = 0; i < mov->nb_streams; i++) {
4440  MOVTrack *track = &mov->tracks[i];
4441  if (tracks >= 0 && i != tracks)
4442  continue;
4443  if (!track->entry)
4444  continue;
4445  mov_write_traf_tag(pb, mov, track, pos, moof_size);
4446  }
4447 
4448  return update_size(pb, pos);
4449 }
4450 
4452  MOVTrack *track, int ref_size, int total_sidx_size)
4453 {
4454  int64_t pos = avio_tell(pb), offset_pos, end_pos;
4455  int64_t presentation_time, duration, offset;
4456  int starts_with_SAP, i, entries;
4457 
4458  if (track->entry) {
4459  entries = 1;
4460  presentation_time = track->start_dts + track->frag_start +
4461  track->cluster[0].cts;
4462  duration = track->end_pts -
4463  (track->cluster[0].dts + track->cluster[0].cts);
4464  starts_with_SAP = track->cluster[0].flags & MOV_SYNC_SAMPLE;
4465 
4466  // pts<0 should be cut away using edts
4467  if (presentation_time < 0) {
4468  duration += presentation_time;
4469  presentation_time = 0;
4470  }
4471  } else {
4472  entries = track->nb_frag_info;
4473  if (entries <= 0)
4474  return 0;
4475  presentation_time = track->frag_info[0].time;
4476  }
4477 
4478  avio_wb32(pb, 0); /* size */
4479  ffio_wfourcc(pb, "sidx");
4480  avio_w8(pb, 1); /* version */
4481  avio_wb24(pb, 0);
4482  avio_wb32(pb, track->track_id); /* reference_ID */
4483  avio_wb32(pb, track->timescale); /* timescale */
4484  avio_wb64(pb, presentation_time); /* earliest_presentation_time */
4485  offset_pos = avio_tell(pb);
4486  avio_wb64(pb, 0); /* first_offset (offset to referenced moof) */
4487  avio_wb16(pb, 0); /* reserved */
4488 
4489  avio_wb16(pb, entries); /* reference_count */
4490  for (i = 0; i < entries; i++) {
4491  if (!track->entry) {
4492  if (i > 1 && track->frag_info[i].offset != track->frag_info[i - 1].offset + track->frag_info[i - 1].size) {
4493  av_log(NULL, AV_LOG_ERROR, "Non-consecutive fragments, writing incorrect sidx\n");
4494  }
4495  duration = track->frag_info[i].duration;
4496  ref_size = track->frag_info[i].size;
4497  starts_with_SAP = 1;
4498  }
4499  avio_wb32(pb, (0 << 31) | (ref_size & 0x7fffffff)); /* reference_type (0 = media) | referenced_size */
4500  avio_wb32(pb, duration); /* subsegment_duration */
4501  avio_wb32(pb, (starts_with_SAP << 31) | (0 << 28) | 0); /* starts_with_SAP | SAP_type | SAP_delta_time */
4502  }
4503 
4504  end_pos = avio_tell(pb);
4505  offset = pos + total_sidx_size - end_pos;
4506  avio_seek(pb, offset_pos, SEEK_SET);
4507  avio_wb64(pb, offset);
4508  avio_seek(pb, end_pos, SEEK_SET);
4509  return update_size(pb, pos);
4510 }
4511 
4513  int tracks, int ref_size)
4514 {
4515  int i, round, ret;
4516  AVIOContext *avio_buf;
4517  int total_size = 0;
4518  for (round = 0; round < 2; round++) {
4519  // First run one round to calculate the total size of all
4520  // sidx atoms.
4521  // This would be much simpler if we'd only write one sidx
4522  // atom, for the first track in the moof.
4523  if (round == 0) {
4524  if ((ret = ffio_open_null_buf(&avio_buf)) < 0)
4525  return ret;
4526  } else {
4527  avio_buf = pb;
4528  }
4529  for (i = 0; i < mov->nb_streams; i++) {
4530  MOVTrack *track = &mov->tracks[i];
4531  if (tracks >= 0 && i != tracks)
4532  continue;
4533  // When writing a sidx for the full file, entry is 0, but
4534  // we want to include all tracks. ref_size is 0 in this case,
4535  // since we read it from frag_info instead.
4536  if (!track->entry && ref_size > 0)
4537  continue;
4538  total_size -= mov_write_sidx_tag(avio_buf, track, ref_size,
4539  total_size);
4540  }
4541  if (round == 0)
4542  total_size = ffio_close_null_buf(avio_buf);
4543  }
4544  return 0;
4545 }
4546 
4547 static int mov_write_prft_tag(AVIOContext *pb, MOVMuxContext *mov, int tracks)
4548 {
4549  int64_t pos = avio_tell(pb), pts_us, ntp_ts;
4550  MOVTrack *first_track;
4551 
4552  /* PRFT should be associated with at most one track. So, choosing only the
4553  * first track. */
4554  if (tracks > 0)
4555  return 0;
4556  first_track = &(mov->tracks[0]);
4557 
4558  if (!first_track->entry) {
4559  av_log(mov->fc, AV_LOG_WARNING, "Unable to write PRFT, no entries in the track\n");
4560  return 0;
4561  }
4562 
4563  if (first_track->cluster[0].pts == AV_NOPTS_VALUE) {
4564  av_log(mov->fc, AV_LOG_WARNING, "Unable to write PRFT, first PTS is invalid\n");
4565  return 0;
4566  }
4567 
4568  if (mov->write_prft == MOV_PRFT_SRC_WALLCLOCK) {
4570  } else if (mov->write_prft == MOV_PRFT_SRC_PTS) {
4571  pts_us = av_rescale_q(first_track->cluster[0].pts,
4572  first_track->st->time_base, AV_TIME_BASE_Q);
4573  ntp_ts = ff_get_formatted_ntp_time(pts_us + NTP_OFFSET_US);
4574  } else {
4575  av_log(mov->fc, AV_LOG_WARNING, "Unsupported PRFT box configuration: %d\n",
4576  mov->write_prft);
4577  return 0;
4578  }
4579 
4580  avio_wb32(pb, 0); // Size place holder
4581  ffio_wfourcc(pb, "prft"); // Type
4582  avio_w8(pb, 1); // Version
4583  avio_wb24(pb, 0); // Flags
4584  avio_wb32(pb, first_track->track_id); // reference track ID
4585  avio_wb64(pb, ntp_ts); // NTP time stamp
4586  avio_wb64(pb, first_track->cluster[0].pts); //media time
4587  return update_size(pb, pos);
4588 }
4589 
4590 static int mov_write_moof_tag(AVIOContext *pb, MOVMuxContext *mov, int tracks,
4591  int64_t mdat_size)
4592 {
4593  AVIOContext *avio_buf;
4594  int ret, moof_size;
4595 
4596  if ((ret = ffio_open_null_buf(&avio_buf)) < 0)
4597  return ret;
4598  mov_write_moof_tag_internal(avio_buf, mov, tracks, 0);
4599  moof_size = ffio_close_null_buf(avio_buf);
4600 
4601  if (mov->flags & FF_MOV_FLAG_DASH && !(mov->flags & FF_MOV_FLAG_GLOBAL_SIDX))
4602  mov_write_sidx_tags(pb, mov, tracks, moof_size + 8 + mdat_size);
4603 
4604  if (mov->write_prft > MOV_PRFT_NONE && mov->write_prft < MOV_PRFT_NB)
4605  mov_write_prft_tag(pb, mov, tracks);
4606 
4607  if (mov->flags & FF_MOV_FLAG_GLOBAL_SIDX ||
4608  !(mov->flags & FF_MOV_FLAG_SKIP_TRAILER) ||
4609  mov->ism_lookahead) {
4610  if ((ret = mov_add_tfra_entries(pb, mov, tracks, moof_size + 8 + mdat_size)) < 0)
4611  return ret;
4612  if (!(mov->flags & FF_MOV_FLAG_GLOBAL_SIDX) &&
4614  mov_prune_frag_info(mov, tracks, mov->ism_lookahead + 1);
4615  }
4616  }
4617 
4618  return mov_write_moof_tag_internal(pb, mov, tracks, moof_size);
4619 }
4620 
4621 static int mov_write_tfra_tag(AVIOContext *pb, MOVTrack *track)
4622 {
4623  int64_t pos = avio_tell(pb);
4624  int i;
4625 
4626  avio_wb32(pb, 0); /* size placeholder */
4627  ffio_wfourcc(pb, "tfra");
4628  avio_w8(pb, 1); /* version */
4629  avio_wb24(pb, 0);
4630 
4631  avio_wb32(pb, track->track_id);
4632  avio_wb32(pb, 0); /* length of traf/trun/sample num */
4633  avio_wb32(pb, track->nb_frag_info);
4634  for (i = 0; i < track->nb_frag_info; i++) {
4635  avio_wb64(pb, track->frag_info[i].time);
4636  avio_wb64(pb, track->frag_info[i].offset + track->data_offset);
4637  avio_w8(pb, 1); /* traf number */
4638  avio_w8(pb, 1); /* trun number */
4639  avio_w8(pb, 1); /* sample number */
4640  }
4641 
4642  return update_size(pb, pos);
4643 }
4644 
4646 {
4647  int64_t pos = avio_tell(pb);
4648  int i;
4649 
4650  avio_wb32(pb, 0); /* size placeholder */
4651  ffio_wfourcc(pb, "mfra");
4652  /* An empty mfra atom is enough to indicate to the publishing point that
4653  * the stream has ended. */
4654  if (mov->flags & FF_MOV_FLAG_ISML)
4655  return update_size(pb, pos);
4656 
4657  for (i = 0; i < mov->nb_streams; i++) {
4658  MOVTrack *track = &mov->tracks[i];
4659  if (track->nb_frag_info)
4660  mov_write_tfra_tag(pb, track);
4661  }
4662 
4663  avio_wb32(pb, 16);
4664  ffio_wfourcc(pb, "mfro");
4665  avio_wb32(pb, 0); /* version + flags */
4666  avio_wb32(pb, avio_tell(pb) + 4 - pos);
4667 
4668  return update_size(pb, pos);
4669 }
4670 
4672 {
4673  avio_wb32(pb, 8); // placeholder for extended size field (64 bit)
4674  ffio_wfourcc(pb, mov->mode == MODE_MOV ? "wide" : "free");
4675 
4676  mov->mdat_pos = avio_tell(pb);
4677  avio_wb32(pb, 0); /* size placeholder*/
4678  ffio_wfourcc(pb, "mdat");
4679  return 0;
4680 }
4681 
4682 /* TODO: This needs to be more general */
4684 {
4685  MOVMuxContext *mov = s->priv_data;
4686  int64_t pos = avio_tell(pb);
4687  int has_h264 = 0, has_video = 0;
4688  int minor = 0x200;
4689  int i;
4690 
4691  for (i = 0; i < s->nb_streams; i++) {
4692  AVStream *st = s->streams[i];
4693  if (is_cover_image(st))
4694  continue;
4696  has_video = 1;
4697  if (st->codecpar->codec_id == AV_CODEC_ID_H264)
4698  has_h264 = 1;
4699  }
4700 
4701  avio_wb32(pb, 0); /* size */
4702  ffio_wfourcc(pb, "ftyp");
4703 
4704  if (mov->major_brand && strlen(mov->major_brand) >= 4)
4705  ffio_wfourcc(pb, mov->major_brand);
4706  else if (mov->mode == MODE_3GP) {
4707  ffio_wfourcc(pb, has_h264 ? "3gp6" : "3gp4");
4708  minor = has_h264 ? 0x100 : 0x200;
4709  } else if (mov->mode & MODE_3G2) {
4710  ffio_wfourcc(pb, has_h264 ? "3g2b" : "3g2a");
4711  minor = has_h264 ? 0x20000 : 0x10000;
4712  } else if (mov->mode == MODE_PSP)
4713  ffio_wfourcc(pb, "MSNV");
4714  else if (mov->mode == MODE_MP4 && mov->flags & FF_MOV_FLAG_DEFAULT_BASE_MOOF)
4715  ffio_wfourcc(pb, "iso5"); // Required when using default-base-is-moof
4716  else if (mov->mode == MODE_MP4 && mov->flags & FF_MOV_FLAG_NEGATIVE_CTS_OFFSETS)
4717  ffio_wfourcc(pb, "iso4");
4718  else if (mov->mode == MODE_MP4)
4719  ffio_wfourcc(pb, "isom");
4720  else if (mov->mode == MODE_IPOD)
4721  ffio_wfourcc(pb, has_video ? "M4V ":"M4A ");
4722  else if (mov->mode == MODE_ISM)
4723  ffio_wfourcc(pb, "isml");
4724  else if (mov->mode == MODE_F4V)
4725  ffio_wfourcc(pb, "f4v ");
4726  else
4727  ffio_wfourcc(pb, "qt ");
4728 
4729  avio_wb32(pb, minor);
4730 
4731  if (mov->mode == MODE_MOV)
4732  ffio_wfourcc(pb, "qt ");
4733  else if (mov->mode == MODE_ISM) {
4734  ffio_wfourcc(pb, "piff");
4735  } else if (!(mov->flags & FF_MOV_FLAG_DEFAULT_BASE_MOOF)) {
4736  ffio_wfourcc(pb, "isom");
4737  ffio_wfourcc(pb, "iso2");
4738  if (has_h264)
4739  ffio_wfourcc(pb, "avc1");
4740  }
4741 
4742  // We add tfdt atoms when fragmenting, signal this with the iso6 compatible
4743  // brand. This is compatible with users that don't understand tfdt.
4744  if (mov->flags & FF_MOV_FLAG_FRAGMENT && mov->mode != MODE_ISM)
4745  ffio_wfourcc(pb, "iso6");
4746 
4747  if (mov->mode == MODE_3GP)
4748  ffio_wfourcc(pb, has_h264 ? "3gp6":"3gp4");
4749  else if (mov->mode & MODE_3G2)
4750  ffio_wfourcc(pb, has_h264 ? "3g2b":"3g2a");
4751  else if (mov->mode == MODE_PSP)
4752  ffio_wfourcc(pb, "MSNV");
4753  else if (mov->mode == MODE_MP4)
4754  ffio_wfourcc(pb, "mp41");
4755 
4756  if (mov->flags & FF_MOV_FLAG_DASH && mov->flags & FF_MOV_FLAG_GLOBAL_SIDX)
4757  ffio_wfourcc(pb, "dash");
4758 
4759  return update_size(pb, pos);
4760 }
4761 
4763 {
4764  AVStream *video_st = s->streams[0];
4765  AVCodecParameters *video_par = s->streams[0]->codecpar;
4766  AVCodecParameters *audio_par = s->streams[1]->codecpar;
4767  int audio_rate = audio_par->sample_rate;
4768  int64_t frame_rate = video_st->avg_frame_rate.den ?
4769  (video_st->avg_frame_rate.num * 0x10000LL) / video_st->avg_frame_rate.den :
4770  0;
4771  int audio_kbitrate = audio_par->bit_rate / 1000;
4772  int video_kbitrate = FFMIN(video_par->bit_rate / 1000, 800 - audio_kbitrate);
4773 
4774  if (frame_rate < 0 || frame_rate > INT32_MAX) {
4775  av_log(s, AV_LOG_ERROR, "Frame rate %f outside supported range\n", frame_rate / (double)0x10000);
4776  return AVERROR(EINVAL);
4777  }
4778 
4779  avio_wb32(pb, 0x94); /* size */
4780  ffio_wfourcc(pb, "uuid");
4781  ffio_wfourcc(pb, "PROF");
4782 
4783  avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
4784  avio_wb32(pb, 0xbb88695c);
4785  avio_wb32(pb, 0xfac9c740);
4786 
4787  avio_wb32(pb, 0x0); /* ? */
4788  avio_wb32(pb, 0x3); /* 3 sections ? */
4789 
4790  avio_wb32(pb, 0x14); /* size */
4791  ffio_wfourcc(pb, "FPRF");
4792  avio_wb32(pb, 0x0); /* ? */
4793  avio_wb32(pb, 0x0); /* ? */
4794  avio_wb32(pb, 0x0); /* ? */
4795 
4796  avio_wb32(pb, 0x2c); /* size */
4797  ffio_wfourcc(pb, "APRF"); /* audio */
4798  avio_wb32(pb, 0x0);
4799  avio_wb32(pb, 0x2); /* TrackID */
4800  ffio_wfourcc(pb, "mp4a");
4801  avio_wb32(pb, 0x20f);
4802  avio_wb32(pb, 0x0);
4803  avio_wb32(pb, audio_kbitrate);
4804  avio_wb32(pb, audio_kbitrate);
4805  avio_wb32(pb, audio_rate);
4806  avio_wb32(pb, audio_par->channels);
4807 
4808  avio_wb32(pb, 0x34); /* size */
4809  ffio_wfourcc(pb, "VPRF"); /* video */
4810  avio_wb32(pb, 0x0);
4811  avio_wb32(pb, 0x1); /* TrackID */
4812  if (video_par->codec_id == AV_CODEC_ID_H264) {
4813  ffio_wfourcc(pb, "avc1");
4814  avio_wb16(pb, 0x014D);
4815  avio_wb16(pb, 0x0015);
4816  } else {
4817  ffio_wfourcc(pb, "mp4v");
4818  avio_wb16(pb, 0x0000);
4819  avio_wb16(pb, 0x0103);
4820  }
4821  avio_wb32(pb, 0x0);
4822  avio_wb32(pb, video_kbitrate);
4823  avio_wb32(pb, video_kbitrate);
4824  avio_wb32(pb, frame_rate);
4825  avio_wb32(pb, frame_rate);
4826  avio_wb16(pb, video_par->width);
4827  avio_wb16(pb, video_par->height);
4828  avio_wb32(pb, 0x010001); /* ? */
4829 
4830  return 0;
4831 }
4832 
4834 {
4835  MOVMuxContext *mov = s->priv_data;
4836  int i;
4837 
4838  mov_write_ftyp_tag(pb,s);
4839  if (mov->mode == MODE_PSP) {
4840  int video_streams_nb = 0, audio_streams_nb = 0, other_streams_nb = 0;
4841  for (i = 0; i < s->nb_streams; i++) {
4842  AVStream *st = s->streams[i];
4843  if (is_cover_image(st))
4844  continue;
4846  video_streams_nb++;
4847  else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
4848  audio_streams_nb++;
4849  else
4850  other_streams_nb++;
4851  }
4852 
4853  if (video_streams_nb != 1 || audio_streams_nb != 1 || other_streams_nb) {
4854  av_log(s, AV_LOG_ERROR, "PSP mode need one video and one audio stream\n");
4855  return AVERROR(EINVAL);
4856  }
4857  return mov_write_uuidprof_tag(pb, s);
4858  }
4859  return 0;
4860 }
4861 
4862 static int mov_parse_mpeg2_frame(AVPacket *pkt, uint32_t *flags)
4863 {
4864  uint32_t c = -1;
4865  int i, closed_gop = 0;
4866 
4867  for (i = 0; i < pkt->size - 4; i++) {
4868  c = (c << 8) + pkt->data[i];
4869  if (c == 0x1b8) { // gop
4870  closed_gop = pkt->data[i + 4] >> 6 & 0x01;
4871  } else if (c == 0x100) { // pic
4872  int temp_ref = (pkt->data[i + 1] << 2) | (pkt->data[i + 2] >> 6);
4873  if (!temp_ref || closed_gop) // I picture is not reordered
4874  *flags = MOV_SYNC_SAMPLE;
4875  else
4876  *flags = MOV_PARTIAL_SYNC_SAMPLE;
4877  break;
4878  }
4879  }
4880  return 0;
4881 }
4882 
4884 {
4885  const uint8_t *start, *next, *end = pkt->data + pkt->size;
4886  int seq = 0, entry = 0;
4887  int key = pkt->flags & AV_PKT_FLAG_KEY;
4888  start = find_next_marker(pkt->data, end);
4889  for (next = start; next < end; start = next) {
4890  next = find_next_marker(start + 4, end);
4891  switch (AV_RB32(start)) {
4892  case VC1_CODE_SEQHDR:
4893  seq = 1;
4894  break;
4895  case VC1_CODE_ENTRYPOINT:
4896  entry = 1;
4897  break;
4898  case VC1_CODE_SLICE:
4899  trk->vc1_info.slices = 1;
4900  break;
4901  }
4902  }
4903  if (!trk->entry && trk->vc1_info.first_packet_seen)
4904  trk->vc1_info.first_frag_written = 1;
4905  if (!trk->entry && !trk->vc1_info.first_frag_written) {
4906  /* First packet in first fragment */
4907  trk->vc1_info.first_packet_seq = seq;
4908  trk->vc1_info.first_packet_entry = entry;
4909  trk->vc1_info.first_packet_seen = 1;
4910  } else if ((seq && !trk->vc1_info.packet_seq) ||
4911  (entry && !trk->vc1_info.packet_entry)) {
4912  int i;
4913  for (i = 0; i < trk->entry; i++)
4914  trk->cluster[i].flags &= ~MOV_SYNC_SAMPLE;
4915  trk->has_keyframes = 0;
4916  if (seq)
4917  trk->vc1_info.packet_seq = 1;
4918  if (entry)
4919  trk->vc1_info.packet_entry = 1;
4920  if (!trk->vc1_info.first_frag_written) {
4921  /* First fragment */
4922  if ((!seq || trk->vc1_info.first_packet_seq) &&
4923  (!entry || trk->vc1_info.first_packet_entry)) {
4924  /* First packet had the same headers as this one, readd the
4925  * sync sample flag. */
4926  trk->cluster[0].flags |= MOV_SYNC_SAMPLE;
4927  trk->has_keyframes = 1;
4928  }
4929  }
4930  }
4931  if (trk->vc1_info.packet_seq && trk->vc1_info.packet_entry)
4932  key = seq && entry;
4933  else if (trk->vc1_info.packet_seq)
4934  key = seq;
4935  else if (trk->vc1_info.packet_entry)
4936  key = entry;
4937  if (key) {
4938  trk->cluster[trk->entry].flags |= MOV_SYNC_SAMPLE;
4939  trk->has_keyframes++;
4940  }
4941 }
4942 
4944 {
4945  MOVMuxContext *mov = s->priv_data;
4946  int ret, buf_size;
4947  uint8_t *buf;
4948  int i, offset;
4949 
4950  if (!track->mdat_buf)
4951  return 0;
4952  if (!mov->mdat_buf) {
4953  if ((ret = avio_open_dyn_buf(&mov->mdat_buf)) < 0)
4954  return ret;
4955  }
4956  buf_size = avio_close_dyn_buf(track->mdat_buf, &buf);
4957  track->mdat_buf = NULL;
4958 
4959  offset = avio_tell(mov->mdat_buf);
4960  avio_write(mov->mdat_buf, buf, buf_size);
4961  av_free(buf);
4962 
4963  for (i = track->entries_flushed; i < track->entry; i++)
4964  track->cluster[i].pos += offset;
4965  track->entries_flushed = track->entry;
4966  return 0;
4967 }
4968 
4969 static int mov_flush_fragment(AVFormatContext *s, int force)
4970 {
4971  MOVMuxContext *mov = s->priv_data;
4972  int i, first_track = -1;
4973  int64_t mdat_size = 0;
4974  int ret;
4975  int has_video = 0, starts_with_key = 0, first_video_track = 1;
4976 
4977  if (!(mov->flags & FF_MOV_FLAG_FRAGMENT))
4978  return 0;
4979 
4980  // Try to fill in the duration of the last packet in each stream
4981  // from queued packets in the interleave queues. If the flushing
4982  // of fragments was triggered automatically by an AVPacket, we
4983  // already have reliable info for the end of that track, but other
4984  // tracks may need to be filled in.
4985  for (i = 0; i < s->nb_streams; i++) {
4986  MOVTrack *track = &mov->tracks[i];
4987  if (!track->end_reliable) {
4988  AVPacket pkt;
4989  if (!ff_interleaved_peek(s, i, &pkt, 1)) {
4990  if (track->dts_shift != AV_NOPTS_VALUE)
4991  pkt.dts += track->dts_shift;
4992  track->track_duration = pkt.dts - track->start_dts;
4993  if (pkt.pts != AV_NOPTS_VALUE)
4994  track->end_pts = pkt.pts;
4995  else
4996  track->end_pts = pkt.dts;
4997  }
4998  }
4999  }
5000 
5001  for (i = 0; i < mov->nb_streams; i++) {
5002  MOVTrack *track = &mov->tracks[i];
5003  if (track->entry <= 1)
5004  continue;
5005  // Sample durations are calculated as the diff of dts values,
5006  // but for the last sample in a fragment, we don't know the dts
5007  // of the first sample in the next fragment, so we have to rely
5008  // on what was set as duration in the AVPacket. Not all callers
5009  // set this though, so we might want to replace it with an
5010  // estimate if it currently is zero.
5011  if (get_cluster_duration(track, track->entry - 1) != 0)
5012  continue;
5013  // Use the duration (i.e. dts diff) of the second last sample for
5014  // the last one. This is a wild guess (and fatal if it turns out
5015  // to be too long), but probably the best we can do - having a zero
5016  // duration is bad as well.
5017  track->track_duration += get_cluster_duration(track, track->entry - 2);
5018  track->end_pts += get_cluster_duration(track, track->entry - 2);
5019  if (!mov->missing_duration_warned) {
5021  "Estimating the duration of the last packet in a "
5022  "fragment, consider setting the duration field in "
5023  "AVPacket instead.\n");
5024  mov->missing_duration_warned = 1;
5025  }
5026  }
5027 
5028  if (!mov->moov_written) {
5029  int64_t pos = avio_tell(s->pb);
5030  uint8_t *buf;
5031  int buf_size, moov_size;
5032 
5033  for (i = 0; i < mov->nb_streams; i++)
5034  if (!mov->tracks[i].entry && !is_cover_image(mov->tracks[i].st))
5035  break;
5036  /* Don't write the initial moov unless all tracks have data */
5037  if (i < mov->nb_streams && !force)
5038  return 0;
5039 
5040  moov_size = get_moov_size(s);
5041  for (i = 0; i < mov->nb_streams; i++)
5042  mov->tracks[i].data_offset = pos + moov_size + 8;
5043 
5045  if (mov->flags & FF_MOV_FLAG_DELAY_MOOV)
5047  if ((ret = mov_write_moov_tag(s->pb, mov, s)) < 0)
5048  return ret;
5049 
5050  if (mov->flags & FF_MOV_FLAG_DELAY_MOOV) {
5051  if (mov->flags & FF_MOV_FLAG_GLOBAL_SIDX)
5052  mov->reserved_header_pos = avio_tell(s->pb);
5053  avio_flush(s->pb);
5054  mov->moov_written = 1;
5055  return 0;
5056  }
5057 
5058  buf_size = avio_close_dyn_buf(mov->mdat_buf, &buf);
5059  mov->mdat_buf = NULL;
5060  avio_wb32(s->pb, buf_size + 8);
5061  ffio_wfourcc(s->pb, "mdat");
5062  avio_write(s->pb, buf, buf_size);
5063  av_free(buf);
5064 
5065  if (mov->flags & FF_MOV_FLAG_GLOBAL_SIDX)
5066  mov->reserved_header_pos = avio_tell(s->pb);
5067 
5068  mov->moov_written = 1;
5069  mov->mdat_size = 0;
5070  for (i = 0; i < mov->nb_streams; i++) {
5071  if (mov->tracks[i].entry)
5072  mov->tracks[i].frag_start += mov->tracks[i].start_dts +
5073  mov->tracks[i].track_duration -
5074  mov->tracks[i].cluster[0].dts;
5075  mov->tracks[i].entry = 0;
5076  mov->tracks[i].end_reliable = 0;
5077  }
5078  avio_flush(s->pb);
5079  return 0;
5080  }
5081 
5082  if (mov->frag_interleave) {
5083  for (i = 0; i < mov->nb_streams; i++) {
5084  MOVTrack *track = &mov->tracks[i];
5085  int ret;
5086  if ((ret = mov_flush_fragment_interleaving(s, track)) < 0)
5087  return ret;
5088  }
5089 
5090  if (!mov->mdat_buf)
5091  return 0;
5092  mdat_size = avio_tell(mov->mdat_buf);
5093  }
5094 
5095  for (i = 0; i < mov->nb_streams; i++) {
5096  MOVTrack *track = &mov->tracks[i];
5097  if (mov->flags & FF_MOV_FLAG_SEPARATE_MOOF || mov->frag_interleave)
5098  track->data_offset = 0;
5099  else
5100  track->data_offset = mdat_size;
5101  if (track->par->codec_type == AVMEDIA_TYPE_VIDEO) {
5102  has_video = 1;
5103  if (first_video_track) {
5104  if (track->entry)
5105  starts_with_key = track->cluster[0].flags & MOV_SYNC_SAMPLE;
5106  first_video_track = 0;
5107  }
5108  }
5109  if (!track->entry)
5110  continue;
5111  if (track->mdat_buf)
5112  mdat_size += avio_tell(track->mdat_buf);
5113  if (first_track < 0)
5114  first_track = i;
5115  }
5116 
5117  if (!mdat_size)
5118  return 0;
5119 
5120  avio_write_marker(s->pb,
5121  av_rescale(mov->tracks[first_track].cluster[0].dts, AV_TIME_BASE, mov->tracks[first_track].timescale),
5122  (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);
5123 
5124  for (i = 0; i < mov->nb_streams; i++) {
5125  MOVTrack *track = &mov->tracks[i];
5126  int buf_size, write_moof = 1, moof_tracks = -1;
5127  uint8_t *buf;
5128  int64_t duration = 0;
5129 
5130  if (track->entry)
5131  duration = track->start_dts + track->track_duration -
5132  track->cluster[0].dts;
5133  if (mov->flags & FF_MOV_FLAG_SEPARATE_MOOF) {
5134  if (!track->mdat_buf)
5135  continue;
5136  mdat_size = avio_tell(track->mdat_buf);
5137  moof_tracks = i;
5138  } else {
5139  write_moof = i == first_track;
5140  }
5141 
5142  if (write_moof) {
5143  avio_flush(s->pb);
5144 
5145  mov_write_moof_tag(s->pb, mov, moof_tracks, mdat_size);
5146  mov->fragments++;
5147 
5148  avio_wb32(s->pb, mdat_size + 8);
5149  ffio_wfourcc(s->pb, "mdat");
5150  }
5151 
5152  if (track->entry)
5153  track->frag_start += duration;
5154  track->entry = 0;
5155  track->entries_flushed = 0;
5156  track->end_reliable = 0;
5157  if (!mov->frag_interleave) {
5158  if (!track->mdat_buf)
5159  continue;
5160  buf_size = avio_close_dyn_buf(track->mdat_buf, &buf);
5161  track->mdat_buf = NULL;
5162  } else {
5163  if (!mov->mdat_buf)
5164  continue;
5165  buf_size = avio_close_dyn_buf(mov->mdat_buf, &buf);
5166  mov->mdat_buf = NULL;
5167  }
5168 
5169  avio_write(s->pb, buf, buf_size);
5170  av_free(buf);
5171  }
5172 
5173  mov->mdat_size = 0;
5174 
5175  avio_flush(s->pb);
5176  return 0;
5177 }
5178 
5180 {
5181  MOVMuxContext *mov = s->priv_data;
5182  int had_moov = mov->moov_written;
5183  int ret = mov_flush_fragment(s, force);
5184  if (ret < 0)
5185  return ret;
5186  // If using delay_moov, the first flush only wrote the moov,
5187  // not the actual moof+mdat pair, thus flush once again.
5188  if (!had_moov && mov->flags & FF_MOV_FLAG_DELAY_MOOV)
5189  ret = mov_flush_fragment(s, force);
5190  return ret;
5191 }
5192 
5194 {
5195  MOVMuxContext *mov = s->priv_data;
5196  MOVTrack *trk = &mov->tracks[pkt->stream_index];
5197  int64_t ref;
5198  uint64_t duration;
5199 
5200  if (trk->entry) {
5201  ref = trk->cluster[trk->entry - 1].dts;
5202  } else if ( trk->start_dts != AV_NOPTS_VALUE
5203  && !trk->frag_discont) {
5204  ref = trk->start_dts + trk->track_duration;
5205  } else
5206  ref = pkt->dts; // Skip tests for the first packet
5207 
5208  if (trk->dts_shift != AV_NOPTS_VALUE) {
5209  /* With negative CTS offsets we have set an offset to the DTS,
5210  * reverse this for the check. */
5211  ref -= trk->dts_shift;
5212  }
5213 
5214  duration = pkt->dts - ref;
5215  if (pkt->dts < ref || duration >= INT_MAX) {
5216  av_log(s, AV_LOG_ERROR, "Application provided duration: %"PRId64" / timestamp: %"PRId64" is out of range for mov/mp4 format\n",
5217  duration, pkt->dts
5218  );
5219 
5220  pkt->dts = ref + 1;
5221  pkt->pts = AV_NOPTS_VALUE;
5222  }
5223 
5224  if (pkt->duration < 0 || pkt->duration > INT_MAX) {
5225  av_log(s, AV_LOG_ERROR, "Application provided duration: %"PRId64" is invalid\n", pkt->duration);
5226  return AVERROR(EINVAL);
5227  }
5228  return 0;
5229 }
5230 
5232 {
5233  MOVMuxContext *mov = s->priv_data;
5234  AVIOContext *pb = s->pb;
5235  MOVTrack *trk = &mov->tracks[pkt->stream_index];
5236  AVCodecParameters *par = trk->par;
5237  unsigned int samples_in_chunk = 0;
5238  int size = pkt->size, ret = 0;
5239  uint8_t *reformatted_data = NULL;
5240 
5241  ret = check_pkt(s, pkt);
5242  if (ret < 0)
5243  return ret;
5244 
5245  if (mov->flags & FF_MOV_FLAG_FRAGMENT) {
5246  int ret;
5247  if (mov->moov_written || mov->flags & FF_MOV_FLAG_EMPTY_MOOV) {
5248  if (mov->frag_interleave && mov->fragments > 0) {
5249  if (trk->entry - trk->entries_flushed >= mov->frag_interleave) {
5250  if ((ret = mov_flush_fragment_interleaving(s, trk)) < 0)
5251  return ret;
5252  }
5253  }
5254 
5255  if (!trk->mdat_buf) {
5256  if ((ret = avio_open_dyn_buf(&trk->mdat_buf)) < 0)
5257  return ret;
5258  }
5259  pb = trk->mdat_buf;
5260  } else {
5261  if (!mov->mdat_buf) {
5262  if ((ret = avio_open_dyn_buf(&mov->mdat_buf)) < 0)
5263  return ret;
5264  }
5265  pb = mov->mdat_buf;
5266  }
5267  }
5268 
5269  if (par->codec_id == AV_CODEC_ID_AMR_NB) {
5270  /* We must find out how many AMR blocks there are in one packet */
5271  static const uint16_t packed_size[16] =
5272  {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 1};
5273  int len = 0;
5274 
5275  while (len < size && samples_in_chunk < 100) {
5276  len += packed_size[(pkt->data[len] >> 3) & 0x0F];
5277  samples_in_chunk++;
5278  }
5279  if (samples_in_chunk > 1) {
5280  av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, implement a AVParser for it\n");
5281  return -1;
5282  }
5283  } else if (par->codec_id == AV_CODEC_ID_ADPCM_MS ||
5285  samples_in_chunk = trk->par->frame_size;
5286  } else if (trk->sample_size)
5287  samples_in_chunk = size / trk->sample_size;
5288  else
5289  samples_in_chunk = 1;
5290 
5291  if (samples_in_chunk < 1) {
5292  av_log(s, AV_LOG_ERROR, "fatal error, input packet contains no samples\n");
5293  return AVERROR_PATCHWELCOME;
5294  }
5295 
5296  /* copy extradata if it exists */
5297  if (trk->vos_len == 0 && par->extradata_size > 0 &&
5298  !TAG_IS_AVCI(trk->tag) &&
5299  (par->codec_id != AV_CODEC_ID_DNXHD)) {
5300  trk->vos_len = par->extradata_size;
5301  trk->vos_data = av_malloc(trk->vos_len);
5302  if (!trk->vos_data) {
5303  ret = AVERROR(ENOMEM);
5304  goto err;
5305  }
5306  memcpy(trk->vos_data, par->extradata, trk->vos_len);
5307  }
5308 
5309  if (par->codec_id == AV_CODEC_ID_AAC && pkt->size > 2 &&
5310  (AV_RB16(pkt->data) & 0xfff0) == 0xfff0) {
5311  if (!s->streams[pkt->stream_index]->nb_frames) {
5312  av_log(s, AV_LOG_ERROR, "Malformed AAC bitstream detected: "
5313  "use the audio bitstream filter 'aac_adtstoasc' to fix it "
5314  "('-bsf:a aac_adtstoasc' option with ffmpeg)\n");
5315  return -1;
5316  }
5317  av_log(s, AV_LOG_WARNING, "aac bitstream error\n");
5318  }
5319  if (par->codec_id == AV_CODEC_ID_H264 && trk->vos_len > 0 && *(uint8_t *)trk->vos_data != 1 && !TAG_IS_AVCI(trk->tag)) {
5320  /* from x264 or from bytestream H.264 */
5321  /* NAL reformatting needed */
5322  if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams) {
5323  ff_avc_parse_nal_units_buf(pkt->data, &reformatted_data,
5324  &size);
5325  avio_write(pb, reformatted_data, size);
5326  } else {
5327  if (trk->cenc.aes_ctr) {
5328  size = ff_mov_cenc_avc_parse_nal_units(&trk->cenc, pb, pkt->data, size);
5329  if (size < 0) {
5330  ret = size;
5331  goto err;
5332  }
5333  } else {
5334  size = ff_avc_parse_nal_units(pb, pkt->data, pkt->size);
5335  }
5336  }
5337  } else if (par->codec_id == AV_CODEC_ID_HEVC && trk->vos_len > 6 &&
5338  (AV_RB24(trk->vos_data) == 1 || AV_RB32(trk->vos_data) == 1)) {
5339  /* extradata is Annex B, assume the bitstream is too and convert it */
5340  if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams) {
5341  ff_hevc_annexb2mp4_buf(pkt->data, &reformatted_data, &size, 0, NULL);
5342  avio_write(pb, reformatted_data, size);
5343  } else {
5344  size = ff_hevc_annexb2mp4(pb, pkt->data, pkt->size, 0, NULL);
5345  }
5346  } else if (par->codec_id == AV_CODEC_ID_AV1) {
5347  if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams) {
5348  ff_av1_filter_obus_buf(pkt->data, &reformatted_data, &size);
5349  avio_write(pb, reformatted_data, size);
5350  } else {
5351  size = ff_av1_filter_obus(pb, pkt->data, pkt->size);
5352  }
5353 #if CONFIG_AC3_PARSER
5354  } else if (par->codec_id == AV_CODEC_ID_EAC3) {
5355  size = handle_eac3(mov, pkt, trk);
5356  if (size < 0)
5357  return size;
5358  else if (!size)
5359  goto end;
5360  avio_write(pb, pkt->data, size);
5361 #endif
5362  } else {
5363  if (trk->cenc.aes_ctr) {
5364  if (par->codec_id == AV_CODEC_ID_H264 && par->extradata_size > 4) {
5365  int nal_size_length = (par->extradata[4] & 0x3) + 1;
5366  ret = ff_mov_cenc_avc_write_nal_units(s, &trk->cenc, nal_size_length, pb, pkt->data, size);
5367  } else {
5368  ret = ff_mov_cenc_write_packet(&trk->cenc, pb, pkt->data, size);
5369  }
5370 
5371  if (ret) {
5372  goto err;
5373  }
5374  } else {
5375  avio_write(pb, pkt->data, size);
5376  }
5377  }
5378 
5379  if ((par->codec_id == AV_CODEC_ID_DNXHD ||
5380  par->codec_id == AV_CODEC_ID_AC3) && !trk->vos_len) {
5381  /* copy frame to create needed atoms */
5382  trk->vos_len = size;
5383  trk->vos_data = av_malloc(size);
5384  if (!trk->vos_data) {
5385  ret = AVERROR(ENOMEM);
5386  goto err;
5387  }
5388  memcpy(trk->vos_data, pkt->data, size);
5389  }
5390 
5391  if (trk->entry >= trk->cluster_capacity) {
5392  unsigned new_capacity = 2 * (trk->entry + MOV_INDEX_CLUSTER_SIZE);
5393  if (av_reallocp_array(&trk->cluster, new_capacity,
5394  sizeof(*trk->cluster))) {
5395  ret = AVERROR(ENOMEM);
5396  goto err;
5397  }
5398  trk->cluster_capacity = new_capacity;
5399  }
5400 
5401  trk->cluster[trk->entry].pos = avio_tell(pb) - size;
5402  trk->cluster[trk->entry].samples_in_chunk = samples_in_chunk;
5403  trk->cluster[trk->entry].chunkNum = 0;
5404  trk->cluster[trk->entry].size = size;
5405  trk->cluster[trk->entry].entries = samples_in_chunk;
5406  trk->cluster[trk->entry].dts = pkt->dts;
5407  trk->cluster[trk->entry].pts = pkt->pts;
5408  if (!trk->entry && trk->start_dts != AV_NOPTS_VALUE) {
5409  if (!trk->frag_discont) {
5410  /* First packet of a new fragment. We already wrote the duration
5411  * of the last packet of the previous fragment based on track_duration,
5412  * which might not exactly match our dts. Therefore adjust the dts
5413  * of this packet to be what the previous packets duration implies. */
5414  trk->cluster[trk->entry].dts = trk->start_dts + trk->track_duration;
5415  /* We also may have written the pts and the corresponding duration
5416  * in sidx/tfrf/tfxd tags; make sure the sidx pts and duration match up with
5417  * the next fragment. This means the cts of the first sample must
5418  * be the same in all fragments, unless end_pts was updated by
5419  * the packet causing the fragment to be written. */
5420  if ((mov->flags & FF_MOV_FLAG_DASH && !(mov->flags & FF_MOV_FLAG_GLOBAL_SIDX)) ||
5421  mov->mode == MODE_ISM)
5422  pkt->pts = pkt->dts + trk->end_pts - trk->cluster[trk->entry].dts;
5423  } else {
5424  /* New fragment, but discontinuous from previous fragments.
5425  * Pretend the duration sum of the earlier fragments is
5426  * pkt->dts - trk->start_dts. */
5427  trk->frag_start = pkt->dts - trk->start_dts;
5428  trk->end_pts = AV_NOPTS_VALUE;
5429  trk->frag_discont = 0;
5430  }
5431  }
5432 
5433  if (!trk->entry && trk->start_dts == AV_NOPTS_VALUE && !mov->use_editlist &&
5435  /* Not using edit lists and shifting the first track to start from zero.
5436  * If the other streams start from a later timestamp, we won't be able
5437  * to signal the difference in starting time without an edit list.
5438  * Thus move the timestamp for this first sample to 0, increasing
5439  * its duration instead. */
5440  trk->cluster[trk->entry].dts = trk->start_dts = 0;
5441  }
5442  if (trk->start_dts == AV_NOPTS_VALUE) {
5443  trk->start_dts = pkt->dts;
5444  if (trk->frag_discont) {
5445  if (mov->use_editlist) {
5446  /* Pretend the whole stream started at pts=0, with earlier fragments
5447  * already written. If the stream started at pts=0, the duration sum
5448  * of earlier fragments would have been pkt->pts. */
5449  trk->frag_start = pkt->pts;
5450  trk->start_dts = pkt->dts - pkt->pts;
5451  } else {
5452  /* Pretend the whole stream started at dts=0, with earlier fragments
5453  * already written, with a duration summing up to pkt->dts. */
5454  trk->frag_start = pkt->dts;
5455  trk->start_dts = 0;
5456  }
5457  trk->frag_discont = 0;
5458  } else if (pkt->dts && mov->moov_written)
5460  "Track %d starts with a nonzero dts %"PRId64", while the moov "
5461  "already has been written. Set the delay_moov flag to handle "
5462  "this case.\n",
5463  pkt->stream_index, pkt->dts);
5464  }
5465  trk->track_duration = pkt->dts - trk->start_dts + pkt->duration;
5466  trk->last_sample_is_subtitle_end = 0;
5467 
5468  if (pkt->pts == AV_NOPTS_VALUE) {
5469  av_log(s, AV_LOG_WARNING, "pts has no value\n");
5470  pkt->pts = pkt->dts;
5471  }
5472  if (pkt->dts != pkt->pts)
5473  trk->flags |= MOV_TRACK_CTTS;
5474  trk->cluster[trk->entry].cts = pkt->pts - pkt->dts;
5475  trk->cluster[trk->entry].flags = 0;
5476  if (trk->start_cts == AV_NOPTS_VALUE)
5477  trk->start_cts = pkt->pts - pkt->dts;
5478  if (trk->end_pts == AV_NOPTS_VALUE)
5479  trk->end_pts = trk->cluster[trk->entry].dts +
5480  trk->cluster[trk->entry].cts + pkt->duration;
5481  else
5482  trk->end_pts = FFMAX(trk->end_pts, trk->cluster[trk->entry].dts +
5483  trk->cluster[trk->entry].cts +
5484  pkt->duration);
5485 
5486  if (par->codec_id == AV_CODEC_ID_VC1) {
5487  mov_parse_vc1_frame(pkt, trk);
5488  } else if (pkt->flags & AV_PKT_FLAG_KEY) {
5489  if (mov->mode == MODE_MOV && par->codec_id == AV_CODEC_ID_MPEG2VIDEO &&
5490  trk->entry > 0) { // force sync sample for the first key frame
5491  mov_parse_mpeg2_frame(pkt, &trk->cluster[trk->entry].flags);
5492  if (trk->cluster[trk->entry].flags & MOV_PARTIAL_SYNC_SAMPLE)
5493  trk->flags |= MOV_TRACK_STPS;
5494  } else {
5495  trk->cluster[trk->entry].flags = MOV_SYNC_SAMPLE;
5496  }
5497  if (trk->cluster[trk->entry].flags & MOV_SYNC_SAMPLE)
5498  trk->has_keyframes++;
5499  }
5500  if (pkt->flags & AV_PKT_FLAG_DISPOSABLE) {
5501  trk->cluster[trk->entry].flags |= MOV_DISPOSABLE_SAMPLE;
5502  trk->has_disposable++;
5503  }
5504  trk->entry++;
5505  trk->sample_count += samples_in_chunk;
5506  mov->mdat_size += size;
5507 
5508  if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams)
5509  ff_mov_add_hinted_packet(s, pkt, trk->hint_track, trk->entry,
5510  reformatted_data, size);
5511 
5512 end:
5513 err:
5514 
5515  av_free(reformatted_data);
5516  return ret;
5517 }
5518 
5520 {
5521  MOVMuxContext *mov = s->priv_data;
5522  MOVTrack *trk = &mov->tracks[pkt->stream_index];
5523  AVCodecParameters *par = trk->par;
5524  int64_t frag_duration = 0;
5525  int size = pkt->size;
5526 
5527  int ret = check_pkt(s, pkt);
5528  if (ret < 0)
5529  return ret;
5530 
5531  if (mov->flags & FF_MOV_FLAG_FRAG_DISCONT) {
5532  int i;
5533  for (i = 0; i < s->nb_streams; i++)
5534  mov->tracks[i].frag_discont = 1;
5536  }
5537 
5539  if (trk->dts_shift == AV_NOPTS_VALUE)
5540  trk->dts_shift = pkt->pts - pkt->dts;
5541  pkt->dts += trk->dts_shift;
5542  }
5543 
5544  if (trk->par->codec_id == AV_CODEC_ID_MP4ALS ||
5545  trk->par->codec_id == AV_CODEC_ID_AAC ||
5546  trk->par->codec_id == AV_CODEC_ID_AV1 ||
5547  trk->par->codec_id == AV_CODEC_ID_FLAC) {
5548  int side_size = 0;
5549  uint8_t *side = av_packet_get_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA, &side_size);
5550  if (side && side_size > 0 && (side_size != par->extradata_size || memcmp(side, par->extradata, side_size))) {
5551  void *newextra = av_mallocz(side_size + AV_INPUT_BUFFER_PADDING_SIZE);
5552  if (!newextra)
5553  return AVERROR(ENOMEM);
5554  av_free(par->extradata);
5555  par->extradata = newextra;
5556  memcpy(par->extradata, side, side_size);
5557  par->extradata_size = side_size;
5558  if (!pkt->size) // Flush packet
5559  mov->need_rewrite_extradata = 1;
5560  }
5561  }
5562 
5563  if (!pkt->size) {
5564  if (trk->start_dts == AV_NOPTS_VALUE && trk->frag_discont) {
5565  trk->start_dts = pkt->dts;
5566  if (pkt->pts != AV_NOPTS_VALUE)
5567  trk->start_cts = pkt->pts - pkt->dts;
5568  else
5569  trk->start_cts = 0;
5570  }
5571 
5572  return 0; /* Discard 0 sized packets */
5573  }
5574 
5575  if (trk->entry && pkt->stream_index < s->nb_streams)
5576  frag_duration = av_rescale_q(pkt->dts - trk->cluster[0].dts,
5577  s->streams[pkt->stream_index]->time_base,
5578  AV_TIME_BASE_Q);
5579  if ((mov->max_fragment_duration &&
5580  frag_duration >= mov->max_fragment_duration) ||
5581  (mov->max_fragment_size && mov->mdat_size + size >= mov->max_fragment_size) ||
5582  (mov->flags & FF_MOV_FLAG_FRAG_KEYFRAME &&
5583  par->codec_type == AVMEDIA_TYPE_VIDEO &&
5584  trk->entry && pkt->flags & AV_PKT_FLAG_KEY) ||
5586  if (frag_duration >= mov->min_fragment_duration) {
5587  // Set the duration of this track to line up with the next
5588  // sample in this track. This avoids relying on AVPacket
5589  // duration, but only helps for this particular track, not
5590  // for the other ones that are flushed at the same time.
5591  trk->track_duration = pkt->dts - trk->start_dts;
5592  if (pkt->pts != AV_NOPTS_VALUE)
5593  trk->end_pts = pkt->pts;
5594  else
5595  trk->end_pts = pkt->dts;
5596  trk->end_reliable = 1;
5598  }
5599  }
5600 
5601  return ff_mov_write_packet(s, pkt);
5602 }
5603 
5605  int stream_index,
5606  int64_t dts) {
5607  AVPacket end;
5608  uint8_t data[2] = {0};
5609  int ret;
5610 
5611  av_init_packet(&end);
5612  end.size = sizeof(data);
5613  end.data = data;
5614  end.pts = dts;
5615  end.dts = dts;
5616  end.duration = 0;
5617  end.stream_index = stream_index;
5618 
5619  ret = mov_write_single_packet(s, &end);
5620  av_packet_unref(&end);
5621 
5622  return ret;
5623 }
5624 
5626 {
5627  MOVMuxContext *mov = s->priv_data;
5628  MOVTrack *trk;
5629 
5630  if (!pkt) {
5631  mov_flush_fragment(s, 1);
5632  return 1;
5633  }
5634 
5635  trk = &mov->tracks[pkt->stream_index];
5636 
5637  if (is_cover_image(trk->st)) {
5638  int ret;
5639 
5640  if (trk->st->nb_frames >= 1) {
5641  if (trk->st->nb_frames == 1)
5642  av_log(s, AV_LOG_WARNING, "Got more than one picture in stream %d,"
5643  " ignoring.\n", pkt->stream_index);
5644  return 0;
5645  }
5646 
5647  if ((ret = av_packet_ref(&trk->cover_image, pkt)) < 0)
5648  return ret;
5649 
5650  return 0;
5651  } else {
5652  int i;
5653 
5654  if (!pkt->size)
5655  return mov_write_single_packet(s, pkt); /* Passthrough. */
5656 
5657  /*
5658  * Subtitles require special handling.
5659  *
5660  * 1) For full complaince, every track must have a sample at
5661  * dts == 0, which is rarely true for subtitles. So, as soon
5662  * as we see any packet with dts > 0, write an empty subtitle
5663  * at dts == 0 for any subtitle track with no samples in it.
5664  *
5665  * 2) For each subtitle track, check if the current packet's
5666  * dts is past the duration of the last subtitle sample. If
5667  * so, we now need to write an end sample for that subtitle.
5668  *
5669  * This must be done conditionally to allow for subtitles that
5670  * immediately replace each other, in which case an end sample
5671  * is not needed, and is, in fact, actively harmful.
5672  *
5673  * 3) See mov_write_trailer for how the final end sample is
5674  * handled.
5675  */
5676  for (i = 0; i < mov->nb_streams; i++) {
5677  MOVTrack *trk = &mov->tracks[i];
5678  int ret;
5679 
5680  if (trk->par->codec_id == AV_CODEC_ID_MOV_TEXT &&
5681  trk->track_duration < pkt->dts &&
5682  (trk->entry == 0 || !trk->last_sample_is_subtitle_end)) {
5684  if (ret < 0) return ret;
5685  trk->last_sample_is_subtitle_end = 1;
5686  }
5687  }
5688 
5689  if (trk->mode == MODE_MOV && trk->par->codec_type == AVMEDIA_TYPE_VIDEO) {
5690  AVPacket *opkt = pkt;
5691  int reshuffle_ret, ret;
5692  if (trk->is_unaligned_qt_rgb) {
5693  int64_t bpc = trk->par->bits_per_coded_sample != 15 ? trk->par->bits_per_coded_sample : 16;
5694  int expected_stride = ((trk->par->width * bpc + 15) >> 4)*2;
5695  reshuffle_ret = ff_reshuffle_raw_rgb(s, &pkt, trk->par, expected_stride);
5696  if (reshuffle_ret < 0)
5697  return reshuffle_ret;
5698  } else
5699  reshuffle_ret = 0;
5700  if (trk->par->format == AV_PIX_FMT_PAL8 && !trk->pal_done) {
5701  ret = ff_get_packet_palette(s, opkt, reshuffle_ret, trk->palette);
5702  if (ret < 0)
5703  goto fail;
5704  if (ret)
5705  trk->pal_done++;
5706  } else if (trk->par->codec_id == AV_CODEC_ID_RAWVIDEO &&
5707  (trk->par->format == AV_PIX_FMT_GRAY8 ||
5708  trk->par->format == AV_PIX_FMT_MONOBLACK)) {
5709  for (i = 0; i < pkt->size; i++)
5710  pkt->data[i] = ~pkt->data[i];
5711  }
5712  if (reshuffle_ret) {
5713  ret = mov_write_single_packet(s, pkt);
5714 fail:
5715  if (reshuffle_ret)
5716  av_packet_free(&pkt);
5717  return ret;
5718  }
5719  }
5720 
5721  return mov_write_single_packet(s, pkt);
5722  }
5723 }
5724 
5725 // QuickTime chapters involve an additional text track with the chapter names
5726 // as samples, and a tref pointing from the other tracks to the chapter one.
5727 static int mov_create_chapter_track(AVFormatContext *s, int tracknum)
5728 {
5729  AVIOContext *pb;
5730 
5731  MOVMuxContext *mov = s->priv_data;
5732  MOVTrack *track = &mov->tracks[tracknum];
5733  AVPacket pkt = { .stream_index = tracknum, .flags = AV_PKT_FLAG_KEY };
5734  int i, len;
5735 
5736  track->mode = mov->mode;
5737  track->tag = MKTAG('t','e','x','t');
5738  track->timescale = MOV_TIMESCALE;
5739  track->par = avcodec_parameters_alloc();
5740  if (!track->par)
5741  return AVERROR(ENOMEM);
5743 #if 0
5744  // These properties are required to make QT recognize the chapter track
5745  uint8_t chapter_properties[43] = { 0, 0, 0, 0, 0, 0, 0, 1, };
5746  if (ff_alloc_extradata(track->par, sizeof(chapter_properties)))
5747  return AVERROR(ENOMEM);
5748  memcpy(track->par->extradata, chapter_properties, sizeof(chapter_properties));
5749 #else
5750  if (avio_open_dyn_buf(&pb) >= 0) {
5751  int size;
5752  uint8_t *buf;
5753 
5754  /* Stub header (usually for Quicktime chapter track) */
5755  // TextSampleEntry
5756  avio_wb32(pb, 0x01); // displayFlags
5757  avio_w8(pb, 0x00); // horizontal justification
5758  avio_w8(pb, 0x00); // vertical justification
5759  avio_w8(pb, 0x00); // bgColourRed
5760  avio_w8(pb, 0x00); // bgColourGreen
5761  avio_w8(pb, 0x00); // bgColourBlue
5762  avio_w8(pb, 0x00); // bgColourAlpha
5763  // BoxRecord
5764  avio_wb16(pb, 0x00); // defTextBoxTop
5765  avio_wb16(pb, 0x00); // defTextBoxLeft
5766  avio_wb16(pb, 0x00); // defTextBoxBottom
5767  avio_wb16(pb, 0x00); // defTextBoxRight
5768  // StyleRecord
5769  avio_wb16(pb, 0x00); // startChar
5770  avio_wb16(pb, 0x00); // endChar
5771  avio_wb16(pb, 0x01); // fontID
5772  avio_w8(pb, 0x00); // fontStyleFlags
5773  avio_w8(pb, 0x00); // fontSize
5774  avio_w8(pb, 0x00); // fgColourRed
5775  avio_w8(pb, 0x00); // fgColourGreen
5776  avio_w8(pb, 0x00); // fgColourBlue
5777  avio_w8(pb, 0x00); // fgColourAlpha
5778  // FontTableBox
5779  avio_wb32(pb, 0x0D); // box size
5780  ffio_wfourcc(pb, "ftab"); // box atom name
5781  avio_wb16(pb, 0x01); // entry count
5782  // FontRecord
5783  avio_wb16(pb, 0x01); // font ID
5784  avio_w8(pb, 0x00); // font name length
5785 
5786  if ((size = avio_close_dyn_buf(pb, &buf)) > 0) {
5787  track->par->extradata = buf;
5788  track->par->extradata_size = size;
5789  } else {
5790  av_freep(&buf);
5791  }
5792  }
5793 #endif
5794 
5795  for (i = 0; i < s->nb_chapters; i++) {
5796  AVChapter *c = s->chapters[i];
5797  AVDictionaryEntry *t;
5798 
5799  int64_t end = av_rescale_q(c->end, c->time_base, (AVRational){1,MOV_TIMESCALE});
5800  pkt.pts = pkt.dts = av_rescale_q(c->start, c->time_base, (AVRational){1,MOV_TIMESCALE});
5801  pkt.duration = end - pkt.dts;
5802 
5803  if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
5804  static const char encd[12] = {
5805  0x00, 0x00, 0x00, 0x0C,
5806  'e', 'n', 'c', 'd',
5807  0x00, 0x00, 0x01, 0x00 };
5808  len = strlen(t->value);
5809  pkt.size = len + 2 + 12;
5810  pkt.data = av_malloc(pkt.size);
5811  if (!pkt.data)
5812  return AVERROR(ENOMEM);
5813  AV_WB16(pkt.data, len);
5814  memcpy(pkt.data + 2, t->value, len);
5815  memcpy(pkt.data + len + 2, encd, sizeof(encd));
5816  ff_mov_write_packet(s, &pkt);
5817  av_freep(&pkt.data);
5818  }
5819  }
5820 
5821  return 0;
5822 }
5823 
5824 
5825 static int mov_check_timecode_track(AVFormatContext *s, AVTimecode *tc, int src_index, const char *tcstr)
5826 {
5827  int ret;
5828 
5829  /* compute the frame number */
5830  ret = av_timecode_init_from_string(tc, find_fps(s, s->streams[src_index]), tcstr, s);
5831  return ret;
5832 }
5833 
5835 {
5836  int ret;
5837  MOVMuxContext *mov = s->priv_data;
5838  MOVTrack *track = &mov->tracks[index];
5839  AVStream *src_st = s->streams[src_index];
5840  AVPacket pkt = {.stream_index = index, .flags = AV_PKT_FLAG_KEY, .size = 4};
5841  AVRational rate = find_fps(s, src_st);
5842 
5843  /* tmcd track based on video stream */
5844  track->mode = mov->mode;
5845  track->tag = MKTAG('t','m','c','d');
5846  track->src_track = src_index;
5847  track->timescale = mov->tracks[src_index].timescale;
5850 
5851  /* set st to src_st for metadata access*/
5852  track->st = src_st;
5853 
5854  /* encode context: tmcd data stream */
5855  track->par = avcodec_parameters_alloc();
5856  if (!track->par)
5857  return AVERROR(ENOMEM);
5858  track->par->codec_type = AVMEDIA_TYPE_DATA;
5859  track->par->codec_tag = track->tag;
5860  track->st->avg_frame_rate = av_inv_q(rate);
5861 
5862  /* the tmcd track just contains one packet with the frame number */
5863  pkt.data = av_malloc(pkt.size);
5864  if (!pkt.data)
5865  return AVERROR(ENOMEM);
5866  AV_WB32(pkt.data, tc.start);
5867  ret = ff_mov_write_packet(s, &pkt);
5868  av_free(pkt.data);
5869  return ret;
5870 }
5871 
5872 /*
5873  * st->disposition controls the "enabled" flag in the tkhd tag.
5874  * QuickTime will not play a track if it is not enabled. So make sure
5875  * that one track of each type (audio, video, subtitle) is enabled.
5876  *
5877  * Subtitles are special. For audio and video, setting "enabled" also
5878  * makes the track "default" (i.e. it is rendered when played). For
5879  * subtitles, an "enabled" subtitle is not rendered by default, but
5880  * if no subtitle is enabled, the subtitle menu in QuickTime will be
5881  * empty!
5882  */
5884 {
5885  MOVMuxContext *mov = s->priv_data;
5886  int i;
5887  int enabled[AVMEDIA_TYPE_NB];
5888  int first[AVMEDIA_TYPE_NB];
5889 
5890  for (i = 0; i < AVMEDIA_TYPE_NB; i++) {
5891  enabled[i] = 0;
5892  first[i] = -1;
5893  }
5894 
5895  for (i = 0; i < s->nb_streams; i++) {
5896  AVStream *st = s->streams[i];
5897 
5899  st->codecpar->codec_type >= AVMEDIA_TYPE_NB ||
5900  is_cover_image(st))
5901  continue;
5902 
5903  if (first[st->codecpar->codec_type] < 0)
5904  first[st->codecpar->codec_type] = i;
5905  if (st->disposition & AV_DISPOSITION_DEFAULT) {
5906  mov->tracks[i].flags |= MOV_TRACK_ENABLED;
5907  enabled[st->codecpar->codec_type]++;
5908  }
5909  }
5910 
5911  for (i = 0; i < AVMEDIA_TYPE_NB; i++) {
5912  switch (i) {
5913  case AVMEDIA_TYPE_VIDEO:
5914  case AVMEDIA_TYPE_AUDIO:
5915  case AVMEDIA_TYPE_SUBTITLE:
5916  if (enabled[i] > 1)
5917  mov->per_stream_grouping = 1;
5918  if (!enabled[i] && first[i] >= 0)
5919  mov->tracks[first[i]].flags |= MOV_TRACK_ENABLED;
5920  break;
5921  }
5922  }
5923 }
5924 
5926 {
5927  MOVMuxContext *mov = s->priv_data;
5928  int i;
5929 
5930  if (mov->chapter_track) {
5931  if (mov->tracks[mov->chapter_track].par)
5932  av_freep(&mov->tracks[mov->chapter_track].par->extradata);
5933  av_freep(&mov->tracks[mov->chapter_track].par);
5934  }
5935 
5936  for (i = 0; i < mov->nb_streams; i++) {
5937  if (mov->tracks[i].tag == MKTAG('r','t','p',' '))
5938  ff_mov_close_hinting(&mov->tracks[i]);
5939  else if (mov->tracks[i].tag == MKTAG('t','m','c','d') && mov->nb_meta_tmcd)
5940  av_freep(&mov->tracks[i].par);
5941  av_freep(&mov->tracks[i].cluster);
5942  av_freep(&mov->tracks[i].frag_info);
5944 
5945  if (mov->tracks[i].eac3_priv) {
5946  struct eac3_info *info = mov->tracks[i].eac3_priv;
5947  av_packet_unref(&info->pkt);
5948  av_freep(&mov->tracks[i].eac3_priv);
5949  }
5950  if (mov->tracks[i].vos_len)
5951  av_freep(&mov->tracks[i].vos_data);
5952 
5953  ff_mov_cenc_free(&mov->tracks[i].cenc);
5954  }
5955 
5956  av_freep(&mov->tracks);
5957 }
5958 
5959 static uint32_t rgb_to_yuv(uint32_t rgb)
5960 {
5961  uint8_t r, g, b;
5962  int y, cb, cr;
5963 
5964  r = (rgb >> 16) & 0xFF;
5965  g = (rgb >> 8) & 0xFF;
5966  b = (rgb ) & 0xFF;
5967 
5968  y = av_clip_uint8(( 16000 + 257 * r + 504 * g + 98 * b)/1000);
5969  cb = av_clip_uint8((128000 - 148 * r - 291 * g + 439 * b)/1000);
5970  cr = av_clip_uint8((128000 + 439 * r - 368 * g - 71 * b)/1000);
5971 
5972  return (y << 16) | (cr << 8) | cb;
5973 }
5974 
5976  AVStream *st)
5977 {
5978  int i, width = 720, height = 480;
5979  int have_palette = 0, have_size = 0;
5980  uint32_t palette[16];
5981  char *cur = st->codecpar->extradata;
5982 
5983  while (cur && *cur) {
5984  if (strncmp("palette:", cur, 8) == 0) {
5985  int i, count;
5986  count = sscanf(cur + 8,
5987  "%06"PRIx32", %06"PRIx32", %06"PRIx32", %06"PRIx32", "
5988  "%06"PRIx32", %06"PRIx32", %06"PRIx32", %06"PRIx32", "
5989  "%06"PRIx32", %06"PRIx32", %06"PRIx32", %06"PRIx32", "
5990  "%06"PRIx32", %06"PRIx32", %06"PRIx32", %06"PRIx32"",
5991  &palette[ 0], &palette[ 1], &palette[ 2], &palette[ 3],
5992  &palette[ 4], &palette[ 5], &palette[ 6], &palette[ 7],
5993  &palette[ 8], &palette[ 9], &palette[10], &palette[11],
5994  &palette[12], &palette[13], &palette[14], &palette[15]);
5995 
5996  for (i = 0; i < count; i++) {
5997  palette[i] = rgb_to_yuv(palette[i]);
5998  }
5999  have_palette = 1;
6000  } else if (!strncmp("size:", cur, 5)) {
6001  sscanf(cur + 5, "%dx%d", &width, &height);
6002  have_size = 1;
6003  }
6004  if (have_palette && have_size)
6005  break;
6006  cur += strcspn(cur, "\n\r");
6007  cur += strspn(cur, "\n\r");
6008  }
6009  if (have_palette) {
6010  track->vos_data = av_malloc(16*4);
6011  if (!track->vos_data)
6012  return AVERROR(ENOMEM);
6013  for (i = 0; i < 16; i++) {
6014  AV_WB32(track->vos_data + i * 4, palette[i]);
6015  }
6016  track->vos_len = 16 * 4;
6017  }
6018  st->codecpar->width = width;
6019  st->codecpar->height = track->height = height;
6020 
6021  return 0;
6022 }
6023 
6025 {
6026  MOVMuxContext *mov = s->priv_data;
6027  AVDictionaryEntry *global_tcr = av_dict_get(s->metadata, "timecode", NULL, 0);
6028  int i, ret;
6029 
6030  mov->fc = s;
6031 
6032  /* Default mode == MP4 */
6033  mov->mode = MODE_MP4;
6034 
6035  if (s->oformat) {
6036  if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
6037  else if (!strcmp("3g2", s->oformat->name)) mov->mode = MODE_3GP|MODE_3G2;
6038  else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
6039  else if (!strcmp("psp", s->oformat->name)) mov->mode = MODE_PSP;
6040  else if (!strcmp("ipod",s->oformat->name)) mov->mode = MODE_IPOD;
6041  else if (!strcmp("ismv",s->oformat->name)) mov->mode = MODE_ISM;
6042  else if (!strcmp("f4v", s->oformat->name)) mov->mode = MODE_F4V;
6043  }
6044 
6045  if (mov->flags & FF_MOV_FLAG_DELAY_MOOV)
6046  mov->flags |= FF_MOV_FLAG_EMPTY_MOOV;
6047 
6048  /* Set the FRAGMENT flag if any of the fragmentation methods are
6049  * enabled. */
6050  if (mov->max_fragment_duration || mov->max_fragment_size ||
6051  mov->flags & (FF_MOV_FLAG_EMPTY_MOOV |
6055  mov->flags |= FF_MOV_FLAG_FRAGMENT;
6056 
6057  /* Set other implicit flags immediately */
6058  if (mov->mode == MODE_ISM)
6061  if (mov->flags & FF_MOV_FLAG_DASH)
6064 
6066  av_log(s, AV_LOG_VERBOSE, "Empty MOOV enabled; disabling automatic bitstream filtering\n");
6067  s->flags &= ~AVFMT_FLAG_AUTO_BSF;
6068  }
6069 
6070  if (mov->flags & FF_MOV_FLAG_FASTSTART) {
6071  mov->reserved_moov_size = -1;
6072  }
6073 
6074  if (mov->use_editlist < 0) {
6075  mov->use_editlist = 1;
6076  if (mov->flags & FF_MOV_FLAG_FRAGMENT &&
6077  !(mov->flags & FF_MOV_FLAG_DELAY_MOOV)) {
6078  // If we can avoid needing an edit list by shifting the
6079  // tracks, prefer that over (trying to) write edit lists
6080  // in fragmented output.
6083  mov->use_editlist = 0;
6084  }
6085  }
6086  if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV &&
6087  !(mov->flags & FF_MOV_FLAG_DELAY_MOOV) && mov->use_editlist)
6088  av_log(s, AV_LOG_WARNING, "No meaningful edit list will be written when using empty_moov without delay_moov\n");
6089 
6092 
6093  /* Clear the omit_tfhd_offset flag if default_base_moof is set;
6094  * if the latter is set that's enough and omit_tfhd_offset doesn't
6095  * add anything extra on top of that. */
6096  if (mov->flags & FF_MOV_FLAG_OMIT_TFHD_OFFSET &&
6099 
6100  if (mov->frag_interleave &&
6102  av_log(s, AV_LOG_ERROR,
6103  "Sample interleaving in fragments is mutually exclusive with "
6104  "omit_tfhd_offset and separate_moof\n");
6105  return AVERROR(EINVAL);
6106  }
6107 
6108  /* Non-seekable output is ok if using fragmentation. If ism_lookahead
6109  * is enabled, we don't support non-seekable output at all. */
6110  if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL) &&
6111  (!(mov->flags & FF_MOV_FLAG_FRAGMENT) || mov->ism_lookahead)) {
6112  av_log(s, AV_LOG_ERROR, "muxer does not support non seekable output\n");
6113  return AVERROR(EINVAL);
6114  }
6115 
6116  mov->nb_streams = s->nb_streams;
6117  if (mov->mode & (MODE_MP4|MODE_MOV|MODE_IPOD) && s->nb_chapters)
6118  mov->chapter_track = mov->nb_streams++;
6119 
6120  if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
6121  for (i = 0; i < s->nb_streams; i++)
6122  if (rtp_hinting_needed(s->streams[i]))
6123  mov->nb_streams++;
6124  }
6125 
6126  if ( mov->write_tmcd == -1 && (mov->mode == MODE_MOV || mov->mode == MODE_MP4)
6127  || mov->write_tmcd == 1) {
6128  /* +1 tmcd track for each video stream with a timecode */
6129  for (i = 0; i < s->nb_streams; i++) {
6130  AVStream *st = s->streams[i];
6131  AVDictionaryEntry *t = global_tcr;
6132  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
6133  (t || (t=av_dict_get(st->metadata, "timecode", NULL, 0)))) {
6134  AVTimecode tc;
6135  ret = mov_check_timecode_track(s, &tc, i, t->value);
6136  if (ret >= 0)
6137  mov->nb_meta_tmcd++;
6138  }
6139  }
6140 
6141  /* check if there is already a tmcd track to remux */
6142  if (mov->nb_meta_tmcd) {
6143  for (i = 0; i < s->nb_streams; i++) {
6144  AVStream *st = s->streams[i];
6145  if (st->codecpar->codec_tag == MKTAG('t','m','c','d')) {
6146  av_log(s, AV_LOG_WARNING, "You requested a copy of the original timecode track "
6147  "so timecode metadata are now ignored\n");
6148  mov->nb_meta_tmcd = 0;
6149  }
6150  }
6151  }
6152 
6153  mov->nb_streams += mov->nb_meta_tmcd;
6154  }
6155 
6156  // Reserve an extra stream for chapters for the case where chapters
6157  // are written in the trailer
6158  mov->tracks = av_mallocz_array((mov->nb_streams + 1), sizeof(*mov->tracks));
6159  if (!mov->tracks)
6160  return AVERROR(ENOMEM);
6161 
6162  if (mov->encryption_scheme_str != NULL && strcmp(mov->encryption_scheme_str, "none") != 0) {
6163  if (strcmp(mov->encryption_scheme_str, "cenc-aes-ctr") == 0) {
6165 
6166  if (mov->encryption_key_len != AES_CTR_KEY_SIZE) {
6167  av_log(s, AV_LOG_ERROR, "Invalid encryption key len %d expected %d\n",
6169  return AVERROR(EINVAL);
6170  }
6171 
6172  if (mov->encryption_kid_len != CENC_KID_SIZE) {
6173  av_log(s, AV_LOG_ERROR, "Invalid encryption kid len %d expected %d\n",
6175  return AVERROR(EINVAL);
6176  }
6177  } else {
6178  av_log(s, AV_LOG_ERROR, "unsupported encryption scheme %s\n",
6179  mov->encryption_scheme_str);
6180  return AVERROR(EINVAL);
6181  }
6182  }
6183 
6184  for (i = 0; i < s->nb_streams; i++) {
6185  AVStream *st= s->streams[i];
6186  MOVTrack *track= &mov->tracks[i];
6187  AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL,0);
6188 
6189  track->st = st;
6190  track->par = st->codecpar;
6191  track->language = ff_mov_iso639_to_lang(lang?lang->value:"und", mov->mode!=MODE_MOV);
6192  if (track->language < 0)
6193  track->language = 0;
6194  track->mode = mov->mode;
6195  track->tag = mov_find_codec_tag(s, track);
6196  if (!track->tag) {
6197  av_log(s, AV_LOG_ERROR, "Could not find tag for codec %s in stream #%d, "
6198  "codec not currently supported in container\n",
6200  return AVERROR(EINVAL);
6201  }
6202  /* If hinting of this track is enabled by a later hint track,
6203  * this is updated. */
6204  track->hint_track = -1;
6205  track->start_dts = AV_NOPTS_VALUE;
6206  track->start_cts = AV_NOPTS_VALUE;
6207  track->end_pts = AV_NOPTS_VALUE;
6208  track->dts_shift = AV_NOPTS_VALUE;
6209  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
6210  if (track->tag == MKTAG('m','x','3','p') || track->tag == MKTAG('m','x','3','n') ||
6211  track->tag == MKTAG('m','x','4','p') || track->tag == MKTAG('m','x','4','n') ||
6212  track->tag == MKTAG('m','x','5','p') || track->tag == MKTAG('m','x','5','n')) {
6213  if (st->codecpar->width != 720 || (st->codecpar->height != 608 && st->codecpar->height != 512)) {
6214  av_log(s, AV_LOG_ERROR, "D-10/IMX must use 720x608 or 720x512 video resolution\n");
6215  return AVERROR(EINVAL);
6216  }
6217  track->height = track->tag >> 24 == 'n' ? 486 : 576;
6218  }
6219  if (mov->video_track_timescale) {
6220  track->timescale = mov->video_track_timescale;
6221  } else {
6222  track->timescale = st->time_base.den;
6223  while(track->timescale < 10000)
6224  track->timescale *= 2;
6225  }
6226  if (st->codecpar->width > 65535 || st->codecpar->height > 65535) {
6227  av_log(s, AV_LOG_ERROR, "Resolution %dx%d too large for mov/mp4\n", st->codecpar->width, st->codecpar->height);
6228  return AVERROR(EINVAL);
6229  }
6230  if (track->mode == MODE_MOV && track->timescale > 100000)
6232  "WARNING codec timebase is very high. If duration is too long,\n"
6233  "file may not be playable by quicktime. Specify a shorter timebase\n"
6234  "or choose different container.\n");
6235  if (track->mode == MODE_MOV &&
6236  track->par->codec_id == AV_CODEC_ID_RAWVIDEO &&
6237  track->tag == MKTAG('r','a','w',' ')) {
6238  enum AVPixelFormat pix_fmt = track->par->format;
6239  if (pix_fmt == AV_PIX_FMT_NONE && track->par->bits_per_coded_sample == 1)
6240  pix_fmt = AV_PIX_FMT_MONOWHITE;
6241  track->is_unaligned_qt_rgb =
6242  pix_fmt == AV_PIX_FMT_RGB24 ||
6243  pix_fmt == AV_PIX_FMT_BGR24 ||
6244  pix_fmt == AV_PIX_FMT_PAL8 ||
6245  pix_fmt == AV_PIX_FMT_GRAY8 ||
6246  pix_fmt == AV_PIX_FMT_MONOWHITE ||
6247  pix_fmt == AV_PIX_FMT_MONOBLACK;
6248  }
6249  if (track->par->codec_id == AV_CODEC_ID_VP9 ||
6250  track->par->codec_id == AV_CODEC_ID_AV1) {
6251  if (track->mode != MODE_MP4) {
6252  av_log(s, AV_LOG_ERROR, "%s only supported in MP4.\n", avcodec_get_name(track->par->codec_id));
6253  return AVERROR(EINVAL);
6254  }
6255  } else if (track->par->codec_id == AV_CODEC_ID_VP8) {
6256  /* altref frames handling is not defined in the spec as of version v1.0,
6257  * so just forbid muxing VP8 streams altogether until a new version does */
6258  av_log(s, AV_LOG_ERROR, "VP8 muxing is currently not supported.\n");
6259  return AVERROR_PATCHWELCOME;
6260  }
6261  } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
6262  track->timescale = st->codecpar->sample_rate;
6264  av_log(s, AV_LOG_WARNING, "track %d: codec frame size is not set\n", i);
6265  track->audio_vbr = 1;
6266  }else if (st->codecpar->codec_id == AV_CODEC_ID_ADPCM_MS ||
6269  if (!st->codecpar->block_align) {
6270  av_log(s, AV_LOG_ERROR, "track %d: codec block align is not set for adpcm\n", i);
6271  return AVERROR(EINVAL);
6272  }
6273  track->sample_size = st->codecpar->block_align;
6274  }else if (st->codecpar->frame_size > 1){ /* assume compressed audio */
6275  track->audio_vbr = 1;
6276  }else{
6278  }
6279  if (st->codecpar->codec_id == AV_CODEC_ID_ILBC ||
6281  track->audio_vbr = 1;
6282  }
6283  if (track->mode != MODE_MOV &&
6284  track->par->codec_id == AV_CODEC_ID_MP3 && track->timescale < 16000) {
6286  av_log(s, AV_LOG_ERROR, "track %d: muxing mp3 at %dhz is not standard, to mux anyway set strict to -1\n",
6287  i, track->par->sample_rate);
6288  return AVERROR(EINVAL);
6289  } else {
6290  av_log(s, AV_LOG_WARNING, "track %d: muxing mp3 at %dhz is not standard in MP4\n",
6291  i, track->par->sample_rate);
6292  }
6293  }
6294  if (track->par->codec_id == AV_CODEC_ID_FLAC ||
6295  track->par->codec_id == AV_CODEC_ID_OPUS) {
6296  if (track->mode != MODE_MP4) {
6297  av_log(s, AV_LOG_ERROR, "%s only supported in MP4.\n", avcodec_get_name(track->par->codec_id));
6298  return AVERROR(EINVAL);
6299  }
6301  av_log(s, AV_LOG_ERROR,
6302  "%s in MP4 support is experimental, add "
6303  "'-strict %d' if you want to use it.\n",
6305  return AVERROR_EXPERIMENTAL;
6306  }
6307  }
6308  } else if (st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) {
6309  track->timescale = st->time_base.den;
6310  } else if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA) {
6311  track->timescale = st->time_base.den;
6312  } else {
6313  track->timescale = MOV_TIMESCALE;
6314  }
6315  if (!track->height)
6316  track->height = st->codecpar->height;
6317  /* The ism specific timescale isn't mandatory, but is assumed by
6318  * some tools, such as mp4split. */
6319  if (mov->mode == MODE_ISM)
6320  track->timescale = 10000000;
6321 
6322  avpriv_set_pts_info(st, 64, 1, track->timescale);
6323 
6325  ret = ff_mov_cenc_init(&track->cenc, mov->encryption_key,
6327  if (ret)
6328  return ret;
6329  }
6330  }
6331 
6332  enable_tracks(s);
6333  return 0;
6334 }
6335 
6337 {
6338  AVIOContext *pb = s->pb;
6339  MOVMuxContext *mov = s->priv_data;
6340  AVDictionaryEntry *t, *global_tcr = av_dict_get(s->metadata, "timecode", NULL, 0);
6341  int i, ret, hint_track = 0, tmcd_track = 0, nb_tracks = s->nb_streams;
6342 
6343  if (mov->mode & (MODE_MP4|MODE_MOV|MODE_IPOD) && s->nb_chapters)
6344  nb_tracks++;
6345 
6346  if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
6347  hint_track = nb_tracks;
6348  for (i = 0; i < s->nb_streams; i++)
6349  if (rtp_hinting_needed(s->streams[i]))
6350  nb_tracks++;
6351  }
6352 
6353  if (mov->mode == MODE_MOV || mov->mode == MODE_MP4)
6354  tmcd_track = nb_tracks;
6355 
6356  for (i = 0; i < s->nb_streams; i++) {
6357  int j;
6358  AVStream *st= s->streams[i];
6359  MOVTrack *track= &mov->tracks[i];
6360 
6361  /* copy extradata if it exists */
6362  if (st->codecpar->extradata_size) {
6365  else if (!TAG_IS_AVCI(track->tag) && st->codecpar->codec_id != AV_CODEC_ID_DNXHD) {
6366  track->vos_len = st->codecpar->extradata_size;
6367  track->vos_data = av_malloc(track->vos_len);
6368  if (!track->vos_data) {
6369  return AVERROR(ENOMEM);
6370  }
6371  memcpy(track->vos_data, st->codecpar->extradata, track->vos_len);
6372  }
6373  }
6374 
6375  if (st->codecpar->codec_type != AVMEDIA_TYPE_AUDIO ||
6377  continue;
6378 
6379  for (j = 0; j < s->nb_streams; j++) {
6380  AVStream *stj= s->streams[j];
6381  MOVTrack *trackj= &mov->tracks[j];
6382  if (j == i)
6383  continue;
6384 
6385  if (stj->codecpar->codec_type != AVMEDIA_TYPE_AUDIO ||
6386  trackj->par->channel_layout != AV_CH_LAYOUT_MONO ||
6387  trackj->language != track->language ||
6388  trackj->tag != track->tag
6389  )
6390  continue;
6391  track->multichannel_as_mono++;
6392  }
6393  }
6394 
6395  if (!(mov->flags & FF_MOV_FLAG_DELAY_MOOV)) {
6396  if ((ret = mov_write_identification(pb, s)) < 0)
6397  return ret;
6398  }
6399 
6400  if (mov->reserved_moov_size){
6401  mov->reserved_header_pos = avio_tell(pb);
6402  if (mov->reserved_moov_size > 0)
6403  avio_skip(pb, mov->reserved_moov_size);
6404  }
6405 
6406  if (mov->flags & FF_MOV_FLAG_FRAGMENT) {
6407  /* If no fragmentation options have been set, set a default. */
6408  if (!(mov->flags & (FF_MOV_FLAG_FRAG_KEYFRAME |
6413  } else {
6414  if (mov->flags & FF_MOV_FLAG_FASTSTART)
6415  mov->reserved_header_pos = avio_tell(pb);
6416  mov_write_mdat_tag(pb, mov);
6417  }
6418 
6420  if (mov->time)
6421  mov->time += 0x7C25B080; // 1970 based -> 1904 based
6422 
6423  if (mov->chapter_track)
6424  if ((ret = mov_create_chapter_track(s, mov->chapter_track)) < 0)
6425  return ret;
6426 
6427  if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
6428  for (i = 0; i < s->nb_streams; i++) {
6429  if (rtp_hinting_needed(s->streams[i])) {
6430  if ((ret = ff_mov_init_hinting(s, hint_track, i)) < 0)
6431  return ret;
6432  hint_track++;
6433  }
6434  }
6435  }
6436 
6437  if (mov->nb_meta_tmcd) {
6438  /* Initialize the tmcd tracks */
6439  for (i = 0; i < s->nb_streams; i++) {
6440  AVStream *st = s->streams[i];
6441  t = global_tcr;
6442 
6443  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
6444  AVTimecode tc;
6445  if (!t)
6446  t = av_dict_get(st->metadata, "timecode", NULL, 0);
6447  if (!t)
6448  continue;
6449  if (mov_check_timecode_track(s, &tc, i, t->value) < 0)
6450  continue;
6451  if ((ret = mov_create_timecode_track(s, tmcd_track, i, tc)) < 0)
6452  return ret;
6453  tmcd_track++;
6454  }
6455  }
6456  }
6457 
6458  avio_flush(pb);
6459 
6460  if (mov->flags & FF_MOV_FLAG_ISML)
6461  mov_write_isml_manifest(pb, mov, s);
6462 
6463  if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV &&
6464  !(mov->flags & FF_MOV_FLAG_DELAY_MOOV)) {
6465  if ((ret = mov_write_moov_tag(pb, mov, s)) < 0)
6466  return ret;
6467  avio_flush(pb);
6468  mov->moov_written = 1;
6469  if (mov->flags & FF_MOV_FLAG_GLOBAL_SIDX)
6470  mov->reserved_header_pos = avio_tell(pb);
6471  }
6472 
6473  return 0;
6474 }
6475 
6477 {
6478  int ret;
6479  AVIOContext *moov_buf;
6480  MOVMuxContext *mov = s->priv_data;
6481 
6482  if ((ret = ffio_open_null_buf(&moov_buf)) < 0)
6483  return ret;
6484  if ((ret = mov_write_moov_tag(moov_buf, mov, s)) < 0)
6485  return ret;
6486  return ffio_close_null_buf(moov_buf);
6487 }
6488 
6490 {
6491  int ret;
6492  AVIOContext *buf;
6493  MOVMuxContext *mov = s->priv_data;
6494 
6495  if ((ret = ffio_open_null_buf(&buf)) < 0)
6496  return ret;
6497  mov_write_sidx_tags(buf, mov, -1, 0);
6498  return ffio_close_null_buf(buf);
6499 }
6500 
6501 /*
6502  * This function gets the moov size if moved to the top of the file: the chunk
6503  * offset table can switch between stco (32-bit entries) to co64 (64-bit
6504  * entries) when the moov is moved to the beginning, so the size of the moov
6505  * would change. It also updates the chunk offset tables.
6506  */
6508 {
6509  int i, moov_size, moov_size2;
6510  MOVMuxContext *mov = s->priv_data;
6511 
6512  moov_size = get_moov_size(s);
6513  if (moov_size < 0)
6514  return moov_size;
6515 
6516  for (i = 0; i < mov->nb_streams; i++)
6517  mov->tracks[i].data_offset += moov_size;
6518 
6519  moov_size2 = get_moov_size(s);
6520  if (moov_size2 < 0)
6521  return moov_size2;
6522 
6523  /* if the size changed, we just switched from stco to co64 and need to
6524  * update the offsets */
6525  if (moov_size2 != moov_size)
6526  for (i = 0; i < mov->nb_streams; i++)
6527  mov->tracks[i].data_offset += moov_size2 - moov_size;
6528 
6529  return moov_size2;
6530 }
6531 
6533 {
6534  int i, sidx_size;
6535  MOVMuxContext *mov = s->priv_data;
6536 
6537  sidx_size = get_sidx_size(s);
6538  if (sidx_size < 0)
6539  return sidx_size;
6540 
6541  for (i = 0; i < mov->nb_streams; i++)
6542  mov->tracks[i].data_offset += sidx_size;
6543 
6544  return sidx_size;
6545 }
6546 
6548 {
6549  int ret = 0, moov_size;
6550  MOVMuxContext *mov = s->priv_data;
6551  int64_t pos, pos_end = avio_tell(s->pb);
6552  uint8_t *buf, *read_buf[2];
6553  int read_buf_id = 0;
6554  int read_size[2];
6555  AVIOContext *read_pb;
6556 
6557  if (mov->flags & FF_MOV_FLAG_FRAGMENT)
6558  moov_size = compute_sidx_size(s);
6559  else
6560  moov_size = compute_moov_size(s);
6561  if (moov_size < 0)
6562  return moov_size;
6563 
6564  buf = av_malloc(moov_size * 2);
6565  if (!buf)
6566  return AVERROR(ENOMEM);
6567  read_buf[0] = buf;
6568  read_buf[1] = buf + moov_size;
6569 
6570  /* Shift the data: the AVIO context of the output can only be used for
6571  * writing, so we re-open the same output, but for reading. It also avoids
6572  * a read/seek/write/seek back and forth. */
6573  avio_flush(s->pb);
6574  ret = s->io_open(s, &read_pb, s->url, AVIO_FLAG_READ, NULL);
6575  if (ret < 0) {
6576  av_log(s, AV_LOG_ERROR, "Unable to re-open %s output file for "
6577  "the second pass (faststart)\n", s->url);
6578  goto end;
6579  }
6580 
6581  /* mark the end of the shift to up to the last data we wrote, and get ready
6582  * for writing */
6583  pos_end = avio_tell(s->pb);
6584  avio_seek(s->pb, mov->reserved_header_pos + moov_size, SEEK_SET);
6585 
6586  /* start reading at where the new moov will be placed */
6587  avio_seek(read_pb, mov->reserved_header_pos, SEEK_SET);
6588  pos = avio_tell(read_pb);
6589 
6590 #define READ_BLOCK do { \
6591  read_size[read_buf_id] = avio_read(read_pb, read_buf[read_buf_id], moov_size); \
6592  read_buf_id ^= 1; \
6593 } while (0)
6594 
6595  /* shift data by chunk of at most moov_size */
6596  READ_BLOCK;
6597  do {
6598  int n;
6599  READ_BLOCK;
6600  n = read_size[read_buf_id];
6601  if (n <= 0)
6602  break;
6603  avio_write(s->pb, read_buf[read_buf_id], n);
6604  pos += n;
6605  } while (pos < pos_end);
6606  ff_format_io_close(s, &read_pb);
6607 
6608 end:
6609  av_free(buf);
6610  return ret;
6611 }
6612 
6614 {
6615  MOVMuxContext *mov = s->priv_data;
6616  AVIOContext *pb = s->pb;
6617  int res = 0;
6618  int i;
6619  int64_t moov_pos;
6620 
6621  if (mov->need_rewrite_extradata) {
6622  for (i = 0; i < s->nb_streams; i++) {
6623  MOVTrack *track = &mov->tracks[i];
6624  AVCodecParameters *par = track->par;
6625 
6626  track->vos_len = par->extradata_size;
6627  track->vos_data = av_malloc(track->vos_len);
6628  if (!track->vos_data)
6629  return AVERROR(ENOMEM);
6630  memcpy(track->vos_data, par->extradata, track->vos_len);
6631  }
6632  mov->need_rewrite_extradata = 0;
6633  }
6634 
6635  /*
6636  * Before actually writing the trailer, make sure that there are no
6637  * dangling subtitles, that need a terminating sample.
6638  */
6639  for (i = 0; i < mov->nb_streams; i++) {
6640  MOVTrack *trk = &mov->tracks[i];
6641  if (trk->par->codec_id == AV_CODEC_ID_MOV_TEXT &&
6644  trk->last_sample_is_subtitle_end = 1;
6645  }
6646  }
6647 
6648  // If there were no chapters when the header was written, but there
6649  // are chapters now, write them in the trailer. This only works
6650  // when we are not doing fragments.
6651  if (!mov->chapter_track && !(mov->flags & FF_MOV_FLAG_FRAGMENT)) {
6652  if (mov->mode & (MODE_MP4|MODE_MOV|MODE_IPOD) && s->nb_chapters) {
6653  mov->chapter_track = mov->nb_streams++;
6654  if ((res = mov_create_chapter_track(s, mov->chapter_track)) < 0)
6655  return res;
6656  }
6657  }
6658 
6659  if (!(mov->flags & FF_MOV_FLAG_FRAGMENT)) {
6660  moov_pos = avio_tell(pb);
6661 
6662  /* Write size of mdat tag */
6663  if (mov->mdat_size + 8 <= UINT32_MAX) {
6664  avio_seek(pb, mov->mdat_pos, SEEK_SET);
6665  avio_wb32(pb, mov->mdat_size + 8);
6666  } else {
6667  /* overwrite 'wide' placeholder atom */
6668  avio_seek(pb, mov->mdat_pos - 8, SEEK_SET);
6669  /* special value: real atom size will be 64 bit value after
6670  * tag field */
6671  avio_wb32(pb, 1);
6672  ffio_wfourcc(pb, "mdat");
6673  avio_wb64(pb, mov->mdat_size + 16);
6674  }
6675  avio_seek(pb, mov->reserved_moov_size > 0 ? mov->reserved_header_pos : moov_pos, SEEK_SET);
6676 
6677  if (mov->flags & FF_MOV_FLAG_FASTSTART) {
6678  av_log(s, AV_LOG_INFO, "Starting second pass: moving the moov atom to the beginning of the file\n");
6679  res = shift_data(s);
6680  if (res < 0)
6681  return res;
6682  avio_seek(pb, mov->reserved_header_pos, SEEK_SET);
6683  if ((res = mov_write_moov_tag(pb, mov, s)) < 0)
6684  return res;
6685  } else if (mov->reserved_moov_size > 0) {
6686  int64_t size;
6687  if ((res = mov_write_moov_tag(pb, mov, s)) < 0)
6688  return res;
6689  size = mov->reserved_moov_size - (avio_tell(pb) - mov->reserved_header_pos);
6690  if (size < 8){
6691  av_log(s, AV_LOG_ERROR, "reserved_moov_size is too small, needed %"PRId64" additional\n", 8-size);
6692  return AVERROR(EINVAL);
6693  }
6694  avio_wb32(pb, size);
6695  ffio_wfourcc(pb, "free");
6696  ffio_fill(pb, 0, size - 8);
6697  avio_seek(pb, moov_pos, SEEK_SET);
6698  } else {
6699  if ((res = mov_write_moov_tag(pb, mov, s)) < 0)
6700  return res;
6701  }
6702  res = 0;
6703  } else {
6705  for (i = 0; i < mov->nb_streams; i++)
6706  mov->tracks[i].data_offset = 0;
6707  if (mov->flags & FF_MOV_FLAG_GLOBAL_SIDX) {
6708  int64_t end;
6709  av_log(s, AV_LOG_INFO, "Starting second pass: inserting sidx atoms\n");
6710  res = shift_data(s);
6711  if (res < 0)
6712  return res;
6713  end = avio_tell(pb);
6714  avio_seek(pb, mov->reserved_header_pos, SEEK_SET);
6715  mov_write_sidx_tags(pb, mov, -1, 0);
6716  avio_seek(pb, end, SEEK_SET);
6718  mov_write_mfra_tag(pb, mov);
6719  } else if (!(mov->flags & FF_MOV_FLAG_SKIP_TRAILER)) {
6721  mov_write_mfra_tag(pb, mov);
6722  }
6723  }
6724 
6725  return res;
6726 }
6727 
6728 static int mov_check_bitstream(struct AVFormatContext *s, const AVPacket *pkt)
6729 {
6730  int ret = 1;
6731  AVStream *st = s->streams[pkt->stream_index];
6732 
6733  if (st->codecpar->codec_id == AV_CODEC_ID_AAC) {
6734  if (pkt->size > 2 && (AV_RB16(pkt->data) & 0xfff0) == 0xfff0)
6735  ret = ff_stream_add_bitstream_filter(st, "aac_adtstoasc", NULL);
6736  } else if (st->codecpar->codec_id == AV_CODEC_ID_VP9) {
6737  ret = ff_stream_add_bitstream_filter(st, "vp9_superframe", NULL);
6738  }
6739 
6740  return ret;
6741 }
6742 
6743 static const AVCodecTag codec_3gp_tags[] = {
6744  { AV_CODEC_ID_H263, MKTAG('s','2','6','3') },
6745  { AV_CODEC_ID_H264, MKTAG('a','v','c','1') },
6746  { AV_CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
6747  { AV_CODEC_ID_AAC, MKTAG('m','p','4','a') },
6748  { AV_CODEC_ID_AMR_NB, MKTAG('s','a','m','r') },
6749  { AV_CODEC_ID_AMR_WB, MKTAG('s','a','w','b') },
6750  { AV_CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
6751  { AV_CODEC_ID_NONE, 0 },
6752 };
6753 
6755  { AV_CODEC_ID_MPEG4 , MKTAG('m', 'p', '4', 'v') },
6756  { AV_CODEC_ID_H264 , MKTAG('a', 'v', 'c', '1') },
6757  { AV_CODEC_ID_H264 , MKTAG('a', 'v', 'c', '3') },
6758  { AV_CODEC_ID_HEVC , MKTAG('h', 'e', 'v', '1') },
6759  { AV_CODEC_ID_HEVC , MKTAG('h', 'v', 'c', '1') },
6760  { AV_CODEC_ID_MPEG2VIDEO , MKTAG('m', 'p', '4', 'v') },
6761  { AV_CODEC_ID_MPEG1VIDEO , MKTAG('m', 'p', '4', 'v') },
6762  { AV_CODEC_ID_MJPEG , MKTAG('m', 'p', '4', 'v') },
6763  { AV_CODEC_ID_PNG , MKTAG('m', 'p', '4', 'v') },
6764  { AV_CODEC_ID_JPEG2000 , MKTAG('m', 'p', '4', 'v') },
6765  { AV_CODEC_ID_VC1 , MKTAG('v', 'c', '-', '1') },
6766  { AV_CODEC_ID_DIRAC , MKTAG('d', 'r', 'a', 'c') },
6767  { AV_CODEC_ID_TSCC2 , MKTAG('m', 'p', '4', 'v') },
6768  { AV_CODEC_ID_VP9 , MKTAG('v', 'p', '0', '9') },
6769  { AV_CODEC_ID_AV1 , MKTAG('a', 'v', '0', '1') },
6770  { AV_CODEC_ID_AAC , MKTAG('m', 'p', '4', 'a') },
6771  { AV_CODEC_ID_MP4ALS , MKTAG('m', 'p', '4', 'a') },
6772  { AV_CODEC_ID_MP3 , MKTAG('m', 'p', '4', 'a') },
6773  { AV_CODEC_ID_MP2 , MKTAG('m', 'p', '4', 'a') },
6774  { AV_CODEC_ID_AC3 , MKTAG('a', 'c', '-', '3') },
6775  { AV_CODEC_ID_EAC3 , MKTAG('e', 'c', '-', '3') },
6776  { AV_CODEC_ID_DTS , MKTAG('m', 'p', '4', 'a') },
6777  { AV_CODEC_ID_FLAC , MKTAG('f', 'L', 'a', 'C') },
6778  { AV_CODEC_ID_OPUS , MKTAG('O', 'p', 'u', 's') },
6779  { AV_CODEC_ID_VORBIS , MKTAG('m', 'p', '4', 'a') },
6780  { AV_CODEC_ID_QCELP , MKTAG('m', 'p', '4', 'a') },
6781  { AV_CODEC_ID_EVRC , MKTAG('m', 'p', '4', 'a') },
6782  { AV_CODEC_ID_DVD_SUBTITLE, MKTAG('m', 'p', '4', 's') },
6783  { AV_CODEC_ID_MOV_TEXT , MKTAG('t', 'x', '3', 'g') },
6784  { AV_CODEC_ID_BIN_DATA , MKTAG('g', 'p', 'm', 'd') },
6785  { AV_CODEC_ID_NONE , 0 },
6786 };
6787 
6789  { AV_CODEC_ID_WMAPRO , MKTAG('w', 'm', 'a', ' ') },
6790  { AV_CODEC_ID_NONE , 0 },
6791 };
6792 
6793 static const AVCodecTag codec_ipod_tags[] = {
6794  { AV_CODEC_ID_H264, MKTAG('a','v','c','1') },
6795  { AV_CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
6796  { AV_CODEC_ID_AAC, MKTAG('m','p','4','a') },
6797  { AV_CODEC_ID_ALAC, MKTAG('a','l','a','c') },
6798  { AV_CODEC_ID_AC3, MKTAG('a','c','-','3') },
6799  { AV_CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
6800  { AV_CODEC_ID_MOV_TEXT, MKTAG('t','e','x','t') },
6801  { AV_CODEC_ID_NONE, 0 },
6802 };
6803 
6804 static const AVCodecTag codec_f4v_tags[] = {
6805  { AV_CODEC_ID_MP3, MKTAG('.','m','p','3') },
6806  { AV_CODEC_ID_AAC, MKTAG('m','p','4','a') },
6807  { AV_CODEC_ID_H264, MKTAG('a','v','c','1') },
6808  { AV_CODEC_ID_VP6A, MKTAG('V','P','6','A') },
6809  { AV_CODEC_ID_VP6F, MKTAG('V','P','6','F') },
6810  { AV_CODEC_ID_NONE, 0 },
6811 };
6812 
6813 #if CONFIG_MOV_MUXER
6814 MOV_CLASS(mov)
6816  .name = "mov",
6817  .long_name = NULL_IF_CONFIG_SMALL("QuickTime / MOV"),
6818  .extensions = "mov",
6819  .priv_data_size = sizeof(MOVMuxContext),
6820  .audio_codec = AV_CODEC_ID_AAC,
6821  .video_codec = CONFIG_LIBX264_ENCODER ?
6823  .init = mov_init,
6827  .deinit = mov_free,
6829  .codec_tag = (const AVCodecTag* const []){
6831  },
6832  .check_bitstream = mov_check_bitstream,
6833  .priv_class = &mov_muxer_class,
6834 };
6835 #endif
6836 #if CONFIG_TGP_MUXER
6837 MOV_CLASS(tgp)
6839  .name = "3gp",
6840  .long_name = NULL_IF_CONFIG_SMALL("3GP (3GPP file format)"),
6841  .extensions = "3gp",
6842  .priv_data_size = sizeof(MOVMuxContext),
6843  .audio_codec = AV_CODEC_ID_AMR_NB,
6844  .video_codec = AV_CODEC_ID_H263,
6845  .init = mov_init,
6849  .deinit = mov_free,
6851  .codec_tag = (const AVCodecTag* const []){ codec_3gp_tags, 0 },
6852  .check_bitstream = mov_check_bitstream,
6853  .priv_class = &tgp_muxer_class,
6854 };
6855 #endif
6856 #if CONFIG_MP4_MUXER
6857 MOV_CLASS(mp4)
6859  .name = "mp4",
6860  .long_name = NULL_IF_CONFIG_SMALL("MP4 (MPEG-4 Part 14)"),
6861  .mime_type = "video/mp4",
6862  .extensions = "mp4",
6863  .priv_data_size = sizeof(MOVMuxContext),
6864  .audio_codec = AV_CODEC_ID_AAC,
6865  .video_codec = CONFIG_LIBX264_ENCODER ?
6867  .init = mov_init,
6871  .deinit = mov_free,
6873  .codec_tag = (const AVCodecTag* const []){ codec_mp4_tags, 0 },
6874  .check_bitstream = mov_check_bitstream,
6875  .priv_class = &mp4_muxer_class,
6876 };
6877 #endif
6878 #if CONFIG_PSP_MUXER
6879 MOV_CLASS(psp)
6881  .name = "psp",
6882  .long_name = NULL_IF_CONFIG_SMALL("PSP MP4 (MPEG-4 Part 14)"),
6883  .extensions = "mp4,psp",
6884  .priv_data_size = sizeof(MOVMuxContext),
6885  .audio_codec = AV_CODEC_ID_AAC,
6886  .video_codec = CONFIG_LIBX264_ENCODER ?
6888  .init = mov_init,
6892  .deinit = mov_free,
6894  .codec_tag = (const AVCodecTag* const []){ codec_mp4_tags, 0 },
6895  .check_bitstream = mov_check_bitstream,
6896  .priv_class = &psp_muxer_class,
6897 };
6898 #endif
6899 #if CONFIG_TG2_MUXER
6900 MOV_CLASS(tg2)
6902  .name = "3g2",
6903  .long_name = NULL_IF_CONFIG_SMALL("3GP2 (3GPP2 file format)"),
6904  .extensions = "3g2",
6905  .priv_data_size = sizeof(MOVMuxContext),
6906  .audio_codec = AV_CODEC_ID_AMR_NB,
6907  .video_codec = AV_CODEC_ID_H263,
6908  .init = mov_init,
6912  .deinit = mov_free,
6914  .codec_tag = (const AVCodecTag* const []){ codec_3gp_tags, 0 },
6915  .check_bitstream = mov_check_bitstream,
6916  .priv_class = &tg2_muxer_class,
6917 };
6918 #endif
6919 #if CONFIG_IPOD_MUXER
6920 MOV_CLASS(ipod)
6922  .name = "ipod",
6923  .long_name = NULL_IF_CONFIG_SMALL("iPod H.264 MP4 (MPEG-4 Part 14)"),
6924  .mime_type = "video/mp4",
6925  .extensions = "m4v,m4a,m4b",
6926  .priv_data_size = sizeof(MOVMuxContext),
6927  .audio_codec = AV_CODEC_ID_AAC,
6928  .video_codec = AV_CODEC_ID_H264,
6929  .init = mov_init,
6933  .deinit = mov_free,
6935  .codec_tag = (const AVCodecTag* const []){ codec_ipod_tags, 0 },
6936  .check_bitstream = mov_check_bitstream,
6937  .priv_class = &ipod_muxer_class,
6938 };
6939 #endif
6940 #if CONFIG_ISMV_MUXER
6941 MOV_CLASS(ismv)
6943  .name = "ismv",
6944  .long_name = NULL_IF_CONFIG_SMALL("ISMV/ISMA (Smooth Streaming)"),
6945  .mime_type = "video/mp4",
6946  .extensions = "ismv,isma",
6947  .priv_data_size = sizeof(MOVMuxContext),
6948  .audio_codec = AV_CODEC_ID_AAC,
6949  .video_codec = AV_CODEC_ID_H264,
6950  .init = mov_init,
6954  .deinit = mov_free,
6956  .codec_tag = (const AVCodecTag* const []){
6958  .check_bitstream = mov_check_bitstream,
6959  .priv_class = &ismv_muxer_class,
6960 };
6961 #endif
6962 #if CONFIG_F4V_MUXER
6963 MOV_CLASS(f4v)
6965  .name = "f4v",
6966  .long_name = NULL_IF_CONFIG_SMALL("F4V Adobe Flash Video"),
6967  .mime_type = "application/f4v",
6968  .extensions = "f4v",
6969  .priv_data_size = sizeof(MOVMuxContext),
6970  .audio_codec = AV_CODEC_ID_AAC,
6971  .video_codec = AV_CODEC_ID_H264,
6972  .init = mov_init,
6976  .deinit = mov_free,
6978  .codec_tag = (const AVCodecTag* const []){ codec_f4v_tags, 0 },
6979  .check_bitstream = mov_check_bitstream,
6980  .priv_class = &f4v_muxer_class,
6981 };
6982 #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:1949
uint8_t bitstream_mode
Definition: ac3.h:185
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
Definition: ffmpeg.c:689
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:4428
static int ascii_to_wc(AVIOContext *pb, const uint8_t *b)
Definition: movenc.c:3650
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:2462
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:4969
#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:4376
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:3774
#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:2141
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:3970
#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:1898
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:5604
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:4883
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:30
static int mov_write_dref_tag(AVIOContext *pb)
Definition: movenc.c:2317
static int mov_write_gpmd_tag(AVIOContext *pb, const MOVTrack *track)
Definition: movenc.c:2197
#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:3340
static const AVCodecTag codec_f4v_tags[]
Definition: movenc.c:6804
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
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:5825
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:1080
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:3099
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:3558
static int mov_write_pasp_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1779
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:3115
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:2479
static int mov_write_track_metadata(AVIOContext *pb, AVStream *st, const char *tag, const char *str)
Definition: movenc.c:3058
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:1926
AVStream * st
Definition: movenc.h:106
static int mov_write_colr_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1817
static int mov_write_source_reference_tag(AVIOContext *pb, MOVTrack *track, const char *reel_name)
Definition: movenc.c:2122
AVDictionary * metadata
Definition: avformat.h:1312
static uint16_t language_code(const char *str)
Definition: movenc.c:3661
#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:1646
AVCodecParameters * par
Definition: movenc.h:107
static int mov_write_tfrf_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track, int entry)
Definition: movenc.c:4273
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:6547
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:3421
static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: movenc.c:5625
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:1676
#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:4322
static int mov_write_single_packet(AVFormatContext *s, AVPacket *pkt)
Definition: movenc.c:5519
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:3787
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
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:3019
static int mov_write_minf_tag(AVFormatContext *s, AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track)
Definition: movenc.c:2669
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:1032
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:2332
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:2231
AVOptions.
uint8_t lfe_on
Definition: ac3.h:187
AVCodecParameters * avcodec_parameters_alloc(void)
Allocate a new AVCodecParameters and set its fields to default values (unknown/invalid/0).
Definition: utils.c:1996
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:3989
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:5671
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:5477
static int mov_write_tapt_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:2891
static int mov_create_chapter_track(AVFormatContext *s, int tracknum)
Definition: movenc.c:5727
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:2563
#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:4547
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:4512
static int mov_write_moov_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
Definition: movenc.c:3894
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:300
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:5925
#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:3475
static int mov_write_itunes_hdlr_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
Definition: movenc.c:3265
int fragments
Definition: movenc.h:199
static int mov_write_subtitle_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1659
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:4590
uint8_t bitstream_id
Definition: ac3.h:184
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:1630
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:1607
#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
static const uint16_t fiel_data[]
Definition: movenc.c:1642
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:2471
#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:6788
uint64_t ff_ntp_time(void)
Get the current time since NTP epoch in microseconds.
Definition: utils.c:4642
static int compute_sidx_size(AVFormatContext *s)
Definition: movenc.c:6532
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:4883
static int mov_write_udta_sdp(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:3037
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:5683
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:1508
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:3586
static int mov_write_meta_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
Definition: movenc.c:3608
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:4671
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:188
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:3503
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:3448
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:186
#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:183
static int mov_write_clap_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1764
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:1096
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:3315
static int mov_write_trun_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track, int moof_size, int first, int end)
Definition: movenc.c:4196
#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:5975
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:177
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:3072
#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:3860
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:1580
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:4943
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:4833
static int mov_write_gama_tag(AVIOContext *pb, MOVTrack *track, double gamma)
Definition: movenc.c:1792
#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:2743
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:2272
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:3288
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:4136
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:3194
uint8_t interlaced
Definition: mxfenc.c:2094
int audio_vbr
Definition: movenc.h:114
static int mov_check_bitstream(struct AVFormatContext *s, const AVPacket *pkt)
Definition: movenc.c:6728
static int mov_write_chpl_tag(AVIOContext *pb, AVFormatContext *s)
Definition: movenc.c:3690
#define width
uint32_t bit_rate
Definition: ac3.h:202
#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:2572
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:5709
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:3301
#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:2553
static int mov_auto_flush_fragment(AVFormatContext *s, int force)
Definition: movenc.c:5179
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:3975
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:2923
static int mov_init(AVFormatContext *s)
Definition: movenc.c:6024
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:3181
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:3630
#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:1139
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:4121
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:6489
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:4647
#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:1587
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:457
#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:4362
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:5959
#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:2654
int slices
Definition: movenc.h:155
static int mov_parse_mpeg2_frame(AVPacket *pkt, uint32_t *flags)
Definition: movenc.c:4862
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:3825
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:60
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:2427
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:1790
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:3980
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:204
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:3370
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:6743
mfxU16 profile
Definition: qsvenc.c:44
static int mov_write_tkhd_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track, AVStream *st)
Definition: movenc.c:2776
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:2578
static int mov_write_gmhd_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:2501
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:475
#define flags(name, subs,...)
Definition: cbs_av1.c:606
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:2762
#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:3716
unsigned int tag
Definition: internal.h:46
static int mov_write_trailer(AVFormatContext *s)
Definition: movenc.c:6613
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:3668
static int mov_write_sv3d_tag(AVFormatContext *s, AVIOContext *pb, AVSphericalMapping *spherical_mapping)
Definition: movenc.c:1706
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:3205
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:4308
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:4621
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:5834
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:4130
#define MOV_SYNC_SAMPLE
Definition: movenc.h:55
static int mov_write_tfxd_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:4252
#define flag(name)
Definition: cbs_av1.c:598
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:4451
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:4388
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:3352
static int mov_write_mdta_hdlr_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
Definition: movenc.c:3543
#define FF_MOV_FLAG_DISABLE_CHPL
Definition: movenc.h:248
static int mov_write_iods_tag(AVIOContext *pb, MOVMuxContext *mov)
Definition: movenc.c:3152
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:708
#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:4645
int avg_bitrate
Average bitrate of the stream, in bits per second.
Definition: avcodec.h:1123
int substreamid
substream identification
Definition: ac3.h:189
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:5546
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:3281
void * priv_data
Format private data.
Definition: avformat.h:1379
static int mov_write_header(AVFormatContext *s)
Definition: movenc.c:6336
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:2704
#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:4683
#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:5193
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:6793
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:2102
static int mov_write_tref_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:3008
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:4762
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:193
int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: movenc.c:5231
uint8_t channel_mode
Definition: ac3.h:186
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:2209
#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:5883
uint32_t flags
flags such as drop frame, +24 hours support, ...
Definition: timecode.h:43
const AVCodecTag codec_mp4_tags[]
Definition: movenc.c:6754
#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:4834
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:6507
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:6476
bitstream writer API