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