vpar_blocks.c 76.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*****************************************************************************
 * vpar_blocks.c : blocks parsing
 * (c)1999 VideoLAN
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/uio.h>

#include "config.h"
#include "common.h"
#include "mtime.h"
#include "vlc_thread.h"

#include "intf_msg.h"
#include "debug.h"                    /* ?? temporaire, requis par netlist.h */

#include "input.h"
#include "input_netlist.h"
#include "decoder_fifo.h"
#include "video.h"
#include "video_output.h"

30
#include "vdec_idct.h"
31
#include "video_decoder.h"
32 33 34 35
#include "vdec_motion.h"

#include "vpar_blocks.h"
#include "vpar_headers.h"
36
#include "vpar_synchro.h"
37
#include "video_parser.h"
38
#include "video_fifo.h"
39

40
/*
41 42 43 44 45 46
 * Welcome to vpar_blocks.c ! Here's where the heavy processor-critical parsing
 * task is done. This file is divided in several parts :
 *  - Initialization of the lookup tables
 *  - Decoding of coded blocks
 *  - Decoding of motion vectors
 *  - Decoding of the other macroblock structures
47 48
 *  - Picture data parsing management (slices and error handling)
 * It's a pretty long file. Good luck and have a nice day.
49
 */
50 51 52 53 54 55


/*
 * Initialization tables
 */

56
    /* Table for coded_block_pattern resolution */
