FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
matroskaenc.c
Go to the documentation of this file.
1 /*
2  * Matroska muxer
3  * Copyright (c) 2007 David Conrad
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include <stdint.h>
23 
24 #include "avc.h"
25 #include "hevc.h"
26 #include "avformat.h"
27 #include "avio_internal.h"
28 #include "avlanguage.h"
29 #include "flacenc.h"
30 #include "internal.h"
31 #include "isom.h"
32 #include "matroska.h"
33 #include "riff.h"
34 #include "subtitles.h"
35 #include "vorbiscomment.h"
36 #include "wv.h"
37 
38 #include "libavutil/avstring.h"
40 #include "libavutil/dict.h"
41 #include "libavutil/intfloat.h"
42 #include "libavutil/intreadwrite.h"
43 #include "libavutil/lfg.h"
44 #include "libavutil/mathematics.h"
45 #include "libavutil/opt.h"
46 #include "libavutil/random_seed.h"
47 #include "libavutil/samplefmt.h"
48 #include "libavutil/sha.h"
49 #include "libavutil/stereo3d.h"
50 
51 #include "libavcodec/xiph.h"
52 #include "libavcodec/mpeg4audio.h"
53 #include "libavcodec/internal.h"
54 
55 typedef struct ebml_master {
56  int64_t pos; ///< absolute offset in the file where the master's elements start
57  int sizebytes; ///< how many bytes were reserved for the size
58 } ebml_master;
59 
60 typedef struct mkv_seekhead_entry {
61  unsigned int elementid;
62  uint64_t segmentpos;
64 
65 typedef struct mkv_seekhead {
66  int64_t filepos;
67  int64_t segment_offset; ///< the file offset to the beginning of the segment
68  int reserved_size; ///< -1 if appending to file
72 } mkv_seekhead;
73 
74 typedef struct {
75  uint64_t pts;
77  int tracknum;
78  int64_t cluster_pos; ///< file offset of the cluster containing the block
79  int64_t relative_pos; ///< relative offset from the position of the cluster containing the block
80  int64_t duration; ///< duration of the block according to time base
81 } mkv_cuepoint;
82 
83 typedef struct {
84  int64_t segment_offset;
87 } mkv_cues;
88 
89 typedef struct {
90  int write_dts;
91  int has_cue;
92  int64_t ts_offset;
93 } mkv_track;
94 
95 #define MODE_MATROSKAv2 0x01
96 #define MODE_WEBM 0x02
97 
98 typedef struct MatroskaMuxContext {
99  const AVClass *class;
100  int mode;
103  int64_t segment_offset;
105  int64_t cluster_pos; ///< file offset of the current cluster
106  int64_t cluster_pts;
108  int64_t duration;
112 
114 
116 
119  int64_t cues_pos;
121  int is_dash;
123 
127 
128 
129 /** 2 bytes * 3 for EBML IDs, 3 1-byte EBML lengths, 8 bytes for 64 bit
130  * offset, 4 bytes for target EBML ID */
131 #define MAX_SEEKENTRY_SIZE 21
132 
133 /** per-cuepoint-track - 5 1-byte EBML IDs, 5 1-byte EBML sizes, 4
134  * 8-byte uint max */
135 #define MAX_CUETRACKPOS_SIZE 42
136 
137 /** per-cuepoint - 2 1-byte EBML IDs, 2 1-byte EBML sizes, 8-byte uint max */
138 #define MAX_CUEPOINT_SIZE(num_tracks) 12 + MAX_CUETRACKPOS_SIZE * num_tracks
139 
140 /** Seek preroll value for opus */
141 #define OPUS_SEEK_PREROLL 80000000
142 
143 static int ebml_id_size(unsigned int id)
144 {
145  return (av_log2(id + 1) - 1) / 7 + 1;
146 }
147 
148 static void put_ebml_id(AVIOContext *pb, unsigned int id)
149 {
150  int i = ebml_id_size(id);
151  while (i--)
152  avio_w8(pb, (uint8_t)(id >> (i * 8)));
153 }
154 
155 /**
156  * Write an EBML size meaning "unknown size".
157  *
158  * @param bytes The number of bytes the size should occupy (maximum: 8).
159  */
160 static void put_ebml_size_unknown(AVIOContext *pb, int bytes)
161 {
162  av_assert0(bytes <= 8);
163  avio_w8(pb, 0x1ff >> bytes);
164  ffio_fill(pb, 0xff, bytes - 1);
165 }
166 
167 /**
168  * Calculate how many bytes are needed to represent a given number in EBML.
169  */
170 static int ebml_num_size(uint64_t num)
171 {
172  int bytes = 1;
173  while ((num + 1) >> bytes * 7)
174  bytes++;
175  return bytes;
176 }
177 
178 /**
179  * Write a number in EBML variable length format.
180  *
181  * @param bytes The number of bytes that need to be used to write the number.
182  * If zero, any number of bytes can be used.
183  */
184 static void put_ebml_num(AVIOContext *pb, uint64_t num, int bytes)
185 {
186  int i, needed_bytes = ebml_num_size(num);
187 
188  // sizes larger than this are currently undefined in EBML
189  av_assert0(num < (1ULL << 56) - 1);
190 
191  if (bytes == 0)
192  // don't care how many bytes are used, so use the min
193  bytes = needed_bytes;
194  // the bytes needed to write the given size would exceed the bytes
195  // that we need to use, so write unknown size. This shouldn't happen.
196  av_assert0(bytes >= needed_bytes);
197 
198  num |= 1ULL << bytes * 7;
199  for (i = bytes - 1; i >= 0; i--)
200  avio_w8(pb, (uint8_t)(num >> i * 8));
201 }
202 
203 static void put_ebml_uint(AVIOContext *pb, unsigned int elementid, uint64_t val)
204 {
205  int i, bytes = 1;
206  uint64_t tmp = val;
207  while (tmp >>= 8)
208  bytes++;
209 
210  put_ebml_id(pb, elementid);
211  put_ebml_num(pb, bytes, 0);
212  for (i = bytes - 1; i >= 0; i--)
213  avio_w8(pb, (uint8_t)(val >> i * 8));
214 }
215 
216 static void put_ebml_sint(AVIOContext *pb, unsigned int elementid, int64_t val)
217 {
218  int i, bytes = 1;
219  uint64_t tmp = 2*(val < 0 ? val^-1 : val);
220 
221  while (tmp>>=8) bytes++;
222 
223  put_ebml_id(pb, elementid);
224  put_ebml_num(pb, bytes, 0);
225  for (i = bytes - 1; i >= 0; i--)
226  avio_w8(pb, (uint8_t)(val >> i * 8));
227 }
228 
229 static void put_ebml_float(AVIOContext *pb, unsigned int elementid, double val)
230 {
231  put_ebml_id(pb, elementid);
232  put_ebml_num(pb, 8, 0);
233  avio_wb64(pb, av_double2int(val));
234 }
235 
236 static void put_ebml_binary(AVIOContext *pb, unsigned int elementid,
237  const void *buf, int size)
238 {
239  put_ebml_id(pb, elementid);
240  put_ebml_num(pb, size, 0);
241  avio_write(pb, buf, size);
242 }
243 
244 static void put_ebml_string(AVIOContext *pb, unsigned int elementid,
245  const char *str)
246 {
247  put_ebml_binary(pb, elementid, str, strlen(str));
248 }
249 
250 /**
251  * Write a void element of a given size. Useful for reserving space in
252  * the file to be written to later.
253  *
254  * @param size The number of bytes to reserve, which must be at least 2.
255  */
256 static void put_ebml_void(AVIOContext *pb, uint64_t size)
257 {
258  int64_t currentpos = avio_tell(pb);
259 
260  av_assert0(size >= 2);
261 
263  // we need to subtract the length needed to store the size from the
264  // size we need to reserve so 2 cases, we use 8 bytes to store the
265  // size if possible, 1 byte otherwise
266  if (size < 10)
267  put_ebml_num(pb, size - 1, 0);
268  else
269  put_ebml_num(pb, size - 9, 8);
270  ffio_fill(pb, 0, currentpos + size - avio_tell(pb));
271 }
272 
273 static ebml_master start_ebml_master(AVIOContext *pb, unsigned int elementid,
274  uint64_t expectedsize)
275 {
276  int bytes = expectedsize ? ebml_num_size(expectedsize) : 8;
277  put_ebml_id(pb, elementid);
278  put_ebml_size_unknown(pb, bytes);
279  return (ebml_master) {avio_tell(pb), bytes };
280 }
281 
283 {
284  int64_t pos = avio_tell(pb);
285 
286  if (avio_seek(pb, master.pos - master.sizebytes, SEEK_SET) < 0)
287  return;
288  put_ebml_num(pb, pos - master.pos, master.sizebytes);
289  avio_seek(pb, pos, SEEK_SET);
290 }
291 
292 static void put_xiph_size(AVIOContext *pb, int size)
293 {
294  ffio_fill(pb, 255, size / 255);
295  avio_w8(pb, size % 255);
296 }
297 
298 /**
299  * Initialize a mkv_seekhead element to be ready to index level 1 Matroska
300  * elements. If a maximum number of elements is specified, enough space
301  * will be reserved at the current file location to write a seek head of
302  * that size.
303  *
304  * @param segment_offset The absolute offset to the position in the file
305  * where the segment begins.
306  * @param numelements The maximum number of elements that will be indexed
307  * by this seek head, 0 if unlimited.
308  */
309 static mkv_seekhead *mkv_start_seekhead(AVIOContext *pb, int64_t segment_offset,
310  int numelements)
311 {
312  mkv_seekhead *new_seekhead = av_mallocz(sizeof(mkv_seekhead));
313  if (!new_seekhead)
314  return NULL;
315 
316  new_seekhead->segment_offset = segment_offset;
317 
318  if (numelements > 0) {
319  new_seekhead->filepos = avio_tell(pb);
320  // 21 bytes max for a seek entry, 10 bytes max for the SeekHead ID
321  // and size, and 3 bytes to guarantee that an EBML void element
322  // will fit afterwards
323  new_seekhead->reserved_size = numelements * MAX_SEEKENTRY_SIZE + 13;
324  new_seekhead->max_entries = numelements;
325  put_ebml_void(pb, new_seekhead->reserved_size);
326  }
327  return new_seekhead;
328 }
329 
330 static int mkv_add_seekhead_entry(mkv_seekhead *seekhead, unsigned int elementid, uint64_t filepos)
331 {
332  mkv_seekhead_entry *entries = seekhead->entries;
333 
334  // don't store more elements than we reserved space for
335  if (seekhead->max_entries > 0 && seekhead->max_entries <= seekhead->num_entries)
336  return -1;
337 
338  entries = av_realloc_array(entries, seekhead->num_entries + 1, sizeof(mkv_seekhead_entry));
339  if (!entries)
340  return AVERROR(ENOMEM);
341  seekhead->entries = entries;
342 
343  seekhead->entries[seekhead->num_entries].elementid = elementid;
344  seekhead->entries[seekhead->num_entries++].segmentpos = filepos - seekhead->segment_offset;
345 
346  return 0;
347 }
348 
349 /**
350  * Write the seek head to the file and free it. If a maximum number of
351  * elements was specified to mkv_start_seekhead(), the seek head will
352  * be written at the location reserved for it. Otherwise, it is written
353  * at the current location in the file.
354  *
355  * @return The file offset where the seekhead was written,
356  * -1 if an error occurred.
357  */
358 static int64_t mkv_write_seekhead(AVIOContext *pb, mkv_seekhead *seekhead)
359 {
360  ebml_master metaseek, seekentry;
361  int64_t currentpos;
362  int i;
363 
364  currentpos = avio_tell(pb);
365 
366  if (seekhead->reserved_size > 0) {
367  if (avio_seek(pb, seekhead->filepos, SEEK_SET) < 0) {
368  currentpos = -1;
369  goto fail;
370  }
371  }
372 
373  metaseek = start_ebml_master(pb, MATROSKA_ID_SEEKHEAD, seekhead->reserved_size);
374  for (i = 0; i < seekhead->num_entries; i++) {
375  mkv_seekhead_entry *entry = &seekhead->entries[i];
376 
378 
380  put_ebml_num(pb, ebml_id_size(entry->elementid), 0);
381  put_ebml_id(pb, entry->elementid);
382 
384  end_ebml_master(pb, seekentry);
385  }
386  end_ebml_master(pb, metaseek);
387 
388  if (seekhead->reserved_size > 0) {
389  uint64_t remaining = seekhead->filepos + seekhead->reserved_size - avio_tell(pb);
390  put_ebml_void(pb, remaining);
391  avio_seek(pb, currentpos, SEEK_SET);
392 
393  currentpos = seekhead->filepos;
394  }
395 fail:
396  av_freep(&seekhead->entries);
397  av_free(seekhead);
398 
399  return currentpos;
400 }
401 
402 static mkv_cues *mkv_start_cues(int64_t segment_offset)
403 {
404  mkv_cues *cues = av_mallocz(sizeof(mkv_cues));
405  if (!cues)
406  return NULL;
407 
408  cues->segment_offset = segment_offset;
409  return cues;
410 }
411 
412 static int mkv_add_cuepoint(mkv_cues *cues, int stream, int tracknum, int64_t ts,
413  int64_t cluster_pos, int64_t relative_pos, int64_t duration)
414 {
415  mkv_cuepoint *entries = cues->entries;
416 
417  if (ts < 0)
418  return 0;
419 
420  entries = av_realloc_array(entries, cues->num_entries + 1, sizeof(mkv_cuepoint));
421  if (!entries)
422  return AVERROR(ENOMEM);
423  cues->entries = entries;
424 
425  cues->entries[cues->num_entries].pts = ts;
426  cues->entries[cues->num_entries].stream_idx = stream;
427  cues->entries[cues->num_entries].tracknum = tracknum;
428  cues->entries[cues->num_entries].cluster_pos = cluster_pos - cues->segment_offset;
429  cues->entries[cues->num_entries].relative_pos = relative_pos;
430  cues->entries[cues->num_entries++].duration = duration;
431 
432  return 0;
433 }
434 
435 static int64_t mkv_write_cues(AVFormatContext *s, mkv_cues *cues, mkv_track *tracks, int num_tracks)
436 {
437  AVIOContext *pb = s->pb;
438  ebml_master cues_element;
439  int64_t currentpos;
440  int i, j;
441 
442  currentpos = avio_tell(pb);
443  cues_element = start_ebml_master(pb, MATROSKA_ID_CUES, 0);
444 
445  for (i = 0; i < cues->num_entries; i++) {
446  ebml_master cuepoint, track_positions;
447  mkv_cuepoint *entry = &cues->entries[i];
448  uint64_t pts = entry->pts;
449  int ctp_nb = 0;
450 
451  // Calculate the number of entries, so we know the element size
452  for (j = 0; j < num_tracks; j++)
453  tracks[j].has_cue = 0;
454  for (j = 0; j < cues->num_entries - i && entry[j].pts == pts; j++) {
455  int tracknum = entry[j].stream_idx;
456  av_assert0(tracknum>=0 && tracknum<num_tracks);
457  if (tracks[tracknum].has_cue && s->streams[tracknum]->codec->codec_type != AVMEDIA_TYPE_SUBTITLE)
458  continue;
459  tracks[tracknum].has_cue = 1;
460  ctp_nb ++;
461  }
462 
465 
466  // put all the entries from different tracks that have the exact same
467  // timestamp into the same CuePoint
468  for (j = 0; j < num_tracks; j++)
469  tracks[j].has_cue = 0;
470  for (j = 0; j < cues->num_entries - i && entry[j].pts == pts; j++) {
471  int tracknum = entry[j].stream_idx;
472  av_assert0(tracknum>=0 && tracknum<num_tracks);
473  if (tracks[tracknum].has_cue && s->streams[tracknum]->codec->codec_type != AVMEDIA_TYPE_SUBTITLE)
474  continue;
475  tracks[tracknum].has_cue = 1;
477  put_ebml_uint(pb, MATROSKA_ID_CUETRACK , entry[j].tracknum );
478  put_ebml_uint(pb, MATROSKA_ID_CUECLUSTERPOSITION , entry[j].cluster_pos);
479  put_ebml_uint(pb, MATROSKA_ID_CUERELATIVEPOSITION, entry[j].relative_pos);
480  if (entry[j].duration != -1)
482  end_ebml_master(pb, track_positions);
483  }
484  i += j - 1;
485  end_ebml_master(pb, cuepoint);
486  }
487  end_ebml_master(pb, cues_element);
488 
489  return currentpos;
490 }
491 
493 {
494  const uint8_t *header_start[3];
495  int header_len[3];
496  int first_header_size;
497  int j;
498 
499  if (codec->codec_id == AV_CODEC_ID_VORBIS)
500  first_header_size = 30;
501  else
502  first_header_size = 42;
503 
505  first_header_size, header_start, header_len) < 0) {
506  av_log(s, AV_LOG_ERROR, "Extradata corrupt.\n");
507  return -1;
508  }
509 
510  avio_w8(pb, 2); // number packets - 1
511  for (j = 0; j < 2; j++) {
512  put_xiph_size(pb, header_len[j]);
513  }
514  for (j = 0; j < 3; j++)
515  avio_write(pb, header_start[j], header_len[j]);
516 
517  return 0;
518 }
519 
521 {
522  if (codec->extradata && codec->extradata_size == 2)
523  avio_write(pb, codec->extradata, 2);
524  else
525  avio_wl16(pb, 0x403); // fallback to the version mentioned in matroska specs
526  return 0;
527 }
528 
530  AVIOContext *pb, AVCodecContext *codec)
531 {
532  int write_comment = (codec->channel_layout &&
533  !(codec->channel_layout & ~0x3ffffULL) &&
535  int ret = ff_flac_write_header(pb, codec->extradata, codec->extradata_size,
536  !write_comment);
537 
538  if (ret < 0)
539  return ret;
540 
541  if (write_comment) {
542  const char *vendor = (s->flags & AVFMT_FLAG_BITEXACT) ?
543  "Lavf" : LIBAVFORMAT_IDENT;
544  AVDictionary *dict = NULL;
545  uint8_t buf[32], *data, *p;
546  int len;
547 
548  snprintf(buf, sizeof(buf), "0x%"PRIx64, codec->channel_layout);
549  av_dict_set(&dict, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", buf, 0);
550 
551  len = ff_vorbiscomment_length(dict, vendor);
552  data = av_malloc(len + 4);
553  if (!data) {
554  av_dict_free(&dict);
555  return AVERROR(ENOMEM);
556  }
557 
558  data[0] = 0x84;
559  AV_WB24(data + 1, len);
560 
561  p = data + 4;
562  ff_vorbiscomment_write(&p, &dict, vendor);
563 
564  avio_write(pb, data, len + 4);
565 
566  av_freep(&data);
567  av_dict_free(&dict);
568  }
569 
570  return 0;
571 }
572 
574  int *sample_rate, int *output_sample_rate)
575 {
576  MPEG4AudioConfig mp4ac;
577 
578  if (avpriv_mpeg4audio_get_config(&mp4ac, codec->extradata,
579  codec->extradata_size * 8, 1) < 0) {
580  av_log(s, AV_LOG_ERROR,
581  "Error parsing AAC extradata, unable to determine samplerate.\n");
582  return AVERROR(EINVAL);
583  }
584 
585  *sample_rate = mp4ac.sample_rate;
586  *output_sample_rate = mp4ac.ext_sample_rate;
587  return 0;
588 }
589 
591  AVCodecContext *codec,
592  AVIOContext *dyn_cp)
593 {
594  switch (codec->codec_id) {
595  case AV_CODEC_ID_VORBIS:
596  case AV_CODEC_ID_THEORA:
597  return put_xiph_codecpriv(s, dyn_cp, codec);
598  case AV_CODEC_ID_FLAC:
599  return put_flac_codecpriv(s, dyn_cp, codec);
600  case AV_CODEC_ID_WAVPACK:
601  return put_wv_codecpriv(dyn_cp, codec);
602  case AV_CODEC_ID_H264:
603  return ff_isom_write_avcc(dyn_cp, codec->extradata,
604  codec->extradata_size);
605  case AV_CODEC_ID_HEVC:
606  return ff_isom_write_hvcc(dyn_cp, codec->extradata,
607  codec->extradata_size, 0);
608  case AV_CODEC_ID_ALAC:
609  if (codec->extradata_size < 36) {
610  av_log(s, AV_LOG_ERROR,
611  "Invalid extradata found, ALAC expects a 36-byte "
612  "QuickTime atom.");
613  return AVERROR_INVALIDDATA;
614  } else
615  avio_write(dyn_cp, codec->extradata + 12,
616  codec->extradata_size - 12);
617  break;
618  default:
619  if (codec->codec_id == AV_CODEC_ID_PRORES &&
621  avio_wl32(dyn_cp, codec->codec_tag);
622  } else if (codec->extradata_size && codec->codec_id != AV_CODEC_ID_TTA)
623  avio_write(dyn_cp, codec->extradata, codec->extradata_size);
624  }
625 
626  return 0;
627 }
628 
630  AVCodecContext *codec, int native_id,
631  int qt_id)
632 {
633  AVIOContext *dyn_cp;
634  uint8_t *codecpriv;
635  int ret, codecpriv_size;
636 
637  ret = avio_open_dyn_buf(&dyn_cp);
638  if (ret < 0)
639  return ret;
640 
641  if (native_id) {
642  ret = mkv_write_native_codecprivate(s, codec, dyn_cp);
643  } else if (codec->codec_type == AVMEDIA_TYPE_VIDEO) {
644  if (qt_id) {
645  if (!codec->codec_tag)
647  codec->codec_id);
648  if (codec->extradata_size) {
651  ) {
652  int i;
653  avio_wb32(dyn_cp, 0x5a + codec->extradata_size);
654  avio_wl32(dyn_cp, codec->codec_tag);
655  for(i = 0; i < 0x5a - 8; i++)
656  avio_w8(dyn_cp, 0);
657  }
658  avio_write(dyn_cp, codec->extradata, codec->extradata_size);
659  }
660  } else {
662  av_log(s, AV_LOG_WARNING, "codec %s is not supported by this format\n",
663  avcodec_get_name(codec->codec_id));
664 
665  if (!codec->codec_tag)
667  codec->codec_id);
668  if (!codec->codec_tag) {
669  av_log(s, AV_LOG_ERROR, "No bmp codec tag found for codec %s\n",
670  avcodec_get_name(codec->codec_id));
671  ret = AVERROR(EINVAL);
672  }
673 
674  ff_put_bmp_header(dyn_cp, codec, ff_codec_bmp_tags, 0, 0);
675  }
676  } else if (codec->codec_type == AVMEDIA_TYPE_AUDIO) {
677  unsigned int tag;
679  if (!tag) {
680  av_log(s, AV_LOG_ERROR, "No wav codec tag found for codec %s\n",
681  avcodec_get_name(codec->codec_id));
682  ret = AVERROR(EINVAL);
683  }
684  if (!codec->codec_tag)
685  codec->codec_tag = tag;
686 
688  }
689 
690  codecpriv_size = avio_close_dyn_buf(dyn_cp, &codecpriv);
691  if (codecpriv_size)
693  codecpriv_size);
694  av_free(codecpriv);
695  return ret;
696 }
697 
698 
700  AVStream *st, int mode, int *h_width, int *h_height)
701 {
702  int i;
703  int ret = 0;
706 
707  *h_width = 1;
708  *h_height = 1;
709  // convert metadata into proper side data and add it to the stream
710  if ((tag = av_dict_get(st->metadata, "stereo_mode", NULL, 0)) ||
711  (tag = av_dict_get( s->metadata, "stereo_mode", NULL, 0))) {
712  int stereo_mode = atoi(tag->value);
713 
714  for (i=0; i<MATROSKA_VIDEO_STEREOMODE_TYPE_NB; i++)
715  if (!strcmp(tag->value, ff_matroska_video_stereo_mode[i])){
716  stereo_mode = i;
717  break;
718  }
719 
720  if (stereo_mode < MATROSKA_VIDEO_STEREOMODE_TYPE_NB &&
721  stereo_mode != 10 && stereo_mode != 12) {
722  int ret = ff_mkv_stereo3d_conv(st, stereo_mode);
723  if (ret < 0)
724  return ret;
725  }
726  }
727 
728  // iterate to find the stereo3d side data
729  for (i = 0; i < st->nb_side_data; i++) {
730  AVPacketSideData sd = st->side_data[i];
731  if (sd.type == AV_PKT_DATA_STEREO3D) {
732  AVStereo3D *stereo = (AVStereo3D *)sd.data;
733 
734  switch (stereo->type) {
735  case AV_STEREO3D_2D:
737  break;
739  format = (stereo->flags & AV_STEREO3D_FLAG_INVERT)
742  *h_width = 2;
743  break;
746  if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
747  format--;
748  *h_height = 2;
749  break;
752  if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
753  format--;
754  break;
755  case AV_STEREO3D_LINES:
757  if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
758  format--;
759  *h_height = 2;
760  break;
761  case AV_STEREO3D_COLUMNS:
763  if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
764  format--;
765  *h_width = 2;
766  break;
769  if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
770  format++;
771  break;
772  }
773  break;
774  }
775  }
776 
777  if (format == MATROSKA_VIDEO_STEREOMODE_TYPE_NB)
778  return ret;
779 
780  // if webm, do not write unsupported modes
781  if ((mode == MODE_WEBM &&
784  || format >= MATROSKA_VIDEO_STEREOMODE_TYPE_NB) {
785  av_log(s, AV_LOG_ERROR,
786  "The specified stereo mode is not valid.\n");
788  return AVERROR(EINVAL);
789  }
790 
791  // write StereoMode if format is valid
793 
794  return ret;
795 }
796 
798  int i, AVIOContext *pb, int default_stream_exists)
799 {
800  AVStream *st = s->streams[i];
801  AVCodecContext *codec = st->codec;
802  ebml_master subinfo, track;
803  int native_id = 0;
804  int qt_id = 0;
805  int bit_depth = av_get_bits_per_sample(codec->codec_id);
806  int sample_rate = codec->sample_rate;
807  int output_sample_rate = 0;
808  int display_width_div = 1;
809  int display_height_div = 1;
810  int j, ret;
812 
813  // ms precision is the de-facto standard timescale for mkv files
814  avpriv_set_pts_info(st, 64, 1, 1000);
815 
816  if (codec->codec_type == AVMEDIA_TYPE_ATTACHMENT) {
817  mkv->have_attachments = 1;
818  return 0;
819  }
820 
821  if (!bit_depth && codec->codec_id != AV_CODEC_ID_ADPCM_G726)
822  bit_depth = av_get_bytes_per_sample(codec->sample_fmt) << 3;
823  if (!bit_depth)
824  bit_depth = codec->bits_per_coded_sample;
825 
826  if (codec->codec_id == AV_CODEC_ID_AAC) {
827  ret = get_aac_sample_rates(s, codec, &sample_rate, &output_sample_rate);
828  if (ret < 0)
829  return ret;
830  }
831 
834  mkv->is_dash ? mkv->dash_track_number : i + 1);
836  mkv->is_dash ? mkv->dash_track_number : i + 1);
837  put_ebml_uint (pb, MATROSKA_ID_TRACKFLAGLACING , 0); // no lacing (yet)
838 
839  if ((tag = av_dict_get(st->metadata, "title", NULL, 0)))
841  tag = av_dict_get(st->metadata, "language", NULL, 0);
842  if (mkv->mode != MODE_WEBM || codec->codec_id != AV_CODEC_ID_WEBVTT) {
843  put_ebml_string(pb, MATROSKA_ID_TRACKLANGUAGE, tag && tag->value ? tag->value:"und");
844  } else if (tag && tag->value) {
846  }
847 
848  // The default value for TRACKFLAGDEFAULT is 1, so add element
849  // if we need to clear it.
850  if (default_stream_exists && !(st->disposition & AV_DISPOSITION_DEFAULT))
852 
855 
856  if (mkv->mode == MODE_WEBM && codec->codec_id == AV_CODEC_ID_WEBVTT) {
857  const char *codec_id;
859  codec_id = "D_WEBVTT/CAPTIONS";
860  native_id = MATROSKA_TRACK_TYPE_SUBTITLE;
861  } else if (st->disposition & AV_DISPOSITION_DESCRIPTIONS) {
862  codec_id = "D_WEBVTT/DESCRIPTIONS";
863  native_id = MATROSKA_TRACK_TYPE_METADATA;
864  } else if (st->disposition & AV_DISPOSITION_METADATA) {
865  codec_id = "D_WEBVTT/METADATA";
866  native_id = MATROSKA_TRACK_TYPE_METADATA;
867  } else {
868  codec_id = "D_WEBVTT/SUBTITLES";
869  native_id = MATROSKA_TRACK_TYPE_SUBTITLE;
870  }
871  put_ebml_string(pb, MATROSKA_ID_CODECID, codec_id);
872  } else {
873  // look for a codec ID string specific to mkv to use,
874  // if none are found, use AVI codes
875  for (j = 0; ff_mkv_codec_tags[j].id != AV_CODEC_ID_NONE; j++) {
876  if (ff_mkv_codec_tags[j].id == codec->codec_id) {
878  native_id = 1;
879  break;
880  }
881  }
882  }
883 
884  if (codec->codec_type == AVMEDIA_TYPE_AUDIO && codec->initial_padding && codec->codec_id == AV_CODEC_ID_OPUS) {
885 // mkv->tracks[i].ts_offset = av_rescale_q(codec->initial_padding,
886 // (AVRational){ 1, codec->sample_rate },
887 // st->time_base);
888 
891  (AVRational){ 1, codec->sample_rate },
892  (AVRational){ 1, 1000000000 }));
893  }
894  if (codec->codec_id == AV_CODEC_ID_OPUS) {
896  }
897 
898  if (mkv->mode == MODE_WEBM && !(codec->codec_id == AV_CODEC_ID_VP8 ||
899  codec->codec_id == AV_CODEC_ID_VP9 ||
900  codec->codec_id == AV_CODEC_ID_OPUS ||
901  codec->codec_id == AV_CODEC_ID_VORBIS ||
902  codec->codec_id == AV_CODEC_ID_WEBVTT)) {
904  "Only VP8 or VP9 video and Vorbis or Opus audio and WebVTT subtitles are supported for WebM.\n");
905  return AVERROR(EINVAL);
906  }
907 
908  switch (codec->codec_type) {
909  case AVMEDIA_TYPE_VIDEO:
911 
912  if( st->avg_frame_rate.num > 0 && st->avg_frame_rate.den > 0
913  && 1.0/av_q2d(st->avg_frame_rate) > av_q2d(codec->time_base))
914  put_ebml_uint(pb, MATROSKA_ID_TRACKDEFAULTDURATION, 1E9 / av_q2d(st->avg_frame_rate));
915  else
916  put_ebml_uint(pb, MATROSKA_ID_TRACKDEFAULTDURATION, av_q2d(codec->time_base)*1E9);
917 
918  if (!native_id &&
919  ff_codec_get_tag(ff_codec_movvideo_tags, codec->codec_id) &&
920  (!ff_codec_get_tag(ff_codec_bmp_tags, codec->codec_id) ||
921  codec->codec_id == AV_CODEC_ID_SVQ1 ||
922  codec->codec_id == AV_CODEC_ID_SVQ3 ||
923  codec->codec_id == AV_CODEC_ID_CINEPAK))
924  qt_id = 1;
925 
926  if (qt_id)
927  put_ebml_string(pb, MATROSKA_ID_CODECID, "V_QUICKTIME");
928  else if (!native_id) {
929  // if there is no mkv-specific codec ID, use VFW mode
930  put_ebml_string(pb, MATROSKA_ID_CODECID, "V_MS/VFW/FOURCC");
931  mkv->tracks[i].write_dts = 1;
932  }
933 
934  subinfo = start_ebml_master(pb, MATROSKA_ID_TRACKVIDEO, 0);
935  // XXX: interlace flag?
936  put_ebml_uint (pb, MATROSKA_ID_VIDEOPIXELWIDTH , codec->width);
937  put_ebml_uint (pb, MATROSKA_ID_VIDEOPIXELHEIGHT, codec->height);
938 
939  // check both side data and metadata for stereo information,
940  // write the result to the bitstream if any is found
941  ret = mkv_write_stereo_mode(s, pb, st, mkv->mode,
942  &display_width_div,
943  &display_height_div);
944  if (ret < 0)
945  return ret;
946 
947  if (((tag = av_dict_get(st->metadata, "alpha_mode", NULL, 0)) && atoi(tag->value)) ||
948  ((tag = av_dict_get( s->metadata, "alpha_mode", NULL, 0)) && atoi(tag->value)) ||
949  (codec->pix_fmt == AV_PIX_FMT_YUVA420P)) {
951  }
952 
953  // write DisplayWidth and DisplayHeight, they contain the size of
954  // a single source view and/or the display aspect ratio
955  if (st->sample_aspect_ratio.num) {
956  int64_t d_width = av_rescale(codec->width, st->sample_aspect_ratio.num, st->sample_aspect_ratio.den);
957  if (d_width > INT_MAX) {
958  av_log(s, AV_LOG_ERROR, "Overflow in display width\n");
959  return AVERROR(EINVAL);
960  }
961  put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYWIDTH , d_width / display_width_div);
962  put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYHEIGHT, codec->height / display_height_div);
963  } else if (display_width_div != 1 || display_height_div != 1) {
964  put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYWIDTH , codec->width / display_width_div);
965  put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYHEIGHT, codec->height / display_height_div);
966  }
967 
968  if (codec->codec_id == AV_CODEC_ID_RAWVIDEO) {
969  uint32_t color_space = av_le2ne32(codec->codec_tag);
970  put_ebml_binary(pb, MATROSKA_ID_VIDEOCOLORSPACE, &color_space, sizeof(color_space));
971  }
972 
973  end_ebml_master(pb, subinfo);
974  break;
975 
976  case AVMEDIA_TYPE_AUDIO:
978 
979  if (!native_id)
980  // no mkv-specific ID, use ACM mode
981  put_ebml_string(pb, MATROSKA_ID_CODECID, "A_MS/ACM");
982 
983  subinfo = start_ebml_master(pb, MATROSKA_ID_TRACKAUDIO, 0);
984  put_ebml_uint (pb, MATROSKA_ID_AUDIOCHANNELS , codec->channels);
986  if (output_sample_rate)
987  put_ebml_float(pb, MATROSKA_ID_AUDIOOUTSAMPLINGFREQ, output_sample_rate);
988  if (bit_depth)
990  end_ebml_master(pb, subinfo);
991  break;
992 
994  if (!native_id) {
995  av_log(s, AV_LOG_ERROR, "Subtitle codec %d is not supported.\n", codec->codec_id);
996  return AVERROR(ENOSYS);
997  }
998 
999  if (mkv->mode != MODE_WEBM || codec->codec_id != AV_CODEC_ID_WEBVTT)
1000  native_id = MATROSKA_TRACK_TYPE_SUBTITLE;
1001 
1002  put_ebml_uint(pb, MATROSKA_ID_TRACKTYPE, native_id);
1003  break;
1004  default:
1005  av_log(s, AV_LOG_ERROR, "Only audio, video, and subtitles are supported for Matroska.\n");
1006  return AVERROR(EINVAL);
1007  }
1008 
1009  if (mkv->mode != MODE_WEBM || codec->codec_id != AV_CODEC_ID_WEBVTT) {
1010  ret = mkv_write_codecprivate(s, pb, codec, native_id, qt_id);
1011  if (ret < 0)
1012  return ret;
1013  }
1014 
1015  end_ebml_master(pb, track);
1016 
1017  return 0;
1018 }
1019 
1021 {
1022  MatroskaMuxContext *mkv = s->priv_data;
1023  AVIOContext *pb = s->pb;
1024  ebml_master tracks;
1025  int i, ret, default_stream_exists = 0;
1026 
1028  if (ret < 0)
1029  return ret;
1030 
1031  tracks = start_ebml_master(pb, MATROSKA_ID_TRACKS, 0);
1032  for (i = 0; i < s->nb_streams; i++) {
1033  AVStream *st = s->streams[i];
1034  default_stream_exists |= st->disposition & AV_DISPOSITION_DEFAULT;
1035  }
1036  for (i = 0; i < s->nb_streams; i++) {
1037  ret = mkv_write_track(s, mkv, i, pb, default_stream_exists);
1038  if (ret < 0)
1039  return ret;
1040  }
1041  end_ebml_master(pb, tracks);
1042  return 0;
1043 }
1044 
1046 {
1047  MatroskaMuxContext *mkv = s->priv_data;
1048  AVIOContext *pb = s->pb;
1049  ebml_master chapters, editionentry;
1050  AVRational scale = {1, 1E9};
1051  int i, ret;
1052 
1053  if (!s->nb_chapters || mkv->wrote_chapters)
1054  return 0;
1055 
1057  if (ret < 0) return ret;
1058 
1059  chapters = start_ebml_master(pb, MATROSKA_ID_CHAPTERS , 0);
1060  editionentry = start_ebml_master(pb, MATROSKA_ID_EDITIONENTRY, 0);
1063  for (i = 0; i < s->nb_chapters; i++) {
1064  ebml_master chapteratom, chapterdisplay;
1065  AVChapter *c = s->chapters[i];
1066  AVDictionaryEntry *t = NULL;
1067 
1068  chapteratom = start_ebml_master(pb, MATROSKA_ID_CHAPTERATOM, 0);
1071  av_rescale_q(c->start, c->time_base, scale));
1073  av_rescale_q(c->end, c->time_base, scale));
1076  if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
1077  chapterdisplay = start_ebml_master(pb, MATROSKA_ID_CHAPTERDISPLAY, 0);
1080  end_ebml_master(pb, chapterdisplay);
1081  }
1082  end_ebml_master(pb, chapteratom);
1083  }
1084  end_ebml_master(pb, editionentry);
1085  end_ebml_master(pb, chapters);
1086 
1087  mkv->wrote_chapters = 1;
1088  return 0;
1089 }
1090 
1092 {
1093  uint8_t *key = av_strdup(t->key);
1094  uint8_t *p = key;
1095  const uint8_t *lang = NULL;
1096  ebml_master tag;
1097 
1098  if (!key)
1099  return AVERROR(ENOMEM);
1100 
1101  if ((p = strrchr(p, '-')) &&
1102  (lang = av_convert_lang_to(p + 1, AV_LANG_ISO639_2_BIBL)))
1103  *p = 0;
1104 
1105  p = key;
1106  while (*p) {
1107  if (*p == ' ')
1108  *p = '_';
1109  else if (*p >= 'a' && *p <= 'z')
1110  *p -= 'a' - 'A';
1111  p++;
1112  }
1113 
1116  if (lang)
1119  end_ebml_master(pb, tag);
1120 
1121  av_freep(&key);
1122  return 0;
1123 }
1124 
1125 static int mkv_write_tag(AVFormatContext *s, AVDictionary *m, unsigned int elementid,
1126  unsigned int uid, ebml_master *tags)
1127 {
1128  MatroskaMuxContext *mkv = s->priv_data;
1129  ebml_master tag, targets;
1130  AVDictionaryEntry *t = NULL;
1131  int ret;
1132 
1133  if (!tags->pos) {
1135  if (ret < 0) return ret;
1136 
1137  *tags = start_ebml_master(s->pb, MATROSKA_ID_TAGS, 0);
1138  }
1139 
1140  tag = start_ebml_master(s->pb, MATROSKA_ID_TAG, 0);
1141  targets = start_ebml_master(s->pb, MATROSKA_ID_TAGTARGETS, 0);
1142  if (elementid)
1143  put_ebml_uint(s->pb, elementid, uid);
1144  end_ebml_master(s->pb, targets);
1145 
1146  while ((t = av_dict_get(m, "", t, AV_DICT_IGNORE_SUFFIX))) {
1147  if (av_strcasecmp(t->key, "title") &&
1148  av_strcasecmp(t->key, "stereo_mode") &&
1149  av_strcasecmp(t->key, "encoding_tool")) {
1150  ret = mkv_write_simpletag(s->pb, t);
1151  if (ret < 0)
1152  return ret;
1153  }
1154  }
1155 
1156  end_ebml_master(s->pb, tag);
1157  return 0;
1158 }
1159 
1161 {
1162  AVDictionaryEntry *t = NULL;
1163 
1164  while ((t = av_dict_get(m, "", t, AV_DICT_IGNORE_SUFFIX)))
1165  if (av_strcasecmp(t->key, "title") && av_strcasecmp(t->key, "stereo_mode"))
1166  return 1;
1167 
1168  return 0;
1169 }
1170 
1172 {
1173  MatroskaMuxContext *mkv = s->priv_data;
1174  ebml_master tags = {0};
1175  int i, ret;
1176 
1178 
1179  if (mkv_check_tag(s->metadata)) {
1180  ret = mkv_write_tag(s, s->metadata, 0, 0, &tags);
1181  if (ret < 0) return ret;
1182  }
1183 
1184  for (i = 0; i < s->nb_streams; i++) {
1185  AVStream *st = s->streams[i];
1186 
1187  if (!mkv_check_tag(st->metadata))
1188  continue;
1189 
1190  ret = mkv_write_tag(s, st->metadata, MATROSKA_ID_TAGTARGETS_TRACKUID, i + 1, &tags);
1191  if (ret < 0) return ret;
1192  }
1193 
1194  for (i = 0; i < s->nb_chapters; i++) {
1195  AVChapter *ch = s->chapters[i];
1196 
1197  if (!mkv_check_tag(ch->metadata))
1198  continue;
1199 
1201  if (ret < 0) return ret;
1202  }
1203 
1204  if (tags.pos)
1205  end_ebml_master(s->pb, tags);
1206  return 0;
1207 }
1208 
1210 {
1211  MatroskaMuxContext *mkv = s->priv_data;
1212  AVIOContext *pb = s->pb;
1213  ebml_master attachments;
1214  AVLFG c;
1215  int i, ret;
1216 
1217  if (!mkv->have_attachments)
1218  return 0;
1219 
1221 
1223  if (ret < 0) return ret;
1224 
1225  attachments = start_ebml_master(pb, MATROSKA_ID_ATTACHMENTS, 0);
1226 
1227  for (i = 0; i < s->nb_streams; i++) {
1228  AVStream *st = s->streams[i];
1229  ebml_master attached_file;
1230  AVDictionaryEntry *t;
1231  const char *mimetype = NULL;
1232  uint64_t fileuid;
1233 
1235  continue;
1236 
1237  attached_file = start_ebml_master(pb, MATROSKA_ID_ATTACHEDFILE, 0);
1238 
1239  if (t = av_dict_get(st->metadata, "title", NULL, 0))
1241  if (!(t = av_dict_get(st->metadata, "filename", NULL, 0))) {
1242  av_log(s, AV_LOG_ERROR, "Attachment stream %d has no filename tag.\n", i);
1243  return AVERROR(EINVAL);
1244  }
1246  if (t = av_dict_get(st->metadata, "mimetype", NULL, 0))
1247  mimetype = t->value;
1248  else if (st->codec->codec_id != AV_CODEC_ID_NONE ) {
1249  int i;
1250  for (i = 0; ff_mkv_mime_tags[i].id != AV_CODEC_ID_NONE; i++)
1251  if (ff_mkv_mime_tags[i].id == st->codec->codec_id) {
1252  mimetype = ff_mkv_mime_tags[i].str;
1253  break;
1254  }
1255  }
1256  if (!mimetype) {
1257  av_log(s, AV_LOG_ERROR, "Attachment stream %d has no mimetype tag and "
1258  "it cannot be deduced from the codec id.\n", i);
1259  return AVERROR(EINVAL);
1260  }
1261 
1262  if (s->flags & AVFMT_FLAG_BITEXACT) {
1263  struct AVSHA *sha = av_sha_alloc();
1264  uint8_t digest[20];
1265  if (!sha)
1266  return AVERROR(ENOMEM);
1267  av_sha_init(sha, 160);
1269  av_sha_final(sha, digest);
1270  av_free(sha);
1271  fileuid = AV_RL64(digest);
1272  } else {
1273  fileuid = av_lfg_get(&c);
1274  }
1275  av_log(s, AV_LOG_VERBOSE, "Using %.16"PRIx64" for attachment %d\n",
1276  fileuid, i);
1277 
1280  put_ebml_uint(pb, MATROSKA_ID_FILEUID, fileuid);
1281  end_ebml_master(pb, attached_file);
1282  }
1283  end_ebml_master(pb, attachments);
1284 
1285  return 0;
1286 }
1287 
1289 {
1290  MatroskaMuxContext *mkv = s->priv_data;
1291  AVIOContext *pb = s->pb;
1292  ebml_master ebml_header, segment_info;
1294  int ret, i, version = 2;
1295 
1296  if (!strcmp(s->oformat->name, "webm"))
1297  mkv->mode = MODE_WEBM;
1298  else
1299  mkv->mode = MODE_MATROSKAv2;
1300 
1301  if (s->avoid_negative_ts < 0)
1302  s->avoid_negative_ts = 1;
1303 
1304  if (mkv->mode != MODE_WEBM ||
1305  av_dict_get(s->metadata, "stereo_mode", NULL, 0) ||
1306  av_dict_get(s->metadata, "alpha_mode", NULL, 0))
1307  version = 4;
1308 
1309  for (i = 0; i < s->nb_streams; i++) {
1310  if (s->streams[i]->codec->codec_id == AV_CODEC_ID_ATRAC3 ||
1311  s->streams[i]->codec->codec_id == AV_CODEC_ID_COOK ||
1313  s->streams[i]->codec->codec_id == AV_CODEC_ID_SIPR ||
1314  s->streams[i]->codec->codec_id == AV_CODEC_ID_RV10 ||
1315  s->streams[i]->codec->codec_id == AV_CODEC_ID_RV20) {
1316  av_log(s, AV_LOG_ERROR,
1317  "The Matroska muxer does not yet support muxing %s\n",
1319  return AVERROR_PATCHWELCOME;
1320  }
1321  if (s->streams[i]->codec->codec_id == AV_CODEC_ID_OPUS ||
1322  av_dict_get(s->streams[i]->metadata, "stereo_mode", NULL, 0) ||
1323  av_dict_get(s->streams[i]->metadata, "alpha_mode", NULL, 0))
1324  version = 4;
1325  }
1326 
1327  mkv->tracks = av_mallocz_array(s->nb_streams, sizeof(*mkv->tracks));
1328  if (!mkv->tracks)
1329  return AVERROR(ENOMEM);
1330 
1331  ebml_header = start_ebml_master(pb, EBML_ID_HEADER, 0);
1337  put_ebml_uint (pb, EBML_ID_DOCTYPEVERSION , version);
1339  end_ebml_master(pb, ebml_header);
1340 
1342  mkv->segment_offset = avio_tell(pb);
1343 
1344  // we write 2 seek heads - one at the end of the file to point to each
1345  // cluster, and one at the beginning to point to all other level one
1346  // elements (including the seek head at the end of the file), which
1347  // isn't more than 10 elements if we only write one of each other
1348  // currently defined level 1 element
1349  mkv->main_seekhead = mkv_start_seekhead(pb, mkv->segment_offset, 10);
1350  if (!mkv->main_seekhead)
1351  return AVERROR(ENOMEM);
1352 
1354  if (ret < 0) return ret;
1355 
1356  segment_info = start_ebml_master(pb, MATROSKA_ID_INFO, 0);
1358  if ((tag = av_dict_get(s->metadata, "title", NULL, 0)))
1360  if (!(s->flags & AVFMT_FLAG_BITEXACT)) {
1361  uint32_t segment_uid[4];
1362  AVLFG lfg;
1363 
1365 
1366  for (i = 0; i < 4; i++)
1367  segment_uid[i] = av_lfg_get(&lfg);
1368 
1370  if ((tag = av_dict_get(s->metadata, "encoding_tool", NULL, 0)))
1372  else
1374  put_ebml_binary(pb, MATROSKA_ID_SEGMENTUID, segment_uid, 16);
1375  } else {
1376  const char *ident = "Lavf";
1379  }
1380 
1381  if (tag = av_dict_get(s->metadata, "creation_time", NULL, 0)) {
1382  // Adjust time so it's relative to 2001-01-01 and convert to nanoseconds.
1383  int64_t date_utc = (ff_iso8601_to_unix_time(tag->value) - 978307200) * 1000000000;
1384  uint8_t date_utc_buf[8];
1385  AV_WB64(date_utc_buf, date_utc);
1386  put_ebml_binary(pb, MATROSKA_ID_DATEUTC, date_utc_buf, 8);
1387  }
1388 
1389  // reserve space for the duration
1390  mkv->duration = 0;
1391  mkv->duration_offset = avio_tell(pb);
1392  put_ebml_void(pb, 11); // assumes double-precision float to be written
1393  end_ebml_master(pb, segment_info);
1394 
1395  ret = mkv_write_tracks(s);
1396  if (ret < 0)
1397  return ret;
1398 
1399  for (i = 0; i < s->nb_chapters; i++)
1400  mkv->chapter_id_offset = FFMAX(mkv->chapter_id_offset, 1LL - s->chapters[i]->id);
1401 
1402  if (mkv->mode != MODE_WEBM) {
1403  ret = mkv_write_chapters(s);
1404  if (ret < 0)
1405  return ret;
1406 
1407  ret = mkv_write_tags(s);
1408  if (ret < 0)
1409  return ret;
1410 
1411  ret = mkv_write_attachments(s);
1412  if (ret < 0)
1413  return ret;
1414  }
1415 
1416  if (!s->pb->seekable)
1418 
1419  mkv->cues = mkv_start_cues(mkv->segment_offset);
1420  if (!mkv->cues)
1421  return AVERROR(ENOMEM);
1422 
1423  if (pb->seekable && mkv->reserve_cues_space) {
1424  mkv->cues_pos = avio_tell(pb);
1426  }
1427 
1429  mkv->cur_audio_pkt.size = 0;
1430  mkv->cluster_pos = -1;
1431 
1432  avio_flush(pb);
1433 
1434  // start a new cluster every 5 MB or 5 sec, or 32k / 1 sec for streaming or
1435  // after 4k and on a keyframe
1436  if (pb->seekable) {
1437  if (mkv->cluster_time_limit < 0)
1438  mkv->cluster_time_limit = 5000;
1439  if (mkv->cluster_size_limit < 0)
1440  mkv->cluster_size_limit = 5 * 1024 * 1024;
1441  } else {
1442  if (mkv->cluster_time_limit < 0)
1443  mkv->cluster_time_limit = 1000;
1444  if (mkv->cluster_size_limit < 0)
1445  mkv->cluster_size_limit = 32 * 1024;
1446  }
1447 
1448  return 0;
1449 }
1450 
1451 static int mkv_blockgroup_size(int pkt_size)
1452 {
1453  int size = pkt_size + 4;
1454  size += ebml_num_size(size);
1455  size += 2; // EBML ID for block and block duration
1456  size += 8; // max size of block duration
1457  size += ebml_num_size(size);
1458  size += 1; // blockgroup EBML ID
1459  return size;
1460 }
1461 
1462 static int mkv_strip_wavpack(const uint8_t *src, uint8_t **pdst, int *size)
1463 {
1464  uint8_t *dst;
1465  int srclen = *size;
1466  int offset = 0;
1467  int ret;
1468 
1469  dst = av_malloc(srclen);
1470  if (!dst)
1471  return AVERROR(ENOMEM);
1472 
1473  while (srclen >= WV_HEADER_SIZE) {
1474  WvHeader header;
1475 
1476  ret = ff_wv_parse_header(&header, src);
1477  if (ret < 0)
1478  goto fail;
1479  src += WV_HEADER_SIZE;
1480  srclen -= WV_HEADER_SIZE;
1481 
1482  if (srclen < header.blocksize) {
1483  ret = AVERROR_INVALIDDATA;
1484  goto fail;
1485  }
1486 
1487  if (header.initial) {
1488  AV_WL32(dst + offset, header.samples);
1489  offset += 4;
1490  }
1491  AV_WL32(dst + offset, header.flags);
1492  AV_WL32(dst + offset + 4, header.crc);
1493  offset += 8;
1494 
1495  if (!(header.initial && header.final)) {
1496  AV_WL32(dst + offset, header.blocksize);
1497  offset += 4;
1498  }
1499 
1500  memcpy(dst + offset, src, header.blocksize);
1501  src += header.blocksize;
1502  srclen -= header.blocksize;
1503  offset += header.blocksize;
1504  }
1505 
1506  *pdst = dst;
1507  *size = offset;
1508 
1509  return 0;
1510 fail:
1511  av_freep(&dst);
1512  return ret;
1513 }
1514 
1516  unsigned int blockid, AVPacket *pkt, int flags)
1517 {
1518  MatroskaMuxContext *mkv = s->priv_data;
1519  AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
1520  uint8_t *data = NULL, *side_data = NULL;
1521  int offset = 0, size = pkt->size, side_data_size = 0;
1522  int64_t ts = mkv->tracks[pkt->stream_index].write_dts ? pkt->dts : pkt->pts;
1523  uint64_t additional_id = 0;
1524  int64_t discard_padding = 0;
1525  ebml_master block_group, block_additions, block_more;
1526 
1527  av_log(s, AV_LOG_DEBUG, "Writing block at offset %" PRIu64 ", size %d, "
1528  "pts %" PRId64 ", dts %" PRId64 ", duration %d, flags %d\n",
1529  avio_tell(pb), pkt->size, pkt->pts, pkt->dts, pkt->duration, flags);
1530  if (codec->codec_id == AV_CODEC_ID_H264 && codec->extradata_size > 0 &&
1531  (AV_RB24(codec->extradata) == 1 || AV_RB32(codec->extradata) == 1))
1532  ff_avc_parse_nal_units_buf(pkt->data, &data, &size);
1533  else if (codec->codec_id == AV_CODEC_ID_HEVC && codec->extradata_size > 6 &&
1534  (AV_RB24(codec->extradata) == 1 || AV_RB32(codec->extradata) == 1))
1535  /* extradata is Annex B, assume the bitstream is too and convert it */
1536  ff_hevc_annexb2mp4_buf(pkt->data, &data, &size, 0, NULL);
1537  else if (codec->codec_id == AV_CODEC_ID_WAVPACK) {
1538  int ret = mkv_strip_wavpack(pkt->data, &data, &size);
1539  if (ret < 0) {
1540  av_log(s, AV_LOG_ERROR, "Error stripping a WavPack packet.\n");
1541  return;
1542  }
1543  } else
1544  data = pkt->data;
1545 
1546  if (codec->codec_id == AV_CODEC_ID_PRORES) {
1547  /* Matroska specification requires to remove the first QuickTime atom
1548  */
1549  size -= 8;
1550  offset = 8;
1551  }
1552 
1553  side_data = av_packet_get_side_data(pkt,
1555  &side_data_size);
1556 
1557  if (side_data && side_data_size >= 10) {
1558  discard_padding = av_rescale_q(AV_RL32(side_data + 4),
1559  (AVRational){1, codec->sample_rate},
1560  (AVRational){1, 1000000000});
1561  }
1562 
1563  side_data = av_packet_get_side_data(pkt,
1565  &side_data_size);
1566  if (side_data) {
1567  additional_id = AV_RB64(side_data);
1568  side_data += 8;
1569  side_data_size -= 8;
1570  }
1571 
1572  if ((side_data_size && additional_id == 1) || discard_padding) {
1573  block_group = start_ebml_master(pb, MATROSKA_ID_BLOCKGROUP, 0);
1574  blockid = MATROSKA_ID_BLOCK;
1575  }
1576 
1577  put_ebml_id(pb, blockid);
1578  put_ebml_num(pb, size + 4, 0);
1579  // this assumes stream_index is less than 126
1580  avio_w8(pb, 0x80 | (mkv->is_dash ? mkv->dash_track_number : (pkt->stream_index + 1)));
1581  avio_wb16(pb, ts - mkv->cluster_pts);
1582  avio_w8(pb, flags);
1583  avio_write(pb, data + offset, size);
1584  if (data != pkt->data)
1585  av_free(data);
1586 
1587  if (discard_padding) {
1588  put_ebml_sint(pb, MATROSKA_ID_DISCARDPADDING, discard_padding);
1589  }
1590 
1591  if (side_data_size && additional_id == 1) {
1592  block_additions = start_ebml_master(pb, MATROSKA_ID_BLOCKADDITIONS, 0);
1593  block_more = start_ebml_master(pb, MATROSKA_ID_BLOCKMORE, 0);
1596  put_ebml_num(pb, side_data_size, 0);
1597  avio_write(pb, side_data, side_data_size);
1598  end_ebml_master(pb, block_more);
1599  end_ebml_master(pb, block_additions);
1600  }
1601  if ((side_data_size && additional_id == 1) || discard_padding) {
1602  end_ebml_master(pb, block_group);
1603  }
1604 }
1605 
1607 {
1608  MatroskaMuxContext *mkv = s->priv_data;
1609  ebml_master blockgroup;
1610  int id_size, settings_size, size;
1611  uint8_t *id, *settings;
1612  int64_t ts = mkv->tracks[pkt->stream_index].write_dts ? pkt->dts : pkt->pts;
1613  const int flags = 0;
1614 
1615  id_size = 0;
1617  &id_size);
1618 
1619  settings_size = 0;
1621  &settings_size);
1622 
1623  size = id_size + 1 + settings_size + 1 + pkt->size;
1624 
1625  av_log(s, AV_LOG_DEBUG, "Writing block at offset %" PRIu64 ", size %d, "
1626  "pts %" PRId64 ", dts %" PRId64 ", duration %d, flags %d\n",
1627  avio_tell(pb), size, pkt->pts, pkt->dts, pkt->duration, flags);
1628 
1630 
1632  put_ebml_num(pb, size + 4, 0);
1633  avio_w8(pb, 0x80 | (pkt->stream_index + 1)); // this assumes stream_index is less than 126
1634  avio_wb16(pb, ts - mkv->cluster_pts);
1635  avio_w8(pb, flags);
1636  avio_printf(pb, "%.*s\n%.*s\n%.*s", id_size, id, settings_size, settings, pkt->size, pkt->data);
1637 
1639  end_ebml_master(pb, blockgroup);
1640 
1641  return pkt->duration;
1642 }
1643 
1645 {
1646  MatroskaMuxContext *mkv = s->priv_data;
1647  int bufsize;
1648  uint8_t *dyn_buf;
1649 
1650  if (!mkv->dyn_bc)
1651  return;
1652 
1653  bufsize = avio_close_dyn_buf(mkv->dyn_bc, &dyn_buf);
1654  avio_write(s->pb, dyn_buf, bufsize);
1655  av_free(dyn_buf);
1656  mkv->dyn_bc = NULL;
1657 }
1658 
1660 {
1661  MatroskaMuxContext *mkv = s->priv_data;
1662  AVIOContext *pb;
1663 
1664  if (s->pb->seekable) {
1665  pb = s->pb;
1666  } else {
1667  pb = mkv->dyn_bc;
1668  }
1669 
1670  av_log(s, AV_LOG_DEBUG,
1671  "Starting new cluster at offset %" PRIu64 " bytes, "
1672  "pts %" PRIu64 "dts %" PRIu64 "\n",
1673  avio_tell(pb), pkt->pts, pkt->dts);
1674  end_ebml_master(pb, mkv->cluster);
1675  mkv->cluster_pos = -1;
1676  if (mkv->dyn_bc)
1677  mkv_flush_dynbuf(s);
1678  avio_flush(s->pb);
1679 }
1680 
1682 {
1683  MatroskaMuxContext *mkv = s->priv_data;
1684  AVIOContext *pb = s->pb;
1685  AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
1686  int keyframe = !!(pkt->flags & AV_PKT_FLAG_KEY);
1687  int duration = pkt->duration;
1688  int ret;
1689  int64_t ts = mkv->tracks[pkt->stream_index].write_dts ? pkt->dts : pkt->pts;
1690  int64_t relative_packet_pos;
1691  int dash_tracknum = mkv->is_dash ? mkv->dash_track_number : pkt->stream_index + 1;
1692 
1693  if (ts == AV_NOPTS_VALUE) {
1694  av_log(s, AV_LOG_ERROR, "Can't write packet with unknown timestamp\n");
1695  return AVERROR(EINVAL);
1696  }
1697  ts += mkv->tracks[pkt->stream_index].ts_offset;
1698 
1699  if (mkv->cluster_pos != -1) {
1700  int64_t cluster_time = ts - mkv->cluster_pts + mkv->tracks[pkt->stream_index].ts_offset;
1701  if ((int16_t)cluster_time != cluster_time) {
1702  av_log(s, AV_LOG_WARNING, "Starting new cluster due to timestamp\n");
1703  mkv_start_new_cluster(s, pkt);
1704  }
1705  }
1706 
1707  if (!s->pb->seekable) {
1708  if (!mkv->dyn_bc) {
1709  ret = avio_open_dyn_buf(&mkv->dyn_bc);
1710  if (ret < 0) {
1711  av_log(s, AV_LOG_ERROR, "Failed to open dynamic buffer\n");
1712  return ret;
1713  }
1714  }
1715  pb = mkv->dyn_bc;
1716  }
1717 
1718  if (mkv->cluster_pos == -1) {
1719  mkv->cluster_pos = avio_tell(s->pb);
1722  mkv->cluster_pts = FFMAX(0, ts);
1723  }
1724 
1725  relative_packet_pos = avio_tell(s->pb) - mkv->cluster.pos;
1726 
1727  if (codec->codec_type != AVMEDIA_TYPE_SUBTITLE) {
1728  mkv_write_block(s, pb, MATROSKA_ID_SIMPLEBLOCK, pkt, keyframe << 7);
1729  if (codec->codec_type == AVMEDIA_TYPE_VIDEO && keyframe || add_cue) {
1730  ret = mkv_add_cuepoint(mkv->cues, pkt->stream_index, dash_tracknum, ts, mkv->cluster_pos, relative_packet_pos, -1);
1731  if (ret < 0) return ret;
1732  }
1733  } else {
1734  if (codec->codec_id == AV_CODEC_ID_WEBVTT) {
1735  duration = mkv_write_vtt_blocks(s, pb, pkt);
1736  } else {
1738  mkv_blockgroup_size(pkt->size));
1739  /* For backward compatibility, prefer convergence_duration. */
1740  if (pkt->convergence_duration > 0) {
1741  duration = pkt->convergence_duration;
1742  }
1743  mkv_write_block(s, pb, MATROSKA_ID_BLOCK, pkt, 0);
1745  end_ebml_master(pb, blockgroup);
1746  }
1747 
1748  ret = mkv_add_cuepoint(mkv->cues, pkt->stream_index, dash_tracknum, ts,
1749  mkv->cluster_pos, relative_packet_pos, duration);
1750  if (ret < 0)
1751  return ret;
1752  }
1753 
1754  mkv->duration = FFMAX(mkv->duration, ts + duration);
1755  return 0;
1756 }
1757 
1759 {
1760  MatroskaMuxContext *mkv = s->priv_data;
1761  int codec_type = s->streams[pkt->stream_index]->codec->codec_type;
1762  int keyframe = !!(pkt->flags & AV_PKT_FLAG_KEY);
1763  int cluster_size;
1764  int64_t cluster_time;
1765  int ret;
1766  int start_new_cluster;
1767 
1768  if (mkv->tracks[pkt->stream_index].write_dts)
1769  cluster_time = pkt->dts - mkv->cluster_pts;
1770  else
1771  cluster_time = pkt->pts - mkv->cluster_pts;
1772  cluster_time += mkv->tracks[pkt->stream_index].ts_offset;
1773 
1774  // start a new cluster every 5 MB or 5 sec, or 32k / 1 sec for streaming or
1775  // after 4k and on a keyframe
1776  if (s->pb->seekable) {
1777  cluster_size = avio_tell(s->pb) - mkv->cluster_pos;
1778  } else {
1779  cluster_size = avio_tell(mkv->dyn_bc);
1780  }
1781 
1782  if (mkv->is_dash && codec_type == AVMEDIA_TYPE_VIDEO) {
1783  // WebM DASH specification states that the first block of every cluster
1784  // has to be a key frame. So for DASH video, we only create a cluster
1785  // on seeing key frames.
1786  start_new_cluster = keyframe;
1787  } else if (mkv->is_dash && codec_type == AVMEDIA_TYPE_AUDIO &&
1788  (mkv->cluster_pos == -1 ||
1789  cluster_time > mkv->cluster_time_limit)) {
1790  // For DASH audio, we create a Cluster based on cluster_time_limit
1791  start_new_cluster = 1;
1792  } else if (!mkv->is_dash &&
1793  (cluster_size > mkv->cluster_size_limit ||
1794  cluster_time > mkv->cluster_time_limit ||
1795  (codec_type == AVMEDIA_TYPE_VIDEO && keyframe &&
1796  cluster_size > 4 * 1024))) {
1797  start_new_cluster = 1;
1798  } else {
1799  start_new_cluster = 0;
1800  }
1801 
1802  if (mkv->cluster_pos != -1 && start_new_cluster) {
1803  mkv_start_new_cluster(s, pkt);
1804  }
1805 
1806  // check if we have an audio packet cached
1807  if (mkv->cur_audio_pkt.size > 0) {
1808  // for DASH audio, a CuePoint has to be added when there is a new cluster.
1810  mkv->is_dash ? start_new_cluster : 0);
1812  if (ret < 0) {
1813  av_log(s, AV_LOG_ERROR,
1814  "Could not write cached audio packet ret:%d\n", ret);
1815  return ret;
1816  }
1817  }
1818 
1819  // buffer an audio packet to ensure the packet containing the video
1820  // keyframe's timecode is contained in the same cluster for WebM
1821  if (codec_type == AVMEDIA_TYPE_AUDIO) {
1822  mkv->cur_audio_pkt = *pkt;
1823  if (pkt->buf) {
1824  mkv->cur_audio_pkt.buf = av_buffer_ref(pkt->buf);
1825  ret = mkv->cur_audio_pkt.buf ? 0 : AVERROR(ENOMEM);
1826  } else
1827  ret = av_dup_packet(&mkv->cur_audio_pkt);
1828  if (mkv->cur_audio_pkt.side_data_elems > 0) {
1830  }
1831  } else
1832  ret = mkv_write_packet_internal(s, pkt, 0);
1833  return ret;
1834 }
1835 
1837 {
1838  MatroskaMuxContext *mkv = s->priv_data;
1839  AVIOContext *pb;
1840  if (s->pb->seekable)
1841  pb = s->pb;
1842  else
1843  pb = mkv->dyn_bc;
1844  if (!pkt) {
1845  if (mkv->cluster_pos != -1) {
1846  av_log(s, AV_LOG_DEBUG,
1847  "Flushing cluster at offset %" PRIu64 " bytes\n",
1848  avio_tell(pb));
1849  end_ebml_master(pb, mkv->cluster);
1850  mkv->cluster_pos = -1;
1851  if (mkv->dyn_bc)
1852  mkv_flush_dynbuf(s);
1853  avio_flush(s->pb);
1854  }
1855  return 0;
1856  }
1857  return mkv_write_packet(s, pkt);
1858 }
1859 
1861 {
1862  MatroskaMuxContext *mkv = s->priv_data;
1863  AVIOContext *pb = s->pb;
1864  int64_t currentpos, cuespos;
1865  int ret;
1866 
1867  // check if we have an audio packet cached
1868  if (mkv->cur_audio_pkt.size > 0) {
1869  ret = mkv_write_packet_internal(s, &mkv->cur_audio_pkt, 0);
1871  if (ret < 0) {
1872  av_log(s, AV_LOG_ERROR,
1873  "Could not write cached audio packet ret:%d\n", ret);
1874  return ret;
1875  }
1876  }
1877 
1878  if (mkv->dyn_bc) {
1879  end_ebml_master(mkv->dyn_bc, mkv->cluster);
1880  mkv_flush_dynbuf(s);
1881  } else if (mkv->cluster_pos != -1) {
1882  end_ebml_master(pb, mkv->cluster);
1883  }
1884 
1885  if (mkv->mode != MODE_WEBM) {
1886  ret = mkv_write_chapters(s);
1887  if (ret < 0)
1888  return ret;
1889  }
1890 
1891  if (pb->seekable) {
1892  if (mkv->cues->num_entries) {
1893  if (mkv->reserve_cues_space) {
1894  int64_t cues_end;
1895 
1896  currentpos = avio_tell(pb);
1897  avio_seek(pb, mkv->cues_pos, SEEK_SET);
1898 
1899  cuespos = mkv_write_cues(s, mkv->cues, mkv->tracks, s->nb_streams);
1900  cues_end = avio_tell(pb);
1901  if (cues_end > cuespos + mkv->reserve_cues_space) {
1902  av_log(s, AV_LOG_ERROR,
1903  "Insufficient space reserved for cues: %d "
1904  "(needed: %" PRId64 ").\n",
1905  mkv->reserve_cues_space, cues_end - cuespos);
1906  return AVERROR(EINVAL);
1907  }
1908 
1909  if (cues_end < cuespos + mkv->reserve_cues_space)
1911  (cues_end - cuespos));
1912 
1913  avio_seek(pb, currentpos, SEEK_SET);
1914  } else {
1915  cuespos = mkv_write_cues(s, mkv->cues, mkv->tracks, s->nb_streams);
1916  }
1917 
1919  cuespos);
1920  if (ret < 0)
1921  return ret;
1922  }
1923 
1925 
1926  // update the duration
1927  av_log(s, AV_LOG_DEBUG, "end duration = %" PRIu64 "\n", mkv->duration);
1928  currentpos = avio_tell(pb);
1929  avio_seek(pb, mkv->duration_offset, SEEK_SET);
1931 
1932  avio_seek(pb, currentpos, SEEK_SET);
1933  }
1934 
1935  end_ebml_master(pb, mkv->segment);
1936  av_freep(&mkv->tracks);
1937  av_freep(&mkv->cues->entries);
1938  av_freep(&mkv->cues);
1939 
1940  return 0;
1941 }
1942 
1943 static int mkv_query_codec(enum AVCodecID codec_id, int std_compliance)
1944 {
1945  int i;
1946  for (i = 0; ff_mkv_codec_tags[i].id != AV_CODEC_ID_NONE; i++)
1947  if (ff_mkv_codec_tags[i].id == codec_id)
1948  return 1;
1949 
1950  if (std_compliance < FF_COMPLIANCE_NORMAL) {
1951  enum AVMediaType type = avcodec_get_type(codec_id);
1952  // mkv theoretically supports any video/audio through VFW/ACM
1953  if (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO)
1954  return 1;
1955  }
1956 
1957  return 0;
1958 }
1959 
1961  { AV_CODEC_ID_ALAC, 0XFFFFFFFF },
1962  { AV_CODEC_ID_EAC3, 0XFFFFFFFF },
1963  { AV_CODEC_ID_MLP, 0xFFFFFFFF },
1964  { AV_CODEC_ID_OPUS, 0xFFFFFFFF },
1965  { AV_CODEC_ID_PCM_S16BE, 0xFFFFFFFF },
1966  { AV_CODEC_ID_PCM_S24BE, 0xFFFFFFFF },
1967  { AV_CODEC_ID_PCM_S32BE, 0xFFFFFFFF },
1968  { AV_CODEC_ID_QDM2, 0xFFFFFFFF },
1969  { AV_CODEC_ID_RA_144, 0xFFFFFFFF },
1970  { AV_CODEC_ID_RA_288, 0xFFFFFFFF },
1971  { AV_CODEC_ID_COOK, 0xFFFFFFFF },
1972  { AV_CODEC_ID_TRUEHD, 0xFFFFFFFF },
1973  { AV_CODEC_ID_NONE, 0xFFFFFFFF }
1974 };
1975 
1977  { AV_CODEC_ID_RV10, 0xFFFFFFFF },
1978  { AV_CODEC_ID_RV20, 0xFFFFFFFF },
1979  { AV_CODEC_ID_RV30, 0xFFFFFFFF },
1980  { AV_CODEC_ID_RV40, 0xFFFFFFFF },
1981  { AV_CODEC_ID_VP9, 0xFFFFFFFF },
1982  { AV_CODEC_ID_NONE, 0xFFFFFFFF }
1983 };
1984 
1985 #define OFFSET(x) offsetof(MatroskaMuxContext, x)
1986 #define FLAGS AV_OPT_FLAG_ENCODING_PARAM
1987 static const AVOption options[] = {
1988  { "reserve_index_space", "Reserve a given amount of space (in bytes) at the beginning of the file for the index (cues).", OFFSET(reserve_cues_space), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
1989  { "cluster_size_limit", "Store at most the provided amount of bytes in a cluster. ", OFFSET(cluster_size_limit), AV_OPT_TYPE_INT , { .i64 = -1 }, -1, INT_MAX, FLAGS },
1990  { "cluster_time_limit", "Store at most the provided number of milliseconds in a cluster.", OFFSET(cluster_time_limit), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT64_MAX, FLAGS },
1991  { "dash", "Create a WebM file conforming to WebM DASH specification", OFFSET(is_dash), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
1992  { "dash_track_number", "Track number for the DASH stream", OFFSET(dash_track_number), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 127, FLAGS },
1993  { NULL },
1994 };
1995 
1996 #if CONFIG_MATROSKA_MUXER
1997 static const AVClass matroska_class = {
1998  .class_name = "matroska muxer",
1999  .item_name = av_default_item_name,
2000  .option = options,
2001  .version = LIBAVUTIL_VERSION_INT,
2002 };
2003 
2004 AVOutputFormat ff_matroska_muxer = {
2005  .name = "matroska",
2006  .long_name = NULL_IF_CONFIG_SMALL("Matroska"),
2007  .mime_type = "video/x-matroska",
2008  .extensions = "mkv",
2009  .priv_data_size = sizeof(MatroskaMuxContext),
2010  .audio_codec = CONFIG_LIBVORBIS_ENCODER ?
2012  .video_codec = CONFIG_LIBX264_ENCODER ?
2019  .codec_tag = (const AVCodecTag* const []){
2022  },
2023  .subtitle_codec = AV_CODEC_ID_ASS,
2024  .query_codec = mkv_query_codec,
2025  .priv_class = &matroska_class,
2026 };
2027 #endif
2028 
2029 #if CONFIG_WEBM_MUXER
2030 static const AVClass webm_class = {
2031  .class_name = "webm muxer",
2032  .item_name = av_default_item_name,
2033  .option = options,
2034  .version = LIBAVUTIL_VERSION_INT,
2035 };
2036 
2037 AVOutputFormat ff_webm_muxer = {
2038  .name = "webm",
2039  .long_name = NULL_IF_CONFIG_SMALL("WebM"),
2040  .mime_type = "video/webm",
2041  .extensions = "webm",
2042  .priv_data_size = sizeof(MatroskaMuxContext),
2043  .audio_codec = AV_CODEC_ID_VORBIS,
2044  .video_codec = AV_CODEC_ID_VP8,
2045  .subtitle_codec = AV_CODEC_ID_WEBVTT,
2051  .priv_class = &webm_class,
2052 };
2053 #endif
2054 
2055 #if CONFIG_MATROSKA_AUDIO_MUXER
2056 static const AVClass mka_class = {
2057  .class_name = "matroska audio muxer",
2058  .item_name = av_default_item_name,
2059  .option = options,
2060  .version = LIBAVUTIL_VERSION_INT,
2061 };
2062 AVOutputFormat ff_matroska_audio_muxer = {
2063  .name = "matroska",
2064  .long_name = NULL_IF_CONFIG_SMALL("Matroska Audio"),
2065  .mime_type = "audio/x-matroska",
2066  .extensions = "mka",
2067  .priv_data_size = sizeof(MatroskaMuxContext),
2068  .audio_codec = CONFIG_LIBVORBIS_ENCODER ?
2069  AV_CODEC_ID_VORBIS : AV_CODEC_ID_AC3,
2070  .video_codec = AV_CODEC_ID_NONE,
2076  .codec_tag = (const AVCodecTag* const []){
2078  },
2079  .priv_class = &mka_class,
2080 };
2081 #endif