1 /*
2  * $Id$
3  * Copyright (c) 2008-2010, Matroska (non-profit organisation)
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *     * Redistributions of source code must retain the above copyright
9  *       notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above copyright
11  *       notice, this list of conditions and the following disclaimer in the
12  *       documentation and/or other materials provided with the distribution.
13  *     * Neither the name of the Matroska assocation nor the
14  *       names of its contributors may be used to endorse or promote products
15  *       derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY the Matroska association ``AS IS'' AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL The Matroska Foundation BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #ifndef MATROSKA_PARSER_H
30 #define MATROSKA_PARSER_H
31 
32 #include "matroska/matroska.h"
33 
34 typedef int64_t longlong;
35 typedef uint64_t ulonglong;
36 
37 typedef struct InputStream
38 {
39 	int (*read)(struct InputStream *cc, filepos_t pos, void *buffer, size_t count);
40 	filepos_t (*scan)(struct InputStream *cc, filepos_t start, unsigned signature);
41 	size_t (*getcachesize)(struct InputStream *cc);
42 	filepos_t (*getfilesize)(struct InputStream *cc);
43 	const char *(*geterror)(struct InputStream *cc);
44 	int (*progress)(struct InputStream *cc, filepos_t cur, filepos_t max);
45 
46     int (*ioreadch)(struct InputStream *inf);
47     int (*ioread)(struct InputStream *inf,void *buffer,int count);
48     void (*ioseek)(struct InputStream *inf,longlong where,int how);
49     filepos_t (*iotell)(struct InputStream *inf);
50     void* (*makeref)(struct InputStream *inf,int count);
51 	void (*releaseref)(struct InputStream *inf,void* ref);
52 
53 	void *(*memalloc)(struct InputStream *cc,size_t size);
54 	void *(*memrealloc)(struct InputStream *cc,void *mem,size_t newsize);
55 	void (*memfree)(struct InputStream *cc,void *mem);
56 
57 #if defined(NO_MATROSKA2_GLOBAL)
58 	anynode *AnyNode;
59 #endif
60 
61 } InputStream;
62 
63 typedef struct TrackInfo
64 {
65 	int Number;
66 	uint8_t Type;
67 	uint64_t UID;
68 
69 	uint8_t *CodecPrivate;
70 	size_t CodecPrivateSize;
71 	timecode_t DefaultDuration;
72 	char *CodecID;
73 	char *Name;
74 	char Language[4];
75 
76 	bool_t Enabled;
77 	bool_t Default;
78 	bool_t Lacing;
79 	bool_t DecodeAll;
80 	float TimecodeScale;
81 
82 	int TrackOverlay;
83 	uint8_t MinCache;
84 	size_t MaxCache;
85 	size_t MaxBlockAdditionID;
86 
87   union {
88     struct {
89       uint8_t   StereoMode;
90       uint8_t   DisplayUnit;
91       uint8_t   AspectRatioType;
92       uint32_t    PixelWidth;
93       uint32_t    PixelHeight;
94       uint32_t    DisplayWidth;
95       uint32_t    DisplayHeight;
96       uint32_t    CropL, CropT, CropR, CropB;
97       unsigned int    ColourSpace;
98       float           GammaValue;
99       //struct {
100     unsigned int  Interlaced:1;
101       //};
102     } Video;
103     struct {
104       float     SamplingFreq;
105       float     OutputSamplingFreq;
106       uint8_t   Channels;
107       uint8_t   BitDepth;
108     } Audio;
109   } AV;
110 
111 } TrackInfo;
112 
113 typedef struct Attachment
114 {
115 	filepos_t Position;
116 	filepos_t Length;
117 	uint64_t UID;
118 	char* Name;
119 	char* Description;
120 	char* MimeType;
121 
122 } Attachment;
123 
124 typedef struct ChapterDisplay
125 {
126 	char *String;
127 	char Language[4];
128 	char Country[4];
129 
130 } ChapterDisplay;
131 
132 typedef struct Chapter
133 {
134 	size_t nChildren;
135 	struct Chapter *Children;
136 	size_t nDisplay;
137 	struct ChapterDisplay *Display;
138 
139 	timecode_t Start;
140 	timecode_t End;
141 	uint64_t UID;
142 
143 	bool_t Enabled;
144 	bool_t Ordered;
145 	bool_t Default;
146 	bool_t Hidden;
147 
148 	array aChildren; // Chapter
149 	array aDisplays;  // ChapterDisplay
150 
151 } Chapter;
152 
153 typedef struct Target {
154   uint64_t UID;
155   uint8_t  Type;
156   uint8_t  Level;
157 };
158 // Tag Target types
159 #define TARGET_TRACK      0
160 #define TARGET_CHAPTER    1
161 #define TARGET_ATTACHMENT 2
162 #define TARGET_EDITION    3
163 
164 typedef struct SimpleTag
165 {
166 	char *Name;
167 	char *Value;
168     char Language[4];
169     bool_t Default;
170 
171 } SimpleTag;
172 
173 typedef struct Tag
174 {
175 	size_t nSimpleTags;
176 	SimpleTag *SimpleTags;
177 
178 	size_t nTargets;
179 	struct Target *Targets;
180 
181 	array aTargets; // Target
182 	array aSimpleTags; // SimpleTag
183 
184 } Tag;
185 
186 typedef struct SegmentInfo
187 {
188 	uint8_t UID[16];
189 	uint8_t PrevUID[16];
190 	uint8_t NextUID[16];
191 	char *Filename;
192 	char *PrevFilename;
193 	char *NextFilename;
194 	char *Title;
195 	char *MuxingApp;
196 	char *WritingApp;
197 
198 	timecode_t TimecodeScale;
199 	timecode_t Duration;
200 
201 	datetime_t DateUTC;
202 
203 } SegmentInfo;
204 
205 typedef struct MatroskaFile MatroskaFile;
206 
207 #define MKVF_AVOID_SEEKS    1 /* use sequential reading only */
208 
209 MatroskaFile *mkv_Open(InputStream *io, char *err_msg, size_t err_msgSize);
210 void mkv_Close(MatroskaFile *File);
211 
212 SegmentInfo *mkv_GetFileInfo(MatroskaFile *File);
213 size_t mkv_GetNumTracks(MatroskaFile *File);
214 TrackInfo *mkv_GetTrackInfo(MatroskaFile *File, size_t n);
215 void mkv_SetTrackMask(MatroskaFile *File, int Mask);
216 
217 #define FRAME_UNKNOWN_START  0x00000001
218 #define FRAME_UNKNOWN_END    0x00000002
219 #define FRAME_KF             0x00000004
220 
221 int mkv_ReadFrame(MatroskaFile *File, int mask, unsigned int *track, ulonglong *StartTime, ulonglong *EndTime, ulonglong *FilePos, unsigned int *FrameSize,
222                 void** FrameRef, unsigned int *FrameFlags);
223 
224 #define MKVF_SEEK_TO_PREV_KEYFRAME  1
225 
226 void mkv_Seek(MatroskaFile *File, timecode_t timecode, int flags);
227 
228 void mkv_GetTags(MatroskaFile *File, Tag **, unsigned *Count);
229 void mkv_GetAttachments(MatroskaFile *File, Attachment **, unsigned *Count);
230 void mkv_GetChapters(MatroskaFile *File, Chapter **, unsigned *Count);
231 
232 int mkv_TruncFloat(float f);
233 
234 #endif /* MATROSKA_PARSER_H */
235