blob: 1ce659016c8fb9beb45dc6c28ba6973ed1a037e7 [file] [log] [blame]
Tom Finegan596f5e02016-03-31 19:46:21 -07001// Copyright (c) 2012 The WebM project authors. All Rights Reserved.
2//
3// Use of this source code is governed by a BSD-style license
4// that can be found in the LICENSE file in the root of the source
5// tree. An additional intellectual property rights grant can be found
6// in the file PATENTS. All contributing project authors may
7// be found in the AUTHORS file in the root of the source tree.
8#include <inttypes.h>
9#include <stdint.h>
10
11#include <cstdlib>
12#include <cstring>
13#include <limits>
14#include <memory>
15#include <queue>
16#include <string>
17#include <vector>
18
Tom Finegan5c50e312016-04-01 10:29:14 -070019#include "common/hdr_util.h"
Tom Finegan596f5e02016-03-31 19:46:21 -070020#include "common/indent.h"
Frank Galligane3c95762016-04-26 09:51:59 -070021#include "common/vp9_header_parser.h"
22#include "common/vp9_level_stats.h"
Tom Finegan596f5e02016-03-31 19:46:21 -070023#include "common/webm_constants.h"
24#include "common/webm_endian.h"
25
26#include "mkvparser/mkvparser.h"
27#include "mkvparser/mkvreader.h"
28
29namespace {
30
31using mkvparser::ContentEncoding;
32using std::string;
33using std::wstring;
34using libwebm::Indent;
35using libwebm::kNanosecondsPerSecond;
36using libwebm::kNanosecondsPerSecondi;
37
Frank Galligan82903f32016-05-17 09:00:41 -070038const char VERSION_STRING[] = "1.0.4.5";
Tom Finegan596f5e02016-03-31 19:46:21 -070039
40struct Options {
41 Options();
42
43 // Returns true if |value| matches -|option| or -no|option|.
44 static bool MatchesBooleanOption(const string& option, const string& value);
45
46 // Set all of the member variables to |value|.
47 void SetAll(bool value);
48
49 bool output_video;
50 bool output_audio;
51 bool output_size;
52 bool output_offset;
53 bool output_seconds;
54 bool output_ebml_header;
55 bool output_segment;
56 bool output_seekhead;
57 bool output_segment_info;
58 bool output_tracks;
59 bool output_clusters;
60 bool output_blocks;
61 bool output_codec_info;
62 bool output_clusters_size;
63 bool output_encrypted_info;
64 bool output_cues;
65 bool output_frame_stats;
Frank Galligane3c95762016-04-26 09:51:59 -070066 bool output_vp9_level;
Tom Finegan596f5e02016-03-31 19:46:21 -070067};
68
69Options::Options()
70 : output_video(true),
71 output_audio(true),
72 output_size(false),
73 output_offset(false),
74 output_seconds(true),
75 output_ebml_header(true),
76 output_segment(true),
77 output_seekhead(false),
78 output_segment_info(true),
79 output_tracks(true),
80 output_clusters(false),
81 output_blocks(false),
82 output_codec_info(false),
83 output_clusters_size(false),
84 output_encrypted_info(false),
85 output_cues(false),
Frank Galligane3c95762016-04-26 09:51:59 -070086 output_frame_stats(false),
87 output_vp9_level(false) {}
Tom Finegan596f5e02016-03-31 19:46:21 -070088
89void Options::SetAll(bool value) {
90 output_video = value;
91 output_audio = value;
92 output_size = value;
93 output_offset = value;
94 output_ebml_header = value;
95 output_seconds = value;
96 output_segment = value;
97 output_segment_info = value;
98 output_tracks = value;
99 output_clusters = value;
100 output_blocks = value;
101 output_codec_info = value;
102 output_clusters_size = value;
103 output_encrypted_info = value;
104 output_cues = value;
105 output_frame_stats = value;
Frank Galligane3c95762016-04-26 09:51:59 -0700106 output_vp9_level = value;
Tom Finegan596f5e02016-03-31 19:46:21 -0700107}
108
109bool Options::MatchesBooleanOption(const string& option, const string& value) {
110 const string opt = "-" + option;
111 const string noopt = "-no" + option;
112 return value == opt || value == noopt;
113}
114
115struct FrameStats {
116 FrameStats()
117 : frames(0),
118 displayed_frames(0),
119 first_altref(true),
120 frames_since_last_altref(0),
121 minimum_altref_distance(std::numeric_limits<int>::max()),
122 min_altref_end_ns(0),
123 max_window_size(0),
124 max_window_end_ns(0) {}
125
126 int frames;
127 int displayed_frames;
128
129 bool first_altref;
130 int frames_since_last_altref;
131 int minimum_altref_distance;
132 int64_t min_altref_end_ns;
133
134 std::queue<int64_t> window;
135 int64_t max_window_size;
136 int64_t max_window_end_ns;
137};
138
139void Usage() {
140 printf("Usage: webm_info [options] -i input\n");
141 printf("\n");
142 printf("Main options:\n");
143 printf(" -h | -? show help\n");
144 printf(" -v show version\n");
145 printf(" -all Enable all output options.\n");
146 printf(" -video Output video tracks (true)\n");
147 printf(" -audio Output audio tracks (true)\n");
148 printf(" -size Output element sizes (false)\n");
149 printf(" -offset Output element offsets (false)\n");
150 printf(" -times_seconds Output times as seconds (true)\n");
151 printf(" -ebml_header Output EBML header (true)\n");
152 printf(" -segment Output Segment (true)\n");
153 printf(" -seekhead Output SeekHead (false)\n");
154 printf(" -segment_info Output SegmentInfo (true)\n");
155 printf(" -tracks Output Tracks (true)\n");
156 printf(" -clusters Output Clusters (false)\n");
157 printf(" -blocks Output Blocks (false)\n");
158 printf(" -codec_info Output video codec information (false)\n");
159 printf(" -clusters_size Output Total Clusters size (false)\n");
160 printf(" -encrypted_info Output encrypted frame info (false)\n");
161 printf(" -cues Output Cues entries (false)\n");
162 printf(" -frame_stats Output frame stats (VP9)(false)\n");
Frank Galligane3c95762016-04-26 09:51:59 -0700163 printf(" -vp9_level Output VP9 level(false)\n");
Tom Finegan596f5e02016-03-31 19:46:21 -0700164 printf("\nOutput options may be negated by prefixing 'no'.\n");
165}
166
167// TODO(fgalligan): Add support for non-ascii.
168wstring UTF8ToWideString(const char* str) {
169 wstring wstr;
170
171 if (str == NULL)
172 return wstr;
173
174 string temp_str(str, strlen(str));
175 wstr.assign(temp_str.begin(), temp_str.end());
176
177 return wstr;
178}
179
180void OutputEBMLHeader(const mkvparser::EBMLHeader& ebml, FILE* o,
181 Indent* indent) {
182 fprintf(o, "EBML Header:\n");
183 indent->Adjust(libwebm::kIncreaseIndent);
184 fprintf(o, "%sEBMLVersion : %lld\n", indent->indent_str().c_str(),
185 ebml.m_version);
186 fprintf(o, "%sEBMLReadVersion : %lld\n", indent->indent_str().c_str(),
187 ebml.m_readVersion);
188 fprintf(o, "%sEBMLMaxIDLength : %lld\n", indent->indent_str().c_str(),
189 ebml.m_maxIdLength);
190 fprintf(o, "%sEBMLMaxSizeLength : %lld\n", indent->indent_str().c_str(),
191 ebml.m_maxSizeLength);
192 fprintf(o, "%sDoc Type : %s\n", indent->indent_str().c_str(),
193 ebml.m_docType);
194 fprintf(o, "%sDocTypeVersion : %lld\n", indent->indent_str().c_str(),
195 ebml.m_docTypeVersion);
196 fprintf(o, "%sDocTypeReadVersion: %lld\n", indent->indent_str().c_str(),
197 ebml.m_docTypeReadVersion);
198 indent->Adjust(libwebm::kDecreaseIndent);
199}
200
201void OutputSegment(const mkvparser::Segment& segment, const Options& options,
202 FILE* o) {
203 fprintf(o, "Segment:");
204 if (options.output_offset)
205 fprintf(o, " @: %lld", segment.m_element_start);
206 if (options.output_size)
207 fprintf(o, " size: %lld",
208 segment.m_size + segment.m_start - segment.m_element_start);
209 fprintf(o, "\n");
210}
211
212bool OutputSeekHead(const mkvparser::Segment& segment, const Options& options,
213 FILE* o, Indent* indent) {
214 const mkvparser::SeekHead* const seekhead = segment.GetSeekHead();
215 if (!seekhead) {
216 // SeekHeads are optional.
217 return true;
218 }
219
220 fprintf(o, "%sSeekHead:", indent->indent_str().c_str());
221 if (options.output_offset)
222 fprintf(o, " @: %lld", seekhead->m_element_start);
223 if (options.output_size)
224 fprintf(o, " size: %lld", seekhead->m_element_size);
225 fprintf(o, "\n");
226
227 indent->Adjust(libwebm::kIncreaseIndent);
228
229 for (int i = 0; i < seekhead->GetCount(); ++i) {
230 const mkvparser::SeekHead::Entry* const entry = seekhead->GetEntry(i);
231 if (!entry) {
232 fprintf(stderr, "Error retrieving SeekHead entry #%d\n", i);
233 return false;
234 }
235
236 fprintf(o, "%sEntry[%d]", indent->indent_str().c_str(), i);
237 if (options.output_offset)
238 fprintf(o, " @: %lld", entry->element_start);
239 if (options.output_size)
240 fprintf(o, " size: %lld", entry->element_size);
241 fprintf(o, "\n");
242
243 indent->Adjust(libwebm::kIncreaseIndent);
244 const char* const entry_indent = indent->indent_str().c_str();
245 // TODO(jzern): 1) known ids could be stringified. 2) ids could be
246 // reencoded to EBML for ease of lookup.
247 fprintf(o, "%sSeek ID : %llx\n", entry_indent, entry->id);
248 fprintf(o, "%sSeek position : %lld\n", entry_indent, entry->pos);
249 indent->Adjust(libwebm::kDecreaseIndent);
250 }
251
252 for (int i = 0; i < seekhead->GetVoidElementCount(); ++i) {
253 const mkvparser::SeekHead::VoidElement* const entry =
254 seekhead->GetVoidElement(i);
255 if (!entry) {
256 fprintf(stderr, "Error retrieving SeekHead void element #%d\n", i);
257 return false;
258 }
259
260 fprintf(o, "%sVoid element[%d]", indent->indent_str().c_str(), i);
261 if (options.output_offset)
262 fprintf(o, " @: %lld", entry->element_start);
263 if (options.output_size)
264 fprintf(o, " size: %lld", entry->element_size);
265 fprintf(o, "\n");
266 }
267
268 indent->Adjust(libwebm::kDecreaseIndent);
269 return true;
270}
271
272bool OutputSegmentInfo(const mkvparser::Segment& segment,
273 const Options& options, FILE* o, Indent* indent) {
274 const mkvparser::SegmentInfo* const segment_info = segment.GetInfo();
275 if (!segment_info) {
276 fprintf(stderr, "SegmentInfo was NULL.\n");
277 return false;
278 }
279
280 const int64_t timecode_scale = segment_info->GetTimeCodeScale();
281 const int64_t duration_ns = segment_info->GetDuration();
282 const wstring title = UTF8ToWideString(segment_info->GetTitleAsUTF8());
283 const wstring muxing_app =
284 UTF8ToWideString(segment_info->GetMuxingAppAsUTF8());
285 const wstring writing_app =
286 UTF8ToWideString(segment_info->GetWritingAppAsUTF8());
287
288 fprintf(o, "%sSegmentInfo:", indent->indent_str().c_str());
289 if (options.output_offset)
290 fprintf(o, " @: %lld", segment_info->m_element_start);
291 if (options.output_size)
292 fprintf(o, " size: %lld", segment_info->m_element_size);
293 fprintf(o, "\n");
294
295 indent->Adjust(libwebm::kIncreaseIndent);
296 fprintf(o, "%sTimecodeScale : %" PRId64 " \n", indent->indent_str().c_str(),
297 timecode_scale);
298 if (options.output_seconds)
299 fprintf(o, "%sDuration(secs): %g\n", indent->indent_str().c_str(),
300 duration_ns / kNanosecondsPerSecond);
301 else
302 fprintf(o, "%sDuration(nano): %" PRId64 "\n", indent->indent_str().c_str(),
303 duration_ns);
304
305 if (!title.empty())
306 fprintf(o, "%sTitle : %ls\n", indent->indent_str().c_str(),
307 title.c_str());
308 if (!muxing_app.empty())
309 fprintf(o, "%sMuxingApp : %ls\n", indent->indent_str().c_str(),
310 muxing_app.c_str());
311 if (!writing_app.empty())
312 fprintf(o, "%sWritingApp : %ls\n", indent->indent_str().c_str(),
313 writing_app.c_str());
314 indent->Adjust(libwebm::kDecreaseIndent);
315 return true;
316}
317
318bool OutputTracks(const mkvparser::Segment& segment, const Options& options,
319 FILE* o, Indent* indent) {
320 const mkvparser::Tracks* const tracks = segment.GetTracks();
321 if (!tracks) {
322 fprintf(stderr, "Tracks was NULL.\n");
323 return false;
324 }
325
326 fprintf(o, "%sTracks:", indent->indent_str().c_str());
327 if (options.output_offset)
328 fprintf(o, " @: %lld", tracks->m_element_start);
329 if (options.output_size)
330 fprintf(o, " size: %lld", tracks->m_element_size);
331 fprintf(o, "\n");
332
333 unsigned int i = 0;
Tom Finegan030518e2016-04-07 17:49:46 -0700334 const unsigned long j = tracks->GetTracksCount();
Tom Finegan596f5e02016-03-31 19:46:21 -0700335 while (i != j) {
336 const mkvparser::Track* const track = tracks->GetTrackByIndex(i++);
337 if (track == NULL)
338 continue;
339
340 indent->Adjust(libwebm::kIncreaseIndent);
341 fprintf(o, "%sTrack:", indent->indent_str().c_str());
342 if (options.output_offset)
343 fprintf(o, " @: %lld", track->m_element_start);
344 if (options.output_size)
345 fprintf(o, " size: %lld", track->m_element_size);
346 fprintf(o, "\n");
347
348 const int64_t track_type = track->GetType();
349 const int64_t track_number = track->GetNumber();
350 const wstring track_name = UTF8ToWideString(track->GetNameAsUTF8());
351
352 indent->Adjust(libwebm::kIncreaseIndent);
353 fprintf(o, "%sTrackType : %" PRId64 "\n", indent->indent_str().c_str(),
354 track_type);
355 fprintf(o, "%sTrackNumber : %" PRId64 "\n", indent->indent_str().c_str(),
356 track_number);
357 if (!track_name.empty())
358 fprintf(o, "%sName : %ls\n", indent->indent_str().c_str(),
359 track_name.c_str());
360
361 const char* const codec_id = track->GetCodecId();
362 if (codec_id)
363 fprintf(o, "%sCodecID : %s\n", indent->indent_str().c_str(),
364 codec_id);
365
366 const wstring codec_name = UTF8ToWideString(track->GetCodecNameAsUTF8());
367 if (!codec_name.empty())
368 fprintf(o, "%sCodecName : %ls\n", indent->indent_str().c_str(),
369 codec_name.c_str());
370
371 size_t private_size;
372 const unsigned char* const private_data =
373 track->GetCodecPrivate(private_size);
Tom Finegan5c50e312016-04-01 10:29:14 -0700374 if (private_data) {
Tom Finegan596f5e02016-03-31 19:46:21 -0700375 fprintf(o, "%sPrivateData(size): %d\n", indent->indent_str().c_str(),
376 static_cast<int>(private_size));
377
Tom Finegan5c50e312016-04-01 10:29:14 -0700378 if (track_type == mkvparser::Track::kVideo) {
379 const std::string codec_id = track->GetCodecId();
380 const std::string v_vp9 = "V_VP9";
381 if (codec_id == v_vp9) {
Frank Galligan47f28432016-04-21 10:29:00 -0700382 libwebm::Vp9CodecFeatures features;
Frank Galligan296429a2016-04-12 23:50:03 -0700383 if (!libwebm::ParseVpxCodecPrivate(private_data,
384 static_cast<int32_t>(private_size),
Frank Galligan47f28432016-04-21 10:29:00 -0700385 &features)) {
Frank Galligan296429a2016-04-12 23:50:03 -0700386 fprintf(stderr, "Error parsing VpxCodecPrivate.\n");
387 return false;
388 }
Frank Galligan47f28432016-04-21 10:29:00 -0700389 if (features.profile != -1)
390 fprintf(o, "%sVP9 profile : %d\n",
391 indent->indent_str().c_str(), features.profile);
392 if (features.level != -1)
393 fprintf(o, "%sVP9 level : %d\n",
394 indent->indent_str().c_str(), features.level);
395 if (features.bit_depth != -1)
396 fprintf(o, "%sVP9 bit_depth : %d\n",
397 indent->indent_str().c_str(), features.bit_depth);
398 if (features.chroma_subsampling != -1)
399 fprintf(o, "%sVP9 chroma subsampling : %d\n",
400 indent->indent_str().c_str(), features.chroma_subsampling);
Tom Finegan5c50e312016-04-01 10:29:14 -0700401 }
402 }
403 }
404
Tom Finegan596f5e02016-03-31 19:46:21 -0700405 const uint64_t default_duration = track->GetDefaultDuration();
406 if (default_duration > 0)
407 fprintf(o, "%sDefaultDuration: %" PRIu64 "\n",
408 indent->indent_str().c_str(), default_duration);
409
410 if (track->GetContentEncodingCount() > 0) {
411 // Only check the first content encoding.
412 const ContentEncoding* const encoding =
413 track->GetContentEncodingByIndex(0);
414 if (!encoding) {
415 printf("Could not get first ContentEncoding.\n");
416 return false;
417 }
418
419 fprintf(o, "%sContentEncodingOrder : %lld\n",
420 indent->indent_str().c_str(), encoding->encoding_order());
421 fprintf(o, "%sContentEncodingScope : %lld\n",
422 indent->indent_str().c_str(), encoding->encoding_scope());
423 fprintf(o, "%sContentEncodingType : %lld\n",
424 indent->indent_str().c_str(), encoding->encoding_type());
425
426 if (encoding->GetEncryptionCount() > 0) {
427 // Only check the first encryption.
428 const ContentEncoding::ContentEncryption* const encryption =
429 encoding->GetEncryptionByIndex(0);
430 if (!encryption) {
431 printf("Could not get first ContentEncryption.\n");
432 return false;
433 }
434
435 fprintf(o, "%sContentEncAlgo : %lld\n",
436 indent->indent_str().c_str(), encryption->algo);
437
438 if (encryption->key_id_len > 0) {
439 fprintf(o, "%sContentEncKeyID : ", indent->indent_str().c_str());
440 for (int k = 0; k < encryption->key_id_len; ++k) {
441 fprintf(o, "0x%02x, ", encryption->key_id[k]);
442 }
443 fprintf(o, "\n");
444 }
445
446 if (encryption->signature_len > 0) {
447 fprintf(o, "%sContentSignature : 0x",
448 indent->indent_str().c_str());
449 for (int k = 0; k < encryption->signature_len; ++k) {
450 fprintf(o, "%x", encryption->signature[k]);
451 }
452 fprintf(o, "\n");
453 }
454
455 if (encryption->sig_key_id_len > 0) {
456 fprintf(o, "%sContentSigKeyID : 0x",
457 indent->indent_str().c_str());
458 for (int k = 0; k < encryption->sig_key_id_len; ++k) {
459 fprintf(o, "%x", encryption->sig_key_id[k]);
460 }
461 fprintf(o, "\n");
462 }
463
464 fprintf(o, "%sContentSigAlgo : %lld\n",
465 indent->indent_str().c_str(), encryption->sig_algo);
466 fprintf(o, "%sContentSigHashAlgo : %lld\n",
467 indent->indent_str().c_str(), encryption->sig_hash_algo);
468
469 const ContentEncoding::ContentEncAESSettings& aes =
470 encryption->aes_settings;
471 fprintf(o, "%sCipherMode : %lld\n",
472 indent->indent_str().c_str(), aes.cipher_mode);
473 }
474 }
475
476 if (track_type == mkvparser::Track::kVideo) {
477 const mkvparser::VideoTrack* const video_track =
478 static_cast<const mkvparser::VideoTrack* const>(track);
479 const int64_t width = video_track->GetWidth();
480 const int64_t height = video_track->GetHeight();
481 const int64_t display_width = video_track->GetDisplayWidth();
482 const int64_t display_height = video_track->GetDisplayHeight();
483 const int64_t display_unit = video_track->GetDisplayUnit();
484 const double frame_rate = video_track->GetFrameRate();
485 fprintf(o, "%sPixelWidth : %" PRId64 "\n", indent->indent_str().c_str(),
486 width);
487 fprintf(o, "%sPixelHeight : %" PRId64 "\n", indent->indent_str().c_str(),
488 height);
489 if (frame_rate > 0.0)
490 fprintf(o, "%sFrameRate : %g\n", indent->indent_str().c_str(),
491 video_track->GetFrameRate());
492 if (display_unit > 0 || display_width != width ||
493 display_height != height) {
494 fprintf(o, "%sDisplayWidth : %" PRId64 "\n",
495 indent->indent_str().c_str(), display_width);
496 fprintf(o, "%sDisplayHeight : %" PRId64 "\n",
497 indent->indent_str().c_str(), display_height);
498 fprintf(o, "%sDisplayUnit : %" PRId64 "\n",
499 indent->indent_str().c_str(), display_unit);
500 }
Frank Galligan4e3d0372016-04-12 16:37:22 -0700501
502 const mkvparser::Colour* const colour = video_track->GetColour();
503 if (colour) {
504 // TODO(fgalligan): Add support for Colour's address and size.
505 fprintf(o, "%sColour:\n", indent->indent_str().c_str());
506 indent->Adjust(libwebm::kIncreaseIndent);
507
508 const int64_t matrix_coefficients = colour->matrix_coefficients;
509 const int64_t bits_per_channel = colour->bits_per_channel;
510 const int64_t chroma_subsampling_horz = colour->chroma_subsampling_horz;
511 const int64_t chroma_subsampling_vert = colour->chroma_subsampling_vert;
512 const int64_t cb_subsampling_horz = colour->cb_subsampling_horz;
513 const int64_t cb_subsampling_vert = colour->cb_subsampling_vert;
514 const int64_t chroma_siting_horz = colour->chroma_siting_horz;
515 const int64_t chroma_siting_vert = colour->chroma_siting_vert;
516 const int64_t range = colour->range;
517 const int64_t transfer_characteristics =
518 colour->transfer_characteristics;
519 const int64_t primaries = colour->primaries;
520 const int64_t max_cll = colour->max_cll;
521 const int64_t max_fall = colour->max_fall;
522 if (matrix_coefficients != mkvparser::Colour::kValueNotPresent)
523 fprintf(o, "%sMatrixCoefficients : %" PRId64 "\n",
524 indent->indent_str().c_str(), matrix_coefficients);
525 if (bits_per_channel != mkvparser::Colour::kValueNotPresent)
526 fprintf(o, "%sBitsPerChannel : %" PRId64 "\n",
527 indent->indent_str().c_str(), bits_per_channel);
528 if (chroma_subsampling_horz != mkvparser::Colour::kValueNotPresent)
529 fprintf(o, "%sChromaSubsamplingHorz : %" PRId64 "\n",
530 indent->indent_str().c_str(), chroma_subsampling_horz);
531 if (chroma_subsampling_vert != mkvparser::Colour::kValueNotPresent)
532 fprintf(o, "%sChromaSubsamplingVert : %" PRId64 "\n",
533 indent->indent_str().c_str(), chroma_subsampling_vert);
534 if (cb_subsampling_horz != mkvparser::Colour::kValueNotPresent)
535 fprintf(o, "%sCbSubsamplingHorz : %" PRId64 "\n",
536 indent->indent_str().c_str(), cb_subsampling_horz);
537 if (cb_subsampling_vert != mkvparser::Colour::kValueNotPresent)
538 fprintf(o, "%sCbSubsamplingVert : %" PRId64 "\n",
539 indent->indent_str().c_str(), cb_subsampling_vert);
540 if (chroma_siting_horz != mkvparser::Colour::kValueNotPresent)
541 fprintf(o, "%sChromaSitingHorz : %" PRId64 "\n",
542 indent->indent_str().c_str(), chroma_siting_horz);
543 if (chroma_siting_vert != mkvparser::Colour::kValueNotPresent)
544 fprintf(o, "%sChromaSitingVert : %" PRId64 "\n",
545 indent->indent_str().c_str(), chroma_siting_vert);
546 if (range != mkvparser::Colour::kValueNotPresent)
547 fprintf(o, "%sRange : %" PRId64 "\n",
548 indent->indent_str().c_str(), range);
549 if (transfer_characteristics != mkvparser::Colour::kValueNotPresent)
550 fprintf(o, "%sTransferCharacteristics : %" PRId64 "\n",
551 indent->indent_str().c_str(), transfer_characteristics);
552 if (primaries != mkvparser::Colour::kValueNotPresent)
553 fprintf(o, "%sPrimaries : %" PRId64 "\n",
554 indent->indent_str().c_str(), primaries);
555 if (max_cll != mkvparser::Colour::kValueNotPresent)
556 fprintf(o, "%sMaxCLL : %" PRId64 "\n",
557 indent->indent_str().c_str(), max_cll);
558 if (max_fall != mkvparser::Colour::kValueNotPresent)
559 fprintf(o, "%sMaxFALL : %" PRId64 "\n",
560 indent->indent_str().c_str(), max_fall);
561
562 const mkvparser::MasteringMetadata* const metadata =
563 colour->mastering_metadata;
564 if (metadata) {
565 // TODO(fgalligan): Add support for MasteringMetadata's address and
566 // size.
567 fprintf(o, "%sMasteringMetadata:\n", indent->indent_str().c_str());
568 indent->Adjust(libwebm::kIncreaseIndent);
569
570 const mkvparser::PrimaryChromaticity* const red = metadata->r;
571 const mkvparser::PrimaryChromaticity* const green = metadata->g;
572 const mkvparser::PrimaryChromaticity* const blue = metadata->b;
573 const mkvparser::PrimaryChromaticity* const white =
574 metadata->white_point;
575 const float max = metadata->luminance_max;
576 const float min = metadata->luminance_min;
577 if (red) {
578 fprintf(o, "%sPrimaryRChromaticityX : %g\n",
579 indent->indent_str().c_str(), red->x);
580 fprintf(o, "%sPrimaryRChromaticityY : %g\n",
581 indent->indent_str().c_str(), red->y);
582 }
583 if (green) {
584 fprintf(o, "%sPrimaryGChromaticityX : %g\n",
585 indent->indent_str().c_str(), green->x);
586 fprintf(o, "%sPrimaryGChromaticityY : %g\n",
587 indent->indent_str().c_str(), green->y);
588 }
589 if (blue) {
590 fprintf(o, "%sPrimaryBChromaticityX : %g\n",
591 indent->indent_str().c_str(), blue->x);
592 fprintf(o, "%sPrimaryBChromaticityY : %g\n",
593 indent->indent_str().c_str(), blue->y);
594 }
595 if (white) {
596 fprintf(o, "%sWhitePointChromaticityX : %g\n",
597 indent->indent_str().c_str(), white->x);
598 fprintf(o, "%sWhitePointChromaticityY : %g\n",
599 indent->indent_str().c_str(), white->y);
600 }
601 if (max != mkvparser::MasteringMetadata::kValueNotPresent)
602 fprintf(o, "%sLuminanceMax : %g\n",
603 indent->indent_str().c_str(), max);
604 if (min != mkvparser::MasteringMetadata::kValueNotPresent)
605 fprintf(o, "%sLuminanceMin : %g\n",
606 indent->indent_str().c_str(), min);
607 indent->Adjust(libwebm::kDecreaseIndent);
608 }
609 indent->Adjust(libwebm::kDecreaseIndent);
610 }
Tom Finegan54d6b6b2016-08-26 21:29:48 -0700611
612 const mkvparser::Projection* const projection =
613 video_track->GetProjection();
614 if (projection) {
615 fprintf(o, "%sProjection:\n", indent->indent_str().c_str());
616 indent->Adjust(libwebm::kIncreaseIndent);
617
618 const int projection_type = static_cast<int>(projection->type);
619 const int kTypeNotPresent =
620 static_cast<int>(mkvparser::Projection::kTypeNotPresent);
621 const float kValueNotPresent = mkvparser::Projection::kValueNotPresent;
622 if (projection_type != kTypeNotPresent)
623 fprintf(o, "%sProjectionType : %d\n",
624 indent->indent_str().c_str(), projection_type);
625 if (projection->private_data)
626 fprintf(o, "%sProjectionPrivate(size) : %d\n",
627 indent->indent_str().c_str(),
628 static_cast<int>(projection->private_data_length));
629 if (projection->pose_yaw != kValueNotPresent)
630 fprintf(o, "%sProjectionPoseYaw : %g\n",
631 indent->indent_str().c_str(), projection->pose_yaw);
632 if (projection->pose_pitch != kValueNotPresent)
633 fprintf(o, "%sProjectionPosePitch : %g\n",
634 indent->indent_str().c_str(), projection->pose_pitch);
635 if (projection->pose_roll != kValueNotPresent)
636 fprintf(o, "%sProjectionPoseRoll : %g\n",
637 indent->indent_str().c_str(), projection->pose_roll);
638 indent->Adjust(libwebm::kDecreaseIndent);
639 }
Tom Finegan596f5e02016-03-31 19:46:21 -0700640 } else if (track_type == mkvparser::Track::kAudio) {
641 const mkvparser::AudioTrack* const audio_track =
642 static_cast<const mkvparser::AudioTrack* const>(track);
643 const int64_t channels = audio_track->GetChannels();
644 const int64_t bit_depth = audio_track->GetBitDepth();
645 const uint64_t codec_delay = audio_track->GetCodecDelay();
646 const uint64_t seek_preroll = audio_track->GetSeekPreRoll();
647 fprintf(o, "%sChannels : %" PRId64 "\n",
648 indent->indent_str().c_str(), channels);
649 if (bit_depth > 0)
650 fprintf(o, "%sBitDepth : %" PRId64 "\n",
651 indent->indent_str().c_str(), bit_depth);
652 fprintf(o, "%sSamplingFrequency: %g\n", indent->indent_str().c_str(),
653 audio_track->GetSamplingRate());
654 if (codec_delay)
655 fprintf(o, "%sCodecDelay : %" PRIu64 "\n",
656 indent->indent_str().c_str(), codec_delay);
657 if (seek_preroll)
658 fprintf(o, "%sSeekPreRoll : %" PRIu64 "\n",
659 indent->indent_str().c_str(), seek_preroll);
660 }
661 indent->Adjust(libwebm::kDecreaseIndent * 2);
662 }
663
664 return true;
665}
666
667// libvpx reference: vp9/vp9_dx_iface.c
668void ParseSuperframeIndex(const uint8_t* data, size_t data_sz,
669 uint32_t sizes[8], int* count) {
670 const uint8_t marker = data[data_sz - 1];
671 *count = 0;
672
673 if ((marker & 0xe0) == 0xc0) {
674 const int frames = (marker & 0x7) + 1;
675 const int mag = ((marker >> 3) & 0x3) + 1;
676 const size_t index_sz = 2 + mag * frames;
677
678 if (data_sz >= index_sz && data[data_sz - index_sz] == marker) {
679 // found a valid superframe index
680 const uint8_t* x = data + data_sz - index_sz + 1;
681
682 for (int i = 0; i < frames; ++i) {
683 uint32_t this_sz = 0;
684
685 for (int j = 0; j < mag; ++j) {
686 this_sz |= (*x++) << (j * 8);
687 }
688 sizes[i] = this_sz;
689 }
690 *count = frames;
691 }
692 }
693}
694
695void PrintVP9Info(const uint8_t* data, int size, FILE* o, int64_t time_ns,
Frank Galligane3c95762016-04-26 09:51:59 -0700696 FrameStats* stats, vp9_parser::Vp9HeaderParser* parser,
697 vp9_parser::Vp9LevelStats* level_stats) {
Tom Finegan596f5e02016-03-31 19:46:21 -0700698 if (size < 1)
699 return;
700
701 uint32_t sizes[8];
702 int i = 0, count = 0;
703 ParseSuperframeIndex(data, size, sizes, &count);
704
705 // Remove all frames that are less than window size.
706 while (!stats->window.empty() &&
707 stats->window.front() < (time_ns - (kNanosecondsPerSecondi - 1)))
708 stats->window.pop();
709
710 do {
Frank Galligane3c95762016-04-26 09:51:59 -0700711 const size_t frame_length = (count > 0) ? sizes[i] : size;
James Zern85f7e2e2017-04-21 11:59:44 -0700712 if (frame_length > std::numeric_limits<int>::max() ||
713 static_cast<int>(frame_length) > size) {
714 fprintf(o, " invalid VP9 frame size (%u)\n",
715 static_cast<uint32_t>(frame_length));
716 return;
717 }
Frank Galligane3c95762016-04-26 09:51:59 -0700718 parser->SetFrame(data, frame_length);
James Zern5261a672017-04-20 23:21:05 -0700719 if (!parser->ParseUncompressedHeader())
720 return;
Frank Galligane3c95762016-04-26 09:51:59 -0700721 level_stats->AddFrame(*parser, time_ns);
722
Tom Finegan596f5e02016-03-31 19:46:21 -0700723 // const int frame_marker = (data[0] >> 6) & 0x3;
Frank Galligane3c95762016-04-26 09:51:59 -0700724 const int version = parser->profile();
725 const int key = parser->key();
726 const int altref_frame = parser->altref();
727 const int error_resilient_mode = parser->error_resilient_mode();
Frank Galligan82903f32016-05-17 09:00:41 -0700728 const int column_tiles = parser->column_tiles();
729 const int frame_parallel_mode = parser->frame_parallel_mode();
Tom Finegan596f5e02016-03-31 19:46:21 -0700730
731 if (key &&
732 !(size >= 4 && data[1] == 0x49 && data[2] == 0x83 && data[3] == 0x42)) {
733 fprintf(o, " invalid VP9 signature");
734 return;
735 }
736
737 stats->window.push(time_ns);
738 ++stats->frames;
739
740 if (altref_frame) {
741 const int delta_altref = stats->frames_since_last_altref;
742 if (stats->first_altref) {
743 stats->first_altref = false;
744 } else if (delta_altref < stats->minimum_altref_distance) {
745 stats->minimum_altref_distance = delta_altref;
746 stats->min_altref_end_ns = time_ns;
747 }
748 stats->frames_since_last_altref = 0;
749 } else {
750 ++stats->frames_since_last_altref;
751 ++stats->displayed_frames;
752 }
753
754 if (count > 0) {
755 fprintf(o, " packed [%d]: {", i);
756 }
757
Frank Galligan82903f32016-05-17 09:00:41 -0700758 fprintf(o, " key:%d v:%d altref:%d errm:%d ct:%d fpm:%d", key, version,
759 altref_frame, error_resilient_mode, column_tiles,
760 frame_parallel_mode);
Tom Finegan596f5e02016-03-31 19:46:21 -0700761
762 if (key && size > 4) {
Frank Galligane3c95762016-04-26 09:51:59 -0700763 fprintf(o, " cs:%d", parser->color_space());
Tom Finegan596f5e02016-03-31 19:46:21 -0700764 }
765
766 if (count > 0) {
767 fprintf(o, " size: %u }", sizes[i]);
768 data += sizes[i];
769 size -= sizes[i];
770 }
771 ++i;
772 } while (i < count);
773
774 if (stats->max_window_size < static_cast<int64_t>(stats->window.size())) {
775 stats->max_window_size = stats->window.size();
776 stats->max_window_end_ns = time_ns;
777 }
778}
779
780void PrintVP8Info(const uint8_t* data, int size, FILE* o) {
781 if (size < 3)
782 return;
783
784 const uint32_t bits = data[0] | (data[1] << 8) | (data[2] << 16);
785 const int key = !(bits & 0x1);
786 const int altref_frame = !((bits >> 4) & 0x1);
787 const int version = (bits >> 1) & 0x7;
788 const int partition_length = (bits >> 5) & 0x7FFFF;
789 if (key &&
790 !(size >= 6 && data[3] == 0x9d && data[4] == 0x01 && data[5] == 0x2a)) {
791 fprintf(o, " invalid VP8 signature");
792 return;
793 }
794 fprintf(o, " key:%d v:%d altref:%d partition_length:%d", key, version,
795 altref_frame, partition_length);
796}
797
Frank Galliganc97e3e72016-09-16 09:19:00 -0700798// Prints the partition offsets of the sub-sample encryption. |data| must point
799// to an encrypted frame just after the signal byte. Returns the number of
800// bytes read from the sub-sample partition information.
801int PrintSubSampleEncryption(const uint8_t* data, int size, FILE* o) {
802 int read_end = sizeof(uint64_t);
803
804 // Skip past IV.
805 if (size < read_end)
806 return 0;
807 data += sizeof(uint64_t);
808
809 // Read number of partitions.
810 read_end += sizeof(uint8_t);
811 if (size < read_end)
812 return 0;
813 const int num_partitions = data[0];
814 data += sizeof(uint8_t);
815
816 // Read partitions.
817 for (int i = 0; i < num_partitions; ++i) {
818 read_end += sizeof(uint32_t);
819 if (size < read_end)
820 return 0;
821 uint32_t partition_offset;
822 memcpy(&partition_offset, data, sizeof(partition_offset));
823 partition_offset = libwebm::bigendian_to_host(partition_offset);
824 fprintf(o, " off[%d]:%u", i, partition_offset);
825 data += sizeof(uint32_t);
826 }
827
828 return read_end;
829}
830
Tom Finegan596f5e02016-03-31 19:46:21 -0700831bool OutputCluster(const mkvparser::Cluster& cluster,
832 const mkvparser::Tracks& tracks, const Options& options,
833 FILE* o, mkvparser::MkvReader* reader, Indent* indent,
Frank Galligane3c95762016-04-26 09:51:59 -0700834 int64_t* clusters_size, FrameStats* stats,
835 vp9_parser::Vp9HeaderParser* parser,
836 vp9_parser::Vp9LevelStats* level_stats) {
Tom Finegan596f5e02016-03-31 19:46:21 -0700837 if (clusters_size) {
838 // Load the Cluster.
839 const mkvparser::BlockEntry* block_entry;
Tom Finegan030518e2016-04-07 17:49:46 -0700840 long status = cluster.GetFirst(block_entry);
Tom Finegan596f5e02016-03-31 19:46:21 -0700841 if (status) {
842 fprintf(stderr, "Could not get first Block of Cluster.\n");
843 return false;
844 }
845
846 *clusters_size += cluster.GetElementSize();
847 }
848
849 if (options.output_clusters) {
850 const int64_t time_ns = cluster.GetTime();
851 const int64_t duration_ns = cluster.GetLastTime() - cluster.GetFirstTime();
852
853 fprintf(o, "%sCluster:", indent->indent_str().c_str());
854 if (options.output_offset)
855 fprintf(o, " @: %lld", cluster.m_element_start);
856 if (options.output_size)
857 fprintf(o, " size: %lld", cluster.GetElementSize());
858 fprintf(o, "\n");
859 indent->Adjust(libwebm::kIncreaseIndent);
860 if (options.output_seconds)
861 fprintf(o, "%sTimecode (sec) : %g\n", indent->indent_str().c_str(),
862 time_ns / kNanosecondsPerSecond);
863 else
864 fprintf(o, "%sTimecode (nano): %" PRId64 "\n",
865 indent->indent_str().c_str(), time_ns);
866 if (options.output_seconds)
867 fprintf(o, "%sDuration (sec) : %g\n", indent->indent_str().c_str(),
868 duration_ns / kNanosecondsPerSecond);
869 else
870 fprintf(o, "%sDuration (nano): %" PRId64 "\n",
871 indent->indent_str().c_str(), duration_ns);
872
873 fprintf(o, "%s# Blocks : %ld\n", indent->indent_str().c_str(),
874 cluster.GetEntryCount());
875 }
876
877 if (options.output_blocks) {
878 const mkvparser::BlockEntry* block_entry;
Tom Finegan030518e2016-04-07 17:49:46 -0700879 long status = cluster.GetFirst(block_entry);
Tom Finegan596f5e02016-03-31 19:46:21 -0700880 if (status) {
881 fprintf(stderr, "Could not get first Block of Cluster.\n");
882 return false;
883 }
884
885 std::vector<unsigned char> vector_data;
886 while (block_entry != NULL && !block_entry->EOS()) {
887 const mkvparser::Block* const block = block_entry->GetBlock();
888 if (!block) {
889 fprintf(stderr, "Could not getblock entry.\n");
890 return false;
891 }
892
893 const unsigned int track_number =
894 static_cast<unsigned int>(block->GetTrackNumber());
895 const mkvparser::Track* track = tracks.GetTrackByNumber(track_number);
896 if (!track) {
897 fprintf(stderr, "Could not get Track.\n");
898 return false;
899 }
900
901 const int64_t track_type = track->GetType();
902 if ((track_type == mkvparser::Track::kVideo && options.output_video) ||
903 (track_type == mkvparser::Track::kAudio && options.output_audio)) {
904 const int64_t time_ns = block->GetTime(&cluster);
905 const bool is_key = block->IsKey();
906
907 if (block_entry->GetKind() == mkvparser::BlockEntry::kBlockGroup) {
908 fprintf(o, "%sBlockGroup:\n", indent->indent_str().c_str());
909 indent->Adjust(libwebm::kIncreaseIndent);
910 }
911
912 fprintf(o, "%sBlock: type:%s frame:%s", indent->indent_str().c_str(),
913 track_type == mkvparser::Track::kVideo ? "V" : "A",
914 is_key ? "I" : "P");
915 if (options.output_seconds)
916 fprintf(o, " secs:%5g", time_ns / kNanosecondsPerSecond);
917 else
918 fprintf(o, " nano:%10" PRId64, time_ns);
919
920 if (options.output_offset)
921 fprintf(o, " @_payload: %lld", block->m_start);
922 if (options.output_size)
923 fprintf(o, " size_payload: %lld", block->m_size);
924
925 const uint8_t KEncryptedBit = 0x1;
Frank Galliganc97e3e72016-09-16 09:19:00 -0700926 const uint8_t kSubSampleBit = 0x2;
Tom Finegan596f5e02016-03-31 19:46:21 -0700927 const int kSignalByteSize = 1;
928 bool encrypted_stream = false;
929 if (options.output_encrypted_info) {
930 if (track->GetContentEncodingCount() > 0) {
931 // Only check the first content encoding.
932 const ContentEncoding* const encoding =
933 track->GetContentEncodingByIndex(0);
934 if (encoding) {
935 if (encoding->GetEncryptionCount() > 0) {
936 const ContentEncoding::ContentEncryption* const encryption =
937 encoding->GetEncryptionByIndex(0);
938 if (encryption) {
939 const ContentEncoding::ContentEncAESSettings& aes =
940 encryption->aes_settings;
941 if (aes.cipher_mode == 1) {
942 encrypted_stream = true;
943 }
944 }
945 }
946 }
947 }
948
949 if (encrypted_stream) {
950 const mkvparser::Block::Frame& frame = block->GetFrame(0);
951 if (frame.len > static_cast<int>(vector_data.size())) {
952 vector_data.resize(frame.len + 1024);
953 }
954
955 unsigned char* data = &vector_data[0];
956 if (frame.Read(reader, data) < 0) {
957 fprintf(stderr, "Could not read frame.\n");
958 return false;
959 }
960
Frank Galliganc97e3e72016-09-16 09:19:00 -0700961 const bool encrypted_frame = !!(data[0] & KEncryptedBit);
962 const bool sub_sample_encrypt = !!(data[0] & kSubSampleBit);
Tom Finegan596f5e02016-03-31 19:46:21 -0700963 fprintf(o, " enc: %d", encrypted_frame ? 1 : 0);
Frank Galliganc97e3e72016-09-16 09:19:00 -0700964 fprintf(o, " sub: %d", sub_sample_encrypt ? 1 : 0);
Tom Finegan596f5e02016-03-31 19:46:21 -0700965
966 if (encrypted_frame) {
967 uint64_t iv;
968 memcpy(&iv, data + kSignalByteSize, sizeof(iv));
969 fprintf(o, " iv: %" PRIx64, iv);
970 }
971 }
972 }
973
974 if (options.output_codec_info) {
975 const int frame_count = block->GetFrameCount();
976
977 if (frame_count > 1) {
978 fprintf(o, "\n");
979 indent->Adjust(libwebm::kIncreaseIndent);
980 }
981
982 for (int i = 0; i < frame_count; ++i) {
983 if (track_type == mkvparser::Track::kVideo) {
984 const mkvparser::Block::Frame& frame = block->GetFrame(i);
985 if (frame.len > static_cast<int>(vector_data.size())) {
986 vector_data.resize(frame.len + 1024);
987 }
988
989 unsigned char* data = &vector_data[0];
990 if (frame.Read(reader, data) < 0) {
991 fprintf(stderr, "Could not read frame.\n");
992 return false;
993 }
994
995 if (frame_count > 1)
996 fprintf(o, "\n%sVP8 data :", indent->indent_str().c_str());
997
998 bool encrypted_frame = false;
Frank Galliganc97e3e72016-09-16 09:19:00 -0700999 bool sub_sample_encrypt = false;
1000 int frame_size = static_cast<int>(frame.len);
1001
Tom Finegan596f5e02016-03-31 19:46:21 -07001002 int frame_offset = 0;
1003 if (encrypted_stream) {
1004 if (data[0] & KEncryptedBit) {
1005 encrypted_frame = true;
Frank Galliganc97e3e72016-09-16 09:19:00 -07001006 if (data[0] & kSubSampleBit) {
1007 sub_sample_encrypt = true;
1008 data += kSignalByteSize;
1009 frame_size -= kSignalByteSize;
1010 frame_offset =
1011 PrintSubSampleEncryption(data, frame_size, o);
1012 }
Tom Finegan596f5e02016-03-31 19:46:21 -07001013 } else {
1014 frame_offset = kSignalByteSize;
1015 }
1016 }
1017
Frank Galliganc97e3e72016-09-16 09:19:00 -07001018 if (!encrypted_frame || sub_sample_encrypt) {
Tom Finegan596f5e02016-03-31 19:46:21 -07001019 data += frame_offset;
Frank Galliganc97e3e72016-09-16 09:19:00 -07001020 frame_size -= frame_offset;
Tom Finegan596f5e02016-03-31 19:46:21 -07001021
1022 const string codec_id = track->GetCodecId();
1023 if (codec_id == "V_VP8") {
Frank Galliganc97e3e72016-09-16 09:19:00 -07001024 PrintVP8Info(data, frame_size, o);
Tom Finegan596f5e02016-03-31 19:46:21 -07001025 } else if (codec_id == "V_VP9") {
Frank Galliganc97e3e72016-09-16 09:19:00 -07001026 PrintVP9Info(data, frame_size, o, time_ns, stats, parser,
1027 level_stats);
Tom Finegan596f5e02016-03-31 19:46:21 -07001028 }
1029 }
1030 }
1031 }
1032
1033 if (frame_count > 1)
1034 indent->Adjust(libwebm::kDecreaseIndent);
1035 }
1036
1037 if (block_entry->GetKind() == mkvparser::BlockEntry::kBlockGroup) {
1038 const int64_t discard_padding = block->GetDiscardPadding();
1039 if (discard_padding != 0) {
1040 fprintf(o, "\n%sDiscardPadding: %10" PRId64,
1041 indent->indent_str().c_str(), discard_padding);
1042 }
1043 indent->Adjust(libwebm::kDecreaseIndent);
1044 }
1045
1046 fprintf(o, "\n");
1047 }
1048
1049 status = cluster.GetNext(block_entry, block_entry);
1050 if (status) {
1051 printf("\n Could not get next block of cluster.\n");
1052 return false;
1053 }
1054 }
1055 }
1056
1057 if (options.output_clusters)
1058 indent->Adjust(libwebm::kDecreaseIndent);
1059
1060 return true;
1061}
1062
1063bool OutputCues(const mkvparser::Segment& segment,
1064 const mkvparser::Tracks& tracks, const Options& options,
1065 FILE* o, Indent* indent) {
1066 const mkvparser::Cues* const cues = segment.GetCues();
1067 if (cues == NULL)
1068 return true;
1069
1070 // Load all of the cue points.
1071 while (!cues->DoneParsing())
1072 cues->LoadCuePoint();
1073
1074 // Confirm that the input has cue points.
1075 const mkvparser::CuePoint* const first_cue = cues->GetFirst();
1076 if (first_cue == NULL) {
1077 fprintf(o, "%sNo cue points.\n", indent->indent_str().c_str());
1078 return true;
1079 }
1080
1081 // Input has cue points, dump them:
1082 fprintf(o, "%sCues:", indent->indent_str().c_str());
1083 if (options.output_offset)
1084 fprintf(o, " @:%lld", cues->m_element_start);
1085 if (options.output_size)
1086 fprintf(o, " size:%lld", cues->m_element_size);
1087 fprintf(o, "\n");
1088
1089 const mkvparser::CuePoint* cue_point = first_cue;
1090 int cue_point_num = 1;
Tom Finegan030518e2016-04-07 17:49:46 -07001091 const int num_tracks = static_cast<int>(tracks.GetTracksCount());
Tom Finegan596f5e02016-03-31 19:46:21 -07001092 indent->Adjust(libwebm::kIncreaseIndent);
1093
1094 do {
James Zern50c44bb2017-04-19 23:10:33 -07001095 for (int track_num = 0; track_num < num_tracks; ++track_num) {
1096 const mkvparser::Track* const track = tracks.GetTrackByIndex(track_num);
Tom Finegan596f5e02016-03-31 19:46:21 -07001097 const mkvparser::CuePoint::TrackPosition* const track_pos =
1098 cue_point->Find(track);
1099
1100 if (track_pos != NULL) {
1101 const char track_type =
1102 (track->GetType() == mkvparser::Track::kVideo) ? 'V' : 'A';
1103 fprintf(o, "%sCue Point:%d type:%c track:%d",
1104 indent->indent_str().c_str(), cue_point_num, track_type,
James Zern50c44bb2017-04-19 23:10:33 -07001105 static_cast<int>(track->GetNumber()));
Tom Finegan596f5e02016-03-31 19:46:21 -07001106
1107 if (options.output_seconds) {
1108 fprintf(o, " secs:%g",
1109 cue_point->GetTime(&segment) / kNanosecondsPerSecond);
1110 } else {
1111 fprintf(o, " nano:%lld", cue_point->GetTime(&segment));
1112 }
1113
1114 if (options.output_blocks)
1115 fprintf(o, " block:%lld", track_pos->m_block);
1116
1117 if (options.output_offset)
1118 fprintf(o, " @:%lld", track_pos->m_pos);
1119
1120 fprintf(o, "\n");
1121 }
1122 }
1123
1124 cue_point = cues->GetNext(cue_point);
1125 ++cue_point_num;
1126 } while (cue_point != NULL);
1127
1128 indent->Adjust(libwebm::kDecreaseIndent);
1129 return true;
1130}
1131
1132} // namespace
1133
1134int main(int argc, char* argv[]) {
1135 string input;
1136 Options options;
1137
1138 const int argc_check = argc - 1;
1139 for (int i = 1; i < argc; ++i) {
1140 if (!strcmp("-h", argv[i]) || !strcmp("-?", argv[i])) {
1141 Usage();
1142 return EXIT_SUCCESS;
1143 } else if (!strcmp("-v", argv[i])) {
1144 printf("version: %s\n", VERSION_STRING);
1145 } else if (!strcmp("-i", argv[i]) && i < argc_check) {
1146 input = argv[++i];
1147 } else if (!strcmp("-all", argv[i])) {
1148 options.SetAll(true);
1149 } else if (Options::MatchesBooleanOption("video", argv[i])) {
1150 options.output_video = !strcmp("-video", argv[i]);
1151 } else if (Options::MatchesBooleanOption("audio", argv[i])) {
1152 options.output_audio = !strcmp("-audio", argv[i]);
1153 } else if (Options::MatchesBooleanOption("size", argv[i])) {
1154 options.output_size = !strcmp("-size", argv[i]);
1155 } else if (Options::MatchesBooleanOption("offset", argv[i])) {
1156 options.output_offset = !strcmp("-offset", argv[i]);
1157 } else if (Options::MatchesBooleanOption("times_seconds", argv[i])) {
1158 options.output_seconds = !strcmp("-times_seconds", argv[i]);
1159 } else if (Options::MatchesBooleanOption("ebml_header", argv[i])) {
1160 options.output_ebml_header = !strcmp("-ebml_header", argv[i]);
1161 } else if (Options::MatchesBooleanOption("segment", argv[i])) {
1162 options.output_segment = !strcmp("-segment", argv[i]);
1163 } else if (Options::MatchesBooleanOption("seekhead", argv[i])) {
1164 options.output_seekhead = !strcmp("-seekhead", argv[i]);
1165 } else if (Options::MatchesBooleanOption("segment_info", argv[i])) {
1166 options.output_segment_info = !strcmp("-segment_info", argv[i]);
1167 } else if (Options::MatchesBooleanOption("tracks", argv[i])) {
1168 options.output_tracks = !strcmp("-tracks", argv[i]);
1169 } else if (Options::MatchesBooleanOption("clusters", argv[i])) {
1170 options.output_clusters = !strcmp("-clusters", argv[i]);
1171 } else if (Options::MatchesBooleanOption("blocks", argv[i])) {
1172 options.output_blocks = !strcmp("-blocks", argv[i]);
1173 } else if (Options::MatchesBooleanOption("codec_info", argv[i])) {
1174 options.output_codec_info = !strcmp("-codec_info", argv[i]);
1175 } else if (Options::MatchesBooleanOption("clusters_size", argv[i])) {
1176 options.output_clusters_size = !strcmp("-clusters_size", argv[i]);
1177 } else if (Options::MatchesBooleanOption("encrypted_info", argv[i])) {
1178 options.output_encrypted_info = !strcmp("-encrypted_info", argv[i]);
1179 } else if (Options::MatchesBooleanOption("cues", argv[i])) {
1180 options.output_cues = !strcmp("-cues", argv[i]);
1181 } else if (Options::MatchesBooleanOption("frame_stats", argv[i])) {
1182 options.output_frame_stats = !strcmp("-frame_stats", argv[i]);
Frank Galligane3c95762016-04-26 09:51:59 -07001183 } else if (Options::MatchesBooleanOption("vp9_level", argv[i])) {
1184 options.output_vp9_level = !strcmp("-vp9_level", argv[i]);
Tom Finegan596f5e02016-03-31 19:46:21 -07001185 }
1186 }
1187
1188 if (argc < 3 || input.empty()) {
1189 Usage();
1190 return EXIT_FAILURE;
1191 }
1192
1193 // TODO(fgalligan): Replace auto_ptr with scoped_ptr.
1194 std::auto_ptr<mkvparser::MkvReader> reader(
1195 new (std::nothrow) mkvparser::MkvReader()); // NOLINT
1196 if (reader->Open(input.c_str())) {
1197 fprintf(stderr, "Error opening file:%s\n", input.c_str());
1198 return EXIT_FAILURE;
1199 }
1200
1201 long long int pos = 0;
1202 std::auto_ptr<mkvparser::EBMLHeader> ebml_header(
1203 new (std::nothrow) mkvparser::EBMLHeader()); // NOLINT
1204 if (ebml_header->Parse(reader.get(), pos) < 0) {
1205 fprintf(stderr, "Error parsing EBML header.\n");
1206 return EXIT_FAILURE;
1207 }
1208
1209 Indent indent(0);
1210 FILE* out = stdout;
1211
1212 if (options.output_ebml_header)
1213 OutputEBMLHeader(*ebml_header.get(), out, &indent);
1214
1215 mkvparser::Segment* temp_segment;
1216 if (mkvparser::Segment::CreateInstance(reader.get(), pos, temp_segment)) {
1217 fprintf(stderr, "Segment::CreateInstance() failed.\n");
1218 return EXIT_FAILURE;
1219 }
1220 std::auto_ptr<mkvparser::Segment> segment(temp_segment);
1221
1222 if (segment->Load() < 0) {
1223 fprintf(stderr, "Segment::Load() failed.\n");
1224 return EXIT_FAILURE;
1225 }
1226
1227 if (options.output_segment) {
1228 OutputSegment(*(segment.get()), options, out);
1229 indent.Adjust(libwebm::kIncreaseIndent);
1230 }
1231
1232 if (options.output_seekhead)
1233 if (!OutputSeekHead(*(segment.get()), options, out, &indent))
1234 return EXIT_FAILURE;
1235
1236 if (options.output_segment_info)
1237 if (!OutputSegmentInfo(*(segment.get()), options, out, &indent))
1238 return EXIT_FAILURE;
1239
1240 if (options.output_tracks)
1241 if (!OutputTracks(*(segment.get()), options, out, &indent))
1242 return EXIT_FAILURE;
1243
1244 const mkvparser::Tracks* const tracks = segment->GetTracks();
1245 if (!tracks) {
1246 fprintf(stderr, "Could not get Tracks.\n");
1247 return EXIT_FAILURE;
1248 }
1249
1250 // If Cues are before the clusters output them first.
1251 if (options.output_cues) {
1252 const mkvparser::Cluster* cluster = segment->GetFirst();
1253 const mkvparser::Cues* const cues = segment->GetCues();
1254 if (cluster != NULL && cues != NULL) {
1255 if (cues->m_element_start < cluster->m_element_start) {
1256 if (!OutputCues(*segment, *tracks, options, out, &indent)) {
1257 return EXIT_FAILURE;
1258 }
1259 options.output_cues = false;
1260 }
1261 }
1262 }
1263
1264 if (options.output_clusters)
1265 fprintf(out, "%sClusters (count):%ld\n", indent.indent_str().c_str(),
1266 segment->GetCount());
1267
1268 int64_t clusters_size = 0;
1269 FrameStats stats;
Frank Galligane3c95762016-04-26 09:51:59 -07001270 vp9_parser::Vp9HeaderParser parser;
1271 vp9_parser::Vp9LevelStats level_stats;
Tom Finegan596f5e02016-03-31 19:46:21 -07001272 const mkvparser::Cluster* cluster = segment->GetFirst();
1273 while (cluster != NULL && !cluster->EOS()) {
1274 if (!OutputCluster(*cluster, *tracks, options, out, reader.get(), &indent,
Frank Galligane3c95762016-04-26 09:51:59 -07001275 &clusters_size, &stats, &parser, &level_stats))
Tom Finegan596f5e02016-03-31 19:46:21 -07001276 return EXIT_FAILURE;
1277 cluster = segment->GetNext(cluster);
1278 }
1279
1280 if (options.output_clusters_size)
1281 fprintf(out, "%sClusters (size):%" PRId64 "\n", indent.indent_str().c_str(),
1282 clusters_size);
1283
1284 if (options.output_cues)
1285 if (!OutputCues(*segment, *tracks, options, out, &indent))
1286 return EXIT_FAILURE;
1287
1288 // TODO(fgalligan): Add support for VP8.
1289 if (options.output_frame_stats &&
1290 stats.minimum_altref_distance != std::numeric_limits<int>::max()) {
1291 const double actual_fps =
1292 stats.frames /
1293 (segment->GetInfo()->GetDuration() / kNanosecondsPerSecond);
1294 const double displayed_fps =
1295 stats.displayed_frames /
1296 (segment->GetInfo()->GetDuration() / kNanosecondsPerSecond);
1297 fprintf(out, "\nActual fps:%g Displayed fps:%g\n", actual_fps,
1298 displayed_fps);
1299
1300 fprintf(out, "Minimum Altref Distance:%d at:%g seconds\n",
1301 stats.minimum_altref_distance,
1302 stats.min_altref_end_ns / kNanosecondsPerSecond);
1303
1304 // TODO(fgalligan): Add support for window duration other than 1 second.
1305 const double sec_end = stats.max_window_end_ns / kNanosecondsPerSecond;
1306 const double sec_start =
1307 stats.max_window_end_ns > kNanosecondsPerSecondi ? sec_end - 1.0 : 0.0;
1308 fprintf(out, "Maximum Window:%g-%g seconds Window fps:%" PRId64 "\n",
1309 sec_start, sec_end, stats.max_window_size);
1310 }
Frank Galligane3c95762016-04-26 09:51:59 -07001311
1312 if (options.output_vp9_level) {
1313 level_stats.set_duration(segment->GetInfo()->GetDuration());
1314 const vp9_parser::Vp9Level level = level_stats.GetLevel();
1315 fprintf(out, "VP9 Level:%d\n", level);
1316 fprintf(out, "mlsr:%" PRId64 " mlps:%" PRId64
1317 " abr:%g mcs:%g cr:%g mct:%d"
1318 " mad:%d mrf:%d\n",
1319 level_stats.GetMaxLumaSampleRate(),
1320 level_stats.GetMaxLumaPictureSize(),
1321 level_stats.GetAverageBitRate(), level_stats.GetMaxCpbSize(),
1322 level_stats.GetCompressionRatio(), level_stats.GetMaxColumnTiles(),
1323 level_stats.GetMinimumAltrefDistance(),
1324 level_stats.GetMaxReferenceFrames());
1325 }
Tom Finegan596f5e02016-03-31 19:46:21 -07001326 return EXIT_SUCCESS;
1327}