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.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 "riff.h"
38 #include "vorbiscomment.h"
39 #include "wv.h"
40 
41 #include "libavutil/avstring.h"
43 #include "libavutil/crc.h"
44 #include "libavutil/dict.h"
45 #include "libavutil/intfloat.h"
46 #include "libavutil/intreadwrite.h"
47 #include "libavutil/lfg.h"
49 #include "libavutil/mathematics.h"
50 #include "libavutil/opt.h"
51 #include "libavutil/parseutils.h"
52 #include "libavutil/random_seed.h"
53 #include "libavutil/rational.h"
54 #include "libavutil/samplefmt.h"
55 #include "libavutil/stereo3d.h"
56 
57 #include "libavcodec/xiph.h"
58 #include "libavcodec/mpeg4audio.h"
59 
60 /* Level 1 elements we create a SeekHead entry for:
61  * Info, Tracks, Chapters, Attachments, Tags (potentially twice) and Cues */
62 #define MAX_SEEKHEAD_ENTRIES 7
63 
64 /* Largest known-length EBML length */
65 #define MAX_EBML_LENGTH ((1ULL << 56) - 2)
66 /* The dynamic buffer API we rely upon has a limit of INT_MAX;
67  * and so has avio_write(). */
68 #define MAX_SUPPORTED_EBML_LENGTH FFMIN(MAX_EBML_LENGTH, INT_MAX)
69 
70 #define MODE_MATROSKAv2 0x01
71 #define MODE_WEBM 0x02
72 
73 #define IS_WEBM(mkv) (CONFIG_WEBM_MUXER && CONFIG_MATROSKA_MUXER ? \
74  ((mkv)->mode == MODE_WEBM) : CONFIG_WEBM_MUXER)
75 #define IS_SEEKABLE(pb, mkv) (((pb)->seekable & AVIO_SEEKABLE_NORMAL) && \
76  !(mkv)->is_live)
77 
78 enum {
82 };
83 
84 typedef struct ebml_master {
85  int64_t pos; ///< absolute offset in the containing AVIOContext where the master's elements start
86  int sizebytes; ///< how many bytes were reserved for the size
87 } ebml_master;
88 
89 typedef struct ebml_stored_master {
91  int64_t pos;
93 
94 typedef enum EbmlType {
102  EBML_BLOCK, ///< pseudo-type for writing (Simple)Blocks
104 } EbmlType;
105 
106 typedef struct BlockContext {
107  struct mkv_track *track;
108  const AVPacket *pkt;
109  int16_t rel_ts;
110  uint8_t flags;
112 } BlockContext;
113 
114 typedef struct EbmlMaster {
115  int nb_elements; ///< -1 if not finished
116  int containing_master; ///< -1 if no parent exists
117 } EbmlMaster;
118 
119 typedef struct EbmlElement {
120  uint32_t id;
122  unsigned length_size;
123  uint64_t size; ///< excluding id and length field
124  union {
125  uint64_t uint;
126  int64_t sint;
127  double f;
128  const char *str;
129  const uint8_t *bin;
130  struct MatroskaMuxContext *mkv; ///< used by EBML_BLOCK
132  } priv;
133 } EbmlElement;
134 
135 typedef struct EbmlWriter {
136  unsigned nb_elements;
139 } EbmlWriter;
140 
141 #define EBML_WRITER(max_nb_elems) \
142  EbmlElement elements[max_nb_elems]; \
143  EbmlWriter writer = (EbmlWriter){ .elements = elements, \
144  .current_master_element = -1 }
145 
146 typedef struct mkv_seekhead_entry {
147  uint32_t elementid;
148  uint64_t segmentpos;
150 
151 typedef struct mkv_seekhead {
152  int64_t filepos;
156 } mkv_seekhead;
157 
158 typedef struct mkv_cuepoint {
159  uint64_t pts;
161  int64_t cluster_pos; ///< offset of the cluster containing the block relative to the segment
162  int64_t relative_pos; ///< relative offset from the position of the cluster containing the block
163  int64_t duration; ///< duration of the block according to time base
164 } mkv_cuepoint;
165 
166 typedef struct mkv_cues {
169 } mkv_cues;
170 
171 struct MatroskaMuxContext;
172 
173 typedef struct mkv_track {
175  int has_cue;
176  uint64_t uid;
177  unsigned track_num;
180  unsigned offset;
182  int64_t last_timestamp;
183  int64_t duration;
186  int64_t ts_offset;
187  /* This callback will be called twice: First with a NULL AVIOContext
188  * to return the size of the (Simple)Block's data via size
189  * and a second time with the AVIOContext set when the data
190  * shall be written.
191  * The callback shall not return an error on the second call. */
193  const AVPacket *, int *size);
194 } mkv_track;
195 
196 typedef struct MatroskaMuxContext {
197  const AVClass *class;
199 
200  int mode;
204  int64_t segment_offset;
206  int64_t cluster_pos; ///< file offset of the current Cluster
207  int64_t cluster_pts;
209  int64_t duration;
213  int64_t cues_pos;
214 
216 
218 
219  unsigned nb_attachments;
221 
224 
229  int is_live;
230 
231  int is_dash;
237 
238  uint32_t segment_uid[4];
240 
241 /** 2 bytes * 3 for EBML IDs, 3 1-byte EBML lengths, 8 bytes for 64 bit
242  * offset, 4 bytes for target EBML ID */
243 #define MAX_SEEKENTRY_SIZE 21
244 
245 /** 4 * (1-byte EBML ID, 1-byte EBML size, 8-byte uint max) */
246 #define MAX_CUETRACKPOS_SIZE 40
247 
248 /** Seek preroll value for opus */
249 #define OPUS_SEEK_PREROLL 80000000
250 
251 static int ebml_id_size(uint32_t id)
252 {
253  return (av_log2(id) + 7U) / 8;
254 }
255 
256 static void put_ebml_id(AVIOContext *pb, uint32_t id)
257 {
258  int i = ebml_id_size(id);
259  while (i--)
260  avio_w8(pb, (uint8_t)(id >> (i * 8)));
261 }
262 
263 /**
264  * Write an EBML size meaning "unknown size".
265  *
266  * @param bytes The number of bytes the size should occupy (maximum: 8).
267  */
268 static void put_ebml_size_unknown(AVIOContext *pb, int bytes)
269 {
270  av_assert0(bytes <= 8);
271  avio_w8(pb, 0x1ff >> bytes);
272  if (av_builtin_constant_p(bytes) && bytes == 1)
273  return;
274  ffio_fill(pb, 0xff, bytes - 1);
275 }
276 
277 /**
278  * Returns how many bytes are needed to represent a number
279  * as EBML variable length integer.
280  */
281 static int ebml_num_size(uint64_t num)
282 {
283  int bytes = 0;
284  do {
285  bytes++;
286  } while (num >>= 7);
287  return bytes;
288 }
289 
290 /**
291  * Calculate how many bytes are needed to represent the length field
292  * of an EBML element whose payload has a given length.
293  */
294 static int ebml_length_size(uint64_t length)
295 {
296  return ebml_num_size(length + 1);
297 }
298 
299 /**
300  * Write a number as EBML variable length integer on `bytes` bytes.
301  * `bytes` is taken literally without checking.
302  */
303 static void put_ebml_num(AVIOContext *pb, uint64_t num, int bytes)
304 {
305  num |= 1ULL << bytes * 7;
306  for (int i = bytes - 1; i >= 0; i--)
307  avio_w8(pb, (uint8_t)(num >> i * 8));
308 }
309 
310 /**
311  * Write a length as EBML variable length integer.
312  *
313  * @param bytes The number of bytes that need to be used to write the number.
314  * If zero, the minimal number of bytes will be used.
315  */
316 static void put_ebml_length(AVIOContext *pb, uint64_t length, int bytes)
317 {
318  int needed_bytes = ebml_length_size(length);
319 
320  // sizes larger than this are currently undefined in EBML
321  av_assert0(length < (1ULL << 56) - 1);
322 
323  if (bytes == 0)
324  bytes = needed_bytes;
325  // The bytes needed to write the given size must not exceed
326  // the bytes that we ought to use.
327  av_assert0(bytes >= needed_bytes);
328  put_ebml_num(pb, length, bytes);
329 }
330 
331 /**
332  * Write a (random) UID with fixed size to make the output more deterministic
333  */
334 static void put_ebml_uid(AVIOContext *pb, uint32_t elementid, uint64_t uid)
335 {
336  put_ebml_id(pb, elementid);
337  put_ebml_length(pb, 8, 0);
338  avio_wb64(pb, uid);
339 }
340 
341 static void put_ebml_uint(AVIOContext *pb, uint32_t elementid, uint64_t val)
342 {
343  int i, bytes = 1;
344  uint64_t tmp = val;
345  while (tmp >>= 8)
346  bytes++;
347 
348  put_ebml_id(pb, elementid);
349  put_ebml_length(pb, bytes, 0);
350  for (i = bytes - 1; i >= 0; i--)
351  avio_w8(pb, (uint8_t)(val >> i * 8));
352 }
353 
354 static void put_ebml_float(AVIOContext *pb, uint32_t elementid, double val)
355 {
356  put_ebml_id(pb, elementid);
357  put_ebml_length(pb, 8, 0);
359 }
360 
361 static void put_ebml_binary(AVIOContext *pb, uint32_t elementid,
362  const void *buf, int size)
363 {
364  put_ebml_id(pb, elementid);
365  put_ebml_length(pb, size, 0);
366  avio_write(pb, buf, size);
367 }
368 
369 static void put_ebml_string(AVIOContext *pb, uint32_t elementid,
370  const char *str)
371 {
372  put_ebml_binary(pb, elementid, str, strlen(str));
373 }
374 
375 /**
376  * Write a void element of a given size. Useful for reserving space in
377  * the file to be written to later.
378  *
379  * @param size The number of bytes to reserve, which must be at least 2.
380  */
381 static void put_ebml_void(AVIOContext *pb, int size)
382 {
383  av_assert0(size >= 2);
384 
386  // we need to subtract the length needed to store the size from the
387  // size we need to reserve so 2 cases, we use 8 bytes to store the
388  // size if possible, 1 byte otherwise
389  if (size < 10) {
390  size -= 2;
391  put_ebml_length(pb, size, 0);
392  } else {
393  size -= 9;
394  put_ebml_length(pb, size, 8);
395  }
396  ffio_fill(pb, 0, size);
397 }
398 
399 static ebml_master start_ebml_master(AVIOContext *pb, uint32_t elementid,
400  uint64_t expectedsize)
401 {
402  int bytes = expectedsize ? ebml_length_size(expectedsize) : 8;
403 
404  put_ebml_id(pb, elementid);
405  put_ebml_size_unknown(pb, bytes);
406  return (ebml_master) { avio_tell(pb), bytes };
407 }
408 
410 {
411  int64_t pos = avio_tell(pb);
412 
413  if (avio_seek(pb, master.pos - master.sizebytes, SEEK_SET) < 0)
414  return;
415  put_ebml_length(pb, pos - master.pos, master.sizebytes);
416  avio_seek(pb, pos, SEEK_SET);
417 }
418 
420  uint32_t id, EbmlType type)
421 {
422  writer->elements[writer->nb_elements].id = id;
423  writer->elements[writer->nb_elements].type = type;
424  return &writer->elements[writer->nb_elements++];
425 }
426 
427 static void ebml_writer_open_master(EbmlWriter *writer, uint32_t id)
428 {
429  EbmlElement *const elem = ebml_writer_add(writer, id, EBML_MASTER);
430  EbmlMaster *const master = &elem->priv.master;
431 
432  master->containing_master = writer->current_master_element;
433  master->nb_elements = -1;
434 
435  writer->current_master_element = writer->nb_elements - 1;
436 }
437 
439 {
440  EbmlElement *elem;
441  av_assert2(writer->current_master_element >= 0);
442  av_assert2(writer->current_master_element < writer->nb_elements);
443  elem = &writer->elements[writer->current_master_element];
444  av_assert2(elem->type == EBML_MASTER);
445  elem->priv.master.nb_elements = writer->nb_elements - writer->current_master_element - 1;
447 }
448 
450 {
451  av_assert2(writer->nb_elements > 0);
452  if (writer->current_master_element == writer->nb_elements - 1) {
453  /* The master element has no children. Discard it. */
454  writer->nb_elements--;
455  return;
456  }
457  ebml_writer_close_master(writer);
458 }
459 
460 static void ebml_writer_add_string(EbmlWriter *writer, uint32_t id,
461  const char *str)
462 {
463  EbmlElement *const elem = ebml_writer_add(writer, id, EBML_STR);
464 
465  elem->priv.str = str;
466 }
467 
468 static void ebml_writer_add_bin(EbmlWriter *writer, uint32_t id,
469  const uint8_t *data, size_t size)
470 {
471  EbmlElement *const elem = ebml_writer_add(writer, id, EBML_BIN);
472 
473 #if SIZE_MAX > UINT64_MAX
474  size = FFMIN(size, UINT64_MAX);
475 #endif
476  elem->size = size;
477  elem->priv.bin = data;
478 }
479 
480 static void ebml_writer_add_float(EbmlWriter *writer, uint32_t id,
481  double val)
482 {
483  EbmlElement *const elem = ebml_writer_add(writer, id, EBML_FLOAT);
484 
485  elem->priv.f = val;
486 }
487 
488 static void ebml_writer_add_uid(EbmlWriter *writer, uint32_t id,
489  uint64_t val)
490 {
491  EbmlElement *const elem = ebml_writer_add(writer, id, EBML_UID);
492  elem->priv.uint = val;
493 }
494 
495 static void ebml_writer_add_uint(EbmlWriter *writer, uint32_t id,
496  uint64_t val)
497 {
498  EbmlElement *elem = ebml_writer_add(writer, id, EBML_UINT);
499  elem->priv.uint = val;
500 }
501 
502 static void ebml_writer_add_sint(EbmlWriter *writer, uint32_t id,
503  int64_t val)
504 {
505  EbmlElement *elem = ebml_writer_add(writer, id, EBML_SINT);
506  elem->priv.sint = val;
507 }
508 
510 {
512  elem->priv.mkv = mkv;
513 }
514 
516 {
517  size_t len = strlen(elem->priv.str);
518 #if SIZE_MAX > UINT64_MAX
519  len = FF_MIN(len, UINT64_MAX);
520 #endif
521  elem->size = len;
522  return 0;
523 }
524 
525 static av_const int uint_size(uint64_t val)
526 {
527  int bytes = 0;
528  do {
529  bytes++;
530  } while (val >>= 8);
531  return bytes;
532 }
533 
535 {
536  elem->size = uint_size(elem->priv.uint);
537  return 0;
538 }
539 
540 static av_const int sint_size(int64_t val)
541 {
542  uint64_t tmp = 2 * (uint64_t)(val < 0 ? val^-1 : val);
543  return uint_size(tmp);
544 }
545 
547 {
548  elem->size = sint_size(elem->priv.sint);
549  return 0;
550 }
551 
552 static int ebml_writer_elem_len(EbmlWriter *writer, EbmlElement *elem,
553  int remaining_elems);
554 
556  int remaining_elems)
557 {
558  int nb_elems = elem->priv.master.nb_elements >= 0 ? elem->priv.master.nb_elements : remaining_elems - 1;
559  EbmlElement *const master = elem;
560  uint64_t total_size = 0;
561 
562  master->priv.master.nb_elements = nb_elems;
563  for (; elem++, nb_elems > 0;) {
564  int ret = ebml_writer_elem_len(writer, elem, nb_elems);
565  if (ret < 0)
566  return ret;
567  av_assert2(ret < nb_elems);
568  /* No overflow is possible here, as both total_size and elem->size
569  * are bounded by MAX_SUPPORTED_EBML_LENGTH. */
570  total_size += ebml_id_size(elem->id) + elem->length_size + elem->size;
571  if (total_size > MAX_SUPPORTED_EBML_LENGTH)
572  return AVERROR(ERANGE);
573  nb_elems--; /* consume elem */
574  elem += ret, nb_elems -= ret; /* and elem's children */
575  }
576  master->size = total_size;
577 
578  return master->priv.master.nb_elements;
579 }
580 
582 {
583  MatroskaMuxContext *const mkv = elem->priv.mkv;
584  BlockContext *const block = &mkv->cur_block;
585  mkv_track *const track = block->track;
586  const AVPacket *const pkt = block->pkt;
587  int err, size;
588 
589  if (track->reformat) {
590  err = track->reformat(mkv, NULL, pkt, &size);
591  if (err < 0) {
592  av_log(mkv->ctx, AV_LOG_ERROR, "Error when reformatting data of "
593  "a packet from stream %d.\n", pkt->stream_index);
594  return err;
595  }
596  } else {
597  size = pkt->size;
598  if (track->offset <= size)
599  size -= track->offset;
600  }
601  elem->size = track->track_num_size + 3U + size;
602 
603  return 0;
604 }
605 
606 static void ebml_writer_write_block(const EbmlElement *elem, AVIOContext *pb)
607 {
608  MatroskaMuxContext *const mkv = elem->priv.mkv;
609  BlockContext *const block = &mkv->cur_block;
610  mkv_track *const track = block->track;
611  const AVPacket *const pkt = block->pkt;
612 
613  put_ebml_num(pb, track->track_num, track->track_num_size);
614  avio_wb16(pb, block->rel_ts);
615  avio_w8(pb, block->flags);
616 
617  if (track->reformat) {
618  int size;
619  track->reformat(mkv, pb, pkt, &size);
620  } else {
621  const uint8_t *data = pkt->data;
622  unsigned offset = track->offset <= pkt->size ? track->offset : 0;
623  avio_write(pb, data + offset, pkt->size - offset);
624  }
625 }
626 
627 static int ebml_writer_elem_len(EbmlWriter *writer, EbmlElement *elem,
628  int remaining_elems)
629 {
630  int ret = 0;
631 
632  switch (elem->type) {
633  case EBML_FLOAT:
634  case EBML_UID:
635  elem->size = 8;
636  break;
637  case EBML_STR:
638  ret = ebml_writer_str_len(elem);
639  break;
640  case EBML_UINT:
641  ret = ebml_writer_uint_len(elem);
642  break;
643  case EBML_SINT:
644  ret = ebml_writer_sint_len(elem);
645  break;
646  case EBML_BLOCK:
647  ret = ebml_writer_block_len(elem);
648  break;
649  case EBML_MASTER:
650  ret = ebml_writer_master_len(writer, elem, remaining_elems);
651  break;
652  }
653  if (ret < 0)
654  return ret;
655  if (elem->size > MAX_SUPPORTED_EBML_LENGTH)
656  return AVERROR(ERANGE);
657  elem->length_size = ebml_length_size(elem->size);
658  return ret; /* number of elements consumed excluding elem itself */
659 }
660 
661 static int ebml_writer_elem_write(const EbmlElement *elem, AVIOContext *pb)
662 {
663  put_ebml_id(pb, elem->id);
664  put_ebml_num(pb, elem->size, elem->length_size);
665  switch (elem->type) {
666  case EBML_UID:
667  case EBML_FLOAT: {
668  uint64_t val = elem->type == EBML_UID ? elem->priv.uint
669  : av_double2int(elem->priv.f);
670  avio_wb64(pb, val);
671  break;
672  }
673  case EBML_UINT:
674  case EBML_SINT: {
675  uint64_t val = elem->type == EBML_UINT ? elem->priv.uint
676  : elem->priv.sint;
677  for (int i = elem->size; --i >= 0; )
678  avio_w8(pb, (uint8_t)(val >> i * 8));
679  break;
680  }
681  case EBML_STR:
682  case EBML_BIN: {
683  const uint8_t *data = elem->type == EBML_BIN ? elem->priv.bin
684  : (const uint8_t*)elem->priv.str;
685  avio_write(pb, data, elem->size);
686  break;
687  }
688  case EBML_BLOCK:
689  ebml_writer_write_block(elem, pb);
690  break;
691  case EBML_MASTER: {
692  int nb_elems = elem->priv.master.nb_elements;
693 
694  elem++;
695  for (int i = 0; i < nb_elems; i++)
696  i += ebml_writer_elem_write(elem + i, pb);
697 
698  return nb_elems;
699  }
700  }
701  return 0;
702 }
703 
704 static int ebml_writer_write(EbmlWriter *writer, AVIOContext *pb)
705 {
706  int ret = ebml_writer_elem_len(writer, writer->elements,
707  writer->nb_elements);
708  if (ret < 0)
709  return ret;
710  ebml_writer_elem_write(writer->elements, pb);
711  return 0;
712 }
713 
714 static void mkv_add_seekhead_entry(MatroskaMuxContext *mkv, uint32_t elementid,
715  uint64_t filepos)
716 {
717  mkv_seekhead *seekhead = &mkv->seekhead;
718 
720 
721  seekhead->entries[seekhead->num_entries].elementid = elementid;
722  seekhead->entries[seekhead->num_entries++].segmentpos = filepos - mkv->segment_offset;
723 }
724 
726 {
727  int ret;
728 
729  if (!*dyn_cp && (ret = avio_open_dyn_buf(dyn_cp)) < 0)
730  return ret;
731 
732  if (mkv->write_crc)
733  put_ebml_void(*dyn_cp, 6); /* Reserve space for CRC32 so position/size calculations using avio_tell() take it into account */
734 
735  return 0;
736 }
737 
739  MatroskaMuxContext *mkv, uint32_t id,
740  int length_size, int keep_buffer,
741  int add_seekentry)
742 {
743  uint8_t *buf, crc[4];
744  int ret, size, skip = 0;
745 
746  size = avio_get_dyn_buf(*dyn_cp, &buf);
747  if ((ret = (*dyn_cp)->error) < 0)
748  goto fail;
749 
750  if (add_seekentry)
751  mkv_add_seekhead_entry(mkv, id, avio_tell(pb));
752 
753  put_ebml_id(pb, id);
754  put_ebml_length(pb, size, length_size);
755  if (mkv->write_crc) {
756  skip = 6; /* Skip reserved 6-byte long void element from the dynamic buffer. */
757  AV_WL32(crc, av_crc(av_crc_get_table(AV_CRC_32_IEEE_LE), UINT32_MAX, buf + skip, size - skip) ^ UINT32_MAX);
758  put_ebml_binary(pb, EBML_ID_CRC32, crc, sizeof(crc));
759  }
760  avio_write(pb, buf + skip, size - skip);
761 
762 fail:
763  if (keep_buffer) {
764  ffio_reset_dyn_buf(*dyn_cp);
765  } else {
766  ffio_free_dyn_buf(dyn_cp);
767  }
768  return ret;
769 }
770 
771 /**
772  * Output EBML master. Keep the buffer if seekable, allowing for later updates.
773  * Furthermore always add a SeekHead Entry for this element.
774  */
776  ebml_stored_master *elem,
777  MatroskaMuxContext *mkv, uint32_t id)
778 {
779  if (IS_SEEKABLE(pb, mkv)) {
780  uint8_t *buf;
781  int size = avio_get_dyn_buf(elem->bc, &buf);
782 
783  if (elem->bc->error < 0)
784  return elem->bc->error;
785 
786  elem->pos = avio_tell(pb);
787  mkv_add_seekhead_entry(mkv, id, elem->pos);
788 
789  put_ebml_id(pb, id);
790  put_ebml_length(pb, size, 0);
791  avio_write(pb, buf, size);
792 
793  return 0;
794  } else
795  return end_ebml_master_crc32(pb, &elem->bc, mkv, id, 0, 0, 1);
796 }
797 
798 static void put_xiph_size(AVIOContext *pb, int size)
799 {
800  ffio_fill(pb, 255, size / 255);
801  avio_w8(pb, size % 255);
802 }
803 
804 /**
805  * Free the members allocated in the mux context.
806  */
808 {
809  MatroskaMuxContext *mkv = s->priv_data;
810 
812  ffio_free_dyn_buf(&mkv->info.bc);
813  ffio_free_dyn_buf(&mkv->track.bc);
814  ffio_free_dyn_buf(&mkv->tags.bc);
815 
817  av_freep(&mkv->cues.entries);
818  av_freep(&mkv->tracks);
819 }
820 
821 /**
822  * Initialize the SeekHead element to be ready to index level 1 Matroska
823  * elements. Enough space to write MAX_SEEKHEAD_ENTRIES SeekHead entries
824  * will be reserved at the current file location.
825  */
827 {
828  mkv->seekhead.filepos = avio_tell(pb);
829  // 21 bytes max for a Seek entry, 6 bytes max for the SeekHead ID
830  // and size, 6 bytes for a CRC32 element, and 2 bytes to guarantee
831  // that an EBML void element will fit afterwards
834 }
835 
836 /**
837  * Write the SeekHead to the file at the location reserved for it
838  * and seek to destpos afterwards. When error_on_seek_failure
839  * is not set, failure to seek to the position designated for the
840  * SeekHead is not considered an error and it is presumed that
841  * destpos is the current position; failure to seek to destpos
842  * afterwards is always an error.
843  *
844  * @return 0 on success, < 0 on error.
845  */
847  int error_on_seek_failure, int64_t destpos)
848 {
849  AVIOContext *dyn_cp = NULL;
850  mkv_seekhead *seekhead = &mkv->seekhead;
851  int64_t remaining, ret64;
852  int i, ret;
853 
854  if ((ret64 = avio_seek(pb, seekhead->filepos, SEEK_SET)) < 0)
855  return error_on_seek_failure ? ret64 : 0;
856 
857  ret = start_ebml_master_crc32(&dyn_cp, mkv);
858  if (ret < 0)
859  return ret;
860 
861  for (i = 0; i < seekhead->num_entries; i++) {
862  mkv_seekhead_entry *entry = &seekhead->entries[i];
865 
867  put_ebml_length(dyn_cp, ebml_id_size(entry->elementid), 0);
868  put_ebml_id(dyn_cp, entry->elementid);
869 
871  end_ebml_master(dyn_cp, seekentry);
872  }
873  ret = end_ebml_master_crc32(pb, &dyn_cp, mkv,
874  MATROSKA_ID_SEEKHEAD, 0, 0, 0);
875  if (ret < 0)
876  return ret;
877 
878  remaining = seekhead->filepos + seekhead->reserved_size - avio_tell(pb);
879  put_ebml_void(pb, remaining);
880 
881  if ((ret64 = avio_seek(pb, destpos, SEEK_SET)) < 0)
882  return ret64;
883 
884  return 0;
885 }
886 
887 static int mkv_add_cuepoint(MatroskaMuxContext *mkv, int stream, int64_t ts,
888  int64_t cluster_pos, int64_t relative_pos, int64_t duration)
889 {
890  mkv_cues *cues = &mkv->cues;
891  mkv_cuepoint *entries = cues->entries;
892  unsigned idx = cues->num_entries;
893 
894  if (ts < 0)
895  return 0;
896 
897  entries = av_realloc_array(entries, cues->num_entries + 1, sizeof(mkv_cuepoint));
898  if (!entries)
899  return AVERROR(ENOMEM);
900  cues->entries = entries;
901 
902  /* Make sure the cues entries are sorted by pts. */
903  while (idx > 0 && entries[idx - 1].pts > ts)
904  idx--;
905  memmove(&entries[idx + 1], &entries[idx],
906  (cues->num_entries - idx) * sizeof(entries[0]));
907 
908  entries[idx].pts = ts;
909  entries[idx].stream_idx = stream;
910  entries[idx].cluster_pos = cluster_pos - mkv->segment_offset;
911  entries[idx].relative_pos = relative_pos;
912  entries[idx].duration = duration;
913 
914  cues->num_entries++;
915 
916  return 0;
917 }
918 
919 static int mkv_assemble_cues(AVStream **streams, AVIOContext *dyn_cp,
920  const mkv_cues *cues, mkv_track *tracks, int num_tracks,
921  uint64_t offset)
922 {
923  AVIOContext *cuepoint;
924  int ret;
925 
926  ret = avio_open_dyn_buf(&cuepoint);
927  if (ret < 0)
928  return ret;
929 
930  for (mkv_cuepoint *entry = cues->entries, *end = entry + cues->num_entries;
931  entry < end;) {
932  uint64_t pts = entry->pts;
933  uint8_t *buf;
934  int size;
935 
937 
938  // put all the entries from different tracks that have the exact same
939  // timestamp into the same CuePoint
940  for (int j = 0; j < num_tracks; j++)
941  tracks[j].has_cue = 0;
942  do {
943  ebml_master track_positions;
944  int idx = entry->stream_idx;
945 
946  av_assert0(idx >= 0 && idx < num_tracks);
947  if (tracks[idx].has_cue && streams[idx]->codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE)
948  continue;
949  tracks[idx].has_cue = 1;
951  put_ebml_uint(cuepoint, MATROSKA_ID_CUETRACK , tracks[idx].track_num);
952  put_ebml_uint(cuepoint, MATROSKA_ID_CUECLUSTERPOSITION , entry->cluster_pos + offset);
953  put_ebml_uint(cuepoint, MATROSKA_ID_CUERELATIVEPOSITION, entry->relative_pos);
954  if (entry->duration > 0)
955  put_ebml_uint(cuepoint, MATROSKA_ID_CUEDURATION , entry->duration);
956  end_ebml_master(cuepoint, track_positions);
957  } while (++entry < end && entry->pts == pts);
958  size = avio_get_dyn_buf(cuepoint, &buf);
959  if ((ret = cuepoint->error) < 0)
960  break;
962  ffio_reset_dyn_buf(cuepoint);
963  }
964  ffio_free_dyn_buf(&cuepoint);
965 
966  return ret;
967 }
968 
970  const AVCodecParameters *par)
971 {
972  const uint8_t *header_start[3];
973  int header_len[3];
974  int first_header_size;
975  int err, j;
976 
977  if (par->codec_id == AV_CODEC_ID_VORBIS)
978  first_header_size = 30;
979  else
980  first_header_size = 42;
981 
983  first_header_size, header_start, header_len);
984  if (err < 0) {
985  av_log(s, AV_LOG_ERROR, "Extradata corrupt.\n");
986  return err;
987  }
988 
989  avio_w8(pb, 2); // number packets - 1
990  for (j = 0; j < 2; j++) {
991  put_xiph_size(pb, header_len[j]);
992  }
993  for (j = 0; j < 3; j++)
994  avio_write(pb, header_start[j], header_len[j]);
995 
996  return 0;
997 }
998 
999 #if CONFIG_MATROSKA_MUXER
1000 static int put_wv_codecpriv(AVIOContext *pb, const AVCodecParameters *par)
1001 {
1002  if (par->extradata && par->extradata_size == 2)
1003  avio_write(pb, par->extradata, 2);
1004  else
1005  avio_wl16(pb, 0x410); // fallback to the most recent version
1006  return 0;
1007 }
1008 
1009 static int put_flac_codecpriv(AVFormatContext *s, AVIOContext *pb,
1010  const AVCodecParameters *par)
1011 {
1012  int write_comment = (par->channel_layout &&
1013  !(par->channel_layout & ~0x3ffffULL) &&
1015  int ret = ff_flac_write_header(pb, par->extradata, par->extradata_size,
1016  !write_comment);
1017 
1018  if (ret < 0)
1019  return ret;
1020 
1021  if (write_comment) {
1022  const char *vendor = (s->flags & AVFMT_FLAG_BITEXACT) ?
1023  "Lavf" : LIBAVFORMAT_IDENT;
1024  AVDictionary *dict = NULL;
1025  uint8_t buf[32];
1026  int64_t len;
1027 
1028  snprintf(buf, sizeof(buf), "0x%"PRIx64, par->channel_layout);
1029  av_dict_set(&dict, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", buf, 0);
1030 
1031  len = ff_vorbiscomment_length(dict, vendor, NULL, 0);
1032  av_assert1(len < (1 << 24) - 4);
1033 
1034  avio_w8(pb, 0x84);
1035  avio_wb24(pb, len);
1036 
1037  ff_vorbiscomment_write(pb, dict, vendor, NULL, 0);
1038 
1039  av_dict_free(&dict);
1040  }
1041 
1042  return 0;
1043 }
1044 
1045 static int get_aac_sample_rates(AVFormatContext *s, MatroskaMuxContext *mkv,
1046  const uint8_t *extradata, int extradata_size,
1047  int *sample_rate, int *output_sample_rate)
1048 {
1049  MPEG4AudioConfig mp4ac;
1050  int ret;
1051 
1052  ret = avpriv_mpeg4audio_get_config2(&mp4ac, extradata, extradata_size, 1, s);
1053  /* Don't abort if the failure is because of missing extradata. Assume in that
1054  * case a bitstream filter will provide the muxer with the extradata in the
1055  * first packet.
1056  * Abort however if s->pb is not seekable, as we would not be able to seek back
1057  * to write the sample rate elements once the extradata shows up, anyway. */
1058  if (ret < 0 && (extradata_size || !IS_SEEKABLE(s->pb, mkv))) {
1060  "Error parsing AAC extradata, unable to determine samplerate.\n");
1061  return AVERROR(EINVAL);
1062  }
1063 
1064  if (ret < 0) {
1065  /* This will only happen when this function is called while writing the
1066  * header and no extradata is available. The space for this element has
1067  * to be reserved for when this function is called again after the
1068  * extradata shows up in the first packet, as there's no way to know if
1069  * output_sample_rate will be different than sample_rate or not. */
1070  *output_sample_rate = *sample_rate;
1071  } else {
1072  *sample_rate = mp4ac.sample_rate;
1073  *output_sample_rate = mp4ac.ext_sample_rate;
1074  }
1075  return 0;
1076 }
1077 #endif
1078 
1080  const AVCodecParameters *par,
1081  AVIOContext *dyn_cp)
1082 {
1083  switch (par->codec_id) {
1084  case AV_CODEC_ID_VORBIS:
1085  case AV_CODEC_ID_THEORA:
1086  return put_xiph_codecpriv(s, dyn_cp, par);
1087  case AV_CODEC_ID_AV1:
1088  if (par->extradata_size)
1089  return ff_isom_write_av1c(dyn_cp, par->extradata,
1090  par->extradata_size);
1091  else
1092  put_ebml_void(pb, 4 + 3);
1093  break;
1094 #if CONFIG_MATROSKA_MUXER
1095  case AV_CODEC_ID_FLAC:
1096  return put_flac_codecpriv(s, dyn_cp, par);
1097  case AV_CODEC_ID_WAVPACK:
1098  return put_wv_codecpriv(dyn_cp, par);
1099  case AV_CODEC_ID_H264:
1100  return ff_isom_write_avcc(dyn_cp, par->extradata,
1101  par->extradata_size);
1102  case AV_CODEC_ID_HEVC:
1103  return ff_isom_write_hvcc(dyn_cp, par->extradata,
1104  par->extradata_size, 0);
1105  case AV_CODEC_ID_ALAC:
1106  if (par->extradata_size < 36) {
1108  "Invalid extradata found, ALAC expects a 36-byte "
1109  "QuickTime atom.");
1110  return AVERROR_INVALIDDATA;
1111  } else
1112  avio_write(dyn_cp, par->extradata + 12,
1113  par->extradata_size - 12);
1114  break;
1115  case AV_CODEC_ID_AAC:
1116  if (par->extradata_size)
1117  avio_write(dyn_cp, par->extradata, par->extradata_size);
1118  else
1119  put_ebml_void(pb, MAX_PCE_SIZE + 2 + 4);
1120  break;
1121 #endif
1122  default:
1123  if (CONFIG_MATROSKA_MUXER && par->codec_id == AV_CODEC_ID_PRORES &&
1125  avio_wl32(dyn_cp, par->codec_tag);
1126  } else if (par->extradata_size && par->codec_id != AV_CODEC_ID_TTA)
1127  avio_write(dyn_cp, par->extradata, par->extradata_size);
1128  }
1129 
1130  return 0;
1131 }
1132 
1134  AVCodecParameters *par,
1135  int native_id, int qt_id)
1136 {
1137  MatroskaMuxContext av_unused *const mkv = s->priv_data;
1138  AVIOContext *dyn_cp;
1139  uint8_t *codecpriv;
1140  int ret, codecpriv_size;
1141 
1142  ret = avio_open_dyn_buf(&dyn_cp);
1143  if (ret < 0)
1144  return ret;
1145 
1146  if (native_id) {
1147  ret = mkv_write_native_codecprivate(s, pb, par, dyn_cp);
1148 #if CONFIG_MATROSKA_MUXER
1149  } else if (par->codec_type == AVMEDIA_TYPE_VIDEO) {
1150  if (qt_id) {
1151  if (!par->codec_tag)
1153  par->codec_id);
1156  ) {
1157  avio_wb32(dyn_cp, 0x5a + par->extradata_size);
1158  avio_wl32(dyn_cp, par->codec_tag);
1159  ffio_fill(dyn_cp, 0, 0x5a - 8);
1160  }
1161  avio_write(dyn_cp, par->extradata, par->extradata_size);
1162  } else {
1164  av_log(s, AV_LOG_WARNING, "codec %s is not supported by this format\n",
1165  avcodec_get_name(par->codec_id));
1166 
1167  if (!par->codec_tag)
1169  par->codec_id);
1170  if (!par->codec_tag && par->codec_id != AV_CODEC_ID_RAWVIDEO) {
1171  av_log(s, AV_LOG_ERROR, "No bmp codec tag found for codec %s\n",
1172  avcodec_get_name(par->codec_id));
1173  ret = AVERROR(EINVAL);
1174  }
1175 
1176  ff_put_bmp_header(dyn_cp, par, 0, 0, mkv->flipped_raw_rgb);
1177  }
1178  } else if (par->codec_type == AVMEDIA_TYPE_AUDIO) {
1179  unsigned int tag;
1181  if (!tag) {
1182  av_log(s, AV_LOG_ERROR, "No wav codec tag found for codec %s\n",
1183  avcodec_get_name(par->codec_id));
1184  ret = AVERROR(EINVAL);
1185  }
1186  if (!par->codec_tag)
1187  par->codec_tag = tag;
1188 
1190 #endif
1191  }
1192 
1193  if (ret >= 0) {
1194  codecpriv_size = avio_get_dyn_buf(dyn_cp, &codecpriv);
1195  if ((ret = dyn_cp->error) >= 0 && codecpriv_size)
1197  codecpriv_size);
1198  }
1199  ffio_free_dyn_buf(&dyn_cp);
1200  return ret;
1201 }
1202 
1203 #define MAX_VIDEO_COLOR_ELEMS 20
1204 static void mkv_write_video_color(EbmlWriter *writer, const AVStream *st,
1205  const AVCodecParameters *par)
1206 {
1207  const void *side_data;
1208 
1210 
1211  if (par->color_trc != AVCOL_TRC_UNSPECIFIED &&
1212  par->color_trc < AVCOL_TRC_NB) {
1214  par->color_trc);
1215  }
1216  if (par->color_space != AVCOL_SPC_UNSPECIFIED &&
1217  par->color_space < AVCOL_SPC_NB) {
1219  par->color_space);
1220  }
1221  if (par->color_primaries != AVCOL_PRI_UNSPECIFIED &&
1222  par->color_primaries < AVCOL_PRI_NB) {
1224  par->color_primaries);
1225  }
1226  if (par->color_range != AVCOL_RANGE_UNSPECIFIED &&
1227  par->color_range < AVCOL_RANGE_NB) {
1229  }
1232  int xpos, ypos;
1233 
1234  avcodec_enum_to_chroma_pos(&xpos, &ypos, par->chroma_location);
1236  (xpos >> 7) + 1);
1238  (ypos >> 7) + 1);
1239  }
1240 
1242  NULL);
1243  if (side_data) {
1244  const AVContentLightMetadata *metadata = side_data;
1246  metadata->MaxCLL);
1248  metadata->MaxFALL);
1249  }
1250 
1252  NULL);
1253  if (side_data) {
1254  const AVMasteringDisplayMetadata *metadata = side_data;
1256  if (metadata->has_primaries) {
1258  av_q2d(metadata->display_primaries[0][0]));
1260  av_q2d(metadata->display_primaries[0][1]));
1262  av_q2d(metadata->display_primaries[1][0]));
1264  av_q2d(metadata->display_primaries[1][1]));
1266  av_q2d(metadata->display_primaries[2][0]));
1268  av_q2d(metadata->display_primaries[2][1]));
1270  av_q2d(metadata->white_point[0]));
1272  av_q2d(metadata->white_point[1]));
1273  }
1274  if (metadata->has_luminance) {
1276  av_q2d(metadata->max_luminance));
1278  av_q2d(metadata->min_luminance));
1279  }
1281  }
1282 
1284 }
1285 
1286 #define MAX_VIDEO_PROJECTION_ELEMS 6
1288  const AVStream *st, uint8_t private[])
1289 {
1290  const AVSphericalMapping *spherical =
1292  NULL);
1293 
1294  if (!spherical)
1295  return;
1296 
1297  if (spherical->projection != AV_SPHERICAL_EQUIRECTANGULAR &&
1299  spherical->projection != AV_SPHERICAL_CUBEMAP) {
1300  av_log(s, AV_LOG_WARNING, "Unknown projection type\n");
1301  return;
1302  }
1303 
1305 
1306  switch (spherical->projection) {
1311  AV_WB32(private, 0); // version + flags
1312  if (spherical->projection == AV_SPHERICAL_EQUIRECTANGULAR) {
1313  AV_WB32(private + 4, 0);
1314  AV_WB32(private + 8, 0);
1315  AV_WB32(private + 12, 0);
1316  AV_WB32(private + 16, 0);
1317  } else {
1318  AV_WB32(private + 4, spherical->bound_top);
1319  AV_WB32(private + 8, spherical->bound_bottom);
1320  AV_WB32(private + 12, spherical->bound_left);
1321  AV_WB32(private + 16, spherical->bound_right);
1322  }
1324  private, 20);
1325  break;
1326  case AV_SPHERICAL_CUBEMAP:
1329  AV_WB32(private, 0); // version + flags
1330  AV_WB32(private + 4, 0); // layout
1331  AV_WB32(private + 8, spherical->padding);
1333  private, 12);
1334  break;
1335  default:
1336  av_assert0(0);
1337  }
1338 
1339  if (spherical->yaw)
1341  (double) spherical->yaw / (1 << 16));
1342  if (spherical->pitch)
1344  (double) spherical->pitch / (1 << 16));
1345  if (spherical->roll)
1347  (double) spherical->roll / (1 << 16));
1348 
1349  ebml_writer_close_master(writer);
1350 }
1351 
1352 #define MAX_FIELD_ORDER_ELEMS 2
1353 static void mkv_write_field_order(EbmlWriter *writer, int is_webm,
1354  enum AVFieldOrder field_order)
1355 {
1356  switch (field_order) {
1357  case AV_FIELD_UNKNOWN:
1358  break;
1359  case AV_FIELD_PROGRESSIVE:
1362  break;
1363  case AV_FIELD_TT:
1364  case AV_FIELD_BB:
1365  case AV_FIELD_TB:
1366  case AV_FIELD_BT:
1369  if (!is_webm) {
1370  switch (field_order) {
1371  case AV_FIELD_TT:
1374  break;
1375  case AV_FIELD_BB:
1378  break;
1379  case AV_FIELD_TB:
1382  break;
1383  case AV_FIELD_BT:
1386  break;
1387  }
1388  }
1389  }
1390 }
1391 
1392 #define MAX_STEREO_MODE_ELEMS 1
1394  AVStream *st, int is_webm,
1395  int *h_width, int *h_height)
1396 {
1397  const AVDictionaryEntry *tag;
1399  const AVStereo3D *stereo;
1400 
1401  *h_width = 1;
1402  *h_height = 1;
1403  // convert metadata into proper side data and add it to the stream
1404  if ((tag = av_dict_get(st->metadata, "stereo_mode", NULL, 0)) ||
1405  (tag = av_dict_get( s->metadata, "stereo_mode", NULL, 0))) {
1406  int stereo_mode = atoi(tag->value);
1407 
1408  for (int i = 0; i < MATROSKA_VIDEO_STEREOMODE_TYPE_NB; i++)
1409  if (!strcmp(tag->value, ff_matroska_video_stereo_mode[i])){
1410  stereo_mode = i;
1411  break;
1412  }
1413 
1414  if (stereo_mode < MATROSKA_VIDEO_STEREOMODE_TYPE_NB &&
1415  stereo_mode != 10 && stereo_mode != 12) {
1416  int ret = ff_mkv_stereo3d_conv(st, stereo_mode);
1417  if (ret < 0)
1418  return ret;
1419  }
1420  }
1421 
1423  NULL);
1424  if (stereo) {
1425  switch (stereo->type) {
1426  case AV_STEREO3D_2D:
1428  break;
1430  format = (stereo->flags & AV_STEREO3D_FLAG_INVERT)
1433  *h_width = 2;
1434  break;
1435  case AV_STEREO3D_TOPBOTTOM:
1437  if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
1438  format--;
1439  *h_height = 2;
1440  break;
1443  if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
1444  format--;
1445  break;
1446  case AV_STEREO3D_LINES:
1448  if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
1449  format--;
1450  *h_height = 2;
1451  break;
1452  case AV_STEREO3D_COLUMNS:
1454  if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
1455  format--;
1456  *h_width = 2;
1457  break;
1460  if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
1461  format++;
1462  break;
1463  }
1464  }
1465 
1467  return 0;
1468 
1469  // if webm, do not write unsupported modes
1470  if ((is_webm &&
1475  "The specified stereo mode is not valid.\n");
1476  return AVERROR(EINVAL);
1477  }
1478 
1479  // write StereoMode if format is valid
1481 
1482  return 0;
1483 }
1484 
1486 {
1487 #if CONFIG_MATROSKA_MUXER
1490 
1491  if (dovi && dovi->dv_profile <= 10) {
1492  ebml_master mapping;
1493  uint8_t buf[ISOM_DVCC_DVVC_SIZE];
1494  uint32_t type;
1495 
1496  uint64_t expected_size = (2 + 1 + (sizeof(DVCC_DVVC_BLOCK_TYPE_NAME) - 1))
1497  + (2 + 1 + 4) + (2 + 1 + ISOM_DVCC_DVVC_SIZE);
1498 
1499  if (dovi->dv_profile > 7) {
1500  type = MKBETAG('d', 'v', 'v', 'C');
1501  } else {
1502  type = MKBETAG('d', 'v', 'c', 'C');
1503  }
1504 
1505  ff_isom_put_dvcc_dvvc(s, buf, dovi);
1506 
1507  mapping = start_ebml_master(pb, MATROSKA_ID_TRACKBLKADDMAPPING, expected_size);
1508 
1511  put_ebml_binary(pb, MATROSKA_ID_BLKADDIDEXTRADATA, buf, sizeof(buf));
1512 
1513  end_ebml_master(pb, mapping);
1514  }
1515 #endif
1516 }
1517 
1519  AVStream *st, const AVCodecParameters *par,
1520  AVIOContext *pb)
1521 {
1522  const AVDictionaryEntry *tag;
1523  int display_width_div = 1, display_height_div = 1;
1524  uint8_t color_space[4], projection_private[20];
1527  int ret;
1528 
1530 
1533 
1534  mkv_write_field_order(&writer, IS_WEBM(mkv), par->field_order);
1535 
1536  // check both side data and metadata for stereo information,
1537  // write the result to the bitstream if any is found
1538  ret = mkv_write_stereo_mode(s, &writer, st, IS_WEBM(mkv),
1539  &display_width_div,
1540  &display_height_div);
1541  if (ret < 0)
1542  return ret;
1543 
1544  if (((tag = av_dict_get(st->metadata, "alpha_mode", NULL, 0)) && atoi(tag->value)) ||
1545  ((tag = av_dict_get( s->metadata, "alpha_mode", NULL, 0)) && atoi(tag->value)) ||
1546  (par->format == AV_PIX_FMT_YUVA420P)) {
1548  }
1549 
1550  // write DisplayWidth and DisplayHeight, they contain the size of
1551  // a single source view and/or the display aspect ratio
1552  if (st->sample_aspect_ratio.num) {
1553  int64_t d_width = av_rescale(par->width, st->sample_aspect_ratio.num, st->sample_aspect_ratio.den);
1554  if (d_width > INT_MAX) {
1555  av_log(s, AV_LOG_ERROR, "Overflow in display width\n");
1556  return AVERROR(EINVAL);
1557  }
1558  if (d_width != par->width || display_width_div != 1 || display_height_div != 1) {
1559  if (IS_WEBM(mkv) || display_width_div != 1 || display_height_div != 1) {
1561  d_width / display_width_div);
1563  par->height / display_height_div);
1564  } else {
1565  AVRational display_aspect_ratio;
1566  av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1567  par->width * (int64_t)st->sample_aspect_ratio.num,
1568  par->height * (int64_t)st->sample_aspect_ratio.den,
1569  1024 * 1024);
1571  display_aspect_ratio.num);
1573  display_aspect_ratio.den);
1576  }
1577  }
1578  } else if (display_width_div != 1 || display_height_div != 1) {
1580  par->width / display_width_div);
1582  par->height / display_height_div);
1583  } else if (!IS_WEBM(mkv))
1586 
1587  if (par->codec_id == AV_CODEC_ID_RAWVIDEO) {
1588  AV_WL32(color_space, par->codec_tag);
1590  color_space, sizeof(color_space));
1591  }
1592  mkv_write_video_color(&writer, st, par);
1593  mkv_write_video_projection(s, &writer, st, projection_private);
1594 
1595  return ebml_writer_write(&writer, pb);
1596 }
1597 
1599  AVStream *st, mkv_track *track, AVIOContext *pb,
1600  int is_default)
1601 {
1602  AVCodecParameters *par = st->codecpar;
1603  ebml_master subinfo, track_master;
1604  int native_id = 0;
1605  int qt_id = 0;
1606  int bit_depth;
1607  int sample_rate = par->sample_rate;
1608  int output_sample_rate = 0;
1609  int j, ret;
1610  const AVDictionaryEntry *tag;
1611 
1612  if (par->codec_type == AVMEDIA_TYPE_ATTACHMENT)
1613  return 0;
1614 
1615  track_master = start_ebml_master(pb, MATROSKA_ID_TRACKENTRY, 0);
1617  put_ebml_uid (pb, MATROSKA_ID_TRACKUID, track->uid);
1618  put_ebml_uint(pb, MATROSKA_ID_TRACKFLAGLACING, 0); // no lacing (yet)
1619 
1620  if ((tag = av_dict_get(st->metadata, "title", NULL, 0)))
1622  tag = av_dict_get(st->metadata, "language", NULL, 0);
1624  tag && tag->value[0] ? tag->value : "und");
1625 
1626  // The default value for TRACKFLAGDEFAULT is 1, so add element
1627  // if we need to clear it.
1628  if (!is_default)
1630 
1633 
1634  if (IS_WEBM(mkv)) {
1635  const char *codec_id;
1636  if (par->codec_type != AVMEDIA_TYPE_SUBTITLE) {
1637  for (j = 0; ff_webm_codec_tags[j].id != AV_CODEC_ID_NONE; j++) {
1638  if (ff_webm_codec_tags[j].id == par->codec_id) {
1640  native_id = 1;
1641  break;
1642  }
1643  }
1644  } else if (par->codec_id == AV_CODEC_ID_WEBVTT) {
1646  codec_id = "D_WEBVTT/CAPTIONS";
1647  native_id = MATROSKA_TRACK_TYPE_SUBTITLE;
1648  } else if (st->disposition & AV_DISPOSITION_DESCRIPTIONS) {
1649  codec_id = "D_WEBVTT/DESCRIPTIONS";
1650  native_id = MATROSKA_TRACK_TYPE_METADATA;
1651  } else if (st->disposition & AV_DISPOSITION_METADATA) {
1652  codec_id = "D_WEBVTT/METADATA";
1653  native_id = MATROSKA_TRACK_TYPE_METADATA;
1654  } else {
1655  codec_id = "D_WEBVTT/SUBTITLES";
1656  native_id = MATROSKA_TRACK_TYPE_SUBTITLE;
1657  }
1658  }
1659 
1660  if (!native_id) {
1662  "Only VP8 or VP9 or AV1 video and Vorbis or Opus audio and WebVTT subtitles are supported for WebM.\n");
1663  return AVERROR(EINVAL);
1664  }
1665 
1667  } else {
1679 
1680  // look for a codec ID string specific to mkv to use,
1681  // if none are found, use AVI codes
1682  if (par->codec_id != AV_CODEC_ID_RAWVIDEO || par->codec_tag) {
1683  for (j = 0; ff_mkv_codec_tags[j].id != AV_CODEC_ID_NONE; j++) {
1684  if (ff_mkv_codec_tags[j].id == par->codec_id && par->codec_id != AV_CODEC_ID_FFV1) {
1686  native_id = 1;
1687  break;
1688  }
1689  }
1690  } else {
1691  if (mkv->allow_raw_vfw) {
1692  native_id = 0;
1693  } else {
1694  av_log(s, AV_LOG_ERROR, "Raw RGB is not supported Natively in Matroska, you can use AVI or NUT or\n"
1695  "If you would like to store it anyway using VFW mode, enable allow_raw_vfw (-allow_raw_vfw 1)\n");
1696  return AVERROR(EINVAL);
1697  }
1698  }
1699  }
1700 
1701  switch (par->codec_type) {
1702  case AVMEDIA_TYPE_VIDEO:
1703  mkv->have_video = 1;
1705 
1706  if( st->avg_frame_rate.num > 0 && st->avg_frame_rate.den > 0
1707  && av_cmp_q(av_inv_q(st->avg_frame_rate), st->time_base) > 0)
1709  else if( st->r_frame_rate.num > 0 && st->r_frame_rate.den > 0
1710  && av_cmp_q(av_inv_q(st->r_frame_rate), st->time_base) > 0)
1712 
1713  if (CONFIG_MATROSKA_MUXER && !native_id &&
1716  par->codec_id == AV_CODEC_ID_SVQ1 ||
1717  par->codec_id == AV_CODEC_ID_SVQ3 ||
1718  par->codec_id == AV_CODEC_ID_CINEPAK))
1719  qt_id = 1;
1720 
1721  if (qt_id)
1722  put_ebml_string(pb, MATROSKA_ID_CODECID, "V_QUICKTIME");
1723  else if (!native_id) {
1724  // if there is no mkv-specific codec ID, use VFW mode
1725  put_ebml_string(pb, MATROSKA_ID_CODECID, "V_MS/VFW/FOURCC");
1726  track->write_dts = 1;
1728  }
1729 
1730  ret = mkv_write_track_video(s, mkv, st, par, pb);
1731  if (ret < 0)
1732  return ret;
1733 
1734  if (!IS_WEBM(mkv))
1735  mkv_write_dovi(s, pb, st);
1736 
1737  break;
1738 
1739  case AVMEDIA_TYPE_AUDIO:
1740  if (par->initial_padding && par->codec_id == AV_CODEC_ID_OPUS) {
1741  int64_t codecdelay = av_rescale_q(par->initial_padding,
1742  (AVRational){ 1, 48000 },
1743  (AVRational){ 1, 1000000000 });
1744  if (codecdelay < 0) {
1745  av_log(s, AV_LOG_ERROR, "Initial padding is invalid\n");
1746  return AVERROR(EINVAL);
1747  }
1748 // track->ts_offset = av_rescale_q(par->initial_padding,
1749 // (AVRational){ 1, par->sample_rate },
1750 // st->time_base);
1751 
1752  put_ebml_uint(pb, MATROSKA_ID_CODECDELAY, codecdelay);
1753  }
1754  if (par->codec_id == AV_CODEC_ID_OPUS)
1756 #if CONFIG_MATROSKA_MUXER
1757  else if (par->codec_id == AV_CODEC_ID_AAC) {
1758  ret = get_aac_sample_rates(s, mkv, par->extradata, par->extradata_size,
1759  &sample_rate, &output_sample_rate);
1760  if (ret < 0)
1761  return ret;
1762  }
1763 #endif
1764 
1766 
1767  if (!native_id)
1768  // no mkv-specific ID, use ACM mode
1769  put_ebml_string(pb, MATROSKA_ID_CODECID, "A_MS/ACM");
1770 
1771  subinfo = start_ebml_master(pb, MATROSKA_ID_TRACKAUDIO, 6 + 4 * 9);
1773 
1774  track->sample_rate_offset = avio_tell(pb);
1776  if (output_sample_rate)
1777  put_ebml_float(pb, MATROSKA_ID_AUDIOOUTSAMPLINGFREQ, output_sample_rate);
1778 
1780  if (!bit_depth && par->codec_id != AV_CODEC_ID_ADPCM_G726) {
1781  if (par->bits_per_raw_sample)
1783  else
1785  }
1786  if (!bit_depth)
1788  if (bit_depth)
1790  end_ebml_master(pb, subinfo);
1791  break;
1792 
1793  case AVMEDIA_TYPE_SUBTITLE:
1794  if (!native_id) {
1795  av_log(s, AV_LOG_ERROR, "Subtitle codec %d is not supported.\n", par->codec_id);
1796  return AVERROR(ENOSYS);
1797  }
1798  if (!IS_WEBM(mkv) && st->disposition & AV_DISPOSITION_DESCRIPTIONS)
1800 
1801  if (!IS_WEBM(mkv) || par->codec_id != AV_CODEC_ID_WEBVTT)
1802  native_id = MATROSKA_TRACK_TYPE_SUBTITLE;
1803 
1804  put_ebml_uint(pb, MATROSKA_ID_TRACKTYPE, native_id);
1805  break;
1806  default:
1807  av_log(s, AV_LOG_ERROR, "Only audio, video, and subtitles are supported for Matroska.\n");
1808  return AVERROR(EINVAL);
1809  }
1810 
1811  if (!IS_WEBM(mkv) || par->codec_id != AV_CODEC_ID_WEBVTT) {
1812  track->codecpriv_offset = avio_tell(pb);
1813  ret = mkv_write_codecprivate(s, pb, par, native_id, qt_id);
1814  if (ret < 0)
1815  return ret;
1816  }
1817 
1818  end_ebml_master(pb, track_master);
1819 
1820  return 0;
1821 }
1822 
1824 {
1825  MatroskaMuxContext *mkv = s->priv_data;
1826  AVIOContext *pb = s->pb;
1827  int video_default_idx = -1, audio_default_idx = -1, subtitle_default_idx = -1;
1828  int i, ret;
1829 
1830  if (mkv->nb_attachments == s->nb_streams)
1831  return 0;
1832 
1833  ret = start_ebml_master_crc32(&mkv->track.bc, mkv);
1834  if (ret < 0)
1835  return ret;
1836 
1837  if (mkv->default_mode != DEFAULT_MODE_PASSTHROUGH) {
1838  int video_idx = -1, audio_idx = -1, subtitle_idx = -1;
1839 
1840  for (i = s->nb_streams - 1; i >= 0; i--) {
1841  AVStream *st = s->streams[i];
1842 
1843  switch (st->codecpar->codec_type) {
1844 #define CASE(type, variable) \
1845  case AVMEDIA_TYPE_ ## type: \
1846  variable ## _idx = i; \
1847  if (st->disposition & AV_DISPOSITION_DEFAULT) \
1848  variable ## _default_idx = i; \
1849  break;
1850  CASE(VIDEO, video)
1851  CASE(AUDIO, audio)
1852  CASE(SUBTITLE, subtitle)
1853 #undef CASE
1854  }
1855  }
1856 
1857  video_default_idx = FFMAX(video_default_idx, video_idx);
1858  audio_default_idx = FFMAX(audio_default_idx, audio_idx);
1860  subtitle_default_idx = FFMAX(subtitle_default_idx, subtitle_idx);
1861  }
1862  for (i = 0; i < s->nb_streams; i++) {
1863  AVStream *st = s->streams[i];
1864  int is_default = st->disposition & AV_DISPOSITION_DEFAULT ||
1865  i == video_default_idx || i == audio_default_idx ||
1866  i == subtitle_default_idx;
1867  ret = mkv_write_track(s, mkv, st, &mkv->tracks[i],
1868  mkv->track.bc, is_default);
1869  if (ret < 0)
1870  return ret;
1871  }
1872 
1873  return end_ebml_master_crc32_tentatively(pb, &mkv->track, mkv,
1875 }
1876 
1878 {
1879  EBML_WRITER(4);
1880  uint8_t *key = av_strdup(t->key);
1881  uint8_t *p = key;
1882  const uint8_t *lang = NULL;
1883  int ret;
1884 
1885  if (!key)
1886  return AVERROR(ENOMEM);
1887 
1888  if ((p = strrchr(p, '-')) &&
1889  (lang = ff_convert_lang_to(p + 1, AV_LANG_ISO639_2_BIBL)))
1890  *p = 0;
1891 
1892  p = key;
1893  while (*p) {
1894  if (*p == ' ')
1895  *p = '_';
1896  else if (*p >= 'a' && *p <= 'z')
1897  *p -= 'a' - 'A';
1898  p++;
1899  }
1900 
1903  if (lang)
1906  ret = ebml_writer_write(&writer, pb);
1907 
1908  av_freep(&key);
1909  return ret;
1910 }
1911 
1913  ebml_master *tag, uint32_t elementid, uint64_t uid)
1914 {
1916  int ret;
1917 
1918  if (!*pb) {
1919  ret = start_ebml_master_crc32(pb, mkv);
1920  if (ret < 0)
1921  return ret;
1922  }
1923 
1926  if (elementid)
1927  put_ebml_uid(*pb, elementid, uid);
1928  end_ebml_master(*pb, targets);
1929  return 0;
1930 }
1931 
1932 static int mkv_check_tag_name(const char *name, uint32_t elementid)
1933 {
1934  return av_strcasecmp(name, "title") &&
1935  av_strcasecmp(name, "stereo_mode") &&
1936  av_strcasecmp(name, "creation_time") &&
1937  av_strcasecmp(name, "encoding_tool") &&
1938  av_strcasecmp(name, "duration") &&
1939  (elementid != MATROSKA_ID_TAGTARGETS_TRACKUID ||
1940  av_strcasecmp(name, "language")) &&
1941  (elementid != MATROSKA_ID_TAGTARGETS_ATTACHUID ||
1942  (av_strcasecmp(name, "filename") &&
1943  av_strcasecmp(name, "mimetype")));
1944 }
1945 
1947  AVIOContext **pb, ebml_master *tag,
1948  uint32_t elementid, uint64_t uid)
1949 {
1950  const AVDictionaryEntry *t = NULL;
1951  ebml_master tag2;
1952  int ret;
1953 
1954  ret = mkv_write_tag_targets(mkv, pb, tag ? tag : &tag2, elementid, uid);
1955  if (ret < 0)
1956  return ret;
1957 
1958  while ((t = av_dict_get(m, "", t, AV_DICT_IGNORE_SUFFIX))) {
1959  if (mkv_check_tag_name(t->key, elementid)) {
1960  ret = mkv_write_simpletag(*pb, t);
1961  if (ret < 0)
1962  return ret;
1963  }
1964  }
1965 
1966  if (!tag)
1967  end_ebml_master(*pb, tag2);
1968 
1969  return 0;
1970 }
1971 
1972 static int mkv_check_tag(const AVDictionary *m, uint32_t elementid)
1973 {
1974  const AVDictionaryEntry *t = NULL;
1975 
1976  while ((t = av_dict_get(m, "", t, AV_DICT_IGNORE_SUFFIX)))
1977  if (mkv_check_tag_name(t->key, elementid))
1978  return 1;
1979 
1980  return 0;
1981 }
1982 
1984 {
1985  MatroskaMuxContext *mkv = s->priv_data;
1986  ebml_master tag, *tagp = IS_SEEKABLE(s->pb, mkv) ? &tag : NULL;
1987  int i, ret;
1988 
1989  mkv->wrote_tags = 1;
1990 
1992 
1993  if (mkv_check_tag(s->metadata, 0)) {
1994  ret = mkv_write_tag(mkv, s->metadata, &mkv->tags.bc, NULL, 0, 0);
1995  if (ret < 0)
1996  return ret;
1997  }
1998 
1999  for (i = 0; i < s->nb_streams; i++) {
2000  const AVStream *st = s->streams[i];
2001  mkv_track *track = &mkv->tracks[i];
2002 
2004  continue;
2005 
2007  continue;
2008 
2009  ret = mkv_write_tag(mkv, st->metadata, &mkv->tags.bc, tagp,
2011  if (ret < 0)
2012  return ret;
2013 
2014  if (tagp) {
2015  AVIOContext *pb = mkv->tags.bc;
2016  ebml_master simpletag;
2017 
2018  simpletag = start_ebml_master(pb, MATROSKA_ID_SIMPLETAG,
2019  2 + 1 + 8 + 23);
2020  put_ebml_string(pb, MATROSKA_ID_TAGNAME, "DURATION");
2021  track->duration_offset = avio_tell(pb);
2022 
2023  // Reserve space to write duration as a 20-byte string.
2024  // 2 (ebml id) + 1 (data size) + 20 (data)
2025  put_ebml_void(pb, 23);
2026  end_ebml_master(pb, simpletag);
2027  end_ebml_master(pb, tag);
2028  }
2029  }
2030 
2031  if (mkv->nb_attachments && !IS_WEBM(mkv)) {
2032  for (i = 0; i < s->nb_streams; i++) {
2033  const mkv_track *track = &mkv->tracks[i];
2034  const AVStream *st = s->streams[i];
2035 
2037  continue;
2038 
2040  continue;
2041 
2042  ret = mkv_write_tag(mkv, st->metadata, &mkv->tags.bc, NULL,
2044  if (ret < 0)
2045  return ret;
2046  }
2047  }
2048 
2049  if (mkv->tags.bc) {
2050  return end_ebml_master_crc32_tentatively(s->pb, &mkv->tags, mkv,
2052  }
2053  return 0;
2054 }
2055 
2057 {
2058  for (unsigned i = 0; i < s->nb_chapters; i++) {
2059  if (!s->chapters[i]->id)
2060  return 1;
2061  for (unsigned j = 0; j < i; j++)
2062  if (s->chapters[j]->id == s->chapters[i]->id)
2063  return 1;
2064  }
2065  return 0;
2066 }
2067 
2069 {
2070  MatroskaMuxContext *mkv = s->priv_data;
2071  AVIOContext *dyn_cp = NULL, *dyn_tags = NULL, **tags, *pb = s->pb;
2072  ebml_master editionentry;
2073  AVRational scale = {1, 1E9};
2074  int ret, create_new_ids;
2075 
2076  if (!s->nb_chapters || mkv->wrote_chapters)
2077  return 0;
2078 
2079  ret = start_ebml_master_crc32(&dyn_cp, mkv);
2080  if (ret < 0)
2081  return ret;
2082 
2083  editionentry = start_ebml_master(dyn_cp, MATROSKA_ID_EDITIONENTRY, 0);
2084  if (!IS_WEBM(mkv)) {
2086  /* If mkv_write_tags() has already been called, then any tags
2087  * corresponding to chapters will be put into a new Tags element. */
2088  tags = mkv->wrote_tags ? &dyn_tags : &mkv->tags.bc;
2089  } else
2090  tags = NULL;
2091 
2092  create_new_ids = mkv_new_chapter_ids_needed(s);
2093 
2094  for (unsigned i = 0; i < s->nb_chapters; i++) {
2095  const AVChapter *c = s->chapters[i];
2096  int64_t chapterstart = av_rescale_q(c->start, c->time_base, scale);
2097  int64_t chapterend = av_rescale_q(c->end, c->time_base, scale);
2098  const AVDictionaryEntry *t;
2099  uint64_t uid = create_new_ids ? i + 1ULL : c->id;
2100  EBML_WRITER(7);
2101 
2102  if (chapterstart < 0 || chapterstart > chapterend || chapterend < 0) {
2104  "Invalid chapter start (%"PRId64") or end (%"PRId64").\n",
2105  chapterstart, chapterend);
2107  goto fail;
2108  }
2109 
2112  ebml_writer_add_uint(&writer, MATROSKA_ID_CHAPTERTIMESTART, chapterstart);
2113  ebml_writer_add_uint(&writer, MATROSKA_ID_CHAPTERTIMEEND, chapterend);
2114  if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
2117  ebml_writer_add_string(&writer, MATROSKA_ID_CHAPLANG , "und");
2118  }
2119  ret = ebml_writer_write(&writer, dyn_cp);
2120  if (ret < 0)
2121  goto fail;
2122 
2123  if (tags && mkv_check_tag(c->metadata, MATROSKA_ID_TAGTARGETS_CHAPTERUID)) {
2124  ret = mkv_write_tag(mkv, c->metadata, tags, NULL,
2126  if (ret < 0)
2127  goto fail;
2128  }
2129  }
2130  end_ebml_master(dyn_cp, editionentry);
2131  mkv->wrote_chapters = 1;
2132 
2133  ret = end_ebml_master_crc32(pb, &dyn_cp, mkv, MATROSKA_ID_CHAPTERS, 0, 0, 1);
2134  if (ret < 0)
2135  goto fail;
2136  if (dyn_tags)
2137  return end_ebml_master_crc32(pb, &dyn_tags, mkv,
2138  MATROSKA_ID_TAGS, 0, 0, 1);
2139  return 0;
2140 
2141 fail:
2142  if (tags) {
2143  /* tags == &mkv->tags.bc can only happen if mkv->tags.bc was
2144  * initially NULL, so we never free older tags. */
2145  ffio_free_dyn_buf(tags);
2146  }
2147  ffio_free_dyn_buf(&dyn_cp);
2148  return ret;
2149 }
2150 
2151 static const char *get_mimetype(const AVStream *st)
2152 {
2153  const AVDictionaryEntry *t;
2154 
2155  if (t = av_dict_get(st->metadata, "mimetype", NULL, 0))
2156  return t->value;
2157  if (st->codecpar->codec_id != AV_CODEC_ID_NONE) {
2159  if (desc && desc->mime_types) {
2160  return desc->mime_types[0];
2161  } else if (st->codecpar->codec_id == AV_CODEC_ID_TEXT)
2162  return "text/plain";
2163  }
2164 
2165  return NULL;
2166 }
2167 
2169 {
2170  MatroskaMuxContext *mkv = s->priv_data;
2171  AVIOContext *dyn_cp = NULL, *pb = s->pb;
2172  int i, ret;
2173 
2174  if (!mkv->nb_attachments)
2175  return 0;
2176 
2177  ret = start_ebml_master_crc32(&dyn_cp, mkv);
2178  if (ret < 0)
2179  return ret;
2180 
2181  for (i = 0; i < s->nb_streams; i++) {
2182  const AVStream *st = s->streams[i];
2183  mkv_track *track = &mkv->tracks[i];
2184  EBML_WRITER(6);
2185  const AVDictionaryEntry *t;
2186  const char *mimetype;
2187 
2189  continue;
2190 
2192 
2193  if (t = av_dict_get(st->metadata, "title", NULL, 0))
2195  if (!(t = av_dict_get(st->metadata, "filename", NULL, 0))) {
2196  av_log(s, AV_LOG_ERROR, "Attachment stream %d has no filename tag.\n", i);
2197  ffio_free_dyn_buf(&dyn_cp);
2198  return AVERROR(EINVAL);
2199  }
2201 
2202  mimetype = get_mimetype(st);
2203  av_assert0(mimetype);
2207  ebml_writer_add_uid(&writer, MATROSKA_ID_FILEUID, track->uid);
2208  ret = ebml_writer_write(&writer, dyn_cp);
2209  if (ret < 0) {
2210  ffio_free_dyn_buf(&dyn_cp);
2211  return ret;
2212  }
2213  }
2214  return end_ebml_master_crc32(pb, &dyn_cp, mkv,
2215  MATROSKA_ID_ATTACHMENTS, 0, 0, 1);
2216 }
2217 
2219 {
2220  const AVDictionaryEntry *duration = av_dict_get(s->metadata, "DURATION",
2221  NULL, 0);
2222  int64_t max = 0;
2223  int64_t us;
2224 
2225  if (duration && (av_parse_time(&us, duration->value, 1) == 0) && us > 0) {
2226  av_log(s, AV_LOG_DEBUG, "get_metadata_duration found duration in context metadata: %" PRId64 "\n", us);
2227  return us;
2228  }
2229 
2230  for (unsigned i = 0; i < s->nb_streams; i++) {
2231  int64_t us;
2232  duration = av_dict_get(s->streams[i]->metadata, "DURATION", NULL, 0);
2233 
2234  if (duration && (av_parse_time(&us, duration->value, 1) == 0))
2235  max = FFMAX(max, us);
2236  }
2237 
2238  av_log(s, AV_LOG_DEBUG, "get_metadata_duration returned: %" PRId64 "\n", max);
2239  return max;
2240 }
2241 
2243  const char *doctype, int version)
2244 {
2245  EBML_WRITER(8);
2251  ebml_writer_add_string(&writer, EBML_ID_DOCTYPE, doctype);
2254  /* The size is bounded, so no need to check this. */
2255  ebml_writer_write(&writer, pb);
2256 }
2257 
2259 {
2260  MatroskaMuxContext *mkv = s->priv_data;
2261  const AVDictionaryEntry *tag;
2262  int64_t creation_time;
2263  AVIOContext *pb;
2264  int ret = start_ebml_master_crc32(&mkv->info.bc, mkv);
2265  if (ret < 0)
2266  return ret;
2267  pb = mkv->info.bc;
2268 
2270  if ((tag = av_dict_get(s->metadata, "title", NULL, 0)))
2271  put_ebml_string(pb, MATROSKA_ID_TITLE, tag->value);
2272  if (!(s->flags & AVFMT_FLAG_BITEXACT)) {
2274  if ((tag = av_dict_get(s->metadata, "encoding_tool", NULL, 0)))
2276  else
2278 
2279  if (!IS_WEBM(mkv))
2281  } else {
2282  const char *ident = "Lavf";
2285  }
2286 
2287  if (ff_parse_creation_time_metadata(s, &creation_time, 0) > 0) {
2288  // Adjust time so it's relative to 2001-01-01 and convert to nanoseconds.
2289  int64_t date_utc = (creation_time - 978307200000000LL) * 1000;
2290  uint8_t date_utc_buf[8];
2291  AV_WB64(date_utc_buf, date_utc);
2292  put_ebml_binary(pb, MATROSKA_ID_DATEUTC, date_utc_buf, 8);
2293  }
2294 
2295  // reserve space for the duration
2296  mkv->duration = 0;
2297  mkv->duration_offset = avio_tell(pb);
2298  if (!mkv->is_live) {
2299  int64_t metadata_duration = get_metadata_duration(s);
2300 
2301  if (s->duration > 0) {
2302  int64_t scaledDuration = av_rescale(s->duration, 1000, AV_TIME_BASE);
2303  put_ebml_float(pb, MATROSKA_ID_DURATION, scaledDuration);
2304  av_log(s, AV_LOG_DEBUG, "Write early duration from recording time = %" PRIu64 "\n", scaledDuration);
2305  } else if (metadata_duration > 0) {
2306  int64_t scaledDuration = av_rescale(metadata_duration, 1000, AV_TIME_BASE);
2307  put_ebml_float(pb, MATROSKA_ID_DURATION, scaledDuration);
2308  av_log(s, AV_LOG_DEBUG, "Write early duration from metadata = %" PRIu64 "\n", scaledDuration);
2309  } else if (s->pb->seekable & AVIO_SEEKABLE_NORMAL) {
2310  put_ebml_void(pb, 11); // assumes double-precision float to be written
2311  }
2312  }
2313  return end_ebml_master_crc32_tentatively(s->pb, &mkv->info,
2314  mkv, MATROSKA_ID_INFO);
2315 }
2316 
2318 {
2319  MatroskaMuxContext *mkv = s->priv_data;
2320  AVIOContext *pb = s->pb;
2321  int ret, version = 2;
2322 
2323  if (!IS_WEBM(mkv) ||
2324  av_dict_get(s->metadata, "stereo_mode", NULL, 0) ||
2325  av_dict_get(s->metadata, "alpha_mode", NULL, 0))
2326  version = 4;
2327 
2328  for (unsigned i = 0; i < s->nb_streams; i++) {
2329  if (s->streams[i]->codecpar->codec_id == AV_CODEC_ID_OPUS ||
2330  av_dict_get(s->streams[i]->metadata, "stereo_mode", NULL, 0) ||
2331  av_dict_get(s->streams[i]->metadata, "alpha_mode", NULL, 0))
2332  version = 4;
2333  }
2334 
2335  ebml_write_header(pb, s->oformat->name, version);
2337  put_ebml_size_unknown(pb, 8);
2338  mkv->segment_offset = avio_tell(pb);
2339 
2340  // We write a SeekHead at the beginning to point to all other level
2341  // one elements (except Clusters).
2342  mkv_start_seekhead(mkv, pb);
2343 
2344  ret = mkv_write_info(s);
2345  if (ret < 0)
2346  return ret;
2347 
2348  ret = mkv_write_tracks(s);
2349  if (ret < 0)
2350  return ret;
2351 
2353  if (ret < 0)
2354  return ret;
2355 
2356  if (!IS_WEBM(mkv)) {
2358  if (ret < 0)
2359  return ret;
2360  }
2361 
2362  /* Must come after mkv_write_chapters() to write chapter tags
2363  * into the same Tags element as the other tags. */
2364  ret = mkv_write_tags(s);
2365  if (ret < 0)
2366  return ret;
2367 
2368  if (!IS_SEEKABLE(pb, mkv)) {
2369  ret = mkv_write_seekhead(pb, mkv, 0, avio_tell(pb));
2370  if (ret < 0)
2371  return ret;
2372  }
2373 
2374  if (s->metadata_header_padding > 0) {
2375  if (s->metadata_header_padding == 1)
2376  s->metadata_header_padding++;
2377  put_ebml_void(pb, s->metadata_header_padding);
2378  }
2379 
2380  if (mkv->reserve_cues_space || mkv->move_cues_to_front) {
2381  if (IS_SEEKABLE(pb, mkv)) {
2382  mkv->cues_pos = avio_tell(pb);
2383  if (mkv->reserve_cues_space >= 1) {
2384  if (mkv->reserve_cues_space == 1)
2385  mkv->reserve_cues_space++;
2387  }
2388  } else
2389  mkv->reserve_cues_space = -1;
2390  }
2391 
2392  mkv->cluster_pos = -1;
2393 
2394  // start a new cluster every 5 MB or 5 sec, or 32k / 1 sec for streaming or
2395  // after 4k and on a keyframe
2396  if (IS_SEEKABLE(pb, mkv)) {
2397  if (mkv->cluster_time_limit < 0)
2398  mkv->cluster_time_limit = 5000;
2399  if (mkv->cluster_size_limit < 0)
2400  mkv->cluster_size_limit = 5 * 1024 * 1024;
2401  } else {
2402  if (mkv->cluster_time_limit < 0)
2403  mkv->cluster_time_limit = 1000;
2404  if (mkv->cluster_size_limit < 0)
2405  mkv->cluster_size_limit = 32 * 1024;
2406  }
2407 
2408  return 0;
2409 }
2410 
2411 #if CONFIG_MATROSKA_MUXER
2412 static int mkv_reformat_h2645(MatroskaMuxContext *mkv, AVIOContext *pb,
2413  const AVPacket *pkt, int *size)
2414 {
2415  int ret;
2416  if (pb) {
2418  } else {
2420  if (ret < 0)
2421  return ret;
2422  *size = ret;
2423  }
2424  return 0;
2425 }
2426 
2427 static int mkv_reformat_wavpack(MatroskaMuxContext *mkv, AVIOContext *pb,
2428  const AVPacket *pkt, int *size)
2429 {
2430  const uint8_t *src = pkt->data;
2431  int srclen = pkt->size;
2432  int offset = 0;
2433  int ret;
2434 
2435  while (srclen >= WV_HEADER_SIZE) {
2436  WvHeader header;
2437 
2439  if (ret < 0)
2440  return ret;
2441  src += WV_HEADER_SIZE;
2442  srclen -= WV_HEADER_SIZE;
2443 
2444  if (srclen < header.blocksize)
2445  return AVERROR_INVALIDDATA;
2446 
2447  offset += 4 * !!header.initial + 8 + 4 * !(header.initial && header.final);
2448  if (pb) {
2449  if (header.initial)
2450  avio_wl32(pb, header.samples);
2451  avio_wl32(pb, header.flags);
2452  avio_wl32(pb, header.crc);
2453 
2454  if (!(header.initial && header.final))
2455  avio_wl32(pb, header.blocksize);
2456 
2457  avio_write(pb, src, header.blocksize);
2458  }
2459  src += header.blocksize;
2460  srclen -= header.blocksize;
2461  offset += header.blocksize;
2462  }
2463  *size = offset;
2464 
2465  return 0;
2466 }
2467 #endif
2468 
2470  const AVPacket *pkt, int *size)
2471 {
2472  int ret = ff_av1_filter_obus(pb, pkt->data, pkt->size);
2473  if (ret < 0)
2474  return ret;
2475  *size = ret;
2476  return 0;
2477 }
2478 
2480  const AVPacket *pkt, int *size)
2481 {
2482  const uint8_t *id, *settings;
2483  size_t id_size, settings_size;
2484  unsigned total = pkt->size + 2U;
2485 
2486  if (total > INT_MAX)
2487  return AVERROR(ERANGE);
2488 
2490  &id_size);
2492  &settings_size);
2493  if (id_size > INT_MAX - total || settings_size > INT_MAX - (total += id_size))
2494  return AVERROR(ERANGE);
2495  *size = total += settings_size;
2496  if (pb) {
2497  avio_write(pb, id, id_size);
2498  avio_w8(pb, '\n');
2499  avio_write(pb, settings, settings_size);
2500  avio_w8(pb, '\n');
2501  avio_write(pb, pkt->data, pkt->size);
2502  }
2503  return 0;
2504 }
2505 
2506 static int mkv_write_block(void *logctx, MatroskaMuxContext *mkv,
2507  AVIOContext *pb, const AVCodecParameters *par,
2508  mkv_track *track, const AVPacket *pkt,
2509  int keyframe, int64_t ts, uint64_t duration,
2510  int force_blockgroup, int64_t relative_packet_pos)
2511 {
2512  uint8_t *side_data;
2513  size_t side_data_size;
2514  uint64_t additional_id;
2515  int64_t discard_padding = 0;
2516  unsigned track_number = track->track_num;
2517  EBML_WRITER(9);
2518 
2519  mkv->cur_block.track = track;
2520  mkv->cur_block.pkt = pkt;
2521  mkv->cur_block.rel_ts = ts - mkv->cluster_pts;
2522  mkv->cur_block.flags = 0;
2523 
2524  /* Open a BlockGroup with a Block now; it will later be converted
2525  * to a SimpleBlock if possible. */
2527  ebml_writer_add_block(&writer, mkv);
2528 
2529  if (duration)
2531 
2532  av_log(logctx, AV_LOG_DEBUG,
2533  "Writing block of size %d with pts %" PRId64 ", dts %" PRId64 ", "
2534  "duration %" PRId64 " at relative offset %" PRId64 " in cluster "
2535  "at offset %" PRId64 ". TrackNumber %u, keyframe %d\n",
2536  pkt->size, pkt->pts, pkt->dts, pkt->duration, relative_packet_pos,
2537  mkv->cluster_pos, track_number, keyframe != 0);
2538 
2539  side_data = av_packet_get_side_data(pkt,
2541  &side_data_size);
2542  if (side_data && side_data_size >= 10) {
2543  discard_padding = av_rescale_q(AV_RL32(side_data + 4),
2544  (AVRational){1, par->sample_rate},
2545  (AVRational){1, 1000000000});
2546  ebml_writer_add_sint(&writer, MATROSKA_ID_DISCARDPADDING, discard_padding);
2547  }
2548 
2549  side_data = av_packet_get_side_data(pkt,
2551  &side_data_size);
2552  if (side_data && side_data_size >= 8 &&
2553  // Only the Codec-specific BlockMore (id == 1) is currently supported.
2554  (additional_id = AV_RB64(side_data)) == 1) {
2557  /* Until dbc50f8a our demuxer used a wrong default value
2558  * of BlockAddID, so we write it unconditionally. */
2559  ebml_writer_add_uint(&writer, MATROSKA_ID_BLOCKADDID, additional_id);
2561  side_data + 8, side_data_size - 8);
2562  ebml_writer_close_master(&writer);
2563  ebml_writer_close_master(&writer);
2564  }
2565 
2566  if (!force_blockgroup && writer.nb_elements == 2) {
2567  /* Nothing except the BlockGroup + Block. Can use a SimpleBlock. */
2568  writer.elements++; // Skip the BlockGroup.
2569  writer.nb_elements--;
2570  av_assert2(writer.elements[0].id == MATROSKA_ID_BLOCK);
2571  writer.elements[0].id = MATROSKA_ID_SIMPLEBLOCK;
2572  if (keyframe)
2573  mkv->cur_block.flags |= 1 << 7;
2574  } else if (!keyframe)
2576  track->last_timestamp - ts);
2577 
2578  return ebml_writer_write(&writer, pb);
2579 }
2580 
2582 {
2583  MatroskaMuxContext *mkv = s->priv_data;
2584  int ret;
2585 
2586  if (!mkv->have_video) {
2587  for (unsigned i = 0; i < s->nb_streams; i++)
2588  mkv->tracks[i].has_cue = 0;
2589  }
2590  mkv->cluster_pos = -1;
2591  ret = end_ebml_master_crc32(s->pb, &mkv->cluster_bc, mkv,
2592  MATROSKA_ID_CLUSTER, 0, 1, 0);
2593  if (ret < 0)
2594  return ret;
2595 
2597  return 0;
2598 }
2599 
2601 {
2602  MatroskaMuxContext *mkv = s->priv_data;
2603  mkv_track *track = &mkv->tracks[pkt->stream_index];
2604  AVCodecParameters *par = s->streams[pkt->stream_index]->codecpar;
2605  uint8_t *side_data;
2606  size_t side_data_size;
2607  int ret;
2608 
2610  &side_data_size);
2611 
2612  switch (par->codec_id) {
2613 #if CONFIG_MATROSKA_MUXER
2614  case AV_CODEC_ID_AAC:
2615  if (side_data_size && mkv->track.bc) {
2616  int filler, output_sample_rate = 0;
2617  ret = get_aac_sample_rates(s, mkv, side_data, side_data_size,
2618  &track->sample_rate, &output_sample_rate);
2619  if (ret < 0)
2620  return ret;
2621  if (!output_sample_rate)
2622  output_sample_rate = track->sample_rate; // Space is already reserved, so it's this or a void element.
2623  ret = ff_alloc_extradata(par, side_data_size);
2624  if (ret < 0)
2625  return ret;
2626  memcpy(par->extradata, side_data, side_data_size);
2627  avio_seek(mkv->track.bc, track->codecpriv_offset, SEEK_SET);
2628  mkv_write_codecprivate(s, mkv->track.bc, par, 1, 0);
2629  filler = MAX_PCE_SIZE + 2 + 4 - (avio_tell(mkv->track.bc) - track->codecpriv_offset);
2630  if (filler)
2631  put_ebml_void(mkv->track.bc, filler);
2632  avio_seek(mkv->track.bc, track->sample_rate_offset, SEEK_SET);
2634  put_ebml_float(mkv->track.bc, MATROSKA_ID_AUDIOOUTSAMPLINGFREQ, output_sample_rate);
2635  } else if (!par->extradata_size && !track->sample_rate) {
2636  // No extradata (codecpar or packet side data).
2637  av_log(s, AV_LOG_ERROR, "Error parsing AAC extradata, unable to determine samplerate.\n");
2638  return AVERROR(EINVAL);
2639  }
2640  break;
2641  case AV_CODEC_ID_FLAC:
2642  if (side_data_size && mkv->track.bc) {
2643  uint8_t *old_extradata = par->extradata;
2644  if (side_data_size != par->extradata_size) {
2645  av_log(s, AV_LOG_ERROR, "Invalid FLAC STREAMINFO metadata for output stream %d\n",
2646  pkt->stream_index);
2647  return AVERROR(EINVAL);
2648  }
2649  par->extradata = side_data;
2650  avio_seek(mkv->track.bc, track->codecpriv_offset, SEEK_SET);
2651  mkv_write_codecprivate(s, mkv->track.bc, par, 1, 0);
2652  par->extradata = old_extradata;
2653  }
2654  break;
2655 #endif
2656  // FIXME: Remove the following once libaom starts propagating extradata during init()
2657  // See https://bugs.chromium.org/p/aomedia/issues/detail?id=2012
2658  case AV_CODEC_ID_AV1:
2659  if (side_data_size && mkv->track.bc && !par->extradata_size) {
2660  AVIOContext *dyn_cp;
2661  uint8_t *codecpriv;
2662  int codecpriv_size;
2663  ret = avio_open_dyn_buf(&dyn_cp);
2664  if (ret < 0)
2665  return ret;
2666  ff_isom_write_av1c(dyn_cp, side_data, side_data_size);
2667  codecpriv_size = avio_get_dyn_buf(dyn_cp, &codecpriv);
2668  if ((ret = dyn_cp->error) < 0 ||
2669  !codecpriv_size && (ret = AVERROR_INVALIDDATA)) {
2670  ffio_free_dyn_buf(&dyn_cp);
2671  return ret;
2672  }
2673  avio_seek(mkv->track.bc, track->codecpriv_offset, SEEK_SET);
2674  // Do not write the OBUs as we don't have space saved for them
2675  put_ebml_binary(mkv->track.bc, MATROSKA_ID_CODECPRIVATE, codecpriv, 4);
2676  ffio_free_dyn_buf(&dyn_cp);
2677  ret = ff_alloc_extradata(par, side_data_size);
2678  if (ret < 0)
2679  return ret;
2680  memcpy(par->extradata, side_data, side_data_size);
2681  } else if (!par->extradata_size)
2682  return AVERROR_INVALIDDATA;
2683  break;
2684  default:
2685  if (side_data_size)
2686  av_log(s, AV_LOG_DEBUG, "Ignoring new extradata in a packet for stream %d.\n", pkt->stream_index);
2687  break;
2688  }
2689 
2690  return 0;
2691 }
2692 
2694 {
2695  MatroskaMuxContext *mkv = s->priv_data;
2696  AVIOContext *pb;
2697  AVCodecParameters *par = s->streams[pkt->stream_index]->codecpar;
2698  mkv_track *track = &mkv->tracks[pkt->stream_index];
2699  int is_sub = par->codec_type == AVMEDIA_TYPE_SUBTITLE;
2700  /* All subtitle blocks are considered to be keyframes. */
2701  int keyframe = is_sub || !!(pkt->flags & AV_PKT_FLAG_KEY);
2702  int64_t duration = FFMAX(pkt->duration, 0);
2703  int64_t write_duration = is_sub ? duration : 0;
2704  int ret;
2705  int64_t ts = track->write_dts ? pkt->dts : pkt->pts;
2706  int64_t relative_packet_pos;
2707 
2708  if (ts == AV_NOPTS_VALUE) {
2709  av_log(s, AV_LOG_ERROR, "Can't write packet with unknown timestamp\n");
2710  return AVERROR(EINVAL);
2711  }
2712  ts += track->ts_offset;
2713 
2714  if (mkv->cluster_pos != -1) {
2715  int64_t cluster_time = ts - mkv->cluster_pts;
2716  if ((int16_t)cluster_time != cluster_time) {
2717  ret = mkv_end_cluster(s);
2718  if (ret < 0)
2719  return ret;
2720  av_log(s, AV_LOG_WARNING, "Starting new cluster due to timestamp\n");
2721  }
2722  }
2723 
2724  if (mkv->cluster_pos == -1) {
2725  ret = start_ebml_master_crc32(&mkv->cluster_bc, mkv);
2726  if (ret < 0)
2727  return ret;
2728  mkv->cluster_bc->direct = 1;
2729  mkv->cluster_pos = avio_tell(s->pb);
2731  mkv->cluster_pts = FFMAX(0, ts);
2733  "Starting new cluster with timestamp "
2734  "%" PRId64 " at offset %" PRId64 " bytes\n",
2735  mkv->cluster_pts, mkv->cluster_pos);
2736  }
2737  pb = mkv->cluster_bc;
2738 
2739  relative_packet_pos = avio_tell(pb);
2740 
2741  /* The WebM spec requires WebVTT to be muxed in BlockGroups;
2742  * so we force it even for packets without duration. */
2743  ret = mkv_write_block(s, mkv, pb, par, track, pkt,
2744  keyframe, ts, write_duration,
2745  par->codec_id == AV_CODEC_ID_WEBVTT,
2746  relative_packet_pos);
2747  if (ret < 0)
2748  return ret;
2749  if (keyframe && IS_SEEKABLE(s->pb, mkv) &&
2750  (par->codec_type == AVMEDIA_TYPE_VIDEO ||
2752  !mkv->have_video && !track->has_cue)) {
2753  ret = mkv_add_cuepoint(mkv, pkt->stream_index, ts,
2754  mkv->cluster_pos, relative_packet_pos,
2755  write_duration);
2756  if (ret < 0)
2757  return ret;
2758  track->has_cue = 1;
2759  }
2760 
2761  track->last_timestamp = ts;
2762  mkv->duration = FFMAX(mkv->duration, ts + duration);
2763  track->duration = FFMAX(track->duration, ts + duration);
2764 
2765  return 0;
2766 }
2767 
2769 {
2770  MatroskaMuxContext *mkv = s->priv_data;
2771  int codec_type = s->streams[pkt->stream_index]->codecpar->codec_type;
2772  int keyframe = !!(pkt->flags & AV_PKT_FLAG_KEY);
2773  int cluster_size;
2774  int64_t cluster_time;
2775  int ret;
2776  int start_new_cluster;
2777 
2779  if (ret < 0)
2780  return ret;
2781 
2782  if (mkv->cluster_pos != -1) {
2783  if (mkv->tracks[pkt->stream_index].write_dts)
2784  cluster_time = pkt->dts - mkv->cluster_pts;
2785  else
2786  cluster_time = pkt->pts - mkv->cluster_pts;
2787  cluster_time += mkv->tracks[pkt->stream_index].ts_offset;
2788 
2789  cluster_size = avio_tell(mkv->cluster_bc);
2790 
2791  if (mkv->is_dash && codec_type == AVMEDIA_TYPE_VIDEO) {
2792  // WebM DASH specification states that the first block of
2793  // every Cluster has to be a key frame. So for DASH video,
2794  // we only create a Cluster on seeing key frames.
2795  start_new_cluster = keyframe;
2796  } else if (mkv->is_dash && codec_type == AVMEDIA_TYPE_AUDIO &&
2797  cluster_time > mkv->cluster_time_limit) {
2798  // For DASH audio, we create a Cluster based on cluster_time_limit.
2799  start_new_cluster = 1;
2800  } else if (!mkv->is_dash &&
2801  (cluster_size > mkv->cluster_size_limit ||
2802  cluster_time > mkv->cluster_time_limit ||
2803  (codec_type == AVMEDIA_TYPE_VIDEO && keyframe &&
2804  cluster_size > 4 * 1024))) {
2805  start_new_cluster = 1;
2806  } else
2807  start_new_cluster = 0;
2808 
2809  if (start_new_cluster) {
2810  ret = mkv_end_cluster(s);
2811  if (ret < 0)
2812  return ret;
2813  }
2814  }
2815 
2816  if (!mkv->cluster_pos)
2817  avio_write_marker(s->pb,
2818  av_rescale_q(pkt->dts, s->streams[pkt->stream_index]->time_base, AV_TIME_BASE_Q),
2820 
2821  // check if we have an audio packet cached
2822  if (mkv->cur_audio_pkt->size > 0) {
2825  if (ret < 0) {
2827  "Could not write cached audio packet ret:%d\n", ret);
2828  return ret;
2829  }
2830  }
2831 
2832  // buffer an audio packet to ensure the packet containing the video
2833  // keyframe's timecode is contained in the same cluster for WebM
2834  if (codec_type == AVMEDIA_TYPE_AUDIO) {
2835  if (pkt->size > 0)
2837  } else
2839  return ret;
2840 }
2841 
2843 {
2844  MatroskaMuxContext *mkv = s->priv_data;
2845 
2846  if (!pkt) {
2847  if (mkv->cluster_pos != -1) {
2848  int ret = mkv_end_cluster(s);
2849  if (ret < 0)
2850  return ret;
2852  "Flushing cluster at offset %" PRIu64 " bytes\n",
2853  avio_tell(s->pb));
2854  }
2855  return 1;
2856  }
2857  return mkv_write_packet(s, pkt);
2858 }
2859 
2861 {
2862  MatroskaMuxContext *mkv = s->priv_data;
2863  AVIOContext *pb = s->pb;
2864  int64_t endpos, ret64;
2865  int ret, ret2 = 0;
2866 
2867  // check if we have an audio packet cached
2868  if (mkv->cur_audio_pkt->size > 0) {
2870  if (ret < 0) {
2872  "Could not write cached audio packet ret:%d\n", ret);
2873  return ret;
2874  }
2875  }
2876 
2877  if (mkv->cluster_pos != -1) {
2878  ret = end_ebml_master_crc32(pb, &mkv->cluster_bc, mkv,
2879  MATROSKA_ID_CLUSTER, 0, 0, 0);
2880  if (ret < 0)
2881  return ret;
2882  }
2883 
2885  if (ret < 0)
2886  return ret;
2887 
2888  if (!IS_SEEKABLE(pb, mkv))
2889  return 0;
2890 
2891  endpos = avio_tell(pb);
2892 
2893  if (mkv->cues.num_entries && mkv->reserve_cues_space >= 0) {
2894  AVIOContext *cues = NULL;
2895  uint64_t size, offset = 0;
2896  int length_size = 0;
2897 
2898 redo_cues:
2899  ret = start_ebml_master_crc32(&cues, mkv);
2900  if (ret < 0)
2901  return ret;
2902 
2903  ret = mkv_assemble_cues(s->streams, cues, &mkv->cues,
2904  mkv->tracks, s->nb_streams, offset);
2905  if (ret < 0) {
2906  ffio_free_dyn_buf(&cues);
2907  return ret;
2908  }
2909 
2910  if (mkv->reserve_cues_space || mkv->move_cues_to_front) {
2911  size = avio_tell(cues);
2912  length_size = ebml_length_size(size);
2913  size += 4 + length_size;
2914  if (offset + mkv->reserve_cues_space < size) {
2915  if (mkv->move_cues_to_front) {
2916  offset = size - mkv->reserve_cues_space;
2917  ffio_reset_dyn_buf(cues);
2918  goto redo_cues;
2919  }
2921  "Insufficient space reserved for Cues: "
2922  "%d < %"PRIu64". No Cues will be output.\n",
2923  mkv->reserve_cues_space, size);
2924  ret2 = AVERROR(EINVAL);
2925  goto after_cues;
2926  } else {
2927  if (offset) {
2929  offset);
2930  if (ret < 0) {
2931  ffio_free_dyn_buf(&cues);
2932  return ret;
2933  }
2934  endpos += offset;
2935  }
2936  if ((ret64 = avio_seek(pb, mkv->cues_pos, SEEK_SET)) < 0) {
2937  ffio_free_dyn_buf(&cues);
2938  return ret64;
2939  }
2940  if (mkv->reserve_cues_space == size + 1) {
2941  /* There is no way to reserve a single byte because
2942  * the minimal size of an EBML Void element is 2
2943  * (1 byte ID, 1 byte length field). This problem
2944  * is solved by writing the Cues' length field on
2945  * one byte more than necessary. */
2946  length_size++;
2947  size++;
2948  }
2949  }
2950  }
2951  ret = end_ebml_master_crc32(pb, &cues, mkv, MATROSKA_ID_CUES,
2952  length_size, 0, 1);
2953  if (ret < 0)
2954  return ret;
2955  if (mkv->reserve_cues_space) {
2956  if (size < mkv->reserve_cues_space)
2958  } else if (!mkv->move_cues_to_front)
2959  endpos = avio_tell(pb);
2960  }
2961 
2962 after_cues:
2963  /* Lengths greater than (1ULL << 56) - 1 can't be represented
2964  * via an EBML number, so leave the unknown length field. */
2965  if (endpos - mkv->segment_offset < (1ULL << 56) - 1) {
2966  if ((ret64 = avio_seek(pb, mkv->segment_offset - 8, SEEK_SET)) < 0)
2967  return ret64;
2968  put_ebml_length(pb, endpos - mkv->segment_offset, 8);
2969  }
2970 
2971  ret = mkv_write_seekhead(pb, mkv, 1, mkv->info.pos);
2972  if (ret < 0)
2973  return ret;
2974 
2975  if (mkv->info.bc) {
2976  // update the duration
2977  av_log(s, AV_LOG_DEBUG, "end duration = %" PRIu64 "\n", mkv->duration);
2978  avio_seek(mkv->info.bc, mkv->duration_offset, SEEK_SET);
2980  ret = end_ebml_master_crc32(pb, &mkv->info.bc, mkv,
2981  MATROSKA_ID_INFO, 0, 0, 0);
2982  if (ret < 0)
2983  return ret;
2984  }
2985 
2986  if (mkv->track.bc) {
2987  // write Tracks master
2988  avio_seek(pb, mkv->track.pos, SEEK_SET);
2989  ret = end_ebml_master_crc32(pb, &mkv->track.bc, mkv,
2990  MATROSKA_ID_TRACKS, 0, 0, 0);
2991  if (ret < 0)
2992  return ret;
2993  }
2994 
2995  // update stream durations
2996  if (mkv->tags.bc) {
2997  int i;
2998  for (i = 0; i < s->nb_streams; ++i) {
2999  const AVStream *st = s->streams[i];
3000  const mkv_track *track = &mkv->tracks[i];
3001 
3002  if (track->duration_offset > 0) {
3003  double duration_sec = track->duration * av_q2d(st->time_base);
3004  char duration_string[20] = "";
3005 
3006  av_log(s, AV_LOG_DEBUG, "stream %d end duration = %" PRIu64 "\n", i,
3007  track->duration);
3008 
3009  avio_seek(mkv->tags.bc, track->duration_offset, SEEK_SET);
3010 
3011  snprintf(duration_string, 20, "%02d:%02d:%012.9f",
3012  (int) duration_sec / 3600, ((int) duration_sec / 60) % 60,
3013  fmod(duration_sec, 60));
3014 
3015  put_ebml_binary(mkv->tags.bc, MATROSKA_ID_TAGSTRING, duration_string, 20);
3016  }
3017  }
3018 
3019  avio_seek(pb, mkv->tags.pos, SEEK_SET);
3020  ret = end_ebml_master_crc32(pb, &mkv->tags.bc, mkv,
3021  MATROSKA_ID_TAGS, 0, 0, 0);
3022  if (ret < 0)
3023  return ret;
3024  }
3025 
3026  avio_seek(pb, endpos, SEEK_SET);
3027 
3028  return ret2;
3029 }
3030 
3031 static uint64_t mkv_get_uid(const mkv_track *tracks, int i, AVLFG *c)
3032 {
3033  while (1) {
3034  uint64_t uid;
3035  int k;
3036  uid = (uint64_t)av_lfg_get(c) << 32;
3037  uid |= av_lfg_get(c);
3038  if (!uid)
3039  continue;
3040  for (k = 0; k < i; k++) {
3041  if (tracks[k].uid == uid)
3042  break;
3043  }
3044  if (k == i)
3045  return uid;
3046  }
3047 }
3048 
3049 static int mkv_init(struct AVFormatContext *s)
3050 {
3051  FFFormatContext *const si = ffformatcontext(s);
3052  MatroskaMuxContext *mkv = s->priv_data;
3053  AVLFG c;
3054  unsigned nb_tracks = 0;
3055  int i;
3056 
3057  mkv->ctx = s;
3058 
3059  for (i = 0; i < s->nb_streams; i++) {
3060  if (s->streams[i]->codecpar->codec_id == AV_CODEC_ID_ATRAC3 ||
3061  s->streams[i]->codecpar->codec_id == AV_CODEC_ID_COOK ||
3062  s->streams[i]->codecpar->codec_id == AV_CODEC_ID_RA_288 ||
3063  s->streams[i]->codecpar->codec_id == AV_CODEC_ID_SIPR ||
3064  s->streams[i]->codecpar->codec_id == AV_CODEC_ID_RV10 ||
3065  s->streams[i]->codecpar->codec_id == AV_CODEC_ID_RV20) {
3067  "The Matroska muxer does not yet support muxing %s\n",
3068  avcodec_get_name(s->streams[i]->codecpar->codec_id));
3069  return AVERROR_PATCHWELCOME;
3070  }
3071  }
3072 
3073  if (s->avoid_negative_ts < 0) {
3074  s->avoid_negative_ts = 1;
3075  si->avoid_negative_ts_use_pts = 1;
3076  }
3077 
3078  if (!CONFIG_MATROSKA_MUXER ||
3079  (CONFIG_WEBM_MUXER && !strcmp(s->oformat->name, "webm"))) {
3080  mkv->mode = MODE_WEBM;
3081  mkv->write_crc = 0;
3082  } else
3083  mkv->mode = MODE_MATROSKAv2;
3084 
3086 
3087  mkv->tracks = av_calloc(s->nb_streams, sizeof(*mkv->tracks));
3088  if (!mkv->tracks)
3089  return AVERROR(ENOMEM);
3090 
3091  if (!(s->flags & AVFMT_FLAG_BITEXACT)) {
3093 
3094  // Calculate the SegmentUID now in order not to waste our random seed.
3095  for (i = 0; i < 4; i++)
3096  mkv->segment_uid[i] = av_lfg_get(&c);
3097  }
3098 
3099  for (i = 0; i < s->nb_streams; i++) {
3100  AVStream *st = s->streams[i];
3101  const AVCodecParameters *const par = st->codecpar;
3102  mkv_track *track = &mkv->tracks[i];
3103 
3104  switch (par->codec_id) {
3105 #if CONFIG_MATROSKA_MUXER
3106  case AV_CODEC_ID_WAVPACK:
3107  track->reformat = mkv_reformat_wavpack;
3108  break;
3109  case AV_CODEC_ID_H264:
3110  case AV_CODEC_ID_HEVC:
3111  if ((par->codec_id == AV_CODEC_ID_H264 && par->extradata_size > 0 ||
3112  par->codec_id == AV_CODEC_ID_HEVC && par->extradata_size > 6) &&
3113  (AV_RB24(par->extradata) == 1 || AV_RB32(par->extradata) == 1))
3114  track->reformat = mkv_reformat_h2645;
3115  break;
3116  case AV_CODEC_ID_PRORES:
3117  /* Matroska specification requires to remove
3118  * the first QuickTime atom. */
3119  track->offset = 8;
3120  break;
3121 #endif
3122  case AV_CODEC_ID_AV1:
3123  track->reformat = mkv_reformat_av1;
3124  break;
3125  case AV_CODEC_ID_WEBVTT:
3126  track->reformat = webm_reformat_vtt;
3127  break;
3128  }
3129 
3130  if (s->flags & AVFMT_FLAG_BITEXACT) {
3131  track->uid = i + 1;
3132  } else {
3133  track->uid = mkv_get_uid(mkv->tracks, i, &c);
3134  }
3135 
3136  // ms precision is the de-facto standard timescale for mkv files
3137  avpriv_set_pts_info(st, 64, 1, 1000);
3138 
3140  if (IS_WEBM(mkv)) {
3141  av_log(s, AV_LOG_WARNING, "Stream %d will be ignored "
3142  "as WebM doesn't support attachments.\n", i);
3143  } else if (!get_mimetype(st)) {
3144  av_log(s, AV_LOG_ERROR, "Attachment stream %d has no mimetype "
3145  "tag and it cannot be deduced from the codec id.\n", i);
3146  return AVERROR(EINVAL);
3147  }
3148  mkv->nb_attachments++;
3149  continue;
3150  }
3151 
3152  nb_tracks++;
3153  track->track_num = mkv->is_dash ? mkv->dash_track_number : nb_tracks;
3154  track->track_num_size = ebml_num_size(track->track_num);
3155  }
3156 
3157  if (mkv->is_dash && nb_tracks != 1)
3158  return AVERROR(EINVAL);
3159 
3160  return 0;
3161 }
3162 
3164  const AVPacket *pkt)
3165 {
3166  int ret = 1;
3167 
3168  if (CONFIG_MATROSKA_MUXER && st->codecpar->codec_id == AV_CODEC_ID_AAC) {
3169  if (pkt->size > 2 && (AV_RB16(pkt->data) & 0xfff0) == 0xfff0)
3170  ret = ff_stream_add_bitstream_filter(st, "aac_adtstoasc", NULL);
3171  } else if (st->codecpar->codec_id == AV_CODEC_ID_VP9) {
3172  ret = ff_stream_add_bitstream_filter(st, "vp9_superframe", NULL);
3173  }
3174 
3175  return ret;
3176 }
3177 
3179  { AV_CODEC_ID_ALAC, 0XFFFFFFFF },
3180  { AV_CODEC_ID_MLP, 0xFFFFFFFF },
3181  { AV_CODEC_ID_OPUS, 0xFFFFFFFF },
3182  { AV_CODEC_ID_PCM_S16BE, 0xFFFFFFFF },
3183  { AV_CODEC_ID_PCM_S24BE, 0xFFFFFFFF },
3184  { AV_CODEC_ID_PCM_S32BE, 0xFFFFFFFF },
3185  { AV_CODEC_ID_QDMC, 0xFFFFFFFF },
3186  { AV_CODEC_ID_QDM2, 0xFFFFFFFF },
3187  { AV_CODEC_ID_RA_144, 0xFFFFFFFF },
3188  { AV_CODEC_ID_RA_288, 0xFFFFFFFF },
3189  { AV_CODEC_ID_COOK, 0xFFFFFFFF },
3190  { AV_CODEC_ID_TRUEHD, 0xFFFFFFFF },
3191  { AV_CODEC_ID_NONE, 0xFFFFFFFF }
3192 };
3193 
3195  { AV_CODEC_ID_RV10, 0xFFFFFFFF },
3196  { AV_CODEC_ID_RV20, 0xFFFFFFFF },
3197  { AV_CODEC_ID_RV30, 0xFFFFFFFF },
3198  { AV_CODEC_ID_NONE, 0xFFFFFFFF }
3199 };
3200 
3202  { AV_CODEC_ID_DVB_SUBTITLE, 0xFFFFFFFF },
3203  { AV_CODEC_ID_DVD_SUBTITLE, 0xFFFFFFFF },
3204  { AV_CODEC_ID_HDMV_PGS_SUBTITLE, 0xFFFFFFFF },
3205  { AV_CODEC_ID_NONE, 0xFFFFFFFF }
3206 };
3207 
3208 #define OFFSET(x) offsetof(MatroskaMuxContext, x)
3209 #define FLAGS AV_OPT_FLAG_ENCODING_PARAM
3210 static const AVOption options[] = {
3211  { "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 },
3212  { "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 },
3213  { "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 },
3214  { "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 },
3215  { "dash", "Create a WebM file conforming to WebM DASH specification", OFFSET(is_dash), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
3216  { "dash_track_number", "Track number for the DASH stream", OFFSET(dash_track_number), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, INT_MAX, FLAGS },
3217  { "live", "Write files assuming it is a live stream.", OFFSET(is_live), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
3218  { "allow_raw_vfw", "allow RAW VFW mode", OFFSET(allow_raw_vfw), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
3219  { "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 },
3220  { "write_crc32", "write a CRC32 element inside every Level 1 element", OFFSET(write_crc), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, FLAGS },
3221  { "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" },
3222  { "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" },
3223  { "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" },
3224  { "passthrough", "Use the disposition flag as-is", 0, AV_OPT_TYPE_CONST, { .i64 = DEFAULT_MODE_PASSTHROUGH }, 0, 0, FLAGS, "default_mode" },
3225  { NULL },
3226 };
3227 
3229  .class_name = "matroska/webm muxer",
3230  .item_name = av_default_item_name,
3231  .option = options,
3232  .version = LIBAVUTIL_VERSION_INT,
3233 };
3234 
3235 #if CONFIG_MATROSKA_MUXER
3236 static int mkv_query_codec(enum AVCodecID codec_id, int std_compliance)
3237 {
3238  for (int i = 0; ff_mkv_codec_tags[i].id != AV_CODEC_ID_NONE; i++)
3239  if (ff_mkv_codec_tags[i].id == codec_id)
3240  return 1;
3241 
3242  if (std_compliance < FF_COMPLIANCE_NORMAL) {
3244  // mkv theoretically supports any video/audio through VFW/ACM
3246  return 1;
3247  }
3248 
3249  return 0;
3250 }
3251 
3253  .name = "matroska",
3254  .long_name = NULL_IF_CONFIG_SMALL("Matroska"),
3255  .mime_type = "video/x-matroska",
3256  .extensions = "mkv",
3257  .priv_data_size = sizeof(MatroskaMuxContext),
3258  .audio_codec = CONFIG_LIBVORBIS_ENCODER ?
3260  .video_codec = CONFIG_LIBX264_ENCODER ?
3262  .init = mkv_init,
3263  .deinit = mkv_deinit,
3269  .codec_tag = (const AVCodecTag* const []){
3272  },
3273  .subtitle_codec = AV_CODEC_ID_ASS,
3274  .query_codec = mkv_query_codec,
3275  .check_bitstream = mkv_check_bitstream,
3276  .priv_class = &matroska_webm_class,
3277 };
3278 #endif
3279 
3280 #if CONFIG_WEBM_MUXER
3281 static int webm_query_codec(enum AVCodecID codec_id, int std_compliance)
3282 {
3283  for (int i = 0; ff_webm_codec_tags[i].id != AV_CODEC_ID_NONE; i++)
3284  if (ff_webm_codec_tags[i].id == codec_id)
3285  return 1;
3286 
3287  return 0;
3288 }
3289 
3290 const AVOutputFormat ff_webm_muxer = {
3291  .name = "webm",
3292  .long_name = NULL_IF_CONFIG_SMALL("WebM"),
3293  .mime_type = "video/webm",
3294  .extensions = "webm",
3295  .priv_data_size = sizeof(MatroskaMuxContext),
3296  .audio_codec = CONFIG_LIBOPUS_ENCODER ? AV_CODEC_ID_OPUS : AV_CODEC_ID_VORBIS,
3297  .video_codec = CONFIG_LIBVPX_VP9_ENCODER? AV_CODEC_ID_VP9 : AV_CODEC_ID_VP8,
3298  .subtitle_codec = AV_CODEC_ID_WEBVTT,
3299  .init = mkv_init,
3300  .deinit = mkv_deinit,
3304  .query_codec = webm_query_codec,
3308  .priv_class = &matroska_webm_class,
3309 };
3310 #endif
3311 
3312 #if CONFIG_MATROSKA_AUDIO_MUXER
3314  .name = "matroska",
3315  .long_name = NULL_IF_CONFIG_SMALL("Matroska Audio"),
3316  .mime_type = "audio/x-matroska",
3317  .extensions = "mka",
3318  .priv_data_size = sizeof(MatroskaMuxContext),
3319  .audio_codec = CONFIG_LIBVORBIS_ENCODER ?
3321  .video_codec = AV_CODEC_ID_NONE,
3322  .init = mkv_init,
3323  .deinit = mkv_deinit,
3330  .codec_tag = (const AVCodecTag* const []){
3332  },
3333  .priv_class = &matroska_webm_class,
3334 };
3335 #endif
MatroskaMuxContext::reserve_cues_space
int reserve_cues_space
Definition: matroskaenc.c:225
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:424
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:101
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:226
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:231
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:1485
IS_SEEKABLE
#define IS_SEEKABLE(pb, mkv)
Definition: matroskaenc.c:75
mkv_init
static int mkv_init(struct AVFormatContext *s)
Definition: matroskaenc.c:3049
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:74
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:46
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:281
AV_CODEC_ID_AC3
@ AV_CODEC_ID_AC3
Definition: codec_id.h:426
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:128
mkv_write_packet
static int mkv_write_packet(AVFormatContext *s, const AVPacket *pkt)
Definition: matroskaenc.c:2768
AVOutputFormat::name
const char * name
Definition: avformat.h:504
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:2198
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:268
ebml_writer_add_string
static void ebml_writer_add_string(EbmlWriter *writer, uint32_t id, const char *str)
Definition: matroskaenc.c:460
mkv_write_packet_internal
static int mkv_write_packet_internal(AVFormatContext *s, const AVPacket *pkt)
Definition: matroskaenc.c:2693
MatroskaMuxContext::allow_raw_vfw
int allow_raw_vfw
Definition: matroskaenc.c:233
ebml_writer_add_float
static void ebml_writer_add_float(EbmlWriter *writer, uint32_t id, double val)
Definition: matroskaenc.c:480
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
AVSphericalMapping::projection
enum AVSphericalProjection projection
Projection type.
Definition: spherical.h:86
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:170
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:555
MatroskaMuxContext::segment_offset
int64_t segment_offset
Definition: matroskaenc.c:204
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:210
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:52
EBML_ID_EBMLMAXSIZELENGTH
#define EBML_ID_EBMLMAXSIZELENGTH
Definition: matroska.h:39
ffformatcontext
static av_always_inline FFFormatContext * ffformatcontext(AVFormatContext *s)
Definition: internal.h:188
mkv_new_chapter_ids_needed
static int mkv_new_chapter_ids_needed(const AVFormatContext *s)
Definition: matroskaenc.c:2056
AVCodecParameters::color_space
enum AVColorSpace color_space
Definition: codec_par.h:149
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:738
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:478
avcodec_get_type
enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
Get the type of the given codec.
Definition: codec_desc.c:3546
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:116
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:183
MATROSKA_ID_AUDIOSAMPLINGFREQ
#define MATROSKA_ID_AUDIOSAMPLINGFREQ
Definition: matroska.h:170
MatroskaMuxContext::cluster_bc
AVIOContext * cluster_bc
Definition: matroskaenc.c:205
OPUS_SEEK_PREROLL
#define OPUS_SEEK_PREROLL
Seek preroll value for opus.
Definition: matroskaenc.c:249
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:119
MODE_MATROSKAv2
#define MODE_MATROSKAv2
Definition: matroskaenc.c:70
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:216
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:495
MATROSKA_ID_DURATION
#define MATROSKA_ID_DURATION
Definition: matroska.h:67
DEFAULT_MODE_INFER
@ DEFAULT_MODE_INFER
Definition: matroskaenc.c:79
MATROSKA_ID_VIDEOCOLORCHROMASITINGVERT
#define MATROSKA_ID_VIDEOCOLORCHROMASITINGVERT
Definition: matroska.h:142
BlockContext::pkt
const AVPacket * pkt
Definition: matroskaenc.c:108
avlanguage.h
av_strcasecmp
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
Definition: avstring.c:215
MATROSKA_ID_VIDEOCOLORPRIMARIES
#define MATROSKA_ID_VIDEOCOLORPRIMARIES
Definition: matroska.h:146
AV_CODEC_ID_RAWVIDEO
@ AV_CODEC_ID_RAWVIDEO
Definition: codec_id.h:63
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:1097
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:820
AV_CODEC_ID_MPEG4
@ AV_CODEC_ID_MPEG4
Definition: codec_id.h:62
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:232
AV_CODEC_ID_RA_144
@ AV_CODEC_ID_RA_144
Definition: codec_id.h:410
ebml_writer_write
static int ebml_writer_write(EbmlWriter *writer, AVIOContext *pb)
Definition: matroskaenc.c:704
MATROSKA_ID_CHAPTERS
#define MATROSKA_ID_CHAPTERS
Definition: matroska.h:63
AVCOL_TRC_NB
@ AVCOL_TRC_NB
Not part of ABI.
Definition: pixfmt.h:516
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:26
ebml_writer_elem_write
static int ebml_writer_elem_write(const EbmlElement *elem, AVIOContext *pb)
Definition: matroskaenc.c:661
MATROSKA_ID_VIDEOCOLOR_BY
#define MATROSKA_ID_VIDEOCOLOR_BY
Definition: matroska.h:156
deinit
static void deinit(AVFormatContext *s)
Definition: chromaprint.c:50
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:373
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:798
AVOption
AVOption.
Definition: opt.h:247
AVCOL_SPC_NB
@ AVCOL_SPC_NB
Not part of ABI.
Definition: pixfmt.h:540
AVCOL_TRC_UNSPECIFIED
@ AVCOL_TRC_UNSPECIFIED
Definition: pixfmt.h:497
MatroskaMuxContext::mode
int mode
Definition: matroskaenc.c:200
AVStream::avg_frame_rate
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:1015
mkv_track::duration_offset
int64_t duration_offset
Definition: matroskaenc.c:184
data
const char data[16]
Definition: mxf.c:143
MATROSKA_VIDEO_INTERLACE_FLAG_INTERLACED
@ MATROSKA_VIDEO_INTERLACE_FLAG_INTERLACED
Definition: matroska.h:300
MatroskaMuxContext::cluster_pts
int64_t cluster_pts
Definition: matroskaenc.c:207
mkv_write_attachments
static int mkv_write_attachments(AVFormatContext *s)
Definition: matroskaenc.c:2168
AV_CODEC_ID_ALAC
@ AV_CODEC_ID_ALAC
Definition: codec_id.h:439
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:251
flacenc.h
AVIOContext::error
int error
contains the error code or 0 if no error happened
Definition: avio.h:240
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: utils.c:1865
MatroskaMuxContext::tags
ebml_stored_master tags
Definition: matroskaenc.c:203
AV_DICT_IGNORE_SUFFIX
#define AV_DICT_IGNORE_SUFFIX
Return first entry in a dictionary whose first part corresponds to the search key,...
Definition: dict.h:68
EBML_UID
@ EBML_UID
Definition: matroskaenc.c:98
MATROSKA_VIDEO_STEREOMODE_TYPE_BOTH_EYES_BLOCK_LR
@ MATROSKA_VIDEO_STEREOMODE_TYPE_BOTH_EYES_BLOCK_LR
Definition: matroska.h:327
matroska.h
ff_codec_wav_tags
const AVCodecTag ff_codec_wav_tags[]
Definition: riff.c:509
mkv_track::ts_offset
int64_t ts_offset
Definition: matroskaenc.c:186
put_ebml_binary
static void put_ebml_binary(AVIOContext *pb, uint32_t elementid, const void *buf, int size)
Definition: matroskaenc.c:361
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:391
AVCodecParameters::codec_tag
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: codec_par.h:64
ff_isom_write_av1c
int ff_isom_write_av1c(AVIOContext *pb, const uint8_t *buf, int size)
Writes AV1 extradata (Sequence Header and Metadata OBUs) to the provided AVIOContext.
Definition: av1.c:364
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:72
MAX_VIDEO_COLOR_ELEMS
#define MAX_VIDEO_COLOR_ELEMS
Definition: matroskaenc.c:1203
max
#define max(a, b)
Definition: cuda_runtime.h:33
BlockContext
Definition: matroskaenc.c:106
mathematics.h
MATROSKA_ID_CUES
#define MATROSKA_ID_CUES
Definition: matroska.h:58
AVDictionary
Definition: dict.c:30
AV_CODEC_ID_FLAC
@ AV_CODEC_ID_FLAC
Definition: codec_id.h:435
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:73
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:228
MATROSKA_ID_CUERELATIVEPOSITION
#define MATROSKA_ID_CUERELATIVEPOSITION
Definition: matroska.h:209
MAX_PCE_SIZE
#define MAX_PCE_SIZE
Maximum size of a PCE including the 3-bit ID_PCE.
Definition: mpeg4audio.h:120
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:528
AV_SPHERICAL_EQUIRECTANGULAR
@ AV_SPHERICAL_EQUIRECTANGULAR
Video represents a sphere mapped on a flat surface using equirectangular projection.
Definition: spherical.h:56
intfloat.h
codec_type
enum AVMediaType codec_type
Definition: rtp.c:37
MatroskaMuxContext::flipped_raw_rgb
int flipped_raw_rgb
Definition: matroskaenc.c:234
MATROSKA_ID_CHAPLANG
#define MATROSKA_ID_CHAPLANG
Definition: matroska.h:268
AV_CODEC_ID_TRUEHD
@ AV_CODEC_ID_TRUEHD
Definition: codec_id.h:467
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:1483
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:428
sample_rate
sample_rate
Definition: ffmpeg_filter.c:153
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:213
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:120
xiph.h
MatroskaMuxContext::cluster_size_limit
int cluster_size_limit
Definition: matroskaenc.c:226
NALUList::nalus
NALU * nalus
Definition: avc.h:34
init
static int init
Definition: av_tx.c:47
MATROSKA_VIDEO_DISPLAYUNIT_UNKNOWN
@ MATROSKA_VIDEO_DISPLAYUNIT_UNKNOWN
Definition: matroska.h:337
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:32
crc.h
avio_wl16
void avio_wl16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:466
AVCodecParameters::color_primaries
enum AVColorPrimaries color_primaries
Definition: codec_par.h:147
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:490
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:67
avcodec_enum_to_chroma_pos
int avcodec_enum_to_chroma_pos(int *xpos, int *ypos, enum AVChromaLocation pos)
Converts AVChromaLocation to swscale x/y chroma position.
Definition: utils.c:352
start_ebml_master
static ebml_master start_ebml_master(AVIOContext *pb, uint32_t elementid, uint64_t expectedsize)
Definition: matroskaenc.c:399
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:544
AVCodecParameters::channels
int channels
Audio only.
Definition: codec_par.h:166
MATROSKA_ID_AUDIOOUTSAMPLINGFREQ
#define MATROSKA_ID_AUDIOOUTSAMPLINGFREQ
Definition: matroska.h:171
MAX_SUPPORTED_EBML_LENGTH
#define MAX_SUPPORTED_EBML_LENGTH
Definition: matroskaenc.c:68
AV_FIELD_TT
@ AV_FIELD_TT
Definition: codec_par.h:39
mpeg4audio.h
MATROSKA_ID_VIDEOPROJECTIONPRIVATE
#define MATROSKA_ID_VIDEOPROJECTIONPRIVATE
Definition: matroska.h:164
AV_CODEC_ID_PCM_S16BE
@ AV_CODEC_ID_PCM_S16BE
Definition: codec_id.h:315
U
#define U(x)
Definition: vp56_arith.h:37
mkv_cues
Definition: matroskaenc.c:166
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:236
fail
#define fail()
Definition: checkasm.h:128
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:115
ebml_writer_sint_len
static int ebml_writer_sint_len(EbmlElement *elem)
Definition: matroskaenc.c:546
EbmlElement::sint
int64_t sint
Definition: matroskaenc.c:126
AV_STEREO3D_2D
@ AV_STEREO3D_2D
Video is not stereoscopic (and metadata has to be there).
Definition: stereo3d.h:55
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:598
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:341
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:504
ff_matroska_video_stereo_mode
const char *const ff_matroska_video_stereo_mode[MATROSKA_VIDEO_STEREOMODE_TYPE_NB]
Definition: matroska.c:128
AVChapter
Definition: avformat.h:1159
query_codec
static int query_codec(enum AVCodecID id, int std_compliance)
Definition: img2enc.c:230
AV_DISPOSITION_FORCED
#define AV_DISPOSITION_FORCED
Track should be used during playback by default.
Definition: avformat.h:853
val
static double val(void *priv, double ch)
Definition: aeval.c:76
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
MATROSKA_ID_VIDEOALPHAMODE
#define MATROSKA_ID_VIDEOALPHAMODE
Definition: matroska.h:130
scale
static av_always_inline float scale(float x, float s)
Definition: vf_v360.c:1388
pts
static int64_t pts
Definition: transcode_aac.c:653
us
#define us(width, name, range_min, range_max, subs,...)
Definition: cbs_h2645.c:278
EBML_ID_DOCTYPE
#define EBML_ID_DOCTYPE
Definition: matroska.h:40
AV_FIELD_TB
@ AV_FIELD_TB
Definition: codec_par.h:41
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
mkv_write_tag_targets
static int mkv_write_tag_targets(MatroskaMuxContext *mkv, AVIOContext **pb, ebml_master *tag, uint32_t elementid, uint64_t uid)
Definition: matroskaenc.c:1912
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:454
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:523
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:580
AV_CODEC_ID_SIPR
@ AV_CODEC_ID_SIPR
Definition: codec_id.h:464
AVCodecParameters::color_trc
enum AVColorTransferCharacteristic color_trc
Definition: codec_par.h:148
MATROSKA_ID_BLKADDIDNAME
#define MATROSKA_ID_BLKADDIDNAME
Definition: matroska.h:195
EbmlElement::priv
union EbmlElement::@267 priv
mkv_write_tracks
static int mkv_write_tracks(AVFormatContext *s)
Definition: matroskaenc.c:1823
MatroskaMuxContext::ctx
AVFormatContext * ctx
Definition: matroskaenc.c:198
mkv_seekhead_entry
Definition: matroskaenc.c:146
MATROSKA_ID_BLOCKADDITIONAL
#define MATROSKA_ID_BLOCKADDITIONAL
Definition: matroska.h:243
AV_STEREO3D_FRAMESEQUENCE
@ AV_STEREO3D_FRAMESEQUENCE
Views are alternated temporally.
Definition: stereo3d.h:92
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:202
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:51
MATROSKA_ID_TAGS
#define MATROSKA_ID_TAGS
Definition: matroska.h:59
AV_CODEC_ID_TTA
@ AV_CODEC_ID_TTA
Definition: codec_id.h:445
options
static const AVOption options[]
Definition: matroskaenc.c:3210
duration
int64_t duration
Definition: movenc.c:64
put_ebml_id
static void put_ebml_id(AVIOContext *pb, uint32_t id)
Definition: matroskaenc.c:256
DEFAULT_MODE_INFER_NO_SUBS
@ DEFAULT_MODE_INFER_NO_SUBS
Definition: matroskaenc.c:80
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:40
avio_open_dyn_buf
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1471
mkv_cuepoint::relative_pos
int64_t relative_pos
relative offset from the position of the cluster containing the block
Definition: matroskaenc.c:162
AV_CODEC_ID_ADPCM_G726
@ AV_CODEC_ID_ADPCM_G726
Definition: codec_id.h:364
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:581
AV_STEREO3D_LINES
@ AV_STEREO3D_LINES
Views are packed per line, as if interlaced.
Definition: stereo3d.h:129
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:409
MATROSKA_ID_FILEUID
#define MATROSKA_ID_FILEUID
Definition: matroska.h:259
s
#define s(width, name)
Definition: cbs_vp9.c:257
AVCHROMA_LOC_TOP
@ AVCHROMA_LOC_TOP
Definition: pixfmt.h:621
MATROSKA_VIDEO_INTERLACE_FLAG_PROGRESSIVE
@ MATROSKA_VIDEO_INTERLACE_FLAG_PROGRESSIVE
Definition: matroska.h:301
EbmlElement::bin
const uint8_t * bin
Definition: matroskaenc.c:129
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:114
AVCOL_PRI_NB
@ AVCOL_PRI_NB
Not part of ABI.
Definition: pixfmt.h:487
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:174
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
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:224
AV_PKT_DATA_MASTERING_DISPLAY_METADATA
@ AV_PKT_DATA_MASTERING_DISPLAY_METADATA
Mastering display metadata (based on SMPTE-2086:2014).
Definition: packet.h:222
mkv_write_flush_packet
static int mkv_write_flush_packet(AVFormatContext *s, AVPacket *pkt)
Definition: matroskaenc.c:2842
ebml_writer_add_block
static void ebml_writer_add_block(EbmlWriter *writer, MatroskaMuxContext *mkv)
Definition: matroskaenc.c:509
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:206
AVDictionaryEntry::key
char * key
Definition: dict.h:80
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:168
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AV_CODEC_ID_VP9
@ AV_CODEC_ID_VP9
Definition: codec_id.h:218
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:126
ebml_writer_open_master
static void ebml_writer_open_master(EbmlWriter *writer, uint32_t id)
Definition: matroskaenc.c:427
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:488
MATROSKA_ID_TIMECODESCALE
#define MATROSKA_ID_TIMECODESCALE
Definition: matroska.h:66
lfg.h
get_mimetype
static const char * get_mimetype(const AVStream *st)
Definition: matroskaenc.c:2151
ebml_writer_add_sint
static void ebml_writer_add_sint(EbmlWriter *writer, uint32_t id, int64_t val)
Definition: matroskaenc.c:502
MatroskaMuxContext::write_crc
int write_crc
Definition: matroskaenc.c:228
matroska_webm_class
static const AVClass matroska_webm_class
Definition: matroskaenc.c:3228
AV_OPT_TYPE_INT64
@ AV_OPT_TYPE_INT64
Definition: opt.h:225
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:3178
MATROSKA_ID_SEEKENTRY
#define MATROSKA_ID_SEEKENTRY
Definition: matroska.h:229
mkv_track::track_num
unsigned track_num
Definition: matroskaenc.c:177
AV_FIELD_UNKNOWN
@ AV_FIELD_UNKNOWN
Definition: codec_par.h:37
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:438
AVDOVIDecoderConfigurationRecord::dv_profile
uint8_t dv_profile
Definition: dovi_meta.h:55
additional_video_tags
static const AVCodecTag additional_video_tags[]
Definition: matroskaenc.c:3194
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:80
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:1393
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:141
sint_size
static av_const int sint_size(int64_t val)
Definition: matroskaenc.c:540
MatroskaMuxContext::cluster_time_limit
int64_t cluster_time_limit
Definition: matroskaenc.c:227
EbmlElement::type
EbmlType type
Definition: matroskaenc.c:121
EbmlElement::f
double f
Definition: matroskaenc.c:127
MATROSKA_ID_INFO
#define MATROSKA_ID_INFO
Definition: matroska.h:56
MatroskaMuxContext::have_video
int have_video
Definition: matroskaenc.c:220
MATROSKA_ID_AUDIOCHANNELS
#define MATROSKA_ID_AUDIOCHANNELS
Definition: matroska.h:174
codec_id
enum AVCodecID codec_id
Definition: vaapi_decode.c:369
AV_CODEC_ID_SVQ3
@ AV_CODEC_ID_SVQ3
Definition: codec_id.h:73
EbmlType
EbmlType
Definition: matroskaenc.c:94
key
const char * key
Definition: hwcontext_opencl.c:168
AVCOL_PRI_UNSPECIFIED
@ AVCOL_PRI_UNSPECIFIED
Definition: pixfmt.h:472
AV_FIELD_BT
@ AV_FIELD_BT
Definition: codec_par.h:42
AV_CODEC_ID_H264
@ AV_CODEC_ID_H264
Definition: codec_id.h:77
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:826
ff_webm_codec_tags
const CodecTags ff_webm_codec_tags[]
Definition: matroska.c:106
mkv_track::uid
uint64_t uid
Definition: matroskaenc.c:176
AVStereo3D::flags
int flags
Additional information about the frame packing.
Definition: stereo3d.h:185
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:73
BlockContext::track
struct mkv_track * track
Definition: matroskaenc.c:107
AV_CODEC_ID_WEBVTT
@ AV_CODEC_ID_WEBVTT
Definition: codec_id.h:540
EBML_ID_DOCTYPEREADVERSION
#define EBML_ID_DOCTYPEREADVERSION
Definition: matroska.h:42
mkv_cues::entries
mkv_cuepoint * entries
Definition: matroskaenc.c:167
AVFormatContext
Format I/O context.
Definition: avformat.h:1200
internal.h
DEFAULT_MODE_PASSTHROUGH
@ DEFAULT_MODE_PASSTHROUGH
Definition: matroskaenc.c:81
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:1095
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
mkv_write_codecprivate
static int mkv_write_codecprivate(AVFormatContext *s, AVIOContext *pb, AVCodecParameters *par, int native_id, int qt_id)
Definition: matroskaenc.c:1133
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:135
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:965
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:55
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:279
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:294
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:98
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:30
MatroskaMuxContext::info
ebml_stored_master info
Definition: matroskaenc.c:201
AV_DISPOSITION_COMMENT
#define AV_DISPOSITION_COMMENT
The stream is a commentary track.
Definition: avformat.h:838
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:93
AV_CODEC_ID_DVD_SUBTITLE
@ AV_CODEC_ID_DVD_SUBTITLE
Definition: codec_id.h:522
MAX_VIDEO_PROJECTION_ELEMS
#define MAX_VIDEO_PROJECTION_ELEMS
Definition: matroskaenc.c:1286
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
ff_stream_add_bitstream_filter
int ff_stream_add_bitstream_filter(AVStream *st, const char *name, const char *args)
Add a bitstream filter to a stream.
Definition: utils.c:1789
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
MatroskaMuxContext::segment_uid
uint32_t segment_uid[4]
Definition: matroskaenc.c:238
MatroskaMuxContext::cur_block
BlockContext cur_block
Definition: matroskaenc.c:215
AVStream::metadata
AVDictionary * metadata
Definition: avformat.h:1006
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:303
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:895
EBML_STR
@ EBML_STR
Definition: matroskaenc.c:99
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
mkv_track::codecpriv_offset
int64_t codecpriv_offset
Definition: matroskaenc.c:185
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:714
AV_DISPOSITION_ORIGINAL
#define AV_DISPOSITION_ORIGINAL
The stream is in original language.
Definition: avformat.h:834
mkv_track::sample_rate_offset
int64_t sample_rate_offset
Definition: matroskaenc.c:181
AV_CODEC_ID_QDM2
@ AV_CODEC_ID_QDM2
Definition: codec_id.h:442
mkv_get_uid
static uint64_t mkv_get_uid(const mkv_track *tracks, int i, AVLFG *c)
Definition: matroskaenc.c:3031
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:419
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:432
get_metadata_duration
static int64_t get_metadata_duration(AVFormatContext *s)
Definition: matroskaenc.c:2218
mkv_track::has_cue
int has_cue
Definition: matroskaenc.c:175
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:138
MATROSKA_ID_TRACKAUDIO
#define MATROSKA_ID_TRACKAUDIO
Definition: matroska.h:82
AVCOL_RANGE_UNSPECIFIED
@ AVCOL_RANGE_UNSPECIFIED
Definition: pixfmt.h:563
EbmlElement::str
const char * str
Definition: matroskaenc.c:128
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:219
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:170
put_ebml_float
static void put_ebml_float(AVIOContext *pb, uint32_t elementid, double val)
Definition: matroskaenc.c:354
uint_size
static av_const int uint_size(uint64_t val)
Definition: matroskaenc.c:525
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:47
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:89
AVCodecParameters::extradata_size
int extradata_size
Size of the extradata content in bytes.
Definition: codec_par.h:78
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:29
AV_CODEC_ID_AAC
@ AV_CODEC_ID_AAC
Definition: codec_id.h:425
mkv_write_header
static int mkv_write_header(AVFormatContext *s)
Definition: matroskaenc.c:2317
MatroskaMuxContext
Definition: matroskaenc.c:196
mkv_cuepoint::duration
int64_t duration
duration of the block according to time base
Definition: matroskaenc.c:163
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:115
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:468
AV_DISPOSITION_CAPTIONS
#define AV_DISPOSITION_CAPTIONS
The subtitle stream contains captions, providing a transcription and possibly a translation of audio.
Definition: avformat.h:884
MATROSKA_ID_AUDIOBITDEPTH
#define MATROSKA_ID_AUDIOBITDEPTH
Definition: matroska.h:173
AV_CODEC_ID_FFV1
@ AV_CODEC_ID_FFV1
Definition: codec_id.h:83
AVLFG
Context structure for the Lagged Fibonacci PRNG.
Definition: lfg.h:33
mkv_track::offset
unsigned offset
Definition: matroskaenc.c:180
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:161
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:65
ff_matroska_muxer
const AVOutputFormat ff_matroska_muxer
put_xiph_codecpriv
static int put_xiph_codecpriv(AVFormatContext *s, AVIOContext *pb, const AVCodecParameters *par)
Definition: matroskaenc.c:969
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:1505
AV_STEREO3D_CHECKERBOARD
@ AV_STEREO3D_CHECKERBOARD
Views are packed in a checkerboard-like structure per pixel.
Definition: stereo3d.h:104
AVMediaType
AVMediaType
Definition: avutil.h:199
MATROSKA_ID_TRACKFLAGLACING
#define MATROSKA_ID_TRACKFLAGLACING
Definition: matroska.h:106
AVPacket::size
int size
Definition: packet.h:374
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:117
id
enum AVCodecID id
Definition: extract_extradata_bsf.c:325
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:357
master
const char * master
Definition: vf_curves.c:119
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:169
mkv_write_field_order
static void mkv_write_field_order(EbmlWriter *writer, int is_webm, enum AVFieldOrder field_order)
Definition: matroskaenc.c:1353
EBML_FLOAT
@ EBML_FLOAT
Definition: matroskaenc.c:97
mkv_write_native_codecprivate
static int mkv_write_native_codecprivate(AVFormatContext *s, AVIOContext *pb, const AVCodecParameters *par, AVIOContext *dyn_cp)
Definition: matroskaenc.c:1079
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:736
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:86
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:62
mkv_cuepoint::pts
uint64_t pts
Definition: matroskaenc.c:159
ebml_stored_master::bc
AVIOContext * bc
Definition: matroskaenc.c:90
EbmlWriter::current_master_element
int current_master_element
Definition: matroskaenc.c:137
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
AV_CODEC_ID_QDMC
@ AV_CODEC_ID_QDMC
Definition: codec_id.h:473
put_ebml_void
static void put_ebml_void(AVIOContext *pb, int size)
Write a void element of a given size.
Definition: matroskaenc.c:381
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:123
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:484
format
ofilter format
Definition: ffmpeg_filter.c:172
AV_CODEC_ID_OPUS
@ AV_CODEC_ID_OPUS
Definition: codec_id.h:483
AVCHROMA_LOC_UNSPECIFIED
@ AVCHROMA_LOC_UNSPECIFIED
Definition: pixfmt.h:617
AV_DISPOSITION_DUB
#define AV_DISPOSITION_DUB
The stream is not in original language.
Definition: avformat.h:828
MatroskaMuxContext::cur_audio_pkt
AVPacket * cur_audio_pkt
Definition: matroskaenc.c:217
EBML_UINT
@ EBML_UINT
Definition: matroskaenc.c:95
AVStream::sample_aspect_ratio
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:1004
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:122
start_ebml_master_crc32
static int start_ebml_master_crc32(AVIOContext **dyn_cp, MatroskaMuxContext *mkv)
Definition: matroskaenc.c:725
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:857
MATROSKA_ID_BLKADDIDEXTRADATA
#define MATROSKA_ID_BLKADDIDEXTRADATA
Definition: matroska.h:197
CASE
#define CASE(type, variable)
AVPacket::dts
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:372
MAX_FIELD_ORDER_ELEMS
#define MAX_FIELD_ORDER_ELEMS
Definition: matroskaenc.c:1352
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:394
AVSphericalMapping::padding
uint32_t padding
Number of pixels to pad from the edge of each cube face.
Definition: spherical.h:182
additional_subtitle_tags
static const AVCodecTag additional_subtitle_tags[]
Definition: matroskaenc.c:3201
EbmlElement::mkv
struct MatroskaMuxContext * mkv
used by EBML_BLOCK
Definition: matroskaenc.c:130
avio_wl32
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:386
av_crc_get_table
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition: crc.c:374
AV_CODEC_ID_RV30
@ AV_CODEC_ID_RV30
Definition: codec_id.h:118
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
AV_PKT_DATA_CONTENT_LIGHT_LEVEL
@ AV_PKT_DATA_CONTENT_LIGHT_LEVEL
Content light level (based on CTA-861.3).
Definition: packet.h:235
BlockContext::flags
uint8_t flags
Definition: matroskaenc.c:110
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:379
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:203
version
version
Definition: libkvazaar.c:313
mkv_write_trailer
static int mkv_write_trailer(AVFormatContext *s)
Definition: matroskaenc.c:2860
AV_STEREO3D_FLAG_INVERT
#define AV_STEREO3D_FLAG_INVERT
Inverted views, Right/Bottom represents the left view.
Definition: stereo3d.h:167
MATROSKA_ID_VIDEOPIXELHEIGHT
#define MATROSKA_ID_VIDEOPIXELHEIGHT
Definition: matroska.h:121
FF_COMPLIANCE_NORMAL
#define FF_COMPLIANCE_NORMAL
Definition: avcodec.h:1283
mkv_write_tags
static int mkv_write_tags(AVFormatContext *s)
Definition: matroskaenc.c:1983
AV_CODEC_ID_SVQ1
@ AV_CODEC_ID_SVQ1
Definition: codec_id.h:72
mkv_cuepoint::stream_idx
int stream_idx
Definition: matroskaenc.c:160
EBML_MASTER
@ EBML_MASTER
Definition: matroskaenc.c:103
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:1204
MatroskaMuxContext::duration
int64_t duration
Definition: matroskaenc.c:209
MATROSKA_ID_EDITIONFLAGDEFAULT
#define MATROSKA_ID_EDITIONFLAGDEFAULT
Definition: matroska.h:272
write_packet
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
Definition: ffmpeg.c:727
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:443
AV_FIELD_BB
@ AV_FIELD_BB
Definition: codec_par.h:40
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:243
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:1067
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:84
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:114
AV_CODEC_ID_RA_288
@ AV_CODEC_ID_RA_288
Definition: codec_id.h:411
ebml_stored_master::pos
int64_t pos
Definition: matroskaenc.c:91
AVSphericalMapping::roll
int32_t roll
Rotation around the forward vector [-180, 180].
Definition: spherical.h:128
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:269
AV_CODEC_ID_RV10
@ AV_CODEC_ID_RV10
Definition: codec_id.h:55
AVFMT_GLOBALHEADER
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition: avformat.h:474
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:48
AVOutputFormat
Definition: avformat.h:503
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:366
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:106
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:316
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:253
AV_CODEC_ID_THEORA
@ AV_CODEC_ID_THEORA
Definition: codec_id.h:80
av_builtin_constant_p
#define av_builtin_constant_p
Definition: attributes.h:160
EbmlElement::uint
uint64_t uint
Definition: matroskaenc.c:125
AVCodecParameters::height
int height
Definition: codec_par.h:127
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:161
mkv_seekhead
Definition: matroskaenc.c:151
MATROSKA_ID_CUETRACK
#define MATROSKA_ID_CUETRACK
Definition: matroska.h:207
mkv_seekhead::entries
mkv_seekhead_entry entries[MAX_SEEKHEAD_ENTRIES]
Definition: matroskaenc.c:153
MATROSKA_ID_SEEKPOSITION
#define MATROSKA_ID_SEEKPOSITION
Definition: matroska.h:233
AV_STEREO3D_TOPBOTTOM
@ AV_STEREO3D_TOPBOTTOM
Views are on top of each other.
Definition: stereo3d.h:79
mkv_assemble_cues
static int mkv_assemble_cues(AVStream **streams, AVIOContext *dyn_cp, const mkv_cues *cues, mkv_track *tracks, int num_tracks, uint64_t offset)
Definition: matroskaenc.c:919
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:120
MATROSKA_ID_TRACKLANGUAGE
#define MATROSKA_ID_TRACKLANGUAGE
Definition: matroska.h:97
AV_CODEC_ID_RV20
@ AV_CODEC_ID_RV20
Definition: codec_id.h:56
AV_CODEC_ID_HEVC
@ AV_CODEC_ID_HEVC
Definition: codec_id.h:224
ebml_master::pos
int64_t pos
absolute offset in the containing AVIOContext where the master's elements start
Definition: matroskaenc.c:85
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:141
AV_CODEC_ID_PCM_S32BE
@ AV_CODEC_ID_PCM_S32BE
Definition: codec_id.h:323
EBML_ID_CRC32
#define EBML_ID_CRC32
Definition: matroska.h:46
ff_mkv_metadata_conv
const AVMetadataConv ff_mkv_metadata_conv[]
Definition: matroska.c:122
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:156
AVCodecParameters::color_range
enum AVColorRange color_range
Video only.
Definition: codec_par.h:146
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:526
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:128
mkv_check_tag_name
static int mkv_check_tag_name(const char *name, uint32_t elementid)
Definition: matroskaenc.c:1932
MPEG4AudioConfig::ext_sample_rate
int ext_sample_rate
Definition: mpeg4audio.h:40
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:271
mkv_reformat_av1
static int mkv_reformat_av1(MatroskaMuxContext *mkv, AVIOContext *pb, const AVPacket *pkt, int *size)
Definition: matroskaenc.c:2469
ebml_writer_close_or_discard_master
static void ebml_writer_close_or_discard_master(EbmlWriter *writer)
Definition: matroskaenc.c:449
AVCodecParameters::field_order
enum AVFieldOrder field_order
Video only.
Definition: codec_par.h:141
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:2581
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:485
ebml_write_header
static void ebml_write_header(AVIOContext *pb, const char *doctype, int version)
Definition: matroskaenc.c:2242
av_cmp_q
static int av_cmp_q(AVRational a, AVRational b)
Compare two rationals.
Definition: rational.h:89
OFFSET
#define OFFSET(x)
Definition: matroskaenc.c:3208
BlockContext::h2645_nalu_list
NALUList h2645_nalu_list
Definition: matroskaenc.c:111
AVStream::disposition
int disposition
Stream disposition - a combination of AV_DISPOSITION_* flags.
Definition: avformat.h:995
AV_DISPOSITION_VISUAL_IMPAIRED
#define AV_DISPOSITION_VISUAL_IMPAIRED
The stream is intended for visually impaired audiences.
Definition: avformat.h:861
tag
uint32_t tag
Definition: movenc.c:1596
ffio_free_dyn_buf
void ffio_free_dyn_buf(AVIOContext **s)
Free a dynamic buffer.
Definition: aviobuf.c:1544
AVFMT_FLAG_BITEXACT
#define AVFMT_FLAG_BITEXACT
When muxing, try to avoid writing any random/volatile data to the output.
Definition: avformat.h:1335
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:935
mkv_track::sample_rate
int sample_rate
Definition: matroskaenc.c:179
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:283
mkv_write_block
static int mkv_write_block(void *logctx, MatroskaMuxContext *mkv, AVIOContext *pb, const AVCodecParameters *par, mkv_track *track, const AVPacket *pkt, int keyframe, int64_t ts, uint64_t duration, int force_blockgroup, int64_t relative_packet_pos)
Definition: matroskaenc.c:2506
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:260
mkv_seekhead_entry::elementid
uint32_t elementid
Definition: matroskaenc.c:147
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:71
AV_STEREO3D_COLUMNS
@ AV_STEREO3D_COLUMNS
Views are packed per column.
Definition: stereo3d.h:141
AVSphericalMapping::pitch
int32_t pitch
Rotation around the right vector [-90, 90].
Definition: spherical.h:127
ff_av1_filter_obus
int ff_av1_filter_obus(AVIOContext *pb, const uint8_t *buf, int size)
Filter out AV1 OBUs not meant to be present in ISOBMFF sample data and write the resulting bitstream ...
Definition: av1.c:81
AVStereo3D::type
enum AVStereo3DType type
How views are packed within the video.
Definition: stereo3d.h:180
mkv_track::last_timestamp
int64_t last_timestamp
Definition: matroskaenc.c:182
MATROSKA_ID_VIDEOCOLOR_BX
#define MATROSKA_ID_VIDEOCOLOR_BX
Definition: matroska.h:155
MatroskaMuxContext::wrote_tags
int wrote_tags
Definition: matroskaenc.c:223
MATROSKA_ID_TAGSTRING
#define MATROSKA_ID_TAGSTRING
Definition: matroska.h:217
mkv_seekhead_entry::segmentpos
uint64_t segmentpos
Definition: matroskaenc.c:148
put_ebml_string
static void put_ebml_string(AVIOContext *pb, uint32_t elementid, const char *str)
Definition: matroskaenc.c:369
MATROSKA_ID_VIDEOCOLORMATRIXCOEFF
#define MATROSKA_ID_VIDEOCOLORMATRIXCOEFF
Definition: matroska.h:135
pos
unsigned int pos
Definition: spdifenc.c:412
avformat.h
av_stream_get_side_data
uint8_t * av_stream_get_side_data(const AVStream *stream, enum AVPacketSideDataType type, size_t *size)
Get side information from stream.
Definition: utils.c:1721
dict.h
AV_CODEC_ID_TEXT
@ AV_CODEC_ID_TEXT
raw UTF-8 text
Definition: codec_id.h:524
MATROSKA_ID_SIMPLEBLOCK
#define MATROSKA_ID_SIMPLEBLOCK
Definition: matroska.h:244
mkv_check_tag
static int mkv_check_tag(const AVDictionary *m, uint32_t elementid)
Definition: matroskaenc.c:1972
AV_DISPOSITION_DESCRIPTIONS
#define AV_DISPOSITION_DESCRIPTIONS
The subtitle stream contains a textual description of the video content.
Definition: avformat.h:890
ebml_writer_str_len
static int ebml_writer_str_len(EbmlElement *elem)
Definition: matroskaenc.c:515
EBML_SINT
@ EBML_SINT
Definition: matroskaenc.c:96
FLAGS
#define FLAGS
Definition: matroskaenc.c:3209
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
AVCodecParameters::chroma_location
enum AVChromaLocation chroma_location
Definition: codec_par.h:150
mkv_deinit
static void mkv_deinit(AVFormatContext *s)
Free the members allocated in the mux context.
Definition: matroskaenc.c:807
NALUList
Definition: avc.h:33
ebml_writer_uint_len
static int ebml_writer_uint_len(EbmlElement *elem)
Definition: matroskaenc.c:534
MAX_STEREO_MODE_ELEMS
#define MAX_STEREO_MODE_ELEMS
Definition: matroskaenc.c:1392
webm_reformat_vtt
static int webm_reformat_vtt(MatroskaMuxContext *mkv, AVIOContext *pb, const AVPacket *pkt, int *size)
Definition: matroskaenc.c:2479
MATROSKA_ID_EDITIONENTRY
#define MATROSKA_ID_EDITIONENTRY
Definition: matroska.h:262
mkv_track::reformat
int(* reformat)(struct MatroskaMuxContext *, AVIOContext *, const AVPacket *, int *size)
Definition: matroskaenc.c:192
ff_codec_bmp_tags
const AVCodecTag ff_codec_bmp_tags[]
Definition: riff.c:34
random_seed.h
EBML_ID_HEADER
#define EBML_ID_HEADER
Definition: matroska.h:33
channel_layout.h
EBML_ID_EBMLVERSION
#define EBML_ID_EBMLVERSION
Definition: matroska.h:36
mkv_track
Definition: matroskaenc.c:173
MatroskaMuxContext::seekhead
mkv_seekhead seekhead
Definition: matroskaenc.c:211
wv.h
ISOM_DVCC_DVVC_SIZE
#define ISOM_DVCC_DVVC_SIZE
Definition: dovi_isom.h:29
AVIO_SEEKABLE_NORMAL
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:40
ebml_writer_elem_len
static int ebml_writer_elem_len(EbmlWriter *writer, EbmlElement *elem, int remaining_elems)
Definition: matroskaenc.c:627
av_crc
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
Definition: crc.c:392
MATROSKA_ID_TAGLANG
#define MATROSKA_ID_TAGLANG
Definition: matroska.h:218
AV_PKT_DATA_NEW_EXTRADATA
@ AV_PKT_DATA_NEW_EXTRADATA
The AV_PKT_DATA_NEW_EXTRADATA is used to notify the codec or the format that the extradata buffer was...
Definition: packet.