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 "C2SoftHevcEnc"
19 #include <log/log.h>
20 
21 #include <media/hardware/VideoAPI.h>
22 #include <media/stagefright/MediaDefs.h>
23 #include <media/stagefright/MediaErrors.h>
24 #include <media/stagefright/MetaData.h>
25 #include <media/stagefright/foundation/AUtils.h>
26 
27 #include <C2Debug.h>
28 #include <Codec2Mapper.h>
29 #include <C2PlatformSupport.h>
30 #include <Codec2BufferUtils.h>
31 #include <SimpleC2Interface.h>
32 #include <util/C2InterfaceHelper.h>
33 
34 #include "ihevc_typedefs.h"
35 #include "itt_video_api.h"
36 #include "ihevce_api.h"
37 #include "ihevce_plugin.h"
38 #include "C2SoftHevcEnc.h"
39 
40 namespace android {
41 
42 namespace {
43 
44 constexpr char COMPONENT_NAME[] = "c2.android.hevc.encoder";
45 
ParseGop(const C2StreamGopTuning::output & gop,uint32_t * syncInterval,uint32_t * iInterval,uint32_t * maxBframes)46 void ParseGop(
47         const C2StreamGopTuning::output &gop,
48         uint32_t *syncInterval, uint32_t *iInterval, uint32_t *maxBframes) {
49     uint32_t syncInt = 1;
50     uint32_t iInt = 1;
51     for (size_t i = 0; i < gop.flexCount(); ++i) {
52         const C2GopLayerStruct &layer = gop.m.values[i];
53         if (layer.count == UINT32_MAX) {
54             syncInt = 0;
55         } else if (syncInt <= UINT32_MAX / (layer.count + 1)) {
56             syncInt *= (layer.count + 1);
57         }
58         if ((layer.type_ & I_FRAME) == 0) {
59             if (layer.count == UINT32_MAX) {
60                 iInt = 0;
61             } else if (iInt <= UINT32_MAX / (layer.count + 1)) {
62                 iInt *= (layer.count + 1);
63             }
64         }
65         if (layer.type_ == C2Config::picture_type_t(P_FRAME | B_FRAME) && maxBframes) {
66             *maxBframes = layer.count;
67         }
68     }
69     if (syncInterval) {
70         *syncInterval = syncInt;
71     }
72     if (iInterval) {
73         *iInterval = iInt;
74     }
75 }
76 } // namepsace
77 
78 class C2SoftHevcEnc::IntfImpl : public SimpleInterface<void>::BaseParams {
79   public:
IntfImpl(const std::shared_ptr<C2ReflectorHelper> & helper)80     explicit IntfImpl(const std::shared_ptr<C2ReflectorHelper> &helper)
81         : SimpleInterface<void>::BaseParams(
82                 helper,
83                 COMPONENT_NAME,
84                 C2Component::KIND_ENCODER,
85                 C2Component::DOMAIN_VIDEO,
86                 MEDIA_MIMETYPE_VIDEO_HEVC) {
87         noPrivateBuffers(); // TODO: account for our buffers here
88         noInputReferences();
89         noOutputReferences();
90         noTimeStretch();
91         setDerivedInstance(this);
92 
93         addParameter(
94                 DefineParam(mGop, C2_PARAMKEY_GOP)
95                 .withDefault(C2StreamGopTuning::output::AllocShared(
96                         0 /* flexCount */, 0u /* stream */))
97                 .withFields({C2F(mGop, m.values[0].type_).any(),
98                              C2F(mGop, m.values[0].count).any()})
99                 .withSetter(GopSetter)
100                 .build());
101 
102         addParameter(
103                 DefineParam(mActualInputDelay, C2_PARAMKEY_INPUT_DELAY)
104                 .withDefault(new C2PortActualDelayTuning::input(
105                     DEFAULT_B_FRAMES + DEFAULT_RC_LOOKAHEAD))
106                 .withFields({C2F(mActualInputDelay, value).inRange(
107                     0, MAX_B_FRAMES + MAX_RC_LOOKAHEAD)})
108                 .calculatedAs(InputDelaySetter, mGop)
109                 .build());
110 
111         addParameter(
112                 DefineParam(mAttrib, C2_PARAMKEY_COMPONENT_ATTRIBUTES)
113                 .withConstValue(new C2ComponentAttributesSetting(
114                     C2Component::ATTRIB_IS_TEMPORAL))
115                 .build());
116 
117         addParameter(
118                 DefineParam(mUsage, C2_PARAMKEY_INPUT_STREAM_USAGE)
119                 .withConstValue(new C2StreamUsageTuning::input(
120                         0u, (uint64_t)C2MemoryUsage::CPU_READ))
121                 .build());
122 
123         // matches size limits in codec library
124         addParameter(
125             DefineParam(mSize, C2_PARAMKEY_PICTURE_SIZE)
126                 .withDefault(new C2StreamPictureSizeInfo::input(0u, 64, 64))
127                 .withFields({
128                     C2F(mSize, width).inRange(2, 1920, 2),
129                     C2F(mSize, height).inRange(2, 1088, 2),
130                 })
131                 .withSetter(SizeSetter)
132                 .build());
133 
134         addParameter(
135             DefineParam(mFrameRate, C2_PARAMKEY_FRAME_RATE)
136                 .withDefault(new C2StreamFrameRateInfo::output(0u, 1.))
137                 .withFields({C2F(mFrameRate, value).greaterThan(0.)})
138                 .withSetter(
139                     Setter<decltype(*mFrameRate)>::StrictValueWithNoDeps)
140                 .build());
141 
142         // matches limits in codec library
143         addParameter(
144             DefineParam(mBitrateMode, C2_PARAMKEY_BITRATE_MODE)
145                 .withDefault(new C2StreamBitrateModeTuning::output(
146                         0u, C2Config::BITRATE_VARIABLE))
147                 .withFields({
148                     C2F(mBitrateMode, value).oneOf({
149                         C2Config::BITRATE_CONST,
150                         C2Config::BITRATE_VARIABLE,
151                         C2Config::BITRATE_IGNORE})
152                 })
153                 .withSetter(
154                     Setter<decltype(*mBitrateMode)>::StrictValueWithNoDeps)
155                 .build());
156 
157         addParameter(
158             DefineParam(mBitrate, C2_PARAMKEY_BITRATE)
159                 .withDefault(new C2StreamBitrateInfo::output(0u, 64000))
160                 .withFields({C2F(mBitrate, value).inRange(4096, 12000000)})
161                 .withSetter(BitrateSetter)
162                 .build());
163 
164         // matches levels allowed within codec library
165         addParameter(
166                 DefineParam(mComplexity, C2_PARAMKEY_COMPLEXITY)
167                 .withDefault(new C2StreamComplexityTuning::output(0u, 0))
168                 .withFields({C2F(mComplexity, value).inRange(0, 10)})
169                 .withSetter(Setter<decltype(*mComplexity)>::NonStrictValueWithNoDeps)
170                 .build());
171 
172         addParameter(
173                 DefineParam(mQuality, C2_PARAMKEY_QUALITY)
174                 .withDefault(new C2StreamQualityTuning::output(0u, 80))
175                 .withFields({C2F(mQuality, value).inRange(0, 100)})
176                 .withSetter(Setter<decltype(*mQuality)>::NonStrictValueWithNoDeps)
177                 .build());
178 
179         addParameter(
180             DefineParam(mProfileLevel, C2_PARAMKEY_PROFILE_LEVEL)
181                 .withDefault(new C2StreamProfileLevelInfo::output(
182                     0u, PROFILE_HEVC_MAIN, LEVEL_HEVC_MAIN_1))
183                 .withFields({
184                     C2F(mProfileLevel, profile)
185                         .oneOf({C2Config::PROFILE_HEVC_MAIN,
186                                 C2Config::PROFILE_HEVC_MAIN_STILL}),
187                     C2F(mProfileLevel, level)
188                         .oneOf({LEVEL_HEVC_MAIN_1, LEVEL_HEVC_MAIN_2,
189                                 LEVEL_HEVC_MAIN_2_1, LEVEL_HEVC_MAIN_3,
190                                 LEVEL_HEVC_MAIN_3_1, LEVEL_HEVC_MAIN_4,
191                                 LEVEL_HEVC_MAIN_4_1, LEVEL_HEVC_MAIN_5,
192                                 LEVEL_HEVC_MAIN_5_1, LEVEL_HEVC_MAIN_5_2}),
193                 })
194                 .withSetter(ProfileLevelSetter, mSize, mFrameRate, mBitrate)
195                 .build());
196 
197         addParameter(
198                 DefineParam(mRequestSync, C2_PARAMKEY_REQUEST_SYNC_FRAME)
199                 .withDefault(new C2StreamRequestSyncFrameTuning::output(0u, C2_FALSE))
200                 .withFields({C2F(mRequestSync, value).oneOf({ C2_FALSE, C2_TRUE }) })
201                 .withSetter(Setter<decltype(*mRequestSync)>::NonStrictValueWithNoDeps)
202                 .build());
203 
204         addParameter(
205             DefineParam(mSyncFramePeriod, C2_PARAMKEY_SYNC_FRAME_INTERVAL)
206                 .withDefault(
207                     new C2StreamSyncFrameIntervalTuning::output(0u, 1000000))
208                 .withFields({C2F(mSyncFramePeriod, value).any()})
209                 .withSetter(
210                     Setter<decltype(*mSyncFramePeriod)>::StrictValueWithNoDeps)
211                 .build());
212 
213         addParameter(
214                 DefineParam(mColorAspects, C2_PARAMKEY_COLOR_ASPECTS)
215                 .withDefault(new C2StreamColorAspectsInfo::input(
216                         0u, C2Color::RANGE_UNSPECIFIED, C2Color::PRIMARIES_UNSPECIFIED,
217                         C2Color::TRANSFER_UNSPECIFIED, C2Color::MATRIX_UNSPECIFIED))
218                 .withFields({
219                     C2F(mColorAspects, range).inRange(
220                                 C2Color::RANGE_UNSPECIFIED,     C2Color::RANGE_OTHER),
221                     C2F(mColorAspects, primaries).inRange(
222                                 C2Color::PRIMARIES_UNSPECIFIED, C2Color::PRIMARIES_OTHER),
223                     C2F(mColorAspects, transfer).inRange(
224                                 C2Color::TRANSFER_UNSPECIFIED,  C2Color::TRANSFER_OTHER),
225                     C2F(mColorAspects, matrix).inRange(
226                                 C2Color::MATRIX_UNSPECIFIED,    C2Color::MATRIX_OTHER)
227                 })
228                 .withSetter(ColorAspectsSetter)
229                 .build());
230 
231         addParameter(
232                 DefineParam(mCodedColorAspects, C2_PARAMKEY_VUI_COLOR_ASPECTS)
233                 .withDefault(new C2StreamColorAspectsInfo::output(
234                         0u, C2Color::RANGE_LIMITED, C2Color::PRIMARIES_UNSPECIFIED,
235                         C2Color::TRANSFER_UNSPECIFIED, C2Color::MATRIX_UNSPECIFIED))
236                 .withFields({
237                     C2F(mCodedColorAspects, range).inRange(
238                                 C2Color::RANGE_UNSPECIFIED,     C2Color::RANGE_OTHER),
239                     C2F(mCodedColorAspects, primaries).inRange(
240                                 C2Color::PRIMARIES_UNSPECIFIED, C2Color::PRIMARIES_OTHER),
241                     C2F(mCodedColorAspects, transfer).inRange(
242                                 C2Color::TRANSFER_UNSPECIFIED,  C2Color::TRANSFER_OTHER),
243                     C2F(mCodedColorAspects, matrix).inRange(
244                                 C2Color::MATRIX_UNSPECIFIED,    C2Color::MATRIX_OTHER)
245                 })
246                 .withSetter(CodedColorAspectsSetter, mColorAspects)
247                 .build());
248 
249         addParameter(
250                 DefineParam(mPictureQuantization, C2_PARAMKEY_PICTURE_QUANTIZATION)
251                 .withDefault(C2StreamPictureQuantizationTuning::output::AllocShared(
252                         0 /* flexCount */, 0u /* stream */))
253                 .withFields({C2F(mPictureQuantization, m.values[0].type_).oneOf(
254                                 {C2Config::picture_type_t(I_FRAME),
255                                   C2Config::picture_type_t(P_FRAME),
256                                   C2Config::picture_type_t(B_FRAME)}),
257                              C2F(mPictureQuantization, m.values[0].min).any(),
258                              C2F(mPictureQuantization, m.values[0].max).any()})
259                 .withSetter(PictureQuantizationSetter)
260                 .build());
261     }
262 
InputDelaySetter(bool mayBlock,C2P<C2PortActualDelayTuning::input> & me,const C2P<C2StreamGopTuning::output> & gop)263     static C2R InputDelaySetter(
264             bool mayBlock,
265             C2P<C2PortActualDelayTuning::input> &me,
266             const C2P<C2StreamGopTuning::output> &gop) {
267         (void)mayBlock;
268         uint32_t maxBframes = 0;
269         ParseGop(gop.v, nullptr, nullptr, &maxBframes);
270         me.set().value = maxBframes + DEFAULT_RC_LOOKAHEAD;
271         return C2R::Ok();
272     }
273 
BitrateSetter(bool mayBlock,C2P<C2StreamBitrateInfo::output> & me)274     static C2R BitrateSetter(bool mayBlock,
275                              C2P<C2StreamBitrateInfo::output>& me) {
276         (void)mayBlock;
277         C2R res = C2R::Ok();
278         if (me.v.value < 4096) {
279             me.set().value = 4096;
280         }
281         return res;
282     }
283 
SizeSetter(bool mayBlock,const C2P<C2StreamPictureSizeInfo::input> & oldMe,C2P<C2StreamPictureSizeInfo::input> & me)284     static C2R SizeSetter(bool mayBlock,
285                           const C2P<C2StreamPictureSizeInfo::input>& oldMe,
286                           C2P<C2StreamPictureSizeInfo::input>& me) {
287         (void)mayBlock;
288         C2R res = C2R::Ok();
289         if (!me.F(me.v.width).supportsAtAll(me.v.width)) {
290             res = res.plus(C2SettingResultBuilder::BadValue(me.F(me.v.width)));
291             me.set().width = oldMe.v.width;
292         }
293         if (!me.F(me.v.height).supportsAtAll(me.v.height)) {
294             res = res.plus(C2SettingResultBuilder::BadValue(me.F(me.v.height)));
295             me.set().height = oldMe.v.height;
296         }
297         return res;
298     }
299 
ProfileLevelSetter(bool mayBlock,C2P<C2StreamProfileLevelInfo::output> & me,const C2P<C2StreamPictureSizeInfo::input> & size,const C2P<C2StreamFrameRateInfo::output> & frameRate,const C2P<C2StreamBitrateInfo::output> & bitrate)300     static C2R ProfileLevelSetter(
301             bool mayBlock,
302             C2P<C2StreamProfileLevelInfo::output> &me,
303             const C2P<C2StreamPictureSizeInfo::input> &size,
304             const C2P<C2StreamFrameRateInfo::output> &frameRate,
305             const C2P<C2StreamBitrateInfo::output> &bitrate) {
306         (void)mayBlock;
307         if (!me.F(me.v.profile).supportsAtAll(me.v.profile)) {
308             me.set().profile = PROFILE_HEVC_MAIN;
309         }
310 
311         struct LevelLimits {
312             C2Config::level_t level;
313             uint64_t samplesPerSec;
314             uint64_t samples;
315             uint32_t bitrate;
316         };
317 
318         constexpr LevelLimits kLimits[] = {
319             { LEVEL_HEVC_MAIN_1,       552960,    36864,    128000 },
320             { LEVEL_HEVC_MAIN_2,      3686400,   122880,   1500000 },
321             { LEVEL_HEVC_MAIN_2_1,    7372800,   245760,   3000000 },
322             { LEVEL_HEVC_MAIN_3,     16588800,   552960,   6000000 },
323             { LEVEL_HEVC_MAIN_3_1,   33177600,   983040,  10000000 },
324             { LEVEL_HEVC_MAIN_4,     66846720,  2228224,  12000000 },
325             { LEVEL_HEVC_MAIN_4_1,  133693440,  2228224,  20000000 },
326             { LEVEL_HEVC_MAIN_5,    267386880,  8912896,  25000000 },
327             { LEVEL_HEVC_MAIN_5_1,  534773760,  8912896,  40000000 },
328             { LEVEL_HEVC_MAIN_5_2, 1069547520,  8912896,  60000000 },
329             { LEVEL_HEVC_MAIN_6,   1069547520, 35651584,  60000000 },
330             { LEVEL_HEVC_MAIN_6_1, 2139095040, 35651584, 120000000 },
331             { LEVEL_HEVC_MAIN_6_2, 4278190080, 35651584, 240000000 },
332         };
333 
334         uint64_t samples = size.v.width * size.v.height;
335         uint64_t samplesPerSec = samples * frameRate.v.value;
336 
337         // Check if the supplied level meets the MB / bitrate requirements. If
338         // not, update the level with the lowest level meeting the requirements.
339 
340         bool found = false;
341         // By default needsUpdate = false in case the supplied level does meet
342         // the requirements.
343         bool needsUpdate = false;
344         if (!me.F(me.v.level).supportsAtAll(me.v.level)) {
345             needsUpdate = true;
346         }
347         for (const LevelLimits &limit : kLimits) {
348             if (samples <= limit.samples && samplesPerSec <= limit.samplesPerSec &&
349                     bitrate.v.value <= limit.bitrate) {
350                 // This is the lowest level that meets the requirements, and if
351                 // we haven't seen the supplied level yet, that means we don't
352                 // need the update.
353                 if (needsUpdate) {
354                     ALOGD("Given level %x does not cover current configuration: "
355                           "adjusting to %x", me.v.level, limit.level);
356                     me.set().level = limit.level;
357                 }
358                 found = true;
359                 break;
360             }
361             if (me.v.level == limit.level) {
362                 // We break out of the loop when the lowest feasible level is
363                 // found. The fact that we're here means that our level doesn't
364                 // meet the requirement and needs to be updated.
365                 needsUpdate = true;
366             }
367         }
368         if (!found || me.v.level > LEVEL_HEVC_MAIN_5_2) {
369             // We set to the highest supported level.
370             me.set().level = LEVEL_HEVC_MAIN_5_2;
371         }
372         return C2R::Ok();
373     }
374 
GopSetter(bool mayBlock,C2P<C2StreamGopTuning::output> & me)375     static C2R GopSetter(bool mayBlock, C2P<C2StreamGopTuning::output> &me) {
376         (void)mayBlock;
377         for (size_t i = 0; i < me.v.flexCount(); ++i) {
378             const C2GopLayerStruct &layer = me.v.m.values[0];
379             if (layer.type_ == C2Config::picture_type_t(P_FRAME | B_FRAME)
380                     && layer.count > MAX_B_FRAMES) {
381                 me.set().m.values[i].count = MAX_B_FRAMES;
382             }
383         }
384         return C2R::Ok();
385     }
386 
getProfile_l() const387     UWORD32 getProfile_l() const {
388         switch (mProfileLevel->profile) {
389         case PROFILE_HEVC_MAIN:  [[fallthrough]];
390         case PROFILE_HEVC_MAIN_STILL: return 1;
391         default:
392             ALOGD("Unrecognized profile: %x", mProfileLevel->profile);
393             return 1;
394         }
395     }
396 
getLevel_l() const397     UWORD32 getLevel_l() const {
398         struct Level {
399             C2Config::level_t c2Level;
400             UWORD32 hevcLevel;
401         };
402         constexpr Level levels[] = {
403             { LEVEL_HEVC_MAIN_1,    30 },
404             { LEVEL_HEVC_MAIN_2,    60 },
405             { LEVEL_HEVC_MAIN_2_1,  63 },
406             { LEVEL_HEVC_MAIN_3,    90 },
407             { LEVEL_HEVC_MAIN_3_1,  93 },
408             { LEVEL_HEVC_MAIN_4,   120 },
409             { LEVEL_HEVC_MAIN_4_1, 123 },
410             { LEVEL_HEVC_MAIN_5,   150 },
411             { LEVEL_HEVC_MAIN_5_1, 153 },
412             { LEVEL_HEVC_MAIN_5_2, 156 },
413             { LEVEL_HEVC_MAIN_6,   180 },
414             { LEVEL_HEVC_MAIN_6_1, 183 },
415             { LEVEL_HEVC_MAIN_6_2, 186 },
416         };
417         for (const Level &level : levels) {
418             if (mProfileLevel->level == level.c2Level) {
419                 return level.hevcLevel;
420             }
421         }
422         ALOGD("Unrecognized level: %x", mProfileLevel->level);
423         return 156;
424     }
getSyncFramePeriod_l() const425     uint32_t getSyncFramePeriod_l() const {
426         if (mSyncFramePeriod->value < 0 ||
427             mSyncFramePeriod->value == INT64_MAX) {
428             return 0;
429         }
430         double period = mSyncFramePeriod->value / 1e6 * mFrameRate->value;
431         return (uint32_t)c2_max(c2_min(period + 0.5, double(UINT32_MAX)), 1.);
432     }
433 
getSize_l() const434     std::shared_ptr<C2StreamPictureSizeInfo::input> getSize_l() const {
435         return mSize;
436     }
getFrameRate_l() const437     std::shared_ptr<C2StreamFrameRateInfo::output> getFrameRate_l() const {
438         return mFrameRate;
439     }
getBitrateMode_l() const440     std::shared_ptr<C2StreamBitrateModeTuning::output> getBitrateMode_l() const {
441         return mBitrateMode;
442     }
getBitrate_l() const443     std::shared_ptr<C2StreamBitrateInfo::output> getBitrate_l() const {
444         return mBitrate;
445     }
getRequestSync_l() const446     std::shared_ptr<C2StreamRequestSyncFrameTuning::output> getRequestSync_l() const {
447         return mRequestSync;
448     }
getComplexity_l() const449     std::shared_ptr<C2StreamComplexityTuning::output> getComplexity_l() const {
450         return mComplexity;
451     }
getQuality_l() const452     std::shared_ptr<C2StreamQualityTuning::output> getQuality_l() const {
453         return mQuality;
454     }
getGop_l() const455     std::shared_ptr<C2StreamGopTuning::output> getGop_l() const {
456         return mGop;
457     }
ColorAspectsSetter(bool mayBlock,C2P<C2StreamColorAspectsInfo::input> & me)458     static C2R ColorAspectsSetter(bool mayBlock, C2P<C2StreamColorAspectsInfo::input> &me) {
459         (void)mayBlock;
460         if (me.v.range > C2Color::RANGE_OTHER) {
461                 me.set().range = C2Color::RANGE_OTHER;
462         }
463         if (me.v.primaries > C2Color::PRIMARIES_OTHER) {
464                 me.set().primaries = C2Color::PRIMARIES_OTHER;
465         }
466         if (me.v.transfer > C2Color::TRANSFER_OTHER) {
467                 me.set().transfer = C2Color::TRANSFER_OTHER;
468         }
469         if (me.v.matrix > C2Color::MATRIX_OTHER) {
470                 me.set().matrix = C2Color::MATRIX_OTHER;
471         }
472         return C2R::Ok();
473     }
CodedColorAspectsSetter(bool mayBlock,C2P<C2StreamColorAspectsInfo::output> & me,const C2P<C2StreamColorAspectsInfo::input> & coded)474     static C2R CodedColorAspectsSetter(bool mayBlock, C2P<C2StreamColorAspectsInfo::output> &me,
475                                        const C2P<C2StreamColorAspectsInfo::input> &coded) {
476         (void)mayBlock;
477         me.set().range = coded.v.range;
478         me.set().primaries = coded.v.primaries;
479         me.set().transfer = coded.v.transfer;
480         me.set().matrix = coded.v.matrix;
481         return C2R::Ok();
482     }
PictureQuantizationSetter(bool mayBlock,C2P<C2StreamPictureQuantizationTuning::output> & me)483     static C2R PictureQuantizationSetter(bool mayBlock,
484                                          C2P<C2StreamPictureQuantizationTuning::output> &me) {
485         (void)mayBlock;
486 
487         // these are the ones we're going to set, so want them to default
488         // to the DEFAULT values for the codec
489         int32_t iMin = HEVC_QP_MIN, pMin = HEVC_QP_MIN, bMin = HEVC_QP_MIN;
490         int32_t iMax = HEVC_QP_MAX, pMax = HEVC_QP_MAX, bMax = HEVC_QP_MAX;
491 
492         for (size_t i = 0; i < me.v.flexCount(); ++i) {
493             const C2PictureQuantizationStruct &layer = me.v.m.values[i];
494 
495             // layerMin is clamped to [HEVC_QP_MIN, layerMax] to avoid error
496             // cases where layer.min > layer.max
497             int32_t layerMax = std::clamp(layer.max, HEVC_QP_MIN, HEVC_QP_MAX);
498             int32_t layerMin = std::clamp(layer.min, HEVC_QP_MIN, layerMax);
499             if (layer.type_ == C2Config::picture_type_t(I_FRAME)) {
500                 iMax = layerMax;
501                 iMin = layerMin;
502                 ALOGV("iMin %d iMax %d", iMin, iMax);
503             } else if (layer.type_ == C2Config::picture_type_t(P_FRAME)) {
504                 pMax = layerMax;
505                 pMin = layerMin;
506                 ALOGV("pMin %d pMax %d", pMin, pMax);
507             } else if (layer.type_ == C2Config::picture_type_t(B_FRAME)) {
508                 bMax = layerMax;
509                 bMin = layerMin;
510                 ALOGV("bMin %d bMax %d", bMin, bMax);
511             }
512         }
513 
514         ALOGV("PictureQuantizationSetter(entry): i %d-%d p %d-%d b %d-%d",
515               iMin, iMax, pMin, pMax, bMin, bMax);
516 
517         int32_t maxFrameQP = std::min(std::min(iMax, pMax), bMax);
518         int32_t minFrameQP = std::max(std::max(iMin, pMin), bMin);
519         if (minFrameQP > maxFrameQP) {
520             minFrameQP = maxFrameQP;
521         }
522 
523         // put them back into the structure
524         for (size_t i = 0; i < me.v.flexCount(); ++i) {
525             const C2PictureQuantizationStruct &layer = me.v.m.values[i];
526 
527             if (layer.type_ == C2Config::picture_type_t(I_FRAME) ||
528                 layer.type_ == C2Config::picture_type_t(P_FRAME) ||
529                 layer.type_ == C2Config::picture_type_t(B_FRAME)) {
530                 me.set().m.values[i].max = maxFrameQP;
531                 me.set().m.values[i].min = minFrameQP;
532             }
533         }
534 
535         ALOGV("PictureQuantizationSetter(exit): i = p = b = %d-%d",
536               minFrameQP, maxFrameQP);
537 
538         return C2R::Ok();
539     }
getCodedColorAspects_l()540     std::shared_ptr<C2StreamColorAspectsInfo::output> getCodedColorAspects_l() {
541         return mCodedColorAspects;
542     }
getPictureQuantization_l() const543     std::shared_ptr<C2StreamPictureQuantizationTuning::output> getPictureQuantization_l() const {
544         return mPictureQuantization;
545     }
546 
547    private:
548     std::shared_ptr<C2StreamUsageTuning::input> mUsage;
549     std::shared_ptr<C2StreamPictureSizeInfo::input> mSize;
550     std::shared_ptr<C2StreamFrameRateInfo::output> mFrameRate;
551     std::shared_ptr<C2StreamRequestSyncFrameTuning::output> mRequestSync;
552     std::shared_ptr<C2StreamBitrateInfo::output> mBitrate;
553     std::shared_ptr<C2StreamBitrateModeTuning::output> mBitrateMode;
554     std::shared_ptr<C2StreamComplexityTuning::output> mComplexity;
555     std::shared_ptr<C2StreamQualityTuning::output> mQuality;
556     std::shared_ptr<C2StreamProfileLevelInfo::output> mProfileLevel;
557     std::shared_ptr<C2StreamSyncFrameIntervalTuning::output> mSyncFramePeriod;
558     std::shared_ptr<C2StreamGopTuning::output> mGop;
559     std::shared_ptr<C2StreamColorAspectsInfo::input> mColorAspects;
560     std::shared_ptr<C2StreamColorAspectsInfo::output> mCodedColorAspects;
561     std::shared_ptr<C2StreamPictureQuantizationTuning::output> mPictureQuantization;
562 };
563 
GetCPUCoreCount()564 static size_t GetCPUCoreCount() {
565     long cpuCoreCount = 0;
566 
567 #if defined(_SC_NPROCESSORS_ONLN)
568     cpuCoreCount = sysconf(_SC_NPROCESSORS_ONLN);
569 #else
570     // _SC_NPROC_ONLN must be defined...
571     cpuCoreCount = sysconf(_SC_NPROC_ONLN);
572 #endif
573 
574     if (cpuCoreCount < 1)
575         cpuCoreCount = 1;
576     return (size_t)cpuCoreCount;
577 }
578 
C2SoftHevcEnc(const char * name,c2_node_id_t id,const std::shared_ptr<IntfImpl> & intfImpl)579 C2SoftHevcEnc::C2SoftHevcEnc(const char* name, c2_node_id_t id,
580                              const std::shared_ptr<IntfImpl>& intfImpl)
581     : SimpleC2Component(
582           std::make_shared<SimpleInterface<IntfImpl>>(name, id, intfImpl)),
583       mIntf(intfImpl),
584       mIvVideoColorFormat(IV_YUV_420P),
585       mHevcEncProfile(1),
586       mHevcEncLevel(30),
587       mStarted(false),
588       mSpsPpsHeaderReceived(false),
589       mSignalledEos(false),
590       mSignalledError(false),
591       mCodecCtx(nullptr) {
592     // If dump is enabled, then create an empty file
593     GENERATE_FILE_NAMES();
594     CREATE_DUMP_FILE(mInFile);
595     CREATE_DUMP_FILE(mOutFile);
596 
597     mTimeStart = mTimeEnd = systemTime();
598 }
599 
~C2SoftHevcEnc()600 C2SoftHevcEnc::~C2SoftHevcEnc() {
601     onRelease();
602 }
603 
onInit()604 c2_status_t C2SoftHevcEnc::onInit() {
605     return C2_OK;
606 }
607 
onStop()608 c2_status_t C2SoftHevcEnc::onStop() {
609     return C2_OK;
610 }
611 
onReset()612 void C2SoftHevcEnc::onReset() {
613     releaseEncoder();
614 }
615 
onRelease()616 void C2SoftHevcEnc::onRelease() {
617     releaseEncoder();
618 }
619 
onFlush_sm()620 c2_status_t C2SoftHevcEnc::onFlush_sm() {
621     return C2_OK;
622 }
623 
fillEmptyWork(const std::unique_ptr<C2Work> & work)624 static void fillEmptyWork(const std::unique_ptr<C2Work>& work) {
625     uint32_t flags = 0;
626     if (work->input.flags & C2FrameData::FLAG_END_OF_STREAM) {
627         flags |= C2FrameData::FLAG_END_OF_STREAM;
628         ALOGV("Signalling EOS");
629     }
630     work->worklets.front()->output.flags = (C2FrameData::flags_t)flags;
631     work->worklets.front()->output.buffers.clear();
632     work->worklets.front()->output.ordinal = work->input.ordinal;
633     work->workletsProcessed = 1u;
634 }
635 
getQpFromQuality(int quality)636 static int getQpFromQuality(int quality) {
637     int qp;
638 #define MIN_QP 4
639 #define MAX_QP 50
640     /* Quality: 100 -> Qp : MIN_QP
641      * Quality: 0 -> Qp : MAX_QP
642      * Qp = ((MIN_QP - MAX_QP) * quality / 100) + MAX_QP;
643      */
644     qp = ((MIN_QP - MAX_QP) * quality / 100) + MAX_QP;
645     qp = std::min(qp, MAX_QP);
646     qp = std::max(qp, MIN_QP);
647     return qp;
648 }
initEncParams()649 c2_status_t C2SoftHevcEnc::initEncParams() {
650     mCodecCtx = nullptr;
651     mNumCores = std::min(GetCPUCoreCount(), (size_t) CODEC_MAX_CORES);
652     memset(&mEncParams, 0, sizeof(ihevce_static_cfg_params_t));
653 
654     // default configuration
655     IHEVCE_PLUGIN_STATUS_T err = ihevce_set_def_params(&mEncParams);
656     if (IHEVCE_EOK != err) {
657         ALOGE("HEVC default init failed : 0x%x", err);
658         return C2_CORRUPTED;
659     }
660     mBframes = 0;
661     if (mGop && mGop->flexCount() > 0) {
662         uint32_t syncInterval = 1;
663         uint32_t iInterval = 1;
664         uint32_t maxBframes = 0;
665         ParseGop(*mGop, &syncInterval, &iInterval, &maxBframes);
666         if (syncInterval > 0) {
667             ALOGD("Updating IDR interval from GOP: old %u new %u", mIDRInterval, syncInterval);
668             mIDRInterval = syncInterval;
669         }
670         if (iInterval > 0) {
671             ALOGD("Updating I interval from GOP: old %u new %u", mIInterval, iInterval);
672             mIInterval = iInterval;
673         }
674         if (mBframes != maxBframes) {
675             ALOGD("Updating max B frames from GOP: old %u new %u", mBframes, maxBframes);
676             mBframes = maxBframes;
677         }
678     }
679     ColorAspects sfAspects;
680     if (!C2Mapper::map(mColorAspects->primaries, &sfAspects.mPrimaries)) {
681         sfAspects.mPrimaries = android::ColorAspects::PrimariesUnspecified;
682     }
683     if (!C2Mapper::map(mColorAspects->range, &sfAspects.mRange)) {
684         sfAspects.mRange = android::ColorAspects::RangeUnspecified;
685     }
686     if (!C2Mapper::map(mColorAspects->matrix, &sfAspects.mMatrixCoeffs)) {
687         sfAspects.mMatrixCoeffs = android::ColorAspects::MatrixUnspecified;
688     }
689     if (!C2Mapper::map(mColorAspects->transfer, &sfAspects.mTransfer)) {
690         sfAspects.mTransfer = android::ColorAspects::TransferUnspecified;
691     }
692     int32_t primaries, transfer, matrixCoeffs;
693     bool range;
694     ColorUtils::convertCodecColorAspectsToIsoAspects(sfAspects,
695             &primaries,
696             &transfer,
697             &matrixCoeffs,
698             &range);
699     mEncParams.s_out_strm_prms.i4_vui_enable = 1;
700     mEncParams.s_vui_sei_prms.u1_colour_description_present_flag = 1;
701     mEncParams.s_vui_sei_prms.u1_colour_primaries = primaries;
702     mEncParams.s_vui_sei_prms.u1_transfer_characteristics = transfer;
703     mEncParams.s_vui_sei_prms.u1_matrix_coefficients = matrixCoeffs;
704     mEncParams.s_vui_sei_prms.u1_video_full_range_flag = range;
705     // update configuration
706     mEncParams.s_src_prms.i4_width = mSize->width;
707     mEncParams.s_src_prms.i4_height = mSize->height;
708     mEncParams.s_src_prms.i4_frm_rate_denom = 1000;
709     mEncParams.s_src_prms.i4_frm_rate_num =
710         mFrameRate->value * mEncParams.s_src_prms.i4_frm_rate_denom;
711     mEncParams.s_tgt_lyr_prms.as_tgt_params[0].i4_quality_preset = IHEVCE_QUALITY_P5;
712     mEncParams.s_tgt_lyr_prms.as_tgt_params[0].ai4_tgt_bitrate[0] =
713         mBitrate->value;
714     mEncParams.s_tgt_lyr_prms.as_tgt_params[0].ai4_peak_bitrate[0] =
715         mBitrate->value << 1;
716     mEncParams.s_tgt_lyr_prms.as_tgt_params[0].i4_codec_level = mHevcEncLevel;
717     mEncParams.s_coding_tools_prms.i4_max_i_open_gop_period = mIDRInterval;
718     mEncParams.s_coding_tools_prms.i4_max_cra_open_gop_period = mIInterval;
719     mIvVideoColorFormat = IV_YUV_420P;
720     mEncParams.s_multi_thrd_prms.i4_max_num_cores = mNumCores;
721     mEncParams.s_out_strm_prms.i4_codec_profile = mHevcEncProfile;
722     mEncParams.s_lap_prms.i4_rc_look_ahead_pics = DEFAULT_RC_LOOKAHEAD;
723     if (mBframes == 0) {
724         mEncParams.s_coding_tools_prms.i4_max_temporal_layers = 0;
725     } else if (mBframes <= 2) {
726         mEncParams.s_coding_tools_prms.i4_max_temporal_layers = 1;
727     } else if (mBframes <= 6) {
728         mEncParams.s_coding_tools_prms.i4_max_temporal_layers = 2;
729     } else {
730         mEncParams.s_coding_tools_prms.i4_max_temporal_layers = 3;
731     }
732 
733     // we resolved out-of-bound and unspecified values in PictureQuantizationSetter()
734     // so we can start with defaults that are overridden as needed.
735     int32_t maxFrameQP = mEncParams.s_config_prms.i4_max_frame_qp;
736     int32_t minFrameQP = mEncParams.s_config_prms.i4_min_frame_qp;
737 
738     for (size_t i = 0; i < mQpBounds->flexCount(); ++i) {
739         const C2PictureQuantizationStruct &layer = mQpBounds->m.values[i];
740 
741         // no need to loop, hevc library takes same range for I/P/B picture type
742         if (layer.type_ == C2Config::picture_type_t(I_FRAME) ||
743             layer.type_ == C2Config::picture_type_t(P_FRAME) ||
744             layer.type_ == C2Config::picture_type_t(B_FRAME)) {
745 
746             maxFrameQP = layer.max;
747             minFrameQP = layer.min;
748             break;
749         }
750     }
751     mEncParams.s_config_prms.i4_max_frame_qp = maxFrameQP;
752     mEncParams.s_config_prms.i4_min_frame_qp = minFrameQP;
753 
754     ALOGV("MaxFrameQp: %d MinFrameQp: %d", maxFrameQP, minFrameQP);
755 
756     mEncParams.s_tgt_lyr_prms.as_tgt_params[0].ai4_frame_qp[0] =
757         std::clamp(kDefaultInitQP, minFrameQP, maxFrameQP);
758 
759     switch (mBitrateMode->value) {
760         case C2Config::BITRATE_IGNORE: {
761             mEncParams.s_config_prms.i4_rate_control_mode = 3;
762             // ensure initial qp values are within our newly configured bounds
763             int32_t frameQp = getQpFromQuality(mQuality->value);
764             mEncParams.s_tgt_lyr_prms.as_tgt_params[0].ai4_frame_qp[0] =
765                 std::clamp(frameQp, minFrameQP, maxFrameQP);
766             break;
767         }
768         case C2Config::BITRATE_CONST:
769             mEncParams.s_config_prms.i4_rate_control_mode = 5;
770             break;
771         case C2Config::BITRATE_VARIABLE:
772             [[fallthrough]];
773         default:
774             mEncParams.s_config_prms.i4_rate_control_mode = 2;
775             break;
776         break;
777     }
778 
779     if (mComplexity->value == 10) {
780         mEncParams.s_tgt_lyr_prms.as_tgt_params[0].i4_quality_preset = IHEVCE_QUALITY_P0;
781     } else if (mComplexity->value >= 8) {
782         mEncParams.s_tgt_lyr_prms.as_tgt_params[0].i4_quality_preset = IHEVCE_QUALITY_P2;
783     } else if (mComplexity->value >= 7) {
784         mEncParams.s_tgt_lyr_prms.as_tgt_params[0].i4_quality_preset = IHEVCE_QUALITY_P3;
785     } else if (mComplexity->value >= 5) {
786         mEncParams.s_tgt_lyr_prms.as_tgt_params[0].i4_quality_preset = IHEVCE_QUALITY_P4;
787     } else {
788         mEncParams.s_tgt_lyr_prms.as_tgt_params[0].i4_quality_preset = IHEVCE_QUALITY_P5;
789     }
790 
791     return C2_OK;
792 }
793 
releaseEncoder()794 c2_status_t C2SoftHevcEnc::releaseEncoder() {
795     mSpsPpsHeaderReceived = false;
796     mSignalledEos = false;
797     mSignalledError = false;
798     mStarted = false;
799 
800     if (mCodecCtx) {
801         IHEVCE_PLUGIN_STATUS_T err = ihevce_close(mCodecCtx);
802         if (IHEVCE_EOK != err) return C2_CORRUPTED;
803         mCodecCtx = nullptr;
804     }
805     return C2_OK;
806 }
807 
drain(uint32_t drainMode,const std::shared_ptr<C2BlockPool> & pool)808 c2_status_t C2SoftHevcEnc::drain(uint32_t drainMode,
809                                  const std::shared_ptr<C2BlockPool>& pool) {
810     return drainInternal(drainMode, pool, nullptr);
811 }
812 
initEncoder()813 c2_status_t C2SoftHevcEnc::initEncoder() {
814     CHECK(!mCodecCtx);
815 
816     {
817         IntfImpl::Lock lock = mIntf->lock();
818         mSize = mIntf->getSize_l();
819         mBitrateMode = mIntf->getBitrateMode_l();
820         mBitrate = mIntf->getBitrate_l();
821         mFrameRate = mIntf->getFrameRate_l();
822         mHevcEncProfile = mIntf->getProfile_l();
823         mHevcEncLevel = mIntf->getLevel_l();
824         mIDRInterval = mIntf->getSyncFramePeriod_l();
825         mIInterval = mIntf->getSyncFramePeriod_l();
826         mComplexity = mIntf->getComplexity_l();
827         mQuality = mIntf->getQuality_l();
828         mGop = mIntf->getGop_l();
829         mRequestSync = mIntf->getRequestSync_l();
830         mColorAspects = mIntf->getCodedColorAspects_l();
831         mQpBounds = mIntf->getPictureQuantization_l();;
832     }
833 
834     c2_status_t status = initEncParams();
835 
836     if (C2_OK != status) {
837         ALOGE("Failed to initialize encoder params : 0x%x", status);
838         mSignalledError = true;
839         return status;
840     }
841 
842     IHEVCE_PLUGIN_STATUS_T err = IHEVCE_EOK;
843     err = ihevce_init(&mEncParams, &mCodecCtx);
844     if (IHEVCE_EOK != err) {
845         ALOGE("HEVC encoder init failed : 0x%x", err);
846         return C2_CORRUPTED;
847     }
848 
849     mStarted = true;
850     return C2_OK;
851 }
852 
setEncodeArgs(ihevce_inp_buf_t * ps_encode_ip,const C2GraphicView * const input,uint64_t workIndex)853 c2_status_t C2SoftHevcEnc::setEncodeArgs(ihevce_inp_buf_t* ps_encode_ip,
854                                          const C2GraphicView* const input,
855                                          uint64_t workIndex) {
856     ihevce_static_cfg_params_t* params = &mEncParams;
857     memset(ps_encode_ip, 0, sizeof(*ps_encode_ip));
858 
859     if (!input) {
860         return C2_OK;
861     }
862 
863     if (input->width() < mSize->width ||
864         input->height() < mSize->height) {
865         /* Expect width height to be configured */
866         ALOGW("unexpected Capacity Aspect %d(%d) x %d(%d)", input->width(),
867               mSize->width, input->height(), mSize->height);
868         return C2_BAD_VALUE;
869     }
870 
871     const C2PlanarLayout& layout = input->layout();
872     uint8_t* yPlane =
873         const_cast<uint8_t *>(input->data()[C2PlanarLayout::PLANE_Y]);
874     uint8_t* uPlane =
875         const_cast<uint8_t *>(input->data()[C2PlanarLayout::PLANE_U]);
876     uint8_t* vPlane =
877         const_cast<uint8_t *>(input->data()[C2PlanarLayout::PLANE_V]);
878     int32_t yStride = layout.planes[C2PlanarLayout::PLANE_Y].rowInc;
879     int32_t uStride = layout.planes[C2PlanarLayout::PLANE_U].rowInc;
880     int32_t vStride = layout.planes[C2PlanarLayout::PLANE_V].rowInc;
881 
882     const uint32_t width = mSize->width;
883     const uint32_t height = mSize->height;
884 
885     // width and height must be even
886     if (width & 1u || height & 1u) {
887         ALOGW("height(%u) and width(%u) must both be even", height, width);
888         return C2_BAD_VALUE;
889     }
890 
891     size_t yPlaneSize = width * height;
892 
893     switch (layout.type) {
894         case C2PlanarLayout::TYPE_RGB:
895             [[fallthrough]];
896         case C2PlanarLayout::TYPE_RGBA: {
897             MemoryBlock conversionBuffer =
898                 mConversionBuffers.fetch(yPlaneSize * 3 / 2);
899             mConversionBuffersInUse.emplace(conversionBuffer.data(),
900                                             conversionBuffer);
901             yPlane = conversionBuffer.data();
902             uPlane = yPlane + yPlaneSize;
903             vPlane = uPlane + yPlaneSize / 4;
904             yStride = width;
905             uStride = vStride = yStride / 2;
906             ConvertRGBToPlanarYUV(yPlane, yStride, height,
907                                   conversionBuffer.size(), *input,
908                                   mColorAspects->matrix, mColorAspects->range);
909             break;
910         }
911         case C2PlanarLayout::TYPE_YUV: {
912             if (!IsYUV420(*input)) {
913                 ALOGE("input is not YUV420");
914                 return C2_BAD_VALUE;
915             }
916 
917             if (layout.planes[layout.PLANE_Y].colInc == 1 &&
918                 layout.planes[layout.PLANE_U].colInc == 1 &&
919                 layout.planes[layout.PLANE_V].colInc == 1 &&
920                 uStride == vStride && yStride == 2 * vStride) {
921                 // I420 compatible - already set up above
922                 break;
923             }
924 
925             // copy to I420
926             yStride = width;
927             uStride = vStride = yStride / 2;
928             MemoryBlock conversionBuffer =
929                 mConversionBuffers.fetch(yPlaneSize * 3 / 2);
930             mConversionBuffersInUse.emplace(conversionBuffer.data(),
931                                             conversionBuffer);
932             MediaImage2 img =
933                 CreateYUV420PlanarMediaImage2(width, height, yStride, height);
934             status_t err = ImageCopy(conversionBuffer.data(), &img, *input);
935             if (err != OK) {
936                 ALOGE("Buffer conversion failed: %d", err);
937                 return C2_BAD_VALUE;
938             }
939             yPlane = conversionBuffer.data();
940             uPlane = yPlane + yPlaneSize;
941             vPlane = uPlane + yPlaneSize / 4;
942             break;
943         }
944 
945         case C2PlanarLayout::TYPE_YUVA:
946             ALOGE("YUVA plane type is not supported");
947             return C2_BAD_VALUE;
948 
949         default:
950             ALOGE("Unrecognized plane type: %d", layout.type);
951             return C2_BAD_VALUE;
952     }
953 
954     switch (mIvVideoColorFormat) {
955         case IV_YUV_420P: {
956             // input buffer is supposed to be const but Ittiam API wants bare
957             // pointer.
958             ps_encode_ip->apv_inp_planes[0] = yPlane;
959             ps_encode_ip->apv_inp_planes[1] = uPlane;
960             ps_encode_ip->apv_inp_planes[2] = vPlane;
961 
962             ps_encode_ip->ai4_inp_strd[0] = yStride;
963             ps_encode_ip->ai4_inp_strd[1] = uStride;
964             ps_encode_ip->ai4_inp_strd[2] = vStride;
965 
966             ps_encode_ip->ai4_inp_size[0] = yStride * height;
967             ps_encode_ip->ai4_inp_size[1] = uStride * height >> 1;
968             ps_encode_ip->ai4_inp_size[2] = vStride * height >> 1;
969             break;
970         }
971 
972         case IV_YUV_422ILE: {
973             // TODO
974             break;
975         }
976 
977         case IV_YUV_420SP_UV:
978         case IV_YUV_420SP_VU:
979         default: {
980             ps_encode_ip->apv_inp_planes[0] = yPlane;
981             ps_encode_ip->apv_inp_planes[1] = uPlane;
982             ps_encode_ip->apv_inp_planes[2] = nullptr;
983 
984             ps_encode_ip->ai4_inp_strd[0] = yStride;
985             ps_encode_ip->ai4_inp_strd[1] = uStride;
986             ps_encode_ip->ai4_inp_strd[2] = 0;
987 
988             ps_encode_ip->ai4_inp_size[0] = yStride * height;
989             ps_encode_ip->ai4_inp_size[1] = uStride * height >> 1;
990             ps_encode_ip->ai4_inp_size[2] = 0;
991             break;
992         }
993     }
994 
995     ps_encode_ip->i4_curr_bitrate =
996         params->s_tgt_lyr_prms.as_tgt_params[0].ai4_tgt_bitrate[0];
997     ps_encode_ip->i4_curr_peak_bitrate =
998         params->s_tgt_lyr_prms.as_tgt_params[0].ai4_peak_bitrate[0];
999     ps_encode_ip->i4_curr_rate_factor = params->s_config_prms.i4_rate_factor;
1000     ps_encode_ip->u8_pts = workIndex;
1001     return C2_OK;
1002 }
1003 
finishWork(uint64_t index,const std::unique_ptr<C2Work> & work,const std::shared_ptr<C2BlockPool> & pool,ihevce_out_buf_t * ps_encode_op)1004 void C2SoftHevcEnc::finishWork(uint64_t index,
1005                                const std::unique_ptr<C2Work>& work,
1006                                const std::shared_ptr<C2BlockPool>& pool,
1007                                ihevce_out_buf_t* ps_encode_op) {
1008     std::shared_ptr<C2LinearBlock> block;
1009     C2MemoryUsage usage = {C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE};
1010     c2_status_t status =
1011         pool->fetchLinearBlock(ps_encode_op->i4_bytes_generated, usage, &block);
1012     if (C2_OK != status) {
1013         ALOGE("fetchLinearBlock for Output failed with status 0x%x", status);
1014         mSignalledError = true;
1015         work->result = status;
1016         work->workletsProcessed = 1u;
1017         return;
1018     }
1019     C2WriteView wView = block->map().get();
1020     if (C2_OK != wView.error()) {
1021         ALOGE("write view map failed with status 0x%x", wView.error());
1022         mSignalledError = true;
1023         work->result = wView.error();
1024         work->workletsProcessed = 1u;
1025         return;
1026     }
1027     memcpy(wView.data(), ps_encode_op->pu1_output_buf,
1028            ps_encode_op->i4_bytes_generated);
1029 
1030     std::shared_ptr<C2Buffer> buffer =
1031         createLinearBuffer(block, 0, ps_encode_op->i4_bytes_generated);
1032 
1033     DUMP_TO_FILE(mOutFile, ps_encode_op->pu1_output_buf,
1034                  ps_encode_op->i4_bytes_generated);
1035 
1036     if (ps_encode_op->i4_is_key_frame) {
1037         ALOGV("IDR frame produced");
1038         buffer->setInfo(std::make_shared<C2StreamPictureTypeMaskInfo::output>(
1039             0u /* stream id */, C2Config::SYNC_FRAME));
1040     }
1041 
1042     auto fillWork = [buffer](const std::unique_ptr<C2Work>& work) {
1043         work->worklets.front()->output.flags = (C2FrameData::flags_t)0;
1044         work->worklets.front()->output.buffers.clear();
1045         work->worklets.front()->output.buffers.push_back(buffer);
1046         work->worklets.front()->output.ordinal = work->input.ordinal;
1047         work->workletsProcessed = 1u;
1048     };
1049     if (work && c2_cntr64_t(index) == work->input.ordinal.frameIndex) {
1050         fillWork(work);
1051         if (mSignalledEos) {
1052             work->worklets.front()->output.flags =
1053                 C2FrameData::FLAG_END_OF_STREAM;
1054         }
1055     } else {
1056         finish(index, fillWork);
1057     }
1058 }
1059 
drainInternal(uint32_t drainMode,const std::shared_ptr<C2BlockPool> & pool,const std::unique_ptr<C2Work> & work)1060 c2_status_t C2SoftHevcEnc::drainInternal(
1061         uint32_t drainMode,
1062         const std::shared_ptr<C2BlockPool> &pool,
1063         const std::unique_ptr<C2Work> &work) {
1064 
1065     if (drainMode == NO_DRAIN) {
1066         ALOGW("drain with NO_DRAIN: no-op");
1067         return C2_OK;
1068     }
1069     if (drainMode == DRAIN_CHAIN) {
1070         ALOGW("DRAIN_CHAIN not supported");
1071         return C2_OMITTED;
1072     }
1073 
1074     while (true) {
1075         ihevce_out_buf_t s_encode_op{};
1076         memset(&s_encode_op, 0, sizeof(s_encode_op));
1077 
1078         ihevce_encode(mCodecCtx, nullptr, &s_encode_op);
1079         if (s_encode_op.i4_bytes_generated) {
1080             finishWork(s_encode_op.u8_pts, work, pool, &s_encode_op);
1081         } else {
1082             if (work->workletsProcessed != 1u) fillEmptyWork(work);
1083             break;
1084         }
1085     }
1086     return C2_OK;
1087 }
1088 
process(const std::unique_ptr<C2Work> & work,const std::shared_ptr<C2BlockPool> & pool)1089 void C2SoftHevcEnc::process(const std::unique_ptr<C2Work>& work,
1090                             const std::shared_ptr<C2BlockPool>& pool) {
1091     // Initialize output work
1092     work->result = C2_OK;
1093     work->workletsProcessed = 0u;
1094     work->worklets.front()->output.flags = work->input.flags;
1095 
1096     if (mSignalledError || mSignalledEos) {
1097         work->result = C2_BAD_VALUE;
1098         ALOGD("Signalled Error / Signalled Eos");
1099         return;
1100     }
1101     c2_status_t status = C2_OK;
1102 
1103     // Initialize encoder if not already initialized
1104     if (!mStarted) {
1105         status = initEncoder();
1106         if (C2_OK != status) {
1107             ALOGE("Failed to initialize encoder : 0x%x", status);
1108             mSignalledError = true;
1109             work->result = status;
1110             work->workletsProcessed = 1u;
1111             return;
1112         }
1113     }
1114 
1115     std::shared_ptr<C2GraphicView> view;
1116     std::shared_ptr<C2Buffer> inputBuffer = nullptr;
1117     bool eos = ((work->input.flags & C2FrameData::FLAG_END_OF_STREAM) != 0);
1118     if (eos) mSignalledEos = true;
1119 
1120     if (!work->input.buffers.empty()) {
1121         inputBuffer = work->input.buffers[0];
1122         view = std::make_shared<C2GraphicView>(
1123             inputBuffer->data().graphicBlocks().front().map().get());
1124         if (view->error() != C2_OK) {
1125             ALOGE("graphic view map err = %d", view->error());
1126             mSignalledError = true;
1127             work->result = C2_CORRUPTED;
1128             work->workletsProcessed = 1u;
1129             return;
1130         }
1131         //(b/232396154)
1132         //workaround for incorrect crop size in view when using surface mode
1133         view->setCrop_be(C2Rect(mSize->width, mSize->height));
1134     }
1135     IHEVCE_PLUGIN_STATUS_T err = IHEVCE_EOK;
1136 
1137     if (!mSpsPpsHeaderReceived) {
1138         ihevce_out_buf_t s_header_op{};
1139         err = ihevce_encode_header(mCodecCtx, &s_header_op);
1140         if (err == IHEVCE_EOK && s_header_op.i4_bytes_generated) {
1141             std::unique_ptr<C2StreamInitDataInfo::output> csd =
1142                 C2StreamInitDataInfo::output::AllocUnique(
1143                     s_header_op.i4_bytes_generated, 0u);
1144             if (!csd) {
1145                 ALOGE("CSD allocation failed");
1146                 mSignalledError = true;
1147                 work->result = C2_NO_MEMORY;
1148                 work->workletsProcessed = 1u;
1149                 return;
1150             }
1151             memcpy(csd->m.value, s_header_op.pu1_output_buf,
1152                    s_header_op.i4_bytes_generated);
1153             DUMP_TO_FILE(mOutFile, csd->m.value, csd->flexCount());
1154             work->worklets.front()->output.configUpdate.push_back(
1155                 std::move(csd));
1156             mSpsPpsHeaderReceived = true;
1157         }
1158         if (!inputBuffer) {
1159             work->workletsProcessed = 1u;
1160             return;
1161         }
1162     }
1163 
1164     // handle dynamic bitrate change
1165     {
1166         IntfImpl::Lock lock = mIntf->lock();
1167         std::shared_ptr<C2StreamBitrateInfo::output> bitrate = mIntf->getBitrate_l();
1168         lock.unlock();
1169 
1170         if (bitrate != mBitrate) {
1171             mBitrate = bitrate;
1172             mEncParams.s_tgt_lyr_prms.as_tgt_params[0].ai4_tgt_bitrate[0] =
1173                 mBitrate->value;
1174             mEncParams.s_tgt_lyr_prms.as_tgt_params[0].ai4_peak_bitrate[0] =
1175                 mBitrate->value << 1;
1176         }
1177     }
1178 
1179     ihevce_inp_buf_t s_encode_ip{};
1180     ihevce_out_buf_t s_encode_op{};
1181     uint64_t workIndex = work->input.ordinal.frameIndex.peekull();
1182 
1183     status = setEncodeArgs(&s_encode_ip, view.get(), workIndex);
1184     if (C2_OK != status) {
1185         ALOGE("setEncodeArgs failed : 0x%x", status);
1186         mSignalledError = true;
1187         work->result = status;
1188         work->workletsProcessed = 1u;
1189         return;
1190     }
1191     // handle request key frame
1192     {
1193         IntfImpl::Lock lock = mIntf->lock();
1194         std::shared_ptr<C2StreamRequestSyncFrameTuning::output> requestSync;
1195         requestSync = mIntf->getRequestSync_l();
1196         lock.unlock();
1197         if (requestSync != mRequestSync) {
1198             // we can handle IDR immediately
1199             if (requestSync->value) {
1200                 // unset request
1201                 C2StreamRequestSyncFrameTuning::output clearSync(0u, C2_FALSE);
1202                 std::vector<std::unique_ptr<C2SettingResult>> failures;
1203                 mIntf->config({ &clearSync }, C2_MAY_BLOCK, &failures);
1204                 ALOGV("Got sync request");
1205                 //Force this as an IDR frame
1206                 s_encode_ip.i4_force_idr_flag = 1;
1207             }
1208             mRequestSync = requestSync;
1209         }
1210     }
1211 
1212     nsecs_t timeDelay = 0;
1213     nsecs_t timeTaken = 0;
1214     memset(&s_encode_op, 0, sizeof(s_encode_op));
1215     mTimeStart = systemTime();
1216     timeDelay = mTimeStart - mTimeEnd;
1217 
1218     if (inputBuffer) {
1219         err = ihevce_encode(mCodecCtx, &s_encode_ip, &s_encode_op);
1220         if (IHEVCE_EOK != err) {
1221             ALOGE("Encode Frame failed : 0x%x", err);
1222             mSignalledError = true;
1223             work->result = C2_CORRUPTED;
1224             work->workletsProcessed = 1u;
1225             return;
1226         }
1227     } else if (!eos) {
1228         fillEmptyWork(work);
1229     }
1230 
1231     /* Compute time taken for decode() */
1232     mTimeEnd = systemTime();
1233     timeTaken = mTimeEnd - mTimeStart;
1234 
1235     ALOGV("timeTaken=%6" PRId64 " delay=%6" PRId64 " numBytes=%6d", timeTaken,
1236           timeDelay, s_encode_op.i4_bytes_generated);
1237 
1238     if (s_encode_op.i4_bytes_generated) {
1239         finishWork(s_encode_op.u8_pts, work, pool, &s_encode_op);
1240     }
1241 
1242     if (eos) {
1243         drainInternal(DRAIN_COMPONENT_WITH_EOS, pool, work);
1244     }
1245 }
1246 
1247 class C2SoftHevcEncFactory : public C2ComponentFactory {
1248    public:
C2SoftHevcEncFactory()1249     C2SoftHevcEncFactory()
1250         : mHelper(std::static_pointer_cast<C2ReflectorHelper>(
1251               GetCodec2PlatformComponentStore()->getParamReflector())) {}
1252 
createComponent(c2_node_id_t id,std::shared_ptr<C2Component> * const component,std::function<void (C2Component *)> deleter)1253     c2_status_t createComponent(
1254         c2_node_id_t id,
1255         std::shared_ptr<C2Component>* const component,
1256         std::function<void(C2Component*)> deleter) override {
1257         *component = std::shared_ptr<C2Component>(
1258             new C2SoftHevcEnc(
1259                 COMPONENT_NAME, id,
1260                 std::make_shared<C2SoftHevcEnc::IntfImpl>(mHelper)),
1261             deleter);
1262         return C2_OK;
1263     }
1264 
createInterface(c2_node_id_t id,std::shared_ptr<C2ComponentInterface> * const interface,std::function<void (C2ComponentInterface *)> deleter)1265     c2_status_t createInterface(
1266         c2_node_id_t id,
1267         std::shared_ptr<C2ComponentInterface>* const interface,
1268         std::function<void(C2ComponentInterface*)> deleter) override {
1269         *interface = std::shared_ptr<C2ComponentInterface>(
1270             new SimpleInterface<C2SoftHevcEnc::IntfImpl>(
1271                 COMPONENT_NAME, id,
1272                 std::make_shared<C2SoftHevcEnc::IntfImpl>(mHelper)),
1273             deleter);
1274         return C2_OK;
1275     }
1276 
1277     ~C2SoftHevcEncFactory() override = default;
1278 
1279    private:
1280     std::shared_ptr<C2ReflectorHelper> mHelper;
1281 };
1282 
1283 }  // namespace android
1284 
1285 __attribute__((cfi_canonical_jump_table))
CreateCodec2Factory()1286 extern "C" ::C2ComponentFactory* CreateCodec2Factory() {
1287     ALOGV("in %s", __func__);
1288     return new ::android::C2SoftHevcEncFactory();
1289 }
1290 
1291 __attribute__((cfi_canonical_jump_table))
DestroyCodec2Factory(::C2ComponentFactory * factory)1292 extern "C" void DestroyCodec2Factory(::C2ComponentFactory* factory) {
1293     ALOGV("in %s", __func__);
1294     delete factory;
1295 }
1296