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