blob: 8569a3ea3a356ad557a692ce61ed0a2c4dae178a [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
38#define CODEC_MAX_CORES 4
39
40struct C2SoftHevcEnc : public SimpleC2Component {
41 class IntfImpl;
42
43 C2SoftHevcEnc(const char* name, c2_node_id_t id,
44 const std::shared_ptr<IntfImpl>& intfImpl);
45
46 // From SimpleC2Component
47 c2_status_t onInit() override;
48 c2_status_t onStop() override;
49 void onReset() override;
50 void onRelease() override;
51 c2_status_t onFlush_sm() override;
52 void process(const std::unique_ptr<C2Work>& work,
53 const std::shared_ptr<C2BlockPool>& pool) override;
54 c2_status_t drain(uint32_t drainMode,
55 const std::shared_ptr<C2BlockPool>& pool) override;
56
57 protected:
Ray Essick0d11a7e2019-03-02 20:10:30 -080058 ~C2SoftHevcEnc() override;
Roma Kauldfe650a2018-08-02 17:48:51 +053059
60 private:
61 std::shared_ptr<IntfImpl> mIntf;
62 ihevce_static_cfg_params_t mEncParams;
63 size_t mNumCores;
64 UWORD32 mIDRInterval;
65 IV_COLOR_FORMAT_T mIvVideoColorFormat;
66 UWORD32 mHevcEncProfile;
67 UWORD32 mHevcEncLevel;
68 bool mStarted;
69 bool mSpsPpsHeaderReceived;
70 bool mSignalledEos;
71 bool mSignalledError;
72 void* mCodecCtx;
73 MemoryBlockPool mConversionBuffers;
74 std::map<void*, MemoryBlock> mConversionBuffersInUse;
75 // configurations used by component in process
76 // (TODO: keep this in intf but make them internal only)
77 std::shared_ptr<C2StreamPictureSizeInfo::input> mSize;
78 std::shared_ptr<C2StreamFrameRateInfo::output> mFrameRate;
79 std::shared_ptr<C2StreamBitrateInfo::output> mBitrate;
Harish Mahendrakar57d7cc92019-02-05 08:34:55 -080080 std::shared_ptr<C2StreamBitrateModeTuning::output> mBitrateMode;
81 std::shared_ptr<C2StreamComplexityTuning::output> mComplexity;
82 std::shared_ptr<C2StreamQualityTuning::output> mQuality;
Roma Kauldfe650a2018-08-02 17:48:51 +053083
84#ifdef FILE_DUMP_ENABLE
85 char mInFile[200];
86 char mOutFile[200];
87#endif /* FILE_DUMP_ENABLE */
88
89 // profile
90 struct timeval mTimeStart;
91 struct timeval mTimeEnd;
92
93 c2_status_t initEncParams();
94 c2_status_t initEncoder();
95 c2_status_t releaseEncoder();
96 c2_status_t setEncodeArgs(ihevce_inp_buf_t* ps_encode_ip,
97 const C2GraphicView* const input,
98 uint64_t timestamp);
99 C2_DO_NOT_COPY(C2SoftHevcEnc);
100};
101
102#ifdef FILE_DUMP_ENABLE
103
104#define INPUT_DUMP_PATH "/data/local/tmp/hevc"
105#define INPUT_DUMP_EXT "yuv"
106#define OUTPUT_DUMP_PATH "/data/local/tmp/hevc"
107#define OUTPUT_DUMP_EXT "h265"
108#define GENERATE_FILE_NAMES() \
109{ \
110 GETTIME(&mTimeStart, NULL); \
111 strcpy(mInFile, ""); \
112 ALOGD("GENERATE_FILE_NAMES"); \
113 sprintf(mInFile, "%s_%ld.%ld.%s", INPUT_DUMP_PATH, mTimeStart.tv_sec, \
114 mTimeStart.tv_usec, INPUT_DUMP_EXT); \
115 strcpy(mOutFile, ""); \
116 sprintf(mOutFile, "%s_%ld.%ld.%s", OUTPUT_DUMP_PATH, \
117 mTimeStart.tv_sec, mTimeStart.tv_usec, OUTPUT_DUMP_EXT); \
118}
119
120#define CREATE_DUMP_FILE(m_filename) \
121{ \
122 FILE* fp = fopen(m_filename, "wb"); \
123 if (fp != NULL) { \
124 ALOGD("Opened file %s", m_filename); \
125 fclose(fp); \
126 } else { \
127 ALOGD("Could not open file %s", m_filename); \
128 } \
129}
130#define DUMP_TO_FILE(m_filename, m_buf, m_size) \
131{ \
132 FILE* fp = fopen(m_filename, "ab"); \
133 if (fp != NULL && m_buf != NULL) { \
134 int i; \
135 ALOGD("Dump to file!"); \
136 i = fwrite(m_buf, 1, m_size, fp); \
137 if (i != (int)m_size) { \
138 ALOGD("Error in fwrite, returned %d", i); \
139 perror("Error in write to file"); \
140 } \
141 fclose(fp); \
142 } else { \
143 ALOGD("Could not write to file %s", m_filename); \
144 if (fp != NULL) fclose(fp); \
145 } \
146}
147#else /* FILE_DUMP_ENABLE */
148#define INPUT_DUMP_PATH
149#define INPUT_DUMP_EXT
150#define OUTPUT_DUMP_PATH
151#define OUTPUT_DUMP_EXT
152#define GENERATE_FILE_NAMES()
153#define CREATE_DUMP_FILE(m_filename)
154#define DUMP_TO_FILE(m_filename, m_buf, m_size)
155#endif /* FILE_DUMP_ENABLE */
156
157} // namespace android
158
159#endif // C2_SOFT_HEVC_ENC_H__