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 #include <inttypes.h> 22 23 #include <media/stagefright/foundation/ColorUtils.h> 24 25 #include <atomic> 26 #include <SimpleC2Component.h> 27 28 #include "ih264_typedefs.h" 29 #include "ih264d.h" 30 31 namespace android { 32 33 #define ivdec_api_function ih264d_api_function 34 #define ivdext_create_ip_t ih264d_create_ip_t 35 #define ivdext_create_op_t ih264d_create_op_t 36 #define ivdext_delete_ip_t ih264d_delete_ip_t 37 #define ivdext_delete_op_t ih264d_delete_op_t 38 #define ivdext_ctl_set_num_cores_ip_t ih264d_ctl_set_num_cores_ip_t 39 #define ivdext_ctl_set_num_cores_op_t ih264d_ctl_set_num_cores_op_t 40 #define ivdext_ctl_get_vui_params_ip_t ih264d_ctl_get_vui_params_ip_t 41 #define ivdext_ctl_get_vui_params_op_t ih264d_ctl_get_vui_params_op_t 42 #define ALIGN128(x) ((((x) + 127) >> 7) << 7) 43 #define MAX_NUM_CORES 4 44 #define IVDEXT_CMD_CTL_SET_NUM_CORES \ 45 (IVD_CONTROL_API_COMMAND_TYPE_T)IH264D_CMD_CTL_SET_NUM_CORES 46 #define MIN(a, b) (((a) < (b)) ? (a) : (b)) 47 48 #ifdef FILE_DUMP_ENABLE 49 #define INPUT_DUMP_PATH "/sdcard/clips/avcd_input" 50 #define INPUT_DUMP_EXT "h264" 51 #define GENERATE_FILE_NAMES() { \ 52 nsecs_t now = systemTime(); \ 53 strcpy(mInFile, ""); \ 54 sprintf(mInFile, "%s_%" PRId64 "d.%s", \ 55 INPUT_DUMP_PATH, now, \ 56 INPUT_DUMP_EXT); \ 57 } 58 #define CREATE_DUMP_FILE(m_filename) { \ 59 FILE *fp = fopen(m_filename, "wb"); \ 60 if (fp != NULL) { \ 61 fclose(fp); \ 62 } else { \ 63 ALOGD("Could not open file %s", m_filename); \ 64 } \ 65 } 66 #define DUMP_TO_FILE(m_filename, m_buf, m_size, m_offset)\ 67 { \ 68 FILE *fp = fopen(m_filename, "ab"); \ 69 if (fp != NULL && m_buf != NULL && m_offset == 0) { \ 70 int i; \ 71 i = fwrite(m_buf, 1, m_size, fp); \ 72 ALOGD("fwrite ret %d to write %d", i, m_size); \ 73 if (i != (int) m_size) { \ 74 ALOGD("Error in fwrite, returned %d", i); \ 75 perror("Error in write to file"); \ 76 } \ 77 } else if (fp == NULL) { \ 78 ALOGD("Could not write to file %s", m_filename);\ 79 } \ 80 if (fp) { \ 81 fclose(fp); \ 82 } \ 83 } 84 #else /* FILE_DUMP_ENABLE */ 85 #define INPUT_DUMP_PATH 86 #define INPUT_DUMP_EXT 87 #define OUTPUT_DUMP_PATH 88 #define OUTPUT_DUMP_EXT 89 #define GENERATE_FILE_NAMES() 90 #define CREATE_DUMP_FILE(m_filename) 91 #define DUMP_TO_FILE(m_filename, m_buf, m_size, m_offset) 92 #endif /* FILE_DUMP_ENABLE */ 93 94 95 class C2SoftAvcDec : public SimpleC2Component { 96 public: 97 class IntfImpl; 98 C2SoftAvcDec(const char *name, c2_node_id_t id, const std::shared_ptr<IntfImpl> &intfImpl); 99 virtual ~C2SoftAvcDec(); 100 101 // From SimpleC2Component 102 c2_status_t onInit() override; 103 c2_status_t onStop() override; 104 void onReset() override; 105 void onRelease() override; 106 c2_status_t onFlush_sm() override; 107 void process( 108 const std::unique_ptr<C2Work> &work, 109 const std::shared_ptr<C2BlockPool> &pool) override; 110 c2_status_t drain( 111 uint32_t drainMode, 112 const std::shared_ptr<C2BlockPool> &pool) override; 113 114 private: 115 status_t createDecoder(); 116 status_t setNumCores(); 117 status_t setParams(size_t stride, IVD_VIDEO_DECODE_MODE_T dec_mode); 118 void getVersion(); 119 status_t initDecoder(); 120 bool setDecodeArgs(ivd_video_decode_ip_t *ps_decode_ip, 121 ivd_video_decode_op_t *ps_decode_op, 122 C2ReadView *inBuffer, 123 C2GraphicView *outBuffer, 124 size_t inOffset, 125 size_t inSize, 126 uint32_t tsMarker); 127 bool getVuiParams(); 128 c2_status_t ensureDecoderState(const std::shared_ptr<C2BlockPool> &pool); 129 void finishWork(uint64_t index, const std::unique_ptr<C2Work> &work); 130 status_t setFlushMode(); 131 c2_status_t drainInternal( 132 uint32_t drainMode, 133 const std::shared_ptr<C2BlockPool> &pool, 134 const std::unique_ptr<C2Work> &work); 135 status_t resetDecoder(); 136 void resetPlugin(); 137 status_t deleteDecoder(); 138 139 std::shared_ptr<IntfImpl> mIntf; 140 141 // TODO:This is not the right place for this enum. These should 142 // be part of c2-vndk so that they can be accessed by all video plugins 143 // until then, make them feel at home 144 enum { 145 kNotSupported, 146 kPreferBitstream, 147 kPreferContainer, 148 }; 149 150 iv_obj_t *mDecHandle; 151 std::shared_ptr<C2GraphicBlock> mOutBlock; 152 uint8_t *mOutBufferFlush; 153 154 size_t mNumCores; 155 IV_COLOR_FORMAT_T mIvColorFormat; 156 uint32_t mOutputDelay; 157 uint32_t mWidth; 158 uint32_t mHeight; 159 uint32_t mStride; 160 bool mSignalledOutputEos; 161 bool mSignalledError; 162 bool mHeaderDecoded; 163 std::atomic_uint64_t mOutIndex; 164 // Color aspects. These are ISO values and are meant to detect changes in aspects to avoid 165 // converting them to C2 values for each frame 166 struct VuiColorAspects { 167 uint8_t primaries; 168 uint8_t transfer; 169 uint8_t coeffs; 170 uint8_t fullRange; 171 172 // default color aspects VuiColorAspectsVuiColorAspects173 VuiColorAspects() 174 : primaries(2), transfer(2), coeffs(2), fullRange(0) { } 175 176 bool operator==(const VuiColorAspects &o) { 177 return primaries == o.primaries && transfer == o.transfer && coeffs == o.coeffs 178 && fullRange == o.fullRange; 179 } 180 } mBitstreamColorAspects; 181 182 // profile 183 nsecs_t mTimeStart = 0; 184 nsecs_t mTimeEnd = 0; 185 #ifdef FILE_DUMP_ENABLE 186 char mInFile[200]; 187 #endif /* FILE_DUMP_ENABLE */ 188 189 C2_DO_NOT_COPY(C2SoftAvcDec); 190 }; 191 192 } // namespace android 193 194 #endif // ANDROID_C2_SOFT_AVC_DEC_H_ 195