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