1/*
2 * Copyright (C) 2017 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 "FLACDecoder"
19#include <utils/Log.h>
20
21#include "FLACDecoder.h"
22
23#include <media/stagefright/foundation/ADebug.h>
24#include <media/stagefright/foundation/hexdump.h>
25#include <media/stagefright/MediaDefs.h>
26#include <media/stagefright/MediaErrors.h>
27#include <media/stagefright/MetaData.h>
28
29namespace android {
30
31// These are the corresponding callbacks with C++ calling conventions
32FLAC__StreamDecoderReadStatus FLACDecoder::readCallback(
33        FLAC__byte buffer[], size_t *bytes) {
34    if (mBuffer == nullptr || mBufferLen == 0) {
35        *bytes = 0;
36        return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
37    }
38
39    size_t actual = *bytes;
40    if (actual > mBufferDataSize - mBufferPos) {
41        actual = mBufferDataSize - mBufferPos;
42    }
43    memcpy(buffer, mBuffer + mBufferPos, actual);
44    mBufferPos += actual;
45    *bytes = actual;
46    return (actual == 0 ? FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM
47                        : FLAC__STREAM_DECODER_READ_STATUS_CONTINUE);
48}
49
50FLAC__StreamDecoderWriteStatus FLACDecoder::writeCallback(
51        const FLAC__Frame *frame, const FLAC__int32 * const buffer[])
52{
53    if (!mWriteRequested) {
54        ALOGE("writeCallback: unexpected");
55        return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
56    }
57
58    mWriteRequested = false;
59    // FLAC decoder doesn't free or realloc buffer until next frame or finish
60    mWriteHeader = frame->header;
61    memmove(mWriteBuffer, buffer, sizeof(const FLAC__int32 * const) * getChannels());
62    mWriteCompleted = true;
63    return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
64}
65
66void FLACDecoder::metadataCallback(const FLAC__StreamMetadata *metadata)
67{
68    switch (metadata->type) {
69        case FLAC__METADATA_TYPE_STREAMINFO:
70        {
71            if (mStreamInfoValid) {
72                ALOGE("metadataCallback: unexpected STREAMINFO");
73            } else {
74                mStreamInfo = metadata->data.stream_info;
75                mStreamInfoValid = true;
76            }
77            break;
78        }
79
80        /* TODO: enable metadata parsing below.
81        case FLAC__METADATA_TYPE_VORBIS_COMMENT:
82        {
83            const FLAC__StreamMetadata_VorbisComment *vc;
84            vc = &metadata->data.vorbis_comment;
85            for (FLAC__uint32 i = 0; i < vc->num_comments; ++i) {
86                FLAC__StreamMetadata_VorbisComment_Entry *vce;
87                vce = &vc->comments[i];
88                if (mFileMetadata != 0 && vce->entry != NULL) {
89                    parseVorbisComment(mFileMetadata, (const char *) vce->entry,
90                            vce->length);
91                }
92            }
93            break;
94        }
95
96        case FLAC__METADATA_TYPE_PICTURE:
97        {
98            if (mFileMetadata != 0) {
99                const FLAC__StreamMetadata_Picture *p = &metadata->data.picture;
100                mFileMetadata->setData(kKeyAlbumArt,
101                        MetaData::TYPE_NONE, p->data, p->data_length);
102                mFileMetadata->setCString(kKeyAlbumArtMIME, p->mime_type);
103            }
104            break;
105        }
106        */
107
108        default:
109            ALOGW("metadataCallback: unexpected type %u", metadata->type);
110            break;
111    }
112}
113
114void FLACDecoder::errorCallback(FLAC__StreamDecoderErrorStatus status)
115{
116    ALOGE("errorCallback: status=%d", status);
117    mErrorStatus = status;
118}
119
120// Copy samples from FLAC native 32-bit non-interleaved to 16-bit interleaved.
121// These are candidates for optimization if needed.
122static void copyMono8(
123        short *dst,
124        const int * src[FLACDecoder::kMaxChannels],
125        unsigned nSamples,
126        unsigned /* nChannels */) {
127    for (unsigned i = 0; i < nSamples; ++i) {
128        *dst++ = src[0][i] << 8;
129    }
130}
131
132static void copyStereo8(
133        short *dst,
134        const int * src[FLACDecoder::kMaxChannels],
135        unsigned nSamples,
136        unsigned /* nChannels */) {
137    for (unsigned i = 0; i < nSamples; ++i) {
138        *dst++ = src[0][i] << 8;
139        *dst++ = src[1][i] << 8;
140    }
141}
142
143static void copyMultiCh8(
144        short *dst,
145        const int * src[FLACDecoder::kMaxChannels],
146        unsigned nSamples,
147        unsigned nChannels) {
148    for (unsigned i = 0; i < nSamples; ++i) {
149        for (unsigned c = 0; c < nChannels; ++c) {
150            *dst++ = src[c][i] << 8;
151        }
152    }
153}
154
155static void copyMono16(
156        short *dst,
157        const int * src[FLACDecoder::kMaxChannels],
158        unsigned nSamples,
159        unsigned /* nChannels */) {
160    for (unsigned i = 0; i < nSamples; ++i) {
161        *dst++ = src[0][i];
162    }
163}
164
165static void copyStereo16(
166        short *dst,
167        const int * src[FLACDecoder::kMaxChannels],
168        unsigned nSamples,
169        unsigned /* nChannels */) {
170    for (unsigned i = 0; i < nSamples; ++i) {
171        *dst++ = src[0][i];
172        *dst++ = src[1][i];
173    }
174}
175
176static void copyMultiCh16(
177        short *dst,
178        const int * src[FLACDecoder::kMaxChannels],
179        unsigned nSamples,
180        unsigned nChannels) {
181    for (unsigned i = 0; i < nSamples; ++i) {
182        for (unsigned c = 0; c < nChannels; ++c) {
183            *dst++ = src[c][i];
184        }
185    }
186}
187
188// TODO: 24-bit versions should do dithering or noise-shaping, here or in AudioFlinger
189static void copyMono24(
190        short *dst,
191        const int * src[FLACDecoder::kMaxChannels],
192        unsigned nSamples,
193        unsigned /* nChannels */) {
194    for (unsigned i = 0; i < nSamples; ++i) {
195        *dst++ = src[0][i] >> 8;
196    }
197}
198
199static void copyStereo24(
200        short *dst,
201        const int * src[FLACDecoder::kMaxChannels],
202        unsigned nSamples,
203        unsigned /* nChannels */) {
204    for (unsigned i = 0; i < nSamples; ++i) {
205        *dst++ = src[0][i] >> 8;
206        *dst++ = src[1][i] >> 8;
207    }
208}
209
210static void copyMultiCh24(
211        short *dst,
212        const int * src[FLACDecoder::kMaxChannels],
213        unsigned nSamples,
214        unsigned nChannels) {
215    for (unsigned i = 0; i < nSamples; ++i) {
216        for (unsigned c = 0; c < nChannels; ++c) {
217            *dst++ = src[c][i] >> 8;
218        }
219    }
220}
221
222// static
223FLACDecoder *FLACDecoder::Create() {
224    FLACDecoder *decoder = new (std::nothrow) FLACDecoder();
225    if (decoder == NULL || decoder->init() != OK) {
226        delete decoder;
227        return NULL;
228    }
229    return decoder;
230}
231
232FLACDecoder::FLACDecoder()
233    : mDecoder(NULL),
234      mBuffer(NULL),
235      mBufferLen(0),
236      mBufferPos(0),
237      mBufferDataSize(0),
238      mStreamInfoValid(false),
239      mWriteRequested(false),
240      mWriteCompleted(false),
241      mErrorStatus((FLAC__StreamDecoderErrorStatus) -1),
242      mCopy(nullptr) {
243    ALOGV("ctor:");
244    memset(&mStreamInfo, 0, sizeof(mStreamInfo));
245    memset(&mWriteHeader, 0, sizeof(mWriteHeader));
246    memset(&mWriteBuffer, 0, sizeof(mWriteBuffer));
247}
248
249FLACDecoder::~FLACDecoder() {
250    ALOGV("dtor:");
251    if (mDecoder != NULL) {
252        FLAC__stream_decoder_delete(mDecoder);
253        mDecoder = NULL;
254    }
255    if (mBuffer != NULL) {
256        free(mBuffer);
257    }
258}
259
260status_t FLACDecoder::init() {
261    ALOGV("init:");
262    // setup libFLAC stream decoder
263    mDecoder = FLAC__stream_decoder_new();
264    if (mDecoder == NULL) {
265        ALOGE("init: failed to create libFLAC stream decoder");
266        return NO_INIT;
267    }
268    FLAC__stream_decoder_set_md5_checking(mDecoder, false);
269    FLAC__stream_decoder_set_metadata_ignore_all(mDecoder);
270    FLAC__stream_decoder_set_metadata_respond(
271            mDecoder, FLAC__METADATA_TYPE_STREAMINFO);
272    /*
273    FLAC__stream_decoder_set_metadata_respond(
274            mDecoder, FLAC__METADATA_TYPE_PICTURE);
275    FLAC__stream_decoder_set_metadata_respond(
276            mDecoder, FLAC__METADATA_TYPE_VORBIS_COMMENT);
277    */
278    static auto read_callback =
279        [] (const FLAC__StreamDecoder * /* decoder */,
280            FLAC__byte buffer[],
281            size_t *bytes,
282            void *client_data) -> FLAC__StreamDecoderReadStatus {
283            return ((FLACDecoder *) client_data)->readCallback(buffer, bytes); };
284
285    static auto write_callback =
286        [] (const FLAC__StreamDecoder * /* decoder */,
287            const FLAC__Frame *frame,
288            const FLAC__int32 * const buffer[],
289            void *client_data) -> FLAC__StreamDecoderWriteStatus {
290            return ((FLACDecoder *) client_data)->writeCallback(frame, buffer); };
291
292    static auto metadata_callback =
293        [] (const FLAC__StreamDecoder * /* decoder */,
294            const FLAC__StreamMetadata *metadata,
295            void *client_data) {
296            ((FLACDecoder *) client_data)->metadataCallback(metadata); };
297
298    static auto error_callback =
299        [] (const FLAC__StreamDecoder * /* decoder */,
300            FLAC__StreamDecoderErrorStatus status,
301            void *client_data) {
302            ((FLACDecoder *) client_data)->errorCallback(status); };
303
304    FLAC__StreamDecoderInitStatus initStatus =
305        FLAC__stream_decoder_init_stream(
306                mDecoder,
307                read_callback,
308                NULL /* seek_callback */,
309                NULL /* tell_callback */,
310                NULL /* length_callback */,
311                NULL /* eof_callback */,
312                write_callback,
313                metadata_callback,
314                error_callback,
315                (void *)this);
316    if (initStatus != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
317        ALOGE("init: init_stream failed, returned %d", initStatus);
318        return NO_INIT;
319    }
320    return OK;
321}
322
323void FLACDecoder::flush() {
324    ALOGV("flush:");
325    mBufferPos = 0;
326    mBufferDataSize = 0;
327    mStreamInfoValid = false;
328    if (!FLAC__stream_decoder_reset(mDecoder)) {
329        ALOGE("flush: failed to reset FLAC stream decoder");
330    }
331}
332
333status_t FLACDecoder::parseMetadata(const uint8_t *inBuffer, size_t inBufferLen) {
334    ALOGV("parseMetadata: input size(%zu)", inBufferLen);
335    //hexdump(inBuffer, inBufferLen);
336
337    if (mStreamInfoValid) {
338        ALOGE("parseMetadata: already have full metadata blocks");
339        return ERROR_MALFORMED;
340    }
341
342    status_t err = addDataToBuffer(inBuffer, inBufferLen);
343    if (err != OK) {
344        ALOGE("parseMetadata: addDataToBuffer returns error %d", err);
345        return err;
346    }
347
348    if (!FLAC__stream_decoder_process_until_end_of_metadata(mDecoder)) {
349        if (!FLAC__stream_decoder_reset(mDecoder)) {
350            ALOGE("parseMetadata: failed to reset FLAC stream decoder");
351            return FAILED_TRANSACTION;
352        }
353        mBufferPos = 0;
354        ALOGV("parseMetadata: do not have full metadata blocks yet");
355        return WOULD_BLOCK;
356    }
357
358    if (!mStreamInfoValid) {
359        ALOGE("parseMetadata: missing STREAMINFO");
360        return ERROR_MALFORMED;
361    }
362
363    // check block size
364    if (getMaxBlockSize() == 0) {
365        ALOGE("wrong max blocksize %u", getMaxBlockSize());
366        mStreamInfoValid = false;
367        return ERROR_MALFORMED;
368    }
369
370    // check channel count
371    if (getChannels() == 0 || getChannels() > kMaxChannels) {
372        ALOGE("unsupported channel count %u", getChannels());
373        mStreamInfoValid = false;
374        return ERROR_MALFORMED;
375    }
376
377    // check bit depth
378    switch (getBitsPerSample()) {
379        case 8:
380        case 16:
381        case 24:
382            break;
383
384        default:
385            ALOGE("parseMetadata: unsupported bits per sample %u", getBitsPerSample());
386            mStreamInfoValid = false;
387            return ERROR_MALFORMED;
388    }
389
390    // configure the appropriate copy function, defaulting to trespass
391    static const struct {
392        unsigned mChannels;
393        unsigned mBitsPerSample;
394        void (*mCopy)(short *dst, const int * src[kMaxChannels],
395                unsigned nSamples, unsigned nChannels);
396    } table[] = {
397        { 1,  8, copyMono8     },
398        { 2,  8, copyStereo8   },
399        { 8,  8, copyMultiCh8  },
400        { 1, 16, copyMono16    },
401        { 2, 16, copyStereo16  },
402        { 8, 16, copyMultiCh16 },
403        { 1, 24, copyMono24    },
404        { 2, 24, copyStereo24  },
405        { 8, 24, copyMultiCh24 },
406    };
407    for (const auto &entry : table) {
408        if (entry.mChannels >= getChannels() &&
409                entry.mBitsPerSample == getBitsPerSample()) {
410            mCopy = entry.mCopy;
411            break;
412        }
413    }
414
415    // Now we have all metadata blocks.
416    mBufferPos = 0;
417    mBufferDataSize = 0;
418
419    return OK;
420}
421
422status_t FLACDecoder::decodeOneFrame(const uint8_t *inBuffer, size_t inBufferLen,
423        short *outBuffer, size_t *outBufferLen) {
424    ALOGV("decodeOneFrame: input size(%zu)", inBufferLen);
425
426    if (!mStreamInfoValid) {
427        ALOGW("decodeOneFrame: no streaminfo metadata block");
428    }
429
430    if (inBufferLen != 0) {
431        status_t err = addDataToBuffer(inBuffer, inBufferLen);
432        if (err != OK) {
433            ALOGW("decodeOneFrame: addDataToBuffer returns error %d", err);
434            return err;
435        }
436    }
437
438    mWriteRequested = true;
439    mWriteCompleted = false;
440    if (!FLAC__stream_decoder_process_single(mDecoder)) {
441        ALOGE("decodeOneFrame: process_single failed");
442        return ERROR_MALFORMED;
443    }
444    if (!mWriteCompleted) {
445        ALOGV("decodeOneFrame: write did not complete");
446        if (outBufferLen) {
447            *outBufferLen = 0;
448        }
449        return OK;
450    }
451
452    // frame header should be consistent with STREAMINFO
453    unsigned blocksize = mWriteHeader.blocksize;
454    if (blocksize == 0 || blocksize > getMaxBlockSize()) {
455        ALOGE("decodeOneFrame: write invalid blocksize %u", blocksize);
456        return ERROR_MALFORMED;
457    }
458    if (mWriteHeader.sample_rate != getSampleRate() ||
459        mWriteHeader.channels != getChannels() ||
460        mWriteHeader.bits_per_sample != getBitsPerSample()) {
461        ALOGE("decodeOneFrame: parameters are changed mid-stream: %d/%d/%d -> %d/%d/%d",
462                getSampleRate(), getChannels(), getBitsPerSample(),
463                mWriteHeader.sample_rate, mWriteHeader.channels, mWriteHeader.bits_per_sample);
464        return ERROR_MALFORMED;
465    }
466    if (mWriteHeader.number_type != FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER) {
467        ALOGE("decodeOneFrame: number type is %d, expected %d",
468                mWriteHeader.number_type, FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
469        return ERROR_MALFORMED;
470    }
471
472    size_t bufferSize = blocksize * getChannels() * sizeof(short);
473    if (bufferSize > *outBufferLen) {
474        ALOGW("decodeOneFrame: output buffer holds only partial frame %zu:%zu",
475                *outBufferLen, bufferSize);
476        blocksize = *outBufferLen / (getChannels() * sizeof(short));
477        bufferSize = blocksize * getChannels() * sizeof(short);
478    }
479
480    if (mCopy == nullptr) {
481        ALOGE("decodeOneFrame: format is not supported: channels(%d), BitsPerSample(%d)",
482                getChannels(), getBitsPerSample());
483        return ERROR_UNSUPPORTED;
484    }
485    // copy PCM from FLAC write buffer to output buffer, with interleaving
486    (*mCopy)(outBuffer, mWriteBuffer, blocksize, getChannels());
487    *outBufferLen = bufferSize;
488    return OK;
489}
490
491status_t FLACDecoder::addDataToBuffer(const uint8_t *inBuffer, size_t inBufferLen) {
492    // mBufferPos should be no larger than mBufferDataSize
493    if (inBufferLen > SIZE_MAX - (mBufferDataSize - mBufferPos)) {
494        ALOGE("addDataToBuffer: input buffer is too large");
495        return ERROR_MALFORMED;
496    }
497
498    if (inBufferLen > mBufferLen - mBufferDataSize) {
499        if (mBufferPos > 0) {
500            memmove(mBuffer, mBuffer + mBufferPos, mBufferDataSize - mBufferPos);
501            mBufferDataSize -= mBufferPos;
502            mBufferPos = 0;
503        }
504        if (inBufferLen > mBufferLen - mBufferDataSize) {
505            mBuffer = (uint8_t*)realloc(mBuffer, mBufferDataSize + inBufferLen);
506            if (mBuffer == nullptr) {
507                mBufferDataSize = 0;
508                mBufferLen = 0;
509                ALOGE("decodeOneFrame: failed to allocate memory for input buffer");
510                return NO_MEMORY;
511            }
512            mBufferLen = mBufferDataSize + inBufferLen;
513        }
514    }
515
516    memcpy(mBuffer + mBufferDataSize, inBuffer, inBufferLen);
517    mBufferDataSize += inBufferLen;
518    return OK;
519}
520
521}  // namespace android
522