3 * Copyright (c) 2007 David Conrad
5 * This file is part of FFmpeg.
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.
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.
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
24 #include "config_components.h"
30 #include "avio_internal.h"
31 #include "avlanguage.h"
32 #include "dovi_isom.h"
41 #include "vorbiscomment.h"
45 #include "libavutil/avstring.h"
46 #include "libavutil/channel_layout.h"
47 #include "libavutil/crc.h"
48 #include "libavutil/dict.h"
49 #include "libavutil/hdr_dynamic_metadata.h"
50 #include "libavutil/intfloat.h"
51 #include "libavutil/intreadwrite.h"
52 #include "libavutil/lfg.h"
53 #include "libavutil/mastering_display_metadata.h"
54 #include "libavutil/mathematics.h"
55 #include "libavutil/mem.h"
56 #include "libavutil/opt.h"
57 #include "libavutil/parseutils.h"
58 #include "libavutil/pixdesc.h"
59 #include "libavutil/random_seed.h"
60 #include "libavutil/rational.h"
61 #include "libavutil/samplefmt.h"
62 #include "libavutil/stereo3d.h"
64 #include "libavcodec/av1.h"
65 #include "libavcodec/bytestream.h"
66 #include "libavcodec/codec_desc.h"
67 #include "libavcodec/codec_par.h"
68 #include "libavcodec/defs.h"
69 #include "libavcodec/itut35.h"
70 #include "libavcodec/xiph.h"
71 #include "libavcodec/mpeg4audio.h"
73 /* Level 1 elements we create a SeekHead entry for:
74 * Info, Tracks, Chapters, Attachments, Tags (potentially twice) and Cues */
75 #define MAX_SEEKHEAD_ENTRIES 7
77 /* Largest known-length EBML length */
78 #define MAX_EBML_LENGTH ((1ULL << 56) - 2)
79 /* The dynamic buffer API we rely upon has a limit of INT_MAX;
80 * and so has avio_write(). */
81 #define MAX_SUPPORTED_EBML_LENGTH FFMIN(MAX_EBML_LENGTH, INT_MAX)
83 #define MODE_MATROSKAv2 0x01
84 #define MODE_WEBM 0x02
86 #define IS_WEBM(mkv) (CONFIG_WEBM_MUXER && CONFIG_MATROSKA_MUXER ? \
87 ((mkv)->mode == MODE_WEBM) : CONFIG_WEBM_MUXER)
88 #define IS_SEEKABLE(pb, mkv) (((pb)->seekable & AVIO_SEEKABLE_NORMAL) && \
93 DEFAULT_MODE_INFER_NO_SUBS
,
94 DEFAULT_MODE_PASSTHROUGH
,
97 typedef struct ebml_master
{
98 int64_t pos
; ///< absolute offset in the containing AVIOContext where the master's elements start
99 int sizebytes
; ///< how many bytes were reserved for the size
102 typedef struct ebml_stored_master
{
105 } ebml_stored_master
;
107 typedef enum EbmlType
{
113 EBML_UTF8
= EBML_STR
,
115 EBML_BLOCK
, ///< pseudo-type for writing (Simple)Blocks
119 typedef struct BlockContext
{
120 struct mkv_track
*track
;
124 NALUList h2645_nalu_list
;
127 typedef struct EbmlMaster
{
128 int nb_elements
; ///< -1 if not finished
129 int containing_master
; ///< -1 if no parent exists
132 typedef struct EbmlElement
{
135 unsigned length_size
;
136 uint64_t size
; ///< excluding id and length field
143 struct MatroskaMuxContext
*mkv
; ///< used by EBML_BLOCK
148 typedef struct EbmlWriter
{
149 unsigned nb_elements
;
150 int current_master_element
;
151 EbmlElement
*elements
;
154 #define EBML_WRITER(max_nb_elems) \
155 EbmlElement elements[max_nb_elems]; \
156 EbmlWriter writer = (EbmlWriter){ .elements = elements, \
157 .current_master_element = -1 }
159 typedef struct mkv_seekhead_entry
{
162 } mkv_seekhead_entry
;
164 typedef struct mkv_seekhead
{
166 mkv_seekhead_entry entries
[MAX_SEEKHEAD_ENTRIES
];
171 typedef struct mkv_cuepoint
{
174 int64_t cluster_pos
; ///< offset of the cluster containing the block relative to the segment
175 int64_t relative_pos
; ///< relative offset from the position of the cluster containing the block
176 int64_t duration
; ///< duration of the block according to time base
179 typedef struct mkv_cues
{
180 mkv_cuepoint
*entries
;
184 struct MatroskaMuxContext
;
186 typedef struct mkv_track
{
194 int64_t sample_rate_offset
;
195 int64_t last_timestamp
;
197 int64_t duration_offset
;
198 uint64_t max_blockaddid
;
199 int64_t blockadditionmapping_offset
;
200 int codecpriv_offset
;
201 unsigned codecpriv_size
; ///< size reserved for CodecPrivate excluding header+length field
203 uint64_t default_duration_low
;
204 uint64_t default_duration_high
;
205 /* This callback will be called twice: First with a NULL AVIOContext
206 * to return the size of the (Simple)Block's data via size
207 * and a second time with the AVIOContext set when the data
209 * The callback shall not return an error on the second call. */
210 int (*reformat
)(struct MatroskaMuxContext
*, AVIOContext
*,
211 const AVPacket
*, int *size
);
214 typedef struct MatroskaMuxContext
{
215 const AVClass
*class;
216 AVFormatContext
*ctx
;
219 ebml_stored_master info
;
220 ebml_stored_master track
;
221 ebml_stored_master tags
;
222 int64_t segment_offset
;
223 AVIOContext
*cluster_bc
;
224 int64_t cluster_pos
; ///< file offset of the current Cluster
226 int64_t duration_offset
;
229 mkv_seekhead seekhead
;
233 BlockContext cur_block
;
235 /* Used as temporary buffer to use the minimal amount of bytes
236 * to write the length field of EBML Masters.
237 * Every user has to reset the buffer after using it and
238 * different uses may not overlap. It is currently used in
239 * mkv_write_tag(), in mkv_assemble_cues() as well as in
240 * mkv_update_codecprivate() and mkv_write_track(). */
243 AVPacket
*cur_audio_pkt
;
245 unsigned nb_attachments
;
251 int reserve_cues_space
;
252 int cluster_size_limit
;
253 int64_t cluster_time_limit
;
258 int dash_track_number
;
262 int move_cues_to_front
;
264 uint32_t segment_uid
[4];
265 } MatroskaMuxContext
;
267 /** 2 bytes * 3 for EBML IDs, 3 1-byte EBML lengths, 8 bytes for 64 bit
268 * offset, 4 bytes for target EBML ID */
269 #define MAX_SEEKENTRY_SIZE 21
271 /** 4 * (1-byte EBML ID, 1-byte EBML size, 8-byte uint max) */
272 #define MAX_CUETRACKPOS_SIZE 40
274 /** DURATION_STRING_LENGTH must be <= 112 or the containing
275 * simpletag will need more than one byte for its length field. */
276 #define DURATION_STRING_LENGTH 19
278 /** 2 + 1 Simpletag header, 2 + 1 + 8 Name "DURATION", rest for TagString */
279 #define DURATION_SIMPLETAG_SIZE (2 + 1 + (2 + 1 + 8) + (2 + 1 + DURATION_STRING_LENGTH))
281 /** Seek preroll value for opus */
282 #define OPUS_SEEK_PREROLL 80000000
284 static int ebml_id_size(uint32_t id
)
286 return (av_log2(id
) + 7U) / 8;
289 static void put_ebml_id(AVIOContext
*pb
, uint32_t id
)
291 int i
= ebml_id_size(id
);
293 avio_w8(pb
, (uint8_t)(id
>> (i
* 8)));
297 * Write an EBML size meaning "unknown size".
299 * @param bytes The number of bytes the size should occupy (maximum: 8).
301 static void put_ebml_size_unknown(AVIOContext
*pb
, int bytes
)
303 av_assert0(bytes
<= 8);
304 avio_w8(pb
, 0x1ff >> bytes
);
305 if (av_builtin_constant_p(bytes
) && bytes
== 1)
307 ffio_fill(pb
, 0xff, bytes
- 1);
311 * Returns how many bytes are needed to represent a number
312 * as EBML variable length integer.
314 static int ebml_num_size(uint64_t num
)
324 * Calculate how many bytes are needed to represent the length field
325 * of an EBML element whose payload has a given length.
327 static int ebml_length_size(uint64_t length
)
329 return ebml_num_size(length
+ 1);
333 * Write a number as EBML variable length integer on `bytes` bytes.
334 * `bytes` is taken literally without checking.
336 static void put_ebml_num(AVIOContext
*pb
, uint64_t num
, int bytes
)
338 num
|= 1ULL << bytes
* 7;
339 for (int i
= bytes
- 1; i
>= 0; i
--)
340 avio_w8(pb
, (uint8_t)(num
>> i
* 8));
344 * Write a length as EBML variable length integer.
346 * @param bytes The number of bytes that need to be used to write the number.
347 * If zero, the minimal number of bytes will be used.
349 static void put_ebml_length(AVIOContext
*pb
, uint64_t length
, int bytes
)
351 int needed_bytes
= ebml_length_size(length
);
353 // sizes larger than this are currently undefined in EBML
354 av_assert0(length
< (1ULL << 56) - 1);
357 bytes
= needed_bytes
;
358 // The bytes needed to write the given size must not exceed
359 // the bytes that we ought to use.
360 av_assert0(bytes
>= needed_bytes
);
361 put_ebml_num(pb
, length
, bytes
);
365 * Write a (random) UID with fixed size to make the output more deterministic
367 static void put_ebml_uid(AVIOContext
*pb
, uint32_t elementid
, uint64_t uid
)
369 put_ebml_id(pb
, elementid
);
370 put_ebml_length(pb
, 8, 0);
374 static void put_ebml_uint(AVIOContext
*pb
, uint32_t elementid
, uint64_t val
)
381 put_ebml_id(pb
, elementid
);
382 put_ebml_length(pb
, bytes
, 0);
383 for (i
= bytes
- 1; i
>= 0; i
--)
384 avio_w8(pb
, (uint8_t)(val
>> i
* 8));
387 static void put_ebml_float(AVIOContext
*pb
, uint32_t elementid
, double val
)
389 put_ebml_id(pb
, elementid
);
390 put_ebml_length(pb
, 8, 0);
391 avio_wb64(pb
, av_double2int(val
));
394 static void put_ebml_binary(AVIOContext
*pb
, uint32_t elementid
,
395 const void *buf
, int size
)
397 put_ebml_id(pb
, elementid
);
398 put_ebml_length(pb
, size
, 0);
399 avio_write(pb
, buf
, size
);
402 static void put_ebml_string(AVIOContext
*pb
, uint32_t elementid
,
405 put_ebml_binary(pb
, elementid
, str
, strlen(str
));
409 * Write a void element of a given size. Useful for reserving space in
410 * the file to be written to later.
412 * @param size The number of bytes to reserve, which must be at least 2.
414 static void put_ebml_void(AVIOContext
*pb
, int size
)
416 av_assert0(size
>= 2);
418 put_ebml_id(pb
, EBML_ID_VOID
);
419 // we need to subtract the length needed to store the size from the
420 // size we need to reserve so 2 cases, we use 8 bytes to store the
421 // size if possible, 1 byte otherwise
424 put_ebml_length(pb
, size
, 0);
427 put_ebml_length(pb
, size
, 8);
429 ffio_fill(pb
, 0, size
);
432 static ebml_master
start_ebml_master(AVIOContext
*pb
, uint32_t elementid
,
433 uint64_t expectedsize
)
435 int bytes
= expectedsize
? ebml_length_size(expectedsize
) : 8;
437 put_ebml_id(pb
, elementid
);
438 put_ebml_size_unknown(pb
, bytes
);
439 return (ebml_master
) { avio_tell(pb
), bytes
};
442 static void end_ebml_master(AVIOContext
*pb
, ebml_master master
)
444 int64_t pos
= avio_tell(pb
);
446 if (avio_seek(pb
, master
.pos
- master
.sizebytes
, SEEK_SET
) < 0)
448 put_ebml_length(pb
, pos
- master
.pos
, master
.sizebytes
);
449 avio_seek(pb
, pos
, SEEK_SET
);
452 static EbmlElement
*ebml_writer_add(EbmlWriter
*writer
,
453 uint32_t id
, EbmlType type
)
455 writer
->elements
[writer
->nb_elements
].id
= id
;
456 writer
->elements
[writer
->nb_elements
].type
= type
;
457 return &writer
->elements
[writer
->nb_elements
++];
460 static void ebml_writer_open_master(EbmlWriter
*writer
, uint32_t id
)
462 EbmlElement
*const elem
= ebml_writer_add(writer
, id
, EBML_MASTER
);
463 EbmlMaster
*const master
= &elem
->priv
.master
;
465 master
->containing_master
= writer
->current_master_element
;
466 master
->nb_elements
= -1;
468 writer
->current_master_element
= writer
->nb_elements
- 1;
471 static void ebml_writer_close_master(EbmlWriter
*writer
)
474 av_assert2(writer
->current_master_element
>= 0);
475 av_assert2(writer
->current_master_element
< writer
->nb_elements
);
476 elem
= &writer
->elements
[writer
->current_master_element
];
477 av_assert2(elem
->type
== EBML_MASTER
);
478 av_assert2(elem
->priv
.master
.nb_elements
< 0); /* means unset */
479 elem
->priv
.master
.nb_elements
= writer
->nb_elements
- writer
->current_master_element
- 1;
480 av_assert2(elem
->priv
.master
.containing_master
< 0 ||
481 elem
->priv
.master
.containing_master
< writer
->current_master_element
);
482 writer
->current_master_element
= elem
->priv
.master
.containing_master
;
485 static void ebml_writer_close_or_discard_master(EbmlWriter
*writer
)
487 av_assert2(writer
->nb_elements
> 0);
488 av_assert2(0 <= writer
->current_master_element
);
489 av_assert2(writer
->current_master_element
< writer
->nb_elements
);
490 if (writer
->current_master_element
== writer
->nb_elements
- 1) {
491 const EbmlElement
*const elem
= &writer
->elements
[writer
->nb_elements
- 1];
492 /* The master element has no children. Discard it. */
493 av_assert2(elem
->type
== EBML_MASTER
);
494 av_assert2(elem
->priv
.master
.containing_master
< 0 ||
495 elem
->priv
.master
.containing_master
< writer
->current_master_element
);
496 writer
->current_master_element
= elem
->priv
.master
.containing_master
;
497 writer
->nb_elements
--;
500 ebml_writer_close_master(writer
);
503 static void ebml_writer_add_string(EbmlWriter
*writer
, uint32_t id
,
506 EbmlElement
*const elem
= ebml_writer_add(writer
, id
, EBML_STR
);
508 elem
->priv
.str
= str
;
511 static void ebml_writer_add_bin(EbmlWriter
*writer
, uint32_t id
,
512 const uint8_t *data
, size_t size
)
514 EbmlElement
*const elem
= ebml_writer_add(writer
, id
, EBML_BIN
);
516 #if SIZE_MAX > UINT64_MAX
517 size
= FFMIN(size
, UINT64_MAX
);
520 elem
->priv
.bin
= data
;
523 static void ebml_writer_add_float(EbmlWriter
*writer
, uint32_t id
,
526 EbmlElement
*const elem
= ebml_writer_add(writer
, id
, EBML_FLOAT
);
531 static void ebml_writer_add_uid(EbmlWriter
*writer
, uint32_t id
,
534 EbmlElement
*const elem
= ebml_writer_add(writer
, id
, EBML_UID
);
535 elem
->priv
.uint
= val
;
538 static void ebml_writer_add_uint(EbmlWriter
*writer
, uint32_t id
,
541 EbmlElement
*elem
= ebml_writer_add(writer
, id
, EBML_UINT
);
542 elem
->priv
.uint
= val
;
545 static void ebml_writer_add_sint(EbmlWriter
*writer
, uint32_t id
,
548 EbmlElement
*elem
= ebml_writer_add(writer
, id
, EBML_SINT
);
549 elem
->priv
.sint
= val
;
552 static void ebml_writer_add_block(EbmlWriter
*writer
, MatroskaMuxContext
*mkv
)
554 EbmlElement
*elem
= ebml_writer_add(writer
, MATROSKA_ID_BLOCK
, EBML_BLOCK
);
555 elem
->priv
.mkv
= mkv
;
558 static int ebml_writer_str_len(EbmlElement
*elem
)
560 size_t len
= strlen(elem
->priv
.str
);
561 #if SIZE_MAX > UINT64_MAX
562 len
= FF_MIN(len
, UINT64_MAX
);
568 static av_const
int uint_size(uint64_t val
)
577 static int ebml_writer_uint_len(EbmlElement
*elem
)
579 elem
->size
= uint_size(elem
->priv
.uint
);
583 static av_const
int sint_size(int64_t val
)
585 uint64_t tmp
= 2 * (uint64_t)(val
< 0 ? val
^-1 : val
);
586 return uint_size(tmp
);
589 static int ebml_writer_sint_len(EbmlElement
*elem
)
591 elem
->size
= sint_size(elem
->priv
.sint
);
595 static int ebml_writer_elem_len(EbmlWriter
*writer
, EbmlElement
*elem
,
596 int remaining_elems
);
598 static int ebml_writer_master_len(EbmlWriter
*writer
, EbmlElement
*elem
,
601 int nb_elems
= elem
->priv
.master
.nb_elements
>= 0 ? elem
->priv
.master
.nb_elements
: remaining_elems
- 1;
602 EbmlElement
*const master
= elem
;
603 uint64_t total_size
= 0;
605 master
->priv
.master
.nb_elements
= nb_elems
;
606 for (; elem
++, nb_elems
> 0;) {
607 int ret
= ebml_writer_elem_len(writer
, elem
, nb_elems
);
610 av_assert2(ret
< nb_elems
);
611 /* No overflow is possible here, as both total_size and elem->size
612 * are bounded by MAX_SUPPORTED_EBML_LENGTH. */
613 total_size
+= ebml_id_size(elem
->id
) + elem
->length_size
+ elem
->size
;
614 if (total_size
> MAX_SUPPORTED_EBML_LENGTH
)
615 return AVERROR(ERANGE
);
616 nb_elems
--; /* consume elem */
617 elem
+= ret
, nb_elems
-= ret
; /* and elem's children */
619 master
->size
= total_size
;
621 return master
->priv
.master
.nb_elements
;
624 static int ebml_writer_block_len(EbmlElement
*elem
)
626 MatroskaMuxContext
*const mkv
= elem
->priv
.mkv
;
627 BlockContext
*const block
= &mkv
->cur_block
;
628 mkv_track
*const track
= block
->track
;
629 const AVPacket
*const pkt
= block
->pkt
;
632 if (track
->reformat
) {
633 err
= track
->reformat(mkv
, NULL
, pkt
, &size
);
635 av_log(mkv
->ctx
, AV_LOG_ERROR
, "Error when reformatting data of "
636 "a packet from stream %d.\n", pkt
->stream_index
);
641 if (track
->offset
<= size
)
642 size
-= track
->offset
;
644 elem
->size
= track
->track_num_size
+ 3U + size
;
649 static void ebml_writer_write_block(const EbmlElement
*elem
, AVIOContext
*pb
)
651 MatroskaMuxContext
*const mkv
= elem
->priv
.mkv
;
652 BlockContext
*const block
= &mkv
->cur_block
;
653 mkv_track
*const track
= block
->track
;
654 const AVPacket
*const pkt
= block
->pkt
;
656 put_ebml_num(pb
, track
->track_num
, track
->track_num_size
);
657 avio_wb16(pb
, block
->rel_ts
);
658 avio_w8(pb
, block
->flags
);
660 if (track
->reformat
) {
662 track
->reformat(mkv
, pb
, pkt
, &size
);
664 const uint8_t *data
= pkt
->data
;
665 unsigned offset
= track
->offset
<= pkt
->size
? track
->offset
: 0;
666 avio_write(pb
, data
+ offset
, pkt
->size
- offset
);
670 static int ebml_writer_elem_len(EbmlWriter
*writer
, EbmlElement
*elem
,
675 switch (elem
->type
) {
681 ret
= ebml_writer_str_len(elem
);
684 ret
= ebml_writer_uint_len(elem
);
687 ret
= ebml_writer_sint_len(elem
);
690 ret
= ebml_writer_block_len(elem
);
693 ret
= ebml_writer_master_len(writer
, elem
, remaining_elems
);
698 if (elem
->size
> MAX_SUPPORTED_EBML_LENGTH
)
699 return AVERROR(ERANGE
);
700 elem
->length_size
= ebml_length_size(elem
->size
);
701 return ret
; /* number of elements consumed excluding elem itself */
704 static int ebml_writer_elem_write(const EbmlElement
*elem
, AVIOContext
*pb
)
706 put_ebml_id(pb
, elem
->id
);
707 put_ebml_num(pb
, elem
->size
, elem
->length_size
);
708 switch (elem
->type
) {
711 uint64_t val
= elem
->type
== EBML_UID
? elem
->priv
.uint
712 : av_double2int(elem
->priv
.f
);
718 uint64_t val
= elem
->type
== EBML_UINT
? elem
->priv
.uint
720 for (int i
= elem
->size
; --i
>= 0; )
721 avio_w8(pb
, (uint8_t)(val
>> i
* 8));
726 const uint8_t *data
= elem
->type
== EBML_BIN
? elem
->priv
.bin
727 : (const uint8_t*)elem
->priv
.str
;
728 avio_write(pb
, data
, elem
->size
);
732 ebml_writer_write_block(elem
, pb
);
735 int nb_elems
= elem
->priv
.master
.nb_elements
;
738 for (int i
= 0; i
< nb_elems
; i
++)
739 i
+= ebml_writer_elem_write(elem
+ i
, pb
);
747 static int ebml_writer_write(EbmlWriter
*writer
, AVIOContext
*pb
)
749 int ret
= ebml_writer_elem_len(writer
, writer
->elements
,
750 writer
->nb_elements
);
753 ebml_writer_elem_write(writer
->elements
, pb
);
757 static void mkv_add_seekhead_entry(MatroskaMuxContext
*mkv
, uint32_t elementid
,
760 mkv_seekhead
*seekhead
= &mkv
->seekhead
;
762 av_assert1(seekhead
->num_entries
< MAX_SEEKHEAD_ENTRIES
);
764 seekhead
->entries
[seekhead
->num_entries
].elementid
= elementid
;
765 seekhead
->entries
[seekhead
->num_entries
++].segmentpos
= filepos
- mkv
->segment_offset
;
768 static int start_ebml_master_crc32(AVIOContext
**dyn_cp
, MatroskaMuxContext
*mkv
)
772 if (!*dyn_cp
&& (ret
= avio_open_dyn_buf(dyn_cp
)) < 0)
776 put_ebml_void(*dyn_cp
, 6); /* Reserve space for CRC32 so position/size calculations using avio_tell() take it into account */
781 static int end_ebml_master_crc32(AVIOContext
*pb
, AVIOContext
**dyn_cp
,
782 MatroskaMuxContext
*mkv
, uint32_t id
,
783 int length_size
, int keep_buffer
,
786 uint8_t *buf
, crc
[4];
787 int ret
, size
, skip
= 0;
789 size
= avio_get_dyn_buf(*dyn_cp
, &buf
);
790 if ((ret
= (*dyn_cp
)->error
) < 0)
794 mkv_add_seekhead_entry(mkv
, id
, avio_tell(pb
));
797 put_ebml_length(pb
, size
, length_size
);
798 if (mkv
->write_crc
) {
799 skip
= 6; /* Skip reserved 6-byte long void element from the dynamic buffer. */
800 AV_WL32(crc
, av_crc(av_crc_get_table(AV_CRC_32_IEEE_LE
), UINT32_MAX
, buf
+ skip
, size
- skip
) ^ UINT32_MAX
);
801 put_ebml_binary(pb
, EBML_ID_CRC32
, crc
, sizeof(crc
));
803 avio_write(pb
, buf
+ skip
, size
- skip
);
807 ffio_reset_dyn_buf(*dyn_cp
);
809 ffio_free_dyn_buf(dyn_cp
);
815 * Output EBML master. Keep the buffer if seekable, allowing for later updates.
816 * Furthermore always add a SeekHead Entry for this element.
818 static int end_ebml_master_crc32_tentatively(AVIOContext
*pb
,
819 ebml_stored_master
*elem
,
820 MatroskaMuxContext
*mkv
, uint32_t id
)
822 if (IS_SEEKABLE(pb
, mkv
)) {
824 int size
= avio_get_dyn_buf(elem
->bc
, &buf
);
826 if (elem
->bc
->error
< 0)
827 return elem
->bc
->error
;
829 elem
->pos
= avio_tell(pb
);
830 mkv_add_seekhead_entry(mkv
, id
, elem
->pos
);
833 put_ebml_length(pb
, size
, 0);
834 avio_write(pb
, buf
, size
);
838 return end_ebml_master_crc32(pb
, &elem
->bc
, mkv
, id
, 0, 0, 1);
841 static void put_xiph_size(AVIOContext
*pb
, int size
)
843 ffio_fill(pb
, 255, size
/ 255);
844 avio_w8(pb
, size
% 255);
848 * Free the members allocated in the mux context.
850 static void mkv_deinit(AVFormatContext
*s
)
852 MatroskaMuxContext
*mkv
= s
->priv_data
;
854 ffio_free_dyn_buf(&mkv
->cluster_bc
);
855 ffio_free_dyn_buf(&mkv
->info
.bc
);
856 ffio_free_dyn_buf(&mkv
->track
.bc
);
857 ffio_free_dyn_buf(&mkv
->tags
.bc
);
858 ffio_free_dyn_buf(&mkv
->tmp_bc
);
860 av_freep(&mkv
->cur_block
.h2645_nalu_list
.nalus
);
861 av_freep(&mkv
->cues
.entries
);
862 av_freep(&mkv
->tracks
);
866 * Initialize the SeekHead element to be ready to index level 1 Matroska
867 * elements. Enough space to write MAX_SEEKHEAD_ENTRIES SeekHead entries
868 * will be reserved at the current file location.
870 static void mkv_start_seekhead(MatroskaMuxContext
*mkv
, AVIOContext
*pb
)
872 mkv
->seekhead
.filepos
= avio_tell(pb
);
873 // 21 bytes max for a Seek entry, 6 bytes max for the SeekHead ID
874 // and size, 6 bytes for a CRC32 element, and 2 bytes to guarantee
875 // that an EBML void element will fit afterwards
876 mkv
->seekhead
.reserved_size
= MAX_SEEKHEAD_ENTRIES
* MAX_SEEKENTRY_SIZE
+ 14;
877 put_ebml_void(pb
, mkv
->seekhead
.reserved_size
);
881 * Write the SeekHead to the file at the location reserved for it
882 * and seek to destpos afterwards. When error_on_seek_failure
883 * is not set, failure to seek to the position designated for the
884 * SeekHead is not considered an error and it is presumed that
885 * destpos is the current position; failure to seek to destpos
886 * afterwards is always an error.
888 * @return 0 on success, < 0 on error.
890 static int mkv_write_seekhead(AVIOContext
*pb
, MatroskaMuxContext
*mkv
,
891 int error_on_seek_failure
, int64_t destpos
)
893 AVIOContext
*dyn_cp
= NULL
;
894 mkv_seekhead
*seekhead
= &mkv
->seekhead
;
895 int64_t remaining
, ret64
;
898 if ((ret64
= avio_seek(pb
, seekhead
->filepos
, SEEK_SET
)) < 0)
899 return error_on_seek_failure
? ret64
: 0;
901 ret
= start_ebml_master_crc32(&dyn_cp
, mkv
);
905 for (i
= 0; i
< seekhead
->num_entries
; i
++) {
906 mkv_seekhead_entry
*entry
= &seekhead
->entries
[i
];
907 ebml_master seekentry
= start_ebml_master(dyn_cp
, MATROSKA_ID_SEEKENTRY
,
910 put_ebml_id(dyn_cp
, MATROSKA_ID_SEEKID
);
911 put_ebml_length(dyn_cp
, ebml_id_size(entry
->elementid
), 0);
912 put_ebml_id(dyn_cp
, entry
->elementid
);
914 put_ebml_uint(dyn_cp
, MATROSKA_ID_SEEKPOSITION
, entry
->segmentpos
);
915 end_ebml_master(dyn_cp
, seekentry
);
917 ret
= end_ebml_master_crc32(pb
, &dyn_cp
, mkv
,
918 MATROSKA_ID_SEEKHEAD
, 0, 0, 0);
922 remaining
= seekhead
->filepos
+ seekhead
->reserved_size
- avio_tell(pb
);
923 put_ebml_void(pb
, remaining
);
925 if ((ret64
= avio_seek(pb
, destpos
, SEEK_SET
)) < 0)
931 static int mkv_add_cuepoint(MatroskaMuxContext
*mkv
, int stream
, int64_t ts
,
932 int64_t cluster_pos
, int64_t relative_pos
, int64_t duration
)
934 mkv_cues
*cues
= &mkv
->cues
;
935 mkv_cuepoint
*entries
= cues
->entries
;
936 unsigned idx
= cues
->num_entries
;
941 entries
= av_realloc_array(entries
, cues
->num_entries
+ 1, sizeof(mkv_cuepoint
));
943 return AVERROR(ENOMEM
);
944 cues
->entries
= entries
;
946 /* Make sure the cues entries are sorted by pts. */
947 while (idx
> 0 && entries
[idx
- 1].pts
> ts
)
949 memmove(&entries
[idx
+ 1], &entries
[idx
],
950 (cues
->num_entries
- idx
) * sizeof(entries
[0]));
952 entries
[idx
].pts
= ts
;
953 entries
[idx
].stream_idx
= stream
;
954 entries
[idx
].cluster_pos
= cluster_pos
- mkv
->segment_offset
;
955 entries
[idx
].relative_pos
= relative_pos
;
956 entries
[idx
].duration
= duration
;
963 static int mkv_assemble_cues(AVStream
**streams
, AVIOContext
*dyn_cp
, AVIOContext
*cuepoint
,
964 const mkv_cues
*cues
, mkv_track
*tracks
, int num_tracks
,
967 for (mkv_cuepoint
*entry
= cues
->entries
, *end
= entry
+ cues
->num_entries
;
969 uint64_t pts
= entry
->pts
;
973 put_ebml_uint(cuepoint
, MATROSKA_ID_CUETIME
, pts
);
975 // put all the entries from different tracks that have the exact same
976 // timestamp into the same CuePoint
977 for (int j
= 0; j
< num_tracks
; j
++)
978 tracks
[j
].has_cue
= 0;
980 ebml_master track_positions
;
981 int idx
= entry
->stream_idx
;
983 av_assert0(idx
>= 0 && idx
< num_tracks
);
984 if (tracks
[idx
].has_cue
&& streams
[idx
]->codecpar
->codec_type
!= AVMEDIA_TYPE_SUBTITLE
)
986 tracks
[idx
].has_cue
= 1;
987 track_positions
= start_ebml_master(cuepoint
, MATROSKA_ID_CUETRACKPOSITION
, MAX_CUETRACKPOS_SIZE
);
988 put_ebml_uint(cuepoint
, MATROSKA_ID_CUETRACK
, tracks
[idx
].track_num
);
989 put_ebml_uint(cuepoint
, MATROSKA_ID_CUECLUSTERPOSITION
, entry
->cluster_pos
+ offset
);
990 put_ebml_uint(cuepoint
, MATROSKA_ID_CUERELATIVEPOSITION
, entry
->relative_pos
);
991 if (entry
->duration
> 0)
992 put_ebml_uint(cuepoint
, MATROSKA_ID_CUEDURATION
, entry
->duration
);
993 end_ebml_master(cuepoint
, track_positions
);
994 } while (++entry
< end
&& entry
->pts
== pts
);
995 size
= avio_get_dyn_buf(cuepoint
, &buf
);
996 if (cuepoint
->error
< 0)
997 return cuepoint
->error
;
998 put_ebml_binary(dyn_cp
, MATROSKA_ID_POINTENTRY
, buf
, size
);
999 ffio_reset_dyn_buf(cuepoint
);
1005 static int put_xiph_codecpriv(AVFormatContext
*s
, AVIOContext
*pb
,
1006 const AVCodecParameters
*par
,
1007 const uint8_t *extradata
, int extradata_size
)
1009 const uint8_t *header_start
[3];
1011 int first_header_size
;
1014 if (par
->codec_id
== AV_CODEC_ID_VORBIS
)
1015 first_header_size
= 30;
1017 first_header_size
= 42;
1019 err
= avpriv_split_xiph_headers(extradata
, extradata_size
,
1020 first_header_size
, header_start
, header_len
);
1022 av_log(s
, AV_LOG_ERROR
, "Extradata corrupt.\n");
1026 avio_w8(pb
, 2); // number packets - 1
1027 for (j
= 0; j
< 2; j
++) {
1028 put_xiph_size(pb
, header_len
[j
]);
1030 for (j
= 0; j
< 3; j
++)
1031 avio_write(pb
, header_start
[j
], header_len
[j
]);
1036 #if CONFIG_MATROSKA_MUXER
1037 static int put_wv_codecpriv(AVIOContext
*pb
, const uint8_t *extradata
, int extradata_size
)
1039 if (extradata
&& extradata_size
== 2)
1040 avio_write(pb
, extradata
, 2);
1042 avio_wl16(pb
, 0x410); // fallback to the most recent version
1046 static int put_flac_codecpriv(AVFormatContext
*s
, AVIOContext
*pb
,
1047 const AVCodecParameters
*par
,
1048 const uint8_t *extradata
, int extradata_size
)
1050 int write_comment
= (par
->ch_layout
.order
== AV_CHANNEL_ORDER_NATIVE
&&
1051 !(par
->ch_layout
.u
.mask
& ~0x3ffffULL
) &&
1052 !ff_flac_is_native_layout(par
->ch_layout
.u
.mask
));
1053 int ret
= ff_flac_write_header(pb
, extradata
, extradata_size
,
1059 if (write_comment
) {
1060 const char *vendor
= (s
->flags
& AVFMT_FLAG_BITEXACT
) ?
1061 "Lavf" : LIBAVFORMAT_IDENT
;
1062 AVDictionary
*dict
= NULL
;
1066 snprintf(buf
, sizeof(buf
), "0x%"PRIx64
, par
->ch_layout
.u
.mask
);
1067 av_dict_set(&dict
, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", buf
, 0);
1069 len
= ff_vorbiscomment_length(dict
, vendor
, NULL
, 0);
1070 av_assert1(len
< (1 << 24) - 4);
1075 ff_vorbiscomment_write(pb
, dict
, vendor
, NULL
, 0);
1077 av_dict_free(&dict
);
1083 static int get_aac_sample_rates(AVFormatContext
*s
, MatroskaMuxContext
*mkv
,
1084 const uint8_t *extradata
, int extradata_size
,
1085 int *sample_rate
, int *output_sample_rate
)
1087 MPEG4AudioConfig mp4ac
;
1090 ret
= avpriv_mpeg4audio_get_config2(&mp4ac
, extradata
, extradata_size
, 1, s
);
1091 /* Don't abort if the failure is because of missing extradata. Assume in that
1092 * case a bitstream filter will provide the muxer with the extradata in the
1094 * Abort however if s->pb is not seekable, as we would not be able to seek back
1095 * to write the sample rate elements once the extradata shows up, anyway. */
1096 if (ret
< 0 && (extradata_size
|| !IS_SEEKABLE(s
->pb
, mkv
))) {
1097 av_log(s
, AV_LOG_ERROR
,
1098 "Error parsing AAC extradata, unable to determine samplerate.\n");
1099 return AVERROR(EINVAL
);
1103 /* This will only happen when this function is called while writing the
1104 * header and no extradata is available. The space for this element has
1105 * to be reserved for when this function is called again after the
1106 * extradata shows up in the first packet, as there's no way to know if
1107 * output_sample_rate will be different than sample_rate or not. */
1108 *output_sample_rate
= *sample_rate
;
1110 *sample_rate
= mp4ac
.sample_rate
;
1111 *output_sample_rate
= mp4ac
.ext_sample_rate
;
1117 static int mkv_assemble_native_codecprivate(AVFormatContext
*s
, AVIOContext
*dyn_cp
,
1118 const AVCodecParameters
*par
,
1119 const uint8_t *extradata
,
1121 unsigned *size_to_reserve
)
1123 switch (par
->codec_id
) {
1124 case AV_CODEC_ID_VORBIS
:
1125 case AV_CODEC_ID_THEORA
:
1126 return put_xiph_codecpriv(s
, dyn_cp
, par
, extradata
, extradata_size
);
1127 case AV_CODEC_ID_AV1
:
1129 return ff_isom_write_av1c(dyn_cp
, extradata
,
1132 *size_to_reserve
= (AV1_SANE_SEQUENCE_HEADER_MAX_BITS
+ 7) / 8 + 100;
1134 #if CONFIG_MATROSKA_MUXER
1135 case AV_CODEC_ID_FLAC
:
1136 return put_flac_codecpriv(s
, dyn_cp
, par
, extradata
, extradata_size
);
1137 case AV_CODEC_ID_WAVPACK
:
1138 return put_wv_codecpriv(dyn_cp
, extradata
, extradata_size
);
1139 case AV_CODEC_ID_H264
:
1140 return ff_isom_write_avcc(dyn_cp
, extradata
,
1142 case AV_CODEC_ID_HEVC
:
1143 return ff_isom_write_hvcc(dyn_cp
, extradata
,
1144 extradata_size
, 0, s
);
1145 case AV_CODEC_ID_VVC
:
1146 return ff_isom_write_vvcc(dyn_cp
, extradata
,
1148 case AV_CODEC_ID_ALAC
:
1149 if (extradata_size
< 36) {
1150 av_log(s
, AV_LOG_ERROR
,
1151 "Invalid extradata found, ALAC expects a 36-byte "
1153 return AVERROR_INVALIDDATA
;
1155 avio_write(dyn_cp
, extradata
+ 12,
1156 extradata_size
- 12);
1158 case AV_CODEC_ID_AAC
:
1160 avio_write(dyn_cp
, extradata
, extradata_size
);
1162 *size_to_reserve
= MAX_PCE_SIZE
;
1164 case AV_CODEC_ID_ARIB_CAPTION
: {
1165 unsigned stream_identifier
, data_component_id
;
1166 switch (par
->profile
) {
1167 case AV_PROFILE_ARIB_PROFILE_A
:
1168 stream_identifier
= 0x30;
1169 data_component_id
= 0x0008;
1171 case AV_PROFILE_ARIB_PROFILE_C
:
1172 stream_identifier
= 0x87;
1173 data_component_id
= 0x0012;
1176 av_log(s
, AV_LOG_ERROR
,
1177 "Unset/unknown ARIB caption profile %d utilized!\n",
1179 return AVERROR_INVALIDDATA
;
1181 avio_w8(dyn_cp
, stream_identifier
);
1182 avio_wb16(dyn_cp
, data_component_id
);
1187 if (CONFIG_MATROSKA_MUXER
&& par
->codec_id
== AV_CODEC_ID_PRORES
&&
1188 ff_codec_get_id(ff_codec_movvideo_tags
, par
->codec_tag
) == AV_CODEC_ID_PRORES
) {
1189 avio_wl32(dyn_cp
, par
->codec_tag
);
1190 } else if (extradata_size
&& par
->codec_id
!= AV_CODEC_ID_TTA
)
1191 avio_write(dyn_cp
, extradata
, extradata_size
);
1197 static int mkv_assemble_codecprivate(AVFormatContext
*s
, AVIOContext
*dyn_cp
,
1198 AVCodecParameters
*par
,
1199 const uint8_t *extradata
, int extradata_size
,
1200 int native_id
, int qt_id
,
1201 uint8_t **codecpriv
, int *codecpriv_size
,
1202 unsigned *max_payload_size
)
1204 av_unused MatroskaMuxContext
*const mkv
= s
->priv_data
;
1205 unsigned size_to_reserve
= 0;
1209 ret
= mkv_assemble_native_codecprivate(s
, dyn_cp
, par
,
1210 extradata
, extradata_size
,
1214 #if CONFIG_MATROSKA_MUXER
1215 } else if (par
->codec_type
== AVMEDIA_TYPE_VIDEO
) {
1217 if (!par
->codec_tag
)
1218 par
->codec_tag
= ff_codec_get_tag(ff_codec_movvideo_tags
,
1220 if ( ff_codec_get_id(ff_codec_movvideo_tags
, par
->codec_tag
) == par
->codec_id
1221 && (!extradata_size
|| ff_codec_get_id(ff_codec_movvideo_tags
, AV_RL32(extradata
+ 4)) != par
->codec_id
)
1223 avio_wb32(dyn_cp
, 0x5a + extradata_size
);
1224 avio_wl32(dyn_cp
, par
->codec_tag
);
1225 ffio_fill(dyn_cp
, 0, 0x5a - 8);
1227 avio_write(dyn_cp
, extradata
, extradata_size
);
1229 if (!ff_codec_get_tag(ff_codec_bmp_tags
, par
->codec_id
))
1230 av_log(s
, AV_LOG_WARNING
, "codec %s is not supported by this format\n",
1231 avcodec_get_name(par
->codec_id
));
1233 if (!par
->codec_tag
)
1234 par
->codec_tag
= ff_codec_get_tag(ff_codec_bmp_tags
,
1236 if (!par
->codec_tag
&& par
->codec_id
!= AV_CODEC_ID_RAWVIDEO
) {
1237 av_log(s
, AV_LOG_ERROR
, "No bmp codec tag found for codec %s\n",
1238 avcodec_get_name(par
->codec_id
));
1239 return AVERROR(EINVAL
);
1242 /* If vfw extradata updates are supported, this will have
1243 * to be updated to pass extradata(_size) explicitly. */
1244 ff_put_bmp_header(dyn_cp
, par
, 0, 0, mkv
->flipped_raw_rgb
);
1246 } else if (par
->codec_type
== AVMEDIA_TYPE_AUDIO
) {
1248 tag
= ff_codec_get_tag(ff_codec_wav_tags
, par
->codec_id
);
1250 av_log(s
, AV_LOG_ERROR
, "No wav codec tag found for codec %s\n",
1251 avcodec_get_name(par
->codec_id
));
1252 return AVERROR(EINVAL
);
1254 if (!par
->codec_tag
)
1255 par
->codec_tag
= tag
;
1257 /* Same comment as for ff_put_bmp_header applies here. */
1258 ret
= ff_put_wav_header(s
, dyn_cp
, par
, FF_PUT_WAV_HEADER_FORCE_WAVEFORMATEX
);
1264 *codecpriv_size
= avio_get_dyn_buf(dyn_cp
, codecpriv
);
1265 if (dyn_cp
->error
< 0)
1266 return dyn_cp
->error
;
1267 *max_payload_size
= *codecpriv_size
+ size_to_reserve
;
1272 static void mkv_put_codecprivate(AVIOContext
*pb
, unsigned max_payload_size
,
1273 const uint8_t *codecpriv
, unsigned codecpriv_size
)
1275 unsigned total_codecpriv_size
= 0, total_size
;
1277 av_assert1(codecpriv_size
<= max_payload_size
);
1279 if (!max_payload_size
)
1282 total_size
= 2 + ebml_length_size(max_payload_size
) + max_payload_size
;
1284 if (codecpriv_size
) {
1285 unsigned length_size
= ebml_length_size(codecpriv_size
);
1287 total_codecpriv_size
= 2U + length_size
+ codecpriv_size
;
1288 if (total_codecpriv_size
+ 1 == total_size
) {
1289 /* It is impossible to add one byte of padding via an EBML Void. */
1291 total_codecpriv_size
++;
1293 put_ebml_id(pb
, MATROSKA_ID_CODECPRIVATE
);
1294 put_ebml_length(pb
, codecpriv_size
, length_size
);
1295 avio_write(pb
, codecpriv
, codecpriv_size
);
1297 if (total_codecpriv_size
< total_size
)
1298 put_ebml_void(pb
, total_size
- total_codecpriv_size
);
1301 static int mkv_update_codecprivate(AVFormatContext
*s
, MatroskaMuxContext
*mkv
,
1302 uint8_t *side_data
, int side_data_size
,
1303 AVCodecParameters
*par
, AVIOContext
*pb
,
1304 mkv_track
*track
, unsigned alternative_size
)
1306 AVIOContext
*const dyn_bc
= mkv
->tmp_bc
;
1308 unsigned max_payload_size
;
1309 int ret
, codecpriv_size
;
1311 ret
= mkv_assemble_codecprivate(s
, dyn_bc
, par
,
1312 side_data
, side_data_size
, 1, 0,
1313 &codecpriv
, &codecpriv_size
, &max_payload_size
);
1316 if (codecpriv_size
> track
->codecpriv_size
&& !alternative_size
) {
1317 ret
= AVERROR(ENOSPC
);
1319 } else if (codecpriv_size
> track
->codecpriv_size
) {
1320 av_assert1(alternative_size
< track
->codecpriv_size
);
1321 codecpriv_size
= alternative_size
;
1323 avio_seek(pb
, track
->codecpriv_offset
, SEEK_SET
);
1324 mkv_put_codecprivate(pb
, track
->codecpriv_size
,
1325 codecpriv
, codecpriv_size
);
1327 if (!par
->extradata_size
) {
1328 ret
= ff_alloc_extradata(par
, side_data_size
);
1331 memcpy(par
->extradata
, side_data
, side_data_size
);
1334 ffio_reset_dyn_buf(dyn_bc
);
1338 #define MAX_VIDEO_COLOR_ELEMS 20
1339 static void mkv_write_video_color(EbmlWriter
*writer
, const AVStream
*st
,
1340 const AVCodecParameters
*par
)
1342 const AVPacketSideData
*side_data
;
1344 ebml_writer_open_master(writer
, MATROSKA_ID_VIDEOCOLOR
);
1346 if (par
->color_trc
!= AVCOL_TRC_UNSPECIFIED
&&
1347 par
->color_trc
< AVCOL_TRC_NB
) {
1348 ebml_writer_add_uint(writer
, MATROSKA_ID_VIDEOCOLORTRANSFERCHARACTERISTICS
,
1351 if (par
->color_space
!= AVCOL_SPC_UNSPECIFIED
&&
1352 par
->color_space
< AVCOL_SPC_NB
) {
1353 ebml_writer_add_uint(writer
, MATROSKA_ID_VIDEOCOLORMATRIXCOEFF
,
1356 if (par
->color_primaries
!= AVCOL_PRI_UNSPECIFIED
&&
1357 par
->color_primaries
< AVCOL_PRI_NB
) {
1358 ebml_writer_add_uint(writer
, MATROSKA_ID_VIDEOCOLORPRIMARIES
,
1359 par
->color_primaries
);
1361 if (par
->color_range
!= AVCOL_RANGE_UNSPECIFIED
&&
1362 par
->color_range
< AVCOL_RANGE_NB
) {
1363 ebml_writer_add_uint(writer
, MATROSKA_ID_VIDEOCOLORRANGE
, par
->color_range
);
1365 if (par
->chroma_location
!= AVCHROMA_LOC_UNSPECIFIED
&&
1366 par
->chroma_location
<= AVCHROMA_LOC_TOP
) {
1369 av_chroma_location_enum_to_pos(&xpos
, &ypos
, par
->chroma_location
);
1370 ebml_writer_add_uint(writer
, MATROSKA_ID_VIDEOCOLORCHROMASITINGHORZ
,
1372 ebml_writer_add_uint(writer
, MATROSKA_ID_VIDEOCOLORCHROMASITINGVERT
,
1376 side_data
= av_packet_side_data_get(par
->coded_side_data
, par
->nb_coded_side_data
,
1377 AV_PKT_DATA_CONTENT_LIGHT_LEVEL
);
1379 const AVContentLightMetadata
*metadata
= (AVContentLightMetadata
*)side_data
->data
;
1380 ebml_writer_add_uint(writer
, MATROSKA_ID_VIDEOCOLORMAXCLL
,
1382 ebml_writer_add_uint(writer
, MATROSKA_ID_VIDEOCOLORMAXFALL
,
1386 side_data
= av_packet_side_data_get(par
->coded_side_data
, par
->nb_coded_side_data
,
1387 AV_PKT_DATA_MASTERING_DISPLAY_METADATA
);
1389 const AVMasteringDisplayMetadata
*metadata
= (AVMasteringDisplayMetadata
*)side_data
->data
;
1390 ebml_writer_open_master(writer
, MATROSKA_ID_VIDEOCOLORMASTERINGMETA
);
1391 if (metadata
->has_primaries
) {
1392 ebml_writer_add_float(writer
, MATROSKA_ID_VIDEOCOLOR_RX
,
1393 av_q2d(metadata
->display_primaries
[0][0]));
1394 ebml_writer_add_float(writer
, MATROSKA_ID_VIDEOCOLOR_RY
,
1395 av_q2d(metadata
->display_primaries
[0][1]));
1396 ebml_writer_add_float(writer
, MATROSKA_ID_VIDEOCOLOR_GX
,
1397 av_q2d(metadata
->display_primaries
[1][0]));
1398 ebml_writer_add_float(writer
, MATROSKA_ID_VIDEOCOLOR_GY
,
1399 av_q2d(metadata
->display_primaries
[1][1]));
1400 ebml_writer_add_float(writer
, MATROSKA_ID_VIDEOCOLOR_BX
,
1401 av_q2d(metadata
->display_primaries
[2][0]));
1402 ebml_writer_add_float(writer
, MATROSKA_ID_VIDEOCOLOR_BY
,
1403 av_q2d(metadata
->display_primaries
[2][1]));
1404 ebml_writer_add_float(writer
, MATROSKA_ID_VIDEOCOLOR_WHITEX
,
1405 av_q2d(metadata
->white_point
[0]));
1406 ebml_writer_add_float(writer
, MATROSKA_ID_VIDEOCOLOR_WHITEY
,
1407 av_q2d(metadata
->white_point
[1]));
1409 if (metadata
->has_luminance
) {
1410 ebml_writer_add_float(writer
, MATROSKA_ID_VIDEOCOLOR_LUMINANCEMAX
,
1411 av_q2d(metadata
->max_luminance
));
1412 ebml_writer_add_float(writer
, MATROSKA_ID_VIDEOCOLOR_LUMINANCEMIN
,
1413 av_q2d(metadata
->min_luminance
));
1415 ebml_writer_close_or_discard_master(writer
);
1418 ebml_writer_close_or_discard_master(writer
);
1421 #define MAX_VIDEO_PROJECTION_ELEMS 6
1422 static void mkv_handle_rotation(void *logctx
, const AVCodecParameters
*par
,
1423 double *yaw
, double *roll
)
1425 const int32_t *matrix
;
1426 const AVPacketSideData
*side_data
=
1427 av_packet_side_data_get(par
->coded_side_data
, par
->nb_coded_side_data
,
1428 AV_PKT_DATA_DISPLAYMATRIX
);
1433 matrix
= (int32_t *)side_data
->data
;
1435 /* Check whether this is an affine transformation */
1436 if (matrix
[2] || matrix
[5])
1439 /* This together with the checks below test whether
1440 * the upper-left 2x2 matrix is nonsingular. */
1441 if (!matrix
[0] && !matrix
[1])
1444 /* We ignore the translation part of the matrix (matrix[6] and matrix[7])
1445 * as well as any scaling, i.e. we only look at the upper left 2x2 matrix.
1446 * We only accept matrices that are an exact multiple of an orthogonal one.
1447 * Apart from the multiple, every such matrix can be obtained by
1448 * potentially flipping in the x-direction (corresponding to yaw = 180)
1449 * followed by a rotation of (say) an angle phi in the counterclockwise
1450 * direction. The upper-left 2x2 matrix then looks like this:
1451 * | (+/-)cos(phi) (-/+)sin(phi) |
1453 * | sin(phi) cos(phi) |
1454 * The first set of signs in the first row apply in case of no flipping,
1455 * the second set applies in case of flipping. */
1457 /* The casts to int64_t are needed because -INT32_MIN doesn't fit
1459 if (matrix
[0] == matrix
[4] && -(int64_t)matrix
[1] == matrix
[3]) {
1460 /* No flipping case */
1462 } else if (-(int64_t)matrix
[0] == matrix
[4] && matrix
[1] == matrix
[3]) {
1463 /* Horizontal flip */
1467 av_log(logctx
, AV_LOG_INFO
, "Ignoring display matrix indicating "
1468 "non-orthogonal transformation.\n");
1471 *roll
= 180 / M_PI
* atan2(matrix
[3], matrix
[4]);
1473 /* We do not write a ProjectionType element indicating "rectangular",
1474 * because this is the default value. */
1477 static int mkv_handle_spherical(void *logctx
, EbmlWriter
*writer
,
1478 const AVCodecParameters
*par
, uint8_t private[],
1479 double *yaw
, double *pitch
, double *roll
)
1481 const AVPacketSideData
*sd
= av_packet_side_data_get(par
->coded_side_data
,
1482 par
->nb_coded_side_data
,
1483 AV_PKT_DATA_SPHERICAL
);
1484 const AVSphericalMapping
*spherical
;
1489 spherical
= (const AVSphericalMapping
*)sd
->data
;
1490 if (spherical
->projection
!= AV_SPHERICAL_EQUIRECTANGULAR
&&
1491 spherical
->projection
!= AV_SPHERICAL_EQUIRECTANGULAR_TILE
&&
1492 spherical
->projection
!= AV_SPHERICAL_CUBEMAP
) {
1493 av_log(logctx
, AV_LOG_WARNING
, "Unknown projection type\n");
1497 switch (spherical
->projection
) {
1498 case AV_SPHERICAL_EQUIRECTANGULAR
:
1499 case AV_SPHERICAL_EQUIRECTANGULAR_TILE
:
1500 ebml_writer_add_uint(writer
, MATROSKA_ID_VIDEOPROJECTIONTYPE
,
1501 MATROSKA_VIDEO_PROJECTION_TYPE_EQUIRECTANGULAR
);
1502 AV_WB32(private, 0); // version + flags
1503 if (spherical
->projection
== AV_SPHERICAL_EQUIRECTANGULAR
) {
1504 AV_WB32(private + 4, 0);
1505 AV_WB32(private + 8, 0);
1506 AV_WB32(private + 12, 0);
1507 AV_WB32(private + 16, 0);
1509 AV_WB32(private + 4, spherical
->bound_top
);
1510 AV_WB32(private + 8, spherical
->bound_bottom
);
1511 AV_WB32(private + 12, spherical
->bound_left
);
1512 AV_WB32(private + 16, spherical
->bound_right
);
1514 ebml_writer_add_bin(writer
, MATROSKA_ID_VIDEOPROJECTIONPRIVATE
,
1517 case AV_SPHERICAL_CUBEMAP
:
1518 ebml_writer_add_uint(writer
, MATROSKA_ID_VIDEOPROJECTIONTYPE
,
1519 MATROSKA_VIDEO_PROJECTION_TYPE_CUBEMAP
);
1520 AV_WB32(private, 0); // version + flags
1521 AV_WB32(private + 4, 0); // layout
1522 AV_WB32(private + 8, spherical
->padding
);
1523 ebml_writer_add_bin(writer
, MATROSKA_ID_VIDEOPROJECTIONPRIVATE
,
1530 *yaw
= (double) spherical
->yaw
/ (1 << 16);
1531 *pitch
= (double) spherical
->pitch
/ (1 << 16);
1532 *roll
= (double) spherical
->roll
/ (1 << 16);
1534 return 1; /* Projection included */
1537 static void mkv_write_video_projection(void *logctx
, EbmlWriter
*wr
,
1538 const AVCodecParameters
*par
,
1541 double yaw
= 0, pitch
= 0, roll
= 0;
1544 ebml_writer_open_master(wr
, MATROSKA_ID_VIDEOPROJECTION
);
1546 ret
= mkv_handle_spherical(logctx
, wr
, par
, private, &yaw
, &pitch
, &roll
);
1548 mkv_handle_rotation(logctx
, par
, &yaw
, &roll
);
1551 ebml_writer_add_float(wr
, MATROSKA_ID_VIDEOPROJECTIONPOSEYAW
, yaw
);
1553 ebml_writer_add_float(wr
, MATROSKA_ID_VIDEOPROJECTIONPOSEPITCH
, pitch
);
1555 ebml_writer_add_float(wr
, MATROSKA_ID_VIDEOPROJECTIONPOSEROLL
, roll
);
1557 ebml_writer_close_or_discard_master(wr
);
1560 #define MAX_FIELD_ORDER_ELEMS 2
1561 static void mkv_write_field_order(EbmlWriter
*writer
, int is_webm
,
1562 enum AVFieldOrder field_order
)
1564 switch (field_order
) {
1565 case AV_FIELD_UNKNOWN
:
1567 case AV_FIELD_PROGRESSIVE
:
1568 ebml_writer_add_uint(writer
, MATROSKA_ID_VIDEOFLAGINTERLACED
,
1569 MATROSKA_VIDEO_INTERLACE_FLAG_PROGRESSIVE
);
1575 ebml_writer_add_uint(writer
, MATROSKA_ID_VIDEOFLAGINTERLACED
,
1576 MATROSKA_VIDEO_INTERLACE_FLAG_INTERLACED
);
1578 switch (field_order
) {
1580 ebml_writer_add_uint(writer
, MATROSKA_ID_VIDEOFIELDORDER
,
1581 MATROSKA_VIDEO_FIELDORDER_TT
);
1584 ebml_writer_add_uint(writer
, MATROSKA_ID_VIDEOFIELDORDER
,
1585 MATROSKA_VIDEO_FIELDORDER_BB
);
1588 ebml_writer_add_uint(writer
, MATROSKA_ID_VIDEOFIELDORDER
,
1589 MATROSKA_VIDEO_FIELDORDER_TB
);
1592 ebml_writer_add_uint(writer
, MATROSKA_ID_VIDEOFIELDORDER
,
1593 MATROSKA_VIDEO_FIELDORDER_BT
);
1600 #define MAX_STEREO_MODE_ELEMS 1
1601 static int mkv_write_stereo_mode(AVFormatContext
*s
, EbmlWriter
*writer
,
1602 const AVCodecParameters
*par
,
1603 const AVStream
*st
, int is_webm
,
1604 int *h_width
, int *h_height
)
1606 const char *error_message_addendum
= "";
1607 const AVDictionaryEntry
*tag
;
1608 MatroskaVideoStereoModeType format
= MATROSKA_VIDEO_STEREOMODE_TYPE_NB
;
1610 /* The following macros create bitfields where the ith bit
1611 * indicates whether the MatroskaVideoStereoModeType with that value
1612 * uses double width/height or is WebM compatible. */
1613 #define FLAG(STEREOMODETYPE, BOOL) | (BOOL) << (STEREOMODETYPE)
1614 #define WDIV1(STEREOMODETYPE, STEREO3DTYPE, FLAGS, WDIV, HDIV, WEBM) \
1615 FLAG(STEREOMODETYPE, WDIV)
1616 #define WDIV2(STEREOMODETYPE, WDIV, HDIV, WEBM) \
1617 FLAG(STEREOMODETYPE, WDIV)
1618 // The zero in the following line consumes the first '|'.
1619 const unsigned width_bitfield
= 0 STEREOMODE_STEREO3D_MAPPING(WDIV1
, WDIV2
);
1621 #define HDIV1(STEREOMODETYPE, STEREO3DTYPE, FLAGS, WDIV, HDIV, WEBM) \
1622 FLAG(STEREOMODETYPE, HDIV)
1623 #define HDIV2(STEREOMODETYPE, WDIV, HDIV, WEBM) \
1624 FLAG(STEREOMODETYPE, HDIV)
1625 const unsigned height_bitfield
= 0 STEREOMODE_STEREO3D_MAPPING(HDIV1
, HDIV2
);
1627 #define WEBM1(STEREOMODETYPE, STEREO3DTYPE, FLAGS, WDIV, HDIV, WEBM) \
1628 FLAG(STEREOMODETYPE, WEBM)
1629 #define WEBM2(STEREOMODETYPE, WDIV, HDIV, WEBM) \
1630 FLAG(STEREOMODETYPE, WEBM)
1631 const unsigned webm_bitfield
= 0 STEREOMODE_STEREO3D_MAPPING(WEBM1
, WEBM2
);
1636 if ((tag
= av_dict_get(st
->metadata
, "stereo_mode", NULL
, 0)) ||
1637 (tag
= av_dict_get( s
->metadata
, "stereo_mode", NULL
, 0))) {
1639 for (int i
= 0; i
< MATROSKA_VIDEO_STEREOMODE_TYPE_NB
; i
++)
1640 if (!strcmp(tag
->value
, ff_matroska_video_stereo_mode
[i
])){
1644 if (format
== MATROSKA_VIDEO_STEREOMODE_TYPE_NB
) {
1645 long stereo_mode
= strtol(tag
->value
, NULL
, 0);
1646 if ((unsigned long)stereo_mode
>= MATROSKA_VIDEO_STEREOMODE_TYPE_NB
)
1648 format
= stereo_mode
;
1651 const AVPacketSideData
*sd
;
1652 const AVStereo3D
*stereo
;
1653 /* The following macro presumes all MATROSKA_VIDEO_STEREOMODE_TYPE_*
1654 * values to be in the range 0..254. */
1655 #define STEREOMODE(STEREOMODETYPE, STEREO3DTYPE, FLAGS, WDIV, HDIV, WEBM) \
1656 [(STEREO3DTYPE)][!!((FLAGS) & AV_STEREO3D_FLAG_INVERT)] = (STEREOMODETYPE) + 1,
1657 #define NOTHING(STEREOMODETYPE, WDIV, HDIV, WEBM)
1658 static const unsigned char conversion_table
[][2] = {
1659 STEREOMODE_STEREO3D_MAPPING(STEREOMODE
, NOTHING
)
1663 sd
= av_packet_side_data_get(par
->coded_side_data
, par
->nb_coded_side_data
,
1664 AV_PKT_DATA_STEREO3D
);
1668 stereo
= (const AVStereo3D
*)sd
->data
;
1670 /* A garbage AVStereo3D or something with no Matroska analogon. */
1671 if ((unsigned)stereo
->type
>= FF_ARRAY_ELEMS(conversion_table
))
1674 fmt
= conversion_table
[stereo
->type
][!!(stereo
->flags
& AV_STEREO3D_FLAG_INVERT
)];
1675 /* Format with no Matroska analogon; ignore it */
1681 // if webm, do not write unsupported modes
1682 if (is_webm
&& !(webm_bitfield
>> format
)) {
1683 error_message_addendum
= " for WebM";
1687 *h_width
= 1 << ((width_bitfield
>> format
) & 1);
1688 *h_height
= 1 << ((height_bitfield
>> format
) & 1);
1690 // write StereoMode if format is valid
1691 ebml_writer_add_uint(writer
, MATROSKA_ID_VIDEOSTEREOMODE
, format
);
1695 av_log(s
, AV_LOG_ERROR
,
1696 "The specified stereo mode is not valid%s.\n",
1697 error_message_addendum
);
1698 return AVERROR(EINVAL
);
1701 static void mkv_write_blockadditionmapping(AVFormatContext
*s
, const MatroskaMuxContext
*mkv
,
1702 const AVCodecParameters
*par
, AVIOContext
*pb
,
1703 mkv_track
*track
, const AVStream
*st
)
1705 #if CONFIG_MATROSKA_MUXER
1706 const AVDOVIDecoderConfigurationRecord
*dovi
;
1707 const AVPacketSideData
*sd
;
1709 if (IS_SEEKABLE(s
->pb
, mkv
)) {
1710 track
->blockadditionmapping_offset
= avio_tell(pb
);
1711 // We can't know at this point if there will be a block with BlockAdditions, so
1712 // we either write the default value here, or a void element. Either of them will
1713 // be overwritten when finishing the track.
1714 put_ebml_uint(pb
, MATROSKA_ID_TRACKMAXBLKADDID
, 0);
1715 if (par
->codec_type
== AVMEDIA_TYPE_VIDEO
) {
1716 // Similarly, reserve space for an eventual
1717 // HDR10+ ITU T.35 metadata BlockAdditionMapping.
1718 put_ebml_void(pb
, 3 /* BlockAdditionMapping */
1719 + 4 /* BlockAddIDValue */
1720 + 4 /* BlockAddIDType */);
1724 sd
= av_packet_side_data_get(par
->coded_side_data
, par
->nb_coded_side_data
,
1725 AV_PKT_DATA_DOVI_CONF
);
1730 dovi
= (const AVDOVIDecoderConfigurationRecord
*)sd
->data
;
1731 if (dovi
->dv_profile
<= 10) {
1732 ebml_master mapping
;
1733 uint8_t buf
[ISOM_DVCC_DVVC_SIZE
];
1736 uint64_t expected_size
= (2 + 1 + (sizeof(DVCC_DVVC_BLOCK_TYPE_NAME
) - 1))
1737 + (2 + 1 + 4) + (2 + 1 + ISOM_DVCC_DVVC_SIZE
);
1739 if (dovi
->dv_profile
> 7) {
1740 type
= MATROSKA_BLOCK_ADD_ID_TYPE_DVVC
;
1742 type
= MATROSKA_BLOCK_ADD_ID_TYPE_DVCC
;
1745 ff_isom_put_dvcc_dvvc(s
, buf
, dovi
);
1747 mapping
= start_ebml_master(pb
, MATROSKA_ID_TRACKBLKADDMAPPING
, expected_size
);
1749 put_ebml_string(pb
, MATROSKA_ID_BLKADDIDNAME
, DVCC_DVVC_BLOCK_TYPE_NAME
);
1750 put_ebml_uint(pb
, MATROSKA_ID_BLKADDIDTYPE
, type
);
1751 put_ebml_binary(pb
, MATROSKA_ID_BLKADDIDEXTRADATA
, buf
, sizeof(buf
));
1753 end_ebml_master(pb
, mapping
);
1758 static int mkv_write_track_video(AVFormatContext
*s
, MatroskaMuxContext
*mkv
,
1759 const AVStream
*st
, const AVCodecParameters
*par
,
1762 const AVDictionaryEntry
*tag
;
1763 int display_width_div
= 1, display_height_div
= 1;
1764 uint8_t color_space
[4], projection_private
[20];
1765 const AVPacketSideData
*sd
;
1766 EBML_WRITER(MAX_FIELD_ORDER_ELEMS
+ MAX_STEREO_MODE_ELEMS
+
1767 MAX_VIDEO_COLOR_ELEMS
+ MAX_VIDEO_PROJECTION_ELEMS
+ 12);
1768 int cropped_width
= par
->width
, cropped_height
= par
->height
;
1771 ebml_writer_open_master(&writer
, MATROSKA_ID_TRACKVIDEO
);
1773 ebml_writer_add_uint(&writer
, MATROSKA_ID_VIDEOPIXELWIDTH
, par
->width
);
1774 ebml_writer_add_uint(&writer
, MATROSKA_ID_VIDEOPIXELHEIGHT
, par
->height
);
1776 mkv_write_field_order(&writer
, IS_WEBM(mkv
), par
->field_order
);
1778 // check both side data and metadata for stereo information,
1779 // write the result to the bitstream if any is found
1780 ret
= mkv_write_stereo_mode(s
, &writer
, par
, st
, IS_WEBM(mkv
),
1782 &display_height_div
);
1786 if (par
->format
== AV_PIX_FMT_YUVA420P
||
1787 ((tag
= av_dict_get(st
->metadata
, "alpha_mode", NULL
, 0)) ||
1788 (tag
= av_dict_get( s
->metadata
, "alpha_mode", NULL
, 0))) && strtol(tag
->value
, NULL
, 0))
1789 ebml_writer_add_uint(&writer
, MATROSKA_ID_VIDEOALPHAMODE
, 1);
1791 sd
= av_packet_side_data_get(par
->coded_side_data
,
1792 par
->nb_coded_side_data
,
1793 AV_PKT_DATA_FRAME_CROPPING
);
1794 if (sd
&& sd
->size
== sizeof(uint32_t) * 4) {
1795 uint64_t top
, bottom
, left
, right
;
1797 top
= AV_RL32(sd
->data
+ 0);
1798 bottom
= AV_RL32(sd
->data
+ 4);
1799 left
= AV_RL32(sd
->data
+ 8);
1800 right
= AV_RL32(sd
->data
+ 12);
1802 if ((left
+ right
) >= par
->width
||
1803 (top
+ bottom
) >= par
->height
) {
1804 av_log(s
, AV_LOG_ERROR
, "Invalid cropping dimensions in stream side data\n");
1805 return AVERROR(EINVAL
);
1809 ebml_writer_add_uint(&writer
, MATROSKA_ID_VIDEOPIXELCROPB
, bottom
);
1811 ebml_writer_add_uint(&writer
, MATROSKA_ID_VIDEOPIXELCROPT
, top
);
1813 ebml_writer_add_uint(&writer
, MATROSKA_ID_VIDEOPIXELCROPL
, left
);
1815 ebml_writer_add_uint(&writer
, MATROSKA_ID_VIDEOPIXELCROPR
, right
);
1817 cropped_width
-= left
+ right
;
1818 cropped_height
-= top
+ bottom
;
1821 // write DisplayWidth and DisplayHeight, they contain the size of
1822 // a single source view and/or the display aspect ratio
1823 if (st
->sample_aspect_ratio
.num
) {
1824 int64_t d_width
= av_rescale(cropped_width
, st
->sample_aspect_ratio
.num
, st
->sample_aspect_ratio
.den
);
1825 if (d_width
> INT_MAX
) {
1826 av_log(s
, AV_LOG_ERROR
, "Overflow in display width\n");
1827 return AVERROR(EINVAL
);
1829 if (d_width
!= cropped_width
|| display_width_div
!= 1 || display_height_div
!= 1) {
1830 if (IS_WEBM(mkv
) || display_width_div
!= 1 || display_height_div
!= 1) {
1831 ebml_writer_add_uint(&writer
, MATROSKA_ID_VIDEODISPLAYWIDTH
,
1832 d_width
/ display_width_div
);
1833 ebml_writer_add_uint(&writer
, MATROSKA_ID_VIDEODISPLAYHEIGHT
,
1834 cropped_height
/ display_height_div
);
1836 AVRational display_aspect_ratio
;
1837 av_reduce(&display_aspect_ratio
.num
, &display_aspect_ratio
.den
,
1838 cropped_width
* (int64_t)st
->sample_aspect_ratio
.num
,
1839 cropped_height
* (int64_t)st
->sample_aspect_ratio
.den
,
1841 ebml_writer_add_uint(&writer
, MATROSKA_ID_VIDEODISPLAYWIDTH
,
1842 display_aspect_ratio
.num
);
1843 ebml_writer_add_uint(&writer
, MATROSKA_ID_VIDEODISPLAYHEIGHT
,
1844 display_aspect_ratio
.den
);
1845 ebml_writer_add_uint(&writer
, MATROSKA_ID_VIDEODISPLAYUNIT
,
1846 MATROSKA_VIDEO_DISPLAYUNIT_DAR
);
1849 } else if (display_width_div
!= 1 || display_height_div
!= 1) {
1850 ebml_writer_add_uint(&writer
, MATROSKA_ID_VIDEODISPLAYWIDTH
,
1851 cropped_width
/ display_width_div
);
1852 ebml_writer_add_uint(&writer
, MATROSKA_ID_VIDEODISPLAYHEIGHT
,
1853 cropped_height
/ display_height_div
);
1854 } else if (!IS_WEBM(mkv
))
1855 ebml_writer_add_uint(&writer
, MATROSKA_ID_VIDEODISPLAYUNIT
,
1856 MATROSKA_VIDEO_DISPLAYUNIT_UNKNOWN
);
1858 if (par
->codec_id
== AV_CODEC_ID_RAWVIDEO
) {
1859 AV_WL32(color_space
, par
->codec_tag
);
1860 ebml_writer_add_bin(&writer
, MATROSKA_ID_VIDEOCOLORSPACE
,
1861 color_space
, sizeof(color_space
));
1863 mkv_write_video_color(&writer
, st
, par
);
1864 mkv_write_video_projection(s
, &writer
, par
, projection_private
);
1866 return ebml_writer_write(&writer
, pb
);
1869 static void mkv_write_default_duration(mkv_track
*track
, AVIOContext
*pb
,
1870 AVRational duration
)
1872 put_ebml_uint(pb
, MATROSKA_ID_TRACKDEFAULTDURATION
,
1873 1000000000LL * duration
.num
/ duration
.den
);
1874 track
->default_duration_low
= 1000LL * duration
.num
/ duration
.den
;
1875 track
->default_duration_high
= track
->default_duration_low
+
1876 !!(1000LL * duration
.num
% duration
.den
);
1879 static int mkv_write_track(AVFormatContext
*s
, MatroskaMuxContext
*mkv
,
1880 AVStream
*st
, mkv_track
*track
, AVIOContext
*pb
,
1883 AVCodecParameters
*par
= st
->codecpar
;
1884 ebml_master subinfo
, track_master
;
1888 int sample_rate
= par
->sample_rate
;
1889 int output_sample_rate
= 0;
1891 const AVDictionaryEntry
*tag
;
1893 if (par
->codec_type
== AVMEDIA_TYPE_ATTACHMENT
)
1896 track_master
= start_ebml_master(pb
, MATROSKA_ID_TRACKENTRY
, 0);
1897 put_ebml_uint(pb
, MATROSKA_ID_TRACKNUMBER
, track
->track_num
);
1898 put_ebml_uid (pb
, MATROSKA_ID_TRACKUID
, track
->uid
);
1899 put_ebml_uint(pb
, MATROSKA_ID_TRACKFLAGLACING
, 0); // no lacing (yet)
1901 if ((tag
= av_dict_get(st
->metadata
, "title", NULL
, 0)))
1902 put_ebml_string(pb
, MATROSKA_ID_TRACKNAME
, tag
->value
);
1903 tag
= av_dict_get(st
->metadata
, "language", NULL
, 0);
1904 put_ebml_string(pb
, MATROSKA_ID_TRACKLANGUAGE
,
1905 tag
&& tag
->value
[0] ? tag
->value
: "und");
1907 // The default value for TRACKFLAGDEFAULT is 1, so add element
1908 // if we need to clear it.
1910 put_ebml_uint(pb
, MATROSKA_ID_TRACKFLAGDEFAULT
, 0);
1912 if (st
->disposition
& AV_DISPOSITION_FORCED
)
1913 put_ebml_uint(pb
, MATROSKA_ID_TRACKFLAGFORCED
, 1);
1916 const char *codec_id
;
1917 if (par
->codec_id
!= AV_CODEC_ID_WEBVTT
) {
1918 for (j
= 0; ff_webm_codec_tags
[j
].id
!= AV_CODEC_ID_NONE
; j
++) {
1919 if (ff_webm_codec_tags
[j
].id
== par
->codec_id
) {
1920 codec_id
= ff_webm_codec_tags
[j
].str
;
1926 if (st
->disposition
& AV_DISPOSITION_CAPTIONS
) {
1927 codec_id
= "D_WEBVTT/CAPTIONS";
1928 native_id
= MATROSKA_TRACK_TYPE_SUBTITLE
;
1929 } else if (st
->disposition
& AV_DISPOSITION_DESCRIPTIONS
) {
1930 codec_id
= "D_WEBVTT/DESCRIPTIONS";
1931 native_id
= MATROSKA_TRACK_TYPE_METADATA
;
1932 } else if (st
->disposition
& AV_DISPOSITION_METADATA
) {
1933 codec_id
= "D_WEBVTT/METADATA";
1934 native_id
= MATROSKA_TRACK_TYPE_METADATA
;
1936 codec_id
= "D_WEBVTT/SUBTITLES";
1937 native_id
= MATROSKA_TRACK_TYPE_SUBTITLE
;
1942 av_log(s
, AV_LOG_ERROR
,
1943 "Only VP8 or VP9 or AV1 video and Vorbis or Opus audio and WebVTT subtitles are supported for WebM.\n");
1944 return AVERROR(EINVAL
);
1947 put_ebml_string(pb
, MATROSKA_ID_CODECID
, codec_id
);
1949 if (st
->disposition
& AV_DISPOSITION_COMMENT
)
1950 put_ebml_uint(pb
, MATROSKA_ID_TRACKFLAGCOMMENTARY
, 1);
1951 if (st
->disposition
& AV_DISPOSITION_HEARING_IMPAIRED
)
1952 put_ebml_uint(pb
, MATROSKA_ID_TRACKFLAGHEARINGIMPAIRED
, 1);
1953 if (st
->disposition
& AV_DISPOSITION_VISUAL_IMPAIRED
)
1954 put_ebml_uint(pb
, MATROSKA_ID_TRACKFLAGVISUALIMPAIRED
, 1);
1955 if (st
->disposition
& (AV_DISPOSITION_ORIGINAL
| AV_DISPOSITION_DUB
) &&
1956 (st
->disposition
& (AV_DISPOSITION_ORIGINAL
| AV_DISPOSITION_DUB
))
1957 != (AV_DISPOSITION_ORIGINAL
| AV_DISPOSITION_DUB
))
1958 put_ebml_uint(pb
, MATROSKA_ID_TRACKFLAGORIGINAL
,
1959 !!(st
->disposition
& AV_DISPOSITION_ORIGINAL
));
1961 // look for a codec ID string specific to mkv to use,
1962 // if none are found, use AVI codes
1963 if (par
->codec_id
== AV_CODEC_ID_JPEG2000
) {
1964 /* JPEG2000 is actually supported natively in Matroska,
1965 * yet we use the VfW way to mux it for compatibility
1966 * with old demuxers. (FIXME: Are they really important?) */
1967 } else if (par
->codec_id
!= AV_CODEC_ID_RAWVIDEO
|| par
->codec_tag
) {
1968 for (j
= 0; ff_mkv_codec_tags
[j
].id
!= AV_CODEC_ID_NONE
; j
++) {
1969 if (ff_mkv_codec_tags
[j
].id
== par
->codec_id
) {
1970 put_ebml_string(pb
, MATROSKA_ID_CODECID
, ff_mkv_codec_tags
[j
].str
);
1976 if (mkv
->allow_raw_vfw
) {
1979 av_log(s
, AV_LOG_ERROR
, "Raw RGB is not supported Natively in Matroska, you can use AVI or NUT or\n"
1980 "If you would like to store it anyway using VFW mode, enable allow_raw_vfw (-allow_raw_vfw 1)\n");
1981 return AVERROR(EINVAL
);
1986 switch (par
->codec_type
) {
1987 AVRational frame_rate
;
1988 int audio_frame_samples
;
1990 case AVMEDIA_TYPE_VIDEO
:
1991 mkv
->have_video
= 1;
1992 put_ebml_uint(pb
, MATROSKA_ID_TRACKTYPE
, MATROSKA_TRACK_TYPE_VIDEO
);
1994 frame_rate
= (AVRational
){ 0, 1 };
1995 if (st
->avg_frame_rate
.num
> 0 && st
->avg_frame_rate
.den
> 0)
1996 frame_rate
= st
->avg_frame_rate
;
1997 else if (st
->r_frame_rate
.num
> 0 && st
->r_frame_rate
.den
> 0)
1998 frame_rate
= st
->r_frame_rate
;
2000 if (frame_rate
.num
> 0)
2001 mkv_write_default_duration(track
, pb
, av_inv_q(frame_rate
));
2003 if (CONFIG_MATROSKA_MUXER
&& !native_id
&&
2004 ff_codec_get_tag(ff_codec_movvideo_tags
, par
->codec_id
) &&
2005 ((!ff_codec_get_tag(ff_codec_bmp_tags
, par
->codec_id
) && par
->codec_id
!= AV_CODEC_ID_RAWVIDEO
) ||
2006 par
->codec_id
== AV_CODEC_ID_SVQ1
||
2007 par
->codec_id
== AV_CODEC_ID_SVQ3
||
2008 par
->codec_id
== AV_CODEC_ID_CINEPAK
))
2012 put_ebml_string(pb
, MATROSKA_ID_CODECID
, "V_QUICKTIME");
2013 else if (!native_id
) {
2014 // if there is no mkv-specific codec ID, use VFW mode
2015 put_ebml_string(pb
, MATROSKA_ID_CODECID
, "V_MS/VFW/FOURCC");
2016 track
->write_dts
= 1;
2017 ffformatcontext(s
)->avoid_negative_ts_use_pts
= 0;
2020 ret
= mkv_write_track_video(s
, mkv
, st
, par
, pb
);
2026 case AVMEDIA_TYPE_AUDIO
:
2027 if (par
->initial_padding
) {
2028 int64_t codecdelay
= av_rescale_q(par
->initial_padding
,
2029 (AVRational
){ 1, par
->sample_rate
},
2030 (AVRational
){ 1, 1000000000 });
2031 if (codecdelay
< 0) {
2032 av_log(s
, AV_LOG_ERROR
, "Initial padding is invalid\n");
2033 return AVERROR(EINVAL
);
2035 put_ebml_uint(pb
, MATROSKA_ID_CODECDELAY
, codecdelay
);
2037 track
->ts_offset
= av_rescale_q(par
->initial_padding
,
2038 (AVRational
){ 1, par
->sample_rate
},
2040 ffstream(st
)->lowest_ts_allowed
= -track
->ts_offset
;
2042 if (par
->codec_id
== AV_CODEC_ID_OPUS
)
2043 put_ebml_uint(pb
, MATROSKA_ID_SEEKPREROLL
, OPUS_SEEK_PREROLL
);
2044 #if CONFIG_MATROSKA_MUXER
2045 else if (par
->codec_id
== AV_CODEC_ID_AAC
) {
2046 ret
= get_aac_sample_rates(s
, mkv
, par
->extradata
, par
->extradata_size
,
2047 &sample_rate
, &output_sample_rate
);
2053 put_ebml_uint(pb
, MATROSKA_ID_TRACKTYPE
, MATROSKA_TRACK_TYPE_AUDIO
);
2055 audio_frame_samples
= av_get_audio_frame_duration2(par
, 0);
2056 if (audio_frame_samples
)
2057 mkv_write_default_duration(track
, pb
, (AVRational
){ audio_frame_samples
,
2058 par
->sample_rate
});
2060 // no mkv-specific ID, use ACM mode
2061 put_ebml_string(pb
, MATROSKA_ID_CODECID
, "A_MS/ACM");
2063 subinfo
= start_ebml_master(pb
, MATROSKA_ID_TRACKAUDIO
, 6 + 4 * 9);
2064 put_ebml_uint(pb
, MATROSKA_ID_AUDIOCHANNELS
, par
->ch_layout
.nb_channels
);
2066 track
->sample_rate_offset
= avio_tell(pb
);
2067 put_ebml_float (pb
, MATROSKA_ID_AUDIOSAMPLINGFREQ
, sample_rate
);
2068 if (output_sample_rate
)
2069 put_ebml_float(pb
, MATROSKA_ID_AUDIOOUTSAMPLINGFREQ
, output_sample_rate
);
2071 bit_depth
= av_get_bits_per_sample(par
->codec_id
);
2072 if (!bit_depth
&& par
->codec_id
!= AV_CODEC_ID_ADPCM_G726
) {
2073 if (par
->bits_per_raw_sample
)
2074 bit_depth
= par
->bits_per_raw_sample
;
2076 bit_depth
= av_get_bytes_per_sample(par
->format
) << 3;
2079 bit_depth
= par
->bits_per_coded_sample
;
2081 put_ebml_uint(pb
, MATROSKA_ID_AUDIOBITDEPTH
, bit_depth
);
2082 end_ebml_master(pb
, subinfo
);
2085 case AVMEDIA_TYPE_SUBTITLE
:
2087 av_log(s
, AV_LOG_ERROR
, "Subtitle codec %s (%d) is not supported.\n",
2088 avcodec_get_name(par
->codec_id
), par
->codec_id
);
2089 return AVERROR(ENOSYS
);
2091 if (!IS_WEBM(mkv
) && st
->disposition
& AV_DISPOSITION_DESCRIPTIONS
)
2092 put_ebml_uint(pb
, MATROSKA_ID_TRACKFLAGTEXTDESCRIPTIONS
, 1);
2094 if (!IS_WEBM(mkv
) || par
->codec_id
!= AV_CODEC_ID_WEBVTT
)
2095 native_id
= MATROSKA_TRACK_TYPE_SUBTITLE
;
2097 put_ebml_uint(pb
, MATROSKA_ID_TRACKTYPE
, native_id
);
2100 av_log(s
, AV_LOG_ERROR
, "Only audio, video, and subtitles are supported for Matroska.\n");
2101 return AVERROR(EINVAL
);
2105 mkv_write_blockadditionmapping(s
, mkv
, par
, pb
, track
, st
);
2107 if (!IS_WEBM(mkv
) || par
->codec_id
!= AV_CODEC_ID_WEBVTT
) {
2109 int codecpriv_size
, max_payload_size
;
2110 track
->codecpriv_offset
= avio_tell(pb
);
2111 ret
= mkv_assemble_codecprivate(s
, mkv
->tmp_bc
, par
,
2112 par
->extradata
, par
->extradata_size
,
2114 &codecpriv
, &codecpriv_size
, &max_payload_size
);
2117 mkv_put_codecprivate(pb
, max_payload_size
, codecpriv
, codecpriv_size
);
2118 track
->codecpriv_size
= max_payload_size
;
2121 end_ebml_master(pb
, track_master
);
2124 ffio_reset_dyn_buf(mkv
->tmp_bc
);
2129 static int mkv_write_tracks(AVFormatContext
*s
)
2131 MatroskaMuxContext
*mkv
= s
->priv_data
;
2132 AVIOContext
*pb
= s
->pb
;
2133 int video_default_idx
= -1, audio_default_idx
= -1, subtitle_default_idx
= -1;
2136 if (mkv
->nb_attachments
== s
->nb_streams
)
2139 ret
= start_ebml_master_crc32(&mkv
->track
.bc
, mkv
);
2143 if (mkv
->default_mode
!= DEFAULT_MODE_PASSTHROUGH
) {
2144 int video_idx
= -1, audio_idx
= -1, subtitle_idx
= -1;
2146 for (i
= s
->nb_streams
- 1; i
>= 0; i
--) {
2147 AVStream
*st
= s
->streams
[i
];
2149 switch (st
->codecpar
->codec_type
) {
2150 #define CASE(type, variable) \
2151 case AVMEDIA_TYPE_ ## type: \
2152 variable ## _idx = i; \
2153 if (st->disposition & AV_DISPOSITION_DEFAULT) \
2154 variable ## _default_idx = i; \
2158 CASE(SUBTITLE
, subtitle
)
2163 video_default_idx
= FFMAX(video_default_idx
, video_idx
);
2164 audio_default_idx
= FFMAX(audio_default_idx
, audio_idx
);
2165 if (mkv
->default_mode
!= DEFAULT_MODE_INFER_NO_SUBS
)
2166 subtitle_default_idx
= FFMAX(subtitle_default_idx
, subtitle_idx
);
2168 for (i
= 0; i
< s
->nb_streams
; i
++) {
2169 AVStream
*st
= s
->streams
[i
];
2170 int is_default
= st
->disposition
& AV_DISPOSITION_DEFAULT
||
2171 i
== video_default_idx
|| i
== audio_default_idx
||
2172 i
== subtitle_default_idx
;
2173 ret
= mkv_write_track(s
, mkv
, st
, &mkv
->tracks
[i
],
2174 mkv
->track
.bc
, is_default
);
2179 return end_ebml_master_crc32_tentatively(pb
, &mkv
->track
, mkv
,
2180 MATROSKA_ID_TRACKS
);
2183 static int mkv_write_simpletag(AVIOContext
*pb
, const AVDictionaryEntry
*t
)
2186 uint8_t *key
= av_strdup(t
->key
);
2188 const uint8_t *lang
= NULL
;
2192 return AVERROR(ENOMEM
);
2194 if ((p
= strrchr(p
, '-')) &&
2195 (lang
= ff_convert_lang_to(p
+ 1, AV_LANG_ISO639_2_BIBL
)))
2202 else if (*p
>= 'a' && *p
<= 'z')
2207 ebml_writer_open_master(&writer
, MATROSKA_ID_SIMPLETAG
);
2208 ebml_writer_add_string(&writer
, MATROSKA_ID_TAGNAME
, key
);
2210 ebml_writer_add_string(&writer
, MATROSKA_ID_TAGLANG
, lang
);
2211 ebml_writer_add_string(&writer
, MATROSKA_ID_TAGSTRING
, t
->value
);
2212 ret
= ebml_writer_write(&writer
, pb
);
2218 static void mkv_write_tag_targets(MatroskaMuxContext
*mkv
, AVIOContext
*pb
,
2219 uint32_t elementid
, uint64_t uid
)
2221 ebml_master targets
= start_ebml_master(pb
, MATROSKA_ID_TAGTARGETS
,
2224 put_ebml_uid(pb
, elementid
, uid
);
2225 end_ebml_master(pb
, targets
);
2228 static int mkv_check_tag_name(const char *name
, uint32_t elementid
)
2230 return av_strcasecmp(name
, "title") &&
2231 av_strcasecmp(name
, "stereo_mode") &&
2232 av_strcasecmp(name
, "creation_time") &&
2233 av_strcasecmp(name
, "encoding_tool") &&
2234 av_strcasecmp(name
, "duration") &&
2235 (elementid
!= MATROSKA_ID_TAGTARGETS_TRACKUID
||
2236 av_strcasecmp(name
, "language")) &&
2237 (elementid
!= MATROSKA_ID_TAGTARGETS_ATTACHUID
||
2238 (av_strcasecmp(name
, "filename") &&
2239 av_strcasecmp(name
, "mimetype")));
2242 static int mkv_write_tag(MatroskaMuxContext
*mkv
, const AVDictionary
*m
,
2243 AVIOContext
**pb
, unsigned reserved_size
,
2244 uint32_t elementid
, uint64_t uid
)
2246 const AVDictionaryEntry
*t
= NULL
;
2247 AVIOContext
*const tmp_bc
= mkv
->tmp_bc
;
2249 int ret
= 0, size
, tag_written
= 0;
2251 mkv_write_tag_targets(mkv
, tmp_bc
, elementid
, uid
);
2253 while ((t
= av_dict_iterate(m
, t
))) {
2254 if (mkv_check_tag_name(t
->key
, elementid
)) {
2255 ret
= mkv_write_simpletag(tmp_bc
, t
);
2262 put_ebml_void(tmp_bc
, reserved_size
);
2263 else if (!tag_written
)
2266 size
= avio_get_dyn_buf(tmp_bc
, &buf
);
2267 if (tmp_bc
->error
) {
2268 ret
= tmp_bc
->error
;
2272 ret
= start_ebml_master_crc32(pb
, mkv
);
2276 put_ebml_binary(*pb
, MATROSKA_ID_TAG
, buf
, size
);
2279 ffio_reset_dyn_buf(tmp_bc
);
2283 static int mkv_write_tags(AVFormatContext
*s
)
2285 MatroskaMuxContext
*mkv
= s
->priv_data
;
2286 int i
, ret
, seekable
= IS_SEEKABLE(s
->pb
, mkv
);
2288 mkv
->wrote_tags
= 1;
2290 ff_metadata_conv_ctx(s
, ff_mkv_metadata_conv
, NULL
);
2292 ret
= mkv_write_tag(mkv
, s
->metadata
, &mkv
->tags
.bc
, 0, 0, 0);
2296 for (i
= 0; i
< s
->nb_streams
; i
++) {
2297 const AVStream
*st
= s
->streams
[i
];
2298 mkv_track
*track
= &mkv
->tracks
[i
];
2300 if (st
->codecpar
->codec_type
== AVMEDIA_TYPE_ATTACHMENT
)
2303 ret
= mkv_write_tag(mkv
, st
->metadata
, &mkv
->tags
.bc
,
2304 seekable
? DURATION_SIMPLETAG_SIZE
: 0,
2305 MATROSKA_ID_TAGTARGETS_TRACKUID
, track
->uid
);
2309 track
->duration_offset
= avio_tell(mkv
->tags
.bc
) - DURATION_SIMPLETAG_SIZE
;
2312 if (mkv
->nb_attachments
&& !IS_WEBM(mkv
)) {
2313 for (i
= 0; i
< s
->nb_streams
; i
++) {
2314 const mkv_track
*track
= &mkv
->tracks
[i
];
2315 const AVStream
*st
= s
->streams
[i
];
2317 if (st
->codecpar
->codec_type
!= AVMEDIA_TYPE_ATTACHMENT
)
2320 ret
= mkv_write_tag(mkv
, st
->metadata
, &mkv
->tags
.bc
, 0,
2321 MATROSKA_ID_TAGTARGETS_ATTACHUID
, track
->uid
);
2328 return end_ebml_master_crc32_tentatively(s
->pb
, &mkv
->tags
, mkv
,
2334 static int mkv_new_chapter_ids_needed(const AVFormatContext
*s
)
2336 for (unsigned i
= 0; i
< s
->nb_chapters
; i
++) {
2337 if (!s
->chapters
[i
]->id
)
2339 for (unsigned j
= 0; j
< i
; j
++)
2340 if (s
->chapters
[j
]->id
== s
->chapters
[i
]->id
)
2346 static int mkv_write_chapters(AVFormatContext
*s
)
2348 MatroskaMuxContext
*mkv
= s
->priv_data
;
2349 AVIOContext
*dyn_cp
= NULL
, *dyn_tags
= NULL
, **tags
, *pb
= s
->pb
;
2350 ebml_master editionentry
;
2351 AVRational scale
= {1, 1E9
};
2352 int ret
, create_new_ids
;
2354 if (!s
->nb_chapters
|| mkv
->wrote_chapters
)
2357 ret
= start_ebml_master_crc32(&dyn_cp
, mkv
);
2361 editionentry
= start_ebml_master(dyn_cp
, MATROSKA_ID_EDITIONENTRY
, 0);
2362 if (!IS_WEBM(mkv
)) {
2363 put_ebml_uint(dyn_cp
, MATROSKA_ID_EDITIONFLAGDEFAULT
, 1);
2364 /* If mkv_write_tags() has already been called, then any tags
2365 * corresponding to chapters will be put into a new Tags element. */
2366 tags
= mkv
->wrote_tags
? &dyn_tags
: &mkv
->tags
.bc
;
2370 create_new_ids
= mkv_new_chapter_ids_needed(s
);
2372 for (unsigned i
= 0; i
< s
->nb_chapters
; i
++) {
2373 AVChapter
*const c
= s
->chapters
[i
];
2374 int64_t chapterstart
= av_rescale_q(c
->start
, c
->time_base
, scale
);
2375 int64_t chapterend
= av_rescale_q(c
->end
, c
->time_base
, scale
);
2376 const AVDictionaryEntry
*t
;
2377 uint64_t uid
= create_new_ids
? i
+ 1ULL : c
->id
;
2380 if (chapterstart
< 0 || chapterstart
> chapterend
|| chapterend
< 0) {
2381 av_log(s
, AV_LOG_ERROR
,
2382 "Invalid chapter start (%"PRId64
") or end (%"PRId64
").\n",
2383 chapterstart
, chapterend
);
2384 ret
= AVERROR_INVALIDDATA
;
2388 ebml_writer_open_master(&writer
, MATROSKA_ID_CHAPTERATOM
);
2389 ebml_writer_add_uint(&writer
, MATROSKA_ID_CHAPTERUID
, uid
);
2390 ebml_writer_add_uint(&writer
, MATROSKA_ID_CHAPTERTIMESTART
, chapterstart
);
2391 ebml_writer_add_uint(&writer
, MATROSKA_ID_CHAPTERTIMEEND
, chapterend
);
2392 if ((t
= av_dict_get(c
->metadata
, "title", NULL
, 0))) {
2393 ebml_writer_open_master(&writer
, MATROSKA_ID_CHAPTERDISPLAY
);
2394 ebml_writer_add_string(&writer
, MATROSKA_ID_CHAPSTRING
, t
->value
);
2395 ebml_writer_add_string(&writer
, MATROSKA_ID_CHAPLANG
, "und");
2397 ret
= ebml_writer_write(&writer
, dyn_cp
);
2402 ff_metadata_conv(&c
->metadata
, ff_mkv_metadata_conv
, NULL
);
2404 ret
= mkv_write_tag(mkv
, c
->metadata
, tags
, 0,
2405 MATROSKA_ID_TAGTARGETS_CHAPTERUID
, uid
);
2410 end_ebml_master(dyn_cp
, editionentry
);
2411 mkv
->wrote_chapters
= 1;
2413 ret
= end_ebml_master_crc32(pb
, &dyn_cp
, mkv
, MATROSKA_ID_CHAPTERS
, 0, 0, 1);
2417 return end_ebml_master_crc32(pb
, &dyn_tags
, mkv
,
2418 MATROSKA_ID_TAGS
, 0, 0, 1);
2423 /* tags == &mkv->tags.bc can only happen if mkv->tags.bc was
2424 * initially NULL, so we never free older tags. */
2425 ffio_free_dyn_buf(tags
);
2427 ffio_free_dyn_buf(&dyn_cp
);
2431 static const char *get_mimetype(const AVStream
*st
)
2433 const AVDictionaryEntry
*t
;
2435 if (t
= av_dict_get(st
->metadata
, "mimetype", NULL
, 0))
2437 if (st
->codecpar
->codec_id
!= AV_CODEC_ID_NONE
) {
2438 const AVCodecDescriptor
*desc
= avcodec_descriptor_get(st
->codecpar
->codec_id
);
2439 if (desc
&& desc
->mime_types
) {
2440 return desc
->mime_types
[0];
2441 } else if (st
->codecpar
->codec_id
== AV_CODEC_ID_TEXT
)
2442 return "text/plain";
2448 static int mkv_write_attachments(AVFormatContext
*s
)
2450 MatroskaMuxContext
*mkv
= s
->priv_data
;
2451 AVIOContext
*dyn_cp
= NULL
, *pb
= s
->pb
;
2454 if (!mkv
->nb_attachments
)
2457 ret
= start_ebml_master_crc32(&dyn_cp
, mkv
);
2461 for (i
= 0; i
< s
->nb_streams
; i
++) {
2462 const AVStream
*st
= s
->streams
[i
];
2463 mkv_track
*track
= &mkv
->tracks
[i
];
2465 const AVDictionaryEntry
*t
;
2466 const char *mimetype
;
2468 if (st
->codecpar
->codec_type
!= AVMEDIA_TYPE_ATTACHMENT
)
2471 ebml_writer_open_master(&writer
, MATROSKA_ID_ATTACHEDFILE
);
2473 if (t
= av_dict_get(st
->metadata
, "title", NULL
, 0))
2474 ebml_writer_add_string(&writer
, MATROSKA_ID_FILEDESC
, t
->value
);
2475 if (!(t
= av_dict_get(st
->metadata
, "filename", NULL
, 0))) {
2476 av_log(s
, AV_LOG_ERROR
, "Attachment stream %d has no filename tag.\n", i
);
2477 ffio_free_dyn_buf(&dyn_cp
);
2478 return AVERROR(EINVAL
);
2480 ebml_writer_add_string(&writer
, MATROSKA_ID_FILENAME
, t
->value
);
2482 mimetype
= get_mimetype(st
);
2483 av_assert0(mimetype
);
2484 ebml_writer_add_string(&writer
, MATROSKA_ID_FILEMIMETYPE
, mimetype
);
2485 ebml_writer_add_bin(&writer
, MATROSKA_ID_FILEDATA
,
2486 st
->codecpar
->extradata
, st
->codecpar
->extradata_size
);
2487 ebml_writer_add_uid(&writer
, MATROSKA_ID_FILEUID
, track
->uid
);
2488 ret
= ebml_writer_write(&writer
, dyn_cp
);
2490 ffio_free_dyn_buf(&dyn_cp
);
2494 return end_ebml_master_crc32(pb
, &dyn_cp
, mkv
,
2495 MATROSKA_ID_ATTACHMENTS
, 0, 0, 1);
2498 static int64_t get_metadata_duration(AVFormatContext
*s
)
2500 const AVDictionaryEntry
*duration
= av_dict_get(s
->metadata
, "DURATION",
2505 if (duration
&& (av_parse_time(&us
, duration
->value
, 1) == 0) && us
> 0) {
2506 av_log(s
, AV_LOG_DEBUG
, "get_metadata_duration found duration in context metadata: %" PRId64
"\n", us
);
2510 for (unsigned i
= 0; i
< s
->nb_streams
; i
++) {
2512 duration
= av_dict_get(s
->streams
[i
]->metadata
, "DURATION", NULL
, 0);
2514 if (duration
&& (av_parse_time(&us
, duration
->value
, 1) == 0))
2515 max
= FFMAX(max
, us
);
2518 av_log(s
, AV_LOG_DEBUG
, "get_metadata_duration returned: %" PRId64
"\n", max
);
2522 static void ebml_write_header(AVIOContext
*pb
,
2523 const char *doctype
, int version
)
2526 ebml_writer_open_master(&writer
, EBML_ID_HEADER
);
2527 ebml_writer_add_uint (&writer
, EBML_ID_EBMLVERSION
, 1);
2528 ebml_writer_add_uint (&writer
, EBML_ID_EBMLREADVERSION
, 1);
2529 ebml_writer_add_uint (&writer
, EBML_ID_EBMLMAXIDLENGTH
, 4);
2530 ebml_writer_add_uint (&writer
, EBML_ID_EBMLMAXSIZELENGTH
, 8);
2531 ebml_writer_add_string(&writer
, EBML_ID_DOCTYPE
, doctype
);
2532 ebml_writer_add_uint (&writer
, EBML_ID_DOCTYPEVERSION
, version
);
2533 ebml_writer_add_uint (&writer
, EBML_ID_DOCTYPEREADVERSION
, 2);
2534 /* The size is bounded, so no need to check this. */
2535 ebml_writer_write(&writer
, pb
);
2538 static int mkv_write_info(AVFormatContext
*s
)
2540 MatroskaMuxContext
*mkv
= s
->priv_data
;
2541 const AVDictionaryEntry
*tag
;
2542 int64_t creation_time
;
2544 int ret
= start_ebml_master_crc32(&mkv
->info
.bc
, mkv
);
2549 put_ebml_uint(pb
, MATROSKA_ID_TIMECODESCALE
, 1000000);
2550 if ((tag
= av_dict_get(s
->metadata
, "title", NULL
, 0)))
2551 put_ebml_string(pb
, MATROSKA_ID_TITLE
, tag
->value
);
2552 if (!(s
->flags
& AVFMT_FLAG_BITEXACT
)) {
2553 put_ebml_string(pb
, MATROSKA_ID_MUXINGAPP
, LIBAVFORMAT_IDENT
);
2554 if ((tag
= av_dict_get(s
->metadata
, "encoding_tool", NULL
, 0)))
2555 put_ebml_string(pb
, MATROSKA_ID_WRITINGAPP
, tag
->value
);
2557 put_ebml_string(pb
, MATROSKA_ID_WRITINGAPP
, LIBAVFORMAT_IDENT
);
2560 put_ebml_binary(pb
, MATROSKA_ID_SEGMENTUID
, mkv
->segment_uid
, 16);
2562 const char *ident
= "Lavf";
2563 put_ebml_string(pb
, MATROSKA_ID_MUXINGAPP
, ident
);
2564 put_ebml_string(pb
, MATROSKA_ID_WRITINGAPP
, ident
);
2567 if (ff_parse_creation_time_metadata(s
, &creation_time
, 0) > 0) {
2568 // Adjust time so it's relative to 2001-01-01 and convert to nanoseconds.
2569 int64_t date_utc
= (creation_time
- 978307200000000LL) * 1000;
2570 uint8_t date_utc_buf
[8];
2571 AV_WB64(date_utc_buf
, date_utc
);
2572 put_ebml_binary(pb
, MATROSKA_ID_DATEUTC
, date_utc_buf
, 8);
2575 // reserve space for the duration
2577 mkv
->duration_offset
= avio_tell(pb
);
2578 if (!mkv
->is_live
) {
2579 int64_t metadata_duration
= get_metadata_duration(s
);
2581 if (s
->duration
> 0) {
2582 int64_t scaledDuration
= av_rescale(s
->duration
, 1000, AV_TIME_BASE
);
2583 put_ebml_float(pb
, MATROSKA_ID_DURATION
, scaledDuration
);
2584 av_log(s
, AV_LOG_DEBUG
, "Write early duration from recording time = %" PRIu64
"\n", scaledDuration
);
2585 } else if (metadata_duration
> 0) {
2586 int64_t scaledDuration
= av_rescale(metadata_duration
, 1000, AV_TIME_BASE
);
2587 put_ebml_float(pb
, MATROSKA_ID_DURATION
, scaledDuration
);
2588 av_log(s
, AV_LOG_DEBUG
, "Write early duration from metadata = %" PRIu64
"\n", scaledDuration
);
2589 } else if (s
->pb
->seekable
& AVIO_SEEKABLE_NORMAL
) {
2590 put_ebml_void(pb
, 11); // assumes double-precision float to be written
2593 return end_ebml_master_crc32_tentatively(s
->pb
, &mkv
->info
,
2594 mkv
, MATROSKA_ID_INFO
);
2597 static int mkv_write_header(AVFormatContext
*s
)
2599 MatroskaMuxContext
*mkv
= s
->priv_data
;
2600 AVIOContext
*pb
= s
->pb
;
2601 int ret
, version
= 2;
2603 ret
= avio_open_dyn_buf(&mkv
->tmp_bc
);
2607 if (!IS_WEBM(mkv
) ||
2608 av_dict_get(s
->metadata
, "stereo_mode", NULL
, 0) ||
2609 av_dict_get(s
->metadata
, "alpha_mode", NULL
, 0))
2612 for (unsigned i
= 0; i
< s
->nb_streams
; i
++) {
2613 if (s
->streams
[i
]->codecpar
->codec_id
== AV_CODEC_ID_OPUS
||
2614 av_dict_get(s
->streams
[i
]->metadata
, "stereo_mode", NULL
, 0) ||
2615 av_dict_get(s
->streams
[i
]->metadata
, "alpha_mode", NULL
, 0))
2619 ebml_write_header(pb
, s
->oformat
->name
, version
);
2620 put_ebml_id(pb
, MATROSKA_ID_SEGMENT
);
2621 put_ebml_size_unknown(pb
, 8);
2622 mkv
->segment_offset
= avio_tell(pb
);
2624 // We write a SeekHead at the beginning to point to all other level
2625 // one elements (except Clusters).
2626 mkv_start_seekhead(mkv
, pb
);
2628 ret
= mkv_write_info(s
);
2632 ret
= mkv_write_tracks(s
);
2636 ret
= mkv_write_chapters(s
);
2640 if (!IS_WEBM(mkv
)) {
2641 ret
= mkv_write_attachments(s
);
2646 /* Must come after mkv_write_chapters() to write chapter tags
2647 * into the same Tags element as the other tags. */
2648 ret
= mkv_write_tags(s
);
2652 if (!IS_SEEKABLE(pb
, mkv
)) {
2653 ret
= mkv_write_seekhead(pb
, mkv
, 0, avio_tell(pb
));
2658 if (s
->metadata_header_padding
> 0) {
2659 if (s
->metadata_header_padding
== 1)
2660 s
->metadata_header_padding
++;
2661 put_ebml_void(pb
, s
->metadata_header_padding
);
2664 if (mkv
->reserve_cues_space
|| mkv
->move_cues_to_front
) {
2665 if (IS_SEEKABLE(pb
, mkv
)) {
2666 mkv
->cues_pos
= avio_tell(pb
);
2667 if (mkv
->reserve_cues_space
>= 1) {
2668 if (mkv
->reserve_cues_space
== 1)
2669 mkv
->reserve_cues_space
++;
2670 put_ebml_void(pb
, mkv
->reserve_cues_space
);
2673 mkv
->reserve_cues_space
= -1;
2676 mkv
->cluster_pos
= -1;
2678 // start a new cluster every 5 MB or 5 sec, or 32k / 1 sec for streaming or
2679 // after 4k and on a keyframe
2680 if (IS_SEEKABLE(pb
, mkv
)) {
2681 if (mkv
->cluster_time_limit
< 0)
2682 mkv
->cluster_time_limit
= 5000;
2683 if (mkv
->cluster_size_limit
< 0)
2684 mkv
->cluster_size_limit
= 5 * 1024 * 1024;
2686 if (mkv
->cluster_time_limit
< 0)
2687 mkv
->cluster_time_limit
= 1000;
2688 if (mkv
->cluster_size_limit
< 0)
2689 mkv
->cluster_size_limit
= 32 * 1024;
2695 #if CONFIG_MATROSKA_MUXER
2696 static int mkv_reformat_h2645(MatroskaMuxContext
*mkv
, AVIOContext
*pb
,
2697 const AVPacket
*pkt
, int *size
)
2701 ff_nal_units_write_list(&mkv
->cur_block
.h2645_nalu_list
, pb
, pkt
->data
);
2703 ret
= ff_nal_units_create_list(&mkv
->cur_block
.h2645_nalu_list
, pkt
->data
, pkt
->size
);
2711 static int mkv_reformat_wavpack(MatroskaMuxContext
*mkv
, AVIOContext
*pb
,
2712 const AVPacket
*pkt
, int *size
)
2714 const uint8_t *src
= pkt
->data
;
2715 int srclen
= pkt
->size
;
2719 while (srclen
>= WV_HEADER_SIZE
) {
2722 ret
= ff_wv_parse_header(&header
, src
);
2725 src
+= WV_HEADER_SIZE
;
2726 srclen
-= WV_HEADER_SIZE
;
2728 if (srclen
< header
.blocksize
)
2729 return AVERROR_INVALIDDATA
;
2731 offset
+= 4 * !!header
.initial
+ 8 + 4 * !(header
.initial
&& header
.final
);
2734 avio_wl32(pb
, header
.samples
);
2735 avio_wl32(pb
, header
.flags
);
2736 avio_wl32(pb
, header
.crc
);
2738 if (!(header
.initial
&& header
.final
))
2739 avio_wl32(pb
, header
.blocksize
);
2741 avio_write(pb
, src
, header
.blocksize
);
2743 src
+= header
.blocksize
;
2744 srclen
-= header
.blocksize
;
2745 offset
+= header
.blocksize
;
2753 static int mkv_reformat_av1(MatroskaMuxContext
*mkv
, AVIOContext
*pb
,
2754 const AVPacket
*pkt
, int *size
)
2756 int ret
= ff_av1_filter_obus(pb
, pkt
->data
, pkt
->size
);
2763 static int webm_reformat_vtt(MatroskaMuxContext
*mkv
, AVIOContext
*pb
,
2764 const AVPacket
*pkt
, int *size
)
2766 const uint8_t *id
, *settings
;
2767 size_t id_size
, settings_size
;
2768 unsigned total
= pkt
->size
+ 2U;
2770 if (total
> INT_MAX
)
2771 return AVERROR(ERANGE
);
2773 id
= av_packet_get_side_data(pkt
, AV_PKT_DATA_WEBVTT_IDENTIFIER
,
2775 settings
= av_packet_get_side_data(pkt
, AV_PKT_DATA_WEBVTT_SETTINGS
,
2777 if (id_size
> INT_MAX
- total
|| settings_size
> INT_MAX
- (total
+= id_size
))
2778 return AVERROR(ERANGE
);
2779 *size
= total
+= settings_size
;
2781 avio_write(pb
, id
, id_size
);
2783 avio_write(pb
, settings
, settings_size
);
2785 avio_write(pb
, pkt
->data
, pkt
->size
);
2790 static void mkv_write_blockadditional(EbmlWriter
*writer
, const uint8_t *buf
,
2791 size_t size
, uint64_t additional_id
)
2793 ebml_writer_open_master(writer
, MATROSKA_ID_BLOCKMORE
);
2794 ebml_writer_add_uint(writer
, MATROSKA_ID_BLOCKADDID
, additional_id
);
2795 ebml_writer_add_bin (writer
, MATROSKA_ID_BLOCKADDITIONAL
, buf
, size
);
2796 ebml_writer_close_master(writer
);
2799 static int mkv_write_block(void *logctx
, MatroskaMuxContext
*mkv
,
2800 AVIOContext
*pb
, const AVCodecParameters
*par
,
2801 mkv_track
*track
, const AVPacket
*pkt
,
2802 int keyframe
, int64_t ts
, uint64_t duration
,
2803 int force_blockgroup
, int64_t relative_packet_pos
)
2805 uint8_t t35_buf
[6 + AV_HDR_PLUS_MAX_PAYLOAD_SIZE
];
2807 size_t side_data_size
;
2808 uint64_t additional_id
;
2809 unsigned track_number
= track
->track_num
;
2813 mkv
->cur_block
.track
= track
;
2814 mkv
->cur_block
.pkt
= pkt
;
2815 mkv
->cur_block
.rel_ts
= ts
- mkv
->cluster_pts
;
2816 mkv
->cur_block
.flags
= 0;
2818 /* Open a BlockGroup with a Block now; it will later be converted
2819 * to a SimpleBlock if possible. */
2820 ebml_writer_open_master(&writer
, MATROSKA_ID_BLOCKGROUP
);
2821 ebml_writer_add_block(&writer
, mkv
);
2823 if (duration
> 0 && (par
->codec_type
== AVMEDIA_TYPE_SUBTITLE
||
2824 /* If the packet's duration is inconsistent with the default duration,
2825 * add an explicit duration element. */
2826 track
->default_duration_high
> 0 &&
2827 duration
!= track
->default_duration_high
&&
2828 duration
!= track
->default_duration_low
))
2829 ebml_writer_add_uint(&writer
, MATROSKA_ID_BLOCKDURATION
, duration
);
2831 av_log(logctx
, AV_LOG_DEBUG
,
2832 "Writing block of size %d with pts %" PRId64
", dts %" PRId64
", "
2833 "duration %" PRId64
" at relative offset %" PRId64
" in cluster "
2834 "at offset %" PRId64
". TrackNumber %u, keyframe %d\n",
2835 pkt
->size
, pkt
->pts
, pkt
->dts
, pkt
->duration
, relative_packet_pos
,
2836 mkv
->cluster_pos
, track_number
, keyframe
!= 0);
2838 side_data
= av_packet_get_side_data(pkt
,
2839 AV_PKT_DATA_SKIP_SAMPLES
,
2841 if (side_data
&& side_data_size
>= 10) {
2842 int64_t discard_padding
= AV_RL32(side_data
+ 4);
2843 if (discard_padding
) {
2844 discard_padding
= av_rescale_q(discard_padding
,
2845 (AVRational
){1, par
->sample_rate
},
2846 (AVRational
){1, 1000000000});
2847 ebml_writer_add_sint(&writer
, MATROSKA_ID_DISCARDPADDING
, discard_padding
);
2851 ebml_writer_open_master(&writer
, MATROSKA_ID_BLOCKADDITIONS
);
2852 side_data
= av_packet_get_side_data(pkt
,
2853 AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL
,
2855 if (side_data
&& side_data_size
>= 8 &&
2856 // Only the Codec-specific BlockMore (id == 1) is currently supported.
2857 (additional_id
= AV_RB64(side_data
)) == MATROSKA_BLOCK_ADD_ID_TYPE_OPAQUE
) {
2858 mkv_write_blockadditional(&writer
, side_data
+ 8, side_data_size
- 8,
2860 track
->max_blockaddid
= FFMAX(track
->max_blockaddid
, additional_id
);
2863 if (par
->codec_type
== AVMEDIA_TYPE_VIDEO
) {
2864 side_data
= av_packet_get_side_data(pkt
,
2865 AV_PKT_DATA_DYNAMIC_HDR10_PLUS
,
2867 if (side_data
&& side_data_size
) {
2868 uint8_t *payload
= t35_buf
;
2869 size_t payload_size
= sizeof(t35_buf
) - 6;
2871 bytestream_put_byte(&payload
, ITU_T_T35_COUNTRY_CODE_US
);
2872 bytestream_put_be16(&payload
, ITU_T_T35_PROVIDER_CODE_SAMSUNG
);
2873 bytestream_put_be16(&payload
, 0x01); // provider_oriented_code
2874 bytestream_put_byte(&payload
, 0x04); // application_identifier
2876 ret
= av_dynamic_hdr_plus_to_t35((AVDynamicHDRPlus
*)side_data
, &payload
,
2881 mkv_write_blockadditional(&writer
, t35_buf
, payload_size
+ 6,
2882 MATROSKA_BLOCK_ADD_ID_ITU_T_T35
);
2883 track
->max_blockaddid
= FFMAX(track
->max_blockaddid
,
2884 MATROSKA_BLOCK_ADD_ID_ITU_T_T35
);
2888 ebml_writer_close_or_discard_master(&writer
);
2890 if (!force_blockgroup
&& writer
.nb_elements
== 2) {
2891 /* Nothing except the BlockGroup + Block. Can use a SimpleBlock. */
2892 writer
.elements
++; // Skip the BlockGroup.
2893 writer
.nb_elements
--;
2894 av_assert2(writer
.elements
[0].id
== MATROSKA_ID_BLOCK
);
2895 writer
.elements
[0].id
= MATROSKA_ID_SIMPLEBLOCK
;
2897 mkv
->cur_block
.flags
|= 1 << 7;
2898 } else if (!keyframe
)
2899 ebml_writer_add_sint(&writer
, MATROSKA_ID_BLOCKREFERENCE
,
2900 track
->last_timestamp
- ts
);
2902 return ebml_writer_write(&writer
, pb
);
2905 static int mkv_end_cluster(AVFormatContext
*s
)
2907 MatroskaMuxContext
*mkv
= s
->priv_data
;
2910 if (!mkv
->have_video
) {
2911 for (unsigned i
= 0; i
< s
->nb_streams
; i
++)
2912 mkv
->tracks
[i
].has_cue
= 0;
2914 mkv
->cluster_pos
= -1;
2915 ret
= end_ebml_master_crc32(s
->pb
, &mkv
->cluster_bc
, mkv
,
2916 MATROSKA_ID_CLUSTER
, 0, 1, 0);
2920 avio_write_marker(s
->pb
, AV_NOPTS_VALUE
, AVIO_DATA_MARKER_FLUSH_POINT
);
2924 static int mkv_check_new_extra_data(AVFormatContext
*s
, const AVPacket
*pkt
)
2926 MatroskaMuxContext
*mkv
= s
->priv_data
;
2927 mkv_track
*track
= &mkv
->tracks
[pkt
->stream_index
];
2928 AVCodecParameters
*par
= s
->streams
[pkt
->stream_index
]->codecpar
;
2930 size_t side_data_size
;
2933 side_data
= av_packet_get_side_data(pkt
, AV_PKT_DATA_NEW_EXTRADATA
,
2936 switch (par
->codec_id
) {
2937 #if CONFIG_MATROSKA_MUXER
2938 case AV_CODEC_ID_AAC
:
2939 if (side_data_size
&& mkv
->track
.bc
) {
2940 int output_sample_rate
= 0;
2941 ret
= get_aac_sample_rates(s
, mkv
, side_data
, side_data_size
,
2942 &track
->sample_rate
, &output_sample_rate
);
2945 if (!output_sample_rate
)
2946 output_sample_rate
= track
->sample_rate
; // Space is already reserved, so it's this or a void element.
2947 ret
= mkv_update_codecprivate(s
, mkv
, side_data
, side_data_size
,
2948 par
, mkv
->track
.bc
, track
, 0);
2951 avio_seek(mkv
->track
.bc
, track
->sample_rate_offset
, SEEK_SET
);
2952 put_ebml_float(mkv
->track
.bc
, MATROSKA_ID_AUDIOSAMPLINGFREQ
, track
->sample_rate
);
2953 put_ebml_float(mkv
->track
.bc
, MATROSKA_ID_AUDIOOUTSAMPLINGFREQ
, output_sample_rate
);
2954 } else if (!par
->extradata_size
&& !track
->sample_rate
) {
2955 // No extradata (codecpar or packet side data).
2956 av_log(s
, AV_LOG_ERROR
, "Error parsing AAC extradata, unable to determine samplerate.\n");
2957 return AVERROR(EINVAL
);
2960 case AV_CODEC_ID_FLAC
:
2961 if (side_data_size
&& mkv
->track
.bc
) {
2962 if (side_data_size
!= par
->extradata_size
) {
2963 av_log(s
, AV_LOG_ERROR
, "Invalid FLAC STREAMINFO metadata for output stream %d\n",
2965 return AVERROR(EINVAL
);
2967 ret
= mkv_update_codecprivate(s
, mkv
, side_data
, side_data_size
,
2968 par
, mkv
->track
.bc
, track
, 0);
2974 // FIXME: Remove the following once libaom starts propagating proper extradata during init()
2975 // See https://bugs.chromium.org/p/aomedia/issues/detail?id=2208
2976 case AV_CODEC_ID_AV1
:
2977 if (side_data_size
&& mkv
->track
.bc
&& !par
->extradata_size
) {
2978 // If the reserved space doesn't suffice, only write
2979 // the first four bytes of the av1C.
2980 ret
= mkv_update_codecprivate(s
, mkv
, side_data
, side_data_size
,
2981 par
, mkv
->track
.bc
, track
, 4);
2984 } else if (!par
->extradata_size
)
2985 return AVERROR_INVALIDDATA
;
2989 av_log(s
, AV_LOG_DEBUG
, "Ignoring new extradata in a packet for stream %d.\n", pkt
->stream_index
);
2996 static int mkv_write_packet_internal(AVFormatContext
*s
, const AVPacket
*pkt
)
2998 MatroskaMuxContext
*mkv
= s
->priv_data
;
3000 AVCodecParameters
*par
= s
->streams
[pkt
->stream_index
]->codecpar
;
3001 mkv_track
*track
= &mkv
->tracks
[pkt
->stream_index
];
3002 int is_sub
= par
->codec_type
== AVMEDIA_TYPE_SUBTITLE
;
3003 /* All subtitle blocks are considered to be keyframes. */
3004 int keyframe
= is_sub
|| !!(pkt
->flags
& AV_PKT_FLAG_KEY
);
3005 int64_t duration
= FFMAX(pkt
->duration
, 0);
3006 int64_t cue_duration
= is_sub
? duration
: 0;
3008 int64_t ts
= track
->write_dts
? pkt
->dts
: pkt
->pts
;
3009 int64_t relative_packet_pos
;
3011 if (ts
== AV_NOPTS_VALUE
) {
3012 av_log(s
, AV_LOG_ERROR
, "Can't write packet with unknown timestamp\n");
3013 return AVERROR(EINVAL
);
3015 ts
+= track
->ts_offset
;
3017 if (mkv
->cluster_pos
!= -1) {
3018 int64_t cluster_time
= ts
- mkv
->cluster_pts
;
3019 if ((int16_t)cluster_time
!= cluster_time
) {
3020 ret
= mkv_end_cluster(s
);
3023 av_log(s
, AV_LOG_WARNING
, "Starting new cluster due to timestamp\n");
3027 if (mkv
->cluster_pos
== -1) {
3028 ret
= start_ebml_master_crc32(&mkv
->cluster_bc
, mkv
);
3031 mkv
->cluster_bc
->direct
= 1;
3032 mkv
->cluster_pos
= avio_tell(s
->pb
);
3033 put_ebml_uint(mkv
->cluster_bc
, MATROSKA_ID_CLUSTERTIMECODE
, FFMAX(0, ts
));
3034 mkv
->cluster_pts
= FFMAX(0, ts
);
3035 av_log(s
, AV_LOG_DEBUG
,
3036 "Starting new cluster with timestamp "
3037 "%" PRId64
" at offset %" PRId64
" bytes\n",
3038 mkv
->cluster_pts
, mkv
->cluster_pos
);
3040 pb
= mkv
->cluster_bc
;
3042 relative_packet_pos
= avio_tell(pb
);
3044 /* The WebM spec requires WebVTT to be muxed in BlockGroups;
3045 * so we force it even for packets without duration. */
3046 ret
= mkv_write_block(s
, mkv
, pb
, par
, track
, pkt
,
3047 keyframe
, ts
, duration
,
3048 par
->codec_id
== AV_CODEC_ID_WEBVTT
,
3049 relative_packet_pos
);
3052 if (keyframe
&& IS_SEEKABLE(s
->pb
, mkv
) &&
3053 (par
->codec_type
== AVMEDIA_TYPE_VIDEO
||
3054 par
->codec_type
== AVMEDIA_TYPE_SUBTITLE
||
3055 !mkv
->have_video
&& !track
->has_cue
)) {
3056 ret
= mkv_add_cuepoint(mkv
, pkt
->stream_index
, ts
,
3057 mkv
->cluster_pos
, relative_packet_pos
,
3064 track
->last_timestamp
= ts
;
3065 mkv
->duration
= FFMAX(mkv
->duration
, ts
+ duration
);
3066 track
->duration
= FFMAX(track
->duration
, ts
+ duration
);
3071 static int mkv_write_packet(AVFormatContext
*s
, const AVPacket
*pkt
)
3073 MatroskaMuxContext
*mkv
= s
->priv_data
;
3074 int codec_type
= s
->streams
[pkt
->stream_index
]->codecpar
->codec_type
;
3075 int keyframe
= !!(pkt
->flags
& AV_PKT_FLAG_KEY
);
3077 int64_t cluster_time
;
3079 int start_new_cluster
;
3081 ret
= mkv_check_new_extra_data(s
, pkt
);
3085 if (mkv
->cluster_pos
!= -1) {
3086 if (mkv
->tracks
[pkt
->stream_index
].write_dts
)
3087 cluster_time
= pkt
->dts
- mkv
->cluster_pts
;
3089 cluster_time
= pkt
->pts
- mkv
->cluster_pts
;
3090 cluster_time
+= mkv
->tracks
[pkt
->stream_index
].ts_offset
;
3092 cluster_size
= avio_tell(mkv
->cluster_bc
);
3094 if (mkv
->is_dash
&& codec_type
== AVMEDIA_TYPE_VIDEO
) {
3095 // WebM DASH specification states that the first block of
3096 // every Cluster has to be a key frame. So for DASH video,
3097 // we only create a Cluster on seeing key frames.
3098 start_new_cluster
= keyframe
;
3099 } else if (mkv
->is_dash
&& codec_type
== AVMEDIA_TYPE_AUDIO
&&
3100 cluster_time
> mkv
->cluster_time_limit
) {
3101 // For DASH audio, we create a Cluster based on cluster_time_limit.
3102 start_new_cluster
= 1;
3103 } else if (!mkv
->is_dash
&&
3104 (cluster_size
> mkv
->cluster_size_limit
||
3105 cluster_time
> mkv
->cluster_time_limit
||
3106 (codec_type
== AVMEDIA_TYPE_VIDEO
&& keyframe
&&
3107 cluster_size
> 4 * 1024))) {
3108 start_new_cluster
= 1;
3110 start_new_cluster
= 0;
3112 if (start_new_cluster
) {
3113 ret
= mkv_end_cluster(s
);
3119 if (mkv
->cluster_pos
== -1)
3120 avio_write_marker(s
->pb
,
3121 av_rescale_q(pkt
->dts
, s
->streams
[pkt
->stream_index
]->time_base
, AV_TIME_BASE_Q
),
3122 keyframe
&& (mkv
->have_video
? codec_type
== AVMEDIA_TYPE_VIDEO
: 1) ? AVIO_DATA_MARKER_SYNC_POINT
: AVIO_DATA_MARKER_BOUNDARY_POINT
);
3124 // check if we have an audio packet cached
3125 if (mkv
->cur_audio_pkt
->size
> 0) {
3126 ret
= mkv_write_packet_internal(s
, mkv
->cur_audio_pkt
);
3127 av_packet_unref(mkv
->cur_audio_pkt
);
3129 av_log(s
, AV_LOG_ERROR
,
3130 "Could not write cached audio packet ret:%d\n", ret
);
3135 // buffer an audio packet to ensure the packet containing the video
3136 // keyframe's timecode is contained in the same cluster for WebM
3137 if (codec_type
== AVMEDIA_TYPE_AUDIO
) {
3139 ret
= av_packet_ref(mkv
->cur_audio_pkt
, pkt
);
3141 ret
= mkv_write_packet_internal(s
, pkt
);
3145 static int mkv_write_flush_packet(AVFormatContext
*s
, AVPacket
*pkt
)
3147 MatroskaMuxContext
*mkv
= s
->priv_data
;
3150 if (mkv
->cluster_pos
!= -1) {
3151 int ret
= mkv_end_cluster(s
);
3154 av_log(s
, AV_LOG_DEBUG
,
3155 "Flushing cluster at offset %" PRIu64
" bytes\n",
3160 return mkv_write_packet(s
, pkt
);
3163 static int mkv_write_trailer(AVFormatContext
*s
)
3165 MatroskaMuxContext
*mkv
= s
->priv_data
;
3166 AVIOContext
*pb
= s
->pb
;
3167 int64_t endpos
, ret64
;
3170 // check if we have an audio packet cached
3171 if (mkv
->cur_audio_pkt
->size
> 0) {
3172 ret
= mkv_write_packet_internal(s
, mkv
->cur_audio_pkt
);
3174 av_log(s
, AV_LOG_ERROR
,
3175 "Could not write cached audio packet ret:%d\n", ret
);
3180 if (mkv
->cluster_pos
!= -1) {
3181 ret
= end_ebml_master_crc32(pb
, &mkv
->cluster_bc
, mkv
,
3182 MATROSKA_ID_CLUSTER
, 0, 0, 0);
3187 ret
= mkv_write_chapters(s
);
3191 if (!IS_SEEKABLE(pb
, mkv
))
3194 endpos
= avio_tell(pb
);
3196 if (mkv
->cues
.num_entries
&& mkv
->reserve_cues_space
>= 0) {
3197 AVIOContext
*cues
= NULL
;
3198 uint64_t size
, offset
= 0;
3199 int length_size
= 0;
3202 ret
= start_ebml_master_crc32(&cues
, mkv
);
3206 ret
= mkv_assemble_cues(s
->streams
, cues
, mkv
->tmp_bc
, &mkv
->cues
,
3207 mkv
->tracks
, s
->nb_streams
, offset
);
3209 ffio_free_dyn_buf(&cues
);
3213 if (mkv
->reserve_cues_space
|| mkv
->move_cues_to_front
) {
3214 size
= avio_tell(cues
);
3215 length_size
= ebml_length_size(size
);
3216 size
+= 4 + length_size
;
3217 if (offset
+ mkv
->reserve_cues_space
< size
) {
3218 if (mkv
->move_cues_to_front
) {
3219 offset
= size
- mkv
->reserve_cues_space
;
3220 ffio_reset_dyn_buf(cues
);
3223 av_log(s
, AV_LOG_WARNING
,
3224 "Insufficient space reserved for Cues: "
3225 "%d < %"PRIu64
". No Cues will be output.\n",
3226 mkv
->reserve_cues_space
, size
);
3227 ret2
= AVERROR(EINVAL
);
3231 ret
= ff_format_shift_data(s
, mkv
->cues_pos
+ mkv
->reserve_cues_space
,
3234 ffio_free_dyn_buf(&cues
);
3239 if ((ret64
= avio_seek(pb
, mkv
->cues_pos
, SEEK_SET
)) < 0) {
3240 ffio_free_dyn_buf(&cues
);
3243 if (mkv
->reserve_cues_space
== size
+ 1) {
3244 /* There is no way to reserve a single byte because
3245 * the minimal size of an EBML Void element is 2
3246 * (1 byte ID, 1 byte length field). This problem
3247 * is solved by writing the Cues' length field on
3248 * one byte more than necessary. */
3254 ret
= end_ebml_master_crc32(pb
, &cues
, mkv
, MATROSKA_ID_CUES
,
3258 if (mkv
->reserve_cues_space
) {
3259 if (size
< mkv
->reserve_cues_space
)
3260 put_ebml_void(pb
, mkv
->reserve_cues_space
- size
);
3261 } else if (!mkv
->move_cues_to_front
)
3262 endpos
= avio_tell(pb
);
3266 /* Lengths greater than (1ULL << 56) - 1 can't be represented
3267 * via an EBML number, so leave the unknown length field. */
3268 if (endpos
- mkv
->segment_offset
< (1ULL << 56) - 1) {
3269 if ((ret64
= avio_seek(pb
, mkv
->segment_offset
- 8, SEEK_SET
)) < 0)
3271 put_ebml_length(pb
, endpos
- mkv
->segment_offset
, 8);
3274 ret
= mkv_write_seekhead(pb
, mkv
, 1, mkv
->info
.pos
);
3279 // update the duration
3280 av_log(s
, AV_LOG_DEBUG
, "end duration = %" PRIu64
"\n", mkv
->duration
);
3281 avio_seek(mkv
->info
.bc
, mkv
->duration_offset
, SEEK_SET
);
3282 put_ebml_float(mkv
->info
.bc
, MATROSKA_ID_DURATION
, mkv
->duration
);
3283 ret
= end_ebml_master_crc32(pb
, &mkv
->info
.bc
, mkv
,
3284 MATROSKA_ID_INFO
, 0, 0, 0);
3289 if (mkv
->track
.bc
) {
3290 // write Tracks master
3291 if (!IS_WEBM(mkv
)) {
3292 AVIOContext
*track_bc
= mkv
->track
.bc
;
3294 for (unsigned i
= 0; i
< s
->nb_streams
; i
++) {
3295 const mkv_track
*track
= &mkv
->tracks
[i
];
3297 if (!track
->max_blockaddid
)
3300 // We reserved a single byte to write this value.
3301 av_assert0(track
->max_blockaddid
<= 0xFF);
3303 avio_seek(track_bc
, track
->blockadditionmapping_offset
, SEEK_SET
);
3305 put_ebml_uint(track_bc
, MATROSKA_ID_TRACKMAXBLKADDID
,
3306 track
->max_blockaddid
);
3307 if (track
->max_blockaddid
== MATROSKA_BLOCK_ADD_ID_ITU_T_T35
) {
3308 ebml_master mapping_master
= start_ebml_master(track_bc
, MATROSKA_ID_TRACKBLKADDMAPPING
, 8);
3309 put_ebml_uint(track_bc
, MATROSKA_ID_BLKADDIDTYPE
,
3310 MATROSKA_BLOCK_ADD_ID_TYPE_ITU_T_T35
);
3311 put_ebml_uint(track_bc
, MATROSKA_ID_BLKADDIDVALUE
,
3312 MATROSKA_BLOCK_ADD_ID_ITU_T_T35
);
3313 end_ebml_master(track_bc
, mapping_master
);
3318 avio_seek(pb
, mkv
->track
.pos
, SEEK_SET
);
3319 ret
= end_ebml_master_crc32(pb
, &mkv
->track
.bc
, mkv
,
3320 MATROSKA_ID_TRACKS
, 0, 0, 0);
3325 // update stream durations
3327 AVIOContext
*tags_bc
= mkv
->tags
.bc
;
3329 for (i
= 0; i
< s
->nb_streams
; ++i
) {
3330 const AVStream
*st
= s
->streams
[i
];
3331 const mkv_track
*track
= &mkv
->tracks
[i
];
3333 if (track
->duration_offset
> 0) {
3334 double duration_sec
= track
->duration
* av_q2d(st
->time_base
);
3335 char duration_string
[DURATION_STRING_LENGTH
+ 1] = "";
3336 ebml_master simpletag
;
3338 av_log(s
, AV_LOG_DEBUG
, "stream %d end duration = %" PRIu64
"\n", i
,
3341 avio_seek(tags_bc
, track
->duration_offset
, SEEK_SET
);
3342 simpletag
= start_ebml_master(tags_bc
, MATROSKA_ID_SIMPLETAG
,
3344 put_ebml_string(tags_bc
, MATROSKA_ID_TAGNAME
, "DURATION");
3346 snprintf(duration_string
, sizeof(duration_string
), "%02d:%02d:%012.9f",
3347 (int) duration_sec
/ 3600, ((int) duration_sec
/ 60) % 60,
3348 fmod(duration_sec
, 60));
3350 put_ebml_binary(tags_bc
, MATROSKA_ID_TAGSTRING
,
3351 duration_string
, DURATION_STRING_LENGTH
);
3352 end_ebml_master(tags_bc
, simpletag
);
3356 avio_seek(pb
, mkv
->tags
.pos
, SEEK_SET
);
3357 ret
= end_ebml_master_crc32(pb
, &mkv
->tags
.bc
, mkv
,
3358 MATROSKA_ID_TAGS
, 0, 0, 0);
3363 avio_seek(pb
, endpos
, SEEK_SET
);
3368 static uint64_t mkv_get_uid(const mkv_track
*tracks
, int i
, AVLFG
*c
)
3373 uid
= (uint64_t)av_lfg_get(c
) << 32;
3374 uid
|= av_lfg_get(c
);
3377 for (k
= 0; k
< i
; k
++) {
3378 if (tracks
[k
].uid
== uid
)
3386 static int mkv_init(struct AVFormatContext
*s
)
3388 FFFormatContext
*const si
= ffformatcontext(s
);
3389 MatroskaMuxContext
*mkv
= s
->priv_data
;
3391 unsigned nb_tracks
= 0;
3396 for (i
= 0; i
< s
->nb_streams
; i
++) {
3397 if (s
->streams
[i
]->codecpar
->codec_id
== AV_CODEC_ID_ATRAC3
||
3398 s
->streams
[i
]->codecpar
->codec_id
== AV_CODEC_ID_COOK
||
3399 s
->streams
[i
]->codecpar
->codec_id
== AV_CODEC_ID_RA_288
||
3400 s
->streams
[i
]->codecpar
->codec_id
== AV_CODEC_ID_SIPR
||
3401 s
->streams
[i
]->codecpar
->codec_id
== AV_CODEC_ID_RV10
||
3402 s
->streams
[i
]->codecpar
->codec_id
== AV_CODEC_ID_RV20
||
3403 s
->streams
[i
]->codecpar
->codec_id
== AV_CODEC_ID_RV30
) {
3404 av_log(s
, AV_LOG_ERROR
,
3405 "The Matroska muxer does not yet support muxing %s\n",
3406 avcodec_get_name(s
->streams
[i
]->codecpar
->codec_id
));
3407 return AVERROR_PATCHWELCOME
;
3411 if (s
->avoid_negative_ts
< 0) {
3412 s
->avoid_negative_ts
= 1;
3413 si
->avoid_negative_ts_use_pts
= 1;
3416 if (!CONFIG_MATROSKA_MUXER
||
3417 (CONFIG_WEBM_MUXER
&& !strcmp(s
->oformat
->name
, "webm"))) {
3418 mkv
->mode
= MODE_WEBM
;
3421 mkv
->mode
= MODE_MATROSKAv2
;
3423 mkv
->cur_audio_pkt
= ffformatcontext(s
)->pkt
;
3425 mkv
->tracks
= av_calloc(s
->nb_streams
, sizeof(*mkv
->tracks
));
3427 return AVERROR(ENOMEM
);
3429 if (!(s
->flags
& AVFMT_FLAG_BITEXACT
)) {
3430 av_lfg_init(&c
, av_get_random_seed());
3432 // Calculate the SegmentUID now in order not to waste our random seed.
3433 for (i
= 0; i
< 4; i
++)
3434 mkv
->segment_uid
[i
] = av_lfg_get(&c
);
3437 for (i
= 0; i
< s
->nb_streams
; i
++) {
3438 AVStream
*st
= s
->streams
[i
];
3439 const AVCodecParameters
*const par
= st
->codecpar
;
3440 mkv_track
*track
= &mkv
->tracks
[i
];
3442 switch (par
->codec_id
) {
3443 #if CONFIG_MATROSKA_MUXER
3444 case AV_CODEC_ID_WAVPACK
:
3445 track
->reformat
= mkv_reformat_wavpack
;
3447 case AV_CODEC_ID_H264
:
3448 case AV_CODEC_ID_HEVC
:
3449 case AV_CODEC_ID_VVC
:
3450 if (((par
->codec_id
== AV_CODEC_ID_H264
&& par
->extradata_size
> 0) ||
3451 (par
->codec_id
== AV_CODEC_ID_HEVC
&& par
->extradata_size
> 6) ||
3452 (par
->codec_id
== AV_CODEC_ID_VVC
&& par
->extradata_size
>= 6)) &&
3453 (AV_RB24(par
->extradata
) == 1 || AV_RB32(par
->extradata
) == 1))
3454 track
->reformat
= mkv_reformat_h2645
;
3456 case AV_CODEC_ID_PRORES
:
3457 /* Matroska specification requires to remove
3458 * the first QuickTime atom. */
3462 case AV_CODEC_ID_AV1
:
3463 track
->reformat
= mkv_reformat_av1
;
3465 case AV_CODEC_ID_WEBVTT
:
3466 track
->reformat
= webm_reformat_vtt
;
3470 if (s
->flags
& AVFMT_FLAG_BITEXACT
) {
3473 track
->uid
= mkv_get_uid(mkv
->tracks
, i
, &c
);
3476 // ms precision is the de-facto standard timescale for mkv files
3477 avpriv_set_pts_info(st
, 64, 1, 1000);
3479 if (st
->codecpar
->codec_type
== AVMEDIA_TYPE_ATTACHMENT
) {
3481 av_log(s
, AV_LOG_WARNING
, "Stream %d will be ignored "
3482 "as WebM doesn't support attachments.\n", i
);
3483 } else if (!get_mimetype(st
)) {
3484 av_log(s
, AV_LOG_ERROR
, "Attachment stream %d has no mimetype "
3485 "tag and it cannot be deduced from the codec id.\n", i
);
3486 return AVERROR(EINVAL
);
3488 mkv
->nb_attachments
++;
3493 track
->track_num
= mkv
->is_dash
? mkv
->dash_track_number
: nb_tracks
;
3494 track
->track_num_size
= ebml_num_size(track
->track_num
);
3497 if (mkv
->is_dash
&& nb_tracks
!= 1)
3498 return AVERROR(EINVAL
);
3503 static int mkv_check_bitstream(AVFormatContext
*s
, AVStream
*st
,
3504 const AVPacket
*pkt
)
3508 if (CONFIG_MATROSKA_MUXER
&& st
->codecpar
->codec_id
== AV_CODEC_ID_AAC
) {
3509 if (pkt
->size
> 2 && (AV_RB16(pkt
->data
) & 0xfff0) == 0xfff0)
3510 ret
= ff_stream_add_bitstream_filter(st
, "aac_adtstoasc", NULL
);
3511 } else if (st
->codecpar
->codec_id
== AV_CODEC_ID_VP9
) {
3512 ret
= ff_stream_add_bitstream_filter(st
, "vp9_superframe", NULL
);
3513 } else if (CONFIG_MATROSKA_MUXER
&&
3514 st
->codecpar
->codec_id
== AV_CODEC_ID_HDMV_PGS_SUBTITLE
) {
3515 ret
= ff_stream_add_bitstream_filter(st
, "pgs_frame_merge", NULL
);
3521 static const AVCodecTag additional_audio_tags
[] = {
3522 { AV_CODEC_ID_ALAC
, 0XFFFFFFFF },
3523 { AV_CODEC_ID_ATRAC1
, 0xFFFFFFFF },
3524 { AV_CODEC_ID_MLP
, 0xFFFFFFFF },
3525 { AV_CODEC_ID_OPUS
, 0xFFFFFFFF },
3526 { AV_CODEC_ID_PCM_S16BE
, 0xFFFFFFFF },
3527 { AV_CODEC_ID_PCM_S24BE
, 0xFFFFFFFF },
3528 { AV_CODEC_ID_PCM_S32BE
, 0xFFFFFFFF },
3529 { AV_CODEC_ID_QDMC
, 0xFFFFFFFF },
3530 { AV_CODEC_ID_QDM2
, 0xFFFFFFFF },
3531 { AV_CODEC_ID_RA_144
, 0xFFFFFFFF },
3532 { AV_CODEC_ID_TRUEHD
, 0xFFFFFFFF },
3533 { AV_CODEC_ID_NONE
, 0xFFFFFFFF }
3536 static const AVCodecTag additional_subtitle_tags
[] = {
3537 { AV_CODEC_ID_DVB_SUBTITLE
, 0xFFFFFFFF },
3538 { AV_CODEC_ID_DVD_SUBTITLE
, 0xFFFFFFFF },
3539 { AV_CODEC_ID_HDMV_PGS_SUBTITLE
, 0xFFFFFFFF },
3540 { AV_CODEC_ID_ARIB_CAPTION
, 0xFFFFFFFF },
3541 { AV_CODEC_ID_NONE
, 0xFFFFFFFF }
3544 #define OFFSET(x) offsetof(MatroskaMuxContext, x)
3545 #define FLAGS AV_OPT_FLAG_ENCODING_PARAM
3546 static const AVOption options
[] = {
3547 { "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
},
3548 { "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
},
3549 { "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
},
3550 { "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
},
3551 { "dash", "create a WebM file conforming to WebM DASH specification", OFFSET(is_dash
), AV_OPT_TYPE_BOOL
, { .i64
= 0 }, 0, 1, FLAGS
},
3552 { "dash_track_number", "track number for the DASH stream", OFFSET(dash_track_number
), AV_OPT_TYPE_INT
, { .i64
= 1 }, 1, INT_MAX
, FLAGS
},
3553 { "live", "write files assuming it is a live stream", OFFSET(is_live
), AV_OPT_TYPE_BOOL
, { .i64
= 0 }, 0, 1, FLAGS
},
3554 { "allow_raw_vfw", "allow raw VFW mode", OFFSET(allow_raw_vfw
), AV_OPT_TYPE_BOOL
, { .i64
= 0 }, 0, 1, FLAGS
},
3555 { "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
},
3556 { "write_crc32", "write a CRC32 element inside every Level 1 element", OFFSET(write_crc
), AV_OPT_TYPE_BOOL
, { .i64
= 1 }, 0, 1, FLAGS
},
3557 { "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" },
3558 { "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" },
3559 { "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" },
3560 { "passthrough", "use the disposition flag as-is", 0, AV_OPT_TYPE_CONST
, { .i64
= DEFAULT_MODE_PASSTHROUGH
}, 0, 0, FLAGS
, .unit
= "default_mode" },
3564 static const AVClass matroska_webm_class
= {
3565 .class_name
= "matroska/webm muxer",
3566 .item_name
= av_default_item_name
,
3568 .version
= LIBAVUTIL_VERSION_INT
,
3571 #if CONFIG_MATROSKA_MUXER
3572 static int mkv_query_codec(enum AVCodecID codec_id
, int std_compliance
)
3574 for (int i
= 0; ff_mkv_codec_tags
[i
].id
!= AV_CODEC_ID_NONE
; i
++)
3575 if (ff_mkv_codec_tags
[i
].id
== codec_id
)
3578 if (std_compliance
< FF_COMPLIANCE_NORMAL
) {
3579 enum AVMediaType type
= avcodec_get_type(codec_id
);
3580 // mkv theoretically supports any video/audio through VFW/ACM
3581 if (type
== AVMEDIA_TYPE_VIDEO
|| type
== AVMEDIA_TYPE_AUDIO
)
3588 const FFOutputFormat ff_matroska_muxer
= {
3589 .p
.name
= "matroska",
3590 .p
.long_name
= NULL_IF_CONFIG_SMALL("Matroska"),
3591 .p
.mime_type
= "video/x-matroska",
3592 .p
.extensions
= "mkv",
3593 .priv_data_size
= sizeof(MatroskaMuxContext
),
3594 .p
.audio_codec
= CONFIG_LIBVORBIS_ENCODER
?
3595 AV_CODEC_ID_VORBIS
: AV_CODEC_ID_AC3
,
3596 .p
.video_codec
= CONFIG_LIBX264_ENCODER
?
3597 AV_CODEC_ID_H264
: AV_CODEC_ID_MPEG4
,
3599 .deinit
= mkv_deinit
,
3600 .write_header
= mkv_write_header
,
3601 .write_packet
= mkv_write_flush_packet
,
3602 .write_trailer
= mkv_write_trailer
,
3603 .p
.flags
= AVFMT_GLOBALHEADER
| AVFMT_VARIABLE_FPS
|
3605 .p
.codec_tag
= (const AVCodecTag
* const []){
3606 ff_codec_bmp_tags
, ff_codec_wav_tags
,
3607 additional_audio_tags
, additional_subtitle_tags
, 0
3609 .p
.subtitle_codec
= AV_CODEC_ID_ASS
,
3610 .query_codec
= mkv_query_codec
,
3611 .check_bitstream
= mkv_check_bitstream
,
3612 .p
.priv_class
= &matroska_webm_class
,
3613 .flags_internal
= FF_OFMT_FLAG_ALLOW_FLUSH
,
3617 #if CONFIG_WEBM_MUXER
3618 static int webm_query_codec(enum AVCodecID codec_id
, int std_compliance
)
3620 for (int i
= 0; ff_webm_codec_tags
[i
].id
!= AV_CODEC_ID_NONE
; i
++)
3621 if (ff_webm_codec_tags
[i
].id
== codec_id
)
3627 const FFOutputFormat ff_webm_muxer
= {
3629 .p
.long_name
= NULL_IF_CONFIG_SMALL("WebM"),
3630 .p
.mime_type
= "video/webm",
3631 .p
.extensions
= "webm",
3632 .priv_data_size
= sizeof(MatroskaMuxContext
),
3633 .p
.audio_codec
= CONFIG_LIBOPUS_ENCODER
? AV_CODEC_ID_OPUS
: AV_CODEC_ID_VORBIS
,
3634 .p
.video_codec
= CONFIG_LIBVPX_VP9_ENCODER
? AV_CODEC_ID_VP9
: AV_CODEC_ID_VP8
,
3635 .p
.subtitle_codec
= AV_CODEC_ID_WEBVTT
,
3637 .deinit
= mkv_deinit
,
3638 .write_header
= mkv_write_header
,
3639 .write_packet
= mkv_write_flush_packet
,
3640 .write_trailer
= mkv_write_trailer
,
3641 .query_codec
= webm_query_codec
,
3642 .check_bitstream
= mkv_check_bitstream
,
3643 .p
.flags
= AVFMT_GLOBALHEADER
| AVFMT_VARIABLE_FPS
|
3645 .p
.priv_class
= &matroska_webm_class
,
3646 .flags_internal
= FF_OFMT_FLAG_ALLOW_FLUSH
,
3650 #if CONFIG_MATROSKA_AUDIO_MUXER
3651 const FFOutputFormat ff_matroska_audio_muxer
= {
3652 .p
.name
= "matroska",
3653 .p
.long_name
= NULL_IF_CONFIG_SMALL("Matroska Audio"),
3654 .p
.mime_type
= "audio/x-matroska",
3655 .p
.extensions
= "mka",
3656 .priv_data_size
= sizeof(MatroskaMuxContext
),
3657 .p
.audio_codec
= CONFIG_LIBVORBIS_ENCODER
?
3658 AV_CODEC_ID_VORBIS
: AV_CODEC_ID_AC3
,
3659 .p
.video_codec
= AV_CODEC_ID_NONE
,
3661 .deinit
= mkv_deinit
,
3662 .write_header
= mkv_write_header
,
3663 .write_packet
= mkv_write_flush_packet
,
3664 .write_trailer
= mkv_write_trailer
,
3665 .check_bitstream
= mkv_check_bitstream
,
3666 .p
.flags
= AVFMT_GLOBALHEADER
| AVFMT_TS_NONSTRICT
,
3667 .p
.codec_tag
= (const AVCodecTag
* const []){
3668 ff_codec_wav_tags
, additional_audio_tags
, 0
3670 .p
.priv_class
= &matroska_webm_class
,
3671 .flags_internal
= FF_OFMT_FLAG_ALLOW_FLUSH
,