FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/matroskaenc.c
Date: 2022-12-09 07:38:14
Exec Total Coverage
Lines: 1501 1778 84.4%
Functions: 92 92 100.0%
Branches: 810 1147 70.6%

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 "matroska.h"
37 #include "mux.h"
38 #include "riff.h"
39 #include "version.h"
40 #include "vorbiscomment.h"
41 #include "wv.h"
42
43 #include "libavutil/avstring.h"
44 #include "libavutil/channel_layout.h"
45 #include "libavutil/crc.h"
46 #include "libavutil/dict.h"
47 #include "libavutil/intfloat.h"
48 #include "libavutil/intreadwrite.h"
49 #include "libavutil/lfg.h"
50 #include "libavutil/mastering_display_metadata.h"
51 #include "libavutil/mathematics.h"
52 #include "libavutil/opt.h"
53 #include "libavutil/parseutils.h"
54 #include "libavutil/pixdesc.h"
55 #include "libavutil/random_seed.h"
56 #include "libavutil/rational.h"
57 #include "libavutil/samplefmt.h"
58 #include "libavutil/stereo3d.h"
59
60 #include "libavcodec/av1.h"
61 #include "libavcodec/codec_desc.h"
62 #include "libavcodec/xiph.h"
63 #include "libavcodec/mpeg4audio.h"
64
65 /* Level 1 elements we create a SeekHead entry for:
66 * Info, Tracks, Chapters, Attachments, Tags (potentially twice) and Cues */
67 #define MAX_SEEKHEAD_ENTRIES 7
68
69 /* Largest known-length EBML length */
70 #define MAX_EBML_LENGTH ((1ULL << 56) - 2)
71 /* The dynamic buffer API we rely upon has a limit of INT_MAX;
72 * and so has avio_write(). */
73 #define MAX_SUPPORTED_EBML_LENGTH FFMIN(MAX_EBML_LENGTH, INT_MAX)
74
75 #define MODE_MATROSKAv2 0x01
76 #define MODE_WEBM 0x02
77
78 #define IS_WEBM(mkv) (CONFIG_WEBM_MUXER && CONFIG_MATROSKA_MUXER ? \
79 ((mkv)->mode == MODE_WEBM) : CONFIG_WEBM_MUXER)
80 #define IS_SEEKABLE(pb, mkv) (((pb)->seekable & AVIO_SEEKABLE_NORMAL) && \
81 !(mkv)->is_live)
82
83 enum {
84 DEFAULT_MODE_INFER,
85 DEFAULT_MODE_INFER_NO_SUBS,
86 DEFAULT_MODE_PASSTHROUGH,
87 };
88
89 typedef struct ebml_master {
90 int64_t pos; ///< absolute offset in the containing AVIOContext where the master's elements start
91 int sizebytes; ///< how many bytes were reserved for the size
92 } ebml_master;
93
94 typedef struct ebml_stored_master {
95 AVIOContext *bc;
96 int64_t pos;
97 } ebml_stored_master;
98
99 typedef enum EbmlType {
100 EBML_UINT,
101 EBML_SINT,
102 EBML_FLOAT,
103 EBML_UID,
104 EBML_STR,
105 EBML_UTF8 = EBML_STR,
106 EBML_BIN,
107 EBML_BLOCK, ///< pseudo-type for writing (Simple)Blocks
108 EBML_MASTER,
109 } EbmlType;
110
111 typedef struct BlockContext {
112 struct mkv_track *track;
113 const AVPacket *pkt;
114 int16_t rel_ts;
115 uint8_t flags;
116 NALUList h2645_nalu_list;
117 } BlockContext;
118
119 typedef struct EbmlMaster {
120 int nb_elements; ///< -1 if not finished
121 int containing_master; ///< -1 if no parent exists
122 } EbmlMaster;
123
124 typedef struct EbmlElement {
125 uint32_t id;
126 EbmlType type;
127 unsigned length_size;
128 uint64_t size; ///< excluding id and length field
129 union {
130 uint64_t uint;
131 int64_t sint;
132 double f;
133 const char *str;
134 const uint8_t *bin;
135 struct MatroskaMuxContext *mkv; ///< used by EBML_BLOCK
136 EbmlMaster master;
137 } priv;
138 } EbmlElement;
139
140 typedef struct EbmlWriter {
141 unsigned nb_elements;
142 int current_master_element;
143 EbmlElement *elements;
144 } EbmlWriter;
145
146 #define EBML_WRITER(max_nb_elems) \
147 EbmlElement elements[max_nb_elems]; \
148 EbmlWriter writer = (EbmlWriter){ .elements = elements, \
149 .current_master_element = -1 }
150
151 typedef struct mkv_seekhead_entry {
152 uint32_t elementid;
153 uint64_t segmentpos;
154 } mkv_seekhead_entry;
155
156 typedef struct mkv_seekhead {
157 int64_t filepos;
158 mkv_seekhead_entry entries[MAX_SEEKHEAD_ENTRIES];
159 int num_entries;
160 int reserved_size;
161 } mkv_seekhead;
162
163 typedef struct mkv_cuepoint {
164 uint64_t pts;
165 int stream_idx;
166 int64_t cluster_pos; ///< offset of the cluster containing the block relative to the segment
167 int64_t relative_pos; ///< relative offset from the position of the cluster containing the block
168 int64_t duration; ///< duration of the block according to time base
169 } mkv_cuepoint;
170
171 typedef struct mkv_cues {
172 mkv_cuepoint *entries;
173 int num_entries;
174 } mkv_cues;
175
176 struct MatroskaMuxContext;
177
178 typedef struct mkv_track {
179 int write_dts;
180 int has_cue;
181 uint64_t uid;
182 unsigned track_num;
183 int track_num_size;
184 int sample_rate;
185 unsigned offset;
186 int64_t sample_rate_offset;
187 int64_t last_timestamp;
188 int64_t duration;
189 int64_t duration_offset;
190 int codecpriv_offset;
191 unsigned codecpriv_size; ///< size reserved for CodecPrivate excluding header+length field
192 int64_t ts_offset;
193 /* This callback will be called twice: First with a NULL AVIOContext
194 * to return the size of the (Simple)Block's data via size
195 * and a second time with the AVIOContext set when the data
196 * shall be written.
197 * The callback shall not return an error on the second call. */
198 int (*reformat)(struct MatroskaMuxContext *, AVIOContext *,
199 const AVPacket *, int *size);
200 } mkv_track;
201
202 typedef struct MatroskaMuxContext {
203 const AVClass *class;
204 AVFormatContext *ctx;
205
206 int mode;
207 ebml_stored_master info;
208 ebml_stored_master track;
209 ebml_stored_master tags;
210 int64_t segment_offset;
211 AVIOContext *cluster_bc;
212 int64_t cluster_pos; ///< file offset of the current Cluster
213 int64_t cluster_pts;
214 int64_t duration_offset;
215 int64_t duration;
216 mkv_track *tracks;
217 mkv_seekhead seekhead;
218 mkv_cues cues;
219 int64_t cues_pos;
220
221 BlockContext cur_block;
222
223 /* Used as temporary buffer to use the minimal amount of bytes
224 * to write the length field of EBML Masters.
225 * Every user has to reset the buffer after using it and
226 * different uses may not overlap. It is currently used in
227 * mkv_write_tag(), in mkv_assemble_cues() as well as in
228 * mkv_update_codecprivate() and mkv_write_track(). */
229 AVIOContext *tmp_bc;
230
231 AVPacket *cur_audio_pkt;
232
233 unsigned nb_attachments;
234 int have_video;
235
236 int wrote_chapters;
237 int wrote_tags;
238
239 int reserve_cues_space;
240 int cluster_size_limit;
241 int64_t cluster_time_limit;
242 int write_crc;
243 int is_live;
244
245 int is_dash;
246 int dash_track_number;
247 int allow_raw_vfw;
248 int flipped_raw_rgb;
249 int default_mode;
250 int move_cues_to_front;
251
252 uint32_t segment_uid[4];
253 } MatroskaMuxContext;
254
255 /** 2 bytes * 3 for EBML IDs, 3 1-byte EBML lengths, 8 bytes for 64 bit
256 * offset, 4 bytes for target EBML ID */
257 #define MAX_SEEKENTRY_SIZE 21
258
259 /** 4 * (1-byte EBML ID, 1-byte EBML size, 8-byte uint max) */
260 #define MAX_CUETRACKPOS_SIZE 40
261
262 /** 2 + 1 Simpletag header, 2 + 1 + 8 Name "DURATION", 23B for TagString */
263 #define DURATION_SIMPLETAG_SIZE (2 + 1 + (2 + 1 + 8) + 23)
264
265 /** Seek preroll value for opus */
266 #define OPUS_SEEK_PREROLL 80000000
267
268 113661 static int ebml_id_size(uint32_t id)
269 {
270 113661 return (av_log2(id) + 7U) / 8;
271 }
272
273 111945 static void put_ebml_id(AVIOContext *pb, uint32_t id)
274 {
275 111945 int i = ebml_id_size(id);
276
2/2
✓ Branch 0 taken 124921 times.
✓ Branch 1 taken 111945 times.
236866 while (i--)
277 124921 avio_w8(pb, (uint8_t)(id >> (i * 8)));
278 111945 }
279
280 /**
281 * Write an EBML size meaning "unknown size".
282 *
283 * @param bytes The number of bytes the size should occupy (maximum: 8).
284 */
285 18221 static void put_ebml_size_unknown(AVIOContext *pb, int bytes)
286 {
287
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18221 times.
18221 av_assert0(bytes <= 8);
288 18221 avio_w8(pb, 0x1ff >> bytes);
289 if (av_builtin_constant_p(bytes) && bytes == 1)
290 return;
291 18221 ffio_fill(pb, 0xff, bytes - 1);
292 }
293
294 /**
295 * Returns how many bytes are needed to represent a number
296 * as EBML variable length integer.
297 */
298 130074 static int ebml_num_size(uint64_t num)
299 {
300 130074 int bytes = 0;
301 do {
302 143466 bytes++;
303
2/2
✓ Branch 0 taken 13392 times.
✓ Branch 1 taken 130074 times.
143466 } while (num >>= 7);
304 130074 return bytes;
305 }
306
307 /**
308 * Calculate how many bytes are needed to represent the length field
309 * of an EBML element whose payload has a given length.
310 */
311 130013 static int ebml_length_size(uint64_t length)
312 {
313 130013 return ebml_num_size(length + 1);
314 }
315
316 /**
317 * Write a number as EBML variable length integer on `bytes` bytes.
318 * `bytes` is taken literally without checking.
319 */
320 122729 static void put_ebml_num(AVIOContext *pb, uint64_t num, int bytes)
321 {
322 122729 num |= 1ULL << bytes * 7;
323
2/2
✓ Branch 0 taken 137752 times.
✓ Branch 1 taken 122729 times.
260481 for (int i = bytes - 1; i >= 0; i--)
324 137752 avio_w8(pb, (uint8_t)(num >> i * 8));
325 122729 }
326
327 /**
328 * Write a length as EBML variable length integer.
329 *
330 * @param bytes The number of bytes that need to be used to write the number.
331 * If zero, the minimal number of bytes will be used.
332 */
333 99133 static void put_ebml_length(AVIOContext *pb, uint64_t length, int bytes)
334 {
335 99133 int needed_bytes = ebml_length_size(length);
336
337 // sizes larger than this are currently undefined in EBML
338
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 99133 times.
99133 av_assert0(length < (1ULL << 56) - 1);
339
340
2/2
✓ Branch 0 taken 80712 times.
✓ Branch 1 taken 18421 times.
99133 if (bytes == 0)
341 80712 bytes = needed_bytes;
342 // The bytes needed to write the given size must not exceed
343 // the bytes that we ought to use.
344
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 99133 times.
99133 av_assert0(bytes >= needed_bytes);
345 99133 put_ebml_num(pb, length, bytes);
346 99133 }
347
348 /**
349 * Write a (random) UID with fixed size to make the output more deterministic
350 */
351 123 static void put_ebml_uid(AVIOContext *pb, uint32_t elementid, uint64_t uid)
352 {
353 123 put_ebml_id(pb, elementid);
354 123 put_ebml_length(pb, 8, 0);
355 123 avio_wb64(pb, uid);
356 123 }
357
358 66402 static void put_ebml_uint(AVIOContext *pb, uint32_t elementid, uint64_t val)
359 {
360 66402 int i, bytes = 1;
361 66402 uint64_t tmp = val;
362
2/2
✓ Branch 0 taken 61422 times.
✓ Branch 1 taken 66402 times.
127824 while (tmp >>= 8)
363 61422 bytes++;
364
365 66402 put_ebml_id(pb, elementid);
366 66402 put_ebml_length(pb, bytes, 0);
367
2/2
✓ Branch 0 taken 127824 times.
✓ Branch 1 taken 66402 times.
194226 for (i = bytes - 1; i >= 0; i--)
368 127824 avio_w8(pb, (uint8_t)(val >> i * 8));
369 66402 }
370
371 67 static void put_ebml_float(AVIOContext *pb, uint32_t elementid, double val)
372 {
373 67 put_ebml_id(pb, elementid);
374 67 put_ebml_length(pb, 8, 0);
375 67 avio_wb64(pb, av_double2int(val));
376 67 }
377
378 10124 static void put_ebml_binary(AVIOContext *pb, uint32_t elementid,
379 const void *buf, int size)
380 {
381 10124 put_ebml_id(pb, elementid);
382 10124 put_ebml_length(pb, size, 0);
383 10124 avio_write(pb, buf, size);
384 10124 }
385
386 259 static void put_ebml_string(AVIOContext *pb, uint32_t elementid,
387 const char *str)
388 {
389 259 put_ebml_binary(pb, elementid, str, strlen(str));
390 259 }
391
392 /**
393 * Write a void element of a given size. Useful for reserving space in
394 * the file to be written to later.
395 *
396 * @param size The number of bytes to reserve, which must be at least 2.
397 */
398 622 static void put_ebml_void(AVIOContext *pb, int size)
399 {
400
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 622 times.
622 av_assert0(size >= 2);
401
402 622 put_ebml_id(pb, EBML_ID_VOID);
403 // we need to subtract the length needed to store the size from the
404 // size we need to reserve so 2 cases, we use 8 bytes to store the
405 // size if possible, 1 byte otherwise
406
2/2
✓ Branch 0 taken 460 times.
✓ Branch 1 taken 162 times.
622 if (size < 10) {
407 460 size -= 2;
408 460 put_ebml_length(pb, size, 0);
409 } else {
410 162 size -= 9;
411 162 put_ebml_length(pb, size, 8);
412 }
413 622 ffio_fill(pb, 0, size);
414 622 }
415
416 18184 static ebml_master start_ebml_master(AVIOContext *pb, uint32_t elementid,
417 uint64_t expectedsize)
418 {
419
2/2
✓ Branch 0 taken 18122 times.
✓ Branch 1 taken 62 times.
18184 int bytes = expectedsize ? ebml_length_size(expectedsize) : 8;
420
421 18184 put_ebml_id(pb, elementid);
422 18184 put_ebml_size_unknown(pb, bytes);
423 18184 return (ebml_master) { avio_tell(pb), bytes };
424 }
425
426 18184 static void end_ebml_master(AVIOContext *pb, ebml_master master)
427 {
428 18184 int64_t pos = avio_tell(pb);
429
430
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 18184 times.
18184 if (avio_seek(pb, master.pos - master.sizebytes, SEEK_SET) < 0)
431 return;
432 18184 put_ebml_length(pb, pos - master.pos, master.sizebytes);
433 18184 avio_seek(pb, pos, SEEK_SET);
434 }
435
436 23390 static EbmlElement *ebml_writer_add(EbmlWriter *writer,
437 uint32_t id, EbmlType type)
438 {
439 23390 writer->elements[writer->nb_elements].id = id;
440 23390 writer->elements[writer->nb_elements].type = type;
441 23390 return &writer->elements[writer->nb_elements++];
442 }
443
444 11357 static void ebml_writer_open_master(EbmlWriter *writer, uint32_t id)
445 {
446 11357 EbmlElement *const elem = ebml_writer_add(writer, id, EBML_MASTER);
447 11357 EbmlMaster *const master = &elem->priv.master;
448
449 11357 master->containing_master = writer->current_master_element;
450 11357 master->nb_elements = -1;
451
452 11357 writer->current_master_element = writer->nb_elements - 1;
453 11357 }
454
455 257 static void ebml_writer_close_master(EbmlWriter *writer)
456 {
457 EbmlElement *elem;
458 av_assert2(writer->current_master_element >= 0);
459 av_assert2(writer->current_master_element < writer->nb_elements);
460 257 elem = &writer->elements[writer->current_master_element];
461 av_assert2(elem->type == EBML_MASTER);
462 av_assert2(elem->priv.master.nb_elements < 0); /* means unset */
463 257 elem->priv.master.nb_elements = writer->nb_elements - writer->current_master_element - 1;
464 av_assert2(elem->priv.master.containing_master < 0 ||
465 elem->priv.master.containing_master < writer->current_master_element);
466 257 writer->current_master_element = elem->priv.master.containing_master;
467 257 }
468
469 23 static void ebml_writer_close_or_discard_master(EbmlWriter *writer)
470 {
471 av_assert2(writer->nb_elements > 0);
472 av_assert2(0 <= writer->current_master_element);
473 av_assert2(writer->current_master_element < writer->nb_elements);
474
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 15 times.
23 if (writer->current_master_element == writer->nb_elements - 1) {
475 8 const EbmlElement *const elem = &writer->elements[writer->nb_elements - 1];
476 /* The master element has no children. Discard it. */
477 av_assert2(elem->type == EBML_MASTER);
478 av_assert2(elem->priv.master.containing_master < 0 ||
479 elem->priv.master.containing_master < writer->current_master_element);
480 8 writer->current_master_element = elem->priv.master.containing_master;
481 8 writer->nb_elements--;
482 8 return;
483 }
484 15 ebml_writer_close_master(writer);
485 }
486
487 237 static void ebml_writer_add_string(EbmlWriter *writer, uint32_t id,
488 const char *str)
489 {
490 237 EbmlElement *const elem = ebml_writer_add(writer, id, EBML_STR);
491
492 237 elem->priv.str = str;
493 237 }
494
495 125 static void ebml_writer_add_bin(EbmlWriter *writer, uint32_t id,
496 const uint8_t *data, size_t size)
497 {
498 125 EbmlElement *const elem = ebml_writer_add(writer, id, EBML_BIN);
499
500 #if SIZE_MAX > UINT64_MAX
501 size = FFMIN(size, UINT64_MAX);
502 #endif
503 125 elem->size = size;
504 125 elem->priv.bin = data;
505 125 }
506
507 26 static void ebml_writer_add_float(EbmlWriter *writer, uint32_t id,
508 double val)
509 {
510 26 EbmlElement *const elem = ebml_writer_add(writer, id, EBML_FLOAT);
511
512 26 elem->priv.f = val;
513 26 }
514
515 1 static void ebml_writer_add_uid(EbmlWriter *writer, uint32_t id,
516 uint64_t val)
517 {
518 1 EbmlElement *const elem = ebml_writer_add(writer, id, EBML_UID);
519 1 elem->priv.uint = val;
520 1 }
521
522 592 static void ebml_writer_add_uint(EbmlWriter *writer, uint32_t id,
523 uint64_t val)
524 {
525 592 EbmlElement *elem = ebml_writer_add(writer, id, EBML_UINT);
526 592 elem->priv.uint = val;
527 592 }
528
529 122 static void ebml_writer_add_sint(EbmlWriter *writer, uint32_t id,
530 int64_t val)
531 {
532 122 EbmlElement *elem = ebml_writer_add(writer, id, EBML_SINT);
533 122 elem->priv.sint = val;
534 122 }
535
536 10930 static void ebml_writer_add_block(EbmlWriter *writer, MatroskaMuxContext *mkv)
537 {
538 10930 EbmlElement *elem = ebml_writer_add(writer, MATROSKA_ID_BLOCK, EBML_BLOCK);
539 10930 elem->priv.mkv = mkv;
540 10930 }
541
542 237 static int ebml_writer_str_len(EbmlElement *elem)
543 {
544 237 size_t len = strlen(elem->priv.str);
545 #if SIZE_MAX > UINT64_MAX
546 len = FF_MIN(len, UINT64_MAX);
547 #endif
548 237 elem->size = len;
549 237 return 0;
550 }
551
552 714 static av_const int uint_size(uint64_t val)
553 {
554 714 int bytes = 0;
555 do {
556 873 bytes++;
557
2/2
✓ Branch 0 taken 159 times.
✓ Branch 1 taken 714 times.
873 } while (val >>= 8);
558 714 return bytes;
559 }
560
561 592 static int ebml_writer_uint_len(EbmlElement *elem)
562 {
563 592 elem->size = uint_size(elem->priv.uint);
564 592 return 0;
565 }
566
567 122 static av_const int sint_size(int64_t val)
568 {
569
2/2
✓ Branch 0 taken 119 times.
✓ Branch 1 taken 3 times.
122 uint64_t tmp = 2 * (uint64_t)(val < 0 ? val^-1 : val);
570 122 return uint_size(tmp);
571 }
572
573 122 static int ebml_writer_sint_len(EbmlElement *elem)
574 {
575 122 elem->size = sint_size(elem->priv.sint);
576 122 return 0;
577 }
578
579 static int ebml_writer_elem_len(EbmlWriter *writer, EbmlElement *elem,
580 int remaining_elems);
581
582 633 static int ebml_writer_master_len(EbmlWriter *writer, EbmlElement *elem,
583 int remaining_elems)
584 {
585
2/2
✓ Branch 0 taken 257 times.
✓ Branch 1 taken 376 times.
633 int nb_elems = elem->priv.master.nb_elements >= 0 ? elem->priv.master.nb_elements : remaining_elems - 1;
586 633 EbmlElement *const master = elem;
587 633 uint64_t total_size = 0;
588
589 633 master->priv.master.nb_elements = nb_elems;
590
2/2
✓ Branch 0 taken 1578 times.
✓ Branch 1 taken 633 times.
2211 for (; elem++, nb_elems > 0;) {
591 1578 int ret = ebml_writer_elem_len(writer, elem, nb_elems);
592
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1578 times.
1578 if (ret < 0)
593 return ret;
594 av_assert2(ret < nb_elems);
595 /* No overflow is possible here, as both total_size and elem->size
596 * are bounded by MAX_SUPPORTED_EBML_LENGTH. */
597 1578 total_size += ebml_id_size(elem->id) + elem->length_size + elem->size;
598
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1578 times.
1578 if (total_size > MAX_SUPPORTED_EBML_LENGTH)
599 return AVERROR(ERANGE);
600 1578 nb_elems--; /* consume elem */
601 1578 elem += ret, nb_elems -= ret; /* and elem's children */
602 }
603 633 master->size = total_size;
604
605 633 return master->priv.master.nb_elements;
606 }
607
608 10930 static int ebml_writer_block_len(EbmlElement *elem)
609 {
610 10930 MatroskaMuxContext *const mkv = elem->priv.mkv;
611 10930 BlockContext *const block = &mkv->cur_block;
612 10930 mkv_track *const track = block->track;
613 10930 const AVPacket *const pkt = block->pkt;
614 int err, size;
615
616
2/2
✓ Branch 0 taken 205 times.
✓ Branch 1 taken 10725 times.
10930 if (track->reformat) {
617 205 err = track->reformat(mkv, NULL, pkt, &size);
618
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 205 times.
205 if (err < 0) {
619 av_log(mkv->ctx, AV_LOG_ERROR, "Error when reformatting data of "
620 "a packet from stream %d.\n", pkt->stream_index);
621 return err;
622 }
623 } else {
624 10725 size = pkt->size;
625
1/2
✓ Branch 0 taken 10725 times.
✗ Branch 1 not taken.
10725 if (track->offset <= size)
626 10725 size -= track->offset;
627 }
628 10930 elem->size = track->track_num_size + 3U + size;
629
630 10930 return 0;
631 }
632
633 10930 static void ebml_writer_write_block(const EbmlElement *elem, AVIOContext *pb)
634 {
635 10930 MatroskaMuxContext *const mkv = elem->priv.mkv;
636 10930 BlockContext *const block = &mkv->cur_block;
637 10930 mkv_track *const track = block->track;
638 10930 const AVPacket *const pkt = block->pkt;
639
640 10930 put_ebml_num(pb, track->track_num, track->track_num_size);
641 10930 avio_wb16(pb, block->rel_ts);
642 10930 avio_w8(pb, block->flags);
643
644
2/2
✓ Branch 0 taken 205 times.
✓ Branch 1 taken 10725 times.
10930 if (track->reformat) {
645 int size;
646 205 track->reformat(mkv, pb, pkt, &size);
647 } else {
648 10725 const uint8_t *data = pkt->data;
649
1/2
✓ Branch 0 taken 10725 times.
✗ Branch 1 not taken.
10725 unsigned offset = track->offset <= pkt->size ? track->offset : 0;
650 10725 avio_write(pb, data + offset, pkt->size - offset);
651 }
652 10930 }
653
654 12666 static int ebml_writer_elem_len(EbmlWriter *writer, EbmlElement *elem,
655 int remaining_elems)
656 {
657 12666 int ret = 0;
658
659
7/7
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 237 times.
✓ Branch 2 taken 592 times.
✓ Branch 3 taken 122 times.
✓ Branch 4 taken 10930 times.
✓ Branch 5 taken 633 times.
✓ Branch 6 taken 125 times.
12666 switch (elem->type) {
660 27 case EBML_FLOAT:
661 case EBML_UID:
662 27 elem->size = 8;
663 27 break;
664 237 case EBML_STR:
665 237 ret = ebml_writer_str_len(elem);
666 237 break;
667 592 case EBML_UINT:
668 592 ret = ebml_writer_uint_len(elem);
669 592 break;
670 122 case EBML_SINT:
671 122 ret = ebml_writer_sint_len(elem);
672 122 break;
673 10930 case EBML_BLOCK:
674 10930 ret = ebml_writer_block_len(elem);
675 10930 break;
676 633 case EBML_MASTER:
677 633 ret = ebml_writer_master_len(writer, elem, remaining_elems);
678 633 break;
679 }
680
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12666 times.
12666 if (ret < 0)
681 return ret;
682
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12666 times.
12666 if (elem->size > MAX_SUPPORTED_EBML_LENGTH)
683 return AVERROR(ERANGE);
684 12666 elem->length_size = ebml_length_size(elem->size);
685 12666 return ret; /* number of elements consumed excluding elem itself */
686 }
687
688 12666 static int ebml_writer_elem_write(const EbmlElement *elem, AVIOContext *pb)
689 {
690 12666 put_ebml_id(pb, elem->id);
691 12666 put_ebml_num(pb, elem->size, elem->length_size);
692
5/6
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 714 times.
✓ Branch 2 taken 362 times.
✓ Branch 3 taken 10930 times.
✓ Branch 4 taken 633 times.
✗ Branch 5 not taken.
12666 switch (elem->type) {
693 27 case EBML_UID:
694 case EBML_FLOAT: {
695 54 uint64_t val = elem->type == EBML_UID ? elem->priv.uint
696
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 26 times.
27 : av_double2int(elem->priv.f);
697 27 avio_wb64(pb, val);
698 27 break;
699 }
700 714 case EBML_UINT:
701 case EBML_SINT: {
702 1428 uint64_t val = elem->type == EBML_UINT ? elem->priv.uint
703
2/2
✓ Branch 0 taken 592 times.
✓ Branch 1 taken 122 times.
714 : elem->priv.sint;
704
2/2
✓ Branch 0 taken 873 times.
✓ Branch 1 taken 714 times.
1587 for (int i = elem->size; --i >= 0; )
705 873 avio_w8(pb, (uint8_t)(val >> i * 8));
706 714 break;
707 }
708 362 case EBML_STR:
709 case EBML_BIN: {
710 724 const uint8_t *data = elem->type == EBML_BIN ? elem->priv.bin
711
2/2
✓ Branch 0 taken 125 times.
✓ Branch 1 taken 237 times.
362 : (const uint8_t*)elem->priv.str;
712 362 avio_write(pb, data, elem->size);
713 362 break;
714 }
715 10930 case EBML_BLOCK:
716 10930 ebml_writer_write_block(elem, pb);
717 10930 break;
718 633 case EBML_MASTER: {
719 633 int nb_elems = elem->priv.master.nb_elements;
720
721 633 elem++;
722
2/2
✓ Branch 0 taken 1578 times.
✓ Branch 1 taken 633 times.
2211 for (int i = 0; i < nb_elems; i++)
723 1578 i += ebml_writer_elem_write(elem + i, pb);
724
725 633 return nb_elems;
726 }
727 }
728 12033 return 0;
729 }
730
731 11088 static int ebml_writer_write(EbmlWriter *writer, AVIOContext *pb)
732 {
733 11088 int ret = ebml_writer_elem_len(writer, writer->elements,
734 11088 writer->nb_elements);
735
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11088 times.
11088 if (ret < 0)
736 return ret;
737 11088 ebml_writer_elem_write(writer->elements, pb);
738 11088 return 0;
739 }
740
741 138 static void mkv_add_seekhead_entry(MatroskaMuxContext *mkv, uint32_t elementid,
742 uint64_t filepos)
743 {
744 138 mkv_seekhead *seekhead = &mkv->seekhead;
745
746 av_assert1(seekhead->num_entries < MAX_SEEKHEAD_ENTRIES);
747
748 138 seekhead->entries[seekhead->num_entries].elementid = elementid;
749 138 seekhead->entries[seekhead->num_entries++].segmentpos = filepos - mkv->segment_offset;
750 138 }
751
752 3319 static int start_ebml_master_crc32(AVIOContext **dyn_cp, MatroskaMuxContext *mkv)
753 {
754 int ret;
755
756
3/4
✓ Branch 0 taken 212 times.
✓ Branch 1 taken 3107 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 212 times.
3319 if (!*dyn_cp && (ret = avio_open_dyn_buf(dyn_cp)) < 0)
757 return ret;
758
759
2/2
✓ Branch 0 taken 459 times.
✓ Branch 1 taken 2860 times.
3319 if (mkv->write_crc)
760 459 put_ebml_void(*dyn_cp, 6); /* Reserve space for CRC32 so position/size calculations using avio_tell() take it into account */
761
762 3319 return 0;
763 }
764
765 3316 static int end_ebml_master_crc32(AVIOContext *pb, AVIOContext **dyn_cp,
766 MatroskaMuxContext *mkv, uint32_t id,
767 int length_size, int keep_buffer,
768 int add_seekentry)
769 {
770 uint8_t *buf, crc[4];
771 3316 int ret, size, skip = 0;
772
773 3316 size = avio_get_dyn_buf(*dyn_cp, &buf);
774
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3316 times.
3316 if ((ret = (*dyn_cp)->error) < 0)
775 goto fail;
776
777
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 3265 times.
3316 if (add_seekentry)
778 51 mkv_add_seekhead_entry(mkv, id, avio_tell(pb));
779
780 3316 put_ebml_id(pb, id);
781 3316 put_ebml_length(pb, size, length_size);
782
2/2
✓ Branch 0 taken 458 times.
✓ Branch 1 taken 2858 times.
3316 if (mkv->write_crc) {
783 458 skip = 6; /* Skip reserved 6-byte long void element from the dynamic buffer. */
784 458 AV_WL32(crc, av_crc(av_crc_get_table(AV_CRC_32_IEEE_LE), UINT32_MAX, buf + skip, size - skip) ^ UINT32_MAX);
785 458 put_ebml_binary(pb, EBML_ID_CRC32, crc, sizeof(crc));
786 }
787 3316 avio_write(pb, buf + skip, size - skip);
788
789 3316 fail:
790
2/2
✓ Branch 0 taken 3104 times.
✓ Branch 1 taken 212 times.
3316 if (keep_buffer) {
791 3104 ffio_reset_dyn_buf(*dyn_cp);
792 } else {
793 212 ffio_free_dyn_buf(dyn_cp);
794 }
795 3316 return ret;
796 }
797
798 /**
799 * Output EBML master. Keep the buffer if seekable, allowing for later updates.
800 * Furthermore always add a SeekHead Entry for this element.
801 */
802 107 static int end_ebml_master_crc32_tentatively(AVIOContext *pb,
803 ebml_stored_master *elem,
804 MatroskaMuxContext *mkv, uint32_t id)
805 {
806
4/4
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 87 times.
✓ Branch 3 taken 9 times.
107 if (IS_SEEKABLE(pb, mkv)) {
807 uint8_t *buf;
808 87 int size = avio_get_dyn_buf(elem->bc, &buf);
809
810
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 87 times.
87 if (elem->bc->error < 0)
811 return elem->bc->error;
812
813 87 elem->pos = avio_tell(pb);
814 87 mkv_add_seekhead_entry(mkv, id, elem->pos);
815
816 87 put_ebml_id(pb, id);
817 87 put_ebml_length(pb, size, 0);
818 87 avio_write(pb, buf, size);
819
820 87 return 0;
821 } else
822 20 return end_ebml_master_crc32(pb, &elem->bc, mkv, id, 0, 0, 1);
823 }
824
825 2 static void put_xiph_size(AVIOContext *pb, int size)
826 {
827 2 ffio_fill(pb, 255, size / 255);
828 2 avio_w8(pb, size % 255);
829 2 }
830
831 /**
832 * Free the members allocated in the mux context.
833 */
834 37 static void mkv_deinit(AVFormatContext *s)
835 {
836 37 MatroskaMuxContext *mkv = s->priv_data;
837
838 37 ffio_free_dyn_buf(&mkv->cluster_bc);
839 37 ffio_free_dyn_buf(&mkv->info.bc);
840 37 ffio_free_dyn_buf(&mkv->track.bc);
841 37 ffio_free_dyn_buf(&mkv->tags.bc);
842 37 ffio_free_dyn_buf(&mkv->tmp_bc);
843
844 37 av_freep(&mkv->cur_block.h2645_nalu_list.nalus);
845 37 av_freep(&mkv->cues.entries);
846 37 av_freep(&mkv->tracks);
847 37 }
848
849 /**
850 * Initialize the SeekHead element to be ready to index level 1 Matroska
851 * elements. Enough space to write MAX_SEEKHEAD_ENTRIES SeekHead entries
852 * will be reserved at the current file location.
853 */
854 37 static void mkv_start_seekhead(MatroskaMuxContext *mkv, AVIOContext *pb)
855 {
856 37 mkv->seekhead.filepos = avio_tell(pb);
857 // 21 bytes max for a Seek entry, 6 bytes max for the SeekHead ID
858 // and size, 6 bytes for a CRC32 element, and 2 bytes to guarantee
859 // that an EBML void element will fit afterwards
860 37 mkv->seekhead.reserved_size = MAX_SEEKHEAD_ENTRIES * MAX_SEEKENTRY_SIZE + 14;
861 37 put_ebml_void(pb, mkv->seekhead.reserved_size);
862 37 }
863
864 /**
865 * Write the SeekHead to the file at the location reserved for it
866 * and seek to destpos afterwards. When error_on_seek_failure
867 * is not set, failure to seek to the position designated for the
868 * SeekHead is not considered an error and it is presumed that
869 * destpos is the current position; failure to seek to destpos
870 * afterwards is always an error.
871 *
872 * @return 0 on success, < 0 on error.
873 */
874 37 static int mkv_write_seekhead(AVIOContext *pb, MatroskaMuxContext *mkv,
875 int error_on_seek_failure, int64_t destpos)
876 {
877 37 AVIOContext *dyn_cp = NULL;
878 37 mkv_seekhead *seekhead = &mkv->seekhead;
879 int64_t remaining, ret64;
880 int i, ret;
881
882
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 37 times.
37 if ((ret64 = avio_seek(pb, seekhead->filepos, SEEK_SET)) < 0)
883 return error_on_seek_failure ? ret64 : 0;
884
885 37 ret = start_ebml_master_crc32(&dyn_cp, mkv);
886
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 if (ret < 0)
887 return ret;
888
889
2/2
✓ Branch 0 taken 138 times.
✓ Branch 1 taken 37 times.
175 for (i = 0; i < seekhead->num_entries; i++) {
890 138 mkv_seekhead_entry *entry = &seekhead->entries[i];
891 138 ebml_master seekentry = start_ebml_master(dyn_cp, MATROSKA_ID_SEEKENTRY,
892 MAX_SEEKENTRY_SIZE);
893
894 138 put_ebml_id(dyn_cp, MATROSKA_ID_SEEKID);
895 138 put_ebml_length(dyn_cp, ebml_id_size(entry->elementid), 0);
896 138 put_ebml_id(dyn_cp, entry->elementid);
897
898 138 put_ebml_uint(dyn_cp, MATROSKA_ID_SEEKPOSITION, entry->segmentpos);
899 138 end_ebml_master(dyn_cp, seekentry);
900 }
901 37 ret = end_ebml_master_crc32(pb, &dyn_cp, mkv,
902 MATROSKA_ID_SEEKHEAD, 0, 0, 0);
903
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 if (ret < 0)
904 return ret;
905
906 37 remaining = seekhead->filepos + seekhead->reserved_size - avio_tell(pb);
907 37 put_ebml_void(pb, remaining);
908
909
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 37 times.
37 if ((ret64 = avio_seek(pb, destpos, SEEK_SET)) < 0)
910 return ret64;
911
912 37 return 0;
913 }
914
915 6539 static int mkv_add_cuepoint(MatroskaMuxContext *mkv, int stream, int64_t ts,
916 int64_t cluster_pos, int64_t relative_pos, int64_t duration)
917 {
918 6539 mkv_cues *cues = &mkv->cues;
919 6539 mkv_cuepoint *entries = cues->entries;
920 6539 unsigned idx = cues->num_entries;
921
922
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6539 times.
6539 if (ts < 0)
923 return 0;
924
925 6539 entries = av_realloc_array(entries, cues->num_entries + 1, sizeof(mkv_cuepoint));
926
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6539 times.
6539 if (!entries)
927 return AVERROR(ENOMEM);
928 6539 cues->entries = entries;
929
930 /* Make sure the cues entries are sorted by pts. */
931
3/4
✓ Branch 0 taken 6510 times.
✓ Branch 1 taken 29 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6510 times.
6539 while (idx > 0 && entries[idx - 1].pts > ts)
932 idx--;
933 6539 memmove(&entries[idx + 1], &entries[idx],
934 6539 (cues->num_entries - idx) * sizeof(entries[0]));
935
936 6539 entries[idx].pts = ts;
937 6539 entries[idx].stream_idx = stream;
938 6539 entries[idx].cluster_pos = cluster_pos - mkv->segment_offset;
939 6539 entries[idx].relative_pos = relative_pos;
940 6539 entries[idx].duration = duration;
941
942 6539 cues->num_entries++;
943
944 6539 return 0;
945 }
946
947 32 static int mkv_assemble_cues(AVStream **streams, AVIOContext *dyn_cp, AVIOContext *cuepoint,
948 const mkv_cues *cues, mkv_track *tracks, int num_tracks,
949 uint64_t offset)
950 {
951 32 for (mkv_cuepoint *entry = cues->entries, *end = entry + cues->num_entries;
952
2/2
✓ Branch 0 taken 9283 times.
✓ Branch 1 taken 32 times.
9315 entry < end;) {
953 9283 uint64_t pts = entry->pts;
954 uint8_t *buf;
955 int size;
956
957 9283 put_ebml_uint(cuepoint, MATROSKA_ID_CUETIME, pts);
958
959 // put all the entries from different tracks that have the exact same
960 // timestamp into the same CuePoint
961
2/2
✓ Branch 0 taken 18584 times.
✓ Branch 1 taken 9283 times.
27867 for (int j = 0; j < num_tracks; j++)
962 18584 tracks[j].has_cue = 0;
963 do {
964 ebml_master track_positions;
965 17801 int idx = entry->stream_idx;
966
967
2/4
✓ Branch 0 taken 17801 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 17801 times.
17801 av_assert0(idx >= 0 && idx < num_tracks);
968
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 17799 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
17801 if (tracks[idx].has_cue && streams[idx]->codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE)
969 continue;
970 17801 tracks[idx].has_cue = 1;
971 17801 track_positions = start_ebml_master(cuepoint, MATROSKA_ID_CUETRACKPOSITION, MAX_CUETRACKPOS_SIZE);
972 17801 put_ebml_uint(cuepoint, MATROSKA_ID_CUETRACK , tracks[idx].track_num);
973 17801 put_ebml_uint(cuepoint, MATROSKA_ID_CUECLUSTERPOSITION , entry->cluster_pos + offset);
974 17801 put_ebml_uint(cuepoint, MATROSKA_ID_CUERELATIVEPOSITION, entry->relative_pos);
975
2/2
✓ Branch 0 taken 83 times.
✓ Branch 1 taken 17718 times.
17801 if (entry->duration > 0)
976 83 put_ebml_uint(cuepoint, MATROSKA_ID_CUEDURATION , entry->duration);
977 17801 end_ebml_master(cuepoint, track_positions);
978
4/4
✓ Branch 0 taken 17769 times.
✓ Branch 1 taken 32 times.
✓ Branch 2 taken 8518 times.
✓ Branch 3 taken 9251 times.
17801 } while (++entry < end && entry->pts == pts);
979 9283 size = avio_get_dyn_buf(cuepoint, &buf);
980
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9283 times.
9283 if (cuepoint->error < 0)
981 return cuepoint->error;
982 9283 put_ebml_binary(dyn_cp, MATROSKA_ID_POINTENTRY, buf, size);
983 9283 ffio_reset_dyn_buf(cuepoint);
984 }
985
986 32 return 0;
987 }
988
989 1 static int put_xiph_codecpriv(AVFormatContext *s, AVIOContext *pb,
990 const AVCodecParameters *par,
991 const uint8_t *extradata, int extradata_size)
992 {
993 const uint8_t *header_start[3];
994 int header_len[3];
995 int first_header_size;
996 int err, j;
997
998
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (par->codec_id == AV_CODEC_ID_VORBIS)
999 1 first_header_size = 30;
1000 else
1001 first_header_size = 42;
1002
1003 1 err = avpriv_split_xiph_headers(extradata, extradata_size,
1004 first_header_size, header_start, header_len);
1005
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (err < 0) {
1006 av_log(s, AV_LOG_ERROR, "Extradata corrupt.\n");
1007 return err;
1008 }
1009
1010 1 avio_w8(pb, 2); // number packets - 1
1011
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 for (j = 0; j < 2; j++) {
1012 2 put_xiph_size(pb, header_len[j]);
1013 }
1014
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 for (j = 0; j < 3; j++)
1015 3 avio_write(pb, header_start[j], header_len[j]);
1016
1017 1 return 0;
1018 }
1019
1020 #if CONFIG_MATROSKA_MUXER
1021 2 static int put_wv_codecpriv(AVIOContext *pb, const uint8_t *extradata, int extradata_size)
1022 {
1023
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)
1024 2 avio_write(pb, extradata, 2);
1025 else
1026 avio_wl16(pb, 0x410); // fallback to the most recent version
1027 2 return 0;
1028 }
1029
1030 6 static int put_flac_codecpriv(AVFormatContext *s, AVIOContext *pb,
1031 const AVCodecParameters *par,
1032 const uint8_t *extradata, int extradata_size)
1033 {
1034 18 int write_comment = (par->ch_layout.order == AV_CHANNEL_ORDER_NATIVE &&
1035
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) &&
1036 6 !ff_flac_is_native_layout(par->ch_layout.u.mask));
1037 6 int ret = ff_flac_write_header(pb, extradata, extradata_size,
1038 !write_comment);
1039
1040
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0)
1041 return ret;
1042
1043
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 if (write_comment) {
1044 8 const char *vendor = (s->flags & AVFMT_FLAG_BITEXACT) ?
1045
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 "Lavf" : LIBAVFORMAT_IDENT;
1046 4 AVDictionary *dict = NULL;
1047 uint8_t buf[32];
1048 int64_t len;
1049
1050 4 snprintf(buf, sizeof(buf), "0x%"PRIx64, par->ch_layout.u.mask);
1051 4 av_dict_set(&dict, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", buf, 0);
1052
1053 4 len = ff_vorbiscomment_length(dict, vendor, NULL, 0);
1054 av_assert1(len < (1 << 24) - 4);
1055
1056 4 avio_w8(pb, 0x84);
1057 4 avio_wb24(pb, len);
1058
1059 4 ff_vorbiscomment_write(pb, dict, vendor, NULL, 0);
1060
1061 4 av_dict_free(&dict);
1062 }
1063
1064 6 return 0;
1065 }
1066
1067 7 static int get_aac_sample_rates(AVFormatContext *s, MatroskaMuxContext *mkv,
1068 const uint8_t *extradata, int extradata_size,
1069 int *sample_rate, int *output_sample_rate)
1070 {
1071 MPEG4AudioConfig mp4ac;
1072 int ret;
1073
1074 7 ret = avpriv_mpeg4audio_get_config2(&mp4ac, extradata, extradata_size, 1, s);
1075 /* Don't abort if the failure is because of missing extradata. Assume in that
1076 * case a bitstream filter will provide the muxer with the extradata in the
1077 * first packet.
1078 * Abort however if s->pb is not seekable, as we would not be able to seek back
1079 * to write the sample rate elements once the extradata shows up, anyway. */
1080
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))) {
1081 av_log(s, AV_LOG_ERROR,
1082 "Error parsing AAC extradata, unable to determine samplerate.\n");
1083 return AVERROR(EINVAL);
1084 }
1085
1086
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6 times.
7 if (ret < 0) {
1087 /* This will only happen when this function is called while writing the
1088 * header and no extradata is available. The space for this element has
1089 * to be reserved for when this function is called again after the
1090 * extradata shows up in the first packet, as there's no way to know if
1091 * output_sample_rate will be different than sample_rate or not. */
1092 1 *output_sample_rate = *sample_rate;
1093 } else {
1094 6 *sample_rate = mp4ac.sample_rate;
1095 6 *output_sample_rate = mp4ac.ext_sample_rate;
1096 }
1097 7 return 0;
1098 }
1099 #endif
1100
1101 56 static int mkv_assemble_native_codecprivate(AVFormatContext *s, AVIOContext *dyn_cp,
1102 const AVCodecParameters *par,
1103 const uint8_t *extradata,
1104 int extradata_size,
1105 unsigned *size_to_reserve)
1106 {
1107
8/9
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 3 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 7 times.
✓ Branch 8 taken 30 times.
56 switch (par->codec_id) {
1108 1 case AV_CODEC_ID_VORBIS:
1109 case AV_CODEC_ID_THEORA:
1110 1 return put_xiph_codecpriv(s, dyn_cp, par, extradata, extradata_size);
1111 3 case AV_CODEC_ID_AV1:
1112
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 if (extradata_size)
1113 2 return ff_isom_write_av1c(dyn_cp, extradata,
1114 extradata_size, 1);
1115 else
1116 1 *size_to_reserve = (AV1_SANE_SEQUENCE_HEADER_MAX_BITS + 7) / 8 + 100;
1117 1 break;
1118 #if CONFIG_MATROSKA_MUXER
1119 6 case AV_CODEC_ID_FLAC:
1120 6 return put_flac_codecpriv(s, dyn_cp, par, extradata, extradata_size);
1121 2 case AV_CODEC_ID_WAVPACK:
1122 2 return put_wv_codecpriv(dyn_cp, extradata, extradata_size);
1123 4 case AV_CODEC_ID_H264:
1124 4 return ff_isom_write_avcc(dyn_cp, extradata,
1125 extradata_size);
1126 3 case AV_CODEC_ID_HEVC:
1127 3 return ff_isom_write_hvcc(dyn_cp, extradata,
1128 extradata_size, 0);
1129 case AV_CODEC_ID_ALAC:
1130 if (extradata_size < 36) {
1131 av_log(s, AV_LOG_ERROR,
1132 "Invalid extradata found, ALAC expects a 36-byte "
1133 "QuickTime atom.");
1134 return AVERROR_INVALIDDATA;
1135 } else
1136 avio_write(dyn_cp, extradata + 12,
1137 extradata_size - 12);
1138 break;
1139 7 case AV_CODEC_ID_AAC:
1140
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1 times.
7 if (extradata_size)
1141 6 avio_write(dyn_cp, extradata, extradata_size);
1142 else
1143 1 *size_to_reserve = MAX_PCE_SIZE;
1144 7 break;
1145 #endif
1146 30 default:
1147
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 29 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
31 if (CONFIG_MATROSKA_MUXER && par->codec_id == AV_CODEC_ID_PRORES &&
1148 1 ff_codec_get_id(ff_codec_movvideo_tags, par->codec_tag) == AV_CODEC_ID_PRORES) {
1149 1 avio_wl32(dyn_cp, par->codec_tag);
1150
3/4
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 19 times.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
29 } else if (extradata_size && par->codec_id != AV_CODEC_ID_TTA)
1151 10 avio_write(dyn_cp, extradata, extradata_size);
1152 }
1153
1154 38 return 0;
1155 }
1156
1157 62 static int mkv_assemble_codecprivate(AVFormatContext *s, AVIOContext *dyn_cp,
1158 AVCodecParameters *par,
1159 const uint8_t *extradata, int extradata_size,
1160 int native_id, int qt_id,
1161 uint8_t **codecpriv, int *codecpriv_size,
1162 unsigned *max_payload_size)
1163 {
1164 62 MatroskaMuxContext av_unused *const mkv = s->priv_data;
1165 62 unsigned size_to_reserve = 0;
1166 int ret;
1167
1168
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 6 times.
62 if (native_id) {
1169 56 ret = mkv_assemble_native_codecprivate(s, dyn_cp, par,
1170 extradata, extradata_size,
1171 &size_to_reserve);
1172
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
56 if (ret < 0)
1173 return ret;
1174 #if CONFIG_MATROSKA_MUXER
1175
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
6 } else if (par->codec_type == AVMEDIA_TYPE_VIDEO) {
1176
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 4 times.
5 if (qt_id) {
1177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!par->codec_tag)
1178 par->codec_tag = ff_codec_get_tag(ff_codec_movvideo_tags,
1179 par->codec_id);
1180
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
1181
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)
1182 ) {
1183 1 avio_wb32(dyn_cp, 0x5a + extradata_size);
1184 1 avio_wl32(dyn_cp, par->codec_tag);
1185 1 ffio_fill(dyn_cp, 0, 0x5a - 8);
1186 }
1187 1 avio_write(dyn_cp, extradata, extradata_size);
1188 } else {
1189
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))
1190 2 av_log(s, AV_LOG_WARNING, "codec %s is not supported by this format\n",
1191 avcodec_get_name(par->codec_id));
1192
1193
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (!par->codec_tag)
1194 2 par->codec_tag = ff_codec_get_tag(ff_codec_bmp_tags,
1195 par->codec_id);
1196
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) {
1197 av_log(s, AV_LOG_ERROR, "No bmp codec tag found for codec %s\n",
1198 avcodec_get_name(par->codec_id));
1199 return AVERROR(EINVAL);
1200 }
1201
1202 /* If vfw extradata updates are supported, this will have
1203 * to be updated to pass extradata(_size) explicitly. */
1204 4 ff_put_bmp_header(dyn_cp, par, 0, 0, mkv->flipped_raw_rgb);
1205 }
1206
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 } else if (par->codec_type == AVMEDIA_TYPE_AUDIO) {
1207 unsigned int tag;
1208 1 tag = ff_codec_get_tag(ff_codec_wav_tags, par->codec_id);
1209
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!tag) {
1210 av_log(s, AV_LOG_ERROR, "No wav codec tag found for codec %s\n",
1211 avcodec_get_name(par->codec_id));
1212 return AVERROR(EINVAL);
1213 }
1214
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!par->codec_tag)
1215 par->codec_tag = tag;
1216
1217 /* Same comment as for ff_put_bmp_header applies here. */
1218 1 ff_put_wav_header(s, dyn_cp, par, FF_PUT_WAV_HEADER_FORCE_WAVEFORMATEX);
1219 #endif
1220 }
1221
1222 62 *codecpriv_size = avio_get_dyn_buf(dyn_cp, codecpriv);
1223
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
62 if (dyn_cp->error < 0)
1224 return dyn_cp->error;
1225 62 *max_payload_size = *codecpriv_size + size_to_reserve;
1226
1227 62 return 0;
1228 }
1229
1230 62 static void mkv_put_codecprivate(AVIOContext *pb, unsigned max_payload_size,
1231 const uint8_t *codecpriv, unsigned codecpriv_size)
1232 {
1233 62 unsigned total_codecpriv_size = 0, total_size;
1234
1235 av_assert1(codecpriv_size <= max_payload_size);
1236
1237
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 43 times.
62 if (!max_payload_size)
1238 19 return;
1239
1240 43 total_size = 2 + ebml_length_size(max_payload_size) + max_payload_size;
1241
1242
2/2
✓ Branch 0 taken 41 times.
✓ Branch 1 taken 2 times.
43 if (codecpriv_size) {
1243 41 unsigned length_size = ebml_length_size(codecpriv_size);
1244
1245 41 total_codecpriv_size = 2U + length_size + codecpriv_size;
1246
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 41 times.
41 if (total_codecpriv_size + 1 == total_size) {
1247 /* It is impossible to add one byte of padding via an EBML Void. */
1248 length_size++;
1249 total_codecpriv_size++;
1250 }
1251 41 put_ebml_id(pb, MATROSKA_ID_CODECPRIVATE);
1252 41 put_ebml_length(pb, codecpriv_size, length_size);
1253 41 avio_write(pb, codecpriv, codecpriv_size);
1254 }
1255
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 39 times.
43 if (total_codecpriv_size < total_size)
1256 4 put_ebml_void(pb, total_size - total_codecpriv_size);
1257 }
1258
1259 5 static int mkv_update_codecprivate(AVFormatContext *s, MatroskaMuxContext *mkv,
1260 uint8_t *side_data, int side_data_size,
1261 AVCodecParameters *par, AVIOContext *pb,
1262 mkv_track *track, unsigned alternative_size)
1263 {
1264 5 AVIOContext *const dyn_bc = mkv->tmp_bc;
1265 uint8_t *codecpriv;
1266 unsigned max_payload_size;
1267 int ret, codecpriv_size;
1268
1269 5 ret = mkv_assemble_codecprivate(s, dyn_bc, par,
1270 side_data, side_data_size, 1, 0,
1271 &codecpriv, &codecpriv_size, &max_payload_size);
1272
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (ret < 0)
1273 goto fail;
1274
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) {
1275 ret = AVERROR(ENOSPC);
1276 goto fail;
1277
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 } else if (codecpriv_size > track->codecpriv_size) {
1278 av_assert1(alternative_size < track->codecpriv_size);
1279 codecpriv_size = alternative_size;
1280 }
1281 5 avio_seek(pb, track->codecpriv_offset, SEEK_SET);
1282 5 mkv_put_codecprivate(pb, track->codecpriv_size,
1283 codecpriv, codecpriv_size);
1284
1285
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2 times.
5 if (!par->extradata_size) {
1286 2 ret = ff_alloc_extradata(par, side_data_size);
1287
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0)
1288 goto fail;
1289 2 memcpy(par->extradata, side_data, side_data_size);
1290 }
1291 3 fail:
1292 5 ffio_reset_dyn_buf(dyn_bc);
1293 5 return ret;
1294 }
1295
1296 #define MAX_VIDEO_COLOR_ELEMS 20
1297 21 static void mkv_write_video_color(EbmlWriter *writer, const AVStream *st,
1298 const AVCodecParameters *par)
1299 {
1300 const void *side_data;
1301
1302 21 ebml_writer_open_master(writer, MATROSKA_ID_VIDEOCOLOR);
1303
1304
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 11 times.
21 if (par->color_trc != AVCOL_TRC_UNSPECIFIED &&
1305
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 par->color_trc < AVCOL_TRC_NB) {
1306 10 ebml_writer_add_uint(writer, MATROSKA_ID_VIDEOCOLORTRANSFERCHARACTERISTICS,
1307 10 par->color_trc);
1308 }
1309
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 13 times.
21 if (par->color_space != AVCOL_SPC_UNSPECIFIED &&
1310
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 par->color_space < AVCOL_SPC_NB) {
1311 8 ebml_writer_add_uint(writer, MATROSKA_ID_VIDEOCOLORMATRIXCOEFF,
1312 8 par->color_space);
1313 }
1314
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 13 times.
21 if (par->color_primaries != AVCOL_PRI_UNSPECIFIED &&
1315
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 par->color_primaries < AVCOL_PRI_NB) {
1316 8 ebml_writer_add_uint(writer, MATROSKA_ID_VIDEOCOLORPRIMARIES,
1317 8 par->color_primaries);
1318 }
1319
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 9 times.
21 if (par->color_range != AVCOL_RANGE_UNSPECIFIED &&
1320
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 par->color_range < AVCOL_RANGE_NB) {
1321 12 ebml_writer_add_uint(writer, MATROSKA_ID_VIDEOCOLORRANGE, par->color_range);
1322 }
1323
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 12 times.
21 if (par->chroma_location != AVCHROMA_LOC_UNSPECIFIED &&
1324
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 par->chroma_location <= AVCHROMA_LOC_TOP) {
1325 int xpos, ypos;
1326
1327 9 av_chroma_location_enum_to_pos(&xpos, &ypos, par->chroma_location);
1328 9 ebml_writer_add_uint(writer, MATROSKA_ID_VIDEOCOLORCHROMASITINGHORZ,
1329 9 (xpos >> 7) + 1);
1330 9 ebml_writer_add_uint(writer, MATROSKA_ID_VIDEOCOLORCHROMASITINGVERT,
1331 9 (ypos >> 7) + 1);
1332 }
1333
1334 21 side_data = av_stream_get_side_data(st, AV_PKT_DATA_CONTENT_LIGHT_LEVEL,
1335 NULL);
1336
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 19 times.
21 if (side_data) {
1337 2 const AVContentLightMetadata *metadata = side_data;
1338 2 ebml_writer_add_uint(writer, MATROSKA_ID_VIDEOCOLORMAXCLL,
1339 2 metadata->MaxCLL);
1340 2 ebml_writer_add_uint(writer, MATROSKA_ID_VIDEOCOLORMAXFALL,
1341 2 metadata->MaxFALL);
1342 }
1343
1344 21 side_data = av_stream_get_side_data(st, AV_PKT_DATA_MASTERING_DISPLAY_METADATA,
1345 NULL);
1346
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 19 times.
21 if (side_data) {
1347 2 const AVMasteringDisplayMetadata *metadata = side_data;
1348 2 ebml_writer_open_master(writer, MATROSKA_ID_VIDEOCOLORMASTERINGMETA);
1349
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (metadata->has_primaries) {
1350 2 ebml_writer_add_float(writer, MATROSKA_ID_VIDEOCOLOR_RX,
1351 av_q2d(metadata->display_primaries[0][0]));
1352 2 ebml_writer_add_float(writer, MATROSKA_ID_VIDEOCOLOR_RY,
1353 av_q2d(metadata->display_primaries[0][1]));
1354 2 ebml_writer_add_float(writer, MATROSKA_ID_VIDEOCOLOR_GX,
1355 av_q2d(metadata->display_primaries[1][0]));
1356 2 ebml_writer_add_float(writer, MATROSKA_ID_VIDEOCOLOR_GY,
1357 av_q2d(metadata->display_primaries[1][1]));
1358 2 ebml_writer_add_float(writer, MATROSKA_ID_VIDEOCOLOR_BX,
1359 av_q2d(metadata->display_primaries[2][0]));
1360 2 ebml_writer_add_float(writer, MATROSKA_ID_VIDEOCOLOR_BY,
1361 av_q2d(metadata->display_primaries[2][1]));
1362 2 ebml_writer_add_float(writer, MATROSKA_ID_VIDEOCOLOR_WHITEX,
1363 av_q2d(metadata->white_point[0]));
1364 2 ebml_writer_add_float(writer, MATROSKA_ID_VIDEOCOLOR_WHITEY,
1365 av_q2d(metadata->white_point[1]));
1366 }
1367
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (metadata->has_luminance) {
1368 2 ebml_writer_add_float(writer, MATROSKA_ID_VIDEOCOLOR_LUMINANCEMAX,
1369 av_q2d(metadata->max_luminance));
1370 2 ebml_writer_add_float(writer, MATROSKA_ID_VIDEOCOLOR_LUMINANCEMIN,
1371 av_q2d(metadata->min_luminance));
1372 }
1373 2 ebml_writer_close_or_discard_master(writer);
1374 }
1375
1376 21 ebml_writer_close_or_discard_master(writer);
1377 21 }
1378
1379 #define MAX_VIDEO_PROJECTION_ELEMS 6
1380 21 static void mkv_write_video_projection(AVFormatContext *s, EbmlWriter *writer,
1381 const AVStream *st, uint8_t private[])
1382 {
1383 const AVSphericalMapping *spherical =
1384 21 (const AVSphericalMapping *)av_stream_get_side_data(st, AV_PKT_DATA_SPHERICAL,
1385 NULL);
1386
1387
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 2 times.
21 if (!spherical)
1388 19 return;
1389
1390
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (spherical->projection != AV_SPHERICAL_EQUIRECTANGULAR &&
1391
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 spherical->projection != AV_SPHERICAL_EQUIRECTANGULAR_TILE &&
1392 spherical->projection != AV_SPHERICAL_CUBEMAP) {
1393 av_log(s, AV_LOG_WARNING, "Unknown projection type\n");
1394 return;
1395 }
1396
1397 2 ebml_writer_open_master(writer, MATROSKA_ID_VIDEOPROJECTION);
1398
1399
1/3
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
2 switch (spherical->projection) {
1400 2 case AV_SPHERICAL_EQUIRECTANGULAR:
1401 case AV_SPHERICAL_EQUIRECTANGULAR_TILE:
1402 2 ebml_writer_add_uint(writer, MATROSKA_ID_VIDEOPROJECTIONTYPE,
1403 MATROSKA_VIDEO_PROJECTION_TYPE_EQUIRECTANGULAR);
1404 2 AV_WB32(private, 0); // version + flags
1405
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (spherical->projection == AV_SPHERICAL_EQUIRECTANGULAR) {
1406 AV_WB32(private + 4, 0);
1407 AV_WB32(private + 8, 0);
1408 AV_WB32(private + 12, 0);
1409 AV_WB32(private + 16, 0);
1410 } else {
1411 2 AV_WB32(private + 4, spherical->bound_top);
1412 2 AV_WB32(private + 8, spherical->bound_bottom);
1413 2 AV_WB32(private + 12, spherical->bound_left);
1414 2 AV_WB32(private + 16, spherical->bound_right);
1415 }
1416 2 ebml_writer_add_bin(writer, MATROSKA_ID_VIDEOPROJECTIONPRIVATE,
1417 private, 20);
1418 2 break;
1419 case AV_SPHERICAL_CUBEMAP:
1420 ebml_writer_add_uint(writer, MATROSKA_ID_VIDEOPROJECTIONTYPE,
1421 MATROSKA_VIDEO_PROJECTION_TYPE_CUBEMAP);
1422 AV_WB32(private, 0); // version + flags
1423 AV_WB32(private + 4, 0); // layout
1424 AV_WB32(private + 8, spherical->padding);
1425 ebml_writer_add_bin(writer, MATROSKA_ID_VIDEOPROJECTIONPRIVATE,
1426 private, 12);
1427 break;
1428 default:
1429 av_assert0(0);
1430 }
1431
1432
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (spherical->yaw)
1433 2 ebml_writer_add_float(writer, MATROSKA_ID_VIDEOPROJECTIONPOSEYAW,
1434 2 (double) spherical->yaw / (1 << 16));
1435
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (spherical->pitch)
1436 2 ebml_writer_add_float(writer, MATROSKA_ID_VIDEOPROJECTIONPOSEPITCH,
1437 2 (double) spherical->pitch / (1 << 16));
1438
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (spherical->roll)
1439 2 ebml_writer_add_float(writer, MATROSKA_ID_VIDEOPROJECTIONPOSEROLL,
1440 2 (double) spherical->roll / (1 << 16));
1441
1442 2 ebml_writer_close_master(writer);
1443 }
1444
1445 #define MAX_FIELD_ORDER_ELEMS 2
1446 21 static void mkv_write_field_order(EbmlWriter *writer, int is_webm,
1447 enum AVFieldOrder field_order)
1448 {
1449
3/4
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
21 switch (field_order) {
1450 8 case AV_FIELD_UNKNOWN:
1451 8 break;
1452 9 case AV_FIELD_PROGRESSIVE:
1453 9 ebml_writer_add_uint(writer, MATROSKA_ID_VIDEOFLAGINTERLACED,
1454 MATROSKA_VIDEO_INTERLACE_FLAG_PROGRESSIVE);
1455 9 break;
1456 4 case AV_FIELD_TT:
1457 case AV_FIELD_BB:
1458 case AV_FIELD_TB:
1459 case AV_FIELD_BT:
1460 4 ebml_writer_add_uint(writer, MATROSKA_ID_VIDEOFLAGINTERLACED,
1461 MATROSKA_VIDEO_INTERLACE_FLAG_INTERLACED);
1462
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (!is_webm) {
1463 switch (field_order) {
1464 3 case AV_FIELD_TT:
1465 3 ebml_writer_add_uint(writer, MATROSKA_ID_VIDEOFIELDORDER,
1466 MATROSKA_VIDEO_FIELDORDER_TT);
1467 3 break;
1468 1 case AV_FIELD_BB:
1469 1 ebml_writer_add_uint(writer, MATROSKA_ID_VIDEOFIELDORDER,
1470 MATROSKA_VIDEO_FIELDORDER_BB);
1471 1 break;
1472 case AV_FIELD_TB:
1473 ebml_writer_add_uint(writer, MATROSKA_ID_VIDEOFIELDORDER,
1474 MATROSKA_VIDEO_FIELDORDER_TB);
1475 break;
1476 case AV_FIELD_BT:
1477 ebml_writer_add_uint(writer, MATROSKA_ID_VIDEOFIELDORDER,
1478 MATROSKA_VIDEO_FIELDORDER_BT);
1479 break;
1480 }
1481 }
1482 }
1483 21 }
1484
1485 #define MAX_STEREO_MODE_ELEMS 1
1486 21 static int mkv_write_stereo_mode(AVFormatContext *s, EbmlWriter *writer,
1487 AVStream *st, int is_webm,
1488 int *h_width, int *h_height)
1489 {
1490 const AVDictionaryEntry *tag;
1491 21 MatroskaVideoStereoModeType format = MATROSKA_VIDEO_STEREOMODE_TYPE_NB;
1492 const AVStereo3D *stereo;
1493
1494 21 *h_width = 1;
1495 21 *h_height = 1;
1496 // convert metadata into proper side data and add it to the stream
1497
2/4
✓ Branch 1 taken 21 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 21 times.
42 if ((tag = av_dict_get(st->metadata, "stereo_mode", NULL, 0)) ||
1498 21 (tag = av_dict_get( s->metadata, "stereo_mode", NULL, 0))) {
1499 int stereo_mode = atoi(tag->value);
1500
1501 for (int i = 0; i < MATROSKA_VIDEO_STEREOMODE_TYPE_NB; i++)
1502 if (!strcmp(tag->value, ff_matroska_video_stereo_mode[i])){
1503 stereo_mode = i;
1504 break;
1505 }
1506
1507 if (stereo_mode < MATROSKA_VIDEO_STEREOMODE_TYPE_NB &&
1508 stereo_mode != 10 && stereo_mode != 12) {
1509 int ret = ff_mkv_stereo3d_conv(st, stereo_mode);
1510 if (ret < 0)
1511 return ret;
1512 }
1513 }
1514
1515 21 stereo = (const AVStereo3D*)av_stream_get_side_data(st, AV_PKT_DATA_STEREO3D,
1516 NULL);
1517
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 17 times.
21 if (stereo) {
1518
1/8
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
4 switch (stereo->type) {
1519 4 case AV_STEREO3D_2D:
1520 4 format = MATROSKA_VIDEO_STEREOMODE_TYPE_MONO;
1521 4 break;
1522 case AV_STEREO3D_SIDEBYSIDE:
1523 format = (stereo->flags & AV_STEREO3D_FLAG_INVERT)
1524 ? MATROSKA_VIDEO_STEREOMODE_TYPE_RIGHT_LEFT
1525 : MATROSKA_VIDEO_STEREOMODE_TYPE_LEFT_RIGHT;
1526 *h_width = 2;
1527 break;
1528 case AV_STEREO3D_TOPBOTTOM:
1529 format = MATROSKA_VIDEO_STEREOMODE_TYPE_TOP_BOTTOM;
1530 if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
1531 format--;
1532 *h_height = 2;
1533 break;
1534 case AV_STEREO3D_CHECKERBOARD:
1535 format = MATROSKA_VIDEO_STEREOMODE_TYPE_CHECKERBOARD_LR;
1536 if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
1537 format--;
1538 break;
1539 case AV_STEREO3D_LINES:
1540 format = MATROSKA_VIDEO_STEREOMODE_TYPE_ROW_INTERLEAVED_LR;
1541 if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
1542 format--;
1543 *h_height = 2;
1544 break;
1545 case AV_STEREO3D_COLUMNS:
1546 format = MATROSKA_VIDEO_STEREOMODE_TYPE_COL_INTERLEAVED_LR;
1547 if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
1548 format--;
1549 *h_width = 2;
1550 break;
1551 case AV_STEREO3D_FRAMESEQUENCE:
1552 format = MATROSKA_VIDEO_STEREOMODE_TYPE_BOTH_EYES_BLOCK_LR;
1553 if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
1554 format++;
1555 break;
1556 }
1557 }
1558
1559
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 4 times.
21 if (format == MATROSKA_VIDEO_STEREOMODE_TYPE_NB)
1560 17 return 0;
1561
1562 // if webm, do not write unsupported modes
1563
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4 if ((is_webm &&
1564 format > MATROSKA_VIDEO_STEREOMODE_TYPE_TOP_BOTTOM &&
1565 format != MATROSKA_VIDEO_STEREOMODE_TYPE_RIGHT_LEFT)
1566
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 || format >= MATROSKA_VIDEO_STEREOMODE_TYPE_NB) {
1567 av_log(s, AV_LOG_ERROR,
1568 "The specified stereo mode is not valid.\n");
1569 return AVERROR(EINVAL);
1570 }
1571
1572 // write StereoMode if format is valid
1573 4 ebml_writer_add_uint(writer, MATROSKA_ID_VIDEOSTEREOMODE, format);
1574
1575 4 return 0;
1576 }
1577
1578 20 static void mkv_write_dovi(AVFormatContext *s, AVIOContext *pb, AVStream *st)
1579 {
1580 #if CONFIG_MATROSKA_MUXER
1581 AVDOVIDecoderConfigurationRecord *dovi = (AVDOVIDecoderConfigurationRecord *)
1582 20 av_stream_get_side_data(st, AV_PKT_DATA_DOVI_CONF, NULL);
1583
1584
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 18 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
20 if (dovi && dovi->dv_profile <= 10) {
1585 ebml_master mapping;
1586 uint8_t buf[ISOM_DVCC_DVVC_SIZE];
1587 uint32_t type;
1588
1589 2 uint64_t expected_size = (2 + 1 + (sizeof(DVCC_DVVC_BLOCK_TYPE_NAME) - 1))
1590 + (2 + 1 + 4) + (2 + 1 + ISOM_DVCC_DVVC_SIZE);
1591
1592
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (dovi->dv_profile > 7) {
1593 1 type = MKBETAG('d', 'v', 'v', 'C');
1594 } else {
1595 1 type = MKBETAG('d', 'v', 'c', 'C');
1596 }
1597
1598 2 ff_isom_put_dvcc_dvvc(s, buf, dovi);
1599
1600 2 mapping = start_ebml_master(pb, MATROSKA_ID_TRACKBLKADDMAPPING, expected_size);
1601
1602 2 put_ebml_string(pb, MATROSKA_ID_BLKADDIDNAME, DVCC_DVVC_BLOCK_TYPE_NAME);
1603 2 put_ebml_uint(pb, MATROSKA_ID_BLKADDIDTYPE, type);
1604 2 put_ebml_binary(pb, MATROSKA_ID_BLKADDIDEXTRADATA, buf, sizeof(buf));
1605
1606 2 end_ebml_master(pb, mapping);
1607 }
1608 #endif
1609 20 }
1610
1611 21 static int mkv_write_track_video(AVFormatContext *s, MatroskaMuxContext *mkv,
1612 AVStream *st, const AVCodecParameters *par,
1613 AVIOContext *pb)
1614 {
1615 const AVDictionaryEntry *tag;
1616 21 int display_width_div = 1, display_height_div = 1;
1617 uint8_t color_space[4], projection_private[20];
1618 21 EBML_WRITER(MAX_FIELD_ORDER_ELEMS + MAX_STEREO_MODE_ELEMS +
1619 MAX_VIDEO_COLOR_ELEMS + MAX_VIDEO_PROJECTION_ELEMS + 8);
1620 int ret;
1621
1622 21 ebml_writer_open_master(&writer, MATROSKA_ID_TRACKVIDEO);
1623
1624 21 ebml_writer_add_uint(&writer, MATROSKA_ID_VIDEOPIXELWIDTH , par->width);
1625 21 ebml_writer_add_uint(&writer, MATROSKA_ID_VIDEOPIXELHEIGHT, par->height);
1626
1627 21 mkv_write_field_order(&writer, IS_WEBM(mkv), par->field_order);
1628
1629 // check both side data and metadata for stereo information,
1630 // write the result to the bitstream if any is found
1631 21 ret = mkv_write_stereo_mode(s, &writer, st, IS_WEBM(mkv),
1632 &display_width_div,
1633 &display_height_div);
1634
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (ret < 0)
1635 return ret;
1636
1637
4/6
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 20 times.
41 if (((tag = av_dict_get(st->metadata, "alpha_mode", NULL, 0)) && atoi(tag->value)) ||
1638
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
20 ((tag = av_dict_get( s->metadata, "alpha_mode", NULL, 0)) && atoi(tag->value)) ||
1639
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 (par->format == AV_PIX_FMT_YUVA420P)) {
1640 1 ebml_writer_add_uint(&writer, MATROSKA_ID_VIDEOALPHAMODE, 1);
1641 }
1642
1643 // write DisplayWidth and DisplayHeight, they contain the size of
1644 // a single source view and/or the display aspect ratio
1645
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 10 times.
21 if (st->sample_aspect_ratio.num) {
1646 11 int64_t d_width = av_rescale(par->width, st->sample_aspect_ratio.num, st->sample_aspect_ratio.den);
1647
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (d_width > INT_MAX) {
1648 av_log(s, AV_LOG_ERROR, "Overflow in display width\n");
1649 return AVERROR(EINVAL);
1650 }
1651
4/6
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 7 times.
11 if (d_width != par->width || display_width_div != 1 || display_height_div != 1) {
1652
4/6
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 3 times.
4 if (IS_WEBM(mkv) || display_width_div != 1 || display_height_div != 1) {
1653 1 ebml_writer_add_uint(&writer, MATROSKA_ID_VIDEODISPLAYWIDTH,
1654 1 d_width / display_width_div);
1655 1 ebml_writer_add_uint(&writer, MATROSKA_ID_VIDEODISPLAYHEIGHT,
1656 1 par->height / display_height_div);
1657 } else {
1658 AVRational display_aspect_ratio;
1659 3 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1660 3 par->width * (int64_t)st->sample_aspect_ratio.num,
1661 3 par->height * (int64_t)st->sample_aspect_ratio.den,
1662 1024 * 1024);
1663 3 ebml_writer_add_uint(&writer, MATROSKA_ID_VIDEODISPLAYWIDTH,
1664 3 display_aspect_ratio.num);
1665 3 ebml_writer_add_uint(&writer, MATROSKA_ID_VIDEODISPLAYHEIGHT,
1666 3 display_aspect_ratio.den);
1667 3 ebml_writer_add_uint(&writer, MATROSKA_ID_VIDEODISPLAYUNIT,
1668 MATROSKA_VIDEO_DISPLAYUNIT_DAR);
1669 }
1670 }
1671
2/4
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 10 times.
10 } else if (display_width_div != 1 || display_height_div != 1) {
1672 ebml_writer_add_uint(&writer, MATROSKA_ID_VIDEODISPLAYWIDTH,
1673 par->width / display_width_div);
1674 ebml_writer_add_uint(&writer, MATROSKA_ID_VIDEODISPLAYHEIGHT,
1675 par->height / display_height_div);
1676
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 } else if (!IS_WEBM(mkv))
1677 10 ebml_writer_add_uint(&writer, MATROSKA_ID_VIDEODISPLAYUNIT,
1678 MATROSKA_VIDEO_DISPLAYUNIT_UNKNOWN);
1679
1680
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 19 times.
21 if (par->codec_id == AV_CODEC_ID_RAWVIDEO) {
1681 2 AV_WL32(color_space, par->codec_tag);
1682 2 ebml_writer_add_bin(&writer, MATROSKA_ID_VIDEOCOLORSPACE,
1683 color_space, sizeof(color_space));
1684 }
1685 21 mkv_write_video_color(&writer, st, par);
1686 21 mkv_write_video_projection(s, &writer, st, projection_private);
1687
1688 21 return ebml_writer_write(&writer, pb);
1689 }
1690
1691 62 static int mkv_write_track(AVFormatContext *s, MatroskaMuxContext *mkv,
1692 AVStream *st, mkv_track *track, AVIOContext *pb,
1693 int is_default)
1694 {
1695 62 AVCodecParameters *par = st->codecpar;
1696 ebml_master subinfo, track_master;
1697 62 int native_id = 0;
1698 62 int qt_id = 0;
1699 int bit_depth;
1700 62 int sample_rate = par->sample_rate;
1701 62 int output_sample_rate = 0;
1702 int j, ret;
1703 const AVDictionaryEntry *tag;
1704
1705
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 61 times.
62 if (par->codec_type == AVMEDIA_TYPE_ATTACHMENT)
1706 1 return 0;
1707
1708 61 track_master = start_ebml_master(pb, MATROSKA_ID_TRACKENTRY, 0);
1709 61 put_ebml_uint(pb, MATROSKA_ID_TRACKNUMBER, track->track_num);
1710 61 put_ebml_uid (pb, MATROSKA_ID_TRACKUID, track->uid);
1711 61 put_ebml_uint(pb, MATROSKA_ID_TRACKFLAGLACING, 0); // no lacing (yet)
1712
1713
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 58 times.
61 if ((tag = av_dict_get(st->metadata, "title", NULL, 0)))
1714 3 put_ebml_string(pb, MATROSKA_ID_TRACKNAME, tag->value);
1715 61 tag = av_dict_get(st->metadata, "language", NULL, 0);
1716
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 37 times.
85 put_ebml_string(pb, MATROSKA_ID_TRACKLANGUAGE,
1717
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 tag && tag->value[0] ? tag->value : "und");
1718
1719 // The default value for TRACKFLAGDEFAULT is 1, so add element
1720 // if we need to clear it.
1721
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 23 times.
61 if (!is_default)
1722 38 put_ebml_uint(pb, MATROSKA_ID_TRACKFLAGDEFAULT, 0);
1723
1724
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 59 times.
61 if (st->disposition & AV_DISPOSITION_FORCED)
1725 2 put_ebml_uint(pb, MATROSKA_ID_TRACKFLAGFORCED, 1);
1726
1727
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 55 times.
61 if (IS_WEBM(mkv)) {
1728 const char *codec_id;
1729
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 if (par->codec_type != AVMEDIA_TYPE_SUBTITLE) {
1730
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 for (j = 0; ff_webm_codec_tags[j].id != AV_CODEC_ID_NONE; j++) {
1731
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5 times.
7 if (ff_webm_codec_tags[j].id == par->codec_id) {
1732 2 codec_id = ff_webm_codec_tags[j].str;
1733 2 native_id = 1;
1734 2 break;
1735 }
1736 }
1737
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 } else if (par->codec_id == AV_CODEC_ID_WEBVTT) {
1738
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
4 if (st->disposition & AV_DISPOSITION_CAPTIONS) {
1739 1 codec_id = "D_WEBVTT/CAPTIONS";
1740 1 native_id = MATROSKA_TRACK_TYPE_SUBTITLE;
1741
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 } else if (st->disposition & AV_DISPOSITION_DESCRIPTIONS) {
1742 1 codec_id = "D_WEBVTT/DESCRIPTIONS";
1743 1 native_id = MATROSKA_TRACK_TYPE_METADATA;
1744
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 } else if (st->disposition & AV_DISPOSITION_METADATA) {
1745 1 codec_id = "D_WEBVTT/METADATA";
1746 1 native_id = MATROSKA_TRACK_TYPE_METADATA;
1747 } else {
1748 1 codec_id = "D_WEBVTT/SUBTITLES";
1749 1 native_id = MATROSKA_TRACK_TYPE_SUBTITLE;
1750 }
1751 }
1752
1753
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (!native_id) {
1754 av_log(s, AV_LOG_ERROR,
1755 "Only VP8 or VP9 or AV1 video and Vorbis or Opus audio and WebVTT subtitles are supported for WebM.\n");
1756 return AVERROR(EINVAL);
1757 }
1758
1759 6 put_ebml_string(pb, MATROSKA_ID_CODECID, codec_id);
1760 } else {
1761
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 55 times.
55 if (st->disposition & AV_DISPOSITION_COMMENT)
1762 put_ebml_uint(pb, MATROSKA_ID_TRACKFLAGCOMMENTARY, 1);
1763
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 51 times.
55 if (st->disposition & AV_DISPOSITION_HEARING_IMPAIRED)
1764 4 put_ebml_uint(pb, MATROSKA_ID_TRACKFLAGHEARINGIMPAIRED, 1);
1765
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 54 times.
55 if (st->disposition & AV_DISPOSITION_VISUAL_IMPAIRED)
1766 1 put_ebml_uint(pb, MATROSKA_ID_TRACKFLAGVISUALIMPAIRED, 1);
1767
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 53 times.
55 if (st->disposition & (AV_DISPOSITION_ORIGINAL | AV_DISPOSITION_DUB) &&
1768
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 (st->disposition & (AV_DISPOSITION_ORIGINAL | AV_DISPOSITION_DUB))
1769 != (AV_DISPOSITION_ORIGINAL | AV_DISPOSITION_DUB))
1770 2 put_ebml_uint(pb, MATROSKA_ID_TRACKFLAGORIGINAL,
1771 2 !!(st->disposition & AV_DISPOSITION_ORIGINAL));
1772
1773 // look for a codec ID string specific to mkv to use,
1774 // if none are found, use AVI codes
1775
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 53 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
55 if (par->codec_id != AV_CODEC_ID_RAWVIDEO || par->codec_tag) {
1776
2/2
✓ Branch 0 taken 1781 times.
✓ Branch 1 taken 4 times.
1785 for (j = 0; ff_mkv_codec_tags[j].id != AV_CODEC_ID_NONE; j++) {
1777
4/4
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 1731 times.
✓ Branch 2 taken 49 times.
✓ Branch 3 taken 1 times.
1781 if (ff_mkv_codec_tags[j].id == par->codec_id && par->codec_id != AV_CODEC_ID_FFV1) {
1778 49 put_ebml_string(pb, MATROSKA_ID_CODECID, ff_mkv_codec_tags[j].str);
1779 49 native_id = 1;
1780 49 break;
1781 }
1782 }
1783 } else {
1784
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (mkv->allow_raw_vfw) {
1785 2 native_id = 0;
1786 } else {
1787 av_log(s, AV_LOG_ERROR, "Raw RGB is not supported Natively in Matroska, you can use AVI or NUT or\n"
1788 "If you would like to store it anyway using VFW mode, enable allow_raw_vfw (-allow_raw_vfw 1)\n");
1789 return AVERROR(EINVAL);
1790 }
1791 }
1792 }
1793
1794
3/4
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 29 times.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
61 switch (par->codec_type) {
1795 21 case AVMEDIA_TYPE_VIDEO:
1796 21 mkv->have_video = 1;
1797 21 put_ebml_uint(pb, MATROSKA_ID_TRACKTYPE, MATROSKA_TRACK_TYPE_VIDEO);
1798
1799
3/4
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
21 if( st->avg_frame_rate.num > 0 && st->avg_frame_rate.den > 0
1800
1/2
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
19 && av_cmp_q(av_inv_q(st->avg_frame_rate), st->time_base) > 0)
1801 19 put_ebml_uint(pb, MATROSKA_ID_TRACKDEFAULTDURATION, 1000000000LL * st->avg_frame_rate.den / st->avg_frame_rate.num);
1802
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
2 else if( st->r_frame_rate.num > 0 && st->r_frame_rate.den > 0
1803
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 && av_cmp_q(av_inv_q(st->r_frame_rate), st->time_base) > 0)
1804 1 put_ebml_uint(pb, MATROSKA_ID_TRACKDEFAULTDURATION, 1000000000LL * st->r_frame_rate.den / st->r_frame_rate.num);
1805
1806
4/4
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 2 times.
26 if (CONFIG_MATROSKA_MUXER && !native_id &&
1807
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
8 ff_codec_get_tag(ff_codec_movvideo_tags, par->codec_id) &&
1808
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) ||
1809
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 par->codec_id == AV_CODEC_ID_SVQ1 ||
1810
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 par->codec_id == AV_CODEC_ID_SVQ3 ||
1811
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 par->codec_id == AV_CODEC_ID_CINEPAK))
1812 1 qt_id = 1;
1813
1814
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 20 times.
21 if (qt_id)
1815 1 put_ebml_string(pb, MATROSKA_ID_CODECID, "V_QUICKTIME");
1816
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 16 times.
20 else if (!native_id) {
1817 // if there is no mkv-specific codec ID, use VFW mode
1818 4 put_ebml_string(pb, MATROSKA_ID_CODECID, "V_MS/VFW/FOURCC");
1819 4 track->write_dts = 1;
1820 4 ffformatcontext(s)->avoid_negative_ts_use_pts = 0;
1821 }
1822
1823 21 ret = mkv_write_track_video(s, mkv, st, par, pb);
1824
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (ret < 0)
1825 return ret;
1826
1827
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 1 times.
21 if (!IS_WEBM(mkv))
1828 20 mkv_write_dovi(s, pb, st);
1829
1830 21 break;
1831
1832 29 case AVMEDIA_TYPE_AUDIO:
1833
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 24 times.
29 if (par->initial_padding) {
1834 5 int64_t codecdelay = av_rescale_q(par->initial_padding,
1835 5 (AVRational){ 1, par->sample_rate },
1836 5 (AVRational){ 1, 1000000000 });
1837
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (codecdelay < 0) {
1838 av_log(s, AV_LOG_ERROR, "Initial padding is invalid\n");
1839 return AVERROR(EINVAL);
1840 }
1841 5 put_ebml_uint(pb, MATROSKA_ID_CODECDELAY, codecdelay);
1842
1843 5 track->ts_offset = av_rescale_q(par->initial_padding,
1844 5 (AVRational){ 1, par->sample_rate },
1845 st->time_base);
1846 5 ffstream(st)->lowest_ts_allowed = -track->ts_offset;
1847 }
1848
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 27 times.
29 if (par->codec_id == AV_CODEC_ID_OPUS)
1849 2 put_ebml_uint(pb, MATROSKA_ID_SEEKPREROLL, OPUS_SEEK_PREROLL);
1850 #if CONFIG_MATROSKA_MUXER
1851
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 21 times.
27 else if (par->codec_id == AV_CODEC_ID_AAC) {
1852 6 ret = get_aac_sample_rates(s, mkv, par->extradata, par->extradata_size,
1853 &sample_rate, &output_sample_rate);
1854
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0)
1855 return ret;
1856 }
1857 #endif
1858
1859 29 put_ebml_uint(pb, MATROSKA_ID_TRACKTYPE, MATROSKA_TRACK_TYPE_AUDIO);
1860
1861
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 28 times.
29 if (!native_id)
1862 // no mkv-specific ID, use ACM mode
1863 1 put_ebml_string(pb, MATROSKA_ID_CODECID, "A_MS/ACM");
1864
1865 29 subinfo = start_ebml_master(pb, MATROSKA_ID_TRACKAUDIO, 6 + 4 * 9);
1866 29 put_ebml_uint(pb, MATROSKA_ID_AUDIOCHANNELS, par->ch_layout.nb_channels);
1867
1868 29 track->sample_rate_offset = avio_tell(pb);
1869 29 put_ebml_float (pb, MATROSKA_ID_AUDIOSAMPLINGFREQ, sample_rate);
1870
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 28 times.
29 if (output_sample_rate)
1871 1 put_ebml_float(pb, MATROSKA_ID_AUDIOOUTSAMPLINGFREQ, output_sample_rate);
1872
1873 29 bit_depth = av_get_bits_per_sample(par->codec_id);
1874
3/4
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
29 if (!bit_depth && par->codec_id != AV_CODEC_ID_ADPCM_G726) {
1875
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 13 times.
22 if (par->bits_per_raw_sample)
1876 9 bit_depth = par->bits_per_raw_sample;
1877 else
1878 13 bit_depth = av_get_bytes_per_sample(par->format) << 3;
1879 }
1880
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 28 times.
29 if (!bit_depth)
1881 1 bit_depth = par->bits_per_coded_sample;
1882
1/2
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
29 if (bit_depth)
1883 29 put_ebml_uint(pb, MATROSKA_ID_AUDIOBITDEPTH, bit_depth);
1884 29 end_ebml_master(pb, subinfo);
1885 29 break;
1886
1887 11 case AVMEDIA_TYPE_SUBTITLE:
1888
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (!native_id) {
1889 av_log(s, AV_LOG_ERROR, "Subtitle codec %d is not supported.\n", par->codec_id);
1890 return AVERROR(ENOSYS);
1891 }
1892
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)
1893 put_ebml_uint(pb, MATROSKA_ID_TRACKFLAGTEXTDESCRIPTIONS, 1);
1894
1895
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)
1896 7 native_id = MATROSKA_TRACK_TYPE_SUBTITLE;
1897
1898 11 put_ebml_uint(pb, MATROSKA_ID_TRACKTYPE, native_id);
1899 11 break;
1900 default:
1901 av_log(s, AV_LOG_ERROR, "Only audio, video, and subtitles are supported for Matroska.\n");
1902 return AVERROR(EINVAL);
1903 }
1904
1905
4/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 55 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 4 times.
61 if (!IS_WEBM(mkv) || par->codec_id != AV_CODEC_ID_WEBVTT) {
1906 uint8_t *codecpriv;
1907 int codecpriv_size, max_payload_size;
1908 57 track->codecpriv_offset = avio_tell(pb);
1909 57 ret = mkv_assemble_codecprivate(s, mkv->tmp_bc, par,
1910 57 par->extradata, par->extradata_size,
1911 native_id, qt_id,
1912 &codecpriv, &codecpriv_size, &max_payload_size);
1913
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 57 times.
57 if (ret < 0)
1914 goto fail;
1915 57 mkv_put_codecprivate(pb, max_payload_size, codecpriv, codecpriv_size);
1916 57 track->codecpriv_size = max_payload_size;
1917 }
1918
1919 61 end_ebml_master(pb, track_master);
1920 61 ret = 0;
1921 61 fail:
1922 61 ffio_reset_dyn_buf(mkv->tmp_bc);
1923
1924 61 return ret;
1925 }
1926
1927 37 static int mkv_write_tracks(AVFormatContext *s)
1928 {
1929 37 MatroskaMuxContext *mkv = s->priv_data;
1930 37 AVIOContext *pb = s->pb;
1931 37 int video_default_idx = -1, audio_default_idx = -1, subtitle_default_idx = -1;
1932 int i, ret;
1933
1934
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 if (mkv->nb_attachments == s->nb_streams)
1935 return 0;
1936
1937 37 ret = start_ebml_master_crc32(&mkv->track.bc, mkv);
1938
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 if (ret < 0)
1939 return ret;
1940
1941
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 35 times.
37 if (mkv->default_mode != DEFAULT_MODE_PASSTHROUGH) {
1942 2 int video_idx = -1, audio_idx = -1, subtitle_idx = -1;
1943
1944
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2 times.
5 for (i = s->nb_streams - 1; i >= 0; i--) {
1945 3 AVStream *st = s->streams[i];
1946
1947
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
3 switch (st->codecpar->codec_type) {
1948 #define CASE(type, variable) \
1949 case AVMEDIA_TYPE_ ## type: \
1950 variable ## _idx = i; \
1951 if (st->disposition & AV_DISPOSITION_DEFAULT) \
1952 variable ## _default_idx = i; \
1953 break;
1954 CASE(VIDEO, video)
1955
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 CASE(AUDIO, audio)
1956
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 CASE(SUBTITLE, subtitle)
1957 #undef CASE
1958 }
1959 }
1960
1961 2 video_default_idx = FFMAX(video_default_idx, video_idx);
1962 2 audio_default_idx = FFMAX(audio_default_idx, audio_idx);
1963
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (mkv->default_mode != DEFAULT_MODE_INFER_NO_SUBS)
1964 1 subtitle_default_idx = FFMAX(subtitle_default_idx, subtitle_idx);
1965 }
1966
2/2
✓ Branch 0 taken 62 times.
✓ Branch 1 taken 37 times.
99 for (i = 0; i < s->nb_streams; i++) {
1967 62 AVStream *st = s->streams[i];
1968
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 int is_default = st->disposition & AV_DISPOSITION_DEFAULT ||
1969
5/6
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 22 times.
✓ Branch 2 taken 39 times.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 39 times.
102 i == video_default_idx || i == audio_default_idx ||
1970 i == subtitle_default_idx;
1971 62 ret = mkv_write_track(s, mkv, st, &mkv->tracks[i],
1972 mkv->track.bc, is_default);
1973
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
62 if (ret < 0)
1974 return ret;
1975 }
1976
1977 37 return end_ebml_master_crc32_tentatively(pb, &mkv->track, mkv,
1978 MATROSKA_ID_TRACKS);
1979 }
1980
1981 95 static int mkv_write_simpletag(AVIOContext *pb, const AVDictionaryEntry *t)
1982 {
1983 95 EBML_WRITER(4);
1984 95 uint8_t *key = av_strdup(t->key);
1985 95 uint8_t *p = key;
1986 95 const uint8_t *lang = NULL;
1987 int ret;
1988
1989
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 95 times.
95 if (!key)
1990 return AVERROR(ENOMEM);
1991
1992
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 94 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
96 if ((p = strrchr(p, '-')) &&
1993 1 (lang = ff_convert_lang_to(p + 1, AV_LANG_ISO639_2_BIBL)))
1994 *p = 0;
1995
1996 95 p = key;
1997
2/2
✓ Branch 0 taken 1111 times.
✓ Branch 1 taken 95 times.
1206 while (*p) {
1998
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1111 times.
1111 if (*p == ' ')
1999 *p = '_';
2000
3/4
✓ Branch 0 taken 972 times.
✓ Branch 1 taken 139 times.
✓ Branch 2 taken 972 times.
✗ Branch 3 not taken.
1111 else if (*p >= 'a' && *p <= 'z')
2001 972 *p -= 'a' - 'A';
2002 1111 p++;
2003 }
2004
2005 95 ebml_writer_open_master(&writer, MATROSKA_ID_SIMPLETAG);
2006 95 ebml_writer_add_string(&writer, MATROSKA_ID_TAGNAME, key);
2007
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 95 times.
95 if (lang)
2008 ebml_writer_add_string(&writer, MATROSKA_ID_TAGLANG, lang);
2009 95 ebml_writer_add_string(&writer, MATROSKA_ID_TAGSTRING, t->value);
2010 95 ret = ebml_writer_write(&writer, pb);
2011
2012 95 av_freep(&key);
2013 95 return ret;
2014 }
2015
2016 99 static void mkv_write_tag_targets(MatroskaMuxContext *mkv, AVIOContext *pb,
2017 uint32_t elementid, uint64_t uid)
2018 {
2019 99 ebml_master targets = start_ebml_master(pb, MATROSKA_ID_TAGTARGETS,
2020 4 + 1 + 8);
2021
2/2
✓ Branch 0 taken 62 times.
✓ Branch 1 taken 37 times.
99 if (elementid)
2022 62 put_ebml_uid(pb, elementid, uid);
2023 99 end_ebml_master(pb, targets);
2024 99 }
2025
2026 141 static int mkv_check_tag_name(const char *name, uint32_t elementid)
2027 {
2028
1/2
✓ Branch 1 taken 133 times.
✗ Branch 2 not taken.
274 return av_strcasecmp(name, "title") &&
2029
2/2
✓ Branch 1 taken 124 times.
✓ Branch 2 taken 9 times.
266 av_strcasecmp(name, "stereo_mode") &&
2030
1/2
✓ Branch 1 taken 124 times.
✗ Branch 2 not taken.
257 av_strcasecmp(name, "creation_time") &&
2031
2/2
✓ Branch 1 taken 121 times.
✓ Branch 2 taken 3 times.
248 av_strcasecmp(name, "encoding_tool") &&
2032
2/2
✓ Branch 1 taken 74 times.
✓ Branch 2 taken 47 times.
245 av_strcasecmp(name, "duration") &&
2033
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 24 times.
74 (elementid != MATROSKA_ID_TAGTARGETS_TRACKUID ||
2034
4/4
✓ Branch 0 taken 133 times.
✓ Branch 1 taken 8 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 95 times.
350 av_strcasecmp(name, "language")) &&
2035
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 (elementid != MATROSKA_ID_TAGTARGETS_ATTACHUID ||
2036
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
3 (av_strcasecmp(name, "filename") &&
2037 1 av_strcasecmp(name, "mimetype")));
2038 }
2039
2040 99 static int mkv_write_tag(MatroskaMuxContext *mkv, const AVDictionary *m,
2041 AVIOContext **pb, unsigned reserved_size,
2042 uint32_t elementid, uint64_t uid)
2043 {
2044 99 const AVDictionaryEntry *t = NULL;
2045 99 AVIOContext *const tmp_bc = mkv->tmp_bc;
2046 uint8_t *buf;
2047 99 int ret = 0, size, tag_written = 0;
2048
2049 99 mkv_write_tag_targets(mkv, tmp_bc, elementid, uid);
2050
2051
2/2
✓ Branch 1 taken 141 times.
✓ Branch 2 taken 99 times.
240 while ((t = av_dict_iterate(m, t))) {
2052
2/2
✓ Branch 1 taken 95 times.
✓ Branch 2 taken 46 times.
141 if (mkv_check_tag_name(t->key, elementid)) {
2053 95 ret = mkv_write_simpletag(tmp_bc, t);
2054
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 95 times.
95 if (ret < 0)
2055 goto end;
2056 95 tag_written = 1;
2057 }
2058 }
2059
2/2
✓ Branch 0 taken 53 times.
✓ Branch 1 taken 46 times.
99 if (reserved_size)
2060 53 put_ebml_void(tmp_bc, reserved_size);
2061
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 12 times.
46 else if (!tag_written)
2062 34 goto end;
2063
2064 65 size = avio_get_dyn_buf(tmp_bc, &buf);
2065
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
65 if (tmp_bc->error) {
2066 ret = tmp_bc->error;
2067 goto end;
2068 }
2069
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 32 times.
65 if (!*pb) {
2070 33 ret = start_ebml_master_crc32(pb, mkv);
2071
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if (ret < 0)
2072 goto end;
2073 }
2074 65 put_ebml_binary(*pb, MATROSKA_ID_TAG, buf, size);
2075
2076 99 end:
2077 99 ffio_reset_dyn_buf(tmp_bc);
2078 99 return ret;
2079 }
2080
2081 37 static int mkv_write_tags(AVFormatContext *s)
2082 {
2083 37 MatroskaMuxContext *mkv = s->priv_data;
2084
4/4
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 29 times.
✓ Branch 3 taken 3 times.
37 int i, ret, seekable = IS_SEEKABLE(s->pb, mkv);
2085
2086 37 mkv->wrote_tags = 1;
2087
2088 37 ff_metadata_conv_ctx(s, ff_mkv_metadata_conv, NULL);
2089
2090 37 ret = mkv_write_tag(mkv, s->metadata, &mkv->tags.bc, 0, 0, 0);
2091
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 if (ret < 0)
2092 return ret;
2093
2094
2/2
✓ Branch 0 taken 62 times.
✓ Branch 1 taken 37 times.
99 for (i = 0; i < s->nb_streams; i++) {
2095 62 const AVStream *st = s->streams[i];
2096 62 mkv_track *track = &mkv->tracks[i];
2097
2098
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 61 times.
62 if (st->codecpar->codec_type == AVMEDIA_TYPE_ATTACHMENT)
2099 1 continue;
2100
2101
2/2
✓ Branch 0 taken 53 times.
✓ Branch 1 taken 8 times.
61 ret = mkv_write_tag(mkv, st->metadata, &mkv->tags.bc,
2102 seekable ? DURATION_SIMPLETAG_SIZE : 0,
2103 MATROSKA_ID_TAGTARGETS_TRACKUID, track->uid);
2104
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 61 times.
61 if (ret < 0)
2105 return ret;
2106
2/2
✓ Branch 0 taken 53 times.
✓ Branch 1 taken 8 times.
61 if (seekable)
2107 53 track->duration_offset = avio_tell(mkv->tags.bc) - DURATION_SIMPLETAG_SIZE;
2108 }
2109
2110
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 36 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
37 if (mkv->nb_attachments && !IS_WEBM(mkv)) {
2111
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 for (i = 0; i < s->nb_streams; i++) {
2112 3 const mkv_track *track = &mkv->tracks[i];
2113 3 const AVStream *st = s->streams[i];
2114
2115
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 if (st->codecpar->codec_type != AVMEDIA_TYPE_ATTACHMENT)
2116 2