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