Home | History | Annotate | Download | only in flac
      1 /*
      2  * Copyright (C) 2011 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 //#define LOG_NDEBUG 0
     18 #define LOG_TAG "FLACExtractor"
     19 #include <utils/Log.h>
     20 
     21 #include <stdint.h>
     22 
     23 #include "FLACExtractor.h"
     24 // libFLAC parser
     25 #include "FLAC/stream_decoder.h"
     26 
     27 #include <android/binder_ibinder.h> // for AIBinder_getCallingUid
     28 #include <audio_utils/primitives.h>
     29 #include <media/MediaExtractorPluginApi.h>
     30 #include <media/NdkMediaFormat.h>
     31 #include <media/stagefright/foundation/ABuffer.h>
     32 #include <media/stagefright/foundation/ADebug.h>
     33 #include <media/stagefright/foundation/base64.h>
     34 #include <media/stagefright/MediaBufferGroup.h>
     35 #include <media/stagefright/MediaDefs.h>
     36 #include <media/stagefright/MediaErrors.h>
     37 #include <media/stagefright/MetaData.h>
     38 #include <media/stagefright/MetaDataUtils.h>
     39 #include <private/android_filesystem_config.h> // for AID_MEDIA
     40 #include <system/audio.h>
     41 
     42 namespace android {
     43 
     44 // MediaServer is capable of handling float extractor output, but general processes
     45 // may not be able to do so.
     46 // TODO: Improve API to set extractor float output.
     47 // (Note: duplicated with WAVExtractor.cpp)
     48 static inline bool shouldExtractorOutputFloat(int bitsPerSample)
     49 {
     50     return bitsPerSample > 16 && AIBinder_getCallingUid() == AID_MEDIA;
     51 }
     52 
     53 class FLACParser;
     54 
     55 class FLACSource : public MediaTrackHelper {
     56 
     57 public:
     58     FLACSource(
     59             DataSourceHelper *dataSource,
     60             AMediaFormat *meta,
     61             bool outputFloat);
     62 
     63     virtual media_status_t start();
     64     virtual media_status_t stop();
     65     virtual media_status_t getFormat(AMediaFormat *meta);
     66 
     67     virtual media_status_t read(
     68             MediaBufferHelper **buffer, const ReadOptions *options = NULL);
     69 
     70 protected:
     71     virtual ~FLACSource();
     72 
     73 private:
     74     DataSourceHelper *mDataSource;
     75     AMediaFormat *mTrackMetadata;
     76     const bool mOutputFloat;
     77     FLACParser *mParser;
     78     bool mInitCheck;
     79     bool mStarted;
     80 
     81     // no copy constructor or assignment
     82     FLACSource(const FLACSource &);
     83     FLACSource &operator=(const FLACSource &);
     84 
     85 };
     86 
     87 // FLACParser wraps a C libFLAC parser aka stream decoder
     88 
     89 class FLACParser {
     90 
     91 public:
     92     enum {
     93         kMaxChannels = FCC_8,
     94     };
     95 
     96     explicit FLACParser(
     97         DataSourceHelper *dataSource,
     98         bool outputFloat,
     99         // If metadata pointers aren't provided, we don't fill them
    100         AMediaFormat *fileMetadata = 0,
    101         AMediaFormat *trackMetadata = 0);
    102 
    103     virtual ~FLACParser();
    104 
    105     status_t initCheck() const {
    106         return mInitCheck;
    107     }
    108 
    109     // stream properties
    110     unsigned getMaxBlockSize() const {
    111         return mStreamInfo.max_blocksize;
    112     }
    113     unsigned getSampleRate() const {
    114         return mStreamInfo.sample_rate;
    115     }
    116     unsigned getChannels() const {
    117         return mStreamInfo.channels;
    118     }
    119     unsigned getBitsPerSample() const {
    120         return mStreamInfo.bits_per_sample;
    121     }
    122     FLAC__uint64 getTotalSamples() const {
    123         return mStreamInfo.total_samples;
    124     }
    125 
    126     // media buffers
    127     void allocateBuffers(MediaBufferGroupHelper *group);
    128     void releaseBuffers();
    129     MediaBufferHelper *readBuffer() {
    130         return readBuffer(false, 0LL);
    131     }
    132     MediaBufferHelper *readBuffer(FLAC__uint64 sample) {
    133         return readBuffer(true, sample);
    134     }
    135 
    136 private:
    137     DataSourceHelper *mDataSource;
    138     const bool mOutputFloat;
    139     AMediaFormat *mFileMetadata;
    140     AMediaFormat *mTrackMetadata;
    141     bool mInitCheck;
    142 
    143     // media buffers
    144     size_t mMaxBufferSize;
    145     MediaBufferGroupHelper *mGroup;
    146     void (*mCopy)(int16_t *dst, const int * src[kMaxChannels], unsigned nSamples, unsigned nChannels);
    147 
    148     // handle to underlying libFLAC parser
    149     FLAC__StreamDecoder *mDecoder;
    150 
    151     // current position within the data source
    152     off64_t mCurrentPos;
    153     bool mEOF;
    154 
    155     // cached when the STREAMINFO metadata is parsed by libFLAC
    156     FLAC__StreamMetadata_StreamInfo mStreamInfo;
    157     bool mStreamInfoValid;
    158 
    159     // cached when a decoded PCM block is "written" by libFLAC parser
    160     bool mWriteRequested;
    161     bool mWriteCompleted;
    162     FLAC__FrameHeader mWriteHeader;
    163     FLAC__int32 const * mWriteBuffer[kMaxChannels];
    164 
    165     // most recent error reported by libFLAC parser
    166     FLAC__StreamDecoderErrorStatus mErrorStatus;
    167 
    168     status_t init();
    169     MediaBufferHelper *readBuffer(bool doSeek, FLAC__uint64 sample);
    170 
    171     // no copy constructor or assignment
    172     FLACParser(const FLACParser &);
    173     FLACParser &operator=(const FLACParser &);
    174 
    175     // FLAC parser callbacks as C++ instance methods
    176     FLAC__StreamDecoderReadStatus readCallback(
    177             FLAC__byte buffer[], size_t *bytes);
    178     FLAC__StreamDecoderSeekStatus seekCallback(
    179             FLAC__uint64 absolute_byte_offset);
    180     FLAC__StreamDecoderTellStatus tellCallback(
    181             FLAC__uint64 *absolute_byte_offset);
    182     FLAC__StreamDecoderLengthStatus lengthCallback(
    183             FLAC__uint64 *stream_length);
    184     FLAC__bool eofCallback();
    185     FLAC__StreamDecoderWriteStatus writeCallback(
    186             const FLAC__Frame *frame, const FLAC__int32 * const buffer[]);
    187     void metadataCallback(const FLAC__StreamMetadata *metadata);
    188     void errorCallback(FLAC__StreamDecoderErrorStatus status);
    189     size_t getOutputSampleSize() const { return mOutputFloat ? sizeof(float) : sizeof(int16_t); }
    190 
    191     // FLAC parser callbacks as C-callable functions
    192     static FLAC__StreamDecoderReadStatus read_callback(
    193             const FLAC__StreamDecoder *decoder,
    194             FLAC__byte buffer[], size_t *bytes,
    195             void *client_data);
    196     static FLAC__StreamDecoderSeekStatus seek_callback(
    197             const FLAC__StreamDecoder *decoder,
    198             FLAC__uint64 absolute_byte_offset,
    199             void *client_data);
    200     static FLAC__StreamDecoderTellStatus tell_callback(
    201             const FLAC__StreamDecoder *decoder,
    202             FLAC__uint64 *absolute_byte_offset,
    203             void *client_data);
    204     static FLAC__StreamDecoderLengthStatus length_callback(
    205             const FLAC__StreamDecoder *decoder,
    206             FLAC__uint64 *stream_length,
    207             void *client_data);
    208     static FLAC__bool eof_callback(
    209             const FLAC__StreamDecoder *decoder,
    210             void *client_data);
    211     static FLAC__StreamDecoderWriteStatus write_callback(
    212             const FLAC__StreamDecoder *decoder,
    213             const FLAC__Frame *frame, const FLAC__int32 * const buffer[],
    214             void *client_data);
    215     static void metadata_callback(
    216             const FLAC__StreamDecoder *decoder,
    217             const FLAC__StreamMetadata *metadata,
    218             void *client_data);
    219     static void error_callback(
    220             const FLAC__StreamDecoder *decoder,
    221             FLAC__StreamDecoderErrorStatus status,
    222             void *client_data);
    223 
    224 };
    225 
    226 // The FLAC parser calls our C++ static callbacks using C calling conventions,
    227 // inside FLAC__stream_decoder_process_until_end_of_metadata
    228 // and FLAC__stream_decoder_process_single.
    229 // We immediately then call our corresponding C++ instance methods
    230 // with the same parameter list, but discard redundant information.
    231 
    232 FLAC__StreamDecoderReadStatus FLACParser::read_callback(
    233         const FLAC__StreamDecoder * /* decoder */, FLAC__byte buffer[],
    234         size_t *bytes, void *client_data)
    235 {
    236     return ((FLACParser *) client_data)->readCallback(buffer, bytes);
    237 }
    238 
    239 FLAC__StreamDecoderSeekStatus FLACParser::seek_callback(
    240         const FLAC__StreamDecoder * /* decoder */,
    241         FLAC__uint64 absolute_byte_offset, void *client_data)
    242 {
    243     return ((FLACParser *) client_data)->seekCallback(absolute_byte_offset);
    244 }
    245 
    246 FLAC__StreamDecoderTellStatus FLACParser::tell_callback(
    247         const FLAC__StreamDecoder * /* decoder */,
    248         FLAC__uint64 *absolute_byte_offset, void *client_data)
    249 {
    250     return ((FLACParser *) client_data)->tellCallback(absolute_byte_offset);
    251 }
    252 
    253 FLAC__StreamDecoderLengthStatus FLACParser::length_callback(
    254         const FLAC__StreamDecoder * /* decoder */,
    255         FLAC__uint64 *stream_length, void *client_data)
    256 {
    257     return ((FLACParser *) client_data)->lengthCallback(stream_length);
    258 }
    259 
    260 FLAC__bool FLACParser::eof_callback(
    261         const FLAC__StreamDecoder * /* decoder */, void *client_data)
    262 {
    263     return ((FLACParser *) client_data)->eofCallback();
    264 }
    265 
    266 FLAC__StreamDecoderWriteStatus FLACParser::write_callback(
    267         const FLAC__StreamDecoder * /* decoder */, const FLAC__Frame *frame,
    268         const FLAC__int32 * const buffer[], void *client_data)
    269 {
    270     return ((FLACParser *) client_data)->writeCallback(frame, buffer);
    271 }
    272 
    273 void FLACParser::metadata_callback(
    274         const FLAC__StreamDecoder * /* decoder */,
    275         const FLAC__StreamMetadata *metadata, void *client_data)
    276 {
    277     ((FLACParser *) client_data)->metadataCallback(metadata);
    278 }
    279 
    280 void FLACParser::error_callback(
    281         const FLAC__StreamDecoder * /* decoder */,
    282         FLAC__StreamDecoderErrorStatus status, void *client_data)
    283 {
    284     ((FLACParser *) client_data)->errorCallback(status);
    285 }
    286 
    287 // These are the corresponding callbacks with C++ calling conventions
    288 
    289 FLAC__StreamDecoderReadStatus FLACParser::readCallback(
    290         FLAC__byte buffer[], size_t *bytes)
    291 {
    292     size_t requested = *bytes;
    293     ssize_t actual = mDataSource->readAt(mCurrentPos, buffer, requested);
    294     if (0 > actual) {
    295         *bytes = 0;
    296         return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
    297     } else if (0 == actual) {
    298         *bytes = 0;
    299         mEOF = true;
    300         return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
    301     } else {
    302         assert(actual <= requested);
    303         *bytes = actual;
    304         mCurrentPos += actual;
    305         return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
    306     }
    307 }
    308 
    309 FLAC__StreamDecoderSeekStatus FLACParser::seekCallback(
    310         FLAC__uint64 absolute_byte_offset)
    311 {
    312     mCurrentPos = absolute_byte_offset;
    313     mEOF = false;
    314     return FLAC__STREAM_DECODER_SEEK_STATUS_OK;
    315 }
    316 
    317 FLAC__StreamDecoderTellStatus FLACParser::tellCallback(
    318         FLAC__uint64 *absolute_byte_offset)
    319 {
    320     *absolute_byte_offset = mCurrentPos;
    321     return FLAC__STREAM_DECODER_TELL_STATUS_OK;
    322 }
    323 
    324 FLAC__StreamDecoderLengthStatus FLACParser::lengthCallback(
    325         FLAC__uint64 *stream_length)
    326 {
    327     off64_t size;
    328     if (OK == mDataSource->getSize(&size)) {
    329         *stream_length = size;
    330         return FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
    331     } else {
    332         return FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED;
    333     }
    334 }
    335 
    336 FLAC__bool FLACParser::eofCallback()
    337 {
    338     return mEOF;
    339 }
    340 
    341 FLAC__StreamDecoderWriteStatus FLACParser::writeCallback(
    342         const FLAC__Frame *frame, const FLAC__int32 * const buffer[])
    343 {
    344     if (mWriteRequested) {
    345         mWriteRequested = false;
    346         // FLAC parser doesn't free or realloc buffer until next frame or finish
    347         mWriteHeader = frame->header;
    348         memmove(mWriteBuffer, buffer, sizeof(const FLAC__int32 * const) * getChannels());
    349         mWriteCompleted = true;
    350         return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
    351     } else {
    352         ALOGE("FLACParser::writeCallback unexpected");
    353         return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
    354     }
    355 }
    356 
    357 void FLACParser::metadataCallback(const FLAC__StreamMetadata *metadata)
    358 {
    359     switch (metadata->type) {
    360     case FLAC__METADATA_TYPE_STREAMINFO:
    361         if (!mStreamInfoValid) {
    362             mStreamInfo = metadata->data.stream_info;
    363             mStreamInfoValid = true;
    364         } else {
    365             ALOGE("FLACParser::metadataCallback unexpected STREAMINFO");
    366         }
    367         break;
    368     case FLAC__METADATA_TYPE_VORBIS_COMMENT:
    369         {
    370         const FLAC__StreamMetadata_VorbisComment *vc;
    371         vc = &metadata->data.vorbis_comment;
    372         for (FLAC__uint32 i = 0; i < vc->num_comments; ++i) {
    373             FLAC__StreamMetadata_VorbisComment_Entry *vce;
    374             vce = &vc->comments[i];
    375             if (mFileMetadata != 0 && vce->entry != NULL) {
    376                 parseVorbisComment(mFileMetadata, (const char *) vce->entry,
    377                         vce->length);
    378             }
    379         }
    380         }
    381         break;
    382     case FLAC__METADATA_TYPE_PICTURE:
    383         if (mFileMetadata != 0) {
    384             const FLAC__StreamMetadata_Picture *p = &metadata->data.picture;
    385             AMediaFormat_setBuffer(mFileMetadata, AMEDIAFORMAT_KEY_ALBUMART,
    386                    p->data, p->data_length);
    387         }
    388         break;
    389     default:
    390         ALOGW("FLACParser::metadataCallback unexpected type %u", metadata->type);
    391         break;
    392     }
    393 }
    394 
    395 void FLACParser::errorCallback(FLAC__StreamDecoderErrorStatus status)
    396 {
    397     ALOGE("FLACParser::errorCallback status=%d", status);
    398     mErrorStatus = status;
    399 }
    400 
    401 // Copy samples from FLAC native 32-bit non-interleaved to 16-bit signed
    402 // or 32-bit float interleaved.
    403 // TODO: Consider moving to audio_utils.
    404 // These are candidates for optimization if needed.
    405 static void copyTo16Signed(
    406         short *dst,
    407         const int *const *src,
    408         unsigned nSamples,
    409         unsigned nChannels,
    410         unsigned bitsPerSample) {
    411     const int leftShift = 16 - (int)bitsPerSample; // cast to int to prevent unsigned overflow.
    412     if (leftShift >= 0) {
    413         for (unsigned i = 0; i < nSamples; ++i) {
    414             for (unsigned c = 0; c < nChannels; ++c) {
    415                 *dst++ = src[c][i] << leftShift;
    416             }
    417         }
    418     } else {
    419         const int rightShift = -leftShift;
    420         for (unsigned i = 0; i < nSamples; ++i) {
    421             for (unsigned c = 0; c < nChannels; ++c) {
    422                 *dst++ = src[c][i] >> rightShift;
    423             }
    424         }
    425     }
    426 }
    427 
    428 static void copyToFloat(
    429         float *dst,
    430         const int *const *src,
    431         unsigned nSamples,
    432         unsigned nChannels,
    433         unsigned bitsPerSample) {
    434     const unsigned leftShift = 32 - bitsPerSample;
    435     for (unsigned i = 0; i < nSamples; ++i) {
    436         for (unsigned c = 0; c < nChannels; ++c) {
    437             *dst++ = float_from_i32(src[c][i] << leftShift);
    438         }
    439     }
    440 }
    441 
    442 // FLACParser
    443 
    444 FLACParser::FLACParser(
    445         DataSourceHelper *dataSource,
    446         bool outputFloat,
    447         AMediaFormat *fileMetadata,
    448         AMediaFormat *trackMetadata)
    449     : mDataSource(dataSource),
    450       mOutputFloat(outputFloat),
    451       mFileMetadata(fileMetadata),
    452       mTrackMetadata(trackMetadata),
    453       mInitCheck(false),
    454       mMaxBufferSize(0),
    455       mGroup(NULL),
    456       mDecoder(NULL),
    457       mCurrentPos(0LL),
    458       mEOF(false),
    459       mStreamInfoValid(false),
    460       mWriteRequested(false),
    461       mWriteCompleted(false),
    462       mErrorStatus((FLAC__StreamDecoderErrorStatus) -1)
    463 {
    464     ALOGV("FLACParser::FLACParser");
    465     memset(&mStreamInfo, 0, sizeof(mStreamInfo));
    466     memset(&mWriteHeader, 0, sizeof(mWriteHeader));
    467     mInitCheck = init();
    468 }
    469 
    470 FLACParser::~FLACParser()
    471 {
    472     ALOGV("FLACParser::~FLACParser");
    473     if (mDecoder != NULL) {
    474         FLAC__stream_decoder_delete(mDecoder);
    475         mDecoder = NULL;
    476     }
    477 }
    478 
    479 status_t FLACParser::init()
    480 {
    481     // setup libFLAC parser
    482     mDecoder = FLAC__stream_decoder_new();
    483     if (mDecoder == NULL) {
    484         // The new should succeed, since probably all it does is a malloc
    485         // that always succeeds in Android.  But to avoid dependence on the
    486         // libFLAC internals, we check and log here.
    487         ALOGE("new failed");
    488         return NO_INIT;
    489     }
    490     FLAC__stream_decoder_set_md5_checking(mDecoder, false);
    491     FLAC__stream_decoder_set_metadata_ignore_all(mDecoder);
    492     FLAC__stream_decoder_set_metadata_respond(
    493             mDecoder, FLAC__METADATA_TYPE_STREAMINFO);
    494     FLAC__stream_decoder_set_metadata_respond(
    495             mDecoder, FLAC__METADATA_TYPE_PICTURE);
    496     FLAC__stream_decoder_set_metadata_respond(
    497             mDecoder, FLAC__METADATA_TYPE_VORBIS_COMMENT);
    498     FLAC__StreamDecoderInitStatus initStatus;
    499     initStatus = FLAC__stream_decoder_init_stream(
    500             mDecoder,
    501             read_callback, seek_callback, tell_callback,
    502             length_callback, eof_callback, write_callback,
    503             metadata_callback, error_callback, (void *) this);
    504     if (initStatus != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
    505         // A failure here probably indicates a programming error and so is
    506         // unlikely to happen. But we check and log here similarly to above.
    507         ALOGE("init_stream failed %d", initStatus);
    508         return NO_INIT;
    509     }
    510     // parse all metadata
    511     if (!FLAC__stream_decoder_process_until_end_of_metadata(mDecoder)) {
    512         ALOGE("end_of_metadata failed");
    513         return NO_INIT;
    514     }
    515     if (mStreamInfoValid) {
    516         // check channel count
    517         if (getChannels() == 0 || getChannels() > kMaxChannels) {
    518             ALOGE("unsupported channel count %u", getChannels());
    519             return NO_INIT;
    520         }
    521         // check bit depth
    522         switch (getBitsPerSample()) {
    523         case 8:
    524         case 16:
    525         case 24:
    526         case 32: // generally not expected for FLAC
    527             break;
    528         default:
    529             // Note: internally the FLAC extractor supports 2-32 bits.
    530             ALOGE("unsupported bits per sample %u", getBitsPerSample());
    531             return NO_INIT;
    532         }
    533         // check sample rate
    534         // Note: flac supports arbitrary sample rates up to 655350 Hz, but Android
    535         // supports sample rates from 8kHz to 192kHz, so use that as the limit.
    536         if (getSampleRate() < 8000 || getSampleRate() > 192000) {
    537             ALOGE("unsupported sample rate %u", getSampleRate());
    538             return NO_INIT;
    539         }
    540         // populate track metadata
    541         if (mTrackMetadata != 0) {
    542             AMediaFormat_setString(mTrackMetadata,
    543                     AMEDIAFORMAT_KEY_MIME, MEDIA_MIMETYPE_AUDIO_RAW);
    544             AMediaFormat_setInt32(mTrackMetadata,
    545                     AMEDIAFORMAT_KEY_CHANNEL_COUNT, getChannels());
    546             AMediaFormat_setInt32(mTrackMetadata,
    547                     AMEDIAFORMAT_KEY_SAMPLE_RATE, getSampleRate());
    548             AMediaFormat_setInt32(mTrackMetadata,
    549                     AMEDIAFORMAT_KEY_BITS_PER_SAMPLE, getBitsPerSample());
    550             // sample rate is non-zero, so division by zero not possible
    551             AMediaFormat_setInt64(mTrackMetadata,
    552                     AMEDIAFORMAT_KEY_DURATION, (getTotalSamples() * 1000000LL) / getSampleRate());
    553         }
    554     } else {
    555         ALOGE("missing STREAMINFO");
    556         return NO_INIT;
    557     }
    558     if (mFileMetadata != 0) {
    559         AMediaFormat_setString(mFileMetadata,
    560                 AMEDIAFORMAT_KEY_MIME, MEDIA_MIMETYPE_AUDIO_FLAC);
    561     }
    562     return OK;
    563 }
    564 
    565 void FLACParser::allocateBuffers(MediaBufferGroupHelper *group)
    566 {
    567     CHECK(mGroup == NULL);
    568     mGroup = group;
    569     mMaxBufferSize = getMaxBlockSize() * getChannels() * getOutputSampleSize();
    570     AMediaFormat_setInt32(mTrackMetadata, AMEDIAFORMAT_KEY_MAX_INPUT_SIZE, mMaxBufferSize);
    571     mGroup->add_buffer(mMaxBufferSize);
    572 }
    573 
    574 void FLACParser::releaseBuffers()
    575 {
    576 }
    577 
    578 MediaBufferHelper *FLACParser::readBuffer(bool doSeek, FLAC__uint64 sample)
    579 {
    580     mWriteRequested = true;
    581     mWriteCompleted = false;
    582     if (doSeek) {
    583         // We implement the seek callback, so this works without explicit flush
    584         if (!FLAC__stream_decoder_seek_absolute(mDecoder, sample)) {
    585             ALOGE("FLACParser::readBuffer seek to sample %lld failed", (long long)sample);
    586             return NULL;
    587         }
    588         ALOGV("FLACParser::readBuffer seek to sample %lld succeeded", (long long)sample);
    589     } else {
    590         if (!FLAC__stream_decoder_process_single(mDecoder)) {
    591             ALOGE("FLACParser::readBuffer process_single failed");
    592             return NULL;
    593         }
    594     }
    595     if (!mWriteCompleted) {
    596         ALOGV("FLACParser::readBuffer write did not complete");
    597         return NULL;
    598     }
    599     // verify that block header keeps the promises made by STREAMINFO
    600     unsigned blocksize = mWriteHeader.blocksize;
    601     if (blocksize == 0 || blocksize > getMaxBlockSize()) {
    602         ALOGE("FLACParser::readBuffer write invalid blocksize %u", blocksize);
    603         return NULL;
    604     }
    605     if (mWriteHeader.sample_rate != getSampleRate() ||
    606         mWriteHeader.channels != getChannels() ||
    607         mWriteHeader.bits_per_sample != getBitsPerSample()) {
    608         ALOGE("FLACParser::readBuffer write changed parameters mid-stream: %d/%d/%d -> %d/%d/%d",
    609                 getSampleRate(), getChannels(), getBitsPerSample(),
    610                 mWriteHeader.sample_rate, mWriteHeader.channels, mWriteHeader.bits_per_sample);
    611         return NULL;
    612     }
    613     // acquire a media buffer
    614     CHECK(mGroup != NULL);
    615     MediaBufferHelper *buffer;
    616     status_t err = mGroup->acquire_buffer(&buffer);
    617     if (err != OK) {
    618         return NULL;
    619     }
    620     const size_t bufferSize = blocksize * getChannels() * getOutputSampleSize();
    621     CHECK(bufferSize <= mMaxBufferSize);
    622     buffer->set_range(0, bufferSize);
    623     // copy PCM from FLAC write buffer to our media buffer, with interleaving
    624     const unsigned bitsPerSample = getBitsPerSample();
    625     if (mOutputFloat) {
    626         copyToFloat(reinterpret_cast<float*>(buffer->data()),
    627                     mWriteBuffer,
    628                     blocksize,
    629                     getChannels(),
    630                     bitsPerSample);
    631     } else {
    632         copyTo16Signed(reinterpret_cast<short*>(buffer->data()),
    633                        mWriteBuffer,
    634                        blocksize,
    635                        getChannels(),
    636                        bitsPerSample);
    637     }
    638     // fill in buffer metadata
    639     CHECK(mWriteHeader.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
    640     FLAC__uint64 sampleNumber = mWriteHeader.number.sample_number;
    641     int64_t timeUs = (1000000LL * sampleNumber) / getSampleRate();
    642     AMediaFormat *meta = buffer->meta_data();
    643     AMediaFormat_setInt64(meta, AMEDIAFORMAT_KEY_TIME_US, timeUs);
    644     AMediaFormat_setInt32(meta, AMEDIAFORMAT_KEY_IS_SYNC_FRAME, 1);
    645     return buffer;
    646 }
    647 
    648 // FLACsource
    649 
    650 FLACSource::FLACSource(
    651         DataSourceHelper *dataSource,
    652         AMediaFormat *trackMetadata,
    653         bool outputFloat)
    654     : mDataSource(dataSource),
    655       mTrackMetadata(trackMetadata),
    656       mOutputFloat(outputFloat),
    657       mParser(new FLACParser(mDataSource, outputFloat, 0, mTrackMetadata)),
    658       mInitCheck(mParser->initCheck()),
    659       mStarted(false)
    660 {
    661     ALOGV("FLACSource::FLACSource");
    662 }
    663 
    664 FLACSource::~FLACSource()
    665 {
    666     ALOGV("~FLACSource::FLACSource");
    667     if (mStarted) {
    668         stop();
    669     }
    670     delete mParser;
    671 }
    672 
    673 media_status_t FLACSource::start()
    674 {
    675     ALOGV("FLACSource::start");
    676 
    677     CHECK(!mStarted);
    678     mParser->allocateBuffers(mBufferGroup);
    679     mStarted = true;
    680 
    681     return AMEDIA_OK;
    682 }
    683 
    684 media_status_t FLACSource::stop()
    685 {
    686     ALOGV("FLACSource::stop");
    687 
    688     CHECK(mStarted);
    689     mParser->releaseBuffers();
    690     mStarted = false;
    691 
    692     return AMEDIA_OK;
    693 }
    694 
    695 media_status_t FLACSource::getFormat(AMediaFormat *meta)
    696 {
    697     const media_status_t status = AMediaFormat_copy(meta, mTrackMetadata);
    698     if (status == OK) {
    699         AMediaFormat_setInt32(meta, AMEDIAFORMAT_KEY_PCM_ENCODING,
    700                 mOutputFloat ? kAudioEncodingPcmFloat : kAudioEncodingPcm16bit);
    701     }
    702     return status;
    703 }
    704 
    705 media_status_t FLACSource::read(
    706         MediaBufferHelper **outBuffer, const ReadOptions *options)
    707 {
    708     MediaBufferHelper *buffer;
    709     // process an optional seek request
    710     int64_t seekTimeUs;
    711     ReadOptions::SeekMode mode;
    712     if ((NULL != options) && options->getSeekTo(&seekTimeUs, &mode)) {
    713         FLAC__uint64 sample;
    714         if (seekTimeUs <= 0LL) {
    715             sample = 0LL;
    716         } else {
    717             // sample and total samples are both zero-based, and seek to EOF ok
    718             sample = (seekTimeUs * mParser->getSampleRate()) / 1000000LL;
    719             if (sample >= mParser->getTotalSamples()) {
    720                 sample = mParser->getTotalSamples();
    721             }
    722         }
    723         buffer = mParser->readBuffer(sample);
    724     // otherwise read sequentially
    725     } else {
    726         buffer = mParser->readBuffer();
    727     }
    728     *outBuffer = buffer;
    729     return buffer != NULL ? AMEDIA_OK : AMEDIA_ERROR_END_OF_STREAM;
    730 }
    731 
    732 // FLACExtractor
    733 
    734 FLACExtractor::FLACExtractor(
    735         DataSourceHelper *dataSource)
    736     : mDataSource(dataSource),
    737       mParser(nullptr),
    738       mInitCheck(false)
    739 {
    740     ALOGV("FLACExtractor::FLACExtractor");
    741     // FLACParser will fill in the metadata for us
    742     mFileMetadata = AMediaFormat_new();
    743     mTrackMetadata = AMediaFormat_new();
    744     mParser = new FLACParser(mDataSource, false /* outputFloat */, mFileMetadata, mTrackMetadata);
    745     mInitCheck = mParser->initCheck();
    746 }
    747 
    748 FLACExtractor::~FLACExtractor()
    749 {
    750     ALOGV("~FLACExtractor::FLACExtractor");
    751     delete mParser;
    752     delete mDataSource;
    753     AMediaFormat_delete(mFileMetadata);
    754     AMediaFormat_delete(mTrackMetadata);
    755 }
    756 
    757 size_t FLACExtractor::countTracks()
    758 {
    759     return mInitCheck == OK ? 1 : 0;
    760 }
    761 
    762 MediaTrackHelper *FLACExtractor::getTrack(size_t index)
    763 {
    764     if (mInitCheck != OK || index > 0) {
    765         return NULL;
    766     }
    767 
    768     return new FLACSource(
    769             mDataSource, mTrackMetadata, shouldExtractorOutputFloat(mParser->getBitsPerSample()));
    770 }
    771 
    772 media_status_t FLACExtractor::getTrackMetaData(
    773         AMediaFormat *meta,
    774         size_t index, uint32_t /* flags */) {
    775     if (mInitCheck != OK || index > 0) {
    776         return AMEDIA_ERROR_UNKNOWN;
    777     }
    778     const media_status_t status = AMediaFormat_copy(meta, mTrackMetadata);
    779     if (status == OK) {
    780         AMediaFormat_setInt32(meta, AMEDIAFORMAT_KEY_PCM_ENCODING,
    781                 shouldExtractorOutputFloat(mParser->getBitsPerSample())
    782                         ? kAudioEncodingPcmFloat : kAudioEncodingPcm16bit);
    783     }
    784     return status;
    785 }
    786 
    787 media_status_t FLACExtractor::getMetaData(AMediaFormat *meta)
    788 {
    789     return AMediaFormat_copy(meta, mFileMetadata);
    790 }
    791 
    792 // Sniffer
    793 
    794 bool SniffFLAC(DataSourceHelper *source, float *confidence)
    795 {
    796     // Skip ID3 tags
    797     off64_t pos = 0;
    798     uint8_t header[10];
    799     for (;;) {
    800         if (source->readAt(pos, header, sizeof(header)) != sizeof(header)) {
    801             return false; // no more file to read.
    802         }
    803 
    804         // check for ID3 tag
    805         if (memcmp("ID3", header, 3) != 0) {
    806             break; // not an ID3 tag.
    807         }
    808 
    809         // skip the ID3v2 data and check again
    810         const unsigned id3Len = 10 +
    811                 (((header[6] & 0x7f) << 21)
    812                  | ((header[7] & 0x7f) << 14)
    813                  | ((header[8] & 0x7f) << 7)
    814                  | (header[9] & 0x7f));
    815         pos += id3Len;
    816 
    817         ALOGV("skipped ID3 tag of len %u new starting offset is %#016llx",
    818                 id3Len, (long long)pos);
    819     }
    820 
    821     // Check FLAC header.
    822     // https://xiph.org/flac/format.html#stream
    823     //
    824     // Note: content stored big endian.
    825     // byte offset  bit size  content
    826     // 0            32        fLaC
    827     // 4            8         metadata type STREAMINFO (0) (note: OR with 0x80 if last metadata)
    828     // 5            24        size of metadata, for STREAMINFO (0x22).
    829 
    830     if (memcmp("fLaC\x00\x00\x00\x22", header, 8) != 0 &&
    831         memcmp("fLaC\x80\x00\x00\x22", header, 8) != 0) {
    832         return false;
    833     }
    834 
    835     *confidence = 0.5;
    836 
    837     return true;
    838 }
    839 
    840 static const char *extensions[] = {
    841     "flac",
    842     "fl",
    843     NULL
    844 };
    845 
    846 extern "C" {
    847 // This is the only symbol that needs to be exported
    848 __attribute__ ((visibility ("default")))
    849 ExtractorDef GETEXTRACTORDEF() {
    850     return {
    851             EXTRACTORDEF_VERSION,
    852             UUID("1364b048-cc45-4fda-9934-327d0ebf9829"),
    853             1,
    854             "FLAC Extractor",
    855             {
    856                 .v3 = {
    857                     [](
    858                         CDataSource *source,
    859                         float *confidence,
    860                         void **,
    861                         FreeMetaFunc *) -> CreatorFunc {
    862                         DataSourceHelper helper(source);
    863                         if (SniffFLAC(&helper, confidence)) {
    864                             return [](
    865                                     CDataSource *source,
    866                                     void *) -> CMediaExtractor* {
    867                                 return wrap(new FLACExtractor(new DataSourceHelper(source)));};
    868                         }
    869                         return NULL;
    870                     },
    871                     extensions
    872                 }
    873             },
    874      };
    875 }
    876 
    877 } // extern "C"
    878 
    879 }  // namespace android
    880