57
static lookup_t     pl_coded_pattern_init_table[512] = 
58
    { {MB_ERROR, 0}, {0, 9}, {39, 9}, {27, 9}, {59, 9}, {55, 9}, {47, 9}, {31, 9},
Jean-Marc Dressler's avatar
Jean-Marc Dressler committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
    {58, 8}, {58, 8}, {54, 8}, {54, 8}, {46, 8}, {46, 8}, {30, 8}, {30, 8},
    {57, 8}, {57, 8}, {53, 8}, {53, 8}, {45, 8}, {45, 8}, {29, 8}, {29, 8},
    {38, 8}, {38, 8}, {26, 8}, {26, 8}, {37, 8}, {37, 8}, {25, 8}, {25, 8},
    {43, 8}, {43, 8}, {23, 8}, {23, 8}, {51, 8}, {51, 8}, {15, 8}, {15, 8},
    {42, 8}, {42, 8}, {22, 8}, {22, 8}, {50, 8}, {50, 8}, {14, 8}, {14, 8},
    {41, 8}, {41, 8}, {21, 8}, {21, 8}, {49, 8}, {49, 8}, {13, 8}, {13, 8},
    {35, 8}, {35, 8}, {19, 8}, {19, 8}, {11, 8}, {11, 8}, {7, 8}, {7, 8},
    {34, 7}, {34, 7}, {34, 7}, {34, 7}, {18, 7}, {18, 7}, {18, 7}, {18, 7},
    {10, 7}, {10, 7}, {10, 7}, {10, 7}, {6, 7}, {6, 7}, {6, 7}, {6, 7}, 
    {33, 7}, {33, 7}, {33, 7}, {33, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, 
    {9, 7}, {9, 7}, {9, 7}, {9, 7}, {5, 7}, {5, 7}, {5, 7}, {5, 7}, 
    {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, 
    {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, 
    {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, 
    {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, 
    {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5},
    {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5},
    {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, 
    {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, 
    {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, 
    {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, 
    {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, 
    {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, 
    {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, 
    {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, 
    {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, 
    {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, 
    {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, 
    {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, 
    {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, 
    {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, 
    {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, 
    {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, 
    {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, 
    {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, 
    {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, 
    {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, 
    {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, 
    {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, 
    {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, 
    {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, 
    {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, 
    {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, 
    {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, 
    {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, 
    {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, 
    {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, 
    {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, 
    {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, 
    {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, 
    {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4},
    {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4},
    {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, 
    {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, 
    {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4},
    {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, 
    {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, 
    {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, 
    {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, 
    {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, 
    {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, 
    {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, 
    {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3} };
122

123 124 125 126 127
    /* Tables for dc DCT coefficients 
     * Tables are cut in two parts to reduce memory occupation
     */
    
    /* Table B-12, dct_dc_size_luminance, codes 00xxx ... 11110 */
128
static lookup_t     pl_dct_dc_lum_init_table_1[32] =
129 130 131 132 133 134 135
    { {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2},
      {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2},
      {0, 3}, {0, 3}, {0, 3}, {0, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3},
      {4, 3}, {4, 3}, {4, 3}, {4, 3}, {5, 4}, {5, 4}, {6, 5}, {MB_ERROR, 0}
    };

    /* Table B-12, dct_dc_size_luminance, codes 111110xxx ... 111111111 */
136
static lookup_t     pl_dct_dc_lum_init_table_2[32] =
137 138 139 140 141 142 143 144 145
    { {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6},
      {8, 7}, {8, 7}, {8, 7}, {8, 7}, {9, 8}, {9, 8}, {10,9}, {11,9},
      {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0},
      {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0},
      {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0},
      {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}
    };

    /* Table B-13, dct_dc_size_chrominance, codes 00xxx ... 11110 */
146
static lookup_t     pl_dct_dc_chrom_init_table_1[32] =
147 148 149 150 151 152 153
    { {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2},
      {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2},
      {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2},
      {3, 3}, {3, 3}, {3, 3}, {3, 3}, {4, 4}, {4, 4}, {5, 5}, {MB_ERROR, 0}
    };

    /* Table B-13, dct_dc_size_chrominance, codes 111110xxxx ... 1111111111 */
154
static lookup_t     pl_dct_dc_chrom_init_table_2[32] =
155 156 157 158 159
    { {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6},
      {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6},
      {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7},
      {8, 8}, {8, 8}, {8, 8}, {8, 8}, {9, 9}, {9, 9}, {10,10}, {11,10}
    };
160 161 162 163 164 165
    
    /* 
     * Structure to store the tables B14 & B15 
     * Is constructed from the tables below 
     */
    dct_lookup_t            ppl_dct_coef[2][16384];
166

167 168

    /* Tables for ac DCT coefficients. There are cut in many parts to save space */
169 170 171
    /* Table B-14, DCT coefficients table zero,
     * codes 0100 ... 1xxx (used for first (DC) coefficient)
     */
172
static dct_lookup_t pl_DCT_tab_dc[12] =
173 174 175 176 177 178 179 180 181
    {
        {0,2,4}, {2,1,4}, {1,1,3}, {1,1,3},
        {0,1,1}, {0,1,1}, {0,1,1}, {0,1,1},
        {0,1,1}, {0,1,1}, {0,1,1}, {0,1,1}
    };

    /* Table B-14, DCT coefficients table zero,
     * codes 0100 ... 1xxx (used for all other coefficients)
     */
182
static dct_lookup_t pl_DCT_tab_ac[12] =
183 184 185 186 187 188 189 190 191
    {
        {0,2,4},  {2,1,4},  {1,1,3},  {1,1,3},
        {DCT_EOB,0,2}, {DCT_EOB,0,2}, {DCT_EOB,0,2}, {DCT_EOB,0,2}, /* EOB */
        {0,1,2},  {0,1,2},  {0,1,2},  {0,1,2}
    };

    /* Table B-14, DCT coefficients table zero,
     * codes 000001xx ... 00111xxx
     */
192
static dct_lookup_t pl_DCT_tab0[60] =
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
    {
        {DCT_ESCAPE,0,6}, {DCT_ESCAPE,0,6}, {DCT_ESCAPE,0,6}, {DCT_ESCAPE,0,6},
        /* Escape */
        {2,2,7}, {2,2,7}, {9,1,7}, {9,1,7},
        {0,4,7}, {0,4,7}, {8,1,7}, {8,1,7},
        {7,1,6}, {7,1,6}, {7,1,6}, {7,1,6},
        {6,1,6}, {6,1,6}, {6,1,6}, {6,1,6},
        {1,2,6}, {1,2,6}, {1,2,6}, {1,2,6},
        {5,1,6}, {5,1,6}, {5,1,6}, {5,1,6},
        {13,1,8}, {0,6,8}, {12,1,8}, {11,1,8},
        {3,2,8}, {1,3,8}, {0,5,8}, {10,1,8},
        {0,3,5}, {0,3,5}, {0,3,5}, {0,3,5},
        {0,3,5}, {0,3,5}, {0,3,5}, {0,3,5},
        {4,1,5}, {4,1,5}, {4,1,5}, {4,1,5},
        {4,1,5}, {4,1,5}, {4,1,5}, {4,1,5},
        {3,1,5}, {3,1,5}, {3,1,5}, {3,1,5},
        {3,1,5}, {3,1,5}, {3,1,5}, {3,1,5}
    };

    /* Table B-15, DCT coefficients table one,
     * codes 000001xx ... 11111111
     */
215
static dct_lookup_t pl_DCT_tab0a[252] =
216
    {
217
        {65,0,6}, {65,0,6}, {65,0,6}, {65,0,6}, /* Escape */
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
        {7,1,7}, {7,1,7}, {8,1,7}, {8,1,7},
        {6,1,7}, {6,1,7}, {2,2,7}, {2,2,7},
        {0,7,6}, {0,7,6}, {0,7,6}, {0,7,6},
        {0,6,6}, {0,6,6}, {0,6,6}, {0,6,6},
        {4,1,6}, {4,1,6}, {4,1,6}, {4,1,6},
        {5,1,6}, {5,1,6}, {5,1,6}, {5,1,6},
        {1,5,8}, {11,1,8}, {0,11,8}, {0,10,8},
        {13,1,8}, {12,1,8}, {3,2,8}, {1,4,8},
        {2,1,5}, {2,1,5}, {2,1,5}, {2,1,5},
        {2,1,5}, {2,1,5}, {2,1,5}, {2,1,5},
        {1,2,5}, {1,2,5}, {1,2,5}, {1,2,5},
        {1,2,5}, {1,2,5}, {1,2,5}, {1,2,5},
        {3,1,5}, {3,1,5}, {3,1,5}, {3,1,5},
        {3,1,5}, {3,1,5}, {3,1,5}, {3,1,5},
        {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
        {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
        {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
        {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
        {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
        {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
        {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
        {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
240 241 242 243
        {64,0,4}, {64,0,4}, {64,0,4}, {64,0,4}, /* EOB */
        {64,0,4}, {64,0,4}, {64,0,4}, {64,0,4},
        {64,0,4}, {64,0,4}, {64,0,4}, {64,0,4},
        {64,0,4}, {64,0,4}, {64,0,4}, {64,0,4},
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
        {0,3,4}, {0,3,4}, {0,3,4}, {0,3,4},
        {0,3,4}, {0,3,4}, {0,3,4}, {0,3,4},
        {0,3,4}, {0,3,4}, {0,3,4}, {0,3,4},
        {0,3,4}, {0,3,4}, {0,3,4}, {0,3,4},
        {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
        {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
        {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
        {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
        {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
        {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
        {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
        {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
        {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
        {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
        {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
        {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
        {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
        {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
        {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
263
        {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
        {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
        {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
        {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
        {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
        {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
        {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
        {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
        {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
        {0,4,5}, {0,4,5}, {0,4,5}, {0,4,5},
        {0,4,5}, {0,4,5}, {0,4,5}, {0,4,5},
        {0,5,5}, {0,5,5}, {0,5,5}, {0,5,5},
        {0,5,5}, {0,5,5}, {0,5,5}, {0,5,5},
        {9,1,7}, {9,1,7}, {1,3,7}, {1,3,7},
        {10,1,7}, {10,1,7}, {0,8,7}, {0,8,7},
        {0,9,7}, {0,9,7}, {0,12,8}, {0,13,8},
        {2,3,8}, {4,2,8}, {0,14,8}, {0,15,8}
    };
281

282 283 284
    /* Table B-14, DCT coefficients table zero,
     * codes 0000001000 ... 0000001111
     */
285
static dct_lookup_t pl_DCT_tab1[8] =
286 287 288 289 290 291 292 293
    {
        {16,1,10}, {5,2,10}, {0,7,10}, {2,3,10},
        {1,4,10}, {15,1,10}, {14,1,10}, {4,2,10}
    };

    /* Table B-15, DCT coefficients table one,
     * codes 000000100x ... 000000111x
     */
294
static dct_lookup_t pl_DCT_tab1a[8] =
295 296 297 298
    {
        {5,2,9}, {5,2,9}, {14,1,9}, {14,1,9},
        {2,4,10}, {16,1,10}, {15,1,9}, {15,1,9}
    };
299

300 301 302
    /* Table B-14/15, DCT coefficients table zero / one,
     * codes 000000010000 ... 000000011111
     */
303
static dct_lookup_t pl_DCT_tab2[16] =
304 305 306 307 308 309 310 311 312 313
    {
        {0,11,12}, {8,2,12}, {4,3,12}, {0,10,12},
        {2,4,12}, {7,2,12}, {21,1,12}, {20,1,12},
        {0,9,12}, {19,1,12}, {18,1,12}, {1,5,12},
        {3,3,12}, {0,8,12}, {6,2,12}, {17,1,12}
    };

    /* Table B-14/15, DCT coefficients table zero / one,
     * codes 0000000010000 ... 0000000011111
     */
314
static dct_lookup_t pl_DCT_tab3[16] =
315 316 317 318 319 320 321 322 323 324
    {
        {10,2,13}, {9,2,13}, {5,3,13}, {3,4,13},
        {2,5,13}, {1,7,13}, {1,6,13}, {0,15,13},
        {0,14,13}, {0,13,13}, {0,12,13}, {26,1,13},
        {25,1,13}, {24,1,13}, {23,1,13}, {22,1,13}
    };

    /* Table B-14/15, DCT coefficients table zero / one,
     * codes 00000000010000 ... 00000000011111
     */
325
static dct_lookup_t pl_DCT_tab4[16] =
326 327 328 329 330 331 332 333 334 335
    {
        {0,31,14}, {0,30,14}, {0,29,14}, {0,28,14},
        {0,27,14}, {0,26,14}, {0,25,14}, {0,24,14},
        {0,23,14}, {0,22,14}, {0,21,14}, {0,20,14},
        {0,19,14}, {0,18,14}, {0,17,14}, {0,16,14}
    };

    /* Table B-14/15, DCT coefficients table zero / one,
     *   codes 000000000010000 ... 000000000011111
     */
336
static dct_lookup_t pl_DCT_tab5[16] =
337 338 339 340 341 342 343 344 345 346
    {
    {0,40,15}, {0,39,15}, {0,38,15}, {0,37,15},
    {0,36,15}, {0,35,15}, {0,34,15}, {0,33,15},
    {0,32,15}, {1,14,15}, {1,13,15}, {1,12,15},
    {1,11,15}, {1,10,15}, {1,9,15}, {1,8,15}
    };

    /* Table B-14/15, DCT coefficients table zero / one,
     * codes 0000000000010000 ... 0000000000011111
     */
347
static dct_lookup_t pl_DCT_tab6[16] =
348 349 350 351 352 353
    {
        {1,18,16}, {1,17,16}, {1,16,16}, {1,15,16},
        {6,3,16}, {16,2,16}, {15,2,16}, {14,2,16},
        {13,2,16}, {12,2,16}, {11,2,16}, {31,1,16},
        {30,1,16}, {29,1,16}, {28,1,16}, {27,1,16}
    };
354 355


356 357 358 359 360 361 362 363
/*
 * Initialization of lookup tables
 */

/*****************************************************************************
 * vpar_InitCrop : Initialize the crop table for saturation
 *                 (ISO/IEC 13818-2 section 7.4.3)
 *****************************************************************************/
364
#if defined(MPEG2_COMPLIANT) && !defined(VDEC_DFT)
365 366 367 368
void vpar_InitCrop( vpar_thread_t * p_vpar )
{
    int i_dummy;

369
    p_vpar->pi_crop = p_vpar->pi_crop_buf + 4096;
370

371
    for( i_dummy = -4096; i_dummy < -2048; i_dummy++ )
372 373 374 375 376 377 378
    {
        p_vpar->pi_crop[i_dummy] = -2048;
    }
    for( ; i_dummy < 2047; i_dummy++ )
    {
        p_vpar->pi_crop[i_dummy] = i_dummy;
    }
379
    for( ; i_dummy < 4095; i_dummy++ )
380 381 382 383 384
    {
        p_vpar->pi_crop[i_dummy] = 2047;
    }
}
#endif
385 386

/*****************************************************************************
387
 * vpar_InitMbAddrInc : Initialize the lookup table for mb_addr_inc
388 389
 *****************************************************************************/

390
/* Function for filling up the lookup table for mb_addr_inc */
391
static void __inline__ FillMbAddrIncTable( vpar_thread_t * p_vpar,
392 393 394
                                    int i_start, int i_end, int i_step, 
                                    int * pi_value, int i_length )
{
395 396
    int i_pos, i_offset;
    for( i_pos = i_start ; i_pos < i_end ; i_pos += i_step )
397
    {
398
        for( i_offset = 0 ; i_offset < i_step ; i_offset ++ )
399
        {
400 401
            p_vpar->pl_mb_addr_inc[i_pos + i_offset].i_value = * pi_value;
            p_vpar->pl_mb_addr_inc[i_pos + i_offset].i_length = i_length;
402
        }
403
        (*pi_value)--;
404
    }
405 406
}
    
407 408
/* Function that initialize the table using the last one */
void vpar_InitMbAddrInc( vpar_thread_t * p_vpar )
409
{
410
    int i_dummy;
411
    int i_value;
412
    
Jean-Marc Dressler's avatar
Jean-Marc Dressler committed
413
    for( i_dummy = 0 ; i_dummy < 8 ; i_dummy++ )
414 415 416 417 418 419 420 421
    {
        p_vpar->pl_mb_addr_inc[i_dummy].i_value = MB_ERROR;
        p_vpar->pl_mb_addr_inc[i_dummy].i_length = 0;
    }

    p_vpar->pl_mb_addr_inc[8].i_value = MB_ADDRINC_ESCAPE;
    p_vpar->pl_mb_addr_inc[8].i_length = 11;

Jean-Marc Dressler's avatar
Jean-Marc Dressler committed
422
    for( i_dummy = 9 ; i_dummy < 15 ; i_dummy ++ ) 
423 424 425 426 427 428 429 430
    {
        p_vpar->pl_mb_addr_inc[i_dummy].i_value =  MB_ERROR;
        p_vpar->pl_mb_addr_inc[i_dummy].i_length = 0;
    }

    p_vpar->pl_mb_addr_inc[15].i_value = MB_ADDRINC_STUFFING;
    p_vpar->pl_mb_addr_inc[15].i_length = 11;

Jean-Marc Dressler's avatar
Jean-Marc Dressler committed
431
    for( i_dummy = 16; i_dummy < 24; i_dummy++ ) 
432 433 434 435
    {
        p_vpar->pl_mb_addr_inc[i_dummy].i_value =  MB_ERROR;
        p_vpar->pl_mb_addr_inc[i_dummy].i_length = 0;
    }
436
    
437
    i_value = 33;
438
    
439
    FillMbAddrIncTable( p_vpar, 24, 36, 1, &i_value, 11 );
440 441 442 443 444 445 446
    FillMbAddrIncTable( p_vpar, 36, 48, 2, &i_value, 10 );
    FillMbAddrIncTable( p_vpar, 48, 96, 8, &i_value, 8 );
    FillMbAddrIncTable( p_vpar, 96, 128, 16, &i_value, 7 );
    FillMbAddrIncTable( p_vpar, 128, 256, 64, &i_value, 5 );
    FillMbAddrIncTable( p_vpar, 256, 512, 128, &i_value, 4 );
    FillMbAddrIncTable( p_vpar, 512, 1024, 256, &i_value, 3 );
    FillMbAddrIncTable( p_vpar, 1024, 2048, 1024, &i_value, 1 );
447 448 449
}

/*****************************************************************************
450
 * vpar_Init*MBType : Initialize lookup table for the Macroblock type
451
 *****************************************************************************/
452 453 454 455 456 457 458 459 460 461

/* Fonction for filling up the tables */
static void __inline__ FillMBType( vpar_thread_t * p_vpar,
                                   int           i_mb_type,
                                   int           i_start,
                                   int           i_end,
                                   int           i_value,
                                   int           i_length )
{
    int i_dummy;
Jean-Marc Dressler's avatar
Jean-Marc Dressler committed
462 463
    
    for( i_dummy = i_start ; i_dummy < i_end ; i_dummy++ )
464
    {
465 466
        p_vpar->ppl_mb_type[i_mb_type][i_dummy].i_value = i_value;
        p_vpar->ppl_mb_type[i_mb_type][i_dummy].i_length = i_length;
467 468 469 470
    }
}

/* Fonction that fills the table for P MB_Type */
471
void vpar_InitPMBType( vpar_thread_t * p_vpar )
472 473 474 475 476
{
    FillMBType( p_vpar, 0, 32, 64, MB_MOTION_FORWARD|MB_PATTERN, 1 );
    FillMBType( p_vpar, 0, 16, 32, MB_PATTERN, 2 );
    FillMBType( p_vpar, 0, 8, 16, MB_MOTION_FORWARD, 3 );
    FillMBType( p_vpar, 0, 6, 8, MB_INTRA, 5 );
Jean-Marc Dressler's avatar
Jean-Marc Dressler committed
477
    FillMBType( p_vpar, 0, 4, 6, MB_QUANT|MB_MOTION_FORWARD|MB_PATTERN, 5 );
478
    FillMBType( p_vpar, 0, 2, 4, MB_QUANT|MB_PATTERN, 5 );
479 480 481 482
    p_vpar->ppl_mb_type[0][1].i_value = MB_QUANT|MB_INTRA;
    p_vpar->ppl_mb_type[0][1].i_length = 6;
    p_vpar->ppl_mb_type[0][0].i_value = MB_ERROR;
    p_vpar->ppl_mb_type[0][0].i_length = 0;
483 484 485
}

/* Fonction that fills the table for B MB_Type */
486
void vpar_InitBMBType( vpar_thread_t * p_vpar )
487 488 489 490 491 492 493 494 495 496 497
{
    FillMBType( p_vpar, 1, 48, 64, MB_MOTION_FORWARD
                                  |MB_MOTION_BACKWARD|MB_PATTERN, 2 );
    FillMBType( p_vpar, 1, 32, 48, MB_MOTION_FORWARD|MB_MOTION_BACKWARD, 2 );
    FillMBType( p_vpar, 1, 24, 32, MB_MOTION_BACKWARD|MB_PATTERN, 3 );
    FillMBType( p_vpar, 1, 16, 24, MB_MOTION_BACKWARD, 3 );
    FillMBType( p_vpar, 1, 12, 16, MB_MOTION_FORWARD|MB_PATTERN, 4 );
    FillMBType( p_vpar, 1, 8, 12, MB_MOTION_FORWARD, 4 );
    FillMBType( p_vpar, 1, 6, 8, MB_INTRA, 5 );
    FillMBType( p_vpar, 1, 4, 6, MB_QUANT|MB_MOTION_FORWARD
                                |MB_MOTION_BACKWARD|MB_PATTERN, 5 );
498 499 500 501 502 503 504 505
    p_vpar->ppl_mb_type[1][3].i_value = MB_QUANT|MB_MOTION_FORWARD|MB_PATTERN;
    p_vpar->ppl_mb_type[1][3].i_length = 6;
    p_vpar->ppl_mb_type[1][2].i_value = MB_QUANT|MB_MOTION_BACKWARD|MB_PATTERN;
    p_vpar->ppl_mb_type[1][2].i_length = 6;
    p_vpar->ppl_mb_type[1][1].i_value = MB_QUANT|MB_INTRA;
    p_vpar->ppl_mb_type[1][1].i_length = 6;
    p_vpar->ppl_mb_type[1][0].i_value =MB_ERROR;
    p_vpar->ppl_mb_type[1][0].i_length = 0;
506 507 508
}

/*****************************************************************************
509 510
 * vpar_InitCodedPattern : Initialize the lookup table for decoding
 *                         coded block pattern
511
 *****************************************************************************/
512
void vpar_InitCodedPattern( vpar_thread_t * p_vpar )
513
{
514
    p_vpar->pl_coded_pattern = (lookup_t*) pl_coded_pattern_init_table;
515 516
}

517
/*****************************************************************************
518 519
 * vpar_InitDCTTables : Initialize tables giving the length of the dct
 *                      coefficient from the vlc code
520 521
 *****************************************************************************/

522
/* First fonction for filling the table */
523 524
static void __inline__ FillDCTTable( dct_lookup_t * p_tab_dest, dct_lookup_t * p_tab_src,
                                     int i_step, int i_nb_elem, int i_offset )
525 526
{
    int i_dummy, i_dummy2;
527 528

    for( i_dummy=0 ; i_dummy < i_nb_elem ; i_dummy++ )
529
    {
530
        for( i_dummy2=0 ; i_dummy2 < i_step ; i_dummy2++ )
531
        {
532
            p_tab_dest[(i_dummy+i_offset)*i_step+i_dummy2] = p_tab_src[i_dummy];
533 534 535 536 537 538
        }
    }
}


/* Fonction that actually fills the table or create the pointers */
539
void vpar_InitDCTTables( vpar_thread_t * p_vpar )
540
{
541
    /* Tables are cut in two parts to reduce memory occupation */    
542 543 544 545
    p_vpar->pppl_dct_dc_size[0][0] = pl_dct_dc_lum_init_table_1;
    p_vpar->pppl_dct_dc_size[0][1] = pl_dct_dc_lum_init_table_2;
    p_vpar->pppl_dct_dc_size[1][0] = pl_dct_dc_chrom_init_table_1;
    p_vpar->pppl_dct_dc_size[1][1] = pl_dct_dc_chrom_init_table_2;
546 547 548

    memset( ppl_dct_coef[0], MB_ERROR, 16 );
    memset( ppl_dct_coef[1], MB_ERROR, 16 );
549
    
550
    /* For table B14 & B15, we have a pointer to tables */
551
    /* We fill the table thanks to the fonction defined above */
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
    FillDCTTable( ppl_dct_coef[0], pl_DCT_tab0, 256, 60,  4 );
    FillDCTTable( ppl_dct_coef[0], pl_DCT_tab1,  64,  8,  8 );
    FillDCTTable( ppl_dct_coef[0], pl_DCT_tab2,  16, 16, 16 );
    FillDCTTable( ppl_dct_coef[0], pl_DCT_tab3,   8, 16, 16 );
    FillDCTTable( ppl_dct_coef[0], pl_DCT_tab4,   4, 16, 16 );
    FillDCTTable( ppl_dct_coef[0], pl_DCT_tab5,   2, 16, 16 );
    FillDCTTable( ppl_dct_coef[0], pl_DCT_tab6,   1, 16, 16 );

    FillDCTTable( ppl_dct_coef[1], pl_DCT_tab0a, 256, 60, 4 );
    FillDCTTable( ppl_dct_coef[1], pl_DCT_tab1a,  64,  8,  8 );
    FillDCTTable( ppl_dct_coef[1], pl_DCT_tab2,   16, 16, 16 );
    FillDCTTable( ppl_dct_coef[1], pl_DCT_tab3,    8, 16, 16 );
    FillDCTTable( ppl_dct_coef[1], pl_DCT_tab4,    4, 16, 16 );
    FillDCTTable( ppl_dct_coef[1], pl_DCT_tab5,    2, 16, 16 );
    FillDCTTable( ppl_dct_coef[1], pl_DCT_tab6,    1, 16, 16 );
567 568
}

569

570
/*
571
 * Block parsing
572
 */
573

574
/*****************************************************************************
575
 * DecodeMPEG1NonIntra : decode MPEG-1 non-intra blocks
576
 *****************************************************************************/
577 578 579
static __inline__ void DecodeMPEG1NonIntra( vpar_thread_t * p_vpar,
                                            macroblock_t * p_mb, int i_b,
                                            int i_chroma_format )
580 581
{

582
    if( p_vpar->picture.i_coding_type == D_CODING_TYPE )
583
    {
584 585 586
        /* Remove end_of_macroblock (always 1, prevents startcode emulation)
         * ISO/IEC 11172-2 section 2.4.2.7 and 2.4.3.6 */
        RemoveBits( &p_vpar->bit_stream, 1 );
587
    }
588 589 590
}

/*****************************************************************************
591
 * DecodeMPEG1Intra : decode MPEG-1 intra blocks
592
 *****************************************************************************/
593 594 595
static __inline__ void DecodeMPEG1Intra( vpar_thread_t * p_vpar,
                                         macroblock_t * p_mb, int i_b ,
                                         int i_chroma_format )
596
{
597 598

    if( p_vpar->picture.i_coding_type == D_CODING_TYPE )
599
    {
600 601 602
        /* Remove end_of_macroblock (always 1, prevents startcode emulation)
         * ISO/IEC 11172-2 section 2.4.2.7 and 2.4.3.6 */
        RemoveBits( &p_vpar->bit_stream, 1 );
603 604 605 606
    }
}

/*****************************************************************************
607
 * DecodeMPEG2NonIntra : decode MPEG-2 non-intra blocks
608
 *****************************************************************************/
609 610 611
static __inline__ void DecodeMPEG2NonIntra( vpar_thread_t * p_vpar,
                                            macroblock_t * p_mb, int i_b,
                                            int i_chroma_format )
612
{
613 614 615 616 617 618 619 620 621 622 623
    int         i_parse;
    int         i_nc;
    int         i_cc;
    int         i_coef;
    int         i_type;
    int         i_code;
    int         i_length;
    int         i_pos;
    int         i_run;
    int         i_level;
    boolean_t   b_sign;
624
    int *       pi_quant;
625 626 627
    
    /* Lookup Table for the chromatic component */
    static int pi_cc_index[12] = { 0, 0, 0, 0, 1, 2, 1, 2, 1, 2 };
628

629 630 631 632
    i_cc = pi_cc_index[i_b];

    /* Determine whether it is luminance or not (chrominance) */
    i_type = ( i_cc + 1 ) >> 1;
633

634
    /* Give a pointer to the quantization matrices for intra blocks */
635 636 637 638 639 640 641 642
    if( (i_chroma_format == CHROMA_420) || (!i_type) )
    {
        pi_quant = p_vpar->sequence.nonintra_quant.pi_matrix;
    }
    else
    {
        pi_quant = p_vpar->sequence.chroma_nonintra_quant.pi_matrix;
    }
643 644

    /* Decoding of the AC coefficients */
645
    
646 647 648
    i_nc = 0;
    i_coef = 0;
    for( i_parse = 0; ; i_parse++ )
649
    {
650 651
        i_code = ShowBits( &p_vpar->bit_stream, 16 );
        if( i_code >= 16384 )
652
        {
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
            if( i_parse == 0 )
            {
                i_run =     pl_DCT_tab_dc[(i_code>>12)-4].i_run;
                i_level =   pl_DCT_tab_dc[(i_code>>12)-4].i_level;
                i_length =  pl_DCT_tab_dc[(i_code>>12)-4].i_length;
            }
            else
            {
                i_run =     pl_DCT_tab_ac[(i_code>>12)-4].i_run;
                i_level =   pl_DCT_tab_ac[(i_code>>12)-4].i_level;
                i_length =  pl_DCT_tab_ac[(i_code>>12)-4].i_length;
             }
        }
        else if( i_code >= 1024 )
        {
            i_run =     pl_DCT_tab0[(i_code>>8)-4].i_run;
            i_length =  pl_DCT_tab0[(i_code>>8)-4].i_length;
            i_level =   pl_DCT_tab0[(i_code>>8)-4].i_level;
671 672 673
        }
        else
        {
674 675 676
            i_run =     ppl_dct_coef[0][i_code].i_run;
            i_length =  ppl_dct_coef[0][i_code].i_length;
            i_level =   ppl_dct_coef[0][i_code].i_level;
677 678
        }

679 680
        
        RemoveBits( &p_vpar->bit_stream, i_length );
Gaël Hendryckx's avatar
CA...  
Gaël Hendryckx committed
681

682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
        switch( i_run )
        {
            case DCT_ESCAPE:
                i_run = GetBits( &p_vpar->bit_stream, 6 );
                i_level = GetBits( &p_vpar->bit_stream, 12 );
                i_level = (b_sign = ( i_level > 2047 )) ? 4096 - i_level
                                                        : i_level;
                break;
            case DCT_EOB:
                if( i_nc <= 1 )
                {
                    p_mb->pf_idct[i_b] = vdec_SparseIDCT;
                    p_mb->pi_sparse_pos[i_b] = i_coef;
                }
                else
                {
                    p_mb->pf_idct[i_b] = vdec_IDCT;
                }
                return;
701

702 703 704 705 706 707 708 709 710
                break;
            default:
                b_sign = GetBits( &p_vpar->bit_stream, 1 );
        }
        i_coef = i_parse;
        i_parse += i_run;
        i_nc ++;

        if( i_parse >= 64 )
711
        {
712
            break;
713
        }
714 715
        
        i_pos = pi_scan[p_vpar->picture.b_alternate_scan][i_parse];
716
        i_level = ( ((i_level << 1) + 1) * p_vpar->mb.i_quantizer_scale 
717
                    * pi_quant[i_pos] ) >> 5;
718
        p_mb->ppi_blocks[i_b][i_pos] = b_sign ? -i_level : i_level;
719
    }
720 721 722

    intf_ErrMsg("vpar error: DCT coeff (non-intra) is out of bounds\n");
    p_vpar->picture.b_error = 1;
723
}
724

725
/*****************************************************************************
726
 * DecodeMPEG2Intra : decode MPEG-2 intra blocks
727
 *****************************************************************************/
728 729 730
static __inline__ void DecodeMPEG2Intra( vpar_thread_t * p_vpar,
                                         macroblock_t * p_mb, int i_b,
                                         int i_chroma_format )
731
{
732 733 734 735
    int         i_parse;
    int         i_nc;
    int         i_cc;
    int         i_coef;
736
    int         i_type;
737 738 739 740 741 742 743 744 745
    int         i_code;
    int         i_length;
    int         i_pos;
    int         i_dct_dc_size;
    int         i_dct_dc_diff;
    int         i_run;
    int         i_level;
    boolean_t   b_vlc_intra;
    boolean_t   b_sign;
746
    int *       pi_quant;
747 748 749 750
    
    /* Lookup Table for the chromatic component */
    static int pi_cc_index[12] = { 0, 0, 0, 0, 1, 2, 1, 2, 1, 2 };
    i_cc = pi_cc_index[i_b];
751

752 753
    /* Determine whether it is luminance or not (chrominance) */
    i_type = ( i_cc + 1 ) >> 1;
Gaël Hendryckx's avatar
CA...  
Gaël Hendryckx committed
754

755
    /* Give a pointer to the quantization matrices for intra blocks */
756 757 758 759 760 761 762 763
    if( (i_chroma_format == CHROMA_420) || (!i_type) )
    {
        pi_quant = p_vpar->sequence.intra_quant.pi_matrix;
    }
    else
    {
        pi_quant = p_vpar->sequence.chroma_intra_quant.pi_matrix;
    }
764

765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
#if 0    
    /* Decoding of the DC intra coefficient */
    /* The nb of bits to parse depends on i_type */
    i_code = ShowBits( &p_vpar->bit_stream, 9 + i_type );
    
    /* To reduce memory occupation, there are two lookup tables
     * See InitDCT above */
    i_code5 = i_code >> (4+i_type);
    
    /* Shall we lookup in the first or in the second table ? */
    i_select = ( i_code5 == 31 );
    /* Offset value for looking in the second table */
    i_offset = 0x1f0 + ( i_type * 0x1f0 );
    i_pos = ( i_code5 * ( ! i_select ) ) + ( ( i_code - i_offset ) * i_select );
    i_dct_dc_size = p_vpar->pppl_dct_dc_size[i_type][i_select][i_pos].i_value;
#endif
    
    if( !i_type/*i_b < 4*/ )
    {
        /* decode length */
        i_code = ShowBits(&p_vpar->bit_stream, 5);
        if (i_code<31)
        {
            i_dct_dc_size = pl_dct_dc_lum_init_table_1[i_code].i_value;
            i_length = pl_dct_dc_lum_init_table_1[i_code].i_length;
            RemoveBits( &p_vpar->bit_stream, i_length);
        }
        else
        {
            i_code = ShowBits(&p_vpar->bit_stream, 9) - 0x1f0;
            i_dct_dc_size = pl_dct_dc_lum_init_table_2[i_code].i_value;
            i_length = pl_dct_dc_lum_init_table_2[i_code].i_length;
            RemoveBits( &p_vpar->bit_stream, i_length);
798
        }
799 800 801 802 803
    }
    else
    {
        /* decode length */
        i_code = ShowBits(&p_vpar->bit_stream, 5);
804

805
        if (i_code<31)
806
        {
807 808 809
            i_dct_dc_size = pl_dct_dc_chrom_init_table_1[i_code].i_value;
            i_length = pl_dct_dc_chrom_init_table_1[i_code].i_length;
            RemoveBits(&p_vpar->bit_stream, i_length);
810
        }
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
        else
        {
            i_code = ShowBits(&p_vpar->bit_stream, 10) - 0x3e0;
            i_dct_dc_size = pl_dct_dc_chrom_init_table_2[i_code].i_value;
            i_length = pl_dct_dc_chrom_init_table_2[i_code].i_length;
            RemoveBits( &p_vpar->bit_stream, i_length);
        }
    }
    if (i_dct_dc_size==0)
        i_dct_dc_diff = 0;
    else
    {
        i_dct_dc_diff = GetBits( &p_vpar->bit_stream, i_dct_dc_size);
        if ((i_dct_dc_diff & (1<<(i_dct_dc_size-1)))==0)
            i_dct_dc_diff-= (1<<i_dct_dc_size) - 1;
    }
827

828 829 830 831 832
    /* Dump the variable length code */
    //RemoveBits( &p_vpar->bit_stream, 
    //          p_vpar->pppl_dct_dc_size[i_type][i_select][i_pos].i_length );
    
    /* Read the actual code with the good length */
833
    p_vpar->mb.pi_dc_dct_pred[i_cc] += i_dct_dc_diff;
834

835
    p_mb->ppi_blocks[i_b][0] = ( p_vpar->mb.pi_dc_dct_pred[i_cc] <<
836
                               ( 3 - p_vpar->picture.i_intra_dc_precision ) );
837
    i_nc = ( p_vpar->mb.pi_dc_dct_pred[i_cc] != 0 );
838

839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
    /* Decoding of the AC coefficients */
    
    i_coef = 0;
    b_vlc_intra = p_vpar->picture.b_intra_vlc_format;
    for( i_parse = 1; /*i_parse < 64*/; i_parse++ )
    {
        i_code = ShowBits( &p_vpar->bit_stream, 16 );
        if( i_code >= 16384 )
        {
            if( b_vlc_intra )
            {
                i_run =     pl_DCT_tab0a[(i_code>>8)-4].i_run;
                i_level =   pl_DCT_tab0a[(i_code>>8)-4].i_level;
                i_length =  pl_DCT_tab0a[(i_code>>8)-4].i_length;
//fprintf( stderr, "**********> %d, %d, %d *******\n", i_run, i_level, (i_code>>8)-4 );
            }
            else
            {
                i_run =     pl_DCT_tab_ac[(i_code>>12)-4].i_run;
                i_level =   pl_DCT_tab_ac[(i_code>>12)-4].i_level;
                i_length =  pl_DCT_tab_ac[(i_code>>12)-4].i_length;
             }
        }
        else
        {
            i_run =     ppl_dct_coef[b_vlc_intra][i_code].i_run;
            i_length =  ppl_dct_coef[b_vlc_intra][i_code].i_length;
            i_level =   ppl_dct_coef[b_vlc_intra][i_code].i_level;
        }

#if 0
        {
            int code = i_code;
            int intra_vlc_format = b_vlc_intra;
            dct_lookup_t tab;
            
    if (code>=16384 && !intra_vlc_format)
      tab = pl_DCT_tab_ac[(code>>12)-4];
    else if (code>=1024)
    {
      if (intra_vlc_format)
        tab = pl_DCT_tab0a[(code>>8)-4];
      else
        tab = pl_DCT_tab0[(code>>8)-4];
    }
    else if (code>=512)
    {
      if (intra_vlc_format)
        tab = pl_DCT_tab1a[(code>>6)-8];
      else
        tab = pl_DCT_tab1[(code>>6)-8];
    }
    else if (code>=256)
      tab = pl_DCT_tab2[(code>>4)-16];
    else if (code>=128)
      tab = pl_DCT_tab3[(code>>3)-16];
    else if (code>=64)
      tab = pl_DCT_tab4[(code>>2)-16];
    else if (code>=32)
      tab = pl_DCT_tab5[(code>>1)-16];
    else if (code>=16)
      tab = pl_DCT_tab6[code-16];
    else
    {
       fprintf( stderr, "invalid Huffman code in Decode_MPEG2_Intra_Block()\n");
904
    }
905

906
    if( (i_run != tab.i_run) || (i_length != tab.i_length) || (i_level != tab.i_level) )
907
    {
908 909
        fprintf( stderr, "ET M....... !!!\n" );
        exit(0);
910
    }
911
        }
912
#endif
913 914 915



916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957
        
        RemoveBits( &p_vpar->bit_stream, i_length );

        switch( i_run )
        {
            case DCT_ESCAPE:
                i_run = GetBits( &p_vpar->bit_stream, 6 );
                i_level = GetBits( &p_vpar->bit_stream, 12 );
                /*p_mb->ppi_blocks[i_b][i_parse] = ( b_sign = ( i_level > 2047 ) ) 
                                                          ? ( -4096 + i_level )
                                                          : i_level;*/
                i_level = (b_sign = ( i_level > 2047 )) ? 4096 - i_level
                                                        : i_level;
                break;
            case DCT_EOB:
                if( i_nc <= 1 )
                {
                    p_mb->pf_idct[i_b] = vdec_SparseIDCT;
                    p_mb->pi_sparse_pos[i_b] = i_coef;
                }
                else
                {
                    p_mb->pf_idct[i_b] = vdec_IDCT;
                }
                return;

                break;
            default:
                b_sign = GetBits( &p_vpar->bit_stream, 1 );
        }

        i_coef = i_parse;
        i_parse += i_run;
        i_nc ++;
 
        if( i_parse >= 64 )
        {
            break;
        }

        i_pos = pi_scan[p_vpar->picture.b_alternate_scan][i_parse];
        i_level = ( i_level *
958
                    p_vpar->mb.i_quantizer_scale *
959
                    pi_quant[i_pos] ) >> 4;
960
        p_mb->ppi_blocks[i_b][i_pos] = b_sign ? -i_level : i_level;
961 962
    }

963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993
    intf_ErrMsg("vpar error: DCT coeff (intra) is out of bounds\n");
    p_vpar->picture.b_error = 1;
}


/*
 * Motion vectors
 */

/****************************************************************************
 * MotionCode : Parse the next motion code
 ****************************************************************************/
static __inline__ int MotionCode( vpar_thread_t * p_vpar )
{
    int i_code;
    static lookup_t pl_mv_tab0[8] = 
        { {-1,0}, {3,3}, {2,2}, {2,2}, {1,1}, {1,1}, {1,1}, {1,1} };
    /* Table B-10, motion_code, codes 0000011 ... 000011x */
    static lookup_t pl_mv_tab1[8] =
        { {-1,0}, {-1,0}, {-1,0}, {7,6}, {6,6}, {5,6}, {4,5}, {4,5} };
    /* Table B-10, motion_code, codes 0000001100 ... 000001011x */
    static lookup_t pl_mv_tab2[12] = {
        {16,9}, {15,9}, {14,9}, {13,9},
        {12,9}, {11,9}, {10,8}, {10,8},
        {9,8},  {9,8},  {8,8},  {8,8} };
    
    if( GetBits( &p_vpar->bit_stream, 1 ) )
    {
        return 0;
    }
    if( (i_code = ShowBits( &p_vpar->bit_stream, 9) ) >= 64 )
994
    {
995 996 997 998
        i_code >>= 6;
        RemoveBits( &p_vpar->bit_stream, pl_mv_tab0[i_code].i_length );
        return( GetBits( &p_vpar->bit_stream, 1 ) ?
            -pl_mv_tab0[i_code].i_value : pl_mv_tab0[i_code].i_value );
999 1000
    }

1001
    if( i_code >= 24 )
1002
    {
1003 1004 1005 1006
        i_code >>= 3;
        RemoveBits( &p_vpar->bit_stream, pl_mv_tab1[i_code].i_length );
        return( GetBits( &p_vpar->bit_stream, 1 ) ?
            -pl_mv_tab1[i_code].i_value : pl_mv_tab1[i_code].i_value );
1007 1008
    }

1009
    if( (i_code -= 12) < 0 )
1010
    {
1011 1012 1013
        p_vpar->picture.b_error = 1;
        intf_DbgMsg( "vpar debug: Invalid motion_vector code\n" );
        return 0;
1014
    }
1015 1016 1017 1018

    RemoveBits( &p_vpar->bit_stream, pl_mv_tab2[i_code].i_length );
    return( GetBits( &p_vpar->bit_stream, 1 ) ?
        -pl_mv_tab2[i_code].i_value : pl_mv_tab2[i_code].i_value );            
1019
}
1020

1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
/****************************************************************************
 * DecodeMotionVector : Decode a motion_vector
 ****************************************************************************/
static __inline__ void DecodeMotionVector( int * pi_prediction, int i_r_size,
        int i_motion_code, int i_motion_residual, int i_full_pel )
{
    int i_limit, i_vector;

    /* ISO/IEC 13818-1 section 7.6.3.1 */
    i_limit = 16 << i_r_size;
    i_vector = *pi_prediction >> i_full_pel;

    if( i_motion_code > 0 )
1034
    {
1035 1036 1037
        i_vector += ((i_motion_code-1) << i_r_size) + i_motion_residual + 1;
        if( i_vector >= i_limit )
            i_vector -= i_limit + i_limit;
1038
    }
1039
    else if( i_motion_code < 0 )
1040
    {
1041 1042 1043
        i_vector -= ((-i_motion_code-1) << i_r_size) + i_motion_residual + 1;
        if( i_vector < -i_limit )
            i_vector += i_limit + i_limit;
1044
    }
1045 1046
    *pi_prediction = i_vector << i_full_pel;
}
1047

1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
/****************************************************************************
 * MotionVector : Parse the next motion_vector field
 ****************************************************************************/
static __inline__ void MotionVector( vpar_thread_t * p_vpar,
                                     macroblock_t * p_mb, int i_r,
                                     int i_s, int i_full_pel, int i_structure )
{
    int i_motion_code, i_motion_residual;
    int i_r_size;
    int pi_dm_vector[2];
    
    i_r_size = p_vpar->picture.ppi_f_code[i_s][0] - 1;
    i_motion_code = MotionCode( p_vpar );
    i_motion_residual = (i_r_size != 0 && i_motion_code != 0) ?
                        GetBits( &p_vpar->bit_stream, i_r_size) : 0;
1063
    DecodeMotionVector( &p_vpar->mb.pppi_pmv[i_r][i_s][0], i_r_size,
1064
                        i_motion_code, i_motion_residual, i_full_pel );
1065
    p_mb->pppi_motion_vectors[i_r][i_s][0] = p_vpar->mb.pppi_pmv[i_r][i_s][0];
1066 1067

    if( p_vpar->mb.b_dmv )
1068
    {
1069
        if( GetBits(&p_vpar->bit_stream, 1) )
1070
        {
1071 1072 1073 1074 1075
            pi_dm_vector[0] = GetBits( &p_vpar->bit_stream, 1 ) ? -1 : 1;
        }
        else
        {
            pi_dm_vector[0] = 0;
1076 1077
        }
    }
1078 1079 1080 1081 1082
    
    i_r_size = p_vpar->picture.ppi_f_code[i_s][1]-1;
    i_motion_code = MotionCode( p_vpar );
    i_motion_residual = (i_r_size != 0 && i_motion_code != 0) ?
                        GetBits( &p_vpar->bit_stream, i_r_size) : 0;
1083

1084 1085 1086 1087
   
    if( (p_vpar->mb.i_mv_format == MOTION_FIELD)
        && (i_structure == FRAME_STRUCTURE) )
    {
1088
         p_vpar->mb.pppi_pmv[i_r][i_s][1] >>= 1;
1089
    }
1090
    
1091
    DecodeMotionVector( &p_vpar->mb.pppi_pmv[i_r][i_s][1], i_r_size,
1092 1093 1094 1095
                        i_motion_code, i_motion_residual, i_full_pel );

    if( (p_vpar->mb.i_mv_format == MOTION_FIELD)
        && (i_structure == FRAME_STRUCTURE) )
1096
         p_vpar->mb.pppi_pmv[i_r][i_s][1] <<= 1;
1097
     
1098
    p_mb->pppi_motion_vectors[i_r][i_s][1] = p_vpar->mb.pppi_pmv[i_r][i_s][1];
1099 1100

    if( p_vpar->mb.b_dmv )
1101
    {
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
        if( GetBits(&p_vpar->bit_stream, 1) )
        {
            pi_dm_vector[1] = GetBits( &p_vpar->bit_stream, 1 ) ? -1 : 1;
        }
        else
        {
            pi_dm_vector[1] = 0;
        }

        /* Dual Prime Arithmetic (ISO/IEC 13818-2 section 7.6.3.6). */
1112

1113 1114
#define i_mv_x  p_mb->pppi_motion_vectors[0][0][0]
        if( i_structure == FRAME_STRUCTURE )
1115
        {
1116 1117 1118 1119 1120 1121
#define i_mv_y  (p_mb->pppi_motion_vectors[0][0][1] << 1)
            if( p_vpar->picture.b_top_field_first )
            {
                /* vector for prediction of top field from bottom field */
                p_mb->ppi_dmv[0][0] = ((i_mv_x + (i_mv_x > 0)) >> 1) + pi_dm_vector[0];
                p_mb->ppi_dmv[0][1] = ((i_mv_y + (i_mv_y > 0)) >> 1) + pi_dm_vector[1] - 1;
1122

1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
                /* vector for prediction of bottom field from top field */
                p_mb->ppi_dmv[1][0] = ((3*i_mv_x + (i_mv_x > 0)) >> 1) + pi_dm_vector[0];
                p_mb->ppi_dmv[1][1] = ((3*i_mv_y + (i_mv_y > 0)) >> 1) + pi_dm_vector[1] + 1;
            }
            else
            {
                /* vector for prediction of top field from bottom field */
                p_mb->ppi_dmv[0][0] = ((3*i_mv_x + (i_mv_x > 0)) >> 1) + pi_dm_vector[0];
                p_mb->ppi_dmv[0][1] = ((3*i_mv_y + (i_mv_y > 0)) >> 1) + pi_dm_vector[1] - 1;

                /* vector for prediction of bottom field from top field */
                p_mb->ppi_dmv[1][0] = ((i_mv_x + (i_mv_x > 0)) >> 1) + pi_dm_vector[0];
                p_mb->ppi_dmv[1][1] = ((i_mv_y + (i_mv_y > 0)) >> 1) + pi_dm_vector[1] + 1;
            }
#undef i_mv_y
        }
        else
        {
#define i_mv_y  p_mb->pppi_motion_vectors[0][0][1]
            /* vector for prediction from field of opposite 'parity' */
            p_mb->ppi_dmv[0][0] = ((i_mv_x + (i_mv_x > 0)) >> 1) + pi_dm_vector[0];
            p_mb->ppi_dmv[0][1] = ((i_mv_y + (i_mv_y > 0)) >> 1) + pi_dm_vector[1];

            /* correct for vertical field shift */
            if( p_vpar->picture.i_structure == TOP_FIELD )
                p_mb->ppi_dmv[0][1]--;
            else
                p_mb->ppi_dmv[0][1]++;
#undef i_mv_y
1152
        }
1153
#undef i_mv_x
1154
    }
1155
}
1156

1157 1158 1159 1160 1161 1162 1163 1164 1165
/*****************************************************************************
 * DecodeMVMPEG1 : Parse the next MPEG-1 motion vectors
 *****************************************************************************/
static __inline__ void DecodeMVMPEG1( vpar_thread_t * p_vpar,
                            macroblock_t * p_mb, int i_s, int i_structure )
{
    MotionVector( p_vpar, p_mb, 0, i_s,
                  p_vpar->picture.pb_full_pel_vector[i_s], i_structure );
}
1166

1167 1168 1169 1170 1171 1172 1173
/*****************************************************************************
 * DecodeMVMPEG2 : Parse the next MPEG-2 motion_vectors field
 *****************************************************************************/
static __inline__ void DecodeMVMPEG2( vpar_thread_t * p_vpar,
                            macroblock_t * p_mb, int i_s, int i_structure )
{
    if( p_vpar->mb.i_mv_count == 1 )
1174
    {
1175 1176 1177 1178 1179 1180
        if( p_vpar->mb.i_mv_format == MOTION_FIELD && !p_vpar->mb.b_dmv )
        {
            p_mb->ppi_field_select[0][i_s] = p_mb->ppi_field_select[1][i_s]
                                            = GetBits( &p_vpar->bit_stream, 1 );
        }
        MotionVector( p_vpar, p_mb, 0, i_s, 0, i_structure );
1181 1182 1183 1184
        p_vpar->mb.pppi_pmv[1][i_s][0] = p_vpar->mb.pppi_pmv[0][i_s][0];
        p_vpar->mb.pppi_pmv[1][i_s][1] = p_vpar->mb.pppi_pmv[0][i_s][1];
        p_mb->pppi_motion_vectors[1][i_s][0] = p_vpar->mb.pppi_pmv[0][i_s][0];
        p_mb->pppi_motion_vectors[1][i_s][1] = p_vpar->mb.pppi_pmv[0][i_s][1];
1185
    }
1186
    else
1187
    {
1188 1189 1190 1191
        p_mb->ppi_field_select[0][i_s] = GetBits( &p_vpar->bit_stream, 1 );
        MotionVector( p_vpar, p_mb, 0, i_s, 0, i_structure );
        p_mb->ppi_field_select[1][i_s] = GetBits( &p_vpar->bit_stream, 1 );
        MotionVector( p_vpar, p_mb, 1, i_s, 0, i_structure );