vl_mpeg12_bitstream.c revision ce31aaec02de0ef509dcee9d641c39623017a638
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_zscan.h" 59#include "vl_mpeg12_bitstream.h" 60 61/* take num bits from the high part of bit_buf and zero extend them */ 62#define UBITS(buf,num) (((uint32_t)(buf)) >> (32 - (num))) 63 64/* take num bits from the high part of bit_buf and sign extend them */ 65#define SBITS(buf,num) (((int32_t)(buf)) >> (32 - (num))) 66 67#define SATURATE(val) \ 68do { \ 69 if ((uint32_t)(val + 2048) > 4095) \ 70 val = (val > 0) ? 2047 : -2048; \ 71} while (0) 72 73/* macroblock modes */ 74#define MACROBLOCK_INTRA 1 75#define MACROBLOCK_PATTERN 2 76#define MACROBLOCK_MOTION_BACKWARD 4 77#define MACROBLOCK_MOTION_FORWARD 8 78#define MACROBLOCK_QUANT 16 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 460static const int non_linear_quantizer_scale[] = { 461 0, 1, 2, 3, 4, 5, 6, 7, 462 8, 10, 12, 14, 16, 18, 20, 22, 463 24, 28, 32, 36, 40, 44, 48, 52, 464 56, 64, 72, 80, 88, 96, 104, 112 465}; 466 467static inline int 468get_macroblock_modes(struct vl_mpg12_bs *bs, struct pipe_mpeg12_picture_desc * picture) 469{ 470 int macroblock_modes; 471 const MBtab * tab; 472 473 switch (picture->picture_coding_type) { 474 case I_TYPE: 475 476 tab = MB_I + vl_vlc_ubits(&bs->vlc, 1); 477 vl_vlc_dumpbits(&bs->vlc, tab->len); 478 macroblock_modes = tab->modes; 479 480 return macroblock_modes; 481 482 case P_TYPE: 483 484 tab = MB_P + vl_vlc_ubits(&bs->vlc, 5); 485 vl_vlc_dumpbits(&bs->vlc, tab->len); 486 macroblock_modes = tab->modes; 487 488 if (picture->picture_structure != FRAME_PICTURE) { 489 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD) { 490 macroblock_modes |= vl_vlc_ubits(&bs->vlc, 2) * MOTION_TYPE_BASE; 491 vl_vlc_dumpbits(&bs->vlc, 2); 492 } 493 return macroblock_modes; 494 } else if (picture->frame_pred_frame_dct) { 495 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD) 496 macroblock_modes |= MC_FRAME; 497 return macroblock_modes; 498 } else { 499 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD) { 500 macroblock_modes |= vl_vlc_ubits(&bs->vlc, 2) * MOTION_TYPE_BASE; 501 vl_vlc_dumpbits(&bs->vlc, 2); 502 } 503 return macroblock_modes; 504 } 505 506 case B_TYPE: 507 508 tab = MB_B + vl_vlc_ubits(&bs->vlc, 6); 509 vl_vlc_dumpbits(&bs->vlc, tab->len); 510 macroblock_modes = tab->modes; 511 512 if (picture->picture_structure != FRAME_PICTURE) { 513 if (! (macroblock_modes & MACROBLOCK_INTRA)) { 514 macroblock_modes |= vl_vlc_ubits(&bs->vlc, 2) * MOTION_TYPE_BASE; 515 vl_vlc_dumpbits(&bs->vlc, 2); 516 } 517 } else if (picture->frame_pred_frame_dct) { 518 macroblock_modes |= MC_FRAME; 519 } else if (!(macroblock_modes & MACROBLOCK_INTRA)) { 520 macroblock_modes |= vl_vlc_ubits(&bs->vlc, 2) * MOTION_TYPE_BASE; 521 vl_vlc_dumpbits(&bs->vlc, 2); 522 } 523 return macroblock_modes; 524 525 case D_TYPE: 526 527 vl_vlc_dumpbits(&bs->vlc, 1); 528 return MACROBLOCK_INTRA; 529 530 default: 531 return 0; 532 } 533} 534 535static inline enum pipe_mpeg12_dct_type 536get_dct_type(struct vl_mpg12_bs *bs, struct pipe_mpeg12_picture_desc * picture, int macroblock_modes) 537{ 538 enum pipe_mpeg12_dct_type dct_type = PIPE_MPEG12_DCT_TYPE_FRAME; 539 540 if ((picture->picture_structure == FRAME_PICTURE) && 541 (!picture->frame_pred_frame_dct) && 542 (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))) { 543 544 dct_type = vl_vlc_ubits(&bs->vlc, 1) ? PIPE_MPEG12_DCT_TYPE_FIELD : PIPE_MPEG12_DCT_TYPE_FRAME; 545 vl_vlc_dumpbits(&bs->vlc, 1); 546 } 547 return dct_type; 548} 549 550static inline int 551get_quantizer_scale(struct vl_mpg12_bs *bs, struct pipe_mpeg12_picture_desc * picture) 552{ 553 int quantizer_scale_code; 554 555 quantizer_scale_code = vl_vlc_ubits(&bs->vlc, 5); 556 vl_vlc_dumpbits(&bs->vlc, 5); 557 558 if (picture->q_scale_type) 559 return non_linear_quantizer_scale[quantizer_scale_code]; 560 else 561 return quantizer_scale_code << 1; 562} 563 564static inline int 565get_motion_delta(struct vl_mpg12_bs *bs, unsigned f_code) 566{ 567 int delta; 568 int sign; 569 const MVtab * tab; 570 571 if (bs->vlc.buf & 0x80000000) { 572 vl_vlc_dumpbits(&bs->vlc, 1); 573 return 0; 574 } else if (bs->vlc.buf >= 0x0c000000) { 575 576 tab = MV_4 + vl_vlc_ubits(&bs->vlc, 4); 577 delta = (tab->delta << f_code) + 1; 578 bs->vlc.bits += tab->len + f_code + 1; 579 bs->vlc.buf <<= tab->len; 580 581 sign = vl_vlc_sbits(&bs->vlc, 1); 582 bs->vlc.buf <<= 1; 583 584 if (f_code) 585 delta += vl_vlc_ubits(&bs->vlc, f_code); 586 bs->vlc.buf <<= f_code; 587 588 return (delta ^ sign) - sign; 589 590 } else { 591 592 tab = MV_10 + vl_vlc_ubits(&bs->vlc, 10); 593 delta = (tab->delta << f_code) + 1; 594 bs->vlc.bits += tab->len + 1; 595 bs->vlc.buf <<= tab->len; 596 597 sign = vl_vlc_sbits(&bs->vlc, 1); 598 bs->vlc.buf <<= 1; 599 600 if (f_code) { 601 vl_vlc_needbits(&bs->vlc); 602 delta += vl_vlc_ubits(&bs->vlc, f_code); 603 vl_vlc_dumpbits(&bs->vlc, f_code); 604 } 605 606 return (delta ^ sign) - sign; 607 } 608} 609 610static inline int 611bound_motion_vector(int vec, unsigned f_code) 612{ 613#if 1 614 unsigned int limit; 615 int sign; 616 617 limit = 16 << f_code; 618 619 if ((unsigned int)(vec + limit) < 2 * limit) 620 return vec; 621 else { 622 sign = ((int32_t)vec) >> 31; 623 return vec - ((2 * limit) ^ sign) + sign; 624 } 625#else 626 return ((int32_t)vec << (28 - f_code)) >> (28 - f_code); 627#endif 628} 629 630static inline int 631get_dmv(struct vl_mpg12_bs *bs) 632{ 633 const DMVtab * tab; 634 635 tab = DMV_2 + vl_vlc_ubits(&bs->vlc, 2); 636 vl_vlc_dumpbits(&bs->vlc, tab->len); 637 return tab->dmv; 638} 639 640static inline int 641get_coded_block_pattern(struct vl_mpg12_bs *bs) 642{ 643 const CBPtab * tab; 644 645 vl_vlc_needbits(&bs->vlc); 646 647 if (bs->vlc.buf >= 0x20000000) { 648 649 tab = CBP_7 + (vl_vlc_ubits(&bs->vlc, 7) - 16); 650 vl_vlc_dumpbits(&bs->vlc, tab->len); 651 return tab->cbp; 652 653 } else { 654 655 tab = CBP_9 + vl_vlc_ubits(&bs->vlc, 9); 656 vl_vlc_dumpbits(&bs->vlc, tab->len); 657 return tab->cbp; 658 } 659} 660 661static inline int 662get_luma_dc_dct_diff(struct vl_mpg12_bs *bs) 663{ 664 const DCtab * tab; 665 int size; 666 int dc_diff; 667 668 if (bs->vlc.buf < 0xf8000000) { 669 tab = DC_lum_5 + vl_vlc_ubits(&bs->vlc, 5); 670 size = tab->size; 671 if (size) { 672 bs->vlc.bits += tab->len + size; 673 bs->vlc.buf <<= tab->len; 674 dc_diff = vl_vlc_ubits(&bs->vlc, size) - UBITS (SBITS (~bs->vlc.buf, 1), size); 675 bs->vlc.buf <<= size; 676 return dc_diff; 677 } else { 678 vl_vlc_dumpbits(&bs->vlc, 3); 679 return 0; 680 } 681 } else { 682 tab = DC_long + (vl_vlc_ubits(&bs->vlc, 9) - 0x1e0); 683 size = tab->size; 684 vl_vlc_dumpbits(&bs->vlc, tab->len); 685 vl_vlc_needbits(&bs->vlc); 686 dc_diff = vl_vlc_ubits(&bs->vlc, size) - UBITS (SBITS (~bs->vlc.buf, 1), size); 687 vl_vlc_dumpbits(&bs->vlc, size); 688 return dc_diff; 689 } 690} 691 692static inline int 693get_chroma_dc_dct_diff(struct vl_mpg12_bs *bs) 694{ 695 const DCtab * tab; 696 int size; 697 int dc_diff; 698 699 if (bs->vlc.buf < 0xf8000000) { 700 tab = DC_chrom_5 + vl_vlc_ubits(&bs->vlc, 5); 701 size = tab->size; 702 if (size) { 703 bs->vlc.bits += tab->len + size; 704 bs->vlc.buf <<= tab->len; 705 dc_diff = vl_vlc_ubits(&bs->vlc, size) - UBITS (SBITS (~bs->vlc.buf, 1), size); 706 bs->vlc.buf <<= size; 707 return dc_diff; 708 } else { 709 vl_vlc_dumpbits(&bs->vlc, 2); 710 return 0; 711 } 712 } else { 713 tab = DC_long + (vl_vlc_ubits(&bs->vlc, 10) - 0x3e0); 714 size = tab->size; 715 vl_vlc_dumpbits(&bs->vlc, tab->len + 1); 716 vl_vlc_needbits(&bs->vlc); 717 dc_diff = vl_vlc_ubits(&bs->vlc, size) - UBITS (SBITS (~bs->vlc.buf, 1), size); 718 vl_vlc_dumpbits(&bs->vlc, size); 719 return dc_diff; 720 } 721} 722 723static inline void 724get_intra_block_B14(struct vl_mpg12_bs *bs, struct pipe_mpeg12_picture_desc * picture, 725 const int scan[64], int quantizer_scale, short *dest) 726{ 727 int i, j, val; 728 uint8_t *quant_matrix = picture->intra_quantizer_matrix; 729 int mismatch; 730 const DCTtab *tab; 731 732 i = 0; 733 mismatch = ~dest[0]; 734 735 vl_vlc_needbits(&bs->vlc); 736 737 while (1) { 738 if (bs->vlc.buf >= 0x28000000) { 739 740 tab = DCT_B14AC_5 + (vl_vlc_ubits(&bs->vlc, 5) - 5); 741 742 i += tab->run; 743 if (i >= 64) 744 break; /* end of block */ 745 746 normal_code: 747 j = scan[i]; 748 749 bs->vlc.buf <<= tab->len; 750 bs->vlc.bits += tab->len + 1; 751 val = (tab->level * quantizer_scale * quant_matrix[j]) >> 4; 752 753 /* if (bitstream_get (1)) val = -val; */ 754 val = (val ^ vl_vlc_sbits(&bs->vlc, 1)) - vl_vlc_sbits(&bs->vlc, 1); 755 756 SATURATE (val); 757 dest[i] = val; 758 mismatch ^= val; 759 760 bs->vlc.buf <<= 1; 761 vl_vlc_needbits(&bs->vlc); 762 763 continue; 764 765 } else if (bs->vlc.buf >= 0x04000000) { 766 767 tab = DCT_B14_8 + (vl_vlc_ubits(&bs->vlc, 8) - 4); 768 769 i += tab->run; 770 if (i < 64) 771 goto normal_code; 772 773 /* escape code */ 774 775 i += UBITS(bs->vlc.buf << 6, 6) - 64; 776 if (i >= 64) 777 break; /* illegal, check needed to avoid buffer overflow */ 778 779 j = scan[i]; 780 781 vl_vlc_dumpbits(&bs->vlc, 12); 782 vl_vlc_needbits(&bs->vlc); 783 val = (vl_vlc_sbits(&bs->vlc, 12) * quantizer_scale * quant_matrix[j]) / 16; 784 785 SATURATE (val); 786 dest[i] = val; 787 mismatch ^= val; 788 789 vl_vlc_dumpbits(&bs->vlc, 12); 790 vl_vlc_needbits(&bs->vlc); 791 792 continue; 793 794 } else if (bs->vlc.buf >= 0x02000000) { 795 tab = DCT_B14_10 + (vl_vlc_ubits(&bs->vlc, 10) - 8); 796 i += tab->run; 797 if (i < 64) 798 goto normal_code; 799 } else if (bs->vlc.buf >= 0x00800000) { 800 tab = DCT_13 + (vl_vlc_ubits(&bs->vlc, 13) - 16); 801 i += tab->run; 802 if (i < 64) 803 goto normal_code; 804 } else if (bs->vlc.buf >= 0x00200000) { 805 tab = DCT_15 + (vl_vlc_ubits(&bs->vlc, 15) - 16); 806 i += tab->run; 807 if (i < 64) 808 goto normal_code; 809 } else { 810 tab = DCT_16 + vl_vlc_ubits(&bs->vlc, 16); 811 bs->vlc.buf <<= 16; 812 vl_vlc_getword(&bs->vlc, bs->vlc.bits + 16); 813 i += tab->run; 814 if (i < 64) 815 goto normal_code; 816 } 817 break; /* illegal, check needed to avoid buffer overflow */ 818 } 819 820 dest[63] ^= mismatch & 1; 821 vl_vlc_dumpbits(&bs->vlc, 2); /* dump end of block code */ 822} 823 824static inline void 825get_intra_block_B15(struct vl_mpg12_bs *bs, struct pipe_mpeg12_picture_desc * picture, 826 const int scan[64], int quantizer_scale, short *dest) 827{ 828 int i, j, val; 829 uint8_t *quant_matrix = picture->intra_quantizer_matrix; 830 int mismatch; 831 const DCTtab * tab; 832 833 i = 0; 834 mismatch = ~dest[0]; 835 836 vl_vlc_needbits(&bs->vlc); 837 838 while (1) { 839 if (bs->vlc.buf >= 0x04000000) { 840 841 tab = DCT_B15_8 + (vl_vlc_ubits(&bs->vlc, 8) - 4); 842 843 i += tab->run; 844 if (i < 64) { 845 846 normal_code: 847 j = scan[i]; 848 bs->vlc.buf <<= tab->len; 849 bs->vlc.bits += tab->len + 1; 850 val = (tab->level * quantizer_scale * quant_matrix[j]) >> 4; 851 852 /* if (bitstream_get (1)) val = -val; */ 853 val = (val ^ vl_vlc_sbits(&bs->vlc, 1)) - vl_vlc_sbits(&bs->vlc, 1); 854 855 SATURATE (val); 856 dest[i] = val; 857 mismatch ^= val; 858 859 bs->vlc.buf <<= 1; 860 vl_vlc_needbits(&bs->vlc); 861 862 continue; 863 864 } else { 865 866 /* end of block. I commented out this code because if we */ 867 /* dont exit here we will still exit at the later test :) */ 868 869 /* if (i >= 128) break; */ /* end of block */ 870 871 /* escape code */ 872 873 i += UBITS(bs->vlc.buf << 6, 6) - 64; 874 if (i >= 64) 875 break; /* illegal, check against buffer overflow */ 876 877 j = scan[i]; 878 879 vl_vlc_dumpbits(&bs->vlc, 12); 880 vl_vlc_needbits(&bs->vlc); 881 val = (vl_vlc_sbits(&bs->vlc, 12) * quantizer_scale * quant_matrix[j]) / 16; 882 883 SATURATE (val); 884 dest[i] = val; 885 mismatch ^= val; 886 887 vl_vlc_dumpbits(&bs->vlc, 12); 888 vl_vlc_needbits(&bs->vlc); 889 890 continue; 891 892 } 893 } else if (bs->vlc.buf >= 0x02000000) { 894 tab = DCT_B15_10 + (vl_vlc_ubits(&bs->vlc, 10) - 8); 895 i += tab->run; 896 if (i < 64) 897 goto normal_code; 898 } else if (bs->vlc.buf >= 0x00800000) { 899 tab = DCT_13 + (vl_vlc_ubits(&bs->vlc, 13) - 16); 900 i += tab->run; 901 if (i < 64) 902 goto normal_code; 903 } else if (bs->vlc.buf >= 0x00200000) { 904 tab = DCT_15 + (vl_vlc_ubits(&bs->vlc, 15) - 16); 905 i += tab->run; 906 if (i < 64) 907 goto normal_code; 908 } else { 909 tab = DCT_16 + vl_vlc_ubits(&bs->vlc, 16); 910 bs->vlc.buf <<= 16; 911 vl_vlc_getword(&bs->vlc, bs->vlc.bits + 16); 912 i += tab->run; 913 if (i < 64) 914 goto normal_code; 915 } 916 break; /* illegal, check needed to avoid buffer overflow */ 917 } 918 919 dest[63] ^= mismatch & 1; 920 vl_vlc_dumpbits(&bs->vlc, 4); /* dump end of block code */ 921} 922 923static inline void 924get_non_intra_block(struct vl_mpg12_bs *bs, struct pipe_mpeg12_picture_desc * picture, 925 const int scan[64], int quantizer_scale, short *dest) 926{ 927 int i, j, val; 928 uint8_t *quant_matrix = picture->non_intra_quantizer_matrix; 929 int mismatch; 930 const DCTtab *tab; 931 932 i = -1; 933 mismatch = 1; 934 935 vl_vlc_needbits(&bs->vlc); 936 if (bs->vlc.buf >= 0x28000000) { 937 tab = DCT_B14DC_5 + (vl_vlc_ubits(&bs->vlc, 5) - 5); 938 goto entry_1; 939 } else 940 goto entry_2; 941 942 while (1) { 943 if (bs->vlc.buf >= 0x28000000) { 944 945 tab = DCT_B14AC_5 + (vl_vlc_ubits(&bs->vlc, 5) - 5); 946 947 entry_1: 948 i += tab->run; 949 if (i >= 64) 950 break; /* end of block */ 951 952 normal_code: 953 j = scan[i]; 954 bs->vlc.buf <<= tab->len; 955 bs->vlc.bits += tab->len + 1; 956 val = ((2*tab->level+1) * quantizer_scale * quant_matrix[j]) >> 5; 957 958 /* if (bitstream_get (1)) val = -val; */ 959 val = (val ^ vl_vlc_sbits(&bs->vlc, 1)) - vl_vlc_sbits(&bs->vlc, 1); 960 961 SATURATE (val); 962 dest[i] = val; 963 mismatch ^= val; 964 965 bs->vlc.buf <<= 1; 966 vl_vlc_needbits(&bs->vlc); 967 968 continue; 969 970 } 971 972 entry_2: 973 if (bs->vlc.buf >= 0x04000000) { 974 975 tab = DCT_B14_8 + (vl_vlc_ubits(&bs->vlc, 8) - 4); 976 977 i += tab->run; 978 if (i < 64) 979 goto normal_code; 980 981 /* escape code */ 982 983 i += UBITS(bs->vlc.buf << 6, 6) - 64; 984 if (i >= 64) 985 break; /* illegal, check needed to avoid buffer overflow */ 986 987 j = scan[i]; 988 989 vl_vlc_dumpbits(&bs->vlc, 12); 990 vl_vlc_needbits(&bs->vlc); 991 val = 2 * (vl_vlc_sbits(&bs->vlc, 12) + vl_vlc_sbits(&bs->vlc, 1)) + 1; 992 val = (val * quantizer_scale * quant_matrix[j]) / 32; 993 994 SATURATE (val); 995 dest[i] = val; 996 mismatch ^= val; 997 998 vl_vlc_dumpbits(&bs->vlc, 12); 999 vl_vlc_needbits(&bs->vlc); 1000 1001 continue; 1002 1003 } else if (bs->vlc.buf >= 0x02000000) { 1004 tab = DCT_B14_10 + (vl_vlc_ubits(&bs->vlc, 10) - 8); 1005 i += tab->run; 1006 if (i < 64) 1007 goto normal_code; 1008 } else if (bs->vlc.buf >= 0x00800000) { 1009 tab = DCT_13 + (vl_vlc_ubits(&bs->vlc, 13) - 16); 1010 i += tab->run; 1011 if (i < 64) 1012 goto normal_code; 1013 } else if (bs->vlc.buf >= 0x00200000) { 1014 tab = DCT_15 + (vl_vlc_ubits(&bs->vlc, 15) - 16); 1015 i += tab->run; 1016 if (i < 64) 1017 goto normal_code; 1018 } else { 1019 tab = DCT_16 + vl_vlc_ubits(&bs->vlc, 16); 1020 bs->vlc.buf <<= 16; 1021 vl_vlc_getword(&bs->vlc, bs->vlc.bits + 16); 1022 i += tab->run; 1023 if (i < 64) 1024 goto normal_code; 1025 } 1026 break; /* illegal, check needed to avoid buffer overflow */ 1027 } 1028 dest[63] ^= mismatch & 1; 1029 vl_vlc_dumpbits(&bs->vlc, 2); /* dump end of block code */ 1030} 1031 1032static inline void 1033get_mpeg1_intra_block(struct vl_mpg12_bs *bs, struct pipe_mpeg12_picture_desc * picture, 1034 const int scan[64], int quantizer_scale, short *dest) 1035{ 1036 int i, j, val; 1037 uint8_t *quant_matrix = picture->intra_quantizer_matrix; 1038 const DCTtab * tab; 1039 1040 i = 0; 1041 1042 vl_vlc_needbits(&bs->vlc); 1043 1044 while (1) { 1045 if (bs->vlc.buf >= 0x28000000) { 1046 1047 tab = DCT_B14AC_5 + (vl_vlc_ubits(&bs->vlc, 5) - 5); 1048 1049 i += tab->run; 1050 if (i >= 64) 1051 break; /* end of block */ 1052 1053 normal_code: 1054 j = scan[i]; 1055 bs->vlc.buf <<= tab->len; 1056 bs->vlc.bits += tab->len + 1; 1057 val = (tab->level * quantizer_scale * quant_matrix[j]) >> 4; 1058 1059 /* oddification */ 1060 val = (val - 1) | 1; 1061 1062 /* if (bitstream_get (1)) val = -val; */ 1063 val = (val ^ vl_vlc_sbits(&bs->vlc, 1)) - vl_vlc_sbits(&bs->vlc, 1); 1064 1065 SATURATE (val); 1066 dest[i] = val; 1067 1068 bs->vlc.buf <<= 1; 1069 vl_vlc_needbits(&bs->vlc); 1070 1071 continue; 1072 1073 } else if (bs->vlc.buf >= 0x04000000) { 1074 1075 tab = DCT_B14_8 + (vl_vlc_ubits(&bs->vlc, 8) - 4); 1076 1077 i += tab->run; 1078 if (i < 64) 1079 goto normal_code; 1080 1081 /* escape code */ 1082 1083 i += UBITS(bs->vlc.buf << 6, 6) - 64; 1084 if (i >= 64) 1085 break; /* illegal, check needed to avoid buffer overflow */ 1086 1087 j = scan[i]; 1088 1089 vl_vlc_dumpbits(&bs->vlc, 12); 1090 vl_vlc_needbits(&bs->vlc); 1091 val = vl_vlc_sbits(&bs->vlc, 8); 1092 if (! (val & 0x7f)) { 1093 vl_vlc_dumpbits(&bs->vlc, 8); 1094 val = vl_vlc_ubits(&bs->vlc, 8) + 2 * val; 1095 } 1096 val = (val * quantizer_scale * quant_matrix[j]) / 16; 1097 1098 /* oddification */ 1099 val = (val + ~SBITS (val, 1)) | 1; 1100 1101 SATURATE (val); 1102 dest[i] = val; 1103 1104 vl_vlc_dumpbits(&bs->vlc, 8); 1105 vl_vlc_needbits(&bs->vlc); 1106 1107 continue; 1108 1109 } else if (bs->vlc.buf >= 0x02000000) { 1110 tab = DCT_B14_10 + (vl_vlc_ubits(&bs->vlc, 10) - 8); 1111 i += tab->run; 1112 if (i < 64) 1113 goto normal_code; 1114 } else if (bs->vlc.buf >= 0x00800000) { 1115 tab = DCT_13 + (vl_vlc_ubits(&bs->vlc, 13) - 16); 1116 i += tab->run; 1117 if (i < 64) 1118 goto normal_code; 1119 } else if (bs->vlc.buf >= 0x00200000) { 1120 tab = DCT_15 + (vl_vlc_ubits(&bs->vlc, 15) - 16); 1121 i += tab->run; 1122 if (i < 64) 1123 goto normal_code; 1124 } else { 1125 tab = DCT_16 + vl_vlc_ubits(&bs->vlc, 16); 1126 bs->vlc.buf <<= 16; 1127 vl_vlc_getword(&bs->vlc, bs->vlc.bits + 16); 1128 i += tab->run; 1129 if (i < 64) 1130 goto normal_code; 1131 } 1132 break; /* illegal, check needed to avoid buffer overflow */ 1133 } 1134 vl_vlc_dumpbits(&bs->vlc, 2); /* dump end of block code */ 1135} 1136 1137static inline void 1138get_mpeg1_non_intra_block(struct vl_mpg12_bs *bs, struct pipe_mpeg12_picture_desc * picture, 1139 const int scan[64], int quantizer_scale, short *dest) 1140{ 1141 int i, j, val; 1142 uint8_t *quant_matrix = picture->non_intra_quantizer_matrix; 1143 const DCTtab * tab; 1144 1145 i = -1; 1146 1147 vl_vlc_needbits(&bs->vlc); 1148 if (bs->vlc.buf >= 0x28000000) { 1149 tab = DCT_B14DC_5 + (vl_vlc_ubits(&bs->vlc, 5) - 5); 1150 goto entry_1; 1151 } else 1152 goto entry_2; 1153 1154 while (1) { 1155 if (bs->vlc.buf >= 0x28000000) { 1156 1157 tab = DCT_B14AC_5 + (vl_vlc_ubits(&bs->vlc, 5) - 5); 1158 1159 entry_1: 1160 i += tab->run; 1161 if (i >= 64) 1162 break; /* end of block */ 1163 1164 normal_code: 1165 j = scan[i]; 1166 bs->vlc.buf <<= tab->len; 1167 bs->vlc.bits += tab->len + 1; 1168 val = ((2*tab->level+1) * quantizer_scale * quant_matrix[j]) >> 5; 1169 1170 /* oddification */ 1171 val = (val - 1) | 1; 1172 1173 /* if (bitstream_get (1)) val = -val; */ 1174 val = (val ^ vl_vlc_sbits(&bs->vlc, 1)) - vl_vlc_sbits(&bs->vlc, 1); 1175 1176 SATURATE (val); 1177 dest[i] = val; 1178 1179 bs->vlc.buf <<= 1; 1180 vl_vlc_needbits(&bs->vlc); 1181 1182 continue; 1183 1184 } 1185 1186 entry_2: 1187 if (bs->vlc.buf >= 0x04000000) { 1188 1189 tab = DCT_B14_8 + (vl_vlc_ubits(&bs->vlc, 8) - 4); 1190 1191 i += tab->run; 1192 if (i < 64) 1193 goto normal_code; 1194 1195 /* escape code */ 1196 1197 i += UBITS(bs->vlc.buf << 6, 6) - 64; 1198 if (i >= 64) 1199 break; /* illegal, check needed to avoid buffer overflow */ 1200 1201 j = scan[i]; 1202 1203 vl_vlc_dumpbits(&bs->vlc, 12); 1204 vl_vlc_needbits(&bs->vlc); 1205 val = vl_vlc_sbits(&bs->vlc, 8); 1206 if (! (val & 0x7f)) { 1207 vl_vlc_dumpbits(&bs->vlc, 8); 1208 val = vl_vlc_ubits(&bs->vlc, 8) + 2 * val; 1209 } 1210 val = 2 * (val + SBITS (val, 1)) + 1; 1211 val = (val * quantizer_scale * quant_matrix[j]) / 32; 1212 1213 /* oddification */ 1214 val = (val + ~SBITS (val, 1)) | 1; 1215 1216 SATURATE (val); 1217 dest[i] = val; 1218 1219 vl_vlc_dumpbits(&bs->vlc, 8); 1220 vl_vlc_needbits(&bs->vlc); 1221 1222 continue; 1223 1224 } else if (bs->vlc.buf >= 0x02000000) { 1225 tab = DCT_B14_10 + (vl_vlc_ubits(&bs->vlc, 10) - 8); 1226 i += tab->run; 1227 if (i < 64) 1228 goto normal_code; 1229 } else if (bs->vlc.buf >= 0x00800000) { 1230 tab = DCT_13 + (vl_vlc_ubits(&bs->vlc, 13) - 16); 1231 i += tab->run; 1232 if (i < 64) 1233 goto normal_code; 1234 } else if (bs->vlc.buf >= 0x00200000) { 1235 tab = DCT_15 + (vl_vlc_ubits(&bs->vlc, 15) - 16); 1236 i += tab->run; 1237 if (i < 64) 1238 goto normal_code; 1239 } else { 1240 tab = DCT_16 + vl_vlc_ubits(&bs->vlc, 16); 1241 bs->vlc.buf <<= 16; 1242 vl_vlc_getword(&bs->vlc, bs->vlc.bits + 16); 1243 i += tab->run; 1244 if (i < 64) 1245 goto normal_code; 1246 } 1247 break; /* illegal, check needed to avoid buffer overflow */ 1248 } 1249 vl_vlc_dumpbits(&bs->vlc, 2); /* dump end of block code */ 1250} 1251 1252static inline void 1253slice_intra_DCT(struct vl_mpg12_bs *bs, struct pipe_mpeg12_picture_desc * picture, const int scan[64], int cc, 1254 unsigned x, unsigned y, enum pipe_mpeg12_dct_type coding, int quantizer_scale, int dc_dct_pred[3]) 1255{ 1256 short dest[64]; 1257 1258 bs->ycbcr_stream[cc]->x = x; 1259 bs->ycbcr_stream[cc]->y = y; 1260 bs->ycbcr_stream[cc]->intra = PIPE_MPEG12_DCT_INTRA; 1261 bs->ycbcr_stream[cc]->coding = coding; 1262 1263 vl_vlc_needbits(&bs->vlc); 1264 1265 /* Get the intra DC coefficient and inverse quantize it */ 1266 if (cc == 0) 1267 dc_dct_pred[0] += get_luma_dc_dct_diff(bs); 1268 else 1269 dc_dct_pred[cc] += get_chroma_dc_dct_diff(bs); 1270 1271 memset(dest, 0, sizeof(int16_t) * 64); 1272 dest[0] = dc_dct_pred[cc] << (3 - picture->intra_dc_precision); 1273 if (picture->mpeg1) { 1274 if (picture->picture_coding_type != D_TYPE) 1275 get_mpeg1_intra_block(bs, picture, scan, quantizer_scale, dest); 1276 } else if (picture->intra_vlc_format) 1277 get_intra_block_B15(bs, picture, scan, quantizer_scale, dest); 1278 else 1279 get_intra_block_B14(bs, picture, scan, quantizer_scale, dest); 1280 1281 memcpy(bs->ycbcr_buffer[cc], dest, sizeof(int16_t) * 64); 1282 1283 bs->num_ycbcr_blocks[cc]++; 1284 bs->ycbcr_stream[cc]++; 1285 bs->ycbcr_buffer[cc] += 64; 1286} 1287 1288static inline void 1289slice_non_intra_DCT(struct vl_mpg12_bs *bs, struct pipe_mpeg12_picture_desc * picture, const int scan[64], int cc, 1290 unsigned x, unsigned y, int quantizer_scale, enum pipe_mpeg12_dct_type coding) 1291{ 1292 short dest[64]; 1293 1294 bs->ycbcr_stream[cc]->x = x; 1295 bs->ycbcr_stream[cc]->y = y; 1296 bs->ycbcr_stream[cc]->intra = PIPE_MPEG12_DCT_DELTA; 1297 bs->ycbcr_stream[cc]->coding = coding; 1298 1299 memset(dest, 0, sizeof(int16_t) * 64); 1300 if (picture->mpeg1) 1301 get_mpeg1_non_intra_block(bs, picture, scan, quantizer_scale, dest); 1302 else 1303 get_non_intra_block(bs, picture, scan, quantizer_scale, dest); 1304 1305 memcpy(bs->ycbcr_buffer[cc], dest, sizeof(int16_t) * 64); 1306 1307 bs->num_ycbcr_blocks[cc]++; 1308 bs->ycbcr_stream[cc]++; 1309 bs->ycbcr_buffer[cc] += 64; 1310} 1311 1312static inline void 1313motion_mp1(struct vl_mpg12_bs *bs, unsigned f_code[2], struct pipe_motionvector *mv) 1314{ 1315 int motion_x, motion_y; 1316 1317 mv->top.field_select = mv->bottom.field_select = PIPE_VIDEO_FRAME; 1318 1319 vl_vlc_needbits(&bs->vlc); 1320 motion_x = (mv->top.x + (get_motion_delta(bs, f_code[0]) << f_code[1])); 1321 motion_x = bound_motion_vector (motion_x, f_code[0] + f_code[1]); 1322 mv->top.x = mv->bottom.x = motion_x; 1323 1324 vl_vlc_needbits(&bs->vlc); 1325 motion_y = (mv->top.y + (get_motion_delta(bs, f_code[0]) << f_code[1])); 1326 motion_y = bound_motion_vector (motion_y, f_code[0] + f_code[1]); 1327 mv->top.y = mv->bottom.y = motion_y; 1328} 1329 1330static inline void 1331motion_fr_frame(struct vl_mpg12_bs *bs, unsigned f_code[2], struct pipe_motionvector *mv) 1332{ 1333 int motion_x, motion_y; 1334 1335 mv->top.field_select = mv->bottom.field_select = PIPE_VIDEO_FRAME; 1336 1337 vl_vlc_needbits(&bs->vlc); 1338 motion_x = mv->top.x + get_motion_delta(bs, f_code[0]); 1339 motion_x = bound_motion_vector(motion_x, f_code[0]); 1340 mv->top.x = mv->bottom.x = motion_x; 1341 1342 vl_vlc_needbits(&bs->vlc); 1343 motion_y = mv->top.y + get_motion_delta(bs, f_code[1]); 1344 motion_y = bound_motion_vector(motion_y, f_code[1]); 1345 mv->top.y = mv->bottom.y = motion_y; 1346} 1347 1348static inline void 1349motion_fr_field(struct vl_mpg12_bs *bs, unsigned f_code[2], struct pipe_motionvector *mv) 1350{ 1351 int motion_x, motion_y; 1352 1353 vl_vlc_needbits(&bs->vlc); 1354 mv->top.field_select = vl_vlc_ubits(&bs->vlc, 1) ? 1355 PIPE_VIDEO_BOTTOM_FIELD : PIPE_VIDEO_TOP_FIELD; 1356 vl_vlc_dumpbits(&bs->vlc, 1); 1357 1358 motion_x = mv->top.x + get_motion_delta(bs, f_code[0]); 1359 motion_x = bound_motion_vector (motion_x, f_code[0]); 1360 mv->top.x = motion_x; 1361 1362 vl_vlc_needbits(&bs->vlc); 1363 motion_y = (mv->top.y >> 1) + get_motion_delta(bs, f_code[1]); 1364 /* motion_y = bound_motion_vector (motion_y, f_code[1]); */ 1365 mv->top.y = motion_y << 1; 1366 1367 vl_vlc_needbits(&bs->vlc); 1368 mv->bottom.field_select = vl_vlc_ubits(&bs->vlc, 1) ? 1369 PIPE_VIDEO_BOTTOM_FIELD : PIPE_VIDEO_TOP_FIELD; 1370 vl_vlc_dumpbits(&bs->vlc, 1); 1371 1372 motion_x = mv->bottom.x + get_motion_delta(bs, f_code[0]); 1373 motion_x = bound_motion_vector (motion_x, f_code[0]); 1374 mv->bottom.x = motion_x; 1375 1376 vl_vlc_needbits(&bs->vlc); 1377 motion_y = (mv->bottom.y >> 1) + get_motion_delta(bs, f_code[1]); 1378 /* motion_y = bound_motion_vector (motion_y, f_code[1]); */ 1379 mv->bottom.y = motion_y << 1; 1380} 1381 1382static inline void 1383motion_fr_dmv(struct vl_mpg12_bs *bs, unsigned f_code[2], struct pipe_motionvector *mv) 1384{ 1385 int motion_x, motion_y; 1386 1387 // TODO Implement dmv 1388 mv->top.field_select = mv->bottom.field_select = PIPE_VIDEO_FRAME; 1389 1390 vl_vlc_needbits(&bs->vlc); 1391 motion_x = mv->top.x + get_motion_delta(bs, f_code[0]); 1392 motion_x = bound_motion_vector(motion_x, f_code[0]); 1393 mv->top.x = mv->bottom.x = motion_x; 1394 1395 vl_vlc_needbits(&bs->vlc); 1396 motion_y = (mv->top.y >> 1) + get_motion_delta(bs, f_code[1]); 1397 /* motion_y = bound_motion_vector (motion_y, f_code[1]); */ 1398 mv->top.y = mv->bottom.y = motion_y << 1; 1399} 1400 1401/* like motion_frame, but parsing without actual motion compensation */ 1402static inline void 1403motion_fr_conceal(struct vl_mpg12_bs *bs, unsigned f_code[2], struct pipe_motionvector *mv) 1404{ 1405 int tmp; 1406 1407 mv->top.field_select = mv->bottom.field_select = PIPE_VIDEO_FRAME; 1408 1409 vl_vlc_needbits(&bs->vlc); 1410 tmp = (mv->top.x + get_motion_delta(bs, f_code[0])); 1411 tmp = bound_motion_vector (tmp, f_code[0]); 1412 mv->top.x = mv->bottom.x = tmp; 1413 1414 vl_vlc_needbits(&bs->vlc); 1415 tmp = (mv->top.y + get_motion_delta(bs, f_code[1])); 1416 tmp = bound_motion_vector (tmp, f_code[1]); 1417 mv->top.y = mv->bottom.y = tmp; 1418 1419 vl_vlc_dumpbits(&bs->vlc, 1); /* remove marker_bit */ 1420} 1421 1422static inline void 1423motion_fi_field(struct vl_mpg12_bs *bs, unsigned f_code[2], struct pipe_motionvector *mv) 1424{ 1425 int motion_x, motion_y; 1426 1427 vl_vlc_needbits(&bs->vlc); 1428 1429 // ref_field 1430 //vl_vlc_ubits(&bs->vlc, 1); 1431 1432 // TODO field select may need to do something here for bob (weave ok) 1433 mv->top.field_select = mv->bottom.field_select = PIPE_VIDEO_FRAME; 1434 vl_vlc_dumpbits(&bs->vlc, 1); 1435 1436 motion_x = mv->top.x + get_motion_delta(bs, f_code[0]); 1437 motion_x = bound_motion_vector (motion_x, f_code[0]); 1438 mv->top.x = mv->bottom.x = motion_x; 1439 1440 vl_vlc_needbits(&bs->vlc); 1441 motion_y = mv->top.y + get_motion_delta(bs, f_code[1]); 1442 motion_y = bound_motion_vector (motion_y, f_code[1]); 1443 mv->top.y = mv->bottom.y = motion_y; 1444} 1445 1446static inline void 1447motion_fi_16x8(struct vl_mpg12_bs *bs, unsigned f_code[2], struct pipe_motionvector *mv) 1448{ 1449 int motion_x, motion_y; 1450 1451 vl_vlc_needbits(&bs->vlc); 1452 1453 // ref_field 1454 //vl_vlc_ubits(&bs->vlc, 1); 1455 1456 // TODO field select may need to do something here bob (weave ok) 1457 mv->top.field_select = PIPE_VIDEO_FRAME; 1458 vl_vlc_dumpbits(&bs->vlc, 1); 1459 1460 motion_x = mv->top.x + get_motion_delta(bs, f_code[0]); 1461 motion_x = bound_motion_vector (motion_x, f_code[0]); 1462 mv->top.x = motion_x; 1463 1464 vl_vlc_needbits(&bs->vlc); 1465 motion_y = mv->top.y + get_motion_delta(bs, f_code[1]); 1466 motion_y = bound_motion_vector (motion_y, f_code[1]); 1467 mv->top.y = motion_y; 1468 1469 vl_vlc_needbits(&bs->vlc); 1470 // ref_field 1471 //vl_vlc_ubits(&bs->vlc, 1); 1472 1473 // TODO field select may need to do something here for bob (weave ok) 1474 mv->bottom.field_select = PIPE_VIDEO_FRAME; 1475 vl_vlc_dumpbits(&bs->vlc, 1); 1476 1477 motion_x = mv->bottom.x + get_motion_delta(bs, f_code[0]); 1478 motion_x = bound_motion_vector (motion_x, f_code[0]); 1479 mv->bottom.x = motion_x; 1480 1481 vl_vlc_needbits(&bs->vlc); 1482 motion_y = mv->bottom.y + get_motion_delta(bs, f_code[1]); 1483 motion_y = bound_motion_vector (motion_y, f_code[1]); 1484 mv->bottom.y = motion_y; 1485} 1486 1487static inline void 1488motion_fi_dmv(struct vl_mpg12_bs *bs, unsigned f_code[2], struct pipe_motionvector *mv) 1489{ 1490 int motion_x, motion_y; 1491 1492 // TODO field select may need to do something here for bob (weave ok) 1493 mv->top.field_select = mv->bottom.field_select = PIPE_VIDEO_FRAME; 1494 1495 vl_vlc_needbits(&bs->vlc); 1496 motion_x = mv->top.x + get_motion_delta(bs, f_code[0]); 1497 motion_x = bound_motion_vector (motion_x, f_code[0]); 1498 mv->top.x = mv->bottom.x = motion_x; 1499 1500 vl_vlc_needbits(&bs->vlc); 1501 motion_y = mv->top.y + get_motion_delta(bs, f_code[1]); 1502 motion_y = bound_motion_vector (motion_y, f_code[1]); 1503 mv->top.y = mv->bottom.y = motion_y; 1504} 1505 1506 1507static inline void 1508motion_fi_conceal(struct vl_mpg12_bs *bs, unsigned f_code[2], struct pipe_motionvector *mv) 1509{ 1510 int tmp; 1511 1512 vl_vlc_needbits(&bs->vlc); 1513 vl_vlc_dumpbits(&bs->vlc, 1); /* remove field_select */ 1514 1515 tmp = (mv->top.x + get_motion_delta(bs, f_code[0])); 1516 tmp = bound_motion_vector(tmp, f_code[0]); 1517 mv->top.x = mv->bottom.x = tmp; 1518 1519 vl_vlc_needbits(&bs->vlc); 1520 tmp = (mv->top.y + get_motion_delta(bs, f_code[1])); 1521 tmp = bound_motion_vector(tmp, f_code[1]); 1522 mv->top.y = mv->bottom.y = tmp; 1523 1524 vl_vlc_dumpbits(&bs->vlc, 1); /* remove marker_bit */ 1525} 1526 1527#define MOTION_CALL(routine, macroblock_modes) \ 1528do { \ 1529 if ((macroblock_modes) & MACROBLOCK_MOTION_FORWARD) \ 1530 routine(bs, picture->f_code[0], &mv_fwd); \ 1531 if ((macroblock_modes) & MACROBLOCK_MOTION_BACKWARD) \ 1532 routine(bs, picture->f_code[1], &mv_bwd); \ 1533} while (0) 1534 1535#define NEXT_MACROBLOCK \ 1536do { \ 1537 bs->mv_stream[0][x+y*bs->width] = mv_fwd; \ 1538 bs->mv_stream[1][x+y*bs->width] = mv_bwd; \ 1539 ++x; \ 1540 if (x == bs->width) { \ 1541 ++y; \ 1542 if (y >= bs->height) \ 1543 return false; \ 1544 x = 0; \ 1545 } \ 1546} while (0) 1547 1548static inline bool 1549slice_init(struct vl_mpg12_bs *bs, struct pipe_mpeg12_picture_desc * picture, 1550 int *quantizer_scale, int *x, int *y) 1551{ 1552 const MBAtab * mba; 1553 1554 vl_vlc_need32bits(&bs->vlc); 1555 while(bs->vlc.buf < 0x101 || bs->vlc.buf > 0x1AF) { 1556 if(!vl_vlc_getbyte(&bs->vlc)) 1557 return false; 1558 } 1559 *y = (bs->vlc.buf & 0xFF) - 1; 1560 vl_vlc_restart(&bs->vlc); 1561 1562 *quantizer_scale = get_quantizer_scale(bs, picture); 1563 1564 /* ignore intra_slice and all the extra data */ 1565 while (bs->vlc.buf & 0x80000000) { 1566 vl_vlc_dumpbits(&bs->vlc, 9); 1567 vl_vlc_needbits(&bs->vlc); 1568 } 1569 1570 /* decode initial macroblock address increment */ 1571 *x = 0; 1572 while (1) { 1573 if (bs->vlc.buf >= 0x08000000) { 1574 mba = MBA_5 + (vl_vlc_ubits(&bs->vlc, 6) - 2); 1575 break; 1576 } else if (bs->vlc.buf >= 0x01800000) { 1577 mba = MBA_11 + (vl_vlc_ubits(&bs->vlc, 12) - 24); 1578 break; 1579 } else switch (vl_vlc_ubits(&bs->vlc, 12)) { 1580 case 8: /* macroblock_escape */ 1581 *x += 33; 1582 vl_vlc_dumpbits(&bs->vlc, 11); 1583 vl_vlc_needbits(&bs->vlc); 1584 continue; 1585 case 15: /* macroblock_stuffing (MPEG1 only) */ 1586 bs->vlc.buf &= 0xfffff; 1587 vl_vlc_dumpbits(&bs->vlc, 11); 1588 vl_vlc_needbits(&bs->vlc); 1589 continue; 1590 default: /* error */ 1591 return false; 1592 } 1593 } 1594 vl_vlc_dumpbits(&bs->vlc, mba->len + 1); 1595 *x += mba->mba; 1596 1597 while (*x >= bs->width) { 1598 *x -= bs->width; 1599 (*y)++; 1600 } 1601 if (*y > bs->height) 1602 return false; 1603 1604 return true; 1605} 1606 1607static inline bool 1608decode_slice(struct vl_mpg12_bs *bs, struct pipe_mpeg12_picture_desc *picture, const int scan[64]) 1609{ 1610 enum pipe_video_field_select default_field_select; 1611 struct pipe_motionvector mv_fwd, mv_bwd; 1612 enum pipe_mpeg12_dct_type dct_type; 1613 1614 /* predictor for DC coefficients in intra blocks */ 1615 int dc_dct_pred[3] = { 0, 0, 0 }; 1616 int quantizer_scale; 1617 1618 int x, y; 1619 1620 switch(picture->picture_structure) { 1621 case TOP_FIELD: 1622 default_field_select = PIPE_VIDEO_TOP_FIELD; 1623 break; 1624 1625 case BOTTOM_FIELD: 1626 default_field_select = PIPE_VIDEO_BOTTOM_FIELD; 1627 break; 1628 1629 default: 1630 default_field_select = PIPE_VIDEO_FRAME; 1631 break; 1632 } 1633 1634 if (!slice_init(bs, picture, &quantizer_scale, &x, &y)) 1635 return false; 1636 1637 mv_fwd.top.x = mv_fwd.top.y = mv_fwd.bottom.x = mv_fwd.bottom.y = 0; 1638 mv_fwd.top.field_select = mv_fwd.bottom.field_select = default_field_select; 1639 1640 mv_bwd.top.x = mv_bwd.top.y = mv_bwd.bottom.x = mv_bwd.bottom.y = 0; 1641 mv_bwd.top.field_select = mv_bwd.bottom.field_select = default_field_select; 1642 1643 while (1) { 1644 int macroblock_modes; 1645 int mba_inc; 1646 const MBAtab * mba; 1647 1648 vl_vlc_needbits(&bs->vlc); 1649 1650 macroblock_modes = get_macroblock_modes(bs, picture); 1651 dct_type = get_dct_type(bs, picture, macroblock_modes); 1652 1653 switch(macroblock_modes & (MACROBLOCK_MOTION_FORWARD|MACROBLOCK_MOTION_BACKWARD)) { 1654 case (MACROBLOCK_MOTION_FORWARD|MACROBLOCK_MOTION_BACKWARD): 1655 mv_fwd.top.weight = mv_fwd.bottom.weight = PIPE_VIDEO_MV_WEIGHT_HALF; 1656 mv_bwd.top.weight = mv_bwd.bottom.weight = PIPE_VIDEO_MV_WEIGHT_HALF; 1657 break; 1658 1659 default: 1660 mv_fwd.top.field_select = mv_fwd.bottom.field_select = default_field_select; 1661 mv_bwd.top.field_select = mv_bwd.bottom.field_select = default_field_select; 1662 1663 /* fall through */ 1664 case MACROBLOCK_MOTION_FORWARD: 1665 mv_fwd.top.weight = mv_fwd.bottom.weight = PIPE_VIDEO_MV_WEIGHT_MAX; 1666 mv_bwd.top.weight = mv_bwd.bottom.weight = PIPE_VIDEO_MV_WEIGHT_MIN; 1667 break; 1668 1669 case MACROBLOCK_MOTION_BACKWARD: 1670 mv_fwd.top.weight = mv_fwd.bottom.weight = PIPE_VIDEO_MV_WEIGHT_MIN; 1671 mv_bwd.top.weight = mv_bwd.bottom.weight = PIPE_VIDEO_MV_WEIGHT_MAX; 1672 break; 1673 } 1674 1675 /* maybe integrate MACROBLOCK_QUANT test into get_macroblock_modes ? */ 1676 if (macroblock_modes & MACROBLOCK_QUANT) 1677 quantizer_scale = get_quantizer_scale(bs, picture); 1678 1679 if (macroblock_modes & MACROBLOCK_INTRA) { 1680 1681 if (picture->concealment_motion_vectors) { 1682 if (picture->picture_structure == FRAME_PICTURE) 1683 motion_fr_conceal(bs, picture->f_code[0], &mv_fwd); 1684 else 1685 motion_fi_conceal(bs, picture->f_code[0], &mv_fwd); 1686 1687 } else { 1688 mv_fwd.top.x = mv_fwd.top.y = mv_fwd.bottom.x = mv_fwd.bottom.y = 0; 1689 mv_bwd.top.x = mv_bwd.top.y = mv_bwd.bottom.x = mv_bwd.bottom.y = 0; 1690 } 1691 mv_fwd.top.weight = mv_fwd.bottom.weight = PIPE_VIDEO_MV_WEIGHT_MIN; 1692 mv_bwd.top.weight = mv_bwd.bottom.weight = PIPE_VIDEO_MV_WEIGHT_MIN; 1693 1694 // unravaled loop of 6 block(i) calls in macroblock() 1695 slice_intra_DCT(bs, picture, scan, 0, x*2+0, y*2+0, dct_type, quantizer_scale, dc_dct_pred); 1696 slice_intra_DCT(bs, picture, scan, 0, x*2+1, y*2+0, dct_type, quantizer_scale, dc_dct_pred); 1697 slice_intra_DCT(bs, picture, scan, 0, x*2+0, y*2+1, dct_type, quantizer_scale, dc_dct_pred); 1698 slice_intra_DCT(bs, picture, scan, 0, x*2+1, y*2+1, dct_type, quantizer_scale, dc_dct_pred); 1699 slice_intra_DCT(bs, picture, scan, 1, x, y, PIPE_MPEG12_DCT_TYPE_FRAME, quantizer_scale, dc_dct_pred); 1700 slice_intra_DCT(bs, picture, scan, 2, x, y, PIPE_MPEG12_DCT_TYPE_FRAME, quantizer_scale, dc_dct_pred); 1701 1702 if (picture->picture_coding_type == D_TYPE) { 1703 vl_vlc_needbits(&bs->vlc); 1704 vl_vlc_dumpbits(&bs->vlc, 1); 1705 } 1706 1707 } else { 1708 if (picture->picture_structure == FRAME_PICTURE) 1709 switch (macroblock_modes & MOTION_TYPE_MASK) { 1710 case MC_FRAME: 1711 if (picture->mpeg1) { 1712 MOTION_CALL(motion_mp1, macroblock_modes); 1713 } else { 1714 MOTION_CALL(motion_fr_frame, macroblock_modes); 1715 } 1716 break; 1717 1718 case MC_FIELD: 1719 MOTION_CALL (motion_fr_field, macroblock_modes); 1720 break; 1721 1722 case MC_DMV: 1723 MOTION_CALL (motion_fr_dmv, MACROBLOCK_MOTION_FORWARD); 1724 break; 1725 1726 case 0: 1727 /* non-intra mb without forward mv in a P picture */ 1728 mv_fwd.top.x = mv_fwd.top.y = mv_fwd.bottom.x = mv_fwd.bottom.y = 0; 1729 mv_bwd.top.x = mv_bwd.top.y = mv_bwd.bottom.x = mv_bwd.bottom.y = 0; 1730 break; 1731 } 1732 else 1733 switch (macroblock_modes & MOTION_TYPE_MASK) { 1734 case MC_FIELD: 1735 MOTION_CALL (motion_fi_field, macroblock_modes); 1736 break; 1737 1738 case MC_16X8: 1739 MOTION_CALL (motion_fi_16x8, macroblock_modes); 1740 break; 1741 1742 case MC_DMV: 1743 MOTION_CALL (motion_fi_dmv, MACROBLOCK_MOTION_FORWARD); 1744 break; 1745 1746 case 0: 1747 /* non-intra mb without forward mv in a P picture */ 1748 mv_fwd.top.x = mv_fwd.top.y = mv_fwd.bottom.x = mv_fwd.bottom.y = 0; 1749 mv_bwd.top.x = mv_bwd.top.y = mv_bwd.bottom.x = mv_bwd.bottom.y = 0; 1750 break; 1751 } 1752 1753 if (macroblock_modes & MACROBLOCK_PATTERN) { 1754 int coded_block_pattern = get_coded_block_pattern(bs); 1755 1756 // TODO optimize not fully used for idct accel only mc. 1757 if (coded_block_pattern & 0x20) 1758 slice_non_intra_DCT(bs, picture, scan, 0, x*2+0, y*2+0, quantizer_scale, dct_type); // cc0 luma 0 1759 if (coded_block_pattern & 0x10) 1760 slice_non_intra_DCT(bs, picture, scan, 0, x*2+1, y*2+0, quantizer_scale, dct_type); // cc0 luma 1 1761 if (coded_block_pattern & 0x08) 1762 slice_non_intra_DCT(bs, picture, scan, 0, x*2+0, y*2+1, quantizer_scale, dct_type); // cc0 luma 2 1763 if (coded_block_pattern & 0x04) 1764 slice_non_intra_DCT(bs, picture, scan, 0, x*2+1, y*2+1, quantizer_scale, dct_type); // cc0 luma 3 1765 if (coded_block_pattern & 0x2) 1766 slice_non_intra_DCT(bs, picture, scan, 1, x, y, quantizer_scale, PIPE_MPEG12_DCT_TYPE_FRAME); // cc1 croma 1767 if (coded_block_pattern & 0x1) 1768 slice_non_intra_DCT(bs, picture, scan, 2, x, y, quantizer_scale, PIPE_MPEG12_DCT_TYPE_FRAME); // cc2 croma 1769 } 1770 1771 dc_dct_pred[0] = dc_dct_pred[1] = dc_dct_pred[2] = 0; 1772 } 1773 1774 NEXT_MACROBLOCK; 1775 1776 vl_vlc_needbits(&bs->vlc); 1777 mba_inc = 0; 1778 while (1) { 1779 if (bs->vlc.buf >= 0x10000000) { 1780 mba = MBA_5 + (vl_vlc_ubits(&bs->vlc, 5) - 2); 1781 break; 1782 } else if (bs->vlc.buf >= 0x03000000) { 1783 mba = MBA_11 + (vl_vlc_ubits(&bs->vlc, 11) - 24); 1784 break; 1785 } else switch (vl_vlc_ubits(&bs->vlc, 11)) { 1786 case 8: /* macroblock_escape */ 1787 mba_inc += 33; 1788 /* pass through */ 1789 case 15: /* macroblock_stuffing (MPEG1 only) */ 1790 vl_vlc_dumpbits(&bs->vlc, 11); 1791 vl_vlc_needbits(&bs->vlc); 1792 continue; 1793 default: /* end of slice, or error */ 1794 return true; 1795 } 1796 } 1797 vl_vlc_dumpbits(&bs->vlc, mba->len); 1798 mba_inc += mba->mba; 1799 if (mba_inc) { 1800 //TODO conversion to signed format signed format 1801 dc_dct_pred[0] = dc_dct_pred[1] = dc_dct_pred[2] = 0; 1802 1803 mv_fwd.top.field_select = mv_fwd.bottom.field_select = default_field_select; 1804 mv_bwd.top.field_select = mv_bwd.bottom.field_select = default_field_select; 1805 1806 if (picture->picture_coding_type == P_TYPE) { 1807 mv_fwd.top.x = mv_fwd.top.y = mv_fwd.bottom.x = mv_fwd.bottom.y = 0; 1808 mv_fwd.top.weight = mv_fwd.bottom.weight = PIPE_VIDEO_MV_WEIGHT_MAX; 1809 } 1810 do { 1811 NEXT_MACROBLOCK; 1812 } while (--mba_inc); 1813 } 1814 } 1815} 1816 1817void 1818vl_mpg12_bs_init(struct vl_mpg12_bs *bs, unsigned width, unsigned height) 1819{ 1820 assert(bs); 1821 1822 memset(bs, 0, sizeof(struct vl_mpg12_bs)); 1823 1824 bs->width = width; 1825 bs->height = height; 1826} 1827 1828void 1829vl_mpg12_bs_set_buffers(struct vl_mpg12_bs *bs, struct pipe_ycbcr_block *ycbcr_stream[VL_MAX_PLANES], 1830 short *ycbcr_buffer[VL_MAX_PLANES], struct pipe_motionvector *mv_stream[VL_MAX_REF_FRAMES]) 1831{ 1832 unsigned i; 1833 1834 assert(bs); 1835 assert(ycbcr_stream && ycbcr_buffer); 1836 assert(mv_stream); 1837 1838 for (i = 0; i < VL_MAX_PLANES; ++i) { 1839 bs->ycbcr_stream[i] = ycbcr_stream[i]; 1840 bs->ycbcr_buffer[i] = ycbcr_buffer[i]; 1841 } 1842 for (i = 0; i < VL_MAX_REF_FRAMES; ++i) 1843 bs->mv_stream[i] = mv_stream[i]; 1844 1845 // TODO 1846 for (i = 0; i < bs->width*bs->height; ++i) { 1847 bs->mv_stream[0][i].top.x = bs->mv_stream[0][i].top.y = 0; 1848 bs->mv_stream[0][i].top.field_select = PIPE_VIDEO_FRAME; 1849 bs->mv_stream[0][i].top.weight = PIPE_VIDEO_MV_WEIGHT_MAX; 1850 bs->mv_stream[0][i].bottom.x = bs->mv_stream[0][i].bottom.y = 0; 1851 bs->mv_stream[0][i].bottom.field_select = PIPE_VIDEO_FRAME; 1852 bs->mv_stream[0][i].bottom.weight = PIPE_VIDEO_MV_WEIGHT_MAX; 1853 1854 bs->mv_stream[1][i].top.x = bs->mv_stream[1][i].top.y = 0; 1855 bs->mv_stream[1][i].top.field_select = PIPE_VIDEO_FRAME; 1856 bs->mv_stream[1][i].top.weight = PIPE_VIDEO_MV_WEIGHT_MIN; 1857 bs->mv_stream[1][i].bottom.x = bs->mv_stream[1][i].bottom.y = 0; 1858 bs->mv_stream[1][i].bottom.field_select = PIPE_VIDEO_FRAME; 1859 bs->mv_stream[1][i].bottom.weight = PIPE_VIDEO_MV_WEIGHT_MIN; 1860 } 1861} 1862 1863void 1864vl_mpg12_bs_decode(struct vl_mpg12_bs *bs, unsigned num_bytes, const void *buffer, 1865 struct pipe_mpeg12_picture_desc *picture, unsigned num_ycbcr_blocks[3]) 1866{ 1867 const int *scan; 1868 1869 assert(bs); 1870 assert(num_ycbcr_blocks); 1871 assert(buffer && num_bytes); 1872 1873 bs->num_ycbcr_blocks = num_ycbcr_blocks; 1874 1875 vl_vlc_init(&bs->vlc, buffer, num_bytes); 1876 1877 scan = picture->alternate_scan ? vl_zscan_alternate : vl_zscan_normal; 1878 1879 while(decode_slice(bs, picture, scan)); 1880} 1881