Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (c) 2011 Intel Corporation. All Rights Reserved.
      3  * Copyright (c) Imagination Technologies Limited, UK
      4  *
      5  * Permission is hereby granted, free of charge, to any person obtaining a
      6  * copy of this software and associated documentation files (the
      7  * "Software"), to deal in the Software without restriction, including
      8  * without limitation the rights to use, copy, modify, merge, publish,
      9  * distribute, sub license, and/or sell copies of the Software, and to
     10  * permit persons to whom the Software is furnished to do so, subject to
     11  * the following conditions:
     12  *
     13  * The above copyright notice and this permission notice (including the
     14  * next paragraph) shall be included in all copies or substantial portions
     15  * of the Software.
     16  *
     17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
     18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
     20  * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
     21  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
     22  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
     23  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     24  *
     25  * Authors:
     26  *    Waldo Bastian <waldo.bastian (at) intel.com>
     27  *    Li Zeng <li.zeng (at) intel.com>
     28  *
     29  */
     30 
     31 #include "pnw_MPEG2.h"
     32 #include "tng_vld_dec.h"
     33 #include "psb_def.h"
     34 #include "psb_drv_debug.h"
     35 
     36 #include "hwdefs/reg_io2.h"
     37 #include "hwdefs/msvdx_offsets.h"
     38 #include "hwdefs/msvdx_cmds_io2.h"
     39 #include "hwdefs/msvdx_vec_reg_io2.h"
     40 #include "hwdefs/msvdx_vec_mpeg2_reg_io2.h"
     41 #include "hwdefs/dxva_fw_ctrl.h"
     42 
     43 #include <stdlib.h>
     44 #include <stdint.h>
     45 #include <string.h>
     46 
     47 #define GET_SURFACE_INFO_is_defined(psb_surface) ((int) (psb_surface->extra_info[0]))
     48 #define SET_SURFACE_INFO_is_defined(psb_surface, val) psb_surface->extra_info[0] = (uint32_t) val;
     49 #define GET_SURFACE_INFO_picture_structure(psb_surface) (psb_surface->extra_info[1])
     50 #define SET_SURFACE_INFO_picture_structure(psb_surface, val) psb_surface->extra_info[1] = val;
     51 #define GET_SURFACE_INFO_picture_coding_type(psb_surface) ((int) (psb_surface->extra_info[2]))
     52 #define SET_SURFACE_INFO_picture_coding_type(psb_surface, val) psb_surface->extra_info[2] = (uint32_t) val;
     53 
     54 #define SLICEDATA_BUFFER_TYPE(type) ((type==VASliceDataBufferType)?"VASliceDataBufferType":"VAProtectedSliceDataBufferType")
     55 
     56 /*
     57  * Frame types - format dependant!
     58  */
     59 #define PICTURE_CODING_I    0x01
     60 #define PICTURE_CODING_P    0x02
     61 #define PICTURE_CODING_B    0x03
     62 /* A special syntax is defined for D-pictures (picture_coding_type = 4). D-pictures are like I-pictures with only Intra-DC coefficients,
     63    no End of Block, and a special end_of_macroblock code '1'. PICTURE_CODING_D is not allowed in MPEG2 */
     64 #define PICTURE_CODING_D    0x04
     65 
     66 /************************************************************************************/
     67 /*                Variable length codes in 'packed' format                            */
     68 /************************************************************************************/
     69 
     70 /* Format is: opcode, width, symbol. All VLC tables are concatenated. Index            */
     71 /* infomation is stored in gui16mpeg2VlcIndexData[]                                    */
     72 #define VLC_PACK(a,b,c)         ( ( (a) << 12 ) | ( (b) << 9  ) | (c) )
     73 static const IMG_UINT16 gaui16mpeg2VlcTableDataPacked[] = {
     74     VLC_PACK(6 , 0 , 0) ,
     75     VLC_PACK(0 , 0 , 6) ,
     76     VLC_PACK(4 , 2 , 4) ,
     77     VLC_PACK(4 , 3 , 5) ,
     78     VLC_PACK(4 , 4 , 6) ,
     79     VLC_PACK(4 , 5 , 7) ,
     80     VLC_PACK(1 , 1 , 3) ,
     81     VLC_PACK(4 , 0 , 0) ,
     82     VLC_PACK(4 , 0 , 3) ,
     83     VLC_PACK(4 , 0 , 8) ,
     84     VLC_PACK(4 , 1 , 9) ,
     85     VLC_PACK(5 , 0 , 5) ,
     86     VLC_PACK(5 , 0 , 0) ,
     87     VLC_PACK(4 , 1 , 2) ,
     88     VLC_PACK(4 , 2 , 3) ,
     89     VLC_PACK(4 , 3 , 4) ,
     90     VLC_PACK(4 , 4 , 5) ,
     91     VLC_PACK(4 , 5 , 6) ,
     92     VLC_PACK(1 , 2 , 1) ,
     93     VLC_PACK(4 , 0 , 7) ,
     94     VLC_PACK(4 , 1 , 8) ,
     95     VLC_PACK(4 , 2 , 9) ,
     96     VLC_PACK(5 , 0 , 5) ,
     97     VLC_PACK(0 , 2 , 16) ,
     98     VLC_PACK(0 , 1 , 87) ,
     99     VLC_PACK(2 , 3 , 90) ,
    100     VLC_PACK(0 , 0 , 98) ,
    101     VLC_PACK(5 , 0 , 253) ,
    102     VLC_PACK(5 , 0 , 366) ,
    103     VLC_PACK(4 , 3 , 380) ,
    104     VLC_PACK(4 , 3 , 381) ,
    105     VLC_PACK(4 , 1 , 508) ,
    106     VLC_PACK(4 , 1 , 508) ,
    107     VLC_PACK(4 , 1 , 508) ,
    108     VLC_PACK(4 , 1 , 508) ,
    109     VLC_PACK(4 , 1 , 509) ,
    110     VLC_PACK(4 , 1 , 509) ,
    111     VLC_PACK(4 , 1 , 509) ,
    112     VLC_PACK(4 , 1 , 509) ,
    113     VLC_PACK(0 , 4 , 8) ,
    114     VLC_PACK(0 , 2 , 63) ,
    115     VLC_PACK(4 , 1 , 255) ,
    116     VLC_PACK(4 , 1 , 255) ,
    117     VLC_PACK(5 , 0 , 365) ,
    118     VLC_PACK(5 , 0 , 470) ,
    119     VLC_PACK(5 , 0 , 251) ,
    120     VLC_PACK(5 , 0 , 471) ,
    121     VLC_PACK(3 , 4 , 0) ,
    122     VLC_PACK(0 , 1 , 31) ,
    123     VLC_PACK(0 , 0 , 40) ,
    124     VLC_PACK(2 , 2 , 41) ,
    125     VLC_PACK(5 , 2 , 224) ,
    126     VLC_PACK(5 , 2 , 228) ,
    127     VLC_PACK(5 , 2 , 232) ,
    128     VLC_PACK(5 , 2 , 236) ,
    129     VLC_PACK(5 , 1 , 437) ,
    130     VLC_PACK(0 , 0 , 39) ,
    131     VLC_PACK(0 , 0 , 40) ,
    132     VLC_PACK(0 , 0 , 41) ,
    133     VLC_PACK(5 , 1 , 241) ,
    134     VLC_PACK(0 , 0 , 41) ,
    135     VLC_PACK(5 , 1 , 454) ,
    136     VLC_PACK(5 , 1 , 456) ,
    137     VLC_PACK(5 , 0 , 244) ,
    138     VLC_PACK(5 , 0 , 439) ,
    139     VLC_PACK(5 , 0 , 348) ,
    140     VLC_PACK(5 , 0 , 245) ,
    141     VLC_PACK(5 , 0 , 363) ,
    142     VLC_PACK(5 , 0 , 325) ,
    143     VLC_PACK(5 , 0 , 458) ,
    144     VLC_PACK(5 , 0 , 459) ,
    145     VLC_PACK(5 , 0 , 246) ,
    146     VLC_PACK(5 , 0 , 460) ,
    147     VLC_PACK(5 , 0 , 461) ,
    148     VLC_PACK(5 , 0 , 186) ,
    149     VLC_PACK(5 , 0 , 356) ,
    150     VLC_PACK(5 , 0 , 247) ,
    151     VLC_PACK(5 , 0 , 333) ,
    152     VLC_PACK(5 , 0 , 462) ,
    153     VLC_PACK(5 , 2 , 173) ,
    154     VLC_PACK(2 , 1 , 3) ,
    155     VLC_PACK(1 , 1 , 5) ,
    156     VLC_PACK(5 , 2 , 449) ,
    157     VLC_PACK(5 , 1 , 432) ,
    158     VLC_PACK(5 , 0 , 431) ,
    159     VLC_PACK(5 , 0 , 332) ,
    160     VLC_PACK(5 , 1 , 434) ,
    161     VLC_PACK(5 , 0 , 436) ,
    162     VLC_PACK(5 , 0 , 448) ,
    163     VLC_PACK(5 , 2 , 215) ,
    164     VLC_PACK(5 , 2 , 219) ,
    165     VLC_PACK(5 , 2 , 180) ,
    166     VLC_PACK(5 , 1 , 178) ,
    167     VLC_PACK(5 , 0 , 177) ,
    168     VLC_PACK(5 , 0 , 223) ,
    169     VLC_PACK(5 , 0 , 340) ,
    170     VLC_PACK(5 , 0 , 355) ,
    171     VLC_PACK(5 , 0 , 362) ,
    172     VLC_PACK(5 , 0 , 184) ,
    173     VLC_PACK(5 , 0 , 185) ,
    174     VLC_PACK(5 , 0 , 240) ,
    175     VLC_PACK(5 , 0 , 243) ,
    176     VLC_PACK(5 , 0 , 453) ,
    177     VLC_PACK(5 , 0 , 463) ,
    178     VLC_PACK(5 , 0 , 341) ,
    179     VLC_PACK(5 , 0 , 248) ,
    180     VLC_PACK(5 , 0 , 364) ,
    181     VLC_PACK(5 , 0 , 187) ,
    182     VLC_PACK(5 , 0 , 464) ,
    183     VLC_PACK(5 , 0 , 465) ,
    184     VLC_PACK(5 , 0 , 349) ,
    185     VLC_PACK(5 , 0 , 326) ,
    186     VLC_PACK(5 , 0 , 334) ,
    187     VLC_PACK(5 , 0 , 189) ,
    188     VLC_PACK(5 , 0 , 342) ,
    189     VLC_PACK(5 , 0 , 252) ,
    190     VLC_PACK(0 , 1 , 4) ,
    191     VLC_PACK(5 , 1 , 467) ,
    192     VLC_PACK(5 , 0 , 249) ,
    193     VLC_PACK(5 , 0 , 466) ,
    194     VLC_PACK(5 , 0 , 357) ,
    195     VLC_PACK(5 , 0 , 188) ,
    196     VLC_PACK(5 , 0 , 250) ,
    197     VLC_PACK(5 , 0 , 469) ,
    198     VLC_PACK(5 , 0 , 350) ,
    199     VLC_PACK(5 , 0 , 358) ,
    200     VLC_PACK(0 , 2 , 16) ,
    201     VLC_PACK(0 , 1 , 87) ,
    202     VLC_PACK(2 , 3 , 90) ,
    203     VLC_PACK(0 , 0 , 98) ,
    204     VLC_PACK(5 , 0 , 253) ,
    205     VLC_PACK(5 , 0 , 366) ,
    206     VLC_PACK(4 , 3 , 380) ,
    207     VLC_PACK(4 , 3 , 381) ,
    208     VLC_PACK(4 , 1 , 254) ,
    209     VLC_PACK(4 , 1 , 254) ,
    210     VLC_PACK(4 , 1 , 254) ,
    211     VLC_PACK(4 , 1 , 254) ,
    212     VLC_PACK(4 , 2 , 508) ,
    213     VLC_PACK(4 , 2 , 508) ,
    214     VLC_PACK(4 , 2 , 509) ,
    215     VLC_PACK(4 , 2 , 509) ,
    216     VLC_PACK(0 , 4 , 8) ,
    217     VLC_PACK(0 , 2 , 63) ,
    218     VLC_PACK(4 , 1 , 255) ,
    219     VLC_PACK(4 , 1 , 255) ,
    220     VLC_PACK(5 , 0 , 365) ,
    221     VLC_PACK(5 , 0 , 470) ,
    222     VLC_PACK(5 , 0 , 251) ,
    223     VLC_PACK(5 , 0 , 471) ,
    224     VLC_PACK(3 , 4 , 0) ,
    225     VLC_PACK(0 , 1 , 31) ,
    226     VLC_PACK(0 , 0 , 40) ,
    227     VLC_PACK(2 , 2 , 41) ,
    228     VLC_PACK(5 , 2 , 224) ,
    229     VLC_PACK(5 , 2 , 228) ,
    230     VLC_PACK(5 , 2 , 232) ,
    231     VLC_PACK(5 , 2 , 236) ,
    232     VLC_PACK(5 , 1 , 437) ,
    233     VLC_PACK(0 , 0 , 39) ,
    234     VLC_PACK(0 , 0 , 40) ,
    235     VLC_PACK(0 , 0 , 41) ,
    236     VLC_PACK(5 , 1 , 241) ,
    237     VLC_PACK(0 , 0 , 41) ,
    238     VLC_PACK(5 , 1 , 454) ,
    239     VLC_PACK(5 , 1 , 456) ,
    240     VLC_PACK(5 , 0 , 244) ,
    241     VLC_PACK(5 , 0 , 439) ,
    242     VLC_PACK(5 , 0 , 348) ,
    243     VLC_PACK(5 , 0 , 245) ,
    244     VLC_PACK(5 , 0 , 363) ,
    245     VLC_PACK(5 , 0 , 325) ,
    246     VLC_PACK(5 , 0 , 458) ,
    247     VLC_PACK(5 , 0 , 459) ,
    248     VLC_PACK(5 , 0 , 246) ,
    249     VLC_PACK(5 , 0 , 460) ,
    250     VLC_PACK(5 , 0 , 461) ,
    251     VLC_PACK(5 , 0 , 186) ,
    252     VLC_PACK(5 , 0 , 356) ,
    253     VLC_PACK(5 , 0 , 247) ,
    254     VLC_PACK(5 , 0 , 333) ,
    255     VLC_PACK(5 , 0 , 462) ,
    256     VLC_PACK(5 , 2 , 173) ,
    257     VLC_PACK(2 , 1 , 3) ,
    258     VLC_PACK(1 , 1 , 5) ,
    259     VLC_PACK(5 , 2 , 449) ,
    260     VLC_PACK(5 , 1 , 432) ,
    261     VLC_PACK(5 , 0 , 431) ,
    262     VLC_PACK(5 , 0 , 332) ,
    263     VLC_PACK(5 , 1 , 434) ,
    264     VLC_PACK(5 , 0 , 436) ,
    265     VLC_PACK(5 , 0 , 448) ,
    266     VLC_PACK(5 , 2 , 215) ,
    267     VLC_PACK(5 , 2 , 219) ,
    268     VLC_PACK(5 , 2 , 180) ,
    269     VLC_PACK(5 , 1 , 178) ,
    270     VLC_PACK(5 , 0 , 177) ,
    271     VLC_PACK(5 , 0 , 223) ,
    272     VLC_PACK(5 , 0 , 340) ,
    273     VLC_PACK(5 , 0 , 355) ,
    274     VLC_PACK(5 , 0 , 362) ,
    275     VLC_PACK(5 , 0 , 184) ,
    276     VLC_PACK(5 , 0 , 185) ,
    277     VLC_PACK(5 , 0 , 240) ,
    278     VLC_PACK(5 , 0 , 243) ,
    279     VLC_PACK(5 , 0 , 453) ,
    280     VLC_PACK(5 , 0 , 463) ,
    281     VLC_PACK(5 , 0 , 341) ,
    282     VLC_PACK(5 , 0 , 248) ,
    283     VLC_PACK(5 , 0 , 364) ,
    284     VLC_PACK(5 , 0 , 187) ,
    285     VLC_PACK(5 , 0 , 464) ,
    286     VLC_PACK(5 , 0 , 465) ,
    287     VLC_PACK(5 , 0 , 349) ,
    288     VLC_PACK(5 , 0 , 326) ,
    289     VLC_PACK(5 , 0 , 334) ,
    290     VLC_PACK(5 , 0 , 189) ,
    291     VLC_PACK(5 , 0 , 342) ,
    292     VLC_PACK(5 , 0 , 252) ,
    293     VLC_PACK(0 , 1 , 4) ,
    294     VLC_PACK(5 , 1 , 467) ,
    295     VLC_PACK(5 , 0 , 249) ,
    296     VLC_PACK(5 , 0 , 466) ,
    297     VLC_PACK(5 , 0 , 357) ,
    298     VLC_PACK(5 , 0 , 188) ,
    299     VLC_PACK(5 , 0 , 250) ,
    300     VLC_PACK(5 , 0 , 469) ,
    301     VLC_PACK(5 , 0 , 350) ,
    302     VLC_PACK(5 , 0 , 358) ,
    303     VLC_PACK(2 , 2 , 32) ,
    304     VLC_PACK(0 , 1 , 87) ,
    305     VLC_PACK(5 , 1 , 248) ,
    306     VLC_PACK(0 , 0 , 89) ,
    307     VLC_PACK(1 , 2 , 90) ,
    308     VLC_PACK(5 , 0 , 366) ,
    309     VLC_PACK(5 , 0 , 189) ,
    310     VLC_PACK(5 , 0 , 358) ,
    311     VLC_PACK(4 , 3 , 380) ,
    312     VLC_PACK(4 , 3 , 380) ,
    313     VLC_PACK(4 , 3 , 381) ,
    314     VLC_PACK(4 , 3 , 381) ,
    315     VLC_PACK(4 , 3 , 254) ,
    316     VLC_PACK(4 , 3 , 254) ,
    317     VLC_PACK(4 , 4 , 504) ,
    318     VLC_PACK(4 , 4 , 505) ,
    319     VLC_PACK(4 , 2 , 508) ,
    320     VLC_PACK(4 , 2 , 508) ,
    321     VLC_PACK(4 , 2 , 508) ,
    322     VLC_PACK(4 , 2 , 508) ,
    323     VLC_PACK(4 , 2 , 509) ,
    324     VLC_PACK(4 , 2 , 509) ,
    325     VLC_PACK(4 , 2 , 509) ,
    326     VLC_PACK(4 , 2 , 509) ,
    327     VLC_PACK(4 , 3 , 506) ,
    328     VLC_PACK(4 , 3 , 506) ,
    329     VLC_PACK(4 , 3 , 507) ,
    330     VLC_PACK(4 , 3 , 507) ,
    331     VLC_PACK(5 , 0 , 251) ,
    332     VLC_PACK(5 , 0 , 250) ,
    333     VLC_PACK(0 , 1 , 71) ,
    334     VLC_PACK(0 , 2 , 74) ,
    335     VLC_PACK(4 , 0 , 255) ,
    336     VLC_PACK(0 , 1 , 3) ,
    337     VLC_PACK(0 , 2 , 8) ,
    338     VLC_PACK(0 , 3 , 17) ,
    339     VLC_PACK(5 , 0 , 341) ,
    340     VLC_PACK(5 , 0 , 465) ,
    341     VLC_PACK(0 , 0 , 2) ,
    342     VLC_PACK(5 , 0 , 464) ,
    343     VLC_PACK(5 , 0 , 363) ,
    344     VLC_PACK(5 , 0 , 463) ,
    345     VLC_PACK(5 , 1 , 438) ,
    346     VLC_PACK(5 , 1 , 348) ,
    347     VLC_PACK(5 , 1 , 324) ,
    348     VLC_PACK(5 , 1 , 458) ,
    349     VLC_PACK(5 , 1 , 459) ,
    350     VLC_PACK(5 , 1 , 461) ,
    351     VLC_PACK(5 , 1 , 356) ,
    352     VLC_PACK(0 , 0 , 1) ,
    353     VLC_PACK(5 , 0 , 333) ,
    354     VLC_PACK(5 , 0 , 462) ,
    355     VLC_PACK(3 , 3 , 0) ,
    356     VLC_PACK(0 , 1 , 15) ,
    357     VLC_PACK(0 , 0 , 24) ,
    358     VLC_PACK(2 , 2 , 25) ,
    359     VLC_PACK(5 , 2 , 224) ,
    360     VLC_PACK(5 , 2 , 228) ,
    361     VLC_PACK(5 , 2 , 232) ,
    362     VLC_PACK(5 , 2 , 236) ,
    363     VLC_PACK(5 , 1 , 437) ,
    364     VLC_PACK(0 , 0 , 23) ,
    365     VLC_PACK(0 , 0 , 24) ,
    366     VLC_PACK(5 , 1 , 185) ,
    367     VLC_PACK(3 , 3 , 0) ,
    368     VLC_PACK(5 , 1 , 452) ,
    369     VLC_PACK(5 , 1 , 454) ,
    370     VLC_PACK(5 , 1 , 456) ,
    371     VLC_PACK(5 , 2 , 173) ,
    372     VLC_PACK(2 , 1 , 3) ,
    373     VLC_PACK(1 , 1 , 5) ,
    374     VLC_PACK(5 , 2 , 449) ,
    375     VLC_PACK(5 , 1 , 432) ,
    376     VLC_PACK(5 , 0 , 431) ,
    377     VLC_PACK(5 , 0 , 332) ,
    378     VLC_PACK(5 , 1 , 434) ,
    379     VLC_PACK(5 , 0 , 436) ,
    380     VLC_PACK(5 , 0 , 448) ,
    381     VLC_PACK(5 , 2 , 215) ,
    382     VLC_PACK(5 , 2 , 219) ,
    383     VLC_PACK(5 , 2 , 180) ,
    384     VLC_PACK(5 , 1 , 178) ,
    385     VLC_PACK(5 , 0 , 177) ,
    386     VLC_PACK(5 , 0 , 223) ,
    387     VLC_PACK(5 , 0 , 340) ,
    388     VLC_PACK(5 , 0 , 355) ,
    389     VLC_PACK(5 , 0 , 362) ,
    390     VLC_PACK(5 , 0 , 184) ,
    391     VLC_PACK(5 , 0 , 326) ,
    392     VLC_PACK(5 , 0 , 471) ,
    393     VLC_PACK(5 , 0 , 334) ,
    394     VLC_PACK(5 , 0 , 365) ,
    395     VLC_PACK(5 , 0 , 350) ,
    396     VLC_PACK(5 , 0 , 342) ,
    397     VLC_PACK(2 , 1 , 4) ,
    398     VLC_PACK(5 , 1 , 466) ,
    399     VLC_PACK(5 , 0 , 357) ,
    400     VLC_PACK(5 , 0 , 187) ,
    401     VLC_PACK(5 , 1 , 244) ,
    402     VLC_PACK(5 , 0 , 468) ,
    403     VLC_PACK(5 , 0 , 186) ,
    404     VLC_PACK(5 , 0 , 470) ,
    405     VLC_PACK(5 , 0 , 188) ,
    406     VLC_PACK(5 , 0 , 469) ,
    407     VLC_PACK(5 , 0 , 247) ,
    408     VLC_PACK(4 , 2 , 492) ,
    409     VLC_PACK(4 , 2 , 493) ,
    410     VLC_PACK(5 , 0 , 243) ,
    411     VLC_PACK(5 , 0 , 242) ,
    412     VLC_PACK(5 , 0 , 364) ,
    413     VLC_PACK(5 , 0 , 349) ,
    414     VLC_PACK(5 , 0 , 241) ,
    415     VLC_PACK(5 , 0 , 240) ,
    416     VLC_PACK(4 , 0 , 30) ,
    417     VLC_PACK(5 , 0 , 14) ,
    418     VLC_PACK(5 , 0 , 13) ,
    419     VLC_PACK(5 , 0 , 12) ,
    420     VLC_PACK(0 , 0 , 3) ,
    421     VLC_PACK(2 , 2 , 4) ,
    422     VLC_PACK(0 , 1 , 7) ,
    423     VLC_PACK(5 , 1 , 9) ,
    424     VLC_PACK(5 , 0 , 11) ,
    425     VLC_PACK(5 , 0 , 8) ,
    426     VLC_PACK(5 , 1 , 6) ,
    427     VLC_PACK(5 , 0 , 5) ,
    428     VLC_PACK(5 , 1 , 3) ,
    429     VLC_PACK(3 , 1 , 0) ,
    430     VLC_PACK(2 , 2 , 3) ,
    431     VLC_PACK(3 , 1 , 0) ,
    432     VLC_PACK(2 , 1 , 5) ,
    433     VLC_PACK(3 , 2 , 0) ,
    434     VLC_PACK(3 , 2 , 0) ,
    435     VLC_PACK(3 , 2 , 0) ,
    436     VLC_PACK(4 , 2 , 226) ,
    437     VLC_PACK(5 , 1 , 1) ,
    438     VLC_PACK(5 , 0 , 0) ,
    439     VLC_PACK(5 , 0 , 31) ,
    440     VLC_PACK(4 , 0 , 62) ,
    441     VLC_PACK(5 , 0 , 30) ,
    442     VLC_PACK(5 , 0 , 29) ,
    443     VLC_PACK(5 , 0 , 28) ,
    444     VLC_PACK(0 , 0 , 3) ,
    445     VLC_PACK(2 , 2 , 4) ,
    446     VLC_PACK(1 , 1 , 7) ,
    447     VLC_PACK(5 , 1 , 25) ,
    448     VLC_PACK(5 , 0 , 27) ,
    449     VLC_PACK(5 , 0 , 24) ,
    450     VLC_PACK(5 , 1 , 22) ,
    451     VLC_PACK(5 , 0 , 21) ,
    452     VLC_PACK(5 , 1 , 19) ,
    453     VLC_PACK(3 , 1 , 0) ,
    454     VLC_PACK(3 , 1 , 0) ,
    455     VLC_PACK(5 , 2 , 15)
    456 };
    457 
    458 #define MAX_QUANT_TABLES    (2) /* only 2 tables for 4:2:0 decode */
    459 
    460 static int scan0[64] = { // spec, fig 7-2
    461     /*u 0  .....                   7*/
    462     0,  1,  5,  6,  14, 15, 27, 28,  /* v = 0 */
    463     2,  4,  7,  13, 16, 26, 29, 42,
    464     3,  8,  12, 17, 25, 30, 41, 43,
    465     9,  11, 18, 24, 31, 40, 44, 53,
    466     10, 19, 23, 32, 39, 45, 52, 54,
    467     20, 22, 33, 38, 46, 51, 55, 60,
    468     21, 34, 37, 47, 50, 56, 59, 61,
    469     35, 36, 48, 49, 57, 58, 62, 63  /* v = 7 */
    470 };
    471 
    472 typedef enum {
    473     NONINTRA_LUMA_Q = 0,
    474     INTRA_LUMA_Q = 1
    475 } QUANT_IDX;
    476 
    477 struct context_MPEG2_s {
    478     struct context_DEC_s dec_ctx;
    479     object_context_p obj_context; /* back reference */
    480 
    481     /* Picture parameters */
    482     VAPictureParameterBufferMPEG2 *pic_params;
    483     object_surface_p forward_ref_surface;
    484     object_surface_p backward_ref_surface;
    485 
    486     uint32_t coded_picture_width;    /* in pixels */
    487     uint32_t coded_picture_height;    /* in pixels */
    488 
    489     uint32_t picture_width_mb;        /* in macroblocks */
    490     uint32_t picture_height_mb;        /* in macroblocks */
    491     uint32_t size_mb;                /* in macroblocks */
    492 
    493     uint32_t coded_picture_size;    /* MSVDX format */
    494     uint32_t display_picture_size;    /* MSVDX format */
    495 
    496     uint32_t BE_PPS0;
    497     uint32_t BE_PPS1;
    498     uint32_t BE_PPS2;
    499     uint32_t BE_SPS0;
    500     uint32_t BE_SPS1;
    501     uint32_t FE_PPS0;
    502     uint32_t FE_PPS1;
    503 
    504     /* IQ Matrix */
    505     uint32_t qmatrix_data[MAX_QUANT_TABLES][16];
    506     int got_iq_matrix;
    507 
    508     /* VLC packed data */
    509     struct psb_buffer_s vlc_packed_table;
    510 
    511     /* Misc */
    512     unsigned int previous_slice_vertical_position;
    513 };
    514 
    515 typedef struct context_MPEG2_s *context_MPEG2_p;
    516 
    517 #define INIT_CONTEXT_MPEG2    context_MPEG2_p ctx = (context_MPEG2_p) obj_context->format_data;
    518 
    519 #define SURFACE(id)    ((object_surface_p) object_heap_lookup( &ctx->obj_context->driver_data->surface_heap, id ))
    520 
    521 
    522 static void pnw_MPEG2_QueryConfigAttributes(
    523     VAProfile __maybe_unused profile,
    524     VAEntrypoint __maybe_unused entrypoint,
    525     VAConfigAttrib __maybe_unused * attrib_list,
    526     int __maybe_unused num_attribs)
    527 {
    528     /* No MPEG2 specific attributes */
    529 }
    530 
    531 static VAStatus pnw_MPEG2_ValidateConfig(
    532     object_config_p obj_config)
    533 {
    534     int i;
    535     /* Check all attributes */
    536     for (i = 0; i < obj_config->attrib_count; i++) {
    537         switch (obj_config->attrib_list[i].type) {
    538         case VAConfigAttribRTFormat:
    539             /* Ignore */
    540             break;
    541 
    542         default:
    543             return VA_STATUS_ERROR_ATTR_NOT_SUPPORTED;
    544         }
    545     }
    546 
    547     return VA_STATUS_SUCCESS;
    548 }
    549 
    550 static VAStatus psb__MPEG2_check_legal_picture(object_context_p obj_context, object_config_p obj_config)
    551 {
    552     VAStatus vaStatus = VA_STATUS_SUCCESS;
    553 
    554     CHECK_CONTEXT(obj_context);
    555     CHECK_CONFIG(obj_config);
    556 
    557     /* MSVDX decode capability for MPEG2:
    558      *     MP@HL
    559      *
    560      * Refer to Table 8-11 (Upper bounds for luminance sample rate) of ISO/IEC 13818-2: 1995(E),
    561      * and the "MSVDX MPEG2 decode capability" table of "Poulsbo Media Software Overview"
    562      */
    563 
    564     switch (obj_config->profile) {
    565     case VAProfileMPEG2Simple:
    566         if ((obj_context->picture_width <= 0) || (obj_context->picture_width > 352)
    567             || (obj_context->picture_height <= 0) || (obj_context->picture_height > 288)) {
    568             vaStatus = VA_STATUS_ERROR_RESOLUTION_NOT_SUPPORTED;
    569         }
    570         break;
    571 
    572     case VAProfileMPEG2Main:
    573         if ((obj_context->picture_width <= 0) || (obj_context->picture_width > 1920)
    574             || (obj_context->picture_height <= 0) || (obj_context->picture_height > 1088)) {
    575             vaStatus = VA_STATUS_ERROR_RESOLUTION_NOT_SUPPORTED;
    576         }
    577         break;
    578 
    579     default:
    580         vaStatus = VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
    581         break;
    582     }
    583 
    584     return vaStatus;
    585 }
    586 
    587 static void pnw_MPEG2_DestroyContext(object_context_p obj_context);
    588 static void psb__MPEG2_process_slice_data(context_DEC_p dec_ctx, VASliceParameterBufferBase *vld_slice_param);
    589 static void psb__MPEG2_end_slice(context_DEC_p dec_ctx);
    590 static void psb__MPEG2_begin_slice(context_DEC_p dec_ctx, VASliceParameterBufferBase *vld_slice_param);
    591 static VAStatus pnw_MPEG2_process_buffer(context_DEC_p dec_ctx, object_buffer_p buffer);
    592 
    593 static VAStatus pnw_MPEG2_CreateContext(
    594     object_context_p obj_context,
    595     object_config_p obj_config)
    596 {
    597     VAStatus vaStatus = VA_STATUS_SUCCESS;
    598     context_MPEG2_p ctx;
    599     /* Validate flag */
    600     /* Validate picture dimensions */
    601     vaStatus = psb__MPEG2_check_legal_picture(obj_context, obj_config);
    602     CHECK_VASTATUS();
    603 
    604     CHECK_INVALID_PARAM(obj_context->num_render_targets < 1);
    605 
    606     ctx = (context_MPEG2_p) calloc(1, sizeof(struct context_MPEG2_s));
    607     CHECK_ALLOCATION(ctx);
    608 
    609     obj_context->format_data = (void*) ctx;
    610     ctx->obj_context = obj_context;
    611     ctx->pic_params = NULL;
    612     ctx->got_iq_matrix = FALSE;
    613     ctx->previous_slice_vertical_position = ~1;
    614 
    615     ctx->dec_ctx.begin_slice = psb__MPEG2_begin_slice;
    616     ctx->dec_ctx.process_slice = psb__MPEG2_process_slice_data;
    617     ctx->dec_ctx.end_slice = psb__MPEG2_end_slice;
    618     ctx->dec_ctx.process_buffer =  pnw_MPEG2_process_buffer;
    619     ctx->dec_ctx.preload_buffer = NULL;
    620 
    621     if (vaStatus == VA_STATUS_SUCCESS) {
    622         vaStatus = psb_buffer_create(obj_context->driver_data,
    623                                      sizeof(gaui16mpeg2VlcTableDataPacked),
    624                                      psb_bt_cpu_vpu,
    625                                      &ctx->vlc_packed_table);
    626         DEBUG_FAILURE;
    627     }
    628     if (vaStatus == VA_STATUS_SUCCESS) {
    629         unsigned char *vlc_packed_data_address;
    630         if (0 ==  psb_buffer_map(&ctx->vlc_packed_table, &vlc_packed_data_address)) {
    631             memcpy(vlc_packed_data_address, gaui16mpeg2VlcTableDataPacked, sizeof(gaui16mpeg2VlcTableDataPacked));
    632             psb_buffer_unmap(&ctx->vlc_packed_table);
    633         } else {
    634             vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
    635             DEBUG_FAILURE;
    636         }
    637     }
    638 
    639     if (vaStatus == VA_STATUS_SUCCESS) {
    640         vaStatus = vld_dec_CreateContext(&ctx->dec_ctx, obj_context);
    641         DEBUG_FAILURE;
    642     }
    643 
    644     if (vaStatus != VA_STATUS_SUCCESS) {
    645         pnw_MPEG2_DestroyContext(obj_context);
    646     }
    647 
    648     return vaStatus;
    649 }
    650 
    651 static void pnw_MPEG2_DestroyContext(
    652     object_context_p obj_context)
    653 {
    654     INIT_CONTEXT_MPEG2
    655 
    656     vld_dec_DestroyContext(&ctx->dec_ctx);
    657 
    658     psb_buffer_destroy(&ctx->vlc_packed_table);
    659 
    660     if (ctx->pic_params) {
    661         free(ctx->pic_params);
    662         ctx->pic_params = NULL;
    663     }
    664 
    665     free(obj_context->format_data);
    666     obj_context->format_data = NULL;
    667 }
    668 
    669 static VAStatus psb__MPEG2_process_picture_param(context_MPEG2_p ctx, object_buffer_p obj_buffer)
    670 {
    671     object_surface_p obj_surface = ctx->obj_context->current_render_target;
    672     ASSERT(obj_buffer->type == VAPictureParameterBufferType);
    673     ASSERT(obj_buffer->num_elements == 1);
    674     ASSERT(obj_buffer->size == sizeof(VAPictureParameterBufferMPEG2));
    675 
    676     if ((obj_buffer->num_elements != 1) ||
    677         (obj_buffer->size != sizeof(VAPictureParameterBufferMPEG2))) {
    678         return VA_STATUS_ERROR_UNKNOWN;
    679     }
    680 
    681     /* Transfer ownership of VAPictureParameterBufferMPEG2 data */
    682     if (ctx->pic_params) {
    683         free(ctx->pic_params);
    684     }
    685     ctx->pic_params = (VAPictureParameterBufferMPEG2 *) obj_buffer->buffer_data;
    686     obj_buffer->buffer_data = NULL;
    687     obj_buffer->size = 0;
    688 
    689     /* Lookup surfaces for backward/forward references */
    690     switch (ctx->pic_params->picture_coding_type) {
    691     case PICTURE_CODING_I:
    692         ctx->forward_ref_surface = NULL;
    693         ctx->backward_ref_surface = NULL;
    694         break;
    695 
    696     case PICTURE_CODING_P:
    697         ctx->forward_ref_surface = SURFACE(ctx->pic_params->forward_reference_picture);
    698         ctx->backward_ref_surface = NULL;
    699         if (NULL == ctx->forward_ref_surface) {
    700             return VA_STATUS_ERROR_INVALID_SURFACE;
    701         }
    702         break;
    703 
    704     case PICTURE_CODING_B:
    705         ctx->forward_ref_surface = SURFACE(ctx->pic_params->forward_reference_picture);
    706         ctx->backward_ref_surface = SURFACE(ctx->pic_params->backward_reference_picture);
    707         if ((NULL == ctx->forward_ref_surface) ||
    708             (NULL == ctx->backward_ref_surface)) {
    709             return VA_STATUS_ERROR_INVALID_SURFACE;
    710         }
    711         break;
    712 
    713     default:
    714         return VA_STATUS_ERROR_UNKNOWN;
    715     }
    716 
    717     ctx->picture_width_mb = (ctx->pic_params->horizontal_size + 15) / 16;
    718     if (ctx->obj_context->va_flags & VA_PROGRESSIVE) {
    719         ctx->picture_height_mb = (ctx->pic_params->vertical_size + 15) / 16;
    720     } else {
    721         ctx->picture_height_mb = (ctx->pic_params->vertical_size + 31) / 32;
    722         ctx->picture_height_mb *= 2;
    723     }
    724     ctx->coded_picture_width = ctx->picture_width_mb * 16;
    725     ctx->coded_picture_height = ctx->picture_height_mb * 16;
    726 
    727     if (obj_surface->share_info) {
    728         obj_surface->share_info->coded_width = ctx->coded_picture_width;
    729         obj_surface->share_info->coded_height = ctx->coded_picture_height;
    730     }
    731 
    732     ctx->size_mb = ctx->picture_width_mb * ctx->picture_height_mb;
    733 
    734     /* Display picture size                                                            */
    735     ctx->display_picture_size = 0;
    736     /*
    737      * coded_picture_width/height is aligned to the size of a macroblock..
    738      * Both coded_picture_height or vertical_size can be used for DISPLAY_SIZE and both give correct results,
    739      * however Vista driver / test app uses the aligned value that's in coded_picture_height so we do too.
    740      * See e.g. low4.m2v for an example clip where vertical_size will differ from coded_picture_height
    741      */
    742 #if 0
    743     REGIO_WRITE_FIELD_LITE(ctx->display_picture_size, MSVDX_CMDS, DISPLAY_PICTURE_SIZE, DISPLAY_PICTURE_HEIGHT, ctx->pic_params->vertical_size - 1);
    744     REGIO_WRITE_FIELD_LITE(ctx->display_picture_size, MSVDX_CMDS, DISPLAY_PICTURE_SIZE, DISPLAY_PICTURE_WIDTH, ctx->pic_params->horizontal_size - 1);
    745 #else
    746     REGIO_WRITE_FIELD_LITE(ctx->display_picture_size, MSVDX_CMDS, DISPLAY_PICTURE_SIZE, DISPLAY_PICTURE_HEIGHT, ctx->coded_picture_height - 1);
    747     REGIO_WRITE_FIELD_LITE(ctx->display_picture_size, MSVDX_CMDS, DISPLAY_PICTURE_SIZE, DISPLAY_PICTURE_WIDTH, ctx->coded_picture_width - 1);
    748 #endif
    749 
    750     /* Coded picture size                                                            */
    751     ctx->coded_picture_size = 0;
    752     REGIO_WRITE_FIELD_LITE(ctx->coded_picture_size, MSVDX_CMDS, CODED_PICTURE_SIZE, CODED_PICTURE_HEIGHT, ctx->coded_picture_height - 1);
    753     REGIO_WRITE_FIELD_LITE(ctx->coded_picture_size, MSVDX_CMDS, CODED_PICTURE_SIZE, CODED_PICTURE_WIDTH, ctx->coded_picture_width - 1);
    754 
    755     ctx->BE_SPS0 = 0;
    756     REGIO_WRITE_FIELD_LITE(ctx->BE_SPS0, MSVDX_VEC_MPEG2, CR_VEC_MPEG2_BE_SPS0, BE_HORIZONTAL_SIZE_MINUS1, ctx->picture_width_mb - 1);
    757 
    758     ctx->BE_SPS1 = 0;
    759     REGIO_WRITE_FIELD_LITE(ctx->BE_SPS1, MSVDX_VEC_MPEG2, CR_VEC_MPEG2_BE_SPS1, BE_VERTICAL_SIZE_MINUS1, ctx->picture_height_mb);
    760 
    761     ctx->FE_PPS0 = 0;
    762     REGIO_WRITE_FIELD_LITE(ctx->FE_PPS0, MSVDX_VEC_MPEG2, CR_VEC_MPEG2_FE_PPS0, FE_ALTERNATE_SCAN,                 !!(ctx->pic_params->picture_coding_extension.bits.alternate_scan));
    763     REGIO_WRITE_FIELD_LITE(ctx->FE_PPS0, MSVDX_VEC_MPEG2, CR_VEC_MPEG2_FE_PPS0, FE_Q_SCALE_TYPE,                 !!(ctx->pic_params->picture_coding_extension.bits.q_scale_type));
    764     REGIO_WRITE_FIELD_LITE(ctx->FE_PPS0, MSVDX_VEC_MPEG2, CR_VEC_MPEG2_FE_PPS0, FE_INTRA_DC_PRECISION,                ctx->pic_params->picture_coding_extension.bits.intra_dc_precision);
    765     REGIO_WRITE_FIELD_LITE(ctx->FE_PPS0, MSVDX_VEC_MPEG2, CR_VEC_MPEG2_FE_PPS0, FE_TOP_FIELD_FIRST,             !!(ctx->pic_params->picture_coding_extension.bits.top_field_first));
    766     REGIO_WRITE_FIELD_LITE(ctx->FE_PPS0, MSVDX_VEC_MPEG2, CR_VEC_MPEG2_FE_PPS0, FE_CONCEALMENT_MOTION_VECTORS,  !!(ctx->pic_params->picture_coding_extension.bits.concealment_motion_vectors));
    767     REGIO_WRITE_FIELD_LITE(ctx->FE_PPS0, MSVDX_VEC_MPEG2, CR_VEC_MPEG2_FE_PPS0, FE_FRAME_PRED_FRAME_DCT,         !!(ctx->pic_params->picture_coding_extension.bits.frame_pred_frame_dct));
    768     REGIO_WRITE_FIELD_LITE(ctx->FE_PPS0, MSVDX_VEC_MPEG2, CR_VEC_MPEG2_FE_PPS0, FE_PICTURE_STRUCTURE,                ctx->pic_params->picture_coding_extension.bits.picture_structure);
    769     REGIO_WRITE_FIELD_LITE(ctx->FE_PPS0, MSVDX_VEC_MPEG2, CR_VEC_MPEG2_FE_PPS0, FE_INTRA_VLC_FORMAT,             !!(ctx->pic_params->picture_coding_extension.bits.intra_vlc_format));
    770 
    771     ctx->FE_PPS1 = 0;
    772     REGIO_WRITE_FIELD_LITE(ctx->FE_PPS1, MSVDX_VEC_MPEG2, CR_VEC_MPEG2_FE_PPS1, FE_PICTURE_CODING_TYPE, ctx->pic_params->picture_coding_type);
    773     REGIO_WRITE_FIELD_LITE(ctx->FE_PPS1, MSVDX_VEC_MPEG2, CR_VEC_MPEG2_FE_PPS1, FE_F_CODE00, (ctx->pic_params->f_code >> 12) & 0x0F);
    774     REGIO_WRITE_FIELD_LITE(ctx->FE_PPS1, MSVDX_VEC_MPEG2, CR_VEC_MPEG2_FE_PPS1, FE_F_CODE01, (ctx->pic_params->f_code >>  8) & 0x0F);
    775     REGIO_WRITE_FIELD_LITE(ctx->FE_PPS1, MSVDX_VEC_MPEG2, CR_VEC_MPEG2_FE_PPS1, FE_F_CODE10, (ctx->pic_params->f_code >>  4) & 0x0F);
    776     REGIO_WRITE_FIELD_LITE(ctx->FE_PPS1, MSVDX_VEC_MPEG2, CR_VEC_MPEG2_FE_PPS1, FE_F_CODE11, (ctx->pic_params->f_code >>  0) & 0x0F);
    777 
    778     /* VEC Control register: Back-End MPEG2 PPS0                                    */
    779     ctx->BE_PPS0 = 0;
    780     REGIO_WRITE_FIELD_LITE(ctx->BE_PPS0, MSVDX_VEC_MPEG2, CR_VEC_MPEG2_BE_PPS0, BE_FRAME_PRED_FRAME_DCT,    !!(ctx->pic_params->picture_coding_extension.bits.frame_pred_frame_dct));
    781     REGIO_WRITE_FIELD_LITE(ctx->BE_PPS0, MSVDX_VEC_MPEG2, CR_VEC_MPEG2_BE_PPS0, BE_INTRA_DC_PRECISION,           ctx->pic_params->picture_coding_extension.bits.intra_dc_precision);
    782     REGIO_WRITE_FIELD_LITE(ctx->BE_PPS0, MSVDX_VEC_MPEG2, CR_VEC_MPEG2_BE_PPS0, BE_Q_SCALE_TYPE,            !!(ctx->pic_params->picture_coding_extension.bits.q_scale_type));
    783     REGIO_WRITE_FIELD_LITE(ctx->BE_PPS0, MSVDX_VEC_MPEG2, CR_VEC_MPEG2_BE_PPS0, BE_ALTERNATE_SCAN,            !!(ctx->pic_params->picture_coding_extension.bits.alternate_scan));
    784 
    785     ctx->BE_PPS1 = 0;
    786     REGIO_WRITE_FIELD_LITE(ctx->BE_PPS1, MSVDX_VEC_MPEG2, CR_VEC_MPEG2_BE_PPS1, BE_F_CODE00, (ctx->pic_params->f_code >> 12) & 0x0F);
    787     REGIO_WRITE_FIELD_LITE(ctx->BE_PPS1, MSVDX_VEC_MPEG2, CR_VEC_MPEG2_BE_PPS1, BE_F_CODE01, (ctx->pic_params->f_code >>  8) & 0x0F);
    788     REGIO_WRITE_FIELD_LITE(ctx->BE_PPS1, MSVDX_VEC_MPEG2, CR_VEC_MPEG2_BE_PPS1, BE_F_CODE10, (ctx->pic_params->f_code >>  4) & 0x0F);
    789     REGIO_WRITE_FIELD_LITE(ctx->BE_PPS1, MSVDX_VEC_MPEG2, CR_VEC_MPEG2_BE_PPS1, BE_F_CODE11, (ctx->pic_params->f_code >>  0) & 0x0F);
    790 
    791     /* VEC Control register: Back-End MPEG2 PPS2                                    */
    792     ctx->BE_PPS2 = 0;
    793     REGIO_WRITE_FIELD_LITE(ctx->BE_PPS2, MSVDX_VEC_MPEG2, CR_VEC_MPEG2_BE_PPS2, BE_PICTURE_CODING_TYPE,           ctx->pic_params->picture_coding_type);
    794     REGIO_WRITE_FIELD_LITE(ctx->BE_PPS2, MSVDX_VEC_MPEG2, CR_VEC_MPEG2_BE_PPS2, BE_TOP_FIELD_FIRST,            !!(ctx->pic_params->picture_coding_extension.bits.top_field_first));
    795     REGIO_WRITE_FIELD_LITE(ctx->BE_PPS2, MSVDX_VEC_MPEG2, CR_VEC_MPEG2_BE_PPS2, BE_CONCEALMENT_MOTION_VECTORS, !!(ctx->pic_params->picture_coding_extension.bits.concealment_motion_vectors));
    796     REGIO_WRITE_FIELD_LITE(ctx->BE_PPS2, MSVDX_VEC_MPEG2, CR_VEC_MPEG2_BE_PPS2, BE_PICTURE_STRUCTURE,               ctx->pic_params->picture_coding_extension.bits.picture_structure);
    797 
    798     ctx->obj_context->operating_mode = 0;
    799     REGIO_WRITE_FIELD_LITE(ctx->obj_context->operating_mode, MSVDX_CMDS, OPERATING_MODE, CHROMA_INTERLEAVED, 0);
    800     REGIO_WRITE_FIELD_LITE(ctx->obj_context->operating_mode, MSVDX_CMDS, OPERATING_MODE, ROW_STRIDE, ctx->obj_context->current_render_target->psb_surface->stride_mode);
    801     REGIO_WRITE_FIELD_LITE(ctx->obj_context->operating_mode, MSVDX_CMDS, OPERATING_MODE, CODEC_PROFILE, 1);                                 /* MPEG2 profile            */
    802     REGIO_WRITE_FIELD_LITE(ctx->obj_context->operating_mode, MSVDX_CMDS, OPERATING_MODE, CODEC_MODE, 3);                                 /* MPEG2 mode                */
    803     REGIO_WRITE_FIELD_LITE(ctx->obj_context->operating_mode, MSVDX_CMDS, OPERATING_MODE, ASYNC_MODE, 1);                                 /* VDMC only                */
    804     REGIO_WRITE_FIELD_LITE(ctx->obj_context->operating_mode, MSVDX_CMDS, OPERATING_MODE, CHROMA_FORMAT, 1);
    805 
    806     return VA_STATUS_SUCCESS;
    807 }
    808 
    809 static void psb__MPEG2_convert_iq_matrix(uint32_t *dest32, unsigned char *src)
    810 {
    811     int i;
    812     int *idx = scan0;
    813     uint8_t *dest8 = (uint8_t*) dest32;
    814 
    815     for (i = 0; i < 64; i++) {
    816         *dest8++ = src[*idx++];
    817     }
    818 }
    819 
    820 static VAStatus psb__MPEG2_process_iq_matrix(context_MPEG2_p ctx, object_buffer_p obj_buffer)
    821 {
    822     VAIQMatrixBufferMPEG2 *iq_matrix = (VAIQMatrixBufferMPEG2 *) obj_buffer->buffer_data;
    823     ASSERT(obj_buffer->type == VAIQMatrixBufferType);
    824     ASSERT(obj_buffer->num_elements == 1);
    825     ASSERT(obj_buffer->size == sizeof(VAIQMatrixBufferMPEG2));
    826 
    827     if ((obj_buffer->num_elements != 1) ||
    828         (obj_buffer->size != sizeof(VAIQMatrixBufferMPEG2))) {
    829         return VA_STATUS_ERROR_UNKNOWN;
    830     }
    831 
    832     /* Only update the qmatrix data if the load flag is set */
    833     if (iq_matrix->load_non_intra_quantiser_matrix) {
    834         psb__MPEG2_convert_iq_matrix(ctx->qmatrix_data[NONINTRA_LUMA_Q], iq_matrix->non_intra_quantiser_matrix);
    835     }
    836     if (iq_matrix->load_intra_quantiser_matrix) {
    837         psb__MPEG2_convert_iq_matrix(ctx->qmatrix_data[INTRA_LUMA_Q], iq_matrix->intra_quantiser_matrix);
    838     }
    839     /* We ignore the Chroma tables because those are not supported with VA_RT_FORMAT_YUV420 */
    840     ctx->got_iq_matrix = TRUE;
    841 
    842     return VA_STATUS_SUCCESS;
    843 }
    844 
    845 /* Precalculated values */
    846 #define ADDR0       (0x00006000)
    847 #define ADDR1       (0x0003f017)
    848 #define ADDR2       (0x000ab0e5)
    849 #define ADDR3       (0x0000016e)
    850 #define WIDTH0      (0x0016c6ed)
    851 #define OPCODE0     (0x00002805)
    852 
    853 static void psb__MPEG2_write_VLC_tables(context_MPEG2_p ctx)
    854 {
    855     psb_cmdbuf_p cmdbuf = ctx->obj_context->cmdbuf;
    856 
    857     psb_cmdbuf_skip_start_block(cmdbuf, SKIP_ON_CONTEXT_SWITCH);
    858     /* VLC Table */
    859     /* Write a LLDMA Cmd to transfer VLD Table data */
    860     psb_cmdbuf_dma_write_cmdbuf(cmdbuf, &ctx->vlc_packed_table, 0,
    861                                   sizeof(gaui16mpeg2VlcTableDataPacked), 0,
    862                                   DMA_TYPE_VLC_TABLE);
    863 
    864     /* Write the vec registers with the index data for each of the tables and then write    */
    865     /* the actual table data.                                                                */
    866     psb_cmdbuf_reg_start_block(cmdbuf, 0);
    867     psb_cmdbuf_reg_set(cmdbuf, REGISTER_OFFSET(MSVDX_VEC, CR_VEC_VLC_TABLE_ADDR0),            ADDR0);
    868     psb_cmdbuf_reg_set(cmdbuf, REGISTER_OFFSET(MSVDX_VEC, CR_VEC_VLC_TABLE_ADDR1),            ADDR1);
    869     psb_cmdbuf_reg_set(cmdbuf, REGISTER_OFFSET(MSVDX_VEC, CR_VEC_VLC_TABLE_ADDR2),            ADDR2);
    870     psb_cmdbuf_reg_set(cmdbuf, REGISTER_OFFSET(MSVDX_VEC, CR_VEC_VLC_TABLE_ADDR3),            ADDR3);
    871     psb_cmdbuf_reg_set(cmdbuf, REGISTER_OFFSET(MSVDX_VEC, CR_VEC_VLC_TABLE_INITIAL_WIDTH0),    WIDTH0);
    872     psb_cmdbuf_reg_set(cmdbuf, REGISTER_OFFSET(MSVDX_VEC, CR_VEC_VLC_TABLE_INITIAL_OPCODE0), OPCODE0);
    873     psb_cmdbuf_reg_end_block(cmdbuf);
    874 
    875     psb_cmdbuf_skip_end_block(cmdbuf);
    876 }
    877 
    878 static void psb__MPEG2_set_operating_mode(context_MPEG2_p ctx)
    879 {
    880     psb_cmdbuf_p cmdbuf = ctx->obj_context->cmdbuf;
    881     psb_surface_p target_surface = ctx->obj_context->current_render_target->psb_surface;
    882 
    883     vld_dec_setup_alternative_frame(ctx->obj_context);
    884 
    885     psb_cmdbuf_rendec_start(cmdbuf, RENDEC_REGISTER_OFFSET(MSVDX_CMDS, DISPLAY_PICTURE_SIZE));
    886     psb_cmdbuf_rendec_write(cmdbuf, ctx->display_picture_size);
    887     psb_cmdbuf_rendec_write(cmdbuf, ctx->coded_picture_size);
    888     psb_cmdbuf_rendec_write(cmdbuf, ctx->obj_context->operating_mode);
    889 
    890     /* LUMA_RECONSTRUCTED_PICTURE_BASE_ADDRESSES                                    */
    891     psb_cmdbuf_rendec_write_address(cmdbuf, &target_surface->buf, target_surface->buf.buffer_ofs);
    892 
    893     /* CHROMA_RECONSTRUCTED_PICTURE_BASE_ADDRESSES                                    */
    894     psb_cmdbuf_rendec_write_address(cmdbuf, &target_surface->buf, target_surface->buf.buffer_ofs + target_surface->chroma_offset);
    895 
    896     psb_cmdbuf_rendec_end(cmdbuf);
    897 }
    898 
    899 static void psb__MPEG2_set_reference_pictures(context_MPEG2_p ctx)
    900 {
    901     psb_cmdbuf_p cmdbuf = ctx->obj_context->cmdbuf;
    902     psb_surface_p target_surface = ctx->obj_context->current_render_target->psb_surface;
    903 
    904     psb_cmdbuf_rendec_start(cmdbuf, RENDEC_REGISTER_OFFSET(MSVDX_CMDS, REFERENCE_PICTURE_BASE_ADDRESSES));
    905 
    906     /* In MPEG2, the registers at N=0 are always used to store the base address of the luma and chroma buffers
    907         of the most recently decoded reference picture. The registers at N=1 are used to store the base address
    908         of the luma and chroma buffers of the older reference picture, if more than one reference picture is used.
    909         This means that when decoding a P picture the forward reference pictures address is at index 0.
    910         When decoding a B-picture the backward reference pictures address is at index 0 and the address of the
    911         forward reference picture  which was decoded earlier than the backward reference  is at index 1.
    912     */
    913 
    914     switch (ctx->pic_params->picture_coding_type) {
    915     case PICTURE_CODING_I:
    916         drv_debug_msg(VIDEO_DEBUG_GENERAL, "    I-Frame\n");
    917         /* No reference pictures */
    918         psb_cmdbuf_rendec_write(cmdbuf, 0);
    919         psb_cmdbuf_rendec_write(cmdbuf, 0);
    920         psb_cmdbuf_rendec_write(cmdbuf, 0);
    921         psb_cmdbuf_rendec_write(cmdbuf, 0);
    922         break;
    923 
    924     case PICTURE_CODING_P:
    925         drv_debug_msg(VIDEO_DEBUG_GENERAL, "    P-Frame\n");
    926         if (ctx->pic_params->picture_coding_extension.bits.is_first_field) {
    927             /* forward reference picture */
    928             /* LUMA_RECONSTRUCTED_PICTURE_BASE_ADDRESSES */
    929             psb_cmdbuf_rendec_write_address(cmdbuf, &ctx->forward_ref_surface->psb_surface->buf, ctx->forward_ref_surface->psb_surface->buf.buffer_ofs);
    930 
    931             /* CHROMA_RECONSTRUCTED_PICTURE_BASE_ADDRESSES */
    932             psb_cmdbuf_rendec_write_address(cmdbuf, &ctx->forward_ref_surface->psb_surface->buf,  ctx->forward_ref_surface->psb_surface\
    933                                             ->buf.buffer_ofs + ctx->forward_ref_surface->psb_surface->chroma_offset);
    934 
    935             /* No backward reference picture */
    936             psb_cmdbuf_rendec_write(cmdbuf, 0);
    937             psb_cmdbuf_rendec_write(cmdbuf, 0);
    938         } else {
    939             /* backward reference picture */
    940             /* LUMA_RECONSTRUCTED_PICTURE_BASE_ADDRESSES                                    */
    941             psb_cmdbuf_rendec_write_address(cmdbuf, &target_surface->buf, target_surface->buf.buffer_ofs);
    942 
    943             /* CHROMA_RECONSTRUCTED_PICTURE_BASE_ADDRESSES                                    */
    944             psb_cmdbuf_rendec_write_address(cmdbuf, &target_surface->buf, target_surface->buf.buffer_ofs + target_surface->chroma_offset);
    945 
    946             /* forward reference picture */
    947             /* LUMA_RECONSTRUCTED_PICTURE_BASE_ADDRESSES                                    */
    948             psb_cmdbuf_rendec_write_address(cmdbuf, &ctx->forward_ref_surface->psb_surface->buf, ctx->forward_ref_surface->psb_surface->buf.buffer_ofs);
    949 
    950             /* CHROMA_RECONSTRUCTED_PICTURE_BASE_ADDRESSES                                    */
    951             psb_cmdbuf_rendec_write_address(cmdbuf, &ctx->forward_ref_surface->psb_surface->buf, ctx->forward_ref_surface->psb_surface->buf.buffer_ofs + ctx->forward_ref_surface->psb_surface->chroma_offset);
    952         }
    953         break;
    954 
    955     case PICTURE_CODING_B:
    956         drv_debug_msg(VIDEO_DEBUG_GENERAL, "    B-Frame\n");
    957         /* backward reference picture */
    958         /* LUMA_RECONSTRUCTED_PICTURE_BASE_ADDRESSES                                    */
    959         psb_cmdbuf_rendec_write_address(cmdbuf, &ctx->backward_ref_surface->psb_surface->buf, ctx->backward_ref_surface->psb_surface->buf.buffer_ofs);
    960 
    961         /* CHROMA_RECONSTRUCTED_PICTURE_BASE_ADDRESSES                                    */
    962         psb_cmdbuf_rendec_write_address(cmdbuf, &ctx->backward_ref_surface->psb_surface->buf, ctx->backward_ref_surface->psb_surface->buf.buffer_ofs + ctx->backward_ref_surface->psb_surface->chroma_offset);
    963 
    964         /* forward reference picture */
    965         /* LUMA_RECONSTRUCTED_PICTURE_BASE_ADDRESSES                                    */
    966         psb_cmdbuf_rendec_write_address(cmdbuf, &ctx->forward_ref_surface->psb_surface->buf, ctx->forward_ref_surface->psb_surface->buf.buffer_ofs);
    967 
    968         /* CHROMA_RECONSTRUCTED_PICTURE_BASE_ADDRESSES                                    */
    969         psb_cmdbuf_rendec_write_address(cmdbuf, &ctx->forward_ref_surface->psb_surface->buf, ctx->forward_ref_surface->psb_surface->buf.buffer_ofs + ctx->forward_ref_surface->psb_surface->chroma_offset);
    970         break;
    971     }
    972 
    973     psb_cmdbuf_rendec_end(cmdbuf);
    974 }
    975 
    976 static void psb__MPEG2_set_picture_header(context_MPEG2_p ctx, VASliceParameterBufferMPEG2 *slice_param)
    977 {
    978     psb_cmdbuf_p cmdbuf = ctx->obj_context->cmdbuf;
    979     psb_surface_p target_surface = ctx->obj_context->current_render_target->psb_surface;
    980     uint32_t FE_slice;
    981     uint32_t BE_slice;
    982 
    983     psb_cmdbuf_reg_start_block(cmdbuf, 0);
    984 
    985     /* VEC Control register: Front-End MPEG2 PPS0 */
    986     psb_cmdbuf_reg_set(cmdbuf, REGISTER_OFFSET(MSVDX_VEC_MPEG2, CR_VEC_MPEG2_FE_PPS0) , ctx->FE_PPS0);
    987 
    988     /* VEC Control register: Front-End MPEG2 PPS1 */
    989     psb_cmdbuf_reg_set(cmdbuf, REGISTER_OFFSET(MSVDX_VEC_MPEG2, CR_VEC_MPEG2_FE_PPS1) , ctx->FE_PPS1);
    990 
    991     /* Slice level */
    992     FE_slice = 0;
    993     REGIO_WRITE_FIELD(FE_slice,
    994                       MSVDX_VEC_MPEG2,
    995                       CR_VEC_MPEG2_FE_SLICE,
    996                       FE_FIRST_IN_ROW,
    997                       (ctx->previous_slice_vertical_position != slice_param->slice_vertical_position));
    998 
    999     REGIO_WRITE_FIELD(FE_slice,
   1000                       MSVDX_VEC_MPEG2,
   1001                       CR_VEC_MPEG2_FE_SLICE,
   1002                       FE_SLICE_VERTICAL_POSITION_MINUS1,
   1003                       slice_param->slice_vertical_position);
   1004 
   1005     REGIO_WRITE_FIELD(FE_slice,
   1006                       MSVDX_VEC_MPEG2,
   1007                       CR_VEC_MPEG2_FE_SLICE,
   1008                       FE_QUANTISER_SCALE_CODE,
   1009                       slice_param->quantiser_scale_code);
   1010 
   1011     psb_cmdbuf_reg_set(cmdbuf, REGISTER_OFFSET(MSVDX_VEC_MPEG2, CR_VEC_MPEG2_FE_SLICE) , FE_slice);
   1012 
   1013     psb_cmdbuf_reg_end_block(cmdbuf);
   1014 
   1015 
   1016     /* BE Section */
   1017     psb_cmdbuf_rendec_start(cmdbuf, RENDEC_REGISTER_OFFSET(MSVDX_VEC, MPEG2_CR_VEC_MPEG2_BE_SPS0));
   1018 
   1019     psb_cmdbuf_rendec_write(cmdbuf, ctx->BE_SPS0);
   1020     psb_cmdbuf_rendec_write(cmdbuf, ctx->BE_SPS1);
   1021     psb_cmdbuf_rendec_write(cmdbuf, ctx->BE_PPS0);   /* VEC Control register: Back-End MPEG2 PPS0 */
   1022     psb_cmdbuf_rendec_write(cmdbuf, ctx->BE_PPS1);   /* VEC Control register: Back-End MPEG2 PPS1 */
   1023     psb_cmdbuf_rendec_write(cmdbuf, ctx->BE_PPS2);   /* VEC Control register: Back-End MPEG2 PPS2 */
   1024 
   1025     BE_slice = 0;
   1026     if (!ctx->pic_params->picture_coding_extension.bits.is_first_field) {
   1027         /*
   1028             BE_IP_PAIR_FLAG is 1 if the current picture_data is a P-coded field which is the 2nd field of a frame,
   1029                         and which uses the first field of the frame (which was I-coded) as a reference. 0 otherwise.
   1030             BE_IP_PAIR_FLAG will only be 1 if BE_SECOND_FIELD_FLAG is 1, and the condition
   1031                 of "this is a P-field which uses the accompanying I-field as a reference" is met.
   1032         */
   1033         if (ctx->pic_params->picture_coding_type == PICTURE_CODING_P) {
   1034             if (GET_SURFACE_INFO_picture_coding_type(target_surface) == PICTURE_CODING_I)            {
   1035                 REGIO_WRITE_FIELD_LITE(BE_slice,
   1036                                        MSVDX_VEC_MPEG2,
   1037                                        CR_VEC_MPEG2_BE_SLICE,
   1038                                        BE_IP_PAIR_FLAG, 1);
   1039             }
   1040         }
   1041 
   1042         REGIO_WRITE_FIELD_LITE(BE_slice,
   1043                                MSVDX_VEC_MPEG2,
   1044                                CR_VEC_MPEG2_BE_SLICE,
   1045                                BE_SECOND_FIELD_FLAG,     1);
   1046 
   1047     } else {
   1048         // BE_IP_PAIR_FLAG = 0;
   1049         // BE_SECOND_FIELD_FLAG = 0;
   1050 
   1051         /*  Update with current settings first field */
   1052         SET_SURFACE_INFO_is_defined(target_surface, TRUE);
   1053         SET_SURFACE_INFO_picture_structure(target_surface, ctx->pic_params->picture_coding_extension.bits.picture_structure);
   1054         SET_SURFACE_INFO_picture_coding_type(target_surface, ctx->pic_params->picture_coding_type);
   1055     }
   1056 
   1057     REGIO_WRITE_FIELD_LITE(BE_slice,
   1058                            MSVDX_VEC_MPEG2,
   1059                            CR_VEC_MPEG2_BE_SLICE,
   1060                            BE_FIRST_IN_ROW,
   1061                            (ctx->previous_slice_vertical_position != slice_param->slice_vertical_position));
   1062 
   1063     REGIO_WRITE_FIELD_LITE(BE_slice,
   1064                            MSVDX_VEC_MPEG2,
   1065                            CR_VEC_MPEG2_BE_SLICE,
   1066                            BE_SLICE_VERTICAL_POSITION_MINUS1,
   1067                            slice_param->slice_vertical_position);
   1068 
   1069     REGIO_WRITE_FIELD_LITE(BE_slice,
   1070                            MSVDX_VEC_MPEG2,
   1071                            CR_VEC_MPEG2_BE_SLICE,
   1072                            BE_QUANTISER_SCALE_CODE,
   1073                            slice_param->quantiser_scale_code);
   1074 
   1075     drv_debug_msg(VIDEO_DEBUG_GENERAL, "BE_slice = %08x first_field = %d\n", BE_slice, ctx->pic_params->picture_coding_extension.bits.is_first_field);
   1076 
   1077     psb_cmdbuf_rendec_write(cmdbuf, BE_slice);
   1078 
   1079     psb_cmdbuf_rendec_end(cmdbuf);
   1080 
   1081     ctx->previous_slice_vertical_position = slice_param->slice_vertical_position;
   1082 }
   1083 
   1084 static void psb__MPEG2_set_slice_params(context_MPEG2_p ctx)
   1085 {
   1086     psb_cmdbuf_p cmdbuf = ctx->obj_context->cmdbuf;
   1087 
   1088     uint32_t cmd_data;
   1089 
   1090     psb_cmdbuf_rendec_start(cmdbuf, RENDEC_REGISTER_OFFSET(MSVDX_CMDS, SLICE_PARAMS));
   1091 
   1092     cmd_data = 0;    /* Build slice parameters */
   1093     REGIO_WRITE_FIELD(cmd_data,
   1094                       MSVDX_CMDS,
   1095                       SLICE_PARAMS,
   1096                       SLICE_FIELD_TYPE,
   1097                       ctx->pic_params->picture_coding_extension.bits.picture_structure - 1);
   1098 
   1099     REGIO_WRITE_FIELD(cmd_data,
   1100                       MSVDX_CMDS,
   1101                       SLICE_PARAMS,
   1102                       SLICE_CODE_TYPE,
   1103                       PICTURE_CODING_D == ctx->pic_params->picture_coding_type ? 0 : ctx->pic_params->picture_coding_type - 1);
   1104 
   1105     psb_cmdbuf_rendec_write(cmdbuf, cmd_data);
   1106 
   1107     *ctx->dec_ctx.p_slice_params = cmd_data;
   1108 
   1109     psb_cmdbuf_rendec_end(cmdbuf);
   1110 }
   1111 
   1112 static void psb__MPEG2_write_qmatrices(context_MPEG2_p ctx)
   1113 {
   1114     psb_cmdbuf_p cmdbuf = ctx->obj_context->cmdbuf;
   1115     int i;
   1116 
   1117     /* Since we only decode 4:2:0 We only need to the Intra tables.
   1118     Chroma quant tables are only used in Mpeg 4:2:2 and 4:4:4.
   1119     The hardware wants non-intra followed by intra */
   1120     psb_cmdbuf_rendec_start(cmdbuf, REG_MSVDX_VEC_IQRAM_OFFSET);
   1121 
   1122     /* todo : optimisation here is to only load the need table */
   1123 
   1124     /*  NONINTRA_LUMA_Q --> REG_MSVDX_VEC_IQRAM_OFFSET + 0 */
   1125     for (i = 0; i < 16; i++) {
   1126         psb_cmdbuf_rendec_write(cmdbuf, ctx->qmatrix_data[NONINTRA_LUMA_Q][i]);
   1127 // drv_debug_msg(VIDEO_DEBUG_GENERAL, "NONINTRA_LUMA_Q[i] = %08x\n", ctx->qmatrix_data[NONINTRA_LUMA_Q][i]);
   1128     }
   1129     /*  INTRA_LUMA_Q --> REG_MSVDX_VEC_IQRAM_OFFSET + (16*4) */
   1130     for (i = 0; i < 16; i++) {
   1131         psb_cmdbuf_rendec_write(cmdbuf, ctx->qmatrix_data[INTRA_LUMA_Q][i]);
   1132 // drv_debug_msg(VIDEO_DEBUG_GENERAL, "INTRA_LUMA_Q[i] = %08x\n", ctx->qmatrix_data[INTRA_LUMA_Q][i]);
   1133     }
   1134 
   1135     psb_cmdbuf_rendec_end(cmdbuf);
   1136 }
   1137 
   1138 static void psb__MPEG2_set_ent_dec(context_MPEG2_p ctx)
   1139 {
   1140     psb_cmdbuf_p cmdbuf = ctx->obj_context->cmdbuf;
   1141 
   1142     uint32_t cmd_data;
   1143 
   1144     psb_cmdbuf_reg_start_block(cmdbuf, 0);
   1145 
   1146     cmd_data = 0;     /* Entdec Front-End controls    */
   1147     REGIO_WRITE_FIELD_LITE(cmd_data, MSVDX_VEC,  CR_VEC_ENTDEC_FE_CONTROL,  ENTDEC_FE_PROFILE, 1); /* MPEG2 Main Profile */
   1148     REGIO_WRITE_FIELD_LITE(cmd_data, MSVDX_VEC,  CR_VEC_ENTDEC_FE_CONTROL,  ENTDEC_FE_MODE,       3); /* Set MPEG2 mode */
   1149 
   1150     psb_cmdbuf_reg_set(cmdbuf, REGISTER_OFFSET(MSVDX_VEC, CR_VEC_ENTDEC_FE_CONTROL), cmd_data);
   1151 
   1152     psb_cmdbuf_reg_end_block(cmdbuf);
   1153 
   1154     psb_cmdbuf_rendec_start(cmdbuf, RENDEC_REGISTER_OFFSET(MSVDX_VEC, CR_VEC_ENTDEC_BE_CONTROL));
   1155 
   1156     cmd_data = 0;     /* Entdec Back-End controls    */
   1157     REGIO_WRITE_FIELD(cmd_data,
   1158                       MSVDX_VEC,
   1159                       CR_VEC_ENTDEC_BE_CONTROL,
   1160                       ENTDEC_BE_PROFILE,
   1161                       1);                                /* MPEG2 Main Profile        */
   1162 
   1163     REGIO_WRITE_FIELD(cmd_data,
   1164                       MSVDX_VEC,
   1165                       CR_VEC_ENTDEC_BE_CONTROL,
   1166                       ENTDEC_BE_MODE,
   1167                       3);                                /* Set MPEG2 mode            */
   1168 
   1169     psb_cmdbuf_rendec_write(cmdbuf, cmd_data);
   1170 
   1171     psb_cmdbuf_rendec_end(cmdbuf);
   1172 }
   1173 
   1174 static void psb__MPEG2_begin_slice(context_DEC_p dec_ctx, VASliceParameterBufferBase *vld_slice_param)
   1175 {
   1176     VASliceParameterBufferMPEG2 *slice_param = (VASliceParameterBufferMPEG2 *) vld_slice_param;
   1177     context_MPEG2_p ctx = (context_MPEG2_p)dec_ctx;
   1178 
   1179     dec_ctx->bits_offset = slice_param->macroblock_offset;
   1180     /* dec_ctx->SR_flags = 0; */
   1181 }
   1182 static void psb__MPEG2_process_slice_data(context_DEC_p dec_ctx, VASliceParameterBufferBase *vld_slice_param)
   1183 {
   1184     VASliceParameterBufferMPEG2 *slice_param = (VASliceParameterBufferMPEG2 *) vld_slice_param;
   1185     context_MPEG2_p ctx = (context_MPEG2_p)dec_ctx;
   1186 
   1187     psb__MPEG2_set_operating_mode(ctx);
   1188     psb__MPEG2_set_reference_pictures(ctx);
   1189     psb__MPEG2_set_picture_header(ctx, slice_param);
   1190     psb__MPEG2_set_slice_params(ctx);
   1191     psb__MPEG2_write_qmatrices(ctx);
   1192     psb__MPEG2_set_ent_dec(ctx);
   1193 }
   1194 
   1195 static void psb__MPEG2_end_slice(context_DEC_p dec_ctx)
   1196 {
   1197     context_MPEG2_p ctx = (context_MPEG2_p)dec_ctx;
   1198     ctx->obj_context->flags = FW_VA_RENDER_IS_VLD_NOT_MC;
   1199 
   1200     if (ctx->pic_params->picture_coding_extension.bits.progressive_frame)
   1201         ctx->obj_context->last_mb = ((ctx->picture_height_mb - 1) << 8) | (ctx->picture_width_mb - 1);
   1202     else
   1203         ctx->obj_context->last_mb = ((ctx->picture_height_mb / 2 - 1) << 8) | (ctx->picture_width_mb - 1);
   1204 
   1205     *(ctx->dec_ctx.slice_first_pic_last) = (ctx->obj_context->first_mb << 16) | (ctx->obj_context->last_mb);
   1206 }
   1207 
   1208 static void psb__MEPG2_send_highlevel_cmd(context_MPEG2_p ctx)
   1209 {
   1210     uint32_t cmd;
   1211     psb_cmdbuf_p cmdbuf = ctx->obj_context->cmdbuf;
   1212     psb_surface_p target_surface = ctx->obj_context->current_render_target->psb_surface;
   1213     psb_surface_p rotate_surface = ctx->obj_context->current_render_target->out_loop_surface;
   1214 
   1215     psb_cmdbuf_reg_start_block(cmdbuf, 0);
   1216     psb_cmdbuf_reg_set(cmdbuf, REGISTER_OFFSET(MSVDX_CMDS, DISPLAY_PICTURE_SIZE), ctx->display_picture_size);
   1217     psb_cmdbuf_reg_set(cmdbuf, REGISTER_OFFSET(MSVDX_CMDS, CODED_PICTURE_SIZE), ctx->coded_picture_size);
   1218 
   1219     cmd = 0;
   1220     REGIO_WRITE_FIELD(cmd, MSVDX_CMDS, OPERATING_MODE, CHROMA_FORMAT, 1);
   1221     REGIO_WRITE_FIELD(cmd, MSVDX_CMDS, OPERATING_MODE, ASYNC_MODE, 1); // 0 = VDMC and VDEB active.  1 = VDEB pass-thru.
   1222     REGIO_WRITE_FIELD(cmd, MSVDX_CMDS, OPERATING_MODE, CODEC_MODE, 3);        // MPEG2
   1223     REGIO_WRITE_FIELD(cmd, MSVDX_CMDS, OPERATING_MODE, CODEC_PROFILE, 1); // MAIN
   1224     REGIO_WRITE_FIELD(cmd, MSVDX_CMDS, OPERATING_MODE, ROW_STRIDE, target_surface->stride_mode);
   1225     psb_cmdbuf_reg_set(cmdbuf, REGISTER_OFFSET(MSVDX_CMDS, OPERATING_MODE), cmd);
   1226 
   1227     psb_cmdbuf_reg_set_RELOC(cmdbuf, REGISTER_OFFSET(MSVDX_CMDS, LUMA_RECONSTRUCTED_PICTURE_BASE_ADDRESSES),
   1228                              &target_surface->buf, target_surface->buf.buffer_ofs);
   1229 
   1230     psb_cmdbuf_reg_set_RELOC(cmdbuf, REGISTER_OFFSET(MSVDX_CMDS, CHROMA_RECONSTRUCTED_PICTURE_BASE_ADDRESSES),
   1231                              &target_surface->buf, target_surface->buf.buffer_ofs + target_surface->chroma_offset);
   1232 
   1233     psb_cmdbuf_reg_set_RELOC(cmdbuf, REGISTER_OFFSET(MSVDX_CMDS, REFERENCE_PICTURE_BASE_ADDRESSES) + (0 * 8),
   1234                              &target_surface->buf, target_surface->buf.buffer_ofs);
   1235 
   1236     psb_cmdbuf_reg_set_RELOC(cmdbuf, REGISTER_OFFSET(MSVDX_CMDS, REFERENCE_PICTURE_BASE_ADDRESSES) + 4 + (0 * 8),
   1237                              &target_surface->buf, target_surface->buf.buffer_ofs + target_surface->chroma_offset);
   1238 
   1239     psb_cmdbuf_reg_set_RELOC(cmdbuf, REGISTER_OFFSET(MSVDX_CMDS, REFERENCE_PICTURE_BASE_ADDRESSES) + (1 * 8),
   1240                              &target_surface->buf, target_surface->buf.buffer_ofs);
   1241 
   1242     psb_cmdbuf_reg_set_RELOC(cmdbuf, REGISTER_OFFSET(MSVDX_CMDS, REFERENCE_PICTURE_BASE_ADDRESSES) + 4 + (1 * 8),
   1243                              &target_surface->buf, target_surface->buf.buffer_ofs + target_surface->chroma_offset);
   1244 
   1245     cmd = 0;
   1246     REGIO_WRITE_FIELD(cmd, MSVDX_CMDS, SLICE_PARAMS, SLICE_FIELD_TYPE,   2); /* FRAME PICTURE -- ui8SliceFldType */
   1247     REGIO_WRITE_FIELD(cmd, MSVDX_CMDS, SLICE_PARAMS, SLICE_CODE_TYPE,    1); /* P PICTURE -- (ui8PicType == WMF_PTYPE_BI) ? WMF_PTYPE_I : (ui8PicType & 0x3) */
   1248     psb_cmdbuf_reg_set(cmdbuf, REGISTER_OFFSET(MSVDX_CMDS, SLICE_PARAMS), cmd);
   1249     *ctx->dec_ctx.p_slice_params = cmd;
   1250     psb_cmdbuf_reg_end_block(cmdbuf);
   1251 
   1252 
   1253     psb_cmdbuf_reg_start_block(cmdbuf, 0);
   1254     psb_cmdbuf_reg_set_RELOC(cmdbuf, REGISTER_OFFSET(MSVDX_CMDS, VC1_LUMA_RANGE_MAPPING_BASE_ADDRESS),
   1255                              &rotate_surface->buf, rotate_surface->buf.buffer_ofs);
   1256 
   1257     psb_cmdbuf_reg_set_RELOC(cmdbuf, REGISTER_OFFSET(MSVDX_CMDS, VC1_CHROMA_RANGE_MAPPING_BASE_ADDRESS),
   1258                              &rotate_surface->buf, rotate_surface->buf.buffer_ofs + rotate_surface->chroma_offset);
   1259     psb_cmdbuf_reg_end_block(cmdbuf);
   1260 
   1261     RELOC(*ctx->dec_ctx.p_range_mapping_base0, rotate_surface->buf.buffer_ofs, &rotate_surface->buf);
   1262     RELOC(*ctx->dec_ctx.p_range_mapping_base1, rotate_surface->buf.buffer_ofs + rotate_surface->chroma_offset, &rotate_surface->buf);
   1263 }
   1264 
   1265 static void psb__MEPG2_send_blit_cmd(context_MPEG2_p ctx)
   1266 {
   1267     uint32_t cmd;
   1268     psb_cmdbuf_p cmdbuf = ctx->obj_context->cmdbuf;
   1269     psb_surface_p rotate_surface = ctx->obj_context->current_render_target->out_loop_surface;
   1270 
   1271     psb_cmdbuf_reg_start_block(cmdbuf, 0);
   1272     cmd = 0;
   1273     REGIO_WRITE_FIELD_LITE(cmd, MSVDX_CMDS, ALTERNATIVE_OUTPUT_PICTURE_ROTATION , ALT_PICTURE_ENABLE, 1);
   1274     REGIO_WRITE_FIELD_LITE(cmd, MSVDX_CMDS, ALTERNATIVE_OUTPUT_PICTURE_ROTATION , ROTATION_ROW_STRIDE, rotate_surface->stride_mode);
   1275     REGIO_WRITE_FIELD_LITE(cmd, MSVDX_CMDS, ALTERNATIVE_OUTPUT_PICTURE_ROTATION , RECON_WRITE_DISABLE, 0);
   1276     REGIO_WRITE_FIELD_LITE(cmd, MSVDX_CMDS, ALTERNATIVE_OUTPUT_PICTURE_ROTATION , ROTATION_MODE, GET_SURFACE_INFO_rotate(rotate_surface));
   1277     psb_cmdbuf_reg_set(cmdbuf, REGISTER_OFFSET(MSVDX_CMDS, ALTERNATIVE_OUTPUT_PICTURE_ROTATION), cmd);
   1278     psb_cmdbuf_reg_end_block(cmdbuf);
   1279 
   1280     *cmdbuf->cmd_idx++ = 0x40000000; /* CMD_BLIT_CMD */
   1281     *cmdbuf->cmd_idx++ = ctx->picture_width_mb;
   1282     *cmdbuf->cmd_idx++ = ctx->picture_height_mb; /* FIXME */
   1283     *cmdbuf->cmd_idx++ = CMD_COMPLETION;
   1284 }
   1285 
   1286 static void psb__MPEG2_insert_blit_cmd_to_rotate(context_MPEG2_p ctx)
   1287 {
   1288     psb_cmdbuf_p cmdbuf = ctx->obj_context->cmdbuf;
   1289 
   1290     /* See RENDER_BUFFER_HEADER */
   1291     *cmdbuf->cmd_idx++ = CMD_HEADER_VC1;
   1292 
   1293     ctx->dec_ctx.p_range_mapping_base0 = cmdbuf->cmd_idx++;
   1294     ctx->dec_ctx.p_range_mapping_base1 = cmdbuf->cmd_idx++;
   1295 
   1296     *ctx->dec_ctx.p_range_mapping_base0 = 0;
   1297     *ctx->dec_ctx.p_range_mapping_base1 = 0;
   1298 
   1299     ctx->dec_ctx.p_slice_params = cmdbuf->cmd_idx;
   1300     *cmdbuf->cmd_idx++ = 0; /* ui32SliceParams */
   1301 
   1302     *cmdbuf->cmd_idx++ = 0; /* skip two lldma addr field */
   1303     *cmdbuf->cmd_idx++ = 0;
   1304 
   1305     ctx->dec_ctx.slice_first_pic_last = cmdbuf->cmd_idx++;
   1306     *ctx->dec_ctx.slice_first_pic_last = 0;
   1307 
   1308     psb__MEPG2_send_highlevel_cmd(ctx);
   1309     psb__MEPG2_send_blit_cmd(ctx);
   1310 
   1311     ctx->obj_context->video_op = psb_video_mc;
   1312     ctx->obj_context->flags = FW_VA_RENDER_IS_LAST_SLICE;
   1313 
   1314     if (psb_context_submit_cmdbuf(ctx->obj_context)) {
   1315         ASSERT(0);
   1316     }
   1317 }
   1318 
   1319 static VAStatus pnw_MPEG2_BeginPicture(
   1320     object_context_p obj_context)
   1321 {
   1322     INIT_CONTEXT_MPEG2
   1323 
   1324     drv_debug_msg(VIDEO_DEBUG_GENERAL, "pnw_MPEG2_BeginPicture\n");
   1325     if (ctx->pic_params) {
   1326         free(ctx->pic_params);
   1327         ctx->pic_params = NULL;
   1328     }
   1329     ctx->previous_slice_vertical_position = ~1;
   1330 
   1331     return VA_STATUS_SUCCESS;
   1332 }
   1333 
   1334 static VAStatus pnw_MPEG2_process_buffer(
   1335     context_DEC_p dec_ctx,
   1336     object_buffer_p buffer)
   1337 {
   1338     context_MPEG2_p ctx = (context_MPEG2_p)dec_ctx;
   1339     VAStatus vaStatus = VA_STATUS_SUCCESS;
   1340     object_buffer_p obj_buffer = buffer;
   1341 
   1342     switch (obj_buffer->type) {
   1343     case VAPictureParameterBufferType:
   1344         drv_debug_msg(VIDEO_DEBUG_GENERAL, "pnw_MPEG2_RenderPicture got VAPictureParameterBuffer\n");
   1345         vaStatus = psb__MPEG2_process_picture_param(ctx, obj_buffer);
   1346         DEBUG_FAILURE;
   1347         break;
   1348 
   1349     case VAIQMatrixBufferType:
   1350         drv_debug_msg(VIDEO_DEBUG_GENERAL,"pnw_MPEG2_RenderPicture got VAIQMatrixBufferType\n");
   1351         vaStatus = psb__MPEG2_process_iq_matrix(ctx, obj_buffer);
   1352         DEBUG_FAILURE;
   1353         break;
   1354 
   1355     default:
   1356         vaStatus = VA_STATUS_ERROR_UNKNOWN;
   1357         DEBUG_FAILURE;
   1358     }
   1359 
   1360     return vaStatus;
   1361 }
   1362 
   1363 static VAStatus pnw_MPEG2_EndPicture(
   1364     object_context_p obj_context)
   1365 {
   1366     VAStatus vaStatus = VA_STATUS_SUCCESS;
   1367     INIT_CONTEXT_MPEG2
   1368 
   1369     drv_debug_msg(VIDEO_DEBUG_GENERAL, "pnw_MPEG2_EndPicture\n");
   1370 
   1371     if (CONTEXT_ROTATE(ctx->obj_context)) {
   1372         if (!(ctx->pic_params->picture_coding_extension.bits.progressive_frame) &&
   1373             !(ctx->pic_params->picture_coding_extension.bits.is_first_field))
   1374             psb__MPEG2_insert_blit_cmd_to_rotate(ctx);
   1375     }
   1376 
   1377     if (psb_context_flush_cmdbuf(ctx->obj_context)) {
   1378         vaStatus = VA_STATUS_ERROR_UNKNOWN;
   1379     }
   1380 
   1381     if (ctx->pic_params) {
   1382         free(ctx->pic_params);
   1383         ctx->pic_params = NULL;
   1384     }
   1385 
   1386     return vaStatus;
   1387 }
   1388 
   1389 struct format_vtable_s pnw_MPEG2_vtable = {
   1390 queryConfigAttributes:
   1391     pnw_MPEG2_QueryConfigAttributes,
   1392 validateConfig:
   1393     pnw_MPEG2_ValidateConfig,
   1394 createContext:
   1395     pnw_MPEG2_CreateContext,
   1396 destroyContext:
   1397     pnw_MPEG2_DestroyContext,
   1398 beginPicture:
   1399     pnw_MPEG2_BeginPicture,
   1400 renderPicture:
   1401     vld_dec_RenderPicture,
   1402 endPicture:
   1403     pnw_MPEG2_EndPicture
   1404 };
   1405