1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef FLAC_PARSER_H_ 18 #define FLAC_PARSER_H_ 19 20 #include <stdint.h> 21 22 #include <array> 23 #include <cstdlib> 24 #include <string> 25 #include <vector> 26 27 // libFLAC parser 28 #include "FLAC/stream_decoder.h" 29 30 #include "include/data_source.h" 31 32 typedef int status_t; 33 34 struct FlacPicture { 35 int type; 36 std::string mimeType; 37 std::string description; 38 FLAC__uint32 width; 39 FLAC__uint32 height; 40 FLAC__uint32 depth; 41 FLAC__uint32 colors; 42 std::vector<char> data; 43 }; 44 45 class FLACParser { 46 public: 47 FLACParser(DataSource *source); 48 ~FLACParser(); 49 50 bool init(); 51 52 // stream properties getMaxBlockSize()53 unsigned getMaxBlockSize() const { return mStreamInfo.max_blocksize; } getSampleRate()54 unsigned getSampleRate() const { return mStreamInfo.sample_rate; } getChannels()55 unsigned getChannels() const { return mStreamInfo.channels; } getBitsPerSample()56 unsigned getBitsPerSample() const { return mStreamInfo.bits_per_sample; } getTotalSamples()57 FLAC__uint64 getTotalSamples() const { return mStreamInfo.total_samples; } 58 getStreamInfo()59 const FLAC__StreamMetadata_StreamInfo& getStreamInfo() const { 60 return mStreamInfo; 61 } 62 areVorbisCommentsValid()63 bool areVorbisCommentsValid() const { return mVorbisCommentsValid; } 64 getVorbisComments()65 const std::vector<std::string>& getVorbisComments() const { 66 return mVorbisComments; 67 } 68 arePicturesValid()69 bool arePicturesValid() const { return mPicturesValid; } 70 getPictures()71 const std::vector<FlacPicture> &getPictures() const { return mPictures; } 72 getLastFrameTimestamp()73 int64_t getLastFrameTimestamp() const { 74 return (1000000LL * mWriteHeader.number.sample_number) / getSampleRate(); 75 } 76 getLastFrameFirstSampleIndex()77 int64_t getLastFrameFirstSampleIndex() const { 78 return mWriteHeader.number.sample_number; 79 } 80 getNextFrameFirstSampleIndex()81 int64_t getNextFrameFirstSampleIndex() const { 82 return mWriteHeader.number.sample_number + mWriteHeader.blocksize; 83 } 84 85 bool decodeMetadata(); 86 size_t readBuffer(void *output, size_t output_size); 87 88 bool getSeekPositions(int64_t timeUs, std::array<int64_t, 4> &result); 89 flush()90 void flush() { 91 reset(mCurrentPos); 92 } 93 reset(int64_t newPosition)94 void reset(int64_t newPosition) { 95 if (mDecoder != NULL) { 96 mCurrentPos = newPosition; 97 mEOF = false; 98 if (newPosition == 0) { 99 mStreamInfoValid = false; 100 mVorbisCommentsValid = false; 101 mPicturesValid = false; 102 mVorbisComments.clear(); 103 mPictures.clear(); 104 FLAC__stream_decoder_reset(mDecoder); 105 } else { 106 FLAC__stream_decoder_flush(mDecoder); 107 } 108 } 109 } 110 getDecodePosition()111 int64_t getDecodePosition() { 112 uint64_t position; 113 if (mDecoder != NULL 114 && FLAC__stream_decoder_get_decode_position(mDecoder, &position)) { 115 return position; 116 } 117 return -1; 118 } 119 getDecoderStateString()120 const char *getDecoderStateString() { 121 return FLAC__stream_decoder_get_resolved_state_string(mDecoder); 122 } 123 isDecoderAtEndOfStream()124 bool isDecoderAtEndOfStream() const { 125 return FLAC__stream_decoder_get_state(mDecoder) == 126 FLAC__STREAM_DECODER_END_OF_STREAM; 127 } 128 129 private: 130 DataSource *mDataSource; 131 132 void (*mCopy)(int8_t *dst, const int *const *src, unsigned bytesPerSample, 133 unsigned nSamples, unsigned nChannels); 134 135 // handle to underlying libFLAC parser 136 FLAC__StreamDecoder *mDecoder; 137 138 // current position within the data source 139 off64_t mCurrentPos; 140 bool mEOF; 141 142 // cached when the STREAMINFO metadata is parsed by libFLAC 143 FLAC__StreamMetadata_StreamInfo mStreamInfo; 144 bool mStreamInfoValid; 145 146 const FLAC__StreamMetadata_SeekTable *mSeekTable; 147 uint64_t firstFrameOffset; 148 149 // cached when the VORBIS_COMMENT metadata is parsed by libFLAC 150 std::vector<std::string> mVorbisComments; 151 bool mVorbisCommentsValid; 152 153 // cached when the PICTURE metadata is parsed by libFLAC 154 std::vector<FlacPicture> mPictures; 155 bool mPicturesValid; 156 157 // cached when a decoded PCM block is "written" by libFLAC parser 158 bool mWriteRequested; 159 bool mWriteCompleted; 160 FLAC__FrameHeader mWriteHeader; 161 const FLAC__int32 *const *mWriteBuffer; 162 163 // most recent error reported by libFLAC parser 164 FLAC__StreamDecoderErrorStatus mErrorStatus; 165 166 // no copy constructor or assignment 167 FLACParser(const FLACParser &); 168 FLACParser &operator=(const FLACParser &); 169 170 // FLAC parser callbacks as C++ instance methods 171 FLAC__StreamDecoderReadStatus readCallback(FLAC__byte buffer[], 172 size_t *bytes); 173 FLAC__StreamDecoderSeekStatus seekCallback(FLAC__uint64 absolute_byte_offset); 174 FLAC__StreamDecoderTellStatus tellCallback( 175 FLAC__uint64 *absolute_byte_offset); 176 FLAC__StreamDecoderLengthStatus lengthCallback(FLAC__uint64 *stream_length); 177 FLAC__bool eofCallback(); 178 FLAC__StreamDecoderWriteStatus writeCallback( 179 const FLAC__Frame *frame, const FLAC__int32 *const buffer[]); 180 void metadataCallback(const FLAC__StreamMetadata *metadata); 181 void errorCallback(FLAC__StreamDecoderErrorStatus status); 182 183 // FLAC parser callbacks as C-callable functions 184 static FLAC__StreamDecoderReadStatus read_callback( 185 const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, 186 void *client_data); 187 static FLAC__StreamDecoderSeekStatus seek_callback( 188 const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, 189 void *client_data); 190 static FLAC__StreamDecoderTellStatus tell_callback( 191 const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, 192 void *client_data); 193 static FLAC__StreamDecoderLengthStatus length_callback( 194 const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, 195 void *client_data); 196 static FLAC__bool eof_callback(const FLAC__StreamDecoder *decoder, 197 void *client_data); 198 static FLAC__StreamDecoderWriteStatus write_callback( 199 const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, 200 const FLAC__int32 *const buffer[], void *client_data); 201 static void metadata_callback(const FLAC__StreamDecoder *decoder, 202 const FLAC__StreamMetadata *metadata, 203 void *client_data); 204 static void error_callback(const FLAC__StreamDecoder *decoder, 205 FLAC__StreamDecoderErrorStatus status, 206 void *client_data); 207 }; 208 209 #endif // FLAC_PARSER_H_ 210