vpar_blocks.c 71.4 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

/*
 * Local prototypes
 */
Jean-Marc Dressler's avatar
Jean-Marc Dressler committed
43
typedef void (*f_decode_block_t)( vpar_thread_t *, macroblock_t *, int );
44 45 46 47
static void vpar_DecodeMPEG1Non( vpar_thread_t * p_vpar, macroblock_t * p_mb, int i_b );
static void vpar_DecodeMPEG1Intra( vpar_thread_t * p_vpar, macroblock_t * p_mb, int i_b );
static void vpar_DecodeMPEG2Non( vpar_thread_t * p_vpar, macroblock_t * p_mb, int i_b );
static void vpar_DecodeMPEG2Intra( vpar_thread_t * p_vpar, macroblock_t * p_mb, int i_b );
48

49
/*
50 51 52 53 54 55 56
 * 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
 * It's a pretty long file. Good luck !
57
 */
58 59 60 61 62 63


/*
 * Initialization tables
 */

64
    /* Table for coded_block_pattern resolution */
65
static lookup_t     pl_coded_pattern_init_table[512] = 
66
    { {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
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 122 123 124 125 126 127 128 129
    {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} };
130

131 132 133 134 135
    /* 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 */
136
static lookup_t     pl_dct_dc_lum_init_table_1[32] =
137 138 139 140 141 142 143
    { {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 */
144
static lookup_t     pl_dct_dc_lum_init_table_2[32] =
145 146 147 148 149 150 151 152 153
    { {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 */
154
static lookup_t     pl_dct_dc_chrom_init_table_1[32] =
155 156 157 158 159 160 161
    { {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 */
162
static lookup_t     pl_dct_dc_chrom_init_table_2[32] =
163 164 165 166 167
    { {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}
    };
168 169 170 171 172 173
    
    /* 
     * Structure to store the tables B14 & B15 
     * Is constructed from the tables below 
     */
    dct_lookup_t            ppl_dct_coef[2][16384];
174

175 176

    /* Tables for ac DCT coefficients. There are cut in many parts to save space */
177 178 179
    /* Table B-14, DCT coefficients table zero,
     * codes 0100 ... 1xxx (used for first (DC) coefficient)
     */
180
static dct_lookup_t pl_DCT_tab_dc[12] =
181 182 183 184 185 186 187 188 189
    {
        {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)
     */
190
static dct_lookup_t pl_DCT_tab_ac[12] =
191 192 193 194 195 196 197 198 199
    {
        {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
     */
200
static dct_lookup_t pl_DCT_tab0[60] =
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
    {
        {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
     */
223
static dct_lookup_t pl_DCT_tab0a[252] =
224
    {
225
        {65,0,6}, {65,0,6}, {65,0,6}, {65,0,6}, /* Escape */
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
        {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},
248 249 250 251
        {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},
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
        {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},
271
        {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
        {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}
    };
289

290 291 292
    /* Table B-14, DCT coefficients table zero,
     * codes 0000001000 ... 0000001111
     */
293
static dct_lookup_t pl_DCT_tab1[8] =
294 295 296 297 298 299 300 301
    {
        {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
     */
302
static dct_lookup_t pl_DCT_tab1a[8] =
303 304 305 306
    {
        {5,2,9}, {5,2,9}, {14,1,9}, {14,1,9},
        {2,4,10}, {16,1,10}, {15,1,9}, {15,1,9}
    };
307

308 309 310
    /* Table B-14/15, DCT coefficients table zero / one,
     * codes 000000010000 ... 000000011111
     */
311
static dct_lookup_t pl_DCT_tab2[16] =
312 313 314 315 316 317 318 319 320 321
    {
        {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
     */
322
static dct_lookup_t pl_DCT_tab3[16] =
323 324 325 326 327 328 329 330 331 332
    {
        {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
     */
333
static dct_lookup_t pl_DCT_tab4[16] =
334 335 336 337 338 339 340 341 342 343
    {
        {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
     */
344
static dct_lookup_t pl_DCT_tab5[16] =
345 346 347 348 349 350 351 352 353 354
    {
    {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
     */
355
static dct_lookup_t pl_DCT_tab6[16] =
356 357 358 359 360 361
    {
        {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}
    };
362 363


364 365 366 367 368 369 370 371
/*
 * Initialization of lookup tables
 */

/*****************************************************************************
 * vpar_InitCrop : Initialize the crop table for saturation
 *                 (ISO/IEC 13818-2 section 7.4.3)
 *****************************************************************************/
372
#if defined(MPEG2_COMPLIANT) && !defined(VDEC_DFT)
373 374 375 376
void vpar_InitCrop( vpar_thread_t * p_vpar )
{
    int i_dummy;

377
    p_vpar->pi_crop = p_vpar->pi_crop_buf + 4096;
378

379
    for( i_dummy = -4096; i_dummy < -2048; i_dummy++ )
380 381 382 383 384 385 386
    {
        p_vpar->pi_crop[i_dummy] = -2048;
    }
    for( ; i_dummy < 2047; i_dummy++ )
    {
        p_vpar->pi_crop[i_dummy] = i_dummy;
    }
387
    for( ; i_dummy < 4095; i_dummy++ )
388 389 390 391 392
    {
        p_vpar->pi_crop[i_dummy] = 2047;
    }
}
#endif
393 394

/*****************************************************************************
395
 * vpar_InitMbAddrInc : Initialize the lookup table for mb_addr_inc
396 397
 *****************************************************************************/

398
/* Function for filling up the lookup table for mb_addr_inc */
399
static void __inline__ FillMbAddrIncTable( vpar_thread_t * p_vpar,
400 401 402
                                    int i_start, int i_end, int i_step, 
                                    int * pi_value, int i_length )
{
403 404
    int i_pos, i_offset;
    for( i_pos = i_start ; i_pos < i_end ; i_pos += i_step )
405
    {
406
        for( i_offset = 0 ; i_offset < i_step ; i_offset ++ )
407
        {
408 409
            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;
410
        }
411
        (*pi_value)--;
412
    }
413 414
}
    
415 416
/* Function that initialize the table using the last one */
void vpar_InitMbAddrInc( vpar_thread_t * p_vpar )
417
{
418
    int i_dummy;
419
    int i_value;
420
    
Jean-Marc Dressler's avatar
Jean-Marc Dressler committed
421
    for( i_dummy = 0 ; i_dummy < 8 ; i_dummy++ )
422 423 424 425 426 427 428 429
    {
        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
430
    for( i_dummy = 9 ; i_dummy < 15 ; i_dummy ++ ) 
431 432 433 434 435 436 437 438
    {
        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
439
    for( i_dummy = 16; i_dummy < 24; i_dummy++ ) 
440 441 442 443
    {
        p_vpar->pl_mb_addr_inc[i_dummy].i_value =  MB_ERROR;
        p_vpar->pl_mb_addr_inc[i_dummy].i_length = 0;
    }
444
    
445
    i_value = 33;
446
    
447
    FillMbAddrIncTable( p_vpar, 24, 36, 1, &i_value, 11 );
448 449 450 451 452 453 454
    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 );
455 456 457
}

/*****************************************************************************
458
 * vpar_Init*MBType : Initialize lookup table for the Macroblock type
459
 *****************************************************************************/
460 461 462 463 464 465 466 467 468 469

/* 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
470 471
    
    for( i_dummy = i_start ; i_dummy < i_end ; i_dummy++ )
472
    {
473 474
        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;
475 476 477 478
    }
}

/* Fonction that fills the table for P MB_Type */
479
void vpar_InitPMBType( vpar_thread_t * p_vpar )
480 481 482 483 484
{
    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
485
    FillMBType( p_vpar, 0, 4, 6, MB_QUANT|MB_MOTION_FORWARD|MB_PATTERN, 5 );
486
    FillMBType( p_vpar, 0, 2, 4, MB_QUANT|MB_PATTERN, 5 );
487 488 489 490
    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;
491 492 493
}

/* Fonction that fills the table for B MB_Type */
494
void vpar_InitBMBType( vpar_thread_t * p_vpar )
495 496 497 498 499 500 501 502 503 504 505
{
    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 );
506 507 508 509 510 511 512 513
    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;
514 515 516
}

/*****************************************************************************
517 518
 * vpar_InitCodedPattern : Initialize the lookup table for decoding
 *                         coded block pattern
519
 *****************************************************************************/
520
void vpar_InitCodedPattern( vpar_thread_t * p_vpar )
521
{
522
    p_vpar->pl_coded_pattern = (lookup_t*) pl_coded_pattern_init_table;
523 524
}

525
/*****************************************************************************
526 527
 * vpar_InitDCTTables : Initialize tables giving the length of the dct
 *                      coefficient from the vlc code
528 529
 *****************************************************************************/

530
/* First fonction for filling the table */
531 532
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 )
533 534
{
    int i_dummy, i_dummy2;
535 536

    for( i_dummy=0 ; i_dummy < i_nb_elem ; i_dummy++ )
537
    {
538
        for( i_dummy2=0 ; i_dummy2 < i_step ; i_dummy2++ )
539
        {
540
            p_tab_dest[(i_dummy+i_offset)*i_step+i_dummy2] = p_tab_src[i_dummy];
541 542 543 544 545 546
        }
    }
}


/* Fonction that actually fills the table or create the pointers */
547
void vpar_InitDCTTables( vpar_thread_t * p_vpar )
548
{
549
    /* Tables are cut in two parts to reduce memory occupation */    
550 551 552 553
    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;
554 555 556

    memset( ppl_dct_coef[0], MB_ERROR, 16 );
    memset( ppl_dct_coef[1], MB_ERROR, 16 );
557
    
558
    /* For table B14 & B15, we have a pointer to tables */
559
    /* We fill the table thanks to the fonction defined above */
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
    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 );
575 576
}

577

578
/*
579
 * Block parsing
580
 */
581

582
/*****************************************************************************
583
 * vpar_DecodeMPEG1Non : decode MPEG-1 non-intra blocks
584
 *****************************************************************************/
585
static void vpar_DecodeMPEG1Non( vpar_thread_t * p_vpar, macroblock_t * p_mb, int i_b )
586 587
{

588
    if( p_vpar->picture.i_coding_type == D_CODING_TYPE )
589
    {
590 591 592
        /* 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 );
593
    }
594 595 596
}

/*****************************************************************************
597
 * vpar_DecodeMPEG1Intra : decode MPEG-1 intra blocks
598
 *****************************************************************************/
599
static void vpar_DecodeMPEG1Intra( vpar_thread_t * p_vpar, macroblock_t * p_mb, int i_b )
600
{
601 602

    if( p_vpar->picture.i_coding_type == D_CODING_TYPE )
603
    {
604 605 606
        /* 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 );
607 608 609 610
    }
}

/*****************************************************************************
611
 * vpar_DecodeMPEG2Non : decode MPEG-2 non-intra blocks
612
 *****************************************************************************/
613
static void vpar_DecodeMPEG2Non( vpar_thread_t * p_vpar, macroblock_t * p_mb, int i_b )
614
{
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
    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;
    int         i_quant_type;
    boolean_t   b_sign;
    int *       ppi_quant[2];
    
    /* Lookup Table for the chromatic component */
    static int pi_cc_index[12] = { 0, 0, 0, 0, 1, 2, 1, 2, 1, 2 };
631

632 633 634 635 636
    i_cc = pi_cc_index[i_b];

    /* Determine whether it is luminance or not (chrominance) */
    i_type = ( i_cc + 1 ) >> 1;
    i_quant_type = (!i_type) || (p_vpar->sequence.i_chroma_format == CHROMA_420);
637
    
638 639 640 641 642
    /* Give a pointer to the quantization matrices for intra blocks */
    ppi_quant[1] = p_vpar->sequence.nonintra_quant.pi_matrix;
    ppi_quant[0] = p_vpar->sequence.chroma_nonintra_quant.pi_matrix;

    /* Decoding of the AC coefficients */
643
    
644 645 646
    i_nc = 0;
    i_coef = 0;
    for( i_parse = 0; ; i_parse++ )
647
    {
648 649
        i_code = ShowBits( &p_vpar->bit_stream, 16 );
        if( i_code >= 16384 )
650
        {
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
            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;
669 670 671
        }
        else
        {
672 673 674
            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;
675 676
        }

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

680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
        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;
699

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

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

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

723
/*****************************************************************************
724
 * vpar_DecodeMPEG2Intra : decode MPEG-2 intra blocks
725
 *****************************************************************************/
726
static void vpar_DecodeMPEG2Intra( vpar_thread_t * p_vpar, macroblock_t * p_mb, int i_b )
727
{
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
    int         i_parse;
    int         i_nc;
    int         i_cc;
    int         i_coef;
    int         i_type, i_quant_type;
    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;
    int *       ppi_quant[2];
    
    /* 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];
747

748 749 750
    /* Determine whether it is luminance or not (chrominance) */
    i_type = ( i_cc + 1 ) >> 1;
    i_quant_type = (!i_type) | (p_vpar->sequence.i_chroma_format == CHROMA_420);
Gaël Hendryckx's avatar
CA...  
Gaël Hendryckx committed
751

752 753 754
    /* Give a pointer to the quantization matrices for intra blocks */
    ppi_quant[1] = p_vpar->sequence.intra_quant.pi_matrix;
    ppi_quant[0] = p_vpar->sequence.chroma_intra_quant.pi_matrix;
755

756 757 758 759 760 761 762 763 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
#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);
789
        }
790 791 792 793 794
    }
    else
    {
        /* decode length */
        i_code = ShowBits(&p_vpar->bit_stream, 5);
795

796
        if (i_code<31)
797
        {
798 799 800
            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);
801
        }
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
        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;
    }
818

819 820 821 822 823 824
    /* 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 */
    p_vpar->slice.pi_dc_dct_pred[i_cc] += i_dct_dc_diff;
825

826 827 828
    p_mb->ppi_blocks[i_b][0] = ( p_vpar->slice.pi_dc_dct_pred[i_cc] <<
                               ( 3 - p_vpar->picture.i_intra_dc_precision ) );
    i_nc = ( p_vpar->slice.pi_dc_dct_pred[i_cc] != 0 );
829

830 831 832 833 834 835 836 837 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
    /* 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");
895
    }
896

897
    if( (i_run != tab.i_run) || (i_length != tab.i_length) || (i_level != tab.i_level) )
898
    {
899 900
        fprintf( stderr, "ET M....... !!!\n" );
        exit(0);
901
    }
902
        }
903
#endif
904 905 906



907 908 909 910 911 912 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
        
        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 *
                    p_vpar->slice.i_quantizer_scale *
                    ppi_quant[i_quant_type][i_pos] ) >> 4;
        p_mb->ppi_blocks[i_b][i_pos] = b_sign ? -i_level : i_level;
952 953
    }

954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984
    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 )
985
    {
986 987 988 989
        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 );
990 991
    }

992
    if( i_code >= 24 )
993
    {
994 995 996 997
        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 );
998 999
    }

1000
    if( (i_code -= 12) < 0 )
1001
    {
1002 1003 1004
        p_vpar->picture.b_error = 1;
        intf_DbgMsg( "vpar debug: Invalid motion_vector code\n" );
        return 0;
1005
    }
1006 1007 1008 1009

    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 );            
1010
}
1011

1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
/****************************************************************************
 * 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 )
1025
    {
1026 1027 1028
        i_vector += ((i_motion_code-1) << i_r_size) + i_motion_residual + 1;
        if( i_vector >= i_limit )
            i_vector -= i_limit + i_limit;
1029
    }
1030
    else if( i_motion_code < 0 )
1031
    {
1032 1033 1034
        i_vector -= ((-i_motion_code-1) << i_r_size) + i_motion_residual + 1;
        if( i_vector < -i_limit )
            i_vector += i_limit + i_limit;
1035
    }
1036 1037
    *pi_prediction = i_vector << i_full_pel;
}
1038

1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
/****************************************************************************
 * 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;
    DecodeMotionVector( &p_vpar->slice.pppi_pmv[i_r][i_s][0], i_r_size,
                        i_motion_code, i_motion_residual, i_full_pel );
    p_mb->pppi_motion_vectors[i_r][i_s][0] = p_vpar->slice.pppi_pmv[i_r][i_s][0];

    if( p_vpar->mb.b_dmv )
1059
    {
1060
        if( GetBits(&p_vpar->bit_stream, 1) )
1061
        {
1062 1063 1064 1065 1066
            pi_dm_vector[0] = GetBits( &p_vpar->bit_stream, 1 ) ? -1 : 1;
        }
        else
        {
            pi_dm_vector[0] = 0;
1067 1068
        }
    }
1069 1070 1071 1072 1073
    
    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;
1074

1075 1076 1077 1078 1079 1080
   
    if( (p_vpar->mb.i_mv_format == MOTION_FIELD)
        && (i_structure == FRAME_STRUCTURE) )
    {
         p_vpar->slice.pppi_pmv[i_r][i_s][1] >>= 1;
    }
1081
    
1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
    DecodeMotionVector( &p_vpar->slice.pppi_pmv[i_r][i_s][1], i_r_size,
                        i_motion_code, i_motion_residual, i_full_pel );

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

    if( p_vpar->mb.b_dmv )
1092
    {
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
        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). */
1103

1104 1105
#define i_mv_x  p_mb->pppi_motion_vectors[0][0][0]
        if( i_structure == FRAME_STRUCTURE )
1106
        {
1107 1108 1109 1110 1111 1112
#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;
1113

1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
                /* 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
1143
        }
1144
#undef i_mv_x
1145
    }
1146
}
1147

1148 1149 1150 1151 1152 1153 1154 1155 1156
/*****************************************************************************
 * 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 );
}
1157

1158 1159 1160 1161 1162 1163 1164
/*****************************************************************************
 * 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 )
1165
    {
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175
        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 );
        p_vpar->slice.pppi_pmv[1][i_s][0] = p_vpar->slice.pppi_pmv[0][i_s][0];
        p_vpar->slice.pppi_pmv[1][i_s][1] = p_vpar->slice.pppi_pmv[0][i_s][1];
        p_mb->pppi_motion_vectors[1][i_s][0] = p_vpar->slice.pppi_pmv[0][i_s][0];
        p_mb->pppi_motion_vectors[1][i_s][1] = p_vpar->slice.pppi_pmv[0][i_s][1];
1176
    }
1177
    else
1178
    {
1179 1180 1181 1182
        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 );
1183
    }
1184
}
1185 1186


Jean-Marc Dressler's avatar
Jean-Marc Dressler committed
1187
/*
1188 1189 1190 1191 1192 1193 1194
 * Macroblock information structures
 */

/*****************************************************************************
 * MacroblockAddressIncrement : Get the macroblock_address_increment field
 *****************************************************************************/
static __inline__ int MacroblockAddressIncrement( vpar_thread_t * p_vpar )
1195
{
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214
    int i_addr_inc = 0;
    /* Index in the lookup table mb_addr_inc */
    int    i_index = ShowBits( &p_vpar->bit_stream, 11 );
        
    /* Test the presence of the escape character */
    while( i_index == 8 )
    {
        RemoveBits( &p_vpar->bit_stream, 11 );
        i_addr_inc += 33;
        i_index = ShowBits( &p_vpar->bit_stream, 11 );
    }
     
    /* Affect the value from the lookup table */
    i_addr_inc += p_vpar->pl_mb_addr_inc[i_index].i_value;
    
    /* Dump the good number of bits */
    RemoveBits( &p_vpar->bit_stream, p_vpar->pl_mb_addr_inc[i_index].i_length );
    
    return i_addr_inc;
1215 1216
}

1217
/*****************************************************************************
1218
 * IMBType : macroblock_type in I pictures
1219
 *****************************************************************************/
1220
static __inline__ int IMBType( vpar_thread_t * p_vpar )
1221
{
1222 1223
    /* Take two bits for testing */
    int                 i_type = ShowBits( &p_vpar->bit_stream, 2 );
1224
    
1225 1226
    /* Lookup table for macroblock_type */
    static lookup_t     pl_mb_Itype[4] = { {MB_ERROR, 0},
1227 1228
                                           {MB_QUANT|MB_INTRA, 2},
                                           {MB_INTRA, 1},
1229
                                           {MB_INTRA, 1} };
1230
    /* Dump the good number of bits */