FFmpeg
Loading...
Searching...
No Matches
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 <stdbool.h>
23#include <stdint.h>
24
25#include "config_components.h"
26
27#include "av1.h"
28#include "avc.h"
29#include "hevc.h"
30#include "avformat.h"
31#include "avio_internal.h"
32#include "avlanguage.h"
33#include "dovi_isom.h"
34#include "flacenc.h"
35#include "internal.h"
36#include "isom.h"
37#include "nal.h"
38#include "matroska.h"
39#include "mux.h"
40#include "riff.h"
41#include "version.h"
42#include "vorbiscomment.h"
43#include "vvc.h"
44#include "wv.h"
45
46#include "libavutil/avstring.h"
48#include "libavutil/crc.h"
49#include "libavutil/dict.h"
51#include "libavutil/intfloat.h"
53#include "libavutil/lfg.h"
56#include "libavutil/mem.h"
57#include "libavutil/opt.h"
59#include "libavutil/pixdesc.h"
61#include "libavutil/rational.h"
62#include "libavutil/samplefmt.h"
63#include "libavutil/stereo3d.h"
64
65#include "libavcodec/av1.h"
69#include "libavcodec/defs.h"
70#include "libavcodec/itut35.h"
71#include "libavcodec/xiph.h"
73#include "libavcodec/opus/tab.h"
74
75/* Level 1 elements we create a SeekHead entry for:
76 * Info, Tracks, Chapters, Attachments, Tags (potentially twice) and Cues */
77#define MAX_SEEKHEAD_ENTRIES 7
78
79/* Largest known-length EBML length */
80#define MAX_EBML_LENGTH ((1ULL << 56) - 2)
81/* The dynamic buffer API we rely upon has a limit of INT_MAX;
82 * and so has avio_write(). */
83#define MAX_SUPPORTED_EBML_LENGTH FFMIN(MAX_EBML_LENGTH, INT_MAX)
84
85#define MODE_MATROSKAv2 0x01
86#define MODE_WEBM 0x02
87
88#define IS_WEBM(mkv) (CONFIG_WEBM_MUXER && CONFIG_MATROSKA_MUXER ? \
89 ((mkv)->mode == MODE_WEBM) : CONFIG_WEBM_MUXER)
90#define IS_SEEKABLE(pb, mkv) (((pb)->seekable & AVIO_SEEKABLE_NORMAL) && \
91 !(mkv)->is_live)
92
93enum {
97};
98
99typedef struct ebml_master {
100 int64_t pos; ///< absolute offset in the containing AVIOContext where the master's elements start
101 int sizebytes; ///< how many bytes were reserved for the size
103
108
120
128
129typedef struct EbmlMaster {
130 int nb_elements; ///< -1 if not finished
131 int containing_master; ///< -1 if no parent exists
132} EbmlMaster;
133
134typedef struct EbmlElement {
135 uint32_t id;
137 unsigned length_size;
138 uint64_t size; ///< excluding id and length field
139 union {
140 uint64_t uint;
142 double f;
143 const char *str;
144 const uint8_t *bin;
145 struct MatroskaMuxContext *mkv; ///< used by EBML_BLOCK
149
155
156#define EBML_WRITER(max_nb_elems) \
157 EbmlElement elements[max_nb_elems]; \
158 EbmlWriter writer = (EbmlWriter){ .elements = elements, \
159 .current_master_element = -1 }
160
161typedef struct mkv_seekhead_entry {
162 uint32_t elementid;
163 uint64_t segmentpos;
165
172
173typedef struct mkv_cuepoint {
174 uint64_t pts;
176 int64_t cluster_pos; ///< offset of the cluster containing the block relative to the segment
177 int64_t relative_pos; ///< relative offset from the position of the cluster containing the block
178 int64_t duration; ///< duration of the block according to time base
180
185
186struct MatroskaMuxContext;
187
188typedef struct mkv_track {
191 uint64_t uid;
192 unsigned track_num;
195 unsigned offset;
203 unsigned codecpriv_size; ///< size reserved for CodecPrivate excluding header+length field
207 /* This callback will be called twice: First with a NULL AVIOContext
208 * to return the size of the (Simple)Block's data via size
209 * and a second time with the AVIOContext set when the data
210 * shall be written.
211 * The callback shall not return an error on the second call. */
213 const AVPacket *, int *size);
214} mkv_track;
215
216typedef struct MatroskaMuxContext {
217 const AVClass *class;
219
220 int mode;
226 int64_t cluster_pos; ///< file offset of the current Cluster
234
236
237 /* Used as temporary buffer to use the minimal amount of bytes
238 * to write the length field of EBML Masters.
239 * Every user has to reset the buffer after using it and
240 * different uses may not overlap. It is currently used in
241 * mkv_write_tag(), in mkv_assemble_cues() as well as in
242 * mkv_update_codecprivate() and mkv_write_track(). */
244
246
249
252
258
265
266 uint32_t segment_uid[4];
268
269/** 2 bytes * 3 for EBML IDs, 3 1-byte EBML lengths, 8 bytes for 64 bit
270 * offset, 4 bytes for target EBML ID */
271#define MAX_SEEKENTRY_SIZE 21
272
273/** 4 * (1-byte EBML ID, 1-byte EBML size, 8-byte uint max) */
274#define MAX_CUETRACKPOS_SIZE 40
275
276/** DURATION_STRING_LENGTH must be <= 112 or the containing
277 * simpletag will need more than one byte for its length field. */
278#define DURATION_STRING_LENGTH 19
279
280/** 2 + 1 Simpletag header, 2 + 1 + 8 Name "DURATION", rest for TagString */
281#define DURATION_SIMPLETAG_SIZE (2 + 1 + (2 + 1 + 8) + (2 + 1 + DURATION_STRING_LENGTH))
282
283/** Seek preroll value for opus */
284#define OPUS_SEEK_PREROLL 80000000
285
286/**
287 * Returns the duration of an Opus packet in samples.
288 */
289static int parse_opus_packet_duration(const uint8_t *buf, int buf_size)
290{
291 int code = buf[0] & 0x3;
292 int config = buf[0] >> 3;
293 int frame_count;
294
295 switch (code) {
296 default:
297 av_unreachable("code is in 0..3");
298 case 0:
299 frame_count = 1;
300 break;
301 case 1:
302 case 2:
303 frame_count = 2;
304 break;
305 case 3:
306 if (buf_size <= 1)
307 return AVERROR_INVALIDDATA;
308 frame_count = buf[1] & 0x3F;
309 break;
310 }
311 return frame_count * ff_opus_frame_duration[config];
312}
313
314static int ebml_id_size(uint32_t id)
315{
316 return (av_log2(id) + 7U) / 8;
317}
318
319static void put_ebml_id(AVIOContext *pb, uint32_t id)
320{
321 int i = ebml_id_size(id);
322 while (i--)
323 avio_w8(pb, (uint8_t)(id >> (i * 8)));
324}
325
326/**
327 * Write an EBML size meaning "unknown size".
328 *
329 * @param bytes The number of bytes the size should occupy (maximum: 8).
330 */
331static void put_ebml_size_unknown(AVIOContext *pb, int bytes)
332{
333 av_assert0(bytes <= 8);
334 avio_w8(pb, 0x1ff >> bytes);
335 if (av_builtin_constant_p(bytes) && bytes == 1)
336 return;
337 ffio_fill(pb, 0xff, bytes - 1);
338}
339
340/**
341 * Returns how many bytes are needed to represent a number
342 * as EBML variable length integer.
343 */
344static int ebml_num_size(uint64_t num)
345{
346 int bytes = 0;
347 do {
348 bytes++;
349 } while (num >>= 7);
350 return bytes;
351}
352
353/**
354 * Calculate how many bytes are needed to represent the length field
355 * of an EBML element whose payload has a given length.
356 */
357static int ebml_length_size(uint64_t length)
358{
359 return ebml_num_size(length + 1);
360}
361
362/**
363 * Write a number as EBML variable length integer on `bytes` bytes.
364 * `bytes` is taken literally without checking.
365 */
366static void put_ebml_num(AVIOContext *pb, uint64_t num, int bytes)
367{
368 num |= 1ULL << bytes * 7;
369 for (int i = bytes - 1; i >= 0; i--)
370 avio_w8(pb, (uint8_t)(num >> i * 8));
371}
372
373/**
374 * Write a length as EBML variable length integer.
375 *
376 * @param bytes The number of bytes that need to be used to write the number.
377 * If zero, the minimal number of bytes will be used.
378 */
379static void put_ebml_length(AVIOContext *pb, uint64_t length, int bytes)
380{
381 int needed_bytes = ebml_length_size(length);
382
383 // sizes larger than this are currently undefined in EBML
384 av_assert0(length < (1ULL << 56) - 1);
385
386 if (bytes == 0)
387 bytes = needed_bytes;
388 // The bytes needed to write the given size must not exceed
389 // the bytes that we ought to use.
390 av_assert0(bytes >= needed_bytes);
391 put_ebml_num(pb, length, bytes);
392}
393
394/**
395 * Write a (random) UID with fixed size to make the output more deterministic
396 */
397static void put_ebml_uid(AVIOContext *pb, uint32_t elementid, uint64_t uid)
398{
399 put_ebml_id(pb, elementid);
400 put_ebml_length(pb, 8, 0);
401 avio_wb64(pb, uid);
402}
403
404static void put_ebml_uint(AVIOContext *pb, uint32_t elementid, uint64_t val)
405{
406 int i, bytes = 1;
407 uint64_t tmp = val;
408 while (tmp >>= 8)
409 bytes++;
410
411 put_ebml_id(pb, elementid);
412 put_ebml_length(pb, bytes, 0);
413 for (i = bytes - 1; i >= 0; i--)
414 avio_w8(pb, (uint8_t)(val >> i * 8));
415}
416
417static void put_ebml_float(AVIOContext *pb, uint32_t elementid, double val)
418{
419 put_ebml_id(pb, elementid);
420 put_ebml_length(pb, 8, 0);
422}
423
424static void put_ebml_binary(AVIOContext *pb, uint32_t elementid,
425 const void *buf, int size)
426{
427 put_ebml_id(pb, elementid);
428 put_ebml_length(pb, size, 0);
429 avio_write(pb, buf, size);
430}
431
432static void put_ebml_string(AVIOContext *pb, uint32_t elementid,
433 const char *str)
434{
435 put_ebml_binary(pb, elementid, str, strlen(str));
436}
437
438/**
439 * Write a void element of a given size. Useful for reserving space in
440 * the file to be written to later.
441 *
442 * @param size The number of bytes to reserve, which must be at least 2.
443 */
444static void put_ebml_void(AVIOContext *pb, int size)
445{
446 av_assert0(size >= 2);
447
449 // we need to subtract the length needed to store the size from the
450 // size we need to reserve so 2 cases, we use 8 bytes to store the
451 // size if possible, 1 byte otherwise
452 if (size < 10) {
453 size -= 2;
454 put_ebml_length(pb, size, 0);
455 } else {
456 size -= 9;
457 put_ebml_length(pb, size, 8);
458 }
459 ffio_fill(pb, 0, size);
460}
461
462static ebml_master start_ebml_master(AVIOContext *pb, uint32_t elementid,
463 uint64_t expectedsize)
464{
465 int bytes = expectedsize ? ebml_length_size(expectedsize) : 8;
466
467 put_ebml_id(pb, elementid);
468 put_ebml_size_unknown(pb, bytes);
469 return (ebml_master) { avio_tell(pb), bytes };
470}
471
473{
474 int64_t pos = avio_tell(pb);
475
476 if (avio_seek(pb, master.pos - master.sizebytes, SEEK_SET) < 0)
477 return;
478 put_ebml_length(pb, pos - master.pos, master.sizebytes);
479 avio_seek(pb, pos, SEEK_SET);
480}
481
483 uint32_t id, EbmlType type)
484{
485 writer->elements[writer->nb_elements].id = id;
486 writer->elements[writer->nb_elements].type = type;
487 return &writer->elements[writer->nb_elements++];
488}
489
490static void ebml_writer_open_master(EbmlWriter *writer, uint32_t id)
491{
492 EbmlElement *const elem = ebml_writer_add(writer, id, EBML_MASTER);
493 EbmlMaster *const master = &elem->priv.master;
494
495 master->containing_master = writer->current_master_element;
496 master->nb_elements = -1;
497
498 writer->current_master_element = writer->nb_elements - 1;
499}
500
502{
503 EbmlElement *elem;
506 elem = &writer->elements[writer->current_master_element];
507 av_assert2(elem->type == EBML_MASTER);
508 av_assert2(elem->priv.master.nb_elements < 0); /* means unset */
509 elem->priv.master.nb_elements = writer->nb_elements - writer->current_master_element - 1;
513}
514
516{
517 av_assert2(writer->nb_elements > 0);
520 if (writer->current_master_element == writer->nb_elements - 1) {
521 const EbmlElement *const elem = &writer->elements[writer->nb_elements - 1];
522 /* The master element has no children. Discard it. */
523 av_assert2(elem->type == EBML_MASTER);
527 writer->nb_elements--;
528 return;
529 }
531}
532
533static void ebml_writer_add_string(EbmlWriter *writer, uint32_t id,
534 const char *str)
535{
536 EbmlElement *const elem = ebml_writer_add(writer, id, EBML_STR);
537
538 elem->priv.str = str;
539}
540
541static void ebml_writer_add_bin(EbmlWriter *writer, uint32_t id,
542 const uint8_t *data, size_t size)
543{
544 EbmlElement *const elem = ebml_writer_add(writer, id, EBML_BIN);
545
546#if SIZE_MAX > UINT64_MAX
547 size = FFMIN(size, UINT64_MAX);
548#endif
549 elem->size = size;
550 elem->priv.bin = data;
551}
552
553static void ebml_writer_add_float(EbmlWriter *writer, uint32_t id,
554 double val)
555{
556 EbmlElement *const elem = ebml_writer_add(writer, id, EBML_FLOAT);
557
558 elem->priv.f = val;
559}
560
561static void ebml_writer_add_uid(EbmlWriter *writer, uint32_t id,
562 uint64_t val)
563{
564 EbmlElement *const elem = ebml_writer_add(writer, id, EBML_UID);
565 elem->priv.uint = val;
566}
567
568static void ebml_writer_add_uint(EbmlWriter *writer, uint32_t id,
569 uint64_t val)
570{
571 EbmlElement *elem = ebml_writer_add(writer, id, EBML_UINT);
572 elem->priv.uint = val;
573}
574
575static void ebml_writer_add_sint(EbmlWriter *writer, uint32_t id,
576 int64_t val)
577{
578 EbmlElement *elem = ebml_writer_add(writer, id, EBML_SINT);
579 elem->priv.sint = val;
580}
581
583{
585 elem->priv.mkv = mkv;
586}
587
589{
590 size_t len = strlen(elem->priv.str);
591#if SIZE_MAX > UINT64_MAX
592 len = FF_MIN(len, UINT64_MAX);
593#endif
594 elem->size = len;
595 return 0;
596}
597
598static av_const int uint_size(uint64_t val)
599{
600 int bytes = 0;
601 do {
602 bytes++;
603 } while (val >>= 8);
604 return bytes;
605}
606
608{
609 elem->size = uint_size(elem->priv.uint);
610 return 0;
611}
612
614{
615 uint64_t tmp = 2 * (uint64_t)(val < 0 ? val^-1 : val);
616 return uint_size(tmp);
617}
618
620{
621 elem->size = sint_size(elem->priv.sint);
622 return 0;
623}
624
625static int ebml_writer_elem_len(EbmlWriter *writer, EbmlElement *elem,
626 int remaining_elems);
627
629 int remaining_elems)
630{
631 int nb_elems = elem->priv.master.nb_elements >= 0 ? elem->priv.master.nb_elements : remaining_elems - 1;
632 EbmlElement *const master = elem;
633 uint64_t total_size = 0;
634
635 master->priv.master.nb_elements = nb_elems;
636 for (; elem++, nb_elems > 0;) {
637 int ret = ebml_writer_elem_len(writer, elem, nb_elems);
638 if (ret < 0)
639 return ret;
640 av_assert2(ret < nb_elems);
641 /* No overflow is possible here, as both total_size and elem->size
642 * are bounded by MAX_SUPPORTED_EBML_LENGTH. */
643 total_size += ebml_id_size(elem->id) + elem->length_size + elem->size;
644 if (total_size > MAX_SUPPORTED_EBML_LENGTH)
645 return AVERROR(ERANGE);
646 nb_elems--; /* consume elem */
647 elem += ret, nb_elems -= ret; /* and elem's children */
648 }
649 master->size = total_size;
650
651 return master->priv.master.nb_elements;
652}
653
655{
656 MatroskaMuxContext *const mkv = elem->priv.mkv;
657 BlockContext *const block = &mkv->cur_block;
658 mkv_track *const track = block->track;
659 const AVPacket *const pkt = block->pkt;
660 int err, size;
661
662 if (track->reformat) {
663 err = track->reformat(mkv, NULL, pkt, &size);
664 if (err < 0) {
665 av_log(mkv->ctx, AV_LOG_ERROR, "Error when reformatting data of "
666 "a packet from stream %d.\n", pkt->stream_index);
667 return err;
668 }
669 } else {
670 size = pkt->size;
671 if (track->offset <= size)
672 size -= track->offset;
673 }
674 elem->size = track->track_num_size + 3U + size;
675
676 return 0;
677}
678
680{
681 MatroskaMuxContext *const mkv = elem->priv.mkv;
682 BlockContext *const block = &mkv->cur_block;
683 mkv_track *const track = block->track;
684 const AVPacket *const pkt = block->pkt;
685
686 put_ebml_num(pb, track->track_num, track->track_num_size);
687 avio_wb16(pb, block->rel_ts);
688 avio_w8(pb, block->flags);
689
690 if (track->reformat) {
691 int size;
692 track->reformat(mkv, pb, pkt, &size);
693 } else {
694 const uint8_t *data = pkt->data;
695 unsigned offset = track->offset <= pkt->size ? track->offset : 0;
696 avio_write(pb, data + offset, pkt->size - offset);
697 }
698}
699
701 int remaining_elems)
702{
703 int ret = 0;
704
705 switch (elem->type) {
706 case EBML_FLOAT:
707 case EBML_UID:
708 elem->size = 8;
709 break;
710 case EBML_STR:
711 ret = ebml_writer_str_len(elem);
712 break;
713 case EBML_UINT:
714 ret = ebml_writer_uint_len(elem);
715 break;
716 case EBML_SINT:
717 ret = ebml_writer_sint_len(elem);
718 break;
719 case EBML_BLOCK:
720 ret = ebml_writer_block_len(elem);
721 break;
722 case EBML_MASTER:
723 ret = ebml_writer_master_len(writer, elem, remaining_elems);
724 break;
725 }
726 if (ret < 0)
727 return ret;
729 return AVERROR(ERANGE);
730 elem->length_size = ebml_length_size(elem->size);
731 return ret; /* number of elements consumed excluding elem itself */
732}
733
735{
736 put_ebml_id(pb, elem->id);
737 put_ebml_num(pb, elem->size, elem->length_size);
738 switch (elem->type) {
739 case EBML_UID:
740 case EBML_FLOAT: {
741 uint64_t val = elem->type == EBML_UID ? elem->priv.uint
742 : av_double2int(elem->priv.f);
743 avio_wb64(pb, val);
744 break;
745 }
746 case EBML_UINT:
747 case EBML_SINT: {
748 uint64_t val = elem->type == EBML_UINT ? elem->priv.uint
749 : elem->priv.sint;
750 for (int i = elem->size; --i >= 0; )
751 avio_w8(pb, (uint8_t)(val >> i * 8));
752 break;
753 }
754 case EBML_STR:
755 case EBML_BIN: {
756 const uint8_t *data = elem->type == EBML_BIN ? elem->priv.bin
757 : (const uint8_t*)elem->priv.str;
758 avio_write(pb, data, elem->size);
759 break;
760 }
761 case EBML_BLOCK:
762 ebml_writer_write_block(elem, pb);
763 break;
764 case EBML_MASTER: {
765 int nb_elems = elem->priv.master.nb_elements;
766
767 elem++;
768 for (int i = 0; i < nb_elems; i++)
769 i += ebml_writer_elem_write(elem + i, pb);
770
771 return nb_elems;
772 }
773 }
774 return 0;
775}
776
778{
779 int ret = ebml_writer_elem_len(writer, writer->elements,
780 writer->nb_elements);
781 if (ret < 0)
782 return ret;
783 ebml_writer_elem_write(writer->elements, pb);
784 return 0;
785}
786
787static void mkv_add_seekhead_entry(MatroskaMuxContext *mkv, uint32_t elementid,
788 uint64_t filepos)
789{
790 mkv_seekhead *seekhead = &mkv->seekhead;
791
793
794 seekhead->entries[seekhead->num_entries].elementid = elementid;
795 seekhead->entries[seekhead->num_entries++].segmentpos = filepos - mkv->segment_offset;
796}
797
799{
800 int ret;
801
802 if (!*dyn_cp && (ret = avio_open_dyn_buf(dyn_cp)) < 0)
803 return ret;
804
805 if (mkv->write_crc)
806 put_ebml_void(*dyn_cp, 6); /* Reserve space for CRC32 so position/size calculations using avio_tell() take it into account */
807
808 return 0;
809}
810
812 MatroskaMuxContext *mkv, uint32_t id,
813 int length_size, int keep_buffer,
814 int add_seekentry)
815{
816 uint8_t *buf, crc[4];
817 int ret, size, skip = 0;
818
819 size = avio_get_dyn_buf(*dyn_cp, &buf);
820 if ((ret = (*dyn_cp)->error) < 0)
821 goto fail;
822
823 if (add_seekentry)
824 mkv_add_seekhead_entry(mkv, id, avio_tell(pb));
825
826 put_ebml_id(pb, id);
827 put_ebml_length(pb, size, length_size);
828 if (mkv->write_crc) {
829 skip = 6; /* Skip reserved 6-byte long void element from the dynamic buffer. */
830 AV_WL32(crc, av_crc(av_crc_get_table(AV_CRC_32_IEEE_LE), UINT32_MAX, buf + skip, size - skip) ^ UINT32_MAX);
831 put_ebml_binary(pb, EBML_ID_CRC32, crc, sizeof(crc));
832 }
833 avio_write(pb, buf + skip, size - skip);
834
835fail:
836 if (keep_buffer) {
837 ffio_reset_dyn_buf(*dyn_cp);
838 } else {
839 ffio_free_dyn_buf(dyn_cp);
840 }
841 return ret;
842}
843
844/**
845 * Output EBML master. Keep the buffer if seekable, allowing for later updates.
846 * Furthermore always add a SeekHead Entry for this element.
847 */
849 ebml_stored_master *elem,
850 MatroskaMuxContext *mkv, uint32_t id)
851{
852 if (IS_SEEKABLE(pb, mkv)) {
853 uint8_t *buf;
854 int size = avio_get_dyn_buf(elem->bc, &buf);
855
856 if (elem->bc->error < 0)
857 return elem->bc->error;
858
859 elem->pos = avio_tell(pb);
860 mkv_add_seekhead_entry(mkv, id, elem->pos);
861
862 put_ebml_id(pb, id);
863 put_ebml_length(pb, size, 0);
864 avio_write(pb, buf, size);
865
866 return 0;
867 } else
868 return end_ebml_master_crc32(pb, &elem->bc, mkv, id, 0, 0, 1);
869}
870
871static void put_xiph_size(AVIOContext *pb, int size)
872{
873 ffio_fill(pb, 255, size / 255);
874 avio_w8(pb, size % 255);
875}
876
877/**
878 * Free the members allocated in the mux context.
879 */
881{
882 MatroskaMuxContext *mkv = s->priv_data;
883
889
891 av_freep(&mkv->cues.entries);
892
893 av_freep(&mkv->tracks);
894}
895
896/**
897 * Initialize the SeekHead element to be ready to index level 1 Matroska
898 * elements. Enough space to write MAX_SEEKHEAD_ENTRIES SeekHead entries
899 * will be reserved at the current file location.
900 */
902{
903 mkv->seekhead.filepos = avio_tell(pb);
904 // 21 bytes max for a Seek entry, 6 bytes max for the SeekHead ID
905 // and size, 6 bytes for a CRC32 element, and 2 bytes to guarantee
906 // that an EBML void element will fit afterwards
909}
910
911/**
912 * Write the SeekHead to the file at the location reserved for it
913 * and seek to destpos afterwards. When error_on_seek_failure
914 * is not set, failure to seek to the position designated for the
915 * SeekHead is not considered an error and it is presumed that
916 * destpos is the current position; failure to seek to destpos
917 * afterwards is always an error.
918 *
919 * @return 0 on success, < 0 on error.
920 */
922 int error_on_seek_failure, int64_t destpos)
923{
924 AVIOContext *dyn_cp = NULL;
925 mkv_seekhead *seekhead = &mkv->seekhead;
926 int64_t remaining, ret64;
927 int i, ret;
928
929 if ((ret64 = avio_seek(pb, seekhead->filepos, SEEK_SET)) < 0)
930 return error_on_seek_failure ? ret64 : 0;
931
932 ret = start_ebml_master_crc32(&dyn_cp, mkv);
933 if (ret < 0)
934 return ret;
935
936 for (i = 0; i < seekhead->num_entries; i++) {
937 mkv_seekhead_entry *entry = &seekhead->entries[i];
940
942 put_ebml_length(dyn_cp, ebml_id_size(entry->elementid), 0);
943 put_ebml_id(dyn_cp, entry->elementid);
944
945 put_ebml_uint(dyn_cp, MATROSKA_ID_SEEKPOSITION, entry->segmentpos);
946 end_ebml_master(dyn_cp, seekentry);
947 }
948 ret = end_ebml_master_crc32(pb, &dyn_cp, mkv,
949 MATROSKA_ID_SEEKHEAD, 0, 0, 0);
950 if (ret < 0)
951 return ret;
952
953 remaining = seekhead->filepos + seekhead->reserved_size - avio_tell(pb);
954 put_ebml_void(pb, remaining);
955
956 if ((ret64 = avio_seek(pb, destpos, SEEK_SET)) < 0)
957 return ret64;
958
959 return 0;
960}
961
962static int mkv_add_cuepoint(MatroskaMuxContext *mkv, int stream, int64_t ts,
963 int64_t cluster_pos, int64_t relative_pos, int64_t duration)
964{
965 mkv_cues *cues = &mkv->cues;
966 mkv_cuepoint *entries = cues->entries;
967 unsigned idx = cues->num_entries;
968
969 if (ts < 0)
970 return 0;
971
972 entries = av_realloc_array(entries, cues->num_entries + 1, sizeof(mkv_cuepoint));
973 if (!entries)
974 return AVERROR(ENOMEM);
975 cues->entries = entries;
976
977 /* Make sure the cues entries are sorted by pts. */
978 while (idx > 0 && entries[idx - 1].pts > ts)
979 idx--;
980 memmove(&entries[idx + 1], &entries[idx],
981 (cues->num_entries - idx) * sizeof(entries[0]));
982
983 entries[idx].pts = ts;
984 entries[idx].stream_idx = stream;
985 entries[idx].cluster_pos = cluster_pos - mkv->segment_offset;
986 entries[idx].relative_pos = relative_pos;
987 entries[idx].duration = duration;
988
989 cues->num_entries++;
990
991 return 0;
992}
993
994static int mkv_assemble_cues(AVStream **streams, AVIOContext *dyn_cp, AVIOContext *cuepoint,
995 const mkv_cues *cues, mkv_track *tracks, int num_tracks,
996 uint64_t offset)
997{
998 for (mkv_cuepoint *entry = cues->entries, *end = entry + cues->num_entries;
999 entry < end;) {
1000 uint64_t pts = entry->pts;
1001 uint8_t *buf;
1002 int size;
1003
1005
1006 // put all the entries from different tracks that have the exact same
1007 // timestamp into the same CuePoint
1008 for (int j = 0; j < num_tracks; j++)
1009 tracks[j].has_cue = 0;
1010 do {
1011 ebml_master track_positions;
1012 int idx = entry->stream_idx;
1013
1014 av_assert0(idx >= 0 && idx < num_tracks);
1015 if (tracks[idx].has_cue && streams[idx]->codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE)
1016 continue;
1017 tracks[idx].has_cue = 1;
1019 put_ebml_uint(cuepoint, MATROSKA_ID_CUETRACK , tracks[idx].track_num);
1020 put_ebml_uint(cuepoint, MATROSKA_ID_CUECLUSTERPOSITION , entry->cluster_pos + offset);
1021 put_ebml_uint(cuepoint, MATROSKA_ID_CUERELATIVEPOSITION, entry->relative_pos);
1022 if (entry->duration > 0)
1023 put_ebml_uint(cuepoint, MATROSKA_ID_CUEDURATION , entry->duration);
1024 end_ebml_master(cuepoint, track_positions);
1025 } while (++entry < end && entry->pts == pts);
1026 size = avio_get_dyn_buf(cuepoint, &buf);
1027 if (cuepoint->error < 0)
1028 return cuepoint->error;
1030 ffio_reset_dyn_buf(cuepoint);
1031 }
1032
1033 return 0;
1034}
1035
1037 const AVCodecParameters *par,
1038 const uint8_t *extradata, int extradata_size)
1039{
1040 const uint8_t *header_start[3];
1041 int header_len[3];
1042 int first_header_size;
1043 int err, j;
1044
1045 if (par->codec_id == AV_CODEC_ID_VORBIS)
1046 first_header_size = 30;
1047 else
1048 first_header_size = 42;
1049
1050 err = avpriv_split_xiph_headers(extradata, extradata_size,
1051 first_header_size, header_start, header_len);
1052 if (err < 0) {
1053 av_log(s, AV_LOG_ERROR, "Extradata corrupt.\n");
1054 return err;
1055 }
1056
1057 avio_w8(pb, 2); // number packets - 1
1058 for (j = 0; j < 2; j++) {
1059 put_xiph_size(pb, header_len[j]);
1060 }
1061 for (j = 0; j < 3; j++)
1062 avio_write(pb, header_start[j], header_len[j]);
1063
1064 return 0;
1065}
1066
1067#if CONFIG_MATROSKA_MUXER
1068static int put_wv_codecpriv(AVIOContext *pb, const uint8_t *extradata, int extradata_size)
1069{
1070 if (extradata && extradata_size == 2)
1071 avio_write(pb, extradata, 2);
1072 else
1073 avio_wl16(pb, 0x410); // fallback to the most recent version
1074 return 0;
1075}
1076
1077static int put_flac_codecpriv(AVFormatContext *s, AVIOContext *pb,
1078 const AVCodecParameters *par,
1079 const uint8_t *extradata, int extradata_size)
1080{
1081 int write_comment = (par->ch_layout.order == AV_CHANNEL_ORDER_NATIVE &&
1082 !(par->ch_layout.u.mask & ~0x3ffffULL) &&
1084 int ret = ff_flac_write_header(pb, extradata, extradata_size,
1085 !write_comment);
1086
1087 if (ret < 0)
1088 return ret;
1089
1090 if (write_comment) {
1091 const char *vendor = (s->flags & AVFMT_FLAG_BITEXACT) ?
1092 "Lavf" : LIBAVFORMAT_IDENT;
1093 AVDictionary *dict = NULL;
1094 uint8_t buf[32];
1095 int len;
1096
1097 snprintf(buf, sizeof(buf), "0x%"PRIx64, par->ch_layout.u.mask);
1098 av_dict_set(&dict, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", buf, 0);
1099
1100 len = ff_vorbiscomment_length(dict, vendor, NULL, 0);
1101 if (len < 0)
1102 return len;
1103 av_assert1(len < (1 << 24) - 4);
1104
1105 avio_w8(pb, 0x84);
1106 avio_wb24(pb, len);
1107
1108 ff_vorbiscomment_write(pb, dict, vendor, NULL, 0);
1109
1110 av_dict_free(&dict);
1111 }
1112
1113 return 0;
1114}
1115
1116static int get_aac_sample_rates(AVFormatContext *s, MatroskaMuxContext *mkv,
1117 const uint8_t *extradata, int extradata_size,
1118 int *sample_rate, int *output_sample_rate)
1119{
1120 MPEG4AudioConfig mp4ac;
1121 int ret;
1122
1123 ret = avpriv_mpeg4audio_get_config2(&mp4ac, extradata, extradata_size, 1, s);
1124 /* Don't abort if the failure is because of missing extradata. Assume in that
1125 * case a bitstream filter will provide the muxer with the extradata in the
1126 * first packet.
1127 * Abort however if s->pb is not seekable, as we would not be able to seek back
1128 * to write the sample rate elements once the extradata shows up, anyway. */
1129 if (ret < 0 && (extradata_size || !IS_SEEKABLE(s->pb, mkv))) {
1131 "Error parsing AAC extradata, unable to determine samplerate.\n");
1132 return AVERROR(EINVAL);
1133 }
1134
1135 if (ret < 0) {
1136 /* This will only happen when this function is called while writing the
1137 * header and no extradata is available. The space for this element has
1138 * to be reserved for when this function is called again after the
1139 * extradata shows up in the first packet, as there's no way to know if
1140 * output_sample_rate will be different than sample_rate or not. */
1141 *output_sample_rate = *sample_rate;
1142 } else {
1143 *sample_rate = mp4ac.sample_rate;
1144 *output_sample_rate = mp4ac.ext_sample_rate;
1145 }
1146 return 0;
1147}
1148#endif
1149
1151 const AVCodecParameters *par,
1152 const uint8_t *extradata,
1153 int extradata_size,
1154 unsigned *size_to_reserve)
1155{
1156 switch (par->codec_id) {
1157 case AV_CODEC_ID_VORBIS:
1158 case AV_CODEC_ID_THEORA:
1159 return put_xiph_codecpriv(s, dyn_cp, par, extradata, extradata_size);
1160 case AV_CODEC_ID_AV1:
1161 if (extradata_size)
1162 return ff_isom_write_av1c(dyn_cp, extradata,
1163 extradata_size, 1);
1164 else
1165 *size_to_reserve = (AV1_SANE_SEQUENCE_HEADER_MAX_BITS + 7) / 8 + 100;
1166 break;
1167#if CONFIG_MATROSKA_MUXER
1168 case AV_CODEC_ID_FLAC:
1169 return put_flac_codecpriv(s, dyn_cp, par, extradata, extradata_size);
1171 return put_wv_codecpriv(dyn_cp, extradata, extradata_size);
1172 case AV_CODEC_ID_H264:
1173 return ff_isom_write_avcc(dyn_cp, extradata,
1174 extradata_size);
1175 case AV_CODEC_ID_HEVC:
1176 return ff_isom_write_hvcc(dyn_cp, extradata,
1177 extradata_size, 0, s);
1178 case AV_CODEC_ID_VVC:
1179 return ff_isom_write_vvcc(dyn_cp, extradata,
1180 extradata_size, 0);
1181 case AV_CODEC_ID_ALAC:
1182 if (extradata_size < 36) {
1184 "Invalid extradata found, ALAC expects a 36-byte "
1185 "QuickTime atom.");
1186 return AVERROR_INVALIDDATA;
1187 } else
1188 avio_write(dyn_cp, extradata + 12,
1189 extradata_size - 12);
1190 break;
1191 case AV_CODEC_ID_AAC:
1192 if (extradata_size)
1193 avio_write(dyn_cp, extradata, extradata_size);
1194 else
1195 *size_to_reserve = MAX_PCE_SIZE;
1196 break;
1198 unsigned stream_identifier, data_component_id;
1199 switch (par->profile) {
1201 stream_identifier = 0x30;
1202 data_component_id = 0x0008;
1203 break;
1205 stream_identifier = 0x87;
1206 data_component_id = 0x0012;
1207 break;
1208 default:
1210 "Unset/unknown ARIB caption profile %d utilized!\n",
1211 par->profile);
1212 return AVERROR_INVALIDDATA;
1213 }
1214 avio_w8(dyn_cp, stream_identifier);
1215 avio_wb16(dyn_cp, data_component_id);
1216 break;
1217 }
1218#endif
1219 default:
1220 if (CONFIG_MATROSKA_MUXER && par->codec_id == AV_CODEC_ID_PRORES &&
1222 avio_wl32(dyn_cp, par->codec_tag);
1223 } else if (extradata_size && par->codec_id != AV_CODEC_ID_TTA)
1224 avio_write(dyn_cp, extradata, extradata_size);
1225 }
1226
1227 return 0;
1228}
1229
1231 AVCodecParameters *par,
1232 const uint8_t *extradata, int extradata_size,
1233 int native_id, int qt_id,
1234 uint8_t **codecpriv, int *codecpriv_size,
1235 unsigned *max_payload_size)
1236{
1237 av_unused MatroskaMuxContext *const mkv = s->priv_data;
1238 unsigned size_to_reserve = 0;
1239 int ret;
1240
1241 if (native_id) {
1242 ret = mkv_assemble_native_codecprivate(s, dyn_cp, par,
1243 extradata, extradata_size,
1244 &size_to_reserve);
1245 if (ret < 0)
1246 return ret;
1247#if CONFIG_MATROSKA_MUXER
1248 } else if (par->codec_type == AVMEDIA_TYPE_VIDEO) {
1249 if (qt_id) {
1250 if (!par->codec_tag)
1252 par->codec_id);
1254 && (!extradata_size || ff_codec_get_id(ff_codec_movvideo_tags, AV_RL32(extradata + 4)) != par->codec_id)
1255 ) {
1256 avio_wb32(dyn_cp, 0x5a + extradata_size);
1257 avio_wl32(dyn_cp, par->codec_tag);
1258 ffio_fill(dyn_cp, 0, 0x5a - 8);
1259 }
1260 avio_write(dyn_cp, extradata, extradata_size);
1261 } else {
1263 av_log(s, AV_LOG_WARNING, "codec %s is not supported by this format\n",
1265
1266 if (!par->codec_tag)
1268 par->codec_id);
1269 if (!par->codec_tag && par->codec_id != AV_CODEC_ID_RAWVIDEO) {
1270 av_log(s, AV_LOG_ERROR, "No bmp codec tag found for codec %s\n",
1272 return AVERROR(EINVAL);
1273 }
1274
1275 /* If vfw extradata updates are supported, this will have
1276 * to be updated to pass extradata(_size) explicitly. */
1277 ff_put_bmp_header(dyn_cp, par, 0, 0, mkv->flipped_raw_rgb);
1278 }
1279 } else if (par->codec_type == AVMEDIA_TYPE_AUDIO) {
1280 unsigned int tag;
1282 if (!tag) {
1283 av_log(s, AV_LOG_ERROR, "No wav codec tag found for codec %s\n",
1285 return AVERROR(EINVAL);
1286 }
1287 if (!par->codec_tag)
1288 par->codec_tag = tag;
1289
1290 /* Same comment as for ff_put_bmp_header applies here. */
1292 if (ret < 0)
1293 return ret;
1294#endif
1295 }
1296
1297 *codecpriv_size = avio_get_dyn_buf(dyn_cp, codecpriv);
1298 if (dyn_cp->error < 0)
1299 return dyn_cp->error;
1300 *max_payload_size = *codecpriv_size + size_to_reserve;
1301
1302 return 0;
1303}
1304
1305static void mkv_put_codecprivate(AVIOContext *pb, unsigned max_payload_size,
1306 const uint8_t *codecpriv, unsigned codecpriv_size)
1307{
1308 unsigned total_codecpriv_size = 0, total_size;
1309
1310 av_assert1(codecpriv_size <= max_payload_size);
1311
1312 if (!max_payload_size)
1313 return;
1314
1315 total_size = 2 + ebml_length_size(max_payload_size) + max_payload_size;
1316
1317 if (codecpriv_size) {
1318 unsigned length_size = ebml_length_size(codecpriv_size);
1319
1320 total_codecpriv_size = 2U + length_size + codecpriv_size;
1321 if (total_codecpriv_size + 1 == total_size) {
1322 /* It is impossible to add one byte of padding via an EBML Void. */
1323 length_size++;
1324 total_codecpriv_size++;
1325 }
1327 put_ebml_length(pb, codecpriv_size, length_size);
1328 avio_write(pb, codecpriv, codecpriv_size);
1329 }
1330 if (total_codecpriv_size < total_size)
1331 put_ebml_void(pb, total_size - total_codecpriv_size);
1332}
1333
1335 uint8_t *side_data, int side_data_size,
1337 mkv_track *track, unsigned alternative_size)
1338{
1339 AVIOContext *const dyn_bc = mkv->tmp_bc;
1340 uint8_t *codecpriv;
1341 unsigned max_payload_size;
1342 int ret, codecpriv_size;
1343
1344 ret = mkv_assemble_codecprivate(s, dyn_bc, par,
1345 side_data, side_data_size, 1, 0,
1346 &codecpriv, &codecpriv_size, &max_payload_size);
1347 if (ret < 0)
1348 goto fail;
1349 if (codecpriv_size > track->codecpriv_size && !alternative_size) {
1350 ret = AVERROR(ENOSPC);
1351 goto fail;
1352 } else if (codecpriv_size > track->codecpriv_size) {
1353 av_assert1(alternative_size < track->codecpriv_size);
1354 codecpriv_size = alternative_size;
1355 }
1356 avio_seek(pb, track->codecpriv_offset, SEEK_SET);
1358 codecpriv, codecpriv_size);
1359
1360 if (!par->extradata_size) {
1361 ret = ff_alloc_extradata(par, side_data_size);
1362 if (ret < 0)
1363 goto fail;
1364 memcpy(par->extradata, side_data, side_data_size);
1365 }
1366fail:
1367 ffio_reset_dyn_buf(dyn_bc);
1368 return ret;
1369}
1370
1371#define MAX_VIDEO_COLOR_ELEMS 20
1372static void mkv_write_video_color(EbmlWriter *writer, const AVStream *st,
1373 const AVCodecParameters *par)
1374{
1375 const AVPacketSideData *side_data;
1376
1378
1379 if (par->color_trc != AVCOL_TRC_UNSPECIFIED &&
1380 par->color_trc < AVCOL_TRC_NB) {
1382 par->color_trc);
1383 }
1384 if (par->color_space != AVCOL_SPC_UNSPECIFIED &&
1385 par->color_space < AVCOL_SPC_NB) {
1387 par->color_space);
1388 }
1392 par->color_primaries);
1393 }
1395 par->color_range < AVCOL_RANGE_NB) {
1397 }
1400 int xpos, ypos;
1401
1404 (xpos >> 7) + 1);
1406 (ypos >> 7) + 1);
1407 }
1408
1411 if (side_data) {
1414 metadata->MaxCLL);
1416 metadata->MaxFALL);
1417 }
1418
1421 if (side_data) {
1424 if (metadata->has_primaries) {
1426 av_q2d(metadata->display_primaries[0][0]));
1428 av_q2d(metadata->display_primaries[0][1]));
1430 av_q2d(metadata->display_primaries[1][0]));
1432 av_q2d(metadata->display_primaries[1][1]));
1434 av_q2d(metadata->display_primaries[2][0]));
1436 av_q2d(metadata->display_primaries[2][1]));
1438 av_q2d(metadata->white_point[0]));
1440 av_q2d(metadata->white_point[1]));
1441 }
1442 if (metadata->has_luminance) {
1444 av_q2d(metadata->max_luminance));
1446 av_q2d(metadata->min_luminance));
1447 }
1449 }
1450
1452}
1453
1454#define MAX_VIDEO_PROJECTION_ELEMS 6
1455static void mkv_handle_rotation(void *logctx, const AVCodecParameters *par,
1456 double *yaw, double *roll)
1457{
1458 const int32_t *matrix;
1459 const AVPacketSideData *side_data =
1462
1463 if (!side_data)
1464 return;
1465
1466 matrix = (int32_t *)side_data->data;
1467
1468 /* Check whether this is an affine transformation */
1469 if (matrix[2] || matrix[5])
1470 goto ignore;
1471
1472 /* This together with the checks below test whether
1473 * the upper-left 2x2 matrix is nonsingular. */
1474 if (!matrix[0] && !matrix[1])
1475 goto ignore;
1476
1477 /* We ignore the translation part of the matrix (matrix[6] and matrix[7])
1478 * as well as any scaling, i.e. we only look at the upper left 2x2 matrix.
1479 * We only accept matrices that are an exact multiple of an orthogonal one.
1480 * Apart from the multiple, every such matrix can be obtained by
1481 * potentially flipping in the x-direction (corresponding to yaw = 180)
1482 * followed by a rotation of (say) an angle phi in the counterclockwise
1483 * direction. The upper-left 2x2 matrix then looks like this:
1484 * | (+/-)cos(phi) (-/+)sin(phi) |
1485 * scale * | |
1486 * | sin(phi) cos(phi) |
1487 * The first set of signs in the first row apply in case of no flipping,
1488 * the second set applies in case of flipping. */
1489
1490 /* The casts to int64_t are needed because -INT32_MIN doesn't fit
1491 * in an int32_t. */
1492 if (matrix[0] == matrix[4] && -(int64_t)matrix[1] == matrix[3]) {
1493 /* No flipping case */
1494 *yaw = 0;
1495 } else if (-(int64_t)matrix[0] == matrix[4] && matrix[1] == matrix[3]) {
1496 /* Horizontal flip */
1497 *yaw = 180;
1498 } else {
1499ignore:
1500 av_log(logctx, AV_LOG_INFO, "Ignoring display matrix indicating "
1501 "non-orthogonal transformation.\n");
1502 return;
1503 }
1504 *roll = 180 / M_PI * atan2(matrix[3], matrix[4]);
1505
1506 /* We do not write a ProjectionType element indicating "rectangular",
1507 * because this is the default value. */
1508}
1509
1510static int mkv_handle_spherical(void *logctx, EbmlWriter *writer,
1511 const AVCodecParameters *par, uint8_t private[],
1512 double *yaw, double *pitch, double *roll)
1513{
1515 par->nb_coded_side_data,
1517 const AVSphericalMapping *spherical;
1518
1519 if (!sd)
1520 return 0;
1521
1522 spherical = (const AVSphericalMapping *)sd->data;
1523 if (spherical->projection != AV_SPHERICAL_EQUIRECTANGULAR &&
1525 spherical->projection != AV_SPHERICAL_CUBEMAP) {
1526 av_log(logctx, AV_LOG_WARNING, "Unknown projection type\n");
1527 return 0;
1528 }
1529
1530 switch (spherical->projection) {
1535 AV_WB32(private, 0); // version + flags
1536 if (spherical->projection == AV_SPHERICAL_EQUIRECTANGULAR) {
1537 AV_WB32(private + 4, 0);
1538 AV_WB32(private + 8, 0);
1539 AV_WB32(private + 12, 0);
1540 AV_WB32(private + 16, 0);
1541 } else {
1542 AV_WB32(private + 4, spherical->bound_top);
1543 AV_WB32(private + 8, spherical->bound_bottom);
1544 AV_WB32(private + 12, spherical->bound_left);
1545 AV_WB32(private + 16, spherical->bound_right);
1546 }
1548 private, 20);
1549 break;
1553 AV_WB32(private, 0); // version + flags
1554 AV_WB32(private + 4, 0); // layout
1555 AV_WB32(private + 8, spherical->padding);
1557 private, 12);
1558 break;
1559 default:
1560 av_assert0(0);
1561 }
1562
1563 *yaw = (double) spherical->yaw / (1 << 16);
1564 *pitch = (double) spherical->pitch / (1 << 16);
1565 *roll = (double) spherical->roll / (1 << 16);
1566
1567 return 1; /* Projection included */
1568}
1569
1570static void mkv_write_video_projection(void *logctx, EbmlWriter *wr,
1571 const AVCodecParameters *par,
1572 uint8_t private[])
1573{
1574 double yaw = 0, pitch = 0, roll = 0;
1575 int ret;
1576
1578
1579 ret = mkv_handle_spherical(logctx, wr, par, private, &yaw, &pitch, &roll);
1580 if (!ret)
1581 mkv_handle_rotation(logctx, par, &yaw, &roll);
1582
1583 if (yaw)
1585 if (pitch)
1587 if (roll)
1589
1591}
1592
1593#define MAX_FIELD_ORDER_ELEMS 2
1594static void mkv_write_field_order(EbmlWriter *writer, int is_webm,
1595 enum AVFieldOrder field_order)
1596{
1597 switch (field_order) {
1598 case AV_FIELD_UNKNOWN:
1599 break;
1603 break;
1604 case AV_FIELD_TT:
1605 case AV_FIELD_BB:
1606 case AV_FIELD_TB:
1607 case AV_FIELD_BT:
1610 if (!is_webm) {
1611 switch (field_order) {
1612 case AV_FIELD_TT:
1615 break;
1616 case AV_FIELD_BB:
1619 break;
1620 case AV_FIELD_TB:
1623 break;
1624 case AV_FIELD_BT:
1627 break;
1628 }
1629 }
1630 }
1631}
1632
1633#define MAX_STEREO_MODE_ELEMS 1
1635 const AVCodecParameters *par,
1636 const AVStream *st, int is_webm,
1637 int *h_width, int *h_height)
1638{
1639 const char *error_message_addendum = "";
1640 const AVDictionaryEntry *tag;
1642
1643 /* The following macros create bitfields where the ith bit
1644 * indicates whether the MatroskaVideoStereoModeType with that value
1645 * uses double width/height or is WebM compatible. */
1646#define FLAG(STEREOMODETYPE, BOOL) | (BOOL) << (STEREOMODETYPE)
1647#define WDIV1(STEREOMODETYPE, STEREO3DTYPE, FLAGS, WDIV, HDIV, WEBM) \
1648 FLAG(STEREOMODETYPE, WDIV)
1649#define WDIV2(STEREOMODETYPE, WDIV, HDIV, WEBM) \
1650 FLAG(STEREOMODETYPE, WDIV)
1651 // The zero in the following line consumes the first '|'.
1652 const unsigned width_bitfield = 0 STEREOMODE_STEREO3D_MAPPING(WDIV1, WDIV2);
1653
1654#define HDIV1(STEREOMODETYPE, STEREO3DTYPE, FLAGS, WDIV, HDIV, WEBM) \
1655 FLAG(STEREOMODETYPE, HDIV)
1656#define HDIV2(STEREOMODETYPE, WDIV, HDIV, WEBM) \
1657 FLAG(STEREOMODETYPE, HDIV)
1658 const unsigned height_bitfield = 0 STEREOMODE_STEREO3D_MAPPING(HDIV1, HDIV2);
1659
1660#define WEBM1(STEREOMODETYPE, STEREO3DTYPE, FLAGS, WDIV, HDIV, WEBM) \
1661 FLAG(STEREOMODETYPE, WEBM)
1662#define WEBM2(STEREOMODETYPE, WDIV, HDIV, WEBM) \
1663 FLAG(STEREOMODETYPE, WEBM)
1664 const unsigned webm_bitfield = 0 STEREOMODE_STEREO3D_MAPPING(WEBM1, WEBM2);
1665
1666 *h_width = 1;
1667 *h_height = 1;
1668
1669 if ((tag = av_dict_get(st->metadata, "stereo_mode", NULL, 0)) ||
1670 (tag = av_dict_get( s->metadata, "stereo_mode", NULL, 0))) {
1671
1672 for (int i = 0; i < MATROSKA_VIDEO_STEREOMODE_TYPE_NB; i++)
1673 if (!strcmp(tag->value, ff_matroska_video_stereo_mode[i])){
1674 format = i;
1675 break;
1676 }
1678 long stereo_mode = strtol(tag->value, NULL, 0);
1679 if ((unsigned long)stereo_mode >= MATROSKA_VIDEO_STEREOMODE_TYPE_NB)
1680 goto fail;
1681 format = stereo_mode;
1682 }
1683 } else {
1684 const AVPacketSideData *sd;
1685 const AVStereo3D *stereo;
1686 /* The following macro presumes all MATROSKA_VIDEO_STEREOMODE_TYPE_*
1687 * values to be in the range 0..254. */
1688#define STEREOMODE(STEREOMODETYPE, STEREO3DTYPE, FLAGS, WDIV, HDIV, WEBM) \
1689 [(STEREO3DTYPE)][!!((FLAGS) & AV_STEREO3D_FLAG_INVERT)] = (STEREOMODETYPE) + 1,
1690#define NOTHING(STEREOMODETYPE, WDIV, HDIV, WEBM)
1691 static const unsigned char conversion_table[][2] = {
1693 };
1694 int fmt;
1695
1698 if (!sd)
1699 return 0;
1700
1701 stereo = (const AVStereo3D*)sd->data;
1702
1703 /* A garbage AVStereo3D or something with no Matroska analogon. */
1704 if ((unsigned)stereo->type >= FF_ARRAY_ELEMS(conversion_table))
1705 return 0;
1706
1707 fmt = conversion_table[stereo->type][!!(stereo->flags & AV_STEREO3D_FLAG_INVERT)];
1708 /* Format with no Matroska analogon; ignore it */
1709 if (!fmt)
1710 return 0;
1711 format = fmt - 1;
1712 }
1713
1714 // if webm, do not write unsupported modes
1715 if (is_webm && !(webm_bitfield >> format)) {
1716 error_message_addendum = " for WebM";
1717 goto fail;
1718 }
1719
1720 *h_width = 1 << ((width_bitfield >> format) & 1);
1721 *h_height = 1 << ((height_bitfield >> format) & 1);
1722
1723 // write StereoMode if format is valid
1725
1726 return 0;
1727fail:
1729 "The specified stereo mode is not valid%s.\n",
1730 error_message_addendum);
1731 return AVERROR(EINVAL);
1732}
1733
1735 const AVCodecParameters *par, AVIOContext *pb,
1736 mkv_track *track, const AVStream *st)
1737{
1738#if CONFIG_MATROSKA_MUXER
1740 const AVPacketSideData *sd;
1741
1742 if (IS_SEEKABLE(s->pb, mkv)) {
1744 // We can't know at this point if there will be a block with BlockAdditions, so
1745 // we either write the default value here, or a void element. Either of them will
1746 // be overwritten when finishing the track.
1748 if (par->codec_type == AVMEDIA_TYPE_VIDEO) {
1749 // Similarly, reserve space for an eventual
1750 // HDR10+ ITU T.35 metadata BlockAdditionMapping.
1751 put_ebml_void(pb, 3 /* BlockAdditionMapping */
1752 + 4 /* BlockAddIDValue */
1753 + 4 /* BlockAddIDType */);
1754 }
1755 }
1756
1759
1760 if (!sd)
1761 return;
1762
1763 dovi = (const AVDOVIDecoderConfigurationRecord *)sd->data;
1764 if (dovi->dv_profile <= 10) {
1765 ebml_master mapping;
1766 uint8_t buf[ISOM_DVCC_DVVC_SIZE];
1767 uint32_t type;
1768
1769 uint64_t expected_size = (2 + 1 + (sizeof(DVCC_DVVC_BLOCK_TYPE_NAME) - 1))
1770 + (2 + 1 + 4) + (2 + 1 + ISOM_DVCC_DVVC_SIZE);
1771
1772 if (dovi->dv_profile > 7) {
1774 } else {
1776 }
1777
1778 ff_isom_put_dvcc_dvvc(s, buf, dovi);
1779
1780 mapping = start_ebml_master(pb, MATROSKA_ID_TRACKBLKADDMAPPING, expected_size);
1781
1784 put_ebml_binary(pb, MATROSKA_ID_BLKADDIDEXTRADATA, buf, sizeof(buf));
1785
1786 end_ebml_master(pb, mapping);
1787 }
1788
1791 if (sd) {
1792 ebml_master mapping;
1793 uint64_t expected_size = (2 + 1 + (sizeof(HVCE_BLOCK_TYPE_NAME) - 1))
1794 + (2 + 1 + 4)
1795 + (2 + ebml_length_size(sd->size) + sd->size);
1796
1797 mapping = start_ebml_master(pb, MATROSKA_ID_TRACKBLKADDMAPPING, expected_size);
1798
1802
1803 end_ebml_master(pb, mapping);
1804
1805 // DV Profile 7 EL uses BlockAddID 1; ensure MaxBlockAdditionID reflects this.
1806 track->max_blockaddid = FFMAX(track->max_blockaddid, 1);
1807 }
1808#endif
1809}
1810
1812 const AVCodecParameters *par)
1813{
1814 const AVDictionaryEntry *tag;
1815 if (par->codec_id != AV_CODEC_ID_VP8 &&
1816 par->codec_id != AV_CODEC_ID_VP9)
1817 return false;
1819 if (desc && (desc->flags & AV_PIX_FMT_FLAG_ALPHA))
1820 return true;
1821 return ((tag = av_dict_get(st->metadata, "alpha_mode", NULL, 0)) ||
1822 (tag = av_dict_get( s->metadata, "alpha_mode", NULL, 0))) && strtol(tag->value, NULL, 0);
1823}
1824
1826 const AVStream *st, const AVCodecParameters *par,
1827 AVIOContext *pb)
1828{
1829 int display_width_div = 1, display_height_div = 1;
1830 uint8_t color_space[4], projection_private[20];
1831 const AVPacketSideData *sd;
1834 int cropped_width = par->width, cropped_height = par->height;
1835 int ret;
1836
1838
1841
1842 mkv_write_field_order(&writer, IS_WEBM(mkv), par->field_order);
1843
1844 // check both side data and metadata for stereo information,
1845 // write the result to the bitstream if any is found
1846 ret = mkv_write_stereo_mode(s, &writer, par, st, IS_WEBM(mkv),
1847 &display_width_div,
1848 &display_height_div);
1849 if (ret < 0)
1850 return ret;
1851
1852 if (codec_has_blockadditional_alpha(s, st, par))
1854
1856 par->nb_coded_side_data,
1858 if (sd && sd->size == sizeof(uint32_t) * 4) {
1859 uint64_t top, bottom, left, right;
1860
1861 top = AV_RL32(sd->data + 0);
1862 bottom = AV_RL32(sd->data + 4);
1863 left = AV_RL32(sd->data + 8);
1864 right = AV_RL32(sd->data + 12);
1865
1866 if ((left + right) >= par->width ||
1867 (top + bottom) >= par->height) {
1868 av_log(s, AV_LOG_ERROR, "Invalid cropping dimensions in stream side data\n");
1869 return AVERROR(EINVAL);
1870 }
1871
1872 if (bottom)
1874 if (top)
1876 if (left)
1878 if (right)
1880
1881 cropped_width -= left + right;
1882 cropped_height -= top + bottom;
1883 }
1884
1885 // write DisplayWidth and DisplayHeight, they contain the size of
1886 // a single source view and/or the display aspect ratio
1887 if (st->sample_aspect_ratio.num) {
1888 int64_t d_width = av_rescale(cropped_width, st->sample_aspect_ratio.num, st->sample_aspect_ratio.den);
1889 if (d_width > INT_MAX) {
1890 av_log(s, AV_LOG_ERROR, "Overflow in display width\n");
1891 return AVERROR(EINVAL);
1892 }
1893 if (d_width != cropped_width || display_width_div != 1 || display_height_div != 1) {
1894 if (IS_WEBM(mkv) || display_width_div != 1 || display_height_div != 1) {
1896 d_width / display_width_div);
1898 cropped_height / display_height_div);
1899 } else {
1900 AVRational display_aspect_ratio;
1901 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1902 cropped_width * (int64_t)st->sample_aspect_ratio.num,
1903 cropped_height * (int64_t)st->sample_aspect_ratio.den,
1904 1024 * 1024);
1906 display_aspect_ratio.num);
1908 display_aspect_ratio.den);
1911 }
1912 }
1913 } else if (display_width_div != 1 || display_height_div != 1) {
1915 cropped_width / display_width_div);
1917 cropped_height / display_height_div);
1918 } else if (!IS_WEBM(mkv))
1921
1922 if (par->codec_id == AV_CODEC_ID_RAWVIDEO) {
1923 AV_WL32(color_space, par->codec_tag);
1925 color_space, sizeof(color_space));
1926 }
1927 mkv_write_video_color(&writer, st, par);
1928 mkv_write_video_projection(s, &writer, par, projection_private);
1929
1930 return ebml_writer_write(&writer, pb);
1931}
1932
1935{
1937 1000000000LL * duration.num / duration.den);
1938 track->default_duration_low = 1000LL * duration.num / duration.den;
1940 !!(1000LL * duration.num % duration.den);
1941}
1942
1944 AVStream *st, mkv_track *track, AVIOContext *pb,
1945 int is_default)
1946{
1947 AVCodecParameters *par = st->codecpar;
1948 ebml_master subinfo, track_master;
1949 int native_id = 0;
1950 int qt_id = 0;
1951 int bit_depth;
1952 int sample_rate = par->sample_rate;
1953 int output_sample_rate = 0;
1954 int j, ret;
1955 const AVDictionaryEntry *tag;
1956
1958 return 0;
1959
1960 track_master = start_ebml_master(pb, MATROSKA_ID_TRACKENTRY, 0);
1963 put_ebml_uint(pb, MATROSKA_ID_TRACKFLAGLACING, 0); // no lacing (yet)
1964
1965 if ((tag = av_dict_get(st->metadata, "title", NULL, 0)))
1967 tag = av_dict_get(st->metadata, "language", NULL, 0);
1969 tag && tag->value[0] ? tag->value : "und");
1970
1971 // The default value for TRACKFLAGDEFAULT is 1, so add element
1972 // if we need to clear it.
1973 if (!is_default)
1975
1978
1979 if (IS_WEBM(mkv)) {
1980 const char *codec_id;
1981 if (par->codec_id != AV_CODEC_ID_WEBVTT) {
1982 for (j = 0; ff_webm_codec_tags[j].id != AV_CODEC_ID_NONE; j++) {
1983 if (ff_webm_codec_tags[j].id == par->codec_id) {
1985 native_id = 1;
1986 break;
1987 }
1988 }
1989 } else {
1991 codec_id = "D_WEBVTT/CAPTIONS";
1992 native_id = MATROSKA_TRACK_TYPE_SUBTITLE;
1993 } else if (st->disposition & AV_DISPOSITION_DESCRIPTIONS) {
1994 codec_id = "D_WEBVTT/DESCRIPTIONS";
1995 native_id = MATROSKA_TRACK_TYPE_METADATA;
1996 } else if (st->disposition & AV_DISPOSITION_METADATA) {
1997 codec_id = "D_WEBVTT/METADATA";
1998 native_id = MATROSKA_TRACK_TYPE_METADATA;
1999 } else {
2000 codec_id = "D_WEBVTT/SUBTITLES";
2001 native_id = MATROSKA_TRACK_TYPE_SUBTITLE;
2002 }
2003 }
2004
2005 if (!native_id) {
2007 "Only VP8 or VP9 or AV1 video and Vorbis or Opus audio and WebVTT subtitles are supported for WebM.\n");
2008 return AVERROR(EINVAL);
2009 }
2010
2012 } else {
2024
2025 // look for a codec ID string specific to mkv to use,
2026 // if none are found, use AVI codes
2027 if (par->codec_id == AV_CODEC_ID_JPEG2000) {
2028 /* JPEG2000 is actually supported natively in Matroska,
2029 * yet we use the VfW way to mux it for compatibility
2030 * with old demuxers. (FIXME: Are they really important?) */
2031 } else if (par->codec_id != AV_CODEC_ID_RAWVIDEO || par->codec_tag) {
2032 for (j = 0; ff_mkv_codec_tags[j].id != AV_CODEC_ID_NONE; j++) {
2033 if (ff_mkv_codec_tags[j].id == par->codec_id) {
2035 native_id = 1;
2036 break;
2037 }
2038 }
2039 } else {
2040 if (mkv->allow_raw_vfw) {
2041 native_id = 0;
2042 } else {
2043 av_log(s, AV_LOG_ERROR, "Raw RGB is not supported Natively in Matroska, you can use AVI or NUT or\n"
2044 "If you would like to store it anyway using VFW mode, enable allow_raw_vfw (-allow_raw_vfw 1)\n");
2045 return AVERROR(EINVAL);
2046 }
2047 }
2048 }
2049
2050 switch (par->codec_type) {
2051 AVRational frame_rate;
2052 int audio_frame_samples;
2053
2054 case AVMEDIA_TYPE_VIDEO:
2055 mkv->have_video = 1;
2057
2058 frame_rate = (AVRational){ 0, 1 };
2059 if (st->avg_frame_rate.num > 0 && st->avg_frame_rate.den > 0)
2060 frame_rate = st->avg_frame_rate;
2061 else if (st->r_frame_rate.num > 0 && st->r_frame_rate.den > 0)
2062 frame_rate = st->r_frame_rate;
2063
2064 if (frame_rate.num > 0)
2065 mkv_write_default_duration(track, pb, av_inv_q(frame_rate));
2066
2067 if (CONFIG_MATROSKA_MUXER && !native_id &&
2070 par->codec_id == AV_CODEC_ID_SVQ1 ||
2071 par->codec_id == AV_CODEC_ID_SVQ3 ||
2073 qt_id = 1;
2074
2075 if (qt_id)
2076 put_ebml_string(pb, MATROSKA_ID_CODECID, "V_QUICKTIME");
2077 else if (!native_id) {
2078 // if there is no mkv-specific codec ID, use VFW mode
2079 put_ebml_string(pb, MATROSKA_ID_CODECID, "V_MS/VFW/FOURCC");
2080 track->write_dts = 1;
2082 }
2083
2084 ret = mkv_write_track_video(s, mkv, st, par, pb);
2085 if (ret < 0)
2086 return ret;
2087
2088 break;
2089
2090 case AVMEDIA_TYPE_AUDIO:
2091 if (par->initial_padding) {
2092 int64_t codecdelay = av_rescale_q(par->initial_padding,
2093 (AVRational){ 1, par->sample_rate },
2094 (AVRational){ 1, 1000000000 });
2095 if (codecdelay < 0) {
2096 av_log(s, AV_LOG_ERROR, "Initial padding is invalid\n");
2097 return AVERROR(EINVAL);
2098 }
2099 put_ebml_uint(pb, MATROSKA_ID_CODECDELAY, codecdelay);
2100
2102 (AVRational){ 1, par->sample_rate },
2103 st->time_base);
2104 ffstream(st)->lowest_ts_allowed = -track->ts_offset;
2105 }
2106 if (par->codec_id == AV_CODEC_ID_OPUS)
2108#if CONFIG_MATROSKA_MUXER
2109 else if (par->codec_id == AV_CODEC_ID_AAC) {
2110 ret = get_aac_sample_rates(s, mkv, par->extradata, par->extradata_size,
2111 &sample_rate, &output_sample_rate);
2112 if (ret < 0)
2113 return ret;
2114 }
2115#endif
2116
2118
2119 audio_frame_samples = av_get_audio_frame_duration2(par, 0);
2120 if (!audio_frame_samples)
2121 audio_frame_samples = par->frame_size;
2122 if (audio_frame_samples)
2123 mkv_write_default_duration(track, pb, (AVRational){ audio_frame_samples,
2124 par->sample_rate });
2125 if (!native_id)
2126 // no mkv-specific ID, use ACM mode
2127 put_ebml_string(pb, MATROSKA_ID_CODECID, "A_MS/ACM");
2128
2129 subinfo = start_ebml_master(pb, MATROSKA_ID_TRACKAUDIO, 6 + 4 * 9);
2131
2132 track->sample_rate_offset = avio_tell(pb);
2134 if (output_sample_rate)
2135 put_ebml_float(pb, MATROSKA_ID_AUDIOOUTSAMPLINGFREQ, output_sample_rate);
2136
2138 if (!bit_depth && par->codec_id != AV_CODEC_ID_ADPCM_G726) {
2139 if (par->bits_per_raw_sample)
2141 else
2143 }
2144 if (!bit_depth)
2146 if (bit_depth)
2148 end_ebml_master(pb, subinfo);
2149 break;
2150
2152 if (!native_id) {
2153 av_log(s, AV_LOG_ERROR, "Subtitle codec %s (%d) is not supported.\n",
2154 avcodec_get_name(par->codec_id), par->codec_id);
2155 return AVERROR(ENOSYS);
2156 }
2159
2160 if (!IS_WEBM(mkv) || par->codec_id != AV_CODEC_ID_WEBVTT)
2161 native_id = MATROSKA_TRACK_TYPE_SUBTITLE;
2162
2163 put_ebml_uint(pb, MATROSKA_ID_TRACKTYPE, native_id);
2164 break;
2165 default:
2166 av_log(s, AV_LOG_ERROR, "Only audio, video, and subtitles are supported for Matroska.\n");
2167 return AVERROR(EINVAL);
2168 }
2169
2170 mkv_write_blockadditionmapping(s, mkv, par, pb, track, st);
2171
2172 if (!IS_WEBM(mkv) || par->codec_id != AV_CODEC_ID_WEBVTT) {
2173 uint8_t *codecpriv;
2174 int codecpriv_size, max_payload_size;
2175 track->codecpriv_offset = avio_tell(pb);
2176 ret = mkv_assemble_codecprivate(s, mkv->tmp_bc, par,
2177 par->extradata, par->extradata_size,
2178 native_id, qt_id,
2179 &codecpriv, &codecpriv_size, &max_payload_size);
2180 if (ret < 0)
2181 goto fail;
2182 mkv_put_codecprivate(pb, max_payload_size, codecpriv, codecpriv_size);
2183 track->codecpriv_size = max_payload_size;
2184 }
2185
2186 end_ebml_master(pb, track_master);
2187 ret = 0;
2188fail:
2190
2191 return ret;
2192}
2193
2195{
2196 MatroskaMuxContext *mkv = s->priv_data;
2197 AVIOContext *pb = s->pb;
2198 int video_default_idx = -1, audio_default_idx = -1, subtitle_default_idx = -1;
2199 int i, ret;
2200
2201 if (mkv->nb_attachments == s->nb_streams)
2202 return 0;
2203
2204 ret = start_ebml_master_crc32(&mkv->track.bc, mkv);
2205 if (ret < 0)
2206 return ret;
2207
2209 int video_idx = -1, audio_idx = -1, subtitle_idx = -1;
2210
2211 for (i = s->nb_streams - 1; i >= 0; i--) {
2212 AVStream *st = s->streams[i];
2213
2214 switch (st->codecpar->codec_type) {
2215#define CASE(type, variable) \
2216 case AVMEDIA_TYPE_ ## type: \
2217 variable ## _idx = i; \
2218 if (st->disposition & AV_DISPOSITION_DEFAULT) \
2219 variable ## _default_idx = i; \
2220 break;
2221 CASE(VIDEO, video)
2222 CASE(AUDIO, audio)
2223 CASE(SUBTITLE, subtitle)
2224#undef CASE
2225 }
2226 }
2227
2228 video_default_idx = FFMAX(video_default_idx, video_idx);
2229 audio_default_idx = FFMAX(audio_default_idx, audio_idx);
2231 subtitle_default_idx = FFMAX(subtitle_default_idx, subtitle_idx);
2232 }
2233 for (i = 0; i < s->nb_streams; i++) {
2234 AVStream *st = s->streams[i];
2235 int is_default = st->disposition & AV_DISPOSITION_DEFAULT ||
2236 i == video_default_idx || i == audio_default_idx ||
2237 i == subtitle_default_idx;
2238 ret = mkv_write_track(s, mkv, st, &mkv->tracks[i],
2239 mkv->track.bc, is_default);
2240 if (ret < 0)
2241 return ret;
2242 }
2243
2244 return end_ebml_master_crc32_tentatively(pb, &mkv->track, mkv,
2246}
2247
2249{
2250 EBML_WRITER(4);
2251 uint8_t *key = av_strdup(t->key);
2252 uint8_t *p = key;
2253 const uint8_t *lang = NULL;
2254 int ret;
2255
2256 if (!key)
2257 return AVERROR(ENOMEM);
2258
2259 if ((p = strrchr(p, '-')) &&
2261 *p = 0;
2262
2263 p = key;
2264 while (*p) {
2265 if (*p == ' ')
2266 *p = '_';
2267 else if (*p >= 'a' && *p <= 'z')
2268 *p -= 'a' - 'A';
2269 p++;
2270 }
2271
2274 if (lang)
2277 ret = ebml_writer_write(&writer, pb);
2278
2279 av_freep(&key);
2280 return ret;
2281}
2282
2284 uint32_t elementid, uint64_t uid)
2285{
2287 4 + 1 + 8);
2288 if (elementid)
2289 put_ebml_uid(pb, elementid, uid);
2290 end_ebml_master(pb, targets);
2291}
2292
2293static int mkv_check_tag_name(const char *name, uint32_t elementid)
2294{
2295 return av_strcasecmp(name, "title") &&
2296 av_strcasecmp(name, "stereo_mode") &&
2297 av_strcasecmp(name, "creation_time") &&
2298 av_strcasecmp(name, "encoding_tool") &&
2299 av_strcasecmp(name, "duration") &&
2300 (elementid != MATROSKA_ID_TAGTARGETS_TRACKUID ||
2301 av_strcasecmp(name, "language")) &&
2302 (elementid != MATROSKA_ID_TAGTARGETS_ATTACHUID ||
2303 (av_strcasecmp(name, "filename") &&
2304 av_strcasecmp(name, "mimetype")));
2305}
2306
2308 AVIOContext **pb, unsigned reserved_size,
2309 uint32_t elementid, uint64_t uid)
2310{
2311 const AVDictionaryEntry *t = NULL;
2312 AVIOContext *const tmp_bc = mkv->tmp_bc;
2313 uint8_t *buf;
2314 int ret = 0, size, tag_written = 0;
2315
2316 mkv_write_tag_targets(mkv, tmp_bc, elementid, uid);
2317
2318 while ((t = av_dict_iterate(m, t))) {
2319 if (mkv_check_tag_name(t->key, elementid)) {
2320 ret = mkv_write_simpletag(tmp_bc, t);
2321 if (ret < 0)
2322 goto end;
2323 tag_written = 1;
2324 }
2325 }
2326 if (reserved_size)
2327 put_ebml_void(tmp_bc, reserved_size);
2328 else if (!tag_written)
2329 goto end;
2330
2331 size = avio_get_dyn_buf(tmp_bc, &buf);
2332 if (tmp_bc->error) {
2333 ret = tmp_bc->error;
2334 goto end;
2335 }
2336 if (!*pb) {
2337 ret = start_ebml_master_crc32(pb, mkv);
2338 if (ret < 0)
2339 goto end;
2340 }
2342
2343end:
2344 ffio_reset_dyn_buf(tmp_bc);
2345 return ret;
2346}
2347
2349{
2350 MatroskaMuxContext *mkv = s->priv_data;
2351 int i, ret, seekable = IS_SEEKABLE(s->pb, mkv);
2352
2353 mkv->wrote_tags = 1;
2354
2356
2357 ret = mkv_write_tag(mkv, s->metadata, &mkv->tags.bc, 0, 0, 0);
2358 if (ret < 0)
2359 return ret;
2360
2361 for (i = 0; i < s->nb_streams; i++) {
2362 const AVStream *st = s->streams[i];
2363 mkv_track *track = &mkv->tracks[i];
2364
2366 continue;
2367
2368 ret = mkv_write_tag(mkv, st->metadata, &mkv->tags.bc,
2369 seekable ? DURATION_SIMPLETAG_SIZE : 0,
2371 if (ret < 0)
2372 return ret;
2373 if (seekable)
2375 }
2376
2377 if (mkv->nb_attachments && !IS_WEBM(mkv)) {
2378 for (i = 0; i < s->nb_streams; i++) {
2379 const mkv_track *track = &mkv->tracks[i];
2380 const AVStream *st = s->streams[i];
2381
2383 continue;
2384
2385 ret = mkv_write_tag(mkv, st->metadata, &mkv->tags.bc, 0,
2387 if (ret < 0)
2388 return ret;
2389 }
2390 }
2391
2392 if (mkv->tags.bc) {
2393 return end_ebml_master_crc32_tentatively(s->pb, &mkv->tags, mkv,
2395 }
2396 return 0;
2397}
2398
2400{
2401 for (unsigned i = 0; i < s->nb_chapters; i++) {
2402 if (!s->chapters[i]->id)
2403 return 1;
2404 for (unsigned j = 0; j < i; j++)
2405 if (s->chapters[j]->id == s->chapters[i]->id)
2406 return 1;
2407 }
2408 return 0;
2409}
2410
2412{
2413 MatroskaMuxContext *mkv = s->priv_data;
2414 AVIOContext *dyn_cp = NULL, *dyn_tags = NULL, **tags, *pb = s->pb;
2415 ebml_master editionentry;
2416 AVRational scale = {1, 1E9};
2417 int ret, create_new_ids;
2418
2419 if (!s->nb_chapters || mkv->wrote_chapters)
2420 return 0;
2421
2422 ret = start_ebml_master_crc32(&dyn_cp, mkv);
2423 if (ret < 0)
2424 return ret;
2425
2426 editionentry = start_ebml_master(dyn_cp, MATROSKA_ID_EDITIONENTRY, 0);
2427 if (!IS_WEBM(mkv)) {
2429 /* If mkv_write_tags() has already been called, then any tags
2430 * corresponding to chapters will be put into a new Tags element. */
2431 tags = mkv->wrote_tags ? &dyn_tags : &mkv->tags.bc;
2432 } else
2433 tags = NULL;
2434
2435 create_new_ids = mkv_new_chapter_ids_needed(s);
2436
2437 for (unsigned i = 0; i < s->nb_chapters; i++) {
2438 AVChapter *const c = s->chapters[i];
2439 int64_t chapterstart = av_rescale_q(c->start, c->time_base, scale);
2440 int64_t chapterend = av_rescale_q(c->end, c->time_base, scale);
2441 const AVDictionaryEntry *t;
2442 uint64_t uid = create_new_ids ? i + 1ULL : c->id;
2443 EBML_WRITER(7);
2444
2445 if (chapterstart < 0 || chapterstart > chapterend || chapterend < 0) {
2447 "Invalid chapter start (%"PRId64") or end (%"PRId64").\n",
2448 chapterstart, chapterend);
2449 ret = AVERROR_INVALIDDATA;
2450 goto fail;
2451 }
2452
2455 ebml_writer_add_uint(&writer, MATROSKA_ID_CHAPTERTIMESTART, chapterstart);
2457 if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
2461 }
2462 ret = ebml_writer_write(&writer, dyn_cp);
2463 if (ret < 0)
2464 goto fail;
2465
2466 if (tags) {
2468
2469 ret = mkv_write_tag(mkv, c->metadata, tags, 0,
2471 if (ret < 0)
2472 goto fail;
2473 }
2474 }
2475 end_ebml_master(dyn_cp, editionentry);
2476 mkv->wrote_chapters = 1;
2477
2478 ret = end_ebml_master_crc32(pb, &dyn_cp, mkv, MATROSKA_ID_CHAPTERS, 0, 0, 1);
2479 if (ret < 0)
2480 goto fail;
2481 if (dyn_tags)
2482 return end_ebml_master_crc32(pb, &dyn_tags, mkv,
2483 MATROSKA_ID_TAGS, 0, 0, 1);
2484 return 0;
2485
2486fail:
2487 if (tags) {
2488 /* tags == &mkv->tags.bc can only happen if mkv->tags.bc was
2489 * initially NULL, so we never free older tags. */
2490 ffio_free_dyn_buf(tags);
2491 }
2492 ffio_free_dyn_buf(&dyn_cp);
2493 return ret;
2494}
2495
2496static const char *get_mimetype(const AVStream *st)
2497{
2498 const AVDictionaryEntry *t;
2499
2500 if (t = av_dict_get(st->metadata, "mimetype", NULL, 0))
2501 return t->value;
2502 if (st->codecpar->codec_id != AV_CODEC_ID_NONE) {
2504 if (desc && desc->mime_types) {
2505 return desc->mime_types[0];
2506 } else if (st->codecpar->codec_id == AV_CODEC_ID_TEXT)
2507 return "text/plain";
2508 }
2509
2510 return NULL;
2511}
2512
2514{
2515 MatroskaMuxContext *mkv = s->priv_data;
2516 AVIOContext *dyn_cp = NULL, *pb = s->pb;
2517 int i, ret;
2518
2519 if (!mkv->nb_attachments)
2520 return 0;
2521
2522 ret = start_ebml_master_crc32(&dyn_cp, mkv);
2523 if (ret < 0)
2524 return ret;
2525
2526 for (i = 0; i < s->nb_streams; i++) {
2527 const AVStream *st = s->streams[i];
2528 mkv_track *track = &mkv->tracks[i];
2529 EBML_WRITER(6);
2530 const AVDictionaryEntry *t;
2531 const char *mimetype;
2532
2534 continue;
2535
2537
2538 if (t = av_dict_get(st->metadata, "title", NULL, 0))
2540 if (!(t = av_dict_get(st->metadata, "filename", NULL, 0))) {
2541 av_log(s, AV_LOG_ERROR, "Attachment stream %d has no filename tag.\n", i);
2542 ffio_free_dyn_buf(&dyn_cp);
2543 return AVERROR(EINVAL);
2544 }
2546
2547 mimetype = get_mimetype(st);
2548 av_assert0(mimetype);
2553 ret = ebml_writer_write(&writer, dyn_cp);
2554 if (ret < 0) {
2555 ffio_free_dyn_buf(&dyn_cp);
2556 return ret;
2557 }
2558 }
2559 return end_ebml_master_crc32(pb, &dyn_cp, mkv,
2560 MATROSKA_ID_ATTACHMENTS, 0, 0, 1);
2561}
2562
2564{
2565 const AVDictionaryEntry *duration = av_dict_get(s->metadata, "DURATION",
2566 NULL, 0);
2567 int64_t max = 0;
2568 int64_t us;
2569
2570 if (duration && (av_parse_time(&us, duration->value, 1) == 0) && us > 0) {
2571 av_log(s, AV_LOG_DEBUG, "get_metadata_duration found duration in context metadata: %" PRId64 "\n", us);
2572 return us;
2573 }
2574
2575 for (unsigned i = 0; i < s->nb_streams; i++) {
2576 int64_t stream_duration_us;
2577 duration = av_dict_get(s->streams[i]->metadata, "DURATION", NULL, 0);
2578
2579 if (duration && (av_parse_time(&stream_duration_us, duration->value, 1) == 0))
2580 max = FFMAX(max, stream_duration_us);
2581 }
2582
2583 av_log(s, AV_LOG_DEBUG, "get_metadata_duration returned: %" PRId64 "\n", max);
2584 return max;
2585}
2586
2588 const char *doctype, int version)
2589{
2590 EBML_WRITER(8);
2596 ebml_writer_add_string(&writer, EBML_ID_DOCTYPE, doctype);
2599 /* The size is bounded, so no need to check this. */
2600 ebml_writer_write(&writer, pb);
2601}
2602
2604{
2605 MatroskaMuxContext *mkv = s->priv_data;
2606 const AVDictionaryEntry *tag;
2607 int64_t creation_time;
2608 AVIOContext *pb;
2609 int ret = start_ebml_master_crc32(&mkv->info.bc, mkv);
2610 if (ret < 0)
2611 return ret;
2612 pb = mkv->info.bc;
2613
2615 if ((tag = av_dict_get(s->metadata, "title", NULL, 0)))
2617 if (!(s->flags & AVFMT_FLAG_BITEXACT)) {
2619 if ((tag = av_dict_get(s->metadata, "encoding_tool", NULL, 0)))
2621 else
2623
2624 if (!IS_WEBM(mkv))
2626 } else {
2627 const char *ident = "Lavf";
2630 }
2631
2632 if (ff_parse_creation_time_metadata(s, &creation_time, 0) > 0) {
2633 // Adjust time so it's relative to 2001-01-01 and convert to nanoseconds.
2634 int64_t date_utc = (creation_time - 978307200000000LL) * 1000;
2635 uint8_t date_utc_buf[8];
2636 AV_WB64(date_utc_buf, date_utc);
2637 put_ebml_binary(pb, MATROSKA_ID_DATEUTC, date_utc_buf, 8);
2638 }
2639
2640 // reserve space for the duration
2641 mkv->duration = 0;
2642 mkv->duration_offset = avio_tell(pb);
2643 if (!mkv->is_live) {
2644 int64_t metadata_duration = get_metadata_duration(s);
2645
2646 if (s->duration > 0) {
2647 int64_t scaledDuration = av_rescale(s->duration, 1000, AV_TIME_BASE);
2648 put_ebml_float(pb, MATROSKA_ID_DURATION, scaledDuration);
2649 av_log(s, AV_LOG_DEBUG, "Write early duration from recording time = %" PRIu64 "\n", scaledDuration);
2650 } else if (metadata_duration > 0) {
2651 int64_t scaledDuration = av_rescale(metadata_duration, 1000, AV_TIME_BASE);
2652 put_ebml_float(pb, MATROSKA_ID_DURATION, scaledDuration);
2653 av_log(s, AV_LOG_DEBUG, "Write early duration from metadata = %" PRIu64 "\n", scaledDuration);
2654 } else if (s->pb->seekable & AVIO_SEEKABLE_NORMAL) {
2655 put_ebml_void(pb, 11); // assumes double-precision float to be written
2656 }
2657 }
2658 return end_ebml_master_crc32_tentatively(s->pb, &mkv->info,
2659 mkv, MATROSKA_ID_INFO);
2660}
2661
2663{
2664 MatroskaMuxContext *mkv = s->priv_data;
2665 AVIOContext *pb = s->pb;
2666 int ret, version = 2;
2667
2668 ret = avio_open_dyn_buf(&mkv->tmp_bc);
2669 if (ret < 0)
2670 return ret;
2671
2672 if (!IS_WEBM(mkv) ||
2673 av_dict_get(s->metadata, "stereo_mode", NULL, 0) ||
2674 av_dict_get(s->metadata, "alpha_mode", NULL, 0))
2675 version = 4;
2676
2677 for (unsigned i = 0; i < s->nb_streams; i++) {
2678 if (s->streams[i]->codecpar->codec_id == AV_CODEC_ID_OPUS ||
2679 av_dict_get(s->streams[i]->metadata, "stereo_mode", NULL, 0) ||
2680 av_dict_get(s->streams[i]->metadata, "alpha_mode", NULL, 0))
2681 version = 4;
2682 }
2683
2684 ebml_write_header(pb, s->oformat->name, version);
2686 put_ebml_size_unknown(pb, 8);
2687 mkv->segment_offset = avio_tell(pb);
2688
2689 // We write a SeekHead at the beginning to point to all other level
2690 // one elements (except Clusters).
2691 mkv_start_seekhead(mkv, pb);
2692
2693 ret = mkv_write_info(s);
2694 if (ret < 0)
2695 return ret;
2696
2697 ret = mkv_write_tracks(s);
2698 if (ret < 0)
2699 return ret;
2700
2701 ret = mkv_write_chapters(s);
2702 if (ret < 0)
2703 return ret;
2704
2705 if (!IS_WEBM(mkv)) {
2706 ret = mkv_write_attachments(s);
2707 if (ret < 0)
2708 return ret;
2709 }
2710
2711 /* Must come after mkv_write_chapters() to write chapter tags
2712 * into the same Tags element as the other tags. */
2713 ret = mkv_write_tags(s);
2714 if (ret < 0)
2715 return ret;
2716
2717 if (!IS_SEEKABLE(pb, mkv)) {
2718 ret = mkv_write_seekhead(pb, mkv, 0, avio_tell(pb));
2719 if (ret < 0)
2720 return ret;
2721 }
2722
2723 if (s->metadata_header_padding > 0) {
2724 if (s->metadata_header_padding == 1)
2725 s->metadata_header_padding++;
2726 put_ebml_void(pb, s->metadata_header_padding);
2727 }
2728
2729 if (mkv->reserve_cues_space || mkv->move_cues_to_front) {
2730 if (IS_SEEKABLE(pb, mkv)) {
2731 mkv->cues_pos = avio_tell(pb);
2732 if (mkv->reserve_cues_space >= 1) {
2733 if (mkv->reserve_cues_space == 1)
2734 mkv->reserve_cues_space++;
2736 }
2737 } else
2738 mkv->reserve_cues_space = -1;
2739 }
2740
2741 mkv->cluster_pos = -1;
2742
2743 // start a new cluster every 5 MB or 5 sec, or 32k / 1 sec for streaming or
2744 // after 4k and on a keyframe
2745 if (IS_SEEKABLE(pb, mkv)) {
2746 if (mkv->cluster_time_limit < 0)
2747 mkv->cluster_time_limit = 5000;
2748 if (mkv->cluster_size_limit < 0)
2749 mkv->cluster_size_limit = 5 * 1024 * 1024;
2750 } else {
2751 if (mkv->cluster_time_limit < 0)
2752 mkv->cluster_time_limit = 1000;
2753 if (mkv->cluster_size_limit < 0)
2754 mkv->cluster_size_limit = 32 * 1024;
2755 }
2756
2757 return 0;
2758}
2759
2760#if CONFIG_MATROSKA_MUXER
2761static int mkv_reformat_h2645(MatroskaMuxContext *mkv, AVIOContext *pb,
2762 const AVPacket *pkt, int *size)
2763{
2764 int ret;
2765 if (pb) {
2767 } else {
2768 ret = ff_nal_units_create_list(&mkv->cur_block.h2645_nalu_list, pkt->data, pkt->size);
2769 if (ret < 0)
2770 return ret;
2771 *size = ret;
2772 }
2773 return 0;
2774}
2775
2776static int mkv_reformat_wavpack(MatroskaMuxContext *mkv, AVIOContext *pb,
2777 const AVPacket *pkt, int *size)
2778{
2779 const uint8_t *src = pkt->data;
2780 int srclen = pkt->size;
2781 int offset = 0;
2782 int ret;
2783
2784 while (srclen >= WV_HEADER_SIZE) {
2786
2787 ret = ff_wv_parse_header(&header, src);
2788 if (ret < 0)
2789 return ret;
2791 srclen -= WV_HEADER_SIZE;
2792
2793 if (srclen < header.blocksize)
2794 return AVERROR_INVALIDDATA;
2795
2796 offset += 4 * !!header.initial + 8 + 4 * !(header.initial && header.final);
2797 if (pb) {
2798 if (header.initial)
2799 avio_wl32(pb, header.samples);
2800 avio_wl32(pb, header.flags);
2801 avio_wl32(pb, header.crc);
2802
2803 if (!(header.initial && header.final))
2804 avio_wl32(pb, header.blocksize);
2805
2806 avio_write(pb, src, header.blocksize);
2807 }
2808 src += header.blocksize;
2809 srclen -= header.blocksize;
2810 offset += header.blocksize;
2811 }
2812 *size = offset;
2813
2814 return 0;
2815}
2816#endif
2817
2819 const AVPacket *pkt, int *size)
2820{
2821 int ret = ff_av1_filter_obus(pb, pkt->data, pkt->size);
2822 if (ret < 0)
2823 return ret;
2824 *size = ret;
2825 return 0;
2826}
2827
2829 const AVPacket *pkt, int *size)
2830{
2831 const uint8_t *id, *settings;
2832 size_t id_size, settings_size;
2833 unsigned total = pkt->size + 2U;
2834
2835 if (total > INT_MAX)
2836 return AVERROR(ERANGE);
2837
2839 &id_size);
2841 &settings_size);
2842 if (id_size > INT_MAX - total || settings_size > INT_MAX - (total += id_size))
2843 return AVERROR(ERANGE);
2844 *size = total += settings_size;
2845 if (pb) {
2846 avio_write(pb, id, id_size);
2847 avio_w8(pb, '\n');
2848 avio_write(pb, settings, settings_size);
2849 avio_w8(pb, '\n');
2850 avio_write(pb, pkt->data, pkt->size);
2851 }
2852 return 0;
2853}
2854
2855static void mkv_write_blockadditional(EbmlWriter *writer, const uint8_t *buf,
2856 size_t size, uint64_t additional_id)
2857{
2859 ebml_writer_add_uint(writer, MATROSKA_ID_BLOCKADDID, additional_id);
2862}
2863
2864static int mkv_write_block(void *logctx, MatroskaMuxContext *mkv,
2865 AVIOContext *pb, const AVStream *st,
2866 mkv_track *track, const AVPacket *pkt,
2867 int keyframe, int64_t ts, uint64_t duration,
2868 int force_blockgroup, int64_t relative_packet_pos)
2869{
2870 const AVCodecParameters *par = st->codecpar;
2871 uint8_t t35_buf[6 + AV_HDR_PLUS_MAX_PAYLOAD_SIZE];
2872#define SMPTE_2094_APP5_MAX_SIZE 855
2873 uint8_t smpte_2094_app5_buf[5 + SMPTE_2094_APP5_MAX_SIZE];
2874 uint8_t *lcevc = NULL;
2875 uint8_t *side_data;
2876 size_t side_data_size;
2877 uint64_t additional_id;
2878 unsigned track_number = track->track_num;
2879 // BlockGroup, Block, BlockDuration, DiscardPadding, BlockReference
2880 // and BlockAdditions with three elements per BlockMore
2881 // Don't forget to increment the number of BlockMore when adding
2882 // support for writing a new blockadditional.
2883 EBML_WRITER(5 + (1 + 4 /* BlockMore */ * 3));
2884 int ret;
2885
2886 mkv->cur_block.track = track;
2887 mkv->cur_block.pkt = pkt;
2888 mkv->cur_block.rel_ts = ts - mkv->cluster_pts;
2889 mkv->cur_block.flags = 0;
2890
2891 /* Open a BlockGroup with a Block now; it will later be converted
2892 * to a SimpleBlock if possible. */
2894 ebml_writer_add_block(&writer, mkv);
2895
2896 if (duration > 0 && (par->codec_type == AVMEDIA_TYPE_SUBTITLE ||
2897 /* If the packet's duration is inconsistent with the default duration,
2898 * add an explicit duration element. */
2899 track->default_duration_high > 0 &&
2900 duration != track->default_duration_high &&
2901 duration != track->default_duration_low))
2903 else if (par->codec_id == AV_CODEC_ID_OPUS) {
2904 ret = parse_opus_packet_duration(pkt->data, pkt->size);
2905 if (ret >= 0) {
2906 /* If the packet's duration is inconsistent with the coded duration,
2907 * add an explicit duration element. */
2908 uint64_t parsed_duration = av_rescale_q(ret, (AVRational){1, 48000},
2909 st->time_base);
2910 if (parsed_duration != duration)
2912 }
2913 }
2914
2915 av_log(logctx, AV_LOG_DEBUG,
2916 "Writing block of size %d with pts %" PRId64 ", dts %" PRId64 ", "
2917 "duration %" PRId64 " at relative offset %" PRId64 " in cluster "
2918 "at offset %" PRId64 ". TrackNumber %u, keyframe %d\n",
2919 pkt->size, pkt->pts, pkt->dts, pkt->duration, relative_packet_pos,
2920 mkv->cluster_pos, track_number, keyframe != 0);
2921
2922 side_data = av_packet_get_side_data(pkt,
2924 &side_data_size);
2925 if (side_data && side_data_size >= 10) {
2926 int64_t discard_padding = AV_RL32(side_data + 4);
2927 if (discard_padding) {
2928 discard_padding = av_rescale_q(discard_padding,
2929 (AVRational){1, par->sample_rate},
2930 (AVRational){1, 1000000000});
2931 ebml_writer_add_sint(&writer, MATROSKA_ID_DISCARDPADDING, discard_padding);
2932 }
2933 }
2934
2936 side_data = av_packet_get_side_data(pkt,
2938 &side_data_size);
2939 if (side_data && side_data_size >= 8 &&
2940 // Only the Codec-specific BlockMore (id == 1) is currently supported.
2941 (additional_id = AV_RB64(side_data)) == MATROSKA_BLOCK_ADD_ID_TYPE_OPAQUE) {
2942 mkv_write_blockadditional(&writer, side_data + 8, side_data_size - 8,
2943 additional_id);
2944 track->max_blockaddid = FFMAX(track->max_blockaddid, additional_id);
2945 }
2946
2947 if (par->codec_type == AVMEDIA_TYPE_VIDEO) {
2948 side_data = av_packet_get_side_data(pkt,
2950 NULL);
2951 if (side_data) {
2952 uint8_t *payload = t35_buf;
2953 size_t payload_size = sizeof(t35_buf) - 6;
2954
2955 bytestream_put_byte(&payload, ITU_T_T35_COUNTRY_CODE_US);
2956 bytestream_put_be16(&payload, ITU_T_T35_PROVIDER_CODE_SAMSUNG);
2957 bytestream_put_be16(&payload, 0x01); // provider_oriented_code
2958 bytestream_put_byte(&payload, 0x04); // application_identifier
2959
2960 ret = av_dynamic_hdr_plus_to_t35((AVDynamicHDRPlus *)side_data, &payload,
2961 &payload_size);
2962 if (ret < 0)
2963 return ret;
2964
2965 mkv_write_blockadditional(&writer, t35_buf, payload_size + 6,
2967 track->max_blockaddid = FFMAX(track->max_blockaddid,
2969 }
2970 side_data = av_packet_get_side_data(pkt,
2972 NULL);
2973 if (side_data) {
2974 uint8_t *payload = smpte_2094_app5_buf;
2975 size_t payload_size = sizeof(smpte_2094_app5_buf) - 5;
2976
2977 bytestream_put_byte(&payload, ITU_T_T35_COUNTRY_CODE_US);
2978 bytestream_put_be16(&payload, ITU_T_T35_PROVIDER_CODE_SMPTE);
2979 bytestream_put_be16(&payload, 0x01); // provider_oriented_code
2980
2982 &payload, &payload_size);
2983 if (ret < 0)
2984 return ret;
2985
2986 mkv_write_blockadditional(&writer, smpte_2094_app5_buf, payload_size + 5,
2988 track->max_blockaddid = FFMAX(track->max_blockaddid,
2990 }
2992 &side_data_size);
2993 if (side_data) {
2994 size_t payload_size = side_data_size + 4;
2995
2996 lcevc = av_malloc(payload_size);
2997 if (!lcevc)
2998 return AVERROR(ENOMEM);
2999
3000 AV_WB8 (lcevc + 0, ITU_T_T35_COUNTRY_CODE_UK);
3001 AV_WB8 (lcevc + 1, 0); // t35_uk_country_code_second_octet
3003 memcpy (lcevc + 4, side_data, side_data_size);
3004
3005 mkv_write_blockadditional(&writer, lcevc, payload_size,
3007 track->max_blockaddid = FFMAX(track->max_blockaddid,
3009 }
3010 }
3011
3013
3014 if (!force_blockgroup && writer.nb_elements == 2) {
3015 /* Nothing except the BlockGroup + Block. Can use a SimpleBlock. */
3016 writer.elements++; // Skip the BlockGroup.
3017 writer.nb_elements--;
3018 av_assert2(writer.elements[0].id == MATROSKA_ID_BLOCK);
3019 writer.elements[0].id = MATROSKA_ID_SIMPLEBLOCK;
3020 if (keyframe)
3021 mkv->cur_block.flags |= 1 << 7;
3022 } else if (!keyframe)
3024 track->last_timestamp - ts);
3025
3026 ret = ebml_writer_write(&writer, pb);
3027
3028 av_free(lcevc);
3029
3030 return ret;
3031}
3032
3034{
3035 MatroskaMuxContext *mkv = s->priv_data;
3036 int ret;
3037
3038 if (!mkv->have_video) {
3039 for (unsigned i = 0; i < s->nb_streams; i++)
3040 mkv->tracks[i].has_cue = 0;
3041 }
3042 mkv->cluster_pos = -1;
3043 ret = end_ebml_master_crc32(s->pb, &mkv->cluster_bc, mkv,
3044 MATROSKA_ID_CLUSTER, 0, 1, 0);
3045 if (ret < 0)
3046 return ret;
3047
3049 return 0;
3050}
3051
3053{
3054 MatroskaMuxContext *mkv = s->priv_data;
3055 mkv_track *track = &mkv->tracks[pkt->stream_index];
3056 AVCodecParameters *par = s->streams[pkt->stream_index]->codecpar;
3057 uint8_t *side_data;
3058 size_t side_data_size;
3059 int ret;
3060
3062 &side_data_size);
3063
3064 switch (par->codec_id) {
3065#if CONFIG_MATROSKA_MUXER
3066 case AV_CODEC_ID_AAC:
3067 if (side_data_size && mkv->track.bc) {
3068 int output_sample_rate = 0;
3069 ret = get_aac_sample_rates(s, mkv, side_data, side_data_size,
3070 &track->sample_rate, &output_sample_rate);
3071 if (ret < 0)
3072 return ret;
3073 if (!output_sample_rate)
3074 output_sample_rate = track->sample_rate; // Space is already reserved, so it's this or a void element.
3075 ret = mkv_update_codecprivate(s, mkv, side_data, side_data_size,
3076 par, mkv->track.bc, track, 0);
3077 if (ret < 0)
3078 return ret;
3079 avio_seek(mkv->track.bc, track->sample_rate_offset, SEEK_SET);
3081 put_ebml_float(mkv->track.bc, MATROSKA_ID_AUDIOOUTSAMPLINGFREQ, output_sample_rate);
3082 } else if (!par->extradata_size && !track->sample_rate) {
3083 // No extradata (codecpar or packet side data).
3084 av_log(s, AV_LOG_ERROR, "Error parsing AAC extradata, unable to determine samplerate.\n");
3085 return AVERROR(EINVAL);
3086 }
3087 break;
3088 case AV_CODEC_ID_FLAC:
3089 if (side_data_size && mkv->track.bc) {
3090 if (side_data_size != par->extradata_size) {
3091 av_log(s, AV_LOG_ERROR, "Invalid FLAC STREAMINFO metadata for output stream %d\n",
3092 pkt->stream_index);
3093 return AVERROR(EINVAL);
3094 }
3095 ret = mkv_update_codecprivate(s, mkv, side_data, side_data_size,
3096 par, mkv->track.bc, track, 0);
3097 if (ret < 0)
3098 return ret;
3099 }
3100 break;
3101#endif
3102 // FIXME: Remove the following once libaom starts propagating proper extradata during init()
3103 // See https://bugs.chromium.org/p/aomedia/issues/detail?id=2208
3104 case AV_CODEC_ID_AV1:
3105 if (side_data_size && mkv->track.bc && !par->extradata_size) {
3106 // If the reserved space doesn't suffice, only write
3107 // the first four bytes of the av1C.
3108 ret = mkv_update_codecprivate(s, mkv, side_data, side_data_size,
3109 par, mkv->track.bc, track, 4);
3110 if (ret < 0)
3111 return ret;
3112 } else if (!par->extradata_size)
3113 return AVERROR_INVALIDDATA;
3114 break;
3115 default:
3116 if (side_data_size)
3117 av_log(s, AV_LOG_DEBUG, "Ignoring new extradata in a packet for stream %d.\n", pkt->stream_index);
3118 break;
3119 }
3120
3121 return 0;
3122}
3123
3125{
3126 MatroskaMuxContext *mkv = s->priv_data;
3127 AVIOContext *pb;
3128 AVStream *st = s->streams[pkt->stream_index];
3129 AVCodecParameters *par = s->streams[pkt->stream_index]->codecpar;
3130 mkv_track *track = &mkv->tracks[pkt->stream_index];
3131 int is_sub = par->codec_type == AVMEDIA_TYPE_SUBTITLE;
3132 /* All subtitle blocks are considered to be keyframes. */
3133 int keyframe = is_sub || !!(pkt->flags & AV_PKT_FLAG_KEY);
3134 int64_t duration = FFMAX(pkt->duration, 0);
3135 int64_t cue_duration = is_sub ? duration : 0;
3136 int ret;
3137 int64_t ts = track->write_dts ? pkt->dts : pkt->pts;
3138 int64_t relative_packet_pos;
3139
3140 if (ts == AV_NOPTS_VALUE) {
3141 av_log(s, AV_LOG_ERROR, "Can't write packet with unknown timestamp\n");
3142 return AVERROR(EINVAL);
3143 }
3144 ts += track->ts_offset;
3145
3146 if (mkv->cluster_pos != -1) {
3147 int64_t cluster_time = ts - mkv->cluster_pts;
3148 if ((int16_t)cluster_time != cluster_time) {
3149 ret = mkv_end_cluster(s);
3150 if (ret < 0)
3151 return ret;
3152 av_log(s, AV_LOG_WARNING, "Starting new cluster due to timestamp\n");
3153 }
3154 }
3155
3156 if (mkv->cluster_pos == -1) {
3157 ret = start_ebml_master_crc32(&mkv->cluster_bc, mkv);
3158 if (ret < 0)
3159 return ret;
3160 mkv->cluster_bc->direct = 1;
3161 mkv->cluster_pos = avio_tell(s->pb);
3163 mkv->cluster_pts = FFMAX(0, ts);
3165 "Starting new cluster with timestamp "
3166 "%" PRId64 " at offset %" PRId64 " bytes\n",
3167 mkv->cluster_pts, mkv->cluster_pos);
3168 }
3169 pb = mkv->cluster_bc;
3170
3171 relative_packet_pos = avio_tell(pb);
3172
3173 /* The WebM spec requires WebVTT to be muxed in BlockGroups;
3174 * so we force it even for packets without duration. */
3175 ret = mkv_write_block(s, mkv, pb, st, track, pkt,
3176 keyframe, ts, duration,
3178 relative_packet_pos);
3179 if (ret < 0)
3180 return ret;
3181 if (keyframe && IS_SEEKABLE(s->pb, mkv) &&
3182 (par->codec_type == AVMEDIA_TYPE_VIDEO ||
3184 !mkv->have_video && !track->has_cue)) {
3185 ret = mkv_add_cuepoint(mkv, pkt->stream_index, ts,
3186 mkv->cluster_pos, relative_packet_pos,
3187 cue_duration);
3188 if (ret < 0)
3189 return ret;
3190 track->has_cue = 1;
3191 }
3192
3193 track->last_timestamp = ts;
3194 mkv->duration = FFMAX(mkv->duration, ts + duration);
3195 track->duration = FFMAX(track->duration, ts + duration);
3196
3197 return 0;
3198}
3199
3201{
3202 MatroskaMuxContext *mkv = s->priv_data;
3203 int codec_type = s->streams[pkt->stream_index]->codecpar->codec_type;
3204 int keyframe = !!(pkt->flags & AV_PKT_FLAG_KEY);
3205 int cluster_size;
3206 int64_t cluster_time;
3207 int ret;
3208 int start_new_cluster;
3209
3211 if (ret < 0)
3212 return ret;
3213
3214 if (mkv->cluster_pos != -1) {
3215 if (mkv->tracks[pkt->stream_index].write_dts)
3216 cluster_time = pkt->dts - mkv->cluster_pts;
3217 else
3218 cluster_time = pkt->pts - mkv->cluster_pts;
3219 cluster_time += mkv->tracks[pkt->stream_index].ts_offset;
3220
3221 cluster_size = avio_tell(mkv->cluster_bc);
3222
3223 if (mkv->is_dash && codec_type == AVMEDIA_TYPE_VIDEO) {
3224 // WebM DASH specification states that the first block of
3225 // every Cluster has to be a key frame. So for DASH video,
3226 // we only create a Cluster on seeing key frames.
3227 start_new_cluster = keyframe;
3228 } else if (mkv->is_dash && codec_type == AVMEDIA_TYPE_AUDIO &&
3229 cluster_time > mkv->cluster_time_limit) {
3230 // For DASH audio, we create a Cluster based on cluster_time_limit.
3231 start_new_cluster = 1;
3232 } else if (!mkv->is_dash &&
3233 (cluster_size > mkv->cluster_size_limit ||
3234 cluster_time > mkv->cluster_time_limit ||
3235 (codec_type == AVMEDIA_TYPE_VIDEO && keyframe &&
3236 cluster_size > 4 * 1024))) {
3237 start_new_cluster = 1;
3238 } else
3239 start_new_cluster = 0;
3240
3241 if (start_new_cluster) {
3242 ret = mkv_end_cluster(s);
3243 if (ret < 0)
3244 return ret;
3245 }
3246 }
3247
3248 if (mkv->cluster_pos == -1)
3249 avio_write_marker(s->pb,
3250 av_rescale_q(pkt->dts, s->streams[pkt->stream_index]->time_base, AV_TIME_BASE_Q),
3252
3253 // check if we have an audio packet cached
3254 if (mkv->cur_audio_pkt->size > 0) {
3257 if (ret < 0) {
3259 "Could not write cached audio packet ret:%d\n", ret);
3260 return ret;
3261 }
3262 }
3263
3264 // buffer an audio packet to ensure the packet containing the video
3265 // keyframe's timecode is contained in the same cluster for WebM
3267 if (pkt->size > 0)
3268 ret = av_packet_ref(mkv->cur_audio_pkt, pkt);
3269 } else
3271 return ret;
3272}
3273
3275{
3276 MatroskaMuxContext *mkv = s->priv_data;
3277
3278 if (!pkt) {
3279 if (mkv->cluster_pos != -1) {
3280 int ret = mkv_end_cluster(s);
3281 if (ret < 0)
3282 return ret;
3284 "Flushing cluster at offset %" PRIu64 " bytes\n",
3285 avio_tell(s->pb));
3286 }
3287 return 1;
3288 }
3289 return mkv_write_packet(s, pkt);
3290}
3291
3293{
3294 MatroskaMuxContext *mkv = s->priv_data;
3295 AVIOContext *pb = s->pb;
3296 int64_t endpos, ret64;
3297 int ret, ret2 = 0;
3298
3299 // check if we have an audio packet cached
3300 if (mkv->cur_audio_pkt->size > 0) {
3302 if (ret < 0) {
3304 "Could not write cached audio packet ret:%d\n", ret);
3305 return ret;
3306 }
3307 }
3308
3309 if (mkv->cluster_pos != -1) {
3310 ret = end_ebml_master_crc32(pb, &mkv->cluster_bc, mkv,
3311 MATROSKA_ID_CLUSTER, 0, 0, 0);
3312 if (ret < 0)
3313 return ret;
3314 }
3315
3316 ret = mkv_write_chapters(s);
3317 if (ret < 0)
3318 return ret;
3319
3320 if (!IS_SEEKABLE(pb, mkv))
3321 return 0;
3322
3323 endpos = avio_tell(pb);
3324
3325 if (mkv->cues.num_entries && mkv->reserve_cues_space >= 0) {
3326 AVIOContext *cues = NULL;
3327 uint64_t size, offset = 0;
3328 int length_size = 0;
3329
3330redo_cues:
3331 ret = start_ebml_master_crc32(&cues, mkv);
3332 if (ret < 0)
3333 return ret;
3334
3335 ret = mkv_assemble_cues(s->streams, cues, mkv->tmp_bc, &mkv->cues,
3336 mkv->tracks, s->nb_streams, offset);
3337 if (ret < 0) {
3338 ffio_free_dyn_buf(&cues);
3339 return ret;
3340 }
3341
3342 if (mkv->reserve_cues_space || mkv->move_cues_to_front) {
3343 size = avio_tell(cues);
3344 length_size = ebml_length_size(size);
3345 size += 4 + length_size;
3346 if (offset + mkv->reserve_cues_space < size) {
3347 if (mkv->move_cues_to_front) {
3349 ffio_reset_dyn_buf(cues);
3350 goto redo_cues;
3351 }
3353 "Insufficient space reserved for Cues: "
3354 "%d < %"PRIu64". No Cues will be output.\n",
3355 mkv->reserve_cues_space, size);
3356 ret2 = AVERROR(EINVAL);
3357 goto after_cues;
3358 } else {
3359 if (offset) {
3361 offset);
3362 if (ret < 0) {
3363 ffio_free_dyn_buf(&cues);
3364 return ret;
3365 }
3366 endpos += offset;
3367 }
3368 if ((ret64 = avio_seek(pb, mkv->cues_pos, SEEK_SET)) < 0) {
3369 ffio_free_dyn_buf(&cues);
3370 return ret64;
3371 }
3372 if (mkv->reserve_cues_space == size + 1) {
3373 /* There is no way to reserve a single byte because
3374 * the minimal size of an EBML Void element is 2
3375 * (1 byte ID, 1 byte length field). This problem
3376 * is solved by writing the Cues' length field on
3377 * one byte more than necessary. */
3378 length_size++;
3379 size++;
3380 }
3381 }
3382 }
3383 ret = end_ebml_master_crc32(pb, &cues, mkv, MATROSKA_ID_CUES,
3384 length_size, 0, 1);
3385 if (ret < 0)
3386 return ret;
3387 if (mkv->reserve_cues_space) {
3388 if (size < mkv->reserve_cues_space)
3390 } else if (!mkv->move_cues_to_front)
3391 endpos = avio_tell(pb);
3392 }
3393
3394after_cues:
3395 /* Lengths greater than (1ULL << 56) - 1 can't be represented
3396 * via an EBML number, so leave the unknown length field. */
3397 if (endpos - mkv->segment_offset < (1ULL << 56) - 1) {
3398 if ((ret64 = avio_seek(pb, mkv->segment_offset - 8, SEEK_SET)) < 0)
3399 return ret64;
3400 put_ebml_length(pb, endpos - mkv->segment_offset, 8);
3401 }
3402
3403 ret = mkv_write_seekhead(pb, mkv, 1, mkv->info.pos);
3404 if (ret < 0)
3405 return ret;
3406
3407 if (mkv->info.bc) {
3408 // update the duration
3409 av_log(s, AV_LOG_DEBUG, "end duration = %" PRIu64 "\n", mkv->duration);
3410 avio_seek(mkv->info.bc, mkv->duration_offset, SEEK_SET);
3412 ret = end_ebml_master_crc32(pb, &mkv->info.bc, mkv,
3413 MATROSKA_ID_INFO, 0, 0, 0);
3414 if (ret < 0)
3415 return ret;
3416 }
3417
3418 if (mkv->track.bc) {
3419 // write Tracks master
3420 AVIOContext *track_bc = mkv->track.bc;
3421
3422 for (unsigned i = 0; i < s->nb_streams; i++) {
3423 const mkv_track *track = &mkv->tracks[i];
3424
3425 if (!track->max_blockaddid)
3426 continue;
3427
3428 // We reserved a single byte to write this value.
3429 av_assert0(track->max_blockaddid <= 0xFF);
3430
3431 avio_seek(track_bc, track->blockadditionmapping_offset, SEEK_SET);
3432
3434 track->max_blockaddid);
3436 ebml_master mapping_master = start_ebml_master(track_bc, MATROSKA_ID_TRACKBLKADDMAPPING, 8);
3441 end_ebml_master(track_bc, mapping_master);
3442 }
3443 }
3444
3445 avio_seek(pb, mkv->track.pos, SEEK_SET);
3446 ret = end_ebml_master_crc32(pb, &mkv->track.bc, mkv,
3447 MATROSKA_ID_TRACKS, 0, 0, 0);
3448 if (ret < 0)
3449 return ret;
3450 }
3451
3452 // update stream durations
3453 if (mkv->tags.bc) {
3454 AVIOContext *tags_bc = mkv->tags.bc;
3455 int i;
3456 for (i = 0; i < s->nb_streams; ++i) {
3457 const AVStream *st = s->streams[i];
3458 const mkv_track *track = &mkv->tracks[i];
3459
3460 if (track->duration_offset > 0) {
3461 double duration_sec = track->duration * av_q2d(st->time_base);
3462 char duration_string[DURATION_STRING_LENGTH + 1] = "";
3463 ebml_master simpletag;
3464
3465 av_log(s, AV_LOG_DEBUG, "stream %d end duration = %" PRIu64 "\n", i,
3466 track->duration);
3467
3468 avio_seek(tags_bc, track->duration_offset, SEEK_SET);
3469 simpletag = start_ebml_master(tags_bc, MATROSKA_ID_SIMPLETAG,
3470 2 + 1 + 8 + 23);
3471 put_ebml_string(tags_bc, MATROSKA_ID_TAGNAME, "DURATION");
3472
3473 snprintf(duration_string, sizeof(duration_string), "%02d:%02d:%012.9f",
3474 (int) duration_sec / 3600, ((int) duration_sec / 60) % 60,
3475 fmod(duration_sec, 60));
3476
3478 duration_string, DURATION_STRING_LENGTH);
3479 end_ebml_master(tags_bc, simpletag);
3480 }
3481 }
3482
3483 avio_seek(pb, mkv->tags.pos, SEEK_SET);
3484 ret = end_ebml_master_crc32(pb, &mkv->tags.bc, mkv,
3485 MATROSKA_ID_TAGS, 0, 0, 0);
3486 if (ret < 0)
3487 return ret;
3488 }
3489
3490 avio_seek(pb, endpos, SEEK_SET);
3491
3492 return ret2;
3493}
3494
3495static uint64_t mkv_get_uid(const mkv_track *tracks, int i, AVLFG *c)
3496{
3497 while (1) {
3498 uint64_t uid;
3499 int k;
3500 uid = (uint64_t)av_lfg_get(c) << 32;
3501 uid |= av_lfg_get(c);
3502 if (!uid)
3503 continue;
3504 for (k = 0; k < i; k++) {
3505 if (tracks[k].uid == uid)
3506 break;
3507 }
3508 if (k == i)
3509 return uid;
3510 }
3511}
3512
3513static int mkv_init(struct AVFormatContext *s)
3514{
3515 FFFormatContext *const si = ffformatcontext(s);
3516 MatroskaMuxContext *mkv = s->priv_data;
3517 AVLFG c;
3518 unsigned nb_tracks = 0;
3519 int i;
3520
3521 mkv->ctx = s;
3522
3523 for (i = 0; i < s->nb_streams; i++) {
3524 if (s->streams[i]->codecpar->codec_id == AV_CODEC_ID_ATRAC3 ||
3525 s->streams[i]->codecpar->codec_id == AV_CODEC_ID_COOK ||
3526 s->streams[i]->codecpar->codec_id == AV_CODEC_ID_RA_288 ||
3527 s->streams[i]->codecpar->codec_id == AV_CODEC_ID_SIPR ||
3528 s->streams[i]->codecpar->codec_id == AV_CODEC_ID_RV10 ||
3529 s->streams[i]->codecpar->codec_id == AV_CODEC_ID_RV20 ||
3530 s->streams[i]->codecpar->codec_id == AV_CODEC_ID_RV30) {
3532 "The Matroska muxer does not yet support muxing %s\n",
3533 avcodec_get_name(s->streams[i]->codecpar->codec_id));
3534 return AVERROR_PATCHWELCOME;
3535 }
3536 }
3537
3538 if (s->avoid_negative_ts < 0) {
3539 s->avoid_negative_ts = 1;
3541 }
3542
3543 if (!CONFIG_MATROSKA_MUXER ||
3544 (CONFIG_WEBM_MUXER && !strcmp(s->oformat->name, "webm"))) {
3545 mkv->mode = MODE_WEBM;
3546 mkv->write_crc = 0;
3547 } else
3548 mkv->mode = MODE_MATROSKAv2;
3549
3551
3552 mkv->tracks = av_calloc(s->nb_streams, sizeof(*mkv->tracks));
3553 if (!mkv->tracks)
3554 return AVERROR(ENOMEM);
3555
3556 if (!(s->flags & AVFMT_FLAG_BITEXACT)) {
3558
3559 // Calculate the SegmentUID now in order not to waste our random seed.
3560 for (i = 0; i < 4; i++)
3561 mkv->segment_uid[i] = av_lfg_get(&c);
3562 }
3563
3564 for (i = 0; i < s->nb_streams; i++) {
3565 AVStream *st = s->streams[i];
3566 const AVCodecParameters *const par = st->codecpar;
3567 mkv_track *track = &mkv->tracks[i];
3568
3569 switch (par->codec_id) {
3570#if CONFIG_MATROSKA_MUXER
3572 track->reformat = mkv_reformat_wavpack;
3573 break;
3574 case AV_CODEC_ID_H264:
3575 case AV_CODEC_ID_HEVC:
3576 case AV_CODEC_ID_VVC:
3577 if (((par->codec_id == AV_CODEC_ID_H264 && par->extradata_size > 0) ||
3578 (par->codec_id == AV_CODEC_ID_HEVC && par->extradata_size > 6) ||
3579 (par->codec_id == AV_CODEC_ID_VVC && par->extradata_size >= 6)) &&
3580 (AV_RB24(par->extradata) == 1 || AV_RB32(par->extradata) == 1))
3581 track->reformat = mkv_reformat_h2645;
3582 break;
3583 case AV_CODEC_ID_PRORES:
3584 /* Matroska specification requires to remove
3585 * the first QuickTime atom. */
3586 track->offset = 8;
3587 break;
3588#endif
3589 case AV_CODEC_ID_AV1:
3590 track->reformat = mkv_reformat_av1;
3591 break;
3592 case AV_CODEC_ID_WEBVTT:
3593 track->reformat = webm_reformat_vtt;
3594 break;
3595 }
3596
3597 if (s->flags & AVFMT_FLAG_BITEXACT) {
3598 track->uid = i + 1;
3599 } else {
3600 track->uid = mkv_get_uid(mkv->tracks, i, &c);
3601 }
3602
3603 // ms precision is the de-facto standard timescale for mkv files
3604 avpriv_set_pts_info(st, 64, 1, 1000);
3605
3607 if (IS_WEBM(mkv)) {
3608 av_log(s, AV_LOG_WARNING, "Stream %d will be ignored "
3609 "as WebM doesn't support attachments.\n", i);
3610 } else if (!get_mimetype(st)) {
3611 av_log(s, AV_LOG_ERROR, "Attachment stream %d has no mimetype "
3612 "tag and it cannot be deduced from the codec id.\n", i);
3613 return AVERROR(EINVAL);
3614 }
3615 mkv->nb_attachments++;
3616 continue;
3617 }
3618
3619 nb_tracks++;
3620 track->track_num = mkv->is_dash ? mkv->dash_track_number : nb_tracks;
3621 track->track_num_size = ebml_num_size(track->track_num);
3622 }
3623
3624 if (mkv->is_dash && nb_tracks != 1)
3625 return AVERROR(EINVAL);
3626
3627 return 0;
3628}
3629
3631 const AVPacket *pkt)
3632{
3633 int ret = 1;
3634
3635 if (CONFIG_MATROSKA_MUXER && st->codecpar->codec_id == AV_CODEC_ID_AAC) {
3636 if (pkt->size > 2 && (AV_RB16(pkt->data) & 0xfff0) == 0xfff0)
3637 ret = ff_stream_add_bitstream_filter(st, "aac_adtstoasc", NULL);
3638 } else if (st->codecpar->codec_id == AV_CODEC_ID_VP9) {
3639 ret = ff_stream_add_bitstream_filter(st, "vp9_superframe", NULL);
3640 } else if (CONFIG_MATROSKA_MUXER &&
3642 ret = ff_stream_add_bitstream_filter(st, "pgs_frame_merge", NULL);
3643 }
3644
3645 return ret;
3646}
3647
3649 { AV_CODEC_ID_ALAC, 0XFFFFFFFF },
3650 { AV_CODEC_ID_ATRAC1, 0xFFFFFFFF },
3651 { AV_CODEC_ID_MLP, 0xFFFFFFFF },
3652 { AV_CODEC_ID_OPUS, 0xFFFFFFFF },
3653 { AV_CODEC_ID_PCM_S16BE, 0xFFFFFFFF },
3654 { AV_CODEC_ID_PCM_S24BE, 0xFFFFFFFF },
3655 { AV_CODEC_ID_PCM_S32BE, 0xFFFFFFFF },
3656 { AV_CODEC_ID_QDMC, 0xFFFFFFFF },
3657 { AV_CODEC_ID_QDM2, 0xFFFFFFFF },
3658 { AV_CODEC_ID_RA_144, 0xFFFFFFFF },
3659 { AV_CODEC_ID_TRUEHD, 0xFFFFFFFF },
3660 { AV_CODEC_ID_NONE, 0xFFFFFFFF }
3661};
3662
3664 { AV_CODEC_ID_DVB_SUBTITLE, 0xFFFFFFFF },
3665 { AV_CODEC_ID_DVD_SUBTITLE, 0xFFFFFFFF },
3666 { AV_CODEC_ID_HDMV_PGS_SUBTITLE, 0xFFFFFFFF },
3667 { AV_CODEC_ID_ARIB_CAPTION, 0xFFFFFFFF },
3668 { AV_CODEC_ID_NONE, 0xFFFFFFFF }
3669};
3670
3671#define OFFSET(x) offsetof(MatroskaMuxContext, x)
3672#define FLAGS AV_OPT_FLAG_ENCODING_PARAM
3673static const AVOption options[] = {
3674 { "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 },
3675 { "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 },
3676 { "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 },
3677 { "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 },
3678 { "dash", "create a WebM file conforming to WebM DASH specification", OFFSET(is_dash), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
3679 { "dash_track_number", "track number for the DASH stream", OFFSET(dash_track_number), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, INT_MAX, FLAGS },
3680 { "live", "write files assuming it is a live stream", OFFSET(is_live), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
3681 { "allow_raw_vfw", "allow raw VFW mode", OFFSET(allow_raw_vfw), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
3682 { "flipped_raw_rgb", "store raw RGB bitmaps in VFW mode in bottom-up mode", OFFSET(flipped_raw_rgb), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
3683 { "write_crc32", "write a CRC32 element inside every Level 1 element", OFFSET(write_crc), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, FLAGS },
3684 { "default_mode", "control how a track's FlagDefault is inferred", OFFSET(default_mode), AV_OPT_TYPE_INT, { .i64 = DEFAULT_MODE_PASSTHROUGH }, DEFAULT_MODE_INFER, DEFAULT_MODE_PASSTHROUGH, FLAGS, .unit = "default_mode" },
3685 { "infer", "for each track type, mark each track of disposition default as default; if none exists, mark the first track as default", 0, AV_OPT_TYPE_CONST, { .i64 = DEFAULT_MODE_INFER }, 0, 0, FLAGS, .unit = "default_mode" },
3686 { "infer_no_subs", "for each track type, mark each track of disposition default as default; for audio and video: if none exists, mark the first track as default", 0, AV_OPT_TYPE_CONST, { .i64 = DEFAULT_MODE_INFER_NO_SUBS }, 0, 0, FLAGS, .unit = "default_mode" },
3687 { "passthrough", "use the disposition flag as-is", 0, AV_OPT_TYPE_CONST, { .i64 = DEFAULT_MODE_PASSTHROUGH }, 0, 0, FLAGS, .unit = "default_mode" },
3688 { NULL },
3689};
3690
3692 .class_name = "matroska/webm muxer",
3693 .item_name = av_default_item_name,
3694 .option = options,
3695 .version = LIBAVUTIL_VERSION_INT,
3696};
3697
3698#if CONFIG_MATROSKA_MUXER
3699static int mkv_query_codec(enum AVCodecID codec_id, int std_compliance)
3700{
3701 for (int i = 0; ff_mkv_codec_tags[i].id != AV_CODEC_ID_NONE; i++)
3702 if (ff_mkv_codec_tags[i].id == codec_id)
3703 return 1;
3704
3705 if (std_compliance < FF_COMPLIANCE_NORMAL) {
3707 // mkv theoretically supports any video/audio through VFW/ACM
3709 return 1;
3710 }
3711
3712 return 0;
3713}
3714
3716 .p.name = "matroska",
3717 .p.long_name = NULL_IF_CONFIG_SMALL("Matroska"),
3718 .p.mime_type = "video/x-matroska",
3719 .p.extensions = "mkv",
3720 .priv_data_size = sizeof(MatroskaMuxContext),
3721 .p.audio_codec = CONFIG_LIBVORBIS_ENCODER ?
3723 .p.video_codec = CONFIG_LIBX264_ENCODER ?
3725 .init = mkv_init,
3726 .deinit = mkv_deinit,
3732 .p.codec_tag = (const AVCodecTag* const []){
3735 },
3736 .p.subtitle_codec = AV_CODEC_ID_ASS,
3737 .query_codec = mkv_query_codec,
3738 .check_bitstream = mkv_check_bitstream,
3739 .p.priv_class = &matroska_webm_class,
3740 .flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH,
3741};
3742#endif
3743
3744#if CONFIG_WEBM_MUXER
3745static int webm_query_codec(enum AVCodecID codec_id, int std_compliance)
3746{
3747 for (int i = 0; ff_webm_codec_tags[i].id != AV_CODEC_ID_NONE; i++)
3748 if (ff_webm_codec_tags[i].id == codec_id)
3749 return 1;
3750
3751 return 0;
3752}
3753
3755 .p.name = "webm",
3756 .p.long_name = NULL_IF_CONFIG_SMALL("WebM"),
3757 .p.mime_type = "video/webm",
3758 .p.extensions = "webm",
3759 .priv_data_size = sizeof(MatroskaMuxContext),
3760 .p.audio_codec = CONFIG_LIBOPUS_ENCODER ? AV_CODEC_ID_OPUS : AV_CODEC_ID_VORBIS,
3761 .p.video_codec = CONFIG_LIBVPX_VP9_ENCODER? AV_CODEC_ID_VP9 : AV_CODEC_ID_VP8,
3762 .p.subtitle_codec = AV_CODEC_ID_WEBVTT,
3763 .init = mkv_init,
3764 .deinit = mkv_deinit,
3768 .query_codec = webm_query_codec,
3772 .p.priv_class = &matroska_webm_class,
3773 .flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH,
3774};
3775#endif
3776
3777#if CONFIG_MATROSKA_AUDIO_MUXER
3779 .p.name = "matroska",
3780 .p.long_name = NULL_IF_CONFIG_SMALL("Matroska Audio"),
3781 .p.mime_type = "audio/x-matroska",
3782 .p.extensions = "mka",
3783 .priv_data_size = sizeof(MatroskaMuxContext),
3784 .p.audio_codec = CONFIG_LIBVORBIS_ENCODER ?
3786 .p.video_codec = AV_CODEC_ID_NONE,
3787 .init = mkv_init,
3788 .deinit = mkv_deinit,
3794 .p.codec_tag = (const AVCodecTag* const []){
3796 },
3797 .p.priv_class = &matroska_webm_class,
3798 .flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH,
3799};
3800#endif
static double val(void *priv, double ch)
Definition aeval.c:77
static const char *const format[]
Definition af_aiir.c:444
static void bit_depth(AudioStatsContext *s, const uint64_t *const mask, uint8_t *depth)
Definition af_astats.c:246
const FFOutputFormat ff_webm_muxer
const FFOutputFormat ff_matroska_audio_muxer
const FFOutputFormat ff_matroska_muxer
#define entry
int32_t
int ff_isom_write_av1c(AVIOContext *pb, const uint8_t *buf, int size, int write_seq_header)
Writes AV1 extradata (Sequence Header and Metadata OBUs) to the provided AVIOContext.
Definition av1.c:399
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:83
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition avassert.h:68
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition avassert.h:58
#define av_unreachable(msg)
Asserts that are used as compiler optimization hints depending upon ASSERT_LEVEL and NBDEBUG.
Definition avassert.h:109
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
int ff_isom_write_avcc(AVIOContext *pb, const uint8_t *data, int len)
Definition avc.c:32
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:834
Main libavformat public API header.
#define AVFMT_FLAG_BITEXACT
When muxing, try to avoid writing any random/volatile data to the output.
Definition avformat.h:1501
#define AV_DISPOSITION_HEARING_IMPAIRED
The stream is intended for hearing impaired audiences.
Definition avformat.h:676
#define AV_DISPOSITION_COMMENT
The stream is a commentary track.
Definition avformat.h:657
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition avformat.h:501
#define AV_DISPOSITION_CAPTIONS
The subtitle stream contains captions, providing a transcription and possibly a translation of audio.
Definition avformat.h:710
#define AV_DISPOSITION_DUB
The stream is not in original language.
Definition avformat.h:647
#define AV_DISPOSITION_METADATA
The subtitle stream contains time-aligned metadata that is not intended to be directly presented to t...
Definition avformat.h:721
#define AVFMT_TS_NONSTRICT
Format does not require strictly increasing timestamps, but they must still be monotonic.
Definition avformat.h:507
#define AV_DISPOSITION_FORCED
Track should be used during playback by default.
Definition avformat.h:672
#define AV_DISPOSITION_VISUAL_IMPAIRED
The stream is intended for visually impaired audiences.
Definition avformat.h:680
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition avformat.h:497
#define AV_DISPOSITION_DESCRIPTIONS
The subtitle stream contains a textual description of the video content.
Definition avformat.h:716
#define AV_DISPOSITION_ORIGINAL
The stream is in original language.
Definition avformat.h:653
#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:639
void avio_write_marker(AVIOContext *s, int64_t time, enum AVIODataMarkerType type)
Mark the written bytestream as a specific type.
Definition aviobuf.c:464
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition avio.h:41
void avio_wl32(AVIOContext *s, unsigned int val)
Definition aviobuf.c:360
void avio_wl16(AVIOContext *s, unsigned int val)
Definition aviobuf.c:440
void avio_w8(AVIOContext *s, int b)
Definition aviobuf.c:184
void avio_wb32(AVIOContext *s, unsigned int val)
Definition aviobuf.c:368
void avio_wb16(AVIOContext *s, unsigned int val)
Definition aviobuf.c:446
@ AVIO_DATA_MARKER_BOUNDARY_POINT
A point in the output bytestream where a demuxer can start parsing (for non self synchronizing bytest...
Definition avio.h:127
@ AVIO_DATA_MARKER_FLUSH_POINT
A point in the output bytestream where the underlying AVIOContext might flush the buffer depending on...
Definition avio.h:145
@ AVIO_DATA_MARKER_SYNC_POINT
A point in the output bytestream where a decoder can start decoding (i.e.
Definition avio.h:121
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition avio.h:494
void avio_wb24(AVIOContext *s, unsigned int val)
Definition aviobuf.c:458
void avio_wb64(AVIOContext *s, uint64_t val)
Definition aviobuf.c:434
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition aviobuf.c:206
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition aviobuf.c:1365
int avio_get_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition aviobuf.c:1377
void ffio_free_dyn_buf(AVIOContext **s)
Free a dynamic buffer.
Definition aviobuf.c:1438
void ffio_reset_dyn_buf(AVIOContext *s)
Reset a dynamic buffer.
Definition aviobuf.c:1399
void ffio_fill(AVIOContext *s, int b, int64_t count)
Definition aviobuf.c:192
const char * ff_convert_lang_to(const char *lang, enum AVLangCodespace target_codespace)
Convert a language code to a target codespace.
Definition avlanguage.c:741
@ AV_LANG_ISO639_2_BIBL
Definition avlanguage.h:28
static void BS_FUNC skip(BSCTX *bc, unsigned int n)
Skip n bits in the buffer.
static int BS_FUNC left(const BSCTX *bc)
Return the number of the bits left in a buffer.
static int FUNC metadata(CodedBitstreamContext *ctx, RWContext *rw, APVRawMetadata *current)
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
Public libavutil channel layout APIs header.
static void deinit(AVFormatContext *s)
Definition chromaprint.c:53
#define FLAGS
Definition cmdutils.c:598
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
Public header for CRC hash function implementation.
#define max(a, b)
static int16_t block[64]
Definition dct.c:125
Misc types and constants that do not belong anywhere else.
#define AV_PROFILE_ARIB_PROFILE_A
Definition defs.h:191
#define AV_PROFILE_ARIB_PROFILE_C
Definition defs.h:192
#define FF_COMPLIANCE_NORMAL
Definition defs.h:60
AVFieldOrder
Definition defs.h:211
@ AV_FIELD_TT
Top coded_first, top displayed first.
Definition defs.h:214
@ AV_FIELD_BB
Bottom coded first, bottom displayed first.
Definition defs.h:215
@ AV_FIELD_UNKNOWN
Definition defs.h:212
@ AV_FIELD_PROGRESSIVE
Definition defs.h:213
@ AV_FIELD_BT
Bottom coded first, top displayed first.
Definition defs.h:217
@ AV_FIELD_TB
Top coded first, bottom displayed first.
Definition defs.h:216
static AVPacket * pkt
Public dictionary API.
void ff_isom_put_dvcc_dvvc(void *logctx, uint8_t out[ISOM_DVCC_DVVC_SIZE], const AVDOVIDecoderConfigurationRecord *dovi)
Definition dovi_isom.c:89
#define ISOM_DVCC_DVVC_SIZE
Definition dovi_isom.h:29
enum AVCodecID id
Definition dts2pts.c:607
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
static int write_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt)
Definition ffmpeg_mux.c:204
const char * key
static int64_t duration
Definition ffplay.c:330
static void write_header(FFV1Context *f)
Definition ffv1enc.c:384
int ff_flac_write_header(AVIOContext *pb, const uint8_t *extradata, int extradata_size, int last_block)
int ff_flac_is_native_layout(uint64_t channel_layout)
const uint16_t ff_opus_frame_duration[32]
#define fail
Definition test.h:479
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_INT64
Underlying C type is int64_t.
Definition opt.h:262
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition utils.c:556
const AVCodecDescriptor * avcodec_descriptor_get(enum AVCodecID id)
enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
Get the type of the given codec.
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
Definition utils.c:421
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition codec_id.h:47
@ AV_CODEC_ID_PCM_S24BE
Definition codec_id.h:343
@ AV_CODEC_ID_ATRAC1
Definition codec_id.h:499
@ AV_CODEC_ID_VORBIS
Definition codec_id.h:458
@ AV_CODEC_ID_HDMV_PGS_SUBTITLE
Definition codec_id.h:572
@ AV_CODEC_ID_DVB_SUBTITLE
Definition codec_id.h:567
@ AV_CODEC_ID_RAWVIDEO
Definition codec_id.h:63
@ AV_CODEC_ID_DVD_SUBTITLE
Definition codec_id.h:566
@ AV_CODEC_ID_SVQ3
Definition codec_id.h:73
@ AV_CODEC_ID_SIPR
Definition codec_id.h:494
@ AV_CODEC_ID_TEXT
raw UTF-8 text
Definition codec_id.h:568
@ AV_CODEC_ID_H264
Definition codec_id.h:77
@ AV_CODEC_ID_NONE
Definition codec_id.h:48
@ AV_CODEC_ID_JPEG2000
Definition codec_id.h:138
@ AV_CODEC_ID_PCM_S16BE
Definition codec_id.h:331
@ AV_CODEC_ID_CINEPAK
Definition codec_id.h:93
@ AV_CODEC_ID_VVC
Definition codec_id.h:247
@ AV_CODEC_ID_RA_288
Definition codec_id.h:439
@ AV_CODEC_ID_AV1
Definition codec_id.h:275
@ AV_CODEC_ID_VP8
Definition codec_id.h:190
@ AV_CODEC_ID_ALAC
Definition codec_id.h:469
@ AV_CODEC_ID_WEBVTT
Definition codec_id.h:584
@ AV_CODEC_ID_RV10
Definition codec_id.h:55
@ AV_CODEC_ID_ARIB_CAPTION
Definition codec_id.h:591
@ AV_CODEC_ID_THEORA
Definition codec_id.h:80
@ AV_CODEC_ID_TTA
Definition codec_id.h:475
@ AV_CODEC_ID_ASS
Definition codec_id.h:588
@ AV_CODEC_ID_HEVC
Definition codec_id.h:223
@ AV_CODEC_ID_RV30
Definition codec_id.h:118
@ AV_CODEC_ID_COOK
Definition codec_id.h:473
@ AV_CODEC_ID_ADPCM_G726
Definition codec_id.h:381
@ AV_CODEC_ID_ATRAC3
Definition codec_id.h:484
@ AV_CODEC_ID_AAC
Definition codec_id.h:455
@ AV_CODEC_ID_RV20
Definition codec_id.h:56
@ AV_CODEC_ID_PRORES
Definition codec_id.h:198
@ AV_CODEC_ID_TRUEHD
Definition codec_id.h:497
@ AV_CODEC_ID_FLAC
Definition codec_id.h:465
@ AV_CODEC_ID_AC3
Definition codec_id.h:456
@ AV_CODEC_ID_QDM2
Definition codec_id.h:472
@ AV_CODEC_ID_PCM_S32BE
Definition codec_id.h:339
@ AV_CODEC_ID_SVQ1
Definition codec_id.h:72
@ AV_CODEC_ID_MLP
Definition codec_id.h:482
@ AV_CODEC_ID_RA_144
Definition codec_id.h:438
@ AV_CODEC_ID_MPEG4
Definition codec_id.h:62
@ AV_CODEC_ID_WAVPACK
Definition codec_id.h:478
@ AV_CODEC_ID_VP9
Definition codec_id.h:217
@ AV_CODEC_ID_QDMC
Definition codec_id.h:503
@ AV_CODEC_ID_OPUS
Definition codec_id.h:513
const AVPacketSideData * av_packet_side_data_get(const AVPacketSideData *sd, int nb_sd, enum AVPacketSideDataType type)
Get side information from a side data array.
Definition packet.c:570
@ AV_PKT_DATA_HEVC_CONF
Dolby Vision enhancement-layer HEVC decoder configuration.
Definition packet.h:384
@ AV_PKT_DATA_SKIP_SAMPLES
Recommends skipping the specified number of samples.
Definition packet.h:153
@ AV_PKT_DATA_DYNAMIC_HDR10_PLUS
HDR10+ dynamic metadata associated with a video frame.
Definition packet.h:296
@ AV_PKT_DATA_MASTERING_DISPLAY_METADATA
Mastering display metadata (based on SMPTE-2086:2014).
Definition packet.h:219
@ AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL
Data found in BlockAdditional element of matroska container.
Definition packet.h:188
@ AV_PKT_DATA_SPHERICAL
This side data should be associated with a video stream and corresponds to the AVSphericalMapping str...
Definition packet.h:225
@ AV_PKT_DATA_WEBVTT_SETTINGS
The optional settings (rendering instructions) that immediately follow the timestamp specifier of a W...
Definition packet.h:199
@ AV_PKT_DATA_DISPLAYMATRIX
This side data contains a 3x3 transformation matrix describing an affine transformation that needs to...
Definition packet.h:105
@ AV_PKT_DATA_DYNAMIC_HDR_SMPTE_2094_APP5
HDR dynamic metadata associated with a video frame.
Definition packet.h:376
@ AV_PKT_DATA_NEW_EXTRADATA
The AV_PKT_DATA_NEW_EXTRADATA is used to notify the codec or the format that the extradata buffer was...
Definition packet.h:56
@ AV_PKT_DATA_STEREO3D
This side data should be associated with a video stream and contains Stereoscopic 3D information in f...
Definition packet.h:111
@ AV_PKT_DATA_WEBVTT_IDENTIFIER
The optional first identifier line of a WebVTT cue.
Definition packet.h:193
@ AV_PKT_DATA_FRAME_CROPPING
The number of pixels to discard from the top/bottom/left/right border of the decoded frame to obtain ...
Definition packet.h:340
@ AV_PKT_DATA_CONTENT_LIGHT_LEVEL
Content light level (based on CTA-861.3).
Definition packet.h:232
@ AV_PKT_DATA_LCEVC
Raw LCEVC payload data, as a uint8_t array, with NAL emulation bytes intact.
Definition packet.h:346
@ AV_PKT_DATA_DOVI_CONF
DOVI configuration ref: dolby-vision-bitstreams-within-the-iso-base-media-file-format-v2....
Definition packet.h:280
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition packet.c:434
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition packet.h:650
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, size_t *size)
Get side information from packet.
Definition packet.c:252
int av_packet_ref(AVPacket *dst, const AVPacket *src)
Setup a new reference to the data described by a given packet.
Definition packet.c:442
@ AV_CHANNEL_ORDER_NATIVE
The native channel order, i.e.
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition crc.c:389
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:421
@ AV_CRC_32_IEEE_LE
Definition crc.h:53
uint32_t av_get_random_seed(void)
Get a seed to use in conjunction with random functions.
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
Definition dict.c:233
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition dict.c:60
const AVDictionaryEntry * av_dict_iterate(const AVDictionary *m, const AVDictionaryEntry *prev)
Iterate over a dictionary.
Definition dict.c:42
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition dict.c:86
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_INFO
Standard information.
Definition log.h:221
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
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
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition rational.h:104
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition rational.h:159
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Definition mem.c:217
AVMediaType
Definition avutil.h:198
@ AVMEDIA_TYPE_ATTACHMENT
Opaque data information usually sparse.
Definition avutil.h:204
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_SUBTITLE
Definition avutil.h:203
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition samplefmt.c:108
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
Definition avstring.c:208
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
#define AV_TIME_BASE
Internal time base represented as integer.
Definition avutil.h:253
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition avutil.h:263
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
@ AV_SPHERICAL_EQUIRECTANGULAR
Video represents a sphere mapped on a flat surface using equirectangular projection.
Definition spherical.h:52
@ AV_SPHERICAL_EQUIRECTANGULAR_TILE
Video represents a portion of a sphere mapped on a flat surface using equirectangular projection.
Definition spherical.h:68
@ AV_SPHERICAL_CUBEMAP
Video frame is split into 6 faces of a cube, and arranged on a 3x2 layout.
Definition spherical.h:61
#define AV_STEREO3D_FLAG_INVERT
Inverted views, Right/Bottom represents the left view.
Definition stereo3d.h:194
int av_dynamic_hdr_smpte2094_app5_to_t35(const AVDynamicHDRSmpte2094App5 *s, uint8_t **data, size_t *size)
Serialize dynamic SMPTE-2094-50 metadata to a ITU-T T.35 message.
int av_dynamic_hdr_plus_to_t35(const AVDynamicHDRPlus *s, uint8_t **data, size_t *size)
Serialize dynamic HDR10+ metadata to a user data registered ITU-T T.35 buffer, excluding the first 48...
#define AV_HDR_PLUS_MAX_PAYLOAD_SIZE
int ff_isom_write_hvcc(AVIOContext *pb, const uint8_t *data, int size, int ps_array_completeness, void *logctx)
Writes HEVC extradata (parameter sets and declarative SEI NAL units with nuh_layer_id == 0,...
Definition hevc.c:1401
#define NOTHING(PROFILE, W, H, NUM_SLICES, TAB_W, TAB_H)
cl_device_type type
static int query_codec(enum AVCodecID id, int std_compliance)
Definition img2enc.c:361
static av_always_inline uint64_t av_double2int(double f)
Reinterpret a double as a 64-bit integer.
Definition intfloat.h:70
#define av_log2
Definition intmath.h:84
static void scale(int *out, const int *in, const int w, const int h, const int shift)
Definition intra.c:278
#define AV_WB32(p, v)
#define AV_WL32(p, v)
#define AV_RL32(p)
#define AV_RB32(p)
#define AV_WB64(p, v)
#define AV_WB8(p, d)
#define AV_RB64(p)
#define AV_WB16(p, v)
#define AV_RB24(x)
#define AV_RB16(p)
const AVCodecTag ff_codec_movvideo_tags[]
Definition isom_tags.c:29
#define ITU_T_T35_PROVIDER_CODE_SMPTE
Definition itut35.h:48
#define ITU_T_T35_PROVIDER_CODE_VNOVA
Definition itut35.h:42
#define ITU_T_T35_COUNTRY_CODE_US
Definition itut35.h:32
#define ITU_T_T35_COUNTRY_CODE_UK
Definition itut35.h:31
#define ITU_T_T35_PROVIDER_CODE_SAMSUNG
Definition itut35.h:47
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition lfg.c:32
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
Definition lfg.h:53
unsigned offset
Definition libaomenc.c:763
AV1 common definitions.
#define AV1_SANE_SEQUENCE_HEADER_MAX_BITS
Definition av1.h:190
#define us(width, name, range_min, range_max, subs,...)
Definition cbs_apv.c:70
int av_get_audio_frame_duration2(AVCodecParameters *par, int frame_bytes)
Definition utils.c:823
internal header for HEVC (de)muxer utilities
unsigned int ff_codec_get_tag(const AVCodecTag *tags, enum AVCodecID id)
Definition utils.c:133
static av_always_inline FFStream * ffstream(AVStream *st)
Definition internal.h:365
int ff_alloc_extradata(AVCodecParameters *par, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0.
Definition utils.c:237
static av_always_inline FFFormatContext * ffformatcontext(AVFormatContext *s)
Definition internal.h:130
enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
Definition utils.c:143
static int check_bitstream(AVFormatContext *s, FFStream *sti, AVPacket *pkt)
Definition mux.c:1056
int ff_stream_add_bitstream_filter(AVStream *st, const char *name, const char *args)
Add a bitstream filter to a stream.
Definition mux.c:1294
Libavformat version macros.
#define LIBAVFORMAT_IDENT
Definition version.h:45
internal header for H.266/VVC (de)muxer utilities
#define av_builtin_constant_p
Definition attributes.h:193
#define av_unused
Definition attributes.h:164
#define av_const
Definition attributes.h:111
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
Stereoscopic video.
version
Definition libkvazaar.c:313
const char * desc
Definition libsvtav1.c:83
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
#define M_PI
Definition mathematics.h:67
const CodecTags ff_webm_codec_tags[]
Definition matroska.c:110
const char *const ff_matroska_video_stereo_mode[MATROSKA_VIDEO_STEREOMODE_TYPE_NB]
Definition matroska.c:132
const CodecTags ff_mkv_codec_tags[]
Definition matroska.c:27
const AVMetadataConv ff_mkv_metadata_conv[]
Definition matroska.c:126
@ MATROSKA_BLOCK_ADD_ID_TYPE_OPAQUE
Definition matroska.h:362
@ MATROSKA_BLOCK_ADD_ID_TYPE_DVCC
Definition matroska.h:364
@ MATROSKA_BLOCK_ADD_ID_TYPE_HVCE
Definition matroska.h:366
@ MATROSKA_BLOCK_ADD_ID_TYPE_DVVC
Definition matroska.h:365
@ MATROSKA_BLOCK_ADD_ID_TYPE_ITU_T_T35
Definition matroska.h:363
#define MATROSKA_ID_VIDEOPROJECTIONPOSEROLL
Definition matroska.h:166
#define MATROSKA_ID_FILEMIMETYPE
Definition matroska.h:256
#define MATROSKA_ID_DISCARDPADDING
Definition matroska.h:250
#define MATROSKA_ID_TRACKFLAGLACING
Definition matroska.h:105
#define MATROSKA_ID_TAGTARGETS
Definition matroska.h:220
#define EBML_ID_DOCTYPE
Definition matroska.h:39
#define MATROSKA_ID_TRACKAUDIO
Definition matroska.h:81
#define MATROSKA_ID_TRACKUID
Definition matroska.h:78
#define MATROSKA_ID_CUERELATIVEPOSITION
Definition matroska.h:208
#define MATROSKA_ID_CHAPSTRING
Definition matroska.h:266
#define MATROSKA_ID_TRACKFLAGHEARINGIMPAIRED
Definition matroska.h:100
#define MATROSKA_ID_TRACKFLAGTEXTDESCRIPTIONS
Definition matroska.h:102
#define MATROSKA_ID_VIDEOCOLOR_LUMINANCEMIN
Definition matroska.h:159
MatroskaVideoStereoModeType
Definition matroska.h:312
@ MATROSKA_VIDEO_STEREOMODE_TYPE_NB
Definition matroska.h:328
#define MATROSKA_ID_VIDEOSTEREOMODE
Definition matroska.h:128
#define MATROSKA_ID_CHAPTERS
Definition matroska.h:62
#define MATROSKA_ID_SIMPLETAG
Definition matroska.h:214
#define MATROSKA_ID_TIMECODESCALE
Definition matroska.h:65
#define HVCE_BLOCK_TYPE_NAME
Definition matroska.h:441
#define EBML_ID_EBMLVERSION
Definition matroska.h:35
#define MATROSKA_ID_VIDEOPIXELWIDTH
Definition matroska.h:119
#define EBML_ID_DOCTYPEVERSION
Definition matroska.h:40
#define MATROSKA_ID_ATTACHMENTS
Definition matroska.h:60
#define MATROSKA_ID_BLOCKDURATION
Definition matroska.h:247
#define EBML_ID_HEADER
Definition matroska.h:32
#define MATROSKA_ID_TITLE
Definition matroska.h:67
#define MATROSKA_ID_TRACKLANGUAGE
Definition matroska.h:96
#define MATROSKA_ID_SEEKID
Definition matroska.h:231
#define MATROSKA_ID_VIDEOCOLORMAXFALL
Definition matroska.h:147
#define MATROSKA_ID_VIDEOCOLOR_GY
Definition matroska.h:153
#define MATROSKA_ID_CHAPTERATOM
Definition matroska.h:262
#define MATROSKA_ID_VIDEOFIELDORDER
Definition matroska.h:127
#define DVCC_DVVC_BLOCK_TYPE_NAME
Definition matroska.h:440
#define MATROSKA_ID_VIDEOPROJECTIONTYPE
Definition matroska.h:162
#define MATROSKA_ID_AUDIOOUTSAMPLINGFREQ
Definition matroska.h:170
#define MATROSKA_ID_CODECDELAY
Definition matroska.h:93
#define MATROSKA_ID_VIDEOALPHAMODE
Definition matroska.h:129
#define MATROSKA_ID_TRACKNAME
Definition matroska.h:95
#define MATROSKA_ID_CHAPTERTIMESTART
Definition matroska.h:263
#define MATROSKA_ID_MUXINGAPP
Definition matroska.h:69
#define MATROSKA_ID_SEEKHEAD
Definition matroska.h:59
#define MATROSKA_ID_CUETIME
Definition matroska.h:202
#define MATROSKA_ID_FILEUID
Definition matroska.h:258
#define MATROSKA_ID_VIDEOCOLORSPACE
Definition matroska.h:131
#define MATROSKA_ID_BLKADDIDEXTRADATA
Definition matroska.h:196
#define MATROSKA_ID_VIDEOCOLOR_BY
Definition matroska.h:155
#define MATROSKA_ID_VIDEOCOLOR_RY
Definition matroska.h:151
#define MATROSKA_ID_EDITIONFLAGDEFAULT
Definition matroska.h:271
#define MATROSKA_ID_SEEKPREROLL
Definition matroska.h:94
#define MATROSKA_ID_VIDEOCOLORTRANSFERCHARACTERISTICS
Definition matroska.h:143
#define MATROSKA_ID_AUDIOCHANNELS
Definition matroska.h:173
#define MATROSKA_BLOCK_ADD_ID_ITU_T_T35
Definition matroska.h:370
#define MATROSKA_ID_VIDEOCOLOR_BX
Definition matroska.h:154
#define MATROSKA_ID_FILEDESC
Definition matroska.h:254
#define MATROSKA_ID_VIDEODISPLAYUNIT
Definition matroska.h:125
#define MATROSKA_ID_SIMPLEBLOCK
Definition matroska.h:243
#define MATROSKA_ID_VIDEOCOLORCHROMASITINGHORZ
Definition matroska.h:140
#define MATROSKA_ID_BLOCKGROUP
Definition matroska.h:238
#define MATROSKA_ID_AUDIOBITDEPTH
Definition matroska.h:172
#define STEREOMODE_STEREO3D_MAPPING(MAP, MKV_ONLY)
Definition matroska.h:402
#define MATROSKA_ID_VIDEOPIXELHEIGHT
Definition matroska.h:120
#define MATROSKA_ID_DATEUTC
Definition matroska.h:70
#define MATROSKA_ID_VIDEOPIXELCROPL
Definition matroska.h:123
#define MATROSKA_ID_CUETRACK
Definition matroska.h:206
@ MATROSKA_VIDEO_INTERLACE_FLAG_INTERLACED
Definition matroska.h:299
@ MATROSKA_VIDEO_INTERLACE_FLAG_PROGRESSIVE
Definition matroska.h:300
#define MATROSKA_ID_INFO
Definition matroska.h:55
#define MATROSKA_ID_TRACKNUMBER
Definition matroska.h:77
#define MATROSKA_ID_POINTENTRY
Definition matroska.h:199
#define MATROSKA_ID_VIDEOPROJECTIONPRIVATE
Definition matroska.h:163
#define MATROSKA_ID_TAGS
Definition matroska.h:58
#define MATROSKA_ID_VIDEOCOLORCHROMASITINGVERT
Definition matroska.h:141
#define MATROSKA_ID_TRACKFLAGORIGINAL
Definition matroska.h:103
#define MATROSKA_ID_VIDEOCOLORMASTERINGMETA
Definition matroska.h:149
@ MATROSKA_VIDEO_FIELDORDER_TB
Definition matroska.h:308
@ MATROSKA_VIDEO_FIELDORDER_BT
Definition matroska.h:309
@ MATROSKA_VIDEO_FIELDORDER_BB
Definition matroska.h:307
@ MATROSKA_VIDEO_FIELDORDER_TT
Definition matroska.h:305
#define MATROSKA_ID_VIDEOCOLOR_LUMINANCEMAX
Definition matroska.h:158
#define MATROSKA_ID_BLOCK
Definition matroska.h:246
#define MATROSKA_ID_TAGLANG
Definition matroska.h:217
#define MATROSKA_ID_TAGSTRING
Definition matroska.h:216
#define MATROSKA_ID_CLUSTER
Definition matroska.h:61
#define MATROSKA_ID_TRACKBLKADDMAPPING
Definition matroska.h:113
#define MATROSKA_ID_BLOCKREFERENCE
Definition matroska.h:248
#define MATROSKA_ID_CHAPTERTIMEEND
Definition matroska.h:264
#define MATROSKA_ID_VIDEODISPLAYHEIGHT
Definition matroska.h:118
#define MATROSKA_ID_ATTACHEDFILE
Definition matroska.h:253
#define MATROSKA_ID_TRACKTYPE
Definition matroska.h:79
#define MATROSKA_ID_TRACKENTRY
Definition matroska.h:74
#define MATROSKA_ID_SEGMENTUID
Definition matroska.h:71
#define EBML_ID_DOCTYPEREADVERSION
Definition matroska.h:41
#define MATROSKA_ID_TRACKDEFAULTDURATION
Definition matroska.h:108
@ MATROSKA_VIDEO_PROJECTION_TYPE_CUBEMAP
Definition matroska.h:356
@ MATROSKA_VIDEO_PROJECTION_TYPE_EQUIRECTANGULAR
Definition matroska.h:355
#define MATROSKA_ID_TRACKS
Definition matroska.h:56
#define MATROSKA_ID_VIDEOFLAGINTERLACED
Definition matroska.h:126
#define MATROSKA_ID_FILENAME
Definition matroska.h:255
#define MATROSKA_ID_TRACKFLAGFORCED
Definition matroska.h:99
#define MATROSKA_ID_TRACKVIDEO
Definition matroska.h:80
#define MATROSKA_ID_TAGTARGETS_CHAPTERUID
Definition matroska.h:224
#define MATROSKA_ID_VIDEOCOLORRANGE
Definition matroska.h:142
#define MATROSKA_ID_FILEDATA
Definition matroska.h:257
#define MATROSKA_ID_TRACKMAXBLKADDID
Definition matroska.h:112
#define MATROSKA_ID_VIDEOCOLOR_RX
Definition matroska.h:150
#define MATROSKA_ID_BLOCKADDITIONAL
Definition matroska.h:242
#define MATROSKA_ID_TAGTARGETS_TRACKUID
Definition matroska.h:223
#define EBML_ID_VOID
Definition matroska.h:44
#define MATROSKA_ID_CUEDURATION
Definition matroska.h:209
#define EBML_ID_EBMLMAXIDLENGTH
Definition matroska.h:37
#define MATROSKA_ID_TRACKFLAGDEFAULT
Definition matroska.h:98
#define MATROSKA_ID_SEEKENTRY
Definition matroska.h:228
#define MATROSKA_ID_VIDEOCOLOR_WHITEY
Definition matroska.h:157
#define MATROSKA_ID_VIDEOCOLORMAXCLL
Definition matroska.h:146
#define MATROSKA_ID_BLKADDIDVALUE
Definition matroska.h:193
@ MATROSKA_TRACK_TYPE_VIDEO
Definition matroska.h:280
@ MATROSKA_TRACK_TYPE_METADATA
Definition matroska.h:287
@ MATROSKA_TRACK_TYPE_SUBTITLE
Definition matroska.h:284
@ MATROSKA_TRACK_TYPE_AUDIO
Definition matroska.h:281
#define MATROSKA_ID_VIDEOCOLORPRIMARIES
Definition matroska.h:145
#define MATROSKA_ID_BLOCKADDID
Definition matroska.h:241
#define MATROSKA_ID_VIDEOCOLOR_WHITEX
Definition matroska.h:156
#define EBML_ID_EBMLMAXSIZELENGTH
Definition matroska.h:38
#define MATROSKA_ID_SEEKPOSITION
Definition matroska.h:232
#define MATROSKA_ID_CODECPRIVATE
Definition matroska.h:88
#define MATROSKA_ID_TAGNAME
Definition matroska.h:215
#define MATROSKA_ID_DURATION
Definition matroska.h:66
#define MATROSKA_ID_CHAPTERUID
Definition matroska.h:273
#define MATROSKA_ID_CODECID
Definition matroska.h:87
#define MATROSKA_ID_BLOCKMORE
Definition matroska.h:240
#define MATROSKA_ID_TRACKFLAGVISUALIMPAIRED
Definition matroska.h:101
#define EBML_ID_CRC32
Definition matroska.h:45
#define MATROSKA_ID_TRACKFLAGCOMMENTARY
Definition matroska.h:104
#define MATROSKA_ID_CUECLUSTERPOSITION
Definition matroska.h:207
#define MATROSKA_ID_CUES
Definition matroska.h:57
#define MATROSKA_ID_BLKADDIDNAME
Definition matroska.h:194
#define MATROSKA_ID_VIDEOPROJECTIONPOSEYAW
Definition matroska.h:164
#define MATROSKA_ID_BLKADDIDTYPE
Definition matroska.h:195
#define MATROSKA_ID_VIDEOPIXELCROPB
Definition matroska.h:121
#define MATROSKA_ID_CUETRACKPOSITION
Definition matroska.h:203
#define MATROSKA_ID_VIDEOPROJECTIONPOSEPITCH
Definition matroska.h:165
#define MATROSKA_ID_EDITIONENTRY
Definition matroska.h:261
#define MATROSKA_ID_CLUSTERTIMECODE
Definition matroska.h:235
#define MATROSKA_ID_VIDEOCOLOR
Definition matroska.h:132
#define MATROSKA_ID_VIDEODISPLAYWIDTH
Definition matroska.h:117
#define MATROSKA_ID_CHAPTERDISPLAY
Definition matroska.h:265
#define MATROSKA_ID_VIDEOCOLORMATRIXCOEFF
Definition matroska.h:134
#define MATROSKA_ID_TAG
Definition matroska.h:213
#define MATROSKA_ID_CHAPLANG
Definition matroska.h:267
#define MATROSKA_ID_VIDEOPROJECTION
Definition matroska.h:161
#define MATROSKA_ID_AUDIOSAMPLINGFREQ
Definition matroska.h:169
#define MATROSKA_ID_VIDEOPIXELCROPT
Definition matroska.h:122
#define EBML_ID_EBMLREADVERSION
Definition matroska.h:36
@ MATROSKA_VIDEO_DISPLAYUNIT_DAR
Definition matroska.h:335
@ MATROSKA_VIDEO_DISPLAYUNIT_UNKNOWN
Definition matroska.h:336
#define MATROSKA_ID_BLOCKADDITIONS
Definition matroska.h:239
#define MATROSKA_ID_WRITINGAPP
Definition matroska.h:68
#define MATROSKA_ID_TAGTARGETS_ATTACHUID
Definition matroska.h:225
#define MATROSKA_ID_SEGMENT
Definition matroska.h:52
#define MATROSKA_ID_VIDEOCOLOR_GX
Definition matroska.h:152
#define MATROSKA_ID_VIDEOPIXELCROPR
Definition matroska.h:124
EbmlType
Definition matroskadec.c:89
@ EBML_STR
Definition matroskadec.c:94
@ EBML_UTF8
Definition matroskadec.c:95
@ EBML_FLOAT
Definition matroskadec.c:93
@ EBML_UINT
Definition matroskadec.c:91
@ EBML_BIN
Definition matroskadec.c:96
@ EBML_SINT
Definition matroskadec.c:92
static int mkv_assemble_cues(AVStream **streams, AVIOContext *dyn_cp, AVIOContext *cuepoint, const mkv_cues *cues, mkv_track *tracks, int num_tracks, uint64_t offset)
static void mkv_handle_rotation(void *logctx, const AVCodecParameters *par, double *yaw, double *roll)
static int ebml_num_size(uint64_t num)
Returns how many bytes are needed to represent a number as EBML variable length integer.
static int ebml_writer_uint_len(EbmlElement *elem)
static void ebml_writer_add_sint(EbmlWriter *writer, uint32_t id, int64_t val)
static int mkv_assemble_codecprivate(AVFormatContext *s, AVIOContext *dyn_cp, AVCodecParameters *par, const uint8_t *extradata, int extradata_size, int native_id, int qt_id, uint8_t **codecpriv, int *codecpriv_size, unsigned *max_payload_size)
static int mkv_write_seekhead(AVIOContext *pb, MatroskaMuxContext *mkv, int error_on_seek_failure, int64_t destpos)
Write the SeekHead to the file at the location reserved for it and seek to destpos afterwards.
static void mkv_write_video_color(EbmlWriter *writer, const AVStream *st, const AVCodecParameters *par)
#define WDIV1(STEREOMODETYPE, STEREO3DTYPE, FLAGS, WDIV, HDIV, WEBM)
static void ebml_write_header(AVIOContext *pb, const char *doctype, int version)
static int mkv_write_simpletag(AVIOContext *pb, const AVDictionaryEntry *t)
static int mkv_reformat_av1(MatroskaMuxContext *mkv, AVIOContext *pb, const AVPacket *pkt, int *size)
static int ebml_writer_block_len(EbmlElement *elem)
EbmlType
@ EBML_BLOCK
pseudo-type for writing (Simple)Blocks
@ EBML_MASTER
@ EBML_UID
static void mkv_write_default_duration(mkv_track *track, AVIOContext *pb, AVRational duration)
static int mkv_write_packet_internal(AVFormatContext *s, const AVPacket *pkt)
static void ebml_writer_add_string(EbmlWriter *writer, uint32_t id, const char *str)
#define WEBM1(STEREOMODETYPE, STEREO3DTYPE, FLAGS, WDIV, HDIV, WEBM)
static void put_ebml_uint(AVIOContext *pb, uint32_t elementid, uint64_t val)
#define OPUS_SEEK_PREROLL
Seek preroll value for opus.
#define SMPTE_2094_APP5_MAX_SIZE
static int mkv_write_trailer(AVFormatContext *s)
static int ebml_writer_str_len(EbmlElement *elem)
static void ebml_writer_add_float(EbmlWriter *writer, uint32_t id, double val)
#define MAX_FIELD_ORDER_ELEMS
static void mkv_write_field_order(EbmlWriter *writer, int is_webm, enum AVFieldOrder field_order)
static int end_ebml_master_crc32_tentatively(AVIOContext *pb, ebml_stored_master *elem, MatroskaMuxContext *mkv, uint32_t id)
Output EBML master.
#define MODE_MATROSKAv2
Definition matroskaenc.c:85
static int mkv_write_packet(AVFormatContext *s, const AVPacket *pkt)
static int mkv_update_codecprivate(AVFormatContext *s, MatroskaMuxContext *mkv, uint8_t *side_data, int side_data_size, AVCodecParameters *par, AVIOContext *pb, mkv_track *track, unsigned alternative_size)
static int webm_reformat_vtt(MatroskaMuxContext *mkv, AVIOContext *pb, const AVPacket *pkt, int *size)
static void mkv_write_tag_targets(MatroskaMuxContext *mkv, AVIOContext *pb, uint32_t elementid, uint64_t uid)
#define MAX_VIDEO_PROJECTION_ELEMS
static int mkv_write_tags(AVFormatContext *s)
static const char * get_mimetype(const AVStream *st)
static void put_ebml_length(AVIOContext *pb, uint64_t length, int bytes)
Write a length as EBML variable length integer.
static int mkv_write_chapters(AVFormatContext *s)
static void put_ebml_id(AVIOContext *pb, uint32_t id)
static const AVCodecTag additional_subtitle_tags[]
static int mkv_write_block(void *logctx, MatroskaMuxContext *mkv, AVIOContext *pb, const AVStream *st, mkv_track *track, const AVPacket *pkt, int keyframe, int64_t ts, uint64_t duration, int force_blockgroup, int64_t relative_packet_pos)
static int mkv_write_info(AVFormatContext *s)
static int ebml_writer_elem_len(EbmlWriter *writer, EbmlElement *elem, int remaining_elems)
static int mkv_write_flush_packet(AVFormatContext *s, AVPacket *pkt)
static void ebml_writer_add_uint(EbmlWriter *writer, uint32_t id, uint64_t val)
static int ebml_id_size(uint32_t id)
#define HDIV1(STEREOMODETYPE, STEREO3DTYPE, FLAGS, WDIV, HDIV, WEBM)
static int ebml_writer_sint_len(EbmlElement *elem)
static int put_xiph_codecpriv(AVFormatContext *s, AVIOContext *pb, const AVCodecParameters *par, const uint8_t *extradata, int extradata_size)
#define MAX_SEEKHEAD_ENTRIES
Definition matroskaenc.c:77
static int mkv_new_chapter_ids_needed(const AVFormatContext *s)
@ DEFAULT_MODE_INFER
Definition matroskaenc.c:94
@ DEFAULT_MODE_INFER_NO_SUBS
Definition matroskaenc.c:95
@ DEFAULT_MODE_PASSTHROUGH
Definition matroskaenc.c:96
static void put_ebml_uid(AVIOContext *pb, uint32_t elementid, uint64_t uid)
Write a (random) UID with fixed size to make the output more deterministic.
static int mkv_write_header(AVFormatContext *s)
static void mkv_put_codecprivate(AVIOContext *pb, unsigned max_payload_size, const uint8_t *codecpriv, unsigned codecpriv_size)
static void ebml_writer_add_bin(EbmlWriter *writer, uint32_t id, const uint8_t *data, size_t size)
static int ebml_writer_master_len(EbmlWriter *writer, EbmlElement *elem, int remaining_elems)
static int mkv_init(struct AVFormatContext *s)
static const AVClass matroska_webm_class
#define DURATION_SIMPLETAG_SIZE
2 + 1 Simpletag header, 2 + 1 + 8 Name "DURATION", rest for TagString
#define MAX_SUPPORTED_EBML_LENGTH
Definition matroskaenc.c:83
#define IS_SEEKABLE(pb, mkv)
Definition matroskaenc.c:90
static int ebml_writer_elem_write(const EbmlElement *elem, AVIOContext *pb)
static uint64_t mkv_get_uid(const mkv_track *tracks, int i, AVLFG *c)
static void ebml_writer_open_master(EbmlWriter *writer, uint32_t id)
static int parse_opus_packet_duration(const uint8_t *buf, int buf_size)
Returns the duration of an Opus packet in samples.
static bool codec_has_blockadditional_alpha(AVFormatContext *s, const AVStream *st, const AVCodecParameters *par)
#define MODE_WEBM
Definition matroskaenc.c:86
static void ebml_writer_add_uid(EbmlWriter *writer, uint32_t id, uint64_t val)
#define EBML_WRITER(max_nb_elems)
static int64_t get_metadata_duration(AVFormatContext *s)
static int mkv_handle_spherical(void *logctx, EbmlWriter *writer, const AVCodecParameters *par, uint8_t private[], double *yaw, double *pitch, double *roll)
static int mkv_assemble_native_codecprivate(AVFormatContext *s, AVIOContext *dyn_cp, const AVCodecParameters *par, const uint8_t *extradata, int extradata_size, unsigned *size_to_reserve)
static int start_ebml_master_crc32(AVIOContext **dyn_cp, MatroskaMuxContext *mkv)
static const AVCodecTag additional_audio_tags[]
static int mkv_write_track_video(AVFormatContext *s, MatroskaMuxContext *mkv, const AVStream *st, const AVCodecParameters *par, AVIOContext *pb)
static void put_ebml_binary(AVIOContext *pb, uint32_t elementid, const void *buf, int 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...
static int mkv_write_tag(MatroskaMuxContext *mkv, const AVDictionary *m, AVIOContext **pb, unsigned reserved_size, uint32_t elementid, uint64_t uid)
#define IS_WEBM(mkv)
Definition matroskaenc.c:88
#define WDIV2(STEREOMODETYPE, WDIV, HDIV, WEBM)
static int mkv_check_bitstream(AVFormatContext *s, AVStream *st, const AVPacket *pkt)
static av_const int uint_size(uint64_t val)
static int mkv_write_track(AVFormatContext *s, MatroskaMuxContext *mkv, AVStream *st, mkv_track *track, AVIOContext *pb, int is_default)
static void mkv_start_seekhead(MatroskaMuxContext *mkv, AVIOContext *pb)
Initialize the SeekHead element to be ready to index level 1 Matroska elements.
static int mkv_end_cluster(AVFormatContext *s)
static void ebml_writer_write_block(const EbmlElement *elem, AVIOContext *pb)
static EbmlElement * ebml_writer_add(EbmlWriter *writer, uint32_t id, EbmlType type)
#define MAX_CUETRACKPOS_SIZE
4 * (1-byte EBML ID, 1-byte EBML size, 8-byte uint max)
static int mkv_add_cuepoint(MatroskaMuxContext *mkv, int stream, int64_t ts, int64_t cluster_pos, int64_t relative_pos, int64_t duration)
static void put_ebml_void(AVIOContext *pb, int size)
Write a void element of a given size.
static void mkv_add_seekhead_entry(MatroskaMuxContext *mkv, uint32_t elementid, uint64_t filepos)
static int mkv_check_tag_name(const char *name, uint32_t elementid)
static void mkv_write_video_projection(void *logctx, EbmlWriter *wr, const AVCodecParameters *par, uint8_t private[])
static void mkv_deinit(AVFormatContext *s)
Free the members allocated in the mux context.
#define STEREOMODE(STEREOMODETYPE, STEREO3DTYPE, FLAGS, WDIV, HDIV, WEBM)
#define HDIV2(STEREOMODETYPE, WDIV, HDIV, WEBM)
static void mkv_write_blockadditional(EbmlWriter *writer, const uint8_t *buf, size_t size, uint64_t additional_id)
static int ebml_writer_write(EbmlWriter *writer, AVIOContext *pb)
static void put_xiph_size(AVIOContext *pb, int size)
static void mkv_write_blockadditionmapping(AVFormatContext *s, const MatroskaMuxContext *mkv, const AVCodecParameters *par, AVIOContext *pb, mkv_track *track, const AVStream *st)
#define CASE(type, variable)
#define MAX_STEREO_MODE_ELEMS
#define OFFSET(x)
static void put_ebml_string(AVIOContext *pb, uint32_t elementid, const char *str)
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)
static void ebml_writer_add_block(EbmlWriter *writer, MatroskaMuxContext *mkv)
#define WEBM2(STEREOMODETYPE, WDIV, HDIV, WEBM)
static int mkv_write_attachments(AVFormatContext *s)
static av_const int sint_size(int64_t val)
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...
static void ebml_writer_close_or_discard_master(EbmlWriter *writer)
static void put_ebml_num(AVIOContext *pb, uint64_t num, int bytes)
Write a number as EBML variable length integer on bytes bytes.
static ebml_master start_ebml_master(AVIOContext *pb, uint32_t elementid, uint64_t expectedsize)
#define DURATION_STRING_LENGTH
DURATION_STRING_LENGTH must be <= 112 or the containing simpletag will need more than one byte for it...
static int mkv_write_tracks(AVFormatContext *s)
static void put_ebml_float(AVIOContext *pb, uint32_t elementid, double val)
static int mkv_write_stereo_mode(AVFormatContext *s, EbmlWriter *writer, const AVCodecParameters *par, const AVStream *st, int is_webm, int *h_width, int *h_height)
static void ebml_writer_close_master(EbmlWriter *writer)
#define MAX_VIDEO_COLOR_ELEMS
static void put_ebml_size_unknown(AVIOContext *pb, int bytes)
Write an EBML size meaning "unknown size".
static void end_ebml_master(AVIOContext *pb, ebml_master master)
static int mkv_check_new_extra_data(AVFormatContext *s, const AVPacket *pkt)
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:264
Memory handling functions.
void ff_metadata_conv(AVDictionary **pm, const AVMetadataConv *d_conv, const AVMetadataConv *s_conv)
Definition metadata.c:26
void ff_metadata_conv_ctx(AVFormatContext *ctx, const AVMetadataConv *d_conv, const AVMetadataConv *s_conv)
Definition metadata.c:59
uint32_t tag
Definition movenc.c:2073
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:212
#define MAX_PCE_SIZE
Maximum size of a PCE including the 3-bit ID_PCE marker and the comment.
Definition mpeg4audio.h:119
#define FF_OFMT_FLAG_ALLOW_FLUSH
This flag indicates that the muxer stores data internally and supports flushing it.
Definition mux.h:38
int ff_format_shift_data(AVFormatContext *s, int64_t read_start, int shift_size)
Make shift_size amount of space at read_start by shifting data in the output at read_start until the ...
Definition mux_utils.c:71
int ff_parse_creation_time_metadata(AVFormatContext *s, int64_t *timestamp, int return_seconds)
Parse creation_time in AVFormatContext metadata if exists and warn if the parsing fails.
Definition mux_utils.c:137
const char data[16]
Definition mxf.c:149
UID uid
Definition mxfenc.c:2488
void ff_nal_units_write_list(const NALUList *list, AVIOContext *pb, const uint8_t *buf)
Definition nal.c:124
int ff_nal_units_create_list(NALUList *list, const uint8_t *buf, int size)
Definition nal.c:118
IDirect3DDxgiInterfaceAccess _COM_Outptr_ void ** p
#define av_strdup(s)
Definition ops_static.c:55
#define av_malloc(s)
Definition ops_static.c:52
AVOptions.
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:592
misc parsing utilities
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3460
int av_chroma_location_enum_to_pos(int *xpos, int *ypos, enum AVChromaLocation pos)
Converts AVChromaLocation to swscale x/y chroma position.
Definition pixdesc.c:3902
#define AV_PIX_FMT_FLAG_ALPHA
The pixel format has an alpha channel.
Definition pixdesc.h:147
@ AVCHROMA_LOC_TOP
Definition pixfmt.h:807
@ AVCHROMA_LOC_UNSPECIFIED
Definition pixfmt.h:803
@ AVCOL_RANGE_UNSPECIFIED
Definition pixfmt.h:749
@ AVCOL_RANGE_NB
Not part of ABI.
Definition pixfmt.h:784
@ AVCOL_PRI_NB
Not part of ABI.
Definition pixfmt.h:660
@ AVCOL_PRI_UNSPECIFIED
Definition pixfmt.h:645
@ AVCOL_TRC_UNSPECIFIED
Definition pixfmt.h:675
@ AVCOL_TRC_NB
Not part of ABI.
Definition pixfmt.h:694
@ AVCOL_SPC_NB
Not part of ABI.
Definition pixfmt.h:726
@ AVCOL_SPC_UNSPECIFIED
Definition pixfmt.h:709
const char * name
Definition qsvenc.c:142
Utilities for rational number calculation.
const AVCodecTag ff_codec_bmp_tags[]
Definition riff.c:36
const AVCodecTag ff_codec_wav_tags[]
Definition riff.c:525
internal header for RIFF based (de)muxers do NOT include this in end user applications
#define FF_PUT_WAV_HEADER_FORCE_WAVEFORMATEX
Tell ff_put_wav_header() to use WAVEFORMATEX even for PCM codecs.
Definition riff.h:53
void ff_put_bmp_header(AVIOContext *pb, AVCodecParameters *par, int for_asf, int ignore_extradata, int rgb_frame_is_flipped)
Definition riffenc.c:236
int ff_put_wav_header(AVFormatContext *s, AVIOContext *pb, AVCodecParameters *par, int flags)
Write WAVEFORMAT header structure.
Definition riffenc.c:54
enum AVMediaType codec_type
Definition rtp.c:37
static const uint8_t header[24]
Definition sdr2.c:68
#define FF_ARRAY_ELEMS(a)
#define snprintf
Definition snprintf.h:34
const uint8_t * code
Definition spdifenc.c:433
unsigned int pos
Definition spdifenc.c:431
enum AVChannelOrder order
Channel order used in this layout.
uint64_t mask
This member must be used for AV_CHANNEL_ORDER_NATIVE, and may be used for AV_CHANNEL_ORDER_AMBISONIC ...
union AVChannelLayout::@162063043056170047076125117143030261346263330336 u
Details about which channels are present in this layout.
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
Definition log.h:76
This struct describes the properties of a single codec described by an AVCodecID.
Definition codec_desc.h:38
This struct describes the properties of an encoded stream.
Definition codec_par.h:49
enum AVColorSpace color_space
Definition codec_par.h:192
int extradata_size
Size of the extradata content in bytes.
Definition codec_par.h:75
int frame_size
Audio frame size, if known.
Definition codec_par.h:227
enum AVFieldOrder field_order
The order of the fields in interlaced video.
Definition codec_par.h:182
int height
The height of the video frame in pixels.
Definition codec_par.h:150
int nb_coded_side_data
Amount of entries in coded_side_data.
Definition codec_par.h:88
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition codec_par.h:113
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
int width
The width of the video frame in pixels.
Definition codec_par.h:143
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
int profile
Codec-specific bitstream restrictions that the stream conforms to.
Definition codec_par.h:135
int bits_per_raw_sample
The number of valid bits in each output sample.
Definition codec_par.h:130
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition codec_par.h:61
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition codec_par.h:71
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
enum AVColorPrimaries color_primaries
Definition codec_par.h:190
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
enum AVColorTransferCharacteristic color_trc
Definition codec_par.h:191
int initial_padding
Number of padding audio samples at the start.
Definition codec_par.h:239
AVPacketSideData * coded_side_data
Additional data associated with the entire stream.
Definition codec_par.h:83
enum AVChromaLocation chroma_location
Definition codec_par.h:193
enum AVColorRange color_range
Additional colorspace characteristics.
Definition codec_par.h:189
Content light level needed by to transmit HDR over HDMI (CTA-861.3).
char * key
Definition dict.h:91
char * value
Definition dict.h:92
This struct represents dynamic metadata for color volume transform - application 4 of SMPTE 2094-40:2...
This struct represents dynamic metadata for color volume transform as specified in the SMPTE 2094-50 ...
Format I/O context.
Definition avformat.h:1333
Bytestream IO Context.
Definition avio.h:160
int direct
avio_read and avio_write should if possible be satisfied directly instead of going through a buffer,...
Definition avio.h:268
int error
contains the error code or 0 if no error happened
Definition avio.h:239
Context structure for the Lagged Fibonacci PRNG.
Definition lfg.h:33
Mastering display metadata capable of representing the color volume of the display used to master the...
AVOption.
Definition opt.h:428
This structure stores auxiliary information for decoding, presenting, or otherwise processing the cod...
Definition packet.h:424
uint8_t * data
Definition packet.h:425
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition pixdesc.h:69
Rational number (pair of numerator and denominator).
Definition rational.h:58
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
This structure describes how to handle spherical videos, outlining information about projection,...
Definition spherical.h:100
uint32_t bound_left
Distance from the left edge.
Definition spherical.h:185
enum AVSphericalProjection projection
Projection type.
Definition spherical.h:104
uint32_t bound_top
Distance from the top edge.
Definition spherical.h:186
uint32_t bound_bottom
Distance from the bottom edge.
Definition spherical.h:188
uint32_t bound_right
Distance from the right edge.
Definition spherical.h:187
int32_t pitch
Rotation around the right vector [-90, 90].
Definition spherical.h:145
int32_t roll
Rotation around the forward vector [-180, 180].
Definition spherical.h:146
int32_t yaw
Rotation around the up vector [-180, 180].
Definition spherical.h:144
uint32_t padding
Number of pixels to pad from the edge of each cube face.
Definition spherical.h:200
Stereo 3D type: this structure describes how two videos are packed within a single video surface,...
Definition stereo3d.h:203
enum AVStereo3DType type
How views are packed within the video.
Definition stereo3d.h:207
int flags
Additional information about the frame packing.
Definition stereo3d.h:212
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition avformat.h:844
AVDictionary * metadata
Definition avformat.h:846
AVRational avg_frame_rate
Average framerate.
Definition avformat.h:855
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition avformat.h:805
AVRational r_frame_rate
Real base framerate of the stream.
Definition avformat.h:900
int disposition
Stream disposition - a combination of AV_DISPOSITION_* flags.
Definition avformat.h:835
int16_t rel_ts
uint8_t flags
const AVPacket * pkt
struct mkv_track * track
NALUList h2645_nalu_list
uint64_t size
excluding id and length field
uint32_t id
unsigned length_size
const uint8_t * bin
int64_t sint
uint64_t uint
union EbmlElement::@322146226050105363311012227307205347315121024315 priv
struct MatroskaMuxContext * mkv
used by EBML_BLOCK
const char * str
EbmlType type
EbmlMaster master
int nb_elements
-1 if not finished
int containing_master
-1 if no parent exists
int current_master_element
EbmlElement * elements
unsigned nb_elements
int avoid_negative_ts_use_pts
Definition internal.h:113
AVPacket * pkt
Used to hold temporary packets for the generic demuxing code.
Definition internal.h:111
int64_t lowest_ts_allowed
This is the lowest ts allowed in this track; it may be set by the muxer during init or write_header a...
Definition internal.h:257
AVPacket * cur_audio_pkt
ebml_stored_master info
int64_t cluster_pos
file offset of the current Cluster
ebml_stored_master tags
mkv_seekhead seekhead
AVIOContext * tmp_bc
int64_t cluster_time_limit
AVFormatContext * ctx
ebml_stored_master track
uint32_t segment_uid[4]
mkv_track * tracks
unsigned nb_attachments
AVIOContext * cluster_bc
BlockContext cur_block
Definition evc.c:73
NALU * nalus
Definition evc.c:74
Definition wv.h:34
int64_t pos
absolute offset in the containing AVIOContext where the master's elements start
int sizebytes
how many bytes were reserved for the size
AVIOContext * bc
int64_t cluster_pos
offset of the cluster containing the block relative to the segment
uint64_t pts
int64_t duration
duration of the block according to time base
int64_t relative_pos
relative offset from the position of the cluster containing the block
mkv_cuepoint * entries
int num_entries
uint64_t segmentpos
uint32_t elementid
int64_t filepos
mkv_seekhead_entry entries[MAX_SEEKHEAD_ENTRIES]
int track_num_size
uint64_t default_duration_low
int64_t last_timestamp
int codecpriv_offset
uint64_t max_blockaddid
int64_t blockadditionmapping_offset
int64_t ts_offset
int sample_rate
unsigned codecpriv_size
size reserved for CodecPrivate excluding header+length field
int64_t duration
int64_t sample_rate_offset
uint64_t default_duration_high
int(* reformat)(struct MatroskaMuxContext *, AVIOContext *, const AVPacket *, int *size)
unsigned offset
int64_t duration_offset
unsigned track_num
uint64_t uid
#define av_free(p)
#define av_freep(p)
#define av_log(a,...)
static uint8_t tmp[40]
Definition aes_ctr.c:52
#define src
Definition vp8dsp.c:248
static int64_t pts
int size
static int write_trailer(AVFormatContext *s1)
Definition v4l2enc.c:101
enum AVCodecID codec_id
const char * master
Definition vf_curves.c:130
int len
int ff_vorbiscomment_write(AVIOContext *pb, const AVDictionary *dict, const char *vendor_string, AVChapter **chapters, unsigned int nb_chapters)
Write a VorbisComment into an AVIOContext.
int ff_vorbiscomment_length(const AVDictionary *m, const char *vendor_string, AVChapter **chapters, unsigned int nb_chapters)
Calculate the length in bytes of a VorbisComment.
static double c[64]
int ff_isom_write_vvcc(AVIOContext *pb, const uint8_t *data, int size, int ps_array_completeness)
Writes H.266/VVC extradata (parameter sets, declarative SEI NAL units) to the provided AVIOContext.
Definition vvc.c:879
#define WV_HEADER_SIZE
Definition wavpack.h:33
int ff_wv_parse_header(WvHeader *wv, const uint8_t *data)
Parse a WavPack block header.
Definition wv.c:30
int avpriv_split_xiph_headers(const uint8_t *extradata, int extradata_size, int first_header_size, const uint8_t *header_start[3], int header_len[3])
Split a single extradata buffer into the three headers that most Xiph codecs use.
Definition xiph.c:26