vpar_blocks.c 90.6 KB
Newer Older
1 2
/*****************************************************************************
 * vpar_blocks.c : blocks parsing
3 4 5
 *****************************************************************************
 * Copyright (C) 1999, 2000 VideoLAN
 *
6 7 8
 * Authors: Christophe Massiot <massiot@via.ecp.fr>
 *          Jean-Marc Dressler <polux@via.ecp.fr>
 *          Stéphane Borel <stef@via.ecp.fr>
9 10 11 12 13
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
14
 * 
15 16
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
19
 *
20 21 22
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23 24 25 26 27
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
28 29
#include "defs.h"

30 31
#include <sys/types.h>                        /* on BSD, uio.h needs types.h */
#include <sys/uio.h>                                            /* "input.h" */
Sam Hocevar's avatar
Sam Hocevar committed
32
#include <string.h>                                                /* memset */
33 34 35

#include "config.h"
#include "common.h"
36
#include "threads.h"
37
#include "mtime.h"
38
#include "plugins.h"
39 40

#include "intf_msg.h"
41
#include "debug.h"                 /* XXX?? temporaire, requis par netlist.h */
42 43 44 45 46 47 48

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

49
#include "vdec_idct.h"
50
#include "video_decoder.h"
51 52 53 54
#include "vdec_motion.h"

#include "vpar_blocks.h"
#include "vpar_headers.h"
55
#include "vpar_synchro.h"
56
#include "video_parser.h"
57
#include "video_fifo.h"
58

59
/*
60 61 62 63 64 65
 * 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
66 67
 *  - Picture data parsing management (slices and error handling)
 * It's a pretty long file. Good luck and have a nice day.
68
 */
69 70 71 72 73 74


/*
 * Initialization tables
 */

Stéphane Borel's avatar
 
Stéphane Borel committed
75
    /* Table for coded_block_pattern resolution */
76
static lookup_t     pl_coded_pattern[512] =
Stéphane Borel's avatar
 
