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