• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019 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 #ifndef ANDROID_C2_SOFT_HEVC_ENC_H_
18 #define ANDROID_C2_SOFT_HEVC_ENC_H_
19 
20 #include <SimpleC2Component.h>
21 #include <algorithm>
22 #include <inttypes.h>
23 #include <map>
24 #include <media/stagefright/foundation/ColorUtils.h>
25 #include <utils/Vector.h>
26 
27 #include "ihevc_typedefs.h"
28 
29 namespace android {
30 
31 #define CODEC_MAX_CORES  4
32 #define MAX_B_FRAMES     1
33 #define MAX_RC_LOOKAHEAD 1
34 
35 #define DEFAULT_B_FRAMES     0
36 #define DEFAULT_RC_LOOKAHEAD 0
37 
38 #define HEVC_QP_MIN 1
39 #define HEVC_QP_MAX 51
40 
41 constexpr int32_t kDefaultInitQP = 32;
42 
43 struct C2SoftHevcEnc : public SimpleC2Component {
44     class IntfImpl;
45 
46     C2SoftHevcEnc(const char* name, c2_node_id_t id,
47                   const std::shared_ptr<IntfImpl>& intfImpl);
48 
49     // From SimpleC2Component
50     c2_status_t onInit() override;
51     c2_status_t onStop() override;
52     void onReset() override;
53     void onRelease() override;
54     c2_status_t onFlush_sm() override;
55     void process(const std::unique_ptr<C2Work>& work,
56                  const std::shared_ptr<C2BlockPool>& pool) override;
57     c2_status_t drain(uint32_t drainMode,
58                       const std::shared_ptr<C2BlockPool>& pool) override;
59 
60    protected:
61     ~C2SoftHevcEnc() override;
62 
63    private:
64     std::shared_ptr<IntfImpl> mIntf;
65     ihevce_static_cfg_params_t mEncParams;
66     size_t mNumCores;
67     UWORD32 mIDRInterval;
68     UWORD32 mIInterval;
69     UWORD32 mBframes;
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;
85     std::shared_ptr<C2StreamBitrateModeTuning::output> mBitrateMode;
86     std::shared_ptr<C2StreamComplexityTuning::output> mComplexity;
87     std::shared_ptr<C2StreamQualityTuning::output> mQuality;
88     std::shared_ptr<C2StreamGopTuning::output> mGop;
89     std::shared_ptr<C2StreamRequestSyncFrameTuning::output> mRequestSync;
90     std::shared_ptr<C2StreamColorAspectsInfo::output> mColorAspects;
91     std::shared_ptr<C2StreamPictureQuantizationTuning::output> mQpBounds;
92 #ifdef FILE_DUMP_ENABLE
93     char mInFile[200];
94     char mOutFile[200];
95 #endif /* FILE_DUMP_ENABLE */
96 
97     // profile
98     nsecs_t mTimeStart = 0;
99     nsecs_t mTimeEnd = 0;
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,
106                               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);
113     C2_DO_NOT_COPY(C2SoftHevcEnc);
114 };
115 #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     nsecs_t now = systemTime();                                           \
124     ALOGD("GENERATE_FILE_NAMES");                                         \
125     sprintf(mInFile, "%s_%" PRId64 ".%s", INPUT_DUMP_PATH,                \
126             mTimeStart, INPUT_DUMP_EXT);                                  \
127     sprintf(mutFile, "%s_%" PRId64 ".%s", OUTPUT_DUMP_PATH,               \
128             mTimeStart, OUTPUT_DUMP_EXT);                                 \
129 }
130 
131 #define CREATE_DUMP_FILE(m_filename)                 \
132 {                                                    \
133     FILE* fp = fopen(m_filename, "wb");              \
134     if (fp != NULL) {                                \
135         ALOGD("Opened file %s", m_filename);         \
136         fclose(fp);                                  \
137     } else {                                         \
138         ALOGD("Could not open file %s", m_filename); \
139     }                                                \
140 }
141 #define DUMP_TO_FILE(m_filename, m_buf, m_size)          \
142 {                                                        \
143     FILE* fp = fopen(m_filename, "ab");                  \
144     if (fp != NULL && m_buf != NULL) {                   \
145         int i;                                           \
146         ALOGD("Dump to file!");                          \
147         i = fwrite(m_buf, 1, m_size, fp);                \
148         if (i != (int)m_size) {                          \
149             ALOGD("Error in fwrite, returned %d", i);    \
150             perror("Error in write to file");            \
151         }                                                \
152         fclose(fp);                                      \
153     } else {                                             \
154         ALOGD("Could not write to file %s", m_filename); \
155         if (fp != NULL) fclose(fp);                      \
156     }                                                    \
157 }
158 #else /* FILE_DUMP_ENABLE */
159 #define INPUT_DUMP_PATH
160 #define INPUT_DUMP_EXT
161 #define OUTPUT_DUMP_PATH
162 #define OUTPUT_DUMP_EXT
163 #define GENERATE_FILE_NAMES()
164 #define CREATE_DUMP_FILE(m_filename)
165 #define DUMP_TO_FILE(m_filename, m_buf, m_size)
166 #endif /* FILE_DUMP_ENABLE */
167 
168 }  // namespace android
169 
170 #endif  // C2_SOFT_HEVC_ENC_H__
171