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