3rdparty/licenseReport: Add seperate LGPL checks
[haiku.git] / src / add-ons / media / plugins / mp4_reader / libMP4 / MP4Parser.h
blobbec95ef99b8083bc1b81c3ad8ec532b3cf80b2cc
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 _MP4_PARSER_H
26 #define _MP4_PARSER_H
29 #include <File.h>
30 #include <MediaDefs.h>
31 #include <MediaFormats.h>
32 #include <SupportDefs.h>
34 // Std Headers
35 #include <map>
37 #include "MP4Atom.h"
40 typedef CompTimeToSample* CompTimeToSamplePtr;
41 typedef std::map<uint32, CompTimeToSamplePtr, std::less<uint32> > CompTimeToSampleArray;
42 typedef TimeToSample* TimeToSamplePtr;
43 typedef std::map<uint32, TimeToSamplePtr, std::less<uint32> > TimeToSampleArray;
44 typedef SampleToChunk* SampleToChunkPtr;
45 typedef std::vector<SampleToChunkPtr> SampleToChunkArray;
46 typedef ChunkToOffset* ChunkToOffsetPtr;
47 typedef std::map<uint32, ChunkToOffsetPtr, std::less<uint32> > ChunkToOffsetArray;
48 typedef SyncSample* SyncSamplePtr;
49 typedef std::vector<SyncSamplePtr> SyncSampleArray;
50 typedef SampleSizeEntry* SampleSizePtr;
51 typedef std::vector<SampleSizePtr> SampleSizeArray;
54 // Atom class for reading the movie header atom
55 class MVHDAtom : public FullAtom {
56 public:
57 MVHDAtom(BPositionIO *pStream, off_t pStreamOffset,
58 uint32 pAtomType, uint64 pAtomSize);
59 virtual ~MVHDAtom();
60 void OnProcessMetaData();
61 const char *OnGetAtomName();
62 uint32 GetTimeScale() {return theHeader.TimeScale;};
63 uint32 GetDuration() {return theHeader.Duration;};
65 private:
66 mvhdV1 theHeader;
70 // Atom class for reading the moov atom
71 class MOOVAtom : public AtomContainer {
72 public:
73 MOOVAtom(BPositionIO *pStream, off_t pStreamOffset,
74 uint32 pAtomType, uint64 pAtomSize);
75 virtual ~MOOVAtom();
76 void OnProcessMetaData();
77 const char *OnGetAtomName();
79 MVHDAtom* GetMVHDAtom();
81 private:
82 MVHDAtom* theMVHDAtom;
86 // Atom class for reading the cmov atom
87 class CMOVAtom : public AtomContainer {
88 public:
89 CMOVAtom(BPositionIO *pStream, off_t pStreamOffset,
90 uint32 pAtomType, uint64 pAtomSize);
91 virtual ~CMOVAtom();
93 BPositionIO *OnGetStream();
94 void OnProcessMetaData();
95 void OnChildProcessingComplete();
97 const char *OnGetAtomName();
99 private:
100 BMallocIO* theUncompressedStream;
104 // Atom class for reading the dcom atom
105 class DCOMAtom : public AtomBase {
106 public:
107 DCOMAtom(BPositionIO *pStream, off_t pStreamOffset,
108 uint32 pAtomType, uint64 pAtomSize);
109 virtual ~DCOMAtom();
110 void OnProcessMetaData();
111 const char *OnGetAtomName();
112 uint32 GetCompressionID() {return compressionID;};
114 private:
115 uint32 compressionID;
119 // Atom class for reading the cmvd atom
120 class CMVDAtom : public AtomBase {
121 public:
122 CMVDAtom(BPositionIO *pStream, off_t pStreamOffset,
123 uint32 pAtomType, uint64 pAtomSize);
124 virtual ~CMVDAtom();
125 void OnProcessMetaData();
126 const char *OnGetAtomName();
127 uint32 GetUncompressedSize() {return UncompressedSize;};
128 uint8* GetCompressedData() {return Buffer;};
129 uint32 GetBufferSize() {return BufferSize;};
131 private:
132 uint32 UncompressedSize;
133 uint32 BufferSize;
134 uint8* Buffer;
138 // Atom class for reading the ftyp atom
139 class FTYPAtom : public AtomBase {
140 public:
141 FTYPAtom(BPositionIO *pStream, off_t pStreamOffset,
142 uint32 pAtomType, uint64 pAtomSize);
143 virtual ~FTYPAtom();
144 void OnProcessMetaData();
145 const char *OnGetAtomName();
147 bool HasBrand(uint32 brand);
149 private:
150 uint32 major_brand;
151 uint32 minor_version;
152 uint32 compatable_brands[32];
153 // Should be infinite but we will settle for max 32
154 uint32 total_brands;
158 // Atom class for reading the wide atom
159 class WIDEAtom : public AtomBase {
160 public:
161 WIDEAtom(BPositionIO *pStream, off_t pStreamOffset,
162 uint32 pAtomType, uint64 pAtomSize);
163 virtual ~WIDEAtom();
164 void OnProcessMetaData();
165 const char *OnGetAtomName();
169 // Atom class for reading the free atom
170 class FREEAtom : public AtomBase {
171 public:
172 FREEAtom(BPositionIO *pStream, off_t pStreamOffset,
173 uint32 pAtomType, uint64 pAtomSize);
174 virtual ~FREEAtom();
175 void OnProcessMetaData();
176 const char *OnGetAtomName();
180 // Atom class for reading the preview atom
181 class PNOTAtom : public AtomBase {
182 public:
183 PNOTAtom(BPositionIO *pStream, off_t pStreamOffset,
184 uint32 pAtomType, uint64 pAtomSize);
185 virtual ~PNOTAtom();
186 void OnProcessMetaData();
187 const char *OnGetAtomName();
192 // Atom class for reading the time to sample atom
193 class STTSAtom : public FullAtom {
194 public:
195 STTSAtom(BPositionIO *pStream, off_t pStreamOffset,
196 uint32 pAtomType, uint64 pAtomSize);
197 virtual ~STTSAtom();
198 void OnProcessMetaData();
199 const char *OnGetAtomName();
201 uint64 GetSUMCounts() { return SUMCounts; };
202 bigtime_t GetSUMDurations() { return SUMDurations; };
203 uint32 GetSampleForTime(bigtime_t pTime);
204 uint32 GetSampleForFrame(uint32 pFrame);
205 void SetFrameRate(float pFrameRate)
206 { FrameRate = pFrameRate; };
208 private:
209 array_header theHeader;
210 TimeToSampleArray theTimeToSampleArray;
211 bigtime_t SUMDurations;
212 uint64 SUMCounts;
213 float FrameRate;
217 // Atom class for reading the composition time to sample atom
218 class CTTSAtom : public FullAtom {
219 public:
220 CTTSAtom(BPositionIO *pStream, off_t pStreamOffset,
221 uint32 pAtomType, uint64 pAtomSize);
222 virtual ~CTTSAtom();
223 void OnProcessMetaData();
224 const char *OnGetAtomName();
226 private:
227 array_header theHeader;
228 CompTimeToSampleArray theCompTimeToSampleArray;
232 // Atom class for reading the sample to chunk atom
233 class STSCAtom : public FullAtom {
234 public:
235 STSCAtom(BPositionIO *pStream, off_t pStreamOffset,
236 uint32 pAtomType, uint64 pAtomSize);
237 virtual ~STSCAtom();
238 void OnProcessMetaData();
239 const char *OnGetAtomName();
241 uint32 GetChunkForSample(uint32 pSample,
242 uint32 *pOffsetInChunk);
243 uint32 GetFirstSampleInChunk(uint32 pChunkIndex);
244 uint32 GetNoSamplesInChunk(uint32 pChunkIndex);
246 private:
247 array_header theHeader;
248 SampleToChunkArray theSampleToChunkArray;
252 // Atom class for reading the chunk to offset atom
253 class STCOAtom : public FullAtom {
254 public:
255 STCOAtom(BPositionIO *pStream, off_t pStreamOffset,
256 uint32 pAtomType, uint64 pAtomSize);
257 virtual ~STCOAtom();
258 void OnProcessMetaData();
259 const char *OnGetAtomName();
261 uint64 GetOffsetForChunk(uint32 pChunkIndex);
262 uint32 GetTotalChunks() {return theHeader.NoEntries;};
264 protected:
265 // Read a single chunk offset from Stream
266 virtual uint64 OnGetChunkOffset();
268 private:
269 array_header theHeader;
270 ChunkToOffsetArray theChunkToOffsetArray;
274 // Atom class for reading the sync sample atom
275 class STSSAtom : public FullAtom {
276 public:
277 STSSAtom(BPositionIO *pStream, off_t pStreamOffset,
278 uint32 pAtomType, uint64 pAtomSize);
279 virtual ~STSSAtom();
280 void OnProcessMetaData();
281 const char *OnGetAtomName();
283 bool IsSyncSample(uint32 pSampleNo);
285 private:
286 array_header theHeader;
287 SyncSampleArray theSyncSampleArray;
291 // Atom class for reading the sample size atom
292 class STSZAtom : public FullAtom {
293 public:
294 STSZAtom(BPositionIO *pStream, off_t pStreamOffset,
295 uint32 pAtomType, uint64 pAtomSize);
296 virtual ~STSZAtom();
297 void OnProcessMetaData();
298 const char *OnGetAtomName();
300 uint32 GetSizeForSample(uint32 pSampleNo);
301 bool IsSingleSampleSize();
303 private:
304 uint32 SampleSize;
305 uint32 SampleCount;
307 SampleSizeArray theSampleSizeArray;
311 // Atom class for reading the sample size 2 atom
312 class STZ2Atom : public FullAtom {
313 public:
314 STZ2Atom(BPositionIO *pStream, off_t pStreamOffset,
315 uint32 pAtomType, uint64 pAtomSize);
316 virtual ~STZ2Atom();
317 void OnProcessMetaData();
318 const char *OnGetAtomName();
320 uint32 GetSizeForSample(uint32 pSampleNo);
321 bool IsSingleSampleSize();
322 private:
323 uint32 SampleSize;
324 uint32 SampleCount;
325 uint8 FieldSize;
327 SampleSizeArray theSampleSizeArray;
331 // Atom class for reading the skip atom
332 class SKIPAtom : public AtomBase {
333 public:
334 SKIPAtom(BPositionIO *pStream, off_t pStreamOffset,
335 uint32 pAtomType, uint64 pAtomSize);
336 virtual ~SKIPAtom();
337 void OnProcessMetaData();
338 const char *OnGetAtomName();
342 // Atom class for reading the media data atom
343 class MDATAtom : public AtomBase {
344 public:
345 MDATAtom(BPositionIO *pStream, off_t pStreamOffset,
346 uint32 pAtomType, uint64 pAtomSize);
347 virtual ~MDATAtom();
348 void OnProcessMetaData();
349 const char *OnGetAtomName();
350 off_t GetEOF();
354 // Subclass for handling special decoder config atoms like ESDS, ALAC, AVCC, AMR
355 // ESDS is actually a FullAtom but others are not :-(
356 class DecoderConfigAtom : public AtomBase {
357 public:
358 DecoderConfigAtom(BPositionIO *pStream,
359 off_t pStreamOffset, uint32 pAtomType,
360 uint64 pAtomSize);
361 virtual ~DecoderConfigAtom();
362 virtual void OnProcessMetaData();
363 virtual const char *OnGetAtomName();
365 // The decoder config is what the decoder uses for it's setup
366 // The values SHOULD mirror the Container values but often don't
367 // So we have to parse the config and use the values as container overrides
368 void OverrideAudioDescription(
369 AudioDescription *pAudioDescription);
370 virtual void OnOverrideAudioDescription(
371 AudioDescription *pAudioDescription) {};
372 void OverrideVideoDescription(
373 VideoDescription *pVideoDescription);
374 virtual void OnOverrideVideoDescription(
375 VideoDescription *pVideoDescription) {};
376 bool SkipTag(uint8 *ESDS, uint8 Tag, uint32 *offset);
378 uint8 *GetDecoderConfig();
379 size_t GetDecoderConfigSize() {return DecoderConfigSize;};
381 private:
382 uint8 *theDecoderConfig;
383 size_t DecoderConfigSize;
387 // Atom class for reading the ESDS decoder config atom
388 class ESDSAtom : public DecoderConfigAtom {
389 public:
390 ESDSAtom(BPositionIO *pStream, off_t pStreamOffset,
391 uint32 pAtomType, uint64 pAtomSize);
392 virtual ~ESDSAtom();
393 void OnProcessMetaData();
394 const char *OnGetAtomName();
395 void OnOverrideAudioDescription(
396 AudioDescription *pAudioDescription);
397 void OnOverrideVideoDescription(
398 VideoDescription *pVideoDescription);
400 private:
401 uint8 ESDSType;
402 uint8 StreamType;
403 uint32 NeededBufferSize;
404 uint32 MaxBitRate;
405 uint32 AvgBitRate;
406 AACHeader theAACHeader;
410 // Atom class for reading the ALAC decoder config atom
411 class ALACAtom : public DecoderConfigAtom {
412 public:
413 ALACAtom(BPositionIO *pStream, off_t pStreamOffset,
414 uint32 pAtomType, uint64 pAtomSize);
415 virtual ~ALACAtom();
416 void OnProcessMetaData();
417 const char *OnGetAtomName();
418 void OnOverrideAudioDescription(
419 AudioDescription *pAudioDescription);
420 void OnOverrideVideoDescription(
421 VideoDescription *pVideoDescription);
425 // Atom class for reading the WAVE atom for mp3 decoder config
426 class WAVEAtom : public DecoderConfigAtom {
427 public:
428 WAVEAtom(BPositionIO *pStream, off_t pStreamOffset,
429 uint32 pAtomType, uint64 pAtomSize);
430 virtual ~WAVEAtom();
431 void OnProcessMetaData();
432 const char *OnGetAtomName();
433 void OnOverrideAudioDescription(
434 AudioDescription *pAudioDescription);
435 void OnOverrideVideoDescription(
436 VideoDescription *pVideoDescription);
440 // Atom class for reading the Sample Description atom
441 class STSDAtom : public FullAtom {
442 public:
443 STSDAtom(BPositionIO *pStream, off_t pStreamOffset,
444 uint32 pAtomType, uint64 pAtomSize);
445 virtual ~STSDAtom();
446 void OnProcessMetaData();
447 const char *OnGetAtomName();
449 VideoDescription GetAsVideo();
450 AudioDescription GetAsAudio();
452 private:
453 uint32 GetMediaHandlerType();
455 void ReadDecoderConfig(uint8 **pDecoderConfig,
456 size_t *pDecoderConfigSize,
457 AudioDescription *pAudioDescription,
458 VideoDescription *pVideoDescription);
459 void ReadSoundDescription();
460 void ReadVideoDescription();
461 void ReadHintDescription();
463 uint32 codecid;
465 array_header theHeader;
466 SampleEntry theSampleEntry;
467 AudioDescription theAudioDescription;
468 VideoDescription theVideoDescription;
472 // Atom class for reading the track header atom
473 class TKHDAtom : public FullAtom {
474 public:
475 TKHDAtom(BPositionIO *pStream, off_t pStreamOffset,
476 uint32 pAtomType, uint64 pAtomSize);
477 virtual ~TKHDAtom();
478 void OnProcessMetaData();
479 const char *OnGetAtomName();
481 uint32 GetDuration() {return theHeader.Duration;};
482 // duration is in the scale of the media
484 // Flags3 should contain the following
485 // 0x001 Track enabled
486 // 0x002 Track in Movie
487 // 0x004 Track in Preview
488 // 0x008 Track in Poster
490 bool IsActive() {return ((GetFlags3() && 0x01)
491 || (GetFlags3() && 0x0f));};
493 private:
494 tkhdV1 theHeader;
499 // Atom class for reading the media header atom
500 class MDHDAtom : public FullAtom {
501 public:
502 MDHDAtom(BPositionIO *pStream, off_t pStreamOffset,
503 uint32 pAtomType, uint64 pAtomSize);
504 virtual ~MDHDAtom();
505 void OnProcessMetaData();
506 const char *OnGetAtomName();
508 // return duration in ms
509 bigtime_t GetDuration();
510 uint32 GetTimeScale();
512 private:
513 mdhdV1 theHeader;
518 // Atom class for reading the video media header atom
519 class VMHDAtom : public FullAtom {
520 public:
521 VMHDAtom(BPositionIO *pStream, off_t pStreamOffset,
522 uint32 pAtomType, uint64 pAtomSize);
523 virtual ~VMHDAtom();
524 void OnProcessMetaData();
525 const char *OnGetAtomName();
527 bool IsCopyMode() {return theHeader.GraphicsMode == 0;};
528 private:
529 vmhd theHeader ;
533 // Atom class for reading the sound media header atom
534 class SMHDAtom : public FullAtom {
535 public:
536 SMHDAtom(BPositionIO *pStream, off_t pStreamOffset,
537 uint32 pAtomType, uint64 pAtomSize);
538 virtual ~SMHDAtom();
539 void OnProcessMetaData();
540 const char *OnGetAtomName();
542 private:
543 smhd theHeader;
547 // Atom class for reading the track atom
548 class TRAKAtom : public AtomContainer {
549 public:
550 TRAKAtom(BPositionIO *pStream, off_t pStreamOffset,
551 uint32 pAtomType, uint64 pAtomSize);
552 virtual ~TRAKAtom();
553 void OnProcessMetaData();
554 const char *OnGetAtomName();
555 void OnChildProcessingComplete();
557 bigtime_t Duration(uint32 TimeScale); // Return duration of track
558 bigtime_t Duration() { return Duration(timeScale); }
559 uint32 FrameCount() { return frameCount; };
560 bool IsVideo(); // Is this a video track
561 bool IsAudio(); // Is this a audio track
562 // GetAudioMetaData() // If this is a audio track Get the audio meta data
563 // GetVideoMetaData() // If this is a video track Get the video meta data
565 bigtime_t GetTimeForFrame(uint32 pFrame);
566 uint32 GetFrameForTime(bigtime_t time);
567 uint32 GetSampleForTime(bigtime_t pTime);
568 uint32 GetSampleForFrame(uint32 pFrame);
569 uint32 GetChunkForSample(uint32 pSample,
570 uint32 *pOffsetInChunk);
571 uint64 GetOffsetForChunk(uint32 pChunkIndex);
572 uint32 GetFirstSampleInChunk(uint32 pChunkIndex);
573 uint32 GetSizeForSample(uint32 pSample);
574 uint32 GetNoSamplesInChunk(uint32 pChunkIndex);
575 uint32 GetTotalChunks();
576 uint32 GetChunkSize(uint32 pChunkIndex);
577 uint32 ChunkCount();
579 float GetSampleRate();
580 float GetFrameRate();
582 bool IsSyncSample(uint32 pSampleNo);
583 bool IsSingleSampleSize();
584 bool IsActive();
586 uint32 GetBytesPerSample();
588 TKHDAtom *GetTKHDAtom();
589 MDHDAtom *GetMDHDAtom();
590 private:
591 TKHDAtom *theTKHDAtom;
592 MDHDAtom *theMDHDAtom;
594 uint32 frameCount;
595 uint32 bytesPerSample;
596 uint32 timeScale;
600 // Atom class for reading the media container atom
601 class MDIAAtom : public AtomContainer {
602 public:
603 MDIAAtom(BPositionIO *pStream, off_t pStreamOffset,
604 uint32 pAtomType, uint64 pAtomSize);
605 virtual ~MDIAAtom();
606 void OnProcessMetaData();
607 const char *OnGetAtomName();
609 uint32 GetMediaHandlerType();
613 // Atom class for reading the media information atom
614 class MINFAtom : public AtomContainer {
615 public:
616 MINFAtom(BPositionIO *pStream, off_t pStreamOffset,
617 uint32 pAtomType, uint64 pAtomSize);
618 virtual ~MINFAtom();
619 void OnProcessMetaData();
620 const char *OnGetAtomName();
622 uint32 GetMediaHandlerType();
626 // Atom class for reading the stbl atom
627 class STBLAtom : public AtomContainer {
628 public:
629 STBLAtom(BPositionIO *pStream, off_t pStreamOffset,
630 uint32 pAtomType, uint64 pAtomSize);
631 virtual ~STBLAtom();
632 void OnProcessMetaData();
633 const char *OnGetAtomName();
635 uint32 GetMediaHandlerType();
639 // Atom class for reading the dinf atom
640 class DINFAtom : public AtomContainer {
641 public:
642 DINFAtom(BPositionIO *pStream, off_t pStreamOffset,
643 uint32 pAtomType, uint64 pAtomSize);
644 virtual ~DINFAtom();
645 void OnProcessMetaData();
646 const char *OnGetAtomName();
650 // Atom class for reading the tmcd atom
651 class TMCDAtom : public AtomBase {
652 public:
653 TMCDAtom(BPositionIO *pStream, off_t pStreamOffset,
654 uint32 pAtomType, uint64 pAtomSize);
655 virtual ~TMCDAtom();
656 void OnProcessMetaData();
657 const char *OnGetAtomName();
661 // Atom class for reading the dac3 atom
662 class DAC3Atom : public DecoderConfigAtom {
663 public:
664 DAC3Atom(BPositionIO *pStream, off_t pStreamOffset,
665 uint32 pAtomType, uint64 pAtomSize);
666 virtual ~DAC3Atom();
667 void OnProcessMetaData();
668 const char *OnGetAtomName();
669 void OnOverrideAudioDescription(
670 AudioDescription *pAudioDescription);
671 void OnOverrideVideoDescription(
672 VideoDescription *pVideoDescription);
676 // Atom class for reading the dec3 atom
677 class DEC3Atom : public DecoderConfigAtom {
678 public:
679 DEC3Atom(BPositionIO *pStream, off_t pStreamOffset,
680 uint32 pAtomType, uint64 pAtomSize);
681 virtual ~DEC3Atom();
682 void OnProcessMetaData();
683 const char *OnGetAtomName();
684 void OnOverrideAudioDescription(
685 AudioDescription *pAudioDescription);
686 void OnOverrideVideoDescription(
687 VideoDescription *pVideoDescription);
691 // Atom class for reading the Media Handler atom
692 class HDLRAtom : public FullAtom {
693 public:
694 HDLRAtom(BPositionIO *pStream, off_t pStreamOffset, uint32 pAtomType, uint64 pAtomSize);
695 virtual ~HDLRAtom();
696 void OnProcessMetaData();
697 const char *OnGetAtomName();
699 bool IsVideoHandler();
700 bool IsAudioHandler();
701 uint32 GetMediaHandlerType();
703 private:
704 hdlr theHeader;
705 char *name;
709 #endif