1 /*
2  * Copyright (C) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef AVCODEC_SAMPLE_SAMPLE_INFO_H
17 #define AVCODEC_SAMPLE_SAMPLE_INFO_H
18 
19 #include <cstdint>
20 #include <string>
21 #include <condition_variable>
22 #include <queue>
23 #include "native_avcodec_base.h"
24 #include "native_avcodec_videoencoder.h"
25 #include "native_avbuffer.h"
26 
27 #define ANNEXB_INPUT_ONLY 1
28 
29 namespace OHOS {
30 namespace MediaAVCodec {
31 namespace Sample {
32 constexpr int32_t BITRATE_10M = 10 * 1024 * 1024; // 10Mbps
33 constexpr int32_t BITRATE_20M = 20 * 1024 * 1024; // 20Mbps
34 constexpr int32_t BITRATE_30M = 30 * 1024 * 1024; // 30Mbps
35 
36 constexpr double SAMPLE_DEFAULT_FRAMERATE = 999;
37 
38 enum class SampleType {
39     VIDEO_SAMPLE = 0,
40     YUV_VIEWER,
41 };
42 
43 /*   CodecType description
44  *   +-----+-----------+------------+
45  *   | Bit |     1     |     0      |
46  *   +-----+-----------+------------+
47  *   |Field| IsEncoder | IsSoftware |
48  *   +-----+-----------+------------+
49  */
50 enum CodecType {
51     VIDEO_HW_DECODER = (0 << 1) | 0,
52     VIDEO_SW_DECODER = (0 << 1) | 1,
53     VIDEO_HW_ENCODER = (1 << 1) | 0,
54     VIDEO_SW_ENCODER = (1 << 1) | 1,
55 };
56 
57 enum DataProducerType {
58     DATA_PRODUCER_TYPE_DEMUXER,
59     DATA_PRODUCER_TYPE_BITSTREAM_READER,
60     DATA_PRODUCER_TYPE_RAW_DATA_READER,
61 };
62 
63 enum BitstreamType {
64     BITSTREAM_TYPE_ANNEXB,
65     BITSTREAM_TYPE_AVCC,
66 };
67 
68 enum DemuxerSourceType {
69     DEMUXER_SOURCE_TYPE_FILE,
70     DEMUXER_SOURCE_TYPE_URI,
71 };
72 
73 /*   CodecRunMode description
74  *   +-----+-------+--------------+
75  *   | Bit |   1   |      0       |
76  *   +-----+-------+--------------+
77  *   |Field| API11 | IsBufferMode |
78  *   +-----+-------+--------------+
79  */
80 enum CodecRunMode {
81     API10_SURFACE   = (0 << 1) | 0,
82     API10_BUFFER    = (0 << 1) | 1,
83     API11_SURFACE   = (1 << 1) | 0,
84     API11_BUFFER    = (1 << 1) | 1,
85 };
86 
87 enum SampleState {
88     SAMPLE_STATE_UNINITIALIZED,
89     SAMPLE_STATE_INITIALIZED,
90     SAMPLE_STATE_STARTED,
91     SAMPLE_STATE_FLUSHED,
92     SAMPLE_STATE_STOPPED,
93 };
94 
95 struct DataProducerInfo {
96     DataProducerType dataProducerType = DATA_PRODUCER_TYPE_DEMUXER;
97     BitstreamType bitstreamType = BITSTREAM_TYPE_ANNEXB;
98     OH_AVSeekMode seekMode = SEEK_MODE_PREVIOUS_SYNC;
99     DemuxerSourceType demuxerSourceType = DEMUXER_SOURCE_TYPE_FILE;
100 };
101 
102 enum CodecConsumerType {
103     CODEC_COMSUMER_TYPE_DEFAULT = 0,
104     CODEC_COMSUMER_TYPE_DECODER_RENDER_OUTPUT,
105 };
106 
107 enum ThreadSleepMode {
108     THREAD_SLEEP_MODE_INPUT_SLEEP = 0,
109     THREAD_SLEEP_MODE_OUTPUT_SLEEP,
110 };
111 
112 struct SampleInfo {
113     CodecType codecType = VIDEO_HW_DECODER;
114     std::string inputFilePath;
115     std::string codecMime = OH_AVCODEC_MIMETYPE_VIDEO_AVC;
116     int32_t videoWidth = 1280;
117     int32_t videoHeight = 720;
118     double frameRate = SAMPLE_DEFAULT_FRAMERATE;
119     int64_t bitrate = 10 * 1024 * 1024; // 10Mbps;
120 
121     CodecRunMode codecRunMode = API11_SURFACE;
122     int32_t frameInterval = -1;
123     std::shared_ptr<NativeWindow> window = nullptr;
124     int32_t sampleRepeatTimes = 0;
125     int32_t demoRepeatTimes = 1;
126     OH_AVPixelFormat pixelFormat = AV_PIXEL_FORMAT_NV12;
127     bool needDumpInput = false;
128     bool needDumpOutput = false;
129     uint32_t maxFrames = UINT32_MAX;
130     uint32_t bitrateMode = CBR;
131     DataProducerInfo dataProducerInfo = DataProducerInfo();
132     int32_t profile = AVC_PROFILE_BASELINE;
133     int64_t videoDuration = 0;
134     std::string outputFilePath;
135     CodecConsumerType codecConsumerType = CODEC_COMSUMER_TYPE_DEFAULT;
136     ThreadSleepMode threadSleepMode = THREAD_SLEEP_MODE_INPUT_SLEEP;
137     int32_t encoderSurfaceMaxInputBuffer = 0;
138     int32_t pauseBeforeRunSample = 0;
139     int32_t videoStrideWidth = 0;
140     int32_t videoSliceHeight = 0;
141     int32_t iFrameInterval = 2'000;
142     int32_t videoDecoderOutputColorspace = -1;
143     SampleType sampleType = SampleType::VIDEO_SAMPLE;
144 };
145 
146 struct CodecBufferInfo {
147     uint32_t bufferIndex = 0;
148     uintptr_t *buffer = nullptr;
149     uint8_t *bufferAddr = nullptr;
150     OH_AVCodecBufferAttr attr = {0, 0, 0, AVCODEC_BUFFER_FLAGS_NONE};
151 
CodecBufferInfoCodecBufferInfo152     CodecBufferInfo(uint8_t *addr) : bufferAddr(addr) {};
153     CodecBufferInfo(OH_AVCodecBufferAttr bufferAttr) : attr(bufferAttr) {};
154     CodecBufferInfo(uint8_t *addr, int32_t bufferSize)
155         : bufferAddr(addr), attr({0, bufferSize, 0, AVCODEC_BUFFER_FLAGS_NONE}) {};
156     CodecBufferInfo(uint32_t argBufferIndex, OH_AVMemory *argBuffer, OH_AVCodecBufferAttr argAttr)
157         : bufferIndex(argBufferIndex), buffer(reinterpret_cast<uintptr_t *>(argBuffer)), attr(argAttr) {};
158     CodecBufferInfo(uint32_t argBufferIndex, OH_AVMemory *argBuffer)
159         : bufferIndex(argBufferIndex), buffer(reinterpret_cast<uintptr_t *>(argBuffer)) {};
160     CodecBufferInfo(uint32_t argBufferIndex, OH_AVBuffer *argBuffer)
161         : bufferIndex(argBufferIndex), buffer(reinterpret_cast<uintptr_t *>(argBuffer))
162     {
163         OH_AVBuffer_GetBufferAttr(argBuffer, &attr);
164     };
165 };
166 static inline CodecBufferInfo eosBufferInfo =
167     CodecBufferInfo(OH_AVCodecBufferAttr({0, 0, 0, AVCODEC_BUFFER_FLAGS_EOS}));
168 } // Sample
169 } // MediaAVCodec
170 } // OHOS
171 #endif // AVCODEC_SAMPLE_SAMPLE_INFO_H