michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et cindent: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef MP3FrameParser_h michael@0: #define MP3FrameParser_h michael@0: michael@0: #include michael@0: michael@0: #include "mozilla/Mutex.h" michael@0: #include "nsString.h" michael@0: michael@0: namespace mozilla { michael@0: michael@0: // Simple parser to tell whether we've found an ID3 header and how long it is, michael@0: // so that we can skip it. michael@0: // XXX maybe actually parse this stuff? michael@0: class ID3Parser michael@0: { michael@0: public: michael@0: ID3Parser(); michael@0: michael@0: void Reset(); michael@0: bool ParseChar(char ch); michael@0: bool IsParsed() const; michael@0: uint32_t GetHeaderLength() const; michael@0: michael@0: private: michael@0: uint32_t mCurrentChar; michael@0: uint32_t mHeaderLength; michael@0: }; michael@0: michael@0: struct MP3Frame { michael@0: uint16_t mSync1 : 8; // Always all set michael@0: uint16_t mProtected : 1; // Ignored michael@0: uint16_t mLayer : 2; michael@0: uint16_t mVersion : 2; michael@0: uint16_t mSync2 : 3; // Always all set michael@0: uint16_t mPrivate : 1; // Ignored michael@0: uint16_t mPad : 1; michael@0: uint16_t mSampleRate : 2; // Index into mpeg_srates above michael@0: uint16_t mBitrate : 4; // Index into mpeg_bitrates above michael@0: michael@0: uint16_t CalculateLength(); michael@0: }; michael@0: michael@0: // Buffering parser for MP3 frames. michael@0: class MP3Parser michael@0: { michael@0: public: michael@0: MP3Parser(); michael@0: michael@0: // Forget all data the parser has seen so far. michael@0: void Reset(); michael@0: michael@0: // Parse the given byte. If we have found a frame header, return the length of michael@0: // the frame. michael@0: uint16_t ParseFrameLength(uint8_t ch); michael@0: michael@0: // Get the sample rate from the current header. michael@0: uint32_t GetSampleRate(); michael@0: michael@0: // Get the number of samples per frame. michael@0: uint32_t GetSamplesPerFrame(); michael@0: michael@0: private: michael@0: uint32_t mCurrentChar; michael@0: union { michael@0: uint8_t mRaw[3]; michael@0: MP3Frame mFrame; michael@0: } mData; michael@0: }; michael@0: michael@0: michael@0: // A description of the MP3 format and its extensions is available at michael@0: // michael@0: // http://www.codeproject.com/Articles/8295/MPEG-Audio-Frame-Header michael@0: // michael@0: // The data in MP3 streams is split into small frames, with each frame michael@0: // containing a fixed number of samples. The duration of a frame depends michael@0: // on the frame's bit rate and sample rate. Both values can vary among michael@0: // frames, so it is necessary to examine each individual frame of an MP3 michael@0: // stream to calculate the stream's overall duration. michael@0: // michael@0: // The MP3 frame parser extracts information from an MP3 data stream. It michael@0: // accepts a range of frames of an MP3 stream as input, and parses all michael@0: // frames for their duration. Callers can query the stream's overall michael@0: // duration from the parser. michael@0: // michael@0: // Call the methods NotifyDataArrived or Parse to add new data. If you added michael@0: // information for a certain stream position, you cannot go back to previous michael@0: // positions. The parser will simply ignore the input. If you skip stream michael@0: // positions, the duration of the related MP3 frames will be estimated from michael@0: // the stream's average. michael@0: // michael@0: // The method GetDuration returns calculated duration of the stream, including michael@0: // estimates for skipped ranges. michael@0: // michael@0: // All public methods are thread-safe. michael@0: michael@0: class MP3FrameParser michael@0: { michael@0: public: michael@0: MP3FrameParser(int64_t aLength=-1); michael@0: michael@0: bool IsMP3() { michael@0: MutexAutoLock mon(mLock); michael@0: return mIsMP3 != NOT_MP3; michael@0: } michael@0: michael@0: void Parse(const char* aBuffer, uint32_t aLength, uint64_t aStreamOffset); michael@0: michael@0: // Returns the duration, in microseconds. If the entire stream has not michael@0: // been parsed yet, this is an estimate based on the bitrate of the michael@0: // frames parsed so far. michael@0: int64_t GetDuration(); michael@0: michael@0: // Returns the offset of the first MP3 frame in the stream, or -1 of michael@0: // no MP3 frame has been detected yet. michael@0: int64_t GetMP3Offset(); michael@0: michael@0: // Returns true if we've seen the whole first frame of the MP3 stream, and michael@0: // therefore can make an estimate on the stream duration. michael@0: // Otherwise, returns false. michael@0: bool ParsedHeaders(); michael@0: michael@0: // Returns true if we know the exact duration of the MP3 stream; michael@0: // false otherwise. michael@0: bool HasExactDuration(); michael@0: michael@0: // Returns true if the parser needs more data for duration estimation. michael@0: bool NeedsData(); michael@0: michael@0: private: michael@0: michael@0: // Parses aBuffer, starting at offset 0. Returns the number of bytes michael@0: // parsed, relative to the start of the buffer. Note this may be michael@0: // greater than aLength if the headers in the buffer indicate that michael@0: // the frame or ID3 tag extends outside of aBuffer. Returns failure michael@0: // if too many non-MP3 bytes are parsed. michael@0: nsresult ParseBuffer(const uint8_t* aBuffer, michael@0: uint32_t aLength, michael@0: int64_t aStreamOffset, michael@0: uint32_t* aOutBytesRead); michael@0: michael@0: // A low-contention lock for protecting the parser results michael@0: Mutex mLock; michael@0: michael@0: // ID3 header parser. Keeps state between reads in case the header falls michael@0: // in between. michael@0: ID3Parser mID3Parser; michael@0: michael@0: // MP3 frame header parser. michael@0: MP3Parser mMP3Parser; michael@0: michael@0: // If we read |MAX_SKIPPED_BYTES| from the stream without finding any MP3 michael@0: // frames, we give up and report |NOT_MP3|. Here we track the cumulative size michael@0: // of any ID3 headers we've seen so big ID3 sections aren't counted towards michael@0: // skipped bytes. michael@0: uint32_t mTotalID3Size; michael@0: michael@0: // All fields below are protected by mLock michael@0: michael@0: // We keep stats on the size of all the frames we've seen, as well as how many michael@0: // so that we can estimate the duration of the rest of the stream. michael@0: uint64_t mTotalFrameSize; michael@0: uint64_t mFrameCount; michael@0: michael@0: // Offset of the last data parsed. This is the end offset of the last data michael@0: // block parsed, so it's the start offset we expect to get on the next michael@0: // call to Parse(). michael@0: uint64_t mOffset; michael@0: michael@0: // Total length of the stream in bytes. michael@0: int64_t mLength; michael@0: michael@0: // Offset of first MP3 frame in the bitstream. Has value -1 until the michael@0: // first MP3 frame is found. michael@0: int64_t mMP3Offset; michael@0: michael@0: // The exact number of frames in this stream, if we know it. -1 otherwise. michael@0: int64_t mNumFrames; michael@0: michael@0: // Number of audio samples per second and per frame. Fixed through the whole michael@0: // file. If we know these variables as well as the number of frames in the michael@0: // file, we can get an exact duration for the stream. michael@0: uint16_t mSamplesPerSecond; michael@0: uint16_t mSamplesPerFrame; michael@0: michael@0: // If the MP3 has a variable bitrate, then there *should* be metadata about michael@0: // the encoding in the first frame. We buffer the first frame here. michael@0: nsAutoCString mFirstFrame; michael@0: michael@0: // While we are reading the first frame, this is the stream offset of the michael@0: // last byte of that frame. -1 at all other times. michael@0: int64_t mFirstFrameEnd; michael@0: michael@0: enum eIsMP3 { michael@0: MAYBE_MP3, // We're giving the stream the benefit of the doubt... michael@0: DEFINITELY_MP3, // We've hit at least one ID3 tag or MP3 frame. michael@0: NOT_MP3 // Not found any evidence of the stream being MP3. michael@0: }; michael@0: michael@0: eIsMP3 mIsMP3; michael@0: michael@0: }; michael@0: michael@0: } michael@0: michael@0: #endif