avcodec/x86/h264_idct: Fix ff_h264_luma_dc_dequant_idct_sse2 checkasm failures
[ffmpeg.git] / libavformat / matroskaenc.c
1 /*
2 * Matroska muxer
3 * Copyright (c) 2007 David Conrad
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <stdint.h>
23
24 #include "config_components.h"
25
26 #include "av1.h"
27 #include "avc.h"
28 #include "hevc.h"
29 #include "avformat.h"
30 #include "avio_internal.h"
31 #include "avlanguage.h"
32 #include "dovi_isom.h"
33 #include "flacenc.h"
34 #include "internal.h"
35 #include "isom.h"
36 #include "nal.h"
37 #include "matroska.h"
38 #include "mux.h"
39 #include "riff.h"
40 #include "version.h"
41 #include "vorbiscomment.h"
42 #include "vvc.h"
43 #include "wv.h"
44
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"
63
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"
72
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
76
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)
82
83 #define MODE_MATROSKAv2 0x01
84 #define MODE_WEBM 0x02
85
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) && \
89 !(mkv)->is_live)
90
91 enum {
92 DEFAULT_MODE_INFER,
93 DEFAULT_MODE_INFER_NO_SUBS,
94 DEFAULT_MODE_PASSTHROUGH,
95 };
96
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
100 } ebml_master;
101
102 typedef struct ebml_stored_master {
103 AVIOContext *bc;
104 int64_t pos;
105 } ebml_stored_master;
106
107 typedef enum EbmlType {
108 EBML_UINT,
109 EBML_SINT,
110 EBML_FLOAT,
111 EBML_UID,
112 EBML_STR,
113 EBML_UTF8 = EBML_STR,
114 EBML_BIN,
115 EBML_BLOCK, ///< pseudo-type for writing (Simple)Blocks
116 EBML_MASTER,
117 } EbmlType;
118
119 typedef struct BlockContext {
120 struct mkv_track *track;
121 const AVPacket *pkt;
122 int16_t rel_ts;
123 uint8_t flags;
124 NALUList h2645_nalu_list;
125 } BlockContext;
126
127 typedef struct EbmlMaster {
128 int nb_elements; ///< -1 if not finished
129 int containing_master; ///< -1 if no parent exists
130 } EbmlMaster;
131
132 typedef struct EbmlElement {
133 uint32_t id;
134 EbmlType type;
135 unsigned length_size;
136 uint64_t size; ///< excluding id and length field
137 union {
138 uint64_t uint;
139 int64_t sint;
140 double f;
141 const char *str;
142 const uint8_t *bin;
143 struct MatroskaMuxContext *mkv; ///< used by EBML_BLOCK
144 EbmlMaster master;
145 } priv;
146 } EbmlElement;
147
148 typedef struct EbmlWriter {
149 unsigned nb_elements;
150 int current_master_element;
151 EbmlElement *elements;
152 } EbmlWriter;
153
154 #define EBML_WRITER(max_nb_elems) \
155 EbmlElement elements[max_nb_elems]; \
156 EbmlWriter writer = (EbmlWriter){ .elements = elements, \
157 .current_master_element = -1 }
158
159 typedef struct mkv_seekhead_entry {
160 uint32_t elementid;
161 uint64_t segmentpos;
162 } mkv_seekhead_entry;
163
164 typedef struct mkv_seekhead {
165 int64_t filepos;
166 mkv_seekhead_entry entries[MAX_SEEKHEAD_ENTRIES];
167 int num_entries;
168 int reserved_size;
169 } mkv_seekhead;
170
171 typedef struct mkv_cuepoint {
172 uint64_t pts;
173 int stream_idx;
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
177 } mkv_cuepoint;
178
179 typedef struct mkv_cues {
180 mkv_cuepoint *entries;
181 int num_entries;
182 } mkv_cues;
183
184 struct MatroskaMuxContext;
185
186 typedef struct mkv_track {
187 int write_dts;
188 int has_cue;
189 uint64_t uid;
190 unsigned track_num;
191 int track_num_size;
192 int sample_rate;
193 unsigned offset;
194 int64_t sample_rate_offset;
195 int64_t last_timestamp;
196 int64_t duration;
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
202 int64_t ts_offset;
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
208 * shall be written.
209 * The callback shall not return an error on the second call. */
210 int (*reformat)(struct MatroskaMuxContext *, AVIOContext *,
211 const AVPacket *, int *size);
212 } mkv_track;
213
214 typedef struct MatroskaMuxContext {
215 const AVClass *class;
216 AVFormatContext *ctx;
217
218 int mode;
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
225 int64_t cluster_pts;
226 int64_t duration_offset;
227 int64_t duration;
228 mkv_track *tracks;
229 mkv_seekhead seekhead;
230 mkv_cues cues;
231 int64_t cues_pos;
232
233 BlockContext cur_block;
234
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(). */
241 AVIOContext *tmp_bc;
242
243 AVPacket *cur_audio_pkt;
244
245 unsigned nb_attachments;
246 int have_video;
247
248 int wrote_chapters;
249 int wrote_tags;
250
251 int reserve_cues_space;
252 int cluster_size_limit;
253 int64_t cluster_time_limit;
254 int write_crc;
255 int is_live;
256
257 int is_dash;
258 int dash_track_number;
259 int allow_raw_vfw;
260 int flipped_raw_rgb;
261 int default_mode;
262 int move_cues_to_front;
263
264 uint32_t segment_uid[4];
265 } MatroskaMuxContext;
266
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
270
271 /** 4 * (1-byte EBML ID, 1-byte EBML size, 8-byte uint max) */
272 #define MAX_CUETRACKPOS_SIZE 40
273
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
277
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))
280
281 /** Seek preroll value for opus */
282 #define OPUS_SEEK_PREROLL 80000000
283
284 static int ebml_id_size(uint32_t id)
285 {
286 return (av_log2(id) + 7U) / 8;
287 }
288
289 static void put_ebml_id(AVIOContext *pb, uint32_t id)
290 {
291 int i = ebml_id_size(id);
292 while (i--)
293 avio_w8(pb, (uint8_t)(id >> (i * 8)));
294 }
295
296 /**
297 * Write an EBML size meaning "unknown size".
298 *
299 * @param bytes The number of bytes the size should occupy (maximum: 8).
300 */
301 static void put_ebml_size_unknown(AVIOContext *pb, int bytes)
302 {
303 av_assert0(bytes <= 8);
304 avio_w8(pb, 0x1ff >> bytes);
305 if (av_builtin_constant_p(bytes) && bytes == 1)
306 return;
307 ffio_fill(pb, 0xff, bytes - 1);
308 }
309
310 /**
311 * Returns how many bytes are needed to represent a number
312 * as EBML variable length integer.
313 */
314 static int ebml_num_size(uint64_t num)
315 {
316 int bytes = 0;
317 do {
318 bytes++;
319 } while (num >>= 7);
320 return bytes;
321 }
322
323 /**
324 * Calculate how many bytes are needed to represent the length field
325 * of an EBML element whose payload has a given length.
326 */
327 static int ebml_length_size(uint64_t length)
328 {
329 return ebml_num_size(length + 1);
330 }
331
332 /**
333 * Write a number as EBML variable length integer on `bytes` bytes.
334 * `bytes` is taken literally without checking.
335 */
336 static void put_ebml_num(AVIOContext *pb, uint64_t num, int bytes)
337 {
338 num |= 1ULL << bytes * 7;
339 for (int i = bytes - 1; i >= 0; i--)
340 avio_w8(pb, (uint8_t)(num >> i * 8));
341 }
342
343 /**
344 * Write a length as EBML variable length integer.
345 *
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.
348 */
349 static void put_ebml_length(AVIOContext *pb, uint64_t length, int bytes)
350 {
351 int needed_bytes = ebml_length_size(length);
352
353 // sizes larger than this are currently undefined in EBML
354 av_assert0(length < (1ULL << 56) - 1);
355
356 if (bytes == 0)
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);
362 }
363
364 /**
365 * Write a (random) UID with fixed size to make the output more deterministic
366 */
367 static void put_ebml_uid(AVIOContext *pb, uint32_t elementid, uint64_t uid)
368 {
369 put_ebml_id(pb, elementid);
370 put_ebml_length(pb, 8, 0);
371 avio_wb64(pb, uid);
372 }
373
374 static void put_ebml_uint(AVIOContext *pb, uint32_t elementid, uint64_t val)
375 {
376 int i, bytes = 1;
377 uint64_t tmp = val;
378 while (tmp >>= 8)
379 bytes++;
380
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));
385 }
386
387 static void put_ebml_float(AVIOContext *pb, uint32_t elementid, double val)
388 {
389 put_ebml_id(pb, elementid);
390 put_ebml_length(pb, 8, 0);
391 avio_wb64(pb, av_double2int(val));
392 }
393
394 static void put_ebml_binary(AVIOContext *pb, uint32_t elementid,
395 const void *buf, int size)
396 {
397 put_ebml_id(pb, elementid);
398 put_ebml_length(pb, size, 0);
399 avio_write(pb, buf, size);
400 }
401
402 static void put_ebml_string(AVIOContext *pb, uint32_t elementid,
403 const char *str)
404 {
405 put_ebml_binary(pb, elementid, str, strlen(str));
406 }
407
408 /**
409 * Write a void element of a given size. Useful for reserving space in
410 * the file to be written to later.
411 *
412 * @param size The number of bytes to reserve, which must be at least 2.
413 */
414 static void put_ebml_void(AVIOContext *pb, int size)
415 {
416 av_assert0(size >= 2);
417
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
422 if (size < 10) {
423 size -= 2;
424 put_ebml_length(pb, size, 0);
425 } else {
426 size -= 9;
427 put_ebml_length(pb, size, 8);
428 }
429 ffio_fill(pb, 0, size);
430 }
431
432 static ebml_master start_ebml_master(AVIOContext *pb, uint32_t elementid,
433 uint64_t expectedsize)
434 {
435 int bytes = expectedsize ? ebml_length_size(expectedsize) : 8;
436
437 put_ebml_id(pb, elementid);
438 put_ebml_size_unknown(pb, bytes);
439 return (ebml_master) { avio_tell(pb), bytes };
440 }
441
442 static void end_ebml_master(AVIOContext *pb, ebml_master master)
443 {
444 int64_t pos = avio_tell(pb);
445
446 if (avio_seek(pb, master.pos - master.sizebytes, SEEK_SET) < 0)
447 return;
448 put_ebml_length(pb, pos - master.pos, master.sizebytes);
449 avio_seek(pb, pos, SEEK_SET);
450 }
451
452 static EbmlElement *ebml_writer_add(EbmlWriter *writer,
453 uint32_t id, EbmlType type)
454 {
455 writer->elements[writer->nb_elements].id = id;
456 writer->elements[writer->nb_elements].type = type;
457 return &writer->elements[writer->nb_elements++];
458 }
459
460 static void ebml_writer_open_master(EbmlWriter *writer, uint32_t id)
461 {
462 EbmlElement *const elem = ebml_writer_add(writer, id, EBML_MASTER);
463 EbmlMaster *const master = &elem->priv.master;
464
465 master->containing_master = writer->current_master_element;
466 master->nb_elements = -1;
467
468 writer->current_master_element = writer->nb_elements - 1;
469 }
470
471 static void ebml_writer_close_master(EbmlWriter *writer)
472 {
473 EbmlElement *elem;
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;
483 }
484
485 static void ebml_writer_close_or_discard_master(EbmlWriter *writer)
486 {
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--;
498 return;
499 }
500 ebml_writer_close_master(writer);
501 }
502
503 static void ebml_writer_add_string(EbmlWriter *writer, uint32_t id,
504 const char *str)
505 {
506 EbmlElement *const elem = ebml_writer_add(writer, id, EBML_STR);
507
508 elem->priv.str = str;
509 }
510
511 static void ebml_writer_add_bin(EbmlWriter *writer, uint32_t id,
512 const uint8_t *data, size_t size)
513 {
514 EbmlElement *const elem = ebml_writer_add(writer, id, EBML_BIN);
515
516 #if SIZE_MAX > UINT64_MAX
517 size = FFMIN(size, UINT64_MAX);
518 #endif
519 elem->size = size;
520 elem->priv.bin = data;
521 }
522
523 static void ebml_writer_add_float(EbmlWriter *writer, uint32_t id,
524 double val)
525 {
526 EbmlElement *const elem = ebml_writer_add(writer, id, EBML_FLOAT);
527
528 elem->priv.f = val;
529 }
530
531 static void ebml_writer_add_uid(EbmlWriter *writer, uint32_t id,
532 uint64_t val)
533 {
534 EbmlElement *const elem = ebml_writer_add(writer, id, EBML_UID);
535 elem->priv.uint = val;
536 }
537
538 static void ebml_writer_add_uint(EbmlWriter *writer, uint32_t id,
539 uint64_t val)
540 {
541 EbmlElement *elem = ebml_writer_add(writer, id, EBML_UINT);
542 elem->priv.uint = val;
543 }
544
545 static void ebml_writer_add_sint(EbmlWriter *writer, uint32_t id,
546 int64_t val)
547 {
548 EbmlElement *elem = ebml_writer_add(writer, id, EBML_SINT);
549 elem->priv.sint = val;
550 }
551
552 static void ebml_writer_add_block(EbmlWriter *writer, MatroskaMuxContext *mkv)
553 {
554 EbmlElement *elem = ebml_writer_add(writer, MATROSKA_ID_BLOCK, EBML_BLOCK);
555 elem->priv.mkv = mkv;
556 }
557
558 static int ebml_writer_str_len(EbmlElement *elem)
559 {
560 size_t len = strlen(elem->priv.str);
561 #if SIZE_MAX > UINT64_MAX
562 len = FF_MIN(len, UINT64_MAX);
563 #endif
564 elem->size = len;
565 return 0;
566 }
567
568 static av_const int uint_size(uint64_t val)
569 {
570 int bytes = 0;
571 do {
572 bytes++;
573 } while (val >>= 8);
574 return bytes;
575 }
576
577 static int ebml_writer_uint_len(EbmlElement *elem)
578 {
579 elem->size = uint_size(elem->priv.uint);
580 return 0;
581 }
582
583 static av_const int sint_size(int64_t val)
584 {
585 uint64_t tmp = 2 * (uint64_t)(val < 0 ? val^-1 : val);
586 return uint_size(tmp);
587 }
588
589 static int ebml_writer_sint_len(EbmlElement *elem)
590 {
591 elem->size = sint_size(elem->priv.sint);
592 return 0;
593 }
594
595 static int ebml_writer_elem_len(EbmlWriter *writer, EbmlElement *elem,
596 int remaining_elems);
597
598 static int ebml_writer_master_len(EbmlWriter *writer, EbmlElement *elem,
599 int remaining_elems)
600 {
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;
604
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);
608 if (ret < 0)
609 return ret;
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 */
618 }
619 master->size = total_size;
620
621 return master->priv.master.nb_elements;
622 }
623
624 static int ebml_writer_block_len(EbmlElement *elem)
625 {
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;
630 int err, size;
631
632 if (track->reformat) {
633 err = track->reformat(mkv, NULL, pkt, &size);
634 if (err < 0) {
635 av_log(mkv->ctx, AV_LOG_ERROR, "Error when reformatting data of "
636 "a packet from stream %d.\n", pkt->stream_index);
637 return err;
638 }
639 } else {
640 size = pkt->size;
641 if (track->offset <= size)
642 size -= track->offset;
643 }
644 elem->size = track->track_num_size + 3U + size;
645
646 return 0;
647 }
648
649 static void ebml_writer_write_block(const EbmlElement *elem, AVIOContext *pb)
650 {
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;
655
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);
659
660 if (track->reformat) {
661 int size;
662 track->reformat(mkv, pb, pkt, &size);
663 } else {
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);
667 }
668 }
669
670 static int ebml_writer_elem_len(EbmlWriter *writer, EbmlElement *elem,
671 int remaining_elems)
672 {
673 int ret = 0;
674
675 switch (elem->type) {
676 case EBML_FLOAT:
677 case EBML_UID:
678 elem->size = 8;
679 break;
680 case EBML_STR:
681 ret = ebml_writer_str_len(elem);
682 break;
683 case EBML_UINT:
684 ret = ebml_writer_uint_len(elem);
685 break;
686 case EBML_SINT:
687 ret = ebml_writer_sint_len(elem);
688 break;
689 case EBML_BLOCK:
690 ret = ebml_writer_block_len(elem);
691 break;
692 case EBML_MASTER:
693 ret = ebml_writer_master_len(writer, elem, remaining_elems);
694 break;
695 }
696 if (ret < 0)
697 return ret;
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 */
702 }
703
704 static int ebml_writer_elem_write(const EbmlElement *elem, AVIOContext *pb)
705 {
706 put_ebml_id(pb, elem->id);
707 put_ebml_num(pb, elem->size, elem->length_size);
708 switch (elem->type) {
709 case EBML_UID:
710 case EBML_FLOAT: {
711 uint64_t val = elem->type == EBML_UID ? elem->priv.uint
712 : av_double2int(elem->priv.f);
713 avio_wb64(pb, val);
714 break;
715 }
716 case EBML_UINT:
717 case EBML_SINT: {
718 uint64_t val = elem->type == EBML_UINT ? elem->priv.uint
719 : elem->priv.sint;
720 for (int i = elem->size; --i >= 0; )
721 avio_w8(pb, (uint8_t)(val >> i * 8));
722 break;
723 }
724 case EBML_STR:
725 case EBML_BIN: {
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);
729 break;
730 }
731 case EBML_BLOCK:
732 ebml_writer_write_block(elem, pb);
733 break;
734 case EBML_MASTER: {
735 int nb_elems = elem->priv.master.nb_elements;
736
737 elem++;
738 for (int i = 0; i < nb_elems; i++)
739 i += ebml_writer_elem_write(elem + i, pb);
740
741 return nb_elems;
742 }
743 }
744 return 0;
745 }
746
747 static int ebml_writer_write(EbmlWriter *writer, AVIOContext *pb)
748 {
749 int ret = ebml_writer_elem_len(writer, writer->elements,
750 writer->nb_elements);
751 if (ret < 0)
752 return ret;
753 ebml_writer_elem_write(writer->elements, pb);
754 return 0;
755 }
756
757 static void mkv_add_seekhead_entry(MatroskaMuxContext *mkv, uint32_t elementid,
758 uint64_t filepos)
759 {
760 mkv_seekhead *seekhead = &mkv->seekhead;
761
762 av_assert1(seekhead->num_entries < MAX_SEEKHEAD_ENTRIES);
763
764 seekhead->entries[seekhead->num_entries].elementid = elementid;
765 seekhead->entries[seekhead->num_entries++].segmentpos = filepos - mkv->segment_offset;
766 }
767
768 static int start_ebml_master_crc32(AVIOContext **dyn_cp, MatroskaMuxContext *mkv)
769 {
770 int ret;
771
772 if (!*dyn_cp && (ret = avio_open_dyn_buf(dyn_cp)) < 0)
773 return ret;
774
775 if (mkv->write_crc)
776 put_ebml_void(*dyn_cp, 6); /* Reserve space for CRC32 so position/size calculations using avio_tell() take it into account */
777
778 return 0;
779 }
780
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,
784 int add_seekentry)
785 {
786 uint8_t *buf, crc[4];
787 int ret, size, skip = 0;
788
789 size = avio_get_dyn_buf(*dyn_cp, &buf);
790 if ((ret = (*dyn_cp)->error) < 0)
791 goto fail;
792
793 if (add_seekentry)
794 mkv_add_seekhead_entry(mkv, id, avio_tell(pb));
795
796 put_ebml_id(pb, id);
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));
802 }
803 avio_write(pb, buf + skip, size - skip);
804
805 fail:
806 if (keep_buffer) {
807 ffio_reset_dyn_buf(*dyn_cp);
808 } else {
809 ffio_free_dyn_buf(dyn_cp);
810 }
811 return ret;
812 }
813
814 /**
815 * Output EBML master. Keep the buffer if seekable, allowing for later updates.
816 * Furthermore always add a SeekHead Entry for this element.
817 */
818 static int end_ebml_master_crc32_tentatively(AVIOContext *pb,
819 ebml_stored_master *elem,
820 MatroskaMuxContext *mkv, uint32_t id)
821 {
822 if (IS_SEEKABLE(pb, mkv)) {
823 uint8_t *buf;
824 int size = avio_get_dyn_buf(elem->bc, &buf);
825
826 if (elem->bc->error < 0)
827 return elem->bc->error;
828
829 elem->pos = avio_tell(pb);
830 mkv_add_seekhead_entry(mkv, id, elem->pos);
831
832 put_ebml_id(pb, id);
833 put_ebml_length(pb, size, 0);
834 avio_write(pb, buf, size);
835
836 return 0;
837 } else
838 return end_ebml_master_crc32(pb, &elem->bc, mkv, id, 0, 0, 1);
839 }
840
841 static void put_xiph_size(AVIOContext *pb, int size)
842 {
843 ffio_fill(pb, 255, size / 255);
844 avio_w8(pb, size % 255);
845 }
846
847 /**
848 * Free the members allocated in the mux context.
849 */
850 static void mkv_deinit(AVFormatContext *s)
851 {
852 MatroskaMuxContext *mkv = s->priv_data;
853
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);
859
860 av_freep(&mkv->cur_block.h2645_nalu_list.nalus);
861 av_freep(&mkv->cues.entries);
862 av_freep(&mkv->tracks);
863 }
864
865 /**
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.
869 */
870 static void mkv_start_seekhead(MatroskaMuxContext *mkv, AVIOContext *pb)
871 {
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);
878 }
879
880 /**
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.
887 *
888 * @return 0 on success, < 0 on error.
889 */
890 static int mkv_write_seekhead(AVIOContext *pb, MatroskaMuxContext *mkv,
891 int error_on_seek_failure, int64_t destpos)
892 {
893 AVIOContext *dyn_cp = NULL;
894 mkv_seekhead *seekhead = &mkv->seekhead;
895 int64_t remaining, ret64;
896 int i, ret;
897
898 if ((ret64 = avio_seek(pb, seekhead->filepos, SEEK_SET)) < 0)
899 return error_on_seek_failure ? ret64 : 0;
900
901 ret = start_ebml_master_crc32(&dyn_cp, mkv);
902 if (ret < 0)
903 return ret;
904
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,
908 MAX_SEEKENTRY_SIZE);
909
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);
913
914 put_ebml_uint(dyn_cp, MATROSKA_ID_SEEKPOSITION, entry->segmentpos);
915 end_ebml_master(dyn_cp, seekentry);
916 }
917 ret = end_ebml_master_crc32(pb, &dyn_cp, mkv,
918 MATROSKA_ID_SEEKHEAD, 0, 0, 0);
919 if (ret < 0)
920 return ret;
921
922 remaining = seekhead->filepos + seekhead->reserved_size - avio_tell(pb);
923 put_ebml_void(pb, remaining);
924
925 if ((ret64 = avio_seek(pb, destpos, SEEK_SET)) < 0)
926 return ret64;
927
928 return 0;
929 }
930
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)
933 {
934 mkv_cues *cues = &mkv->cues;
935 mkv_cuepoint *entries = cues->entries;
936 unsigned idx = cues->num_entries;
937
938 if (ts < 0)
939 return 0;
940
941 entries = av_realloc_array(entries, cues->num_entries + 1, sizeof(mkv_cuepoint));
942 if (!entries)
943 return AVERROR(ENOMEM);
944 cues->entries = entries;
945
946 /* Make sure the cues entries are sorted by pts. */
947 while (idx > 0 && entries[idx - 1].pts > ts)
948 idx--;
949 memmove(&entries[idx + 1], &entries[idx],
950 (cues->num_entries - idx) * sizeof(entries[0]));
951
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;
957
958 cues->num_entries++;
959
960 return 0;
961 }
962
963 static int mkv_assemble_cues(AVStream **streams, AVIOContext *dyn_cp, AVIOContext *cuepoint,
964 const mkv_cues *cues, mkv_track *tracks, int num_tracks,
965 uint64_t offset)
966 {
967 for (mkv_cuepoint *entry = cues->entries, *end = entry + cues->num_entries;
968 entry < end;) {
969 uint64_t pts = entry->pts;
970 uint8_t *buf;
971 int size;
972
973 put_ebml_uint(cuepoint, MATROSKA_ID_CUETIME, pts);
974
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;
979 do {
980 ebml_master track_positions;
981 int idx = entry->stream_idx;
982
983 av_assert0(idx >= 0 && idx < num_tracks);
984 if (tracks[idx].has_cue && streams[idx]->codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE)
985 continue;
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);
1000 }
1001
1002 return 0;
1003 }
1004
1005 static int put_xiph_codecpriv(AVFormatContext *s, AVIOContext *pb,
1006 const AVCodecParameters *par,
1007 const uint8_t *extradata, int extradata_size)
1008 {
1009 const uint8_t *header_start[3];
1010 int header_len[3];
1011 int first_header_size;
1012 int err, j;
1013
1014 if (par->codec_id == AV_CODEC_ID_VORBIS)
1015 first_header_size = 30;
1016 else
1017 first_header_size = 42;
1018
1019 err = avpriv_split_xiph_headers(extradata, extradata_size,
1020 first_header_size, header_start, header_len);
1021 if (err < 0) {
1022 av_log(s, AV_LOG_ERROR, "Extradata corrupt.\n");
1023 return err;
1024 }
1025
1026 avio_w8(pb, 2); // number packets - 1
1027 for (j = 0; j < 2; j++) {
1028 put_xiph_size(pb, header_len[j]);
1029 }
1030 for (j = 0; j < 3; j++)
1031 avio_write(pb, header_start[j], header_len[j]);
1032
1033 return 0;
1034 }
1035
1036 #if CONFIG_MATROSKA_MUXER
1037 static int put_wv_codecpriv(AVIOContext *pb, const uint8_t *extradata, int extradata_size)
1038 {
1039 if (extradata && extradata_size == 2)
1040 avio_write(pb, extradata, 2);
1041 else
1042 avio_wl16(pb, 0x410); // fallback to the most recent version
1043 return 0;
1044 }
1045
1046 static int put_flac_codecpriv(AVFormatContext *s, AVIOContext *pb,
1047 const AVCodecParameters *par,
1048 const uint8_t *extradata, int extradata_size)
1049 {
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,
1054 !write_comment);
1055
1056 if (ret < 0)
1057 return ret;
1058
1059 if (write_comment) {
1060 const char *vendor = (s->flags & AVFMT_FLAG_BITEXACT) ?
1061 "Lavf" : LIBAVFORMAT_IDENT;
1062 AVDictionary *dict = NULL;
1063 uint8_t buf[32];
1064 int64_t len;
1065
1066 snprintf(buf, sizeof(buf), "0x%"PRIx64, par->ch_layout.u.mask);
1067 av_dict_set(&dict, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", buf, 0);
1068
1069 len = ff_vorbiscomment_length(dict, vendor, NULL, 0);
1070 av_assert1(len < (1 << 24) - 4);
1071
1072 avio_w8(pb, 0x84);
1073 avio_wb24(pb, len);
1074
1075 ff_vorbiscomment_write(pb, dict, vendor, NULL, 0);
1076
1077 av_dict_free(&dict);
1078 }
1079
1080 return 0;
1081 }
1082
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)
1086 {
1087 MPEG4AudioConfig mp4ac;
1088 int ret;
1089
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
1093 * first packet.
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);
1100 }
1101
1102 if (ret < 0) {
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;
1109 } else {
1110 *sample_rate = mp4ac.sample_rate;
1111 *output_sample_rate = mp4ac.ext_sample_rate;
1112 }
1113 return 0;
1114 }
1115 #endif
1116
1117 static int mkv_assemble_native_codecprivate(AVFormatContext *s, AVIOContext *dyn_cp,
1118 const AVCodecParameters *par,
1119 const uint8_t *extradata,
1120 int extradata_size,
1121 unsigned *size_to_reserve)
1122 {
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:
1128 if (extradata_size)
1129 return ff_isom_write_av1c(dyn_cp, extradata,
1130 extradata_size, 1);
1131 else
1132 *size_to_reserve = (AV1_SANE_SEQUENCE_HEADER_MAX_BITS + 7) / 8 + 100;
1133 break;
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,
1141 extradata_size);
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,
1147 extradata_size, 0);
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 "
1152 "QuickTime atom.");
1153 return AVERROR_INVALIDDATA;
1154 } else
1155 avio_write(dyn_cp, extradata + 12,
1156 extradata_size - 12);
1157 break;
1158 case AV_CODEC_ID_AAC:
1159 if (extradata_size)
1160 avio_write(dyn_cp, extradata, extradata_size);
1161 else
1162 *size_to_reserve = MAX_PCE_SIZE;
1163 break;
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;
1170 break;
1171 case AV_PROFILE_ARIB_PROFILE_C:
1172 stream_identifier = 0x87;
1173 data_component_id = 0x0012;
1174 break;
1175 default:
1176 av_log(s, AV_LOG_ERROR,
1177 "Unset/unknown ARIB caption profile %d utilized!\n",
1178 par->profile);
1179 return AVERROR_INVALIDDATA;
1180 }
1181 avio_w8(dyn_cp, stream_identifier);
1182 avio_wb16(dyn_cp, data_component_id);
1183 break;
1184 }
1185 #endif
1186 default:
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);
1192 }
1193
1194 return 0;
1195 }
1196
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)
1203 {
1204 av_unused MatroskaMuxContext *const mkv = s->priv_data;
1205 unsigned size_to_reserve = 0;
1206 int ret;
1207
1208 if (native_id) {
1209 ret = mkv_assemble_native_codecprivate(s, dyn_cp, par,
1210 extradata, extradata_size,
1211 &size_to_reserve);
1212 if (ret < 0)
1213 return ret;
1214 #if CONFIG_MATROSKA_MUXER
1215 } else if (par->codec_type == AVMEDIA_TYPE_VIDEO) {
1216 if (qt_id) {
1217 if (!par->codec_tag)
1218 par->codec_tag = ff_codec_get_tag(ff_codec_movvideo_tags,
1219 par->codec_id);
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)
1222 ) {
1223 avio_wb32(dyn_cp, 0x5a + extradata_size);
1224 avio_wl32(dyn_cp, par->codec_tag);
1225 ffio_fill(dyn_cp, 0, 0x5a - 8);
1226 }
1227 avio_write(dyn_cp, extradata, extradata_size);
1228 } else {
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));
1232
1233 if (!par->codec_tag)
1234 par->codec_tag = ff_codec_get_tag(ff_codec_bmp_tags,
1235 par->codec_id);
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);
1240 }
1241
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);
1245 }
1246 } else if (par->codec_type == AVMEDIA_TYPE_AUDIO) {
1247 unsigned int tag;
1248 tag = ff_codec_get_tag(ff_codec_wav_tags, par->codec_id);
1249 if (!tag) {
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);
1253 }
1254 if (!par->codec_tag)
1255 par->codec_tag = tag;
1256
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);
1259 if (ret < 0)
1260 return ret;
1261 #endif
1262 }
1263
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;
1268
1269 return 0;
1270 }
1271
1272 static void mkv_put_codecprivate(AVIOContext *pb, unsigned max_payload_size,
1273 const uint8_t *codecpriv, unsigned codecpriv_size)
1274 {
1275 unsigned total_codecpriv_size = 0, total_size;
1276
1277 av_assert1(codecpriv_size <= max_payload_size);
1278
1279 if (!max_payload_size)
1280 return;
1281
1282 total_size = 2 + ebml_length_size(max_payload_size) + max_payload_size;
1283
1284 if (codecpriv_size) {
1285 unsigned length_size = ebml_length_size(codecpriv_size);
1286
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. */
1290 length_size++;
1291 total_codecpriv_size++;
1292 }
1293 put_ebml_id(pb, MATROSKA_ID_CODECPRIVATE);
1294 put_ebml_length(pb, codecpriv_size, length_size);
1295 avio_write(pb, codecpriv, codecpriv_size);
1296 }
1297 if (total_codecpriv_size < total_size)
1298 put_ebml_void(pb, total_size - total_codecpriv_size);
1299 }
1300
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)
1305 {
1306 AVIOContext *const dyn_bc = mkv->tmp_bc;
1307 uint8_t *codecpriv;
1308 unsigned max_payload_size;
1309 int ret, codecpriv_size;
1310
1311 ret = mkv_assemble_codecprivate(s, dyn_bc, par,
1312 side_data, side_data_size, 1, 0,
1313 &codecpriv, &codecpriv_size, &max_payload_size);
1314 if (ret < 0)
1315 goto fail;
1316 if (codecpriv_size > track->codecpriv_size && !alternative_size) {
1317 ret = AVERROR(ENOSPC);
1318 goto fail;
1319 } else if (codecpriv_size > track->codecpriv_size) {
1320 av_assert1(alternative_size < track->codecpriv_size);
1321 codecpriv_size = alternative_size;
1322 }
1323 avio_seek(pb, track->codecpriv_offset, SEEK_SET);
1324 mkv_put_codecprivate(pb, track->codecpriv_size,
1325 codecpriv, codecpriv_size);
1326
1327 if (!par->extradata_size) {
1328 ret = ff_alloc_extradata(par, side_data_size);
1329 if (ret < 0)
1330 goto fail;
1331 memcpy(par->extradata, side_data, side_data_size);
1332 }
1333 fail:
1334 ffio_reset_dyn_buf(dyn_bc);
1335 return ret;
1336 }
1337
1338 #define MAX_VIDEO_COLOR_ELEMS 20
1339 static void mkv_write_video_color(EbmlWriter *writer, const AVStream *st,
1340 const AVCodecParameters *par)
1341 {
1342 const AVPacketSideData *side_data;
1343
1344 ebml_writer_open_master(writer, MATROSKA_ID_VIDEOCOLOR);
1345
1346 if (par->color_trc != AVCOL_TRC_UNSPECIFIED &&
1347 par->color_trc < AVCOL_TRC_NB) {
1348 ebml_writer_add_uint(writer, MATROSKA_ID_VIDEOCOLORTRANSFERCHARACTERISTICS,
1349 par->color_trc);
1350 }
1351 if (par->color_space != AVCOL_SPC_UNSPECIFIED &&
1352 par->color_space < AVCOL_SPC_NB) {
1353 ebml_writer_add_uint(writer, MATROSKA_ID_VIDEOCOLORMATRIXCOEFF,
1354 par->color_space);
1355 }
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);
1360 }
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);
1364 }
1365 if (par->chroma_location != AVCHROMA_LOC_UNSPECIFIED &&
1366 par->chroma_location <= AVCHROMA_LOC_TOP) {
1367 int xpos, ypos;
1368
1369 av_chroma_location_enum_to_pos(&xpos, &ypos, par->chroma_location);
1370 ebml_writer_add_uint(writer, MATROSKA_ID_VIDEOCOLORCHROMASITINGHORZ,
1371 (xpos >> 7) + 1);
1372 ebml_writer_add_uint(writer, MATROSKA_ID_VIDEOCOLORCHROMASITINGVERT,
1373 (ypos >> 7) + 1);
1374 }
1375
1376 side_data = av_packet_side_data_get(par->coded_side_data, par->nb_coded_side_data,
1377 AV_PKT_DATA_CONTENT_LIGHT_LEVEL);
1378 if (side_data) {
1379 const AVContentLightMetadata *metadata = (AVContentLightMetadata *)side_data->data;
1380 ebml_writer_add_uint(writer, MATROSKA_ID_VIDEOCOLORMAXCLL,
1381 metadata->MaxCLL);
1382 ebml_writer_add_uint(writer, MATROSKA_ID_VIDEOCOLORMAXFALL,
1383 metadata->MaxFALL);
1384 }
1385
1386 side_data = av_packet_side_data_get(par->coded_side_data, par->nb_coded_side_data,
1387 AV_PKT_DATA_MASTERING_DISPLAY_METADATA);
1388 if (side_data) {
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]));
1408 }
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));
1414 }
1415 ebml_writer_close_or_discard_master(writer);
1416 }
1417
1418 ebml_writer_close_or_discard_master(writer);
1419 }
1420
1421 #define MAX_VIDEO_PROJECTION_ELEMS 6
1422 static void mkv_handle_rotation(void *logctx, const AVCodecParameters *par,
1423 double *yaw, double *roll)
1424 {
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);
1429
1430 if (!side_data)
1431 return;
1432
1433 matrix = (int32_t *)side_data->data;
1434
1435 /* Check whether this is an affine transformation */
1436 if (matrix[2] || matrix[5])
1437 goto ignore;
1438
1439 /* This together with the checks below test whether
1440 * the upper-left 2x2 matrix is nonsingular. */
1441 if (!matrix[0] && !matrix[1])
1442 goto ignore;
1443
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) |
1452 * scale * | |
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. */
1456
1457 /* The casts to int64_t are needed because -INT32_MIN doesn't fit
1458 * in an int32_t. */
1459 if (matrix[0] == matrix[4] && -(int64_t)matrix[1] == matrix[3]) {
1460 /* No flipping case */
1461 *yaw = 0;
1462 } else if (-(int64_t)matrix[0] == matrix[4] && matrix[1] == matrix[3]) {
1463 /* Horizontal flip */
1464 *yaw = 180;
1465 } else {
1466 ignore:
1467 av_log(logctx, AV_LOG_INFO, "Ignoring display matrix indicating "
1468 "non-orthogonal transformation.\n");
1469 return;
1470 }
1471 *roll = 180 / M_PI * atan2(matrix[3], matrix[4]);
1472
1473 /* We do not write a ProjectionType element indicating "rectangular",
1474 * because this is the default value. */
1475 }
1476
1477 static int mkv_handle_spherical(void *logctx, EbmlWriter *writer,
1478 const AVCodecParameters *par, uint8_t private[],
1479 double *yaw, double *pitch, double *roll)
1480 {
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;
1485
1486 if (!sd)
1487 return 0;
1488
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");
1494 return 0;
1495 }
1496
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);
1508 } else {
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);
1513 }
1514 ebml_writer_add_bin(writer, MATROSKA_ID_VIDEOPROJECTIONPRIVATE,
1515 private, 20);
1516 break;
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,
1524 private, 12);
1525 break;
1526 default:
1527 av_assert0(0);
1528 }
1529
1530 *yaw = (double) spherical->yaw / (1 << 16);
1531 *pitch = (double) spherical->pitch / (1 << 16);
1532 *roll = (double) spherical->roll / (1 << 16);
1533
1534 return 1; /* Projection included */
1535 }
1536
1537 static void mkv_write_video_projection(void *logctx, EbmlWriter *wr,
1538 const AVCodecParameters *par,
1539 uint8_t private[])
1540 {
1541 double yaw = 0, pitch = 0, roll = 0;
1542 int ret;
1543
1544 ebml_writer_open_master(wr, MATROSKA_ID_VIDEOPROJECTION);
1545
1546 ret = mkv_handle_spherical(logctx, wr, par, private, &yaw, &pitch, &roll);
1547 if (!ret)
1548 mkv_handle_rotation(logctx, par, &yaw, &roll);
1549
1550 if (yaw)
1551 ebml_writer_add_float(wr, MATROSKA_ID_VIDEOPROJECTIONPOSEYAW, yaw);
1552 if (pitch)
1553 ebml_writer_add_float(wr, MATROSKA_ID_VIDEOPROJECTIONPOSEPITCH, pitch);
1554 if (roll)
1555 ebml_writer_add_float(wr, MATROSKA_ID_VIDEOPROJECTIONPOSEROLL, roll);
1556
1557 ebml_writer_close_or_discard_master(wr);
1558 }
1559
1560 #define MAX_FIELD_ORDER_ELEMS 2
1561 static void mkv_write_field_order(EbmlWriter *writer, int is_webm,
1562 enum AVFieldOrder field_order)
1563 {
1564 switch (field_order) {
1565 case AV_FIELD_UNKNOWN:
1566 break;
1567 case AV_FIELD_PROGRESSIVE:
1568 ebml_writer_add_uint(writer, MATROSKA_ID_VIDEOFLAGINTERLACED,
1569 MATROSKA_VIDEO_INTERLACE_FLAG_PROGRESSIVE);
1570 break;
1571 case AV_FIELD_TT:
1572 case AV_FIELD_BB:
1573 case AV_FIELD_TB:
1574 case AV_FIELD_BT:
1575 ebml_writer_add_uint(writer, MATROSKA_ID_VIDEOFLAGINTERLACED,
1576 MATROSKA_VIDEO_INTERLACE_FLAG_INTERLACED);
1577 if (!is_webm) {
1578 switch (field_order) {
1579 case AV_FIELD_TT:
1580 ebml_writer_add_uint(writer, MATROSKA_ID_VIDEOFIELDORDER,
1581 MATROSKA_VIDEO_FIELDORDER_TT);
1582 break;
1583 case AV_FIELD_BB:
1584 ebml_writer_add_uint(writer, MATROSKA_ID_VIDEOFIELDORDER,
1585 MATROSKA_VIDEO_FIELDORDER_BB);
1586 break;
1587 case AV_FIELD_TB:
1588 ebml_writer_add_uint(writer, MATROSKA_ID_VIDEOFIELDORDER,
1589 MATROSKA_VIDEO_FIELDORDER_TB);
1590 break;
1591 case AV_FIELD_BT:
1592 ebml_writer_add_uint(writer, MATROSKA_ID_VIDEOFIELDORDER,
1593 MATROSKA_VIDEO_FIELDORDER_BT);
1594 break;
1595 }
1596 }
1597 }
1598 }
1599
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)
1605 {
1606 const char *error_message_addendum = "";
1607 const AVDictionaryEntry *tag;
1608 MatroskaVideoStereoModeType format = MATROSKA_VIDEO_STEREOMODE_TYPE_NB;
1609
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);
1620
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);
1626
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);
1632
1633 *h_width = 1;
1634 *h_height = 1;
1635
1636 if ((tag = av_dict_get(st->metadata, "stereo_mode", NULL, 0)) ||
1637 (tag = av_dict_get( s->metadata, "stereo_mode", NULL, 0))) {
1638
1639 for (int i = 0; i < MATROSKA_VIDEO_STEREOMODE_TYPE_NB; i++)
1640 if (!strcmp(tag->value, ff_matroska_video_stereo_mode[i])){
1641 format = i;
1642 break;
1643 }
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)
1647 goto fail;
1648 format = stereo_mode;
1649 }
1650 } else {
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)
1660 };
1661 int fmt;
1662
1663 sd = av_packet_side_data_get(par->coded_side_data, par->nb_coded_side_data,
1664 AV_PKT_DATA_STEREO3D);
1665 if (!sd)
1666 return 0;
1667
1668 stereo = (const AVStereo3D*)sd->data;
1669
1670 /* A garbage AVStereo3D or something with no Matroska analogon. */
1671 if ((unsigned)stereo->type >= FF_ARRAY_ELEMS(conversion_table))
1672 return 0;
1673
1674 fmt = conversion_table[stereo->type][!!(stereo->flags & AV_STEREO3D_FLAG_INVERT)];
1675 /* Format with no Matroska analogon; ignore it */
1676 if (!fmt)
1677 return 0;
1678 format = fmt - 1;
1679 }
1680
1681 // if webm, do not write unsupported modes
1682 if (is_webm && !(webm_bitfield >> format)) {
1683 error_message_addendum = " for WebM";
1684 goto fail;
1685 }
1686
1687 *h_width = 1 << ((width_bitfield >> format) & 1);
1688 *h_height = 1 << ((height_bitfield >> format) & 1);
1689
1690 // write StereoMode if format is valid
1691 ebml_writer_add_uint(writer, MATROSKA_ID_VIDEOSTEREOMODE, format);
1692
1693 return 0;
1694 fail:
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);
1699 }
1700
1701 static void mkv_write_blockadditionmapping(AVFormatContext *s, const MatroskaMuxContext *mkv,
1702 const AVCodecParameters *par, AVIOContext *pb,
1703 mkv_track *track, const AVStream *st)
1704 {
1705 #if CONFIG_MATROSKA_MUXER
1706 const AVDOVIDecoderConfigurationRecord *dovi;
1707 const AVPacketSideData *sd;
1708
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 */);
1721 }
1722 }
1723
1724 sd = av_packet_side_data_get(par->coded_side_data, par->nb_coded_side_data,
1725 AV_PKT_DATA_DOVI_CONF);
1726
1727 if (!sd)
1728 return;
1729
1730 dovi = (const AVDOVIDecoderConfigurationRecord *)sd->data;
1731 if (dovi->dv_profile <= 10) {
1732 ebml_master mapping;
1733 uint8_t buf[ISOM_DVCC_DVVC_SIZE];
1734 uint32_t type;
1735
1736 uint64_t expected_size = (2 + 1 + (sizeof(DVCC_DVVC_BLOCK_TYPE_NAME) - 1))
1737 + (2 + 1 + 4) + (2 + 1 + ISOM_DVCC_DVVC_SIZE);
1738
1739 if (dovi->dv_profile > 7) {
1740 type = MATROSKA_BLOCK_ADD_ID_TYPE_DVVC;
1741 } else {
1742 type = MATROSKA_BLOCK_ADD_ID_TYPE_DVCC;
1743 }
1744
1745 ff_isom_put_dvcc_dvvc(s, buf, dovi);
1746
1747 mapping = start_ebml_master(pb, MATROSKA_ID_TRACKBLKADDMAPPING, expected_size);
1748
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));
1752
1753 end_ebml_master(pb, mapping);
1754 }
1755 #endif
1756 }
1757
1758 static int mkv_write_track_video(AVFormatContext *s, MatroskaMuxContext *mkv,
1759 const AVStream *st, const AVCodecParameters *par,
1760 AVIOContext *pb)
1761 {
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;
1769 int ret;
1770
1771 ebml_writer_open_master(&writer, MATROSKA_ID_TRACKVIDEO);
1772
1773 ebml_writer_add_uint(&writer, MATROSKA_ID_VIDEOPIXELWIDTH , par->width);
1774 ebml_writer_add_uint(&writer, MATROSKA_ID_VIDEOPIXELHEIGHT, par->height);
1775
1776 mkv_write_field_order(&writer, IS_WEBM(mkv), par->field_order);
1777
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),
1781 &display_width_div,
1782 &display_height_div);
1783 if (ret < 0)
1784 return ret;
1785
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);
1790
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;
1796
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);
1801
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);
1806 }
1807
1808 if (bottom)
1809 ebml_writer_add_uint(&writer, MATROSKA_ID_VIDEOPIXELCROPB, bottom);
1810 if (top)
1811 ebml_writer_add_uint(&writer, MATROSKA_ID_VIDEOPIXELCROPT, top);
1812 if (left)
1813 ebml_writer_add_uint(&writer, MATROSKA_ID_VIDEOPIXELCROPL, left);
1814 if (right)
1815 ebml_writer_add_uint(&writer, MATROSKA_ID_VIDEOPIXELCROPR, right);
1816
1817 cropped_width -= left + right;
1818 cropped_height -= top + bottom;
1819 }
1820
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);
1828 }
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);
1835 } else {
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,
1840 1024 * 1024);
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);
1847 }
1848 }
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);
1857
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));
1862 }
1863 mkv_write_video_color(&writer, st, par);
1864 mkv_write_video_projection(s, &writer, par, projection_private);
1865
1866 return ebml_writer_write(&writer, pb);
1867 }
1868
1869 static void mkv_write_default_duration(mkv_track *track, AVIOContext *pb,
1870 AVRational duration)
1871 {
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);
1877 }
1878
1879 static int mkv_write_track(AVFormatContext *s, MatroskaMuxContext *mkv,
1880 AVStream *st, mkv_track *track, AVIOContext *pb,
1881 int is_default)
1882 {
1883 AVCodecParameters *par = st->codecpar;
1884 ebml_master subinfo, track_master;
1885 int native_id = 0;
1886 int qt_id = 0;
1887 int bit_depth;
1888 int sample_rate = par->sample_rate;
1889 int output_sample_rate = 0;
1890 int j, ret;
1891 const AVDictionaryEntry *tag;
1892
1893 if (par->codec_type == AVMEDIA_TYPE_ATTACHMENT)
1894 return 0;
1895
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)
1900
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");
1906
1907 // The default value for TRACKFLAGDEFAULT is 1, so add element
1908 // if we need to clear it.
1909 if (!is_default)
1910 put_ebml_uint(pb, MATROSKA_ID_TRACKFLAGDEFAULT, 0);
1911
1912 if (st->disposition & AV_DISPOSITION_FORCED)
1913 put_ebml_uint(pb, MATROSKA_ID_TRACKFLAGFORCED, 1);
1914
1915 if (IS_WEBM(mkv)) {
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;
1921 native_id = 1;
1922 break;
1923 }
1924 }
1925 } else {
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;
1935 } else {
1936 codec_id = "D_WEBVTT/SUBTITLES";
1937 native_id = MATROSKA_TRACK_TYPE_SUBTITLE;
1938 }
1939 }
1940
1941 if (!native_id) {
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);
1945 }
1946
1947 put_ebml_string(pb, MATROSKA_ID_CODECID, codec_id);
1948 } else {
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));
1960
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);
1971 native_id = 1;
1972 break;
1973 }
1974 }
1975 } else {
1976 if (mkv->allow_raw_vfw) {
1977 native_id = 0;
1978 } else {
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);
1982 }
1983 }
1984 }
1985
1986 switch (par->codec_type) {
1987 AVRational frame_rate;
1988 int audio_frame_samples;
1989
1990 case AVMEDIA_TYPE_VIDEO:
1991 mkv->have_video = 1;
1992 put_ebml_uint(pb, MATROSKA_ID_TRACKTYPE, MATROSKA_TRACK_TYPE_VIDEO);
1993
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;
1999
2000 if (frame_rate.num > 0)
2001 mkv_write_default_duration(track, pb, av_inv_q(frame_rate));
2002
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))
2009 qt_id = 1;
2010
2011 if (qt_id)
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;
2018 }
2019
2020 ret = mkv_write_track_video(s, mkv, st, par, pb);
2021 if (ret < 0)
2022 return ret;
2023
2024 break;
2025
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);
2034 }
2035 put_ebml_uint(pb, MATROSKA_ID_CODECDELAY, codecdelay);
2036
2037 track->ts_offset = av_rescale_q(par->initial_padding,
2038 (AVRational){ 1, par->sample_rate },
2039 st->time_base);
2040 ffstream(st)->lowest_ts_allowed = -track->ts_offset;
2041 }
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);
2048 if (ret < 0)
2049 return ret;
2050 }
2051 #endif
2052
2053 put_ebml_uint(pb, MATROSKA_ID_TRACKTYPE, MATROSKA_TRACK_TYPE_AUDIO);
2054
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 });
2059 if (!native_id)
2060 // no mkv-specific ID, use ACM mode
2061 put_ebml_string(pb, MATROSKA_ID_CODECID, "A_MS/ACM");
2062
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);
2065
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);
2070
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;
2075 else
2076 bit_depth = av_get_bytes_per_sample(par->format) << 3;
2077 }
2078 if (!bit_depth)
2079 bit_depth = par->bits_per_coded_sample;
2080 if (bit_depth)
2081 put_ebml_uint(pb, MATROSKA_ID_AUDIOBITDEPTH, bit_depth);
2082 end_ebml_master(pb, subinfo);
2083 break;
2084
2085 case AVMEDIA_TYPE_SUBTITLE:
2086 if (!native_id) {
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);
2090 }
2091 if (!IS_WEBM(mkv) && st->disposition & AV_DISPOSITION_DESCRIPTIONS)
2092 put_ebml_uint(pb, MATROSKA_ID_TRACKFLAGTEXTDESCRIPTIONS, 1);
2093
2094 if (!IS_WEBM(mkv) || par->codec_id != AV_CODEC_ID_WEBVTT)
2095 native_id = MATROSKA_TRACK_TYPE_SUBTITLE;
2096
2097 put_ebml_uint(pb, MATROSKA_ID_TRACKTYPE, native_id);
2098 break;
2099 default:
2100 av_log(s, AV_LOG_ERROR, "Only audio, video, and subtitles are supported for Matroska.\n");
2101 return AVERROR(EINVAL);
2102 }
2103
2104 if (!IS_WEBM(mkv))
2105 mkv_write_blockadditionmapping(s, mkv, par, pb, track, st);
2106
2107 if (!IS_WEBM(mkv) || par->codec_id != AV_CODEC_ID_WEBVTT) {
2108 uint8_t *codecpriv;
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,
2113 native_id, qt_id,
2114 &codecpriv, &codecpriv_size, &max_payload_size);
2115 if (ret < 0)
2116 goto fail;
2117 mkv_put_codecprivate(pb, max_payload_size, codecpriv, codecpriv_size);
2118 track->codecpriv_size = max_payload_size;
2119 }
2120
2121 end_ebml_master(pb, track_master);
2122 ret = 0;
2123 fail:
2124 ffio_reset_dyn_buf(mkv->tmp_bc);
2125
2126 return ret;
2127 }
2128
2129 static int mkv_write_tracks(AVFormatContext *s)
2130 {
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;
2134 int i, ret;
2135
2136 if (mkv->nb_attachments == s->nb_streams)
2137 return 0;
2138
2139 ret = start_ebml_master_crc32(&mkv->track.bc, mkv);
2140 if (ret < 0)
2141 return ret;
2142
2143 if (mkv->default_mode != DEFAULT_MODE_PASSTHROUGH) {
2144 int video_idx = -1, audio_idx = -1, subtitle_idx = -1;
2145
2146 for (i = s->nb_streams - 1; i >= 0; i--) {
2147 AVStream *st = s->streams[i];
2148
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; \
2155 break;
2156 CASE(VIDEO, video)
2157 CASE(AUDIO, audio)
2158 CASE(SUBTITLE, subtitle)
2159 #undef CASE
2160 }
2161 }
2162
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);
2167 }
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);
2175 if (ret < 0)
2176 return ret;
2177 }
2178
2179 return end_ebml_master_crc32_tentatively(pb, &mkv->track, mkv,
2180 MATROSKA_ID_TRACKS);
2181 }
2182
2183 static int mkv_write_simpletag(AVIOContext *pb, const AVDictionaryEntry *t)
2184 {
2185 EBML_WRITER(4);
2186 uint8_t *key = av_strdup(t->key);
2187 uint8_t *p = key;
2188 const uint8_t *lang = NULL;
2189 int ret;
2190
2191 if (!key)
2192 return AVERROR(ENOMEM);
2193
2194 if ((p = strrchr(p, '-')) &&
2195 (lang = ff_convert_lang_to(p + 1, AV_LANG_ISO639_2_BIBL)))
2196 *p = 0;
2197
2198 p = key;
2199 while (*p) {
2200 if (*p == ' ')
2201 *p = '_';
2202 else if (*p >= 'a' && *p <= 'z')
2203 *p -= 'a' - 'A';
2204 p++;
2205 }
2206
2207 ebml_writer_open_master(&writer, MATROSKA_ID_SIMPLETAG);
2208 ebml_writer_add_string(&writer, MATROSKA_ID_TAGNAME, key);
2209 if (lang)
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);
2213
2214 av_freep(&key);
2215 return ret;
2216 }
2217
2218 static void mkv_write_tag_targets(MatroskaMuxContext *mkv, AVIOContext *pb,
2219 uint32_t elementid, uint64_t uid)
2220 {
2221 ebml_master targets = start_ebml_master(pb, MATROSKA_ID_TAGTARGETS,
2222 4 + 1 + 8);
2223 if (elementid)
2224 put_ebml_uid(pb, elementid, uid);
2225 end_ebml_master(pb, targets);
2226 }
2227
2228 static int mkv_check_tag_name(const char *name, uint32_t elementid)
2229 {
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")));
2240 }
2241
2242 static int mkv_write_tag(MatroskaMuxContext *mkv, const AVDictionary *m,
2243 AVIOContext **pb, unsigned reserved_size,
2244 uint32_t elementid, uint64_t uid)
2245 {
2246 const AVDictionaryEntry *t = NULL;
2247 AVIOContext *const tmp_bc = mkv->tmp_bc;
2248 uint8_t *buf;
2249 int ret = 0, size, tag_written = 0;
2250
2251 mkv_write_tag_targets(mkv, tmp_bc, elementid, uid);
2252
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);
2256 if (ret < 0)
2257 goto end;
2258 tag_written = 1;
2259 }
2260 }
2261 if (reserved_size)
2262 put_ebml_void(tmp_bc, reserved_size);
2263 else if (!tag_written)
2264 goto end;
2265
2266 size = avio_get_dyn_buf(tmp_bc, &buf);
2267 if (tmp_bc->error) {
2268 ret = tmp_bc->error;
2269 goto end;
2270 }
2271 if (!*pb) {
2272 ret = start_ebml_master_crc32(pb, mkv);
2273 if (ret < 0)
2274 goto end;
2275 }
2276 put_ebml_binary(*pb, MATROSKA_ID_TAG, buf, size);
2277
2278 end:
2279 ffio_reset_dyn_buf(tmp_bc);
2280 return ret;
2281 }
2282
2283 static int mkv_write_tags(AVFormatContext *s)
2284 {
2285 MatroskaMuxContext *mkv = s->priv_data;
2286 int i, ret, seekable = IS_SEEKABLE(s->pb, mkv);
2287
2288 mkv->wrote_tags = 1;
2289
2290 ff_metadata_conv_ctx(s, ff_mkv_metadata_conv, NULL);
2291
2292 ret = mkv_write_tag(mkv, s->metadata, &mkv->tags.bc, 0, 0, 0);
2293 if (ret < 0)
2294 return ret;
2295
2296 for (i = 0; i < s->nb_streams; i++) {
2297 const AVStream *st = s->streams[i];
2298 mkv_track *track = &mkv->tracks[i];
2299
2300 if (st->codecpar->codec_type == AVMEDIA_TYPE_ATTACHMENT)
2301 continue;
2302
2303 ret = mkv_write_tag(mkv, st->metadata, &mkv->tags.bc,
2304 seekable ? DURATION_SIMPLETAG_SIZE : 0,
2305 MATROSKA_ID_TAGTARGETS_TRACKUID, track->uid);
2306 if (ret < 0)
2307 return ret;
2308 if (seekable)
2309 track->duration_offset = avio_tell(mkv->tags.bc) - DURATION_SIMPLETAG_SIZE;
2310 }
2311
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];
2316
2317 if (st->codecpar->codec_type != AVMEDIA_TYPE_ATTACHMENT)
2318 continue;
2319
2320 ret = mkv_write_tag(mkv, st->metadata, &mkv->tags.bc, 0,
2321 MATROSKA_ID_TAGTARGETS_ATTACHUID, track->uid);
2322 if (ret < 0)
2323 return ret;
2324 }
2325 }
2326
2327 if (mkv->tags.bc) {
2328 return end_ebml_master_crc32_tentatively(s->pb, &mkv->tags, mkv,
2329 MATROSKA_ID_TAGS);
2330 }
2331 return 0;
2332 }
2333
2334 static int mkv_new_chapter_ids_needed(const AVFormatContext *s)
2335 {
2336 for (unsigned i = 0; i < s->nb_chapters; i++) {
2337 if (!s->chapters[i]->id)
2338 return 1;
2339 for (unsigned j = 0; j < i; j++)
2340 if (s->chapters[j]->id == s->chapters[i]->id)
2341 return 1;
2342 }
2343 return 0;
2344 }
2345
2346 static int mkv_write_chapters(AVFormatContext *s)
2347 {
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;
2353
2354 if (!s->nb_chapters || mkv->wrote_chapters)
2355 return 0;
2356
2357 ret = start_ebml_master_crc32(&dyn_cp, mkv);
2358 if (ret < 0)
2359 return ret;
2360
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;
2367 } else
2368 tags = NULL;
2369
2370 create_new_ids = mkv_new_chapter_ids_needed(s);
2371
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;
2378 EBML_WRITER(7);
2379
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;
2385 goto fail;
2386 }
2387
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");
2396 }
2397 ret = ebml_writer_write(&writer, dyn_cp);
2398 if (ret < 0)
2399 goto fail;
2400
2401 if (tags) {
2402 ff_metadata_conv(&c->metadata, ff_mkv_metadata_conv, NULL);
2403
2404 ret = mkv_write_tag(mkv, c->metadata, tags, 0,
2405 MATROSKA_ID_TAGTARGETS_CHAPTERUID, uid);
2406 if (ret < 0)
2407 goto fail;
2408 }
2409 }
2410 end_ebml_master(dyn_cp, editionentry);
2411 mkv->wrote_chapters = 1;
2412
2413 ret = end_ebml_master_crc32(pb, &dyn_cp, mkv, MATROSKA_ID_CHAPTERS, 0, 0, 1);
2414 if (ret < 0)
2415 goto fail;
2416 if (dyn_tags)
2417 return end_ebml_master_crc32(pb, &dyn_tags, mkv,
2418 MATROSKA_ID_TAGS, 0, 0, 1);
2419 return 0;
2420
2421 fail:
2422 if (tags) {
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);
2426 }
2427 ffio_free_dyn_buf(&dyn_cp);
2428 return ret;
2429 }
2430
2431 static const char *get_mimetype(const AVStream *st)
2432 {
2433 const AVDictionaryEntry *t;
2434
2435 if (t = av_dict_get(st->metadata, "mimetype", NULL, 0))
2436 return t->value;
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";
2443 }
2444
2445 return NULL;
2446 }
2447
2448 static int mkv_write_attachments(AVFormatContext *s)
2449 {
2450 MatroskaMuxContext *mkv = s->priv_data;
2451 AVIOContext *dyn_cp = NULL, *pb = s->pb;
2452 int i, ret;
2453
2454 if (!mkv->nb_attachments)
2455 return 0;
2456
2457 ret = start_ebml_master_crc32(&dyn_cp, mkv);
2458 if (ret < 0)
2459 return ret;
2460
2461 for (i = 0; i < s->nb_streams; i++) {
2462 const AVStream *st = s->streams[i];
2463 mkv_track *track = &mkv->tracks[i];
2464 EBML_WRITER(6);
2465 const AVDictionaryEntry *t;
2466 const char *mimetype;
2467
2468 if (st->codecpar->codec_type != AVMEDIA_TYPE_ATTACHMENT)
2469 continue;
2470
2471 ebml_writer_open_master(&writer, MATROSKA_ID_ATTACHEDFILE);
2472
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);
2479 }
2480 ebml_writer_add_string(&writer, MATROSKA_ID_FILENAME, t->value);
2481
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);
2489 if (ret < 0) {
2490 ffio_free_dyn_buf(&dyn_cp);
2491 return ret;
2492 }
2493 }
2494 return end_ebml_master_crc32(pb, &dyn_cp, mkv,
2495 MATROSKA_ID_ATTACHMENTS, 0, 0, 1);
2496 }
2497
2498 static int64_t get_metadata_duration(AVFormatContext *s)
2499 {
2500 const AVDictionaryEntry *duration = av_dict_get(s->metadata, "DURATION",
2501 NULL, 0);
2502 int64_t max = 0;
2503 int64_t us;
2504
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);
2507 return us;
2508 }
2509
2510 for (unsigned i = 0; i < s->nb_streams; i++) {
2511 int64_t us;
2512 duration = av_dict_get(s->streams[i]->metadata, "DURATION", NULL, 0);
2513
2514 if (duration && (av_parse_time(&us, duration->value, 1) == 0))
2515 max = FFMAX(max, us);
2516 }
2517
2518 av_log(s, AV_LOG_DEBUG, "get_metadata_duration returned: %" PRId64 "\n", max);
2519 return max;
2520 }
2521
2522 static void ebml_write_header(AVIOContext *pb,
2523 const char *doctype, int version)
2524 {
2525 EBML_WRITER(8);
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);
2536 }
2537
2538 static int mkv_write_info(AVFormatContext *s)
2539 {
2540 MatroskaMuxContext *mkv = s->priv_data;
2541 const AVDictionaryEntry *tag;
2542 int64_t creation_time;
2543 AVIOContext *pb;
2544 int ret = start_ebml_master_crc32(&mkv->info.bc, mkv);
2545 if (ret < 0)
2546 return ret;
2547 pb = mkv->info.bc;
2548
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);
2556 else
2557 put_ebml_string(pb, MATROSKA_ID_WRITINGAPP, LIBAVFORMAT_IDENT);
2558
2559 if (!IS_WEBM(mkv))
2560 put_ebml_binary(pb, MATROSKA_ID_SEGMENTUID, mkv->segment_uid, 16);
2561 } else {
2562 const char *ident = "Lavf";
2563 put_ebml_string(pb, MATROSKA_ID_MUXINGAPP , ident);
2564 put_ebml_string(pb, MATROSKA_ID_WRITINGAPP, ident);
2565 }
2566
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);
2573 }
2574
2575 // reserve space for the duration
2576 mkv->duration = 0;
2577 mkv->duration_offset = avio_tell(pb);
2578 if (!mkv->is_live) {
2579 int64_t metadata_duration = get_metadata_duration(s);
2580
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
2591 }
2592 }
2593 return end_ebml_master_crc32_tentatively(s->pb, &mkv->info,
2594 mkv, MATROSKA_ID_INFO);
2595 }
2596
2597 static int mkv_write_header(AVFormatContext *s)
2598 {
2599 MatroskaMuxContext *mkv = s->priv_data;
2600 AVIOContext *pb = s->pb;
2601 int ret, version = 2;
2602
2603 ret = avio_open_dyn_buf(&mkv->tmp_bc);
2604 if (ret < 0)
2605 return ret;
2606
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))
2610 version = 4;
2611
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))
2616 version = 4;
2617 }
2618
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);
2623
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);
2627
2628 ret = mkv_write_info(s);
2629 if (ret < 0)
2630 return ret;
2631
2632 ret = mkv_write_tracks(s);
2633 if (ret < 0)
2634 return ret;
2635
2636 ret = mkv_write_chapters(s);
2637 if (ret < 0)
2638 return ret;
2639
2640 if (!IS_WEBM(mkv)) {
2641 ret = mkv_write_attachments(s);
2642 if (ret < 0)
2643 return ret;
2644 }
2645
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);
2649 if (ret < 0)
2650 return ret;
2651
2652 if (!IS_SEEKABLE(pb, mkv)) {
2653 ret = mkv_write_seekhead(pb, mkv, 0, avio_tell(pb));
2654 if (ret < 0)
2655 return ret;
2656 }
2657
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);
2662 }
2663
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);
2671 }
2672 } else
2673 mkv->reserve_cues_space = -1;
2674 }
2675
2676 mkv->cluster_pos = -1;
2677
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;
2685 } else {
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;
2690 }
2691
2692 return 0;
2693 }
2694
2695 #if CONFIG_MATROSKA_MUXER
2696 static int mkv_reformat_h2645(MatroskaMuxContext *mkv, AVIOContext *pb,
2697 const AVPacket *pkt, int *size)
2698 {
2699 int ret;
2700 if (pb) {
2701 ff_nal_units_write_list(&mkv->cur_block.h2645_nalu_list, pb, pkt->data);
2702 } else {
2703 ret = ff_nal_units_create_list(&mkv->cur_block.h2645_nalu_list, pkt->data, pkt->size);
2704 if (ret < 0)
2705 return ret;
2706 *size = ret;
2707 }
2708 return 0;
2709 }
2710
2711 static int mkv_reformat_wavpack(MatroskaMuxContext *mkv, AVIOContext *pb,
2712 const AVPacket *pkt, int *size)
2713 {
2714 const uint8_t *src = pkt->data;
2715 int srclen = pkt->size;
2716 int offset = 0;
2717 int ret;
2718
2719 while (srclen >= WV_HEADER_SIZE) {
2720 WvHeader header;
2721
2722 ret = ff_wv_parse_header(&header, src);
2723 if (ret < 0)
2724 return ret;
2725 src += WV_HEADER_SIZE;
2726 srclen -= WV_HEADER_SIZE;
2727
2728 if (srclen < header.blocksize)
2729 return AVERROR_INVALIDDATA;
2730
2731 offset += 4 * !!header.initial + 8 + 4 * !(header.initial && header.final);
2732 if (pb) {
2733 if (header.initial)
2734 avio_wl32(pb, header.samples);
2735 avio_wl32(pb, header.flags);
2736 avio_wl32(pb, header.crc);
2737
2738 if (!(header.initial && header.final))
2739 avio_wl32(pb, header.blocksize);
2740
2741 avio_write(pb, src, header.blocksize);
2742 }
2743 src += header.blocksize;
2744 srclen -= header.blocksize;
2745 offset += header.blocksize;
2746 }
2747 *size = offset;
2748
2749 return 0;
2750 }
2751 #endif
2752
2753 static int mkv_reformat_av1(MatroskaMuxContext *mkv, AVIOContext *pb,
2754 const AVPacket *pkt, int *size)
2755 {
2756 int ret = ff_av1_filter_obus(pb, pkt->data, pkt->size);
2757 if (ret < 0)
2758 return ret;
2759 *size = ret;
2760 return 0;
2761 }
2762
2763 static int webm_reformat_vtt(MatroskaMuxContext *mkv, AVIOContext *pb,
2764 const AVPacket *pkt, int *size)
2765 {
2766 const uint8_t *id, *settings;
2767 size_t id_size, settings_size;
2768 unsigned total = pkt->size + 2U;
2769
2770 if (total > INT_MAX)
2771 return AVERROR(ERANGE);
2772
2773 id = av_packet_get_side_data(pkt, AV_PKT_DATA_WEBVTT_IDENTIFIER,
2774 &id_size);
2775 settings = av_packet_get_side_data(pkt, AV_PKT_DATA_WEBVTT_SETTINGS,
2776 &settings_size);
2777 if (id_size > INT_MAX - total || settings_size > INT_MAX - (total += id_size))
2778 return AVERROR(ERANGE);
2779 *size = total += settings_size;
2780 if (pb) {
2781 avio_write(pb, id, id_size);
2782 avio_w8(pb, '\n');
2783 avio_write(pb, settings, settings_size);
2784 avio_w8(pb, '\n');
2785 avio_write(pb, pkt->data, pkt->size);
2786 }
2787 return 0;
2788 }
2789
2790 static void mkv_write_blockadditional(EbmlWriter *writer, const uint8_t *buf,
2791 size_t size, uint64_t additional_id)
2792 {
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);
2797 }
2798
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)
2804 {
2805 uint8_t t35_buf[6 + AV_HDR_PLUS_MAX_PAYLOAD_SIZE];
2806 uint8_t *side_data;
2807 size_t side_data_size;
2808 uint64_t additional_id;
2809 unsigned track_number = track->track_num;
2810 EBML_WRITER(12);
2811 int ret;
2812
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;
2817
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);
2822
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);
2830
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);
2837
2838 side_data = av_packet_get_side_data(pkt,
2839 AV_PKT_DATA_SKIP_SAMPLES,
2840 &side_data_size);
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);
2848 }
2849 }
2850
2851 ebml_writer_open_master(&writer, MATROSKA_ID_BLOCKADDITIONS);
2852 side_data = av_packet_get_side_data(pkt,
2853 AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL,
2854 &side_data_size);
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,
2859 additional_id);
2860 track->max_blockaddid = FFMAX(track->max_blockaddid, additional_id);
2861 }
2862
2863 if (par->codec_type == AVMEDIA_TYPE_VIDEO) {
2864 side_data = av_packet_get_side_data(pkt,
2865 AV_PKT_DATA_DYNAMIC_HDR10_PLUS,
2866 &side_data_size);
2867 if (side_data && side_data_size) {
2868 uint8_t *payload = t35_buf;
2869 size_t payload_size = sizeof(t35_buf) - 6;
2870
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
2875
2876 ret = av_dynamic_hdr_plus_to_t35((AVDynamicHDRPlus *)side_data, &payload,
2877 &payload_size);
2878 if (ret < 0)
2879 return ret;
2880
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);
2885 }
2886 }
2887
2888 ebml_writer_close_or_discard_master(&writer);
2889
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;
2896 if (keyframe)
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);
2901
2902 return ebml_writer_write(&writer, pb);
2903 }
2904
2905 static int mkv_end_cluster(AVFormatContext *s)
2906 {
2907 MatroskaMuxContext *mkv = s->priv_data;
2908 int ret;
2909
2910 if (!mkv->have_video) {
2911 for (unsigned i = 0; i < s->nb_streams; i++)
2912 mkv->tracks[i].has_cue = 0;
2913 }
2914 mkv->cluster_pos = -1;
2915 ret = end_ebml_master_crc32(s->pb, &mkv->cluster_bc, mkv,
2916 MATROSKA_ID_CLUSTER, 0, 1, 0);
2917 if (ret < 0)
2918 return ret;
2919
2920 avio_write_marker(s->pb, AV_NOPTS_VALUE, AVIO_DATA_MARKER_FLUSH_POINT);
2921 return 0;
2922 }
2923
2924 static int mkv_check_new_extra_data(AVFormatContext *s, const AVPacket *pkt)
2925 {
2926 MatroskaMuxContext *mkv = s->priv_data;
2927 mkv_track *track = &mkv->tracks[pkt->stream_index];
2928 AVCodecParameters *par = s->streams[pkt->stream_index]->codecpar;
2929 uint8_t *side_data;
2930 size_t side_data_size;
2931 int ret;
2932
2933 side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
2934 &side_data_size);
2935
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);
2943 if (ret < 0)
2944 return ret;
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);
2949 if (ret < 0)
2950 return ret;
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);
2958 }
2959 break;
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",
2964 pkt->stream_index);
2965 return AVERROR(EINVAL);
2966 }
2967 ret = mkv_update_codecprivate(s, mkv, side_data, side_data_size,
2968 par, mkv->track.bc, track, 0);
2969 if (ret < 0)
2970 return ret;
2971 }
2972 break;
2973 #endif
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);
2982 if (ret < 0)
2983 return ret;
2984 } else if (!par->extradata_size)
2985 return AVERROR_INVALIDDATA;
2986 break;
2987 default:
2988 if (side_data_size)
2989 av_log(s, AV_LOG_DEBUG, "Ignoring new extradata in a packet for stream %d.\n", pkt->stream_index);
2990 break;
2991 }
2992
2993 return 0;
2994 }
2995
2996 static int mkv_write_packet_internal(AVFormatContext *s, const AVPacket *pkt)
2997 {
2998 MatroskaMuxContext *mkv = s->priv_data;
2999 AVIOContext *pb;
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;
3007 int ret;
3008 int64_t ts = track->write_dts ? pkt->dts : pkt->pts;
3009 int64_t relative_packet_pos;
3010
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);
3014 }
3015 ts += track->ts_offset;
3016
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);
3021 if (ret < 0)
3022 return ret;
3023 av_log(s, AV_LOG_WARNING, "Starting new cluster due to timestamp\n");
3024 }
3025 }
3026
3027 if (mkv->cluster_pos == -1) {
3028 ret = start_ebml_master_crc32(&mkv->cluster_bc, mkv);
3029 if (ret < 0)
3030 return ret;
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);
3039 }
3040 pb = mkv->cluster_bc;
3041
3042 relative_packet_pos = avio_tell(pb);
3043
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);
3050 if (ret < 0)
3051 return ret;
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,
3058 cue_duration);
3059 if (ret < 0)
3060 return ret;
3061 track->has_cue = 1;
3062 }
3063
3064 track->last_timestamp = ts;
3065 mkv->duration = FFMAX(mkv->duration, ts + duration);
3066 track->duration = FFMAX(track->duration, ts + duration);
3067
3068 return 0;
3069 }
3070
3071 static int mkv_write_packet(AVFormatContext *s, const AVPacket *pkt)
3072 {
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);
3076 int cluster_size;
3077 int64_t cluster_time;
3078 int ret;
3079 int start_new_cluster;
3080
3081 ret = mkv_check_new_extra_data(s, pkt);
3082 if (ret < 0)
3083 return ret;
3084
3085 if (mkv->cluster_pos != -1) {
3086 if (mkv->tracks[pkt->stream_index].write_dts)
3087 cluster_time = pkt->dts - mkv->cluster_pts;
3088 else
3089 cluster_time = pkt->pts - mkv->cluster_pts;
3090 cluster_time += mkv->tracks[pkt->stream_index].ts_offset;
3091
3092 cluster_size = avio_tell(mkv->cluster_bc);
3093
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;
3109 } else
3110 start_new_cluster = 0;
3111
3112 if (start_new_cluster) {
3113 ret = mkv_end_cluster(s);
3114 if (ret < 0)
3115 return ret;
3116 }
3117 }
3118
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);
3123
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);
3128 if (ret < 0) {
3129 av_log(s, AV_LOG_ERROR,
3130 "Could not write cached audio packet ret:%d\n", ret);
3131 return ret;
3132 }
3133 }
3134
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) {
3138 if (pkt->size > 0)
3139 ret = av_packet_ref(mkv->cur_audio_pkt, pkt);
3140 } else
3141 ret = mkv_write_packet_internal(s, pkt);
3142 return ret;
3143 }
3144
3145 static int mkv_write_flush_packet(AVFormatContext *s, AVPacket *pkt)
3146 {
3147 MatroskaMuxContext *mkv = s->priv_data;
3148
3149 if (!pkt) {
3150 if (mkv->cluster_pos != -1) {
3151 int ret = mkv_end_cluster(s);
3152 if (ret < 0)
3153 return ret;
3154 av_log(s, AV_LOG_DEBUG,
3155 "Flushing cluster at offset %" PRIu64 " bytes\n",
3156 avio_tell(s->pb));
3157 }
3158 return 1;
3159 }
3160 return mkv_write_packet(s, pkt);
3161 }
3162
3163 static int mkv_write_trailer(AVFormatContext *s)
3164 {
3165 MatroskaMuxContext *mkv = s->priv_data;
3166 AVIOContext *pb = s->pb;
3167 int64_t endpos, ret64;
3168 int ret, ret2 = 0;
3169
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);
3173 if (ret < 0) {
3174 av_log(s, AV_LOG_ERROR,
3175 "Could not write cached audio packet ret:%d\n", ret);
3176 return ret;
3177 }
3178 }
3179
3180 if (mkv->cluster_pos != -1) {
3181 ret = end_ebml_master_crc32(pb, &mkv->cluster_bc, mkv,
3182 MATROSKA_ID_CLUSTER, 0, 0, 0);
3183 if (ret < 0)
3184 return ret;
3185 }
3186
3187 ret = mkv_write_chapters(s);
3188 if (ret < 0)
3189 return ret;
3190
3191 if (!IS_SEEKABLE(pb, mkv))
3192 return 0;
3193
3194 endpos = avio_tell(pb);
3195
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;
3200
3201 redo_cues:
3202 ret = start_ebml_master_crc32(&cues, mkv);
3203 if (ret < 0)
3204 return ret;
3205
3206 ret = mkv_assemble_cues(s->streams, cues, mkv->tmp_bc, &mkv->cues,
3207 mkv->tracks, s->nb_streams, offset);
3208 if (ret < 0) {
3209 ffio_free_dyn_buf(&cues);
3210 return ret;
3211 }
3212
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);
3221 goto redo_cues;
3222 }
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);
3228 goto after_cues;
3229 } else {
3230 if (offset) {
3231 ret = ff_format_shift_data(s, mkv->cues_pos + mkv->reserve_cues_space,
3232 offset);
3233 if (ret < 0) {
3234 ffio_free_dyn_buf(&cues);
3235 return ret;
3236 }
3237 endpos += offset;
3238 }
3239 if ((ret64 = avio_seek(pb, mkv->cues_pos, SEEK_SET)) < 0) {
3240 ffio_free_dyn_buf(&cues);
3241 return ret64;
3242 }
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. */
3249 length_size++;
3250 size++;
3251 }
3252 }
3253 }
3254 ret = end_ebml_master_crc32(pb, &cues, mkv, MATROSKA_ID_CUES,
3255 length_size, 0, 1);
3256 if (ret < 0)
3257 return ret;
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);
3263 }
3264
3265 after_cues:
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)
3270 return ret64;
3271 put_ebml_length(pb, endpos - mkv->segment_offset, 8);
3272 }
3273
3274 ret = mkv_write_seekhead(pb, mkv, 1, mkv->info.pos);
3275 if (ret < 0)
3276 return ret;
3277
3278 if (mkv->info.bc) {
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);
3285 if (ret < 0)
3286 return ret;
3287 }
3288
3289 if (mkv->track.bc) {
3290 // write Tracks master
3291 if (!IS_WEBM(mkv)) {
3292 AVIOContext *track_bc = mkv->track.bc;
3293
3294 for (unsigned i = 0; i < s->nb_streams; i++) {
3295 const mkv_track *track = &mkv->tracks[i];
3296
3297 if (!track->max_blockaddid)
3298 continue;
3299
3300 // We reserved a single byte to write this value.
3301 av_assert0(track->max_blockaddid <= 0xFF);
3302
3303 avio_seek(track_bc, track->blockadditionmapping_offset, SEEK_SET);
3304
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);
3314 }
3315 }
3316 }
3317
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);
3321 if (ret < 0)
3322 return ret;
3323 }
3324
3325 // update stream durations
3326 if (mkv->tags.bc) {
3327 AVIOContext *tags_bc = mkv->tags.bc;
3328 int i;
3329 for (i = 0; i < s->nb_streams; ++i) {
3330 const AVStream *st = s->streams[i];
3331 const mkv_track *track = &mkv->tracks[i];
3332
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;
3337
3338 av_log(s, AV_LOG_DEBUG, "stream %d end duration = %" PRIu64 "\n", i,
3339 track->duration);
3340
3341 avio_seek(tags_bc, track->duration_offset, SEEK_SET);
3342 simpletag = start_ebml_master(tags_bc, MATROSKA_ID_SIMPLETAG,
3343 2 + 1 + 8 + 23);
3344 put_ebml_string(tags_bc, MATROSKA_ID_TAGNAME, "DURATION");
3345
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));
3349
3350 put_ebml_binary(tags_bc, MATROSKA_ID_TAGSTRING,
3351 duration_string, DURATION_STRING_LENGTH);
3352 end_ebml_master(tags_bc, simpletag);
3353 }
3354 }
3355
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);
3359 if (ret < 0)
3360 return ret;
3361 }
3362
3363 avio_seek(pb, endpos, SEEK_SET);
3364
3365 return ret2;
3366 }
3367
3368 static uint64_t mkv_get_uid(const mkv_track *tracks, int i, AVLFG *c)
3369 {
3370 while (1) {
3371 uint64_t uid;
3372 int k;
3373 uid = (uint64_t)av_lfg_get(c) << 32;
3374 uid |= av_lfg_get(c);
3375 if (!uid)
3376 continue;
3377 for (k = 0; k < i; k++) {
3378 if (tracks[k].uid == uid)
3379 break;
3380 }
3381 if (k == i)
3382 return uid;
3383 }
3384 }
3385
3386 static int mkv_init(struct AVFormatContext *s)
3387 {
3388 FFFormatContext *const si = ffformatcontext(s);
3389 MatroskaMuxContext *mkv = s->priv_data;
3390 AVLFG c;
3391 unsigned nb_tracks = 0;
3392 int i;
3393
3394 mkv->ctx = s;
3395
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;
3408 }
3409 }
3410
3411 if (s->avoid_negative_ts < 0) {
3412 s->avoid_negative_ts = 1;
3413 si->avoid_negative_ts_use_pts = 1;
3414 }
3415
3416 if (!CONFIG_MATROSKA_MUXER ||
3417 (CONFIG_WEBM_MUXER && !strcmp(s->oformat->name, "webm"))) {
3418 mkv->mode = MODE_WEBM;
3419 mkv->write_crc = 0;
3420 } else
3421 mkv->mode = MODE_MATROSKAv2;
3422
3423 mkv->cur_audio_pkt = ffformatcontext(s)->pkt;
3424
3425 mkv->tracks = av_calloc(s->nb_streams, sizeof(*mkv->tracks));
3426 if (!mkv->tracks)
3427 return AVERROR(ENOMEM);
3428
3429 if (!(s->flags & AVFMT_FLAG_BITEXACT)) {
3430 av_lfg_init(&c, av_get_random_seed());
3431
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);
3435 }
3436
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];
3441
3442 switch (par->codec_id) {
3443 #if CONFIG_MATROSKA_MUXER
3444 case AV_CODEC_ID_WAVPACK:
3445 track->reformat = mkv_reformat_wavpack;
3446 break;
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;
3455 break;
3456 case AV_CODEC_ID_PRORES:
3457 /* Matroska specification requires to remove
3458 * the first QuickTime atom. */
3459 track->offset = 8;
3460 break;
3461 #endif
3462 case AV_CODEC_ID_AV1:
3463 track->reformat = mkv_reformat_av1;
3464 break;
3465 case AV_CODEC_ID_WEBVTT:
3466 track->reformat = webm_reformat_vtt;
3467 break;
3468 }
3469
3470 if (s->flags & AVFMT_FLAG_BITEXACT) {
3471 track->uid = i + 1;
3472 } else {
3473 track->uid = mkv_get_uid(mkv->tracks, i, &c);
3474 }
3475
3476 // ms precision is the de-facto standard timescale for mkv files
3477 avpriv_set_pts_info(st, 64, 1, 1000);
3478
3479 if (st->codecpar->codec_type == AVMEDIA_TYPE_ATTACHMENT) {
3480 if (IS_WEBM(mkv)) {
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);
3487 }
3488 mkv->nb_attachments++;
3489 continue;
3490 }
3491
3492 nb_tracks++;
3493 track->track_num = mkv->is_dash ? mkv->dash_track_number : nb_tracks;
3494 track->track_num_size = ebml_num_size(track->track_num);
3495 }
3496
3497 if (mkv->is_dash && nb_tracks != 1)
3498 return AVERROR(EINVAL);
3499
3500 return 0;
3501 }
3502
3503 static int mkv_check_bitstream(AVFormatContext *s, AVStream *st,
3504 const AVPacket *pkt)
3505 {
3506 int ret = 1;
3507
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);
3516 }
3517
3518 return ret;
3519 }
3520
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 }
3534 };
3535
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 }
3542 };
3543
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" },
3561 { NULL },
3562 };
3563
3564 static const AVClass matroska_webm_class = {
3565 .class_name = "matroska/webm muxer",
3566 .item_name = av_default_item_name,
3567 .option = options,
3568 .version = LIBAVUTIL_VERSION_INT,
3569 };
3570
3571 #if CONFIG_MATROSKA_MUXER
3572 static int mkv_query_codec(enum AVCodecID codec_id, int std_compliance)
3573 {
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)
3576 return 1;
3577
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)
3582 return 1;
3583 }
3584
3585 return 0;
3586 }
3587
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,
3598 .init = mkv_init,
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 |
3604 AVFMT_TS_NONSTRICT,
3605 .p.codec_tag = (const AVCodecTag* const []){
3606 ff_codec_bmp_tags, ff_codec_wav_tags,
3607 additional_audio_tags, additional_subtitle_tags, 0
3608 },
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,
3614 };
3615 #endif
3616
3617 #if CONFIG_WEBM_MUXER
3618 static int webm_query_codec(enum AVCodecID codec_id, int std_compliance)
3619 {
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)
3622 return 1;
3623
3624 return 0;
3625 }
3626
3627 const FFOutputFormat ff_webm_muxer = {
3628 .p.name = "webm",
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,
3636 .init = mkv_init,
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 |
3644 AVFMT_TS_NONSTRICT,
3645 .p.priv_class = &matroska_webm_class,
3646 .flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH,
3647 };
3648 #endif
3649
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,
3660 .init = mkv_init,
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
3669 },
3670 .p.priv_class = &matroska_webm_class,
3671 .flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH,
3672 };
3673 #endif