1 /*
2  * Copyright 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "C2SoftAvcEnc"
19 #include <log/log.h>
20 #include <utils/misc.h>
21 
22 #include <media/hardware/VideoAPI.h>
23 #include <media/stagefright/MediaDefs.h>
24 #include <media/stagefright/MediaErrors.h>
25 #include <media/stagefright/MetaData.h>
26 #include <media/stagefright/foundation/AUtils.h>
27 
28 #include <C2Debug.h>
29 #include <C2PlatformSupport.h>
30 #include <Codec2BufferUtils.h>
31 #include <SimpleC2Interface.h>
32 #include <util/C2InterfaceHelper.h>
33 
34 #include "C2SoftAvcEnc.h"
35 #include "ih264e.h"
36 #include "ih264e_error.h"
37 
38 namespace android {
39 
40 class C2SoftAvcEnc::IntfImpl : public C2InterfaceHelper {
41 public:
IntfImpl(const std::shared_ptr<C2ReflectorHelper> & helper)42     explicit IntfImpl(const std::shared_ptr<C2ReflectorHelper> &helper)
43         : C2InterfaceHelper(helper) {
44 
45         setDerivedInstance(this);
46 
47         addParameter(
48                 DefineParam(mInputFormat, C2_NAME_INPUT_STREAM_FORMAT_SETTING)
49                 .withConstValue(new C2StreamFormatConfig::input(0u, C2FormatVideo))
50                 .build());
51 
52         addParameter(
53                 DefineParam(mOutputFormat, C2_NAME_OUTPUT_STREAM_FORMAT_SETTING)
54                 .withConstValue(new C2StreamFormatConfig::output(0u, C2FormatCompressed))
55                 .build());
56 
57         addParameter(
58                 DefineParam(mInputMediaType, C2_NAME_INPUT_PORT_MIME_SETTING)
59                 .withConstValue(AllocSharedString<C2PortMimeConfig::input>(
60                         MEDIA_MIMETYPE_VIDEO_RAW))
61                 .build());
62 
63         addParameter(
64                 DefineParam(mOutputMediaType, C2_NAME_OUTPUT_PORT_MIME_SETTING)
65                 .withConstValue(AllocSharedString<C2PortMimeConfig::output>(
66                         MEDIA_MIMETYPE_VIDEO_AVC))
67                 .build());
68 
69         addParameter(
70                 DefineParam(mUsage, C2_NAME_INPUT_STREAM_USAGE_SETTING)
71                 .withConstValue(new C2StreamUsageTuning::input(
72                         0u, (uint64_t)C2MemoryUsage::CPU_READ))
73                 .build());
74 
75         addParameter(
76                 DefineParam(mSize, C2_NAME_STREAM_VIDEO_SIZE_SETTING)
77                 .withDefault(new C2VideoSizeStreamTuning::input(0u, 320, 240))
78                 .withFields({
79                     C2F(mSize, width).inRange(2, 2560, 2),
80                     C2F(mSize, height).inRange(2, 2560, 2),
81                 })
82                 .withSetter(SizeSetter)
83                 .build());
84 
85         addParameter(
86                 DefineParam(mFrameRate, C2_NAME_STREAM_FRAME_RATE_SETTING)
87                 .withDefault(new C2StreamFrameRateInfo::output(0u, 30.))
88                 // TODO: More restriction?
89                 .withFields({C2F(mFrameRate, value).greaterThan(0.)})
90                 .withSetter(Setter<decltype(*mFrameRate)>::StrictValueWithNoDeps)
91                 .build());
92 
93         addParameter(
94                 DefineParam(mBitrate, C2_NAME_STREAM_BITRATE_SETTING)
95                 .withDefault(new C2BitrateTuning::output(0u, 64000))
96                 .withFields({C2F(mBitrate, value).inRange(4096, 12000000)})
97                 .withSetter(BitrateSetter)
98                 .build());
99 
100         addParameter(
101                 DefineParam(mIntraRefresh, C2_PARAMKEY_INTRA_REFRESH)
102                 .withDefault(new C2StreamIntraRefreshTuning::output(
103                         0u, C2Config::INTRA_REFRESH_DISABLED, 0.))
104                 .withFields({
105                     C2F(mIntraRefresh, mode).oneOf({
106                         C2Config::INTRA_REFRESH_DISABLED, C2Config::INTRA_REFRESH_ARBITRARY }),
107                     C2F(mIntraRefresh, period).any()
108                 })
109                 .withSetter(IntraRefreshSetter)
110                 .build());
111 
112         addParameter(
113                 DefineParam(mProfileLevel, C2_PARAMKEY_PROFILE_LEVEL)
114                 .withDefault(new C2StreamProfileLevelInfo::output(
115                         0u, PROFILE_AVC_CONSTRAINED_BASELINE, LEVEL_AVC_4_1))
116                 .withFields({
117                     C2F(mProfileLevel, profile).oneOf({
118                         PROFILE_AVC_BASELINE,
119                         PROFILE_AVC_CONSTRAINED_BASELINE,
120                         PROFILE_AVC_MAIN,
121                     }),
122                     C2F(mProfileLevel, level).oneOf({
123                         LEVEL_AVC_1,
124                         LEVEL_AVC_1B,
125                         LEVEL_AVC_1_1,
126                         LEVEL_AVC_1_2,
127                         LEVEL_AVC_1_3,
128                         LEVEL_AVC_2,
129                         LEVEL_AVC_2_1,
130                         LEVEL_AVC_2_2,
131                         LEVEL_AVC_3,
132                         LEVEL_AVC_3_1,
133                         LEVEL_AVC_3_2,
134                         LEVEL_AVC_4,
135                         LEVEL_AVC_4_1,
136                         LEVEL_AVC_4_2,
137                         LEVEL_AVC_5,
138                     }),
139                 })
140                 .withSetter(ProfileLevelSetter, mSize, mFrameRate, mBitrate)
141                 .build());
142 
143         addParameter(
144                 DefineParam(mRequestSync, C2_PARAMKEY_REQUEST_SYNC_FRAME)
145                 .withDefault(new C2StreamRequestSyncFrameTuning::output(0u, C2_FALSE))
146                 .withFields({C2F(mRequestSync, value).oneOf({ C2_FALSE, C2_TRUE }) })
147                 .withSetter(Setter<decltype(*mRequestSync)>::NonStrictValueWithNoDeps)
148                 .build());
149 
150         addParameter(
151                 DefineParam(mSyncFramePeriod, C2_PARAMKEY_SYNC_FRAME_INTERVAL)
152                 .withDefault(new C2StreamSyncFrameIntervalTuning::output(0u, 1000000))
153                 .withFields({C2F(mSyncFramePeriod, value).any()})
154                 .withSetter(Setter<decltype(*mSyncFramePeriod)>::StrictValueWithNoDeps)
155                 .build());
156     }
157 
BitrateSetter(bool mayBlock,C2P<C2StreamBitrateInfo::output> & me)158     static C2R BitrateSetter(bool mayBlock, C2P<C2StreamBitrateInfo::output> &me) {
159         (void)mayBlock;
160         C2R res = C2R::Ok();
161         if (me.v.value <= 4096) {
162             me.set().value = 4096;
163         }
164         return res;
165     }
166 
SizeSetter(bool mayBlock,const C2P<C2StreamPictureSizeInfo::input> & oldMe,C2P<C2StreamPictureSizeInfo::input> & me)167     static C2R SizeSetter(bool mayBlock, const C2P<C2StreamPictureSizeInfo::input> &oldMe,
168                           C2P<C2StreamPictureSizeInfo::input> &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 
ProfileLevelSetter(bool mayBlock,C2P<C2StreamProfileLevelInfo::output> & me,const C2P<C2VideoSizeStreamTuning::input> & size,const C2P<C2StreamFrameRateInfo::output> & frameRate,const C2P<C2BitrateTuning::output> & bitrate)182     static C2R ProfileLevelSetter(
183             bool mayBlock,
184             C2P<C2StreamProfileLevelInfo::output> &me,
185             const C2P<C2VideoSizeStreamTuning::input> &size,
186             const C2P<C2StreamFrameRateInfo::output> &frameRate,
187             const C2P<C2BitrateTuning::output> &bitrate) {
188         (void)mayBlock;
189         if (!me.F(me.v.profile).supportsAtAll(me.v.profile)) {
190             me.set().profile = PROFILE_AVC_CONSTRAINED_BASELINE;
191         }
192 
193         struct LevelLimits {
194             C2Config::level_t level;
195             float mbsPerSec;
196             uint64_t mbs;
197             uint32_t bitrate;
198         };
199         constexpr LevelLimits kLimits[] = {
200             { LEVEL_AVC_1,     1485,    99,     64000 },
201             // Decoder does not properly handle level 1b.
202             // { LEVEL_AVC_1B,    1485,   99,   128000 },
203             { LEVEL_AVC_1_1,   3000,   396,    192000 },
204             { LEVEL_AVC_1_2,   6000,   396,    384000 },
205             { LEVEL_AVC_1_3,  11880,   396,    768000 },
206             { LEVEL_AVC_2,    11880,   396,   2000000 },
207             { LEVEL_AVC_2_1,  19800,   792,   4000000 },
208             { LEVEL_AVC_2_2,  20250,  1620,   4000000 },
209             { LEVEL_AVC_3,    40500,  1620,  10000000 },
210             { LEVEL_AVC_3_1, 108000,  3600,  14000000 },
211             { LEVEL_AVC_3_2, 216000,  5120,  20000000 },
212             { LEVEL_AVC_4,   245760,  8192,  20000000 },
213             { LEVEL_AVC_4_1, 245760,  8192,  50000000 },
214             { LEVEL_AVC_4_2, 522240,  8704,  50000000 },
215             { LEVEL_AVC_5,   589824, 22080, 135000000 },
216         };
217 
218         uint64_t mbs = uint64_t((size.v.width + 15) / 16) * ((size.v.height + 15) / 16);
219         float mbsPerSec = float(mbs) * frameRate.v.value;
220 
221         // Check if the supplied level meets the MB / bitrate requirements. If
222         // not, update the level with the lowest level meeting the requirements.
223 
224         bool found = false;
225         // By default needsUpdate = false in case the supplied level does meet
226         // the requirements. For Level 1b, we want to update the level anyway,
227         // so we set it to true in that case.
228         bool needsUpdate = (me.v.level == LEVEL_AVC_1B);
229         for (const LevelLimits &limit : kLimits) {
230             if (mbs <= limit.mbs && mbsPerSec <= limit.mbsPerSec &&
231                     bitrate.v.value <= limit.bitrate) {
232                 // This is the lowest level that meets the requirements, and if
233                 // we haven't seen the supplied level yet, that means we don't
234                 // need the update.
235                 if (needsUpdate) {
236                     ALOGD("Given level %x does not cover current configuration: "
237                           "adjusting to %x", me.v.level, limit.level);
238                     me.set().level = limit.level;
239                 }
240                 found = true;
241                 break;
242             }
243             if (me.v.level == limit.level) {
244                 // We break out of the loop when the lowest feasible level is
245                 // found. The fact that we're here means that our level doesn't
246                 // meet the requirement and needs to be updated.
247                 needsUpdate = true;
248             }
249         }
250         if (!found) {
251             // We set to the highest supported level.
252             me.set().level = LEVEL_AVC_5;
253         }
254 
255         return C2R::Ok();
256     }
257 
IntraRefreshSetter(bool mayBlock,C2P<C2StreamIntraRefreshTuning::output> & me)258     static C2R IntraRefreshSetter(bool mayBlock, C2P<C2StreamIntraRefreshTuning::output> &me) {
259         (void)mayBlock;
260         C2R res = C2R::Ok();
261         if (me.v.period < 1) {
262             me.set().mode = C2Config::INTRA_REFRESH_DISABLED;
263             me.set().period = 0;
264         } else {
265             // only support arbitrary mode (cyclic in our case)
266             me.set().mode = C2Config::INTRA_REFRESH_ARBITRARY;
267         }
268         return res;
269     }
270 
getProfile_l() const271     IV_PROFILE_T getProfile_l() const {
272         switch (mProfileLevel->profile) {
273         case PROFILE_AVC_CONSTRAINED_BASELINE:  [[fallthrough]];
274         case PROFILE_AVC_BASELINE: return IV_PROFILE_BASE;
275         case PROFILE_AVC_MAIN:     return IV_PROFILE_MAIN;
276         default:
277             ALOGD("Unrecognized profile: %x", mProfileLevel->profile);
278             return IV_PROFILE_DEFAULT;
279         }
280     }
281 
getLevel_l() const282     UWORD32 getLevel_l() const {
283         struct Level {
284             C2Config::level_t c2Level;
285             UWORD32 avcLevel;
286         };
287         constexpr Level levels[] = {
288             { LEVEL_AVC_1,   10 },
289             { LEVEL_AVC_1B,   9 },
290             { LEVEL_AVC_1_1, 11 },
291             { LEVEL_AVC_1_2, 12 },
292             { LEVEL_AVC_1_3, 13 },
293             { LEVEL_AVC_2,   20 },
294             { LEVEL_AVC_2_1, 21 },
295             { LEVEL_AVC_2_2, 22 },
296             { LEVEL_AVC_3,   30 },
297             { LEVEL_AVC_3_1, 31 },
298             { LEVEL_AVC_3_2, 32 },
299             { LEVEL_AVC_4,   40 },
300             { LEVEL_AVC_4_1, 41 },
301             { LEVEL_AVC_4_2, 42 },
302             { LEVEL_AVC_5,   50 },
303         };
304         for (const Level &level : levels) {
305             if (mProfileLevel->level == level.c2Level) {
306                 return level.avcLevel;
307             }
308         }
309         ALOGD("Unrecognized level: %x", mProfileLevel->level);
310         return 41;
311     }
getSyncFramePeriod_l() const312     uint32_t getSyncFramePeriod_l() const {
313         if (mSyncFramePeriod->value < 0 || mSyncFramePeriod->value == INT64_MAX) {
314             return 0;
315         }
316         double period = mSyncFramePeriod->value / 1e6 * mFrameRate->value;
317         return (uint32_t)c2_max(c2_min(period + 0.5, double(UINT32_MAX)), 1.);
318     }
319 
320     // unsafe getters
getSize_l() const321     std::shared_ptr<C2StreamPictureSizeInfo::input> getSize_l() const { return mSize; }
getIntraRefresh_l() const322     std::shared_ptr<C2StreamIntraRefreshTuning::output> getIntraRefresh_l() const { return mIntraRefresh; }
getFrameRate_l() const323     std::shared_ptr<C2StreamFrameRateInfo::output> getFrameRate_l() const { return mFrameRate; }
getBitrate_l() const324     std::shared_ptr<C2StreamBitrateInfo::output> getBitrate_l() const { return mBitrate; }
getRequestSync_l() const325     std::shared_ptr<C2StreamRequestSyncFrameTuning::output> getRequestSync_l() const { return mRequestSync; }
326 
327 private:
328     std::shared_ptr<C2StreamFormatConfig::input> mInputFormat;
329     std::shared_ptr<C2StreamFormatConfig::output> mOutputFormat;
330     std::shared_ptr<C2PortMimeConfig::input> mInputMediaType;
331     std::shared_ptr<C2PortMimeConfig::output> mOutputMediaType;
332     std::shared_ptr<C2StreamUsageTuning::input> mUsage;
333     std::shared_ptr<C2VideoSizeStreamTuning::input> mSize;
334     std::shared_ptr<C2StreamFrameRateInfo::output> mFrameRate;
335     std::shared_ptr<C2StreamRequestSyncFrameTuning::output> mRequestSync;
336     std::shared_ptr<C2StreamIntraRefreshTuning::output> mIntraRefresh;
337     std::shared_ptr<C2BitrateTuning::output> mBitrate;
338     std::shared_ptr<C2StreamProfileLevelInfo::output> mProfileLevel;
339     std::shared_ptr<C2StreamSyncFrameIntervalTuning::output> mSyncFramePeriod;
340 };
341 
342 #define ive_api_function  ih264e_api_function
343 
344 constexpr char COMPONENT_NAME[] = "c2.android.avc.encoder";
345 
346 namespace {
347 
348 // From external/libavc/encoder/ih264e_bitstream.h
349 constexpr uint32_t MIN_STREAM_SIZE = 0x800;
350 
GetCPUCoreCount()351 static size_t GetCPUCoreCount() {
352     long cpuCoreCount = 1;
353 #if defined(_SC_NPROCESSORS_ONLN)
354     cpuCoreCount = sysconf(_SC_NPROCESSORS_ONLN);
355 #else
356     // _SC_NPROC_ONLN must be defined...
357     cpuCoreCount = sysconf(_SC_NPROC_ONLN);
358 #endif
359     CHECK(cpuCoreCount >= 1);
360     ALOGV("Number of CPU cores: %ld", cpuCoreCount);
361     return (size_t)cpuCoreCount;
362 }
363 
364 }  // namespace
365 
C2SoftAvcEnc(const char * name,c2_node_id_t id,const std::shared_ptr<IntfImpl> & intfImpl)366 C2SoftAvcEnc::C2SoftAvcEnc(
367         const char *name, c2_node_id_t id, const std::shared_ptr<IntfImpl> &intfImpl)
368     : SimpleC2Component(std::make_shared<SimpleInterface<IntfImpl>>(name, id, intfImpl)),
369       mIntf(intfImpl),
370       mIvVideoColorFormat(IV_YUV_420P),
371       mAVCEncProfile(IV_PROFILE_BASE),
372       mAVCEncLevel(41),
373       mStarted(false),
374       mSawInputEOS(false),
375       mSawOutputEOS(false),
376       mSignalledError(false),
377       mCodecCtx(nullptr),
378       // TODO: output buffer size
379       mOutBufferSize(524288) {
380 
381     // If dump is enabled, then open create an empty file
382     GENERATE_FILE_NAMES();
383     CREATE_DUMP_FILE(mInFile);
384     CREATE_DUMP_FILE(mOutFile);
385 
386     initEncParams();
387 }
388 
~C2SoftAvcEnc()389 C2SoftAvcEnc::~C2SoftAvcEnc() {
390     releaseEncoder();
391 }
392 
onInit()393 c2_status_t C2SoftAvcEnc::onInit() {
394     return C2_OK;
395 }
396 
onStop()397 c2_status_t C2SoftAvcEnc::onStop() {
398     return C2_OK;
399 }
400 
onReset()401 void C2SoftAvcEnc::onReset() {
402     // TODO: use IVE_CMD_CTL_RESET?
403     releaseEncoder();
404     initEncParams();
405 }
406 
onRelease()407 void C2SoftAvcEnc::onRelease() {
408     releaseEncoder();
409 }
410 
onFlush_sm()411 c2_status_t C2SoftAvcEnc::onFlush_sm() {
412     // TODO: use IVE_CMD_CTL_FLUSH?
413     return C2_OK;
414 }
415 
initEncParams()416 void  C2SoftAvcEnc::initEncParams() {
417     mCodecCtx = nullptr;
418     mMemRecords = nullptr;
419     mNumMemRecords = DEFAULT_MEM_REC_CNT;
420     mHeaderGenerated = 0;
421     mNumCores = GetCPUCoreCount();
422     mArch = DEFAULT_ARCH;
423     mSliceMode = DEFAULT_SLICE_MODE;
424     mSliceParam = DEFAULT_SLICE_PARAM;
425     mHalfPelEnable = DEFAULT_HPEL;
426     mIInterval = DEFAULT_I_INTERVAL;
427     mIDRInterval = DEFAULT_IDR_INTERVAL;
428     mDisableDeblkLevel = DEFAULT_DISABLE_DEBLK_LEVEL;
429     mEnableFastSad = DEFAULT_ENABLE_FAST_SAD;
430     mEnableAltRef = DEFAULT_ENABLE_ALT_REF;
431     mEncSpeed = DEFAULT_ENC_SPEED;
432     mIntra4x4 = DEFAULT_INTRA4x4;
433     mConstrainedIntraFlag = DEFAULT_CONSTRAINED_INTRA;
434     mPSNREnable = DEFAULT_PSNR_ENABLE;
435     mReconEnable = DEFAULT_RECON_ENABLE;
436     mEntropyMode = DEFAULT_ENTROPY_MODE;
437     mBframes = DEFAULT_B_FRAMES;
438 
439     gettimeofday(&mTimeStart, nullptr);
440     gettimeofday(&mTimeEnd, nullptr);
441 }
442 
setDimensions()443 c2_status_t C2SoftAvcEnc::setDimensions() {
444     ive_ctl_set_dimensions_ip_t s_dimensions_ip;
445     ive_ctl_set_dimensions_op_t s_dimensions_op;
446     IV_STATUS_T status;
447 
448     s_dimensions_ip.e_cmd = IVE_CMD_VIDEO_CTL;
449     s_dimensions_ip.e_sub_cmd = IVE_CMD_CTL_SET_DIMENSIONS;
450     s_dimensions_ip.u4_ht = mSize->height;
451     s_dimensions_ip.u4_wd = mSize->width;
452 
453     s_dimensions_ip.u4_timestamp_high = -1;
454     s_dimensions_ip.u4_timestamp_low = -1;
455 
456     s_dimensions_ip.u4_size = sizeof(ive_ctl_set_dimensions_ip_t);
457     s_dimensions_op.u4_size = sizeof(ive_ctl_set_dimensions_op_t);
458 
459     status = ive_api_function(mCodecCtx, &s_dimensions_ip, &s_dimensions_op);
460     if (status != IV_SUCCESS) {
461         ALOGE("Unable to set frame dimensions = 0x%x\n",
462                 s_dimensions_op.u4_error_code);
463         return C2_CORRUPTED;
464     }
465     return C2_OK;
466 }
467 
setNumCores()468 c2_status_t C2SoftAvcEnc::setNumCores() {
469     IV_STATUS_T status;
470     ive_ctl_set_num_cores_ip_t s_num_cores_ip;
471     ive_ctl_set_num_cores_op_t s_num_cores_op;
472     s_num_cores_ip.e_cmd = IVE_CMD_VIDEO_CTL;
473     s_num_cores_ip.e_sub_cmd = IVE_CMD_CTL_SET_NUM_CORES;
474     s_num_cores_ip.u4_num_cores = MIN(mNumCores, CODEC_MAX_CORES);
475     s_num_cores_ip.u4_timestamp_high = -1;
476     s_num_cores_ip.u4_timestamp_low = -1;
477     s_num_cores_ip.u4_size = sizeof(ive_ctl_set_num_cores_ip_t);
478 
479     s_num_cores_op.u4_size = sizeof(ive_ctl_set_num_cores_op_t);
480 
481     status = ive_api_function(
482             mCodecCtx, (void *) &s_num_cores_ip, (void *) &s_num_cores_op);
483     if (status != IV_SUCCESS) {
484         ALOGE("Unable to set processor params = 0x%x\n",
485                 s_num_cores_op.u4_error_code);
486         return C2_CORRUPTED;
487     }
488     return C2_OK;
489 }
490 
setFrameRate()491 c2_status_t C2SoftAvcEnc::setFrameRate() {
492     ive_ctl_set_frame_rate_ip_t s_frame_rate_ip;
493     ive_ctl_set_frame_rate_op_t s_frame_rate_op;
494     IV_STATUS_T status;
495 
496     s_frame_rate_ip.e_cmd = IVE_CMD_VIDEO_CTL;
497     s_frame_rate_ip.e_sub_cmd = IVE_CMD_CTL_SET_FRAMERATE;
498 
499     s_frame_rate_ip.u4_src_frame_rate = mFrameRate->value + 0.5;
500     s_frame_rate_ip.u4_tgt_frame_rate = mFrameRate->value + 0.5;
501 
502     s_frame_rate_ip.u4_timestamp_high = -1;
503     s_frame_rate_ip.u4_timestamp_low = -1;
504 
505     s_frame_rate_ip.u4_size = sizeof(ive_ctl_set_frame_rate_ip_t);
506     s_frame_rate_op.u4_size = sizeof(ive_ctl_set_frame_rate_op_t);
507 
508     status = ive_api_function(mCodecCtx, &s_frame_rate_ip, &s_frame_rate_op);
509     if (status != IV_SUCCESS) {
510         ALOGE("Unable to set frame rate = 0x%x\n",
511                 s_frame_rate_op.u4_error_code);
512         return C2_CORRUPTED;
513     }
514     return C2_OK;
515 }
516 
setIpeParams()517 c2_status_t C2SoftAvcEnc::setIpeParams() {
518     ive_ctl_set_ipe_params_ip_t s_ipe_params_ip;
519     ive_ctl_set_ipe_params_op_t s_ipe_params_op;
520     IV_STATUS_T status;
521 
522     s_ipe_params_ip.e_cmd = IVE_CMD_VIDEO_CTL;
523     s_ipe_params_ip.e_sub_cmd = IVE_CMD_CTL_SET_IPE_PARAMS;
524 
525     s_ipe_params_ip.u4_enable_intra_4x4 = mIntra4x4;
526     s_ipe_params_ip.u4_enc_speed_preset = mEncSpeed;
527     s_ipe_params_ip.u4_constrained_intra_pred = mConstrainedIntraFlag;
528 
529     s_ipe_params_ip.u4_timestamp_high = -1;
530     s_ipe_params_ip.u4_timestamp_low = -1;
531 
532     s_ipe_params_ip.u4_size = sizeof(ive_ctl_set_ipe_params_ip_t);
533     s_ipe_params_op.u4_size = sizeof(ive_ctl_set_ipe_params_op_t);
534 
535     status = ive_api_function(mCodecCtx, &s_ipe_params_ip, &s_ipe_params_op);
536     if (status != IV_SUCCESS) {
537         ALOGE("Unable to set ipe params = 0x%x\n",
538                 s_ipe_params_op.u4_error_code);
539         return C2_CORRUPTED;
540     }
541     return C2_OK;
542 }
543 
setBitRate()544 c2_status_t C2SoftAvcEnc::setBitRate() {
545     ive_ctl_set_bitrate_ip_t s_bitrate_ip;
546     ive_ctl_set_bitrate_op_t s_bitrate_op;
547     IV_STATUS_T status;
548 
549     s_bitrate_ip.e_cmd = IVE_CMD_VIDEO_CTL;
550     s_bitrate_ip.e_sub_cmd = IVE_CMD_CTL_SET_BITRATE;
551 
552     s_bitrate_ip.u4_target_bitrate = mBitrate->value;
553 
554     s_bitrate_ip.u4_timestamp_high = -1;
555     s_bitrate_ip.u4_timestamp_low = -1;
556 
557     s_bitrate_ip.u4_size = sizeof(ive_ctl_set_bitrate_ip_t);
558     s_bitrate_op.u4_size = sizeof(ive_ctl_set_bitrate_op_t);
559 
560     status = ive_api_function(mCodecCtx, &s_bitrate_ip, &s_bitrate_op);
561     if (status != IV_SUCCESS) {
562         ALOGE("Unable to set bit rate = 0x%x\n", s_bitrate_op.u4_error_code);
563         return C2_CORRUPTED;
564     }
565     return C2_OK;
566 }
567 
setFrameType(IV_PICTURE_CODING_TYPE_T e_frame_type)568 c2_status_t C2SoftAvcEnc::setFrameType(IV_PICTURE_CODING_TYPE_T e_frame_type) {
569     ive_ctl_set_frame_type_ip_t s_frame_type_ip;
570     ive_ctl_set_frame_type_op_t s_frame_type_op;
571     IV_STATUS_T status;
572     s_frame_type_ip.e_cmd = IVE_CMD_VIDEO_CTL;
573     s_frame_type_ip.e_sub_cmd = IVE_CMD_CTL_SET_FRAMETYPE;
574 
575     s_frame_type_ip.e_frame_type = e_frame_type;
576 
577     s_frame_type_ip.u4_timestamp_high = -1;
578     s_frame_type_ip.u4_timestamp_low = -1;
579 
580     s_frame_type_ip.u4_size = sizeof(ive_ctl_set_frame_type_ip_t);
581     s_frame_type_op.u4_size = sizeof(ive_ctl_set_frame_type_op_t);
582 
583     status = ive_api_function(mCodecCtx, &s_frame_type_ip, &s_frame_type_op);
584     if (status != IV_SUCCESS) {
585         ALOGE("Unable to set frame type = 0x%x\n",
586                 s_frame_type_op.u4_error_code);
587         return C2_CORRUPTED;
588     }
589     return C2_OK;
590 }
591 
setQp()592 c2_status_t C2SoftAvcEnc::setQp() {
593     ive_ctl_set_qp_ip_t s_qp_ip;
594     ive_ctl_set_qp_op_t s_qp_op;
595     IV_STATUS_T status;
596 
597     s_qp_ip.e_cmd = IVE_CMD_VIDEO_CTL;
598     s_qp_ip.e_sub_cmd = IVE_CMD_CTL_SET_QP;
599 
600     s_qp_ip.u4_i_qp = DEFAULT_I_QP;
601     s_qp_ip.u4_i_qp_max = DEFAULT_QP_MAX;
602     s_qp_ip.u4_i_qp_min = DEFAULT_QP_MIN;
603 
604     s_qp_ip.u4_p_qp = DEFAULT_P_QP;
605     s_qp_ip.u4_p_qp_max = DEFAULT_QP_MAX;
606     s_qp_ip.u4_p_qp_min = DEFAULT_QP_MIN;
607 
608     s_qp_ip.u4_b_qp = DEFAULT_P_QP;
609     s_qp_ip.u4_b_qp_max = DEFAULT_QP_MAX;
610     s_qp_ip.u4_b_qp_min = DEFAULT_QP_MIN;
611 
612     s_qp_ip.u4_timestamp_high = -1;
613     s_qp_ip.u4_timestamp_low = -1;
614 
615     s_qp_ip.u4_size = sizeof(ive_ctl_set_qp_ip_t);
616     s_qp_op.u4_size = sizeof(ive_ctl_set_qp_op_t);
617 
618     status = ive_api_function(mCodecCtx, &s_qp_ip, &s_qp_op);
619     if (status != IV_SUCCESS) {
620         ALOGE("Unable to set qp 0x%x\n", s_qp_op.u4_error_code);
621         return C2_CORRUPTED;
622     }
623     return C2_OK;
624 }
625 
setEncMode(IVE_ENC_MODE_T e_enc_mode)626 c2_status_t C2SoftAvcEnc::setEncMode(IVE_ENC_MODE_T e_enc_mode) {
627     IV_STATUS_T status;
628     ive_ctl_set_enc_mode_ip_t s_enc_mode_ip;
629     ive_ctl_set_enc_mode_op_t s_enc_mode_op;
630 
631     s_enc_mode_ip.e_cmd = IVE_CMD_VIDEO_CTL;
632     s_enc_mode_ip.e_sub_cmd = IVE_CMD_CTL_SET_ENC_MODE;
633 
634     s_enc_mode_ip.e_enc_mode = e_enc_mode;
635 
636     s_enc_mode_ip.u4_timestamp_high = -1;
637     s_enc_mode_ip.u4_timestamp_low = -1;
638 
639     s_enc_mode_ip.u4_size = sizeof(ive_ctl_set_enc_mode_ip_t);
640     s_enc_mode_op.u4_size = sizeof(ive_ctl_set_enc_mode_op_t);
641 
642     status = ive_api_function(mCodecCtx, &s_enc_mode_ip, &s_enc_mode_op);
643     if (status != IV_SUCCESS) {
644         ALOGE("Unable to set in header encode mode = 0x%x\n",
645                 s_enc_mode_op.u4_error_code);
646         return C2_CORRUPTED;
647     }
648     return C2_OK;
649 }
650 
setVbvParams()651 c2_status_t C2SoftAvcEnc::setVbvParams() {
652     ive_ctl_set_vbv_params_ip_t s_vbv_ip;
653     ive_ctl_set_vbv_params_op_t s_vbv_op;
654     IV_STATUS_T status;
655 
656     s_vbv_ip.e_cmd = IVE_CMD_VIDEO_CTL;
657     s_vbv_ip.e_sub_cmd = IVE_CMD_CTL_SET_VBV_PARAMS;
658 
659     s_vbv_ip.u4_vbv_buf_size = 0;
660     s_vbv_ip.u4_vbv_buffer_delay = 1000;
661 
662     s_vbv_ip.u4_timestamp_high = -1;
663     s_vbv_ip.u4_timestamp_low = -1;
664 
665     s_vbv_ip.u4_size = sizeof(ive_ctl_set_vbv_params_ip_t);
666     s_vbv_op.u4_size = sizeof(ive_ctl_set_vbv_params_op_t);
667 
668     status = ive_api_function(mCodecCtx, &s_vbv_ip, &s_vbv_op);
669     if (status != IV_SUCCESS) {
670         ALOGE("Unable to set VBV params = 0x%x\n", s_vbv_op.u4_error_code);
671         return C2_CORRUPTED;
672     }
673     return C2_OK;
674 }
675 
setAirParams()676 c2_status_t C2SoftAvcEnc::setAirParams() {
677     ive_ctl_set_air_params_ip_t s_air_ip;
678     ive_ctl_set_air_params_op_t s_air_op;
679     IV_STATUS_T status;
680 
681     s_air_ip.e_cmd = IVE_CMD_VIDEO_CTL;
682     s_air_ip.e_sub_cmd = IVE_CMD_CTL_SET_AIR_PARAMS;
683 
684     s_air_ip.e_air_mode =
685         (mIntraRefresh->mode == C2Config::INTRA_REFRESH_DISABLED || mIntraRefresh->period < 1)
686             ? IVE_AIR_MODE_NONE : IVE_AIR_MODE_CYCLIC;
687     s_air_ip.u4_air_refresh_period = mIntraRefresh->period;
688 
689     s_air_ip.u4_timestamp_high = -1;
690     s_air_ip.u4_timestamp_low = -1;
691 
692     s_air_ip.u4_size = sizeof(ive_ctl_set_air_params_ip_t);
693     s_air_op.u4_size = sizeof(ive_ctl_set_air_params_op_t);
694 
695     status = ive_api_function(mCodecCtx, &s_air_ip, &s_air_op);
696     if (status != IV_SUCCESS) {
697         ALOGE("Unable to set air params = 0x%x\n", s_air_op.u4_error_code);
698         return C2_CORRUPTED;
699     }
700     return C2_OK;
701 }
702 
setMeParams()703 c2_status_t C2SoftAvcEnc::setMeParams() {
704     IV_STATUS_T status;
705     ive_ctl_set_me_params_ip_t s_me_params_ip;
706     ive_ctl_set_me_params_op_t s_me_params_op;
707 
708     s_me_params_ip.e_cmd = IVE_CMD_VIDEO_CTL;
709     s_me_params_ip.e_sub_cmd = IVE_CMD_CTL_SET_ME_PARAMS;
710 
711     s_me_params_ip.u4_enable_fast_sad = mEnableFastSad;
712     s_me_params_ip.u4_enable_alt_ref = mEnableAltRef;
713 
714     s_me_params_ip.u4_enable_hpel = mHalfPelEnable;
715     s_me_params_ip.u4_enable_qpel = DEFAULT_QPEL;
716     s_me_params_ip.u4_me_speed_preset = DEFAULT_ME_SPEED;
717     s_me_params_ip.u4_srch_rng_x = DEFAULT_SRCH_RNG_X;
718     s_me_params_ip.u4_srch_rng_y = DEFAULT_SRCH_RNG_Y;
719 
720     s_me_params_ip.u4_timestamp_high = -1;
721     s_me_params_ip.u4_timestamp_low = -1;
722 
723     s_me_params_ip.u4_size = sizeof(ive_ctl_set_me_params_ip_t);
724     s_me_params_op.u4_size = sizeof(ive_ctl_set_me_params_op_t);
725 
726     status = ive_api_function(mCodecCtx, &s_me_params_ip, &s_me_params_op);
727     if (status != IV_SUCCESS) {
728         ALOGE("Unable to set me params = 0x%x\n", s_me_params_op.u4_error_code);
729         return C2_CORRUPTED;
730     }
731     return C2_OK;
732 }
733 
setGopParams()734 c2_status_t C2SoftAvcEnc::setGopParams() {
735     IV_STATUS_T status;
736     ive_ctl_set_gop_params_ip_t s_gop_params_ip;
737     ive_ctl_set_gop_params_op_t s_gop_params_op;
738 
739     s_gop_params_ip.e_cmd = IVE_CMD_VIDEO_CTL;
740     s_gop_params_ip.e_sub_cmd = IVE_CMD_CTL_SET_GOP_PARAMS;
741 
742     s_gop_params_ip.u4_i_frm_interval = mIInterval;
743     s_gop_params_ip.u4_idr_frm_interval = mIDRInterval;
744 
745     s_gop_params_ip.u4_timestamp_high = -1;
746     s_gop_params_ip.u4_timestamp_low = -1;
747 
748     s_gop_params_ip.u4_size = sizeof(ive_ctl_set_gop_params_ip_t);
749     s_gop_params_op.u4_size = sizeof(ive_ctl_set_gop_params_op_t);
750 
751     status = ive_api_function(mCodecCtx, &s_gop_params_ip, &s_gop_params_op);
752     if (status != IV_SUCCESS) {
753         ALOGE("Unable to set GOP params = 0x%x\n",
754                 s_gop_params_op.u4_error_code);
755         return C2_CORRUPTED;
756     }
757     return C2_OK;
758 }
759 
setProfileParams()760 c2_status_t C2SoftAvcEnc::setProfileParams() {
761     IntfImpl::Lock lock = mIntf->lock();
762 
763     IV_STATUS_T status;
764     ive_ctl_set_profile_params_ip_t s_profile_params_ip;
765     ive_ctl_set_profile_params_op_t s_profile_params_op;
766 
767     s_profile_params_ip.e_cmd = IVE_CMD_VIDEO_CTL;
768     s_profile_params_ip.e_sub_cmd = IVE_CMD_CTL_SET_PROFILE_PARAMS;
769 
770     s_profile_params_ip.e_profile = mIntf->getProfile_l();
771     s_profile_params_ip.u4_entropy_coding_mode = mEntropyMode;
772     s_profile_params_ip.u4_timestamp_high = -1;
773     s_profile_params_ip.u4_timestamp_low = -1;
774 
775     s_profile_params_ip.u4_size = sizeof(ive_ctl_set_profile_params_ip_t);
776     s_profile_params_op.u4_size = sizeof(ive_ctl_set_profile_params_op_t);
777     lock.unlock();
778 
779     status = ive_api_function(mCodecCtx, &s_profile_params_ip, &s_profile_params_op);
780     if (status != IV_SUCCESS) {
781         ALOGE("Unable to set profile params = 0x%x\n",
782                 s_profile_params_op.u4_error_code);
783         return C2_CORRUPTED;
784     }
785     return C2_OK;
786 }
787 
setDeblockParams()788 c2_status_t C2SoftAvcEnc::setDeblockParams() {
789     IV_STATUS_T status;
790     ive_ctl_set_deblock_params_ip_t s_deblock_params_ip;
791     ive_ctl_set_deblock_params_op_t s_deblock_params_op;
792 
793     s_deblock_params_ip.e_cmd = IVE_CMD_VIDEO_CTL;
794     s_deblock_params_ip.e_sub_cmd = IVE_CMD_CTL_SET_DEBLOCK_PARAMS;
795 
796     s_deblock_params_ip.u4_disable_deblock_level = mDisableDeblkLevel;
797 
798     s_deblock_params_ip.u4_timestamp_high = -1;
799     s_deblock_params_ip.u4_timestamp_low = -1;
800 
801     s_deblock_params_ip.u4_size = sizeof(ive_ctl_set_deblock_params_ip_t);
802     s_deblock_params_op.u4_size = sizeof(ive_ctl_set_deblock_params_op_t);
803 
804     status = ive_api_function(mCodecCtx, &s_deblock_params_ip, &s_deblock_params_op);
805     if (status != IV_SUCCESS) {
806         ALOGE("Unable to enable/disable deblock params = 0x%x\n",
807                 s_deblock_params_op.u4_error_code);
808         return C2_CORRUPTED;
809     }
810     return C2_OK;
811 }
812 
logVersion()813 void C2SoftAvcEnc::logVersion() {
814     ive_ctl_getversioninfo_ip_t s_ctl_ip;
815     ive_ctl_getversioninfo_op_t s_ctl_op;
816     UWORD8 au1_buf[512];
817     IV_STATUS_T status;
818 
819     s_ctl_ip.e_cmd = IVE_CMD_VIDEO_CTL;
820     s_ctl_ip.e_sub_cmd = IVE_CMD_CTL_GETVERSION;
821     s_ctl_ip.u4_size = sizeof(ive_ctl_getversioninfo_ip_t);
822     s_ctl_op.u4_size = sizeof(ive_ctl_getversioninfo_op_t);
823     s_ctl_ip.pu1_version = au1_buf;
824     s_ctl_ip.u4_version_bufsize = sizeof(au1_buf);
825 
826     status = ive_api_function(mCodecCtx, (void *) &s_ctl_ip, (void *) &s_ctl_op);
827 
828     if (status != IV_SUCCESS) {
829         ALOGE("Error in getting version: 0x%x", s_ctl_op.u4_error_code);
830     } else {
831         ALOGV("Ittiam encoder version: %s", (char *)s_ctl_ip.pu1_version);
832     }
833     return;
834 }
835 
initEncoder()836 c2_status_t C2SoftAvcEnc::initEncoder() {
837     IV_STATUS_T status;
838     WORD32 level;
839 
840     CHECK(!mStarted);
841 
842     c2_status_t errType = C2_OK;
843 
844     {
845         IntfImpl::Lock lock = mIntf->lock();
846         mSize = mIntf->getSize_l();
847         mBitrate = mIntf->getBitrate_l();
848         mFrameRate = mIntf->getFrameRate_l();
849         mIntraRefresh = mIntf->getIntraRefresh_l();
850         mAVCEncLevel = mIntf->getLevel_l();
851         mIInterval = mIntf->getSyncFramePeriod_l();
852         mIDRInterval = mIntf->getSyncFramePeriod_l();
853     }
854     uint32_t width = mSize->width;
855     uint32_t height = mSize->height;
856 
857     mStride = width;
858 
859     // TODO
860     mIvVideoColorFormat = IV_YUV_420P;
861 
862     ALOGD("Params width %d height %d level %d colorFormat %d", width,
863             height, mAVCEncLevel, mIvVideoColorFormat);
864 
865     /* Getting Number of MemRecords */
866     {
867         iv_num_mem_rec_ip_t s_num_mem_rec_ip;
868         iv_num_mem_rec_op_t s_num_mem_rec_op;
869 
870         s_num_mem_rec_ip.u4_size = sizeof(iv_num_mem_rec_ip_t);
871         s_num_mem_rec_op.u4_size = sizeof(iv_num_mem_rec_op_t);
872 
873         s_num_mem_rec_ip.e_cmd = IV_CMD_GET_NUM_MEM_REC;
874 
875         status = ive_api_function(nullptr, &s_num_mem_rec_ip, &s_num_mem_rec_op);
876 
877         if (status != IV_SUCCESS) {
878             ALOGE("Get number of memory records failed = 0x%x\n",
879                     s_num_mem_rec_op.u4_error_code);
880             return C2_CORRUPTED;
881         }
882 
883         mNumMemRecords = s_num_mem_rec_op.u4_num_mem_rec;
884     }
885 
886     /* Allocate array to hold memory records */
887     if (mNumMemRecords > SIZE_MAX / sizeof(iv_mem_rec_t)) {
888         ALOGE("requested memory size is too big.");
889         return C2_CORRUPTED;
890     }
891     mMemRecords = (iv_mem_rec_t *)malloc(mNumMemRecords * sizeof(iv_mem_rec_t));
892     if (nullptr == mMemRecords) {
893         ALOGE("Unable to allocate memory for hold memory records: Size %zu",
894                 mNumMemRecords * sizeof(iv_mem_rec_t));
895         mSignalledError = true;
896         return C2_CORRUPTED;
897     }
898 
899     {
900         iv_mem_rec_t *ps_mem_rec;
901         ps_mem_rec = mMemRecords;
902         for (size_t i = 0; i < mNumMemRecords; i++) {
903             ps_mem_rec->u4_size = sizeof(iv_mem_rec_t);
904             ps_mem_rec->pv_base = nullptr;
905             ps_mem_rec->u4_mem_size = 0;
906             ps_mem_rec->u4_mem_alignment = 0;
907             ps_mem_rec->e_mem_type = IV_NA_MEM_TYPE;
908 
909             ps_mem_rec++;
910         }
911     }
912 
913     /* Getting MemRecords Attributes */
914     {
915         iv_fill_mem_rec_ip_t s_fill_mem_rec_ip;
916         iv_fill_mem_rec_op_t s_fill_mem_rec_op;
917 
918         s_fill_mem_rec_ip.u4_size = sizeof(iv_fill_mem_rec_ip_t);
919         s_fill_mem_rec_op.u4_size = sizeof(iv_fill_mem_rec_op_t);
920 
921         s_fill_mem_rec_ip.e_cmd = IV_CMD_FILL_NUM_MEM_REC;
922         s_fill_mem_rec_ip.ps_mem_rec = mMemRecords;
923         s_fill_mem_rec_ip.u4_num_mem_rec = mNumMemRecords;
924         s_fill_mem_rec_ip.u4_max_wd = width;
925         s_fill_mem_rec_ip.u4_max_ht = height;
926         s_fill_mem_rec_ip.u4_max_level = mAVCEncLevel;
927         s_fill_mem_rec_ip.e_color_format = DEFAULT_INP_COLOR_FORMAT;
928         s_fill_mem_rec_ip.u4_max_ref_cnt = DEFAULT_MAX_REF_FRM;
929         s_fill_mem_rec_ip.u4_max_reorder_cnt = DEFAULT_MAX_REORDER_FRM;
930         s_fill_mem_rec_ip.u4_max_srch_rng_x = DEFAULT_MAX_SRCH_RANGE_X;
931         s_fill_mem_rec_ip.u4_max_srch_rng_y = DEFAULT_MAX_SRCH_RANGE_Y;
932 
933         status = ive_api_function(nullptr, &s_fill_mem_rec_ip, &s_fill_mem_rec_op);
934 
935         if (status != IV_SUCCESS) {
936             ALOGE("Fill memory records failed = 0x%x\n",
937                     s_fill_mem_rec_op.u4_error_code);
938             return C2_CORRUPTED;
939         }
940     }
941 
942     /* Allocating Memory for Mem Records */
943     {
944         WORD32 total_size;
945         iv_mem_rec_t *ps_mem_rec;
946         total_size = 0;
947         ps_mem_rec = mMemRecords;
948 
949         for (size_t i = 0; i < mNumMemRecords; i++) {
950             ps_mem_rec->pv_base = ive_aligned_malloc(
951                     ps_mem_rec->u4_mem_alignment, ps_mem_rec->u4_mem_size);
952             if (ps_mem_rec->pv_base == nullptr) {
953                 ALOGE("Allocation failure for mem record id %zu size %u\n", i,
954                         ps_mem_rec->u4_mem_size);
955                 return C2_CORRUPTED;
956 
957             }
958             total_size += ps_mem_rec->u4_mem_size;
959 
960             ps_mem_rec++;
961         }
962     }
963 
964     /* Codec Instance Creation */
965     {
966         ive_init_ip_t s_init_ip;
967         ive_init_op_t s_init_op;
968 
969         mCodecCtx = (iv_obj_t *)mMemRecords[0].pv_base;
970         mCodecCtx->u4_size = sizeof(iv_obj_t);
971         mCodecCtx->pv_fxns = (void *)ive_api_function;
972 
973         s_init_ip.u4_size = sizeof(ive_init_ip_t);
974         s_init_op.u4_size = sizeof(ive_init_op_t);
975 
976         s_init_ip.e_cmd = IV_CMD_INIT;
977         s_init_ip.u4_num_mem_rec = mNumMemRecords;
978         s_init_ip.ps_mem_rec = mMemRecords;
979         s_init_ip.u4_max_wd = width;
980         s_init_ip.u4_max_ht = height;
981         s_init_ip.u4_max_ref_cnt = DEFAULT_MAX_REF_FRM;
982         s_init_ip.u4_max_reorder_cnt = DEFAULT_MAX_REORDER_FRM;
983         s_init_ip.u4_max_level = mAVCEncLevel;
984         s_init_ip.e_inp_color_fmt = mIvVideoColorFormat;
985 
986         if (mReconEnable || mPSNREnable) {
987             s_init_ip.u4_enable_recon = 1;
988         } else {
989             s_init_ip.u4_enable_recon = 0;
990         }
991         s_init_ip.e_recon_color_fmt = DEFAULT_RECON_COLOR_FORMAT;
992         s_init_ip.e_rc_mode = DEFAULT_RC_MODE;
993         s_init_ip.u4_max_framerate = DEFAULT_MAX_FRAMERATE;
994         s_init_ip.u4_max_bitrate = DEFAULT_MAX_BITRATE;
995         s_init_ip.u4_num_bframes = mBframes;
996         s_init_ip.e_content_type = IV_PROGRESSIVE;
997         s_init_ip.u4_max_srch_rng_x = DEFAULT_MAX_SRCH_RANGE_X;
998         s_init_ip.u4_max_srch_rng_y = DEFAULT_MAX_SRCH_RANGE_Y;
999         s_init_ip.e_slice_mode = mSliceMode;
1000         s_init_ip.u4_slice_param = mSliceParam;
1001         s_init_ip.e_arch = mArch;
1002         s_init_ip.e_soc = DEFAULT_SOC;
1003 
1004         status = ive_api_function(mCodecCtx, &s_init_ip, &s_init_op);
1005 
1006         if (status != IV_SUCCESS) {
1007             ALOGE("Init encoder failed = 0x%x\n", s_init_op.u4_error_code);
1008             return C2_CORRUPTED;
1009         }
1010     }
1011 
1012     /* Get Codec Version */
1013     logVersion();
1014 
1015     /* set processor details */
1016     setNumCores();
1017 
1018     /* Video control Set Frame dimensions */
1019     setDimensions();
1020 
1021     /* Video control Set Frame rates */
1022     setFrameRate();
1023 
1024     /* Video control Set IPE Params */
1025     setIpeParams();
1026 
1027     /* Video control Set Bitrate */
1028     setBitRate();
1029 
1030     /* Video control Set QP */
1031     setQp();
1032 
1033     /* Video control Set AIR params */
1034     setAirParams();
1035 
1036     /* Video control Set VBV params */
1037     setVbvParams();
1038 
1039     /* Video control Set Motion estimation params */
1040     setMeParams();
1041 
1042     /* Video control Set GOP params */
1043     setGopParams();
1044 
1045     /* Video control Set Deblock params */
1046     setDeblockParams();
1047 
1048     /* Video control Set Profile params */
1049     setProfileParams();
1050 
1051     /* Video control Set in Encode header mode */
1052     setEncMode(IVE_ENC_MODE_HEADER);
1053 
1054     ALOGV("init_codec successfull");
1055 
1056     mSpsPpsHeaderReceived = false;
1057     mStarted = true;
1058 
1059     return C2_OK;
1060 }
1061 
releaseEncoder()1062 c2_status_t C2SoftAvcEnc::releaseEncoder() {
1063     IV_STATUS_T status = IV_SUCCESS;
1064     iv_retrieve_mem_rec_ip_t s_retrieve_mem_ip;
1065     iv_retrieve_mem_rec_op_t s_retrieve_mem_op;
1066     iv_mem_rec_t *ps_mem_rec;
1067 
1068     if (!mStarted) {
1069         return C2_OK;
1070     }
1071 
1072     s_retrieve_mem_ip.u4_size = sizeof(iv_retrieve_mem_rec_ip_t);
1073     s_retrieve_mem_op.u4_size = sizeof(iv_retrieve_mem_rec_op_t);
1074     s_retrieve_mem_ip.e_cmd = IV_CMD_RETRIEVE_MEMREC;
1075     s_retrieve_mem_ip.ps_mem_rec = mMemRecords;
1076 
1077     status = ive_api_function(mCodecCtx, &s_retrieve_mem_ip, &s_retrieve_mem_op);
1078 
1079     if (status != IV_SUCCESS) {
1080         ALOGE("Unable to retrieve memory records = 0x%x\n",
1081                 s_retrieve_mem_op.u4_error_code);
1082         return C2_CORRUPTED;
1083     }
1084 
1085     /* Free memory records */
1086     ps_mem_rec = mMemRecords;
1087     for (size_t i = 0; i < s_retrieve_mem_op.u4_num_mem_rec_filled; i++) {
1088         if (ps_mem_rec) ive_aligned_free(ps_mem_rec->pv_base);
1089         else {
1090             ALOGE("memory record is null.");
1091             return C2_CORRUPTED;
1092         }
1093         ps_mem_rec++;
1094     }
1095 
1096     if (mMemRecords) free(mMemRecords);
1097 
1098     // clear other pointers into the space being free()d
1099     mCodecCtx = nullptr;
1100 
1101     mStarted = false;
1102 
1103     return C2_OK;
1104 }
1105 
setEncodeArgs(ive_video_encode_ip_t * ps_encode_ip,ive_video_encode_op_t * ps_encode_op,const C2GraphicView * const input,uint8_t * base,uint32_t capacity,uint64_t timestamp)1106 c2_status_t C2SoftAvcEnc::setEncodeArgs(
1107         ive_video_encode_ip_t *ps_encode_ip,
1108         ive_video_encode_op_t *ps_encode_op,
1109         const C2GraphicView *const input,
1110         uint8_t *base,
1111         uint32_t capacity,
1112         uint64_t timestamp) {
1113     iv_raw_buf_t *ps_inp_raw_buf;
1114 
1115     ps_inp_raw_buf = &ps_encode_ip->s_inp_buf;
1116     ps_encode_ip->s_out_buf.pv_buf = base;
1117     ps_encode_ip->s_out_buf.u4_bytes = 0;
1118     ps_encode_ip->s_out_buf.u4_bufsize = capacity;
1119     ps_encode_ip->u4_size = sizeof(ive_video_encode_ip_t);
1120     ps_encode_op->u4_size = sizeof(ive_video_encode_op_t);
1121 
1122     ps_encode_ip->e_cmd = IVE_CMD_VIDEO_ENCODE;
1123     ps_encode_ip->pv_bufs = nullptr;
1124     ps_encode_ip->pv_mb_info = nullptr;
1125     ps_encode_ip->pv_pic_info = nullptr;
1126     ps_encode_ip->u4_mb_info_type = 0;
1127     ps_encode_ip->u4_pic_info_type = 0;
1128     ps_encode_ip->u4_is_last = 0;
1129     ps_encode_ip->u4_timestamp_high = timestamp >> 32;
1130     ps_encode_ip->u4_timestamp_low = timestamp & 0xFFFFFFFF;
1131     ps_encode_op->s_out_buf.pv_buf = nullptr;
1132 
1133     /* Initialize color formats */
1134     memset(ps_inp_raw_buf, 0, sizeof(iv_raw_buf_t));
1135     ps_inp_raw_buf->u4_size = sizeof(iv_raw_buf_t);
1136     ps_inp_raw_buf->e_color_fmt = mIvVideoColorFormat;
1137     if (input == nullptr) {
1138         if (mSawInputEOS){
1139             ps_encode_ip->u4_is_last = 1;
1140         }
1141         return C2_OK;
1142     }
1143 
1144     if (input->width() < mSize->width ||
1145         input->height() < mSize->height) {
1146         /* Expect width height to be configured */
1147         ALOGW("unexpected Capacity Aspect %d(%d) x %d(%d)", input->width(),
1148               mSize->width, input->height(), mSize->height);
1149         return C2_BAD_VALUE;
1150     }
1151     ALOGV("width = %d, height = %d", input->width(), input->height());
1152     const C2PlanarLayout &layout = input->layout();
1153     uint8_t *yPlane = const_cast<uint8_t *>(input->data()[C2PlanarLayout::PLANE_Y]);
1154     uint8_t *uPlane = const_cast<uint8_t *>(input->data()[C2PlanarLayout::PLANE_U]);
1155     uint8_t *vPlane = const_cast<uint8_t *>(input->data()[C2PlanarLayout::PLANE_V]);
1156     int32_t yStride = layout.planes[C2PlanarLayout::PLANE_Y].rowInc;
1157     int32_t uStride = layout.planes[C2PlanarLayout::PLANE_U].rowInc;
1158     int32_t vStride = layout.planes[C2PlanarLayout::PLANE_V].rowInc;
1159 
1160     uint32_t width = mSize->width;
1161     uint32_t height = mSize->height;
1162     // width and height are always even (as block size is 16x16)
1163     CHECK_EQ((width & 1u), 0u);
1164     CHECK_EQ((height & 1u), 0u);
1165     size_t yPlaneSize = width * height;
1166 
1167     switch (layout.type) {
1168         case C2PlanarLayout::TYPE_RGB:
1169             [[fallthrough]];
1170         case C2PlanarLayout::TYPE_RGBA: {
1171             ALOGV("yPlaneSize = %zu", yPlaneSize);
1172             MemoryBlock conversionBuffer = mConversionBuffers.fetch(yPlaneSize * 3 / 2);
1173             mConversionBuffersInUse.emplace(conversionBuffer.data(), conversionBuffer);
1174             yPlane = conversionBuffer.data();
1175             uPlane = yPlane + yPlaneSize;
1176             vPlane = uPlane + yPlaneSize / 4;
1177             yStride = width;
1178             uStride = vStride = yStride / 2;
1179             ConvertRGBToPlanarYUV(yPlane, yStride, height, conversionBuffer.size(), *input);
1180             break;
1181         }
1182         case C2PlanarLayout::TYPE_YUV: {
1183             if (!IsYUV420(*input)) {
1184                 ALOGE("input is not YUV420");
1185                 return C2_BAD_VALUE;
1186             }
1187 
1188             if (layout.planes[layout.PLANE_Y].colInc == 1
1189                     && layout.planes[layout.PLANE_U].colInc == 1
1190                     && layout.planes[layout.PLANE_V].colInc == 1
1191                     && uStride == vStride
1192                     && yStride == 2 * vStride) {
1193                 // I420 compatible - already set up above
1194                 break;
1195             }
1196 
1197             // copy to I420
1198             yStride = width;
1199             uStride = vStride = yStride / 2;
1200             MemoryBlock conversionBuffer = mConversionBuffers.fetch(yPlaneSize * 3 / 2);
1201             mConversionBuffersInUse.emplace(conversionBuffer.data(), conversionBuffer);
1202             MediaImage2 img = CreateYUV420PlanarMediaImage2(width, height, yStride, height);
1203             status_t err = ImageCopy(conversionBuffer.data(), &img, *input);
1204             if (err != OK) {
1205                 ALOGE("Buffer conversion failed: %d", err);
1206                 return C2_BAD_VALUE;
1207             }
1208             yPlane = conversionBuffer.data();
1209             uPlane = yPlane + yPlaneSize;
1210             vPlane = uPlane + yPlaneSize / 4;
1211             break;
1212 
1213         }
1214 
1215         case C2PlanarLayout::TYPE_YUVA:
1216             ALOGE("YUVA plane type is not supported");
1217             return C2_BAD_VALUE;
1218 
1219         default:
1220             ALOGE("Unrecognized plane type: %d", layout.type);
1221             return C2_BAD_VALUE;
1222     }
1223 
1224     switch (mIvVideoColorFormat) {
1225         case IV_YUV_420P:
1226         {
1227             // input buffer is supposed to be const but Ittiam API wants bare pointer.
1228             ps_inp_raw_buf->apv_bufs[0] = yPlane;
1229             ps_inp_raw_buf->apv_bufs[1] = uPlane;
1230             ps_inp_raw_buf->apv_bufs[2] = vPlane;
1231 
1232             ps_inp_raw_buf->au4_wd[0] = input->width();
1233             ps_inp_raw_buf->au4_wd[1] = input->width() / 2;
1234             ps_inp_raw_buf->au4_wd[2] = input->width() / 2;
1235 
1236             ps_inp_raw_buf->au4_ht[0] = input->height();
1237             ps_inp_raw_buf->au4_ht[1] = input->height() / 2;
1238             ps_inp_raw_buf->au4_ht[2] = input->height() / 2;
1239 
1240             ps_inp_raw_buf->au4_strd[0] = yStride;
1241             ps_inp_raw_buf->au4_strd[1] = uStride;
1242             ps_inp_raw_buf->au4_strd[2] = vStride;
1243             break;
1244         }
1245 
1246         case IV_YUV_422ILE:
1247         {
1248             // TODO
1249             // ps_inp_raw_buf->apv_bufs[0] = pu1_buf;
1250             // ps_inp_raw_buf->au4_wd[0] = mWidth * 2;
1251             // ps_inp_raw_buf->au4_ht[0] = mHeight;
1252             // ps_inp_raw_buf->au4_strd[0] = mStride * 2;
1253             break;
1254         }
1255 
1256         case IV_YUV_420SP_UV:
1257         case IV_YUV_420SP_VU:
1258         default:
1259         {
1260             ps_inp_raw_buf->apv_bufs[0] = yPlane;
1261             ps_inp_raw_buf->apv_bufs[1] = uPlane;
1262 
1263             ps_inp_raw_buf->au4_wd[0] = input->width();
1264             ps_inp_raw_buf->au4_wd[1] = input->width();
1265 
1266             ps_inp_raw_buf->au4_ht[0] = input->height();
1267             ps_inp_raw_buf->au4_ht[1] = input->height() / 2;
1268 
1269             ps_inp_raw_buf->au4_strd[0] = yStride;
1270             ps_inp_raw_buf->au4_strd[1] = uStride;
1271             break;
1272         }
1273     }
1274     return C2_OK;
1275 }
1276 
process(const std::unique_ptr<C2Work> & work,const std::shared_ptr<C2BlockPool> & pool)1277 void C2SoftAvcEnc::process(
1278         const std::unique_ptr<C2Work> &work,
1279         const std::shared_ptr<C2BlockPool> &pool) {
1280     // Initialize output work
1281     work->result = C2_OK;
1282     work->workletsProcessed = 1u;
1283     work->worklets.front()->output.flags = work->input.flags;
1284 
1285     IV_STATUS_T status;
1286     WORD32 timeDelay, timeTaken;
1287     uint64_t timestamp = work->input.ordinal.timestamp.peekull();
1288 
1289     // Initialize encoder if not already initialized
1290     if (mCodecCtx == nullptr) {
1291         if (C2_OK != initEncoder()) {
1292             ALOGE("Failed to initialize encoder");
1293             mSignalledError = true;
1294             work->result = C2_CORRUPTED;
1295             return;
1296         }
1297     }
1298     if (mSignalledError) {
1299         return;
1300     }
1301 
1302     // while (!mSawOutputEOS && !outQueue.empty()) {
1303     c2_status_t error;
1304     ive_video_encode_ip_t s_encode_ip;
1305     ive_video_encode_op_t s_encode_op;
1306 
1307     if (!mSpsPpsHeaderReceived) {
1308         constexpr uint32_t kHeaderLength = MIN_STREAM_SIZE;
1309         uint8_t header[kHeaderLength];
1310         error = setEncodeArgs(
1311                 &s_encode_ip, &s_encode_op, nullptr, header, kHeaderLength, timestamp);
1312         if (error != C2_OK) {
1313             ALOGE("setEncodeArgs failed: %d", error);
1314             mSignalledError = true;
1315             work->result = C2_CORRUPTED;
1316             return;
1317         }
1318         status = ive_api_function(mCodecCtx, &s_encode_ip, &s_encode_op);
1319 
1320         if (IV_SUCCESS != status) {
1321             ALOGE("Encode header failed = 0x%x\n",
1322                     s_encode_op.u4_error_code);
1323             return;
1324         } else {
1325             ALOGV("Bytes Generated in header %d\n",
1326                     s_encode_op.s_out_buf.u4_bytes);
1327         }
1328 
1329         mSpsPpsHeaderReceived = true;
1330 
1331         std::unique_ptr<C2StreamCsdInfo::output> csd =
1332             C2StreamCsdInfo::output::AllocUnique(s_encode_op.s_out_buf.u4_bytes, 0u);
1333         if (!csd) {
1334             ALOGE("CSD allocation failed");
1335             mSignalledError = true;
1336             work->result = C2_NO_MEMORY;
1337             return;
1338         }
1339         memcpy(csd->m.value, header, s_encode_op.s_out_buf.u4_bytes);
1340         work->worklets.front()->output.configUpdate.push_back(std::move(csd));
1341 
1342         DUMP_TO_FILE(
1343                 mOutFile, csd->m.value, csd->flexCount());
1344     }
1345 
1346     // handle dynamic config parameters
1347     {
1348         IntfImpl::Lock lock = mIntf->lock();
1349         std::shared_ptr<C2StreamIntraRefreshTuning::output> intraRefresh = mIntf->getIntraRefresh_l();
1350         std::shared_ptr<C2StreamBitrateInfo::output> bitrate = mIntf->getBitrate_l();
1351         std::shared_ptr<C2StreamRequestSyncFrameTuning::output> requestSync = mIntf->getRequestSync_l();
1352         lock.unlock();
1353 
1354         if (bitrate != mBitrate) {
1355             mBitrate = bitrate;
1356             setBitRate();
1357         }
1358 
1359         if (intraRefresh != mIntraRefresh) {
1360             mIntraRefresh = intraRefresh;
1361             setAirParams();
1362         }
1363 
1364         if (requestSync != mRequestSync) {
1365             // we can handle IDR immediately
1366             if (requestSync->value) {
1367                 // unset request
1368                 C2StreamRequestSyncFrameTuning::output clearSync(0u, C2_FALSE);
1369                 std::vector<std::unique_ptr<C2SettingResult>> failures;
1370                 mIntf->config({ &clearSync }, C2_MAY_BLOCK, &failures);
1371                 ALOGV("Got sync request");
1372                 setFrameType(IV_IDR_FRAME);
1373             }
1374             mRequestSync = requestSync;
1375         }
1376     }
1377 
1378     if (work->input.flags & C2FrameData::FLAG_END_OF_STREAM) {
1379         mSawInputEOS = true;
1380     }
1381 
1382     /* In normal mode, store inputBufferInfo and this will be returned
1383        when encoder consumes this input */
1384     // if (!mInputDataIsMeta && (inputBufferInfo != NULL)) {
1385     //     for (size_t i = 0; i < MAX_INPUT_BUFFER_HEADERS; i++) {
1386     //         if (NULL == mInputBufferInfo[i]) {
1387     //             mInputBufferInfo[i] = inputBufferInfo;
1388     //             break;
1389     //         }
1390     //     }
1391     // }
1392     std::shared_ptr<const C2GraphicView> view;
1393     std::shared_ptr<C2Buffer> inputBuffer;
1394     if (!work->input.buffers.empty()) {
1395         inputBuffer = work->input.buffers[0];
1396         view = std::make_shared<const C2GraphicView>(
1397                 inputBuffer->data().graphicBlocks().front().map().get());
1398         if (view->error() != C2_OK) {
1399             ALOGE("graphic view map err = %d", view->error());
1400             return;
1401         }
1402     }
1403 
1404     std::shared_ptr<C2LinearBlock> block;
1405 
1406     do {
1407         C2MemoryUsage usage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE };
1408         // TODO: error handling, proper usage, etc.
1409         c2_status_t err = pool->fetchLinearBlock(mOutBufferSize, usage, &block);
1410         if (err != C2_OK) {
1411             ALOGE("fetch linear block err = %d", err);
1412             work->result = err;
1413             return;
1414         }
1415         C2WriteView wView = block->map().get();
1416         if (wView.error() != C2_OK) {
1417             ALOGE("write view map err = %d", wView.error());
1418             work->result = wView.error();
1419             return;
1420         }
1421 
1422         error = setEncodeArgs(
1423                 &s_encode_ip, &s_encode_op, view.get(), wView.base(), wView.capacity(), timestamp);
1424         if (error != C2_OK) {
1425             ALOGE("setEncodeArgs failed : %d", error);
1426             mSignalledError = true;
1427             work->result = error;
1428             return;
1429         }
1430 
1431         // DUMP_TO_FILE(
1432         //         mInFile, s_encode_ip.s_inp_buf.apv_bufs[0],
1433         //         (mHeight * mStride * 3 / 2));
1434 
1435         GETTIME(&mTimeStart, nullptr);
1436         /* Compute time elapsed between end of previous decode()
1437          * to start of current decode() */
1438         TIME_DIFF(mTimeEnd, mTimeStart, timeDelay);
1439         status = ive_api_function(mCodecCtx, &s_encode_ip, &s_encode_op);
1440 
1441         if (IV_SUCCESS != status) {
1442             if ((s_encode_op.u4_error_code & 0xFF) == IH264E_BITSTREAM_BUFFER_OVERFLOW) {
1443                 // TODO: use IVE_CMD_CTL_GETBUFINFO for proper max input size?
1444                 mOutBufferSize *= 2;
1445                 continue;
1446             }
1447             ALOGE("Encode Frame failed = 0x%x\n",
1448                     s_encode_op.u4_error_code);
1449             mSignalledError = true;
1450             work->result = C2_CORRUPTED;
1451             return;
1452         }
1453     } while (IV_SUCCESS != status);
1454 
1455     // Hold input buffer reference
1456     if (inputBuffer) {
1457         mBuffers[s_encode_ip.s_inp_buf.apv_bufs[0]] = inputBuffer;
1458     }
1459 
1460     GETTIME(&mTimeEnd, nullptr);
1461     /* Compute time taken for decode() */
1462     TIME_DIFF(mTimeStart, mTimeEnd, timeTaken);
1463 
1464     ALOGV("timeTaken=%6d delay=%6d numBytes=%6d", timeTaken, timeDelay,
1465             s_encode_op.s_out_buf.u4_bytes);
1466 
1467     void *freed = s_encode_op.s_inp_buf.apv_bufs[0];
1468     /* If encoder frees up an input buffer, mark it as free */
1469     if (freed != nullptr) {
1470         if (mBuffers.count(freed) == 0u) {
1471             ALOGD("buffer not tracked");
1472         } else {
1473             // Release input buffer reference
1474             mBuffers.erase(freed);
1475             mConversionBuffersInUse.erase(freed);
1476         }
1477     }
1478 
1479     work->worklets.front()->output.flags = work->input.flags;
1480     work->worklets.front()->output.ordinal = work->input.ordinal;
1481     work->worklets.front()->output.ordinal.timestamp =
1482         ((uint64_t)s_encode_op.u4_timestamp_high << 32) | s_encode_op.u4_timestamp_low;
1483     work->worklets.front()->output.buffers.clear();
1484 
1485     if (s_encode_op.s_out_buf.u4_bytes) {
1486         std::shared_ptr<C2Buffer> buffer =
1487             createLinearBuffer(block, 0, s_encode_op.s_out_buf.u4_bytes);
1488         if (IV_IDR_FRAME == s_encode_op.u4_encoded_frame_type) {
1489             ALOGV("IDR frame produced");
1490             buffer->setInfo(std::make_shared<C2StreamPictureTypeMaskInfo::output>(
1491                     0u /* stream id */, C2PictureTypeKeyFrame));
1492         }
1493         work->worklets.front()->output.buffers.push_back(buffer);
1494     }
1495 
1496     if (s_encode_op.u4_is_last) {
1497         // outputBufferHeader->nFlags |= OMX_BUFFERFLAG_EOS;
1498         mSawOutputEOS = true;
1499     } else {
1500         // outputBufferHeader->nFlags &= ~OMX_BUFFERFLAG_EOS;
1501     }
1502 }
1503 
drain(uint32_t drainMode,const std::shared_ptr<C2BlockPool> & pool)1504 c2_status_t C2SoftAvcEnc::drain(
1505         uint32_t drainMode,
1506         const std::shared_ptr<C2BlockPool> &pool) {
1507     // TODO: use IVE_CMD_CTL_FLUSH?
1508     (void)drainMode;
1509     (void)pool;
1510     return C2_OK;
1511 }
1512 
1513 
1514 class C2SoftAvcEncFactory : public C2ComponentFactory {
1515 public:
C2SoftAvcEncFactory()1516     C2SoftAvcEncFactory() : mHelper(std::static_pointer_cast<C2ReflectorHelper>(
1517         GetCodec2PlatformComponentStore()->getParamReflector())) {
1518     }
1519 
createComponent(c2_node_id_t id,std::shared_ptr<C2Component> * const component,std::function<void (C2Component *)> deleter)1520     virtual c2_status_t createComponent(
1521             c2_node_id_t id,
1522             std::shared_ptr<C2Component>* const component,
1523             std::function<void(C2Component*)> deleter) override {
1524         *component = std::shared_ptr<C2Component>(
1525                 new C2SoftAvcEnc(COMPONENT_NAME,
1526                                  id,
1527                                  std::make_shared<C2SoftAvcEnc::IntfImpl>(mHelper)),
1528                 deleter);
1529         return C2_OK;
1530     }
1531 
createInterface(c2_node_id_t id,std::shared_ptr<C2ComponentInterface> * const interface,std::function<void (C2ComponentInterface *)> deleter)1532     virtual c2_status_t createInterface(
1533             c2_node_id_t id,
1534             std::shared_ptr<C2ComponentInterface>* const interface,
1535             std::function<void(C2ComponentInterface*)> deleter) override {
1536         *interface = std::shared_ptr<C2ComponentInterface>(
1537                 new SimpleInterface<C2SoftAvcEnc::IntfImpl>(
1538                         COMPONENT_NAME, id, std::make_shared<C2SoftAvcEnc::IntfImpl>(mHelper)),
1539                 deleter);
1540         return C2_OK;
1541     }
1542 
1543     virtual ~C2SoftAvcEncFactory() override = default;
1544 
1545 private:
1546     std::shared_ptr<C2ReflectorHelper> mHelper;
1547 };
1548 
1549 }  // namespace android
1550 
CreateCodec2Factory()1551 extern "C" ::C2ComponentFactory* CreateCodec2Factory() {
1552     ALOGV("in %s", __func__);
1553     return new ::android::C2SoftAvcEncFactory();
1554 }
1555 
DestroyCodec2Factory(::C2ComponentFactory * factory)1556 extern "C" void DestroyCodec2Factory(::C2ComponentFactory* factory) {
1557     ALOGV("in %s", __func__);
1558     delete factory;
1559 }
1560