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->delay && codec->codec_id == AV_CODEC_ID_OPUS) {
874 // mkv->tracks[i].ts_offset = av_rescale_q(codec->delay,
875 // (AVRational){ 1, codec->sample_rate },
876 // st->time_base);
877 
879  av_rescale_q(codec->delay, (AVRational){ 1, codec->sample_rate },
880  (AVRational){ 1, 1000000000 }));
881  }
882  if (codec->codec_id == AV_CODEC_ID_OPUS) {
884  }
885 
886  if (mkv->mode == MODE_WEBM && !(codec->codec_id == AV_CODEC_ID_VP8 ||
887  codec->codec_id == AV_CODEC_ID_VP9 ||
888  codec->codec_id == AV_CODEC_ID_OPUS ||
889  codec->codec_id == AV_CODEC_ID_VORBIS ||
890  codec->codec_id == AV_CODEC_ID_WEBVTT)) {
892  "Only VP8 or VP9 video and Vorbis or Opus audio and WebVTT subtitles are supported for WebM.\n");
893  return AVERROR(EINVAL);
894  }
895 
896  switch (codec->codec_type) {
897  case AVMEDIA_TYPE_VIDEO:
899 
900  if( st->avg_frame_rate.num > 0 && st->avg_frame_rate.den > 0
901  && 1.0/av_q2d(st->avg_frame_rate) > av_q2d(codec->time_base))
902  put_ebml_uint(pb, MATROSKA_ID_TRACKDEFAULTDURATION, 1E9 / av_q2d(st->avg_frame_rate));
903  else
904  put_ebml_uint(pb, MATROSKA_ID_TRACKDEFAULTDURATION, av_q2d(codec->time_base)*1E9);
905 
906  if (!native_id &&
907  ff_codec_get_tag(ff_codec_movvideo_tags, codec->codec_id) &&
908  (!ff_codec_get_tag(ff_codec_bmp_tags, codec->codec_id) ||
909  codec->codec_id == AV_CODEC_ID_SVQ1 ||
910  codec->codec_id == AV_CODEC_ID_SVQ3 ||
911  codec->codec_id == AV_CODEC_ID_CINEPAK))
912  qt_id = 1;
913 
914  if (qt_id)
915  put_ebml_string(pb, MATROSKA_ID_CODECID, "V_QUICKTIME");
916  else if (!native_id) {
917  // if there is no mkv-specific codec ID, use VFW mode
918  put_ebml_string(pb, MATROSKA_ID_CODECID, "V_MS/VFW/FOURCC");
919  mkv->tracks[i].write_dts = 1;
920  }
921 
922  subinfo = start_ebml_master(pb, MATROSKA_ID_TRACKVIDEO, 0);
923  // XXX: interlace flag?
924  put_ebml_uint (pb, MATROSKA_ID_VIDEOPIXELWIDTH , codec->width);
925  put_ebml_uint (pb, MATROSKA_ID_VIDEOPIXELHEIGHT, codec->height);
926 
927  // check both side data and metadata for stereo information,
928  // write the result to the bitstream if any is found
929  ret = mkv_write_stereo_mode(s, pb, st, mkv->mode);
930  if (ret < 0)
931  return ret;
932 
933  switch (ret) {
935  case AV_STEREO3D_COLUMNS:
936  display_width_div = 2;
937  break;
939  case AV_STEREO3D_LINES:
940  display_height_div = 2;
941  break;
942  }
943 
944  if (((tag = av_dict_get(st->metadata, "alpha_mode", NULL, 0)) && atoi(tag->value)) ||
945  ((tag = av_dict_get( s->metadata, "alpha_mode", NULL, 0)) && atoi(tag->value)) ||
946  (codec->pix_fmt == AV_PIX_FMT_YUVA420P)) {
948  }
949 
950  if (st->sample_aspect_ratio.num) {
951  int64_t d_width = av_rescale(codec->width, st->sample_aspect_ratio.num, st->sample_aspect_ratio.den);
952  if (d_width > INT_MAX) {
953  av_log(s, AV_LOG_ERROR, "Overflow in display width\n");
954  return AVERROR(EINVAL);
955  }
956  put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYWIDTH , d_width / display_width_div);
957  put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYHEIGHT, codec->height / display_height_div);
958  } else if (display_width_div != 1 || display_height_div != 1) {
959  put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYWIDTH , codec->width / display_width_div);
960  put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYHEIGHT, codec->height / display_height_div);
961  }
962 
963  if (codec->codec_id == AV_CODEC_ID_RAWVIDEO) {
964  uint32_t color_space = av_le2ne32(codec->codec_tag);
965  put_ebml_binary(pb, MATROSKA_ID_VIDEOCOLORSPACE, &color_space, sizeof(color_space));
966  }
967  end_ebml_master(pb, subinfo);
968  break;
969 
970  case AVMEDIA_TYPE_AUDIO:
972 
973  if (!native_id)
974  // no mkv-specific ID, use ACM mode
975  put_ebml_string(pb, MATROSKA_ID_CODECID, "A_MS/ACM");
976 
977  subinfo = start_ebml_master(pb, MATROSKA_ID_TRACKAUDIO, 0);
978  put_ebml_uint (pb, MATROSKA_ID_AUDIOCHANNELS , codec->channels);
980  if (output_sample_rate)
981  put_ebml_float(pb, MATROSKA_ID_AUDIOOUTSAMPLINGFREQ, output_sample_rate);
982  if (bit_depth)
984  end_ebml_master(pb, subinfo);
985  break;
986 
988  if (!native_id) {
989  av_log(s, AV_LOG_ERROR, "Subtitle codec %d is not supported.\n", codec->codec_id);
990  return AVERROR(ENOSYS);
991  }
992 
993  if (mkv->mode != MODE_WEBM || codec->codec_id != AV_CODEC_ID_WEBVTT)
994  native_id = MATROSKA_TRACK_TYPE_SUBTITLE;
995 
996  put_ebml_uint(pb, MATROSKA_ID_TRACKTYPE, native_id);
997  break;
998  default:
999  av_log(s, AV_LOG_ERROR, "Only audio, video, and subtitles are supported for Matroska.\n");
1000  return AVERROR(EINVAL);
1001  }
1002 
1003  if (mkv->mode != MODE_WEBM || codec->codec_id != AV_CODEC_ID_WEBVTT) {
1004  ret = mkv_write_codecprivate(s, pb, codec, native_id, qt_id);
1005  if (ret < 0)
1006  return ret;
1007  }
1008 
1009  end_ebml_master(pb, track);
1010 
1011  return 0;
1012 }
1013 
1015 {
1016  MatroskaMuxContext *mkv = s->priv_data;
1017  AVIOContext *pb = s->pb;
1018  ebml_master tracks;
1019  int i, ret, default_stream_exists = 0;
1020 
1022  if (ret < 0)
1023  return ret;
1024 
1025  tracks = start_ebml_master(pb, MATROSKA_ID_TRACKS, 0);
1026  for (i = 0; i < s->nb_streams; i++) {
1027  AVStream *st = s->streams[i];
1028  default_stream_exists |= st->disposition & AV_DISPOSITION_DEFAULT;
1029  }
1030  for (i = 0; i < s->nb_streams; i++) {
1031  ret = mkv_write_track(s, mkv, i, pb, default_stream_exists);
1032  if (ret < 0)
1033  return ret;
1034  }
1035  end_ebml_master(pb, tracks);
1036  return 0;
1037 }
1038 
1040 {
1041  MatroskaMuxContext *mkv = s->priv_data;
1042  AVIOContext *pb = s->pb;
1043  ebml_master chapters, editionentry;
1044  AVRational scale = {1, 1E9};
1045  int i, ret;
1046 
1047  if (!s->nb_chapters || mkv->wrote_chapters)
1048  return 0;
1049 
1051  if (ret < 0) return ret;
1052 
1053  chapters = start_ebml_master(pb, MATROSKA_ID_CHAPTERS , 0);
1054  editionentry = start_ebml_master(pb, MATROSKA_ID_EDITIONENTRY, 0);
1057  for (i = 0; i < s->nb_chapters; i++) {
1058  ebml_master chapteratom, chapterdisplay;
1059  AVChapter *c = s->chapters[i];
1060  AVDictionaryEntry *t = NULL;
1061 
1062  chapteratom = start_ebml_master(pb, MATROSKA_ID_CHAPTERATOM, 0);
1065  av_rescale_q(c->start, c->time_base, scale));
1067  av_rescale_q(c->end, c->time_base, scale));
1070  if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
1071  chapterdisplay = start_ebml_master(pb, MATROSKA_ID_CHAPTERDISPLAY, 0);
1074  end_ebml_master(pb, chapterdisplay);
1075  }
1076  end_ebml_master(pb, chapteratom);
1077  }
1078  end_ebml_master(pb, editionentry);
1079  end_ebml_master(pb, chapters);
1080 
1081  mkv->wrote_chapters = 1;
1082  return 0;
1083 }
1084 
1086 {
1087  uint8_t *key = av_strdup(t->key);
1088  uint8_t *p = key;
1089  const uint8_t *lang = NULL;
1090  ebml_master tag;
1091 
1092  if ((p = strrchr(p, '-')) &&
1093  (lang = av_convert_lang_to(p + 1, AV_LANG_ISO639_2_BIBL)))
1094  *p = 0;
1095 
1096  p = key;
1097  while (*p) {
1098  if (*p == ' ')
1099  *p = '_';
1100  else if (*p >= 'a' && *p <= 'z')
1101  *p -= 'a' - 'A';
1102  p++;
1103  }
1104 
1107  if (lang)
1110  end_ebml_master(pb, tag);
1111 
1112  av_freep(&key);
1113 }
1114 
1115 static int mkv_write_tag(AVFormatContext *s, AVDictionary *m, unsigned int elementid,
1116  unsigned int uid, ebml_master *tags)
1117 {
1118  MatroskaMuxContext *mkv = s->priv_data;
1119  ebml_master tag, targets;
1120  AVDictionaryEntry *t = NULL;
1121  int ret;
1122 
1123  if (!tags->pos) {
1125  if (ret < 0) return ret;
1126 
1127  *tags = start_ebml_master(s->pb, MATROSKA_ID_TAGS, 0);
1128  }
1129 
1130  tag = start_ebml_master(s->pb, MATROSKA_ID_TAG, 0);
1131  targets = start_ebml_master(s->pb, MATROSKA_ID_TAGTARGETS, 0);
1132  if (elementid)
1133  put_ebml_uint(s->pb, elementid, uid);
1134  end_ebml_master(s->pb, targets);
1135 
1136  while ((t = av_dict_get(m, "", t, AV_DICT_IGNORE_SUFFIX)))
1137  if (av_strcasecmp(t->key, "title") &&
1138  av_strcasecmp(t->key, "stereo_mode") &&
1139  av_strcasecmp(t->key, "encoding_tool"))
1140  mkv_write_simpletag(s->pb, t);
1141 
1142  end_ebml_master(s->pb, tag);
1143  return 0;
1144 }
1145 
1147 {
1148  AVDictionaryEntry *t = NULL;
1149 
1150  while ((t = av_dict_get(m, "", t, AV_DICT_IGNORE_SUFFIX)))
1151  if (av_strcasecmp(t->key, "title") && av_strcasecmp(t->key, "stereo_mode"))
1152  return 1;
1153 
1154  return 0;
1155 }
1156 
1158 {
1159  MatroskaMuxContext *mkv = s->priv_data;
1160  ebml_master tags = {0};
1161  int i, ret;
1162 
1164 
1165  if (mkv_check_tag(s->metadata)) {
1166  ret = mkv_write_tag(s, s->metadata, 0, 0, &tags);
1167  if (ret < 0) return ret;
1168  }
1169 
1170  for (i = 0; i < s->nb_streams; i++) {
1171  AVStream *st = s->streams[i];
1172 
1173  if (!mkv_check_tag(st->metadata))
1174  continue;
1175 
1176  ret = mkv_write_tag(s, st->metadata, MATROSKA_ID_TAGTARGETS_TRACKUID, i + 1, &tags);
1177  if (ret < 0) return ret;
1178  }
1179 
1180  for (i = 0; i < s->nb_chapters; i++) {
1181  AVChapter *ch = s->chapters[i];
1182 
1183  if (!mkv_check_tag(ch->metadata))
1184  continue;
1185 
1187  if (ret < 0) return ret;
1188  }
1189 
1190  if (tags.pos)
1191  end_ebml_master(s->pb, tags);
1192  return 0;
1193 }
1194 
1196 {
1197  MatroskaMuxContext *mkv = s->priv_data;
1198  AVIOContext *pb = s->pb;
1199  ebml_master attachments;
1200  AVLFG c;
1201  int i, ret;
1202 
1203  if (!mkv->have_attachments)
1204  return 0;
1205 
1207 
1209  if (ret < 0) return ret;
1210 
1211  attachments = start_ebml_master(pb, MATROSKA_ID_ATTACHMENTS, 0);
1212 
1213  for (i = 0; i < s->nb_streams; i++) {
1214  AVStream *st = s->streams[i];
1215  ebml_master attached_file;
1216  AVDictionaryEntry *t;
1217  const char *mimetype = NULL;
1218  uint64_t fileuid;
1219 
1221  continue;
1222 
1223  attached_file = start_ebml_master(pb, MATROSKA_ID_ATTACHEDFILE, 0);
1224 
1225  if (t = av_dict_get(st->metadata, "title", NULL, 0))
1227  if (!(t = av_dict_get(st->metadata, "filename", NULL, 0))) {
1228  av_log(s, AV_LOG_ERROR, "Attachment stream %d has no filename tag.\n", i);
1229  return AVERROR(EINVAL);
1230  }
1232  if (t = av_dict_get(st->metadata, "mimetype", NULL, 0))
1233  mimetype = t->value;
1234  else if (st->codec->codec_id != AV_CODEC_ID_NONE ) {
1235  int i;
1236  for (i = 0; ff_mkv_mime_tags[i].id != AV_CODEC_ID_NONE; i++)
1237  if (ff_mkv_mime_tags[i].id == st->codec->codec_id) {
1238  mimetype = ff_mkv_mime_tags[i].str;
1239  break;
1240  }
1241  }
1242  if (!mimetype) {
1243  av_log(s, AV_LOG_ERROR, "Attachment stream %d has no mimetype tag and "
1244  "it cannot be deduced from the codec id.\n", i);
1245  return AVERROR(EINVAL);
1246  }
1247 
1248  if (s->flags & AVFMT_FLAG_BITEXACT) {
1249  struct AVSHA *sha = av_sha_alloc();
1250  uint8_t digest[20];
1251  if (!sha)
1252  return AVERROR(ENOMEM);
1253  av_sha_init(sha, 160);
1255  av_sha_final(sha, digest);
1256  av_free(sha);
1257  fileuid = AV_RL64(digest);
1258  } else {
1259  fileuid = av_lfg_get(&c);
1260  }
1261  av_log(s, AV_LOG_VERBOSE, "Using %.16"PRIx64" for attachment %d\n",
1262  fileuid, i);
1263 
1266  put_ebml_uint(pb, MATROSKA_ID_FILEUID, fileuid);
1267  end_ebml_master(pb, attached_file);
1268  }
1269  end_ebml_master(pb, attachments);
1270 
1271  return 0;
1272 }
1273 
1275 {
1276  MatroskaMuxContext *mkv = s->priv_data;
1277  AVIOContext *pb = s->pb;
1278  ebml_master ebml_header, segment_info;
1280  int ret, i, version = 2;
1281 
1282  if (!strcmp(s->oformat->name, "webm"))
1283  mkv->mode = MODE_WEBM;
1284  else
1285  mkv->mode = MODE_MATROSKAv2;
1286 
1287  if (s->avoid_negative_ts < 0)
1288  s->avoid_negative_ts = 1;
1289 
1290  if (mkv->mode != MODE_WEBM ||
1291  av_dict_get(s->metadata, "stereo_mode", NULL, 0) ||
1292  av_dict_get(s->metadata, "alpha_mode", NULL, 0))
1293  version = 4;
1294 
1295  for (i = 0; i < s->nb_streams; i++) {
1296  if (s->streams[i]->codec->codec_id == AV_CODEC_ID_ATRAC3 ||
1297  s->streams[i]->codec->codec_id == AV_CODEC_ID_COOK ||
1299  s->streams[i]->codec->codec_id == AV_CODEC_ID_SIPR ||
1300  s->streams[i]->codec->codec_id == AV_CODEC_ID_RV10 ||
1301  s->streams[i]->codec->codec_id == AV_CODEC_ID_RV20) {
1302  av_log(s, AV_LOG_ERROR,
1303  "The Matroska muxer does not yet support muxing %s\n",
1305  return AVERROR_PATCHWELCOME;
1306  }
1307  if (s->streams[i]->codec->codec_id == AV_CODEC_ID_OPUS ||
1308  av_dict_get(s->streams[i]->metadata, "stereo_mode", NULL, 0) ||
1309  av_dict_get(s->streams[i]->metadata, "alpha_mode", NULL, 0))
1310  version = 4;
1311  }
1312 
1313  mkv->tracks = av_mallocz_array(s->nb_streams, sizeof(*mkv->tracks));
1314  if (!mkv->tracks)
1315  return AVERROR(ENOMEM);
1316 
1317  ebml_header = start_ebml_master(pb, EBML_ID_HEADER, 0);
1323  put_ebml_uint (pb, EBML_ID_DOCTYPEVERSION , version);
1325  end_ebml_master(pb, ebml_header);
1326 
1328  mkv->segment_offset = avio_tell(pb);
1329 
1330  // we write 2 seek heads - one at the end of the file to point to each
1331  // cluster, and one at the beginning to point to all other level one
1332  // elements (including the seek head at the end of the file), which
1333  // isn't more than 10 elements if we only write one of each other
1334  // currently defined level 1 element
1335  mkv->main_seekhead = mkv_start_seekhead(pb, mkv->segment_offset, 10);
1336  if (!mkv->main_seekhead)
1337  return AVERROR(ENOMEM);
1338 
1340  if (ret < 0) return ret;
1341 
1342  segment_info = start_ebml_master(pb, MATROSKA_ID_INFO, 0);
1344  if ((tag = av_dict_get(s->metadata, "title", NULL, 0)))
1346  if (!(s->flags & AVFMT_FLAG_BITEXACT)) {
1347  uint32_t segment_uid[4];
1348  AVLFG lfg;
1349 
1351 
1352  for (i = 0; i < 4; i++)
1353  segment_uid[i] = av_lfg_get(&lfg);
1354 
1356  if ((tag = av_dict_get(s->metadata, "encoding_tool", NULL, 0)))
1358  else
1360  put_ebml_binary(pb, MATROSKA_ID_SEGMENTUID, segment_uid, 16);
1361  } else {
1362  const char *ident = "Lavf";
1365  }
1366 
1367  if (tag = av_dict_get(s->metadata, "creation_time", NULL, 0)) {
1368  // Adjust time so it's relative to 2001-01-01 and convert to nanoseconds.
1369  int64_t date_utc = (ff_iso8601_to_unix_time(tag->value) - 978307200) * 1000000000;
1370  uint8_t date_utc_buf[8];
1371  AV_WB64(date_utc_buf, date_utc);
1372  put_ebml_binary(pb, MATROSKA_ID_DATEUTC, date_utc_buf, 8);
1373  }
1374 
1375  // reserve space for the duration
1376  mkv->duration = 0;
1377  mkv->duration_offset = avio_tell(pb);
1378  put_ebml_void(pb, 11); // assumes double-precision float to be written
1379  end_ebml_master(pb, segment_info);
1380 
1381  ret = mkv_write_tracks(s);
1382  if (ret < 0)
1383  return ret;
1384 
1385  for (i = 0; i < s->nb_chapters; i++)
1386  mkv->chapter_id_offset = FFMAX(mkv->chapter_id_offset, 1LL - s->chapters[i]->id);
1387 
1388  if (mkv->mode != MODE_WEBM) {
1389  ret = mkv_write_chapters(s);
1390  if (ret < 0)
1391  return ret;
1392 
1393  ret = mkv_write_tags(s);
1394  if (ret < 0)
1395  return ret;
1396 
1397  ret = mkv_write_attachments(s);
1398  if (ret < 0)
1399  return ret;
1400  }
1401 
1402  if (!s->pb->seekable)
1404 
1405  mkv->cues = mkv_start_cues(mkv->segment_offset);
1406  if (!mkv->cues)
1407  return AVERROR(ENOMEM);
1408 
1409  if (pb->seekable && mkv->reserve_cues_space) {
1410  mkv->cues_pos = avio_tell(pb);
1412  }
1413 
1415  mkv->cur_audio_pkt.size = 0;
1416  mkv->cluster_pos = -1;
1417 
1418  avio_flush(pb);
1419 
1420  // start a new cluster every 5 MB or 5 sec, or 32k / 1 sec for streaming or
1421  // after 4k and on a keyframe
1422  if (pb->seekable) {
1423  if (mkv->cluster_time_limit < 0)
1424  mkv->cluster_time_limit = 5000;
1425  if (mkv->cluster_size_limit < 0)
1426  mkv->cluster_size_limit = 5 * 1024 * 1024;
1427  } else {
1428  if (mkv->cluster_time_limit < 0)
1429  mkv->cluster_time_limit = 1000;
1430  if (mkv->cluster_size_limit < 0)
1431  mkv->cluster_size_limit = 32 * 1024;
1432  }
1433 
1434  return 0;
1435 }
1436 
1437 static int mkv_blockgroup_size(int pkt_size)
1438 {
1439  int size = pkt_size + 4;
1440  size += ebml_num_size(size);
1441  size += 2; // EBML ID for block and block duration
1442  size += 8; // max size of block duration
1443  size += ebml_num_size(size);
1444  size += 1; // blockgroup EBML ID
1445  return size;
1446 }
1447 
1448 static int mkv_strip_wavpack(const uint8_t *src, uint8_t **pdst, int *size)
1449 {
1450  uint8_t *dst;
1451  int srclen = *size;
1452  int offset = 0;
1453  int ret;
1454 
1455  dst = av_malloc(srclen);
1456  if (!dst)
1457  return AVERROR(ENOMEM);
1458 
1459  while (srclen >= WV_HEADER_SIZE) {
1460  WvHeader header;
1461 
1462  ret = ff_wv_parse_header(&header, src);
1463  if (ret < 0)
1464  goto fail;
1465  src += WV_HEADER_SIZE;
1466  srclen -= WV_HEADER_SIZE;
1467 
1468  if (srclen < header.blocksize) {
1469  ret = AVERROR_INVALIDDATA;
1470  goto fail;
1471  }
1472 
1473  if (header.initial) {
1474  AV_WL32(dst + offset, header.samples);
1475  offset += 4;
1476  }
1477  AV_WL32(dst + offset, header.flags);
1478  AV_WL32(dst + offset + 4, header.crc);
1479  offset += 8;
1480 
1481  if (!(header.initial && header.final)) {
1482  AV_WL32(dst + offset, header.blocksize);
1483  offset += 4;
1484  }
1485 
1486  memcpy(dst + offset, src, header.blocksize);
1487  src += header.blocksize;
1488  srclen -= header.blocksize;
1489  offset += header.blocksize;
1490  }
1491 
1492  *pdst = dst;
1493  *size = offset;
1494 
1495  return 0;
1496 fail:
1497  av_freep(&dst);
1498  return ret;
1499 }
1500 
1502  unsigned int blockid, AVPacket *pkt, int flags)
1503 {
1504  MatroskaMuxContext *mkv = s->priv_data;
1505  AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
1506  uint8_t *data = NULL, *side_data = NULL;
1507  int offset = 0, size = pkt->size, side_data_size = 0;
1508  int64_t ts = mkv->tracks[pkt->stream_index].write_dts ? pkt->dts : pkt->pts;
1509  uint64_t additional_id = 0;
1510  int64_t discard_padding = 0;
1511  ebml_master block_group, block_additions, block_more;
1512 
1513  av_log(s, AV_LOG_DEBUG, "Writing block at offset %" PRIu64 ", size %d, "
1514  "pts %" PRId64 ", dts %" PRId64 ", duration %d, flags %d\n",
1515  avio_tell(pb), pkt->size, pkt->pts, pkt->dts, pkt->duration, flags);
1516  if (codec->codec_id == AV_CODEC_ID_H264 && codec->extradata_size > 0 &&
1517  (AV_RB24(codec->extradata) == 1 || AV_RB32(codec->extradata) == 1))
1518  ff_avc_parse_nal_units_buf(pkt->data, &data, &size);
1519  else if (codec->codec_id == AV_CODEC_ID_HEVC && codec->extradata_size > 6 &&
1520  (AV_RB24(codec->extradata) == 1 || AV_RB32(codec->extradata) == 1))
1521  /* extradata is Annex B, assume the bitstream is too and convert it */
1522  ff_hevc_annexb2mp4_buf(pkt->data, &data, &size, 0, NULL);
1523  else if (codec->codec_id == AV_CODEC_ID_WAVPACK) {
1524  int ret = mkv_strip_wavpack(pkt->data, &data, &size);
1525  if (ret < 0) {
1526  av_log(s, AV_LOG_ERROR, "Error stripping a WavPack packet.\n");
1527  return;
1528  }
1529  } else
1530  data = pkt->data;
1531 
1532  if (codec->codec_id == AV_CODEC_ID_PRORES) {
1533  /* Matroska specification requires to remove the first QuickTime atom
1534  */
1535  size -= 8;
1536  offset = 8;
1537  }
1538 
1539  side_data = av_packet_get_side_data(pkt,
1541  &side_data_size);
1542 
1543  if (side_data && side_data_size >= 10) {
1544  discard_padding = av_rescale_q(AV_RL32(side_data + 4),
1545  (AVRational){1, codec->sample_rate},
1546  (AVRational){1, 1000000000});
1547  }
1548 
1549  side_data = av_packet_get_side_data(pkt,
1551  &side_data_size);
1552  if (side_data) {
1553  additional_id = AV_RB64(side_data);
1554  side_data += 8;
1555  side_data_size -= 8;
1556  }
1557 
1558  if ((side_data_size && additional_id == 1) || discard_padding) {
1559  block_group = start_ebml_master(pb, MATROSKA_ID_BLOCKGROUP, 0);
1560  blockid = MATROSKA_ID_BLOCK;
1561  }
1562 
1563  put_ebml_id(pb, blockid);
1564  put_ebml_num(pb, size + 4, 0);
1565  // this assumes stream_index is less than 126
1566  avio_w8(pb, 0x80 | (mkv->is_dash ? mkv->dash_track_number : (pkt->stream_index + 1)));
1567  avio_wb16(pb, ts - mkv->cluster_pts);
1568  avio_w8(pb, flags);
1569  avio_write(pb, data + offset, size);
1570  if (data != pkt->data)
1571  av_free(data);
1572 
1573  if (discard_padding) {
1574  put_ebml_sint(pb, MATROSKA_ID_DISCARDPADDING, discard_padding);
1575  }
1576 
1577  if (side_data_size && additional_id == 1) {
1578  block_additions = start_ebml_master(pb, MATROSKA_ID_BLOCKADDITIONS, 0);
1579  block_more = start_ebml_master(pb, MATROSKA_ID_BLOCKMORE, 0);
1582  put_ebml_num(pb, side_data_size, 0);
1583  avio_write(pb, side_data, side_data_size);
1584  end_ebml_master(pb, block_more);
1585  end_ebml_master(pb, block_additions);
1586  }
1587  if ((side_data_size && additional_id == 1) || discard_padding) {
1588  end_ebml_master(pb, block_group);
1589  }
1590 }
1591 
1593 {
1594  int i, duration = 0;
1595 
1596  for (i = 0; i < 2 && !duration; i++) {
1597  int s_hour, s_min, s_sec, s_hsec, e_hour, e_min, e_sec, e_hsec;
1598  if (sscanf(*buf, "%d:%2d:%2d%*1[,.]%3d --> %d:%2d:%2d%*1[,.]%3d",
1599  &s_hour, &s_min, &s_sec, &s_hsec,
1600  &e_hour, &e_min, &e_sec, &e_hsec) == 8) {
1601  s_min += 60 * s_hour;
1602  e_min += 60 * e_hour;
1603  s_sec += 60 * s_min;
1604 
1605  e_sec += 60 * e_min;
1606  s_hsec += 1000 * s_sec;
1607  e_hsec += 1000 * e_sec;
1608 
1609  duration = e_hsec - s_hsec;
1610  }
1611  *buf += ff_subtitles_next_line(*buf);
1612  }
1613  return duration;
1614 }
1615 
1617  AVPacket *pkt)
1618 {
1619  ebml_master blockgroup;
1620  AVPacket pkt2 = *pkt;
1621  int64_t duration = srt_get_duration(&pkt2.data);
1622  pkt2.size -= pkt2.data - pkt->data;
1623 
1624  blockgroup = start_ebml_master(pb, MATROSKA_ID_BLOCKGROUP,
1625  mkv_blockgroup_size(pkt2.size));
1626  mkv_write_block(s, pb, MATROSKA_ID_BLOCK, &pkt2, 0);
1628  end_ebml_master(pb, blockgroup);
1629 
1630  return duration;
1631 }
1632 
1634 {
1635  MatroskaMuxContext *mkv = s->priv_data;
1636  ebml_master blockgroup;
1637  int id_size, settings_size, size;
1638  uint8_t *id, *settings;
1639  int64_t ts = mkv->tracks[pkt->stream_index].write_dts ? pkt->dts : pkt->pts;
1640  const int flags = 0;
1641 
1642  id_size = 0;
1644  &id_size);
1645 
1646  settings_size = 0;
1648  &settings_size);
1649 
1650  size = id_size + 1 + settings_size + 1 + pkt->size;
1651 
1652  av_log(s, AV_LOG_DEBUG, "Writing block at offset %" PRIu64 ", size %d, "
1653  "pts %" PRId64 ", dts %" PRId64 ", duration %d, flags %d\n",
1654  avio_tell(pb), size, pkt->pts, pkt->dts, pkt->duration, flags);
1655 
1657 
1659  put_ebml_num(pb, size + 4, 0);
1660  avio_w8(pb, 0x80 | (pkt->stream_index + 1)); // this assumes stream_index is less than 126
1661  avio_wb16(pb, ts - mkv->cluster_pts);
1662  avio_w8(pb, flags);
1663  avio_printf(pb, "%.*s\n%.*s\n%.*s", id_size, id, settings_size, settings, pkt->size, pkt->data);
1664 
1666  end_ebml_master(pb, blockgroup);
1667 
1668  return pkt->duration;
1669 }
1670 
1672 {
1673  MatroskaMuxContext *mkv = s->priv_data;
1674  int bufsize;
1675  uint8_t *dyn_buf;
1676 
1677  if (!mkv->dyn_bc)
1678  return;
1679 
1680  bufsize = avio_close_dyn_buf(mkv->dyn_bc, &dyn_buf);
1681  avio_write(s->pb, dyn_buf, bufsize);
1682  av_free(dyn_buf);
1683  mkv->dyn_bc = NULL;
1684 }
1685 
1687 {
1688  MatroskaMuxContext *mkv = s->priv_data;
1689  AVIOContext *pb;
1690 
1691  if (s->pb->seekable) {
1692  pb = s->pb;
1693  } else {
1694  pb = mkv->dyn_bc;
1695  }
1696 
1697  av_log(s, AV_LOG_DEBUG,
1698  "Starting new cluster at offset %" PRIu64 " bytes, "
1699  "pts %" PRIu64 "dts %" PRIu64 "\n",
1700  avio_tell(pb), pkt->pts, pkt->dts);
1701  end_ebml_master(pb, mkv->cluster);
1702  mkv->cluster_pos = -1;
1703  if (mkv->dyn_bc)
1704  mkv_flush_dynbuf(s);
1705  avio_flush(s->pb);
1706 }
1707 
1709 {
1710  MatroskaMuxContext *mkv = s->priv_data;
1711  AVIOContext *pb = s->pb;
1712  AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
1713  int keyframe = !!(pkt->flags & AV_PKT_FLAG_KEY);
1714  int duration = pkt->duration;
1715  int ret;
1716  int64_t ts = mkv->tracks[pkt->stream_index].write_dts ? pkt->dts : pkt->pts;
1717  int64_t relative_packet_pos;
1718  int dash_tracknum = mkv->is_dash ? mkv->dash_track_number : pkt->stream_index + 1;
1719 
1720  if (ts == AV_NOPTS_VALUE) {
1721  av_log(s, AV_LOG_ERROR, "Can't write packet with unknown timestamp\n");
1722  return AVERROR(EINVAL);
1723  }
1724  ts += mkv->tracks[pkt->stream_index].ts_offset;
1725 
1726  if (mkv->cluster_pos != -1) {
1727  int64_t cluster_time = ts - mkv->cluster_pts + mkv->tracks[pkt->stream_index].ts_offset;
1728  if ((int16_t)cluster_time != cluster_time) {
1729  av_log(s, AV_LOG_WARNING, "Starting new cluster due to timestamp\n");
1730  mkv_start_new_cluster(s, pkt);
1731  }
1732  }
1733 
1734  if (!s->pb->seekable) {
1735  if (!mkv->dyn_bc) {
1736  if ((ret = avio_open_dyn_buf(&mkv->dyn_bc)) < 0) {
1737  av_log(s, AV_LOG_ERROR, "Failed to open dynamic buffer\n");
1738  return ret;
1739  }
1740  }
1741  pb = mkv->dyn_bc;
1742  }
1743 
1744  if (mkv->cluster_pos == -1) {
1745  mkv->cluster_pos = avio_tell(s->pb);
1748  mkv->cluster_pts = FFMAX(0, ts);
1749  }
1750 
1751  relative_packet_pos = avio_tell(s->pb) - mkv->cluster.pos;
1752 
1753  if (codec->codec_type != AVMEDIA_TYPE_SUBTITLE) {
1754  mkv_write_block(s, pb, MATROSKA_ID_SIMPLEBLOCK, pkt, keyframe << 7);
1755  if (codec->codec_type == AVMEDIA_TYPE_VIDEO && keyframe || add_cue) {
1756  ret = mkv_add_cuepoint(mkv->cues, pkt->stream_index, dash_tracknum, ts, mkv->cluster_pos, relative_packet_pos, -1);
1757  if (ret < 0) return ret;
1758  }
1759  } else {
1760  if (codec->codec_id == AV_CODEC_ID_SRT) {
1761  duration = mkv_write_srt_blocks(s, pb, pkt);
1762  } else if (codec->codec_id == AV_CODEC_ID_WEBVTT) {
1763  duration = mkv_write_vtt_blocks(s, pb, pkt);
1764  } else {
1766  mkv_blockgroup_size(pkt->size));
1767  /* For backward compatibility, prefer convergence_duration. */
1768  if (pkt->convergence_duration > 0) {
1769  duration = pkt->convergence_duration;
1770  }
1771  mkv_write_block(s, pb, MATROSKA_ID_BLOCK, pkt, 0);
1773  end_ebml_master(pb, blockgroup);
1774  }
1775 
1776  ret = mkv_add_cuepoint(mkv->cues, pkt->stream_index, dash_tracknum, ts,
1777  mkv->cluster_pos, relative_packet_pos, duration);
1778  if (ret < 0)
1779  return ret;
1780  }
1781 
1782  mkv->duration = FFMAX(mkv->duration, ts + duration);
1783  return 0;
1784 }
1785 
1787 {
1788  MatroskaMuxContext *mkv = s->priv_data;
1789  int codec_type = s->streams[pkt->stream_index]->codec->codec_type;
1790  int keyframe = !!(pkt->flags & AV_PKT_FLAG_KEY);
1791  int cluster_size;
1792  int64_t cluster_time;
1793  int ret;
1794  int start_new_cluster;
1795 
1796  if (mkv->tracks[pkt->stream_index].write_dts)
1797  cluster_time = pkt->dts - mkv->cluster_pts;
1798  else
1799  cluster_time = pkt->pts - mkv->cluster_pts;
1800  cluster_time += mkv->tracks[pkt->stream_index].ts_offset;
1801 
1802  // start a new cluster every 5 MB or 5 sec, or 32k / 1 sec for streaming or
1803  // after 4k and on a keyframe
1804  if (s->pb->seekable) {
1805  cluster_size = avio_tell(s->pb) - mkv->cluster_pos;
1806  } else {
1807  cluster_size = avio_tell(mkv->dyn_bc);
1808  }
1809 
1810  if (mkv->is_dash && codec_type == AVMEDIA_TYPE_VIDEO) {
1811  // WebM DASH specification states that the first block of every cluster
1812  // has to be a key frame. So for DASH video, we only create a cluster
1813  // on seeing key frames.
1814  start_new_cluster = keyframe;
1815  } else if (mkv->is_dash && codec_type == AVMEDIA_TYPE_AUDIO &&
1816  cluster_time > mkv->cluster_time_limit) {
1817  // For DASH audio, we create a Cluster based on cluster_time_limit
1818  start_new_cluster = 1;
1819  } else if (!mkv->is_dash &&
1820  (cluster_size > mkv->cluster_size_limit ||
1821  cluster_time > mkv->cluster_time_limit ||
1822  (codec_type == AVMEDIA_TYPE_VIDEO && keyframe &&
1823  cluster_size > 4 * 1024))) {
1824  start_new_cluster = 1;
1825  } else {
1826  start_new_cluster = 0;
1827  }
1828 
1829  if (mkv->cluster_pos != -1 && start_new_cluster) {
1830  mkv_start_new_cluster(s, pkt);
1831  }
1832 
1833  // check if we have an audio packet cached
1834  if (mkv->cur_audio_pkt.size > 0) {
1835  // for DASH audio, a CuePoint has to be added when there is a new cluster.
1837  mkv->is_dash ? start_new_cluster : 0);
1839  if (ret < 0) {
1840  av_log(s, AV_LOG_ERROR,
1841  "Could not write cached audio packet ret:%d\n", ret);
1842  return ret;
1843  }
1844  }
1845 
1846  // buffer an audio packet to ensure the packet containing the video
1847  // keyframe's timecode is contained in the same cluster for WebM
1848  if (codec_type == AVMEDIA_TYPE_AUDIO) {
1849  mkv->cur_audio_pkt = *pkt;
1850  if (pkt->buf) {
1851  mkv->cur_audio_pkt.buf = av_buffer_ref(pkt->buf);
1852  ret = mkv->cur_audio_pkt.buf ? 0 : AVERROR(ENOMEM);
1853  } else
1854  ret = av_dup_packet(&mkv->cur_audio_pkt);
1855  if (mkv->cur_audio_pkt.side_data_elems > 0) {
1857  }
1858  } else
1859  ret = mkv_write_packet_internal(s, pkt, 0);
1860  return ret;
1861 }
1862 
1864 {
1865  MatroskaMuxContext *mkv = s->priv_data;
1866  AVIOContext *pb;
1867  if (s->pb->seekable)
1868  pb = s->pb;
1869  else
1870  pb = mkv->dyn_bc;
1871  if (!pkt) {
1872  if (mkv->cluster_pos != -1) {
1873  av_log(s, AV_LOG_DEBUG,
1874  "Flushing cluster at offset %" PRIu64 " bytes\n",
1875  avio_tell(pb));
1876  end_ebml_master(pb, mkv->cluster);
1877  mkv->cluster_pos = -1;
1878  if (mkv->dyn_bc)
1879  mkv_flush_dynbuf(s);
1880  avio_flush(s->pb);
1881  }
1882  return 0;
1883  }
1884  return mkv_write_packet(s, pkt);
1885 }
1886 
1888 {
1889  MatroskaMuxContext *mkv = s->priv_data;
1890  AVIOContext *pb = s->pb;
1891  int64_t currentpos, cuespos;
1892  int ret;
1893 
1894  // check if we have an audio packet cached
1895  if (mkv->cur_audio_pkt.size > 0) {
1896  ret = mkv_write_packet_internal(s, &mkv->cur_audio_pkt, 0);
1898  if (ret < 0) {
1899  av_log(s, AV_LOG_ERROR,
1900  "Could not write cached audio packet ret:%d\n", ret);
1901  return ret;
1902  }
1903  }
1904 
1905  if (mkv->dyn_bc) {
1906  end_ebml_master(mkv->dyn_bc, mkv->cluster);
1907  mkv_flush_dynbuf(s);
1908  } else if (mkv->cluster_pos != -1) {
1909  end_ebml_master(pb, mkv->cluster);
1910  }
1911 
1912  if (mkv->mode != MODE_WEBM) {
1913  ret = mkv_write_chapters(s);
1914  if (ret < 0)
1915  return ret;
1916  }
1917 
1918  if (pb->seekable) {
1919  if (mkv->cues->num_entries) {
1920  if (mkv->reserve_cues_space) {
1921  int64_t cues_end;
1922 
1923  currentpos = avio_tell(pb);
1924  avio_seek(pb, mkv->cues_pos, SEEK_SET);
1925 
1926  cuespos = mkv_write_cues(s, mkv->cues, mkv->tracks, s->nb_streams);
1927  cues_end = avio_tell(pb);
1928  if (cues_end > cuespos + mkv->reserve_cues_space) {
1929  av_log(s, AV_LOG_ERROR,
1930  "Insufficient space reserved for cues: %d "
1931  "(needed: %" PRId64 ").\n",
1932  mkv->reserve_cues_space, cues_end - cuespos);
1933  return AVERROR(EINVAL);
1934  }
1935 
1936  if (cues_end < cuespos + mkv->reserve_cues_space)
1938  (cues_end - cuespos));
1939 
1940  avio_seek(pb, currentpos, SEEK_SET);
1941  } else {
1942  cuespos = mkv_write_cues(s, mkv->cues, mkv->tracks, s->nb_streams);
1943  }
1944 
1946  cuespos);
1947  if (ret < 0)
1948  return ret;
1949  }
1950 
1952 
1953  // update the duration
1954  av_log(s, AV_LOG_DEBUG, "end duration = %" PRIu64 "\n", mkv->duration);
1955  currentpos = avio_tell(pb);
1956  avio_seek(pb, mkv->duration_offset, SEEK_SET);
1958 
1959  avio_seek(pb, currentpos, SEEK_SET);
1960  }
1961 
1962  end_ebml_master(pb, mkv->segment);
1963  av_freep(&mkv->tracks);
1964  av_freep(&mkv->cues->entries);
1965  av_freep(&mkv->cues);
1966 
1967  return 0;
1968 }
1969 
1970 static int mkv_query_codec(enum AVCodecID codec_id, int std_compliance)
1971 {
1972  int i;
1973  for (i = 0; ff_mkv_codec_tags[i].id != AV_CODEC_ID_NONE; i++)
1974  if (ff_mkv_codec_tags[i].id == codec_id)
1975  return 1;
1976 
1977  if (std_compliance < FF_COMPLIANCE_NORMAL) {
1978  enum AVMediaType type = avcodec_get_type(codec_id);
1979  // mkv theoretically supports any video/audio through VFW/ACM
1980  if (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO)
1981  return 1;
1982  }
1983 
1984  return 0;
1985 }
1986 
1988  { AV_CODEC_ID_ALAC, 0XFFFFFFFF },
1989  { AV_CODEC_ID_EAC3, 0XFFFFFFFF },
1990  { AV_CODEC_ID_MLP, 0xFFFFFFFF },
1991  { AV_CODEC_ID_OPUS, 0xFFFFFFFF },
1992  { AV_CODEC_ID_PCM_S16BE, 0xFFFFFFFF },
1993  { AV_CODEC_ID_PCM_S24BE, 0xFFFFFFFF },
1994  { AV_CODEC_ID_PCM_S32BE, 0xFFFFFFFF },
1995  { AV_CODEC_ID_QDM2, 0xFFFFFFFF },
1996  { AV_CODEC_ID_RA_144, 0xFFFFFFFF },
1997  { AV_CODEC_ID_RA_288, 0xFFFFFFFF },
1998  { AV_CODEC_ID_COOK, 0xFFFFFFFF },
1999  { AV_CODEC_ID_TRUEHD, 0xFFFFFFFF },
2000  { AV_CODEC_ID_NONE, 0xFFFFFFFF }
2001 };
2002 
2004  { AV_CODEC_ID_RV10, 0xFFFFFFFF },
2005  { AV_CODEC_ID_RV20, 0xFFFFFFFF },
2006  { AV_CODEC_ID_RV30, 0xFFFFFFFF },
2007  { AV_CODEC_ID_RV40, 0xFFFFFFFF },
2008  { AV_CODEC_ID_VP9, 0xFFFFFFFF },
2009  { AV_CODEC_ID_NONE, 0xFFFFFFFF }
2010 };
2011 
2012 #define OFFSET(x) offsetof(MatroskaMuxContext, x)
2013 #define FLAGS AV_OPT_FLAG_ENCODING_PARAM
2014 static const AVOption options[] = {
2015  { "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 },
2016  { "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 },
2017  { "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 },
2018  { "dash", "Create a WebM file conforming to WebM DASH specification", OFFSET(is_dash), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
2019  { "dash_track_number", "Track number for the DASH stream", OFFSET(dash_track_number), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 127, FLAGS },
2020  { NULL },
2021 };
2022 
2023 #if CONFIG_MATROSKA_MUXER
2024 static const AVClass matroska_class = {
2025  .class_name = "matroska muxer",
2026  .item_name = av_default_item_name,
2027  .option = options,
2028  .version = LIBAVUTIL_VERSION_INT,
2029 };
2030 
2031 AVOutputFormat ff_matroska_muxer = {
2032  .name = "matroska",
2033  .long_name = NULL_IF_CONFIG_SMALL("Matroska"),
2034  .mime_type = "video/x-matroska",
2035  .extensions = "mkv",
2036  .priv_data_size = sizeof(MatroskaMuxContext),
2037  .audio_codec = CONFIG_LIBVORBIS_ENCODER ?
2039  .video_codec = CONFIG_LIBX264_ENCODER ?
2046  .codec_tag = (const AVCodecTag* const []){
2049  },
2050  .subtitle_codec = AV_CODEC_ID_ASS,
2051  .query_codec = mkv_query_codec,
2052  .priv_class = &matroska_class,
2053 };
2054 #endif
2055 
2056 #if CONFIG_WEBM_MUXER
2057 static const AVClass webm_class = {
2058  .class_name = "webm muxer",
2059  .item_name = av_default_item_name,
2060  .option = options,
2061  .version = LIBAVUTIL_VERSION_INT,
2062 };
2063 
2064 AVOutputFormat ff_webm_muxer = {
2065  .name = "webm",
2066  .long_name = NULL_IF_CONFIG_SMALL("WebM"),
2067  .mime_type = "video/webm",
2068  .extensions = "webm",
2069  .priv_data_size = sizeof(MatroskaMuxContext),
2070  .audio_codec = AV_CODEC_ID_VORBIS,
2071  .video_codec = AV_CODEC_ID_VP8,
2072  .subtitle_codec = AV_CODEC_ID_WEBVTT,
2078  .priv_class = &webm_class,
2079 };
2080 #endif
2081 
2082 #if CONFIG_MATROSKA_AUDIO_MUXER
2083 static const AVClass mka_class = {
2084  .class_name = "matroska audio muxer",
2085  .item_name = av_default_item_name,
2086  .option = options,
2087  .version = LIBAVUTIL_VERSION_INT,
2088 };
2089 AVOutputFormat ff_matroska_audio_muxer = {
2090  .name = "matroska",
2091  .long_name = NULL_IF_CONFIG_SMALL("Matroska Audio"),
2092  .mime_type = "audio/x-matroska",
2093  .extensions = "mka",
2094  .priv_data_size = sizeof(MatroskaMuxContext),
2095  .audio_codec = CONFIG_LIBVORBIS_ENCODER ?
2096  AV_CODEC_ID_VORBIS : AV_CODEC_ID_AC3,
2097  .video_codec = AV_CODEC_ID_NONE,
2103  .codec_tag = (const AVCodecTag* const []){
2105  },
2106  .priv_class = &mka_class,
2107 };
2108 #endif