FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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"
34 #include "libavcodec/ac3_parser.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  { "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" },
66  { "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" },
67  { "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" },
68  { "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" },
69  { "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" },
70  { "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" },
71  { "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" },
72  { "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" },
73  { "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" },
74  { "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" },
75  { "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" },
76  { "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" },
77  { "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" },
78  { "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" },
79  { "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" },
80  { "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" },
81  FF_RTP_FLAG_OPTS(MOVMuxContext, rtp_flags),
82  { "skip_iods", "Skip writing iods atom.", offsetof(MOVMuxContext, iods_skip), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
83  { "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},
84  { "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},
85  { "frag_duration", "Maximum fragment duration", offsetof(MOVMuxContext, max_fragment_duration), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
86  { "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},
87  { "frag_size", "Maximum fragment size", offsetof(MOVMuxContext, max_fragment_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
88  { "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},
89  { "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},
90  { "brand", "Override major brand", offsetof(MOVMuxContext, major_brand), AV_OPT_TYPE_STRING, {.str = NULL}, .flags = AV_OPT_FLAG_ENCODING_PARAM },
91  { "use_editlist", "use edit list", offsetof(MOVMuxContext, use_editlist), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, AV_OPT_FLAG_ENCODING_PARAM},
92  { "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},
93  { "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},
94  { "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 },
95  { "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 },
96  { "encryption_key", "The media encryption key (hex)", offsetof(MOVMuxContext, encryption_key), AV_OPT_TYPE_BINARY, .flags = AV_OPT_FLAG_ENCODING_PARAM },
97  { "encryption_kid", "The media encryption key identifier (hex)", offsetof(MOVMuxContext, encryption_kid), AV_OPT_TYPE_BINARY, .flags = AV_OPT_FLAG_ENCODING_PARAM },
98  { "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},
99  { "write_tmcd", "force or disable writing tmcd", offsetof(MOVMuxContext, write_tmcd), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, AV_OPT_FLAG_ENCODING_PARAM},
100  { NULL },
101 };
102 
103 #define MOV_CLASS(flavor)\
104 static const AVClass flavor ## _muxer_class = {\
105  .class_name = #flavor " muxer",\
106  .item_name = av_default_item_name,\
107  .option = options,\
108  .version = LIBAVUTIL_VERSION_INT,\
109 };
110 
111 static int get_moov_size(AVFormatContext *s);
112 
113 static int utf8len(const uint8_t *b)
114 {
115  int len = 0;
116  int val;
117  while (*b) {
118  GET_UTF8(val, *b++, return -1;)
119  len++;
120  }
121  return len;
122 }
123 
124 //FIXME support 64 bit variant with wide placeholders
125 static int64_t update_size(AVIOContext *pb, int64_t pos)
126 {
127  int64_t curpos = avio_tell(pb);
128  avio_seek(pb, pos, SEEK_SET);
129  avio_wb32(pb, curpos - pos); /* rewrite size */
130  avio_seek(pb, curpos, SEEK_SET);
131 
132  return curpos - pos;
133 }
134 
135 static int co64_required(const MOVTrack *track)
136 {
137  if (track->entry > 0 && track->cluster[track->entry - 1].pos + track->data_offset > UINT32_MAX)
138  return 1;
139  return 0;
140 }
141 
142 /* Chunk offset atom */
143 static int mov_write_stco_tag(AVIOContext *pb, MOVTrack *track)
144 {
145  int i;
146  int mode64 = co64_required(track); // use 32 bit size variant if possible
147  int64_t pos = avio_tell(pb);
148  avio_wb32(pb, 0); /* size */
149  if (mode64)
150  ffio_wfourcc(pb, "co64");
151  else
152  ffio_wfourcc(pb, "stco");
153  avio_wb32(pb, 0); /* version & flags */
154  avio_wb32(pb, track->chunkCount); /* entry count */
155  for (i = 0; i < track->entry; i++) {
156  if (!track->cluster[i].chunkNum)
157  continue;
158  if (mode64 == 1)
159  avio_wb64(pb, track->cluster[i].pos + track->data_offset);
160  else
161  avio_wb32(pb, track->cluster[i].pos + track->data_offset);
162  }
163  return update_size(pb, pos);
164 }
165 
166 /* Sample size atom */
167 static int mov_write_stsz_tag(AVIOContext *pb, MOVTrack *track)
168 {
169  int equalChunks = 1;
170  int i, j, entries = 0, tst = -1, oldtst = -1;
171 
172  int64_t pos = avio_tell(pb);
173  avio_wb32(pb, 0); /* size */
174  ffio_wfourcc(pb, "stsz");
175  avio_wb32(pb, 0); /* version & flags */
176 
177  for (i = 0; i < track->entry; i++) {
178  tst = track->cluster[i].size / track->cluster[i].entries;
179  if (oldtst != -1 && tst != oldtst)
180  equalChunks = 0;
181  oldtst = tst;
182  entries += track->cluster[i].entries;
183  }
184  if (equalChunks && track->entry) {
185  int sSize = track->entry ? track->cluster[0].size / track->cluster[0].entries : 0;
186  sSize = FFMAX(1, sSize); // adpcm mono case could make sSize == 0
187  avio_wb32(pb, sSize); // sample size
188  avio_wb32(pb, entries); // sample count
189  } else {
190  avio_wb32(pb, 0); // sample size
191  avio_wb32(pb, entries); // sample count
192  for (i = 0; i < track->entry; i++) {
193  for (j = 0; j < track->cluster[i].entries; j++) {
194  avio_wb32(pb, track->cluster[i].size /
195  track->cluster[i].entries);
196  }
197  }
198  }
199  return update_size(pb, pos);
200 }
201 
202 /* Sample to chunk atom */
203 static int mov_write_stsc_tag(AVIOContext *pb, MOVTrack *track)
204 {
205  int index = 0, oldval = -1, i;
206  int64_t entryPos, curpos;
207 
208  int64_t pos = avio_tell(pb);
209  avio_wb32(pb, 0); /* size */
210  ffio_wfourcc(pb, "stsc");
211  avio_wb32(pb, 0); // version & flags
212  entryPos = avio_tell(pb);
213  avio_wb32(pb, track->chunkCount); // entry count
214  for (i = 0; i < track->entry; i++) {
215  if (oldval != track->cluster[i].samples_in_chunk && track->cluster[i].chunkNum) {
216  avio_wb32(pb, track->cluster[i].chunkNum); // first chunk
217  avio_wb32(pb, track->cluster[i].samples_in_chunk); // samples per chunk
218  avio_wb32(pb, 0x1); // sample description index
219  oldval = track->cluster[i].samples_in_chunk;
220  index++;
221  }
222  }
223  curpos = avio_tell(pb);
224  avio_seek(pb, entryPos, SEEK_SET);
225  avio_wb32(pb, index); // rewrite size
226  avio_seek(pb, curpos, SEEK_SET);
227 
228  return update_size(pb, pos);
229 }
230 
231 /* Sync sample atom */
232 static int mov_write_stss_tag(AVIOContext *pb, MOVTrack *track, uint32_t flag)
233 {
234  int64_t curpos, entryPos;
235  int i, index = 0;
236  int64_t pos = avio_tell(pb);
237  avio_wb32(pb, 0); // size
238  ffio_wfourcc(pb, flag == MOV_SYNC_SAMPLE ? "stss" : "stps");
239  avio_wb32(pb, 0); // version & flags
240  entryPos = avio_tell(pb);
241  avio_wb32(pb, track->entry); // entry count
242  for (i = 0; i < track->entry; i++) {
243  if (track->cluster[i].flags & flag) {
244  avio_wb32(pb, i + 1);
245  index++;
246  }
247  }
248  curpos = avio_tell(pb);
249  avio_seek(pb, entryPos, SEEK_SET);
250  avio_wb32(pb, index); // rewrite size
251  avio_seek(pb, curpos, SEEK_SET);
252  return update_size(pb, pos);
253 }
254 
255 static int mov_write_amr_tag(AVIOContext *pb, MOVTrack *track)
256 {
257  avio_wb32(pb, 0x11); /* size */
258  if (track->mode == MODE_MOV) ffio_wfourcc(pb, "samr");
259  else ffio_wfourcc(pb, "damr");
260  ffio_wfourcc(pb, "FFMP");
261  avio_w8(pb, 0); /* decoder version */
262 
263  avio_wb16(pb, 0x81FF); /* Mode set (all modes for AMR_NB) */
264  avio_w8(pb, 0x00); /* Mode change period (no restriction) */
265  avio_w8(pb, 0x01); /* Frames per sample */
266  return 0x11;
267 }
268 
269 static int mov_write_ac3_tag(AVIOContext *pb, MOVTrack *track)
270 {
271  GetBitContext gbc;
272  PutBitContext pbc;
273  uint8_t buf[3];
274  int fscod, bsid, bsmod, acmod, lfeon, frmsizecod;
275 
276  if (track->vos_len < 7)
277  return -1;
278 
279  avio_wb32(pb, 11);
280  ffio_wfourcc(pb, "dac3");
281 
282  init_get_bits(&gbc, track->vos_data + 4, (track->vos_len - 4) * 8);
283  fscod = get_bits(&gbc, 2);
284  frmsizecod = get_bits(&gbc, 6);
285  bsid = get_bits(&gbc, 5);
286  bsmod = get_bits(&gbc, 3);
287  acmod = get_bits(&gbc, 3);
288  if (acmod == 2) {
289  skip_bits(&gbc, 2); // dsurmod
290  } else {
291  if ((acmod & 1) && acmod != 1)
292  skip_bits(&gbc, 2); // cmixlev
293  if (acmod & 4)
294  skip_bits(&gbc, 2); // surmixlev
295  }
296  lfeon = get_bits1(&gbc);
297 
298  init_put_bits(&pbc, buf, sizeof(buf));
299  put_bits(&pbc, 2, fscod);
300  put_bits(&pbc, 5, bsid);
301  put_bits(&pbc, 3, bsmod);
302  put_bits(&pbc, 3, acmod);
303  put_bits(&pbc, 1, lfeon);
304  put_bits(&pbc, 5, frmsizecod >> 1); // bit_rate_code
305  put_bits(&pbc, 5, 0); // reserved
306 
307  flush_put_bits(&pbc);
308  avio_write(pb, buf, sizeof(buf));
309 
310  return 11;
311 }
312 
313 struct eac3_info {
317 
318  /* Layout of the EC3SpecificBox */
319  /* maximum bitrate */
320  uint16_t data_rate;
321  /* number of independent substreams */
323  struct {
324  /* sample rate code (see ff_ac3_sample_rate_tab) 2 bits */
326  /* bit stream identification 5 bits */
328  /* one bit reserved */
329  /* audio service mixing (not supported yet) 1 bit */
330  /* bit stream mode 3 bits */
332  /* audio coding mode 3 bits */
334  /* sub woofer on 1 bit */
336  /* 3 bits reserved */
337  /* number of dependent substreams associated with this substream 4 bits */
339  /* channel locations of the dependent substream(s), if any, 9 bits */
340  uint16_t chan_loc;
341  /* if there is no dependent substream, then one bit reserved instead */
342  } substream[1]; /* TODO: support 8 independent substreams */
343 };
344 
345 #if CONFIG_AC3_PARSER
346 static int handle_eac3(MOVMuxContext *mov, AVPacket *pkt, MOVTrack *track)
347 {
348  GetBitContext gbc;
349  AC3HeaderInfo tmp, *hdr = &tmp;
350  struct eac3_info *info;
351  int num_blocks;
352 
353  if (!track->eac3_priv && !(track->eac3_priv = av_mallocz(sizeof(*info))))
354  return AVERROR(ENOMEM);
355  info = track->eac3_priv;
356 
357  init_get_bits(&gbc, pkt->data, pkt->size * 8);
358  if (avpriv_ac3_parse_header(&gbc, &hdr) < 0) {
359  /* drop the packets until we see a good one */
360  if (!track->entry) {
361  av_log(mov, AV_LOG_WARNING, "Dropping invalid packet from start of the stream\n");
362  return 0;
363  }
364  return AVERROR_INVALIDDATA;
365  }
366 
367  info->data_rate = FFMAX(info->data_rate, hdr->bit_rate / 1000);
368  num_blocks = hdr->num_blocks;
369 
370  if (!info->ec3_done) {
371  /* AC-3 substream must be the first one */
372  if (hdr->bitstream_id <= 10 && hdr->substreamid != 0)
373  return AVERROR(EINVAL);
374 
375  /* this should always be the case, given that our AC-3 parser
376  * concatenates dependent frames to their independent parent */
378  /* substream ids must be incremental */
379  if (hdr->substreamid > info->num_ind_sub + 1)
380  return AVERROR(EINVAL);
381 
382  if (hdr->substreamid == info->num_ind_sub + 1) {
383  //info->num_ind_sub++;
384  avpriv_request_sample(track->par, "Multiple independent substreams");
385  return AVERROR_PATCHWELCOME;
386  } else if (hdr->substreamid < info->num_ind_sub ||
387  hdr->substreamid == 0 && info->substream[0].bsid) {
388  info->ec3_done = 1;
389  goto concatenate;
390  }
391  }
392 
393  /* fill the info needed for the "dec3" atom */
394  info->substream[hdr->substreamid].fscod = hdr->sr_code;
395  info->substream[hdr->substreamid].bsid = hdr->bitstream_id;
396  info->substream[hdr->substreamid].bsmod = hdr->bitstream_mode;
397  info->substream[hdr->substreamid].acmod = hdr->channel_mode;
398  info->substream[hdr->substreamid].lfeon = hdr->lfe_on;
399 
400  /* Parse dependent substream(s), if any */
401  if (pkt->size != hdr->frame_size) {
402  int cumul_size = hdr->frame_size;
403  int parent = hdr->substreamid;
404 
405  while (cumul_size != pkt->size) {
406  int i;
407  init_get_bits(&gbc, pkt->data + cumul_size, (pkt->size - cumul_size) * 8);
408  if (avpriv_ac3_parse_header(&gbc, &hdr) < 0)
409  return AVERROR_INVALIDDATA;
411  return AVERROR(EINVAL);
412  cumul_size += hdr->frame_size;
413  info->substream[parent].num_dep_sub++;
414 
415  /* header is parsed up to lfeon, but custom channel map may be needed */
416  /* skip bsid */
417  skip_bits(&gbc, 5);
418  /* skip volume control params */
419  for (i = 0; i < (hdr->channel_mode ? 1 : 2); i++) {
420  skip_bits(&gbc, 5); // skip dialog normalization
421  if (get_bits1(&gbc)) {
422  skip_bits(&gbc, 8); // skip compression gain word
423  }
424  }
425  /* get the dependent stream channel map, if exists */
426  if (get_bits1(&gbc))
427  info->substream[parent].chan_loc |= (get_bits(&gbc, 16) >> 5) & 0x1f;
428  else
429  info->substream[parent].chan_loc |= hdr->channel_mode;
430  }
431  }
432  }
433 
434 concatenate:
435  if (!info->num_blocks && num_blocks == 6)
436  return pkt->size;
437  else if (info->num_blocks + num_blocks > 6)
438  return AVERROR_INVALIDDATA;
439 
440  if (!info->num_blocks) {
441  int ret = av_packet_ref(&info->pkt, pkt);
442  if (ret < 0)
443  return ret;
444  info->num_blocks = num_blocks;
445  return 0;
446  } else {
447  int ret;
448  if ((ret = av_grow_packet(&info->pkt, pkt->size)) < 0)
449  return ret;
450  memcpy(info->pkt.data + info->pkt.size - pkt->size, pkt->data, pkt->size);
451  info->num_blocks += num_blocks;
452  info->pkt.duration += pkt->duration;
453  if ((ret = av_copy_packet_side_data(&info->pkt, pkt)) < 0)
454  return ret;
455  if (info->num_blocks != 6)
456  return 0;
457  av_packet_unref(pkt);
458  ret = av_packet_ref(pkt, &info->pkt);
459  if (ret < 0)
460  return ret;
461  av_packet_unref(&info->pkt);
462  info->num_blocks = 0;
463  }
464 
465  return pkt->size;
466 }
467 #endif
468 
469 static int mov_write_eac3_tag(AVIOContext *pb, MOVTrack *track)
470 {
471  PutBitContext pbc;
472  uint8_t *buf;
473  struct eac3_info *info;
474  int size, i;
475 
476  if (!track->eac3_priv)
477  return AVERROR(EINVAL);
478 
479  info = track->eac3_priv;
480  size = 2 + 4 * (info->num_ind_sub + 1);
481  buf = av_malloc(size);
482  if (!buf) {
483  size = AVERROR(ENOMEM);
484  goto end;
485  }
486 
487  init_put_bits(&pbc, buf, size);
488  put_bits(&pbc, 13, info->data_rate);
489  put_bits(&pbc, 3, info->num_ind_sub);
490  for (i = 0; i <= info->num_ind_sub; i++) {
491  put_bits(&pbc, 2, info->substream[i].fscod);
492  put_bits(&pbc, 5, info->substream[i].bsid);
493  put_bits(&pbc, 1, 0); /* reserved */
494  put_bits(&pbc, 1, 0); /* asvc */
495  put_bits(&pbc, 3, info->substream[i].bsmod);
496  put_bits(&pbc, 3, info->substream[i].acmod);
497  put_bits(&pbc, 1, info->substream[i].lfeon);
498  put_bits(&pbc, 5, 0); /* reserved */
499  put_bits(&pbc, 4, info->substream[i].num_dep_sub);
500  if (!info->substream[i].num_dep_sub) {
501  put_bits(&pbc, 1, 0); /* reserved */
502  size--;
503  } else {
504  put_bits(&pbc, 9, info->substream[i].chan_loc);
505  }
506  }
507  flush_put_bits(&pbc);
508 
509  avio_wb32(pb, size + 8);
510  ffio_wfourcc(pb, "dec3");
511  avio_write(pb, buf, size);
512 
513  av_free(buf);
514 
515 end:
516  av_packet_unref(&info->pkt);
517  av_freep(&track->eac3_priv);
518 
519  return size;
520 }
521 
522 /**
523  * This function writes extradata "as is".
524  * Extradata must be formatted like a valid atom (with size and tag).
525  */
527 {
528  avio_write(pb, track->par->extradata, track->par->extradata_size);
529  return track->par->extradata_size;
530 }
531 
533 {
534  avio_wb32(pb, 10);
535  ffio_wfourcc(pb, "enda");
536  avio_wb16(pb, 1); /* little endian */
537  return 10;
538 }
539 
541 {
542  avio_wb32(pb, 10);
543  ffio_wfourcc(pb, "enda");
544  avio_wb16(pb, 0); /* big endian */
545  return 10;
546 }
547 
548 static void put_descr(AVIOContext *pb, int tag, unsigned int size)
549 {
550  int i = 3;
551  avio_w8(pb, tag);
552  for (; i > 0; i--)
553  avio_w8(pb, (size >> (7 * i)) | 0x80);
554  avio_w8(pb, size & 0x7F);
555 }
556 
557 static unsigned compute_avg_bitrate(MOVTrack *track)
558 {
559  uint64_t size = 0;
560  int i;
561  if (!track->track_duration)
562  return 0;
563  for (i = 0; i < track->entry; i++)
564  size += track->cluster[i].size;
565  return size * 8 * track->timescale / track->track_duration;
566 }
567 
568 static int mov_write_esds_tag(AVIOContext *pb, MOVTrack *track) // Basic
569 {
570  AVCPBProperties *props;
571  int64_t pos = avio_tell(pb);
572  int decoder_specific_info_len = track->vos_len ? 5 + track->vos_len : 0;
573  unsigned avg_bitrate;
574 
575  avio_wb32(pb, 0); // size
576  ffio_wfourcc(pb, "esds");
577  avio_wb32(pb, 0); // Version
578 
579  // ES descriptor
580  put_descr(pb, 0x03, 3 + 5+13 + decoder_specific_info_len + 5+1);
581  avio_wb16(pb, track->track_id);
582  avio_w8(pb, 0x00); // flags (= no flags)
583 
584  // DecoderConfig descriptor
585  put_descr(pb, 0x04, 13 + decoder_specific_info_len);
586 
587  // Object type indication
588  if ((track->par->codec_id == AV_CODEC_ID_MP2 ||
589  track->par->codec_id == AV_CODEC_ID_MP3) &&
590  track->par->sample_rate > 24000)
591  avio_w8(pb, 0x6B); // 11172-3
592  else
594 
595  // the following fields is made of 6 bits to identify the streamtype (4 for video, 5 for audio)
596  // plus 1 bit to indicate upstream and 1 bit set to 1 (reserved)
597  if (track->par->codec_id == AV_CODEC_ID_DVD_SUBTITLE)
598  avio_w8(pb, (0x38 << 2) | 1); // flags (= NeroSubpicStream)
599  else if (track->par->codec_type == AVMEDIA_TYPE_AUDIO)
600  avio_w8(pb, 0x15); // flags (= Audiostream)
601  else
602  avio_w8(pb, 0x11); // flags (= Visualstream)
603 
605  NULL);
606 
607  avio_wb24(pb, props ? props->buffer_size / 8 : 0); // Buffersize DB
608 
609  avg_bitrate = compute_avg_bitrate(track);
610  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)
611  avio_wb32(pb, avg_bitrate);
612 
613  if (track->vos_len) {
614  // DecoderSpecific info descriptor
615  put_descr(pb, 0x05, track->vos_len);
616  avio_write(pb, track->vos_data, track->vos_len);
617  }
618 
619  // SL descriptor
620  put_descr(pb, 0x06, 1);
621  avio_w8(pb, 0x02);
622  return update_size(pb, pos);
623 }
624 
626 {
627  return codec_id == AV_CODEC_ID_PCM_S24LE ||
628  codec_id == AV_CODEC_ID_PCM_S32LE ||
629  codec_id == AV_CODEC_ID_PCM_F32LE ||
630  codec_id == AV_CODEC_ID_PCM_F64LE;
631 }
632 
634 {
635  return codec_id == AV_CODEC_ID_PCM_S24BE ||
636  codec_id == AV_CODEC_ID_PCM_S32BE ||
637  codec_id == AV_CODEC_ID_PCM_F32BE ||
638  codec_id == AV_CODEC_ID_PCM_F64BE;
639 }
640 
642 {
643  int ret;
644  int64_t pos = avio_tell(pb);
645  avio_wb32(pb, 0);
646  avio_wl32(pb, track->tag); // store it byteswapped
647  track->par->codec_tag = av_bswap16(track->tag >> 16);
648  if ((ret = ff_put_wav_header(s, pb, track->par, 0)) < 0)
649  return ret;
650  return update_size(pb, pos);
651 }
652 
654 {
655  int ret;
656  int64_t pos = avio_tell(pb);
657  avio_wb32(pb, 0);
658  ffio_wfourcc(pb, "wfex");
659  if ((ret = ff_put_wav_header(s, pb, track->st->codecpar, FF_PUT_WAV_HEADER_FORCE_WAVEFORMATEX)) < 0)
660  return ret;
661  return update_size(pb, pos);
662 }
663 
664 static int mov_write_dfla_tag(AVIOContext *pb, MOVTrack *track)
665 {
666  int64_t pos = avio_tell(pb);
667  avio_wb32(pb, 0);
668  ffio_wfourcc(pb, "dfLa");
669  avio_w8(pb, 0); /* version */
670  avio_wb24(pb, 0); /* flags */
671 
672  /* Expect the encoder to pass a METADATA_BLOCK_TYPE_STREAMINFO. */
673  if (track->par->extradata_size != FLAC_STREAMINFO_SIZE)
674  return AVERROR_INVALIDDATA;
675 
676  /* TODO: Write other METADATA_BLOCK_TYPEs if the encoder makes them available. */
677  avio_w8(pb, 1 << 7 | FLAC_METADATA_TYPE_STREAMINFO); /* LastMetadataBlockFlag << 7 | BlockType */
678  avio_wb24(pb, track->par->extradata_size); /* Length */
679  avio_write(pb, track->par->extradata, track->par->extradata_size); /* BlockData[Length] */
680 
681  return update_size(pb, pos);
682 }
683 
684 static int mov_write_dops_tag(AVIOContext *pb, MOVTrack *track)
685 {
686  int64_t pos = avio_tell(pb);
687  avio_wb32(pb, 0);
688  ffio_wfourcc(pb, "dOps");
689  avio_w8(pb, 0); /* Version */
690  if (track->par->extradata_size < 19) {
691  av_log(pb, AV_LOG_ERROR, "invalid extradata size\n");
692  return AVERROR_INVALIDDATA;
693  }
694  /* extradata contains an Ogg OpusHead, other than byte-ordering and
695  OpusHead's preceeding magic/version, OpusSpecificBox is currently
696  identical. */
697  avio_w8(pb, AV_RB8(track->par->extradata + 9)); /* OuputChannelCount */
698  avio_wb16(pb, AV_RL16(track->par->extradata + 10)); /* PreSkip */
699  avio_wb32(pb, AV_RL32(track->par->extradata + 12)); /* InputSampleRate */
700  avio_wb16(pb, AV_RL16(track->par->extradata + 16)); /* OutputGain */
701  /* Write the rest of the header out without byte-swapping. */
702  avio_write(pb, track->par->extradata + 18, track->par->extradata_size - 18);
703 
704  return update_size(pb, pos);
705 }
706 
708 {
709  uint32_t layout_tag, bitmap;
710  int64_t pos = avio_tell(pb);
711 
712  layout_tag = ff_mov_get_channel_layout_tag(track->par->codec_id,
713  track->par->channel_layout,
714  &bitmap);
715  if (!layout_tag) {
716  av_log(s, AV_LOG_WARNING, "not writing 'chan' tag due to "
717  "lack of channel information\n");
718  return 0;
719  }
720 
721  if (track->multichannel_as_mono)
722  return 0;
723 
724  avio_wb32(pb, 0); // Size
725  ffio_wfourcc(pb, "chan"); // Type
726  avio_w8(pb, 0); // Version
727  avio_wb24(pb, 0); // Flags
728  avio_wb32(pb, layout_tag); // mChannelLayoutTag
729  avio_wb32(pb, bitmap); // mChannelBitmap
730  avio_wb32(pb, 0); // mNumberChannelDescriptions
731 
732  return update_size(pb, pos);
733 }
734 
736 {
737  int64_t pos = avio_tell(pb);
738 
739  avio_wb32(pb, 0); /* size */
740  ffio_wfourcc(pb, "wave");
741 
742  if (track->par->codec_id != AV_CODEC_ID_QDM2) {
743  avio_wb32(pb, 12); /* size */
744  ffio_wfourcc(pb, "frma");
745  avio_wl32(pb, track->tag);
746  }
747 
748  if (track->par->codec_id == AV_CODEC_ID_AAC) {
749  /* useless atom needed by mplayer, ipod, not needed by quicktime */
750  avio_wb32(pb, 12); /* size */
751  ffio_wfourcc(pb, "mp4a");
752  avio_wb32(pb, 0);
753  mov_write_esds_tag(pb, track);
754  } else if (mov_pcm_le_gt16(track->par->codec_id)) {
755  mov_write_enda_tag(pb);
756  } else if (mov_pcm_be_gt16(track->par->codec_id)) {
758  } else if (track->par->codec_id == AV_CODEC_ID_AMR_NB) {
759  mov_write_amr_tag(pb, track);
760  } else if (track->par->codec_id == AV_CODEC_ID_AC3) {
761  mov_write_ac3_tag(pb, track);
762  } else if (track->par->codec_id == AV_CODEC_ID_EAC3) {
763  mov_write_eac3_tag(pb, track);
764  } else if (track->par->codec_id == AV_CODEC_ID_ALAC ||
765  track->par->codec_id == AV_CODEC_ID_QDM2) {
766  mov_write_extradata_tag(pb, track);
767  } else if (track->par->codec_id == AV_CODEC_ID_ADPCM_MS ||
769  mov_write_ms_tag(s, pb, track);
770  }
771 
772  avio_wb32(pb, 8); /* size */
773  avio_wb32(pb, 0); /* null tag */
774 
775  return update_size(pb, pos);
776 }
777 
779 {
780  uint8_t *unescaped;
781  const uint8_t *start, *next, *end = track->vos_data + track->vos_len;
782  int unescaped_size, seq_found = 0;
783  int level = 0, interlace = 0;
784  int packet_seq = track->vc1_info.packet_seq;
785  int packet_entry = track->vc1_info.packet_entry;
786  int slices = track->vc1_info.slices;
787  PutBitContext pbc;
788 
789  if (track->start_dts == AV_NOPTS_VALUE) {
790  /* No packets written yet, vc1_info isn't authoritative yet. */
791  /* Assume inline sequence and entry headers. */
792  packet_seq = packet_entry = 1;
794  "moov atom written before any packets, unable to write correct "
795  "dvc1 atom. Set the delay_moov flag to fix this.\n");
796  }
797 
798  unescaped = av_mallocz(track->vos_len + AV_INPUT_BUFFER_PADDING_SIZE);
799  if (!unescaped)
800  return AVERROR(ENOMEM);
801  start = find_next_marker(track->vos_data, end);
802  for (next = start; next < end; start = next) {
803  GetBitContext gb;
804  int size;
805  next = find_next_marker(start + 4, end);
806  size = next - start - 4;
807  if (size <= 0)
808  continue;
809  unescaped_size = vc1_unescape_buffer(start + 4, size, unescaped);
810  init_get_bits(&gb, unescaped, 8 * unescaped_size);
811  if (AV_RB32(start) == VC1_CODE_SEQHDR) {
812  int profile = get_bits(&gb, 2);
813  if (profile != PROFILE_ADVANCED) {
814  av_free(unescaped);
815  return AVERROR(ENOSYS);
816  }
817  seq_found = 1;
818  level = get_bits(&gb, 3);
819  /* chromaformat, frmrtq_postproc, bitrtq_postproc, postprocflag,
820  * width, height */
821  skip_bits_long(&gb, 2 + 3 + 5 + 1 + 2*12);
822  skip_bits(&gb, 1); /* broadcast */
823  interlace = get_bits1(&gb);
824  skip_bits(&gb, 4); /* tfcntrflag, finterpflag, reserved, psf */
825  }
826  }
827  if (!seq_found) {
828  av_free(unescaped);
829  return AVERROR(ENOSYS);
830  }
831 
832  init_put_bits(&pbc, buf, 7);
833  /* VC1DecSpecStruc */
834  put_bits(&pbc, 4, 12); /* profile - advanced */
835  put_bits(&pbc, 3, level);
836  put_bits(&pbc, 1, 0); /* reserved */
837  /* VC1AdvDecSpecStruc */
838  put_bits(&pbc, 3, level);
839  put_bits(&pbc, 1, 0); /* cbr */
840  put_bits(&pbc, 6, 0); /* reserved */
841  put_bits(&pbc, 1, !interlace); /* no interlace */
842  put_bits(&pbc, 1, !packet_seq); /* no multiple seq */
843  put_bits(&pbc, 1, !packet_entry); /* no multiple entry */
844  put_bits(&pbc, 1, !slices); /* no slice code */
845  put_bits(&pbc, 1, 0); /* no bframe */
846  put_bits(&pbc, 1, 0); /* reserved */
847 
848  /* framerate */
849  if (track->st->avg_frame_rate.num > 0 && track->st->avg_frame_rate.den > 0)
850  put_bits32(&pbc, track->st->avg_frame_rate.num / track->st->avg_frame_rate.den);
851  else
852  put_bits32(&pbc, 0xffffffff);
853 
854  flush_put_bits(&pbc);
855 
856  av_free(unescaped);
857 
858  return 0;
859 }
860 
861 static int mov_write_dvc1_tag(AVIOContext *pb, MOVTrack *track)
862 {
863  uint8_t buf[7] = { 0 };
864  int ret;
865 
866  if ((ret = mov_write_dvc1_structs(track, buf)) < 0)
867  return ret;
868 
869  avio_wb32(pb, track->vos_len + 8 + sizeof(buf));
870  ffio_wfourcc(pb, "dvc1");
871  avio_write(pb, buf, sizeof(buf));
872  avio_write(pb, track->vos_data, track->vos_len);
873 
874  return 0;
875 }
876 
877 static int mov_write_glbl_tag(AVIOContext *pb, MOVTrack *track)
878 {
879  avio_wb32(pb, track->vos_len + 8);
880  ffio_wfourcc(pb, "glbl");
881  avio_write(pb, track->vos_data, track->vos_len);
882  return 8 + track->vos_len;
883 }
884 
885 /**
886  * Compute flags for 'lpcm' tag.
887  * See CoreAudioTypes and AudioStreamBasicDescription at Apple.
888  */
890 {
891  switch (codec_id) {
894  return 11;
897  return 9;
898  case AV_CODEC_ID_PCM_U8:
899  return 10;
903  return 14;
904  case AV_CODEC_ID_PCM_S8:
908  return 12;
909  default:
910  return 0;
911  }
912 }
913 
914 static int get_cluster_duration(MOVTrack *track, int cluster_idx)
915 {
916  int64_t next_dts;
917 
918  if (cluster_idx >= track->entry)
919  return 0;
920 
921  if (cluster_idx + 1 == track->entry)
922  next_dts = track->track_duration + track->start_dts;
923  else
924  next_dts = track->cluster[cluster_idx + 1].dts;
925 
926  next_dts -= track->cluster[cluster_idx].dts;
927 
928  av_assert0(next_dts >= 0);
929  av_assert0(next_dts <= INT_MAX);
930 
931  return next_dts;
932 }
933 
935 {
936  int i, first_duration;
937 
938 // return track->par->frame_size;
939 
940  /* use 1 for raw PCM */
941  if (!track->audio_vbr)
942  return 1;
943 
944  /* check to see if duration is constant for all clusters */
945  if (!track->entry)
946  return 0;
947  first_duration = get_cluster_duration(track, 0);
948  for (i = 1; i < track->entry; i++) {
949  if (get_cluster_duration(track, i) != first_duration)
950  return 0;
951  }
952  return first_duration;
953 }
954 
956 {
957  int64_t pos = avio_tell(pb);
958  int version = 0;
959  uint32_t tag = track->tag;
960 
961  if (track->mode == MODE_MOV) {
962  if (track->timescale > UINT16_MAX) {
963  if (mov_get_lpcm_flags(track->par->codec_id))
964  tag = AV_RL32("lpcm");
965  version = 2;
966  } else if (track->audio_vbr || mov_pcm_le_gt16(track->par->codec_id) ||
967  mov_pcm_be_gt16(track->par->codec_id) ||
968  track->par->codec_id == AV_CODEC_ID_ADPCM_MS ||
970  track->par->codec_id == AV_CODEC_ID_QDM2) {
971  version = 1;
972  }
973  }
974 
975  avio_wb32(pb, 0); /* size */
976  if (mov->encryption_scheme != MOV_ENC_NONE) {
977  ffio_wfourcc(pb, "enca");
978  } else {
979  avio_wl32(pb, tag); // store it byteswapped
980  }
981  avio_wb32(pb, 0); /* Reserved */
982  avio_wb16(pb, 0); /* Reserved */
983  avio_wb16(pb, 1); /* Data-reference index, XXX == 1 */
984 
985  /* SoundDescription */
986  avio_wb16(pb, version); /* Version */
987  avio_wb16(pb, 0); /* Revision level */
988  avio_wb32(pb, 0); /* Reserved */
989 
990  if (version == 2) {
991  avio_wb16(pb, 3);
992  avio_wb16(pb, 16);
993  avio_wb16(pb, 0xfffe);
994  avio_wb16(pb, 0);
995  avio_wb32(pb, 0x00010000);
996  avio_wb32(pb, 72);
997  avio_wb64(pb, av_double2int(track->par->sample_rate));
998  avio_wb32(pb, track->par->channels);
999  avio_wb32(pb, 0x7F000000);
1001  avio_wb32(pb, mov_get_lpcm_flags(track->par->codec_id));
1002  avio_wb32(pb, track->sample_size);
1003  avio_wb32(pb, get_samples_per_packet(track));
1004  } else {
1005  if (track->mode == MODE_MOV) {
1006  avio_wb16(pb, track->par->channels);
1007  if (track->par->codec_id == AV_CODEC_ID_PCM_U8 ||
1008  track->par->codec_id == AV_CODEC_ID_PCM_S8)
1009  avio_wb16(pb, 8); /* bits per sample */
1010  else if (track->par->codec_id == AV_CODEC_ID_ADPCM_G726)
1011  avio_wb16(pb, track->par->bits_per_coded_sample);
1012  else
1013  avio_wb16(pb, 16);
1014  avio_wb16(pb, track->audio_vbr ? -2 : 0); /* compression ID */
1015  } else { /* reserved for mp4/3gp */
1016  if (track->par->codec_id == AV_CODEC_ID_FLAC ||
1017  track->par->codec_id == AV_CODEC_ID_OPUS) {
1018  avio_wb16(pb, track->par->channels);
1019  } else {
1020  avio_wb16(pb, 2);
1021  }
1022  if (track->par->codec_id == AV_CODEC_ID_FLAC) {
1023  avio_wb16(pb, track->par->bits_per_raw_sample);
1024  } else {
1025  avio_wb16(pb, 16);
1026  }
1027  avio_wb16(pb, 0);
1028  }
1029 
1030  avio_wb16(pb, 0); /* packet size (= 0) */
1031  if (track->par->codec_id == AV_CODEC_ID_OPUS)
1032  avio_wb16(pb, 48000);
1033  else
1034  avio_wb16(pb, track->par->sample_rate <= UINT16_MAX ?
1035  track->par->sample_rate : 0);
1036  avio_wb16(pb, 0); /* Reserved */
1037  }
1038 
1039  if (version == 1) { /* SoundDescription V1 extended info */
1040  if (mov_pcm_le_gt16(track->par->codec_id) ||
1041  mov_pcm_be_gt16(track->par->codec_id))
1042  avio_wb32(pb, 1); /* must be 1 for uncompressed formats */
1043  else
1044  avio_wb32(pb, track->par->frame_size); /* Samples per packet */
1045  avio_wb32(pb, track->sample_size / track->par->channels); /* Bytes per packet */
1046  avio_wb32(pb, track->sample_size); /* Bytes per frame */
1047  avio_wb32(pb, 2); /* Bytes per sample */
1048  }
1049 
1050  if (track->mode == MODE_MOV &&
1051  (track->par->codec_id == AV_CODEC_ID_AAC ||
1052  track->par->codec_id == AV_CODEC_ID_AC3 ||
1053  track->par->codec_id == AV_CODEC_ID_EAC3 ||
1054  track->par->codec_id == AV_CODEC_ID_AMR_NB ||
1055  track->par->codec_id == AV_CODEC_ID_ALAC ||
1056  track->par->codec_id == AV_CODEC_ID_ADPCM_MS ||
1057  track->par->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV ||
1058  track->par->codec_id == AV_CODEC_ID_QDM2 ||
1059  (mov_pcm_le_gt16(track->par->codec_id) && version==1) ||
1060  (mov_pcm_be_gt16(track->par->codec_id) && version==1)))
1061  mov_write_wave_tag(s, pb, track);
1062  else if (track->tag == MKTAG('m','p','4','a'))
1063  mov_write_esds_tag(pb, track);
1064  else if (track->par->codec_id == AV_CODEC_ID_AMR_NB)
1065  mov_write_amr_tag(pb, track);
1066  else if (track->par->codec_id == AV_CODEC_ID_AC3)
1067  mov_write_ac3_tag(pb, track);
1068  else if (track->par->codec_id == AV_CODEC_ID_EAC3)
1069  mov_write_eac3_tag(pb, track);
1070  else if (track->par->codec_id == AV_CODEC_ID_ALAC)
1071  mov_write_extradata_tag(pb, track);
1072  else if (track->par->codec_id == AV_CODEC_ID_WMAPRO)
1073  mov_write_wfex_tag(s, pb, track);
1074  else if (track->par->codec_id == AV_CODEC_ID_FLAC)
1075  mov_write_dfla_tag(pb, track);
1076  else if (track->par->codec_id == AV_CODEC_ID_OPUS)
1077  mov_write_dops_tag(pb, track);
1078  else if (track->vos_len > 0)
1079  mov_write_glbl_tag(pb, track);
1080 
1081  if (track->mode == MODE_MOV && track->par->codec_type == AVMEDIA_TYPE_AUDIO)
1082  mov_write_chan_tag(s, pb, track);
1083 
1084  if (mov->encryption_scheme != MOV_ENC_NONE) {
1085  ff_mov_cenc_write_sinf_tag(track, pb, mov->encryption_kid);
1086  }
1087 
1088  return update_size(pb, pos);
1089 }
1090 
1092 {
1093  avio_wb32(pb, 0xf); /* size */
1094  ffio_wfourcc(pb, "d263");
1095  ffio_wfourcc(pb, "FFMP");
1096  avio_w8(pb, 0); /* decoder version */
1097  /* FIXME use AVCodecContext level/profile, when encoder will set values */
1098  avio_w8(pb, 0xa); /* level */
1099  avio_w8(pb, 0); /* profile */
1100  return 0xf;
1101 }
1102 
1103 static int mov_write_avcc_tag(AVIOContext *pb, MOVTrack *track)
1104 {
1105  int64_t pos = avio_tell(pb);
1106 
1107  avio_wb32(pb, 0);
1108  ffio_wfourcc(pb, "avcC");
1109  ff_isom_write_avcc(pb, track->vos_data, track->vos_len);
1110  return update_size(pb, pos);
1111 }
1112 
1114 {
1115  int64_t pos = avio_tell(pb);
1116 
1117  avio_wb32(pb, 0);
1118  ffio_wfourcc(pb, "vpcC");
1119  avio_w8(pb, 1); /* version */
1120  avio_wb24(pb, 0); /* flags */
1121  ff_isom_write_vpcc(s, pb, track->par);
1122  return update_size(pb, pos);
1123 }
1124 
1125 static int mov_write_hvcc_tag(AVIOContext *pb, MOVTrack *track)
1126 {
1127  int64_t pos = avio_tell(pb);
1128 
1129  avio_wb32(pb, 0);
1130  ffio_wfourcc(pb, "hvcC");
1131  if (track->tag == MKTAG('h','v','c','1'))
1132  ff_isom_write_hvcc(pb, track->vos_data, track->vos_len, 1);
1133  else
1134  ff_isom_write_hvcc(pb, track->vos_data, track->vos_len, 0);
1135  return update_size(pb, pos);
1136 }
1137 
1138 /* also used by all avid codecs (dv, imx, meridien) and their variants */
1139 static int mov_write_avid_tag(AVIOContext *pb, MOVTrack *track)
1140 {
1141  int i;
1142  int interlaced;
1143  int cid;
1144  int display_width = track->par->width;
1145 
1146  if (track->vos_data && track->vos_len > 0x29) {
1147  if (ff_dnxhd_parse_header_prefix(track->vos_data) != 0) {
1148  /* looks like a DNxHD bit stream */
1149  interlaced = (track->vos_data[5] & 2);
1150  cid = AV_RB32(track->vos_data + 0x28);
1151  } else {
1152  av_log(NULL, AV_LOG_WARNING, "Could not locate DNxHD bit stream in vos_data\n");
1153  return 0;
1154  }
1155  } else {
1156  av_log(NULL, AV_LOG_WARNING, "Could not locate DNxHD bit stream, vos_data too small\n");
1157  return 0;
1158  }
1159 
1160  avio_wb32(pb, 24); /* size */
1161  ffio_wfourcc(pb, "ACLR");
1162  ffio_wfourcc(pb, "ACLR");
1163  ffio_wfourcc(pb, "0001");
1164  if (track->par->color_range == AVCOL_RANGE_MPEG || /* Legal range (16-235) */
1166  avio_wb32(pb, 1); /* Corresponds to 709 in official encoder */
1167  } else { /* Full range (0-255) */
1168  avio_wb32(pb, 2); /* Corresponds to RGB in official encoder */
1169  }
1170  avio_wb32(pb, 0); /* unknown */
1171 
1172  if (track->tag == MKTAG('A','V','d','h')) {
1173  avio_wb32(pb, 32);
1174  ffio_wfourcc(pb, "ADHR");
1175  ffio_wfourcc(pb, "0001");
1176  avio_wb32(pb, cid);
1177  avio_wb32(pb, 0); /* unknown */
1178  avio_wb32(pb, 1); /* unknown */
1179  avio_wb32(pb, 0); /* unknown */
1180  avio_wb32(pb, 0); /* unknown */
1181  return 0;
1182  }
1183 
1184  avio_wb32(pb, 24); /* size */
1185  ffio_wfourcc(pb, "APRG");
1186  ffio_wfourcc(pb, "APRG");
1187  ffio_wfourcc(pb, "0001");
1188  avio_wb32(pb, 1); /* unknown */
1189  avio_wb32(pb, 0); /* unknown */
1190 
1191  avio_wb32(pb, 120); /* size */
1192  ffio_wfourcc(pb, "ARES");
1193  ffio_wfourcc(pb, "ARES");
1194  ffio_wfourcc(pb, "0001");
1195  avio_wb32(pb, cid); /* dnxhd cid, some id ? */
1196  if ( track->par->sample_aspect_ratio.num > 0
1197  && track->par->sample_aspect_ratio.den > 0)
1198  display_width = display_width * track->par->sample_aspect_ratio.num / track->par->sample_aspect_ratio.den;
1199  avio_wb32(pb, display_width);
1200  /* values below are based on samples created with quicktime and avid codecs */
1201  if (interlaced) {
1202  avio_wb32(pb, track->par->height / 2);
1203  avio_wb32(pb, 2); /* unknown */
1204  avio_wb32(pb, 0); /* unknown */
1205  avio_wb32(pb, 4); /* unknown */
1206  } else {
1207  avio_wb32(pb, track->par->height);
1208  avio_wb32(pb, 1); /* unknown */
1209  avio_wb32(pb, 0); /* unknown */
1210  if (track->par->height == 1080)
1211  avio_wb32(pb, 5); /* unknown */
1212  else
1213  avio_wb32(pb, 6); /* unknown */
1214  }
1215  /* padding */
1216  for (i = 0; i < 10; i++)
1217  avio_wb64(pb, 0);
1218 
1219  return 0;
1220 }
1221 
1222 static int mov_write_dpxe_tag(AVIOContext *pb, MOVTrack *track)
1223 {
1224  avio_wb32(pb, 12);
1225  ffio_wfourcc(pb, "DpxE");
1226  if (track->par->extradata_size >= 12 &&
1227  !memcmp(&track->par->extradata[4], "DpxE", 4)) {
1228  avio_wb32(pb, track->par->extradata[11]);
1229  } else {
1230  avio_wb32(pb, 1);
1231  }
1232  return 0;
1233 }
1234 
1236 {
1237  int tag;
1238 
1239  if (track->par->width == 720) { /* SD */
1240  if (track->par->height == 480) { /* NTSC */
1241  if (track->par->format == AV_PIX_FMT_YUV422P) tag = MKTAG('d','v','5','n');
1242  else tag = MKTAG('d','v','c',' ');
1243  }else if (track->par->format == AV_PIX_FMT_YUV422P) tag = MKTAG('d','v','5','p');
1244  else if (track->par->format == AV_PIX_FMT_YUV420P) tag = MKTAG('d','v','c','p');
1245  else tag = MKTAG('d','v','p','p');
1246  } else if (track->par->height == 720) { /* HD 720 line */
1247  if (track->st->time_base.den == 50) tag = MKTAG('d','v','h','q');
1248  else tag = MKTAG('d','v','h','p');
1249  } else if (track->par->height == 1080) { /* HD 1080 line */
1250  if (track->st->time_base.den == 25) tag = MKTAG('d','v','h','5');
1251  else tag = MKTAG('d','v','h','6');
1252  } else {
1253  av_log(s, AV_LOG_ERROR, "unsupported height for dv codec\n");
1254  return 0;
1255  }
1256 
1257  return tag;
1258 }
1259 
1261 {
1262  AVRational rate = st->avg_frame_rate;
1263 
1264 #if FF_API_LAVF_AVCTX
1266  rate = av_inv_q(st->codec->time_base);
1267  if (av_timecode_check_frame_rate(rate) < 0) {
1268  av_log(s, AV_LOG_DEBUG, "timecode: tbc=%d/%d invalid, fallback on %d/%d\n",
1269  rate.num, rate.den, st->avg_frame_rate.num, st->avg_frame_rate.den);
1270  rate = st->avg_frame_rate;
1271  }
1273 #endif
1274 
1275  return rate;
1276 }
1277 
1279 {
1280  AVRational rational_framerate = find_fps(s, st);
1281  int rate = 0;
1282  if (rational_framerate.den != 0)
1283  rate = av_q2d(rational_framerate);
1284  return rate;
1285 }
1286 
1288 {
1289  int tag = track->par->codec_tag;
1291  AVStream *st = track->st;
1292  int rate = defined_frame_rate(s, st);
1293 
1294  if (!tag)
1295  tag = MKTAG('m', '2', 'v', '1'); //fallback tag
1296 
1297  if (track->par->format == AV_PIX_FMT_YUV420P) {
1298  if (track->par->width == 1280 && track->par->height == 720) {
1299  if (!interlaced) {
1300  if (rate == 24) tag = MKTAG('x','d','v','4');
1301  else if (rate == 25) tag = MKTAG('x','d','v','5');
1302  else if (rate == 30) tag = MKTAG('x','d','v','1');
1303  else if (rate == 50) tag = MKTAG('x','d','v','a');
1304  else if (rate == 60) tag = MKTAG('x','d','v','9');
1305  }
1306  } else if (track->par->width == 1440 && track->par->height == 1080) {
1307  if (!interlaced) {
1308  if (rate == 24) tag = MKTAG('x','d','v','6');
1309  else if (rate == 25) tag = MKTAG('x','d','v','7');
1310  else if (rate == 30) tag = MKTAG('x','d','v','8');
1311  } else {
1312  if (rate == 25) tag = MKTAG('x','d','v','3');
1313  else if (rate == 30) tag = MKTAG('x','d','v','2');
1314  }
1315  } else if (track->par->width == 1920 && track->par->height == 1080) {
1316  if (!interlaced) {
1317  if (rate == 24) tag = MKTAG('x','d','v','d');
1318  else if (rate == 25) tag = MKTAG('x','d','v','e');
1319  else if (rate == 30) tag = MKTAG('x','d','v','f');
1320  } else {
1321  if (rate == 25) tag = MKTAG('x','d','v','c');
1322  else if (rate == 30) tag = MKTAG('x','d','v','b');
1323  }
1324  }
1325  } else if (track->par->format == AV_PIX_FMT_YUV422P) {
1326  if (track->par->width == 1280 && track->par->height == 720) {
1327  if (!interlaced) {
1328  if (rate == 24) tag = MKTAG('x','d','5','4');
1329  else if (rate == 25) tag = MKTAG('x','d','5','5');
1330  else if (rate == 30) tag = MKTAG('x','d','5','1');
1331  else if (rate == 50) tag = MKTAG('x','d','5','a');
1332  else if (rate == 60) tag = MKTAG('x','d','5','9');
1333  }
1334  } else if (track->par->width == 1920 && track->par->height == 1080) {
1335  if (!interlaced) {
1336  if (rate == 24) tag = MKTAG('x','d','5','d');
1337  else if (rate == 25) tag = MKTAG('x','d','5','e');
1338  else if (rate == 30) tag = MKTAG('x','d','5','f');
1339  } else {
1340  if (rate == 25) tag = MKTAG('x','d','5','c');
1341  else if (rate == 30) tag = MKTAG('x','d','5','b');
1342  }
1343  }
1344  }
1345 
1346  return tag;
1347 }
1348 
1350 {
1351  int tag = track->par->codec_tag;
1353  AVStream *st = track->st;
1354  int rate = defined_frame_rate(s, st);
1355 
1356  if (!tag)
1357  tag = MKTAG('a', 'v', 'c', 'i'); //fallback tag
1358 
1359  if (track->par->format == AV_PIX_FMT_YUV420P10) {
1360  if (track->par->width == 960 && track->par->height == 720) {
1361  if (!interlaced) {
1362  if (rate == 24) tag = MKTAG('a','i','5','p');
1363  else if (rate == 25) tag = MKTAG('a','i','5','q');
1364  else if (rate == 30) tag = MKTAG('a','i','5','p');
1365  else if (rate == 50) tag = MKTAG('a','i','5','q');
1366  else if (rate == 60) tag = MKTAG('a','i','5','p');
1367  }
1368  } else if (track->par->width == 1440 && track->par->height == 1080) {
1369  if (!interlaced) {
1370  if (rate == 24) tag = MKTAG('a','i','5','3');
1371  else if (rate == 25) tag = MKTAG('a','i','5','2');
1372  else if (rate == 30) tag = MKTAG('a','i','5','3');
1373  } else {
1374  if (rate == 50) tag = MKTAG('a','i','5','5');
1375  else if (rate == 60) tag = MKTAG('a','i','5','6');
1376  }
1377  }
1378  } else if (track->par->format == AV_PIX_FMT_YUV422P10) {
1379  if (track->par->width == 1280 && track->par->height == 720) {
1380  if (!interlaced) {
1381  if (rate == 24) tag = MKTAG('a','i','1','p');
1382  else if (rate == 25) tag = MKTAG('a','i','1','q');
1383  else if (rate == 30) tag = MKTAG('a','i','1','p');
1384  else if (rate == 50) tag = MKTAG('a','i','1','q');
1385  else if (rate == 60) tag = MKTAG('a','i','1','p');
1386  }
1387  } else if (track->par->width == 1920 && track->par->height == 1080) {
1388  if (!interlaced) {
1389  if (rate == 24) tag = MKTAG('a','i','1','3');
1390  else if (rate == 25) tag = MKTAG('a','i','1','2');
1391  else if (rate == 30) tag = MKTAG('a','i','1','3');
1392  } else {
1393  if (rate == 25) tag = MKTAG('a','i','1','5');
1394  else if (rate == 50) tag = MKTAG('a','i','1','5');
1395  else if (rate == 60) tag = MKTAG('a','i','1','6');
1396  }
1397  } else if ( track->par->width == 4096 && track->par->height == 2160
1398  || track->par->width == 3840 && track->par->height == 2160
1399  || track->par->width == 2048 && track->par->height == 1080) {
1400  tag = MKTAG('a','i','v','x');
1401  }
1402  }
1403 
1404  return tag;
1405 }
1406 
1407 static const struct {
1409  uint32_t tag;
1410  unsigned bps;
1411 } mov_pix_fmt_tags[] = {
1412  { AV_PIX_FMT_YUYV422, MKTAG('y','u','v','2'), 0 },
1413  { AV_PIX_FMT_YUYV422, MKTAG('y','u','v','s'), 0 },
1414  { AV_PIX_FMT_UYVY422, MKTAG('2','v','u','y'), 0 },
1415  { AV_PIX_FMT_RGB555BE,MKTAG('r','a','w',' '), 16 },
1416  { AV_PIX_FMT_RGB555LE,MKTAG('L','5','5','5'), 16 },
1417  { AV_PIX_FMT_RGB565LE,MKTAG('L','5','6','5'), 16 },
1418  { AV_PIX_FMT_RGB565BE,MKTAG('B','5','6','5'), 16 },
1419  { AV_PIX_FMT_GRAY16BE,MKTAG('b','1','6','g'), 16 },
1420  { AV_PIX_FMT_RGB24, MKTAG('r','a','w',' '), 24 },
1421  { AV_PIX_FMT_BGR24, MKTAG('2','4','B','G'), 24 },
1422  { AV_PIX_FMT_ARGB, MKTAG('r','a','w',' '), 32 },
1423  { AV_PIX_FMT_BGRA, MKTAG('B','G','R','A'), 32 },
1424  { AV_PIX_FMT_RGBA, MKTAG('R','G','B','A'), 32 },
1425  { AV_PIX_FMT_ABGR, MKTAG('A','B','G','R'), 32 },
1426  { AV_PIX_FMT_RGB48BE, MKTAG('b','4','8','r'), 48 },
1427 };
1428 
1430 {
1431  int tag = MKTAG('A','V','d','n');
1432  if (track->par->profile != FF_PROFILE_UNKNOWN &&
1433  track->par->profile != FF_PROFILE_DNXHD)
1434  tag = MKTAG('A','V','d','h');
1435  return tag;
1436 }
1437 
1439 {
1440  int tag = track->par->codec_tag;
1441  int i;
1442  enum AVPixelFormat pix_fmt;
1443 
1444  for (i = 0; i < FF_ARRAY_ELEMS(mov_pix_fmt_tags); i++) {
1445  if (track->par->format == mov_pix_fmt_tags[i].pix_fmt) {
1446  tag = mov_pix_fmt_tags[i].tag;
1447  track->par->bits_per_coded_sample = mov_pix_fmt_tags[i].bps;
1448  if (track->par->codec_tag == mov_pix_fmt_tags[i].tag)
1449  break;
1450  }
1451  }
1452 
1454  track->par->bits_per_coded_sample);
1455  if (tag == MKTAG('r','a','w',' ') &&
1456  track->par->format != pix_fmt &&
1457  track->par->format != AV_PIX_FMT_GRAY8 &&
1458  track->par->format != AV_PIX_FMT_NONE)
1459  av_log(s, AV_LOG_ERROR, "%s rawvideo cannot be written to mov, output file will be unreadable\n",
1460  av_get_pix_fmt_name(track->par->format));
1461  return tag;
1462 }
1463 
1465 {
1466  int tag = track->par->codec_tag;
1467 
1468  if (!tag || (s->strict_std_compliance >= FF_COMPLIANCE_NORMAL &&
1469  (track->par->codec_id == AV_CODEC_ID_DVVIDEO ||
1470  track->par->codec_id == AV_CODEC_ID_RAWVIDEO ||
1471  track->par->codec_id == AV_CODEC_ID_H263 ||
1472  track->par->codec_id == AV_CODEC_ID_H264 ||
1473  track->par->codec_id == AV_CODEC_ID_DNXHD ||
1474  track->par->codec_id == AV_CODEC_ID_MPEG2VIDEO ||
1475  av_get_bits_per_sample(track->par->codec_id)))) { // pcm audio
1476  if (track->par->codec_id == AV_CODEC_ID_DVVIDEO)
1477  tag = mov_get_dv_codec_tag(s, track);
1478  else if (track->par->codec_id == AV_CODEC_ID_RAWVIDEO)
1479  tag = mov_get_rawvideo_codec_tag(s, track);
1480  else if (track->par->codec_id == AV_CODEC_ID_MPEG2VIDEO)
1481  tag = mov_get_mpeg2_xdcam_codec_tag(s, track);
1482  else if (track->par->codec_id == AV_CODEC_ID_H264)
1483  tag = mov_get_h264_codec_tag(s, track);
1484  else if (track->par->codec_id == AV_CODEC_ID_DNXHD)
1485  tag = mov_get_dnxhd_codec_tag(s, track);
1486  else if (track->par->codec_type == AVMEDIA_TYPE_VIDEO) {
1488  if (!tag) { // if no mac fcc found, try with Microsoft tags
1490  if (tag)
1491  av_log(s, AV_LOG_WARNING, "Using MS style video codec tag, "
1492  "the file may be unplayable!\n");
1493  }
1494  } else if (track->par->codec_type == AVMEDIA_TYPE_AUDIO) {
1496  if (!tag) { // if no mac fcc found, try with Microsoft tags
1497  int ms_tag = ff_codec_get_tag(ff_codec_wav_tags, track->par->codec_id);
1498  if (ms_tag) {
1499  tag = MKTAG('m', 's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
1500  av_log(s, AV_LOG_WARNING, "Using MS style audio codec tag, "
1501  "the file may be unplayable!\n");
1502  }
1503  }
1504  } else if (track->par->codec_type == AVMEDIA_TYPE_SUBTITLE)
1506  }
1507 
1508  return tag;
1509 }
1510 
1512 {
1513  int tag;
1514 
1515  if (track->mode == MODE_MP4 || track->mode == MODE_PSP)
1516  tag = track->par->codec_tag;
1517  else if (track->mode == MODE_ISM)
1518  tag = track->par->codec_tag;
1519  else if (track->mode == MODE_IPOD) {
1520  if (!av_match_ext(s->filename, "m4a") &&
1521  !av_match_ext(s->filename, "m4v") &&
1522  !av_match_ext(s->filename, "m4b"))
1523  av_log(s, AV_LOG_WARNING, "Warning, extension is not .m4a nor .m4v "
1524  "Quicktime/Ipod might not play the file\n");
1525  tag = track->par->codec_tag;
1526  } else if (track->mode & MODE_3GP)
1527  tag = track->par->codec_tag;
1528  else if (track->mode == MODE_F4V)
1529  tag = track->par->codec_tag;
1530  else
1531  tag = mov_get_codec_tag(s, track);
1532 
1533  return tag;
1534 }
1535 
1536 /** Write uuid atom.
1537  * Needed to make file play in iPods running newest firmware
1538  * goes after avcC atom in moov.trak.mdia.minf.stbl.stsd.avc1
1539  */
1541 {
1542  avio_wb32(pb, 28);
1543  ffio_wfourcc(pb, "uuid");
1544  avio_wb32(pb, 0x6b6840f2);
1545  avio_wb32(pb, 0x5f244fc5);
1546  avio_wb32(pb, 0xba39a51b);
1547  avio_wb32(pb, 0xcf0323f3);
1548  avio_wb32(pb, 0x0);
1549  return 28;
1550 }
1551 
1552 static const uint16_t fiel_data[] = {
1553  0x0000, 0x0100, 0x0201, 0x0206, 0x0209, 0x020e
1554 };
1555 
1556 static int mov_write_fiel_tag(AVIOContext *pb, MOVTrack *track, int field_order)
1557 {
1558  unsigned mov_field_order = 0;
1559  if (field_order < FF_ARRAY_ELEMS(fiel_data))
1560  mov_field_order = fiel_data[field_order];
1561  else
1562  return 0;
1563  avio_wb32(pb, 10);
1564  ffio_wfourcc(pb, "fiel");
1565  avio_wb16(pb, mov_field_order);
1566  return 10;
1567 }
1568 
1570 {
1571  int64_t pos = avio_tell(pb);
1572  avio_wb32(pb, 0); /* size */
1573  avio_wl32(pb, track->tag); // store it byteswapped
1574  avio_wb32(pb, 0); /* Reserved */
1575  avio_wb16(pb, 0); /* Reserved */
1576  avio_wb16(pb, 1); /* Data-reference index */
1577 
1578  if (track->par->codec_id == AV_CODEC_ID_DVD_SUBTITLE)
1579  mov_write_esds_tag(pb, track);
1580  else if (track->par->extradata_size)
1581  avio_write(pb, track->par->extradata, track->par->extradata_size);
1582 
1583  return update_size(pb, pos);
1584 }
1585 
1586 static int mov_write_st3d_tag(AVIOContext *pb, AVStereo3D *stereo_3d)
1587 {
1588  int8_t stereo_mode;
1589 
1590  if (stereo_3d->flags != 0) {
1591  av_log(pb, AV_LOG_WARNING, "Unsupported stereo_3d flags %x. st3d not written.\n", stereo_3d->flags);
1592  return 0;
1593  }
1594 
1595  switch (stereo_3d->type) {
1596  case AV_STEREO3D_2D:
1597  stereo_mode = 0;
1598  break;
1599  case AV_STEREO3D_TOPBOTTOM:
1600  stereo_mode = 1;
1601  break;
1603  stereo_mode = 2;
1604  break;
1605  default:
1606  av_log(pb, AV_LOG_WARNING, "Unsupported stereo_3d type %s. st3d not written.\n", av_stereo3d_type_name(stereo_3d->type));
1607  return 0;
1608  }
1609  avio_wb32(pb, 13); /* size */
1610  ffio_wfourcc(pb, "st3d");
1611  avio_wb32(pb, 0); /* version = 0 & flags = 0 */
1612  avio_w8(pb, stereo_mode);
1613  return 13;
1614 }
1615 
1617 {
1618  int64_t sv3d_pos, svhd_pos, proj_pos;
1619  const char* metadata_source = s->flags & AVFMT_FLAG_BITEXACT ? "Lavf" : LIBAVFORMAT_IDENT;
1620 
1621  if (spherical_mapping->projection != AV_SPHERICAL_EQUIRECTANGULAR &&
1622  spherical_mapping->projection != AV_SPHERICAL_EQUIRECTANGULAR_TILE &&
1623  spherical_mapping->projection != AV_SPHERICAL_CUBEMAP) {
1624  av_log(pb, AV_LOG_WARNING, "Unsupported projection %d. sv3d not written.\n", spherical_mapping->projection);
1625  return 0;
1626  }
1627 
1628  sv3d_pos = avio_tell(pb);
1629  avio_wb32(pb, 0); /* size */
1630  ffio_wfourcc(pb, "sv3d");
1631 
1632  svhd_pos = avio_tell(pb);
1633  avio_wb32(pb, 0); /* size */
1634  ffio_wfourcc(pb, "svhd");
1635  avio_wb32(pb, 0); /* version = 0 & flags = 0 */
1636  avio_put_str(pb, metadata_source);
1637  update_size(pb, svhd_pos);
1638 
1639  proj_pos = avio_tell(pb);
1640  avio_wb32(pb, 0); /* size */
1641  ffio_wfourcc(pb, "proj");
1642 
1643  avio_wb32(pb, 24); /* size */
1644  ffio_wfourcc(pb, "prhd");
1645  avio_wb32(pb, 0); /* version = 0 & flags = 0 */
1646  avio_wb32(pb, spherical_mapping->yaw);
1647  avio_wb32(pb, spherical_mapping->pitch);
1648  avio_wb32(pb, spherical_mapping->roll);
1649 
1650  switch (spherical_mapping->projection) {
1653  avio_wb32(pb, 28); /* size */
1654  ffio_wfourcc(pb, "equi");
1655  avio_wb32(pb, 0); /* version = 0 & flags = 0 */
1656  avio_wb32(pb, spherical_mapping->bound_top);
1657  avio_wb32(pb, spherical_mapping->bound_bottom);
1658  avio_wb32(pb, spherical_mapping->bound_left);
1659  avio_wb32(pb, spherical_mapping->bound_right);
1660  break;
1661  case AV_SPHERICAL_CUBEMAP:
1662  avio_wb32(pb, 20); /* size */
1663  ffio_wfourcc(pb, "cbmp");
1664  avio_wb32(pb, 0); /* version = 0 & flags = 0 */
1665  avio_wb32(pb, 0); /* layout */
1666  avio_wb32(pb, spherical_mapping->padding); /* padding */
1667  break;
1668  }
1669  update_size(pb, proj_pos);
1670 
1671  return update_size(pb, sv3d_pos);
1672 }
1673 
1674 static int mov_write_pasp_tag(AVIOContext *pb, MOVTrack *track)
1675 {
1676  AVRational sar;
1677  av_reduce(&sar.num, &sar.den, track->par->sample_aspect_ratio.num,
1678  track->par->sample_aspect_ratio.den, INT_MAX);
1679 
1680  avio_wb32(pb, 16);
1681  ffio_wfourcc(pb, "pasp");
1682  avio_wb32(pb, sar.num);
1683  avio_wb32(pb, sar.den);
1684  return 16;
1685 }
1686 
1687 static int mov_write_gama_tag(AVIOContext *pb, MOVTrack *track, double gamma)
1688 {
1689  uint32_t gama = 0;
1690  if (gamma <= 0.0)
1691  {
1692  gamma = avpriv_get_gamma_from_trc(track->par->color_trc);
1693  }
1694  av_log(pb, AV_LOG_DEBUG, "gamma value %g\n", gamma);
1695 
1696  if (gamma > 1e-6) {
1697  gama = (uint32_t)lrint((double)(1<<16) * gamma);
1698  av_log(pb, AV_LOG_DEBUG, "writing gama value %"PRId32"\n", gama);
1699 
1700  av_assert0(track->mode == MODE_MOV);
1701  avio_wb32(pb, 12);
1702  ffio_wfourcc(pb, "gama");
1703  avio_wb32(pb, gama);
1704  return 12;
1705  }
1706  else {
1707  av_log(pb, AV_LOG_WARNING, "gamma value unknown, unable to write gama atom\n");
1708  }
1709  return 0;
1710 }
1711 
1712 static int mov_write_colr_tag(AVIOContext *pb, MOVTrack *track)
1713 {
1714  // Ref (MOV): https://developer.apple.com/library/mac/technotes/tn2162/_index.html#//apple_ref/doc/uid/DTS40013070-CH1-TNTAG9
1715  // Ref (MP4): ISO/IEC 14496-12:2012
1716 
1717  if (track->par->color_primaries == AVCOL_PRI_UNSPECIFIED &&
1718  track->par->color_trc == AVCOL_TRC_UNSPECIFIED &&
1719  track->par->color_space == AVCOL_SPC_UNSPECIFIED) {
1720  if ((track->par->width >= 1920 && track->par->height >= 1080)
1721  || (track->par->width == 1280 && track->par->height == 720)) {
1722  av_log(NULL, AV_LOG_WARNING, "color primaries unspecified, assuming bt709\n");
1724  } else if (track->par->width == 720 && track->height == 576) {
1725  av_log(NULL, AV_LOG_WARNING, "color primaries unspecified, assuming bt470bg\n");
1727  } else if (track->par->width == 720 &&
1728  (track->height == 486 || track->height == 480)) {
1729  av_log(NULL, AV_LOG_WARNING, "color primaries unspecified, assuming smpte170\n");
1731  } else {
1732  av_log(NULL, AV_LOG_WARNING, "color primaries unspecified, unable to assume anything\n");
1733  }
1734  switch (track->par->color_primaries) {
1735  case AVCOL_PRI_BT709:
1736  track->par->color_trc = AVCOL_TRC_BT709;
1737  track->par->color_space = AVCOL_SPC_BT709;
1738  break;
1739  case AVCOL_PRI_SMPTE170M:
1740  case AVCOL_PRI_BT470BG:
1741  track->par->color_trc = AVCOL_TRC_BT709;
1743  break;
1744  }
1745  }
1746 
1747  /* We should only ever be called by MOV or MP4. */
1748  av_assert0(track->mode == MODE_MOV || track->mode == MODE_MP4);
1749 
1750  avio_wb32(pb, 18 + (track->mode == MODE_MP4));
1751  ffio_wfourcc(pb, "colr");
1752  if (track->mode == MODE_MP4)
1753  ffio_wfourcc(pb, "nclx");
1754  else
1755  ffio_wfourcc(pb, "nclc");
1756  switch (track->par->color_primaries) {
1757  case AVCOL_PRI_BT709: avio_wb16(pb, 1); break;
1758  case AVCOL_PRI_SMPTE170M:
1759  case AVCOL_PRI_SMPTE240M: avio_wb16(pb, 6); break;
1760  case AVCOL_PRI_BT470BG: avio_wb16(pb, 5); break;
1761  default: avio_wb16(pb, 2);
1762  }
1763  switch (track->par->color_trc) {
1764  case AVCOL_TRC_BT709: avio_wb16(pb, 1); break;
1765  case AVCOL_TRC_SMPTE170M: avio_wb16(pb, 1); break; // remapped
1766  case AVCOL_TRC_SMPTE240M: avio_wb16(pb, 7); break;
1767  default: avio_wb16(pb, 2);
1768  }
1769  switch (track->par->color_space) {
1770  case AVCOL_SPC_BT709: avio_wb16(pb, 1); break;
1771  case AVCOL_SPC_BT470BG:
1772  case AVCOL_SPC_SMPTE170M: avio_wb16(pb, 6); break;
1773  case AVCOL_SPC_SMPTE240M: avio_wb16(pb, 7); break;
1774  default: avio_wb16(pb, 2);
1775  }
1776 
1777  if (track->mode == MODE_MP4) {
1778  int full_range = track->par->color_range == AVCOL_RANGE_JPEG;
1779  avio_w8(pb, full_range << 7);
1780  return 19;
1781  } else {
1782  return 18;
1783  }
1784 }
1785 
1786 static void find_compressor(char * compressor_name, int len, MOVTrack *track)
1787 {
1788  AVDictionaryEntry *encoder;
1789  int xdcam_res = (track->par->width == 1280 && track->par->height == 720)
1790  || (track->par->width == 1440 && track->par->height == 1080)
1791  || (track->par->width == 1920 && track->par->height == 1080);
1792 
1793  if (track->mode == MODE_MOV &&
1794  (encoder = av_dict_get(track->st->metadata, "encoder", NULL, 0))) {
1795  av_strlcpy(compressor_name, encoder->value, 32);
1796  } else if (track->par->codec_id == AV_CODEC_ID_MPEG2VIDEO && xdcam_res) {
1798  AVStream *st = track->st;
1799  int rate = defined_frame_rate(NULL, st);
1800  av_strlcatf(compressor_name, len, "XDCAM");
1801  if (track->par->format == AV_PIX_FMT_YUV422P) {
1802  av_strlcatf(compressor_name, len, " HD422");
1803  } else if(track->par->width == 1440) {
1804  av_strlcatf(compressor_name, len, " HD");
1805  } else
1806  av_strlcatf(compressor_name, len, " EX");
1807 
1808  av_strlcatf(compressor_name, len, " %d%c", track->par->height, interlaced ? 'i' : 'p');
1809 
1810  av_strlcatf(compressor_name, len, "%d", rate * (interlaced + 1));
1811  }
1812 }
1813 
1815 {
1816  int64_t pos = avio_tell(pb);
1817  char compressor_name[32] = { 0 };
1818  int avid = 0;
1819 
1820  avio_wb32(pb, 0); /* size */
1821  if (mov->encryption_scheme != MOV_ENC_NONE) {
1822  ffio_wfourcc(pb, "encv");
1823  } else {
1824  avio_wl32(pb, track->tag); // store it byteswapped
1825  }
1826  avio_wb32(pb, 0); /* Reserved */
1827  avio_wb16(pb, 0); /* Reserved */
1828  avio_wb16(pb, 1); /* Data-reference index */
1829 
1830  avio_wb16(pb, 0); /* Codec stream version */
1831  avio_wb16(pb, 0); /* Codec stream revision (=0) */
1832  if (track->mode == MODE_MOV) {
1833  ffio_wfourcc(pb, "FFMP"); /* Vendor */
1834  if (track->par->codec_id == AV_CODEC_ID_RAWVIDEO) {
1835  avio_wb32(pb, 0); /* Temporal Quality */
1836  avio_wb32(pb, 0x400); /* Spatial Quality = lossless*/
1837  } else {
1838  avio_wb32(pb, 0x200); /* Temporal Quality = normal */
1839  avio_wb32(pb, 0x200); /* Spatial Quality = normal */
1840  }
1841  } else {
1842  avio_wb32(pb, 0); /* Reserved */
1843  avio_wb32(pb, 0); /* Reserved */
1844  avio_wb32(pb, 0); /* Reserved */
1845  }
1846  avio_wb16(pb, track->par->width); /* Video width */
1847  avio_wb16(pb, track->height); /* Video height */
1848  avio_wb32(pb, 0x00480000); /* Horizontal resolution 72dpi */
1849  avio_wb32(pb, 0x00480000); /* Vertical resolution 72dpi */
1850  avio_wb32(pb, 0); /* Data size (= 0) */
1851  avio_wb16(pb, 1); /* Frame count (= 1) */
1852 
1853  /* FIXME not sure, ISO 14496-1 draft where it shall be set to 0 */
1854  find_compressor(compressor_name, 32, track);
1855  avio_w8(pb, strlen(compressor_name));
1856  avio_write(pb, compressor_name, 31);
1857 
1858  if (track->mode == MODE_MOV && track->par->bits_per_coded_sample)
1859  avio_wb16(pb, track->par->bits_per_coded_sample |
1860  (track->par->format == AV_PIX_FMT_GRAY8 ? 0x20 : 0));
1861  else
1862  avio_wb16(pb, 0x18); /* Reserved */
1863 
1864  if (track->mode == MODE_MOV && track->par->format == AV_PIX_FMT_PAL8) {
1865  int pal_size = 1 << track->par->bits_per_coded_sample;
1866  int i;
1867  avio_wb16(pb, 0); /* Color table ID */
1868  avio_wb32(pb, 0); /* Color table seed */
1869  avio_wb16(pb, 0x8000); /* Color table flags */
1870  avio_wb16(pb, pal_size - 1); /* Color table size (zero-relative) */
1871  for (i = 0; i < pal_size; i++) {
1872  uint32_t rgb = track->palette[i];
1873  uint16_t r = (rgb >> 16) & 0xff;
1874  uint16_t g = (rgb >> 8) & 0xff;
1875  uint16_t b = rgb & 0xff;
1876  avio_wb16(pb, 0);
1877  avio_wb16(pb, (r << 8) | r);
1878  avio_wb16(pb, (g << 8) | g);
1879  avio_wb16(pb, (b << 8) | b);
1880  }
1881  } else
1882  avio_wb16(pb, 0xffff); /* Reserved */
1883 
1884  if (track->tag == MKTAG('m','p','4','v'))
1885  mov_write_esds_tag(pb, track);
1886  else if (track->par->codec_id == AV_CODEC_ID_H263)
1887  mov_write_d263_tag(pb);
1888  else if (track->par->codec_id == AV_CODEC_ID_AVUI ||
1889  track->par->codec_id == AV_CODEC_ID_SVQ3) {
1890  mov_write_extradata_tag(pb, track);
1891  avio_wb32(pb, 0);
1892  } else if (track->par->codec_id == AV_CODEC_ID_DNXHD) {
1893  mov_write_avid_tag(pb, track);
1894  avid = 1;
1895  } else if (track->par->codec_id == AV_CODEC_ID_HEVC)
1896  mov_write_hvcc_tag(pb, track);
1897  else if (track->par->codec_id == AV_CODEC_ID_H264 && !TAG_IS_AVCI(track->tag)) {
1898  mov_write_avcc_tag(pb, track);
1899  if (track->mode == MODE_IPOD)
1901  } else if (track->par->codec_id == AV_CODEC_ID_VP9) {
1902  mov_write_vpcc_tag(mov->fc, pb, track);
1903  } else if (track->par->codec_id == AV_CODEC_ID_VC1 && track->vos_len > 0)
1904  mov_write_dvc1_tag(pb, track);
1905  else if (track->par->codec_id == AV_CODEC_ID_VP6F ||
1906  track->par->codec_id == AV_CODEC_ID_VP6A) {
1907  /* Don't write any potential extradata here - the cropping
1908  * is signalled via the normal width/height fields. */
1909  } else if (track->par->codec_id == AV_CODEC_ID_R10K) {
1910  if (track->par->codec_tag == MKTAG('R','1','0','k'))
1911  mov_write_dpxe_tag(pb, track);
1912  } else if (track->vos_len > 0)
1913  mov_write_glbl_tag(pb, track);
1914 
1915  if (track->par->codec_id != AV_CODEC_ID_H264 &&
1916  track->par->codec_id != AV_CODEC_ID_MPEG4 &&
1917  track->par->codec_id != AV_CODEC_ID_DNXHD) {
1918  int field_order = track->par->field_order;
1919 
1920 #if FF_API_LAVF_AVCTX
1922  if (field_order != track->st->codec->field_order && track->st->codec->field_order != AV_FIELD_UNKNOWN)
1923  field_order = track->st->codec->field_order;
1925 #endif
1926 
1927  if (field_order != AV_FIELD_UNKNOWN)
1928  mov_write_fiel_tag(pb, track, field_order);
1929  }
1930 
1931  if (mov->flags & FF_MOV_FLAG_WRITE_GAMA) {
1932  if (track->mode == MODE_MOV)
1933  mov_write_gama_tag(pb, track, mov->gamma);
1934  else
1935  av_log(mov->fc, AV_LOG_WARNING, "Not writing 'gama' atom. Format is not MOV.\n");
1936  }
1937  if (mov->flags & FF_MOV_FLAG_WRITE_COLR) {
1938  if (track->mode == MODE_MOV || track->mode == MODE_MP4)
1939  mov_write_colr_tag(pb, track);
1940  else
1941  av_log(mov->fc, AV_LOG_WARNING, "Not writing 'colr' atom. Format is not MOV or MP4.\n");
1942  }
1943 
1944  if (track->mode == MODE_MP4 && mov->fc->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL) {
1947 
1948  if (stereo_3d)
1949  mov_write_st3d_tag(pb, stereo_3d);
1950  if (spherical_mapping)
1951  mov_write_sv3d_tag(mov->fc, pb, spherical_mapping);
1952  }
1953 
1954  if (track->par->sample_aspect_ratio.den && track->par->sample_aspect_ratio.num) {
1955  mov_write_pasp_tag(pb, track);
1956  }
1957 
1958  if (mov->encryption_scheme != MOV_ENC_NONE) {
1959  ff_mov_cenc_write_sinf_tag(track, pb, mov->encryption_kid);
1960  }
1961 
1962  /* extra padding for avid stsd */
1963  /* https://developer.apple.com/library/mac/documentation/QuickTime/QTFF/QTFFChap2/qtff2.html#//apple_ref/doc/uid/TP40000939-CH204-61112 */
1964  if (avid)
1965  avio_wb32(pb, 0);
1966 
1967  return update_size(pb, pos);
1968 }
1969 
1970 static int mov_write_rtp_tag(AVIOContext *pb, MOVTrack *track)
1971 {
1972  int64_t pos = avio_tell(pb);
1973  avio_wb32(pb, 0); /* size */
1974  ffio_wfourcc(pb, "rtp ");
1975  avio_wb32(pb, 0); /* Reserved */
1976  avio_wb16(pb, 0); /* Reserved */
1977  avio_wb16(pb, 1); /* Data-reference index */
1978 
1979  avio_wb16(pb, 1); /* Hint track version */
1980  avio_wb16(pb, 1); /* Highest compatible version */
1981  avio_wb32(pb, track->max_packet_size); /* Max packet size */
1982 
1983  avio_wb32(pb, 12); /* size */
1984  ffio_wfourcc(pb, "tims");
1985  avio_wb32(pb, track->timescale);
1986 
1987  return update_size(pb, pos);
1988 }
1989 
1990 static int mov_write_source_reference_tag(AVIOContext *pb, MOVTrack *track, const char *reel_name)
1991 {
1992  uint64_t str_size =strlen(reel_name);
1993  int64_t pos = avio_tell(pb);
1994 
1995  if (str_size >= UINT16_MAX){
1996  av_log(NULL, AV_LOG_ERROR, "reel_name length %"PRIu64" is too large\n", str_size);
1997  avio_wb16(pb, 0);
1998  return AVERROR(EINVAL);
1999  }
2000 
2001  avio_wb32(pb, 0); /* size */
2002  ffio_wfourcc(pb, "name"); /* Data format */
2003  avio_wb16(pb, str_size); /* string size */
2004  avio_wb16(pb, track->language); /* langcode */
2005  avio_write(pb, reel_name, str_size); /* reel name */
2006  return update_size(pb,pos);
2007 }
2008 
2009 static int mov_write_tmcd_tag(AVIOContext *pb, MOVTrack *track)
2010 {
2011  int64_t pos = avio_tell(pb);
2012 #if 1
2013  int frame_duration;
2014  int nb_frames;
2015  AVDictionaryEntry *t = NULL;
2016 
2017  if (!track->st->avg_frame_rate.num || !track->st->avg_frame_rate.den) {
2018 #if FF_API_LAVF_AVCTX
2020  frame_duration = av_rescale(track->timescale, track->st->codec->time_base.num, track->st->codec->time_base.den);
2021  nb_frames = ROUNDED_DIV(track->st->codec->time_base.den, track->st->codec->time_base.num);
2023 #else
2024  av_log(NULL, AV_LOG_ERROR, "avg_frame_rate not set for tmcd track.\n");
2025  return AVERROR(EINVAL);
2026 #endif
2027  } else {
2028  frame_duration = av_rescale(track->timescale, track->st->avg_frame_rate.num, track->st->avg_frame_rate.den);
2029  nb_frames = ROUNDED_DIV(track->st->avg_frame_rate.den, track->st->avg_frame_rate.num);
2030  }
2031 
2032  if (nb_frames > 255) {
2033  av_log(NULL, AV_LOG_ERROR, "fps %d is too large\n", nb_frames);
2034  return AVERROR(EINVAL);
2035  }
2036 
2037  avio_wb32(pb, 0); /* size */
2038  ffio_wfourcc(pb, "tmcd"); /* Data format */
2039  avio_wb32(pb, 0); /* Reserved */
2040  avio_wb32(pb, 1); /* Data reference index */
2041  avio_wb32(pb, 0); /* Flags */
2042  avio_wb32(pb, track->timecode_flags); /* Flags (timecode) */
2043  avio_wb32(pb, track->timescale); /* Timescale */
2044  avio_wb32(pb, frame_duration); /* Frame duration */
2045  avio_w8(pb, nb_frames); /* Number of frames */
2046  avio_w8(pb, 0); /* Reserved */
2047 
2048  t = av_dict_get(track->st->metadata, "reel_name", NULL, 0);
2049  if (t && utf8len(t->value) && track->mode != MODE_MP4)
2050  mov_write_source_reference_tag(pb, track, t->value);
2051  else
2052  avio_wb16(pb, 0); /* zero size */
2053 #else
2054 
2055  avio_wb32(pb, 0); /* size */
2056  ffio_wfourcc(pb, "tmcd"); /* Data format */
2057  avio_wb32(pb, 0); /* Reserved */
2058  avio_wb32(pb, 1); /* Data reference index */
2059  if (track->par->extradata_size)
2060  avio_write(pb, track->par->extradata, track->par->extradata_size);
2061 #endif
2062  return update_size(pb, pos);
2063 }
2064 
2065 static int mov_write_gpmd_tag(AVIOContext *pb, const MOVTrack *track)
2066 {
2067  int64_t pos = avio_tell(pb);
2068  avio_wb32(pb, 0); /* size */
2069  ffio_wfourcc(pb, "gpmd");
2070  avio_wb32(pb, 0); /* Reserved */
2071  avio_wb16(pb, 0); /* Reserved */
2072  avio_wb16(pb, 1); /* Data-reference index */
2073  avio_wb32(pb, 0); /* Reserved */
2074  return update_size(pb, pos);
2075 }
2076 
2078 {
2079  int64_t pos = avio_tell(pb);
2080  avio_wb32(pb, 0); /* size */
2081  ffio_wfourcc(pb, "stsd");
2082  avio_wb32(pb, 0); /* version & flags */
2083  avio_wb32(pb, 1); /* entry count */
2084  if (track->par->codec_type == AVMEDIA_TYPE_VIDEO)
2085  mov_write_video_tag(pb, mov, track);
2086  else if (track->par->codec_type == AVMEDIA_TYPE_AUDIO)
2087  mov_write_audio_tag(s, pb, mov, track);
2088  else if (track->par->codec_type == AVMEDIA_TYPE_SUBTITLE)
2089  mov_write_subtitle_tag(pb, track);
2090  else if (track->par->codec_tag == MKTAG('r','t','p',' '))
2091  mov_write_rtp_tag(pb, track);
2092  else if (track->par->codec_tag == MKTAG('t','m','c','d'))
2093  mov_write_tmcd_tag(pb, track);
2094  else if (track->par->codec_tag == MKTAG('g','p','m','d'))
2095  mov_write_gpmd_tag(pb, track);
2096  return update_size(pb, pos);
2097 }
2098 
2100 {
2101  MOVMuxContext *mov = s->priv_data;
2102  MOVStts *ctts_entries;
2103  uint32_t entries = 0;
2104  uint32_t atom_size;
2105  int i;
2106 
2107  ctts_entries = av_malloc_array((track->entry + 1), sizeof(*ctts_entries)); /* worst case */
2108  if (!ctts_entries)
2109  return AVERROR(ENOMEM);
2110  ctts_entries[0].count = 1;
2111  ctts_entries[0].duration = track->cluster[0].cts;
2112  for (i = 1; i < track->entry; i++) {
2113  if (track->cluster[i].cts == ctts_entries[entries].duration) {
2114  ctts_entries[entries].count++; /* compress */
2115  } else {
2116  entries++;
2117  ctts_entries[entries].duration = track->cluster[i].cts;
2118  ctts_entries[entries].count = 1;
2119  }
2120  }
2121  entries++; /* last one */
2122  atom_size = 16 + (entries * 8);
2123  avio_wb32(pb, atom_size); /* size */
2124  ffio_wfourcc(pb, "ctts");
2126  avio_w8(pb, 1); /* version */
2127  else
2128  avio_w8(pb, 0); /* version */
2129  avio_wb24(pb, 0); /* flags */
2130  avio_wb32(pb, entries); /* entry count */
2131  for (i = 0; i < entries; i++) {
2132  avio_wb32(pb, ctts_entries[i].count);
2133  avio_wb32(pb, ctts_entries[i].duration);
2134  }
2135  av_free(ctts_entries);
2136  return atom_size;
2137 }
2138 
2139 /* Time to sample atom */
2140 static int mov_write_stts_tag(AVIOContext *pb, MOVTrack *track)
2141 {
2142  MOVStts *stts_entries = NULL;
2143  uint32_t entries = -1;
2144  uint32_t atom_size;
2145  int i;
2146 
2147  if (track->par->codec_type == AVMEDIA_TYPE_AUDIO && !track->audio_vbr) {
2148  stts_entries = av_malloc(sizeof(*stts_entries)); /* one entry */
2149  if (!stts_entries)
2150  return AVERROR(ENOMEM);
2151  stts_entries[0].count = track->sample_count;
2152  stts_entries[0].duration = 1;
2153  entries = 1;
2154  } else {
2155  if (track->entry) {
2156  stts_entries = av_malloc_array(track->entry, sizeof(*stts_entries)); /* worst case */
2157  if (!stts_entries)
2158  return AVERROR(ENOMEM);
2159  }
2160  for (i = 0; i < track->entry; i++) {
2161  int duration = get_cluster_duration(track, i);
2162  if (i && duration == stts_entries[entries].duration) {
2163  stts_entries[entries].count++; /* compress */
2164  } else {
2165  entries++;
2166  stts_entries[entries].duration = duration;
2167  stts_entries[entries].count = 1;
2168  }
2169  }
2170  entries++; /* last one */
2171  }
2172  atom_size = 16 + (entries * 8);
2173  avio_wb32(pb, atom_size); /* size */
2174  ffio_wfourcc(pb, "stts");
2175  avio_wb32(pb, 0); /* version & flags */
2176  avio_wb32(pb, entries); /* entry count */
2177  for (i = 0; i < entries; i++) {
2178  avio_wb32(pb, stts_entries[i].count);
2179  avio_wb32(pb, stts_entries[i].duration);
2180  }
2181  av_free(stts_entries);
2182  return atom_size;
2183 }
2184 
2186 {
2187  avio_wb32(pb, 28); /* size */
2188  ffio_wfourcc(pb, "dref");
2189  avio_wb32(pb, 0); /* version & flags */
2190  avio_wb32(pb, 1); /* entry count */
2191 
2192  avio_wb32(pb, 0xc); /* size */
2193  //FIXME add the alis and rsrc atom
2194  ffio_wfourcc(pb, "url ");
2195  avio_wb32(pb, 1); /* version & flags */
2196 
2197  return 28;
2198 }
2199 
2201 {
2202  struct sgpd_entry {
2203  int count;
2204  int16_t roll_distance;
2205  int group_description_index;
2206  };
2207 
2208  struct sgpd_entry *sgpd_entries = NULL;
2209  int entries = -1;
2210  int group = 0;
2211  int i, j;
2212 
2213  const int OPUS_SEEK_PREROLL_MS = 80;
2214  int roll_samples = av_rescale_q(OPUS_SEEK_PREROLL_MS,
2215  (AVRational){1, 1000},
2216  (AVRational){1, 48000});
2217 
2218  if (!track->entry)
2219  return 0;
2220 
2221  sgpd_entries = av_malloc_array(track->entry, sizeof(*sgpd_entries));
2222  if (!sgpd_entries)
2223  return AVERROR(ENOMEM);
2224 
2226 
2227  if (track->par->codec_id == AV_CODEC_ID_OPUS) {
2228  for (i = 0; i < track->entry; i++) {
2229  int roll_samples_remaining = roll_samples;
2230  int distance = 0;
2231  for (j = i - 1; j >= 0; j--) {
2232  roll_samples_remaining -= get_cluster_duration(track, j);
2233  distance++;
2234  if (roll_samples_remaining <= 0)
2235  break;
2236  }
2237  /* We don't have enough preceeding samples to compute a valid
2238  roll_distance here, so this sample can't be independently
2239  decoded. */
2240  if (roll_samples_remaining > 0)
2241  distance = 0;
2242  /* Verify distance is a minimum of 2 (60ms) packets and a maximum of
2243  32 (2.5ms) packets. */
2244  av_assert0(distance == 0 || (distance >= 2 && distance <= 32));
2245  if (i && distance == sgpd_entries[entries].roll_distance) {
2246  sgpd_entries[entries].count++;
2247  } else {
2248  entries++;
2249  sgpd_entries[entries].count = 1;
2250  sgpd_entries[entries].roll_distance = distance;
2251  sgpd_entries[entries].group_description_index = distance ? ++group : 0;
2252  }
2253  }
2254  } else {
2255  entries++;
2256  sgpd_entries[entries].count = track->sample_count;
2257  sgpd_entries[entries].roll_distance = 1;
2258  sgpd_entries[entries].group_description_index = ++group;
2259  }
2260  entries++;
2261 
2262  if (!group) {
2263  av_free(sgpd_entries);
2264  return 0;
2265  }
2266 
2267  /* Write sgpd tag */
2268  avio_wb32(pb, 24 + (group * 2)); /* size */
2269  ffio_wfourcc(pb, "sgpd");
2270  avio_wb32(pb, 1 << 24); /* fullbox */
2271  ffio_wfourcc(pb, "roll");
2272  avio_wb32(pb, 2); /* default_length */
2273  avio_wb32(pb, group); /* entry_count */
2274  for (i = 0; i < entries; i++) {
2275  if (sgpd_entries[i].group_description_index) {
2276  avio_wb16(pb, -sgpd_entries[i].roll_distance); /* roll_distance */
2277  }
2278  }
2279 
2280  /* Write sbgp tag */
2281  avio_wb32(pb, 20 + (entries * 8)); /* size */
2282  ffio_wfourcc(pb, "sbgp");
2283  avio_wb32(pb, 0); /* fullbox */
2284  ffio_wfourcc(pb, "roll");
2285  avio_wb32(pb, entries); /* entry_count */
2286  for (i = 0; i < entries; i++) {
2287  avio_wb32(pb, sgpd_entries[i].count); /* sample_count */
2288  avio_wb32(pb, sgpd_entries[i].group_description_index); /* group_description_index */
2289  }
2290 
2291  av_free(sgpd_entries);
2292  return 0;
2293 }
2294 
2296 {
2297  int64_t pos = avio_tell(pb);
2298  int ret;
2299 
2300  avio_wb32(pb, 0); /* size */
2301  ffio_wfourcc(pb, "stbl");
2302  mov_write_stsd_tag(s, pb, mov, track);
2303  mov_write_stts_tag(pb, track);
2304  if ((track->par->codec_type == AVMEDIA_TYPE_VIDEO ||
2305  track->par->codec_tag == MKTAG('r','t','p',' ')) &&
2306  track->has_keyframes && track->has_keyframes < track->entry)
2307  mov_write_stss_tag(pb, track, MOV_SYNC_SAMPLE);
2308  if (track->mode == MODE_MOV && track->flags & MOV_TRACK_STPS)
2310  if (track->par->codec_type == AVMEDIA_TYPE_VIDEO &&
2311  track->flags & MOV_TRACK_CTTS && track->entry) {
2312 
2313  if ((ret = mov_write_ctts_tag(s, pb, track)) < 0)
2314  return ret;
2315  }
2316  mov_write_stsc_tag(pb, track);
2317  mov_write_stsz_tag(pb, track);
2318  mov_write_stco_tag(pb, track);
2319  if (track->cenc.aes_ctr) {
2320  ff_mov_cenc_write_stbl_atoms(&track->cenc, pb);
2321  }
2322  if (track->par->codec_id == AV_CODEC_ID_OPUS || track->par->codec_id == AV_CODEC_ID_AAC) {
2323  mov_preroll_write_stbl_atoms(pb, track);
2324  }
2325  return update_size(pb, pos);
2326 }
2327 
2329 {
2330  int64_t pos = avio_tell(pb);
2331  avio_wb32(pb, 0); /* size */
2332  ffio_wfourcc(pb, "dinf");
2333  mov_write_dref_tag(pb);
2334  return update_size(pb, pos);
2335 }
2336 
2338 {
2339  avio_wb32(pb, 12);
2340  ffio_wfourcc(pb, "nmhd");
2341  avio_wb32(pb, 0);
2342  return 12;
2343 }
2344 
2345 static int mov_write_tcmi_tag(AVIOContext *pb, MOVTrack *track)
2346 {
2347  int64_t pos = avio_tell(pb);
2348  const char *font = "Lucida Grande";
2349  avio_wb32(pb, 0); /* size */
2350  ffio_wfourcc(pb, "tcmi"); /* timecode media information atom */
2351  avio_wb32(pb, 0); /* version & flags */
2352  avio_wb16(pb, 0); /* text font */
2353  avio_wb16(pb, 0); /* text face */
2354  avio_wb16(pb, 12); /* text size */
2355  avio_wb16(pb, 0); /* (unknown, not in the QT specs...) */
2356  avio_wb16(pb, 0x0000); /* text color (red) */
2357  avio_wb16(pb, 0x0000); /* text color (green) */
2358  avio_wb16(pb, 0x0000); /* text color (blue) */
2359  avio_wb16(pb, 0xffff); /* background color (red) */
2360  avio_wb16(pb, 0xffff); /* background color (green) */
2361  avio_wb16(pb, 0xffff); /* background color (blue) */
2362  avio_w8(pb, strlen(font)); /* font len (part of the pascal string) */
2363  avio_write(pb, font, strlen(font)); /* font name */
2364  return update_size(pb, pos);
2365 }
2366 
2367 static int mov_write_gmhd_tag(AVIOContext *pb, MOVTrack *track)
2368 {
2369  int64_t pos = avio_tell(pb);
2370  avio_wb32(pb, 0); /* size */
2371  ffio_wfourcc(pb, "gmhd");
2372  avio_wb32(pb, 0x18); /* gmin size */
2373  ffio_wfourcc(pb, "gmin");/* generic media info */
2374  avio_wb32(pb, 0); /* version & flags */
2375  avio_wb16(pb, 0x40); /* graphics mode = */
2376  avio_wb16(pb, 0x8000); /* opColor (r?) */
2377  avio_wb16(pb, 0x8000); /* opColor (g?) */
2378  avio_wb16(pb, 0x8000); /* opColor (b?) */
2379  avio_wb16(pb, 0); /* balance */
2380  avio_wb16(pb, 0); /* reserved */
2381 
2382  /*
2383  * This special text atom is required for
2384  * Apple Quicktime chapters. The contents
2385  * don't appear to be documented, so the
2386  * bytes are copied verbatim.
2387  */
2388  if (track->tag != MKTAG('c','6','0','8')) {
2389  avio_wb32(pb, 0x2C); /* size */
2390  ffio_wfourcc(pb, "text");
2391  avio_wb16(pb, 0x01);
2392  avio_wb32(pb, 0x00);
2393  avio_wb32(pb, 0x00);
2394  avio_wb32(pb, 0x00);
2395  avio_wb32(pb, 0x01);
2396  avio_wb32(pb, 0x00);
2397  avio_wb32(pb, 0x00);
2398  avio_wb32(pb, 0x00);
2399  avio_wb32(pb, 0x00004000);
2400  avio_wb16(pb, 0x0000);
2401  }
2402 
2403  if (track->par->codec_tag == MKTAG('t','m','c','d')) {
2404  int64_t tmcd_pos = avio_tell(pb);
2405  avio_wb32(pb, 0); /* size */
2406  ffio_wfourcc(pb, "tmcd");
2407  mov_write_tcmi_tag(pb, track);
2408  update_size(pb, tmcd_pos);
2409  } else if (track->par->codec_tag == MKTAG('g','p','m','d')) {
2410  int64_t gpmd_pos = avio_tell(pb);
2411  avio_wb32(pb, 0); /* size */
2412  ffio_wfourcc(pb, "gpmd");
2413  avio_wb32(pb, 0); /* version */
2414  update_size(pb, gpmd_pos);
2415  }
2416  return update_size(pb, pos);
2417 }
2418 
2420 {
2421  avio_wb32(pb, 16); /* size */
2422  ffio_wfourcc(pb, "smhd");
2423  avio_wb32(pb, 0); /* version & flags */
2424  avio_wb16(pb, 0); /* reserved (balance, normally = 0) */
2425  avio_wb16(pb, 0); /* reserved */
2426  return 16;
2427 }
2428 
2430 {
2431  avio_wb32(pb, 0x14); /* size (always 0x14) */
2432  ffio_wfourcc(pb, "vmhd");
2433  avio_wb32(pb, 0x01); /* version & flags */
2434  avio_wb64(pb, 0); /* reserved (graphics mode = copy) */
2435  return 0x14;
2436 }
2437 
2438 static int is_clcp_track(MOVTrack *track)
2439 {
2440  return track->tag == MKTAG('c','7','0','8') ||
2441  track->tag == MKTAG('c','6','0','8');
2442 }
2443 
2445 {
2446  const char *hdlr, *descr = NULL, *hdlr_type = NULL;
2447  int64_t pos = avio_tell(pb);
2448 
2449  hdlr = "dhlr";
2450  hdlr_type = "url ";
2451  descr = "DataHandler";
2452 
2453  if (track) {
2454  hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
2455  if (track->par->codec_type == AVMEDIA_TYPE_VIDEO) {
2456  hdlr_type = "vide";
2457  descr = "VideoHandler";
2458  } else if (track->par->codec_type == AVMEDIA_TYPE_AUDIO) {
2459  hdlr_type = "soun";
2460  descr = "SoundHandler";
2461  } else if (track->par->codec_type == AVMEDIA_TYPE_SUBTITLE) {
2462  if (is_clcp_track(track)) {
2463  hdlr_type = "clcp";
2464  descr = "ClosedCaptionHandler";
2465  } else {
2466  if (track->tag == MKTAG('t','x','3','g')) {
2467  hdlr_type = "sbtl";
2468  } else if (track->tag == MKTAG('m','p','4','s')) {
2469  hdlr_type = "subp";
2470  } else {
2471  hdlr_type = "text";
2472  }
2473  descr = "SubtitleHandler";
2474  }
2475  } else if (track->par->codec_tag == MKTAG('r','t','p',' ')) {
2476  hdlr_type = "hint";
2477  descr = "HintHandler";
2478  } else if (track->par->codec_tag == MKTAG('t','m','c','d')) {
2479  hdlr_type = "tmcd";
2480  descr = "TimeCodeHandler";
2481  } else if (track->par->codec_tag == MKTAG('g','p','m','d')) {
2482  hdlr_type = "meta";
2483  descr = "GoPro MET"; // GoPro Metadata
2484  } else {
2486  "Unknown hldr_type for %s, writing dummy values\n",
2487  av_fourcc2str(track->par->codec_tag));
2488  }
2489  if (track->st) {
2490  // hdlr.name is used by some players to identify the content title
2491  // of the track. So if an alternate handler description is
2492  // specified, use it.
2493  AVDictionaryEntry *t;
2494  t = av_dict_get(track->st->metadata, "handler", NULL, 0);
2495  if (t && utf8len(t->value))
2496  descr = t->value;
2497  }
2498  }
2499 
2500  avio_wb32(pb, 0); /* size */
2501  ffio_wfourcc(pb, "hdlr");
2502  avio_wb32(pb, 0); /* Version & flags */
2503  avio_write(pb, hdlr, 4); /* handler */
2504  ffio_wfourcc(pb, hdlr_type); /* handler type */
2505  avio_wb32(pb, 0); /* reserved */
2506  avio_wb32(pb, 0); /* reserved */
2507  avio_wb32(pb, 0); /* reserved */
2508  if (!track || track->mode == MODE_MOV)
2509  avio_w8(pb, strlen(descr)); /* pascal string */
2510  avio_write(pb, descr, strlen(descr)); /* handler description */
2511  if (track && track->mode != MODE_MOV)
2512  avio_w8(pb, 0); /* c string */
2513  return update_size(pb, pos);
2514 }
2515 
2517 {
2518  /* This atom must be present, but leaving the values at zero
2519  * seems harmless. */
2520  avio_wb32(pb, 28); /* size */
2521  ffio_wfourcc(pb, "hmhd");
2522  avio_wb32(pb, 0); /* version, flags */
2523  avio_wb16(pb, 0); /* maxPDUsize */
2524  avio_wb16(pb, 0); /* avgPDUsize */
2525  avio_wb32(pb, 0); /* maxbitrate */
2526  avio_wb32(pb, 0); /* avgbitrate */
2527  avio_wb32(pb, 0); /* reserved */
2528  return 28;
2529 }
2530 
2532 {
2533  int64_t pos = avio_tell(pb);
2534  int ret;
2535 
2536  avio_wb32(pb, 0); /* size */
2537  ffio_wfourcc(pb, "minf");
2538  if (track->par->codec_type == AVMEDIA_TYPE_VIDEO)
2539  mov_write_vmhd_tag(pb);
2540  else if (track->par->codec_type == AVMEDIA_TYPE_AUDIO)
2541  mov_write_smhd_tag(pb);
2542  else if (track->par->codec_type == AVMEDIA_TYPE_SUBTITLE) {
2543  if (track->tag == MKTAG('t','e','x','t') || is_clcp_track(track)) {
2544  mov_write_gmhd_tag(pb, track);
2545  } else {
2546  mov_write_nmhd_tag(pb);
2547  }
2548  } else if (track->tag == MKTAG('r','t','p',' ')) {
2549  mov_write_hmhd_tag(pb);
2550  } else if (track->tag == MKTAG('t','m','c','d')) {
2551  if (track->mode != MODE_MOV)
2552  mov_write_nmhd_tag(pb);
2553  else
2554  mov_write_gmhd_tag(pb, track);
2555  } else if (track->tag == MKTAG('g','p','m','d')) {
2556  mov_write_gmhd_tag(pb, track);
2557  }
2558  if (track->mode == MODE_MOV) /* FIXME: Why do it for MODE_MOV only ? */
2559  mov_write_hdlr_tag(s, pb, NULL);
2560  mov_write_dinf_tag(pb);
2561  if ((ret = mov_write_stbl_tag(s, pb, mov, track)) < 0)
2562  return ret;
2563  return update_size(pb, pos);
2564 }
2565 
2567  MOVTrack *track)
2568 {
2569  int version = track->track_duration < INT32_MAX ? 0 : 1;
2570 
2571  if (track->mode == MODE_ISM)
2572  version = 1;
2573 
2574  (version == 1) ? avio_wb32(pb, 44) : avio_wb32(pb, 32); /* size */
2575  ffio_wfourcc(pb, "mdhd");
2576  avio_w8(pb, version);
2577  avio_wb24(pb, 0); /* flags */
2578  if (version == 1) {
2579  avio_wb64(pb, track->time);
2580  avio_wb64(pb, track->time);
2581  } else {
2582  avio_wb32(pb, track->time); /* creation time */
2583  avio_wb32(pb, track->time); /* modification time */
2584  }
2585  avio_wb32(pb, track->timescale); /* time scale (sample rate for audio) */
2586  if (!track->entry && mov->mode == MODE_ISM)
2587  (version == 1) ? avio_wb64(pb, UINT64_C(0xffffffffffffffff)) : avio_wb32(pb, 0xffffffff);
2588  else if (!track->entry)
2589  (version == 1) ? avio_wb64(pb, 0) : avio_wb32(pb, 0);
2590  else
2591  (version == 1) ? avio_wb64(pb, track->track_duration) : avio_wb32(pb, track->track_duration); /* duration */
2592  avio_wb16(pb, track->language); /* language */
2593  avio_wb16(pb, 0); /* reserved (quality) */
2594 
2595  if (version != 0 && track->mode == MODE_MOV) {
2597  "FATAL error, file duration too long for timebase, this file will not be\n"
2598  "playable with quicktime. Choose a different timebase or a different\n"
2599  "container format\n");
2600  }
2601 
2602  return 32;
2603 }
2604 
2606  MOVMuxContext *mov, MOVTrack *track)
2607 {
2608  int64_t pos = avio_tell(pb);
2609  int ret;
2610 
2611  avio_wb32(pb, 0); /* size */
2612  ffio_wfourcc(pb, "mdia");
2613  mov_write_mdhd_tag(pb, mov, track);
2614  mov_write_hdlr_tag(s, pb, track);
2615  if ((ret = mov_write_minf_tag(s, pb, mov, track)) < 0)
2616  return ret;
2617  return update_size(pb, pos);
2618 }
2619 
2620 /* transformation matrix
2621  |a b u|
2622  |c d v|
2623  |tx ty w| */
2624 static void write_matrix(AVIOContext *pb, int16_t a, int16_t b, int16_t c,
2625  int16_t d, int16_t tx, int16_t ty)
2626 {
2627  avio_wb32(pb, a << 16); /* 16.16 format */
2628  avio_wb32(pb, b << 16); /* 16.16 format */
2629  avio_wb32(pb, 0); /* u in 2.30 format */
2630  avio_wb32(pb, c << 16); /* 16.16 format */
2631  avio_wb32(pb, d << 16); /* 16.16 format */
2632  avio_wb32(pb, 0); /* v in 2.30 format */
2633  avio_wb32(pb, tx << 16); /* 16.16 format */
2634  avio_wb32(pb, ty << 16); /* 16.16 format */
2635  avio_wb32(pb, 1 << 30); /* w in 2.30 format */
2636 }
2637 
2639  MOVTrack *track, AVStream *st)
2640 {
2642  track->timescale, AV_ROUND_UP);
2643  int version = duration < INT32_MAX ? 0 : 1;
2645  int rotation = 0;
2646  int group = 0;
2647 
2648  uint32_t *display_matrix = NULL;
2649  int display_matrix_size, i;
2650 
2651  if (st) {
2652  if (mov->per_stream_grouping)
2653  group = st->index;
2654  else
2655  group = st->codecpar->codec_type;
2656 
2657  display_matrix = (uint32_t*)av_stream_get_side_data(st, AV_PKT_DATA_DISPLAYMATRIX,
2658  &display_matrix_size);
2659  if (display_matrix && display_matrix_size < 9 * sizeof(*display_matrix))
2660  display_matrix = NULL;
2661  }
2662 
2663  if (track->flags & MOV_TRACK_ENABLED)
2664  flags |= MOV_TKHD_FLAG_ENABLED;
2665 
2666  if (track->mode == MODE_ISM)
2667  version = 1;
2668 
2669  (version == 1) ? avio_wb32(pb, 104) : avio_wb32(pb, 92); /* size */
2670  ffio_wfourcc(pb, "tkhd");
2671  avio_w8(pb, version);
2672  avio_wb24(pb, flags);
2673  if (version == 1) {
2674  avio_wb64(pb, track->time);
2675  avio_wb64(pb, track->time);
2676  } else {
2677  avio_wb32(pb, track->time); /* creation time */
2678  avio_wb32(pb, track->time); /* modification time */
2679  }
2680  avio_wb32(pb, track->track_id); /* track-id */
2681  avio_wb32(pb, 0); /* reserved */
2682  if (!track->entry && mov->mode == MODE_ISM)
2683  (version == 1) ? avio_wb64(pb, UINT64_C(0xffffffffffffffff)) : avio_wb32(pb, 0xffffffff);
2684  else if (!track->entry)
2685  (version == 1) ? avio_wb64(pb, 0) : avio_wb32(pb, 0);
2686  else
2687  (version == 1) ? avio_wb64(pb, duration) : avio_wb32(pb, duration);
2688 
2689  avio_wb32(pb, 0); /* reserved */
2690  avio_wb32(pb, 0); /* reserved */
2691  avio_wb16(pb, 0); /* layer */
2692  avio_wb16(pb, group); /* alternate group) */
2693  /* Volume, only for audio */
2694  if (track->par->codec_type == AVMEDIA_TYPE_AUDIO)
2695  avio_wb16(pb, 0x0100);
2696  else
2697  avio_wb16(pb, 0);
2698  avio_wb16(pb, 0); /* reserved */
2699 
2700  /* Matrix structure */
2701 #if FF_API_OLD_ROTATE_API
2702  if (st && st->metadata) {
2703  AVDictionaryEntry *rot = av_dict_get(st->metadata, "rotate", NULL, 0);
2704  rotation = (rot && rot->value) ? atoi(rot->value) : 0;
2705  }
2706 #endif
2707  if (display_matrix) {
2708  for (i = 0; i < 9; i++)
2709  avio_wb32(pb, display_matrix[i]);
2710 #if FF_API_OLD_ROTATE_API
2711  } else if (rotation == 90) {
2712  write_matrix(pb, 0, 1, -1, 0, track->par->height, 0);
2713  } else if (rotation == 180) {
2714  write_matrix(pb, -1, 0, 0, -1, track->par->width, track->par->height);
2715  } else if (rotation == 270) {
2716  write_matrix(pb, 0, -1, 1, 0, 0, track->par->width);
2717 #endif
2718  } else {
2719  write_matrix(pb, 1, 0, 0, 1, 0, 0);
2720  }
2721  /* Track width and height, for visual only */
2722  if (st && (track->par->codec_type == AVMEDIA_TYPE_VIDEO ||
2723  track->par->codec_type == AVMEDIA_TYPE_SUBTITLE)) {
2724  int64_t track_width_1616;
2725  if (track->mode == MODE_MOV) {
2726  track_width_1616 = track->par->width * 0x10000ULL;
2727  } else {
2728  track_width_1616 = av_rescale(st->sample_aspect_ratio.num,
2729  track->par->width * 0x10000LL,
2730  st->sample_aspect_ratio.den);
2731  if (!track_width_1616 ||
2732  track->height != track->par->height ||
2733  track_width_1616 > UINT32_MAX)
2734  track_width_1616 = track->par->width * 0x10000ULL;
2735  }
2736  if (track_width_1616 > UINT32_MAX) {
2737  av_log(mov->fc, AV_LOG_WARNING, "track width is too large\n");
2738  track_width_1616 = 0;
2739  }
2740  avio_wb32(pb, track_width_1616);
2741  if (track->height > 0xFFFF) {
2742  av_log(mov->fc, AV_LOG_WARNING, "track height is too large\n");
2743  avio_wb32(pb, 0);
2744  } else
2745  avio_wb32(pb, track->height * 0x10000U);
2746  } else {
2747  avio_wb32(pb, 0);
2748  avio_wb32(pb, 0);
2749  }
2750  return 0x5c;
2751 }
2752 
2753 static int mov_write_tapt_tag(AVIOContext *pb, MOVTrack *track)
2754 {
2756  track->par->sample_aspect_ratio.den);
2757 
2758  int64_t pos = avio_tell(pb);
2759 
2760  avio_wb32(pb, 0); /* size */
2761  ffio_wfourcc(pb, "tapt");
2762 
2763  avio_wb32(pb, 20);
2764  ffio_wfourcc(pb, "clef");
2765  avio_wb32(pb, 0);
2766  avio_wb32(pb, width << 16);
2767  avio_wb32(pb, track->par->height << 16);
2768 
2769  avio_wb32(pb, 20);
2770  ffio_wfourcc(pb, "prof");
2771  avio_wb32(pb, 0);
2772  avio_wb32(pb, width << 16);
2773  avio_wb32(pb, track->par->height << 16);
2774 
2775  avio_wb32(pb, 20);
2776  ffio_wfourcc(pb, "enof");
2777  avio_wb32(pb, 0);
2778  avio_wb32(pb, track->par->width << 16);
2779  avio_wb32(pb, track->par->height << 16);
2780 
2781  return update_size(pb, pos);
2782 }
2783 
2784 // This box seems important for the psp playback ... without it the movie seems to hang
2786  MOVTrack *track)
2787 {
2789  track->timescale, AV_ROUND_UP);
2790  int version = duration < INT32_MAX ? 0 : 1;
2791  int entry_size, entry_count, size;
2792  int64_t delay, start_ct = track->start_cts;
2793  int64_t start_dts = track->start_dts;
2794 
2795  if (track->entry) {
2796  if (start_dts != track->cluster[0].dts || start_ct != track->cluster[0].cts) {
2797 
2798  av_log(mov->fc, AV_LOG_DEBUG,
2799  "EDTS using dts:%"PRId64" cts:%d instead of dts:%"PRId64" cts:%"PRId64" tid:%d\n",
2800  track->cluster[0].dts, track->cluster[0].cts,
2801  start_dts, start_ct, track->track_id);
2802  start_dts = track->cluster[0].dts;
2803  start_ct = track->cluster[0].cts;
2804  }
2805  }
2806 
2807  delay = av_rescale_rnd(start_dts + start_ct, MOV_TIMESCALE,
2808  track->timescale, AV_ROUND_DOWN);
2809  version |= delay < INT32_MAX ? 0 : 1;
2810 
2811  entry_size = (version == 1) ? 20 : 12;
2812  entry_count = 1 + (delay > 0);
2813  size = 24 + entry_count * entry_size;
2814 
2815  /* write the atom data */
2816  avio_wb32(pb, size);
2817  ffio_wfourcc(pb, "edts");
2818  avio_wb32(pb, size - 8);
2819  ffio_wfourcc(pb, "elst");
2820  avio_w8(pb, version);
2821  avio_wb24(pb, 0); /* flags */
2822 
2823  avio_wb32(pb, entry_count);
2824  if (delay > 0) { /* add an empty edit to delay presentation */
2825  /* In the positive delay case, the delay includes the cts
2826  * offset, and the second edit list entry below trims out
2827  * the same amount from the actual content. This makes sure
2828  * that the offset last sample is included in the edit
2829  * list duration as well. */
2830  if (version == 1) {
2831  avio_wb64(pb, delay);
2832  avio_wb64(pb, -1);
2833  } else {
2834  avio_wb32(pb, delay);
2835  avio_wb32(pb, -1);
2836  }
2837  avio_wb32(pb, 0x00010000);
2838  } else {
2839  /* Avoid accidentally ending up with start_ct = -1 which has got a
2840  * special meaning. Normally start_ct should end up positive or zero
2841  * here, but use FFMIN in case dts is a small positive integer
2842  * rounded to 0 when represented in MOV_TIMESCALE units. */
2843  av_assert0(av_rescale_rnd(start_dts, MOV_TIMESCALE, track->timescale, AV_ROUND_DOWN) <= 0);
2844  start_ct = -FFMIN(start_dts, 0);
2845  /* Note, this delay is calculated from the pts of the first sample,
2846  * ensuring that we don't reduce the duration for cases with
2847  * dts<0 pts=0. */
2848  duration += delay;
2849  }
2850 
2851  /* For fragmented files, we don't know the full length yet. Setting
2852  * duration to 0 allows us to only specify the offset, including
2853  * the rest of the content (from all future fragments) without specifying
2854  * an explicit duration. */
2855  if (mov->flags & FF_MOV_FLAG_FRAGMENT)
2856  duration = 0;
2857 
2858  /* duration */
2859  if (version == 1) {
2860  avio_wb64(pb, duration);
2861  avio_wb64(pb, start_ct);
2862  } else {
2863  avio_wb32(pb, duration);
2864  avio_wb32(pb, start_ct);
2865  }
2866  avio_wb32(pb, 0x00010000);
2867  return size;
2868 }
2869 
2870 static int mov_write_tref_tag(AVIOContext *pb, MOVTrack *track)
2871 {
2872  avio_wb32(pb, 20); // size
2873  ffio_wfourcc(pb, "tref");
2874  avio_wb32(pb, 12); // size (subatom)
2875  avio_wl32(pb, track->tref_tag);
2876  avio_wb32(pb, track->tref_id);
2877  return 20;
2878 }
2879 
2880 // goes at the end of each track! ... Critical for PSP playback ("Incompatible data" without it)
2882 {
2883  avio_wb32(pb, 0x34); /* size ... reports as 28 in mp4box! */
2884  ffio_wfourcc(pb, "uuid");
2885  ffio_wfourcc(pb, "USMT");
2886  avio_wb32(pb, 0x21d24fce);
2887  avio_wb32(pb, 0xbb88695c);
2888  avio_wb32(pb, 0xfac9c740);
2889  avio_wb32(pb, 0x1c); // another size here!
2890  ffio_wfourcc(pb, "MTDT");
2891  avio_wb32(pb, 0x00010012);
2892  avio_wb32(pb, 0x0a);
2893  avio_wb32(pb, 0x55c40000);
2894  avio_wb32(pb, 0x1);
2895  avio_wb32(pb, 0x0);
2896  return 0x34;
2897 }
2898 
2899 static int mov_write_udta_sdp(AVIOContext *pb, MOVTrack *track)
2900 {
2901  AVFormatContext *ctx = track->rtp_ctx;
2902  char buf[1000] = "";
2903  int len;
2904 
2905  ff_sdp_write_media(buf, sizeof(buf), ctx->streams[0], track->src_track,
2906  NULL, NULL, 0, 0, ctx);
2907  av_strlcatf(buf, sizeof(buf), "a=control:streamid=%d\r\n", track->track_id);
2908  len = strlen(buf);
2909 
2910  avio_wb32(pb, len + 24);
2911  ffio_wfourcc(pb, "udta");
2912  avio_wb32(pb, len + 16);
2913  ffio_wfourcc(pb, "hnti");
2914  avio_wb32(pb, len + 8);
2915  ffio_wfourcc(pb, "sdp ");
2916  avio_write(pb, buf, len);
2917  return len + 24;
2918 }
2919 
2921  const char *tag, const char *str)
2922 {
2923  int64_t pos = avio_tell(pb);
2924  AVDictionaryEntry *t = av_dict_get(st->metadata, str, NULL, 0);
2925  if (!t || !utf8len(t->value))
2926  return 0;
2927 
2928  avio_wb32(pb, 0); /* size */
2929  ffio_wfourcc(pb, tag); /* type */
2930  avio_write(pb, t->value, strlen(t->value)); /* UTF8 string value */
2931  return update_size(pb, pos);
2932 }
2933 
2935  AVStream *st)
2936 {
2937  AVIOContext *pb_buf;
2938  int ret, size;
2939  uint8_t *buf;
2940 
2941  if (!st)
2942  return 0;
2943 
2944  ret = avio_open_dyn_buf(&pb_buf);
2945  if (ret < 0)
2946  return ret;
2947 
2948  if (mov->mode & MODE_MP4)
2949  mov_write_track_metadata(pb_buf, st, "name", "title");
2950 
2951  if ((size = avio_close_dyn_buf(pb_buf, &buf)) > 0) {
2952  avio_wb32(pb, size + 8);
2953  ffio_wfourcc(pb, "udta");
2954  avio_write(pb, buf, size);
2955  }
2956  av_free(buf);
2957 
2958  return 0;
2959 }
2960 
2962  MOVTrack *track, AVStream *st)
2963 {
2964  int64_t pos = avio_tell(pb);
2965  int entry_backup = track->entry;
2966  int chunk_backup = track->chunkCount;
2967  int ret;
2968 
2969  /* If we want to have an empty moov, but some samples already have been
2970  * buffered (delay_moov), pretend that no samples have been written yet. */
2971  if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV)
2972  track->chunkCount = track->entry = 0;
2973 
2974  avio_wb32(pb, 0); /* size */
2975  ffio_wfourcc(pb, "trak");
2976  mov_write_tkhd_tag(pb, mov, track, st);
2977 
2978  av_assert2(mov->use_editlist >= 0);
2979 
2980  if (track->start_dts != AV_NOPTS_VALUE) {
2981  if (mov->use_editlist)
2982  mov_write_edts_tag(pb, mov, track); // PSP Movies and several other cases require edts box
2983  else if ((track->entry && track->cluster[0].dts) || track->mode == MODE_PSP || is_clcp_track(track))
2984  av_log(mov->fc, AV_LOG_WARNING,
2985  "Not writing any edit list even though one would have been required\n");
2986  }
2987 
2988  if (track->tref_tag)
2989  mov_write_tref_tag(pb, track);
2990 
2991  if ((ret = mov_write_mdia_tag(s, pb, mov, track)) < 0)
2992  return ret;
2993  if (track->mode == MODE_PSP)
2994  mov_write_uuid_tag_psp(pb, track); // PSP Movies require this uuid box
2995  if (track->tag == MKTAG('r','t','p',' '))
2996  mov_write_udta_sdp(pb, track);
2997  if (track->mode == MODE_MOV) {
2998  if (track->par->codec_type == AVMEDIA_TYPE_VIDEO) {
2999  double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
3000  if (st->sample_aspect_ratio.num && 1.0 != sample_aspect_ratio) {
3001  mov_write_tapt_tag(pb, track);
3002  }
3003  }
3004  if (is_clcp_track(track) && st->sample_aspect_ratio.num) {
3005  mov_write_tapt_tag(pb, track);
3006  }
3007  }
3008  mov_write_track_udta_tag(pb, mov, st);
3009  track->entry = entry_backup;
3010  track->chunkCount = chunk_backup;
3011  return update_size(pb, pos);
3012 }
3013 
3015 {
3016  int i, has_audio = 0, has_video = 0;
3017  int64_t pos = avio_tell(pb);
3018  int audio_profile = mov->iods_audio_profile;
3019  int video_profile = mov->iods_video_profile;
3020  for (i = 0; i < mov->nb_streams; i++) {
3021  if (mov->tracks[i].entry > 0 || mov->flags & FF_MOV_FLAG_EMPTY_MOOV) {
3022  has_audio |= mov->tracks[i].par->codec_type == AVMEDIA_TYPE_AUDIO;
3023  has_video |= mov->tracks[i].par->codec_type == AVMEDIA_TYPE_VIDEO;
3024  }
3025  }
3026  if (audio_profile < 0)
3027  audio_profile = 0xFF - has_audio;
3028  if (video_profile < 0)
3029  video_profile = 0xFF - has_video;
3030  avio_wb32(pb, 0x0); /* size */
3031  ffio_wfourcc(pb, "iods");
3032  avio_wb32(pb, 0); /* version & flags */
3033  put_descr(pb, 0x10, 7);
3034  avio_wb16(pb, 0x004f);
3035  avio_w8(pb, 0xff);
3036  avio_w8(pb, 0xff);
3037  avio_w8(pb, audio_profile);
3038  avio_w8(pb, video_profile);
3039  avio_w8(pb, 0xff);
3040  return update_size(pb, pos);
3041 }
3042 
3043 static int mov_write_trex_tag(AVIOContext *pb, MOVTrack *track)
3044 {
3045  avio_wb32(pb, 0x20); /* size */
3046  ffio_wfourcc(pb, "trex");
3047  avio_wb32(pb, 0); /* version & flags */
3048  avio_wb32(pb, track->track_id); /* track ID */
3049  avio_wb32(pb, 1); /* default sample description index */
3050  avio_wb32(pb, 0); /* default sample duration */
3051  avio_wb32(pb, 0); /* default sample size */
3052  avio_wb32(pb, 0); /* default sample flags */
3053  return 0;
3054 }
3055 
3057 {
3058  int64_t pos = avio_tell(pb);
3059  int i;
3060  avio_wb32(pb, 0x0); /* size */
3061  ffio_wfourcc(pb, "mvex");
3062  for (i = 0; i < mov->nb_streams; i++)
3063  mov_write_trex_tag(pb, &mov->tracks[i]);
3064  return update_size(pb, pos);
3065 }
3066 
3068 {
3069  int max_track_id = 1, i;
3070  int64_t max_track_len = 0;
3071  int version;
3072 
3073  for (i = 0; i < mov->nb_streams; i++) {
3074  if (mov->tracks[i].entry > 0 && mov->tracks[i].timescale) {
3075  int64_t max_track_len_temp = av_rescale_rnd(mov->tracks[i].track_duration,
3076  MOV_TIMESCALE,
3077  mov->tracks[i].timescale,
3078  AV_ROUND_UP);
3079  if (max_track_len < max_track_len_temp)
3080  max_track_len = max_track_len_temp;
3081  if (max_track_id < mov->tracks[i].track_id)
3082  max_track_id = mov->tracks[i].track_id;
3083  }
3084  }
3085  /* If using delay_moov, make sure the output is the same as if no
3086  * samples had been written yet. */
3087  if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV) {
3088  max_track_len = 0;
3089  max_track_id = 1;
3090  }
3091 
3092  version = max_track_len < UINT32_MAX ? 0 : 1;
3093  avio_wb32(pb, version == 1 ? 120 : 108); /* size */
3094 
3095  ffio_wfourcc(pb, "mvhd");
3096  avio_w8(pb, version);
3097  avio_wb24(pb, 0); /* flags */
3098  if (version == 1) {
3099  avio_wb64(pb, mov->time);
3100  avio_wb64(pb, mov->time);
3101  } else {
3102  avio_wb32(pb, mov->time); /* creation time */
3103  avio_wb32(pb, mov->time); /* modification time */
3104  }
3105  avio_wb32(pb, MOV_TIMESCALE);
3106  (version == 1) ? avio_wb64(pb, max_track_len) : avio_wb32(pb, max_track_len); /* duration of longest track */
3107 
3108  avio_wb32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
3109  avio_wb16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
3110  avio_wb16(pb, 0); /* reserved */
3111  avio_wb32(pb, 0); /* reserved */
3112  avio_wb32(pb, 0); /* reserved */
3113 
3114  /* Matrix structure */
3115  write_matrix(pb, 1, 0, 0, 1, 0, 0);
3116 
3117  avio_wb32(pb, 0); /* reserved (preview time) */
3118  avio_wb32(pb, 0); /* reserved (preview duration) */
3119  avio_wb32(pb, 0); /* reserved (poster time) */
3120  avio_wb32(pb, 0); /* reserved (selection time) */
3121  avio_wb32(pb, 0); /* reserved (selection duration) */
3122  avio_wb32(pb, 0); /* reserved (current time) */
3123  avio_wb32(pb, max_track_id + 1); /* Next track id */
3124  return 0x6c;
3125 }
3126 
3128  AVFormatContext *s)
3129 {
3130  avio_wb32(pb, 33); /* size */
3131  ffio_wfourcc(pb, "hdlr");
3132  avio_wb32(pb, 0);
3133  avio_wb32(pb, 0);
3134  ffio_wfourcc(pb, "mdir");
3135  ffio_wfourcc(pb, "appl");
3136  avio_wb32(pb, 0);
3137  avio_wb32(pb, 0);
3138  avio_w8(pb, 0);
3139  return 33;
3140 }
3141 
3142 /* helper function to write a data tag with the specified string as data */
3143 static int mov_write_string_data_tag(AVIOContext *pb, const char *data, int lang, int long_style)
3144 {
3145  if (long_style) {
3146  int size = 16 + strlen(data);
3147  avio_wb32(pb, size); /* size */
3148  ffio_wfourcc(pb, "data");
3149  avio_wb32(pb, 1);
3150  avio_wb32(pb, 0);
3151  avio_write(pb, data, strlen(data));
3152  return size;
3153  } else {
3154  if (!lang)
3155  lang = ff_mov_iso639_to_lang("und", 1);
3156  avio_wb16(pb, strlen(data)); /* string length */
3157  avio_wb16(pb, lang);
3158  avio_write(pb, data, strlen(data));
3159  return strlen(data) + 4;
3160  }
3161 }
3162 
3163 static int mov_write_string_tag(AVIOContext *pb, const char *name,
3164  const char *value, int lang, int long_style)
3165 {
3166  int size = 0;
3167  if (value && value[0]) {
3168  int64_t pos = avio_tell(pb);
3169  avio_wb32(pb, 0); /* size */
3170  ffio_wfourcc(pb, name);
3171  mov_write_string_data_tag(pb, value, lang, long_style);
3172  size = update_size(pb, pos);
3173  }
3174  return size;
3175 }
3176 
3178  const char *tag, int *lang)
3179 {
3180  int l, len, len2;
3181  AVDictionaryEntry *t, *t2 = NULL;
3182  char tag2[16];
3183 
3184  *lang = 0;
3185 
3186  if (!(t = av_dict_get(s->metadata, tag, NULL, 0)))
3187  return NULL;
3188 
3189  len = strlen(t->key);
3190  snprintf(tag2, sizeof(tag2), "%s-", tag);
3191  while ((t2 = av_dict_get(s->metadata, tag2, t2, AV_DICT_IGNORE_SUFFIX))) {
3192  len2 = strlen(t2->key);
3193  if (len2 == len + 4 && !strcmp(t->value, t2->value)
3194  && (l = ff_mov_iso639_to_lang(&t2->key[len2 - 3], 1)) >= 0) {
3195  *lang = l;
3196  return t;
3197  }
3198  }
3199  return t;
3200 }
3201 
3203  const char *name, const char *tag,
3204  int long_style)
3205 {
3206  int lang;
3207  AVDictionaryEntry *t = get_metadata_lang(s, tag, &lang);
3208  if (!t)
3209  return 0;
3210  return mov_write_string_tag(pb, name, t->value, lang, long_style);
3211 }
3212 
3213 /* iTunes bpm number */
3215 {
3216  AVDictionaryEntry *t = av_dict_get(s->metadata, "tmpo", NULL, 0);
3217  int size = 0, tmpo = t ? atoi(t->value) : 0;
3218  if (tmpo) {
3219  size = 26;
3220  avio_wb32(pb, size);
3221  ffio_wfourcc(pb, "tmpo");
3222  avio_wb32(pb, size-8); /* size */
3223  ffio_wfourcc(pb, "data");
3224  avio_wb32(pb, 0x15); //type specifier
3225  avio_wb32(pb, 0);
3226  avio_wb16(pb, tmpo); // data
3227  }
3228  return size;
3229 }
3230 
3231 /* 3GPP TS 26.244 */
3233 {
3234  int lang;
3235  int64_t pos = avio_tell(pb);
3236  double latitude, longitude, altitude;
3237  int32_t latitude_fix, longitude_fix, altitude_fix;
3238  AVDictionaryEntry *t = get_metadata_lang(s, "location", &lang);
3239  const char *ptr, *place = "";
3240  char *end;
3241  static const char *astronomical_body = "earth";
3242  if (!t)
3243  return 0;
3244 
3245  ptr = t->value;
3246  longitude = strtod(ptr, &end);
3247  if (end == ptr) {
3248  av_log(s, AV_LOG_WARNING, "malformed location metadata\n");
3249  return 0;
3250  }
3251  ptr = end;
3252  latitude = strtod(ptr, &end);
3253  if (end == ptr) {
3254  av_log(s, AV_LOG_WARNING, "malformed location metadata\n");
3255  return 0;
3256  }
3257  ptr = end;
3258  altitude = strtod(ptr, &end);
3259  /* If no altitude was present, the default 0 should be fine */
3260  if (*end == '/')
3261  place = end + 1;
3262 
3263  latitude_fix = (int32_t) ((1 << 16) * latitude);
3264  longitude_fix = (int32_t) ((1 << 16) * longitude);
3265  altitude_fix = (int32_t) ((1 << 16) * altitude);
3266 
3267  avio_wb32(pb, 0); /* size */
3268  ffio_wfourcc(pb, "loci"); /* type */
3269  avio_wb32(pb, 0); /* version + flags */
3270  avio_wb16(pb, lang);
3271  avio_write(pb, place, strlen(place) + 1);
3272  avio_w8(pb, 0); /* role of place (0 == shooting location, 1 == real location, 2 == fictional location) */
3273  avio_wb32(pb, latitude_fix);
3274  avio_wb32(pb, longitude_fix);
3275  avio_wb32(pb, altitude_fix);
3276  avio_write(pb, astronomical_body, strlen(astronomical_body) + 1);
3277  avio_w8(pb, 0); /* additional notes, null terminated string */
3278 
3279  return update_size(pb, pos);
3280 }
3281 
3282 /* iTunes track or disc number */
3284  AVFormatContext *s, int disc)
3285 {
3287  disc ? "disc" : "track",
3288  NULL, 0);
3289  int size = 0, track = t ? atoi(t->value) : 0;
3290  if (track) {
3291  int tracks = 0;
3292  char *slash = strchr(t->value, '/');
3293  if (slash)
3294  tracks = atoi(slash + 1);
3295  avio_wb32(pb, 32); /* size */
3296  ffio_wfourcc(pb, disc ? "disk" : "trkn");
3297  avio_wb32(pb, 24); /* size */
3298  ffio_wfourcc(pb, "data");
3299  avio_wb32(pb, 0); // 8 bytes empty
3300  avio_wb32(pb, 0);
3301  avio_wb16(pb, 0); // empty
3302  avio_wb16(pb, track); // track / disc number
3303  avio_wb16(pb, tracks); // total track / disc number
3304  avio_wb16(pb, 0); // empty
3305  size = 32;
3306  }
3307  return size;
3308 }
3309 
3311  const char *name, const char *tag,
3312  int len)
3313 {
3314  AVDictionaryEntry *t = NULL;
3315  uint8_t num;
3316  int size = 24 + len;
3317 
3318  if (len != 1 && len != 4)
3319  return -1;
3320 
3321  if (!(t = av_dict_get(s->metadata, tag, NULL, 0)))
3322  return 0;
3323  num = atoi(t->value);
3324 
3325  avio_wb32(pb, size);
3326  ffio_wfourcc(pb, name);
3327  avio_wb32(pb, size - 8);
3328  ffio_wfourcc(pb, "data");
3329  avio_wb32(pb, 0x15);
3330  avio_wb32(pb, 0);
3331  if (len==4) avio_wb32(pb, num);
3332  else avio_w8 (pb, num);
3333 
3334  return size;
3335 }
3336 
3337 /* iTunes meta data list */
3339  AVFormatContext *s)
3340 {
3341  int64_t pos = avio_tell(pb);
3342  avio_wb32(pb, 0); /* size */
3343  ffio_wfourcc(pb, "ilst");
3344  mov_write_string_metadata(s, pb, "\251nam", "title" , 1);
3345  mov_write_string_metadata(s, pb, "\251ART", "artist" , 1);
3346  mov_write_string_metadata(s, pb, "aART", "album_artist", 1);
3347  mov_write_string_metadata(s, pb, "\251wrt", "composer" , 1);
3348  mov_write_string_metadata(s, pb, "\251alb", "album" , 1);
3349  mov_write_string_metadata(s, pb, "\251day", "date" , 1);
3350  if (!mov_write_string_metadata(s, pb, "\251too", "encoding_tool", 1)) {
3351  if (!(s->flags & AVFMT_FLAG_BITEXACT))
3352  mov_write_string_tag(pb, "\251too", LIBAVFORMAT_IDENT, 0, 1);
3353  }
3354  mov_write_string_metadata(s, pb, "\251cmt", "comment" , 1);
3355  mov_write_string_metadata(s, pb, "\251gen", "genre" , 1);
3356  mov_write_string_metadata(s, pb, "\251cpy", "copyright", 1);
3357  mov_write_string_metadata(s, pb, "\251grp", "grouping" , 1);
3358  mov_write_string_metadata(s, pb, "\251lyr", "lyrics" , 1);
3359  mov_write_string_metadata(s, pb, "desc", "description",1);
3360  mov_write_string_metadata(s, pb, "ldes", "synopsis" , 1);
3361  mov_write_string_metadata(s, pb, "tvsh", "show" , 1);
3362  mov_write_string_metadata(s, pb, "tven", "episode_id",1);
3363  mov_write_string_metadata(s, pb, "tvnn", "network" , 1);
3364  mov_write_string_metadata(s, pb, "keyw", "keywords" , 1);
3365  mov_write_int8_metadata (s, pb, "tves", "episode_sort",4);
3366  mov_write_int8_metadata (s, pb, "tvsn", "season_number",4);
3367  mov_write_int8_metadata (s, pb, "stik", "media_type",1);
3368  mov_write_int8_metadata (s, pb, "hdvd", "hd_video", 1);
3369  mov_write_int8_metadata (s, pb, "pgap", "gapless_playback",1);
3370  mov_write_int8_metadata (s, pb, "cpil", "compilation", 1);
3371  mov_write_trkn_tag(pb, mov, s, 0); // track number
3372  mov_write_trkn_tag(pb, mov, s, 1); // disc number
3373  mov_write_tmpo_tag(pb, s);
3374  return update_size(pb, pos);
3375 }
3376 
3378  AVFormatContext *s)
3379 {
3380  avio_wb32(pb, 33); /* size */
3381  ffio_wfourcc(pb, "hdlr");
3382  avio_wb32(pb, 0);
3383  avio_wb32(pb, 0);
3384  ffio_wfourcc(pb, "mdta");
3385  avio_wb32(pb, 0);
3386  avio_wb32(pb, 0);
3387  avio_wb32(pb, 0);
3388  avio_w8(pb, 0);
3389  return 33;
3390 }
3391 
3393  AVFormatContext *s)
3394 {
3395  AVDictionaryEntry *t = NULL;
3396  int64_t pos = avio_tell(pb);
3397  int64_t curpos, entry_pos;
3398  int count = 0;
3399 
3400  avio_wb32(pb, 0); /* size */
3401  ffio_wfourcc(pb, "keys");
3402  avio_wb32(pb, 0);
3403  entry_pos = avio_tell(pb);
3404  avio_wb32(pb, 0); /* entry count */
3405 
3406  while (t = av_dict_get(s->metadata, "", t, AV_DICT_IGNORE_SUFFIX)) {
3407  avio_wb32(pb, strlen(t->key) + 8);
3408  ffio_wfourcc(pb, "mdta");
3409  avio_write(pb, t->key, strlen(t->key));
3410  count += 1;
3411  }
3412  curpos = avio_tell(pb);
3413  avio_seek(pb, entry_pos, SEEK_SET);
3414  avio_wb32(pb, count); // rewrite entry count
3415  avio_seek(pb, curpos, SEEK_SET);
3416 
3417  return update_size(pb, pos);
3418 }
3419 
3421  AVFormatContext *s)
3422 {
3423  AVDictionaryEntry *t = NULL;
3424  int64_t pos = avio_tell(pb);
3425  int count = 1; /* keys are 1-index based */
3426 
3427  avio_wb32(pb, 0); /* size */
3428  ffio_wfourcc(pb, "ilst");
3429 
3430  while (t = av_dict_get(s->metadata, "", t, AV_DICT_IGNORE_SUFFIX)) {
3431  int64_t entry_pos = avio_tell(pb);
3432  avio_wb32(pb, 0); /* size */
3433  avio_wb32(pb, count); /* key */
3434  mov_write_string_data_tag(pb, t->value, 0, 1);
3435  update_size(pb, entry_pos);
3436  count += 1;
3437  }
3438  return update_size(pb, pos);
3439 }
3440 
3441 /* meta data tags */
3443  AVFormatContext *s)
3444 {
3445  int size = 0;
3446  int64_t pos = avio_tell(pb);
3447  avio_wb32(pb, 0); /* size */
3448  ffio_wfourcc(pb, "meta");
3449  avio_wb32(pb, 0);
3450  if (mov->flags & FF_MOV_FLAG_USE_MDTA) {
3451  mov_write_mdta_hdlr_tag(pb, mov, s);
3452  mov_write_mdta_keys_tag(pb, mov, s);
3453  mov_write_mdta_ilst_tag(pb, mov, s);
3454  }
3455  else {
3456  /* iTunes metadata tag */
3457  mov_write_itunes_hdlr_tag(pb, mov, s);
3458  mov_write_ilst_tag(pb, mov, s);
3459  }
3460  size = update_size(pb, pos);
3461  return size;
3462 }
3463 
3465  const char *name, const char *key)
3466 {
3467  int len;
3468  AVDictionaryEntry *t;
3469 
3470  if (!(t = av_dict_get(s->metadata, key, NULL, 0)))
3471  return 0;
3472 
3473  len = strlen(t->value);
3474  if (len > 0) {
3475  int size = len + 8;
3476  avio_wb32(pb, size);
3477  ffio_wfourcc(pb, name);
3478  avio_write(pb, t->value, len);
3479  return size;
3480  }
3481  return 0;
3482 }
3483 
3484 static int ascii_to_wc(AVIOContext *pb, const uint8_t *b)
3485 {
3486  int val;
3487  while (*b) {
3488  GET_UTF8(val, *b++, return -1;)
3489  avio_wb16(pb, val);
3490  }
3491  avio_wb16(pb, 0x00);
3492  return 0;
3493 }
3494 
3495 static uint16_t language_code(const char *str)
3496 {
3497  return (((str[0] - 0x60) & 0x1F) << 10) +
3498  (((str[1] - 0x60) & 0x1F) << 5) +
3499  (( str[2] - 0x60) & 0x1F);
3500 }
3501 
3503  const char *tag, const char *str)
3504 {
3505  int64_t pos = avio_tell(pb);
3506  AVDictionaryEntry *t = av_dict_get(s->metadata, str, NULL, 0);
3507  if (!t || !utf8len(t->value))
3508  return 0;
3509  avio_wb32(pb, 0); /* size */
3510  ffio_wfourcc(pb, tag); /* type */
3511  avio_wb32(pb, 0); /* version + flags */
3512  if (!strcmp(tag, "yrrc"))
3513  avio_wb16(pb, atoi(t->value));
3514  else {
3515  avio_wb16(pb, language_code("eng")); /* language */
3516  avio_write(pb, t->value, strlen(t->value) + 1); /* UTF8 string value */
3517  if (!strcmp(tag, "albm") &&
3518  (t = av_dict_get(s->metadata, "track", NULL, 0)))
3519  avio_w8(pb, atoi(t->value));
3520  }
3521  return update_size(pb, pos);
3522 }
3523 
3525 {
3526  int64_t pos = avio_tell(pb);
3527  int i, nb_chapters = FFMIN(s->nb_chapters, 255);
3528 
3529  avio_wb32(pb, 0); // size
3530  ffio_wfourcc(pb, "chpl");
3531  avio_wb32(pb, 0x01000000); // version + flags
3532  avio_wb32(pb, 0); // unknown
3533  avio_w8(pb, nb_chapters);
3534 
3535  for (i = 0; i < nb_chapters; i++) {
3536  AVChapter *c = s->chapters[i];
3537  AVDictionaryEntry *t;
3538  avio_wb64(pb, av_rescale_q(c->start, c->time_base, (AVRational){1,10000000}));
3539 
3540  if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
3541  int len = FFMIN(strlen(t->value), 255);
3542  avio_w8(pb, len);
3543  avio_write(pb, t->value, len);
3544  } else
3545  avio_w8(pb, 0);
3546  }
3547  return update_size(pb, pos);
3548 }
3549 
3551  AVFormatContext *s)
3552 {
3553  AVIOContext *pb_buf;
3554  int ret, size;
3555  uint8_t *buf;
3556 
3557  ret = avio_open_dyn_buf(&pb_buf);
3558  if (ret < 0)
3559  return ret;
3560 
3561  if (mov->mode & MODE_3GP) {
3562  mov_write_3gp_udta_tag(pb_buf, s, "perf", "artist");
3563  mov_write_3gp_udta_tag(pb_buf, s, "titl", "title");
3564  mov_write_3gp_udta_tag(pb_buf, s, "auth", "author");
3565  mov_write_3gp_udta_tag(pb_buf, s, "gnre", "genre");
3566  mov_write_3gp_udta_tag(pb_buf, s, "dscp", "comment");
3567  mov_write_3gp_udta_tag(pb_buf, s, "albm", "album");
3568  mov_write_3gp_udta_tag(pb_buf, s, "cprt", "copyright");
3569  mov_write_3gp_udta_tag(pb_buf, s, "yrrc", "date");
3570  mov_write_loci_tag(s, pb_buf);
3571  } 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
3572  mov_write_string_metadata(s, pb_buf, "\251ART", "artist", 0);
3573  mov_write_string_metadata(s, pb_buf, "\251nam", "title", 0);
3574  mov_write_string_metadata(s, pb_buf, "\251aut", "author", 0);
3575  mov_write_string_metadata(s, pb_buf, "\251alb", "album", 0);
3576  mov_write_string_metadata(s, pb_buf, "\251day", "date", 0);
3577  mov_write_string_metadata(s, pb_buf, "\251swr", "encoder", 0);
3578  // currently ignored by mov.c
3579  mov_write_string_metadata(s, pb_buf, "\251des", "comment", 0);
3580  // add support for libquicktime, this atom is also actually read by mov.c
3581  mov_write_string_metadata(s, pb_buf, "\251cmt", "comment", 0);
3582  mov_write_string_metadata(s, pb_buf, "\251gen", "genre", 0);
3583  mov_write_string_metadata(s, pb_buf, "\251cpy", "copyright", 0);
3584  mov_write_string_metadata(s, pb_buf, "\251mak", "make", 0);
3585  mov_write_string_metadata(s, pb_buf, "\251mod", "model", 0);
3586  mov_write_string_metadata(s, pb_buf, "\251xyz", "location", 0);
3587  mov_write_string_metadata(s, pb_buf, "\251key", "keywords", 0);
3588  mov_write_raw_metadata_tag(s, pb_buf, "XMP_", "xmp");
3589  } else {
3590  /* iTunes meta data */
3591  mov_write_meta_tag(pb_buf, mov, s);
3592  mov_write_loci_tag(s, pb_buf);
3593  }
3594 
3595  if (s->nb_chapters && !(mov->flags & FF_MOV_FLAG_DISABLE_CHPL))
3596  mov_write_chpl_tag(pb_buf, s);
3597 
3598  if ((size = avio_close_dyn_buf(pb_buf, &buf)) > 0) {
3599  avio_wb32(pb, size + 8);
3600  ffio_wfourcc(pb, "udta");
3601  avio_write(pb, buf, size);
3602  }
3603  av_free(buf);
3604 
3605  return 0;
3606 }
3607 
3609  const char *str, const char *lang, int type)
3610 {
3611  int len = utf8len(str) + 1;
3612  if (len <= 0)
3613  return;
3614  avio_wb16(pb, len * 2 + 10); /* size */
3615  avio_wb32(pb, type); /* type */
3616  avio_wb16(pb, language_code(lang)); /* language */
3617  avio_wb16(pb, 0x01); /* ? */
3618  ascii_to_wc(pb, str);
3619 }
3620 
3622 {
3623  AVDictionaryEntry *title = av_dict_get(s->metadata, "title", NULL, 0);
3624  int64_t pos, pos2;
3625 
3626  if (title) {
3627  pos = avio_tell(pb);
3628  avio_wb32(pb, 0); /* size placeholder*/
3629  ffio_wfourcc(pb, "uuid");
3630  ffio_wfourcc(pb, "USMT");
3631  avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
3632  avio_wb32(pb, 0xbb88695c);
3633  avio_wb32(pb, 0xfac9c740);
3634 
3635  pos2 = avio_tell(pb);
3636  avio_wb32(pb, 0); /* size placeholder*/
3637  ffio_wfourcc(pb, "MTDT");
3638  avio_wb16(pb, 4);
3639 
3640  // ?
3641  avio_wb16(pb, 0x0C); /* size */
3642  avio_wb32(pb, 0x0B); /* type */
3643  avio_wb16(pb, language_code("und")); /* language */
3644  avio_wb16(pb, 0x0); /* ? */
3645  avio_wb16(pb, 0x021C); /* data */
3646 
3647  if (!(s->flags & AVFMT_FLAG_BITEXACT))
3648  mov_write_psp_udta_tag(pb, LIBAVCODEC_IDENT, "eng", 0x04);
3649  mov_write_psp_udta_tag(pb, title->value, "eng", 0x01);
3650  mov_write_psp_udta_tag(pb, "2006/04/01 11:11:11", "und", 0x03);
3651 
3652  update_size(pb, pos2);
3653  return update_size(pb, pos);
3654  }
3655 
3656  return 0;
3657 }
3658 
3659 static void build_chunks(MOVTrack *trk)
3660 {
3661  int i;
3662  MOVIentry *chunk = &trk->cluster[0];
3663  uint64_t chunkSize = chunk->size;
3664  chunk->chunkNum = 1;
3665  if (trk->chunkCount)
3666  return;
3667  trk->chunkCount = 1;
3668  for (i = 1; i<trk->entry; i++){
3669  if (chunk->pos + chunkSize == trk->cluster[i].pos &&
3670  chunkSize + trk->cluster[i].size < (1<<20)){
3671  chunkSize += trk->cluster[i].size;
3672  chunk->samples_in_chunk += trk->cluster[i].entries;
3673  } else {
3674  trk->cluster[i].chunkNum = chunk->chunkNum+1;
3675  chunk=&trk->cluster[i];
3676  chunkSize = chunk->size;
3677  trk->chunkCount++;
3678  }
3679  }
3680 }
3681 
3682 /**
3683  * Assign track ids. If option "use_stream_ids_as_track_ids" is set,
3684  * the stream ids are used as track ids.
3685  *
3686  * This assumes mov->tracks and s->streams are in the same order and
3687  * there are no gaps in either of them (so mov->tracks[n] refers to
3688  * s->streams[n]).
3689  *
3690  * As an exception, there can be more entries in
3691  * s->streams than in mov->tracks, in which case new track ids are
3692  * generated (starting after the largest found stream id).
3693  */
3695 {
3696  int i;
3697 
3698  if (mov->track_ids_ok)
3699  return 0;
3700 
3701  if (mov->use_stream_ids_as_track_ids) {
3702  int next_generated_track_id = 0;
3703  for (i = 0; i < s->nb_streams; i++) {
3704  if (s->streams[i]->id > next_generated_track_id)
3705  next_generated_track_id = s->streams[i]->id;
3706  }
3707 
3708  for (i = 0; i < mov->nb_streams; i++) {
3709  if (mov->tracks[i].entry <= 0 && !(mov->flags & FF_MOV_FLAG_FRAGMENT))
3710  continue;
3711 
3712  mov->tracks[i].track_id = i >= s->nb_streams ? ++next_generated_track_id : s->streams[i]->id;
3713  }
3714  } else {
3715  for (i = 0; i < mov->nb_streams; i++) {
3716  if (mov->tracks[i].entry <= 0 && !(mov->flags & FF_MOV_FLAG_FRAGMENT))
3717  continue;
3718 
3719  mov->tracks[i].track_id = i + 1;
3720  }
3721  }
3722 
3723  mov->track_ids_ok = 1;
3724 
3725  return 0;
3726 }
3727 
3729  AVFormatContext *s)
3730 {
3731  int i;
3732  int64_t pos = avio_tell(pb);
3733  avio_wb32(pb, 0); /* size placeholder*/
3734  ffio_wfourcc(pb, "moov");
3735 
3736  mov_setup_track_ids(mov, s);
3737 
3738  for (i = 0; i < mov->nb_streams; i++) {
3739  if (mov->tracks[i].entry <= 0 && !(mov->flags & FF_MOV_FLAG_FRAGMENT))
3740  continue;
3741 
3742  mov->tracks[i].time = mov->time;
3743 
3744  if (mov->tracks[i].entry)
3745  build_chunks(&mov->tracks[i]);
3746  }
3747 
3748  if (mov->chapter_track)
3749  for (i = 0; i < s->nb_streams; i++) {
3750  mov->tracks[i].tref_tag = MKTAG('c','h','a','p');
3751  mov->tracks[i].tref_id = mov->tracks[mov->chapter_track].track_id;
3752  }
3753  for (i = 0; i < mov->nb_streams; i++) {
3754  MOVTrack *track = &mov->tracks[i];
3755  if (track->tag == MKTAG('r','t','p',' ')) {
3756  track->tref_tag = MKTAG('h','i','n','t');
3757  track->tref_id = mov->tracks[track->src_track].track_id;
3758  } else if (track->par->codec_type == AVMEDIA_TYPE_AUDIO) {
3759  int * fallback, size;
3760  fallback = (int*)av_stream_get_side_data(track->st,
3762  &size);
3763  if (fallback != NULL && size == sizeof(int)) {
3764  if (*fallback >= 0 && *fallback < mov->nb_streams) {
3765  track->tref_tag = MKTAG('f','a','l','l');
3766  track->tref_id = mov->tracks[*fallback].track_id;
3767  }
3768  }
3769  }
3770  }
3771  for (i = 0; i < mov->nb_streams; i++) {
3772  if (mov->tracks[i].tag == MKTAG('t','m','c','d')) {
3773  int src_trk = mov->tracks[i].src_track;
3774  mov->tracks[src_trk].tref_tag = mov->tracks[i].tag;
3775  mov->tracks[src_trk].tref_id = mov->tracks[i].track_id;
3776  //src_trk may have a different timescale than the tmcd track
3777  mov->tracks[i].track_duration = av_rescale(mov->tracks[src_trk].track_duration,
3778  mov->tracks[i].timescale,
3779  mov->tracks[src_trk].timescale);
3780  }
3781  }
3782 
3783  mov_write_mvhd_tag(pb, mov);
3784  if (mov->mode != MODE_MOV && !mov->iods_skip)
3785  mov_write_iods_tag(pb, mov);
3786  for (i = 0; i < mov->nb_streams; i++) {
3787  if (mov->tracks[i].entry > 0 || mov->flags & FF_MOV_FLAG_FRAGMENT) {
3788  int ret = mov_write_trak_tag(s, pb, mov, &(mov->tracks[i]), i < s->nb_streams ? s->streams[i] : NULL);
3789  if (ret < 0)
3790  return ret;
3791  }
3792  }
3793  if (mov->flags & FF_MOV_FLAG_FRAGMENT)
3794  mov_write_mvex_tag(pb, mov); /* QuickTime requires trak to precede this */
3795 
3796  if (mov->mode == MODE_PSP)
3797  mov_write_uuidusmt_tag(pb, s);
3798  else
3799  mov_write_udta_tag(pb, mov, s);
3800 
3801  return update_size(pb, pos);
3802 }
3803 
3804 static void param_write_int(AVIOContext *pb, const char *name, int value)
3805 {
3806  avio_printf(pb, "<param name=\"%s\" value=\"%d\" valuetype=\"data\"/>\n", name, value);
3807 }
3808 
3809 static void param_write_string(AVIOContext *pb, const char *name, const char *value)
3810 {
3811  avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, value);
3812 }
3813 
3814 static void param_write_hex(AVIOContext *pb, const char *name, const uint8_t *value, int len)
3815 {
3816  char buf[150];
3817  len = FFMIN(sizeof(buf) / 2 - 1, len);
3818  ff_data_to_hex(buf, value, len, 0);
3819  buf[2 * len] = '\0';
3820  avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, buf);
3821 }
3822 
3824 {
3825  int64_t pos = avio_tell(pb);
3826  int i;
3827  int64_t manifest_bit_rate = 0;
3828  AVCPBProperties *props = NULL;
3829 
3830  static const uint8_t uuid[] = {
3831  0xa5, 0xd4, 0x0b, 0x30, 0xe8, 0x14, 0x11, 0xdd,
3832  0xba, 0x2f, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66
3833  };
3834 
3835  avio_wb32(pb, 0);
3836  ffio_wfourcc(pb, "uuid");
3837  avio_write(pb, uuid, sizeof(uuid));
3838  avio_wb32(pb, 0);
3839 
3840  avio_printf(pb, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
3841  avio_printf(pb, "<smil xmlns=\"http://www.w3.org/2001/SMIL20/Language\">\n");
3842  avio_printf(pb, "<head>\n");
3843  if (!(mov->fc->flags & AVFMT_FLAG_BITEXACT))
3844  avio_printf(pb, "<meta name=\"creator\" content=\"%s\" />\n",
3846  avio_printf(pb, "</head>\n");
3847  avio_printf(pb, "<body>\n");
3848  avio_printf(pb, "<switch>\n");
3849 
3850  mov_setup_track_ids(mov, s);
3851 
3852  for (i = 0; i < mov->nb_streams; i++) {
3853  MOVTrack *track = &mov->tracks[i];
3854  const char *type;
3855  int track_id = track->track_id;
3856  char track_name_buf[32] = { 0 };
3857 
3858  AVStream *st = track->st;
3859  AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL,0);
3860 
3861  if (track->par->codec_type == AVMEDIA_TYPE_VIDEO) {
3862  type = "video";
3863  } else if (track->par->codec_type == AVMEDIA_TYPE_AUDIO) {
3864  type = "audio";
3865  } else {
3866  continue;
3867  }
3868 
3870 
3871  if (track->par->bit_rate) {
3872  manifest_bit_rate = track->par->bit_rate;
3873  } else if (props) {
3874  manifest_bit_rate = props->max_bitrate;
3875  }
3876 
3877  avio_printf(pb, "<%s systemBitrate=\"%"PRId64"\">\n", type,
3878  manifest_bit_rate);
3879  param_write_int(pb, "systemBitrate", manifest_bit_rate);
3880  param_write_int(pb, "trackID", track_id);
3881  param_write_string(pb, "systemLanguage", lang ? lang->value : "und");
3882 
3883  /* Build track name piece by piece: */
3884  /* 1. track type */
3885  av_strlcat(track_name_buf, type, sizeof(track_name_buf));
3886  /* 2. track language, if available */
3887  if (lang)
3888  av_strlcatf(track_name_buf, sizeof(track_name_buf),
3889  "_%s", lang->value);
3890  /* 3. special type suffix */
3891  /* "_cc" = closed captions, "_ad" = audio_description */
3893  av_strlcat(track_name_buf, "_cc", sizeof(track_name_buf));
3895  av_strlcat(track_name_buf, "_ad", sizeof(track_name_buf));
3896 
3897  param_write_string(pb, "trackName", track_name_buf);
3898 
3899  if (track->par->codec_type == AVMEDIA_TYPE_VIDEO) {
3900  if (track->par->codec_id == AV_CODEC_ID_H264) {
3901  uint8_t *ptr;
3902  int size = track->par->extradata_size;
3903  if (!ff_avc_write_annexb_extradata(track->par->extradata, &ptr,
3904  &size)) {
3905  param_write_hex(pb, "CodecPrivateData",
3906  ptr ? ptr : track->par->extradata,
3907  size);
3908  av_free(ptr);
3909  }
3910  param_write_string(pb, "FourCC", "H264");
3911  } else if (track->par->codec_id == AV_CODEC_ID_VC1) {
3912  param_write_string(pb, "FourCC", "WVC1");
3913  param_write_hex(pb, "CodecPrivateData", track->par->extradata,
3914  track->par->extradata_size);
3915  }
3916  param_write_int(pb, "MaxWidth", track->par->width);
3917  param_write_int(pb, "MaxHeight", track->par->height);
3918  param_write_int(pb, "DisplayWidth", track->par->width);
3919  param_write_int(pb, "DisplayHeight", track->par->height);
3920  } else {
3921  if (track->par->codec_id == AV_CODEC_ID_AAC) {
3922  switch (track->par->profile)
3923  {
3924  case FF_PROFILE_AAC_HE_V2:
3925  param_write_string(pb, "FourCC", "AACP");
3926  break;
3927  case FF_PROFILE_AAC_HE:
3928  param_write_string(pb, "FourCC", "AACH");
3929  break;
3930  default:
3931  param_write_string(pb, "FourCC", "AACL");
3932  }
3933  } else if (track->par->codec_id == AV_CODEC_ID_WMAPRO) {
3934  param_write_string(pb, "FourCC", "WMAP");
3935  }
3936  param_write_hex(pb, "CodecPrivateData", track->par->extradata,
3937  track->par->extradata_size);
3939  track->par->codec_id));
3940  param_write_int(pb, "Channels", track->par->channels);
3941  param_write_int(pb, "SamplingRate", track->par->sample_rate);
3942  param_write_int(pb, "BitsPerSample", 16);
3943  param_write_int(pb, "PacketSize", track->par->block_align ?
3944  track->par->block_align : 4);
3945  }
3946  avio_printf(pb, "</%s>\n", type);
3947  }
3948  avio_printf(pb, "</switch>\n");
3949  avio_printf(pb, "</body>\n");
3950  avio_printf(pb, "</smil>\n");
3951 
3952  return update_size(pb, pos);
3953 }
3954 
3956 {
3957  avio_wb32(pb, 16);
3958  ffio_wfourcc(pb, "mfhd");
3959  avio_wb32(pb, 0);
3960  avio_wb32(pb, mov->fragments);
3961  return 0;
3962 }
3963 
3964 static uint32_t get_sample_flags(MOVTrack *track, MOVIentry *entry)
3965 {
3968 }
3969 
3971  MOVTrack *track, int64_t moof_offset)
3972 {
3973  int64_t pos = avio_tell(pb);
3976  if (!track->entry) {
3977  flags |= MOV_TFHD_DURATION_IS_EMPTY;
3978  } else {
3979  flags |= MOV_TFHD_DEFAULT_FLAGS;
3980  }
3982  flags &= ~MOV_TFHD_BASE_DATA_OFFSET;
3983  if (mov->flags & FF_MOV_FLAG_DEFAULT_BASE_MOOF) {
3984  flags &= ~MOV_TFHD_BASE_DATA_OFFSET;
3986  }
3987 
3988  /* Don't set a default sample size, the silverlight player refuses
3989  * to play files with that set. Don't set a default sample duration,
3990  * WMP freaks out if it is set. Don't set a base data offset, PIFF
3991  * file format says it MUST NOT be set. */
3992  if (track->mode == MODE_ISM)
3995 
3996  avio_wb32(pb, 0); /* size placeholder */
3997  ffio_wfourcc(pb, "tfhd");
3998  avio_w8(pb, 0); /* version */
3999  avio_wb24(pb, flags);
4000 
4001  avio_wb32(pb, track->track_id); /* track-id */
4002  if (flags & MOV_TFHD_BASE_DATA_OFFSET)
4003  avio_wb64(pb, moof_offset);
4004  if (flags & MOV_TFHD_DEFAULT_DURATION) {
4005  track->default_duration = get_cluster_duration(track, 0);
4006  avio_wb32(pb, track->default_duration);
4007  }
4008  if (flags & MOV_TFHD_DEFAULT_SIZE) {
4009  track->default_size = track->entry ? track->cluster[0].size : 1;
4010  avio_wb32(pb, track->default_size);
4011  } else
4012  track->default_size = -1;
4013 
4014  if (flags & MOV_TFHD_DEFAULT_FLAGS) {
4015  /* Set the default flags based on the second sample, if available.
4016  * If the first sample is different, that can be signaled via a separate field. */
4017  if (track->entry > 1)
4018  track->default_sample_flags = get_sample_flags(track, &track->cluster[1]);
4019  else
4020  track->default_sample_flags =
4021  track->par->codec_type == AVMEDIA_TYPE_VIDEO ?
4024  avio_wb32(pb, track->default_sample_flags);
4025  }
4026 
4027  return update_size(pb, pos);
4028 }
4029 
4031  MOVTrack *track, int moof_size,
4032  int first, int end)
4033 {
4034  int64_t pos = avio_tell(pb);
4035  uint32_t flags = MOV_TRUN_DATA_OFFSET;
4036  int i;
4037 
4038  for (i = first; i < end; i++) {
4039  if (get_cluster_duration(track, i) != track->default_duration)
4040  flags |= MOV_TRUN_SAMPLE_DURATION;
4041  if (track->cluster[i].size != track->default_size)
4042  flags |= MOV_TRUN_SAMPLE_SIZE;
4043  if (i > first && get_sample_flags(track, &track->cluster[i]) != track->default_sample_flags)
4044  flags |= MOV_TRUN_SAMPLE_FLAGS;
4045  }
4046  if (!(flags & MOV_TRUN_SAMPLE_FLAGS) && track->entry > 0 &&
4047  get_sample_flags(track, &track->cluster[0]) != track->default_sample_flags)
4048  flags |= MOV_TRUN_FIRST_SAMPLE_FLAGS;
4049  if (track->flags & MOV_TRACK_CTTS)
4050  flags |= MOV_TRUN_SAMPLE_CTS;
4051 
4052  avio_wb32(pb, 0); /* size placeholder */
4053  ffio_wfourcc(pb, "trun");
4055  avio_w8(pb, 1); /* version */
4056  else
4057  avio_w8(pb, 0); /* version */
4058  avio_wb24(pb, flags);
4059 
4060  avio_wb32(pb, end - first); /* sample count */
4061  if (mov->flags & FF_MOV_FLAG_OMIT_TFHD_OFFSET &&
4063  !mov->first_trun)
4064  avio_wb32(pb, 0); /* Later tracks follow immediately after the previous one */
4065  else
4066  avio_wb32(pb, moof_size + 8 + track->data_offset +
4067  track->cluster[first].pos); /* data offset */
4068  if (flags & MOV_TRUN_FIRST_SAMPLE_FLAGS)
4069  avio_wb32(pb, get_sample_flags(track, &track->cluster[first]));
4070 
4071  for (i = first; i < end; i++) {
4072  if (flags & MOV_TRUN_SAMPLE_DURATION)
4073  avio_wb32(pb, get_cluster_duration(track, i));
4074  if (flags & MOV_TRUN_SAMPLE_SIZE)
4075  avio_wb32(pb, track->cluster[i].size);
4076  if (flags & MOV_TRUN_SAMPLE_FLAGS)
4077  avio_wb32(pb, get_sample_flags(track, &track->cluster[i]));
4078  if (flags & MOV_TRUN_SAMPLE_CTS)
4079  avio_wb32(pb, track->cluster[i].cts);
4080  }
4081 
4082  mov->first_trun = 0;
4083  return update_size(pb, pos);
4084 }
4085 
4086 static int mov_write_tfxd_tag(AVIOContext *pb, MOVTrack *track)
4087 {
4088  int64_t pos = avio_tell(pb);
4089  static const uint8_t uuid[] = {
4090  0x6d, 0x1d, 0x9b, 0x05, 0x42, 0xd5, 0x44, 0xe6,
4091  0x80, 0xe2, 0x14, 0x1d, 0xaf, 0xf7, 0x57, 0xb2
4092  };
4093 
4094  avio_wb32(pb, 0); /* size placeholder */
4095  ffio_wfourcc(pb, "uuid");
4096  avio_write(pb, uuid, sizeof(uuid));
4097  avio_w8(pb, 1);
4098  avio_wb24(pb, 0);
4099  avio_wb64(pb, track->start_dts + track->frag_start +
4100  track->cluster[0].cts);
4101  avio_wb64(pb, track->end_pts -
4102  (track->cluster[0].dts + track->cluster[0].cts));
4103 
4104  return update_size(pb, pos);
4105 }
4106 
4108  MOVTrack *track, int entry)
4109 {
4110  int n = track->nb_frag_info - 1 - entry, i;
4111  int size = 8 + 16 + 4 + 1 + 16*n;
4112  static const uint8_t uuid[] = {
4113  0xd4, 0x80, 0x7e, 0xf2, 0xca, 0x39, 0x46, 0x95,
4114  0x8e, 0x54, 0x26, 0xcb, 0x9e, 0x46, 0xa7, 0x9f
4115  };
4116 
4117  if (entry < 0)
4118  return 0;
4119 
4120  avio_seek(pb, track->frag_info[entry].tfrf_offset, SEEK_SET);
4121  avio_wb32(pb, size);
4122  ffio_wfourcc(pb, "uuid");
4123  avio_write(pb, uuid, sizeof(uuid));
4124  avio_w8(pb, 1);
4125  avio_wb24(pb, 0);
4126  avio_w8(pb, n);
4127  for (i = 0; i < n; i++) {
4128  int index = entry + 1 + i;
4129  avio_wb64(pb, track->frag_info[index].time);
4130  avio_wb64(pb, track->frag_info[index].duration);
4131  }
4132  if (n < mov->ism_lookahead) {
4133  int free_size = 16 * (mov->ism_lookahead - n);
4134  avio_wb32(pb, free_size);
4135  ffio_wfourcc(pb, "free");
4136  ffio_fill(pb, 0, free_size - 8);
4137  }
4138 
4139  return 0;
4140 }
4141 
4143  MOVTrack *track)
4144 {
4145  int64_t pos = avio_tell(pb);
4146  int i;
4147  for (i = 0; i < mov->ism_lookahead; i++) {
4148  /* Update the tfrf tag for the last ism_lookahead fragments,
4149  * nb_frag_info - 1 is the next fragment to be written. */
4150  mov_write_tfrf_tag(pb, mov, track, track->nb_frag_info - 2 - i);
4151  }
4152  avio_seek(pb, pos, SEEK_SET);
4153  return 0;
4154 }
4155 
4156 static int mov_add_tfra_entries(AVIOContext *pb, MOVMuxContext *mov, int tracks,
4157  int size)
4158 {
4159  int i;
4160  for (i = 0; i < mov->nb_streams; i++) {
4161  MOVTrack *track = &mov->tracks[i];
4162  MOVFragmentInfo *info;
4163  if ((tracks >= 0 && i != tracks) || !track->entry)
4164  continue;
4165  track->nb_frag_info++;
4166  if (track->nb_frag_info >= track->frag_info_capacity) {
4167  unsigned new_capacity = track->nb_frag_info + MOV_FRAG_INFO_ALLOC_INCREMENT;
4168  if (av_reallocp_array(&track->frag_info,
4169  new_capacity,
4170  sizeof(*track->frag_info)))
4171  return AVERROR(ENOMEM);
4172  track->frag_info_capacity = new_capacity;
4173  }
4174  info = &track->frag_info[track->nb_frag_info - 1];
4175  info->offset = avio_tell(pb);
4176  info->size = size;
4177  // Try to recreate the original pts for the first packet
4178  // from the fields we have stored
4179  info->time = track->start_dts + track->frag_start +
4180  track->cluster[0].cts;
4181  info->duration = track->end_pts -
4182  (track->cluster[0].dts + track->cluster[0].cts);
4183  // If the pts is less than zero, we will have trimmed
4184  // away parts of the media track using an edit list,
4185  // and the corresponding start presentation time is zero.
4186  if (info->time < 0) {
4187  info->duration += info->time;
4188  info->time = 0;
4189  }
4190  info->tfrf_offset = 0;
4191  mov_write_tfrf_tags(pb, mov, track);
4192  }
4193  return 0;
4194 }
4195 
4196 static void mov_prune_frag_info(MOVMuxContext *mov, int tracks, int max)
4197 {
4198  int i;
4199  for (i = 0; i < mov->nb_streams; i++) {
4200  MOVTrack *track = &mov->tracks[i];
4201  if ((tracks >= 0 && i != tracks) || !track->entry)
4202  continue;
4203  if (track->nb_frag_info > max) {
4204  memmove(track->frag_info, track->frag_info + (track->nb_frag_info - max), max * sizeof(*track->frag_info));
4205  track->nb_frag_info = max;
4206  }
4207  }
4208 }
4209 
4210 static int mov_write_tfdt_tag(AVIOContext *pb, MOVTrack *track)
4211 {
4212  int64_t pos = avio_tell(pb);
4213 
4214  avio_wb32(pb, 0); /* size */
4215  ffio_wfourcc(pb, "tfdt");
4216  avio_w8(pb, 1); /* version */
4217  avio_wb24(pb, 0);
4218  avio_wb64(pb, track->frag_start);
4219  return update_size(pb, pos);
4220 }
4221 
4223  MOVTrack *track, int64_t moof_offset,
4224  int moof_size)
4225 {
4226  int64_t pos = avio_tell(pb);
4227  int i, start = 0;
4228  avio_wb32(pb, 0); /* size placeholder */
4229  ffio_wfourcc(pb, "traf");
4230 
4231  mov_write_tfhd_tag(pb, mov, track, moof_offset);
4232  if (mov->mode != MODE_ISM)
4233  mov_write_tfdt_tag(pb, track);
4234  for (i = 1; i < track->entry; i++) {
4235  if (track->cluster[i].pos != track->cluster[i - 1].pos + track->cluster[i - 1].size) {
4236  mov_write_trun_tag(pb, mov, track, moof_size, start, i);
4237  start = i;
4238  }
4239  }
4240  mov_write_trun_tag(pb, mov, track, moof_size, start, track->entry);
4241  if (mov->mode == MODE_ISM) {
4242  mov_write_tfxd_tag(pb, track);
4243 
4244  if (mov->ism_lookahead) {
4245  int i, size = 16 + 4 + 1 + 16 * mov->ism_lookahead;
4246 
4247  if (track->nb_frag_info > 0) {
4248  MOVFragmentInfo *info = &track->frag_info[track->nb_frag_info - 1];
4249  if (!info->tfrf_offset)
4250  info->tfrf_offset = avio_tell(pb);
4251  }
4252  avio_wb32(pb, 8 + size);
4253  ffio_wfourcc(pb, "free");
4254  for (i = 0; i < size; i++)
4255  avio_w8(pb, 0);
4256  }
4257  }
4258 
4259  return update_size(pb, pos);
4260 }
4261 
4263  int tracks, int moof_size)
4264 {
4265  int64_t pos = avio_tell(pb);
4266  int i;
4267 
4268  avio_wb32(pb, 0); /* size placeholder */
4269  ffio_wfourcc(pb, "moof");
4270  mov->first_trun = 1;
4271 
4272  mov_write_mfhd_tag(pb, mov);
4273  for (i = 0; i < mov->nb_streams; i++) {
4274  MOVTrack *track = &mov->tracks[i];
4275  if (tracks >= 0 && i != tracks)
4276  continue;
4277  if (!track->entry)
4278  continue;
4279  mov_write_traf_tag(pb, mov, track, pos, moof_size);
4280  }
4281 
4282  return update_size(pb, pos);
4283 }
4284 
4286  MOVTrack *track, int ref_size, int total_sidx_size)
4287 {
4288  int64_t pos = avio_tell(pb), offset_pos, end_pos;
4289  int64_t presentation_time, duration, offset;
4290  int starts_with_SAP, i, entries;
4291 
4292  if (track->entry) {
4293  entries = 1;
4294  presentation_time = track->start_dts + track->frag_start +
4295  track->cluster[0].cts;
4296  duration = track->end_pts -
4297  (track->cluster[0].dts + track->cluster[0].cts);
4298  starts_with_SAP = track->cluster[0].flags & MOV_SYNC_SAMPLE;
4299 
4300  // pts<0 should be cut away using edts
4301  if (presentation_time < 0) {
4302  duration += presentation_time;
4303  presentation_time = 0;
4304  }
4305  } else {
4306  entries = track->nb_frag_info;
4307  if (entries <= 0)
4308  return 0;
4309  presentation_time = track->frag_info[0].time;
4310  }
4311 
4312  avio_wb32(pb, 0); /* size */
4313  ffio_wfourcc(pb, "sidx");
4314  avio_w8(pb, 1); /* version */
4315  avio_wb24(pb, 0);
4316  avio_wb32(pb, track->track_id); /* reference_ID */
4317  avio_wb32(pb, track->timescale); /* timescale */
4318  avio_wb64(pb, presentation_time); /* earliest_presentation_time */
4319  offset_pos = avio_tell(pb);
4320  avio_wb64(pb, 0); /* first_offset (offset to referenced moof) */
4321  avio_wb16(pb, 0); /* reserved */
4322 
4323  avio_wb16(pb, entries); /* reference_count */
4324  for (i = 0; i < entries; i++) {
4325  if (!track->entry) {
4326  if (i > 1 && track->frag_info[i].offset != track->frag_info[i - 1].offset + track->frag_info[i - 1].size) {
4327  av_log(NULL, AV_LOG_ERROR, "Non-consecutive fragments, writing incorrect sidx\n");
4328  }
4329  duration = track->frag_info[i].duration;
4330  ref_size = track->frag_info[i].size;
4331  starts_with_SAP = 1;
4332  }
4333  avio_wb32(pb, (0 << 31) | (ref_size & 0x7fffffff)); /* reference_type (0 = media) | referenced_size */
4334  avio_wb32(pb, duration); /* subsegment_duration */
4335  avio_wb32(pb, (starts_with_SAP << 31) | (0 << 28) | 0); /* starts_with_SAP | SAP_type | SAP_delta_time */
4336  }
4337 
4338  end_pos = avio_tell(pb);
4339  offset = pos + total_sidx_size - end_pos;
4340  avio_seek(pb, offset_pos, SEEK_SET);
4341  avio_wb64(pb, offset);
4342  avio_seek(pb, end_pos, SEEK_SET);
4343  return update_size(pb, pos);
4344 }
4345 
4347  int tracks, int ref_size)
4348 {
4349  int i, round, ret;
4350  AVIOContext *avio_buf;
4351  int total_size = 0;
4352  for (round = 0; round < 2; round++) {
4353  // First run one round to calculate the total size of all
4354  // sidx atoms.
4355  // This would be much simpler if we'd only write one sidx
4356  // atom, for the first track in the moof.
4357  if (round == 0) {
4358  if ((ret = ffio_open_null_buf(&avio_buf)) < 0)
4359  return ret;
4360  } else {
4361  avio_buf = pb;
4362  }
4363  for (i = 0; i < mov->nb_streams; i++) {
4364  MOVTrack *track = &mov->tracks[i];
4365  if (tracks >= 0 && i != tracks)
4366  continue;
4367  // When writing a sidx for the full file, entry is 0, but
4368  // we want to include all tracks. ref_size is 0 in this case,
4369  // since we read it from frag_info instead.
4370  if (!track->entry && ref_size > 0)
4371  continue;
4372  total_size -= mov_write_sidx_tag(avio_buf, track, ref_size,
4373  total_size);
4374  }
4375  if (round == 0)
4376  total_size = ffio_close_null_buf(avio_buf);
4377  }
4378  return 0;
4379 }
4380 
4381 static int mov_write_moof_tag(AVIOContext *pb, MOVMuxContext *mov, int tracks,
4382  int64_t mdat_size)
4383 {
4384  AVIOContext *avio_buf;
4385  int ret, moof_size;
4386 
4387  if ((ret = ffio_open_null_buf(&avio_buf)) < 0)
4388  return ret;
4389  mov_write_moof_tag_internal(avio_buf, mov, tracks, 0);
4390  moof_size = ffio_close_null_buf(avio_buf);
4391 
4392  if (mov->flags & FF_MOV_FLAG_DASH && !(mov->flags & FF_MOV_FLAG_GLOBAL_SIDX))
4393  mov_write_sidx_tags(pb, mov, tracks, moof_size + 8 + mdat_size);
4394 
4395  if (mov->flags & FF_MOV_FLAG_GLOBAL_SIDX ||
4396  !(mov->flags & FF_MOV_FLAG_SKIP_TRAILER) ||
4397  mov->ism_lookahead) {
4398  if ((ret = mov_add_tfra_entries(pb, mov, tracks, moof_size + 8 + mdat_size)) < 0)
4399  return ret;
4400  if (!(mov->flags & FF_MOV_FLAG_GLOBAL_SIDX) &&
4402  mov_prune_frag_info(mov, tracks, mov->ism_lookahead + 1);
4403  }
4404  }
4405 
4406  return mov_write_moof_tag_internal(pb, mov, tracks, moof_size);
4407 }
4408 
4409 static int mov_write_tfra_tag(AVIOContext *pb, MOVTrack *track)
4410 {
4411  int64_t pos = avio_tell(pb);
4412  int i;
4413 
4414  avio_wb32(pb, 0); /* size placeholder */
4415  ffio_wfourcc(pb, "tfra");
4416  avio_w8(pb, 1); /* version */
4417  avio_wb24(pb, 0);
4418 
4419  avio_wb32(pb, track->track_id);
4420  avio_wb32(pb, 0); /* length of traf/trun/sample num */
4421  avio_wb32(pb, track->nb_frag_info);
4422  for (i = 0; i < track->nb_frag_info; i++) {
4423  avio_wb64(pb, track->frag_info[i].time);
4424  avio_wb64(pb, track->frag_info[i].offset + track->data_offset);
4425  avio_w8(pb, 1); /* traf number */
4426  avio_w8(pb, 1); /* trun number */
4427  avio_w8(pb, 1); /* sample number */
4428  }
4429 
4430  return update_size(pb, pos);
4431 }
4432 
4434 {
4435  int64_t pos = avio_tell(pb);
4436  int i;
4437 
4438  avio_wb32(pb, 0); /* size placeholder */
4439  ffio_wfourcc(pb, "mfra");
4440  /* An empty mfra atom is enough to indicate to the publishing point that
4441  * the stream has ended. */
4442  if (mov->flags & FF_MOV_FLAG_ISML)
4443  return update_size(pb, pos);
4444 
4445  for (i = 0; i < mov->nb_streams; i++) {
4446  MOVTrack *track = &mov->tracks[i];
4447  if (track->nb_frag_info)
4448  mov_write_tfra_tag(pb, track);
4449  }
4450 
4451  avio_wb32(pb, 16);
4452  ffio_wfourcc(pb, "mfro");
4453  avio_wb32(pb, 0); /* version + flags */
4454  avio_wb32(pb, avio_tell(pb) + 4 - pos);
4455 
4456  return update_size(pb, pos);
4457 }
4458 
4460 {
4461  avio_wb32(pb, 8); // placeholder for extended size field (64 bit)
4462  ffio_wfourcc(pb, mov->mode == MODE_MOV ? "wide" : "free");
4463 
4464  mov->mdat_pos = avio_tell(pb);
4465  avio_wb32(pb, 0); /* size placeholder*/
4466  ffio_wfourcc(pb, "mdat");
4467  return 0;
4468 }
4469 
4470 /* TODO: This needs to be more general */
4472 {
4473  MOVMuxContext *mov = s->priv_data;
4474  int64_t pos = avio_tell(pb);
4475  int has_h264 = 0, has_video = 0;
4476  int minor = 0x200;
4477  int i;