Stéphane Borel committed
77
    { {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
78 79 80 81 82 83 84 85
    {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},
86 87 88 89 90 91 92
    {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},
Jean-Marc Dressler's avatar
 
Jean-Marc Dressler committed
93 94
    {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},
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
    {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},
Jean-Marc Dressler's avatar
 
Jean-Marc Dressler committed
125
    {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4},
126 127 128 129
    {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},
Jean-Marc Dressler's avatar
 
Jean-Marc Dressler committed
130 131
    {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},
132 133 134 135 136 137 138 139
    {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},
Jean-Marc Dressler's avatar
 
Jean-Marc Dressler committed
140
    {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3} };
141

142
    /* Tables for dc DCT coefficients
Stéphane Borel's avatar
 
Stéphane Borel committed
143 144
     * Tables are cut in two parts to reduce memory occupation
     */
145

146
    /* Table B-12, dct_dc_size_luminance, codes 00XXX ... 11110 */
Stéphane Borel's avatar
 
Stéphane Borel committed
147

148
static lookup_t     pl_dct_dc_lum_init_table_1[32] =
Stéphane Borel's avatar
 
Stéphane Borel committed
149 150 151 152 153 154
    { {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}
    };

Stéphane Borel's avatar
 
Stéphane Borel committed
155 156 157 158 159 160 161 162 163 164 165 166
static lookup_t     ppl_dct_dc_init_table_1[2][32] =
{    { {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}},
{ {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}
    }
    };

Stéphane Borel's avatar
 
Stéphane Borel committed
167
    /* Table B-12, dct_dc_size_luminance, codes 111110xxx ... 111111111 */
Stéphane Borel's avatar
 
Stéphane Borel committed
168

169
static lookup_t     pl_dct_dc_lum_init_table_2[32] =
Stéphane Borel's avatar
 
Stéphane Borel committed
170 171 172 173 174 175 176 177
    { {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}
    };

Stéphane Borel's avatar
 
Stéphane Borel committed
178 179 180 181 182 183 184 185 186 187 188 189 190 191
static lookup_t     ppl_dct_dc_init_table_2[2][32] =
{    { {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}},
    { {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}
    }
    };

Stéphane Borel's avatar
 
Stéphane Borel committed
192
    /* Table B-13, dct_dc_size_chrominance, codes 00xxx ... 11110 */
193
static lookup_t     pl_dct_dc_chrom_init_table_1[32] =
Stéphane Borel's avatar
 
Stéphane Borel committed
194
 { {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2},
Stéphane Borel's avatar
 
Stéphane Borel committed
195 196 197 198
      {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}
    };
Stéphane Borel's avatar
 
Stéphane Borel committed
199
    
Stéphane Borel's avatar
 
Stéphane Borel committed
200

201
   /* Table B-13, dct_dc_size_chrominance, codes 111110xxxx ... 1111111111 */
202
static lookup_t     pl_dct_dc_chrom_init_table_2[32] =
Stéphane Borel's avatar
 
Stéphane Borel committed
203 204 205 206 207
    { {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}
    };
Stéphane Borel's avatar
 
Stéphane Borel committed
208
 
Jean-Marc Dressler's avatar
 
Jean-Marc Dressler committed
209 210

    /* Tables for ac DCT coefficients. There are cut in many parts to save space */
Stéphane Borel's avatar
 
Stéphane Borel committed
211 212 213
    /* Table B-14, DCT coefficients table zero,
     * codes 0100 ... 1xxx (used for first (DC) coefficient)
     */
214
static dct_lookup_t pl_DCT_tab_dc[12] =
Stéphane Borel's avatar
 
Stéphane Borel committed
215 216 217 218 219 220 221 222 223
    {
        {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)
     */
224
static dct_lookup_t pl_DCT_tab_ac[12] =
Stéphane Borel's avatar
 
Stéphane Borel committed
225 226 227 228 229 230 231 232 233
    {
        {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
     */
234
static dct_lookup_t pl_DCT_tab0[60] =
Stéphane Borel's avatar
 
Stéphane Borel committed
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
    {
        {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
     */
257
static dct_lookup_t pl_DCT_tab0a[252] =
Stéphane Borel's avatar
 
Stéphane Borel committed
258
    {
Stéphane Borel's avatar
 
Stéphane Borel committed
259
        {65,0,6}, {65,0,6}, {65,0,6}, {65,0,6}, /* Escape */
Stéphane Borel's avatar
 
Stéphane Borel committed
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
        {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},
Stéphane Borel's avatar
 
Stéphane Borel committed
282 283 284 285
        {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},
Stéphane Borel's avatar
 
Stéphane Borel committed
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
        {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},
Stéphane Borel's avatar
 
Stéphane Borel committed
305
        {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
Stéphane Borel's avatar
 
Stéphane Borel committed
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
        {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}
    };
Jean-Marc Dressler's avatar
 
Jean-Marc Dressler committed
323

Stéphane Borel's avatar
 
Stéphane Borel committed
324 325 326
    /* Table B-14, DCT coefficients table zero,
     * codes 0000001000 ... 0000001111
     */
327
static dct_lookup_t pl_DCT_tab1[8] =
Stéphane Borel's avatar
 
Stéphane Borel committed
328 329 330 331 332 333 334 335
    {
        {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
     */
336
static dct_lookup_t pl_DCT_tab1a[8] =
Stéphane Borel's avatar
 
Stéphane Borel committed
337 338 339 340
    {
        {5,2,9}, {5,2,9}, {14,1,9}, {14,1,9},
        {2,4,10}, {16,1,10}, {15,1,9}, {15,1,9}
    };
341

Stéphane Borel's avatar
 
Stéphane Borel committed
342 343 344
    /* Table B-14/15, DCT coefficients table zero / one,
     * codes 000000010000 ... 000000011111
     */
345
static dct_lookup_t pl_DCT_tab2[16] =
Stéphane Borel's avatar
 
Stéphane Borel committed
346 347 348 349 350 351 352 353 354 355
    {
        {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
     */
356
static dct_lookup_t pl_DCT_tab3[16] =
Stéphane Borel's avatar
 
Stéphane Borel committed
357 358 359 360 361 362 363 364 365 366
    {
        {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
     */
367
static dct_lookup_t pl_DCT_tab4[16] =
Stéphane Borel's avatar
 
Stéphane Borel committed
368 369 370 371 372 373 374 375 376 377
    {
        {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
     */
378
static dct_lookup_t pl_DCT_tab5[16] =
Stéphane Borel's avatar
 
Stéphane Borel committed
379 380 381 382 383 384 385 386 387 388
    {
    {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
     */
389
static dct_lookup_t pl_DCT_tab6[16] =
Stéphane Borel's avatar
 
Stéphane Borel committed
390 391 392 393 394 395
    {
        {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}
    };
396

Stéphane Borel's avatar
 
Stéphane Borel committed
397 398 399
    /* Pointers on tables of dct coefficients */
static dct_lookup_t * ppl_dct_tab1[2] = { pl_DCT_tab_ac, pl_DCT_tab0a };

Stéphane Borel's avatar
 
Stéphane Borel committed
400
static dct_lookup_t * ppl_dct_tab2[2] = { pl_DCT_tab_ac, pl_DCT_tab_dc };
Stéphane Borel's avatar
 
Stéphane Borel committed
401 402 403 404 405


    /* Lookup Table for the chromatic component */
static int pi_cc_index[12] = { 0, 0, 0, 0, 1, 2, 1, 2, 1, 2 };

406

407 408 409 410 411 412 413 414
/*
 * Initialization of lookup tables
 */

/*****************************************************************************
 * vpar_InitCrop : Initialize the crop table for saturation
 *                 (ISO/IEC 13818-2 section 7.4.3)
 *****************************************************************************/
415
#if defined(MPEG2_COMPLIANT) && !defined(VDEC_DFT)
416 417 418 419
void vpar_InitCrop( vpar_thread_t * p_vpar )
{
    int i_dummy;

Jean-Marc Dressler's avatar
 
Jean-Marc Dressler committed
420
    p_vpar->pi_crop = p_vpar->pi_crop_buf + 4096;
421

Jean-Marc Dressler's avatar
 
Jean-Marc Dressler committed
422
    for( i_dummy = -4096; i_dummy < -2048; i_dummy++ )
423 424 425 426 427 428 429
    {
        p_vpar->pi_crop[i_dummy] = -2048;
    }
    for( ; i_dummy < 2047; i_dummy++ )
    {
        p_vpar->pi_crop[i_dummy] = i_dummy;
    }
Jean-Marc Dressler's avatar
 
Jean-Marc Dressler committed
430
    for( ; i_dummy < 4095; i_dummy++ )
431 432 433 434 435
    {
        p_vpar->pi_crop[i_dummy] = 2047;
    }
}
#endif
Stéphane Borel's avatar
 
Stéphane Borel committed
436 437

/*****************************************************************************
438
 * vpar_InitMbAddrInc : Initialize the lookup table for mb_addr_inc
Stéphane Borel's avatar
 
Stéphane Borel committed
439 440
 *****************************************************************************/

Jean-Marc Dressler's avatar
 
Jean-Marc Dressler committed
441
/* Function for filling up the lookup table for mb_addr_inc */
442
static void FillMbAddrIncTable( vpar_thread_t * p_vpar,
443
                                    int i_start, int i_end, int i_step,
444 445
                                    int * pi_value, int i_length )
{
Stéphane Borel's avatar
 
Stéphane Borel committed
446 447
    int i_pos, i_offset;
    for( i_pos = i_start ; i_pos < i_end ; i_pos += i_step )
Stéphane Borel's avatar
 
Stéphane Borel committed
448
    {
Stéphane Borel's avatar
 
Stéphane Borel committed
449
        for( i_offset = 0 ; i_offset < i_step ; i_offset ++ )
450
        {
Stéphane Borel's avatar
 
Stéphane Borel committed
451 452
            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;
453
        }
Jean-Marc Dressler's avatar
 
Jean-Marc Dressler committed
454
        (*pi_value)--;
Stéphane Borel's avatar
 
Stéphane Borel committed
455
    }
456
}
457

Jean-Marc Dressler's avatar
 
Jean-Marc Dressler committed
458 459
/* Function that initialize the table using the last one */
void vpar_InitMbAddrInc( vpar_thread_t * p_vpar )
Stéphane Borel's avatar
 
Stéphane Borel committed
460
{
461
    int i_dummy;
Stéphane Borel's avatar
 
Stéphane Borel committed
462
    int i_value;
463

Jean-Marc Dressler's avatar
 
Jean-Marc Dressler committed
464
    for( i_dummy = 0 ; i_dummy < 8 ; i_dummy++ )
465 466 467 468 469 470 471 472
    {
        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;

473
    for( i_dummy = 9 ; i_dummy < 15 ; i_dummy ++ )
474 475 476 477 478 479 480 481
    {
        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;

482
    for( i_dummy = 16; i_dummy < 24; i_dummy++ )
483 484 485 486
    {
        p_vpar->pl_mb_addr_inc[i_dummy].i_value =  MB_ERROR;
        p_vpar->pl_mb_addr_inc[i_dummy].i_length = 0;
    }
487

Stéphane Borel's avatar
 
Stéphane Borel committed
488
    i_value = 33;
489

Stéphane Borel's avatar
 
Stéphane Borel committed
490
    FillMbAddrIncTable( p_vpar, 24, 36, 1, &i_value, 11 );
Jean-Marc Dressler's avatar
 
Jean-Marc Dressler committed
491 492 493 494 495 496 497
    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 );
498 499 500
}

/*****************************************************************************
501
 * vpar_Init*MBType : Initialize lookup table for the Macroblock type
Stéphane Borel's avatar
 
Stéphane Borel committed
502
 *****************************************************************************/
503 504

/* Fonction for filling up the tables */
505
static void FillMBType( vpar_thread_t * p_vpar,
506 507 508 509 510 511 512
                                   int           i_mb_type,
                                   int           i_start,
                                   int           i_end,
                                   int           i_value,
                                   int           i_length )
{
    int i_dummy;
513

Jean-Marc Dressler's avatar
 
Jean-Marc Dressler committed
514
    for( i_dummy = i_start ; i_dummy < i_end ; i_dummy++ )
515
    {
Stéphane Borel's avatar
 
Stéphane Borel committed
516 517
        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;
518 519 520 521
    }
}

/* Fonction that fills the table for P MB_Type */
Jean-Marc Dressler's avatar
 
Jean-Marc Dressler committed
522
void vpar_InitPMBType( vpar_thread_t * p_vpar )
523 524 525 526 527
{
    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
528
    FillMBType( p_vpar, 0, 4, 6, MB_QUANT|MB_MOTION_FORWARD|MB_PATTERN, 5 );
529
    FillMBType( p_vpar, 0, 2, 4, MB_QUANT|MB_PATTERN, 5 );
Stéphane Borel's avatar
 
Stéphane Borel committed
530 531 532 533
    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;
534 535 536
}

/* Fonction that fills the table for B MB_Type */
Jean-Marc Dressler's avatar
 
Jean-Marc Dressler committed
537
void vpar_InitBMBType( vpar_thread_t * p_vpar )
538 539 540 541 542 543 544 545 546 547 548
{
    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 );
Stéphane Borel's avatar
 
Stéphane Borel committed
549 550 551 552 553 554 555 556
    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;
557 558
}

Stéphane Borel's avatar
 
Stéphane Borel committed
559

Stéphane Borel's avatar
 
Stéphane Borel committed
560
/*****************************************************************************
561 562
 * vpar_InitDCTTables : Initialize tables giving the length of the dct
 *                      coefficient from the vlc code
Stéphane Borel's avatar
 
Stéphane Borel committed
563 564
 *****************************************************************************/

Stéphane Borel's avatar
 
Stéphane Borel committed
565
/* First fonction for filling the table */
566
static void FillDCTTable( dct_lookup_t * p_tab_dest, dct_lookup_t * p_tab_src,
Jean-Marc Dressler's avatar
 
Jean-Marc Dressler committed
567
                                     int i_step, int i_nb_elem, int i_offset )
Stéphane Borel's avatar
 
Stéphane Borel committed
568 569
{
    int i_dummy, i_dummy2;
Jean-Marc Dressler's avatar
 
Jean-Marc Dressler committed
570 571

    for( i_dummy=0 ; i_dummy < i_nb_elem ; i_dummy++ )
Stéphane Borel's avatar
 
Stéphane Borel committed
572
    {
Jean-Marc Dressler's avatar
 
Jean-Marc Dressler committed
573
        for( i_dummy2=0 ; i_dummy2 < i_step ; i_dummy2++ )
Stéphane Borel's avatar
 
Stéphane Borel committed
574
        {
Jean-Marc Dressler's avatar
 
Jean-Marc Dressler committed
575
            p_tab_dest[(i_dummy+i_offset)*i_step+i_dummy2] = p_tab_src[i_dummy];
Stéphane Borel's avatar
 
Stéphane Borel committed
576 577 578 579 580 581
        }
    }
}


/* Fonction that actually fills the table or create the pointers */
Jean-Marc Dressler's avatar
 
Jean-Marc Dressler committed
582
void vpar_InitDCTTables( vpar_thread_t * p_vpar )
Stéphane Borel's avatar
 
Stéphane Borel committed
583
{
584
    /* Tables are cut in two parts to reduce memory occupation */
Stéphane Borel's avatar
 
Stéphane Borel committed
585 586 587 588
    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;
Jean-Marc Dressler's avatar
 
Jean-Marc Dressler committed
589

590
    /* XXX?? MB_ERROR is replaced by 0 because if we use -1 we
591 592 593
     * can block in DecodeMPEG2Intra and others */
    memset( p_vpar->ppl_dct_coef[0], 0, 16 );
    memset( p_vpar->ppl_dct_coef[1], 0, 16 );
594

Stéphane Borel's avatar
 
Stéphane Borel committed
595
    /* For table B14 & B15, we have a pointer to tables */
Stéphane Borel's avatar
 
Stéphane Borel committed
596
    /* We fill the table thanks to the fonction defined above */
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
    FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab0, 256, 60,  4 );
    FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab1,  64,  8,  8 );
    FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab2,  16, 16, 16 );
    FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab3,   8, 16, 16 );
    FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab4,   4, 16, 16 );
    FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab5,   2, 16, 16 );
    FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab6,   1, 16, 16 );

    FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab0a, 256, 60, 4 );
    FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab1a,  64,  8,  8 );
    FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab2,   16, 16, 16 );
    FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab3,    8, 16, 16 );
    FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab4,    4, 16, 16 );
    FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab5,    2, 16, 16 );
    FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab6,    1, 16, 16 );
Stéphane Borel's avatar
 
Stéphane Borel committed
612 613
}

614

615
/*
616
 * Block parsing
617
 */
618

619
/*****************************************************************************
620
 * DecodeMPEG1NonIntra : decode MPEG-1 non-intra blocks
621
 *****************************************************************************/
Sam Hocevar's avatar
Sam Hocevar committed
622
static __inline__ void DecodeMPEG1NonIntra( vpar_thread_t * p_vpar,
623 624
                                            macroblock_t * p_mb, int i_b,
                                            int i_chroma_format )
625
{
Stéphane Borel's avatar
 
Stéphane Borel committed
626 627 628 629 630 631 632 633 634 635 636 637
    int         i_parse;
    int         i_nc;
    int         i_cc;
    int         i_coef;
    int         i_code;
    int         i_length;
    int         i_pos;
    int         i_run;
    int         i_level;
    boolean_t   b_dc;
    boolean_t   b_sign;
    boolean_t   b_chroma;
638

Stéphane Borel's avatar
 
Stéphane Borel committed
639 640 641 642 643 644 645
    /* Give the chromatic component (0, 1, 2) */
    i_cc = pi_cc_index[i_b];

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

    /* There should be no D picture in non-intra blocks */
646
    if( p_vpar->picture.i_coding_type == D_CODING_TYPE )
Stéphane Borel's avatar
 
Stéphane Borel committed
647 648 649 650 651 652 653 654 655
        intf_ErrMsg("vpar error : D-picture in non intra block\n");
    
    /* Decoding of the AC coefficients */

    i_nc = 0;
    i_coef = 0;
    b_sign = 0;

    for( i_parse = 0; !p_vpar->b_die; i_parse++ )
656
    {
Stéphane Borel's avatar
 
Stéphane Borel committed
657 658 659
        i_code = ShowBits( &p_vpar->bit_stream, 16 );
        if( i_code >= 16384 )
        {
660
            b_dc = (i_parse == 0);
Stéphane Borel's avatar
 
Stéphane Borel committed
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
            i_run =     ppl_dct_tab2[b_dc][(i_code>>12)-4].i_run;
            i_level =   ppl_dct_tab2[b_dc][(i_code>>12)-4].i_level;
            i_length =  ppl_dct_tab2[b_dc][(i_code>>12)-4].i_length;
        }
        else
        {
            i_run =     p_vpar->ppl_dct_coef[0][i_code].i_run;
            i_length =  p_vpar->ppl_dct_coef[0][i_code].i_length;
            i_level =   p_vpar->ppl_dct_coef[0][i_code].i_level;
        }

        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, 8 );
                if (i_level == 0)
                    i_level = GetBits( &p_vpar->bit_stream, 8 );
                else if (i_level == 128)
                    i_level = GetBits( &p_vpar->bit_stream, 8 ) - 256;
                else if (i_level > 128)
                    i_level -= 256;

686
                if( (b_sign = i_level < 0) )
Stéphane Borel's avatar
 
Stéphane Borel committed
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
                    i_level = -i_level;
                
                break;
            case DCT_EOB:
#ifdef HAVE_MMX
                /* The MMX IDCT has a precision problem with non-intra
                 * blocks. */
                p_mb->ppi_blocks[i_b][0] += 4;
#endif
                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 << 1) + 1) * p_vpar->mb.i_quantizer_scale
                    * p_vpar->sequence.nonintra_quant.pi_matrix[i_pos] ) >> 4;
723 724 725 726 727 728 729 730

        /* Mismatch control */
        if( i_level ) /* Should always be true */
        {
            /* Equivalent to : if( (val & 1) == 0 ) val = val - 1; */
            i_level = (i_level - 1) | 1;
        }

Stéphane Borel's avatar
 
Stéphane Borel committed
731
        p_mb->ppi_blocks[i_b][i_pos] = b_sign ? -i_level : i_level;
732
    }
