• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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 "C2SoftGav1Dec"
19 #include "C2SoftGav1Dec.h"
20 
21 #include <C2Debug.h>
22 #include <C2PlatformSupport.h>
23 #include <SimpleC2Interface.h>
24 #include <log/log.h>
25 #include <media/stagefright/foundation/AUtils.h>
26 #include <media/stagefright/foundation/MediaDefs.h>
27 
28 namespace android {
29 namespace {
30 
31 constexpr uint8_t NEUTRAL_UV_VALUE = 128;
32 
33 }  // namespace
34 
35 // codecname set and passed in as a compile flag from Android.bp
36 constexpr char COMPONENT_NAME[] = CODECNAME;
37 
38 class C2SoftGav1Dec::IntfImpl : public SimpleInterface<void>::BaseParams {
39  public:
IntfImpl(const std::shared_ptr<C2ReflectorHelper> & helper)40   explicit IntfImpl(const std::shared_ptr<C2ReflectorHelper> &helper)
41       : SimpleInterface<void>::BaseParams(
42             helper, COMPONENT_NAME, C2Component::KIND_DECODER,
43             C2Component::DOMAIN_VIDEO, MEDIA_MIMETYPE_VIDEO_AV1) {
44     noPrivateBuffers();  // TODO: account for our buffers here.
45     noInputReferences();
46     noOutputReferences();
47     noInputLatency();
48     noTimeStretch();
49 
50     addParameter(DefineParam(mAttrib, C2_PARAMKEY_COMPONENT_ATTRIBUTES)
51                      .withConstValue(new C2ComponentAttributesSetting(
52                          C2Component::ATTRIB_IS_TEMPORAL))
53                      .build());
54 
55     addParameter(
56         DefineParam(mSize, C2_PARAMKEY_PICTURE_SIZE)
57             .withDefault(new C2StreamPictureSizeInfo::output(0u, 320, 240))
58             .withFields({
59                 C2F(mSize, width).inRange(2, 4096, 2),
60                 C2F(mSize, height).inRange(2, 4096, 2),
61             })
62             .withSetter(SizeSetter)
63             .build());
64 
65     addParameter(DefineParam(mProfileLevel, C2_PARAMKEY_PROFILE_LEVEL)
66                      .withDefault(new C2StreamProfileLevelInfo::input(
67                          0u, C2Config::PROFILE_AV1_0, C2Config::LEVEL_AV1_2_1))
68                      .withFields({C2F(mProfileLevel, profile)
69                                       .oneOf({C2Config::PROFILE_AV1_0,
70                                               C2Config::PROFILE_AV1_1}),
71                                   C2F(mProfileLevel, level)
72                                       .oneOf({
73                                           C2Config::LEVEL_AV1_2, C2Config::LEVEL_AV1_2_1,
74                                           C2Config::LEVEL_AV1_2_2, C2Config::LEVEL_AV1_2_3,
75                                           C2Config::LEVEL_AV1_3, C2Config::LEVEL_AV1_3_1,
76                                           C2Config::LEVEL_AV1_3_2, C2Config::LEVEL_AV1_3_3,
77                                           C2Config::LEVEL_AV1_4, C2Config::LEVEL_AV1_4_1,
78                                           C2Config::LEVEL_AV1_4_2, C2Config::LEVEL_AV1_4_3,
79                                           C2Config::LEVEL_AV1_5, C2Config::LEVEL_AV1_5_1,
80                                           C2Config::LEVEL_AV1_5_2, C2Config::LEVEL_AV1_5_3,
81                                       })})
82                      .withSetter(ProfileLevelSetter, mSize)
83                      .build());
84 
85     mHdr10PlusInfoInput = C2StreamHdr10PlusInfo::input::AllocShared(0);
86     addParameter(
87         DefineParam(mHdr10PlusInfoInput, C2_PARAMKEY_INPUT_HDR10_PLUS_INFO)
88             .withDefault(mHdr10PlusInfoInput)
89             .withFields({
90                 C2F(mHdr10PlusInfoInput, m.value).any(),
91             })
92             .withSetter(Hdr10PlusInfoInputSetter)
93             .build());
94 
95     mHdr10PlusInfoOutput = C2StreamHdr10PlusInfo::output::AllocShared(0);
96     addParameter(
97         DefineParam(mHdr10PlusInfoOutput, C2_PARAMKEY_OUTPUT_HDR10_PLUS_INFO)
98             .withDefault(mHdr10PlusInfoOutput)
99             .withFields({
100                 C2F(mHdr10PlusInfoOutput, m.value).any(),
101             })
102             .withSetter(Hdr10PlusInfoOutputSetter)
103             .build());
104 
105     addParameter(
106         DefineParam(mMaxSize, C2_PARAMKEY_MAX_PICTURE_SIZE)
107             .withDefault(new C2StreamMaxPictureSizeTuning::output(0u, 320, 240))
108             .withFields({
109                 C2F(mSize, width).inRange(2, 2048, 2),
110                 C2F(mSize, height).inRange(2, 2048, 2),
111             })
112             .withSetter(MaxPictureSizeSetter, mSize)
113             .build());
114 
115     addParameter(DefineParam(mMaxInputSize, C2_PARAMKEY_INPUT_MAX_BUFFER_SIZE)
116                      .withDefault(new C2StreamMaxBufferSizeInfo::input(
117                          0u, 320 * 240 * 3 / 4))
118                      .withFields({
119                          C2F(mMaxInputSize, value).any(),
120                      })
121                      .calculatedAs(MaxInputSizeSetter, mMaxSize)
122                      .build());
123 
124     C2ChromaOffsetStruct locations[1] = {C2ChromaOffsetStruct::ITU_YUV_420_0()};
125     std::shared_ptr<C2StreamColorInfo::output> defaultColorInfo =
126         C2StreamColorInfo::output::AllocShared(1u, 0u, 8u /* bitDepth */,
127                                                C2Color::YUV_420);
128     memcpy(defaultColorInfo->m.locations, locations, sizeof(locations));
129 
130     defaultColorInfo = C2StreamColorInfo::output::AllocShared(
131         {C2ChromaOffsetStruct::ITU_YUV_420_0()}, 0u, 8u /* bitDepth */,
132         C2Color::YUV_420);
133     helper->addStructDescriptors<C2ChromaOffsetStruct>();
134 
135     addParameter(DefineParam(mColorInfo, C2_PARAMKEY_CODED_COLOR_INFO)
136                      .withConstValue(defaultColorInfo)
137                      .build());
138 
139     addParameter(
140         DefineParam(mDefaultColorAspects, C2_PARAMKEY_DEFAULT_COLOR_ASPECTS)
141             .withDefault(new C2StreamColorAspectsTuning::output(
142                 0u, C2Color::RANGE_UNSPECIFIED, C2Color::PRIMARIES_UNSPECIFIED,
143                 C2Color::TRANSFER_UNSPECIFIED, C2Color::MATRIX_UNSPECIFIED))
144             .withFields(
145                 {C2F(mDefaultColorAspects, range)
146                      .inRange(C2Color::RANGE_UNSPECIFIED, C2Color::RANGE_OTHER),
147                  C2F(mDefaultColorAspects, primaries)
148                      .inRange(C2Color::PRIMARIES_UNSPECIFIED,
149                               C2Color::PRIMARIES_OTHER),
150                  C2F(mDefaultColorAspects, transfer)
151                      .inRange(C2Color::TRANSFER_UNSPECIFIED,
152                               C2Color::TRANSFER_OTHER),
153                  C2F(mDefaultColorAspects, matrix)
154                      .inRange(C2Color::MATRIX_UNSPECIFIED,
155                               C2Color::MATRIX_OTHER)})
156             .withSetter(DefaultColorAspectsSetter)
157             .build());
158 
159     // TODO: support more formats?
160     addParameter(DefineParam(mPixelFormat, C2_PARAMKEY_PIXEL_FORMAT)
161                      .withConstValue(new C2StreamPixelFormatInfo::output(
162                          0u, HAL_PIXEL_FORMAT_YCBCR_420_888))
163                      .build());
164   }
165 
SizeSetter(bool mayBlock,const C2P<C2StreamPictureSizeInfo::output> & oldMe,C2P<C2StreamPictureSizeInfo::output> & me)166   static C2R SizeSetter(bool mayBlock,
167                         const C2P<C2StreamPictureSizeInfo::output> &oldMe,
168                         C2P<C2StreamPictureSizeInfo::output> &me) {
169     (void)mayBlock;
170     C2R res = C2R::Ok();
171     if (!me.F(me.v.width).supportsAtAll(me.v.width)) {
172       res = res.plus(C2SettingResultBuilder::BadValue(me.F(me.v.width)));
173       me.set().width = oldMe.v.width;
174     }
175     if (!me.F(me.v.height).supportsAtAll(me.v.height)) {
176       res = res.plus(C2SettingResultBuilder::BadValue(me.F(me.v.height)));
177       me.set().height = oldMe.v.height;
178     }
179     return res;
180   }
181 
MaxPictureSizeSetter(bool mayBlock,C2P<C2StreamMaxPictureSizeTuning::output> & me,const C2P<C2StreamPictureSizeInfo::output> & size)182   static C2R MaxPictureSizeSetter(
183       bool mayBlock, C2P<C2StreamMaxPictureSizeTuning::output> &me,
184       const C2P<C2StreamPictureSizeInfo::output> &size) {
185     (void)mayBlock;
186     // TODO: get max width/height from the size's field helpers vs.
187     // hardcoding
188     me.set().width = c2_min(c2_max(me.v.width, size.v.width), 4096u);
189     me.set().height = c2_min(c2_max(me.v.height, size.v.height), 4096u);
190     return C2R::Ok();
191   }
192 
MaxInputSizeSetter(bool mayBlock,C2P<C2StreamMaxBufferSizeInfo::input> & me,const C2P<C2StreamMaxPictureSizeTuning::output> & maxSize)193   static C2R MaxInputSizeSetter(
194       bool mayBlock, C2P<C2StreamMaxBufferSizeInfo::input> &me,
195       const C2P<C2StreamMaxPictureSizeTuning::output> &maxSize) {
196     (void)mayBlock;
197     // assume compression ratio of 2
198     me.set().value =
199         (((maxSize.v.width + 63) / 64) * ((maxSize.v.height + 63) / 64) * 3072);
200     return C2R::Ok();
201   }
202 
DefaultColorAspectsSetter(bool mayBlock,C2P<C2StreamColorAspectsTuning::output> & me)203   static C2R DefaultColorAspectsSetter(
204       bool mayBlock, C2P<C2StreamColorAspectsTuning::output> &me) {
205     (void)mayBlock;
206     if (me.v.range > C2Color::RANGE_OTHER) {
207       me.set().range = C2Color::RANGE_OTHER;
208     }
209     if (me.v.primaries > C2Color::PRIMARIES_OTHER) {
210       me.set().primaries = C2Color::PRIMARIES_OTHER;
211     }
212     if (me.v.transfer > C2Color::TRANSFER_OTHER) {
213       me.set().transfer = C2Color::TRANSFER_OTHER;
214     }
215     if (me.v.matrix > C2Color::MATRIX_OTHER) {
216       me.set().matrix = C2Color::MATRIX_OTHER;
217     }
218     return C2R::Ok();
219   }
220 
ProfileLevelSetter(bool mayBlock,C2P<C2StreamProfileLevelInfo::input> & me,const C2P<C2StreamPictureSizeInfo::output> & size)221   static C2R ProfileLevelSetter(
222       bool mayBlock, C2P<C2StreamProfileLevelInfo::input> &me,
223       const C2P<C2StreamPictureSizeInfo::output> &size) {
224     (void)mayBlock;
225     (void)size;
226     (void)me;  // TODO: validate
227     return C2R::Ok();
228   }
229 
230   std::shared_ptr<C2StreamColorAspectsTuning::output>
getDefaultColorAspects_l()231   getDefaultColorAspects_l() {
232     return mDefaultColorAspects;
233   }
234 
Hdr10PlusInfoInputSetter(bool mayBlock,C2P<C2StreamHdr10PlusInfo::input> & me)235   static C2R Hdr10PlusInfoInputSetter(bool mayBlock,
236                                       C2P<C2StreamHdr10PlusInfo::input> &me) {
237     (void)mayBlock;
238     (void)me;  // TODO: validate
239     return C2R::Ok();
240   }
241 
Hdr10PlusInfoOutputSetter(bool mayBlock,C2P<C2StreamHdr10PlusInfo::output> & me)242   static C2R Hdr10PlusInfoOutputSetter(bool mayBlock,
243                                        C2P<C2StreamHdr10PlusInfo::output> &me) {
244     (void)mayBlock;
245     (void)me;  // TODO: validate
246     return C2R::Ok();
247   }
248 
249  private:
250   std::shared_ptr<C2StreamProfileLevelInfo::input> mProfileLevel;
251   std::shared_ptr<C2StreamPictureSizeInfo::output> mSize;
252   std::shared_ptr<C2StreamMaxPictureSizeTuning::output> mMaxSize;
253   std::shared_ptr<C2StreamMaxBufferSizeInfo::input> mMaxInputSize;
254   std::shared_ptr<C2StreamColorInfo::output> mColorInfo;
255   std::shared_ptr<C2StreamPixelFormatInfo::output> mPixelFormat;
256   std::shared_ptr<C2StreamColorAspectsTuning::output> mDefaultColorAspects;
257   std::shared_ptr<C2StreamHdr10PlusInfo::input> mHdr10PlusInfoInput;
258   std::shared_ptr<C2StreamHdr10PlusInfo::output> mHdr10PlusInfoOutput;
259 };
260 
C2SoftGav1Dec(const char * name,c2_node_id_t id,const std::shared_ptr<IntfImpl> & intfImpl)261 C2SoftGav1Dec::C2SoftGav1Dec(const char *name, c2_node_id_t id,
262                              const std::shared_ptr<IntfImpl> &intfImpl)
263     : SimpleC2Component(
264           std::make_shared<SimpleInterface<IntfImpl>>(name, id, intfImpl)),
265       mIntf(intfImpl),
266       mCodecCtx(nullptr) {
267   gettimeofday(&mTimeStart, nullptr);
268   gettimeofday(&mTimeEnd, nullptr);
269 }
270 
~C2SoftGav1Dec()271 C2SoftGav1Dec::~C2SoftGav1Dec() { onRelease(); }
272 
onInit()273 c2_status_t C2SoftGav1Dec::onInit() {
274   return initDecoder() ? C2_OK : C2_CORRUPTED;
275 }
276 
onStop()277 c2_status_t C2SoftGav1Dec::onStop() {
278   mSignalledError = false;
279   mSignalledOutputEos = false;
280   return C2_OK;
281 }
282 
onReset()283 void C2SoftGav1Dec::onReset() {
284   (void)onStop();
285   c2_status_t err = onFlush_sm();
286   if (err != C2_OK) {
287     ALOGW("Failed to flush the av1 decoder. Trying to hard reset.");
288     destroyDecoder();
289     if (!initDecoder()) {
290       ALOGE("Hard reset failed.");
291     }
292   }
293 }
294 
onRelease()295 void C2SoftGav1Dec::onRelease() { destroyDecoder(); }
296 
onFlush_sm()297 c2_status_t C2SoftGav1Dec::onFlush_sm() {
298   Libgav1StatusCode status = mCodecCtx->SignalEOS();
299   if (status != kLibgav1StatusOk) {
300     ALOGE("Failed to flush av1 decoder. status: %d.", status);
301     return C2_CORRUPTED;
302   }
303 
304   // Dequeue frame (if any) that was enqueued previously.
305   const libgav1::DecoderBuffer *buffer;
306   status = mCodecCtx->DequeueFrame(&buffer);
307   if (status != kLibgav1StatusOk && status != kLibgav1StatusNothingToDequeue) {
308     ALOGE("Failed to dequeue frame after flushing the av1 decoder. status: %d",
309           status);
310     return C2_CORRUPTED;
311   }
312 
313   mSignalledError = false;
314   mSignalledOutputEos = false;
315 
316   return C2_OK;
317 }
318 
GetCPUCoreCount()319 static int GetCPUCoreCount() {
320   int cpuCoreCount = 1;
321 #if defined(_SC_NPROCESSORS_ONLN)
322   cpuCoreCount = sysconf(_SC_NPROCESSORS_ONLN);
323 #else
324   // _SC_NPROC_ONLN must be defined...
325   cpuCoreCount = sysconf(_SC_NPROC_ONLN);
326 #endif
327   CHECK(cpuCoreCount >= 1);
328   ALOGV("Number of CPU cores: %d", cpuCoreCount);
329   return cpuCoreCount;
330 }
331 
initDecoder()332 bool C2SoftGav1Dec::initDecoder() {
333   mSignalledError = false;
334   mSignalledOutputEos = false;
335   mCodecCtx.reset(new libgav1::Decoder());
336 
337   if (mCodecCtx == nullptr) {
338     ALOGE("mCodecCtx is null");
339     return false;
340   }
341 
342   libgav1::DecoderSettings settings = {};
343   settings.threads = GetCPUCoreCount();
344 
345   ALOGV("Using libgav1 AV1 software decoder.");
346   Libgav1StatusCode status = mCodecCtx->Init(&settings);
347   if (status != kLibgav1StatusOk) {
348     ALOGE("av1 decoder failed to initialize. status: %d.", status);
349     return false;
350   }
351 
352   return true;
353 }
354 
destroyDecoder()355 void C2SoftGav1Dec::destroyDecoder() { mCodecCtx = nullptr; }
356 
fillEmptyWork(const std::unique_ptr<C2Work> & work)357 void fillEmptyWork(const std::unique_ptr<C2Work> &work) {
358   uint32_t flags = 0;
359   if (work->input.flags & C2FrameData::FLAG_END_OF_STREAM) {
360     flags |= C2FrameData::FLAG_END_OF_STREAM;
361     ALOGV("signalling eos");
362   }
363   work->worklets.front()->output.flags = (C2FrameData::flags_t)flags;
364   work->worklets.front()->output.buffers.clear();
365   work->worklets.front()->output.ordinal = work->input.ordinal;
366   work->workletsProcessed = 1u;
367 }
368 
finishWork(uint64_t index,const std::unique_ptr<C2Work> & work,const std::shared_ptr<C2GraphicBlock> & block)369 void C2SoftGav1Dec::finishWork(uint64_t index,
370                                const std::unique_ptr<C2Work> &work,
371                                const std::shared_ptr<C2GraphicBlock> &block) {
372   std::shared_ptr<C2Buffer> buffer =
373       createGraphicBuffer(block, C2Rect(mWidth, mHeight));
374   auto fillWork = [buffer, index](const std::unique_ptr<C2Work> &work) {
375     uint32_t flags = 0;
376     if ((work->input.flags & C2FrameData::FLAG_END_OF_STREAM) &&
377         (c2_cntr64_t(index) == work->input.ordinal.frameIndex)) {
378       flags |= C2FrameData::FLAG_END_OF_STREAM;
379       ALOGV("signalling eos");
380     }
381     work->worklets.front()->output.flags = (C2FrameData::flags_t)flags;
382     work->worklets.front()->output.buffers.clear();
383     work->worklets.front()->output.buffers.push_back(buffer);
384     work->worklets.front()->output.ordinal = work->input.ordinal;
385     work->workletsProcessed = 1u;
386   };
387   if (work && c2_cntr64_t(index) == work->input.ordinal.frameIndex) {
388     fillWork(work);
389   } else {
390     finish(index, fillWork);
391   }
392 }
393 
process(const std::unique_ptr<C2Work> & work,const std::shared_ptr<C2BlockPool> & pool)394 void C2SoftGav1Dec::process(const std::unique_ptr<C2Work> &work,
395                             const std::shared_ptr<C2BlockPool> &pool) {
396   work->result = C2_OK;
397   work->workletsProcessed = 0u;
398   work->worklets.front()->output.configUpdate.clear();
399   work->worklets.front()->output.flags = work->input.flags;
400   if (mSignalledError || mSignalledOutputEos) {
401     work->result = C2_BAD_VALUE;
402     return;
403   }
404 
405   size_t inOffset = 0u;
406   size_t inSize = 0u;
407   C2ReadView rView = mDummyReadView;
408   if (!work->input.buffers.empty()) {
409     rView = work->input.buffers[0]->data().linearBlocks().front().map().get();
410     inSize = rView.capacity();
411     if (inSize && rView.error()) {
412       ALOGE("read view map failed %d", rView.error());
413       work->result = C2_CORRUPTED;
414       return;
415     }
416   }
417 
418   bool codecConfig =
419       ((work->input.flags & C2FrameData::FLAG_CODEC_CONFIG) != 0);
420   bool eos = ((work->input.flags & C2FrameData::FLAG_END_OF_STREAM) != 0);
421 
422   ALOGV("in buffer attr. size %zu timestamp %d frameindex %d, flags %x", inSize,
423         (int)work->input.ordinal.timestamp.peeku(),
424         (int)work->input.ordinal.frameIndex.peeku(), work->input.flags);
425 
426   if (codecConfig) {
427     fillEmptyWork(work);
428     return;
429   }
430 
431   int64_t frameIndex = work->input.ordinal.frameIndex.peekll();
432   if (inSize) {
433     uint8_t *bitstream = const_cast<uint8_t *>(rView.data() + inOffset);
434     int32_t decodeTime = 0;
435     int32_t delay = 0;
436 
437     GETTIME(&mTimeStart, nullptr);
438     TIME_DIFF(mTimeEnd, mTimeStart, delay);
439 
440     const Libgav1StatusCode status =
441         mCodecCtx->EnqueueFrame(bitstream, inSize, frameIndex,
442                                 /*buffer_private_data=*/nullptr);
443 
444     GETTIME(&mTimeEnd, nullptr);
445     TIME_DIFF(mTimeStart, mTimeEnd, decodeTime);
446     ALOGV("decodeTime=%4d delay=%4d\n", decodeTime, delay);
447 
448     if (status != kLibgav1StatusOk) {
449       ALOGE("av1 decoder failed to decode frame. status: %d.", status);
450       work->result = C2_CORRUPTED;
451       work->workletsProcessed = 1u;
452       mSignalledError = true;
453       return;
454     }
455 
456   }
457 
458   (void)outputBuffer(pool, work);
459 
460   if (eos) {
461     drainInternal(DRAIN_COMPONENT_WITH_EOS, pool, work);
462     mSignalledOutputEos = true;
463   } else if (!inSize) {
464     fillEmptyWork(work);
465   }
466 }
467 
copyOutputBufferToYV12Frame(uint8_t * dstY,uint8_t * dstU,uint8_t * dstV,const uint8_t * srcY,const uint8_t * srcU,const uint8_t * srcV,size_t srcYStride,size_t srcUStride,size_t srcVStride,size_t dstYStride,size_t dstUVStride,uint32_t width,uint32_t height,bool isMonochrome)468 static void copyOutputBufferToYV12Frame(uint8_t *dstY, uint8_t *dstU, uint8_t *dstV,
469                                         const uint8_t *srcY, const uint8_t *srcU, const uint8_t *srcV,
470                                         size_t srcYStride, size_t srcUStride, size_t srcVStride,
471                                         size_t dstYStride, size_t dstUVStride,
472                                         uint32_t width, uint32_t height,
473                                         bool isMonochrome) {
474 
475   for (size_t i = 0; i < height; ++i) {
476     memcpy(dstY, srcY, width);
477     srcY += srcYStride;
478     dstY += dstYStride;
479   }
480 
481   if (isMonochrome) {
482     // Fill with neutral U/V values.
483     for (size_t i = 0; i < height / 2; ++i) {
484       memset(dstV, NEUTRAL_UV_VALUE, width / 2);
485       memset(dstU, NEUTRAL_UV_VALUE, width / 2);
486       dstV += dstUVStride;
487       dstU += dstUVStride;
488     }
489     return;
490   }
491 
492   for (size_t i = 0; i < height / 2; ++i) {
493     memcpy(dstV, srcV, width / 2);
494     srcV += srcVStride;
495     dstV += dstUVStride;
496   }
497 
498   for (size_t i = 0; i < height / 2; ++i) {
499     memcpy(dstU, srcU, width / 2);
500     srcU += srcUStride;
501     dstU += dstUVStride;
502   }
503 }
504 
convertYUV420Planar16ToY410(uint32_t * dst,const uint16_t * srcY,const uint16_t * srcU,const uint16_t * srcV,size_t srcYStride,size_t srcUStride,size_t srcVStride,size_t dstStride,size_t width,size_t height)505 static void convertYUV420Planar16ToY410(uint32_t *dst, const uint16_t *srcY,
506                                         const uint16_t *srcU,
507                                         const uint16_t *srcV, size_t srcYStride,
508                                         size_t srcUStride, size_t srcVStride,
509                                         size_t dstStride, size_t width,
510                                         size_t height) {
511   // Converting two lines at a time, slightly faster
512   for (size_t y = 0; y < height; y += 2) {
513     uint32_t *dstTop = (uint32_t *)dst;
514     uint32_t *dstBot = (uint32_t *)(dst + dstStride);
515     uint16_t *ySrcTop = (uint16_t *)srcY;
516     uint16_t *ySrcBot = (uint16_t *)(srcY + srcYStride);
517     uint16_t *uSrc = (uint16_t *)srcU;
518     uint16_t *vSrc = (uint16_t *)srcV;
519 
520     uint32_t u01, v01, y01, y23, y45, y67, uv0, uv1;
521     size_t x = 0;
522     for (; x < width - 3; x += 4) {
523       u01 = *((uint32_t *)uSrc);
524       uSrc += 2;
525       v01 = *((uint32_t *)vSrc);
526       vSrc += 2;
527 
528       y01 = *((uint32_t *)ySrcTop);
529       ySrcTop += 2;
530       y23 = *((uint32_t *)ySrcTop);
531       ySrcTop += 2;
532       y45 = *((uint32_t *)ySrcBot);
533       ySrcBot += 2;
534       y67 = *((uint32_t *)ySrcBot);
535       ySrcBot += 2;
536 
537       uv0 = (u01 & 0x3FF) | ((v01 & 0x3FF) << 20);
538       uv1 = (u01 >> 16) | ((v01 >> 16) << 20);
539 
540       *dstTop++ = 3 << 30 | ((y01 & 0x3FF) << 10) | uv0;
541       *dstTop++ = 3 << 30 | ((y01 >> 16) << 10) | uv0;
542       *dstTop++ = 3 << 30 | ((y23 & 0x3FF) << 10) | uv1;
543       *dstTop++ = 3 << 30 | ((y23 >> 16) << 10) | uv1;
544 
545       *dstBot++ = 3 << 30 | ((y45 & 0x3FF) << 10) | uv0;
546       *dstBot++ = 3 << 30 | ((y45 >> 16) << 10) | uv0;
547       *dstBot++ = 3 << 30 | ((y67 & 0x3FF) << 10) | uv1;
548       *dstBot++ = 3 << 30 | ((y67 >> 16) << 10) | uv1;
549     }
550 
551     // There should be at most 2 more pixels to process. Note that we don't
552     // need to consider odd case as the buffer is always aligned to even.
553     if (x < width) {
554       u01 = *uSrc;
555       v01 = *vSrc;
556       y01 = *((uint32_t *)ySrcTop);
557       y45 = *((uint32_t *)ySrcBot);
558       uv0 = (u01 & 0x3FF) | ((v01 & 0x3FF) << 20);
559       *dstTop++ = ((y01 & 0x3FF) << 10) | uv0;
560       *dstTop++ = ((y01 >> 16) << 10) | uv0;
561       *dstBot++ = ((y45 & 0x3FF) << 10) | uv0;
562       *dstBot++ = ((y45 >> 16) << 10) | uv0;
563     }
564 
565     srcY += srcYStride * 2;
566     srcU += srcUStride;
567     srcV += srcVStride;
568     dst += dstStride * 2;
569   }
570 }
571 
convertYUV420Planar16ToYUV420Planar(uint8_t * dstY,uint8_t * dstU,uint8_t * dstV,const uint16_t * srcY,const uint16_t * srcU,const uint16_t * srcV,size_t srcYStride,size_t srcUStride,size_t srcVStride,size_t dstYStride,size_t dstUVStride,size_t width,size_t height,bool isMonochrome)572 static void convertYUV420Planar16ToYUV420Planar(
573     uint8_t *dstY, uint8_t *dstU, uint8_t *dstV,
574     const uint16_t *srcY, const uint16_t *srcU, const uint16_t *srcV,
575     size_t srcYStride, size_t srcUStride, size_t srcVStride,
576     size_t dstYStride, size_t dstUVStride,
577     size_t width, size_t height, bool isMonochrome) {
578 
579   for (size_t y = 0; y < height; ++y) {
580     for (size_t x = 0; x < width; ++x) {
581       dstY[x] = (uint8_t)(srcY[x] >> 2);
582     }
583 
584     srcY += srcYStride;
585     dstY += dstYStride;
586   }
587 
588   if (isMonochrome) {
589     // Fill with neutral U/V values.
590     for (size_t y = 0; y < (height + 1) / 2; ++y) {
591       memset(dstV, NEUTRAL_UV_VALUE, (width + 1) / 2);
592       memset(dstU, NEUTRAL_UV_VALUE, (width + 1) / 2);
593       dstV += dstUVStride;
594       dstU += dstUVStride;
595     }
596     return;
597   }
598 
599   for (size_t y = 0; y < (height + 1) / 2; ++y) {
600     for (size_t x = 0; x < (width + 1) / 2; ++x) {
601       dstU[x] = (uint8_t)(srcU[x] >> 2);
602       dstV[x] = (uint8_t)(srcV[x] >> 2);
603     }
604 
605     srcU += srcUStride;
606     srcV += srcVStride;
607     dstU += dstUVStride;
608     dstV += dstUVStride;
609   }
610 }
611 
outputBuffer(const std::shared_ptr<C2BlockPool> & pool,const std::unique_ptr<C2Work> & work)612 bool C2SoftGav1Dec::outputBuffer(const std::shared_ptr<C2BlockPool> &pool,
613                                  const std::unique_ptr<C2Work> &work) {
614   if (!(work && pool)) return false;
615 
616   const libgav1::DecoderBuffer *buffer;
617   const Libgav1StatusCode status = mCodecCtx->DequeueFrame(&buffer);
618 
619   if (status != kLibgav1StatusOk && status != kLibgav1StatusNothingToDequeue) {
620     ALOGE("av1 decoder DequeueFrame failed. status: %d.", status);
621     return false;
622   }
623 
624   // |buffer| can be NULL if status was equal to kLibgav1StatusOk or
625   // kLibgav1StatusNothingToDequeue. This is not an error. This could mean one
626   // of two things:
627   //  - The EnqueueFrame() call was either a flush (called with nullptr).
628   //  - The enqueued frame did not have any displayable frames.
629   if (!buffer) {
630     return false;
631   }
632 
633   const int width = buffer->displayed_width[0];
634   const int height = buffer->displayed_height[0];
635   if (width != mWidth || height != mHeight) {
636     mWidth = width;
637     mHeight = height;
638 
639     C2StreamPictureSizeInfo::output size(0u, mWidth, mHeight);
640     std::vector<std::unique_ptr<C2SettingResult>> failures;
641     c2_status_t err = mIntf->config({&size}, C2_MAY_BLOCK, &failures);
642     if (err == C2_OK) {
643       work->worklets.front()->output.configUpdate.push_back(
644           C2Param::Copy(size));
645     } else {
646       ALOGE("Config update size failed");
647       mSignalledError = true;
648       work->result = C2_CORRUPTED;
649       work->workletsProcessed = 1u;
650       return false;
651     }
652   }
653 
654   if (!(buffer->image_format == libgav1::kImageFormatYuv420 ||
655         buffer->image_format == libgav1::kImageFormatMonochrome400)) {
656     ALOGE("image_format %d not supported", buffer->image_format);
657     mSignalledError = true;
658     work->workletsProcessed = 1u;
659     work->result = C2_CORRUPTED;
660     return false;
661   }
662   const bool isMonochrome =
663       buffer->image_format == libgav1::kImageFormatMonochrome400;
664 
665   std::shared_ptr<C2GraphicBlock> block;
666   uint32_t format = HAL_PIXEL_FORMAT_YV12;
667   if (buffer->bitdepth == 10) {
668     IntfImpl::Lock lock = mIntf->lock();
669     std::shared_ptr<C2StreamColorAspectsTuning::output> defaultColorAspects =
670         mIntf->getDefaultColorAspects_l();
671 
672     if (defaultColorAspects->primaries == C2Color::PRIMARIES_BT2020 &&
673         defaultColorAspects->matrix == C2Color::MATRIX_BT2020 &&
674         defaultColorAspects->transfer == C2Color::TRANSFER_ST2084) {
675       if (buffer->image_format != libgav1::kImageFormatYuv420) {
676         ALOGE("Only YUV420 output is supported when targeting RGBA_1010102");
677         mSignalledError = true;
678         work->result = C2_OMITTED;
679         work->workletsProcessed = 1u;
680         return false;
681       }
682       format = HAL_PIXEL_FORMAT_RGBA_1010102;
683     }
684   }
685   C2MemoryUsage usage = {C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE};
686 
687   c2_status_t err = pool->fetchGraphicBlock(align(mWidth, 16), mHeight, format,
688                                             usage, &block);
689 
690   if (err != C2_OK) {
691     ALOGE("fetchGraphicBlock for Output failed with status %d", err);
692     work->result = err;
693     return false;
694   }
695 
696   C2GraphicView wView = block->map().get();
697 
698   if (wView.error()) {
699     ALOGE("graphic view map failed %d", wView.error());
700     work->result = C2_CORRUPTED;
701     return false;
702   }
703 
704   ALOGV("provided (%dx%d) required (%dx%d), out frameindex %d", block->width(),
705         block->height(), mWidth, mHeight, (int)buffer->user_private_data);
706 
707   uint8_t *dstY = const_cast<uint8_t *>(wView.data()[C2PlanarLayout::PLANE_Y]);
708   uint8_t *dstU = const_cast<uint8_t *>(wView.data()[C2PlanarLayout::PLANE_U]);
709   uint8_t *dstV = const_cast<uint8_t *>(wView.data()[C2PlanarLayout::PLANE_V]);
710   size_t srcYStride = buffer->stride[0];
711   size_t srcUStride = buffer->stride[1];
712   size_t srcVStride = buffer->stride[2];
713 
714   C2PlanarLayout layout = wView.layout();
715   size_t dstYStride = layout.planes[C2PlanarLayout::PLANE_Y].rowInc;
716   size_t dstUVStride = layout.planes[C2PlanarLayout::PLANE_U].rowInc;
717 
718   if (buffer->bitdepth == 10) {
719     const uint16_t *srcY = (const uint16_t *)buffer->plane[0];
720     const uint16_t *srcU = (const uint16_t *)buffer->plane[1];
721     const uint16_t *srcV = (const uint16_t *)buffer->plane[2];
722 
723     if (format == HAL_PIXEL_FORMAT_RGBA_1010102) {
724       convertYUV420Planar16ToY410(
725           (uint32_t *)dstY, srcY, srcU, srcV, srcYStride / 2, srcUStride / 2,
726           srcVStride / 2, dstYStride / sizeof(uint32_t), mWidth, mHeight);
727     } else {
728       convertYUV420Planar16ToYUV420Planar(
729           dstY, dstU, dstV, srcY, srcU, srcV, srcYStride / 2, srcUStride / 2,
730           srcVStride / 2, dstYStride, dstUVStride, mWidth, mHeight,
731           isMonochrome);
732     }
733   } else {
734     const uint8_t *srcY = (const uint8_t *)buffer->plane[0];
735     const uint8_t *srcU = (const uint8_t *)buffer->plane[1];
736     const uint8_t *srcV = (const uint8_t *)buffer->plane[2];
737     copyOutputBufferToYV12Frame(
738         dstY, dstU, dstV, srcY, srcU, srcV, srcYStride, srcUStride, srcVStride,
739         dstYStride, dstUVStride, mWidth, mHeight, isMonochrome);
740   }
741   finishWork(buffer->user_private_data, work, std::move(block));
742   block = nullptr;
743   return true;
744 }
745 
drainInternal(uint32_t drainMode,const std::shared_ptr<C2BlockPool> & pool,const std::unique_ptr<C2Work> & work)746 c2_status_t C2SoftGav1Dec::drainInternal(
747     uint32_t drainMode, const std::shared_ptr<C2BlockPool> &pool,
748     const std::unique_ptr<C2Work> &work) {
749   if (drainMode == NO_DRAIN) {
750     ALOGW("drain with NO_DRAIN: no-op");
751     return C2_OK;
752   }
753   if (drainMode == DRAIN_CHAIN) {
754     ALOGW("DRAIN_CHAIN not supported");
755     return C2_OMITTED;
756   }
757 
758   const Libgav1StatusCode status = mCodecCtx->SignalEOS();
759   if (status != kLibgav1StatusOk) {
760     ALOGE("Failed to flush av1 decoder. status: %d.", status);
761     return C2_CORRUPTED;
762   }
763 
764   while (outputBuffer(pool, work)) {
765   }
766 
767   if (drainMode == DRAIN_COMPONENT_WITH_EOS && work &&
768       work->workletsProcessed == 0u) {
769     fillEmptyWork(work);
770   }
771 
772   return C2_OK;
773 }
774 
drain(uint32_t drainMode,const std::shared_ptr<C2BlockPool> & pool)775 c2_status_t C2SoftGav1Dec::drain(uint32_t drainMode,
776                                  const std::shared_ptr<C2BlockPool> &pool) {
777   return drainInternal(drainMode, pool, nullptr);
778 }
779 
780 class C2SoftGav1Factory : public C2ComponentFactory {
781  public:
C2SoftGav1Factory()782   C2SoftGav1Factory()
783       : mHelper(std::static_pointer_cast<C2ReflectorHelper>(
784             GetCodec2PlatformComponentStore()->getParamReflector())) {}
785 
createComponent(c2_node_id_t id,std::shared_ptr<C2Component> * const component,std::function<void (C2Component *)> deleter)786   virtual c2_status_t createComponent(
787       c2_node_id_t id, std::shared_ptr<C2Component> *const component,
788       std::function<void(C2Component *)> deleter) override {
789     *component = std::shared_ptr<C2Component>(
790         new C2SoftGav1Dec(COMPONENT_NAME, id,
791                           std::make_shared<C2SoftGav1Dec::IntfImpl>(mHelper)),
792         deleter);
793     return C2_OK;
794   }
795 
createInterface(c2_node_id_t id,std::shared_ptr<C2ComponentInterface> * const interface,std::function<void (C2ComponentInterface *)> deleter)796   virtual c2_status_t createInterface(
797       c2_node_id_t id, std::shared_ptr<C2ComponentInterface> *const interface,
798       std::function<void(C2ComponentInterface *)> deleter) override {
799     *interface = std::shared_ptr<C2ComponentInterface>(
800         new SimpleInterface<C2SoftGav1Dec::IntfImpl>(
801             COMPONENT_NAME, id,
802             std::make_shared<C2SoftGav1Dec::IntfImpl>(mHelper)),
803         deleter);
804     return C2_OK;
805   }
806 
807   virtual ~C2SoftGav1Factory() override = default;
808 
809  private:
810   std::shared_ptr<C2ReflectorHelper> mHelper;
811 };
812 
813 }  // namespace android
814 
815 __attribute__((cfi_canonical_jump_table))
CreateCodec2Factory()816 extern "C" ::C2ComponentFactory *CreateCodec2Factory() {
817   ALOGV("in %s", __func__);
818   return new ::android::C2SoftGav1Factory();
819 }
820 
821 __attribute__((cfi_canonical_jump_table))
DestroyCodec2Factory(::C2ComponentFactory * factory)822 extern "C" void DestroyCodec2Factory(::C2ComponentFactory *factory) {
823   ALOGV("in %s", __func__);
824   delete factory;
825 }
826