blob: c759c6ccb55a47884e9003da35359a7ba2e6ad38 [file] [log] [blame]
Matthew Heaney4a514132012-08-30 15:16:06 -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
9#include <cstdio>
Tom Finegane64bf752016-03-18 09:32:52 -070010#include <cstdlib>
Matthew Heaney4a514132012-08-30 15:16:06 -070011#include <cstring>
12#include <map>
13#include <memory>
14#include <string>
Tom Fineganbaba8b12016-03-09 14:12:21 -080015#include <utility>
16
Tom Finegan504e0f22016-03-21 11:20:48 -070017#include "mkvparser/mkvparser.h"
18#include "mkvparser/mkvreader.h"
Tom Finegan5f1065e2016-03-17 15:09:46 -070019#include "webvtt/webvttparser.h"
Matthew Heaney4a514132012-08-30 15:16:06 -070020
Vignesh Venkatasubramaniane3485c92014-04-14 12:14:06 -070021#ifdef _MSC_VER
22// Disable MSVC warnings that suggest making code non-portable.
23#pragma warning(disable : 4996)
24#endif
25
Matthew Heaney4a514132012-08-30 15:16:06 -070026using std::string;
27
Tom Finegane64bf752016-03-18 09:32:52 -070028namespace libwebm {
Matthew Heaney4a514132012-08-30 15:16:06 -070029namespace vttdemux {
30
31typedef long long mkvtime_t; // NOLINT
Vignesh Venkatasubramanian7b245012014-04-29 00:35:56 -070032typedef long long mkvpos_t; // NOLINT
Tom Finegan1e1872b2016-02-17 11:22:21 -080033typedef std::auto_ptr<mkvparser::Segment> segment_ptr_t;
Matthew Heaney4a514132012-08-30 15:16:06 -070034
35// WebVTT metadata tracks have a type (encoded in the CodecID for the track).
36// We use |type| to synthesize a filename for the out-of-band WebVTT |file|.
37struct MetadataInfo {
Vignesh Venkatasubramaniane3485c92014-04-14 12:14:06 -070038 enum Type { kSubtitles, kCaptions, kDescriptions, kMetadata, kChapters } type;
Matthew Heaney4a514132012-08-30 15:16:06 -070039 FILE* file;
40};
41
42// We use a map, indexed by track number, to collect information about
43// each track in the input file.
44typedef std::map<long, MetadataInfo> metadata_map_t; // NOLINT
45
Matthew Heaneyc26db032012-10-26 15:06:28 -070046// The distinguished key value we use to store the chapters
47// information in the metadata map.
48enum { kChaptersKey = 0 };
49
Matthew Heaney4a514132012-08-30 15:16:06 -070050// The data from the original WebVTT Cue is stored as a WebM block.
51// The FrameParser is used to parse the lines of text out from the
52// block, in order to reconstruct the original WebVTT Cue.
53class FrameParser : public libwebvtt::LineReader {
54 public:
55 // Bind the FrameParser instance to a WebM block.
56 explicit FrameParser(const mkvparser::BlockGroup* block_group);
57 virtual ~FrameParser();
58
59 // The Webm block (group) to which this instance is bound. We
60 // treat the payload of the block as a stream of characters.
61 const mkvparser::BlockGroup* const block_group_;
62
63 protected:
64 // Read the next character from the character stream (the payload
65 // of the WebM block). We increment the stream pointer |pos_| as
66 // each character from the stream is consumed.
67 virtual int GetChar(char* c);
68
69 // End-of-line handling requires that we put a character back into
70 // the stream. Here we need only decrement the stream pointer |pos_|
71 // to unconsume the character.
72 virtual void UngetChar(char c);
73
74 // The current position in the character stream (the payload of the block).
75 mkvpos_t pos_;
76
77 // The position of the end of the character stream. When the current
78 // position |pos_| equals the end position |pos_end_|, the entire
79 // stream (block payload) has been consumed and end-of-stream is indicated.
80 mkvpos_t pos_end_;
81
82 private:
83 // Disable copy ctor and copy assign
84 FrameParser(const FrameParser&);
85 FrameParser& operator=(const FrameParser&);
86};
87
Matthew Heaneyc26db032012-10-26 15:06:28 -070088// The data from the original WebVTT Cue is stored as an MKV Chapters
89// Atom element (the cue payload is stored as a Display sub-element).
90// The ChapterAtomParser is used to parse the lines of text out from
91// the String sub-element of the Display element (though it would be
92// admittedly odd if there were more than one line).
93class ChapterAtomParser : public libwebvtt::LineReader {
94 public:
95 explicit ChapterAtomParser(const mkvparser::Chapters::Display* display);
96 virtual ~ChapterAtomParser();
97
98 const mkvparser::Chapters::Display* const display_;
99
100 protected:
101 // Read the next character from the character stream (the title
102 // member of the atom's display). We increment the stream pointer
103 // |str_| as each character from the stream is consumed.
104 virtual int GetChar(char* c);
105
106 // End-of-line handling requires that we put a character back into
107 // the stream. Here we need only decrement the stream pointer |str_|
108 // to unconsume the character.
109 virtual void UngetChar(char c);
110
111 // The current position in the character stream (the title of the
112 // atom's display).
113 const char* str_;
114
115 // The position of the end of the character stream. When the current
116 // position |str_| equals the end position |str_end_|, the entire
117 // stream (title of the display) has been consumed and end-of-stream
118 // is indicated.
119 const char* str_end_;
120
121 private:
122 ChapterAtomParser(const ChapterAtomParser&);
123 ChapterAtomParser& operator=(const ChapterAtomParser&);
124};
125
Matthew Heaney4a514132012-08-30 15:16:06 -0700126// Parse the EBML header of the WebM input file, to determine whether we
127// actually have a WebM file. Returns false if this is not a WebM file.
128bool ParseHeader(mkvparser::IMkvReader* reader, mkvpos_t* pos);
129
130// Parse the Segment of the input file and load all of its clusters.
131// Returns false if there was an error parsing the file.
Vignesh Venkatasubramaniane3485c92014-04-14 12:14:06 -0700132bool ParseSegment(mkvparser::IMkvReader* reader, mkvpos_t pos,
133 segment_ptr_t* segment);
Matthew Heaney4a514132012-08-30 15:16:06 -0700134
Matthew Heaneyc26db032012-10-26 15:06:28 -0700135// If |segment| has a Chapters element (in which case, there will be a
136// corresponding entry in |metadata_map|), convert the MKV chapters to
137// WebVTT chapter cues and write them to the output file. Returns
138// false on error.
Vignesh Venkatasubramaniane3485c92014-04-14 12:14:06 -0700139bool WriteChaptersFile(const metadata_map_t& metadata_map,
140 const mkvparser::Segment* segment);
Matthew Heaneyc26db032012-10-26 15:06:28 -0700141
142// Convert an MKV Chapters Atom to a WebVTT cue and write it to the
143// output |file|. Returns false on error.
Vignesh Venkatasubramaniane3485c92014-04-14 12:14:06 -0700144bool WriteChaptersCue(FILE* file, const mkvparser::Chapters* chapters,
145 const mkvparser::Chapters::Atom* atom,
146 const mkvparser::Chapters::Display* display);
Matthew Heaneyc26db032012-10-26 15:06:28 -0700147
Matthew Heaney28222b42012-11-13 12:44:06 -0800148// Write the Cue Identifier line of the WebVTT cue, if it's present.
149// Returns false on error.
Vignesh Venkatasubramaniane3485c92014-04-14 12:14:06 -0700150bool WriteChaptersCueIdentifier(FILE* file,
151 const mkvparser::Chapters::Atom* atom);
Matthew Heaney28222b42012-11-13 12:44:06 -0800152
Matthew Heaneyc26db032012-10-26 15:06:28 -0700153// Use the timecodes from the chapters |atom| to write just the
154// timings line of the WebVTT cue. Returns false on error.
Vignesh Venkatasubramaniane3485c92014-04-14 12:14:06 -0700155bool WriteChaptersCueTimings(FILE* file, const mkvparser::Chapters* chapters,
156 const mkvparser::Chapters::Atom* atom);
Matthew Heaneyc26db032012-10-26 15:06:28 -0700157
158// Parse the String sub-element of the |display| and write the payload
159// of the WebVTT cue. Returns false on error.
Vignesh Venkatasubramaniane3485c92014-04-14 12:14:06 -0700160bool WriteChaptersCuePayload(FILE* file,
161 const mkvparser::Chapters::Display* display);
Matthew Heaneyc26db032012-10-26 15:06:28 -0700162
163// Iterate over the tracks of the input file (and any chapters
164// element) and cache information about each metadata track.
Matthew Heaney4a514132012-08-30 15:16:06 -0700165void BuildMap(const mkvparser::Segment* segment, metadata_map_t* metadata_map);
166
167// For each track listed in the cache, synthesize its output filename
168// and open a file handle that designates the out-of-band file.
169// Returns false if we were unable to open an output file for a track.
170bool OpenFiles(metadata_map_t* metadata_map, const char* filename);
171
172// Close the file handle for each track in the cache.
173void CloseFiles(metadata_map_t* metadata_map);
174
175// Iterate over the clusters of the input file, and write a WebVTT cue
176// for each metadata block. Returns false if processing of a cluster
177// failed.
178bool WriteFiles(const metadata_map_t& m, mkvparser::Segment* s);
179
180// Write the WebVTT header for each track in the cache. We do this
181// immediately before writing the actual WebVTT cues. Returns false
182// if the write failed.
183bool InitializeFiles(const metadata_map_t& metadata_map);
184
185// Iterate over the blocks of the |cluster|, writing a WebVTT cue to
186// its associated output file for each block of metadata. Returns
187// false if processing a block failed, or there was a parse error.
Vignesh Venkatasubramaniane3485c92014-04-14 12:14:06 -0700188bool ProcessCluster(const metadata_map_t& metadata_map,
189 const mkvparser::Cluster* cluster);
Matthew Heaney4a514132012-08-30 15:16:06 -0700190
191// Look up this track number in the cache, and if found (meaning this
192// is a metadata track), write a WebVTT cue to the associated output
193// file. Returns false if writing the WebVTT cue failed.
Vignesh Venkatasubramaniane3485c92014-04-14 12:14:06 -0700194bool ProcessBlockEntry(const metadata_map_t& metadata_map,
195 const mkvparser::BlockEntry* block_entry);
Matthew Heaney4a514132012-08-30 15:16:06 -0700196
197// Parse the lines of text from the |block_group| to reconstruct the
198// original WebVTT cue, and write it to the associated output |file|.
199// Returns false if there was an error writing to the output file.
200bool WriteCue(FILE* file, const mkvparser::BlockGroup* block_group);
201
202// Consume a line of text from the character stream, and if the line
203// is not empty write the cue identifier to the associated output
204// file. Returns false if there was an error writing to the file.
205bool WriteCueIdentifier(FILE* f, FrameParser* parser);
206
207// Consume a line of text from the character stream (which holds any
208// cue settings) and write the cue timings line for this cue to the
209// associated output file. Returns false if there was an error
210// writing to the file.
Vignesh Venkatasubramaniane3485c92014-04-14 12:14:06 -0700211bool WriteCueTimings(FILE* f, FrameParser* parser);
Matthew Heaney4a514132012-08-30 15:16:06 -0700212
213// Write the timestamp (representating either the start time or stop
214// time of the cue) to the output file. Returns false if there was an
215// error writing to the file.
Vignesh Venkatasubramaniane3485c92014-04-14 12:14:06 -0700216bool WriteCueTime(FILE* f, mkvtime_t time_ns);
Matthew Heaney4a514132012-08-30 15:16:06 -0700217
218// Consume the remaining lines of text from the character stream
219// (these lines are the actual payload of the WebVTT cue), and write
220// them to the associated output file. Returns false if there was an
221// error writing to the file.
Vignesh Venkatasubramaniane3485c92014-04-14 12:14:06 -0700222bool WriteCuePayload(FILE* f, FrameParser* parser);
Matthew Heaney4a514132012-08-30 15:16:06 -0700223} // namespace vttdemux
224
Matthew Heaney4a514132012-08-30 15:16:06 -0700225namespace vttdemux {
226
227FrameParser::FrameParser(const mkvparser::BlockGroup* block_group)
228 : block_group_(block_group) {
229 const mkvparser::Block* const block = block_group->GetBlock();
230 const mkvparser::Block::Frame& f = block->GetFrame(0);
231
232 // The beginning and end of the character stream corresponds to the
233 // position of this block's frame within the WebM input file.
234
235 pos_ = f.pos;
236 pos_end_ = f.pos + f.len;
237}
238
Vignesh Venkatasubramaniane3485c92014-04-14 12:14:06 -0700239FrameParser::~FrameParser() {}
Matthew Heaney4a514132012-08-30 15:16:06 -0700240
241int FrameParser::GetChar(char* c) {
242 if (pos_ >= pos_end_) // end-of-stream
Vignesh Venkatasubramanian7b245012014-04-29 00:35:56 -0700243 return 1; // per the semantics of libwebvtt::Reader::GetChar
Matthew Heaney4a514132012-08-30 15:16:06 -0700244
245 const mkvparser::Cluster* const cluster = block_group_->GetCluster();
246 const mkvparser::Segment* const segment = cluster->m_pSegment;
247 mkvparser::IMkvReader* const reader = segment->m_pReader;
248
249 unsigned char* const buf = reinterpret_cast<unsigned char*>(c);
250 const int result = reader->Read(pos_, 1, buf);
251
252 if (result < 0) // error
253 return -1;
254
255 ++pos_; // consume this character in the stream
256 return 0;
257}
258
Vignesh Venkatasubramaniane3485c92014-04-14 12:14:06 -0700259void FrameParser::UngetChar(char /* c */) {
Matthew Heaney4a514132012-08-30 15:16:06 -0700260 // All we need to do here is decrement the position in the stream.
261 // The next time GetChar is called the same character will be
262 // re-read from the input file.
263 --pos_;
264}
265
Matthew Heaneyc26db032012-10-26 15:06:28 -0700266ChapterAtomParser::ChapterAtomParser(
267 const mkvparser::Chapters::Display* display)
268 : display_(display) {
269 str_ = display->GetString();
270 const size_t len = strlen(str_);
271 str_end_ = str_ + len;
272}
273
Vignesh Venkatasubramaniane3485c92014-04-14 12:14:06 -0700274ChapterAtomParser::~ChapterAtomParser() {}
Matthew Heaneyc26db032012-10-26 15:06:28 -0700275
276int ChapterAtomParser::GetChar(char* c) {
277 if (str_ >= str_end_) // end-of-stream
Vignesh Venkatasubramanian7b245012014-04-29 00:35:56 -0700278 return 1; // per the semantics of libwebvtt::Reader::GetChar
Matthew Heaneyc26db032012-10-26 15:06:28 -0700279
280 *c = *str_++; // consume this character in the stream
281 return 0;
282}
283
Vignesh Venkatasubramaniane3485c92014-04-14 12:14:06 -0700284void ChapterAtomParser::UngetChar(char /* c */) {
Matthew Heaneyc26db032012-10-26 15:06:28 -0700285 // All we need to do here is decrement the position in the stream.
286 // The next time GetChar is called the same character will be
287 // re-read from the input file.
288 --str_;
289}
290
Matthew Heaney4a514132012-08-30 15:16:06 -0700291} // namespace vttdemux
292
Vignesh Venkatasubramaniane3485c92014-04-14 12:14:06 -0700293bool vttdemux::ParseHeader(mkvparser::IMkvReader* reader, mkvpos_t* pos) {
Matthew Heaney4a514132012-08-30 15:16:06 -0700294 mkvparser::EBMLHeader h;
295 const mkvpos_t status = h.Parse(reader, *pos);
296
297 if (status) {
298 printf("error parsing EBML header\n");
299 return false;
300 }
301
Tom Finegan714f3c42015-09-04 10:18:20 -0700302 if (h.m_docType == NULL || strcmp(h.m_docType, "webm") != 0) {
Matthew Heaney4a514132012-08-30 15:16:06 -0700303 printf("bad doctype\n");
304 return false;
305 }
306
307 return true; // success
308}
309
Vignesh Venkatasubramaniane3485c92014-04-14 12:14:06 -0700310bool vttdemux::ParseSegment(mkvparser::IMkvReader* reader, mkvpos_t pos,
311 segment_ptr_t* segment_ptr) {
Matthew Heaney4a514132012-08-30 15:16:06 -0700312 // We first create the segment object.
313
314 mkvparser::Segment* p;
315 const mkvpos_t create = mkvparser::Segment::CreateInstance(reader, pos, p);
316
317 if (create) {
318 printf("error parsing segment element\n");
319 return false;
320 }
321
322 segment_ptr->reset(p);
323
324 // Now parse all of the segment's sub-elements, in toto.
325
326 const long status = p->Load(); // NOLINT
327
328 if (status < 0) {
329 printf("error loading segment\n");
330 return false;
331 }
332
333 return true;
334}
335
Vignesh Venkatasubramaniane3485c92014-04-14 12:14:06 -0700336void vttdemux::BuildMap(const mkvparser::Segment* segment,
337 metadata_map_t* map_ptr) {
Matthew Heaneyc26db032012-10-26 15:06:28 -0700338 metadata_map_t& m = *map_ptr;
339 m.clear();
340
341 if (segment->GetChapters()) {
342 MetadataInfo info;
343 info.file = NULL;
344 info.type = MetadataInfo::kChapters;
345
346 m[kChaptersKey] = info;
347 }
348
Matthew Heaney4a514132012-08-30 15:16:06 -0700349 const mkvparser::Tracks* const tt = segment->GetTracks();
350 if (tt == NULL)
351 return;
352
353 const long tc = tt->GetTracksCount(); // NOLINT
354 if (tc <= 0)
355 return;
356
Matthew Heaney4a514132012-08-30 15:16:06 -0700357 // Iterate over the tracks in the intput file. We determine whether
358 // a track holds metadata by inspecting its CodecID.
359
360 for (long idx = 0; idx < tc; ++idx) { // NOLINT
361 const mkvparser::Track* const t = tt->GetTrackByIndex(idx);
362
363 if (t == NULL) // weird
364 continue;
365
Matthew Heaneyc26db032012-10-26 15:06:28 -0700366 const long tn = t->GetNumber(); // NOLINT
367
368 if (tn <= 0) // weird
369 continue;
370
Matthew Heaney4a514132012-08-30 15:16:06 -0700371 const char* const codec_id = t->GetCodecId();
372
373 if (codec_id == NULL) // weird
374 continue;
375
376 MetadataInfo info;
377 info.file = NULL;
378
379 if (strcmp(codec_id, "D_WEBVTT/SUBTITLES") == 0) {
380 info.type = MetadataInfo::kSubtitles;
381 } else if (strcmp(codec_id, "D_WEBVTT/CAPTIONS") == 0) {
382 info.type = MetadataInfo::kCaptions;
383 } else if (strcmp(codec_id, "D_WEBVTT/DESCRIPTIONS") == 0) {
384 info.type = MetadataInfo::kDescriptions;
385 } else if (strcmp(codec_id, "D_WEBVTT/METADATA") == 0) {
386 info.type = MetadataInfo::kMetadata;
387 } else {
388 continue;
389 }
390
Matthew Heaney4a514132012-08-30 15:16:06 -0700391 m[tn] = info; // create an entry in the cache for this track
392 }
393}
394
395bool vttdemux::OpenFiles(metadata_map_t* metadata_map, const char* filename) {
396 if (metadata_map == NULL || metadata_map->empty())
397 return false;
398
399 if (filename == NULL)
400 return false;
401
402 // Find the position of the filename extension. We synthesize the
403 // output filename from the directory path and basename of the input
404 // filename.
405
406 const char* const ext = strrchr(filename, '.');
407
408 if (ext == NULL) // TODO(matthewjheaney): liberalize?
409 return false;
410
411 // Remember whether a track of this type has already been seen (the
412 // map key) by keeping a count (the map item). We quality the
413 // output filename with the track number if there is more than one
414 // track having a given type.
415
416 std::map<MetadataInfo::Type, int> exists;
417
418 typedef metadata_map_t::iterator iter_t;
419
420 metadata_map_t& m = *metadata_map;
421 const iter_t ii = m.begin();
422 const iter_t j = m.end();
423
424 // Make a first pass over the cache to determine whether there is
425 // more than one track corresponding to a given metadata type.
426
427 iter_t i = ii;
428 while (i != j) {
429 const metadata_map_t::value_type& v = *i++;
430 const MetadataInfo& info = v.second;
431 const MetadataInfo::Type type = info.type;
432 ++exists[type];
433 }
434
435 // Make a second pass over the cache, synthesizing the filename of
436 // each output file (from the input file basename, the input track
437 // metadata type, and its track number if necessary), and then
438 // opening a WebVTT output file having that filename.
439
440 i = ii;
441 while (i != j) {
442 metadata_map_t::value_type& v = *i++;
443 MetadataInfo& info = v.second;
444 const MetadataInfo::Type type = info.type;
445
446 // Start with the basename of the input file.
447
448 string name(filename, ext);
449
450 // Next append the metadata kind.
451
452 switch (type) {
453 case MetadataInfo::kSubtitles:
454 name += "_SUBTITLES";
455 break;
456
457 case MetadataInfo::kCaptions:
458 name += "_CAPTIONS";
459 break;
460
461 case MetadataInfo::kDescriptions:
462 name += "_DESCRIPTIONS";
463 break;
464
465 case MetadataInfo::kMetadata:
466 name += "_METADATA";
467 break;
468
Matthew Heaneyc26db032012-10-26 15:06:28 -0700469 case MetadataInfo::kChapters:
470 name += "_CHAPTERS";
471 break;
472
Matthew Heaney4a514132012-08-30 15:16:06 -0700473 default:
474 return false;
475 }
476
477 // If there is more than one metadata track having a given type
478 // (the WebVTT-in-WebM spec doesn't preclude this), then qualify
479 // the output filename with the input track number.
480
481 if (exists[type] > 1) {
482 enum { kLen = 33 };
483 char str[kLen]; // max 126 tracks, so only 4 chars really needed
Matthew Heaney17cf7cc2014-02-28 12:33:58 -0800484#ifndef _MSC_VER
Matthew Heaney4a514132012-08-30 15:16:06 -0700485 snprintf(str, kLen, "%ld", v.first); // track number
Matthew Heaney17cf7cc2014-02-28 12:33:58 -0800486#else
487 _snprintf_s(str, sizeof(str), kLen, "%ld", v.first); // track number
488#endif
Matthew Heaney4a514132012-08-30 15:16:06 -0700489 name += str;
490 }
491
492 // Finally append the output filename extension.
493
494 name += ".vtt";
495
496 // We have synthesized the full output filename, so attempt to
497 // open the WebVTT output file.
498
499 info.file = fopen(name.c_str(), "wb");
Matthew Heaney17cf7cc2014-02-28 12:33:58 -0800500 const bool success = (info.file != NULL);
Matthew Heaney4a514132012-08-30 15:16:06 -0700501
Matthew Heaney17cf7cc2014-02-28 12:33:58 -0800502 if (!success) {
Matthew Heaney4a514132012-08-30 15:16:06 -0700503 printf("unable to open output file %s\n", name.c_str());
504 return false;
505 }
506 }
507
508 return true;
509}
510
511void vttdemux::CloseFiles(metadata_map_t* metadata_map) {
512 if (metadata_map == NULL)
513 return;
514
515 metadata_map_t& m = *metadata_map;
516
517 typedef metadata_map_t::iterator iter_t;
518
519 iter_t i = m.begin();
520 const iter_t j = m.end();
521
522 // Gracefully close each output file, to ensure all output gets
523 // propertly flushed.
524
525 while (i != j) {
526 metadata_map_t::value_type& v = *i++;
527 MetadataInfo& info = v.second;
528
529 fclose(info.file);
530 info.file = NULL;
531 }
532}
533
534bool vttdemux::WriteFiles(const metadata_map_t& m, mkvparser::Segment* s) {
535 // First write the WebVTT header.
536
537 InitializeFiles(m);
538
Matthew Heaneyc26db032012-10-26 15:06:28 -0700539 if (!WriteChaptersFile(m, s))
540 return false;
541
Matthew Heaney4a514132012-08-30 15:16:06 -0700542 // Now iterate over the clusters, writing the WebVTT cue as we parse
543 // each metadata block.
544
545 const mkvparser::Cluster* cluster = s->GetFirst();
546
547 while (cluster != NULL && !cluster->EOS()) {
548 if (!ProcessCluster(m, cluster))
549 return false;
550
551 cluster = s->GetNext(cluster);
552 }
553
554 return true;
555}
556
557bool vttdemux::InitializeFiles(const metadata_map_t& m) {
558 // Write the WebVTT header for each output file in the cache.
559
560 typedef metadata_map_t::const_iterator iter_t;
561 iter_t i = m.begin();
562 const iter_t j = m.end();
563
564 while (i != j) {
565 const metadata_map_t::value_type& v = *i++;
566 const MetadataInfo& info = v.second;
567 FILE* const f = info.file;
568
569 if (fputs("WEBVTT\n", f) < 0) {
570 printf("unable to initialize output file\n");
571 return false;
572 }
573 }
574
575 return true;
576}
577
Vignesh Venkatasubramaniane3485c92014-04-14 12:14:06 -0700578bool vttdemux::WriteChaptersFile(const metadata_map_t& m,
579 const mkvparser::Segment* s) {
Matthew Heaneyc26db032012-10-26 15:06:28 -0700580 const metadata_map_t::const_iterator info_iter = m.find(kChaptersKey);
581 if (info_iter == m.end()) // no chapters, so nothing to do
582 return true;
583
584 const mkvparser::Chapters* const chapters = s->GetChapters();
585 if (chapters == NULL) // weird
586 return true;
587
588 const MetadataInfo& info = info_iter->second;
589 FILE* const file = info.file;
590
591 const int edition_count = chapters->GetEditionCount();
592
593 if (edition_count <= 0) // weird
Vignesh Venkatasubramanian7b245012014-04-29 00:35:56 -0700594 return true; // nothing to do
Matthew Heaneyc26db032012-10-26 15:06:28 -0700595
596 if (edition_count > 1) {
597 // TODO(matthewjheaney): figure what to do here
598 printf("more than one chapter edition detected\n");
599 return false;
600 }
601
602 const mkvparser::Chapters::Edition* const edition = chapters->GetEdition(0);
603
604 const int atom_count = edition->GetAtomCount();
605
606 for (int idx = 0; idx < atom_count; ++idx) {
607 const mkvparser::Chapters::Atom* const atom = edition->GetAtom(idx);
608 const int display_count = atom->GetDisplayCount();
609
610 if (display_count <= 0)
611 continue;
612
613 if (display_count > 1) {
614 // TODO(matthewjheaney): handle case of multiple languages
615 printf("more than 1 display in atom detected\n");
616 return false;
617 }
618
619 const mkvparser::Chapters::Display* const display = atom->GetDisplay(0);
620
621 if (const char* language = display->GetLanguage()) {
622 if (strcmp(language, "eng") != 0) {
623 // TODO(matthewjheaney): handle case of multiple languages.
624
625 // We must create a separate webvtt file for each language.
626 // This isn't a simple problem (which is why we defer it for
627 // now), because there's nothing in the header that tells us
628 // what languages we have as cues. We must parse the displays
629 // of each atom to determine that.
630
631 // One solution is to make two passes over the input data.
632 // First parse the displays, creating an in-memory cache of
633 // all the chapter cues, sorted according to their language.
634 // After we have read all of the chapter atoms from the input
635 // file, we can then write separate output files for each
636 // language.
637
638 printf("only English-language chapter cues are supported\n");
639 return false;
640 }
641 }
642
643 if (!WriteChaptersCue(file, chapters, atom, display))
644 return false;
645 }
646
647 return true;
648}
649
Vignesh Venkatasubramaniane3485c92014-04-14 12:14:06 -0700650bool vttdemux::WriteChaptersCue(FILE* f, const mkvparser::Chapters* chapters,
651 const mkvparser::Chapters::Atom* atom,
652 const mkvparser::Chapters::Display* display) {
Matthew Heaneyc26db032012-10-26 15:06:28 -0700653 // We start a new cue by writing a cue separator (an empty line)
654 // into the stream.
655
656 if (fputc('\n', f) < 0)
657 return false;
658
659 // A WebVTT Cue comprises 3 things: a cue identifier, followed by
660 // the cue timings, followed by the payload of the cue. We write
661 // each part of the cue in sequence.
662
Matthew Heaney28222b42012-11-13 12:44:06 -0800663 if (!WriteChaptersCueIdentifier(f, atom))
664 return false;
Matthew Heaneyc26db032012-10-26 15:06:28 -0700665
666 if (!WriteChaptersCueTimings(f, chapters, atom))
667 return false;
668
669 if (!WriteChaptersCuePayload(f, display))
670 return false;
671
672 return true;
673}
674
Matthew Heaney28222b42012-11-13 12:44:06 -0800675bool vttdemux::WriteChaptersCueIdentifier(
Vignesh Venkatasubramaniane3485c92014-04-14 12:14:06 -0700676 FILE* f, const mkvparser::Chapters::Atom* atom) {
Matthew Heaney28222b42012-11-13 12:44:06 -0800677 const char* const identifier = atom->GetStringUID();
678
679 if (identifier == NULL)
680 return true; // nothing else to do
681
682 if (fprintf(f, "%s\n", identifier) < 0)
683 return false;
684
685 return true;
686}
687
Vignesh Venkatasubramaniane3485c92014-04-14 12:14:06 -0700688bool vttdemux::WriteChaptersCueTimings(FILE* f,
689 const mkvparser::Chapters* chapters,
690 const mkvparser::Chapters::Atom* atom) {
Matthew Heaneyc26db032012-10-26 15:06:28 -0700691 const mkvtime_t start_ns = atom->GetStartTime(chapters);
692
693 if (start_ns < 0)
694 return false;
695
696 const mkvtime_t stop_ns = atom->GetStopTime(chapters);
697
698 if (stop_ns < 0)
699 return false;
700
701 if (!WriteCueTime(f, start_ns))
702 return false;
703
704 if (fputs(" --> ", f) < 0)
705 return false;
706
707 if (!WriteCueTime(f, stop_ns))
708 return false;
709
710 if (fputc('\n', f) < 0)
711 return false;
712
713 return true;
714}
715
716bool vttdemux::WriteChaptersCuePayload(
Vignesh Venkatasubramaniane3485c92014-04-14 12:14:06 -0700717 FILE* f, const mkvparser::Chapters::Display* display) {
Matthew Heaneyc26db032012-10-26 15:06:28 -0700718 // Bind a Chapter parser object to the display, which allows us to
719 // extract each line of text from the title-part of the display.
720 ChapterAtomParser parser(display);
721
722 int count = 0; // count of lines of payload text written to output file
723 for (string line;;) {
724 const int e = parser.GetLine(&line);
725
726 if (e < 0) // error (only -- we allow EOS here)
727 return false;
728
729 if (line.empty()) // TODO(matthewjheaney): retain this check?
730 break;
731
732 if (fprintf(f, "%s\n", line.c_str()) < 0)
733 return false;
734
735 ++count;
736 }
737
738 if (count <= 0) // WebVTT cue requires non-empty payload
739 return false;
740
741 return true;
742}
743
Vignesh Venkatasubramaniane3485c92014-04-14 12:14:06 -0700744bool vttdemux::ProcessCluster(const metadata_map_t& m,
745 const mkvparser::Cluster* c) {
Matthew Heaney4a514132012-08-30 15:16:06 -0700746 // Visit the blocks in this cluster, writing a WebVTT cue for each
747 // metadata block.
748
749 const mkvparser::BlockEntry* block_entry;
750
751 long result = c->GetFirst(block_entry); // NOLINT
Vignesh Venkatasubramanian7b245012014-04-29 00:35:56 -0700752 if (result < 0) {
Matthew Heaney4a514132012-08-30 15:16:06 -0700753 printf("bad cluster (unable to get first block)\n");
754 return false;
755 }
756
757 while (block_entry != NULL && !block_entry->EOS()) {
758 if (!ProcessBlockEntry(m, block_entry))
759 return false;
760
761 result = c->GetNext(block_entry, block_entry);
762 if (result < 0) { // error
763 printf("bad cluster (unable to get next block)\n");
764 return false;
765 }
766 }
767
768 return true;
769}
770
Vignesh Venkatasubramaniane3485c92014-04-14 12:14:06 -0700771bool vttdemux::ProcessBlockEntry(const metadata_map_t& m,
772 const mkvparser::BlockEntry* block_entry) {
Matthew Heaney4a514132012-08-30 15:16:06 -0700773 // If the track number for this block is in the cache, then we have
774 // a metadata block, so write the WebVTT cue to the output file.
775
776 const mkvparser::Block* const block = block_entry->GetBlock();
777 const long long tn = block->GetTrackNumber(); // NOLINT
778
779 typedef metadata_map_t::const_iterator iter_t;
Matthew Heaney17cf7cc2014-02-28 12:33:58 -0800780 const iter_t i = m.find(static_cast<metadata_map_t::key_type>(tn));
Matthew Heaney4a514132012-08-30 15:16:06 -0700781
782 if (i == m.end()) // not a metadata track
Vignesh Venkatasubramanian7b245012014-04-29 00:35:56 -0700783 return true; // nothing else to do
Matthew Heaney4a514132012-08-30 15:16:06 -0700784
785 if (block_entry->GetKind() != mkvparser::BlockEntry::kBlockGroup)
786 return false; // weird
787
788 typedef mkvparser::BlockGroup BG;
789 const BG* const block_group = static_cast<const BG*>(block_entry);
790
791 const MetadataInfo& info = i->second;
792 FILE* const f = info.file;
793
794 return WriteCue(f, block_group);
795}
796
Vignesh Venkatasubramaniane3485c92014-04-14 12:14:06 -0700797bool vttdemux::WriteCue(FILE* f, const mkvparser::BlockGroup* block_group) {
Matthew Heaney4a514132012-08-30 15:16:06 -0700798 // Bind a FrameParser object to the block, which allows us to
799 // extract each line of text from the payload of the block.
800 FrameParser parser(block_group);
801
802 // We start a new cue by writing a cue separator (an empty line)
803 // into the stream.
804
805 if (fputc('\n', f) < 0)
806 return false;
807
808 // A WebVTT Cue comprises 3 things: a cue identifier, followed by
809 // the cue timings, followed by the payload of the cue. We write
810 // each part of the cue in sequence.
811
812 if (!WriteCueIdentifier(f, &parser))
813 return false;
814
815 if (!WriteCueTimings(f, &parser))
816 return false;
817
818 if (!WriteCuePayload(f, &parser))
819 return false;
820
821 return true;
822}
823
Vignesh Venkatasubramaniane3485c92014-04-14 12:14:06 -0700824bool vttdemux::WriteCueIdentifier(FILE* f, FrameParser* parser) {
Matthew Heaney4a514132012-08-30 15:16:06 -0700825 string line;
826 int e = parser->GetLine(&line);
827
828 if (e) // error or EOS
829 return false;
830
831 // If the cue identifier line is empty, this means that the original
832 // WebVTT cue did not have a cue identifier, so we don't bother
833 // writing an extra line terminator to the output file (though doing
834 // so would be harmless).
835
836 if (!line.empty()) {
837 if (fputs(line.c_str(), f) < 0)
838 return false;
839
840 if (fputc('\n', f) < 0)
841 return false;
842 }
843
844 return true;
845}
846
Vignesh Venkatasubramaniane3485c92014-04-14 12:14:06 -0700847bool vttdemux::WriteCueTimings(FILE* f, FrameParser* parser) {
Matthew Heaney4a514132012-08-30 15:16:06 -0700848 const mkvparser::BlockGroup* const block_group = parser->block_group_;
849 const mkvparser::Cluster* const cluster = block_group->GetCluster();
850 const mkvparser::Block* const block = block_group->GetBlock();
851
852 // A WebVTT Cue "timings" line comprises two parts: the start and
853 // stop time for this cue, followed by the (optional) cue settings,
854 // such as orientation of the rendered text or its size. Only the
855 // settings part of the cue timings line is stored in the WebM
856 // block. We reconstruct the start and stop times of the WebVTT cue
857 // from the timestamp and duration of the WebM block.
858
859 const mkvtime_t start_ns = block->GetTime(cluster);
860
861 if (!WriteCueTime(f, start_ns))
862 return false;
863
864 if (fputs(" --> ", f) < 0)
865 return false;
866
867 const mkvtime_t duration_timecode = block_group->GetDurationTimeCode();
868
869 if (duration_timecode < 0)
870 return false;
871
872 const mkvparser::Segment* const segment = cluster->m_pSegment;
873 const mkvparser::SegmentInfo* const info = segment->GetInfo();
874
875 if (info == NULL)
876 return false;
877
878 const mkvtime_t timecode_scale = info->GetTimeCodeScale();
879
880 if (timecode_scale <= 0)
881 return false;
882
883 const mkvtime_t duration_ns = duration_timecode * timecode_scale;
884 const mkvtime_t stop_ns = start_ns + duration_ns;
885
886 if (!WriteCueTime(f, stop_ns))
887 return false;
888
889 string line;
890 int e = parser->GetLine(&line);
891
892 if (e) // error or EOS
893 return false;
894
895 if (!line.empty()) {
896 if (fputc(' ', f) < 0)
897 return false;
898
899 if (fputs(line.c_str(), f) < 0)
900 return false;
901 }
902
903 if (fputc('\n', f) < 0)
904 return false;
905
906 return true;
907}
908
Vignesh Venkatasubramaniane3485c92014-04-14 12:14:06 -0700909bool vttdemux::WriteCueTime(FILE* f, mkvtime_t time_ns) {
Matthew Heaney4a514132012-08-30 15:16:06 -0700910 mkvtime_t ms = time_ns / 1000000; // WebVTT time has millisecond resolution
911
912 mkvtime_t sec = ms / 1000;
913 ms -= sec * 1000;
914
915 mkvtime_t min = sec / 60;
916 sec -= 60 * min;
917
918 mkvtime_t hr = min / 60;
919 min -= 60 * hr;
920
921 if (hr > 0) {
922 if (fprintf(f, "%02lld:", hr) < 0)
923 return false;
924 }
925
926 if (fprintf(f, "%02lld:%02lld.%03lld", min, sec, ms) < 0)
Vignesh Venkatasubramaniane3485c92014-04-14 12:14:06 -0700927 return false;
Matthew Heaney4a514132012-08-30 15:16:06 -0700928
929 return true;
930}
931
Vignesh Venkatasubramaniane3485c92014-04-14 12:14:06 -0700932bool vttdemux::WriteCuePayload(FILE* f, FrameParser* parser) {
Matthew Heaney4a514132012-08-30 15:16:06 -0700933 int count = 0; // count of lines of payload text written to output file
934 for (string line;;) {
935 const int e = parser->GetLine(&line);
936
937 if (e < 0) // error (only -- we allow EOS here)
938 return false;
939
940 if (line.empty()) // TODO(matthewjheaney): retain this check?
941 break;
942
943 if (fprintf(f, "%s\n", line.c_str()) < 0)
944 return false;
945
946 ++count;
947 }
948
949 if (count <= 0) // WebVTT cue requires non-empty payload
950 return false;
951
952 return true;
953}
Tom Finegane64bf752016-03-18 09:32:52 -0700954
955} // namespace libwebm
956
957int main(int argc, const char* argv[]) {
958 if (argc != 2) {
959 printf("usage: vttdemux <webmfile>\n");
960 return EXIT_SUCCESS;
961 }
962
963 const char* const filename = argv[1];
Tom Finegancbe5c402016-03-21 12:16:30 -0700964 mkvparser::MkvReader reader;
Tom Finegane64bf752016-03-18 09:32:52 -0700965
966 int e = reader.Open(filename);
967
968 if (e) { // error
969 printf("unable to open file\n");
970 return EXIT_FAILURE;
971 }
972
973 libwebm::vttdemux::mkvpos_t pos;
974
975 if (!libwebm::vttdemux::ParseHeader(&reader, &pos))
976 return EXIT_FAILURE;
977
978 libwebm::vttdemux::segment_ptr_t segment_ptr;
979
980 if (!libwebm::vttdemux::ParseSegment(&reader, pos, &segment_ptr))
981 return EXIT_FAILURE;
982
983 libwebm::vttdemux::metadata_map_t metadata_map;
984
985 BuildMap(segment_ptr.get(), &metadata_map);
986
987 if (metadata_map.empty()) {
988 printf("no WebVTT metadata found\n");
989 return EXIT_FAILURE;
990 }
991
992 if (!OpenFiles(&metadata_map, filename)) {
993 CloseFiles(&metadata_map); // nothing to flush, so not strictly necessary
994 return EXIT_FAILURE;
995 }
996
997 if (!WriteFiles(metadata_map, segment_ptr.get())) {
998 CloseFiles(&metadata_map); // might as well flush what we do have
999 return EXIT_FAILURE;
1000 }
1001
1002 CloseFiles(&metadata_map);
1003
1004 return EXIT_SUCCESS;
Tom Finegan5f1065e2016-03-17 15:09:46 -07001005}