Stéphane Borel's avatar
 
Stéphane Borel committed
733 734 735

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

/*****************************************************************************
739
 * DecodeMPEG1Intra : decode MPEG-1 intra blocks
740
 *****************************************************************************/
Sam Hocevar's avatar
Sam Hocevar committed
741
static __inline__ void DecodeMPEG1Intra( vpar_thread_t * p_vpar,
742 743
                                         macroblock_t * p_mb, int i_b ,
                                         int i_chroma_format )
744
{
Stéphane Borel's avatar
 
Stéphane Borel committed
745 746 747 748 749 750 751 752 753 754 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 789 790 791 792 793
    int             i_parse;
    int             i_nc;
    int             i_cc;
    int             i_coef;
    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_sign;
    boolean_t       b_chroma;
    
    /* Give the chromatic component (0, 1, 2) */
    i_cc = pi_cc_index[i_b];

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

    /* decode length */
    i_code = ShowBits(&p_vpar->bit_stream, 5);

    if (i_code<31)
    {
        i_dct_dc_size = ppl_dct_dc_init_table_1[b_chroma][i_code].i_value;
        i_length = ppl_dct_dc_init_table_1[b_chroma][i_code].i_length;
        RemoveBits( &p_vpar->bit_stream, i_length);
    }
    else
    {
        i_code = ShowBits(&p_vpar->bit_stream, (9+b_chroma)) - (0x1f0 * (b_chroma + 1));
        i_dct_dc_size = ppl_dct_dc_init_table_2[b_chroma][i_code].i_value;
        i_length = ppl_dct_dc_init_table_2[b_chroma][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;
    }

    /* Read the actual code with the good length */
    p_vpar->mb.pi_dc_dct_pred[i_cc] += i_dct_dc_diff;

794
    p_mb->ppi_blocks[i_b][0] = p_vpar->mb.pi_dc_dct_pred[i_cc] << 3;
Stéphane Borel's avatar
 
Stéphane Borel committed
795 796 797

    i_nc = ( p_vpar->mb.pi_dc_dct_pred[i_cc] != 0 );

798 799

    if( p_vpar->picture.i_coding_type == D_CODING_TYPE )
800
    {
801 802 803
        /* 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 );
Stéphane Borel's avatar
 
Stéphane Borel committed
804 805
        /* D pictures do not have AC coefficients */
        return;
806
    }
Stéphane Borel's avatar
 
Stéphane Borel committed
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842

    
    /* Decoding of the AC coefficients */
    i_coef = 0;
    b_sign = 0;

    for( i_parse = 1; !p_vpar->b_die/*i_parse < 64*/; i_parse++ )
    {
        i_code = ShowBits( &p_vpar->bit_stream, 16 );
        /* We use 2 main tables for the coefficients */
        if( i_code >= 16384 )
        {
            i_run =     ppl_dct_tab1[0][(i_code>>12)-4].i_run;
            i_level =   ppl_dct_tab1[0][(i_code>>12)-4].i_level;
            i_length =  ppl_dct_tab1[0][(i_code>>12)-4].i_length;
        }
        else
        {
            i_run =     p_vpar->ppl_dct_coef[0][i_code].i_run;
            i_length =  p_vpar->ppl_dct_coef[0][i_code].i_length;
            i_level =   p_vpar->ppl_dct_coef[0][i_code].i_level;
        }

        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, 8 );
                if (i_level == 0)
                    i_level = GetBits( &p_vpar->bit_stream, 8 );
                else if (i_level == 128)
                    i_level = GetBits( &p_vpar->bit_stream, 8 ) - 256;
                else if (i_level > 128)
                    i_level -= 256;
843 844
                if( (b_sign = i_level < 0) )
                    i_level = -i_level;
Stéphane Borel's avatar
 
Stéphane Borel committed
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
                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 );
        }
        
        /* Prepare the next block */
        i_coef = i_parse;
        i_parse += i_run;
        i_nc ++;

        if( i_parse >= 64 )
        {
            /* We have an error in the stream */
            break;
        }

        /* Determine the position of the block in the frame */
        i_pos = pi_scan[p_vpar->picture.b_alternate_scan][i_parse];
        i_level = ( i_level *
                    p_vpar->mb.i_quantizer_scale *
                    p_vpar->sequence.intra_quant.pi_matrix[i_pos] ) >> 3;
