blob: 5ea46022cd5cc5449980900440769c3a66ae3eb9 [file] [log] [blame]
Roma Kauldfe650a2018-08-02 17:48:51 +05301/*
Ray Essick0d11a7e2019-03-02 20:10:30 -08002 * Copyright 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#ifndef ANDROID_C2_SOFT_HEVC_ENC_H_
18#define ANDROID_C2_SOFT_HEVC_ENC_H_
19
Roma Kauldfe650a2018-08-02 17:48:51 +053020#include <SimpleC2Component.h>
Ray Essick0d11a7e2019-03-02 20:10:30 -080021#include <algorithm>
22#include <map>
23#include <media/stagefright/foundation/ColorUtils.h>
24#include <utils/Vector.h>
Roma Kauldfe650a2018-08-02 17:48:51 +053025
26#include "ihevc_typedefs.h"
27
28namespace android {
Roma Kauldfe650a2018-08-02 17:48:51 +053029
30/** Get time */
Ray Essick0d11a7e2019-03-02 20:10:30 -080031#define GETTIME(a, b) gettimeofday(a, b)
Roma Kauldfe650a2018-08-02 17:48:51 +053032
33/** Compute difference between start and end */
34#define TIME_DIFF(start, end, diff) \
35 diff = (((end).tv_sec - (start).tv_sec) * 1000000) + \
36 ((end).tv_usec - (start).tv_usec);
37
Manisha Jajoo863bcfc2019-03-22 16:32:55 +053038#define CODEC_MAX_CORES 4
39#define MAX_B_FRAMES 1
40#define MAX_RC_LOOKAHEAD 1
41
42#define DEFAULT_B_FRAMES 0
43#define DEFAULT_RC_LOOKAHEAD 0
Roma Kauldfe650a2018-08-02 17:48:51 +053044
45struct C2SoftHevcEnc : public SimpleC2Component {
46 class IntfImpl;
47
48 C2SoftHevcEnc(const char* name, c2_node_id_t id,
49 const std::shared_ptr<IntfImpl>& intfImpl);
50
51 // From SimpleC2Component
52 c2_status_t onInit() override;
53 c2_status_t onStop() override;
54 void onReset() override;
55 void onRelease() override;
56 c2_status_t onFlush_sm() override;
57 void process(const std::unique_ptr<C2Work>& work,
58 const std::shared_ptr<C2BlockPool>& pool) override;
59 c2_status_t drain(uint32_t drainMode,
60 const std::shared_ptr<C2BlockPool>& pool) override;
61
62 protected:
Ray Essick0d11a7e2019-03-02 20:10:30 -080063 ~C2SoftHevcEnc() override;
Roma Kauldfe650a2018-08-02 17:48:51 +053064
65 private:
66 std::shared_ptr<IntfImpl> mIntf;
67 ihevce_static_cfg_params_t mEncParams;
68 size_t mNumCores;
69 UWORD32 mIDRInterval;
Harish Mahendrakarfbccd2e2019-05-22 15:20:21 -070070 UWORD32 mIInterval;
71 UWORD32 mBframes;
Roma Kauldfe650a2018-08-02 17:48:51 +053072 IV_COLOR_FORMAT_T mIvVideoColorFormat;
73 UWORD32 mHevcEncProfile;
74 UWORD32 mHevcEncLevel;
75 bool mStarted;
76 bool mSpsPpsHeaderReceived;
77 bool mSignalledEos;
78 bool mSignalledError;
79 void* mCodecCtx;
80 MemoryBlockPool mConversionBuffers;
81 std::map<void*, MemoryBlock> mConversionBuffersInUse;
82 // configurations used by component in process
83 // (TODO: keep this in intf but make them internal only)
84 std::shared_ptr<C2StreamPictureSizeInfo::input> mSize;
85 std::shared_ptr<C2StreamFrameRateInfo::output> mFrameRate;
86 std::shared_ptr<C2StreamBitrateInfo::output> mBitrate;
Harish Mahendrakar57d7cc92019-02-05 08:34:55 -080087 std::shared_ptr<C2StreamBitrateModeTuning::output> mBitrateMode;
88 std::shared_ptr<C2StreamComplexityTuning::output> mComplexity;
89 std::shared_ptr<C2StreamQualityTuning::output> mQuality;
Harish Mahendrakarfbccd2e2019-05-22 15:20:21 -070090 std::shared_ptr<C2StreamGopTuning::output> mGop;
Rakesh Kumarb17576c2019-07-30 20:10:54 +053091 std::shared_ptr<C2StreamRequestSyncFrameTuning::output> mRequestSync;
Roma Kauldfe650a2018-08-02 17:48:51 +053092#ifdef FILE_DUMP_ENABLE
93 char mInFile[200];
94 char mOutFile[200];
95#endif /* FILE_DUMP_ENABLE */
96
97 // profile
98 struct timeval mTimeStart;
99 struct timeval mTimeEnd;
100
101 c2_status_t initEncParams();
102 c2_status_t initEncoder();
103 c2_status_t releaseEncoder();
104 c2_status_t setEncodeArgs(ihevce_inp_buf_t* ps_encode_ip,
105 const C2GraphicView* const input,
Manisha Jajoo863bcfc2019-03-22 16:32:55 +0530106 uint64_t workIndex);
107 void finishWork(uint64_t index, const std::unique_ptr<C2Work>& work,
108 const std::shared_ptr<C2BlockPool>& pool,
109 ihevce_out_buf_t* ps_encode_op);
110 c2_status_t drainInternal(uint32_t drainMode,
111 const std::shared_ptr<C2BlockPool>& pool,
112 const std::unique_ptr<C2Work>& work);
Roma Kauldfe650a2018-08-02 17:48:51 +0530113 C2_DO_NOT_COPY(C2SoftHevcEnc);
114};
Roma Kauldfe650a2018-08-02 17:48:51 +0530115#ifdef FILE_DUMP_ENABLE
116
117#define INPUT_DUMP_PATH "/data/local/tmp/hevc"
118#define INPUT_DUMP_EXT "yuv"
119#define OUTPUT_DUMP_PATH "/data/local/tmp/hevc"
120#define OUTPUT_DUMP_EXT "h265"
121#define GENERATE_FILE_NAMES() \
122{ \
123 GETTIME(&mTimeStart, NULL); \
124 strcpy(mInFile, ""); \
125 ALOGD("GENERATE_FILE_NAMES"); \
126 sprintf(mInFile, "%s_%ld.%ld.%s", INPUT_DUMP_PATH, mTimeStart.tv_sec, \
127 mTimeStart.tv_usec, INPUT_DUMP_EXT); \
128 strcpy(mOutFile, ""); \
129 sprintf(mOutFile, "%s_%ld.%ld.%s", OUTPUT_DUMP_PATH, \
130 mTimeStart.tv_sec, mTimeStart.tv_usec, OUTPUT_DUMP_EXT); \
131}
132
133#define CREATE_DUMP_FILE(m_filename) \
134{ \
135 FILE* fp = fopen(m_filename, "wb"); \
136 if (fp != NULL) { \
137 ALOGD("Opened file %s", m_filename); \
138 fclose(fp); \
139 } else { \
140 ALOGD("Could not open file %s", m_filename); \
141 } \
142}
143#define DUMP_TO_FILE(m_filename, m_buf, m_size) \
144{ \
145 FILE* fp = fopen(m_filename, "ab"); \
146 if (fp != NULL && m_buf != NULL) { \
147 int i; \
148 ALOGD("Dump to file!"); \
149 i = fwrite(m_buf, 1, m_size, fp); \
150 if (i != (int)m_size) { \
151 ALOGD("Error in fwrite, returned %d", i); \
152 perror("Error in write to file"); \
153 } \
154 fclose(fp); \
155 } else { \
156 ALOGD("Could not write to file %s", m_filename); \
157 if (fp != NULL) fclose(fp); \
158 } \
159}
160#else /* FILE_DUMP_ENABLE */
161#define INPUT_DUMP_PATH
162#define INPUT_DUMP_EXT
163#define OUTPUT_DUMP_PATH
164#define OUTPUT_DUMP_EXT
165#define GENERATE_FILE_NAMES()
166#define CREATE_DUMP_FILE(m_filename)
167#define DUMP_TO_FILE(m_filename, m_buf, m_size)
168#endif /* FILE_DUMP_ENABLE */
169
170} // namespace android
171
172#endif // C2_SOFT_HEVC_ENC_H__