forked from mpv-player/mpv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemux_mkv.c
3252 lines (2870 loc) · 111 KB
/
demux_mkv.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Matroska demuxer
* Copyright (C) 2004 Aurelien Jacobs <[email protected]>
* Based on the one written by Ronald Bultje for gstreamer
* and on demux_mkv.cpp from Moritz Bunkus.
*
* This file is part of mpv.
*
* mpv is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* mpv is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <stdio.h>
#include <inttypes.h>
#include <stdbool.h>
#include <math.h>
#include <assert.h>
#include <libavutil/common.h>
#include <libavutil/lzo.h>
#include <libavutil/intreadwrite.h>
#include <libavutil/avstring.h>
#include <libavcodec/avcodec.h>
#include <libavcodec/version.h>
#include "config.h"
#if HAVE_ZLIB
#include <zlib.h>
#endif
#include "mpv_talloc.h"
#include "common/av_common.h"
#include "options/m_config.h"
#include "options/m_option.h"
#include "misc/bstr.h"
#include "stream/stream.h"
#include "video/csputils.h"
#include "video/mp_image.h"
#include "demux.h"
#include "stheader.h"
#include "ebml.h"
#include "matroska.h"
#include "codec_tags.h"
#include "common/msg.h"
static const unsigned char sipr_swaps[38][2] = {
{0,63},{1,22},{2,44},{3,90},{5,81},{7,31},{8,86},{9,58},{10,36},{12,68},
{13,39},{14,73},{15,53},{16,69},{17,57},{19,88},{20,34},{21,71},{24,46},
{25,94},{26,54},{28,75},{29,50},{32,70},{33,92},{35,74},{38,85},{40,56},
{42,87},{43,65},{45,59},{48,79},{49,93},{51,89},{55,95},{61,76},{67,83},
{77,80}
};
// Map flavour to bytes per second
#define SIPR_FLAVORS 4
#define ATRC_FLAVORS 8
#define COOK_FLAVORS 34
static const int sipr_fl2bps[SIPR_FLAVORS] = { 813, 1062, 625, 2000 };
static const int atrc_fl2bps[ATRC_FLAVORS] = {
8269, 11714, 13092, 16538, 18260, 22050, 33075, 44100 };
static const int cook_fl2bps[COOK_FLAVORS] = {
1000, 1378, 2024, 2584, 4005, 5513, 8010, 4005, 750, 2498,
4048, 5513, 8010, 11973, 8010, 2584, 4005, 2067, 2584, 2584,
4005, 4005, 5513, 5513, 8010, 12059, 1550, 8010, 12059, 5513,
12016, 16408, 22911, 33506
};
enum {
MAX_NUM_LACES = 256,
};
typedef struct mkv_content_encoding {
uint64_t order, type, scope;
uint64_t comp_algo;
uint8_t *comp_settings;
int comp_settings_len;
} mkv_content_encoding_t;
typedef struct mkv_track {
int tnum;
uint64_t uid;
char *name;
struct sh_stream *stream;
char *codec_id;
char *language;
int type;
uint32_t v_width, v_height, v_dwidth, v_dheight;
bool v_dwidth_set, v_dheight_set;
double v_frate;
uint32_t colorspace;
int stereo_mode;
struct mp_colorspace color;
struct mp_spherical_params spherical;
uint32_t a_channels, a_bps;
float a_sfreq;
float a_osfreq;
double default_duration;
double codec_delay;
int default_track;
int forced_track;
unsigned char *private_data;
unsigned int private_size;
bool parse;
int64_t parse_timebase;
void *parser_tmp;
AVCodecParserContext *av_parser;
AVCodecContext *av_parser_codec;
bool require_keyframes;
/* stuff for realaudio braincancer */
double ra_pts; /* previous audio timestamp */
uint32_t sub_packet_size; ///< sub packet size, per stream
uint32_t sub_packet_h; ///< number of coded frames per block
uint32_t coded_framesize; ///< coded frame size, per stream
uint32_t audiopk_size; ///< audio packet size
unsigned char *audio_buf; ///< place to store reordered audio data
double *audio_timestamp; ///< timestamp for each audio packet
uint32_t sub_packet_cnt; ///< number of subpacket already received
/* generic content encoding support */
mkv_content_encoding_t *encodings;
int num_encodings;
/* latest added index entry for this track */
size_t last_index_entry;
} mkv_track_t;
typedef struct mkv_index {
int tnum;
int64_t timecode, duration;
uint64_t filepos; // position of the cluster which contains the packet
} mkv_index_t;
struct block_info {
uint64_t duration, discardpadding;
bool simple, keyframe, duration_known;
int64_t timecode;
mkv_track_t *track;
// Actual packet data.
AVBufferRef *laces[MAX_NUM_LACES];
int num_laces;
int64_t filepos;
struct ebml_block_additions *additions;
};
typedef struct mkv_demuxer {
struct demux_mkv_opts *opts;
int64_t segment_start, segment_end;
double duration;
mkv_track_t **tracks;
int num_tracks;
struct ebml_tags *tags;
int64_t tc_scale, cluster_tc;
uint64_t cluster_start;
uint64_t cluster_end;
mkv_index_t *indexes;
size_t num_indexes;
bool index_complete;
int index_mode;
int edition_id;
struct header_elem {
int32_t id;
int64_t pos;
bool parsed;
} *headers;
int num_headers;
int64_t skip_to_timecode;
int v_skip_to_keyframe, a_skip_to_keyframe;
int a_skip_preroll;
int subtitle_preroll;
bool index_has_durations;
bool eof_warning, keyframe_warning;
// Small queue of read but not yet returned packets. This is mostly
// temporary data, and not normally larger than 0 or 1 elements.
struct block_info *blocks;
int num_blocks;
} mkv_demuxer_t;
#define OPT_BASE_STRUCT struct demux_mkv_opts
struct demux_mkv_opts {
int subtitle_preroll;
double subtitle_preroll_secs;
double subtitle_preroll_secs_index;
int probe_duration;
int probe_start_time;
};
const struct m_sub_options demux_mkv_conf = {
.opts = (const m_option_t[]) {
OPT_CHOICE("subtitle-preroll", subtitle_preroll, 0,
({"no", 0}, {"yes", 1}, {"index", 2})),
OPT_DOUBLE("subtitle-preroll-secs", subtitle_preroll_secs,
M_OPT_MIN, .min = 0),
OPT_DOUBLE("subtitle-preroll-secs-index", subtitle_preroll_secs_index,
M_OPT_MIN, .min = 0),
OPT_CHOICE("probe-video-duration", probe_duration, 0,
({"no", 0}, {"yes", 1}, {"full", 2})),
OPT_FLAG("probe-start-time", probe_start_time, 0),
{0}
},
.size = sizeof(struct demux_mkv_opts),
.defaults = &(const struct demux_mkv_opts){
.subtitle_preroll = 2,
.subtitle_preroll_secs = 1.0,
.subtitle_preroll_secs_index = 10.0,
},
};
#define REALHEADER_SIZE 16
#define RVPROPERTIES_SIZE 34
#define RAPROPERTIES4_SIZE 56
#define RAPROPERTIES5_SIZE 70
// Maximum number of subtitle packets that are accepted for pre-roll.
// (Subtitle packets added before first A/V keyframe packet is found with seek.)
#define NUM_SUB_PREROLL_PACKETS 500
static void probe_last_timestamp(struct demuxer *demuxer, int64_t start_pos);
static void probe_first_timestamp(struct demuxer *demuxer);
static int read_next_block_into_queue(demuxer_t *demuxer);
static void free_block(struct block_info *block);
#define AAC_SYNC_EXTENSION_TYPE 0x02b7
static int aac_get_sample_rate_index(uint32_t sample_rate)
{
static const int srates[] = {
92017, 75132, 55426, 46009, 37566, 27713,
23004, 18783, 13856, 11502, 9391, 0
};
int i = 0;
while (sample_rate < srates[i])
i++;
return i;
}
static bstr demux_mkv_decode(struct mp_log *log, mkv_track_t *track,
bstr data, uint32_t type)
{
uint8_t *src = data.start;
uint8_t *orig_src = src;
uint8_t *dest = src;
uint32_t size = data.len;
for (int i = 0; i < track->num_encodings; i++) {
struct mkv_content_encoding *enc = track->encodings + i;
if (!(enc->scope & type))
continue;
if (src != dest && src != orig_src)
talloc_free(src);
src = dest; // output from last iteration is new source
if (enc->comp_algo == 0) {
#if HAVE_ZLIB
/* zlib encoded track */
if (size == 0)
continue;
z_stream zstream;
zstream.zalloc = (alloc_func) 0;
zstream.zfree = (free_func) 0;
zstream.opaque = (voidpf) 0;
if (inflateInit(&zstream) != Z_OK) {
mp_warn(log, "zlib initialization failed.\n");
goto error;
}
zstream.next_in = (Bytef *) src;
zstream.avail_in = size;
dest = NULL;
zstream.avail_out = size;
int result;
do {
if (size >= INT_MAX - 4000) {
talloc_free(dest);
dest = NULL;
inflateEnd(&zstream);
goto error;
}
size += 4000;
dest = talloc_realloc_size(track->parser_tmp, dest, size);
zstream.next_out = (Bytef *) (dest + zstream.total_out);
result = inflate(&zstream, Z_NO_FLUSH);
if (result != Z_OK && result != Z_STREAM_END) {
mp_warn(log, "zlib decompression failed.\n");
talloc_free(dest);
dest = NULL;
inflateEnd(&zstream);
goto error;
}
zstream.avail_out += 4000;
} while (zstream.avail_out == 4000 && zstream.avail_in != 0
&& result != Z_STREAM_END);
size = zstream.total_out;
inflateEnd(&zstream);
#endif
} else if (enc->comp_algo == 2) {
/* lzo encoded track */
int out_avail;
int maxlen = INT_MAX - AV_LZO_OUTPUT_PADDING;
if (size >= maxlen / 3)
goto error;
int dstlen = size * 3;
dest = NULL;
while (1) {
int srclen = size;
dest = talloc_realloc_size(track->parser_tmp, dest,
dstlen + AV_LZO_OUTPUT_PADDING);
out_avail = dstlen;
int result = av_lzo1x_decode(dest, &out_avail, src, &srclen);
if (result == 0)
break;
if (!(result & AV_LZO_OUTPUT_FULL)) {
mp_warn(log, "lzo decompression failed.\n");
talloc_free(dest);
dest = NULL;
goto error;
}
mp_trace(log, "lzo decompression buffer too small.\n");
if (dstlen >= maxlen / 2) {
talloc_free(dest);
dest = NULL;
goto error;
}
dstlen = MPMAX(1, 2 * dstlen);
}
size = dstlen - out_avail;
} else if (enc->comp_algo == 3) {
dest = talloc_size(track->parser_tmp, size + enc->comp_settings_len);
memcpy(dest, enc->comp_settings, enc->comp_settings_len);
memcpy(dest + enc->comp_settings_len, src, size);
size += enc->comp_settings_len;
}
}
error:
if (src != dest && src != orig_src)
talloc_free(src);
if (!size)
dest = NULL;
return (bstr){dest, size};
}
static int demux_mkv_read_info(demuxer_t *demuxer)
{
mkv_demuxer_t *mkv_d = demuxer->priv;
stream_t *s = demuxer->stream;
int res = 0;
MP_VERBOSE(demuxer, "|+ segment information...\n");
mkv_d->tc_scale = 1000000;
mkv_d->duration = 0;
struct ebml_info info = {0};
struct ebml_parse_ctx parse_ctx = {demuxer->log};
if (ebml_read_element(s, &parse_ctx, &info, &ebml_info_desc) < 0)
return -1;
if (info.muxing_app)
MP_VERBOSE(demuxer, "| + muxing app: %s\n", info.muxing_app);
if (info.writing_app)
MP_VERBOSE(demuxer, "| + writing app: %s\n", info.writing_app);
if (info.n_timecode_scale) {
mkv_d->tc_scale = info.timecode_scale;
MP_VERBOSE(demuxer, "| + timecode scale: %"PRId64"\n", mkv_d->tc_scale);
if (mkv_d->tc_scale < 1 || mkv_d->tc_scale > INT_MAX) {
res = -1;
goto out;
}
}
if (info.n_duration) {
mkv_d->duration = info.duration * mkv_d->tc_scale / 1e9;
MP_VERBOSE(demuxer, "| + duration: %.3fs\n",
mkv_d->duration);
demuxer->duration = mkv_d->duration;
}
if (info.title) {
mp_tags_set_str(demuxer->metadata, "TITLE", info.title);
}
if (info.n_segment_uid) {
size_t len = info.segment_uid.len;
if (len != sizeof(demuxer->matroska_data.uid.segment)) {
MP_INFO(demuxer, "segment uid invalid length %zu\n", len);
} else {
memcpy(demuxer->matroska_data.uid.segment, info.segment_uid.start,
len);
MP_VERBOSE(demuxer, "| + segment uid");
for (size_t i = 0; i < len; i++)
MP_VERBOSE(demuxer, " %02x",
demuxer->matroska_data.uid.segment[i]);
MP_VERBOSE(demuxer, "\n");
}
}
if (demuxer->params && demuxer->params->matroska_wanted_uids) {
if (info.n_segment_uid) {
for (int i = 0; i < demuxer->params->matroska_num_wanted_uids; i++) {
struct matroska_segment_uid *uid = demuxer->params->matroska_wanted_uids + i;
if (!memcmp(info.segment_uid.start, uid->segment, 16)) {
demuxer->matroska_data.uid.edition = uid->edition;
goto out;
}
}
}
MP_VERBOSE(demuxer, "This is not one of the wanted files. "
"Stopping attempt to open.\n");
res = -2;
}
out:
talloc_free(parse_ctx.talloc_ctx);
return res;
}
static void parse_trackencodings(struct demuxer *demuxer,
struct mkv_track *track,
struct ebml_content_encodings *encodings)
{
// initial allocation to be a non-NULL context before realloc
mkv_content_encoding_t *ce = talloc_size(track, 1);
for (int n_enc = 0; n_enc < encodings->n_content_encoding; n_enc++) {
struct ebml_content_encoding *enc = encodings->content_encoding + n_enc;
struct mkv_content_encoding e = {0};
e.order = enc->content_encoding_order;
if (enc->n_content_encoding_scope)
e.scope = enc->content_encoding_scope;
else
e.scope = 1;
e.type = enc->content_encoding_type;
if (enc->n_content_compression) {
struct ebml_content_compression *z = &enc->content_compression;
e.comp_algo = z->content_comp_algo;
if (z->n_content_comp_settings) {
int sz = z->content_comp_settings.len;
e.comp_settings = talloc_size(ce, sz);
memcpy(e.comp_settings, z->content_comp_settings.start, sz);
e.comp_settings_len = sz;
}
}
if (e.type == 1) {
MP_WARN(demuxer, "Track "
"number %d has been encrypted and "
"decryption has not yet been\n"
"implemented. Skipping track.\n",
track->tnum);
} else if (e.type != 0) {
MP_WARN(demuxer, "Unknown content encoding type for "
"track %u. Skipping track.\n",
track->tnum);
} else if (e.comp_algo != 0 && e.comp_algo != 2 && e.comp_algo != 3) {
MP_WARN(demuxer, "Track %d has been compressed with "
"an unknown/unsupported compression\n"
"algorithm (%"PRIu64"). Skipping track.\n",
track->tnum, e.comp_algo);
}
#if !HAVE_ZLIB
else if (e.comp_algo == 0) {
MP_WARN(demuxer, "Track %d was compressed with zlib "
"but mpv has not been compiled\n"
"with support for zlib compression. "
"Skipping track.\n",
track->tnum);
}
#endif
int i;
for (i = 0; i < n_enc; i++) {
if (e.order >= ce[i].order)
break;
}
ce = talloc_realloc(track, ce, mkv_content_encoding_t, n_enc + 1);
memmove(ce + i + 1, ce + i, (n_enc - i) * sizeof(*ce));
memcpy(ce + i, &e, sizeof(e));
}
track->encodings = ce;
track->num_encodings = encodings->n_content_encoding;
}
static void parse_trackaudio(struct demuxer *demuxer, struct mkv_track *track,
struct ebml_audio *audio)
{
if (audio->n_sampling_frequency) {
track->a_sfreq = audio->sampling_frequency;
MP_VERBOSE(demuxer, "| + Sampling frequency: %f\n", track->a_sfreq);
} else {
track->a_sfreq = 8000;
}
if (audio->n_output_sampling_frequency) {
track->a_osfreq = audio->output_sampling_frequency;
MP_VERBOSE(demuxer, "| + Output sampling frequency: %f\n", track->a_osfreq);
} else {
track->a_osfreq = track->a_sfreq;
}
if (audio->n_bit_depth) {
track->a_bps = audio->bit_depth;
MP_VERBOSE(demuxer, "| + Bit depth: %"PRIu32"\n", track->a_bps);
}
if (audio->n_channels) {
track->a_channels = audio->channels;
MP_VERBOSE(demuxer, "| + Channels: %"PRIu32"\n", track->a_channels);
} else {
track->a_channels = 1;
}
}
static void parse_trackcolour(struct demuxer *demuxer, struct mkv_track *track,
struct ebml_colour *colour)
{
// Note: As per matroska spec, the order is consistent with ISO/IEC
// 23001-8:2013/DCOR1, which is the same order used by libavutil/pixfmt.h,
// so we can just re-use our avcol_ conversion functions.
if (colour->n_matrix_coefficients) {
track->color.space = avcol_spc_to_mp_csp(colour->matrix_coefficients);
MP_VERBOSE(demuxer, "| + Matrix: %s\n",
m_opt_choice_str(mp_csp_names, track->color.space));
}
if (colour->n_primaries) {
track->color.primaries = avcol_pri_to_mp_csp_prim(colour->primaries);
MP_VERBOSE(demuxer, "| + Primaries: %s\n",
m_opt_choice_str(mp_csp_prim_names, track->color.primaries));
}
if (colour->n_transfer_characteristics) {
track->color.gamma = avcol_trc_to_mp_csp_trc(colour->transfer_characteristics);
MP_VERBOSE(demuxer, "| + Gamma: %s\n",
m_opt_choice_str(mp_csp_trc_names, track->color.gamma));
}
if (colour->n_range) {
track->color.levels = avcol_range_to_mp_csp_levels(colour->range);
MP_VERBOSE(demuxer, "| + Levels: %s\n",
m_opt_choice_str(mp_csp_levels_names, track->color.levels));
}
if (colour->n_max_cll) {
track->color.sig_peak = colour->max_cll / MP_REF_WHITE;
MP_VERBOSE(demuxer, "| + MaxCLL: %"PRIu64"\n", colour->max_cll);
}
// if MaxCLL is unavailable, try falling back to the mastering metadata
if (!track->color.sig_peak && colour->n_mastering_metadata) {
struct ebml_mastering_metadata *mastering = &colour->mastering_metadata;
if (mastering->n_luminance_max) {
track->color.sig_peak = mastering->luminance_max / MP_REF_WHITE;
MP_VERBOSE(demuxer, "| + HDR peak: %f\n", track->color.sig_peak);
}
}
}
static void parse_trackprojection(struct demuxer *demuxer, struct mkv_track *track,
struct ebml_projection *projection)
{
if (projection->n_projection_type) {
const char *name;
switch (projection->projection_type) {
case 0:
name = "rectangular";
track->spherical.type = MP_SPHERICAL_NONE;
break;
case 1:
name = "equirectangular";
track->spherical.type = MP_SPHERICAL_EQUIRECTANGULAR;
break;
default:
name = "unknown";
track->spherical.type = MP_SPHERICAL_UNKNOWN;
}
MP_VERBOSE(demuxer, "| + ProjectionType: %s (%"PRIu64")\n", name,
projection->projection_type);
}
if (projection->n_projection_private) {
MP_VERBOSE(demuxer, "| + ProjectionPrivate: %zd bytes\n",
projection->projection_private.len);
MP_WARN(demuxer, "Unknown ProjectionPrivate element.\n");
}
if (projection->n_projection_pose_yaw) {
track->spherical.ref_angles[0] = projection->projection_pose_yaw;
MP_VERBOSE(demuxer, "| + ProjectionPoseYaw: %f\n",
projection->projection_pose_yaw);
}
if (projection->n_projection_pose_pitch) {
track->spherical.ref_angles[1] = projection->projection_pose_pitch;
MP_VERBOSE(demuxer, "| + ProjectionPosePitch: %f\n",
projection->projection_pose_pitch);
}
if (projection->n_projection_pose_roll) {
track->spherical.ref_angles[2] = projection->projection_pose_roll;
MP_VERBOSE(demuxer, "| + ProjectionPoseRoll: %f\n",
projection->projection_pose_roll);
}
}
static void parse_trackvideo(struct demuxer *demuxer, struct mkv_track *track,
struct ebml_video *video)
{
if (video->n_frame_rate) {
MP_VERBOSE(demuxer, "| + Frame rate: %f (ignored)\n", video->frame_rate);
}
if (video->n_display_width) {
track->v_dwidth = video->display_width;
track->v_dwidth_set = true;
MP_VERBOSE(demuxer, "| + Display width: %"PRIu32"\n",
track->v_dwidth);
}
if (video->n_display_height) {
track->v_dheight = video->display_height;
track->v_dheight_set = true;
MP_VERBOSE(demuxer, "| + Display height: %"PRIu32"\n",
track->v_dheight);
}
if (video->n_pixel_width) {
track->v_width = video->pixel_width;
MP_VERBOSE(demuxer, "| + Pixel width: %"PRIu32"\n", track->v_width);
}
if (video->n_pixel_height) {
track->v_height = video->pixel_height;
MP_VERBOSE(demuxer, "| + Pixel height: %"PRIu32"\n", track->v_height);
}
if (video->n_colour_space && video->colour_space.len == 4) {
uint8_t *d = (uint8_t *)&video->colour_space.start[0];
track->colorspace = d[0] | (d[1] << 8) | (d[2] << 16) | (d[3] << 24);
MP_VERBOSE(demuxer, "| + Colorspace: %#"PRIx32"\n",
track->colorspace);
}
if (video->n_stereo_mode) {
const char *name = MP_STEREO3D_NAME(video->stereo_mode);
if (name) {
track->stereo_mode = video->stereo_mode;
MP_VERBOSE(demuxer, "| + StereoMode: %s\n", name);
} else {
MP_WARN(demuxer, "Unknown StereoMode: %"PRIu64"\n",
video->stereo_mode);
}
}
if (video->n_colour)
parse_trackcolour(demuxer, track, &video->colour);
if (video->n_projection)
parse_trackprojection(demuxer, track, &video->projection);
}
/**
* \brief free any data associated with given track
* \param track track of which to free data
*/
static void demux_mkv_free_trackentry(mkv_track_t *track)
{
talloc_free(track->parser_tmp);
talloc_free(track);
}
static void parse_trackentry(struct demuxer *demuxer,
struct ebml_track_entry *entry)
{
mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
struct mkv_track *track = talloc_zero(NULL, struct mkv_track);
track->last_index_entry = (size_t)-1;
track->parser_tmp = talloc_new(track);
track->tnum = entry->track_number;
if (track->tnum) {
MP_VERBOSE(demuxer, "| + Track number: %d\n", track->tnum);
} else {
MP_ERR(demuxer, "Missing track number!\n");
}
track->uid = entry->track_uid;
if (entry->name) {
track->name = talloc_strdup(track, entry->name);
MP_VERBOSE(demuxer, "| + Name: %s\n", track->name);
}
track->type = entry->track_type;
MP_VERBOSE(demuxer, "| + Track type: ");
switch (track->type) {
case MATROSKA_TRACK_AUDIO:
MP_VERBOSE(demuxer, "Audio\n");
break;
case MATROSKA_TRACK_VIDEO:
MP_VERBOSE(demuxer, "Video\n");
break;
case MATROSKA_TRACK_SUBTITLE:
MP_VERBOSE(demuxer, "Subtitle\n");
break;
default:
MP_VERBOSE(demuxer, "unknown\n");
break;
}
if (entry->n_audio) {
MP_VERBOSE(demuxer, "| + Audio track\n");
parse_trackaudio(demuxer, track, &entry->audio);
}
if (entry->n_video) {
MP_VERBOSE(demuxer, "| + Video track\n");
parse_trackvideo(demuxer, track, &entry->video);
}
if (entry->codec_id) {
track->codec_id = talloc_strdup(track, entry->codec_id);
MP_VERBOSE(demuxer, "| + Codec ID: %s\n", track->codec_id);
} else {
MP_ERR(demuxer, "Missing codec ID!\n");
track->codec_id = "";
}
if (entry->n_codec_private && entry->codec_private.len <= 0x10000000) {
int len = entry->codec_private.len;
track->private_data = talloc_size(track, len + AV_LZO_INPUT_PADDING);
memcpy(track->private_data, entry->codec_private.start, len);
track->private_size = len;
MP_VERBOSE(demuxer, "| + CodecPrivate, length %u\n", track->private_size);
}
if (entry->language) {
track->language = talloc_strdup(track, entry->language);
MP_VERBOSE(demuxer, "| + Language: %s\n", track->language);
} else {
track->language = talloc_strdup(track, "eng");
}
if (entry->n_flag_default) {
track->default_track = entry->flag_default;
MP_VERBOSE(demuxer, "| + Default flag: %d\n", track->default_track);
} else {
track->default_track = 1;
}
if (entry->n_flag_forced) {
track->forced_track = entry->flag_forced;
MP_VERBOSE(demuxer, "| + Forced flag: %d\n", track->forced_track);
}
if (entry->n_default_duration) {
track->default_duration = entry->default_duration / 1e9;
if (entry->default_duration == 0) {
MP_VERBOSE(demuxer, "| + Default duration: 0");
} else {
track->v_frate = 1e9 / entry->default_duration;
MP_VERBOSE(demuxer, "| + Default duration: %.3fms ( = %.3f fps)\n",
entry->default_duration / 1000000.0, track->v_frate);
}
}
if (entry->n_content_encodings)
parse_trackencodings(demuxer, track, &entry->content_encodings);
if (entry->n_codec_delay)
track->codec_delay = entry->codec_delay / 1e9;
mkv_d->tracks[mkv_d->num_tracks++] = track;
}
static int demux_mkv_read_tracks(demuxer_t *demuxer)
{
mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
stream_t *s = demuxer->stream;
MP_VERBOSE(demuxer, "|+ segment tracks...\n");
struct ebml_tracks tracks = {0};
struct ebml_parse_ctx parse_ctx = {demuxer->log};
if (ebml_read_element(s, &parse_ctx, &tracks, &ebml_tracks_desc) < 0)
return -1;
mkv_d->tracks = talloc_zero_array(mkv_d, struct mkv_track*,
tracks.n_track_entry);
for (int i = 0; i < tracks.n_track_entry; i++) {
MP_VERBOSE(demuxer, "| + a track...\n");
parse_trackentry(demuxer, &tracks.track_entry[i]);
}
talloc_free(parse_ctx.talloc_ctx);
return 0;
}
static void cue_index_add(demuxer_t *demuxer, int track_id, uint64_t filepos,
int64_t timecode, int64_t duration)
{
mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
MP_TARRAY_GROW(mkv_d, mkv_d->indexes, mkv_d->num_indexes);
mkv_d->indexes[mkv_d->num_indexes] = (mkv_index_t) {
.tnum = track_id,
.filepos = filepos,
.timecode = timecode,
.duration = duration,
};
mkv_d->num_indexes++;
}
static void add_block_position(demuxer_t *demuxer, struct mkv_track *track,
uint64_t filepos,
int64_t timecode, int64_t duration)
{
mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
if (mkv_d->index_complete || !track)
return;
mkv_d->index_has_durations = true;
if (track->last_index_entry != (size_t)-1) {
mkv_index_t *index = &mkv_d->indexes[track->last_index_entry];
// Never add blocks which are already covered by the index.
if (index->timecode >= timecode)
return;
}
cue_index_add(demuxer, track->tnum, filepos, timecode, duration);
track->last_index_entry = mkv_d->num_indexes - 1;
}
static int demux_mkv_read_cues(demuxer_t *demuxer)
{
mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
stream_t *s = demuxer->stream;
if (mkv_d->index_mode != 1 || mkv_d->index_complete) {
ebml_read_skip(demuxer->log, -1, s);
return 0;
}
MP_VERBOSE(demuxer, "Parsing cues...\n");
struct ebml_cues cues = {0};
struct ebml_parse_ctx parse_ctx = {demuxer->log};
if (ebml_read_element(s, &parse_ctx, &cues, &ebml_cues_desc) < 0)
return -1;
for (int i = 0; i < cues.n_cue_point; i++) {
struct ebml_cue_point *cuepoint = &cues.cue_point[i];
if (cuepoint->n_cue_time != 1 || !cuepoint->n_cue_track_positions) {
MP_WARN(demuxer, "Malformed CuePoint element\n");
goto done;
}
if (cuepoint->cue_time / 1e9 > mkv_d->duration / mkv_d->tc_scale * 10 &&
mkv_d->duration != 0)
goto done;
}
if (cues.n_cue_point <= 3) // probably too sparse and will just break seeking
goto done;
// Discard incremental index. (Keep the first entry, which must be the
// start of the file - helps with files that miss the first index entry.)
mkv_d->num_indexes = MPMIN(1, mkv_d->num_indexes);
mkv_d->index_has_durations = false;
for (int i = 0; i < cues.n_cue_point; i++) {
struct ebml_cue_point *cuepoint = &cues.cue_point[i];
uint64_t time = cuepoint->cue_time;
for (int c = 0; c < cuepoint->n_cue_track_positions; c++) {
struct ebml_cue_track_positions *trackpos =
&cuepoint->cue_track_positions[c];
uint64_t pos = mkv_d->segment_start + trackpos->cue_cluster_position;
cue_index_add(demuxer, trackpos->cue_track, pos,
time, trackpos->cue_duration);
mkv_d->index_has_durations |= trackpos->n_cue_duration > 0;
MP_TRACE(demuxer, "|+ found cue point for track %"PRIu64": "
"timecode %"PRIu64", filepos: %"PRIu64""
"offset %"PRIu64", duration %"PRIu64"\n",
trackpos->cue_track, time, pos,
trackpos->cue_relative_position, trackpos->cue_duration);
}
}
// Do not attempt to create index on the fly.
mkv_d->index_complete = true;
done:
if (!mkv_d->index_complete)
MP_WARN(demuxer, "Discarding potentially broken or useless index.\n");
talloc_free(parse_ctx.talloc_ctx);
return 0;
}
static int demux_mkv_read_chapters(struct demuxer *demuxer)
{
mkv_demuxer_t *mkv_d = demuxer->priv;
stream_t *s = demuxer->stream;
int wanted_edition = mkv_d->edition_id;
uint64_t wanted_edition_uid = demuxer->matroska_data.uid.edition;
/* A specific edition UID was requested; ignore the user option which is
* only applicable to the top-level file. */
if (wanted_edition_uid)
wanted_edition = -1;
MP_VERBOSE(demuxer, "Parsing chapters...\n");
struct ebml_chapters file_chapters = {0};
struct ebml_parse_ctx parse_ctx = {demuxer->log};
if (ebml_read_element(s, &parse_ctx, &file_chapters,
&ebml_chapters_desc) < 0)
return -1;
int selected_edition = -1;
int num_editions = file_chapters.n_edition_entry;
struct ebml_edition_entry *editions = file_chapters.edition_entry;
for (int i = 0; i < num_editions; i++) {
struct demux_edition new = {
.demuxer_id = editions[i].edition_uid,
.default_edition = editions[i].edition_flag_default,
.metadata = talloc_zero(demuxer, struct mp_tags),
};
MP_TARRAY_APPEND(demuxer, demuxer->editions, demuxer->num_editions, new);
}
if (wanted_edition >= 0 && wanted_edition < num_editions) {
selected_edition = wanted_edition;
MP_VERBOSE(demuxer, "User-specified edition: %d\n", selected_edition);
} else {
for (int i = 0; i < num_editions; i++) {
if (wanted_edition_uid &&
editions[i].edition_uid == wanted_edition_uid) {
selected_edition = i;
break;
} else if (editions[i].edition_flag_default) {
selected_edition = i;
MP_VERBOSE(demuxer, "Default edition: %d\n", i);
break;
}
}
}
if (selected_edition < 0) {
if (wanted_edition_uid) {
MP_ERR(demuxer, "Unable to find expected edition uid: %"PRIu64"\n",
wanted_edition_uid);
talloc_free(parse_ctx.talloc_ctx);
return -1;
} else {
selected_edition = 0;
}
}
for (int idx = 0; idx < num_editions; idx++) {
MP_VERBOSE(demuxer, "New edition %d\n", idx);
int warn_level = idx == selected_edition ? MSGL_WARN : MSGL_V;
if (editions[idx].n_edition_flag_default)
MP_VERBOSE(demuxer, "Default edition flag: %"PRIu64"\n",
editions[idx].edition_flag_default);
if (editions[idx].n_edition_flag_ordered)
MP_VERBOSE(demuxer, "Ordered chapter flag: %"PRIu64"\n",
editions[idx].edition_flag_ordered);
int chapter_count = editions[idx].n_chapter_atom;
struct matroska_chapter *m_chapters = NULL;
if (idx == selected_edition && editions[idx].edition_flag_ordered) {
m_chapters = talloc_array_ptrtype(demuxer, m_chapters, chapter_count);
demuxer->matroska_data.ordered_chapters = m_chapters;
demuxer->matroska_data.num_ordered_chapters = chapter_count;
}
for (int i = 0; i < chapter_count; i++) {
struct ebml_chapter_atom *ca = editions[idx].chapter_atom + i;
struct matroska_chapter chapter = {0};
char *name = "(unnamed)";
chapter.start = ca->chapter_time_start;
chapter.end = ca->chapter_time_end;
if (!ca->n_chapter_time_start)
MP_MSG(demuxer, warn_level, "Chapter lacks start time\n");