1 /* 2 * Copyright (c) 2011 Intel Corporation. All Rights Reserved. 3 * Copyright (c) Imagination Technologies Limited, UK 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining a 6 * copy of this software and associated documentation files (the 7 * "Software"), to deal in the Software without restriction, including 8 * without limitation the rights to use, copy, modify, merge, publish, 9 * distribute, sub license, and/or sell copies of the Software, and to 10 * permit persons to whom the Software is furnished to do so, subject to 11 * the following conditions: 12 * 13 * The above copyright notice and this permission notice (including the 14 * next paragraph) shall be included in all copies or substantial portions 15 * of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 20 * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR 21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 * 25 * Authors: 26 * Zeng Li <zeng.li@intel.com> 27 * Shengquan Yuan <shengquan.yuan@intel.com> 28 * Binglin Chen <binglin.chen@intel.com> 29 * 30 */ 31 32 33 #include "img_types.h" 34 #include "psb_drv_video.h" 35 #include "psb_surface.h" 36 #include "lnc_cmdbuf.h" 37 38 #define MAX_SLICES_PER_PICTURE 72 39 40 #define CODED_BUFFER_EOSEQ_FLAG (0x1u<<0) 41 #define CODED_BUFFER_EOSTREAM_FLAG (0x1u<<1) 42 43 /* commands for topaz,shared with user space driver */ 44 enum drm_lnc_topaz_cmd { 45 MTX_CMDID_NULL = 0, 46 MTX_CMDID_DO_HEADER = 1, 47 MTX_CMDID_ENCODE_SLICE = 2, 48 MTX_CMDID_WRITEREG = 3, 49 MTX_CMDID_START_PIC = 4, 50 MTX_CMDID_END_PIC = 5, 51 MTX_CMDID_SYNC = 6, 52 MTX_CMDID_ENCODE_ONE_ROW = 7, 53 MTX_CMDID_FLUSH = 8, 54 MTX_CMDID_SW_LEAVE_LOWPOWER = 0x7c, 55 MTX_CMDID_SW_ENTER_LOWPOWER = 0x7e, 56 MTX_CMDID_SW_NEW_CODEC = 0x7f 57 }; 58 59 /* codecs topaz supports,shared with user space driver */ 60 enum drm_lnc_topaz_codec { 61 IMG_CODEC_JPEG = 0, 62 IMG_CODEC_H264_NO_RC, 63 IMG_CODEC_H264_VBR, 64 IMG_CODEC_H264_CBR, 65 IMG_CODEC_H264_VCM, 66 IMG_CODEC_H263_NO_RC, 67 IMG_CODEC_H263_VBR, 68 IMG_CODEC_H263_CBR, 69 IMG_CODEC_MPEG4_NO_RC, 70 IMG_CODEC_MPEG4_VBR, 71 IMG_CODEC_MPEG4_CBR, 72 IMG_CODEC_NUM 73 }; 74 75 typedef enum _img_format_ { 76 IMG_CODEC_IYUV, /* IYUV */ 77 IMG_CODEC_IMC2, /* IMC2 */ 78 IMG_CODEC_PL8, 79 IMG_CODEC_PL12, 80 IMG_CODEC_NV12, 81 } IMG_FORMAT; 82 83 84 typedef struct _RC_PARAMS_ { 85 IMG_UINT32 BitsPerSecond; 86 IMG_UINT32 InitialQp; 87 IMG_UINT32 BUSize; 88 IMG_UINT32 FrameRate; 89 IMG_UINT32 BufferSize; 90 IMG_UINT32 BitsConsumed; 91 IMG_UINT32 IntraFreq; 92 IMG_UINT16 IDRFreq; 93 94 IMG_UINT16 MinQP; 95 IMG_BOOL RCEnable; 96 IMG_BOOL FrameSkip; 97 98 IMG_UINT8 Slices; 99 IMG_UINT8 VCMBitrateMargin; 100 IMG_UINT32 InitialLevel; 101 IMG_UINT32 InitialDelay; 102 } IMG_RC_PARAMS; 103 104 /*! 105 ***************************************************************************** 106 * 107 * @Description Struct describing rate control input parameters 108 * 109 * @Brief Rate control input parameters 110 * 111 ****************************************************************************/ 112 typedef struct { 113 IMG_UINT8 SeInitQP; //!< Initial QP for Sequence 114 IMG_UINT8 MinQPVal; //!< Minimum QP value to use 115 IMG_UINT8 MaxQPVal; //!< Maximum QP value to use 116 117 IMG_UINT8 MBPerRow; /* Number of MBs Per Row */ 118 IMG_UINT16 MBPerFrm; /* Number of MBs Per Frame */ 119 IMG_UINT16 MBPerBU; /* Number of MBs Per BU */ 120 IMG_UINT16 BUPerFrm; /* Number of BUs Per Frame */ 121 122 IMG_UINT16 IntraPeriod; /* Intra frame frequency */ 123 124 IMG_INT32 BitsPerFrm; /* Bits Per Frame */ 125 IMG_INT32 BitsPerBU; /* Bits Per BU */ 126 IMG_INT32 BitsPerMB; /* Bits Per MB */ 127 128 IMG_INT32 BitRate; /* Bit Rate (bps) */ 129 IMG_INT32 BufferSize; /* Size of Buffer */ 130 IMG_UINT32 InitialLevel; /* Initial Level of Buffer */ 131 IMG_INT32 InitialDelay; /* Initial Delay of Buffer */ 132 133 IMG_UINT8 ScaleFactor; /* Scale Factor (H264 only) */ 134 IMG_UINT8 VCMBitrateMargin; /* Bitrate that should be 135 targetted as a fraction of 136 128 relative to maximum bitrate 137 i32BitRate (VCM mode only) */ 138 IMG_UINT8 HalfFrameRate; /* Half Frame Rate (MP4 only) */ 139 IMG_UINT8 FCode; /* F Code (MP4 only) */ 140 141 /* TO BE DELETED -- ONCE MP4 RC CODE IS OPTIMISED */ 142 IMG_INT32 BitsPerGOP; /* Bits Per GOP (MP4 only) */ 143 IMG_UINT16 AvQPVal; /* Average QP in Current Picture */ 144 IMG_UINT16 MyInitQP; /* Initial Quantizer */ 145 146 IMG_INT32 ForceSkipMargin; /* The number of bits of margin 147 to leave before forcing skipped 148 macroblocks (VCM mode only) */ 149 IMG_UINT32 RCScaleFactor; /* A constant used in rate control = (GopSize/(BufferSize-InitialLevel))*256 */ 150 } IN_RC_PARAMS; 151 152 struct coded_buf_aux_info { 153 object_buffer_p buf; 154 uint32_t aux_flag; /*Indicate which operation should be applied when map coded buffer.*/ 155 struct coded_buf_aux_info *next; 156 }; 157 158 struct context_ENC_s { 159 object_context_p obj_context; /* back reference */ 160 161 IMG_UINT32 Width; 162 IMG_UINT32 Height; 163 IMG_UINT16 RawWidth; 164 IMG_UINT16 RawHeight; 165 IMG_UINT16 Slices; 166 enum drm_lnc_topaz_codec eCodec; 167 IMG_FORMAT eFormat; 168 unsigned int FCode; 169 IMG_RC_PARAMS sRCParams; 170 171 IMG_INT16 HeightMinus16MinusLRBTopOffset; 172 IMG_INT16 HeightMinus32MinusLRBTopOffset; 173 IMG_INT16 HeightMinusLRB_TopAndBottom_OffsetsPlus16; 174 IMG_INT16 HeightMinusLRBSearchHeight; 175 IMG_UINT32 IPEControl; 176 177 object_surface_p src_surface; 178 object_surface_p ref_surface; 179 object_surface_p dest_surface;/* reconstructed surface */ 180 object_buffer_p coded_buf; 181 182 /* save previous settings */ 183 object_surface_p previous_src_surface; 184 object_surface_p previous_ref_surface; 185 object_surface_p previous_dest_surface; /* reconstructed surface */ 186 object_buffer_p previous_coded_buf; 187 object_buffer_p pprevious_coded_buf; 188 189 /* point to the place in cmdbuf following START_PIC, the initial_qp will fill into it later */ 190 uint32_t *initial_qp_in_cmdbuf; 191 192 193 /* global topaz_params buffer shared by every cmdbuffer 194 * it is because filling InParams for every MB is very time-consuming 195 * and in most cases, we can reuse previous frames buffer 196 */ 197 /* 0 and 1 are for in_parms, 2 is for bellow and above params*/ 198 struct psb_buffer_s topaz_in_params_I; 199 struct psb_buffer_s topaz_in_params_P; 200 struct psb_buffer_s topaz_above_bellow_params; 201 uint32_t topaz_buffer_size; 202 uint32_t in_params_size; 203 uint32_t bellow_params_size; 204 uint32_t above_params_size; 205 206 /* offset in topaz_param buffer */ 207 uint32_t in_params_ofs; 208 uint32_t bellow_params_ofs; 209 uint32_t above_params_ofs; 210 211 uint32_t pic_params_size; 212 213 uint32_t header_buffer_size; 214 215 uint32_t seq_header_ofs; 216 uint32_t pic_header_ofs; 217 uint32_t eoseq_header_ofs; 218 uint32_t eostream_header_ofs; 219 uint32_t slice_header_ofs; 220 221 uint32_t sliceparam_buffer_size; 222 223 IN_RC_PARAMS in_params_cache; /* following frames reuse the first frame's IN_RC_PARAMS, cache it */ 224 225 VAEncSliceParameterBuffer *slice_param_cache; 226 uint16_t slice_param_num; 227 228 IMG_UINT16 MPEG4_vop_time_increment_resolution; 229 230 /* saved information for FrameSkip redo */ 231 uint32_t MPEG4_vop_time_increment_frameskip; 232 uint32_t MPEG4_picture_type_frameskip; 233 uint8_t profile_idc; 234 uint8_t force_idr_h264; 235 uint32_t update_rc_control; 236 uint8_t OptionalCustomPCF; 237 uint32_t max_slice_size; 238 uint16_t num_air_mbs; 239 uint16_t air_threshold; 240 uint32_t autotune_air_flag; 241 uint32_t delta_change; 242 243 struct coded_buf_aux_info *p_coded_buf_info; 244 unsigned char *save_seq_header_p; 245 246 /*For H264 only.*/ 247 uint16_t idr_pic_id; 248 }; 249 250 typedef struct context_ENC_s *context_ENC_p; 251 252 /*#define BELOW_PARAMS_SIZE 8*/ 253 254 #define HEADER_SIZE 128 255 256 #define BELOW_PARAMS_SIZE 16 257 #define REGION_TYPE_2D 1 258 #define REGION_TYPE_LINEAR 0 259 #define REGION_TYPE_2DREF 3 260 261 262 #define MAX_RESIDUAL_PER_MB_H264 1260 263 #define ISINTER_FLAGS 0x1 264 #define ISH264_FLAGS 0x2 265 #define ISMPEG4_FLAGS 0x4 266 #define ISH263_FLAGS 0x8 267 #define DEBLOCK_FRAME 0x10 268 #define ISRC_FLAGS 0x20 269 #define ISCBR_FLAGS 0x40 270 #define ISVBR_FLAGS 0x80 271 #define ISRC_I16BIAS 0x100 272 #define INTERLEAVE_TARGET 0x200 273 #define ISVCM_FLAGS 0x400 274 #define AUTOTUNE_AIR 0x800 275 276 277 #define SPE_EDGE_LEFT 1 /* ->bMinXRealEdge*/ 278 #define SPE_EDGE_RIGHT 2 /* ->bMaxXRealEdge*/ 279 #define SPE_EDGE_TOP 4 /* ->bMinYRealEdge*/ 280 #define SPE_EDGE_BOTTOM 8 /* ->bMaxYRealEdge*/ 281 282 typedef struct { 283 /* Transferred into the input params area of the macroblock parameter structure*/ 284 IMG_BYTE CurBlockAddr; 285 IMG_BYTE IPEMin[2]; 286 IMG_BYTE IPEMax[2]; 287 IMG_BYTE RealEdge; /*bMinXRealEdge, bMaxXRealEdge, bMinXRealEdge and bMinYRealEdge*/ 288 /* Surrounding block availability */ 289 IMG_BYTE MVValid; 290 IMG_BYTE ParamsValid; 291 IMG_BYTE bySliceQP; 292 IMG_BYTE bySliceQPC; 293 294 IMG_BYTE Reserved[6]; /* This is padding to make the transfers 16 bytes aligned*/ 295 /* Transferred into the SW communication section of the macroblock 296 * parameter structure We shall EDMA the whole lot of this into eiob 297 * in one go, and then use two TDMA's to put it into seperate locations 298 * within the macroblock structure 299 */ 300 IMG_UINT32 IPEControl; 301 IMG_UINT32 SPEControl; 302 IMG_UINT32 JMCompControl; 303 IMG_UINT32 VLCControl; 304 } MTX_CURRENT_IN_PARAMS; 305 306 typedef struct { /* corresponding bytes inside the MB_IN structure: */ 307 IMG_BYTE BlockSizes; /****************/ 308 IMG_BYTE IntraMode; /* */ 309 IMG_BYTE Intra4x4ModesBottom[2]; /* */ 310 IMG_BYTE CodeType; /* [64 : 71] */ 311 IMG_BYTE Reserved2; /* */ 312 /*IMG_BYTE SAD;*/ 313 IMG_BYTE QPy; /* */ 314 IMG_BYTE QPc; /****************/ 315 316 IMG_BYTE Reserved3[8]; /* This is padding to make the transfers 16 byte aligned*/ 317 318 IMG_UINT16 LumaSubBlockCoded; /****************/ 319 IMG_BYTE ChromaSubBlockCoded; /* */ 320 IMG_BYTE LumaChromaDCCoded; /* */ 321 /* [129 : 143] */ 322 IMG_BYTE Lambda; /* */ 323 IMG_BYTE Reserved[3]; /* */ 324 /* */ 325 IMG_BYTE Intra4x4ModeDeltas[8]; /****************/ 326 327 /* Motion vectors */ 328 IMG_UINT16 IntegerMV[16][2]; /* [207 : 144] */ 329 /* input region from host */ 330 } MTX_CURRENT_OUT_PARAMS; 331 332 333 334 typedef enum _TH_SKIP_SCALE_ { 335 TH_SKIP_0 = 0, 336 TH_SKIP_12 = 1, 337 TH_SKIP_24 = 2 338 } TH_SKIP_SCALE; 339 340 typedef struct _PIC_PARAMS_ { 341 IMG_UINT32 SrcYBase; 342 IMG_UINT32 SrcUBase; 343 IMG_UINT32 SrcVBase; 344 IMG_UINT32 DstYBase; 345 IMG_UINT32 DstUVBase; 346 347 IMG_UINT16 SrcYStride; 348 IMG_UINT16 SrcUVStride; 349 IMG_UINT16 SrcYRowStride; 350 IMG_UINT16 SrcUVRowStride; 351 352 IMG_UINT16 DstYStride; 353 IMG_UINT16 DstUVStride; 354 IMG_UINT16 DstYRowStride; 355 IMG_UINT16 DstUVRowStride; 356 357 IMG_UINT32 InParamsBase; 358 IMG_UINT32 InParamsRowStride; 359 360 IMG_UINT32 OutParamsBase; 361 IMG_UINT32 CodedBase; 362 363 IMG_UINT32 BelowParamsBase; 364 IMG_UINT32 BelowParamRowStride; 365 366 IMG_UINT32 AboveParamsBase; 367 IMG_UINT32 AboveParamRowStride; 368 IMG_UINT16 Width; 369 IMG_UINT16 Height; 370 IMG_UINT16 Flags; 371 372 IN_RC_PARAMS sInParams; 373 TH_SKIP_SCALE THSkip; 374 375 IMG_UINT16 NumSlices; //!< Number of slices in the picture 376 } PIC_PARAMS; 377 378 379 /* This holds the data that is needed at the start of a slice 380 */ 381 typedef struct _SLICE_PARAMS_ { 382 383 IMG_UINT16 SliceStartRowNum; 384 IMG_UINT16 SliceHeight; 385 386 IMG_UINT32 RefYBase; 387 IMG_UINT32 RefUVBase; 388 IMG_UINT16 RefYStride; 389 IMG_UINT16 RefUVStride; 390 IMG_UINT16 RefYRowStride; 391 IMG_UINT16 RefUVRowStride; 392 393 IMG_UINT32 CodedData; 394 IMG_UINT32 Flags; 395 IMG_UINT32 CodedDataPos; 396 IMG_UINT32 TotalCoded; 397 IMG_UINT32 FCode; 398 399 IMG_UINT32 MaxSliceSize; 400 IMG_INT16 NumAirMBs; //!< Maximum number of Adaptive intra refresh macroblocks for this slice 401 IMG_INT16 AirThreshold; //!< Theshold value used in Adaptive intra refresh calculation. 402 403 } SLICE_PARAMS; 404 405 406 typedef struct _ROW_PARAMS_ { 407 IMG_UINT32 TargetYBase; 408 IMG_UINT32 TargetYStride; 409 IMG_UINT32 TargetUBase; 410 IMG_UINT32 TargetVBase; 411 IMG_UINT32 TargetUVStride; 412 413 IMG_UINT32 ReferenceYBase; 414 IMG_UINT32 ReferenceYStride; 415 IMG_UINT32 ReferenceUVBase; 416 IMG_UINT32 ReferenceUVStride; 417 418 IMG_UINT32 ReconstructedYBase; 419 IMG_UINT32 ReconstructedYStride; 420 IMG_UINT32 ReconstructedUVBase; 421 IMG_UINT32 ReconstructedUVStride; 422 423 IMG_UINT32 AboveParamsBase; 424 IMG_UINT32 OutAboveParamsBase; 425 IMG_UINT32 MacroblockInParamsBase; 426 IMG_UINT32 MacroblockOutParamsBase; 427 IMG_UINT32 BelowParamsBase; 428 IMG_UINT32 OutBelowParamsBase; 429 IMG_UINT32 CodedData; 430 431 IMG_UINT32 Flags; 432 IMG_UINT32 BlockWidth; 433 IMG_UINT32 BlockHeight; 434 IMG_UINT32 YPos; 435 IMG_UINT32 FrameNum; 436 437 IMG_INT BelowParamsOffset; 438 IMG_UINT32 CodedDataPos; 439 IMG_UINT32 BaseResidual; 440 IMG_UINT32 TotalCoded; 441 442 IMG_UINT32 PADDING[5]; 443 444 IMG_UINT32 IPESkipVecBias; 445 IMG_UINT32 SPESkipVecBias; 446 IMG_INT32 InterMBBias; 447 IMG_INT32 Intra16Bias; 448 IMG_UINT32 SpeZeroThld; 449 IMG_UINT32 SpeZeroThreshold; 450 451 } ROW_PARAMS; 452 453 #define ROW_PARAMS_TDMA_DIMENSIONS 16,16,sizeof(ROW_PARAMS) 454 455 typedef struct _ENCODER_VARIABLES_ { 456 IMG_UINT32 ActionFlags; 457 458 IMG_UINT32 SrcYCurrent; 459 IMG_UINT32 SrcUCurrent; 460 IMG_UINT32 SrcVCurrent; 461 462 IMG_UINT32 DstYCurrent; 463 IMG_UINT32 DstUCurrent; 464 IMG_UINT32 DstVCurrent; 465 466 IMG_INT BelowParamsOffset; 467 IMG_UINT32 BaseResidual; 468 IMG_UINT32 CodedDataPos; 469 IMG_UINT32 TotalCoded; 470 471 IMG_UINT32 SrcYOffset; 472 IMG_UINT32 SrcUOffset; 473 IMG_UINT32 SrcVOffset; 474 475 IMG_UINT32 PADDING[2]; 476 } ENCODER_VARIABLES; 477 478 #define SLICE_FLAGS_ISINTER 0x00000001 479 #define SLICE_FLAGS_DEBLOCK 0x00000002 480 481 #define SLICE_FLAGS_ISINTER 0x00000001 482 #define SLICE_FLAGS_DEBLOCK 0x00000002 483 484 #define RC_STATUS_FRAME_AVE_QP_MASK 0x0ff /* the average Qp used in this frame */ 485 #define RC_STATUS_FLAG_LARGE_SLICE 0x100 /* At least one slice in this frame was large enough for the firmware to try to reduce it by increasing Qp or skipping MBs */ 486 #define RC_STATUS_FLAG_SLICE_OVERFLOW 0x200 /* At least one slice in this frame was larger than the slice limit */ 487 488 enum { 489 CBR = 0, 490 VBR, 491 VCM 492 } eRCMode; 493 494 enum { 495 EH263 = 0, 496 EMpeg4 = 1, 497 EH264 = 2, 498 EHJpeg = 3 499 } eEncodingFormat; 500 501 #define VAEncSliceParameter_Equal(src, dst) \ 502 (((src)->start_row_number == (dst)->start_row_number) \ 503 && ((src)->slice_height == (dst)->slice_height) \ 504 && ((src)->slice_flags.bits.is_intra == (dst)->slice_flags.bits.is_intra) \ 505 && ((src)->slice_flags.bits.disable_deblocking_filter_idc == (dst)->slice_flags.bits.disable_deblocking_filter_idc)) 506 507 #define VAEncSliceParameter_LightEqual(src, dst) \ 508 (((src)->start_row_number == (dst)->start_row_number) \ 509 && ((src)->slice_height == (dst)->slice_height) \ 510 && ((src)->slice_flags.bits.disable_deblocking_filter_idc == (dst)->slice_flags.bits.disable_deblocking_filter_idc)) 511 512 513 514 #define SURFACE_INFO_SKIP_FLAG_SETTLED 0X80000000 515 #define GET_SURFACE_INFO_skipped_flag(psb_surface) ((int) (psb_surface->extra_info[5])) 516 #define SET_SURFACE_INFO_skipped_flag(psb_surface, value) psb_surface->extra_info[5] = (SURFACE_INFO_SKIP_FLAG_SETTLED | value) 517 #define CLEAR_SURFACE_INFO_skipped_flag(psb_surface) psb_surface->extra_info[5] = 0 518 519 VAStatus lnc_CreateContext(object_context_p obj_context, 520 object_config_p obj_config); 521 522 523 void lnc__setup_rcdata(context_ENC_p ctx, PIC_PARAMS *psPicParams, IMG_RC_PARAMS *rc_params); 524 void lnc__update_rcdata(context_ENC_p ctx, PIC_PARAMS *psPicParams, IMG_RC_PARAMS *rc_params); 525 526 void lnc_DestroyContext( 527 object_context_p obj_context 528 ); 529 530 VAStatus lnc_BeginPicture(context_ENC_p ctx); 531 VAStatus lnc_EndPicture(context_ENC_p ctx); 532 533 void lnc_setup_slice_params( 534 context_ENC_p ctx, IMG_UINT16 YSliceStartPos, 535 IMG_UINT16 SliceHeight, IMG_BOOL IsIntra, 536 IMG_BOOL VectorsValid, int bySliceQP); 537 538 IMG_UINT32 lnc__send_encode_slice_params( 539 context_ENC_p ctx, 540 IMG_BOOL IsIntra, 541 IMG_UINT16 CurrentRow, 542 IMG_BOOL DeblockSlice, 543 IMG_UINT32 FrameNum, 544 IMG_UINT16 SliceHeight, 545 IMG_UINT16 CurrentSlice, 546 IMG_UINT32 MaxSliceSize); 547 548 VAStatus lnc_RenderPictureParameter(context_ENC_p ctx); 549 550 551 void lnc_reset_encoder_params(context_ENC_p ctx); 552 unsigned int lnc__get_ipe_control(enum drm_lnc_topaz_codec eEncodingFormat); 553 554 555 556 557 558