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