FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/matroskaenc.c
Date: 2025-03-08 20:38:41
Exec Total Coverage
Lines: 1616 1880 86.0%
Functions: 96 96 100.0%
Branches: 891 1203 74.1%

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