This repository has been archived by the owner on Jun 23, 2023. It is now read-only.
forked from webmproject/libwebm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webm_info.cc
1328 lines (1167 loc) · 49.5 KB
/
webm_info.cc
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
// Copyright (c) 2012 The WebM project authors. All Rights Reserved.
//
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file in the root of the source
// tree. An additional intellectual property rights grant can be found
// in the file PATENTS. All contributing project authors may
// be found in the AUTHORS file in the root of the source tree.
#include <inttypes.h>
#include <stdint.h>
#include <cstdlib>
#include <cstring>
#include <limits>
#include <memory>
#include <queue>
#include <string>
#include <vector>
#include "common/hdr_util.h"
#include "common/indent.h"
#include "common/vp9_header_parser.h"
#include "common/vp9_level_stats.h"
#include "common/webm_constants.h"
#include "common/webm_endian.h"
#include "mkvparser/mkvparser.h"
#include "mkvparser/mkvreader.h"
namespace {
using libwebm::Indent;
using libwebm::kNanosecondsPerSecond;
using libwebm::kNanosecondsPerSecondi;
using mkvparser::ContentEncoding;
using std::string;
using std::wstring;
const char VERSION_STRING[] = "1.0.4.5";
struct Options {
Options();
// Returns true if |value| matches -|option| or -no|option|.
static bool MatchesBooleanOption(const string& option, const string& value);
// Set all of the member variables to |value|.
void SetAll(bool value);
bool output_video;
bool output_audio;
bool output_size;
bool output_offset;
bool output_seconds;
bool output_ebml_header;
bool output_segment;
bool output_seekhead;
bool output_segment_info;
bool output_tracks;
bool output_clusters;
bool output_blocks;
bool output_codec_info;
bool output_clusters_size;
bool output_encrypted_info;
bool output_cues;
bool output_frame_stats;
bool output_vp9_level;
};
Options::Options()
: output_video(true),
output_audio(true),
output_size(false),
output_offset(false),
output_seconds(true),
output_ebml_header(true),
output_segment(true),
output_seekhead(false),
output_segment_info(true),
output_tracks(true),
output_clusters(false),
output_blocks(false),
output_codec_info(false),
output_clusters_size(false),
output_encrypted_info(false),
output_cues(false),
output_frame_stats(false),
output_vp9_level(false) {}
void Options::SetAll(bool value) {
output_video = value;
output_audio = value;
output_size = value;
output_offset = value;
output_ebml_header = value;
output_seconds = value;
output_segment = value;
output_segment_info = value;
output_tracks = value;
output_clusters = value;
output_blocks = value;
output_codec_info = value;
output_clusters_size = value;
output_encrypted_info = value;
output_cues = value;
output_frame_stats = value;
output_vp9_level = value;
}
bool Options::MatchesBooleanOption(const string& option, const string& value) {
const string opt = "-" + option;
const string noopt = "-no" + option;
return value == opt || value == noopt;
}
struct FrameStats {
FrameStats()
: frames(0),
displayed_frames(0),
first_altref(true),
frames_since_last_altref(0),
minimum_altref_distance(std::numeric_limits<int>::max()),
min_altref_end_ns(0),
max_window_size(0),
max_window_end_ns(0) {}
int frames;
int displayed_frames;
bool first_altref;
int frames_since_last_altref;
int minimum_altref_distance;
int64_t min_altref_end_ns;
std::queue<int64_t> window;
int64_t max_window_size;
int64_t max_window_end_ns;
};
void Usage() {
printf("Usage: webm_info [options] -i input\n");
printf("\n");
printf("Main options:\n");
printf(" -h | -? show help\n");
printf(" -v show version\n");
printf(" -all Enable all output options.\n");
printf(" -video Output video tracks (true)\n");
printf(" -audio Output audio tracks (true)\n");
printf(" -size Output element sizes (false)\n");
printf(" -offset Output element offsets (false)\n");
printf(" -times_seconds Output times as seconds (true)\n");
printf(" -ebml_header Output EBML header (true)\n");
printf(" -segment Output Segment (true)\n");
printf(" -seekhead Output SeekHead (false)\n");
printf(" -segment_info Output SegmentInfo (true)\n");
printf(" -tracks Output Tracks (true)\n");
printf(" -clusters Output Clusters (false)\n");
printf(" -blocks Output Blocks (false)\n");
printf(" -codec_info Output video codec information (false)\n");
printf(" -clusters_size Output Total Clusters size (false)\n");
printf(" -encrypted_info Output encrypted frame info (false)\n");
printf(" -cues Output Cues entries (false)\n");
printf(" -frame_stats Output frame stats (VP9)(false)\n");
printf(" -vp9_level Output VP9 level(false)\n");
printf("\nOutput options may be negated by prefixing 'no'.\n");
}
// TODO(fgalligan): Add support for non-ascii.
wstring UTF8ToWideString(const char* str) {
wstring wstr;
if (str == NULL)
return wstr;
string temp_str(str, strlen(str));
wstr.assign(temp_str.begin(), temp_str.end());
return wstr;
}
string ToString(const char* str) { return string((str == NULL) ? "" : str); }
void OutputEBMLHeader(const mkvparser::EBMLHeader& ebml, FILE* o,
Indent* indent) {
fprintf(o, "EBML Header:\n");
indent->Adjust(libwebm::kIncreaseIndent);
fprintf(o, "%sEBMLVersion : %lld\n", indent->indent_str().c_str(),
ebml.m_version);
fprintf(o, "%sEBMLReadVersion : %lld\n", indent->indent_str().c_str(),
ebml.m_readVersion);
fprintf(o, "%sEBMLMaxIDLength : %lld\n", indent->indent_str().c_str(),
ebml.m_maxIdLength);
fprintf(o, "%sEBMLMaxSizeLength : %lld\n", indent->indent_str().c_str(),
ebml.m_maxSizeLength);
fprintf(o, "%sDoc Type : %s\n", indent->indent_str().c_str(),
ebml.m_docType);
fprintf(o, "%sDocTypeVersion : %lld\n", indent->indent_str().c_str(),
ebml.m_docTypeVersion);
fprintf(o, "%sDocTypeReadVersion: %lld\n", indent->indent_str().c_str(),
ebml.m_docTypeReadVersion);
indent->Adjust(libwebm::kDecreaseIndent);
}
void OutputSegment(const mkvparser::Segment& segment, const Options& options,
FILE* o) {
fprintf(o, "Segment:");
if (options.output_offset)
fprintf(o, " @: %lld", segment.m_element_start);
if (options.output_size)
fprintf(o, " size: %lld",
segment.m_size + segment.m_start - segment.m_element_start);
fprintf(o, "\n");
}
bool OutputSeekHead(const mkvparser::Segment& segment, const Options& options,
FILE* o, Indent* indent) {
const mkvparser::SeekHead* const seekhead = segment.GetSeekHead();
if (!seekhead) {
// SeekHeads are optional.
return true;
}
fprintf(o, "%sSeekHead:", indent->indent_str().c_str());
if (options.output_offset)
fprintf(o, " @: %lld", seekhead->m_element_start);
if (options.output_size)
fprintf(o, " size: %lld", seekhead->m_element_size);
fprintf(o, "\n");
indent->Adjust(libwebm::kIncreaseIndent);
for (int i = 0; i < seekhead->GetCount(); ++i) {
const mkvparser::SeekHead::Entry* const entry = seekhead->GetEntry(i);
if (!entry) {
fprintf(stderr, "Error retrieving SeekHead entry #%d\n", i);
return false;
}
fprintf(o, "%sEntry[%d]", indent->indent_str().c_str(), i);
if (options.output_offset)
fprintf(o, " @: %lld", entry->element_start);
if (options.output_size)
fprintf(o, " size: %lld", entry->element_size);
fprintf(o, "\n");
indent->Adjust(libwebm::kIncreaseIndent);
const char* const entry_indent = indent->indent_str().c_str();
// TODO(jzern): 1) known ids could be stringified. 2) ids could be
// reencoded to EBML for ease of lookup.
fprintf(o, "%sSeek ID : %llx\n", entry_indent, entry->id);
fprintf(o, "%sSeek position : %lld\n", entry_indent, entry->pos);
indent->Adjust(libwebm::kDecreaseIndent);
}
for (int i = 0; i < seekhead->GetVoidElementCount(); ++i) {
const mkvparser::SeekHead::VoidElement* const entry =
seekhead->GetVoidElement(i);
if (!entry) {
fprintf(stderr, "Error retrieving SeekHead void element #%d\n", i);
return false;
}
fprintf(o, "%sVoid element[%d]", indent->indent_str().c_str(), i);
if (options.output_offset)
fprintf(o, " @: %lld", entry->element_start);
if (options.output_size)
fprintf(o, " size: %lld", entry->element_size);
fprintf(o, "\n");
}
indent->Adjust(libwebm::kDecreaseIndent);
return true;
}
bool OutputSegmentInfo(const mkvparser::Segment& segment,
const Options& options, FILE* o, Indent* indent) {
const mkvparser::SegmentInfo* const segment_info = segment.GetInfo();
if (!segment_info) {
fprintf(stderr, "SegmentInfo was NULL.\n");
return false;
}
const int64_t timecode_scale = segment_info->GetTimeCodeScale();
const int64_t duration_ns = segment_info->GetDuration();
const wstring title = UTF8ToWideString(segment_info->GetTitleAsUTF8());
const wstring muxing_app =
UTF8ToWideString(segment_info->GetMuxingAppAsUTF8());
const wstring writing_app =
UTF8ToWideString(segment_info->GetWritingAppAsUTF8());
fprintf(o, "%sSegmentInfo:", indent->indent_str().c_str());
if (options.output_offset)
fprintf(o, " @: %lld", segment_info->m_element_start);
if (options.output_size)
fprintf(o, " size: %lld", segment_info->m_element_size);
fprintf(o, "\n");
indent->Adjust(libwebm::kIncreaseIndent);
fprintf(o, "%sTimecodeScale : %" PRId64 " \n", indent->indent_str().c_str(),
timecode_scale);
if (options.output_seconds)
fprintf(o, "%sDuration(secs): %g\n", indent->indent_str().c_str(),
duration_ns / kNanosecondsPerSecond);
else
fprintf(o, "%sDuration(nano): %" PRId64 "\n", indent->indent_str().c_str(),
duration_ns);
if (!title.empty())
fprintf(o, "%sTitle : %ls\n", indent->indent_str().c_str(),
title.c_str());
if (!muxing_app.empty())
fprintf(o, "%sMuxingApp : %ls\n", indent->indent_str().c_str(),
muxing_app.c_str());
if (!writing_app.empty())
fprintf(o, "%sWritingApp : %ls\n", indent->indent_str().c_str(),
writing_app.c_str());
indent->Adjust(libwebm::kDecreaseIndent);
return true;
}
bool OutputTracks(const mkvparser::Segment& segment, const Options& options,
FILE* o, Indent* indent) {
const mkvparser::Tracks* const tracks = segment.GetTracks();
if (!tracks) {
fprintf(stderr, "Tracks was NULL.\n");
return false;
}
fprintf(o, "%sTracks:", indent->indent_str().c_str());
if (options.output_offset)
fprintf(o, " @: %lld", tracks->m_element_start);
if (options.output_size)
fprintf(o, " size: %lld", tracks->m_element_size);
fprintf(o, "\n");
unsigned int i = 0;
const unsigned long j = tracks->GetTracksCount();
while (i != j) {
const mkvparser::Track* const track = tracks->GetTrackByIndex(i++);
if (track == NULL)
continue;
indent->Adjust(libwebm::kIncreaseIndent);
fprintf(o, "%sTrack:", indent->indent_str().c_str());
if (options.output_offset)
fprintf(o, " @: %lld", track->m_element_start);
if (options.output_size)
fprintf(o, " size: %lld", track->m_element_size);
fprintf(o, "\n");
const int64_t track_type = track->GetType();
const int64_t track_number = track->GetNumber();
const wstring track_name = UTF8ToWideString(track->GetNameAsUTF8());
indent->Adjust(libwebm::kIncreaseIndent);
fprintf(o, "%sTrackType : %" PRId64 "\n", indent->indent_str().c_str(),
track_type);
fprintf(o, "%sTrackNumber : %" PRId64 "\n", indent->indent_str().c_str(),
track_number);
if (!track_name.empty())
fprintf(o, "%sName : %ls\n", indent->indent_str().c_str(),
track_name.c_str());
const char* const codec_id = track->GetCodecId();
if (codec_id)
fprintf(o, "%sCodecID : %s\n", indent->indent_str().c_str(),
codec_id);
const wstring codec_name = UTF8ToWideString(track->GetCodecNameAsUTF8());
if (!codec_name.empty())
fprintf(o, "%sCodecName : %ls\n", indent->indent_str().c_str(),
codec_name.c_str());
size_t private_size;
const unsigned char* const private_data =
track->GetCodecPrivate(private_size);
if (private_data) {
fprintf(o, "%sPrivateData(size): %d\n", indent->indent_str().c_str(),
static_cast<int>(private_size));
if (track_type == mkvparser::Track::kVideo) {
const std::string codec_id = ToString(track->GetCodecId());
const std::string v_vp9 = "V_VP9";
if (codec_id == v_vp9) {
libwebm::Vp9CodecFeatures features;
if (!libwebm::ParseVpxCodecPrivate(private_data,
static_cast<int32_t>(private_size),
&features)) {
fprintf(stderr, "Error parsing VpxCodecPrivate.\n");
return false;
}
if (features.profile != -1)
fprintf(o, "%sVP9 profile : %d\n",
indent->indent_str().c_str(), features.profile);
if (features.level != -1)
fprintf(o, "%sVP9 level : %d\n",
indent->indent_str().c_str(), features.level);
if (features.bit_depth != -1)
fprintf(o, "%sVP9 bit_depth : %d\n",
indent->indent_str().c_str(), features.bit_depth);
if (features.chroma_subsampling != -1)
fprintf(o, "%sVP9 chroma subsampling : %d\n",
indent->indent_str().c_str(), features.chroma_subsampling);
}
}
}
const uint64_t default_duration = track->GetDefaultDuration();
if (default_duration > 0)
fprintf(o, "%sDefaultDuration: %" PRIu64 "\n",
indent->indent_str().c_str(), default_duration);
if (track->GetContentEncodingCount() > 0) {
// Only check the first content encoding.
const ContentEncoding* const encoding =
track->GetContentEncodingByIndex(0);
if (!encoding) {
printf("Could not get first ContentEncoding.\n");
return false;
}
fprintf(o, "%sContentEncodingOrder : %lld\n",
indent->indent_str().c_str(), encoding->encoding_order());
fprintf(o, "%sContentEncodingScope : %lld\n",
indent->indent_str().c_str(), encoding->encoding_scope());
fprintf(o, "%sContentEncodingType : %lld\n",
indent->indent_str().c_str(), encoding->encoding_type());
if (encoding->GetEncryptionCount() > 0) {
// Only check the first encryption.
const ContentEncoding::ContentEncryption* const encryption =
encoding->GetEncryptionByIndex(0);
if (!encryption) {
printf("Could not get first ContentEncryption.\n");
return false;
}
fprintf(o, "%sContentEncAlgo : %lld\n",
indent->indent_str().c_str(), encryption->algo);
if (encryption->key_id_len > 0) {
fprintf(o, "%sContentEncKeyID : ", indent->indent_str().c_str());
for (int k = 0; k < encryption->key_id_len; ++k) {
fprintf(o, "0x%02x, ", encryption->key_id[k]);
}
fprintf(o, "\n");
}
if (encryption->signature_len > 0) {
fprintf(o, "%sContentSignature : 0x",
indent->indent_str().c_str());
for (int k = 0; k < encryption->signature_len; ++k) {
fprintf(o, "%x", encryption->signature[k]);
}
fprintf(o, "\n");
}
if (encryption->sig_key_id_len > 0) {
fprintf(o, "%sContentSigKeyID : 0x",
indent->indent_str().c_str());
for (int k = 0; k < encryption->sig_key_id_len; ++k) {
fprintf(o, "%x", encryption->sig_key_id[k]);
}
fprintf(o, "\n");
}
fprintf(o, "%sContentSigAlgo : %lld\n",
indent->indent_str().c_str(), encryption->sig_algo);
fprintf(o, "%sContentSigHashAlgo : %lld\n",
indent->indent_str().c_str(), encryption->sig_hash_algo);
const ContentEncoding::ContentEncAESSettings& aes =
encryption->aes_settings;
fprintf(o, "%sCipherMode : %lld\n",
indent->indent_str().c_str(), aes.cipher_mode);
}
}
if (track_type == mkvparser::Track::kVideo) {
const mkvparser::VideoTrack* const video_track =
static_cast<const mkvparser::VideoTrack*>(track);
const int64_t width = video_track->GetWidth();
const int64_t height = video_track->GetHeight();
const int64_t display_width = video_track->GetDisplayWidth();
const int64_t display_height = video_track->GetDisplayHeight();
const int64_t display_unit = video_track->GetDisplayUnit();
const double frame_rate = video_track->GetFrameRate();
fprintf(o, "%sPixelWidth : %" PRId64 "\n", indent->indent_str().c_str(),
width);
fprintf(o, "%sPixelHeight : %" PRId64 "\n", indent->indent_str().c_str(),
height);
if (frame_rate > 0.0)
fprintf(o, "%sFrameRate : %g\n", indent->indent_str().c_str(),
video_track->GetFrameRate());
if (display_unit > 0 || display_width != width ||
display_height != height) {
fprintf(o, "%sDisplayWidth : %" PRId64 "\n",
indent->indent_str().c_str(), display_width);
fprintf(o, "%sDisplayHeight : %" PRId64 "\n",
indent->indent_str().c_str(), display_height);
fprintf(o, "%sDisplayUnit : %" PRId64 "\n",
indent->indent_str().c_str(), display_unit);
}
const mkvparser::Colour* const colour = video_track->GetColour();
if (colour) {
// TODO(fgalligan): Add support for Colour's address and size.
fprintf(o, "%sColour:\n", indent->indent_str().c_str());
indent->Adjust(libwebm::kIncreaseIndent);
const int64_t matrix_coefficients = colour->matrix_coefficients;
const int64_t bits_per_channel = colour->bits_per_channel;
const int64_t chroma_subsampling_horz = colour->chroma_subsampling_horz;
const int64_t chroma_subsampling_vert = colour->chroma_subsampling_vert;
const int64_t cb_subsampling_horz = colour->cb_subsampling_horz;
const int64_t cb_subsampling_vert = colour->cb_subsampling_vert;
const int64_t chroma_siting_horz = colour->chroma_siting_horz;
const int64_t chroma_siting_vert = colour->chroma_siting_vert;
const int64_t range = colour->range;
const int64_t transfer_characteristics =
colour->transfer_characteristics;
const int64_t primaries = colour->primaries;
const int64_t max_cll = colour->max_cll;
const int64_t max_fall = colour->max_fall;
if (matrix_coefficients != mkvparser::Colour::kValueNotPresent)
fprintf(o, "%sMatrixCoefficients : %" PRId64 "\n",
indent->indent_str().c_str(), matrix_coefficients);
if (bits_per_channel != mkvparser::Colour::kValueNotPresent)
fprintf(o, "%sBitsPerChannel : %" PRId64 "\n",
indent->indent_str().c_str(), bits_per_channel);
if (chroma_subsampling_horz != mkvparser::Colour::kValueNotPresent)
fprintf(o, "%sChromaSubsamplingHorz : %" PRId64 "\n",
indent->indent_str().c_str(), chroma_subsampling_horz);
if (chroma_subsampling_vert != mkvparser::Colour::kValueNotPresent)
fprintf(o, "%sChromaSubsamplingVert : %" PRId64 "\n",
indent->indent_str().c_str(), chroma_subsampling_vert);
if (cb_subsampling_horz != mkvparser::Colour::kValueNotPresent)
fprintf(o, "%sCbSubsamplingHorz : %" PRId64 "\n",
indent->indent_str().c_str(), cb_subsampling_horz);
if (cb_subsampling_vert != mkvparser::Colour::kValueNotPresent)
fprintf(o, "%sCbSubsamplingVert : %" PRId64 "\n",
indent->indent_str().c_str(), cb_subsampling_vert);
if (chroma_siting_horz != mkvparser::Colour::kValueNotPresent)
fprintf(o, "%sChromaSitingHorz : %" PRId64 "\n",
indent->indent_str().c_str(), chroma_siting_horz);
if (chroma_siting_vert != mkvparser::Colour::kValueNotPresent)
fprintf(o, "%sChromaSitingVert : %" PRId64 "\n",
indent->indent_str().c_str(), chroma_siting_vert);
if (range != mkvparser::Colour::kValueNotPresent)
fprintf(o, "%sRange : %" PRId64 "\n",
indent->indent_str().c_str(), range);
if (transfer_characteristics != mkvparser::Colour::kValueNotPresent)
fprintf(o, "%sTransferCharacteristics : %" PRId64 "\n",
indent->indent_str().c_str(), transfer_characteristics);
if (primaries != mkvparser::Colour::kValueNotPresent)
fprintf(o, "%sPrimaries : %" PRId64 "\n",
indent->indent_str().c_str(), primaries);
if (max_cll != mkvparser::Colour::kValueNotPresent)
fprintf(o, "%sMaxCLL : %" PRId64 "\n",
indent->indent_str().c_str(), max_cll);
if (max_fall != mkvparser::Colour::kValueNotPresent)
fprintf(o, "%sMaxFALL : %" PRId64 "\n",
indent->indent_str().c_str(), max_fall);
const mkvparser::MasteringMetadata* const metadata =
colour->mastering_metadata;
if (metadata) {
// TODO(fgalligan): Add support for MasteringMetadata's address and
// size.
fprintf(o, "%sMasteringMetadata:\n", indent->indent_str().c_str());
indent->Adjust(libwebm::kIncreaseIndent);
const mkvparser::PrimaryChromaticity* const red = metadata->r;
const mkvparser::PrimaryChromaticity* const green = metadata->g;
const mkvparser::PrimaryChromaticity* const blue = metadata->b;
const mkvparser::PrimaryChromaticity* const white =
metadata->white_point;
const float max = metadata->luminance_max;
const float min = metadata->luminance_min;
if (red) {
fprintf(o, "%sPrimaryRChromaticityX : %g\n",
indent->indent_str().c_str(), red->x);
fprintf(o, "%sPrimaryRChromaticityY : %g\n",
indent->indent_str().c_str(), red->y);
}
if (green) {
fprintf(o, "%sPrimaryGChromaticityX : %g\n",
indent->indent_str().c_str(), green->x);
fprintf(o, "%sPrimaryGChromaticityY : %g\n",
indent->indent_str().c_str(), green->y);
}
if (blue) {
fprintf(o, "%sPrimaryBChromaticityX : %g\n",
indent->indent_str().c_str(), blue->x);
fprintf(o, "%sPrimaryBChromaticityY : %g\n",
indent->indent_str().c_str(), blue->y);
}
if (white) {
fprintf(o, "%sWhitePointChromaticityX : %g\n",
indent->indent_str().c_str(), white->x);
fprintf(o, "%sWhitePointChromaticityY : %g\n",
indent->indent_str().c_str(), white->y);
}
if (max != mkvparser::MasteringMetadata::kValueNotPresent)
fprintf(o, "%sLuminanceMax : %g\n",
indent->indent_str().c_str(), max);
if (min != mkvparser::MasteringMetadata::kValueNotPresent)
fprintf(o, "%sLuminanceMin : %g\n",
indent->indent_str().c_str(), min);
indent->Adjust(libwebm::kDecreaseIndent);
}
indent->Adjust(libwebm::kDecreaseIndent);
}
const mkvparser::Projection* const projection =
video_track->GetProjection();
if (projection) {
fprintf(o, "%sProjection:\n", indent->indent_str().c_str());
indent->Adjust(libwebm::kIncreaseIndent);
const int projection_type = static_cast<int>(projection->type);
const int kTypeNotPresent =
static_cast<int>(mkvparser::Projection::kTypeNotPresent);
const float kValueNotPresent = mkvparser::Projection::kValueNotPresent;
if (projection_type != kTypeNotPresent)
fprintf(o, "%sProjectionType : %d\n",
indent->indent_str().c_str(), projection_type);
if (projection->private_data)
fprintf(o, "%sProjectionPrivate(size) : %d\n",
indent->indent_str().c_str(),
static_cast<int>(projection->private_data_length));
if (projection->pose_yaw != kValueNotPresent)
fprintf(o, "%sProjectionPoseYaw : %g\n",
indent->indent_str().c_str(), projection->pose_yaw);
if (projection->pose_pitch != kValueNotPresent)
fprintf(o, "%sProjectionPosePitch : %g\n",
indent->indent_str().c_str(), projection->pose_pitch);
if (projection->pose_roll != kValueNotPresent)
fprintf(o, "%sProjectionPoseRoll : %g\n",
indent->indent_str().c_str(), projection->pose_roll);
indent->Adjust(libwebm::kDecreaseIndent);
}
} else if (track_type == mkvparser::Track::kAudio) {
const mkvparser::AudioTrack* const audio_track =
static_cast<const mkvparser::AudioTrack*>(track);
const int64_t channels = audio_track->GetChannels();
const int64_t bit_depth = audio_track->GetBitDepth();
const uint64_t codec_delay = audio_track->GetCodecDelay();
const uint64_t seek_preroll = audio_track->GetSeekPreRoll();
fprintf(o, "%sChannels : %" PRId64 "\n",
indent->indent_str().c_str(), channels);
if (bit_depth > 0)
fprintf(o, "%sBitDepth : %" PRId64 "\n",
indent->indent_str().c_str(), bit_depth);
fprintf(o, "%sSamplingFrequency: %g\n", indent->indent_str().c_str(),
audio_track->GetSamplingRate());
if (codec_delay)
fprintf(o, "%sCodecDelay : %" PRIu64 "\n",
indent->indent_str().c_str(), codec_delay);
if (seek_preroll)
fprintf(o, "%sSeekPreRoll : %" PRIu64 "\n",
indent->indent_str().c_str(), seek_preroll);
}
indent->Adjust(libwebm::kDecreaseIndent * 2);
}
return true;
}
// libvpx reference: vp9/vp9_dx_iface.c
void ParseSuperframeIndex(const uint8_t* data, size_t data_sz,
uint32_t sizes[8], int* count) {
const uint8_t marker = data[data_sz - 1];
*count = 0;
if ((marker & 0xe0) == 0xc0) {
const int frames = (marker & 0x7) + 1;
const int mag = ((marker >> 3) & 0x3) + 1;
const size_t index_sz = 2 + mag * frames;
if (data_sz >= index_sz && data[data_sz - index_sz] == marker) {
// found a valid superframe index
const uint8_t* x = data + data_sz - index_sz + 1;
for (int i = 0; i < frames; ++i) {
uint32_t this_sz = 0;
for (int j = 0; j < mag; ++j) {
this_sz |= (*x++) << (j * 8);
}
sizes[i] = this_sz;
}
*count = frames;
}
}
}
void PrintVP9Info(const uint8_t* data, int size, FILE* o, int64_t time_ns,
FrameStats* stats, vp9_parser::Vp9HeaderParser* parser,
vp9_parser::Vp9LevelStats* level_stats) {
if (size < 1)
return;
uint32_t sizes[8];
int i = 0, count = 0;
ParseSuperframeIndex(data, size, sizes, &count);
// Remove all frames that are less than window size.
while (!stats->window.empty() &&
stats->window.front() < (time_ns - (kNanosecondsPerSecondi - 1)))
stats->window.pop();
do {
const size_t frame_length = (count > 0) ? sizes[i] : size;
if (frame_length > std::numeric_limits<int>::max() ||
static_cast<int>(frame_length) > size) {
fprintf(o, " invalid VP9 frame size (%u)\n",
static_cast<uint32_t>(frame_length));
return;
}
if (!parser->ParseUncompressedHeader(data, frame_length))
return;
level_stats->AddFrame(*parser, time_ns);
// const int frame_marker = (data[0] >> 6) & 0x3;
const int version = parser->profile();
const int key = parser->key();
const int altref_frame = parser->altref();
const int error_resilient_mode = parser->error_resilient_mode();
const int column_tiles = parser->column_tiles();
const int frame_parallel_mode = parser->frame_parallel_mode();
if (key &&
!(size >= 4 && data[1] == 0x49 && data[2] == 0x83 && data[3] == 0x42)) {
fprintf(o, " invalid VP9 signature");
return;
}
stats->window.push(time_ns);
++stats->frames;
if (altref_frame) {
const int delta_altref = stats->frames_since_last_altref;
if (stats->first_altref) {
stats->first_altref = false;
} else if (delta_altref < stats->minimum_altref_distance) {
stats->minimum_altref_distance = delta_altref;
stats->min_altref_end_ns = time_ns;
}
stats->frames_since_last_altref = 0;
} else {
++stats->frames_since_last_altref;
++stats->displayed_frames;
}
if (count > 0) {
fprintf(o, " packed [%d]: {", i);
}
fprintf(o, " key:%d v:%d altref:%d errm:%d ct:%d fpm:%d", key, version,
altref_frame, error_resilient_mode, column_tiles,
frame_parallel_mode);
if (key && size > 4) {
fprintf(o, " cs:%d", parser->color_space());
}
if (count > 0) {
fprintf(o, " size: %u }", sizes[i]);
data += sizes[i];
size -= sizes[i];
}
++i;
} while (i < count);
if (stats->max_window_size < static_cast<int64_t>(stats->window.size())) {
stats->max_window_size = stats->window.size();
stats->max_window_end_ns = time_ns;
}
}
void PrintVP8Info(const uint8_t* data, int size, FILE* o) {
if (size < 3)
return;
const uint32_t bits = data[0] | (data[1] << 8) | (data[2] << 16);
const int key = !(bits & 0x1);
const int altref_frame = !((bits >> 4) & 0x1);
const int version = (bits >> 1) & 0x7;
const int partition_length = (bits >> 5) & 0x7FFFF;
if (key &&
!(size >= 6 && data[3] == 0x9d && data[4] == 0x01 && data[5] == 0x2a)) {
fprintf(o, " invalid VP8 signature");
return;
}
fprintf(o, " key:%d v:%d altref:%d partition_length:%d", key, version,
altref_frame, partition_length);
}
// Prints the partition offsets of the sub-sample encryption. |data| must point
// to an encrypted frame just after the signal byte. Returns the number of
// bytes read from the sub-sample partition information.
int PrintSubSampleEncryption(const uint8_t* data, int size, FILE* o) {
int read_end = sizeof(uint64_t);
// Skip past IV.
if (size < read_end)
return 0;
data += sizeof(uint64_t);
// Read number of partitions.
read_end += sizeof(uint8_t);
if (size < read_end)
return 0;
const int num_partitions = data[0];
data += sizeof(uint8_t);
// Read partitions.
for (int i = 0; i < num_partitions; ++i) {
read_end += sizeof(uint32_t);
if (size < read_end)
return 0;
uint32_t partition_offset;
memcpy(&partition_offset, data, sizeof(partition_offset));
partition_offset = libwebm::bigendian_to_host(partition_offset);
fprintf(o, " off[%d]:%u", i, partition_offset);
data += sizeof(uint32_t);
}
return read_end;
}
bool OutputCluster(const mkvparser::Cluster& cluster,
const mkvparser::Tracks& tracks, const Options& options,
FILE* o, mkvparser::MkvReader* reader, Indent* indent,
int64_t* clusters_size, FrameStats* stats,
vp9_parser::Vp9HeaderParser* parser,
vp9_parser::Vp9LevelStats* level_stats) {
if (clusters_size) {
// Load the Cluster.
const mkvparser::BlockEntry* block_entry;
long status = cluster.GetFirst(block_entry);
if (status) {
fprintf(stderr, "Could not get first Block of Cluster.\n");
return false;
}
*clusters_size += cluster.GetElementSize();
}
if (options.output_clusters) {
const int64_t time_ns = cluster.GetTime();
const int64_t duration_ns = cluster.GetLastTime() - cluster.GetFirstTime();
fprintf(o, "%sCluster:", indent->indent_str().c_str());
if (options.output_offset)
fprintf(o, " @: %lld", cluster.m_element_start);
if (options.output_size)
fprintf(o, " size: %lld", cluster.GetElementSize());
fprintf(o, "\n");
indent->Adjust(libwebm::kIncreaseIndent);
if (options.output_seconds)
fprintf(o, "%sTimecode (sec) : %g\n", indent->indent_str().c_str(),
time_ns / kNanosecondsPerSecond);
else
fprintf(o, "%sTimecode (nano): %" PRId64 "\n",
indent->indent_str().c_str(), time_ns);
if (options.output_seconds)
fprintf(o, "%sDuration (sec) : %g\n", indent->indent_str().c_str(),
duration_ns / kNanosecondsPerSecond);
else
fprintf(o, "%sDuration (nano): %" PRId64 "\n",
indent->indent_str().c_str(), duration_ns);
fprintf(o, "%s# Blocks : %ld\n", indent->indent_str().c_str(),
cluster.GetEntryCount());
}
if (options.output_blocks) {
const mkvparser::BlockEntry* block_entry;
long status = cluster.GetFirst(block_entry);
if (status) {
fprintf(stderr, "Could not get first Block of Cluster.\n");
return false;
}
std::vector<unsigned char> vector_data;
while (block_entry != NULL && !block_entry->EOS()) {
const mkvparser::Block* const block = block_entry->GetBlock();
if (!block) {
fprintf(stderr, "Could not getblock entry.\n");
return false;
}
const unsigned int track_number =
static_cast<unsigned int>(block->GetTrackNumber());
const mkvparser::Track* track = tracks.GetTrackByNumber(track_number);
if (!track) {
fprintf(stderr, "Could not get Track.\n");
return false;
}
const int64_t track_type = track->GetType();
if ((track_type == mkvparser::Track::kVideo && options.output_video) ||
(track_type == mkvparser::Track::kAudio && options.output_audio)) {
const int64_t time_ns = block->GetTime(&cluster);
const bool is_key = block->IsKey();
if (block_entry->GetKind() == mkvparser::BlockEntry::kBlockGroup) {
fprintf(o, "%sBlockGroup:\n", indent->indent_str().c_str());
indent->Adjust(libwebm::kIncreaseIndent);
}
fprintf(o, "%sBlock: type:%s frame:%s", indent->indent_str().c_str(),
track_type == mkvparser::Track::kVideo ? "V" : "A",
is_key ? "I" : "P");
if (options.output_seconds)
fprintf(o, " secs:%5g", time_ns / kNanosecondsPerSecond);
else
fprintf(o, " nano:%10" PRId64, time_ns);
if (options.output_offset)
fprintf(o, " @_payload: %lld", block->m_start);
if (options.output_size)
fprintf(o, " size_payload: %lld", block->m_size);
const uint8_t KEncryptedBit = 0x1;
const uint8_t kSubSampleBit = 0x2;
const int kSignalByteSize = 1;
bool encrypted_stream = false;
if (options.output_encrypted_info) {
if (track->GetContentEncodingCount() > 0) {
// Only check the first content encoding.
const ContentEncoding* const encoding =
track->GetContentEncodingByIndex(0);
if (encoding) {
if (encoding->GetEncryptionCount() > 0) {
const ContentEncoding::ContentEncryption* const encryption =
encoding->GetEncryptionByIndex(0);
if (encryption) {
const ContentEncoding::ContentEncAESSettings& aes =
encryption->aes_settings;
if (aes.cipher_mode == 1) {
encrypted_stream = true;
}
}
}
}
}
if (encrypted_stream) {
const mkvparser::Block::Frame& frame = block->GetFrame(0);
if (frame.len > static_cast<int>(vector_data.size())) {
vector_data.resize(frame.len + 1024);
}
unsigned char* data = &vector_data[0];
if (frame.Read(reader, data) < 0) {
fprintf(stderr, "Could not read frame.\n");
return false;
}
const bool encrypted_frame = !!(data[0] & KEncryptedBit);
const bool sub_sample_encrypt = !!(data[0] & kSubSampleBit);
fprintf(o, " enc: %d", encrypted_frame ? 1 : 0);
fprintf(o, " sub: %d", sub_sample_encrypt ? 1 : 0);
if (encrypted_frame) {
uint64_t iv;
memcpy(&iv, data + kSignalByteSize, sizeof(iv));
fprintf(o, " iv: %" PRIx64, iv);
}
}
}
if (options.output_codec_info) {
const int frame_count = block->GetFrameCount();
if (frame_count > 1) {
fprintf(o, "\n");
indent->Adjust(libwebm::kIncreaseIndent);
}
for (int i = 0; i < frame_count; ++i) {
if (track_type == mkvparser::Track::kVideo) {
const mkvparser::Block::Frame& frame = block->GetFrame(i);
if (frame.len > static_cast<int>(vector_data.size())) {
vector_data.resize(frame.len + 1024);
}
unsigned char* data = &vector_data[0];
if (frame.Read(reader, data) < 0) {
fprintf(stderr, "Could not read frame.\n");
return false;
}
if (frame_count > 1)
fprintf(o, "\n%sVP8 data :", indent->indent_str().c_str());
bool encrypted_frame = false;
bool sub_sample_encrypt = false;