vl_mpeg12_bitstream.c revision ae56a1dd67040dc5d53f4a1622f775462f0fec05
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, const int quant_matrix[64], int quantizer_scale, short *dest) 725{ 726 int i, val; 727 const DCTtab *tab; 728 729 i = 0; 730 731 vl_vlc_needbits(&bs->vlc); 732 733 while (1) { 734 if (bs->vlc.buf >= 0x28000000) { 735 736 tab = DCT_B14AC_5 + (vl_vlc_ubits(&bs->vlc, 5) - 5); 737 738 i += tab->run; 739 if (i >= 64) 740 break; /* end of block */ 741 742 normal_code: 743 bs->vlc.buf <<= tab->len; 744 bs->vlc.bits += tab->len + 1; 745 val = (tab->level * quantizer_scale * quant_matrix[i]) >> 4; 746 747 /* if (bitstream_get (1)) val = -val; */ 748 val = (val ^ vl_vlc_sbits(&bs->vlc, 1)) - vl_vlc_sbits(&bs->vlc, 1); 749 750 SATURATE (val); 751 dest[i] = val; 752 753 bs->vlc.buf <<= 1; 754 vl_vlc_needbits(&bs->vlc); 755 756 continue; 757 758 } else if (bs->vlc.buf >= 0x04000000) { 759 760 tab = DCT_B14_8 + (vl_vlc_ubits(&bs->vlc, 8) - 4); 761 762 i += tab->run; 763 if (i < 64) 764 goto normal_code; 765 766 /* escape code */ 767 768 i += UBITS(bs->vlc.buf << 6, 6) - 64; 769 if (i >= 64) 770 break; /* illegal, check needed to avoid buffer overflow */ 771 772 vl_vlc_dumpbits(&bs->vlc, 12); 773 vl_vlc_needbits(&bs->vlc); 774 val = (vl_vlc_sbits(&bs->vlc, 12) * quantizer_scale * quant_matrix[i]) / 16; 775 776 SATURATE (val); 777 dest[i] = val; 778 779 vl_vlc_dumpbits(&bs->vlc, 12); 780 vl_vlc_needbits(&bs->vlc); 781 782 continue; 783 784 } else if (bs->vlc.buf >= 0x02000000) { 785 tab = DCT_B14_10 + (vl_vlc_ubits(&bs->vlc, 10) - 8); 786 i += tab->run; 787 if (i < 64) 788 goto normal_code; 789 } else if (bs->vlc.buf >= 0x00800000) { 790 tab = DCT_13 + (vl_vlc_ubits(&bs->vlc, 13) - 16); 791 i += tab->run; 792 if (i < 64) 793 goto normal_code; 794 } else if (bs->vlc.buf >= 0x00200000) { 795 tab = DCT_15 + (vl_vlc_ubits(&bs->vlc, 15) - 16); 796 i += tab->run; 797 if (i < 64) 798 goto normal_code; 799 } else { 800 tab = DCT_16 + vl_vlc_ubits(&bs->vlc, 16); 801 bs->vlc.buf <<= 16; 802 vl_vlc_getword(&bs->vlc, bs->vlc.bits + 16); 803 i += tab->run; 804 if (i < 64) 805 goto normal_code; 806 } 807 break; /* illegal, check needed to avoid buffer overflow */ 808 } 809 810 vl_vlc_dumpbits(&bs->vlc, 2); /* dump end of block code */ 811} 812 813static inline void 814get_intra_block_B15(struct vl_mpg12_bs *bs, const int quant_matrix[64], int quantizer_scale, short *dest) 815{ 816 int i, val; 817 const DCTtab * tab; 818 819 i = 0; 820 821 vl_vlc_needbits(&bs->vlc); 822 823 while (1) { 824 if (bs->vlc.buf >= 0x04000000) { 825 826 tab = DCT_B15_8 + (vl_vlc_ubits(&bs->vlc, 8) - 4); 827 828 i += tab->run; 829 if (i < 64) { 830 831 normal_code: 832 bs->vlc.buf <<= tab->len; 833 bs->vlc.bits += tab->len + 1; 834 val = (tab->level * quantizer_scale * quant_matrix[i]) >> 4; 835 836 /* if (bitstream_get (1)) val = -val; */ 837 val = (val ^ vl_vlc_sbits(&bs->vlc, 1)) - vl_vlc_sbits(&bs->vlc, 1); 838 839 SATURATE (val); 840 dest[i] = val; 841 842 bs->vlc.buf <<= 1; 843 vl_vlc_needbits(&bs->vlc); 844 845 continue; 846 847 } else { 848 849 /* end of block. I commented out this code because if we */ 850 /* dont exit here we will still exit at the later test :) */ 851 852 /* if (i >= 128) break; */ /* end of block */ 853 854 /* escape code */ 855 856 i += UBITS(bs->vlc.buf << 6, 6) - 64; 857 if (i >= 64) 858 break; /* illegal, check against buffer overflow */ 859 860 vl_vlc_dumpbits(&bs->vlc, 12); 861 vl_vlc_needbits(&bs->vlc); 862 val = (vl_vlc_sbits(&bs->vlc, 12) * quantizer_scale * quant_matrix[i]) / 16; 863 864 SATURATE (val); 865 dest[i] = val; 866 867 vl_vlc_dumpbits(&bs->vlc, 12); 868 vl_vlc_needbits(&bs->vlc); 869 870 continue; 871 872 } 873 } else if (bs->vlc.buf >= 0x02000000) { 874 tab = DCT_B15_10 + (vl_vlc_ubits(&bs->vlc, 10) - 8); 875 i += tab->run; 876 if (i < 64) 877 goto normal_code; 878 } else if (bs->vlc.buf >= 0x00800000) { 879 tab = DCT_13 + (vl_vlc_ubits(&bs->vlc, 13) - 16); 880 i += tab->run; 881 if (i < 64) 882 goto normal_code; 883 } else if (bs->vlc.buf >= 0x00200000) { 884 tab = DCT_15 + (vl_vlc_ubits(&bs->vlc, 15) - 16); 885 i += tab->run; 886 if (i < 64) 887 goto normal_code; 888 } else { 889 tab = DCT_16 + vl_vlc_ubits(&bs->vlc, 16); 890 bs->vlc.buf <<= 16; 891 vl_vlc_getword(&bs->vlc, bs->vlc.bits + 16); 892 i += tab->run; 893 if (i < 64) 894 goto normal_code; 895 } 896 break; /* illegal, check needed to avoid buffer overflow */ 897 } 898 899 vl_vlc_dumpbits(&bs->vlc, 4); /* dump end of block code */ 900} 901 902static inline void 903get_non_intra_block(struct vl_mpg12_bs *bs, const int quant_matrix[64], int quantizer_scale, short *dest) 904{ 905 int i, val; 906 const DCTtab *tab; 907 908 i = -1; 909 910 vl_vlc_needbits(&bs->vlc); 911 if (bs->vlc.buf >= 0x28000000) { 912 tab = DCT_B14DC_5 + (vl_vlc_ubits(&bs->vlc, 5) - 5); 913 goto entry_1; 914 } else 915 goto entry_2; 916 917 while (1) { 918 if (bs->vlc.buf >= 0x28000000) { 919 920 tab = DCT_B14AC_5 + (vl_vlc_ubits(&bs->vlc, 5) - 5); 921 922 entry_1: 923 i += tab->run; 924 if (i >= 64) 925 break; /* end of block */ 926 927 normal_code: 928 bs->vlc.buf <<= tab->len; 929 bs->vlc.bits += tab->len + 1; 930 val = ((2*tab->level+1) * quantizer_scale * quant_matrix[i]) >> 5; 931 932 /* if (bitstream_get (1)) val = -val; */ 933 val = (val ^ vl_vlc_sbits(&bs->vlc, 1)) - vl_vlc_sbits(&bs->vlc, 1); 934 935 SATURATE (val); 936 dest[i] = val; 937 938 bs->vlc.buf <<= 1; 939 vl_vlc_needbits(&bs->vlc); 940 941 continue; 942 943 } 944 945 entry_2: 946 if (bs->vlc.buf >= 0x04000000) { 947 948 tab = DCT_B14_8 + (vl_vlc_ubits(&bs->vlc, 8) - 4); 949 950 i += tab->run; 951 if (i < 64) 952 goto normal_code; 953 954 /* escape code */ 955 956 i += UBITS(bs->vlc.buf << 6, 6) - 64; 957 if (i >= 64) 958 break; /* illegal, check needed to avoid buffer overflow */ 959 960 vl_vlc_dumpbits(&bs->vlc, 12); 961 vl_vlc_needbits(&bs->vlc); 962 val = 2 * (vl_vlc_sbits(&bs->vlc, 12) + vl_vlc_sbits(&bs->vlc, 1)) + 1; 963 val = (val * quantizer_scale * quant_matrix[i]) / 32; 964 965 SATURATE (val); 966 dest[i] = val; 967 968 vl_vlc_dumpbits(&bs->vlc, 12); 969 vl_vlc_needbits(&bs->vlc); 970 971 continue; 972 973 } else if (bs->vlc.buf >= 0x02000000) { 974 tab = DCT_B14_10 + (vl_vlc_ubits(&bs->vlc, 10) - 8); 975 i += tab->run; 976 if (i < 64) 977 goto normal_code; 978 } else if (bs->vlc.buf >= 0x00800000) { 979 tab = DCT_13 + (vl_vlc_ubits(&bs->vlc, 13) - 16); 980 i += tab->run; 981 if (i < 64) 982 goto normal_code; 983 } else if (bs->vlc.buf >= 0x00200000) { 984 tab = DCT_15 + (vl_vlc_ubits(&bs->vlc, 15) - 16); 985 i += tab->run; 986 if (i < 64) 987 goto normal_code; 988 } else { 989 tab = DCT_16 + vl_vlc_ubits(&bs->vlc, 16); 990 bs->vlc.buf <<= 16; 991 vl_vlc_getword(&bs->vlc, bs->vlc.bits + 16); 992 i += tab->run; 993 if (i < 64) 994 goto normal_code; 995 } 996 break; /* illegal, check needed to avoid buffer overflow */ 997 } 998 vl_vlc_dumpbits(&bs->vlc, 2); /* dump end of block code */ 999} 1000 1001static inline void 1002get_mpeg1_intra_block(struct vl_mpg12_bs *bs, const int quant_matrix[64], int quantizer_scale, short *dest) 1003{ 1004 int i, val; 1005 const DCTtab * tab; 1006 1007 i = 0; 1008 1009 vl_vlc_needbits(&bs->vlc); 1010 1011 while (1) { 1012 if (bs->vlc.buf >= 0x28000000) { 1013 1014 tab = DCT_B14AC_5 + (vl_vlc_ubits(&bs->vlc, 5) - 5); 1015 1016 i += tab->run; 1017 if (i >= 64) 1018 break; /* end of block */ 1019 1020 normal_code: 1021 bs->vlc.buf <<= tab->len; 1022 bs->vlc.bits += tab->len + 1; 1023 val = (tab->level * quantizer_scale * quant_matrix[i]) >> 4; 1024 1025 /* oddification */ 1026 val = (val - 1) | 1; 1027 1028 /* if (bitstream_get (1)) val = -val; */ 1029 val = (val ^ vl_vlc_sbits(&bs->vlc, 1)) - vl_vlc_sbits(&bs->vlc, 1); 1030 1031 SATURATE (val); 1032 dest[i] = val; 1033 1034 bs->vlc.buf <<= 1; 1035 vl_vlc_needbits(&bs->vlc); 1036 1037 continue; 1038 1039 } else if (bs->vlc.buf >= 0x04000000) { 1040 1041 tab = DCT_B14_8 + (vl_vlc_ubits(&bs->vlc, 8) - 4); 1042 1043 i += tab->run; 1044 if (i < 64) 1045 goto normal_code; 1046 1047 /* escape code */ 1048 1049 i += UBITS(bs->vlc.buf << 6, 6) - 64; 1050 if (i >= 64) 1051 break; /* illegal, check needed to avoid buffer overflow */ 1052 1053 vl_vlc_dumpbits(&bs->vlc, 12); 1054 vl_vlc_needbits(&bs->vlc); 1055 val = vl_vlc_sbits(&bs->vlc, 8); 1056 if (! (val & 0x7f)) { 1057 vl_vlc_dumpbits(&bs->vlc, 8); 1058 val = vl_vlc_ubits(&bs->vlc, 8) + 2 * val; 1059 } 1060 val = (val * quantizer_scale * quant_matrix[i]) / 16; 1061 1062 /* oddification */ 1063 val = (val + ~SBITS (val, 1)) | 1; 1064 1065 SATURATE (val); 1066 dest[i] = val; 1067 1068 vl_vlc_dumpbits(&bs->vlc, 8); 1069 vl_vlc_needbits(&bs->vlc); 1070 1071 continue; 1072 1073 } else if (bs->vlc.buf >= 0x02000000) { 1074 tab = DCT_B14_10 + (vl_vlc_ubits(&bs->vlc, 10) - 8); 1075 i += tab->run; 1076 if (i < 64) 1077 goto normal_code; 1078 } else if (bs->vlc.buf >= 0x00800000) { 1079 tab = DCT_13 + (vl_vlc_ubits(&bs->vlc, 13) - 16); 1080 i += tab->run; 1081 if (i < 64) 1082 goto normal_code; 1083 } else if (bs->vlc.buf >= 0x00200000) { 1084 tab = DCT_15 + (vl_vlc_ubits(&bs->vlc, 15) - 16); 1085 i += tab->run; 1086 if (i < 64) 1087 goto normal_code; 1088 } else { 1089 tab = DCT_16 + vl_vlc_ubits(&bs->vlc, 16); 1090 bs->vlc.buf <<= 16; 1091 vl_vlc_getword(&bs->vlc, bs->vlc.bits + 16); 1092 i += tab->run; 1093 if (i < 64) 1094 goto normal_code; 1095 } 1096 break; /* illegal, check needed to avoid buffer overflow */ 1097 } 1098 vl_vlc_dumpbits(&bs->vlc, 2); /* dump end of block code */ 1099} 1100 1101static inline void 1102get_mpeg1_non_intra_block(struct vl_mpg12_bs *bs, const int quant_matrix[64], int quantizer_scale, short *dest) 1103{ 1104 int i, val; 1105 const DCTtab * tab; 1106 1107 i = -1; 1108 1109 vl_vlc_needbits(&bs->vlc); 1110 if (bs->vlc.buf >= 0x28000000) { 1111 tab = DCT_B14DC_5 + (vl_vlc_ubits(&bs->vlc, 5) - 5); 1112 goto entry_1; 1113 } else 1114 goto entry_2; 1115 1116 while (1) { 1117 if (bs->vlc.buf >= 0x28000000) { 1118 1119 tab = DCT_B14AC_5 + (vl_vlc_ubits(&bs->vlc, 5) - 5); 1120 1121 entry_1: 1122 i += tab->run; 1123 if (i >= 64) 1124 break; /* end of block */ 1125 1126 normal_code: 1127 bs->vlc.buf <<= tab->len; 1128 bs->vlc.bits += tab->len + 1; 1129 val = ((2*tab->level+1) * quantizer_scale * quant_matrix[i]) >> 5; 1130 1131 /* oddification */ 1132 val = (val - 1) | 1; 1133 1134 /* if (bitstream_get (1)) val = -val; */ 1135 val = (val ^ vl_vlc_sbits(&bs->vlc, 1)) - vl_vlc_sbits(&bs->vlc, 1); 1136 1137 SATURATE (val); 1138 dest[i] = val; 1139 1140 bs->vlc.buf <<= 1; 1141 vl_vlc_needbits(&bs->vlc); 1142 1143 continue; 1144 1145 } 1146 1147 entry_2: 1148 if (bs->vlc.buf >= 0x04000000) { 1149 1150 tab = DCT_B14_8 + (vl_vlc_ubits(&bs->vlc, 8) - 4); 1151 1152 i += tab->run; 1153 if (i < 64) 1154 goto normal_code; 1155 1156 /* escape code */ 1157 1158 i += UBITS(bs->vlc.buf << 6, 6) - 64; 1159 if (i >= 64) 1160 break; /* illegal, check needed to avoid buffer overflow */ 1161 1162 vl_vlc_dumpbits(&bs->vlc, 12); 1163 vl_vlc_needbits(&bs->vlc); 1164 val = vl_vlc_sbits(&bs->vlc, 8); 1165 if (! (val & 0x7f)) { 1166 vl_vlc_dumpbits(&bs->vlc, 8); 1167 val = vl_vlc_ubits(&bs->vlc, 8) + 2 * val; 1168 } 1169 val = 2 * (val + SBITS (val, 1)) + 1; 1170 val = (val * quantizer_scale * quant_matrix[i]) / 32; 1171 1172 /* oddification */ 1173 val = (val + ~SBITS (val, 1)) | 1; 1174 1175 SATURATE (val); 1176 dest[i] = val; 1177 1178 vl_vlc_dumpbits(&bs->vlc, 8); 1179 vl_vlc_needbits(&bs->vlc); 1180 1181 continue; 1182 1183 } else if (bs->vlc.buf >= 0x02000000) { 1184 tab = DCT_B14_10 + (vl_vlc_ubits(&bs->vlc, 10) - 8); 1185 i += tab->run; 1186 if (i < 64) 1187 goto normal_code; 1188 } else if (bs->vlc.buf >= 0x00800000) { 1189 tab = DCT_13 + (vl_vlc_ubits(&bs->vlc, 13) - 16); 1190 i += tab->run; 1191 if (i < 64) 1192 goto normal_code; 1193 } else if (bs->vlc.buf >= 0x00200000) { 1194 tab = DCT_15 + (vl_vlc_ubits(&bs->vlc, 15) - 16); 1195 i += tab->run; 1196 if (i < 64) 1197 goto normal_code; 1198 } else { 1199 tab = DCT_16 + vl_vlc_ubits(&bs->vlc, 16); 1200 bs->vlc.buf <<= 16; 1201 vl_vlc_getword(&bs->vlc, bs->vlc.bits + 16); 1202 i += tab->run; 1203 if (i < 64) 1204 goto normal_code; 1205 } 1206 break; /* illegal, check needed to avoid buffer overflow */ 1207 } 1208 vl_vlc_dumpbits(&bs->vlc, 2); /* dump end of block code */ 1209} 1210 1211static inline void 1212slice_intra_DCT(struct vl_mpg12_bs *bs, struct pipe_mpeg12_picture_desc * picture, const int quant_matrix[64], int cc, 1213 unsigned x, unsigned y, enum pipe_mpeg12_dct_type coding, int quantizer_scale, int dc_dct_pred[3]) 1214{ 1215 short dest[64]; 1216 1217 bs->ycbcr_stream[cc]->x = x; 1218 bs->ycbcr_stream[cc]->y = y; 1219 bs->ycbcr_stream[cc]->intra = PIPE_MPEG12_DCT_INTRA; 1220 bs->ycbcr_stream[cc]->coding = coding; 1221 1222 vl_vlc_needbits(&bs->vlc); 1223 1224 /* Get the intra DC coefficient and inverse quantize it */ 1225 if (cc == 0) 1226 dc_dct_pred[0] += get_luma_dc_dct_diff(bs); 1227 else 1228 dc_dct_pred[cc] += get_chroma_dc_dct_diff(bs); 1229 1230 memset(dest, 0, sizeof(int16_t) * 64); 1231 dest[0] = dc_dct_pred[cc] << (3 - picture->intra_dc_precision); 1232 if (picture->mpeg1) { 1233 if (picture->picture_coding_type != D_TYPE) 1234 get_mpeg1_intra_block(bs, quant_matrix, quantizer_scale, dest); 1235 } else if (picture->intra_vlc_format) 1236 get_intra_block_B15(bs, quant_matrix, quantizer_scale, dest); 1237 else 1238 get_intra_block_B14(bs, quant_matrix, quantizer_scale, dest); 1239 1240 memcpy(bs->ycbcr_buffer[cc], dest, sizeof(int16_t) * 64); 1241 1242 bs->num_ycbcr_blocks[cc]++; 1243 bs->ycbcr_stream[cc]++; 1244 bs->ycbcr_buffer[cc] += 64; 1245} 1246 1247static inline void 1248slice_non_intra_DCT(struct vl_mpg12_bs *bs, struct pipe_mpeg12_picture_desc * picture, const int quant_matrix[64], int cc, 1249 unsigned x, unsigned y, enum pipe_mpeg12_dct_type coding, int quantizer_scale) 1250{ 1251 short dest[64]; 1252 1253 bs->ycbcr_stream[cc]->x = x; 1254 bs->ycbcr_stream[cc]->y = y; 1255 bs->ycbcr_stream[cc]->intra = PIPE_MPEG12_DCT_DELTA; 1256 bs->ycbcr_stream[cc]->coding = coding; 1257 1258 memset(dest, 0, sizeof(int16_t) * 64); 1259 if (picture->mpeg1) 1260 get_mpeg1_non_intra_block(bs, quant_matrix, quantizer_scale, dest); 1261 else 1262 get_non_intra_block(bs, quant_matrix, quantizer_scale, dest); 1263 1264 memcpy(bs->ycbcr_buffer[cc], dest, sizeof(int16_t) * 64); 1265 1266 bs->num_ycbcr_blocks[cc]++; 1267 bs->ycbcr_stream[cc]++; 1268 bs->ycbcr_buffer[cc] += 64; 1269} 1270 1271static inline void 1272motion_mp1(struct vl_mpg12_bs *bs, unsigned f_code[2], struct pipe_motionvector *mv) 1273{ 1274 int motion_x, motion_y; 1275 1276 mv->top.field_select = mv->bottom.field_select = PIPE_VIDEO_FRAME; 1277 1278 vl_vlc_needbits(&bs->vlc); 1279 motion_x = (mv->top.x + (get_motion_delta(bs, f_code[0]) << f_code[1])); 1280 motion_x = bound_motion_vector (motion_x, f_code[0] + f_code[1]); 1281 mv->top.x = mv->bottom.x = motion_x; 1282 1283 vl_vlc_needbits(&bs->vlc); 1284 motion_y = (mv->top.y + (get_motion_delta(bs, f_code[0]) << f_code[1])); 1285 motion_y = bound_motion_vector (motion_y, f_code[0] + f_code[1]); 1286 mv->top.y = mv->bottom.y = motion_y; 1287} 1288 1289static inline void 1290motion_fr_frame(struct vl_mpg12_bs *bs, unsigned f_code[2], struct pipe_motionvector *mv) 1291{ 1292 int motion_x, motion_y; 1293 1294 mv->top.field_select = mv->bottom.field_select = PIPE_VIDEO_FRAME; 1295 1296 vl_vlc_needbits(&bs->vlc); 1297 motion_x = mv->top.x + get_motion_delta(bs, f_code[0]); 1298 motion_x = bound_motion_vector(motion_x, f_code[0]); 1299 mv->top.x = mv->bottom.x = motion_x; 1300 1301 vl_vlc_needbits(&bs->vlc); 1302 motion_y = mv->top.y + get_motion_delta(bs, f_code[1]); 1303 motion_y = bound_motion_vector(motion_y, f_code[1]); 1304 mv->top.y = mv->bottom.y = motion_y; 1305} 1306 1307static inline void 1308motion_fr_field(struct vl_mpg12_bs *bs, unsigned f_code[2], struct pipe_motionvector *mv) 1309{ 1310 int motion_x, motion_y; 1311 1312 vl_vlc_needbits(&bs->vlc); 1313 mv->top.field_select = vl_vlc_ubits(&bs->vlc, 1) ? 1314 PIPE_VIDEO_BOTTOM_FIELD : PIPE_VIDEO_TOP_FIELD; 1315 vl_vlc_dumpbits(&bs->vlc, 1); 1316 1317 motion_x = mv->top.x + get_motion_delta(bs, f_code[0]); 1318 motion_x = bound_motion_vector (motion_x, f_code[0]); 1319 mv->top.x = motion_x; 1320 1321 vl_vlc_needbits(&bs->vlc); 1322 motion_y = (mv->top.y >> 1) + get_motion_delta(bs, f_code[1]); 1323 /* motion_y = bound_motion_vector (motion_y, f_code[1]); */ 1324 mv->top.y = motion_y << 1; 1325 1326 vl_vlc_needbits(&bs->vlc); 1327 mv->bottom.field_select = vl_vlc_ubits(&bs->vlc, 1) ? 1328 PIPE_VIDEO_BOTTOM_FIELD : PIPE_VIDEO_TOP_FIELD; 1329 vl_vlc_dumpbits(&bs->vlc, 1); 1330 1331 motion_x = mv->bottom.x + get_motion_delta(bs, f_code[0]); 1332 motion_x = bound_motion_vector (motion_x, f_code[0]); 1333 mv->bottom.x = motion_x; 1334 1335 vl_vlc_needbits(&bs->vlc); 1336 motion_y = (mv->bottom.y >> 1) + get_motion_delta(bs, f_code[1]); 1337 /* motion_y = bound_motion_vector (motion_y, f_code[1]); */ 1338 mv->bottom.y = motion_y << 1; 1339} 1340 1341static inline void 1342motion_fr_dmv(struct vl_mpg12_bs *bs, unsigned f_code[2], struct pipe_motionvector *mv) 1343{ 1344 int motion_x, motion_y; 1345 1346 // TODO Implement dmv 1347 mv->top.field_select = mv->bottom.field_select = PIPE_VIDEO_FRAME; 1348 1349 vl_vlc_needbits(&bs->vlc); 1350 motion_x = mv->top.x + get_motion_delta(bs, f_code[0]); 1351 motion_x = bound_motion_vector(motion_x, f_code[0]); 1352 mv->top.x = mv->bottom.x = motion_x; 1353 1354 vl_vlc_needbits(&bs->vlc); 1355 motion_y = (mv->top.y >> 1) + get_motion_delta(bs, f_code[1]); 1356 /* motion_y = bound_motion_vector (motion_y, f_code[1]); */ 1357 mv->top.y = mv->bottom.y = motion_y << 1; 1358} 1359 1360/* like motion_frame, but parsing without actual motion compensation */ 1361static inline void 1362motion_fr_conceal(struct vl_mpg12_bs *bs, unsigned f_code[2], struct pipe_motionvector *mv) 1363{ 1364 int tmp; 1365 1366 mv->top.field_select = mv->bottom.field_select = PIPE_VIDEO_FRAME; 1367 1368 vl_vlc_needbits(&bs->vlc); 1369 tmp = (mv->top.x + get_motion_delta(bs, f_code[0])); 1370 tmp = bound_motion_vector (tmp, f_code[0]); 1371 mv->top.x = mv->bottom.x = tmp; 1372 1373 vl_vlc_needbits(&bs->vlc); 1374 tmp = (mv->top.y + get_motion_delta(bs, f_code[1])); 1375 tmp = bound_motion_vector (tmp, f_code[1]); 1376 mv->top.y = mv->bottom.y = tmp; 1377 1378 vl_vlc_dumpbits(&bs->vlc, 1); /* remove marker_bit */ 1379} 1380 1381static inline void 1382motion_fi_field(struct vl_mpg12_bs *bs, unsigned f_code[2], struct pipe_motionvector *mv) 1383{ 1384 int motion_x, motion_y; 1385 1386 vl_vlc_needbits(&bs->vlc); 1387 1388 // ref_field 1389 //vl_vlc_ubits(&bs->vlc, 1); 1390 1391 // TODO field select may need to do something here for bob (weave ok) 1392 mv->top.field_select = mv->bottom.field_select = PIPE_VIDEO_FRAME; 1393 vl_vlc_dumpbits(&bs->vlc, 1); 1394 1395 motion_x = mv->top.x + get_motion_delta(bs, f_code[0]); 1396 motion_x = bound_motion_vector (motion_x, f_code[0]); 1397 mv->top.x = mv->bottom.x = motion_x; 1398 1399 vl_vlc_needbits(&bs->vlc); 1400 motion_y = mv->top.y + get_motion_delta(bs, f_code[1]); 1401 motion_y = bound_motion_vector (motion_y, f_code[1]); 1402 mv->top.y = mv->bottom.y = motion_y; 1403} 1404 1405static inline void 1406motion_fi_16x8(struct vl_mpg12_bs *bs, unsigned f_code[2], struct pipe_motionvector *mv) 1407{ 1408 int motion_x, motion_y; 1409 1410 vl_vlc_needbits(&bs->vlc); 1411 1412 // ref_field 1413 //vl_vlc_ubits(&bs->vlc, 1); 1414 1415 // TODO field select may need to do something here bob (weave ok) 1416 mv->top.field_select = PIPE_VIDEO_FRAME; 1417 vl_vlc_dumpbits(&bs->vlc, 1); 1418 1419 motion_x = mv->top.x + get_motion_delta(bs, f_code[0]); 1420 motion_x = bound_motion_vector (motion_x, f_code[0]); 1421 mv->top.x = motion_x; 1422 1423 vl_vlc_needbits(&bs->vlc); 1424 motion_y = mv->top.y + get_motion_delta(bs, f_code[1]); 1425 motion_y = bound_motion_vector (motion_y, f_code[1]); 1426 mv->top.y = motion_y; 1427 1428 vl_vlc_needbits(&bs->vlc); 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->bottom.field_select = PIPE_VIDEO_FRAME; 1434 vl_vlc_dumpbits(&bs->vlc, 1); 1435 1436 motion_x = mv->bottom.x + get_motion_delta(bs, f_code[0]); 1437 motion_x = bound_motion_vector (motion_x, f_code[0]); 1438 mv->bottom.x = motion_x; 1439 1440 vl_vlc_needbits(&bs->vlc); 1441 motion_y = mv->bottom.y + get_motion_delta(bs, f_code[1]); 1442 motion_y = bound_motion_vector (motion_y, f_code[1]); 1443 mv->bottom.y = motion_y; 1444} 1445 1446static inline void 1447motion_fi_dmv(struct vl_mpg12_bs *bs, unsigned f_code[2], struct pipe_motionvector *mv) 1448{ 1449 int motion_x, motion_y; 1450 1451 // TODO field select may need to do something here for bob (weave ok) 1452 mv->top.field_select = mv->bottom.field_select = PIPE_VIDEO_FRAME; 1453 1454 vl_vlc_needbits(&bs->vlc); 1455 motion_x = mv->top.x + get_motion_delta(bs, f_code[0]); 1456 motion_x = bound_motion_vector (motion_x, f_code[0]); 1457 mv->top.x = mv->bottom.x = motion_x; 1458 1459 vl_vlc_needbits(&bs->vlc); 1460 motion_y = mv->top.y + get_motion_delta(bs, f_code[1]); 1461 motion_y = bound_motion_vector (motion_y, f_code[1]); 1462 mv->top.y = mv->bottom.y = motion_y; 1463} 1464 1465 1466static inline void 1467motion_fi_conceal(struct vl_mpg12_bs *bs, unsigned f_code[2], struct pipe_motionvector *mv) 1468{ 1469 int tmp; 1470 1471 vl_vlc_needbits(&bs->vlc); 1472 vl_vlc_dumpbits(&bs->vlc, 1); /* remove field_select */ 1473 1474 tmp = (mv->top.x + get_motion_delta(bs, f_code[0])); 1475 tmp = bound_motion_vector(tmp, f_code[0]); 1476 mv->top.x = mv->bottom.x = tmp; 1477 1478 vl_vlc_needbits(&bs->vlc); 1479 tmp = (mv->top.y + get_motion_delta(bs, f_code[1])); 1480 tmp = bound_motion_vector(tmp, f_code[1]); 1481 mv->top.y = mv->bottom.y = tmp; 1482 1483 vl_vlc_dumpbits(&bs->vlc, 1); /* remove marker_bit */ 1484} 1485 1486#define MOTION_CALL(routine, macroblock_modes) \ 1487do { \ 1488 if ((macroblock_modes) & MACROBLOCK_MOTION_FORWARD) \ 1489 routine(bs, picture->f_code[0], &mv_fwd); \ 1490 if ((macroblock_modes) & MACROBLOCK_MOTION_BACKWARD) \ 1491 routine(bs, picture->f_code[1], &mv_bwd); \ 1492} while (0) 1493 1494static inline void 1495store_motionvectors(struct vl_mpg12_bs *bs, unsigned *mv_pos, 1496 struct pipe_motionvector *mv_fwd, 1497 struct pipe_motionvector *mv_bwd) 1498{ 1499 bs->mv_stream[0][*mv_pos].top = mv_fwd->top; 1500 bs->mv_stream[0][*mv_pos].bottom = 1501 mv_fwd->top.field_select == PIPE_VIDEO_FRAME ? 1502 mv_fwd->top : mv_fwd->bottom; 1503 1504 bs->mv_stream[1][*mv_pos].top = mv_bwd->top; 1505 bs->mv_stream[1][*mv_pos].bottom = 1506 mv_bwd->top.field_select == PIPE_VIDEO_FRAME ? 1507 mv_bwd->top : mv_bwd->bottom; 1508 1509 (*mv_pos)++; 1510} 1511 1512static inline bool 1513slice_init(struct vl_mpg12_bs *bs, struct pipe_mpeg12_picture_desc * picture, 1514 int *quantizer_scale, unsigned *x, unsigned *y, unsigned *mv_pos) 1515{ 1516 const MBAtab * mba; 1517 1518 vl_vlc_need32bits(&bs->vlc); 1519 while(bs->vlc.buf < 0x101 || bs->vlc.buf > 0x1AF) { 1520 if(!vl_vlc_getbyte(&bs->vlc)) 1521 return false; 1522 } 1523 *y = (bs->vlc.buf & 0xFF) - 1; 1524 vl_vlc_restart(&bs->vlc); 1525 1526 *quantizer_scale = get_quantizer_scale(bs, picture); 1527 1528 /* ignore intra_slice and all the extra data */ 1529 while (bs->vlc.buf & 0x80000000) { 1530 vl_vlc_dumpbits(&bs->vlc, 9); 1531 vl_vlc_needbits(&bs->vlc); 1532 } 1533 1534 /* decode initial macroblock address increment */ 1535 *x = 0; 1536 while (1) { 1537 if (bs->vlc.buf >= 0x08000000) { 1538 mba = MBA_5 + (vl_vlc_ubits(&bs->vlc, 6) - 2); 1539 break; 1540 } else if (bs->vlc.buf >= 0x01800000) { 1541 mba = MBA_11 + (vl_vlc_ubits(&bs->vlc, 12) - 24); 1542 break; 1543 } else switch (vl_vlc_ubits(&bs->vlc, 12)) { 1544 case 8: /* macroblock_escape */ 1545 *x += 33; 1546 vl_vlc_dumpbits(&bs->vlc, 11); 1547 vl_vlc_needbits(&bs->vlc); 1548 continue; 1549 case 15: /* macroblock_stuffing (MPEG1 only) */ 1550 bs->vlc.buf &= 0xfffff; 1551 vl_vlc_dumpbits(&bs->vlc, 11); 1552 vl_vlc_needbits(&bs->vlc); 1553 continue; 1554 default: /* error */ 1555 return false; 1556 } 1557 } 1558 vl_vlc_dumpbits(&bs->vlc, mba->len + 1); 1559 *x += mba->mba; 1560 1561 while (*x >= bs->width) { 1562 *x -= bs->width; 1563 (*y)++; 1564 } 1565 if (*y > bs->height) 1566 return false; 1567 1568 *mv_pos = *x + *y * bs->width; 1569 1570 return true; 1571} 1572 1573static inline bool 1574decode_slice(struct vl_mpg12_bs *bs, struct pipe_mpeg12_picture_desc *picture, 1575 const int intra_quantizer_matrix[64], const int non_intra_quantizer_matrix[64]) 1576{ 1577 enum pipe_video_field_select default_field_select; 1578 struct pipe_motionvector mv_fwd, mv_bwd; 1579 enum pipe_mpeg12_dct_type dct_type; 1580 1581 /* predictor for DC coefficients in intra blocks */ 1582 int dc_dct_pred[3] = { 0, 0, 0 }; 1583 int quantizer_scale; 1584 1585 unsigned x, y, mv_pos; 1586 1587 switch(picture->picture_structure) { 1588 case TOP_FIELD: 1589 default_field_select = PIPE_VIDEO_TOP_FIELD; 1590 break; 1591 1592 case BOTTOM_FIELD: 1593 default_field_select = PIPE_VIDEO_BOTTOM_FIELD; 1594 break; 1595 1596 default: 1597 default_field_select = PIPE_VIDEO_FRAME; 1598 break; 1599 } 1600 1601 if (!slice_init(bs, picture, &quantizer_scale, &x, &y, &mv_pos)) 1602 return false; 1603 1604 mv_fwd.top.x = mv_fwd.top.y = mv_fwd.bottom.x = mv_fwd.bottom.y = 0; 1605 mv_fwd.top.field_select = mv_fwd.bottom.field_select = default_field_select; 1606 1607 mv_bwd.top.x = mv_bwd.top.y = mv_bwd.bottom.x = mv_bwd.bottom.y = 0; 1608 mv_bwd.top.field_select = mv_bwd.bottom.field_select = default_field_select; 1609 1610 while (1) { 1611 int macroblock_modes; 1612 int mba_inc; 1613 const MBAtab * mba; 1614 1615 vl_vlc_needbits(&bs->vlc); 1616 1617 macroblock_modes = get_macroblock_modes(bs, picture); 1618 dct_type = get_dct_type(bs, picture, macroblock_modes); 1619 1620 switch(macroblock_modes & (MACROBLOCK_MOTION_FORWARD|MACROBLOCK_MOTION_BACKWARD)) { 1621 case (MACROBLOCK_MOTION_FORWARD|MACROBLOCK_MOTION_BACKWARD): 1622 mv_fwd.top.weight = mv_fwd.bottom.weight = PIPE_VIDEO_MV_WEIGHT_HALF; 1623 mv_bwd.top.weight = mv_bwd.bottom.weight = PIPE_VIDEO_MV_WEIGHT_HALF; 1624 break; 1625 1626 default: 1627 mv_fwd.top.field_select = mv_fwd.bottom.field_select = default_field_select; 1628 mv_bwd.top.field_select = mv_bwd.bottom.field_select = default_field_select; 1629 1630 /* fall through */ 1631 case MACROBLOCK_MOTION_FORWARD: 1632 mv_fwd.top.weight = mv_fwd.bottom.weight = PIPE_VIDEO_MV_WEIGHT_MAX; 1633 mv_bwd.top.weight = mv_bwd.bottom.weight = PIPE_VIDEO_MV_WEIGHT_MIN; 1634 break; 1635 1636 case MACROBLOCK_MOTION_BACKWARD: 1637 mv_fwd.top.weight = mv_fwd.bottom.weight = PIPE_VIDEO_MV_WEIGHT_MIN; 1638 mv_bwd.top.weight = mv_bwd.bottom.weight = PIPE_VIDEO_MV_WEIGHT_MAX; 1639 break; 1640 } 1641 1642 /* maybe integrate MACROBLOCK_QUANT test into get_macroblock_modes ? */ 1643 if (macroblock_modes & MACROBLOCK_QUANT) 1644 quantizer_scale = get_quantizer_scale(bs, picture); 1645 1646 if (macroblock_modes & MACROBLOCK_INTRA) { 1647 1648 if (picture->concealment_motion_vectors) { 1649 if (picture->picture_structure == FRAME_PICTURE) 1650 motion_fr_conceal(bs, picture->f_code[0], &mv_fwd); 1651 else 1652 motion_fi_conceal(bs, picture->f_code[0], &mv_fwd); 1653 1654 } else { 1655 mv_fwd.top.x = mv_fwd.top.y = mv_fwd.bottom.x = mv_fwd.bottom.y = 0; 1656 mv_bwd.top.x = mv_bwd.top.y = mv_bwd.bottom.x = mv_bwd.bottom.y = 0; 1657 } 1658 mv_fwd.top.weight = mv_fwd.bottom.weight = PIPE_VIDEO_MV_WEIGHT_MIN; 1659 mv_bwd.top.weight = mv_bwd.bottom.weight = PIPE_VIDEO_MV_WEIGHT_MIN; 1660 1661 // unravaled loop of 6 block(i) calls in macroblock() 1662 slice_intra_DCT(bs, picture, intra_quantizer_matrix, 0, x*2+0, y*2+0, dct_type, quantizer_scale, dc_dct_pred); 1663 slice_intra_DCT(bs, picture, intra_quantizer_matrix, 0, x*2+1, y*2+0, dct_type, quantizer_scale, dc_dct_pred); 1664 slice_intra_DCT(bs, picture, intra_quantizer_matrix, 0, x*2+0, y*2+1, dct_type, quantizer_scale, dc_dct_pred); 1665 slice_intra_DCT(bs, picture, intra_quantizer_matrix, 0, x*2+1, y*2+1, dct_type, quantizer_scale, dc_dct_pred); 1666 slice_intra_DCT(bs, picture, intra_quantizer_matrix, 1, x, y, PIPE_MPEG12_DCT_TYPE_FRAME, quantizer_scale, dc_dct_pred); 1667 slice_intra_DCT(bs, picture, intra_quantizer_matrix, 2, x, y, PIPE_MPEG12_DCT_TYPE_FRAME, quantizer_scale, dc_dct_pred); 1668 1669 if (picture->picture_coding_type == D_TYPE) { 1670 vl_vlc_needbits(&bs->vlc); 1671 vl_vlc_dumpbits(&bs->vlc, 1); 1672 } 1673 1674 } else { 1675 if (picture->picture_structure == FRAME_PICTURE) 1676 switch (macroblock_modes & MOTION_TYPE_MASK) { 1677 case MC_FRAME: 1678 if (picture->mpeg1) { 1679 MOTION_CALL(motion_mp1, macroblock_modes); 1680 } else { 1681 MOTION_CALL(motion_fr_frame, macroblock_modes); 1682 } 1683 break; 1684 1685 case MC_FIELD: 1686 MOTION_CALL (motion_fr_field, macroblock_modes); 1687 break; 1688 1689 case MC_DMV: 1690 MOTION_CALL (motion_fr_dmv, MACROBLOCK_MOTION_FORWARD); 1691 break; 1692 1693 case 0: 1694 /* non-intra mb without forward mv in a P picture */ 1695 mv_fwd.top.x = mv_fwd.top.y = mv_fwd.bottom.x = mv_fwd.bottom.y = 0; 1696 mv_bwd.top.x = mv_bwd.top.y = mv_bwd.bottom.x = mv_bwd.bottom.y = 0; 1697 break; 1698 } 1699 else 1700 switch (macroblock_modes & MOTION_TYPE_MASK) { 1701 case MC_FIELD: 1702 MOTION_CALL (motion_fi_field, macroblock_modes); 1703 break; 1704 1705 case MC_16X8: 1706 MOTION_CALL (motion_fi_16x8, macroblock_modes); 1707 break; 1708 1709 case MC_DMV: 1710 MOTION_CALL (motion_fi_dmv, MACROBLOCK_MOTION_FORWARD); 1711 break; 1712 1713 case 0: 1714 /* non-intra mb without forward mv in a P picture */ 1715 mv_fwd.top.x = mv_fwd.top.y = mv_fwd.bottom.x = mv_fwd.bottom.y = 0; 1716 mv_bwd.top.x = mv_bwd.top.y = mv_bwd.bottom.x = mv_bwd.bottom.y = 0; 1717 break; 1718 } 1719 1720 if (macroblock_modes & MACROBLOCK_PATTERN) { 1721 int coded_block_pattern = get_coded_block_pattern(bs); 1722 1723 // TODO optimize not fully used for idct accel only mc. 1724 if (coded_block_pattern & 0x20) 1725 slice_non_intra_DCT(bs, picture, non_intra_quantizer_matrix, 0, x*2+0, y*2+0, dct_type, quantizer_scale); // cc0 luma 0 1726 if (coded_block_pattern & 0x10) 1727 slice_non_intra_DCT(bs, picture, non_intra_quantizer_matrix, 0, x*2+1, y*2+0, dct_type, quantizer_scale); // cc0 luma 1 1728 if (coded_block_pattern & 0x08) 1729 slice_non_intra_DCT(bs, picture, non_intra_quantizer_matrix, 0, x*2+0, y*2+1, dct_type, quantizer_scale); // cc0 luma 2 1730 if (coded_block_pattern & 0x04) 1731 slice_non_intra_DCT(bs, picture, non_intra_quantizer_matrix, 0, x*2+1, y*2+1, dct_type, quantizer_scale); // cc0 luma 3 1732 if (coded_block_pattern & 0x2) 1733 slice_non_intra_DCT(bs, picture, non_intra_quantizer_matrix, 1, x, y, PIPE_MPEG12_DCT_TYPE_FRAME, quantizer_scale); // cc1 croma 1734 if (coded_block_pattern & 0x1) 1735 slice_non_intra_DCT(bs, picture, non_intra_quantizer_matrix, 2, x, y, PIPE_MPEG12_DCT_TYPE_FRAME, quantizer_scale); // cc2 croma 1736 } 1737 1738 dc_dct_pred[0] = dc_dct_pred[1] = dc_dct_pred[2] = 0; 1739 } 1740 1741 store_motionvectors(bs, &mv_pos, &mv_fwd, &mv_bwd); 1742 if (++x >= bs->width) { 1743 ++y; 1744 if (y >= bs->height) 1745 return false; 1746 x -= bs->width; 1747 } 1748 1749 vl_vlc_needbits(&bs->vlc); 1750 mba_inc = 0; 1751 while (1) { 1752 if (bs->vlc.buf >= 0x10000000) { 1753 mba = MBA_5 + (vl_vlc_ubits(&bs->vlc, 5) - 2); 1754 break; 1755 } else if (bs->vlc.buf >= 0x03000000) { 1756 mba = MBA_11 + (vl_vlc_ubits(&bs->vlc, 11) - 24); 1757 break; 1758 } else switch (vl_vlc_ubits(&bs->vlc, 11)) { 1759 case 8: /* macroblock_escape */ 1760 mba_inc += 33; 1761 /* pass through */ 1762 case 15: /* macroblock_stuffing (MPEG1 only) */ 1763 vl_vlc_dumpbits(&bs->vlc, 11); 1764 vl_vlc_needbits(&bs->vlc); 1765 continue; 1766 default: /* end of slice, or error */ 1767 return true; 1768 } 1769 } 1770 vl_vlc_dumpbits(&bs->vlc, mba->len); 1771 mba_inc += mba->mba; 1772 if (mba_inc) { 1773 //TODO conversion to signed format signed format 1774 dc_dct_pred[0] = dc_dct_pred[1] = dc_dct_pred[2] = 0; 1775 1776 mv_fwd.top.field_select = mv_fwd.bottom.field_select = default_field_select; 1777 mv_bwd.top.field_select = mv_bwd.bottom.field_select = default_field_select; 1778 1779 if (picture->picture_coding_type == P_TYPE) { 1780 mv_fwd.top.x = mv_fwd.top.y = mv_fwd.bottom.x = mv_fwd.bottom.y = 0; 1781 mv_fwd.top.weight = mv_fwd.bottom.weight = PIPE_VIDEO_MV_WEIGHT_MAX; 1782 } 1783 1784 x += mba_inc; 1785 do { 1786 store_motionvectors(bs, &mv_pos, &mv_fwd, &mv_bwd); 1787 } while (--mba_inc); 1788 } 1789 while (x >= bs->width) { 1790 ++y; 1791 if (y >= bs->height) 1792 return false; 1793 x -= bs->width; 1794 } 1795 } 1796} 1797 1798void 1799vl_mpg12_bs_init(struct vl_mpg12_bs *bs, unsigned width, unsigned height) 1800{ 1801 assert(bs); 1802 1803 memset(bs, 0, sizeof(struct vl_mpg12_bs)); 1804 1805 bs->width = width; 1806 bs->height = height; 1807} 1808 1809void 1810vl_mpg12_bs_set_buffers(struct vl_mpg12_bs *bs, struct pipe_ycbcr_block *ycbcr_stream[VL_MAX_PLANES], 1811 short *ycbcr_buffer[VL_MAX_PLANES], struct pipe_motionvector *mv_stream[VL_MAX_REF_FRAMES]) 1812{ 1813 unsigned i; 1814 1815 assert(bs); 1816 assert(ycbcr_stream && ycbcr_buffer); 1817 assert(mv_stream); 1818 1819 for (i = 0; i < VL_MAX_PLANES; ++i) { 1820 bs->ycbcr_stream[i] = ycbcr_stream[i]; 1821 bs->ycbcr_buffer[i] = ycbcr_buffer[i]; 1822 } 1823 for (i = 0; i < VL_MAX_REF_FRAMES; ++i) 1824 bs->mv_stream[i] = mv_stream[i]; 1825 1826 // TODO 1827 for (i = 0; i < bs->width*bs->height; ++i) { 1828 bs->mv_stream[0][i].top.x = bs->mv_stream[0][i].top.y = 0; 1829 bs->mv_stream[0][i].top.field_select = PIPE_VIDEO_FRAME; 1830 bs->mv_stream[0][i].top.weight = PIPE_VIDEO_MV_WEIGHT_MAX; 1831 bs->mv_stream[0][i].bottom.x = bs->mv_stream[0][i].bottom.y = 0; 1832 bs->mv_stream[0][i].bottom.field_select = PIPE_VIDEO_FRAME; 1833 bs->mv_stream[0][i].bottom.weight = PIPE_VIDEO_MV_WEIGHT_MAX; 1834 1835 bs->mv_stream[1][i].top.x = bs->mv_stream[1][i].top.y = 0; 1836 bs->mv_stream[1][i].top.field_select = PIPE_VIDEO_FRAME; 1837 bs->mv_stream[1][i].top.weight = PIPE_VIDEO_MV_WEIGHT_MIN; 1838 bs->mv_stream[1][i].bottom.x = bs->mv_stream[1][i].bottom.y = 0; 1839 bs->mv_stream[1][i].bottom.field_select = PIPE_VIDEO_FRAME; 1840 bs->mv_stream[1][i].bottom.weight = PIPE_VIDEO_MV_WEIGHT_MIN; 1841 } 1842} 1843 1844void 1845vl_mpg12_bs_decode(struct vl_mpg12_bs *bs, unsigned num_bytes, const void *buffer, 1846 struct pipe_mpeg12_picture_desc *picture, unsigned num_ycbcr_blocks[3]) 1847{ 1848 int intra_quantizer_matrix[64]; 1849 int non_intra_quantizer_matrix[64]; 1850 1851 const int *scan; 1852 unsigned i; 1853 1854 assert(bs); 1855 assert(num_ycbcr_blocks); 1856 assert(buffer && num_bytes); 1857 1858 bs->num_ycbcr_blocks = num_ycbcr_blocks; 1859 1860 vl_vlc_init(&bs->vlc, buffer, num_bytes); 1861 1862 scan = picture->alternate_scan ? vl_zscan_alternate : vl_zscan_normal; 1863 for (i = 0; i < 64; ++i) { 1864 intra_quantizer_matrix[i] = picture->intra_quantizer_matrix[scan[i]]; 1865 non_intra_quantizer_matrix[i] = picture->non_intra_quantizer_matrix[scan[i]]; 1866 } 1867 1868 while(decode_slice(bs, picture, intra_quantizer_matrix, non_intra_quantizer_matrix)); 1869} 1870