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