FFmpeg
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 "config_components.h"
25 
26 #include "av1.h"
27 #include "avc.h"
28 #include "hevc.h"
29 #include "avformat.h"
30 #include "avio_internal.h"
31 #include "avlanguage.h"
32 #include "dovi_isom.h"
33 #include "flacenc.h"
34 #include "internal.h"
35 #include "isom.h"
36 #include "matroska.h"
37 #include "mux.h"
38 #include "riff.h"
39 #include "version.h"
40 #include "vorbiscomment.h"
41 #include "wv.h"
42 
43 #include "libavutil/avstring.h"
45 #include "libavutil/crc.h"
46 #include "libavutil/dict.h"
47 #include "libavutil/intfloat.h"
48 #include "libavutil/intreadwrite.h"
49 #include "libavutil/lfg.h"
51 #include "libavutil/mathematics.h"
52 #include "libavutil/opt.h"
53 #include "libavutil/parseutils.h"
54 #include "libavutil/random_seed.h"
55 #include "libavutil/rational.h"
56 #include "libavutil/samplefmt.h"
57 #include "libavutil/stereo3d.h"
58 
59 #include "libavcodec/av1.h"
60 #include "libavcodec/xiph.h"
61 #include "libavcodec/mpeg4audio.h"
62 
63 /* Level 1 elements we create a SeekHead entry for:
64  * Info, Tracks, Chapters, Attachments, Tags (potentially twice) and Cues */
65 #define MAX_SEEKHEAD_ENTRIES 7
66 
67 /* Largest known-length EBML length */
68 #define MAX_EBML_LENGTH ((1ULL << 56) - 2)
69 /* The dynamic buffer API we rely upon has a limit of INT_MAX;
70  * and so has avio_write(). */
71 #define MAX_SUPPORTED_EBML_LENGTH FFMIN(MAX_EBML_LENGTH, INT_MAX)
72 
73 #define MODE_MATROSKAv2 0x01
74 #define MODE_WEBM 0x02
75 
76 #define IS_WEBM(mkv) (CONFIG_WEBM_MUXER && CONFIG_MATROSKA_MUXER ? \
77  ((mkv)->mode == MODE_WEBM) : CONFIG_WEBM_MUXER)
78 #define IS_SEEKABLE(pb, mkv) (((pb)->seekable & AVIO_SEEKABLE_NORMAL) && \
79  !(mkv)->is_live)
80 
81 enum {
85 };
86 
87 typedef struct ebml_master {
88  int64_t pos; ///< absolute offset in the containing AVIOContext where the master's elements start
89  int sizebytes; ///< how many bytes were reserved for the size
90 } ebml_master;
91 
92 typedef struct ebml_stored_master {
94  int64_t pos;
96 
97 typedef enum EbmlType {
105  EBML_BLOCK, ///< pseudo-type for writing (Simple)Blocks
107 } EbmlType;
108 
109 typedef struct BlockContext {
110  struct mkv_track *track;
111  const AVPacket *pkt;
112  int16_t rel_ts;
113  uint8_t flags;
115 } BlockContext;
116 
117 typedef struct EbmlMaster {
118  int nb_elements; ///< -1 if not finished
119  int containing_master; ///< -1 if no parent exists
120 } EbmlMaster;
121 
122 typedef struct EbmlElement {
123  uint32_t id;
125  unsigned length_size;
126  uint64_t size; ///< excluding id and length field
127  union {
128  uint64_t uint;
129  int64_t sint;
130  double f;
131  const char *str;
132  const uint8_t *bin;
133  struct MatroskaMuxContext *mkv; ///< used by EBML_BLOCK
135  } priv;
136 } EbmlElement;
137 
138 typedef struct EbmlWriter {
139  unsigned nb_elements;
142 } EbmlWriter;
143 
144 #define EBML_WRITER(max_nb_elems) \
145  EbmlElement elements[max_nb_elems]; \
146  EbmlWriter writer = (EbmlWriter){ .elements = elements, \
147  .current_master_element = -1 }
148 
149 typedef struct mkv_seekhead_entry {
150  uint32_t elementid;
151  uint64_t segmentpos;
153 
154 typedef struct mkv_seekhead {
155  int64_t filepos;
159 } mkv_seekhead;
160 
161 typedef struct mkv_cuepoint {
162  uint64_t pts;
164  int64_t cluster_pos; ///< offset of the cluster containing the block relative to the segment
165  int64_t relative_pos; ///< relative offset from the position of the cluster containing the block
166  int64_t duration; ///< duration of the block according to time base
167 } mkv_cuepoint;
168 
169 typedef struct mkv_cues {
172 } mkv_cues;
173 
174 struct MatroskaMuxContext;
175 
176 typedef struct mkv_track {
178  int has_cue;
179  uint64_t uid;
180  unsigned track_num;
183  unsigned offset;
185  int64_t last_timestamp;
186  int64_t duration;
189  unsigned codecpriv_size; ///< size reserved for CodecPrivate excluding header+length field
190  int64_t ts_offset;
191  /* This callback will be called twice: First with a NULL AVIOContext
192  * to return the size of the (Simple)Block's data via size
193  * and a second time with the AVIOContext set when the data
194  * shall be written.
195  * The callback shall not return an error on the second call. */
197  const AVPacket *, int *size);
198 } mkv_track;
199 
200 typedef struct MatroskaMuxContext {
201  const AVClass *class;
203 
204  int mode;
208  int64_t segment_offset;
210  int64_t cluster_pos; ///< file offset of the current Cluster
211  int64_t cluster_pts;
213  int64_t duration;
217  int64_t cues_pos;
218 
220 
221  /* Used as temporary buffer to use the minimal amount of bytes
222  * to write the length field of EBML Masters.
223  * Every user has to reset the buffer after using it and
224  * different uses may not overlap. It is currently used in
225  * mkv_write_tag(), in mkv_assemble_cues() as well as in
226  * mkv_update_codecprivate() and mkv_write_track(). */
228 
230 
231  unsigned nb_attachments;
233 
236 
241  int is_live;
242 
243  int is_dash;
249 
250  uint32_t segment_uid[4];
252 
253 /** 2 bytes * 3 for EBML IDs, 3 1-byte EBML lengths, 8 bytes for 64 bit
254  * offset, 4 bytes for target EBML ID */
255 #define MAX_SEEKENTRY_SIZE 21
256 
257 /** 4 * (1-byte EBML ID, 1-byte EBML size, 8-byte uint max) */
258 #define MAX_CUETRACKPOS_SIZE 40
259 
260 /** 2 + 1 Simpletag header, 2 + 1 + 8 Name "DURATION", 23B for TagString */
261 #define DURATION_SIMPLETAG_SIZE (2 + 1 + (2 + 1 + 8) + 23)
262 
263 /** Seek preroll value for opus */
264 #define OPUS_SEEK_PREROLL 80000000
265 
266 static int ebml_id_size(uint32_t id)
267 {
268  return (av_log2(id) + 7U) / 8;
269 }
270 
271 static void put_ebml_id(AVIOContext *pb, uint32_t id)
272 {
273  int i = ebml_id_size(id);
274  while (i--)
275  avio_w8(pb, (uint8_t)(id >> (i * 8)));
276 }
277 
278 /**
279  * Write an EBML size meaning "unknown size".
280  *
281  * @param bytes The number of bytes the size should occupy (maximum: 8).
282  */
283 static void put_ebml_size_unknown(AVIOContext *pb, int bytes)
284 {
285  av_assert0(bytes <= 8);
286  avio_w8(pb, 0x1ff >> bytes);
287  if (av_builtin_constant_p(bytes) && bytes == 1)
288  return;
289  ffio_fill(pb, 0xff, bytes - 1);
290 }
291 
292 /**
293  * Returns how many bytes are needed to represent a number
294  * as EBML variable length integer.
295  */
296 static int ebml_num_size(uint64_t num)
297 {
298  int bytes = 0;
299  do {
300  bytes++;
301  } while (num >>= 7);
302  return bytes;
303 }
304 
305 /**
306  * Calculate how many bytes are needed to represent the length field
307  * of an EBML element whose payload has a given length.
308  */
309 static int ebml_length_size(uint64_t length)
310 {
311  return ebml_num_size(length + 1);
312 }
313 
314 /**
315  * Write a number as EBML variable length integer on `bytes` bytes.
316  * `bytes` is taken literally without checking.
317  */
318 static void put_ebml_num(AVIOContext *pb, uint64_t num, int bytes)
319 {
320  num |= 1ULL << bytes * 7;
321  for (int i = bytes - 1; i >= 0; i--)
322  avio_w8(pb, (uint8_t)(num >> i * 8));
323 }
324 
325 /**
326  * Write a length as EBML variable length integer.
327  *
328  * @param bytes The number of bytes that need to be used to write the number.
329  * If zero, the minimal number of bytes will be used.
330  */
331 static void put_ebml_length(AVIOContext *pb, uint64_t length, int bytes)
332 {
333  int needed_bytes = ebml_length_size(length);
334 
335  // sizes larger than this are currently undefined in EBML
336  av_assert0(length < (1ULL << 56) - 1);
337 
338  if (bytes == 0)
339  bytes = needed_bytes;
340  // The bytes needed to write the given size must not exceed
341  // the bytes that we ought to use.
342  av_assert0(bytes >= needed_bytes);
343  put_ebml_num(pb, length, bytes);
344 }
345 
346 /**
347  * Write a (random) UID with fixed size to make the output more deterministic
348  */
349 static void put_ebml_uid(AVIOContext *pb, uint32_t elementid, uint64_t uid)
350 {
351  put_ebml_id(pb, elementid);
352  put_ebml_length(pb, 8, 0);
353  avio_wb64(pb, uid);
354 }
355 
356 static void put_ebml_uint(AVIOContext *pb, uint32_t elementid, uint64_t val)
357 {
358  int i, bytes = 1;
359  uint64_t tmp = val;
360  while (tmp >>= 8)
361  bytes++;
362 
363  put_ebml_id(pb, elementid);
364  put_ebml_length(pb, bytes, 0);
365  for (i = bytes - 1; i >= 0; i--)
366  avio_w8(pb, (uint8_t)(val >> i * 8));
367 }
368 
369 static void put_ebml_float(AVIOContext *pb, uint32_t elementid, double val)
370 {
371  put_ebml_id(pb, elementid);
372  put_ebml_length(pb, 8, 0);
374 }
375 
376 static void put_ebml_binary(AVIOContext *pb, uint32_t elementid,
377  const void *buf, int size)
378 {
379  put_ebml_id(pb, elementid);
380  put_ebml_length(pb, size, 0);
381  avio_write(pb, buf, size);
382 }
383 
384 static void put_ebml_string(AVIOContext *pb, uint32_t elementid,
385  const char *str)
386 {
387  put_ebml_binary(pb, elementid, str, strlen(str));
388 }
389 
390 /**
391  * Write a void element of a given size. Useful for reserving space in
392  * the file to be written to later.
393  *
394  * @param size The number of bytes to reserve, which must be at least 2.
395  */
396 static void put_ebml_void(AVIOContext *pb, int size)
397 {
398  av_assert0(size >= 2);
399 
401  // we need to subtract the length needed to store the size from the
402  // size we need to reserve so 2 cases, we use 8 bytes to store the
403  // size if possible, 1 byte otherwise
404  if (size < 10) {
405  size -= 2;
406  put_ebml_length(pb, size, 0);
407  } else {
408  size -= 9;
409  put_ebml_length(pb, size, 8);
410  }
411  ffio_fill(pb, 0, size);
412 }
413 
414 static ebml_master start_ebml_master(AVIOContext *pb, uint32_t elementid,
415  uint64_t expectedsize)
416 {
417  int bytes = expectedsize ? ebml_length_size(expectedsize) : 8;
418 
419  put_ebml_id(pb, elementid);
420  put_ebml_size_unknown(pb, bytes);
421  return (ebml_master) { avio_tell(pb), bytes };
422 }
423 
425 {
426  int64_t pos = avio_tell(pb);
427 
428  if (avio_seek(pb, master.pos - master.sizebytes, SEEK_SET) < 0)
429  return;
430  put_ebml_length(pb, pos - master.pos, master.sizebytes);
431  avio_seek(pb, pos, SEEK_SET);
432 }
433 
435  uint32_t id, EbmlType type)
436 {
437  writer->elements[writer->nb_elements].id = id;
438  writer->elements[writer->nb_elements].type = type;
439  return &writer->elements[writer->nb_elements++];
440 }
441 
442 static void ebml_writer_open_master(EbmlWriter *writer, uint32_t id)
443 {
444  EbmlElement *const elem = ebml_writer_add(writer, id, EBML_MASTER);
445  EbmlMaster *const master = &elem->priv.master;
446 
447  master->containing_master = writer->current_master_element;
448  master->nb_elements = -1;
449 
450  writer->current_master_element = writer->nb_elements - 1;
451 }
452 
454 {
455  EbmlElement *elem;
456  av_assert2(writer->current_master_element >= 0);
457  av_assert2(writer->current_master_element < writer->nb_elements);
458  elem = &writer->elements[writer->current_master_element];
459  av_assert2(elem->type == EBML_MASTER);
460  av_assert2(elem->priv.master.nb_elements < 0); /* means unset */
461  elem->priv.master.nb_elements = writer->nb_elements - writer->current_master_element - 1;
465 }
466 
468 {
469  av_assert2(writer->nb_elements > 0);
470  av_assert2(0 <= writer->current_master_element);
471  av_assert2(writer->current_master_element < writer->nb_elements);
472  if (writer->current_master_element == writer->nb_elements - 1) {
473  const EbmlElement *const elem = &writer->elements[writer->nb_elements - 1];
474  /* The master element has no children. Discard it. */
475  av_assert2(elem->type == EBML_MASTER);
479  writer->nb_elements--;
480  return;
481  }
482  ebml_writer_close_master(writer);
483 }
484 
485 static void ebml_writer_add_string(EbmlWriter *writer, uint32_t id,
486  const char *str)
487 {
488  EbmlElement *const elem = ebml_writer_add(writer, id, EBML_STR);
489 
490  elem->priv.str = str;
491 }
492 
493 static void ebml_writer_add_bin(EbmlWriter *writer, uint32_t id,
494  const uint8_t *data, size_t size)
495 {
496  EbmlElement *const elem = ebml_writer_add(writer, id, EBML_BIN);
497 
498 #if SIZE_MAX > UINT64_MAX
499  size = FFMIN(size, UINT64_MAX);
500 #endif
501  elem->size = size;
502  elem->priv.bin = data;
503 }
504 
505 static void ebml_writer_add_float(EbmlWriter *writer, uint32_t id,
506  double val)
507 {
508  EbmlElement *const elem = ebml_writer_add(writer, id, EBML_FLOAT);
509 
510  elem->priv.f = val;
511 }
512 
513 static void ebml_writer_add_uid(EbmlWriter *writer, uint32_t id,
514  uint64_t val)
515 {
516  EbmlElement *const elem = ebml_writer_add(writer, id, EBML_UID);
517  elem->priv.uint = val;
518 }
519 
520 static void ebml_writer_add_uint(EbmlWriter *writer, uint32_t id,
521  uint64_t val)
522 {
523  EbmlElement *elem = ebml_writer_add(writer, id, EBML_UINT);
524  elem->priv.uint = val;
525 }
526 
527 static void ebml_writer_add_sint(EbmlWriter *writer, uint32_t id,
528  int64_t val)
529 {
530  EbmlElement *elem = ebml_writer_add(writer, id, EBML_SINT);
531  elem->priv.sint = val;
532 }
533 
535 {
537  elem->priv.mkv = mkv;
538 }
539 
541 {
542  size_t len = strlen(elem->priv.str);
543 #if SIZE_MAX > UINT64_MAX
544  len = FF_MIN(len, UINT64_MAX);
545 #endif
546  elem->size = len;
547  return 0;
548 }
549 
550 static av_const int uint_size(uint64_t val)
551 {
552  int bytes = 0;
553  do {
554  bytes++;
555  } while (val >>= 8);
556  return bytes;
557 }
558 
560 {
561  elem->size = uint_size(elem->priv.uint);
562  return 0;
563 }
564 
565 static av_const int sint_size(int64_t val)
566 {
567  uint64_t tmp = 2 * (uint64_t)(val < 0 ? val^-1 : val);
568  return uint_size(tmp);
569 }
570 
572 {
573  elem->size = sint_size(elem->priv.sint);
574  return 0;
575 }
576 
577 static int ebml_writer_elem_len(EbmlWriter *writer, EbmlElement *elem,
578  int remaining_elems);
579 
581  int remaining_elems)
582 {
583  int nb_elems = elem->priv.master.nb_elements >= 0 ? elem->priv.master.nb_elements : remaining_elems - 1;
584  EbmlElement *const master = elem;
585  uint64_t total_size = 0;
586 
587  master->priv.master.nb_elements = nb_elems;
588  for (; elem++, nb_elems > 0;) {
589  int ret = ebml_writer_elem_len(writer, elem, nb_elems);
590  if (ret < 0)
591  return ret;
592  av_assert2(ret < nb_elems);
593  /* No overflow is possible here, as both total_size and elem->size
594  * are bounded by MAX_SUPPORTED_EBML_LENGTH. */
595  total_size += ebml_id_size(elem->id) + elem->length_size + elem->size;
596  if (total_size > MAX_SUPPORTED_EBML_LENGTH)
597  return AVERROR(ERANGE);
598  nb_elems--; /* consume elem */
599  elem += ret, nb_elems -= ret; /* and elem's children */
600  }
601  master->size = total_size;
602 
603  return master->priv.master.nb_elements;
604 }
605 
607 {
608  MatroskaMuxContext *const mkv = elem->priv.mkv;
609  BlockContext *const block = &mkv->cur_block;
610  mkv_track *const track = block->track;
611  const AVPacket *const pkt = block->pkt;
612  int err, size;
613 
614  if (track->reformat) {
615  err = track->reformat(mkv, NULL, pkt, &size);
616  if (err < 0) {
617  av_log(mkv->ctx, AV_LOG_ERROR, "Error when reformatting data of "
618  "a packet from stream %d.\n", pkt->stream_index);
619  return err;
620  }
621  } else {
622  size = pkt->size;
623  if (track->offset <= size)
624  size -= track->offset;
625  }
626  elem->size = track->track_num_size + 3U + size;
627 
628  return 0;
629 }
630 
631 static void ebml_writer_write_block(const EbmlElement *elem, AVIOContext *pb)
632 {
633  MatroskaMuxContext *const mkv = elem->priv.mkv;
634  BlockContext *const block = &mkv->cur_block;
635  mkv_track *const track = block->track;
636  const AVPacket *const pkt = block->pkt;
637 
638  put_ebml_num(pb, track->track_num, track->track_num_size);
639  avio_wb16(pb, block->rel_ts);
640  avio_w8(pb, block->flags);
641 
642  if (track->reformat) {
643  int size;
644  track->reformat(mkv, pb, pkt, &size);
645  } else {
646  const uint8_t *data = pkt->data;
647  unsigned offset = track->offset <= pkt->size ? track->offset : 0;
648  avio_write(pb, data + offset, pkt->size - offset);
649  }
650 }
651 
652 static int ebml_writer_elem_len(EbmlWriter *writer, EbmlElement *elem,
653  int remaining_elems)
654 {
655  int ret = 0;
656 
657  switch (elem->type) {
658  case EBML_FLOAT:
659  case EBML_UID:
660  elem->size = 8;
661  break;
662  case EBML_STR:
663  ret = ebml_writer_str_len(elem);
664  break;
665  case EBML_UINT:
666  ret = ebml_writer_uint_len(elem);
667  break;
668  case EBML_SINT:
669  ret = ebml_writer_sint_len(elem);
670  break;
671  case EBML_BLOCK:
672  ret = ebml_writer_block_len(elem);
673  break;
674  case EBML_MASTER:
675  ret = ebml_writer_master_len(writer, elem, remaining_elems);
676  break;
677  }
678  if (ret < 0)
679  return ret;
680  if (elem->size > MAX_SUPPORTED_EBML_LENGTH)
681  return AVERROR(ERANGE);
682  elem->length_size = ebml_length_size(elem->size);
683  return ret; /* number of elements consumed excluding elem itself */
684 }
685 
686 static int ebml_writer_elem_write(const EbmlElement *elem, AVIOContext *pb)
687 {
688  put_ebml_id(pb, elem->id);
689  put_ebml_num(pb, elem->size, elem->length_size);
690  switch (elem->type) {
691  case EBML_UID:
692  case EBML_FLOAT: {
693  uint64_t val = elem->type == EBML_UID ? elem->priv.uint
694  : av_double2int(elem->priv.f);
695  avio_wb64(pb, val);
696  break;
697  }
698  case EBML_UINT:
699  case EBML_SINT: {
700  uint64_t val = elem->type == EBML_UINT ? elem->priv.uint
701  : elem->priv.sint;
702  for (int i = elem->size; --i >= 0; )
703  avio_w8(pb, (uint8_t)(val >> i * 8));
704  break;
705  }
706  case EBML_STR:
707  case EBML_BIN: {
708  const uint8_t *data = elem->type == EBML_BIN ? elem->priv.bin
709  : (const uint8_t*)elem->priv.str;
710  avio_write(pb, data, elem->size);
711  break;
712  }
713  case EBML_BLOCK:
714  ebml_writer_write_block(elem, pb);
715  break;
716  case EBML_MASTER: {
717  int nb_elems = elem->priv.master.nb_elements;
718 
719  elem++;
720  for (int i = 0; i < nb_elems; i++)
721  i += ebml_writer_elem_write(elem + i, pb);
722 
723  return nb_elems;
724  }
725  }
726  return 0;
727 }
728 
729 static int ebml_writer_write(EbmlWriter *writer, AVIOContext *pb)
730 {
731  int ret = ebml_writer_elem_len(writer, writer->elements,
732  writer->nb_elements);
733  if (ret < 0)
734  return ret;
735  ebml_writer_elem_write(writer->elements, pb);
736  return 0;
737 }
738 
739 static void mkv_add_seekhead_entry(MatroskaMuxContext *mkv, uint32_t elementid,
740  uint64_t filepos)
741 {
742  mkv_seekhead *seekhead = &mkv->seekhead;
743 
745 
746  seekhead->entries[seekhead->num_entries].elementid = elementid;
747  seekhead->entries[seekhead->num_entries++].segmentpos = filepos - mkv->segment_offset;
748 }
749 
751 {
752  int ret;
753 
754  if (!*dyn_cp && (ret = avio_open_dyn_buf(dyn_cp)) < 0)
755  return ret;
756 
757  if (mkv->write_crc)
758  put_ebml_void(*dyn_cp, 6); /* Reserve space for CRC32 so position/size calculations using avio_tell() take it into account */
759 
760  return 0;
761 }
762 
764  MatroskaMuxContext *mkv, uint32_t id,
765  int length_size, int keep_buffer,
766  int add_seekentry)
767 {
768  uint8_t *buf, crc[4];
769  int ret, size, skip = 0;
770 
771  size = avio_get_dyn_buf(*dyn_cp, &buf);
772  if ((ret = (*dyn_cp)->error) < 0)
773  goto fail;
774 
775  if (add_seekentry)
776  mkv_add_seekhead_entry(mkv, id, avio_tell(pb));
777 
778  put_ebml_id(pb, id);
779  put_ebml_length(pb, size, length_size);
780  if (mkv->write_crc) {
781  skip = 6; /* Skip reserved 6-byte long void element from the dynamic buffer. */
782  AV_WL32(crc, av_crc(av_crc_get_table(AV_CRC_32_IEEE_LE), UINT32_MAX, buf + skip, size - skip) ^ UINT32_MAX);
783  put_ebml_binary(pb, EBML_ID_CRC32, crc, sizeof(crc));
784  }
785  avio_write(pb, buf + skip, size - skip);
786 
787 fail:
788  if (keep_buffer) {
789  ffio_reset_dyn_buf(*dyn_cp);
790  } else {
791  ffio_free_dyn_buf(dyn_cp);
792  }
793  return ret;
794 }
795 
796 /**
797  * Output EBML master. Keep the buffer if seekable, allowing for later updates.
798  * Furthermore always add a SeekHead Entry for this element.
799  */
801  ebml_stored_master *elem,
802  MatroskaMuxContext *mkv, uint32_t id)
803 {
804  if (IS_SEEKABLE(pb, mkv)) {
805  uint8_t *buf;
806  int size = avio_get_dyn_buf(elem->bc, &buf);
807 
808  if (elem->bc->error < 0)
809  return elem->bc->error;
810 
811  elem->pos = avio_tell(pb);
812  mkv_add_seekhead_entry(mkv, id, elem->pos);
813 
814  put_ebml_id(pb, id);
815  put_ebml_length(pb, size, 0);
816  avio_write(pb, buf, size);
817 
818  return 0;
819  } else
820  return end_ebml_master_crc32(pb, &elem->bc, mkv, id, 0, 0, 1);
821 }
822 
823 static void put_xiph_size(AVIOContext *pb, int size)
824 {
825  ffio_fill(pb, 255, size / 255);
826  avio_w8(pb, size % 255);
827 }
828 
829 /**
830  * Free the members allocated in the mux context.
831  */
833 {
834  MatroskaMuxContext *mkv = s->priv_data;
835 
837  ffio_free_dyn_buf(&mkv->info.bc);
838  ffio_free_dyn_buf(&mkv->track.bc);
839  ffio_free_dyn_buf(&mkv->tags.bc);
840  ffio_free_dyn_buf(&mkv->tmp_bc);
841 
843  av_freep(&mkv->cues.entries);
844  av_freep(&mkv->tracks);
845 }
846 
847 /**
848  * Initialize the SeekHead element to be ready to index level 1 Matroska
849  * elements. Enough space to write MAX_SEEKHEAD_ENTRIES SeekHead entries
850  * will be reserved at the current file location.
851  */
853 {
854  mkv->seekhead.filepos = avio_tell(pb);
855  // 21 bytes max for a Seek entry, 6 bytes max for the SeekHead ID
856  // and size, 6 bytes for a CRC32 element, and 2 bytes to guarantee
857  // that an EBML void element will fit afterwards
860 }
861 
862 /**
863  * Write the SeekHead to the file at the location reserved for it
864  * and seek to destpos afterwards. When error_on_seek_failure
865  * is not set, failure to seek to the position designated for the
866  * SeekHead is not considered an error and it is presumed that
867  * destpos is the current position; failure to seek to destpos
868  * afterwards is always an error.
869  *
870  * @return 0 on success, < 0 on error.
871  */
873  int error_on_seek_failure, int64_t destpos)
874 {
875  AVIOContext *dyn_cp = NULL;
876  mkv_seekhead *seekhead = &mkv->seekhead;
877  int64_t remaining, ret64;
878  int i, ret;
879 
880  if ((ret64 = avio_seek(pb, seekhead->filepos, SEEK_SET)) < 0)
881  return error_on_seek_failure ? ret64 : 0;
882 
883  ret = start_ebml_master_crc32(&dyn_cp, mkv);
884  if (ret < 0)
885  return ret;
886 
887  for (i = 0; i < seekhead->num_entries; i++) {
888  mkv_seekhead_entry *entry = &seekhead->entries[i];
891 
893  put_ebml_length(dyn_cp, ebml_id_size(entry->elementid), 0);
894  put_ebml_id(dyn_cp, entry->elementid);
895 
897  end_ebml_master(dyn_cp, seekentry);
898  }
899  ret = end_ebml_master_crc32(pb, &dyn_cp, mkv,
900  MATROSKA_ID_SEEKHEAD, 0, 0, 0);
901  if (ret < 0)
902  return ret;
903 
904  remaining = seekhead->filepos + seekhead->reserved_size - avio_tell(pb);
905  put_ebml_void(pb, remaining);
906 
907  if ((ret64 = avio_seek(pb, destpos, SEEK_SET)) < 0)
908  return ret64;
909 
910  return 0;
911 }
912 
913 static int mkv_add_cuepoint(MatroskaMuxContext *mkv, int stream, int64_t ts,
914  int64_t cluster_pos, int64_t relative_pos, int64_t duration)
915 {
916  mkv_cues *cues = &mkv->cues;
917  mkv_cuepoint *entries = cues->entries;
918  unsigned idx = cues->num_entries;
919 
920  if (ts < 0)
921  return 0;
922 
923  entries = av_realloc_array(entries, cues->num_entries + 1, sizeof(mkv_cuepoint));
924  if (!entries)
925  return AVERROR(ENOMEM);
926  cues->entries = entries;
927 
928  /* Make sure the cues entries are sorted by pts. */
929  while (idx > 0 && entries[idx - 1].pts > ts)
930  idx--;
931  memmove(&entries[idx + 1], &entries[idx],
932  (cues->num_entries - idx) * sizeof(entries[0]));
933 
934  entries[idx].pts = ts;
935  entries[idx].stream_idx = stream;
936  entries[idx].cluster_pos = cluster_pos - mkv->segment_offset;
937  entries[idx].relative_pos = relative_pos;
938  entries[idx].duration = duration;
939 
940  cues->num_entries++;
941 
942  return 0;
943 }
944 
945 static int mkv_assemble_cues(AVStream **streams, AVIOContext *dyn_cp, AVIOContext *cuepoint,
946  const mkv_cues *cues, mkv_track *tracks, int num_tracks,
947  uint64_t offset)
948 {
949  for (mkv_cuepoint *entry = cues->entries, *end = entry + cues->num_entries;
950  entry < end;) {
951  uint64_t pts = entry->pts;
952  uint8_t *buf;
953  int size;
954 
956 
957  // put all the entries from different tracks that have the exact same
958  // timestamp into the same CuePoint
959  for (int j = 0; j < num_tracks; j++)
960  tracks[j].has_cue = 0;
961  do {
962  ebml_master track_positions;
963  int idx = entry->stream_idx;
964 
965  av_assert0(idx >= 0 && idx < num_tracks);
966  if (tracks[idx].has_cue && streams[idx]->codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE)
967  continue;
968  tracks[idx].has_cue = 1;
970  put_ebml_uint(cuepoint, MATROSKA_ID_CUETRACK , tracks[idx].track_num);
971  put_ebml_uint(cuepoint, MATROSKA_ID_CUECLUSTERPOSITION , entry->cluster_pos + offset);
972  put_ebml_uint(cuepoint, MATROSKA_ID_CUERELATIVEPOSITION, entry->relative_pos);
973  if (entry->duration > 0)
974  put_ebml_uint(cuepoint, MATROSKA_ID_CUEDURATION , entry->duration);
975  end_ebml_master(cuepoint, track_positions);
976  } while (++entry < end && entry->pts == pts);
977  size = avio_get_dyn_buf(cuepoint, &buf);
978  if (cuepoint->error < 0)
979  return cuepoint->error;
981  ffio_reset_dyn_buf(cuepoint);
982  }
983 
984  return 0;
985 }
986 
988  const AVCodecParameters *par,
989  const uint8_t *extradata, int extradata_size)
990 {
991  const uint8_t *header_start[3];
992  int header_len[3];
993  int first_header_size;
994  int err, j;
995 
996  if (par->codec_id == AV_CODEC_ID_VORBIS)
997  first_header_size = 30;
998  else
999  first_header_size = 42;
1000 
1001  err = avpriv_split_xiph_headers(extradata, extradata_size,
1002  first_header_size, header_start, header_len);
1003  if (err < 0) {
1004  av_log(s, AV_LOG_ERROR, "Extradata corrupt.\n");
1005  return err;
1006  }
1007 
1008  avio_w8(pb, 2); // number packets - 1
1009  for (j = 0; j < 2; j++) {
1010  put_xiph_size(pb, header_len[j]);
1011  }
1012  for (j = 0; j < 3; j++)
1013  avio_write(pb, header_start[j], header_len[j]);
1014 
1015  return 0;
1016 }
1017 
1018 #if CONFIG_MATROSKA_MUXER
1019 static int put_wv_codecpriv(AVIOContext *pb, const uint8_t *extradata, int extradata_size)
1020 {
1021  if (extradata && extradata_size == 2)
1022  avio_write(pb, extradata, 2);
1023  else
1024  avio_wl16(pb, 0x410); // fallback to the most recent version
1025  return 0;
1026 }
1027 
1028 static int put_flac_codecpriv(AVFormatContext *s, AVIOContext *pb,
1029  const AVCodecParameters *par,
1030  const uint8_t *extradata, int extradata_size)
1031 {
1032  int write_comment = (par->ch_layout.order == AV_CHANNEL_ORDER_NATIVE &&
1033  !(par->ch_layout.u.mask & ~0x3ffffULL) &&
1035  int ret = ff_flac_write_header(pb, extradata, extradata_size,
1036  !write_comment);
1037 
1038  if (ret < 0)
1039  return ret;
1040 
1041  if (write_comment) {
1042  const char *vendor = (s->flags & AVFMT_FLAG_BITEXACT) ?
1043  "Lavf" : LIBAVFORMAT_IDENT;
1044  AVDictionary *dict = NULL;
1045  uint8_t buf[32];
1046  int64_t len;
1047 
1048  snprintf(buf, sizeof(buf), "0x%"PRIx64, par->ch_layout.u.mask);
1049  av_dict_set(&dict, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", buf, 0);
1050 
1051  len = ff_vorbiscomment_length(dict, vendor, NULL, 0);
1052  av_assert1(len < (1 << 24) - 4);
1053 
1054  avio_w8(pb, 0x84);
1055  avio_wb24(pb, len);
1056 
1057  ff_vorbiscomment_write(pb, dict, vendor, NULL, 0);
1058 
1059  av_dict_free(&dict);
1060  }
1061 
1062  return 0;
1063 }
1064 
1065 static int get_aac_sample_rates(AVFormatContext *s, MatroskaMuxContext *mkv,
1066  const uint8_t *extradata, int extradata_size,
1067  int *sample_rate, int *output_sample_rate)
1068 {
1069  MPEG4AudioConfig mp4ac;
1070  int ret;
1071 
1072  ret = avpriv_mpeg4audio_get_config2(&mp4ac, extradata, extradata_size, 1, s);
1073  /* Don't abort if the failure is because of missing extradata. Assume in that
1074  * case a bitstream filter will provide the muxer with the extradata in the
1075  * first packet.
1076  * Abort however if s->pb is not seekable, as we would not be able to seek back
1077  * to write the sample rate elements once the extradata shows up, anyway. */
1078  if (ret < 0 && (extradata_size || !IS_SEEKABLE(s->pb, mkv))) {
1080  "Error parsing AAC extradata, unable to determine samplerate.\n");
1081  return AVERROR(EINVAL);
1082  }
1083 
1084  if (ret < 0) {
1085  /* This will only happen when this function is called while writing the
1086  * header and no extradata is available. The space for this element has
1087  * to be reserved for when this function is called again after the
1088  * extradata shows up in the first packet, as there's no way to know if
1089  * output_sample_rate will be different than sample_rate or not. */
1090  *output_sample_rate = *sample_rate;
1091  } else {
1092  *sample_rate = mp4ac.sample_rate;
1093  *output_sample_rate = mp4ac.ext_sample_rate;
1094  }
1095  return 0;
1096 }
1097 #endif
1098 
1100  const AVCodecParameters *par,
1101  const uint8_t *extradata,
1102  int extradata_size,
1103  unsigned *size_to_reserve)
1104 {
1105  switch (par->codec_id) {
1106  case AV_CODEC_ID_VORBIS:
1107  case AV_CODEC_ID_THEORA:
1108  return put_xiph_codecpriv(s, dyn_cp, par, extradata, extradata_size);
1109  case AV_CODEC_ID_AV1:
1110  if (extradata_size)
1111  return ff_isom_write_av1c(dyn_cp, extradata,
1112  extradata_size, 1);
1113  else
1114  *size_to_reserve = (AV1_SANE_SEQUENCE_HEADER_MAX_BITS + 7) / 8 + 100;
1115  break;
1116 #if CONFIG_MATROSKA_MUXER
1117  case AV_CODEC_ID_FLAC:
1118  return put_flac_codecpriv(s, dyn_cp, par, extradata, extradata_size);
1119  case AV_CODEC_ID_WAVPACK:
1120  return put_wv_codecpriv(dyn_cp, extradata, extradata_size);
1121  case AV_CODEC_ID_H264:
1122  return ff_isom_write_avcc(dyn_cp, extradata,
1123  extradata_size);
1124  case AV_CODEC_ID_HEVC:
1125  return ff_isom_write_hvcc(dyn_cp, extradata,
1126  extradata_size, 0);
1127  case AV_CODEC_ID_ALAC:
1128  if (extradata_size < 36) {
1130  "Invalid extradata found, ALAC expects a 36-byte "
1131  "QuickTime atom.");
1132  return AVERROR_INVALIDDATA;
1133  } else
1134  avio_write(dyn_cp, extradata + 12,
1135  extradata_size - 12);
1136  break;
1137  case AV_CODEC_ID_AAC:
1138  if (extradata_size)
1139  avio_write(dyn_cp, extradata, extradata_size);
1140  else
1141  *size_to_reserve = MAX_PCE_SIZE;
1142  break;
1143 #endif
1144  default:
1145  if (CONFIG_MATROSKA_MUXER && par->codec_id == AV_CODEC_ID_PRORES &&
1147  avio_wl32(dyn_cp, par->codec_tag);
1148  } else if (extradata_size && par->codec_id != AV_CODEC_ID_TTA)
1149  avio_write(dyn_cp, extradata, extradata_size);
1150  }
1151 
1152  return 0;
1153 }
1154 
1156  AVCodecParameters *par,
1157  const uint8_t *extradata, int extradata_size,
1158  int native_id, int qt_id,
1159  uint8_t **codecpriv, int *codecpriv_size,
1160  unsigned *max_payload_size)
1161 {
1162  MatroskaMuxContext av_unused *const mkv = s->priv_data;
1163  unsigned size_to_reserve = 0;
1164  int ret;
1165 
1166  if (native_id) {
1167  ret = mkv_assemble_native_codecprivate(s, dyn_cp, par,
1168  extradata, extradata_size,
1169  &size_to_reserve);
1170  if (ret < 0)
1171  return ret;
1172 #if CONFIG_MATROSKA_MUXER
1173  } else if (par->codec_type == AVMEDIA_TYPE_VIDEO) {
1174  if (qt_id) {
1175  if (!par->codec_tag)
1177  par->codec_id);
1179  && (!extradata_size || ff_codec_get_id(ff_codec_movvideo_tags, AV_RL32(extradata + 4)) != par->codec_id)
1180  ) {
1181  avio_wb32(dyn_cp, 0x5a + extradata_size);
1182  avio_wl32(dyn_cp, par->codec_tag);
1183  ffio_fill(dyn_cp, 0, 0x5a - 8);
1184  }
1185  avio_write(dyn_cp, extradata, extradata_size);
1186  } else {
1188  av_log(s, AV_LOG_WARNING, "codec %s is not supported by this format\n",
1189  avcodec_get_name(par->codec_id));
1190 
1191  if (!par->codec_tag)
1193  par->codec_id);
1194  if (!par->codec_tag && par->codec_id != AV_CODEC_ID_RAWVIDEO) {
1195  av_log(s, AV_LOG_ERROR, "No bmp codec tag found for codec %s\n",
1196  avcodec_get_name(par->codec_id));
1197  return AVERROR(EINVAL);
1198  }
1199 
1200  /* If vfw extradata updates are supported, this will have
1201  * to be updated to pass extradata(_size) explicitly. */
1202  ff_put_bmp_header(dyn_cp, par, 0, 0, mkv->flipped_raw_rgb);
1203  }
1204  } else if (par->codec_type == AVMEDIA_TYPE_AUDIO) {
1205  unsigned int tag;
1207  if (!tag) {
1208  av_log(s, AV_LOG_ERROR, "No wav codec tag found for codec %s\n",
1209  avcodec_get_name(par->codec_id));
1210  return AVERROR(EINVAL);
1211  }
1212  if (!par->codec_tag)
1213  par->codec_tag = tag;
1214 
1215  /* Same comment as for ff_put_bmp_header applies here. */
1217 #endif
1218  }
1219 
1220  *codecpriv_size = avio_get_dyn_buf(dyn_cp, codecpriv);
1221  if (dyn_cp->error < 0)
1222  return dyn_cp->error;
1223  *max_payload_size = *codecpriv_size + size_to_reserve;
1224 
1225  return 0;
1226 }
1227 
1228 static void mkv_put_codecprivate(AVIOContext *pb, unsigned max_payload_size,
1229  const uint8_t *codecpriv, unsigned codecpriv_size)
1230 {
1231  unsigned total_codecpriv_size = 0, total_size;
1232 
1233  av_assert1(codecpriv_size <= max_payload_size);
1234 
1235  if (!max_payload_size)
1236  return;
1237 
1238  total_size = 2 + ebml_length_size(max_payload_size) + max_payload_size;
1239 
1240  if (codecpriv_size) {
1241  unsigned length_size = ebml_length_size(codecpriv_size);
1242 
1243  total_codecpriv_size = 2U + length_size + codecpriv_size;
1244  if (total_codecpriv_size + 1 == total_size) {
1245  /* It is impossible to add one byte of padding via an EBML Void. */
1246  length_size++;
1247  total_codecpriv_size++;
1248  }
1250  put_ebml_length(pb, codecpriv_size, length_size);
1251  avio_write(pb, codecpriv, codecpriv_size);
1252  }
1253  if (total_codecpriv_size < total_size)
1254  put_ebml_void(pb, total_size - total_codecpriv_size);
1255 }
1256 
1258  uint8_t *side_data, int side_data_size,
1259  AVCodecParameters *par, AVIOContext *pb,
1260  mkv_track *track, unsigned alternative_size)
1261 {
1262  AVIOContext *const dyn_bc = mkv->tmp_bc;
1263  uint8_t *codecpriv;
1264  unsigned max_payload_size;
1265  int ret, codecpriv_size;
1266 
1267  ret = mkv_assemble_codecprivate(s, dyn_bc, par,
1268  side_data, side_data_size, 1, 0,
1269  &codecpriv, &codecpriv_size, &max_payload_size);
1270  if (ret < 0)
1271  goto fail;
1272  if (codecpriv_size > track->codecpriv_size && !alternative_size) {
1273  ret = AVERROR(ENOSPC);
1274  goto fail;
1275  } else if (codecpriv_size > track->codecpriv_size) {
1276  av_assert1(alternative_size < track->codecpriv_size);
1277  codecpriv_size = alternative_size;
1278  }
1279  avio_seek(pb, track->codecpriv_offset, SEEK_SET);
1281  codecpriv, codecpriv_size);
1282 
1283  if (!par->extradata_size) {
1284  ret = ff_alloc_extradata(par, side_data_size);
1285  if (ret < 0)
1286  goto fail;
1287  memcpy(par->extradata, side_data, side_data_size);
1288  }
1289 fail:
1290  ffio_reset_dyn_buf(dyn_bc);
1291  return ret;
1292 }
1293 
1294 #define MAX_VIDEO_COLOR_ELEMS 20
1295 static void mkv_write_video_color(EbmlWriter *writer, const AVStream *st,
1296  const AVCodecParameters *par)
1297 {
1298  const void *side_data;
1299 
1301 
1302  if (par->color_trc != AVCOL_TRC_UNSPECIFIED &&
1303  par->color_trc < AVCOL_TRC_NB) {
1305  par->color_trc);
1306  }
1307  if (par->color_space != AVCOL_SPC_UNSPECIFIED &&
1308  par->color_space < AVCOL_SPC_NB) {
1310  par->color_space);
1311  }
1312  if (par->color_primaries != AVCOL_PRI_UNSPECIFIED &&
1313  par->color_primaries < AVCOL_PRI_NB) {
1315  par->color_primaries);
1316  }
1317  if (par->color_range != AVCOL_RANGE_UNSPECIFIED &&
1318  par->color_range < AVCOL_RANGE_NB) {
1320  }
1323  int xpos, ypos;
1324 
1325  avcodec_enum_to_chroma_pos(&xpos, &ypos, par->chroma_location);
1327  (xpos >> 7) + 1);
1329  (ypos >> 7) + 1);
1330  }
1331 
1333  NULL);
1334  if (side_data) {
1335  const AVContentLightMetadata *metadata = side_data;
1337  metadata->MaxCLL);
1339  metadata->MaxFALL);
1340  }
1341 
1343  NULL);
1344  if (side_data) {
1345  const AVMasteringDisplayMetadata *metadata = side_data;
1347  if (metadata->has_primaries) {
1349  av_q2d(metadata->display_primaries[0][0]));
1351  av_q2d(metadata->display_primaries[0][1]));
1353  av_q2d(metadata->display_primaries[1][0]));
1355  av_q2d(metadata->display_primaries[1][1]));
1357  av_q2d(metadata->display_primaries[2][0]));
1359  av_q2d(metadata->display_primaries[2][1]));
1361  av_q2d(metadata->white_point[0]));
1363  av_q2d(metadata->white_point[1]));
1364  }
1365  if (metadata->has_luminance) {
1367  av_q2d(metadata->max_luminance));
1369  av_q2d(metadata->min_luminance));
1370  }
1372  }
1373 
1375 }
1376 
1377 #define MAX_VIDEO_PROJECTION_ELEMS 6
1379  const AVStream *st, uint8_t private[])
1380 {
1381  const AVSphericalMapping *spherical =
1383  NULL);
1384 
1385  if (!spherical)
1386  return;
1387 
1388  if (spherical->projection != AV_SPHERICAL_EQUIRECTANGULAR &&
1390  spherical->projection != AV_SPHERICAL_CUBEMAP) {
1391  av_log(s, AV_LOG_WARNING, "Unknown projection type\n");
1392  return;
1393  }
1394 
1396 
1397  switch (spherical->projection) {
1402  AV_WB32(private, 0); // version + flags
1403  if (spherical->projection == AV_SPHERICAL_EQUIRECTANGULAR) {
1404  AV_WB32(private + 4, 0);
1405  AV_WB32(private + 8, 0);
1406  AV_WB32(private + 12, 0);
1407  AV_WB32(private + 16, 0);
1408  } else {
1409  AV_WB32(private + 4, spherical->bound_top);
1410  AV_WB32(private + 8, spherical->bound_bottom);
1411  AV_WB32(private + 12, spherical->bound_left);
1412  AV_WB32(private + 16, spherical->bound_right);
1413  }
1415  private, 20);
1416  break;
1417  case AV_SPHERICAL_CUBEMAP:
1420  AV_WB32(private, 0); // version + flags
1421  AV_WB32(private + 4, 0); // layout
1422  AV_WB32(private + 8, spherical->padding);
1424  private, 12);
1425  break;
1426  default:
1427  av_assert0(0);
1428  }
1429 
1430  if (spherical->yaw)
1432  (double) spherical->yaw / (1 << 16));
1433  if (spherical->pitch)
1435  (double) spherical->pitch / (1 << 16));
1436  if (spherical->roll)
1438  (double) spherical->roll / (1 << 16));
1439 
1440  ebml_writer_close_master(writer);
1441 }
1442 
1443 #define MAX_FIELD_ORDER_ELEMS 2
1444 static void mkv_write_field_order(EbmlWriter *writer, int is_webm,
1445  enum AVFieldOrder field_order)
1446 {
1447  switch (field_order) {
1448  case AV_FIELD_UNKNOWN:
1449  break;
1450  case AV_FIELD_PROGRESSIVE:
1453  break;
1454  case AV_FIELD_TT:
1455  case AV_FIELD_BB:
1456  case AV_FIELD_TB:
1457  case AV_FIELD_BT:
1460  if (!is_webm) {
1461  switch (field_order) {
1462  case AV_FIELD_TT:
1465  break;
1466  case AV_FIELD_BB:
1469  break;
1470  case AV_FIELD_TB:
1473  break;
1474  case AV_FIELD_BT:
1477  break;
1478  }
1479  }
1480  }
1481 }
1482 
1483 #define MAX_STEREO_MODE_ELEMS 1
1485  AVStream *st, int is_webm,
1486  int *h_width, int *h_height)
1487 {
1488  const AVDictionaryEntry *tag;
1490  const AVStereo3D *stereo;
1491 
1492  *h_width = 1;
1493  *h_height = 1;
1494  // convert metadata into proper side data and add it to the stream
1495  if ((tag = av_dict_get(st->metadata, "stereo_mode", NULL, 0)) ||
1496  (tag = av_dict_get( s->metadata, "stereo_mode", NULL, 0))) {
1497  int stereo_mode = atoi(tag->value);
1498 
1499  for (int i = 0; i < MATROSKA_VIDEO_STEREOMODE_TYPE_NB; i++)
1500  if (!strcmp(tag->value, ff_matroska_video_stereo_mode[i])){
1501  stereo_mode = i;
1502  break;
1503  }
1504 
1505  if (stereo_mode < MATROSKA_VIDEO_STEREOMODE_TYPE_NB &&
1506  stereo_mode != 10 && stereo_mode != 12) {
1507  int ret = ff_mkv_stereo3d_conv(st, stereo_mode);
1508  if (ret < 0)
1509  return ret;
1510  }
1511  }
1512 
1514  NULL);
1515  if (stereo) {
1516  switch (stereo->type) {
1517  case AV_STEREO3D_2D:
1519  break;
1521  format = (stereo->flags & AV_STEREO3D_FLAG_INVERT)
1524  *h_width = 2;
1525  break;
1526  case AV_STEREO3D_TOPBOTTOM:
1528  if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
1529  format--;
1530  *h_height = 2;
1531  break;
1534  if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
1535  format--;
1536  break;
1537  case AV_STEREO3D_LINES:
1539  if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
1540  format--;
1541  *h_height = 2;
1542  break;
1543  case AV_STEREO3D_COLUMNS:
1545  if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
1546  format--;
1547  *h_width = 2;
1548  break;
1551  if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
1552  format++;
1553  break;
1554  }
1555  }
1556 
1558  return 0;
1559 
1560  // if webm, do not write unsupported modes
1561  if ((is_webm &&
1566  "The specified stereo mode is not valid.\n");
1567  return AVERROR(EINVAL);
1568  }
1569 
1570  // write StereoMode if format is valid
1572 
1573  return 0;
1574 }
1575 
1577 {
1578 #if CONFIG_MATROSKA_MUXER
1581 
1582  if (dovi && dovi->dv_profile <= 10) {
1583  ebml_master mapping;
1584  uint8_t buf[ISOM_DVCC_DVVC_SIZE];
1585  uint32_t type;
1586 
1587  uint64_t expected_size = (2 + 1 + (sizeof(DVCC_DVVC_BLOCK_TYPE_NAME) - 1))
1588  + (2 + 1 + 4) + (2 + 1 + ISOM_DVCC_DVVC_SIZE);
1589 
1590  if (dovi->dv_profile > 7) {
1591  type = MKBETAG('d', 'v', 'v', 'C');
1592  } else {
1593  type = MKBETAG('d', 'v', 'c', 'C');
1594  }
1595 
1596  ff_isom_put_dvcc_dvvc(s, buf, dovi);
1597 
1598  mapping = start_ebml_master(pb, MATROSKA_ID_TRACKBLKADDMAPPING, expected_size);
1599 
1602  put_ebml_binary(pb, MATROSKA_ID_BLKADDIDEXTRADATA, buf, sizeof(buf));
1603 
1604  end_ebml_master(pb, mapping);
1605  }
1606 #endif
1607 }
1608 
1610  AVStream *st, const AVCodecParameters *par,
1611  AVIOContext *pb)
1612 {
1613  const AVDictionaryEntry *tag;
1614  int display_width_div = 1, display_height_div = 1;
1615  uint8_t color_space[4], projection_private[20];
1618  int ret;
1619 
1621 
1624 
1625  mkv_write_field_order(&writer, IS_WEBM(mkv), par->field_order);
1626 
1627  // check both side data and metadata for stereo information,
1628  // write the result to the bitstream if any is found
1629  ret = mkv_write_stereo_mode(s, &writer, st, IS_WEBM(mkv),
1630  &display_width_div,
1631  &display_height_div);
1632  if (ret < 0)
1633  return ret;
1634 
1635  if (((tag = av_dict_get(st->metadata, "alpha_mode", NULL, 0)) && atoi(tag->value)) ||
1636  ((tag = av_dict_get( s->metadata, "alpha_mode", NULL, 0)) && atoi(tag->value)) ||
1637  (par->format == AV_PIX_FMT_YUVA420P)) {
1639  }
1640 
1641  // write DisplayWidth and DisplayHeight, they contain the size of
1642  // a single source view and/or the display aspect ratio
1643  if (st->sample_aspect_ratio.num) {
1644  int64_t d_width = av_rescale(par->width, st->sample_aspect_ratio.num, st->sample_aspect_ratio.den);
1645  if (d_width > INT_MAX) {
1646  av_log(s, AV_LOG_ERROR, "Overflow in display width\n");
1647  return AVERROR(EINVAL);
1648  }
1649  if (d_width != par->width || display_width_div != 1 || display_height_div != 1) {
1650  if (IS_WEBM(mkv) || display_width_div != 1 || display_height_div != 1) {
1652  d_width / display_width_div);
1654  par->height / display_height_div);
1655  } else {
1656  AVRational display_aspect_ratio;
1657  av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1658  par->width * (int64_t)st->sample_aspect_ratio.num,
1659  par->height * (int64_t)st->sample_aspect_ratio.den,
1660  1024 * 1024);
1662  display_aspect_ratio.num);
1664  display_aspect_ratio.den);
1667  }
1668  }
1669  } else if (display_width_div != 1 || display_height_div != 1) {
1671  par->width / display_width_div);
1673  par->height / display_height_div);
1674  } else if (!IS_WEBM(mkv))
1677 
1678  if (par->codec_id == AV_CODEC_ID_RAWVIDEO) {
1679  AV_WL32(color_space, par->codec_tag);
1681  color_space, sizeof(color_space));
1682  }
1683  mkv_write_video_color(&writer, st, par);
1684  mkv_write_video_projection(s, &writer, st, projection_private);
1685 
1686  return ebml_writer_write(&writer, pb);
1687 }
1688 
1690  AVStream *st, mkv_track *track, AVIOContext *pb,
1691  int is_default)
1692 {
1693  AVCodecParameters *par = st->codecpar;
1694  ebml_master subinfo, track_master;
1695  int native_id = 0;
1696  int qt_id = 0;
1697  int bit_depth;
1698  int sample_rate = par->sample_rate;
1699  int output_sample_rate = 0;
1700  int j, ret;
1701  const AVDictionaryEntry *tag;
1702 
1703  if (par->codec_type == AVMEDIA_TYPE_ATTACHMENT)
1704  return 0;
1705 
1706  track_master = start_ebml_master(pb, MATROSKA_ID_TRACKENTRY, 0);
1708  put_ebml_uid (pb, MATROSKA_ID_TRACKUID, track->uid);
1709  put_ebml_uint(pb, MATROSKA_ID_TRACKFLAGLACING, 0); // no lacing (yet)
1710 
1711  if ((tag = av_dict_get(st->metadata, "title", NULL, 0)))
1713  tag = av_dict_get(st->metadata, "language", NULL, 0);
1715  tag && tag->value[0] ? tag->value : "und");
1716 
1717  // The default value for TRACKFLAGDEFAULT is 1, so add element
1718  // if we need to clear it.
1719  if (!is_default)
1721 
1724 
1725  if (IS_WEBM(mkv)) {
1726  const char *codec_id;
1727  if (par->codec_type != AVMEDIA_TYPE_SUBTITLE) {
1728  for (j = 0; ff_webm_codec_tags[j].id != AV_CODEC_ID_NONE; j++) {
1729  if (ff_webm_codec_tags[j].id == par->codec_id) {
1731  native_id = 1;
1732  break;
1733  }
1734  }
1735  } else if (par->codec_id == AV_CODEC_ID_WEBVTT) {
1737  codec_id = "D_WEBVTT/CAPTIONS";
1738  native_id = MATROSKA_TRACK_TYPE_SUBTITLE;
1739  } else if (st->disposition & AV_DISPOSITION_DESCRIPTIONS) {
1740  codec_id = "D_WEBVTT/DESCRIPTIONS";
1741  native_id = MATROSKA_TRACK_TYPE_METADATA;
1742  } else if (st->disposition & AV_DISPOSITION_METADATA) {
1743  codec_id = "D_WEBVTT/METADATA";
1744  native_id = MATROSKA_TRACK_TYPE_METADATA;
1745  } else {
1746  codec_id = "D_WEBVTT/SUBTITLES";
1747  native_id = MATROSKA_TRACK_TYPE_SUBTITLE;
1748  }
1749  }
1750 
1751  if (!native_id) {
1753  "Only VP8 or VP9 or AV1 video and Vorbis or Opus audio and WebVTT subtitles are supported for WebM.\n");
1754  return AVERROR(EINVAL);
1755  }
1756 
1758  } else {
1770 
1771  // look for a codec ID string specific to mkv to use,
1772  // if none are found, use AVI codes
1773  if (par->codec_id != AV_CODEC_ID_RAWVIDEO || par->codec_tag) {
1774  for (j = 0; ff_mkv_codec_tags[j].id != AV_CODEC_ID_NONE; j++) {
1775  if (ff_mkv_codec_tags[j].id == par->codec_id && par->codec_id != AV_CODEC_ID_FFV1) {
1777  native_id = 1;
1778  break;
1779  }
1780  }
1781  } else {
1782  if (mkv->allow_raw_vfw) {
1783  native_id = 0;
1784  } else {
1785  av_log(s, AV_LOG_ERROR, "Raw RGB is not supported Natively in Matroska, you can use AVI or NUT or\n"
1786  "If you would like to store it anyway using VFW mode, enable allow_raw_vfw (-allow_raw_vfw 1)\n");
1787  return AVERROR(EINVAL);
1788  }
1789  }
1790  }
1791 
1792  switch (par->codec_type) {
1793  case AVMEDIA_TYPE_VIDEO:
1794  mkv->have_video = 1;
1796 
1797  if( st->avg_frame_rate.num > 0 && st->avg_frame_rate.den > 0
1798  && av_cmp_q(av_inv_q(st->avg_frame_rate), st->time_base) > 0)
1800  else if( st->r_frame_rate.num > 0 && st->r_frame_rate.den > 0
1801  && av_cmp_q(av_inv_q(st->r_frame_rate), st->time_base) > 0)
1803 
1804  if (CONFIG_MATROSKA_MUXER && !native_id &&
1807  par->codec_id == AV_CODEC_ID_SVQ1 ||
1808  par->codec_id == AV_CODEC_ID_SVQ3 ||
1809  par->codec_id == AV_CODEC_ID_CINEPAK))
1810  qt_id = 1;
1811 
1812  if (qt_id)
1813  put_ebml_string(pb, MATROSKA_ID_CODECID, "V_QUICKTIME");
1814  else if (!native_id) {
1815  // if there is no mkv-specific codec ID, use VFW mode
1816  put_ebml_string(pb, MATROSKA_ID_CODECID, "V_MS/VFW/FOURCC");
1817  track->write_dts = 1;
1819  }
1820 
1821  ret = mkv_write_track_video(s, mkv, st, par, pb);
1822  if (ret < 0)
1823  return ret;
1824 
1825  if (!IS_WEBM(mkv))
1826  mkv_write_dovi(s, pb, st);
1827 
1828  break;
1829 
1830  case AVMEDIA_TYPE_AUDIO:
1831  if (par->initial_padding && par->codec_id == AV_CODEC_ID_OPUS) {
1832  int64_t codecdelay = av_rescale_q(par->initial_padding,
1833  (AVRational){ 1, 48000 },
1834  (AVRational){ 1, 1000000000 });
1835  if (codecdelay < 0) {
1836  av_log(s, AV_LOG_ERROR, "Initial padding is invalid\n");
1837  return AVERROR(EINVAL);
1838  }
1839 // track->ts_offset = av_rescale_q(par->initial_padding,
1840 // (AVRational){ 1, par->sample_rate },
1841 // st->time_base);
1842 
1843  put_ebml_uint(pb, MATROSKA_ID_CODECDELAY, codecdelay);
1844  }
1845  if (par->codec_id == AV_CODEC_ID_OPUS)
1847 #if CONFIG_MATROSKA_MUXER
1848  else if (par->codec_id == AV_CODEC_ID_AAC) {
1849  ret = get_aac_sample_rates(s, mkv, par->extradata, par->extradata_size,
1850  &sample_rate, &output_sample_rate);
1851  if (ret < 0)
1852  return ret;
1853  }
1854 #endif
1855 
1857 
1858  if (!native_id)
1859  // no mkv-specific ID, use ACM mode
1860  put_ebml_string(pb, MATROSKA_ID_CODECID, "A_MS/ACM");
1861 
1862  subinfo = start_ebml_master(pb, MATROSKA_ID_TRACKAUDIO, 6 + 4 * 9);
1864 
1865  track->sample_rate_offset = avio_tell(pb);
1867  if (output_sample_rate)
1868  put_ebml_float(pb, MATROSKA_ID_AUDIOOUTSAMPLINGFREQ, output_sample_rate);
1869 
1871  if (!bit_depth && par->codec_id != AV_CODEC_ID_ADPCM_G726) {
1872  if (par->bits_per_raw_sample)
1874  else
1876  }
1877  if (!bit_depth)
1879  if (bit_depth)
1881  end_ebml_master(pb, subinfo);
1882  break;
1883 
1884  case AVMEDIA_TYPE_SUBTITLE:
1885  if (!native_id) {
1886  av_log(s, AV_LOG_ERROR, "Subtitle codec %d is not supported.\n", par->codec_id);
1887  return AVERROR(ENOSYS);
1888  }
1889  if (!IS_WEBM(mkv) && st->disposition & AV_DISPOSITION_DESCRIPTIONS)
1891 
1892  if (!IS_WEBM(mkv) || par->codec_id != AV_CODEC_ID_WEBVTT)
1893  native_id = MATROSKA_TRACK_TYPE_SUBTITLE;
1894 
1895  put_ebml_uint(pb, MATROSKA_ID_TRACKTYPE, native_id);
1896  break;
1897  default:
1898  av_log(s, AV_LOG_ERROR, "Only audio, video, and subtitles are supported for Matroska.\n");
1899  return AVERROR(EINVAL);
1900  }
1901 
1902  if (!IS_WEBM(mkv) || par->codec_id != AV_CODEC_ID_WEBVTT) {
1903  uint8_t *codecpriv;
1904  int codecpriv_size, max_payload_size;
1905  track->codecpriv_offset = avio_tell(pb);
1906  ret = mkv_assemble_codecprivate(s, mkv->tmp_bc, par,
1907  par->extradata, par->extradata_size,
1908  native_id, qt_id,
1909  &codecpriv, &codecpriv_size, &max_payload_size);
1910  if (ret < 0)
1911  goto fail;
1912  mkv_put_codecprivate(pb, max_payload_size, codecpriv, codecpriv_size);
1913  track->codecpriv_size = max_payload_size;
1914  }
1915 
1916  end_ebml_master(pb, track_master);
1917  ret = 0;
1918 fail:
1919  ffio_reset_dyn_buf(mkv->tmp_bc);
1920 
1921  return ret;
1922 }
1923 
1925 {
1926  MatroskaMuxContext *mkv = s->priv_data;
1927  AVIOContext *pb = s->pb;
1928  int video_default_idx = -1, audio_default_idx = -1, subtitle_default_idx = -1;
1929  int i, ret;
1930 
1931  if (mkv->nb_attachments == s->nb_streams)
1932  return 0;
1933 
1934  ret = start_ebml_master_crc32(&mkv->track.bc, mkv);
1935  if (ret < 0)
1936  return ret;
1937 
1938  if (mkv->default_mode != DEFAULT_MODE_PASSTHROUGH) {
1939  int video_idx = -1, audio_idx = -1, subtitle_idx = -1;
1940 
1941  for (i = s->nb_streams - 1; i >= 0; i--) {
1942  AVStream *st = s->streams[i];
1943 
1944  switch (st->codecpar->codec_type) {
1945 #define CASE(type, variable) \
1946  case AVMEDIA_TYPE_ ## type: \
1947  variable ## _idx = i; \
1948  if (st->disposition & AV_DISPOSITION_DEFAULT) \
1949  variable ## _default_idx = i; \
1950  break;
1951  CASE(VIDEO, video)
1952  CASE(AUDIO, audio)
1953  CASE(SUBTITLE, subtitle)
1954 #undef CASE
1955  }
1956  }
1957 
1958  video_default_idx = FFMAX(video_default_idx, video_idx);
1959  audio_default_idx = FFMAX(audio_default_idx, audio_idx);
1961  subtitle_default_idx = FFMAX(subtitle_default_idx, subtitle_idx);
1962  }
1963  for (i = 0; i < s->nb_streams; i++) {
1964  AVStream *st = s->streams[i];
1965  int is_default = st->disposition & AV_DISPOSITION_DEFAULT ||
1966  i == video_default_idx || i == audio_default_idx ||
1967  i == subtitle_default_idx;
1968  ret = mkv_write_track(s, mkv, st, &mkv->tracks[i],
1969  mkv->track.bc, is_default);
1970  if (ret < 0)
1971  return ret;
1972  }
1973 
1974  return end_ebml_master_crc32_tentatively(pb, &mkv->track, mkv,
1976 }
1977 
1979 {
1980  EBML_WRITER(4);
1981  uint8_t *key = av_strdup(t->key);
1982  uint8_t *p = key;
1983  const uint8_t *lang = NULL;
1984  int ret;
1985 
1986  if (!key)
1987  return AVERROR(ENOMEM);
1988 
1989  if ((p = strrchr(p, '-')) &&
1990  (lang = ff_convert_lang_to(p + 1, AV_LANG_ISO639_2_BIBL)))
1991  *p = 0;
1992 
1993  p = key;
1994  while (*p) {
1995  if (*p == ' ')
1996  *p = '_';
1997  else if (*p >= 'a' && *p <= 'z')
1998  *p -= 'a' - 'A';
1999  p++;
2000  }
2001 
2004  if (lang)
2007  ret = ebml_writer_write(&writer, pb);
2008 
2009  av_freep(&key);
2010  return ret;
2011 }
2012 
2014  uint32_t elementid, uint64_t uid)
2015 {
2017  4 + 1 + 8);
2018  if (elementid)
2019  put_ebml_uid(pb, elementid, uid);
2020  end_ebml_master(pb, targets);
2021 }
2022 
2023 static int mkv_check_tag_name(const char *name, uint32_t elementid)
2024 {
2025  return av_strcasecmp(name, "title") &&
2026  av_strcasecmp(name, "stereo_mode") &&
2027  av_strcasecmp(name, "creation_time") &&
2028  av_strcasecmp(name, "encoding_tool") &&
2029  av_strcasecmp(name, "duration") &&
2030  (elementid != MATROSKA_ID_TAGTARGETS_TRACKUID ||
2031  av_strcasecmp(name, "language")) &&
2032  (elementid != MATROSKA_ID_TAGTARGETS_ATTACHUID ||
2033  (av_strcasecmp(name, "filename") &&
2034  av_strcasecmp(name, "mimetype")));
2035 }
2036 
2038  AVIOContext **pb, unsigned reserved_size,
2039  uint32_t elementid, uint64_t uid)
2040 {
2041  const AVDictionaryEntry *t = NULL;
2042  AVIOContext *const tmp_bc = mkv->tmp_bc;
2043  uint8_t *buf;
2044  int ret = 0, size, tag_written = 0;
2045 
2046  mkv_write_tag_targets(mkv, tmp_bc, elementid, uid);
2047 
2048  while ((t = av_dict_get(m, "", t, AV_DICT_IGNORE_SUFFIX))) {
2049  if (mkv_check_tag_name(t->key, elementid)) {
2050  ret = mkv_write_simpletag(tmp_bc, t);
2051  if (ret < 0)
2052  goto end;
2053  tag_written = 1;
2054  }
2055  }
2056  if (reserved_size)
2057  put_ebml_void(tmp_bc, reserved_size);
2058  else if (!tag_written)
2059  goto end;
2060 
2061  size = avio_get_dyn_buf(tmp_bc, &buf);
2062  if (tmp_bc->error) {
2063  ret = tmp_bc->error;
2064  goto end;
2065  }
2066  if (!*pb) {
2067  ret = start_ebml_master_crc32(pb, mkv);
2068  if (ret < 0)
2069  goto end;
2070  }
2071  put_ebml_binary(*pb, MATROSKA_ID_TAG, buf, size);
2072 
2073 end:
2074  ffio_reset_dyn_buf(tmp_bc);
2075  return ret;
2076 }
2077 
2079 {
2080  MatroskaMuxContext *mkv = s->priv_data;
2081  int i, ret, seekable = IS_SEEKABLE(s->pb, mkv);
2082 
2083  mkv->wrote_tags = 1;
2084 
2086 
2087  ret = mkv_write_tag(mkv, s->metadata, &mkv->tags.bc, 0, 0, 0);
2088  if (ret < 0)
2089  return ret;
2090 
2091  for (i = 0; i < s->nb_streams; i++) {
2092  const AVStream *st = s->streams[i];
2093  mkv_track *track = &mkv->tracks[i];
2094 
2096  continue;
2097 
2098  ret = mkv_write_tag(mkv, st->metadata, &mkv->tags.bc,
2099  seekable ? DURATION_SIMPLETAG_SIZE : 0,
2101  if (ret < 0)
2102  return ret;
2103  if (seekable)
2105  }
2106 
2107  if (mkv->nb_attachments && !IS_WEBM(mkv)) {
2108  for (i = 0; i < s->nb_streams; i++) {
2109  const mkv_track *track = &mkv->tracks[i];
2110  const AVStream *st = s->streams[i];
2111 
2113  continue;
2114 
2115  ret = mkv_write_tag(mkv, st->metadata, &mkv->tags.bc, 0,
2117  if (ret < 0)
2118  return ret;
2119  }
2120  }
2121 
2122  if (mkv->tags.bc) {
2123  return end_ebml_master_crc32_tentatively(s->pb, &mkv->tags, mkv,
2125  }
2126  return 0;
2127 }
2128 
2130 {
2131  for (unsigned i = 0; i < s->nb_chapters; i++) {
2132  if (!s->chapters[i]->id)
2133  return 1;
2134  for (unsigned j = 0; j < i; j++)
2135  if (s->chapters[j]->id == s->chapters[i]->id)
2136  return 1;
2137  }
2138  return 0;
2139 }
2140 
2142 {
2143  MatroskaMuxContext *mkv = s->priv_data;
2144  AVIOContext *dyn_cp = NULL, *dyn_tags = NULL, **tags, *pb = s->pb;
2145  ebml_master editionentry;
2146  AVRational scale = {1, 1E9};
2147  int ret, create_new_ids;
2148 
2149  if (!s->nb_chapters || mkv->wrote_chapters)
2150  return 0;
2151 
2152  ret = start_ebml_master_crc32(&dyn_cp, mkv);
2153  if (ret < 0)
2154  return ret;
2155 
2156  editionentry = start_ebml_master(dyn_cp, MATROSKA_ID_EDITIONENTRY, 0);
2157  if (!IS_WEBM(mkv)) {
2159  /* If mkv_write_tags() has already been called, then any tags
2160  * corresponding to chapters will be put into a new Tags element. */
2161  tags = mkv->wrote_tags ? &dyn_tags : &mkv->tags.bc;
2162  } else
2163  tags = NULL;
2164 
2165  create_new_ids = mkv_new_chapter_ids_needed(s);
2166 
2167  for (unsigned i = 0; i < s->nb_chapters; i++) {
2168  AVChapter *const c = s->chapters[i];
2169  int64_t chapterstart = av_rescale_q(c->start, c->time_base, scale);
2170  int64_t chapterend = av_rescale_q(c->end, c->time_base, scale);
2171  const AVDictionaryEntry *t;
2172  uint64_t uid = create_new_ids ? i + 1ULL : c->id;
2173  EBML_WRITER(7);
2174 
2175  if (chapterstart < 0 || chapterstart > chapterend || chapterend < 0) {
2177  "Invalid chapter start (%"PRId64") or end (%"PRId64").\n",
2178  chapterstart, chapterend);
2180  goto fail;
2181  }
2182 
2185  ebml_writer_add_uint(&writer, MATROSKA_ID_CHAPTERTIMESTART, chapterstart);
2186  ebml_writer_add_uint(&writer, MATROSKA_ID_CHAPTERTIMEEND, chapterend);
2187  if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
2190  ebml_writer_add_string(&writer, MATROSKA_ID_CHAPLANG , "und");
2191  }
2192  ret = ebml_writer_write(&writer, dyn_cp);
2193  if (ret < 0)
2194  goto fail;
2195 
2196  if (tags) {
2198 
2199  ret = mkv_write_tag(mkv, c->metadata, tags, 0,
2201  if (ret < 0)
2202  goto fail;
2203  }
2204  }
2205  end_ebml_master(dyn_cp, editionentry);
2206  mkv->wrote_chapters = 1;
2207 
2208  ret = end_ebml_master_crc32(pb, &dyn_cp, mkv, MATROSKA_ID_CHAPTERS, 0, 0, 1);
2209  if (ret < 0)
2210  goto fail;
2211  if (dyn_tags)
2212  return end_ebml_master_crc32(pb, &dyn_tags, mkv,
2213  MATROSKA_ID_TAGS, 0, 0, 1);
2214  return 0;
2215 
2216 fail:
2217  if (tags) {
2218  /* tags == &mkv->tags.bc can only happen if mkv->tags.bc was
2219  * initially NULL, so we never free older tags. */
2220  ffio_free_dyn_buf(tags);
2221  }
2222  ffio_free_dyn_buf(&dyn_cp);
2223  return ret;
2224 }
2225 
2226 static const char *get_mimetype(const AVStream *st)
2227 {
2228  const AVDictionaryEntry *t;
2229 
2230  if (t = av_dict_get(st->metadata, "mimetype", NULL, 0))
2231  return t->value;
2232  if (st->codecpar->codec_id != AV_CODEC_ID_NONE) {
2234  if (desc && desc->mime_types) {
2235  return desc->mime_types[0];
2236  } else if (st->codecpar->codec_id == AV_CODEC_ID_TEXT)
2237  return "text/plain";
2238  }
2239 
2240  return NULL;
2241 }
2242 
2244 {
2245  MatroskaMuxContext *mkv = s->priv_data;
2246  AVIOContext *dyn_cp = NULL, *pb = s->pb;
2247  int i, ret;
2248 
2249  if (!mkv->nb_attachments)
2250  return 0;
2251 
2252  ret = start_ebml_master_crc32(&dyn_cp, mkv);
2253  if (ret < 0)
2254  return ret;
2255 
2256  for (i = 0; i < s->nb_streams; i++) {
2257  const AVStream *st = s->streams[i];
2258  mkv_track *track = &mkv->tracks[i];
2259  EBML_WRITER(6);
2260  const AVDictionaryEntry *t;
2261  const char *mimetype;
2262 
2264  continue;
2265 
2267 
2268  if (t = av_dict_get(st->metadata, "title", NULL, 0))
2270  if (!(t = av_dict_get(st->metadata, "filename", NULL, 0))) {
2271  av_log(s, AV_LOG_ERROR, "Attachment stream %d has no filename tag.\n", i);
2272  ffio_free_dyn_buf(&dyn_cp);
2273  return AVERROR(EINVAL);
2274  }
2276 
2277  mimetype = get_mimetype(st);
2278  av_assert0(mimetype);
2282  ebml_writer_add_uid(&writer, MATROSKA_ID_FILEUID, track->uid);
2283  ret = ebml_writer_write(&writer, dyn_cp);
2284  if (ret < 0) {
2285  ffio_free_dyn_buf(&dyn_cp);
2286  return ret;
2287  }
2288  }
2289  return end_ebml_master_crc32(pb, &dyn_cp, mkv,
2290  MATROSKA_ID_ATTACHMENTS, 0, 0, 1);
2291 }
2292 
2294 {
2295  const AVDictionaryEntry *duration = av_dict_get(s->metadata, "DURATION",
2296  NULL, 0);
2297  int64_t max = 0;
2298  int64_t us;
2299 
2300  if (duration && (av_parse_time(&us, duration->value, 1) == 0) && us > 0) {
2301  av_log(s, AV_LOG_DEBUG, "get_metadata_duration found duration in context metadata: %" PRId64 "\n", us);
2302  return us;
2303  }
2304 
2305  for (unsigned i = 0; i < s->nb_streams; i++) {
2306  int64_t us;
2307  duration = av_dict_get(s->streams[i]->metadata, "DURATION", NULL, 0);
2308 
2309  if (duration && (av_parse_time(&us, duration->value, 1) == 0))
2310  max = FFMAX(max, us);
2311  }
2312 
2313  av_log(s, AV_LOG_DEBUG, "get_metadata_duration returned: %" PRId64 "\n", max);
2314  return max;
2315 }
2316 
2318  const char *doctype, int version)
2319 {
2320  EBML_WRITER(8);
2326  ebml_writer_add_string(&writer, EBML_ID_DOCTYPE, doctype);
2329  /* The size is bounded, so no need to check this. */
2330  ebml_writer_write(&writer, pb);
2331 }
2332 
2334 {
2335  MatroskaMuxContext *mkv = s->priv_data;
2336  const AVDictionaryEntry *tag;
2337  int64_t creation_time;
2338  AVIOContext *pb;
2339  int ret = start_ebml_master_crc32(&mkv->info.bc, mkv);
2340  if (ret < 0)
2341  return ret;
2342  pb = mkv->info.bc;
2343 
2345  if ((tag = av_dict_get(s->metadata, "title", NULL, 0)))
2346  put_ebml_string(pb, MATROSKA_ID_TITLE, tag->value);
2347  if (!(s->flags & AVFMT_FLAG_BITEXACT)) {
2349  if ((tag = av_dict_get(s->metadata, "encoding_tool", NULL, 0)))
2351  else
2353 
2354  if (!IS_WEBM(mkv))
2356  } else {
2357  const char *ident = "Lavf";
2360  }
2361 
2362  if (ff_parse_creation_time_metadata(s, &creation_time, 0) > 0) {
2363  // Adjust time so it's relative to 2001-01-01 and convert to nanoseconds.
2364  int64_t date_utc = (creation_time - 978307200000000LL) * 1000;
2365  uint8_t date_utc_buf[8];
2366  AV_WB64(date_utc_buf, date_utc);
2367  put_ebml_binary(pb, MATROSKA_ID_DATEUTC, date_utc_buf, 8);
2368  }
2369 
2370  // reserve space for the duration
2371  mkv->duration = 0;
2372  mkv->duration_offset = avio_tell(pb);
2373  if (!mkv->is_live) {
2374  int64_t metadata_duration = get_metadata_duration(s);
2375 
2376  if (s->duration > 0) {
2377  int64_t scaledDuration = av_rescale(s->duration, 1000, AV_TIME_BASE);
2378  put_ebml_float(pb, MATROSKA_ID_DURATION, scaledDuration);
2379  av_log(s, AV_LOG_DEBUG, "Write early duration from recording time = %" PRIu64 "\n", scaledDuration);
2380  } else if (metadata_duration > 0) {
2381  int64_t scaledDuration = av_rescale(metadata_duration, 1000, AV_TIME_BASE);
2382  put_ebml_float(pb, MATROSKA_ID_DURATION, scaledDuration);
2383  av_log(s, AV_LOG_DEBUG, "Write early duration from metadata = %" PRIu64 "\n", scaledDuration);
2384  } else if (s->pb->seekable & AVIO_SEEKABLE_NORMAL) {
2385  put_ebml_void(pb, 11); // assumes double-precision float to be written
2386  }
2387  }
2388  return end_ebml_master_crc32_tentatively(s->pb, &mkv->info,
2389  mkv, MATROSKA_ID_INFO);
2390 }
2391 
2393 {
2394  MatroskaMuxContext *mkv = s->priv_data;
2395  AVIOContext *pb = s->pb;
2396  int ret, version = 2;
2397 
2398  ret = avio_open_dyn_buf(&mkv->tmp_bc);
2399  if (ret < 0)
2400  return ret;
2401 
2402  if (!IS_WEBM(mkv) ||
2403  av_dict_get(s->metadata, "stereo_mode", NULL, 0) ||
2404  av_dict_get(s->metadata, "alpha_mode", NULL, 0))
2405  version = 4;
2406 
2407  for (unsigned i = 0; i < s->nb_streams; i++) {
2408  if (s->streams[i]->codecpar->codec_id == AV_CODEC_ID_OPUS ||
2409  av_dict_get(s->streams[i]->metadata, "stereo_mode", NULL, 0) ||
2410  av_dict_get(s->streams[i]->metadata, "alpha_mode", NULL, 0))
2411  version = 4;
2412  }
2413 
2414  ebml_write_header(pb, s->oformat->name, version);
2416  put_ebml_size_unknown(pb, 8);
2417  mkv->segment_offset = avio_tell(pb);
2418 
2419  // We write a SeekHead at the beginning to point to all other level
2420  // one elements (except Clusters).
2421  mkv_start_seekhead(mkv, pb);
2422 
2423  ret = mkv_write_info(s);
2424  if (ret < 0)
2425  return ret;
2426 
2427  ret = mkv_write_tracks(s);
2428  if (ret < 0)
2429  return ret;
2430 
2432  if (ret < 0)
2433  return ret;
2434 
2435  if (!IS_WEBM(mkv)) {
2437  if (ret < 0)
2438  return ret;
2439  }
2440 
2441  /* Must come after mkv_write_chapters() to write chapter tags
2442  * into the same Tags element as the other tags. */
2443  ret = mkv_write_tags(s);
2444  if (ret < 0)
2445  return ret;
2446 
2447  if (!IS_SEEKABLE(pb, mkv)) {
2448  ret = mkv_write_seekhead(pb, mkv, 0, avio_tell(pb));
2449  if (ret < 0)
2450  return ret;
2451  }
2452 
2453  if (s->metadata_header_padding > 0) {
2454  if (s->metadata_header_padding == 1)
2455  s->metadata_header_padding++;
2456  put_ebml_void(pb, s->metadata_header_padding);
2457  }
2458 
2459  if (mkv->reserve_cues_space || mkv->move_cues_to_front) {
2460  if (IS_SEEKABLE(pb, mkv)) {
2461  mkv->cues_pos = avio_tell(pb);
2462  if (mkv->reserve_cues_space >= 1) {
2463  if (mkv->reserve_cues_space == 1)
2464  mkv->reserve_cues_space++;
2466  }
2467  } else
2468  mkv->reserve_cues_space = -1;
2469  }
2470 
2471  mkv->cluster_pos = -1;
2472 
2473  // start a new cluster every 5 MB or 5 sec, or 32k / 1 sec for streaming or
2474  // after 4k and on a keyframe
2475  if (IS_SEEKABLE(pb, mkv)) {
2476  if (mkv->cluster_time_limit < 0)
2477  mkv->cluster_time_limit = 5000;
2478  if (mkv->cluster_size_limit < 0)
2479  mkv->cluster_size_limit = 5 * 1024 * 1024;
2480  } else {
2481  if (mkv->cluster_time_limit < 0)
2482  mkv->cluster_time_limit = 1000;
2483  if (mkv->cluster_size_limit < 0)
2484  mkv->cluster_size_limit = 32 * 1024;
2485  }
2486 
2487  return 0;
2488 }
2489 
2490 #if CONFIG_MATROSKA_MUXER
2491 static int mkv_reformat_h2645(MatroskaMuxContext *mkv, AVIOContext *pb,
2492  const AVPacket *pkt, int *size)
2493 {
2494  int ret;
2495  if (pb) {
2497  } else {
2499  if (ret < 0)
2500  return ret;
2501  *size = ret;
2502  }
2503  return 0;
2504 }
2505 
2506 static int mkv_reformat_wavpack(MatroskaMuxContext *mkv, AVIOContext *pb,
2507  const AVPacket *pkt, int *size)
2508 {
2509  const uint8_t *src = pkt->data;
2510  int srclen = pkt->size;
2511  int offset = 0;
2512  int ret;
2513 
2514  while (srclen >= WV_HEADER_SIZE) {
2515  WvHeader header;
2516 
2518  if (ret < 0)
2519  return ret;
2520  src += WV_HEADER_SIZE;
2521  srclen -= WV_HEADER_SIZE;
2522 
2523  if (srclen < header.blocksize)
2524  return AVERROR_INVALIDDATA;
2525 
2526  offset += 4 * !!header.initial + 8 + 4 * !(header.initial && header.final);
2527  if (pb) {
2528  if (header.initial)
2529  avio_wl32(pb, header.samples);
2530  avio_wl32(pb, header.flags);
2531  avio_wl32(pb, header.crc);
2532 
2533  if (!(header.initial && header.final))
2534  avio_wl32(pb, header.blocksize);
2535 
2536  avio_write(pb, src, header.blocksize);
2537  }
2538  src += header.blocksize;
2539  srclen -= header.blocksize;
2540  offset += header.blocksize;
2541  }
2542  *size = offset;
2543 
2544  return 0;
2545 }
2546 #endif
2547 
2549  const AVPacket *pkt, int *size)
2550 {
2551  int ret = ff_av1_filter_obus(pb, pkt->data, pkt->size);
2552  if (ret < 0)
2553  return ret;
2554  *size = ret;
2555  return 0;
2556 }
2557 
2559  const AVPacket *pkt, int *size)
2560 {
2561  const uint8_t *id, *settings;
2562  size_t id_size, settings_size;
2563  unsigned total = pkt->size + 2U;
2564 
2565  if (total > INT_MAX)
2566  return AVERROR(ERANGE);
2567 
2569  &id_size);
2571  &settings_size);
2572  if (id_size > INT_MAX - total || settings_size > INT_MAX - (total += id_size))
2573  return AVERROR(ERANGE);
2574  *size = total += settings_size;
2575  if (pb) {
2576  avio_write(pb, id, id_size);
2577  avio_w8(pb, '\n');
2578  avio_write(pb, settings, settings_size);
2579  avio_w8(pb, '\n');
2580  avio_write(pb, pkt->data, pkt->size);
2581  }
2582  return 0;
2583 }
2584 
2585 static int mkv_write_block(void *logctx, MatroskaMuxContext *mkv,
2586  AVIOContext *pb, const AVCodecParameters *par,
2587  mkv_track *track, const AVPacket *pkt,
2588  int keyframe, int64_t ts, uint64_t duration,
2589  int force_blockgroup, int64_t relative_packet_pos)
2590 {
2591  uint8_t *side_data;
2592  size_t side_data_size;
2593  uint64_t additional_id;
2594  int64_t discard_padding = 0;
2595  unsigned track_number = track->track_num;
2596  EBML_WRITER(9);
2597 
2598  mkv->cur_block.track = track;
2599  mkv->cur_block.pkt = pkt;
2600  mkv->cur_block.rel_ts = ts - mkv->cluster_pts;
2601  mkv->cur_block.flags = 0;
2602 
2603  /* Open a BlockGroup with a Block now; it will later be converted
2604  * to a SimpleBlock if possible. */
2606  ebml_writer_add_block(&writer, mkv);
2607 
2608  if (duration)
2610 
2611  av_log(logctx, AV_LOG_DEBUG,
2612  "Writing block of size %d with pts %" PRId64 ", dts %" PRId64 ", "
2613  "duration %" PRId64 " at relative offset %" PRId64 " in cluster "
2614  "at offset %" PRId64 ". TrackNumber %u, keyframe %d\n",
2615  pkt->size, pkt->pts, pkt->dts, pkt->duration, relative_packet_pos,
2616  mkv->cluster_pos, track_number, keyframe != 0);
2617 
2618  side_data = av_packet_get_side_data(pkt,
2620  &side_data_size);
2621  if (side_data && side_data_size >= 10) {
2622  discard_padding = av_rescale_q(AV_RL32(side_data + 4),
2623  (AVRational){1, par->sample_rate},
2624  (AVRational){1, 1000000000});
2625  ebml_writer_add_sint(&writer, MATROSKA_ID_DISCARDPADDING, discard_padding);
2626  }
2627 
2628  side_data = av_packet_get_side_data(pkt,
2630  &side_data_size);
2631  if (side_data && side_data_size >= 8 &&
2632  // Only the Codec-specific BlockMore (id == 1) is currently supported.
2633  (additional_id = AV_RB64(side_data)) == 1) {
2636  /* Until dbc50f8a our demuxer used a wrong default value
2637  * of BlockAddID, so we write it unconditionally. */
2638  ebml_writer_add_uint(&writer, MATROSKA_ID_BLOCKADDID, additional_id);
2640  side_data + 8, side_data_size - 8);
2641  ebml_writer_close_master(&writer);
2642  ebml_writer_close_master(&writer);
2643  }
2644 
2645  if (!force_blockgroup && writer.nb_elements == 2) {
2646  /* Nothing except the BlockGroup + Block. Can use a SimpleBlock. */
2647  writer.elements++; // Skip the BlockGroup.
2648  writer.nb_elements--;
2649  av_assert2(writer.elements[0].id == MATROSKA_ID_BLOCK);
2650  writer.elements[0].id = MATROSKA_ID_SIMPLEBLOCK;
2651  if (keyframe)
2652  mkv->cur_block.flags |= 1 << 7;
2653  } else if (!keyframe)
2655  track->last_timestamp - ts);
2656 
2657  return ebml_writer_write(&writer, pb);
2658 }
2659 
2661 {
2662  MatroskaMuxContext *mkv = s->priv_data;
2663  int ret;
2664 
2665  if (!mkv->have_video) {
2666  for (unsigned i = 0; i < s->nb_streams; i++)
2667  mkv->tracks[i].has_cue = 0;
2668  }
2669  mkv->cluster_pos = -1;
2670  ret = end_ebml_master_crc32(s->pb, &mkv->cluster_bc, mkv,
2671  MATROSKA_ID_CLUSTER, 0, 1, 0);
2672  if (ret < 0)
2673  return ret;
2674 
2676  return 0;
2677 }
2678 
2680 {
2681  MatroskaMuxContext *mkv = s->priv_data;
2682  mkv_track *track = &mkv->tracks[pkt->stream_index];
2683  AVCodecParameters *par = s->streams[pkt->stream_index]->codecpar;
2684  uint8_t *side_data;
2685  size_t side_data_size;
2686  int ret;
2687 
2689  &side_data_size);
2690 
2691  switch (par->codec_id) {
2692 #if CONFIG_MATROSKA_MUXER
2693  case AV_CODEC_ID_AAC:
2694  if (side_data_size && mkv->track.bc) {
2695  int output_sample_rate = 0;
2696  ret = get_aac_sample_rates(s, mkv, side_data, side_data_size,
2697  &track->sample_rate, &output_sample_rate);
2698  if (ret < 0)
2699  return ret;
2700  if (!output_sample_rate)
2701  output_sample_rate = track->sample_rate; // Space is already reserved, so it's this or a void element.
2702  ret = mkv_update_codecprivate(s, mkv, side_data, side_data_size,
2703  par, mkv->track.bc, track, 0);
2704  if (ret < 0)
2705  return ret;
2706  avio_seek(mkv->track.bc, track->sample_rate_offset, SEEK_SET);
2708  put_ebml_float(mkv->track.bc, MATROSKA_ID_AUDIOOUTSAMPLINGFREQ, output_sample_rate);
2709  } else if (!par->extradata_size && !track->sample_rate) {
2710  // No extradata (codecpar or packet side data).
2711  av_log(s, AV_LOG_ERROR, "Error parsing AAC extradata, unable to determine samplerate.\n");
2712  return AVERROR(EINVAL);
2713  }
2714  break;
2715  case AV_CODEC_ID_FLAC:
2716  if (side_data_size && mkv->track.bc) {
2717  if (side_data_size != par->extradata_size) {
2718  av_log(s, AV_LOG_ERROR, "Invalid FLAC STREAMINFO metadata for output stream %d\n",
2719  pkt->stream_index);
2720  return AVERROR(EINVAL);
2721  }
2722  ret = mkv_update_codecprivate(s, mkv, side_data, side_data_size,
2723  par, mkv->track.bc, track, 0);
2724  if (ret < 0)
2725  return ret;
2726  }
2727  break;
2728 #endif
2729  // FIXME: Remove the following once libaom starts propagating proper extradata during init()
2730  // See https://bugs.chromium.org/p/aomedia/issues/detail?id=2208
2731  case AV_CODEC_ID_AV1:
2732  if (side_data_size && mkv->track.bc && !par->extradata_size) {
2733  // If the reserved space doesn't suffice, only write
2734  // the first four bytes of the av1C.
2735  ret = mkv_update_codecprivate(s, mkv, side_data, side_data_size,
2736  par, mkv->track.bc, track, 4);
2737  if (ret < 0)
2738  return ret;
2739  } else if (!par->extradata_size)
2740  return AVERROR_INVALIDDATA;
2741  break;
2742  default:
2743  if (side_data_size)
2744  av_log(s, AV_LOG_DEBUG, "Ignoring new extradata in a packet for stream %d.\n", pkt->stream_index);
2745  break;
2746  }
2747 
2748  return 0;
2749 }
2750 
2752 {
2753  MatroskaMuxContext *mkv = s->priv_data;
2754  AVIOContext *pb;
2755  AVCodecParameters *par = s->streams[pkt->stream_index]->codecpar;
2756  mkv_track *track = &mkv->tracks[pkt->stream_index];
2757  int is_sub = par->codec_type == AVMEDIA_TYPE_SUBTITLE;
2758  /* All subtitle blocks are considered to be keyframes. */
2759  int keyframe = is_sub || !!(pkt->flags & AV_PKT_FLAG_KEY);
2760  int64_t duration = FFMAX(pkt->duration, 0);
2761  int64_t write_duration = is_sub ? duration : 0;
2762  int ret;
2763  int64_t ts = track->write_dts ? pkt->dts : pkt->pts;
2764  int64_t relative_packet_pos;
2765 
2766  if (ts == AV_NOPTS_VALUE) {
2767  av_log(s, AV_LOG_ERROR, "Can't write packet with unknown timestamp\n");
2768  return AVERROR(EINVAL);
2769  }
2770  ts += track->ts_offset;
2771 
2772  if (mkv->cluster_pos != -1) {
2773  int64_t cluster_time = ts - mkv->cluster_pts;
2774  if ((int16_t)cluster_time != cluster_time) {
2775  ret = mkv_end_cluster(s);
2776  if (ret < 0)
2777  return ret;
2778  av_log(s, AV_LOG_WARNING, "Starting new cluster due to timestamp\n");
2779  }
2780  }
2781 
2782  if (mkv->cluster_pos == -1) {
2783  ret = start_ebml_master_crc32(&mkv->cluster_bc, mkv);
2784  if (ret < 0)
2785  return ret;
2786  mkv->cluster_bc->direct = 1;
2787  mkv->cluster_pos = avio_tell(s->pb);
2789  mkv->cluster_pts = FFMAX(0, ts);
2791  "Starting new cluster with timestamp "
2792  "%" PRId64 " at offset %" PRId64 " bytes\n",
2793  mkv->cluster_pts, mkv->cluster_pos);
2794  }
2795  pb = mkv->cluster_bc;
2796 
2797  relative_packet_pos = avio_tell(pb);
2798 
2799  /* The WebM spec requires WebVTT to be muxed in BlockGroups;
2800  * so we force it even for packets without duration. */
2801  ret = mkv_write_block(s, mkv, pb, par, track, pkt,
2802  keyframe, ts, write_duration,
2803  par->codec_id == AV_CODEC_ID_WEBVTT,
2804  relative_packet_pos);
2805  if (ret < 0)
2806  return ret;
2807  if (keyframe && IS_SEEKABLE(s->pb, mkv) &&
2808  (par->codec_type == AVMEDIA_TYPE_VIDEO ||
2810  !mkv->have_video && !track->has_cue)) {
2811  ret = mkv_add_cuepoint(mkv, pkt->stream_index, ts,
2812  mkv->cluster_pos, relative_packet_pos,
2813  write_duration);
2814  if (ret < 0)
2815  return ret;
2816  track->has_cue = 1;
2817  }
2818 
2819  track->last_timestamp = ts;
2820  mkv->duration = FFMAX(mkv->duration, ts + duration);
2821  track->duration = FFMAX(track->duration, ts + duration);
2822 
2823  return 0;
2824 }
2825 
2827 {
2828  MatroskaMuxContext *mkv = s->priv_data;
2829  int codec_type = s->streams[pkt->stream_index]->codecpar->codec_type;
2830  int keyframe = !!(pkt->flags & AV_PKT_FLAG_KEY);
2831  int cluster_size;
2832  int64_t cluster_time;
2833  int ret;
2834  int start_new_cluster;
2835 
2837  if (ret < 0)
2838  return ret;
2839 
2840  if (mkv->cluster_pos != -1) {
2841  if (mkv->tracks[pkt->stream_index].write_dts)
2842  cluster_time = pkt->dts - mkv->cluster_pts;
2843  else
2844  cluster_time = pkt->pts - mkv->cluster_pts;
2845  cluster_time += mkv->tracks[pkt->stream_index].ts_offset;
2846 
2847  cluster_size = avio_tell(mkv->cluster_bc);
2848 
2849  if (mkv->is_dash && codec_type == AVMEDIA_TYPE_VIDEO) {
2850  // WebM DASH specification states that the first block of
2851  // every Cluster has to be a key frame. So for DASH video,
2852  // we only create a Cluster on seeing key frames.
2853  start_new_cluster = keyframe;
2854  } else if (mkv->is_dash && codec_type == AVMEDIA_TYPE_AUDIO &&
2855  cluster_time > mkv->cluster_time_limit) {
2856  // For DASH audio, we create a Cluster based on cluster_time_limit.
2857  start_new_cluster = 1;
2858  } else if (!mkv->is_dash &&
2859  (cluster_size > mkv->cluster_size_limit ||
2860  cluster_time > mkv->cluster_time_limit ||
2861  (codec_type == AVMEDIA_TYPE_VIDEO && keyframe &&
2862  cluster_size > 4 * 1024))) {
2863  start_new_cluster = 1;
2864  } else
2865  start_new_cluster = 0;
2866 
2867  if (start_new_cluster) {
2868  ret = mkv_end_cluster(s);
2869  if (ret < 0)
2870  return ret;
2871  }
2872  }
2873 
2874  if (!mkv->cluster_pos)
2875  avio_write_marker(s->pb,
2876  av_rescale_q(pkt->dts, s->streams[pkt->stream_index]->time_base, AV_TIME_BASE_Q),
2878 
2879  // check if we have an audio packet cached
2880  if (mkv->cur_audio_pkt->size > 0) {
2883  if (ret < 0) {
2885  "Could not write cached audio packet ret:%d\n", ret);
2886  return ret;
2887  }
2888  }
2889 
2890  // buffer an audio packet to ensure the packet containing the video
2891  // keyframe's timecode is contained in the same cluster for WebM
2892  if (codec_type == AVMEDIA_TYPE_AUDIO) {
2893  if (pkt->size > 0)
2895  } else
2897  return ret;
2898 }
2899 
2901 {
2902  MatroskaMuxContext *mkv = s->priv_data;
2903 
2904  if (!pkt) {
2905  if (mkv->cluster_pos != -1) {
2906  int ret = mkv_end_cluster(s);
2907  if (ret < 0)
2908  return ret;
2910  "Flushing cluster at offset %" PRIu64 " bytes\n",
2911  avio_tell(s->pb));
2912  }
2913  return 1;
2914  }
2915  return mkv_write_packet(s, pkt);
2916 }
2917 
2919 {
2920  MatroskaMuxContext *mkv = s->priv_data;
2921  AVIOContext *pb = s->pb;
2922  int64_t endpos, ret64;
2923  int ret, ret2 = 0;
2924 
2925  // check if we have an audio packet cached
2926  if (mkv->cur_audio_pkt->size > 0) {
2928  if (ret < 0) {
2930  "Could not write cached audio packet ret:%d\n", ret);
2931  return ret;
2932  }
2933  }
2934 
2935  if (mkv->cluster_pos != -1) {
2936  ret = end_ebml_master_crc32(pb, &mkv->cluster_bc, mkv,
2937  MATROSKA_ID_CLUSTER, 0, 0, 0);
2938  if (ret < 0)
2939  return ret;
2940  }
2941 
2943  if (ret < 0)
2944  return ret;
2945 
2946  if (!IS_SEEKABLE(pb, mkv))
2947  return 0;
2948 
2949  endpos = avio_tell(pb);
2950 
2951  if (mkv->cues.num_entries && mkv->reserve_cues_space >= 0) {
2952  AVIOContext *cues = NULL;
2953  uint64_t size, offset = 0;
2954  int length_size = 0;
2955 
2956 redo_cues:
2957  ret = start_ebml_master_crc32(&cues, mkv);
2958  if (ret < 0)
2959  return ret;
2960 
2961  ret = mkv_assemble_cues(s->streams, cues, mkv->tmp_bc, &mkv->cues,
2962  mkv->tracks, s->nb_streams, offset);
2963  if (ret < 0) {
2964  ffio_free_dyn_buf(&cues);
2965  return ret;
2966  }
2967 
2968  if (mkv->reserve_cues_space || mkv->move_cues_to_front) {
2969  size = avio_tell(cues);
2970  length_size = ebml_length_size(size);
2971  size += 4 + length_size;
2972  if (offset + mkv->reserve_cues_space < size) {
2973  if (mkv->move_cues_to_front) {
2974  offset = size - mkv->reserve_cues_space;
2975  ffio_reset_dyn_buf(cues);
2976  goto redo_cues;
2977  }
2979  "Insufficient space reserved for Cues: "
2980  "%d < %"PRIu64". No Cues will be output.\n",
2981  mkv->reserve_cues_space, size);
2982  ret2 = AVERROR(EINVAL);
2983  goto after_cues;
2984  } else {
2985  if (offset) {
2987  offset);
2988  if (ret < 0) {
2989  ffio_free_dyn_buf(&cues);
2990  return ret;
2991  }
2992  endpos += offset;
2993  }
2994  if ((ret64 = avio_seek(pb, mkv->cues_pos, SEEK_SET)) < 0) {
2995  ffio_free_dyn_buf(&cues);
2996  return ret64;
2997  }
2998  if (mkv->reserve_cues_space == size + 1) {
2999  /* There is no way to reserve a single byte because
3000  * the minimal size of an EBML Void element is 2
3001  * (1 byte ID, 1 byte length field). This problem
3002  * is solved by writing the Cues' length field on
3003  * one byte more than necessary. */
3004  length_size++;
3005  size++;
3006  }
3007  }
3008  }
3009  ret = end_ebml_master_crc32(pb, &cues, mkv, MATROSKA_ID_CUES,
3010  length_size, 0, 1);
3011  if (ret < 0)
3012  return ret;
3013  if (mkv->reserve_cues_space) {
3014  if (size < mkv->reserve_cues_space)
3016  } else if (!mkv->move_cues_to_front)
3017  endpos = avio_tell(pb);
3018  }
3019 
3020 after_cues:
3021  /* Lengths greater than (1ULL << 56) - 1 can't be represented
3022  * via an EBML number, so leave the unknown length field. */
3023  if (endpos - mkv->segment_offset < (1ULL << 56) - 1) {
3024  if ((ret64 = avio_seek(pb, mkv->segment_offset - 8, SEEK_SET)) < 0)
3025  return ret64;
3026  put_ebml_length(pb, endpos - mkv->segment_offset, 8);
3027  }
3028 
3029  ret = mkv_write_seekhead(pb, mkv, 1, mkv->info.pos);
3030  if (ret < 0)
3031  return ret;
3032 
3033  if (mkv->info.bc) {
3034  // update the duration
3035  av_log(s, AV_LOG_DEBUG, "end duration = %" PRIu64 "\n", mkv->duration);
3036  avio_seek(mkv->info.bc, mkv->duration_offset, SEEK_SET);
3038  ret = end_ebml_master_crc32(pb, &mkv->info.bc, mkv,
3039  MATROSKA_ID_INFO, 0, 0, 0);
3040  if (ret < 0)
3041  return ret;
3042  }
3043 
3044  if (mkv->track.bc) {
3045  // write Tracks master
3046  avio_seek(pb, mkv->track.pos, SEEK_SET);
3047  ret = end_ebml_master_crc32(pb, &mkv->track.bc, mkv,
3048  MATROSKA_ID_TRACKS, 0, 0, 0);
3049  if (ret < 0)
3050  return ret;
3051  }
3052 
3053  // update stream durations
3054  if (mkv->tags.bc) {
3055  AVIOContext *tags_bc = mkv->tags.bc;
3056  int i;
3057  for (i = 0; i < s->nb_streams; ++i) {
3058  const AVStream *st = s->streams[i];
3059  const mkv_track *track = &mkv->tracks[i];
3060 
3061  if (track->duration_offset > 0) {
3062  double duration_sec = track->duration * av_q2d(st->time_base);
3063  char duration_string[20] = "";
3064  ebml_master simpletag;
3065 
3066  av_log(s, AV_LOG_DEBUG, "stream %d end duration = %" PRIu64 "\n", i,
3067  track->duration);
3068 
3069  avio_seek(tags_bc, track->duration_offset, SEEK_SET);
3070  simpletag = start_ebml_master(tags_bc, MATROSKA_ID_SIMPLETAG,
3071  2 + 1 + 8 + 23);
3072  put_ebml_string(tags_bc, MATROSKA_ID_TAGNAME, "DURATION");
3073 
3074  snprintf(duration_string, 20, "%02d:%02d:%012.9f",
3075  (int) duration_sec / 3600, ((int) duration_sec / 60) % 60,
3076  fmod(duration_sec, 60));
3077 
3078  put_ebml_binary(tags_bc, MATROSKA_ID_TAGSTRING, duration_string, 20);
3079  end_ebml_master(tags_bc, simpletag);
3080  }
3081  }
3082 
3083  avio_seek(pb, mkv->tags.pos, SEEK_SET);
3084  ret = end_ebml_master_crc32(pb, &mkv->tags.bc, mkv,
3085  MATROSKA_ID_TAGS, 0, 0, 0);
3086  if (ret < 0)
3087  return ret;
3088  }
3089 
3090  avio_seek(pb, endpos, SEEK_SET);
3091 
3092  return ret2;
3093 }
3094 
3095 static uint64_t mkv_get_uid(const mkv_track *tracks, int i, AVLFG *c)
3096 {
3097  while (1) {
3098  uint64_t uid;
3099  int k;
3100  uid = (uint64_t)av_lfg_get(c) << 32;
3101  uid |= av_lfg_get(c);
3102  if (!uid)
3103  continue;
3104  for (k = 0; k < i; k++) {
3105  if (tracks[k].uid == uid)
3106  break;
3107  }
3108  if (k == i)
3109  return uid;
3110  }
3111 }
3112 
3113 static int mkv_init(struct AVFormatContext *s)
3114 {
3115  FFFormatContext *const si = ffformatcontext(s);
3116  MatroskaMuxContext *mkv = s->priv_data;
3117  AVLFG c;
3118  unsigned nb_tracks = 0;
3119  int i;
3120 
3121  mkv->ctx = s;
3122 
3123  for (i = 0; i < s->nb_streams; i++) {
3124  if (s->streams[i]->codecpar->codec_id == AV_CODEC_ID_ATRAC3 ||
3125  s->streams[i]->codecpar->codec_id == AV_CODEC_ID_COOK ||
3126  s->streams[i]->codecpar->codec_id == AV_CODEC_ID_RA_288 ||
3127  s->streams[i]->codecpar->codec_id == AV_CODEC_ID_SIPR ||
3128  s->streams[i]->codecpar->codec_id == AV_CODEC_ID_RV10 ||
3129  s->streams[i]->codecpar->codec_id == AV_CODEC_ID_RV20) {
3131  "The Matroska muxer does not yet support muxing %s\n",
3132  avcodec_get_name(s->streams[i]->codecpar->codec_id));
3133  return AVERROR_PATCHWELCOME;
3134  }
3135  }
3136 
3137  if (s->avoid_negative_ts < 0) {
3138  s->avoid_negative_ts = 1;
3139  si->avoid_negative_ts_use_pts = 1;
3140  }
3141 
3142  if (!CONFIG_MATROSKA_MUXER ||
3143  (CONFIG_WEBM_MUXER && !strcmp(s->oformat->name, "webm"))) {
3144  mkv->mode = MODE_WEBM;
3145  mkv->write_crc = 0;
3146  } else
3147  mkv->mode = MODE_MATROSKAv2;
3148 
3150 
3151  mkv->tracks = av_calloc(s->nb_streams, sizeof(*mkv->tracks));
3152  if (!mkv->tracks)
3153  return AVERROR(ENOMEM);
3154 
3155  if (!(s->flags & AVFMT_FLAG_BITEXACT)) {
3157 
3158  // Calculate the SegmentUID now in order not to waste our random seed.
3159  for (i = 0; i < 4; i++)
3160  mkv->segment_uid[i] = av_lfg_get(&c);
3161  }
3162 
3163  for (i = 0; i < s->nb_streams; i++) {
3164  AVStream *st = s->streams[i];
3165  const AVCodecParameters *const par = st->codecpar;
3166  mkv_track *track = &mkv->tracks[i];
3167 
3168  switch (par->codec_id) {
3169 #if CONFIG_MATROSKA_MUXER
3170  case AV_CODEC_ID_WAVPACK:
3171  track->reformat = mkv_reformat_wavpack;
3172  break;
3173  case AV_CODEC_ID_H264:
3174  case AV_CODEC_ID_HEVC:
3175  if ((par->codec_id == AV_CODEC_ID_H264 && par->extradata_size > 0 ||
3176  par->codec_id == AV_CODEC_ID_HEVC && par->extradata_size > 6) &&
3177  (AV_RB24(par->extradata) == 1 || AV_RB32(par->extradata) == 1))
3178  track->reformat = mkv_reformat_h2645;
3179  break;
3180  case AV_CODEC_ID_PRORES:
3181  /* Matroska specification requires to remove
3182  * the first QuickTime atom. */
3183  track->offset = 8;
3184  break;
3185 #endif
3186  case AV_CODEC_ID_AV1:
3187  track->reformat = mkv_reformat_av1;
3188  break;
3189  case AV_CODEC_ID_WEBVTT:
3190  track->reformat = webm_reformat_vtt;
3191  break;
3192  }
3193 
3194  if (s->flags & AVFMT_FLAG_BITEXACT) {
3195  track->uid = i + 1;
3196  } else {
3197  track->uid = mkv_get_uid(mkv->tracks, i, &c);
3198  }
3199 
3200  // ms precision is the de-facto standard timescale for mkv files
3201  avpriv_set_pts_info(st, 64, 1, 1000);
3202 
3204  if (IS_WEBM(mkv)) {
3205  av_log(s, AV_LOG_WARNING, "Stream %d will be ignored "
3206  "as WebM doesn't support attachments.\n", i);
3207  } else if (!get_mimetype(st)) {
3208  av_log(s, AV_LOG_ERROR, "Attachment stream %d has no mimetype "
3209  "tag and it cannot be deduced from the codec id.\n", i);
3210  return AVERROR(EINVAL);
3211  }
3212  mkv->nb_attachments++;
3213  continue;
3214  }
3215 
3216  nb_tracks++;
3217  track->track_num = mkv->is_dash ? mkv->dash_track_number : nb_tracks;
3218  track->track_num_size = ebml_num_size(track->track_num);
3219  }
3220 
3221  if (mkv->is_dash && nb_tracks != 1)
3222  return AVERROR(EINVAL);
3223 
3224  return 0;
3225 }
3226 
3228  const AVPacket *pkt)
3229 {
3230  int ret = 1;
3231 
3232  if (CONFIG_MATROSKA_MUXER && st->codecpar->codec_id == AV_CODEC_ID_AAC) {
3233  if (pkt->size > 2 && (AV_RB16(pkt->data) & 0xfff0) == 0xfff0)
3234  ret = ff_stream_add_bitstream_filter(st, "aac_adtstoasc", NULL);
3235  } else if (st->codecpar->codec_id == AV_CODEC_ID_VP9) {
3236  ret = ff_stream_add_bitstream_filter(st, "vp9_superframe", NULL);
3237  } else if (CONFIG_MATROSKA_MUXER &&
3239  ret = ff_stream_add_bitstream_filter(st, "pgs_frame_merge", NULL);
3240  }
3241 
3242  return ret;
3243 }
3244 
3246  { AV_CODEC_ID_ALAC, 0XFFFFFFFF },
3247  { AV_CODEC_ID_MLP, 0xFFFFFFFF },
3248  { AV_CODEC_ID_OPUS, 0xFFFFFFFF },
3249  { AV_CODEC_ID_PCM_S16BE, 0xFFFFFFFF },
3250  { AV_CODEC_ID_PCM_S24BE, 0xFFFFFFFF },
3251  { AV_CODEC_ID_PCM_S32BE, 0xFFFFFFFF },
3252  { AV_CODEC_ID_QDMC, 0xFFFFFFFF },
3253  { AV_CODEC_ID_QDM2, 0xFFFFFFFF },
3254  { AV_CODEC_ID_RA_144, 0xFFFFFFFF },
3255  { AV_CODEC_ID_RA_288, 0xFFFFFFFF },
3256  { AV_CODEC_ID_COOK, 0xFFFFFFFF },
3257  { AV_CODEC_ID_TRUEHD, 0xFFFFFFFF },
3258  { AV_CODEC_ID_NONE, 0xFFFFFFFF }
3259 };
3260 
3262  { AV_CODEC_ID_RV10, 0xFFFFFFFF },
3263  { AV_CODEC_ID_RV20, 0xFFFFFFFF },
3264  { AV_CODEC_ID_RV30, 0xFFFFFFFF },
3265  { AV_CODEC_ID_NONE, 0xFFFFFFFF }
3266 };
3267 
3269  { AV_CODEC_ID_DVB_SUBTITLE, 0xFFFFFFFF },
3270  { AV_CODEC_ID_DVD_SUBTITLE, 0xFFFFFFFF },
3271  { AV_CODEC_ID_HDMV_PGS_SUBTITLE, 0xFFFFFFFF },
3272  { AV_CODEC_ID_NONE, 0xFFFFFFFF }
3273 };
3274 
3275 #define OFFSET(x) offsetof(MatroskaMuxContext, x)
3276 #define FLAGS AV_OPT_FLAG_ENCODING_PARAM
3277 static const AVOption options[] = {
3278  { "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 },
3279  { "cues_to_front", "Move Cues (the index) to the front by shifting data if necessary", OFFSET(move_cues_to_front), AV_OPT_TYPE_BOOL, { .i64 = 0}, 0, 1, FLAGS },
3280  { "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 },
3281  { "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 },
3282  { "dash", "Create a WebM file conforming to WebM DASH specification", OFFSET(is_dash), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
3283  { "dash_track_number", "Track number for the DASH stream", OFFSET(dash_track_number), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, INT_MAX, FLAGS },
3284  { "live", "Write files assuming it is a live stream.", OFFSET(is_live), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
3285  { "allow_raw_vfw", "allow RAW VFW mode", OFFSET(allow_raw_vfw), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
3286  { "flipped_raw_rgb", "Raw RGB bitmaps in VFW mode are stored bottom-up", OFFSET(flipped_raw_rgb), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
3287  { "write_crc32", "write a CRC32 element inside every Level 1 element", OFFSET(write_crc), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, FLAGS },
3288  { "default_mode", "Controls how a track's FlagDefault is inferred", OFFSET(default_mode), AV_OPT_TYPE_INT, { .i64 = DEFAULT_MODE_PASSTHROUGH }, DEFAULT_MODE_INFER, DEFAULT_MODE_PASSTHROUGH, FLAGS, "default_mode" },
3289  { "infer", "For each track type, mark each track of disposition default as default; if none exists, mark the first track as default.", 0, AV_OPT_TYPE_CONST, { .i64 = DEFAULT_MODE_INFER }, 0, 0, FLAGS, "default_mode" },
3290  { "infer_no_subs", "For each track type, mark each track of disposition default as default; for audio and video: if none exists, mark the first track as default.", 0, AV_OPT_TYPE_CONST, { .i64 = DEFAULT_MODE_INFER_NO_SUBS }, 0, 0, FLAGS, "default_mode" },
3291  { "passthrough", "Use the disposition flag as-is", 0, AV_OPT_TYPE_CONST, { .i64 = DEFAULT_MODE_PASSTHROUGH }, 0, 0, FLAGS, "default_mode" },
3292  { NULL },
3293 };
3294 
3296  .class_name = "matroska/webm muxer",
3297  .item_name = av_default_item_name,
3298  .option = options,
3299  .version = LIBAVUTIL_VERSION_INT,
3300 };
3301 
3302 #if CONFIG_MATROSKA_MUXER
3303 static int mkv_query_codec(enum AVCodecID codec_id, int std_compliance)
3304 {
3305  for (int i = 0; ff_mkv_codec_tags[i].id != AV_CODEC_ID_NONE; i++)
3306  if (ff_mkv_codec_tags[i].id == codec_id)
3307  return 1;
3308 
3309  if (std_compliance < FF_COMPLIANCE_NORMAL) {
3311  // mkv theoretically supports any video/audio through VFW/ACM
3313  return 1;
3314  }
3315 
3316  return 0;
3317 }
3318 
3320  .name = "matroska",
3321  .long_name = NULL_IF_CONFIG_SMALL("Matroska"),
3322  .mime_type = "video/x-matroska",
3323  .extensions = "mkv",
3324  .priv_data_size = sizeof(MatroskaMuxContext),
3325  .audio_codec = CONFIG_LIBVORBIS_ENCODER ?
3327  .video_codec = CONFIG_LIBX264_ENCODER ?
3329  .init = mkv_init,
3330  .deinit = mkv_deinit,
3336  .codec_tag = (const AVCodecTag* const []){
3339  },
3340  .subtitle_codec = AV_CODEC_ID_ASS,
3341  .query_codec = mkv_query_codec,
3342  .check_bitstream = mkv_check_bitstream,
3343  .priv_class = &matroska_webm_class,
3344 };
3345 #endif
3346 
3347 #if CONFIG_WEBM_MUXER
3348 static int webm_query_codec(enum AVCodecID codec_id, int std_compliance)
3349 {
3350  for (int i = 0; ff_webm_codec_tags[i].id != AV_CODEC_ID_NONE; i++)
3351  if (ff_webm_codec_tags[i].id == codec_id)
3352  return 1;
3353 
3354  return 0;
3355 }
3356 
3357 const AVOutputFormat ff_webm_muxer = {
3358  .name = "webm",
3359  .long_name = NULL_IF_CONFIG_SMALL("WebM"),
3360  .mime_type = "video/webm",
3361  .extensions = "webm",
3362  .priv_data_size = sizeof(MatroskaMuxContext),
3363  .audio_codec = CONFIG_LIBOPUS_ENCODER ? AV_CODEC_ID_OPUS : AV_CODEC_ID_VORBIS,
3364  .video_codec = CONFIG_LIBVPX_VP9_ENCODER? AV_CODEC_ID_VP9 : AV_CODEC_ID_VP8,
3365  .subtitle_codec = AV_CODEC_ID_WEBVTT,
3366  .init = mkv_init,
3367  .deinit = mkv_deinit,
3371  .query_codec = webm_query_codec,
3375  .priv_class = &matroska_webm_class,
3376 };
3377 #endif
3378 
3379 #if CONFIG_MATROSKA_AUDIO_MUXER
3381  .name = "matroska",
3382  .long_name = NULL_IF_CONFIG_SMALL("Matroska Audio"),
3383  .mime_type = "audio/x-matroska",
3384  .extensions = "mka",
3385  .priv_data_size = sizeof(MatroskaMuxContext),
3386  .audio_codec = CONFIG_LIBVORBIS_ENCODER ?
3388  .video_codec = AV_CODEC_ID_NONE,
3389  .init = mkv_init,
3390  .deinit = mkv_deinit,
3397  .codec_tag = (const AVCodecTag* const []){
3399  },
3400  .priv_class = &matroska_webm_class,
3401 };
3402 #endif
MatroskaMuxContext::reserve_cues_space
int reserve_cues_space
Definition: matroskaenc.c:237
MATROSKA_ID_TAGTARGETS_ATTACHUID
#define MATROSKA_ID_TAGTARGETS_ATTACHUID
Definition: matroska.h:226
AVMasteringDisplayMetadata::has_primaries
int has_primaries
Flag indicating whether the display primaries (and white point) are set.
Definition: mastering_display_metadata.h:62
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:422
MATROSKA_ID_VIDEODISPLAYUNIT
#define MATROSKA_ID_VIDEODISPLAYUNIT
Definition: matroska.h:126
MATROSKA_ID_CODECPRIVATE
#define MATROSKA_ID_CODECPRIVATE
Definition: matroska.h:89
MATROSKA_ID_TRACKNUMBER
#define MATROSKA_ID_TRACKNUMBER
Definition: matroska.h:78
AVMEDIA_TYPE_SUBTITLE
@ AVMEDIA_TYPE_SUBTITLE
Definition: avutil.h:204
EBML_BIN
@ EBML_BIN
Definition: matroskaenc.c:104
MATROSKA_ID_TRACKFLAGVISUALIMPAIRED
#define MATROSKA_ID_TRACKFLAGVISUALIMPAIRED
Definition: matroska.h:102
bit_depth
static void bit_depth(AudioStatsContext *s, uint64_t mask, uint64_t imask, AVRational *depth)
Definition: af_astats.c:226
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
MatroskaMuxContext::is_dash
int is_dash
Definition: matroskaenc.c:243
MATROSKA_ID_CHAPSTRING
#define MATROSKA_ID_CHAPSTRING
Definition: matroska.h:267
mkv_write_dovi
static void mkv_write_dovi(AVFormatContext *s, AVIOContext *pb, AVStream *st)
Definition: matroskaenc.c:1576
IS_SEEKABLE
#define IS_SEEKABLE(pb, mkv)
Definition: matroskaenc.c:78
mkv_init
static int mkv_init(struct AVFormatContext *s)
Definition: matroskaenc.c:3113
MATROSKA_ID_TAGTARGETS
#define MATROSKA_ID_TAGTARGETS
Definition: matroska.h:221
AVMasteringDisplayMetadata::max_luminance
AVRational max_luminance
Max luminance of mastering display (cd/m^2).
Definition: mastering_display_metadata.h:57
AVCodecParameters::extradata
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:75
name
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option name
Definition: writing_filters.txt:88
MATROSKA_ID_CLUSTERTIMECODE
#define MATROSKA_ID_CLUSTERTIMECODE
Definition: matroska.h:236
LIBAVFORMAT_IDENT
#define LIBAVFORMAT_IDENT
Definition: version.h:45
ebml_num_size
static int ebml_num_size(uint64_t num)
Returns how many bytes are needed to represent a number as EBML variable length integer.
Definition: matroskaenc.c:296
AV_CODEC_ID_AC3
@ AV_CODEC_ID_AC3
Definition: codec_id.h:432
AVIO_DATA_MARKER_BOUNDARY_POINT
@ AVIO_DATA_MARKER_BOUNDARY_POINT
A point in the output bytestream where a demuxer can start parsing (for non self synchronizing bytest...
Definition: avio.h:129
mkv_write_packet
static int mkv_write_packet(AVFormatContext *s, const AVPacket *pkt)
Definition: matroskaenc.c:2826
AVOutputFormat::name
const char * name
Definition: avformat.h:510
MATROSKA_ID_TITLE
#define MATROSKA_ID_TITLE
Definition: matroska.h:68
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
uid
UID uid
Definition: mxfenc.c:2200
opt.h
DEFAULT_MODE_PASSTHROUGH
@ DEFAULT_MODE_PASSTHROUGH
Definition: matroskaenc.c:84
put_ebml_size_unknown
static void put_ebml_size_unknown(AVIOContext *pb, int bytes)
Write an EBML size meaning "unknown size".
Definition: matroskaenc.c:283
ebml_writer_add_string
static void ebml_writer_add_string(EbmlWriter *writer, uint32_t id, const char *str)
Definition: matroskaenc.c:485
mkv_write_packet_internal
static int mkv_write_packet_internal(AVFormatContext *s, const AVPacket *pkt)
Definition: matroskaenc.c:2751
MatroskaMuxContext::allow_raw_vfw
int allow_raw_vfw
Definition: matroskaenc.c:245
ebml_writer_add_float
static void ebml_writer_add_float(EbmlWriter *writer, uint32_t id, double val)
Definition: matroskaenc.c:505
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:57
AVSphericalMapping::projection
enum AVSphericalProjection projection
Projection type.
Definition: spherical.h:86
AV1_SANE_SEQUENCE_HEADER_MAX_BITS
#define AV1_SANE_SEQUENCE_HEADER_MAX_BITS
Definition: av1.h:182
AV_WL32
#define AV_WL32(p, v)
Definition: intreadwrite.h:426
hevc.h
DEFAULT_MODE_INFER
@ DEFAULT_MODE_INFER
Definition: matroskaenc.c:82
AVSphericalMapping::bound_bottom
uint32_t bound_bottom
Distance from the bottom edge.
Definition: spherical.h:170
MATROSKA_ID_VIDEOCOLORMASTERINGMETA
#define MATROSKA_ID_VIDEOCOLORMASTERINGMETA
Definition: matroska.h:150
ebml_writer_master_len
static int ebml_writer_master_len(EbmlWriter *writer, EbmlElement *elem, int remaining_elems)
Definition: matroskaenc.c:580
MatroskaMuxContext::segment_offset
int64_t segment_offset
Definition: matroskaenc.c:208
av_lfg_init
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition: lfg.c:32
MatroskaMuxContext::tracks
mkv_track * tracks
Definition: matroskaenc.c:214
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:53
EBML_ID_EBMLMAXSIZELENGTH
#define EBML_ID_EBMLMAXSIZELENGTH
Definition: matroska.h:39
ffformatcontext
static av_always_inline FFFormatContext * ffformatcontext(AVFormatContext *s)
Definition: internal.h:192
mkv_new_chapter_ids_needed
static int mkv_new_chapter_ids_needed(const AVFormatContext *s)
Definition: matroskaenc.c:2129
AVCodecParameters::color_space
enum AVColorSpace color_space
Definition: codec_par.h:150
end_ebml_master_crc32
static int end_ebml_master_crc32(AVIOContext *pb, AVIOContext **dyn_cp, MatroskaMuxContext *mkv, uint32_t id, int length_size, int keep_buffer, int add_seekentry)
Definition: matroskaenc.c:763
MATROSKA_ID_TRACKFLAGTEXTDESCRIPTIONS
#define MATROSKA_ID_TRACKFLAGTEXTDESCRIPTIONS
Definition: matroska.h:103
AVFMT_VARIABLE_FPS
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition: avformat.h:484
avcodec_get_type
enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
Get the type of the given codec.
Definition: codec_desc.c:3598
MATROSKA_ID_VIDEOPIXELWIDTH
#define MATROSKA_ID_VIDEOPIXELWIDTH
Definition: matroska.h:120
EbmlMaster::containing_master
int containing_master
-1 if no parent exists
Definition: matroskaenc.c:119
MATROSKA_VIDEO_STEREOMODE_TYPE_CHECKERBOARD_LR
@ MATROSKA_VIDEO_STEREOMODE_TYPE_CHECKERBOARD_LR
Definition: matroska.h:319
mkv_track::duration
int64_t duration
Definition: matroskaenc.c:186
MATROSKA_ID_AUDIOSAMPLINGFREQ
#define MATROSKA_ID_AUDIOSAMPLINGFREQ
Definition: matroska.h:170
MatroskaMuxContext::cluster_bc
AVIOContext * cluster_bc
Definition: matroskaenc.c:209
OPUS_SEEK_PREROLL
#define OPUS_SEEK_PREROLL
Seek preroll value for opus.
Definition: matroskaenc.c:264
AVMasteringDisplayMetadata::display_primaries
AVRational display_primaries[3][2]
CIE 1931 xy chromaticity coords of color primaries (r, g, b order).
Definition: mastering_display_metadata.h:42
EbmlElement
Definition: matroskaenc.c:122
MODE_MATROSKAv2
#define MODE_MATROSKAv2
Definition: matroskaenc.c:73
ff_nal_units_create_list
int ff_nal_units_create_list(NALUList *list, const uint8_t *buf, int size)
Definition: avc.c:114
AVMasteringDisplayMetadata::has_luminance
int has_luminance
Flag indicating whether the luminance (min_ and max_) have been set.
Definition: mastering_display_metadata.h:67
ff_put_bmp_header
void ff_put_bmp_header(AVIOContext *pb, AVCodecParameters *par, int for_asf, int ignore_extradata, int rgb_frame_is_flipped)
Definition: riffenc.c:216
rational.h
MATROSKA_ID_DISCARDPADDING
#define MATROSKA_ID_DISCARDPADDING
Definition: matroska.h:251
AV_TIME_BASE_Q
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:260
MATROSKA_VIDEO_FIELDORDER_BB
@ MATROSKA_VIDEO_FIELDORDER_BB
Definition: matroska.h:308
ebml_writer_add_uint
static void ebml_writer_add_uint(EbmlWriter *writer, uint32_t id, uint64_t val)
Definition: matroskaenc.c:520
MATROSKA_ID_DURATION
#define MATROSKA_ID_DURATION
Definition: matroska.h:67
MATROSKA_ID_VIDEOCOLORCHROMASITINGVERT
#define MATROSKA_ID_VIDEOCOLORCHROMASITINGVERT
Definition: matroska.h:142
BlockContext::pkt
const AVPacket * pkt
Definition: matroskaenc.c:111
avlanguage.h
av_strcasecmp
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
Definition: avstring.c:218
MATROSKA_ID_VIDEOCOLORPRIMARIES
#define MATROSKA_ID_VIDEOCOLORPRIMARIES
Definition: matroska.h:146
AV_CODEC_ID_RAWVIDEO
@ AV_CODEC_ID_RAWVIDEO
Definition: codec_id.h:63
ff_webm_muxer
const AVOutputFormat ff_webm_muxer
av_unused
#define av_unused
Definition: attributes.h:131
check_bitstream
static int check_bitstream(AVFormatContext *s, FFStream *sti, AVPacket *pkt)
Definition: mux.c:1069
MATROSKA_ID_SEGMENT
#define MATROSKA_ID_SEGMENT
Definition: matroska.h:53
AV_DISPOSITION_DEFAULT
#define AV_DISPOSITION_DEFAULT
The stream should be chosen by default among other streams of the same type, unless the user has expl...
Definition: avformat.h:826
AV_CODEC_ID_MPEG4
@ AV_CODEC_ID_MPEG4
Definition: codec_id.h:62
MATROSKA_ID_BLKADDIDTYPE
#define MATROSKA_ID_BLKADDIDTYPE
Definition: matroska.h:196
AVContentLightMetadata::MaxCLL
unsigned MaxCLL
Max content light level (cd/m^2).
Definition: mastering_display_metadata.h:102
MatroskaMuxContext::dash_track_number
int dash_track_number
Definition: matroskaenc.c:244
AV_CODEC_ID_RA_144
@ AV_CODEC_ID_RA_144
Definition: codec_id.h:416
ebml_writer_write
static int ebml_writer_write(EbmlWriter *writer, AVIOContext *pb)
Definition: matroskaenc.c:729
MATROSKA_ID_CHAPTERS
#define MATROSKA_ID_CHAPTERS
Definition: matroska.h:63
AVCOL_TRC_NB
@ AVCOL_TRC_NB
Not part of ABI.
Definition: pixfmt.h:520
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
ebml_writer_elem_write
static int ebml_writer_elem_write(const EbmlElement *elem, AVIOContext *pb)
Definition: matroskaenc.c:686
MATROSKA_ID_VIDEOCOLOR_BY
#define MATROSKA_ID_VIDEOCOLOR_BY
Definition: matroska.h:156
deinit
static void deinit(AVFormatContext *s)
Definition: chromaprint.c:49
MATROSKA_ID_BLOCKDURATION
#define MATROSKA_ID_BLOCKDURATION
Definition: matroska.h:248
MATROSKA_ID_VIDEOCOLORRANGE
#define MATROSKA_ID_VIDEOCOLORRANGE
Definition: matroska.h:143
MATROSKA_ID_BLOCK
#define MATROSKA_ID_BLOCK
Definition: matroska.h:247
AVPacket::data
uint8_t * data
Definition: packet.h:374
MATROSKA_ID_TRACKDEFAULTDURATION
#define MATROSKA_ID_TRACKDEFAULTDURATION
Definition: matroska.h:109
vorbiscomment.h
targets
FFmpeg currently uses a custom build this text attempts to document some of its obscure features and options Makefile the full command issued by make and its output will be shown on the screen DBG Preprocess x86 external assembler files to a dbg asm file in the object which then gets compiled Helps in developing those assembler files DESTDIR Destination directory for the install targets
Definition: build_system.txt:16
av_const
#define av_const
Definition: attributes.h:84
put_xiph_size
static void put_xiph_size(AVIOContext *pb, int size)
Definition: matroskaenc.c:823
AVOption
AVOption.
Definition: opt.h:251
AVCOL_SPC_NB
@ AVCOL_SPC_NB
Not part of ABI.
Definition: pixfmt.h:544
AVCOL_TRC_UNSPECIFIED
@ AVCOL_TRC_UNSPECIFIED
Definition: pixfmt.h:501
MatroskaMuxContext::mode
int mode
Definition: matroskaenc.c:204
AVStream::avg_frame_rate
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:1028
mkv_track::duration_offset
int64_t duration_offset
Definition: matroskaenc.c:187
data
const char data[16]
Definition: mxf.c:146
MATROSKA_VIDEO_INTERLACE_FLAG_INTERLACED
@ MATROSKA_VIDEO_INTERLACE_FLAG_INTERLACED
Definition: matroska.h:300
MatroskaMuxContext::cluster_pts
int64_t cluster_pts
Definition: matroskaenc.c:211
mkv_write_attachments
static int mkv_write_attachments(AVFormatContext *s)
Definition: matroskaenc.c:2243
AV_CODEC_ID_ALAC
@ AV_CODEC_ID_ALAC
Definition: codec_id.h:445
MATROSKA_ID_DATEUTC
#define MATROSKA_ID_DATEUTC
Definition: matroska.h:71
ebml_id_size
static int ebml_id_size(uint32_t id)
Definition: matroskaenc.c:266
flacenc.h
AVIOContext::error
int error
contains the error code or 0 if no error happened
Definition: avio.h:241
MatroskaMuxContext::tags
ebml_stored_master tags
Definition: matroskaenc.c:207
AV_DICT_IGNORE_SUFFIX
#define AV_DICT_IGNORE_SUFFIX
Return first entry in a dictionary whose first part corresponds to the search key,...
Definition: dict.h:68
EBML_UID
@ EBML_UID
Definition: matroskaenc.c:101
MATROSKA_VIDEO_STEREOMODE_TYPE_BOTH_EYES_BLOCK_LR
@ MATROSKA_VIDEO_STEREOMODE_TYPE_BOTH_EYES_BLOCK_LR
Definition: matroska.h:327
matroska.h
ff_parse_creation_time_metadata
int ff_parse_creation_time_metadata(AVFormatContext *s, int64_t *timestamp, int return_seconds)
Parse creation_time in AVFormatContext metadata if exists and warn if the parsing fails.
Definition: mux_utils.c:152
ff_codec_wav_tags
const AVCodecTag ff_codec_wav_tags[]
Definition: riff.c:511
mkv_track::ts_offset
int64_t ts_offset
Definition: matroskaenc.c:190
put_ebml_binary
static void put_ebml_binary(AVIOContext *pb, uint32_t elementid, const void *buf, int size)
Definition: matroskaenc.c:376
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:392
AVCodecParameters::codec_tag
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: codec_par.h:65
AV_SPHERICAL_EQUIRECTANGULAR_TILE
@ AV_SPHERICAL_EQUIRECTANGULAR_TILE
Video represents a portion of a sphere mapped on a flat surface using equirectangular projection.
Definition: spherical.h:72
MAX_VIDEO_COLOR_ELEMS
#define MAX_VIDEO_COLOR_ELEMS
Definition: matroskaenc.c:1294
max
#define max(a, b)
Definition: cuda_runtime.h:33
BlockContext
Definition: matroskaenc.c:109
mathematics.h
MATROSKA_ID_CUES
#define MATROSKA_ID_CUES
Definition: matroska.h:58
AVDictionary
Definition: dict.c:30
AV_CODEC_ID_FLAC
@ AV_CODEC_ID_FLAC
Definition: codec_id.h:441
AVChannelLayout::order
enum AVChannelOrder order
Channel order used in this layout.
Definition: channel_layout.h:295
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
MATROSKA_ID_CUETIME
#define MATROSKA_ID_CUETIME
Definition: matroska.h:203
ff_isom_put_dvcc_dvvc
void ff_isom_put_dvcc_dvvc(AVFormatContext *s, uint8_t out[ISOM_DVCC_DVVC_SIZE], AVDOVIDecoderConfigurationRecord *dovi)
Definition: dovi_isom.c:85
IS_WEBM
#define IS_WEBM(mkv)
Definition: matroskaenc.c:76
AVChannelLayout::mask
uint64_t mask
This member must be used for AV_CHANNEL_ORDER_NATIVE, and may be used for AV_CHANNEL_ORDER_AMBISONIC ...
Definition: channel_layout.h:322
WvHeader
Definition: wv.h:34
AV_PKT_DATA_SPHERICAL
@ AV_PKT_DATA_SPHERICAL
This side data should be associated with a video stream and corresponds to the AVSphericalMapping str...
Definition: packet.h:229
MATROSKA_ID_CUERELATIVEPOSITION
#define MATROSKA_ID_CUERELATIVEPOSITION
Definition: matroska.h:209
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:300
MAX_PCE_SIZE
#define MAX_PCE_SIZE
Maximum size of a PCE including the 3-bit ID_PCE.
Definition: mpeg4audio.h:120
ff_flac_is_native_layout
int ff_flac_is_native_layout(uint64_t channel_layout)
Definition: flacenc_header.c:50
AV_CODEC_ID_HDMV_PGS_SUBTITLE
@ AV_CODEC_ID_HDMV_PGS_SUBTITLE
Definition: codec_id.h:535
AV_SPHERICAL_EQUIRECTANGULAR
@ AV_SPHERICAL_EQUIRECTANGULAR
Video represents a sphere mapped on a flat surface using equirectangular projection.
Definition: spherical.h:56
intfloat.h
codec_type
enum AVMediaType codec_type
Definition: rtp.c:37
MatroskaMuxContext::flipped_raw_rgb
int flipped_raw_rgb
Definition: matroskaenc.c:246
MATROSKA_ID_CHAPLANG
#define MATROSKA_ID_CHAPLANG
Definition: matroska.h:268
AV_CODEC_ID_TRUEHD
@ AV_CODEC_ID_TRUEHD
Definition: codec_id.h:473
MATROSKA_ID_CUEDURATION
#define MATROSKA_ID_CUEDURATION
Definition: matroska.h:210
avio_get_dyn_buf
int avio_get_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1495
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:429
sample_rate
sample_rate
Definition: ffmpeg_filter.c:155
MATROSKA_VIDEO_STEREOMODE_TYPE_ROW_INTERLEAVED_LR
@ MATROSKA_VIDEO_STEREOMODE_TYPE_ROW_INTERLEAVED_LR
Definition: matroska.h:321
AV_WB64
#define AV_WB64(p, v)
Definition: intreadwrite.h:433
MatroskaMuxContext::cues_pos
int64_t cues_pos
Definition: matroskaenc.c:217
av_get_random_seed
uint32_t av_get_random_seed(void)
Get a seed to use in conjunction with random functions.
Definition: random_seed.c:120
xiph.h
MatroskaMuxContext::cluster_size_limit
int cluster_size_limit
Definition: matroskaenc.c:238
NALUList::nalus
NALU * nalus
Definition: avc.h:35
init
static int init
Definition: av_tx.c:47
MATROSKA_VIDEO_DISPLAYUNIT_UNKNOWN
@ MATROSKA_VIDEO_DISPLAYUNIT_UNKNOWN
Definition: matroska.h:337
mkv_track::codecpriv_offset
int codecpriv_offset
Definition: matroskaenc.c:188
AVContentLightMetadata
Content light level needed by to transmit HDR over HDMI (CTA-861.3).
Definition: mastering_display_metadata.h:98
MATROSKA_ID_VIDEOCOLOR_LUMINANCEMAX
#define MATROSKA_ID_VIDEOCOLOR_LUMINANCEMAX
Definition: matroska.h:159
MPEG4AudioConfig
Definition: mpeg4audio.h:32
crc.h
avio_wl16
void avio_wl16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:466
AVCodecParameters::color_primaries
enum AVColorPrimaries color_primaries
Definition: codec_par.h:148
avio_write_marker
void avio_write_marker(AVIOContext *s, int64_t time, enum AVIODataMarkerType type)
Mark the written bytestream as a specific type.
Definition: aviobuf.c:490
MATROSKA_ID_VIDEOCOLOR_WHITEY
#define MATROSKA_ID_VIDEOCOLOR_WHITEY
Definition: matroska.h:158
AV_STEREO3D_SIDEBYSIDE
@ AV_STEREO3D_SIDEBYSIDE
Views are next to each other.
Definition: stereo3d.h:67
avcodec_enum_to_chroma_pos
int avcodec_enum_to_chroma_pos(int *xpos, int *ypos, enum AVChromaLocation pos)
Converts AVChromaLocation to swscale x/y chroma position.
Definition: utils.c:356
start_ebml_master
static ebml_master start_ebml_master(AVIOContext *pb, uint32_t elementid, uint64_t expectedsize)
Definition: matroskaenc.c:414
DVCC_DVVC_BLOCK_TYPE_NAME
#define DVCC_DVVC_BLOCK_TYPE_NAME
Definition: matroska.h:395
AV_CODEC_ID_ASS
@ AV_CODEC_ID_ASS
Definition: codec_id.h:551
MATROSKA_ID_AUDIOOUTSAMPLINGFREQ
#define MATROSKA_ID_AUDIOOUTSAMPLINGFREQ
Definition: matroska.h:171
MAX_SUPPORTED_EBML_LENGTH
#define MAX_SUPPORTED_EBML_LENGTH
Definition: matroskaenc.c:71
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: avformat.c:697
AV_FIELD_TT
@ AV_FIELD_TT
Definition: codec_par.h:40
mpeg4audio.h
MATROSKA_ID_VIDEOPROJECTIONPRIVATE
#define MATROSKA_ID_VIDEOPROJECTIONPRIVATE
Definition: matroska.h:164
AV_CODEC_ID_PCM_S16BE
@ AV_CODEC_ID_PCM_S16BE
Definition: codec_id.h:321
mkv_cues
Definition: matroskaenc.c:169
MATROSKA_VIDEO_STEREOMODE_TYPE_MONO
@ MATROSKA_VIDEO_STEREOMODE_TYPE_MONO
Definition: matroska.h:314
MatroskaMuxContext::move_cues_to_front
int move_cues_to_front
Definition: matroskaenc.c:248
fail
#define fail()
Definition: checkasm.h:131
AVCodecParameters::bits_per_raw_sample
int bits_per_raw_sample
This is the number of valid bits in each output sample.
Definition: codec_par.h:116
ebml_writer_sint_len
static int ebml_writer_sint_len(EbmlElement *elem)
Definition: matroskaenc.c:571
EbmlElement::sint
int64_t sint
Definition: matroskaenc.c:129
AV_STEREO3D_2D
@ AV_STEREO3D_2D
Video is not stereoscopic (and metadata has to be there).
Definition: stereo3d.h:55
MATROSKA_ID_CHAPTERDISPLAY
#define MATROSKA_ID_CHAPTERDISPLAY
Definition: matroska.h:266
MATROSKA_ID_TRACKUID
#define MATROSKA_ID_TRACKUID
Definition: matroska.h:79
samplefmt.h
EBML_ID_DOCTYPEVERSION
#define EBML_ID_DOCTYPEVERSION
Definition: matroska.h:41
AVCOL_RANGE_NB
@ AVCOL_RANGE_NB
Not part of ABI.
Definition: pixfmt.h:602
ff_vorbiscomment_write
int ff_vorbiscomment_write(AVIOContext *pb, const AVDictionary *m, const char *vendor_string, AVChapter **chapters, unsigned int nb_chapters)
Write a VorbisComment into an AVIOContext.
Definition: vorbiscomment.c:65
put_ebml_uint
static void put_ebml_uint(AVIOContext *pb, uint32_t elementid, uint64_t val)
Definition: matroskaenc.c:356
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:505
ff_matroska_video_stereo_mode
const char *const ff_matroska_video_stereo_mode[MATROSKA_VIDEO_STEREOMODE_TYPE_NB]
Definition: matroska.c:130
AVChapter
Definition: avformat.h:1172
query_codec
static int query_codec(enum AVCodecID id, int std_compliance)
Definition: img2enc.c:240
AV_DISPOSITION_FORCED
#define AV_DISPOSITION_FORCED
Track should be used during playback by default.
Definition: avformat.h:859
val
static double val(void *priv, double ch)
Definition: aeval.c:77
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
MATROSKA_ID_VIDEOALPHAMODE
#define MATROSKA_ID_VIDEOALPHAMODE
Definition: matroska.h:130
scale
static av_always_inline float scale(float x, float s)
Definition: vf_v360.c:1389
pts
static int64_t pts
Definition: transcode_aac.c:654
us
#define us(width, name, range_min, range_max, subs,...)
Definition: cbs_h2645.c:276
EBML_ID_DOCTYPE
#define EBML_ID_DOCTYPE
Definition: matroska.h:40
AV_FIELD_TB
@ AV_FIELD_TB
Definition: codec_par.h:42
av_reduce
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:35
MATROSKA_ID_VIDEOCOLOR_WHITEX
#define MATROSKA_ID_VIDEOCOLOR_WHITEX
Definition: matroska.h:157
AVRational::num
int num
Numerator.
Definition: rational.h:59
MATROSKA_TRACK_TYPE_METADATA
@ MATROSKA_TRACK_TYPE_METADATA
Definition: matroska.h:288
AV_CODEC_ID_ATRAC3
@ AV_CODEC_ID_ATRAC3
Definition: codec_id.h:460
MATROSKA_VIDEO_FIELDORDER_TT
@ MATROSKA_VIDEO_FIELDORDER_TT
Definition: matroska.h:306
AV_CODEC_ID_DVB_SUBTITLE
@ AV_CODEC_ID_DVB_SUBTITLE
Definition: codec_id.h:530
MatroskaVideoStereoModeType
MatroskaVideoStereoModeType
Definition: matroska.h:313
av_get_bits_per_sample
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:586
AV_CODEC_ID_SIPR
@ AV_CODEC_ID_SIPR
Definition: codec_id.h:470
AVCodecParameters::color_trc
enum AVColorTransferCharacteristic color_trc
Definition: codec_par.h:149
MATROSKA_ID_BLKADDIDNAME
#define MATROSKA_ID_BLKADDIDNAME
Definition: matroska.h:195
mkv_write_tracks
static int mkv_write_tracks(AVFormatContext *s)
Definition: matroskaenc.c:1924
MatroskaMuxContext::ctx
AVFormatContext * ctx
Definition: matroskaenc.c:202
mkv_seekhead_entry
Definition: matroskaenc.c:149
MATROSKA_ID_BLOCKADDITIONAL
#define MATROSKA_ID_BLOCKADDITIONAL
Definition: matroska.h:243
AV_STEREO3D_FRAMESEQUENCE
@ AV_STEREO3D_FRAMESEQUENCE
Views are alternated temporally.
Definition: stereo3d.h:92
AV_PKT_DATA_WEBVTT_SETTINGS
@ AV_PKT_DATA_WEBVTT_SETTINGS
The optional settings (rendering instructions) that immediately follow the timestamp specifier of a W...
Definition: packet.h:203
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
EBML_ID_VOID
#define EBML_ID_VOID
Definition: matroska.h:45
MATROSKA_ID_VIDEOFIELDORDER
#define MATROSKA_ID_VIDEOFIELDORDER
Definition: matroska.h:128
AVCodecTag
Definition: internal.h:50
MATROSKA_ID_TAGS
#define MATROSKA_ID_TAGS
Definition: matroska.h:59
AV_CODEC_ID_TTA
@ AV_CODEC_ID_TTA
Definition: codec_id.h:451
options
static const AVOption options[]
Definition: matroskaenc.c:3277
AV_CHANNEL_ORDER_NATIVE
@ AV_CHANNEL_ORDER_NATIVE
The native channel order, i.e.
Definition: channel_layout.h:112
duration
int64_t duration
Definition: movenc.c:64
put_ebml_id
static void put_ebml_id(AVIOContext *pb, uint32_t id)
Definition: matroskaenc.c:271
EBML_ID_EBMLMAXIDLENGTH
#define EBML_ID_EBMLMAXIDLENGTH
Definition: matroska.h:38
av_dict_get
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:40
avio_open_dyn_buf
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1483
mkv_cuepoint::relative_pos
int64_t relative_pos
relative offset from the position of the cluster containing the block
Definition: matroskaenc.c:165
mkv_update_codecprivate
static int mkv_update_codecprivate(AVFormatContext *s, MatroskaMuxContext *mkv, uint8_t *side_data, int side_data_size, AVCodecParameters *par, AVIOContext *pb, mkv_track *track, unsigned alternative_size)
Definition: matroskaenc.c:1257
AV_CODEC_ID_ADPCM_G726
@ AV_CODEC_ID_ADPCM_G726
Definition: codec_id.h:370
avpriv_mpeg4audio_get_config2
int avpriv_mpeg4audio_get_config2(MPEG4AudioConfig *c, const uint8_t *buf, int size, int sync_extension, void *logctx)
Parse MPEG-4 systems extradata from a raw buffer to retrieve audio configuration.
Definition: mpeg4audio.c:165
ebml_writer_block_len
static int ebml_writer_block_len(EbmlElement *elem)
Definition: matroskaenc.c:606
AV_STEREO3D_LINES
@ AV_STEREO3D_LINES
Views are packed per line, as if interlaced.
Definition: stereo3d.h:129
AVCodecDescriptor
This struct describes the properties of a single codec described by an AVCodecID.
Definition: codec_desc.h:38
stereo3d.h
AVMasteringDisplayMetadata::white_point
AVRational white_point[2]
CIE 1931 xy chromaticity coords of white point.
Definition: mastering_display_metadata.h:47
intreadwrite.h
end_ebml_master
static void end_ebml_master(AVIOContext *pb, ebml_master master)
Definition: matroskaenc.c:424
MATROSKA_ID_FILEUID
#define MATROSKA_ID_FILEUID
Definition: matroska.h:259
s
#define s(width, name)
Definition: cbs_vp9.c:256
AVCHROMA_LOC_TOP
@ AVCHROMA_LOC_TOP
Definition: pixfmt.h:625
MATROSKA_VIDEO_INTERLACE_FLAG_PROGRESSIVE
@ MATROSKA_VIDEO_INTERLACE_FLAG_PROGRESSIVE
Definition: matroska.h:301
EbmlElement::bin
const uint8_t * bin
Definition: matroskaenc.c:132
AV_PKT_DATA_STEREO3D
@ AV_PKT_DATA_STEREO3D
This side data should be associated with a video stream and contains Stereoscopic 3D information in f...
Definition: packet.h:115
AVCOL_PRI_NB
@ AVCOL_PRI_NB
Not part of ABI.
Definition: pixfmt.h:491
AV_PIX_FMT_YUVA420P
@ AV_PIX_FMT_YUVA420P
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:101
mkv_track::write_dts
int write_dts
Definition: matroskaenc.c:177
CodecTags::str
char str[22]
Definition: matroska.h:366
av_lfg_get
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
Definition: lfg.h:53
MATROSKA_ID_VIDEODISPLAYWIDTH
#define MATROSKA_ID_VIDEODISPLAYWIDTH
Definition: matroska.h:118
av_realloc_array
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:225
AV_PKT_DATA_MASTERING_DISPLAY_METADATA
@ AV_PKT_DATA_MASTERING_DISPLAY_METADATA
Mastering display metadata (based on SMPTE-2086:2014).
Definition: packet.h:223
format
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample format(the sample packing is implied by the sample format) and sample rate. The lists are not just lists
mkv_write_flush_packet
static int mkv_write_flush_packet(AVFormatContext *s, AVPacket *pkt)
Definition: matroskaenc.c:2900
ebml_writer_add_block
static void ebml_writer_add_block(EbmlWriter *writer, MatroskaMuxContext *mkv)
Definition: matroskaenc.c:534
MATROSKA_VIDEO_FIELDORDER_BT
@ MATROSKA_VIDEO_FIELDORDER_BT
Definition: matroska.h:310
MatroskaMuxContext::cluster_pos
int64_t cluster_pos
file offset of the current Cluster
Definition: matroskaenc.c:210
AVDictionaryEntry::key
char * key
Definition: dict.h:80
MATROSKA_ID_TAGNAME
#define MATROSKA_ID_TAGNAME
Definition: matroska.h:216
AVSphericalMapping::bound_top
uint32_t bound_top
Distance from the top edge.
Definition: spherical.h:168
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AV_CODEC_ID_VP9
@ AV_CODEC_ID_VP9
Definition: codec_id.h:218
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:127
ebml_writer_open_master
static void ebml_writer_open_master(EbmlWriter *writer, uint32_t id)
Definition: matroskaenc.c:442
ff_matroska_audio_muxer
const AVOutputFormat ff_matroska_audio_muxer
MATROSKA_ID_TAG
#define MATROSKA_ID_TAG
Definition: matroska.h:214
av_q2d
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition: rational.h:104
ebml_writer_add_uid
static void ebml_writer_add_uid(EbmlWriter *writer, uint32_t id, uint64_t val)
Definition: matroskaenc.c:513
MATROSKA_ID_TIMECODESCALE
#define MATROSKA_ID_TIMECODESCALE
Definition: matroska.h:66
lfg.h
MatroskaMuxContext::tmp_bc
AVIOContext * tmp_bc
Definition: matroskaenc.c:227
get_mimetype
static const char * get_mimetype(const AVStream *st)
Definition: matroskaenc.c:2226
ebml_writer_add_sint
static void ebml_writer_add_sint(EbmlWriter *writer, uint32_t id, int64_t val)
Definition: matroskaenc.c:527
MatroskaMuxContext::write_crc
int write_crc
Definition: matroskaenc.c:240
matroska_webm_class
static const AVClass matroska_webm_class
Definition: matroskaenc.c:3295
AV_OPT_TYPE_INT64
@ AV_OPT_TYPE_INT64
Definition: opt.h:226
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
MATROSKA_ID_CUETRACKPOSITION
#define MATROSKA_ID_CUETRACKPOSITION
Definition: matroska.h:204
additional_audio_tags
static const AVCodecTag additional_audio_tags[]
Definition: matroskaenc.c:3245
MATROSKA_ID_SEEKENTRY
#define MATROSKA_ID_SEEKENTRY
Definition: matroska.h:229
mkv_track::track_num
unsigned track_num
Definition: matroskaenc.c:180
AV_FIELD_UNKNOWN
@ AV_FIELD_UNKNOWN
Definition: codec_par.h:38
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
MATROSKA_ID_TRACKTYPE
#define MATROSKA_ID_TRACKTYPE
Definition: matroska.h:80
ebml_writer_close_master
static void ebml_writer_close_master(EbmlWriter *writer)
Definition: matroskaenc.c:453
AVDOVIDecoderConfigurationRecord::dv_profile
uint8_t dv_profile
Definition: dovi_meta.h:55
additional_video_tags
static const AVCodecTag additional_video_tags[]
Definition: matroskaenc.c:3261
MATROSKA_VIDEO_STEREOMODE_TYPE_COL_INTERLEAVED_LR
@ MATROSKA_VIDEO_STEREOMODE_TYPE_COL_INTERLEAVED_LR
Definition: matroska.h:323
MATROSKA_ID_SEGMENTUID
#define MATROSKA_ID_SEGMENTUID
Definition: matroska.h:72
EbmlType
EbmlType
Definition: matroskadec.c:83
MATROSKA_ID_TRACKBLKADDMAPPING
#define MATROSKA_ID_TRACKBLKADDMAPPING
Definition: matroska.h:114
mkv_write_stereo_mode
static int mkv_write_stereo_mode(AVFormatContext *s, EbmlWriter *writer, AVStream *st, int is_webm, int *h_width, int *h_height)
Definition: matroskaenc.c:1484
av_rescale_q
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
sint_size
static av_const int sint_size(int64_t val)
Definition: matroskaenc.c:565
MatroskaMuxContext::cluster_time_limit
int64_t cluster_time_limit
Definition: matroskaenc.c:239
EbmlElement::type
EbmlType type
Definition: matroskaenc.c:124
EbmlElement::f
double f
Definition: matroskaenc.c:130
MATROSKA_ID_INFO
#define MATROSKA_ID_INFO
Definition: matroska.h:56
MatroskaMuxContext::have_video
int have_video
Definition: matroskaenc.c:232
MATROSKA_ID_AUDIOCHANNELS
#define MATROSKA_ID_AUDIOCHANNELS
Definition: matroska.h:174
codec_id
enum AVCodecID codec_id
Definition: vaapi_decode.c:374
AV_CODEC_ID_SVQ3
@ AV_CODEC_ID_SVQ3
Definition: codec_id.h:73
EbmlType
EbmlType
Definition: matroskaenc.c:97
key
const char * key
Definition: hwcontext_opencl.c:174
AVCOL_PRI_UNSPECIFIED
@ AVCOL_PRI_UNSPECIFIED
Definition: pixfmt.h:476
ff_format_shift_data
int ff_format_shift_data(AVFormatContext *s, int64_t read_start, int shift_size)
Make shift_size amount of space at read_start by shifting data in the output at read_start until the ...
Definition: mux_utils.c:58
AV_FIELD_BT
@ AV_FIELD_BT
Definition: codec_par.h:43
AV_CODEC_ID_H264
@ AV_CODEC_ID_H264
Definition: codec_id.h:77
MATROSKA_ID_VIDEOSTEREOMODE
#define MATROSKA_ID_VIDEOSTEREOMODE
Definition: matroska.h:129
mkv_start_seekhead
static void mkv_start_seekhead(MatroskaMuxContext *mkv, AVIOContext *pb)
Initialize the SeekHead element to be ready to index level 1 Matroska elements.
Definition: matroskaenc.c:852
ff_webm_codec_tags
const CodecTags ff_webm_codec_tags[]
Definition: matroska.c:108
mkv_track::uid
uint64_t uid
Definition: matroskaenc.c:179
AVStereo3D::flags
int flags
Additional information about the frame packing.
Definition: stereo3d.h:185
MATROSKA_ID_VIDEOCOLORSPACE
#define MATROSKA_ID_VIDEOCOLORSPACE
Definition: matroska.h:132
MATROSKA_ID_TRACKNAME
#define MATROSKA_ID_TRACKNAME
Definition: matroska.h:96
FFFormatContext
Definition: internal.h:72
BlockContext::track
struct mkv_track * track
Definition: matroskaenc.c:110
AV_CODEC_ID_WEBVTT
@ AV_CODEC_ID_WEBVTT
Definition: codec_id.h:547
EBML_ID_DOCTYPEREADVERSION
#define EBML_ID_DOCTYPEREADVERSION
Definition: matroska.h:42
mkv_cues::entries
mkv_cuepoint * entries
Definition: matroskaenc.c:170
AVFormatContext
Format I/O context.
Definition: avformat.h:1213
internal.h
MATROSKA_ID_TRACKFLAGHEARINGIMPAIRED
#define MATROSKA_ID_TRACKFLAGHEARINGIMPAIRED
Definition: matroska.h:101
ff_metadata_conv_ctx
void ff_metadata_conv_ctx(AVFormatContext *ctx, const AVMetadataConv *d_conv, const AVMetadataConv *s_conv)
Definition: metadata.c:59
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1108
AVChannelLayout::u
union AVChannelLayout::@303 u
Details about which channels are present in this layout.
MATROSKA_VIDEO_STEREOMODE_TYPE_TOP_BOTTOM
@ MATROSKA_VIDEO_STEREOMODE_TYPE_TOP_BOTTOM
Definition: matroska.h:317
AV_LANG_ISO639_2_BIBL
@ AV_LANG_ISO639_2_BIBL
Definition: avlanguage.h:28
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
MATROSKA_ID_BLOCKGROUP
#define MATROSKA_ID_BLOCKGROUP
Definition: matroska.h:239
MATROSKA_ID_VIDEOCOLOR_LUMINANCEMIN
#define MATROSKA_ID_VIDEOCOLOR_LUMINANCEMIN
Definition: matroska.h:160
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
EbmlWriter
Definition: matroskaenc.c:138
mkv_assemble_codecprivate
static int mkv_assemble_codecprivate(AVFormatContext *s, AVIOContext *dyn_cp, AVCodecParameters *par, const uint8_t *extradata, int extradata_size, int native_id, int qt_id, uint8_t **codecpriv, int *codecpriv_size, unsigned *max_payload_size)
Definition: matroskaenc.c:1155
AVStream::time_base
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avformat.h:978
NULL
#define NULL
Definition: coverity.c:32
MATROSKA_ID_ATTACHMENTS
#define MATROSKA_ID_ATTACHMENTS
Definition: matroska.h:61
ff_put_wav_header
int ff_put_wav_header(AVFormatContext *s, AVIOContext *pb, AVCodecParameters *par, int flags)
Write WAVEFORMAT header structure.
Definition: riffenc.c:55
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
MATROSKA_ID_CHAPTERATOM
#define MATROSKA_ID_CHAPTERATOM
Definition: matroska.h:263
AV_CODEC_ID_AV1
@ AV_CODEC_ID_AV1
Definition: codec_id.h:279
DEFAULT_MODE_INFER_NO_SUBS
@ DEFAULT_MODE_INFER_NO_SUBS
Definition: matroskaenc.c:83
MATROSKA_TRACK_TYPE_AUDIO
@ MATROSKA_TRACK_TYPE_AUDIO
Definition: matroska.h:282
ebml_length_size
static int ebml_length_size(uint64_t length)
Calculate how many bytes are needed to represent the length field of an EBML element whose payload ha...
Definition: matroskaenc.c:309
isom.h
MATROSKA_ID_TRACKFLAGORIGINAL
#define MATROSKA_ID_TRACKFLAGORIGINAL
Definition: matroska.h:104
MATROSKA_VIDEO_DISPLAYUNIT_DAR
@ MATROSKA_VIDEO_DISPLAYUNIT_DAR
Definition: matroska.h:336
write_trailer
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:100
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
MATROSKA_ID_WRITINGAPP
#define MATROSKA_ID_WRITINGAPP
Definition: matroska.h:69
WV_HEADER_SIZE
#define WV_HEADER_SIZE
Definition: wavpack.h:33
MatroskaMuxContext::info
ebml_stored_master info
Definition: matroskaenc.c:205
AV_DISPOSITION_COMMENT
#define AV_DISPOSITION_COMMENT
The stream is a commentary track.
Definition: avformat.h:844
MATROSKA_ID_VIDEOCOLOR_GY
#define MATROSKA_ID_VIDEOCOLOR_GY
Definition: matroska.h:154
AV_CODEC_ID_CINEPAK
@ AV_CODEC_ID_CINEPAK
Definition: codec_id.h:93
AV_CODEC_ID_DVD_SUBTITLE
@ AV_CODEC_ID_DVD_SUBTITLE
Definition: codec_id.h:529
MAX_VIDEO_PROJECTION_ELEMS
#define MAX_VIDEO_PROJECTION_ELEMS
Definition: matroskaenc.c:1377
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
avc.h
parseutils.h
MATROSKA_ID_SIMPLETAG
#define MATROSKA_ID_SIMPLETAG
Definition: matroska.h:215
MATROSKA_ID_VIDEOPROJECTION
#define MATROSKA_ID_VIDEOPROJECTION
Definition: matroska.h:162
mkv_assemble_native_codecprivate
static int mkv_assemble_native_codecprivate(AVFormatContext *s, AVIOContext *dyn_cp, const AVCodecParameters *par, const uint8_t *extradata, int extradata_size, unsigned *size_to_reserve)
Definition: matroskaenc.c:1099
MatroskaMuxContext::segment_uid
uint32_t segment_uid[4]
Definition: matroskaenc.c:250
MatroskaMuxContext::cur_block
BlockContext cur_block
Definition: matroskaenc.c:219
put_xiph_codecpriv
static int put_xiph_codecpriv(AVFormatContext *s, AVIOContext *pb, const AVCodecParameters *par, const uint8_t *extradata, int extradata_size)
Definition: matroskaenc.c:987
AVStream::metadata
AVDictionary * metadata
Definition: avformat.h:1019
MATROSKA_ID_SEEKID
#define MATROSKA_ID_SEEKID
Definition: matroska.h:232
put_ebml_num
static void put_ebml_num(AVIOContext *pb, uint64_t num, int bytes)
Write a number as EBML variable length integer on bytes bytes.
Definition: matroskaenc.c:318
AV_DISPOSITION_METADATA
#define AV_DISPOSITION_METADATA
The subtitle stream contains time-aligned metadata that is not intended to be directly presented to t...
Definition: avformat.h:908
EBML_STR
@ EBML_STR
Definition: matroskaenc.c:102
av_parse_time
int av_parse_time(int64_t *timeval, const char *timestr, int duration)
Parse timestr and return in *time a corresponding number of microseconds.
Definition: parseutils.c:589
avio_w8
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:210
MATROSKA_ID_CHAPTERTIMESTART
#define MATROSKA_ID_CHAPTERTIMESTART
Definition: matroska.h:264
ff_codec_movvideo_tags
const AVCodecTag ff_codec_movvideo_tags[]
Definition: isom_tags.c:29
ffio_fill
void ffio_fill(AVIOContext *s, int b, int64_t count)
Definition: aviobuf.c:218
mkv_add_seekhead_entry
static void mkv_add_seekhead_entry(MatroskaMuxContext *mkv, uint32_t elementid, uint64_t filepos)
Definition: matroskaenc.c:739
AV_DISPOSITION_ORIGINAL
#define AV_DISPOSITION_ORIGINAL
The stream is in original language.
Definition: avformat.h:840
mkv_track::sample_rate_offset
int64_t sample_rate_offset
Definition: matroskaenc.c:184
AV_CODEC_ID_QDM2
@ AV_CODEC_ID_QDM2
Definition: codec_id.h:448
mkv_get_uid
static uint64_t mkv_get_uid(const mkv_track *tracks, int i, AVLFG *c)
Definition: matroskaenc.c:3095
MATROSKA_TRACK_TYPE_VIDEO
@ MATROSKA_TRACK_TYPE_VIDEO
Definition: matroska.h:281
ebml_writer_add
static EbmlElement * ebml_writer_add(EbmlWriter *writer, uint32_t id, EbmlType type)
Definition: matroskaenc.c:434
av_packet_ref
int av_packet_ref(AVPacket *dst, const AVPacket *src)
Setup a new reference to the data described by a given packet.
Definition: avpacket.c:430
get_metadata_duration
static int64_t get_metadata_duration(AVFormatContext *s)
Definition: matroskaenc.c:2293
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:212
mkv_track::has_cue
int has_cue
Definition: matroskaenc.c:178
MATROSKA_ID_FILEMIMETYPE
#define MATROSKA_ID_FILEMIMETYPE
Definition: matroska.h:257
MATROSKA_ID_VIDEODISPLAYHEIGHT
#define MATROSKA_ID_VIDEODISPLAYHEIGHT
Definition: matroska.h:119
EbmlWriter::elements
EbmlElement * elements
Definition: matroskaenc.c:141
MATROSKA_ID_TRACKAUDIO
#define MATROSKA_ID_TRACKAUDIO
Definition: matroska.h:82
AVCOL_RANGE_UNSPECIFIED
@ AVCOL_RANGE_UNSPECIFIED
Definition: pixfmt.h:567
EbmlElement::str
const char * str
Definition: matroskaenc.c:131
ff_vorbiscomment_length
int64_t ff_vorbiscomment_length(const AVDictionary *m, const char *vendor_string, AVChapter **chapters, unsigned int nb_chapters)
Calculate the length in bytes of a VorbisComment.
Definition: vorbiscomment.c:41
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
MatroskaMuxContext::nb_attachments
unsigned nb_attachments
Definition: matroskaenc.c:231
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:177
put_ebml_float
static void put_ebml_float(AVIOContext *pb, uint32_t elementid, double val)
Definition: matroskaenc.c:369
uint_size
static av_const int uint_size(uint64_t val)
Definition: matroskaenc.c:550
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:47
MATROSKA_ID_VIDEOCOLORMAXCLL
#define MATROSKA_ID_VIDEOCOLORMAXCLL
Definition: matroska.h:147
MATROSKA_ID_TRACKENTRY
#define MATROSKA_ID_TRACKENTRY
Definition: matroska.h:75
ebml_stored_master
Definition: matroskaenc.c:92
AVCodecParameters::extradata_size
int extradata_size
Size of the extradata content in bytes.
Definition: codec_par.h:79
AV_WB32
#define AV_WB32(p, v)
Definition: intreadwrite.h:419
ff_wv_parse_header
int ff_wv_parse_header(WvHeader *wv, const uint8_t *data)
Parse a WavPack block header.
Definition: wv.c:30
mkv_put_codecprivate
static void mkv_put_codecprivate(AVIOContext *pb, unsigned max_payload_size, const uint8_t *codecpriv, unsigned codecpriv_size)
Definition: matroskaenc.c:1228
AV_CODEC_ID_AAC
@ AV_CODEC_ID_AAC
Definition: codec_id.h:431
mkv_write_header
static int mkv_write_header(AVFormatContext *s)
Definition: matroskaenc.c:2392
MatroskaMuxContext
Definition: matroskaenc.c:200
mkv_cuepoint::duration
int64_t duration
duration of the block according to time base
Definition: matroskaenc.c:166
ff_nal_units_write_list
void ff_nal_units_write_list(const NALUList *list, AVIOContext *pb, const uint8_t *buf)
Definition: avc.c:120
EbmlMaster::nb_elements
int nb_elements
-1 if not finished
Definition: matroskaenc.c:118
ebml_writer_add_bin
static void ebml_writer_add_bin(EbmlWriter *writer, uint32_t id, const uint8_t *data, size_t size)
Definition: matroskaenc.c:493
AV_DISPOSITION_CAPTIONS
#define AV_DISPOSITION_CAPTIONS
The subtitle stream contains captions, providing a transcription and possibly a translation of audio.
Definition: avformat.h:897
MATROSKA_ID_AUDIOBITDEPTH
#define MATROSKA_ID_AUDIOBITDEPTH
Definition: matroska.h:173
AV_CODEC_ID_FFV1
@ AV_CODEC_ID_FFV1
Definition: codec_id.h:83
AVLFG
Context structure for the Lagged Fibonacci PRNG.
Definition: lfg.h:33
mkv_track::offset
unsigned offset
Definition: matroskaenc.c:183
MATROSKA_VIDEO_STEREOMODE_TYPE_LEFT_RIGHT
@ MATROSKA_VIDEO_STEREOMODE_TYPE_LEFT_RIGHT
Definition: matroska.h:315
MATROSKA_ID_TRACKFLAGDEFAULT
#define MATROSKA_ID_TRACKFLAGDEFAULT
Definition: matroska.h:99
AVIOContext
Bytestream IO Context.
Definition: avio.h:162
AV_SPHERICAL_CUBEMAP
@ AV_SPHERICAL_CUBEMAP
Video frame is split into 6 faces of a cube, and arranged on a 3x2 layout.
Definition: spherical.h:65
ff_matroska_muxer
const AVOutputFormat ff_matroska_muxer
MATROSKA_ID_TRACKFLAGCOMMENTARY
#define MATROSKA_ID_TRACKFLAGCOMMENTARY
Definition: matroska.h:105
ffio_reset_dyn_buf
void ffio_reset_dyn_buf(AVIOContext *s)
Reset a dynamic buffer.
Definition: aviobuf.c:1517
AV_STEREO3D_CHECKERBOARD
@ AV_STEREO3D_CHECKERBOARD
Views are packed in a checkerboard-like structure per pixel.
Definition: stereo3d.h:104
AVMediaType
AVMediaType
Definition: avutil.h:199
MATROSKA_ID_TRACKFLAGLACING
#define MATROSKA_ID_TRACKFLAGLACING
Definition: matroska.h:106
AVPacket::size
int size
Definition: packet.h:375
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
id
enum AVCodecID id
Definition: extract_extradata_bsf.c:324
MATROSKA_ID_VIDEOCOLOR_GX
#define MATROSKA_ID_VIDEOCOLOR_GX
Definition: matroska.h:153
ff_codec_get_id
enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
Definition: utils.c:144
master
const char * master
Definition: vf_curves.c:119
ff_isom_write_avcc
int ff_isom_write_avcc(AVIOContext *pb, const uint8_t *data, int len)
Definition: avc.c:142
AVSphericalMapping::bound_right
uint32_t bound_right
Distance from the right edge.
Definition: spherical.h:169
mkv_write_field_order
static void mkv_write_field_order(EbmlWriter *writer, int is_webm, enum AVFieldOrder field_order)
Definition: matroskaenc.c:1444
EBML_FLOAT
@ EBML_FLOAT
Definition: matroskaenc.c:100
ff_convert_lang_to
const char * ff_convert_lang_to(const char *lang, enum AVLangCodespace target_codespace)
Convert a language code to a target codespace.
Definition: avlanguage.c:735
MATROSKA_ID_FILEDATA
#define MATROSKA_ID_FILEDATA
Definition: matroska.h:258
ebml_master::sizebytes
int sizebytes
how many bytes were reserved for the size
Definition: matroskaenc.c:89
MATROSKA_ID_VIDEOPROJECTIONPOSEPITCH
#define MATROSKA_ID_VIDEOPROJECTIONPOSEPITCH
Definition: matroska.h:166
size
int size
Definition: twinvq_data.h:10344
MATROSKA_ID_BLOCKMORE
#define MATROSKA_ID_BLOCKMORE
Definition: matroska.h:241
MAX_SEEKHEAD_ENTRIES
#define MAX_SEEKHEAD_ENTRIES
Definition: matroskaenc.c:65
mkv_cuepoint::pts
uint64_t pts
Definition: matroskaenc.c:162
ebml_stored_master::bc
AVIOContext * bc
Definition: matroskaenc.c:93
EbmlWriter::current_master_element
int current_master_element
Definition: matroskaenc.c:140
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
MKBETAG
#define MKBETAG(a, b, c, d)
Definition: macros.h:56
AV_CODEC_ID_QDMC
@ AV_CODEC_ID_QDMC
Definition: codec_id.h:479
put_ebml_void
static void put_ebml_void(AVIOContext *pb, int size)
Write a void element of a given size.
Definition: matroskaenc.c:396
AV_RB32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:96
MATROSKA_ID_VIDEOCOLORMAXFALL
#define MATROSKA_ID_VIDEOCOLORMAXFALL
Definition: matroska.h:148
EbmlElement::size
uint64_t size
excluding id and length field
Definition: matroskaenc.c:126
FF_PUT_WAV_HEADER_FORCE_WAVEFORMATEX
#define FF_PUT_WAV_HEADER_FORCE_WAVEFORMATEX
Tell ff_put_wav_header() to use WAVEFORMATEX even for PCM codecs.
Definition: riff.h:53
AVFMT_ALLOW_FLUSH
#define AVFMT_ALLOW_FLUSH
Format allows flushing.
Definition: avformat.h:490
AV_CODEC_ID_OPUS
@ AV_CODEC_ID_OPUS
Definition: codec_id.h:489
AVCHROMA_LOC_UNSPECIFIED
@ AVCHROMA_LOC_UNSPECIFIED
Definition: pixfmt.h:621
AV_DISPOSITION_DUB
#define AV_DISPOSITION_DUB
The stream is not in original language.
Definition: avformat.h:834
MatroskaMuxContext::cur_audio_pkt
AVPacket * cur_audio_pkt
Definition: matroskaenc.c:229
EBML_UINT
@ EBML_UINT
Definition: matroskaenc.c:98
AVStream::sample_aspect_ratio
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:1017
av1.h
AVIO_DATA_MARKER_SYNC_POINT
@ AVIO_DATA_MARKER_SYNC_POINT
A point in the output bytestream where a decoder can start decoding (i.e.
Definition: avio.h:123
mkv_track::codecpriv_size
unsigned codecpriv_size
size reserved for CodecPrivate excluding header+length field
Definition: matroskaenc.c:189
start_ebml_master_crc32
static int start_ebml_master_crc32(AVIOContext **dyn_cp, MatroskaMuxContext *mkv)
Definition: matroskaenc.c:750
MATROSKA_ID_POINTENTRY
#define MATROSKA_ID_POINTENTRY
Definition: matroska.h:200
dovi_isom.h
header
static const uint8_t header[24]
Definition: sdr2.c:67
AV_DISPOSITION_HEARING_IMPAIRED
#define AV_DISPOSITION_HEARING_IMPAIRED
The stream is intended for hearing impaired audiences.
Definition: avformat.h:863
MATROSKA_ID_BLKADDIDEXTRADATA
#define MATROSKA_ID_BLKADDIDEXTRADATA
Definition: matroska.h:197
CASE
#define CASE(type, variable)
ff_isom_write_av1c
int ff_isom_write_av1c(AVIOContext *pb, const uint8_t *buf, int size, int write_seq_header)
Writes AV1 extradata (Sequence Header and Metadata OBUs) to the provided AVIOContext.
Definition: av1.c:398
AVPacket::dts
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:373
MAX_FIELD_ORDER_ELEMS
#define MAX_FIELD_ORDER_ELEMS
Definition: matroskaenc.c:1443
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:232
avio_wb32
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:394
AVSphericalMapping::padding
uint32_t padding
Number of pixels to pad from the edge of each cube face.
Definition: spherical.h:182
additional_subtitle_tags
static const AVCodecTag additional_subtitle_tags[]
Definition: matroskaenc.c:3268
EbmlElement::mkv
struct MatroskaMuxContext * mkv
used by EBML_BLOCK
Definition: matroskaenc.c:133
avio_wl32
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:386
av_crc_get_table
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition: crc.c:374
AV_CODEC_ID_RV30
@ AV_CODEC_ID_RV30
Definition: codec_id.h:118
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
mkv_write_tag_targets
static void mkv_write_tag_targets(MatroskaMuxContext *mkv, AVIOContext *pb, uint32_t elementid, uint64_t uid)
Definition: matroskaenc.c:2013
AV_PKT_DATA_CONTENT_LIGHT_LEVEL
@ AV_PKT_DATA_CONTENT_LIGHT_LEVEL
Content light level (based on CTA-861.3).
Definition: packet.h:236
BlockContext::flags
uint8_t flags
Definition: matroskaenc.c:113
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:380
av_dict_free
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
Definition: dict.c:203
version
version
Definition: libkvazaar.c:313
mkv_write_trailer
static int mkv_write_trailer(AVFormatContext *s)
Definition: matroskaenc.c:2918
AV_STEREO3D_FLAG_INVERT
#define AV_STEREO3D_FLAG_INVERT
Inverted views, Right/Bottom represents the left view.
Definition: stereo3d.h:167
MATROSKA_ID_VIDEOPIXELHEIGHT
#define MATROSKA_ID_VIDEOPIXELHEIGHT
Definition: matroska.h:121
FF_COMPLIANCE_NORMAL
#define FF_COMPLIANCE_NORMAL
Definition: avcodec.h:1312
mkv_write_tags
static int mkv_write_tags(AVFormatContext *s)
Definition: matroskaenc.c:2078
AV_CODEC_ID_SVQ1
@ AV_CODEC_ID_SVQ1
Definition: codec_id.h:72
mkv_cuepoint::stream_idx
int stream_idx
Definition: matroskaenc.c:163
EBML_MASTER
@ EBML_MASTER
Definition: matroskaenc.c:106
MATROSKA_ID_MUXINGAPP
#define MATROSKA_ID_MUXINGAPP
Definition: matroska.h:70
mkv_write_video_color
static void mkv_write_video_color(EbmlWriter *writer, const AVStream *st, const AVCodecParameters *par)
Definition: matroskaenc.c:1295
MatroskaMuxContext::duration
int64_t duration
Definition: matroskaenc.c:213
MATROSKA_ID_EDITIONFLAGDEFAULT
#define MATROSKA_ID_EDITIONFLAGDEFAULT
Definition: matroska.h:272
MATROSKA_ID_FILEDESC
#define MATROSKA_ID_FILEDESC
Definition: matroska.h:255
avcodec_get_name
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
Definition: utils.c:447
AV_FIELD_BB
@ AV_FIELD_BB
Definition: codec_par.h:41
MAX_SEEKENTRY_SIZE
#define MAX_SEEKENTRY_SIZE
2 bytes * 3 for EBML IDs, 3 1-byte EBML lengths, 8 bytes for 64 bit offset, 4 bytes for target EBML I...
Definition: matroskaenc.c:255
MATROSKA_ID_VIDEOPROJECTIONTYPE
#define MATROSKA_ID_VIDEOPROJECTIONTYPE
Definition: matroska.h:163
ff_isom_write_hvcc
int ff_isom_write_hvcc(AVIOContext *pb, const uint8_t *data, int size, int ps_array_completeness)
Writes HEVC extradata (parameter sets, declarative SEI NAL units) to the provided AVIOContext.
Definition: hevc.c:1067
av_double2int
static av_always_inline uint64_t av_double2int(double f)
Reinterpret a double as a 64-bit integer.
Definition: intfloat.h:70
ebml_master
Definition: matroskaenc.c:87
MATROSKA_ID_FILENAME
#define MATROSKA_ID_FILENAME
Definition: matroska.h:256
av_assert2
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:64
EbmlMaster
Definition: matroskaenc.c:117
AV_CODEC_ID_RA_288
@ AV_CODEC_ID_RA_288
Definition: codec_id.h:417
ebml_stored_master::pos
int64_t pos
Definition: matroskaenc.c:94
AVSphericalMapping::roll
int32_t roll
Rotation around the forward vector [-180, 180].
Definition: spherical.h:128
MATROSKA_ID_CODECID
#define MATROSKA_ID_CODECID
Definition: matroska.h:88
AVIOContext::direct
int direct
avio_read and avio_write should if possible be satisfied directly instead of going through a buffer,...
Definition: avio.h:270
AV_CODEC_ID_RV10
@ AV_CODEC_ID_RV10
Definition: codec_id.h:55
AVFMT_GLOBALHEADER
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition: avformat.h:480
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:48
AVOutputFormat
Definition: avformat.h:509
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:367
avio_internal.h
av_get_bytes_per_sample
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition: samplefmt.c:108
put_ebml_length
static void put_ebml_length(AVIOContext *pb, uint64_t length, int bytes)
Write a length as EBML variable length integer.
Definition: matroskaenc.c:331
av_packet_get_side_data
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, size_t *size)
Get side information from packet.
Definition: avpacket.c:251
AV_CODEC_ID_THEORA
@ AV_CODEC_ID_THEORA
Definition: codec_id.h:80
av_builtin_constant_p
#define av_builtin_constant_p
Definition: attributes.h:160
EbmlElement::uint
uint64_t uint
Definition: matroskaenc.c:128
AVCodecParameters::height
int height
Definition: codec_par.h:128
AV_TIME_BASE
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:254
MATROSKA_ID_CUECLUSTERPOSITION
#define MATROSKA_ID_CUECLUSTERPOSITION
Definition: matroska.h:208
mkv_cuepoint::cluster_pos
int64_t cluster_pos
offset of the cluster containing the block relative to the segment
Definition: matroskaenc.c:164
mkv_seekhead
Definition: matroskaenc.c:154
MATROSKA_ID_CUETRACK
#define MATROSKA_ID_CUETRACK
Definition: matroska.h:207
mkv_seekhead::entries
mkv_seekhead_entry entries[MAX_SEEKHEAD_ENTRIES]
Definition: matroskaenc.c:156
MATROSKA_ID_SEEKPOSITION
#define MATROSKA_ID_SEEKPOSITION
Definition: matroska.h:233
ff_metadata_conv
void ff_metadata_conv(AVDictionary **pm, const AVMetadataConv *d_conv, const AVMetadataConv *s_conv)
Definition: metadata.c:26
AV_STEREO3D_TOPBOTTOM
@ AV_STEREO3D_TOPBOTTOM
Views are on top of each other.
Definition: stereo3d.h:79
av_assert1
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
MATROSKA_ID_CLUSTER
#define MATROSKA_ID_CLUSTER
Definition: matroska.h:62
EbmlElement::id
uint32_t id
Definition: matroskaenc.c:123
MATROSKA_ID_TRACKLANGUAGE
#define MATROSKA_ID_TRACKLANGUAGE
Definition: matroska.h:97
AV_CODEC_ID_RV20
@ AV_CODEC_ID_RV20
Definition: codec_id.h:56
AV_CODEC_ID_HEVC
@ AV_CODEC_ID_HEVC
Definition: codec_id.h:224
ebml_master::pos
int64_t pos
absolute offset in the containing AVIOContext where the master's elements start
Definition: matroskaenc.c:88
AVMEDIA_TYPE_ATTACHMENT
@ AVMEDIA_TYPE_ATTACHMENT
Opaque data information usually sparse.
Definition: avutil.h:205
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
MATROSKA_ID_VIDEOFLAGINTERLACED
#define MATROSKA_ID_VIDEOFLAGINTERLACED
Definition: matroska.h:127
EBML_WRITER
#define EBML_WRITER(max_nb_elems)
Definition: matroskaenc.c:144
AV_CODEC_ID_PCM_S32BE
@ AV_CODEC_ID_PCM_S32BE
Definition: codec_id.h:329
EBML_ID_CRC32
#define EBML_ID_CRC32
Definition: matroska.h:46
ff_mkv_metadata_conv
const AVMetadataConv ff_mkv_metadata_conv[]
Definition: matroska.c:124
av_inv_q
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:159
AV_PKT_DATA_SKIP_SAMPLES
@ AV_PKT_DATA_SKIP_SAMPLES
Recommmends skipping the specified number of samples.
Definition: packet.h:157
AVCodecParameters::color_range
enum AVColorRange color_range
Video only.
Definition: codec_par.h:147
AVMasteringDisplayMetadata
Mastering display metadata capable of representing the color volume of the display used to master the...
Definition: mastering_display_metadata.h:38
len
int len
Definition: vorbis_enc_data.h:426
AVCOL_SPC_UNSPECIFIED
@ AVCOL_SPC_UNSPECIFIED
Definition: pixfmt.h:530
av_rescale
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:129
mkv_check_tag_name
static int mkv_check_tag_name(const char *name, uint32_t elementid)
Definition: matroskaenc.c:2023
MPEG4AudioConfig::ext_sample_rate
int ext_sample_rate
Definition: mpeg4audio.h:40
MATROSKA_ID_CHAPTERUID
#define MATROSKA_ID_CHAPTERUID
Definition: matroska.h:274
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:272
mkv_reformat_av1
static int mkv_reformat_av1(MatroskaMuxContext *mkv, AVIOContext *pb, const AVPacket *pkt, int *size)
Definition: matroskaenc.c:2548
ebml_writer_close_or_discard_master
static void ebml_writer_close_or_discard_master(EbmlWriter *writer)
Definition: matroskaenc.c:467
AVCodecParameters::field_order
enum AVFieldOrder field_order
Video only.
Definition: codec_par.h:142
mkv_write_tag
static int mkv_write_tag(MatroskaMuxContext *mkv, const AVDictionary *m, AVIOContext **pb, unsigned reserved_size, uint32_t elementid, uint64_t uid)
Definition: matroskaenc.c:2037
MATROSKA_VIDEO_STEREOMODE_TYPE_RIGHT_LEFT
@ MATROSKA_VIDEO_STEREOMODE_TYPE_RIGHT_LEFT
Definition: matroska.h:325
mkv_end_cluster
static int mkv_end_cluster(AVFormatContext *s)
Definition: matroskaenc.c:2660
MATROSKA_ID_VIDEOPROJECTIONPOSEYAW
#define MATROSKA_ID_VIDEOPROJECTIONPOSEYAW
Definition: matroska.h:165
AVFMT_TS_NONSTRICT
#define AVFMT_TS_NONSTRICT
Format does not require strictly increasing timestamps, but they must still be monotonic.
Definition: avformat.h:491
ebml_write_header
static void ebml_write_header(AVIOContext *pb, const char *doctype, int version)
Definition: matroskaenc.c:2317
av_cmp_q
static int av_cmp_q(AVRational a, AVRational b)
Compare two rationals.
Definition: rational.h:89
version.h
OFFSET
#define OFFSET(x)
Definition: matroskaenc.c:3275
mkv_assemble_cues
static int mkv_assemble_cues(AVStream **streams, AVIOContext *dyn_cp, AVIOContext *cuepoint, const mkv_cues *cues, mkv_track *tracks, int num_tracks, uint64_t offset)
Definition: matroskaenc.c:945
BlockContext::h2645_nalu_list
NALUList h2645_nalu_list
Definition: matroskaenc.c:114
AVStream::disposition
int disposition
Stream disposition - a combination of AV_DISPOSITION_* flags.
Definition: avformat.h:1008
AV_DISPOSITION_VISUAL_IMPAIRED
#define AV_DISPOSITION_VISUAL_IMPAIRED
The stream is intended for visually impaired audiences.
Definition: avformat.h:867
tag
uint32_t tag
Definition: movenc.c:1646
ffio_free_dyn_buf
void ffio_free_dyn_buf(AVIOContext **s)
Free a dynamic buffer.
Definition: aviobuf.c:1556
AVFMT_FLAG_BITEXACT
#define AVFMT_FLAG_BITEXACT
When muxing, try to avoid writing any random/volatile data to the output.
Definition: avformat.h:1348
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:948
mkv_track::sample_rate
int sample_rate
Definition: matroskaenc.c:182
AV_PKT_DATA_DOVI_CONF
@ AV_PKT_DATA_DOVI_CONF
DOVI configuration ref: dolby-vision-bitstreams-within-the-iso-base-media-file-format-v2....
Definition: packet.h:284
mkv_write_block
static int mkv_write_block(void *logctx, MatroskaMuxContext *mkv, AVIOContext *pb, const AVCodecParameters *par, mkv_track *track, const AVPacket *pkt, int keyframe, int64_t ts, uint64_t duration, int force_blockgroup, int64_t relative_packet_pos)
Definition: matroskaenc.c:2585
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:260