1 /*
2 	MIT License
3 
4 	Copyright (c) 2016 Błażej Szczygieł
5 
6 	Permission is hereby granted, free of charge, to any person obtaining a copy
7 	of this software and associated documentation files (the "Software"), to deal
8 	in the Software without restriction, including without limitation the rights
9 	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 	copies of the Software, and to permit persons to whom the Software is
11 	furnished to do so, subject to the following conditions:
12 
13 	The above copyright notice and this permission notice shall be included in all
14 	copies or substantial portions of the Software.
15 
16 	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 	SOFTWARE.
23 */
24 
25 #ifndef WEBMDEMUXER_HPP
26 #define WEBMDEMUXER_HPP
27 
28 #include <stddef.h>
29 
30 namespace mkvparser {
31 	class IMkvReader;
32 	class Segment;
33 	class Cluster;
34 	class Block;
35 	class BlockEntry;
36 	class VideoTrack;
37 	class AudioTrack;
38 }
39 
40 class WebMFrame
41 {
42 	WebMFrame(const WebMFrame &);
43 	void operator =(const WebMFrame &);
44 public:
45 	WebMFrame();
46 	~WebMFrame();
47 
isValid() const48 	inline bool isValid() const
49 	{
50 		return bufferSize > 0;
51 	}
52 
53 	long bufferSize, bufferCapacity;
54 	unsigned char *buffer;
55 	double time;
56 	bool key;
57 };
58 
59 class WebMDemuxer
60 {
61 	WebMDemuxer(const WebMDemuxer &);
62 	void operator =(const WebMDemuxer &);
63 public:
64 	enum VIDEO_CODEC
65 	{
66 		NO_VIDEO,
67 		VIDEO_VP8,
68 		VIDEO_VP9
69 	};
70 	enum AUDIO_CODEC
71 	{
72 		NO_AUDIO,
73 		AUDIO_VORBIS,
74 		AUDIO_OPUS
75 	};
76 
77 	WebMDemuxer(mkvparser::IMkvReader *reader, int videoTrack = 0, int audioTrack = 0);
78 	~WebMDemuxer();
79 
isOpen() const80 	inline bool isOpen() const
81 	{
82 		return m_isOpen;
83 	}
isEOS() const84 	inline bool isEOS() const
85 	{
86 		return m_eos;
87 	}
88 
89 	double getLength() const;
90 
91 	VIDEO_CODEC getVideoCodec() const;
92 	int getWidth() const;
93 	int getHeight() const;
94 
95 	AUDIO_CODEC getAudioCodec() const;
96 	const unsigned char *getAudioExtradata(size_t &size) const; // Needed for Vorbis
97 	double getSampleRate() const;
98 	int getChannels() const;
99 	int getAudioDepth() const;
100 
101 	bool readFrame(WebMFrame *videoFrame, WebMFrame *audioFrame);
102 
103 private:
104 	inline bool notSupportedTrackNumber(long videoTrackNumber, long audioTrackNumber) const;
105 
106 	mkvparser::IMkvReader *m_reader;
107 	mkvparser::Segment *m_segment;
108 
109 	const mkvparser::Cluster *m_cluster;
110 	const mkvparser::Block *m_block;
111 	const mkvparser::BlockEntry *m_blockEntry;
112 
113 	int m_blockFrameIndex;
114 
115 	const mkvparser::VideoTrack *m_videoTrack;
116 	VIDEO_CODEC m_vCodec;
117 
118 	const mkvparser::AudioTrack *m_audioTrack;
119 	AUDIO_CODEC m_aCodec;
120 
121 	bool m_isOpen;
122 	bool m_eos;
123 };
124 
125 #endif // WEBMDEMUXER_HPP
126