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