vfs: check userland buffers before reading them.
[haiku.git] / src / add-ons / media / plugins / mov_reader / libMOV / MOVParser.h
blob82fd9eac08a13e3586495c577299ee234cd4ebef
1 /*
2 * Copyright (c) 2005, David McPaul
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
18 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
21 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
22 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
23 * OF THE POSSIBILITY OF SUCH DAMAGE.
25 #ifndef _MOV_PARSER_H
26 #define _MOV_PARSER_H
29 #include "MOVAtom.h"
31 #include <File.h>
32 #include <MediaDefs.h>
33 #include <MediaFormats.h>
34 #include <SupportDefs.h>
36 #include <map>
39 typedef SoundDescriptionV1* SoundDescPtr;
40 typedef std::map<uint32, SoundDescPtr, std::less<uint32> > SoundDescArray;
41 typedef VideoDescriptionV0* VideoDescPtr;
42 typedef std::map<uint32, VideoDescPtr, std::less<uint32> > VideoDescArray;
43 typedef TimeToSample* TimeToSamplePtr;
44 typedef std::map<uint32, TimeToSamplePtr, std::less<uint32> > TimeToSampleArray;
45 typedef SampleToChunk* SampleToChunkPtr;
46 typedef std::map<uint32, SampleToChunkPtr, std::less<uint32> > SampleToChunkArray;
47 typedef ChunkToOffset* ChunkToOffsetPtr;
48 typedef std::map<uint32, ChunkToOffsetPtr, std::less<uint32> > ChunkToOffsetArray;
49 typedef SyncSample* SyncSamplePtr;
50 typedef std::map<uint32, SyncSamplePtr, std::less<uint32> > SyncSampleArray;
51 typedef SampleSizeEntry* SampleSizeEntryPtr;
52 typedef std::map<uint32, SampleSizeEntryPtr, std::less<uint32> > SampleSizeArray;
54 // Atom class for reading the movie header atom
55 class MVHDAtom : public AtomBase {
56 public:
57 MVHDAtom(BPositionIO *pStream, off_t pstreamOffset, uint32 patomType, uint64 patomSize);
58 virtual ~MVHDAtom();
59 void OnProcessMetaData();
60 char *OnGetAtomName();
61 uint32 getTimeScale() {return theHeader.TimeScale;};
62 uint32 getDuration() {return theHeader.Duration;};
63 private:
64 mvhd theHeader;
68 // Atom class for reading the moov atom
69 class MOOVAtom : public AtomContainer {
70 public:
71 MOOVAtom(BPositionIO *pStream, off_t pstreamOffset, uint32 patomType, uint64 patomSize);
72 virtual ~MOOVAtom();
73 void OnProcessMetaData();
74 char *OnGetAtomName();
76 MVHDAtom *getMVHDAtom();
77 private:
78 MVHDAtom *theMVHDAtom;
81 // Atom class for reading the cmov atom
82 class CMOVAtom : public AtomContainer {
83 public:
84 CMOVAtom(BPositionIO *pStream, off_t pstreamOffset, uint32 patomType, uint64 patomSize);
85 virtual ~CMOVAtom();
87 BPositionIO *OnGetStream();
88 void OnProcessMetaData();
89 void OnChildProcessingComplete();
91 char *OnGetAtomName();
92 private:
93 BMallocIO *theUncompressedStream;
96 // Atom class for reading the dcom atom
97 class DCOMAtom : public AtomBase {
98 public:
99 DCOMAtom(BPositionIO *pStream, off_t pstreamOffset, uint32 patomType, uint64 patomSize);
100 virtual ~DCOMAtom();
101 void OnProcessMetaData();
102 char *OnGetAtomName();
103 uint32 getCompressionID() {return compressionID;};
104 private:
105 uint32 compressionID;
108 // Atom class for reading the cmvd atom
109 class CMVDAtom : public AtomBase {
110 public:
111 CMVDAtom(BPositionIO *pStream, off_t pstreamOffset, uint32 patomType, uint64 patomSize);
112 virtual ~CMVDAtom();
113 void OnProcessMetaData();
114 char *OnGetAtomName();
115 uint32 getUncompressedSize() {return UncompressedSize;};
116 uint8 *getCompressedData() {return Buffer;};
117 uint32 getBufferSize() {return BufferSize;};
118 private:
119 uint32 UncompressedSize;
120 uint32 BufferSize;
121 uint8 *Buffer;
124 // Atom class for reading the wide atom
125 class WIDEAtom : public AtomBase {
126 public:
127 WIDEAtom(BPositionIO *pStream, off_t pstreamOffset, uint32 patomType, uint64 patomSize);
128 virtual ~WIDEAtom();
129 void OnProcessMetaData();
130 char *OnGetAtomName();
134 // Atom class for reading the free atom
135 class FREEAtom : public AtomBase {
136 public:
137 FREEAtom(BPositionIO *pStream, off_t pstreamOffset, uint32 patomType, uint64 patomSize);
138 virtual ~FREEAtom();
139 void OnProcessMetaData();
140 char *OnGetAtomName();
144 // Atom class for reading the preview atom
145 class PNOTAtom : public AtomBase {
146 public:
147 PNOTAtom(BPositionIO *pStream, off_t pstreamOffset, uint32 patomType, uint64 patomSize);
148 virtual ~PNOTAtom();
149 void OnProcessMetaData();
150 char *OnGetAtomName();
154 // Atom class for reading the time to sample atom
155 class STTSAtom : public AtomBase {
156 public:
157 STTSAtom(BPositionIO *pStream, off_t pstreamOffset, uint32 patomType, uint64 patomSize);
158 virtual ~STTSAtom();
159 void OnProcessMetaData();
160 char *OnGetAtomName();
162 uint64 getSUMCounts() {return SUMCounts;};
163 uint64 getSUMDurations() {return SUMDurations;};
164 uint32 getSampleForTime(uint32 pTime);
165 uint32 getSampleForFrame(uint32 pFrame);
166 private:
167 array_header theHeader;
168 TimeToSampleArray theTimeToSampleArray;
169 uint64 SUMDurations;
170 uint64 SUMCounts;
173 // Atom class for reading the sample to chunk atom
174 class STSCAtom : public AtomBase {
175 public:
176 STSCAtom(BPositionIO *pStream, off_t pstreamOffset, uint32 patomType, uint64 patomSize);
177 virtual ~STSCAtom();
178 void OnProcessMetaData();
179 char *OnGetAtomName();
181 uint32 getChunkForSample(uint32 pSample, uint32 *pOffsetInChunk);
182 uint32 getFirstSampleInChunk(uint32 pChunkID);
183 uint32 getNoSamplesInChunk(uint32 pChunkID);
184 private:
185 array_header theHeader;
186 SampleToChunkArray theSampleToChunkArray;
189 // Atom class for reading the chunk to offset atom
190 class STCOAtom : public AtomBase {
191 public:
192 STCOAtom(BPositionIO *pStream, off_t pstreamOffset, uint32 patomType, uint64 patomSize);
193 virtual ~STCOAtom();
194 void OnProcessMetaData();
195 char *OnGetAtomName();
197 uint64 getOffsetForChunk(uint32 pChunkID);
198 uint32 getTotalChunks() {return theHeader.NoEntries;};
200 protected:
201 // Read a single chunk offset from Stream
202 virtual uint64 OnGetChunkOffset();
204 private:
205 array_header theHeader;
206 ChunkToOffsetArray theChunkToOffsetArray;
209 // Atom class for reading the sync sample atom
210 class STSSAtom : public AtomBase {
211 public:
212 STSSAtom(BPositionIO *pStream, off_t pstreamOffset, uint32 patomType, uint64 patomSize);
213 virtual ~STSSAtom();
214 void OnProcessMetaData();
215 char *OnGetAtomName();
217 bool IsSyncSample(uint32 pSampleNo);
218 private:
219 array_header theHeader;
220 SyncSampleArray theSyncSampleArray;
223 // Atom class for reading the sample size atom
224 class STSZAtom : public AtomBase {
225 public:
226 STSZAtom(BPositionIO *pStream, off_t pstreamOffset, uint32 patomType, uint64 patomSize);
227 virtual ~STSZAtom();
228 void OnProcessMetaData();
229 char *OnGetAtomName();
231 uint32 getSizeForSample(uint32 pSampleNo);
232 bool IsSingleSampleSize();
233 private:
234 SampleSizeHeader theHeader;
235 SampleSizeArray theSampleSizeArray;
238 // Atom class for reading the skip atom
239 class SKIPAtom : public AtomBase {
240 public:
241 SKIPAtom(BPositionIO *pStream, off_t pstreamOffset, uint32 patomType, uint64 patomSize);
242 virtual ~SKIPAtom();
243 void OnProcessMetaData();
244 char *OnGetAtomName();
248 // Atom class for reading the media data atom
249 class MDATAtom : public AtomBase {
250 public:
251 MDATAtom(BPositionIO *pStream, off_t pstreamOffset, uint32 patomType, uint64 patomSize);
252 virtual ~MDATAtom();
253 void OnProcessMetaData();
254 char *OnGetAtomName();
255 off_t getEOF();
258 class ESDSAtom : public AtomBase {
259 public:
260 ESDSAtom(BPositionIO *pStream, off_t pstreamOffset, uint32 patomType, uint64 patomSize);
261 virtual ~ESDSAtom();
262 void OnProcessMetaData();
263 char *OnGetAtomName();
265 uint8 *getVOL();
266 private:
267 uint8 *theVOL;
270 // Atom class for reading the sdst atom
271 class STSDAtom : public AtomBase {
272 public:
273 STSDAtom(BPositionIO *pStream, off_t pstreamOffset, uint32 patomType, uint64 patomSize);
274 virtual ~STSDAtom();
275 void OnProcessMetaData();
276 char *OnGetAtomName();
278 VideoDescriptionV0 getAsVideo();
279 SoundDescriptionV1 getAsAudio();
281 private:
282 uint32 getMediaComponentSubType();
284 void ReadSoundDescription();
285 void ReadVideoDescription();
287 array_header theHeader;
288 SoundDescArray theAudioDescArray;
289 VideoDescArray theVideoDescArray;
292 // Atom class for reading the track header atom
293 class TKHDAtom : public AtomBase {
294 public:
295 TKHDAtom(BPositionIO *pStream, off_t pstreamOffset, uint32 patomType, uint64 patomSize);
296 virtual ~TKHDAtom();
297 void OnProcessMetaData();
298 char *OnGetAtomName();
300 uint32 getDuration() {return theHeader.Duration;}; // duration is in the scale of the media
302 // Flags3 should contain the following
303 // 0x001 Track enabled
304 // 0x002 Track in Movie
305 // 0x004 Track in Preview
306 // 0x008 Track in Poster
307 // It seems active tracks have all 4 flags set?
309 bool IsActive() {return ((theHeader.Flags3 == 0x0f) || (theHeader.Flags3 == 0x03));};
311 private:
312 tkhdV1 theHeader;
313 uint8 Version;
317 // Atom class for reading the media header atom
318 class MDHDAtom : public AtomBase {
319 public:
320 MDHDAtom(BPositionIO *pStream, off_t pstreamOffset, uint32 patomType, uint64 patomSize);
321 virtual ~MDHDAtom();
322 void OnProcessMetaData();
323 char *OnGetAtomName();
325 // return duration in ms
326 bigtime_t getDuration();
327 uint32 getTimeScale();
329 private:
330 mdhd theHeader;
334 // Atom class for reading the video media header atom
335 class VMHDAtom : public AtomBase {
336 public:
337 VMHDAtom(BPositionIO *pStream, off_t pstreamOffset, uint32 patomType, uint64 patomSize);
338 virtual ~VMHDAtom();
339 void OnProcessMetaData();
340 char *OnGetAtomName();
344 // Atom class for reading the sound media header atom
345 class SMHDAtom : public AtomBase {
346 public:
347 SMHDAtom(BPositionIO *pStream, off_t pstreamOffset, uint32 patomType, uint64 patomSize);
348 virtual ~SMHDAtom();
349 void OnProcessMetaData();
350 char *OnGetAtomName();
354 // Atom class for reading the track atom
355 class TRAKAtom : public AtomContainer {
356 public:
357 TRAKAtom(BPositionIO *pStream, off_t pstreamOffset, uint32 patomType, uint64 patomSize);
358 virtual ~TRAKAtom();
359 void OnProcessMetaData();
360 char *OnGetAtomName();
361 void OnChildProcessingComplete();
363 bigtime_t Duration(uint32 TimeScale); // Return duration of track
364 uint32 FrameCount() {return framecount;};
365 bool IsVideo(); // Is this a video track
366 bool IsAudio(); // Is this a audio track
367 // GetAudioMetaData() // If this is a audio track get the audio meta data
368 // GetVideoMetaData() // If this is a video track get the video meta data
370 uint32 getTimeForFrame(uint32 pFrame, uint32 pTimeScale);
371 uint32 getSampleForTime(uint32 pTime);
372 uint32 getSampleForFrame(uint32 pFrame);
373 uint32 getChunkForSample(uint32 pSample, uint32 *pOffsetInChunk);
374 uint64 getOffsetForChunk(uint32 pChunkID);
375 uint32 getFirstSampleInChunk(uint32 pChunkID);
376 uint32 getSizeForSample(uint32 pSample);
377 uint32 getNoSamplesInChunk(uint32 pChunkID);
378 uint32 getTotalChunks();
380 bool IsSyncSample(uint32 pSampleNo);
381 bool IsSingleSampleSize();
382 bool IsActive();
384 uint32 getBytesPerSample();
386 TKHDAtom *getTKHDAtom();
387 MDHDAtom *getMDHDAtom();
388 private:
389 TKHDAtom *theTKHDAtom;
390 MDHDAtom *theMDHDAtom;
392 uint32 framecount;
393 uint32 bytespersample;
396 // Atom class for reading the media container atom
397 class MDIAAtom : public AtomContainer {
398 public:
399 MDIAAtom(BPositionIO *pStream, off_t pstreamOffset, uint32 patomType, uint64 patomSize);
400 virtual ~MDIAAtom();
401 void OnProcessMetaData();
402 char *OnGetAtomName();
404 uint32 getMediaComponentSubType();
407 // Atom class for reading the media information atom
408 class MINFAtom : public AtomContainer {
409 public:
410 MINFAtom(BPositionIO *pStream, off_t pstreamOffset, uint32 patomType, uint64 patomSize);
411 virtual ~MINFAtom();
412 void OnProcessMetaData();
413 char *OnGetAtomName();
415 uint32 getMediaComponentSubType();
418 // Atom class for reading the stbl atom
419 class STBLAtom : public AtomContainer {
420 public:
421 STBLAtom(BPositionIO *pStream, off_t pstreamOffset, uint32 patomType, uint64 patomSize);
422 virtual ~STBLAtom();
423 void OnProcessMetaData();
424 char *OnGetAtomName();
426 uint32 getMediaComponentSubType();
429 // Atom class for reading the dinf atom
430 class DINFAtom : public AtomContainer {
431 public:
432 DINFAtom(BPositionIO *pStream, off_t pstreamOffset, uint32 patomType, uint64 patomSize);
433 virtual ~DINFAtom();
434 void OnProcessMetaData();
435 char *OnGetAtomName();
438 // Atom class for reading the tmcd atom
439 class TMCDAtom : public AtomBase {
440 public:
441 TMCDAtom(BPositionIO *pStream, off_t pstreamOffset, uint32 patomType, uint64 patomSize);
442 virtual ~TMCDAtom();
443 void OnProcessMetaData();
444 char *OnGetAtomName();
448 // Atom class for reading the wave atom
449 class WAVEAtom : public AtomBase {
450 public:
451 WAVEAtom(BPositionIO *pStream, off_t pstreamOffset, uint32 patomType, uint64 patomSize);
452 virtual ~WAVEAtom();
453 void OnProcessMetaData();
454 char *OnGetAtomName();
455 wave_format_ex getWaveFormat() {return theWaveFormat;};
456 private:
457 wave_format_ex theWaveFormat;
460 // Atom class for reading the Media Handler atom
461 class HDLRAtom : public AtomBase {
462 public:
463 HDLRAtom(BPositionIO *pStream, off_t pstreamOffset, uint32 patomType, uint64 patomSize);
464 virtual ~HDLRAtom();
465 void OnProcessMetaData();
466 char *OnGetAtomName();
468 bool IsVideoHandler();
469 bool IsAudioHandler();
470 uint32 getMediaComponentSubType();
472 private:
473 hdlr theHeader;
476 class FTYPAtom : public AtomBase {
477 public:
478 FTYPAtom(BPositionIO *pStream, off_t pstreamOffset, uint32 patomType, uint64 patomSize);
479 virtual ~FTYPAtom();
480 void OnProcessMetaData();
481 char *OnGetAtomName();
482 bool HasBrand(uint32 brand);
484 private:
485 uint32 major_brand;
486 uint32 minor_version;
487 uint32 compatable_brands[32]; // Should be infinite but we will settle for max 32
488 uint32 total_brands;
492 #endif