1 /*
2 * Copyright(c) 2019 Intel Corporation
3 * SPDX - License - Identifier: BSD - 2 - Clause - Patent
4 */
5 
6 #ifndef EbAppConfig_h
7 #define EbAppConfig_h
8 
9 #include <stdio.h>
10 
11 #include "EbSvtVp9Enc.h"
12 
13 // Define Cross-Platform 64-bit fseek() and ftell()
14 #ifdef _WIN32
15 typedef __int64 Off64;
16 #define fseeko64 _fseeki64
17 #define ftello64 _ftelli64
18 #elif defined(__GNUC__)
19 #define fseeko64 fseek
20 #define ftello64 ftell
21 #endif
22 
23 #ifndef _RSIZE_DEFINED
24 typedef size_t rsize_t;
25 #define _RSIZE_DEFINED
26 #endif /* _RSIZE_DEFINED */
27 
28 #ifndef _ERRNO_DEFINED
29 #define _ERRNO_DEFINED
30 typedef int32_t errno_t;
31 #endif  /* _ERRNO_DEFINED */
32 
33 /** The AppExitConditionType type is used to define the App main loop exit
34 conditions.
35 */
36 typedef enum AppExitConditionType
37 {
38     APP_ExitConditionNone = 0,
39     APP_ExitConditionFinished,
40     APP_ExitConditionError
41 } AppExitConditionType;
42 
43 /** The AppPortActiveType type is used to define the state of output ports in
44 the App.
45 */
46 typedef enum AppPortActiveType
47 {
48     APP_PortActive = 0,
49     APP_PortInactive
50 } AppPortActiveType;
51 
52 /** The EB_PTR type is intended to be used to pass pointers to and from the svt
53 API.  This is a 32 bit pointer and is aligned on a 32 bit word boundary.
54 */
55 typedef void * EB_PTR;
56 
57 /** The EB_NULL type is used to define the C style NULL pointer.
58 */
59 #define EB_NULL ((void*) 0)
60 
61 // memory map to be removed and replaced by malloc / free
62 typedef enum EbPtrType
63 {
64     EB_N_PTR = 0,                                   // malloc'd pointer
65     EB_A_PTR = 1,                                   // malloc'd pointer aligned
66     EB_MUTEX = 2,                                   // mutex
67     EB_SEMAPHORE = 3,                               // semaphore
68     EB_THREAD = 4                                   // thread handle
69 }EbPtrType;
70 typedef void * EbPtr;
71 typedef struct EbMemoryMapEntry
72 {
73     EbPtr                     ptr;                   // points to a memory pointer
74     EbPtrType                 ptr_type;              // pointer type
75 } EbMemoryMapEntry;
76 
77 extern    EbMemoryMapEntry *app_memory_map;          // App Memory table
78 extern    uint32_t         *app_memory_map_index;    // App Memory index
79 extern    uint64_t         *total_app_memory;        // App Memory malloc'd
80 extern    uint32_t          app_malloc_count;
81 
82 #define MAX_APP_NUM_PTR                             (0x186A0 << 2)             // Maximum number of pointers to be allocated for the app
83 
84 #define EB_APP_MALLOC(type, pointer, nElements, pointerClass, returnType) \
85     pointer = (type)malloc(nElements); \
86     if (pointer == (type)EB_NULL){ \
87         return returnType; \
88             } \
89                 else { \
90         app_memory_map[*(app_memory_map_index)].ptr_type = pointerClass; \
91         app_memory_map[(*(app_memory_map_index))++].ptr = pointer; \
92         if (nElements % 8 == 0) { \
93             *total_app_memory += (nElements); \
94                         } \
95                                 else { \
96             *total_app_memory += ((nElements) + (8 - ((nElements) % 8))); \
97             } \
98         } \
99     if (*(app_memory_map_index) >= MAX_APP_NUM_PTR) { \
100         return returnType; \
101                 } \
102     app_malloc_count++;
103 
104 #define EB_APP_MALLOC_NR(type, pointer, nElements, pointerClass,returnType) \
105     (void)returnType; \
106     pointer = (type)malloc(nElements); \
107     if (pointer == (type)EB_NULL){ \
108         returnType = EB_ErrorInsufficientResources; \
109         SVT_LOG("Malloc has failed due to insuffucient resources"); \
110         return; \
111             } \
112                 else { \
113         app_memory_map[*(app_memory_map_index)].ptrType = pointerClass; \
114         app_memory_map[(*(app_memory_map_index))++].ptr = pointer; \
115         if (nElements % 8 == 0) { \
116             *total_app_memory += (nElements); \
117                         } \
118                                 else { \
119             *total_app_memory += ((nElements) + (8 - ((nElements) % 8))); \
120             } \
121         } \
122     if (*(app_memory_map_index) >= MAX_APP_NUM_PTR) { \
123         returnType = EB_ErrorInsufficientResources; \
124         SVT_LOG("Malloc has failed due to insuffucient resources"); \
125         return; \
126                 } \
127     app_malloc_count++;
128 
129 /* string copy */
130 extern errno_t strcpy_ss(char *dest, rsize_t dmax, const char *src);
131 
132 /* fitted string copy */
133 extern errno_t strncpy_ss(char *dest, rsize_t dmax, const char *src, rsize_t slen);
134 
135 /* string length */
136 extern rsize_t eb_vp9_strnlen_ss(const char *s, rsize_t smax);
137 
138 #define EB_STRNCPY(dst, src, count) \
139     strncpy_ss(dst, sizeof(dst), src, count)
140 
141 #define EB_STRCPY(dst, size, src) \
142     strcpy_ss(dst, size, src)
143 
144 #define EB_STRCMP(target,token) \
145     strcmp(target,token)
146 
147 #define EB_STRLEN(target, max_size) \
148     eb_vp9_strnlen_ss(target, max_size)
149 
150 #define EB_APP_MEMORY() \
151     SVT_LOG("Total Number of Mallocs in App: %d\n", app_malloc_count); \
152     SVT_LOG("Total App Memory: %.2lf KB\n\n",*total_app_memory/(double)1024);
153 
154 #define MAX_CHANNEL_NUMBER      6
155 #define MAX_NUM_TOKENS          200
156 
157 #ifdef _WIN32
158 #define FOPEN(f,s,m) fopen_s(&f,s,m)
159 #else
160 #define FOPEN(f,s,m) f=fopen(s,m)
161 #endif
162 
163 typedef struct EbPerformanceContext
164 {
165 
166     /****************************************
167      * Computational Performance Data
168      ****************************************/
169     uint64_t  lib_start_time[2];       // [sec, micro_sec] including init time
170     uint64_t  encode_start_time[2];    // [sec, micro_sec] first frame sent
171 
172     double    total_execution_time;    // includes init
173     double    total_encode_time;       // not including init
174 
175     uint64_t  total_latency;
176     uint32_t  max_latency;
177 
178     uint64_t  starts_time;
179     uint64_t  start_utime;
180 
181     uint64_t  frame_count;
182 
183     double    average_speed;
184     double    average_latency;
185 
186     uint64_t  byte_count;
187 
188 }EbPerformanceContext;
189 
190 typedef struct EbConfig
191 {
192     /****************************************
193      * File I/O
194      ****************************************/
195     FILE           *config_file;
196     FILE           *input_file;
197     FILE           *bitstream_file;
198     FILE           *recon_file;
199     FILE           *error_log_file;
200     FILE           *qp_file;
201 
202     uint8_t         use_qp_file;
203     int32_t         frame_rate;
204     int32_t         frame_rate_numerator;
205     int32_t         frame_rate_denominator;
206     int32_t         injector_frame_rate;
207     uint32_t        injector;
208     uint32_t        speed_control_flag;
209     uint32_t        encoder_bit_depth;
210     uint32_t        source_width;
211     uint32_t        source_height;
212 
213     int64_t         frames_to_be_encoded;
214     int32_t         buffered_input;
215     unsigned char **sequence_buffer;
216 
217     /*****************************************
218      * Coding Structure
219      *****************************************/
220     uint32_t        base_layer_switch_mode;
221     uint8_t         enc_mode;
222     int32_t         intra_period;
223     uint32_t        pred_structure;
224 
225     /****************************************
226      * Quantization
227      ****************************************/
228     uint32_t        qp;
229 
230     /****************************************
231      * Loop Filter
232      ****************************************/
233     uint32_t       loop_filter;
234 
235     /****************************************
236      * ME Tools
237      ****************************************/
238     uint8_t        use_default_me_hme;
239     uint8_t        enable_hme_flag;
240 
241     /****************************************
242      * ME Parameters
243      ****************************************/
244     uint32_t       search_area_width;
245     uint32_t       search_area_height;
246 
247     /****************************************
248      * Rate Control
249      ****************************************/
250     uint32_t       rate_control_mode;
251     uint32_t       target_bit_rate;
252     uint32_t       max_qp_allowed;
253     uint32_t       min_qp_allowed;
254     uint32_t       vbv_max_rate;
255     uint32_t       vbv_buf_size;
256     /****************************************
257     * TUNE
258     ****************************************/
259     uint8_t        tune;
260 
261     /****************************************
262      * Annex A Parameters
263      ****************************************/
264     uint32_t       profile;
265     uint32_t       level;
266 
267     /****************************************
268      * On-the-fly Testing
269      ****************************************/
270     uint8_t        eos_flag;
271 
272     /****************************************
273     * Optimization Type
274     ****************************************/
275     uint8_t        asm_type;
276 
277     /****************************************
278      * Computational Performance Data
279      ****************************************/
280     EbPerformanceContext  performance_context;
281 
282     /****************************************
283     * Instance Info
284     ****************************************/
285     uint32_t        channel_id;
286     uint32_t        active_channel_count;
287     int32_t         target_socket;
288     uint32_t        logical_processors;
289     uint8_t         stop_encoder;         // to signal CTRL+C Event, need to stop encoding.
290 
291     uint64_t        processed_frame_count;
292     uint64_t        processed_byte_count;
293 
294 } EbConfig;
295 
296 extern void eb_config_ctor(EbConfig *config_ptr);
297 extern void eb_config_dtor(EbConfig *config_ptr);
298 
299 extern EbErrorType read_command_line(
300     int         argc,
301     char *const argv[],
302     EbConfig  **config,
303     uint32_t    num_channels,
304     EbErrorType *return_errors);
305 
306 extern uint32_t get_help(int argc, char *const argv[]);
307 extern uint32_t get_svt_version(int argc, char *const argv[]);
308 extern uint32_t get_number_of_channels(int argc, char *const argv[]);
309 
310 #endif //EbAppConfig_h
311