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