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 #endif
1255  }
1256 
1257  *codecpriv_size = avio_get_dyn_buf(dyn_cp, codecpriv);
1258  if (dyn_cp->error < 0)
1259  return dyn_cp->error;
1260  *max_payload_size = *codecpriv_size + size_to_reserve;
1261 
1262  return 0;
1263 }
1264 
1265 static void mkv_put_codecprivate(AVIOContext *pb, unsigned max_payload_size,
1266  const uint8_t *codecpriv, unsigned codecpriv_size)
1267 {
1268  unsigned total_codecpriv_size = 0, total_size;
1269 
1270  av_assert1(codecpriv_size <= max_payload_size);
1271 
1272  if (!max_payload_size)
1273  return;
1274 
1275  total_size = 2 + ebml_length_size(max_payload_size) + max_payload_size;
1276 
1277  if (codecpriv_size) {
1278  unsigned length_size = ebml_length_size(codecpriv_size);
1279 
1280  total_codecpriv_size = 2U + length_size + codecpriv_size;
1281  if (total_codecpriv_size + 1 == total_size) {
1282  /* It is impossible to add one byte of padding via an EBML Void. */
1283  length_size++;
1284  total_codecpriv_size++;
1285  }
1287  put_ebml_length(pb, codecpriv_size, length_size);
1288  avio_write(pb, codecpriv, codecpriv_size);
1289  }
1290  if (total_codecpriv_size < total_size)
1291  put_ebml_void(pb, total_size - total_codecpriv_size);
1292 }
1293 
1295  uint8_t *side_data, int side_data_size,
1296  AVCodecParameters *par, AVIOContext *pb,
1297  mkv_track *track, unsigned alternative_size)
1298 {
1299  AVIOContext *const dyn_bc = mkv->tmp_bc;
1300  uint8_t *codecpriv;
1301  unsigned max_payload_size;
1302  int ret, codecpriv_size;
1303 
1304  ret = mkv_assemble_codecprivate(s, dyn_bc, par,
1305  side_data, side_data_size, 1, 0,
1306  &codecpriv, &codecpriv_size, &max_payload_size);
1307  if (ret < 0)
1308  goto fail;
1309  if (codecpriv_size > track->codecpriv_size && !alternative_size) {
1310  ret = AVERROR(ENOSPC);
1311  goto fail;
1312  } else if (codecpriv_size > track->codecpriv_size) {
1313  av_assert1(alternative_size < track->codecpriv_size);
1314  codecpriv_size = alternative_size;
1315  }
1316  avio_seek(pb, track->codecpriv_offset, SEEK_SET);
1318  codecpriv, codecpriv_size);
1319 
1320  if (!par->extradata_size) {
1321  ret = ff_alloc_extradata(par, side_data_size);
1322  if (ret < 0)
1323  goto fail;
1324  memcpy(par->extradata, side_data, side_data_size);
1325  }
1326 fail:
1327  ffio_reset_dyn_buf(dyn_bc);
1328  return ret;
1329 }
1330 
1331 #define MAX_VIDEO_COLOR_ELEMS 20
1332 static void mkv_write_video_color(EbmlWriter *writer, const AVStream *st,
1333  const AVCodecParameters *par)
1334 {
1335  const AVPacketSideData *side_data;
1336 
1338 
1339  if (par->color_trc != AVCOL_TRC_UNSPECIFIED &&
1340  par->color_trc < AVCOL_TRC_NB) {
1342  par->color_trc);
1343  }
1344  if (par->color_space != AVCOL_SPC_UNSPECIFIED &&
1345  par->color_space < AVCOL_SPC_NB) {
1347  par->color_space);
1348  }
1349  if (par->color_primaries != AVCOL_PRI_UNSPECIFIED &&
1350  par->color_primaries < AVCOL_PRI_NB) {
1352  par->color_primaries);
1353  }
1354  if (par->color_range != AVCOL_RANGE_UNSPECIFIED &&
1355  par->color_range < AVCOL_RANGE_NB) {
1357  }
1360  int xpos, ypos;
1361 
1362  av_chroma_location_enum_to_pos(&xpos, &ypos, par->chroma_location);
1364  (xpos >> 7) + 1);
1366  (ypos >> 7) + 1);
1367  }
1368 
1371  if (side_data) {
1372  const AVContentLightMetadata *metadata = (AVContentLightMetadata *)side_data->data;
1374  metadata->MaxCLL);
1376  metadata->MaxFALL);
1377  }
1378 
1381  if (side_data) {
1382  const AVMasteringDisplayMetadata *metadata = (AVMasteringDisplayMetadata *)side_data->data;
1384  if (metadata->has_primaries) {
1386  av_q2d(metadata->display_primaries[0][0]));
1388  av_q2d(metadata->display_primaries[0][1]));
1390  av_q2d(metadata->display_primaries[1][0]));
1392  av_q2d(metadata->display_primaries[1][1]));
1394  av_q2d(metadata->display_primaries[2][0]));
1396  av_q2d(metadata->display_primaries[2][1]));
1398  av_q2d(metadata->white_point[0]));
1400  av_q2d(metadata->white_point[1]));
1401  }
1402  if (metadata->has_luminance) {
1404  av_q2d(metadata->max_luminance));
1406  av_q2d(metadata->min_luminance));
1407  }
1409  }
1410 
1412 }
1413 
1414 #define MAX_VIDEO_PROJECTION_ELEMS 6
1415 static void mkv_handle_rotation(void *logctx, const AVStream *st,
1416  double *yaw, double *roll)
1417 {
1418  const int32_t *matrix;
1419  const AVPacketSideData *side_data =
1422 
1423  if (!side_data)
1424  return;
1425 
1426  matrix = (int32_t *)side_data->data;
1427 
1428  /* Check whether this is an affine transformation */
1429  if (matrix[2] || matrix[5])
1430  goto ignore;
1431 
1432  /* This together with the checks below test whether
1433  * the upper-left 2x2 matrix is nonsingular. */
1434  if (!matrix[0] && !matrix[1])
1435  goto ignore;
1436 
1437  /* We ignore the translation part of the matrix (matrix[6] and matrix[7])
1438  * as well as any scaling, i.e. we only look at the upper left 2x2 matrix.
1439  * We only accept matrices that are an exact multiple of an orthogonal one.
1440  * Apart from the multiple, every such matrix can be obtained by
1441  * potentially flipping in the x-direction (corresponding to yaw = 180)
1442  * followed by a rotation of (say) an angle phi in the counterclockwise
1443  * direction. The upper-left 2x2 matrix then looks like this:
1444  * | (+/-)cos(phi) (-/+)sin(phi) |
1445  * scale * | |
1446  * | sin(phi) cos(phi) |
1447  * The first set of signs in the first row apply in case of no flipping,
1448  * the second set applies in case of flipping. */
1449 
1450  /* The casts to int64_t are needed because -INT32_MIN doesn't fit
1451  * in an int32_t. */
1452  if (matrix[0] == matrix[4] && -(int64_t)matrix[1] == matrix[3]) {
1453  /* No flipping case */
1454  *yaw = 0;
1455  } else if (-(int64_t)matrix[0] == matrix[4] && matrix[1] == matrix[3]) {
1456  /* Horizontal flip */
1457  *yaw = 180;
1458  } else {
1459 ignore:
1460  av_log(logctx, AV_LOG_INFO, "Ignoring display matrix indicating "
1461  "non-orthogonal transformation.\n");
1462  return;
1463  }
1464  *roll = 180 / M_PI * atan2(matrix[3], matrix[4]);
1465 
1466  /* We do not write a ProjectionType element indicating "rectangular",
1467  * because this is the default value. */
1468 }
1469 
1470 static int mkv_handle_spherical(void *logctx, EbmlWriter *writer,
1471  const AVStream *st, uint8_t private[],
1472  double *yaw, double *pitch, double *roll)
1473 {
1477  const AVSphericalMapping *spherical;
1478 
1479  if (!sd)
1480  return 0;
1481 
1482  spherical = (const AVSphericalMapping *)sd->data;
1483  if (spherical->projection != AV_SPHERICAL_EQUIRECTANGULAR &&
1485  spherical->projection != AV_SPHERICAL_CUBEMAP) {
1486  av_log(logctx, AV_LOG_WARNING, "Unknown projection type\n");
1487  return 0;
1488  }
1489 
1490  switch (spherical->projection) {
1495  AV_WB32(private, 0); // version + flags
1496  if (spherical->projection == AV_SPHERICAL_EQUIRECTANGULAR) {
1497  AV_WB32(private + 4, 0);
1498  AV_WB32(private + 8, 0);
1499  AV_WB32(private + 12, 0);
1500  AV_WB32(private + 16, 0);
1501  } else {
1502  AV_WB32(private + 4, spherical->bound_top);
1503  AV_WB32(private + 8, spherical->bound_bottom);
1504  AV_WB32(private + 12, spherical->bound_left);
1505  AV_WB32(private + 16, spherical->bound_right);
1506  }
1508  private, 20);
1509  break;
1510  case AV_SPHERICAL_CUBEMAP:
1513  AV_WB32(private, 0); // version + flags
1514  AV_WB32(private + 4, 0); // layout
1515  AV_WB32(private + 8, spherical->padding);
1517  private, 12);
1518  break;
1519  default:
1520  av_assert0(0);
1521  }
1522 
1523  *yaw = (double) spherical->yaw / (1 << 16);
1524  *pitch = (double) spherical->pitch / (1 << 16);
1525  *roll = (double) spherical->roll / (1 << 16);
1526 
1527  return 1; /* Projection included */
1528 }
1529 
1530 static void mkv_write_video_projection(void *logctx, EbmlWriter *wr,
1531  const AVStream *st, uint8_t private[])
1532 {
1533  double yaw = 0, pitch = 0, roll = 0;
1534  int ret;
1535 
1537 
1538  ret = mkv_handle_spherical(logctx, wr, st, private, &yaw, &pitch, &roll);
1539  if (!ret)
1540  mkv_handle_rotation(logctx, st, &yaw, &roll);
1541 
1542  if (yaw)
1544  if (pitch)
1546  if (roll)
1548 
1550 }
1551 
1552 #define MAX_FIELD_ORDER_ELEMS 2
1553 static void mkv_write_field_order(EbmlWriter *writer, int is_webm,
1554  enum AVFieldOrder field_order)
1555 {
1556  switch (field_order) {
1557  case AV_FIELD_UNKNOWN:
1558  break;
1559  case AV_FIELD_PROGRESSIVE:
1562  break;
1563  case AV_FIELD_TT:
1564  case AV_FIELD_BB:
1565  case AV_FIELD_TB:
1566  case AV_FIELD_BT:
1569  if (!is_webm) {
1570  switch (field_order) {
1571  case AV_FIELD_TT:
1574  break;
1575  case AV_FIELD_BB:
1578  break;
1579  case AV_FIELD_TB:
1582  break;
1583  case AV_FIELD_BT:
1586  break;
1587  }
1588  }
1589  }
1590 }
1591 
1592 #define MAX_STEREO_MODE_ELEMS 1
1594  const AVStream *st, int is_webm,
1595  int *h_width, int *h_height)
1596 {
1597  const char *error_message_addendum = "";
1598  const AVDictionaryEntry *tag;
1600 
1601  /* The following macros create bitfields where the ith bit
1602  * indicates whether the MatroskaVideoStereoModeType with that value
1603  * uses double width/height or is WebM compatible. */
1604 #define FLAG(STEREOMODETYPE, BOOL) | (BOOL) << (STEREOMODETYPE)
1605 #define WDIV1(STEREOMODETYPE, STEREO3DTYPE, FLAGS, WDIV, HDIV, WEBM) \
1606  FLAG(STEREOMODETYPE, WDIV)
1607 #define WDIV2(STEREOMODETYPE, WDIV, HDIV, WEBM) \
1608  FLAG(STEREOMODETYPE, WDIV)
1609  // The zero in the following line consumes the first '|'.
1610  const unsigned width_bitfield = 0 STEREOMODE_STEREO3D_MAPPING(WDIV1, WDIV2);
1611 
1612 #define HDIV1(STEREOMODETYPE, STEREO3DTYPE, FLAGS, WDIV, HDIV, WEBM) \
1613  FLAG(STEREOMODETYPE, HDIV)
1614 #define HDIV2(STEREOMODETYPE, WDIV, HDIV, WEBM) \
1615  FLAG(STEREOMODETYPE, HDIV)
1616  const unsigned height_bitfield = 0 STEREOMODE_STEREO3D_MAPPING(HDIV1, HDIV2);
1617 
1618 #define WEBM1(STEREOMODETYPE, STEREO3DTYPE, FLAGS, WDIV, HDIV, WEBM) \
1619  FLAG(STEREOMODETYPE, WEBM)
1620 #define WEBM2(STEREOMODETYPE, WDIV, HDIV, WEBM) \
1621  FLAG(STEREOMODETYPE, WEBM)
1622  const unsigned webm_bitfield = 0 STEREOMODE_STEREO3D_MAPPING(WEBM1, WEBM2);
1623 
1624  *h_width = 1;
1625  *h_height = 1;
1626 
1627  if ((tag = av_dict_get(st->metadata, "stereo_mode", NULL, 0)) ||
1628  (tag = av_dict_get( s->metadata, "stereo_mode", NULL, 0))) {
1629 
1630  for (int i = 0; i < MATROSKA_VIDEO_STEREOMODE_TYPE_NB; i++)
1631  if (!strcmp(tag->value, ff_matroska_video_stereo_mode[i])){
1632  format = i;
1633  break;
1634  }
1636  long stereo_mode = strtol(tag->value, NULL, 0);
1637  if ((unsigned long)stereo_mode >= MATROSKA_VIDEO_STEREOMODE_TYPE_NB)
1638  goto fail;
1639  format = stereo_mode;
1640  }
1641  } else {
1642  const AVPacketSideData *sd;
1643  const AVStereo3D *stereo;
1644  /* The following macro presumes all MATROSKA_VIDEO_STEREOMODE_TYPE_*
1645  * values to be in the range 0..254. */
1646 #define STEREOMODE(STEREOMODETYPE, STEREO3DTYPE, FLAGS, WDIV, HDIV, WEBM) \
1647  [(STEREO3DTYPE)][!!((FLAGS) & AV_STEREO3D_FLAG_INVERT)] = (STEREOMODETYPE) + 1,
1648 #define NOTHING(STEREOMODETYPE, WDIV, HDIV, WEBM)
1649  static const unsigned char conversion_table[][2] = {
1651  };
1652  int fmt;
1653 
1656  if (!sd)
1657  return 0;
1658 
1659  stereo = (const AVStereo3D*)sd->data;
1660 
1661  /* A garbage AVStereo3D or something with no Matroska analogon. */
1662  if ((unsigned)stereo->type >= FF_ARRAY_ELEMS(conversion_table))
1663  return 0;
1664 
1665  fmt = conversion_table[stereo->type][!!(stereo->flags & AV_STEREO3D_FLAG_INVERT)];
1666  /* Format with no Matroska analogon; ignore it */
1667  if (!fmt)
1668  return 0;
1669  format = fmt - 1;
1670  }
1671 
1672  // if webm, do not write unsupported modes
1673  if (is_webm && !(webm_bitfield >> format)) {
1674  error_message_addendum = " for WebM";
1675  goto fail;
1676  }
1677 
1678  *h_width = 1 << ((width_bitfield >> format) & 1);
1679  *h_height = 1 << ((height_bitfield >> format) & 1);
1680 
1681  // write StereoMode if format is valid
1683 
1684  return 0;
1685 fail:
1687  "The specified stereo mode is not valid%s.\n",
1688  error_message_addendum);
1689  return AVERROR(EINVAL);
1690 }
1691 
1693  const AVCodecParameters *par, AVIOContext *pb,
1694  mkv_track *track, const AVStream *st)
1695 {
1696 #if CONFIG_MATROSKA_MUXER
1698  const AVPacketSideData *sd;
1699 
1700  if (IS_SEEKABLE(s->pb, mkv)) {
1702  // We can't know at this point if there will be a block with BlockAdditions, so
1703  // we either write the default value here, or a void element. Either of them will
1704  // be overwritten when finishing the track.
1706  if (par->codec_type == AVMEDIA_TYPE_VIDEO) {
1707  // Similarly, reserve space for an eventual
1708  // HDR10+ ITU T.35 metadata BlockAdditionMapping.
1709  put_ebml_void(pb, 3 /* BlockAdditionMapping */
1710  + 4 /* BlockAddIDValue */
1711  + 4 /* BlockAddIDType */);
1712  }
1713  }
1714 
1717 
1718  if (!sd)
1719  return;
1720 
1721  dovi = (const AVDOVIDecoderConfigurationRecord *)sd->data;
1722  if (dovi->dv_profile <= 10) {
1723  ebml_master mapping;
1724  uint8_t buf[ISOM_DVCC_DVVC_SIZE];
1725  uint32_t type;
1726 
1727  uint64_t expected_size = (2 + 1 + (sizeof(DVCC_DVVC_BLOCK_TYPE_NAME) - 1))
1728  + (2 + 1 + 4) + (2 + 1 + ISOM_DVCC_DVVC_SIZE);
1729 
1730  if (dovi->dv_profile > 7) {
1732  } else {
1734  }
1735 
1736  ff_isom_put_dvcc_dvvc(s, buf, dovi);
1737 
1738  mapping = start_ebml_master(pb, MATROSKA_ID_TRACKBLKADDMAPPING, expected_size);
1739 
1742  put_ebml_binary(pb, MATROSKA_ID_BLKADDIDEXTRADATA, buf, sizeof(buf));
1743 
1744  end_ebml_master(pb, mapping);
1745  }
1746 #endif
1747 }
1748 
1750  const AVStream *st, const AVCodecParameters *par,
1751  AVIOContext *pb)
1752 {
1753  const AVDictionaryEntry *tag;
1754  int display_width_div = 1, display_height_div = 1;
1755  uint8_t color_space[4], projection_private[20];
1758  int ret;
1759 
1761 
1764 
1765  mkv_write_field_order(&writer, IS_WEBM(mkv), par->field_order);
1766 
1767  // check both side data and metadata for stereo information,
1768  // write the result to the bitstream if any is found
1769  ret = mkv_write_stereo_mode(s, &writer, st, IS_WEBM(mkv),
1770  &display_width_div,
1771  &display_height_div);
1772  if (ret < 0)
1773  return ret;
1774 
1775  if (par->format == AV_PIX_FMT_YUVA420P ||
1776  ((tag = av_dict_get(st->metadata, "alpha_mode", NULL, 0)) ||
1777  (tag = av_dict_get( s->metadata, "alpha_mode", NULL, 0))) && strtol(tag->value, NULL, 0))
1779 
1780  // write DisplayWidth and DisplayHeight, they contain the size of
1781  // a single source view and/or the display aspect ratio
1782  if (st->sample_aspect_ratio.num) {
1783  int64_t d_width = av_rescale(par->width, st->sample_aspect_ratio.num, st->sample_aspect_ratio.den);
1784  if (d_width > INT_MAX) {
1785  av_log(s, AV_LOG_ERROR, "Overflow in display width\n");
1786  return AVERROR(EINVAL);
1787  }
1788  if (d_width != par->width || display_width_div != 1 || display_height_div != 1) {
1789  if (IS_WEBM(mkv) || display_width_div != 1 || display_height_div != 1) {
1791  d_width / display_width_div);
1793  par->height / display_height_div);
1794  } else {
1795  AVRational display_aspect_ratio;
1796  av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1797  par->width * (int64_t)st->sample_aspect_ratio.num,
1798  par->height * (int64_t)st->sample_aspect_ratio.den,
1799  1024 * 1024);
1801  display_aspect_ratio.num);
1803  display_aspect_ratio.den);
1806  }
1807  }
1808  } else if (display_width_div != 1 || display_height_div != 1) {
1810  par->width / display_width_div);
1812  par->height / display_height_div);
1813  } else if (!IS_WEBM(mkv))
1816 
1817  if (par->codec_id == AV_CODEC_ID_RAWVIDEO) {
1818  AV_WL32(color_space, par->codec_tag);
1820  color_space, sizeof(color_space));
1821  }
1822  mkv_write_video_color(&writer, st, par);
1823  mkv_write_video_projection(s, &writer, st, projection_private);
1824 
1825  return ebml_writer_write(&writer, pb);
1826 }
1827 
1830 {
1832  1000000000LL * duration.num / duration.den);
1833  track->default_duration_low = 1000LL * duration.num / duration.den;
1835  !!(1000LL * duration.num % duration.den);
1836 }
1837 
1839  AVStream *st, mkv_track *track, AVIOContext *pb,
1840  int is_default)
1841 {
1842  AVCodecParameters *par = st->codecpar;
1843  ebml_master subinfo, track_master;
1844  int native_id = 0;
1845  int qt_id = 0;
1846  int bit_depth;
1847  int sample_rate = par->sample_rate;
1848  int output_sample_rate = 0;
1849  int j, ret;
1850  const AVDictionaryEntry *tag;
1851 
1852  if (par->codec_type == AVMEDIA_TYPE_ATTACHMENT)
1853  return 0;
1854 
1855  track_master = start_ebml_master(pb, MATROSKA_ID_TRACKENTRY, 0);
1857  put_ebml_uid (pb, MATROSKA_ID_TRACKUID, track->uid);
1858  put_ebml_uint(pb, MATROSKA_ID_TRACKFLAGLACING, 0); // no lacing (yet)
1859 
1860  if ((tag = av_dict_get(st->metadata, "title", NULL, 0)))
1862  tag = av_dict_get(st->metadata, "language", NULL, 0);
1864  tag && tag->value[0] ? tag->value : "und");
1865 
1866  // The default value for TRACKFLAGDEFAULT is 1, so add element
1867  // if we need to clear it.
1868  if (!is_default)
1870 
1873 
1874  if (IS_WEBM(mkv)) {
1875  const char *codec_id;
1876  if (par->codec_id != AV_CODEC_ID_WEBVTT) {
1877  for (j = 0; ff_webm_codec_tags[j].id != AV_CODEC_ID_NONE; j++) {
1878  if (ff_webm_codec_tags[j].id == par->codec_id) {
1880  native_id = 1;
1881  break;
1882  }
1883  }
1884  } else {
1886  codec_id = "D_WEBVTT/CAPTIONS";
1887  native_id = MATROSKA_TRACK_TYPE_SUBTITLE;
1888  } else if (st->disposition & AV_DISPOSITION_DESCRIPTIONS) {
1889  codec_id = "D_WEBVTT/DESCRIPTIONS";
1890  native_id = MATROSKA_TRACK_TYPE_METADATA;
1891  } else if (st->disposition & AV_DISPOSITION_METADATA) {
1892  codec_id = "D_WEBVTT/METADATA";
1893  native_id = MATROSKA_TRACK_TYPE_METADATA;
1894  } else {
1895  codec_id = "D_WEBVTT/SUBTITLES";
1896  native_id = MATROSKA_TRACK_TYPE_SUBTITLE;
1897  }
1898  }
1899 
1900  if (!native_id) {
1902  "Only VP8 or VP9 or AV1 video and Vorbis or Opus audio and WebVTT subtitles are supported for WebM.\n");
1903  return AVERROR(EINVAL);
1904  }
1905 
1907  } else {
1919 
1920  // look for a codec ID string specific to mkv to use,
1921  // if none are found, use AVI codes
1922  if (par->codec_id == AV_CODEC_ID_FFV1) {
1923  /* FFV1 is actually supported natively in Matroska,
1924  * yet we use the VfW way to mux it for compatibility
1925  * with old demuxers. (FIXME: Are they really important?) */
1926  } else if (par->codec_id != AV_CODEC_ID_RAWVIDEO || par->codec_tag) {
1927  for (j = 0; ff_mkv_codec_tags[j].id != AV_CODEC_ID_NONE; j++) {
1928  if (ff_mkv_codec_tags[j].id == par->codec_id) {
1930  native_id = 1;
1931  break;
1932  }
1933  }
1934  } else {
1935  if (mkv->allow_raw_vfw) {
1936  native_id = 0;
1937  } else {
1938  av_log(s, AV_LOG_ERROR, "Raw RGB is not supported Natively in Matroska, you can use AVI or NUT or\n"
1939  "If you would like to store it anyway using VFW mode, enable allow_raw_vfw (-allow_raw_vfw 1)\n");
1940  return AVERROR(EINVAL);
1941  }
1942  }
1943  }
1944 
1945  switch (par->codec_type) {
1946  AVRational frame_rate;
1947  int audio_frame_samples;
1948 
1949  case AVMEDIA_TYPE_VIDEO:
1950  mkv->have_video = 1;
1952 
1953  frame_rate = (AVRational){ 0, 1 };
1954  if (st->avg_frame_rate.num > 0 && st->avg_frame_rate.den > 0)
1955  frame_rate = st->avg_frame_rate;
1956  else if (st->r_frame_rate.num > 0 && st->r_frame_rate.den > 0)
1957  frame_rate = st->r_frame_rate;
1958 
1959  if (frame_rate.num > 0)
1960  mkv_write_default_duration(track, pb, av_inv_q(frame_rate));
1961 
1962  if (CONFIG_MATROSKA_MUXER && !native_id &&
1965  par->codec_id == AV_CODEC_ID_SVQ1 ||
1966  par->codec_id == AV_CODEC_ID_SVQ3 ||
1967  par->codec_id == AV_CODEC_ID_CINEPAK))
1968  qt_id = 1;
1969 
1970  if (qt_id)
1971  put_ebml_string(pb, MATROSKA_ID_CODECID, "V_QUICKTIME");
1972  else if (!native_id) {
1973  // if there is no mkv-specific codec ID, use VFW mode
1974  put_ebml_string(pb, MATROSKA_ID_CODECID, "V_MS/VFW/FOURCC");
1975  track->write_dts = 1;
1977  }
1978 
1979  ret = mkv_write_track_video(s, mkv, st, par, pb);
1980  if (ret < 0)
1981  return ret;
1982 
1983  break;
1984 
1985  case AVMEDIA_TYPE_AUDIO:
1986  if (par->initial_padding) {
1987  int64_t codecdelay = av_rescale_q(par->initial_padding,
1988  (AVRational){ 1, par->sample_rate },
1989  (AVRational){ 1, 1000000000 });
1990  if (codecdelay < 0) {
1991  av_log(s, AV_LOG_ERROR, "Initial padding is invalid\n");
1992  return AVERROR(EINVAL);
1993  }
1994  put_ebml_uint(pb, MATROSKA_ID_CODECDELAY, codecdelay);
1995 
1996  track->ts_offset = av_rescale_q(par->initial_padding,
1997  (AVRational){ 1, par->sample_rate },
1998  st->time_base);
1999  ffstream(st)->lowest_ts_allowed = -track->ts_offset;
2000  }
2001  if (par->codec_id == AV_CODEC_ID_OPUS)
2003 #if CONFIG_MATROSKA_MUXER
2004  else if (par->codec_id == AV_CODEC_ID_AAC) {
2005  ret = get_aac_sample_rates(s, mkv, par->extradata, par->extradata_size,
2006  &sample_rate, &output_sample_rate);
2007  if (ret < 0)
2008  return ret;
2009  }
2010 #endif
2011 
2013 
2014  audio_frame_samples = av_get_audio_frame_duration2(par, 0);
2015  if (audio_frame_samples)
2016  mkv_write_default_duration(track, pb, (AVRational){ audio_frame_samples,
2017  par->sample_rate });
2018  if (!native_id)
2019  // no mkv-specific ID, use ACM mode
2020  put_ebml_string(pb, MATROSKA_ID_CODECID, "A_MS/ACM");
2021 
2022  subinfo = start_ebml_master(pb, MATROSKA_ID_TRACKAUDIO, 6 + 4 * 9);
2024 
2025  track->sample_rate_offset = avio_tell(pb);
2027  if (output_sample_rate)
2028  put_ebml_float(pb, MATROSKA_ID_AUDIOOUTSAMPLINGFREQ, output_sample_rate);
2029 
2031  if (!bit_depth && par->codec_id != AV_CODEC_ID_ADPCM_G726) {
2032  if (par->bits_per_raw_sample)
2034  else
2036  }
2037  if (!bit_depth)
2039  if (bit_depth)
2041  end_ebml_master(pb, subinfo);
2042  break;
2043 
2044  case AVMEDIA_TYPE_SUBTITLE:
2045  if (!native_id) {
2046  av_log(s, AV_LOG_ERROR, "Subtitle codec %d is not supported.\n", par->codec_id);
2047  return AVERROR(ENOSYS);
2048  }
2049  if (!IS_WEBM(mkv) && st->disposition & AV_DISPOSITION_DESCRIPTIONS)
2051 
2052  if (!IS_WEBM(mkv) || par->codec_id != AV_CODEC_ID_WEBVTT)
2053  native_id = MATROSKA_TRACK_TYPE_SUBTITLE;
2054 
2055  put_ebml_uint(pb, MATROSKA_ID_TRACKTYPE, native_id);
2056  break;
2057  default:
2058  av_log(s, AV_LOG_ERROR, "Only audio, video, and subtitles are supported for Matroska.\n");
2059  return AVERROR(EINVAL);
2060  }
2061 
2062  if (!IS_WEBM(mkv))
2063  mkv_write_blockadditionmapping(s, mkv, par, pb, track, st);
2064 
2065  if (!IS_WEBM(mkv) || par->codec_id != AV_CODEC_ID_WEBVTT) {
2066  uint8_t *codecpriv;
2067  int codecpriv_size, max_payload_size;
2068  track->codecpriv_offset = avio_tell(pb);
2069  ret = mkv_assemble_codecprivate(s, mkv->tmp_bc, par,
2070  par->extradata, par->extradata_size,
2071  native_id, qt_id,
2072  &codecpriv, &codecpriv_size, &max_payload_size);
2073  if (ret < 0)
2074  goto fail;
2075  mkv_put_codecprivate(pb, max_payload_size, codecpriv, codecpriv_size);
2076  track->codecpriv_size = max_payload_size;
2077  }
2078 
2079  end_ebml_master(pb, track_master);
2080  ret = 0;
2081 fail:
2082  ffio_reset_dyn_buf(mkv->tmp_bc);
2083 
2084  return ret;
2085 }
2086 
2088 {
2089  MatroskaMuxContext *mkv = s->priv_data;
2090  AVIOContext *pb = s->pb;
2091  int video_default_idx = -1, audio_default_idx = -1, subtitle_default_idx = -1;
2092  int i, ret;
2093 
2094  if (mkv->nb_attachments == s->nb_streams)
2095  return 0;
2096 
2097  ret = start_ebml_master_crc32(&mkv->track.bc, mkv);
2098  if (ret < 0)
2099  return ret;
2100 
2101  if (mkv->default_mode != DEFAULT_MODE_PASSTHROUGH) {
2102  int video_idx = -1, audio_idx = -1, subtitle_idx = -1;
2103 
2104  for (i = s->nb_streams - 1; i >= 0; i--) {
2105  AVStream *st = s->streams[i];
2106 
2107  switch (st->codecpar->codec_type) {
2108 #define CASE(type, variable) \
2109  case AVMEDIA_TYPE_ ## type: \
2110  variable ## _idx = i; \
2111  if (st->disposition & AV_DISPOSITION_DEFAULT) \
2112  variable ## _default_idx = i; \
2113  break;
2114  CASE(VIDEO, video)
2115  CASE(AUDIO, audio)
2116  CASE(SUBTITLE, subtitle)
2117 #undef CASE
2118  }
2119  }
2120 
2121  video_default_idx = FFMAX(video_default_idx, video_idx);
2122  audio_default_idx = FFMAX(audio_default_idx, audio_idx);
2124  subtitle_default_idx = FFMAX(subtitle_default_idx, subtitle_idx);
2125  }
2126  for (i = 0; i < s->nb_streams; i++) {
2127  AVStream *st = s->streams[i];
2128  int is_default = st->disposition & AV_DISPOSITION_DEFAULT ||
2129  i == video_default_idx || i == audio_default_idx ||
2130  i == subtitle_default_idx;
2131  ret = mkv_write_track(s, mkv, st, &mkv->tracks[i],
2132  mkv->track.bc, is_default);
2133  if (ret < 0)
2134  return ret;
2135  }
2136 
2137  return end_ebml_master_crc32_tentatively(pb, &mkv->track, mkv,
2139 }
2140 
2142 {
2143  EBML_WRITER(4);
2144  uint8_t *key = av_strdup(t->key);
2145  uint8_t *p = key;
2146  const uint8_t *lang = NULL;
2147  int ret;
2148 
2149  if (!key)
2150  return AVERROR(ENOMEM);
2151 
2152  if ((p = strrchr(p, '-')) &&
2153  (lang = ff_convert_lang_to(p + 1, AV_LANG_ISO639_2_BIBL)))
2154  *p = 0;
2155 
2156  p = key;
2157  while (*p) {
2158  if (*p == ' ')
2159  *p = '_';
2160  else if (*p >= 'a' && *p <= 'z')
2161  *p -= 'a' - 'A';
2162  p++;
2163  }
2164 
2167  if (lang)
2170  ret = ebml_writer_write(&writer, pb);
2171 
2172  av_freep(&key);
2173  return ret;
2174 }
2175 
2177  uint32_t elementid, uint64_t uid)
2178 {
2180  4 + 1 + 8);
2181  if (elementid)
2182  put_ebml_uid(pb, elementid, uid);
2183  end_ebml_master(pb, targets);
2184 }
2185 
2186 static int mkv_check_tag_name(const char *name, uint32_t elementid)
2187 {
2188  return av_strcasecmp(name, "title") &&
2189  av_strcasecmp(name, "stereo_mode") &&
2190  av_strcasecmp(name, "creation_time") &&
2191  av_strcasecmp(name, "encoding_tool") &&
2192  av_strcasecmp(name, "duration") &&
2193  (elementid != MATROSKA_ID_TAGTARGETS_TRACKUID ||
2194  av_strcasecmp(name, "language")) &&
2195  (elementid != MATROSKA_ID_TAGTARGETS_ATTACHUID ||
2196  (av_strcasecmp(name, "filename") &&
2197  av_strcasecmp(name, "mimetype")));
2198 }
2199 
2201  AVIOContext **pb, unsigned reserved_size,
2202  uint32_t elementid, uint64_t uid)
2203 {
2204  const AVDictionaryEntry *t = NULL;
2205  AVIOContext *const tmp_bc = mkv->tmp_bc;
2206  uint8_t *buf;
2207  int ret = 0, size, tag_written = 0;
2208 
2209  mkv_write_tag_targets(mkv, tmp_bc, elementid, uid);
2210 
2211  while ((t = av_dict_iterate(m, t))) {
2212  if (mkv_check_tag_name(t->key, elementid)) {
2213  ret = mkv_write_simpletag(tmp_bc, t);
2214  if (ret < 0)
2215  goto end;
2216  tag_written = 1;
2217  }
2218  }
2219  if (reserved_size)
2220  put_ebml_void(tmp_bc, reserved_size);
2221  else if (!tag_written)
2222  goto end;
2223 
2224  size = avio_get_dyn_buf(tmp_bc, &buf);
2225  if (tmp_bc->error) {
2226  ret = tmp_bc->error;
2227  goto end;
2228  }
2229  if (!*pb) {
2230  ret = start_ebml_master_crc32(pb, mkv);
2231  if (ret < 0)
2232  goto end;
2233  }
2234  put_ebml_binary(*pb, MATROSKA_ID_TAG, buf, size);
2235 
2236 end:
2237  ffio_reset_dyn_buf(tmp_bc);
2238  return ret;
2239 }
2240 
2242 {
2243  MatroskaMuxContext *mkv = s->priv_data;
2244  int i, ret, seekable = IS_SEEKABLE(s->pb, mkv);
2245 
2246  mkv->wrote_tags = 1;
2247 
2249 
2250  ret = mkv_write_tag(mkv, s->metadata, &mkv->tags.bc, 0, 0, 0);
2251  if (ret < 0)
2252  return ret;
2253 
2254  for (i = 0; i < s->nb_streams; i++) {
2255  const AVStream *st = s->streams[i];
2256  mkv_track *track = &mkv->tracks[i];
2257 
2259  continue;
2260 
2261  ret = mkv_write_tag(mkv, st->metadata, &mkv->tags.bc,
2262  seekable ? DURATION_SIMPLETAG_SIZE : 0,
2264  if (ret < 0)
2265  return ret;
2266  if (seekable)
2268  }
2269 
2270  if (mkv->nb_attachments && !IS_WEBM(mkv)) {
2271  for (i = 0; i < s->nb_streams; i++) {
2272  const mkv_track *track = &mkv->tracks[i];
2273  const AVStream *st = s->streams[i];
2274 
2276  continue;
2277 
2278  ret = mkv_write_tag(mkv, st->metadata, &mkv->tags.bc, 0,
2280  if (ret < 0)
2281  return ret;
2282  }
2283  }
2284 
2285  if (mkv->tags.bc) {
2286  return end_ebml_master_crc32_tentatively(s->pb, &mkv->tags, mkv,
2288  }
2289  return 0;
2290 }
2291 
2293 {
2294  for (unsigned i = 0; i < s->nb_chapters; i++) {
2295  if (!s->chapters[i]->id)
2296  return 1;
2297  for (unsigned j = 0; j < i; j++)
2298  if (s->chapters[j]->id == s->chapters[i]->id)
2299  return 1;
2300  }
2301  return 0;
2302 }
2303 
2305 {
2306  MatroskaMuxContext *mkv = s->priv_data;
2307  AVIOContext *dyn_cp = NULL, *dyn_tags = NULL, **tags, *pb = s->pb;
2308  ebml_master editionentry;
2309  AVRational scale = {1, 1E9};
2310  int ret, create_new_ids;
2311 
2312  if (!s->nb_chapters || mkv->wrote_chapters)
2313  return 0;
2314 
2315  ret = start_ebml_master_crc32(&dyn_cp, mkv);
2316  if (ret < 0)
2317  return ret;
2318 
2319  editionentry = start_ebml_master(dyn_cp, MATROSKA_ID_EDITIONENTRY, 0);
2320  if (!IS_WEBM(mkv)) {
2322  /* If mkv_write_tags() has already been called, then any tags
2323  * corresponding to chapters will be put into a new Tags element. */
2324  tags = mkv->wrote_tags ? &dyn_tags : &mkv->tags.bc;
2325  } else
2326  tags = NULL;
2327 
2328  create_new_ids = mkv_new_chapter_ids_needed(s);
2329 
2330  for (unsigned i = 0; i < s->nb_chapters; i++) {
2331  AVChapter *const c = s->chapters[i];
2332  int64_t chapterstart = av_rescale_q(c->start, c->time_base, scale);
2333  int64_t chapterend = av_rescale_q(c->end, c->time_base, scale);
2334  const AVDictionaryEntry *t;
2335  uint64_t uid = create_new_ids ? i + 1ULL : c->id;
2336  EBML_WRITER(7);
2337 
2338  if (chapterstart < 0 || chapterstart > chapterend || chapterend < 0) {
2340  "Invalid chapter start (%"PRId64") or end (%"PRId64").\n",
2341  chapterstart, chapterend);
2343  goto fail;
2344  }
2345 
2348  ebml_writer_add_uint(&writer, MATROSKA_ID_CHAPTERTIMESTART, chapterstart);
2349  ebml_writer_add_uint(&writer, MATROSKA_ID_CHAPTERTIMEEND, chapterend);
2350  if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
2353  ebml_writer_add_string(&writer, MATROSKA_ID_CHAPLANG , "und");
2354  }
2355  ret = ebml_writer_write(&writer, dyn_cp);
2356  if (ret < 0)
2357  goto fail;
2358 
2359  if (tags) {
2361 
2362  ret = mkv_write_tag(mkv, c->metadata, tags, 0,
2364  if (ret < 0)
2365  goto fail;
2366  }
2367  }
2368  end_ebml_master(dyn_cp, editionentry);
2369  mkv->wrote_chapters = 1;
2370 
2371  ret = end_ebml_master_crc32(pb, &dyn_cp, mkv, MATROSKA_ID_CHAPTERS, 0, 0, 1);
2372  if (ret < 0)
2373  goto fail;
2374  if (dyn_tags)
2375  return end_ebml_master_crc32(pb, &dyn_tags, mkv,
2376  MATROSKA_ID_TAGS, 0, 0, 1);
2377  return 0;
2378 
2379 fail:
2380  if (tags) {
2381  /* tags == &mkv->tags.bc can only happen if mkv->tags.bc was
2382  * initially NULL, so we never free older tags. */
2383  ffio_free_dyn_buf(tags);
2384  }
2385  ffio_free_dyn_buf(&dyn_cp);
2386  return ret;
2387 }
2388 
2389 static const char *get_mimetype(const AVStream *st)
2390 {
2391  const AVDictionaryEntry *t;
2392 
2393  if (t = av_dict_get(st->metadata, "mimetype", NULL, 0))
2394  return t->value;
2395  if (st->codecpar->codec_id != AV_CODEC_ID_NONE) {
2397  if (desc && desc->mime_types) {
2398  return desc->mime_types[0];
2399  } else if (st->codecpar->codec_id == AV_CODEC_ID_TEXT)
2400  return "text/plain";
2401  }
2402 
2403  return NULL;
2404 }
2405 
2407 {
2408  MatroskaMuxContext *mkv = s->priv_data;
2409  AVIOContext *dyn_cp = NULL, *pb = s->pb;
2410  int i, ret;
2411 
2412  if (!mkv->nb_attachments)
2413  return 0;
2414 
2415  ret = start_ebml_master_crc32(&dyn_cp, mkv);
2416  if (ret < 0)
2417  return ret;
2418 
2419  for (i = 0; i < s->nb_streams; i++) {
2420  const AVStream *st = s->streams[i];
2421  mkv_track *track = &mkv->tracks[i];
2422  EBML_WRITER(6);
2423  const AVDictionaryEntry *t;
2424  const char *mimetype;
2425 
2427  continue;
2428 
2430 
2431  if (t = av_dict_get(st->metadata, "title", NULL, 0))
2433  if (!(t = av_dict_get(st->metadata, "filename", NULL, 0))) {
2434  av_log(s, AV_LOG_ERROR, "Attachment stream %d has no filename tag.\n", i);
2435  ffio_free_dyn_buf(&dyn_cp);
2436  return AVERROR(EINVAL);
2437  }
2439 
2440  mimetype = get_mimetype(st);
2441  av_assert0(mimetype);
2445  ebml_writer_add_uid(&writer, MATROSKA_ID_FILEUID, track->uid);
2446  ret = ebml_writer_write(&writer, dyn_cp);
2447  if (ret < 0) {
2448  ffio_free_dyn_buf(&dyn_cp);
2449  return ret;
2450  }
2451  }
2452  return end_ebml_master_crc32(pb, &dyn_cp, mkv,
2453  MATROSKA_ID_ATTACHMENTS, 0, 0, 1);
2454 }
2455 
2457 {
2458  const AVDictionaryEntry *duration = av_dict_get(s->metadata, "DURATION",
2459  NULL, 0);
2460  int64_t max = 0;
2461  int64_t us;
2462 
2463  if (duration && (av_parse_time(&us, duration->value, 1) == 0) && us > 0) {
2464  av_log(s, AV_LOG_DEBUG, "get_metadata_duration found duration in context metadata: %" PRId64 "\n", us);
2465  return us;
2466  }
2467 
2468  for (unsigned i = 0; i < s->nb_streams; i++) {
2469  int64_t us;
2470  duration = av_dict_get(s->streams[i]->metadata, "DURATION", NULL, 0);
2471 
2472  if (duration && (av_parse_time(&us, duration->value, 1) == 0))
2473  max = FFMAX(max, us);
2474  }
2475 
2476  av_log(s, AV_LOG_DEBUG, "get_metadata_duration returned: %" PRId64 "\n", max);
2477  return max;
2478 }
2479 
2481  const char *doctype, int version)
2482 {
2483  EBML_WRITER(8);
2489  ebml_writer_add_string(&writer, EBML_ID_DOCTYPE, doctype);
2492  /* The size is bounded, so no need to check this. */
2493  ebml_writer_write(&writer, pb);
2494 }
2495 
2497 {
2498  MatroskaMuxContext *mkv = s->priv_data;
2499  const AVDictionaryEntry *tag;
2500  int64_t creation_time;
2501  AVIOContext *pb;
2502  int ret = start_ebml_master_crc32(&mkv->info.bc, mkv);
2503  if (ret < 0)
2504  return ret;
2505  pb = mkv->info.bc;
2506 
2508  if ((tag = av_dict_get(s->metadata, "title", NULL, 0)))
2509  put_ebml_string(pb, MATROSKA_ID_TITLE, tag->value);
2510  if (!(s->flags & AVFMT_FLAG_BITEXACT)) {
2512  if ((tag = av_dict_get(s->metadata, "encoding_tool", NULL, 0)))
2514  else
2516 
2517  if (!IS_WEBM(mkv))
2519  } else {
2520  const char *ident = "Lavf";
2523  }
2524 
2525  if (ff_parse_creation_time_metadata(s, &creation_time, 0) > 0) {
2526  // Adjust time so it's relative to 2001-01-01 and convert to nanoseconds.
2527  int64_t date_utc = (creation_time - 978307200000000LL) * 1000;
2528  uint8_t date_utc_buf[8];
2529  AV_WB64(date_utc_buf, date_utc);
2530  put_ebml_binary(pb, MATROSKA_ID_DATEUTC, date_utc_buf, 8);
2531  }
2532 
2533  // reserve space for the duration
2534  mkv->duration = 0;
2535  mkv->duration_offset = avio_tell(pb);
2536  if (!mkv->is_live) {
2537  int64_t metadata_duration = get_metadata_duration(s);
2538 
2539  if (s->duration > 0) {
2540  int64_t scaledDuration = av_rescale(s->duration, 1000, AV_TIME_BASE);
2541  put_ebml_float(pb, MATROSKA_ID_DURATION, scaledDuration);
2542  av_log(s, AV_LOG_DEBUG, "Write early duration from recording time = %" PRIu64 "\n", scaledDuration);
2543  } else if (metadata_duration > 0) {
2544  int64_t scaledDuration = av_rescale(metadata_duration, 1000, AV_TIME_BASE);
2545  put_ebml_float(pb, MATROSKA_ID_DURATION, scaledDuration);
2546  av_log(s, AV_LOG_DEBUG, "Write early duration from metadata = %" PRIu64 "\n", scaledDuration);
2547  } else if (s->pb->seekable & AVIO_SEEKABLE_NORMAL) {
2548  put_ebml_void(pb, 11); // assumes double-precision float to be written
2549  }
2550  }
2551  return end_ebml_master_crc32_tentatively(s->pb, &mkv->info,
2552  mkv, MATROSKA_ID_INFO);
2553 }
2554 
2556 {
2557  MatroskaMuxContext *mkv = s->priv_data;
2558  AVIOContext *pb = s->pb;
2559  int ret, version = 2;
2560 
2561  ret = avio_open_dyn_buf(&mkv->tmp_bc);
2562  if (ret < 0)
2563  return ret;
2564 
2565  if (!IS_WEBM(mkv) ||
2566  av_dict_get(s->metadata, "stereo_mode", NULL, 0) ||
2567  av_dict_get(s->metadata, "alpha_mode", NULL, 0))
2568  version = 4;
2569 
2570  for (unsigned i = 0; i < s->nb_streams; i++) {
2571  if (s->streams[i]->codecpar->codec_id == AV_CODEC_ID_OPUS ||
2572  av_dict_get(s->streams[i]->metadata, "stereo_mode", NULL, 0) ||
2573  av_dict_get(s->streams[i]->metadata, "alpha_mode", NULL, 0))
2574  version = 4;
2575  }
2576 
2577  ebml_write_header(pb, s->oformat->name, version);
2579  put_ebml_size_unknown(pb, 8);
2580  mkv->segment_offset = avio_tell(pb);
2581 
2582  // We write a SeekHead at the beginning to point to all other level
2583  // one elements (except Clusters).
2584  mkv_start_seekhead(mkv, pb);
2585 
2586  ret = mkv_write_info(s);
2587  if (ret < 0)
2588  return ret;
2589 
2590  ret = mkv_write_tracks(s);
2591  if (ret < 0)
2592  return ret;
2593 
2595  if (ret < 0)
2596  return ret;
2597 
2598  if (!IS_WEBM(mkv)) {
2600  if (ret < 0)
2601  return ret;
2602  }
2603 
2604  /* Must come after mkv_write_chapters() to write chapter tags
2605  * into the same Tags element as the other tags. */
2606  ret = mkv_write_tags(s);
2607  if (ret < 0)
2608  return ret;
2609 
2610  if (!IS_SEEKABLE(pb, mkv)) {
2611  ret = mkv_write_seekhead(pb, mkv, 0, avio_tell(pb));
2612  if (ret < 0)
2613  return ret;
2614  }
2615 
2616  if (s->metadata_header_padding > 0) {
2617  if (s->metadata_header_padding == 1)
2618  s->metadata_header_padding++;
2619  put_ebml_void(pb, s->metadata_header_padding);
2620  }
2621 
2622  if (mkv->reserve_cues_space || mkv->move_cues_to_front) {
2623  if (IS_SEEKABLE(pb, mkv)) {
2624  mkv->cues_pos = avio_tell(pb);
2625  if (mkv->reserve_cues_space >= 1) {
2626  if (mkv->reserve_cues_space == 1)
2627  mkv->reserve_cues_space++;
2629  }
2630  } else
2631  mkv->reserve_cues_space = -1;
2632  }
2633 
2634  mkv->cluster_pos = -1;
2635 
2636  // start a new cluster every 5 MB or 5 sec, or 32k / 1 sec for streaming or
2637  // after 4k and on a keyframe
2638  if (IS_SEEKABLE(pb, mkv)) {
2639  if (mkv->cluster_time_limit < 0)
2640  mkv->cluster_time_limit = 5000;
2641  if (mkv->cluster_size_limit < 0)
2642  mkv->cluster_size_limit = 5 * 1024 * 1024;
2643  } else {
2644  if (mkv->cluster_time_limit < 0)
2645  mkv->cluster_time_limit = 1000;
2646  if (mkv->cluster_size_limit < 0)
2647  mkv->cluster_size_limit = 32 * 1024;
2648  }
2649 
2650  return 0;
2651 }
2652 
2653 #if CONFIG_MATROSKA_MUXER
2654 static int mkv_reformat_h2645(MatroskaMuxContext *mkv, AVIOContext *pb,
2655  const AVPacket *pkt, int *size)
2656 {
2657  int ret;
2658  if (pb) {
2660  } else {
2662  if (ret < 0)
2663  return ret;
2664  *size = ret;
2665  }
2666  return 0;
2667 }
2668 
2669 static int mkv_reformat_wavpack(MatroskaMuxContext *mkv, AVIOContext *pb,
2670  const AVPacket *pkt, int *size)
2671 {
2672  const uint8_t *src = pkt->data;
2673  int srclen = pkt->size;
2674  int offset = 0;
2675  int ret;
2676 
2677  while (srclen >= WV_HEADER_SIZE) {
2678  WvHeader header;
2679 
2681  if (ret < 0)
2682  return ret;
2683  src += WV_HEADER_SIZE;
2684  srclen -= WV_HEADER_SIZE;
2685 
2686  if (srclen < header.blocksize)
2687  return AVERROR_INVALIDDATA;
2688 
2689  offset += 4 * !!header.initial + 8 + 4 * !(header.initial && header.final);
2690  if (pb) {
2691  if (header.initial)
2692  avio_wl32(pb, header.samples);
2693  avio_wl32(pb, header.flags);
2694  avio_wl32(pb, header.crc);
2695 
2696  if (!(header.initial && header.final))
2697  avio_wl32(pb, header.blocksize);
2698 
2699  avio_write(pb, src, header.blocksize);
2700  }
2701  src += header.blocksize;
2702  srclen -= header.blocksize;
2703  offset += header.blocksize;
2704  }
2705  *size = offset;
2706 
2707  return 0;
2708 }
2709 #endif
2710 
2712  const AVPacket *pkt, int *size)
2713 {
2714  int ret = ff_av1_filter_obus(pb, pkt->data, pkt->size);
2715  if (ret < 0)
2716  return ret;
2717  *size = ret;
2718  return 0;
2719 }
2720 
2722  const AVPacket *pkt, int *size)
2723 {
2724  const uint8_t *id, *settings;
2725  size_t id_size, settings_size;
2726  unsigned total = pkt->size + 2U;
2727 
2728  if (total > INT_MAX)
2729  return AVERROR(ERANGE);
2730 
2732  &id_size);
2734  &settings_size);
2735  if (id_size > INT_MAX - total || settings_size > INT_MAX - (total += id_size))
2736  return AVERROR(ERANGE);
2737  *size = total += settings_size;
2738  if (pb) {
2739  avio_write(pb, id, id_size);
2740  avio_w8(pb, '\n');
2741  avio_write(pb, settings, settings_size);
2742  avio_w8(pb, '\n');
2743  avio_write(pb, pkt->data, pkt->size);
2744  }
2745  return 0;
2746 }
2747 
2748 static void mkv_write_blockadditional(EbmlWriter *writer, const uint8_t *buf,
2749  size_t size, uint64_t additional_id)
2750 {
2752  ebml_writer_add_uint(writer, MATROSKA_ID_BLOCKADDID, additional_id);
2754  ebml_writer_close_master(writer);
2755 }
2756 
2757 static int mkv_write_block(void *logctx, MatroskaMuxContext *mkv,
2758  AVIOContext *pb, const AVCodecParameters *par,
2759  mkv_track *track, const AVPacket *pkt,
2760  int keyframe, int64_t ts, uint64_t duration,
2761  int force_blockgroup, int64_t relative_packet_pos)
2762 {
2763  uint8_t t35_buf[6 + AV_HDR_PLUS_MAX_PAYLOAD_SIZE];
2764  uint8_t *side_data;
2765  size_t side_data_size;
2766  uint64_t additional_id;
2767  unsigned track_number = track->track_num;
2768  EBML_WRITER(12);
2769  int ret;
2770 
2771  mkv->cur_block.track = track;
2772  mkv->cur_block.pkt = pkt;
2773  mkv->cur_block.rel_ts = ts - mkv->cluster_pts;
2774  mkv->cur_block.flags = 0;
2775 
2776  /* Open a BlockGroup with a Block now; it will later be converted
2777  * to a SimpleBlock if possible. */
2779  ebml_writer_add_block(&writer, mkv);
2780 
2781  if (duration > 0 && (par->codec_type == AVMEDIA_TYPE_SUBTITLE ||
2782  /* If the packet's duration is inconsistent with the default duration,
2783  * add an explicit duration element. */
2784  track->default_duration_high > 0 &&
2785  duration != track->default_duration_high &&
2786  duration != track->default_duration_low))
2788 
2789  av_log(logctx, AV_LOG_DEBUG,
2790  "Writing block of size %d with pts %" PRId64 ", dts %" PRId64 ", "
2791  "duration %" PRId64 " at relative offset %" PRId64 " in cluster "
2792  "at offset %" PRId64 ". TrackNumber %u, keyframe %d\n",
2793  pkt->size, pkt->pts, pkt->dts, pkt->duration, relative_packet_pos,
2794  mkv->cluster_pos, track_number, keyframe != 0);
2795 
2796  side_data = av_packet_get_side_data(pkt,
2798  &side_data_size);
2799  if (side_data && side_data_size >= 10) {
2800  int64_t discard_padding = AV_RL32(side_data + 4);
2801  if (discard_padding) {
2802  discard_padding = av_rescale_q(discard_padding,
2803  (AVRational){1, par->sample_rate},
2804  (AVRational){1, 1000000000});
2805  ebml_writer_add_sint(&writer, MATROSKA_ID_DISCARDPADDING, discard_padding);
2806  }
2807  }
2808 
2810  side_data = av_packet_get_side_data(pkt,
2812  &side_data_size);
2813  if (side_data && side_data_size >= 8 &&
2814  // Only the Codec-specific BlockMore (id == 1) is currently supported.
2815  (additional_id = AV_RB64(side_data)) == MATROSKA_BLOCK_ADD_ID_TYPE_OPAQUE) {
2816  mkv_write_blockadditional(&writer, side_data + 8, side_data_size - 8,
2817  additional_id);
2818  track->max_blockaddid = FFMAX(track->max_blockaddid, additional_id);
2819  }
2820 
2821  if (par->codec_type == AVMEDIA_TYPE_VIDEO) {
2822  side_data = av_packet_get_side_data(pkt,
2824  &side_data_size);
2825  if (side_data && side_data_size) {
2826  uint8_t *payload = t35_buf;
2827  size_t payload_size = sizeof(t35_buf) - 6;
2828 
2829  bytestream_put_byte(&payload, ITU_T_T35_COUNTRY_CODE_US);
2830  bytestream_put_be16(&payload, ITU_T_T35_PROVIDER_CODE_SMTPE);
2831  bytestream_put_be16(&payload, 0x01); // provider_oriented_code
2832  bytestream_put_byte(&payload, 0x04); // application_identifier
2833 
2834  ret = av_dynamic_hdr_plus_to_t35((AVDynamicHDRPlus *)side_data, &payload,
2835  &payload_size);
2836  if (ret < 0)
2837  return ret;
2838 
2839  mkv_write_blockadditional(&writer, t35_buf, payload_size + 6,
2841  track->max_blockaddid = FFMAX(track->max_blockaddid,
2843  }
2844  }
2845 
2847 
2848  if (!force_blockgroup && writer.nb_elements == 2) {
2849  /* Nothing except the BlockGroup + Block. Can use a SimpleBlock. */
2850  writer.elements++; // Skip the BlockGroup.
2851  writer.nb_elements--;
2852  av_assert2(writer.elements[0].id == MATROSKA_ID_BLOCK);
2853  writer.elements[0].id = MATROSKA_ID_SIMPLEBLOCK;
2854  if (keyframe)
2855  mkv->cur_block.flags |= 1 << 7;
2856  } else if (!keyframe)
2858  track->last_timestamp - ts);
2859 
2860  return ebml_writer_write(&writer, pb);
2861 }
2862 
2864 {
2865  MatroskaMuxContext *mkv = s->priv_data;
2866  int ret;
2867 
2868  if (!mkv->have_video) {
2869  for (unsigned i = 0; i < s->nb_streams; i++)
2870  mkv->tracks[i].has_cue = 0;
2871  }
2872  mkv->cluster_pos = -1;
2873  ret = end_ebml_master_crc32(s->pb, &mkv->cluster_bc, mkv,
2874  MATROSKA_ID_CLUSTER, 0, 1, 0);
2875  if (ret < 0)
2876  return ret;
2877 
2879  return 0;
2880 }
2881 
2883 {
2884  MatroskaMuxContext *mkv = s->priv_data;
2885  mkv_track *track = &mkv->tracks[pkt->stream_index];
2886  AVCodecParameters *par = s->streams[pkt->stream_index]->codecpar;
2887  uint8_t *side_data;
2888  size_t side_data_size;
2889  int ret;
2890 
2892  &side_data_size);
2893 
2894  switch (par->codec_id) {
2895 #if CONFIG_MATROSKA_MUXER
2896  case AV_CODEC_ID_AAC:
2897  if (side_data_size && mkv->track.bc) {
2898  int output_sample_rate = 0;
2899  ret = get_aac_sample_rates(s, mkv, side_data, side_data_size,
2900  &track->sample_rate, &output_sample_rate);
2901  if (ret < 0)
2902  return ret;
2903  if (!output_sample_rate)
2904  output_sample_rate = track->sample_rate; // Space is already reserved, so it's this or a void element.
2905  ret = mkv_update_codecprivate(s, mkv, side_data, side_data_size,
2906  par, mkv->track.bc, track, 0);
2907  if (ret < 0)
2908  return ret;
2909  avio_seek(mkv->track.bc, track->sample_rate_offset, SEEK_SET);
2911  put_ebml_float(mkv->track.bc, MATROSKA_ID_AUDIOOUTSAMPLINGFREQ, output_sample_rate);
2912  } else if (!par->extradata_size && !track->sample_rate) {
2913  // No extradata (codecpar or packet side data).
2914  av_log(s, AV_LOG_ERROR, "Error parsing AAC extradata, unable to determine samplerate.\n");
2915  return AVERROR(EINVAL);
2916  }
2917  break;
2918  case AV_CODEC_ID_FLAC:
2919  if (side_data_size && mkv->track.bc) {
2920  if (side_data_size != par->extradata_size) {
2921  av_log(s, AV_LOG_ERROR, "Invalid FLAC STREAMINFO metadata for output stream %d\n",
2922  pkt->stream_index);
2923  return AVERROR(EINVAL);
2924  }
2925  ret = mkv_update_codecprivate(s, mkv, side_data, side_data_size,
2926  par, mkv->track.bc, track, 0);
2927  if (ret < 0)
2928  return ret;
2929  }
2930  break;
2931 #endif
2932  // FIXME: Remove the following once libaom starts propagating proper extradata during init()
2933  // See https://bugs.chromium.org/p/aomedia/issues/detail?id=2208
2934  case AV_CODEC_ID_AV1:
2935  if (side_data_size && mkv->track.bc && !par->extradata_size) {
2936  // If the reserved space doesn't suffice, only write
2937  // the first four bytes of the av1C.
2938  ret = mkv_update_codecprivate(s, mkv, side_data, side_data_size,
2939  par, mkv->track.bc, track, 4);
2940  if (ret < 0)
2941  return ret;
2942  } else if (!par->extradata_size)
2943  return AVERROR_INVALIDDATA;
2944  break;
2945  default:
2946  if (side_data_size)
2947  av_log(s, AV_LOG_DEBUG, "Ignoring new extradata in a packet for stream %d.\n", pkt->stream_index);
2948  break;
2949  }
2950 
2951  return 0;
2952 }
2953 
2955 {
2956  MatroskaMuxContext *mkv = s->priv_data;
2957  AVIOContext *pb;
2958  AVCodecParameters *par = s->streams[pkt->stream_index]->codecpar;
2959  mkv_track *track = &mkv->tracks[pkt->stream_index];
2960  int is_sub = par->codec_type == AVMEDIA_TYPE_SUBTITLE;
2961  /* All subtitle blocks are considered to be keyframes. */
2962  int keyframe = is_sub || !!(pkt->flags & AV_PKT_FLAG_KEY);
2963  int64_t duration = FFMAX(pkt->duration, 0);
2964  int64_t cue_duration = is_sub ? duration : 0;
2965  int ret;
2966  int64_t ts = track->write_dts ? pkt->dts : pkt->pts;
2967  int64_t relative_packet_pos;
2968 
2969  if (ts == AV_NOPTS_VALUE) {
2970  av_log(s, AV_LOG_ERROR, "Can't write packet with unknown timestamp\n");
2971  return AVERROR(EINVAL);
2972  }
2973  ts += track->ts_offset;
2974 
2975  if (mkv->cluster_pos != -1) {
2976  int64_t cluster_time = ts - mkv->cluster_pts;
2977  if ((int16_t)cluster_time != cluster_time) {
2978  ret = mkv_end_cluster(s);
2979  if (ret < 0)
2980  return ret;
2981  av_log(s, AV_LOG_WARNING, "Starting new cluster due to timestamp\n");
2982  }
2983  }
2984 
2985  if (mkv->cluster_pos == -1) {
2986  ret = start_ebml_master_crc32(&mkv->cluster_bc, mkv);
2987  if (ret < 0)
2988  return ret;
2989  mkv->cluster_bc->direct = 1;
2990  mkv->cluster_pos = avio_tell(s->pb);
2992  mkv->cluster_pts = FFMAX(0, ts);
2994  "Starting new cluster with timestamp "
2995  "%" PRId64 " at offset %" PRId64 " bytes\n",
2996  mkv->cluster_pts, mkv->cluster_pos);
2997  }
2998  pb = mkv->cluster_bc;
2999 
3000  relative_packet_pos = avio_tell(pb);
3001 
3002  /* The WebM spec requires WebVTT to be muxed in BlockGroups;
3003  * so we force it even for packets without duration. */
3004  ret = mkv_write_block(s, mkv, pb, par, track, pkt,
3005  keyframe, ts, duration,
3006  par->codec_id == AV_CODEC_ID_WEBVTT,
3007  relative_packet_pos);
3008  if (ret < 0)
3009  return ret;
3010  if (keyframe && IS_SEEKABLE(s->pb, mkv) &&
3011  (par->codec_type == AVMEDIA_TYPE_VIDEO ||
3013  !mkv->have_video && !track->has_cue)) {
3014  ret = mkv_add_cuepoint(mkv, pkt->stream_index, ts,
3015  mkv->cluster_pos, relative_packet_pos,
3016  cue_duration);
3017  if (ret < 0)
3018  return ret;
3019  track->has_cue = 1;
3020  }
3021 
3022  track->last_timestamp = ts;
3023  mkv->duration = FFMAX(mkv->duration, ts + duration);
3024  track->duration = FFMAX(track->duration, ts + duration);
3025 
3026  return 0;
3027 }
3028 
3030 {
3031  MatroskaMuxContext *mkv = s->priv_data;
3032  int codec_type = s->streams[pkt->stream_index]->codecpar->codec_type;
3033  int keyframe = !!(pkt->flags & AV_PKT_FLAG_KEY);
3034  int cluster_size;
3035  int64_t cluster_time;
3036  int ret;
3037  int start_new_cluster;
3038 
3040  if (ret < 0)
3041  return ret;
3042 
3043  if (mkv->cluster_pos != -1) {
3044  if (mkv->tracks[pkt->stream_index].write_dts)
3045  cluster_time = pkt->dts - mkv->cluster_pts;
3046  else
3047  cluster_time = pkt->pts - mkv->cluster_pts;
3048  cluster_time += mkv->tracks[pkt->stream_index].ts_offset;
3049 
3050  cluster_size = avio_tell(mkv->cluster_bc);
3051 
3052  if (mkv->is_dash && codec_type == AVMEDIA_TYPE_VIDEO) {
3053  // WebM DASH specification states that the first block of
3054  // every Cluster has to be a key frame. So for DASH video,
3055  // we only create a Cluster on seeing key frames.
3056  start_new_cluster = keyframe;
3057  } else if (mkv->is_dash && codec_type == AVMEDIA_TYPE_AUDIO &&
3058  cluster_time > mkv->cluster_time_limit) {
3059  // For DASH audio, we create a Cluster based on cluster_time_limit.
3060  start_new_cluster = 1;
3061  } else if (!mkv->is_dash &&
3062  (cluster_size > mkv->cluster_size_limit ||
3063  cluster_time > mkv->cluster_time_limit ||
3064  (codec_type == AVMEDIA_TYPE_VIDEO && keyframe &&
3065  cluster_size > 4 * 1024))) {
3066  start_new_cluster = 1;
3067  } else
3068  start_new_cluster = 0;
3069 
3070  if (start_new_cluster) {
3071  ret = mkv_end_cluster(s);
3072  if (ret < 0)
3073  return ret;
3074  }
3075  }
3076 
3077  if (mkv->cluster_pos == -1)
3078  avio_write_marker(s->pb,
3079  av_rescale_q(pkt->dts, s->streams[pkt->stream_index]->time_base, AV_TIME_BASE_Q),
3081 
3082  // check if we have an audio packet cached
3083  if (mkv->cur_audio_pkt->size > 0) {
3086  if (ret < 0) {
3088  "Could not write cached audio packet ret:%d\n", ret);
3089  return ret;
3090  }
3091  }
3092 
3093  // buffer an audio packet to ensure the packet containing the video
3094  // keyframe's timecode is contained in the same cluster for WebM
3095  if (codec_type == AVMEDIA_TYPE_AUDIO) {
3096  if (pkt->size > 0)
3098  } else
3100  return ret;
3101 }
3102 
3104 {
3105  MatroskaMuxContext *mkv = s->priv_data;
3106 
3107  if (!pkt) {
3108  if (mkv->cluster_pos != -1) {
3109  int ret = mkv_end_cluster(s);
3110  if (ret < 0)
3111  return ret;
3113  "Flushing cluster at offset %" PRIu64 " bytes\n",
3114  avio_tell(s->pb));
3115  }
3116  return 1;
3117  }
3118  return mkv_write_packet(s, pkt);
3119 }
3120 
3122 {
3123  MatroskaMuxContext *mkv = s->priv_data;
3124  AVIOContext *pb = s->pb;
3125  int64_t endpos, ret64;
3126  int ret, ret2 = 0;
3127 
3128  // check if we have an audio packet cached
3129  if (mkv->cur_audio_pkt->size > 0) {
3131  if (ret < 0) {
3133  "Could not write cached audio packet ret:%d\n", ret);
3134  return ret;
3135  }
3136  }
3137 
3138  if (mkv->cluster_pos != -1) {
3139  ret = end_ebml_master_crc32(pb, &mkv->cluster_bc, mkv,
3140  MATROSKA_ID_CLUSTER, 0, 0, 0);
3141  if (ret < 0)
3142  return ret;
3143  }
3144 
3146  if (ret < 0)
3147  return ret;
3148 
3149  if (!IS_SEEKABLE(pb, mkv))
3150  return 0;
3151 
3152  endpos = avio_tell(pb);
3153 
3154  if (mkv->cues.num_entries && mkv->reserve_cues_space >= 0) {
3155  AVIOContext *cues = NULL;
3156  uint64_t size, offset = 0;
3157  int length_size = 0;
3158 
3159 redo_cues:
3160  ret = start_ebml_master_crc32(&cues, mkv);
3161  if (ret < 0)
3162  return ret;
3163 
3164  ret = mkv_assemble_cues(s->streams, cues, mkv->tmp_bc, &mkv->cues,
3165  mkv->tracks, s->nb_streams, offset);
3166  if (ret < 0) {
3167  ffio_free_dyn_buf(&cues);
3168  return ret;
3169  }
3170 
3171  if (mkv->reserve_cues_space || mkv->move_cues_to_front) {
3172  size = avio_tell(cues);
3173  length_size = ebml_length_size(size);
3174  size += 4 + length_size;
3175  if (offset + mkv->reserve_cues_space < size) {
3176  if (mkv->move_cues_to_front) {
3177  offset = size - mkv->reserve_cues_space;
3178  ffio_reset_dyn_buf(cues);
3179  goto redo_cues;
3180  }
3182  "Insufficient space reserved for Cues: "
3183  "%d < %"PRIu64". No Cues will be output.\n",
3184  mkv->reserve_cues_space, size);
3185  ret2 = AVERROR(EINVAL);
3186  goto after_cues;
3187  } else {
3188  if (offset) {
3190  offset);
3191  if (ret < 0) {
3192  ffio_free_dyn_buf(&cues);
3193  return ret;
3194  }
3195  endpos += offset;
3196  }
3197  if ((ret64 = avio_seek(pb, mkv->cues_pos, SEEK_SET)) < 0) {
3198  ffio_free_dyn_buf(&cues);
3199  return ret64;
3200  }
3201  if (mkv->reserve_cues_space == size + 1) {
3202  /* There is no way to reserve a single byte because
3203  * the minimal size of an EBML Void element is 2
3204  * (1 byte ID, 1 byte length field). This problem
3205  * is solved by writing the Cues' length field on
3206  * one byte more than necessary. */
3207  length_size++;
3208  size++;
3209  }
3210  }
3211  }
3212  ret = end_ebml_master_crc32(pb, &cues, mkv, MATROSKA_ID_CUES,
3213  length_size, 0, 1);
3214  if (ret < 0)
3215  return ret;
3216  if (mkv->reserve_cues_space) {
3217  if (size < mkv->reserve_cues_space)
3219  } else if (!mkv->move_cues_to_front)
3220  endpos = avio_tell(pb);
3221  }
3222 
3223 after_cues:
3224  /* Lengths greater than (1ULL << 56) - 1 can't be represented
3225  * via an EBML number, so leave the unknown length field. */
3226  if (endpos - mkv->segment_offset < (1ULL << 56) - 1) {
3227  if ((ret64 = avio_seek(pb, mkv->segment_offset - 8, SEEK_SET)) < 0)
3228  return ret64;
3229  put_ebml_length(pb, endpos - mkv->segment_offset, 8);
3230  }
3231 
3232  ret = mkv_write_seekhead(pb, mkv, 1, mkv->info.pos);
3233  if (ret < 0)
3234  return ret;
3235 
3236  if (mkv->info.bc) {
3237  // update the duration
3238  av_log(s, AV_LOG_DEBUG, "end duration = %" PRIu64 "\n", mkv->duration);
3239  avio_seek(mkv->info.bc, mkv->duration_offset, SEEK_SET);
3241  ret = end_ebml_master_crc32(pb, &mkv->info.bc, mkv,
3242  MATROSKA_ID_INFO, 0, 0, 0);
3243  if (ret < 0)
3244  return ret;
3245  }
3246 
3247  if (mkv->track.bc) {
3248  // write Tracks master
3249  if (!IS_WEBM(mkv)) {
3250  AVIOContext *track_bc = mkv->track.bc;
3251 
3252  for (unsigned i = 0; i < s->nb_streams; i++) {
3253  const mkv_track *track = &mkv->tracks[i];
3254 
3255  if (!track->max_blockaddid)
3256  continue;
3257 
3258  // We reserved a single byte to write this value.
3259  av_assert0(track->max_blockaddid <= 0xFF);
3260 
3261  avio_seek(track_bc, track->blockadditionmapping_offset, SEEK_SET);
3262 
3264  track->max_blockaddid);
3266  ebml_master mapping_master = start_ebml_master(track_bc, MATROSKA_ID_TRACKBLKADDMAPPING, 8);
3271  end_ebml_master(track_bc, mapping_master);
3272  }
3273  }
3274  }
3275 
3276  avio_seek(pb, mkv->track.pos, SEEK_SET);
3277  ret = end_ebml_master_crc32(pb, &mkv->track.bc, mkv,
3278  MATROSKA_ID_TRACKS, 0, 0, 0);
3279  if (ret < 0)
3280  return ret;
3281  }
3282 
3283  // update stream durations
3284  if (mkv->tags.bc) {
3285  AVIOContext *tags_bc = mkv->tags.bc;
3286  int i;
3287  for (i = 0; i < s->nb_streams; ++i) {
3288  const AVStream *st = s->streams[i];
3289  const mkv_track *track = &mkv->tracks[i];
3290 
3291  if (track->duration_offset > 0) {
3292  double duration_sec = track->duration * av_q2d(st->time_base);
3293  char duration_string[DURATION_STRING_LENGTH + 1] = "";
3294  ebml_master simpletag;
3295 
3296  av_log(s, AV_LOG_DEBUG, "stream %d end duration = %" PRIu64 "\n", i,
3297  track->duration);
3298 
3299  avio_seek(tags_bc, track->duration_offset, SEEK_SET);
3300  simpletag = start_ebml_master(tags_bc, MATROSKA_ID_SIMPLETAG,
3301  2 + 1 + 8 + 23);
3302  put_ebml_string(tags_bc, MATROSKA_ID_TAGNAME, "DURATION");
3303 
3304  snprintf(duration_string, sizeof(duration_string), "%02d:%02d:%012.9f",
3305  (int) duration_sec / 3600, ((int) duration_sec / 60) % 60,
3306  fmod(duration_sec, 60));
3307 
3309  duration_string, DURATION_STRING_LENGTH);
3310  end_ebml_master(tags_bc, simpletag);
3311  }
3312  }
3313 
3314  avio_seek(pb, mkv->tags.pos, SEEK_SET);
3315  ret = end_ebml_master_crc32(pb, &mkv->tags.bc, mkv,
3316  MATROSKA_ID_TAGS, 0, 0, 0);
3317  if (ret < 0)
3318  return ret;
3319  }
3320 
3321  avio_seek(pb, endpos, SEEK_SET);
3322 
3323  return ret2;
3324 }
3325 
3326 static uint64_t mkv_get_uid(const mkv_track *tracks, int i, AVLFG *c)
3327 {
3328  while (1) {
3329  uint64_t uid;
3330  int k;
3331  uid = (uint64_t)av_lfg_get(c) << 32;
3332  uid |= av_lfg_get(c);
3333  if (!uid)
3334  continue;
3335  for (k = 0; k < i; k++) {
3336  if (tracks[k].uid == uid)
3337  break;
3338  }
3339  if (k == i)
3340  return uid;
3341  }
3342 }
3343 
3344 static int mkv_init(struct AVFormatContext *s)
3345 {
3346  FFFormatContext *const si = ffformatcontext(s);
3347  MatroskaMuxContext *mkv = s->priv_data;
3348  AVLFG c;
3349  unsigned nb_tracks = 0;
3350  int i;
3351 
3352  mkv->ctx = s;
3353 
3354  for (i = 0; i < s->nb_streams; i++) {
3355  if (s->streams[i]->codecpar->codec_id == AV_CODEC_ID_ATRAC3 ||
3356  s->streams[i]->codecpar->codec_id == AV_CODEC_ID_COOK ||
3357  s->streams[i]->codecpar->codec_id == AV_CODEC_ID_RA_288 ||
3358  s->streams[i]->codecpar->codec_id == AV_CODEC_ID_SIPR ||
3359  s->streams[i]->codecpar->codec_id == AV_CODEC_ID_RV10 ||
3360  s->streams[i]->codecpar->codec_id == AV_CODEC_ID_RV20 ||
3361  s->streams[i]->codecpar->codec_id == AV_CODEC_ID_RV30) {
3363  "The Matroska muxer does not yet support muxing %s\n",
3364  avcodec_get_name(s->streams[i]->codecpar->codec_id));
3365  return AVERROR_PATCHWELCOME;
3366  }
3367  }
3368 
3369  if (s->avoid_negative_ts < 0) {
3370  s->avoid_negative_ts = 1;
3371  si->avoid_negative_ts_use_pts = 1;
3372  }
3373 
3374  if (!CONFIG_MATROSKA_MUXER ||
3375  (CONFIG_WEBM_MUXER && !strcmp(s->oformat->name, "webm"))) {
3376  mkv->mode = MODE_WEBM;
3377  mkv->write_crc = 0;
3378  } else
3379  mkv->mode = MODE_MATROSKAv2;
3380 
3382 
3383  mkv->tracks = av_calloc(s->nb_streams, sizeof(*mkv->tracks));
3384  if (!mkv->tracks)
3385  return AVERROR(ENOMEM);
3386 
3387  if (!(s->flags & AVFMT_FLAG_BITEXACT)) {
3389 
3390  // Calculate the SegmentUID now in order not to waste our random seed.
3391  for (i = 0; i < 4; i++)
3392  mkv->segment_uid[i] = av_lfg_get(&c);
3393  }
3394 
3395  for (i = 0; i < s->nb_streams; i++) {
3396  AVStream *st = s->streams[i];
3397  const AVCodecParameters *const par = st->codecpar;
3398  mkv_track *track = &mkv->tracks[i];
3399 
3400  switch (par->codec_id) {
3401 #if CONFIG_MATROSKA_MUXER
3402  case AV_CODEC_ID_WAVPACK:
3403  track->reformat = mkv_reformat_wavpack;
3404  break;
3405  case AV_CODEC_ID_H264:
3406  case AV_CODEC_ID_HEVC:
3407  if ((par->codec_id == AV_CODEC_ID_H264 && par->extradata_size > 0 ||
3408  par->codec_id == AV_CODEC_ID_HEVC && par->extradata_size > 6) &&
3409  (AV_RB24(par->extradata) == 1 || AV_RB32(par->extradata) == 1))
3410  track->reformat = mkv_reformat_h2645;
3411  break;
3412  case AV_CODEC_ID_PRORES:
3413  /* Matroska specification requires to remove
3414  * the first QuickTime atom. */
3415  track->offset = 8;
3416  break;
3417 #endif
3418  case AV_CODEC_ID_AV1:
3419  track->reformat = mkv_reformat_av1;
3420  break;
3421  case AV_CODEC_ID_WEBVTT:
3422  track->reformat = webm_reformat_vtt;
3423  break;
3424  }
3425 
3426  if (s->flags & AVFMT_FLAG_BITEXACT) {
3427  track->uid = i + 1;
3428  } else {
3429  track->uid = mkv_get_uid(mkv->tracks, i, &c);
3430  }
3431 
3432  // ms precision is the de-facto standard timescale for mkv files
3433  avpriv_set_pts_info(st, 64, 1, 1000);
3434 
3436  if (IS_WEBM(mkv)) {
3437  av_log(s, AV_LOG_WARNING, "Stream %d will be ignored "
3438  "as WebM doesn't support attachments.\n", i);
3439  } else if (!get_mimetype(st)) {
3440  av_log(s, AV_LOG_ERROR, "Attachment stream %d has no mimetype "
3441  "tag and it cannot be deduced from the codec id.\n", i);
3442  return AVERROR(EINVAL);
3443  }
3444  mkv->nb_attachments++;
3445  continue;
3446  }
3447 
3448  nb_tracks++;
3449  track->track_num = mkv->is_dash ? mkv->dash_track_number : nb_tracks;
3450  track->track_num_size = ebml_num_size(track->track_num);
3451  }
3452 
3453  if (mkv->is_dash && nb_tracks != 1)
3454  return AVERROR(EINVAL);
3455 
3456  return 0;
3457 }
3458 
3460  const AVPacket *pkt)
3461 {
3462  int ret = 1;
3463 
3464  if (CONFIG_MATROSKA_MUXER && st->codecpar->codec_id == AV_CODEC_ID_AAC) {
3465  if (pkt->size > 2 && (AV_RB16(pkt->data) & 0xfff0) == 0xfff0)
3466  ret = ff_stream_add_bitstream_filter(st, "aac_adtstoasc", NULL);
3467  } else if (st->codecpar->codec_id == AV_CODEC_ID_VP9) {
3468  ret = ff_stream_add_bitstream_filter(st, "vp9_superframe", NULL);
3469  } else if (CONFIG_MATROSKA_MUXER &&
3471  ret = ff_stream_add_bitstream_filter(st, "pgs_frame_merge", NULL);
3472  }
3473 
3474  return ret;
3475 }
3476 
3478  { AV_CODEC_ID_ALAC, 0XFFFFFFFF },
3479  { AV_CODEC_ID_ATRAC1, 0xFFFFFFFF },
3480  { AV_CODEC_ID_MLP, 0xFFFFFFFF },
3481  { AV_CODEC_ID_OPUS, 0xFFFFFFFF },
3482  { AV_CODEC_ID_PCM_S16BE, 0xFFFFFFFF },
3483  { AV_CODEC_ID_PCM_S24BE, 0xFFFFFFFF },
3484  { AV_CODEC_ID_PCM_S32BE, 0xFFFFFFFF },
3485  { AV_CODEC_ID_QDMC, 0xFFFFFFFF },
3486  { AV_CODEC_ID_QDM2, 0xFFFFFFFF },
3487  { AV_CODEC_ID_RA_144, 0xFFFFFFFF },
3488  { AV_CODEC_ID_TRUEHD, 0xFFFFFFFF },
3489  { AV_CODEC_ID_NONE, 0xFFFFFFFF }
3490 };
3491 
3493  { AV_CODEC_ID_DVB_SUBTITLE, 0xFFFFFFFF },
3494  { AV_CODEC_ID_DVD_SUBTITLE, 0xFFFFFFFF },
3495  { AV_CODEC_ID_HDMV_PGS_SUBTITLE, 0xFFFFFFFF },
3496  { AV_CODEC_ID_ARIB_CAPTION, 0xFFFFFFFF },
3497  { AV_CODEC_ID_NONE, 0xFFFFFFFF }
3498 };
3499 
3500 #define OFFSET(x) offsetof(MatroskaMuxContext, x)
3501 #define FLAGS AV_OPT_FLAG_ENCODING_PARAM
3502 static const AVOption options[] = {
3503  { "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 },
3504  { "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 },
3505  { "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 },
3506  { "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 },
3507  { "dash", "create a WebM file conforming to WebM DASH specification", OFFSET(is_dash), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
3508  { "dash_track_number", "track number for the DASH stream", OFFSET(dash_track_number), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, INT_MAX, FLAGS },
3509  { "live", "write files assuming it is a live stream", OFFSET(is_live), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
3510  { "allow_raw_vfw", "allow raw VFW mode", OFFSET(allow_raw_vfw), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
3511  { "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 },
3512  { "write_crc32", "write a CRC32 element inside every Level 1 element", OFFSET(write_crc), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, FLAGS },
3513  { "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" },
3514  { "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" },
3515  { "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" },
3516  { "passthrough", "use the disposition flag as-is", 0, AV_OPT_TYPE_CONST, { .i64 = DEFAULT_MODE_PASSTHROUGH }, 0, 0, FLAGS, .unit = "default_mode" },
3517  { NULL },
3518 };
3519 
3521  .class_name = "matroska/webm muxer",
3522  .item_name = av_default_item_name,
3523  .option = options,
3524  .version = LIBAVUTIL_VERSION_INT,
3525 };
3526 
3527 #if CONFIG_MATROSKA_MUXER
3528 static int mkv_query_codec(enum AVCodecID codec_id, int std_compliance)
3529 {
3530  for (int i = 0; ff_mkv_codec_tags[i].id != AV_CODEC_ID_NONE; i++)
3531  if (ff_mkv_codec_tags[i].id == codec_id)
3532  return 1;
3533 
3534  if (std_compliance < FF_COMPLIANCE_NORMAL) {
3536  // mkv theoretically supports any video/audio through VFW/ACM
3538  return 1;
3539  }
3540 
3541  return 0;
3542 }
3543 
3545  .p.name = "matroska",
3546  .p.long_name = NULL_IF_CONFIG_SMALL("Matroska"),
3547  .p.mime_type = "video/x-matroska",
3548  .p.extensions = "mkv",
3549  .priv_data_size = sizeof(MatroskaMuxContext),
3550  .p.audio_codec = CONFIG_LIBVORBIS_ENCODER ?
3552  .p.video_codec = CONFIG_LIBX264_ENCODER ?
3554  .init = mkv_init,
3555  .deinit = mkv_deinit,
3556  .write_header = mkv_write_header,
3557  .write_packet = mkv_write_flush_packet,
3558  .write_trailer = mkv_write_trailer,
3559  .p.flags = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS |
3561  AVFMT_TS_NONSTRICT | AVFMT_ALLOW_FLUSH,
3562 #else
3564 #endif
3565  .p.codec_tag = (const AVCodecTag* const []){
3568  },
3569  .p.subtitle_codec = AV_CODEC_ID_ASS,
3570  .query_codec = mkv_query_codec,
3571  .check_bitstream = mkv_check_bitstream,
3572  .p.priv_class = &matroska_webm_class,
3573  .flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH,
3574 };
3575 #endif
3576 
3577 #if CONFIG_WEBM_MUXER
3578 static int webm_query_codec(enum AVCodecID codec_id, int std_compliance)
3579 {
3580  for (int i = 0; ff_webm_codec_tags[i].id != AV_CODEC_ID_NONE; i++)
3581  if (ff_webm_codec_tags[i].id == codec_id)
3582  return 1;
3583 
3584  return 0;
3585 }
3586 
3587 const FFOutputFormat ff_webm_muxer = {
3588  .p.name = "webm",
3589  .p.long_name = NULL_IF_CONFIG_SMALL("WebM"),
3590  .p.mime_type = "video/webm",
3591  .p.extensions = "webm",
3592  .priv_data_size = sizeof(MatroskaMuxContext),
3593  .p.audio_codec = CONFIG_LIBOPUS_ENCODER ? AV_CODEC_ID_OPUS : AV_CODEC_ID_VORBIS,
3594  .p.video_codec = CONFIG_LIBVPX_VP9_ENCODER? AV_CODEC_ID_VP9 : AV_CODEC_ID_VP8,
3595  .p.subtitle_codec = AV_CODEC_ID_WEBVTT,
3596  .init = mkv_init,
3597  .deinit = mkv_deinit,
3598  .write_header = mkv_write_header,
3599  .write_packet = mkv_write_flush_packet,
3600  .write_trailer = mkv_write_trailer,
3601  .query_codec = webm_query_codec,
3602  .check_bitstream = mkv_check_bitstream,
3603  .p.flags = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS |
3605  AVFMT_TS_NONSTRICT | AVFMT_ALLOW_FLUSH,
3606 #else
3608 #endif
3609  .p.priv_class = &matroska_webm_class,
3610  .flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH,
3611 };
3612 #endif
3613 
3614 #if CONFIG_MATROSKA_AUDIO_MUXER
3616  .p.name = "matroska",
3617  .p.long_name = NULL_IF_CONFIG_SMALL("Matroska Audio"),
3618  .p.mime_type = "audio/x-matroska",
3619  .p.extensions = "mka",
3620  .priv_data_size = sizeof(MatroskaMuxContext),
3621  .p.audio_codec = CONFIG_LIBVORBIS_ENCODER ?
3623  .p.video_codec = AV_CODEC_ID_NONE,
3624  .init = mkv_init,
3625  .deinit = mkv_deinit,
3626  .write_header = mkv_write_header,
3627  .write_packet = mkv_write_flush_packet,
3628  .write_trailer = mkv_write_trailer,
3629  .check_bitstream = mkv_check_bitstream,
3631  .p.flags = AVFMT_GLOBALHEADER | AVFMT_TS_NONSTRICT |
3632  AVFMT_ALLOW_FLUSH,
3633 #else
3635 #endif
3636  .p.codec_tag = (const AVCodecTag* const []){
3638  },
3639  .p.priv_class = &matroska_webm_class,
3640  .flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH,
3641 };
3642 #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:109
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:3344
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:3029
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:2954
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:424
hevc.h
AVSphericalMapping::bound_bottom
uint32_t bound_bottom
Distance from the bottom edge.
Definition: spherical.h:166
MATROSKA_ID_VIDEOCOLORMASTERINGMETA
#define MATROSKA_ID_VIDEOCOLORMASTERINGMETA
Definition: matroska.h:149
ebml_writer_master_len
static int ebml_writer_master_len(EbmlWriter *writer, EbmlElement *elem, int remaining_elems)
Definition: matroskaenc.c:596
AVFieldOrder
AVFieldOrder
Definition: defs.h:198
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:1470
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:2292
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:3759
AV_FIELD_PROGRESSIVE
@ AV_FIELD_PROGRESSIVE
Definition: defs.h:200
MATROSKA_ID_VIDEOPIXELWIDTH
#define MATROSKA_ID_VIDEOPIXELWIDTH
Definition: matroska.h:119
EbmlMaster::containing_master
int containing_master
-1 if no parent exists
Definition: matroskaenc.c: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:223
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
AVChannelLayout::u
union AVChannelLayout::@381 u
Details about which channels are present in this layout.
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:375
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:524
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
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:2406
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:542
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:1331
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
AV_CODEC_ID_FLAC
@ AV_CODEC_ID_FLAC
Definition: codec_id.h:452
AVChannelLayout::order
enum AVChannelOrder order
Channel order used in this layout.
Definition: channel_layout.h:308
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
MATROSKA_ID_CUETIME
#define MATROSKA_ID_CUETIME
Definition: matroska.h:202
IS_WEBM
#define IS_WEBM(mkv)
Definition: matroskaenc.c:84
AV_PROFILE_ARIB_PROFILE_C
#define AV_PROFILE_ARIB_PROFILE_C
Definition: defs.h:187
AVChannelLayout::mask
uint64_t mask
This member must be used for AV_CHANNEL_ORDER_NATIVE, and may be used for AV_CHANNEL_ORDER_AMBISONIC ...
Definition: channel_layout.h:335
WvHeader
Definition: wv.h:34
MATROSKA_ID_CUERELATIVEPOSITION
#define MATROSKA_ID_CUERELATIVEPOSITION
Definition: matroska.h:208
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:313
MAX_PCE_SIZE
#define MAX_PCE_SIZE
Maximum size of a PCE including the 3-bit ID_PCE.
Definition: mpeg4audio.h:117
ff_flac_is_native_layout
int ff_flac_is_native_layout(uint64_t channel_layout)
Definition: flacenc_header.c:50
AV_CODEC_ID_HDMV_PGS_SUBTITLE
@ AV_CODEC_ID_HDMV_PGS_SUBTITLE
Definition: codec_id.h: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:579
sample_rate
sample_rate
Definition: ffmpeg_filter.c:424
AV_WB64
#define AV_WB64(p, v)
Definition: intreadwrite.h:431
FFOutputFormat::p
AVOutputFormat p
The public AVOutputFormat.
Definition: mux.h:65
MatroskaMuxContext::cues_pos
int64_t cues_pos
Definition: matroskaenc.c:229
AV_FIELD_BT
@ AV_FIELD_BT
Bottom coded first, top displayed first.
Definition: defs.h:204
av_get_random_seed
uint32_t av_get_random_seed(void)
Get a seed to use in conjunction with random functions.
Definition: random_seed.c:167
xiph.h
MatroskaMuxContext::cluster_size_limit
int cluster_size_limit
Definition: matroskaenc.c: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:284
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:186
HDIV1
#define HDIV1(STEREOMODETYPE, STEREO3DTYPE, FLAGS, WDIV, HDIV, WEBM)
start_ebml_master
static ebml_master start_ebml_master(AVIOContext *pb, uint32_t elementid, uint64_t expectedsize)
Definition: matroskaenc.c:430
DEFAULT_MODE_INFER
@ DEFAULT_MODE_INFER
Definition: matroskaenc.c:90
AV_FIELD_TT
@ AV_FIELD_TT
Top coded_first, top displayed first.
Definition: defs.h:201
DVCC_DVVC_BLOCK_TYPE_NAME
#define DVCC_DVVC_BLOCK_TYPE_NAME
Definition: matroska.h:439
AV_CODEC_ID_ASS
@ AV_CODEC_ID_ASS
Definition: codec_id.h: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:182
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
DEFAULT_MODE_INFER_NO_SUBS
@ DEFAULT_MODE_INFER_NO_SUBS
Definition: matroskaenc.c:91
MATROSKA_ID_VIDEOCOLOR_WHITEX
#define MATROSKA_ID_VIDEOCOLOR_WHITEX
Definition: matroska.h:156
AVRational::num
int num
Numerator.
Definition: rational.h:59
AV_FIELD_TB
@ AV_FIELD_TB
Top coded first, bottom displayed first.
Definition: defs.h:203
MATROSKA_TRACK_TYPE_METADATA
@ MATROSKA_TRACK_TYPE_METADATA
Definition: matroska.h:287
AV_CODEC_ID_ATRAC3
@ AV_CODEC_ID_ATRAC3
Definition: codec_id.h:471
mkv_track::max_blockaddid
uint64_t max_blockaddid
Definition: matroskaenc.c: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:203
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:2087
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:3502
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:199
EBML_ID_EBMLMAXIDLENGTH
#define EBML_ID_EBMLMAXIDLENGTH
Definition: matroska.h:37
av_dict_get
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:62
avio_open_dyn_buf
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c: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:1294
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:1593
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:3103
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:2389
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:3520
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:3477
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:376
ebml_writer_close_master
static void ebml_writer_close_master(EbmlWriter *writer)
Definition: matroskaenc.c:469
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:115
av_rescale_q
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
sint_size
static av_const int sint_size(int64_t val)
Definition: matroskaenc.c: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:387
MATROSKA_ID_BLKADDIDVALUE
#define MATROSKA_ID_BLKADDIDVALUE
Definition: matroska.h:193
AV_CODEC_ID_SVQ3
@ AV_CODEC_ID_SVQ3
Definition: codec_id.h:75
EbmlType
EbmlType
Definition: matroskaenc.c: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:1414
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:3326
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:2456
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:236
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:229
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:417
ff_wv_parse_header
int ff_wv_parse_header(WvHeader *wv, const uint8_t *data)
Parse a WavPack block header.
Definition: wv.c:30
mkv_put_codecprivate
static void mkv_put_codecprivate(AVIOContext *pb, unsigned max_payload_size, const uint8_t *codecpriv, unsigned codecpriv_size)
Definition: matroskaenc.c:1265
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:2555
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:525
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:146
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:1553
AV_PKT_DATA_DYNAMIC_HDR10_PLUS
@ AV_PKT_DATA_DYNAMIC_HDR10_PLUS
HDR10+ dynamic metadata associated with a video frame.
Definition: packet.h:300
EBML_FLOAT
@ EBML_FLOAT
Definition: matroskaenc.c: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
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:1415
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
AVCodecParameters::profile
int profile
Codec-specific bitstream restrictions that the stream conforms to.
Definition: codec_par.h:128
AV_CODEC_ID_OPUS
@ AV_CODEC_ID_OPUS
Definition: codec_id.h:500
AVCHROMA_LOC_UNSPECIFIED
@ AVCHROMA_LOC_UNSPECIFIED
Definition: pixfmt.h:706