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(const char * name,c2_node_id_t id,const std::shared_ptr<C2ReflectorHelper> & helper)235 C2SoftMpeg4Dec::C2SoftMpeg4Dec(
236 const char *name,
237 c2_node_id_t id,
238 const std::shared_ptr<C2ReflectorHelper> &helper)
239 : C2SoftMpeg4Dec(name, id, std::make_shared<IntfImpl>(helper)) {
240 }
241
~C2SoftMpeg4Dec()242 C2SoftMpeg4Dec::~C2SoftMpeg4Dec() {
243 onRelease();
244 }
245
onInit()246 c2_status_t C2SoftMpeg4Dec::onInit() {
247 status_t err = initDecoder();
248 return err == OK ? C2_OK : C2_CORRUPTED;
249 }
250
onStop()251 c2_status_t C2SoftMpeg4Dec::onStop() {
252 if (mInitialized) {
253 PVCleanUpVideoDecoder(&mVideoDecControls);
254 mInitialized = false;
255 }
256 for (int32_t i = 0; i < kNumOutputBuffers; ++i) {
257 if (mOutputBuffer[i]) {
258 free(mOutputBuffer[i]);
259 mOutputBuffer[i] = nullptr;
260 }
261 }
262 mNumSamplesOutput = 0;
263 mFramesConfigured = false;
264 mSignalledOutputEos = false;
265 mSignalledError = false;
266 if (mOutBlock) {
267 mOutBlock.reset();
268 }
269 return C2_OK;
270 }
271
onReset()272 void C2SoftMpeg4Dec::onReset() {
273 (void)onStop();
274 (void)onInit();
275 }
276
onRelease()277 void C2SoftMpeg4Dec::onRelease() {
278 (void)onStop();
279 if (mOutBlock) {
280 mOutBlock.reset();
281 }
282 }
283
onFlush_sm()284 c2_status_t C2SoftMpeg4Dec::onFlush_sm() {
285 if (mInitialized) {
286 if (PV_TRUE != PVResetVideoDecoder(&mVideoDecControls)) {
287 return C2_CORRUPTED;
288 }
289 }
290 mSignalledOutputEos = false;
291 mSignalledError = false;
292 return C2_OK;
293 }
294
initDecoder()295 status_t C2SoftMpeg4Dec::initDecoder() {
296 #ifdef MPEG4
297 mIsMpeg4 = true;
298 #else
299 mIsMpeg4 = false;
300 #endif
301
302 memset(&mVideoDecControls, 0, sizeof(tagvideoDecControls));
303
304 /* TODO: bring these values to 352 and 288. It cannot be done as of now
305 * because, h263 doesn't seem to allow port reconfiguration. In OMX, the
306 * problem of larger width and height than default width and height is
307 * overcome by adaptivePlayBack() api call. This call gets width and height
308 * information from extractor. Such a thing is not possible here.
309 * So we are configuring to larger values.*/
310 mWidth = 1408;
311 mHeight = 1152;
312 mNumSamplesOutput = 0;
313 mInitialized = false;
314 mFramesConfigured = false;
315 mSignalledOutputEos = false;
316 mSignalledError = false;
317
318 return OK;
319 }
320
fillEmptyWork(const std::unique_ptr<C2Work> & work)321 void fillEmptyWork(const std::unique_ptr<C2Work> &work) {
322 uint32_t flags = 0;
323 if (work->input.flags & C2FrameData::FLAG_END_OF_STREAM) {
324 flags |= C2FrameData::FLAG_END_OF_STREAM;
325 ALOGV("signalling eos");
326 }
327 work->worklets.front()->output.flags = (C2FrameData::flags_t)flags;
328 work->worklets.front()->output.buffers.clear();
329 work->worklets.front()->output.ordinal = work->input.ordinal;
330 work->workletsProcessed = 1u;
331 }
332
finishWork(uint64_t index,const std::unique_ptr<C2Work> & work)333 void C2SoftMpeg4Dec::finishWork(uint64_t index, const std::unique_ptr<C2Work> &work) {
334 std::shared_ptr<C2Buffer> buffer = createGraphicBuffer(std::move(mOutBlock),
335 C2Rect(mWidth, mHeight));
336 mOutBlock = nullptr;
337 auto fillWork = [buffer, index](const std::unique_ptr<C2Work> &work) {
338 uint32_t flags = 0;
339 if ((work->input.flags & C2FrameData::FLAG_END_OF_STREAM) &&
340 (c2_cntr64_t(index) == work->input.ordinal.frameIndex)) {
341 flags |= C2FrameData::FLAG_END_OF_STREAM;
342 ALOGV("signalling eos");
343 }
344 work->worklets.front()->output.flags = (C2FrameData::flags_t)flags;
345 work->worklets.front()->output.buffers.clear();
346 work->worklets.front()->output.buffers.push_back(buffer);
347 work->worklets.front()->output.ordinal = work->input.ordinal;
348 work->workletsProcessed = 1u;
349 };
350 if (work && c2_cntr64_t(index) == work->input.ordinal.frameIndex) {
351 fillWork(work);
352 } else {
353 finish(index, fillWork);
354 }
355 }
356
ensureDecoderState(const std::shared_ptr<C2BlockPool> & pool)357 c2_status_t C2SoftMpeg4Dec::ensureDecoderState(const std::shared_ptr<C2BlockPool> &pool) {
358
359 mOutputBufferSize = align(mIntf->getMaxWidth(), 16) * align(mIntf->getMaxHeight(), 16) * 3 / 2;
360 for (int32_t i = 0; i < kNumOutputBuffers; ++i) {
361 if (!mOutputBuffer[i]) {
362 mOutputBuffer[i] = (uint8_t *)malloc(mOutputBufferSize);
363 if (!mOutputBuffer[i]) {
364 return C2_NO_MEMORY;
365 }
366 }
367 }
368 if (mOutBlock &&
369 (mOutBlock->width() != align(mWidth, 16) || mOutBlock->height() != mHeight)) {
370 mOutBlock.reset();
371 }
372 if (!mOutBlock) {
373 uint32_t format = HAL_PIXEL_FORMAT_YV12;
374 C2MemoryUsage usage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE };
375 c2_status_t err = pool->fetchGraphicBlock(align(mWidth, 16), mHeight, format, usage, &mOutBlock);
376 if (err != C2_OK) {
377 ALOGE("fetchGraphicBlock for Output failed with status %d", err);
378 return err;
379 }
380 ALOGV("provided (%dx%d) required (%dx%d)",
381 mOutBlock->width(), mOutBlock->height(), mWidth, mHeight);
382 }
383 return C2_OK;
384 }
385
handleResChange(const std::unique_ptr<C2Work> & work)386 bool C2SoftMpeg4Dec::handleResChange(const std::unique_ptr<C2Work> &work) {
387 uint32_t disp_width, disp_height;
388 PVGetVideoDimensions(&mVideoDecControls, (int32 *)&disp_width, (int32 *)&disp_height);
389
390 uint32_t buf_width, buf_height;
391 PVGetBufferDimensions(&mVideoDecControls, (int32 *)&buf_width, (int32 *)&buf_height);
392
393 CHECK_LE(disp_width, buf_width);
394 CHECK_LE(disp_height, buf_height);
395
396 ALOGV("display size (%dx%d), buffer size (%dx%d)",
397 disp_width, disp_height, buf_width, buf_height);
398
399 bool resChanged = false;
400 if (disp_width != mWidth || disp_height != mHeight) {
401 mWidth = disp_width;
402 mHeight = disp_height;
403 resChanged = true;
404 for (int32_t i = 0; i < kNumOutputBuffers; ++i) {
405 if (mOutputBuffer[i]) {
406 free(mOutputBuffer[i]);
407 mOutputBuffer[i] = nullptr;
408 }
409 }
410
411 if (!mIsMpeg4) {
412 PVCleanUpVideoDecoder(&mVideoDecControls);
413
414 uint8_t *vol_data[1]{};
415 int32_t vol_size = 0;
416
417 if (!PVInitVideoDecoder(
418 &mVideoDecControls, vol_data, &vol_size, 1, mIntf->getMaxWidth(),
419 mIntf->getMaxHeight(), H263_MODE)) {
420 ALOGE("Error in PVInitVideoDecoder H263_MODE while resChanged was set to true");
421 mSignalledError = true;
422 work->result = C2_CORRUPTED;
423 return true;
424 }
425 }
426 mFramesConfigured = false;
427 }
428 return resChanged;
429 }
430
process(const std::unique_ptr<C2Work> & work,const std::shared_ptr<C2BlockPool> & pool)431 void C2SoftMpeg4Dec::process(
432 const std::unique_ptr<C2Work> &work,
433 const std::shared_ptr<C2BlockPool> &pool) {
434 // Initialize output work
435 work->result = C2_OK;
436 work->workletsProcessed = 1u;
437 work->worklets.front()->output.configUpdate.clear();
438 work->worklets.front()->output.flags = work->input.flags;
439
440 if (mSignalledError || mSignalledOutputEos) {
441 work->result = C2_BAD_VALUE;
442 return;
443 }
444
445 size_t inOffset = 0u;
446 size_t inSize = 0u;
447 uint32_t workIndex = work->input.ordinal.frameIndex.peeku() & 0xFFFFFFFF;
448 C2ReadView rView = mDummyReadView;
449 if (!work->input.buffers.empty()) {
450 rView = work->input.buffers[0]->data().linearBlocks().front().map().get();
451 inSize = rView.capacity();
452 if (inSize && rView.error()) {
453 ALOGE("read view map failed %d", rView.error());
454 work->result = C2_CORRUPTED;
455 return;
456 }
457 }
458 ALOGV("in buffer attr. size %zu timestamp %d frameindex %d, flags %x",
459 inSize, (int)work->input.ordinal.timestamp.peeku(),
460 (int)work->input.ordinal.frameIndex.peeku(), work->input.flags);
461
462 bool eos = ((work->input.flags & C2FrameData::FLAG_END_OF_STREAM) != 0);
463 if (inSize == 0) {
464 fillEmptyWork(work);
465 if (eos) {
466 mSignalledOutputEos = true;
467 }
468 return;
469 }
470
471 uint8_t *bitstream = const_cast<uint8_t *>(rView.data() + inOffset);
472 uint32_t *start_code = (uint32_t *)bitstream;
473 bool volHeader = *start_code == 0xB0010000;
474 if (volHeader) {
475 PVCleanUpVideoDecoder(&mVideoDecControls);
476 mInitialized = false;
477 }
478
479 bool codecConfig = (work->input.flags & C2FrameData::FLAG_CODEC_CONFIG) != 0;
480
481 if (!mInitialized) {
482 uint8_t *vol_data[1]{};
483 int32_t vol_size = 0;
484
485 if (codecConfig || volHeader) {
486 vol_data[0] = bitstream;
487 vol_size = inSize;
488 }
489 MP4DecodingMode mode = (mIsMpeg4) ? MPEG4_MODE : H263_MODE;
490 if (!PVInitVideoDecoder(
491 &mVideoDecControls, vol_data, &vol_size, 1,
492 mIntf->getMaxWidth(), mIntf->getMaxHeight(), mode)) {
493 ALOGE("PVInitVideoDecoder failed. Unsupported content?");
494 mSignalledError = true;
495 work->result = C2_CORRUPTED;
496 return;
497 }
498 mInitialized = true;
499 MP4DecodingMode actualMode = PVGetDecBitstreamMode(&mVideoDecControls);
500 if (mode != actualMode) {
501 ALOGE("Decoded mode not same as actual mode of the decoder");
502 mSignalledError = true;
503 work->result = C2_CORRUPTED;
504 return;
505 }
506
507 PVSetPostProcType(&mVideoDecControls, 0);
508 if (handleResChange(work)) {
509 ALOGI("Setting width and height");
510 C2StreamPictureSizeInfo::output size(0u, mWidth, mHeight);
511 std::vector<std::unique_ptr<C2SettingResult>> failures;
512 c2_status_t err = mIntf->config({&size}, C2_MAY_BLOCK, &failures);
513 if (err == OK) {
514 work->worklets.front()->output.configUpdate.push_back(
515 C2Param::Copy(size));
516 } else {
517 ALOGE("Config update size failed");
518 mSignalledError = true;
519 work->result = C2_CORRUPTED;
520 return;
521 }
522 }
523 }
524
525 if (codecConfig) {
526 fillEmptyWork(work);
527 return;
528 }
529
530 size_t inPos = 0;
531 while (inPos < inSize) {
532 c2_status_t err = ensureDecoderState(pool);
533 if (C2_OK != err) {
534 mSignalledError = true;
535 work->result = err;
536 return;
537 }
538 C2GraphicView wView = mOutBlock->map().get();
539 if (wView.error()) {
540 ALOGE("graphic view map failed %d", wView.error());
541 work->result = C2_CORRUPTED;
542 return;
543 }
544
545 uint32_t yFrameSize = sizeof(uint8) * mVideoDecControls.size;
546 if (mOutputBufferSize < yFrameSize * 3 / 2){
547 ALOGE("Too small output buffer: %zu bytes", mOutputBufferSize);
548 mSignalledError = true;
549 work->result = C2_NO_MEMORY;
550 return;
551 }
552
553 if (!mFramesConfigured) {
554 PVSetReferenceYUV(&mVideoDecControls,mOutputBuffer[1]);
555 mFramesConfigured = true;
556 }
557
558 // Need to check if header contains new info, e.g., width/height, etc.
559 VopHeaderInfo header_info;
560 uint32_t useExtTimestamp = (inPos == 0);
561 int32_t tmpInSize = (int32_t)inSize;
562 uint8_t *bitstreamTmp = bitstream;
563 uint32_t timestamp = workIndex;
564 if (PVDecodeVopHeader(
565 &mVideoDecControls, &bitstreamTmp, ×tamp, &tmpInSize,
566 &header_info, &useExtTimestamp,
567 mOutputBuffer[mNumSamplesOutput & 1]) != PV_TRUE) {
568 ALOGE("failed to decode vop header.");
569 mSignalledError = true;
570 work->result = C2_CORRUPTED;
571 return;
572 }
573
574 // H263 doesn't have VOL header, the frame size information is in short header, i.e. the
575 // decoder may detect size change after PVDecodeVopHeader.
576 bool resChange = handleResChange(work);
577 if (mIsMpeg4 && resChange) {
578 mSignalledError = true;
579 work->result = C2_CORRUPTED;
580 return;
581 } else if (resChange) {
582 ALOGI("Setting width and height");
583 C2StreamPictureSizeInfo::output size(0u, mWidth, mHeight);
584 std::vector<std::unique_ptr<C2SettingResult>> failures;
585 c2_status_t err = mIntf->config({&size}, C2_MAY_BLOCK, &failures);
586 if (err == OK) {
587 work->worklets.front()->output.configUpdate.push_back(C2Param::Copy(size));
588 } else {
589 ALOGE("Config update size failed");
590 mSignalledError = true;
591 work->result = C2_CORRUPTED;
592 return;
593 }
594 continue;
595 }
596
597 if (PVDecodeVopBody(&mVideoDecControls, &tmpInSize) != PV_TRUE) {
598 ALOGE("failed to decode video frame.");
599 mSignalledError = true;
600 work->result = C2_CORRUPTED;
601 return;
602 }
603 if (handleResChange(work)) {
604 mSignalledError = true;
605 work->result = C2_CORRUPTED;
606 return;
607 }
608
609 uint8_t *outputBufferY = wView.data()[C2PlanarLayout::PLANE_Y];
610 uint8_t *outputBufferU = wView.data()[C2PlanarLayout::PLANE_U];
611 uint8_t *outputBufferV = wView.data()[C2PlanarLayout::PLANE_V];
612
613 C2PlanarLayout layout = wView.layout();
614 size_t dstYStride = layout.planes[C2PlanarLayout::PLANE_Y].rowInc;
615 size_t dstUStride = layout.planes[C2PlanarLayout::PLANE_U].rowInc;
616 size_t dstVStride = layout.planes[C2PlanarLayout::PLANE_V].rowInc;
617 size_t srcYStride = align(mWidth, 16);
618 size_t srcUStride = srcYStride / 2;
619 size_t srcVStride = srcYStride / 2;
620 size_t vStride = align(mHeight, 16);
621 const uint8_t *srcY = (const uint8_t *)mOutputBuffer[mNumSamplesOutput & 1];
622 const uint8_t *srcU = (const uint8_t *)srcY + vStride * srcYStride;
623 const uint8_t *srcV = (const uint8_t *)srcY + vStride * srcYStride * 5 / 4;
624
625 convertYUV420Planar8ToYV12(outputBufferY, outputBufferU, outputBufferV, srcY, srcU, srcV,
626 srcYStride, srcUStride, srcVStride, dstYStride, dstUStride,
627 dstVStride, mWidth, mHeight);
628
629 inPos += inSize - (size_t)tmpInSize;
630 finishWork(workIndex, work);
631 ++mNumSamplesOutput;
632 if (inSize - inPos != 0) {
633 ALOGD("decoded frame, ignoring further trailing bytes %d",
634 (int)inSize - (int)inPos);
635 break;
636 }
637 }
638 }
639
drain(uint32_t drainMode,const std::shared_ptr<C2BlockPool> & pool)640 c2_status_t C2SoftMpeg4Dec::drain(
641 uint32_t drainMode,
642 const std::shared_ptr<C2BlockPool> &pool) {
643 (void)pool;
644 if (drainMode == NO_DRAIN) {
645 ALOGW("drain with NO_DRAIN: no-op");
646 return C2_OK;
647 }
648 if (drainMode == DRAIN_CHAIN) {
649 ALOGW("DRAIN_CHAIN not supported");
650 return C2_OMITTED;
651 }
652 return C2_OK;
653 }
654
655 class C2SoftMpeg4DecFactory : public C2ComponentFactory {
656 public:
C2SoftMpeg4DecFactory()657 C2SoftMpeg4DecFactory() : mHelper(std::static_pointer_cast<C2ReflectorHelper>(
658 GetCodec2PlatformComponentStore()->getParamReflector())) {
659 }
660
createComponent(c2_node_id_t id,std::shared_ptr<C2Component> * const component,std::function<void (C2Component *)> deleter)661 virtual c2_status_t createComponent(
662 c2_node_id_t id,
663 std::shared_ptr<C2Component>* const component,
664 std::function<void(C2Component*)> deleter) override {
665 *component = std::shared_ptr<C2Component>(
666 new C2SoftMpeg4Dec(COMPONENT_NAME,
667 id,
668 std::make_shared<C2SoftMpeg4Dec::IntfImpl>(mHelper)),
669 deleter);
670 return C2_OK;
671 }
672
createInterface(c2_node_id_t id,std::shared_ptr<C2ComponentInterface> * const interface,std::function<void (C2ComponentInterface *)> deleter)673 virtual c2_status_t createInterface(
674 c2_node_id_t id,
675 std::shared_ptr<C2ComponentInterface>* const interface,
676 std::function<void(C2ComponentInterface*)> deleter) override {
677 *interface = std::shared_ptr<C2ComponentInterface>(
678 new SimpleInterface<C2SoftMpeg4Dec::IntfImpl>(
679 COMPONENT_NAME, id, std::make_shared<C2SoftMpeg4Dec::IntfImpl>(mHelper)),
680 deleter);
681 return C2_OK;
682 }
683
684 virtual ~C2SoftMpeg4DecFactory() override = default;
685
686 private:
687 std::shared_ptr<C2ReflectorHelper> mHelper;
688 };
689
690 } // namespace android
691
692 __attribute__((cfi_canonical_jump_table))
CreateCodec2Factory()693 extern "C" ::C2ComponentFactory* CreateCodec2Factory() {
694 ALOGV("in %s", __func__);
695 return new ::android::C2SoftMpeg4DecFactory();
696 }
697
698 __attribute__((cfi_canonical_jump_table))
DestroyCodec2Factory(::C2ComponentFactory * factory)699 extern "C" void DestroyCodec2Factory(::C2ComponentFactory* factory) {
700 ALOGV("in %s", __func__);
701 delete factory;
702 }
703