blob: b3f01d544a80e7b3970d2e91e18ec41694c6d74a [file] [log] [blame]
Pawin Vongmasa36653902018-11-15 00:10:25 -08001/*
2 * Copyright (C) 2018 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 ANDROID_C2_SOFT_FLAC_ENC_H_
18#define ANDROID_C2_SOFT_FLAC_ENC_H_
19
20#include <SimpleC2Component.h>
21
22#include "FLAC/stream_encoder.h"
23
24#define FLAC_COMPRESSION_LEVEL_MIN 0
25#define FLAC_COMPRESSION_LEVEL_DEFAULT 5
26#define FLAC_COMPRESSION_LEVEL_MAX 8
27
28#define FLAC_HEADER_SIZE 128
29
30#define MIN(a, b) (((a) < (b)) ? (a) : (b))
31
32namespace android {
33
34class C2SoftFlacEnc : public SimpleC2Component {
35public:
36 class IntfImpl;
37
38 C2SoftFlacEnc(const char *name, c2_node_id_t id, const std::shared_ptr<IntfImpl> &intfImpl);
39 virtual ~C2SoftFlacEnc();
40
41 // From SimpleC2Component
42 c2_status_t onInit() override;
43 c2_status_t onStop() override;
44 void onReset() override;
45 void onRelease() override;
46 c2_status_t onFlush_sm() override;
47 void process(
48 const std::unique_ptr<C2Work> &work,
49 const std::shared_ptr<C2BlockPool> &pool) override;
50 c2_status_t drain(
51 uint32_t drainMode,
52 const std::shared_ptr<C2BlockPool> &pool) override;
53
54private:
55 status_t configureEncoder();
56 static FLAC__StreamEncoderWriteStatus flacEncoderWriteCallback(
57 const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[],
58 size_t bytes, unsigned samples, unsigned current_frame,
59 void *client_data);
60 FLAC__StreamEncoderWriteStatus onEncodedFlacAvailable(
61 const FLAC__byte buffer[], size_t bytes, unsigned samples,
62 unsigned current_frame);
63
64 std::shared_ptr<IntfImpl> mIntf;
65 const unsigned int kInBlockSize = 1152;
66 const unsigned int kMaxNumChannels = 2;
67 FLAC__StreamEncoder* mFlacStreamEncoder;
68 FLAC__int32* mInputBufferPcm32;
69 std::shared_ptr<C2LinearBlock> mOutputBlock;
70 bool mSignalledError;
71 bool mSignalledOutputEos;
Pawin Vongmasa36653902018-11-15 00:10:25 -080072 uint32_t mBlockSize;
73 bool mIsFirstFrame;
74 uint64_t mAnchorTimeStamp;
75 uint64_t mProcessedSamples;
76 // should the data received by the callback be written to the output port
77 bool mEncoderWriteData;
78 size_t mEncoderReturnedNbBytes;
79 unsigned mHeaderOffset;
80 bool mWroteHeader;
81 char mHeader[FLAC_HEADER_SIZE];
82
83 C2_DO_NOT_COPY(C2SoftFlacEnc);
84};
85
86} // namespace android
87
88#endif // ANDROID_C2_SOFT_FLAC_ENC_H_