1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef SOFT_FLAC_ENC_H_
18
19#define SOFT_FLAC_ENC_H_
20
21#include "SimpleSoftOMXComponent.h"
22
23#include "FLAC/stream_encoder.h"
24
25// use this symbol to have the first output buffer start with FLAC frame header so a dump of
26// all the output buffers can be opened as a .flac file
27//#define WRITE_FLAC_HEADER_IN_FIRST_BUFFER
28
29namespace android {
30
31struct SoftFlacEncoder : public SimpleSoftOMXComponent {
32    SoftFlacEncoder(const char *name,
33            const OMX_CALLBACKTYPE *callbacks,
34            OMX_PTR appData,
35            OMX_COMPONENTTYPE **component);
36
37    virtual OMX_ERRORTYPE initCheck() const;
38
39protected:
40    virtual ~SoftFlacEncoder();
41
42    virtual OMX_ERRORTYPE internalGetParameter(
43            OMX_INDEXTYPE index, OMX_PTR params);
44
45    virtual OMX_ERRORTYPE internalSetParameter(
46            OMX_INDEXTYPE index, const OMX_PTR params);
47
48    virtual void onQueueFilled(OMX_U32 portIndex);
49
50private:
51
52    enum {
53        kNumBuffers = 2,
54        kMaxNumSamplesPerFrame = 1152,
55        kMaxOutputBufferSize = 65536,    //TODO check if this can be reduced
56    };
57
58    bool mSignalledError;
59
60    OMX_U32 mNumChannels;
61    OMX_U32 mSampleRate;
62    OMX_U32 mCompressionLevel;
63
64    // should the data received by the callback be written to the output port
65    bool        mEncoderWriteData;
66    bool        mEncoderReturnedEncodedData;
67    size_t      mEncoderReturnedNbBytes;
68    OMX_TICKS  mCurrentInputTimeStamp;
69
70    FLAC__StreamEncoder* mFlacStreamEncoder;
71
72    void initPorts();
73
74    OMX_ERRORTYPE configureEncoder();
75
76    // FLAC encoder callbacks
77    // maps to encoderEncodeFlac()
78    static FLAC__StreamEncoderWriteStatus flacEncoderWriteCallback(
79            const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[],
80            size_t bytes, unsigned samples, unsigned current_frame, void *client_data);
81
82    FLAC__StreamEncoderWriteStatus onEncodedFlacAvailable(
83                const FLAC__byte buffer[],
84                size_t bytes, unsigned samples, unsigned current_frame);
85
86    // FLAC takes samples aligned on 32bit boundaries, use this buffer for the conversion
87    // before passing the input data to the encoder
88    FLAC__int32* mInputBufferPcm32;
89
90#ifdef WRITE_FLAC_HEADER_IN_FIRST_BUFFER
91    unsigned mHeaderOffset;
92    bool mWroteHeader;
93    char mHeader[128];
94#endif
95
96    DISALLOW_EVIL_CONSTRUCTORS(SoftFlacEncoder);
97};
98
99}  // namespace android
100
101#endif  // SOFT_FLAC_ENC_H_
102
103