vl_mpeg12_bitstream.c revision c888fe027c338f337123de4da2de1ac73b0f7587
1/************************************************************************** 2 * 3 * Copyright 2011 Christian König. 4 * All Rights Reserved. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the 8 * "Software"), to deal in the Software without restriction, including 9 * without limitation the rights to use, copy, modify, merge, publish, 10 * distribute, sub license, and/or sell copies of the Software, and to 11 * permit persons to whom the Software is furnished to do so, subject to 12 * the following conditions: 13 * 14 * The above copyright notice and this permission notice (including the 15 * next paragraph) shall be included in all copies or substantial portions 16 * of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR 22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 * 26 **************************************************************************/ 27 28/** 29 * This file is based uppon slice_xvmc.c and vlc.h from the xine project, 30 * which in turn is based on mpeg2dec. The following is the original copyright: 31 * 32 * Copyright (C) 2000-2002 Michel Lespinasse <walken@zoy.org> 33 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca> 34 * 35 * This file is part of mpeg2dec, a free MPEG-2 video stream decoder. 36 * See http://libmpeg2.sourceforge.net/ for updates. 37 * 38 * mpeg2dec is free software; you can redistribute it and/or modify 39 * it under the terms of the GNU General Public License as published by 40 * the Free Software Foundation; either version 2 of the License, or 41 * (at your option) any later version. 42 * 43 * mpeg2dec is distributed in the hope that it will be useful, 44 * but WITHOUT ANY WARRANTY; without even the implied warranty of 45 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 46 * GNU General Public License for more details. 47 * 48 * You should have received a copy of the GNU General Public License 49 * along with this program; if not, write to the Free Software 50 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 51 */ 52 53#include <stdint.h> 54 55#include <pipe/p_video_state.h> 56 57#include "vl_vlc.h" 58#include "vl_mpeg12_bitstream.h" 59 60/* take num bits from the high part of bit_buf and zero extend them */ 61#define UBITS(buf,num) (((uint32_t)(buf)) >> (32 - (num))) 62 63/* take num bits from the high part of bit_buf and sign extend them */ 64#define SBITS(buf,num) (((int32_t)(buf)) >> (32 - (num))) 65 66#define SATURATE(val) \ 67do { \ 68 if ((uint32_t)(val + 2048) > 4095) \ 69 val = (val > 0) ? 2047 : -2048; \ 70} while (0) 71 72/* macroblock modes */ 73#define MACROBLOCK_INTRA 1 74#define MACROBLOCK_PATTERN 2 75#define MACROBLOCK_MOTION_BACKWARD 4 76#define MACROBLOCK_MOTION_FORWARD 8 77#define MACROBLOCK_QUANT 16 78#define DCT_TYPE_INTERLACED 32 79 80/* motion_type */ 81#define MOTION_TYPE_MASK (3*64) 82#define MOTION_TYPE_BASE 64 83#define MC_FIELD (1*64) 84#define MC_FRAME (2*64) 85#define MC_16X8 (2*64) 86#define MC_DMV (3*64) 87 88/* picture structure */ 89#define TOP_FIELD 1 90#define BOTTOM_FIELD 2 91#define FRAME_PICTURE 3 92 93/* picture coding type (mpeg2 header) */ 94#define I_TYPE 1 95#define P_TYPE 2 96#define B_TYPE 3 97#define D_TYPE 4 98 99typedef struct { 100 uint8_t modes; 101 uint8_t len; 102} MBtab; 103 104typedef struct { 105 uint8_t delta; 106 uint8_t len; 107} MVtab; 108 109typedef struct { 110 int8_t dmv; 111 uint8_t len; 112} DMVtab; 113 114typedef struct { 115 uint8_t cbp; 116 uint8_t len; 117} CBPtab; 118 119typedef struct { 120 uint8_t size; 121 uint8_t len; 122} DCtab; 123 124typedef struct { 125 uint8_t run; 126 uint8_t level; 127 uint8_t len; 128} DCTtab; 129 130typedef struct { 131 uint8_t mba; 132 uint8_t len; 133} MBAtab; 134 135#define INTRA MACROBLOCK_INTRA 136#define QUANT MACROBLOCK_QUANT 137#define MC MACROBLOCK_MOTION_FORWARD 138#define CODED MACROBLOCK_PATTERN 139#define FWD MACROBLOCK_MOTION_FORWARD 140#define BWD MACROBLOCK_MOTION_BACKWARD 141#define INTER MACROBLOCK_MOTION_FORWARD|MACROBLOCK_MOTION_BACKWARD 142 143static const MBtab MB_I [] = { 144 {INTRA|QUANT, 2}, {INTRA, 1} 145}; 146 147static const MBtab MB_P [] = { 148 {INTRA|QUANT, 6}, {CODED|QUANT, 5}, {MC|CODED|QUANT, 5}, {INTRA, 5}, 149 {MC, 3}, {MC, 3}, {MC, 3}, {MC, 3}, 150 {CODED, 2}, {CODED, 2}, {CODED, 2}, {CODED, 2}, 151 {CODED, 2}, {CODED, 2}, {CODED, 2}, {CODED, 2}, 152 {MC|CODED, 1}, {MC|CODED, 1}, {MC|CODED, 1}, {MC|CODED, 1}, 153 {MC|CODED, 1}, {MC|CODED, 1}, {MC|CODED, 1}, {MC|CODED, 1}, 154 {MC|CODED, 1}, {MC|CODED, 1}, {MC|CODED, 1}, {MC|CODED, 1}, 155 {MC|CODED, 1}, {MC|CODED, 1}, {MC|CODED, 1}, {MC|CODED, 1} 156}; 157 158static const MBtab MB_B [] = { 159 {0, 0}, {INTRA|QUANT, 6}, 160 {BWD|CODED|QUANT, 6}, {FWD|CODED|QUANT, 6}, 161 {INTER|CODED|QUANT, 5}, {INTER|CODED|QUANT, 5}, 162 {INTRA, 5}, {INTRA, 5}, 163 {FWD, 4}, {FWD, 4}, {FWD, 4}, {FWD, 4}, 164 {FWD|CODED, 4}, {FWD|CODED, 4}, {FWD|CODED, 4}, {FWD|CODED, 4}, 165 {BWD, 3}, {BWD, 3}, {BWD, 3}, {BWD, 3}, 166 {BWD, 3}, {BWD, 3}, {BWD, 3}, {BWD, 3}, 167 {BWD|CODED, 3}, {BWD|CODED, 3}, {BWD|CODED, 3}, {BWD|CODED, 3}, 168 {BWD|CODED, 3}, {BWD|CODED, 3}, {BWD|CODED, 3}, {BWD|CODED, 3}, 169 {INTER, 2}, {INTER, 2}, {INTER, 2}, {INTER, 2}, 170 {INTER, 2}, {INTER, 2}, {INTER, 2}, {INTER, 2}, 171 {INTER, 2}, {INTER, 2}, {INTER, 2}, {INTER, 2}, 172 {INTER, 2}, {INTER, 2}, {INTER, 2}, {INTER, 2}, 173 {INTER|CODED, 2}, {INTER|CODED, 2}, {INTER|CODED, 2}, {INTER|CODED, 2}, 174 {INTER|CODED, 2}, {INTER|CODED, 2}, {INTER|CODED, 2}, {INTER|CODED, 2}, 175 {INTER|CODED, 2}, {INTER|CODED, 2}, {INTER|CODED, 2}, {INTER|CODED, 2}, 176 {INTER|CODED, 2}, {INTER|CODED, 2}, {INTER|CODED, 2}, {INTER|CODED, 2} 177}; 178 179#undef INTRA 180#undef QUANT 181#undef MC 182#undef CODED 183#undef FWD 184#undef BWD 185#undef INTER 186 187static const MVtab MV_4 [] = { 188 { 3, 6}, { 2, 4}, { 1, 3}, { 1, 3}, { 0, 2}, { 0, 2}, { 0, 2}, { 0, 2} 189}; 190 191static const MVtab MV_10 [] = { 192 { 0,10}, { 0,10}, { 0,10}, { 0,10}, { 0,10}, { 0,10}, { 0,10}, { 0,10}, 193 { 0,10}, { 0,10}, { 0,10}, { 0,10}, {15,10}, {14,10}, {13,10}, {12,10}, 194 {11,10}, {10,10}, { 9, 9}, { 9, 9}, { 8, 9}, { 8, 9}, { 7, 9}, { 7, 9}, 195 { 6, 7}, { 6, 7}, { 6, 7}, { 6, 7}, { 6, 7}, { 6, 7}, { 6, 7}, { 6, 7}, 196 { 5, 7}, { 5, 7}, { 5, 7}, { 5, 7}, { 5, 7}, { 5, 7}, { 5, 7}, { 5, 7}, 197 { 4, 7}, { 4, 7}, { 4, 7}, { 4, 7}, { 4, 7}, { 4, 7}, { 4, 7}, { 4, 7} 198}; 199 200static const DMVtab DMV_2 [] = { 201 { 0, 1}, { 0, 1}, { 1, 2}, {-1, 2} 202}; 203 204static const CBPtab CBP_7 [] = { 205 {0x22, 7}, {0x12, 7}, {0x0a, 7}, {0x06, 7}, 206 {0x21, 7}, {0x11, 7}, {0x09, 7}, {0x05, 7}, 207 {0x3f, 6}, {0x3f, 6}, {0x03, 6}, {0x03, 6}, 208 {0x24, 6}, {0x24, 6}, {0x18, 6}, {0x18, 6}, 209 {0x3e, 5}, {0x3e, 5}, {0x3e, 5}, {0x3e, 5}, 210 {0x02, 5}, {0x02, 5}, {0x02, 5}, {0x02, 5}, 211 {0x3d, 5}, {0x3d, 5}, {0x3d, 5}, {0x3d, 5}, 212 {0x01, 5}, {0x01, 5}, {0x01, 5}, {0x01, 5}, 213 {0x38, 5}, {0x38, 5}, {0x38, 5}, {0x38, 5}, 214 {0x34, 5}, {0x34, 5}, {0x34, 5}, {0x34, 5}, 215 {0x2c, 5}, {0x2c, 5}, {0x2c, 5}, {0x2c, 5}, 216 {0x1c, 5}, {0x1c, 5}, {0x1c, 5}, {0x1c, 5}, 217 {0x28, 5}, {0x28, 5}, {0x28, 5}, {0x28, 5}, 218 {0x14, 5}, {0x14, 5}, {0x14, 5}, {0x14, 5}, 219 {0x30, 5}, {0x30, 5}, {0x30, 5}, {0x30, 5}, 220 {0x0c, 5}, {0x0c, 5}, {0x0c, 5}, {0x0c, 5}, 221 {0x20, 4}, {0x20, 4}, {0x20, 4}, {0x20, 4}, 222 {0x20, 4}, {0x20, 4}, {0x20, 4}, {0x20, 4}, 223 {0x10, 4}, {0x10, 4}, {0x10, 4}, {0x10, 4}, 224 {0x10, 4}, {0x10, 4}, {0x10, 4}, {0x10, 4}, 225 {0x08, 4}, {0x08, 4}, {0x08, 4}, {0x08, 4}, 226 {0x08, 4}, {0x08, 4}, {0x08, 4}, {0x08, 4}, 227 {0x04, 4}, {0x04, 4}, {0x04, 4}, {0x04, 4}, 228 {0x04, 4}, {0x04, 4}, {0x04, 4}, {0x04, 4}, 229 {0x3c, 3}, {0x3c, 3}, {0x3c, 3}, {0x3c, 3}, 230 {0x3c, 3}, {0x3c, 3}, {0x3c, 3}, {0x3c, 3}, 231 {0x3c, 3}, {0x3c, 3}, {0x3c, 3}, {0x3c, 3}, 232 {0x3c, 3}, {0x3c, 3}, {0x3c, 3}, {0x3c, 3} 233}; 234 235static const CBPtab CBP_9 [] = { 236 {0, 0}, {0x00, 9}, {0x27, 9}, {0x1b, 9}, 237 {0x3b, 9}, {0x37, 9}, {0x2f, 9}, {0x1f, 9}, 238 {0x3a, 8}, {0x3a, 8}, {0x36, 8}, {0x36, 8}, 239 {0x2e, 8}, {0x2e, 8}, {0x1e, 8}, {0x1e, 8}, 240 {0x39, 8}, {0x39, 8}, {0x35, 8}, {0x35, 8}, 241 {0x2d, 8}, {0x2d, 8}, {0x1d, 8}, {0x1d, 8}, 242 {0x26, 8}, {0x26, 8}, {0x1a, 8}, {0x1a, 8}, 243 {0x25, 8}, {0x25, 8}, {0x19, 8}, {0x19, 8}, 244 {0x2b, 8}, {0x2b, 8}, {0x17, 8}, {0x17, 8}, 245 {0x33, 8}, {0x33, 8}, {0x0f, 8}, {0x0f, 8}, 246 {0x2a, 8}, {0x2a, 8}, {0x16, 8}, {0x16, 8}, 247 {0x32, 8}, {0x32, 8}, {0x0e, 8}, {0x0e, 8}, 248 {0x29, 8}, {0x29, 8}, {0x15, 8}, {0x15, 8}, 249 {0x31, 8}, {0x31, 8}, {0x0d, 8}, {0x0d, 8}, 250 {0x23, 8}, {0x23, 8}, {0x13, 8}, {0x13, 8}, 251 {0x0b, 8}, {0x0b, 8}, {0x07, 8}, {0x07, 8} 252}; 253 254static const DCtab DC_lum_5 [] = { 255 {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, 256 {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, 257 {0, 3}, {0, 3}, {0, 3}, {0, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, 258 {4, 3}, {4, 3}, {4, 3}, {4, 3}, {5, 4}, {5, 4}, {6, 5} 259}; 260 261static const DCtab DC_chrom_5 [] = { 262 {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, 263 {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, 264 {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, 265 {3, 3}, {3, 3}, {3, 3}, {3, 3}, {4, 4}, {4, 4}, {5, 5} 266}; 267 268static const DCtab DC_long [] = { 269 {6, 5}, {6, 5}, {6, 5}, {6, 5}, {6, 5}, {6, 5}, { 6, 5}, { 6, 5}, 270 {6, 5}, {6, 5}, {6, 5}, {6, 5}, {6, 5}, {6, 5}, { 6, 5}, { 6, 5}, 271 {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, { 7, 6}, { 7, 6}, 272 {8, 7}, {8, 7}, {8, 7}, {8, 7}, {9, 8}, {9, 8}, {10, 9}, {11, 9} 273}; 274 275static const DCTtab DCT_16 [] = { 276 {129, 0, 0}, {129, 0, 0}, {129, 0, 0}, {129, 0, 0}, 277 {129, 0, 0}, {129, 0, 0}, {129, 0, 0}, {129, 0, 0}, 278 {129, 0, 0}, {129, 0, 0}, {129, 0, 0}, {129, 0, 0}, 279 {129, 0, 0}, {129, 0, 0}, {129, 0, 0}, {129, 0, 0}, 280 { 2,18, 0}, { 2,17, 0}, { 2,16, 0}, { 2,15, 0}, 281 { 7, 3, 0}, { 17, 2, 0}, { 16, 2, 0}, { 15, 2, 0}, 282 { 14, 2, 0}, { 13, 2, 0}, { 12, 2, 0}, { 32, 1, 0}, 283 { 31, 1, 0}, { 30, 1, 0}, { 29, 1, 0}, { 28, 1, 0} 284}; 285 286static const DCTtab DCT_15 [] = { 287 { 1,40,15}, { 1,39,15}, { 1,38,15}, { 1,37,15}, 288 { 1,36,15}, { 1,35,15}, { 1,34,15}, { 1,33,15}, 289 { 1,32,15}, { 2,14,15}, { 2,13,15}, { 2,12,15}, 290 { 2,11,15}, { 2,10,15}, { 2, 9,15}, { 2, 8,15}, 291 { 1,31,14}, { 1,31,14}, { 1,30,14}, { 1,30,14}, 292 { 1,29,14}, { 1,29,14}, { 1,28,14}, { 1,28,14}, 293 { 1,27,14}, { 1,27,14}, { 1,26,14}, { 1,26,14}, 294 { 1,25,14}, { 1,25,14}, { 1,24,14}, { 1,24,14}, 295 { 1,23,14}, { 1,23,14}, { 1,22,14}, { 1,22,14}, 296 { 1,21,14}, { 1,21,14}, { 1,20,14}, { 1,20,14}, 297 { 1,19,14}, { 1,19,14}, { 1,18,14}, { 1,18,14}, 298 { 1,17,14}, { 1,17,14}, { 1,16,14}, { 1,16,14} 299}; 300 301static const DCTtab DCT_13 [] = { 302 { 11, 2,13}, { 10, 2,13}, { 6, 3,13}, { 4, 4,13}, 303 { 3, 5,13}, { 2, 7,13}, { 2, 6,13}, { 1,15,13}, 304 { 1,14,13}, { 1,13,13}, { 1,12,13}, { 27, 1,13}, 305 { 26, 1,13}, { 25, 1,13}, { 24, 1,13}, { 23, 1,13}, 306 { 1,11,12}, { 1,11,12}, { 9, 2,12}, { 9, 2,12}, 307 { 5, 3,12}, { 5, 3,12}, { 1,10,12}, { 1,10,12}, 308 { 3, 4,12}, { 3, 4,12}, { 8, 2,12}, { 8, 2,12}, 309 { 22, 1,12}, { 22, 1,12}, { 21, 1,12}, { 21, 1,12}, 310 { 1, 9,12}, { 1, 9,12}, { 20, 1,12}, { 20, 1,12}, 311 { 19, 1,12}, { 19, 1,12}, { 2, 5,12}, { 2, 5,12}, 312 { 4, 3,12}, { 4, 3,12}, { 1, 8,12}, { 1, 8,12}, 313 { 7, 2,12}, { 7, 2,12}, { 18, 1,12}, { 18, 1,12} 314}; 315 316static const DCTtab DCT_B14_10 [] = { 317 { 17, 1,10}, { 6, 2,10}, { 1, 7,10}, { 3, 3,10}, 318 { 2, 4,10}, { 16, 1,10}, { 15, 1,10}, { 5, 2,10} 319}; 320 321static const DCTtab DCT_B14_8 [] = { 322 { 65, 0, 6}, { 65, 0, 6}, { 65, 0, 6}, { 65, 0, 6}, 323 { 3, 2, 7}, { 3, 2, 7}, { 10, 1, 7}, { 10, 1, 7}, 324 { 1, 4, 7}, { 1, 4, 7}, { 9, 1, 7}, { 9, 1, 7}, 325 { 8, 1, 6}, { 8, 1, 6}, { 8, 1, 6}, { 8, 1, 6}, 326 { 7, 1, 6}, { 7, 1, 6}, { 7, 1, 6}, { 7, 1, 6}, 327 { 2, 2, 6}, { 2, 2, 6}, { 2, 2, 6}, { 2, 2, 6}, 328 { 6, 1, 6}, { 6, 1, 6}, { 6, 1, 6}, { 6, 1, 6}, 329 { 14, 1, 8}, { 1, 6, 8}, { 13, 1, 8}, { 12, 1, 8}, 330 { 4, 2, 8}, { 2, 3, 8}, { 1, 5, 8}, { 11, 1, 8} 331}; 332 333static const DCTtab DCT_B14AC_5 [] = { 334 { 1, 3, 5}, { 5, 1, 5}, { 4, 1, 5}, 335 { 1, 2, 4}, { 1, 2, 4}, { 3, 1, 4}, { 3, 1, 4}, 336 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, 337 {129, 0, 2}, {129, 0, 2}, {129, 0, 2}, {129, 0, 2}, 338 {129, 0, 2}, {129, 0, 2}, {129, 0, 2}, {129, 0, 2}, 339 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, 340 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2} 341}; 342 343static const DCTtab DCT_B14DC_5 [] = { 344 { 1, 3, 5}, { 5, 1, 5}, { 4, 1, 5}, 345 { 1, 2, 4}, { 1, 2, 4}, { 3, 1, 4}, { 3, 1, 4}, 346 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, 347 { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1}, 348 { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1}, 349 { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1}, 350 { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1} 351}; 352 353static const DCTtab DCT_B15_10 [] = { 354 { 6, 2, 9}, { 6, 2, 9}, { 15, 1, 9}, { 15, 1, 9}, 355 { 3, 4,10}, { 17, 1,10}, { 16, 1, 9}, { 16, 1, 9} 356}; 357 358static const DCTtab DCT_B15_8 [] = { 359 { 65, 0, 6}, { 65, 0, 6}, { 65, 0, 6}, { 65, 0, 6}, 360 { 8, 1, 7}, { 8, 1, 7}, { 9, 1, 7}, { 9, 1, 7}, 361 { 7, 1, 7}, { 7, 1, 7}, { 3, 2, 7}, { 3, 2, 7}, 362 { 1, 7, 6}, { 1, 7, 6}, { 1, 7, 6}, { 1, 7, 6}, 363 { 1, 6, 6}, { 1, 6, 6}, { 1, 6, 6}, { 1, 6, 6}, 364 { 5, 1, 6}, { 5, 1, 6}, { 5, 1, 6}, { 5, 1, 6}, 365 { 6, 1, 6}, { 6, 1, 6}, { 6, 1, 6}, { 6, 1, 6}, 366 { 2, 5, 8}, { 12, 1, 8}, { 1,11, 8}, { 1,10, 8}, 367 { 14, 1, 8}, { 13, 1, 8}, { 4, 2, 8}, { 2, 4, 8}, 368 { 3, 1, 5}, { 3, 1, 5}, { 3, 1, 5}, { 3, 1, 5}, 369 { 3, 1, 5}, { 3, 1, 5}, { 3, 1, 5}, { 3, 1, 5}, 370 { 2, 2, 5}, { 2, 2, 5}, { 2, 2, 5}, { 2, 2, 5}, 371 { 2, 2, 5}, { 2, 2, 5}, { 2, 2, 5}, { 2, 2, 5}, 372 { 4, 1, 5}, { 4, 1, 5}, { 4, 1, 5}, { 4, 1, 5}, 373 { 4, 1, 5}, { 4, 1, 5}, { 4, 1, 5}, { 4, 1, 5}, 374 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, 375 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, 376 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, 377 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, 378 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, 379 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, 380 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, 381 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, 382 {129, 0, 4}, {129, 0, 4}, {129, 0, 4}, {129, 0, 4}, 383 {129, 0, 4}, {129, 0, 4}, {129, 0, 4}, {129, 0, 4}, 384 {129, 0, 4}, {129, 0, 4}, {129, 0, 4}, {129, 0, 4}, 385 {129, 0, 4}, {129, 0, 4}, {129, 0, 4}, {129, 0, 4}, 386 { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4}, 387 { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4}, 388 { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4}, 389 { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4}, 390 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, 391 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, 392 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, 393 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, 394 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, 395 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, 396 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, 397 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, 398 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, 399 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, 400 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, 401 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, 402 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, 403 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, 404 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, 405 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, 406 { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, 407 { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, 408 { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, 409 { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, 410 { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, 411 { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, 412 { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, 413 { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, 414 { 1, 4, 5}, { 1, 4, 5}, { 1, 4, 5}, { 1, 4, 5}, 415 { 1, 4, 5}, { 1, 4, 5}, { 1, 4, 5}, { 1, 4, 5}, 416 { 1, 5, 5}, { 1, 5, 5}, { 1, 5, 5}, { 1, 5, 5}, 417 { 1, 5, 5}, { 1, 5, 5}, { 1, 5, 5}, { 1, 5, 5}, 418 { 10, 1, 7}, { 10, 1, 7}, { 2, 3, 7}, { 2, 3, 7}, 419 { 11, 1, 7}, { 11, 1, 7}, { 1, 8, 7}, { 1, 8, 7}, 420 { 1, 9, 7}, { 1, 9, 7}, { 1,12, 8}, { 1,13, 8}, 421 { 3, 3, 8}, { 5, 2, 8}, { 1,14, 8}, { 1,15, 8} 422}; 423 424static const MBAtab MBA_5 [] = { 425 {6, 5}, {5, 5}, {4, 4}, {4, 4}, {3, 4}, {3, 4}, 426 {2, 3}, {2, 3}, {2, 3}, {2, 3}, {1, 3}, {1, 3}, {1, 3}, {1, 3}, 427 {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, 428 {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1} 429}; 430 431static const MBAtab MBA_11 [] = { 432 {32, 11}, {31, 11}, {30, 11}, {29, 11}, 433 {28, 11}, {27, 11}, {26, 11}, {25, 11}, 434 {24, 11}, {23, 11}, {22, 11}, {21, 11}, 435 {20, 10}, {20, 10}, {19, 10}, {19, 10}, 436 {18, 10}, {18, 10}, {17, 10}, {17, 10}, 437 {16, 10}, {16, 10}, {15, 10}, {15, 10}, 438 {14, 8}, {14, 8}, {14, 8}, {14, 8}, 439 {14, 8}, {14, 8}, {14, 8}, {14, 8}, 440 {13, 8}, {13, 8}, {13, 8}, {13, 8}, 441 {13, 8}, {13, 8}, {13, 8}, {13, 8}, 442 {12, 8}, {12, 8}, {12, 8}, {12, 8}, 443 {12, 8}, {12, 8}, {12, 8}, {12, 8}, 444 {11, 8}, {11, 8}, {11, 8}, {11, 8}, 445 {11, 8}, {11, 8}, {11, 8}, {11, 8}, 446 {10, 8}, {10, 8}, {10, 8}, {10, 8}, 447 {10, 8}, {10, 8}, {10, 8}, {10, 8}, 448 { 9, 8}, { 9, 8}, { 9, 8}, { 9, 8}, 449 { 9, 8}, { 9, 8}, { 9, 8}, { 9, 8}, 450 { 8, 7}, { 8, 7}, { 8, 7}, { 8, 7}, 451 { 8, 7}, { 8, 7}, { 8, 7}, { 8, 7}, 452 { 8, 7}, { 8, 7}, { 8, 7}, { 8, 7}, 453 { 8, 7}, { 8, 7}, { 8, 7}, { 8, 7}, 454 { 7, 7}, { 7, 7}, { 7, 7}, { 7, 7}, 455 { 7, 7}, { 7, 7}, { 7, 7}, { 7, 7}, 456 { 7, 7}, { 7, 7}, { 7, 7}, { 7, 7}, 457 { 7, 7}, { 7, 7}, { 7, 7}, { 7, 7} 458}; 459 460/* original (non-patched) scan tables */ 461static const uint8_t mpeg2_scan_norm_orig[64] = 462{ 463 /* Zig-Zag scan pattern */ 464 0, 1, 8,16, 9, 2, 3,10, 465 17,24,32,25,18,11, 4, 5, 466 12,19,26,33,40,48,41,34, 467 27,20,13, 6, 7,14,21,28, 468 35,42,49,56,57,50,43,36, 469 29,22,15,23,30,37,44,51, 470 58,59,52,45,38,31,39,46, 471 53,60,61,54,47,55,62,63 472}; 473 474static const uint8_t mpeg2_scan_alt_orig[64] = 475{ 476 /* Alternate scan pattern */ 477 0,8,16,24,1,9,2,10,17,25,32,40,48,56,57,49, 478 41,33,26,18,3,11,4,12,19,27,34,42,50,58,35,43, 479 51,59,20,28,5,13,6,14,21,29,36,44,52,60,37,45, 480 53,61,22,30,7,15,23,31,38,46,54,62,39,47,55,63 481}; 482 483static uint8_t mpeg2_scan_alt_ptable[64]; 484static uint8_t mpeg2_scan_norm_ptable[64]; 485static uint8_t mpeg2_scan_orig_ptable[64]; 486 487static inline void 488setup_scan_ptable( void ) 489{ 490 int i; 491 for (i=0; i<64; ++i) { 492 mpeg2_scan_norm_ptable[mpeg2_scan_norm_orig[i]] = mpeg2_scan_norm_orig[i]; 493 mpeg2_scan_alt_ptable[mpeg2_scan_alt_orig[i]] = mpeg2_scan_alt_orig[i]; 494 mpeg2_scan_orig_ptable[i] = i; 495 } 496} 497 498static const int non_linear_quantizer_scale[] = { 499 0, 1, 2, 3, 4, 5, 6, 7, 500 8, 10, 12, 14, 16, 18, 20, 22, 501 24, 28, 32, 36, 40, 44, 48, 52, 502 56, 64, 72, 80, 88, 96, 104, 112 503}; 504 505static inline int 506get_macroblock_modes(struct vl_mpg12_bs *bs, struct pipe_mpeg12_picture_desc * picture) 507{ 508 int macroblock_modes; 509 const MBtab * tab; 510 511 switch (picture->picture_coding_type) { 512 case I_TYPE: 513 514 tab = MB_I + vl_vlc_ubits(&bs->vlc, 1); 515 vl_vlc_dumpbits(&bs->vlc, tab->len); 516 macroblock_modes = tab->modes; 517 518 if ((!(picture->frame_pred_frame_dct)) && (picture->picture_structure == FRAME_PICTURE)) { 519 macroblock_modes |= vl_vlc_ubits(&bs->vlc, 1) * DCT_TYPE_INTERLACED; 520 vl_vlc_dumpbits(&bs->vlc, 1); 521 } 522 523 return macroblock_modes; 524 525 case P_TYPE: 526 527 tab = MB_P + vl_vlc_ubits(&bs->vlc, 5); 528 vl_vlc_dumpbits(&bs->vlc, tab->len); 529 macroblock_modes = tab->modes; 530 531 if (picture->picture_structure != FRAME_PICTURE) { 532 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD) { 533 macroblock_modes |= vl_vlc_ubits(&bs->vlc, 2) * MOTION_TYPE_BASE; 534 vl_vlc_dumpbits(&bs->vlc, 2); 535 } 536 return macroblock_modes; 537 } else if (picture->frame_pred_frame_dct) { 538 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD) 539 macroblock_modes |= MC_FRAME; 540 return macroblock_modes; 541 } else { 542 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD) { 543 macroblock_modes |= vl_vlc_ubits(&bs->vlc, 2) * MOTION_TYPE_BASE; 544 vl_vlc_dumpbits(&bs->vlc, 2); 545 } 546 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN)) { 547 macroblock_modes |= vl_vlc_ubits(&bs->vlc, 1) * DCT_TYPE_INTERLACED; 548 vl_vlc_dumpbits(&bs->vlc, 1); 549 } 550 return macroblock_modes; 551 } 552 553 case B_TYPE: 554 555 tab = MB_B + vl_vlc_ubits(&bs->vlc, 6); 556 vl_vlc_dumpbits(&bs->vlc, tab->len); 557 macroblock_modes = tab->modes; 558 559 if (picture->picture_structure != FRAME_PICTURE) { 560 if (! (macroblock_modes & MACROBLOCK_INTRA)) { 561 macroblock_modes |= vl_vlc_ubits(&bs->vlc, 2) * MOTION_TYPE_BASE; 562 vl_vlc_dumpbits(&bs->vlc, 2); 563 } 564 return macroblock_modes; 565 } else if (picture->frame_pred_frame_dct) { 566 /* if (! (macroblock_modes & MACROBLOCK_INTRA)) */ 567 macroblock_modes |= MC_FRAME; 568 return macroblock_modes; 569 } else { 570 if (macroblock_modes & MACROBLOCK_INTRA) 571 goto intra; 572 macroblock_modes |= vl_vlc_ubits(&bs->vlc, 2) * MOTION_TYPE_BASE; 573 vl_vlc_dumpbits(&bs->vlc, 2); 574 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN)) { 575 intra: 576 macroblock_modes |= vl_vlc_ubits(&bs->vlc, 1) * DCT_TYPE_INTERLACED; 577 vl_vlc_dumpbits(&bs->vlc, 1); 578 } 579 return macroblock_modes; 580 } 581 582 case D_TYPE: 583 584 vl_vlc_dumpbits(&bs->vlc, 1); 585 return MACROBLOCK_INTRA; 586 587 default: 588 return 0; 589 } 590} 591 592static inline int 593get_quantizer_scale(struct vl_mpg12_bs *bs, struct pipe_mpeg12_picture_desc * picture) 594{ 595 int quantizer_scale_code; 596 597 quantizer_scale_code = vl_vlc_ubits(&bs->vlc, 5); 598 vl_vlc_dumpbits(&bs->vlc, 5); 599 600 if (picture->q_scale_type) 601 return non_linear_quantizer_scale[quantizer_scale_code]; 602 else 603 return quantizer_scale_code << 1; 604} 605 606static inline int 607get_motion_delta(struct vl_mpg12_bs *bs, unsigned f_code) 608{ 609 int delta; 610 int sign; 611 const MVtab * tab; 612 613 if (bs->vlc.buf & 0x80000000) { 614 vl_vlc_dumpbits(&bs->vlc, 1); 615 return 0; 616 } else if (bs->vlc.buf >= 0x0c000000) { 617 618 tab = MV_4 + vl_vlc_ubits(&bs->vlc, 4); 619 delta = (tab->delta << f_code) + 1; 620 bs->vlc.bits += tab->len + f_code + 1; 621 bs->vlc.buf <<= tab->len; 622 623 sign = vl_vlc_sbits(&bs->vlc, 1); 624 bs->vlc.buf <<= 1; 625 626 if (f_code) 627 delta += vl_vlc_ubits(&bs->vlc, f_code); 628 bs->vlc.buf <<= f_code; 629 630 return (delta ^ sign) - sign; 631 632 } else { 633 634 tab = MV_10 + vl_vlc_ubits(&bs->vlc, 10); 635 delta = (tab->delta << f_code) + 1; 636 bs->vlc.bits += tab->len + 1; 637 bs->vlc.buf <<= tab->len; 638 639 sign = vl_vlc_sbits(&bs->vlc, 1); 640 bs->vlc.buf <<= 1; 641 642 if (f_code) { 643 vl_vlc_needbits(&bs->vlc); 644 delta += vl_vlc_ubits(&bs->vlc, f_code); 645 vl_vlc_dumpbits(&bs->vlc, f_code); 646 } 647 648 return (delta ^ sign) - sign; 649 } 650} 651 652static inline int 653bound_motion_vector(int vec, unsigned f_code) 654{ 655#if 1 656 unsigned int limit; 657 int sign; 658 659 limit = 16 << f_code; 660 661 if ((unsigned int)(vec + limit) < 2 * limit) 662 return vec; 663 else { 664 sign = ((int32_t)vec) >> 31; 665 return vec - ((2 * limit) ^ sign) + sign; 666 } 667#else 668 return ((int32_t)vec << (28 - f_code)) >> (28 - f_code); 669#endif 670} 671 672static inline int 673get_dmv(struct vl_mpg12_bs *bs) 674{ 675 const DMVtab * tab; 676 677 tab = DMV_2 + vl_vlc_ubits(&bs->vlc, 2); 678 vl_vlc_dumpbits(&bs->vlc, tab->len); 679 return tab->dmv; 680} 681 682static inline int 683get_coded_block_pattern(struct vl_mpg12_bs *bs) 684{ 685 const CBPtab * tab; 686 687 vl_vlc_needbits(&bs->vlc); 688 689 if (bs->vlc.buf >= 0x20000000) { 690 691 tab = CBP_7 + (vl_vlc_ubits(&bs->vlc, 7) - 16); 692 vl_vlc_dumpbits(&bs->vlc, tab->len); 693 return tab->cbp; 694 695 } else { 696 697 tab = CBP_9 + vl_vlc_ubits(&bs->vlc, 9); 698 vl_vlc_dumpbits(&bs->vlc, tab->len); 699 return tab->cbp; 700 } 701} 702 703static inline int 704get_luma_dc_dct_diff(struct vl_mpg12_bs *bs) 705{ 706 const DCtab * tab; 707 int size; 708 int dc_diff; 709 710 if (bs->vlc.buf < 0xf8000000) { 711 tab = DC_lum_5 + vl_vlc_ubits(&bs->vlc, 5); 712 size = tab->size; 713 if (size) { 714 bs->vlc.bits += tab->len + size; 715 bs->vlc.buf <<= tab->len; 716 dc_diff = vl_vlc_ubits(&bs->vlc, size) - UBITS (SBITS (~bs->vlc.buf, 1), size); 717 bs->vlc.buf <<= size; 718 return dc_diff; 719 } else { 720 vl_vlc_dumpbits(&bs->vlc, 3); 721 return 0; 722 } 723 } else { 724 tab = DC_long + (vl_vlc_ubits(&bs->vlc, 9) - 0x1e0); 725 size = tab->size; 726 vl_vlc_dumpbits(&bs->vlc, tab->len); 727 vl_vlc_needbits(&bs->vlc); 728 dc_diff = vl_vlc_ubits(&bs->vlc, size) - UBITS (SBITS (~bs->vlc.buf, 1), size); 729 vl_vlc_dumpbits(&bs->vlc, size); 730 return dc_diff; 731 } 732} 733 734static inline int 735get_chroma_dc_dct_diff(struct vl_mpg12_bs *bs) 736{ 737 const DCtab * tab; 738 int size; 739 int dc_diff; 740 741 if (bs->vlc.buf < 0xf8000000) { 742 tab = DC_chrom_5 + vl_vlc_ubits(&bs->vlc, 5); 743 size = tab->size; 744 if (size) { 745 bs->vlc.bits += tab->len + size; 746 bs->vlc.buf <<= tab->len; 747 dc_diff = vl_vlc_ubits(&bs->vlc, size) - UBITS (SBITS (~bs->vlc.buf, 1), size); 748 bs->vlc.buf <<= size; 749 return dc_diff; 750 } else { 751 vl_vlc_dumpbits(&bs->vlc, 2); 752 return 0; 753 } 754 } else { 755 tab = DC_long + (vl_vlc_ubits(&bs->vlc, 10) - 0x3e0); 756 size = tab->size; 757 vl_vlc_dumpbits(&bs->vlc, tab->len + 1); 758 vl_vlc_needbits(&bs->vlc); 759 dc_diff = vl_vlc_ubits(&bs->vlc, size) - UBITS (SBITS (~bs->vlc.buf, 1), size); 760 vl_vlc_dumpbits(&bs->vlc, size); 761 return dc_diff; 762 } 763} 764 765static inline void 766get_intra_block_B14(struct vl_mpg12_bs *bs, struct pipe_mpeg12_picture_desc * picture, short *dest) 767{ 768 int i, j, l, val; 769 const uint8_t *scan; 770 uint8_t *scan_ptable; 771 uint8_t *quant_matrix = picture->intra_quantizer_matrix; 772 int quantizer_scale = picture->quantizer_scale; 773 int mismatch; 774 const DCTtab *tab; 775 776 if (!picture->alternate_scan) { 777 scan = mpeg2_scan_norm_orig; 778 scan_ptable = mpeg2_scan_norm_ptable; 779 } else { 780 scan = mpeg2_scan_alt_orig; 781 scan_ptable = mpeg2_scan_alt_ptable; 782 } 783 784 i = 0; 785 mismatch = ~dest[0]; 786 787 vl_vlc_needbits(&bs->vlc); 788 789 while (1) { 790 if (bs->vlc.buf >= 0x28000000) { 791 792 tab = DCT_B14AC_5 + (vl_vlc_ubits(&bs->vlc, 5) - 5); 793 794 i += tab->run; 795 if (i >= 64) 796 break; /* end of block */ 797 798 normal_code: 799 l = scan_ptable[j = scan[i]]; 800 801 bs->vlc.buf <<= tab->len; 802 bs->vlc.bits += tab->len + 1; 803 val = (tab->level * quantizer_scale * quant_matrix[l]) >> 4; 804 805 /* if (bitstream_get (1)) val = -val; */ 806 val = (val ^ vl_vlc_sbits(&bs->vlc, 1)) - vl_vlc_sbits(&bs->vlc, 1); 807 808 SATURATE (val); 809 dest[j] = val; 810 mismatch ^= val; 811 812 bs->vlc.buf <<= 1; 813 vl_vlc_needbits(&bs->vlc); 814 815 continue; 816 817 } else if (bs->vlc.buf >= 0x04000000) { 818 819 tab = DCT_B14_8 + (vl_vlc_ubits(&bs->vlc, 8) - 4); 820 821 i += tab->run; 822 if (i < 64) 823 goto normal_code; 824 825 /* escape code */ 826 827 i += UBITS(bs->vlc.buf << 6, 6) - 64; 828 if (i >= 64) 829 break; /* illegal, check needed to avoid buffer overflow */ 830 831 l = scan_ptable[j = scan[i]]; 832 833 vl_vlc_dumpbits(&bs->vlc, 12); 834 vl_vlc_needbits(&bs->vlc); 835 val = (vl_vlc_sbits(&bs->vlc, 12) * quantizer_scale * quant_matrix[l]) / 16; 836 837 SATURATE (val); 838 dest[j] = val; 839 mismatch ^= val; 840 841 vl_vlc_dumpbits(&bs->vlc, 12); 842 vl_vlc_needbits(&bs->vlc); 843 844 continue; 845 846 } else if (bs->vlc.buf >= 0x02000000) { 847 tab = DCT_B14_10 + (vl_vlc_ubits(&bs->vlc, 10) - 8); 848 i += tab->run; 849 if (i < 64) 850 goto normal_code; 851 } else if (bs->vlc.buf >= 0x00800000) { 852 tab = DCT_13 + (vl_vlc_ubits(&bs->vlc, 13) - 16); 853 i += tab->run; 854 if (i < 64) 855 goto normal_code; 856 } else if (bs->vlc.buf >= 0x00200000) { 857 tab = DCT_15 + (vl_vlc_ubits(&bs->vlc, 15) - 16); 858 i += tab->run; 859 if (i < 64) 860 goto normal_code; 861 } else { 862 tab = DCT_16 + vl_vlc_ubits(&bs->vlc, 16); 863 bs->vlc.buf <<= 16; 864 vl_vlc_getword(&bs->vlc, bs->vlc.bits + 16); 865 i += tab->run; 866 if (i < 64) 867 goto normal_code; 868 } 869 break; /* illegal, check needed to avoid buffer overflow */ 870 } 871 872 dest[63] ^= mismatch & 1; 873 vl_vlc_dumpbits(&bs->vlc, 2); /* dump end of block code */ 874} 875 876static inline void 877get_intra_block_B15(struct vl_mpg12_bs *bs, struct pipe_mpeg12_picture_desc * picture, short *dest) 878{ 879 int i, j, l, val; 880 const uint8_t *scan; 881 uint8_t *scan_ptable; 882 uint8_t *quant_matrix = picture->intra_quantizer_matrix; 883 int quantizer_scale = picture->quantizer_scale; 884 int mismatch; 885 const DCTtab * tab; 886 887 if (!picture->alternate_scan) { 888 scan = mpeg2_scan_norm_orig; 889 scan_ptable = mpeg2_scan_norm_ptable; 890 } else { 891 scan = mpeg2_scan_alt_orig; 892 scan_ptable = mpeg2_scan_alt_ptable; 893 } 894 895 i = 0; 896 mismatch = ~dest[0]; 897 898 vl_vlc_needbits(&bs->vlc); 899 900 while (1) { 901 if (bs->vlc.buf >= 0x04000000) { 902 903 tab = DCT_B15_8 + (vl_vlc_ubits(&bs->vlc, 8) - 4); 904 905 i += tab->run; 906 if (i < 64) { 907 908 normal_code: 909 l = scan_ptable[j = scan[i]]; 910 bs->vlc.buf <<= tab->len; 911 bs->vlc.bits += tab->len + 1; 912 val = (tab->level * quantizer_scale * quant_matrix[l]) >> 4; 913 914 /* if (bitstream_get (1)) val = -val; */ 915 val = (val ^ vl_vlc_sbits(&bs->vlc, 1)) - vl_vlc_sbits(&bs->vlc, 1); 916 917 SATURATE (val); 918 dest[j] = val; 919 mismatch ^= val; 920 921 bs->vlc.buf <<= 1; 922 vl_vlc_needbits(&bs->vlc); 923 924 continue; 925 926 } else { 927 928 /* end of block. I commented out this code because if we */ 929 /* dont exit here we will still exit at the later test :) */ 930 931 /* if (i >= 128) break; */ /* end of block */ 932 933 /* escape code */ 934 935 i += UBITS(bs->vlc.buf << 6, 6) - 64; 936 if (i >= 64) 937 break; /* illegal, check against buffer overflow */ 938 939 l = scan_ptable[j = scan[i]]; 940 941 vl_vlc_dumpbits(&bs->vlc, 12); 942 vl_vlc_needbits(&bs->vlc); 943 val = (vl_vlc_sbits(&bs->vlc, 12) * quantizer_scale * quant_matrix[l]) / 16; 944 945 SATURATE (val); 946 dest[j] = val; 947 mismatch ^= val; 948 949 vl_vlc_dumpbits(&bs->vlc, 12); 950 vl_vlc_needbits(&bs->vlc); 951 952 continue; 953 954 } 955 } else if (bs->vlc.buf >= 0x02000000) { 956 tab = DCT_B15_10 + (vl_vlc_ubits(&bs->vlc, 10) - 8); 957 i += tab->run; 958 if (i < 64) 959 goto normal_code; 960 } else if (bs->vlc.buf >= 0x00800000) { 961 tab = DCT_13 + (vl_vlc_ubits(&bs->vlc, 13) - 16); 962 i += tab->run; 963 if (i < 64) 964 goto normal_code; 965 } else if (bs->vlc.buf >= 0x00200000) { 966 tab = DCT_15 + (vl_vlc_ubits(&bs->vlc, 15) - 16); 967 i += tab->run; 968 if (i < 64) 969 goto normal_code; 970 } else { 971 tab = DCT_16 + vl_vlc_ubits(&bs->vlc, 16); 972 bs->vlc.buf <<= 16; 973 vl_vlc_getword(&bs->vlc, bs->vlc.bits + 16); 974 i += tab->run; 975 if (i < 64) 976 goto normal_code; 977 } 978 break; /* illegal, check needed to avoid buffer overflow */ 979 } 980 981 dest[63] ^= mismatch & 1; 982 vl_vlc_dumpbits(&bs->vlc, 4); /* dump end of block code */ 983} 984 985static inline void 986get_non_intra_block(struct vl_mpg12_bs *bs, struct pipe_mpeg12_picture_desc * picture, short *dest) 987{ 988 int i, j, l, val; 989 const uint8_t *scan; 990 uint8_t *scan_ptable; 991 uint8_t *quant_matrix = picture->non_intra_quantizer_matrix; 992 int quantizer_scale = picture->quantizer_scale; 993 int mismatch; 994 const DCTtab *tab; 995 996 i = -1; 997 mismatch = 1; 998 999 if (!picture->alternate_scan) { 1000 scan = mpeg2_scan_norm_orig; 1001 scan_ptable = mpeg2_scan_norm_ptable; 1002 } else { 1003 scan = mpeg2_scan_alt_orig; 1004 scan_ptable = mpeg2_scan_alt_ptable; 1005 } 1006 1007 vl_vlc_needbits(&bs->vlc); 1008 if (bs->vlc.buf >= 0x28000000) { 1009 tab = DCT_B14DC_5 + (vl_vlc_ubits(&bs->vlc, 5) - 5); 1010 goto entry_1; 1011 } else 1012 goto entry_2; 1013 1014 while (1) { 1015 if (bs->vlc.buf >= 0x28000000) { 1016 1017 tab = DCT_B14AC_5 + (vl_vlc_ubits(&bs->vlc, 5) - 5); 1018 1019 entry_1: 1020 i += tab->run; 1021 if (i >= 64) 1022 break; /* end of block */ 1023 1024 normal_code: 1025 l = scan_ptable[j = scan[i]]; 1026 bs->vlc.buf <<= tab->len; 1027 bs->vlc.bits += tab->len + 1; 1028 val = ((2*tab->level+1) * quantizer_scale * quant_matrix[l]) >> 5; 1029 1030 /* if (bitstream_get (1)) val = -val; */ 1031 val = (val ^ vl_vlc_sbits(&bs->vlc, 1)) - vl_vlc_sbits(&bs->vlc, 1); 1032 1033 SATURATE (val); 1034 dest[j] = val; 1035 mismatch ^= val; 1036 1037 bs->vlc.buf <<= 1; 1038 vl_vlc_needbits(&bs->vlc); 1039 1040 continue; 1041 1042 } 1043 1044 entry_2: 1045 if (bs->vlc.buf >= 0x04000000) { 1046 1047 tab = DCT_B14_8 + (vl_vlc_ubits(&bs->vlc, 8) - 4); 1048 1049 i += tab->run; 1050 if (i < 64) 1051 goto normal_code; 1052 1053 /* escape code */ 1054 1055 i += UBITS(bs->vlc.buf << 6, 6) - 64; 1056 if (i >= 64) 1057 break; /* illegal, check needed to avoid buffer overflow */ 1058 1059 l = scan_ptable[j = scan[i]]; 1060 1061 vl_vlc_dumpbits(&bs->vlc, 12); 1062 vl_vlc_needbits(&bs->vlc); 1063 val = 2 * (vl_vlc_sbits(&bs->vlc, 12) + vl_vlc_sbits(&bs->vlc, 1)) + 1; 1064 val = (val * quantizer_scale * quant_matrix[l]) / 32; 1065 1066 SATURATE (val); 1067 dest[j] = val; 1068 mismatch ^= val; 1069 1070 vl_vlc_dumpbits(&bs->vlc, 12); 1071 vl_vlc_needbits(&bs->vlc); 1072 1073 continue; 1074 1075 } else if (bs->vlc.buf >= 0x02000000) { 1076 tab = DCT_B14_10 + (vl_vlc_ubits(&bs->vlc, 10) - 8); 1077 i += tab->run; 1078 if (i < 64) 1079 goto normal_code; 1080 } else if (bs->vlc.buf >= 0x00800000) { 1081 tab = DCT_13 + (vl_vlc_ubits(&bs->vlc, 13) - 16); 1082 i += tab->run; 1083 if (i < 64) 1084 goto normal_code; 1085 } else if (bs->vlc.buf >= 0x00200000) { 1086 tab = DCT_15 + (vl_vlc_ubits(&bs->vlc, 15) - 16); 1087 i += tab->run; 1088 if (i < 64) 1089 goto normal_code; 1090 } else { 1091 tab = DCT_16 + vl_vlc_ubits(&bs->vlc, 16); 1092 bs->vlc.buf <<= 16; 1093 vl_vlc_getword(&bs->vlc, bs->vlc.bits + 16); 1094 i += tab->run; 1095 if (i < 64) 1096 goto normal_code; 1097 } 1098 break; /* illegal, check needed to avoid buffer overflow */ 1099 } 1100 dest[63] ^= mismatch & 1; 1101 vl_vlc_dumpbits(&bs->vlc, 2); /* dump end of block code */ 1102} 1103 1104static inline void 1105get_mpeg1_intra_block(struct vl_mpg12_bs *bs, struct pipe_mpeg12_picture_desc * picture, short *dest) 1106{ 1107 int i, j, l, val; 1108 const uint8_t *scan; 1109 uint8_t *scan_ptable; 1110 uint8_t *quant_matrix = picture->intra_quantizer_matrix; 1111 int quantizer_scale = picture->quantizer_scale; 1112 const DCTtab * tab; 1113 1114 i = 0; 1115 1116 if (!picture->alternate_scan) { 1117 scan = mpeg2_scan_norm_orig; 1118 scan_ptable = mpeg2_scan_norm_ptable; 1119 } else { 1120 scan = mpeg2_scan_alt_orig; 1121 scan_ptable = mpeg2_scan_alt_ptable; 1122 } 1123 1124 vl_vlc_needbits(&bs->vlc); 1125 1126 while (1) { 1127 if (bs->vlc.buf >= 0x28000000) { 1128 1129 tab = DCT_B14AC_5 + (vl_vlc_ubits(&bs->vlc, 5) - 5); 1130 1131 i += tab->run; 1132 if (i >= 64) 1133 break; /* end of block */ 1134 1135 normal_code: 1136 l = scan_ptable[j = scan[i]]; 1137 bs->vlc.buf <<= tab->len; 1138 bs->vlc.bits += tab->len + 1; 1139 val = (tab->level * quantizer_scale * quant_matrix[l]) >> 4; 1140 1141 /* oddification */ 1142 val = (val - 1) | 1; 1143 1144 /* if (bitstream_get (1)) val = -val; */ 1145 val = (val ^ vl_vlc_sbits(&bs->vlc, 1)) - vl_vlc_sbits(&bs->vlc, 1); 1146 1147 SATURATE (val); 1148 dest[j] = val; 1149 1150 bs->vlc.buf <<= 1; 1151 vl_vlc_needbits(&bs->vlc); 1152 1153 continue; 1154 1155 } else if (bs->vlc.buf >= 0x04000000) { 1156 1157 tab = DCT_B14_8 + (vl_vlc_ubits(&bs->vlc, 8) - 4); 1158 1159 i += tab->run; 1160 if (i < 64) 1161 goto normal_code; 1162 1163 /* escape code */ 1164 1165 i += UBITS(bs->vlc.buf << 6, 6) - 64; 1166 if (i >= 64) 1167 break; /* illegal, check needed to avoid buffer overflow */ 1168 1169 l = scan_ptable[j = scan[i]]; 1170 1171 vl_vlc_dumpbits(&bs->vlc, 12); 1172 vl_vlc_needbits(&bs->vlc); 1173 val = vl_vlc_sbits(&bs->vlc, 8); 1174 if (! (val & 0x7f)) { 1175 vl_vlc_dumpbits(&bs->vlc, 8); 1176 val = vl_vlc_ubits(&bs->vlc, 8) + 2 * val; 1177 } 1178 val = (val * quantizer_scale * quant_matrix[l]) / 16; 1179 1180 /* oddification */ 1181 val = (val + ~SBITS (val, 1)) | 1; 1182 1183 SATURATE (val); 1184 dest[j] = val; 1185 1186 vl_vlc_dumpbits(&bs->vlc, 8); 1187 vl_vlc_needbits(&bs->vlc); 1188 1189 continue; 1190 1191 } else if (bs->vlc.buf >= 0x02000000) { 1192 tab = DCT_B14_10 + (vl_vlc_ubits(&bs->vlc, 10) - 8); 1193 i += tab->run; 1194 if (i < 64) 1195 goto normal_code; 1196 } else if (bs->vlc.buf >= 0x00800000) { 1197 tab = DCT_13 + (vl_vlc_ubits(&bs->vlc, 13) - 16); 1198 i += tab->run; 1199 if (i < 64) 1200 goto normal_code; 1201 } else if (bs->vlc.buf >= 0x00200000) { 1202 tab = DCT_15 + (vl_vlc_ubits(&bs->vlc, 15) - 16); 1203 i += tab->run; 1204 if (i < 64) 1205 goto normal_code; 1206 } else { 1207 tab = DCT_16 + vl_vlc_ubits(&bs->vlc, 16); 1208 bs->vlc.buf <<= 16; 1209 vl_vlc_getword(&bs->vlc, bs->vlc.bits + 16); 1210 i += tab->run; 1211 if (i < 64) 1212 goto normal_code; 1213 } 1214 break; /* illegal, check needed to avoid buffer overflow */ 1215 } 1216 vl_vlc_dumpbits(&bs->vlc, 2); /* dump end of block code */ 1217} 1218 1219static inline void 1220get_mpeg1_non_intra_block(struct vl_mpg12_bs *bs, struct pipe_mpeg12_picture_desc * picture, short *dest) 1221{ 1222 int i, j, l, val; 1223 const uint8_t * scan; 1224 uint8_t *scan_ptable; 1225 uint8_t *quant_matrix = picture->non_intra_quantizer_matrix; 1226 int quantizer_scale = picture->quantizer_scale; 1227 const DCTtab * tab; 1228 1229 i = -1; 1230 1231 if (!picture->alternate_scan) { 1232 scan = mpeg2_scan_norm_orig; 1233 scan_ptable = mpeg2_scan_norm_ptable; 1234 } else { 1235 scan = mpeg2_scan_alt_orig; 1236 scan_ptable = mpeg2_scan_alt_ptable; 1237 } 1238 1239 vl_vlc_needbits(&bs->vlc); 1240 if (bs->vlc.buf >= 0x28000000) { 1241 tab = DCT_B14DC_5 + (vl_vlc_ubits(&bs->vlc, 5) - 5); 1242 goto entry_1; 1243 } else 1244 goto entry_2; 1245 1246 while (1) { 1247 if (bs->vlc.buf >= 0x28000000) { 1248 1249 tab = DCT_B14AC_5 + (vl_vlc_ubits(&bs->vlc, 5) - 5); 1250 1251 entry_1: 1252 i += tab->run; 1253 if (i >= 64) 1254 break; /* end of block */ 1255 1256 normal_code: 1257 l = scan_ptable[j = scan[i]]; 1258 bs->vlc.buf <<= tab->len; 1259 bs->vlc.bits += tab->len + 1; 1260 val = ((2*tab->level+1) * quantizer_scale * quant_matrix[l]) >> 5; 1261 1262 /* oddification */ 1263 val = (val - 1) | 1; 1264 1265 /* if (bitstream_get (1)) val = -val; */ 1266 val = (val ^ vl_vlc_sbits(&bs->vlc, 1)) - vl_vlc_sbits(&bs->vlc, 1); 1267 1268 SATURATE (val); 1269 dest[j] = val; 1270 1271 bs->vlc.buf <<= 1; 1272 vl_vlc_needbits(&bs->vlc); 1273 1274 continue; 1275 1276 } 1277 1278 entry_2: 1279 if (bs->vlc.buf >= 0x04000000) { 1280 1281 tab = DCT_B14_8 + (vl_vlc_ubits(&bs->vlc, 8) - 4); 1282 1283 i += tab->run; 1284 if (i < 64) 1285 goto normal_code; 1286 1287 /* escape code */ 1288 1289 i += UBITS(bs->vlc.buf << 6, 6) - 64; 1290 if (i >= 64) 1291 break; /* illegal, check needed to avoid buffer overflow */ 1292 1293 l = scan_ptable[j = scan[i]]; 1294 1295 vl_vlc_dumpbits(&bs->vlc, 12); 1296 vl_vlc_needbits(&bs->vlc); 1297 val = vl_vlc_sbits(&bs->vlc, 8); 1298 if (! (val & 0x7f)) { 1299 vl_vlc_dumpbits(&bs->vlc, 8); 1300 val = vl_vlc_ubits(&bs->vlc, 8) + 2 * val; 1301 } 1302 val = 2 * (val + SBITS (val, 1)) + 1; 1303 val = (val * quantizer_scale * quant_matrix[l]) / 32; 1304 1305 /* oddification */ 1306 val = (val + ~SBITS (val, 1)) | 1; 1307 1308 SATURATE (val); 1309 dest[j] = val; 1310 1311 vl_vlc_dumpbits(&bs->vlc, 8); 1312 vl_vlc_needbits(&bs->vlc); 1313 1314 continue; 1315 1316 } else if (bs->vlc.buf >= 0x02000000) { 1317 tab = DCT_B14_10 + (vl_vlc_ubits(&bs->vlc, 10) - 8); 1318 i += tab->run; 1319 if (i < 64) 1320 goto normal_code; 1321 } else if (bs->vlc.buf >= 0x00800000) { 1322 tab = DCT_13 + (vl_vlc_ubits(&bs->vlc, 13) - 16); 1323 i += tab->run; 1324 if (i < 64) 1325 goto normal_code; 1326 } else if (bs->vlc.buf >= 0x00200000) { 1327 tab = DCT_15 + (vl_vlc_ubits(&bs->vlc, 15) - 16); 1328 i += tab->run; 1329 if (i < 64) 1330 goto normal_code; 1331 } else { 1332 tab = DCT_16 + vl_vlc_ubits(&bs->vlc, 16); 1333 bs->vlc.buf <<= 16; 1334 vl_vlc_getword(&bs->vlc, bs->vlc.bits + 16); 1335 i += tab->run; 1336 if (i < 64) 1337 goto normal_code; 1338 } 1339 break; /* illegal, check needed to avoid buffer overflow */ 1340 } 1341 vl_vlc_dumpbits(&bs->vlc, 2); /* dump end of block code */ 1342} 1343 1344static inline void 1345slice_intra_DCT(struct vl_mpg12_bs *bs, struct pipe_mpeg12_picture_desc * picture, int cc, 1346 unsigned x, unsigned y, enum pipe_mpeg12_dct_type coding) 1347{ 1348 short *dest = bs->ycbcr_buffer[cc]; 1349 1350 bs->ycbcr_stream[cc]->x = x; 1351 bs->ycbcr_stream[cc]->y = y; 1352 bs->ycbcr_stream[cc]->intra = PIPE_MPEG12_DCT_INTRA; 1353 bs->ycbcr_stream[cc]->coding = coding; 1354 1355 vl_vlc_needbits(&bs->vlc); 1356 1357 /* Get the intra DC coefficient and inverse quantize it */ 1358 if (cc == 0) 1359 picture->dc_dct_pred[0] += get_luma_dc_dct_diff(bs); 1360 else 1361 picture->dc_dct_pred[cc] += get_chroma_dc_dct_diff(bs); 1362 1363 memset(dest, 0, sizeof(int16_t) * 64); 1364 dest[0] = picture->dc_dct_pred[cc] << (3 - picture->intra_dc_precision); 1365 if (picture->mpeg1) { 1366 if (picture->picture_coding_type != D_TYPE) 1367 get_mpeg1_intra_block(bs, picture, dest); 1368 } else if (picture->intra_vlc_format) 1369 get_intra_block_B15(bs, picture, dest); 1370 else 1371 get_intra_block_B14(bs, picture, dest); 1372 1373 bs->num_ycbcr_blocks[cc]++; 1374 bs->ycbcr_stream[cc]++; 1375 bs->ycbcr_buffer[cc] += 64; 1376} 1377 1378static inline void 1379slice_non_intra_DCT(struct vl_mpg12_bs *bs, struct pipe_mpeg12_picture_desc * picture, int cc, 1380 unsigned x, unsigned y, enum pipe_mpeg12_dct_type coding) 1381{ 1382 short *dest = bs->ycbcr_buffer[cc]; 1383 1384 bs->ycbcr_stream[cc]->x = x; 1385 bs->ycbcr_stream[cc]->y = y; 1386 bs->ycbcr_stream[cc]->intra = PIPE_MPEG12_DCT_DELTA; 1387 bs->ycbcr_stream[cc]->coding = coding; 1388 1389 memset(dest, 0, sizeof(int16_t) * 64); 1390 if (picture->mpeg1) 1391 get_mpeg1_non_intra_block(bs, picture, dest); 1392 else 1393 get_non_intra_block(bs, picture, dest); 1394 1395 bs->num_ycbcr_blocks[cc]++; 1396 bs->ycbcr_stream[cc]++; 1397 bs->ycbcr_buffer[cc] += 64; 1398} 1399 1400static inline void 1401motion_mp1(struct vl_mpg12_bs *bs, unsigned f_code[2], struct pipe_motionvector *mv) 1402{ 1403 int motion_x, motion_y; 1404 1405 mv->top.field_select = mv->bottom.field_select = PIPE_VIDEO_FRAME; 1406 1407 vl_vlc_needbits(&bs->vlc); 1408 motion_x = (mv->top.x + (get_motion_delta(bs, f_code[0]) << f_code[1])); 1409 motion_x = bound_motion_vector (motion_x, f_code[0] + f_code[1]); 1410 mv->top.x = mv->bottom.x = motion_x; 1411 1412 vl_vlc_needbits(&bs->vlc); 1413 motion_y = (mv->top.y + (get_motion_delta(bs, f_code[0]) << f_code[1])); 1414 motion_y = bound_motion_vector (motion_y, f_code[0] + f_code[1]); 1415 mv->top.y = mv->bottom.y = motion_y; 1416} 1417 1418static inline void 1419motion_fr_frame(struct vl_mpg12_bs *bs, unsigned f_code[2], struct pipe_motionvector *mv) 1420{ 1421 int motion_x, motion_y; 1422 1423 mv->top.field_select = mv->bottom.field_select = PIPE_VIDEO_FRAME; 1424 1425 vl_vlc_needbits(&bs->vlc); 1426 motion_x = mv->top.x + get_motion_delta(bs, f_code[0]); 1427 motion_x = bound_motion_vector(motion_x, f_code[0]); 1428 mv->top.x = mv->bottom.x = motion_x; 1429 1430 vl_vlc_needbits(&bs->vlc); 1431 motion_y = mv->top.y + get_motion_delta(bs, f_code[1]); 1432 motion_y = bound_motion_vector(motion_y, f_code[1]); 1433 mv->top.y = mv->bottom.y = motion_y; 1434} 1435 1436static inline void 1437motion_fr_field(struct vl_mpg12_bs *bs, unsigned f_code[2], struct pipe_motionvector *mv) 1438{ 1439 int motion_x, motion_y; 1440 1441 vl_vlc_needbits(&bs->vlc); 1442 mv->top.field_select = vl_vlc_ubits(&bs->vlc, 1) ? 1443 PIPE_VIDEO_BOTTOM_FIELD : PIPE_VIDEO_TOP_FIELD; 1444 vl_vlc_dumpbits(&bs->vlc, 1); 1445 1446 motion_x = mv->top.x + get_motion_delta(bs, f_code[0]); 1447 motion_x = bound_motion_vector (motion_x, f_code[0]); 1448 mv->top.x = motion_x; 1449 1450 vl_vlc_needbits(&bs->vlc); 1451 motion_y = (mv->top.y >> 1) + get_motion_delta(bs, f_code[1]); 1452 /* motion_y = bound_motion_vector (motion_y, f_code[1]); */ 1453 mv->top.y = motion_y << 1; 1454 1455 vl_vlc_needbits(&bs->vlc); 1456 mv->bottom.field_select = vl_vlc_ubits(&bs->vlc, 1) ? 1457 PIPE_VIDEO_BOTTOM_FIELD : PIPE_VIDEO_TOP_FIELD; 1458 vl_vlc_dumpbits(&bs->vlc, 1); 1459 1460 motion_x = mv->bottom.x + get_motion_delta(bs, f_code[0]); 1461 motion_x = bound_motion_vector (motion_x, f_code[0]); 1462 mv->bottom.x = motion_x; 1463 1464 vl_vlc_needbits(&bs->vlc); 1465 motion_y = (mv->bottom.y >> 1) + get_motion_delta(bs, f_code[1]); 1466 /* motion_y = bound_motion_vector (motion_y, f_code[1]); */ 1467 mv->bottom.y = motion_y << 1; 1468} 1469 1470static inline void 1471motion_fr_dmv(struct vl_mpg12_bs *bs, unsigned f_code[2], struct pipe_motionvector *mv) 1472{ 1473 int motion_x, motion_y; 1474 1475 // TODO Implement dmv 1476 mv->top.field_select = mv->bottom.field_select = PIPE_VIDEO_FRAME; 1477 1478 vl_vlc_needbits(&bs->vlc); 1479 motion_x = mv->top.x + get_motion_delta(bs, f_code[0]); 1480 motion_x = bound_motion_vector(motion_x, f_code[0]); 1481 mv->top.x = mv->bottom.x = motion_x; 1482 1483 vl_vlc_needbits(&bs->vlc); 1484 motion_y = (mv->top.y >> 1) + get_motion_delta(bs, f_code[1]); 1485 /* motion_y = bound_motion_vector (motion_y, f_code[1]); */ 1486 mv->top.y = mv->bottom.y = motion_y << 1; 1487} 1488 1489/* like motion_frame, but parsing without actual motion compensation */ 1490static inline void 1491motion_fr_conceal(struct vl_mpg12_bs *bs, unsigned f_code[2], struct pipe_motionvector *mv) 1492{ 1493 int tmp; 1494 1495 mv->top.field_select = mv->bottom.field_select = PIPE_VIDEO_FRAME; 1496 1497 vl_vlc_needbits(&bs->vlc); 1498 tmp = (mv->top.x + get_motion_delta(bs, f_code[0])); 1499 tmp = bound_motion_vector (tmp, f_code[0]); 1500 mv->top.x = mv->bottom.x = tmp; 1501 1502 vl_vlc_needbits(&bs->vlc); 1503 tmp = (mv->top.y + get_motion_delta(bs, f_code[1])); 1504 tmp = bound_motion_vector (tmp, f_code[1]); 1505 mv->top.y = mv->bottom.y = tmp; 1506 1507 vl_vlc_dumpbits(&bs->vlc, 1); /* remove marker_bit */ 1508} 1509 1510static inline void 1511motion_fi_field(struct vl_mpg12_bs *bs, unsigned f_code[2], struct pipe_motionvector *mv) 1512{ 1513 int motion_x, motion_y; 1514 1515 vl_vlc_needbits(&bs->vlc); 1516 1517 // ref_field 1518 //vl_vlc_ubits(&bs->vlc, 1); 1519 1520 // TODO field select may need to do something here for bob (weave ok) 1521 mv->top.field_select = mv->bottom.field_select = PIPE_VIDEO_FRAME; 1522 vl_vlc_dumpbits(&bs->vlc, 1); 1523 1524 motion_x = mv->top.x + get_motion_delta(bs, f_code[0]); 1525 motion_x = bound_motion_vector (motion_x, f_code[0]); 1526 mv->top.x = mv->bottom.x = motion_x; 1527 1528 vl_vlc_needbits(&bs->vlc); 1529 motion_y = mv->top.y + get_motion_delta(bs, f_code[1]); 1530 motion_y = bound_motion_vector (motion_y, f_code[1]); 1531 mv->top.y = mv->bottom.y = motion_y; 1532} 1533 1534static inline void 1535motion_fi_16x8(struct vl_mpg12_bs *bs, unsigned f_code[2], struct pipe_motionvector *mv) 1536{ 1537 int motion_x, motion_y; 1538 1539 vl_vlc_needbits(&bs->vlc); 1540 1541 // ref_field 1542 //vl_vlc_ubits(&bs->vlc, 1); 1543 1544 // TODO field select may need to do something here bob (weave ok) 1545 mv->top.field_select = PIPE_VIDEO_FRAME; 1546 vl_vlc_dumpbits(&bs->vlc, 1); 1547 1548 motion_x = mv->top.x + get_motion_delta(bs, f_code[0]); 1549 motion_x = bound_motion_vector (motion_x, f_code[0]); 1550 mv->top.x = motion_x; 1551 1552 vl_vlc_needbits(&bs->vlc); 1553 motion_y = mv->top.y + get_motion_delta(bs, f_code[1]); 1554 motion_y = bound_motion_vector (motion_y, f_code[1]); 1555 mv->top.y = motion_y; 1556 1557 vl_vlc_needbits(&bs->vlc); 1558 // ref_field 1559 //vl_vlc_ubits(&bs->vlc, 1); 1560 1561 // TODO field select may need to do something here for bob (weave ok) 1562 mv->bottom.field_select = PIPE_VIDEO_FRAME; 1563 vl_vlc_dumpbits(&bs->vlc, 1); 1564 1565 motion_x = mv->bottom.x + get_motion_delta(bs, f_code[0]); 1566 motion_x = bound_motion_vector (motion_x, f_code[0]); 1567 mv->bottom.x = motion_x; 1568 1569 vl_vlc_needbits(&bs->vlc); 1570 motion_y = mv->bottom.y + get_motion_delta(bs, f_code[1]); 1571 motion_y = bound_motion_vector (motion_y, f_code[1]); 1572 mv->bottom.y = motion_y; 1573} 1574 1575static inline void 1576motion_fi_dmv(struct vl_mpg12_bs *bs, unsigned f_code[2], struct pipe_motionvector *mv) 1577{ 1578 int motion_x, motion_y; 1579 1580 // TODO field select may need to do something here for bob (weave ok) 1581 mv->top.field_select = mv->bottom.field_select = PIPE_VIDEO_FRAME; 1582 1583 vl_vlc_needbits(&bs->vlc); 1584 motion_x = mv->top.x + get_motion_delta(bs, f_code[0]); 1585 motion_x = bound_motion_vector (motion_x, f_code[0]); 1586 mv->top.x = mv->bottom.x = motion_x; 1587 1588 vl_vlc_needbits(&bs->vlc); 1589 motion_y = mv->top.y + get_motion_delta(bs, f_code[1]); 1590 motion_y = bound_motion_vector (motion_y, f_code[1]); 1591 mv->top.y = mv->bottom.y = motion_y; 1592} 1593 1594 1595static inline void 1596motion_fi_conceal(struct vl_mpg12_bs *bs, unsigned f_code[2], struct pipe_motionvector *mv) 1597{ 1598 int tmp; 1599 1600 vl_vlc_needbits(&bs->vlc); 1601 vl_vlc_dumpbits(&bs->vlc, 1); /* remove field_select */ 1602 1603 tmp = (mv->top.x + get_motion_delta(bs, f_code[0])); 1604 tmp = bound_motion_vector(tmp, f_code[0]); 1605 mv->top.x = mv->bottom.x = tmp; 1606 1607 vl_vlc_needbits(&bs->vlc); 1608 tmp = (mv->top.y + get_motion_delta(bs, f_code[1])); 1609 tmp = bound_motion_vector(tmp, f_code[1]); 1610 mv->top.y = mv->bottom.y = tmp; 1611 1612 vl_vlc_dumpbits(&bs->vlc, 1); /* remove marker_bit */ 1613} 1614 1615#define MOTION_CALL(routine, macroblock_modes) \ 1616do { \ 1617 if ((macroblock_modes) & MACROBLOCK_MOTION_FORWARD) \ 1618 routine(bs, picture->f_code[0], &mv_fwd); \ 1619 if ((macroblock_modes) & MACROBLOCK_MOTION_BACKWARD) \ 1620 routine(bs, picture->f_code[1], &mv_bwd); \ 1621} while (0) 1622 1623#define NEXT_MACROBLOCK \ 1624do { \ 1625 bs->mv_stream[0][x+y*bs->width/16] = mv_fwd; \ 1626 bs->mv_stream[1][x+y*bs->width/16] = mv_bwd; \ 1627 ++x; \ 1628 if (x == bs->width/16) { \ 1629 ++y; \ 1630 if (y >= bs->height/16) \ 1631 return false; \ 1632 x = 0; \ 1633 } \ 1634} while (0) 1635 1636static inline bool 1637slice_init(struct vl_mpg12_bs *bs, struct pipe_mpeg12_picture_desc * picture, int *x, int *y) 1638{ 1639 const MBAtab * mba; 1640 1641 vl_vlc_need32bits(&bs->vlc); 1642 while(bs->vlc.buf < 0x101 || bs->vlc.buf > 0x1AF) { 1643 if(!vl_vlc_getbyte(&bs->vlc)) 1644 return false; 1645 } 1646 *y = ((bs->vlc.buf & 0xFF) - 1) * 16; 1647 vl_vlc_restart(&bs->vlc); 1648 1649 //TODO conversion to signed format signed format 1650 picture->dc_dct_pred[0] = picture->dc_dct_pred[1] = picture->dc_dct_pred[2] = 0; 1651 1652 picture->quantizer_scale = get_quantizer_scale(bs, picture); 1653 1654 /* ignore intra_slice and all the extra data */ 1655 while (bs->vlc.buf & 0x80000000) { 1656 vl_vlc_dumpbits(&bs->vlc, 9); 1657 vl_vlc_needbits(&bs->vlc); 1658 } 1659 1660 /* decode initial macroblock address increment */ 1661 *x = 0; 1662 while (1) { 1663 if (bs->vlc.buf >= 0x08000000) { 1664 mba = MBA_5 + (vl_vlc_ubits(&bs->vlc, 6) - 2); 1665 break; 1666 } else if (bs->vlc.buf >= 0x01800000) { 1667 mba = MBA_11 + (vl_vlc_ubits(&bs->vlc, 12) - 24); 1668 break; 1669 } else switch (vl_vlc_ubits(&bs->vlc, 12)) { 1670 case 8: /* macroblock_escape */ 1671 *x += 33; 1672 vl_vlc_dumpbits(&bs->vlc, 11); 1673 vl_vlc_needbits(&bs->vlc); 1674 continue; 1675 case 15: /* macroblock_stuffing (MPEG1 only) */ 1676 bs->vlc.buf &= 0xfffff; 1677 vl_vlc_dumpbits(&bs->vlc, 11); 1678 vl_vlc_needbits(&bs->vlc); 1679 continue; 1680 default: /* error */ 1681 return false; 1682 } 1683 } 1684 vl_vlc_dumpbits(&bs->vlc, mba->len + 1); 1685 *x = (*x + mba->mba) << 4; 1686 1687 while (*x >= bs->width) { 1688 *x -= bs->width; 1689 *y += 16; 1690 } 1691 if (*y > bs->height) 1692 return false; 1693 1694 return true; 1695} 1696 1697static inline bool 1698decode_slice(struct vl_mpg12_bs *bs, struct pipe_mpeg12_picture_desc *picture) 1699{ 1700 struct pipe_motionvector mv_fwd, mv_bwd; 1701 enum pipe_mpeg12_dct_type dct_type; 1702 int x, y; 1703 1704 if (!slice_init(bs, picture, &x, &y)) 1705 return false; 1706 1707 mv_fwd.top.x = mv_fwd.top.y = mv_fwd.bottom.x = mv_fwd.bottom.y = 0; 1708 mv_fwd.top.field_select = mv_fwd.bottom.field_select = PIPE_VIDEO_FRAME; 1709 1710 mv_bwd.top.x = mv_bwd.top.y = mv_bwd.bottom.x = mv_bwd.bottom.y = 0; 1711 mv_bwd.top.field_select = mv_bwd.bottom.field_select = PIPE_VIDEO_FRAME; 1712 1713 x /= 16; 1714 y /= 16; 1715 1716 while (1) { 1717 int macroblock_modes; 1718 int mba_inc; 1719 const MBAtab * mba; 1720 1721 vl_vlc_needbits(&bs->vlc); 1722 1723 macroblock_modes = get_macroblock_modes(bs, picture); //macroblock_modes() 1724 dct_type = macroblock_modes & DCT_TYPE_INTERLACED ? 1725 PIPE_MPEG12_DCT_TYPE_FIELD : PIPE_MPEG12_DCT_TYPE_FRAME; 1726 1727 switch(macroblock_modes & (MACROBLOCK_MOTION_FORWARD|MACROBLOCK_MOTION_BACKWARD)) { 1728 case (MACROBLOCK_MOTION_FORWARD|MACROBLOCK_MOTION_BACKWARD): 1729 mv_fwd.top.weight = mv_fwd.bottom.weight = PIPE_VIDEO_MV_WEIGHT_HALF; 1730 mv_bwd.top.weight = mv_bwd.bottom.weight = PIPE_VIDEO_MV_WEIGHT_HALF; 1731 break; 1732 1733 default: 1734 case MACROBLOCK_MOTION_FORWARD: 1735 mv_fwd.top.weight = mv_fwd.bottom.weight = PIPE_VIDEO_MV_WEIGHT_MAX; 1736 mv_bwd.top.weight = mv_bwd.bottom.weight = PIPE_VIDEO_MV_WEIGHT_MIN; 1737 break; 1738 1739 case MACROBLOCK_MOTION_BACKWARD: 1740 mv_fwd.top.weight = mv_fwd.bottom.weight = PIPE_VIDEO_MV_WEIGHT_MIN; 1741 mv_bwd.top.weight = mv_bwd.bottom.weight = PIPE_VIDEO_MV_WEIGHT_MAX; 1742 break; 1743 } 1744 1745 /* maybe integrate MACROBLOCK_QUANT test into get_macroblock_modes ? */ 1746 if (macroblock_modes & MACROBLOCK_QUANT) 1747 picture->quantizer_scale = get_quantizer_scale(bs, picture); 1748 1749 if (macroblock_modes & MACROBLOCK_INTRA) { 1750 1751 if (picture->concealment_motion_vectors) { 1752 if (picture->picture_structure == FRAME_PICTURE) 1753 motion_fr_conceal(bs, picture->f_code[0], &mv_fwd); 1754 else 1755 motion_fi_conceal(bs, picture->f_code[0], &mv_fwd); 1756 1757 } else { 1758 mv_fwd.top.x = mv_fwd.top.y = mv_fwd.bottom.x = mv_fwd.bottom.y = 0; 1759 mv_bwd.top.x = mv_bwd.top.y = mv_bwd.bottom.x = mv_bwd.bottom.y = 0; 1760 } 1761 mv_fwd.top.weight = mv_fwd.bottom.weight = PIPE_VIDEO_MV_WEIGHT_MIN; 1762 mv_bwd.top.weight = mv_bwd.bottom.weight = PIPE_VIDEO_MV_WEIGHT_MIN; 1763 1764 // unravaled loop of 6 block(i) calls in macroblock() 1765 slice_intra_DCT(bs, picture, 0, x*2+0, y*2+0, dct_type); 1766 slice_intra_DCT(bs, picture, 0, x*2+1, y*2+0, dct_type); 1767 slice_intra_DCT(bs, picture, 0, x*2+0, y*2+1, dct_type); 1768 slice_intra_DCT(bs, picture, 0, x*2+1, y*2+1, dct_type); 1769 slice_intra_DCT(bs, picture, 1, x, y, dct_type); 1770 slice_intra_DCT(bs, picture, 2, x, y, dct_type); 1771 1772 if (picture->picture_coding_type == D_TYPE) { 1773 vl_vlc_needbits(&bs->vlc); 1774 vl_vlc_dumpbits(&bs->vlc, 1); 1775 } 1776 1777 } else { 1778 if (picture->picture_structure == FRAME_PICTURE) 1779 switch (macroblock_modes & MOTION_TYPE_MASK) { 1780 case MC_FRAME: 1781 if (picture->mpeg1) { 1782 MOTION_CALL(motion_mp1, macroblock_modes); 1783 } else { 1784 MOTION_CALL(motion_fr_frame, macroblock_modes); 1785 } 1786 break; 1787 1788 case MC_FIELD: 1789 MOTION_CALL (motion_fr_field, macroblock_modes); 1790 break; 1791 1792 case MC_DMV: 1793 MOTION_CALL (motion_fr_dmv, MACROBLOCK_MOTION_FORWARD); 1794 break; 1795 1796 case 0: 1797 /* non-intra mb without forward mv in a P picture */ 1798 mv_fwd.top.x = mv_fwd.top.y = mv_fwd.bottom.x = mv_fwd.bottom.y = 0; 1799 mv_bwd.top.x = mv_bwd.top.y = mv_bwd.bottom.x = mv_bwd.bottom.y = 0; 1800 break; 1801 } 1802 else 1803 switch (macroblock_modes & MOTION_TYPE_MASK) { 1804 case MC_FIELD: 1805 MOTION_CALL (motion_fi_field, macroblock_modes); 1806 break; 1807 1808 case MC_16X8: 1809 MOTION_CALL (motion_fi_16x8, macroblock_modes); 1810 break; 1811 1812 case MC_DMV: 1813 MOTION_CALL (motion_fi_dmv, MACROBLOCK_MOTION_FORWARD); 1814 break; 1815 1816 case 0: 1817 /* non-intra mb without forward mv in a P picture */ 1818 mv_fwd.top.x = mv_fwd.top.y = mv_fwd.bottom.x = mv_fwd.bottom.y = 0; 1819 mv_bwd.top.x = mv_bwd.top.y = mv_bwd.bottom.x = mv_bwd.bottom.y = 0; 1820 break; 1821 } 1822 1823 if (macroblock_modes & MACROBLOCK_PATTERN) { 1824 int coded_block_pattern = get_coded_block_pattern(bs); 1825 1826 // TODO optimize not fully used for idct accel only mc. 1827 if (coded_block_pattern & 0x20) 1828 slice_non_intra_DCT(bs, picture, 0, x*2+0, y*2+0, dct_type); // cc0 luma 0 1829 if (coded_block_pattern & 0x10) 1830 slice_non_intra_DCT(bs, picture, 0, x*2+1, y*2+0, dct_type); // cc0 luma 1 1831 if (coded_block_pattern & 0x08) 1832 slice_non_intra_DCT(bs, picture, 0, x*2+0, y*2+1, dct_type); // cc0 luma 2 1833 if (coded_block_pattern & 0x04) 1834 slice_non_intra_DCT(bs, picture, 0, x*2+1, y*2+1, dct_type); // cc0 luma 3 1835 if (coded_block_pattern & 0x2) 1836 slice_non_intra_DCT(bs, picture, 1, x, y, dct_type); // cc1 croma 1837 if (coded_block_pattern & 0x1) 1838 slice_non_intra_DCT(bs, picture, 2, x, y, dct_type); // cc2 croma 1839 } 1840 1841 picture->dc_dct_pred[0] = picture->dc_dct_pred[1] = picture->dc_dct_pred[2] = 0; 1842 } 1843 1844 NEXT_MACROBLOCK; 1845 1846 vl_vlc_needbits(&bs->vlc); 1847 mba_inc = 0; 1848 while (1) { 1849 if (bs->vlc.buf >= 0x10000000) { 1850 mba = MBA_5 + (vl_vlc_ubits(&bs->vlc, 5) - 2); 1851 break; 1852 } else if (bs->vlc.buf >= 0x03000000) { 1853 mba = MBA_11 + (vl_vlc_ubits(&bs->vlc, 11) - 24); 1854 break; 1855 } else switch (vl_vlc_ubits(&bs->vlc, 11)) { 1856 case 8: /* macroblock_escape */ 1857 mba_inc += 33; 1858 /* pass through */ 1859 case 15: /* macroblock_stuffing (MPEG1 only) */ 1860 vl_vlc_dumpbits(&bs->vlc, 11); 1861 vl_vlc_needbits(&bs->vlc); 1862 continue; 1863 default: /* end of slice, or error */ 1864 return true; 1865 } 1866 } 1867 vl_vlc_dumpbits(&bs->vlc, mba->len); 1868 mba_inc += mba->mba; 1869 if (mba_inc) { 1870 //TODO conversion to signed format signed format 1871 picture->dc_dct_pred[0] = picture->dc_dct_pred[1] = picture->dc_dct_pred[2] = 0; 1872 1873 switch(picture->picture_structure) { 1874 case FRAME_PICTURE: 1875 mv_fwd.top.field_select = mv_fwd.bottom.field_select = PIPE_VIDEO_FRAME; 1876 mv_bwd.top.field_select = mv_bwd.bottom.field_select = PIPE_VIDEO_FRAME; 1877 break; 1878 1879 case TOP_FIELD: 1880 mv_fwd.top.field_select = mv_fwd.bottom.field_select = PIPE_VIDEO_TOP_FIELD; 1881 mv_bwd.top.field_select = mv_bwd.bottom.field_select = PIPE_VIDEO_TOP_FIELD; 1882 break; 1883 1884 case BOTTOM_FIELD: 1885 mv_fwd.top.field_select = mv_fwd.bottom.field_select = PIPE_VIDEO_BOTTOM_FIELD; 1886 mv_bwd.top.field_select = mv_bwd.bottom.field_select = PIPE_VIDEO_BOTTOM_FIELD; 1887 break; 1888 } 1889 1890 if (picture->picture_coding_type == P_TYPE) { 1891 mv_fwd.top.x = mv_fwd.top.y = mv_fwd.bottom.x = mv_fwd.bottom.y = 0; 1892 mv_fwd.top.weight = mv_fwd.bottom.weight = PIPE_VIDEO_MV_WEIGHT_MAX; 1893 } 1894 do { 1895 NEXT_MACROBLOCK; 1896 } while (--mba_inc); 1897 } 1898 } 1899} 1900 1901void 1902vl_mpg12_bs_init(struct vl_mpg12_bs *bs, unsigned width, unsigned height) 1903{ 1904 assert(bs); 1905 1906 memset(bs, 0, sizeof(struct vl_mpg12_bs)); 1907 1908 bs->width = width; 1909 bs->height = height; 1910 1911 setup_scan_ptable(); 1912} 1913 1914void 1915vl_mpg12_bs_set_buffers(struct vl_mpg12_bs *bs, struct pipe_ycbcr_block *ycbcr_stream[VL_MAX_PLANES], 1916 short *ycbcr_buffer[VL_MAX_PLANES], struct pipe_motionvector *mv_stream[VL_MAX_REF_FRAMES]) 1917{ 1918 unsigned i; 1919 1920 assert(bs); 1921 assert(ycbcr_stream && ycbcr_buffer); 1922 assert(mv_stream); 1923 1924 for (i = 0; i < VL_MAX_PLANES; ++i) { 1925 bs->ycbcr_stream[i] = ycbcr_stream[i]; 1926 bs->ycbcr_buffer[i] = ycbcr_buffer[i]; 1927 } 1928 for (i = 0; i < VL_MAX_REF_FRAMES; ++i) 1929 bs->mv_stream[i] = mv_stream[i]; 1930 1931 // TODO 1932 for (i = 0; i < bs->width/16*bs->height/16; ++i) { 1933 bs->mv_stream[0][i].top.x = bs->mv_stream[0][i].top.y = 0; 1934 bs->mv_stream[0][i].top.field_select = PIPE_VIDEO_FRAME; 1935 bs->mv_stream[0][i].top.weight = PIPE_VIDEO_MV_WEIGHT_MAX; 1936 bs->mv_stream[0][i].bottom.x = bs->mv_stream[0][i].bottom.y = 0; 1937 bs->mv_stream[0][i].bottom.field_select = PIPE_VIDEO_FRAME; 1938 bs->mv_stream[0][i].bottom.weight = PIPE_VIDEO_MV_WEIGHT_MAX; 1939 1940 bs->mv_stream[1][i].top.x = bs->mv_stream[1][i].top.y = 0; 1941 bs->mv_stream[1][i].top.field_select = PIPE_VIDEO_FRAME; 1942 bs->mv_stream[1][i].top.weight = PIPE_VIDEO_MV_WEIGHT_MIN; 1943 bs->mv_stream[1][i].bottom.x = bs->mv_stream[1][i].bottom.y = 0; 1944 bs->mv_stream[1][i].bottom.field_select = PIPE_VIDEO_FRAME; 1945 bs->mv_stream[1][i].bottom.weight = PIPE_VIDEO_MV_WEIGHT_MIN; 1946 } 1947} 1948 1949void 1950vl_mpg12_bs_decode(struct vl_mpg12_bs *bs, unsigned num_bytes, const void *buffer, 1951 struct pipe_mpeg12_picture_desc *picture, unsigned num_ycbcr_blocks[3]) 1952{ 1953 assert(bs); 1954 assert(num_ycbcr_blocks); 1955 assert(buffer && num_bytes); 1956 1957 bs->num_ycbcr_blocks = num_ycbcr_blocks; 1958 1959 vl_vlc_init(&bs->vlc, buffer, num_bytes); 1960 1961 while(decode_slice(bs, picture)); 1962} 1963