Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright © 2010 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. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <assert.h>
  33.  
  34. #include "intel_batchbuffer.h"
  35. #include "intel_driver.h"
  36. #include "i965_defines.h"
  37. #include "i965_structs.h"
  38. #include "i965_drv_video.h"
  39. #include "i965_post_processing.h"
  40. #include "i965_render.h"
  41. #include "i965_yuv_coefs.h"
  42. #include "intel_media.h"
  43.  
  44. extern VAStatus
  45. vpp_surface_convert(VADriverContextP ctx,
  46.                     struct object_surface *src_obj_surf,
  47.                     struct object_surface *dst_obj_surf);
  48.  
  49. #define HAS_VPP(ctx) ((ctx)->codec_info->has_vpp)
  50.  
  51. #define SURFACE_STATE_PADDED_SIZE               MAX(SURFACE_STATE_PADDED_SIZE_GEN8,\
  52.                         MAX(SURFACE_STATE_PADDED_SIZE_GEN6, SURFACE_STATE_PADDED_SIZE_GEN7))
  53.  
  54. #define SURFACE_STATE_OFFSET(index)             (SURFACE_STATE_PADDED_SIZE * index)
  55. #define BINDING_TABLE_OFFSET                    SURFACE_STATE_OFFSET(MAX_PP_SURFACES)
  56.  
  57. #define GPU_ASM_BLOCK_WIDTH         16
  58. #define GPU_ASM_BLOCK_HEIGHT        8
  59. #define GPU_ASM_X_OFFSET_ALIGNMENT  4
  60.  
  61. #define VA_STATUS_SUCCESS_1                     0xFFFFFFFE
  62.  
  63. static const uint32_t pp_null_gen5[][4] = {
  64. #include "shaders/post_processing/gen5_6/null.g4b.gen5"
  65. };
  66.  
  67. static const uint32_t pp_nv12_load_save_nv12_gen5[][4] = {
  68. #include "shaders/post_processing/gen5_6/nv12_load_save_nv12.g4b.gen5"
  69. };
  70.  
  71. static const uint32_t pp_nv12_load_save_pl3_gen5[][4] = {
  72. #include "shaders/post_processing/gen5_6/nv12_load_save_pl3.g4b.gen5"
  73. };
  74.  
  75. static const uint32_t pp_pl3_load_save_nv12_gen5[][4] = {
  76. #include "shaders/post_processing/gen5_6/pl3_load_save_nv12.g4b.gen5"
  77. };
  78.  
  79. static const uint32_t pp_pl3_load_save_pl3_gen5[][4] = {
  80. #include "shaders/post_processing/gen5_6/pl3_load_save_pl3.g4b.gen5"
  81. };
  82.  
  83. static const uint32_t pp_nv12_scaling_gen5[][4] = {
  84. #include "shaders/post_processing/gen5_6/nv12_scaling_nv12.g4b.gen5"
  85. };
  86.  
  87. static const uint32_t pp_nv12_avs_gen5[][4] = {
  88. #include "shaders/post_processing/gen5_6/nv12_avs_nv12.g4b.gen5"
  89. };
  90.  
  91. static const uint32_t pp_nv12_dndi_gen5[][4] = {
  92. #include "shaders/post_processing/gen5_6/nv12_dndi_nv12.g4b.gen5"
  93. };
  94.  
  95. static const uint32_t pp_nv12_dn_gen5[][4] = {
  96. #include "shaders/post_processing/gen5_6/nv12_dn_nv12.g4b.gen5"
  97. };
  98.  
  99. static const uint32_t pp_nv12_load_save_pa_gen5[][4] = {
  100. #include "shaders/post_processing/gen5_6/nv12_load_save_pa.g4b.gen5"
  101. };
  102.  
  103. static const uint32_t pp_pl3_load_save_pa_gen5[][4] = {
  104. #include "shaders/post_processing/gen5_6/pl3_load_save_pa.g4b.gen5"
  105. };
  106.  
  107. static const uint32_t pp_pa_load_save_nv12_gen5[][4] = {
  108. #include "shaders/post_processing/gen5_6/pa_load_save_nv12.g4b.gen5"
  109. };
  110.  
  111. static const uint32_t pp_pa_load_save_pl3_gen5[][4] = {
  112. #include "shaders/post_processing/gen5_6/pa_load_save_pl3.g4b.gen5"
  113. };
  114.  
  115. static const uint32_t pp_pa_load_save_pa_gen5[][4] = {
  116. #include "shaders/post_processing/gen5_6/pa_load_save_pa.g4b.gen5"
  117. };
  118.  
  119. static const uint32_t pp_rgbx_load_save_nv12_gen5[][4] = {
  120. #include "shaders/post_processing/gen5_6/rgbx_load_save_nv12.g4b.gen5"
  121. };
  122.  
  123. static const uint32_t pp_nv12_load_save_rgbx_gen5[][4] = {
  124. #include "shaders/post_processing/gen5_6/nv12_load_save_rgbx.g4b.gen5"
  125. };
  126.  
  127. static VAStatus pp_null_initialize(VADriverContextP ctx, struct i965_post_processing_context *pp_context,
  128.                                    const struct i965_surface *src_surface,
  129.                                    const VARectangle *src_rect,
  130.                                    struct i965_surface *dst_surface,
  131.                                    const VARectangle *dst_rect,
  132.                                    void *filter_param);
  133. static VAStatus
  134. pp_nv12_avs_initialize(VADriverContextP ctx,
  135.     struct i965_post_processing_context *pp_context,
  136.     const struct i965_surface *src_surface, const VARectangle *src_rect,
  137.     struct i965_surface *dst_surface, const VARectangle *dst_rect,
  138.     void *filter_param);
  139. static VAStatus pp_nv12_scaling_initialize(VADriverContextP ctx, struct i965_post_processing_context *pp_context,
  140.                                            const struct i965_surface *src_surface,
  141.                                            const VARectangle *src_rect,
  142.                                            struct i965_surface *dst_surface,
  143.                                            const VARectangle *dst_rect,
  144.                                            void *filter_param);
  145. static VAStatus gen6_nv12_scaling_initialize(VADriverContextP ctx, struct i965_post_processing_context *pp_context,
  146.                                              const struct i965_surface *src_surface,
  147.                                              const VARectangle *src_rect,
  148.                                              struct i965_surface *dst_surface,
  149.                                              const VARectangle *dst_rect,
  150.                                              void *filter_param);
  151. static VAStatus pp_plx_load_save_plx_initialize(VADriverContextP ctx, struct i965_post_processing_context *pp_context,
  152.                                                 const struct i965_surface *src_surface,
  153.                                                 const VARectangle *src_rect,
  154.                                                 struct i965_surface *dst_surface,
  155.                                                 const VARectangle *dst_rect,
  156.                                                 void *filter_param);
  157. static VAStatus pp_nv12_dndi_initialize(VADriverContextP ctx, struct i965_post_processing_context *pp_context,
  158.                                         const struct i965_surface *src_surface,
  159.                                         const VARectangle *src_rect,
  160.                                         struct i965_surface *dst_surface,
  161.                                         const VARectangle *dst_rect,
  162.                                         void *filter_param);
  163. static VAStatus pp_nv12_dn_initialize(VADriverContextP ctx, struct i965_post_processing_context *pp_context,
  164.                                       const struct i965_surface *src_surface,
  165.                                       const VARectangle *src_rect,
  166.                                       struct i965_surface *dst_surface,
  167.                                       const VARectangle *dst_rect,
  168.                                       void *filter_param);
  169.  
  170. static struct pp_module pp_modules_gen5[] = {
  171.     {
  172.         {
  173.             "NULL module (for testing)",
  174.             PP_NULL,
  175.             pp_null_gen5,
  176.             sizeof(pp_null_gen5),
  177.             NULL,
  178.         },
  179.  
  180.         pp_null_initialize,
  181.     },
  182.  
  183.     {
  184.         {
  185.             "NV12_NV12",
  186.             PP_NV12_LOAD_SAVE_N12,
  187.             pp_nv12_load_save_nv12_gen5,
  188.             sizeof(pp_nv12_load_save_nv12_gen5),
  189.             NULL,
  190.         },
  191.  
  192.         pp_plx_load_save_plx_initialize,
  193.     },
  194.  
  195.     {
  196.         {
  197.             "NV12_PL3",
  198.             PP_NV12_LOAD_SAVE_PL3,
  199.             pp_nv12_load_save_pl3_gen5,
  200.             sizeof(pp_nv12_load_save_pl3_gen5),
  201.             NULL,
  202.         },
  203.  
  204.         pp_plx_load_save_plx_initialize,
  205.     },
  206.  
  207.     {
  208.         {
  209.             "PL3_NV12",
  210.             PP_PL3_LOAD_SAVE_N12,
  211.             pp_pl3_load_save_nv12_gen5,
  212.             sizeof(pp_pl3_load_save_nv12_gen5),
  213.             NULL,
  214.         },
  215.  
  216.         pp_plx_load_save_plx_initialize,
  217.     },
  218.  
  219.     {
  220.         {
  221.             "PL3_PL3",
  222.             PP_PL3_LOAD_SAVE_PL3,
  223.             pp_pl3_load_save_pl3_gen5,
  224.             sizeof(pp_pl3_load_save_pl3_gen5),
  225.             NULL,
  226.         },
  227.  
  228.         pp_plx_load_save_plx_initialize
  229.     },
  230.  
  231.     {
  232.         {
  233.             "NV12 Scaling module",
  234.             PP_NV12_SCALING,
  235.             pp_nv12_scaling_gen5,
  236.             sizeof(pp_nv12_scaling_gen5),
  237.             NULL,
  238.         },
  239.  
  240.         pp_nv12_scaling_initialize,
  241.     },
  242.  
  243.     {
  244.         {
  245.             "NV12 AVS module",
  246.             PP_NV12_AVS,
  247.             pp_nv12_avs_gen5,
  248.             sizeof(pp_nv12_avs_gen5),
  249.             NULL,
  250.         },
  251.  
  252.         pp_nv12_avs_initialize,
  253.     },
  254.  
  255.     {
  256.         {
  257.             "NV12 DNDI module",
  258.             PP_NV12_DNDI,
  259.             pp_nv12_dndi_gen5,
  260.             sizeof(pp_nv12_dndi_gen5),
  261.             NULL,
  262.         },
  263.  
  264.         pp_nv12_dndi_initialize,
  265.     },
  266.  
  267.     {
  268.         {
  269.             "NV12 DN module",
  270.             PP_NV12_DN,
  271.             pp_nv12_dn_gen5,
  272.             sizeof(pp_nv12_dn_gen5),
  273.             NULL,
  274.         },
  275.  
  276.         pp_nv12_dn_initialize,
  277.     },
  278.  
  279.     {
  280.         {
  281.             "NV12_PA module",
  282.             PP_NV12_LOAD_SAVE_PA,
  283.             pp_nv12_load_save_pa_gen5,
  284.             sizeof(pp_nv12_load_save_pa_gen5),
  285.             NULL,
  286.         },
  287.    
  288.         pp_plx_load_save_plx_initialize,
  289.     },
  290.  
  291.     {
  292.         {
  293.             "PL3_PA module",
  294.             PP_PL3_LOAD_SAVE_PA,
  295.             pp_pl3_load_save_pa_gen5,
  296.             sizeof(pp_pl3_load_save_pa_gen5),
  297.             NULL,
  298.         },
  299.    
  300.         pp_plx_load_save_plx_initialize,
  301.     },
  302.  
  303.     {
  304.         {
  305.             "PA_NV12 module",
  306.             PP_PA_LOAD_SAVE_NV12,
  307.             pp_pa_load_save_nv12_gen5,
  308.             sizeof(pp_pa_load_save_nv12_gen5),
  309.             NULL,
  310.         },
  311.    
  312.         pp_plx_load_save_plx_initialize,
  313.     },
  314.  
  315.     {
  316.         {
  317.             "PA_PL3 module",
  318.             PP_PA_LOAD_SAVE_PL3,
  319.             pp_pa_load_save_pl3_gen5,
  320.             sizeof(pp_pa_load_save_pl3_gen5),
  321.             NULL,
  322.         },
  323.    
  324.         pp_plx_load_save_plx_initialize,
  325.     },
  326.  
  327.     {
  328.         {
  329.             "PA_PA module",
  330.             PP_PA_LOAD_SAVE_PA,
  331.             pp_pa_load_save_pa_gen5,
  332.             sizeof(pp_pa_load_save_pa_gen5),
  333.             NULL,
  334.         },
  335.  
  336.         pp_plx_load_save_plx_initialize,
  337.     },
  338.  
  339.     {
  340.         {
  341.             "RGBX_NV12 module",
  342.             PP_RGBX_LOAD_SAVE_NV12,
  343.             pp_rgbx_load_save_nv12_gen5,
  344.             sizeof(pp_rgbx_load_save_nv12_gen5),
  345.             NULL,
  346.         },
  347.    
  348.         pp_plx_load_save_plx_initialize,
  349.     },
  350.            
  351.     {
  352.         {
  353.             "NV12_RGBX module",
  354.             PP_NV12_LOAD_SAVE_RGBX,
  355.             pp_nv12_load_save_rgbx_gen5,
  356.             sizeof(pp_nv12_load_save_rgbx_gen5),
  357.             NULL,
  358.         },
  359.    
  360.         pp_plx_load_save_plx_initialize,
  361.     },
  362. };
  363.  
  364. static const uint32_t pp_null_gen6[][4] = {
  365. #include "shaders/post_processing/gen5_6/null.g6b"
  366. };
  367.  
  368. static const uint32_t pp_nv12_load_save_nv12_gen6[][4] = {
  369. #include "shaders/post_processing/gen5_6/nv12_load_save_nv12.g6b"
  370. };
  371.  
  372. static const uint32_t pp_nv12_load_save_pl3_gen6[][4] = {
  373. #include "shaders/post_processing/gen5_6/nv12_load_save_pl3.g6b"
  374. };
  375.  
  376. static const uint32_t pp_pl3_load_save_nv12_gen6[][4] = {
  377. #include "shaders/post_processing/gen5_6/pl3_load_save_nv12.g6b"
  378. };
  379.  
  380. static const uint32_t pp_pl3_load_save_pl3_gen6[][4] = {
  381. #include "shaders/post_processing/gen5_6/pl3_load_save_pl3.g6b"
  382. };
  383.  
  384. static const uint32_t pp_nv12_scaling_gen6[][4] = {
  385. #include "shaders/post_processing/gen5_6/nv12_avs_nv12.g6b"
  386. };
  387.  
  388. static const uint32_t pp_nv12_avs_gen6[][4] = {
  389. #include "shaders/post_processing/gen5_6/nv12_avs_nv12.g6b"
  390. };
  391.  
  392. static const uint32_t pp_nv12_dndi_gen6[][4] = {
  393. #include "shaders/post_processing/gen5_6/nv12_dndi_nv12.g6b"
  394. };
  395.  
  396. static const uint32_t pp_nv12_dn_gen6[][4] = {
  397. #include "shaders/post_processing/gen5_6/nv12_dn_nv12.g6b"
  398. };
  399.  
  400. static const uint32_t pp_nv12_load_save_pa_gen6[][4] = {
  401. #include "shaders/post_processing/gen5_6/nv12_load_save_pa.g6b"
  402. };
  403.  
  404. static const uint32_t pp_pl3_load_save_pa_gen6[][4] = {
  405. #include "shaders/post_processing/gen5_6/pl3_load_save_pa.g6b"
  406. };
  407.  
  408. static const uint32_t pp_pa_load_save_nv12_gen6[][4] = {
  409. #include "shaders/post_processing/gen5_6/pa_load_save_nv12.g6b"
  410. };
  411.  
  412. static const uint32_t pp_pa_load_save_pl3_gen6[][4] = {
  413. #include "shaders/post_processing/gen5_6/pa_load_save_pl3.g6b"
  414. };
  415.  
  416. static const uint32_t pp_pa_load_save_pa_gen6[][4] = {
  417. #include "shaders/post_processing/gen5_6/pa_load_save_pa.g6b"
  418. };
  419.  
  420. static const uint32_t pp_rgbx_load_save_nv12_gen6[][4] = {
  421. #include "shaders/post_processing/gen5_6/rgbx_load_save_nv12.g6b"
  422. };
  423.  
  424. static const uint32_t pp_nv12_load_save_rgbx_gen6[][4] = {
  425. #include "shaders/post_processing/gen5_6/nv12_load_save_rgbx.g6b"
  426. };
  427.  
  428. static struct pp_module pp_modules_gen6[] = {
  429.     {
  430.         {
  431.             "NULL module (for testing)",
  432.             PP_NULL,
  433.             pp_null_gen6,
  434.             sizeof(pp_null_gen6),
  435.             NULL,
  436.         },
  437.  
  438.         pp_null_initialize,
  439.     },
  440.  
  441.     {
  442.         {
  443.             "NV12_NV12",
  444.             PP_NV12_LOAD_SAVE_N12,
  445.             pp_nv12_load_save_nv12_gen6,
  446.             sizeof(pp_nv12_load_save_nv12_gen6),
  447.             NULL,
  448.         },
  449.  
  450.         pp_plx_load_save_plx_initialize,
  451.     },
  452.  
  453.     {
  454.         {
  455.             "NV12_PL3",
  456.             PP_NV12_LOAD_SAVE_PL3,
  457.             pp_nv12_load_save_pl3_gen6,
  458.             sizeof(pp_nv12_load_save_pl3_gen6),
  459.             NULL,
  460.         },
  461.        
  462.         pp_plx_load_save_plx_initialize,
  463.     },
  464.  
  465.     {
  466.         {
  467.             "PL3_NV12",
  468.             PP_PL3_LOAD_SAVE_N12,
  469.             pp_pl3_load_save_nv12_gen6,
  470.             sizeof(pp_pl3_load_save_nv12_gen6),
  471.             NULL,
  472.         },
  473.  
  474.         pp_plx_load_save_plx_initialize,
  475.     },
  476.  
  477.     {
  478.         {
  479.             "PL3_PL3",
  480.             PP_PL3_LOAD_SAVE_PL3,
  481.             pp_pl3_load_save_pl3_gen6,
  482.             sizeof(pp_pl3_load_save_pl3_gen6),
  483.             NULL,
  484.         },
  485.  
  486.         pp_plx_load_save_plx_initialize,
  487.     },
  488.  
  489.     {
  490.         {
  491.             "NV12 Scaling module",
  492.             PP_NV12_SCALING,
  493.             pp_nv12_scaling_gen6,
  494.             sizeof(pp_nv12_scaling_gen6),
  495.             NULL,
  496.         },
  497.  
  498.         gen6_nv12_scaling_initialize,
  499.     },
  500.  
  501.     {
  502.         {
  503.             "NV12 AVS module",
  504.             PP_NV12_AVS,
  505.             pp_nv12_avs_gen6,
  506.             sizeof(pp_nv12_avs_gen6),
  507.             NULL,
  508.         },
  509.  
  510.         pp_nv12_avs_initialize,
  511.     },
  512.  
  513.     {
  514.         {
  515.             "NV12 DNDI module",
  516.             PP_NV12_DNDI,
  517.             pp_nv12_dndi_gen6,
  518.             sizeof(pp_nv12_dndi_gen6),
  519.             NULL,
  520.         },
  521.  
  522.         pp_nv12_dndi_initialize,
  523.     },
  524.  
  525.     {
  526.         {
  527.             "NV12 DN module",
  528.             PP_NV12_DN,
  529.             pp_nv12_dn_gen6,
  530.             sizeof(pp_nv12_dn_gen6),
  531.             NULL,
  532.         },
  533.  
  534.         pp_nv12_dn_initialize,
  535.     },
  536.     {
  537.         {
  538.             "NV12_PA module",
  539.             PP_NV12_LOAD_SAVE_PA,
  540.             pp_nv12_load_save_pa_gen6,
  541.             sizeof(pp_nv12_load_save_pa_gen6),
  542.             NULL,
  543.         },
  544.    
  545.         pp_plx_load_save_plx_initialize,
  546.     },
  547.  
  548.     {
  549.         {
  550.             "PL3_PA module",
  551.             PP_PL3_LOAD_SAVE_PA,
  552.             pp_pl3_load_save_pa_gen6,
  553.             sizeof(pp_pl3_load_save_pa_gen6),
  554.             NULL,
  555.         },
  556.    
  557.         pp_plx_load_save_plx_initialize,
  558.     },
  559.  
  560.     {
  561.         {
  562.             "PA_NV12 module",
  563.             PP_PA_LOAD_SAVE_NV12,
  564.             pp_pa_load_save_nv12_gen6,
  565.             sizeof(pp_pa_load_save_nv12_gen6),
  566.             NULL,
  567.         },
  568.    
  569.         pp_plx_load_save_plx_initialize,
  570.     },
  571.  
  572.     {
  573.         {
  574.             "PA_PL3 module",
  575.             PP_PA_LOAD_SAVE_PL3,
  576.             pp_pa_load_save_pl3_gen6,
  577.             sizeof(pp_pa_load_save_pl3_gen6),
  578.             NULL,
  579.         },
  580.    
  581.         pp_plx_load_save_plx_initialize,
  582.     },
  583.  
  584.     {
  585.         {
  586.             "PA_PA module",
  587.             PP_PA_LOAD_SAVE_PA,
  588.             pp_pa_load_save_pa_gen6,
  589.             sizeof(pp_pa_load_save_pa_gen6),
  590.             NULL,
  591.         },
  592.  
  593.         pp_plx_load_save_plx_initialize,
  594.     },
  595.  
  596.     {
  597.         {
  598.             "RGBX_NV12 module",
  599.             PP_RGBX_LOAD_SAVE_NV12,
  600.             pp_rgbx_load_save_nv12_gen6,
  601.             sizeof(pp_rgbx_load_save_nv12_gen6),
  602.             NULL,
  603.         },
  604.    
  605.         pp_plx_load_save_plx_initialize,
  606.     },
  607.  
  608.     {
  609.         {
  610.             "NV12_RGBX module",
  611.             PP_NV12_LOAD_SAVE_RGBX,
  612.             pp_nv12_load_save_rgbx_gen6,
  613.             sizeof(pp_nv12_load_save_rgbx_gen6),
  614.             NULL,
  615.         },
  616.    
  617.         pp_plx_load_save_plx_initialize,
  618.     },
  619. };
  620.  
  621. static const uint32_t pp_null_gen7[][4] = {
  622. };
  623.  
  624. static const uint32_t pp_nv12_load_save_nv12_gen7[][4] = {
  625. #include "shaders/post_processing/gen7/pl2_to_pl2.g7b"
  626. };
  627.  
  628. static const uint32_t pp_nv12_load_save_pl3_gen7[][4] = {
  629. #include "shaders/post_processing/gen7/pl2_to_pl3.g7b"
  630. };
  631.  
  632. static const uint32_t pp_pl3_load_save_nv12_gen7[][4] = {
  633. #include "shaders/post_processing/gen7/pl3_to_pl2.g7b"
  634. };
  635.  
  636. static const uint32_t pp_pl3_load_save_pl3_gen7[][4] = {
  637. #include "shaders/post_processing/gen7/pl3_to_pl3.g7b"
  638. };
  639.  
  640. static const uint32_t pp_nv12_scaling_gen7[][4] = {
  641. #include "shaders/post_processing/gen7/avs.g7b"
  642. };
  643.  
  644. static const uint32_t pp_nv12_avs_gen7[][4] = {
  645. #include "shaders/post_processing/gen7/avs.g7b"
  646. };
  647.  
  648. static const uint32_t pp_nv12_dndi_gen7[][4] = {
  649. #include "shaders/post_processing/gen7/dndi.g7b"
  650. };
  651.  
  652. static const uint32_t pp_nv12_dn_gen7[][4] = {
  653. #include "shaders/post_processing/gen7/nv12_dn_nv12.g7b"
  654. };
  655. static const uint32_t pp_nv12_load_save_pa_gen7[][4] = {
  656. #include "shaders/post_processing/gen7/pl2_to_pa.g7b"
  657. };
  658. static const uint32_t pp_pl3_load_save_pa_gen7[][4] = {
  659. #include "shaders/post_processing/gen7/pl3_to_pa.g7b"
  660. };
  661. static const uint32_t pp_pa_load_save_nv12_gen7[][4] = {
  662. #include "shaders/post_processing/gen7/pa_to_pl2.g7b"
  663. };
  664. static const uint32_t pp_pa_load_save_pl3_gen7[][4] = {
  665. #include "shaders/post_processing/gen7/pa_to_pl3.g7b"
  666. };
  667. static const uint32_t pp_pa_load_save_pa_gen7[][4] = {
  668. #include "shaders/post_processing/gen7/pa_to_pa.g7b"
  669. };
  670. static const uint32_t pp_rgbx_load_save_nv12_gen7[][4] = {
  671. #include "shaders/post_processing/gen7/rgbx_to_nv12.g7b"
  672. };
  673. static const uint32_t pp_nv12_load_save_rgbx_gen7[][4] = {
  674. #include "shaders/post_processing/gen7/pl2_to_rgbx.g7b"
  675. };
  676.  
  677. static VAStatus gen7_pp_plx_avs_initialize(VADriverContextP ctx, struct i965_post_processing_context *pp_context,
  678.                                            const struct i965_surface *src_surface,
  679.                                            const VARectangle *src_rect,
  680.                                            struct i965_surface *dst_surface,
  681.                                            const VARectangle *dst_rect,
  682.                                            void *filter_param);
  683. static VAStatus gen7_pp_nv12_dndi_initialize(VADriverContextP ctx, struct i965_post_processing_context *pp_context,
  684.                                              const struct i965_surface *src_surface,
  685.                                              const VARectangle *src_rect,
  686.                                              struct i965_surface *dst_surface,
  687.                                              const VARectangle *dst_rect,
  688.                                              void *filter_param);
  689. static VAStatus gen7_pp_nv12_dn_initialize(VADriverContextP ctx, struct i965_post_processing_context *pp_context,
  690.                                            const struct i965_surface *src_surface,
  691.                                            const VARectangle *src_rect,
  692.                                            struct i965_surface *dst_surface,
  693.                                            const VARectangle *dst_rect,
  694.                                            void *filter_param);
  695.  
  696. static struct pp_module pp_modules_gen7[] = {
  697.     {
  698.         {
  699.             "NULL module (for testing)",
  700.             PP_NULL,
  701.             pp_null_gen7,
  702.             sizeof(pp_null_gen7),
  703.             NULL,
  704.         },
  705.  
  706.         pp_null_initialize,
  707.     },
  708.  
  709.     {
  710.         {
  711.             "NV12_NV12",
  712.             PP_NV12_LOAD_SAVE_N12,
  713.             pp_nv12_load_save_nv12_gen7,
  714.             sizeof(pp_nv12_load_save_nv12_gen7),
  715.             NULL,
  716.         },
  717.  
  718.         gen7_pp_plx_avs_initialize,
  719.     },
  720.  
  721.     {
  722.         {
  723.             "NV12_PL3",
  724.             PP_NV12_LOAD_SAVE_PL3,
  725.             pp_nv12_load_save_pl3_gen7,
  726.             sizeof(pp_nv12_load_save_pl3_gen7),
  727.             NULL,
  728.         },
  729.        
  730.         gen7_pp_plx_avs_initialize,
  731.     },
  732.  
  733.     {
  734.         {
  735.             "PL3_NV12",
  736.             PP_PL3_LOAD_SAVE_N12,
  737.             pp_pl3_load_save_nv12_gen7,
  738.             sizeof(pp_pl3_load_save_nv12_gen7),
  739.             NULL,
  740.         },
  741.  
  742.         gen7_pp_plx_avs_initialize,
  743.     },
  744.  
  745.     {
  746.         {
  747.             "PL3_PL3",
  748.             PP_PL3_LOAD_SAVE_PL3,
  749.             pp_pl3_load_save_pl3_gen7,
  750.             sizeof(pp_pl3_load_save_pl3_gen7),
  751.             NULL,
  752.         },
  753.  
  754.         gen7_pp_plx_avs_initialize,
  755.     },
  756.  
  757.     {
  758.         {
  759.             "NV12 Scaling module",
  760.             PP_NV12_SCALING,
  761.             pp_nv12_scaling_gen7,
  762.             sizeof(pp_nv12_scaling_gen7),
  763.             NULL,
  764.         },
  765.  
  766.         gen7_pp_plx_avs_initialize,
  767.     },
  768.  
  769.     {
  770.         {
  771.             "NV12 AVS module",
  772.             PP_NV12_AVS,
  773.             pp_nv12_avs_gen7,
  774.             sizeof(pp_nv12_avs_gen7),
  775.             NULL,
  776.         },
  777.  
  778.         gen7_pp_plx_avs_initialize,
  779.     },
  780.  
  781.     {
  782.         {
  783.             "NV12 DNDI module",
  784.             PP_NV12_DNDI,
  785.             pp_nv12_dndi_gen7,
  786.             sizeof(pp_nv12_dndi_gen7),
  787.             NULL,
  788.         },
  789.  
  790.         gen7_pp_nv12_dndi_initialize,
  791.     },
  792.  
  793.     {
  794.         {
  795.             "NV12 DN module",
  796.             PP_NV12_DN,
  797.             pp_nv12_dn_gen7,
  798.             sizeof(pp_nv12_dn_gen7),
  799.             NULL,
  800.         },
  801.  
  802.         gen7_pp_nv12_dn_initialize,
  803.     },
  804.     {
  805.         {
  806.             "NV12_PA module",
  807.             PP_NV12_LOAD_SAVE_PA,
  808.             pp_nv12_load_save_pa_gen7,
  809.             sizeof(pp_nv12_load_save_pa_gen7),
  810.             NULL,
  811.         },
  812.    
  813.         gen7_pp_plx_avs_initialize,
  814.     },
  815.  
  816.     {
  817.         {
  818.             "PL3_PA module",
  819.             PP_PL3_LOAD_SAVE_PA,
  820.             pp_pl3_load_save_pa_gen7,
  821.             sizeof(pp_pl3_load_save_pa_gen7),
  822.             NULL,
  823.         },
  824.    
  825.         gen7_pp_plx_avs_initialize,
  826.     },
  827.  
  828.     {
  829.         {
  830.             "PA_NV12 module",
  831.             PP_PA_LOAD_SAVE_NV12,
  832.             pp_pa_load_save_nv12_gen7,
  833.             sizeof(pp_pa_load_save_nv12_gen7),
  834.             NULL,
  835.         },
  836.    
  837.         gen7_pp_plx_avs_initialize,
  838.     },
  839.  
  840.     {
  841.         {
  842.             "PA_PL3 module",
  843.             PP_PA_LOAD_SAVE_PL3,
  844.             pp_pa_load_save_pl3_gen7,
  845.             sizeof(pp_pa_load_save_pl3_gen7),
  846.             NULL,
  847.         },
  848.    
  849.         gen7_pp_plx_avs_initialize,
  850.     },
  851.  
  852.     {
  853.         {
  854.             "PA_PA module",
  855.             PP_PA_LOAD_SAVE_PA,
  856.             pp_pa_load_save_pa_gen7,
  857.             sizeof(pp_pa_load_save_pa_gen7),
  858.             NULL,
  859.         },
  860.  
  861.         gen7_pp_plx_avs_initialize,
  862.     },
  863.  
  864.     {
  865.         {
  866.             "RGBX_NV12 module",
  867.             PP_RGBX_LOAD_SAVE_NV12,
  868.             pp_rgbx_load_save_nv12_gen7,
  869.             sizeof(pp_rgbx_load_save_nv12_gen7),
  870.             NULL,
  871.         },
  872.    
  873.         gen7_pp_plx_avs_initialize,
  874.     },
  875.  
  876.     {
  877.         {
  878.             "NV12_RGBX module",
  879.             PP_NV12_LOAD_SAVE_RGBX,
  880.             pp_nv12_load_save_rgbx_gen7,
  881.             sizeof(pp_nv12_load_save_rgbx_gen7),
  882.             NULL,
  883.         },
  884.    
  885.         gen7_pp_plx_avs_initialize,
  886.     },
  887.            
  888. };
  889.  
  890. static const uint32_t pp_null_gen75[][4] = {
  891. };
  892.  
  893. static const uint32_t pp_nv12_load_save_nv12_gen75[][4] = {
  894. #include "shaders/post_processing/gen7/pl2_to_pl2.g75b"
  895. };
  896.  
  897. static const uint32_t pp_nv12_load_save_pl3_gen75[][4] = {
  898. #include "shaders/post_processing/gen7/pl2_to_pl3.g75b"
  899. };
  900.  
  901. static const uint32_t pp_pl3_load_save_nv12_gen75[][4] = {
  902. #include "shaders/post_processing/gen7/pl3_to_pl2.g75b"
  903. };
  904.  
  905. static const uint32_t pp_pl3_load_save_pl3_gen75[][4] = {
  906. #include "shaders/post_processing/gen7/pl3_to_pl3.g75b"
  907. };
  908.  
  909. static const uint32_t pp_nv12_scaling_gen75[][4] = {
  910. #include "shaders/post_processing/gen7/avs.g75b"
  911. };
  912.  
  913. static const uint32_t pp_nv12_avs_gen75[][4] = {
  914. #include "shaders/post_processing/gen7/avs.g75b"
  915. };
  916.  
  917. static const uint32_t pp_nv12_dndi_gen75[][4] = {
  918. // #include "shaders/post_processing/gen7/dndi.g75b"
  919. };
  920.  
  921. static const uint32_t pp_nv12_dn_gen75[][4] = {
  922. // #include "shaders/post_processing/gen7/nv12_dn_nv12.g75b"
  923. };
  924. static const uint32_t pp_nv12_load_save_pa_gen75[][4] = {
  925. #include "shaders/post_processing/gen7/pl2_to_pa.g75b"
  926. };
  927. static const uint32_t pp_pl3_load_save_pa_gen75[][4] = {
  928. #include "shaders/post_processing/gen7/pl3_to_pa.g75b"
  929. };
  930. static const uint32_t pp_pa_load_save_nv12_gen75[][4] = {
  931. #include "shaders/post_processing/gen7/pa_to_pl2.g75b"
  932. };
  933. static const uint32_t pp_pa_load_save_pl3_gen75[][4] = {
  934. #include "shaders/post_processing/gen7/pa_to_pl3.g75b"
  935. };
  936. static const uint32_t pp_pa_load_save_pa_gen75[][4] = {
  937. #include "shaders/post_processing/gen7/pa_to_pa.g75b"
  938. };
  939. static const uint32_t pp_rgbx_load_save_nv12_gen75[][4] = {
  940. #include "shaders/post_processing/gen7/rgbx_to_nv12.g75b"
  941. };
  942. static const uint32_t pp_nv12_load_save_rgbx_gen75[][4] = {
  943. #include "shaders/post_processing/gen7/pl2_to_rgbx.g75b"
  944. };
  945.  
  946. static struct pp_module pp_modules_gen75[] = {
  947.     {
  948.         {
  949.             "NULL module (for testing)",
  950.             PP_NULL,
  951.             pp_null_gen75,
  952.             sizeof(pp_null_gen75),
  953.             NULL,
  954.         },
  955.  
  956.         pp_null_initialize,
  957.     },
  958.  
  959.     {
  960.         {
  961.             "NV12_NV12",
  962.             PP_NV12_LOAD_SAVE_N12,
  963.             pp_nv12_load_save_nv12_gen75,
  964.             sizeof(pp_nv12_load_save_nv12_gen75),
  965.             NULL,
  966.         },
  967.  
  968.         gen7_pp_plx_avs_initialize,
  969.     },
  970.  
  971.     {
  972.         {
  973.             "NV12_PL3",
  974.             PP_NV12_LOAD_SAVE_PL3,
  975.             pp_nv12_load_save_pl3_gen75,
  976.             sizeof(pp_nv12_load_save_pl3_gen75),
  977.             NULL,
  978.         },
  979.        
  980.         gen7_pp_plx_avs_initialize,
  981.     },
  982.  
  983.     {
  984.         {
  985.             "PL3_NV12",
  986.             PP_PL3_LOAD_SAVE_N12,
  987.             pp_pl3_load_save_nv12_gen75,
  988.             sizeof(pp_pl3_load_save_nv12_gen75),
  989.             NULL,
  990.         },
  991.  
  992.         gen7_pp_plx_avs_initialize,
  993.     },
  994.  
  995.     {
  996.         {
  997.             "PL3_PL3",
  998.             PP_PL3_LOAD_SAVE_PL3,
  999.             pp_pl3_load_save_pl3_gen75,
  1000.             sizeof(pp_pl3_load_save_pl3_gen75),
  1001.             NULL,
  1002.         },
  1003.  
  1004.         gen7_pp_plx_avs_initialize,
  1005.     },
  1006.  
  1007.     {
  1008.         {
  1009.             "NV12 Scaling module",
  1010.             PP_NV12_SCALING,
  1011.             pp_nv12_scaling_gen75,
  1012.             sizeof(pp_nv12_scaling_gen75),
  1013.             NULL,
  1014.         },
  1015.  
  1016.         gen7_pp_plx_avs_initialize,
  1017.     },
  1018.  
  1019.     {
  1020.         {
  1021.             "NV12 AVS module",
  1022.             PP_NV12_AVS,
  1023.             pp_nv12_avs_gen75,
  1024.             sizeof(pp_nv12_avs_gen75),
  1025.             NULL,
  1026.         },
  1027.  
  1028.         gen7_pp_plx_avs_initialize,
  1029.     },
  1030.  
  1031.     {
  1032.         {
  1033.             "NV12 DNDI module",
  1034.             PP_NV12_DNDI,
  1035.             pp_nv12_dndi_gen75,
  1036.             sizeof(pp_nv12_dndi_gen75),
  1037.             NULL,
  1038.         },
  1039.  
  1040.         gen7_pp_nv12_dn_initialize,
  1041.     },
  1042.  
  1043.     {
  1044.         {
  1045.             "NV12 DN module",
  1046.             PP_NV12_DN,
  1047.             pp_nv12_dn_gen75,
  1048.             sizeof(pp_nv12_dn_gen75),
  1049.             NULL,
  1050.         },
  1051.  
  1052.         gen7_pp_nv12_dn_initialize,
  1053.     },
  1054.  
  1055.     {
  1056.         {
  1057.             "NV12_PA module",
  1058.             PP_NV12_LOAD_SAVE_PA,
  1059.             pp_nv12_load_save_pa_gen75,
  1060.             sizeof(pp_nv12_load_save_pa_gen75),
  1061.             NULL,
  1062.         },
  1063.    
  1064.         gen7_pp_plx_avs_initialize,
  1065.     },
  1066.  
  1067.     {
  1068.         {
  1069.             "PL3_PA module",
  1070.             PP_PL3_LOAD_SAVE_PA,
  1071.             pp_pl3_load_save_pa_gen75,
  1072.             sizeof(pp_pl3_load_save_pa_gen75),
  1073.             NULL,
  1074.         },
  1075.    
  1076.         gen7_pp_plx_avs_initialize,
  1077.     },
  1078.  
  1079.     {
  1080.         {
  1081.             "PA_NV12 module",
  1082.             PP_PA_LOAD_SAVE_NV12,
  1083.             pp_pa_load_save_nv12_gen75,
  1084.             sizeof(pp_pa_load_save_nv12_gen75),
  1085.             NULL,
  1086.         },
  1087.    
  1088.         gen7_pp_plx_avs_initialize,
  1089.     },
  1090.  
  1091.     {
  1092.         {
  1093.             "PA_PL3 module",
  1094.             PP_PA_LOAD_SAVE_PL3,
  1095.             pp_pa_load_save_pl3_gen75,
  1096.             sizeof(pp_pa_load_save_pl3_gen75),
  1097.             NULL,
  1098.         },
  1099.    
  1100.         gen7_pp_plx_avs_initialize,
  1101.     },
  1102.  
  1103.     {
  1104.         {
  1105.             "PA_PA module",
  1106.             PP_PA_LOAD_SAVE_PA,
  1107.             pp_pa_load_save_pa_gen75,
  1108.             sizeof(pp_pa_load_save_pa_gen75),
  1109.             NULL,
  1110.         },
  1111.  
  1112.         gen7_pp_plx_avs_initialize,
  1113.     },
  1114.  
  1115.     {
  1116.         {
  1117.             "RGBX_NV12 module",
  1118.             PP_RGBX_LOAD_SAVE_NV12,
  1119.             pp_rgbx_load_save_nv12_gen75,
  1120.             sizeof(pp_rgbx_load_save_nv12_gen75),
  1121.             NULL,
  1122.         },
  1123.    
  1124.         gen7_pp_plx_avs_initialize,
  1125.     },
  1126.  
  1127.     {
  1128.         {
  1129.             "NV12_RGBX module",
  1130.             PP_NV12_LOAD_SAVE_RGBX,
  1131.             pp_nv12_load_save_rgbx_gen75,
  1132.             sizeof(pp_nv12_load_save_rgbx_gen75),
  1133.             NULL,
  1134.         },
  1135.    
  1136.         gen7_pp_plx_avs_initialize,
  1137.     },
  1138.            
  1139. };
  1140.  
  1141. static void
  1142. pp_dndi_frame_store_reset(DNDIFrameStore *fs)
  1143. {
  1144.     fs->obj_surface = NULL;
  1145.     fs->surface_id = VA_INVALID_ID;
  1146.     fs->is_scratch_surface = 0;
  1147. }
  1148.  
  1149. static inline void
  1150. pp_dndi_frame_store_swap(DNDIFrameStore *fs1, DNDIFrameStore *fs2)
  1151. {
  1152.     const DNDIFrameStore tmpfs = *fs1;
  1153.     *fs1 = *fs2;
  1154.     *fs2 = tmpfs;
  1155. }
  1156.  
  1157. static inline void
  1158. pp_dndi_frame_store_clear(DNDIFrameStore *fs, VADriverContextP ctx)
  1159. {
  1160.     if (fs->obj_surface && fs->is_scratch_surface) {
  1161.         VASurfaceID va_surface = fs->obj_surface->base.id;
  1162.         i965_DestroySurfaces(ctx, &va_surface, 1);
  1163.     }
  1164.     pp_dndi_frame_store_reset(fs);
  1165. }
  1166.  
  1167. static void
  1168. pp_dndi_context_init(struct pp_dndi_context *dndi_ctx)
  1169. {
  1170.     int i;
  1171.  
  1172.     memset(dndi_ctx, 0, sizeof(*dndi_ctx));
  1173.     for (i = 0; i < ARRAY_ELEMS(dndi_ctx->frame_store); i++)
  1174.         pp_dndi_frame_store_reset(&dndi_ctx->frame_store[i]);
  1175. }
  1176.  
  1177. static VAStatus
  1178. pp_dndi_context_init_surface_params(struct pp_dndi_context *dndi_ctx,
  1179.     struct object_surface *obj_surface,
  1180.     const VAProcPipelineParameterBuffer *pipe_params,
  1181.     const VAProcFilterParameterBufferDeinterlacing *deint_params)
  1182. {
  1183.     DNDIFrameStore *fs;
  1184.  
  1185.     dndi_ctx->is_di_enabled = 1;
  1186.     dndi_ctx->is_di_adv_enabled = 0;
  1187.     dndi_ctx->is_first_frame = 0;
  1188.     dndi_ctx->is_second_field = 0;
  1189.  
  1190.     /* Check whether we are deinterlacing the second field */
  1191.     if (dndi_ctx->is_di_enabled) {
  1192.         const unsigned int tff =
  1193.             !(deint_params->flags & VA_DEINTERLACING_BOTTOM_FIELD_FIRST);
  1194.         const unsigned int is_top_field =
  1195.             !(deint_params->flags & VA_DEINTERLACING_BOTTOM_FIELD);
  1196.  
  1197.         if ((tff ^ is_top_field) != 0) {
  1198.             fs = &dndi_ctx->frame_store[DNDI_FRAME_IN_CURRENT];
  1199.             if (fs->surface_id != obj_surface->base.id) {
  1200.                 WARN_ONCE("invalid surface provided for second field\n");
  1201.                 return VA_STATUS_ERROR_INVALID_PARAMETER;
  1202.             }
  1203.             dndi_ctx->is_second_field = 1;
  1204.         }
  1205.     }
  1206.  
  1207.     /* Check whether we are deinterlacing the first frame */
  1208.     if (dndi_ctx->is_di_enabled) {
  1209.         switch (deint_params->algorithm) {
  1210.         case VAProcDeinterlacingBob:
  1211.             dndi_ctx->is_first_frame = 1;
  1212.             break;
  1213.         case VAProcDeinterlacingMotionAdaptive:
  1214.         case VAProcDeinterlacingMotionCompensated:
  1215.             fs = &dndi_ctx->frame_store[DNDI_FRAME_IN_CURRENT];
  1216.             if (fs->surface_id == VA_INVALID_ID)
  1217.                 dndi_ctx->is_first_frame = 1;
  1218.             else if (dndi_ctx->is_second_field) {
  1219.                 /* At this stage, we have already deinterlaced the
  1220.                    first field successfully. So, the first frame flag
  1221.                    is trigerred if the previous field was deinterlaced
  1222.                    without reference frame */
  1223.                 fs = &dndi_ctx->frame_store[DNDI_FRAME_IN_PREVIOUS];
  1224.                 if (fs->surface_id == VA_INVALID_ID)
  1225.                     dndi_ctx->is_first_frame = 1;
  1226.             }
  1227.             else {
  1228.                 if (pipe_params->num_forward_references < 1 ||
  1229.                     pipe_params->forward_references[0] == VA_INVALID_ID) {
  1230.                     WARN_ONCE("A forward temporal reference is needed for Motion adaptive/compensated deinterlacing !!!\n");
  1231.                     return VA_STATUS_ERROR_INVALID_PARAMETER;
  1232.                 }
  1233.             }
  1234.             dndi_ctx->is_di_adv_enabled = 1;
  1235.             break;
  1236.         default:
  1237.             WARN_ONCE("unsupported deinterlacing algorithm (%d)\n",
  1238.                       deint_params->algorithm);
  1239.             return VA_STATUS_ERROR_UNSUPPORTED_FILTER;
  1240.         }
  1241.     }
  1242.     return VA_STATUS_SUCCESS;
  1243. }
  1244.  
  1245. static VAStatus
  1246. pp_dndi_context_ensure_surfaces_storage(VADriverContextP ctx,
  1247.     struct i965_post_processing_context *pp_context,
  1248.     struct object_surface *src_surface, struct object_surface *dst_surface)
  1249. {
  1250.     struct i965_driver_data * const i965 = i965_driver_data(ctx);
  1251.     struct pp_dndi_context * const dndi_ctx = &pp_context->pp_dndi_context;
  1252.     unsigned int src_fourcc, dst_fourcc;
  1253.     unsigned int src_sampling, dst_sampling;
  1254.     unsigned int src_tiling, dst_tiling;
  1255.     unsigned int i, swizzle;
  1256.     VAStatus status;
  1257.  
  1258.     /* Determine input surface info. Always use NV12 Y-tiled */
  1259.     if (src_surface->bo) {
  1260.         src_fourcc = src_surface->fourcc;
  1261.         src_sampling = src_surface->subsampling;
  1262.         dri_bo_get_tiling(src_surface->bo, &src_tiling, &swizzle);
  1263.         src_tiling = !!src_tiling;
  1264.     }
  1265.     else {
  1266.         src_fourcc = VA_FOURCC_NV12;
  1267.         src_sampling = SUBSAMPLE_YUV420;
  1268.         src_tiling = 1;
  1269.         status = i965_check_alloc_surface_bo(ctx, src_surface,
  1270.             src_tiling, src_fourcc, src_sampling);
  1271.         if (status != VA_STATUS_SUCCESS)
  1272.             return status;
  1273.     }
  1274.  
  1275.     /* Determine output surface info. Always use NV12 Y-tiled */
  1276.     if (dst_surface->bo) {
  1277.         dst_fourcc   = dst_surface->fourcc;
  1278.         dst_sampling = dst_surface->subsampling;
  1279.         dri_bo_get_tiling(dst_surface->bo, &dst_tiling, &swizzle);
  1280.         dst_tiling = !!dst_tiling;
  1281.     }
  1282.     else {
  1283.         dst_fourcc = VA_FOURCC_NV12;
  1284.         dst_sampling = SUBSAMPLE_YUV420;
  1285.         dst_tiling = 1;
  1286.         status = i965_check_alloc_surface_bo(ctx, dst_surface,
  1287.             dst_tiling, dst_fourcc, dst_sampling);
  1288.         if (status != VA_STATUS_SUCCESS)
  1289.             return status;
  1290.     }
  1291.  
  1292.     /* Create pipeline surfaces */
  1293.     for (i = 0; i < ARRAY_ELEMS(dndi_ctx->frame_store); i ++) {
  1294.         struct object_surface *obj_surface;
  1295.         VASurfaceID new_surface;
  1296.         unsigned int width, height;
  1297.  
  1298.         if (dndi_ctx->frame_store[i].obj_surface &&
  1299.             dndi_ctx->frame_store[i].obj_surface->bo)
  1300.             continue; // user allocated surface, not VPP internal
  1301.  
  1302.         if (dndi_ctx->frame_store[i].obj_surface) {
  1303.             obj_surface = dndi_ctx->frame_store[i].obj_surface;
  1304.             dndi_ctx->frame_store[i].is_scratch_surface = 0;
  1305.         } else {
  1306.             if (i <= DNDI_FRAME_IN_STMM) {
  1307.                 width = src_surface->orig_width;
  1308.                 height = src_surface->orig_height;
  1309.             }
  1310.             else {
  1311.                 width = dst_surface->orig_width;
  1312.                 height = dst_surface->orig_height;
  1313.             }
  1314.  
  1315.             status = i965_CreateSurfaces(ctx, width, height, VA_RT_FORMAT_YUV420,
  1316.                                          1, &new_surface);
  1317.             if (status != VA_STATUS_SUCCESS)
  1318.                 return status;
  1319.  
  1320.             obj_surface = SURFACE(new_surface);
  1321.             assert(obj_surface != NULL);
  1322.             dndi_ctx->frame_store[i].is_scratch_surface = 1;
  1323.         }
  1324.  
  1325.         if (i <= DNDI_FRAME_IN_PREVIOUS) {
  1326.             status = i965_check_alloc_surface_bo(ctx, obj_surface,
  1327.                 src_tiling, src_fourcc, src_sampling);
  1328.         }
  1329.         else if (i == DNDI_FRAME_IN_STMM || i == DNDI_FRAME_OUT_STMM) {
  1330.             status = i965_check_alloc_surface_bo(ctx, obj_surface,
  1331.                 1, VA_FOURCC_Y800, SUBSAMPLE_YUV400);
  1332.         }
  1333.         else if (i >= DNDI_FRAME_OUT_CURRENT) {
  1334.             status = i965_check_alloc_surface_bo(ctx, obj_surface,
  1335.                 dst_tiling, dst_fourcc, dst_sampling);
  1336.         }
  1337.         if (status != VA_STATUS_SUCCESS)
  1338.             return status;
  1339.  
  1340.         dndi_ctx->frame_store[i].obj_surface = obj_surface;
  1341.     }
  1342.     return VA_STATUS_SUCCESS;
  1343. }
  1344.  
  1345. static VAStatus
  1346. pp_dndi_context_ensure_surfaces(VADriverContextP ctx,
  1347.     struct i965_post_processing_context *pp_context,
  1348.     struct object_surface *src_surface, struct object_surface *dst_surface)
  1349. {
  1350.     struct i965_driver_data * const i965 = i965_driver_data(ctx);
  1351.     struct pp_dndi_context * const dndi_ctx = &pp_context->pp_dndi_context;
  1352.     DNDIFrameStore *ifs, *ofs;
  1353.     bool is_new_frame = false;
  1354.  
  1355.     /* Update the previous input surface */
  1356.     is_new_frame = dndi_ctx->frame_store[DNDI_FRAME_IN_CURRENT].surface_id !=
  1357.         src_surface->base.id;
  1358.     if (is_new_frame) {
  1359.         ifs = &dndi_ctx->frame_store[DNDI_FRAME_IN_PREVIOUS];
  1360.         ofs = &dndi_ctx->frame_store[DNDI_FRAME_IN_CURRENT];
  1361.         do {
  1362.             const VAProcPipelineParameterBuffer * const pipe_params =
  1363.                 pp_context->pipeline_param;
  1364.             struct object_surface *obj_surface;
  1365.  
  1366.             if (pipe_params->num_forward_references < 1)
  1367.                 break;
  1368.             if (pipe_params->forward_references[0] == VA_INVALID_ID)
  1369.                 break;
  1370.  
  1371.             obj_surface = SURFACE(pipe_params->forward_references[0]);
  1372.             if (!obj_surface || obj_surface->base.id == ifs->surface_id)
  1373.                 break;
  1374.  
  1375.             pp_dndi_frame_store_clear(ifs, ctx);
  1376.             if (obj_surface->base.id == ofs->surface_id) {
  1377.                 *ifs = *ofs;
  1378.                 pp_dndi_frame_store_reset(ofs);
  1379.             }
  1380.             else {
  1381.                 ifs->obj_surface = obj_surface;
  1382.                 ifs->surface_id = obj_surface->base.id;
  1383.             }
  1384.         } while (0);
  1385.     }
  1386.  
  1387.     /* Update the input surface */
  1388.     ifs = &dndi_ctx->frame_store[DNDI_FRAME_IN_CURRENT];
  1389.     pp_dndi_frame_store_clear(ifs, ctx);
  1390.     ifs->obj_surface = src_surface;
  1391.     ifs->surface_id = src_surface->base.id;
  1392.  
  1393.     /* Update the Spatial Temporal Motion Measure (STMM) surfaces */
  1394.     if (is_new_frame)
  1395.         pp_dndi_frame_store_swap(&dndi_ctx->frame_store[DNDI_FRAME_IN_STMM],
  1396.             &dndi_ctx->frame_store[DNDI_FRAME_OUT_STMM]);
  1397.  
  1398.     /* Update the output surfaces */
  1399.     ofs = &dndi_ctx->frame_store[DNDI_FRAME_OUT_CURRENT];
  1400.     if (dndi_ctx->is_di_adv_enabled && !dndi_ctx->is_first_frame) {
  1401.         pp_dndi_frame_store_swap(ofs,
  1402.             &dndi_ctx->frame_store[DNDI_FRAME_OUT_PREVIOUS]);
  1403.         if (!dndi_ctx->is_second_field)
  1404.             ofs = &dndi_ctx->frame_store[DNDI_FRAME_OUT_PREVIOUS];
  1405.     }
  1406.     pp_dndi_frame_store_clear(ofs, ctx);
  1407.     ofs->obj_surface = dst_surface;
  1408.     ofs->surface_id = dst_surface->base.id;
  1409.  
  1410.     return VA_STATUS_SUCCESS;
  1411. }
  1412.  
  1413. static int
  1414. pp_get_surface_fourcc(VADriverContextP ctx, const struct i965_surface *surface)
  1415. {
  1416.     int fourcc;
  1417.  
  1418.     if (surface->type == I965_SURFACE_TYPE_IMAGE) {
  1419.         struct object_image *obj_image = (struct object_image *)surface->base;
  1420.         fourcc = obj_image->image.format.fourcc;
  1421.     } else {
  1422.         struct object_surface *obj_surface = (struct object_surface *)surface->base;
  1423.         fourcc = obj_surface->fourcc;
  1424.     }
  1425.  
  1426.     return fourcc;
  1427. }
  1428.  
  1429. static void
  1430. pp_get_surface_size(VADriverContextP ctx, const struct i965_surface *surface, int *width, int *height)
  1431. {
  1432.     if (surface->type == I965_SURFACE_TYPE_IMAGE) {
  1433.         struct object_image *obj_image = (struct object_image *)surface->base;
  1434.  
  1435.         *width = obj_image->image.width;
  1436.         *height = obj_image->image.height;
  1437.     } else {
  1438.         struct object_surface *obj_surface = (struct object_surface *)surface->base;
  1439.  
  1440.         *width = obj_surface->orig_width;
  1441.         *height = obj_surface->orig_height;
  1442.     }
  1443. }
  1444.  
  1445. static void
  1446. pp_set_surface_tiling(struct i965_surface_state *ss, unsigned int tiling)
  1447. {
  1448.     switch (tiling) {
  1449.     case I915_TILING_NONE:
  1450.         ss->ss3.tiled_surface = 0;
  1451.         ss->ss3.tile_walk = 0;
  1452.         break;
  1453.     case I915_TILING_X:
  1454.         ss->ss3.tiled_surface = 1;
  1455.         ss->ss3.tile_walk = I965_TILEWALK_XMAJOR;
  1456.         break;
  1457.     case I915_TILING_Y:
  1458.         ss->ss3.tiled_surface = 1;
  1459.         ss->ss3.tile_walk = I965_TILEWALK_YMAJOR;
  1460.         break;
  1461.     }
  1462. }
  1463.  
  1464. static void
  1465. pp_set_surface2_tiling(struct i965_surface_state2 *ss, unsigned int tiling)
  1466. {
  1467.     switch (tiling) {
  1468.     case I915_TILING_NONE:
  1469.         ss->ss2.tiled_surface = 0;
  1470.         ss->ss2.tile_walk = 0;
  1471.         break;
  1472.     case I915_TILING_X:
  1473.         ss->ss2.tiled_surface = 1;
  1474.         ss->ss2.tile_walk = I965_TILEWALK_XMAJOR;
  1475.         break;
  1476.     case I915_TILING_Y:
  1477.         ss->ss2.tiled_surface = 1;
  1478.         ss->ss2.tile_walk = I965_TILEWALK_YMAJOR;
  1479.         break;
  1480.     }
  1481. }
  1482.  
  1483. static void
  1484. gen7_pp_set_surface_tiling(struct gen7_surface_state *ss, unsigned int tiling)
  1485. {
  1486.     switch (tiling) {
  1487.     case I915_TILING_NONE:
  1488.         ss->ss0.tiled_surface = 0;
  1489.         ss->ss0.tile_walk = 0;
  1490.         break;
  1491.     case I915_TILING_X:
  1492.         ss->ss0.tiled_surface = 1;
  1493.         ss->ss0.tile_walk = I965_TILEWALK_XMAJOR;
  1494.         break;
  1495.     case I915_TILING_Y:
  1496.         ss->ss0.tiled_surface = 1;
  1497.         ss->ss0.tile_walk = I965_TILEWALK_YMAJOR;
  1498.         break;
  1499.     }
  1500. }
  1501.  
  1502. static void
  1503. gen7_pp_set_surface2_tiling(struct gen7_surface_state2 *ss, unsigned int tiling)
  1504. {
  1505.     switch (tiling) {
  1506.     case I915_TILING_NONE:
  1507.         ss->ss2.tiled_surface = 0;
  1508.         ss->ss2.tile_walk = 0;
  1509.         break;
  1510.     case I915_TILING_X:
  1511.         ss->ss2.tiled_surface = 1;
  1512.         ss->ss2.tile_walk = I965_TILEWALK_XMAJOR;
  1513.         break;
  1514.     case I915_TILING_Y:
  1515.         ss->ss2.tiled_surface = 1;
  1516.         ss->ss2.tile_walk = I965_TILEWALK_YMAJOR;
  1517.         break;
  1518.     }
  1519. }
  1520.  
  1521. static void
  1522. ironlake_pp_interface_descriptor_table(struct i965_post_processing_context *pp_context)
  1523. {
  1524.     struct i965_interface_descriptor *desc;
  1525.     dri_bo *bo;
  1526.     int pp_index = pp_context->current_pp;
  1527.  
  1528.     bo = pp_context->idrt.bo;
  1529.     dri_bo_map(bo, 1);
  1530.     assert(bo->virtual);
  1531.     desc = bo->virtual;
  1532.     memset(desc, 0, sizeof(*desc));
  1533.     desc->desc0.grf_reg_blocks = 10;
  1534.     desc->desc0.kernel_start_pointer = pp_context->pp_modules[pp_index].kernel.bo->offset >> 6; /* reloc */
  1535.     desc->desc1.const_urb_entry_read_offset = 0;
  1536.     desc->desc1.const_urb_entry_read_len = 4; /* grf 1-4 */
  1537.     desc->desc2.sampler_state_pointer = pp_context->sampler_state_table.bo->offset >> 5;
  1538.     desc->desc2.sampler_count = 0;
  1539.     desc->desc3.binding_table_entry_count = 0;
  1540.     desc->desc3.binding_table_pointer = (BINDING_TABLE_OFFSET >> 5);
  1541.  
  1542.     dri_bo_emit_reloc(bo,
  1543.                       I915_GEM_DOMAIN_INSTRUCTION, 0,
  1544.                       desc->desc0.grf_reg_blocks,
  1545.                       offsetof(struct i965_interface_descriptor, desc0),
  1546.                       pp_context->pp_modules[pp_index].kernel.bo);
  1547.  
  1548.     dri_bo_emit_reloc(bo,
  1549.                       I915_GEM_DOMAIN_INSTRUCTION, 0,
  1550.                       desc->desc2.sampler_count << 2,
  1551.                       offsetof(struct i965_interface_descriptor, desc2),
  1552.                       pp_context->sampler_state_table.bo);
  1553.  
  1554.     dri_bo_unmap(bo);
  1555.     pp_context->idrt.num_interface_descriptors++;
  1556. }
  1557.  
  1558. static void
  1559. ironlake_pp_vfe_state(struct i965_post_processing_context *pp_context)
  1560. {
  1561.     struct i965_vfe_state *vfe_state;
  1562.     dri_bo *bo;
  1563.  
  1564.     bo = pp_context->vfe_state.bo;
  1565.     dri_bo_map(bo, 1);
  1566.     assert(bo->virtual);
  1567.     vfe_state = bo->virtual;
  1568.     memset(vfe_state, 0, sizeof(*vfe_state));
  1569.     vfe_state->vfe1.max_threads = pp_context->urb.num_vfe_entries - 1;
  1570.     vfe_state->vfe1.urb_entry_alloc_size = pp_context->urb.size_vfe_entry - 1;
  1571.     vfe_state->vfe1.num_urb_entries = pp_context->urb.num_vfe_entries;
  1572.     vfe_state->vfe1.vfe_mode = VFE_GENERIC_MODE;
  1573.     vfe_state->vfe1.children_present = 0;
  1574.     vfe_state->vfe2.interface_descriptor_base =
  1575.         pp_context->idrt.bo->offset >> 4; /* reloc */
  1576.     dri_bo_emit_reloc(bo,
  1577.                       I915_GEM_DOMAIN_INSTRUCTION, 0,
  1578.                       0,
  1579.                       offsetof(struct i965_vfe_state, vfe2),
  1580.                       pp_context->idrt.bo);
  1581.     dri_bo_unmap(bo);
  1582. }
  1583.  
  1584. static void
  1585. ironlake_pp_upload_constants(struct i965_post_processing_context *pp_context)
  1586. {
  1587.     unsigned char *constant_buffer;
  1588.     struct pp_static_parameter *pp_static_parameter = pp_context->pp_static_parameter;
  1589.  
  1590.     assert(sizeof(*pp_static_parameter) == 128);
  1591.     dri_bo_map(pp_context->curbe.bo, 1);
  1592.     assert(pp_context->curbe.bo->virtual);
  1593.     constant_buffer = pp_context->curbe.bo->virtual;
  1594.     memcpy(constant_buffer, pp_static_parameter, sizeof(*pp_static_parameter));
  1595.     dri_bo_unmap(pp_context->curbe.bo);
  1596. }
  1597.  
  1598. static void
  1599. ironlake_pp_states_setup(VADriverContextP ctx,
  1600.                          struct i965_post_processing_context *pp_context)
  1601. {
  1602.     ironlake_pp_interface_descriptor_table(pp_context);
  1603.     ironlake_pp_vfe_state(pp_context);
  1604.     ironlake_pp_upload_constants(pp_context);
  1605. }
  1606.  
  1607. static void
  1608. ironlake_pp_pipeline_select(VADriverContextP ctx,
  1609.                             struct i965_post_processing_context *pp_context)
  1610. {
  1611.     struct intel_batchbuffer *batch = pp_context->batch;
  1612.  
  1613.     BEGIN_BATCH(batch, 1);
  1614.     OUT_BATCH(batch, CMD_PIPELINE_SELECT | PIPELINE_SELECT_MEDIA);
  1615.     ADVANCE_BATCH(batch);
  1616. }
  1617.  
  1618. static void
  1619. ironlake_pp_urb_layout(VADriverContextP ctx,
  1620.                        struct i965_post_processing_context *pp_context)
  1621. {
  1622.     struct intel_batchbuffer *batch = pp_context->batch;
  1623.     unsigned int vfe_fence, cs_fence;
  1624.  
  1625.     vfe_fence = pp_context->urb.cs_start;
  1626.     cs_fence = pp_context->urb.size;
  1627.  
  1628.     BEGIN_BATCH(batch, 3);
  1629.     OUT_BATCH(batch, CMD_URB_FENCE | UF0_VFE_REALLOC | UF0_CS_REALLOC | 1);
  1630.     OUT_BATCH(batch, 0);
  1631.     OUT_BATCH(batch,
  1632.               (vfe_fence << UF2_VFE_FENCE_SHIFT) |      /* VFE_SIZE */
  1633.               (cs_fence << UF2_CS_FENCE_SHIFT));        /* CS_SIZE */
  1634.     ADVANCE_BATCH(batch);
  1635. }
  1636.  
  1637. static void
  1638. ironlake_pp_state_base_address(VADriverContextP ctx,
  1639.                                struct i965_post_processing_context *pp_context)
  1640. {
  1641.     struct intel_batchbuffer *batch = pp_context->batch;
  1642.  
  1643.     BEGIN_BATCH(batch, 8);
  1644.     OUT_BATCH(batch, CMD_STATE_BASE_ADDRESS | 6);
  1645.     OUT_BATCH(batch, 0 | BASE_ADDRESS_MODIFY);
  1646.     OUT_RELOC(batch, pp_context->surface_state_binding_table.bo, I915_GEM_DOMAIN_INSTRUCTION, 0, BASE_ADDRESS_MODIFY);
  1647.     OUT_BATCH(batch, 0 | BASE_ADDRESS_MODIFY);
  1648.     OUT_BATCH(batch, 0 | BASE_ADDRESS_MODIFY);
  1649.     OUT_BATCH(batch, 0 | BASE_ADDRESS_MODIFY);
  1650.     OUT_BATCH(batch, 0 | BASE_ADDRESS_MODIFY);
  1651.     OUT_BATCH(batch, 0 | BASE_ADDRESS_MODIFY);
  1652.     ADVANCE_BATCH(batch);
  1653. }
  1654.  
  1655. static void
  1656. ironlake_pp_state_pointers(VADriverContextP ctx,
  1657.                            struct i965_post_processing_context *pp_context)
  1658. {
  1659.     struct intel_batchbuffer *batch = pp_context->batch;
  1660.  
  1661.     BEGIN_BATCH(batch, 3);
  1662.     OUT_BATCH(batch, CMD_MEDIA_STATE_POINTERS | 1);
  1663.     OUT_BATCH(batch, 0);
  1664.     OUT_RELOC(batch, pp_context->vfe_state.bo, I915_GEM_DOMAIN_INSTRUCTION, 0, 0);
  1665.     ADVANCE_BATCH(batch);
  1666. }
  1667.  
  1668. static void
  1669. ironlake_pp_cs_urb_layout(VADriverContextP ctx,
  1670.                           struct i965_post_processing_context *pp_context)
  1671. {
  1672.     struct intel_batchbuffer *batch = pp_context->batch;
  1673.  
  1674.     BEGIN_BATCH(batch, 2);
  1675.     OUT_BATCH(batch, CMD_CS_URB_STATE | 0);
  1676.     OUT_BATCH(batch,
  1677.               ((pp_context->urb.size_cs_entry - 1) << 4) |     /* URB Entry Allocation Size */
  1678.               (pp_context->urb.num_cs_entries << 0));          /* Number of URB Entries */
  1679.     ADVANCE_BATCH(batch);
  1680. }
  1681.  
  1682. static void
  1683. ironlake_pp_constant_buffer(VADriverContextP ctx,
  1684.                             struct i965_post_processing_context *pp_context)
  1685. {
  1686.     struct intel_batchbuffer *batch = pp_context->batch;
  1687.  
  1688.     BEGIN_BATCH(batch, 2);
  1689.     OUT_BATCH(batch, CMD_CONSTANT_BUFFER | (1 << 8) | (2 - 2));
  1690.     OUT_RELOC(batch, pp_context->curbe.bo,
  1691.               I915_GEM_DOMAIN_INSTRUCTION, 0,
  1692.               pp_context->urb.size_cs_entry - 1);
  1693.     ADVANCE_BATCH(batch);    
  1694. }
  1695.  
  1696. static void
  1697. ironlake_pp_object_walker(VADriverContextP ctx,
  1698.                           struct i965_post_processing_context *pp_context)
  1699. {
  1700.     struct intel_batchbuffer *batch = pp_context->batch;
  1701.     int x, x_steps, y, y_steps;
  1702.     struct pp_inline_parameter *pp_inline_parameter = pp_context->pp_inline_parameter;
  1703.  
  1704.     x_steps = pp_context->pp_x_steps(pp_context->private_context);
  1705.     y_steps = pp_context->pp_y_steps(pp_context->private_context);
  1706.  
  1707.     for (y = 0; y < y_steps; y++) {
  1708.         for (x = 0; x < x_steps; x++) {
  1709.             if (!pp_context->pp_set_block_parameter(pp_context, x, y)) {
  1710.                 BEGIN_BATCH(batch, 20);
  1711.                 OUT_BATCH(batch, CMD_MEDIA_OBJECT | 18);
  1712.                 OUT_BATCH(batch, 0);
  1713.                 OUT_BATCH(batch, 0); /* no indirect data */
  1714.                 OUT_BATCH(batch, 0);
  1715.  
  1716.                 /* inline data grf 5-6 */
  1717.                 assert(sizeof(*pp_inline_parameter) == 64);
  1718.                 intel_batchbuffer_data(batch, pp_inline_parameter, sizeof(*pp_inline_parameter));
  1719.  
  1720.                 ADVANCE_BATCH(batch);
  1721.             }
  1722.         }
  1723.     }
  1724. }
  1725.  
  1726. static void
  1727. ironlake_pp_pipeline_setup(VADriverContextP ctx,
  1728.                            struct i965_post_processing_context *pp_context)
  1729. {
  1730.     struct intel_batchbuffer *batch = pp_context->batch;
  1731.  
  1732.     intel_batchbuffer_start_atomic(batch, 0x1000);
  1733.     intel_batchbuffer_emit_mi_flush(batch);
  1734.     ironlake_pp_pipeline_select(ctx, pp_context);
  1735.     ironlake_pp_state_base_address(ctx, pp_context);
  1736.     ironlake_pp_state_pointers(ctx, pp_context);
  1737.     ironlake_pp_urb_layout(ctx, pp_context);
  1738.     ironlake_pp_cs_urb_layout(ctx, pp_context);
  1739.     ironlake_pp_constant_buffer(ctx, pp_context);
  1740.     ironlake_pp_object_walker(ctx, pp_context);
  1741.     intel_batchbuffer_end_atomic(batch);
  1742. }
  1743.  
  1744. // update u/v offset when the surface format are packed yuv
  1745. static void i965_update_src_surface_static_parameter(
  1746.     VADriverContextP    ctx,
  1747.     struct i965_post_processing_context *pp_context,
  1748.     const struct i965_surface *surface)
  1749. {
  1750.     struct pp_static_parameter *pp_static_parameter = pp_context->pp_static_parameter;
  1751.     int fourcc = pp_get_surface_fourcc(ctx, surface);
  1752.  
  1753.     switch (fourcc) {
  1754.     case VA_FOURCC_YUY2:
  1755.         pp_static_parameter->grf1.source_packed_u_offset = 1;
  1756.         pp_static_parameter->grf1.source_packed_v_offset = 3;
  1757.         break;
  1758.     case VA_FOURCC_UYVY:
  1759.         pp_static_parameter->grf1.source_packed_y_offset = 1;
  1760.         pp_static_parameter->grf1.source_packed_v_offset = 2;
  1761.         break;
  1762.     case VA_FOURCC_BGRX:
  1763.     case VA_FOURCC_BGRA:
  1764.         pp_static_parameter->grf1.source_rgb_layout = 0;
  1765.         break;
  1766.     case VA_FOURCC_RGBX:
  1767.     case VA_FOURCC_RGBA:
  1768.         pp_static_parameter->grf1.source_rgb_layout = 1;
  1769.         break;
  1770.     default:
  1771.         break;
  1772.     }
  1773.    
  1774. }
  1775.  
  1776. static void i965_update_dst_surface_static_parameter(
  1777.     VADriverContextP    ctx,
  1778.     struct i965_post_processing_context *pp_context,
  1779.     const struct i965_surface *surface)
  1780. {
  1781.     struct pp_static_parameter *pp_static_parameter = pp_context->pp_static_parameter;
  1782.     int fourcc = pp_get_surface_fourcc(ctx, surface);
  1783.  
  1784.     switch (fourcc) {
  1785.     case VA_FOURCC_YUY2:
  1786.         pp_static_parameter->grf1.r1_2.load_and_save.destination_packed_u_offset = 1;
  1787.         pp_static_parameter->grf1.r1_2.load_and_save.destination_packed_v_offset = 3;
  1788.         break;
  1789.     case VA_FOURCC_UYVY:
  1790.         pp_static_parameter->grf1.r1_2.load_and_save.destination_packed_y_offset = 1;
  1791.         pp_static_parameter->grf1.r1_2.load_and_save.destination_packed_v_offset = 2;
  1792.         break;
  1793.     case VA_FOURCC_BGRX:
  1794.     case VA_FOURCC_BGRA:
  1795.         pp_static_parameter->grf1.r1_2.csc.destination_rgb_layout = 0;
  1796.         break;
  1797.     case VA_FOURCC_RGBX:
  1798.     case VA_FOURCC_RGBA:
  1799.         pp_static_parameter->grf1.r1_2.csc.destination_rgb_layout = 1;
  1800.         break;
  1801.     default:
  1802.         break;
  1803.     }
  1804.    
  1805. }
  1806.  
  1807. static void
  1808. i965_pp_set_surface_state(VADriverContextP ctx, struct i965_post_processing_context *pp_context,
  1809.                           dri_bo *surf_bo, unsigned long surf_bo_offset,
  1810.                           int width, int height, int pitch, int format,
  1811.                           int index, int is_target)
  1812. {
  1813.     struct i965_surface_state *ss;
  1814.     dri_bo *ss_bo;
  1815.     unsigned int tiling;
  1816.     unsigned int swizzle;
  1817.  
  1818.     dri_bo_get_tiling(surf_bo, &tiling, &swizzle);
  1819.     ss_bo = pp_context->surface_state_binding_table.bo;
  1820.     assert(ss_bo);
  1821.  
  1822.     dri_bo_map(ss_bo, True);
  1823.     assert(ss_bo->virtual);
  1824.     ss = (struct i965_surface_state *)((char *)ss_bo->virtual + SURFACE_STATE_OFFSET(index));
  1825.     memset(ss, 0, sizeof(*ss));
  1826.     ss->ss0.surface_type = I965_SURFACE_2D;
  1827.     ss->ss0.surface_format = format;
  1828.     ss->ss1.base_addr = surf_bo->offset + surf_bo_offset;
  1829.     ss->ss2.width = width - 1;
  1830.     ss->ss2.height = height - 1;
  1831.     ss->ss3.pitch = pitch - 1;
  1832.     pp_set_surface_tiling(ss, tiling);
  1833.     dri_bo_emit_reloc(ss_bo,
  1834.                       I915_GEM_DOMAIN_RENDER, is_target ? I915_GEM_DOMAIN_RENDER : 0,
  1835.                       surf_bo_offset,
  1836.                       SURFACE_STATE_OFFSET(index) + offsetof(struct i965_surface_state, ss1),
  1837.                       surf_bo);
  1838.     ((unsigned int *)((char *)ss_bo->virtual + BINDING_TABLE_OFFSET))[index] = SURFACE_STATE_OFFSET(index);
  1839.     dri_bo_unmap(ss_bo);
  1840. }
  1841.  
  1842. static void
  1843. i965_pp_set_surface2_state(VADriverContextP ctx, struct i965_post_processing_context *pp_context,
  1844.                            dri_bo *surf_bo, unsigned long surf_bo_offset,
  1845.                            int width, int height, int wpitch,
  1846.                            int xoffset, int yoffset,
  1847.                            int format, int interleave_chroma,
  1848.                            int index)
  1849. {
  1850.     struct i965_surface_state2 *ss2;
  1851.     dri_bo *ss2_bo;
  1852.     unsigned int tiling;
  1853.     unsigned int swizzle;
  1854.  
  1855.     dri_bo_get_tiling(surf_bo, &tiling, &swizzle);
  1856.     ss2_bo = pp_context->surface_state_binding_table.bo;
  1857.     assert(ss2_bo);
  1858.  
  1859.     dri_bo_map(ss2_bo, True);
  1860.     assert(ss2_bo->virtual);
  1861.     ss2 = (struct i965_surface_state2 *)((char *)ss2_bo->virtual + SURFACE_STATE_OFFSET(index));
  1862.     memset(ss2, 0, sizeof(*ss2));
  1863.     ss2->ss0.surface_base_address = surf_bo->offset + surf_bo_offset;
  1864.     ss2->ss1.cbcr_pixel_offset_v_direction = 0;
  1865.     ss2->ss1.width = width - 1;
  1866.     ss2->ss1.height = height - 1;
  1867.     ss2->ss2.pitch = wpitch - 1;
  1868.     ss2->ss2.interleave_chroma = interleave_chroma;
  1869.     ss2->ss2.surface_format = format;
  1870.     ss2->ss3.x_offset_for_cb = xoffset;
  1871.     ss2->ss3.y_offset_for_cb = yoffset;
  1872.     pp_set_surface2_tiling(ss2, tiling);
  1873.     dri_bo_emit_reloc(ss2_bo,
  1874.                       I915_GEM_DOMAIN_RENDER, 0,
  1875.                       surf_bo_offset,
  1876.                       SURFACE_STATE_OFFSET(index) + offsetof(struct i965_surface_state2, ss0),
  1877.                       surf_bo);
  1878.     ((unsigned int *)((char *)ss2_bo->virtual + BINDING_TABLE_OFFSET))[index] = SURFACE_STATE_OFFSET(index);
  1879.     dri_bo_unmap(ss2_bo);
  1880. }
  1881.  
  1882. static void
  1883. gen7_pp_set_surface_state(VADriverContextP ctx, struct i965_post_processing_context *pp_context,
  1884.                           dri_bo *surf_bo, unsigned long surf_bo_offset,
  1885.                           int width, int height, int pitch, int format,
  1886.                           int index, int is_target)
  1887. {
  1888.     struct i965_driver_data * const i965 = i965_driver_data(ctx);  
  1889.     struct gen7_surface_state *ss;
  1890.     dri_bo *ss_bo;
  1891.     unsigned int tiling;
  1892.     unsigned int swizzle;
  1893.  
  1894.     dri_bo_get_tiling(surf_bo, &tiling, &swizzle);
  1895.     ss_bo = pp_context->surface_state_binding_table.bo;
  1896.     assert(ss_bo);
  1897.  
  1898.     dri_bo_map(ss_bo, True);
  1899.     assert(ss_bo->virtual);
  1900.     ss = (struct gen7_surface_state *)((char *)ss_bo->virtual + SURFACE_STATE_OFFSET(index));
  1901.     memset(ss, 0, sizeof(*ss));
  1902.     ss->ss0.surface_type = I965_SURFACE_2D;
  1903.     ss->ss0.surface_format = format;
  1904.     ss->ss1.base_addr = surf_bo->offset + surf_bo_offset;
  1905.     ss->ss2.width = width - 1;
  1906.     ss->ss2.height = height - 1;
  1907.     ss->ss3.pitch = pitch - 1;
  1908.     gen7_pp_set_surface_tiling(ss, tiling);
  1909.     if (IS_HASWELL(i965->intel.device_info))
  1910.         gen7_render_set_surface_scs(ss);
  1911.     dri_bo_emit_reloc(ss_bo,
  1912.                       I915_GEM_DOMAIN_RENDER, is_target ? I915_GEM_DOMAIN_RENDER : 0,
  1913.                       surf_bo_offset,
  1914.                       SURFACE_STATE_OFFSET(index) + offsetof(struct gen7_surface_state, ss1),
  1915.                       surf_bo);
  1916.     ((unsigned int *)((char *)ss_bo->virtual + BINDING_TABLE_OFFSET))[index] = SURFACE_STATE_OFFSET(index);
  1917.     dri_bo_unmap(ss_bo);
  1918. }
  1919.  
  1920. static void
  1921. gen7_pp_set_surface2_state(VADriverContextP ctx, struct i965_post_processing_context *pp_context,
  1922.                            dri_bo *surf_bo, unsigned long surf_bo_offset,
  1923.                            int width, int height, int wpitch,
  1924.                            int xoffset, int yoffset,
  1925.                            int format, int interleave_chroma,
  1926.                            int index)
  1927. {
  1928.     struct gen7_surface_state2 *ss2;
  1929.     dri_bo *ss2_bo;
  1930.     unsigned int tiling;
  1931.     unsigned int swizzle;
  1932.  
  1933.     dri_bo_get_tiling(surf_bo, &tiling, &swizzle);
  1934.     ss2_bo = pp_context->surface_state_binding_table.bo;
  1935.     assert(ss2_bo);
  1936.  
  1937.     dri_bo_map(ss2_bo, True);
  1938.     assert(ss2_bo->virtual);
  1939.     ss2 = (struct gen7_surface_state2 *)((char *)ss2_bo->virtual + SURFACE_STATE_OFFSET(index));
  1940.     memset(ss2, 0, sizeof(*ss2));
  1941.     ss2->ss0.surface_base_address = surf_bo->offset + surf_bo_offset;
  1942.     ss2->ss1.cbcr_pixel_offset_v_direction = 0;
  1943.     ss2->ss1.width = width - 1;
  1944.     ss2->ss1.height = height - 1;
  1945.     ss2->ss2.pitch = wpitch - 1;
  1946.     ss2->ss2.interleave_chroma = interleave_chroma;
  1947.     ss2->ss2.surface_format = format;
  1948.     ss2->ss3.x_offset_for_cb = xoffset;
  1949.     ss2->ss3.y_offset_for_cb = yoffset;
  1950.     gen7_pp_set_surface2_tiling(ss2, tiling);
  1951.     dri_bo_emit_reloc(ss2_bo,
  1952.                       I915_GEM_DOMAIN_RENDER, 0,
  1953.                       surf_bo_offset,
  1954.                       SURFACE_STATE_OFFSET(index) + offsetof(struct gen7_surface_state2, ss0),
  1955.                       surf_bo);
  1956.     ((unsigned int *)((char *)ss2_bo->virtual + BINDING_TABLE_OFFSET))[index] = SURFACE_STATE_OFFSET(index);
  1957.     dri_bo_unmap(ss2_bo);
  1958. }
  1959.  
  1960. static void
  1961. pp_set_media_rw_message_surface(VADriverContextP ctx, struct i965_post_processing_context *pp_context,
  1962.                                 const struct i965_surface *surface,
  1963.                                 int base_index, int is_target,
  1964.                                 int *width, int *height, int *pitch, int *offset)
  1965. {
  1966.     struct object_surface *obj_surface;
  1967.     struct object_image *obj_image;
  1968.     dri_bo *bo;
  1969.     int fourcc = pp_get_surface_fourcc(ctx, surface);
  1970.     const int Y = 0;
  1971.     const int U = ((fourcc == VA_FOURCC_YV12) ||
  1972.                    (fourcc == VA_FOURCC_YV16))
  1973.                    ? 2 : 1;
  1974.     const int V = ((fourcc == VA_FOURCC_YV12) ||
  1975.                    (fourcc == VA_FOURCC_YV16))
  1976.                    ? 1 : 2;
  1977.     const int UV = 1;
  1978.     int interleaved_uv = fourcc == VA_FOURCC_NV12;
  1979.     int packed_yuv = (fourcc == VA_FOURCC_YUY2 || fourcc == VA_FOURCC_UYVY);
  1980.     int full_packed_format = (fourcc == VA_FOURCC_RGBA ||
  1981.                               fourcc == VA_FOURCC_RGBX ||
  1982.                               fourcc == VA_FOURCC_BGRA ||
  1983.                               fourcc == VA_FOURCC_BGRX);
  1984.     int scale_factor_of_1st_plane_width_in_byte = 1;
  1985.                              
  1986.     if (surface->type == I965_SURFACE_TYPE_SURFACE) {
  1987.         obj_surface = (struct object_surface *)surface->base;
  1988.         bo = obj_surface->bo;
  1989.         width[0] = obj_surface->orig_width;
  1990.         height[0] = obj_surface->orig_height;
  1991.         pitch[0] = obj_surface->width;
  1992.         offset[0] = 0;
  1993.  
  1994.         if (full_packed_format) {
  1995.             scale_factor_of_1st_plane_width_in_byte = 4;
  1996.         }
  1997.         else if (packed_yuv ) {
  1998.             scale_factor_of_1st_plane_width_in_byte =  2;
  1999.         }
  2000.         else if (interleaved_uv) {
  2001.             width[1] = obj_surface->orig_width;
  2002.             height[1] = obj_surface->orig_height / 2;
  2003.             pitch[1] = obj_surface->width;
  2004.             offset[1] = offset[0] + obj_surface->width * obj_surface->height;
  2005.         } else {
  2006.             width[1] = obj_surface->orig_width / 2;
  2007.             height[1] = obj_surface->orig_height / 2;
  2008.             pitch[1] = obj_surface->width / 2;
  2009.             offset[1] = offset[0] + obj_surface->width * obj_surface->height;
  2010.             width[2] = obj_surface->orig_width / 2;
  2011.             height[2] = obj_surface->orig_height / 2;
  2012.             pitch[2] = obj_surface->width / 2;
  2013.             offset[2] = offset[1] + (obj_surface->width / 2) * (obj_surface->height / 2);
  2014.         }
  2015.     } else {
  2016.         obj_image = (struct object_image *)surface->base;
  2017.         bo = obj_image->bo;
  2018.         width[0] = obj_image->image.width;
  2019.         height[0] = obj_image->image.height;
  2020.         pitch[0] = obj_image->image.pitches[0];
  2021.         offset[0] = obj_image->image.offsets[0];
  2022.  
  2023.         if (full_packed_format) {
  2024.             scale_factor_of_1st_plane_width_in_byte = 4;
  2025.         }
  2026.         else if (packed_yuv ) {
  2027.             scale_factor_of_1st_plane_width_in_byte = 2;
  2028.         }
  2029.         else if (interleaved_uv) {
  2030.             width[1] = obj_image->image.width;
  2031.             height[1] = obj_image->image.height / 2;
  2032.             pitch[1] = obj_image->image.pitches[1];
  2033.             offset[1] = obj_image->image.offsets[1];
  2034.         } else {
  2035.             width[1] = obj_image->image.width / 2;
  2036.             height[1] = obj_image->image.height / 2;
  2037.             pitch[1] = obj_image->image.pitches[1];
  2038.             offset[1] = obj_image->image.offsets[1];
  2039.             width[2] = obj_image->image.width / 2;
  2040.             height[2] = obj_image->image.height / 2;
  2041.             pitch[2] = obj_image->image.pitches[2];
  2042.             offset[2] = obj_image->image.offsets[2];
  2043.             if (fourcc == VA_FOURCC_YV16) {
  2044.                 width[1] = obj_image->image.width / 2;
  2045.                 height[1] = obj_image->image.height;
  2046.                 width[2] = obj_image->image.width / 2;
  2047.                 height[2] = obj_image->image.height;
  2048.             }
  2049.         }
  2050.     }
  2051.  
  2052.     /* Y surface */
  2053.     i965_pp_set_surface_state(ctx, pp_context,
  2054.                               bo, offset[Y],
  2055.                               ALIGN(width[Y] *scale_factor_of_1st_plane_width_in_byte, 4) / 4, height[Y], pitch[Y], I965_SURFACEFORMAT_R8_UNORM,
  2056.                               base_index, is_target);
  2057.  
  2058.     if (!packed_yuv && !full_packed_format) {
  2059.         if (interleaved_uv) {
  2060.             i965_pp_set_surface_state(ctx, pp_context,
  2061.                                       bo, offset[UV],
  2062.                                       ALIGN(width[UV], 4) / 4, height[UV], pitch[UV], I965_SURFACEFORMAT_R8_UNORM,
  2063.                                       base_index + 1, is_target);
  2064.         } else {
  2065.             /* U surface */
  2066.             i965_pp_set_surface_state(ctx, pp_context,
  2067.                                       bo, offset[U],
  2068.                                       ALIGN(width[U], 4) / 4, height[U], pitch[U], I965_SURFACEFORMAT_R8_UNORM,
  2069.                                       base_index + 1, is_target);
  2070.  
  2071.             /* V surface */
  2072.             i965_pp_set_surface_state(ctx, pp_context,
  2073.                                       bo, offset[V],
  2074.                                       ALIGN(width[V], 4) / 4, height[V], pitch[V], I965_SURFACEFORMAT_R8_UNORM,
  2075.                                       base_index + 2, is_target);
  2076.         }
  2077.     }
  2078.  
  2079. }
  2080.  
  2081. static void
  2082. gen7_pp_set_media_rw_message_surface(VADriverContextP ctx, struct i965_post_processing_context *pp_context,
  2083.                                      const struct i965_surface *surface,
  2084.                                      int base_index, int is_target,
  2085.                                      const VARectangle *rect,
  2086.                                      int *width, int *height, int *pitch, int *offset)
  2087. {
  2088.     struct object_surface *obj_surface;
  2089.     struct object_image *obj_image;
  2090.     dri_bo *bo;
  2091.     int fourcc = pp_get_surface_fourcc(ctx, surface);
  2092.     const i965_fourcc_info *fourcc_info = get_fourcc_info(fourcc);
  2093.  
  2094.     if (fourcc_info == NULL)
  2095.         return;
  2096.  
  2097.     if (surface->type == I965_SURFACE_TYPE_SURFACE) {
  2098.         obj_surface = (struct object_surface *)surface->base;
  2099.         bo = obj_surface->bo;
  2100.         width[0] = MIN(rect->x + rect->width, obj_surface->orig_width);
  2101.         height[0] = MIN(rect->y + rect->height, obj_surface->orig_height);
  2102.         pitch[0] = obj_surface->width;
  2103.         offset[0] = 0;
  2104.  
  2105.         if (fourcc_info->num_planes == 1 && is_target)
  2106.             width[0] = width[0] * (fourcc_info->bpp[0] / 8); /* surface format is R8 */
  2107.  
  2108.         width[1] = MIN(rect->x / fourcc_info->hfactor + rect->width / fourcc_info->hfactor, obj_surface->cb_cr_width);
  2109.         height[1] = MIN(rect->y / fourcc_info->vfactor + rect->height / fourcc_info->vfactor, obj_surface->cb_cr_height);
  2110.         pitch[1] = obj_surface->cb_cr_pitch;
  2111.         offset[1] = obj_surface->y_cb_offset * obj_surface->width;
  2112.  
  2113.         width[2] = MIN(rect->x / fourcc_info->hfactor + rect->width / fourcc_info->hfactor, obj_surface->cb_cr_width);
  2114.         height[2] = MIN(rect->y / fourcc_info->vfactor + rect->height / fourcc_info->vfactor, obj_surface->cb_cr_height);
  2115.         pitch[2] = obj_surface->cb_cr_pitch;
  2116.         offset[2] = obj_surface->y_cr_offset * obj_surface->width;
  2117.     } else {
  2118.         int U = 0, V = 0;
  2119.  
  2120.         /* FIXME: add support for ARGB/ABGR image */
  2121.         obj_image = (struct object_image *)surface->base;
  2122.         bo = obj_image->bo;
  2123.         width[0] = MIN(rect->x + rect->width, obj_image->image.width);
  2124.         height[0] = MIN(rect->y + rect->height, obj_image->image.height);
  2125.         pitch[0] = obj_image->image.pitches[0];
  2126.         offset[0] = obj_image->image.offsets[0];
  2127.  
  2128.         if (fourcc_info->num_planes == 1) {
  2129.             if (is_target)
  2130.                 width[0] = width[0] * (fourcc_info->bpp[0] / 8); /* surface format is R8 */
  2131.         } else if (fourcc_info->num_planes == 2) {
  2132.             U = 1, V = 1;
  2133.         } else {
  2134.             assert(fourcc_info->num_components == 3);
  2135.  
  2136.             U = fourcc_info->components[1].plane;
  2137.             V = fourcc_info->components[2].plane;
  2138.             assert((U == 1 && V == 2) ||
  2139.                    (U == 2 && V == 1));
  2140.         }
  2141.  
  2142.         /* Always set width/height although they aren't used for fourcc_info->num_planes == 1 */
  2143.         width[1] = MIN(rect->x / fourcc_info->hfactor + rect->width / fourcc_info->hfactor, obj_image->image.width / fourcc_info->hfactor);
  2144.         height[1] = MIN(rect->y / fourcc_info->vfactor + rect->height / fourcc_info->vfactor, obj_image->image.height / fourcc_info->vfactor);
  2145.         pitch[1] = obj_image->image.pitches[U];
  2146.         offset[1] = obj_image->image.offsets[U];
  2147.  
  2148.         width[2] = MIN(rect->x / fourcc_info->hfactor + rect->width / fourcc_info->hfactor, obj_image->image.width / fourcc_info->hfactor);
  2149.         height[2] = MIN(rect->y / fourcc_info->vfactor + rect->height / fourcc_info->vfactor, obj_image->image.height / fourcc_info->vfactor);
  2150.         pitch[2] = obj_image->image.pitches[V];
  2151.         offset[2] = obj_image->image.offsets[V];
  2152.     }
  2153.  
  2154.     if (is_target) {
  2155.         gen7_pp_set_surface_state(ctx, pp_context,
  2156.                                   bo, 0,
  2157.                                   ALIGN(width[0], 4) / 4, height[0], pitch[0],
  2158.                                   I965_SURFACEFORMAT_R8_UINT,
  2159.                                   base_index, 1);
  2160.  
  2161.         if (fourcc_info->num_planes == 2) {
  2162.             gen7_pp_set_surface_state(ctx, pp_context,
  2163.                                       bo, offset[1],
  2164.                                       ALIGN(width[1], 2) / 2, height[1], pitch[1],
  2165.                                       I965_SURFACEFORMAT_R8G8_SINT,
  2166.                                       base_index + 1, 1);
  2167.         } else if (fourcc_info->num_planes == 3) {
  2168.             gen7_pp_set_surface_state(ctx, pp_context,
  2169.                                       bo, offset[1],
  2170.                                       ALIGN(width[1], 4) / 4, height[1], pitch[1],
  2171.                                       I965_SURFACEFORMAT_R8_SINT,
  2172.                                       base_index + 1, 1);
  2173.             gen7_pp_set_surface_state(ctx, pp_context,
  2174.                                       bo, offset[2],
  2175.                                       ALIGN(width[2], 4) / 4, height[2], pitch[2],
  2176.                                       I965_SURFACEFORMAT_R8_SINT,
  2177.                                       base_index + 2, 1);
  2178.         }
  2179.  
  2180.         if (fourcc_info->format == I965_COLOR_RGB) {
  2181.             struct gen7_pp_static_parameter *pp_static_parameter = pp_context->pp_static_parameter;
  2182.             /* the format is MSB: X-B-G-R */
  2183.             pp_static_parameter->grf2.save_avs_rgb_swap = 0;
  2184.             if ((fourcc == VA_FOURCC_BGRA) ||
  2185.                 (fourcc == VA_FOURCC_BGRX)) {
  2186.                 /* It is stored as MSB: X-R-G-B */
  2187.                 pp_static_parameter->grf2.save_avs_rgb_swap = 1;
  2188.             }
  2189.         }
  2190.     } else {
  2191.         int format0 = SURFACE_FORMAT_Y8_UNORM;
  2192.  
  2193.         switch (fourcc) {
  2194.         case VA_FOURCC_YUY2:
  2195.             format0 = SURFACE_FORMAT_YCRCB_NORMAL;
  2196.             break;
  2197.  
  2198.         case VA_FOURCC_UYVY:
  2199.             format0 = SURFACE_FORMAT_YCRCB_SWAPY;
  2200.             break;
  2201.  
  2202.         default:
  2203.             break;
  2204.         }
  2205.  
  2206.         if (fourcc_info->format == I965_COLOR_RGB) {
  2207.             struct gen7_pp_static_parameter *pp_static_parameter = pp_context->pp_static_parameter;
  2208.             /* Only R8G8B8A8_UNORM is supported for BGRX or RGBX */
  2209.             format0 = SURFACE_FORMAT_R8G8B8A8_UNORM;
  2210.             pp_static_parameter->grf2.src_avs_rgb_swap = 0;
  2211.             if ((fourcc == VA_FOURCC_BGRA) ||
  2212.                 (fourcc == VA_FOURCC_BGRX)) {
  2213.                 pp_static_parameter->grf2.src_avs_rgb_swap = 1;
  2214.             }
  2215.         }
  2216.  
  2217.         gen7_pp_set_surface2_state(ctx, pp_context,
  2218.                                    bo, offset[0],
  2219.                                    width[0], height[0], pitch[0],
  2220.                                    0, 0,
  2221.                                    format0, 0,
  2222.                                    base_index);
  2223.  
  2224.         if (fourcc_info->num_planes == 2) {
  2225.             gen7_pp_set_surface2_state(ctx, pp_context,
  2226.                                        bo, offset[1],
  2227.                                        width[1], height[1], pitch[1],
  2228.                                        0, 0,
  2229.                                        SURFACE_FORMAT_R8B8_UNORM, 0,
  2230.                                        base_index + 1);
  2231.         } else if (fourcc_info->num_planes == 3) {
  2232.             gen7_pp_set_surface2_state(ctx, pp_context,
  2233.                                        bo, offset[1],
  2234.                                        width[1], height[1], pitch[1],
  2235.                                        0, 0,
  2236.                                        SURFACE_FORMAT_R8_UNORM, 0,
  2237.                                        base_index + 1);
  2238.             gen7_pp_set_surface2_state(ctx, pp_context,
  2239.                                        bo, offset[2],
  2240.                                        width[2], height[2], pitch[2],
  2241.                                        0, 0,
  2242.                                        SURFACE_FORMAT_R8_UNORM, 0,
  2243.                                        base_index + 2);
  2244.         }
  2245.     }
  2246. }
  2247.  
  2248. static int
  2249. pp_null_x_steps(void *private_context)
  2250. {
  2251.     return 1;
  2252. }
  2253.  
  2254. static int
  2255. pp_null_y_steps(void *private_context)
  2256. {
  2257.     return 1;
  2258. }
  2259.  
  2260. static int
  2261. pp_null_set_block_parameter(struct i965_post_processing_context *pp_context, int x, int y)
  2262. {
  2263.     return 0;
  2264. }
  2265.  
  2266. static VAStatus
  2267. pp_null_initialize(VADriverContextP ctx, struct i965_post_processing_context *pp_context,
  2268.                    const struct i965_surface *src_surface,
  2269.                    const VARectangle *src_rect,
  2270.                    struct i965_surface *dst_surface,
  2271.                    const VARectangle *dst_rect,
  2272.                    void *filter_param)
  2273. {
  2274.     /* private function & data */
  2275.     pp_context->pp_x_steps = pp_null_x_steps;
  2276.     pp_context->pp_y_steps = pp_null_y_steps;
  2277.     pp_context->private_context = NULL;
  2278.     pp_context->pp_set_block_parameter = pp_null_set_block_parameter;
  2279.  
  2280.     dst_surface->flags = src_surface->flags;
  2281.  
  2282.     return VA_STATUS_SUCCESS;
  2283. }
  2284.  
  2285. static int
  2286. pp_load_save_x_steps(void *private_context)
  2287. {
  2288.     return 1;
  2289. }
  2290.  
  2291. static int
  2292. pp_load_save_y_steps(void *private_context)
  2293. {
  2294.     struct pp_load_save_context *pp_load_save_context = private_context;
  2295.  
  2296.     return pp_load_save_context->dest_h / 8;
  2297. }
  2298.  
  2299. static int
  2300. pp_load_save_set_block_parameter(struct i965_post_processing_context *pp_context, int x, int y)
  2301. {
  2302.     struct pp_inline_parameter *pp_inline_parameter = pp_context->pp_inline_parameter;
  2303.     struct pp_load_save_context *pp_load_save_context = (struct pp_load_save_context *)pp_context->private_context;
  2304.  
  2305.     pp_inline_parameter->grf5.destination_block_horizontal_origin = x * 16 + pp_load_save_context->dest_x;
  2306.     pp_inline_parameter->grf5.destination_block_vertical_origin = y * 8 + pp_load_save_context->dest_y;
  2307.  
  2308.     return 0;
  2309. }
  2310.  
  2311. static void calculate_boundary_block_mask(struct i965_post_processing_context *pp_context, const VARectangle *dst_rect)
  2312. {
  2313.     int i;
  2314.     /* x offset of dest surface must be dword aligned.
  2315.      * so we have to extend dst surface on left edge, and mask out pixels not interested
  2316.      */
  2317.     if (dst_rect->x%GPU_ASM_X_OFFSET_ALIGNMENT) {
  2318.         pp_context->block_horizontal_mask_left = 0;
  2319.         for (i=dst_rect->x%GPU_ASM_X_OFFSET_ALIGNMENT; i<GPU_ASM_BLOCK_WIDTH; i++)
  2320.         {
  2321.             pp_context->block_horizontal_mask_left |= 1<<i;
  2322.         }
  2323.     }
  2324.     else {
  2325.         pp_context->block_horizontal_mask_left = 0xffff;
  2326.     }
  2327.    
  2328.     int dst_width_adjust = dst_rect->width + dst_rect->x%GPU_ASM_X_OFFSET_ALIGNMENT;
  2329.     if (dst_width_adjust%GPU_ASM_BLOCK_WIDTH){
  2330.         pp_context->block_horizontal_mask_right = (1 << (dst_width_adjust%GPU_ASM_BLOCK_WIDTH)) - 1;
  2331.     }
  2332.     else {
  2333.         pp_context->block_horizontal_mask_right = 0xffff;
  2334.     }
  2335.    
  2336.     if (dst_rect->height%GPU_ASM_BLOCK_HEIGHT){
  2337.         pp_context->block_vertical_mask_bottom = (1 << (dst_rect->height%GPU_ASM_BLOCK_HEIGHT)) - 1;
  2338.     }
  2339.     else {
  2340.         pp_context->block_vertical_mask_bottom = 0xff;
  2341.     }
  2342.  
  2343. }
  2344. static VAStatus
  2345. pp_plx_load_save_plx_initialize(VADriverContextP ctx, struct i965_post_processing_context *pp_context,
  2346.                                 const struct i965_surface *src_surface,
  2347.                                 const VARectangle *src_rect,
  2348.                                 struct i965_surface *dst_surface,
  2349.                                 const VARectangle *dst_rect,
  2350.                                 void *filter_param)
  2351. {
  2352.     struct pp_load_save_context *pp_load_save_context = (struct pp_load_save_context *)&pp_context->pp_load_save_context;
  2353.     struct pp_inline_parameter *pp_inline_parameter = pp_context->pp_inline_parameter;
  2354.     struct pp_static_parameter *pp_static_parameter = pp_context->pp_static_parameter;
  2355.     int width[3], height[3], pitch[3], offset[3];
  2356.  
  2357.     /* source surface */
  2358.     pp_set_media_rw_message_surface(ctx, pp_context, src_surface, 1, 0,
  2359.                                     width, height, pitch, offset);
  2360.  
  2361.     /* destination surface */
  2362.     pp_set_media_rw_message_surface(ctx, pp_context, dst_surface, 7, 1,
  2363.                                     width, height, pitch, offset);
  2364.  
  2365.     /* private function & data */
  2366.     pp_context->pp_x_steps = pp_load_save_x_steps;
  2367.     pp_context->pp_y_steps = pp_load_save_y_steps;
  2368.     pp_context->private_context = &pp_context->pp_load_save_context;
  2369.     pp_context->pp_set_block_parameter = pp_load_save_set_block_parameter;
  2370.  
  2371.     int dst_left_edge_extend = dst_rect->x%GPU_ASM_X_OFFSET_ALIGNMENT;;
  2372.     pp_load_save_context->dest_x = dst_rect->x - dst_left_edge_extend;
  2373.     pp_load_save_context->dest_y = dst_rect->y;
  2374.     pp_load_save_context->dest_h = ALIGN(dst_rect->height, 8);
  2375.     pp_load_save_context->dest_w = ALIGN(dst_rect->width+dst_left_edge_extend, 16);
  2376.  
  2377.     pp_inline_parameter->grf5.block_count_x = pp_load_save_context->dest_w / 16;   /* 1 x N */
  2378.     pp_inline_parameter->grf5.number_blocks = pp_load_save_context->dest_w / 16;
  2379.  
  2380.     pp_static_parameter->grf3.horizontal_origin_offset = src_rect->x;
  2381.     pp_static_parameter->grf3.vertical_origin_offset = src_rect->y;
  2382.  
  2383.     // update u/v offset for packed yuv
  2384.     i965_update_src_surface_static_parameter (ctx, pp_context, src_surface);
  2385.     i965_update_dst_surface_static_parameter (ctx, pp_context, dst_surface);
  2386.  
  2387.     dst_surface->flags = src_surface->flags;
  2388.  
  2389.     return VA_STATUS_SUCCESS;
  2390. }
  2391.  
  2392. static int
  2393. pp_scaling_x_steps(void *private_context)
  2394. {
  2395.     return 1;
  2396. }
  2397.  
  2398. static int
  2399. pp_scaling_y_steps(void *private_context)
  2400. {
  2401.     struct pp_scaling_context *pp_scaling_context = private_context;
  2402.  
  2403.     return pp_scaling_context->dest_h / 8;
  2404. }
  2405.  
  2406. static int
  2407. pp_scaling_set_block_parameter(struct i965_post_processing_context *pp_context, int x, int y)
  2408. {
  2409.     struct pp_scaling_context *pp_scaling_context = (struct pp_scaling_context *)pp_context->private_context;
  2410.     struct pp_inline_parameter *pp_inline_parameter = pp_context->pp_inline_parameter;
  2411.     struct pp_static_parameter *pp_static_parameter = pp_context->pp_static_parameter;
  2412.     float src_x_steping = pp_inline_parameter->grf5.normalized_video_x_scaling_step;
  2413.     float src_y_steping = pp_static_parameter->grf1.r1_6.normalized_video_y_scaling_step;
  2414.  
  2415.     pp_inline_parameter->grf5.r5_1.source_surface_block_normalized_horizontal_origin = src_x_steping * x * 16 + pp_scaling_context->src_normalized_x;
  2416.     pp_inline_parameter->grf5.source_surface_block_normalized_vertical_origin = src_y_steping * y * 8 + pp_scaling_context->src_normalized_y;
  2417.     pp_inline_parameter->grf5.destination_block_horizontal_origin = x * 16 + pp_scaling_context->dest_x;
  2418.     pp_inline_parameter->grf5.destination_block_vertical_origin = y * 8 + pp_scaling_context->dest_y;
  2419.    
  2420.     return 0;
  2421. }
  2422.  
  2423. static VAStatus
  2424. pp_nv12_scaling_initialize(VADriverContextP ctx, struct i965_post_processing_context *pp_context,
  2425.                            const struct i965_surface *src_surface,
  2426.                            const VARectangle *src_rect,
  2427.                            struct i965_surface *dst_surface,
  2428.                            const VARectangle *dst_rect,
  2429.                            void *filter_param)
  2430. {
  2431.     struct pp_scaling_context *pp_scaling_context = (struct pp_scaling_context *)&pp_context->pp_scaling_context;
  2432.     struct pp_inline_parameter *pp_inline_parameter = pp_context->pp_inline_parameter;
  2433.     struct pp_static_parameter *pp_static_parameter = pp_context->pp_static_parameter;
  2434.     struct object_surface *obj_surface;
  2435.     struct i965_sampler_state *sampler_state;
  2436.     int in_w, in_h, in_wpitch, in_hpitch;
  2437.     int out_w, out_h, out_wpitch, out_hpitch;
  2438.  
  2439.     /* source surface */
  2440.     obj_surface = (struct object_surface *)src_surface->base;
  2441.     in_w = obj_surface->orig_width;
  2442.     in_h = obj_surface->orig_height;
  2443.     in_wpitch = obj_surface->width;
  2444.     in_hpitch = obj_surface->height;
  2445.  
  2446.     /* source Y surface index 1 */
  2447.     i965_pp_set_surface_state(ctx, pp_context,
  2448.                               obj_surface->bo, 0,
  2449.                               in_w, in_h, in_wpitch, I965_SURFACEFORMAT_R8_UNORM,
  2450.                               1, 0);
  2451.  
  2452.     /* source UV surface index 2 */
  2453.     i965_pp_set_surface_state(ctx, pp_context,
  2454.                               obj_surface->bo, in_wpitch * in_hpitch,
  2455.                               ALIGN(in_w, 2) / 2, in_h / 2, in_wpitch, I965_SURFACEFORMAT_R8G8_UNORM,
  2456.                               2, 0);
  2457.  
  2458.     /* destination surface */
  2459.     obj_surface = (struct object_surface *)dst_surface->base;
  2460.     out_w = obj_surface->orig_width;
  2461.     out_h = obj_surface->orig_height;
  2462.     out_wpitch = obj_surface->width;
  2463.     out_hpitch = obj_surface->height;
  2464.  
  2465.     /* destination Y surface index 7 */
  2466.     i965_pp_set_surface_state(ctx, pp_context,
  2467.                               obj_surface->bo, 0,
  2468.                               ALIGN(out_w, 4) / 4, out_h, out_wpitch, I965_SURFACEFORMAT_R8_UNORM,
  2469.                               7, 1);
  2470.  
  2471.     /* destination UV surface index 8 */
  2472.     i965_pp_set_surface_state(ctx, pp_context,
  2473.                               obj_surface->bo, out_wpitch * out_hpitch,
  2474.                               ALIGN(out_w, 4) / 4, out_h / 2, out_wpitch, I965_SURFACEFORMAT_R8G8_UNORM,
  2475.                               8, 1);
  2476.  
  2477.     /* sampler state */
  2478.     dri_bo_map(pp_context->sampler_state_table.bo, True);
  2479.     assert(pp_context->sampler_state_table.bo->virtual);
  2480.     sampler_state = pp_context->sampler_state_table.bo->virtual;
  2481.  
  2482.     /* SIMD16 Y index 1 */
  2483.     sampler_state[1].ss0.min_filter = I965_MAPFILTER_LINEAR;
  2484.     sampler_state[1].ss0.mag_filter = I965_MAPFILTER_LINEAR;
  2485.     sampler_state[1].ss1.r_wrap_mode = I965_TEXCOORDMODE_CLAMP;
  2486.     sampler_state[1].ss1.s_wrap_mode = I965_TEXCOORDMODE_CLAMP;
  2487.     sampler_state[1].ss1.t_wrap_mode = I965_TEXCOORDMODE_CLAMP;
  2488.  
  2489.     /* SIMD16 UV index 2 */
  2490.     sampler_state[2].ss0.min_filter = I965_MAPFILTER_LINEAR;
  2491.     sampler_state[2].ss0.mag_filter = I965_MAPFILTER_LINEAR;
  2492.     sampler_state[2].ss1.r_wrap_mode = I965_TEXCOORDMODE_CLAMP;
  2493.     sampler_state[2].ss1.s_wrap_mode = I965_TEXCOORDMODE_CLAMP;
  2494.     sampler_state[2].ss1.t_wrap_mode = I965_TEXCOORDMODE_CLAMP;
  2495.  
  2496.     dri_bo_unmap(pp_context->sampler_state_table.bo);
  2497.  
  2498.     /* private function & data */
  2499.     pp_context->pp_x_steps = pp_scaling_x_steps;
  2500.     pp_context->pp_y_steps = pp_scaling_y_steps;
  2501.     pp_context->private_context = &pp_context->pp_scaling_context;
  2502.     pp_context->pp_set_block_parameter = pp_scaling_set_block_parameter;
  2503.  
  2504.     int dst_left_edge_extend = dst_rect->x%GPU_ASM_X_OFFSET_ALIGNMENT;
  2505.     float src_left_edge_extend = (float)dst_left_edge_extend*src_rect->width/dst_rect->width;
  2506.     pp_scaling_context->dest_x = dst_rect->x - dst_left_edge_extend;
  2507.     pp_scaling_context->dest_y = dst_rect->y;
  2508.     pp_scaling_context->dest_w = ALIGN(dst_rect->width + dst_left_edge_extend, 16);
  2509.     pp_scaling_context->dest_h = ALIGN(dst_rect->height, 8);
  2510.     pp_scaling_context->src_normalized_x = (float)(src_rect->x - src_left_edge_extend)/ in_w;
  2511.     pp_scaling_context->src_normalized_y = (float)src_rect->y / in_h;
  2512.  
  2513.     pp_static_parameter->grf1.r1_6.normalized_video_y_scaling_step = (float) src_rect->height / in_h / dst_rect->height;
  2514.  
  2515.     pp_inline_parameter->grf5.normalized_video_x_scaling_step = (float) (src_rect->width + src_left_edge_extend)/ in_w / (dst_rect->width + dst_left_edge_extend);
  2516.     pp_inline_parameter->grf5.block_count_x = pp_scaling_context->dest_w / 16;   /* 1 x N */
  2517.     pp_inline_parameter->grf5.number_blocks = pp_scaling_context->dest_w / 16;
  2518.  
  2519.     dst_surface->flags = src_surface->flags;
  2520.  
  2521.     return VA_STATUS_SUCCESS;
  2522. }
  2523.  
  2524. static int
  2525. pp_avs_x_steps(void *private_context)
  2526. {
  2527.     struct pp_avs_context *pp_avs_context = private_context;
  2528.  
  2529.     return pp_avs_context->dest_w / 16;
  2530. }
  2531.  
  2532. static int
  2533. pp_avs_y_steps(void *private_context)
  2534. {
  2535.     return 1;
  2536. }
  2537.  
  2538. static int
  2539. pp_avs_set_block_parameter(struct i965_post_processing_context *pp_context, int x, int y)
  2540. {
  2541.     struct pp_avs_context *pp_avs_context = (struct pp_avs_context *)pp_context->private_context;
  2542.     struct pp_inline_parameter *pp_inline_parameter = pp_context->pp_inline_parameter;
  2543.     struct pp_static_parameter *pp_static_parameter = pp_context->pp_static_parameter;
  2544.     float src_x_steping, src_y_steping, video_step_delta;
  2545.     int tmp_w = ALIGN(pp_avs_context->dest_h * pp_avs_context->src_w / pp_avs_context->src_h, 16);
  2546.  
  2547.     if (pp_static_parameter->grf4.r4_2.avs.nlas == 0) {
  2548.         src_x_steping = pp_inline_parameter->grf5.normalized_video_x_scaling_step;
  2549.         pp_inline_parameter->grf5.r5_1.source_surface_block_normalized_horizontal_origin = src_x_steping * x * 16 + pp_avs_context->src_normalized_x;
  2550.     } else if (tmp_w >= pp_avs_context->dest_w) {
  2551.         pp_inline_parameter->grf5.normalized_video_x_scaling_step = 1.0 / tmp_w;
  2552.         pp_inline_parameter->grf6.video_step_delta = 0;
  2553.        
  2554.         if (x == 0) {
  2555.             pp_inline_parameter->grf5.r5_1.source_surface_block_normalized_horizontal_origin = (float)(tmp_w - pp_avs_context->dest_w) / tmp_w / 2 +
  2556.                 pp_avs_context->src_normalized_x;
  2557.         } else {
  2558.             src_x_steping = pp_inline_parameter->grf5.normalized_video_x_scaling_step;
  2559.             video_step_delta = pp_inline_parameter->grf6.video_step_delta;
  2560.             pp_inline_parameter->grf5.r5_1.source_surface_block_normalized_horizontal_origin += src_x_steping * 16 +
  2561.                 16 * 15 * video_step_delta / 2;
  2562.         }
  2563.     } else {
  2564.         int n0, n1, n2, nls_left, nls_right;
  2565.         int factor_a = 5, factor_b = 4;
  2566.         float f;
  2567.  
  2568.         n0 = (pp_avs_context->dest_w - tmp_w) / (16 * 2);
  2569.         n1 = (pp_avs_context->dest_w - tmp_w) / 16 - n0;
  2570.         n2 = tmp_w / (16 * factor_a);
  2571.         nls_left = n0 + n2;
  2572.         nls_right = n1 + n2;
  2573.         f = (float) n2 * 16 / tmp_w;
  2574.        
  2575.         if (n0 < 5) {
  2576.             pp_inline_parameter->grf6.video_step_delta = 0.0;
  2577.  
  2578.             if (x == 0) {
  2579.                 pp_inline_parameter->grf5.normalized_video_x_scaling_step = 1.0 / pp_avs_context->dest_w;
  2580.                 pp_inline_parameter->grf5.r5_1.source_surface_block_normalized_horizontal_origin = pp_avs_context->src_normalized_x;
  2581.             } else {
  2582.                 src_x_steping = pp_inline_parameter->grf5.normalized_video_x_scaling_step;
  2583.                 video_step_delta = pp_inline_parameter->grf6.video_step_delta;
  2584.                 pp_inline_parameter->grf5.r5_1.source_surface_block_normalized_horizontal_origin += src_x_steping * 16 +
  2585.                     16 * 15 * video_step_delta / 2;
  2586.             }
  2587.         } else {
  2588.             if (x < nls_left) {
  2589.                 /* f = a * nls_left * 16 + b * nls_left * 16 * (nls_left * 16 - 1) / 2 */
  2590.                 float a = f / (nls_left * 16 * factor_b);
  2591.                 float b = (f - nls_left * 16 * a) * 2 / (nls_left * 16 * (nls_left * 16 - 1));
  2592.                
  2593.                 pp_inline_parameter->grf6.video_step_delta = b;
  2594.  
  2595.                 if (x == 0) {
  2596.                     pp_inline_parameter->grf5.r5_1.source_surface_block_normalized_horizontal_origin = pp_avs_context->src_normalized_x;
  2597.                     pp_inline_parameter->grf5.normalized_video_x_scaling_step = a;
  2598.                 } else {
  2599.                     src_x_steping = pp_inline_parameter->grf5.normalized_video_x_scaling_step;
  2600.                     video_step_delta = pp_inline_parameter->grf6.video_step_delta;
  2601.                     pp_inline_parameter->grf5.r5_1.source_surface_block_normalized_horizontal_origin += src_x_steping * 16 +
  2602.                         16 * 15 * video_step_delta / 2;
  2603.                     pp_inline_parameter->grf5.normalized_video_x_scaling_step += 16 * b;
  2604.                 }
  2605.             } else if (x < (pp_avs_context->dest_w / 16 - nls_right)) {
  2606.                 /* scale the center linearly */
  2607.                 src_x_steping = pp_inline_parameter->grf5.normalized_video_x_scaling_step;
  2608.                 video_step_delta = pp_inline_parameter->grf6.video_step_delta;
  2609.                 pp_inline_parameter->grf5.r5_1.source_surface_block_normalized_horizontal_origin += src_x_steping * 16 +
  2610.                     16 * 15 * video_step_delta / 2;
  2611.                 pp_inline_parameter->grf6.video_step_delta = 0.0;
  2612.                 pp_inline_parameter->grf5.normalized_video_x_scaling_step = 1.0 / tmp_w;
  2613.             } else {
  2614.                 float a = f / (nls_right * 16 * factor_b);
  2615.                 float b = (f - nls_right * 16 * a) * 2 / (nls_right * 16 * (nls_right * 16 - 1));
  2616.  
  2617.                 src_x_steping = pp_inline_parameter->grf5.normalized_video_x_scaling_step;
  2618.                 video_step_delta = pp_inline_parameter->grf6.video_step_delta;
  2619.                 pp_inline_parameter->grf5.r5_1.source_surface_block_normalized_horizontal_origin += src_x_steping * 16 +
  2620.                     16 * 15 * video_step_delta / 2;
  2621.                 pp_inline_parameter->grf6.video_step_delta = -b;
  2622.  
  2623.                 if (x == (pp_avs_context->dest_w / 16 - nls_right))
  2624.                     pp_inline_parameter->grf5.normalized_video_x_scaling_step = a + (nls_right * 16  - 1) * b;
  2625.                 else
  2626.                     pp_inline_parameter->grf5.normalized_video_x_scaling_step -= b * 16;
  2627.             }
  2628.         }
  2629.     }
  2630.  
  2631.     src_y_steping = pp_static_parameter->grf1.r1_6.normalized_video_y_scaling_step;
  2632.     pp_inline_parameter->grf5.source_surface_block_normalized_vertical_origin = src_y_steping * y * 8 + pp_avs_context->src_normalized_y;
  2633.     pp_inline_parameter->grf5.destination_block_horizontal_origin = x * 16 + pp_avs_context->dest_x;
  2634.     pp_inline_parameter->grf5.destination_block_vertical_origin = y * 8 + pp_avs_context->dest_y;
  2635.  
  2636.     return 0;
  2637. }
  2638.  
  2639. static const AVSConfig gen5_avs_config = {
  2640.     .coeff_frac_bits = 6,
  2641.     .coeff_epsilon = 1.0f / (1U << 6),
  2642.     .num_phases = 16,
  2643.     .num_luma_coeffs = 8,
  2644.     .num_chroma_coeffs = 4,
  2645.  
  2646.     .coeff_range = {
  2647.         .lower_bound = {
  2648.             .y_k_h = { -0.25f, -0.5f, -1, 0, 0, -1, -0.5f, -0.25f },
  2649.             .y_k_v = { -0.25f, -0.5f, -1, 0, 0, -1, -0.5f, -0.25f },
  2650.             .uv_k_h = { -1, 0, 0, -1 },
  2651.             .uv_k_v = { -1, 0, 0, -1 },
  2652.         },
  2653.         .upper_bound = {
  2654.             .y_k_h = { 0.25f, 0.5f, 1, 2, 2, 1, 0.5f, 0.25f },
  2655.             .y_k_v = { 0.25f, 0.5f, 1, 2, 2, 1, 0.5f, 0.25f },
  2656.             .uv_k_h = { 1, 2, 2, 1 },
  2657.             .uv_k_v = { 1, 2, 2, 1 },
  2658.         },
  2659.     },
  2660. };
  2661.  
  2662. static const AVSConfig gen6_avs_config = {
  2663.     .coeff_frac_bits = 6,
  2664.     .coeff_epsilon = 1.0f / (1U << 6),
  2665.     .num_phases = 16,
  2666.     .num_luma_coeffs = 8,
  2667.     .num_chroma_coeffs = 4,
  2668.  
  2669.     .coeff_range = {
  2670.         .lower_bound = {
  2671.             .y_k_h = { -0.25f, -0.5f, -1, -2, -2, -1, -0.5f, -0.25f },
  2672.             .y_k_v = { -0.25f, -0.5f, -1, -2, -2, -1, -0.5f, -0.25f },
  2673.             .uv_k_h = { -1, 0, 0, -1 },
  2674.             .uv_k_v = { -1, 0, 0, -1 },
  2675.         },
  2676.         .upper_bound = {
  2677.             .y_k_h = { 0.25f, 0.5f, 1, 2, 2, 1, 0.5f, 0.25f },
  2678.             .y_k_v = { 0.25f, 0.5f, 1, 2, 2, 1, 0.5f, 0.25f },
  2679.             .uv_k_h = { 1, 2, 2, 1 },
  2680.             .uv_k_v = { 1, 2, 2, 1 },
  2681.         },
  2682.     },
  2683. };
  2684.  
  2685. static VAStatus
  2686. pp_nv12_avs_initialize(VADriverContextP ctx, struct i965_post_processing_context *pp_context,
  2687.                        const struct i965_surface *src_surface,
  2688.                        const VARectangle *src_rect,
  2689.                        struct i965_surface *dst_surface,
  2690.                        const VARectangle *dst_rect,
  2691.                        void *filter_param)
  2692. {
  2693.     struct pp_avs_context *pp_avs_context = (struct pp_avs_context *)&pp_context->pp_avs_context;
  2694.     struct pp_inline_parameter *pp_inline_parameter = pp_context->pp_inline_parameter;
  2695.     struct pp_static_parameter *pp_static_parameter = pp_context->pp_static_parameter;
  2696.     struct object_surface *obj_surface;
  2697.     struct i965_sampler_8x8 *sampler_8x8;
  2698.     struct i965_sampler_8x8_state *sampler_8x8_state;
  2699.     int index;
  2700.     int in_w, in_h, in_wpitch, in_hpitch;
  2701.     int out_w, out_h, out_wpitch, out_hpitch;
  2702.     int i;
  2703.     AVSState * const avs = &pp_avs_context->state;
  2704.     float sx, sy;
  2705.  
  2706.     const int nlas = (pp_context->filter_flags & VA_FILTER_SCALING_MASK) ==
  2707.         VA_FILTER_SCALING_NL_ANAMORPHIC;
  2708.  
  2709.     /* surface */
  2710.     obj_surface = (struct object_surface *)src_surface->base;
  2711.     in_w = obj_surface->orig_width;
  2712.     in_h = obj_surface->orig_height;
  2713.     in_wpitch = obj_surface->width;
  2714.     in_hpitch = obj_surface->height;
  2715.  
  2716.     /* source Y surface index 1 */
  2717.     i965_pp_set_surface2_state(ctx, pp_context,
  2718.                                obj_surface->bo, 0,
  2719.                                in_w, in_h, in_wpitch,
  2720.                                0, 0,
  2721.                                SURFACE_FORMAT_Y8_UNORM, 0,
  2722.                                1);
  2723.  
  2724.     /* source UV surface index 2 */
  2725.     i965_pp_set_surface2_state(ctx, pp_context,
  2726.                                obj_surface->bo, in_wpitch * in_hpitch,
  2727.                                in_w / 2, in_h / 2, in_wpitch,
  2728.                                0, 0,
  2729.                                SURFACE_FORMAT_R8B8_UNORM, 0,
  2730.                                2);
  2731.  
  2732.     /* destination surface */
  2733.     obj_surface = (struct object_surface *)dst_surface->base;
  2734.     out_w = obj_surface->orig_width;
  2735.     out_h = obj_surface->orig_height;
  2736.     out_wpitch = obj_surface->width;
  2737.     out_hpitch = obj_surface->height;
  2738.     assert(out_w <= out_wpitch && out_h <= out_hpitch);
  2739.  
  2740.     /* destination Y surface index 7 */
  2741.     i965_pp_set_surface_state(ctx, pp_context,
  2742.                               obj_surface->bo, 0,
  2743.                               ALIGN(out_w, 4) / 4, out_h, out_wpitch, I965_SURFACEFORMAT_R8_UNORM,
  2744.                               7, 1);
  2745.  
  2746.     /* destination UV surface index 8 */
  2747.     i965_pp_set_surface_state(ctx, pp_context,
  2748.                               obj_surface->bo, out_wpitch * out_hpitch,
  2749.                               ALIGN(out_w, 4) / 4, out_h / 2, out_wpitch, I965_SURFACEFORMAT_R8G8_UNORM,
  2750.                               8, 1);
  2751.  
  2752.     /* sampler 8x8 state */
  2753.     dri_bo_map(pp_context->sampler_state_table.bo_8x8, True);
  2754.     assert(pp_context->sampler_state_table.bo_8x8->virtual);
  2755.     assert(sizeof(*sampler_8x8_state) == sizeof(int) * 138);
  2756.     sampler_8x8_state = pp_context->sampler_state_table.bo_8x8->virtual;
  2757.     memset(sampler_8x8_state, 0, sizeof(*sampler_8x8_state));
  2758.  
  2759.     sx = (float)dst_rect->width / src_rect->width;
  2760.     sy = (float)dst_rect->height / src_rect->height;
  2761.     avs_update_coefficients(avs, sx, sy, pp_context->filter_flags);
  2762.  
  2763.     assert(avs->config->num_phases == 16);
  2764.     for (i = 0; i <= 16; i++) {
  2765.         const AVSCoeffs * const coeffs = &avs->coeffs[i];
  2766.  
  2767.         sampler_8x8_state->coefficients[i].dw0.table_0x_filter_c0 =
  2768.             intel_format_convert(coeffs->y_k_h[0], 1, 6, 1);
  2769.         sampler_8x8_state->coefficients[i].dw0.table_0x_filter_c1 =
  2770.             intel_format_convert(coeffs->y_k_h[1], 1, 6, 1);
  2771.         sampler_8x8_state->coefficients[i].dw0.table_0x_filter_c2 =
  2772.             intel_format_convert(coeffs->y_k_h[2], 1, 6, 1);
  2773.         sampler_8x8_state->coefficients[i].dw0.table_0x_filter_c3 =
  2774.             intel_format_convert(coeffs->y_k_h[3], 1, 6, 1);
  2775.         sampler_8x8_state->coefficients[i].dw1.table_0x_filter_c4 =
  2776.             intel_format_convert(coeffs->y_k_h[4], 1, 6, 1);
  2777.         sampler_8x8_state->coefficients[i].dw1.table_0x_filter_c5 =
  2778.             intel_format_convert(coeffs->y_k_h[5], 1, 6, 1);
  2779.         sampler_8x8_state->coefficients[i].dw1.table_0x_filter_c6 =
  2780.             intel_format_convert(coeffs->y_k_h[6], 1, 6, 1);
  2781.         sampler_8x8_state->coefficients[i].dw1.table_0x_filter_c7 =
  2782.             intel_format_convert(coeffs->y_k_h[7], 1, 6, 1);
  2783.  
  2784.         sampler_8x8_state->coefficients[i].dw4.table_1x_filter_c2 =
  2785.             intel_format_convert(coeffs->uv_k_h[0], 1, 6, 1);
  2786.         sampler_8x8_state->coefficients[i].dw4.table_1x_filter_c3 =
  2787.             intel_format_convert(coeffs->uv_k_h[1], 1, 6, 1);
  2788.         sampler_8x8_state->coefficients[i].dw5.table_1x_filter_c4 =
  2789.             intel_format_convert(coeffs->uv_k_h[2], 1, 6, 1);
  2790.         sampler_8x8_state->coefficients[i].dw5.table_1x_filter_c5 =
  2791.             intel_format_convert(coeffs->uv_k_h[3], 1, 6, 1);
  2792.  
  2793.         sampler_8x8_state->coefficients[i].dw2.table_0y_filter_c0 =
  2794.             intel_format_convert(coeffs->y_k_v[0], 1, 6, 1);
  2795.         sampler_8x8_state->coefficients[i].dw2.table_0y_filter_c1 =
  2796.             intel_format_convert(coeffs->y_k_v[1], 1, 6, 1);
  2797.         sampler_8x8_state->coefficients[i].dw2.table_0y_filter_c2 =
  2798.             intel_format_convert(coeffs->y_k_v[2], 1, 6, 1);
  2799.         sampler_8x8_state->coefficients[i].dw2.table_0y_filter_c3 =
  2800.             intel_format_convert(coeffs->y_k_v[3], 1, 6, 1);
  2801.         sampler_8x8_state->coefficients[i].dw3.table_0y_filter_c4 =
  2802.             intel_format_convert(coeffs->y_k_v[4], 1, 6, 1);
  2803.         sampler_8x8_state->coefficients[i].dw3.table_0y_filter_c5 =
  2804.             intel_format_convert(coeffs->y_k_v[5], 1, 6, 1);
  2805.         sampler_8x8_state->coefficients[i].dw3.table_0y_filter_c6 =
  2806.             intel_format_convert(coeffs->y_k_v[6], 1, 6, 1);
  2807.         sampler_8x8_state->coefficients[i].dw3.table_0y_filter_c7 =
  2808.             intel_format_convert(coeffs->y_k_v[7], 1, 6, 1);
  2809.  
  2810.         sampler_8x8_state->coefficients[i].dw6.table_1y_filter_c2 =
  2811.             intel_format_convert(coeffs->uv_k_v[0], 1, 6, 1);
  2812.         sampler_8x8_state->coefficients[i].dw6.table_1y_filter_c3 =
  2813.             intel_format_convert(coeffs->uv_k_v[1], 1, 6, 1);
  2814.         sampler_8x8_state->coefficients[i].dw7.table_1y_filter_c4 =
  2815.             intel_format_convert(coeffs->uv_k_v[2], 1, 6, 1);
  2816.         sampler_8x8_state->coefficients[i].dw7.table_1y_filter_c5 =
  2817.             intel_format_convert(coeffs->uv_k_v[3], 1, 6, 1);
  2818.     }
  2819.  
  2820.     /* Adaptive filter for all channels (DW4.15) */
  2821.     sampler_8x8_state->coefficients[0].dw4.table_1x_filter_c1 = 1U << 7;
  2822.  
  2823.     sampler_8x8_state->dw136.default_sharpness_level =
  2824.         -avs_is_needed(pp_context->filter_flags);
  2825.     sampler_8x8_state->dw137.ilk.bypass_y_adaptive_filtering = 1;
  2826.     sampler_8x8_state->dw137.ilk.bypass_x_adaptive_filtering = 1;
  2827.     dri_bo_unmap(pp_context->sampler_state_table.bo_8x8);
  2828.  
  2829.     /* sampler 8x8 */
  2830.     dri_bo_map(pp_context->sampler_state_table.bo, True);
  2831.     assert(pp_context->sampler_state_table.bo->virtual);
  2832.     assert(sizeof(*sampler_8x8) == sizeof(int) * 16);
  2833.     sampler_8x8 = pp_context->sampler_state_table.bo->virtual;
  2834.  
  2835.     /* sample_8x8 Y index 1 */
  2836.     index = 1;
  2837.     memset(&sampler_8x8[index], 0, sizeof(*sampler_8x8));
  2838.     sampler_8x8[index].dw0.avs_filter_type = AVS_FILTER_ADAPTIVE_8_TAP;
  2839.     sampler_8x8[index].dw0.ief_bypass = 1;
  2840.     sampler_8x8[index].dw0.ief_filter_type = IEF_FILTER_DETAIL;
  2841.     sampler_8x8[index].dw0.ief_filter_size = IEF_FILTER_SIZE_5X5;
  2842.     sampler_8x8[index].dw1.sampler_8x8_state_pointer = pp_context->sampler_state_table.bo_8x8->offset >> 5;
  2843.     sampler_8x8[index].dw2.global_noise_estimation = 22;
  2844.     sampler_8x8[index].dw2.strong_edge_threshold = 8;
  2845.     sampler_8x8[index].dw2.weak_edge_threshold = 1;
  2846.     sampler_8x8[index].dw3.strong_edge_weight = 7;
  2847.     sampler_8x8[index].dw3.regular_weight = 2;
  2848.     sampler_8x8[index].dw3.non_edge_weight = 0;
  2849.     sampler_8x8[index].dw3.gain_factor = 40;
  2850.     sampler_8x8[index].dw4.steepness_boost = 0;
  2851.     sampler_8x8[index].dw4.steepness_threshold = 0;
  2852.     sampler_8x8[index].dw4.mr_boost = 0;
  2853.     sampler_8x8[index].dw4.mr_threshold = 5;
  2854.     sampler_8x8[index].dw5.pwl1_point_1 = 4;
  2855.     sampler_8x8[index].dw5.pwl1_point_2 = 12;
  2856.     sampler_8x8[index].dw5.pwl1_point_3 = 16;
  2857.     sampler_8x8[index].dw5.pwl1_point_4 = 26;
  2858.     sampler_8x8[index].dw6.pwl1_point_5 = 40;
  2859.     sampler_8x8[index].dw6.pwl1_point_6 = 160;
  2860.     sampler_8x8[index].dw6.pwl1_r3_bias_0 = 127;
  2861.     sampler_8x8[index].dw6.pwl1_r3_bias_1 = 98;
  2862.     sampler_8x8[index].dw7.pwl1_r3_bias_2 = 88;
  2863.     sampler_8x8[index].dw7.pwl1_r3_bias_3 = 64;
  2864.     sampler_8x8[index].dw7.pwl1_r3_bias_4 = 44;
  2865.     sampler_8x8[index].dw7.pwl1_r3_bias_5 = 0;
  2866.     sampler_8x8[index].dw8.pwl1_r3_bias_6 = 0;
  2867.     sampler_8x8[index].dw8.pwl1_r5_bias_0 = 3;
  2868.     sampler_8x8[index].dw8.pwl1_r5_bias_1 = 32;
  2869.     sampler_8x8[index].dw8.pwl1_r5_bias_2 = 32;
  2870.     sampler_8x8[index].dw9.pwl1_r5_bias_3 = 58;
  2871.     sampler_8x8[index].dw9.pwl1_r5_bias_4 = 100;
  2872.     sampler_8x8[index].dw9.pwl1_r5_bias_5 = 108;
  2873.     sampler_8x8[index].dw9.pwl1_r5_bias_6 = 88;
  2874.     sampler_8x8[index].dw10.pwl1_r3_slope_0 = -116;
  2875.     sampler_8x8[index].dw10.pwl1_r3_slope_1 = -20;
  2876.     sampler_8x8[index].dw10.pwl1_r3_slope_2 = -96;
  2877.     sampler_8x8[index].dw10.pwl1_r3_slope_3 = -32;
  2878.     sampler_8x8[index].dw11.pwl1_r3_slope_4 = -50;
  2879.     sampler_8x8[index].dw11.pwl1_r3_slope_5 = 0;
  2880.     sampler_8x8[index].dw11.pwl1_r3_slope_6 = 0;
  2881.     sampler_8x8[index].dw11.pwl1_r5_slope_0 = 116;
  2882.     sampler_8x8[index].dw12.pwl1_r5_slope_1 = 0;
  2883.     sampler_8x8[index].dw12.pwl1_r5_slope_2 = 114;
  2884.     sampler_8x8[index].dw12.pwl1_r5_slope_3 = 67;
  2885.     sampler_8x8[index].dw12.pwl1_r5_slope_4 = 9;
  2886.     sampler_8x8[index].dw13.pwl1_r5_slope_5 = -3;
  2887.     sampler_8x8[index].dw13.pwl1_r5_slope_6 = -15;
  2888.     sampler_8x8[index].dw13.limiter_boost = 0;
  2889.     sampler_8x8[index].dw13.minimum_limiter = 10;
  2890.     sampler_8x8[index].dw13.maximum_limiter = 11;
  2891.     sampler_8x8[index].dw14.clip_limiter = 130;
  2892.     dri_bo_emit_reloc(pp_context->sampler_state_table.bo,
  2893.                       I915_GEM_DOMAIN_RENDER,
  2894.                       0,
  2895.                       0,
  2896.                       sizeof(*sampler_8x8) * index + offsetof(struct i965_sampler_8x8, dw1),
  2897.                       pp_context->sampler_state_table.bo_8x8);
  2898.  
  2899.     /* sample_8x8 UV index 2 */
  2900.     index = 2;
  2901.     memset(&sampler_8x8[index], 0, sizeof(*sampler_8x8));
  2902.     sampler_8x8[index].dw0.avs_filter_type = AVS_FILTER_ADAPTIVE_8_TAP;
  2903.     sampler_8x8[index].dw0.ief_bypass = 1;
  2904.     sampler_8x8[index].dw0.ief_filter_type = IEF_FILTER_DETAIL;
  2905.     sampler_8x8[index].dw0.ief_filter_size = IEF_FILTER_SIZE_5X5;
  2906.     sampler_8x8[index].dw1.sampler_8x8_state_pointer = pp_context->sampler_state_table.bo_8x8->offset >> 5;
  2907.     sampler_8x8[index].dw2.global_noise_estimation = 22;
  2908.     sampler_8x8[index].dw2.strong_edge_threshold = 8;
  2909.     sampler_8x8[index].dw2.weak_edge_threshold = 1;
  2910.     sampler_8x8[index].dw3.strong_edge_weight = 7;
  2911.     sampler_8x8[index].dw3.regular_weight = 2;
  2912.     sampler_8x8[index].dw3.non_edge_weight = 0;
  2913.     sampler_8x8[index].dw3.gain_factor = 40;
  2914.     sampler_8x8[index].dw4.steepness_boost = 0;
  2915.     sampler_8x8[index].dw4.steepness_threshold = 0;
  2916.     sampler_8x8[index].dw4.mr_boost = 0;
  2917.     sampler_8x8[index].dw4.mr_threshold = 5;
  2918.     sampler_8x8[index].dw5.pwl1_point_1 = 4;
  2919.     sampler_8x8[index].dw5.pwl1_point_2 = 12;
  2920.     sampler_8x8[index].dw5.pwl1_point_3 = 16;
  2921.     sampler_8x8[index].dw5.pwl1_point_4 = 26;
  2922.     sampler_8x8[index].dw6.pwl1_point_5 = 40;
  2923.     sampler_8x8[index].dw6.pwl1_point_6 = 160;
  2924.     sampler_8x8[index].dw6.pwl1_r3_bias_0 = 127;
  2925.     sampler_8x8[index].dw6.pwl1_r3_bias_1 = 98;
  2926.     sampler_8x8[index].dw7.pwl1_r3_bias_2 = 88;
  2927.     sampler_8x8[index].dw7.pwl1_r3_bias_3 = 64;
  2928.     sampler_8x8[index].dw7.pwl1_r3_bias_4 = 44;
  2929.     sampler_8x8[index].dw7.pwl1_r3_bias_5 = 0;
  2930.     sampler_8x8[index].dw8.pwl1_r3_bias_6 = 0;
  2931.     sampler_8x8[index].dw8.pwl1_r5_bias_0 = 3;
  2932.     sampler_8x8[index].dw8.pwl1_r5_bias_1 = 32;
  2933.     sampler_8x8[index].dw8.pwl1_r5_bias_2 = 32;
  2934.     sampler_8x8[index].dw9.pwl1_r5_bias_3 = 58;
  2935.     sampler_8x8[index].dw9.pwl1_r5_bias_4 = 100;
  2936.     sampler_8x8[index].dw9.pwl1_r5_bias_5 = 108;
  2937.     sampler_8x8[index].dw9.pwl1_r5_bias_6 = 88;
  2938.     sampler_8x8[index].dw10.pwl1_r3_slope_0 = -116;
  2939.     sampler_8x8[index].dw10.pwl1_r3_slope_1 = -20;
  2940.     sampler_8x8[index].dw10.pwl1_r3_slope_2 = -96;
  2941.     sampler_8x8[index].dw10.pwl1_r3_slope_3 = -32;
  2942.     sampler_8x8[index].dw11.pwl1_r3_slope_4 = -50;
  2943.     sampler_8x8[index].dw11.pwl1_r3_slope_5 = 0;
  2944.     sampler_8x8[index].dw11.pwl1_r3_slope_6 = 0;
  2945.     sampler_8x8[index].dw11.pwl1_r5_slope_0 = 116;
  2946.     sampler_8x8[index].dw12.pwl1_r5_slope_1 = 0;
  2947.     sampler_8x8[index].dw12.pwl1_r5_slope_2 = 114;
  2948.     sampler_8x8[index].dw12.pwl1_r5_slope_3 = 67;
  2949.     sampler_8x8[index].dw12.pwl1_r5_slope_4 = 9;
  2950.     sampler_8x8[index].dw13.pwl1_r5_slope_5 = -3;
  2951.     sampler_8x8[index].dw13.pwl1_r5_slope_6 = -15;
  2952.     sampler_8x8[index].dw13.limiter_boost = 0;
  2953.     sampler_8x8[index].dw13.minimum_limiter = 10;
  2954.     sampler_8x8[index].dw13.maximum_limiter = 11;
  2955.     sampler_8x8[index].dw14.clip_limiter = 130;
  2956.     dri_bo_emit_reloc(pp_context->sampler_state_table.bo,
  2957.                       I915_GEM_DOMAIN_RENDER,
  2958.                       0,
  2959.                       0,
  2960.                       sizeof(*sampler_8x8) * index + offsetof(struct i965_sampler_8x8, dw1),
  2961.                       pp_context->sampler_state_table.bo_8x8);
  2962.  
  2963.     dri_bo_unmap(pp_context->sampler_state_table.bo);
  2964.  
  2965.     /* private function & data */
  2966.     pp_context->pp_x_steps = pp_avs_x_steps;
  2967.     pp_context->pp_y_steps = pp_avs_y_steps;
  2968.     pp_context->private_context = &pp_context->pp_avs_context;
  2969.     pp_context->pp_set_block_parameter = pp_avs_set_block_parameter;
  2970.  
  2971.     int dst_left_edge_extend = dst_rect->x%GPU_ASM_X_OFFSET_ALIGNMENT;
  2972.     float src_left_edge_extend = (float)dst_left_edge_extend*src_rect->width/dst_rect->width;
  2973.     pp_avs_context->dest_x = dst_rect->x - dst_left_edge_extend;
  2974.     pp_avs_context->dest_y = dst_rect->y;
  2975.     pp_avs_context->dest_w = ALIGN(dst_rect->width + dst_left_edge_extend, 16);
  2976.     pp_avs_context->dest_h = ALIGN(dst_rect->height, 8);
  2977.     pp_avs_context->src_normalized_x = (float)(src_rect->x - src_left_edge_extend)/ in_w;
  2978.     pp_avs_context->src_normalized_y = (float)src_rect->y / in_h;
  2979.     pp_avs_context->src_w = src_rect->width + src_left_edge_extend;
  2980.     pp_avs_context->src_h = src_rect->height;
  2981.  
  2982.     pp_static_parameter->grf4.r4_2.avs.nlas = nlas;
  2983.     pp_static_parameter->grf1.r1_6.normalized_video_y_scaling_step = (float) src_rect->height / in_h / dst_rect->height;
  2984.  
  2985.     pp_inline_parameter->grf5.normalized_video_x_scaling_step = (float) (src_rect->width + src_left_edge_extend)/ in_w / (dst_rect->width + dst_left_edge_extend);
  2986.     pp_inline_parameter->grf5.block_count_x = 1;        /* M x 1 */
  2987.     pp_inline_parameter->grf5.number_blocks = pp_avs_context->dest_h / 8;
  2988.     pp_inline_parameter->grf6.video_step_delta = 0.0;
  2989.  
  2990.     dst_surface->flags = src_surface->flags;
  2991.  
  2992.     return VA_STATUS_SUCCESS;
  2993. }
  2994.  
  2995. static VAStatus
  2996. gen6_nv12_scaling_initialize(VADriverContextP ctx, struct i965_post_processing_context *pp_context,
  2997.                              const struct i965_surface *src_surface,
  2998.                              const VARectangle *src_rect,
  2999.                              struct i965_surface *dst_surface,
  3000.                              const VARectangle *dst_rect,
  3001.                              void *filter_param)
  3002. {
  3003.     return pp_nv12_avs_initialize(ctx, pp_context,
  3004.                                   src_surface,
  3005.                                   src_rect,
  3006.                                   dst_surface,
  3007.                                   dst_rect,
  3008.                                   filter_param);
  3009. }
  3010.  
  3011. static int
  3012. gen7_pp_avs_x_steps(void *private_context)
  3013. {
  3014.     struct pp_avs_context *pp_avs_context = private_context;
  3015.  
  3016.     return pp_avs_context->dest_w / 16;
  3017. }
  3018.  
  3019. static int
  3020. gen7_pp_avs_y_steps(void *private_context)
  3021. {
  3022.     struct pp_avs_context *pp_avs_context = private_context;
  3023.  
  3024.     return pp_avs_context->dest_h / 16;
  3025. }
  3026.  
  3027. static int
  3028. gen7_pp_avs_set_block_parameter(struct i965_post_processing_context *pp_context, int x, int y)
  3029. {
  3030.     struct pp_avs_context *pp_avs_context = (struct pp_avs_context *)pp_context->private_context;
  3031.     struct gen7_pp_inline_parameter *pp_inline_parameter = pp_context->pp_inline_parameter;
  3032.  
  3033.     pp_inline_parameter->grf9.destination_block_horizontal_origin = x * 16 + pp_avs_context->dest_x;
  3034.     pp_inline_parameter->grf9.destination_block_vertical_origin = y * 16 + pp_avs_context->dest_y;
  3035.     pp_inline_parameter->grf9.constant_0 = 0xffffffff;
  3036.     pp_inline_parameter->grf9.sampler_load_main_video_x_scaling_step = pp_avs_context->horiz_range / pp_avs_context->src_w;
  3037.  
  3038.     return 0;
  3039. }
  3040.  
  3041. static void gen7_update_src_surface_uv_offset(VADriverContextP    ctx,
  3042.                                               struct i965_post_processing_context *pp_context,
  3043.                                               const struct i965_surface *surface)
  3044. {
  3045.     struct gen7_pp_static_parameter *pp_static_parameter = pp_context->pp_static_parameter;
  3046.     int fourcc = pp_get_surface_fourcc(ctx, surface);
  3047.    
  3048.     if (fourcc == VA_FOURCC_YUY2) {
  3049.         pp_static_parameter->grf2.di_destination_packed_y_component_offset = 0;
  3050.         pp_static_parameter->grf2.di_destination_packed_u_component_offset = 1;
  3051.         pp_static_parameter->grf2.di_destination_packed_v_component_offset = 3;
  3052.     } else if (fourcc == VA_FOURCC_UYVY) {
  3053.         pp_static_parameter->grf2.di_destination_packed_y_component_offset = 1;
  3054.         pp_static_parameter->grf2.di_destination_packed_u_component_offset = 0;
  3055.         pp_static_parameter->grf2.di_destination_packed_v_component_offset = 2;
  3056.     }
  3057. }
  3058.  
  3059. static VAStatus
  3060. gen7_pp_plx_avs_initialize(VADriverContextP ctx, struct i965_post_processing_context *pp_context,
  3061.                            const struct i965_surface *src_surface,
  3062.                            const VARectangle *src_rect,
  3063.                            struct i965_surface *dst_surface,
  3064.                            const VARectangle *dst_rect,
  3065.                            void *filter_param)
  3066. {
  3067.     struct pp_avs_context *pp_avs_context = (struct pp_avs_context *)&pp_context->pp_avs_context;
  3068.     struct i965_driver_data *i965 = i965_driver_data(ctx);
  3069.     struct gen7_pp_static_parameter *pp_static_parameter = pp_context->pp_static_parameter;
  3070.     struct gen7_sampler_8x8 *sampler_8x8;
  3071.     struct i965_sampler_8x8_state *sampler_8x8_state;
  3072.     int index, i;
  3073.     int width[3], height[3], pitch[3], offset[3];
  3074.     int src_width, src_height;
  3075.     AVSState * const avs = &pp_avs_context->state;
  3076.     float sx, sy;
  3077.     const float * yuv_to_rgb_coefs;
  3078.     size_t yuv_to_rgb_coefs_size;
  3079.  
  3080.     /* source surface */
  3081.     gen7_pp_set_media_rw_message_surface(ctx, pp_context, src_surface, 0, 0,
  3082.                                          src_rect,
  3083.                                          width, height, pitch, offset);
  3084.     src_width = width[0];
  3085.     src_height = height[0];
  3086.  
  3087.     /* destination surface */
  3088.     gen7_pp_set_media_rw_message_surface(ctx, pp_context, dst_surface, 24, 1,
  3089.                                          dst_rect,
  3090.                                          width, height, pitch, offset);
  3091.  
  3092.     /* sampler 8x8 state */
  3093.     dri_bo_map(pp_context->sampler_state_table.bo_8x8, True);
  3094.     assert(pp_context->sampler_state_table.bo_8x8->virtual);
  3095.     assert(sizeof(*sampler_8x8_state) == sizeof(int) * 138);
  3096.     sampler_8x8_state = pp_context->sampler_state_table.bo_8x8->virtual;
  3097.     memset(sampler_8x8_state, 0, sizeof(*sampler_8x8_state));
  3098.  
  3099.     sx = (float)dst_rect->width / src_rect->width;
  3100.     sy = (float)dst_rect->height / src_rect->height;
  3101.     avs_update_coefficients(avs, sx, sy, pp_context->filter_flags);
  3102.  
  3103.     assert(avs->config->num_phases == 16);
  3104.     for (i = 0; i <= 16; i++) {
  3105.         const AVSCoeffs * const coeffs = &avs->coeffs[i];
  3106.  
  3107.         sampler_8x8_state->coefficients[i].dw0.table_0x_filter_c0 =
  3108.             intel_format_convert(coeffs->y_k_h[0], 1, 6, 1);
  3109.         sampler_8x8_state->coefficients[i].dw0.table_0x_filter_c1 =
  3110.             intel_format_convert(coeffs->y_k_h[1], 1, 6, 1);
  3111.         sampler_8x8_state->coefficients[i].dw0.table_0x_filter_c2 =
  3112.             intel_format_convert(coeffs->y_k_h[2], 1, 6, 1);
  3113.         sampler_8x8_state->coefficients[i].dw0.table_0x_filter_c3 =
  3114.             intel_format_convert(coeffs->y_k_h[3], 1, 6, 1);
  3115.         sampler_8x8_state->coefficients[i].dw1.table_0x_filter_c4 =
  3116.             intel_format_convert(coeffs->y_k_h[4], 1, 6, 1);
  3117.         sampler_8x8_state->coefficients[i].dw1.table_0x_filter_c5 =
  3118.             intel_format_convert(coeffs->y_k_h[5], 1, 6, 1);
  3119.         sampler_8x8_state->coefficients[i].dw1.table_0x_filter_c6 =
  3120.             intel_format_convert(coeffs->y_k_h[6], 1, 6, 1);
  3121.         sampler_8x8_state->coefficients[i].dw1.table_0x_filter_c7 =
  3122.             intel_format_convert(coeffs->y_k_h[7], 1, 6, 1);
  3123.  
  3124.         sampler_8x8_state->coefficients[i].dw4.table_1x_filter_c2 =
  3125.             intel_format_convert(coeffs->uv_k_h[0], 1, 6, 1);
  3126.         sampler_8x8_state->coefficients[i].dw4.table_1x_filter_c3 =
  3127.             intel_format_convert(coeffs->uv_k_h[1], 1, 6, 1);
  3128.         sampler_8x8_state->coefficients[i].dw5.table_1x_filter_c4 =
  3129.             intel_format_convert(coeffs->uv_k_h[2], 1, 6, 1);
  3130.         sampler_8x8_state->coefficients[i].dw5.table_1x_filter_c5 =
  3131.             intel_format_convert(coeffs->uv_k_h[3], 1, 6, 1);
  3132.  
  3133.         sampler_8x8_state->coefficients[i].dw2.table_0y_filter_c0 =
  3134.             intel_format_convert(coeffs->y_k_v[0], 1, 6, 1);
  3135.         sampler_8x8_state->coefficients[i].dw2.table_0y_filter_c1 =
  3136.             intel_format_convert(coeffs->y_k_v[1], 1, 6, 1);
  3137.         sampler_8x8_state->coefficients[i].dw2.table_0y_filter_c2 =
  3138.             intel_format_convert(coeffs->y_k_v[2], 1, 6, 1);
  3139.         sampler_8x8_state->coefficients[i].dw2.table_0y_filter_c3 =
  3140.             intel_format_convert(coeffs->y_k_v[3], 1, 6, 1);
  3141.         sampler_8x8_state->coefficients[i].dw3.table_0y_filter_c4 =
  3142.             intel_format_convert(coeffs->y_k_v[4], 1, 6, 1);
  3143.         sampler_8x8_state->coefficients[i].dw3.table_0y_filter_c5 =
  3144.             intel_format_convert(coeffs->y_k_v[5], 1, 6, 1);
  3145.         sampler_8x8_state->coefficients[i].dw3.table_0y_filter_c6 =
  3146.             intel_format_convert(coeffs->y_k_v[6], 1, 6, 1);
  3147.         sampler_8x8_state->coefficients[i].dw3.table_0y_filter_c7 =
  3148.             intel_format_convert(coeffs->y_k_v[7], 1, 6, 1);
  3149.  
  3150.         sampler_8x8_state->coefficients[i].dw6.table_1y_filter_c2 =
  3151.             intel_format_convert(coeffs->uv_k_v[0], 1, 6, 1);
  3152.         sampler_8x8_state->coefficients[i].dw6.table_1y_filter_c3 =
  3153.             intel_format_convert(coeffs->uv_k_v[1], 1, 6, 1);
  3154.         sampler_8x8_state->coefficients[i].dw7.table_1y_filter_c4 =
  3155.             intel_format_convert(coeffs->uv_k_v[2], 1, 6, 1);
  3156.         sampler_8x8_state->coefficients[i].dw7.table_1y_filter_c5 =
  3157.             intel_format_convert(coeffs->uv_k_v[3], 1, 6, 1);
  3158.     }
  3159.  
  3160.     sampler_8x8_state->dw136.default_sharpness_level =
  3161.         -avs_is_needed(pp_context->filter_flags);
  3162.     if (IS_HASWELL(i965->intel.device_info)) {
  3163.         sampler_8x8_state->dw137.hsw.adaptive_filter_for_all_channel = 1;
  3164.         sampler_8x8_state->dw137.hsw.bypass_y_adaptive_filtering = 1;
  3165.         sampler_8x8_state->dw137.hsw.bypass_x_adaptive_filtering = 1;
  3166.     }
  3167.     else {
  3168.         sampler_8x8_state->coefficients[0].dw4.table_1x_filter_c1 = 1U << 7;
  3169.         sampler_8x8_state->dw137.ilk.bypass_y_adaptive_filtering = 1;
  3170.         sampler_8x8_state->dw137.ilk.bypass_x_adaptive_filtering = 1;
  3171.     }
  3172.     dri_bo_unmap(pp_context->sampler_state_table.bo_8x8);
  3173.  
  3174.     /* sampler 8x8 */
  3175.     dri_bo_map(pp_context->sampler_state_table.bo, True);
  3176.     assert(pp_context->sampler_state_table.bo->virtual);
  3177.     assert(sizeof(*sampler_8x8) == sizeof(int) * 4);
  3178.     sampler_8x8 = pp_context->sampler_state_table.bo->virtual;
  3179.  
  3180.     /* sample_8x8 Y index 4 */
  3181.     index = 4;
  3182.     memset(&sampler_8x8[index], 0, sizeof(*sampler_8x8));
  3183.     sampler_8x8[index].dw0.global_noise_estimation = 255;
  3184.     sampler_8x8[index].dw0.ief_bypass = 1;
  3185.  
  3186.     sampler_8x8[index].dw1.sampler_8x8_state_pointer = pp_context->sampler_state_table.bo_8x8->offset >> 5;
  3187.  
  3188.     sampler_8x8[index].dw2.weak_edge_threshold = 1;
  3189.     sampler_8x8[index].dw2.strong_edge_threshold = 8;
  3190.     sampler_8x8[index].dw2.r5x_coefficient = 9;
  3191.     sampler_8x8[index].dw2.r5cx_coefficient = 8;
  3192.     sampler_8x8[index].dw2.r5c_coefficient = 3;
  3193.  
  3194.     sampler_8x8[index].dw3.r3x_coefficient = 27;
  3195.     sampler_8x8[index].dw3.r3c_coefficient = 5;
  3196.     sampler_8x8[index].dw3.gain_factor = 40;
  3197.     sampler_8x8[index].dw3.non_edge_weight = 1;
  3198.     sampler_8x8[index].dw3.regular_weight = 2;
  3199.     sampler_8x8[index].dw3.strong_edge_weight = 7;
  3200.     sampler_8x8[index].dw3.ief4_smooth_enable = 0;
  3201.  
  3202.     dri_bo_emit_reloc(pp_context->sampler_state_table.bo,
  3203.                       I915_GEM_DOMAIN_RENDER,
  3204.                       0,
  3205.                       0,
  3206.                       sizeof(*sampler_8x8) * index + offsetof(struct i965_sampler_8x8, dw1),
  3207.                       pp_context->sampler_state_table.bo_8x8);
  3208.  
  3209.     /* sample_8x8 UV index 8 */
  3210.     index = 8;
  3211.     memset(&sampler_8x8[index], 0, sizeof(*sampler_8x8));
  3212.     sampler_8x8[index].dw0.disable_8x8_filter = 0;
  3213.     sampler_8x8[index].dw0.global_noise_estimation = 255;
  3214.     sampler_8x8[index].dw0.ief_bypass = 1;
  3215.     sampler_8x8[index].dw1.sampler_8x8_state_pointer = pp_context->sampler_state_table.bo_8x8->offset >> 5;
  3216.     sampler_8x8[index].dw2.weak_edge_threshold = 1;
  3217.     sampler_8x8[index].dw2.strong_edge_threshold = 8;
  3218.     sampler_8x8[index].dw2.r5x_coefficient = 9;
  3219.     sampler_8x8[index].dw2.r5cx_coefficient = 8;
  3220.     sampler_8x8[index].dw2.r5c_coefficient = 3;
  3221.     sampler_8x8[index].dw3.r3x_coefficient = 27;
  3222.     sampler_8x8[index].dw3.r3c_coefficient = 5;
  3223.     sampler_8x8[index].dw3.gain_factor = 40;
  3224.     sampler_8x8[index].dw3.non_edge_weight = 1;
  3225.     sampler_8x8[index].dw3.regular_weight = 2;
  3226.     sampler_8x8[index].dw3.strong_edge_weight = 7;
  3227.     sampler_8x8[index].dw3.ief4_smooth_enable = 0;
  3228.  
  3229.     dri_bo_emit_reloc(pp_context->sampler_state_table.bo,
  3230.                       I915_GEM_DOMAIN_RENDER,
  3231.                       0,
  3232.                       0,
  3233.                       sizeof(*sampler_8x8) * index + offsetof(struct i965_sampler_8x8, dw1),
  3234.                       pp_context->sampler_state_table.bo_8x8);
  3235.  
  3236.     /* sampler_8x8 V, index 12 */
  3237.     index = 12;
  3238.     memset(&sampler_8x8[index], 0, sizeof(*sampler_8x8));
  3239.     sampler_8x8[index].dw0.disable_8x8_filter = 0;
  3240.     sampler_8x8[index].dw0.global_noise_estimation = 255;
  3241.     sampler_8x8[index].dw0.ief_bypass = 1;
  3242.     sampler_8x8[index].dw1.sampler_8x8_state_pointer = pp_context->sampler_state_table.bo_8x8->offset >> 5;
  3243.     sampler_8x8[index].dw2.weak_edge_threshold = 1;
  3244.     sampler_8x8[index].dw2.strong_edge_threshold = 8;
  3245.     sampler_8x8[index].dw2.r5x_coefficient = 9;
  3246.     sampler_8x8[index].dw2.r5cx_coefficient = 8;
  3247.     sampler_8x8[index].dw2.r5c_coefficient = 3;
  3248.     sampler_8x8[index].dw3.r3x_coefficient = 27;
  3249.     sampler_8x8[index].dw3.r3c_coefficient = 5;
  3250.     sampler_8x8[index].dw3.gain_factor = 40;
  3251.     sampler_8x8[index].dw3.non_edge_weight = 1;
  3252.     sampler_8x8[index].dw3.regular_weight = 2;
  3253.     sampler_8x8[index].dw3.strong_edge_weight = 7;
  3254.     sampler_8x8[index].dw3.ief4_smooth_enable = 0;
  3255.  
  3256.     dri_bo_emit_reloc(pp_context->sampler_state_table.bo,
  3257.                       I915_GEM_DOMAIN_RENDER,
  3258.                       0,
  3259.                       0,
  3260.                       sizeof(*sampler_8x8) * index + offsetof(struct i965_sampler_8x8, dw1),
  3261.                       pp_context->sampler_state_table.bo_8x8);
  3262.  
  3263.     dri_bo_unmap(pp_context->sampler_state_table.bo);
  3264.  
  3265.     /* private function & data */
  3266.     pp_context->pp_x_steps = gen7_pp_avs_x_steps;
  3267.     pp_context->pp_y_steps = gen7_pp_avs_y_steps;
  3268.     pp_context->private_context = &pp_context->pp_avs_context;
  3269.     pp_context->pp_set_block_parameter = gen7_pp_avs_set_block_parameter;
  3270.  
  3271.     int dst_left_edge_extend = dst_rect->x%GPU_ASM_X_OFFSET_ALIGNMENT;
  3272.     pp_avs_context->dest_x = dst_rect->x - dst_left_edge_extend;
  3273.     pp_avs_context->dest_y = dst_rect->y;
  3274.     pp_avs_context->dest_w = ALIGN(dst_rect->width + dst_left_edge_extend, 16);
  3275.     pp_avs_context->dest_h = ALIGN(dst_rect->height, 16);
  3276.     pp_avs_context->src_w = src_rect->width;
  3277.     pp_avs_context->src_h = src_rect->height;
  3278.     pp_avs_context->horiz_range = (float)src_rect->width / src_width;
  3279.  
  3280.     int dw = (pp_avs_context->src_w - 1) / 16 + 1;
  3281.     dw = MAX(dw, dst_rect->width + dst_left_edge_extend);
  3282.  
  3283.     pp_static_parameter->grf1.pointer_to_inline_parameter = 7;
  3284.     pp_static_parameter->grf2.avs_wa_enable = 1; /* must be set for GEN7 */
  3285.     if (IS_HASWELL(i965->intel.device_info))
  3286.         pp_static_parameter->grf2.avs_wa_enable = 0; /* HSW don't use the WA */
  3287.  
  3288.     if (pp_static_parameter->grf2.avs_wa_enable) {
  3289.         int src_fourcc = pp_get_surface_fourcc(ctx, src_surface);
  3290.         if ((src_fourcc == VA_FOURCC_RGBA) ||
  3291.             (src_fourcc == VA_FOURCC_RGBX) ||
  3292.             (src_fourcc == VA_FOURCC_BGRA) ||
  3293.             (src_fourcc == VA_FOURCC_BGRX)) {
  3294.             pp_static_parameter->grf2.avs_wa_enable = 0;
  3295.         }
  3296.     }
  3297.        
  3298.     pp_static_parameter->grf2.avs_wa_width = src_width;
  3299.     pp_static_parameter->grf2.avs_wa_one_div_256_width = (float) 1.0 / (256 * src_width);
  3300.     pp_static_parameter->grf2.avs_wa_five_div_256_width = (float) 5.0 / (256 * src_width);
  3301.     pp_static_parameter->grf2.alpha = 255;
  3302.  
  3303.     pp_static_parameter->grf3.sampler_load_horizontal_scaling_step_ratio = (float) pp_avs_context->src_w / dw;
  3304.     pp_static_parameter->grf4.sampler_load_vertical_scaling_step = (float) src_rect->height / src_height / dst_rect->height;
  3305.     pp_static_parameter->grf5.sampler_load_vertical_frame_origin = (float) src_rect->y / src_height -
  3306.         (float) pp_avs_context->dest_y * pp_static_parameter->grf4.sampler_load_vertical_scaling_step;
  3307.     pp_static_parameter->grf6.sampler_load_horizontal_frame_origin = (float) src_rect->x / src_width -
  3308.         (float) pp_avs_context->dest_x * pp_avs_context->horiz_range / dw;
  3309.  
  3310.     gen7_update_src_surface_uv_offset(ctx, pp_context, dst_surface);
  3311.  
  3312.     yuv_to_rgb_coefs = i915_color_standard_to_coefs (i915_filter_to_color_standard (src_surface->flags &
  3313.                                                                                     VA_SRC_COLOR_MASK),
  3314.                                                      &yuv_to_rgb_coefs_size);
  3315.     memcpy(&pp_static_parameter->grf7, yuv_to_rgb_coefs, yuv_to_rgb_coefs_size);
  3316.  
  3317.     dst_surface->flags = src_surface->flags;
  3318.  
  3319.     return VA_STATUS_SUCCESS;
  3320. }
  3321.  
  3322. static int
  3323. pp_dndi_x_steps(void *private_context)
  3324. {
  3325.     return 1;
  3326. }
  3327.  
  3328. static int
  3329. pp_dndi_y_steps(void *private_context)
  3330. {
  3331.     struct pp_dndi_context *pp_dndi_context = private_context;
  3332.  
  3333.     return pp_dndi_context->dest_h / 4;
  3334. }
  3335.  
  3336. static int
  3337. pp_dndi_set_block_parameter(struct i965_post_processing_context *pp_context, int x, int y)
  3338. {
  3339.     struct pp_inline_parameter *pp_inline_parameter = pp_context->pp_inline_parameter;
  3340.  
  3341.     pp_inline_parameter->grf5.destination_block_horizontal_origin = x * 16;
  3342.     pp_inline_parameter->grf5.destination_block_vertical_origin = y * 4;
  3343.  
  3344.     return 0;
  3345. }
  3346.  
  3347. static VAStatus
  3348. pp_nv12_dndi_initialize(VADriverContextP ctx, struct i965_post_processing_context *pp_context,
  3349.                         const struct i965_surface *src_surface,
  3350.                         const VARectangle *src_rect,
  3351.                         struct i965_surface *dst_surface,
  3352.                         const VARectangle *dst_rect,
  3353.                         void *filter_param)
  3354. {
  3355.     struct pp_dndi_context * const dndi_ctx = &pp_context->pp_dndi_context;
  3356.     struct pp_inline_parameter *pp_inline_parameter = pp_context->pp_inline_parameter;
  3357.     struct pp_static_parameter *pp_static_parameter = pp_context->pp_static_parameter;
  3358.     const VAProcPipelineParameterBuffer * const pipe_params =
  3359.         pp_context->pipeline_param;
  3360.     const VAProcFilterParameterBufferDeinterlacing * const deint_params =
  3361.         filter_param;
  3362.     struct object_surface * const src_obj_surface = (struct object_surface *)
  3363.         src_surface->base;
  3364.     struct object_surface * const dst_obj_surface = (struct object_surface *)
  3365.         dst_surface->base;
  3366.     struct object_surface *obj_surface;
  3367.     struct i965_sampler_dndi *sampler_dndi;
  3368.     int index, dndi_top_first;
  3369.     int w, h, orig_w, orig_h;
  3370.     VAStatus status;
  3371.  
  3372.     status = pp_dndi_context_init_surface_params(dndi_ctx, src_obj_surface,
  3373.         pipe_params, deint_params);
  3374.     if (status != VA_STATUS_SUCCESS)
  3375.         return status;
  3376.  
  3377.     status = pp_dndi_context_ensure_surfaces(ctx, pp_context,
  3378.         src_obj_surface, dst_obj_surface);
  3379.     if (status != VA_STATUS_SUCCESS)
  3380.         return status;
  3381.  
  3382.     status = pp_dndi_context_ensure_surfaces_storage(ctx, pp_context,
  3383.         src_obj_surface, dst_obj_surface);
  3384.     if (status != VA_STATUS_SUCCESS)
  3385.         return status;
  3386.  
  3387.     /* Current input surface (index = 4) */
  3388.     obj_surface = dndi_ctx->frame_store[DNDI_FRAME_IN_CURRENT].obj_surface;
  3389.     i965_pp_set_surface2_state(ctx, pp_context, obj_surface->bo, 0,
  3390.         obj_surface->orig_width, obj_surface->orig_height, obj_surface->width,
  3391.         0, obj_surface->y_cb_offset, SURFACE_FORMAT_PLANAR_420_8, 1, 4);
  3392.  
  3393.     /* Previous input surface (index = 5) */
  3394.     obj_surface = dndi_ctx->frame_store[DNDI_FRAME_IN_PREVIOUS].obj_surface;
  3395.     i965_pp_set_surface2_state(ctx, pp_context, obj_surface->bo, 0,
  3396.         obj_surface->orig_width, obj_surface->orig_height, obj_surface->width,
  3397.         0, obj_surface->y_cb_offset, SURFACE_FORMAT_PLANAR_420_8, 1, 5);
  3398.  
  3399.     /* STMM input surface (index = 6) */
  3400.     obj_surface = dndi_ctx->frame_store[DNDI_FRAME_IN_STMM].obj_surface;
  3401.     i965_pp_set_surface_state(ctx, pp_context, obj_surface->bo, 0,
  3402.         obj_surface->orig_width, obj_surface->orig_height, obj_surface->width,
  3403.         I965_SURFACEFORMAT_R8_UNORM, 6, 1);
  3404.  
  3405.     /* Previous output surfaces (index = { 7, 8 }) */
  3406.     obj_surface = dndi_ctx->frame_store[DNDI_FRAME_OUT_PREVIOUS].obj_surface;
  3407.     w = obj_surface->width;
  3408.     h = obj_surface->height;
  3409.     orig_w = obj_surface->orig_width;
  3410.     orig_h = obj_surface->orig_height;
  3411.  
  3412.     i965_pp_set_surface_state(ctx, pp_context, obj_surface->bo, 0,
  3413.         ALIGN(orig_w, 4) / 4, orig_h, w, I965_SURFACEFORMAT_R8_UNORM, 7, 1);
  3414.     i965_pp_set_surface_state(ctx, pp_context, obj_surface->bo, w * h,
  3415.         ALIGN(orig_w, 4) / 4, orig_h / 2, w, I965_SURFACEFORMAT_R8G8_UNORM, 8, 1);
  3416.  
  3417.     /* Current output surfaces (index = { 10, 11 }) */
  3418.     obj_surface = dndi_ctx->frame_store[DNDI_FRAME_OUT_CURRENT].obj_surface;
  3419.     w = obj_surface->width;
  3420.     h = obj_surface->height;
  3421.     orig_w = obj_surface->orig_width;
  3422.     orig_h = obj_surface->orig_height;
  3423.  
  3424.     i965_pp_set_surface_state(ctx, pp_context, obj_surface->bo, 0,
  3425.         ALIGN(orig_w, 4) / 4, orig_h, w, I965_SURFACEFORMAT_R8_UNORM, 10, 1);
  3426.     i965_pp_set_surface_state(ctx, pp_context, obj_surface->bo, w * h,
  3427.         ALIGN(orig_w, 4) / 4, orig_h / 2, w, I965_SURFACEFORMAT_R8G8_UNORM, 11, 1);
  3428.  
  3429.     /* STMM output surface (index = 20) */
  3430.     obj_surface = dndi_ctx->frame_store[DNDI_FRAME_OUT_STMM].obj_surface;
  3431.     i965_pp_set_surface_state(ctx, pp_context, obj_surface->bo, 0,
  3432.         obj_surface->orig_width, obj_surface->orig_height, obj_surface->width,
  3433.         I965_SURFACEFORMAT_R8_UNORM, 20, 1);
  3434.  
  3435.     dndi_top_first = !(deint_params->flags & VA_DEINTERLACING_BOTTOM_FIELD);
  3436.  
  3437.     /* sampler dndi */
  3438.     dri_bo_map(pp_context->sampler_state_table.bo, True);
  3439.     assert(pp_context->sampler_state_table.bo->virtual);
  3440.     assert(sizeof(*sampler_dndi) == sizeof(int) * 8);
  3441.     sampler_dndi = pp_context->sampler_state_table.bo->virtual;
  3442.  
  3443.     /* sample dndi index 1 */
  3444.     index = 0;
  3445.     sampler_dndi[index].dw0.denoise_asd_threshold = 38;
  3446.     sampler_dndi[index].dw0.denoise_history_delta = 7;          // 0-15, default is 8
  3447.     sampler_dndi[index].dw0.denoise_maximum_history = 192;      // 128-240
  3448.     sampler_dndi[index].dw0.denoise_stad_threshold = 140;
  3449.  
  3450.     sampler_dndi[index].dw1.denoise_threshold_for_sum_of_complexity_measure = 38;
  3451.     sampler_dndi[index].dw1.denoise_moving_pixel_threshold = 1;
  3452.     sampler_dndi[index].dw1.stmm_c2 = 1;
  3453.     sampler_dndi[index].dw1.low_temporal_difference_threshold = 0;
  3454.     sampler_dndi[index].dw1.temporal_difference_threshold = 0;
  3455.  
  3456.     sampler_dndi[index].dw2.block_noise_estimate_noise_threshold = 20;   // 0-31
  3457.     sampler_dndi[index].dw2.block_noise_estimate_edge_threshold = 1;    // 0-15
  3458.     sampler_dndi[index].dw2.denoise_edge_threshold = 7;                 // 0-15
  3459.     sampler_dndi[index].dw2.good_neighbor_threshold = 12;                // 0-63
  3460.  
  3461.     sampler_dndi[index].dw3.maximum_stmm = 150;
  3462.     sampler_dndi[index].dw3.multipler_for_vecm = 30;
  3463.     sampler_dndi[index].dw3.blending_constant_across_time_for_small_values_of_stmm = 125;
  3464.     sampler_dndi[index].dw3.blending_constant_across_time_for_large_values_of_stmm = 64;
  3465.     sampler_dndi[index].dw3.stmm_blending_constant_select = 0;
  3466.  
  3467.     sampler_dndi[index].dw4.sdi_delta = 5;
  3468.     sampler_dndi[index].dw4.sdi_threshold = 100;
  3469.     sampler_dndi[index].dw4.stmm_output_shift = 5;                      // stmm_max - stmm_min = 2 ^ stmm_output_shift
  3470.     sampler_dndi[index].dw4.stmm_shift_up = 1;
  3471.     sampler_dndi[index].dw4.stmm_shift_down = 3;
  3472.     sampler_dndi[index].dw4.minimum_stmm = 118;
  3473.  
  3474.     sampler_dndi[index].dw5.fmd_temporal_difference_threshold = 175;
  3475.     sampler_dndi[index].dw5.sdi_fallback_mode_2_constant = 37;
  3476.     sampler_dndi[index].dw5.sdi_fallback_mode_1_t2_constant = 100;
  3477.     sampler_dndi[index].dw5.sdi_fallback_mode_1_t1_constant = 50;
  3478.  
  3479.     sampler_dndi[index].dw6.dn_enable = 1;
  3480.     sampler_dndi[index].dw6.di_enable = 1;
  3481.     sampler_dndi[index].dw6.di_partial = 0;
  3482.     sampler_dndi[index].dw6.dndi_top_first = dndi_top_first;
  3483.     sampler_dndi[index].dw6.dndi_stream_id = 0;
  3484.     sampler_dndi[index].dw6.dndi_first_frame = dndi_ctx->is_first_frame;
  3485.     sampler_dndi[index].dw6.progressive_dn = 0;
  3486.     sampler_dndi[index].dw6.fmd_tear_threshold = 2;
  3487.     sampler_dndi[index].dw6.fmd2_vertical_difference_threshold = 100;
  3488.     sampler_dndi[index].dw6.fmd1_vertical_difference_threshold = 16;
  3489.  
  3490.     sampler_dndi[index].dw7.fmd_for_1st_field_of_current_frame = 0;
  3491.     sampler_dndi[index].dw7.fmd_for_2nd_field_of_previous_frame = 0;
  3492.     sampler_dndi[index].dw7.vdi_walker_enable = 0;
  3493.     sampler_dndi[index].dw7.column_width_minus1 = w / 16;
  3494.  
  3495.     dri_bo_unmap(pp_context->sampler_state_table.bo);
  3496.  
  3497.     /* private function & data */
  3498.     pp_context->pp_x_steps = pp_dndi_x_steps;
  3499.     pp_context->pp_y_steps = pp_dndi_y_steps;
  3500.     pp_context->private_context = dndi_ctx;
  3501.     pp_context->pp_set_block_parameter = pp_dndi_set_block_parameter;
  3502.  
  3503.     pp_static_parameter->grf1.statistics_surface_picth = w / 2;
  3504.     pp_static_parameter->grf1.r1_6.di.top_field_first = dndi_top_first;
  3505.     pp_static_parameter->grf4.r4_2.di.motion_history_coefficient_m2 = 0;
  3506.     pp_static_parameter->grf4.r4_2.di.motion_history_coefficient_m1 = 0;
  3507.  
  3508.     pp_inline_parameter->grf5.block_count_x = w / 16;   /* 1 x N */
  3509.     pp_inline_parameter->grf5.number_blocks = w / 16;
  3510.     pp_inline_parameter->grf5.block_vertical_mask = 0xff;
  3511.     pp_inline_parameter->grf5.block_horizontal_mask = 0xffff;
  3512.  
  3513.     dndi_ctx->dest_w = w;
  3514.     dndi_ctx->dest_h = h;
  3515.  
  3516.     dst_surface->flags = I965_SURFACE_FLAG_FRAME;
  3517.     return VA_STATUS_SUCCESS;
  3518. }
  3519.  
  3520. static int
  3521. pp_dn_x_steps(void *private_context)
  3522. {
  3523.     return 1;
  3524. }
  3525.  
  3526. static int
  3527. pp_dn_y_steps(void *private_context)
  3528. {
  3529.     struct pp_dn_context *pp_dn_context = private_context;
  3530.  
  3531.     return pp_dn_context->dest_h / 8;
  3532. }
  3533.  
  3534. static int
  3535. pp_dn_set_block_parameter(struct i965_post_processing_context *pp_context, int x, int y)
  3536. {
  3537.     struct pp_inline_parameter *pp_inline_parameter = pp_context->pp_inline_parameter;
  3538.  
  3539.     pp_inline_parameter->grf5.destination_block_horizontal_origin = x * 16;
  3540.     pp_inline_parameter->grf5.destination_block_vertical_origin = y * 8;
  3541.  
  3542.     return 0;
  3543. }
  3544.  
  3545. static VAStatus
  3546. pp_nv12_dn_initialize(VADriverContextP ctx, struct i965_post_processing_context *pp_context,
  3547.                       const struct i965_surface *src_surface,
  3548.                       const VARectangle *src_rect,
  3549.                       struct i965_surface *dst_surface,
  3550.                       const VARectangle *dst_rect,
  3551.                       void *filter_param)
  3552. {
  3553.     struct i965_driver_data *i965 = i965_driver_data(ctx);
  3554.     struct pp_dn_context *pp_dn_context = (struct pp_dn_context *)&pp_context->pp_dn_context;
  3555.     struct object_surface *obj_surface;
  3556.     struct i965_sampler_dndi *sampler_dndi;
  3557.     struct pp_static_parameter *pp_static_parameter = pp_context->pp_static_parameter;
  3558.     struct pp_inline_parameter *pp_inline_parameter = pp_context->pp_inline_parameter;
  3559.     VAProcFilterParameterBuffer *dn_filter_param = filter_param; /* FIXME: parameter */
  3560.     int index;
  3561.     int w, h;
  3562.     int orig_w, orig_h;
  3563.     int dn_strength = 15;
  3564.     int dndi_top_first = 1;
  3565.     int dn_progressive = 0;
  3566.  
  3567.     if (src_surface->flags == I965_SURFACE_FLAG_FRAME) {
  3568.         dndi_top_first = 1;
  3569.         dn_progressive = 1;
  3570.     } else if (src_surface->flags == I965_SURFACE_FLAG_TOP_FIELD_FIRST) {
  3571.         dndi_top_first = 1;
  3572.         dn_progressive = 0;
  3573.     } else {
  3574.         dndi_top_first = 0;
  3575.         dn_progressive = 0;
  3576.     }
  3577.  
  3578.     if (dn_filter_param) {
  3579.         float value = dn_filter_param->value;
  3580.        
  3581.         if (value > 1.0)
  3582.             value = 1.0;
  3583.        
  3584.         if (value < 0.0)
  3585.             value = 0.0;
  3586.  
  3587.         dn_strength = (int)(value * 31.0F);
  3588.     }
  3589.  
  3590.     /* surface */
  3591.     obj_surface = (struct object_surface *)src_surface->base;
  3592.     orig_w = obj_surface->orig_width;
  3593.     orig_h = obj_surface->orig_height;
  3594.     w = obj_surface->width;
  3595.     h = obj_surface->height;
  3596.  
  3597.     if (pp_dn_context->stmm_bo == NULL) {
  3598.         pp_dn_context->stmm_bo = dri_bo_alloc(i965->intel.bufmgr,
  3599.                                               "STMM surface",
  3600.                                               w * h,
  3601.                                               4096);
  3602.         assert(pp_dn_context->stmm_bo);
  3603.     }
  3604.  
  3605.     /* source UV surface index 2 */
  3606.     i965_pp_set_surface_state(ctx, pp_context,
  3607.                               obj_surface->bo, w * h,
  3608.                               ALIGN(orig_w, 4) / 4, orig_h / 2, w, I965_SURFACEFORMAT_R8G8_UNORM,
  3609.                               2, 0);
  3610.  
  3611.     /* source YUV surface index 4 */
  3612.     i965_pp_set_surface2_state(ctx, pp_context,
  3613.                                obj_surface->bo, 0,
  3614.                                orig_w, orig_h, w,
  3615.                                0, h,
  3616.                                SURFACE_FORMAT_PLANAR_420_8, 1,
  3617.                                4);
  3618.  
  3619.     /* source STMM surface index 20 */
  3620.     i965_pp_set_surface_state(ctx, pp_context,
  3621.                               pp_dn_context->stmm_bo, 0,
  3622.                               orig_w, orig_h, w, I965_SURFACEFORMAT_R8_UNORM,
  3623.                               20, 1);
  3624.  
  3625.     /* destination surface */
  3626.     obj_surface = (struct object_surface *)dst_surface->base;
  3627.     orig_w = obj_surface->orig_width;
  3628.     orig_h = obj_surface->orig_height;
  3629.     w = obj_surface->width;
  3630.     h = obj_surface->height;
  3631.  
  3632.     /* destination Y surface index 7 */
  3633.     i965_pp_set_surface_state(ctx, pp_context,
  3634.                               obj_surface->bo, 0,
  3635.                               ALIGN(orig_w, 4) / 4, orig_h, w, I965_SURFACEFORMAT_R8_UNORM,
  3636.                               7, 1);
  3637.  
  3638.     /* destination UV surface index 8 */
  3639.     i965_pp_set_surface_state(ctx, pp_context,
  3640.                               obj_surface->bo, w * h,
  3641.                               ALIGN(orig_w, 4) / 4, orig_h / 2, w, I965_SURFACEFORMAT_R8G8_UNORM,
  3642.                               8, 1);
  3643.     /* sampler dn */
  3644.     dri_bo_map(pp_context->sampler_state_table.bo, True);
  3645.     assert(pp_context->sampler_state_table.bo->virtual);
  3646.     assert(sizeof(*sampler_dndi) == sizeof(int) * 8);
  3647.     sampler_dndi = pp_context->sampler_state_table.bo->virtual;
  3648.  
  3649.     /* sample dndi index 1 */
  3650.     index = 0;
  3651.     sampler_dndi[index].dw0.denoise_asd_threshold = 0;
  3652.     sampler_dndi[index].dw0.denoise_history_delta = 8;          // 0-15, default is 8
  3653.     sampler_dndi[index].dw0.denoise_maximum_history = 128;      // 128-240
  3654.     sampler_dndi[index].dw0.denoise_stad_threshold = 0;
  3655.  
  3656.     sampler_dndi[index].dw1.denoise_threshold_for_sum_of_complexity_measure = 64;
  3657.     sampler_dndi[index].dw1.denoise_moving_pixel_threshold = 0;
  3658.     sampler_dndi[index].dw1.stmm_c2 = 0;
  3659.     sampler_dndi[index].dw1.low_temporal_difference_threshold = 8;
  3660.     sampler_dndi[index].dw1.temporal_difference_threshold = 16;
  3661.  
  3662.     sampler_dndi[index].dw2.block_noise_estimate_noise_threshold = dn_strength;   // 0-31
  3663.     sampler_dndi[index].dw2.block_noise_estimate_edge_threshold = 7;    // 0-15
  3664.     sampler_dndi[index].dw2.denoise_edge_threshold = 7;                 // 0-15
  3665.     sampler_dndi[index].dw2.good_neighbor_threshold = 7;                // 0-63
  3666.  
  3667.     sampler_dndi[index].dw3.maximum_stmm = 128;
  3668.     sampler_dndi[index].dw3.multipler_for_vecm = 2;
  3669.     sampler_dndi[index].dw3.blending_constant_across_time_for_small_values_of_stmm = 0;
  3670.     sampler_dndi[index].dw3.blending_constant_across_time_for_large_values_of_stmm = 64;
  3671.     sampler_dndi[index].dw3.stmm_blending_constant_select = 0;
  3672.  
  3673.     sampler_dndi[index].dw4.sdi_delta = 8;
  3674.     sampler_dndi[index].dw4.sdi_threshold = 128;
  3675.     sampler_dndi[index].dw4.stmm_output_shift = 7;                      // stmm_max - stmm_min = 2 ^ stmm_output_shift
  3676.     sampler_dndi[index].dw4.stmm_shift_up = 0;
  3677.     sampler_dndi[index].dw4.stmm_shift_down = 0;
  3678.     sampler_dndi[index].dw4.minimum_stmm = 0;
  3679.  
  3680.     sampler_dndi[index].dw5.fmd_temporal_difference_threshold = 0;
  3681.     sampler_dndi[index].dw5.sdi_fallback_mode_2_constant = 0;
  3682.     sampler_dndi[index].dw5.sdi_fallback_mode_1_t2_constant = 0;
  3683.     sampler_dndi[index].dw5.sdi_fallback_mode_1_t1_constant = 0;
  3684.  
  3685.     sampler_dndi[index].dw6.dn_enable = 1;
  3686.     sampler_dndi[index].dw6.di_enable = 0;
  3687.     sampler_dndi[index].dw6.di_partial = 0;
  3688.     sampler_dndi[index].dw6.dndi_top_first = dndi_top_first;
  3689.     sampler_dndi[index].dw6.dndi_stream_id = 1;
  3690.     sampler_dndi[index].dw6.dndi_first_frame = 1;
  3691.     sampler_dndi[index].dw6.progressive_dn = dn_progressive;
  3692.     sampler_dndi[index].dw6.fmd_tear_threshold = 32;
  3693.     sampler_dndi[index].dw6.fmd2_vertical_difference_threshold = 32;
  3694.     sampler_dndi[index].dw6.fmd1_vertical_difference_threshold = 32;
  3695.  
  3696.     sampler_dndi[index].dw7.fmd_for_1st_field_of_current_frame = 2;
  3697.     sampler_dndi[index].dw7.fmd_for_2nd_field_of_previous_frame = 1;
  3698.     sampler_dndi[index].dw7.vdi_walker_enable = 0;
  3699.     sampler_dndi[index].dw7.column_width_minus1 = w / 16;
  3700.  
  3701.     dri_bo_unmap(pp_context->sampler_state_table.bo);
  3702.  
  3703.     /* private function & data */
  3704.     pp_context->pp_x_steps = pp_dn_x_steps;
  3705.     pp_context->pp_y_steps = pp_dn_y_steps;
  3706.     pp_context->private_context = &pp_context->pp_dn_context;
  3707.     pp_context->pp_set_block_parameter = pp_dn_set_block_parameter;
  3708.  
  3709.     pp_static_parameter->grf1.statistics_surface_picth = w / 2;
  3710.     pp_static_parameter->grf1.r1_6.di.top_field_first = 0;
  3711.     pp_static_parameter->grf4.r4_2.di.motion_history_coefficient_m2 = 64;
  3712.     pp_static_parameter->grf4.r4_2.di.motion_history_coefficient_m1 = 192;
  3713.  
  3714.     pp_inline_parameter->grf5.block_count_x = w / 16;   /* 1 x N */
  3715.     pp_inline_parameter->grf5.number_blocks = w / 16;
  3716.     pp_inline_parameter->grf5.block_vertical_mask = 0xff;
  3717.     pp_inline_parameter->grf5.block_horizontal_mask = 0xffff;
  3718.  
  3719.     pp_dn_context->dest_w = w;
  3720.     pp_dn_context->dest_h = h;
  3721.  
  3722.     dst_surface->flags = src_surface->flags;
  3723.    
  3724.     return VA_STATUS_SUCCESS;
  3725. }
  3726.  
  3727. static int
  3728. gen7_pp_dndi_x_steps(void *private_context)
  3729. {
  3730.     struct pp_dndi_context *pp_dndi_context = private_context;
  3731.  
  3732.     return pp_dndi_context->dest_w / 16;
  3733. }
  3734.  
  3735. static int
  3736. gen7_pp_dndi_y_steps(void *private_context)
  3737. {
  3738.     struct pp_dndi_context *pp_dndi_context = private_context;
  3739.  
  3740.     return pp_dndi_context->dest_h / 4;
  3741. }
  3742.  
  3743. static int
  3744. gen7_pp_dndi_set_block_parameter(struct i965_post_processing_context *pp_context, int x, int y)
  3745. {
  3746.     struct gen7_pp_inline_parameter *pp_inline_parameter = pp_context->pp_inline_parameter;
  3747.  
  3748.     pp_inline_parameter->grf9.destination_block_horizontal_origin = x * 16;
  3749.     pp_inline_parameter->grf9.destination_block_vertical_origin = y * 4;
  3750.  
  3751.     return 0;
  3752. }
  3753.  
  3754. static VAStatus
  3755. gen7_pp_nv12_dndi_initialize(VADriverContextP ctx, struct i965_post_processing_context *pp_context,
  3756.                              const struct i965_surface *src_surface,
  3757.                              const VARectangle *src_rect,
  3758.                              struct i965_surface *dst_surface,
  3759.                              const VARectangle *dst_rect,
  3760.                              void *filter_param)
  3761. {
  3762.     struct pp_dndi_context * const dndi_ctx = &pp_context->pp_dndi_context;
  3763.     struct gen7_pp_static_parameter *pp_static_parameter = pp_context->pp_static_parameter;
  3764.     const VAProcPipelineParameterBuffer * const pipe_params =
  3765.         pp_context->pipeline_param;
  3766.     const VAProcFilterParameterBufferDeinterlacing * const deint_params =
  3767.         filter_param;
  3768.     struct object_surface * const src_obj_surface = (struct object_surface *)
  3769.         src_surface->base;
  3770.     struct object_surface * const dst_obj_surface = (struct object_surface *)
  3771.         dst_surface->base;
  3772.     struct object_surface *obj_surface;
  3773.     struct gen7_sampler_dndi *sampler_dndi;
  3774.     int index, dndi_top_first;
  3775.     int w, h, orig_w, orig_h;
  3776.     VAStatus status;
  3777.  
  3778.     status = pp_dndi_context_init_surface_params(dndi_ctx, src_obj_surface,
  3779.         pipe_params, deint_params);
  3780.     if (status != VA_STATUS_SUCCESS)
  3781.         return status;
  3782.  
  3783.     status = pp_dndi_context_ensure_surfaces(ctx, pp_context,
  3784.         src_obj_surface, dst_obj_surface);
  3785.     if (status != VA_STATUS_SUCCESS)
  3786.         return status;
  3787.  
  3788.     status = pp_dndi_context_ensure_surfaces_storage(ctx, pp_context,
  3789.         src_obj_surface, dst_obj_surface);
  3790.     if (status != VA_STATUS_SUCCESS)
  3791.         return status;
  3792.  
  3793.     /* Current input surface (index = 3) */
  3794.     obj_surface = dndi_ctx->frame_store[DNDI_FRAME_IN_CURRENT].obj_surface;
  3795.     gen7_pp_set_surface2_state(ctx, pp_context, obj_surface->bo, 0,
  3796.         obj_surface->orig_width, obj_surface->orig_height, obj_surface->width,
  3797.         0, obj_surface->y_cb_offset, SURFACE_FORMAT_PLANAR_420_8, 1, 3);
  3798.  
  3799.     /* Previous input surface (index = 4) */
  3800.     obj_surface = dndi_ctx->frame_store[DNDI_FRAME_IN_PREVIOUS].obj_surface;
  3801.     gen7_pp_set_surface2_state(ctx, pp_context, obj_surface->bo, 0,
  3802.         obj_surface->orig_width, obj_surface->orig_height, obj_surface->width,
  3803.         0, obj_surface->y_cb_offset, SURFACE_FORMAT_PLANAR_420_8, 1, 4);
  3804.  
  3805.     /* STMM input surface (index = 5) */
  3806.     obj_surface = dndi_ctx->frame_store[DNDI_FRAME_IN_STMM].obj_surface;
  3807.     gen7_pp_set_surface_state(ctx, pp_context, obj_surface->bo, 0,
  3808.         obj_surface->orig_width, obj_surface->orig_height, obj_surface->width,
  3809.         I965_SURFACEFORMAT_R8_UNORM, 5, 1);
  3810.  
  3811.     /* Previous output surfaces (index = { 27, 28 }) */
  3812.     obj_surface = dndi_ctx->frame_store[DNDI_FRAME_OUT_PREVIOUS].obj_surface;
  3813.     w = obj_surface->width;
  3814.     h = obj_surface->height;
  3815.     orig_w = obj_surface->orig_width;
  3816.     orig_h = obj_surface->orig_height;
  3817.  
  3818.     gen7_pp_set_surface_state(ctx, pp_context, obj_surface->bo, 0,
  3819.         ALIGN(orig_w, 4) / 4, orig_h, w, I965_SURFACEFORMAT_R8_UNORM, 27, 1);
  3820.     gen7_pp_set_surface_state(ctx, pp_context, obj_surface->bo, w * h,
  3821.         ALIGN(orig_w, 4) / 4, orig_h / 2, w, I965_SURFACEFORMAT_R8G8_UNORM, 28, 1);
  3822.  
  3823.     /* Current output surfaces (index = { 30, 31 }) */
  3824.     obj_surface = dndi_ctx->frame_store[DNDI_FRAME_OUT_CURRENT].obj_surface;
  3825.     w = obj_surface->width;
  3826.     h = obj_surface->height;
  3827.     orig_w = obj_surface->orig_width;
  3828.     orig_h = obj_surface->orig_height;
  3829.  
  3830.     gen7_pp_set_surface_state(ctx, pp_context, obj_surface->bo, 0,
  3831.         ALIGN(orig_w, 4) / 4, orig_h, w, I965_SURFACEFORMAT_R8_UNORM, 30, 1);
  3832.     gen7_pp_set_surface_state(ctx, pp_context, obj_surface->bo, w * h,
  3833.         ALIGN(orig_w, 4) / 4, orig_h / 2, w, I965_SURFACEFORMAT_R8G8_UNORM, 31, 1);
  3834.  
  3835.     /* STMM output surface (index = 33) */
  3836.     obj_surface = dndi_ctx->frame_store[DNDI_FRAME_OUT_STMM].obj_surface;
  3837.     gen7_pp_set_surface_state(ctx, pp_context, obj_surface->bo, 0,
  3838.         obj_surface->orig_width, obj_surface->orig_height, obj_surface->width,
  3839.         I965_SURFACEFORMAT_R8_UNORM, 33, 1);
  3840.  
  3841.     dndi_top_first = !(deint_params->flags & VA_DEINTERLACING_BOTTOM_FIELD);
  3842.  
  3843.     /* sampler dndi */
  3844.     dri_bo_map(pp_context->sampler_state_table.bo, True);
  3845.     assert(pp_context->sampler_state_table.bo->virtual);
  3846.     assert(sizeof(*sampler_dndi) == sizeof(int) * 8);
  3847.     sampler_dndi = pp_context->sampler_state_table.bo->virtual;
  3848.  
  3849.     /* sample dndi index 0 */
  3850.     index = 0;
  3851.     sampler_dndi[index].dw0.denoise_asd_threshold = 38;
  3852.     sampler_dndi[index].dw0.dnmh_delt = 7;
  3853.     sampler_dndi[index].dw0.vdi_walker_y_stride = 0;
  3854.     sampler_dndi[index].dw0.vdi_walker_frame_sharing_enable = 0;
  3855.     sampler_dndi[index].dw0.denoise_maximum_history = 192;      // 128-240
  3856.     sampler_dndi[index].dw0.denoise_stad_threshold = 140;
  3857.  
  3858.     sampler_dndi[index].dw1.denoise_threshold_for_sum_of_complexity_measure = 38;
  3859.     sampler_dndi[index].dw1.denoise_moving_pixel_threshold = 1;
  3860.     sampler_dndi[index].dw1.stmm_c2 = 2;
  3861.     sampler_dndi[index].dw1.low_temporal_difference_threshold = 0;
  3862.     sampler_dndi[index].dw1.temporal_difference_threshold = 0;
  3863.  
  3864.     sampler_dndi[index].dw2.block_noise_estimate_noise_threshold = 20;   // 0-31
  3865.     sampler_dndi[index].dw2.bne_edge_th = 1;
  3866.     sampler_dndi[index].dw2.smooth_mv_th = 0;
  3867.     sampler_dndi[index].dw2.sad_tight_th = 5;
  3868.     sampler_dndi[index].dw2.cat_slope_minus1 = 9;
  3869.     sampler_dndi[index].dw2.good_neighbor_th = 12;
  3870.  
  3871.     sampler_dndi[index].dw3.maximum_stmm = 150;
  3872.     sampler_dndi[index].dw3.multipler_for_vecm = 30;
  3873.     sampler_dndi[index].dw3.blending_constant_across_time_for_small_values_of_stmm = 125;
  3874.     sampler_dndi[index].dw3.blending_constant_across_time_for_large_values_of_stmm = 64;
  3875.     sampler_dndi[index].dw3.stmm_blending_constant_select = 0;
  3876.  
  3877.     sampler_dndi[index].dw4.sdi_delta = 5;
  3878.     sampler_dndi[index].dw4.sdi_threshold = 100;
  3879.     sampler_dndi[index].dw4.stmm_output_shift = 5;                      // stmm_max - stmm_min = 2 ^ stmm_output_shift
  3880.     sampler_dndi[index].dw4.stmm_shift_up = 1;
  3881.     sampler_dndi[index].dw4.stmm_shift_down = 3;
  3882.     sampler_dndi[index].dw4.minimum_stmm = 118;
  3883.  
  3884.     sampler_dndi[index].dw5.fmd_temporal_difference_threshold = 175;
  3885.     sampler_dndi[index].dw5.sdi_fallback_mode_2_constant = 37;
  3886.     sampler_dndi[index].dw5.sdi_fallback_mode_1_t2_constant = 100;
  3887.     sampler_dndi[index].dw5.sdi_fallback_mode_1_t1_constant = 50;
  3888.     sampler_dndi[index].dw6.dn_enable = 0;
  3889.     sampler_dndi[index].dw6.di_enable = 1;
  3890.     sampler_dndi[index].dw6.di_partial = 0;
  3891.     sampler_dndi[index].dw6.dndi_top_first = dndi_top_first;
  3892.     sampler_dndi[index].dw6.dndi_stream_id = 1;
  3893.     sampler_dndi[index].dw6.dndi_first_frame = dndi_ctx->is_first_frame;
  3894.     sampler_dndi[index].dw6.progressive_dn = 0;
  3895.     sampler_dndi[index].dw6.mcdi_enable =
  3896.         (deint_params->algorithm == VAProcDeinterlacingMotionCompensated);
  3897.     sampler_dndi[index].dw6.fmd_tear_threshold = 2;
  3898.     sampler_dndi[index].dw6.cat_th1 = 0;
  3899.     sampler_dndi[index].dw6.fmd2_vertical_difference_threshold = 100;
  3900.     sampler_dndi[index].dw6.fmd1_vertical_difference_threshold = 16;
  3901.  
  3902.     sampler_dndi[index].dw7.sad_tha = 5;
  3903.     sampler_dndi[index].dw7.sad_thb = 10;
  3904.     sampler_dndi[index].dw7.fmd_for_1st_field_of_current_frame = 0;
  3905.     sampler_dndi[index].dw7.mc_pixel_consistency_th = 25;
  3906.     sampler_dndi[index].dw7.fmd_for_2nd_field_of_previous_frame = 0;
  3907.     sampler_dndi[index].dw7.vdi_walker_enable = 0;
  3908.     sampler_dndi[index].dw7.neighborpixel_th = 10;
  3909.     sampler_dndi[index].dw7.column_width_minus1 = w / 16;
  3910.  
  3911.     dri_bo_unmap(pp_context->sampler_state_table.bo);
  3912.  
  3913.     /* private function & data */
  3914.     pp_context->pp_x_steps = gen7_pp_dndi_x_steps;
  3915.     pp_context->pp_y_steps = gen7_pp_dndi_y_steps;
  3916.     pp_context->private_context = dndi_ctx;
  3917.     pp_context->pp_set_block_parameter = gen7_pp_dndi_set_block_parameter;
  3918.  
  3919.     pp_static_parameter->grf1.di_statistics_surface_pitch_div2 = w / 2;
  3920.     pp_static_parameter->grf1.di_statistics_surface_height_div4 = h / 4;
  3921.     pp_static_parameter->grf1.di_top_field_first = 0;
  3922.     pp_static_parameter->grf1.pointer_to_inline_parameter = 7;
  3923.  
  3924.     pp_static_parameter->grf2.di_destination_packed_y_component_offset = 0;
  3925.     pp_static_parameter->grf2.di_destination_packed_u_component_offset = 1;
  3926.     pp_static_parameter->grf2.di_destination_packed_v_component_offset = 3;
  3927.  
  3928.     pp_static_parameter->grf4.di_hoffset_svf_from_dvf = 0;
  3929.     pp_static_parameter->grf4.di_voffset_svf_from_dvf = 0;
  3930.  
  3931.     dndi_ctx->dest_w = w;
  3932.     dndi_ctx->dest_h = h;
  3933.  
  3934.     dst_surface->flags = I965_SURFACE_FLAG_FRAME;
  3935.     return VA_STATUS_SUCCESS;
  3936. }
  3937.  
  3938. static int
  3939. gen7_pp_dn_x_steps(void *private_context)
  3940. {
  3941.     struct pp_dn_context *pp_dn_context = private_context;
  3942.  
  3943.     return pp_dn_context->dest_w / 16;
  3944. }
  3945.  
  3946. static int
  3947. gen7_pp_dn_y_steps(void *private_context)
  3948. {
  3949.     struct pp_dn_context *pp_dn_context = private_context;
  3950.  
  3951.     return pp_dn_context->dest_h / 4;
  3952. }
  3953.  
  3954. static int
  3955. gen7_pp_dn_set_block_parameter(struct i965_post_processing_context *pp_context, int x, int y)
  3956. {
  3957.     struct pp_inline_parameter *pp_inline_parameter = pp_context->pp_inline_parameter;
  3958.  
  3959.     pp_inline_parameter->grf5.destination_block_horizontal_origin = x * 16;
  3960.     pp_inline_parameter->grf5.destination_block_vertical_origin = y * 4;
  3961.  
  3962.     return 0;
  3963. }
  3964.  
  3965. static VAStatus
  3966. gen7_pp_nv12_dn_initialize(VADriverContextP ctx, struct i965_post_processing_context *pp_context,
  3967.                            const struct i965_surface *src_surface,
  3968.                            const VARectangle *src_rect,
  3969.                            struct i965_surface *dst_surface,
  3970.                            const VARectangle *dst_rect,
  3971.                            void *filter_param)
  3972. {
  3973.     struct i965_driver_data *i965 = i965_driver_data(ctx);
  3974.     struct pp_dn_context *pp_dn_context = (struct pp_dn_context *)&pp_context->pp_dn_context;
  3975.     struct gen7_pp_static_parameter *pp_static_parameter = pp_context->pp_static_parameter;
  3976.     struct object_surface *obj_surface;
  3977.     struct gen7_sampler_dndi *sampler_dn;
  3978.     VAProcFilterParameterBuffer *dn_filter_param = filter_param; /* FIXME: parameter */
  3979.     int index;
  3980.     int w, h;
  3981.     int orig_w, orig_h;
  3982.     int dn_strength = 15;
  3983.     int dndi_top_first = 1;
  3984.     int dn_progressive = 0;
  3985.  
  3986.     if (src_surface->flags == I965_SURFACE_FLAG_FRAME) {
  3987.         dndi_top_first = 1;
  3988.         dn_progressive = 1;
  3989.     } else if (src_surface->flags == I965_SURFACE_FLAG_TOP_FIELD_FIRST) {
  3990.         dndi_top_first = 1;
  3991.         dn_progressive = 0;
  3992.     } else {
  3993.         dndi_top_first = 0;
  3994.         dn_progressive = 0;
  3995.     }
  3996.  
  3997.     if (dn_filter_param) {
  3998.         float value = dn_filter_param->value;
  3999.        
  4000.         if (value > 1.0)
  4001.             value = 1.0;
  4002.        
  4003.         if (value < 0.0)
  4004.             value = 0.0;
  4005.  
  4006.         dn_strength = (int)(value * 31.0F);
  4007.     }
  4008.  
  4009.     /* surface */
  4010.     obj_surface = (struct object_surface *)src_surface->base;
  4011.     orig_w = obj_surface->orig_width;
  4012.     orig_h = obj_surface->orig_height;
  4013.     w = obj_surface->width;
  4014.     h = obj_surface->height;
  4015.  
  4016.     if (pp_dn_context->stmm_bo == NULL) {
  4017.         pp_dn_context->stmm_bo= dri_bo_alloc(i965->intel.bufmgr,
  4018.                                              "STMM surface",
  4019.                                              w * h,
  4020.                                              4096);
  4021.         assert(pp_dn_context->stmm_bo);
  4022.     }
  4023.  
  4024.     /* source UV surface index 1 */
  4025.     gen7_pp_set_surface_state(ctx, pp_context,
  4026.                               obj_surface->bo, w * h,
  4027.                               ALIGN(orig_w, 4) / 4, orig_h / 2, w, I965_SURFACEFORMAT_R8G8_UNORM,
  4028.                               1, 0);
  4029.  
  4030.     /* source YUV surface index 3 */
  4031.     gen7_pp_set_surface2_state(ctx, pp_context,
  4032.                                obj_surface->bo, 0,
  4033.                                orig_w, orig_h, w,
  4034.                                0, h,
  4035.                                SURFACE_FORMAT_PLANAR_420_8, 1,
  4036.                                3);
  4037.  
  4038.     /* source (temporal reference) YUV surface index 4 */
  4039.     gen7_pp_set_surface2_state(ctx, pp_context,
  4040.                                obj_surface->bo, 0,
  4041.                                orig_w, orig_h, w,
  4042.                                0, h,
  4043.                                SURFACE_FORMAT_PLANAR_420_8, 1,
  4044.                                4);
  4045.  
  4046.     /* STMM / History Statistics input surface, index 5 */
  4047.     gen7_pp_set_surface_state(ctx, pp_context,
  4048.                               pp_dn_context->stmm_bo, 0,
  4049.                               orig_w, orig_h, w, I965_SURFACEFORMAT_R8_UNORM,
  4050.                               33, 1);
  4051.  
  4052.     /* destination surface */
  4053.     obj_surface = (struct object_surface *)dst_surface->base;
  4054.     orig_w = obj_surface->orig_width;
  4055.     orig_h = obj_surface->orig_height;
  4056.     w = obj_surface->width;
  4057.     h = obj_surface->height;
  4058.  
  4059.     /* destination Y surface index 24 */
  4060.     gen7_pp_set_surface_state(ctx, pp_context,
  4061.                               obj_surface->bo, 0,
  4062.                               ALIGN(orig_w, 4) / 4, orig_h, w, I965_SURFACEFORMAT_R8_UNORM,
  4063.                               24, 1);
  4064.  
  4065.     /* destination UV surface index 25 */
  4066.     gen7_pp_set_surface_state(ctx, pp_context,
  4067.                               obj_surface->bo, w * h,
  4068.                               ALIGN(orig_w, 4) / 4, orig_h / 2, w, I965_SURFACEFORMAT_R8G8_UNORM,
  4069.                               25, 1);
  4070.  
  4071.     /* sampler dn */
  4072.     dri_bo_map(pp_context->sampler_state_table.bo, True);
  4073.     assert(pp_context->sampler_state_table.bo->virtual);
  4074.     assert(sizeof(*sampler_dn) == sizeof(int) * 8);
  4075.     sampler_dn = pp_context->sampler_state_table.bo->virtual;
  4076.  
  4077.     /* sample dn index 1 */
  4078.     index = 0;
  4079.     sampler_dn[index].dw0.denoise_asd_threshold = 0;
  4080.     sampler_dn[index].dw0.dnmh_delt = 8;
  4081.     sampler_dn[index].dw0.vdi_walker_y_stride = 0;
  4082.     sampler_dn[index].dw0.vdi_walker_frame_sharing_enable = 0;
  4083.     sampler_dn[index].dw0.denoise_maximum_history = 128;      // 128-240
  4084.     sampler_dn[index].dw0.denoise_stad_threshold = 0;
  4085.  
  4086.     sampler_dn[index].dw1.denoise_threshold_for_sum_of_complexity_measure = 64;
  4087.     sampler_dn[index].dw1.denoise_moving_pixel_threshold = 0;
  4088.     sampler_dn[index].dw1.stmm_c2 = 0;
  4089.     sampler_dn[index].dw1.low_temporal_difference_threshold = 8;
  4090.     sampler_dn[index].dw1.temporal_difference_threshold = 16;
  4091.  
  4092.     sampler_dn[index].dw2.block_noise_estimate_noise_threshold = dn_strength;   // 0-31
  4093.     sampler_dn[index].dw2.bne_edge_th = 1;
  4094.     sampler_dn[index].dw2.smooth_mv_th = 0;
  4095.     sampler_dn[index].dw2.sad_tight_th = 5;
  4096.     sampler_dn[index].dw2.cat_slope_minus1 = 9;
  4097.     sampler_dn[index].dw2.good_neighbor_th = 4;
  4098.  
  4099.     sampler_dn[index].dw3.maximum_stmm = 128;
  4100.     sampler_dn[index].dw3.multipler_for_vecm = 2;
  4101.     sampler_dn[index].dw3.blending_constant_across_time_for_small_values_of_stmm = 0;
  4102.     sampler_dn[index].dw3.blending_constant_across_time_for_large_values_of_stmm = 64;
  4103.     sampler_dn[index].dw3.stmm_blending_constant_select = 0;
  4104.  
  4105.     sampler_dn[index].dw4.sdi_delta = 8;
  4106.     sampler_dn[index].dw4.sdi_threshold = 128;
  4107.     sampler_dn[index].dw4.stmm_output_shift = 7;                      // stmm_max - stmm_min = 2 ^ stmm_output_shift
  4108.     sampler_dn[index].dw4.stmm_shift_up = 0;
  4109.     sampler_dn[index].dw4.stmm_shift_down = 0;
  4110.     sampler_dn[index].dw4.minimum_stmm = 0;
  4111.  
  4112.     sampler_dn[index].dw5.fmd_temporal_difference_threshold = 0;
  4113.     sampler_dn[index].dw5.sdi_fallback_mode_2_constant = 0;
  4114.     sampler_dn[index].dw5.sdi_fallback_mode_1_t2_constant = 0;
  4115.     sampler_dn[index].dw5.sdi_fallback_mode_1_t1_constant = 0;
  4116.  
  4117.     sampler_dn[index].dw6.dn_enable = 1;
  4118.     sampler_dn[index].dw6.di_enable = 0;
  4119.     sampler_dn[index].dw6.di_partial = 0;
  4120.     sampler_dn[index].dw6.dndi_top_first = dndi_top_first;
  4121.     sampler_dn[index].dw6.dndi_stream_id = 1;
  4122.     sampler_dn[index].dw6.dndi_first_frame = 1;
  4123.     sampler_dn[index].dw6.progressive_dn = dn_progressive;
  4124.     sampler_dn[index].dw6.mcdi_enable = 0;
  4125.     sampler_dn[index].dw6.fmd_tear_threshold = 32;
  4126.     sampler_dn[index].dw6.cat_th1 = 0;
  4127.     sampler_dn[index].dw6.fmd2_vertical_difference_threshold = 32;
  4128.     sampler_dn[index].dw6.fmd1_vertical_difference_threshold = 32;
  4129.  
  4130.     sampler_dn[index].dw7.sad_tha = 5;
  4131.     sampler_dn[index].dw7.sad_thb = 10;
  4132.     sampler_dn[index].dw7.fmd_for_1st_field_of_current_frame = 2;
  4133.     sampler_dn[index].dw7.mc_pixel_consistency_th = 25;
  4134.     sampler_dn[index].dw7.fmd_for_2nd_field_of_previous_frame = 1;
  4135.     sampler_dn[index].dw7.vdi_walker_enable = 0;
  4136.     sampler_dn[index].dw7.neighborpixel_th = 10;
  4137.     sampler_dn[index].dw7.column_width_minus1 = w / 16;
  4138.  
  4139.     dri_bo_unmap(pp_context->sampler_state_table.bo);
  4140.  
  4141.     /* private function & data */
  4142.     pp_context->pp_x_steps = gen7_pp_dn_x_steps;
  4143.     pp_context->pp_y_steps = gen7_pp_dn_y_steps;
  4144.     pp_context->private_context = &pp_context->pp_dn_context;
  4145.     pp_context->pp_set_block_parameter = gen7_pp_dn_set_block_parameter;
  4146.  
  4147.     pp_static_parameter->grf1.di_statistics_surface_pitch_div2 = w / 2;
  4148.     pp_static_parameter->grf1.di_statistics_surface_height_div4 = h / 4;
  4149.     pp_static_parameter->grf1.di_top_field_first = 0;
  4150.     pp_static_parameter->grf1.pointer_to_inline_parameter = 7;
  4151.  
  4152.     pp_static_parameter->grf2.di_destination_packed_y_component_offset = 0;
  4153.     pp_static_parameter->grf2.di_destination_packed_u_component_offset = 1;
  4154.     pp_static_parameter->grf2.di_destination_packed_v_component_offset = 3;
  4155.  
  4156.     pp_static_parameter->grf4.di_hoffset_svf_from_dvf = 0;
  4157.     pp_static_parameter->grf4.di_voffset_svf_from_dvf = 0;
  4158.  
  4159.     pp_dn_context->dest_w = w;
  4160.     pp_dn_context->dest_h = h;
  4161.  
  4162.     dst_surface->flags = src_surface->flags;
  4163.  
  4164.     return VA_STATUS_SUCCESS;
  4165. }
  4166.  
  4167. static VAStatus
  4168. ironlake_pp_initialize(
  4169.     VADriverContextP ctx,
  4170.     struct i965_post_processing_context *pp_context,
  4171.     const struct i965_surface *src_surface,
  4172.     const VARectangle *src_rect,
  4173.     struct i965_surface *dst_surface,
  4174.     const VARectangle *dst_rect,
  4175.     int pp_index,
  4176.     void *filter_param
  4177. )
  4178. {
  4179.     VAStatus va_status;
  4180.     struct i965_driver_data *i965 = i965_driver_data(ctx);
  4181.     struct pp_module *pp_module;
  4182.     dri_bo *bo;
  4183.     int static_param_size, inline_param_size;
  4184.  
  4185.     dri_bo_unreference(pp_context->surface_state_binding_table.bo);
  4186.     bo = dri_bo_alloc(i965->intel.bufmgr,
  4187.                       "surface state & binding table",
  4188.                       (SURFACE_STATE_PADDED_SIZE + sizeof(unsigned int)) * MAX_PP_SURFACES,
  4189.                       4096);
  4190.     assert(bo);
  4191.     pp_context->surface_state_binding_table.bo = bo;
  4192.  
  4193.     dri_bo_unreference(pp_context->curbe.bo);
  4194.     bo = dri_bo_alloc(i965->intel.bufmgr,
  4195.                       "constant buffer",
  4196.                       4096,
  4197.                       4096);
  4198.     assert(bo);
  4199.     pp_context->curbe.bo = bo;
  4200.  
  4201.     dri_bo_unreference(pp_context->idrt.bo);
  4202.     bo = dri_bo_alloc(i965->intel.bufmgr,
  4203.                       "interface discriptor",
  4204.                       sizeof(struct i965_interface_descriptor),
  4205.                       4096);
  4206.     assert(bo);
  4207.     pp_context->idrt.bo = bo;
  4208.     pp_context->idrt.num_interface_descriptors = 0;
  4209.  
  4210.     dri_bo_unreference(pp_context->sampler_state_table.bo);
  4211.     bo = dri_bo_alloc(i965->intel.bufmgr,
  4212.                       "sampler state table",
  4213.                       4096,
  4214.                       4096);
  4215.     assert(bo);
  4216.     dri_bo_map(bo, True);
  4217.     memset(bo->virtual, 0, bo->size);
  4218.     dri_bo_unmap(bo);
  4219.     pp_context->sampler_state_table.bo = bo;
  4220.  
  4221.     dri_bo_unreference(pp_context->sampler_state_table.bo_8x8);
  4222.     bo = dri_bo_alloc(i965->intel.bufmgr,
  4223.                       "sampler 8x8 state ",
  4224.                       4096,
  4225.                       4096);
  4226.     assert(bo);
  4227.     pp_context->sampler_state_table.bo_8x8 = bo;
  4228.  
  4229.     dri_bo_unreference(pp_context->sampler_state_table.bo_8x8_uv);
  4230.     bo = dri_bo_alloc(i965->intel.bufmgr,
  4231.                       "sampler 8x8 state ",
  4232.                       4096,
  4233.                       4096);
  4234.     assert(bo);
  4235.     pp_context->sampler_state_table.bo_8x8_uv = bo;
  4236.  
  4237.     dri_bo_unreference(pp_context->vfe_state.bo);
  4238.     bo = dri_bo_alloc(i965->intel.bufmgr,
  4239.                       "vfe state",
  4240.                       sizeof(struct i965_vfe_state),
  4241.                       4096);
  4242.     assert(bo);
  4243.     pp_context->vfe_state.bo = bo;
  4244.  
  4245.     static_param_size = sizeof(struct pp_static_parameter);
  4246.     inline_param_size = sizeof(struct pp_inline_parameter);
  4247.  
  4248.     memset(pp_context->pp_static_parameter, 0, static_param_size);
  4249.     memset(pp_context->pp_inline_parameter, 0, inline_param_size);
  4250.    
  4251.     assert(pp_index >= PP_NULL && pp_index < NUM_PP_MODULES);
  4252.     pp_context->current_pp = pp_index;
  4253.     pp_module = &pp_context->pp_modules[pp_index];
  4254.    
  4255.     if (pp_module->initialize)
  4256.         va_status = pp_module->initialize(ctx, pp_context,
  4257.                                           src_surface,
  4258.                                           src_rect,
  4259.                                           dst_surface,
  4260.                                           dst_rect,
  4261.                                           filter_param);
  4262.     else
  4263.         va_status = VA_STATUS_ERROR_UNIMPLEMENTED;
  4264.  
  4265.     return va_status;
  4266. }
  4267.  
  4268. static VAStatus
  4269. ironlake_post_processing(
  4270.     VADriverContextP   ctx,
  4271.     struct i965_post_processing_context *pp_context,
  4272.     const struct i965_surface *src_surface,
  4273.     const VARectangle *src_rect,
  4274.     struct i965_surface *dst_surface,
  4275.     const VARectangle *dst_rect,
  4276.     int                pp_index,
  4277.     void *filter_param
  4278. )
  4279. {
  4280.     VAStatus va_status;
  4281.  
  4282.     va_status = ironlake_pp_initialize(ctx, pp_context,
  4283.                                        src_surface,
  4284.                                        src_rect,
  4285.                                        dst_surface,
  4286.                                        dst_rect,
  4287.                                        pp_index,
  4288.                                        filter_param);
  4289.  
  4290.     if (va_status == VA_STATUS_SUCCESS) {
  4291.         ironlake_pp_states_setup(ctx, pp_context);
  4292.         ironlake_pp_pipeline_setup(ctx, pp_context);
  4293.     }
  4294.  
  4295.     return va_status;
  4296. }
  4297.  
  4298. static VAStatus
  4299. gen6_pp_initialize(
  4300.     VADriverContextP ctx,
  4301.     struct i965_post_processing_context *pp_context,
  4302.     const struct i965_surface *src_surface,
  4303.     const VARectangle *src_rect,
  4304.     struct i965_surface *dst_surface,
  4305.     const VARectangle *dst_rect,
  4306.     int pp_index,
  4307.     void *filter_param
  4308. )
  4309. {
  4310.     VAStatus va_status;
  4311.     struct i965_driver_data *i965 = i965_driver_data(ctx);
  4312.     struct pp_module *pp_module;
  4313.     dri_bo *bo;
  4314.     int static_param_size, inline_param_size;
  4315.  
  4316.     dri_bo_unreference(pp_context->surface_state_binding_table.bo);
  4317.     bo = dri_bo_alloc(i965->intel.bufmgr,
  4318.                       "surface state & binding table",
  4319.                       (SURFACE_STATE_PADDED_SIZE + sizeof(unsigned int)) * MAX_PP_SURFACES,
  4320.                       4096);
  4321.     assert(bo);
  4322.     pp_context->surface_state_binding_table.bo = bo;
  4323.  
  4324.     dri_bo_unreference(pp_context->curbe.bo);
  4325.     bo = dri_bo_alloc(i965->intel.bufmgr,
  4326.                       "constant buffer",
  4327.                       4096,
  4328.                       4096);
  4329.     assert(bo);
  4330.     pp_context->curbe.bo = bo;
  4331.  
  4332.     dri_bo_unreference(pp_context->idrt.bo);
  4333.     bo = dri_bo_alloc(i965->intel.bufmgr,
  4334.                       "interface discriptor",
  4335.                       sizeof(struct gen6_interface_descriptor_data),
  4336.                       4096);
  4337.     assert(bo);
  4338.     pp_context->idrt.bo = bo;
  4339.     pp_context->idrt.num_interface_descriptors = 0;
  4340.  
  4341.     dri_bo_unreference(pp_context->sampler_state_table.bo);
  4342.     bo = dri_bo_alloc(i965->intel.bufmgr,
  4343.                       "sampler state table",
  4344.                       4096,
  4345.                       4096);
  4346.     assert(bo);
  4347.     dri_bo_map(bo, True);
  4348.     memset(bo->virtual, 0, bo->size);
  4349.     dri_bo_unmap(bo);
  4350.     pp_context->sampler_state_table.bo = bo;
  4351.  
  4352.     dri_bo_unreference(pp_context->sampler_state_table.bo_8x8);
  4353.     bo = dri_bo_alloc(i965->intel.bufmgr,
  4354.                       "sampler 8x8 state ",
  4355.                       4096,
  4356.                       4096);
  4357.     assert(bo);
  4358.     pp_context->sampler_state_table.bo_8x8 = bo;
  4359.  
  4360.     dri_bo_unreference(pp_context->sampler_state_table.bo_8x8_uv);
  4361.     bo = dri_bo_alloc(i965->intel.bufmgr,
  4362.                       "sampler 8x8 state ",
  4363.                       4096,
  4364.                       4096);
  4365.     assert(bo);
  4366.     pp_context->sampler_state_table.bo_8x8_uv = bo;
  4367.  
  4368.     dri_bo_unreference(pp_context->vfe_state.bo);
  4369.     bo = dri_bo_alloc(i965->intel.bufmgr,
  4370.                       "vfe state",
  4371.                       sizeof(struct i965_vfe_state),
  4372.                       4096);
  4373.     assert(bo);
  4374.     pp_context->vfe_state.bo = bo;
  4375.    
  4376.     if (IS_GEN7(i965->intel.device_info)) {
  4377.         static_param_size = sizeof(struct gen7_pp_static_parameter);
  4378.         inline_param_size = sizeof(struct gen7_pp_inline_parameter);
  4379.     } else {
  4380.         static_param_size = sizeof(struct pp_static_parameter);
  4381.         inline_param_size = sizeof(struct pp_inline_parameter);
  4382.     }
  4383.  
  4384.     memset(pp_context->pp_static_parameter, 0, static_param_size);
  4385.     memset(pp_context->pp_inline_parameter, 0, inline_param_size);
  4386.  
  4387.     assert(pp_index >= PP_NULL && pp_index < NUM_PP_MODULES);
  4388.     pp_context->current_pp = pp_index;
  4389.     pp_module = &pp_context->pp_modules[pp_index];
  4390.    
  4391.     if (pp_module->initialize)
  4392.         va_status = pp_module->initialize(ctx, pp_context,
  4393.                                           src_surface,
  4394.                                           src_rect,
  4395.                                           dst_surface,
  4396.                                           dst_rect,
  4397.                                           filter_param);
  4398.     else
  4399.         va_status = VA_STATUS_ERROR_UNIMPLEMENTED;
  4400.  
  4401.     calculate_boundary_block_mask(pp_context, dst_rect);
  4402.  
  4403.     return va_status;
  4404. }
  4405.  
  4406.  
  4407. static void
  4408. gen6_pp_interface_descriptor_table(VADriverContextP   ctx,
  4409.                                    struct i965_post_processing_context *pp_context)
  4410. {
  4411.     struct i965_driver_data *i965 = i965_driver_data(ctx);
  4412.     struct gen6_interface_descriptor_data *desc;
  4413.     dri_bo *bo;
  4414.     int pp_index = pp_context->current_pp;
  4415.  
  4416.     bo = pp_context->idrt.bo;
  4417.     dri_bo_map(bo, True);
  4418.     assert(bo->virtual);
  4419.     desc = bo->virtual;
  4420.     memset(desc, 0, sizeof(*desc));
  4421.     desc->desc0.kernel_start_pointer =
  4422.         pp_context->pp_modules[pp_index].kernel.bo->offset >> 6; /* reloc */
  4423.     desc->desc1.single_program_flow = 1;
  4424.     desc->desc1.floating_point_mode = FLOATING_POINT_IEEE_754;
  4425.     desc->desc2.sampler_count = 1;      /* 1 - 4 samplers used */
  4426.     desc->desc2.sampler_state_pointer =
  4427.         pp_context->sampler_state_table.bo->offset >> 5;
  4428.     desc->desc3.binding_table_entry_count = 0;
  4429.     desc->desc3.binding_table_pointer = (BINDING_TABLE_OFFSET >> 5);
  4430.     desc->desc4.constant_urb_entry_read_offset = 0;
  4431.  
  4432.     if (IS_GEN7(i965->intel.device_info))
  4433.         desc->desc4.constant_urb_entry_read_length = 8; /* grf 1-8 */
  4434.     else
  4435.         desc->desc4.constant_urb_entry_read_length = 4; /* grf 1-4 */
  4436.  
  4437.     dri_bo_emit_reloc(bo,
  4438.                       I915_GEM_DOMAIN_INSTRUCTION, 0,
  4439.                       0,
  4440.                       offsetof(struct gen6_interface_descriptor_data, desc0),
  4441.                       pp_context->pp_modules[pp_index].kernel.bo);
  4442.  
  4443.     dri_bo_emit_reloc(bo,
  4444.                       I915_GEM_DOMAIN_INSTRUCTION, 0,
  4445.                       desc->desc2.sampler_count << 2,
  4446.                       offsetof(struct gen6_interface_descriptor_data, desc2),
  4447.                       pp_context->sampler_state_table.bo);
  4448.  
  4449.     dri_bo_unmap(bo);
  4450.     pp_context->idrt.num_interface_descriptors++;
  4451. }
  4452.  
  4453. static void
  4454. gen6_pp_upload_constants(VADriverContextP ctx,
  4455.                          struct i965_post_processing_context *pp_context)
  4456. {
  4457.     struct i965_driver_data *i965 = i965_driver_data(ctx);
  4458.     unsigned char *constant_buffer;
  4459.     int param_size;
  4460.  
  4461.     assert(sizeof(struct pp_static_parameter) == 128);
  4462.     assert(sizeof(struct gen7_pp_static_parameter) == 256);
  4463.  
  4464.     if (IS_GEN7(i965->intel.device_info))
  4465.         param_size = sizeof(struct gen7_pp_static_parameter);
  4466.     else
  4467.         param_size = sizeof(struct pp_static_parameter);
  4468.  
  4469.     dri_bo_map(pp_context->curbe.bo, 1);
  4470.     assert(pp_context->curbe.bo->virtual);
  4471.     constant_buffer = pp_context->curbe.bo->virtual;
  4472.     memcpy(constant_buffer, pp_context->pp_static_parameter, param_size);
  4473.     dri_bo_unmap(pp_context->curbe.bo);
  4474. }
  4475.  
  4476. static void
  4477. gen6_pp_states_setup(VADriverContextP ctx,
  4478.                      struct i965_post_processing_context *pp_context)
  4479. {
  4480.     gen6_pp_interface_descriptor_table(ctx, pp_context);
  4481.     gen6_pp_upload_constants(ctx, pp_context);
  4482. }
  4483.  
  4484. static void
  4485. gen6_pp_pipeline_select(VADriverContextP ctx,
  4486.                         struct i965_post_processing_context *pp_context)
  4487. {
  4488.     struct intel_batchbuffer *batch = pp_context->batch;
  4489.  
  4490.     BEGIN_BATCH(batch, 1);
  4491.     OUT_BATCH(batch, CMD_PIPELINE_SELECT | PIPELINE_SELECT_MEDIA);
  4492.     ADVANCE_BATCH(batch);
  4493. }
  4494.  
  4495. static void
  4496. gen6_pp_state_base_address(VADriverContextP ctx,
  4497.                            struct i965_post_processing_context *pp_context)
  4498. {
  4499.     struct intel_batchbuffer *batch = pp_context->batch;
  4500.  
  4501.     BEGIN_BATCH(batch, 10);
  4502.     OUT_BATCH(batch, CMD_STATE_BASE_ADDRESS | (10 - 2));
  4503.     OUT_BATCH(batch, 0 | BASE_ADDRESS_MODIFY);
  4504.     OUT_RELOC(batch, pp_context->surface_state_binding_table.bo, I915_GEM_DOMAIN_INSTRUCTION, 0, BASE_ADDRESS_MODIFY); /* Surface state base address */
  4505.     OUT_BATCH(batch, 0 | BASE_ADDRESS_MODIFY);
  4506.     OUT_BATCH(batch, 0 | BASE_ADDRESS_MODIFY);
  4507.     OUT_BATCH(batch, 0 | BASE_ADDRESS_MODIFY);
  4508.     OUT_BATCH(batch, 0 | BASE_ADDRESS_MODIFY);
  4509.     OUT_BATCH(batch, 0 | BASE_ADDRESS_MODIFY);
  4510.     OUT_BATCH(batch, 0 | BASE_ADDRESS_MODIFY);
  4511.     OUT_BATCH(batch, 0 | BASE_ADDRESS_MODIFY);
  4512.     ADVANCE_BATCH(batch);
  4513. }
  4514.  
  4515. static void
  4516. gen6_pp_vfe_state(VADriverContextP ctx,
  4517.                   struct i965_post_processing_context *pp_context)
  4518. {
  4519.     struct intel_batchbuffer *batch = pp_context->batch;
  4520.  
  4521.     BEGIN_BATCH(batch, 8);
  4522.     OUT_BATCH(batch, CMD_MEDIA_VFE_STATE | (8 - 2));
  4523.     OUT_BATCH(batch, 0);
  4524.     OUT_BATCH(batch,
  4525.               (pp_context->vfe_gpu_state.max_num_threads - 1) << 16 |
  4526.               pp_context->vfe_gpu_state.num_urb_entries << 8);
  4527.     OUT_BATCH(batch, 0);
  4528.     OUT_BATCH(batch,
  4529.               (pp_context->vfe_gpu_state.urb_entry_size) << 16 |  
  4530.                 /* URB Entry Allocation Size, in 256 bits unit */
  4531.               (pp_context->vfe_gpu_state.curbe_allocation_size));
  4532.                 /* CURBE Allocation Size, in 256 bits unit */
  4533.     OUT_BATCH(batch, 0);
  4534.     OUT_BATCH(batch, 0);
  4535.     OUT_BATCH(batch, 0);
  4536.     ADVANCE_BATCH(batch);
  4537. }
  4538.  
  4539. static void
  4540. gen6_pp_curbe_load(VADriverContextP ctx,
  4541.                    struct i965_post_processing_context *pp_context)
  4542. {
  4543.     struct intel_batchbuffer *batch = pp_context->batch;
  4544.     struct i965_driver_data *i965 = i965_driver_data(ctx);
  4545.     int param_size;
  4546.  
  4547.     if (IS_GEN7(i965->intel.device_info))
  4548.         param_size = sizeof(struct gen7_pp_static_parameter);
  4549.     else
  4550.         param_size = sizeof(struct pp_static_parameter);
  4551.  
  4552.     BEGIN_BATCH(batch, 4);
  4553.     OUT_BATCH(batch, CMD_MEDIA_CURBE_LOAD | (4 - 2));
  4554.     OUT_BATCH(batch, 0);
  4555.     OUT_BATCH(batch,
  4556.               param_size);
  4557.     OUT_RELOC(batch,
  4558.               pp_context->curbe.bo,
  4559.               I915_GEM_DOMAIN_INSTRUCTION, 0,
  4560.               0);
  4561.     ADVANCE_BATCH(batch);
  4562. }
  4563.  
  4564. static void
  4565. gen6_interface_descriptor_load(VADriverContextP ctx,
  4566.                                struct i965_post_processing_context *pp_context)
  4567. {
  4568.     struct intel_batchbuffer *batch = pp_context->batch;
  4569.  
  4570.     BEGIN_BATCH(batch, 4);
  4571.     OUT_BATCH(batch, CMD_MEDIA_INTERFACE_DESCRIPTOR_LOAD | (4 - 2));
  4572.     OUT_BATCH(batch, 0);
  4573.     OUT_BATCH(batch,
  4574.               pp_context->idrt.num_interface_descriptors * sizeof(struct gen6_interface_descriptor_data));
  4575.     OUT_RELOC(batch,
  4576.               pp_context->idrt.bo,
  4577.               I915_GEM_DOMAIN_INSTRUCTION, 0,
  4578.               0);
  4579.     ADVANCE_BATCH(batch);
  4580. }
  4581.  
  4582. static void update_block_mask_parameter(struct i965_post_processing_context *pp_context, int x, int y, int x_steps, int y_steps)
  4583. {
  4584.     struct pp_inline_parameter *pp_inline_parameter = pp_context->pp_inline_parameter;
  4585.  
  4586.     pp_inline_parameter->grf5.block_vertical_mask = 0xff;
  4587.     pp_inline_parameter->grf6.block_vertical_mask_bottom = pp_context->block_vertical_mask_bottom;
  4588.     // for the first block, it always on the left edge. the second block will reload horizontal_mask from grf6.block_horizontal_mask_middle
  4589.     pp_inline_parameter->grf5.block_horizontal_mask = pp_context->block_horizontal_mask_left;
  4590.     pp_inline_parameter->grf6.block_horizontal_mask_middle = 0xffff;
  4591.     pp_inline_parameter->grf6.block_horizontal_mask_right = pp_context->block_horizontal_mask_right;
  4592.  
  4593.     /* 1 x N */
  4594.     if (x_steps == 1) {
  4595.         if (y == y_steps-1) {
  4596.             pp_inline_parameter->grf5.block_vertical_mask = pp_context->block_vertical_mask_bottom;
  4597.         }
  4598.         else {
  4599.             pp_inline_parameter->grf6.block_vertical_mask_bottom = 0xff;
  4600.         }
  4601.     }
  4602.  
  4603.     /* M x 1 */
  4604.     if (y_steps == 1) {
  4605.         if (x == 0) { // all blocks in this group are on the left edge
  4606.             pp_inline_parameter->grf6.block_horizontal_mask_middle = pp_context->block_horizontal_mask_left;
  4607.             pp_inline_parameter->grf6.block_horizontal_mask_right = pp_context->block_horizontal_mask_left;
  4608.         }
  4609.         else if (x == x_steps-1) {
  4610.             pp_inline_parameter->grf5.block_horizontal_mask = pp_context->block_horizontal_mask_right;
  4611.             pp_inline_parameter->grf6.block_horizontal_mask_middle = pp_context->block_horizontal_mask_right;
  4612.         }
  4613.         else {
  4614.             pp_inline_parameter->grf5.block_horizontal_mask = 0xffff;
  4615.             pp_inline_parameter->grf6.block_horizontal_mask_middle = 0xffff;
  4616.             pp_inline_parameter->grf6.block_horizontal_mask_right = 0xffff;
  4617.         }
  4618.     }
  4619.  
  4620. }
  4621.  
  4622. static void
  4623. gen6_pp_object_walker(VADriverContextP ctx,
  4624.                       struct i965_post_processing_context *pp_context)
  4625. {
  4626.     struct i965_driver_data *i965 = i965_driver_data(ctx);
  4627.     struct intel_batchbuffer *batch = pp_context->batch;
  4628.     int x, x_steps, y, y_steps;
  4629.     int param_size, command_length_in_dws;
  4630.     dri_bo *command_buffer;
  4631.     unsigned int *command_ptr;
  4632.  
  4633.     if (IS_GEN7(i965->intel.device_info))
  4634.         param_size = sizeof(struct gen7_pp_inline_parameter);
  4635.     else
  4636.         param_size = sizeof(struct pp_inline_parameter);
  4637.  
  4638.     x_steps = pp_context->pp_x_steps(pp_context->private_context);
  4639.     y_steps = pp_context->pp_y_steps(pp_context->private_context);
  4640.     command_length_in_dws = 6 + (param_size >> 2);
  4641.     command_buffer = dri_bo_alloc(i965->intel.bufmgr,
  4642.                                   "command objects buffer",
  4643.                                   command_length_in_dws * 4 * x_steps * y_steps + 8,
  4644.                                   4096);
  4645.  
  4646.     dri_bo_map(command_buffer, 1);
  4647.     command_ptr = command_buffer->virtual;
  4648.  
  4649.     for (y = 0; y < y_steps; y++) {
  4650.         for (x = 0; x < x_steps; x++) {
  4651.             if (!pp_context->pp_set_block_parameter(pp_context, x, y)) {
  4652.                 // some common block parameter update goes here, apply to all pp functions
  4653.                 if (IS_GEN6(i965->intel.device_info))
  4654.                     update_block_mask_parameter (pp_context, x, y, x_steps, y_steps);
  4655.                
  4656.                 *command_ptr++ = (CMD_MEDIA_OBJECT | (command_length_in_dws - 2));
  4657.                 *command_ptr++ = 0;
  4658.                 *command_ptr++ = 0;
  4659.                 *command_ptr++ = 0;
  4660.                 *command_ptr++ = 0;
  4661.                 *command_ptr++ = 0;
  4662.                 memcpy(command_ptr, pp_context->pp_inline_parameter, param_size);
  4663.                 command_ptr += (param_size >> 2);
  4664.             }
  4665.         }
  4666.     }
  4667.  
  4668.     if (command_length_in_dws * x_steps * y_steps % 2 == 0)
  4669.         *command_ptr++ = 0;
  4670.  
  4671.     *command_ptr = MI_BATCH_BUFFER_END;
  4672.  
  4673.     dri_bo_unmap(command_buffer);
  4674.  
  4675.     BEGIN_BATCH(batch, 2);
  4676.     OUT_BATCH(batch, MI_BATCH_BUFFER_START | (1 << 8));
  4677.     OUT_RELOC(batch, command_buffer,
  4678.               I915_GEM_DOMAIN_COMMAND, 0,
  4679.               0);
  4680.     ADVANCE_BATCH(batch);
  4681.  
  4682.     dri_bo_unreference(command_buffer);
  4683.  
  4684.     /* Have to execute the batch buffer here becuase MI_BATCH_BUFFER_END
  4685.      * will cause control to pass back to ring buffer
  4686.      */
  4687.     intel_batchbuffer_end_atomic(batch);
  4688.     intel_batchbuffer_flush(batch);
  4689.     intel_batchbuffer_start_atomic(batch, 0x1000);
  4690. }
  4691.  
  4692. static void
  4693. gen6_pp_pipeline_setup(VADriverContextP ctx,
  4694.                        struct i965_post_processing_context *pp_context)
  4695. {
  4696.     struct intel_batchbuffer *batch = pp_context->batch;
  4697.  
  4698.     intel_batchbuffer_start_atomic(batch, 0x1000);
  4699.     intel_batchbuffer_emit_mi_flush(batch);
  4700.     gen6_pp_pipeline_select(ctx, pp_context);
  4701.     gen6_pp_state_base_address(ctx, pp_context);
  4702.     gen6_pp_vfe_state(ctx, pp_context);
  4703.     gen6_pp_curbe_load(ctx, pp_context);
  4704.     gen6_interface_descriptor_load(ctx, pp_context);
  4705.     gen6_pp_object_walker(ctx, pp_context);
  4706.     intel_batchbuffer_end_atomic(batch);
  4707. }
  4708.  
  4709. static VAStatus
  4710. gen6_post_processing(
  4711.     VADriverContextP ctx,
  4712.     struct i965_post_processing_context *pp_context,
  4713.     const struct i965_surface *src_surface,
  4714.     const VARectangle *src_rect,
  4715.     struct i965_surface *dst_surface,
  4716.     const VARectangle *dst_rect,
  4717.     int pp_index,
  4718.     void *filter_param
  4719. )
  4720. {
  4721.     VAStatus va_status;
  4722.    
  4723.     va_status = gen6_pp_initialize(ctx, pp_context,
  4724.                                    src_surface,
  4725.                                    src_rect,
  4726.                                    dst_surface,
  4727.                                    dst_rect,
  4728.                                    pp_index,
  4729.                                    filter_param);
  4730.  
  4731.     if (va_status == VA_STATUS_SUCCESS) {
  4732.         gen6_pp_states_setup(ctx, pp_context);
  4733.         gen6_pp_pipeline_setup(ctx, pp_context);
  4734.     }
  4735.  
  4736.     if (va_status == VA_STATUS_SUCCESS_1)
  4737.         va_status = VA_STATUS_SUCCESS;
  4738.  
  4739.     return va_status;
  4740. }
  4741.  
  4742. static VAStatus
  4743. i965_post_processing_internal(
  4744.     VADriverContextP   ctx,
  4745.     struct i965_post_processing_context *pp_context,
  4746.     const struct i965_surface *src_surface,
  4747.     const VARectangle *src_rect,
  4748.     struct i965_surface *dst_surface,
  4749.     const VARectangle *dst_rect,
  4750.     int                pp_index,
  4751.     void *filter_param
  4752. )
  4753. {
  4754.     VAStatus va_status;
  4755.  
  4756.     if (pp_context && pp_context->intel_post_processing) {
  4757.         va_status = (pp_context->intel_post_processing)(ctx, pp_context,
  4758.                           src_surface, src_rect,
  4759.                           dst_surface, dst_rect,
  4760.                           pp_index, filter_param);
  4761.     } else {
  4762.         va_status = VA_STATUS_ERROR_UNIMPLEMENTED;
  4763.     }
  4764.  
  4765.     return va_status;
  4766. }
  4767.  
  4768. static void
  4769. rgb_to_yuv(unsigned int argb,
  4770.            unsigned char *y,
  4771.            unsigned char *u,
  4772.            unsigned char *v,
  4773.            unsigned char *a)
  4774. {
  4775.     int r = ((argb >> 16) & 0xff);
  4776.     int g = ((argb >> 8) & 0xff);
  4777.     int b = ((argb >> 0) & 0xff);
  4778.    
  4779.     *y = (257 * r + 504 * g + 98 * b) / 1000 + 16;
  4780.     *v = (439 * r - 368 * g - 71 * b) / 1000 + 128;
  4781.     *u = (-148 * r - 291 * g + 439 * b) / 1000 + 128;
  4782.     *a = ((argb >> 24) & 0xff);
  4783. }
  4784.  
  4785. static void
  4786. i965_vpp_clear_surface(VADriverContextP ctx,
  4787.                        struct i965_post_processing_context *pp_context,
  4788.                        struct object_surface *obj_surface,
  4789.                        unsigned int color)
  4790. {
  4791.     struct i965_driver_data *i965 = i965_driver_data(ctx);
  4792.     struct intel_batchbuffer *batch = pp_context->batch;
  4793.     unsigned int blt_cmd, br13;
  4794.     unsigned int tiling = 0, swizzle = 0;
  4795.     int pitch;
  4796.     unsigned char y, u, v, a = 0;
  4797.     int region_width, region_height;
  4798.  
  4799.     /* Currently only support NV12 surface */
  4800.     if (!obj_surface || obj_surface->fourcc != VA_FOURCC_NV12)
  4801.         return;
  4802.  
  4803.     rgb_to_yuv(color, &y, &u, &v, &a);
  4804.  
  4805.     if (a == 0)
  4806.         return;
  4807.  
  4808.     dri_bo_get_tiling(obj_surface->bo, &tiling, &swizzle);
  4809.     blt_cmd = XY_COLOR_BLT_CMD;
  4810.     pitch = obj_surface->width;
  4811.  
  4812.     if (tiling != I915_TILING_NONE) {
  4813.         assert(tiling == I915_TILING_Y);
  4814.         // blt_cmd |= XY_COLOR_BLT_DST_TILED;
  4815.         // pitch >>= 2;
  4816.     }
  4817.  
  4818.     br13 = 0xf0 << 16;
  4819.     br13 |= BR13_8;
  4820.     br13 |= pitch;
  4821.  
  4822.     if (IS_IRONLAKE(i965->intel.device_info)) {
  4823.         intel_batchbuffer_start_atomic(batch, 48);
  4824.         BEGIN_BATCH(batch, 12);
  4825.     } else {
  4826.         /* Will double-check the command if the new chipset is added */
  4827.         intel_batchbuffer_start_atomic_blt(batch, 48);
  4828.         BEGIN_BLT_BATCH(batch, 12);
  4829.     }
  4830.  
  4831.     region_width = obj_surface->width;
  4832.     region_height = obj_surface->height;
  4833.  
  4834.     OUT_BATCH(batch, blt_cmd);
  4835.     OUT_BATCH(batch, br13);
  4836.     OUT_BATCH(batch,
  4837.               0 << 16 |
  4838.               0);
  4839.     OUT_BATCH(batch,
  4840.               region_height << 16 |
  4841.               region_width);
  4842.     OUT_RELOC(batch, obj_surface->bo,
  4843.               I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER,
  4844.               0);
  4845.     OUT_BATCH(batch, y);
  4846.  
  4847.     br13 = 0xf0 << 16;
  4848.     br13 |= BR13_565;
  4849.     br13 |= pitch;
  4850.  
  4851.     region_width = obj_surface->width / 2;
  4852.     region_height = obj_surface->height / 2;
  4853.  
  4854.     if (tiling == I915_TILING_Y) {
  4855.         region_height = ALIGN(obj_surface->height / 2, 32);
  4856.     }
  4857.  
  4858.     OUT_BATCH(batch, blt_cmd);
  4859.     OUT_BATCH(batch, br13);
  4860.     OUT_BATCH(batch,
  4861.               0 << 16 |
  4862.               0);
  4863.     OUT_BATCH(batch,
  4864.               region_height << 16 |
  4865.               region_width);
  4866.     OUT_RELOC(batch, obj_surface->bo,
  4867.               I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER,
  4868.               obj_surface->width * obj_surface->y_cb_offset);
  4869.     OUT_BATCH(batch, v << 8 | u);
  4870.  
  4871.     ADVANCE_BATCH(batch);
  4872.     intel_batchbuffer_end_atomic(batch);
  4873. }
  4874.  
  4875. VAStatus
  4876. i965_scaling_processing(
  4877.     VADriverContextP   ctx,
  4878.     struct object_surface *src_surface_obj,
  4879.     const VARectangle *src_rect,
  4880.     struct object_surface *dst_surface_obj,
  4881.     const VARectangle *dst_rect,
  4882.     unsigned int       va_flags)
  4883. {
  4884.     VAStatus va_status = VA_STATUS_SUCCESS;
  4885.     struct i965_driver_data *i965 = i965_driver_data(ctx);
  4886.  
  4887.     assert(src_surface_obj->fourcc == VA_FOURCC_NV12);
  4888.     assert(dst_surface_obj->fourcc == VA_FOURCC_NV12);
  4889.  
  4890.     if (HAS_VPP(i965)) {
  4891.         struct i965_surface src_surface;
  4892.         struct i965_surface dst_surface;
  4893.         struct i965_post_processing_context *pp_context;
  4894.         unsigned int filter_flags;
  4895.  
  4896.          _i965LockMutex(&i965->pp_mutex);
  4897.  
  4898.          src_surface.base = (struct object_base *)src_surface_obj;
  4899.          src_surface.type = I965_SURFACE_TYPE_SURFACE;
  4900.          src_surface.flags = I965_SURFACE_FLAG_FRAME;
  4901.          dst_surface.base = (struct object_base *)dst_surface_obj;
  4902.          dst_surface.type = I965_SURFACE_TYPE_SURFACE;
  4903.          dst_surface.flags = I965_SURFACE_FLAG_FRAME;
  4904.  
  4905.          pp_context = i965->pp_context;
  4906.          filter_flags = pp_context->filter_flags;
  4907.          pp_context->filter_flags = va_flags;
  4908.  
  4909.          va_status = i965_post_processing_internal(ctx, pp_context,
  4910.              &src_surface, src_rect, &dst_surface, dst_rect,
  4911.              avs_is_needed(va_flags) ? PP_NV12_AVS : PP_NV12_SCALING, NULL);
  4912.  
  4913.          pp_context->filter_flags = filter_flags;
  4914.  
  4915.          _i965UnlockMutex(&i965->pp_mutex);
  4916.     }
  4917.  
  4918.     return va_status;
  4919. }
  4920.  
  4921. VASurfaceID
  4922. i965_post_processing(
  4923.     VADriverContextP   ctx,
  4924.     struct object_surface *obj_surface,
  4925.     const VARectangle *src_rect,
  4926.     const VARectangle *dst_rect,
  4927.     unsigned int       va_flags,
  4928.     int               *has_done_scaling,
  4929.     VARectangle *calibrated_rect
  4930. )
  4931. {
  4932.     struct i965_driver_data *i965 = i965_driver_data(ctx);
  4933.     VASurfaceID out_surface_id = VA_INVALID_ID;
  4934.     VASurfaceID tmp_id = VA_INVALID_ID;
  4935.    
  4936.     *has_done_scaling = 0;
  4937.  
  4938.     if (HAS_VPP(i965)) {
  4939.         VAStatus status;
  4940.         struct i965_surface src_surface;
  4941.         struct i965_surface dst_surface;
  4942.         struct i965_post_processing_context *pp_context;
  4943.  
  4944.         /* Currently only support post processing for NV12 surface */
  4945.         if (obj_surface->fourcc != VA_FOURCC_NV12)
  4946.             return out_surface_id;
  4947.  
  4948.         _i965LockMutex(&i965->pp_mutex);
  4949.  
  4950.         pp_context = i965->pp_context;
  4951.         pp_context->filter_flags = va_flags;
  4952.         if (avs_is_needed(va_flags)) {
  4953.             VARectangle tmp_dst_rect;
  4954.  
  4955.             if (out_surface_id != VA_INVALID_ID)
  4956.                 tmp_id = out_surface_id;
  4957.  
  4958.             tmp_dst_rect.x = 0;
  4959.             tmp_dst_rect.y = 0;
  4960.             tmp_dst_rect.width = dst_rect->width;
  4961.             tmp_dst_rect.height = dst_rect->height;
  4962.             src_surface.base = (struct object_base *)obj_surface;
  4963.             src_surface.type = I965_SURFACE_TYPE_SURFACE;
  4964.             src_surface.flags = I965_SURFACE_FLAG_FRAME;
  4965.  
  4966.             status = i965_CreateSurfaces(ctx,
  4967.                                          dst_rect->width,
  4968.                                          dst_rect->height,
  4969.                                          VA_RT_FORMAT_YUV420,
  4970.                                          1,
  4971.                                          &out_surface_id);
  4972.             assert(status == VA_STATUS_SUCCESS);
  4973.             obj_surface = SURFACE(out_surface_id);
  4974.             assert(obj_surface);
  4975.             i965_check_alloc_surface_bo(ctx, obj_surface, 0, VA_FOURCC_NV12, SUBSAMPLE_YUV420);
  4976.             i965_vpp_clear_surface(ctx, pp_context, obj_surface, 0);
  4977.  
  4978.             dst_surface.base = (struct object_base *)obj_surface;
  4979.             dst_surface.type = I965_SURFACE_TYPE_SURFACE;
  4980.             dst_surface.flags = I965_SURFACE_FLAG_FRAME;
  4981.  
  4982.             i965_post_processing_internal(ctx, pp_context,
  4983.                                           &src_surface,
  4984.                                           src_rect,
  4985.                                           &dst_surface,
  4986.                                           &tmp_dst_rect,
  4987.                                           PP_NV12_AVS,
  4988.                                           NULL);
  4989.  
  4990.             if (tmp_id != VA_INVALID_ID)
  4991.                 i965_DestroySurfaces(ctx, &tmp_id, 1);
  4992.                
  4993.             *has_done_scaling = 1;
  4994.             calibrated_rect->x = 0;
  4995.             calibrated_rect->y = 0;
  4996.             calibrated_rect->width = dst_rect->width;
  4997.             calibrated_rect->height = dst_rect->height;
  4998.         }
  4999.  
  5000.         _i965UnlockMutex(&i965->pp_mutex);
  5001.     }
  5002.  
  5003.     return out_surface_id;
  5004. }      
  5005.  
  5006. static VAStatus
  5007. i965_image_pl2_processing(VADriverContextP ctx,
  5008.                           const struct i965_surface *src_surface,
  5009.                           const VARectangle *src_rect,
  5010.                           struct i965_surface *dst_surface,
  5011.                           const VARectangle *dst_rect);
  5012.  
  5013. static VAStatus
  5014. i965_image_plx_nv12_plx_processing(VADriverContextP ctx,
  5015.                                    VAStatus (*i965_image_plx_nv12_processing)(
  5016.                                        VADriverContextP,
  5017.                                        const struct i965_surface *,
  5018.                                        const VARectangle *,
  5019.                                        struct i965_surface *,
  5020.                                        const VARectangle *),
  5021.                                    const struct i965_surface *src_surface,
  5022.                                    const VARectangle *src_rect,
  5023.                                    struct i965_surface *dst_surface,
  5024.                                    const VARectangle *dst_rect)
  5025. {
  5026.     struct i965_driver_data *i965 = i965_driver_data(ctx);
  5027.     VAStatus status;
  5028.     VASurfaceID tmp_surface_id = VA_INVALID_SURFACE;
  5029.     struct object_surface *obj_surface = NULL;
  5030.     struct i965_surface tmp_surface;
  5031.     int width, height;
  5032.  
  5033.     pp_get_surface_size(ctx, dst_surface, &width, &height);
  5034.     status = i965_CreateSurfaces(ctx,
  5035.                                  width,
  5036.                                  height,
  5037.                                  VA_RT_FORMAT_YUV420,
  5038.                                  1,
  5039.                                  &tmp_surface_id);
  5040.     assert(status == VA_STATUS_SUCCESS);
  5041.     obj_surface = SURFACE(tmp_surface_id);
  5042.     assert(obj_surface);
  5043.     i965_check_alloc_surface_bo(ctx, obj_surface, 0, VA_FOURCC_NV12, SUBSAMPLE_YUV420);
  5044.  
  5045.     tmp_surface.base = (struct object_base *)obj_surface;
  5046.     tmp_surface.type = I965_SURFACE_TYPE_SURFACE;
  5047.     tmp_surface.flags = I965_SURFACE_FLAG_FRAME;
  5048.  
  5049.     status = i965_image_plx_nv12_processing(ctx,
  5050.                                             src_surface,
  5051.                                             src_rect,
  5052.                                             &tmp_surface,
  5053.                                             dst_rect);
  5054.  
  5055.     if (status == VA_STATUS_SUCCESS)
  5056.         status = i965_image_pl2_processing(ctx,
  5057.                                            &tmp_surface,
  5058.                                            dst_rect,
  5059.                                            dst_surface,
  5060.                                            dst_rect);
  5061.  
  5062.     i965_DestroySurfaces(ctx,
  5063.                          &tmp_surface_id,
  5064.                          1);
  5065.  
  5066.     return status;
  5067. }
  5068.  
  5069.  
  5070. static VAStatus
  5071. i965_image_pl1_rgbx_processing(VADriverContextP ctx,
  5072.                                const struct i965_surface *src_surface,
  5073.                                const VARectangle *src_rect,
  5074.                                struct i965_surface *dst_surface,
  5075.                                const VARectangle *dst_rect)
  5076. {
  5077.     struct i965_driver_data *i965 = i965_driver_data(ctx);
  5078.     struct i965_post_processing_context *pp_context = i965->pp_context;
  5079.     int fourcc = pp_get_surface_fourcc(ctx, dst_surface);
  5080.     VAStatus vaStatus;
  5081.  
  5082.     switch (fourcc) {
  5083.     case VA_FOURCC_NV12:
  5084.         vaStatus = i965_post_processing_internal(ctx, i965->pp_context,
  5085.                                                  src_surface,
  5086.                                                  src_rect,
  5087.                                                  dst_surface,
  5088.                                                  dst_rect,
  5089.                                                  PP_RGBX_LOAD_SAVE_NV12,
  5090.                                                  NULL);
  5091.         intel_batchbuffer_flush(pp_context->batch);
  5092.         break;
  5093.  
  5094.     default:
  5095.         vaStatus = i965_image_plx_nv12_plx_processing(ctx,
  5096.                                                       i965_image_pl1_rgbx_processing,
  5097.                                                       src_surface,
  5098.                                                       src_rect,
  5099.                                                       dst_surface,
  5100.                                                       dst_rect);
  5101.         break;
  5102.     }
  5103.  
  5104.     return vaStatus;
  5105. }
  5106.  
  5107. static VAStatus
  5108. i965_image_pl3_processing(VADriverContextP ctx,
  5109.                           const struct i965_surface *src_surface,
  5110.                           const VARectangle *src_rect,
  5111.                           struct i965_surface *dst_surface,
  5112.                           const VARectangle *dst_rect)
  5113. {
  5114.     struct i965_driver_data *i965 = i965_driver_data(ctx);
  5115.     struct i965_post_processing_context *pp_context = i965->pp_context;
  5116.     int fourcc = pp_get_surface_fourcc(ctx, dst_surface);
  5117.     VAStatus vaStatus = VA_STATUS_ERROR_UNIMPLEMENTED;
  5118.  
  5119.     switch (fourcc) {
  5120.     case VA_FOURCC_NV12:
  5121.         vaStatus = i965_post_processing_internal(ctx, i965->pp_context,
  5122.                                                  src_surface,
  5123.                                                  src_rect,
  5124.                                                  dst_surface,
  5125.                                                  dst_rect,
  5126.                                                  PP_PL3_LOAD_SAVE_N12,
  5127.                                                  NULL);
  5128.         intel_batchbuffer_flush(pp_context->batch);
  5129.         break;
  5130.  
  5131.     case VA_FOURCC_IMC1:
  5132.     case VA_FOURCC_IMC3:
  5133.     case VA_FOURCC_YV12:
  5134.     case VA_FOURCC_I420:
  5135.         vaStatus = i965_post_processing_internal(ctx, i965->pp_context,
  5136.                                                  src_surface,
  5137.                                                  src_rect,
  5138.                                                  dst_surface,
  5139.                                                  dst_rect,
  5140.                                                  PP_PL3_LOAD_SAVE_PL3,
  5141.                                                  NULL);
  5142.         intel_batchbuffer_flush(pp_context->batch);
  5143.         break;
  5144.  
  5145.     case VA_FOURCC_YUY2:
  5146.     case VA_FOURCC_UYVY:
  5147.         vaStatus = i965_post_processing_internal(ctx, i965->pp_context,
  5148.                                                  src_surface,
  5149.                                                  src_rect,
  5150.                                                  dst_surface,
  5151.                                                  dst_rect,
  5152.                                                  PP_PL3_LOAD_SAVE_PA,
  5153.                                                  NULL);
  5154.         intel_batchbuffer_flush(pp_context->batch);
  5155.         break;
  5156.  
  5157.     default:
  5158.         vaStatus = i965_image_plx_nv12_plx_processing(ctx,
  5159.                                                       i965_image_pl3_processing,
  5160.                                                       src_surface,
  5161.                                                       src_rect,
  5162.                                                       dst_surface,
  5163.                                                       dst_rect);
  5164.         break;
  5165.     }
  5166.  
  5167.     return vaStatus;
  5168. }
  5169.  
  5170. static VAStatus
  5171. i965_image_pl2_processing(VADriverContextP ctx,
  5172.                           const struct i965_surface *src_surface,
  5173.                           const VARectangle *src_rect,
  5174.                           struct i965_surface *dst_surface,
  5175.                           const VARectangle *dst_rect)
  5176. {
  5177.     struct i965_driver_data *i965 = i965_driver_data(ctx);
  5178.     struct i965_post_processing_context *pp_context = i965->pp_context;
  5179.     int fourcc = pp_get_surface_fourcc(ctx, dst_surface);
  5180.     VAStatus vaStatus = VA_STATUS_ERROR_UNIMPLEMENTED;
  5181.  
  5182.     switch (fourcc) {
  5183.     case VA_FOURCC_NV12:
  5184.         vaStatus = i965_post_processing_internal(ctx, i965->pp_context,
  5185.                                                  src_surface,
  5186.                                                  src_rect,
  5187.                                                  dst_surface,
  5188.                                                  dst_rect,
  5189.                                                  PP_NV12_LOAD_SAVE_N12,
  5190.                                                  NULL);
  5191.         break;
  5192.  
  5193.     case VA_FOURCC_IMC1:
  5194.     case VA_FOURCC_IMC3:
  5195.     case VA_FOURCC_YV12:
  5196.     case VA_FOURCC_I420:
  5197.         vaStatus = i965_post_processing_internal(ctx, i965->pp_context,
  5198.                                                  src_surface,
  5199.                                                  src_rect,
  5200.                                                  dst_surface,
  5201.                                                  dst_rect,
  5202.                                                  PP_NV12_LOAD_SAVE_PL3,
  5203.                                                  NULL);
  5204.         break;
  5205.  
  5206.     case VA_FOURCC_YUY2:
  5207.     case VA_FOURCC_UYVY:
  5208.         vaStatus = i965_post_processing_internal(ctx, i965->pp_context,
  5209.                                                  src_surface,
  5210.                                                  src_rect,
  5211.                                                  dst_surface,
  5212.                                                  dst_rect,
  5213.                                                  PP_NV12_LOAD_SAVE_PA,
  5214.                                                  NULL);
  5215.         break;
  5216.  
  5217.     case VA_FOURCC_BGRX:
  5218.     case VA_FOURCC_BGRA:
  5219.     case VA_FOURCC_RGBX:
  5220.     case VA_FOURCC_RGBA:
  5221.         vaStatus = i965_post_processing_internal(ctx, i965->pp_context,
  5222.                                                  src_surface,
  5223.                                                  src_rect,
  5224.                                                  dst_surface,
  5225.                                                  dst_rect,
  5226.                                                  PP_NV12_LOAD_SAVE_RGBX,
  5227.                                                  NULL);
  5228.         break;
  5229.  
  5230.     default:
  5231.         return VA_STATUS_ERROR_UNIMPLEMENTED;
  5232.     }
  5233.  
  5234.     intel_batchbuffer_flush(pp_context->batch);
  5235.  
  5236.     return vaStatus;
  5237. }
  5238.  
  5239. static VAStatus
  5240. i965_image_pl1_processing(VADriverContextP ctx,
  5241.                           const struct i965_surface *src_surface,
  5242.                           const VARectangle *src_rect,
  5243.                           struct i965_surface *dst_surface,
  5244.                           const VARectangle *dst_rect)
  5245. {
  5246.     struct i965_driver_data *i965 = i965_driver_data(ctx);
  5247.     struct i965_post_processing_context *pp_context = i965->pp_context;
  5248.     int fourcc = pp_get_surface_fourcc(ctx, dst_surface);
  5249.     VAStatus vaStatus;
  5250.  
  5251.     switch (fourcc) {
  5252.     case VA_FOURCC_NV12:
  5253.         vaStatus = i965_post_processing_internal(ctx, i965->pp_context,
  5254.                                                  src_surface,
  5255.                                                  src_rect,
  5256.                                                  dst_surface,
  5257.                                                  dst_rect,
  5258.                                                  PP_PA_LOAD_SAVE_NV12,
  5259.                                                  NULL);
  5260.         intel_batchbuffer_flush(pp_context->batch);
  5261.         break;
  5262.  
  5263.     case VA_FOURCC_YV12:
  5264.         vaStatus = i965_post_processing_internal(ctx, i965->pp_context,
  5265.                                                  src_surface,
  5266.                                                  src_rect,
  5267.                                                  dst_surface,
  5268.                                                  dst_rect,
  5269.                                                  PP_PA_LOAD_SAVE_PL3,
  5270.                                                  NULL);
  5271.         intel_batchbuffer_flush(pp_context->batch);
  5272.         break;
  5273.  
  5274.     case VA_FOURCC_YUY2:
  5275.     case VA_FOURCC_UYVY:
  5276.         vaStatus = i965_post_processing_internal(ctx, i965->pp_context,
  5277.                                                  src_surface,
  5278.                                                  src_rect,
  5279.                                                  dst_surface,
  5280.                                                  dst_rect,
  5281.                                                  PP_PA_LOAD_SAVE_PA,
  5282.                                                  NULL);
  5283.         intel_batchbuffer_flush(pp_context->batch);
  5284.         break;
  5285.  
  5286.     default:
  5287.         vaStatus = i965_image_plx_nv12_plx_processing(ctx,
  5288.                                                       i965_image_pl1_processing,
  5289.                                                       src_surface,
  5290.                                                       src_rect,
  5291.                                                       dst_surface,
  5292.                                                       dst_rect);
  5293.         break;
  5294.     }
  5295.  
  5296.     return vaStatus;
  5297. }
  5298.  
  5299. VAStatus
  5300. i965_image_processing(VADriverContextP ctx,
  5301.                       const struct i965_surface *src_surface,
  5302.                       const VARectangle *src_rect,
  5303.                       struct i965_surface *dst_surface,
  5304.                       const VARectangle *dst_rect)
  5305. {
  5306.     struct i965_driver_data *i965 = i965_driver_data(ctx);
  5307.     VAStatus status = VA_STATUS_ERROR_UNIMPLEMENTED;
  5308.  
  5309.     if (HAS_VPP(i965)) {
  5310.         int fourcc = pp_get_surface_fourcc(ctx, src_surface);
  5311.  
  5312.         _i965LockMutex(&i965->pp_mutex);
  5313.  
  5314.         switch (fourcc) {
  5315.         case VA_FOURCC_YV12:
  5316.         case VA_FOURCC_I420:
  5317.         case VA_FOURCC_IMC1:
  5318.         case VA_FOURCC_IMC3:
  5319.         case VA_FOURCC_422H:
  5320.         case VA_FOURCC_422V:
  5321.         case VA_FOURCC_411P:
  5322.         case VA_FOURCC_444P:
  5323.         case VA_FOURCC_YV16:
  5324.             status = i965_image_pl3_processing(ctx,
  5325.                                                src_surface,
  5326.                                                src_rect,
  5327.                                                dst_surface,
  5328.                                                dst_rect);
  5329.             break;
  5330.  
  5331.         case  VA_FOURCC_NV12:
  5332.             status = i965_image_pl2_processing(ctx,
  5333.                                                src_surface,
  5334.                                                src_rect,
  5335.                                                dst_surface,
  5336.                                                dst_rect);
  5337.             break;
  5338.         case VA_FOURCC_YUY2:
  5339.         case VA_FOURCC_UYVY:
  5340.             status = i965_image_pl1_processing(ctx,
  5341.                                                src_surface,
  5342.                                                src_rect,
  5343.                                                dst_surface,
  5344.                                                dst_rect);
  5345.             break;
  5346.         case VA_FOURCC_BGRA:
  5347.         case VA_FOURCC_BGRX:
  5348.         case VA_FOURCC_RGBA:
  5349.         case VA_FOURCC_RGBX:
  5350.             status = i965_image_pl1_rgbx_processing(ctx,
  5351.                                                src_surface,
  5352.                                                src_rect,
  5353.                                                dst_surface,
  5354.                                                dst_rect);
  5355.             break;
  5356.         default:
  5357.             status = VA_STATUS_ERROR_UNIMPLEMENTED;
  5358.             break;
  5359.         }
  5360.        
  5361.         _i965UnlockMutex(&i965->pp_mutex);
  5362.     }
  5363.  
  5364.     return status;
  5365. }      
  5366.  
  5367. static void
  5368. i965_post_processing_context_finalize(VADriverContextP ctx,
  5369.     struct i965_post_processing_context *pp_context)
  5370. {
  5371.     int i;
  5372.  
  5373.     dri_bo_unreference(pp_context->surface_state_binding_table.bo);
  5374.     pp_context->surface_state_binding_table.bo = NULL;
  5375.  
  5376.     dri_bo_unreference(pp_context->curbe.bo);
  5377.     pp_context->curbe.bo = NULL;
  5378.  
  5379.     dri_bo_unreference(pp_context->sampler_state_table.bo);
  5380.     pp_context->sampler_state_table.bo = NULL;
  5381.  
  5382.     dri_bo_unreference(pp_context->sampler_state_table.bo_8x8);
  5383.     pp_context->sampler_state_table.bo_8x8 = NULL;
  5384.  
  5385.     dri_bo_unreference(pp_context->sampler_state_table.bo_8x8_uv);
  5386.     pp_context->sampler_state_table.bo_8x8_uv = NULL;
  5387.  
  5388.     dri_bo_unreference(pp_context->idrt.bo);
  5389.     pp_context->idrt.bo = NULL;
  5390.     pp_context->idrt.num_interface_descriptors = 0;
  5391.  
  5392.     dri_bo_unreference(pp_context->vfe_state.bo);
  5393.     pp_context->vfe_state.bo = NULL;
  5394.  
  5395.     for (i = 0; i < ARRAY_ELEMS(pp_context->pp_dndi_context.frame_store); i++)
  5396.         pp_dndi_frame_store_clear(&pp_context->pp_dndi_context.frame_store[i],
  5397.             ctx);
  5398.  
  5399.     dri_bo_unreference(pp_context->pp_dn_context.stmm_bo);
  5400.     pp_context->pp_dn_context.stmm_bo = NULL;
  5401.  
  5402.     for (i = 0; i < NUM_PP_MODULES; i++) {
  5403.         struct pp_module *pp_module = &pp_context->pp_modules[i];
  5404.  
  5405.         dri_bo_unreference(pp_module->kernel.bo);
  5406.         pp_module->kernel.bo = NULL;
  5407.     }
  5408.  
  5409.     free(pp_context->pp_static_parameter);
  5410.     free(pp_context->pp_inline_parameter);
  5411.     pp_context->pp_static_parameter = NULL;
  5412.     pp_context->pp_inline_parameter = NULL;
  5413. }
  5414.  
  5415. void
  5416. i965_post_processing_terminate(VADriverContextP ctx)
  5417. {
  5418.     struct i965_driver_data *i965 = i965_driver_data(ctx);
  5419.     struct i965_post_processing_context *pp_context = i965->pp_context;
  5420.  
  5421.     if (pp_context) {
  5422.         pp_context->finalize(ctx, pp_context);
  5423.         free(pp_context);
  5424.     }
  5425.  
  5426.     i965->pp_context = NULL;
  5427. }
  5428.  
  5429. #define VPP_CURBE_ALLOCATION_SIZE       32
  5430.  
  5431. void
  5432. i965_post_processing_context_init(VADriverContextP ctx,
  5433.                                   void *data,
  5434.                                   struct intel_batchbuffer *batch)
  5435. {
  5436.     struct i965_driver_data *i965 = i965_driver_data(ctx);
  5437.     int i;
  5438.     struct i965_post_processing_context *pp_context = data;
  5439.     const AVSConfig *avs_config;
  5440.  
  5441.     if (IS_IRONLAKE(i965->intel.device_info)) {
  5442.         pp_context->urb.size = i965->intel.device_info->urb_size;
  5443.         pp_context->urb.num_vfe_entries = 32;
  5444.         pp_context->urb.size_vfe_entry = 1;     /* in 512 bits unit */
  5445.         pp_context->urb.num_cs_entries = 1;
  5446.         pp_context->urb.size_cs_entry = 2;
  5447.         pp_context->urb.vfe_start = 0;
  5448.         pp_context->urb.cs_start = pp_context->urb.vfe_start +
  5449.             pp_context->urb.num_vfe_entries * pp_context->urb.size_vfe_entry;
  5450.         assert(pp_context->urb.cs_start +
  5451.            pp_context->urb.num_cs_entries * pp_context->urb.size_cs_entry <= i965->intel.device_info->urb_size);
  5452.         pp_context->intel_post_processing = ironlake_post_processing;
  5453.     } else {
  5454.         pp_context->vfe_gpu_state.max_num_threads = 60;
  5455.         pp_context->vfe_gpu_state.num_urb_entries = 59;
  5456.         pp_context->vfe_gpu_state.gpgpu_mode = 0;
  5457.         pp_context->vfe_gpu_state.urb_entry_size = 16 - 1;
  5458.         pp_context->vfe_gpu_state.curbe_allocation_size = VPP_CURBE_ALLOCATION_SIZE;
  5459.         pp_context->intel_post_processing = gen6_post_processing;
  5460.     }
  5461.  
  5462.     pp_context->finalize = i965_post_processing_context_finalize;
  5463.  
  5464.     assert(NUM_PP_MODULES == ARRAY_ELEMS(pp_modules_gen5));
  5465.     assert(NUM_PP_MODULES == ARRAY_ELEMS(pp_modules_gen6));
  5466.     assert(NUM_PP_MODULES == ARRAY_ELEMS(pp_modules_gen7));
  5467.     assert(NUM_PP_MODULES == ARRAY_ELEMS(pp_modules_gen75));
  5468.  
  5469.     if (IS_HASWELL(i965->intel.device_info))
  5470.         memcpy(pp_context->pp_modules, pp_modules_gen75, sizeof(pp_context->pp_modules));
  5471.     else if (IS_GEN7(i965->intel.device_info))
  5472.         memcpy(pp_context->pp_modules, pp_modules_gen7, sizeof(pp_context->pp_modules));
  5473.     else if (IS_GEN6(i965->intel.device_info))
  5474.         memcpy(pp_context->pp_modules, pp_modules_gen6, sizeof(pp_context->pp_modules));
  5475.     else if (IS_IRONLAKE(i965->intel.device_info))
  5476.         memcpy(pp_context->pp_modules, pp_modules_gen5, sizeof(pp_context->pp_modules));
  5477.  
  5478.     for (i = 0; i < NUM_PP_MODULES; i++) {
  5479.         struct pp_module *pp_module = &pp_context->pp_modules[i];
  5480.         dri_bo_unreference(pp_module->kernel.bo);
  5481.         if (pp_module->kernel.bin && pp_module->kernel.size) {
  5482.             pp_module->kernel.bo = dri_bo_alloc(i965->intel.bufmgr,
  5483.                                                 pp_module->kernel.name,
  5484.                                                 pp_module->kernel.size,
  5485.                                                 4096);
  5486.             assert(pp_module->kernel.bo);
  5487.             dri_bo_subdata(pp_module->kernel.bo, 0, pp_module->kernel.size, pp_module->kernel.bin);
  5488.         } else {
  5489.             pp_module->kernel.bo = NULL;
  5490.         }
  5491.     }
  5492.  
  5493.     /* static & inline parameters */
  5494.     if (IS_GEN7(i965->intel.device_info)) {
  5495.         pp_context->pp_static_parameter = calloc(sizeof(struct gen7_pp_static_parameter), 1);
  5496.         pp_context->pp_inline_parameter = calloc(sizeof(struct gen7_pp_inline_parameter), 1);
  5497.     } else {
  5498.         pp_context->pp_static_parameter = calloc(sizeof(struct pp_static_parameter), 1);
  5499.         pp_context->pp_inline_parameter = calloc(sizeof(struct pp_inline_parameter), 1);
  5500.     }
  5501.  
  5502.     pp_context->batch = batch;
  5503.     pp_dndi_context_init(&pp_context->pp_dndi_context);
  5504.  
  5505.     avs_config = IS_IRONLAKE(i965->intel.device_info) ? &gen5_avs_config :
  5506.         &gen6_avs_config;
  5507.     avs_init_state(&pp_context->pp_avs_context.state, avs_config);
  5508. }
  5509.  
  5510. bool
  5511. i965_post_processing_init(VADriverContextP ctx)
  5512. {
  5513.     struct i965_driver_data *i965 = i965_driver_data(ctx);
  5514.     struct i965_post_processing_context *pp_context = i965->pp_context;
  5515.  
  5516.     if (HAS_VPP(i965)) {
  5517.         if (pp_context == NULL) {
  5518.             pp_context = calloc(1, sizeof(*pp_context));
  5519.             assert(pp_context);
  5520.             i965->codec_info->post_processing_context_init(ctx, pp_context, i965->pp_batch);
  5521.             i965->pp_context = pp_context;
  5522.         }
  5523.     }
  5524.  
  5525.     return true;
  5526. }
  5527.  
  5528. static const int procfilter_to_pp_flag[VAProcFilterCount] = {
  5529.     PP_NULL,    /* VAProcFilterNone */
  5530.     PP_NV12_DN, /* VAProcFilterNoiseReduction */
  5531.     PP_NV12_DNDI, /* VAProcFilterDeinterlacing */
  5532.     PP_NULL,    /* VAProcFilterSharpening */
  5533.     PP_NULL,    /* VAProcFilterColorBalance */
  5534. };
  5535.  
  5536. static const int proc_frame_to_pp_frame[3] = {
  5537.     I965_SURFACE_FLAG_FRAME,
  5538.     I965_SURFACE_FLAG_TOP_FIELD_FIRST,
  5539.     I965_SURFACE_FLAG_BOTTOME_FIELD_FIRST
  5540. };
  5541.  
  5542. enum {
  5543.     PP_OP_CHANGE_FORMAT = 1 << 0,
  5544.     PP_OP_CHANGE_SIZE   = 1 << 1,
  5545.     PP_OP_DEINTERLACE   = 1 << 2,
  5546.     PP_OP_COMPLEX       = 1 << 3,
  5547. };
  5548.  
  5549. static int
  5550. pp_get_kernel_index(uint32_t src_fourcc, uint32_t dst_fourcc, uint32_t pp_ops,
  5551.     uint32_t filter_flags)
  5552. {
  5553.     int pp_index = -1;
  5554.  
  5555.     if (!dst_fourcc)
  5556.         dst_fourcc = src_fourcc;
  5557.  
  5558.     switch (src_fourcc) {
  5559.     case VA_FOURCC_RGBX:
  5560.     case VA_FOURCC_RGBA:
  5561.     case VA_FOURCC_BGRX:
  5562.     case VA_FOURCC_BGRA:
  5563.         switch (dst_fourcc) {
  5564.         case VA_FOURCC_NV12:
  5565.             pp_index = PP_RGBX_LOAD_SAVE_NV12;
  5566.             break;
  5567.         }
  5568.         break;
  5569.     case VA_FOURCC_YUY2:
  5570.     case VA_FOURCC_UYVY:
  5571.         switch (dst_fourcc) {
  5572.         case VA_FOURCC_NV12:
  5573.             pp_index = PP_PA_LOAD_SAVE_NV12;
  5574.             break;
  5575.         case VA_FOURCC_I420:
  5576.         case VA_FOURCC_YV12:
  5577.             pp_index = PP_PA_LOAD_SAVE_PL3;
  5578.             break;
  5579.         case VA_FOURCC_YUY2:
  5580.         case VA_FOURCC_UYVY:
  5581.             pp_index = PP_PA_LOAD_SAVE_PA;
  5582.             break;
  5583.         }
  5584.         break;
  5585.     case VA_FOURCC_NV12:
  5586.         switch (dst_fourcc) {
  5587.         case VA_FOURCC_NV12:
  5588.             if (pp_ops & PP_OP_CHANGE_SIZE)
  5589.                 pp_index = avs_is_needed(filter_flags) ?
  5590.                     PP_NV12_AVS : PP_NV12_SCALING;
  5591.             else
  5592.                 pp_index = PP_NV12_LOAD_SAVE_N12;
  5593.             break;
  5594.         case VA_FOURCC_I420:
  5595.         case VA_FOURCC_YV12:
  5596.         case VA_FOURCC_IMC1:
  5597.         case VA_FOURCC_IMC3:
  5598.             pp_index = PP_NV12_LOAD_SAVE_PL3;
  5599.             break;
  5600.         case VA_FOURCC_YUY2:
  5601.         case VA_FOURCC_UYVY:
  5602.             pp_index = PP_NV12_LOAD_SAVE_PA;
  5603.             break;
  5604.         case VA_FOURCC_RGBX:
  5605.         case VA_FOURCC_RGBA:
  5606.         case VA_FOURCC_BGRX:
  5607.         case VA_FOURCC_BGRA:
  5608.             pp_index = PP_NV12_LOAD_SAVE_RGBX;
  5609.             break;
  5610.         }
  5611.         break;
  5612.     case VA_FOURCC_I420:
  5613.     case VA_FOURCC_YV12:
  5614.     case VA_FOURCC_IMC1:
  5615.     case VA_FOURCC_IMC3:
  5616.     case VA_FOURCC_YV16:
  5617.     case VA_FOURCC_411P:
  5618.     case VA_FOURCC_422H:
  5619.     case VA_FOURCC_422V:
  5620.     case VA_FOURCC_444P:
  5621.         switch (dst_fourcc) {
  5622.         case VA_FOURCC_NV12:
  5623.             pp_index = PP_PL3_LOAD_SAVE_N12;
  5624.             break;
  5625.         case VA_FOURCC_I420:
  5626.         case VA_FOURCC_YV12:
  5627.         case VA_FOURCC_IMC1:
  5628.         case VA_FOURCC_IMC3:
  5629.             pp_index = PP_PL3_LOAD_SAVE_PL3;
  5630.             break;
  5631.         case VA_FOURCC_YUY2:
  5632.         case VA_FOURCC_UYVY:
  5633.             pp_index = PP_PL3_LOAD_SAVE_PA;
  5634.             break;
  5635.         }
  5636.         break;
  5637.     }
  5638.     return pp_index;
  5639. }
  5640.  
  5641. static VAStatus
  5642. i965_proc_picture_fast(VADriverContextP ctx,
  5643.     struct i965_proc_context *proc_context, struct proc_state *proc_state)
  5644. {
  5645.     struct i965_driver_data * const i965 = i965_driver_data(ctx);
  5646.     const VAProcPipelineParameterBuffer * const pipeline_param =
  5647.         (VAProcPipelineParameterBuffer *)proc_state->pipeline_param->buffer;
  5648.     struct object_surface *src_obj_surface, *dst_obj_surface;
  5649.     struct i965_surface src_surface, dst_surface;
  5650.     const VAProcFilterParameterBufferDeinterlacing *deint_params = NULL;
  5651.     VARectangle src_rect, dst_rect;
  5652.     VAStatus status;
  5653.     uint32_t i, filter_flags = 0, pp_ops = 0;
  5654.     int pp_index;
  5655.  
  5656.     /* Validate pipeline parameters */
  5657.     if (pipeline_param->num_filters > 0 && !pipeline_param->filters)
  5658.         return VA_STATUS_ERROR_INVALID_PARAMETER;
  5659.  
  5660.     for (i = 0; i < pipeline_param->num_filters; i++) {
  5661.         const VAProcFilterParameterBuffer *filter;
  5662.         struct object_buffer * const obj_buffer =
  5663.             BUFFER(pipeline_param->filters[i]);
  5664.  
  5665.         assert(obj_buffer && obj_buffer->buffer_store);
  5666.         if (!obj_buffer || !obj_buffer->buffer_store)
  5667.             return VA_STATUS_ERROR_INVALID_PARAMETER;
  5668.  
  5669.         filter = (VAProcFilterParameterBuffer *)
  5670.             obj_buffer->buffer_store->buffer;
  5671.         switch (filter->type) {
  5672.         case VAProcFilterDeinterlacing:
  5673.             pp_ops |= PP_OP_DEINTERLACE;
  5674.             deint_params = (VAProcFilterParameterBufferDeinterlacing *)filter;
  5675.             break;
  5676.         default:
  5677.             pp_ops |= PP_OP_COMPLEX;
  5678.             break;
  5679.         }
  5680.     }
  5681.     filter_flags |= pipeline_param->filter_flags & VA_FILTER_SCALING_MASK;
  5682.  
  5683.     /* Validate source surface */
  5684.     src_obj_surface = SURFACE(pipeline_param->surface);
  5685.     if (!src_obj_surface)
  5686.         return VA_STATUS_ERROR_INVALID_SURFACE;
  5687.  
  5688.     if (!src_obj_surface->fourcc)
  5689.         return VA_STATUS_ERROR_INVALID_IMAGE_FORMAT;
  5690.  
  5691.     if (pipeline_param->surface_region) {
  5692.         src_rect.x = pipeline_param->surface_region->x;
  5693.         src_rect.y = pipeline_param->surface_region->y;
  5694.         src_rect.width = pipeline_param->surface_region->width;
  5695.         src_rect.height = pipeline_param->surface_region->height;
  5696.     } else {
  5697.         src_rect.x = 0;
  5698.         src_rect.y = 0;
  5699.         src_rect.width = src_obj_surface->orig_width;
  5700.         src_rect.height = src_obj_surface->orig_height;
  5701.     }
  5702.  
  5703.     src_surface.base  = &src_obj_surface->base;
  5704.     src_surface.type  = I965_SURFACE_TYPE_SURFACE;
  5705.     src_surface.flags = I965_SURFACE_FLAG_FRAME;
  5706.  
  5707.     if (pp_ops & PP_OP_DEINTERLACE) {
  5708.         filter_flags |= !(deint_params->flags & VA_DEINTERLACING_BOTTOM_FIELD) ?
  5709.             VA_TOP_FIELD : VA_BOTTOM_FIELD;
  5710.         if (deint_params->algorithm != VAProcDeinterlacingBob)
  5711.             pp_ops |= PP_OP_COMPLEX;
  5712.     }
  5713.     else if (pipeline_param->filter_flags & (VA_TOP_FIELD | VA_BOTTOM_FIELD)) {
  5714.         filter_flags |= (pipeline_param->filter_flags & VA_TOP_FIELD) ?
  5715.             VA_TOP_FIELD : VA_BOTTOM_FIELD;
  5716.         pp_ops |= PP_OP_DEINTERLACE;
  5717.     }
  5718.     if (pp_ops & PP_OP_DEINTERLACE) // XXX: no bob-deinterlacing optimization yet
  5719.         pp_ops |= PP_OP_COMPLEX;
  5720.  
  5721.     /* Validate target surface */
  5722.     dst_obj_surface = SURFACE(proc_state->current_render_target);
  5723.     if (!dst_obj_surface)
  5724.         return VA_STATUS_ERROR_INVALID_SURFACE;
  5725.  
  5726.     if (!dst_obj_surface->bo)
  5727.         return VA_STATUS_ERROR_INVALID_SURFACE;
  5728.  
  5729.     if (dst_obj_surface->fourcc &&
  5730.         dst_obj_surface->fourcc != src_obj_surface->fourcc)
  5731.         pp_ops |= PP_OP_CHANGE_FORMAT;
  5732.  
  5733.     if (pipeline_param->output_region) {
  5734.         dst_rect.x = pipeline_param->output_region->x;
  5735.         dst_rect.y = pipeline_param->output_region->y;
  5736.         dst_rect.width = pipeline_param->output_region->width;
  5737.         dst_rect.height = pipeline_param->output_region->height;
  5738.     } else {
  5739.         dst_rect.x = 0;
  5740.         dst_rect.y = 0;
  5741.         dst_rect.width = dst_obj_surface->orig_width;
  5742.         dst_rect.height = dst_obj_surface->orig_height;
  5743.     }
  5744.  
  5745.     if (dst_rect.width != src_rect.width || dst_rect.height != src_rect.height)
  5746.         pp_ops |= PP_OP_CHANGE_SIZE;
  5747.  
  5748.     dst_surface.base  = &dst_obj_surface->base;
  5749.     dst_surface.type  = I965_SURFACE_TYPE_SURFACE;
  5750.     dst_surface.flags = I965_SURFACE_FLAG_FRAME;
  5751.  
  5752.     /* Validate "fast-path" processing capabilities */
  5753.     if (!IS_GEN7(i965->intel.device_info)) {
  5754.         if ((pp_ops & PP_OP_CHANGE_FORMAT) && (pp_ops & PP_OP_CHANGE_SIZE))
  5755.             return VA_STATUS_ERROR_UNIMPLEMENTED; // temporary surface is needed
  5756.     }
  5757.     if (pipeline_param->pipeline_flags & VA_PROC_PIPELINE_FAST) {
  5758.         filter_flags &= ~VA_FILTER_SCALING_MASK;
  5759.         filter_flags |= VA_FILTER_SCALING_FAST;
  5760.     }
  5761.     else {
  5762.         if (pp_ops & PP_OP_COMPLEX)
  5763.             return VA_STATUS_ERROR_UNIMPLEMENTED; // full pipeline is needed
  5764.         if ((filter_flags & VA_FILTER_SCALING_MASK) > VA_FILTER_SCALING_HQ)
  5765.             return VA_STATUS_ERROR_UNIMPLEMENTED;
  5766.     }
  5767.  
  5768.     pp_index = pp_get_kernel_index(src_obj_surface->fourcc,
  5769.         dst_obj_surface->fourcc, pp_ops, filter_flags);
  5770.     if (pp_index < 0)
  5771.         return VA_STATUS_ERROR_UNIMPLEMENTED;
  5772.  
  5773.     proc_context->pp_context.filter_flags = filter_flags;
  5774.     status = i965_post_processing_internal(ctx, &proc_context->pp_context,
  5775.         &src_surface, &src_rect, &dst_surface, &dst_rect, pp_index, NULL);
  5776.     intel_batchbuffer_flush(proc_context->pp_context.batch);
  5777.     return status;
  5778. }
  5779.  
  5780. VAStatus
  5781. i965_proc_picture(VADriverContextP ctx,
  5782.                   VAProfile profile,
  5783.                   union codec_state *codec_state,
  5784.                   struct hw_context *hw_context)
  5785. {
  5786.     struct i965_driver_data *i965 = i965_driver_data(ctx);
  5787.     struct i965_proc_context *proc_context = (struct i965_proc_context *)hw_context;
  5788.     struct proc_state *proc_state = &codec_state->proc;
  5789.     VAProcPipelineParameterBuffer *pipeline_param = (VAProcPipelineParameterBuffer *)proc_state->pipeline_param->buffer;
  5790.     struct object_surface *obj_surface;
  5791.     struct i965_surface src_surface, dst_surface;
  5792.     VARectangle src_rect, dst_rect;
  5793.     VAStatus status;
  5794.     int i;
  5795.     VASurfaceID tmp_surfaces[VAProcFilterCount + 4];
  5796.     int num_tmp_surfaces = 0;
  5797.     unsigned int tiling = 0, swizzle = 0;
  5798.     int in_width, in_height;
  5799.  
  5800.     status = i965_proc_picture_fast(ctx, proc_context, proc_state);
  5801.     if (status != VA_STATUS_ERROR_UNIMPLEMENTED)
  5802.         return status;
  5803.  
  5804.     if (pipeline_param->surface == VA_INVALID_ID ||
  5805.         proc_state->current_render_target == VA_INVALID_ID) {
  5806.         status = VA_STATUS_ERROR_INVALID_SURFACE;
  5807.         goto error;
  5808.     }
  5809.  
  5810.     obj_surface = SURFACE(pipeline_param->surface);
  5811.  
  5812.     if (!obj_surface) {
  5813.         status = VA_STATUS_ERROR_INVALID_SURFACE;
  5814.         goto error;
  5815.     }
  5816.  
  5817.     if (!obj_surface->bo) {
  5818.         status = VA_STATUS_ERROR_INVALID_VALUE; /* The input surface is created without valid content */
  5819.         goto error;
  5820.     }
  5821.  
  5822.     if (pipeline_param->num_filters && !pipeline_param->filters) {
  5823.         status = VA_STATUS_ERROR_INVALID_PARAMETER;
  5824.         goto error;
  5825.     }
  5826.  
  5827.     in_width = obj_surface->orig_width;
  5828.     in_height = obj_surface->orig_height;
  5829.     dri_bo_get_tiling(obj_surface->bo, &tiling, &swizzle);
  5830.  
  5831.     src_surface.base = (struct object_base *)obj_surface;
  5832.     src_surface.type = I965_SURFACE_TYPE_SURFACE;
  5833.     src_surface.flags = proc_frame_to_pp_frame[pipeline_param->filter_flags & 0x3];
  5834.  
  5835.     VASurfaceID out_surface_id = VA_INVALID_ID;
  5836.     if (obj_surface->fourcc != VA_FOURCC_NV12) {
  5837.         src_surface.base = (struct object_base *)obj_surface;
  5838.         src_surface.type = I965_SURFACE_TYPE_SURFACE;
  5839.         src_surface.flags = I965_SURFACE_FLAG_FRAME;
  5840.         src_rect.x = 0;
  5841.         src_rect.y = 0;
  5842.         src_rect.width = in_width;
  5843.         src_rect.height = in_height;
  5844.  
  5845.         status = i965_CreateSurfaces(ctx,
  5846.                                      in_width,
  5847.                                      in_height,
  5848.                                      VA_RT_FORMAT_YUV420,
  5849.                                      1,
  5850.                                      &out_surface_id);
  5851.         assert(status == VA_STATUS_SUCCESS);
  5852.         tmp_surfaces[num_tmp_surfaces++] = out_surface_id;
  5853.         obj_surface = SURFACE(out_surface_id);
  5854.         assert(obj_surface);
  5855.         i965_check_alloc_surface_bo(ctx, obj_surface, !!tiling, VA_FOURCC_NV12, SUBSAMPLE_YUV420);
  5856.  
  5857.         dst_surface.base = (struct object_base *)obj_surface;
  5858.         dst_surface.type = I965_SURFACE_TYPE_SURFACE;
  5859.         dst_surface.flags = I965_SURFACE_FLAG_FRAME;
  5860.         dst_rect.x = 0;
  5861.         dst_rect.y = 0;
  5862.         dst_rect.width = in_width;
  5863.         dst_rect.height = in_height;
  5864.  
  5865.         status = i965_image_processing(ctx,
  5866.                                        &src_surface,
  5867.                                        &src_rect,
  5868.                                        &dst_surface,
  5869.                                        &dst_rect);
  5870.         assert(status == VA_STATUS_SUCCESS);
  5871.  
  5872.         src_surface.base = (struct object_base *)obj_surface;
  5873.         src_surface.type = I965_SURFACE_TYPE_SURFACE;
  5874.         src_surface.flags = proc_frame_to_pp_frame[pipeline_param->filter_flags & 0x3];
  5875.     }
  5876.  
  5877.     if (pipeline_param->surface_region) {
  5878.         src_rect.x = pipeline_param->surface_region->x;
  5879.         src_rect.y = pipeline_param->surface_region->y;
  5880.         src_rect.width = pipeline_param->surface_region->width;
  5881.         src_rect.height = pipeline_param->surface_region->height;
  5882.     } else {
  5883.         src_rect.x = 0;
  5884.         src_rect.y = 0;
  5885.         src_rect.width = in_width;
  5886.         src_rect.height = in_height;
  5887.     }
  5888.  
  5889.     proc_context->pp_context.pipeline_param = pipeline_param;
  5890.  
  5891.     for (i = 0; i < pipeline_param->num_filters; i++) {
  5892.         struct object_buffer *obj_buffer = BUFFER(pipeline_param->filters[i]);
  5893.         VAProcFilterParameterBufferBase *filter_param = NULL;
  5894.         VAProcFilterType filter_type;
  5895.         int kernel_index;
  5896.  
  5897.         if (!obj_buffer ||
  5898.             !obj_buffer->buffer_store ||
  5899.             !obj_buffer->buffer_store->buffer) {
  5900.             status = VA_STATUS_ERROR_INVALID_FILTER_CHAIN;
  5901.             goto error;
  5902.         }
  5903.  
  5904.         out_surface_id = VA_INVALID_ID;
  5905.         filter_param = (VAProcFilterParameterBufferBase *)obj_buffer->buffer_store->buffer;
  5906.         filter_type = filter_param->type;
  5907.         kernel_index = procfilter_to_pp_flag[filter_type];
  5908.  
  5909.         if (kernel_index != PP_NULL &&
  5910.             proc_context->pp_context.pp_modules[kernel_index].kernel.bo != NULL) {
  5911.             status = i965_CreateSurfaces(ctx,
  5912.                                          in_width,
  5913.                                          in_height,
  5914.                                          VA_RT_FORMAT_YUV420,
  5915.                                          1,
  5916.                                          &out_surface_id);
  5917.             assert(status == VA_STATUS_SUCCESS);
  5918.             tmp_surfaces[num_tmp_surfaces++] = out_surface_id;
  5919.             obj_surface = SURFACE(out_surface_id);
  5920.             assert(obj_surface);
  5921.             i965_check_alloc_surface_bo(ctx, obj_surface, !!tiling, VA_FOURCC_NV12, SUBSAMPLE_YUV420);
  5922.             dst_surface.base = (struct object_base *)obj_surface;
  5923.             dst_surface.type = I965_SURFACE_TYPE_SURFACE;
  5924.             status = i965_post_processing_internal(ctx, &proc_context->pp_context,
  5925.                                                    &src_surface,
  5926.                                                    &src_rect,
  5927.                                                    &dst_surface,
  5928.                                                    &src_rect,
  5929.                                                    kernel_index,
  5930.                                                    filter_param);
  5931.  
  5932.             if (status == VA_STATUS_SUCCESS) {
  5933.                 src_surface.base = dst_surface.base;
  5934.                 src_surface.type = dst_surface.type;
  5935.                 src_surface.flags = dst_surface.flags;
  5936.             }
  5937.         }
  5938.     }
  5939.  
  5940.     proc_context->pp_context.pipeline_param = NULL;
  5941.     obj_surface = SURFACE(proc_state->current_render_target);
  5942.    
  5943.     if (!obj_surface) {
  5944.         status = VA_STATUS_ERROR_INVALID_SURFACE;
  5945.         goto error;
  5946.     }
  5947.  
  5948.     if (pipeline_param->output_region) {
  5949.         dst_rect.x = pipeline_param->output_region->x;
  5950.         dst_rect.y = pipeline_param->output_region->y;
  5951.         dst_rect.width = pipeline_param->output_region->width;
  5952.         dst_rect.height = pipeline_param->output_region->height;
  5953.     } else {
  5954.         dst_rect.x = 0;
  5955.         dst_rect.y = 0;
  5956.         dst_rect.width = obj_surface->orig_width;
  5957.         dst_rect.height = obj_surface->orig_height;
  5958.     }
  5959.  
  5960.     if (IS_GEN7(i965->intel.device_info) ||
  5961.         IS_GEN8(i965->intel.device_info) ||
  5962.         IS_GEN9(i965->intel.device_info)) {
  5963.         unsigned int saved_filter_flag;
  5964.         struct i965_post_processing_context *i965pp_context = i965->pp_context;
  5965.  
  5966.         if (obj_surface->fourcc == 0) {
  5967.             i965_check_alloc_surface_bo(ctx, obj_surface, 1,
  5968.                                         VA_FOURCC_NV12,
  5969.                                         SUBSAMPLE_YUV420);
  5970.         }
  5971.  
  5972.         i965_vpp_clear_surface(ctx, &proc_context->pp_context,
  5973.                                obj_surface,
  5974.                                pipeline_param->output_background_color);
  5975.  
  5976.         intel_batchbuffer_flush(hw_context->batch);
  5977.  
  5978.         saved_filter_flag = i965pp_context->filter_flags;
  5979.         i965pp_context->filter_flags = (pipeline_param->filter_flags & VA_FILTER_SCALING_MASK);
  5980.  
  5981.         dst_surface.base = (struct object_base *)obj_surface;
  5982.         dst_surface.type = I965_SURFACE_TYPE_SURFACE;
  5983.         i965_image_processing(ctx, &src_surface, &src_rect, &dst_surface, &dst_rect);
  5984.  
  5985.         i965pp_context->filter_flags = saved_filter_flag;
  5986.  
  5987.         if (num_tmp_surfaces)
  5988.             i965_DestroySurfaces(ctx,
  5989.                              tmp_surfaces,
  5990.                              num_tmp_surfaces);
  5991.  
  5992.         return VA_STATUS_SUCCESS;
  5993.     }
  5994.  
  5995.     int csc_needed = 0;
  5996.     if (obj_surface->fourcc && obj_surface->fourcc !=  VA_FOURCC_NV12){
  5997.         csc_needed = 1;
  5998.         out_surface_id = VA_INVALID_ID;
  5999.         status = i965_CreateSurfaces(ctx,
  6000.                                      obj_surface->orig_width,
  6001.                                      obj_surface->orig_height,
  6002.                                      VA_RT_FORMAT_YUV420,
  6003.                                      1,
  6004.                                      &out_surface_id);
  6005.         assert(status == VA_STATUS_SUCCESS);
  6006.         tmp_surfaces[num_tmp_surfaces++] = out_surface_id;
  6007.         struct object_surface *csc_surface = SURFACE(out_surface_id);
  6008.         assert(csc_surface);
  6009.         i965_check_alloc_surface_bo(ctx, csc_surface, !!tiling, VA_FOURCC_NV12, SUBSAMPLE_YUV420);
  6010.         dst_surface.base = (struct object_base *)csc_surface;
  6011.     } else {
  6012.         i965_check_alloc_surface_bo(ctx, obj_surface, !!tiling, VA_FOURCC_NV12, SUBSAMPLE_YUV420);
  6013.         dst_surface.base = (struct object_base *)obj_surface;
  6014.     }
  6015.  
  6016.     dst_surface.type = I965_SURFACE_TYPE_SURFACE;
  6017.     i965_vpp_clear_surface(ctx, &proc_context->pp_context, obj_surface, pipeline_param->output_background_color);
  6018.  
  6019.     // load/save doesn't support different origin offset for src and dst surface
  6020.     if (src_rect.width == dst_rect.width &&
  6021.         src_rect.height == dst_rect.height &&
  6022.         src_rect.x == dst_rect.x &&
  6023.         src_rect.y == dst_rect.y) {
  6024.         i965_post_processing_internal(ctx, &proc_context->pp_context,
  6025.                                       &src_surface,
  6026.                                       &src_rect,
  6027.                                       &dst_surface,
  6028.                                       &dst_rect,
  6029.                                       PP_NV12_LOAD_SAVE_N12,
  6030.                                       NULL);
  6031.     } else {
  6032.  
  6033.         proc_context->pp_context.filter_flags = pipeline_param->filter_flags;
  6034.         i965_post_processing_internal(ctx, &proc_context->pp_context,
  6035.                                       &src_surface,
  6036.                                       &src_rect,
  6037.                                       &dst_surface,
  6038.                                       &dst_rect,
  6039.                                       avs_is_needed(pipeline_param->filter_flags) ? PP_NV12_AVS : PP_NV12_SCALING,
  6040.                                       NULL);
  6041.     }
  6042.  
  6043.     if (csc_needed) {
  6044.         src_surface.base = dst_surface.base;
  6045.         src_surface.type = dst_surface.type;
  6046.         src_surface.flags = dst_surface.flags;
  6047.         dst_surface.base = (struct object_base *)obj_surface;
  6048.         dst_surface.type = I965_SURFACE_TYPE_SURFACE;
  6049.         i965_image_processing(ctx, &src_surface, &dst_rect, &dst_surface, &dst_rect);
  6050.     }
  6051.    
  6052.     if (num_tmp_surfaces)
  6053.         i965_DestroySurfaces(ctx,
  6054.                              tmp_surfaces,
  6055.                              num_tmp_surfaces);
  6056.  
  6057.     intel_batchbuffer_flush(hw_context->batch);
  6058.  
  6059.     return VA_STATUS_SUCCESS;
  6060.  
  6061. error:
  6062.     if (num_tmp_surfaces)
  6063.         i965_DestroySurfaces(ctx,
  6064.                              tmp_surfaces,
  6065.                              num_tmp_surfaces);
  6066.  
  6067.     return status;
  6068. }
  6069.  
  6070. static void
  6071. i965_proc_context_destroy(void *hw_context)
  6072. {
  6073.     struct i965_proc_context * const proc_context = hw_context;
  6074.     VADriverContextP const ctx = proc_context->driver_context;
  6075.  
  6076.     proc_context->pp_context.finalize(ctx, &proc_context->pp_context);
  6077.     intel_batchbuffer_free(proc_context->base.batch);
  6078.     free(proc_context);
  6079. }
  6080.  
  6081. struct hw_context *
  6082. i965_proc_context_init(VADriverContextP ctx, struct object_config *obj_config)
  6083. {
  6084.     struct i965_driver_data *i965 = i965_driver_data(ctx);
  6085.     struct intel_driver_data *intel = intel_driver_data(ctx);
  6086.     struct i965_proc_context *proc_context = calloc(1, sizeof(struct i965_proc_context));
  6087.  
  6088.     if (!proc_context)
  6089.         return NULL;
  6090.  
  6091.     proc_context->base.destroy = i965_proc_context_destroy;
  6092.     proc_context->base.run = i965_proc_picture;
  6093.     proc_context->base.batch = intel_batchbuffer_new(intel, I915_EXEC_RENDER, 0);
  6094.     proc_context->driver_context = ctx;
  6095.     i965->codec_info->post_processing_context_init(ctx, &proc_context->pp_context, proc_context->base.batch);
  6096.  
  6097.     return (struct hw_context *)proc_context;
  6098. }
  6099.  
  6100.  
  6101.