FlacFrameParser.h (2352B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef FLAC_FRAME_PARSER_H_ 8 #define FLAC_FRAME_PARSER_H_ 9 10 #include "MediaDecoder.h" // For MetadataTags 11 #include "MediaInfo.h" 12 #include "MediaResource.h" 13 #include "mozilla/Maybe.h" 14 #include "mozilla/Result.h" 15 16 namespace mozilla { 17 18 #define FLAC_MAX_CHANNELS 8 19 #define FLAC_MIN_BLOCKSIZE 16 20 #define FLAC_MAX_BLOCKSIZE 65535 21 #define FLAC_MIN_FRAME_SIZE 11 22 #define FLAC_MAX_FRAME_HEADER_SIZE 16 23 #define FLAC_MAX_FRAME_SIZE \ 24 (FLAC_MAX_FRAME_HEADER_SIZE + FLAC_MAX_BLOCKSIZE * FLAC_MAX_CHANNELS * 3) 25 26 class OpusParser; 27 28 // Decode a Flac Metadata block contained in either a ogg packet 29 // (https://xiph.org/flac/ogg_mapping.html) or in flac container 30 // (https://xiph.org/flac/format.html#frame_header) 31 32 class FlacFrameParser { 33 public: 34 FlacFrameParser(); 35 ~FlacFrameParser(); 36 37 Result<bool, nsresult> IsHeaderBlock(const uint8_t* aPacket, 38 size_t aLength) const; 39 // Return the length of the block header (METADATA_BLOCK_HEADER+ 40 // METADATA_BLOCK_DATA), aPacket must point to at least 4 41 // bytes and to a valid block header start (as determined by IsHeaderBlock). 42 uint32_t HeaderBlockLength(const uint8_t* aPacket) const; 43 Result<Ok, nsresult> DecodeHeaderBlock(const uint8_t* aPacket, 44 size_t aLength); 45 bool HasFullMetadata() const { return mFullMetadata; } 46 // Return the duration in frames found in the block. -1 if error 47 // such as invalid packet. 48 int64_t BlockDuration(const uint8_t* aPacket, size_t aLength) const; 49 50 // Return a hash table with tag metadata. 51 UniquePtr<MetadataTags> GetTags() const; 52 53 AudioInfo mInfo; 54 55 private: 56 bool ReconstructFlacGranulepos(void); 57 Maybe<uint32_t> mNumHeaders; 58 uint32_t mMinBlockSize; 59 uint32_t mMaxBlockSize; 60 uint32_t mMinFrameSize; 61 uint32_t mMaxFrameSize; 62 uint64_t mNumFrames; 63 bool mFullMetadata; 64 uint32_t mPacketCount; 65 66 // Used to decode the vorbis comment metadata. 67 UniquePtr<OpusParser> mParser; 68 }; 69 70 } // namespace mozilla 71 72 #endif // FLAC_FRAME_PARSER_H_