Libav
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 Libav.
8  *
9  * Libav 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  * Libav 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 Libav; 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/get_bits.h"
35 #include "libavcodec/put_bits.h"
36 #include "libavcodec/vc1.h"
37 #include "internal.h"
38 #include "libavutil/avstring.h"
39 #include "libavutil/intfloat.h"
40 #include "libavutil/mathematics.h"
41 #include "libavutil/opt.h"
42 #include "libavutil/dict.h"
43 #include "hevc.h"
44 #include "rtpenc.h"
45 #include "mov_chan.h"
46 
47 #undef NDEBUG
48 #include <assert.h>
49 
50 static const AVOption options[] = {
51  { "movflags", "MOV muxer flags", offsetof(MOVMuxContext, flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
52  { "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" },
53  { "empty_moov", "Make the initial moov atom empty (not supported by QuickTime)", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_EMPTY_MOOV}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
54  { "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" },
55  { "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" },
56  { "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" },
57  { "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" },
58  { "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" },
59  { "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" },
60  { "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" },
61  FF_RTP_FLAG_OPTS(MOVMuxContext, rtp_flags),
62  { "skip_iods", "Skip writing iods atom.", offsetof(MOVMuxContext, iods_skip), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
63  { "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},
64  { "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},
65  { "frag_duration", "Maximum fragment duration", offsetof(MOVMuxContext, max_fragment_duration), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
66  { "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},
67  { "frag_size", "Maximum fragment size", offsetof(MOVMuxContext, max_fragment_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
68  { "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},
69  { "brand", "Override major brand", offsetof(MOVMuxContext, major_brand), AV_OPT_TYPE_STRING, {.str = NULL}, .flags = AV_OPT_FLAG_ENCODING_PARAM },
70  { NULL },
71 };
72 
73 #define MOV_CLASS(flavor)\
74 static const AVClass flavor ## _muxer_class = {\
75  .class_name = #flavor " muxer",\
76  .item_name = av_default_item_name,\
77  .option = options,\
78  .version = LIBAVUTIL_VERSION_INT,\
79 };
80 
81 static int utf8len(const uint8_t *b)
82 {
83  int len = 0;
84  int val;
85  while (*b) {
86  GET_UTF8(val, *b++, return -1;)
87  len++;
88  }
89  return len;
90 }
91 
92 //FIXME support 64 bit variant with wide placeholders
93 static int64_t update_size(AVIOContext *pb, int64_t pos)
94 {
95  int64_t curpos = avio_tell(pb);
96  avio_seek(pb, pos, SEEK_SET);
97  avio_wb32(pb, curpos - pos); /* rewrite size */
98  avio_seek(pb, curpos, SEEK_SET);
99 
100  return curpos - pos;
101 }
102 
103 static int co64_required(const MOVTrack *track)
104 {
105  if (track->entry > 0 && track->cluster[track->entry - 1].pos + track->data_offset > UINT32_MAX)
106  return 1;
107  return 0;
108 }
109 
110 /* Chunk offset atom */
111 static int mov_write_stco_tag(AVIOContext *pb, MOVTrack *track)
112 {
113  int i;
114  int mode64 = co64_required(track); // use 32 bit size variant if possible
115  int64_t pos = avio_tell(pb);
116  avio_wb32(pb, 0); /* size */
117  if (mode64) {
118  ffio_wfourcc(pb, "co64");
119  } else
120  ffio_wfourcc(pb, "stco");
121  avio_wb32(pb, 0); /* version & flags */
122  avio_wb32(pb, track->entry); /* entry count */
123  for (i = 0; i < track->entry; i++) {
124  if (mode64 == 1)
125  avio_wb64(pb, track->cluster[i].pos + track->data_offset);
126  else
127  avio_wb32(pb, track->cluster[i].pos + track->data_offset);
128  }
129  return update_size(pb, pos);
130 }
131 
132 /* Sample size atom */
133 static int mov_write_stsz_tag(AVIOContext *pb, MOVTrack *track)
134 {
135  int equalChunks = 1;
136  int i, j, entries = 0, tst = -1, oldtst = -1;
137 
138  int64_t pos = avio_tell(pb);
139  avio_wb32(pb, 0); /* size */
140  ffio_wfourcc(pb, "stsz");
141  avio_wb32(pb, 0); /* version & flags */
142 
143  for (i = 0; i < track->entry; i++) {
144  tst = track->cluster[i].size / track->cluster[i].entries;
145  if (oldtst != -1 && tst != oldtst)
146  equalChunks = 0;
147  oldtst = tst;
148  entries += track->cluster[i].entries;
149  }
150  if (equalChunks && track->entry) {
151  int sSize = track->entry ? track->cluster[0].size / track->cluster[0].entries : 0;
152  sSize = FFMAX(1, sSize); // adpcm mono case could make sSize == 0
153  avio_wb32(pb, sSize); // sample size
154  avio_wb32(pb, entries); // sample count
155  } else {
156  avio_wb32(pb, 0); // sample size
157  avio_wb32(pb, entries); // sample count
158  for (i = 0; i < track->entry; i++) {
159  for (j = 0; j < track->cluster[i].entries; j++) {
160  avio_wb32(pb, track->cluster[i].size /
161  track->cluster[i].entries);
162  }
163  }
164  }
165  return update_size(pb, pos);
166 }
167 
168 /* Sample to chunk atom */
169 static int mov_write_stsc_tag(AVIOContext *pb, MOVTrack *track)
170 {
171  int index = 0, oldval = -1, i;
172  int64_t entryPos, curpos;
173 
174  int64_t pos = avio_tell(pb);
175  avio_wb32(pb, 0); /* size */
176  ffio_wfourcc(pb, "stsc");
177  avio_wb32(pb, 0); // version & flags
178  entryPos = avio_tell(pb);
179  avio_wb32(pb, track->entry); // entry count
180  for (i = 0; i < track->entry; i++) {
181  if (oldval != track->cluster[i].samples_in_chunk) {
182  avio_wb32(pb, i + 1); // first chunk
183  avio_wb32(pb, track->cluster[i].samples_in_chunk); // samples per chunk
184  avio_wb32(pb, 0x1); // sample description index
185  oldval = track->cluster[i].samples_in_chunk;
186  index++;
187  }
188  }
189  curpos = avio_tell(pb);
190  avio_seek(pb, entryPos, SEEK_SET);
191  avio_wb32(pb, index); // rewrite size
192  avio_seek(pb, curpos, SEEK_SET);
193 
194  return update_size(pb, pos);
195 }
196 
197 /* Sync sample atom */
198 static int mov_write_stss_tag(AVIOContext *pb, MOVTrack *track, uint32_t flag)
199 {
200  int64_t curpos, entryPos;
201  int i, index = 0;
202  int64_t pos = avio_tell(pb);
203  avio_wb32(pb, 0); // size
204  ffio_wfourcc(pb, flag == MOV_SYNC_SAMPLE ? "stss" : "stps");
205  avio_wb32(pb, 0); // version & flags
206  entryPos = avio_tell(pb);
207  avio_wb32(pb, track->entry); // entry count
208  for (i = 0; i < track->entry; i++) {
209  if (track->cluster[i].flags & flag) {
210  avio_wb32(pb, i + 1);
211  index++;
212  }
213  }
214  curpos = avio_tell(pb);
215  avio_seek(pb, entryPos, SEEK_SET);
216  avio_wb32(pb, index); // rewrite size
217  avio_seek(pb, curpos, SEEK_SET);
218  return update_size(pb, pos);
219 }
220 
221 static int mov_write_amr_tag(AVIOContext *pb, MOVTrack *track)
222 {
223  avio_wb32(pb, 0x11); /* size */
224  if (track->mode == MODE_MOV) ffio_wfourcc(pb, "samr");
225  else ffio_wfourcc(pb, "damr");
226  ffio_wfourcc(pb, "FFMP");
227  avio_w8(pb, 0); /* decoder version */
228 
229  avio_wb16(pb, 0x81FF); /* Mode set (all modes for AMR_NB) */
230  avio_w8(pb, 0x00); /* Mode change period (no restriction) */
231  avio_w8(pb, 0x01); /* Frames per sample */
232  return 0x11;
233 }
234 
235 static int mov_write_ac3_tag(AVIOContext *pb, MOVTrack *track)
236 {
237  GetBitContext gbc;
238  PutBitContext pbc;
239  uint8_t buf[3];
240  int fscod, bsid, bsmod, acmod, lfeon, frmsizecod;
241 
242  if (track->vos_len < 7)
243  return -1;
244 
245  avio_wb32(pb, 11);
246  ffio_wfourcc(pb, "dac3");
247 
248  init_get_bits(&gbc, track->vos_data + 4, (track->vos_len - 4) * 8);
249  fscod = get_bits(&gbc, 2);
250  frmsizecod = get_bits(&gbc, 6);
251  bsid = get_bits(&gbc, 5);
252  bsmod = get_bits(&gbc, 3);
253  acmod = get_bits(&gbc, 3);
254  if (acmod == 2) {
255  skip_bits(&gbc, 2); // dsurmod
256  } else {
257  if ((acmod & 1) && acmod != 1)
258  skip_bits(&gbc, 2); // cmixlev
259  if (acmod & 4)
260  skip_bits(&gbc, 2); // surmixlev
261  }
262  lfeon = get_bits1(&gbc);
263 
264  init_put_bits(&pbc, buf, sizeof(buf));
265  put_bits(&pbc, 2, fscod);
266  put_bits(&pbc, 5, bsid);
267  put_bits(&pbc, 3, bsmod);
268  put_bits(&pbc, 3, acmod);
269  put_bits(&pbc, 1, lfeon);
270  put_bits(&pbc, 5, frmsizecod >> 1); // bit_rate_code
271  put_bits(&pbc, 5, 0); // reserved
272 
273  flush_put_bits(&pbc);
274  avio_write(pb, buf, sizeof(buf));
275 
276  return 11;
277 }
278 
284 {
285  avio_write(pb, track->enc->extradata, track->enc->extradata_size);
286  return track->enc->extradata_size;
287 }
288 
289 static void put_descr(AVIOContext *pb, int tag, unsigned int size)
290 {
291  int i = 3;
292  avio_w8(pb, tag);
293  for (; i > 0; i--)
294  avio_w8(pb, (size >> (7 * i)) | 0x80);
295  avio_w8(pb, size & 0x7F);
296 }
297 
298 static int mov_write_esds_tag(AVIOContext *pb, MOVTrack *track) // Basic
299 {
300  int64_t pos = avio_tell(pb);
301  int decoder_specific_info_len = track->vos_len ? 5 + track->vos_len : 0;
302 
303  avio_wb32(pb, 0); // size
304  ffio_wfourcc(pb, "esds");
305  avio_wb32(pb, 0); // Version
306 
307  // ES descriptor
308  put_descr(pb, 0x03, 3 + 5+13 + decoder_specific_info_len + 5+1);
309  avio_wb16(pb, track->track_id);
310  avio_w8(pb, 0x00); // flags (= no flags)
311 
312  // DecoderConfig descriptor
313  put_descr(pb, 0x04, 13 + decoder_specific_info_len);
314 
315  // Object type indication
316  if ((track->enc->codec_id == AV_CODEC_ID_MP2 ||
317  track->enc->codec_id == AV_CODEC_ID_MP3) &&
318  track->enc->sample_rate > 24000)
319  avio_w8(pb, 0x6B); // 11172-3
320  else
322 
323  // the following fields is made of 6 bits to identify the streamtype (4 for video, 5 for audio)
324  // plus 1 bit to indicate upstream and 1 bit set to 1 (reserved)
325  if (track->enc->codec_id == AV_CODEC_ID_DVD_SUBTITLE)
326  avio_w8(pb, (0x38 << 2) | 1); // flags (= NeroSubpicStream)
327  else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
328  avio_w8(pb, 0x15); // flags (= Audiostream)
329  else
330  avio_w8(pb, 0x11); // flags (= Visualstream)
331 
332  avio_wb24(pb, track->enc->rc_buffer_size >> 3); // Buffersize DB
333 
334  avio_wb32(pb, FFMAX(track->enc->bit_rate, track->enc->rc_max_rate)); // maxbitrate (FIXME should be max rate in any 1 sec window)
335  if (track->enc->rc_max_rate != track->enc->rc_min_rate ||
336  track->enc->rc_min_rate == 0)
337  avio_wb32(pb, 0); // vbr
338  else
339  avio_wb32(pb, track->enc->rc_max_rate); // avg bitrate
340 
341  if (track->vos_len) {
342  // DecoderSpecific info descriptor
343  put_descr(pb, 0x05, track->vos_len);
344  avio_write(pb, track->vos_data, track->vos_len);
345  }
346 
347  // SL descriptor
348  put_descr(pb, 0x06, 1);
349  avio_w8(pb, 0x02);
350  return update_size(pb, pos);
351 }
352 
353 static int mov_write_ms_tag(AVIOContext *pb, MOVTrack *track)
354 {
355  int64_t pos = avio_tell(pb);
356  avio_wb32(pb, 0);
357  avio_wl32(pb, track->tag); // store it byteswapped
358  track->enc->codec_tag = av_bswap16(track->tag >> 16);
359  ff_put_wav_header(pb, track->enc);
360  return update_size(pb, pos);
361 }
362 
363 static int mov_write_wfex_tag(AVIOContext *pb, MOVTrack *track)
364 {
365  int64_t pos = avio_tell(pb);
366  avio_wb32(pb, 0);
367  ffio_wfourcc(pb, "wfex");
368  ff_put_wav_header(pb, track->enc);
369  return update_size(pb, pos);
370 }
371 
372 static int mov_write_chan_tag(AVIOContext *pb, MOVTrack *track)
373 {
374  uint32_t layout_tag, bitmap;
375  int64_t pos = avio_tell(pb);
376 
377  layout_tag = ff_mov_get_channel_layout_tag(track->enc->codec_id,
378  track->enc->channel_layout,
379  &bitmap);
380  if (!layout_tag) {
381  av_log(track->enc, AV_LOG_WARNING, "not writing 'chan' tag due to "
382  "lack of channel information\n");
383  return 0;
384  }
385 
386  avio_wb32(pb, 0); // Size
387  ffio_wfourcc(pb, "chan"); // Type
388  avio_w8(pb, 0); // Version
389  avio_wb24(pb, 0); // Flags
390  avio_wb32(pb, layout_tag); // mChannelLayoutTag
391  avio_wb32(pb, bitmap); // mChannelBitmap
392  avio_wb32(pb, 0); // mNumberChannelDescriptions
393 
394  return update_size(pb, pos);
395 }
396 
397 static int mov_write_wave_tag(AVIOContext *pb, MOVTrack *track)
398 {
399  int64_t pos = avio_tell(pb);
400 
401  avio_wb32(pb, 0); /* size */
402  ffio_wfourcc(pb, "wave");
403 
404  avio_wb32(pb, 12); /* size */
405  ffio_wfourcc(pb, "frma");
406  avio_wl32(pb, track->tag);
407 
408  if (track->enc->codec_id == AV_CODEC_ID_AAC) {
409  /* useless atom needed by mplayer, ipod, not needed by quicktime */
410  avio_wb32(pb, 12); /* size */
411  ffio_wfourcc(pb, "mp4a");
412  avio_wb32(pb, 0);
413  mov_write_esds_tag(pb, track);
414  } else if (track->enc->codec_id == AV_CODEC_ID_AMR_NB) {
415  mov_write_amr_tag(pb, track);
416  } else if (track->enc->codec_id == AV_CODEC_ID_AC3) {
417  mov_write_ac3_tag(pb, track);
418  } else if (track->enc->codec_id == AV_CODEC_ID_ALAC) {
419  mov_write_extradata_tag(pb, track);
420  } else if (track->enc->codec_id == AV_CODEC_ID_ADPCM_MS ||
422  mov_write_ms_tag(pb, track);
423  }
424 
425  avio_wb32(pb, 8); /* size */
426  avio_wb32(pb, 0); /* null tag */
427 
428  return update_size(pb, pos);
429 }
430 
431 static int mov_write_dvc1_structs(MOVTrack *track, uint8_t *buf)
432 {
433  uint8_t *unescaped;
434  const uint8_t *start, *next, *end = track->vos_data + track->vos_len;
435  int unescaped_size, seq_found = 0;
436  int level = 0, interlace = 0;
437  int packet_seq = track->vc1_info.packet_seq;
438  int packet_entry = track->vc1_info.packet_entry;
439  int slices = track->vc1_info.slices;
440  PutBitContext pbc;
441 
442  if (track->start_dts == AV_NOPTS_VALUE) {
443  /* No packets written yet, vc1_info isn't authoritative yet. */
444  /* Assume inline sequence and entry headers. This will be
445  * overwritten at the end if the file is seekable. */
446  packet_seq = packet_entry = 1;
447  }
448 
449  unescaped = av_mallocz(track->vos_len + FF_INPUT_BUFFER_PADDING_SIZE);
450  if (!unescaped)
451  return AVERROR(ENOMEM);
452  start = find_next_marker(track->vos_data, end);
453  for (next = start; next < end; start = next) {
454  GetBitContext gb;
455  int size;
456  next = find_next_marker(start + 4, end);
457  size = next - start - 4;
458  if (size <= 0)
459  continue;
460  unescaped_size = vc1_unescape_buffer(start + 4, size, unescaped);
461  init_get_bits(&gb, unescaped, 8 * unescaped_size);
462  if (AV_RB32(start) == VC1_CODE_SEQHDR) {
463  int profile = get_bits(&gb, 2);
464  if (profile != PROFILE_ADVANCED) {
465  av_free(unescaped);
466  return AVERROR(ENOSYS);
467  }
468  seq_found = 1;
469  level = get_bits(&gb, 3);
470  /* chromaformat, frmrtq_postproc, bitrtq_postproc, postprocflag,
471  * width, height */
472  skip_bits_long(&gb, 2 + 3 + 5 + 1 + 2*12);
473  skip_bits(&gb, 1); /* broadcast */
474  interlace = get_bits1(&gb);
475  skip_bits(&gb, 4); /* tfcntrflag, finterpflag, reserved, psf */
476  }
477  }
478  if (!seq_found) {
479  av_free(unescaped);
480  return AVERROR(ENOSYS);
481  }
482 
483  init_put_bits(&pbc, buf, 7);
484  /* VC1DecSpecStruc */
485  put_bits(&pbc, 4, 12); /* profile - advanced */
486  put_bits(&pbc, 3, level);
487  put_bits(&pbc, 1, 0); /* reserved */
488  /* VC1AdvDecSpecStruc */
489  put_bits(&pbc, 3, level);
490  put_bits(&pbc, 1, 0); /* cbr */
491  put_bits(&pbc, 6, 0); /* reserved */
492  put_bits(&pbc, 1, !interlace); /* no interlace */
493  put_bits(&pbc, 1, !packet_seq); /* no multiple seq */
494  put_bits(&pbc, 1, !packet_entry); /* no multiple entry */
495  put_bits(&pbc, 1, !slices); /* no slice code */
496  put_bits(&pbc, 1, 0); /* no bframe */
497  put_bits(&pbc, 1, 0); /* reserved */
498 
499  /* framerate */
500  if (track->st->avg_frame_rate.num > 0 && track->st->avg_frame_rate.den > 0)
501  put_bits32(&pbc, track->st->avg_frame_rate.num / track->st->avg_frame_rate.den);
502  else
503  put_bits32(&pbc, 0xffffffff);
504 
505  flush_put_bits(&pbc);
506 
507  av_free(unescaped);
508 
509  return 0;
510 }
511 
512 static int mov_write_dvc1_tag(AVIOContext *pb, MOVTrack *track)
513 {
514  uint8_t buf[7] = { 0 };
515  int ret;
516 
517  if ((ret = mov_write_dvc1_structs(track, buf)) < 0)
518  return ret;
519 
520  avio_wb32(pb, track->vos_len + 8 + sizeof(buf));
521  ffio_wfourcc(pb, "dvc1");
522  track->vc1_info.struct_offset = avio_tell(pb);
523  avio_write(pb, buf, sizeof(buf));
524  avio_write(pb, track->vos_data, track->vos_len);
525 
526  return 0;
527 }
528 
529 static int mov_write_glbl_tag(AVIOContext *pb, MOVTrack *track)
530 {
531  avio_wb32(pb, track->vos_len + 8);
532  ffio_wfourcc(pb, "glbl");
533  avio_write(pb, track->vos_data, track->vos_len);
534  return 8 + track->vos_len;
535 }
536 
542 {
543  switch (codec_id) {
546  return 11;
549  return 9;
550  case AV_CODEC_ID_PCM_U8:
551  return 10;
555  return 14;
556  case AV_CODEC_ID_PCM_S8:
560  return 12;
561  default:
562  return 0;
563  }
564 }
565 
566 static int get_cluster_duration(MOVTrack *track, int cluster_idx)
567 {
568  int64_t next_dts;
569 
570  if (cluster_idx >= track->entry)
571  return 0;
572 
573  if (cluster_idx + 1 == track->entry)
574  next_dts = track->track_duration + track->start_dts;
575  else
576  next_dts = track->cluster[cluster_idx + 1].dts;
577 
578  return next_dts - track->cluster[cluster_idx].dts;
579 }
580 
582 {
583  int i, first_duration;
584 
585  /* use 1 for raw PCM */
586  if (!track->audio_vbr)
587  return 1;
588 
589  /* check to see if duration is constant for all clusters */
590  if (!track->entry)
591  return 0;
592  first_duration = get_cluster_duration(track, 0);
593  for (i = 1; i < track->entry; i++) {
594  if (get_cluster_duration(track, i) != first_duration)
595  return 0;
596  }
597  return first_duration;
598 }
599 
600 static int mov_write_audio_tag(AVIOContext *pb, MOVTrack *track)
601 {
602  int64_t pos = avio_tell(pb);
603  int version = 0;
604  uint32_t tag = track->tag;
605 
606  if (track->mode == MODE_MOV) {
607  if (mov_get_lpcm_flags(track->enc->codec_id))
608  tag = AV_RL32("lpcm");
609  version = 2;
610  }
611 
612  avio_wb32(pb, 0); /* size */
613  avio_wl32(pb, tag); // store it byteswapped
614  avio_wb32(pb, 0); /* Reserved */
615  avio_wb16(pb, 0); /* Reserved */
616  avio_wb16(pb, 1); /* Data-reference index, XXX == 1 */
617 
618  /* SoundDescription */
619  avio_wb16(pb, version); /* Version */
620  avio_wb16(pb, 0); /* Revision level */
621  avio_wb32(pb, 0); /* Reserved */
622 
623  if (version == 2) {
624  avio_wb16(pb, 3);
625  avio_wb16(pb, 16);
626  avio_wb16(pb, 0xfffe);
627  avio_wb16(pb, 0);
628  avio_wb32(pb, 0x00010000);
629  avio_wb32(pb, 72);
630  avio_wb64(pb, av_double2int(track->enc->sample_rate));
631  avio_wb32(pb, track->enc->channels);
632  avio_wb32(pb, 0x7F000000);
635  avio_wb32(pb, track->sample_size);
636  avio_wb32(pb, get_samples_per_packet(track));
637  } else {
638  /* reserved for mp4/3gp */
639  avio_wb16(pb, 2);
640  avio_wb16(pb, 16);
641  avio_wb16(pb, 0);
642 
643  avio_wb16(pb, 0); /* packet size (= 0) */
644  avio_wb16(pb, track->enc->sample_rate <= UINT16_MAX ?
645  track->enc->sample_rate : 0);
646  avio_wb16(pb, 0); /* Reserved */
647  }
648 
649  if (track->mode == MODE_MOV &&
650  (track->enc->codec_id == AV_CODEC_ID_AAC ||
651  track->enc->codec_id == AV_CODEC_ID_AC3 ||
652  track->enc->codec_id == AV_CODEC_ID_AMR_NB ||
653  track->enc->codec_id == AV_CODEC_ID_ALAC ||
654  track->enc->codec_id == AV_CODEC_ID_ADPCM_MS ||
656  mov_write_wave_tag(pb, track);
657  else if (track->tag == MKTAG('m','p','4','a'))
658  mov_write_esds_tag(pb, track);
659  else if (track->enc->codec_id == AV_CODEC_ID_AMR_NB)
660  mov_write_amr_tag(pb, track);
661  else if (track->enc->codec_id == AV_CODEC_ID_AC3)
662  mov_write_ac3_tag(pb, track);
663  else if (track->enc->codec_id == AV_CODEC_ID_ALAC)
664  mov_write_extradata_tag(pb, track);
665  else if (track->enc->codec_id == AV_CODEC_ID_WMAPRO)
666  mov_write_wfex_tag(pb, track);
667  else if (track->vos_len > 0)
668  mov_write_glbl_tag(pb, track);
669 
670  if (track->mode == MODE_MOV && track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
671  mov_write_chan_tag(pb, track);
672 
673  return update_size(pb, pos);
674 }
675 
677 {
678  avio_wb32(pb, 0xf); /* size */
679  ffio_wfourcc(pb, "d263");
680  ffio_wfourcc(pb, "FFMP");
681  avio_w8(pb, 0); /* decoder version */
682  /* FIXME use AVCodecContext level/profile, when encoder will set values */
683  avio_w8(pb, 0xa); /* level */
684  avio_w8(pb, 0); /* profile */
685  return 0xf;
686 }
687 
688 /* TODO: No idea about these values */
690 {
691  avio_wb32(pb, 0x15);
692  ffio_wfourcc(pb, "SMI ");
693  ffio_wfourcc(pb, "SEQH");
694  avio_wb32(pb, 0x5);
695  avio_wb32(pb, 0xe2c0211d);
696  avio_wb32(pb, 0xc0000000);
697  avio_w8(pb, 0);
698  return 0x15;
699 }
700 
701 static int mov_write_avcc_tag(AVIOContext *pb, MOVTrack *track)
702 {
703  int64_t pos = avio_tell(pb);
704 
705  avio_wb32(pb, 0);
706  ffio_wfourcc(pb, "avcC");
707  ff_isom_write_avcc(pb, track->vos_data, track->vos_len);
708  return update_size(pb, pos);
709 }
710 
711 static int mov_write_hvcc_tag(AVIOContext *pb, MOVTrack *track)
712 {
713  int64_t pos = avio_tell(pb);
714 
715  avio_wb32(pb, 0);
716  ffio_wfourcc(pb, "hvcC");
717  ff_isom_write_hvcc(pb, track->vos_data, track->vos_len, 0);
718  return update_size(pb, pos);
719 }
720 
721 /* also used by all avid codecs (dv, imx, meridien) and their variants */
722 static int mov_write_avid_tag(AVIOContext *pb, MOVTrack *track)
723 {
724  int i;
725  avio_wb32(pb, 24); /* size */
726  ffio_wfourcc(pb, "ACLR");
727  ffio_wfourcc(pb, "ACLR");
728  ffio_wfourcc(pb, "0001");
729  avio_wb32(pb, 2); /* yuv range: full 1 / normal 2 */
730  avio_wb32(pb, 0); /* unknown */
731 
732  avio_wb32(pb, 24); /* size */
733  ffio_wfourcc(pb, "APRG");
734  ffio_wfourcc(pb, "APRG");
735  ffio_wfourcc(pb, "0001");
736  avio_wb32(pb, 1); /* unknown */
737  avio_wb32(pb, 0); /* unknown */
738 
739  avio_wb32(pb, 120); /* size */
740  ffio_wfourcc(pb, "ARES");
741  ffio_wfourcc(pb, "ARES");
742  ffio_wfourcc(pb, "0001");
743  avio_wb32(pb, AV_RB32(track->vos_data + 0x28)); /* dnxhd cid, some id ? */
744  avio_wb32(pb, track->enc->width);
745  /* values below are based on samples created with quicktime and avid codecs */
746  if (track->vos_data[5] & 2) { // interlaced
747  avio_wb32(pb, track->enc->height / 2);
748  avio_wb32(pb, 2); /* unknown */
749  avio_wb32(pb, 0); /* unknown */
750  avio_wb32(pb, 4); /* unknown */
751  } else {
752  avio_wb32(pb, track->enc->height);
753  avio_wb32(pb, 1); /* unknown */
754  avio_wb32(pb, 0); /* unknown */
755  if (track->enc->height == 1080)
756  avio_wb32(pb, 5); /* unknown */
757  else
758  avio_wb32(pb, 6); /* unknown */
759  }
760  /* padding */
761  for (i = 0; i < 10; i++)
762  avio_wb64(pb, 0);
763 
764  /* extra padding for stsd needed */
765  avio_wb32(pb, 0);
766  return 0;
767 }
768 
770 {
771  int tag = track->enc->codec_tag;
772 
774  return 0;
775 
776  if (track->enc->codec_id == AV_CODEC_ID_H264) tag = MKTAG('a','v','c','1');
777  else if (track->enc->codec_id == AV_CODEC_ID_HEVC) tag = MKTAG('h','e','v','1');
778  else if (track->enc->codec_id == AV_CODEC_ID_AC3) tag = MKTAG('a','c','-','3');
779  else if (track->enc->codec_id == AV_CODEC_ID_DIRAC) tag = MKTAG('d','r','a','c');
780  else if (track->enc->codec_id == AV_CODEC_ID_MOV_TEXT) tag = MKTAG('t','x','3','g');
781  else if (track->enc->codec_id == AV_CODEC_ID_VC1) tag = MKTAG('v','c','-','1');
782  else if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) tag = MKTAG('m','p','4','v');
783  else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) tag = MKTAG('m','p','4','a');
784  else if (track->enc->codec_id == AV_CODEC_ID_DVD_SUBTITLE) tag = MKTAG('m','p','4','s');
785 
786  return tag;
787 }
788 
789 static const AVCodecTag codec_ipod_tags[] = {
790  { AV_CODEC_ID_H264, MKTAG('a','v','c','1') },
791  { AV_CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
792  { AV_CODEC_ID_AAC, MKTAG('m','p','4','a') },
793  { AV_CODEC_ID_ALAC, MKTAG('a','l','a','c') },
794  { AV_CODEC_ID_AC3, MKTAG('a','c','-','3') },
795  { AV_CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
796  { AV_CODEC_ID_MOV_TEXT, MKTAG('t','e','x','t') },
797  { AV_CODEC_ID_NONE, 0 },
798 };
799 
801 {
802  int tag = track->enc->codec_tag;
803 
804  // keep original tag for subs, ipod supports both formats
805  if (!(track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE &&
806  (tag == MKTAG('t', 'x', '3', 'g') ||
807  tag == MKTAG('t', 'e', 'x', 't'))))
808  tag = ff_codec_get_tag(codec_ipod_tags, track->enc->codec_id);
809 
810  if (!av_match_ext(s->filename, "m4a") && !av_match_ext(s->filename, "m4v"))
811  av_log(s, AV_LOG_WARNING, "Warning, extension is not .m4a nor .m4v "
812  "Quicktime/Ipod might not play the file\n");
813 
814  return tag;
815 }
816 
818 {
819  int tag;
820 
821  if (track->enc->width == 720) /* SD */
822  if (track->enc->height == 480) /* NTSC */
823  if (track->enc->pix_fmt == AV_PIX_FMT_YUV422P) tag = MKTAG('d','v','5','n');
824  else tag = MKTAG('d','v','c',' ');
825  else if (track->enc->pix_fmt == AV_PIX_FMT_YUV422P) tag = MKTAG('d','v','5','p');
826  else if (track->enc->pix_fmt == AV_PIX_FMT_YUV420P) tag = MKTAG('d','v','c','p');
827  else tag = MKTAG('d','v','p','p');
828  else if (track->enc->height == 720) /* HD 720 line */
829  if (track->st->time_base.den == 50) tag = MKTAG('d','v','h','q');
830  else tag = MKTAG('d','v','h','p');
831  else if (track->enc->height == 1080) /* HD 1080 line */
832  if (track->st->time_base.den == 25) tag = MKTAG('d','v','h','5');
833  else tag = MKTAG('d','v','h','6');
834  else {
835  av_log(s, AV_LOG_ERROR, "unsupported height for dv codec\n");
836  return 0;
837  }
838 
839  return tag;
840 }
841 
842 static const struct {
844  uint32_t tag;
845  unsigned bps;
846 } mov_pix_fmt_tags[] = {
847  { AV_PIX_FMT_YUYV422, MKTAG('y','u','v','s'), 0 },
848  { AV_PIX_FMT_UYVY422, MKTAG('2','v','u','y'), 0 },
849  { AV_PIX_FMT_RGB555BE,MKTAG('r','a','w',' '), 16 },
850  { AV_PIX_FMT_RGB555LE,MKTAG('L','5','5','5'), 16 },
851  { AV_PIX_FMT_RGB565LE,MKTAG('L','5','6','5'), 16 },
852  { AV_PIX_FMT_RGB565BE,MKTAG('B','5','6','5'), 16 },
853  { AV_PIX_FMT_GRAY16BE,MKTAG('b','1','6','g'), 16 },
854  { AV_PIX_FMT_RGB24, MKTAG('r','a','w',' '), 24 },
855  { AV_PIX_FMT_BGR24, MKTAG('2','4','B','G'), 24 },
856  { AV_PIX_FMT_ARGB, MKTAG('r','a','w',' '), 32 },
857  { AV_PIX_FMT_BGRA, MKTAG('B','G','R','A'), 32 },
858  { AV_PIX_FMT_RGBA, MKTAG('R','G','B','A'), 32 },
859  { AV_PIX_FMT_ABGR, MKTAG('A','B','G','R'), 32 },
860  { AV_PIX_FMT_RGB48BE, MKTAG('b','4','8','r'), 48 },
861 };
862 
864 {
865  int tag = track->enc->codec_tag;
866  int i;
867 
868  for (i = 0; i < FF_ARRAY_ELEMS(mov_pix_fmt_tags); i++) {
869  if (track->enc->pix_fmt == mov_pix_fmt_tags[i].pix_fmt) {
870  tag = mov_pix_fmt_tags[i].tag;
871  track->enc->bits_per_coded_sample = mov_pix_fmt_tags[i].bps;
872  break;
873  }
874  }
875 
876  return tag;
877 }
878 
880 {
881  int tag = track->enc->codec_tag;
882 
883  if (!tag || (track->enc->strict_std_compliance >= FF_COMPLIANCE_NORMAL &&
884  (track->enc->codec_id == AV_CODEC_ID_DVVIDEO ||
885  track->enc->codec_id == AV_CODEC_ID_RAWVIDEO ||
886  track->enc->codec_id == AV_CODEC_ID_H263 ||
887  av_get_bits_per_sample(track->enc->codec_id)))) { // pcm audio
888  if (track->enc->codec_id == AV_CODEC_ID_DVVIDEO)
889  tag = mov_get_dv_codec_tag(s, track);
890  else if (track->enc->codec_id == AV_CODEC_ID_RAWVIDEO)
891  tag = mov_get_rawvideo_codec_tag(s, track);
892  else if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
894  if (!tag) { // if no mac fcc found, try with Microsoft tags
896  if (tag)
897  av_log(s, AV_LOG_WARNING, "Using MS style video codec tag, "
898  "the file may be unplayable!\n");
899  }
900  } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
902  if (!tag) { // if no mac fcc found, try with Microsoft tags
903  int ms_tag = ff_codec_get_tag(ff_codec_wav_tags, track->enc->codec_id);
904  if (ms_tag) {
905  tag = MKTAG('m', 's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
906  av_log(s, AV_LOG_WARNING, "Using MS style audio codec tag, "
907  "the file may be unplayable!\n");
908  }
909  }
910  } else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)
912  }
913 
914  return tag;
915 }
916 
917 static const AVCodecTag codec_3gp_tags[] = {
918  { AV_CODEC_ID_H263, MKTAG('s','2','6','3') },
919  { AV_CODEC_ID_H264, MKTAG('a','v','c','1') },
920  { AV_CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
921  { AV_CODEC_ID_AAC, MKTAG('m','p','4','a') },
922  { AV_CODEC_ID_AMR_NB, MKTAG('s','a','m','r') },
923  { AV_CODEC_ID_AMR_WB, MKTAG('s','a','w','b') },
924  { AV_CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
925  { AV_CODEC_ID_NONE, 0 },
926 };
927 
928 static const AVCodecTag codec_f4v_tags[] = {
929  { AV_CODEC_ID_MP3, MKTAG('.','m','p','3') },
930  { AV_CODEC_ID_AAC, MKTAG('m','p','4','a') },
931  { AV_CODEC_ID_H264, MKTAG('a','v','c','1') },
932  { AV_CODEC_ID_VP6A, MKTAG('V','P','6','A') },
933  { AV_CODEC_ID_VP6F, MKTAG('V','P','6','F') },
934  { AV_CODEC_ID_NONE, 0 },
935 };
936 
938 {
939  int tag;
940 
941  if (track->mode == MODE_MP4 || track->mode == MODE_PSP)
942  tag = mp4_get_codec_tag(s, track);
943  else if (track->mode == MODE_ISM) {
944  tag = mp4_get_codec_tag(s, track);
945  if (!tag && track->enc->codec_id == AV_CODEC_ID_WMAPRO)
946  tag = MKTAG('w', 'm', 'a', ' ');
947  } else if (track->mode == MODE_IPOD)
948  tag = ipod_get_codec_tag(s, track);
949  else if (track->mode & MODE_3GP)
950  tag = ff_codec_get_tag(codec_3gp_tags, track->enc->codec_id);
951  else if (track->mode == MODE_F4V)
952  tag = ff_codec_get_tag(codec_f4v_tags, track->enc->codec_id);
953  else
954  tag = mov_get_codec_tag(s, track);
955 
956  return tag;
957 }
958 
964 {
965  avio_wb32(pb, 28);
966  ffio_wfourcc(pb, "uuid");
967  avio_wb32(pb, 0x6b6840f2);
968  avio_wb32(pb, 0x5f244fc5);
969  avio_wb32(pb, 0xba39a51b);
970  avio_wb32(pb, 0xcf0323f3);
971  avio_wb32(pb, 0x0);
972  return 28;
973 }
974 
975 static const uint16_t fiel_data[] = {
976  0x0000, 0x0100, 0x0201, 0x0206, 0x0209, 0x020e
977 };
978 
979 static int mov_write_fiel_tag(AVIOContext *pb, MOVTrack *track)
980 {
981  unsigned mov_field_order = 0;
982  if (track->enc->field_order < FF_ARRAY_ELEMS(fiel_data))
983  mov_field_order = fiel_data[track->enc->field_order];
984  else
985  return 0;
986  avio_wb32(pb, 10);
987  ffio_wfourcc(pb, "fiel");
988  avio_wb16(pb, mov_field_order);
989  return 10;
990 }
991 
993 {
994  int64_t pos = avio_tell(pb);
995  avio_wb32(pb, 0); /* size */
996  avio_wl32(pb, track->tag); // store it byteswapped
997  avio_wb32(pb, 0); /* Reserved */
998  avio_wb16(pb, 0); /* Reserved */
999  avio_wb16(pb, 1); /* Data-reference index */
1000 
1001  if (track->enc->codec_id == AV_CODEC_ID_DVD_SUBTITLE)
1002  mov_write_esds_tag(pb, track);
1003  else if (track->enc->extradata_size)
1004  avio_write(pb, track->enc->extradata, track->enc->extradata_size);
1005 
1006  return update_size(pb, pos);
1007 }
1008 
1009 static int mov_write_pasp_tag(AVIOContext *pb, MOVTrack *track)
1010 {
1011  AVRational sar;
1012  av_reduce(&sar.num, &sar.den, track->enc->sample_aspect_ratio.num,
1013  track->enc->sample_aspect_ratio.den, INT_MAX);
1014 
1015  avio_wb32(pb, 16);
1016  ffio_wfourcc(pb, "pasp");
1017  avio_wb32(pb, sar.num);
1018  avio_wb32(pb, sar.den);
1019  return 16;
1020 }
1021 
1023 {
1024  AVDictionaryEntry *encoder;
1025  int64_t pos = avio_tell(pb);
1026  char compressor_name[32] = { 0 };
1027 
1028  avio_wb32(pb, 0); /* size */
1029  avio_wl32(pb, track->tag); // store it byteswapped
1030  avio_wb32(pb, 0); /* Reserved */
1031  avio_wb16(pb, 0); /* Reserved */
1032  avio_wb16(pb, 1); /* Data-reference index */
1033 
1034  avio_wb16(pb, 0); /* Codec stream version */
1035  avio_wb16(pb, 0); /* Codec stream revision (=0) */
1036  if (track->mode == MODE_MOV) {
1037  ffio_wfourcc(pb, "FFMP"); /* Vendor */
1038  if (track->enc->codec_id == AV_CODEC_ID_RAWVIDEO) {
1039  avio_wb32(pb, 0); /* Temporal Quality */
1040  avio_wb32(pb, 0x400); /* Spatial Quality = lossless*/
1041  } else {
1042  avio_wb32(pb, 0x200); /* Temporal Quality = normal */
1043  avio_wb32(pb, 0x200); /* Spatial Quality = normal */
1044  }
1045  } else {
1046  avio_wb32(pb, 0); /* Reserved */
1047  avio_wb32(pb, 0); /* Reserved */
1048  avio_wb32(pb, 0); /* Reserved */
1049  }
1050  avio_wb16(pb, track->enc->width); /* Video width */
1051  avio_wb16(pb, track->height); /* Video height */
1052  avio_wb32(pb, 0x00480000); /* Horizontal resolution 72dpi */
1053  avio_wb32(pb, 0x00480000); /* Vertical resolution 72dpi */
1054  avio_wb32(pb, 0); /* Data size (= 0) */
1055  avio_wb16(pb, 1); /* Frame count (= 1) */
1056 
1057  /* FIXME not sure, ISO 14496-1 draft where it shall be set to 0 */
1058  if (track->mode == MODE_MOV &&
1059  (encoder = av_dict_get(track->st->metadata, "encoder", NULL, 0)))
1060  av_strlcpy(compressor_name, encoder->value, 32);
1061  avio_w8(pb, strlen(compressor_name));
1062  avio_write(pb, compressor_name, 31);
1063 
1064  if (track->mode == MODE_MOV && track->enc->bits_per_coded_sample)
1065  avio_wb16(pb, track->enc->bits_per_coded_sample);
1066  else
1067  avio_wb16(pb, 0x18); /* Reserved */
1068  avio_wb16(pb, 0xffff); /* Reserved */
1069  if (track->tag == MKTAG('m','p','4','v'))
1070  mov_write_esds_tag(pb, track);
1071  else if (track->enc->codec_id == AV_CODEC_ID_H263)
1072  mov_write_d263_tag(pb);
1073  else if (track->enc->codec_id == AV_CODEC_ID_SVQ3)
1074  mov_write_svq3_tag(pb);
1075  else if (track->enc->codec_id == AV_CODEC_ID_DNXHD)
1076  mov_write_avid_tag(pb, track);
1077  else if (track->enc->codec_id == AV_CODEC_ID_HEVC)
1078  mov_write_hvcc_tag(pb, track);
1079  else if (track->enc->codec_id == AV_CODEC_ID_H264) {
1080  mov_write_avcc_tag(pb, track);
1081  if (track->mode == MODE_IPOD)
1083  } else if (track->enc->field_order != AV_FIELD_UNKNOWN)
1084  mov_write_fiel_tag(pb, track);
1085  else if (track->enc->codec_id == AV_CODEC_ID_VC1 && track->vos_len > 0)
1086  mov_write_dvc1_tag(pb, track);
1087  else if (track->enc->codec_id == AV_CODEC_ID_VP6F ||
1088  track->enc->codec_id == AV_CODEC_ID_VP6A) {
1089  /* Don't write any potential extradata here - the cropping
1090  * is signalled via the normal width/height fields. */
1091  } else if (track->vos_len > 0)
1092  mov_write_glbl_tag(pb, track);
1093 
1094  if (track->enc->sample_aspect_ratio.den && track->enc->sample_aspect_ratio.num &&
1095  track->enc->sample_aspect_ratio.den != track->enc->sample_aspect_ratio.num) {
1096  mov_write_pasp_tag(pb, track);
1097  }
1098 
1099  return update_size(pb, pos);
1100 }
1101 
1102 static int mov_write_rtp_tag(AVIOContext *pb, MOVTrack *track)
1103 {
1104  int64_t pos = avio_tell(pb);
1105  avio_wb32(pb, 0); /* size */
1106  ffio_wfourcc(pb, "rtp ");
1107  avio_wb32(pb, 0); /* Reserved */
1108  avio_wb16(pb, 0); /* Reserved */
1109  avio_wb16(pb, 1); /* Data-reference index */
1110 
1111  avio_wb16(pb, 1); /* Hint track version */
1112  avio_wb16(pb, 1); /* Highest compatible version */
1113  avio_wb32(pb, track->max_packet_size); /* Max packet size */
1114 
1115  avio_wb32(pb, 12); /* size */
1116  ffio_wfourcc(pb, "tims");
1117  avio_wb32(pb, track->timescale);
1118 
1119  return update_size(pb, pos);
1120 }
1121 
1122 static int mov_write_tmcd_tag(AVIOContext *pb, MOVTrack *track)
1123 {
1124  int64_t pos = avio_tell(pb);
1125 
1126  avio_wb32(pb, 0); /* size */
1127  ffio_wfourcc(pb, "tmcd"); /* Data format */
1128  avio_wb32(pb, 0); /* Reserved */
1129  avio_wb32(pb, 1); /* Data reference index */
1130  if (track->enc->extradata_size)
1131  avio_write(pb, track->enc->extradata, track->enc->extradata_size);
1132  return update_size(pb, pos);
1133 }
1134 
1135 static int mov_write_stsd_tag(AVIOContext *pb, MOVTrack *track)
1136 {
1137  int64_t pos = avio_tell(pb);
1138  avio_wb32(pb, 0); /* size */
1139  ffio_wfourcc(pb, "stsd");
1140  avio_wb32(pb, 0); /* version & flags */
1141  avio_wb32(pb, 1); /* entry count */
1142  if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO)
1143  mov_write_video_tag(pb, track);
1144  else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
1145  mov_write_audio_tag(pb, track);
1146  else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)
1147  mov_write_subtitle_tag(pb, track);
1148  else if (track->enc->codec_tag == MKTAG('r','t','p',' '))
1149  mov_write_rtp_tag(pb, track);
1150  else if (track->enc->codec_tag == MKTAG('t','m','c','d'))
1151  mov_write_tmcd_tag(pb, track);
1152  return update_size(pb, pos);
1153 }
1154 
1155 static int mov_write_ctts_tag(AVIOContext *pb, MOVTrack *track)
1156 {
1157  MOVStts *ctts_entries;
1158  uint32_t entries = 0;
1159  uint32_t atom_size;
1160  int i;
1161 
1162  ctts_entries = av_malloc((track->entry + 1) * sizeof(*ctts_entries)); /* worst case */
1163  ctts_entries[0].count = 1;
1164  ctts_entries[0].duration = track->cluster[0].cts;
1165  for (i = 1; i < track->entry; i++) {
1166  if (track->cluster[i].cts == ctts_entries[entries].duration) {
1167  ctts_entries[entries].count++; /* compress */
1168  } else {
1169  entries++;
1170  ctts_entries[entries].duration = track->cluster[i].cts;
1171  ctts_entries[entries].count = 1;
1172  }
1173  }
1174  entries++; /* last one */
1175  atom_size = 16 + (entries * 8);
1176  avio_wb32(pb, atom_size); /* size */
1177  ffio_wfourcc(pb, "ctts");
1178  avio_wb32(pb, 0); /* version & flags */
1179  avio_wb32(pb, entries); /* entry count */
1180  for (i = 0; i < entries; i++) {
1181  avio_wb32(pb, ctts_entries[i].count);
1182  avio_wb32(pb, ctts_entries[i].duration);
1183  }
1184  av_free(ctts_entries);
1185  return atom_size;
1186 }
1187 
1188 /* Time to sample atom */
1189 static int mov_write_stts_tag(AVIOContext *pb, MOVTrack *track)
1190 {
1191  MOVStts *stts_entries;
1192  uint32_t entries = -1;
1193  uint32_t atom_size;
1194  int i;
1195 
1196  if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO && !track->audio_vbr) {
1197  stts_entries = av_malloc(sizeof(*stts_entries)); /* one entry */
1198  stts_entries[0].count = track->sample_count;
1199  stts_entries[0].duration = 1;
1200  entries = 1;
1201  } else {
1202  stts_entries = track->entry ?
1203  av_malloc(track->entry * sizeof(*stts_entries)) : /* worst case */
1204  NULL;
1205  for (i = 0; i < track->entry; i++) {
1206  int duration = get_cluster_duration(track, i);
1207  if (i && duration == stts_entries[entries].duration) {
1208  stts_entries[entries].count++; /* compress */
1209  } else {
1210  entries++;
1211  stts_entries[entries].duration = duration;
1212  stts_entries[entries].count = 1;
1213  }
1214  }
1215  entries++; /* last one */
1216  }
1217  atom_size = 16 + (entries * 8);
1218  avio_wb32(pb, atom_size); /* size */
1219  ffio_wfourcc(pb, "stts");
1220  avio_wb32(pb, 0); /* version & flags */
1221  avio_wb32(pb, entries); /* entry count */
1222  for (i = 0; i < entries; i++) {
1223  avio_wb32(pb, stts_entries[i].count);
1224  avio_wb32(pb, stts_entries[i].duration);
1225  }
1226  av_free(stts_entries);
1227  return atom_size;
1228 }
1229 
1231 {
1232  avio_wb32(pb, 28); /* size */
1233  ffio_wfourcc(pb, "dref");
1234  avio_wb32(pb, 0); /* version & flags */
1235  avio_wb32(pb, 1); /* entry count */
1236 
1237  avio_wb32(pb, 0xc); /* size */
1238  ffio_wfourcc(pb, "url ");
1239  avio_wb32(pb, 1); /* version & flags */
1240 
1241  return 28;
1242 }
1243 
1244 static int mov_write_stbl_tag(AVIOContext *pb, MOVTrack *track)
1245 {
1246  int64_t pos = avio_tell(pb);
1247  avio_wb32(pb, 0); /* size */
1248  ffio_wfourcc(pb, "stbl");
1249  mov_write_stsd_tag(pb, track);
1250  mov_write_stts_tag(pb, track);
1251  if ((track->enc->codec_type == AVMEDIA_TYPE_VIDEO ||
1252  track->enc->codec_tag == MKTAG('r','t','p',' ')) &&
1253  track->has_keyframes && track->has_keyframes < track->entry)
1254  mov_write_stss_tag(pb, track, MOV_SYNC_SAMPLE);
1255  if (track->mode == MODE_MOV && track->flags & MOV_TRACK_STPS)
1257  if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO &&
1258  track->flags & MOV_TRACK_CTTS)
1259  mov_write_ctts_tag(pb, track);
1260  mov_write_stsc_tag(pb, track);
1261  mov_write_stsz_tag(pb, track);
1262  mov_write_stco_tag(pb, track);
1263  return update_size(pb, pos);
1264 }
1265 
1267 {
1268  int64_t pos = avio_tell(pb);
1269  avio_wb32(pb, 0); /* size */
1270  ffio_wfourcc(pb, "dinf");
1271  mov_write_dref_tag(pb);
1272  return update_size(pb, pos);
1273 }
1274 
1276 {
1277  avio_wb32(pb, 12);
1278  ffio_wfourcc(pb, "nmhd");
1279  avio_wb32(pb, 0);
1280  return 12;
1281 }
1282 
1284 {
1285  avio_wb32(pb, 0x20); /* size */
1286  ffio_wfourcc(pb, "gmhd");
1287  avio_wb32(pb, 0x18); /* gmin size */
1288  ffio_wfourcc(pb, "gmin");/* generic media info */
1289  avio_wb32(pb, 0); /* version & flags */
1290  avio_wb16(pb, 0x40); /* graphics mode = */
1291  avio_wb16(pb, 0x8000); /* opColor (r?) */
1292  avio_wb16(pb, 0x8000); /* opColor (g?) */
1293  avio_wb16(pb, 0x8000); /* opColor (b?) */
1294  avio_wb16(pb, 0); /* balance */
1295  avio_wb16(pb, 0); /* reserved */
1296  return 0x20;
1297 }
1298 
1300 {
1301  avio_wb32(pb, 16); /* size */
1302  ffio_wfourcc(pb, "smhd");
1303  avio_wb32(pb, 0); /* version & flags */
1304  avio_wb16(pb, 0); /* reserved (balance, normally = 0) */
1305  avio_wb16(pb, 0); /* reserved */
1306  return 16;
1307 }
1308 
1310 {
1311  avio_wb32(pb, 0x14); /* size (always 0x14) */
1312  ffio_wfourcc(pb, "vmhd");
1313  avio_wb32(pb, 0x01); /* version & flags */
1314  avio_wb64(pb, 0); /* reserved (graphics mode = copy) */
1315  return 0x14;
1316 }
1317 
1318 static int is_clcp_track(MOVTrack *track)
1319 {
1320  return track->tag == MKTAG('c','7','0','8') ||
1321  track->tag == MKTAG('c','6','0','8');
1322 }
1323 
1324 static int mov_write_hdlr_tag(AVIOContext *pb, MOVTrack *track)
1325 {
1326  const char *hdlr, *descr = NULL, *hdlr_type = NULL;
1327  int64_t pos = avio_tell(pb);
1328 
1329  hdlr = "dhlr";
1330  hdlr_type = "url ";
1331  descr = "DataHandler";
1332 
1333  if (track) {
1334  hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
1335  if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
1336  hdlr_type = "vide";
1337  descr = "VideoHandler";
1338  } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
1339  hdlr_type = "soun";
1340  descr = "SoundHandler";
1341  } else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) {
1342  if (track->tag == MKTAG('t','x','3','g')) {
1343  hdlr_type = "sbtl";
1344  } else if (track->tag == MKTAG('m','p','4','s')) {
1345  hdlr_type = "subp";
1346  } else if (is_clcp_track(track)) {
1347  hdlr_type = "clcp";
1348  } else {
1349  hdlr_type = "text";
1350  }
1351  descr = "SubtitleHandler";
1352  } else if (track->enc->codec_tag == MKTAG('r','t','p',' ')) {
1353  hdlr_type = "hint";
1354  descr = "HintHandler";
1355  } else if (track->enc->codec_tag == MKTAG('t','m','c','d')) {
1356  hdlr_type = "tmcd";
1357  descr = "TimeCodeHandler";
1358  } else {
1359  char tag_buf[32];
1360  av_get_codec_tag_string(tag_buf, sizeof(tag_buf),
1361  track->enc->codec_tag);
1362 
1363  av_log(track->enc, AV_LOG_WARNING,
1364  "Unknown hldr_type for %s / 0x%04X, writing dummy values\n",
1365  tag_buf, track->enc->codec_tag);
1366  }
1367  if (track->st) {
1368  // hdlr.name is used by some players to identify the content title
1369  // of the track. So if an alternate handler description is
1370  // specified, use it.
1371  AVDictionaryEntry *t;
1372  t = av_dict_get(track->st->metadata, "handler", NULL, 0);
1373  if (t && utf8len(t->value))
1374  descr = t->value;
1375  }
1376  }
1377 
1378  avio_wb32(pb, 0); /* size */
1379  ffio_wfourcc(pb, "hdlr");
1380  avio_wb32(pb, 0); /* Version & flags */
1381  avio_write(pb, hdlr, 4); /* handler */
1382  ffio_wfourcc(pb, hdlr_type); /* handler type */
1383  avio_wb32(pb, 0); /* reserved */
1384  avio_wb32(pb, 0); /* reserved */
1385  avio_wb32(pb, 0); /* reserved */
1386  if (!track || track->mode == MODE_MOV)
1387  avio_w8(pb, strlen(descr)); /* pascal string */
1388  avio_write(pb, descr, strlen(descr)); /* handler description */
1389  if (track && track->mode != MODE_MOV)
1390  avio_w8(pb, 0); /* c string */
1391  return update_size(pb, pos);
1392 }
1393 
1395 {
1396  /* This atom must be present, but leaving the values at zero
1397  * seems harmless. */
1398  avio_wb32(pb, 28); /* size */
1399  ffio_wfourcc(pb, "hmhd");
1400  avio_wb32(pb, 0); /* version, flags */
1401  avio_wb16(pb, 0); /* maxPDUsize */
1402  avio_wb16(pb, 0); /* avgPDUsize */
1403  avio_wb32(pb, 0); /* maxbitrate */
1404  avio_wb32(pb, 0); /* avgbitrate */
1405  avio_wb32(pb, 0); /* reserved */
1406  return 28;
1407 }
1408 
1409 static int mov_write_minf_tag(AVIOContext *pb, MOVTrack *track)
1410 {
1411  int64_t pos = avio_tell(pb);
1412  avio_wb32(pb, 0); /* size */
1413  ffio_wfourcc(pb, "minf");
1414  if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO)
1415  mov_write_vmhd_tag(pb);
1416  else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
1417  mov_write_smhd_tag(pb);
1418  else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) {
1419  if (track->tag == MKTAG('t','e','x','t') || is_clcp_track(track)) {
1420  mov_write_gmhd_tag(pb);
1421  } else {
1422  mov_write_nmhd_tag(pb);
1423  }
1424  } else if (track->tag == MKTAG('r','t','p',' ')) {
1425  mov_write_hmhd_tag(pb);
1426  } else if (track->tag == MKTAG('t','m','c','d')) {
1427  mov_write_gmhd_tag(pb);
1428  }
1429  if (track->mode == MODE_MOV) /* FIXME: Why do it for MODE_MOV only ? */
1430  mov_write_hdlr_tag(pb, NULL);
1431  mov_write_dinf_tag(pb);
1432  mov_write_stbl_tag(pb, track);
1433  return update_size(pb, pos);
1434 }
1435 
1436 static int mov_write_mdhd_tag(AVIOContext *pb, MOVTrack *track)
1437 {
1438  int version = track->track_duration < INT32_MAX ? 0 : 1;
1439 
1440  if (track->mode == MODE_ISM)
1441  version = 1;
1442 
1443  (version == 1) ? avio_wb32(pb, 44) : avio_wb32(pb, 32); /* size */
1444  ffio_wfourcc(pb, "mdhd");
1445  avio_w8(pb, version);
1446  avio_wb24(pb, 0); /* flags */
1447  if (version == 1) {
1448  avio_wb64(pb, track->time);
1449  avio_wb64(pb, track->time);
1450  } else {
1451  avio_wb32(pb, track->time); /* creation time */
1452  avio_wb32(pb, track->time); /* modification time */
1453  }
1454  avio_wb32(pb, track->timescale); /* time scale (sample rate for audio) */
1455  if (!track->entry)
1456  (version == 1) ? avio_wb64(pb, UINT64_C(0xffffffffffffffff)) : avio_wb32(pb, 0xffffffff);
1457  else
1458  (version == 1) ? avio_wb64(pb, track->track_duration) : avio_wb32(pb, track->track_duration); /* duration */
1459  avio_wb16(pb, track->language); /* language */
1460  avio_wb16(pb, 0); /* reserved (quality) */
1461 
1462  if (version != 0 && track->mode == MODE_MOV) {
1464  "FATAL error, file duration too long for timebase, this file will not be\n"
1465  "playable with quicktime. Choose a different timebase or a different\n"
1466  "container format\n");
1467  }
1468 
1469  return 32;
1470 }
1471 
1472 static int mov_write_mdia_tag(AVIOContext *pb, MOVTrack *track)
1473 {
1474  int64_t pos = avio_tell(pb);
1475  avio_wb32(pb, 0); /* size */
1476  ffio_wfourcc(pb, "mdia");
1477  mov_write_mdhd_tag(pb, track);
1478  mov_write_hdlr_tag(pb, track);
1479  mov_write_minf_tag(pb, track);
1480  return update_size(pb, pos);
1481 }
1482 
1484  MOVTrack *track, AVStream *st)
1485 {
1487  track->timescale, AV_ROUND_UP);
1488  int version = duration < INT32_MAX ? 0 : 1;
1490  int group = 0;
1491 
1492 
1493  if (st) {
1494  if (mov->per_stream_grouping)
1495  group = st->index;
1496  else
1497  group = st->codec->codec_type;
1498  }
1499 
1500  if (track->flags & MOV_TRACK_ENABLED)
1501  flags |= MOV_TKHD_FLAG_ENABLED;
1502 
1503  if (track->mode == MODE_ISM)
1504  version = 1;
1505 
1506  (version == 1) ? avio_wb32(pb, 104) : avio_wb32(pb, 92); /* size */
1507  ffio_wfourcc(pb, "tkhd");
1508  avio_w8(pb, version);
1509  avio_wb24(pb, flags);
1510  if (version == 1) {
1511  avio_wb64(pb, track->time);
1512  avio_wb64(pb, track->time);
1513  } else {
1514  avio_wb32(pb, track->time); /* creation time */
1515  avio_wb32(pb, track->time); /* modification time */
1516  }
1517  avio_wb32(pb, track->track_id); /* track-id */
1518  avio_wb32(pb, 0); /* reserved */
1519  if (!track->entry)
1520  (version == 1) ? avio_wb64(pb, UINT64_C(0xffffffffffffffff)) : avio_wb32(pb, 0xffffffff);
1521  else
1522  (version == 1) ? avio_wb64(pb, duration) : avio_wb32(pb, duration);
1523 
1524  avio_wb32(pb, 0); /* reserved */
1525  avio_wb32(pb, 0); /* reserved */
1526  avio_wb16(pb, 0); /* layer */
1527  avio_wb16(pb, group); /* alternate group) */
1528  /* Volume, only for audio */
1529  if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
1530  avio_wb16(pb, 0x0100);
1531  else
1532  avio_wb16(pb, 0);
1533  avio_wb16(pb, 0); /* reserved */
1534 
1535  /* Matrix structure */
1536  avio_wb32(pb, 0x00010000); /* reserved */
1537  avio_wb32(pb, 0x0); /* reserved */
1538  avio_wb32(pb, 0x0); /* reserved */
1539  avio_wb32(pb, 0x0); /* reserved */
1540  avio_wb32(pb, 0x00010000); /* reserved */
1541  avio_wb32(pb, 0x0); /* reserved */
1542  avio_wb32(pb, 0x0); /* reserved */
1543  avio_wb32(pb, 0x0); /* reserved */
1544  avio_wb32(pb, 0x40000000); /* reserved */
1545 
1546  /* Track width and height, for visual only */
1547  if (st && (track->enc->codec_type == AVMEDIA_TYPE_VIDEO ||
1548  track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)) {
1549  if (track->mode == MODE_MOV) {
1550  avio_wb32(pb, track->enc->width << 16);
1551  avio_wb32(pb, track->height << 16);
1552  } else {
1553  double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
1554  if (!sample_aspect_ratio || track->height != track->enc->height)
1555  sample_aspect_ratio = 1;
1556  avio_wb32(pb, sample_aspect_ratio * track->enc->width * 0x10000);
1557  avio_wb32(pb, track->height * 0x10000);
1558  }
1559  } else {
1560  avio_wb32(pb, 0);
1561  avio_wb32(pb, 0);
1562  }
1563  return 0x5c;
1564 }
1565 
1566 static int mov_write_tapt_tag(AVIOContext *pb, MOVTrack *track)
1567 {
1569  track->enc->sample_aspect_ratio.den);
1570 
1571  int64_t pos = avio_tell(pb);
1572 
1573  avio_wb32(pb, 0); /* size */
1574  ffio_wfourcc(pb, "tapt");
1575 
1576  avio_wb32(pb, 20);
1577  ffio_wfourcc(pb, "clef");
1578  avio_wb32(pb, 0);
1579  avio_wb32(pb, width << 16);
1580  avio_wb32(pb, track->enc->height << 16);
1581 
1582  avio_wb32(pb, 20);
1583  ffio_wfourcc(pb, "prof");
1584  avio_wb32(pb, 0);
1585  avio_wb32(pb, width << 16);
1586  avio_wb32(pb, track->enc->height << 16);
1587 
1588  avio_wb32(pb, 20);
1589  ffio_wfourcc(pb, "enof");
1590  avio_wb32(pb, 0);
1591  avio_wb32(pb, track->enc->width << 16);
1592  avio_wb32(pb, track->enc->height << 16);
1593 
1594  return update_size(pb, pos);
1595 }
1596 
1597 // This box seems important for the psp playback ... without it the movie seems to hang
1598 static int mov_write_edts_tag(AVIOContext *pb, MOVTrack *track)
1599 {
1601  track->timescale, AV_ROUND_UP);
1602  int version = duration < INT32_MAX ? 0 : 1;
1603  int entry_size, entry_count, size;
1604  int64_t delay, start_ct = track->cluster[0].cts;
1605  delay = av_rescale_rnd(track->cluster[0].dts + start_ct, MOV_TIMESCALE,
1606  track->timescale, AV_ROUND_DOWN);
1607  version |= delay < INT32_MAX ? 0 : 1;
1608 
1609  entry_size = (version == 1) ? 20 : 12;
1610  entry_count = 1 + (delay > 0);
1611  size = 24 + entry_count * entry_size;
1612 
1613  /* write the atom data */
1614  avio_wb32(pb, size);
1615  ffio_wfourcc(pb, "edts");
1616  avio_wb32(pb, size - 8);
1617  ffio_wfourcc(pb, "elst");
1618  avio_w8(pb, version);
1619  avio_wb24(pb, 0); /* flags */
1620 
1621  avio_wb32(pb, entry_count);
1622  if (delay > 0) { /* add an empty edit to delay presentation */
1623  if (version == 1) {
1624  avio_wb64(pb, delay);
1625  avio_wb64(pb, -1);
1626  } else {
1627  avio_wb32(pb, delay);
1628  avio_wb32(pb, -1);
1629  }
1630  avio_wb32(pb, 0x00010000);
1631  }
1632 
1633  /* duration */
1634  if (version == 1) {
1635  avio_wb64(pb, duration);
1636  avio_wb64(pb, start_ct);
1637  } else {
1638  avio_wb32(pb, duration);
1639  avio_wb32(pb, start_ct);
1640  }
1641  avio_wb32(pb, 0x00010000);
1642  return size;
1643 }
1644 
1645 static int mov_write_tref_tag(AVIOContext *pb, MOVTrack *track)
1646 {
1647  avio_wb32(pb, 20); // size
1648  ffio_wfourcc(pb, "tref");
1649  avio_wb32(pb, 12); // size (subatom)
1650  avio_wl32(pb, track->tref_tag);
1651  avio_wb32(pb, track->tref_id);
1652  return 20;
1653 }
1654 
1655 // goes at the end of each track! ... Critical for PSP playback ("Incompatible data" without it)
1657 {
1658  avio_wb32(pb, 0x34); /* size ... reports as 28 in mp4box! */
1659  ffio_wfourcc(pb, "uuid");
1660  ffio_wfourcc(pb, "USMT");
1661  avio_wb32(pb, 0x21d24fce);
1662  avio_wb32(pb, 0xbb88695c);
1663  avio_wb32(pb, 0xfac9c740);
1664  avio_wb32(pb, 0x1c); // another size here!
1665  ffio_wfourcc(pb, "MTDT");
1666  avio_wb32(pb, 0x00010012);
1667  avio_wb32(pb, 0x0a);
1668  avio_wb32(pb, 0x55c40000);
1669  avio_wb32(pb, 0x1);
1670  avio_wb32(pb, 0x0);
1671  return 0x34;
1672 }
1673 
1674 static int mov_write_udta_sdp(AVIOContext *pb, MOVTrack *track)
1675 {
1676  AVFormatContext *ctx = track->rtp_ctx;
1677  char buf[1000] = "";
1678  int len;
1679 
1680  ff_sdp_write_media(buf, sizeof(buf), ctx->streams[0], track->src_track,
1681  NULL, NULL, 0, 0, ctx);
1682  av_strlcatf(buf, sizeof(buf), "a=control:streamid=%d\r\n", track->track_id);
1683  len = strlen(buf);
1684 
1685  avio_wb32(pb, len + 24);
1686  ffio_wfourcc(pb, "udta");
1687  avio_wb32(pb, len + 16);
1688  ffio_wfourcc(pb, "hnti");
1689  avio_wb32(pb, len + 8);
1690  ffio_wfourcc(pb, "sdp ");
1691  avio_write(pb, buf, len);
1692  return len + 24;
1693 }
1694 
1696  const char *tag, const char *str)
1697 {
1698  int64_t pos = avio_tell(pb);
1699  AVDictionaryEntry *t = av_dict_get(st->metadata, str, NULL, 0);
1700  if (!t || !utf8len(t->value))
1701  return 0;
1702 
1703  avio_wb32(pb, 0); /* size */
1704  ffio_wfourcc(pb, tag); /* type */
1705  avio_write(pb, t->value, strlen(t->value)); /* UTF8 string value */
1706  return update_size(pb, pos);
1707 }
1708 
1710  AVStream *st)
1711 {
1712  AVIOContext *pb_buf;
1713  int ret, size;
1714  uint8_t *buf;
1715 
1716  if (!st)
1717  return 0;
1718 
1719  ret = avio_open_dyn_buf(&pb_buf);
1720  if (ret < 0)
1721  return ret;
1722 
1723  if (mov->mode & MODE_MP4)
1724  mov_write_track_metadata(pb_buf, st, "name", "title");
1725 
1726  if ((size = avio_close_dyn_buf(pb_buf, &buf)) > 0) {
1727  avio_wb32(pb, size + 8);
1728  ffio_wfourcc(pb, "udta");
1729  avio_write(pb, buf, size);
1730  }
1731  av_free(buf);
1732 
1733  return 0;
1734 }
1735 
1737  MOVTrack *track, AVStream *st)
1738 {
1739  int64_t pos = avio_tell(pb);
1740  avio_wb32(pb, 0); /* size */
1741  ffio_wfourcc(pb, "trak");
1742  mov_write_tkhd_tag(pb, mov, track, st);
1743  if (track->mode == MODE_PSP || track->flags & MOV_TRACK_CTTS ||
1744  (track->entry && track->cluster[0].dts) ||
1745  is_clcp_track(track)) {
1746  if (!(mov->flags & FF_MOV_FLAG_FRAGMENT))
1747  mov_write_edts_tag(pb, track); // PSP Movies require edts box
1748  }
1749  if (track->tref_tag)
1750  mov_write_tref_tag(pb, track);
1751  mov_write_mdia_tag(pb, track);
1752  if (track->mode == MODE_PSP)
1753  mov_write_uuid_tag_psp(pb, track); // PSP Movies require this uuid box
1754  if (track->tag == MKTAG('r','t','p',' '))
1755  mov_write_udta_sdp(pb, track);
1756  if (track->mode == MODE_MOV) {
1757  if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
1758  double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
1759  if ((0.0 != sample_aspect_ratio && 1.0 != sample_aspect_ratio)) {
1760  mov_write_tapt_tag(pb, track);
1761  }
1762  }
1763  if (is_clcp_track(track)) {
1764  mov_write_tapt_tag(pb, track);
1765  }
1766  }
1767  mov_write_track_udta_tag(pb, mov, st);
1768  return update_size(pb, pos);
1769 }
1770 
1772 {
1773  int i, has_audio = 0, has_video = 0;
1774  int64_t pos = avio_tell(pb);
1775  int audio_profile = mov->iods_audio_profile;
1776  int video_profile = mov->iods_video_profile;
1777  for (i = 0; i < mov->nb_streams; i++) {
1778  if (mov->tracks[i].entry > 0) {
1779  has_audio |= mov->tracks[i].enc->codec_type == AVMEDIA_TYPE_AUDIO;
1780  has_video |= mov->tracks[i].enc->codec_type == AVMEDIA_TYPE_VIDEO;
1781  }
1782  }
1783  if (audio_profile < 0)
1784  audio_profile = 0xFF - has_audio;
1785  if (video_profile < 0)
1786  video_profile = 0xFF - has_video;
1787  avio_wb32(pb, 0x0); /* size */
1788  ffio_wfourcc(pb, "iods");
1789  avio_wb32(pb, 0); /* version & flags */
1790  put_descr(pb, 0x10, 7);
1791  avio_wb16(pb, 0x004f);
1792  avio_w8(pb, 0xff);
1793  avio_w8(pb, 0xff);
1794  avio_w8(pb, audio_profile);
1795  avio_w8(pb, video_profile);
1796  avio_w8(pb, 0xff);
1797  return update_size(pb, pos);
1798 }
1799 
1800 static int mov_write_trex_tag(AVIOContext *pb, MOVTrack *track)
1801 {
1802  avio_wb32(pb, 0x20); /* size */
1803  ffio_wfourcc(pb, "trex");
1804  avio_wb32(pb, 0); /* version & flags */
1805  avio_wb32(pb, track->track_id); /* track ID */
1806  avio_wb32(pb, 1); /* default sample description index */
1807  avio_wb32(pb, 0); /* default sample duration */
1808  avio_wb32(pb, 0); /* default sample size */
1809  avio_wb32(pb, 0); /* default sample flags */
1810  return 0;
1811 }
1812 
1814 {
1815  int64_t pos = avio_tell(pb);
1816  int i;
1817  avio_wb32(pb, 0x0); /* size */
1818  ffio_wfourcc(pb, "mvex");
1819  for (i = 0; i < mov->nb_streams; i++)
1820  mov_write_trex_tag(pb, &mov->tracks[i]);
1821  return update_size(pb, pos);
1822 }
1823 
1825 {
1826  int max_track_id = 1, i;
1827  int64_t max_track_len_temp, max_track_len = 0;
1828  int version;
1829 
1830  for (i = 0; i < mov->nb_streams; i++) {
1831  if (mov->tracks[i].entry > 0 && mov->tracks[i].timescale) {
1832  max_track_len_temp = av_rescale_rnd(mov->tracks[i].track_duration,
1833  MOV_TIMESCALE,
1834  mov->tracks[i].timescale,
1835  AV_ROUND_UP);
1836  if (max_track_len < max_track_len_temp)
1837  max_track_len = max_track_len_temp;
1838  if (max_track_id < mov->tracks[i].track_id)
1839  max_track_id = mov->tracks[i].track_id;
1840  }
1841  }
1842 
1843  version = max_track_len < UINT32_MAX ? 0 : 1;
1844  (version == 1) ? avio_wb32(pb, 120) : avio_wb32(pb, 108); /* size */
1845  ffio_wfourcc(pb, "mvhd");
1846  avio_w8(pb, version);
1847  avio_wb24(pb, 0); /* flags */
1848  if (version == 1) {
1849  avio_wb64(pb, mov->time);
1850  avio_wb64(pb, mov->time);
1851  } else {
1852  avio_wb32(pb, mov->time); /* creation time */
1853  avio_wb32(pb, mov->time); /* modification time */
1854  }
1855  avio_wb32(pb, MOV_TIMESCALE);
1856  (version == 1) ? avio_wb64(pb, max_track_len) : avio_wb32(pb, max_track_len); /* duration of longest track */
1857 
1858  avio_wb32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
1859  avio_wb16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
1860  avio_wb16(pb, 0); /* reserved */
1861  avio_wb32(pb, 0); /* reserved */
1862  avio_wb32(pb, 0); /* reserved */
1863 
1864  /* Matrix structure */
1865  avio_wb32(pb, 0x00010000); /* reserved */
1866  avio_wb32(pb, 0x0); /* reserved */
1867  avio_wb32(pb, 0x0); /* reserved */
1868  avio_wb32(pb, 0x0); /* reserved */
1869  avio_wb32(pb, 0x00010000); /* reserved */
1870  avio_wb32(pb, 0x0); /* reserved */
1871  avio_wb32(pb, 0x0); /* reserved */
1872  avio_wb32(pb, 0x0); /* reserved */
1873  avio_wb32(pb, 0x40000000); /* reserved */
1874 
1875  avio_wb32(pb, 0); /* reserved (preview time) */
1876  avio_wb32(pb, 0); /* reserved (preview duration) */
1877  avio_wb32(pb, 0); /* reserved (poster time) */
1878  avio_wb32(pb, 0); /* reserved (selection time) */
1879  avio_wb32(pb, 0); /* reserved (selection duration) */
1880  avio_wb32(pb, 0); /* reserved (current time) */
1881  avio_wb32(pb, max_track_id + 1); /* Next track id */
1882  return 0x6c;
1883 }
1884 
1886  AVFormatContext *s)
1887 {
1888  avio_wb32(pb, 33); /* size */
1889  ffio_wfourcc(pb, "hdlr");
1890  avio_wb32(pb, 0);
1891  avio_wb32(pb, 0);
1892  ffio_wfourcc(pb, "mdir");
1893  ffio_wfourcc(pb, "appl");
1894  avio_wb32(pb, 0);
1895  avio_wb32(pb, 0);
1896  avio_w8(pb, 0);
1897  return 33;
1898 }
1899 
1900 /* helper function to write a data tag with the specified string as data */
1901 static int mov_write_string_data_tag(AVIOContext *pb, const char *data, int lang, int long_style)
1902 {
1903  if (long_style) {
1904  int size = 16 + strlen(data);
1905  avio_wb32(pb, size); /* size */
1906  ffio_wfourcc(pb, "data");
1907  avio_wb32(pb, 1);
1908  avio_wb32(pb, 0);
1909  avio_write(pb, data, strlen(data));
1910  return size;
1911  } else {
1912  if (!lang)
1913  lang = ff_mov_iso639_to_lang("und", 1);
1914  avio_wb16(pb, strlen(data)); /* string length */
1915  avio_wb16(pb, lang);
1916  avio_write(pb, data, strlen(data));
1917  return strlen(data) + 4;
1918  }
1919 }
1920 
1921 static int mov_write_string_tag(AVIOContext *pb, const char *name,
1922  const char *value, int lang, int long_style)
1923 {
1924  int size = 0;
1925  if (value && value[0]) {
1926  int64_t pos = avio_tell(pb);
1927  avio_wb32(pb, 0); /* size */
1928  ffio_wfourcc(pb, name);
1929  mov_write_string_data_tag(pb, value, lang, long_style);
1930  size = update_size(pb, pos);
1931  }
1932  return size;
1933 }
1934 
1936  const char *name, const char *tag,
1937  int long_style)
1938 {
1939  int l, lang = 0, len, len2;
1940  AVDictionaryEntry *t, *t2 = NULL;
1941  char tag2[16];
1942 
1943  if (!(t = av_dict_get(s->metadata, tag, NULL, 0)))
1944  return 0;
1945 
1946  len = strlen(t->key);
1947  snprintf(tag2, sizeof(tag2), "%s-", tag);
1948  while ((t2 = av_dict_get(s->metadata, tag2, t2, AV_DICT_IGNORE_SUFFIX))) {
1949  len2 = strlen(t2->key);
1950  if (len2 == len + 4 && !strcmp(t->value, t2->value)
1951  && (l = ff_mov_iso639_to_lang(&t2->key[len2 - 3], 1)) >= 0) {
1952  lang = l;
1953  break;
1954  }
1955  }
1956  return mov_write_string_tag(pb, name, t->value, lang, long_style);
1957 }
1958 
1959 /* iTunes track number */
1961  AVFormatContext *s)
1962 {
1963  AVDictionaryEntry *t = av_dict_get(s->metadata, "track", NULL, 0);
1964  int size = 0, track = t ? atoi(t->value) : 0;
1965  if (track) {
1966  avio_wb32(pb, 32); /* size */
1967  ffio_wfourcc(pb, "trkn");
1968  avio_wb32(pb, 24); /* size */
1969  ffio_wfourcc(pb, "data");
1970  avio_wb32(pb, 0); // 8 bytes empty
1971  avio_wb32(pb, 0);
1972  avio_wb16(pb, 0); // empty
1973  avio_wb16(pb, track); // track number
1974  avio_wb16(pb, 0); // total track number
1975  avio_wb16(pb, 0); // empty
1976  size = 32;
1977  }
1978  return size;
1979 }
1980 
1981 /* iTunes meta data list */
1983  AVFormatContext *s)
1984 {
1985  int64_t pos = avio_tell(pb);
1986  avio_wb32(pb, 0); /* size */
1987  ffio_wfourcc(pb, "ilst");
1988  mov_write_string_metadata(s, pb, "\251nam", "title" , 1);
1989  mov_write_string_metadata(s, pb, "\251ART", "artist" , 1);
1990  mov_write_string_metadata(s, pb, "aART", "album_artist", 1);
1991  mov_write_string_metadata(s, pb, "\251wrt", "composer" , 1);
1992  mov_write_string_metadata(s, pb, "\251alb", "album" , 1);
1993  mov_write_string_metadata(s, pb, "\251day", "date" , 1);
1994  if (!mov_write_string_metadata(s, pb, "\251too", "encoding_tool", 1)) {
1995  if (!(s->flags & AVFMT_FLAG_BITEXACT))
1996  mov_write_string_tag(pb, "\251too", LIBAVFORMAT_IDENT, 0, 1);
1997  }
1998  mov_write_string_metadata(s, pb, "\251cmt", "comment" , 1);
1999  mov_write_string_metadata(s, pb, "\251gen", "genre" , 1);
2000  mov_write_string_metadata(s, pb, "\251cpy", "copyright", 1);
2001  mov_write_string_metadata(s, pb, "\251grp", "grouping" , 1);
2002  mov_write_string_metadata(s, pb, "\251lyr", "lyrics" , 1);
2003  mov_write_string_metadata(s, pb, "desc", "description",1);
2004  mov_write_string_metadata(s, pb, "ldes", "synopsis" , 1);
2005  mov_write_string_metadata(s, pb, "tvsh", "show" , 1);
2006  mov_write_string_metadata(s, pb, "tven", "episode_id",1);
2007  mov_write_string_metadata(s, pb, "tvnn", "network" , 1);
2008  mov_write_trkn_tag(pb, mov, s);
2009  return update_size(pb, pos);
2010 }
2011 
2012 /* iTunes meta data tag */
2014  AVFormatContext *s)
2015 {
2016  int size = 0;
2017  int64_t pos = avio_tell(pb);
2018  avio_wb32(pb, 0); /* size */
2019  ffio_wfourcc(pb, "meta");
2020  avio_wb32(pb, 0);
2021  mov_write_itunes_hdlr_tag(pb, mov, s);
2022  mov_write_ilst_tag(pb, mov, s);
2023  size = update_size(pb, pos);
2024  return size;
2025 }
2026 
2027 static int ascii_to_wc(AVIOContext *pb, const uint8_t *b)
2028 {
2029  int val;
2030  while (*b) {
2031  GET_UTF8(val, *b++, return -1;)
2032  avio_wb16(pb, val);
2033  }
2034  avio_wb16(pb, 0x00);
2035  return 0;
2036 }
2037 
2038 static uint16_t language_code(const char *str)
2039 {
2040  return (((str[0] - 0x60) & 0x1F) << 10) +
2041  (((str[1] - 0x60) & 0x1F) << 5) +
2042  (( str[2] - 0x60) & 0x1F);
2043 }
2044 
2046  const char *tag, const char *str)
2047 {
2048  int64_t pos = avio_tell(pb);
2049  AVDictionaryEntry *t = av_dict_get(s->metadata, str, NULL, 0);
2050  if (!t || !utf8len(t->value))
2051  return 0;
2052  avio_wb32(pb, 0); /* size */
2053  ffio_wfourcc(pb, tag); /* type */
2054  avio_wb32(pb, 0); /* version + flags */
2055  if (!strcmp(tag, "yrrc"))
2056  avio_wb16(pb, atoi(t->value));
2057  else {
2058  avio_wb16(pb, language_code("eng")); /* language */
2059  avio_write(pb, t->value, strlen(t->value) + 1); /* UTF8 string value */
2060  if (!strcmp(tag, "albm") &&
2061  (t = av_dict_get(s->metadata, "track", NULL, 0)))
2062  avio_w8(pb, atoi(t->value));
2063  }
2064  return update_size(pb, pos);
2065 }
2066 
2068 {
2069  int64_t pos = avio_tell(pb);
2070  int i, nb_chapters = FFMIN(s->nb_chapters, 255);
2071 
2072  avio_wb32(pb, 0); // size
2073  ffio_wfourcc(pb, "chpl");
2074  avio_wb32(pb, 0x01000000); // version + flags
2075  avio_wb32(pb, 0); // unknown
2076  avio_w8(pb, nb_chapters);
2077 
2078  for (i = 0; i < nb_chapters; i++) {
2079  AVChapter *c = s->chapters[i];
2080  AVDictionaryEntry *t;
2081  avio_wb64(pb, av_rescale_q(c->start, c->time_base, (AVRational){1,10000000}));
2082 
2083  if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
2084  int len = FFMIN(strlen(t->value), 255);
2085  avio_w8(pb, len);
2086  avio_write(pb, t->value, len);
2087  } else
2088  avio_w8(pb, 0);
2089  }
2090  return update_size(pb, pos);
2091 }
2092 
2094  AVFormatContext *s)
2095 {
2096  AVIOContext *pb_buf;
2097  int ret, size;
2098  uint8_t *buf;
2099 
2100  ret = avio_open_dyn_buf(&pb_buf);
2101  if (ret < 0)
2102  return ret;
2103 
2104  if (mov->mode & MODE_3GP) {
2105  mov_write_3gp_udta_tag(pb_buf, s, "perf", "artist");
2106  mov_write_3gp_udta_tag(pb_buf, s, "titl", "title");
2107  mov_write_3gp_udta_tag(pb_buf, s, "auth", "author");
2108  mov_write_3gp_udta_tag(pb_buf, s, "gnre", "genre");
2109  mov_write_3gp_udta_tag(pb_buf, s, "dscp", "comment");
2110  mov_write_3gp_udta_tag(pb_buf, s, "albm", "album");
2111  mov_write_3gp_udta_tag(pb_buf, s, "cprt", "copyright");
2112  mov_write_3gp_udta_tag(pb_buf, s, "yrrc", "date");
2113  } else if (mov->mode == MODE_MOV) { // the title field breaks gtkpod with mp4 and my suspicion is that stuff is not valid in mp4
2114  mov_write_string_metadata(s, pb_buf, "\251ART", "artist", 0);
2115  mov_write_string_metadata(s, pb_buf, "\251nam", "title", 0);
2116  mov_write_string_metadata(s, pb_buf, "\251aut", "author", 0);
2117  mov_write_string_metadata(s, pb_buf, "\251alb", "album", 0);
2118  mov_write_string_metadata(s, pb_buf, "\251day", "date", 0);
2119  if (!(s->flags & AVFMT_FLAG_BITEXACT))
2120  mov_write_string_metadata(s, pb_buf, "\251swr", "encoder", 0);
2121  mov_write_string_metadata(s, pb_buf, "\251des", "comment", 0);
2122  mov_write_string_metadata(s, pb_buf, "\251gen", "genre", 0);
2123  mov_write_string_metadata(s, pb_buf, "\251cpy", "copyright", 0);
2124  } else {
2125  /* iTunes meta data */
2126  mov_write_meta_tag(pb_buf, mov, s);
2127  }
2128 
2129  if (s->nb_chapters && !(mov->flags & FF_MOV_FLAG_DISABLE_CHPL))
2130  mov_write_chpl_tag(pb_buf, s);
2131 
2132  if ((size = avio_close_dyn_buf(pb_buf, &buf)) > 0) {
2133  avio_wb32(pb, size + 8);
2134  ffio_wfourcc(pb, "udta");
2135  avio_write(pb, buf, size);
2136  }
2137  av_free(buf);
2138 
2139  return 0;
2140 }
2141 
2143  const char *str, const char *lang, int type)
2144 {
2145  int len = utf8len(str) + 1;
2146  if (len <= 0)
2147  return;
2148  avio_wb16(pb, len * 2 + 10); /* size */
2149  avio_wb32(pb, type); /* type */
2150  avio_wb16(pb, language_code(lang)); /* language */
2151  avio_wb16(pb, 0x01); /* ? */
2152  ascii_to_wc(pb, str);
2153 }
2154 
2156 {
2157  AVDictionaryEntry *title = av_dict_get(s->metadata, "title", NULL, 0);
2158  int64_t pos, pos2;
2159 
2160  if (title) {
2161  pos = avio_tell(pb);
2162  avio_wb32(pb, 0); /* size placeholder*/
2163  ffio_wfourcc(pb, "uuid");
2164  ffio_wfourcc(pb, "USMT");
2165  avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
2166  avio_wb32(pb, 0xbb88695c);
2167  avio_wb32(pb, 0xfac9c740);
2168 
2169  pos2 = avio_tell(pb);
2170  avio_wb32(pb, 0); /* size placeholder*/
2171  ffio_wfourcc(pb, "MTDT");
2172  avio_wb16(pb, 4);
2173 
2174  // ?
2175  avio_wb16(pb, 0x0C); /* size */
2176  avio_wb32(pb, 0x0B); /* type */
2177  avio_wb16(pb, language_code("und")); /* language */
2178  avio_wb16(pb, 0x0); /* ? */
2179  avio_wb16(pb, 0x021C); /* data */
2180 
2181  mov_write_psp_udta_tag(pb, LIBAVCODEC_IDENT, "eng", 0x04);
2182  mov_write_psp_udta_tag(pb, title->value, "eng", 0x01);
2183  mov_write_psp_udta_tag(pb, "2006/04/01 11:11:11", "und", 0x03);
2184 
2185  update_size(pb, pos2);
2186  return update_size(pb, pos);
2187  }
2188 
2189  return 0;
2190 }
2191 
2193  AVFormatContext *s)
2194 {
2195  int i;
2196  int64_t pos = avio_tell(pb);
2197  avio_wb32(pb, 0); /* size placeholder*/
2198  ffio_wfourcc(pb, "moov");
2199 
2200  for (i = 0; i < mov->nb_streams; i++) {
2201  if (mov->tracks[i].entry <= 0 && !(mov->flags & FF_MOV_FLAG_FRAGMENT))
2202  continue;
2203 
2204  mov->tracks[i].time = mov->time;
2205  mov->tracks[i].track_id = i + 1;
2206  }
2207 
2208  if (mov->chapter_track)
2209  for (i = 0; i < s->nb_streams; i++) {
2210  mov->tracks[i].tref_tag = MKTAG('c','h','a','p');
2211  mov->tracks[i].tref_id = mov->tracks[mov->chapter_track].track_id;
2212  }
2213  for (i = 0; i < mov->nb_streams; i++) {
2214  if (mov->tracks[i].tag == MKTAG('r','t','p',' ')) {
2215  mov->tracks[i].tref_tag = MKTAG('h','i','n','t');
2216  mov->tracks[i].tref_id =
2217  mov->tracks[mov->tracks[i].src_track].track_id;
2218  }
2219  }
2220 
2221  mov_write_mvhd_tag(pb, mov);
2222  if (mov->mode != MODE_MOV && !mov->iods_skip)
2223  mov_write_iods_tag(pb, mov);
2224  for (i = 0; i < mov->nb_streams; i++) {
2225  if (mov->tracks[i].entry > 0 || mov->flags & FF_MOV_FLAG_FRAGMENT) {
2226  mov_write_trak_tag(pb, mov, &(mov->tracks[i]), i < s->nb_streams ? s->streams[i] : NULL);
2227  }
2228  }
2229  if (mov->flags & FF_MOV_FLAG_FRAGMENT)
2230  mov_write_mvex_tag(pb, mov); /* QuickTime requires trak to precede this */
2231 
2232  if (mov->mode == MODE_PSP)
2233  mov_write_uuidusmt_tag(pb, s);
2234  else
2235  mov_write_udta_tag(pb, mov, s);
2236 
2237  return update_size(pb, pos);
2238 }
2239 
2240 static void param_write_int(AVIOContext *pb, const char *name, int value)
2241 {
2242  avio_printf(pb, "<param name=\"%s\" value=\"%d\" valuetype=\"data\"/>\n", name, value);
2243 }
2244 
2245 static void param_write_string(AVIOContext *pb, const char *name, const char *value)
2246 {
2247  avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, value);
2248 }
2249 
2250 static void param_write_hex(AVIOContext *pb, const char *name, const uint8_t *value, int len)
2251 {
2252  char buf[150];
2253  len = FFMIN(sizeof(buf) / 2 - 1, len);
2254  ff_data_to_hex(buf, value, len, 0);
2255  buf[2 * len] = '\0';
2256  avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, buf);
2257 }
2258 
2260 {
2261  int64_t pos = avio_tell(pb);
2262  int i;
2263  const uint8_t uuid[] = {
2264  0xa5, 0xd4, 0x0b, 0x30, 0xe8, 0x14, 0x11, 0xdd,
2265  0xba, 0x2f, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66
2266  };
2267 
2268  avio_wb32(pb, 0);
2269  ffio_wfourcc(pb, "uuid");
2270  avio_write(pb, uuid, sizeof(uuid));
2271  avio_wb32(pb, 0);
2272 
2273  avio_printf(pb, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
2274  avio_printf(pb, "<smil xmlns=\"http://www.w3.org/2001/SMIL20/Language\">\n");
2275  avio_printf(pb, "<head>\n");
2276  avio_printf(pb, "<meta name=\"creator\" content=\"%s\" />\n",
2278  avio_printf(pb, "</head>\n");
2279  avio_printf(pb, "<body>\n");
2280  avio_printf(pb, "<switch>\n");
2281  for (i = 0; i < mov->nb_streams; i++) {
2282  MOVTrack *track = &mov->tracks[i];
2283  const char *type;
2284  /* track->track_id is initialized in write_moov, and thus isn't known
2285  * here yet */
2286  int track_id = i + 1;
2287 
2288  if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
2289  type = "video";
2290  } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
2291  type = "audio";
2292  } else {
2293  continue;
2294  }
2295  avio_printf(pb, "<%s systemBitrate=\"%d\">\n", type,
2296  track->enc->bit_rate);
2297  param_write_int(pb, "systemBitrate", track->enc->bit_rate);
2298  param_write_int(pb, "trackID", track_id);
2299  if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
2300  if (track->enc->codec_id == AV_CODEC_ID_H264) {
2301  uint8_t *ptr;
2302  int size = track->enc->extradata_size;
2303  if (!ff_avc_write_annexb_extradata(track->enc->extradata, &ptr,
2304  &size)) {
2305  param_write_hex(pb, "CodecPrivateData",
2306  ptr ? ptr : track->enc->extradata,
2307  size);
2308  av_free(ptr);
2309  }
2310  param_write_string(pb, "FourCC", "H264");
2311  } else if (track->enc->codec_id == AV_CODEC_ID_VC1) {
2312  param_write_string(pb, "FourCC", "WVC1");
2313  param_write_hex(pb, "CodecPrivateData", track->enc->extradata,
2314  track->enc->extradata_size);
2315  }
2316  param_write_int(pb, "MaxWidth", track->enc->width);
2317  param_write_int(pb, "MaxHeight", track->enc->height);
2318  param_write_int(pb, "DisplayWidth", track->enc->width);
2319  param_write_int(pb, "DisplayHeight", track->enc->height);
2320  } else {
2321  if (track->enc->codec_id == AV_CODEC_ID_AAC) {
2322  param_write_string(pb, "FourCC", "AACL");
2323  } else if (track->enc->codec_id == AV_CODEC_ID_WMAPRO) {
2324  param_write_string(pb, "FourCC", "WMAP");
2325  }
2326  param_write_hex(pb, "CodecPrivateData", track->enc->extradata,
2327  track->enc->extradata_size);
2329  track->enc->codec_id));
2330  param_write_int(pb, "Channels", track->enc->channels);
2331  param_write_int(pb, "SamplingRate", track->enc->sample_rate);
2332  param_write_int(pb, "BitsPerSample", 16);
2333  param_write_int(pb, "PacketSize", track->enc->block_align ?
2334  track->enc->block_align : 4);
2335  }
2336  avio_printf(pb, "</%s>\n", type);
2337  }
2338  avio_printf(pb, "</switch>\n");
2339  avio_printf(pb, "</body>\n");
2340  avio_printf(pb, "</smil>\n");
2341 
2342  return update_size(pb, pos);
2343 }
2344 
2346 {
2347  avio_wb32(pb, 16);
2348  ffio_wfourcc(pb, "mfhd");
2349  avio_wb32(pb, 0);
2350  avio_wb32(pb, mov->fragments);
2351  return 0;
2352 }
2353 
2355  MOVTrack *track, int64_t moof_offset)
2356 {
2357  int64_t pos = avio_tell(pb);
2360  if (!track->entry) {
2361  flags |= MOV_TFHD_DURATION_IS_EMPTY;
2362  } else {
2363  flags |= MOV_TFHD_DEFAULT_FLAGS;
2364  }
2366  flags &= ~MOV_TFHD_BASE_DATA_OFFSET;
2367 
2368  /* Don't set a default sample size, the silverlight player refuses
2369  * to play files with that set. Don't set a default sample duration,
2370  * WMP freaks out if it is set. Don't set a base data offset, PIFF
2371  * file format says it MUST NOT be set. */
2372  if (track->mode == MODE_ISM)
2375 
2376  avio_wb32(pb, 0); /* size placeholder */
2377  ffio_wfourcc(pb, "tfhd");
2378  avio_w8(pb, 0); /* version */
2379  avio_wb24(pb, flags);
2380 
2381  avio_wb32(pb, track->track_id); /* track-id */
2382  if (flags & MOV_TFHD_BASE_DATA_OFFSET)
2383  avio_wb64(pb, moof_offset);
2384  if (flags & MOV_TFHD_DEFAULT_DURATION) {
2385  track->default_duration = get_cluster_duration(track, 0);
2386  avio_wb32(pb, track->default_duration);
2387  }
2388  if (flags & MOV_TFHD_DEFAULT_SIZE) {
2389  track->default_size = track->entry ? track->cluster[0].size : 1;
2390  avio_wb32(pb, track->default_size);
2391  } else
2392  track->default_size = -1;
2393 
2394  if (flags & MOV_TFHD_DEFAULT_FLAGS) {
2395  track->default_sample_flags =
2396  track->enc->codec_type == AVMEDIA_TYPE_VIDEO ?
2399  avio_wb32(pb, track->default_sample_flags);
2400  }
2401 
2402  return update_size(pb, pos);
2403 }
2404 
2405 static uint32_t get_sample_flags(MOVTrack *track, MOVIentry *entry)
2406 {
2409 }
2410 
2412  MOVTrack *track, int moof_size)
2413 {
2414  int64_t pos = avio_tell(pb);
2415  uint32_t flags = MOV_TRUN_DATA_OFFSET;
2416  int i;
2417 
2418  for (i = 0; i < track->entry; i++) {
2419  if (get_cluster_duration(track, i) != track->default_duration)
2420  flags |= MOV_TRUN_SAMPLE_DURATION;
2421  if (track->cluster[i].size != track->default_size)
2422  flags |= MOV_TRUN_SAMPLE_SIZE;
2423  if (i > 0 && get_sample_flags(track, &track->cluster[i]) != track->default_sample_flags)
2424  flags |= MOV_TRUN_SAMPLE_FLAGS;
2425  }
2426  if (!(flags & MOV_TRUN_SAMPLE_FLAGS))
2427  flags |= MOV_TRUN_FIRST_SAMPLE_FLAGS;
2428  if (track->flags & MOV_TRACK_CTTS)
2429  flags |= MOV_TRUN_SAMPLE_CTS;
2430 
2431  avio_wb32(pb, 0); /* size placeholder */
2432  ffio_wfourcc(pb, "trun");
2433  avio_w8(pb, 0); /* version */
2434  avio_wb24(pb, flags);
2435 
2436  avio_wb32(pb, track->entry); /* sample count */
2437  if (mov->flags & FF_MOV_FLAG_OMIT_TFHD_OFFSET &&
2438  !(mov->flags & FF_MOV_FLAG_SEPARATE_MOOF) &&
2439  track->track_id != 1)
2440  avio_wb32(pb, 0); /* Later tracks follow immediately after the previous one */
2441  else
2442  avio_wb32(pb, moof_size + 8 + track->data_offset +
2443  track->cluster[0].pos); /* data offset */
2444  if (flags & MOV_TRUN_FIRST_SAMPLE_FLAGS)
2445  avio_wb32(pb, get_sample_flags(track, &track->cluster[0]));
2446 
2447  for (i = 0; i < track->entry; i++) {
2448  if (flags & MOV_TRUN_SAMPLE_DURATION)
2449  avio_wb32(pb, get_cluster_duration(track, i));
2450  if (flags & MOV_TRUN_SAMPLE_SIZE)
2451  avio_wb32(pb, track->cluster[i].size);
2452  if (flags & MOV_TRUN_SAMPLE_FLAGS)
2453  avio_wb32(pb, get_sample_flags(track, &track->cluster[i]));
2454  if (flags & MOV_TRUN_SAMPLE_CTS)
2455  avio_wb32(pb, track->cluster[i].cts);
2456  }
2457 
2458  return update_size(pb, pos);
2459 }
2460 
2461 static int mov_write_tfxd_tag(AVIOContext *pb, MOVTrack *track)
2462 {
2463  int64_t pos = avio_tell(pb);
2464  const uint8_t uuid[] = {
2465  0x6d, 0x1d, 0x9b, 0x05, 0x42, 0xd5, 0x44, 0xe6,
2466  0x80, 0xe2, 0x14, 0x1d, 0xaf, 0xf7, 0x57, 0xb2
2467  };
2468 
2469  avio_wb32(pb, 0); /* size placeholder */
2470  ffio_wfourcc(pb, "uuid");
2471  avio_write(pb, uuid, sizeof(uuid));
2472  avio_w8(pb, 1);
2473  avio_wb24(pb, 0);
2474  avio_wb64(pb, track->frag_start);
2475  avio_wb64(pb, track->start_dts + track->track_duration -
2476  track->cluster[0].dts);
2477 
2478  return update_size(pb, pos);
2479 }
2480 
2482  MOVTrack *track, int entry)
2483 {
2484  int n = track->nb_frag_info - 1 - entry, i;
2485  int size = 8 + 16 + 4 + 1 + 16*n;
2486  const uint8_t uuid[] = {
2487  0xd4, 0x80, 0x7e, 0xf2, 0xca, 0x39, 0x46, 0x95,
2488  0x8e, 0x54, 0x26, 0xcb, 0x9e, 0x46, 0xa7, 0x9f
2489  };
2490 
2491  if (entry < 0)
2492  return 0;
2493 
2494  avio_seek(pb, track->frag_info[entry].tfrf_offset, SEEK_SET);
2495  avio_wb32(pb, size);
2496  ffio_wfourcc(pb, "uuid");
2497  avio_write(pb, uuid, sizeof(uuid));
2498  avio_w8(pb, 1);
2499  avio_wb24(pb, 0);
2500  avio_w8(pb, n);
2501  for (i = 0; i < n; i++) {
2502  int index = entry + 1 + i;
2503  avio_wb64(pb, track->frag_info[index].time);
2504  avio_wb64(pb, track->frag_info[index].duration);
2505  }
2506  if (n < mov->ism_lookahead) {
2507  int free_size = 16 * (mov->ism_lookahead - n);
2508  avio_wb32(pb, free_size);
2509  ffio_wfourcc(pb, "free");
2510  for (i = 0; i < free_size - 8; i++)
2511  avio_w8(pb, 0);
2512  }
2513 
2514  return 0;
2515 }
2516 
2518  MOVTrack *track)
2519 {
2520  int64_t pos = avio_tell(pb);
2521  int i;
2522  for (i = 0; i < mov->ism_lookahead; i++) {
2523  /* Update the tfrf tag for the last ism_lookahead fragments,
2524  * nb_frag_info - 1 is the next fragment to be written. */
2525  mov_write_tfrf_tag(pb, mov, track, track->nb_frag_info - 2 - i);
2526  }
2527  avio_seek(pb, pos, SEEK_SET);
2528  return 0;
2529 }
2530 
2532  MOVTrack *track, int64_t moof_offset,
2533  int moof_size)
2534 {
2535  int64_t pos = avio_tell(pb);
2536  avio_wb32(pb, 0); /* size placeholder */
2537  ffio_wfourcc(pb, "traf");
2538 
2539  mov_write_tfhd_tag(pb, mov, track, moof_offset);
2540  mov_write_trun_tag(pb, mov, track, moof_size);
2541  if (mov->mode == MODE_ISM) {
2542  mov_write_tfxd_tag(pb, track);
2543 
2544  if (mov->ism_lookahead) {
2545  int i, size = 16 + 4 + 1 + 16 * mov->ism_lookahead;
2546 
2547  track->tfrf_offset = avio_tell(pb);
2548  avio_wb32(pb, 8 + size);
2549  ffio_wfourcc(pb, "free");
2550  for (i = 0; i < size; i++)
2551  avio_w8(pb, 0);
2552  }
2553  }
2554 
2555  return update_size(pb, pos);
2556 }
2557 
2559  int tracks, int moof_size)
2560 {
2561  int64_t pos = avio_tell(pb);
2562  int i;
2563 
2564  avio_wb32(pb, 0); /* size placeholder */
2565  ffio_wfourcc(pb, "moof");
2566 
2567  mov_write_mfhd_tag(pb, mov);
2568  for (i = 0; i < mov->nb_streams; i++) {
2569  MOVTrack *track = &mov->tracks[i];
2570  if (tracks >= 0 && i != tracks)
2571  continue;
2572  if (!track->entry)
2573  continue;
2574  mov_write_traf_tag(pb, mov, track, pos, moof_size);
2575  }
2576 
2577  return update_size(pb, pos);
2578 }
2579 
2580 static int mov_write_moof_tag(AVIOContext *pb, MOVMuxContext *mov, int tracks)
2581 {
2582  AVIOContext *avio_buf;
2583  int ret, moof_size;
2584 
2585  if ((ret = ffio_open_null_buf(&avio_buf)) < 0)
2586  return ret;
2587  mov_write_moof_tag_internal(avio_buf, mov, tracks, 0);
2588  moof_size = ffio_close_null_buf(avio_buf);
2589  return mov_write_moof_tag_internal(pb, mov, tracks, moof_size);
2590 }
2591 
2592 static int mov_write_tfra_tag(AVIOContext *pb, MOVTrack *track)
2593 {
2594  int64_t pos = avio_tell(pb);
2595  int i;
2596 
2597  avio_wb32(pb, 0); /* size placeholder */
2598  ffio_wfourcc(pb, "tfra");
2599  avio_w8(pb, 1); /* version */
2600  avio_wb24(pb, 0);
2601 
2602  avio_wb32(pb, track->track_id);
2603  avio_wb32(pb, 0); /* length of traf/trun/sample num */
2604  avio_wb32(pb, track->nb_frag_info);
2605  for (i = 0; i < track->nb_frag_info; i++) {
2606  avio_wb64(pb, track->frag_info[i].time);
2607  avio_wb64(pb, track->frag_info[i].offset);
2608  avio_w8(pb, 1); /* traf number */
2609  avio_w8(pb, 1); /* trun number */
2610  avio_w8(pb, 1); /* sample number */
2611  }
2612 
2613  return update_size(pb, pos);
2614 }
2615 
2617 {
2618  int64_t pos = avio_tell(pb);
2619  int i;
2620 
2621  avio_wb32(pb, 0); /* size placeholder */
2622  ffio_wfourcc(pb, "mfra");
2623  /* An empty mfra atom is enough to indicate to the publishing point that
2624  * the stream has ended. */
2625  if (mov->flags & FF_MOV_FLAG_ISML)
2626  return update_size(pb, pos);
2627 
2628  for (i = 0; i < mov->nb_streams; i++) {
2629  MOVTrack *track = &mov->tracks[i];
2630  if (track->nb_frag_info)
2631  mov_write_tfra_tag(pb, track);
2632  }
2633 
2634  avio_wb32(pb, 16);
2635  ffio_wfourcc(pb, "mfro");
2636  avio_wb32(pb, 0); /* version + flags */
2637  avio_wb32(pb, avio_tell(pb) + 4 - pos);
2638 
2639  return update_size(pb, pos);
2640 }
2641 
2643 {
2644  avio_wb32(pb, 8); // placeholder for extended size field (64 bit)
2645  ffio_wfourcc(pb, mov->mode == MODE_MOV ? "wide" : "free");
2646 
2647  mov->mdat_pos = avio_tell(pb);
2648  avio_wb32(pb, 0); /* size placeholder*/
2649  ffio_wfourcc(pb, "mdat");
2650  return 0;
2651 }
2652 
2653 /* TODO: This needs to be more general */
2655 {
2656  MOVMuxContext *mov = s->priv_data;
2657  int64_t pos = avio_tell(pb);
2658  int has_h264 = 0, has_video = 0;
2659  int minor = 0x200;
2660  int i;
2661 
2662  for (i = 0; i < s->nb_streams; i++) {
2663  AVStream *st = s->streams[i];
2664  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
2665  has_video = 1;
2666  if (st->codec->codec_id == AV_CODEC_ID_H264)
2667  has_h264 = 1;
2668  }
2669 
2670  avio_wb32(pb, 0); /* size */
2671  ffio_wfourcc(pb, "ftyp");
2672 
2673  if (mov->major_brand && strlen(mov->major_brand) >= 4)
2674  ffio_wfourcc(pb, mov->major_brand);
2675  else if (mov->mode == MODE_3GP) {
2676  ffio_wfourcc(pb, has_h264 ? "3gp6" : "3gp4");
2677  minor = has_h264 ? 0x100 : 0x200;
2678  } else if (mov->mode & MODE_3G2) {
2679  ffio_wfourcc(pb, has_h264 ? "3g2b" : "3g2a");
2680  minor = has_h264 ? 0x20000 : 0x10000;
2681  } else if (mov->mode == MODE_PSP)
2682  ffio_wfourcc(pb, "MSNV");
2683  else if (mov->mode == MODE_MP4)
2684  ffio_wfourcc(pb, "isom");
2685  else if (mov->mode == MODE_IPOD)
2686  ffio_wfourcc(pb, has_video ? "M4V ":"M4A ");
2687  else if (mov->mode == MODE_ISM)
2688  ffio_wfourcc(pb, "isml");
2689  else if (mov->mode == MODE_F4V)
2690  ffio_wfourcc(pb, "f4v ");
2691  else
2692  ffio_wfourcc(pb, "qt ");
2693 
2694  avio_wb32(pb, minor);
2695 
2696  if (mov->mode == MODE_MOV)
2697  ffio_wfourcc(pb, "qt ");
2698  else if (mov->mode == MODE_ISM) {
2699  ffio_wfourcc(pb, "piff");
2700  ffio_wfourcc(pb, "iso2");
2701  } else {
2702  ffio_wfourcc(pb, "isom");
2703  ffio_wfourcc(pb, "iso2");
2704  if (has_h264)
2705  ffio_wfourcc(pb, "avc1");
2706  }
2707 
2708  if (mov->mode == MODE_3GP)
2709  ffio_wfourcc(pb, has_h264 ? "3gp6":"3gp4");
2710  else if (mov->mode & MODE_3G2)
2711  ffio_wfourcc(pb, has_h264 ? "3g2b":"3g2a");
2712  else if (mov->mode == MODE_PSP)
2713  ffio_wfourcc(pb, "MSNV");
2714  else if (mov->mode == MODE_MP4)
2715  ffio_wfourcc(pb, "mp41");
2716  return update_size(pb, pos);
2717 }
2718 
2720 {
2721  AVStream *video_st = s->streams[0];
2722  AVCodecContext *video_codec = s->streams[0]->codec;
2723  AVCodecContext *audio_codec = s->streams[1]->codec;
2724  int audio_rate = audio_codec->sample_rate;
2725  // TODO: should be avg_frame_rate
2726  int frame_rate = ((video_st->time_base.den) * (0x10000)) / (video_st->time_base.num);
2727  int audio_kbitrate = audio_codec->bit_rate / 1000;
2728  int video_kbitrate = FFMIN(video_codec->bit_rate / 1000, 800 - audio_kbitrate);
2729 
2730  avio_wb32(pb, 0x94); /* size */
2731  ffio_wfourcc(pb, "uuid");
2732  ffio_wfourcc(pb, "PROF");
2733 
2734  avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
2735  avio_wb32(pb, 0xbb88695c);
2736  avio_wb32(pb, 0xfac9c740);
2737 
2738  avio_wb32(pb, 0x0); /* ? */
2739  avio_wb32(pb, 0x3); /* 3 sections ? */
2740 
2741  avio_wb32(pb, 0x14); /* size */
2742  ffio_wfourcc(pb, "FPRF");
2743  avio_wb32(pb, 0x0); /* ? */
2744  avio_wb32(pb, 0x0); /* ? */
2745  avio_wb32(pb, 0x0); /* ? */
2746 
2747  avio_wb32(pb, 0x2c); /* size */
2748  ffio_wfourcc(pb, "APRF"); /* audio */
2749  avio_wb32(pb, 0x0);
2750  avio_wb32(pb, 0x2); /* TrackID */
2751  ffio_wfourcc(pb, "mp4a");
2752  avio_wb32(pb, 0x20f);
2753  avio_wb32(pb, 0x0);
2754  avio_wb32(pb, audio_kbitrate);
2755  avio_wb32(pb, audio_kbitrate);
2756  avio_wb32(pb, audio_rate);
2757  avio_wb32(pb, audio_codec->channels);
2758 
2759  avio_wb32(pb, 0x34); /* size */
2760  ffio_wfourcc(pb, "VPRF"); /* video */
2761  avio_wb32(pb, 0x0);
2762  avio_wb32(pb, 0x1); /* TrackID */
2763  if (video_codec->codec_id == AV_CODEC_ID_H264) {
2764  ffio_wfourcc(pb, "avc1");
2765  avio_wb16(pb, 0x014D);
2766  avio_wb16(pb, 0x0015);
2767  } else {
2768  ffio_wfourcc(pb, "mp4v");
2769  avio_wb16(pb, 0x0000);
2770  avio_wb16(pb, 0x0103);
2771  }
2772  avio_wb32(pb, 0x0);
2773  avio_wb32(pb, video_kbitrate);
2774  avio_wb32(pb, video_kbitrate);
2775  avio_wb32(pb, frame_rate);
2776  avio_wb32(pb, frame_rate);
2777  avio_wb16(pb, video_codec->width);
2778  avio_wb16(pb, video_codec->height);
2779  avio_wb32(pb, 0x010001); /* ? */
2780 }
2781 
2782 static int mov_parse_mpeg2_frame(AVPacket *pkt, uint32_t *flags)
2783 {
2784  uint32_t c = -1;
2785  int i, closed_gop = 0;
2786 
2787  for (i = 0; i < pkt->size - 4; i++) {
2788  c = (c << 8) + pkt->data[i];
2789  if (c == 0x1b8) { // gop
2790  closed_gop = pkt->data[i + 4] >> 6 & 0x01;
2791  } else if (c == 0x100) { // pic
2792  int temp_ref = (pkt->data[i + 1] << 2) | (pkt->data[i + 2] >> 6);
2793  if (!temp_ref || closed_gop) // I picture is not reordered
2794  *flags = MOV_SYNC_SAMPLE;
2795  else
2796  *flags = MOV_PARTIAL_SYNC_SAMPLE;
2797  break;
2798  }
2799  }
2800  return 0;
2801 }
2802 
2803 static void mov_parse_vc1_frame(AVPacket *pkt, MOVTrack *trk, int fragment)
2804 {
2805  const uint8_t *start, *next, *end = pkt->data + pkt->size;
2806  int seq = 0, entry = 0;
2807  int key = pkt->flags & AV_PKT_FLAG_KEY;
2808  start = find_next_marker(pkt->data, end);
2809  for (next = start; next < end; start = next) {
2810  next = find_next_marker(start + 4, end);
2811  switch (AV_RB32(start)) {
2812  case VC1_CODE_SEQHDR:
2813  seq = 1;
2814  break;
2815  case VC1_CODE_ENTRYPOINT:
2816  entry = 1;
2817  break;
2818  case VC1_CODE_SLICE:
2819  trk->vc1_info.slices = 1;
2820  break;
2821  }
2822  }
2823  if (!trk->entry && !fragment) {
2824  /* First packet in first fragment */
2825  trk->vc1_info.first_packet_seq = seq;
2826  trk->vc1_info.first_packet_entry = entry;
2827  } else if ((seq && !trk->vc1_info.packet_seq) ||
2828  (entry && !trk->vc1_info.packet_entry)) {
2829  int i;
2830  for (i = 0; i < trk->entry; i++)
2831  trk->cluster[i].flags &= ~MOV_SYNC_SAMPLE;
2832  trk->has_keyframes = 0;
2833  if (seq)
2834  trk->vc1_info.packet_seq = 1;
2835  if (entry)
2836  trk->vc1_info.packet_entry = 1;
2837  if (!fragment) {
2838  /* First fragment */
2839  if ((!seq || trk->vc1_info.first_packet_seq) &&
2840  (!entry || trk->vc1_info.first_packet_entry)) {
2841  /* First packet had the same headers as this one, readd the
2842  * sync sample flag. */
2843  trk->cluster[0].flags |= MOV_SYNC_SAMPLE;
2844  trk->has_keyframes = 1;
2845  }
2846  }
2847  }
2848  if (trk->vc1_info.packet_seq && trk->vc1_info.packet_entry)
2849  key = seq && entry;
2850  else if (trk->vc1_info.packet_seq)
2851  key = seq;
2852  else if (trk->vc1_info.packet_entry)
2853  key = entry;
2854  if (key) {
2855  trk->cluster[trk->entry].flags |= MOV_SYNC_SAMPLE;
2856  trk->has_keyframes++;
2857  }
2858 }
2859 
2861 {
2862  MOVMuxContext *mov = s->priv_data;
2863  int i, first_track = -1;
2864  int64_t mdat_size = 0;
2865 
2866  if (!(mov->flags & FF_MOV_FLAG_FRAGMENT))
2867  return 0;
2868 
2869  if (!(mov->flags & FF_MOV_FLAG_EMPTY_MOOV) && mov->fragments == 0) {
2870  int64_t pos = avio_tell(s->pb);
2871  int ret;
2872  AVIOContext *moov_buf;
2873  uint8_t *buf;
2874  int buf_size;
2875 
2876  for (i = 0; i < mov->nb_streams; i++)
2877  if (!mov->tracks[i].entry)
2878  break;
2879  /* Don't write the initial moov unless all tracks have data */
2880  if (i < mov->nb_streams)
2881  return 0;
2882 
2883  if ((ret = ffio_open_null_buf(&moov_buf)) < 0)
2884  return ret;
2885  mov_write_moov_tag(moov_buf, mov, s);
2886  buf_size = ffio_close_null_buf(moov_buf);
2887  for (i = 0; i < mov->nb_streams; i++)
2888  mov->tracks[i].data_offset = pos + buf_size + 8;
2889 
2890  mov_write_moov_tag(s->pb, mov, s);
2891 
2892  buf_size = avio_close_dyn_buf(mov->mdat_buf, &buf);
2893  mov->mdat_buf = NULL;
2894  avio_wb32(s->pb, buf_size + 8);
2895  ffio_wfourcc(s->pb, "mdat");
2896  avio_write(s->pb, buf, buf_size);
2897  av_free(buf);
2898 
2899  mov->fragments++;
2900  mov->mdat_size = 0;
2901  for (i = 0; i < mov->nb_streams; i++) {
2902  if (mov->tracks[i].entry)
2903  mov->tracks[i].frag_start += mov->tracks[i].start_dts +
2904  mov->tracks[i].track_duration -
2905  mov->tracks[i].cluster[0].dts;
2906  mov->tracks[i].entry = 0;
2907  }
2908  avio_flush(s->pb);
2909  return 0;
2910  }
2911 
2912  for (i = 0; i < mov->nb_streams; i++) {
2913  MOVTrack *track = &mov->tracks[i];
2914  if (mov->flags & FF_MOV_FLAG_SEPARATE_MOOF)
2915  track->data_offset = 0;
2916  else
2917  track->data_offset = mdat_size;
2918  if (!track->mdat_buf)
2919  continue;
2920  mdat_size += avio_tell(track->mdat_buf);
2921  if (first_track < 0)
2922  first_track = i;
2923  }
2924 
2925  if (!mdat_size)
2926  return 0;
2927 
2928  for (i = 0; i < mov->nb_streams; i++) {
2929  MOVTrack *track = &mov->tracks[i];
2930  int buf_size, write_moof = 1, moof_tracks = -1;
2931  uint8_t *buf;
2932  int64_t duration = 0;
2933 
2934  if (track->entry)
2935  duration = track->start_dts + track->track_duration -
2936  track->cluster[0].dts;
2937  if (mov->flags & FF_MOV_FLAG_SEPARATE_MOOF) {
2938  if (!track->mdat_buf)
2939  continue;
2940  mdat_size = avio_tell(track->mdat_buf);
2941  moof_tracks = i;
2942  } else {
2943  write_moof = i == first_track;
2944  }
2945 
2946  if (write_moof) {
2947  MOVFragmentInfo *info;
2948  avio_flush(s->pb);
2949  track->nb_frag_info++;
2950  if (track->nb_frag_info >= track->frag_info_capacity) {
2951  unsigned new_capacity = track->nb_frag_info + MOV_FRAG_INFO_ALLOC_INCREMENT;
2952  if (av_reallocp_array(&track->frag_info,
2953  new_capacity,
2954  sizeof(*track->frag_info)))
2955  return AVERROR(ENOMEM);
2956  track->frag_info_capacity = new_capacity;
2957  }
2958  info = &track->frag_info[track->nb_frag_info - 1];
2959  info->offset = avio_tell(s->pb);
2960  info->time = mov->tracks[i].frag_start;
2961  info->duration = duration;
2962  mov_write_tfrf_tags(s->pb, mov, track);
2963 
2964  mov_write_moof_tag(s->pb, mov, moof_tracks);
2965  info->tfrf_offset = track->tfrf_offset;
2966  mov->fragments++;
2967 
2968  avio_wb32(s->pb, mdat_size + 8);
2969  ffio_wfourcc(s->pb, "mdat");
2970  }
2971 
2972  if (track->entry)
2973  track->frag_start += duration;
2974  track->entry = 0;
2975  if (!track->mdat_buf)
2976  continue;
2977  buf_size = avio_close_dyn_buf(track->mdat_buf, &buf);
2978  track->mdat_buf = NULL;
2979 
2980  avio_write(s->pb, buf, buf_size);
2981  av_free(buf);
2982  }
2983 
2984  mov->mdat_size = 0;
2985 
2986  avio_flush(s->pb);
2987  return 0;
2988 }
2989 
2991 {
2992  MOVMuxContext *mov = s->priv_data;
2993  AVIOContext *pb = s->pb;
2994  MOVTrack *trk = &mov->tracks[pkt->stream_index];
2995  AVCodecContext *enc = trk->enc;
2996  unsigned int samples_in_chunk = 0;
2997  int size = pkt->size;
2998  uint8_t *reformatted_data = NULL;
2999 
3000  if (mov->flags & FF_MOV_FLAG_FRAGMENT) {
3001  int ret;
3002  if (mov->fragments > 0) {
3003  if (!trk->mdat_buf) {
3004  if ((ret = avio_open_dyn_buf(&trk->mdat_buf)) < 0)
3005  return ret;
3006  }
3007  pb = trk->mdat_buf;
3008  } else {
3009  if (!mov->mdat_buf) {
3010  if ((ret = avio_open_dyn_buf(&mov->mdat_buf)) < 0)
3011  return ret;
3012  }
3013  pb = mov->mdat_buf;
3014  }
3015  }
3016 
3017  if (enc->codec_id == AV_CODEC_ID_AMR_NB) {
3018  /* We must find out how many AMR blocks there are in one packet */
3019  static uint16_t packed_size[16] =
3020  {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 1};
3021  int len = 0;
3022 
3023  while (len < size && samples_in_chunk < 100) {
3024  len += packed_size[(pkt->data[len] >> 3) & 0x0F];
3025  samples_in_chunk++;
3026  }
3027  if (samples_in_chunk > 1) {
3028  av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, implement a AVParser for it\n");
3029  return -1;
3030  }
3031  } else if (trk->sample_size)
3032  samples_in_chunk = size / trk->sample_size;
3033  else
3034  samples_in_chunk = 1;
3035 
3036  if (samples_in_chunk < 1) {
3037  av_log(s, AV_LOG_ERROR, "fatal error, input packet contains no samples\n");
3038  return AVERROR_PATCHWELCOME;
3039  }
3040 
3041  /* copy extradata if it exists */
3042  if (trk->vos_len == 0 && enc->extradata_size > 0) {
3043  trk->vos_len = enc->extradata_size;
3044  trk->vos_data = av_malloc(trk->vos_len);
3045  memcpy(trk->vos_data, enc->extradata, trk->vos_len);
3046  }
3047 
3048  if (enc->codec_id == AV_CODEC_ID_H264 && trk->vos_len > 0 && *(uint8_t *)trk->vos_data != 1) {
3049  /* from x264 or from bytestream h264 */
3050  /* nal reformating needed */
3051  if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams) {
3052  ff_avc_parse_nal_units_buf(pkt->data, &reformatted_data,
3053  &size);
3054  avio_write(pb, reformatted_data, size);
3055  } else {
3056  size = ff_avc_parse_nal_units(pb, pkt->data, pkt->size);
3057  }
3058  } else if (enc->codec_id == AV_CODEC_ID_HEVC && trk->vos_len > 6 &&
3059  (AV_RB24(trk->vos_data) == 1 || AV_RB32(trk->vos_data) == 1)) {
3060  /* extradata is Annex B, assume the bitstream is too and convert it */
3061  if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams) {
3062  ff_hevc_annexb2mp4_buf(pkt->data, &reformatted_data, &size, 0, NULL);
3063  avio_write(pb, reformatted_data, size);
3064  } else {
3065  size = ff_hevc_annexb2mp4(pb, pkt->data, pkt->size, 0, NULL);
3066  }
3067  } else {
3068  avio_write(pb, pkt->data, size);
3069  }
3070 
3071  if ((enc->codec_id == AV_CODEC_ID_DNXHD ||
3072  enc->codec_id == AV_CODEC_ID_AC3) && !trk->vos_len) {
3073  /* copy frame to create needed atoms */
3074  trk->vos_len = size;
3075  trk->vos_data = av_malloc(size);
3076  if (!trk->vos_data)
3077  return AVERROR(ENOMEM);
3078  memcpy(trk->vos_data, pkt->data, size);
3079  }
3080 
3081  if (trk->entry >= trk->cluster_capacity) {
3082  unsigned new_capacity = 2 * (trk->entry + MOV_INDEX_CLUSTER_SIZE);
3083  if (av_reallocp_array(&trk->cluster, new_capacity,
3084  sizeof(*trk->cluster)))
3085  return AVERROR(ENOMEM);
3086  trk->cluster_capacity = new_capacity;
3087  }
3088 
3089  trk->cluster[trk->entry].pos = avio_tell(pb) - size;
3090  trk->cluster[trk->entry].samples_in_chunk = samples_in_chunk;
3091  trk->cluster[trk->entry].size = size;
3092  trk->cluster[trk->entry].entries = samples_in_chunk;
3093  trk->cluster[trk->entry].dts = pkt->dts;
3094  if (!trk->entry && trk->start_dts != AV_NOPTS_VALUE) {
3095  /* First packet of a new fragment. We already wrote the duration
3096  * of the last packet of the previous fragment based on track_duration,
3097  * which might not exactly match our dts. Therefore adjust the dts
3098  * of this packet to be what the previous packets duration implies. */
3099  trk->cluster[trk->entry].dts = trk->start_dts + trk->track_duration;
3100  }
3101  if (trk->start_dts == AV_NOPTS_VALUE)
3102  trk->start_dts = pkt->dts;
3103  trk->track_duration = pkt->dts - trk->start_dts + pkt->duration;
3104 
3105  if (pkt->pts == AV_NOPTS_VALUE) {
3106  av_log(s, AV_LOG_WARNING, "pts has no value\n");
3107  pkt->pts = pkt->dts;
3108  }
3109  if (pkt->dts != pkt->pts)
3110  trk->flags |= MOV_TRACK_CTTS;
3111  trk->cluster[trk->entry].cts = pkt->pts - pkt->dts;
3112  trk->cluster[trk->entry].flags = 0;
3113  if (enc->codec_id == AV_CODEC_ID_VC1) {
3114  mov_parse_vc1_frame(pkt, trk, mov->fragments);
3115  } else if (pkt->flags & AV_PKT_FLAG_KEY) {
3116  if (mov->mode == MODE_MOV && enc->codec_id == AV_CODEC_ID_MPEG2VIDEO &&
3117  trk->entry > 0) { // force sync sample for the first key frame
3118  mov_parse_mpeg2_frame(pkt, &trk->cluster[trk->entry].flags);
3119  if (trk->cluster[trk->entry].flags & MOV_PARTIAL_SYNC_SAMPLE)
3120  trk->flags |= MOV_TRACK_STPS;
3121  } else {
3122  trk->cluster[trk->entry].flags = MOV_SYNC_SAMPLE;
3123  }
3124  if (trk->cluster[trk->entry].flags & MOV_SYNC_SAMPLE)
3125  trk->has_keyframes++;
3126  }
3127  trk->entry++;
3128  trk->sample_count += samples_in_chunk;
3129  mov->mdat_size += size;
3130 
3131  if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams)
3132  ff_mov_add_hinted_packet(s, pkt, trk->hint_track, trk->entry,
3133  reformatted_data, size);
3134  av_free(reformatted_data);
3135  return 0;
3136 }
3137 
3139 {
3140  if (!pkt) {
3141  mov_flush_fragment(s);
3142  return 1;
3143  } else {
3144  MOVMuxContext *mov = s->priv_data;
3145  MOVTrack *trk = &mov->tracks[pkt->stream_index];
3146  AVCodecContext *enc = trk->enc;
3147  int64_t frag_duration = 0;
3148  int size = pkt->size;
3149 
3150  if (!pkt->size)
3151  return 0; /* Discard 0 sized packets */
3152 
3153  if (trk->entry)
3154  frag_duration = av_rescale_q(pkt->dts - trk->cluster[0].dts,
3155  s->streams[pkt->stream_index]->time_base,
3156  AV_TIME_BASE_Q);
3157  if ((mov->max_fragment_duration &&
3158  frag_duration >= mov->max_fragment_duration) ||
3159  (mov->max_fragment_size && mov->mdat_size + size >= mov->max_fragment_size) ||
3160  (mov->flags & FF_MOV_FLAG_FRAG_KEYFRAME &&
3161  enc->codec_type == AVMEDIA_TYPE_VIDEO &&
3162  trk->entry && pkt->flags & AV_PKT_FLAG_KEY)) {
3163  if (frag_duration >= mov->min_fragment_duration)
3164  mov_flush_fragment(s);
3165  }
3166 
3167  return ff_mov_write_packet(s, pkt);
3168  }
3169 }
3170 
3171 // QuickTime chapters involve an additional text track with the chapter names
3172 // as samples, and a tref pointing from the other tracks to the chapter one.
3173 static int mov_create_chapter_track(AVFormatContext *s, int tracknum)
3174 {
3175  MOVMuxContext *mov = s->priv_data;
3176  MOVTrack *track = &mov->tracks[tracknum];
3177  AVPacket pkt = { .stream_index = tracknum, .flags = AV_PKT_FLAG_KEY };
3178  int i, len;
3179  // These properties are required to make QT recognize the chapter track
3180  uint8_t chapter_properties[43] = { 0, 0, 0, 0, 0, 0, 0, 1, };
3181 
3182  track->mode = mov->mode;
3183  track->tag = MKTAG('t','e','x','t');
3184  track->timescale = MOV_TIMESCALE;
3185  track->enc = avcodec_alloc_context3(NULL);
3186  if (!track->enc)
3187  return AVERROR(ENOMEM);
3189  track->enc->extradata = av_malloc(sizeof(chapter_properties));
3190  if (!track->enc->extradata)
3191  return AVERROR(ENOMEM);
3192  track->enc->extradata_size = sizeof(chapter_properties);
3193  memcpy(track->enc->extradata, chapter_properties, sizeof(chapter_properties));
3194 
3195  for (i = 0; i < s->nb_chapters; i++) {
3196  AVChapter *c = s->chapters[i];
3197  AVDictionaryEntry *t;
3198 
3199  int64_t end = av_rescale_q(c->end, c->time_base, (AVRational){1,MOV_TIMESCALE});
3200  pkt.pts = pkt.dts = av_rescale_q(c->start, c->time_base, (AVRational){1,MOV_TIMESCALE});
3201  pkt.duration = end - pkt.dts;
3202 
3203  if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
3204  const char encd[12] = {
3205  0x00, 0x00, 0x00, 0x0C,
3206  'e', 'n', 'c', 'd',
3207  0x00, 0x00, 0x01, 0x00 };
3208  len = strlen(t->value);
3209  pkt.size = len + 2 + 12;
3210  pkt.data = av_malloc(pkt.size);
3211  if (!pkt.data)
3212  return AVERROR(ENOMEM);
3213  AV_WB16(pkt.data, len);
3214  memcpy(pkt.data + 2, t->value, len);
3215  memcpy(pkt.data + len + 2, encd, sizeof(encd));
3216  ff_mov_write_packet(s, &pkt);
3217  av_freep(&pkt.data);
3218  }
3219  }
3220 
3221  return 0;
3222 }
3223 
3224 /*
3225  * st->disposition controls the "enabled" flag in the tkhd tag.
3226  * QuickTime will not play a track if it is not enabled. So make sure
3227  * that one track of each type (audio, video, subtitle) is enabled.
3228  *
3229  * Subtitles are special. For audio and video, setting "enabled" also
3230  * makes the track "default" (i.e. it is rendered when played). For
3231  * subtitles, an "enabled" subtitle is not rendered by default, but
3232  * if no subtitle is enabled, the subtitle menu in QuickTime will be
3233  * empty!
3234  */
3236 {
3237  MOVMuxContext *mov = s->priv_data;
3238  int i;
3239  int enabled[AVMEDIA_TYPE_NB];
3240  int first[AVMEDIA_TYPE_NB];
3241 
3242  for (i = 0; i < AVMEDIA_TYPE_NB; i++) {
3243  enabled[i] = 0;
3244  first[i] = -1;
3245  }
3246 
3247  for (i = 0; i < s->nb_streams; i++) {
3248  AVStream *st = s->streams[i];
3249 
3250  if (st->codec->codec_type <= AVMEDIA_TYPE_UNKNOWN ||
3251  st->codec->codec_type >= AVMEDIA_TYPE_NB)
3252  continue;
3253 
3254  if (first[st->codec->codec_type] < 0)
3255  first[st->codec->codec_type] = i;
3256  if (st->disposition & AV_DISPOSITION_DEFAULT) {
3257  mov->tracks[i].flags |= MOV_TRACK_ENABLED;
3258  enabled[st->codec->codec_type]++;
3259  }
3260  }
3261 
3262  for (i = 0; i < AVMEDIA_TYPE_NB; i++) {
3263  switch (i) {
3264  case AVMEDIA_TYPE_VIDEO:
3265  case AVMEDIA_TYPE_AUDIO:
3266  case AVMEDIA_TYPE_SUBTITLE:
3267  if (enabled[i] > 1)
3268  mov->per_stream_grouping = 1;
3269  if (!enabled[i] && first[i] >= 0)
3270  mov->tracks[first[i]].flags |= MOV_TRACK_ENABLED;
3271  break;
3272  }
3273  }
3274 }
3275 
3276 static void mov_free(AVFormatContext *s)
3277 {
3278  MOVMuxContext *mov = s->priv_data;
3279  int i;
3280 
3281  if (mov->chapter_track) {
3282  if (mov->tracks[mov->chapter_track].enc)
3283  av_free(mov->tracks[mov->chapter_track].enc->extradata);
3284  av_freep(&mov->tracks[mov->chapter_track].enc);
3285  }
3286 
3287  for (i = 0; i < mov->nb_streams; i++) {
3288  if (mov->tracks[i].tag == MKTAG('r','t','p',' '))
3289  ff_mov_close_hinting(&mov->tracks[i]);
3290  av_freep(&mov->tracks[i].cluster);
3291  av_freep(&mov->tracks[i].frag_info);
3292 
3293  if (mov->tracks[i].vos_len)
3294  av_free(mov->tracks[i].vos_data);
3295  }
3296 
3297  av_freep(&mov->tracks);
3298 }
3299 
3300 static uint32_t rgb_to_yuv(uint32_t rgb)
3301 {
3302  uint8_t r, g, b;
3303  int y, cb, cr;
3304 
3305  r = (rgb >> 16) & 0xFF;
3306  g = (rgb >> 8) & 0xFF;
3307  b = (rgb ) & 0xFF;
3308 
3309  y = av_clip_uint8( 16. + 0.257 * r + 0.504 * g + 0.098 * b);
3310  cb = av_clip_uint8(128. - 0.148 * r - 0.291 * g + 0.439 * b);
3311  cr = av_clip_uint8(128. + 0.439 * r - 0.368 * g - 0.071 * b);
3312 
3313  return (y << 16) | (cr << 8) | cb;
3314 }
3315 
3317  AVStream *st)
3318 {
3319  int i, width = 720, height = 480;
3320  int have_palette = 0, have_size = 0;
3321  uint32_t palette[16];
3322  char *cur = st->codec->extradata;
3323 
3324  while (cur && *cur) {
3325  if (strncmp("palette:", cur, 8) == 0) {
3326  int i, count;
3327  count = sscanf(cur + 8,
3328  "%06"PRIx32", %06"PRIx32", %06"PRIx32", %06"PRIx32", "
3329  "%06"PRIx32", %06"PRIx32", %06"PRIx32", %06"PRIx32", "
3330  "%06"PRIx32", %06"PRIx32", %06"PRIx32", %06"PRIx32", "
3331  "%06"PRIx32", %06"PRIx32", %06"PRIx32", %06"PRIx32"",
3332  &palette[ 0], &palette[ 1], &palette[ 2], &palette[ 3],
3333  &palette[ 4], &palette[ 5], &palette[ 6], &palette[ 7],
3334  &palette[ 8], &palette[ 9], &palette[10], &palette[11],
3335  &palette[12], &palette[13], &palette[14], &palette[15]);
3336 
3337  for (i = 0; i < count; i++) {
3338  palette[i] = rgb_to_yuv(palette[i]);
3339  }
3340  have_palette = 1;
3341  } else if (!strncmp("size:", cur, 5)) {
3342  sscanf(cur + 5, "%dx%d", &width, &height);
3343  have_size = 1;
3344  }
3345  if (have_palette && have_size)
3346  break;
3347  cur += strcspn(cur, "\n\r");
3348  cur += strspn(cur, "\n\r");
3349  }
3350  if (have_palette) {
3351  track->vos_data = av_malloc(16*4);
3352  if (!track->vos_data)
3353  return AVERROR(ENOMEM);
3354  for (i = 0; i < 16; i++) {
3355  AV_WB32(track->vos_data + i * 4, palette[i]);
3356  }
3357  track->vos_len = 16 * 4;
3358  }
3359  st->codec->width = width;
3360  st->codec->height = track->height = height;
3361 
3362  return 0;
3363 }
3364 
3366 {
3367  AVIOContext *pb = s->pb;
3368  MOVMuxContext *mov = s->priv_data;
3369  AVDictionaryEntry *t;
3370  int i, hint_track = 0;
3371 
3372  mov->fc = s;
3373 
3374  /* Default mode == MP4 */
3375  mov->mode = MODE_MP4;
3376 
3377  if (s->oformat) {
3378  if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
3379  else if (!strcmp("3g2", s->oformat->name)) mov->mode = MODE_3GP|MODE_3G2;
3380  else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
3381  else if (!strcmp("psp", s->oformat->name)) mov->mode = MODE_PSP;
3382  else if (!strcmp("ipod",s->oformat->name)) mov->mode = MODE_IPOD;
3383  else if (!strcmp("ismv",s->oformat->name)) mov->mode = MODE_ISM;
3384  else if (!strcmp("f4v", s->oformat->name)) mov->mode = MODE_F4V;
3385  }
3386 
3387  /* Set the FRAGMENT flag if any of the fragmentation methods are
3388  * enabled. */
3389  if (mov->max_fragment_duration || mov->max_fragment_size ||
3390  mov->flags & (FF_MOV_FLAG_EMPTY_MOOV |
3393  mov->flags |= FF_MOV_FLAG_FRAGMENT;
3394 
3395  /* Set other implicit flags immediately */
3396  if (mov->mode == MODE_ISM)
3399 
3400  /* faststart: moov at the beginning of the file, if supported */
3401  if (mov->flags & FF_MOV_FLAG_FASTSTART) {
3402  if ((mov->flags & FF_MOV_FLAG_FRAGMENT) ||
3403  (s->flags & AVFMT_FLAG_CUSTOM_IO)) {
3404  av_log(s, AV_LOG_WARNING, "The faststart flag is incompatible "
3405  "with fragmentation and custom IO, disabling faststart\n");
3406  mov->flags &= ~FF_MOV_FLAG_FASTSTART;
3407  }
3408  }
3409 
3410  /* Non-seekable output is ok if using fragmentation. If ism_lookahead
3411  * is enabled, we don't support non-seekable output at all. */
3412  if (!s->pb->seekable &&
3413  (!(mov->flags & FF_MOV_FLAG_FRAGMENT) || mov->ism_lookahead)) {
3414  av_log(s, AV_LOG_ERROR, "muxer does not support non seekable output\n");
3415  return AVERROR(EINVAL);
3416  }
3417 
3418 
3419  mov_write_ftyp_tag(pb,s);
3420  if (mov->mode == MODE_PSP) {
3421  if (s->nb_streams != 2) {
3422  av_log(s, AV_LOG_ERROR, "PSP mode need one video and one audio stream\n");
3423  return AVERROR(EINVAL);
3424  }
3425  mov_write_uuidprof_tag(pb, s);
3426  }
3427 
3428  mov->nb_streams = s->nb_streams;
3429  if (mov->mode & (MODE_MP4|MODE_MOV|MODE_IPOD) && s->nb_chapters)
3430  mov->chapter_track = mov->nb_streams++;
3431 
3432  if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
3433  /* Add hint tracks for each audio and video stream */
3434  hint_track = mov->nb_streams;
3435  for (i = 0; i < s->nb_streams; i++) {
3436  AVStream *st = s->streams[i];
3437  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
3439  mov->nb_streams++;
3440  }
3441  }
3442  }
3443 
3444  // Reserve an extra stream for chapters for the case where chapters
3445  // are written in the trailer
3446  mov->tracks = av_mallocz((mov->nb_streams + 1) * sizeof(*mov->tracks));
3447  if (!mov->tracks)
3448  return AVERROR(ENOMEM);
3449 
3450  for (i = 0; i < s->nb_streams; i++) {
3451  AVStream *st= s->streams[i];
3452  MOVTrack *track= &mov->tracks[i];
3453  AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL,0);
3454 
3455  track->st = st;
3456  track->enc = st->codec;
3457  track->language = ff_mov_iso639_to_lang(lang?lang->value:"und", mov->mode!=MODE_MOV);
3458  if (track->language < 0)
3459  track->language = 0;
3460  track->mode = mov->mode;
3461  track->tag = mov_find_codec_tag(s, track);
3462  if (!track->tag) {
3463  av_log(s, AV_LOG_ERROR, "track %d: could not find tag, "
3464  "codec not currently supported in container\n", i);
3465  goto error;
3466  }
3467  /* If hinting of this track is enabled by a later hint track,
3468  * this is updated. */
3469  track->hint_track = -1;
3470  track->start_dts = AV_NOPTS_VALUE;
3471  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
3472  if (track->tag == MKTAG('m','x','3','p') || track->tag == MKTAG('m','x','3','n') ||
3473  track->tag == MKTAG('m','x','4','p') || track->tag == MKTAG('m','x','4','n') ||
3474  track->tag == MKTAG('m','x','5','p') || track->tag == MKTAG('m','x','5','n')) {
3475  if (st->codec->width != 720 || (st->codec->height != 608 && st->codec->height != 512)) {
3476  av_log(s, AV_LOG_ERROR, "D-10/IMX must use 720x608 or 720x512 video resolution\n");
3477  goto error;
3478  }
3479  track->height = track->tag >> 24 == 'n' ? 486 : 576;
3480  }
3481  track->timescale = st->time_base.den;
3482  if (track->mode == MODE_MOV && track->timescale > 100000)
3484  "WARNING codec timebase is very high. If duration is too long,\n"
3485  "file may not be playable by quicktime. Specify a shorter timebase\n"
3486  "or choose different container.\n");
3487  } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
3488  track->timescale = st->codec->sample_rate;
3489  /* set sample_size for PCM and ADPCM */
3491  st->codec->codec_id == AV_CODEC_ID_ILBC) {
3492  if (!st->codec->block_align) {
3493  av_log(s, AV_LOG_ERROR, "track %d: codec block align is not set\n", i);
3494  goto error;
3495  }
3496  track->sample_size = st->codec->block_align;
3497  }
3498  /* set audio_vbr for compressed audio */
3499  if (av_get_bits_per_sample(st->codec->codec_id) < 8) {
3500  track->audio_vbr = 1;
3501  }
3502  if (track->mode != MODE_MOV &&
3503  track->enc->codec_id == AV_CODEC_ID_MP3 && track->timescale < 16000) {
3504  av_log(s, AV_LOG_ERROR, "track %d: muxing mp3 at %dhz is not supported\n",
3505  i, track->enc->sample_rate);
3506  goto error;
3507  }
3508  } else if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
3509  track->timescale = st->time_base.den;
3510  } else if (st->codec->codec_type == AVMEDIA_TYPE_DATA) {
3511  track->timescale = st->time_base.den;
3512  }
3513  if (!track->height)
3514  track->height = st->codec->height;
3515  /* The ism specific timescale isn't mandatory, but is assumed by
3516  * some tools, such as mp4split. */
3517  if (mov->mode == MODE_ISM)
3518  track->timescale = 10000000;
3519 
3520  avpriv_set_pts_info(st, 64, 1, track->timescale);
3521 
3522  /* copy extradata if it exists */
3523  if (st->codec->extradata_size) {
3526  else {
3527  track->vos_len = st->codec->extradata_size;
3528  track->vos_data = av_malloc(track->vos_len);
3529  memcpy(track->vos_data, st->codec->extradata, track->vos_len);
3530  }
3531  }
3532  }
3533 
3534  enable_tracks(s);
3535 
3536  if (mov->flags & FF_MOV_FLAG_FRAGMENT) {
3537  /* If no fragmentation options have been set, set a default. */
3538  if (!(mov->flags & (FF_MOV_FLAG_FRAG_KEYFRAME |
3542  } else {
3543  if (mov->flags & FF_MOV_FLAG_FASTSTART)
3544  mov->reserved_moov_pos = avio_tell(pb);
3545  mov_write_mdat_tag(pb, mov);
3546  }
3547 
3548  if (t = av_dict_get(s->metadata, "creation_time", NULL, 0))
3549  mov->time = ff_iso8601_to_unix_time(t->value);
3550  if (mov->time)
3551  mov->time += 0x7C25B080; // 1970 based -> 1904 based
3552 
3553  if (mov->chapter_track)
3554  if (mov_create_chapter_track(s, mov->chapter_track) < 0)
3555  goto error;
3556 
3557  if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
3558  /* Initialize the hint tracks for each audio and video stream */
3559  for (i = 0; i < s->nb_streams; i++) {
3560  AVStream *st = s->streams[i];
3561  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
3563  ff_mov_init_hinting(s, hint_track, i);
3564  hint_track++;
3565  }
3566  }
3567  }
3568 
3569  avio_flush(pb);
3570 
3571  if (mov->flags & FF_MOV_FLAG_ISML)
3572  mov_write_isml_manifest(pb, mov);
3573 
3574  if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV) {
3575  mov_write_moov_tag(pb, mov, s);
3576  mov->fragments++;
3577  }
3578 
3579  return 0;
3580  error:
3581  mov_free(s);
3582  return -1;
3583 }
3584 
3586 {
3587  int ret;
3588  AVIOContext *moov_buf;
3589  MOVMuxContext *mov = s->priv_data;
3590 
3591  if ((ret = ffio_open_null_buf(&moov_buf)) < 0)
3592  return ret;
3593  mov_write_moov_tag(moov_buf, mov, s);
3594  return ffio_close_null_buf(moov_buf);
3595 }
3596 
3597 /*
3598  * This function gets the moov size if moved to the top of the file: the chunk
3599  * offset table can switch between stco (32-bit entries) to co64 (64-bit
3600  * entries) when the moov is moved to the beginning, so the size of the moov
3601  * would change. It also updates the chunk offset tables.
3602  */
3604 {
3605  int i, moov_size, moov_size2;
3606  MOVMuxContext *mov = s->priv_data;
3607 
3608  moov_size = get_moov_size(s);
3609  if (moov_size < 0)
3610  return moov_size;
3611 
3612  for (i = 0; i < mov->nb_streams; i++)
3613  mov->tracks[i].data_offset += moov_size;
3614 
3615  moov_size2 = get_moov_size(s);
3616  if (moov_size2 < 0)
3617  return moov_size2;
3618 
3619  /* if the size changed, we just switched from stco to co64 and need to
3620  * update the offsets */
3621  if (moov_size2 != moov_size)
3622  for (i = 0; i < mov->nb_streams; i++)
3623  mov->tracks[i].data_offset += moov_size2 - moov_size;
3624 
3625  return moov_size2;
3626 }
3627 
3629 {
3630  int ret = 0, moov_size;
3631  MOVMuxContext *mov = s->priv_data;
3632  int64_t pos, pos_end = avio_tell(s->pb);
3633  uint8_t *buf, *read_buf[2];
3634  int read_buf_id = 0;
3635  int read_size[2];
3636  AVIOContext *read_pb;
3637 
3638  moov_size = compute_moov_size(s);
3639  if (moov_size < 0)
3640  return moov_size;
3641 
3642  buf = av_malloc(moov_size * 2);
3643  if (!buf)
3644  return AVERROR(ENOMEM);
3645  read_buf[0] = buf;
3646  read_buf[1] = buf + moov_size;
3647 
3648  /* Shift the data: the AVIO context of the output can only be used for
3649  * writing, so we re-open the same output, but for reading. It also avoids
3650  * a read/seek/write/seek back and forth. */
3651  avio_flush(s->pb);
3652  ret = avio_open(&read_pb, s->filename, AVIO_FLAG_READ);
3653  if (ret < 0) {
3654  av_log(s, AV_LOG_ERROR, "Unable to re-open %s output file for "
3655  "the second pass (faststart)\n", s->filename);
3656  goto end;
3657  }
3658 
3659  /* mark the end of the shift to up to the last data we wrote, and get ready
3660  * for writing */
3661  pos_end = avio_tell(s->pb);
3662  avio_seek(s->pb, mov->reserved_moov_pos + moov_size, SEEK_SET);
3663 
3664  /* start reading at where the new moov will be placed */
3665  avio_seek(read_pb, mov->reserved_moov_pos, SEEK_SET);
3666  pos = avio_tell(read_pb);
3667 
3668 #define READ_BLOCK do { \
3669  read_size[read_buf_id] = avio_read(read_pb, read_buf[read_buf_id], moov_size); \
3670  read_buf_id ^= 1; \
3671 } while (0)
3672 
3673  /* shift data by chunk of at most moov_size */
3674  READ_BLOCK;
3675  do {
3676  int n;
3677  READ_BLOCK;
3678  n = read_size[read_buf_id];
3679  if (n <= 0)
3680  break;
3681  avio_write(s->pb, read_buf[read_buf_id], n);
3682  pos += n;
3683  } while (pos < pos_end);
3684  avio_close(read_pb);
3685 
3686 end:
3687  av_free(buf);
3688  return ret;
3689 }
3690 
3692 {
3693  MOVMuxContext *mov = s->priv_data;
3694  AVIOContext *pb = s->pb;
3695  int res = 0;
3696  int i;
3697  int64_t moov_pos;
3698 
3699  // If there were no chapters when the header was written, but there
3700  // are chapters now, write them in the trailer. This only works
3701  // when we are not doing fragments.
3702  if (!mov->chapter_track && !(mov->flags & FF_MOV_FLAG_FRAGMENT)) {
3703  if (mov->mode & (MODE_MP4|MODE_MOV|MODE_IPOD) && s->nb_chapters) {
3704  mov->chapter_track = mov->nb_streams++;
3705  if ((res = mov_create_chapter_track(s, mov->chapter_track)) < 0)
3706  goto error;
3707  }
3708  }
3709 
3710  if (!(mov->flags & FF_MOV_FLAG_FRAGMENT)) {
3711  moov_pos = avio_tell(pb);
3712 
3713  /* Write size of mdat tag */
3714  if (mov->mdat_size + 8 <= UINT32_MAX) {
3715  avio_seek(pb, mov->mdat_pos, SEEK_SET);
3716  avio_wb32(pb, mov->mdat_size + 8);
3717  } else {
3718  /* overwrite 'wide' placeholder atom */
3719  avio_seek(pb, mov->mdat_pos - 8, SEEK_SET);
3720  /* special value: real atom size will be 64 bit value after
3721  * tag field */
3722  avio_wb32(pb, 1);
3723  ffio_wfourcc(pb, "mdat");
3724  avio_wb64(pb, mov->mdat_size + 16);
3725  }
3726  avio_seek(pb, moov_pos, SEEK_SET);
3727 
3728  if (mov->flags & FF_MOV_FLAG_FASTSTART) {
3729  av_log(s, AV_LOG_INFO, "Starting second pass: moving the moov atom to the beginning of the file\n");
3730  res = shift_data(s);
3731  if (res == 0) {
3732  avio_seek(s->pb, mov->reserved_moov_pos, SEEK_SET);
3733  mov_write_moov_tag(pb, mov, s);
3734  }
3735  } else {
3736  mov_write_moov_tag(pb, mov, s);
3737  }
3738  } else {
3739  mov_flush_fragment(s);
3740  mov_write_mfra_tag(pb, mov);
3741  }
3742 
3743  for (i = 0; i < mov->nb_streams; i++) {
3744  if (mov->flags & FF_MOV_FLAG_FRAGMENT &&
3745  mov->tracks[i].vc1_info.struct_offset && s->pb->seekable) {
3746  int64_t off = avio_tell(pb);
3747  uint8_t buf[7];
3748  if (mov_write_dvc1_structs(&mov->tracks[i], buf) >= 0) {
3749  avio_seek(pb, mov->tracks[i].vc1_info.struct_offset, SEEK_SET);
3750  avio_write(pb, buf, 7);
3751  avio_seek(pb, off, SEEK_SET);
3752  }
3753  }
3754  }
3755 
3756 error:
3757  mov_free(s);
3758 
3759  return res;
3760 }
3761 
3762 #if CONFIG_MOV_MUXER
3763 MOV_CLASS(mov)
3764 AVOutputFormat ff_mov_muxer = {
3765  .name = "mov",
3766  .long_name = NULL_IF_CONFIG_SMALL("QuickTime / MOV"),
3767  .extensions = "mov",
3768  .priv_data_size = sizeof(MOVMuxContext),
3769  .audio_codec = AV_CODEC_ID_AAC,
3770  .video_codec = CONFIG_LIBX264_ENCODER ?
3776  .codec_tag = (const AVCodecTag* const []){
3778  },
3779  .priv_class = &mov_muxer_class,
3780 };
3781 #endif
3782 #if CONFIG_TGP_MUXER
3783 MOV_CLASS(tgp)
3784 AVOutputFormat ff_tgp_muxer = {
3785  .name = "3gp",
3786  .long_name = NULL_IF_CONFIG_SMALL("3GP (3GPP file format)"),
3787  .extensions = "3gp",
3788  .priv_data_size = sizeof(MOVMuxContext),
3789  .audio_codec = AV_CODEC_ID_AMR_NB,
3790  .video_codec = AV_CODEC_ID_H263,
3795  .codec_tag = (const AVCodecTag* const []){ codec_3gp_tags, 0 },
3796  .priv_class = &tgp_muxer_class,
3797 };
3798 #endif
3799 #if CONFIG_MP4_MUXER
3800 MOV_CLASS(mp4)
3801 AVOutputFormat ff_mp4_muxer = {
3802  .name = "mp4",
3803  .long_name = NULL_IF_CONFIG_SMALL("MP4 (MPEG-4 Part 14)"),
3804  .mime_type = "application/mp4",
3805  .extensions = "mp4",
3806  .priv_data_size = sizeof(MOVMuxContext),
3807  .audio_codec = AV_CODEC_ID_AAC,
3808  .video_codec = CONFIG_LIBX264_ENCODER ?
3809  AV_CODEC_ID_H264 : AV_CODEC_ID_MPEG4,
3814  .codec_tag = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
3815  .priv_class = &mp4_muxer_class,
3816 };
3817 #endif
3818 #if CONFIG_PSP_MUXER
3819 MOV_CLASS(psp)
3820 AVOutputFormat ff_psp_muxer = {
3821  .name = "psp",
3822  .long_name = NULL_IF_CONFIG_SMALL("PSP MP4 (MPEG-4 Part 14)"),
3823  .extensions = "mp4,psp",
3824  .priv_data_size = sizeof(MOVMuxContext),
3825  .audio_codec = AV_CODEC_ID_AAC,
3826  .video_codec = CONFIG_LIBX264_ENCODER ?
3827  AV_CODEC_ID_H264 : AV_CODEC_ID_MPEG4,
3832  .codec_tag = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
3833  .priv_class = &psp_muxer_class,
3834 };
3835 #endif
3836 #if CONFIG_TG2_MUXER
3837 MOV_CLASS(tg2)
3838 AVOutputFormat ff_tg2_muxer = {
3839  .name = "3g2",
3840  .long_name = NULL_IF_CONFIG_SMALL("3GP2 (3GPP2 file format)"),
3841  .extensions = "3g2",
3842  .priv_data_size = sizeof(MOVMuxContext),
3843  .audio_codec = AV_CODEC_ID_AMR_NB,
3844  .video_codec = AV_CODEC_ID_H263,
3849  .codec_tag = (const AVCodecTag* const []){ codec_3gp_tags, 0 },
3850  .priv_class = &tg2_muxer_class,
3851 };
3852 #endif
3853 #if CONFIG_IPOD_MUXER
3854 MOV_CLASS(ipod)
3855 AVOutputFormat ff_ipod_muxer = {
3856  .name = "ipod",
3857  .long_name = NULL_IF_CONFIG_SMALL("iPod H.264 MP4 (MPEG-4 Part 14)"),
3858  .mime_type = "application/mp4",
3859  .extensions = "m4v,m4a",
3860  .priv_data_size = sizeof(MOVMuxContext),
3861  .audio_codec = AV_CODEC_ID_AAC,
3862  .video_codec = AV_CODEC_ID_H264,
3867  .codec_tag = (const AVCodecTag* const []){ codec_ipod_tags, 0 },
3868  .priv_class = &ipod_muxer_class,
3869 };
3870 #endif
3871 #if CONFIG_ISMV_MUXER
3872 MOV_CLASS(ismv)
3873 AVOutputFormat ff_ismv_muxer = {
3874  .name = "ismv",
3875  .long_name = NULL_IF_CONFIG_SMALL("ISMV/ISMA (Smooth Streaming)"),
3876  .mime_type = "application/mp4",
3877  .extensions = "ismv,isma",
3878  .priv_data_size = sizeof(MOVMuxContext),
3879  .audio_codec = AV_CODEC_ID_AAC,
3880  .video_codec = AV_CODEC_ID_H264,
3885  .codec_tag = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
3886  .priv_class = &ismv_muxer_class,
3887 };
3888 #endif
3889 #if CONFIG_F4V_MUXER
3890 MOV_CLASS(f4v)
3891 AVOutputFormat ff_f4v_muxer = {
3892  .name = "f4v",
3893  .long_name = NULL_IF_CONFIG_SMALL("F4V Adobe Flash Video"),
3894  .mime_type = "application/f4v",
3895  .extensions = "f4v",
3896  .priv_data_size = sizeof(MOVMuxContext),
3897  .audio_codec = AV_CODEC_ID_AAC,
3898  .video_codec = AV_CODEC_ID_H264,
3903  .codec_tag = (const AVCodecTag* const []){ codec_f4v_tags, 0 },
3904  .priv_class = &f4v_muxer_class,
3905 };
3906 #endif
unsigned int nb_chapters
Number of chapters in AVChapter array.
Definition: avformat.h:1119
#define MOV_TRACK_STPS
Definition: movenc.h:88
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:789
static int utf8len(const uint8_t *b)
Definition: movenc.c:81
static int mov_write_extradata_tag(AVIOContext *pb, MOVTrack *track)
This function writes extradata "as is".
Definition: movenc.c:283
packed YUV 4:2:2, 16bpp, Cb Y0 Cr Y1
Definition: pixfmt.h:84
int ff_mov_iso639_to_lang(const char lang[4], int mp4)
Definition: isom.c:336
internal header for HEVC (de)muxer utilities
void avio_wb64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:341
static void av_unused put_bits32(PutBitContext *s, uint32_t value)
Write exactly 32 bits into a bitstream.
Definition: put_bits.h:182
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
static int mov_write_moof_tag_internal(AVIOContext *pb, MOVMuxContext *mov, int tracks, int moof_size)
Definition: movenc.c:2558
static int ascii_to_wc(AVIOContext *pb, const uint8_t *b)
Definition: movenc.c:2027
Bytestream IO Context.
Definition: avio.h:68
int packet_entry
Definition: movenc.h:134
#define FF_MOV_FLAG_FASTSTART
Definition: movenc.h:177
static int get_cluster_duration(MOVTrack *track, int cluster_idx)
Definition: movenc.c:566
static int mov_write_dinf_tag(AVIOContext *pb)
Definition: movenc.c:1266
Buffered I/O operations.
int size
#define GET_UTF8(val, GET_BYTE, ERROR)
Convert a UTF-8 character (up to 4 bytes) to its 32-bit UCS-4 encoded form.
Definition: common.h:252
AVCodecContext * enc
Definition: movenc.h:95
int tag
stsd fourcc
Definition: movenc.h:93
int language
Definition: movenc.h:91
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:977
AVOption.
Definition: opt.h:234
int ffio_close_null_buf(AVIOContext *s)
Close a null buffer.
Definition: aviobuf.c:1025
static int mov_write_ac3_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:235
static void mov_write_psp_udta_tag(AVIOContext *pb, const char *str, const char *lang, int type)
Definition: movenc.c:2142
#define MOV_TFHD_DEFAULT_DURATION
Definition: isom.h:173
unsigned int entries
Definition: movenc.h:50
AVIOContext * mdat_buf
Definition: movenc.h:160
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:61
static int mov_write_tmcd_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1122
Definition: isom.h:45
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:240
static void param_write_int(AVIOContext *pb, const char *name, int value)
Definition: movenc.c:2240
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:129
#define FF_MOV_FLAG_FRAGMENT
Definition: movenc.h:171
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:67
int nb_frag_info
Definition: movenc.h:125
int max_fragment_size
Definition: movenc.h:158
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:2829
static void skip_bits_long(GetBitContext *s, int n)
Definition: get_bits.h:199
static int mov_write_dref_tag(AVIOContext *pb)
Definition: movenc.c:1230
static int mov_write_string_metadata(AVFormatContext *s, AVIOContext *pb, const char *name, const char *tag, int long_style)
Definition: movenc.c:1935
static const AVCodecTag codec_f4v_tags[]
Definition: movenc.c:928
static int write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: assenc.c:58
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:400
static int mov_get_rawvideo_codec_tag(AVFormatContext *s, MOVTrack *track)
Definition: movenc.c:863
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:769
int num
numerator
Definition: rational.h:44
packed RGB 5:5:5, 16bpp, (msb)1A 5R 5G 5B(lsb), little-endian, most significant bit to 0 ...
Definition: pixfmt.h:118
int index
stream index in AVFormatContext
Definition: avformat.h:700
int size
Definition: avcodec.h:974
#define MOV_TRUN_SAMPLE_CTS
Definition: isom.h:184
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:186
#define AVIO_FLAG_READ
read-only
Definition: avio.h:294
#define av_bswap16
Definition: bswap.h:31
#define AV_RB24
Definition: intreadwrite.h:64
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:1445
static int mov_write_ms_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:353
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1270
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:84
static int mov_write_moof_tag(AVIOContext *pb, MOVMuxContext *mov, int tracks)
Definition: movenc.c:2580
static const struct @111 mov_pix_fmt_tags[]
static int mov_write_pasp_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1009
unsigned int ff_codec_get_tag(const AVCodecTag *tags, enum AVCodecID id)
Definition: utils.c:1933
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:1834
#define FF_ARRAY_ELEMS(a)
#define FF_MOV_FLAG_ISML
Definition: movenc.h:176
static int mov_write_track_metadata(AVIOContext *pb, AVStream *st, const char *tag, const char *str)
Definition: movenc.c:1695
#define MOV_TFHD_DURATION_IS_EMPTY
Definition: isom.h:176
AVStream * st
Definition: movenc.h:94
AVDictionary * metadata
Definition: avformat.h:909
static uint16_t language_code(const char *str)
Definition: movenc.c:2038
#define AVFMT_ALLOW_FLUSH
Format allows flushing.
Definition: avformat.h:425
static int mov_write_hdlr_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1324
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:965
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
Definition: avcodec.h:1844
static int mov_write_tfrf_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track, int entry)
Definition: movenc.c:2481
int64_t track_duration
Definition: movenc.h:83
static int64_t duration
Definition: avplay.c:246
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
static int mov_write_glbl_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:529
#define MOV_TKHD_FLAG_ENABLED
Definition: isom.h:196
packed RGB 5:6:5, 16bpp, (msb) 5R 6G 5B(lsb), little-endian
Definition: pixfmt.h:116
static int shift_data(AVFormatContext *s)
Definition: movenc.c:3628
Format I/O context.
Definition: avformat.h:922
int64_t frag_start
Definition: movenc.h:122
static int mov_write_stsc_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:169
static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: movenc.c:3138
static int mov_write_fiel_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:979
Public dictionary API.
static int mov_write_uuidusmt_tag(AVIOContext *pb, AVFormatContext *s)
Definition: movenc.c:2155
int64_t default_duration
Definition: movenc.h:114
#define READ_BLOCK
uint32_t flags
Definition: movenc.h:90
#define FF_MOV_FLAG_EMPTY_MOOV
Definition: movenc.h:172
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:271
uint8_t
Round toward +infinity.
Definition: mathematics.h:53
static int mov_write_uuid_tag_psp(AVIOContext *pb, MOVTrack *mov)
Definition: movenc.c:1656
unsigned frag_info_capacity
Definition: movenc.h:127
Opaque data information usually continuous.
Definition: avutil.h:189
uint64_t mdat_size
Definition: movenc.h:146
unsigned int samples_in_chunk
Definition: movenc.h:49
#define MOV_CLASS(flavor)
Definition: movenc.c:73
AVOptions.
static int get_samples_per_packet(MOVTrack *track)
Definition: movenc.c:581
#define AV_WB32(p, d)
Definition: intreadwrite.h:239
const AVCodecTag ff_codec_movvideo_tags[]
Definition: isom.c:69
#define AV_RB32
Definition: intreadwrite.h:130
static int mov_get_codec_tag(AVFormatContext *s, MOVTrack *track)
Definition: movenc.c:879
static void mov_write_uuidprof_tag(AVIOContext *pb, AVFormatContext *s)
Definition: movenc.c:2719
#define b
Definition: input.c:52
static const AVOption options[]
Definition: movenc.c:50
#define MODE_PSP
Definition: movenc.h:38
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1164
const char * name
static int mov_write_tapt_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1566
static int mov_create_chapter_track(AVFormatContext *s, int tracknum)
Definition: movenc.c:3173
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:990
static int mov_write_vmhd_tag(AVIOContext *pb)
Definition: movenc.c:1309
#define MODE_MP4
Definition: movenc.h:35
packed RGB 5:6:5, 16bpp, (msb) 5R 6G 5B(lsb), big-endian
Definition: pixfmt.h:115
static int mov_write_amr_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:221
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:97
static int mov_write_moov_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
Definition: movenc.c:2192
const char data[16]
Definition: mxf.c:70
static double av_q2d(AVRational a)
Convert rational to double.
Definition: rational.h:69
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:38
int chapter_track
qt chapter track number
Definition: movenc.h:144
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1033
uint8_t * data
Definition: avcodec.h:973
#define FF_MOV_FLAG_OMIT_TFHD_OFFSET
Definition: movenc.h:178
static int flags
Definition: log.c:44
int64_t time
Definition: movenc.h:73
static void mov_free(AVFormatContext *s)
Definition: movenc.c:3276
static int mov_write_minf_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1409
#define MOV_TRUN_SAMPLE_SIZE
Definition: isom.h:182
uint32_t tag
Definition: movenc.c:844
static int mov_write_itunes_hdlr_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
Definition: movenc.c:1885
int fragments
Definition: movenc.h:155
static int mov_write_subtitle_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:992
static int mov_write_chan_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:372
int iods_audio_profile
Definition: movenc.h:153
bitstream reader API header.
#define MOV_TIMESCALE
Definition: movenc.h:31
int max_fragment_duration
Definition: movenc.h:156
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:77
static int mov_write_wave_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:397
int64_t mdat_pos
Definition: movenc.h:145
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:219
void ff_mov_close_hinting(MOVTrack *track)
Definition: movenchint.c:458
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2523
static int mov_write_uuid_tag_ipod(AVIOContext *pb)
Write uuid atom.
Definition: movenc.c:963
unsigned int size
Definition: movenc.h:48
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:165
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:1050
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:991
static const uint16_t fiel_data[]
Definition: movenc.c:975
static int write_trailer(AVFormatContext *s)
Definition: assenc.c:64
#define MOV_TFHD_DEFAULT_SIZE
Definition: isom.h:174
static int mov_write_nmhd_tag(AVIOContext *pb)
Definition: movenc.c:1275
#define AV_OPT_FLAG_ENCODING_PARAM
a generic parameter which can be set by the user for muxing or encoding
Definition: opt.h:264
int ism_lookahead
Definition: movenc.h:159
struct AVOutputFormat * oformat
The output container format.
Definition: avformat.h:941
#define r
Definition: input.c:51
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1019
#define MODE_3G2
Definition: movenc.h:40
static int mov_find_codec_tag(AVFormatContext *s, MOVTrack *track)
Definition: movenc.c:937
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:129
uint32_t tref_tag
Definition: movenc.h:103
uint32_t flags
Definition: movenc.h:54
static int mov_write_udta_sdp(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1674
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:167
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:105
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1130
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:2055
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:186
static int mov_write_meta_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
Definition: movenc.c:2013
int64_t tfrf_offset
Definition: movenc.h:75
#define MOV_FRAG_SAMPLE_FLAG_DEPENDS_NO
Definition: isom.h:193
static int mov_write_mdat_tag(AVIOContext *pb, MOVMuxContext *mov)
Definition: movenc.c:2642
static int mov_write_stsz_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:133
int64_t duration
Definition: movenc.h:74
static void put_descr(AVIOContext *pb, int tag, unsigned int size)
Definition: movenc.c:289
static int mov_flush_fragment(AVFormatContext *s)
Definition: movenc.c:2860
int64_t struct_offset
Definition: movenc.h:130
static int mov_write_ilst_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
Definition: movenc.c:1982
static int mov_write_avid_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:722
#define AVERROR(e)
Definition: error.h:43
static void mov_parse_vc1_frame(AVPacket *pkt, MOVTrack *trk, int fragment)
Definition: movenc.c:2803
static int mov_write_dvc1_structs(MOVTrack *track, uint8_t *buf)
Definition: movenc.c:431
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:145
#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:811
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:98
g
Definition: yuv2rgb.c:535
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: avcodec.h:379
AVChapter ** chapters
Definition: avformat.h:1120
int rc_max_rate
maximum bitrate
Definition: avcodec.h:2143
static int mov_create_dvd_sub_decoder_specific_info(MOVTrack *track, AVStream *st)
Definition: movenc.c:3316
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
static int mov_write_isml_manifest(AVIOContext *pb, MOVMuxContext *mov)
Definition: movenc.c:2259
int height
active picture (w/o VBI) height for D-10/IMX
Definition: movenc.h:102
static void put_bits(PutBitContext *s, int n, unsigned int value)
Write up to 31 bits into a bitstream.
Definition: put_bits.h:134
static int mov_write_track_udta_tag(AVIOContext *pb, MOVMuxContext *mov, AVStream *st)
Definition: movenc.c:1709
#define FF_MOV_FLAG_SEPARATE_MOOF
Definition: movenc.h:174
enum AVCodecID codec_id
Definition: mov_chan.c:432
static int mov_write_trun_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track, int moof_size)
Definition: movenc.c:2411
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:780
const AVCodecTag ff_codec_wav_tags[]
Definition: riff.c:353
#define FFMAX(a, b)
Definition: common.h:55
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:1017
int ff_avc_parse_nal_units_buf(const uint8_t *buf_in, uint8_t **buf, int *size)
Definition: avc.c:92
struct MOVTrack::@112 vc1_info
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:95
#define MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES
Definition: isom.h:194
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:81
int mode
Definition: movenc.h:79
const AVCodecTag ff_mp4_obj_type[]
Definition: isom.c:34
const AVCodecTag ff_codec_movsubtitle_tags[]
Definition: isom.c:304
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:96
static int mov_write_mdhd_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1436
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:979
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1868
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:69
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:718
int rc_buffer_size
decoder bitstream buffer size
Definition: avcodec.h:2121
static int mov_write_wfex_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:363
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:97
int iods_skip
Definition: movenc.h:151
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:531
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:978
int ff_avc_parse_nal_units(AVIOContext *pb, const uint8_t *buf_in, int size)
Definition: avc.c:70
#define LIBAVFORMAT_IDENT
Definition: version.h:44
uint64_t pos
Definition: movenc.h:46
int64_t dts
Definition: movenc.h:47
#define MOV_FRAG_INFO_ALLOC_INCREMENT
Definition: movenc.h:29
#define MOV_TRACK_ENABLED
Definition: movenc.h:89
static int mov_write_stts_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1189
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:117
int bit_rate
the average bitrate
Definition: avcodec.h:1114
static int mov_write_trkn_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
Definition: movenc.c:1960
int void avio_flush(AVIOContext *s)
Definition: aviobuf.c:180
int per_stream_grouping
Definition: movenc.h:166
int64_t data_offset
Definition: movenc.h:121
char filename[1024]
input or output filename
Definition: avformat.h:998
static int mov_write_tfhd_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track, int64_t moof_offset)
Definition: movenc.c:2354
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:116
#define FFMIN(a, b)
Definition: common.h:57
const AVCodecTag ff_codec_bmp_tags[]
Definition: riff.c:29
static int mov_write_mvex_tag(AVIOContext *pb, MOVMuxContext *mov)
Definition: movenc.c:1813
int audio_vbr
Definition: movenc.h:101
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
Definition: options.c:124
static int mov_write_chpl_tag(AVIOContext *pb, AVFormatContext *s)
Definition: movenc.c:2067
#define MOV_TKHD_FLAG_IN_MOVIE
Definition: isom.h:197
#define MOV_PARTIAL_SYNC_SAMPLE
Definition: movenc.h:53
int width
picture width / height.
Definition: avcodec.h:1229
int64_t tfrf_offset
Definition: movenc.h:123
int ff_mov_init_hinting(AVFormatContext *s, int index, int src_index)
Definition: movenchint.c:29
int64_t ff_iso8601_to_unix_time(const char *datestr)
Convert a date string in ISO8601 format to Unix timestamp.
Definition: utils.c:2915
static int mov_write_avcc_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:701
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition: avformat.h:415
static int is_clcp_track(MOVTrack *track)
Definition: movenc.c:1318
static int mov_write_trak_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track, AVStream *st)
Definition: movenc.c:1736
void avio_wb24(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:365
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:1065
const char * name
Definition: avformat.h:446
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:817
long sample_size
Definition: movenc.h:85
static int mov_write_string_tag(AVIOContext *pb, const char *name, const char *value, int lang, int long_style)
Definition: movenc.c:1921
static int mov_write_smhd_tag(AVIOContext *pb)
Definition: movenc.c:1299
static int mov_write_stco_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:111
#define AV_RL32
Definition: intreadwrite.h:146
static void param_write_string(AVIOContext *pb, const char *name, const char *value)
Definition: movenc.c:2245
#define MOV_TRUN_SAMPLE_DURATION
Definition: isom.h:181
AVDictionary * metadata
Definition: avformat.h:771
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:68
#define AVFMT_FLAG_CUSTOM_IO
The caller has supplied a custom AVIOContext, don't avio_close() it.
Definition: avformat.h:1041
static int mov_write_hvcc_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:711
Usually treated as AVMEDIA_TYPE_DATA.
Definition: avutil.h:186
enum AVPixelFormat pix_fmt
Definition: movenc.c:843
static int mov_write_trex_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1800
preferred ID for MPEG-1/2 video decoding
Definition: avcodec.h:110
#define MOV_TRACK_CTTS
Definition: movenc.h:87
static int mov_write_mfhd_tag(AVIOContext *pb, MOVMuxContext *mov)
Definition: movenc.c:2345
int iods_video_profile
Definition: movenc.h:152
if(ac->has_optimized_func)
Stream structure.
Definition: avformat.h:699
int64_t reserved_moov_pos
Definition: movenc.h:162
#define AVERROR_PATCHWELCOME
Not yet implemented in Libav, patches welcome.
Definition: error.h:57
int64_t end
chapter start/end time in time_base units
Definition: avformat.h:908
NULL
Definition: eval.c:55
#define AV_DISPOSITION_DEFAULT
Definition: avformat.h:668
static int width
Definition: utils.c:156
#define AV_LOG_INFO
Standard information.
Definition: log.h:134
static int mov_write_esds_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:298
#define MOV_INDEX_CLUSTER_SIZE
Definition: movenc.h:30
static int mov_write_d263_tag(AVIOContext *pb)
Definition: movenc.c:676
enum AVMediaType codec_type
Definition: avcodec.h:1058
version
Definition: ffv1enc.c:1080
const AVCodecTag ff_codec_movaudio_tags[]
Definition: isom.c:252
static int mov_write_ctts_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1155
int duration
Definition: isom.h:47
enum AVCodecID codec_id
Definition: avcodec.h:1067
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:240
int cts
Definition: movenc.h:51
int sample_rate
samples per second
Definition: avcodec.h:1807
AVIOContext * pb
I/O context.
Definition: avformat.h:964
#define FF_RTP_FLAG_OPTS(ctx, fieldname)
Definition: rtpenc.h:73
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:144
main external API structure.
Definition: avcodec.h:1050
static uint32_t rgb_to_yuv(uint32_t rgb)
Definition: movenc.c:3300
#define MOV_TRUN_FIRST_SAMPLE_FLAGS
Definition: isom.h:180
#define FF_MOV_FLAG_FRAG_CUSTOM
Definition: movenc.h:175
int hint_track
the track that hints this track, -1 if no hint track is set
Definition: movenc.h:107
static int mov_write_video_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1022
static int mov_write_hmhd_tag(AVIOContext *pb)
Definition: movenc.c:1394
int slices
Definition: movenc.h:135
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1082
static int mov_parse_mpeg2_frame(AVPacket *pkt, uint32_t *flags)
Definition: movenc.c:2782
packed YUV 4:2:2, 16bpp, Y0 Cb Y1 Cr
Definition: pixfmt.h:66
static int mov_write_dvc1_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:512
int extradata_size
Definition: avcodec.h:1165
uint32_t default_size
Definition: movenc.h:116
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:271
static int mov_write_edts_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1598
#define FF_MOV_FLAG_RTP_HINT
Definition: movenc.h:170
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:263
#define FF_COMPLIANCE_NORMAL
Definition: avcodec.h:2360
int count
Definition: isom.h:46
AVFormatContext * fc
Definition: movenc.h:167
Y , 16bpp, big-endian.
Definition: pixfmt.h:100
int index
Definition: gxfenc.c:72
static int co64_required(const MOVTrack *track)
Definition: movenc.c:103
rational number numerator/denominator
Definition: rational.h:43
uint64_t time
Definition: movenc.h:82
int first_packet_seq
Definition: movenc.h:131
static void param_write_hex(AVIOContext *pb, const char *name, const uint8_t *value, int len)
Definition: movenc.c:2250
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:375
int64_t time
Definition: movenc.h:142
#define CONFIG_LIBX264_ENCODER
Definition: config.h:1073
#define AV_WB16(p, d)
Definition: intreadwrite.h:213
int has_keyframes
Definition: movenc.h:86
int ffio_open_null_buf(AVIOContext **s)
Open a write-only fake memory stream.
Definition: aviobuf.c:1015
int entry
Definition: movenc.h:80
size_t av_strlcatf(char *dst, size_t size, const char *fmt,...)
Definition: avstring.c:99
#define MOV_TFHD_DEFAULT_FLAGS
Definition: isom.h:175
MOVFragmentInfo * frag_info
Definition: movenc.h:126
char * major_brand
Definition: movenc.h:164
#define MOV_TRUN_SAMPLE_FLAGS
Definition: isom.h:183
uint8_t * vos_data
Definition: movenc.h:98
int first_packet_entry
Definition: movenc.h:132
Round toward -infinity.
Definition: mathematics.h:52
static const AVCodecTag codec_3gp_tags[]
Definition: movenc.c:917
static int mov_write_tkhd_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track, AVStream *st)
Definition: movenc.c:1483
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:353
static int mp4_get_codec_tag(AVFormatContext *s, MOVTrack *track)
Definition: movenc.c:769
packed RGB 5:5:5, 16bpp, (msb)1A 5R 5G 5B(lsb), big-endian, most significant bit to 0 ...
Definition: pixfmt.h:117
AVFormatContext * rtp_ctx
the format context for the hinting rtp muxer
Definition: movenc.h:109
uint8_t level
Definition: svq3.c:147
int track_id
Definition: movenc.h:92
static int mov_write_udta_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
Definition: movenc.c:2093
static int mov_write_trailer(AVFormatContext *s)
Definition: movenc.c:3691
int height
Definition: gxfenc.c:72
int64_t start
Definition: avformat.h:908
#define FF_MOV_FLAG_FRAG_KEYFRAME
Definition: movenc.h:173
static int mov_write_3gp_udta_tag(AVIOContext *pb, AVFormatContext *s, const char *tag, const char *str)
Definition: movenc.c:2045
static int mov_write_mvhd_tag(AVIOContext *pb, MOVMuxContext *mov)
Definition: movenc.c:1824
Main libavformat public API header.
static int mov_write_tfrf_tags(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track)
Definition: movenc.c:2517
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:65
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:694
int packet_seq
Definition: movenc.h:133
static int mov_write_tfra_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:2592
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:83
#define MOV_TFHD_BASE_DATA_OFFSET
Definition: isom.h:171
static uint32_t get_sample_flags(MOVTrack *track, MOVIentry *entry)
Definition: movenc.c:2405
#define MOV_SYNC_SAMPLE
Definition: movenc.h:52
static int mov_write_tfxd_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:2461
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:112
int disposition
AV_DISPOSITION_* bit field.
Definition: avformat.h:760
uint32_t max_packet_size
Definition: movenc.h:112
int ff_put_wav_header(AVIOContext *pb, AVCodecContext *enc)
Definition: riffenc.c:50
static int mov_write_gmhd_tag(AVIOContext *pb)
Definition: movenc.c:1283
int src_track
the track that this hint track describes
Definition: movenc.h:108
AVRational time_base
time base in which the start/end timestamps are specified
Definition: avformat.h:907
static int mov_write_mdia_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1472
static int mov_write_traf_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track, int64_t moof_offset, int moof_size)
Definition: movenc.c:2531
char * key
Definition: dict.h:75
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:99
unsigned bps
Definition: movenc.c:845
#define MODE_IPOD
Definition: movenc.h:41
static int mov_write_audio_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:600
#define FF_MOV_FLAG_DISABLE_CHPL
Definition: movenc.h:179
static int mov_write_iods_tag(AVIOContext *pb, MOVMuxContext *mov)
Definition: movenc.c:1771
static int mov_write_stss_tag(AVIOContext *pb, MOVTrack *track, uint32_t flag)
Definition: movenc.c:198
char * value
Definition: dict.h:76
uint32_t default_sample_flags
Definition: movenc.h:115
unsigned timescale
Definition: movenc.h:81
int len
static int mov_write_mfra_tag(AVIOContext *pb, MOVMuxContext *mov)
Definition: movenc.c:2616
int channels
number of audio channels
Definition: avcodec.h:1808
static int mov_write_string_data_tag(AVIOContext *pb, const char *data, int lang, int long_style)
Definition: movenc.c:1901
void * priv_data
Format private data.
Definition: avformat.h:950
static int mov_write_header(AVFormatContext *s)
Definition: movenc.c:3365
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:380
static int64_t update_size(AVIOContext *pb, int64_t pos)
Definition: movenc.c:93
#define MOV_TRUN_DATA_OFFSET
Definition: isom.h:179
int64_t start_dts
Definition: movenc.h:105
static av_always_inline int vc1_unescape_buffer(const uint8_t *src, int size, uint8_t *dst)
Definition: vc1.h:424
#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:972
int tref_id
trackID of the referenced track
Definition: movenc.h:104
static int mov_write_ftyp_tag(AVIOContext *pb, AVFormatContext *s)
Definition: movenc.c:2654
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:279
static const AVCodecTag codec_ipod_tags[]
Definition: movenc.c:789
enum AVFieldOrder field_order
Field order.
Definition: avcodec.h:1804
#define AV_DICT_IGNORE_SUFFIX
Definition: dict.h:62
static int mov_write_rtp_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1102
static int mov_write_tref_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1645
#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:1081
unsigned cluster_capacity
Definition: movenc.h:100
int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: movenc.c:2990
static int mov_get_lpcm_flags(enum AVCodecID codec_id)
Compute flags for 'lpcm' tag.
Definition: movenc.c:541
int stream_index
Definition: avcodec.h:975
static int mov_write_stbl_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1244
#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:741
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:493
int rc_min_rate
minimum bitrate
Definition: avcodec.h:2150
static int ipod_get_codec_tag(AVFormatContext *s, MOVTrack *track)
Definition: movenc.c:800
static int mov_write_stsd_tag(AVIOContext *pb, MOVTrack *track)
Definition: movenc.c:1135
static int mov_write_svq3_tag(AVIOContext *pb)
Definition: movenc.c:689
#define MKTAG(a, b, c, d)
Definition: common.h:238
#define AVFMT_TS_NEGATIVE
Format allows muxing negative timestamps.
Definition: avformat.h:431
static void enable_tracks(AVFormatContext *s)
Definition: movenc.c:3235
#define MODE_ISM
Definition: movenc.h:42
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63
char * ff_data_to_hex(char *buf, const uint8_t *src, int size, int lowercase)
Definition: utils.c:2780
This structure stores compressed data.
Definition: avcodec.h:950
int64_t offset
Definition: movenc.h:72
int min_fragment_duration
Definition: movenc.h:157
MOVTrack * tracks
Definition: movenc.h:147
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:205
int strict_std_compliance
strictly follow the standard (MPEG4, ...).
Definition: avcodec.h:2357
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:966
AVIOContext * mdat_buf
Definition: movenc.h:120
int ff_isom_write_avcc(AVIOContext *pb, const uint8_t *data, int len)
Definition: avc.c:106
int nb_streams
Definition: movenc.h:143
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:228
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.h:410
int avio_printf(AVIOContext *s, const char *fmt,...) av_printf_format(2
#define MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC
Definition: isom.h:187
static int compute_moov_size(AVFormatContext *s)
Definition: movenc.c:3603
static int get_moov_size(AVFormatContext *s)
Definition: movenc.c:3585
bitstream writer API