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