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