1 /* 2 * Copyright 2017 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_AVC_DEC_H_ 18 #define ANDROID_C2_SOFT_AVC_DEC_H_ 19 20 #include <sys/time.h> 21 22 #include <media/stagefright/foundation/ColorUtils.h> 23 24 #include "MediaH264Decoder.h" 25 #include "GoldfishH264Helper.h" 26 #include <SimpleC2Component.h> 27 #include <atomic> 28 #include <map> 29 30 namespace android { 31 32 #define ALIGN2(x) ((((x) + 1) >> 1) << 1) 33 #define ALIGN8(x) ((((x) + 7) >> 3) << 3) 34 #define ALIGN16(x) ((((x) + 15) >> 4) << 4) 35 #define ALIGN32(x) ((((x) + 31) >> 5) << 5) 36 #define MAX_NUM_CORES 4 37 #define MIN(a, b) (((a) < (b)) ? (a) : (b)) 38 #define GETTIME(a, b) gettimeofday(a, b); 39 #define TIME_DIFF(start, end, diff) \ 40 diff = (((end).tv_sec - (start).tv_sec) * 1000000) + \ 41 ((end).tv_usec - (start).tv_usec); 42 43 class C2GoldfishAvcDec : public SimpleC2Component { 44 public: 45 class IntfImpl; 46 C2GoldfishAvcDec(const char *name, c2_node_id_t id, 47 const std::shared_ptr<IntfImpl> &intfImpl); 48 virtual ~C2GoldfishAvcDec(); 49 50 // From SimpleC2Component 51 c2_status_t onInit() override; 52 c2_status_t onStop() override; 53 void onReset() override; 54 void onRelease() override; 55 c2_status_t onFlush_sm() override; 56 void process(const std::unique_ptr<C2Work> &work, 57 const std::shared_ptr<C2BlockPool> &pool) override; 58 c2_status_t drain(uint32_t drainMode, 59 const std::shared_ptr<C2BlockPool> &pool) override; 60 61 private: 62 std::unique_ptr<MediaH264Decoder> mContext; 63 bool mEnableAndroidNativeBuffers{true}; 64 65 void checkMode(const std::shared_ptr<C2BlockPool> &pool); 66 // status_t createDecoder(); 67 status_t createDecoder(); 68 status_t setParams(size_t stride); 69 status_t initDecoder(); 70 bool setDecodeArgs(C2ReadView *inBuffer, C2GraphicView *outBuffer, 71 size_t inOffset, size_t inSize, uint32_t tsMarker, bool hasPicture); 72 c2_status_t ensureDecoderState(const std::shared_ptr<C2BlockPool> &pool); 73 void finishWork(uint64_t index, const std::unique_ptr<C2Work> &work); 74 status_t setFlushMode(); 75 c2_status_t drainInternal(uint32_t drainMode, 76 const std::shared_ptr<C2BlockPool> &pool, 77 const std::unique_ptr<C2Work> &work); 78 status_t resetDecoder(); 79 void resetPlugin(); 80 void deleteContext(); 81 82 std::shared_ptr<IntfImpl> mIntf; 83 84 void removePts(uint64_t pts); 85 void insertPts(uint32_t work_index, uint64_t pts); 86 uint64_t getWorkIndex(uint64_t pts); 87 88 // there are same pts matching to different work indices 89 // this happen during csd0/csd1 switching 90 std::map<uint64_t, uint64_t> mOldPts2Index; 91 std::map<uint64_t, uint64_t> mPts2Index; 92 std::map<uint64_t, uint64_t> mIndex2Pts; 93 uint64_t mPts {0}; 94 95 // TODO:This is not the right place for this enum. These should 96 // be part of c2-vndk so that they can be accessed by all video plugins 97 // until then, make them feel at home 98 enum { 99 kNotSupported, 100 kPreferBitstream, 101 kPreferContainer, 102 }; 103 104 std::shared_ptr<C2GraphicBlock> mOutBlock; 105 uint8_t *mOutBufferFlush; 106 107 int mHostColorBufferId{-1}; 108 109 void getVuiParams(h264_image_t &img); 110 void copyImageData(h264_image_t &img); 111 112 h264_image_t mImg{}; 113 uint32_t mConsumedBytes{0}; 114 uint8_t *mInPBuffer{nullptr}; 115 uint32_t mInPBufferSize; 116 uint32_t mInTsMarker; 117 118 // size_t mNumCores; 119 // uint32_t mOutputDelay; 120 uint32_t mWidth; 121 uint32_t mHeight; 122 uint32_t mStride; 123 bool mSignalledOutputEos; 124 bool mSignalledError; 125 bool mHeaderDecoded; 126 std::atomic_uint64_t mOutIndex; 127 // Color aspects. These are ISO values and are meant to detect changes in 128 // aspects to avoid converting them to C2 values for each frame 129 struct VuiColorAspects { 130 uint8_t primaries; 131 uint8_t transfer; 132 uint8_t coeffs; 133 uint8_t fullRange; 134 135 // default color aspects VuiColorAspectsVuiColorAspects136 VuiColorAspects() 137 : primaries(2), transfer(2), coeffs(2), fullRange(0) {} 138 139 bool operator==(const VuiColorAspects &o) { 140 return primaries == o.primaries && transfer == o.transfer && 141 coeffs == o.coeffs && fullRange == o.fullRange; 142 } 143 } mBitstreamColorAspects; 144 145 MetaDataColorAspects mSentMetadata = {1, 0, 0, 0}; 146 147 void sendMetadata(); 148 149 // profile 150 struct timeval mTimeStart; 151 struct timeval mTimeEnd; 152 #ifdef FILE_DUMP_ENABLE 153 char mInFile[200]; 154 #endif /* FILE_DUMP_ENABLE */ 155 156 std::vector<uint8_t> mCsd0; 157 std::vector<uint8_t> mCsd1; 158 void decodeHeaderAfterFlush(); 159 160 std::unique_ptr<GoldfishH264Helper> mH264Helper; 161 162 int mId = -1; 163 C2_DO_NOT_COPY(C2GoldfishAvcDec); 164 }; 165 166 } // namespace android 167 168 #endif // ANDROID_C2_SOFT_AVC_DEC_H_ 169