OSDN Git Service

Follow the HW spec to configure the buffer cache on Gen9+
[android-x86/hardware-intel-common-vaapi.git] / src / gen9_vdenc.h
1 /*
2  * Copyright © 2015 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sub license, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial portions
14  * of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
19  * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
20  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  * Authors:
25  *    Xiang Haihao <haihao.xiang@intel.com>
26  *
27  */
28
29 #ifndef GEN9_VDENC_H
30 #define GEN9_VDENC_H
31
32 #include <drm.h>
33 #include <i915_drm.h>
34 #include <intel_bufmgr.h>
35
36 #include "i965_gpe_utils.h"
37 #include "i965_encoder.h"
38
39 struct encode_state;
40
41 #define HUC_BRC_INIT_RESET                      4
42 #define HUC_BRC_UPDATE                          5
43
44 #define HUC_DMEM_DATA_OFFSET                    0x2000
45
46 #define NUM_OF_BRC_PAK_PASSES                   2
47
48 #define HUC_BRC_HISTORY_BUFFER_SIZE             832
49 #define HUC_BRC_STREAM_INOUT_BUFFER_SIZE        4096
50 #define VDENC_STATISTICS_SIZE                   128
51 #define PAK_STATISTICS_SIZE                     256
52 #define VDENC_AVC_IMAGE_STATE_SIZE              (sizeof(struct gen9_mfx_avc_img_state) + sizeof(struct gen9_vdenc_img_state) + 2 * sizeof(int))
53 #define HME_DETECTION_SUMMARY_BUFFER_SIZE       256
54 #define BRC_CONSTANT_DATA_SIZE                  4096
55 #define BRC_DEBUG_OUTPUT_SIZE                   4096
56
57 #define HUC_STATUS_MMIO_OFFSET                  0x0D000
58
59 #define SCALE_FACTOR_4X                         4
60
61 #define VDENC_FRAME_I                           0
62 #define VDENC_FRAME_P                           1
63
64 #define VDENC_LUTMODE_INTRA_NONPRED             0x00
65 #define VDENC_LUTMODE_INTRA                     0x01
66 #define VDENC_LUTMODE_INTRA_16x16               0x01
67 #define VDENC_LUTMODE_INTRA_8x8                 0x02
68 #define VDENC_LUTMODE_INTRA_4x4                 0x03
69 #define VDENC_LUTMODE_INTER_16x8                0x04
70 #define VDENC_LUTMODE_INTER_8x16                0x04
71 #define VDENC_LUTMODE_INTER_8X8Q                0x05
72 #define VDENC_LUTMODE_INTER_8X4Q                0x06
73 #define VDENC_LUTMODE_INTER_4X8Q                0x06
74 #define VDENC_LUTMODE_INTER_16x8_FIELD          0x06
75 #define VDENC_LUTMODE_INTER_4X4Q                0x07
76 #define VDENC_LUTMODE_INTER_8x8_FIELD           0x07
77 #define VDENC_LUTMODE_INTER                     0x08
78 #define VDENC_LUTMODE_INTER_16x16               0x08
79 #define VDENC_LUTMODE_INTER_BWD                 0x09
80 #define VDENC_LUTMODE_REF_ID                    0x0A
81 #define VDENC_LUTMODE_INTRA_CHROMA              0x0B
82
83 struct gen9_mfx_avc_img_state
84 {
85     union {
86         struct {
87             uint32_t dword_length:16;
88             uint32_t sub_opcode_b:5;
89             uint32_t sub_opcode_a:3;
90             uint32_t command_opcode:3;
91             uint32_t pipeline:2;
92             uint32_t command_type:3;
93         };
94
95         uint32_t value;
96     } dw0;
97
98     struct {
99         uint32_t frame_size_in_mbs_minus1:16;
100         uint32_t pad0:16;
101     } dw1;
102
103     struct {
104         uint32_t frame_width_in_mbs_minus1:8;
105         uint32_t pad0:8;
106         uint32_t frame_height_in_mbs_minus1:8;
107         uint32_t pad1:8;
108     } dw2;
109
110     struct {
111         uint32_t pad0:8;
112         uint32_t image_structure:2;
113         uint32_t weighted_bipred_idc:2;
114         uint32_t weighted_pred_flag:1;
115         uint32_t brc_domain_rate_control_enable:1;
116         uint32_t pad1:2;
117         uint32_t chroma_qp_offset:5;
118         uint32_t pad2:3;
119         uint32_t second_chroma_qp_offset:5;
120         uint32_t pad3:3;
121     } dw3;
122
123     struct {
124         uint32_t field_picture_flag:1;
125         uint32_t mbaff_mode_active:1;
126         uint32_t frame_mb_only_flag:1;
127         uint32_t transform_8x8_idct_mode_flag:1;
128         uint32_t direct_8x8_interface_flag:1;
129         uint32_t constrained_intra_prediction_flag:1;
130         uint32_t current_img_dispoable_flag:1;
131         uint32_t entropy_coding_flag:1;
132         uint32_t mb_mv_format_flag:1;
133         uint32_t pad0:1;
134         uint32_t chroma_format_idc:2;
135         uint32_t mv_unpacked_flag:1;
136         uint32_t insert_test_flag:1;
137         uint32_t load_slice_pointer_flag:1;
138         uint32_t macroblock_stat_enable:1;
139         uint32_t minimum_frame_size:16;
140     } dw4;
141
142     struct {
143         uint32_t intra_mb_max_bit_flag:1;
144         uint32_t inter_mb_max_bit_flag:1;
145         uint32_t frame_size_over_flag:1;
146         uint32_t frame_size_under_flag:1;
147         uint32_t pad0:3;
148         uint32_t intra_mb_ipcm_flag:1;
149         uint32_t pad1:1;
150         uint32_t mb_rate_ctrl_flag:1;
151         uint32_t min_frame_size_units:2;
152         uint32_t inter_mb_zero_cbp_flag:1;
153         uint32_t pad2:3;
154         uint32_t non_first_pass_flag:1;
155         uint32_t pad3:10;
156         uint32_t aq_chroma_disable:1;
157         uint32_t aq_rounding:3;
158         uint32_t aq_enable:1;
159     } dw5;
160
161     struct {
162         uint32_t intra_mb_max_size:12;
163         uint32_t pad0:4;
164         uint32_t inter_mb_max_size:12;
165         uint32_t pad1:4;
166     } dw6;
167
168     struct {
169         uint32_t pad0;
170     } dw7;
171
172     struct {
173         uint32_t slice_delta_qp_max0:8;
174         uint32_t slice_delta_qp_max1:8;
175         uint32_t slice_delta_qp_max2:8;
176         uint32_t slice_delta_qp_max3:8;
177     } dw8;
178
179     struct {
180         uint32_t slice_delta_qp_min0:8;
181         uint32_t slice_delta_qp_min1:8;
182         uint32_t slice_delta_qp_min2:8;
183         uint32_t slice_delta_qp_min3:8;
184     } dw9;
185
186     struct {
187         uint32_t frame_bitrate_min:14;
188         uint32_t frame_bitrate_min_unit_mode:1;
189         uint32_t frame_bitrate_min_unit:1;
190         uint32_t frame_bitrate_max:14;
191         uint32_t frame_bitrate_max_unit_mode:1;
192         uint32_t frame_bitrate_max_unit:1;
193     } dw10;
194
195     struct {
196         uint32_t frame_bitrate_min_delta:15;
197         uint32_t pad0:1;
198         uint32_t frame_bitrate_max_delta:15;
199         uint32_t pad1:1;
200     } dw11;
201
202     struct {
203         uint32_t pad0:18;
204         uint32_t vad_error_logic:1;
205         uint32_t pad1:13;
206     } dw12;
207
208     struct {
209         uint32_t pic_qp_init_minus26:8;
210         uint32_t pic_num_ref_idx_l0_active_minus1:6;
211         uint32_t pad0:2;
212         uint32_t pic_num_ref_idx_l1_active_minus1:6;
213         uint32_t pad1:2;
214         uint32_t num_ref_frames:5;
215         uint32_t is_curr_pic_has_mmco5:1;
216     } dw13;
217
218     struct {
219         uint32_t pic_order_present_flag:1;
220         uint32_t delta_pic_order_always_zero_flag:1;
221         uint32_t pic_order_cnt_type:2;
222         uint32_t pad0:4;
223         uint32_t slice_group_map_type:3;
224         uint32_t redundant_pic_cnt_present_flag:1;
225         uint32_t num_slice_groups_minus1:3;
226         uint32_t deblock_filter_ctrl_present_flag:1;
227         uint32_t log2_max_frame_num_minus4:8;
228         uint32_t log2_max_pic_order_cnt_lsb_minus4:8;
229     } dw14;
230
231     struct {
232         uint32_t slice_group_change_rate:16;
233         uint32_t curr_pic_frame_num:16;
234     } dw15;
235
236     struct {
237         uint32_t current_frame_view_id:10;
238         uint32_t pad0:2;
239         uint32_t max_view_idx_l0:4;
240         uint32_t pad1:2;
241         uint32_t max_view_idx_l1:4;
242         uint32_t pad2:9;
243         uint32_t inter_view_order_disable:1;
244     } dw16;
245
246     struct {
247         uint32_t fqp:3;                         // Must be zero for SKL
248         uint32_t fqp_offset:3;                  // Must be zero for SKL
249         uint32_t pad0:2;
250         uint32_t ext_brc_dm_stat_en:1;          // Must be zero for SKL
251         uint32_t pad1:7;
252         uint32_t brc_dm_avg_mb_qp:6;            // Must be zero for SKL
253         uint32_t pad2:10;
254     } dw17;
255
256     struct {
257         uint32_t brc_domain_target_frame_size;
258     } dw18;
259
260     struct {
261         uint32_t threshold_size_in_bytes;
262     } dw19;
263
264     struct {
265         uint32_t target_slice_size_in_bytes;
266     } dw20;
267 };
268
269 struct gen9_image_state_cost
270 {
271     struct {
272         uint32_t mv0_cost:8;
273         uint32_t mv1_cost:8;
274         uint32_t mv2_cost:8;
275         uint32_t mv3_cost:8;
276     } dw0;
277
278     struct {
279         uint32_t mv4_cost:8;
280         uint32_t mv5_cost:8;
281         uint32_t mv6_cost:8;
282         uint32_t mv7_cost:8;
283     } dw1;
284 };
285
286 struct gen9_vdenc_img_state
287 {
288     union {
289         struct {
290             uint32_t dword_length:12;
291             uint32_t pad0:4;
292             uint32_t sub_opcode_b:5;
293             uint32_t sub_opcode_a:2;
294             uint32_t command_opcode:4;
295             uint32_t pipeline:2;
296             uint32_t command_type:3;
297         };
298
299         uint32_t value;
300     } dw0;
301
302     struct {
303         uint32_t pad0:2;
304         uint32_t bidirectional_mix_disable:1;
305         uint32_t pad1:1;
306         uint32_t time_budget_overflow_check:1;
307         uint32_t pad2:1;
308         uint32_t extended_pak_obj_cmd_enable:1;
309         uint32_t transform_8x8_flag:1;
310         uint32_t vdenc_l1_cache_priority:2;
311         uint32_t pad3:22;
312     } dw1;
313
314     struct {
315         uint32_t pad0:16;
316         uint32_t bidirectional_weight:6;
317         uint32_t pad1:6;
318         uint32_t unidirection_mix_disable:1;
319         uint32_t pad2:3;
320     } dw2;
321
322     struct {
323         uint32_t pad0:16;
324         uint32_t picture_width:16;
325     } dw3;
326
327     struct {
328         uint32_t pad0:12;
329         uint32_t subpel_mode:2;
330         uint32_t pad1:3;
331         uint32_t forward_transform_skip_check_enable:1;
332         uint32_t bme_disable_for_fbr_message:1;
333         uint32_t block_based_skip_enabled:1;
334         uint32_t inter_sad_measure_adjustment:2;
335         uint32_t intra_sad_measure_adjustment:2;
336         uint32_t sub_macroblock_sub_partition_mask:7;
337         uint32_t block_based_skip_type:1;
338     } dw4;
339
340     struct {
341         uint32_t picture_height_minus1:16;
342         uint32_t cre_prefetch_enable:1;
343         uint32_t hme_ref1_disable:1;
344         uint32_t mb_slice_threshold_value:4;
345         uint32_t pad0:4;
346         uint32_t constrained_intra_prediction_flag:1;
347         uint32_t pad1:2;
348         uint32_t picture_type:2;
349         uint32_t pad2:1;
350     } dw5;
351
352     struct {
353         uint32_t slice_macroblock_height_minus1:16;
354         uint32_t pad0:16;
355     } dw6;
356
357     struct {
358         uint32_t pad0;
359     } dw7;
360
361     struct {
362         uint32_t luma_intra_partition_mask:5;
363         uint32_t non_skip_zero_mv_const_added:1;
364         uint32_t non_skip_mb_mode_const_added:1;
365         uint32_t pad0:9;
366         uint32_t mv_cost_scaling_factor:2;
367         uint32_t bilinear_filter_enable:1;
368         uint32_t pad1:3;
369         uint32_t ref_id_cost_mode_select:1;
370         uint32_t pad2:9;
371     } dw8;
372
373     struct {
374         uint32_t mode0_cost:8;
375         uint32_t mode1_cost:8;
376         uint32_t mode2_cost:8;
377         uint32_t mode3_cost:8;
378     } dw9;
379
380     struct {
381         uint32_t mode4_cost:8;
382         uint32_t mode5_cost:8;
383         uint32_t mode6_cost:8;
384         uint32_t mode7_cost:8;
385     } dw10;
386
387     struct {
388         uint32_t mode8_cost:8;
389         uint32_t mode9_cost:8;
390         uint32_t ref_id_cost:8;
391         uint32_t chroma_intra_mode_cost:8;
392     } dw11;
393
394     struct {
395         struct gen9_image_state_cost mv_cost;
396     } dw12_13;
397
398     struct {
399         uint32_t qp_prime_y:8;
400         uint32_t pad0:16;
401         uint32_t target_size_in_word:8;
402     } dw14;
403
404     struct {
405         uint32_t pad0;
406     } dw15;
407
408     struct {
409         uint32_t pad0;
410     } dw16;
411
412     struct {
413         uint32_t avc_intra_4x4_mode_mask:9;
414         uint32_t pad0:7;
415         uint32_t avc_intra_8x8_mode_mask:9;
416         uint32_t pad1:7;
417     } dw17;
418
419     struct {
420         uint32_t avc_intra_16x16_mode_mask:4;
421         uint32_t avc_intra_chroma_mode_mask:4;
422         uint32_t intra_compute_type_intra_compute_type:2;
423         uint32_t pad0:22;
424     } dw18;
425
426     struct {
427         uint32_t pad0;
428     } dw19;
429
430     struct {
431         uint32_t penalty_for_intra_16x16_non_dc_prediction:8;
432         uint32_t penalty_for_intra_8x8_non_dc_prediction:8;
433         uint32_t penalty_for_intra_4x4_non_dc_prediction:8;
434         uint32_t pad0:8;
435     } dw20;
436
437     struct {
438         uint32_t pad0;
439     } dw21;
440
441     struct {
442         uint32_t panic_mode_mb_threadhold:16;
443         uint32_t small_mb_size_in_word:8;
444         uint32_t large_mb_size_in_word:8;
445     } dw22;
446
447     struct {
448         uint32_t l0_number_of_reference_minus1:8;
449         uint32_t pad0:8;
450         uint32_t l1_number_of_reference_minus1:8;
451         uint32_t pad1:8;
452     } dw23;
453
454     struct {
455         uint32_t pad0;
456     } dw24;
457
458     struct {
459         uint32_t pad0;
460     } dw25;
461
462     struct {
463         uint32_t pad0:8;
464         uint32_t hme_ref_windows_combining_threshold:8;
465         uint32_t pad1:16;
466     } dw26;
467
468     struct {
469         uint32_t max_hmv_r:16;
470         uint32_t max_vmv_r:16;
471     } dw27;
472
473     struct {
474         struct gen9_image_state_cost hme_mv_cost;
475     } dw28_29;
476
477     struct {
478         uint32_t roi_qp_adjustment_for_zone0:4;
479         uint32_t roi_qp_adjustment_for_zone1:4;
480         uint32_t roi_qp_adjustment_for_zone2:4;
481         uint32_t roi_qp_adjustment_for_zone3:4;
482         uint32_t qp_adjustment_for_shape_best_intra_4x4_winner:4;
483         uint32_t qp_adjustment_for_shape_best_intra_8x8_winner:4;
484         uint32_t qp_adjustment_for_shape_best_intra_16x16_winner:4;
485         uint32_t pad0:4;
486     } dw30;
487
488     struct {
489         uint32_t best_distortion_qp_adjustment_for_zone0:4;
490         uint32_t best_distortion_qp_adjustment_for_zone1:4;
491         uint32_t best_distortion_qp_adjustment_for_zone2:4;
492         uint32_t best_distortion_qp_adjustment_for_zone3:4;
493         uint32_t offset0_for_zone0_neg_zone1_boundary:16;
494     } dw31;
495
496     struct {
497         uint32_t offset1_for_zone1_neg_zone2_boundary:16;
498         uint32_t offset2_for_zone2_neg_zone3_boundary:16;
499     } dw32;
500
501     struct {
502         uint32_t qp_range_check_upper_bound:8;
503         uint32_t qp_range_check_lower_bound:8;
504         uint32_t pad0:8;
505         uint32_t qp_range_check_value:4;
506         uint32_t pad1:4;
507     } dw33;
508
509     struct {
510         uint32_t roi_enable:1;
511         uint32_t fwd_predictor0_mv_enable:1;
512         uint32_t bdw_predictor1_mv_enable:1;
513         uint32_t mb_level_qp_enable:1;
514         uint32_t target_size_in_words_mb_max_size_in_words_mb_enable:1;
515         uint32_t pad0:3;
516         uint32_t ppmv_disable:1;
517         uint32_t coefficient_clamp_enable:1;
518         uint32_t long_term_reference_frame_bwd_ref0_indicator:1;
519         uint32_t long_term_reference_frame_fwd_ref2_indicator:1;
520         uint32_t long_term_reference_frame_fwd_ref1_indicator:1;
521         uint32_t long_term_reference_frame_fwd_ref0_indicator:1;
522         uint32_t image_state_qp_override:1;
523         uint32_t pad1:1;
524         uint32_t midpoint_distortion:16;
525     } dw34;
526 };
527
528 struct gen9_vdenc_streamin_state
529 {
530     struct {
531         uint32_t roi_selection:8;
532         uint32_t force_intra:1;
533         uint32_t force_skip:1;
534         uint32_t pad0:22;
535     } dw0;
536
537     struct {
538         uint32_t qp_prime_y:8;
539         uint32_t target_size_in_word:8;
540         uint32_t max_size_in_word:8;
541         uint32_t pad0:8;
542     } dw1;
543
544     struct {
545         uint32_t fwd_predictor_x:16;
546         uint32_t fwd_predictor_y:16;
547     } dw2;
548
549     struct {
550         uint32_t bwd_predictore_x:16;
551         uint32_t bwd_predictore_y:16;
552     } dw3;
553
554     struct {
555         uint32_t fwd_ref_id0:4;
556         uint32_t bdw_ref_id0:4;
557         uint32_t pad0:24;
558     } dw4;
559
560     struct {
561         uint32_t pad0[11];
562     } dw5_15;
563 };
564
565 struct huc_brc_update_constant_data
566 {
567     uint8_t global_rate_qp_adj_tab_i[64];
568     uint8_t global_rate_qp_adj_tab_p[64];
569     uint8_t global_rate_qp_adj_tab_b[64];
570     uint8_t dist_threshld_i[10];
571     uint8_t dist_threshld_p[10];
572     uint8_t dist_threshld_b[10];
573     uint8_t dist_qp_adj_tab_i[81];
574     uint8_t dist_qp_adj_tab_p[81];
575     uint8_t dist_qp_adj_tab_b[81];
576     int8_t  buf_rate_adj_tab_i[72];
577     int8_t  buf_rate_adj_tab_p[72];
578     int8_t  buf_rate_adj_tab_b[72];
579     uint8_t frame_size_min_tab_p[9];
580     uint8_t frame_size_min_tab_b[9];
581     uint8_t frame_size_min_tab_i[9];
582     uint8_t frame_size_max_tab_p[9];
583     uint8_t frame_size_max_tab_b[9];
584     uint8_t frame_size_max_tab_i[9];
585     uint8_t frame_size_scg_tab_p[9];
586     uint8_t frame_size_scg_tab_b[9];
587     uint8_t frame_size_scg_tab_i[9];
588     /* cost table 14*42 = 588 bytes */
589     uint8_t i_intra_non_pred[42];
590     uint8_t i_intra_16x16[42];
591     uint8_t i_intra_8x8[42];
592     uint8_t i_intra_4x4[42];
593     uint8_t i_intra_chroma[42];
594     uint8_t p_intra_non_pred[42];
595     uint8_t p_intra_16x16[42];
596     uint8_t p_intra_8x8[42];
597     uint8_t p_intra_4x4[42];
598     uint8_t p_intra_chroma[42];
599     uint8_t p_inter_16x8[42];
600     uint8_t p_inter_8x8[42];
601     uint8_t p_inter_16x16[42];
602     uint8_t p_ref_id[42];
603     uint8_t hme_mv_cost[8][42];
604     uint8_t pad0[42];
605 };
606
607 struct huc_brc_init_dmem
608 {
609     uint8_t     brc_func;                       // 0: Init; 2: Reset
610     uint8_t     os_enabled;                     // Always 1
611     uint8_t     pad0[2];
612     uint16_t    brc_flag;                       // ICQ or CQP with slice size control: 0x00 CBR: 0x10; VBR: 0x20; VCM: 0x40; LOWDELAY: 0x80.
613     uint16_t    pad1;
614     uint16_t    frame_width;                    // Luma width in bytes
615     uint16_t    frame_height;                   // Luma height in bytes
616     uint32_t    target_bitrate;                 // target bitrate, set by application
617     uint32_t    min_rate;                       // 0
618     uint32_t    max_rate;                       // Maximum bit rate in bits per second (bps).
619     uint32_t    buffer_size;                    // buffer size in bits
620     uint32_t    init_buffer_fullness;           // initial buffer fullness in bits
621     uint32_t    profile_level_max_frame;        // user defined. refer to AVC BRC HLD for conformance check and correction
622     uint32_t    frame_rate_m;                   // FrameRateM is the number of frames in FrameRateD
623     uint32_t    frame_rate_d;                   // If driver gets this FrameRateD from VUI, it is the num_units_in_tick field (32 bits UINT).
624     uint16_t    num_p_in_gop;                   // number of P frames in a GOP
625     uint16_t    num_b_in_gop;                   // number of B frames in a GOP
626     uint16_t    min_qp;                         // 10
627     uint16_t    max_qp;                         // 51
628     int8_t      dev_thresh_pb0[8];              // lowdelay ? (-45, -33, -23, -15, -8, 0, 15, 25) : (-46, -38, -30, -23, 23, 30, 40, 46)
629     int8_t      dev_thresh_vbr0[8];             // lowdelay ? (-45, -35, -25, -15, -8, 0, 20, 40) : (-46, -40, -32, -23, 56, 64, 83, 93)
630     int8_t      dev_thresh_i0[8];               // lowdelay ? (-40, -30, -17, -10, -5, 0, 10, 20) : (-43, -36, -25, -18, 18, 28, 38, 46)
631     uint8_t     init_qp_ip;                     // Initial QP for I and P
632
633     uint8_t     pad2;                           // Reserved
634     uint8_t     init_qp_b;                      // Initial QP for B
635     uint8_t     mb_qp_ctrl;                     // Enable MB level QP control (global)
636     uint8_t     slice_size_ctrl_en;             // Enable slice size control
637     int8_t      intra_qp_delta[3];              // set to zero for all by default
638     int8_t      skip_qp_delta;                  // Reserved
639     int8_t      dist_qp_delta[4];               // lowdelay ? (-5, -2, 2, 5) : (0, 0, 0, 0)
640     uint8_t     oscillation_qp_delta;           // BRCFLAG_ISVCM ? 16 : 0
641     uint8_t     first_iframe_no_hrd_check;      // BRCFLAG_ISVCM ? 1 : 0
642     uint8_t     skip_frame_enable_flag;
643     uint8_t     top_qp_delta_thr_for_2nd_pass;  // =1. QP Delta threshold for second pass.
644     uint8_t     top_frame_size_threshold_for_2nd_pass;          // lowdelay ? 10 : 50. Top frame size threshold for second pass
645     uint8_t     bottom_frame_size_threshold_for_2nd_pass;       // lowdelay ? 10 : 200. Bottom frame size threshold for second pass
646     uint8_t     qp_select_for_first_pass;       // lowdelay ? 0 : 1. =0 to use previous frame final QP; or =1 to use (targetQP + previousQP) / 2.
647     uint8_t     mb_header_compensation;         // Reserved
648     uint8_t     over_shoot_carry_flag;          // set to zero by default
649     uint8_t     over_shoot_skip_frame_pct;      // set to zero by default
650     uint8_t     estrate_thresh_p0[7];           // 4, 8, 12, 16, 20, 24, 28
651     uint8_t     estrate_thresh_b0[7];           // 4, 8, 12, 16, 20, 24, 28
652     uint8_t     estrate_thresh_i0[7];           // 4, 8, 12, 16, 20, 24, 28
653     uint8_t     fqp_enable;                     // ExtendedBrcDomainEn
654     uint8_t     scenario_info;                  // 0: UNKNOWN, 1: DISPLAYREMOTING, 2: VIDEOCONFERENCE, 3: ARCHIVE, 4: LIVESTREAMING.
655     uint8_t     static_Region_streamin;         // should be programmed from par file
656     uint8_t     delta_qp_adaptation;            // =1,
657     uint8_t     max_crf_quality_factor;         // =52,
658     uint8_t     crf_quality_factor;             // =25,
659     uint8_t     bottom_qp_delta_thr_for_2nd_pass;// =1. QP Delta threshold for second pass.
660     uint8_t     sliding_window_size;            // =30, the window size (in frames) used to compute bit rate
661     uint8_t     sliding_widow_rc_enable;        // =0, sliding window based rate control (SWRC) disabled, 1: enabled
662     uint8_t     sliding_window_max_rate_ratio;  // =120, ratio between the max rate within the window and average target bitrate
663     uint8_t     low_delay_golden_frame_boost;   // only for lowdelay mode, 0 (default): no boost for I and scene change frames, 1: boost
664     uint8_t     pad3[61];                       // Must be zero
665 };
666
667 struct huc_brc_update_dmem
668 {
669     uint8_t     brc_func;                       // =1 for Update, other values are reserved for future use
670     uint8_t     pad0[3];
671     uint32_t    target_size;                    // refer to AVC BRC HLD for calculation
672     uint32_t    frame_number;                   // frame number
673     uint32_t    peak_tx_bits_per_frame;         // current global target bits - previous global target bits (global target bits += input bits per frame)
674     uint32_t    frame_budget;                   // target time counter
675     uint32_t    frame_byte_count;               // PAK output via MMIO
676     uint32_t    timing_budget_overflow;         // PAK output via MMIO
677     uint32_t    slice_size_violation;           // PAK output via MMIO
678     uint32_t    ipcm_non_conformant;            // PAK output via MMIO
679
680     uint16_t    start_global_adjust_frame[4];   // 10, 50, 100, 150
681     uint16_t    mb_budget[52];                  // MB bugdet for QP 0 - 51.
682     uint16_t    target_slice_size;              // target slice size
683     uint16_t    slcsz_thr_deltai[42];           // slice size threshold delta for I frame
684     uint16_t    slcsz_thr_deltap[42];           // slice size threshold delta for P frame
685     uint16_t    num_of_frames_skipped;          // Recording how many frames have been skipped.
686     uint16_t    skip_frame_size;                // Recording the skip frame size for one frame. =NumMBs * 1, assuming one bit per mb for skip frame.
687     uint16_t    static_region_pct;              // One entry, recording the percentage of static region
688     uint8_t     global_rate_ratio_threshold[7]; // 80,95,99,101,105,125,160
689     uint8_t     current_frame_type;             // I frame: 2; P frame: 0; B frame: 1.
690     uint8_t     start_global_adjust_mult[5];    // 1, 1, 3, 2, 1
691     uint8_t     start_global_adjust_div[5];     // 40, 5, 5, 3, 1
692     uint8_t     global_rate_ratio_threshold_qp[8];      // 253,254,255,0,1,1,2,3
693     uint8_t     current_pak_pass;               // current pak pass number
694     uint8_t     max_num_passes;                 // 2
695     uint8_t     scene_change_width[2];          // set both to MIN((NumP + 1) / 5, 6)
696     uint8_t     scene_change_detect_enable;                     // Enable scene change detection
697     uint8_t     scene_change_prev_intra_percent_threshold;      // =96. scene change previous intra percentage threshold
698     uint8_t     scene_change_cur_intra_perent_threshold;        // =192. scene change current intra percentage threshold
699     uint8_t     ip_average_coeff;               // lowdelay ? 0 : 128
700     uint8_t     min_qp_adjustment;              // Minimum QP increase step
701     uint8_t     timing_budget_check;            // Flag indicating if kernel will check timing budget.
702     int8_t      roi_qp_delta_i8[4];             // Application specified ROI QP Adjustment for Zone0, Zone1, Zone2 and Zone3.
703     uint8_t     cqp_qp_value;                   // Application specified target QP in BRC_ICQ mode
704     uint8_t     cqp_fqp;                        // Application specified fine position in BRC_ICQ mode
705     uint8_t     hme_detection_enable;           // 0: default, 1: HuC BRC kernel requires information from HME detection kernel output
706     uint8_t     hme_cost_enable;                // 0: default, 1: driver provides HME cost table
707     uint8_t     disable_pframe_8x8_transform;
708     uint8_t     skl_cabac_wa_enable;
709     uint8_t     roi_source;                     // =0: disable, 1: ROIMap from HME Static Region or from App dirty rectangle, 2: ROIMap from App
710     uint8_t     slice_size_consertative_threshold;      // =0, 0: do not set conservative threshold (suggested for video conference) 1: set conservative threshold for non-video conference
711     uint16_t    max_target_slice_size;          // default: 1498, max target slice size from app DDI
712     uint16_t    max_num_slice_allowed;          // computed by driver based on level idc
713     uint16_t    second_level_batchbuffer_size;  // second level batch buffer (SLBB) size in bytes, the input buffer will contain two SLBBs A and B, A followed by B, A and B have the same structure.
714     uint16_t    second_level_batchbuffer_b_offset;      // offset in bytes from the beginning of the input buffer, it points to the start of SLBB B, set by driver for skip frame support
715     uint16_t    avc_img_state_offset;           // offset in bytes from the beginning of SLBB A
716
717     /* HME distortion based QP adjustment */
718     uint16_t    ave_hme_dist;
719     uint8_t     hme_dist_available;             // 0: disabled, 1: enabled
720
721     uint8_t     pad1[63];
722 };
723
724 struct gen9_vdenc_status
725 {
726     uint32_t    bytes_per_frame;
727 };
728
729 struct gen9_vdenc_context
730 {
731     uint32_t    frame_width_in_mbs;
732     uint32_t    frame_height_in_mbs;
733     uint32_t    frame_width;                    // frame_width_in_mbs * 16
734     uint32_t    frame_height;                   // frame_height_in_mbs * 16
735     uint32_t    down_scaled_width_in_mb4x;
736     uint32_t    down_scaled_height_in_mb4x;
737     uint32_t    down_scaled_width_4x;           // down_scaled_width_in_mb4x * 16
738     uint32_t    down_scaled_height_4x;          // down_scaled_height_in_mbs * 16
739
740     uint32_t    target_bit_rate;        /* in kbps */
741     uint32_t    max_bit_rate;           /* in kbps */
742     uint32_t    min_bit_rate;           /* in kbps */
743     uint64_t    init_vbv_buffer_fullness_in_bit;
744     uint64_t    vbv_buffer_size_in_bit;
745     struct intel_fraction framerate;
746     uint32_t    gop_size;
747     uint32_t    ref_dist;
748     uint32_t    brc_target_size;
749     double      brc_init_current_target_buf_full_in_bits;
750     double      brc_init_reset_input_bits_per_frame;
751     uint32_t    brc_init_reset_buf_size_in_bits;
752     uint32_t    brc_init_previous_target_buf_full_in_bits;
753
754     uint8_t     mode_cost[12];
755     uint8_t     mv_cost[8];
756     uint8_t     hme_mv_cost[8];
757
758     uint32_t    num_roi;
759     uint32_t    max_delta_qp;
760     uint32_t    min_delta_qp;
761     struct intel_roi roi[3];
762
763     uint32_t    brc_initted:1;
764     uint32_t    brc_need_reset:1;
765     uint32_t    is_low_delay:1;
766     uint32_t    brc_enabled:1;
767     uint32_t    internal_rate_mode:4;
768     uint32_t    current_pass:4;
769     uint32_t    num_passes:4;
770     uint32_t    is_first_pass:1;
771     uint32_t    is_last_pass:1;
772
773     uint32_t    vdenc_streamin_enable:1;
774     uint32_t    vdenc_pak_threshold_check_enable:1;
775     uint32_t    pad1:1;
776     uint32_t    transform_8x8_mode_enable:1;
777     uint32_t    frame_type:2;
778
779     uint32_t    mb_brc_enabled:1;
780     uint32_t    is_frame_level_vdenc:1;
781     uint32_t    use_extended_pak_obj_cmd:1;
782     uint32_t    pad0:29;
783
784     struct i965_gpe_resource brc_init_reset_dmem_res;
785     struct i965_gpe_resource brc_history_buffer_res;
786     struct i965_gpe_resource brc_stream_in_res;
787     struct i965_gpe_resource brc_stream_out_res;
788     struct i965_gpe_resource huc_dummy_res;
789
790     struct i965_gpe_resource brc_update_dmem_res[NUM_OF_BRC_PAK_PASSES];
791     struct i965_gpe_resource vdenc_statistics_res;
792     struct i965_gpe_resource pak_statistics_res;
793     struct i965_gpe_resource vdenc_avc_image_state_res;
794     struct i965_gpe_resource hme_detection_summary_buffer_res;
795     struct i965_gpe_resource brc_constant_data_res;
796     struct i965_gpe_resource second_level_batch_res;
797
798     struct i965_gpe_resource huc_status_res;
799     struct i965_gpe_resource huc_status2_res;
800
801     struct i965_gpe_resource recon_surface_res;
802     struct i965_gpe_resource scaled_4x_recon_surface_res;
803     struct i965_gpe_resource post_deblocking_output_res;
804     struct i965_gpe_resource pre_deblocking_output_res;
805     struct i965_gpe_resource list_reference_res[16];
806     struct i965_gpe_resource list_scaled_4x_reference_res[16];
807     struct i965_gpe_resource uncompressed_input_surface_res;                    // Input
808
809     struct {
810         struct i965_gpe_resource res;                                           // Output
811         uint32_t start_offset;
812         uint32_t end_offset;
813     } compressed_bitstream;
814
815     struct i965_gpe_resource mfx_intra_row_store_scratch_res;                   // MFX internal buffer
816     struct i965_gpe_resource mfx_deblocking_filter_row_store_scratch_res;       // MFX internal buffer
817     struct i965_gpe_resource mfx_bsd_mpc_row_store_scratch_res;                 // MFX internal buffer
818     struct i965_gpe_resource vdenc_row_store_scratch_res;                       // VDENC internal buffer
819
820     struct i965_gpe_resource vdenc_streamin_res;
821
822     uint32_t    num_refs[2];
823     uint32_t    list_ref_idx[2][32];
824
825     struct {
826         struct i965_gpe_resource res;
827         uint32_t base_offset;
828         uint32_t size;
829         uint32_t bytes_per_frame_offset;
830     } status_bffuer;
831 };
832
833 struct huc_pipe_mode_select_parameter
834 {
835     uint32_t    huc_stream_object_enable;
836     uint32_t    indirect_stream_out_enable;
837     uint32_t    media_soft_reset_counter;
838 };
839
840 struct huc_imem_state_parameter
841 {
842     uint32_t    huc_firmware_descriptor;
843 };
844
845 struct huc_dmem_state_parameter
846 {
847     struct i965_gpe_resource *huc_data_source_res;
848     uint32_t    huc_data_destination_base_address;
849     uint32_t    huc_data_length;
850 };
851
852 struct huc_cfg_state_parameter
853 {
854     uint32_t    force_reset;
855 };
856
857
858 struct huc_virtual_addr_parameter
859 {
860     struct {
861         struct i965_gpe_resource *huc_surface_res;
862         uint32_t is_target;
863     } regions[16];
864 };
865
866 struct huc_ind_obj_base_addr_parameter
867 {
868     struct i965_gpe_resource *huc_indirect_stream_in_object_res;
869     struct i965_gpe_resource *huc_indirect_stream_out_object_res;
870 };
871
872 struct huc_stream_object_parameter
873 {
874     uint32_t indirect_stream_in_data_length;
875     uint32_t indirect_stream_in_start_address;
876     uint32_t indirect_stream_out_start_address;
877     uint32_t huc_bitstream_enable;
878     uint32_t length_mode;
879     uint32_t stream_out;
880     uint32_t emulation_prevention_byte_removal;
881     uint32_t start_code_search_engine;
882     uint8_t start_code_byte2;
883     uint8_t start_code_byte1;
884     uint8_t start_code_byte0;
885 };
886
887 struct huc_start_parameter
888 {
889     uint32_t last_stream_object;
890 };
891
892 struct vd_pipeline_flush_parameter
893 {
894     uint32_t hevc_pipeline_done;
895     uint32_t vdenc_pipeline_done;
896     uint32_t mfl_pipeline_done;
897     uint32_t mfx_pipeline_done;
898     uint32_t vd_command_message_parser_done;
899     uint32_t hevc_pipeline_command_flush;
900     uint32_t vdenc_pipeline_command_flush;
901     uint32_t mfl_pipeline_command_flush;
902     uint32_t mfx_pipeline_command_flush;
903 };
904
905 extern Bool
906 gen9_vdenc_context_init(VADriverContextP ctx, struct intel_encoder_context *encoder_context);
907
908 #endif  /* GEN9_VDENC_H */