1 /*
2 * Copyright(c) 2019 Intel Corporation
3 * SPDX - License - Identifier: BSD - 2 - Clause - Patent
4 */
5 
6 #ifndef EbSvtVp9Enc_h
7 #define EbSvtVp9Enc_h
8 
9 #include <stdint.h>
10 #include <EbApiVersion.h>
11 
12 #ifdef __cplusplus
13 extern "C" {
14 #endif // __cplusplus
15 
16 #define EB_IndexH265EncParam                    0x7F004000
17 #define EB_IndexH265DynEncParam                 0x7F004001
18 #define EB_MAX_TEMPORAL_LAYERS                  MAX_TEMPORAL_LAYERS
19 #define EB_HME_SEARCH_AREA_COLUMN_MAX_COUNT     2
20 #define EB_HME_SEARCH_AREA_ROW_MAX_COUNT        2
21 
22 /********************************
23 * Defines
24 ********************************/
25 #define EB_ENCODERSTREAMPORT                    1
26 #define EB_ENCODERRECONPORT                     2
27 #define EB_ENCODERSTATISTICSPORT                3
28 
29 #define EbBool                  uint8_t
30 #define EB_FALSE                0
31 #define EB_TRUE                 1
32 
33 #define EB_BUFFERFLAG_EOS           0x00000001  // signals the last packet of the stream
34 #define EB_BUFFERFLAG_SHOW_EXT      0x00000002  // signals that the packet contains a show existing frame at the end
35 
36 #ifdef _WIN32
37 #define EB_API __declspec(dllexport)
38 #else
39 #define EB_API __attribute__((visibility("default")))
40 #endif
41 
42 #define EB_PICTURE           uint32_t
43 #define EB_B_PICTURE         0
44 #define EB_P_PICTURE         1
45 #define EB_I_PICTURE         2
46 #define EB_IDR_PICTURE       3
47 #define EB_NON_REF_PICTURE   4
48 #define EB_INVALID_PICTURE   0xFF
49 
50     typedef struct EbComponentType
51     {
52         uint32_t n_size;
53         void* p_component_private;
54         void* p_application_private;
55     } EbComponentType;
56 
57     typedef struct EbSvtEncInput
58     {
59         // Hosts 8 bit or 16 bit input YUV420p / YUV420p10le
60         uint8_t *luma;
61         uint8_t *cb;
62         uint8_t *cr;
63 
64         // Hosts LSB 2 bits of 10bit input when the compressed 10bit format is used
65         uint8_t *luma_ext;
66         uint8_t *cb_ext;
67         uint8_t *cr_ext;
68 
69         uint32_t y_stride;
70         uint32_t cr_stride;
71         uint32_t cb_stride;
72 
73     } EbSvtEncInput;
74 
75     typedef struct EbBufferHeaderType
76     {
77         // EbBufferHeaderType size
78         uint32_t size;
79 
80         // picture (input or output) buffer
81         uint8_t* p_buffer;
82         uint32_t n_filled_len;
83         uint32_t n_alloc_len;
84 
85         // pic private data
86         void*    p_app_private;
87         void*    wrapper_ptr;
88 
89         // pic timing param
90         uint32_t n_tick_count;
91         int64_t  dts;
92         int64_t  pts;
93 
94         // pic info
95         uint32_t qp;
96         uint32_t pic_type;
97 
98         // pic n_flags
99         uint32_t flags;
100 
101     } EbBufferHeaderType;
102 
103     typedef enum EbErrorType
104     {
105         EB_ErrorNone = 0,
106         EB_ErrorInsufficientResources = (int32_t)0x80001000,
107         EB_ErrorUndefined = (int32_t)0x80001001,
108         EB_ErrorComponentNotFound = (int32_t)0x80001003,
109         EB_ErrorInvalidComponent = (int32_t)0x80001004,
110         EB_ErrorBadParameter = (int32_t)0x80001005,
111         EB_ErrorNotImplemented = (int32_t)0x80001006,
112         EB_ErrorCreateThreadFailed = (int32_t)0x80002010,
113         EB_ErrorThreadUnresponsive = (int32_t)0x80002011,
114         EB_ErrorDestroyThreadFailed = (int32_t)0x80002012,
115         EB_ErrorNullThread = (int32_t)0x80002013,
116         EB_ErrorCreateSemaphoreFailed = (int32_t)0x80002020,
117         EB_ErrorSemaphoreUnresponsive = (int32_t)0x80002021,
118         EB_ErrorDestroySemaphoreFailed = (int32_t)0x80002022,
119         EB_ErrorCreateMutexFailed = (int32_t)0x80002030,
120         EB_ErrorMutexUnresponsive = (int32_t)0x80002031,
121         EB_ErrorDestroyMutexFailed = (int32_t)0x80002032,
122         EB_NoErrorEmptyQueue = (int32_t)0x80002033,
123         EB_ErrorMax = 0x7FFFFFFF
124     } EbErrorType;
125 
126 // Will contain the EbEncApi which will live in the EncHandle class
127 // Only modifiable during config-time.
128 typedef struct EbSvtVp9EncConfiguration
129 {
130     // Encoding preset
131     /* A preset defining the quality vs density tradeoff point that the encoding
132     * is to be performed at. 0 is the highest quality mode, 12 is the highest
133     * density mode.
134     * [0,  9] for all resolution and modes.
135     * Default is 9. */
136     uint8_t                  enc_mode;
137 
138     /* Encoder tuning for specific aim.
139     * 0 = SQ - visually optimized mode.
140     * 1 = OQ - PSNR / SSIM / VMAF optimized mode.
141     * 2 = VMAF - VMAF optimized mode
142     * Default is 1. */
143     uint8_t                  tune;
144 
145     // GOP Structure
146     /* The intra period defines the interval of frames after which you insert an
147     * Intra refresh. It is strongly recommended to set the value to multiple of
148     * 8 minus 1 the closest to 1 second (e.g. 55, 47, 31, 23 should be used for
149     * 60, 50, 30, (24 or 25) respectively.
150     * -1 = no intra update.
151     * -2 = auto.
152     * [-2 - 255]
153     * Deault is -2. */
154     int32_t                  intra_period;
155 
156     /* Prediction structure used to construct GOP. There are two main structures
157     * supported, which are: Low Delay (P or B) and Random Access.
158     *
159     * In Low Delay structure, pictures within a mini GOP refer to the previously
160     * encoded pictures in display order. In other words, pictures with display
161     * order N can only be referenced by pictures with display order greater than
162     * N, and it can only refer pictures with picture order lower than N. The Low
163     * Delay structure can be flat structured (e.g. IPPPPPPP…) or hierarchically
164     * structured. B/b pictures can be used instead of P/p pictures. However, the
165     * reference picture list 0 and the reference picture list 1 will contain the
166     * same reference picture.
167     *
168     * In Random Access structure, the B/b pictures can refer to reference pictures
169     * from both directions (past and future).
170     * [2]
171     * Default is 2. */
172     uint8_t                  pred_structure;
173 
174     /* Decides whether to use B picture or P picture in the base layer.
175     * 0 = B Picture.
176     * 1 = P Picture.
177     * Default is 0. */
178     uint32_t                 base_layer_switch_mode;
179 
180     // Input Info
181     /* The width of input source in units of picture luma pixels.
182     * [64 - 8192]
183     * Default is 0. */
184     uint32_t                 source_width;
185 
186     /* The height of input source in units of picture luma pixels.
187     * [64 - 4320]
188     * Default is 0. */
189     uint32_t                 source_height;
190 
191     /* The frequecy of images being displayed. If the number is less than 1000,
192     * the input frame rate is an integer number between 1 and 60, else the input
193     * number is in Q16 format, shifted by 16 bits, where max allowed is 240 fps.
194     * If FrameRateNumerator and FrameRateDenominator are both not equal to zero,
195     * the encoder will ignore this parameter.
196     * [0 - 2^64-1]
197     * Default is 25. */
198     uint32_t                 frame_rate;
199 
200     /* Frame rate numerator. When zero, the encoder will use –fps if
201     * FrameRateDenominator is also zero, otherwise an error is returned.
202     * [0 - 2^64-1]
203     * Default is 0. */
204     uint32_t                 frame_rate_numerator;
205 
206     /* Frame rate denominator. When zero, the encoder will use –fps if
207     * FrameRateNumerator is also zero, otherwise an error is returned.
208     * [0 - 2^64 - 1]
209     * Default is 0. */
210     uint32_t                 frame_rate_denominator;
211 
212     /* Specifies the bit depth of input video.
213     * [8] = 8 bit.
214     * Default is 8. */
215     uint32_t                 encoder_bit_depth;
216 
217     /* The maximum partitioning depth with 0 being the superblock depth
218     * Default is 4. */
219     uint32_t                 partition_depth;
220 
221     // Quantization
222     /* Initial quantization parameter for the Intra pictures used under constant
223     * qp rate control mode.
224     * [0 - 63]
225     * Default is 50. */
226     uint32_t                 qp;
227 
228     /* force qp values for every picture that are passed in the header pointer
229     * Default is 0.*/
230     EbBool                   use_qp_file;
231 
232     /* Enable picture QP scaling between hierarchical levels
233     * Default is null.*/
234     uint32_t                 enable_qp_scaling_flag;
235 
236     // Deblock Filter
237     /* Flag to enable the Deblocking Loop Filtering.
238     * Default is 1. */
239     EbBool                   loop_filter;
240 
241     /* Flag to enable the use of default ME HME parameters.
242     * Default is 1. */
243     EbBool                   use_default_me_hme;
244 
245     /* Flag to enable Hierarchical Motion Estimation.
246     * Default is 1. */
247     EbBool                   enable_hme_flag;
248 
249     // ME Parameters
250     /* Number of search positions in the horizontal direction.
251     * [1 - 256]
252     * Default depends on input resolution. */
253     uint32_t                 search_area_width;
254 
255     /* Number of search positions in the vertical direction.
256     * [1 - 256]
257     * Default depends on input resolution. */
258     uint32_t                 search_area_height;
259 
260     // Rate Control
261 
262     /* Rate control mode.
263     * 0 = Constant QP.
264     * 1 = Variable BitRate.
265     * 2 = Constant BitRate.
266     [0 - 2]
267     * Default is 0. */
268     uint32_t                 rate_control_mode;
269 
270     /* Target bitrate in bits/second, only apllicable when rate control mode is
271     * set to 1.
272     * Default is 7000000. */
273     uint32_t                 target_bit_rate;
274     /* Maxium QP value allowed for rate control use, only apllicable when rate
275     * control mode is set to 1. It has to be greater or equal to minQpAllowed.
276     * Default is 63. */
277 
278     uint32_t                 max_qp_allowed;
279     /* Minimum QP value allowed for rate control use, only apllicable when rate
280     * control mode is set to 1. It has to be smaller or equal to maxQpAllowed.
281     * Default is 0. */
282 
283     uint32_t                 min_qp_allowed;
284 
285     /* Defined set of coding tools to create bitstream.
286     * 0 = 8bit 4:2:0
287     * Default is 0. */
288     uint32_t                 profile;
289 
290     /* Constraints for bitstream in terms of max bitrate and max buffer size.
291     * 0 = auto determination.
292     * Default is 0. */
293     uint32_t                 level;
294 
295     /* Assembly instruction set used by encoder.
296     *
297     * 0 = non-AVX2, C only.
298     * 1 = up to AVX512, auto-select highest assembly insturction set supported.
299     *
300     * Default is 1. */
301     uint32_t                 asm_type;
302 
303     // Application Specific parameters
304     /* ID assigned to each channel when multiple instances are running within the
305     * same application. */
306     uint32_t                 channel_id;
307     uint32_t                 active_channel_count;
308 
309     /* Flag to enable the Speed Control functionality to achieve the real-time
310     * encoding speed defined by dynamically changing the encoding preset to meet
311     * the average speed defined in injectorFrameRate. When this parameter is set
312     * to 1 it forces –inj to be 1 -inj-frm-rt to be set to the –fps.
313     * Default is 0. */
314     uint32_t                 speed_control_flag;
315 
316     /* Frame Rate used for the injector. Recommended to match the encoder speed.
317     * [1 - 240]
318     * Default is 60. */
319     int32_t                  injector_frame_rate;
320 
321     /* The number of logical processor which encoder threads run on. If
322     * LogicalProcessorNumber and TargetSocket are not set, threads are managed by
323     * OS thread scheduler. */
324     uint32_t                logical_processors;
325 
326     /* Target socket to run on. For dual socket systems, this can specify which
327      * socket the encoder runs on.
328      *
329      * -1 = Both Sockets.
330      *  0 = Socket 0.
331      *  1 = Socket 1.
332      *
333      * Default is -1. */
334     int32_t                 target_socket;
335 
336     // Debug tools
337 
338     /* Output reconstructed yuv used for debug purposes. The value is set through
339     * ReconFile token (-o) and using the feature will affect the speed of encoder.
340     *
341     * Default is 0. */
342     uint32_t                 recon_file;
343 
344     // Input stride
345     uint32_t               input_picture_stride; // Includes padding
346 
347     // VBV Parameters
348     /* Sets the maximum rate the VBV buffer should be assumed to refill at
349     *
350     *  Default is 0. */
351     uint32_t                    vbv_max_rate;
352 
353     /* Sets the size of the VBV buffer in bits.
354     *
355     *  Default is 0. */
356     uint32_t                    vbv_buf_size;
357 
358     uint64_t                    frames_to_be_encoded;
359 
360 } EbSvtVp9EncConfiguration;
361 
362 /* STEP 1: Call the library to construct a Component Handle.
363 *
364 * Parameter:
365 * @ **p_handle      Handle to be called in the future for manipulating the
366 *                  component.
367 * @ *p_app_data      Callback data.
368 * @ *config_ptr     Pointer passed back to the client during callbacks, it will be
369 *                  loaded with default params from the library. */
370 EB_API EbErrorType eb_vp9_svt_init_handle(
371     EbComponentType** p_handle,
372     void* p_app_data,
373     EbSvtVp9EncConfiguration  *config_ptr); // config_ptr will be loaded with default params from the library
374 
375 /* STEP 2: Set all configuration parameters.
376 *
377 * Parameter:
378 * @ *svt_enc_component              Encoder handler.
379 * @ *pComponentParameterStructure  Encoder and buffer configurations will be copied to the library. */
380 EB_API EbErrorType eb_vp9_svt_enc_set_parameter(
381     EbComponentType           *svt_enc_component,
382     EbSvtVp9EncConfiguration    *p_component_parameter_structure); // pComponentParameterStructure contents will be copied to the library
383 
384 /* STEP 3: Initialize encoder and allocates memory to necessary buffers.
385 *
386 * Parameter:
387 * @ *svt_enc_component  Encoder handler. */
388 EB_API EbErrorType eb_vp9_init_encoder(
389     EbComponentType *svt_enc_component);
390 
391 /* OPTIONAL: Get stream headers at init time.
392 *
393 * Parameter:
394 * @ *svt_enc_component   Encoder handler.
395 * @ **output_stream_ptr  Output buffer. */
396 EB_API EbErrorType eb_vp9_svt_enc_stream_header(
397     EbComponentType           *svt_enc_component,
398     EbBufferHeaderType       **output_stream_ptr);
399 
400 /* OPTIONAL: Get the end of sequence Network Abstraction Layer.
401 *
402 * Parameter:
403 * @ *svt_enc_component  Encoder handler.
404 * @ **output_stream_ptr  Output stream. */
405 EB_API EbErrorType eb_vp9_svt_enc_eos_nal(
406     EbComponentType           *svt_enc_component,
407     EbBufferHeaderType       **output_stream_ptr);
408 
409 /* STEP 4: Send the picture.
410 *
411 * Parameter:
412 * @ *svt_enc_component  Encoder handler.
413 * @ *p_buffer           Header pointer, picture buffer. */
414 EB_API EbErrorType eb_vp9_svt_enc_send_picture(
415     EbComponentType      *svt_enc_component,
416     EbBufferHeaderType   *p_buffer);
417 
418 /* STEP 5: Receive packet.
419 * Parameter:
420 * @ *svt_enc_component  Encoder handler.
421 * @ **p_buffer          Header pointer to return packet with.
422 * @ pic_send_done       Flag to signal that all input pictures have been sent, this call becomes locking one this signal is 1.
423 * Non-locking call, returns EB_ErrorMax for an encode error, EB_NoErrorEmptyQueue when the library does not have any available packets.*/
424 EB_API EbErrorType eb_vp9_svt_get_packet(
425     EbComponentType      *svt_enc_component,
426     EbBufferHeaderType  **p_buffer,
427     uint8_t                pic_send_done);
428 
429 /* STEP 5-1: Release output buffer back into the pool.
430 *
431 * Parameter:
432 * @ **p_buffer          Header pointer that contains the output packet to be released. */
433 EB_API void eb_vp9_svt_release_out_buffer(
434     EbBufferHeaderType  **p_buffer);
435 
436 /* OPTIONAL: Fill buffer with reconstructed picture.
437 *
438 * Parameter:
439 * @ *svt_enc_component  Encoder handler.
440 * @ *p_buffer           Output buffer. */
441 EB_API EbErrorType eb_vp9_svt_get_recon(
442     EbComponentType      *svt_enc_component,
443     EbBufferHeaderType   *p_buffer);
444 
445 /* STEP 6: Deinitialize encoder library.
446 *
447 * Parameter:
448 * @ *svt_enc_component  Encoder handler. */
449 EB_API EbErrorType eb_vp9_deinit_encoder(
450     EbComponentType *svt_enc_component);
451 
452 /* STEP 7: Deconstruct encoder handler.
453 *
454 * Parameter:
455 * @ *svt_enc_component  Encoder handler. */
456 EB_API EbErrorType eb_vp9_deinit_handle(
457     EbComponentType  *svt_enc_component);
458 
459 #ifdef __cplusplus
460 }
461 #endif // __cplusplus
462 
463 #endif // EbSvtVp9Enc_h
464