blob: f2c76421e6ab26943b207fdf4fb9d91caf1443ee [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;
70 IV_COLOR_FORMAT_T mIvVideoColorFormat;
71 UWORD32 mHevcEncProfile;
72 UWORD32 mHevcEncLevel;
73 bool mStarted;
74 bool mSpsPpsHeaderReceived;
75 bool mSignalledEos;
76 bool mSignalledError;
77 void* mCodecCtx;
78 MemoryBlockPool mConversionBuffers;
79 std::map<void*, MemoryBlock> mConversionBuffersInUse;
80 // configurations used by component in process
81 // (TODO: keep this in intf but make them internal only)
82 std::shared_ptr<C2StreamPictureSizeInfo::input> mSize;
83 std::shared_ptr<C2StreamFrameRateInfo::output> mFrameRate;
84 std::shared_ptr<C2StreamBitrateInfo::output> mBitrate;
Harish Mahendrakar57d7cc92019-02-05 08:34:55 -080085 std::shared_ptr<C2StreamBitrateModeTuning::output> mBitrateMode;
86 std::shared_ptr<C2StreamComplexityTuning::output> mComplexity;
87 std::shared_ptr<C2StreamQualityTuning::output> mQuality;
Roma Kauldfe650a2018-08-02 17:48:51 +053088
89#ifdef FILE_DUMP_ENABLE
90 char mInFile[200];
91 char mOutFile[200];
92#endif /* FILE_DUMP_ENABLE */
93
94 // profile
95 struct timeval mTimeStart;
96 struct timeval mTimeEnd;
97
98 c2_status_t initEncParams();
99 c2_status_t initEncoder();
100 c2_status_t releaseEncoder();
101 c2_status_t setEncodeArgs(ihevce_inp_buf_t* ps_encode_ip,
102 const C2GraphicView* const input,
Manisha Jajoo863bcfc2019-03-22 16:32:55 +0530103 uint64_t workIndex);
104 void finishWork(uint64_t index, const std::unique_ptr<C2Work>& work,
105 const std::shared_ptr<C2BlockPool>& pool,
106 ihevce_out_buf_t* ps_encode_op);
107 c2_status_t drainInternal(uint32_t drainMode,
108 const std::shared_ptr<C2BlockPool>& pool,
109 const std::unique_ptr<C2Work>& work);
Roma Kauldfe650a2018-08-02 17:48:51 +0530110 C2_DO_NOT_COPY(C2SoftHevcEnc);
111};
Roma Kauldfe650a2018-08-02 17:48:51 +0530112#ifdef FILE_DUMP_ENABLE
113
114#define INPUT_DUMP_PATH "/data/local/tmp/hevc"
115#define INPUT_DUMP_EXT "yuv"
116#define OUTPUT_DUMP_PATH "/data/local/tmp/hevc"
117#define OUTPUT_DUMP_EXT "h265"
118#define GENERATE_FILE_NAMES() \
119{ \
120 GETTIME(&mTimeStart, NULL); \
121 strcpy(mInFile, ""); \
122 ALOGD("GENERATE_FILE_NAMES"); \
123 sprintf(mInFile, "%s_%ld.%ld.%s", INPUT_DUMP_PATH, mTimeStart.tv_sec, \
124 mTimeStart.tv_usec, INPUT_DUMP_EXT); \
125 strcpy(mOutFile, ""); \
126 sprintf(mOutFile, "%s_%ld.%ld.%s", OUTPUT_DUMP_PATH, \
127 mTimeStart.tv_sec, mTimeStart.tv_usec, OUTPUT_DUMP_EXT); \
128}
129
130#define CREATE_DUMP_FILE(m_filename) \
131{ \
132 FILE* fp = fopen(m_filename, "wb"); \
133 if (fp != NULL) { \
134 ALOGD("Opened file %s", m_filename); \
135 fclose(fp); \
136 } else { \
137 ALOGD("Could not open file %s", m_filename); \
138 } \
139}
140#define DUMP_TO_FILE(m_filename, m_buf, m_size) \
141{ \
142 FILE* fp = fopen(m_filename, "ab"); \
143 if (fp != NULL && m_buf != NULL) { \
144 int i; \
145 ALOGD("Dump to file!"); \
146 i = fwrite(m_buf, 1, m_size, fp); \
147 if (i != (int)m_size) { \
148 ALOGD("Error in fwrite, returned %d", i); \
149 perror("Error in write to file"); \
150 } \
151 fclose(fp); \
152 } else { \
153 ALOGD("Could not write to file %s", m_filename); \
154 if (fp != NULL) fclose(fp); \
155 } \
156}
157#else /* FILE_DUMP_ENABLE */
158#define INPUT_DUMP_PATH
159#define INPUT_DUMP_EXT
160#define OUTPUT_DUMP_PATH
161#define OUTPUT_DUMP_EXT
162#define GENERATE_FILE_NAMES()
163#define CREATE_DUMP_FILE(m_filename)
164#define DUMP_TO_FILE(m_filename, m_buf, m_size)
165#endif /* FILE_DUMP_ENABLE */
166
167} // namespace android
168
169#endif // C2_SOFT_HEVC_ENC_H__