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 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 32 namespace android { 33 34 class C2SoftFlacEnc : public SimpleC2Component { 35 public: 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 54 private: 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; 72 uint32_t mCompressionLevel; 73 uint32_t mBlockSize; 74 bool mIsFirstFrame; 75 uint64_t mAnchorTimeStamp; 76 uint64_t mProcessedSamples; 77 // should the data received by the callback be written to the output port 78 bool mEncoderWriteData; 79 size_t mEncoderReturnedNbBytes; 80 unsigned mHeaderOffset; 81 bool mWroteHeader; 82 char mHeader[FLAC_HEADER_SIZE]; 83 84 C2_DO_NOT_COPY(C2SoftFlacEnc); 85 }; 86 87 } // namespace android 88 89 #endif // ANDROID_C2_SOFT_FLAC_ENC_H_ 90