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