Home | History | Annotate | Download | only in vl
      1 /**************************************************************************
      2  *
      3  * Copyright 2011 Maarten Lankhorst
      4  * Copyright 2011 Christian Knig
      5  * All Rights Reserved.
      6  *
      7  * Permission is hereby granted, free of charge, to any person obtaining a
      8  * copy of this software and associated documentation files (the
      9  * "Software"), to deal in the Software without restriction, including
     10  * without limitation the rights to use, copy, modify, merge, publish,
     11  * distribute, sub license, and/or sell copies of the Software, and to
     12  * permit persons to whom the Software is furnished to do so, subject to
     13  * the following conditions:
     14  *
     15  * The above copyright notice and this permission notice (including the
     16  * next paragraph) shall be included in all copies or substantial portions
     17  * of the Software.
     18  *
     19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
     20  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
     22  * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
     23  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
     24  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
     25  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     26  *
     27  **************************************************************************/
     28 
     29 #include "pipe/p_video_codec.h"
     30 #include "util/u_memory.h"
     31 
     32 #include "vl_vlc.h"
     33 #include "vl_mpeg12_bitstream.h"
     34 
     35 enum {
     36    dct_End_of_Block = 0xFF,
     37    dct_Escape = 0xFE,
     38    dct_DC = 0xFD,
     39    dct_AC = 0xFC
     40 };
     41 
     42 struct dct_coeff
     43 {
     44    uint8_t length;
     45    uint8_t run;
     46    int16_t level;
     47 };
     48 
     49 struct dct_coeff_compressed
     50 {
     51    uint32_t bitcode;
     52    struct dct_coeff coeff;
     53 };
     54 
     55 /* coding table as found in the spec annex B.5 table B-1 */
     56 static const struct vl_vlc_compressed macroblock_address_increment[] = {
     57    { 0x8000, { 1, 1 } },
     58    { 0x6000, { 3, 2 } },
     59    { 0x4000, { 3, 3 } },
     60    { 0x3000, { 4, 4 } },
     61    { 0x2000, { 4, 5 } },
     62    { 0x1800, { 5, 6 } },
     63    { 0x1000, { 5, 7 } },
     64    { 0x0e00, { 7, 8 } },
     65    { 0x0c00, { 7, 9 } },
     66    { 0x0b00, { 8, 10 } },
     67    { 0x0a00, { 8, 11 } },
     68    { 0x0900, { 8, 12 } },
     69    { 0x0800, { 8, 13 } },
     70    { 0x0700, { 8, 14 } },
     71    { 0x0600, { 8, 15 } },
     72    { 0x05c0, { 10, 16 } },
     73    { 0x0580, { 10, 17 } },
     74    { 0x0540, { 10, 18 } },
     75    { 0x0500, { 10, 19 } },
     76    { 0x04c0, { 10, 20 } },
     77    { 0x0480, { 10, 21 } },
     78    { 0x0460, { 11, 22 } },
     79    { 0x0440, { 11, 23 } },
     80    { 0x0420, { 11, 24 } },
     81    { 0x0400, { 11, 25 } },
     82    { 0x03e0, { 11, 26 } },
     83    { 0x03c0, { 11, 27 } },
     84    { 0x03a0, { 11, 28 } },
     85    { 0x0380, { 11, 29 } },
     86    { 0x0360, { 11, 30 } },
     87    { 0x0340, { 11, 31 } },
     88    { 0x0320, { 11, 32 } },
     89    { 0x0300, { 11, 33 } }
     90 };
     91 
     92 #define Q PIPE_MPEG12_MB_TYPE_QUANT
     93 #define F PIPE_MPEG12_MB_TYPE_MOTION_FORWARD
     94 #define B PIPE_MPEG12_MB_TYPE_MOTION_BACKWARD
     95 #define P PIPE_MPEG12_MB_TYPE_PATTERN
     96 #define I PIPE_MPEG12_MB_TYPE_INTRA
     97 
     98 /* coding table as found in the spec annex B.5 table B-2 */
     99 static const struct vl_vlc_compressed macroblock_type_i[] = {
    100    { 0x8000, { 1, I } },
    101    { 0x4000, { 2, Q|I } }
    102 };
    103 
    104 /* coding table as found in the spec annex B.5 table B-3 */
    105 static const struct vl_vlc_compressed macroblock_type_p[] = {
    106    { 0x8000, { 1, F|P } },
    107    { 0x4000, { 2, P } },
    108    { 0x2000, { 3, F } },
    109    { 0x1800, { 5, I } },
    110    { 0x1000, { 5, Q|F|P } },
    111    { 0x0800, { 5, Q|P } },
    112    { 0x0400, { 6, Q|I } }
    113 };
    114 
    115 /* coding table as found in the spec annex B.5 table B-4 */
    116 static const struct vl_vlc_compressed macroblock_type_b[] = {
    117    { 0x8000, { 2, F|B } },
    118    { 0xC000, { 2, F|B|P } },
    119    { 0x4000, { 3, B } },
    120    { 0x6000, { 3, B|P } },
    121    { 0x2000, { 4, F } },
    122    { 0x3000, { 4, F|P } },
    123    { 0x1800, { 5, I } },
    124    { 0x1000, { 5, Q|F|B|P } },
    125    { 0x0C00, { 6, Q|F|P } },
    126    { 0x0800, { 6, Q|B|P } },
    127    { 0x0400, { 6, Q|I } }
    128 };
    129 
    130 #undef Q
    131 #undef F
    132 #undef B
    133 #undef P
    134 #undef I
    135 
    136 /* coding table as found in the spec annex B.5 table B-9 */
    137 static const struct vl_vlc_compressed coded_block_pattern[] = {
    138    { 0xE000, { 3, 60 } },
    139    { 0xD000, { 4, 4 } },
    140    { 0xC000, { 4, 8 } },
    141    { 0xB000, { 4, 16 } },
    142    { 0xA000, { 4, 32 } },
    143    { 0x9800, { 5, 12 } },
    144    { 0x9000, { 5, 48 } },
    145    { 0x8800, { 5, 20 } },
    146    { 0x8000, { 5, 40 } },
    147    { 0x7800, { 5, 28 } },
    148    { 0x7000, { 5, 44 } },
    149    { 0x6800, { 5, 52 } },
    150    { 0x6000, { 5, 56 } },
    151    { 0x5800, { 5, 1 } },
    152    { 0x5000, { 5, 61 } },
    153    { 0x4800, { 5, 2 } },
    154    { 0x4000, { 5, 62 } },
    155    { 0x3C00, { 6, 24 } },
    156    { 0x3800, { 6, 36 } },
    157    { 0x3400, { 6, 3 } },
    158    { 0x3000, { 6, 63 } },
    159    { 0x2E00, { 7, 5 } },
    160    { 0x2C00, { 7, 9 } },
    161    { 0x2A00, { 7, 17 } },
    162    { 0x2800, { 7, 33 } },
    163    { 0x2600, { 7, 6 } },
    164    { 0x2400, { 7, 10 } },
    165    { 0x2200, { 7, 18 } },
    166    { 0x2000, { 7, 34 } },
    167    { 0x1F00, { 8, 7 } },
    168    { 0x1E00, { 8, 11 } },
    169    { 0x1D00, { 8, 19 } },
    170    { 0x1C00, { 8, 35 } },
    171    { 0x1B00, { 8, 13 } },
    172    { 0x1A00, { 8, 49 } },
    173    { 0x1900, { 8, 21 } },
    174    { 0x1800, { 8, 41 } },
    175    { 0x1700, { 8, 14 } },
    176    { 0x1600, { 8, 50 } },
    177    { 0x1500, { 8, 22 } },
    178    { 0x1400, { 8, 42 } },
    179    { 0x1300, { 8, 15 } },
    180    { 0x1200, { 8, 51 } },
    181    { 0x1100, { 8, 23 } },
    182    { 0x1000, { 8, 43 } },
    183    { 0x0F00, { 8, 25 } },
    184    { 0x0E00, { 8, 37 } },
    185    { 0x0D00, { 8, 26 } },
    186    { 0x0C00, { 8, 38 } },
    187    { 0x0B00, { 8, 29 } },
    188    { 0x0A00, { 8, 45 } },
    189    { 0x0900, { 8, 53 } },
    190    { 0x0800, { 8, 57 } },
    191    { 0x0700, { 8, 30 } },
    192    { 0x0600, { 8, 46 } },
    193    { 0x0500, { 8, 54 } },
    194    { 0x0400, { 8, 58 } },
    195    { 0x0380, { 9, 31 } },
    196    { 0x0300, { 9, 47 } },
    197    { 0x0280, { 9, 55 } },
    198    { 0x0200, { 9, 59 } },
    199    { 0x0180, { 9, 27 } },
    200    { 0x0100, { 9, 39 } },
    201    { 0x0080, { 9, 0 } }
    202 };
    203 
    204 /* coding table as found in the spec annex B.5 table B-10 */
    205 static const struct vl_vlc_compressed motion_code[] = {
    206    { 0x0320, { 11, -16 } },
    207    { 0x0360, { 11, -15 } },
    208    { 0x03a0, { 11, -14 } },
    209    { 0x03e0, { 11, -13 } },
    210    { 0x0420, { 11, -12 } },
    211    { 0x0460, { 11, -11 } },
    212    { 0x04c0, { 10, -10 } },
    213    { 0x0540, { 10, -9 } },
    214    { 0x05c0, { 10, -8 } },
    215    { 0x0700, { 8, -7 } },
    216    { 0x0900, { 8, -6 } },
    217    { 0x0b00, { 8, -5 } },
    218    { 0x0e00, { 7, -4 } },
    219    { 0x1800, { 5, -3 } },
    220    { 0x3000, { 4, -2 } },
    221    { 0x6000, { 3, -1 } },
    222    { 0x8000, { 1, 0 } },
    223    { 0x4000, { 3, 1 } },
    224    { 0x2000, { 4, 2 } },
    225    { 0x1000, { 5, 3 } },
    226    { 0x0c00, { 7, 4 } },
    227    { 0x0a00, { 8, 5 } },
    228    { 0x0800, { 8, 6 } },
    229    { 0x0600, { 8, 7 } },
    230    { 0x0580, { 10, 8 } },
    231    { 0x0500, { 10, 9 } },
    232    { 0x0480, { 10, 10 } },
    233    { 0x0440, { 11, 11 } },
    234    { 0x0400, { 11, 12 } },
    235    { 0x03c0, { 11, 13 } },
    236    { 0x0380, { 11, 14 } },
    237    { 0x0340, { 11, 15 } },
    238    { 0x0300, { 11, 16 } }
    239 };
    240 
    241 /* coding table as found in the spec annex B.5 table B-11 */
    242 static const struct vl_vlc_compressed dmvector[] = {
    243    { 0x0000, { 1, 0 } },
    244    { 0x8000, { 2, 1 } },
    245    { 0xc000, { 2, -1 } }
    246 };
    247 
    248 /* coding table as found in the spec annex B.5 table B-12 */
    249 static const struct vl_vlc_compressed dct_dc_size_luminance[] = {
    250    { 0x8000, { 3, 0 } },
    251    { 0x0000, { 2, 1 } },
    252    { 0x4000, { 2, 2 } },
    253    { 0xA000, { 3, 3 } },
    254    { 0xC000, { 3, 4 } },
    255    { 0xE000, { 4, 5 } },
    256    { 0xF000, { 5, 6 } },
    257    { 0xF800, { 6, 7 } },
    258    { 0xFC00, { 7, 8 } },
    259    { 0xFE00, { 8, 9 } },
    260    { 0xFF00, { 9, 10 } },
    261    { 0xFF80, { 9, 11 } }
    262 };
    263 
    264 /* coding table as found in the spec annex B.5 table B-13 */
    265 static const struct vl_vlc_compressed dct_dc_size_chrominance[] = {
    266    { 0x0000, { 2, 0 } },
    267    { 0x4000, { 2, 1 } },
    268    { 0x8000, { 2, 2 } },
    269    { 0xC000, { 3, 3 } },
    270    { 0xE000, { 4, 4 } },
    271    { 0xF000, { 5, 5 } },
    272    { 0xF800, { 6, 6 } },
    273    { 0xFC00, { 7, 7 } },
    274    { 0xFE00, { 8, 8 } },
    275    { 0xFF00, { 9, 9 } },
    276    { 0xFF80, { 10, 10 } },
    277    { 0xFFC0, { 10, 11 } }
    278 };
    279 
    280 /* coding table as found in the spec annex B.5 table B-14 */
    281 static const struct dct_coeff_compressed dct_coeff_tbl_zero[] = {
    282    { 0x8000, { 2, dct_End_of_Block, 0 } },
    283    { 0x8000, { 1, dct_DC, 1 } },
    284    { 0xC000, { 2, dct_AC, 1 } },
    285    { 0x6000, { 3, 1, 1 } },
    286    { 0x4000, { 4, 0, 2 } },
    287    { 0x5000, { 4, 2, 1 } },
    288    { 0x2800, { 5, 0, 3 } },
    289    { 0x3800, { 5, 3, 1 } },
    290    { 0x3000, { 5, 4, 1 } },
    291    { 0x1800, { 6, 1, 2 } },
    292    { 0x1C00, { 6, 5, 1 } },
    293    { 0x1400, { 6, 6, 1 } },
    294    { 0x1000, { 6, 7, 1 } },
    295    { 0x0C00, { 7, 0, 4 } },
    296    { 0x0800, { 7, 2, 2 } },
    297    { 0x0E00, { 7, 8, 1 } },
    298    { 0x0A00, { 7, 9, 1 } },
    299    { 0x0400, { 6, dct_Escape, 0 } },
    300    { 0x2600, { 8, 0, 5 } },
    301    { 0x2100, { 8, 0, 6 } },
    302    { 0x2500, { 8, 1, 3 } },
    303    { 0x2400, { 8, 3, 2 } },
    304    { 0x2700, { 8, 10, 1 } },
    305    { 0x2300, { 8, 11, 1 } },
    306    { 0x2200, { 8, 12, 1 } },
    307    { 0x2000, { 8, 13, 1 } },
    308    { 0x0280, { 10, 0, 7 } },
    309    { 0x0300, { 10, 1, 4 } },
    310    { 0x02C0, { 10, 2, 3 } },
    311    { 0x03C0, { 10, 4, 2 } },
    312    { 0x0240, { 10, 5, 2 } },
    313    { 0x0380, { 10, 14, 1 } },
    314    { 0x0340, { 10, 15, 1 } },
    315    { 0x0200, { 10, 16, 1 } },
    316    { 0x01D0, { 12, 0, 8 } },
    317    { 0x0180, { 12, 0, 9 } },
    318    { 0x0130, { 12, 0, 10 } },
    319    { 0x0100, { 12, 0, 11 } },
    320    { 0x01B0, { 12, 1, 5 } },
    321    { 0x0140, { 12, 2, 4 } },
    322    { 0x01C0, { 12, 3, 3 } },
    323    { 0x0120, { 12, 4, 3 } },
    324    { 0x01E0, { 12, 6, 2 } },
    325    { 0x0150, { 12, 7, 2 } },
    326    { 0x0110, { 12, 8, 2 } },
    327    { 0x01F0, { 12, 17, 1 } },
    328    { 0x01A0, { 12, 18, 1 } },
    329    { 0x0190, { 12, 19, 1 } },
    330    { 0x0170, { 12, 20, 1 } },
    331    { 0x0160, { 12, 21, 1 } },
    332    { 0x00D0, { 13, 0, 12 } },
    333    { 0x00C8, { 13, 0, 13 } },
    334    { 0x00C0, { 13, 0, 14 } },
    335    { 0x00B8, { 13, 0, 15 } },
    336    { 0x00B0, { 13, 1, 6 } },
    337    { 0x00A8, { 13, 1, 7 } },
    338    { 0x00A0, { 13, 2, 5 } },
    339    { 0x0098, { 13, 3, 4 } },
    340    { 0x0090, { 13, 5, 3 } },
    341    { 0x0088, { 13, 9, 2 } },
    342    { 0x0080, { 13, 10, 2 } },
    343    { 0x00F8, { 13, 22, 1 } },
    344    { 0x00F0, { 13, 23, 1 } },
    345    { 0x00E8, { 13, 24, 1 } },
    346    { 0x00E0, { 13, 25, 1 } },
    347    { 0x00D8, { 13, 26, 1 } },
    348    { 0x007C, { 14, 0, 16 } },
    349    { 0x0078, { 14, 0, 17 } },
    350    { 0x0074, { 14, 0, 18 } },
    351    { 0x0070, { 14, 0, 19 } },
    352    { 0x006C, { 14, 0, 20 } },
    353    { 0x0068, { 14, 0, 21 } },
    354    { 0x0064, { 14, 0, 22 } },
    355    { 0x0060, { 14, 0, 23 } },
    356    { 0x005C, { 14, 0, 24 } },
    357    { 0x0058, { 14, 0, 25 } },
    358    { 0x0054, { 14, 0, 26 } },
    359    { 0x0050, { 14, 0, 27 } },
    360    { 0x004C, { 14, 0, 28 } },
    361    { 0x0048, { 14, 0, 29 } },
    362    { 0x0044, { 14, 0, 30 } },
    363    { 0x0040, { 14, 0, 31 } },
    364    { 0x0030, { 15, 0, 32 } },
    365    { 0x002E, { 15, 0, 33 } },
    366    { 0x002C, { 15, 0, 34 } },
    367    { 0x002A, { 15, 0, 35 } },
    368    { 0x0028, { 15, 0, 36 } },
    369    { 0x0026, { 15, 0, 37 } },
    370    { 0x0024, { 15, 0, 38 } },
    371    { 0x0022, { 15, 0, 39 } },
    372    { 0x0020, { 15, 0, 40 } },
    373    { 0x003E, { 15, 1, 8 } },
    374    { 0x003C, { 15, 1, 9 } },
    375    { 0x003A, { 15, 1, 10 } },
    376    { 0x0038, { 15, 1, 11 } },
    377    { 0x0036, { 15, 1, 12 } },
    378    { 0x0034, { 15, 1, 13 } },
    379    { 0x0032, { 15, 1, 14 } },
    380    { 0x0013, { 16, 1, 15 } },
    381    { 0x0012, { 16, 1, 16 } },
    382    { 0x0011, { 16, 1, 17 } },
    383    { 0x0010, { 16, 1, 18 } },
    384    { 0x0014, { 16, 6, 3 } },
    385    { 0x001A, { 16, 11, 2 } },
    386    { 0x0019, { 16, 12, 2 } },
    387    { 0x0018, { 16, 13, 2 } },
    388    { 0x0017, { 16, 14, 2 } },
    389    { 0x0016, { 16, 15, 2 } },
    390    { 0x0015, { 16, 16, 2 } },
    391    { 0x001F, { 16, 27, 1 } },
    392    { 0x001E, { 16, 28, 1 } },
    393    { 0x001D, { 16, 29, 1 } },
    394    { 0x001C, { 16, 30, 1 } },
    395    { 0x001B, { 16, 31, 1 } }
    396 };
    397 
    398 /* coding table as found in the spec annex B.5 table B-15 */
    399 static const struct dct_coeff_compressed dct_coeff_tbl_one[] = {
    400    { 0x6000, { 4, dct_End_of_Block, 0 } },
    401    { 0x8000, { 2, 0, 1 } },
    402    { 0x4000, { 3, 1, 1 } },
    403    { 0xC000, { 3, 0, 2 } },
    404    { 0x2800, { 5, 2, 1 } },
    405    { 0x7000, { 4, 0, 3 } },
    406    { 0x3800, { 5, 3, 1 } },
    407    { 0x1800, { 6, 4, 1 } },
    408    { 0x3000, { 5, 1, 2 } },
    409    { 0x1C00, { 6, 5, 1 } },
    410    { 0x0C00, { 7, 6, 1 } },
    411    { 0x0800, { 7, 7, 1 } },
    412    { 0xE000, { 5, 0, 4 } },
    413    { 0x0E00, { 7, 2, 2 } },
    414    { 0x0A00, { 7, 8, 1 } },
    415    { 0xF000, { 7, 9, 1 } },
    416    { 0x0400, { 6, dct_Escape, 0 } },
    417    { 0xE800, { 5, 0, 5 } },
    418    { 0x1400, { 6, 0, 6 } },
    419    { 0xF200, { 7, 1, 3 } },
    420    { 0x2600, { 8, 3, 2 } },
    421    { 0xF400, { 7, 10, 1 } },
    422    { 0x2100, { 8, 11, 1 } },
    423    { 0x2500, { 8, 12, 1 } },
    424    { 0x2400, { 8, 13, 1 } },
    425    { 0x1000, { 6, 0, 7 } },
    426    { 0x2700, { 8, 1, 4 } },
    427    { 0xFC00, { 8, 2, 3 } },
    428    { 0xFD00, { 8, 4, 2 } },
    429    { 0x0200, { 9, 5, 2 } },
    430    { 0x0280, { 9, 14, 1 } },
    431    { 0x0380, { 9, 15, 1 } },
    432    { 0x0340, { 10, 16, 1 } },
    433    { 0xF600, { 7, 0, 8 } },
    434    { 0xF800, { 7, 0, 9 } },
    435    { 0x2300, { 8, 0, 10 } },
    436    { 0x2200, { 8, 0, 11 } },
    437    { 0x2000, { 8, 1, 5 } },
    438    { 0x0300, { 10, 2, 4 } },
    439    { 0x01C0, { 12, 3, 3 } },
    440    { 0x0120, { 12, 4, 3 } },
    441    { 0x01E0, { 12, 6, 2 } },
    442    { 0x0150, { 12, 7, 2 } },
    443    { 0x0110, { 12, 8, 2 } },
    444    { 0x01F0, { 12, 17, 1 } },
    445    { 0x01A0, { 12, 18, 1 } },
    446    { 0x0190, { 12, 19, 1 } },
    447    { 0x0170, { 12, 20, 1 } },
    448    { 0x0160, { 12, 21, 1 } },
    449    { 0xFA00, { 8, 0, 12 } },
    450    { 0xFB00, { 8, 0, 13 } },
    451    { 0xFE00, { 8, 0, 14 } },
    452    { 0xFF00, { 8, 0, 15 } },
    453    { 0x00B0, { 13, 1, 6 } },
    454    { 0x00A8, { 13, 1, 7 } },
    455    { 0x00A0, { 13, 2, 5 } },
    456    { 0x0098, { 13, 3, 4 } },
    457    { 0x0090, { 13, 5, 3 } },
    458    { 0x0088, { 13, 9, 2 } },
    459    { 0x0080, { 13, 10, 2 } },
    460    { 0x00F8, { 13, 22, 1 } },
    461    { 0x00F0, { 13, 23, 1 } },
    462    { 0x00E8, { 13, 24, 1 } },
    463    { 0x00E0, { 13, 25, 1 } },
    464    { 0x00D8, { 13, 26, 1 } },
    465    { 0x007C, { 14, 0, 16 } },
    466    { 0x0078, { 14, 0, 17 } },
    467    { 0x0074, { 14, 0, 18 } },
    468    { 0x0070, { 14, 0, 19 } },
    469    { 0x006C, { 14, 0, 20 } },
    470    { 0x0068, { 14, 0, 21 } },
    471    { 0x0064, { 14, 0, 22 } },
    472    { 0x0060, { 14, 0, 23 } },
    473    { 0x005C, { 14, 0, 24 } },
    474    { 0x0058, { 14, 0, 25 } },
    475    { 0x0054, { 14, 0, 26 } },
    476    { 0x0050, { 14, 0, 27 } },
    477    { 0x004C, { 14, 0, 28 } },
    478    { 0x0048, { 14, 0, 29 } },
    479    { 0x0044, { 14, 0, 30 } },
    480    { 0x0040, { 14, 0, 31 } },
    481    { 0x0030, { 15, 0, 32 } },
    482    { 0x002E, { 15, 0, 33 } },
    483    { 0x002C, { 15, 0, 34 } },
    484    { 0x002A, { 15, 0, 35 } },
    485    { 0x0028, { 15, 0, 36 } },
    486    { 0x0026, { 15, 0, 37 } },
    487    { 0x0024, { 15, 0, 38 } },
    488    { 0x0022, { 15, 0, 39 } },
    489    { 0x0020, { 15, 0, 40 } },
    490    { 0x003E, { 15, 1, 8 } },
    491    { 0x003C, { 15, 1, 9 } },
    492    { 0x003A, { 15, 1, 10 } },
    493    { 0x0038, { 15, 1, 11 } },
    494    { 0x0036, { 15, 1, 12 } },
    495    { 0x0034, { 15, 1, 13 } },
    496    { 0x0032, { 15, 1, 14 } },
    497    { 0x0013, { 16, 1, 15 } },
    498    { 0x0012, { 16, 1, 16 } },
    499    { 0x0011, { 16, 1, 17 } },
    500    { 0x0010, { 16, 1, 18 } },
    501    { 0x0014, { 16, 6, 3 } },
    502    { 0x001A, { 16, 11, 2 } },
    503    { 0x0019, { 16, 12, 2 } },
    504    { 0x0018, { 16, 13, 2 } },
    505    { 0x0017, { 16, 14, 2 } },
    506    { 0x0016, { 16, 15, 2 } },
    507    { 0x0015, { 16, 16, 2 } },
    508    { 0x001F, { 16, 27, 1 } },
    509    { 0x001E, { 16, 28, 1 } },
    510    { 0x001D, { 16, 29, 1 } },
    511    { 0x001C, { 16, 30, 1 } },
    512    { 0x001B, { 16, 31, 1 } }
    513 };
    514 
    515 /* q_scale_type */
    516 static const unsigned quant_scale[2][32] = {
    517   { 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30,
    518     32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62 },
    519   { 0, 1, 2, 3, 4,  5,  6,  7,  8, 10, 12, 14, 16, 18, 20, 22, 24,
    520     28, 32, 36, 40, 44, 48, 52, 56, 64, 72, 80, 88, 96, 104, 112 }
    521 };
    522 
    523 static struct vl_vlc_entry tbl_B1[1 << 11];
    524 static struct vl_vlc_entry tbl_B2[1 << 2];
    525 static struct vl_vlc_entry tbl_B3[1 << 6];
    526 static struct vl_vlc_entry tbl_B4[1 << 6];
    527 static struct vl_vlc_entry tbl_B9[1 << 9];
    528 static struct vl_vlc_entry tbl_B10[1 << 11];
    529 static struct vl_vlc_entry tbl_B11[1 << 2];
    530 static struct vl_vlc_entry tbl_B12[1 << 10];
    531 static struct vl_vlc_entry tbl_B13[1 << 10];
    532 static struct dct_coeff tbl_B14_DC[1 << 17];
    533 static struct dct_coeff tbl_B14_AC[1 << 17];
    534 static struct dct_coeff tbl_B15[1 << 17];
    535 
    536 static inline void
    537 init_dct_coeff_table(struct dct_coeff *dst, const struct dct_coeff_compressed *src,
    538                      unsigned size, bool is_DC)
    539 {
    540    unsigned i;
    541 
    542    for (i=0;i<(1<<17);++i) {
    543       dst[i].length = 0;
    544       dst[i].level = 0;
    545       dst[i].run = dct_End_of_Block;
    546    }
    547 
    548    for(; size > 0; --size, ++src) {
    549       struct dct_coeff coeff = src->coeff;
    550       bool has_sign = true;
    551 
    552       switch (coeff.run) {
    553       case dct_End_of_Block:
    554          if (is_DC)
    555             continue;
    556 
    557          has_sign = false;
    558          break;
    559 
    560       case dct_Escape:
    561          has_sign = false;
    562          break;
    563 
    564       case dct_DC:
    565          if (!is_DC)
    566             continue;
    567 
    568          coeff.length += 1;
    569          coeff.run = 1;
    570          break;
    571 
    572       case dct_AC:
    573          if (is_DC)
    574             continue;
    575 
    576          coeff.length += 1;
    577          coeff.run = 1;
    578          break;
    579 
    580       default:
    581          coeff.length += 1;
    582          coeff.run += 1;
    583          break;
    584       }
    585 
    586       for(i = 0; i < (1u << (17 - coeff.length)); ++i)
    587          dst[src->bitcode << 1 | i] = coeff;
    588 
    589       if (has_sign) {
    590 	 coeff.level = -coeff.level;
    591          for(; i < (1u << (18 - coeff.length)); ++i)
    592             dst[src->bitcode << 1 | i] = coeff;
    593       }
    594    }
    595 }
    596 
    597 static inline void
    598 init_tables()
    599 {
    600    vl_vlc_init_table(tbl_B1, ARRAY_SIZE(tbl_B1), macroblock_address_increment, ARRAY_SIZE(macroblock_address_increment));
    601    vl_vlc_init_table(tbl_B2, ARRAY_SIZE(tbl_B2), macroblock_type_i, ARRAY_SIZE(macroblock_type_i));
    602    vl_vlc_init_table(tbl_B3, ARRAY_SIZE(tbl_B3), macroblock_type_p, ARRAY_SIZE(macroblock_type_p));
    603    vl_vlc_init_table(tbl_B4, ARRAY_SIZE(tbl_B4), macroblock_type_b, ARRAY_SIZE(macroblock_type_b));
    604    vl_vlc_init_table(tbl_B9, ARRAY_SIZE(tbl_B9), coded_block_pattern, ARRAY_SIZE(coded_block_pattern));
    605    vl_vlc_init_table(tbl_B10, ARRAY_SIZE(tbl_B10), motion_code, ARRAY_SIZE(motion_code));
    606    vl_vlc_init_table(tbl_B11, ARRAY_SIZE(tbl_B11), dmvector, ARRAY_SIZE(dmvector));
    607    vl_vlc_init_table(tbl_B12, ARRAY_SIZE(tbl_B12), dct_dc_size_luminance, ARRAY_SIZE(dct_dc_size_luminance));
    608    vl_vlc_init_table(tbl_B13, ARRAY_SIZE(tbl_B13), dct_dc_size_chrominance, ARRAY_SIZE(dct_dc_size_chrominance));
    609    init_dct_coeff_table(tbl_B14_DC, dct_coeff_tbl_zero, ARRAY_SIZE(dct_coeff_tbl_zero), true);
    610    init_dct_coeff_table(tbl_B14_AC, dct_coeff_tbl_zero, ARRAY_SIZE(dct_coeff_tbl_zero), false);
    611    init_dct_coeff_table(tbl_B15, dct_coeff_tbl_one, ARRAY_SIZE(dct_coeff_tbl_one), false);
    612 }
    613 
    614 static inline int
    615 DIV2DOWN(int todiv)
    616 {
    617    return (todiv&~1)/2;
    618 }
    619 
    620 static inline int
    621 DIV2UP(int todiv)
    622 {
    623    return (todiv+1)/2;
    624 }
    625 
    626 static inline void
    627 motion_vector(struct vl_mpg12_bs *bs, int r, int s, int dmv, short delta[2], short dmvector[2])
    628 {
    629    int t;
    630    for (t = 0; t < 2; ++t) {
    631       int motion_code;
    632       int r_size = bs->desc->f_code[s][t];
    633 
    634       vl_vlc_fillbits(&bs->vlc);
    635       motion_code = vl_vlc_get_vlclbf(&bs->vlc, tbl_B10, 11);
    636 
    637       assert(r_size >= 0);
    638       if (r_size && motion_code) {
    639          int residual = vl_vlc_get_uimsbf(&bs->vlc, r_size) + 1;
    640          delta[t] = ((abs(motion_code) - 1) << r_size) + residual;
    641          if (motion_code < 0)
    642             delta[t] = -delta[t];
    643       } else
    644          delta[t] = motion_code;
    645       if (dmv)
    646          dmvector[t] = vl_vlc_get_vlclbf(&bs->vlc, tbl_B11, 2);
    647    }
    648 }
    649 
    650 static inline int
    651 wrap(short f, int shift)
    652 {
    653    if (f < (-16 << shift))
    654       return f + (32 << shift);
    655    else if (f >= 16 << shift)
    656       return f - (32 << shift);
    657    else
    658       return f;
    659 }
    660 
    661 static inline void
    662 motion_vector_frame(struct vl_mpg12_bs *bs, int s, struct pipe_mpeg12_macroblock *mb)
    663 {
    664    int dmv = mb->macroblock_modes.bits.frame_motion_type == PIPE_MPEG12_MO_TYPE_DUAL_PRIME;
    665    short dmvector[2], delta[2];
    666 
    667    if (mb->macroblock_modes.bits.frame_motion_type == PIPE_MPEG12_MO_TYPE_FIELD) {
    668       mb->motion_vertical_field_select |= vl_vlc_get_uimsbf(&bs->vlc, 1) << s;
    669       motion_vector(bs, 0, s, dmv, delta, dmvector);
    670       mb->PMV[0][s][0] = wrap(mb->PMV[0][s][0] + delta[0], bs->desc->f_code[s][0]);
    671       mb->PMV[0][s][1] = wrap(DIV2DOWN(mb->PMV[0][s][1]) + delta[1], bs->desc->f_code[s][1]) * 2;
    672 
    673       mb->motion_vertical_field_select |= vl_vlc_get_uimsbf(&bs->vlc, 1) << (s + 2);
    674       motion_vector(bs, 1, s, dmv, delta, dmvector);
    675       mb->PMV[1][s][0] = wrap(mb->PMV[1][s][0] + delta[0], bs->desc->f_code[s][0]);
    676       mb->PMV[1][s][1] = wrap(DIV2DOWN(mb->PMV[1][s][1]) + delta[1], bs->desc->f_code[s][1]) * 2;
    677 
    678    } else {
    679       motion_vector(bs, 0, s, dmv, delta, dmvector);
    680       mb->PMV[0][s][0] = wrap(mb->PMV[0][s][0] + delta[0], bs->desc->f_code[s][0]);
    681       mb->PMV[0][s][1] = wrap(mb->PMV[0][s][1] + delta[1], bs->desc->f_code[s][1]);
    682    }
    683 }
    684 
    685 static inline void
    686 motion_vector_field(struct vl_mpg12_bs *bs, int s, struct pipe_mpeg12_macroblock *mb)
    687 {
    688    int dmv = mb->macroblock_modes.bits.field_motion_type == PIPE_MPEG12_MO_TYPE_DUAL_PRIME;
    689    short dmvector[2], delta[2];
    690 
    691    if (mb->macroblock_modes.bits.field_motion_type == PIPE_MPEG12_MO_TYPE_16x8) {
    692       mb->motion_vertical_field_select |= vl_vlc_get_uimsbf(&bs->vlc, 1) << s;
    693       motion_vector(bs, 0, s, dmv, delta, dmvector);
    694 
    695       mb->motion_vertical_field_select |= vl_vlc_get_uimsbf(&bs->vlc, 1) << (s + 2);
    696       motion_vector(bs, 1, s, dmv, delta, dmvector);
    697    } else {
    698       if (!dmv)
    699          mb->motion_vertical_field_select |= vl_vlc_get_uimsbf(&bs->vlc, 1) << s;
    700       motion_vector(bs, 0, s, dmv, delta, dmvector);
    701    }
    702 }
    703 
    704 static inline void
    705 reset_predictor(struct vl_mpg12_bs *bs) {
    706    bs->pred_dc[0] = bs->pred_dc[1] = bs->pred_dc[2] = 0;
    707 }
    708 
    709 static inline void
    710 decode_dct(struct vl_mpg12_bs *bs, struct pipe_mpeg12_macroblock *mb, int scale)
    711 {
    712    static const unsigned blk2cc[] = { 0, 0, 0, 0, 1, 2 };
    713    static const struct vl_vlc_entry *blk2dcsize[] = {
    714       tbl_B12, tbl_B12, tbl_B12, tbl_B12, tbl_B13, tbl_B13
    715    };
    716 
    717    bool intra = mb->macroblock_type & PIPE_MPEG12_MB_TYPE_INTRA;
    718    const struct dct_coeff *table = intra ? bs->intra_dct_tbl : tbl_B14_AC;
    719    const struct dct_coeff *entry;
    720    int i, cbp, blk = 0;
    721    short *dst = mb->blocks;
    722 
    723    vl_vlc_fillbits(&bs->vlc);
    724    mb->coded_block_pattern = cbp = intra ? 0x3F : vl_vlc_get_vlclbf(&bs->vlc, tbl_B9, 9);
    725 
    726    goto entry;
    727 
    728    while(1) {
    729       vl_vlc_eatbits(&bs->vlc, entry->length);
    730       if (entry->run == dct_End_of_Block) {
    731 
    732 next_d:
    733          dst += 64;
    734          cbp <<= 1;
    735          cbp &= 0x3F;
    736          blk++;
    737 
    738 entry:
    739          if (!cbp)
    740             break;
    741 
    742          while(!(cbp & 0x20)) {
    743             cbp <<= 1;
    744             blk++;
    745          }
    746 
    747          vl_vlc_fillbits(&bs->vlc);
    748 
    749          if (intra) {
    750             unsigned cc = blk2cc[blk];
    751             unsigned size = vl_vlc_get_vlclbf(&bs->vlc, blk2dcsize[blk], 10);
    752 
    753             if (size) {
    754                int dct_diff = vl_vlc_get_uimsbf(&bs->vlc, size);
    755                int half_range = 1 << (size - 1);
    756                if (dct_diff < half_range)
    757                   dct_diff = (dct_diff + 1) - (2 * half_range);
    758                bs->pred_dc[cc] += dct_diff;
    759             }
    760 
    761             dst[0] = bs->pred_dc[cc];
    762             i = 0;
    763 
    764             if (bs->desc->picture_coding_type == PIPE_MPEG12_PICTURE_CODING_TYPE_D)
    765                goto next_d;
    766          } else {
    767             entry = tbl_B14_DC + vl_vlc_peekbits(&bs->vlc, 17);
    768             i = -1;
    769             continue;
    770          }
    771 
    772       } else if (entry->run == dct_Escape &&
    773                  bs->decoder->profile == PIPE_VIDEO_PROFILE_MPEG1) {
    774          i += vl_vlc_get_uimsbf(&bs->vlc, 6) + 1;
    775          if (i > 64)
    776             break;
    777 
    778          dst[i] = vl_vlc_get_simsbf(&bs->vlc, 8);
    779          if (dst[i] == -128)
    780             dst[i] = vl_vlc_get_uimsbf(&bs->vlc, 8) - 256;
    781          else if (dst[i] == 0)
    782             dst[i] = vl_vlc_get_uimsbf(&bs->vlc, 8);
    783 
    784          dst[i] *= scale;
    785       } else if (entry->run == dct_Escape) {
    786          i += vl_vlc_get_uimsbf(&bs->vlc, 6) + 1;
    787          if (i > 64)
    788             break;
    789 
    790          dst[i] = vl_vlc_get_simsbf(&bs->vlc, 12) * scale;
    791 
    792       } else {
    793          i += entry->run;
    794          if (i > 64)
    795             break;
    796 
    797          dst[i] = entry->level * scale;
    798       }
    799 
    800       vl_vlc_fillbits(&bs->vlc);
    801       entry = table + vl_vlc_peekbits(&bs->vlc, 17);
    802    }
    803 
    804    if (bs->desc->picture_coding_type == PIPE_MPEG12_PICTURE_CODING_TYPE_D)
    805       vl_vlc_eatbits(&bs->vlc, 1);
    806 }
    807 
    808 static inline void
    809 decode_slice(struct vl_mpg12_bs *bs, struct pipe_video_buffer *target)
    810 {
    811    struct pipe_mpeg12_macroblock mb;
    812    short dct_blocks[64*6];
    813    unsigned dct_scale;
    814    signed x = -1;
    815 
    816    memset(&mb, 0, sizeof(mb));
    817    mb.base.codec = PIPE_VIDEO_FORMAT_MPEG12;
    818    mb.y = vl_vlc_get_uimsbf(&bs->vlc, 8) - 1;
    819    mb.blocks = dct_blocks;
    820 
    821    reset_predictor(bs);
    822    vl_vlc_fillbits(&bs->vlc);
    823    dct_scale = quant_scale[bs->desc->q_scale_type][vl_vlc_get_uimsbf(&bs->vlc, 5)];
    824 
    825    if (vl_vlc_get_uimsbf(&bs->vlc, 1))
    826       while (vl_vlc_get_uimsbf(&bs->vlc, 9) & 1)
    827          vl_vlc_fillbits(&bs->vlc);
    828 
    829    vl_vlc_fillbits(&bs->vlc);
    830    assert(vl_vlc_peekbits(&bs->vlc, 23));
    831    do {
    832       int inc = 0;
    833 
    834       while (1) {
    835          /* MPEG-1 macroblock stuffing, can appear an arbitrary number of times. */
    836          while (vl_vlc_peekbits(&bs->vlc, 11) == 15) {
    837             vl_vlc_eatbits(&bs->vlc, 11);
    838             vl_vlc_fillbits(&bs->vlc);
    839          }
    840 
    841          if (vl_vlc_peekbits(&bs->vlc, 11) == 8) {
    842             vl_vlc_eatbits(&bs->vlc, 11);
    843             vl_vlc_fillbits(&bs->vlc);
    844             inc += 33;
    845          } else {
    846             inc += vl_vlc_get_vlclbf(&bs->vlc, tbl_B1, 11);
    847             break;
    848          }
    849       }
    850 
    851       if (x != -1) {
    852          if (!inc)
    853             return;
    854          mb.num_skipped_macroblocks = inc - 1;
    855          bs->decoder->decode_macroblock(bs->decoder, target, &bs->desc->base, &mb.base, 1);
    856       }
    857       mb.x = x += inc;
    858       if (bs->decoder->profile == PIPE_VIDEO_PROFILE_MPEG1) {
    859          int width = align(bs->decoder->width, 16) / 16;
    860          mb.y += mb.x / width;
    861          mb.x = x %= width;
    862       }
    863 
    864       switch (bs->desc->picture_coding_type) {
    865       case PIPE_MPEG12_PICTURE_CODING_TYPE_I:
    866          mb.macroblock_type = vl_vlc_get_vlclbf(&bs->vlc, tbl_B2, 2);
    867          break;
    868 
    869       case PIPE_MPEG12_PICTURE_CODING_TYPE_P:
    870          mb.macroblock_type = vl_vlc_get_vlclbf(&bs->vlc, tbl_B3, 6);
    871          break;
    872 
    873       case PIPE_MPEG12_PICTURE_CODING_TYPE_B:
    874          mb.macroblock_type = vl_vlc_get_vlclbf(&bs->vlc, tbl_B4, 6);
    875          break;
    876 
    877       case PIPE_MPEG12_PICTURE_CODING_TYPE_D:
    878          vl_vlc_eatbits(&bs->vlc, 1);
    879          mb.macroblock_type = PIPE_MPEG12_MB_TYPE_INTRA;
    880          break;
    881       }
    882 
    883       mb.macroblock_modes.value = 0;
    884       if (mb.macroblock_type & (PIPE_MPEG12_MB_TYPE_MOTION_FORWARD | PIPE_MPEG12_MB_TYPE_MOTION_BACKWARD)) {
    885          if (bs->desc->picture_structure == PIPE_MPEG12_PICTURE_STRUCTURE_FRAME) {
    886             if (bs->desc->frame_pred_frame_dct == 0)
    887                mb.macroblock_modes.bits.frame_motion_type = vl_vlc_get_uimsbf(&bs->vlc, 2);
    888             else
    889                mb.macroblock_modes.bits.frame_motion_type = 2;
    890          } else
    891             mb.macroblock_modes.bits.field_motion_type = vl_vlc_get_uimsbf(&bs->vlc, 2);
    892 
    893       } else if ((mb.macroblock_type & PIPE_MPEG12_MB_TYPE_INTRA) && bs->desc->concealment_motion_vectors) {
    894          if (bs->desc->picture_structure == PIPE_MPEG12_PICTURE_STRUCTURE_FRAME)
    895             mb.macroblock_modes.bits.frame_motion_type = 2;
    896          else
    897             mb.macroblock_modes.bits.field_motion_type = 1;
    898       }
    899 
    900       if (bs->desc->picture_structure == PIPE_MPEG12_PICTURE_STRUCTURE_FRAME &&
    901           bs->desc->frame_pred_frame_dct == 0 &&
    902           mb.macroblock_type & (PIPE_MPEG12_MB_TYPE_INTRA | PIPE_MPEG12_MB_TYPE_PATTERN))
    903          mb.macroblock_modes.bits.dct_type = vl_vlc_get_uimsbf(&bs->vlc, 1);
    904 
    905       if (mb.macroblock_type & PIPE_MPEG12_MB_TYPE_QUANT)
    906          dct_scale = quant_scale[bs->desc->q_scale_type][vl_vlc_get_uimsbf(&bs->vlc, 5)];
    907 
    908       if (inc > 1 && bs->desc->picture_coding_type == PIPE_MPEG12_PICTURE_CODING_TYPE_P)
    909          memset(mb.PMV, 0, sizeof(mb.PMV));
    910 
    911       mb.motion_vertical_field_select = 0;
    912       if ((mb.macroblock_type & PIPE_MPEG12_MB_TYPE_MOTION_FORWARD) ||
    913           (mb.macroblock_type & PIPE_MPEG12_MB_TYPE_INTRA && bs->desc->concealment_motion_vectors)) {
    914          if (bs->desc->picture_structure == PIPE_MPEG12_PICTURE_STRUCTURE_FRAME)
    915             motion_vector_frame(bs, 0, &mb);
    916          else
    917             motion_vector_field(bs, 0, &mb);
    918       }
    919 
    920       if (mb.macroblock_type & PIPE_MPEG12_MB_TYPE_MOTION_BACKWARD) {
    921          if (bs->desc->picture_structure == PIPE_MPEG12_PICTURE_STRUCTURE_FRAME)
    922             motion_vector_frame(bs, 1, &mb);
    923          else
    924             motion_vector_field(bs, 1, &mb);
    925       }
    926 
    927       if (mb.macroblock_type & PIPE_MPEG12_MB_TYPE_INTRA && bs->desc->concealment_motion_vectors) {
    928          unsigned extra = vl_vlc_get_uimsbf(&bs->vlc, 1);
    929          mb.PMV[1][0][0] = mb.PMV[0][0][0];
    930          mb.PMV[1][0][1] = mb.PMV[0][0][1];
    931          assert(extra);
    932          (void) extra;
    933       } else if (mb.macroblock_type & PIPE_MPEG12_MB_TYPE_INTRA ||
    934                 !(mb.macroblock_type & (PIPE_MPEG12_MB_TYPE_MOTION_FORWARD |
    935                                         PIPE_MPEG12_MB_TYPE_MOTION_BACKWARD))) {
    936          memset(mb.PMV, 0, sizeof(mb.PMV));
    937       }
    938 
    939       if ((mb.macroblock_type & PIPE_MPEG12_MB_TYPE_MOTION_FORWARD &&
    940            mb.macroblock_modes.bits.frame_motion_type == 2) ||
    941           (mb.macroblock_modes.bits.frame_motion_type == 3)) {
    942             mb.PMV[1][0][0] = mb.PMV[0][0][0];
    943             mb.PMV[1][0][1] = mb.PMV[0][0][1];
    944       }
    945 
    946       if (mb.macroblock_type & PIPE_MPEG12_MB_TYPE_MOTION_BACKWARD &&
    947           mb.macroblock_modes.bits.frame_motion_type == 2) {
    948             mb.PMV[1][1][0] = mb.PMV[0][1][0];
    949             mb.PMV[1][1][1] = mb.PMV[0][1][1];
    950       }
    951 
    952       if (inc > 1 || !(mb.macroblock_type & PIPE_MPEG12_MB_TYPE_INTRA))
    953          reset_predictor(bs);
    954 
    955       if (mb.macroblock_type & (PIPE_MPEG12_MB_TYPE_INTRA | PIPE_MPEG12_MB_TYPE_PATTERN)) {
    956          memset(dct_blocks, 0, sizeof(dct_blocks));
    957          decode_dct(bs, &mb, dct_scale);
    958       } else
    959          mb.coded_block_pattern = 0;
    960 
    961       vl_vlc_fillbits(&bs->vlc);
    962    } while (vl_vlc_bits_left(&bs->vlc) && vl_vlc_peekbits(&bs->vlc, 23));
    963 
    964    mb.num_skipped_macroblocks = 0;
    965    bs->decoder->decode_macroblock(bs->decoder, target, &bs->desc->base, &mb.base, 1);
    966 }
    967 
    968 void
    969 vl_mpg12_bs_init(struct vl_mpg12_bs *bs, struct pipe_video_codec *decoder)
    970 {
    971    static bool tables_initialized = false;
    972 
    973    assert(bs);
    974 
    975    memset(bs, 0, sizeof(struct vl_mpg12_bs));
    976 
    977    bs->decoder = decoder;
    978 
    979    if (!tables_initialized) {
    980       init_tables();
    981       tables_initialized = true;
    982    }
    983 }
    984 
    985 void
    986 vl_mpg12_bs_decode(struct vl_mpg12_bs *bs,
    987                    struct pipe_video_buffer *target,
    988                    struct pipe_mpeg12_picture_desc *picture,
    989                    unsigned num_buffers,
    990                    const void * const *buffers,
    991                    const unsigned *sizes)
    992 {
    993    assert(bs);
    994 
    995    bs->desc = picture;
    996    bs->intra_dct_tbl = picture->intra_vlc_format ? tbl_B15 : tbl_B14_AC;
    997 
    998    vl_vlc_init(&bs->vlc, num_buffers, buffers, sizes);
    999    while (vl_vlc_search_byte(&bs->vlc, ~0, 0x00) && vl_vlc_bits_left(&bs->vlc) > 32) {
   1000       uint32_t code = vl_vlc_peekbits(&bs->vlc, 32);
   1001 
   1002       if (code >= 0x101 && code <= 0x1AF) {
   1003          vl_vlc_eatbits(&bs->vlc, 24);
   1004          decode_slice(bs, target);
   1005 
   1006          /* align to a byte again */
   1007          vl_vlc_eatbits(&bs->vlc, vl_vlc_valid_bits(&bs->vlc) & 7);
   1008 
   1009       } else {
   1010          vl_vlc_eatbits(&bs->vlc, 8);
   1011       }
   1012 
   1013       vl_vlc_fillbits(&bs->vlc);
   1014    }
   1015 }
   1016