| 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 "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 |
| 53 | unsigned getMaxBlockSize() const { return mStreamInfo.max_blocksize; } |
| 54 | unsigned getSampleRate() const { return mStreamInfo.sample_rate; } |
| 55 | unsigned getChannels() const { return mStreamInfo.channels; } |
| 56 | unsigned getBitsPerSample() const { return mStreamInfo.bits_per_sample; } |
| 57 | FLAC__uint64 getTotalSamples() const { return mStreamInfo.total_samples; } |
| 58 |
| 59 | const FLAC__StreamMetadata_StreamInfo& getStreamInfo() const { |
| 60 | return mStreamInfo; |
| 61 | } |
| 62 |
| 63 | bool areVorbisCommentsValid() const { return mVorbisCommentsValid; } |
| 64 |
| 65 | std::vector<std::string> getVorbisComments() { return mVorbisComments; } |
| 66 |
| 67 | bool arePicturesValid() const { return mPicturesValid; } |
| 68 |
| 69 | const std::vector<FlacPicture> &getPictures() const { return mPictures; } |
| 70 |
| 71 | int64_t getLastFrameTimestamp() const { |
| 72 | return (1000000LL * mWriteHeader.number.sample_number) / getSampleRate(); |
| 73 | } |
| 74 |
| 75 | int64_t getLastFrameFirstSampleIndex() const { |
| 76 | return mWriteHeader.number.sample_number; |
| 77 | } |
| 78 |
| 79 | int64_t getNextFrameFirstSampleIndex() const { |
| 80 | return mWriteHeader.number.sample_number + mWriteHeader.blocksize; |
| 81 | } |
| 82 |
| 83 | bool decodeMetadata(); |
| 84 | size_t readBuffer(void *output, size_t output_size); |
| 85 |
| 86 | bool getSeekPositions(int64_t timeUs, std::array<int64_t, 4> &result); |
| 87 |
| 88 | void flush() { |
| 89 | reset(mCurrentPos); |
| 90 | } |
| 91 |
| 92 | void reset(int64_t newPosition) { |
| 93 | if (mDecoder != NULL) { |
| 94 | mCurrentPos = newPosition; |
| 95 | mEOF = false; |
| 96 | if (newPosition == 0) { |
| 97 | mStreamInfoValid = false; |
| 98 | mVorbisCommentsValid = false; |
| 99 | mPicturesValid = false; |
| 100 | mVorbisComments.clear(); |
| 101 | mPictures.clear(); |
| 102 | FLAC__stream_decoder_reset(mDecoder); |
| 103 | } else { |
| 104 | FLAC__stream_decoder_flush(mDecoder); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 |
| 109 | int64_t getDecodePosition() { |
| 110 | uint64_t position; |
| 111 | if (mDecoder != NULL |
| 112 | && FLAC__stream_decoder_get_decode_position(mDecoder, &position)) { |
| 113 | return position; |
| 114 | } |
| 115 | return -1; |
| 116 | } |
| 117 |
| 118 | const char *getDecoderStateString() { |
| 119 | return FLAC__stream_decoder_get_resolved_state_string(mDecoder); |
| 120 | } |
| 121 |
| 122 | bool isDecoderAtEndOfStream() const { |
| 123 | return FLAC__stream_decoder_get_state(mDecoder) == |
| 124 | FLAC__STREAM_DECODER_END_OF_STREAM; |
| 125 | } |
| 126 |
| 127 | private: |
| 128 | DataSource *mDataSource; |
| 129 |
| 130 | void (*mCopy)(int8_t *dst, const int *const *src, unsigned bytesPerSample, |
| 131 | unsigned nSamples, unsigned nChannels); |
| 132 |
| 133 | // handle to underlying libFLAC parser |
| 134 | FLAC__StreamDecoder *mDecoder; |
| 135 |
| 136 | // current position within the data source |
| 137 | off64_t mCurrentPos; |
| 138 | bool mEOF; |
| 139 |
| 140 | // cached when the STREAMINFO metadata is parsed by libFLAC |
| 141 | FLAC__StreamMetadata_StreamInfo mStreamInfo; |
| 142 | bool mStreamInfoValid; |
| 143 |
| 144 | const FLAC__StreamMetadata_SeekTable *mSeekTable; |
| 145 | uint64_t firstFrameOffset; |
| 146 |
| 147 | // cached when the VORBIS_COMMENT metadata is parsed by libFLAC |
| 148 | std::vector<std::string> mVorbisComments; |
| 149 | bool mVorbisCommentsValid; |
| 150 |
| 151 | // cached when the PICTURE metadata is parsed by libFLAC |
| 152 | std::vector<FlacPicture> mPictures; |
| 153 | bool mPicturesValid; |
| 154 |
| 155 | // cached when a decoded PCM block is "written" by libFLAC parser |
| 156 | bool mWriteRequested; |
| 157 | bool mWriteCompleted; |
| 158 | FLAC__FrameHeader mWriteHeader; |
| 159 | const FLAC__int32 *const *mWriteBuffer; |
| 160 |
| 161 | // most recent error reported by libFLAC parser |
| 162 | FLAC__StreamDecoderErrorStatus mErrorStatus; |
| 163 |
| 164 | // no copy constructor or assignment |
| 165 | FLACParser(const FLACParser &); |
| 166 | FLACParser &operator=(const FLACParser &); |
| 167 |
| 168 | // FLAC parser callbacks as C++ instance methods |
| 169 | FLAC__StreamDecoderReadStatus readCallback(FLAC__byte buffer[], |
| 170 | size_t *bytes); |
| 171 | FLAC__StreamDecoderSeekStatus seekCallback(FLAC__uint64 absolute_byte_offset); |
| 172 | FLAC__StreamDecoderTellStatus tellCallback( |
| 173 | FLAC__uint64 *absolute_byte_offset); |
| 174 | FLAC__StreamDecoderLengthStatus lengthCallback(FLAC__uint64 *stream_length); |
| 175 | FLAC__bool eofCallback(); |
| 176 | FLAC__StreamDecoderWriteStatus writeCallback( |
| 177 | const FLAC__Frame *frame, const FLAC__int32 *const buffer[]); |
| 178 | void metadataCallback(const FLAC__StreamMetadata *metadata); |
| 179 | void errorCallback(FLAC__StreamDecoderErrorStatus status); |
| 180 |
| 181 | // FLAC parser callbacks as C-callable functions |
| 182 | static FLAC__StreamDecoderReadStatus read_callback( |
| 183 | const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, |
| 184 | void *client_data); |
| 185 | static FLAC__StreamDecoderSeekStatus seek_callback( |
| 186 | const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, |
| 187 | void *client_data); |
| 188 | static FLAC__StreamDecoderTellStatus tell_callback( |
| 189 | const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, |
| 190 | void *client_data); |
| 191 | static FLAC__StreamDecoderLengthStatus length_callback( |
| 192 | const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, |
| 193 | void *client_data); |
| 194 | static FLAC__bool eof_callback(const FLAC__StreamDecoder *decoder, |
| 195 | void *client_data); |
| 196 | static FLAC__StreamDecoderWriteStatus write_callback( |
| 197 | const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, |
| 198 | const FLAC__int32 *const buffer[], void *client_data); |
| 199 | static void metadata_callback(const FLAC__StreamDecoder *decoder, |
| 200 | const FLAC__StreamMetadata *metadata, |
| 201 | void *client_data); |
| 202 | static void error_callback(const FLAC__StreamDecoder *decoder, |
| 203 | FLAC__StreamDecoderErrorStatus status, |
| 204 | void *client_data); |
| 205 | }; |
| 206 |
| 207 | #endif // FLAC_PARSER_H_ |
| 208 |