FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/matroskaenc.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 1613 1876 86.0%
Functions: 96 96 100.0%
Branches: 887 1198 74.0%

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