blob: 9d90b95c631432f53a8f3e97f58abe65700409fd [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;
80
81#ifdef FILE_DUMP_ENABLE
82 char mInFile[200];
83 char mOutFile[200];
84#endif /* FILE_DUMP_ENABLE */
85
86 // profile
87 struct timeval mTimeStart;
88 struct timeval mTimeEnd;
89
90 c2_status_t initEncParams();
91 c2_status_t initEncoder();
92 c2_status_t releaseEncoder();
93 c2_status_t setEncodeArgs(ihevce_inp_buf_t* ps_encode_ip,
94 const C2GraphicView* const input,
95 uint64_t timestamp);
96 C2_DO_NOT_COPY(C2SoftHevcEnc);
97};
98
99#ifdef FILE_DUMP_ENABLE
100
101#define INPUT_DUMP_PATH "/data/local/tmp/hevc"
102#define INPUT_DUMP_EXT "yuv"
103#define OUTPUT_DUMP_PATH "/data/local/tmp/hevc"
104#define OUTPUT_DUMP_EXT "h265"
105#define GENERATE_FILE_NAMES() \
106{ \
107 GETTIME(&mTimeStart, NULL); \
108 strcpy(mInFile, ""); \
109 ALOGD("GENERATE_FILE_NAMES"); \
110 sprintf(mInFile, "%s_%ld.%ld.%s", INPUT_DUMP_PATH, mTimeStart.tv_sec, \
111 mTimeStart.tv_usec, INPUT_DUMP_EXT); \
112 strcpy(mOutFile, ""); \
113 sprintf(mOutFile, "%s_%ld.%ld.%s", OUTPUT_DUMP_PATH, \
114 mTimeStart.tv_sec, mTimeStart.tv_usec, OUTPUT_DUMP_EXT); \
115}
116
117#define CREATE_DUMP_FILE(m_filename) \
118{ \
119 FILE* fp = fopen(m_filename, "wb"); \
120 if (fp != NULL) { \
121 ALOGD("Opened file %s", m_filename); \
122 fclose(fp); \
123 } else { \
124 ALOGD("Could not open file %s", m_filename); \
125 } \
126}
127#define DUMP_TO_FILE(m_filename, m_buf, m_size) \
128{ \
129 FILE* fp = fopen(m_filename, "ab"); \
130 if (fp != NULL && m_buf != NULL) { \
131 int i; \
132 ALOGD("Dump to file!"); \
133 i = fwrite(m_buf, 1, m_size, fp); \
134 if (i != (int)m_size) { \
135 ALOGD("Error in fwrite, returned %d", i); \
136 perror("Error in write to file"); \
137 } \
138 fclose(fp); \
139 } else { \
140 ALOGD("Could not write to file %s", m_filename); \
141 if (fp != NULL) fclose(fp); \
142 } \
143}
144#else /* FILE_DUMP_ENABLE */
145#define INPUT_DUMP_PATH
146#define INPUT_DUMP_EXT
147#define OUTPUT_DUMP_PATH
148#define OUTPUT_DUMP_EXT
149#define GENERATE_FILE_NAMES()
150#define CREATE_DUMP_FILE(m_filename)
151#define DUMP_TO_FILE(m_filename, m_buf, m_size)
152#endif /* FILE_DUMP_ENABLE */
153
154} // namespace android
155
156#endif // C2_SOFT_HEVC_ENC_H__