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