879 880 881 882 883 884 885 886

        /* Mismatch control */
        if( i_level ) /* Should always be true */
        {
            /* Equivalent to : if( (val & 1) == 0 ) val = val - 1; */
            i_level = (i_level - 1) | 1;
        }

Stéphane Borel's avatar
 
Stéphane Borel committed
887 888 889 890 891
        p_mb->ppi_blocks[i_b][i_pos] = b_sign ? -i_level : i_level;
    }

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

/*****************************************************************************
895
 * DecodeMPEG2NonIntra : decode MPEG-2 non-intra blocks
896
 *****************************************************************************/
Sam Hocevar's avatar
Sam Hocevar committed
897
static __inline__ void DecodeMPEG2NonIntra( vpar_thread_t * p_vpar,
898 899
                                            macroblock_t * p_mb, int i_b,
                                            int i_chroma_format )
900
{
901 902 903 904 905 906 907 908 909
    int         i_parse;
    int         i_nc;
    int         i_cc;
    int         i_coef;
    int         i_code;
    int         i_length;
    int         i_pos;
    int         i_run;
    int         i_level;
Stéphane Borel's avatar
 
Stéphane Borel committed
910
    boolean_t   b_dc;
911
    boolean_t   b_sign;
Stéphane Borel's avatar
 
Stéphane Borel committed
912
    boolean_t   b_chroma;
913
    int *       pi_quant;
914

Stéphane Borel's avatar
 
Stéphane Borel committed
915
    /* Give the chromatic component (0, 1, 2) */
916 917 918
    i_cc = pi_cc_index[i_b];

    /* Determine whether it is luminance or not (chrominance) */
Stéphane Borel's avatar
 
Stéphane Borel committed
919
    b_chroma = ( i_cc + 1 ) >> 1;
920

921
    /* Give a pointer to the quantization matrices for intra blocks */
Stéphane Borel's avatar
 
Stéphane Borel committed
922
    if( (i_chroma_format == CHROMA_420) || (!b_chroma) )
923 924 925 926 927 928 929
    {
        pi_quant = p_vpar->sequence.nonintra_quant.pi_matrix;
    }
    else
    {
        pi_quant = p_vpar->sequence.chroma_nonintra_quant.pi_matrix;
    }
930 931

    /* Decoding of the AC coefficients */
932

933 934
    i_nc = 0;
    i_coef = 0;
Michel Kaempf's avatar
Michel Kaempf committed
935
    for( i_parse = 0; !p_vpar->b_die; i_parse++ )
936
    {
937 938
        i_code = ShowBits( &p_vpar->bit_stream, 16 );
        if( i_code >= 16384 )
939
        {
Stéphane Borel's avatar
 
Stéphane Borel committed
940
            b_dc = (i_parse == 0);
Stéphane Borel's avatar
 
Stéphane Borel committed
941 942 943
            i_run =     ppl_dct_tab2[b_dc][(i_code>>12)-4].i_run;
            i_level =   ppl_dct_tab2[b_dc][(i_code>>12)-4].i_level;
            i_length =  ppl_dct_tab2[b_dc][(i_code>>12)-4].i_length;
944 945 946
        }
        else
        {
947 948 949
            i_run =     p_vpar->ppl_dct_coef[0][i_code].i_run;
            i_length =  p_vpar->ppl_dct_coef[0][i_code].i_length;
            i_level =   p_vpar->ppl_dct_coef[0][i_code].i_level;
950 951
        }

952

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

955 956 957 958 959 960 961 962 963
        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:
964 965 966 967 968
#ifdef HAVE_MMX
                /* The MMX IDCT has a precision problem with non-intra
                 * blocks. */
                p_mb->ppi_blocks[i_b][0] += 4;
#endif
969 970 971 972 973 974 975 976 977 978
                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;
979

980 981 982 983 984 985 986 987 988
                break;
            default:
                b_sign = GetBits( &p_vpar->bit_stream, 1 );
        }
        i_coef = i_parse;
        i_parse += i_run;
        i_nc ++;

        if( i_parse >= 64 )
989
        {
990
            break;
991
        }
992

993
        i_pos = pi_scan[p_vpar->picture.b_alternate_scan][i_parse];
994
        i_level = ( ((i_level << 1) + 1) * p_vpar->mb.i_quantizer_scale
995
                    * pi_quant[i_pos] ) >> 5;
996
        p_mb->ppi_blocks[i_b][i_pos] = b_sign ? -i_level : i_level;
997
    }
998 999 1000

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

1003
/*****************************************************************************
1004
 * DecodeMPEG2Intra : decode MPEG-2 intra blocks
1005
 *****************************************************************************/
Sam Hocevar's avatar
Sam Hocevar committed
1006
static __inline__ void DecodeMPEG2Intra( vpar_thread_t * p_vpar,
1007 1008
                                         macroblock_t * p_mb, int i_b,
                                         int i_chroma_format )
1009
{
Stéphane Borel's avatar
 
Stéphane Borel committed
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020