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) {
581  "Error parsing AAC extradata, unable to determine samplerate.\n");
582  return;
583  }
584 
585  *sample_rate = mp4ac.sample_rate;
586  *output_sample_rate = mp4ac.ext_sample_rate;
587 }
588 
590  AVCodecContext *codec,
591  AVIOContext *dyn_cp)
592 {
593  switch (codec->codec_id) {
594  case AV_CODEC_ID_VORBIS:
595  case AV_CODEC_ID_THEORA:
596  return put_xiph_codecpriv(s, dyn_cp, codec);
597  case AV_CODEC_ID_FLAC:
598  return put_flac_codecpriv(s, dyn_cp, codec);
599  case AV_CODEC_ID_WAVPACK:
600  return put_wv_codecpriv(dyn_cp, codec);
601  case AV_CODEC_ID_H264:
602  return ff_isom_write_avcc(dyn_cp, codec->extradata,
603  codec->extradata_size);
604  case AV_CODEC_ID_HEVC:
605  return ff_isom_write_hvcc(dyn_cp, codec->extradata,
606  codec->extradata_size, 0);
607  case AV_CODEC_ID_ALAC:
608  if (codec->extradata_size < 36) {
609  av_log(s, AV_LOG_ERROR,
610  "Invalid extradata found, ALAC expects a 36-byte "
611  "QuickTime atom.");
612  return AVERROR_INVALIDDATA;
613  } else
614  avio_write(dyn_cp, codec->extradata + 12,
615  codec->extradata_size - 12);
616  break;
617  default:
618  if (codec->codec_id == AV_CODEC_ID_PRORES &&
620  avio_wl32(dyn_cp, codec->codec_tag);
621  } else if (codec->extradata_size && codec->codec_id != AV_CODEC_ID_TTA)
622  avio_write(dyn_cp, codec->extradata, codec->extradata_size);
623  }
624 
625  return 0;
626 }
627 
629  AVCodecContext *codec, int native_id,
630  int qt_id)
631 {
632  AVIOContext *dyn_cp;
633  uint8_t *codecpriv;
634  int ret, codecpriv_size;
635 
636  ret = avio_open_dyn_buf(&dyn_cp);
637  if (ret < 0)
638  return ret;
639 
640  if (native_id) {
641  ret = mkv_write_native_codecprivate(s, codec, dyn_cp);
642  } else if (codec->codec_type == AVMEDIA_TYPE_VIDEO) {
643  if (qt_id) {
644  if (!codec->codec_tag)
646  codec->codec_id);
647  if (codec->extradata_size) {
650  ) {
651  int i;
652  avio_wb32(dyn_cp, 0x5a + codec->extradata_size);
653  avio_wl32(dyn_cp, codec->codec_tag);
654  for(i = 0; i < 0x5a - 8; i++)
655  avio_w8(dyn_cp, 0);
656  }
657  avio_write(dyn_cp, codec->extradata, codec->extradata_size);
658  }
659  } else {
661  av_log(s, AV_LOG_WARNING, "codec %s is not supported by this format\n",
662  avcodec_get_name(codec->codec_id));
663 
664  if (!codec->codec_tag)
666  codec->codec_id);
667  if (!codec->codec_tag) {
668  av_log(s, AV_LOG_ERROR, "No bmp codec tag found for codec %s\n",
669  avcodec_get_name(codec->codec_id));
670  ret = AVERROR(EINVAL);
671  }
672 
673  ff_put_bmp_header(dyn_cp, codec, ff_codec_bmp_tags, 0, 0);
674  }
675  } else if (codec->codec_type == AVMEDIA_TYPE_AUDIO) {
676  unsigned int tag;
678  if (!tag) {
679  av_log(s, AV_LOG_ERROR, "No wav codec tag found for codec %s\n",
680  avcodec_get_name(codec->codec_id));
681  ret = AVERROR(EINVAL);
682  }
683  if (!codec->codec_tag)
684  codec->codec_tag = tag;
685 
687  }
688 
689  codecpriv_size = avio_close_dyn_buf(dyn_cp, &codecpriv);
690  if (codecpriv_size)
692  codecpriv_size);
693  av_free(codecpriv);
694  return ret;
695 }
696 
697 
699  AVStream *st, int mode, int *h_width, int *h_height)
700 {
701  int i;
702  int ret = 0;
705 
706  *h_width = 1;
707  *h_height = 1;
708  // convert metadata into proper side data and add it to the stream
709  if ((tag = av_dict_get(st->metadata, "stereo_mode", NULL, 0)) ||
710  (tag = av_dict_get( s->metadata, "stereo_mode", NULL, 0))) {
711  int stereo_mode = atoi(tag->value);
712 
713  for (i=0; i<MATROSKA_VIDEO_STEREOMODE_TYPE_NB; i++)
714  if (!strcmp(tag->value, ff_matroska_video_stereo_mode[i])){
715  stereo_mode = i;
716  break;
717  }
718 
719  if (stereo_mode < MATROSKA_VIDEO_STEREOMODE_TYPE_NB &&
720  stereo_mode != 10 && stereo_mode != 12) {
721  int ret = ff_mkv_stereo3d_conv(st, stereo_mode);
722  if (ret < 0)
723  return ret;
724  }
725  }
726 
727  // iterate to find the stereo3d side data
728  for (i = 0; i < st->nb_side_data; i++) {
729  AVPacketSideData sd = st->side_data[i];
730  if (sd.type == AV_PKT_DATA_STEREO3D) {
731  AVStereo3D *stereo = (AVStereo3D *)sd.data;
732 
733  switch (stereo->type) {
734  case AV_STEREO3D_2D:
736  break;
738  format = (stereo->flags & AV_STEREO3D_FLAG_INVERT)
741  *h_width = 2;
742  break;
745  if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
746  format--;
747  *h_height = 2;
748  break;
751  if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
752  format--;
753  break;
754  case AV_STEREO3D_LINES:
756  if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
757  format--;
758  *h_height = 2;
759  break;
760  case AV_STEREO3D_COLUMNS:
762  if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
763  format--;
764  *h_width = 2;
765  break;
768  if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
769  format++;
770  break;
771  }
772  break;
773  }
774  }
775 
776  if (format == MATROSKA_VIDEO_STEREOMODE_TYPE_NB)
777  return ret;
778 
779  // if webm, do not write unsupported modes
780  if ((mode == MODE_WEBM &&
783  || format >= MATROSKA_VIDEO_STEREOMODE_TYPE_NB) {
784  av_log(s, AV_LOG_ERROR,
785  "The specified stereo mode is not valid.\n");
787  return AVERROR(EINVAL);
788  }
789 
790  // write StereoMode if format is valid
792 
793  return ret;
794 }
795 
797  int i, AVIOContext *pb, int default_stream_exists)
798 {
799  AVStream *st = s->streams[i];
800  AVCodecContext *codec = st->codec;
801  ebml_master subinfo, track;
802  int native_id = 0;
803  int qt_id = 0;
804  int bit_depth = av_get_bits_per_sample(codec->codec_id);
805  int sample_rate = codec->sample_rate;
806  int output_sample_rate = 0;
807  int display_width_div = 1;
808  int display_height_div = 1;
809  int j, ret;
811 
812  // ms precision is the de-facto standard timescale for mkv files
813  avpriv_set_pts_info(st, 64, 1, 1000);
814 
815  if (codec->codec_type == AVMEDIA_TYPE_ATTACHMENT) {
816  mkv->have_attachments = 1;
817  return 0;
818  }
819 
820  if (!bit_depth && codec->codec_id != AV_CODEC_ID_ADPCM_G726)
821  bit_depth = av_get_bytes_per_sample(codec->sample_fmt) << 3;
822  if (!bit_depth)
823  bit_depth = codec->bits_per_coded_sample;
824 
825  if (codec->codec_id == AV_CODEC_ID_AAC)
826  get_aac_sample_rates(s, codec, &sample_rate, &output_sample_rate);
827 
830  mkv->is_dash ? mkv->dash_track_number : i + 1);
832  mkv->is_dash ? mkv->dash_track_number : i + 1);
833  put_ebml_uint (pb, MATROSKA_ID_TRACKFLAGLACING , 0); // no lacing (yet)
834 
835  if ((tag = av_dict_get(st->metadata, "title", NULL, 0)))
837  tag = av_dict_get(st->metadata, "language", NULL, 0);
838  if (mkv->mode != MODE_WEBM || codec->codec_id != AV_CODEC_ID_WEBVTT) {
839  put_ebml_string(pb, MATROSKA_ID_TRACKLANGUAGE, tag && tag->value ? tag->value:"und");
840  } else if (tag && tag->value) {
842  }
843 
844  // The default value for TRACKFLAGDEFAULT is 1, so add element
845  // if we need to clear it.
846  if (default_stream_exists && !(st->disposition & AV_DISPOSITION_DEFAULT))
848 
851 
852  if (mkv->mode == MODE_WEBM && codec->codec_id == AV_CODEC_ID_WEBVTT) {
853  const char *codec_id;
855  codec_id = "D_WEBVTT/CAPTIONS";
856  native_id = MATROSKA_TRACK_TYPE_SUBTITLE;
857  } else if (st->disposition & AV_DISPOSITION_DESCRIPTIONS) {
858  codec_id = "D_WEBVTT/DESCRIPTIONS";
859  native_id = MATROSKA_TRACK_TYPE_METADATA;
860  } else if (st->disposition & AV_DISPOSITION_METADATA) {
861  codec_id = "D_WEBVTT/METADATA";
862  native_id = MATROSKA_TRACK_TYPE_METADATA;
863  } else {
864  codec_id = "D_WEBVTT/SUBTITLES";
865  native_id = MATROSKA_TRACK_TYPE_SUBTITLE;
866  }
867  put_ebml_string(pb, MATROSKA_ID_CODECID, codec_id);
868  } else {
869  // look for a codec ID string specific to mkv to use,
870  // if none are found, use AVI codes
871  for (j = 0; ff_mkv_codec_tags[j].id != AV_CODEC_ID_NONE; j++) {
872  if (ff_mkv_codec_tags[j].id == codec->codec_id) {
874  native_id = 1;
875  break;
876  }
877  }
878  }
879 
880  if (codec->codec_type == AVMEDIA_TYPE_AUDIO && codec->initial_padding && codec->codec_id == AV_CODEC_ID_OPUS) {
881 // mkv->tracks[i].ts_offset = av_rescale_q(codec->initial_padding,
882 // (AVRational){ 1, codec->sample_rate },
883 // st->time_base);
884 
887  (AVRational){ 1, codec->sample_rate },
888  (AVRational){ 1, 1000000000 }));
889  }
890  if (codec->codec_id == AV_CODEC_ID_OPUS) {
892  }
893 
894  if (mkv->mode == MODE_WEBM && !(codec->codec_id == AV_CODEC_ID_VP8 ||
895  codec->codec_id == AV_CODEC_ID_VP9 ||
896  codec->codec_id == AV_CODEC_ID_OPUS ||
897  codec->codec_id == AV_CODEC_ID_VORBIS ||
898  codec->codec_id == AV_CODEC_ID_WEBVTT)) {
900  "Only VP8 or VP9 video and Vorbis or Opus audio and WebVTT subtitles are supported for WebM.\n");
901  return AVERROR(EINVAL);
902  }
903 
904  switch (codec->codec_type) {
905  case AVMEDIA_TYPE_VIDEO:
907 
908  if( st->avg_frame_rate.num > 0 && st->avg_frame_rate.den > 0
909  && 1.0/av_q2d(st->avg_frame_rate) > av_q2d(codec->time_base))
910  put_ebml_uint(pb, MATROSKA_ID_TRACKDEFAULTDURATION, 1E9 / av_q2d(st->avg_frame_rate));
911  else
912  put_ebml_uint(pb, MATROSKA_ID_TRACKDEFAULTDURATION, av_q2d(codec->time_base)*1E9);
913 
914  if (!native_id &&
915  ff_codec_get_tag(ff_codec_movvideo_tags, codec->codec_id) &&
916  (!ff_codec_get_tag(ff_codec_bmp_tags, codec->codec_id) ||
917  codec->codec_id == AV_CODEC_ID_SVQ1 ||
918  codec->codec_id == AV_CODEC_ID_SVQ3 ||
919  codec->codec_id == AV_CODEC_ID_CINEPAK))
920  qt_id = 1;
921 
922  if (qt_id)
923  put_ebml_string(pb, MATROSKA_ID_CODECID, "V_QUICKTIME");
924  else if (!native_id) {
925  // if there is no mkv-specific codec ID, use VFW mode
926  put_ebml_string(pb, MATROSKA_ID_CODECID, "V_MS/VFW/FOURCC");
927  mkv->tracks[i].write_dts = 1;
928  }
929 
930  subinfo = start_ebml_master(pb, MATROSKA_ID_TRACKVIDEO, 0);
931  // XXX: interlace flag?
932  put_ebml_uint (pb, MATROSKA_ID_VIDEOPIXELWIDTH , codec->width);
933  put_ebml_uint (pb, MATROSKA_ID_VIDEOPIXELHEIGHT, codec->height);
934 
935  // check both side data and metadata for stereo information,
936  // write the result to the bitstream if any is found
937  ret = mkv_write_stereo_mode(s, pb, st, mkv->mode,
938  &display_width_div,
939  &display_height_div);
940  if (ret < 0)
941  return ret;
942 
943  if (((tag = av_dict_get(st->metadata, "alpha_mode", NULL, 0)) && atoi(tag->value)) ||
944  ((tag = av_dict_get( s->metadata, "alpha_mode", NULL, 0)) && atoi(tag->value)) ||
945  (codec->pix_fmt == AV_PIX_FMT_YUVA420P)) {
947  }
948 
949  // write DisplayWidth and DisplayHeight, they contain the size of
950  // a single source view and/or the display aspect ratio
951  if (st->sample_aspect_ratio.num) {
952  int64_t d_width = av_rescale(codec->width, st->sample_aspect_ratio.num, st->sample_aspect_ratio.den);
953  if (d_width > INT_MAX) {
954  av_log(s, AV_LOG_ERROR, "Overflow in display width\n");
955  return AVERROR(EINVAL);
956  }
957  put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYWIDTH , d_width / display_width_div);
958  put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYHEIGHT, codec->height / display_height_div);
959  } else if (display_width_div != 1 || display_height_div != 1) {
960  put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYWIDTH , codec->width / display_width_div);
961  put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYHEIGHT, codec->height / display_height_div);
962  }
963 
964  if (codec->codec_id == AV_CODEC_ID_RAWVIDEO) {
965  uint32_t color_space = av_le2ne32(codec->codec_tag);
966  put_ebml_binary(pb, MATROSKA_ID_VIDEOCOLORSPACE, &color_space, sizeof(color_space));
967  }
968 
969  end_ebml_master(pb, subinfo);
970  break;
971 
972  case AVMEDIA_TYPE_AUDIO:
974 
975  if (!native_id)
976  // no mkv-specific ID, use ACM mode
977  put_ebml_string(pb, MATROSKA_ID_CODECID, "A_MS/ACM");
978 
979  subinfo = start_ebml_master(pb, MATROSKA_ID_TRACKAUDIO, 0);
980  put_ebml_uint (pb, MATROSKA_ID_AUDIOCHANNELS , codec->channels);
982  if (output_sample_rate)
983  put_ebml_float(pb, MATROSKA_ID_AUDIOOUTSAMPLINGFREQ, output_sample_rate);
984  if (bit_depth)
986  end_ebml_master(pb, subinfo);
987  break;
988 
990  if (!native_id) {
991  av_log(s, AV_LOG_ERROR, "Subtitle codec %d is not supported.\n", codec->codec_id);
992  return AVERROR(ENOSYS);
993  }
994 
995  if (mkv->mode != MODE_WEBM || codec->codec_id != AV_CODEC_ID_WEBVTT)
996  native_id = MATROSKA_TRACK_TYPE_SUBTITLE;
997 
998  put_ebml_uint(pb, MATROSKA_ID_TRACKTYPE, native_id);
999  break;
1000  default:
1001  av_log(s, AV_LOG_ERROR, "Only audio, video, and subtitles are supported for Matroska.\n");
1002  return AVERROR(EINVAL);
1003  }
1004 
1005  if (mkv->mode != MODE_WEBM || codec->codec_id != AV_CODEC_ID_WEBVTT) {
1006  ret = mkv_write_codecprivate(s, pb, codec, native_id, qt_id);
1007  if (ret < 0)
1008  return ret;
1009  }
1010 
1011  end_ebml_master(pb, track);
1012 
1013  return 0;
1014 }
1015 
1017 {
1018  MatroskaMuxContext *mkv = s->priv_data;
1019  AVIOContext *pb = s->pb;
1020  ebml_master tracks;
1021  int i, ret, default_stream_exists = 0;
1022 
1024  if (ret < 0)
1025  return ret;
1026 
1027  tracks = start_ebml_master(pb, MATROSKA_ID_TRACKS, 0);
1028  for (i = 0; i < s->nb_streams; i++) {
1029  AVStream *st = s->streams[i];
1030  default_stream_exists |= st->disposition & AV_DISPOSITION_DEFAULT;
1031  }
1032  for (i = 0; i < s->nb_streams; i++) {
1033  ret = mkv_write_track(s, mkv, i, pb, default_stream_exists);
1034  if (ret < 0)
1035  return ret;
1036  }
1037  end_ebml_master(pb, tracks);
1038  return 0;
1039 }
1040 
1042 {
1043  MatroskaMuxContext *mkv = s->priv_data;
1044  AVIOContext *pb = s->pb;
1045  ebml_master chapters, editionentry;
1046  AVRational scale = {1, 1E9};
1047  int i, ret;
1048 
1049  if (!s->nb_chapters || mkv->wrote_chapters)
1050  return 0;
1051 
1053  if (ret < 0) return ret;
1054 
1055  chapters = start_ebml_master(pb, MATROSKA_ID_CHAPTERS , 0);
1056  editionentry = start_ebml_master(pb, MATROSKA_ID_EDITIONENTRY, 0);
1059  for (i = 0; i < s->nb_chapters; i++) {
1060  ebml_master chapteratom, chapterdisplay;
1061  AVChapter *c = s->chapters[i];
1062  AVDictionaryEntry *t = NULL;
1063 
1064  chapteratom = start_ebml_master(pb, MATROSKA_ID_CHAPTERATOM, 0);
1067  av_rescale_q(c->start, c->time_base, scale));
1069  av_rescale_q(c->end, c->time_base, scale));
1072  if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
1073  chapterdisplay = start_ebml_master(pb, MATROSKA_ID_CHAPTERDISPLAY, 0);
1076  end_ebml_master(pb, chapterdisplay);
1077  }
1078  end_ebml_master(pb, chapteratom);
1079  }
1080  end_ebml_master(pb, editionentry);
1081  end_ebml_master(pb, chapters);
1082 
1083  mkv->wrote_chapters = 1;
1084  return 0;
1085 }
1086 
1088 {
1089  uint8_t *key = av_strdup(t->key);
1090  uint8_t *p = key;
1091  const uint8_t *lang = NULL;
1092  ebml_master tag;
1093 
1094  if (!key)
1095  return AVERROR(ENOMEM);
1096 
1097  if ((p = strrchr(p, '-')) &&
1098  (lang = av_convert_lang_to(p + 1, AV_LANG_ISO639_2_BIBL)))
1099  *p = 0;
1100 
1101  p = key;
1102  while (*p) {
1103  if (*p == ' ')
1104  *p = '_';
1105  else if (*p >= 'a' && *p <= 'z')
1106  *p -= 'a' - 'A';
1107  p++;
1108  }
1109 
1112  if (lang)
1115  end_ebml_master(pb, tag);
1116 
1117  av_freep(&key);
1118  return 0;
1119 }
1120 
1121 static int mkv_write_tag(AVFormatContext *s, AVDictionary *m, unsigned int elementid,
1122  unsigned int uid, ebml_master *tags)
1123 {
1124  MatroskaMuxContext *mkv = s->priv_data;
1125  ebml_master tag, targets;
1126  AVDictionaryEntry *t = NULL;
1127  int ret;
1128 
1129  if (!tags->pos) {
1131  if (ret < 0) return ret;
1132 
1133  *tags = start_ebml_master(s->pb, MATROSKA_ID_TAGS, 0);
1134  }
1135 
1136  tag = start_ebml_master(s->pb, MATROSKA_ID_TAG, 0);
1137  targets = start_ebml_master(s->pb, MATROSKA_ID_TAGTARGETS, 0);
1138  if (elementid)
1139  put_ebml_uint(s->pb, elementid, uid);
1140  end_ebml_master(s->pb, targets);
1141 
1142  while ((t = av_dict_get(m, "", t, AV_DICT_IGNORE_SUFFIX))) {
1143  if (av_strcasecmp(t->key, "title") &&
1144  av_strcasecmp(t->key, "stereo_mode") &&
1145  av_strcasecmp(t->key, "encoding_tool")) {
1146  ret = mkv_write_simpletag(s->pb, t);
1147  if (ret < 0)
1148  return ret;
1149  }
1150  }
1151 
1152  end_ebml_master(s->pb, tag);
1153  return 0;
1154 }
1155 
1157 {
1158  AVDictionaryEntry *t = NULL;
1159 
1160  while ((t = av_dict_get(m, "", t, AV_DICT_IGNORE_SUFFIX)))
1161  if (av_strcasecmp(t->key, "title") && av_strcasecmp(t->key, "stereo_mode"))
1162  return 1;
1163 
1164  return 0;
1165 }
1166 
1168 {
1169  MatroskaMuxContext *mkv = s->priv_data;
1170  ebml_master tags = {0};
1171  int i, ret;
1172 
1174 
1175  if (mkv_check_tag(s->metadata)) {
1176  ret = mkv_write_tag(s, s->metadata, 0, 0, &tags);
1177  if (ret < 0) return ret;
1178  }
1179 
1180  for (i = 0; i < s->nb_streams; i++) {
1181  AVStream *st = s->streams[i];
1182 
1183  if (!mkv_check_tag(st->metadata))
1184  continue;
1185 
1186  ret = mkv_write_tag(s, st->metadata, MATROSKA_ID_TAGTARGETS_TRACKUID, i + 1, &tags);
1187  if (ret < 0) return ret;
1188  }
1189 
1190  for (i = 0; i < s->nb_chapters; i++) {
1191  AVChapter *ch = s->chapters[i];
1192 
1193  if (!mkv_check_tag(ch->metadata))
1194  continue;
1195 
1197  if (ret < 0) return ret;
1198  }
1199 
1200  if (tags.pos)
1201  end_ebml_master(s->pb, tags);
1202  return 0;
1203 }
1204 
1206 {
1207  MatroskaMuxContext *mkv = s->priv_data;
1208  AVIOContext *pb = s->pb;
1209  ebml_master attachments;
1210  AVLFG c;
1211  int i, ret;
1212 
1213  if (!mkv->have_attachments)
1214  return 0;
1215 
1217 
1219  if (ret < 0) return ret;
1220 
1221  attachments = start_ebml_master(pb, MATROSKA_ID_ATTACHMENTS, 0);
1222 
1223  for (i = 0; i < s->nb_streams; i++) {
1224  AVStream *st = s->streams[i];
1225  ebml_master attached_file;
1226  AVDictionaryEntry *t;
1227  const char *mimetype = NULL;
1228  uint64_t fileuid;
1229 
1231  continue;
1232 
1233  attached_file = start_ebml_master(pb, MATROSKA_ID_ATTACHEDFILE, 0);
1234 
1235  if (t = av_dict_get(st->metadata, "title", NULL, 0))
1237  if (!(t = av_dict_get(st->metadata, "filename", NULL, 0))) {
1238  av_log(s, AV_LOG_ERROR, "Attachment stream %d has no filename tag.\n", i);
1239  return AVERROR(EINVAL);
1240  }
1242  if (t = av_dict_get(st->metadata, "mimetype", NULL, 0))
1243  mimetype = t->value;
1244  else if (st->codec->codec_id != AV_CODEC_ID_NONE ) {
1245  int i;
1246  for (i = 0; ff_mkv_mime_tags[i].id != AV_CODEC_ID_NONE; i++)
1247  if (ff_mkv_mime_tags[i].id == st->codec->codec_id) {
1248  mimetype = ff_mkv_mime_tags[i].str;
1249  break;
1250  }
1251  }
1252  if (!mimetype) {
1253  av_log(s, AV_LOG_ERROR, "Attachment stream %d has no mimetype tag and "
1254  "it cannot be deduced from the codec id.\n", i);
1255  return AVERROR(EINVAL);
1256  }
1257 
1258  if (s->flags & AVFMT_FLAG_BITEXACT) {
1259  struct AVSHA *sha = av_sha_alloc();
1260  uint8_t digest[20];
1261  if (!sha)
1262  return AVERROR(ENOMEM);
1263  av_sha_init(sha, 160);
1265  av_sha_final(sha, digest);
1266  av_free(sha);
1267  fileuid = AV_RL64(digest);
1268  } else {
1269  fileuid = av_lfg_get(&c);
1270  }
1271  av_log(s, AV_LOG_VERBOSE, "Using %.16"PRIx64" for attachment %d\n",
1272  fileuid, i);
1273 
1276  put_ebml_uint(pb, MATROSKA_ID_FILEUID, fileuid);
1277  end_ebml_master(pb, attached_file);
1278  }
1279  end_ebml_master(pb, attachments);
1280 
1281  return 0;
1282 }
1283 
1285 {
1286  MatroskaMuxContext *mkv = s->priv_data;
1287  AVIOContext *pb = s->pb;
1288  ebml_master ebml_header, segment_info;
1290  int ret, i, version = 2;
1291 
1292  if (!strcmp(s->oformat->name, "webm"))
1293  mkv->mode = MODE_WEBM;
1294  else
1295  mkv->mode = MODE_MATROSKAv2;
1296 
1297  if (s->avoid_negative_ts < 0)
1298  s->avoid_negative_ts = 1;
1299 
1300  if (mkv->mode != MODE_WEBM ||
1301  av_dict_get(s->metadata, "stereo_mode", NULL, 0) ||
1302  av_dict_get(s->metadata, "alpha_mode", NULL, 0))
1303  version = 4;
1304 
1305  for (i = 0; i < s->nb_streams; i++) {
1306  if (s->streams[i]->codec->codec_id == AV_CODEC_ID_ATRAC3 ||
1307  s->streams[i]->codec->codec_id == AV_CODEC_ID_COOK ||
1309  s->streams[i]->codec->codec_id == AV_CODEC_ID_SIPR ||
1310  s->streams[i]->codec->codec_id == AV_CODEC_ID_RV10 ||
1311  s->streams[i]->codec->codec_id == AV_CODEC_ID_RV20) {
1312  av_log(s, AV_LOG_ERROR,
1313  "The Matroska muxer does not yet support muxing %s\n",
1315  return AVERROR_PATCHWELCOME;
1316  }
1317  if (s->streams[i]->codec->codec_id == AV_CODEC_ID_OPUS ||
1318  av_dict_get(s->streams[i]->metadata, "stereo_mode", NULL, 0) ||
1319  av_dict_get(s->streams[i]->metadata, "alpha_mode", NULL, 0))
1320  version = 4;
1321  }
1322 
1323  mkv->tracks = av_mallocz_array(s->nb_streams, sizeof(*mkv->tracks));
1324  if (!mkv->tracks)
1325  return AVERROR(ENOMEM);
1326 
1327  ebml_header = start_ebml_master(pb, EBML_ID_HEADER, 0);
1333  put_ebml_uint (pb, EBML_ID_DOCTYPEVERSION , version);
1335  end_ebml_master(pb, ebml_header);
1336 
1338  mkv->segment_offset = avio_tell(pb);
1339 
1340  // we write 2 seek heads - one at the end of the file to point to each
1341  // cluster, and one at the beginning to point to all other level one
1342  // elements (including the seek head at the end of the file), which
1343  // isn't more than 10 elements if we only write one of each other
1344  // currently defined level 1 element
1345  mkv->main_seekhead = mkv_start_seekhead(pb, mkv->segment_offset, 10);
1346  if (!mkv->main_seekhead)
1347  return AVERROR(ENOMEM);
1348 
1350  if (ret < 0) return ret;
1351 
1352  segment_info = start_ebml_master(pb, MATROSKA_ID_INFO, 0);
1354  if ((tag = av_dict_get(s->metadata, "title", NULL, 0)))
1356  if (!(s->flags & AVFMT_FLAG_BITEXACT)) {
1357  uint32_t segment_uid[4];
1358  AVLFG lfg;
1359 
1361 
1362  for (i = 0; i < 4; i++)
1363  segment_uid[i] = av_lfg_get(&lfg);
1364 
1366  if ((tag = av_dict_get(s->metadata, "encoding_tool", NULL, 0)))
1368  else
1370  put_ebml_binary(pb, MATROSKA_ID_SEGMENTUID, segment_uid, 16);
1371  } else {
1372  const char *ident = "Lavf";
1375  }
1376 
1377  if (tag = av_dict_get(s->metadata, "creation_time", NULL, 0)) {
1378  // Adjust time so it's relative to 2001-01-01 and convert to nanoseconds.
1379  int64_t date_utc = (ff_iso8601_to_unix_time(tag->value) - 978307200) * 1000000000;
1380  uint8_t date_utc_buf[8];
1381  AV_WB64(date_utc_buf, date_utc);
1382  put_ebml_binary(pb, MATROSKA_ID_DATEUTC, date_utc_buf, 8);
1383  }
1384 
1385  // reserve space for the duration
1386  mkv->duration = 0;
1387  mkv->duration_offset = avio_tell(pb);
1388  put_ebml_void(pb, 11); // assumes double-precision float to be written
1389  end_ebml_master(pb, segment_info);
1390 
1391  ret = mkv_write_tracks(s);
1392  if (ret < 0)
1393  return ret;
1394 
1395  for (i = 0; i < s->nb_chapters; i++)
1396  mkv->chapter_id_offset = FFMAX(mkv->chapter_id_offset, 1LL - s->chapters[i]->id);
1397 
1398  if (mkv->mode != MODE_WEBM) {
1399  ret = mkv_write_chapters(s);
1400  if (ret < 0)
1401  return ret;
1402 
1403  ret = mkv_write_tags(s);
1404  if (ret < 0)
1405  return ret;
1406 
1407  ret = mkv_write_attachments(s);
1408  if (ret < 0)
1409  return ret;
1410  }
1411 
1412  if (!s->pb->seekable)
1414 
1415  mkv->cues = mkv_start_cues(mkv->segment_offset);
1416  if (!mkv->cues)
1417  return AVERROR(ENOMEM);
1418 
1419  if (pb->seekable && mkv->reserve_cues_space) {
1420  mkv->cues_pos = avio_tell(pb);
1422  }
1423 
1425  mkv->cur_audio_pkt.size = 0;
1426  mkv->cluster_pos = -1;
1427 
1428  avio_flush(pb);
1429 
1430  // start a new cluster every 5 MB or 5 sec, or 32k / 1 sec for streaming or
1431  // after 4k and on a keyframe
1432  if (pb->seekable) {
1433  if (mkv->cluster_time_limit < 0)
1434  mkv->cluster_time_limit = 5000;
1435  if (mkv->cluster_size_limit < 0)
1436  mkv->cluster_size_limit = 5 * 1024 * 1024;
1437  } else {
1438  if (mkv->cluster_time_limit < 0)
1439  mkv->cluster_time_limit = 1000;
1440  if (mkv->cluster_size_limit < 0)
1441  mkv->cluster_size_limit = 32 * 1024;
1442  }
1443 
1444  return 0;
1445 }
1446 
1447 static int mkv_blockgroup_size(int pkt_size)
1448 {
1449  int size = pkt_size + 4;
1450  size += ebml_num_size(size);
1451  size += 2; // EBML ID for block and block duration
1452  size += 8; // max size of block duration
1453  size += ebml_num_size(size);
1454  size += 1; // blockgroup EBML ID
1455  return size;
1456 }
1457 
1458 static int mkv_strip_wavpack(const uint8_t *src, uint8_t **pdst, int *size)
1459 {
1460  uint8_t *dst;
1461  int srclen = *size;
1462  int offset = 0;
1463  int ret;
1464 
1465  dst = av_malloc(srclen);
1466  if (!dst)
1467  return AVERROR(ENOMEM);
1468 
1469  while (srclen >= WV_HEADER_SIZE) {
1470  WvHeader header;
1471 
1472  ret = ff_wv_parse_header(&header, src);
1473  if (ret < 0)
1474  goto fail;
1475  src += WV_HEADER_SIZE;
1476  srclen -= WV_HEADER_SIZE;
1477 
1478  if (srclen < header.blocksize) {
1479  ret = AVERROR_INVALIDDATA;
1480  goto fail;
1481  }
1482 
1483  if (header.initial) {
1484  AV_WL32(dst + offset, header.samples);
1485  offset += 4;
1486  }
1487  AV_WL32(dst + offset, header.flags);
1488  AV_WL32(dst + offset + 4, header.crc);
1489  offset += 8;
1490 
1491  if (!(header.initial && header.final)) {
1492  AV_WL32(dst + offset, header.blocksize);
1493  offset += 4;
1494  }
1495 
1496  memcpy(dst + offset, src, header.blocksize);
1497  src += header.blocksize;
1498  srclen -= header.blocksize;
1499  offset += header.blocksize;
1500  }
1501 
1502  *pdst = dst;
1503  *size = offset;
1504 
1505  return 0;
1506 fail:
1507  av_freep(&dst);
1508  return ret;
1509 }
1510 
1512  unsigned int blockid, AVPacket *pkt, int flags)
1513 {
1514  MatroskaMuxContext *mkv = s->priv_data;
1515  AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
1516  uint8_t *data = NULL, *side_data = NULL;
1517  int offset = 0, size = pkt->size, side_data_size = 0;
1518  int64_t ts = mkv->tracks[pkt->stream_index].write_dts ? pkt->dts : pkt->pts;
1519  uint64_t additional_id = 0;
1520  int64_t discard_padding = 0;
1521  ebml_master block_group, block_additions, block_more;
1522 
1523  av_log(s, AV_LOG_DEBUG, "Writing block at offset %" PRIu64 ", size %d, "
1524  "pts %" PRId64 ", dts %" PRId64 ", duration %d, flags %d\n",
1525  avio_tell(pb), pkt->size, pkt->pts, pkt->dts, pkt->duration, flags);
1526  if (codec->codec_id == AV_CODEC_ID_H264 && codec->extradata_size > 0 &&
1527  (AV_RB24(codec->extradata) == 1 || AV_RB32(codec->extradata) == 1))
1528  ff_avc_parse_nal_units_buf(pkt->data, &data, &size);
1529  else if (codec->codec_id == AV_CODEC_ID_HEVC && codec->extradata_size > 6 &&
1530  (AV_RB24(codec->extradata) == 1 || AV_RB32(codec->extradata) == 1))
1531  /* extradata is Annex B, assume the bitstream is too and convert it */
1532  ff_hevc_annexb2mp4_buf(pkt->data, &data, &size, 0, NULL);
1533  else if (codec->codec_id == AV_CODEC_ID_WAVPACK) {
1534  int ret = mkv_strip_wavpack(pkt->data, &data, &size);
1535  if (ret < 0) {
1536  av_log(s, AV_LOG_ERROR, "Error stripping a WavPack packet.\n");
1537  return;
1538  }
1539  } else
1540  data = pkt->data;
1541 
1542  if (codec->codec_id == AV_CODEC_ID_PRORES) {
1543  /* Matroska specification requires to remove the first QuickTime atom
1544  */
1545  size -= 8;
1546  offset = 8;
1547  }
1548 
1549  side_data = av_packet_get_side_data(pkt,
1551  &side_data_size);
1552 
1553  if (side_data && side_data_size >= 10) {
1554  discard_padding = av_rescale_q(AV_RL32(side_data + 4),
1555  (AVRational){1, codec->sample_rate},
1556  (AVRational){1, 1000000000});
1557  }
1558 
1559  side_data = av_packet_get_side_data(pkt,
1561  &side_data_size);
1562  if (side_data) {
1563  additional_id = AV_RB64(side_data);
1564  side_data += 8;
1565  side_data_size -= 8;
1566  }
1567 
1568  if ((side_data_size && additional_id == 1) || discard_padding) {
1569  block_group = start_ebml_master(pb, MATROSKA_ID_BLOCKGROUP, 0);
1570  blockid = MATROSKA_ID_BLOCK;
1571  }
1572 
1573  put_ebml_id(pb, blockid);
1574  put_ebml_num(pb, size + 4, 0);
1575  // this assumes stream_index is less than 126
1576  avio_w8(pb, 0x80 | (mkv->is_dash ? mkv->dash_track_number : (pkt->stream_index + 1)));
1577  avio_wb16(pb, ts - mkv->cluster_pts);
1578  avio_w8(pb, flags);
1579  avio_write(pb, data + offset, size);
1580  if (data != pkt->data)
1581  av_free(data);
1582 
1583  if (discard_padding) {
1584  put_ebml_sint(pb, MATROSKA_ID_DISCARDPADDING, discard_padding);
1585  }
1586 
1587  if (side_data_size && additional_id == 1) {
1588  block_additions = start_ebml_master(pb, MATROSKA_ID_BLOCKADDITIONS, 0);
1589  block_more = start_ebml_master(pb, MATROSKA_ID_BLOCKMORE, 0);
1592  put_ebml_num(pb, side_data_size, 0);
1593  avio_write(pb, side_data, side_data_size);
1594  end_ebml_master(pb, block_more);
1595  end_ebml_master(pb, block_additions);
1596  }
1597  if ((side_data_size && additional_id == 1) || discard_padding) {
1598  end_ebml_master(pb, block_group);
1599  }
1600 }
1601 
1603 {
1604  MatroskaMuxContext *mkv = s->priv_data;
1605  ebml_master blockgroup;
1606  int id_size, settings_size, size;
1607  uint8_t *id, *settings;
1608  int64_t ts = mkv->tracks[pkt->stream_index].write_dts ? pkt->dts : pkt->pts;
1609  const int flags = 0;
1610 
1611  id_size = 0;
1613  &id_size);
1614 
1615  settings_size = 0;
1617  &settings_size);
1618 
1619  size = id_size + 1 + settings_size + 1 + pkt->size;
1620 
1621  av_log(s, AV_LOG_DEBUG, "Writing block at offset %" PRIu64 ", size %d, "
1622  "pts %" PRId64 ", dts %" PRId64 ", duration %d, flags %d\n",
1623  avio_tell(pb), size, pkt->pts, pkt->dts, pkt->duration, flags);
1624 
1626 
1628  put_ebml_num(pb, size + 4, 0);
1629  avio_w8(pb, 0x80 | (pkt->stream_index + 1)); // this assumes stream_index is less than 126
1630  avio_wb16(pb, ts - mkv->cluster_pts);
1631  avio_w8(pb, flags);
1632  avio_printf(pb, "%.*s\n%.*s\n%.*s", id_size, id, settings_size, settings, pkt->size, pkt->data);
1633 
1635  end_ebml_master(pb, blockgroup);
1636 
1637  return pkt->duration;
1638 }
1639 
1641 {
1642  MatroskaMuxContext *mkv = s->priv_data;
1643  int bufsize;
1644  uint8_t *dyn_buf;
1645 
1646  if (!mkv->dyn_bc)
1647  return;
1648 
1649  bufsize = avio_close_dyn_buf(mkv->dyn_bc, &dyn_buf);
1650  avio_write(s->pb, dyn_buf, bufsize);
1651  av_free(dyn_buf);
1652  mkv->dyn_bc = NULL;
1653 }
1654 
1656 {
1657  MatroskaMuxContext *mkv = s->priv_data;
1658  AVIOContext *pb;
1659 
1660  if (s->pb->seekable) {
1661  pb = s->pb;
1662  } else {
1663  pb = mkv->dyn_bc;
1664  }
1665 
1666  av_log(s, AV_LOG_DEBUG,
1667  "Starting new cluster at offset %" PRIu64 " bytes, "
1668  "pts %" PRIu64 "dts %" PRIu64 "\n",
1669  avio_tell(pb), pkt->pts, pkt->dts);
1670  end_ebml_master(pb, mkv->cluster);
1671  mkv->cluster_pos = -1;
1672  if (mkv->dyn_bc)
1673  mkv_flush_dynbuf(s);
1674  avio_flush(s->pb);
1675 }
1676 
1678 {
1679  MatroskaMuxContext *mkv = s->priv_data;
1680  AVIOContext *pb = s->pb;
1681  AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
1682  int keyframe = !!(pkt->flags & AV_PKT_FLAG_KEY);
1683  int duration = pkt->duration;
1684  int ret;
1685  int64_t ts = mkv->tracks[pkt->stream_index].write_dts ? pkt->dts : pkt->pts;
1686  int64_t relative_packet_pos;
1687  int dash_tracknum = mkv->is_dash ? mkv->dash_track_number : pkt->stream_index + 1;
1688 
1689  if (ts == AV_NOPTS_VALUE) {
1690  av_log(s, AV_LOG_ERROR, "Can't write packet with unknown timestamp\n");
1691  return AVERROR(EINVAL);
1692  }
1693  ts += mkv->tracks[pkt->stream_index].ts_offset;
1694 
1695  if (mkv->cluster_pos != -1) {
1696  int64_t cluster_time = ts - mkv->cluster_pts + mkv->tracks[pkt->stream_index].ts_offset;
1697  if ((int16_t)cluster_time != cluster_time) {
1698  av_log(s, AV_LOG_WARNING, "Starting new cluster due to timestamp\n");
1699  mkv_start_new_cluster(s, pkt);
1700  }
1701  }
1702 
1703  if (!s->pb->seekable) {
1704  if (!mkv->dyn_bc) {
1705  ret = avio_open_dyn_buf(&mkv->dyn_bc);
1706  if (ret < 0) {
1707  av_log(s, AV_LOG_ERROR, "Failed to open dynamic buffer\n");
1708  return ret;
1709  }
1710  }
1711  pb = mkv->dyn_bc;
1712  }
1713 
1714  if (mkv->cluster_pos == -1) {
1715  mkv->cluster_pos = avio_tell(s->pb);
1718  mkv->cluster_pts = FFMAX(0, ts);
1719  }
1720 
1721  relative_packet_pos = avio_tell(s->pb) - mkv->cluster.pos;
1722 
1723  if (codec->codec_type != AVMEDIA_TYPE_SUBTITLE) {
1724  mkv_write_block(s, pb, MATROSKA_ID_SIMPLEBLOCK, pkt, keyframe << 7);
1725  if (codec->codec_type == AVMEDIA_TYPE_VIDEO && keyframe || add_cue) {
1726  ret = mkv_add_cuepoint(mkv->cues, pkt->stream_index, dash_tracknum, ts, mkv->cluster_pos, relative_packet_pos, -1);
1727  if (ret < 0) return ret;
1728  }
1729  } else {
1730  if (codec->codec_id == AV_CODEC_ID_WEBVTT) {
1731  duration = mkv_write_vtt_blocks(s, pb, pkt);
1732  } else {
1734  mkv_blockgroup_size(pkt->size));
1735  /* For backward compatibility, prefer convergence_duration. */
1736  if (pkt->convergence_duration > 0) {
1737  duration = pkt->convergence_duration;
1738  }
1739  mkv_write_block(s, pb, MATROSKA_ID_BLOCK, pkt, 0);
1741  end_ebml_master(pb, blockgroup);
1742  }
1743 
1744  ret = mkv_add_cuepoint(mkv->cues, pkt->stream_index, dash_tracknum, ts,
1745  mkv->cluster_pos, relative_packet_pos, duration);
1746  if (ret < 0)
1747  return ret;
1748  }
1749 
1750  mkv->duration = FFMAX(mkv->duration, ts + duration);
1751  return 0;
1752 }
1753 
1755 {
1756  MatroskaMuxContext *mkv = s->priv_data;
1757  int codec_type = s->streams[pkt->stream_index]->codec->codec_type;
1758  int keyframe = !!(pkt->flags & AV_PKT_FLAG_KEY);
1759  int cluster_size;
1760  int64_t cluster_time;
1761  int ret;
1762  int start_new_cluster;
1763 
1764  if (mkv->tracks[pkt->stream_index].write_dts)
1765  cluster_time = pkt->dts - mkv->cluster_pts;
1766  else
1767  cluster_time = pkt->pts - mkv->cluster_pts;
1768  cluster_time += mkv->tracks[pkt->stream_index].ts_offset;
1769 
1770  // start a new cluster every 5 MB or 5 sec, or 32k / 1 sec for streaming or
1771  // after 4k and on a keyframe
1772  if (s->pb->seekable) {
1773  cluster_size = avio_tell(s->pb) - mkv->cluster_pos;
1774  } else {
1775  cluster_size = avio_tell(mkv->dyn_bc);
1776  }
1777 
1778  if (mkv->is_dash && codec_type == AVMEDIA_TYPE_VIDEO) {
1779  // WebM DASH specification states that the first block of every cluster
1780  // has to be a key frame. So for DASH video, we only create a cluster
1781  // on seeing key frames.
1782  start_new_cluster = keyframe;
1783  } else if (mkv->is_dash && codec_type == AVMEDIA_TYPE_AUDIO &&
1784  (mkv->cluster_pos == -1 ||
1785  cluster_time > mkv->cluster_time_limit)) {
1786  // For DASH audio, we create a Cluster based on cluster_time_limit
1787  start_new_cluster = 1;
1788  } else if (!mkv->is_dash &&
1789  (cluster_size > mkv->cluster_size_limit ||
1790  cluster_time > mkv->cluster_time_limit ||
1791  (codec_type == AVMEDIA_TYPE_VIDEO && keyframe &&
1792  cluster_size > 4 * 1024))) {
1793  start_new_cluster = 1;
1794  } else {
1795  start_new_cluster = 0;
1796  }
1797 
1798  if (mkv->cluster_pos != -1 && start_new_cluster) {
1799  mkv_start_new_cluster(s, pkt);
1800  }
1801 
1802  // check if we have an audio packet cached
1803  if (mkv->cur_audio_pkt.size > 0) {
1804  // for DASH audio, a CuePoint has to be added when there is a new cluster.
1806  mkv->is_dash ? start_new_cluster : 0);
1808  if (ret < 0) {
1809  av_log(s, AV_LOG_ERROR,
1810  "Could not write cached audio packet ret:%d\n", ret);
1811  return ret;
1812  }
1813  }
1814 
1815  // buffer an audio packet to ensure the packet containing the video
1816  // keyframe's timecode is contained in the same cluster for WebM
1817  if (codec_type == AVMEDIA_TYPE_AUDIO) {
1818  mkv->cur_audio_pkt = *pkt;
1819  if (pkt->buf) {
1820  mkv->cur_audio_pkt.buf = av_buffer_ref(pkt->buf);
1821  ret = mkv->cur_audio_pkt.buf ? 0 : AVERROR(ENOMEM);
1822  } else
1823  ret = av_dup_packet(&mkv->cur_audio_pkt);
1824  if (mkv->cur_audio_pkt.side_data_elems > 0) {
1826  }
1827  } else
1828  ret = mkv_write_packet_internal(s, pkt, 0);
1829  return ret;
1830 }
1831 
1833 {
1834  MatroskaMuxContext *mkv = s->priv_data;
1835  AVIOContext *pb;
1836  if (s->pb->seekable)
1837  pb = s->pb;
1838  else
1839  pb = mkv->dyn_bc;
1840  if (!pkt) {
1841  if (mkv->cluster_pos != -1) {
1842  av_log(s, AV_LOG_DEBUG,
1843  "Flushing cluster at offset %" PRIu64 " bytes\n",
1844  avio_tell(pb));
1845  end_ebml_master(pb, mkv->cluster);
1846  mkv->cluster_pos = -1;
1847  if (mkv->dyn_bc)
1848  mkv_flush_dynbuf(s);
1849  avio_flush(s->pb);
1850  }
1851  return 0;
1852  }
1853  return mkv_write_packet(s, pkt);
1854 }
1855 
1857 {
1858  MatroskaMuxContext *mkv = s->priv_data;
1859  AVIOContext *pb = s->pb;
1860  int64_t currentpos, cuespos;
1861  int ret;
1862 
1863  // check if we have an audio packet cached
1864  if (mkv->cur_audio_pkt.size > 0) {
1865  ret = mkv_write_packet_internal(s, &mkv->cur_audio_pkt, 0);
1867  if (ret < 0) {
1868  av_log(s, AV_LOG_ERROR,
1869  "Could not write cached audio packet ret:%d\n", ret);
1870  return ret;
1871  }
1872  }
1873 
1874  if (mkv->dyn_bc) {
1875  end_ebml_master(mkv->dyn_bc, mkv->cluster);
1876  mkv_flush_dynbuf(s);
1877  } else if (mkv->cluster_pos != -1) {
1878  end_ebml_master(pb, mkv->cluster);
1879  }
1880 
1881  if (mkv->mode != MODE_WEBM) {
1882  ret = mkv_write_chapters(s);
1883  if (ret < 0)
1884  return ret;
1885  }
1886 
1887  if (pb->seekable) {
1888  if (mkv->cues->num_entries) {
1889  if (mkv->reserve_cues_space) {
1890  int64_t cues_end;
1891 
1892  currentpos = avio_tell(pb);
1893  avio_seek(pb, mkv->cues_pos, SEEK_SET);
1894 
1895  cuespos = mkv_write_cues(s, mkv->cues, mkv->tracks, s->nb_streams);
1896  cues_end = avio_tell(pb);
1897  if (cues_end > cuespos + mkv->reserve_cues_space) {
1898  av_log(s, AV_LOG_ERROR,
1899  "Insufficient space reserved for cues: %d "
1900  "(needed: %" PRId64 ").\n",
1901  mkv->reserve_cues_space, cues_end - cuespos);
1902  return AVERROR(EINVAL);
1903  }
1904 
1905  if (cues_end < cuespos + mkv->reserve_cues_space)
1907  (cues_end - cuespos));
1908 
1909  avio_seek(pb, currentpos, SEEK_SET);
1910  } else {
1911  cuespos = mkv_write_cues(s, mkv->cues, mkv->tracks, s->nb_streams);
1912  }
1913 
1915  cuespos);
1916  if (ret < 0)
1917  return ret;
1918  }
1919 
1921 
1922  // update the duration
1923  av_log(s, AV_LOG_DEBUG, "end duration = %" PRIu64 "\n", mkv->duration);
1924  currentpos = avio_tell(pb);
1925  avio_seek(pb, mkv->duration_offset, SEEK_SET);
1927 
1928  avio_seek(pb, currentpos, SEEK_SET);
1929  }
1930 
1931  end_ebml_master(pb, mkv->segment);
1932  av_freep(&mkv->tracks);
1933  av_freep(&mkv->cues->entries);
1934  av_freep(&mkv->cues);
1935 
1936  return 0;
1937 }
1938 
1939 static int mkv_query_codec(enum AVCodecID codec_id, int std_compliance)
1940 {
1941  int i;
1942  for (i = 0; ff_mkv_codec_tags[i].id != AV_CODEC_ID_NONE; i++)
1943  if (ff_mkv_codec_tags[i].id == codec_id)
1944  return 1;
1945 
1946  if (std_compliance < FF_COMPLIANCE_NORMAL) {
1947  enum AVMediaType type = avcodec_get_type(codec_id);
1948  // mkv theoretically supports any video/audio through VFW/ACM
1949  if (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO)
1950  return 1;
1951  }
1952 
1953  return 0;
1954 }
1955 
1957  { AV_CODEC_ID_ALAC, 0XFFFFFFFF },
1958  { AV_CODEC_ID_EAC3, 0XFFFFFFFF },
1959  { AV_CODEC_ID_MLP, 0xFFFFFFFF },
1960  { AV_CODEC_ID_OPUS, 0xFFFFFFFF },
1961  { AV_CODEC_ID_PCM_S16BE, 0xFFFFFFFF },
1962  { AV_CODEC_ID_PCM_S24BE, 0xFFFFFFFF },
1963  { AV_CODEC_ID_PCM_S32BE, 0xFFFFFFFF },
1964  { AV_CODEC_ID_QDM2, 0xFFFFFFFF },
1965  { AV_CODEC_ID_RA_144, 0xFFFFFFFF },
1966  { AV_CODEC_ID_RA_288, 0xFFFFFFFF },
1967  { AV_CODEC_ID_COOK, 0xFFFFFFFF },
1968  { AV_CODEC_ID_TRUEHD, 0xFFFFFFFF },
1969  { AV_CODEC_ID_NONE, 0xFFFFFFFF }
1970 };
1971 
1973  { AV_CODEC_ID_RV10, 0xFFFFFFFF },
1974  { AV_CODEC_ID_RV20, 0xFFFFFFFF },
1975  { AV_CODEC_ID_RV30, 0xFFFFFFFF },
1976  { AV_CODEC_ID_RV40, 0xFFFFFFFF },
1977  { AV_CODEC_ID_VP9, 0xFFFFFFFF },
1978  { AV_CODEC_ID_NONE, 0xFFFFFFFF }
1979 };
1980 
1981 #define OFFSET(x) offsetof(MatroskaMuxContext, x)
1982 #define FLAGS AV_OPT_FLAG_ENCODING_PARAM
1983 static const AVOption options[] = {
1984  { "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 },
1985  { "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 },
1986  { "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 },
1987  { "dash", "Create a WebM file conforming to WebM DASH specification", OFFSET(is_dash), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
1988  { "dash_track_number", "Track number for the DASH stream", OFFSET(dash_track_number), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 127, FLAGS },
1989  { NULL },
1990 };
1991 
1992 #if CONFIG_MATROSKA_MUXER
1993 static const AVClass matroska_class = {
1994  .class_name = "matroska muxer",
1995  .item_name = av_default_item_name,
1996  .option = options,
1997  .version = LIBAVUTIL_VERSION_INT,
1998 };
1999 
2000 AVOutputFormat ff_matroska_muxer = {
2001  .name = "matroska",
2002  .long_name = NULL_IF_CONFIG_SMALL("Matroska"),
2003  .mime_type = "video/x-matroska",
2004  .extensions = "mkv",
2005  .priv_data_size = sizeof(MatroskaMuxContext),
2006  .audio_codec = CONFIG_LIBVORBIS_ENCODER ?
2008  .video_codec = CONFIG_LIBX264_ENCODER ?
2015  .codec_tag = (const AVCodecTag* const []){
2018  },
2019  .subtitle_codec = AV_CODEC_ID_ASS,
2020  .query_codec = mkv_query_codec,
2021  .priv_class = &matroska_class,
2022 };
2023 #endif
2024 
2025 #if CONFIG_WEBM_MUXER
2026 static const AVClass webm_class = {
2027  .class_name = "webm muxer",
2028  .item_name = av_default_item_name,
2029  .option = options,
2030  .version = LIBAVUTIL_VERSION_INT,
2031 };
2032 
2033 AVOutputFormat ff_webm_muxer = {
2034  .name = "webm",
2035  .long_name = NULL_IF_CONFIG_SMALL("WebM"),
2036  .mime_type = "video/webm",
2037  .extensions = "webm",
2038  .priv_data_size = sizeof(MatroskaMuxContext),
2039  .audio_codec = AV_CODEC_ID_VORBIS,
2040  .video_codec = AV_CODEC_ID_VP8,
2041  .subtitle_codec = AV_CODEC_ID_WEBVTT,
2047  .priv_class = &webm_class,
2048 };
2049 #endif
2050 
2051 #if CONFIG_MATROSKA_AUDIO_MUXER
2052 static const AVClass mka_class = {
2053  .class_name = "matroska audio muxer",
2054  .item_name = av_default_item_name,
2055  .option = options,
2056  .version = LIBAVUTIL_VERSION_INT,
2057 };
2058 AVOutputFormat ff_matroska_audio_muxer = {
2059  .name = "matroska",
2060  .long_name = NULL_IF_CONFIG_SMALL("Matroska Audio"),
2061  .mime_type = "audio/x-matroska",
2062  .extensions = "mka",
2063  .priv_data_size = sizeof(MatroskaMuxContext),
2064  .audio_codec = CONFIG_LIBVORBIS_ENCODER ?
2065  AV_CODEC_ID_VORBIS : AV_CODEC_ID_AC3,
2066  .video_codec = AV_CODEC_ID_NONE,
2072  .codec_tag = (const AVCodecTag* const []){
2074  },
2075  .priv_class = &mka_class,
2076 };
2077 #endif