blob: 7045b6a4945be78a0dd6622707434260817c8dff [file] [log] [blame]
Roma Kauldfe650a2018-08-02 17:48:51 +05301/*
Ray Essick0d11a7e2019-03-02 20:10:30 -08002 * Copyright (C) 2019 The Android Open Source Project
Roma Kauldfe650a2018-08-02 17:48:51 +05303 *
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 <C2PlatformSupport.h>
29#include <Codec2BufferUtils.h>
30#include <SimpleC2Interface.h>
31#include <util/C2InterfaceHelper.h>
32
33#include "ihevc_typedefs.h"
34#include "itt_video_api.h"
35#include "ihevce_api.h"
36#include "ihevce_plugin.h"
37#include "C2SoftHevcEnc.h"
38
39namespace android {
40
41class C2SoftHevcEnc::IntfImpl : public C2InterfaceHelper {
Ray Essick0d11a7e2019-03-02 20:10:30 -080042 public:
Roma Kauldfe650a2018-08-02 17:48:51 +053043 explicit IntfImpl(const std::shared_ptr<C2ReflectorHelper>& helper)
44 : C2InterfaceHelper(helper) {
45 setDerivedInstance(this);
46
47 addParameter(
Lajos Molnar3bb81cd2019-02-20 15:10:30 -080048 DefineParam(mInputFormat, C2_PARAMKEY_INPUT_STREAM_BUFFER_TYPE)
Roma Kauldfe650a2018-08-02 17:48:51 +053049 .withConstValue(
Lajos Molnar3bb81cd2019-02-20 15:10:30 -080050 new C2StreamBufferTypeSetting::input(0u, C2BufferData::GRAPHIC))
Roma Kauldfe650a2018-08-02 17:48:51 +053051 .build());
52
53 addParameter(
Lajos Molnar3bb81cd2019-02-20 15:10:30 -080054 DefineParam(mOutputFormat, C2_PARAMKEY_OUTPUT_STREAM_BUFFER_TYPE)
Roma Kauldfe650a2018-08-02 17:48:51 +053055 .withConstValue(
Lajos Molnar3bb81cd2019-02-20 15:10:30 -080056 new C2StreamBufferTypeSetting::output(0u, C2BufferData::LINEAR))
Roma Kauldfe650a2018-08-02 17:48:51 +053057 .build());
58
59 addParameter(
Lajos Molnar3bb81cd2019-02-20 15:10:30 -080060 DefineParam(mInputMediaType, C2_PARAMKEY_INPUT_MEDIA_TYPE)
61 .withConstValue(AllocSharedString<C2PortMediaTypeSetting::input>(
Roma Kauldfe650a2018-08-02 17:48:51 +053062 MEDIA_MIMETYPE_VIDEO_RAW))
63 .build());
64
65 addParameter(
Lajos Molnar3bb81cd2019-02-20 15:10:30 -080066 DefineParam(mOutputMediaType, C2_PARAMKEY_OUTPUT_MEDIA_TYPE)
67 .withConstValue(AllocSharedString<C2PortMediaTypeSetting::output>(
Roma Kauldfe650a2018-08-02 17:48:51 +053068 MEDIA_MIMETYPE_VIDEO_HEVC))
69 .build());
70
Lajos Molnar3bb81cd2019-02-20 15:10:30 -080071 addParameter(DefineParam(mUsage, C2_PARAMKEY_INPUT_STREAM_USAGE)
Roma Kauldfe650a2018-08-02 17:48:51 +053072 .withConstValue(new C2StreamUsageTuning::input(
73 0u, (uint64_t)C2MemoryUsage::CPU_READ))
74 .build());
75
Ray Essick0d11a7e2019-03-02 20:10:30 -080076 // matches size limits in codec library
Roma Kauldfe650a2018-08-02 17:48:51 +053077 addParameter(
Lajos Molnar3bb81cd2019-02-20 15:10:30 -080078 DefineParam(mSize, C2_PARAMKEY_PICTURE_SIZE)
79 .withDefault(new C2StreamPictureSizeInfo::input(0u, 320, 240))
Roma Kauldfe650a2018-08-02 17:48:51 +053080 .withFields({
81 C2F(mSize, width).inRange(320, 1920, 2),
82 C2F(mSize, height).inRange(128, 1088, 2),
83 })
84 .withSetter(SizeSetter)
85 .build());
86
87 addParameter(
Lajos Molnar3bb81cd2019-02-20 15:10:30 -080088 DefineParam(mFrameRate, C2_PARAMKEY_FRAME_RATE)
Roma Kauldfe650a2018-08-02 17:48:51 +053089 .withDefault(new C2StreamFrameRateInfo::output(0u, 30.))
90 .withFields({C2F(mFrameRate, value).greaterThan(0.)})
91 .withSetter(
92 Setter<decltype(*mFrameRate)>::StrictValueWithNoDeps)
93 .build());
94
Ray Essick0d11a7e2019-03-02 20:10:30 -080095 // matches limits in codec library
Roma Kauldfe650a2018-08-02 17:48:51 +053096 addParameter(
Lajos Molnar3bb81cd2019-02-20 15:10:30 -080097 DefineParam(mBitrate, C2_PARAMKEY_BITRATE)
98 .withDefault(new C2StreamBitrateInfo::output(0u, 64000))
Roma Kauldfe650a2018-08-02 17:48:51 +053099 .withFields({C2F(mBitrate, value).inRange(4096, 12000000)})
100 .withSetter(BitrateSetter)
101 .build());
102
Ray Essick0d11a7e2019-03-02 20:10:30 -0800103 // matches levels allowed within codec library
Roma Kauldfe650a2018-08-02 17:48:51 +0530104 addParameter(
105 DefineParam(mProfileLevel, C2_PARAMKEY_PROFILE_LEVEL)
106 .withDefault(new C2StreamProfileLevelInfo::output(
107 0u, PROFILE_HEVC_MAIN, LEVEL_HEVC_MAIN_1))
108 .withFields({
109 C2F(mProfileLevel, profile)
110 .oneOf({C2Config::PROFILE_HEVC_MAIN,
111 C2Config::PROFILE_HEVC_MAIN_STILL}),
112 C2F(mProfileLevel, level)
113 .oneOf({LEVEL_HEVC_MAIN_1, LEVEL_HEVC_MAIN_2,
114 LEVEL_HEVC_MAIN_2_1, LEVEL_HEVC_MAIN_3,
115 LEVEL_HEVC_MAIN_3_1, LEVEL_HEVC_MAIN_4,
116 LEVEL_HEVC_MAIN_4_1, LEVEL_HEVC_MAIN_5,
117 LEVEL_HEVC_MAIN_5_1, LEVEL_HEVC_MAIN_5_2}),
118 })
119 .withSetter(ProfileLevelSetter, mSize, mFrameRate, mBitrate)
120 .build());
121
122 addParameter(
123 DefineParam(mRequestSync, C2_PARAMKEY_REQUEST_SYNC_FRAME)
124 .withDefault(new C2StreamRequestSyncFrameTuning::output(0u, C2_FALSE))
125 .withFields({C2F(mRequestSync, value).oneOf({ C2_FALSE, C2_TRUE }) })
126 .withSetter(Setter<decltype(*mRequestSync)>::NonStrictValueWithNoDeps)
127 .build());
128
129 addParameter(
130 DefineParam(mSyncFramePeriod, C2_PARAMKEY_SYNC_FRAME_INTERVAL)
131 .withDefault(
132 new C2StreamSyncFrameIntervalTuning::output(0u, 1000000))
133 .withFields({C2F(mSyncFramePeriod, value).any()})
134 .withSetter(
135 Setter<decltype(*mSyncFramePeriod)>::StrictValueWithNoDeps)
136 .build());
137 }
138
139 static C2R BitrateSetter(bool mayBlock,
140 C2P<C2StreamBitrateInfo::output>& me) {
141 (void)mayBlock;
142 C2R res = C2R::Ok();
Ray Essick0d11a7e2019-03-02 20:10:30 -0800143 if (me.v.value < 4096) {
Roma Kauldfe650a2018-08-02 17:48:51 +0530144 me.set().value = 4096;
145 }
146 return res;
147 }
148
149 static C2R SizeSetter(bool mayBlock,
150 const C2P<C2StreamPictureSizeInfo::input>& oldMe,
151 C2P<C2StreamPictureSizeInfo::input>& me) {
152 (void)mayBlock;
153 C2R res = C2R::Ok();
154 if (!me.F(me.v.width).supportsAtAll(me.v.width)) {
155 res = res.plus(C2SettingResultBuilder::BadValue(me.F(me.v.width)));
156 me.set().width = oldMe.v.width;
157 }
158 if (!me.F(me.v.height).supportsAtAll(me.v.height)) {
159 res = res.plus(C2SettingResultBuilder::BadValue(me.F(me.v.height)));
160 me.set().height = oldMe.v.height;
161 }
162 return res;
163 }
164
165 static C2R ProfileLevelSetter(
166 bool mayBlock,
167 C2P<C2StreamProfileLevelInfo::output> &me,
Lajos Molnar3bb81cd2019-02-20 15:10:30 -0800168 const C2P<C2StreamPictureSizeInfo::input> &size,
Roma Kauldfe650a2018-08-02 17:48:51 +0530169 const C2P<C2StreamFrameRateInfo::output> &frameRate,
Lajos Molnar3bb81cd2019-02-20 15:10:30 -0800170 const C2P<C2StreamBitrateInfo::output> &bitrate) {
Roma Kauldfe650a2018-08-02 17:48:51 +0530171 (void)mayBlock;
172 if (!me.F(me.v.profile).supportsAtAll(me.v.profile)) {
173 me.set().profile = PROFILE_HEVC_MAIN;
174 }
175
176 struct LevelLimits {
177 C2Config::level_t level;
178 uint64_t samplesPerSec;
179 uint64_t samples;
180 uint32_t bitrate;
181 };
182
183 constexpr LevelLimits kLimits[] = {
184 { LEVEL_HEVC_MAIN_1, 552960, 36864, 128000 },
185 { LEVEL_HEVC_MAIN_2, 3686400, 122880, 1500000 },
186 { LEVEL_HEVC_MAIN_2_1, 7372800, 245760, 3000000 },
187 { LEVEL_HEVC_MAIN_3, 16588800, 552960, 6000000 },
188 { LEVEL_HEVC_MAIN_3_1, 33177600, 983040, 10000000 },
189 { LEVEL_HEVC_MAIN_4, 66846720, 2228224, 12000000 },
190 { LEVEL_HEVC_MAIN_4_1, 133693440, 2228224, 20000000 },
191 { LEVEL_HEVC_MAIN_5, 267386880, 8912896, 25000000 },
192 { LEVEL_HEVC_MAIN_5_1, 534773760, 8912896, 40000000 },
193 { LEVEL_HEVC_MAIN_5_2, 1069547520, 8912896, 60000000 },
194 { LEVEL_HEVC_MAIN_6, 1069547520, 35651584, 60000000 },
195 { LEVEL_HEVC_MAIN_6_1, 2139095040, 35651584, 120000000 },
196 { LEVEL_HEVC_MAIN_6_2, 4278190080, 35651584, 240000000 },
197 };
198
199 uint64_t samples = size.v.width * size.v.height;
200 uint64_t samplesPerSec = samples * frameRate.v.value;
201
202 // Check if the supplied level meets the MB / bitrate requirements. If
203 // not, update the level with the lowest level meeting the requirements.
204
205 bool found = false;
206 // By default needsUpdate = false in case the supplied level does meet
207 // the requirements.
208 bool needsUpdate = false;
209 for (const LevelLimits &limit : kLimits) {
210 if (samples <= limit.samples && samplesPerSec <= limit.samplesPerSec &&
211 bitrate.v.value <= limit.bitrate) {
212 // This is the lowest level that meets the requirements, and if
213 // we haven't seen the supplied level yet, that means we don't
214 // need the update.
215 if (needsUpdate) {
216 ALOGD("Given level %x does not cover current configuration: "
217 "adjusting to %x", me.v.level, limit.level);
218 me.set().level = limit.level;
219 }
220 found = true;
221 break;
222 }
223 if (me.v.level == limit.level) {
224 // We break out of the loop when the lowest feasible level is
225 // found. The fact that we're here means that our level doesn't
226 // meet the requirement and needs to be updated.
227 needsUpdate = true;
228 }
229 }
230 if (!found) {
231 // We set to the highest supported level.
232 me.set().level = LEVEL_HEVC_MAIN_5_2;
233 }
234 return C2R::Ok();
235 }
236
237 UWORD32 getProfile_l() const {
238 switch (mProfileLevel->profile) {
239 case PROFILE_HEVC_MAIN: [[fallthrough]];
240 case PROFILE_HEVC_MAIN_STILL: return 1;
241 default:
242 ALOGD("Unrecognized profile: %x", mProfileLevel->profile);
243 return 1;
244 }
245 }
246
247 UWORD32 getLevel_l() const {
248 struct Level {
249 C2Config::level_t c2Level;
250 UWORD32 hevcLevel;
251 };
252 constexpr Level levels[] = {
253 { LEVEL_HEVC_MAIN_1, 30 },
254 { LEVEL_HEVC_MAIN_2, 60 },
255 { LEVEL_HEVC_MAIN_2_1, 63 },
256 { LEVEL_HEVC_MAIN_3, 90 },
257 { LEVEL_HEVC_MAIN_3_1, 93 },
258 { LEVEL_HEVC_MAIN_4, 120 },
259 { LEVEL_HEVC_MAIN_4_1, 123 },
260 { LEVEL_HEVC_MAIN_5, 150 },
261 { LEVEL_HEVC_MAIN_5_1, 153 },
262 { LEVEL_HEVC_MAIN_5_2, 156 },
263 { LEVEL_HEVC_MAIN_6, 180 },
264 { LEVEL_HEVC_MAIN_6_1, 183 },
265 { LEVEL_HEVC_MAIN_6_2, 186 },
266 };
267 for (const Level &level : levels) {
268 if (mProfileLevel->level == level.c2Level) {
269 return level.hevcLevel;
270 }
271 }
272 ALOGD("Unrecognized level: %x", mProfileLevel->level);
273 return 156;
274 }
275 uint32_t getSyncFramePeriod_l() const {
276 if (mSyncFramePeriod->value < 0 ||
277 mSyncFramePeriod->value == INT64_MAX) {
278 return 0;
279 }
280 double period = mSyncFramePeriod->value / 1e6 * mFrameRate->value;
281 return (uint32_t)c2_max(c2_min(period + 0.5, double(UINT32_MAX)), 1.);
282 }
283
Ray Essick0d11a7e2019-03-02 20:10:30 -0800284 std::shared_ptr<C2StreamPictureSizeInfo::input> getSize_l() const {
Roma Kauldfe650a2018-08-02 17:48:51 +0530285 return mSize;
286 }
287 std::shared_ptr<C2StreamFrameRateInfo::output> getFrameRate_l() const {
288 return mFrameRate;
289 }
290 std::shared_ptr<C2StreamBitrateInfo::output> getBitrate_l() const {
291 return mBitrate;
292 }
293 std::shared_ptr<C2StreamRequestSyncFrameTuning::output> getRequestSync_l() const {
294 return mRequestSync;
295 }
296
297 private:
Lajos Molnar3bb81cd2019-02-20 15:10:30 -0800298 std::shared_ptr<C2StreamBufferTypeSetting::input> mInputFormat;
299 std::shared_ptr<C2StreamBufferTypeSetting::output> mOutputFormat;
300 std::shared_ptr<C2PortMediaTypeSetting::input> mInputMediaType;
301 std::shared_ptr<C2PortMediaTypeSetting::output> mOutputMediaType;
Roma Kauldfe650a2018-08-02 17:48:51 +0530302 std::shared_ptr<C2StreamUsageTuning::input> mUsage;
Lajos Molnar3bb81cd2019-02-20 15:10:30 -0800303 std::shared_ptr<C2StreamPictureSizeInfo::input> mSize;
Roma Kauldfe650a2018-08-02 17:48:51 +0530304 std::shared_ptr<C2StreamFrameRateInfo::output> mFrameRate;
305 std::shared_ptr<C2StreamRequestSyncFrameTuning::output> mRequestSync;
Lajos Molnar3bb81cd2019-02-20 15:10:30 -0800306 std::shared_ptr<C2StreamBitrateInfo::output> mBitrate;
Roma Kauldfe650a2018-08-02 17:48:51 +0530307 std::shared_ptr<C2StreamProfileLevelInfo::output> mProfileLevel;
308 std::shared_ptr<C2StreamSyncFrameIntervalTuning::output> mSyncFramePeriod;
309};
Ray Essick0d11a7e2019-03-02 20:10:30 -0800310
Roma Kauldfe650a2018-08-02 17:48:51 +0530311constexpr char COMPONENT_NAME[] = "c2.android.hevc.encoder";
312
313static size_t GetCPUCoreCount() {
Ray Essick0d11a7e2019-03-02 20:10:30 -0800314 long cpuCoreCount = 0;
315
Roma Kauldfe650a2018-08-02 17:48:51 +0530316#if defined(_SC_NPROCESSORS_ONLN)
317 cpuCoreCount = sysconf(_SC_NPROCESSORS_ONLN);
318#else
319 // _SC_NPROC_ONLN must be defined...
320 cpuCoreCount = sysconf(_SC_NPROC_ONLN);
321#endif
Ray Essick0d11a7e2019-03-02 20:10:30 -0800322
323 if (cpuCoreCount < 1)
324 cpuCoreCount = 1;
Roma Kauldfe650a2018-08-02 17:48:51 +0530325 return (size_t)cpuCoreCount;
326}
327
328C2SoftHevcEnc::C2SoftHevcEnc(const char* name, c2_node_id_t id,
329 const std::shared_ptr<IntfImpl>& intfImpl)
330 : SimpleC2Component(
331 std::make_shared<SimpleInterface<IntfImpl>>(name, id, intfImpl)),
332 mIntf(intfImpl),
333 mIvVideoColorFormat(IV_YUV_420P),
334 mHevcEncProfile(1),
335 mHevcEncLevel(30),
336 mStarted(false),
337 mSpsPpsHeaderReceived(false),
338 mSignalledEos(false),
339 mSignalledError(false),
340 mCodecCtx(nullptr) {
341 // If dump is enabled, then create an empty file
342 GENERATE_FILE_NAMES();
343 CREATE_DUMP_FILE(mInFile);
344 CREATE_DUMP_FILE(mOutFile);
345
346 gettimeofday(&mTimeStart, nullptr);
347 gettimeofday(&mTimeEnd, nullptr);
348}
349
350C2SoftHevcEnc::~C2SoftHevcEnc() {
351 releaseEncoder();
352}
353
354c2_status_t C2SoftHevcEnc::onInit() {
355 return initEncoder();
356}
357
358c2_status_t C2SoftHevcEnc::onStop() {
359 if (!mStarted) {
360 return C2_OK;
361 }
362 return releaseEncoder();
363}
364
365void C2SoftHevcEnc::onReset() {
366 onStop();
367 initEncoder();
368}
369
370void C2SoftHevcEnc::onRelease() {
371 onStop();
372}
373
374c2_status_t C2SoftHevcEnc::onFlush_sm() {
375 return C2_OK;
376}
377
378static void fillEmptyWork(const std::unique_ptr<C2Work>& work) {
379 uint32_t flags = 0;
380 if (work->input.flags & C2FrameData::FLAG_END_OF_STREAM) {
381 flags |= C2FrameData::FLAG_END_OF_STREAM;
382 ALOGV("Signalling EOS");
383 }
384 work->worklets.front()->output.flags = (C2FrameData::flags_t)flags;
385 work->worklets.front()->output.buffers.clear();
386 work->worklets.front()->output.ordinal = work->input.ordinal;
387 work->workletsProcessed = 1u;
388}
389
390c2_status_t C2SoftHevcEnc::initEncParams() {
391 mCodecCtx = nullptr;
Ray Essick0d11a7e2019-03-02 20:10:30 -0800392 mNumCores = std::min(GetCPUCoreCount(), (size_t) CODEC_MAX_CORES);
Roma Kauldfe650a2018-08-02 17:48:51 +0530393 memset(&mEncParams, 0, sizeof(ihevce_static_cfg_params_t));
394
395 // default configuration
396 IHEVCE_PLUGIN_STATUS_T err = ihevce_set_def_params(&mEncParams);
397 if (IHEVCE_EOK != err) {
398 ALOGE("HEVC default init failed : 0x%x", err);
399 return C2_CORRUPTED;
400 }
401
402 // update configuration
403 mEncParams.s_src_prms.i4_width = mSize->width;
404 mEncParams.s_src_prms.i4_height = mSize->height;
405 mEncParams.s_src_prms.i4_frm_rate_denom = 1000;
Ray Essick0d11a7e2019-03-02 20:10:30 -0800406 mEncParams.s_src_prms.i4_frm_rate_num =
407 mFrameRate->value * mEncParams.s_src_prms.i4_frm_rate_denom;
Roma Kauldfe650a2018-08-02 17:48:51 +0530408 mEncParams.s_tgt_lyr_prms.as_tgt_params[0].i4_quality_preset = IHEVCE_QUALITY_P5;
409 mEncParams.s_tgt_lyr_prms.as_tgt_params[0].ai4_tgt_bitrate[0] =
410 mBitrate->value;
411 mEncParams.s_tgt_lyr_prms.as_tgt_params[0].ai4_peak_bitrate[0] =
412 mBitrate->value << 1;
413 mEncParams.s_tgt_lyr_prms.as_tgt_params[0].i4_codec_level = mHevcEncLevel;
414 mEncParams.s_coding_tools_prms.i4_max_i_open_gop_period = mIDRInterval;
415 mEncParams.s_coding_tools_prms.i4_max_cra_open_gop_period = mIDRInterval;
416 mIvVideoColorFormat = IV_YUV_420P;
417 mEncParams.s_multi_thrd_prms.i4_max_num_cores = mNumCores;
418 mEncParams.s_out_strm_prms.i4_codec_profile = mHevcEncProfile;
419 mEncParams.s_config_prms.i4_rate_control_mode = 2;
420 mEncParams.s_lap_prms.i4_rc_look_ahead_pics = 0;
421
422 return C2_OK;
423}
424
425c2_status_t C2SoftHevcEnc::releaseEncoder() {
426 mSpsPpsHeaderReceived = false;
427 mSignalledEos = false;
428 mSignalledError = false;
429 mStarted = false;
430
431 if (mCodecCtx) {
432 IHEVCE_PLUGIN_STATUS_T err = ihevce_close(mCodecCtx);
433 if (IHEVCE_EOK != err) return C2_CORRUPTED;
434 mCodecCtx = nullptr;
435 }
436 return C2_OK;
437}
438
439c2_status_t C2SoftHevcEnc::drain(uint32_t drainMode,
440 const std::shared_ptr<C2BlockPool>& pool) {
441 (void)drainMode;
442 (void)pool;
443 return C2_OK;
444}
445c2_status_t C2SoftHevcEnc::initEncoder() {
446 CHECK(!mCodecCtx);
447 {
448 IntfImpl::Lock lock = mIntf->lock();
449 mSize = mIntf->getSize_l();
450 mBitrate = mIntf->getBitrate_l();
451 mFrameRate = mIntf->getFrameRate_l();
452 mHevcEncProfile = mIntf->getProfile_l();
453 mHevcEncLevel = mIntf->getLevel_l();
454 mIDRInterval = mIntf->getSyncFramePeriod_l();
455 }
456
457 c2_status_t status = initEncParams();
458
459 if (C2_OK != status) {
460 ALOGE("Failed to initialize encoder params : 0x%x", status);
461 mSignalledError = true;
462 return status;
463 }
464
465 IHEVCE_PLUGIN_STATUS_T err = IHEVCE_EOK;
466 err = ihevce_init(&mEncParams, &mCodecCtx);
467 if (IHEVCE_EOK != err) {
468 ALOGE("HEVC encoder init failed : 0x%x", err);
469 return C2_CORRUPTED;
470 }
471
472 mStarted = true;
473 return C2_OK;
474}
475
476c2_status_t C2SoftHevcEnc::setEncodeArgs(ihevce_inp_buf_t* ps_encode_ip,
477 const C2GraphicView* const input,
478 uint64_t timestamp) {
479 ihevce_static_cfg_params_t* params = &mEncParams;
Ray Essick0d11a7e2019-03-02 20:10:30 -0800480 memset(ps_encode_ip, 0, sizeof(*ps_encode_ip));
Roma Kauldfe650a2018-08-02 17:48:51 +0530481
482 if (!input) {
483 return C2_OK;
484 }
485
486 if (input->width() < mSize->width ||
487 input->height() < mSize->height) {
488 /* Expect width height to be configured */
489 ALOGW("unexpected Capacity Aspect %d(%d) x %d(%d)", input->width(),
490 mSize->width, input->height(), mSize->height);
491 return C2_BAD_VALUE;
492 }
493
494 const C2PlanarLayout& layout = input->layout();
495 uint8_t* yPlane =
496 const_cast<uint8_t *>(input->data()[C2PlanarLayout::PLANE_Y]);
497 uint8_t* uPlane =
498 const_cast<uint8_t *>(input->data()[C2PlanarLayout::PLANE_U]);
499 uint8_t* vPlane =
500 const_cast<uint8_t *>(input->data()[C2PlanarLayout::PLANE_V]);
501 int32_t yStride = layout.planes[C2PlanarLayout::PLANE_Y].rowInc;
502 int32_t uStride = layout.planes[C2PlanarLayout::PLANE_U].rowInc;
503 int32_t vStride = layout.planes[C2PlanarLayout::PLANE_V].rowInc;
504
Ray Essick0d11a7e2019-03-02 20:10:30 -0800505 const uint32_t width = mSize->width;
506 const uint32_t height = mSize->height;
Roma Kauldfe650a2018-08-02 17:48:51 +0530507
Ray Essick0d11a7e2019-03-02 20:10:30 -0800508 // width and height must be even
509 if (width & 1u || height & 1u) {
510 ALOGW("height(%u) and width(%u) must both be even", height, width);
511 return C2_BAD_VALUE;
512 }
Roma Kauldfe650a2018-08-02 17:48:51 +0530513
514 size_t yPlaneSize = width * height;
515
516 switch (layout.type) {
517 case C2PlanarLayout::TYPE_RGB:
518 [[fallthrough]];
519 case C2PlanarLayout::TYPE_RGBA: {
520 MemoryBlock conversionBuffer =
521 mConversionBuffers.fetch(yPlaneSize * 3 / 2);
522 mConversionBuffersInUse.emplace(conversionBuffer.data(),
523 conversionBuffer);
524 yPlane = conversionBuffer.data();
525 uPlane = yPlane + yPlaneSize;
526 vPlane = uPlane + yPlaneSize / 4;
527 yStride = width;
528 uStride = vStride = yStride / 2;
529 ConvertRGBToPlanarYUV(yPlane, yStride, height,
530 conversionBuffer.size(), *input);
531 break;
532 }
533 case C2PlanarLayout::TYPE_YUV: {
534 if (!IsYUV420(*input)) {
535 ALOGE("input is not YUV420");
536 return C2_BAD_VALUE;
537 }
538
539 if (layout.planes[layout.PLANE_Y].colInc == 1 &&
540 layout.planes[layout.PLANE_U].colInc == 1 &&
541 layout.planes[layout.PLANE_V].colInc == 1 &&
542 uStride == vStride && yStride == 2 * vStride) {
543 // I420 compatible - already set up above
544 break;
545 }
546
547 // copy to I420
548 yStride = width;
549 uStride = vStride = yStride / 2;
550 MemoryBlock conversionBuffer =
551 mConversionBuffers.fetch(yPlaneSize * 3 / 2);
552 mConversionBuffersInUse.emplace(conversionBuffer.data(),
553 conversionBuffer);
554 MediaImage2 img =
555 CreateYUV420PlanarMediaImage2(width, height, yStride, height);
556 status_t err = ImageCopy(conversionBuffer.data(), &img, *input);
557 if (err != OK) {
558 ALOGE("Buffer conversion failed: %d", err);
559 return C2_BAD_VALUE;
560 }
561 yPlane = conversionBuffer.data();
562 uPlane = yPlane + yPlaneSize;
563 vPlane = uPlane + yPlaneSize / 4;
564 break;
565 }
566
567 case C2PlanarLayout::TYPE_YUVA:
568 ALOGE("YUVA plane type is not supported");
569 return C2_BAD_VALUE;
570
571 default:
572 ALOGE("Unrecognized plane type: %d", layout.type);
573 return C2_BAD_VALUE;
574 }
575
576 switch (mIvVideoColorFormat) {
577 case IV_YUV_420P: {
578 // input buffer is supposed to be const but Ittiam API wants bare
579 // pointer.
580 ps_encode_ip->apv_inp_planes[0] = yPlane;
581 ps_encode_ip->apv_inp_planes[1] = uPlane;
582 ps_encode_ip->apv_inp_planes[2] = vPlane;
583
584 ps_encode_ip->ai4_inp_strd[0] = yStride;
585 ps_encode_ip->ai4_inp_strd[1] = uStride;
586 ps_encode_ip->ai4_inp_strd[2] = vStride;
587
588 ps_encode_ip->ai4_inp_size[0] = yStride * height;
589 ps_encode_ip->ai4_inp_size[1] = uStride * height >> 1;
590 ps_encode_ip->ai4_inp_size[2] = vStride * height >> 1;
591 break;
592 }
593
594 case IV_YUV_422ILE: {
595 // TODO
596 break;
597 }
598
599 case IV_YUV_420SP_UV:
600 case IV_YUV_420SP_VU:
601 default: {
602 ps_encode_ip->apv_inp_planes[0] = yPlane;
603 ps_encode_ip->apv_inp_planes[1] = uPlane;
604 ps_encode_ip->apv_inp_planes[2] = nullptr;
605
606 ps_encode_ip->ai4_inp_strd[0] = yStride;
607 ps_encode_ip->ai4_inp_strd[1] = uStride;
608 ps_encode_ip->ai4_inp_strd[2] = 0;
609
610 ps_encode_ip->ai4_inp_size[0] = yStride * height;
611 ps_encode_ip->ai4_inp_size[1] = uStride * height >> 1;
612 ps_encode_ip->ai4_inp_size[2] = 0;
613 break;
614 }
615 }
616
617 ps_encode_ip->i4_curr_bitrate =
618 params->s_tgt_lyr_prms.as_tgt_params[0].ai4_tgt_bitrate[0];
619 ps_encode_ip->i4_curr_peak_bitrate =
620 params->s_tgt_lyr_prms.as_tgt_params[0].ai4_peak_bitrate[0];
621 ps_encode_ip->i4_curr_rate_factor = params->s_config_prms.i4_rate_factor;
622 ps_encode_ip->u8_pts = timestamp;
623 return C2_OK;
624}
625
626void C2SoftHevcEnc::process(const std::unique_ptr<C2Work>& work,
627 const std::shared_ptr<C2BlockPool>& pool) {
628 // Initialize output work
629 work->result = C2_OK;
630 work->workletsProcessed = 1u;
631 work->worklets.front()->output.flags = work->input.flags;
632
633 if (mSignalledError || mSignalledEos) {
634 work->result = C2_BAD_VALUE;
635 ALOGD("Signalled Error / Signalled Eos");
636 return;
637 }
638 c2_status_t status = C2_OK;
639
640 // Initialize encoder if not already initialized
641 if (!mStarted) {
642 status = initEncoder();
643 if (C2_OK != status) {
644 ALOGE("Failed to initialize encoder : 0x%x", status);
645 mSignalledError = true;
646 work->result = status;
647 return;
648 }
649 }
650
651 std::shared_ptr<const C2GraphicView> view;
652 std::shared_ptr<C2Buffer> inputBuffer = nullptr;
653 bool eos = ((work->input.flags & C2FrameData::FLAG_END_OF_STREAM) != 0);
654 if (!work->input.buffers.empty()) {
655 inputBuffer = work->input.buffers[0];
656 view = std::make_shared<const C2GraphicView>(
657 inputBuffer->data().graphicBlocks().front().map().get());
658 if (view->error() != C2_OK) {
659 ALOGE("graphic view map err = %d", view->error());
660 mSignalledError = true;
Ray Essick0d11a7e2019-03-02 20:10:30 -0800661 work->result = C2_CORRUPTED;
Roma Kauldfe650a2018-08-02 17:48:51 +0530662 return;
663 }
664 }
665
666 IHEVCE_PLUGIN_STATUS_T err = IHEVCE_EOK;
667
668 fillEmptyWork(work);
669 if (!mSpsPpsHeaderReceived) {
670 ihevce_out_buf_t s_header_op{};
671 err = ihevce_encode_header(mCodecCtx, &s_header_op);
672 if (err == IHEVCE_EOK && s_header_op.i4_bytes_generated) {
Lajos Molnar3bb81cd2019-02-20 15:10:30 -0800673 std::unique_ptr<C2StreamInitDataInfo::output> csd =
674 C2StreamInitDataInfo::output::AllocUnique(
Roma Kauldfe650a2018-08-02 17:48:51 +0530675 s_header_op.i4_bytes_generated, 0u);
676 if (!csd) {
677 ALOGE("CSD allocation failed");
678 mSignalledError = true;
679 work->result = C2_NO_MEMORY;
680 return;
681 }
682 memcpy(csd->m.value, s_header_op.pu1_output_buf,
683 s_header_op.i4_bytes_generated);
684 DUMP_TO_FILE(mOutFile, csd->m.value, csd->flexCount());
685 work->worklets.front()->output.configUpdate.push_back(
686 std::move(csd));
687 mSpsPpsHeaderReceived = true;
688 }
689 if (!inputBuffer) {
690 return;
691 }
692 }
693 ihevce_inp_buf_t s_encode_ip{};
694 ihevce_out_buf_t s_encode_op{};
695 uint64_t timestamp = work->input.ordinal.timestamp.peekull();
696
697 status = setEncodeArgs(&s_encode_ip, view.get(), timestamp);
698 if (C2_OK != status) {
Roma Kauldfe650a2018-08-02 17:48:51 +0530699 ALOGE("setEncodeArgs failed : 0x%x", status);
Ray Essick0d11a7e2019-03-02 20:10:30 -0800700 mSignalledError = true;
Roma Kauldfe650a2018-08-02 17:48:51 +0530701 work->result = status;
702 return;
703 }
704
705 uint64_t timeDelay = 0;
706 uint64_t timeTaken = 0;
707 GETTIME(&mTimeStart, nullptr);
708 TIME_DIFF(mTimeEnd, mTimeStart, timeDelay);
709
710 ihevce_inp_buf_t* ps_encode_ip = (inputBuffer) ? &s_encode_ip : nullptr;
711
712 err = ihevce_encode(mCodecCtx, ps_encode_ip, &s_encode_op);
713 if (IHEVCE_EOK != err) {
714 ALOGE("Encode Frame failed : 0x%x", err);
715 mSignalledError = true;
716 work->result = C2_CORRUPTED;
717 return;
718 }
719
720 GETTIME(&mTimeEnd, nullptr);
721 /* Compute time taken for decode() */
722 TIME_DIFF(mTimeStart, mTimeEnd, timeTaken);
723
724 ALOGV("timeTaken=%6d delay=%6d numBytes=%6d", (int)timeTaken,
725 (int)timeDelay, s_encode_op.i4_bytes_generated);
726
727 if (s_encode_op.i4_bytes_generated) {
728 std::shared_ptr<C2LinearBlock> block;
729 C2MemoryUsage usage = {C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE};
730 status = pool->fetchLinearBlock(s_encode_op.i4_bytes_generated, usage, &block);
731 if (C2_OK != status) {
732 ALOGE("fetchLinearBlock for Output failed with status 0x%x", status);
733 work->result = C2_NO_MEMORY;
734 mSignalledError = true;
735 return;
736 }
737 C2WriteView wView = block->map().get();
738 if (C2_OK != wView.error()) {
739 ALOGE("write view map failed with status 0x%x", wView.error());
740 work->result = wView.error();
741 mSignalledError = true;
742 return;
743 }
744 memcpy(wView.data(), s_encode_op.pu1_output_buf,
745 s_encode_op.i4_bytes_generated);
746
747 std::shared_ptr<C2Buffer> buffer =
748 createLinearBuffer(block, 0, s_encode_op.i4_bytes_generated);
749
750 DUMP_TO_FILE(mOutFile, s_encode_op.pu1_output_buf,
751 s_encode_op.i4_bytes_generated);
752
753 work->worklets.front()->output.ordinal.timestamp = s_encode_op.u8_pts;
754 if (s_encode_op.i4_is_key_frame) {
755 ALOGV("IDR frame produced");
756 buffer->setInfo(
757 std::make_shared<C2StreamPictureTypeMaskInfo::output>(
Lajos Molnar3bb81cd2019-02-20 15:10:30 -0800758 0u /* stream id */, C2Config::SYNC_FRAME));
Roma Kauldfe650a2018-08-02 17:48:51 +0530759 }
760 work->worklets.front()->output.buffers.push_back(buffer);
761 }
762 if (eos) {
763 mSignalledEos = true;
764 }
765}
766
767class C2SoftHevcEncFactory : public C2ComponentFactory {
768 public:
769 C2SoftHevcEncFactory()
770 : mHelper(std::static_pointer_cast<C2ReflectorHelper>(
771 GetCodec2PlatformComponentStore()->getParamReflector())) {}
772
Ray Essick0d11a7e2019-03-02 20:10:30 -0800773 c2_status_t createComponent(
774 c2_node_id_t id,
775 std::shared_ptr<C2Component>* const component,
Roma Kauldfe650a2018-08-02 17:48:51 +0530776 std::function<void(C2Component*)> deleter) override {
777 *component = std::shared_ptr<C2Component>(
778 new C2SoftHevcEnc(
779 COMPONENT_NAME, id,
780 std::make_shared<C2SoftHevcEnc::IntfImpl>(mHelper)),
781 deleter);
782 return C2_OK;
783 }
784
Ray Essick0d11a7e2019-03-02 20:10:30 -0800785 c2_status_t createInterface(
786 c2_node_id_t id,
787 std::shared_ptr<C2ComponentInterface>* const interface,
Roma Kauldfe650a2018-08-02 17:48:51 +0530788 std::function<void(C2ComponentInterface*)> deleter) override {
789 *interface = std::shared_ptr<C2ComponentInterface>(
790 new SimpleInterface<C2SoftHevcEnc::IntfImpl>(
791 COMPONENT_NAME, id,
792 std::make_shared<C2SoftHevcEnc::IntfImpl>(mHelper)),
793 deleter);
794 return C2_OK;
795 }
796
Ray Essick0d11a7e2019-03-02 20:10:30 -0800797 ~C2SoftHevcEncFactory() override = default;
Roma Kauldfe650a2018-08-02 17:48:51 +0530798
799 private:
800 std::shared_ptr<C2ReflectorHelper> mHelper;
801};
802
803} // namespace android
804
805extern "C" ::C2ComponentFactory* CreateCodec2Factory() {
806 ALOGV("in %s", __func__);
807 return new ::android::C2SoftHevcEncFactory();
808}
809
810extern "C" void DestroyCodec2Factory(::C2ComponentFactory* factory) {
811 ALOGV("in %s", __func__);
812 delete factory;
813}