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