xref: /aosp_15_r20/frameworks/av/media/libstagefright/codecs/flac/enc/SoftFlacEncoder.h (revision ec779b8e0859a360c3d303172224686826e6e0e1)
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 <media/stagefright/omx/SimpleSoftOMXComponent.h>
22 
23 #include "FLAC/stream_encoder.h"
24 
25 namespace android {
26 
27 struct SoftFlacEncoder : public SimpleSoftOMXComponent {
28     SoftFlacEncoder(const char *name,
29             const OMX_CALLBACKTYPE *callbacks,
30             OMX_PTR appData,
31             OMX_COMPONENTTYPE **component);
32 
33     virtual OMX_ERRORTYPE initCheck() const;
34 
35 protected:
36     virtual ~SoftFlacEncoder();
37 
38     virtual OMX_ERRORTYPE internalGetParameter(
39             OMX_INDEXTYPE index, OMX_PTR params);
40 
41     virtual OMX_ERRORTYPE internalSetParameter(
42             OMX_INDEXTYPE index, const OMX_PTR params);
43 
44     virtual void onQueueFilled(OMX_U32 portIndex);
45 
46 private:
47     const unsigned int kNumBuffers = 2;
48     static constexpr unsigned int kMaxChannels = 2;
49     static constexpr unsigned int kNumSamplesPerFrame = 1152;
50     static constexpr unsigned int kMaxInputBufferSize =
51             kNumSamplesPerFrame * kMaxChannels * sizeof(float);
52     const unsigned int kMaxOutputBufferSize = 65536;    //TODO check if this can be reduced
53 
54     bool mSignalledError;
55 
56     OMX_U32 mNumChannels;
57     OMX_U32 mSampleRate;
58     OMX_U32 mCompressionLevel;
59     OMX_NUMERICALDATATYPE mNumericalData = OMX_NumericalDataSigned;
60     OMX_U32 mBitsPerSample = 16;
61 
62     // should the data received by the callback be written to the output port
63     bool        mEncoderWriteData;
64     bool        mEncoderReturnedEncodedData;
65     bool        mSawInputEOS;
66     bool        mSentOutputEOS;
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     unsigned mHeaderOffset;
91     bool mHeaderComplete;
92     bool mWroteHeader;
93     char mHeader[128];
94 
95     DISALLOW_EVIL_CONSTRUCTORS(SoftFlacEncoder);
96 };
97 
98 }  // namespace android
99 
100 #endif  // SOFT_FLAC_ENC_H_
101 
102