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