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