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