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        kMaxInputBufferSize = kMaxNumSamplesPerFrame * sizeof(int16_t) * 2,
56        kMaxOutputBufferSize = 65536,    //TODO check if this can be reduced
57    };
58
59    bool mSignalledError;
60
61    OMX_U32 mNumChannels;
62    OMX_U32 mSampleRate;
63    OMX_U32 mCompressionLevel;
64
65    // should the data received by the callback be written to the output port
66    bool        mEncoderWriteData;
67    bool        mEncoderReturnedEncodedData;
68    size_t      mEncoderReturnedNbBytes;
69    OMX_TICKS  mCurrentInputTimeStamp;
70
71    FLAC__StreamEncoder* mFlacStreamEncoder;
72
73    void initPorts();
74
75    OMX_ERRORTYPE configureEncoder();
76
77    // FLAC encoder callbacks
78    // maps to encoderEncodeFlac()
79    static FLAC__StreamEncoderWriteStatus flacEncoderWriteCallback(
80            const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[],
81            size_t bytes, unsigned samples, unsigned current_frame, void *client_data);
82
83    FLAC__StreamEncoderWriteStatus onEncodedFlacAvailable(
84                const FLAC__byte buffer[],
85                size_t bytes, unsigned samples, unsigned current_frame);
86
87    // FLAC takes samples aligned on 32bit boundaries, use this buffer for the conversion
88    // before passing the input data to the encoder
89    FLAC__int32* mInputBufferPcm32;
90
91#ifdef WRITE_FLAC_HEADER_IN_FIRST_BUFFER
92    unsigned mHeaderOffset;
93    bool mWroteHeader;
94    char mHeader[128];
95#endif
96
97    DISALLOW_EVIL_CONSTRUCTORS(SoftFlacEncoder);
98};
99
100}  // namespace android
101
102#endif  // SOFT_FLAC_ENC_H_
103
104