1*103e46e4SHarish Mahendrakar // Copyright (c) 2012 The WebM project authors. All Rights Reserved. 2*103e46e4SHarish Mahendrakar // 3*103e46e4SHarish Mahendrakar // Use of this source code is governed by a BSD-style license 4*103e46e4SHarish Mahendrakar // that can be found in the LICENSE file in the root of the source 5*103e46e4SHarish Mahendrakar // tree. An additional intellectual property rights grant can be found 6*103e46e4SHarish Mahendrakar // in the file PATENTS. All contributing project authors may 7*103e46e4SHarish Mahendrakar // be found in the AUTHORS file in the root of the source tree. 8*103e46e4SHarish Mahendrakar 9*103e46e4SHarish Mahendrakar #ifndef SAMPLE_MUXER_METADATA_H_ // NOLINT 10*103e46e4SHarish Mahendrakar #define SAMPLE_MUXER_METADATA_H_ 11*103e46e4SHarish Mahendrakar 12*103e46e4SHarish Mahendrakar #include <stdint.h> 13*103e46e4SHarish Mahendrakar 14*103e46e4SHarish Mahendrakar #include <list> 15*103e46e4SHarish Mahendrakar #include <set> 16*103e46e4SHarish Mahendrakar #include <string> 17*103e46e4SHarish Mahendrakar 18*103e46e4SHarish Mahendrakar #include "webvtt/webvttparser.h" 19*103e46e4SHarish Mahendrakar 20*103e46e4SHarish Mahendrakar namespace mkvmuxer { 21*103e46e4SHarish Mahendrakar class Chapter; 22*103e46e4SHarish Mahendrakar class Frame; 23*103e46e4SHarish Mahendrakar class Segment; 24*103e46e4SHarish Mahendrakar class Track; 25*103e46e4SHarish Mahendrakar } // namespace mkvmuxer 26*103e46e4SHarish Mahendrakar 27*103e46e4SHarish Mahendrakar class SampleMuxerMetadata { 28*103e46e4SHarish Mahendrakar public: 29*103e46e4SHarish Mahendrakar enum Kind { kSubtitles, kCaptions, kDescriptions, kMetadata, kChapters }; 30*103e46e4SHarish Mahendrakar 31*103e46e4SHarish Mahendrakar SampleMuxerMetadata(); 32*103e46e4SHarish Mahendrakar 33*103e46e4SHarish Mahendrakar // Bind this metadata object to the muxer instance. Returns false 34*103e46e4SHarish Mahendrakar // if segment equals NULL, or Init has already been called. 35*103e46e4SHarish Mahendrakar bool Init(mkvmuxer::Segment* segment); 36*103e46e4SHarish Mahendrakar 37*103e46e4SHarish Mahendrakar // Parse the WebVTT file |filename| having the indicated |kind|, and 38*103e46e4SHarish Mahendrakar // create a corresponding track (or chapters element) in the 39*103e46e4SHarish Mahendrakar // segment. Returns false on error. 40*103e46e4SHarish Mahendrakar bool Load(const char* filename, Kind kind); 41*103e46e4SHarish Mahendrakar 42*103e46e4SHarish Mahendrakar bool AddChapters(); 43*103e46e4SHarish Mahendrakar 44*103e46e4SHarish Mahendrakar // Write any WebVTT cues whose time is less or equal to |time_ns| as 45*103e46e4SHarish Mahendrakar // a metadata block in its corresponding track. If |time_ns| is 46*103e46e4SHarish Mahendrakar // negative, write all remaining cues. Returns false on error. 47*103e46e4SHarish Mahendrakar bool Write(int64_t time_ns); 48*103e46e4SHarish Mahendrakar 49*103e46e4SHarish Mahendrakar private: 50*103e46e4SHarish Mahendrakar typedef libwebvtt::Cue cue_t; 51*103e46e4SHarish Mahendrakar 52*103e46e4SHarish Mahendrakar // Used to sort cues as they are loaded. 53*103e46e4SHarish Mahendrakar struct SortableCue { 54*103e46e4SHarish Mahendrakar bool operator>(int64_t time_ns) const { 55*103e46e4SHarish Mahendrakar // Cue start time expressed in milliseconds 56*103e46e4SHarish Mahendrakar const int64_t start_ms = cue.start_time.presentation(); 57*103e46e4SHarish Mahendrakar 58*103e46e4SHarish Mahendrakar // Cue start time expressed in nanoseconds (MKV time) 59*103e46e4SHarish Mahendrakar const int64_t start_ns = start_ms * 1000000; 60*103e46e4SHarish Mahendrakar 61*103e46e4SHarish Mahendrakar return (start_ns > time_ns); 62*103e46e4SHarish Mahendrakar } 63*103e46e4SHarish Mahendrakar 64*103e46e4SHarish Mahendrakar bool operator<(const SortableCue& rhs) const { 65*103e46e4SHarish Mahendrakar if (cue.start_time < rhs.cue.start_time) 66*103e46e4SHarish Mahendrakar return true; 67*103e46e4SHarish Mahendrakar 68*103e46e4SHarish Mahendrakar if (cue.start_time > rhs.cue.start_time) 69*103e46e4SHarish Mahendrakar return false; 70*103e46e4SHarish Mahendrakar 71*103e46e4SHarish Mahendrakar return (track_num < rhs.track_num); 72*103e46e4SHarish Mahendrakar } 73*103e46e4SHarish Mahendrakar 74*103e46e4SHarish Mahendrakar // Write this cue as a metablock to |segment|. Returns false on 75*103e46e4SHarish Mahendrakar // error. 76*103e46e4SHarish Mahendrakar bool Write(mkvmuxer::Segment* segment) const; 77*103e46e4SHarish Mahendrakar 78*103e46e4SHarish Mahendrakar uint64_t track_num; 79*103e46e4SHarish Mahendrakar cue_t cue; 80*103e46e4SHarish Mahendrakar }; 81*103e46e4SHarish Mahendrakar 82*103e46e4SHarish Mahendrakar typedef std::multiset<SortableCue> cues_set_t; 83*103e46e4SHarish Mahendrakar typedef std::list<cue_t> cue_list_t; 84*103e46e4SHarish Mahendrakar 85*103e46e4SHarish Mahendrakar // Parse the WebVTT cues in the named |file|, returning false on 86*103e46e4SHarish Mahendrakar // error. We handle chapters as a special case, because they are 87*103e46e4SHarish Mahendrakar // stored in their own, dedicated level-1 element. 88*103e46e4SHarish Mahendrakar bool LoadChapters(const char* file); 89*103e46e4SHarish Mahendrakar 90*103e46e4SHarish Mahendrakar // Parse the WebVTT chapters in |file| to populate |cues|. Returns 91*103e46e4SHarish Mahendrakar // false on error. 92*103e46e4SHarish Mahendrakar static bool ParseChapters(const char* file, cue_list_t* cues); 93*103e46e4SHarish Mahendrakar 94*103e46e4SHarish Mahendrakar // Adds WebVTT cue |chapter| to the chapters element of the output 95*103e46e4SHarish Mahendrakar // file's segment element. Returns false on error. 96*103e46e4SHarish Mahendrakar bool AddChapter(const cue_t& chapter); 97*103e46e4SHarish Mahendrakar 98*103e46e4SHarish Mahendrakar // Add a metadata track to the segment having the indicated |kind|, 99*103e46e4SHarish Mahendrakar // returning the |track_num| that has been chosen for this track. 100*103e46e4SHarish Mahendrakar // Returns false on error. 101*103e46e4SHarish Mahendrakar bool AddTrack(Kind kind, uint64_t* track_num); 102*103e46e4SHarish Mahendrakar 103*103e46e4SHarish Mahendrakar // Parse the WebVTT |file| having the indicated |kind| and 104*103e46e4SHarish Mahendrakar // |track_num|, adding each parsed cue to cues set. Returns false 105*103e46e4SHarish Mahendrakar // on error. 106*103e46e4SHarish Mahendrakar bool Parse(const char* file, Kind kind, uint64_t track_num); 107*103e46e4SHarish Mahendrakar 108*103e46e4SHarish Mahendrakar // Converts a WebVTT cue to a Matroska metadata block. 109*103e46e4SHarish Mahendrakar static void MakeFrame(const cue_t& cue, std::string* frame); 110*103e46e4SHarish Mahendrakar 111*103e46e4SHarish Mahendrakar // Populate the cue identifier part of the metadata block. 112*103e46e4SHarish Mahendrakar static void WriteCueIdentifier(const std::string& identifier, 113*103e46e4SHarish Mahendrakar std::string* frame); 114*103e46e4SHarish Mahendrakar 115*103e46e4SHarish Mahendrakar // Populate the cue settings part of the metadata block. 116*103e46e4SHarish Mahendrakar static void WriteCueSettings(const cue_t::settings_t& settings, 117*103e46e4SHarish Mahendrakar std::string* frame); 118*103e46e4SHarish Mahendrakar 119*103e46e4SHarish Mahendrakar // Populate the payload part of the metadata block. 120*103e46e4SHarish Mahendrakar static void WriteCuePayload(const cue_t::payload_t& payload, 121*103e46e4SHarish Mahendrakar std::string* frame); 122*103e46e4SHarish Mahendrakar 123*103e46e4SHarish Mahendrakar mkvmuxer::Segment* segment_; 124*103e46e4SHarish Mahendrakar 125*103e46e4SHarish Mahendrakar // Set of cues ordered by time and then by track number. 126*103e46e4SHarish Mahendrakar cues_set_t cues_set_; 127*103e46e4SHarish Mahendrakar 128*103e46e4SHarish Mahendrakar // The cues that will be used to populate the Chapters level-1 129*103e46e4SHarish Mahendrakar // element of the output file. 130*103e46e4SHarish Mahendrakar cue_list_t chapter_cues_; 131*103e46e4SHarish Mahendrakar 132*103e46e4SHarish Mahendrakar // Disable copy ctor and copy assign. 133*103e46e4SHarish Mahendrakar SampleMuxerMetadata(const SampleMuxerMetadata&); 134*103e46e4SHarish Mahendrakar SampleMuxerMetadata& operator=(const SampleMuxerMetadata&); 135*103e46e4SHarish Mahendrakar }; 136*103e46e4SHarish Mahendrakar 137*103e46e4SHarish Mahendrakar #endif // SAMPLE_MUXER_METADATA_H_ // NOLINT 138