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"
48 #include "libavutil/intfloat.h"
49 #include "libavutil/intreadwrite.h"
50 #include "libavutil/lfg.h"
52 #include "libavutil/mathematics.h"
53 #include "libavutil/opt.h"
54 #include "libavutil/parseutils.h"
55 #include "libavutil/pixdesc.h"
56 #include "libavutil/random_seed.h"
57 #include "libavutil/rational.h"
58 #include "libavutil/samplefmt.h"
59 #include "libavutil/stereo3d.h"
60 
61 #include "libavcodec/av1.h"
62 #include "libavcodec/bytestream.h"
63 #include "libavcodec/codec_desc.h"
64 #include "libavcodec/codec_par.h"
65 #include "libavcodec/defs.h"
66 #include "libavcodec/xiph.h"
67 #include "libavcodec/mpeg4audio.h"
68 
69 /* Level 1 elements we create a SeekHead entry for:
70  * Info, Tracks, Chapters, Attachments, Tags (potentially twice) and Cues */
71 #define MAX_SEEKHEAD_ENTRIES 7
72 
73 /* Largest known-length EBML length */
74 #define MAX_EBML_LENGTH ((1ULL << 56) - 2)
75 /* The dynamic buffer API we rely upon has a limit of INT_MAX;
76  * and so has avio_write(). */
77 #define MAX_SUPPORTED_EBML_LENGTH FFMIN(MAX_EBML_LENGTH, INT_MAX)
78 
79 #define MODE_MATROSKAv2 0x01
80 #define MODE_WEBM 0x02
81 
82 #define IS_WEBM(mkv) (CONFIG_WEBM_MUXER && CONFIG_MATROSKA_MUXER ? \
83  ((mkv)->mode == MODE_WEBM) : CONFIG_WEBM_MUXER)
84 #define IS_SEEKABLE(pb, mkv) (((pb)->seekable & AVIO_SEEKABLE_NORMAL) && \
85  !(mkv)->is_live)
86 
87 enum {
91 };
92 
93 typedef struct ebml_master {
94  int64_t pos; ///< absolute offset in the containing AVIOContext where the master's elements start
95  int sizebytes; ///< how many bytes were reserved for the size
96 } ebml_master;
97 
98 typedef struct ebml_stored_master {
100  int64_t pos;
102 
103 typedef enum EbmlType {
111  EBML_BLOCK, ///< pseudo-type for writing (Simple)Blocks
113 } EbmlType;
114 
115 typedef struct BlockContext {
116  struct mkv_track *track;
117  const AVPacket *pkt;
118  int16_t rel_ts;
119  uint8_t flags;
121 } BlockContext;
122 
123 typedef struct EbmlMaster {
124  int nb_elements; ///< -1 if not finished
125  int containing_master; ///< -1 if no parent exists
126 } EbmlMaster;
127 
128 typedef struct EbmlElement {
129  uint32_t id;
131  unsigned length_size;
132  uint64_t size; ///< excluding id and length field
133  union {
134  uint64_t uint;
135  int64_t sint;
136  double f;
137  const char *str;
138  const uint8_t *bin;
139  struct MatroskaMuxContext *mkv; ///< used by EBML_BLOCK
141  } priv;
142 } EbmlElement;
143 
144 typedef struct EbmlWriter {
145  unsigned nb_elements;
148 } EbmlWriter;
149 
150 #define EBML_WRITER(max_nb_elems) \
151  EbmlElement elements[max_nb_elems]; \
152  EbmlWriter writer = (EbmlWriter){ .elements = elements, \
153  .current_master_element = -1 }
154 
155 typedef struct mkv_seekhead_entry {
156  uint32_t elementid;
157  uint64_t segmentpos;
159 
160 typedef struct mkv_seekhead {
161  int64_t filepos;
165 } mkv_seekhead;
166 
167 typedef struct mkv_cuepoint {
168  uint64_t pts;
170  int64_t cluster_pos; ///< offset of the cluster containing the block relative to the segment
171  int64_t relative_pos; ///< relative offset from the position of the cluster containing the block
172  int64_t duration; ///< duration of the block according to time base
173 } mkv_cuepoint;
174 
175 typedef struct mkv_cues {
178 } mkv_cues;
179 
180 struct MatroskaMuxContext;
181 
182 typedef struct mkv_track {
184  int has_cue;
185  uint64_t uid;
186  unsigned track_num;
189  unsigned offset;
191  int64_t last_timestamp;
192  int64_t duration;
194  uint64_t max_blockaddid;
197  unsigned codecpriv_size; ///< size reserved for CodecPrivate excluding header+length field
198  int64_t ts_offset;
201  /* This callback will be called twice: First with a NULL AVIOContext
202  * to return the size of the (Simple)Block's data via size
203  * and a second time with the AVIOContext set when the data
204  * shall be written.
205  * The callback shall not return an error on the second call. */
207  const AVPacket *, int *size);
208 } mkv_track;
209 
210 typedef struct MatroskaMuxContext {
211  const AVClass *class;
213 
214  int mode;
218  int64_t segment_offset;
220  int64_t cluster_pos; ///< file offset of the current Cluster
221  int64_t cluster_pts;
223  int64_t duration;
227  int64_t cues_pos;
228 
230 
231  /* Used as temporary buffer to use the minimal amount of bytes
232  * to write the length field of EBML Masters.
233  * Every user has to reset the buffer after using it and
234  * different uses may not overlap. It is currently used in
235  * mkv_write_tag(), in mkv_assemble_cues() as well as in
236  * mkv_update_codecprivate() and mkv_write_track(). */
238 
240 
241  unsigned nb_attachments;
243 
246 
251  int is_live;
252 
253  int is_dash;
259 
260  uint32_t segment_uid[4];
262 
263 /** 2 bytes * 3 for EBML IDs, 3 1-byte EBML lengths, 8 bytes for 64 bit
264  * offset, 4 bytes for target EBML ID */
265 #define MAX_SEEKENTRY_SIZE 21
266 
267 /** 4 * (1-byte EBML ID, 1-byte EBML size, 8-byte uint max) */
268 #define MAX_CUETRACKPOS_SIZE 40
269 
270 /** DURATION_STRING_LENGTH must be <= 112 or the containing
271  * simpletag will need more than one byte for its length field. */
272 #define DURATION_STRING_LENGTH 19
273 
274 /** 2 + 1 Simpletag header, 2 + 1 + 8 Name "DURATION", rest for TagString */
275 #define DURATION_SIMPLETAG_SIZE (2 + 1 + (2 + 1 + 8) + (2 + 1 + DURATION_STRING_LENGTH))
276 
277 /** Seek preroll value for opus */
278 #define OPUS_SEEK_PREROLL 80000000
279 
280 static int ebml_id_size(uint32_t id)
281 {
282  return (av_log2(id) + 7U) / 8;
283 }
284 
285 static void put_ebml_id(AVIOContext *pb, uint32_t id)
286 {
287  int i = ebml_id_size(id);
288  while (i--)
289  avio_w8(pb, (uint8_t)(id >> (i * 8)));
290 }
291 
292 /**
293  * Write an EBML size meaning "unknown size".
294  *
295  * @param bytes The number of bytes the size should occupy (maximum: 8).
296  */
297 static void put_ebml_size_unknown(AVIOContext *pb, int bytes)
298 {
299  av_assert0(bytes <= 8);
300  avio_w8(pb, 0x1ff >> bytes);
301  if (av_builtin_constant_p(bytes) && bytes == 1)
302  return;
303  ffio_fill(pb, 0xff, bytes - 1);
304 }
305 
306 /**
307  * Returns how many bytes are needed to represent a number
308  * as EBML variable length integer.
309  */
310 static int ebml_num_size(uint64_t num)
311 {
312  int bytes = 0;
313  do {
314  bytes++;
315  } while (num >>= 7);
316  return bytes;
317 }
318 
319 /**
320  * Calculate how many bytes are needed to represent the length field
321  * of an EBML element whose payload has a given length.
322  */
323 static int ebml_length_size(uint64_t length)
324 {
325  return ebml_num_size(length + 1);
326 }
327 
328 /**
329  * Write a number as EBML variable length integer on `bytes` bytes.
330  * `bytes` is taken literally without checking.
331  */
332 static void put_ebml_num(AVIOContext *pb, uint64_t num, int bytes)
333 {
334  num |= 1ULL << bytes * 7;
335  for (int i = bytes - 1; i >= 0; i--)
336  avio_w8(pb, (uint8_t)(num >> i * 8));
337 }
338 
339 /**
340  * Write a length as EBML variable length integer.
341  *
342  * @param bytes The number of bytes that need to be used to write the number.
343  * If zero, the minimal number of bytes will be used.
344  */
345 static void put_ebml_length(AVIOContext *pb, uint64_t length, int bytes)
346 {
347  int needed_bytes = ebml_length_size(length);
348 
349  // sizes larger than this are currently undefined in EBML
350  av_assert0(length < (1ULL << 56) - 1);
351 
352  if (bytes == 0)
353  bytes = needed_bytes;
354  // The bytes needed to write the given size must not exceed
355  // the bytes that we ought to use.
356  av_assert0(bytes >= needed_bytes);
357  put_ebml_num(pb, length, bytes);
358 }
359 
360 /**
361  * Write a (random) UID with fixed size to make the output more deterministic
362  */
363 static void put_ebml_uid(AVIOContext *pb, uint32_t elementid, uint64_t uid)
364 {
365  put_ebml_id(pb, elementid);
366  put_ebml_length(pb, 8, 0);
367  avio_wb64(pb, uid);
368 }
369 
370 static void put_ebml_uint(AVIOContext *pb, uint32_t elementid, uint64_t val)
371 {
372  int i, bytes = 1;
373  uint64_t tmp = val;
374  while (tmp >>= 8)
375  bytes++;
376 
377  put_ebml_id(pb, elementid);
378  put_ebml_length(pb, bytes, 0);
379  for (i = bytes - 1; i >= 0; i--)
380  avio_w8(pb, (uint8_t)(val >> i * 8));
381 }
382 
383 static void put_ebml_float(AVIOContext *pb, uint32_t elementid, double val)
384 {
385  put_ebml_id(pb, elementid);
386  put_ebml_length(pb, 8, 0);
388 }
389 
390 static void put_ebml_binary(AVIOContext *pb, uint32_t elementid,
391  const void *buf, int size)
392 {
393  put_ebml_id(pb, elementid);
394  put_ebml_length(pb, size, 0);
395  avio_write(pb, buf, size);
396 }
397 
398 static void put_ebml_string(AVIOContext *pb, uint32_t elementid,
399  const char *str)
400 {
401  put_ebml_binary(pb, elementid, str, strlen(str));
402 }
403 
404 /**
405  * Write a void element of a given size. Useful for reserving space in
406  * the file to be written to later.
407  *
408  * @param size The number of bytes to reserve, which must be at least 2.
409  */
410 static void put_ebml_void(AVIOContext *pb, int size)
411 {
412  av_assert0(size >= 2);
413 
415  // we need to subtract the length needed to store the size from the
416  // size we need to reserve so 2 cases, we use 8 bytes to store the
417  // size if possible, 1 byte otherwise
418  if (size < 10) {
419  size -= 2;
420  put_ebml_length(pb, size, 0);
421  } else {
422  size -= 9;
423  put_ebml_length(pb, size, 8);
424  }
425  ffio_fill(pb, 0, size);
426 }
427 
428 static ebml_master start_ebml_master(AVIOContext *pb, uint32_t elementid,
429  uint64_t expectedsize)
430 {
431  int bytes = expectedsize ? ebml_length_size(expectedsize) : 8;
432 
433  put_ebml_id(pb, elementid);
434  put_ebml_size_unknown(pb, bytes);
435  return (ebml_master) { avio_tell(pb), bytes };
436 }
437 
439 {
440  int64_t pos = avio_tell(pb);
441 
442  if (avio_seek(pb, master.pos - master.sizebytes, SEEK_SET) < 0)
443  return;
444  put_ebml_length(pb, pos - master.pos, master.sizebytes);
445  avio_seek(pb, pos, SEEK_SET);
446 }
447 
449  uint32_t id, EbmlType type)
450 {
451  writer->elements[writer->nb_elements].id = id;
452  writer->elements[writer->nb_elements].type = type;
453  return &writer->elements[writer->nb_elements++];
454 }
455 
456 static void ebml_writer_open_master(EbmlWriter *writer, uint32_t id)
457 {
458  EbmlElement *const elem = ebml_writer_add(writer, id, EBML_MASTER);
459  EbmlMaster *const master = &elem->priv.master;
460 
461  master->containing_master = writer->current_master_element;
462  master->nb_elements = -1;
463 
464  writer->current_master_element = writer->nb_elements - 1;
465 }
466 
468 {
469  EbmlElement *elem;
470  av_assert2(writer->current_master_element >= 0);
471  av_assert2(writer->current_master_element < writer->nb_elements);
472  elem = &writer->elements[writer->current_master_element];
473  av_assert2(elem->type == EBML_MASTER);
474  av_assert2(elem->priv.master.nb_elements < 0); /* means unset */
475  elem->priv.master.nb_elements = writer->nb_elements - writer->current_master_element - 1;
479 }
480 
482 {
483  av_assert2(writer->nb_elements > 0);
484  av_assert2(0 <= writer->current_master_element);
485  av_assert2(writer->current_master_element < writer->nb_elements);
486  if (writer->current_master_element == writer->nb_elements - 1) {
487  const EbmlElement *const elem = &writer->elements[writer->nb_elements - 1];
488  /* The master element has no children. Discard it. */
489  av_assert2(elem->type == EBML_MASTER);
493  writer->nb_elements--;
494  return;
495  }
496  ebml_writer_close_master(writer);
497 }
498 
499 static void ebml_writer_add_string(EbmlWriter *writer, uint32_t id,
500  const char *str)
501 {
502  EbmlElement *const elem = ebml_writer_add(writer, id, EBML_STR);
503 
504  elem->priv.str = str;
505 }
506 
507 static void ebml_writer_add_bin(EbmlWriter *writer, uint32_t id,
508  const uint8_t *data, size_t size)
509 {
510  EbmlElement *const elem = ebml_writer_add(writer, id, EBML_BIN);
511 
512 #if SIZE_MAX > UINT64_MAX
513  size = FFMIN(size, UINT64_MAX);
514 #endif
515  elem->size = size;
516  elem->priv.bin = data;
517 }
518 
519 static void ebml_writer_add_float(EbmlWriter *writer, uint32_t id,
520  double val)
521 {
522  EbmlElement *const elem = ebml_writer_add(writer, id, EBML_FLOAT);
523 
524  elem->priv.f = val;
525 }
526 
527 static void ebml_writer_add_uid(EbmlWriter *writer, uint32_t id,
528  uint64_t val)
529 {
530  EbmlElement *const elem = ebml_writer_add(writer, id, EBML_UID);
531  elem->priv.uint = val;
532 }
533 
534 static void ebml_writer_add_uint(EbmlWriter *writer, uint32_t id,
535  uint64_t val)
536 {
537  EbmlElement *elem = ebml_writer_add(writer, id, EBML_UINT);
538  elem->priv.uint = val;
539 }
540 
541 static void ebml_writer_add_sint(EbmlWriter *writer, uint32_t id,
542  int64_t val)
543 {
544  EbmlElement *elem = ebml_writer_add(writer, id, EBML_SINT);
545  elem->priv.sint = val;
546 }
547 
549 {
551  elem->priv.mkv = mkv;
552 }
553 
555 {
556  size_t len = strlen(elem->priv.str);
557 #if SIZE_MAX > UINT64_MAX
558  len = FF_MIN(len, UINT64_MAX);
559 #endif
560  elem->size = len;
561  return 0;
562 }
563 
564 static av_const int uint_size(uint64_t val)
565 {
566  int bytes = 0;
567  do {
568  bytes++;
569  } while (val >>= 8);
570  return bytes;
571 }
572 
574 {
575  elem->size = uint_size(elem->priv.uint);
576  return 0;
577 }
578 
579 static av_const int sint_size(int64_t val)
580 {
581  uint64_t tmp = 2 * (uint64_t)(val < 0 ? val^-1 : val);
582  return uint_size(tmp);
583 }
584 
586 {
587  elem->size = sint_size(elem->priv.sint);
588  return 0;
589 }
590 
591 static int ebml_writer_elem_len(EbmlWriter *writer, EbmlElement *elem,
592  int remaining_elems);
593 
595  int remaining_elems)
596 {
597  int nb_elems = elem->priv.master.nb_elements >= 0 ? elem->priv.master.nb_elements : remaining_elems - 1;
598  EbmlElement *const master = elem;
599  uint64_t total_size = 0;
600 
601  master->priv.master.nb_elements = nb_elems;
602  for (; elem++, nb_elems > 0;) {
603  int ret = ebml_writer_elem_len(writer, elem, nb_elems);
604  if (ret < 0)
605  return ret;
606  av_assert2(ret < nb_elems);
607  /* No overflow is possible here, as both total_size and elem->size
608  * are bounded by MAX_SUPPORTED_EBML_LENGTH. */
609  total_size += ebml_id_size(elem->id) + elem->length_size + elem->size;
610  if (total_size > MAX_SUPPORTED_EBML_LENGTH)
611  return AVERROR(ERANGE);
612  nb_elems--; /* consume elem */
613  elem += ret, nb_elems -= ret; /* and elem's children */
614  }
615  master->size = total_size;
616 
617  return master->priv.master.nb_elements;
618 }
619 
621 {
622  MatroskaMuxContext *const mkv = elem->priv.mkv;
623  BlockContext *const block = &mkv->cur_block;
624  mkv_track *const track = block->track;
625  const AVPacket *const pkt = block->pkt;
626  int err, size;
627 
628  if (track->reformat) {
629  err = track->reformat(mkv, NULL, pkt, &size);
630  if (err < 0) {
631  av_log(mkv->ctx, AV_LOG_ERROR, "Error when reformatting data of "
632  "a packet from stream %d.\n", pkt->stream_index);
633  return err;
634  }
635  } else {
636  size = pkt->size;
637  if (track->offset <= size)
638  size -= track->offset;
639  }
640  elem->size = track->track_num_size + 3U + size;
641 
642  return 0;
643 }
644 
645 static void ebml_writer_write_block(const EbmlElement *elem, AVIOContext *pb)
646 {
647  MatroskaMuxContext *const mkv = elem->priv.mkv;
648  BlockContext *const block = &mkv->cur_block;
649  mkv_track *const track = block->track;
650  const AVPacket *const pkt = block->pkt;
651 
652  put_ebml_num(pb, track->track_num, track->track_num_size);
653  avio_wb16(pb, block->rel_ts);
654  avio_w8(pb, block->flags);
655 
656  if (track->reformat) {
657  int size;
658  track->reformat(mkv, pb, pkt, &size);
659  } else {
660  const uint8_t *data = pkt->data;
661  unsigned offset = track->offset <= pkt->size ? track->offset : 0;
662  avio_write(pb, data + offset, pkt->size - offset);
663  }
664 }
665 
666 static int ebml_writer_elem_len(EbmlWriter *writer, EbmlElement *elem,
667  int remaining_elems)
668 {
669  int ret = 0;
670 
671  switch (elem->type) {
672  case EBML_FLOAT:
673  case EBML_UID:
674  elem->size = 8;
675  break;
676  case EBML_STR:
677  ret = ebml_writer_str_len(elem);
678  break;
679  case EBML_UINT:
680  ret = ebml_writer_uint_len(elem);
681  break;
682  case EBML_SINT:
683  ret = ebml_writer_sint_len(elem);
684  break;
685  case EBML_BLOCK:
686  ret = ebml_writer_block_len(elem);
687  break;
688  case EBML_MASTER:
689  ret = ebml_writer_master_len(writer, elem, remaining_elems);
690  break;
691  }
692  if (ret < 0)
693  return ret;
694  if (elem->size > MAX_SUPPORTED_EBML_LENGTH)
695  return AVERROR(ERANGE);
696  elem->length_size = ebml_length_size(elem->size);
697  return ret; /* number of elements consumed excluding elem itself */
698 }
699 
700 static int ebml_writer_elem_write(const EbmlElement *elem, AVIOContext *pb)
701 {
702  put_ebml_id(pb, elem->id);
703  put_ebml_num(pb, elem->size, elem->length_size);
704  switch (elem->type) {
705  case EBML_UID:
706  case EBML_FLOAT: {
707  uint64_t val = elem->type == EBML_UID ? elem->priv.uint
708  : av_double2int(elem->priv.f);
709  avio_wb64(pb, val);
710  break;
711  }
712  case EBML_UINT:
713  case EBML_SINT: {
714  uint64_t val = elem->type == EBML_UINT ? elem->priv.uint
715  : elem->priv.sint;
716  for (int i = elem->size; --i >= 0; )
717  avio_w8(pb, (uint8_t)(val >> i * 8));
718  break;
719  }
720  case EBML_STR:
721  case EBML_BIN: {
722  const uint8_t *data = elem->type == EBML_BIN ? elem->priv.bin
723  : (const uint8_t*)elem->priv.str;
724  avio_write(pb, data, elem->size);
725  break;
726  }
727  case EBML_BLOCK:
728  ebml_writer_write_block(elem, pb);
729  break;
730  case EBML_MASTER: {
731  int nb_elems = elem->priv.master.nb_elements;
732 
733  elem++;
734  for (int i = 0; i < nb_elems; i++)
735  i += ebml_writer_elem_write(elem + i, pb);
736 
737  return nb_elems;
738  }
739  }
740  return 0;
741 }
742 
743 static int ebml_writer_write(EbmlWriter *writer, AVIOContext *pb)
744 {
745  int ret = ebml_writer_elem_len(writer, writer->elements,
746  writer->nb_elements);
747  if (ret < 0)
748  return ret;
749  ebml_writer_elem_write(writer->elements, pb);
750  return 0;
751 }
752 
753 static void mkv_add_seekhead_entry(MatroskaMuxContext *mkv, uint32_t elementid,
754  uint64_t filepos)
755 {
756  mkv_seekhead *seekhead = &mkv->seekhead;
757 
759 
760  seekhead->entries[seekhead->num_entries].elementid = elementid;
761  seekhead->entries[seekhead->num_entries++].segmentpos = filepos - mkv->segment_offset;
762 }
763 
765 {
766  int ret;
767 
768  if (!*dyn_cp && (ret = avio_open_dyn_buf(dyn_cp)) < 0)
769  return ret;
770 
771  if (mkv->write_crc)
772  put_ebml_void(*dyn_cp, 6); /* Reserve space for CRC32 so position/size calculations using avio_tell() take it into account */
773 
774  return 0;
775 }
776 
778  MatroskaMuxContext *mkv, uint32_t id,
779  int length_size, int keep_buffer,
780  int add_seekentry)
781 {
782  uint8_t *buf, crc[4];
783  int ret, size, skip = 0;
784 
785  size = avio_get_dyn_buf(*dyn_cp, &buf);
786  if ((ret = (*dyn_cp)->error) < 0)
787  goto fail;
788 
789  if (add_seekentry)
790  mkv_add_seekhead_entry(mkv, id, avio_tell(pb));
791 
792  put_ebml_id(pb, id);
793  put_ebml_length(pb, size, length_size);
794  if (mkv->write_crc) {
795  skip = 6; /* Skip reserved 6-byte long void element from the dynamic buffer. */
796  AV_WL32(crc, av_crc(av_crc_get_table(AV_CRC_32_IEEE_LE), UINT32_MAX, buf + skip, size - skip) ^ UINT32_MAX);
797  put_ebml_binary(pb, EBML_ID_CRC32, crc, sizeof(crc));
798  }
799  avio_write(pb, buf + skip, size - skip);
800 
801 fail:
802  if (keep_buffer) {
803  ffio_reset_dyn_buf(*dyn_cp);
804  } else {
805  ffio_free_dyn_buf(dyn_cp);
806  }
807  return ret;
808 }
809 
810 /**
811  * Output EBML master. Keep the buffer if seekable, allowing for later updates.
812  * Furthermore always add a SeekHead Entry for this element.
813  */
815  ebml_stored_master *elem,
816  MatroskaMuxContext *mkv, uint32_t id)
817 {
818  if (IS_SEEKABLE(pb, mkv)) {
819  uint8_t *buf;
820  int size = avio_get_dyn_buf(elem->bc, &buf);
821 
822  if (elem->bc->error < 0)
823  return elem->bc->error;
824 
825  elem->pos = avio_tell(pb);
826  mkv_add_seekhead_entry(mkv, id, elem->pos);
827 
828  put_ebml_id(pb, id);
829  put_ebml_length(pb, size, 0);
830  avio_write(pb, buf, size);
831 
832  return 0;
833  } else
834  return end_ebml_master_crc32(pb, &elem->bc, mkv, id, 0, 0, 1);
835 }
836 
837 static void put_xiph_size(AVIOContext *pb, int size)
838 {
839  ffio_fill(pb, 255, size / 255);
840  avio_w8(pb, size % 255);
841 }
842 
843 /**
844  * Free the members allocated in the mux context.
845  */
847 {
848  MatroskaMuxContext *mkv = s->priv_data;
849 
851  ffio_free_dyn_buf(&mkv->info.bc);
852  ffio_free_dyn_buf(&mkv->track.bc);
853  ffio_free_dyn_buf(&mkv->tags.bc);
854  ffio_free_dyn_buf(&mkv->tmp_bc);
855 
857  av_freep(&mkv->cues.entries);
858  av_freep(&mkv->tracks);
859 }
860 
861 /**
862  * Initialize the SeekHead element to be ready to index level 1 Matroska
863  * elements. Enough space to write MAX_SEEKHEAD_ENTRIES SeekHead entries
864  * will be reserved at the current file location.
865  */
867 {
868  mkv->seekhead.filepos = avio_tell(pb);
869  // 21 bytes max for a Seek entry, 6 bytes max for the SeekHead ID
870  // and size, 6 bytes for a CRC32 element, and 2 bytes to guarantee
871  // that an EBML void element will fit afterwards
874 }
875 
876 /**
877  * Write the SeekHead to the file at the location reserved for it
878  * and seek to destpos afterwards. When error_on_seek_failure
879  * is not set, failure to seek to the position designated for the
880  * SeekHead is not considered an error and it is presumed that
881  * destpos is the current position; failure to seek to destpos
882  * afterwards is always an error.
883  *
884  * @return 0 on success, < 0 on error.
885  */
887  int error_on_seek_failure, int64_t destpos)
888 {
889  AVIOContext *dyn_cp = NULL;
890  mkv_seekhead *seekhead = &mkv->seekhead;
891  int64_t remaining, ret64;
892  int i, ret;
893 
894  if ((ret64 = avio_seek(pb, seekhead->filepos, SEEK_SET)) < 0)
895  return error_on_seek_failure ? ret64 : 0;
896 
897  ret = start_ebml_master_crc32(&dyn_cp, mkv);
898  if (ret < 0)
899  return ret;
900 
901  for (i = 0; i < seekhead->num_entries; i++) {
902  mkv_seekhead_entry *entry = &seekhead->entries[i];
905 
907  put_ebml_length(dyn_cp, ebml_id_size(entry->elementid), 0);
908  put_ebml_id(dyn_cp, entry->elementid);
909 
911  end_ebml_master(dyn_cp, seekentry);
912  }
913  ret = end_ebml_master_crc32(pb, &dyn_cp, mkv,
914  MATROSKA_ID_SEEKHEAD, 0, 0, 0);
915  if (ret < 0)
916  return ret;
917 
918  remaining = seekhead->filepos + seekhead->reserved_size - avio_tell(pb);
919  put_ebml_void(pb, remaining);
920 
921  if ((ret64 = avio_seek(pb, destpos, SEEK_SET)) < 0)
922  return ret64;
923 
924  return 0;
925 }
926 
927 static int mkv_add_cuepoint(MatroskaMuxContext *mkv, int stream, int64_t ts,
928  int64_t cluster_pos, int64_t relative_pos, int64_t duration)
929 {
930  mkv_cues *cues = &mkv->cues;
931  mkv_cuepoint *entries = cues->entries;
932  unsigned idx = cues->num_entries;
933 
934  if (ts < 0)
935  return 0;
936 
937  entries = av_realloc_array(entries, cues->num_entries + 1, sizeof(mkv_cuepoint));
938  if (!entries)
939  return AVERROR(ENOMEM);
940  cues->entries = entries;
941 
942  /* Make sure the cues entries are sorted by pts. */
943  while (idx > 0 && entries[idx - 1].pts > ts)
944  idx--;
945  memmove(&entries[idx + 1], &entries[idx],
946  (cues->num_entries - idx) * sizeof(entries[0]));
947 
948  entries[idx].pts = ts;
949  entries[idx].stream_idx = stream;
950  entries[idx].cluster_pos = cluster_pos - mkv->segment_offset;
951  entries[idx].relative_pos = relative_pos;
952  entries[idx].duration = duration;
953 
954  cues->num_entries++;
955 
956  return 0;
957 }
958 
959 static int mkv_assemble_cues(AVStream **streams, AVIOContext *dyn_cp, AVIOContext *cuepoint,
960  const mkv_cues *cues, mkv_track *tracks, int num_tracks,
961  uint64_t offset)
962 {
963  for (mkv_cuepoint *entry = cues->entries, *end = entry + cues->num_entries;
964  entry < end;) {
965  uint64_t pts = entry->pts;
966  uint8_t *buf;
967  int size;
968 
970 
971  // put all the entries from different tracks that have the exact same
972  // timestamp into the same CuePoint
973  for (int j = 0; j < num_tracks; j++)
974  tracks[j].has_cue = 0;
975  do {
976  ebml_master track_positions;
977  int idx = entry->stream_idx;
978 
979  av_assert0(idx >= 0 && idx < num_tracks);
980  if (tracks[idx].has_cue && streams[idx]->codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE)
981  continue;
982  tracks[idx].has_cue = 1;
984  put_ebml_uint(cuepoint, MATROSKA_ID_CUETRACK , tracks[idx].track_num);
985  put_ebml_uint(cuepoint, MATROSKA_ID_CUECLUSTERPOSITION , entry->cluster_pos + offset);
986  put_ebml_uint(cuepoint, MATROSKA_ID_CUERELATIVEPOSITION, entry->relative_pos);
987  if (entry->duration > 0)
988  put_ebml_uint(cuepoint, MATROSKA_ID_CUEDURATION , entry->duration);
989  end_ebml_master(cuepoint, track_positions);
990  } while (++entry < end && entry->pts == pts);
991  size = avio_get_dyn_buf(cuepoint, &buf);
992  if (cuepoint->error < 0)
993  return cuepoint->error;
995  ffio_reset_dyn_buf(cuepoint);
996  }
997 
998  return 0;
999 }
1000 
1002  const AVCodecParameters *par,
1003  const uint8_t *extradata, int extradata_size)
1004 {
1005  const uint8_t *header_start[3];
1006  int header_len[3];
1007  int first_header_size;
1008  int err, j;
1009 
1010  if (par->codec_id == AV_CODEC_ID_VORBIS)
1011  first_header_size = 30;
1012  else
1013  first_header_size = 42;
1014 
1015  err = avpriv_split_xiph_headers(extradata, extradata_size,
1016  first_header_size, header_start, header_len);
1017  if (err < 0) {
1018  av_log(s, AV_LOG_ERROR, "Extradata corrupt.\n");
1019  return err;
1020  }
1021 
1022  avio_w8(pb, 2); // number packets - 1
1023  for (j = 0; j < 2; j++) {
1024  put_xiph_size(pb, header_len[j]);
1025  }
1026  for (j = 0; j < 3; j++)
1027  avio_write(pb, header_start[j], header_len[j]);
1028 
1029  return 0;
1030 }
1031 
1032 #if CONFIG_MATROSKA_MUXER
1033 static int put_wv_codecpriv(AVIOContext *pb, const uint8_t *extradata, int extradata_size)
1034 {
1035  if (extradata && extradata_size == 2)
1036  avio_write(pb, extradata, 2);
1037  else
1038  avio_wl16(pb, 0x410); // fallback to the most recent version
1039  return 0;
1040 }
1041 
1042 static int put_flac_codecpriv(AVFormatContext *s, AVIOContext *pb,
1043  const AVCodecParameters *par,
1044  const uint8_t *extradata, int extradata_size)
1045 {
1046  int write_comment = (par->ch_layout.order == AV_CHANNEL_ORDER_NATIVE &&
1047  !(par->ch_layout.u.mask & ~0x3ffffULL) &&
1049  int ret = ff_flac_write_header(pb, extradata, extradata_size,
1050  !write_comment);
1051 
1052  if (ret < 0)
1053  return ret;
1054 
1055  if (write_comment) {
1056  const char *vendor = (s->flags & AVFMT_FLAG_BITEXACT) ?
1057  "Lavf" : LIBAVFORMAT_IDENT;
1058  AVDictionary *dict = NULL;
1059  uint8_t buf[32];
1060  int64_t len;
1061 
1062  snprintf(buf, sizeof(buf), "0x%"PRIx64, par->ch_layout.u.mask);
1063  av_dict_set(&dict, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", buf, 0);
1064 
1065  len = ff_vorbiscomment_length(dict, vendor, NULL, 0);
1066  av_assert1(len < (1 << 24) - 4);
1067 
1068  avio_w8(pb, 0x84);
1069  avio_wb24(pb, len);
1070 
1071  ff_vorbiscomment_write(pb, dict, vendor, NULL, 0);
1072 
1073  av_dict_free(&dict);
1074  }
1075 
1076  return 0;
1077 }
1078 
1079 static int get_aac_sample_rates(AVFormatContext *s, MatroskaMuxContext *mkv,
1080  const uint8_t *extradata, int extradata_size,
1081  int *sample_rate, int *output_sample_rate)
1082 {
1083  MPEG4AudioConfig mp4ac;
1084  int ret;
1085 
1086  ret = avpriv_mpeg4audio_get_config2(&mp4ac, extradata, extradata_size, 1, s);
1087  /* Don't abort if the failure is because of missing extradata. Assume in that
1088  * case a bitstream filter will provide the muxer with the extradata in the
1089  * first packet.
1090  * Abort however if s->pb is not seekable, as we would not be able to seek back
1091  * to write the sample rate elements once the extradata shows up, anyway. */
1092  if (ret < 0 && (extradata_size || !IS_SEEKABLE(s->pb, mkv))) {
1094  "Error parsing AAC extradata, unable to determine samplerate.\n");
1095  return AVERROR(EINVAL);
1096  }
1097 
1098  if (ret < 0) {
1099  /* This will only happen when this function is called while writing the
1100  * header and no extradata is available. The space for this element has
1101  * to be reserved for when this function is called again after the
1102  * extradata shows up in the first packet, as there's no way to know if
1103  * output_sample_rate will be different than sample_rate or not. */
1104  *output_sample_rate = *sample_rate;
1105  } else {
1106  *sample_rate = mp4ac.sample_rate;
1107  *output_sample_rate = mp4ac.ext_sample_rate;
1108  }
1109  return 0;
1110 }
1111 #endif
1112 
1114  const AVCodecParameters *par,
1115  const uint8_t *extradata,
1116  int extradata_size,
1117  unsigned *size_to_reserve)
1118 {
1119  switch (par->codec_id) {
1120  case AV_CODEC_ID_VORBIS:
1121  case AV_CODEC_ID_THEORA:
1122  return put_xiph_codecpriv(s, dyn_cp, par, extradata, extradata_size);
1123  case AV_CODEC_ID_AV1:
1124  if (extradata_size)
1125  return ff_isom_write_av1c(dyn_cp, extradata,
1126  extradata_size, 1);
1127  else
1128  *size_to_reserve = (AV1_SANE_SEQUENCE_HEADER_MAX_BITS + 7) / 8 + 100;
1129  break;
1130 #if CONFIG_MATROSKA_MUXER
1131  case AV_CODEC_ID_FLAC:
1132  return put_flac_codecpriv(s, dyn_cp, par, extradata, extradata_size);
1133  case AV_CODEC_ID_WAVPACK:
1134  return put_wv_codecpriv(dyn_cp, extradata, extradata_size);
1135  case AV_CODEC_ID_H264:
1136  return ff_isom_write_avcc(dyn_cp, extradata,
1137  extradata_size);
1138  case AV_CODEC_ID_HEVC:
1139  return ff_isom_write_hvcc(dyn_cp, extradata,
1140  extradata_size, 0);
1141  case AV_CODEC_ID_ALAC:
1142  if (extradata_size < 36) {
1144  "Invalid extradata found, ALAC expects a 36-byte "
1145  "QuickTime atom.");
1146  return AVERROR_INVALIDDATA;
1147  } else
1148  avio_write(dyn_cp, extradata + 12,
1149  extradata_size - 12);
1150  break;
1151  case AV_CODEC_ID_AAC:
1152  if (extradata_size)
1153  avio_write(dyn_cp, extradata, extradata_size);
1154  else
1155  *size_to_reserve = MAX_PCE_SIZE;
1156  break;
1157  case AV_CODEC_ID_ARIB_CAPTION: {
1158  unsigned stream_identifier, data_component_id;
1159  switch (par->profile) {
1161  stream_identifier = 0x30;
1162  data_component_id = 0x0008;
1163  break;
1165  stream_identifier = 0x87;
1166  data_component_id = 0x0012;
1167  break;
1168  default:
1170  "Unset/unknown ARIB caption profile %d utilized!\n",
1171  par->profile);
1172  return AVERROR_INVALIDDATA;
1173  }
1174  avio_w8(dyn_cp, stream_identifier);
1175  avio_wb16(dyn_cp, data_component_id);
1176  break;
1177  }
1178 #endif
1179  default:
1180  if (CONFIG_MATROSKA_MUXER && par->codec_id == AV_CODEC_ID_PRORES &&
1182  avio_wl32(dyn_cp, par->codec_tag);
1183  } else if (extradata_size && par->codec_id != AV_CODEC_ID_TTA)
1184  avio_write(dyn_cp, extradata, extradata_size);
1185  }
1186 
1187  return 0;
1188 }
1189 
1191  AVCodecParameters *par,
1192  const uint8_t *extradata, int extradata_size,
1193  int native_id, int qt_id,
1194  uint8_t **codecpriv, int *codecpriv_size,
1195  unsigned *max_payload_size)
1196 {
1197  MatroskaMuxContext av_unused *const mkv = s->priv_data;
1198  unsigned size_to_reserve = 0;
1199  int ret;
1200 
1201  if (native_id) {
1202  ret = mkv_assemble_native_codecprivate(s, dyn_cp, par,
1203  extradata, extradata_size,
1204  &size_to_reserve);
1205  if (ret < 0)
1206  return ret;
1207 #if CONFIG_MATROSKA_MUXER
1208  } else if (par->codec_type == AVMEDIA_TYPE_VIDEO) {
1209  if (qt_id) {
1210  if (!par->codec_tag)
1212  par->codec_id);
1214  && (!extradata_size || ff_codec_get_id(ff_codec_movvideo_tags, AV_RL32(extradata + 4)) != par->codec_id)
1215  ) {
1216  avio_wb32(dyn_cp, 0x5a + extradata_size);
1217  avio_wl32(dyn_cp, par->codec_tag);
1218  ffio_fill(dyn_cp, 0, 0x5a - 8);
1219  }
1220  avio_write(dyn_cp, extradata, extradata_size);
1221  } else {
1223  av_log(s, AV_LOG_WARNING, "codec %s is not supported by this format\n",
1224  avcodec_get_name(par->codec_id));
1225 
1226  if (!par->codec_tag)
1228  par->codec_id);
1229  if (!par->codec_tag && par->codec_id != AV_CODEC_ID_RAWVIDEO) {
1230  av_log(s, AV_LOG_ERROR, "No bmp codec tag found for codec %s\n",
1231  avcodec_get_name(par->codec_id));
1232  return AVERROR(EINVAL);
1233  }
1234 
1235  /* If vfw extradata updates are supported, this will have
1236  * to be updated to pass extradata(_size) explicitly. */
1237  ff_put_bmp_header(dyn_cp, par, 0, 0, mkv->flipped_raw_rgb);
1238  }
1239  } else if (par->codec_type == AVMEDIA_TYPE_AUDIO) {
1240  unsigned int tag;
1242  if (!tag) {
1243  av_log(s, AV_LOG_ERROR, "No wav codec tag found for codec %s\n",
1244  avcodec_get_name(par->codec_id));
1245  return AVERROR(EINVAL);
1246  }
1247  if (!par->codec_tag)
1248  par->codec_tag = tag;
1249 
1250  /* Same comment as for ff_put_bmp_header applies here. */
1252 #endif
1253  }
1254 
1255  *codecpriv_size = avio_get_dyn_buf(dyn_cp, codecpriv);
1256  if (dyn_cp->error < 0)
1257  return dyn_cp->error;
1258  *max_payload_size = *codecpriv_size + size_to_reserve;
1259 
1260  return 0;
1261 }
1262 
1263 static void mkv_put_codecprivate(AVIOContext *pb, unsigned max_payload_size,
1264  const uint8_t *codecpriv, unsigned codecpriv_size)
1265 {
1266  unsigned total_codecpriv_size = 0, total_size;
1267 
1268  av_assert1(codecpriv_size <= max_payload_size);
1269 
1270  if (!max_payload_size)
1271  return;
1272 
1273  total_size = 2 + ebml_length_size(max_payload_size) + max_payload_size;
1274 
1275  if (codecpriv_size) {
1276  unsigned length_size = ebml_length_size(codecpriv_size);
1277 
1278  total_codecpriv_size = 2U + length_size + codecpriv_size;
1279  if (total_codecpriv_size + 1 == total_size) {
1280  /* It is impossible to add one byte of padding via an EBML Void. */
1281  length_size++;
1282  total_codecpriv_size++;
1283  }
1285  put_ebml_length(pb, codecpriv_size, length_size);
1286  avio_write(pb, codecpriv, codecpriv_size);
1287  }
1288  if (total_codecpriv_size < total_size)
1289  put_ebml_void(pb, total_size - total_codecpriv_size);
1290 }
1291 
1293  uint8_t *side_data, int side_data_size,
1294  AVCodecParameters *par, AVIOContext *pb,
1295  mkv_track *track, unsigned alternative_size)
1296 {
1297  AVIOContext *const dyn_bc = mkv->tmp_bc;
1298  uint8_t *codecpriv;
1299  unsigned max_payload_size;
1300  int ret, codecpriv_size;
1301 
1302  ret = mkv_assemble_codecprivate(s, dyn_bc, par,
1303  side_data, side_data_size, 1, 0,
1304  &codecpriv, &codecpriv_size, &max_payload_size);
1305  if (ret < 0)
1306  goto fail;
1307  if (codecpriv_size > track->codecpriv_size && !alternative_size) {
1308  ret = AVERROR(ENOSPC);
1309  goto fail;
1310  } else if (codecpriv_size > track->codecpriv_size) {
1311  av_assert1(alternative_size < track->codecpriv_size);
1312  codecpriv_size = alternative_size;
1313  }
1314  avio_seek(pb, track->codecpriv_offset, SEEK_SET);
1316  codecpriv, codecpriv_size);
1317 
1318  if (!par->extradata_size) {
1319  ret = ff_alloc_extradata(par, side_data_size);
1320  if (ret < 0)
1321  goto fail;
1322  memcpy(par->extradata, side_data, side_data_size);
1323  }
1324 fail:
1325  ffio_reset_dyn_buf(dyn_bc);
1326  return ret;
1327 }
1328 
1329 #define MAX_VIDEO_COLOR_ELEMS 20
1330 static void mkv_write_video_color(EbmlWriter *writer, const AVStream *st,
1331  const AVCodecParameters *par)
1332 {
1333  const AVPacketSideData *side_data;
1334 
1336 
1337  if (par->color_trc != AVCOL_TRC_UNSPECIFIED &&
1338  par->color_trc < AVCOL_TRC_NB) {
1340  par->color_trc);
1341  }
1342  if (par->color_space != AVCOL_SPC_UNSPECIFIED &&
1343  par->color_space < AVCOL_SPC_NB) {
1345  par->color_space);
1346  }
1347  if (par->color_primaries != AVCOL_PRI_UNSPECIFIED &&
1348  par->color_primaries < AVCOL_PRI_NB) {
1350  par->color_primaries);
1351  }
1352  if (par->color_range != AVCOL_RANGE_UNSPECIFIED &&
1353  par->color_range < AVCOL_RANGE_NB) {
1355  }
1358  int xpos, ypos;
1359 
1360  av_chroma_location_enum_to_pos(&xpos, &ypos, par->chroma_location);
1362  (xpos >> 7) + 1);
1364  (ypos >> 7) + 1);
1365  }
1366 
1369  if (side_data) {
1370  const AVContentLightMetadata *metadata = (AVContentLightMetadata *)side_data->data;
1372  metadata->MaxCLL);
1374  metadata->MaxFALL);
1375  }
1376 
1379  if (side_data) {
1380  const AVMasteringDisplayMetadata *metadata = (AVMasteringDisplayMetadata *)side_data->data;
1382  if (metadata->has_primaries) {
1384  av_q2d(metadata->display_primaries[0][0]));
1386  av_q2d(metadata->display_primaries[0][1]));
1388  av_q2d(metadata->display_primaries[1][0]));
1390  av_q2d(metadata->display_primaries[1][1]));
1392  av_q2d(metadata->display_primaries[2][0]));
1394  av_q2d(metadata->display_primaries[2][1]));
1396  av_q2d(metadata->white_point[0]));
1398  av_q2d(metadata->white_point[1]));
1399  }
1400  if (metadata->has_luminance) {
1402  av_q2d(metadata->max_luminance));
1404  av_q2d(metadata->min_luminance));
1405  }
1407  }
1408 
1410 }
1411 
1412 #define MAX_VIDEO_PROJECTION_ELEMS 6
1413 static void mkv_handle_rotation(void *logctx, const AVStream *st,
1414  double *yaw, double *roll)
1415 {
1416  const int32_t *matrix;
1417  const AVPacketSideData *side_data =
1420 
1421  if (!side_data)
1422  return;
1423 
1424  matrix = (int32_t *)side_data->data;
1425 
1426  /* Check whether this is an affine transformation */
1427  if (matrix[2] || matrix[5])
1428  goto ignore;
1429 
1430  /* This together with the checks below test whether
1431  * the upper-left 2x2 matrix is nonsingular. */
1432  if (!matrix[0] && !matrix[1])
1433  goto ignore;
1434 
1435  /* We ignore the translation part of the matrix (matrix[6] and matrix[7])
1436  * as well as any scaling, i.e. we only look at the upper left 2x2 matrix.
1437  * We only accept matrices that are an exact multiple of an orthogonal one.
1438  * Apart from the multiple, every such matrix can be obtained by
1439  * potentially flipping in the x-direction (corresponding to yaw = 180)
1440  * followed by a rotation of (say) an angle phi in the counterclockwise
1441  * direction. The upper-left 2x2 matrix then looks like this:
1442  * | (+/-)cos(phi) (-/+)sin(phi) |
1443  * scale * | |
1444  * | sin(phi) cos(phi) |
1445  * The first set of signs in the first row apply in case of no flipping,
1446  * the second set applies in case of flipping. */
1447 
1448  /* The casts to int64_t are needed because -INT32_MIN doesn't fit
1449  * in an int32_t. */
1450  if (matrix[0] == matrix[4] && -(int64_t)matrix[1] == matrix[3]) {
1451  /* No flipping case */
1452  *yaw = 0;
1453  } else if (-(int64_t)matrix[0] == matrix[4] && matrix[1] == matrix[3]) {
1454  /* Horizontal flip */
1455  *yaw = 180;
1456  } else {
1457 ignore:
1458  av_log(logctx, AV_LOG_INFO, "Ignoring display matrix indicating "
1459  "non-orthogonal transformation.\n");
1460  return;
1461  }
1462  *roll = 180 / M_PI * atan2(matrix[3], matrix[4]);
1463 
1464  /* We do not write a ProjectionType element indicating "rectangular",
1465  * because this is the default value. */
1466 }
1467 
1468 static int mkv_handle_spherical(void *logctx, EbmlWriter *writer,
1469  const AVStream *st, uint8_t private[],
1470  double *yaw, double *pitch, double *roll)
1471 {
1475  const AVSphericalMapping *spherical;
1476 
1477  if (!sd)
1478  return 0;
1479 
1480  spherical = (const AVSphericalMapping *)sd->data;
1481  if (spherical->projection != AV_SPHERICAL_EQUIRECTANGULAR &&
1483  spherical->projection != AV_SPHERICAL_CUBEMAP) {
1484  av_log(logctx, AV_LOG_WARNING, "Unknown projection type\n");
1485  return 0;
1486  }
1487 
1488  switch (spherical->projection) {
1493  AV_WB32(private, 0); // version + flags
1494  if (spherical->projection == AV_SPHERICAL_EQUIRECTANGULAR) {
1495  AV_WB32(private + 4, 0);
1496  AV_WB32(private + 8, 0);
1497  AV_WB32(private + 12, 0);
1498  AV_WB32(private + 16, 0);
1499  } else {
1500  AV_WB32(private + 4, spherical->bound_top);
1501  AV_WB32(private + 8, spherical->bound_bottom);
1502  AV_WB32(private + 12, spherical->bound_left);
1503  AV_WB32(private + 16, spherical->bound_right);
1504  }
1506  private, 20);
1507  break;
1508  case AV_SPHERICAL_CUBEMAP:
1511  AV_WB32(private, 0); // version + flags
1512  AV_WB32(private + 4, 0); // layout
1513  AV_WB32(private + 8, spherical->padding);
1515  private, 12);
1516  break;
1517  default:
1518  av_assert0(0);
1519  }
1520 
1521  *yaw = (double) spherical->yaw / (1 << 16);
1522  *pitch = (double) spherical->pitch / (1 << 16);
1523  *roll = (double) spherical->roll / (1 << 16);
1524 
1525  return 1; /* Projection included */
1526 }
1527 
1528 static void mkv_write_video_projection(void *logctx, EbmlWriter *wr,
1529  const AVStream *st, uint8_t private[])
1530 {
1531  double yaw = 0, pitch = 0, roll = 0;
1532  int ret;
1533 
1535 
1536  ret = mkv_handle_spherical(logctx, wr, st, private, &yaw, &pitch, &roll);
1537  if (!ret)
1538  mkv_handle_rotation(logctx, st, &yaw, &roll);
1539 
1540  if (yaw)
1542  if (pitch)
1544  if (roll)
1546 
1548 }
1549 
1550 #define MAX_FIELD_ORDER_ELEMS 2
1551 static void mkv_write_field_order(EbmlWriter *writer, int is_webm,
1552  enum AVFieldOrder field_order)
1553 {
1554  switch (field_order) {
1555  case AV_FIELD_UNKNOWN:
1556  break;
1557  case AV_FIELD_PROGRESSIVE:
1560  break;
1561  case AV_FIELD_TT:
1562  case AV_FIELD_BB:
1563  case AV_FIELD_TB:
1564  case AV_FIELD_BT:
1567  if (!is_webm) {
1568  switch (field_order) {
1569  case AV_FIELD_TT:
1572  break;
1573  case AV_FIELD_BB:
1576  break;
1577  case AV_FIELD_TB:
1580  break;
1581  case AV_FIELD_BT:
1584  break;
1585  }
1586  }
1587  }
1588 }
1589 
1590 #define MAX_STEREO_MODE_ELEMS 1
1592  const AVStream *st, int is_webm,
1593  int *h_width, int *h_height)
1594 {
1595  const char *error_message_addendum = "";
1596  const AVDictionaryEntry *tag;
1598 
1599  /* The following macros create bitfields where the ith bit
1600  * indicates whether the MatroskaVideoStereoModeType with that value
1601  * uses double width/height or is WebM compatible. */
1602 #define FLAG(STEREOMODETYPE, BOOL) | (BOOL) << (STEREOMODETYPE)
1603 #define WDIV1(STEREOMODETYPE, STEREO3DTYPE, FLAGS, WDIV, HDIV, WEBM) \
1604  FLAG(STEREOMODETYPE, WDIV)
1605 #define WDIV2(STEREOMODETYPE, WDIV, HDIV, WEBM) \
1606  FLAG(STEREOMODETYPE, WDIV)
1607  // The zero in the following line consumes the first '|'.
1608  const unsigned width_bitfield = 0 STEREOMODE_STEREO3D_MAPPING(WDIV1, WDIV2);
1609 
1610 #define HDIV1(STEREOMODETYPE, STEREO3DTYPE, FLAGS, WDIV, HDIV, WEBM) \
1611  FLAG(STEREOMODETYPE, HDIV)
1612 #define HDIV2(STEREOMODETYPE, WDIV, HDIV, WEBM) \
1613  FLAG(STEREOMODETYPE, HDIV)
1614  const unsigned height_bitfield = 0 STEREOMODE_STEREO3D_MAPPING(HDIV1, HDIV2);
1615 
1616 #define WEBM1(STEREOMODETYPE, STEREO3DTYPE, FLAGS, WDIV, HDIV, WEBM) \
1617  FLAG(STEREOMODETYPE, WEBM)
1618 #define WEBM2(STEREOMODETYPE, WDIV, HDIV, WEBM) \
1619  FLAG(STEREOMODETYPE, WEBM)
1620  const unsigned webm_bitfield = 0 STEREOMODE_STEREO3D_MAPPING(WEBM1, WEBM2);
1621 
1622  *h_width = 1;
1623  *h_height = 1;
1624 
1625  if ((tag = av_dict_get(st->metadata, "stereo_mode", NULL, 0)) ||
1626  (tag = av_dict_get( s->metadata, "stereo_mode", NULL, 0))) {
1627 
1628  for (int i = 0; i < MATROSKA_VIDEO_STEREOMODE_TYPE_NB; i++)
1629  if (!strcmp(tag->value, ff_matroska_video_stereo_mode[i])){
1630  format = i;
1631  break;
1632  }
1634  long stereo_mode = strtol(tag->value, NULL, 0);
1635  if ((unsigned long)stereo_mode >= MATROSKA_VIDEO_STEREOMODE_TYPE_NB)
1636  goto fail;
1637  format = stereo_mode;
1638  }
1639  } else {
1640  const AVPacketSideData *sd;
1641  const AVStereo3D *stereo;
1642  /* The following macro presumes all MATROSKA_VIDEO_STEREOMODE_TYPE_*
1643  * values to be in the range 0..254. */
1644 #define STEREOMODE(STEREOMODETYPE, STEREO3DTYPE, FLAGS, WDIV, HDIV, WEBM) \
1645  [(STEREO3DTYPE)][!!((FLAGS) & AV_STEREO3D_FLAG_INVERT)] = (STEREOMODETYPE) + 1,
1646 #define NOTHING(STEREOMODETYPE, WDIV, HDIV, WEBM)
1647  static const unsigned char conversion_table[][2] = {
1649  };
1650  int fmt;
1651 
1654  if (!sd)
1655  return 0;
1656 
1657  stereo = (const AVStereo3D*)sd->data;
1658 
1659  /* A garbage AVStereo3D or something with no Matroska analogon. */
1660  if ((unsigned)stereo->type >= FF_ARRAY_ELEMS(conversion_table))
1661  return 0;
1662 
1663  fmt = conversion_table[stereo->type][!!(stereo->flags & AV_STEREO3D_FLAG_INVERT)];
1664  /* Format with no Matroska analogon; ignore it */
1665  if (!fmt)
1666  return 0;
1667  format = fmt - 1;
1668  }
1669 
1670  // if webm, do not write unsupported modes
1671  if (is_webm && !(webm_bitfield >> format)) {
1672  error_message_addendum = " for WebM";
1673  goto fail;
1674  }
1675 
1676  *h_width = 1 << ((width_bitfield >> format) & 1);
1677  *h_height = 1 << ((height_bitfield >> format) & 1);
1678 
1679  // write StereoMode if format is valid
1681 
1682  return 0;
1683 fail:
1685  "The specified stereo mode is not valid%s.\n",
1686  error_message_addendum);
1687  return AVERROR(EINVAL);
1688 }
1689 
1691  const AVCodecParameters *par, AVIOContext *pb,
1692  mkv_track *track, const AVStream *st)
1693 {
1694 #if CONFIG_MATROSKA_MUXER
1696  const AVPacketSideData *sd;
1697 
1698  if (IS_SEEKABLE(s->pb, mkv)) {
1700  // We can't know at this point if there will be a block with BlockAdditions, so
1701  // we either write the default value here, or a void element. Either of them will
1702  // be overwritten when finishing the track.
1704  if (par->codec_type == AVMEDIA_TYPE_VIDEO) {
1705  // Similarly, reserve space for an eventual
1706  // HDR10+ ITU T.35 metadata BlockAdditionMapping.
1707  put_ebml_void(pb, 3 /* BlockAdditionMapping */
1708  + 4 /* BlockAddIDValue */
1709  + 4 /* BlockAddIDType */);
1710  }
1711  }
1712 
1715 
1716  if (!sd)
1717  return;
1718 
1719  dovi = (const AVDOVIDecoderConfigurationRecord *)sd->data;
1720  if (dovi->dv_profile <= 10) {
1721  ebml_master mapping;
1722  uint8_t buf[ISOM_DVCC_DVVC_SIZE];
1723  uint32_t type;
1724 
1725  uint64_t expected_size = (2 + 1 + (sizeof(DVCC_DVVC_BLOCK_TYPE_NAME) - 1))
1726  + (2 + 1 + 4) + (2 + 1 + ISOM_DVCC_DVVC_SIZE);
1727 
1728  if (dovi->dv_profile > 7) {
1730  } else {
1732  }
1733 
1734  ff_isom_put_dvcc_dvvc(s, buf, dovi);
1735 
1736  mapping = start_ebml_master(pb, MATROSKA_ID_TRACKBLKADDMAPPING, expected_size);
1737 
1740  put_ebml_binary(pb, MATROSKA_ID_BLKADDIDEXTRADATA, buf, sizeof(buf));
1741 
1742  end_ebml_master(pb, mapping);
1743  }
1744 #endif
1745 }
1746 
1748  const AVStream *st, const AVCodecParameters *par,
1749  AVIOContext *pb)
1750 {
1751  const AVDictionaryEntry *tag;
1752  int display_width_div = 1, display_height_div = 1;
1753  uint8_t color_space[4], projection_private[20];
1756  int ret;
1757 
1759 
1762 
1763  mkv_write_field_order(&writer, IS_WEBM(mkv), par->field_order);
1764 
1765  // check both side data and metadata for stereo information,
1766  // write the result to the bitstream if any is found
1767  ret = mkv_write_stereo_mode(s, &writer, st, IS_WEBM(mkv),
1768  &display_width_div,
1769  &display_height_div);
1770  if (ret < 0)
1771  return ret;
1772 
1773  if (par->format == AV_PIX_FMT_YUVA420P ||
1774  ((tag = av_dict_get(st->metadata, "alpha_mode", NULL, 0)) ||
1775  (tag = av_dict_get( s->metadata, "alpha_mode", NULL, 0))) && strtol(tag->value, NULL, 0))
1777 
1778  // write DisplayWidth and DisplayHeight, they contain the size of
1779  // a single source view and/or the display aspect ratio
1780  if (st->sample_aspect_ratio.num) {
1781  int64_t d_width = av_rescale(par->width, st->sample_aspect_ratio.num, st->sample_aspect_ratio.den);
1782  if (d_width > INT_MAX) {
1783  av_log(s, AV_LOG_ERROR, "Overflow in display width\n");
1784  return AVERROR(EINVAL);
1785  }
1786  if (d_width != par->width || display_width_div != 1 || display_height_div != 1) {
1787  if (IS_WEBM(mkv) || display_width_div != 1 || display_height_div != 1) {
1789  d_width / display_width_div);
1791  par->height / display_height_div);
1792  } else {
1793  AVRational display_aspect_ratio;
1794  av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1795  par->width * (int64_t)st->sample_aspect_ratio.num,
1796  par->height * (int64_t)st->sample_aspect_ratio.den,
1797  1024 * 1024);
1799  display_aspect_ratio.num);
1801  display_aspect_ratio.den);
1804  }
1805  }
1806  } else if (display_width_div != 1 || display_height_div != 1) {
1808  par->width / display_width_div);
1810  par->height / display_height_div);
1811  } else if (!IS_WEBM(mkv))
1814 
1815  if (par->codec_id == AV_CODEC_ID_RAWVIDEO) {
1816  AV_WL32(color_space, par->codec_tag);
1818  color_space, sizeof(color_space));
1819  }
1820  mkv_write_video_color(&writer, st, par);
1821  mkv_write_video_projection(s, &writer, st, projection_private);
1822 
1823  return ebml_writer_write(&writer, pb);
1824 }
1825 
1828 {
1830  1000000000LL * duration.num / duration.den);
1831  track->default_duration_low = 1000LL * duration.num / duration.den;
1833  !!(1000LL * duration.num % duration.den);
1834 }
1835 
1837  AVStream *st, mkv_track *track, AVIOContext *pb,
1838  int is_default)
1839 {
1840  AVCodecParameters *par = st->codecpar;
1841  ebml_master subinfo, track_master;
1842  int native_id = 0;
1843  int qt_id = 0;
1844  int bit_depth;
1845  int sample_rate = par->sample_rate;
1846  int output_sample_rate = 0;
1847  int j, ret;
1848  const AVDictionaryEntry *tag;
1849 
1850  if (par->codec_type == AVMEDIA_TYPE_ATTACHMENT)
1851  return 0;
1852 
1853  track_master = start_ebml_master(pb, MATROSKA_ID_TRACKENTRY, 0);
1855  put_ebml_uid (pb, MATROSKA_ID_TRACKUID, track->uid);
1856  put_ebml_uint(pb, MATROSKA_ID_TRACKFLAGLACING, 0); // no lacing (yet)
1857 
1858  if ((tag = av_dict_get(st->metadata, "title", NULL, 0)))
1860  tag = av_dict_get(st->metadata, "language", NULL, 0);
1862  tag && tag->value[0] ? tag->value : "und");
1863 
1864  // The default value for TRACKFLAGDEFAULT is 1, so add element
1865  // if we need to clear it.
1866  if (!is_default)
1868 
1871 
1872  if (IS_WEBM(mkv)) {
1873  const char *codec_id;
1874  if (par->codec_id != AV_CODEC_ID_WEBVTT) {
1875  for (j = 0; ff_webm_codec_tags[j].id != AV_CODEC_ID_NONE; j++) {
1876  if (ff_webm_codec_tags[j].id == par->codec_id) {
1878  native_id = 1;
1879  break;
1880  }
1881  }
1882  } else {
1884  codec_id = "D_WEBVTT/CAPTIONS";
1885  native_id = MATROSKA_TRACK_TYPE_SUBTITLE;
1886  } else if (st->disposition & AV_DISPOSITION_DESCRIPTIONS) {
1887  codec_id = "D_WEBVTT/DESCRIPTIONS";
1888  native_id = MATROSKA_TRACK_TYPE_METADATA;
1889  } else if (st->disposition & AV_DISPOSITION_METADATA) {
1890  codec_id = "D_WEBVTT/METADATA";
1891  native_id = MATROSKA_TRACK_TYPE_METADATA;
1892  } else {
1893  codec_id = "D_WEBVTT/SUBTITLES";
1894  native_id = MATROSKA_TRACK_TYPE_SUBTITLE;
1895  }
1896  }
1897 
1898  if (!native_id) {
1900  "Only VP8 or VP9 or AV1 video and Vorbis or Opus audio and WebVTT subtitles are supported for WebM.\n");
1901  return AVERROR(EINVAL);
1902  }
1903 
1905  } else {
1917 
1918  // look for a codec ID string specific to mkv to use,
1919  // if none are found, use AVI codes
1920  if (par->codec_id == AV_CODEC_ID_FFV1) {
1921  /* FFV1 is actually supported natively in Matroska,
1922  * yet we use the VfW way to mux it for compatibility
1923  * with old demuxers. (FIXME: Are they really important?) */
1924  } else if (par->codec_id != AV_CODEC_ID_RAWVIDEO || par->codec_tag) {
1925  for (j = 0; ff_mkv_codec_tags[j].id != AV_CODEC_ID_NONE; j++) {
1926  if (ff_mkv_codec_tags[j].id == par->codec_id) {
1928  native_id = 1;
1929  break;
1930  }
1931  }
1932  } else {
1933  if (mkv->allow_raw_vfw) {
1934  native_id = 0;
1935  } else {
1936  av_log(s, AV_LOG_ERROR, "Raw RGB is not supported Natively in Matroska, you can use AVI or NUT or\n"
1937  "If you would like to store it anyway using VFW mode, enable allow_raw_vfw (-allow_raw_vfw 1)\n");
1938  return AVERROR(EINVAL);
1939  }
1940  }
1941  }
1942 
1943  switch (par->codec_type) {
1944  AVRational frame_rate;
1945  int audio_frame_samples;
1946 
1947  case AVMEDIA_TYPE_VIDEO:
1948  mkv->have_video = 1;
1950 
1951  frame_rate = (AVRational){ 0, 1 };
1952  if (st->avg_frame_rate.num > 0 && st->avg_frame_rate.den > 0)
1953  frame_rate = st->avg_frame_rate;
1954  else if (st->r_frame_rate.num > 0 && st->r_frame_rate.den > 0)
1955  frame_rate = st->r_frame_rate;
1956 
1957  if (frame_rate.num > 0)
1958  mkv_write_default_duration(track, pb, av_inv_q(frame_rate));
1959 
1960  if (CONFIG_MATROSKA_MUXER && !native_id &&
1963  par->codec_id == AV_CODEC_ID_SVQ1 ||
1964  par->codec_id == AV_CODEC_ID_SVQ3 ||
1965  par->codec_id == AV_CODEC_ID_CINEPAK))
1966  qt_id = 1;
1967 
1968  if (qt_id)
1969  put_ebml_string(pb, MATROSKA_ID_CODECID, "V_QUICKTIME");
1970  else if (!native_id) {
1971  // if there is no mkv-specific codec ID, use VFW mode
1972  put_ebml_string(pb, MATROSKA_ID_CODECID, "V_MS/VFW/FOURCC");
1973  track->write_dts = 1;
1975  }
1976 
1977  ret = mkv_write_track_video(s, mkv, st, par, pb);
1978  if (ret < 0)
1979  return ret;
1980 
1981  break;
1982 
1983  case AVMEDIA_TYPE_AUDIO:
1984  if (par->initial_padding) {
1985  int64_t codecdelay = av_rescale_q(par->initial_padding,
1986  (AVRational){ 1, par->sample_rate },
1987  (AVRational){ 1, 1000000000 });
1988  if (codecdelay < 0) {
1989  av_log(s, AV_LOG_ERROR, "Initial padding is invalid\n");
1990  return AVERROR(EINVAL);
1991  }
1992  put_ebml_uint(pb, MATROSKA_ID_CODECDELAY, codecdelay);
1993 
1994  track->ts_offset = av_rescale_q(par->initial_padding,
1995  (AVRational){ 1, par->sample_rate },
1996  st->time_base);
1997  ffstream(st)->lowest_ts_allowed = -track->ts_offset;
1998  }
1999  if (par->codec_id == AV_CODEC_ID_OPUS)
2001 #if CONFIG_MATROSKA_MUXER
2002  else if (par->codec_id == AV_CODEC_ID_AAC) {
2003  ret = get_aac_sample_rates(s, mkv, par->extradata, par->extradata_size,
2004  &sample_rate, &output_sample_rate);
2005  if (ret < 0)
2006  return ret;
2007  }
2008 #endif
2009 
2011 
2012  audio_frame_samples = av_get_audio_frame_duration2(par, 0);
2013  if (audio_frame_samples)
2014  mkv_write_default_duration(track, pb, (AVRational){ audio_frame_samples,
2015  par->sample_rate });
2016  if (!native_id)
2017  // no mkv-specific ID, use ACM mode
2018  put_ebml_string(pb, MATROSKA_ID_CODECID, "A_MS/ACM");
2019 
2020  subinfo = start_ebml_master(pb, MATROSKA_ID_TRACKAUDIO, 6 + 4 * 9);
2022 
2023  track->sample_rate_offset = avio_tell(pb);
2025  if (output_sample_rate)
2026  put_ebml_float(pb, MATROSKA_ID_AUDIOOUTSAMPLINGFREQ, output_sample_rate);
2027 
2029  if (!bit_depth && par->codec_id != AV_CODEC_ID_ADPCM_G726) {
2030  if (par->bits_per_raw_sample)
2032  else
2034  }
2035  if (!bit_depth)
2037  if (bit_depth)
2039  end_ebml_master(pb, subinfo);
2040  break;
2041 
2042  case AVMEDIA_TYPE_SUBTITLE:
2043  if (!native_id) {
2044  av_log(s, AV_LOG_ERROR, "Subtitle codec %d is not supported.\n", par->codec_id);
2045  return AVERROR(ENOSYS);
2046  }
2047  if (!IS_WEBM(mkv) && st->disposition & AV_DISPOSITION_DESCRIPTIONS)
2049 
2050  if (!IS_WEBM(mkv) || par->codec_id != AV_CODEC_ID_WEBVTT)
2051  native_id = MATROSKA_TRACK_TYPE_SUBTITLE;
2052 
2053  put_ebml_uint(pb, MATROSKA_ID_TRACKTYPE, native_id);
2054  break;
2055  default:
2056  av_log(s, AV_LOG_ERROR, "Only audio, video, and subtitles are supported for Matroska.\n");
2057  return AVERROR(EINVAL);
2058  }
2059 
2060  if (!IS_WEBM(mkv))
2061  mkv_write_blockadditionmapping(s, mkv, par, pb, track, st);
2062 
2063  if (!IS_WEBM(mkv) || par->codec_id != AV_CODEC_ID_WEBVTT) {
2064  uint8_t *codecpriv;
2065  int codecpriv_size, max_payload_size;
2066  track->codecpriv_offset = avio_tell(pb);
2067  ret = mkv_assemble_codecprivate(s, mkv->tmp_bc, par,
2068  par->extradata, par->extradata_size,
2069  native_id, qt_id,
2070  &codecpriv, &codecpriv_size, &max_payload_size);
2071  if (ret < 0)
2072  goto fail;
2073  mkv_put_codecprivate(pb, max_payload_size, codecpriv, codecpriv_size);
2074  track->codecpriv_size = max_payload_size;
2075  }
2076 
2077  end_ebml_master(pb, track_master);
2078  ret = 0;
2079 fail:
2080  ffio_reset_dyn_buf(mkv->tmp_bc);
2081 
2082  return ret;
2083 }
2084 
2086 {
2087  MatroskaMuxContext *mkv = s->priv_data;
2088  AVIOContext *pb = s->pb;
2089  int video_default_idx = -1, audio_default_idx = -1, subtitle_default_idx = -1;
2090  int i, ret;
2091 
2092  if (mkv->nb_attachments == s->nb_streams)
2093  return 0;
2094 
2095  ret = start_ebml_master_crc32(&mkv->track.bc, mkv);
2096  if (ret < 0)
2097  return ret;
2098 
2099  if (mkv->default_mode != DEFAULT_MODE_PASSTHROUGH) {
2100  int video_idx = -1, audio_idx = -1, subtitle_idx = -1;
2101 
2102  for (i = s->nb_streams - 1; i >= 0; i--) {
2103  AVStream *st = s->streams[i];
2104 
2105  switch (st->codecpar->codec_type) {
2106 #define CASE(type, variable) \
2107  case AVMEDIA_TYPE_ ## type: \
2108  variable ## _idx = i; \
2109  if (st->disposition & AV_DISPOSITION_DEFAULT) \
2110  variable ## _default_idx = i; \
2111  break;
2112  CASE(VIDEO, video)
2113  CASE(AUDIO, audio)
2114  CASE(SUBTITLE, subtitle)
2115 #undef CASE
2116  }
2117  }
2118 
2119  video_default_idx = FFMAX(video_default_idx, video_idx);
2120  audio_default_idx = FFMAX(audio_default_idx, audio_idx);
2122  subtitle_default_idx = FFMAX(subtitle_default_idx, subtitle_idx);
2123  }
2124  for (i = 0; i < s->nb_streams; i++) {
2125  AVStream *st = s->streams[i];
2126  int is_default = st->disposition & AV_DISPOSITION_DEFAULT ||
2127  i == video_default_idx || i == audio_default_idx ||
2128  i == subtitle_default_idx;
2129  ret = mkv_write_track(s, mkv, st, &mkv->tracks[i],
2130  mkv->track.bc, is_default);
2131  if (ret < 0)
2132  return ret;
2133  }
2134 
2135  return end_ebml_master_crc32_tentatively(pb, &mkv->track, mkv,
2137 }
2138 
2140 {
2141  EBML_WRITER(4);
2142  uint8_t *key = av_strdup(t->key);
2143  uint8_t *p = key;
2144  const uint8_t *lang = NULL;
2145  int ret;
2146 
2147  if (!key)
2148  return AVERROR(ENOMEM);
2149 
2150  if ((p = strrchr(p, '-')) &&
2151  (lang = ff_convert_lang_to(p + 1, AV_LANG_ISO639_2_BIBL)))
2152  *p = 0;
2153 
2154  p = key;
2155  while (*p) {
2156  if (*p == ' ')
2157  *p = '_';
2158  else if (*p >= 'a' && *p <= 'z')
2159  *p -= 'a' - 'A';
2160  p++;
2161  }
2162 
2165  if (lang)
2168  ret = ebml_writer_write(&writer, pb);
2169 
2170  av_freep(&key);
2171  return ret;
2172 }
2173 
2175  uint32_t elementid, uint64_t uid)
2176 {
2178  4 + 1 + 8);
2179  if (elementid)
2180  put_ebml_uid(pb, elementid, uid);
2181  end_ebml_master(pb, targets);
2182 }
2183 
2184 static int mkv_check_tag_name(const char *name, uint32_t elementid)
2185 {
2186  return av_strcasecmp(name, "title") &&
2187  av_strcasecmp(name, "stereo_mode") &&
2188  av_strcasecmp(name, "creation_time") &&
2189  av_strcasecmp(name, "encoding_tool") &&
2190  av_strcasecmp(name, "duration") &&
2191  (elementid != MATROSKA_ID_TAGTARGETS_TRACKUID ||
2192  av_strcasecmp(name, "language")) &&
2193  (elementid != MATROSKA_ID_TAGTARGETS_ATTACHUID ||
2194  (av_strcasecmp(name, "filename") &&
2195  av_strcasecmp(name, "mimetype")));
2196 }
2197 
2199  AVIOContext **pb, unsigned reserved_size,
2200  uint32_t elementid, uint64_t uid)
2201 {
2202  const AVDictionaryEntry *t = NULL;
2203  AVIOContext *const tmp_bc = mkv->tmp_bc;
2204  uint8_t *buf;
2205  int ret = 0, size, tag_written = 0;
2206 
2207  mkv_write_tag_targets(mkv, tmp_bc, elementid, uid);
2208 
2209  while ((t = av_dict_iterate(m, t))) {
2210  if (mkv_check_tag_name(t->key, elementid)) {
2211  ret = mkv_write_simpletag(tmp_bc, t);
2212  if (ret < 0)
2213  goto end;
2214  tag_written = 1;
2215  }
2216  }
2217  if (reserved_size)
2218  put_ebml_void(tmp_bc, reserved_size);
2219  else if (!tag_written)
2220  goto end;
2221 
2222  size = avio_get_dyn_buf(tmp_bc, &buf);
2223  if (tmp_bc->error) {
2224  ret = tmp_bc->error;
2225  goto end;
2226  }
2227  if (!*pb) {
2228  ret = start_ebml_master_crc32(pb, mkv);
2229  if (ret < 0)
2230  goto end;
2231  }
2232  put_ebml_binary(*pb, MATROSKA_ID_TAG, buf, size);
2233 
2234 end:
2235  ffio_reset_dyn_buf(tmp_bc);
2236  return ret;
2237 }
2238 
2240 {
2241  MatroskaMuxContext *mkv = s->priv_data;
2242  int i, ret, seekable = IS_SEEKABLE(s->pb, mkv);
2243 
2244  mkv->wrote_tags = 1;
2245 
2247 
2248  ret = mkv_write_tag(mkv, s->metadata, &mkv->tags.bc, 0, 0, 0);
2249  if (ret < 0)
2250  return ret;
2251 
2252  for (i = 0; i < s->nb_streams; i++) {
2253  const AVStream *st = s->streams[i];
2254  mkv_track *track = &mkv->tracks[i];
2255 
2257  continue;
2258 
2259  ret = mkv_write_tag(mkv, st->metadata, &mkv->tags.bc,
2260  seekable ? DURATION_SIMPLETAG_SIZE : 0,
2262  if (ret < 0)
2263  return ret;
2264  if (seekable)
2266  }
2267 
2268  if (mkv->nb_attachments && !IS_WEBM(mkv)) {
2269  for (i = 0; i < s->nb_streams; i++) {
2270  const mkv_track *track = &mkv->tracks[i];
2271  const AVStream *st = s->streams[i];
2272 
2274  continue;
2275 
2276  ret = mkv_write_tag(mkv, st->metadata, &mkv->tags.bc, 0,
2278  if (ret < 0)
2279  return ret;
2280  }
2281  }
2282 
2283  if (mkv->tags.bc) {
2284  return end_ebml_master_crc32_tentatively(s->pb, &mkv->tags, mkv,
2286  }
2287  return 0;
2288 }
2289 
2291 {
2292  for (unsigned i = 0; i < s->nb_chapters; i++) {
2293  if (!s->chapters[i]->id)
2294  return 1;
2295  for (unsigned j = 0; j < i; j++)
2296  if (s->chapters[j]->id == s->chapters[i]->id)
2297  return 1;
2298  }
2299  return 0;
2300 }
2301 
2303 {
2304  MatroskaMuxContext *mkv = s->priv_data;
2305  AVIOContext *dyn_cp = NULL, *dyn_tags = NULL, **tags, *pb = s->pb;
2306  ebml_master editionentry;
2307  AVRational scale = {1, 1E9};
2308  int ret, create_new_ids;
2309 
2310  if (!s->nb_chapters || mkv->wrote_chapters)
2311  return 0;
2312 
2313  ret = start_ebml_master_crc32(&dyn_cp, mkv);
2314  if (ret < 0)
2315  return ret;
2316 
2317  editionentry = start_ebml_master(dyn_cp, MATROSKA_ID_EDITIONENTRY, 0);
2318  if (!IS_WEBM(mkv)) {
2320  /* If mkv_write_tags() has already been called, then any tags
2321  * corresponding to chapters will be put into a new Tags element. */
2322  tags = mkv->wrote_tags ? &dyn_tags : &mkv->tags.bc;
2323  } else
2324  tags = NULL;
2325 
2326  create_new_ids = mkv_new_chapter_ids_needed(s);
2327 
2328  for (unsigned i = 0; i < s->nb_chapters; i++) {
2329  AVChapter *const c = s->chapters[i];
2330  int64_t chapterstart = av_rescale_q(c->start, c->time_base, scale);
2331  int64_t chapterend = av_rescale_q(c->end, c->time_base, scale);
2332  const AVDictionaryEntry *t;
2333  uint64_t uid = create_new_ids ? i + 1ULL : c->id;
2334  EBML_WRITER(7);
2335 
2336  if (chapterstart < 0 || chapterstart > chapterend || chapterend < 0) {
2338  "Invalid chapter start (%"PRId64") or end (%"PRId64").\n",
2339  chapterstart, chapterend);
2341  goto fail;
2342  }
2343 
2346  ebml_writer_add_uint(&writer, MATROSKA_ID_CHAPTERTIMESTART, chapterstart);
2347  ebml_writer_add_uint(&writer, MATROSKA_ID_CHAPTERTIMEEND, chapterend);
2348  if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
2351  ebml_writer_add_string(&writer, MATROSKA_ID_CHAPLANG , "und");
2352  }
2353  ret = ebml_writer_write(&writer, dyn_cp);
2354  if (ret < 0)
2355  goto fail;
2356 
2357  if (tags) {
2359 
2360  ret = mkv_write_tag(mkv, c->metadata, tags, 0,
2362  if (ret < 0)
2363  goto fail;
2364  }
2365  }
2366  end_ebml_master(dyn_cp, editionentry);
2367  mkv->wrote_chapters = 1;
2368 
2369  ret = end_ebml_master_crc32(pb, &dyn_cp, mkv, MATROSKA_ID_CHAPTERS, 0, 0, 1);
2370  if (ret < 0)
2371  goto fail;
2372  if (dyn_tags)
2373  return end_ebml_master_crc32(pb, &dyn_tags, mkv,
2374  MATROSKA_ID_TAGS, 0, 0, 1);
2375  return 0;
2376 
2377 fail:
2378  if (tags) {
2379  /* tags == &mkv->tags.bc can only happen if mkv->tags.bc was
2380  * initially NULL, so we never free older tags. */
2381  ffio_free_dyn_buf(tags);
2382  }
2383  ffio_free_dyn_buf(&dyn_cp);
2384  return ret;
2385 }
2386 
2387 static const char *get_mimetype(const AVStream *st)
2388 {
2389  const AVDictionaryEntry *t;
2390 
2391  if (t = av_dict_get(st->metadata, "mimetype", NULL, 0))
2392  return t->value;
2393  if (st->codecpar->codec_id != AV_CODEC_ID_NONE) {
2395  if (desc && desc->mime_types) {
2396  return desc->mime_types[0];
2397  } else if (st->codecpar->codec_id == AV_CODEC_ID_TEXT)
2398  return "text/plain";
2399  }
2400 
2401  return NULL;
2402 }
2403 
2405 {
2406  MatroskaMuxContext *mkv = s->priv_data;
2407  AVIOContext *dyn_cp = NULL, *pb = s->pb;
2408  int i, ret;
2409 
2410  if (!mkv->nb_attachments)
2411  return 0;
2412 
2413  ret = start_ebml_master_crc32(&dyn_cp, mkv);
2414  if (ret < 0)
2415  return ret;
2416 
2417  for (i = 0; i < s->nb_streams; i++) {
2418  const AVStream *st = s->streams[i];
2419  mkv_track *track = &mkv->tracks[i];
2420  EBML_WRITER(6);
2421  const AVDictionaryEntry *t;
2422  const char *mimetype;
2423 
2425  continue;
2426 
2428 
2429  if (t = av_dict_get(st->metadata, "title", NULL, 0))
2431  if (!(t = av_dict_get(st->metadata, "filename", NULL, 0))) {
2432  av_log(s, AV_LOG_ERROR, "Attachment stream %d has no filename tag.\n", i);
2433  ffio_free_dyn_buf(&dyn_cp);
2434  return AVERROR(EINVAL);
2435  }
2437 
2438  mimetype = get_mimetype(st);
2439  av_assert0(mimetype);
2443  ebml_writer_add_uid(&writer, MATROSKA_ID_FILEUID, track->uid);
2444  ret = ebml_writer_write(&writer, dyn_cp);
2445  if (ret < 0) {
2446  ffio_free_dyn_buf(&dyn_cp);
2447  return ret;
2448  }
2449  }
2450  return end_ebml_master_crc32(pb, &dyn_cp, mkv,
2451  MATROSKA_ID_ATTACHMENTS, 0, 0, 1);
2452 }
2453 
2455 {
2456  const AVDictionaryEntry *duration = av_dict_get(s->metadata, "DURATION",
2457  NULL, 0);
2458  int64_t max = 0;
2459  int64_t us;
2460 
2461  if (duration && (av_parse_time(&us, duration->value, 1) == 0) && us > 0) {
2462  av_log(s, AV_LOG_DEBUG, "get_metadata_duration found duration in context metadata: %" PRId64 "\n", us);
2463  return us;
2464  }
2465 
2466  for (unsigned i = 0; i < s->nb_streams; i++) {
2467  int64_t us;
2468  duration = av_dict_get(s->streams[i]->metadata, "DURATION", NULL, 0);
2469 
2470  if (duration && (av_parse_time(&us, duration->value, 1) == 0))
2471  max = FFMAX(max, us);
2472  }
2473 
2474  av_log(s, AV_LOG_DEBUG, "get_metadata_duration returned: %" PRId64 "\n", max);
2475  return max;
2476 }
2477 
2479  const char *doctype, int version)
2480 {
2481  EBML_WRITER(8);
2487  ebml_writer_add_string(&writer, EBML_ID_DOCTYPE, doctype);
2490  /* The size is bounded, so no need to check this. */
2491  ebml_writer_write(&writer, pb);
2492 }
2493 
2495 {
2496  MatroskaMuxContext *mkv = s->priv_data;
2497  const AVDictionaryEntry *tag;
2498  int64_t creation_time;
2499  AVIOContext *pb;
2500  int ret = start_ebml_master_crc32(&mkv->info.bc, mkv);
2501  if (ret < 0)
2502  return ret;
2503  pb = mkv->info.bc;
2504 
2506  if ((tag = av_dict_get(s->metadata, "title", NULL, 0)))
2507  put_ebml_string(pb, MATROSKA_ID_TITLE, tag->value);
2508  if (!(s->flags & AVFMT_FLAG_BITEXACT)) {
2510  if ((tag = av_dict_get(s->metadata, "encoding_tool", NULL, 0)))
2512  else
2514 
2515  if (!IS_WEBM(mkv))
2517  } else {
2518  const char *ident = "Lavf";
2521  }
2522 
2523  if (ff_parse_creation_time_metadata(s, &creation_time, 0) > 0) {
2524  // Adjust time so it's relative to 2001-01-01 and convert to nanoseconds.
2525  int64_t date_utc = (creation_time - 978307200000000LL) * 1000;
2526  uint8_t date_utc_buf[8];
2527  AV_WB64(date_utc_buf, date_utc);
2528  put_ebml_binary(pb, MATROSKA_ID_DATEUTC, date_utc_buf, 8);
2529  }
2530 
2531  // reserve space for the duration
2532  mkv->duration = 0;
2533  mkv->duration_offset = avio_tell(pb);
2534  if (!mkv->is_live) {
2535  int64_t metadata_duration = get_metadata_duration(s);
2536 
2537  if (s->duration > 0) {
2538  int64_t scaledDuration = av_rescale(s->duration, 1000, AV_TIME_BASE);
2539  put_ebml_float(pb, MATROSKA_ID_DURATION, scaledDuration);
2540  av_log(s, AV_LOG_DEBUG, "Write early duration from recording time = %" PRIu64 "\n", scaledDuration);
2541  } else if (metadata_duration > 0) {
2542  int64_t scaledDuration = av_rescale(metadata_duration, 1000, AV_TIME_BASE);
2543  put_ebml_float(pb, MATROSKA_ID_DURATION, scaledDuration);
2544  av_log(s, AV_LOG_DEBUG, "Write early duration from metadata = %" PRIu64 "\n", scaledDuration);
2545  } else if (s->pb->seekable & AVIO_SEEKABLE_NORMAL) {
2546  put_ebml_void(pb, 11); // assumes double-precision float to be written
2547  }
2548  }
2549  return end_ebml_master_crc32_tentatively(s->pb, &mkv->info,
2550  mkv, MATROSKA_ID_INFO);
2551 }
2552 
2554 {
2555  MatroskaMuxContext *mkv = s->priv_data;
2556  AVIOContext *pb = s->pb;
2557  int ret, version = 2;
2558 
2559  ret = avio_open_dyn_buf(&mkv->tmp_bc);
2560  if (ret < 0)
2561  return ret;
2562 
2563  if (!IS_WEBM(mkv) ||
2564  av_dict_get(s->metadata, "stereo_mode", NULL, 0) ||
2565  av_dict_get(s->metadata, "alpha_mode", NULL, 0))
2566  version = 4;
2567 
2568  for (unsigned i = 0; i < s->nb_streams; i++) {
2569  if (s->streams[i]->codecpar->codec_id == AV_CODEC_ID_OPUS ||
2570  av_dict_get(s->streams[i]->metadata, "stereo_mode", NULL, 0) ||
2571  av_dict_get(s->streams[i]->metadata, "alpha_mode", NULL, 0))
2572  version = 4;
2573  }
2574 
2575  ebml_write_header(pb, s->oformat->name, version);
2577  put_ebml_size_unknown(pb, 8);
2578  mkv->segment_offset = avio_tell(pb);
2579 
2580  // We write a SeekHead at the beginning to point to all other level
2581  // one elements (except Clusters).
2582  mkv_start_seekhead(mkv, pb);
2583 
2584  ret = mkv_write_info(s);
2585  if (ret < 0)
2586  return ret;
2587 
2588  ret = mkv_write_tracks(s);
2589  if (ret < 0)
2590  return ret;
2591 
2593  if (ret < 0)
2594  return ret;
2595 
2596  if (!IS_WEBM(mkv)) {
2598  if (ret < 0)
2599  return ret;
2600  }
2601 
2602  /* Must come after mkv_write_chapters() to write chapter tags
2603  * into the same Tags element as the other tags. */
2604  ret = mkv_write_tags(s);
2605  if (ret < 0)
2606  return ret;
2607 
2608  if (!IS_SEEKABLE(pb, mkv)) {
2609  ret = mkv_write_seekhead(pb, mkv, 0, avio_tell(pb));
2610  if (ret < 0)
2611  return ret;
2612  }
2613 
2614  if (s->metadata_header_padding > 0) {
2615  if (s->metadata_header_padding == 1)
2616  s->metadata_header_padding++;
2617  put_ebml_void(pb, s->metadata_header_padding);
2618  }
2619 
2620  if (mkv->reserve_cues_space || mkv->move_cues_to_front) {
2621  if (IS_SEEKABLE(pb, mkv)) {
2622  mkv->cues_pos = avio_tell(pb);
2623  if (mkv->reserve_cues_space >= 1) {
2624  if (mkv->reserve_cues_space == 1)
2625  mkv->reserve_cues_space++;
2627  }
2628  } else
2629  mkv->reserve_cues_space = -1;
2630  }
2631 
2632  mkv->cluster_pos = -1;
2633 
2634  // start a new cluster every 5 MB or 5 sec, or 32k / 1 sec for streaming or
2635  // after 4k and on a keyframe
2636  if (IS_SEEKABLE(pb, mkv)) {
2637  if (mkv->cluster_time_limit < 0)
2638  mkv->cluster_time_limit = 5000;
2639  if (mkv->cluster_size_limit < 0)
2640  mkv->cluster_size_limit = 5 * 1024 * 1024;
2641  } else {
2642  if (mkv->cluster_time_limit < 0)
2643  mkv->cluster_time_limit = 1000;
2644  if (mkv->cluster_size_limit < 0)
2645  mkv->cluster_size_limit = 32 * 1024;
2646  }
2647 
2648  return 0;
2649 }
2650 
2651 #if CONFIG_MATROSKA_MUXER
2652 static int mkv_reformat_h2645(MatroskaMuxContext *mkv, AVIOContext *pb,
2653  const AVPacket *pkt, int *size)
2654 {
2655  int ret;
2656  if (pb) {
2658  } else {
2660  if (ret < 0)
2661  return ret;
2662  *size = ret;
2663  }
2664  return 0;
2665 }
2666 
2667 static int mkv_reformat_wavpack(MatroskaMuxContext *mkv, AVIOContext *pb,
2668  const AVPacket *pkt, int *size)
2669 {
2670  const uint8_t *src = pkt->data;
2671  int srclen = pkt->size;
2672  int offset = 0;
2673  int ret;
2674 
2675  while (srclen >= WV_HEADER_SIZE) {
2676  WvHeader header;
2677 
2679  if (ret < 0)
2680  return ret;
2681  src += WV_HEADER_SIZE;
2682  srclen -= WV_HEADER_SIZE;
2683 
2684  if (srclen < header.blocksize)
2685  return AVERROR_INVALIDDATA;
2686 
2687  offset += 4 * !!header.initial + 8 + 4 * !(header.initial && header.final);
2688  if (pb) {
2689  if (header.initial)
2690  avio_wl32(pb, header.samples);
2691  avio_wl32(pb, header.flags);
2692  avio_wl32(pb, header.crc);
2693 
2694  if (!(header.initial && header.final))
2695  avio_wl32(pb, header.blocksize);
2696 
2697  avio_write(pb, src, header.blocksize);
2698  }
2699  src += header.blocksize;
2700  srclen -= header.blocksize;
2701  offset += header.blocksize;
2702  }
2703  *size = offset;
2704 
2705  return 0;
2706 }
2707 #endif
2708 
2710  const AVPacket *pkt, int *size)
2711 {
2712  int ret = ff_av1_filter_obus(pb, pkt->data, pkt->size);
2713  if (ret < 0)
2714  return ret;
2715  *size = ret;
2716  return 0;
2717 }
2718 
2720  const AVPacket *pkt, int *size)
2721 {
2722  const uint8_t *id, *settings;
2723  size_t id_size, settings_size;
2724  unsigned total = pkt->size + 2U;
2725 
2726  if (total > INT_MAX)
2727  return AVERROR(ERANGE);
2728 
2730  &id_size);
2732  &settings_size);
2733  if (id_size > INT_MAX - total || settings_size > INT_MAX - (total += id_size))
2734  return AVERROR(ERANGE);
2735  *size = total += settings_size;
2736  if (pb) {
2737  avio_write(pb, id, id_size);
2738  avio_w8(pb, '\n');
2739  avio_write(pb, settings, settings_size);
2740  avio_w8(pb, '\n');
2741  avio_write(pb, pkt->data, pkt->size);
2742  }
2743  return 0;
2744 }
2745 
2746 static void mkv_write_blockadditional(EbmlWriter *writer, const uint8_t *buf,
2747  size_t size, uint64_t additional_id)
2748 {
2750  ebml_writer_add_uint(writer, MATROSKA_ID_BLOCKADDID, additional_id);
2752  ebml_writer_close_master(writer);
2753 }
2754 
2755 static int mkv_write_block(void *logctx, MatroskaMuxContext *mkv,
2756  AVIOContext *pb, const AVCodecParameters *par,
2757  mkv_track *track, const AVPacket *pkt,
2758  int keyframe, int64_t ts, uint64_t duration,
2759  int force_blockgroup, int64_t relative_packet_pos)
2760 {
2761  uint8_t t35_buf[6 + AV_HDR_PLUS_MAX_PAYLOAD_SIZE];
2762  uint8_t *side_data;
2763  size_t side_data_size;
2764  uint64_t additional_id;
2765  unsigned track_number = track->track_num;
2766  EBML_WRITER(12);
2767  int ret;
2768 
2769  mkv->cur_block.track = track;
2770  mkv->cur_block.pkt = pkt;
2771  mkv->cur_block.rel_ts = ts - mkv->cluster_pts;
2772  mkv->cur_block.flags = 0;
2773 
2774  /* Open a BlockGroup with a Block now; it will later be converted
2775  * to a SimpleBlock if possible. */
2777  ebml_writer_add_block(&writer, mkv);
2778 
2779  if (duration > 0 && (par->codec_type == AVMEDIA_TYPE_SUBTITLE ||
2780  /* If the packet's duration is inconsistent with the default duration,
2781  * add an explicit duration element. */
2782  track->default_duration_high > 0 &&
2783  duration != track->default_duration_high &&
2784  duration != track->default_duration_low))
2786 
2787  av_log(logctx, AV_LOG_DEBUG,
2788  "Writing block of size %d with pts %" PRId64 ", dts %" PRId64 ", "
2789  "duration %" PRId64 " at relative offset %" PRId64 " in cluster "
2790  "at offset %" PRId64 ". TrackNumber %u, keyframe %d\n",
2791  pkt->size, pkt->pts, pkt->dts, pkt->duration, relative_packet_pos,
2792  mkv->cluster_pos, track_number, keyframe != 0);
2793 
2794  side_data = av_packet_get_side_data(pkt,
2796  &side_data_size);
2797  if (side_data && side_data_size >= 10) {
2798  int64_t discard_padding = AV_RL32(side_data + 4);
2799  if (discard_padding) {
2800  discard_padding = av_rescale_q(discard_padding,
2801  (AVRational){1, par->sample_rate},
2802  (AVRational){1, 1000000000});
2803  ebml_writer_add_sint(&writer, MATROSKA_ID_DISCARDPADDING, discard_padding);
2804  }
2805  }
2806 
2808  side_data = av_packet_get_side_data(pkt,
2810  &side_data_size);
2811  if (side_data && side_data_size >= 8 &&
2812  // Only the Codec-specific BlockMore (id == 1) is currently supported.
2813  (additional_id = AV_RB64(side_data)) == MATROSKA_BLOCK_ADD_ID_TYPE_OPAQUE) {
2814  mkv_write_blockadditional(&writer, side_data + 8, side_data_size - 8,
2815  additional_id);
2816  track->max_blockaddid = FFMAX(track->max_blockaddid, additional_id);
2817  }
2818 
2819  if (par->codec_type == AVMEDIA_TYPE_VIDEO) {
2820  side_data = av_packet_get_side_data(pkt,
2822  &side_data_size);
2823  if (side_data && side_data_size) {
2824  uint8_t *payload = t35_buf;
2825  size_t payload_size = sizeof(t35_buf) - 6;
2826 
2827  bytestream_put_byte(&payload, 0xB5); // country_code
2828  bytestream_put_be16(&payload, 0x3C); // provider_code
2829  bytestream_put_be16(&payload, 0x01); // provider_oriented_code
2830  bytestream_put_byte(&payload, 0x04); // application_identifier
2831 
2832  ret = av_dynamic_hdr_plus_to_t35((AVDynamicHDRPlus *)side_data, &payload,
2833  &payload_size);
2834  if (ret < 0)
2835  return ret;
2836 
2837  mkv_write_blockadditional(&writer, t35_buf, payload_size + 6,
2839  track->max_blockaddid = FFMAX(track->max_blockaddid,
2841  }
2842  }
2843 
2845 
2846  if (!force_blockgroup && writer.nb_elements == 2) {
2847  /* Nothing except the BlockGroup + Block. Can use a SimpleBlock. */
2848  writer.elements++; // Skip the BlockGroup.
2849  writer.nb_elements--;
2850  av_assert2(writer.elements[0].id == MATROSKA_ID_BLOCK);
2851  writer.elements[0].id = MATROSKA_ID_SIMPLEBLOCK;
2852  if (keyframe)
2853  mkv->cur_block.flags |= 1 << 7;
2854  } else if (!keyframe)
2856  track->last_timestamp - ts);
2857 
2858  return ebml_writer_write(&writer, pb);
2859 }
2860 
2862 {
2863  MatroskaMuxContext *mkv = s->priv_data;
2864  int ret;
2865 
2866  if (!mkv->have_video) {
2867  for (unsigned i = 0; i < s->nb_streams; i++)
2868  mkv->tracks[i].has_cue = 0;
2869  }
2870  mkv->cluster_pos = -1;
2871  ret = end_ebml_master_crc32(s->pb, &mkv->cluster_bc, mkv,
2872  MATROSKA_ID_CLUSTER, 0, 1, 0);
2873  if (ret < 0)
2874  return ret;
2875 
2877  return 0;
2878 }
2879 
2881 {
2882  MatroskaMuxContext *mkv = s->priv_data;
2883  mkv_track *track = &mkv->tracks[pkt->stream_index];
2884  AVCodecParameters *par = s->streams[pkt->stream_index]->codecpar;
2885  uint8_t *side_data;
2886  size_t side_data_size;
2887  int ret;
2888 
2890  &side_data_size);
2891 
2892  switch (par->codec_id) {
2893 #if CONFIG_MATROSKA_MUXER
2894  case AV_CODEC_ID_AAC:
2895  if (side_data_size && mkv->track.bc) {
2896  int output_sample_rate = 0;
2897  ret = get_aac_sample_rates(s, mkv, side_data, side_data_size,
2898  &track->sample_rate, &output_sample_rate);
2899  if (ret < 0)
2900  return ret;
2901  if (!output_sample_rate)
2902  output_sample_rate = track->sample_rate; // Space is already reserved, so it's this or a void element.
2903  ret = mkv_update_codecprivate(s, mkv, side_data, side_data_size,
2904  par, mkv->track.bc, track, 0);
2905  if (ret < 0)
2906  return ret;
2907  avio_seek(mkv->track.bc, track->sample_rate_offset, SEEK_SET);
2909  put_ebml_float(mkv->track.bc, MATROSKA_ID_AUDIOOUTSAMPLINGFREQ, output_sample_rate);
2910  } else if (!par->extradata_size && !track->sample_rate) {
2911  // No extradata (codecpar or packet side data).
2912  av_log(s, AV_LOG_ERROR, "Error parsing AAC extradata, unable to determine samplerate.\n");
2913  return AVERROR(EINVAL);
2914  }
2915  break;
2916  case AV_CODEC_ID_FLAC:
2917  if (side_data_size && mkv->track.bc) {
2918  if (side_data_size != par->extradata_size) {
2919  av_log(s, AV_LOG_ERROR, "Invalid FLAC STREAMINFO metadata for output stream %d\n",
2920  pkt->stream_index);
2921  return AVERROR(EINVAL);
2922  }
2923  ret = mkv_update_codecprivate(s, mkv, side_data, side_data_size,
2924  par, mkv->track.bc, track, 0);
2925  if (ret < 0)
2926  return ret;
2927  }
2928  break;
2929 #endif
2930  // FIXME: Remove the following once libaom starts propagating proper extradata during init()
2931  // See https://bugs.chromium.org/p/aomedia/issues/detail?id=2208
2932  case AV_CODEC_ID_AV1:
2933  if (side_data_size && mkv->track.bc && !par->extradata_size) {
2934  // If the reserved space doesn't suffice, only write
2935  // the first four bytes of the av1C.
2936  ret = mkv_update_codecprivate(s, mkv, side_data, side_data_size,
2937  par, mkv->track.bc, track, 4);
2938  if (ret < 0)
2939  return ret;
2940  } else if (!par->extradata_size)
2941  return AVERROR_INVALIDDATA;
2942  break;
2943  default:
2944  if (side_data_size)
2945  av_log(s, AV_LOG_DEBUG, "Ignoring new extradata in a packet for stream %d.\n", pkt->stream_index);
2946  break;
2947  }
2948 
2949  return 0;
2950 }
2951 
2953 {
2954  MatroskaMuxContext *mkv = s->priv_data;
2955  AVIOContext *pb;
2956  AVCodecParameters *par = s->streams[pkt->stream_index]->codecpar;
2957  mkv_track *track = &mkv->tracks[pkt->stream_index];
2958  int is_sub = par->codec_type == AVMEDIA_TYPE_SUBTITLE;
2959  /* All subtitle blocks are considered to be keyframes. */
2960  int keyframe = is_sub || !!(pkt->flags & AV_PKT_FLAG_KEY);
2961  int64_t duration = FFMAX(pkt->duration, 0);
2962  int64_t cue_duration = is_sub ? duration : 0;
2963  int ret;
2964  int64_t ts = track->write_dts ? pkt->dts : pkt->pts;
2965  int64_t relative_packet_pos;
2966 
2967  if (ts == AV_NOPTS_VALUE) {
2968  av_log(s, AV_LOG_ERROR, "Can't write packet with unknown timestamp\n");
2969  return AVERROR(EINVAL);
2970  }
2971  ts += track->ts_offset;
2972 
2973  if (mkv->cluster_pos != -1) {
2974  int64_t cluster_time = ts - mkv->cluster_pts;
2975  if ((int16_t)cluster_time != cluster_time) {
2976  ret = mkv_end_cluster(s);
2977  if (ret < 0)
2978  return ret;
2979  av_log(s, AV_LOG_WARNING, "Starting new cluster due to timestamp\n");
2980  }
2981  }
2982 
2983  if (mkv->cluster_pos == -1) {
2984  ret = start_ebml_master_crc32(&mkv->cluster_bc, mkv);
2985  if (ret < 0)
2986  return ret;
2987  mkv->cluster_bc->direct = 1;
2988  mkv->cluster_pos = avio_tell(s->pb);
2990  mkv->cluster_pts = FFMAX(0, ts);
2992  "Starting new cluster with timestamp "
2993  "%" PRId64 " at offset %" PRId64 " bytes\n",
2994  mkv->cluster_pts, mkv->cluster_pos);
2995  }
2996  pb = mkv->cluster_bc;
2997 
2998  relative_packet_pos = avio_tell(pb);
2999 
3000  /* The WebM spec requires WebVTT to be muxed in BlockGroups;
3001  * so we force it even for packets without duration. */
3002  ret = mkv_write_block(s, mkv, pb, par, track, pkt,
3003  keyframe, ts, duration,
3004  par->codec_id == AV_CODEC_ID_WEBVTT,
3005  relative_packet_pos);
3006  if (ret < 0)
3007  return ret;
3008  if (keyframe && IS_SEEKABLE(s->pb, mkv) &&
3009  (par->codec_type == AVMEDIA_TYPE_VIDEO ||
3011  !mkv->have_video && !track->has_cue)) {
3012  ret = mkv_add_cuepoint(mkv, pkt->stream_index, ts,
3013  mkv->cluster_pos, relative_packet_pos,
3014  cue_duration);
3015  if (ret < 0)
3016  return ret;
3017  track->has_cue = 1;
3018  }
3019 
3020  track->last_timestamp = ts;
3021  mkv->duration = FFMAX(mkv->duration, ts + duration);
3022  track->duration = FFMAX(track->duration, ts + duration);
3023 
3024  return 0;
3025 }
3026 
3028 {
3029  MatroskaMuxContext *mkv = s->priv_data;
3030  int codec_type = s->streams[pkt->stream_index]->codecpar->codec_type;
3031  int keyframe = !!(pkt->flags & AV_PKT_FLAG_KEY);
3032  int cluster_size;
3033  int64_t cluster_time;
3034  int ret;
3035  int start_new_cluster;
3036 
3038  if (ret < 0)
3039  return ret;
3040 
3041  if (mkv->cluster_pos != -1) {
3042  if (mkv->tracks[pkt->stream_index].write_dts)
3043  cluster_time = pkt->dts - mkv->cluster_pts;
3044  else
3045  cluster_time = pkt->pts - mkv->cluster_pts;
3046  cluster_time += mkv->tracks[pkt->stream_index].ts_offset;
3047 
3048  cluster_size = avio_tell(mkv->cluster_bc);
3049 
3050  if (mkv->is_dash && codec_type == AVMEDIA_TYPE_VIDEO) {
3051  // WebM DASH specification states that the first block of
3052  // every Cluster has to be a key frame. So for DASH video,
3053  // we only create a Cluster on seeing key frames.
3054  start_new_cluster = keyframe;
3055  } else if (mkv->is_dash && codec_type == AVMEDIA_TYPE_AUDIO &&
3056  cluster_time > mkv->cluster_time_limit) {
3057  // For DASH audio, we create a Cluster based on cluster_time_limit.
3058  start_new_cluster = 1;
3059  } else if (!mkv->is_dash &&
3060  (cluster_size > mkv->cluster_size_limit ||
3061  cluster_time > mkv->cluster_time_limit ||
3062  (codec_type == AVMEDIA_TYPE_VIDEO && keyframe &&
3063  cluster_size > 4 * 1024))) {
3064  start_new_cluster = 1;
3065  } else
3066  start_new_cluster = 0;
3067 
3068  if (start_new_cluster) {
3069  ret = mkv_end_cluster(s);
3070  if (ret < 0)
3071  return ret;
3072  }
3073  }
3074 
3075  if (mkv->cluster_pos == -1)
3076  avio_write_marker(s->pb,
3077  av_rescale_q(pkt->dts, s->streams[pkt->stream_index]->time_base, AV_TIME_BASE_Q),
3079 
3080  // check if we have an audio packet cached
3081  if (mkv->cur_audio_pkt->size > 0) {
3084  if (ret < 0) {
3086  "Could not write cached audio packet ret:%d\n", ret);
3087  return ret;
3088  }
3089  }
3090 
3091  // buffer an audio packet to ensure the packet containing the video
3092  // keyframe's timecode is contained in the same cluster for WebM
3093  if (codec_type == AVMEDIA_TYPE_AUDIO) {
3094  if (pkt->size > 0)
3096  } else
3098  return ret;
3099 }
3100 
3102 {
3103  MatroskaMuxContext *mkv = s->priv_data;
3104 
3105  if (!pkt) {
3106  if (mkv->cluster_pos != -1) {
3107  int ret = mkv_end_cluster(s);
3108  if (ret < 0)
3109  return ret;
3111  "Flushing cluster at offset %" PRIu64 " bytes\n",
3112  avio_tell(s->pb));
3113  }
3114  return 1;
3115  }
3116  return mkv_write_packet(s, pkt);
3117 }
3118 
3120 {
3121  MatroskaMuxContext *mkv = s->priv_data;
3122  AVIOContext *pb = s->pb;
3123  int64_t endpos, ret64;
3124  int ret, ret2 = 0;
3125 
3126  // check if we have an audio packet cached
3127  if (mkv->cur_audio_pkt->size > 0) {
3129  if (ret < 0) {
3131  "Could not write cached audio packet ret:%d\n", ret);
3132  return ret;
3133  }
3134  }
3135 
3136  if (mkv->cluster_pos != -1) {
3137  ret = end_ebml_master_crc32(pb, &mkv->cluster_bc, mkv,
3138  MATROSKA_ID_CLUSTER, 0, 0, 0);
3139  if (ret < 0)
3140  return ret;
3141  }
3142 
3144  if (ret < 0)
3145  return ret;
3146 
3147  if (!IS_SEEKABLE(pb, mkv))
3148  return 0;
3149 
3150  endpos = avio_tell(pb);
3151 
3152  if (mkv->cues.num_entries && mkv->reserve_cues_space >= 0) {
3153  AVIOContext *cues = NULL;
3154  uint64_t size, offset = 0;
3155  int length_size = 0;
3156 
3157 redo_cues:
3158  ret = start_ebml_master_crc32(&cues, mkv);
3159  if (ret < 0)
3160  return ret;
3161 
3162  ret = mkv_assemble_cues(s->streams, cues, mkv->tmp_bc, &mkv->cues,
3163  mkv->tracks, s->nb_streams, offset);
3164  if (ret < 0) {
3165  ffio_free_dyn_buf(&cues);
3166  return ret;
3167  }
3168 
3169  if (mkv->reserve_cues_space || mkv->move_cues_to_front) {
3170  size = avio_tell(cues);
3171  length_size = ebml_length_size(size);
3172  size += 4 + length_size;
3173  if (offset + mkv->reserve_cues_space < size) {
3174  if (mkv->move_cues_to_front) {
3175  offset = size - mkv->reserve_cues_space;
3176  ffio_reset_dyn_buf(cues);
3177  goto redo_cues;
3178  }
3180  "Insufficient space reserved for Cues: "
3181  "%d < %"PRIu64". No Cues will be output.\n",
3182  mkv->reserve_cues_space, size);
3183  ret2 = AVERROR(EINVAL);
3184  goto after_cues;
3185  } else {
3186  if (offset) {
3188  offset);
3189  if (ret < 0) {
3190  ffio_free_dyn_buf(&cues);
3191  return ret;
3192  }
3193  endpos += offset;
3194  }
3195  if ((ret64 = avio_seek(pb, mkv->cues_pos, SEEK_SET)) < 0) {
3196  ffio_free_dyn_buf(&cues);
3197  return ret64;
3198  }
3199  if (mkv->reserve_cues_space == size + 1) {
3200  /* There is no way to reserve a single byte because
3201  * the minimal size of an EBML Void element is 2
3202  * (1 byte ID, 1 byte length field). This problem
3203  * is solved by writing the Cues' length field on
3204  * one byte more than necessary. */
3205  length_size++;
3206  size++;
3207  }
3208  }
3209  }
3210  ret = end_ebml_master_crc32(pb, &cues, mkv, MATROSKA_ID_CUES,
3211  length_size, 0, 1);
3212  if (ret < 0)
3213  return ret;
3214  if (mkv->reserve_cues_space) {
3215  if (size < mkv->reserve_cues_space)
3217  } else if (!mkv->move_cues_to_front)
3218  endpos = avio_tell(pb);
3219  }
3220 
3221 after_cues:
3222  /* Lengths greater than (1ULL << 56) - 1 can't be represented
3223  * via an EBML number, so leave the unknown length field. */
3224  if (endpos - mkv->segment_offset < (1ULL << 56) - 1) {
3225  if ((ret64 = avio_seek(pb, mkv->segment_offset - 8, SEEK_SET)) < 0)
3226  return ret64;
3227  put_ebml_length(pb, endpos - mkv->segment_offset, 8);
3228  }
3229 
3230  ret = mkv_write_seekhead(pb, mkv, 1, mkv->info.pos);
3231  if (ret < 0)
3232  return ret;
3233 
3234  if (mkv->info.bc) {
3235  // update the duration
3236  av_log(s, AV_LOG_DEBUG, "end duration = %" PRIu64 "\n", mkv->duration);
3237  avio_seek(mkv->info.bc, mkv->duration_offset, SEEK_SET);
3239  ret = end_ebml_master_crc32(pb, &mkv->info.bc, mkv,
3240  MATROSKA_ID_INFO, 0, 0, 0);
3241  if (ret < 0)
3242  return ret;
3243  }
3244 
3245  if (mkv->track.bc) {
3246  // write Tracks master
3247  if (!IS_WEBM(mkv)) {
3248  AVIOContext *track_bc = mkv->track.bc;
3249 
3250  for (unsigned i = 0; i < s->nb_streams; i++) {
3251  const mkv_track *track = &mkv->tracks[i];
3252 
3253  if (!track->max_blockaddid)
3254  continue;
3255 
3256  // We reserved a single byte to write this value.
3257  av_assert0(track->max_blockaddid <= 0xFF);
3258 
3259  avio_seek(track_bc, track->blockadditionmapping_offset, SEEK_SET);
3260 
3262  track->max_blockaddid);
3264  ebml_master mapping_master = start_ebml_master(track_bc, MATROSKA_ID_TRACKBLKADDMAPPING, 8);
3269  end_ebml_master(track_bc, mapping_master);
3270  }
3271  }
3272  }
3273 
3274  avio_seek(pb, mkv->track.pos, SEEK_SET);
3275  ret = end_ebml_master_crc32(pb, &mkv->track.bc, mkv,
3276  MATROSKA_ID_TRACKS, 0, 0, 0);
3277  if (ret < 0)
3278  return ret;
3279  }
3280 
3281  // update stream durations
3282  if (mkv->tags.bc) {
3283  AVIOContext *tags_bc = mkv->tags.bc;
3284  int i;
3285  for (i = 0; i < s->nb_streams; ++i) {
3286  const AVStream *st = s->streams[i];
3287  const mkv_track *track = &mkv->tracks[i];
3288 
3289  if (track->duration_offset > 0) {
3290  double duration_sec = track->duration * av_q2d(st->time_base);
3291  char duration_string[DURATION_STRING_LENGTH + 1] = "";
3292  ebml_master simpletag;
3293 
3294  av_log(s, AV_LOG_DEBUG, "stream %d end duration = %" PRIu64 "\n", i,
3295  track->duration);
3296 
3297  avio_seek(tags_bc, track->duration_offset, SEEK_SET);
3298  simpletag = start_ebml_master(tags_bc, MATROSKA_ID_SIMPLETAG,
3299  2 + 1 + 8 + 23);
3300  put_ebml_string(tags_bc, MATROSKA_ID_TAGNAME, "DURATION");
3301 
3302  snprintf(duration_string, sizeof(duration_string), "%02d:%02d:%012.9f",
3303  (int) duration_sec / 3600, ((int) duration_sec / 60) % 60,
3304  fmod(duration_sec, 60));
3305 
3307  duration_string, DURATION_STRING_LENGTH);
3308  end_ebml_master(tags_bc, simpletag);
3309  }
3310  }
3311 
3312  avio_seek(pb, mkv->tags.pos, SEEK_SET);
3313  ret = end_ebml_master_crc32(pb, &mkv->tags.bc, mkv,
3314  MATROSKA_ID_TAGS, 0, 0, 0);
3315  if (ret < 0)
3316  return ret;
3317  }
3318 
3319  avio_seek(pb, endpos, SEEK_SET);
3320 
3321  return ret2;
3322 }
3323 
3324 static uint64_t mkv_get_uid(const mkv_track *tracks, int i, AVLFG *c)
3325 {
3326  while (1) {
3327  uint64_t uid;
3328  int k;
3329  uid = (uint64_t)av_lfg_get(c) << 32;
3330  uid |= av_lfg_get(c);
3331  if (!uid)
3332  continue;
3333  for (k = 0; k < i; k++) {
3334  if (tracks[k].uid == uid)
3335  break;
3336  }
3337  if (k == i)
3338  return uid;
3339  }
3340 }
3341 
3342 static int mkv_init(struct AVFormatContext *s)
3343 {
3344  FFFormatContext *const si = ffformatcontext(s);
3345  MatroskaMuxContext *mkv = s->priv_data;
3346  AVLFG c;
3347  unsigned nb_tracks = 0;
3348  int i;
3349 
3350  mkv->ctx = s;
3351 
3352  for (i = 0; i < s->nb_streams; i++) {
3353  if (s->streams[i]->codecpar->codec_id == AV_CODEC_ID_ATRAC3 ||
3354  s->streams[i]->codecpar->codec_id == AV_CODEC_ID_COOK ||
3355  s->streams[i]->codecpar->codec_id == AV_CODEC_ID_RA_288 ||
3356  s->streams[i]->codecpar->codec_id == AV_CODEC_ID_SIPR ||
3357  s->streams[i]->codecpar->codec_id == AV_CODEC_ID_RV10 ||
3358  s->streams[i]->codecpar->codec_id == AV_CODEC_ID_RV20 ||
3359  s->streams[i]->codecpar->codec_id == AV_CODEC_ID_RV30) {
3361  "The Matroska muxer does not yet support muxing %s\n",
3362  avcodec_get_name(s->streams[i]->codecpar->codec_id));
3363  return AVERROR_PATCHWELCOME;
3364  }
3365  }
3366 
3367  if (s->avoid_negative_ts < 0) {
3368  s->avoid_negative_ts = 1;
3369  si->avoid_negative_ts_use_pts = 1;
3370  }
3371 
3372  if (!CONFIG_MATROSKA_MUXER ||
3373  (CONFIG_WEBM_MUXER && !strcmp(s->oformat->name, "webm"))) {
3374  mkv->mode = MODE_WEBM;
3375  mkv->write_crc = 0;
3376  } else
3377  mkv->mode = MODE_MATROSKAv2;
3378 
3380 
3381  mkv->tracks = av_calloc(s->nb_streams, sizeof(*mkv->tracks));
3382  if (!mkv->tracks)
3383  return AVERROR(ENOMEM);
3384 
3385  if (!(s->flags & AVFMT_FLAG_BITEXACT)) {
3387 
3388  // Calculate the SegmentUID now in order not to waste our random seed.
3389  for (i = 0; i < 4; i++)
3390  mkv->segment_uid[i] = av_lfg_get(&c);
3391  }
3392 
3393  for (i = 0; i < s->nb_streams; i++) {
3394  AVStream *st = s->streams[i];
3395  const AVCodecParameters *const par = st->codecpar;
3396  mkv_track *track = &mkv->tracks[i];
3397 
3398  switch (par->codec_id) {
3399 #if CONFIG_MATROSKA_MUXER
3400  case AV_CODEC_ID_WAVPACK:
3401  track->reformat = mkv_reformat_wavpack;
3402  break;
3403  case AV_CODEC_ID_H264:
3404  case AV_CODEC_ID_HEVC:
3405  if ((par->codec_id == AV_CODEC_ID_H264 && par->extradata_size > 0 ||
3406  par->codec_id == AV_CODEC_ID_HEVC && par->extradata_size > 6) &&
3407  (AV_RB24(par->extradata) == 1 || AV_RB32(par->extradata) == 1))
3408  track->reformat = mkv_reformat_h2645;
3409  break;
3410  case AV_CODEC_ID_PRORES:
3411  /* Matroska specification requires to remove
3412  * the first QuickTime atom. */
3413  track->offset = 8;
3414  break;
3415 #endif
3416  case AV_CODEC_ID_AV1:
3417  track->reformat = mkv_reformat_av1;
3418  break;
3419  case AV_CODEC_ID_WEBVTT:
3420  track->reformat = webm_reformat_vtt;
3421  break;
3422  }
3423 
3424  if (s->flags & AVFMT_FLAG_BITEXACT) {
3425  track->uid = i + 1;
3426  } else {
3427  track->uid = mkv_get_uid(mkv->tracks, i, &c);
3428  }
3429 
3430  // ms precision is the de-facto standard timescale for mkv files
3431  avpriv_set_pts_info(st, 64, 1, 1000);
3432 
3434  if (IS_WEBM(mkv)) {
3435  av_log(s, AV_LOG_WARNING, "Stream %d will be ignored "
3436  "as WebM doesn't support attachments.\n", i);
3437  } else if (!get_mimetype(st)) {
3438  av_log(s, AV_LOG_ERROR, "Attachment stream %d has no mimetype "
3439  "tag and it cannot be deduced from the codec id.\n", i);
3440  return AVERROR(EINVAL);
3441  }
3442  mkv->nb_attachments++;
3443  continue;
3444  }
3445 
3446  nb_tracks++;
3447  track->track_num = mkv->is_dash ? mkv->dash_track_number : nb_tracks;
3448  track->track_num_size = ebml_num_size(track->track_num);
3449  }
3450 
3451  if (mkv->is_dash && nb_tracks != 1)
3452  return AVERROR(EINVAL);
3453 
3454  return 0;
3455 }
3456 
3458  const AVPacket *pkt)
3459 {
3460  int ret = 1;
3461 
3462  if (CONFIG_MATROSKA_MUXER && st->codecpar->codec_id == AV_CODEC_ID_AAC) {
3463  if (pkt->size > 2 && (AV_RB16(pkt->data) & 0xfff0) == 0xfff0)
3464  ret = ff_stream_add_bitstream_filter(st, "aac_adtstoasc", NULL);
3465  } else if (st->codecpar->codec_id == AV_CODEC_ID_VP9) {
3466  ret = ff_stream_add_bitstream_filter(st, "vp9_superframe", NULL);
3467  } else if (CONFIG_MATROSKA_MUXER &&
3469  ret = ff_stream_add_bitstream_filter(st, "pgs_frame_merge", NULL);
3470  }
3471 
3472  return ret;
3473 }
3474 
3476  { AV_CODEC_ID_ALAC, 0XFFFFFFFF },
3477  { AV_CODEC_ID_MLP, 0xFFFFFFFF },
3478  { AV_CODEC_ID_OPUS, 0xFFFFFFFF },
3479  { AV_CODEC_ID_PCM_S16BE, 0xFFFFFFFF },
3480  { AV_CODEC_ID_PCM_S24BE, 0xFFFFFFFF },
3481  { AV_CODEC_ID_PCM_S32BE, 0xFFFFFFFF },
3482  { AV_CODEC_ID_QDMC, 0xFFFFFFFF },
3483  { AV_CODEC_ID_QDM2, 0xFFFFFFFF },
3484  { AV_CODEC_ID_RA_144, 0xFFFFFFFF },
3485  { AV_CODEC_ID_TRUEHD, 0xFFFFFFFF },
3486  { AV_CODEC_ID_NONE, 0xFFFFFFFF }
3487 };
3488 
3490  { AV_CODEC_ID_DVB_SUBTITLE, 0xFFFFFFFF },
3491  { AV_CODEC_ID_DVD_SUBTITLE, 0xFFFFFFFF },
3492  { AV_CODEC_ID_HDMV_PGS_SUBTITLE, 0xFFFFFFFF },
3493  { AV_CODEC_ID_ARIB_CAPTION, 0xFFFFFFFF },
3494  { AV_CODEC_ID_NONE, 0xFFFFFFFF }
3495 };
3496 
3497 #define OFFSET(x) offsetof(MatroskaMuxContext, x)
3498 #define FLAGS AV_OPT_FLAG_ENCODING_PARAM
3499 static const AVOption options[] = {
3500  { "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 },
3501  { "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 },
3502  { "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 },
3503  { "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 },
3504  { "dash", "Create a WebM file conforming to WebM DASH specification", OFFSET(is_dash), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
3505  { "dash_track_number", "Track number for the DASH stream", OFFSET(dash_track_number), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, INT_MAX, FLAGS },
3506  { "live", "Write files assuming it is a live stream.", OFFSET(is_live), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
3507  { "allow_raw_vfw", "allow RAW VFW mode", OFFSET(allow_raw_vfw), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
3508  { "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 },
3509  { "write_crc32", "write a CRC32 element inside every Level 1 element", OFFSET(write_crc), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, FLAGS },
3510  { "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" },
3511  { "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" },
3512  { "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" },
3513  { "passthrough", "Use the disposition flag as-is", 0, AV_OPT_TYPE_CONST, { .i64 = DEFAULT_MODE_PASSTHROUGH }, 0, 0, FLAGS, "default_mode" },
3514  { NULL },
3515 };
3516 
3518  .class_name = "matroska/webm muxer",
3519  .item_name = av_default_item_name,
3520  .option = options,
3521  .version = LIBAVUTIL_VERSION_INT,
3522 };
3523 
3524 #if CONFIG_MATROSKA_MUXER
3525 static int mkv_query_codec(enum AVCodecID codec_id, int std_compliance)
3526 {
3527  for (int i = 0; ff_mkv_codec_tags[i].id != AV_CODEC_ID_NONE; i++)
3528  if (ff_mkv_codec_tags[i].id == codec_id)
3529  return 1;
3530 
3531  if (std_compliance < FF_COMPLIANCE_NORMAL) {
3533  // mkv theoretically supports any video/audio through VFW/ACM
3535  return 1;
3536  }
3537 
3538  return 0;
3539 }
3540 
3542  .p.name = "matroska",
3543  .p.long_name = NULL_IF_CONFIG_SMALL("Matroska"),
3544  .p.mime_type = "video/x-matroska",
3545  .p.extensions = "mkv",
3546  .priv_data_size = sizeof(MatroskaMuxContext),
3547  .p.audio_codec = CONFIG_LIBVORBIS_ENCODER ?
3549  .p.video_codec = CONFIG_LIBX264_ENCODER ?
3551  .init = mkv_init,
3552  .deinit = mkv_deinit,
3553  .write_header = mkv_write_header,
3554  .write_packet = mkv_write_flush_packet,
3555  .write_trailer = mkv_write_trailer,
3556  .p.flags = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS |
3558  AVFMT_TS_NONSTRICT | AVFMT_ALLOW_FLUSH,
3559 #else
3561 #endif
3562  .p.codec_tag = (const AVCodecTag* const []){
3565  },
3566  .p.subtitle_codec = AV_CODEC_ID_ASS,
3567  .query_codec = mkv_query_codec,
3568  .check_bitstream = mkv_check_bitstream,
3569  .p.priv_class = &matroska_webm_class,
3570  .flags_internal = FF_FMT_ALLOW_FLUSH,
3571 };
3572 #endif
3573 
3574 #if CONFIG_WEBM_MUXER
3575 static int webm_query_codec(enum AVCodecID codec_id, int std_compliance)
3576 {
3577  for (int i = 0; ff_webm_codec_tags[i].id != AV_CODEC_ID_NONE; i++)
3578  if (ff_webm_codec_tags[i].id == codec_id)
3579  return 1;
3580 
3581  return 0;
3582 }
3583 
3584 const FFOutputFormat ff_webm_muxer = {
3585  .p.name = "webm",
3586  .p.long_name = NULL_IF_CONFIG_SMALL("WebM"),
3587  .p.mime_type = "video/webm",
3588  .p.extensions = "webm",
3589  .priv_data_size = sizeof(MatroskaMuxContext),
3590  .p.audio_codec = CONFIG_LIBOPUS_ENCODER ? AV_CODEC_ID_OPUS : AV_CODEC_ID_VORBIS,
3591  .p.video_codec = CONFIG_LIBVPX_VP9_ENCODER? AV_CODEC_ID_VP9 : AV_CODEC_ID_VP8,
3592  .p.subtitle_codec = AV_CODEC_ID_WEBVTT,
3593  .init = mkv_init,
3594  .deinit = mkv_deinit,
3595  .write_header = mkv_write_header,
3596  .write_packet = mkv_write_flush_packet,
3597  .write_trailer = mkv_write_trailer,
3598  .query_codec = webm_query_codec,
3599  .check_bitstream = mkv_check_bitstream,
3600  .p.flags = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS |
3602  AVFMT_TS_NONSTRICT | AVFMT_ALLOW_FLUSH,
3603 #else
3605 #endif
3606  .p.priv_class = &matroska_webm_class,
3607  .flags_internal = FF_FMT_ALLOW_FLUSH,
3608 };
3609 #endif
3610 
3611 #if CONFIG_MATROSKA_AUDIO_MUXER
3613  .p.name = "matroska",
3614  .p.long_name = NULL_IF_CONFIG_SMALL("Matroska Audio"),
3615  .p.mime_type = "audio/x-matroska",
3616  .p.extensions = "mka",
3617  .priv_data_size = sizeof(MatroskaMuxContext),
3618  .p.audio_codec = CONFIG_LIBVORBIS_ENCODER ?
3620  .p.video_codec = AV_CODEC_ID_NONE,
3621  .init = mkv_init,
3622  .deinit = mkv_deinit,
3623  .write_header = mkv_write_header,
3624  .write_packet = mkv_write_flush_packet,
3625  .write_trailer = mkv_write_trailer,
3626  .check_bitstream = mkv_check_bitstream,
3628  .p.flags = AVFMT_GLOBALHEADER | AVFMT_TS_NONSTRICT |
3629  AVFMT_ALLOW_FLUSH,
3630 #else
3632 #endif
3633  .p.codec_tag = (const AVCodecTag* const []){
3635  },
3636  .p.priv_class = &matroska_webm_class,
3637  .flags_internal = FF_FMT_ALLOW_FLUSH,
3638 };
3639 #endif
MatroskaMuxContext::reserve_cues_space
int reserve_cues_space
Definition: matroskaenc.c:247
MATROSKA_ID_TAGTARGETS_ATTACHUID
#define MATROSKA_ID_TAGTARGETS_ATTACHUID
Definition: matroska.h:225
AVMasteringDisplayMetadata::has_primaries
int has_primaries
Flag indicating whether the display primaries (and white point) are set.
Definition: mastering_display_metadata.h:62
AV_PKT_DATA_DISPLAYMATRIX
@ AV_PKT_DATA_DISPLAYMATRIX
This side data contains a 3x3 transformation matrix describing an affine transformation that needs to...
Definition: packet.h:109
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:423
MATROSKA_ID_VIDEODISPLAYUNIT
#define MATROSKA_ID_VIDEODISPLAYUNIT
Definition: matroska.h:125
MATROSKA_ID_CODECPRIVATE
#define MATROSKA_ID_CODECPRIVATE
Definition: matroska.h:88
MATROSKA_ID_TRACKNUMBER
#define MATROSKA_ID_TRACKNUMBER
Definition: matroska.h:77
AVMEDIA_TYPE_SUBTITLE
@ AVMEDIA_TYPE_SUBTITLE
Definition: avutil.h:204
EBML_BIN
@ EBML_BIN
Definition: matroskaenc.c:110
MATROSKA_ID_TRACKFLAGVISUALIMPAIRED
#define MATROSKA_ID_TRACKFLAGVISUALIMPAIRED
Definition: matroska.h:101
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:253
MATROSKA_ID_CHAPSTRING
#define MATROSKA_ID_CHAPSTRING
Definition: matroska.h:266
IS_SEEKABLE
#define IS_SEEKABLE(pb, mkv)
Definition: matroskaenc.c:84
mkv_init
static int mkv_init(struct AVFormatContext *s)
Definition: matroskaenc.c:3342
MATROSKA_ID_TAGTARGETS
#define MATROSKA_ID_TAGTARGETS
Definition: matroska.h:220
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:69
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:235
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:310
AV_CODEC_ID_AC3
@ AV_CODEC_ID_AC3
Definition: codec_id.h:446
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:133
mkv_write_packet
static int mkv_write_packet(AVFormatContext *s, const AVPacket *pkt)
Definition: matroskaenc.c:3027
AVOutputFormat::name
const char * name
Definition: avformat.h:511
MATROSKA_ID_TITLE
#define MATROSKA_ID_TITLE
Definition: matroska.h:67
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:2310
opt.h
put_ebml_size_unknown
static void put_ebml_size_unknown(AVIOContext *pb, int bytes)
Write an EBML size meaning "unknown size".
Definition: matroskaenc.c:297
ebml_writer_add_string
static void ebml_writer_add_string(EbmlWriter *writer, uint32_t id, const char *str)
Definition: matroskaenc.c:499
mkv_write_packet_internal
static int mkv_write_packet_internal(AVFormatContext *s, const AVPacket *pkt)
Definition: matroskaenc.c:2952
MatroskaMuxContext::allow_raw_vfw
int allow_raw_vfw
Definition: matroskaenc.c:255
ebml_writer_add_float
static void ebml_writer_add_float(EbmlWriter *writer, uint32_t id, double val)
Definition: matroskaenc.c:519
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
AVSphericalMapping::projection
enum AVSphericalProjection projection
Projection type.
Definition: spherical.h:82
AV1_SANE_SEQUENCE_HEADER_MAX_BITS
#define AV1_SANE_SEQUENCE_HEADER_MAX_BITS
Definition: av1.h:189
AV_WL32
#define AV_WL32(p, v)
Definition: intreadwrite.h:424
hevc.h
AVSphericalMapping::bound_bottom
uint32_t bound_bottom
Distance from the bottom edge.
Definition: spherical.h:166
MATROSKA_ID_VIDEOCOLORMASTERINGMETA
#define MATROSKA_ID_VIDEOCOLORMASTERINGMETA
Definition: matroska.h:149
ebml_writer_master_len
static int ebml_writer_master_len(EbmlWriter *writer, EbmlElement *elem, int remaining_elems)
Definition: matroskaenc.c:594
AVFieldOrder
AVFieldOrder
Definition: defs.h:198
MatroskaMuxContext::segment_offset
int64_t segment_offset
Definition: matroskaenc.c:218
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:224
mkv_handle_spherical
static int mkv_handle_spherical(void *logctx, EbmlWriter *writer, const AVStream *st, uint8_t private[], double *yaw, double *pitch, double *roll)
Definition: matroskaenc.c:1468
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:47
EBML_ID_EBMLMAXSIZELENGTH
#define EBML_ID_EBMLMAXSIZELENGTH
Definition: matroska.h:38
ffformatcontext
static av_always_inline FFFormatContext * ffformatcontext(AVFormatContext *s)
Definition: internal.h:194
mkv_new_chapter_ids_needed
static int mkv_new_chapter_ids_needed(const AVFormatContext *s)
Definition: matroskaenc.c:2290
AVCodecParameters::color_space
enum AVColorSpace color_space
Definition: codec_par.h:144
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:777
MATROSKA_ID_TRACKFLAGTEXTDESCRIPTIONS
#define MATROSKA_ID_TRACKFLAGTEXTDESCRIPTIONS
Definition: matroska.h:102
AV_PKT_DATA_NEW_EXTRADATA
@ AV_PKT_DATA_NEW_EXTRADATA
The AV_PKT_DATA_NEW_EXTRADATA is used to notify the codec or the format that the extradata buffer was...
Definition: packet.h:56
AVFMT_VARIABLE_FPS
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition: avformat.h:483
avcodec_get_type
enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
Get the type of the given codec.
Definition: codec_desc.c:3761
AV_FIELD_PROGRESSIVE
@ AV_FIELD_PROGRESSIVE
Definition: defs.h:200
MATROSKA_ID_VIDEOPIXELWIDTH
#define MATROSKA_ID_VIDEOPIXELWIDTH
Definition: matroska.h:119
EbmlMaster::containing_master
int containing_master
-1 if no parent exists
Definition: matroskaenc.c:125
mkv_track::duration
int64_t duration
Definition: matroskaenc.c:192
MATROSKA_ID_AUDIOSAMPLINGFREQ
#define MATROSKA_ID_AUDIOSAMPLINGFREQ
Definition: matroska.h:169
AV_PKT_DATA_MASTERING_DISPLAY_METADATA
@ AV_PKT_DATA_MASTERING_DISPLAY_METADATA
Mastering display metadata (based on SMPTE-2086:2014).
Definition: packet.h:223
MatroskaMuxContext::cluster_bc
AVIOContext * cluster_bc
Definition: matroskaenc.c:219
OPUS_SEEK_PREROLL
#define OPUS_SEEK_PREROLL
Seek preroll value for opus.
Definition: matroskaenc.c:278
matrix
Definition: vc1dsp.c:42
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:128
MODE_MATROSKAv2
#define MODE_MATROSKAv2
Definition: matroskaenc.c:79
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:215
rational.h
MATROSKA_ID_DISCARDPADDING
#define MATROSKA_ID_DISCARDPADDING
Definition: matroska.h:250
AV_TIME_BASE_Q
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:264
MATROSKA_VIDEO_FIELDORDER_BB
@ MATROSKA_VIDEO_FIELDORDER_BB
Definition: matroska.h:307
ebml_writer_add_uint
static void ebml_writer_add_uint(EbmlWriter *writer, uint32_t id, uint64_t val)
Definition: matroskaenc.c:534
MATROSKA_ID_DURATION
#define MATROSKA_ID_DURATION
Definition: matroska.h:66
MATROSKA_ID_VIDEOCOLORCHROMASITINGVERT
#define MATROSKA_ID_VIDEOCOLORCHROMASITINGVERT
Definition: matroska.h:141
BlockContext::pkt
const AVPacket * pkt
Definition: matroskaenc.c:117
avlanguage.h
av_strcasecmp
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
Definition: avstring.c:207
MATROSKA_ID_VIDEOCOLORPRIMARIES
#define MATROSKA_ID_VIDEOCOLORPRIMARIES
Definition: matroska.h:145
AV_CODEC_ID_RAWVIDEO
@ AV_CODEC_ID_RAWVIDEO
Definition: codec_id.h:65
av_unused
#define av_unused
Definition: attributes.h:131
MATROSKA_ID_SEGMENT
#define MATROSKA_ID_SEGMENT
Definition: matroska.h:52
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:719
AV_CODEC_ID_MPEG4
@ AV_CODEC_ID_MPEG4
Definition: codec_id.h:64
MATROSKA_ID_BLKADDIDTYPE
#define MATROSKA_ID_BLKADDIDTYPE
Definition: matroska.h:195
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:254
AV_CODEC_ID_RA_144
@ AV_CODEC_ID_RA_144
Definition: codec_id.h:428
ff_metadata_conv
void ff_metadata_conv(AVDictionary **pm, const AVMetadataConv *d_conv, const AVMetadataConv *s_conv)
Definition: metadata.c:26
ebml_writer_write
static int ebml_writer_write(EbmlWriter *writer, AVIOContext *pb)
Definition: matroskaenc.c:743
MATROSKA_ID_CHAPTERS
#define MATROSKA_ID_CHAPTERS
Definition: matroska.h:62
AVCOL_TRC_NB
@ AVCOL_TRC_NB
Not part of ABI.
Definition: pixfmt.h:592
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:700
MATROSKA_ID_VIDEOCOLOR_BY
#define MATROSKA_ID_VIDEOCOLOR_BY
Definition: matroska.h:155
pixdesc.h
AVPacketSideData
This structure stores auxiliary information for decoding, presenting, or otherwise processing the cod...
Definition: packet.h:342
MATROSKA_ID_BLOCKDURATION
#define MATROSKA_ID_BLOCKDURATION
Definition: matroska.h:247
MATROSKA_ID_VIDEOCOLORRANGE
#define MATROSKA_ID_VIDEOCOLORRANGE
Definition: matroska.h:142
MATROSKA_ID_BLOCK
#define MATROSKA_ID_BLOCK
Definition: matroska.h:246
av_dynamic_hdr_plus_to_t35
int av_dynamic_hdr_plus_to_t35(const AVDynamicHDRPlus *s, uint8_t **data, size_t *size)
Serialize dynamic HDR10+ metadata to a user data registered ITU-T T.35 buffer, excluding the first 48...
Definition: hdr_dynamic_metadata.c:239
AVPacket::data
uint8_t * data
Definition: packet.h:491
MATROSKA_ID_TRACKDEFAULTDURATION
#define MATROSKA_ID_TRACKDEFAULTDURATION
Definition: matroska.h:108
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:837
AVOption
AVOption.
Definition: opt.h:251
AVCOL_SPC_NB
@ AVCOL_SPC_NB
Not part of ABI.
Definition: pixfmt.h:616
AVCOL_TRC_UNSPECIFIED
@ AVCOL_TRC_UNSPECIFIED
Definition: pixfmt.h:573
MatroskaMuxContext::mode
int mode
Definition: matroskaenc.c:214
AVStream::avg_frame_rate
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:930
mkv_track::duration_offset
int64_t duration_offset
Definition: matroskaenc.c:193
data
const char data[16]
Definition: mxf.c:148
MATROSKA_VIDEO_INTERLACE_FLAG_INTERLACED
@ MATROSKA_VIDEO_INTERLACE_FLAG_INTERLACED
Definition: matroska.h:299
MatroskaMuxContext::cluster_pts
int64_t cluster_pts
Definition: matroskaenc.c:221
mkv_write_attachments
static int mkv_write_attachments(AVFormatContext *s)
Definition: matroskaenc.c:2404
AV_CODEC_ID_ALAC
@ AV_CODEC_ID_ALAC
Definition: codec_id.h:459
MATROSKA_ID_DATEUTC
#define MATROSKA_ID_DATEUTC
Definition: matroska.h:70
ebml_id_size
static int ebml_id_size(uint32_t id)
Definition: matroskaenc.c:280
flacenc.h
AVIOContext::error
int error
contains the error code or 0 if no error happened
Definition: avio.h:249
MatroskaMuxContext::tags
ebml_stored_master tags
Definition: matroskaenc.c:217
EBML_UID
@ EBML_UID
Definition: matroskaenc.c:107
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:126
ff_codec_wav_tags
const AVCodecTag ff_codec_wav_tags[]
Definition: riff.c:518
mkv_track::ts_offset
int64_t ts_offset
Definition: matroskaenc.c:198
put_ebml_binary
static void put_ebml_binary(AVIOContext *pb, uint32_t elementid, const void *buf, int size)
Definition: matroskaenc.c:390
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:509
AVCodecParameters::codec_tag
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: codec_par.h:59
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:68
MAX_VIDEO_COLOR_ELEMS
#define MAX_VIDEO_COLOR_ELEMS
Definition: matroskaenc.c:1329
max
#define max(a, b)
Definition: cuda_runtime.h:33
BlockContext
Definition: matroskaenc.c:115
mathematics.h
MATROSKA_ID_CUES
#define MATROSKA_ID_CUES
Definition: matroska.h:57
AVDictionary
Definition: dict.c:34
AV_CODEC_ID_FLAC
@ AV_CODEC_ID_FLAC
Definition: codec_id.h:455
AVChannelLayout::order
enum AVChannelOrder order
Channel order used in this layout.
Definition: channel_layout.h:314
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
MATROSKA_ID_CUETIME
#define MATROSKA_ID_CUETIME
Definition: matroska.h:202
IS_WEBM
#define IS_WEBM(mkv)
Definition: matroskaenc.c:82
AV_PROFILE_ARIB_PROFILE_C
#define AV_PROFILE_ARIB_PROFILE_C
Definition: defs.h:187
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:341
WvHeader
Definition: wv.h:34
MATROSKA_ID_CUERELATIVEPOSITION
#define MATROSKA_ID_CUERELATIVEPOSITION
Definition: matroska.h:208
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:319
MAX_PCE_SIZE
#define MAX_PCE_SIZE
Maximum size of a PCE including the 3-bit ID_PCE.
Definition: mpeg4audio.h:117
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:558
bit_depth
static void bit_depth(AudioStatsContext *s, const uint64_t *const mask, uint8_t *depth)
Definition: af_astats.c:245
AV_SPHERICAL_EQUIRECTANGULAR
@ AV_SPHERICAL_EQUIRECTANGULAR
Video represents a sphere mapped on a flat surface using equirectangular projection.
Definition: spherical.h:52
intfloat.h
codec_type
enum AVMediaType codec_type
Definition: rtp.c:37
MatroskaMuxContext::flipped_raw_rgb
int flipped_raw_rgb
Definition: matroskaenc.c:256
MATROSKA_ID_CHAPLANG
#define MATROSKA_ID_CHAPLANG
Definition: matroska.h:267
AV_CODEC_ID_TRUEHD
@ AV_CODEC_ID_TRUEHD
Definition: codec_id.h:487
MATROSKA_ID_CUEDURATION
#define MATROSKA_ID_CUEDURATION
Definition: matroska.h:209
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:1519
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:546
sample_rate
sample_rate
Definition: ffmpeg_filter.c:368
AV_WB64
#define AV_WB64(p, v)
Definition: intreadwrite.h:431
FFOutputFormat::p
AVOutputFormat p
The public AVOutputFormat.
Definition: mux.h:36
MatroskaMuxContext::cues_pos
int64_t cues_pos
Definition: matroskaenc.c:227
AV_FIELD_BT
@ AV_FIELD_BT
Bottom coded first, top displayed first.
Definition: defs.h:204
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:167
xiph.h
MatroskaMuxContext::cluster_size_limit
int cluster_size_limit
Definition: matroskaenc.c:248
MATROSKA_VIDEO_DISPLAYUNIT_UNKNOWN
@ MATROSKA_VIDEO_DISPLAYUNIT_UNKNOWN
Definition: matroska.h:336
mkv_track::codecpriv_offset
int codecpriv_offset
Definition: matroskaenc.c:196
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:158
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
MPEG4AudioConfig
Definition: mpeg4audio.h:29
crc.h
avio_wl16
void avio_wl16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:484
AVCodecParameters::color_primaries
enum AVColorPrimaries color_primaries
Definition: codec_par.h:142
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:508
MATROSKA_ID_VIDEOCOLOR_WHITEY
#define MATROSKA_ID_VIDEOCOLOR_WHITEY
Definition: matroska.h:157
NOTHING
#define NOTHING(STEREOMODETYPE, WDIV, HDIV, WEBM)
AV_PROFILE_ARIB_PROFILE_A
#define AV_PROFILE_ARIB_PROFILE_A
Definition: defs.h:186
HDIV1
#define HDIV1(STEREOMODETYPE, STEREO3DTYPE, FLAGS, WDIV, HDIV, WEBM)
start_ebml_master
static ebml_master start_ebml_master(AVIOContext *pb, uint32_t elementid, uint64_t expectedsize)
Definition: matroskaenc.c:428
AV_FIELD_TT
@ AV_FIELD_TT
Top coded_first, top displayed first.
Definition: defs.h:201
DVCC_DVVC_BLOCK_TYPE_NAME
#define DVCC_DVVC_BLOCK_TYPE_NAME
Definition: matroska.h:439
AV_CODEC_ID_ASS
@ AV_CODEC_ID_ASS
Definition: codec_id.h:574
MATROSKA_ID_AUDIOOUTSAMPLINGFREQ
#define MATROSKA_ID_AUDIOOUTSAMPLINGFREQ
Definition: matroska.h:170
MAX_SUPPORTED_EBML_LENGTH
#define MAX_SUPPORTED_EBML_LENGTH
Definition: matroskaenc.c:77
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:761
mpeg4audio.h
MATROSKA_ID_VIDEOPROJECTIONPRIVATE
#define MATROSKA_ID_VIDEOPROJECTIONPRIVATE
Definition: matroska.h:163
FF_FMT_ALLOW_FLUSH
#define FF_FMT_ALLOW_FLUSH
Definition: mux.h:30
ffstream
static av_always_inline FFStream * ffstream(AVStream *st)
Definition: internal.h:420
AV_CODEC_ID_PCM_S16BE
@ AV_CODEC_ID_PCM_S16BE
Definition: codec_id.h:332
mkv_cues
Definition: matroskaenc.c:175
MatroskaMuxContext::move_cues_to_front
int move_cues_to_front
Definition: matroskaenc.c:258
fail
#define fail()
Definition: checkasm.h:142
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:110
ebml_writer_sint_len
static int ebml_writer_sint_len(EbmlElement *elem)
Definition: matroskaenc.c:585
EbmlElement::sint
int64_t sint
Definition: matroskaenc.c:135
MATROSKA_ID_CHAPTERDISPLAY
#define MATROSKA_ID_CHAPTERDISPLAY
Definition: matroska.h:265
MATROSKA_ID_TRACKUID
#define MATROSKA_ID_TRACKUID
Definition: matroska.h:78
samplefmt.h
EBML_ID_DOCTYPEVERSION
#define EBML_ID_DOCTYPEVERSION
Definition: matroska.h:40
AVCOL_RANGE_NB
@ AVCOL_RANGE_NB
Not part of ABI.
Definition: pixfmt.h:674
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:370
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:513
ff_matroska_video_stereo_mode
const char *const ff_matroska_video_stereo_mode[MATROSKA_VIDEO_STEREOMODE_TYPE_NB]
Definition: matroska.c:129
AVChapter
Definition: avformat.h:1074
AV_DISPOSITION_FORCED
#define AV_DISPOSITION_FORCED
Track should be used during playback by default.
Definition: avformat.h:752
val
static double val(void *priv, double ch)
Definition: aeval.c:78
MATROSKA_BLOCK_ADD_ID_TYPE_ITU_T_T35
@ MATROSKA_BLOCK_ADD_ID_TYPE_ITU_T_T35
Definition: matroska.h:363
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:129
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:643
us
#define us(width, name, range_min, range_max, subs,...)
Definition: cbs_h2645.c:252
EBML_ID_DOCTYPE
#define EBML_ID_DOCTYPE
Definition: matroska.h:39
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:156
AVRational::num
int num
Numerator.
Definition: rational.h:59
AV_FIELD_TB
@ AV_FIELD_TB
Top coded first, bottom displayed first.
Definition: defs.h:203
MATROSKA_TRACK_TYPE_METADATA
@ MATROSKA_TRACK_TYPE_METADATA
Definition: matroska.h:287
AV_CODEC_ID_ATRAC3
@ AV_CODEC_ID_ATRAC3
Definition: codec_id.h:474
mkv_track::max_blockaddid
uint64_t max_blockaddid
Definition: matroskaenc.c:194
MATROSKA_VIDEO_FIELDORDER_TT
@ MATROSKA_VIDEO_FIELDORDER_TT
Definition: matroska.h:305
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
AV_CODEC_ID_DVB_SUBTITLE
@ AV_CODEC_ID_DVB_SUBTITLE
Definition: codec_id.h:553
MatroskaVideoStereoModeType
MatroskaVideoStereoModeType
Definition: matroska.h:312
av_get_bits_per_sample
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:558
AV_CODEC_ID_SIPR
@ AV_CODEC_ID_SIPR
Definition: codec_id.h:484
AVCodecParameters::color_trc
enum AVColorTransferCharacteristic color_trc
Definition: codec_par.h:143
MATROSKA_ID_BLKADDIDNAME
#define MATROSKA_ID_BLKADDIDNAME
Definition: matroska.h:194
mkv_write_tracks
static int mkv_write_tracks(AVFormatContext *s)
Definition: matroskaenc.c:2085
MatroskaMuxContext::ctx
AVFormatContext * ctx
Definition: matroskaenc.c:212
mkv_seekhead_entry
Definition: matroskaenc.c:155
MATROSKA_ID_BLOCKADDITIONAL
#define MATROSKA_ID_BLOCKADDITIONAL
Definition: matroska.h:242
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
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
EBML_ID_VOID
#define EBML_ID_VOID
Definition: matroska.h:44
MATROSKA_ID_VIDEOFIELDORDER
#define MATROSKA_ID_VIDEOFIELDORDER
Definition: matroska.h:127
AVCodecTag
Definition: internal.h:48
MATROSKA_ID_TAGS
#define MATROSKA_ID_TAGS
Definition: matroska.h:58
AV_CODEC_ID_TTA
@ AV_CODEC_ID_TTA
Definition: codec_id.h:465
options
static const AVOption options[]
Definition: matroskaenc.c:3499
duration
int64_t duration
Definition: movenc.c:64
put_ebml_id
static void put_ebml_id(AVIOContext *pb, uint32_t id)
Definition: matroskaenc.c:285
AV_FIELD_UNKNOWN
@ AV_FIELD_UNKNOWN
Definition: defs.h:199
EBML_ID_EBMLMAXIDLENGTH
#define EBML_ID_EBMLMAXIDLENGTH
Definition: matroska.h:37
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:62
avio_open_dyn_buf
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1507
mkv_cuepoint::relative_pos
int64_t relative_pos
relative offset from the position of the cluster containing the block
Definition: matroskaenc.c:171
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:1292
AV_CODEC_ID_ADPCM_G726
@ AV_CODEC_ID_ADPCM_G726
Definition: codec_id.h:381
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:620
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:438
MATROSKA_ID_FILEUID
#define MATROSKA_ID_FILEUID
Definition: matroska.h:258
s
#define s(width, name)
Definition: cbs_vp9.c:198
AVCHROMA_LOC_TOP
@ AVCHROMA_LOC_TOP
Definition: pixfmt.h:697
MATROSKA_VIDEO_INTERLACE_FLAG_PROGRESSIVE
@ MATROSKA_VIDEO_INTERLACE_FLAG_PROGRESSIVE
Definition: matroska.h:300
EbmlElement::bin
const uint8_t * bin
Definition: matroskaenc.c:138
av_chroma_location_enum_to_pos
int av_chroma_location_enum_to_pos(int *xpos, int *ypos, enum AVChromaLocation pos)
Converts AVChromaLocation to swscale x/y chroma position.
Definition: pixdesc.c:3382
NALUList::nalus
NALU * nalus
Definition: avc.h:35
AVCOL_PRI_NB
@ AVCOL_PRI_NB
Not part of ABI.
Definition: pixfmt.h:563
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:183
mkv_write_stereo_mode
static int mkv_write_stereo_mode(AVFormatContext *s, EbmlWriter *writer, const AVStream *st, int is_webm, int *h_width, int *h_height)
Definition: matroskaenc.c:1591
CodecTags::str
char str[22]
Definition: matroska.h:376
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:117
av_realloc_array
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:215
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:3101
ebml_writer_add_block
static void ebml_writer_add_block(EbmlWriter *writer, MatroskaMuxContext *mkv)
Definition: matroskaenc.c:548
MATROSKA_VIDEO_FIELDORDER_BT
@ MATROSKA_VIDEO_FIELDORDER_BT
Definition: matroska.h:309
MatroskaMuxContext::cluster_pos
int64_t cluster_pos
file offset of the current Cluster
Definition: matroskaenc.c:220
AVDictionaryEntry::key
char * key
Definition: dict.h:90
MATROSKA_ID_TAGNAME
#define MATROSKA_ID_TAGNAME
Definition: matroska.h:215
AVSphericalMapping::bound_top
uint32_t bound_top
Distance from the top edge.
Definition: spherical.h:164
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AV_CODEC_ID_VP9
@ AV_CODEC_ID_VP9
Definition: codec_id.h:220
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:121
ebml_writer_open_master
static void ebml_writer_open_master(EbmlWriter *writer, uint32_t id)
Definition: matroskaenc.c:456
MATROSKA_ID_TAG
#define MATROSKA_ID_TAG
Definition: matroska.h:213
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:527
MATROSKA_ID_TIMECODESCALE
#define MATROSKA_ID_TIMECODESCALE
Definition: matroska.h:65
lfg.h
MatroskaMuxContext::tmp_bc
AVIOContext * tmp_bc
Definition: matroskaenc.c:237
get_mimetype
static const char * get_mimetype(const AVStream *st)
Definition: matroskaenc.c:2387
ebml_writer_add_sint
static void ebml_writer_add_sint(EbmlWriter *writer, uint32_t id, int64_t val)
Definition: matroskaenc.c:541
MatroskaMuxContext::write_crc
int write_crc
Definition: matroskaenc.c:250
matroska_webm_class
static const AVClass matroska_webm_class
Definition: matroskaenc.c:3517
DEFAULT_MODE_INFER
@ DEFAULT_MODE_INFER
Definition: matroskaenc.c:88
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:40
MATROSKA_ID_CUETRACKPOSITION
#define MATROSKA_ID_CUETRACKPOSITION
Definition: matroska.h:203
additional_audio_tags
static const AVCodecTag additional_audio_tags[]
Definition: matroskaenc.c:3475
MATROSKA_ID_SEEKENTRY
#define MATROSKA_ID_SEEKENTRY
Definition: matroska.h:228
ff_isom_put_dvcc_dvvc
void ff_isom_put_dvcc_dvvc(void *logctx, uint8_t out[ISOM_DVCC_DVVC_SIZE], const AVDOVIDecoderConfigurationRecord *dovi)
Definition: dovi_isom.c:84
mkv_track::track_num
unsigned track_num
Definition: matroskaenc.c:186
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:79
AVPacketSideData::data
uint8_t * data
Definition: packet.h:343
ebml_writer_close_master
static void ebml_writer_close_master(EbmlWriter *writer)
Definition: matroskaenc.c:467
AVDOVIDecoderConfigurationRecord::dv_profile
uint8_t dv_profile
Definition: dovi_meta.h:55
MATROSKA_ID_SEGMENTUID
#define MATROSKA_ID_SEGMENTUID
Definition: matroska.h:71
EbmlType
EbmlType
Definition: matroskadec.c:87
MATROSKA_ID_TRACKBLKADDMAPPING
#define MATROSKA_ID_TRACKBLKADDMAPPING
Definition: matroska.h:113
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
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:579
MatroskaMuxContext::cluster_time_limit
int64_t cluster_time_limit
Definition: matroskaenc.c:249
EbmlElement::type
EbmlType type
Definition: matroskaenc.c:130
EbmlElement::f
double f
Definition: matroskaenc.c:136
MATROSKA_ID_INFO
#define MATROSKA_ID_INFO
Definition: matroska.h:55
MatroskaMuxContext::have_video
int have_video
Definition: matroskaenc.c:242
MATROSKA_ID_AUDIOCHANNELS
#define MATROSKA_ID_AUDIOCHANNELS
Definition: matroska.h:173
codec_id
enum AVCodecID codec_id
Definition: vaapi_decode.c:389
MATROSKA_ID_BLKADDIDVALUE
#define MATROSKA_ID_BLKADDIDVALUE
Definition: matroska.h:193
AV_CODEC_ID_SVQ3
@ AV_CODEC_ID_SVQ3
Definition: codec_id.h:75
EbmlType
EbmlType
Definition: matroskaenc.c:103
key
const char * key
Definition: hwcontext_opencl.c:174
AVCodecParameters::nb_coded_side_data
int nb_coded_side_data
Amount of entries in coded_side_data.
Definition: codec_par.h:231
AVCOL_PRI_UNSPECIFIED
@ AVCOL_PRI_UNSPECIFIED
Definition: pixfmt.h:548
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:60
AV_CODEC_ID_H264
@ AV_CODEC_ID_H264
Definition: codec_id.h:79
MATROSKA_ID_VIDEOSTEREOMODE
#define MATROSKA_ID_VIDEOSTEREOMODE
Definition: matroska.h:128
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:866
ff_webm_codec_tags
const CodecTags ff_webm_codec_tags[]
Definition: matroska.c:107
mkv_track::uid
uint64_t uid
Definition: matroskaenc.c:185
HDIV2
#define HDIV2(STEREOMODETYPE, WDIV, HDIV, WEBM)
AV_CODEC_ID_ARIB_CAPTION
@ AV_CODEC_ID_ARIB_CAPTION
Definition: codec_id.h:577
AVStereo3D::flags
int flags
Additional information about the frame packing.
Definition: stereo3d.h:182
MATROSKA_ID_VIDEOCOLORSPACE
#define MATROSKA_ID_VIDEOCOLORSPACE
Definition: matroska.h:131
if
if(ret)
Definition: filter_design.txt:179
MATROSKA_ID_TRACKNAME
#define MATROSKA_ID_TRACKNAME
Definition: matroska.h:95
FFFormatContext
Definition: internal.h:70
BlockContext::track
struct mkv_track * track
Definition: matroskaenc.c:116
AV_CODEC_ID_WEBVTT
@ AV_CODEC_ID_WEBVTT
Definition: codec_id.h:570
EBML_ID_DOCTYPEREADVERSION
#define EBML_ID_DOCTYPEREADVERSION
Definition: matroska.h:41
mkv_cues::entries
mkv_cuepoint * entries
Definition: matroskaenc.c:176
AVFormatContext
Format I/O context.
Definition: avformat.h:1115
internal.h
MATROSKA_ID_TRACKFLAGHEARINGIMPAIRED
#define MATROSKA_ID_TRACKFLAGHEARINGIMPAIRED
Definition: matroska.h:100
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:864
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:238
MATROSKA_ID_VIDEOCOLOR_LUMINANCEMIN
#define MATROSKA_ID_VIDEOCOLOR_LUMINANCEMIN
Definition: matroska.h:159
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
EbmlWriter
Definition: matroskaenc.c:144
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:1190
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:880
NULL
#define NULL
Definition: coverity.c:32
MATROSKA_ID_ATTACHMENTS
#define MATROSKA_ID_ATTACHMENTS
Definition: matroska.h:60
STEREOMODE_STEREO3D_MAPPING
#define STEREOMODE_STEREO3D_MAPPING(MAP, MKV_ONLY)
Definition: matroska.h:401
ff_put_wav_header
int ff_put_wav_header(AVFormatContext *s, AVIOContext *pb, AVCodecParameters *par, int flags)
Write WAVEFORMAT header structure.
Definition: riffenc.c:54
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:262
AV_CODEC_ID_AV1
@ AV_CODEC_ID_AV1
Definition: codec_id.h:283
MATROSKA_TRACK_TYPE_AUDIO
@ MATROSKA_TRACK_TYPE_AUDIO
Definition: matroska.h:281
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:323
STEREOMODE
#define STEREOMODE(STEREOMODETYPE, STEREO3DTYPE, FLAGS, WDIV, HDIV, WEBM)
isom.h
MATROSKA_ID_TRACKFLAGORIGINAL
#define MATROSKA_ID_TRACKFLAGORIGINAL
Definition: matroska.h:103
MATROSKA_VIDEO_DISPLAYUNIT_DAR
@ MATROSKA_VIDEO_DISPLAYUNIT_DAR
Definition: matroska.h:335
mkv_track::default_duration_high
uint64_t default_duration_high
Definition: matroskaenc.c:200
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
MATROSKA_ID_WRITINGAPP
#define MATROSKA_ID_WRITINGAPP
Definition: matroska.h:68
WV_HEADER_SIZE
#define WV_HEADER_SIZE
Definition: wavpack.h:33
MatroskaMuxContext::info
ebml_stored_master info
Definition: matroskaenc.c:215
AV_DISPOSITION_COMMENT
#define AV_DISPOSITION_COMMENT
The stream is a commentary track.
Definition: avformat.h:737
MATROSKA_ID_VIDEOCOLOR_GY
#define MATROSKA_ID_VIDEOCOLOR_GY
Definition: matroska.h:153
AV_CODEC_ID_CINEPAK
@ AV_CODEC_ID_CINEPAK
Definition: codec_id.h:95
AV_CODEC_ID_DVD_SUBTITLE
@ AV_CODEC_ID_DVD_SUBTITLE
Definition: codec_id.h:552
MAX_VIDEO_PROJECTION_ELEMS
#define MAX_VIDEO_PROJECTION_ELEMS
Definition: matroskaenc.c:1412
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:214
MATROSKA_ID_VIDEOPROJECTION
#define MATROSKA_ID_VIDEOPROJECTION
Definition: matroska.h:161
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:1113
MatroskaMuxContext::segment_uid
uint32_t segment_uid[4]
Definition: matroskaenc.c:260
MatroskaMuxContext::cur_block
BlockContext cur_block
Definition: matroskaenc.c:229
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:1001
AVStream::metadata
AVDictionary * metadata
Definition: avformat.h:921
MATROSKA_ID_SEEKID
#define MATROSKA_ID_SEEKID
Definition: matroska.h:231
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:332
FFOutputFormat
Definition: mux.h:32
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:801
EBML_STR
@ EBML_STR
Definition: matroskaenc.c:108
double
double
Definition: af_crystalizer.c:131
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:226
MATROSKA_ID_CHAPTERTIMESTART
#define MATROSKA_ID_CHAPTERTIMESTART
Definition: matroska.h:263
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:234
mkv_add_seekhead_entry
static void mkv_add_seekhead_entry(MatroskaMuxContext *mkv, uint32_t elementid, uint64_t filepos)
Definition: matroskaenc.c:753
AV_DISPOSITION_ORIGINAL
#define AV_DISPOSITION_ORIGINAL
The stream is in original language.
Definition: avformat.h:733
mkv_track::sample_rate_offset
int64_t sample_rate_offset
Definition: matroskaenc.c:190
AV_CODEC_ID_QDM2
@ AV_CODEC_ID_QDM2
Definition: codec_id.h:462
mkv_get_uid
static uint64_t mkv_get_uid(const mkv_track *tracks, int i, AVLFG *c)
Definition: matroskaenc.c:3324
MATROSKA_TRACK_TYPE_VIDEO
@ MATROSKA_TRACK_TYPE_VIDEO
Definition: matroska.h:280
ebml_writer_add
static EbmlElement * ebml_writer_add(EbmlWriter *writer, uint32_t id, EbmlType type)
Definition: matroskaenc.c:448
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:431
get_metadata_duration
static int64_t get_metadata_duration(AVFormatContext *s)
Definition: matroskaenc.c:2454
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:206
mkv_track::has_cue
int has_cue
Definition: matroskaenc.c:184
MATROSKA_ID_FILEMIMETYPE
#define MATROSKA_ID_FILEMIMETYPE
Definition: matroska.h:256
MATROSKA_ID_VIDEODISPLAYHEIGHT
#define MATROSKA_ID_VIDEODISPLAYHEIGHT
Definition: matroska.h:118
WDIV1
#define WDIV1(STEREOMODETYPE, STEREO3DTYPE, FLAGS, WDIV, HDIV, WEBM)
EbmlWriter::elements
EbmlElement * elements
Definition: matroskaenc.c:147
ff_matroska_audio_muxer
const FFOutputFormat ff_matroska_audio_muxer
DURATION_STRING_LENGTH
#define DURATION_STRING_LENGTH
DURATION_STRING_LENGTH must be <= 112 or the containing simpletag will need more than one byte for it...
Definition: matroskaenc.c:272
AV_PKT_DATA_CONTENT_LIGHT_LEVEL
@ AV_PKT_DATA_CONTENT_LIGHT_LEVEL
Content light level (based on CTA-861.3).
Definition: packet.h:236
MATROSKA_ID_TRACKAUDIO
#define MATROSKA_ID_TRACKAUDIO
Definition: matroska.h:81
AVCOL_RANGE_UNSPECIFIED
@ AVCOL_RANGE_UNSPECIFIED
Definition: pixfmt.h:639
EbmlElement::str
const char * str
Definition: matroskaenc.c:137
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:241
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:171
put_ebml_float
static void put_ebml_float(AVIOContext *pb, uint32_t elementid, double val)
Definition: matroskaenc.c:383
uint_size
static av_const int uint_size(uint64_t val)
Definition: matroskaenc.c:564
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:49
av_packet_side_data_get
const AVPacketSideData * av_packet_side_data_get(const AVPacketSideData *sd, int nb_sd, enum AVPacketSideDataType type)
Get side information from a side data array.
Definition: avpacket.c:650
MATROSKA_ID_VIDEOCOLORMAXCLL
#define MATROSKA_ID_VIDEOCOLORMAXCLL
Definition: matroska.h:146
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_TRACKENTRY
#define MATROSKA_ID_TRACKENTRY
Definition: matroska.h:74
ebml_stored_master
Definition: matroskaenc.c:98
WDIV2
#define WDIV2(STEREOMODETYPE, WDIV, HDIV, WEBM)
AVCodecParameters::extradata_size
int extradata_size
Size of the extradata content in bytes.
Definition: codec_par.h:73
AV_WB32
#define AV_WB32(p, v)
Definition: intreadwrite.h:417
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:1263
AV_CODEC_ID_AAC
@ AV_CODEC_ID_AAC
Definition: codec_id.h:445
mkv_write_header
static int mkv_write_header(AVFormatContext *s)
Definition: matroskaenc.c:2553
mkv_track::default_duration_low
uint64_t default_duration_low
Definition: matroskaenc.c:199
MatroskaMuxContext
Definition: matroskaenc.c:210
mkv_cuepoint::duration
int64_t duration
duration of the block according to time base
Definition: matroskaenc.c:172
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:124
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:507
AV_DISPOSITION_CAPTIONS
#define AV_DISPOSITION_CAPTIONS
The subtitle stream contains captions, providing a transcription and possibly a translation of audio.
Definition: avformat.h:790
mkv_track::blockadditionmapping_offset
int64_t blockadditionmapping_offset
Definition: matroskaenc.c:195
MATROSKA_ID_AUDIOBITDEPTH
#define MATROSKA_ID_AUDIOBITDEPTH
Definition: matroska.h:172
AV_CODEC_ID_FFV1
@ AV_CODEC_ID_FFV1
Definition: codec_id.h:85
AVLFG
Context structure for the Lagged Fibonacci PRNG.
Definition: lfg.h:33
mkv_track::offset
unsigned offset
Definition: matroskaenc.c:189
MATROSKA_ID_TRACKFLAGDEFAULT
#define MATROSKA_ID_TRACKFLAGDEFAULT
Definition: matroska.h:98
AVIOContext
Bytestream IO Context.
Definition: avio.h:166
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:61
MATROSKA_ID_TRACKFLAGCOMMENTARY
#define MATROSKA_ID_TRACKFLAGCOMMENTARY
Definition: matroska.h:104
ffio_reset_dyn_buf
void ffio_reset_dyn_buf(AVIOContext *s)
Reset a dynamic buffer.
Definition: aviobuf.c:1541
AVMediaType
AVMediaType
Definition: avutil.h:199
MATROSKA_ID_TRACKFLAGLACING
#define MATROSKA_ID_TRACKFLAGLACING
Definition: matroska.h:105
AVPacket::size
int size
Definition: packet.h:492
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:106
MATROSKA_ID_VIDEOCOLOR_GX
#define MATROSKA_ID_VIDEOCOLOR_GX
Definition: matroska.h:152
ff_codec_get_id
enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
Definition: utils.c:145
DEFAULT_MODE_INFER_NO_SUBS
@ DEFAULT_MODE_INFER_NO_SUBS
Definition: matroskaenc.c:89
master
const char * master
Definition: vf_curves.c:129
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:165
mkv_write_field_order
static void mkv_write_field_order(EbmlWriter *writer, int is_webm, enum AVFieldOrder field_order)
Definition: matroskaenc.c:1551
AV_PKT_DATA_DYNAMIC_HDR10_PLUS
@ AV_PKT_DATA_DYNAMIC_HDR10_PLUS
HDR10+ dynamic metadata associated with a video frame.
Definition: packet.h:300
EBML_FLOAT
@ EBML_FLOAT
Definition: matroskaenc.c:106
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:257
ebml_master::sizebytes
int sizebytes
how many bytes were reserved for the size
Definition: matroskaenc.c:95
MATROSKA_ID_VIDEOPROJECTIONPOSEPITCH
#define MATROSKA_ID_VIDEOPROJECTIONPOSEPITCH
Definition: matroska.h:165
size
int size
Definition: twinvq_data.h:10344
MATROSKA_ID_BLOCKMORE
#define MATROSKA_ID_BLOCKMORE
Definition: matroska.h:240
MAX_SEEKHEAD_ENTRIES
#define MAX_SEEKHEAD_ENTRIES
Definition: matroskaenc.c:71
mkv_handle_rotation
static void mkv_handle_rotation(void *logctx, const AVStream *st, double *yaw, double *roll)
Definition: matroskaenc.c:1413
MATROSKA_BLOCK_ADD_ID_TYPE_OPAQUE
@ MATROSKA_BLOCK_ADD_ID_TYPE_OPAQUE
Definition: matroska.h:362
mkv_cuepoint::pts
uint64_t pts
Definition: matroskaenc.c:168
ebml_stored_master::bc
AVIOContext * bc
Definition: matroskaenc.c:99
EbmlWriter::current_master_element
int current_master_element
Definition: matroskaenc.c:146
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
AV_CODEC_ID_QDMC
@ AV_CODEC_ID_QDMC
Definition: codec_id.h:493
put_ebml_void
static void put_ebml_void(AVIOContext *pb, int size)
Write a void element of a given size.
Definition: matroskaenc.c:410
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:147
EbmlElement::size
uint64_t size
excluding id and length field
Definition: matroskaenc.c:132
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
av_get_audio_frame_duration2
int av_get_audio_frame_duration2(AVCodecParameters *par, int frame_bytes)
This function is the same as av_get_audio_frame_duration(), except it works with AVCodecParameters in...
Definition: utils.c:812
AVCodecParameters::profile
int profile
Codec-specific bitstream restrictions that the stream conforms to.
Definition: codec_par.h:115
AV_CODEC_ID_OPUS
@ AV_CODEC_ID_OPUS
Definition: codec_id.h:503
AVCHROMA_LOC_UNSPECIFIED
@ AVCHROMA_LOC_UNSPECIFIED
Definition: pixfmt.h:693
AV_DISPOSITION_DUB
#define AV_DISPOSITION_DUB
The stream is not in original language.
Definition: avformat.h:727
MatroskaMuxContext::cur_audio_pkt
AVPacket * cur_audio_pkt
Definition: matroskaenc.c:239
EBML_UINT
@ EBML_UINT
Definition: matroskaenc.c:104
AVStream::sample_aspect_ratio
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:919
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:127