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