1 /*
2  * Copyright (C) 2018 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 //#define LOG_NDEBUG 0
18 #define LOG_TAG "C2SoftHevcDec"
19 #include <log/log.h>
20 
21 #include <media/stagefright/foundation/MediaDefs.h>
22 
23 #include <C2Debug.h>
24 #include <C2PlatformSupport.h>
25 #include <Codec2Mapper.h>
26 #include <SimpleC2Interface.h>
27 
28 #include "C2SoftHevcDec.h"
29 
30 namespace android {
31 
32 namespace {
33 
34 constexpr char COMPONENT_NAME[] = "c2.android.hevc.decoder";
35 constexpr uint32_t kDefaultOutputDelay = 8;
36 constexpr uint32_t kMaxOutputDelay = 16;
37 }  // namespace
38 
39 class C2SoftHevcDec::IntfImpl : public SimpleInterface<void>::BaseParams {
40 public:
IntfImpl(const std::shared_ptr<C2ReflectorHelper> & helper)41     explicit IntfImpl(const std::shared_ptr<C2ReflectorHelper> &helper)
42         : SimpleInterface<void>::BaseParams(
43                 helper,
44                 COMPONENT_NAME,
45                 C2Component::KIND_DECODER,
46                 C2Component::DOMAIN_VIDEO,
47                 MEDIA_MIMETYPE_VIDEO_HEVC) {
48         noPrivateBuffers(); // TODO: account for our buffers here
49         noInputReferences();
50         noOutputReferences();
51         noInputLatency();
52         noTimeStretch();
53 
54         // TODO: Proper support for reorder depth.
55         addParameter(
56                 DefineParam(mActualOutputDelay, C2_PARAMKEY_OUTPUT_DELAY)
57                 .withDefault(new C2PortActualDelayTuning::output(kDefaultOutputDelay))
58                 .withFields({C2F(mActualOutputDelay, value).inRange(0, kMaxOutputDelay)})
59                 .withSetter(Setter<decltype(*mActualOutputDelay)>::StrictValueWithNoDeps)
60                 .build());
61 
62         addParameter(
63                 DefineParam(mAttrib, C2_PARAMKEY_COMPONENT_ATTRIBUTES)
64                 .withConstValue(new C2ComponentAttributesSetting(C2Component::ATTRIB_IS_TEMPORAL))
65                 .build());
66 
67         addParameter(
68                 DefineParam(mSize, C2_PARAMKEY_PICTURE_SIZE)
69                 .withDefault(new C2StreamPictureSizeInfo::output(0u, 320, 240))
70                 .withFields({
71                     C2F(mSize, width).inRange(2, 4096, 2),
72                     C2F(mSize, height).inRange(2, 4096, 2),
73                 })
74                 .withSetter(SizeSetter)
75                 .build());
76 
77         addParameter(
78                 DefineParam(mProfileLevel, C2_PARAMKEY_PROFILE_LEVEL)
79                 .withDefault(new C2StreamProfileLevelInfo::input(0u,
80                         C2Config::PROFILE_HEVC_MAIN, C2Config::LEVEL_HEVC_MAIN_5_1))
81                 .withFields({
82                     C2F(mProfileLevel, profile).oneOf({
83                             C2Config::PROFILE_HEVC_MAIN,
84                             C2Config::PROFILE_HEVC_MAIN_STILL}),
85                     C2F(mProfileLevel, level).oneOf({
86                             C2Config::LEVEL_HEVC_MAIN_1,
87                             C2Config::LEVEL_HEVC_MAIN_2, C2Config::LEVEL_HEVC_MAIN_2_1,
88                             C2Config::LEVEL_HEVC_MAIN_3, C2Config::LEVEL_HEVC_MAIN_3_1,
89                             C2Config::LEVEL_HEVC_MAIN_4, C2Config::LEVEL_HEVC_MAIN_4_1,
90                             C2Config::LEVEL_HEVC_MAIN_5, C2Config::LEVEL_HEVC_MAIN_5_1,
91                             C2Config::LEVEL_HEVC_MAIN_5_2, C2Config::LEVEL_HEVC_HIGH_4,
92                             C2Config::LEVEL_HEVC_HIGH_4_1, C2Config::LEVEL_HEVC_HIGH_5,
93                             C2Config::LEVEL_HEVC_HIGH_5_1, C2Config::LEVEL_HEVC_HIGH_5_2
94                     })
95                 })
96                 .withSetter(ProfileLevelSetter, mSize)
97                 .build());
98 
99         addParameter(
100                 DefineParam(mMaxSize, C2_PARAMKEY_MAX_PICTURE_SIZE)
101                 .withDefault(new C2StreamMaxPictureSizeTuning::output(0u, 320, 240))
102                 .withFields({
103                     C2F(mSize, width).inRange(2, 4096, 2),
104                     C2F(mSize, height).inRange(2, 4096, 2),
105                 })
106                 .withSetter(MaxPictureSizeSetter, mSize)
107                 .build());
108 
109         addParameter(
110                 DefineParam(mMaxInputSize, C2_PARAMKEY_INPUT_MAX_BUFFER_SIZE)
111                 .withDefault(new C2StreamMaxBufferSizeInfo::input(0u, 320 * 240 * 3 / 4))
112                 .withFields({
113                     C2F(mMaxInputSize, value).any(),
114                 })
115                 .calculatedAs(MaxInputSizeSetter, mMaxSize)
116                 .build());
117 
118         C2ChromaOffsetStruct locations[1] = { C2ChromaOffsetStruct::ITU_YUV_420_0() };
119         std::shared_ptr<C2StreamColorInfo::output> defaultColorInfo =
120             C2StreamColorInfo::output::AllocShared(
121                     1u, 0u, 8u /* bitDepth */, C2Color::YUV_420);
122         memcpy(defaultColorInfo->m.locations, locations, sizeof(locations));
123 
124         defaultColorInfo = C2StreamColorInfo::output::AllocShared(
125             {C2ChromaOffsetStruct::ITU_YUV_420_0()}, 0u, 8u /* bitDepth */,
126             C2Color::YUV_420);
127         helper->addStructDescriptors<C2ChromaOffsetStruct>();
128 
129         addParameter(
130                 DefineParam(mColorInfo, C2_PARAMKEY_CODED_COLOR_INFO)
131                 .withConstValue(defaultColorInfo)
132                 .build());
133 
134         addParameter(
135                 DefineParam(mDefaultColorAspects, C2_PARAMKEY_DEFAULT_COLOR_ASPECTS)
136                 .withDefault(new C2StreamColorAspectsTuning::output(
137                         0u, C2Color::RANGE_UNSPECIFIED, C2Color::PRIMARIES_UNSPECIFIED,
138                         C2Color::TRANSFER_UNSPECIFIED, C2Color::MATRIX_UNSPECIFIED))
139                 .withFields({
140                     C2F(mDefaultColorAspects, range).inRange(
141                                 C2Color::RANGE_UNSPECIFIED,     C2Color::RANGE_OTHER),
142                     C2F(mDefaultColorAspects, primaries).inRange(
143                                 C2Color::PRIMARIES_UNSPECIFIED, C2Color::PRIMARIES_OTHER),
144                     C2F(mDefaultColorAspects, transfer).inRange(
145                                 C2Color::TRANSFER_UNSPECIFIED,  C2Color::TRANSFER_OTHER),
146                     C2F(mDefaultColorAspects, matrix).inRange(
147                                 C2Color::MATRIX_UNSPECIFIED,    C2Color::MATRIX_OTHER)
148                 })
149                 .withSetter(DefaultColorAspectsSetter)
150                 .build());
151 
152         addParameter(
153                 DefineParam(mCodedColorAspects, C2_PARAMKEY_VUI_COLOR_ASPECTS)
154                 .withDefault(new C2StreamColorAspectsInfo::input(
155                         0u, C2Color::RANGE_LIMITED, C2Color::PRIMARIES_UNSPECIFIED,
156                         C2Color::TRANSFER_UNSPECIFIED, C2Color::MATRIX_UNSPECIFIED))
157                 .withFields({
158                     C2F(mCodedColorAspects, range).inRange(
159                                 C2Color::RANGE_UNSPECIFIED,     C2Color::RANGE_OTHER),
160                     C2F(mCodedColorAspects, primaries).inRange(
161                                 C2Color::PRIMARIES_UNSPECIFIED, C2Color::PRIMARIES_OTHER),
162                     C2F(mCodedColorAspects, transfer).inRange(
163                                 C2Color::TRANSFER_UNSPECIFIED,  C2Color::TRANSFER_OTHER),
164                     C2F(mCodedColorAspects, matrix).inRange(
165                                 C2Color::MATRIX_UNSPECIFIED,    C2Color::MATRIX_OTHER)
166                 })
167                 .withSetter(CodedColorAspectsSetter)
168                 .build());
169 
170         addParameter(
171                 DefineParam(mColorAspects, C2_PARAMKEY_COLOR_ASPECTS)
172                 .withDefault(new C2StreamColorAspectsInfo::output(
173                         0u, C2Color::RANGE_UNSPECIFIED, C2Color::PRIMARIES_UNSPECIFIED,
174                         C2Color::TRANSFER_UNSPECIFIED, C2Color::MATRIX_UNSPECIFIED))
175                 .withFields({
176                     C2F(mColorAspects, range).inRange(
177                                 C2Color::RANGE_UNSPECIFIED,     C2Color::RANGE_OTHER),
178                     C2F(mColorAspects, primaries).inRange(
179                                 C2Color::PRIMARIES_UNSPECIFIED, C2Color::PRIMARIES_OTHER),
180                     C2F(mColorAspects, transfer).inRange(
181                                 C2Color::TRANSFER_UNSPECIFIED,  C2Color::TRANSFER_OTHER),
182                     C2F(mColorAspects, matrix).inRange(
183                                 C2Color::MATRIX_UNSPECIFIED,    C2Color::MATRIX_OTHER)
184                 })
185                 .withSetter(ColorAspectsSetter, mDefaultColorAspects, mCodedColorAspects)
186                 .build());
187 
188         // TODO: support more formats?
189         addParameter(
190                 DefineParam(mPixelFormat, C2_PARAMKEY_PIXEL_FORMAT)
191                 .withConstValue(new C2StreamPixelFormatInfo::output(
192                                      0u, HAL_PIXEL_FORMAT_YCBCR_420_888))
193                 .build());
194     }
195 
SizeSetter(bool mayBlock,const C2P<C2StreamPictureSizeInfo::output> & oldMe,C2P<C2StreamPictureSizeInfo::output> & me)196     static C2R SizeSetter(bool mayBlock, const C2P<C2StreamPictureSizeInfo::output> &oldMe,
197                           C2P<C2StreamPictureSizeInfo::output> &me) {
198         (void)mayBlock;
199         C2R res = C2R::Ok();
200         if (!me.F(me.v.width).supportsAtAll(me.v.width)) {
201             res = res.plus(C2SettingResultBuilder::BadValue(me.F(me.v.width)));
202             me.set().width = oldMe.v.width;
203         }
204         if (!me.F(me.v.height).supportsAtAll(me.v.height)) {
205             res = res.plus(C2SettingResultBuilder::BadValue(me.F(me.v.height)));
206             me.set().height = oldMe.v.height;
207         }
208         return res;
209     }
210 
MaxPictureSizeSetter(bool mayBlock,C2P<C2StreamMaxPictureSizeTuning::output> & me,const C2P<C2StreamPictureSizeInfo::output> & size)211     static C2R MaxPictureSizeSetter(bool mayBlock, C2P<C2StreamMaxPictureSizeTuning::output> &me,
212                                     const C2P<C2StreamPictureSizeInfo::output> &size) {
213         (void)mayBlock;
214         // TODO: get max width/height from the size's field helpers vs. hardcoding
215         me.set().width = c2_min(c2_max(me.v.width, size.v.width), 4096u);
216         me.set().height = c2_min(c2_max(me.v.height, size.v.height), 4096u);
217         return C2R::Ok();
218     }
219 
MaxInputSizeSetter(bool mayBlock,C2P<C2StreamMaxBufferSizeInfo::input> & me,const C2P<C2StreamMaxPictureSizeTuning::output> & maxSize)220     static C2R MaxInputSizeSetter(bool mayBlock, C2P<C2StreamMaxBufferSizeInfo::input> &me,
221                                   const C2P<C2StreamMaxPictureSizeTuning::output> &maxSize) {
222         (void)mayBlock;
223         // assume compression ratio of 2
224         me.set().value = (((maxSize.v.width + 63) / 64) * ((maxSize.v.height + 63) / 64) * 3072);
225         return C2R::Ok();
226     }
227 
ProfileLevelSetter(bool mayBlock,C2P<C2StreamProfileLevelInfo::input> & me,const C2P<C2StreamPictureSizeInfo::output> & size)228     static C2R ProfileLevelSetter(bool mayBlock, C2P<C2StreamProfileLevelInfo::input> &me,
229                                   const C2P<C2StreamPictureSizeInfo::output> &size) {
230         (void)mayBlock;
231         (void)size;
232         (void)me;  // TODO: validate
233         return C2R::Ok();
234     }
235 
DefaultColorAspectsSetter(bool mayBlock,C2P<C2StreamColorAspectsTuning::output> & me)236     static C2R DefaultColorAspectsSetter(bool mayBlock, C2P<C2StreamColorAspectsTuning::output> &me) {
237         (void)mayBlock;
238         if (me.v.range > C2Color::RANGE_OTHER) {
239                 me.set().range = C2Color::RANGE_OTHER;
240         }
241         if (me.v.primaries > C2Color::PRIMARIES_OTHER) {
242                 me.set().primaries = C2Color::PRIMARIES_OTHER;
243         }
244         if (me.v.transfer > C2Color::TRANSFER_OTHER) {
245                 me.set().transfer = C2Color::TRANSFER_OTHER;
246         }
247         if (me.v.matrix > C2Color::MATRIX_OTHER) {
248                 me.set().matrix = C2Color::MATRIX_OTHER;
249         }
250         return C2R::Ok();
251     }
252 
CodedColorAspectsSetter(bool mayBlock,C2P<C2StreamColorAspectsInfo::input> & me)253     static C2R CodedColorAspectsSetter(bool mayBlock, C2P<C2StreamColorAspectsInfo::input> &me) {
254         (void)mayBlock;
255         if (me.v.range > C2Color::RANGE_OTHER) {
256                 me.set().range = C2Color::RANGE_OTHER;
257         }
258         if (me.v.primaries > C2Color::PRIMARIES_OTHER) {
259                 me.set().primaries = C2Color::PRIMARIES_OTHER;
260         }
261         if (me.v.transfer > C2Color::TRANSFER_OTHER) {
262                 me.set().transfer = C2Color::TRANSFER_OTHER;
263         }
264         if (me.v.matrix > C2Color::MATRIX_OTHER) {
265                 me.set().matrix = C2Color::MATRIX_OTHER;
266         }
267         return C2R::Ok();
268     }
269 
ColorAspectsSetter(bool mayBlock,C2P<C2StreamColorAspectsInfo::output> & me,const C2P<C2StreamColorAspectsTuning::output> & def,const C2P<C2StreamColorAspectsInfo::input> & coded)270     static C2R ColorAspectsSetter(bool mayBlock, C2P<C2StreamColorAspectsInfo::output> &me,
271                                   const C2P<C2StreamColorAspectsTuning::output> &def,
272                                   const C2P<C2StreamColorAspectsInfo::input> &coded) {
273         (void)mayBlock;
274         // take default values for all unspecified fields, and coded values for specified ones
275         me.set().range = coded.v.range == RANGE_UNSPECIFIED ? def.v.range : coded.v.range;
276         me.set().primaries = coded.v.primaries == PRIMARIES_UNSPECIFIED
277                 ? def.v.primaries : coded.v.primaries;
278         me.set().transfer = coded.v.transfer == TRANSFER_UNSPECIFIED
279                 ? def.v.transfer : coded.v.transfer;
280         me.set().matrix = coded.v.matrix == MATRIX_UNSPECIFIED ? def.v.matrix : coded.v.matrix;
281         return C2R::Ok();
282     }
283 
getColorAspects_l()284     std::shared_ptr<C2StreamColorAspectsInfo::output> getColorAspects_l() {
285         return mColorAspects;
286     }
287 
288 private:
289     std::shared_ptr<C2StreamProfileLevelInfo::input> mProfileLevel;
290     std::shared_ptr<C2StreamPictureSizeInfo::output> mSize;
291     std::shared_ptr<C2StreamMaxPictureSizeTuning::output> mMaxSize;
292     std::shared_ptr<C2StreamMaxBufferSizeInfo::input> mMaxInputSize;
293     std::shared_ptr<C2StreamColorInfo::output> mColorInfo;
294     std::shared_ptr<C2StreamColorAspectsInfo::input> mCodedColorAspects;
295     std::shared_ptr<C2StreamColorAspectsTuning::output> mDefaultColorAspects;
296     std::shared_ptr<C2StreamColorAspectsInfo::output> mColorAspects;
297     std::shared_ptr<C2StreamPixelFormatInfo::output> mPixelFormat;
298 };
299 
getCpuCoreCount()300 static size_t getCpuCoreCount() {
301     long cpuCoreCount = 1;
302 #if defined(_SC_NPROCESSORS_ONLN)
303     cpuCoreCount = sysconf(_SC_NPROCESSORS_ONLN);
304 #else
305     // _SC_NPROC_ONLN must be defined...
306     cpuCoreCount = sysconf(_SC_NPROC_ONLN);
307 #endif
308     CHECK(cpuCoreCount >= 1);
309     ALOGV("Number of CPU cores: %ld", cpuCoreCount);
310     return (size_t)cpuCoreCount;
311 }
312 
ivd_aligned_malloc(void * ctxt,WORD32 alignment,WORD32 size)313 static void *ivd_aligned_malloc(void *ctxt, WORD32 alignment, WORD32 size) {
314     (void) ctxt;
315     return memalign(alignment, size);
316 }
317 
ivd_aligned_free(void * ctxt,void * mem)318 static void ivd_aligned_free(void *ctxt, void *mem) {
319     (void) ctxt;
320     free(mem);
321 }
322 
C2SoftHevcDec(const char * name,c2_node_id_t id,const std::shared_ptr<IntfImpl> & intfImpl)323 C2SoftHevcDec::C2SoftHevcDec(
324         const char *name,
325         c2_node_id_t id,
326         const std::shared_ptr<IntfImpl> &intfImpl)
327     : SimpleC2Component(std::make_shared<SimpleInterface<IntfImpl>>(name, id, intfImpl)),
328         mIntf(intfImpl),
329         mDecHandle(nullptr),
330         mOutBufferFlush(nullptr),
331         mIvColorformat(IV_YUV_420P),
332         mOutputDelay(kDefaultOutputDelay),
333         mWidth(320),
334         mHeight(240),
335         mHeaderDecoded(false),
336         mOutIndex(0u) {
337 }
338 
~C2SoftHevcDec()339 C2SoftHevcDec::~C2SoftHevcDec() {
340     onRelease();
341 }
342 
onInit()343 c2_status_t C2SoftHevcDec::onInit() {
344     status_t err = initDecoder();
345     return err == OK ? C2_OK : C2_CORRUPTED;
346 }
347 
onStop()348 c2_status_t C2SoftHevcDec::onStop() {
349     if (OK != resetDecoder()) return C2_CORRUPTED;
350     resetPlugin();
351     return C2_OK;
352 }
353 
onReset()354 void C2SoftHevcDec::onReset() {
355     (void) onStop();
356 }
357 
onRelease()358 void C2SoftHevcDec::onRelease() {
359     (void) deleteDecoder();
360     if (mOutBufferFlush) {
361         ivd_aligned_free(nullptr, mOutBufferFlush);
362         mOutBufferFlush = nullptr;
363     }
364     if (mOutBlock) {
365         mOutBlock.reset();
366     }
367 }
368 
onFlush_sm()369 c2_status_t C2SoftHevcDec::onFlush_sm() {
370     if (OK != setFlushMode()) return C2_CORRUPTED;
371 
372     uint32_t displayStride = mStride;
373     uint32_t displayHeight = mHeight;
374     uint32_t bufferSize = displayStride * displayHeight * 3 / 2;
375     mOutBufferFlush = (uint8_t *)ivd_aligned_malloc(nullptr, 128, bufferSize);
376     if (!mOutBufferFlush) {
377         ALOGE("could not allocate tmp output buffer (for flush) of size %u ", bufferSize);
378         return C2_NO_MEMORY;
379     }
380 
381     while (true) {
382         ihevcd_cxa_video_decode_ip_t s_hevcd_decode_ip = {};
383         ihevcd_cxa_video_decode_op_t s_hevcd_decode_op = {};
384         ivd_video_decode_ip_t *ps_decode_ip = &s_hevcd_decode_ip.s_ivd_video_decode_ip_t;
385         ivd_video_decode_op_t *ps_decode_op = &s_hevcd_decode_op.s_ivd_video_decode_op_t;
386 
387         setDecodeArgs(ps_decode_ip, ps_decode_op, nullptr, nullptr, 0, 0, 0);
388         (void) ivdec_api_function(mDecHandle, ps_decode_ip, ps_decode_op);
389         if (0 == ps_decode_op->u4_output_present) {
390             resetPlugin();
391             break;
392         }
393     }
394 
395     if (mOutBufferFlush) {
396         ivd_aligned_free(nullptr, mOutBufferFlush);
397         mOutBufferFlush = nullptr;
398     }
399 
400     return C2_OK;
401 }
402 
createDecoder()403 status_t C2SoftHevcDec::createDecoder() {
404     ivdext_create_ip_t s_create_ip = {};
405     ivdext_create_op_t s_create_op = {};
406 
407     s_create_ip.s_ivd_create_ip_t.u4_size = sizeof(ivdext_create_ip_t);
408     s_create_ip.s_ivd_create_ip_t.e_cmd = IVD_CMD_CREATE;
409     s_create_ip.s_ivd_create_ip_t.u4_share_disp_buf = 0;
410     s_create_ip.s_ivd_create_ip_t.e_output_format = mIvColorformat;
411     s_create_ip.s_ivd_create_ip_t.pf_aligned_alloc = ivd_aligned_malloc;
412     s_create_ip.s_ivd_create_ip_t.pf_aligned_free = ivd_aligned_free;
413     s_create_ip.s_ivd_create_ip_t.pv_mem_ctxt = nullptr;
414     s_create_op.s_ivd_create_op_t.u4_size = sizeof(ivdext_create_op_t);
415     IV_API_CALL_STATUS_T status = ivdec_api_function(mDecHandle,
416                                                      &s_create_ip,
417                                                      &s_create_op);
418     if (status != IV_SUCCESS) {
419         ALOGE("error in %s: 0x%x", __func__,
420               s_create_op.s_ivd_create_op_t.u4_error_code);
421         return UNKNOWN_ERROR;
422     }
423     mDecHandle = (iv_obj_t*)s_create_op.s_ivd_create_op_t.pv_handle;
424     mDecHandle->pv_fxns = (void *)ivdec_api_function;
425     mDecHandle->u4_size = sizeof(iv_obj_t);
426 
427     return OK;
428 }
429 
setNumCores()430 status_t C2SoftHevcDec::setNumCores() {
431     ivdext_ctl_set_num_cores_ip_t s_set_num_cores_ip = {};
432     ivdext_ctl_set_num_cores_op_t s_set_num_cores_op = {};
433 
434     s_set_num_cores_ip.u4_size = sizeof(ivdext_ctl_set_num_cores_ip_t);
435     s_set_num_cores_ip.e_cmd = IVD_CMD_VIDEO_CTL;
436     s_set_num_cores_ip.e_sub_cmd = IVDEXT_CMD_CTL_SET_NUM_CORES;
437     s_set_num_cores_ip.u4_num_cores = mNumCores;
438     s_set_num_cores_op.u4_size = sizeof(ivdext_ctl_set_num_cores_op_t);
439     IV_API_CALL_STATUS_T status = ivdec_api_function(mDecHandle,
440                                                      &s_set_num_cores_ip,
441                                                      &s_set_num_cores_op);
442     if (IV_SUCCESS != status) {
443         ALOGD("error in %s: 0x%x", __func__, s_set_num_cores_op.u4_error_code);
444         return UNKNOWN_ERROR;
445     }
446 
447     return OK;
448 }
449 
setParams(size_t stride,IVD_VIDEO_DECODE_MODE_T dec_mode)450 status_t C2SoftHevcDec::setParams(size_t stride, IVD_VIDEO_DECODE_MODE_T dec_mode) {
451     ihevcd_cxa_ctl_set_config_ip_t s_hevcd_set_dyn_params_ip = {};
452     ihevcd_cxa_ctl_set_config_op_t s_hevcd_set_dyn_params_op = {};
453     ivd_ctl_set_config_ip_t *ps_set_dyn_params_ip =
454         &s_hevcd_set_dyn_params_ip.s_ivd_ctl_set_config_ip_t;
455     ivd_ctl_set_config_op_t *ps_set_dyn_params_op =
456         &s_hevcd_set_dyn_params_op.s_ivd_ctl_set_config_op_t;
457 
458     ps_set_dyn_params_ip->u4_size = sizeof(ihevcd_cxa_ctl_set_config_ip_t);
459     ps_set_dyn_params_ip->e_cmd = IVD_CMD_VIDEO_CTL;
460     ps_set_dyn_params_ip->e_sub_cmd = IVD_CMD_CTL_SETPARAMS;
461     ps_set_dyn_params_ip->u4_disp_wd = (UWORD32) stride;
462     ps_set_dyn_params_ip->e_frm_skip_mode = IVD_SKIP_NONE;
463     ps_set_dyn_params_ip->e_frm_out_mode = IVD_DISPLAY_FRAME_OUT;
464     ps_set_dyn_params_ip->e_vid_dec_mode = dec_mode;
465     ps_set_dyn_params_op->u4_size = sizeof(ihevcd_cxa_ctl_set_config_op_t);
466     IV_API_CALL_STATUS_T status = ivdec_api_function(mDecHandle,
467                                                      ps_set_dyn_params_ip,
468                                                      ps_set_dyn_params_op);
469     if (status != IV_SUCCESS) {
470         ALOGE("error in %s: 0x%x", __func__, ps_set_dyn_params_op->u4_error_code);
471         return UNKNOWN_ERROR;
472     }
473 
474     return OK;
475 }
476 
getVersion()477 status_t C2SoftHevcDec::getVersion() {
478     ivd_ctl_getversioninfo_ip_t s_get_versioninfo_ip = {};
479     ivd_ctl_getversioninfo_op_t s_get_versioninfo_op = {};
480     UWORD8 au1_buf[512];
481 
482     s_get_versioninfo_ip.u4_size = sizeof(ivd_ctl_getversioninfo_ip_t);
483     s_get_versioninfo_ip.e_cmd = IVD_CMD_VIDEO_CTL;
484     s_get_versioninfo_ip.e_sub_cmd = IVD_CMD_CTL_GETVERSION;
485     s_get_versioninfo_ip.pv_version_buffer = au1_buf;
486     s_get_versioninfo_ip.u4_version_buffer_size = sizeof(au1_buf);
487     s_get_versioninfo_op.u4_size = sizeof(ivd_ctl_getversioninfo_op_t);
488     IV_API_CALL_STATUS_T status = ivdec_api_function(mDecHandle,
489                                                      &s_get_versioninfo_ip,
490                                                      &s_get_versioninfo_op);
491     if (status != IV_SUCCESS) {
492         ALOGD("error in %s: 0x%x", __func__,
493               s_get_versioninfo_op.u4_error_code);
494     } else {
495         ALOGV("ittiam decoder version number: %s",
496               (char *) s_get_versioninfo_ip.pv_version_buffer);
497     }
498 
499     return OK;
500 }
501 
initDecoder()502 status_t C2SoftHevcDec::initDecoder() {
503     if (OK != createDecoder()) return UNKNOWN_ERROR;
504     mNumCores = MIN(getCpuCoreCount(), MAX_NUM_CORES);
505     mStride = ALIGN32(mWidth);
506     mSignalledError = false;
507     resetPlugin();
508     (void) setNumCores();
509     if (OK != setParams(mStride, IVD_DECODE_FRAME)) return UNKNOWN_ERROR;
510     (void) getVersion();
511 
512     return OK;
513 }
514 
setDecodeArgs(ivd_video_decode_ip_t * ps_decode_ip,ivd_video_decode_op_t * ps_decode_op,C2ReadView * inBuffer,C2GraphicView * outBuffer,size_t inOffset,size_t inSize,uint32_t tsMarker)515 bool C2SoftHevcDec::setDecodeArgs(ivd_video_decode_ip_t *ps_decode_ip,
516                                   ivd_video_decode_op_t *ps_decode_op,
517                                   C2ReadView *inBuffer,
518                                   C2GraphicView *outBuffer,
519                                   size_t inOffset,
520                                   size_t inSize,
521                                   uint32_t tsMarker) {
522     uint32_t displayStride = mStride;
523     if (outBuffer) {
524         C2PlanarLayout layout;
525         layout = outBuffer->layout();
526         displayStride = layout.planes[C2PlanarLayout::PLANE_Y].rowInc;
527     }
528     uint32_t displayHeight = mHeight;
529     size_t lumaSize = displayStride * displayHeight;
530     size_t chromaSize = lumaSize >> 2;
531 
532     if (mStride != displayStride) {
533         mStride = displayStride;
534         if (OK != setParams(mStride, IVD_DECODE_FRAME)) return false;
535     }
536 
537     ps_decode_ip->u4_size = sizeof(ihevcd_cxa_video_decode_ip_t);
538     ps_decode_ip->e_cmd = IVD_CMD_VIDEO_DECODE;
539     if (inBuffer) {
540         ps_decode_ip->u4_ts = tsMarker;
541         ps_decode_ip->pv_stream_buffer = const_cast<uint8_t *>(inBuffer->data() + inOffset);
542         ps_decode_ip->u4_num_Bytes = inSize;
543     } else {
544         ps_decode_ip->u4_ts = 0;
545         ps_decode_ip->pv_stream_buffer = nullptr;
546         ps_decode_ip->u4_num_Bytes = 0;
547     }
548     ps_decode_ip->s_out_buffer.u4_min_out_buf_size[0] = lumaSize;
549     ps_decode_ip->s_out_buffer.u4_min_out_buf_size[1] = chromaSize;
550     ps_decode_ip->s_out_buffer.u4_min_out_buf_size[2] = chromaSize;
551     if (outBuffer) {
552         if (outBuffer->height() < displayHeight) {
553             ALOGE("Output buffer too small: provided (%dx%d) required (%ux%u)",
554                   outBuffer->width(), outBuffer->height(), displayStride, displayHeight);
555             return false;
556         }
557         ps_decode_ip->s_out_buffer.pu1_bufs[0] = outBuffer->data()[C2PlanarLayout::PLANE_Y];
558         ps_decode_ip->s_out_buffer.pu1_bufs[1] = outBuffer->data()[C2PlanarLayout::PLANE_U];
559         ps_decode_ip->s_out_buffer.pu1_bufs[2] = outBuffer->data()[C2PlanarLayout::PLANE_V];
560     } else {
561         ps_decode_ip->s_out_buffer.pu1_bufs[0] = mOutBufferFlush;
562         ps_decode_ip->s_out_buffer.pu1_bufs[1] = mOutBufferFlush + lumaSize;
563         ps_decode_ip->s_out_buffer.pu1_bufs[2] = mOutBufferFlush + lumaSize + chromaSize;
564     }
565     ps_decode_ip->s_out_buffer.u4_num_bufs = 3;
566     ps_decode_op->u4_size = sizeof(ihevcd_cxa_video_decode_op_t);
567     ps_decode_op->u4_output_present = 0;
568 
569     return true;
570 }
571 
getVuiParams()572 bool C2SoftHevcDec::getVuiParams() {
573     ivdext_ctl_get_vui_params_ip_t s_get_vui_params_ip = {};
574     ivdext_ctl_get_vui_params_op_t s_get_vui_params_op = {};
575 
576     s_get_vui_params_ip.u4_size = sizeof(ivdext_ctl_get_vui_params_ip_t);
577     s_get_vui_params_ip.e_cmd = IVD_CMD_VIDEO_CTL;
578     s_get_vui_params_ip.e_sub_cmd =
579             (IVD_CONTROL_API_COMMAND_TYPE_T) IHEVCD_CXA_CMD_CTL_GET_VUI_PARAMS;
580     s_get_vui_params_op.u4_size = sizeof(ivdext_ctl_get_vui_params_op_t);
581     IV_API_CALL_STATUS_T status = ivdec_api_function(mDecHandle,
582                                                      &s_get_vui_params_ip,
583                                                      &s_get_vui_params_op);
584     if (status != IV_SUCCESS) {
585         ALOGD("error in %s: 0x%x", __func__, s_get_vui_params_op.u4_error_code);
586         return false;
587     }
588 
589     VuiColorAspects vuiColorAspects;
590     vuiColorAspects.primaries = s_get_vui_params_op.u1_colour_primaries;
591     vuiColorAspects.transfer = s_get_vui_params_op.u1_transfer_characteristics;
592     vuiColorAspects.coeffs = s_get_vui_params_op.u1_matrix_coefficients;
593     vuiColorAspects.fullRange = s_get_vui_params_op.u1_video_full_range_flag;
594 
595     // convert vui aspects to C2 values if changed
596     if (!(vuiColorAspects == mBitstreamColorAspects)) {
597         mBitstreamColorAspects = vuiColorAspects;
598         ColorAspects sfAspects;
599         C2StreamColorAspectsInfo::input codedAspects = { 0u };
600         ColorUtils::convertIsoColorAspectsToCodecAspects(
601                 vuiColorAspects.primaries, vuiColorAspects.transfer, vuiColorAspects.coeffs,
602                 vuiColorAspects.fullRange, sfAspects);
603         if (!C2Mapper::map(sfAspects.mPrimaries, &codedAspects.primaries)) {
604             codedAspects.primaries = C2Color::PRIMARIES_UNSPECIFIED;
605         }
606         if (!C2Mapper::map(sfAspects.mRange, &codedAspects.range)) {
607             codedAspects.range = C2Color::RANGE_UNSPECIFIED;
608         }
609         if (!C2Mapper::map(sfAspects.mMatrixCoeffs, &codedAspects.matrix)) {
610             codedAspects.matrix = C2Color::MATRIX_UNSPECIFIED;
611         }
612         if (!C2Mapper::map(sfAspects.mTransfer, &codedAspects.transfer)) {
613             codedAspects.transfer = C2Color::TRANSFER_UNSPECIFIED;
614         }
615         std::vector<std::unique_ptr<C2SettingResult>> failures;
616         (void)mIntf->config({&codedAspects}, C2_MAY_BLOCK, &failures);
617     }
618     return true;
619 }
620 
setFlushMode()621 status_t C2SoftHevcDec::setFlushMode() {
622     ivd_ctl_flush_ip_t s_set_flush_ip = {};
623     ivd_ctl_flush_op_t s_set_flush_op = {};
624 
625     s_set_flush_ip.u4_size = sizeof(ivd_ctl_flush_ip_t);
626     s_set_flush_ip.e_cmd = IVD_CMD_VIDEO_CTL;
627     s_set_flush_ip.e_sub_cmd = IVD_CMD_CTL_FLUSH;
628     s_set_flush_op.u4_size = sizeof(ivd_ctl_flush_op_t);
629     IV_API_CALL_STATUS_T status = ivdec_api_function(mDecHandle,
630                                                      &s_set_flush_ip,
631                                                      &s_set_flush_op);
632     if (status != IV_SUCCESS) {
633         ALOGE("error in %s: 0x%x", __func__, s_set_flush_op.u4_error_code);
634         return UNKNOWN_ERROR;
635     }
636 
637     return OK;
638 }
639 
resetDecoder()640 status_t C2SoftHevcDec::resetDecoder() {
641     ivd_ctl_reset_ip_t s_reset_ip = {};
642     ivd_ctl_reset_op_t s_reset_op = {};
643 
644     s_reset_ip.u4_size = sizeof(ivd_ctl_reset_ip_t);
645     s_reset_ip.e_cmd = IVD_CMD_VIDEO_CTL;
646     s_reset_ip.e_sub_cmd = IVD_CMD_CTL_RESET;
647     s_reset_op.u4_size = sizeof(ivd_ctl_reset_op_t);
648     IV_API_CALL_STATUS_T status = ivdec_api_function(mDecHandle,
649                                                      &s_reset_ip,
650                                                      &s_reset_op);
651     if (IV_SUCCESS != status) {
652         ALOGE("error in %s: 0x%x", __func__, s_reset_op.u4_error_code);
653         return UNKNOWN_ERROR;
654     }
655     mStride = 0;
656     (void) setNumCores();
657     mSignalledError = false;
658     mHeaderDecoded = false;
659     return OK;
660 }
661 
resetPlugin()662 void C2SoftHevcDec::resetPlugin() {
663     mSignalledOutputEos = false;
664     gettimeofday(&mTimeStart, nullptr);
665     gettimeofday(&mTimeEnd, nullptr);
666 }
667 
deleteDecoder()668 status_t C2SoftHevcDec::deleteDecoder() {
669     if (mDecHandle) {
670         ivdext_delete_ip_t s_delete_ip = {};
671         ivdext_delete_op_t s_delete_op = {};
672 
673         s_delete_ip.s_ivd_delete_ip_t.u4_size = sizeof(ivdext_delete_ip_t);
674         s_delete_ip.s_ivd_delete_ip_t.e_cmd = IVD_CMD_DELETE;
675         s_delete_op.s_ivd_delete_op_t.u4_size = sizeof(ivdext_delete_op_t);
676         IV_API_CALL_STATUS_T status = ivdec_api_function(mDecHandle,
677                                                          &s_delete_ip,
678                                                          &s_delete_op);
679         if (status != IV_SUCCESS) {
680             ALOGE("error in %s: 0x%x", __func__,
681                   s_delete_op.s_ivd_delete_op_t.u4_error_code);
682             return UNKNOWN_ERROR;
683         }
684         mDecHandle = nullptr;
685     }
686 
687     return OK;
688 }
689 
fillEmptyWork(const std::unique_ptr<C2Work> & work)690 void fillEmptyWork(const std::unique_ptr<C2Work> &work) {
691     uint32_t flags = 0;
692     if (work->input.flags & C2FrameData::FLAG_END_OF_STREAM) {
693         flags |= C2FrameData::FLAG_END_OF_STREAM;
694         ALOGV("signalling eos");
695     }
696     work->worklets.front()->output.flags = (C2FrameData::flags_t)flags;
697     work->worklets.front()->output.buffers.clear();
698     work->worklets.front()->output.ordinal = work->input.ordinal;
699     work->workletsProcessed = 1u;
700 }
701 
finishWork(uint64_t index,const std::unique_ptr<C2Work> & work)702 void C2SoftHevcDec::finishWork(uint64_t index, const std::unique_ptr<C2Work> &work) {
703     std::shared_ptr<C2Buffer> buffer = createGraphicBuffer(std::move(mOutBlock),
704                                                            C2Rect(mWidth, mHeight));
705     mOutBlock = nullptr;
706     {
707         IntfImpl::Lock lock = mIntf->lock();
708         buffer->setInfo(mIntf->getColorAspects_l());
709     }
710 
711     class FillWork {
712        public:
713         FillWork(uint32_t flags, C2WorkOrdinalStruct ordinal,
714                  const std::shared_ptr<C2Buffer>& buffer)
715             : mFlags(flags), mOrdinal(ordinal), mBuffer(buffer) {}
716         ~FillWork() = default;
717 
718         void operator()(const std::unique_ptr<C2Work>& work) {
719             work->worklets.front()->output.flags = (C2FrameData::flags_t)mFlags;
720             work->worklets.front()->output.buffers.clear();
721             work->worklets.front()->output.ordinal = mOrdinal;
722             work->workletsProcessed = 1u;
723             work->result = C2_OK;
724             if (mBuffer) {
725                 work->worklets.front()->output.buffers.push_back(mBuffer);
726             }
727             ALOGV("timestamp = %lld, index = %lld, w/%s buffer",
728                   mOrdinal.timestamp.peekll(), mOrdinal.frameIndex.peekll(),
729                   mBuffer ? "" : "o");
730         }
731 
732        private:
733         const uint32_t mFlags;
734         const C2WorkOrdinalStruct mOrdinal;
735         const std::shared_ptr<C2Buffer> mBuffer;
736     };
737 
738     auto fillWork = [buffer](const std::unique_ptr<C2Work> &work) {
739         work->worklets.front()->output.flags = (C2FrameData::flags_t)0;
740         work->worklets.front()->output.buffers.clear();
741         work->worklets.front()->output.buffers.push_back(buffer);
742         work->worklets.front()->output.ordinal = work->input.ordinal;
743         work->workletsProcessed = 1u;
744     };
745     if (work && c2_cntr64_t(index) == work->input.ordinal.frameIndex) {
746         bool eos = ((work->input.flags & C2FrameData::FLAG_END_OF_STREAM) != 0);
747         // TODO: Check if cloneAndSend can be avoided by tracking number of frames remaining
748         if (eos) {
749             if (buffer) {
750                 mOutIndex = index;
751                 C2WorkOrdinalStruct outOrdinal = work->input.ordinal;
752                 cloneAndSend(
753                     mOutIndex, work,
754                     FillWork(C2FrameData::FLAG_INCOMPLETE, outOrdinal, buffer));
755                 buffer.reset();
756             }
757         } else {
758             fillWork(work);
759         }
760     } else {
761         finish(index, fillWork);
762     }
763 }
764 
ensureDecoderState(const std::shared_ptr<C2BlockPool> & pool)765 c2_status_t C2SoftHevcDec::ensureDecoderState(const std::shared_ptr<C2BlockPool> &pool) {
766     if (!mDecHandle) {
767         ALOGE("not supposed to be here, invalid decoder context");
768         return C2_CORRUPTED;
769     }
770     if (mOutBlock &&
771             (mOutBlock->width() != ALIGN32(mWidth) || mOutBlock->height() != mHeight)) {
772         mOutBlock.reset();
773     }
774     if (!mOutBlock) {
775         uint32_t format = HAL_PIXEL_FORMAT_YV12;
776         C2MemoryUsage usage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE };
777         c2_status_t err =
778             pool->fetchGraphicBlock(ALIGN32(mWidth), mHeight, format, usage, &mOutBlock);
779         if (err != C2_OK) {
780             ALOGE("fetchGraphicBlock for Output failed with status %d", err);
781             return err;
782         }
783         ALOGV("provided (%dx%d) required (%dx%d)",
784               mOutBlock->width(), mOutBlock->height(), ALIGN32(mWidth), mHeight);
785     }
786 
787     return C2_OK;
788 }
789 
790 // TODO: can overall error checking be improved?
791 // TODO: allow configuration of color format and usage for graphic buffers instead
792 //       of hard coding them to HAL_PIXEL_FORMAT_YV12
793 // TODO: pass coloraspects information to surface
794 // TODO: test support for dynamic change in resolution
795 // TODO: verify if the decoder sent back all frames
process(const std::unique_ptr<C2Work> & work,const std::shared_ptr<C2BlockPool> & pool)796 void C2SoftHevcDec::process(
797         const std::unique_ptr<C2Work> &work,
798         const std::shared_ptr<C2BlockPool> &pool) {
799     // Initialize output work
800     work->result = C2_OK;
801     work->workletsProcessed = 0u;
802     work->worklets.front()->output.configUpdate.clear();
803     work->worklets.front()->output.flags = work->input.flags;
804 
805     if (mSignalledError || mSignalledOutputEos) {
806         work->result = C2_BAD_VALUE;
807         return;
808     }
809 
810     size_t inOffset = 0u;
811     size_t inSize = 0u;
812     uint32_t workIndex = work->input.ordinal.frameIndex.peeku() & 0xFFFFFFFF;
813     C2ReadView rView = mDummyReadView;
814     if (!work->input.buffers.empty()) {
815         rView = work->input.buffers[0]->data().linearBlocks().front().map().get();
816         inSize = rView.capacity();
817         if (inSize && rView.error()) {
818             ALOGE("read view map failed %d", rView.error());
819             work->result = rView.error();
820             return;
821         }
822     }
823     bool eos = ((work->input.flags & C2FrameData::FLAG_END_OF_STREAM) != 0);
824     bool hasPicture = false;
825 
826     ALOGV("in buffer attr. size %zu timestamp %d frameindex %d, flags %x",
827           inSize, (int)work->input.ordinal.timestamp.peeku(),
828           (int)work->input.ordinal.frameIndex.peeku(), work->input.flags);
829     size_t inPos = 0;
830     while (inPos < inSize) {
831         if (C2_OK != ensureDecoderState(pool)) {
832             mSignalledError = true;
833             work->workletsProcessed = 1u;
834             work->result = C2_CORRUPTED;
835             return;
836         }
837         C2GraphicView wView = mOutBlock->map().get();
838         if (wView.error()) {
839             ALOGE("graphic view map failed %d", wView.error());
840             work->result = wView.error();
841             return;
842         }
843         ihevcd_cxa_video_decode_ip_t s_hevcd_decode_ip = {};
844         ihevcd_cxa_video_decode_op_t s_hevcd_decode_op = {};
845         ivd_video_decode_ip_t *ps_decode_ip = &s_hevcd_decode_ip.s_ivd_video_decode_ip_t;
846         ivd_video_decode_op_t *ps_decode_op = &s_hevcd_decode_op.s_ivd_video_decode_op_t;
847         if (!setDecodeArgs(ps_decode_ip, ps_decode_op, &rView, &wView,
848                            inOffset + inPos, inSize - inPos, workIndex)) {
849             mSignalledError = true;
850             work->workletsProcessed = 1u;
851             work->result = C2_CORRUPTED;
852             return;
853         }
854 
855         if (false == mHeaderDecoded) {
856             /* Decode header and get dimensions */
857             setParams(mStride, IVD_DECODE_HEADER);
858         }
859         WORD32 delay;
860         GETTIME(&mTimeStart, nullptr);
861         TIME_DIFF(mTimeEnd, mTimeStart, delay);
862         (void) ivdec_api_function(mDecHandle, ps_decode_ip, ps_decode_op);
863         WORD32 decodeTime;
864         GETTIME(&mTimeEnd, nullptr);
865         TIME_DIFF(mTimeStart, mTimeEnd, decodeTime);
866         ALOGV("decodeTime=%6d delay=%6d numBytes=%6d", decodeTime, delay,
867               ps_decode_op->u4_num_bytes_consumed);
868         if (IVD_MEM_ALLOC_FAILED == (ps_decode_op->u4_error_code & IVD_ERROR_MASK)) {
869             ALOGE("allocation failure in decoder");
870             mSignalledError = true;
871             work->workletsProcessed = 1u;
872             work->result = C2_CORRUPTED;
873             return;
874         } else if (IVD_STREAM_WIDTH_HEIGHT_NOT_SUPPORTED ==
875                    (ps_decode_op->u4_error_code & IVD_ERROR_MASK)) {
876             ALOGE("unsupported resolution : %dx%d", mWidth, mHeight);
877             mSignalledError = true;
878             work->workletsProcessed = 1u;
879             work->result = C2_CORRUPTED;
880             return;
881         } else if (IVD_RES_CHANGED == (ps_decode_op->u4_error_code & IVD_ERROR_MASK)) {
882             ALOGV("resolution changed");
883             drainInternal(DRAIN_COMPONENT_NO_EOS, pool, work);
884             resetDecoder();
885             resetPlugin();
886             work->workletsProcessed = 0u;
887 
888             /* Decode header and get new dimensions */
889             setParams(mStride, IVD_DECODE_HEADER);
890             (void) ivdec_api_function(mDecHandle, ps_decode_ip, ps_decode_op);
891         } else if (IS_IVD_FATAL_ERROR(ps_decode_op->u4_error_code)) {
892             ALOGE("Fatal error in decoder 0x%x", ps_decode_op->u4_error_code);
893             mSignalledError = true;
894             work->workletsProcessed = 1u;
895             work->result = C2_CORRUPTED;
896             return;
897         }
898         if (ps_decode_op->i4_reorder_depth >= 0 && mOutputDelay != ps_decode_op->i4_reorder_depth) {
899             mOutputDelay = ps_decode_op->i4_reorder_depth;
900             ALOGV("New Output delay %d ", mOutputDelay);
901 
902             C2PortActualDelayTuning::output outputDelay(mOutputDelay);
903             std::vector<std::unique_ptr<C2SettingResult>> failures;
904             c2_status_t err =
905                 mIntf->config({&outputDelay}, C2_MAY_BLOCK, &failures);
906             if (err == OK) {
907                 work->worklets.front()->output.configUpdate.push_back(
908                     C2Param::Copy(outputDelay));
909             } else {
910                 ALOGE("Cannot set output delay");
911                 mSignalledError = true;
912                 work->workletsProcessed = 1u;
913                 work->result = C2_CORRUPTED;
914                 return;
915             }
916         }
917         if (0 < ps_decode_op->u4_pic_wd && 0 < ps_decode_op->u4_pic_ht) {
918             if (mHeaderDecoded == false) {
919                 mHeaderDecoded = true;
920                 setParams(ALIGN32(ps_decode_op->u4_pic_wd), IVD_DECODE_FRAME);
921             }
922             if (ps_decode_op->u4_pic_wd != mWidth ||  ps_decode_op->u4_pic_ht != mHeight) {
923                 mWidth = ps_decode_op->u4_pic_wd;
924                 mHeight = ps_decode_op->u4_pic_ht;
925                 CHECK_EQ(0u, ps_decode_op->u4_output_present);
926 
927                 C2StreamPictureSizeInfo::output size(0u, mWidth, mHeight);
928                 std::vector<std::unique_ptr<C2SettingResult>> failures;
929                 c2_status_t err =
930                     mIntf->config({&size}, C2_MAY_BLOCK, &failures);
931                 if (err == OK) {
932                     work->worklets.front()->output.configUpdate.push_back(
933                         C2Param::Copy(size));
934                 } else {
935                     ALOGE("Cannot set width and height");
936                     mSignalledError = true;
937                     work->workletsProcessed = 1u;
938                     work->result = C2_CORRUPTED;
939                     return;
940                 }
941                 continue;
942             }
943         }
944         (void) getVuiParams();
945         hasPicture |= (1 == ps_decode_op->u4_frame_decoded_flag);
946         if (ps_decode_op->u4_output_present) {
947             finishWork(ps_decode_op->u4_ts, work);
948         }
949         if (0 == ps_decode_op->u4_num_bytes_consumed) {
950             ALOGD("Bytes consumed is zero. Ignoring remaining bytes");
951             break;
952         }
953         inPos += ps_decode_op->u4_num_bytes_consumed;
954         if (hasPicture && (inSize - inPos)) {
955             ALOGD("decoded frame in current access nal, ignoring further trailing bytes %d",
956                   (int)inSize - (int)inPos);
957             break;
958         }
959     }
960 
961     if (eos) {
962         drainInternal(DRAIN_COMPONENT_WITH_EOS, pool, work);
963         mSignalledOutputEos = true;
964     } else if (!hasPicture) {
965         fillEmptyWork(work);
966     }
967 }
968 
drainInternal(uint32_t drainMode,const std::shared_ptr<C2BlockPool> & pool,const std::unique_ptr<C2Work> & work)969 c2_status_t C2SoftHevcDec::drainInternal(
970         uint32_t drainMode,
971         const std::shared_ptr<C2BlockPool> &pool,
972         const std::unique_ptr<C2Work> &work) {
973     if (drainMode == NO_DRAIN) {
974         ALOGW("drain with NO_DRAIN: no-op");
975         return C2_OK;
976     }
977     if (drainMode == DRAIN_CHAIN) {
978         ALOGW("DRAIN_CHAIN not supported");
979         return C2_OMITTED;
980     }
981 
982     if (OK != setFlushMode()) return C2_CORRUPTED;
983     while (true) {
984         if (C2_OK != ensureDecoderState(pool)) {
985             mSignalledError = true;
986             work->workletsProcessed = 1u;
987             work->result = C2_CORRUPTED;
988             return C2_CORRUPTED;
989         }
990         C2GraphicView wView = mOutBlock->map().get();
991         if (wView.error()) {
992             ALOGE("graphic view map failed %d", wView.error());
993             return C2_CORRUPTED;
994         }
995         ihevcd_cxa_video_decode_ip_t s_hevcd_decode_ip = {};
996         ihevcd_cxa_video_decode_op_t s_hevcd_decode_op = {};
997         ivd_video_decode_ip_t *ps_decode_ip = &s_hevcd_decode_ip.s_ivd_video_decode_ip_t;
998         ivd_video_decode_op_t *ps_decode_op = &s_hevcd_decode_op.s_ivd_video_decode_op_t;
999         if (!setDecodeArgs(ps_decode_ip, ps_decode_op, nullptr, &wView, 0, 0, 0)) {
1000             mSignalledError = true;
1001             work->workletsProcessed = 1u;
1002             return C2_CORRUPTED;
1003         }
1004         (void) ivdec_api_function(mDecHandle, ps_decode_ip, ps_decode_op);
1005         if (ps_decode_op->u4_output_present) {
1006             finishWork(ps_decode_op->u4_ts, work);
1007         } else {
1008             fillEmptyWork(work);
1009             break;
1010         }
1011     }
1012 
1013     return C2_OK;
1014 }
1015 
drain(uint32_t drainMode,const std::shared_ptr<C2BlockPool> & pool)1016 c2_status_t C2SoftHevcDec::drain(
1017         uint32_t drainMode,
1018         const std::shared_ptr<C2BlockPool> &pool) {
1019     return drainInternal(drainMode, pool, nullptr);
1020 }
1021 
1022 class C2SoftHevcDecFactory : public C2ComponentFactory {
1023 public:
C2SoftHevcDecFactory()1024     C2SoftHevcDecFactory() : mHelper(std::static_pointer_cast<C2ReflectorHelper>(
1025         GetCodec2PlatformComponentStore()->getParamReflector())) {
1026     }
1027 
createComponent(c2_node_id_t id,std::shared_ptr<C2Component> * const component,std::function<void (C2Component *)> deleter)1028     virtual c2_status_t createComponent(
1029             c2_node_id_t id,
1030             std::shared_ptr<C2Component>* const component,
1031             std::function<void(C2Component*)> deleter) override {
1032         *component = std::shared_ptr<C2Component>(
1033                 new C2SoftHevcDec(COMPONENT_NAME,
1034                                  id,
1035                                  std::make_shared<C2SoftHevcDec::IntfImpl>(mHelper)),
1036                 deleter);
1037         return C2_OK;
1038     }
1039 
createInterface(c2_node_id_t id,std::shared_ptr<C2ComponentInterface> * const interface,std::function<void (C2ComponentInterface *)> deleter)1040     virtual c2_status_t createInterface(
1041             c2_node_id_t id,
1042             std::shared_ptr<C2ComponentInterface>* const interface,
1043             std::function<void(C2ComponentInterface*)> deleter) override {
1044         *interface = std::shared_ptr<C2ComponentInterface>(
1045                 new SimpleInterface<C2SoftHevcDec::IntfImpl>(
1046                         COMPONENT_NAME, id, std::make_shared<C2SoftHevcDec::IntfImpl>(mHelper)),
1047                 deleter);
1048         return C2_OK;
1049     }
1050 
1051     virtual ~C2SoftHevcDecFactory() override = default;
1052 
1053 private:
1054     std::shared_ptr<C2ReflectorHelper> mHelper;
1055 };
1056 
1057 }  // namespace android
1058 
1059 __attribute__((cfi_canonical_jump_table))
CreateCodec2Factory()1060 extern "C" ::C2ComponentFactory* CreateCodec2Factory() {
1061     ALOGV("in %s", __func__);
1062     return new ::android::C2SoftHevcDecFactory();
1063 }
1064 
1065 __attribute__((cfi_canonical_jump_table))
DestroyCodec2Factory(::C2ComponentFactory * factory)1066 extern "C" void DestroyCodec2Factory(::C2ComponentFactory* factory) {
1067     ALOGV("in %s", __func__);
1068     delete factory;
1069 }
1070