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 #ifdef MPEG4
19 #define LOG_TAG "C2SoftMpeg4Dec"
20 #else
21 #define LOG_TAG "C2SoftH263Dec"
22 #endif
23 #include <log/log.h>
24
25 #include <media/stagefright/foundation/AUtils.h>
26 #include <media/stagefright/foundation/MediaDefs.h>
27
28 #include <C2Debug.h>
29 #include <C2PlatformSupport.h>
30 #include <SimpleC2Interface.h>
31
32 #include "C2SoftMpeg4Dec.h"
33 #include "mp4dec_api.h"
34
35 namespace android {
36 constexpr size_t kMinInputBufferSize = 2 * 1024 * 1024;
37 #ifdef MPEG4
38 constexpr size_t kMaxDimension = 1920;
39 constexpr char COMPONENT_NAME[] = "c2.android.mpeg4.decoder";
40 #else
41 constexpr size_t kMaxDimension = 352;
42 constexpr char COMPONENT_NAME[] = "c2.android.h263.decoder";
43 #endif
44
45 class C2SoftMpeg4Dec::IntfImpl : public SimpleInterface<void>::BaseParams {
46 public:
IntfImpl(const std::shared_ptr<C2ReflectorHelper> & helper)47 explicit IntfImpl(const std::shared_ptr<C2ReflectorHelper> &helper)
48 : SimpleInterface<void>::BaseParams(
49 helper,
50 COMPONENT_NAME,
51 C2Component::KIND_DECODER,
52 C2Component::DOMAIN_VIDEO,
53 #ifdef MPEG4
54 MEDIA_MIMETYPE_VIDEO_MPEG4
55 #else
56 MEDIA_MIMETYPE_VIDEO_H263
57 #endif
58 ) {
59 noPrivateBuffers(); // TODO: account for our buffers here
60 noInputReferences();
61 noOutputReferences();
62 noInputLatency();
63 noTimeStretch();
64
65 // TODO: Proper support for reorder depth.
66 addParameter(
67 DefineParam(mActualOutputDelay, C2_PARAMKEY_OUTPUT_DELAY)
68 .withConstValue(new C2PortActualDelayTuning::output(1u))
69 .build());
70
71 addParameter(
72 DefineParam(mAttrib, C2_PARAMKEY_COMPONENT_ATTRIBUTES)
73 .withConstValue(new C2ComponentAttributesSetting(C2Component::ATTRIB_IS_TEMPORAL))
74 .build());
75
76 addParameter(
77 DefineParam(mSize, C2_PARAMKEY_PICTURE_SIZE)
78 .withDefault(new C2StreamPictureSizeInfo::output(0u, 176, 144))
79 .withFields({
80 C2F(mSize, width).inRange(2, kMaxDimension, 2),
81 C2F(mSize, height).inRange(2, kMaxDimension, 2),
82 })
83 .withSetter(SizeSetter)
84 .build());
85
86 #ifdef MPEG4
87 addParameter(
88 DefineParam(mProfileLevel, C2_PARAMKEY_PROFILE_LEVEL)
89 .withDefault(new C2StreamProfileLevelInfo::input(0u,
90 C2Config::PROFILE_MP4V_SIMPLE, C2Config::LEVEL_MP4V_3))
91 .withFields({
92 C2F(mProfileLevel, profile).equalTo(
93 C2Config::PROFILE_MP4V_SIMPLE),
94 C2F(mProfileLevel, level).oneOf({
95 C2Config::LEVEL_MP4V_0,
96 C2Config::LEVEL_MP4V_0B,
97 C2Config::LEVEL_MP4V_1,
98 C2Config::LEVEL_MP4V_2,
99 C2Config::LEVEL_MP4V_3,
100 C2Config::LEVEL_MP4V_3B,
101 C2Config::LEVEL_MP4V_4,
102 C2Config::LEVEL_MP4V_4A,
103 C2Config::LEVEL_MP4V_5,
104 C2Config::LEVEL_MP4V_6})
105 })
106 .withSetter(ProfileLevelSetter, mSize)
107 .build());
108 #else
109 addParameter(
110 DefineParam(mProfileLevel, C2_PARAMKEY_PROFILE_LEVEL)
111 .withDefault(new C2StreamProfileLevelInfo::input(0u,
112 C2Config::PROFILE_H263_BASELINE, C2Config::LEVEL_H263_30))
113 .withFields({
114 C2F(mProfileLevel, profile).oneOf({
115 C2Config::PROFILE_H263_BASELINE,
116 C2Config::PROFILE_H263_ISWV2}),
117 C2F(mProfileLevel, level).oneOf({
118 C2Config::LEVEL_H263_10,
119 C2Config::LEVEL_H263_20,
120 C2Config::LEVEL_H263_30,
121 C2Config::LEVEL_H263_40,
122 C2Config::LEVEL_H263_45})
123 })
124 .withSetter(ProfileLevelSetter, mSize)
125 .build());
126 #endif
127
128 addParameter(
129 DefineParam(mMaxSize, C2_PARAMKEY_MAX_PICTURE_SIZE)
130 .withDefault(new C2StreamMaxPictureSizeTuning::output(0u, 352, 288))
131 .withFields({
132 C2F(mSize, width).inRange(2, kMaxDimension, 2),
133 C2F(mSize, height).inRange(2, kMaxDimension, 2),
134 })
135 .withSetter(MaxPictureSizeSetter, mSize)
136 .build());
137
138 addParameter(
139 DefineParam(mMaxInputSize, C2_PARAMKEY_INPUT_MAX_BUFFER_SIZE)
140 .withDefault(new C2StreamMaxBufferSizeInfo::input(0u, kMinInputBufferSize))
141 .withFields({
142 C2F(mMaxInputSize, value).any(),
143 })
144 .calculatedAs(MaxInputSizeSetter, mMaxSize)
145 .build());
146
147 C2ChromaOffsetStruct locations[1] = { C2ChromaOffsetStruct::ITU_YUV_420_0() };
148 std::shared_ptr<C2StreamColorInfo::output> defaultColorInfo =
149 C2StreamColorInfo::output::AllocShared(
150 1u, 0u, 8u /* bitDepth */, C2Color::YUV_420);
151 memcpy(defaultColorInfo->m.locations, locations, sizeof(locations));
152
153 defaultColorInfo =
154 C2StreamColorInfo::output::AllocShared(
155 { C2ChromaOffsetStruct::ITU_YUV_420_0() },
156 0u, 8u /* bitDepth */, C2Color::YUV_420);
157 helper->addStructDescriptors<C2ChromaOffsetStruct>();
158
159 addParameter(
160 DefineParam(mColorInfo, C2_PARAMKEY_CODED_COLOR_INFO)
161 .withConstValue(defaultColorInfo)
162 .build());
163
164 // TODO: support more formats?
165 addParameter(
166 DefineParam(mPixelFormat, C2_PARAMKEY_PIXEL_FORMAT)
167 .withConstValue(new C2StreamPixelFormatInfo::output(
168 0u, HAL_PIXEL_FORMAT_YCBCR_420_888))
169 .build());
170 }
171
SizeSetter(bool mayBlock,const C2P<C2StreamPictureSizeInfo::output> & oldMe,C2P<C2StreamPictureSizeInfo::output> & me)172 static C2R SizeSetter(bool mayBlock, const C2P<C2StreamPictureSizeInfo::output> &oldMe,
173 C2P<C2StreamPictureSizeInfo::output> &me) {
174 (void)mayBlock;
175 C2R res = C2R::Ok();
176 if (!me.F(me.v.width).supportsAtAll(me.v.width)) {
177 res = res.plus(C2SettingResultBuilder::BadValue(me.F(me.v.width)));
178 me.set().width = oldMe.v.width;
179 }
180 if (!me.F(me.v.height).supportsAtAll(me.v.height)) {
181 res = res.plus(C2SettingResultBuilder::BadValue(me.F(me.v.height)));
182 me.set().height = oldMe.v.height;
183 }
184 return res;
185 }
186
MaxPictureSizeSetter(bool mayBlock,C2P<C2StreamMaxPictureSizeTuning::output> & me,const C2P<C2StreamPictureSizeInfo::output> & size)187 static C2R MaxPictureSizeSetter(bool mayBlock, C2P<C2StreamMaxPictureSizeTuning::output> &me,
188 const C2P<C2StreamPictureSizeInfo::output> &size) {
189 (void)mayBlock;
190 // TODO: get max width/height from the size's field helpers vs. hardcoding
191 me.set().width = c2_min(c2_max(me.v.width, size.v.width), kMaxDimension);
192 me.set().height = c2_min(c2_max(me.v.height, size.v.height), kMaxDimension);
193 return C2R::Ok();
194 }
195
MaxInputSizeSetter(bool mayBlock,C2P<C2StreamMaxBufferSizeInfo::input> & me,const C2P<C2StreamMaxPictureSizeTuning::output> & maxSize)196 static C2R MaxInputSizeSetter(bool mayBlock, C2P<C2StreamMaxBufferSizeInfo::input> &me,
197 const C2P<C2StreamMaxPictureSizeTuning::output> &maxSize) {
198 (void)mayBlock;
199 // assume compression ratio of 1
200 me.set().value = c2_max((((maxSize.v.width + 15) / 16)
201 * ((maxSize.v.height + 15) / 16) * 384), kMinInputBufferSize);
202 return C2R::Ok();
203 }
204
ProfileLevelSetter(bool mayBlock,C2P<C2StreamProfileLevelInfo::input> & me,const C2P<C2StreamPictureSizeInfo::output> & size)205 static C2R ProfileLevelSetter(bool mayBlock, C2P<C2StreamProfileLevelInfo::input> &me,
206 const C2P<C2StreamPictureSizeInfo::output> &size) {
207 (void)mayBlock;
208 (void)size;
209 (void)me; // TODO: validate
210 return C2R::Ok();
211 }
212
getMaxWidth() const213 uint32_t getMaxWidth() const { return mMaxSize->width; }
getMaxHeight() const214 uint32_t getMaxHeight() const { return mMaxSize->height; }
215
216 private:
217 std::shared_ptr<C2StreamProfileLevelInfo::input> mProfileLevel;
218 std::shared_ptr<C2StreamPictureSizeInfo::output> mSize;
219 std::shared_ptr<C2StreamMaxPictureSizeTuning::output> mMaxSize;
220 std::shared_ptr<C2StreamMaxBufferSizeInfo::input> mMaxInputSize;
221 std::shared_ptr<C2StreamColorInfo::output> mColorInfo;
222 std::shared_ptr<C2StreamPixelFormatInfo::output> mPixelFormat;
223 };
224
C2SoftMpeg4Dec(const char * name,c2_node_id_t id,const std::shared_ptr<IntfImpl> & intfImpl)225 C2SoftMpeg4Dec::C2SoftMpeg4Dec(
226 const char *name,
227 c2_node_id_t id,
228 const std::shared_ptr<IntfImpl> &intfImpl)
229 : SimpleC2Component(std::make_shared<SimpleInterface<IntfImpl>>(name, id, intfImpl)),
230 mIntf(intfImpl),
231 mOutputBuffer{},
232 mInitialized(false) {
233 }
234
~C2SoftMpeg4Dec()235 C2SoftMpeg4Dec::~C2SoftMpeg4Dec() {
236 onRelease();
237 }
238
onInit()239 c2_status_t C2SoftMpeg4Dec::onInit() {
240 status_t err = initDecoder();
241 return err == OK ? C2_OK : C2_CORRUPTED;
242 }
243
onStop()244 c2_status_t C2SoftMpeg4Dec::onStop() {
245 if (mInitialized) {
246 PVCleanUpVideoDecoder(&mVideoDecControls);
247 mInitialized = false;
248 }
249 for (int32_t i = 0; i < kNumOutputBuffers; ++i) {
250 if (mOutputBuffer[i]) {
251 free(mOutputBuffer[i]);
252 mOutputBuffer[i] = nullptr;
253 }
254 }
255 mNumSamplesOutput = 0;
256 mFramesConfigured = false;
257 mSignalledOutputEos = false;
258 mSignalledError = false;
259 if (mOutBlock) {
260 mOutBlock.reset();
261 }
262 return C2_OK;
263 }
264
onReset()265 void C2SoftMpeg4Dec::onReset() {
266 (void)onStop();
267 (void)onInit();
268 }
269
onRelease()270 void C2SoftMpeg4Dec::onRelease() {
271 (void)onStop();
272 if (mOutBlock) {
273 mOutBlock.reset();
274 }
275 }
276
onFlush_sm()277 c2_status_t C2SoftMpeg4Dec::onFlush_sm() {
278 if (mInitialized) {
279 if (PV_TRUE != PVResetVideoDecoder(&mVideoDecControls)) {
280 return C2_CORRUPTED;
281 }
282 }
283 mSignalledOutputEos = false;
284 mSignalledError = false;
285 return C2_OK;
286 }
287
initDecoder()288 status_t C2SoftMpeg4Dec::initDecoder() {
289 #ifdef MPEG4
290 mIsMpeg4 = true;
291 #else
292 mIsMpeg4 = false;
293 #endif
294
295 memset(&mVideoDecControls, 0, sizeof(tagvideoDecControls));
296
297 /* TODO: bring these values to 352 and 288. It cannot be done as of now
298 * because, h263 doesn't seem to allow port reconfiguration. In OMX, the
299 * problem of larger width and height than default width and height is
300 * overcome by adaptivePlayBack() api call. This call gets width and height
301 * information from extractor. Such a thing is not possible here.
302 * So we are configuring to larger values.*/
303 mWidth = 1408;
304 mHeight = 1152;
305 mNumSamplesOutput = 0;
306 mInitialized = false;
307 mFramesConfigured = false;
308 mSignalledOutputEos = false;
309 mSignalledError = false;
310
311 return OK;
312 }
313
fillEmptyWork(const std::unique_ptr<C2Work> & work)314 void fillEmptyWork(const std::unique_ptr<C2Work> &work) {
315 uint32_t flags = 0;
316 if (work->input.flags & C2FrameData::FLAG_END_OF_STREAM) {
317 flags |= C2FrameData::FLAG_END_OF_STREAM;
318 ALOGV("signalling eos");
319 }
320 work->worklets.front()->output.flags = (C2FrameData::flags_t)flags;
321 work->worklets.front()->output.buffers.clear();
322 work->worklets.front()->output.ordinal = work->input.ordinal;
323 work->workletsProcessed = 1u;
324 }
325
finishWork(uint64_t index,const std::unique_ptr<C2Work> & work)326 void C2SoftMpeg4Dec::finishWork(uint64_t index, const std::unique_ptr<C2Work> &work) {
327 std::shared_ptr<C2Buffer> buffer = createGraphicBuffer(std::move(mOutBlock),
328 C2Rect(mWidth, mHeight));
329 mOutBlock = nullptr;
330 auto fillWork = [buffer, index](const std::unique_ptr<C2Work> &work) {
331 uint32_t flags = 0;
332 if ((work->input.flags & C2FrameData::FLAG_END_OF_STREAM) &&
333 (c2_cntr64_t(index) == work->input.ordinal.frameIndex)) {
334 flags |= C2FrameData::FLAG_END_OF_STREAM;
335 ALOGV("signalling eos");
336 }
337 work->worklets.front()->output.flags = (C2FrameData::flags_t)flags;
338 work->worklets.front()->output.buffers.clear();
339 work->worklets.front()->output.buffers.push_back(buffer);
340 work->worklets.front()->output.ordinal = work->input.ordinal;
341 work->workletsProcessed = 1u;
342 };
343 if (work && c2_cntr64_t(index) == work->input.ordinal.frameIndex) {
344 fillWork(work);
345 } else {
346 finish(index, fillWork);
347 }
348 }
349
ensureDecoderState(const std::shared_ptr<C2BlockPool> & pool)350 c2_status_t C2SoftMpeg4Dec::ensureDecoderState(const std::shared_ptr<C2BlockPool> &pool) {
351
352 mOutputBufferSize = align(mIntf->getMaxWidth(), 16) * align(mIntf->getMaxHeight(), 16) * 3 / 2;
353 for (int32_t i = 0; i < kNumOutputBuffers; ++i) {
354 if (!mOutputBuffer[i]) {
355 mOutputBuffer[i] = (uint8_t *)malloc(mOutputBufferSize);
356 if (!mOutputBuffer[i]) {
357 return C2_NO_MEMORY;
358 }
359 }
360 }
361 if (mOutBlock &&
362 (mOutBlock->width() != align(mWidth, 16) || mOutBlock->height() != mHeight)) {
363 mOutBlock.reset();
364 }
365 if (!mOutBlock) {
366 uint32_t format = HAL_PIXEL_FORMAT_YV12;
367 C2MemoryUsage usage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE };
368 c2_status_t err = pool->fetchGraphicBlock(align(mWidth, 16), mHeight, format, usage, &mOutBlock);
369 if (err != C2_OK) {
370 ALOGE("fetchGraphicBlock for Output failed with status %d", err);
371 return err;
372 }
373 ALOGV("provided (%dx%d) required (%dx%d)",
374 mOutBlock->width(), mOutBlock->height(), mWidth, mHeight);
375 }
376 return C2_OK;
377 }
378
handleResChange(const std::unique_ptr<C2Work> & work)379 bool C2SoftMpeg4Dec::handleResChange(const std::unique_ptr<C2Work> &work) {
380 uint32_t disp_width, disp_height;
381 PVGetVideoDimensions(&mVideoDecControls, (int32 *)&disp_width, (int32 *)&disp_height);
382
383 uint32_t buf_width, buf_height;
384 PVGetBufferDimensions(&mVideoDecControls, (int32 *)&buf_width, (int32 *)&buf_height);
385
386 CHECK_LE(disp_width, buf_width);
387 CHECK_LE(disp_height, buf_height);
388
389 ALOGV("display size (%dx%d), buffer size (%dx%d)",
390 disp_width, disp_height, buf_width, buf_height);
391
392 bool resChanged = false;
393 if (disp_width != mWidth || disp_height != mHeight) {
394 mWidth = disp_width;
395 mHeight = disp_height;
396 resChanged = true;
397 for (int32_t i = 0; i < kNumOutputBuffers; ++i) {
398 if (mOutputBuffer[i]) {
399 free(mOutputBuffer[i]);
400 mOutputBuffer[i] = nullptr;
401 }
402 }
403
404 if (!mIsMpeg4) {
405 PVCleanUpVideoDecoder(&mVideoDecControls);
406
407 uint8_t *vol_data[1]{};
408 int32_t vol_size = 0;
409
410 if (!PVInitVideoDecoder(
411 &mVideoDecControls, vol_data, &vol_size, 1, mIntf->getMaxWidth(),
412 mIntf->getMaxHeight(), H263_MODE)) {
413 ALOGE("Error in PVInitVideoDecoder H263_MODE while resChanged was set to true");
414 mSignalledError = true;
415 work->result = C2_CORRUPTED;
416 return true;
417 }
418 }
419 mFramesConfigured = false;
420 }
421 return resChanged;
422 }
423
process(const std::unique_ptr<C2Work> & work,const std::shared_ptr<C2BlockPool> & pool)424 void C2SoftMpeg4Dec::process(
425 const std::unique_ptr<C2Work> &work,
426 const std::shared_ptr<C2BlockPool> &pool) {
427 // Initialize output work
428 work->result = C2_OK;
429 work->workletsProcessed = 1u;
430 work->worklets.front()->output.configUpdate.clear();
431 work->worklets.front()->output.flags = work->input.flags;
432
433 if (mSignalledError || mSignalledOutputEos) {
434 work->result = C2_BAD_VALUE;
435 return;
436 }
437
438 size_t inOffset = 0u;
439 size_t inSize = 0u;
440 uint32_t workIndex = work->input.ordinal.frameIndex.peeku() & 0xFFFFFFFF;
441 C2ReadView rView = mDummyReadView;
442 if (!work->input.buffers.empty()) {
443 rView = work->input.buffers[0]->data().linearBlocks().front().map().get();
444 inSize = rView.capacity();
445 if (inSize && rView.error()) {
446 ALOGE("read view map failed %d", rView.error());
447 work->result = C2_CORRUPTED;
448 return;
449 }
450 }
451 ALOGV("in buffer attr. size %zu timestamp %d frameindex %d, flags %x",
452 inSize, (int)work->input.ordinal.timestamp.peeku(),
453 (int)work->input.ordinal.frameIndex.peeku(), work->input.flags);
454
455 bool eos = ((work->input.flags & C2FrameData::FLAG_END_OF_STREAM) != 0);
456 if (inSize == 0) {
457 fillEmptyWork(work);
458 if (eos) {
459 mSignalledOutputEos = true;
460 }
461 return;
462 }
463
464 uint8_t *bitstream = const_cast<uint8_t *>(rView.data() + inOffset);
465 uint32_t *start_code = (uint32_t *)bitstream;
466 bool volHeader = *start_code == 0xB0010000;
467 if (volHeader) {
468 PVCleanUpVideoDecoder(&mVideoDecControls);
469 mInitialized = false;
470 }
471
472 bool codecConfig = (work->input.flags & C2FrameData::FLAG_CODEC_CONFIG) != 0;
473
474 if (!mInitialized) {
475 uint8_t *vol_data[1]{};
476 int32_t vol_size = 0;
477
478 if (codecConfig || volHeader) {
479 vol_data[0] = bitstream;
480 vol_size = inSize;
481 }
482 MP4DecodingMode mode = (mIsMpeg4) ? MPEG4_MODE : H263_MODE;
483 if (!PVInitVideoDecoder(
484 &mVideoDecControls, vol_data, &vol_size, 1,
485 mIntf->getMaxWidth(), mIntf->getMaxHeight(), mode)) {
486 ALOGE("PVInitVideoDecoder failed. Unsupported content?");
487 mSignalledError = true;
488 work->result = C2_CORRUPTED;
489 return;
490 }
491 mInitialized = true;
492 MP4DecodingMode actualMode = PVGetDecBitstreamMode(&mVideoDecControls);
493 if (mode != actualMode) {
494 ALOGE("Decoded mode not same as actual mode of the decoder");
495 mSignalledError = true;
496 work->result = C2_CORRUPTED;
497 return;
498 }
499
500 PVSetPostProcType(&mVideoDecControls, 0);
501 if (handleResChange(work)) {
502 ALOGI("Setting width and height");
503 C2StreamPictureSizeInfo::output size(0u, mWidth, mHeight);
504 std::vector<std::unique_ptr<C2SettingResult>> failures;
505 c2_status_t err = mIntf->config({&size}, C2_MAY_BLOCK, &failures);
506 if (err == OK) {
507 work->worklets.front()->output.configUpdate.push_back(
508 C2Param::Copy(size));
509 } else {
510 ALOGE("Config update size failed");
511 mSignalledError = true;
512 work->result = C2_CORRUPTED;
513 return;
514 }
515 }
516 }
517
518 if (codecConfig) {
519 fillEmptyWork(work);
520 return;
521 }
522
523 size_t inPos = 0;
524 while (inPos < inSize) {
525 c2_status_t err = ensureDecoderState(pool);
526 if (C2_OK != err) {
527 mSignalledError = true;
528 work->result = err;
529 return;
530 }
531 C2GraphicView wView = mOutBlock->map().get();
532 if (wView.error()) {
533 ALOGE("graphic view map failed %d", wView.error());
534 work->result = C2_CORRUPTED;
535 return;
536 }
537
538 uint32_t yFrameSize = sizeof(uint8) * mVideoDecControls.size;
539 if (mOutputBufferSize < yFrameSize * 3 / 2){
540 ALOGE("Too small output buffer: %zu bytes", mOutputBufferSize);
541 mSignalledError = true;
542 work->result = C2_NO_MEMORY;
543 return;
544 }
545
546 if (!mFramesConfigured) {
547 PVSetReferenceYUV(&mVideoDecControls,mOutputBuffer[1]);
548 mFramesConfigured = true;
549 }
550
551 // Need to check if header contains new info, e.g., width/height, etc.
552 VopHeaderInfo header_info;
553 uint32_t useExtTimestamp = (inPos == 0);
554 int32_t tmpInSize = (int32_t)inSize;
555 uint8_t *bitstreamTmp = bitstream;
556 uint32_t timestamp = workIndex;
557 if (PVDecodeVopHeader(
558 &mVideoDecControls, &bitstreamTmp, ×tamp, &tmpInSize,
559 &header_info, &useExtTimestamp,
560 mOutputBuffer[mNumSamplesOutput & 1]) != PV_TRUE) {
561 ALOGE("failed to decode vop header.");
562 mSignalledError = true;
563 work->result = C2_CORRUPTED;
564 return;
565 }
566
567 // H263 doesn't have VOL header, the frame size information is in short header, i.e. the
568 // decoder may detect size change after PVDecodeVopHeader.
569 bool resChange = handleResChange(work);
570 if (mIsMpeg4 && resChange) {
571 mSignalledError = true;
572 work->result = C2_CORRUPTED;
573 return;
574 } else if (resChange) {
575 ALOGI("Setting width and height");
576 C2StreamPictureSizeInfo::output size(0u, mWidth, mHeight);
577 std::vector<std::unique_ptr<C2SettingResult>> failures;
578 c2_status_t err = mIntf->config({&size}, C2_MAY_BLOCK, &failures);
579 if (err == OK) {
580 work->worklets.front()->output.configUpdate.push_back(C2Param::Copy(size));
581 } else {
582 ALOGE("Config update size failed");
583 mSignalledError = true;
584 work->result = C2_CORRUPTED;
585 return;
586 }
587 continue;
588 }
589
590 if (PVDecodeVopBody(&mVideoDecControls, &tmpInSize) != PV_TRUE) {
591 ALOGE("failed to decode video frame.");
592 mSignalledError = true;
593 work->result = C2_CORRUPTED;
594 return;
595 }
596 if (handleResChange(work)) {
597 mSignalledError = true;
598 work->result = C2_CORRUPTED;
599 return;
600 }
601
602 uint8_t *outputBufferY = wView.data()[C2PlanarLayout::PLANE_Y];
603 uint8_t *outputBufferU = wView.data()[C2PlanarLayout::PLANE_U];
604 uint8_t *outputBufferV = wView.data()[C2PlanarLayout::PLANE_V];
605
606 C2PlanarLayout layout = wView.layout();
607 size_t dstYStride = layout.planes[C2PlanarLayout::PLANE_Y].rowInc;
608 size_t dstUStride = layout.planes[C2PlanarLayout::PLANE_U].rowInc;
609 size_t dstVStride = layout.planes[C2PlanarLayout::PLANE_V].rowInc;
610 size_t srcYStride = align(mWidth, 16);
611 size_t srcUStride = srcYStride / 2;
612 size_t srcVStride = srcYStride / 2;
613 size_t vStride = align(mHeight, 16);
614 const uint8_t *srcY = (const uint8_t *)mOutputBuffer[mNumSamplesOutput & 1];
615 const uint8_t *srcU = (const uint8_t *)srcY + vStride * srcYStride;
616 const uint8_t *srcV = (const uint8_t *)srcY + vStride * srcYStride * 5 / 4;
617
618 convertYUV420Planar8ToYV12(outputBufferY, outputBufferU, outputBufferV, srcY, srcU, srcV,
619 srcYStride, srcUStride, srcVStride, dstYStride, dstUStride,
620 dstVStride, mWidth, mHeight);
621
622 inPos += inSize - (size_t)tmpInSize;
623 finishWork(workIndex, work);
624 ++mNumSamplesOutput;
625 if (inSize - inPos != 0) {
626 ALOGD("decoded frame, ignoring further trailing bytes %d",
627 (int)inSize - (int)inPos);
628 break;
629 }
630 }
631 }
632
drain(uint32_t drainMode,const std::shared_ptr<C2BlockPool> & pool)633 c2_status_t C2SoftMpeg4Dec::drain(
634 uint32_t drainMode,
635 const std::shared_ptr<C2BlockPool> &pool) {
636 (void)pool;
637 if (drainMode == NO_DRAIN) {
638 ALOGW("drain with NO_DRAIN: no-op");
639 return C2_OK;
640 }
641 if (drainMode == DRAIN_CHAIN) {
642 ALOGW("DRAIN_CHAIN not supported");
643 return C2_OMITTED;
644 }
645 return C2_OK;
646 }
647
648 class C2SoftMpeg4DecFactory : public C2ComponentFactory {
649 public:
C2SoftMpeg4DecFactory()650 C2SoftMpeg4DecFactory() : mHelper(std::static_pointer_cast<C2ReflectorHelper>(
651 GetCodec2PlatformComponentStore()->getParamReflector())) {
652 }
653
createComponent(c2_node_id_t id,std::shared_ptr<C2Component> * const component,std::function<void (C2Component *)> deleter)654 virtual c2_status_t createComponent(
655 c2_node_id_t id,
656 std::shared_ptr<C2Component>* const component,
657 std::function<void(C2Component*)> deleter) override {
658 *component = std::shared_ptr<C2Component>(
659 new C2SoftMpeg4Dec(COMPONENT_NAME,
660 id,
661 std::make_shared<C2SoftMpeg4Dec::IntfImpl>(mHelper)),
662 deleter);
663 return C2_OK;
664 }
665
createInterface(c2_node_id_t id,std::shared_ptr<C2ComponentInterface> * const interface,std::function<void (C2ComponentInterface *)> deleter)666 virtual c2_status_t createInterface(
667 c2_node_id_t id,
668 std::shared_ptr<C2ComponentInterface>* const interface,
669 std::function<void(C2ComponentInterface*)> deleter) override {
670 *interface = std::shared_ptr<C2ComponentInterface>(
671 new SimpleInterface<C2SoftMpeg4Dec::IntfImpl>(
672 COMPONENT_NAME, id, std::make_shared<C2SoftMpeg4Dec::IntfImpl>(mHelper)),
673 deleter);
674 return C2_OK;
675 }
676
677 virtual ~C2SoftMpeg4DecFactory() override = default;
678
679 private:
680 std::shared_ptr<C2ReflectorHelper> mHelper;
681 };
682
683 } // namespace android
684
685 __attribute__((cfi_canonical_jump_table))
CreateCodec2Factory()686 extern "C" ::C2ComponentFactory* CreateCodec2Factory() {
687 ALOGV("in %s", __func__);
688 return new ::android::C2SoftMpeg4DecFactory();
689 }
690
691 __attribute__((cfi_canonical_jump_table))
DestroyCodec2Factory(::C2ComponentFactory * factory)692 extern "C" void DestroyCodec2Factory(::C2ComponentFactory* factory) {
693 ALOGV("in %s", __func__);
694 delete factory;
695 }
696