2 * Copyright © 2010 Intel Corporation
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:
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
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.
25 * Zhou Chang <chang.zhou@intel.com>
34 #include <intel_bufmgr.h>
36 #include "i965_encoder.h"
37 #include "i965_gpe_utils.h"
41 #define MAX_MFC_REFERENCE_SURFACES 16
42 #define NUM_MFC_DMV_BUFFERS 34
44 #define INTRA_MB_FLAG_MASK 0x00002000
46 /* The space required for slice header SLICE_STATE + header.
48 #define SLICE_HEADER 80
50 /* the space required for slice tail. */
54 #define MFC_BATCHBUFFER_AVC_INTRA 0
55 #define MFC_BATCHBUFFER_AVC_INTER 1
56 #define NUM_MFC_KERNEL 2
58 #define BIND_IDX_VME_OUTPUT 0
59 #define BIND_IDX_MFC_SLICE_HEADER 1
60 #define BIND_IDX_MFC_BATCHBUFFER 2
62 #define CMD_LEN_IN_OWORD 4
64 #define BRC_CLIP(x, min, max) \
66 x = ((x > (max)) ? (max) : ((x < (min)) ? (min) : x)); \
69 #define BRC_P_B_QP_DIFF 4
70 #define BRC_I_P_QP_DIFF 2
71 #define BRC_I_B_QP_DIFF (BRC_I_P_QP_DIFF + BRC_P_B_QP_DIFF)
73 #define BRC_PWEIGHT 0.6 /* weight if P slice with comparison to I slice */
74 #define BRC_BWEIGHT 0.25 /* weight if B slice with comparison to I slice */
76 #define BRC_QP_MAX_CHANGE 5 /* maximum qp modification */
77 #define BRC_CY 0.1 /* weight for */
78 #define BRC_CX_UNDERFLOW 5.
79 #define BRC_CX_OVERFLOW -4.
81 #define BRC_PI_0_5 1.5707963267948966192313216916398
89 VME_MB_INTRA_MODE_COUNT
90 } VME_MB_INTRA_PRED_MODE;
98 PAK_MB_INTRA_MODE_COUNT
99 } VP8_PAK_MB_INTRA_PRED_MODE;
113 VME_B_INTRA_MODE_COUNT
114 } VME_BLOCK_INTRA_PRED_MODE;
129 PAK_B_INTRA_MODE_COUNT
130 } VP8_PAK_BLOCK_INTRA_PRED_MODE;
134 int vme_intra_mb_mode;
135 int vp8_pak_intra_mb_mode;
136 } vp8_intra_mb_mode_map_t;
140 int vme_intra_block_mode;
141 int vp8_pak_intra_block_mode;
142 } vp8_intra_block_mode_map_t;
144 typedef enum _gen6_brc_status
146 BRC_NO_HRD_VIOLATION = 0,
149 BRC_UNDERFLOW_WITH_MAX_QP = 3,
150 BRC_OVERFLOW_WITH_MIN_QP = 4,
153 struct gen6_mfc_avc_surface_aux
159 struct gen6_mfc_context
164 unsigned int w_pitch;
165 unsigned int h_pitch;
168 //MFX_PIPE_BUF_ADDR_STATE
171 } post_deblocking_output; //OUTPUT: reconstructed picture
175 } pre_deblocking_output; //OUTPUT: reconstructed picture with deblocked
179 } uncompressed_picture_source; //INPUT: original compressed image
183 } intra_row_store_scratch_buffer; //INTERNAL:
187 } macroblock_status_buffer; //INTERNAL:
191 } deblocking_filter_row_store_scratch_buffer; //INTERNAL:
195 } reference_surfaces[MAX_MFC_REFERENCE_SURFACES]; //INTERNAL: refrence surfaces
197 //MFX_IND_OBJ_BASE_ADDR_STATE
200 } mfc_indirect_mv_object; //INPUT: the blocks' mv info
206 } mfc_indirect_pak_bse_object; //OUTPUT: the compressed bitstream
208 //MFX_BSP_BUF_BASE_ADDR_STATE
211 } bsd_mpc_row_store_scratch_buffer; //INTERNAL:
213 //MFX_AVC_DIRECTMODE_STATE
216 } direct_mv_buffers[NUM_MFC_DMV_BUFFERS]; //INTERNAL: 0-31 as input,32 and 33 as output
218 //Bit rate tracking context
220 unsigned int MaxQpNegModifier;
221 unsigned int MaxQpPosModifier;
222 unsigned char Correct[6];
223 unsigned char GrowInit;
224 unsigned char GrowResistance;
225 unsigned char ShrinkInit;
226 unsigned char ShrinkResistance;
227 } bit_rate_control_context[3]; //INTERNAL: for I, P, B frames
231 int gop_nums[MAX_MFC_REFERENCE_SURFACES][3];
232 int target_frame_size[MAX_TEMPORAL_LAYERS][3]; // I,P,B
233 int qp_prime_y[MAX_TEMPORAL_LAYERS][3];
234 double bits_per_frame[MAX_TEMPORAL_LAYERS];
235 double qpf_rounding_accumulator[MAX_TEMPORAL_LAYERS];
236 int bits_prev_frame[MAX_TEMPORAL_LAYERS];
237 int prev_slice_type[MAX_TEMPORAL_LAYERS];
241 double current_buffer_fullness[MAX_TEMPORAL_LAYERS];
242 double target_buffer_fullness[MAX_TEMPORAL_LAYERS];
243 double buffer_capacity[MAX_TEMPORAL_LAYERS];
244 unsigned int buffer_size[MAX_TEMPORAL_LAYERS];
245 unsigned int violation_noted;
248 //HRD control context
250 int i_bit_rate_value;
252 int i_initial_cpb_removal_delay;
253 int i_cpb_removal_delay;
257 int i_initial_cpb_removal_delay_length;
258 int i_cpb_removal_delay_length;
259 int i_dpb_output_delay_length;
263 unsigned char *vp8_frame_header;
264 unsigned int frame_header_bit_count;
265 unsigned int frame_header_qindex_update_pos;
266 unsigned int frame_header_lf_update_pos;
267 unsigned int frame_header_token_update_pos;
268 unsigned int frame_header_bin_mv_upate_pos;
270 unsigned int intermediate_partition_offset[8];
271 unsigned int intermediate_buffer_max_size;
272 unsigned int final_frame_byte_offset;
274 unsigned char mb_segment_tree_probs[3];
275 unsigned char y_mode_probs[4];
276 unsigned char uv_mode_probs[3];
277 unsigned char mv_probs[2][19];
279 unsigned char prob_skip_false;
280 unsigned char prob_intra;
281 unsigned char prob_last;
282 unsigned char prob_gf;
284 dri_bo *frame_header_bo;
285 dri_bo *intermediate_bo;
286 dri_bo *final_frame_bo;
287 dri_bo *stream_out_bo;
288 dri_bo *coeff_probs_stream_in_bo;
289 dri_bo *token_statistics_bo;
290 dri_bo *mpc_row_store_bo;
293 //"buffered_QMatrix" will be used to buffer the QMatrix if the app sends one.
294 // Or else, we will load a default QMatrix from the driver for JPEG encode.
295 VAQMatrixBufferJPEG buffered_qmatrix;
296 struct i965_gpe_context gpe_context;
297 struct i965_buffer_surface mfc_batchbuffer_surface;
298 struct intel_batchbuffer *aux_batchbuffer;
299 struct i965_buffer_surface aux_batchbuffer_surface;
301 void (*pipe_mode_select)(VADriverContextP ctx,
303 struct intel_encoder_context *encoder_context);
304 void (*set_surface_state)(VADriverContextP ctx,
305 struct intel_encoder_context *encoder_context);
306 void (*ind_obj_base_addr_state)(VADriverContextP ctx,
307 struct intel_encoder_context *encoder_context);
308 void (*avc_img_state)(VADriverContextP ctx,
309 struct encode_state *encode_state,
310 struct intel_encoder_context *encoder_context);
311 void (*avc_qm_state)(VADriverContextP ctx,
312 struct encode_state *encode_state,
313 struct intel_encoder_context *encoder_context);
314 void (*avc_fqm_state)(VADriverContextP ctx,
315 struct encode_state *encode_state,
316 struct intel_encoder_context *encoder_context);
317 void (*insert_object)(VADriverContextP ctx,
318 struct intel_encoder_context *encoder_context,
319 unsigned int *insert_data,
320 int lenght_in_dws, int data_bits_in_last_dw,
321 int skip_emul_byte_count,
322 int is_last_header, int is_end_of_slice,
324 struct intel_batchbuffer *batch);
325 void (*buffer_suface_setup)(VADriverContextP ctx,
326 struct i965_gpe_context *gpe_context,
327 struct i965_buffer_surface *buffer_surface,
328 unsigned long binding_table_offset,
329 unsigned long surface_state_offset);
332 VAStatus gen6_mfc_pipeline(VADriverContextP ctx,
334 struct encode_state *encode_state,
335 struct intel_encoder_context *encoder_context);
336 void gen6_mfc_context_destroy(void *context);
339 Bool gen6_mfc_context_init(VADriverContextP ctx, struct intel_encoder_context *encoder_context);
342 Bool gen7_mfc_context_init(VADriverContextP ctx, struct intel_encoder_context *encoder_context);
345 Bool gen75_mfc_context_init(VADriverContextP ctx, struct intel_encoder_context *encoder_context);
348 extern int intel_mfc_update_hrd(struct encode_state *encode_state,
349 struct intel_encoder_context *encoder_context,
352 extern int intel_mfc_brc_postpack(struct encode_state *encode_state,
353 struct intel_encoder_context *encoder_context,
356 extern void intel_mfc_hrd_context_update(struct encode_state *encode_state,
357 struct gen6_mfc_context *mfc_context);
359 extern int intel_mfc_interlace_check(VADriverContextP ctx,
360 struct encode_state *encode_state,
361 struct intel_encoder_context *encoder_context);
363 extern void intel_mfc_brc_prepare(struct encode_state *encode_state,
364 struct intel_encoder_context *encoder_context);
366 extern void intel_mfc_avc_pipeline_header_programing(VADriverContextP ctx,
367 struct encode_state *encode_state,
368 struct intel_encoder_context *encoder_context,
369 struct intel_batchbuffer *slice_batch);
371 extern VAStatus intel_mfc_avc_prepare(VADriverContextP ctx,
372 struct encode_state *encode_state,
373 struct intel_encoder_context *encoder_context);
375 extern int intel_avc_enc_slice_type_fixup(int type);
378 intel_mfc_avc_ref_idx_state(VADriverContextP ctx,
379 struct encode_state *encode_state,
380 struct intel_encoder_context *encoder_context);
383 Bool gen8_mfc_context_init(VADriverContextP ctx, struct intel_encoder_context *encoder_context);
386 intel_avc_slice_insert_packed_data(VADriverContextP ctx,
387 struct encode_state *encode_state,
388 struct intel_encoder_context *encoder_context,
390 struct intel_batchbuffer *slice_batch);
393 Bool gen9_mfc_context_init(VADriverContextP ctx, struct intel_encoder_context *encoder_context);
395 #endif /* _GEN6_MFC_BCS_H_ */