2 * Copyright (c) 2005, David McPaul
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.
30 #include <MediaDefs.h>
31 #include <MediaFormats.h>
32 #include <SupportDefs.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
{
57 MVHDAtom(BPositionIO
*pStream
, off_t pStreamOffset
,
58 uint32 pAtomType
, uint64 pAtomSize
);
60 void OnProcessMetaData();
61 const char *OnGetAtomName();
62 uint32
GetTimeScale() {return theHeader
.TimeScale
;};
63 uint32
GetDuration() {return theHeader
.Duration
;};
70 // Atom class for reading the moov atom
71 class MOOVAtom
: public AtomContainer
{
73 MOOVAtom(BPositionIO
*pStream
, off_t pStreamOffset
,
74 uint32 pAtomType
, uint64 pAtomSize
);
76 void OnProcessMetaData();
77 const char *OnGetAtomName();
79 MVHDAtom
* GetMVHDAtom();
82 MVHDAtom
* theMVHDAtom
;
86 // Atom class for reading the cmov atom
87 class CMOVAtom
: public AtomContainer
{
89 CMOVAtom(BPositionIO
*pStream
, off_t pStreamOffset
,
90 uint32 pAtomType
, uint64 pAtomSize
);
93 BPositionIO
*OnGetStream();
94 void OnProcessMetaData();
95 void OnChildProcessingComplete();
97 const char *OnGetAtomName();
100 BMallocIO
* theUncompressedStream
;
104 // Atom class for reading the dcom atom
105 class DCOMAtom
: public AtomBase
{
107 DCOMAtom(BPositionIO
*pStream
, off_t pStreamOffset
,
108 uint32 pAtomType
, uint64 pAtomSize
);
110 void OnProcessMetaData();
111 const char *OnGetAtomName();
112 uint32
GetCompressionID() {return compressionID
;};
115 uint32 compressionID
;
119 // Atom class for reading the cmvd atom
120 class CMVDAtom
: public AtomBase
{
122 CMVDAtom(BPositionIO
*pStream
, off_t pStreamOffset
,
123 uint32 pAtomType
, uint64 pAtomSize
);
125 void OnProcessMetaData();
126 const char *OnGetAtomName();
127 uint32
GetUncompressedSize() {return UncompressedSize
;};
128 uint8
* GetCompressedData() {return Buffer
;};
129 uint32
GetBufferSize() {return BufferSize
;};
132 uint32 UncompressedSize
;
138 // Atom class for reading the ftyp atom
139 class FTYPAtom
: public AtomBase
{
141 FTYPAtom(BPositionIO
*pStream
, off_t pStreamOffset
,
142 uint32 pAtomType
, uint64 pAtomSize
);
144 void OnProcessMetaData();
145 const char *OnGetAtomName();
147 bool HasBrand(uint32 brand
);
151 uint32 minor_version
;
152 uint32 compatable_brands
[32];
153 // Should be infinite but we will settle for max 32
158 // Atom class for reading the wide atom
159 class WIDEAtom
: public AtomBase
{
161 WIDEAtom(BPositionIO
*pStream
, off_t pStreamOffset
,
162 uint32 pAtomType
, uint64 pAtomSize
);
164 void OnProcessMetaData();
165 const char *OnGetAtomName();
169 // Atom class for reading the free atom
170 class FREEAtom
: public AtomBase
{
172 FREEAtom(BPositionIO
*pStream
, off_t pStreamOffset
,
173 uint32 pAtomType
, uint64 pAtomSize
);
175 void OnProcessMetaData();
176 const char *OnGetAtomName();
180 // Atom class for reading the preview atom
181 class PNOTAtom
: public AtomBase
{
183 PNOTAtom(BPositionIO
*pStream
, off_t pStreamOffset
,
184 uint32 pAtomType
, uint64 pAtomSize
);
186 void OnProcessMetaData();
187 const char *OnGetAtomName();
192 // Atom class for reading the time to sample atom
193 class STTSAtom
: public FullAtom
{
195 STTSAtom(BPositionIO
*pStream
, off_t pStreamOffset
,
196 uint32 pAtomType
, uint64 pAtomSize
);
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
; };
209 array_header theHeader
;
210 TimeToSampleArray theTimeToSampleArray
;
211 bigtime_t SUMDurations
;
217 // Atom class for reading the composition time to sample atom
218 class CTTSAtom
: public FullAtom
{
220 CTTSAtom(BPositionIO
*pStream
, off_t pStreamOffset
,
221 uint32 pAtomType
, uint64 pAtomSize
);
223 void OnProcessMetaData();
224 const char *OnGetAtomName();
227 array_header theHeader
;
228 CompTimeToSampleArray theCompTimeToSampleArray
;
232 // Atom class for reading the sample to chunk atom
233 class STSCAtom
: public FullAtom
{
235 STSCAtom(BPositionIO
*pStream
, off_t pStreamOffset
,
236 uint32 pAtomType
, uint64 pAtomSize
);
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
);
247 array_header theHeader
;
248 SampleToChunkArray theSampleToChunkArray
;
252 // Atom class for reading the chunk to offset atom
253 class STCOAtom
: public FullAtom
{
255 STCOAtom(BPositionIO
*pStream
, off_t pStreamOffset
,
256 uint32 pAtomType
, uint64 pAtomSize
);
258 void OnProcessMetaData();
259 const char *OnGetAtomName();
261 uint64
GetOffsetForChunk(uint32 pChunkIndex
);
262 uint32
GetTotalChunks() {return theHeader
.NoEntries
;};
265 // Read a single chunk offset from Stream
266 virtual uint64
OnGetChunkOffset();
269 array_header theHeader
;
270 ChunkToOffsetArray theChunkToOffsetArray
;
274 // Atom class for reading the sync sample atom
275 class STSSAtom
: public FullAtom
{
277 STSSAtom(BPositionIO
*pStream
, off_t pStreamOffset
,
278 uint32 pAtomType
, uint64 pAtomSize
);
280 void OnProcessMetaData();
281 const char *OnGetAtomName();
283 bool IsSyncSample(uint32 pSampleNo
);
286 array_header theHeader
;
287 SyncSampleArray theSyncSampleArray
;
291 // Atom class for reading the sample size atom
292 class STSZAtom
: public FullAtom
{
294 STSZAtom(BPositionIO
*pStream
, off_t pStreamOffset
,
295 uint32 pAtomType
, uint64 pAtomSize
);
297 void OnProcessMetaData();
298 const char *OnGetAtomName();
300 uint32
GetSizeForSample(uint32 pSampleNo
);
301 bool IsSingleSampleSize();
307 SampleSizeArray theSampleSizeArray
;
311 // Atom class for reading the sample size 2 atom
312 class STZ2Atom
: public FullAtom
{
314 STZ2Atom(BPositionIO
*pStream
, off_t pStreamOffset
,
315 uint32 pAtomType
, uint64 pAtomSize
);
317 void OnProcessMetaData();
318 const char *OnGetAtomName();
320 uint32
GetSizeForSample(uint32 pSampleNo
);
321 bool IsSingleSampleSize();
327 SampleSizeArray theSampleSizeArray
;
331 // Atom class for reading the skip atom
332 class SKIPAtom
: public AtomBase
{
334 SKIPAtom(BPositionIO
*pStream
, off_t pStreamOffset
,
335 uint32 pAtomType
, uint64 pAtomSize
);
337 void OnProcessMetaData();
338 const char *OnGetAtomName();
342 // Atom class for reading the media data atom
343 class MDATAtom
: public AtomBase
{
345 MDATAtom(BPositionIO
*pStream
, off_t pStreamOffset
,
346 uint32 pAtomType
, uint64 pAtomSize
);
348 void OnProcessMetaData();
349 const char *OnGetAtomName();
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
{
358 DecoderConfigAtom(BPositionIO
*pStream
,
359 off_t pStreamOffset
, uint32 pAtomType
,
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
;};
382 uint8
*theDecoderConfig
;
383 size_t DecoderConfigSize
;
387 // Atom class for reading the ESDS decoder config atom
388 class ESDSAtom
: public DecoderConfigAtom
{
390 ESDSAtom(BPositionIO
*pStream
, off_t pStreamOffset
,
391 uint32 pAtomType
, uint64 pAtomSize
);
393 void OnProcessMetaData();
394 const char *OnGetAtomName();
395 void OnOverrideAudioDescription(
396 AudioDescription
*pAudioDescription
);
397 void OnOverrideVideoDescription(
398 VideoDescription
*pVideoDescription
);
403 uint32 NeededBufferSize
;
406 AACHeader theAACHeader
;
410 // Atom class for reading the ALAC decoder config atom
411 class ALACAtom
: public DecoderConfigAtom
{
413 ALACAtom(BPositionIO
*pStream
, off_t pStreamOffset
,
414 uint32 pAtomType
, uint64 pAtomSize
);
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
{
428 WAVEAtom(BPositionIO
*pStream
, off_t pStreamOffset
,
429 uint32 pAtomType
, uint64 pAtomSize
);
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
{
443 STSDAtom(BPositionIO
*pStream
, off_t pStreamOffset
,
444 uint32 pAtomType
, uint64 pAtomSize
);
446 void OnProcessMetaData();
447 const char *OnGetAtomName();
449 VideoDescription
GetAsVideo();
450 AudioDescription
GetAsAudio();
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();
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
{
475 TKHDAtom(BPositionIO
*pStream
, off_t pStreamOffset
,
476 uint32 pAtomType
, uint64 pAtomSize
);
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));};
499 // Atom class for reading the media header atom
500 class MDHDAtom
: public FullAtom
{
502 MDHDAtom(BPositionIO
*pStream
, off_t pStreamOffset
,
503 uint32 pAtomType
, uint64 pAtomSize
);
505 void OnProcessMetaData();
506 const char *OnGetAtomName();
508 // return duration in ms
509 bigtime_t
GetDuration();
510 uint32
GetTimeScale();
518 // Atom class for reading the video media header atom
519 class VMHDAtom
: public FullAtom
{
521 VMHDAtom(BPositionIO
*pStream
, off_t pStreamOffset
,
522 uint32 pAtomType
, uint64 pAtomSize
);
524 void OnProcessMetaData();
525 const char *OnGetAtomName();
527 bool IsCopyMode() {return theHeader
.GraphicsMode
== 0;};
533 // Atom class for reading the sound media header atom
534 class SMHDAtom
: public FullAtom
{
536 SMHDAtom(BPositionIO
*pStream
, off_t pStreamOffset
,
537 uint32 pAtomType
, uint64 pAtomSize
);
539 void OnProcessMetaData();
540 const char *OnGetAtomName();
547 // Atom class for reading the track atom
548 class TRAKAtom
: public AtomContainer
{
550 TRAKAtom(BPositionIO
*pStream
, off_t pStreamOffset
,
551 uint32 pAtomType
, uint64 pAtomSize
);
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
);
579 float GetSampleRate();
580 float GetFrameRate();
582 bool IsSyncSample(uint32 pSampleNo
);
583 bool IsSingleSampleSize();
586 uint32
GetBytesPerSample();
588 TKHDAtom
*GetTKHDAtom();
589 MDHDAtom
*GetMDHDAtom();
591 TKHDAtom
*theTKHDAtom
;
592 MDHDAtom
*theMDHDAtom
;
595 uint32 bytesPerSample
;
600 // Atom class for reading the media container atom
601 class MDIAAtom
: public AtomContainer
{
603 MDIAAtom(BPositionIO
*pStream
, off_t pStreamOffset
,
604 uint32 pAtomType
, uint64 pAtomSize
);
606 void OnProcessMetaData();
607 const char *OnGetAtomName();
609 uint32
GetMediaHandlerType();
613 // Atom class for reading the media information atom
614 class MINFAtom
: public AtomContainer
{
616 MINFAtom(BPositionIO
*pStream
, off_t pStreamOffset
,
617 uint32 pAtomType
, uint64 pAtomSize
);
619 void OnProcessMetaData();
620 const char *OnGetAtomName();
622 uint32
GetMediaHandlerType();
626 // Atom class for reading the stbl atom
627 class STBLAtom
: public AtomContainer
{
629 STBLAtom(BPositionIO
*pStream
, off_t pStreamOffset
,
630 uint32 pAtomType
, uint64 pAtomSize
);
632 void OnProcessMetaData();
633 const char *OnGetAtomName();
635 uint32
GetMediaHandlerType();
639 // Atom class for reading the dinf atom
640 class DINFAtom
: public AtomContainer
{
642 DINFAtom(BPositionIO
*pStream
, off_t pStreamOffset
,
643 uint32 pAtomType
, uint64 pAtomSize
);
645 void OnProcessMetaData();
646 const char *OnGetAtomName();
650 // Atom class for reading the tmcd atom
651 class TMCDAtom
: public AtomBase
{
653 TMCDAtom(BPositionIO
*pStream
, off_t pStreamOffset
,
654 uint32 pAtomType
, uint64 pAtomSize
);
656 void OnProcessMetaData();
657 const char *OnGetAtomName();
661 // Atom class for reading the dac3 atom
662 class DAC3Atom
: public DecoderConfigAtom
{
664 DAC3Atom(BPositionIO
*pStream
, off_t pStreamOffset
,
665 uint32 pAtomType
, uint64 pAtomSize
);
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
{
679 DEC3Atom(BPositionIO
*pStream
, off_t pStreamOffset
,
680 uint32 pAtomType
, uint64 pAtomSize
);
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
{
694 HDLRAtom(BPositionIO
*pStream
, off_t pStreamOffset
, uint32 pAtomType
, uint64 pAtomSize
);
696 void OnProcessMetaData();
697 const char *OnGetAtomName();
699 bool IsVideoHandler();
700 bool IsAudioHandler();
701 uint32
GetMediaHandlerType();