• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 #include <util/C2InterfaceHelper.h>
22 
23 #include "FLAC/stream_encoder.h"
24 
25 #define FLAC_COMPRESSION_LEVEL_MIN     0
26 #define FLAC_COMPRESSION_LEVEL_DEFAULT 5
27 #define FLAC_COMPRESSION_LEVEL_MAX     8
28 
29 #define FLAC_HEADER_SIZE               128
30 
31 #define MIN(a, b)                      (((a) < (b)) ? (a) : (b))
32 
33 namespace android {
34 
35 class C2SoftFlacEnc : public SimpleC2Component {
36 public:
37     class IntfImpl;
38 
39     C2SoftFlacEnc(const char *name, c2_node_id_t id, const std::shared_ptr<IntfImpl> &intfImpl);
40     C2SoftFlacEnc(const char *name, c2_node_id_t id,
41                   const std::shared_ptr<C2ReflectorHelper> &helper);
42     virtual ~C2SoftFlacEnc();
43 
44     // From SimpleC2Component
45     c2_status_t onInit() override;
46     c2_status_t onStop() override;
47     void onReset() override;
48     void onRelease() override;
49     c2_status_t onFlush_sm() override;
50     void process(
51             const std::unique_ptr<C2Work> &work,
52             const std::shared_ptr<C2BlockPool> &pool) override;
53     c2_status_t drain(
54             uint32_t drainMode,
55             const std::shared_ptr<C2BlockPool> &pool) override;
56 
57 private:
58     status_t configureEncoder();
59     static FLAC__StreamEncoderWriteStatus flacEncoderWriteCallback(
60             const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[],
61             size_t bytes, unsigned samples, unsigned current_frame,
62             void *client_data);
63     FLAC__StreamEncoderWriteStatus onEncodedFlacAvailable(
64             const FLAC__byte buffer[], size_t bytes, unsigned samples,
65             unsigned current_frame);
66 
67     std::shared_ptr<IntfImpl> mIntf;
68     const unsigned int kInBlockSize = 1152;
69     static constexpr unsigned int kMaxNumChannels = 2;
70     static constexpr unsigned int kMaxBlockSize = 4608;
71     FLAC__StreamEncoder* mFlacStreamEncoder;
72     FLAC__int32* mInputBufferPcm32;
73     std::shared_ptr<C2LinearBlock> mOutputBlock;
74     bool mSignalledError;
75     bool mSignalledOutputEos;
76     uint32_t mBlockSize;
77     bool mIsFirstFrame;
78     int64_t mAnchorTimeStamp;
79     uint64_t mProcessedSamples;
80     // should the data received by the callback be written to the output port
81     bool mEncoderWriteData;
82     size_t mEncoderReturnedNbBytes;
83     unsigned mHeaderOffset;
84     bool mWroteHeader;
85     char mHeader[FLAC_HEADER_SIZE];
86     struct OutputBuffer {
87         std::shared_ptr<C2Buffer> buffer;
88         c2_cntr64_t timestampUs;
89         std::uint64_t frameIndex;
90     };
91     std::list<OutputBuffer> mOutputBuffers;
92 
93     C2_DO_NOT_COPY(C2SoftFlacEnc);
94 };
95 
96 }  // namespace android
97 
98 #endif  // ANDROID_C2_SOFT_FLAC_ENC_H_
99