blob: 140b4a928790fdf11f8d03c487f26cea0cca8ed4 [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;
Roma Kauldfe650a2018-08-02 17:48:51 +053091#ifdef FILE_DUMP_ENABLE
92 char mInFile[200];
93 char mOutFile[200];
94#endif /* FILE_DUMP_ENABLE */
95
96 // profile
97 struct timeval mTimeStart;
98 struct timeval mTimeEnd;
99
100 c2_status_t initEncParams();
101 c2_status_t initEncoder();
102 c2_status_t releaseEncoder();
103 c2_status_t setEncodeArgs(ihevce_inp_buf_t* ps_encode_ip,
104 const C2GraphicView* const input,
Manisha Jajoo863bcfc2019-03-22 16:32:55 +0530105 uint64_t workIndex);
106 void finishWork(uint64_t index, const std::unique_ptr<C2Work>& work,
107 const std::shared_ptr<C2BlockPool>& pool,
108 ihevce_out_buf_t* ps_encode_op);
109 c2_status_t drainInternal(uint32_t drainMode,
110 const std::shared_ptr<C2BlockPool>& pool,
111 const std::unique_ptr<C2Work>& work);
Roma Kauldfe650a2018-08-02 17:48:51 +0530112 C2_DO_NOT_COPY(C2SoftHevcEnc);
113};
Roma Kauldfe650a2018-08-02 17:48:51 +0530114#ifdef FILE_DUMP_ENABLE
115
116#define INPUT_DUMP_PATH "/data/local/tmp/hevc"
117#define INPUT_DUMP_EXT "yuv"
118#define OUTPUT_DUMP_PATH "/data/local/tmp/hevc"
119#define OUTPUT_DUMP_EXT "h265"
120#define GENERATE_FILE_NAMES() \
121{ \
122 GETTIME(&mTimeStart, NULL); \
123 strcpy(mInFile, ""); \
124 ALOGD("GENERATE_FILE_NAMES"); \
125 sprintf(mInFile, "%s_%ld.%ld.%s", INPUT_DUMP_PATH, mTimeStart.tv_sec, \
126 mTimeStart.tv_usec, INPUT_DUMP_EXT); \
127 strcpy(mOutFile, ""); \
128 sprintf(mOutFile, "%s_%ld.%ld.%s", OUTPUT_DUMP_PATH, \
129 mTimeStart.tv_sec, mTimeStart.tv_usec, OUTPUT_DUMP_EXT); \
130}
131
132#define CREATE_DUMP_FILE(m_filename) \
133{ \
134 FILE* fp = fopen(m_filename, "wb"); \
135 if (fp != NULL) { \
136 ALOGD("Opened file %s", m_filename); \
137 fclose(fp); \
138 } else { \
139 ALOGD("Could not open file %s", m_filename); \
140 } \
141}
142#define DUMP_TO_FILE(m_filename, m_buf, m_size) \
143{ \
144 FILE* fp = fopen(m_filename, "ab"); \
145 if (fp != NULL && m_buf != NULL) { \
146 int i; \
147 ALOGD("Dump to file!"); \
148 i = fwrite(m_buf, 1, m_size, fp); \
149 if (i != (int)m_size) { \
150 ALOGD("Error in fwrite, returned %d", i); \
151 perror("Error in write to file"); \
152 } \
153 fclose(fp); \
154 } else { \
155 ALOGD("Could not write to file %s", m_filename); \
156 if (fp != NULL) fclose(fp); \
157 } \
158}
159#else /* FILE_DUMP_ENABLE */
160#define INPUT_DUMP_PATH
161#define INPUT_DUMP_EXT
162#define OUTPUT_DUMP_PATH
163#define OUTPUT_DUMP_EXT
164#define GENERATE_FILE_NAMES()
165#define CREATE_DUMP_FILE(m_filename)
166#define DUMP_TO_FILE(m_filename, m_buf, m_size)
167#endif /* FILE_DUMP_ENABLE */
168
169} // namespace android
170
171#endif // C2_SOFT_HEVC_ENC_H__