1/* 2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 12#include "treereader.h" 13#include "vp8/common/entropymv.h" 14#include "vp8/common/entropymode.h" 15#include "onyxd_int.h" 16#include "vp8/common/findnearmv.h" 17 18#if CONFIG_DEBUG 19#include <assert.h> 20#endif 21static int vp8_read_bmode(vp8_reader *bc, const vp8_prob *p) 22{ 23 const int i = vp8_treed_read(bc, vp8_bmode_tree, p); 24 25 return i; 26} 27 28 29static int vp8_read_ymode(vp8_reader *bc, const vp8_prob *p) 30{ 31 const int i = vp8_treed_read(bc, vp8_ymode_tree, p); 32 33 return i; 34} 35 36static int vp8_kfread_ymode(vp8_reader *bc, const vp8_prob *p) 37{ 38 const int i = vp8_treed_read(bc, vp8_kf_ymode_tree, p); 39 40 return i; 41} 42 43 44 45static int vp8_read_uv_mode(vp8_reader *bc, const vp8_prob *p) 46{ 47 const int i = vp8_treed_read(bc, vp8_uv_mode_tree, p); 48 49 return i; 50} 51 52static void vp8_read_mb_features(vp8_reader *r, MB_MODE_INFO *mi, MACROBLOCKD *x) 53{ 54 /* Is segmentation enabled */ 55 if (x->segmentation_enabled && x->update_mb_segmentation_map) 56 { 57 /* If so then read the segment id. */ 58 if (vp8_read(r, x->mb_segment_tree_probs[0])) 59 mi->segment_id = (unsigned char)(2 + vp8_read(r, x->mb_segment_tree_probs[2])); 60 else 61 mi->segment_id = (unsigned char)(vp8_read(r, x->mb_segment_tree_probs[1])); 62 } 63} 64 65static void vp8_kfread_modes(VP8D_COMP *pbi, MODE_INFO *m, int mb_row, int mb_col) 66{ 67 vp8_reader *const bc = & pbi->bc; 68 const int mis = pbi->common.mode_info_stride; 69 70 { 71 MB_PREDICTION_MODE y_mode; 72 73 /* Read the Macroblock segmentation map if it is being updated explicitly this frame (reset to 0 above by default) 74 * By default on a key frame reset all MBs to segment 0 75 */ 76 m->mbmi.segment_id = 0; 77 78 if (pbi->mb.update_mb_segmentation_map) 79 vp8_read_mb_features(bc, &m->mbmi, &pbi->mb); 80 81 /* Read the macroblock coeff skip flag if this feature is in use, else default to 0 */ 82 if (pbi->common.mb_no_coeff_skip) 83 m->mbmi.mb_skip_coeff = vp8_read(bc, pbi->prob_skip_false); 84 else 85 m->mbmi.mb_skip_coeff = 0; 86 87 y_mode = (MB_PREDICTION_MODE) vp8_kfread_ymode(bc, pbi->common.kf_ymode_prob); 88 89 m->mbmi.ref_frame = INTRA_FRAME; 90 91 if ((m->mbmi.mode = y_mode) == B_PRED) 92 { 93 int i = 0; 94 95 do 96 { 97 const B_PREDICTION_MODE A = vp8_above_bmi(m, i, mis)->mode; 98 const B_PREDICTION_MODE L = vp8_left_bmi(m, i)->mode; 99 100 m->bmi[i].mode = (B_PREDICTION_MODE) vp8_read_bmode(bc, pbi->common.kf_bmode_prob [A] [L]); 101 } 102 while (++i < 16); 103 } 104 else 105 { 106 int BMode; 107 int i = 0; 108 109 switch (y_mode) 110 { 111 case DC_PRED: 112 BMode = B_DC_PRED; 113 break; 114 case V_PRED: 115 BMode = B_VE_PRED; 116 break; 117 case H_PRED: 118 BMode = B_HE_PRED; 119 break; 120 case TM_PRED: 121 BMode = B_TM_PRED; 122 break; 123 default: 124 BMode = B_DC_PRED; 125 break; 126 } 127 128 do 129 { 130 m->bmi[i].mode = (B_PREDICTION_MODE)BMode; 131 } 132 while (++i < 16); 133 } 134 135 m->mbmi.uv_mode = (MB_PREDICTION_MODE)vp8_read_uv_mode(bc, pbi->common.kf_uv_mode_prob); 136 } 137} 138 139static int read_mvcomponent(vp8_reader *r, const MV_CONTEXT *mvc) 140{ 141 const vp8_prob *const p = (const vp8_prob *) mvc; 142 int x = 0; 143 144 if (vp8_read(r, p [mvpis_short])) /* Large */ 145 { 146 int i = 0; 147 148 do 149 { 150 x += vp8_read(r, p [MVPbits + i]) << i; 151 } 152 while (++i < 3); 153 154 i = mvlong_width - 1; /* Skip bit 3, which is sometimes implicit */ 155 156 do 157 { 158 x += vp8_read(r, p [MVPbits + i]) << i; 159 } 160 while (--i > 3); 161 162 if (!(x & 0xFFF0) || vp8_read(r, p [MVPbits + 3])) 163 x += 8; 164 } 165 else /* small */ 166 x = vp8_treed_read(r, vp8_small_mvtree, p + MVPshort); 167 168 if (x && vp8_read(r, p [MVPsign])) 169 x = -x; 170 171 return x; 172} 173 174static void read_mv(vp8_reader *r, MV *mv, const MV_CONTEXT *mvc) 175{ 176 mv->row = (short)(read_mvcomponent(r, mvc) << 1); 177 mv->col = (short)(read_mvcomponent(r, ++mvc) << 1); 178} 179 180 181static void read_mvcontexts(vp8_reader *bc, MV_CONTEXT *mvc) 182{ 183 int i = 0; 184 185 do 186 { 187 const vp8_prob *up = vp8_mv_update_probs[i].prob; 188 vp8_prob *p = (vp8_prob *)(mvc + i); 189 vp8_prob *const pstop = p + MVPcount; 190 191 do 192 { 193 if (vp8_read(bc, *up++)) 194 { 195 const vp8_prob x = (vp8_prob)vp8_read_literal(bc, 7); 196 197 *p = x ? x << 1 : 1; 198 } 199 } 200 while (++p < pstop); 201 } 202 while (++i < 2); 203} 204 205 206static MB_PREDICTION_MODE read_mv_ref(vp8_reader *bc, const vp8_prob *p) 207{ 208 const int i = vp8_treed_read(bc, vp8_mv_ref_tree, p); 209 210 return (MB_PREDICTION_MODE)i; 211} 212 213static MB_PREDICTION_MODE sub_mv_ref(vp8_reader *bc, const vp8_prob *p) 214{ 215 const int i = vp8_treed_read(bc, vp8_sub_mv_ref_tree, p); 216 217 return (MB_PREDICTION_MODE)i; 218} 219 220#ifdef VPX_MODE_COUNT 221unsigned int vp8_mv_cont_count[5][4] = 222{ 223 { 0, 0, 0, 0 }, 224 { 0, 0, 0, 0 }, 225 { 0, 0, 0, 0 }, 226 { 0, 0, 0, 0 }, 227 { 0, 0, 0, 0 } 228}; 229#endif 230 231static const unsigned char mbsplit_fill_count[4] = {8, 8, 4, 1}; 232static const unsigned char mbsplit_fill_offset[4][16] = { 233 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, 234 { 0, 1, 4, 5, 8, 9, 12, 13, 2, 3, 6, 7, 10, 11, 14, 15}, 235 { 0, 1, 4, 5, 2, 3, 6, 7, 8, 9, 12, 13, 10, 11, 14, 15}, 236 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15} 237}; 238 239 240 241 242static void mb_mode_mv_init(VP8D_COMP *pbi) 243{ 244 vp8_reader *const bc = & pbi->bc; 245 MV_CONTEXT *const mvc = pbi->common.fc.mvc; 246 247 pbi->prob_skip_false = 0; 248 if (pbi->common.mb_no_coeff_skip) 249 pbi->prob_skip_false = (vp8_prob)vp8_read_literal(bc, 8); 250 251 if(pbi->common.frame_type != KEY_FRAME) 252 { 253 pbi->prob_intra = (vp8_prob)vp8_read_literal(bc, 8); 254 pbi->prob_last = (vp8_prob)vp8_read_literal(bc, 8); 255 pbi->prob_gf = (vp8_prob)vp8_read_literal(bc, 8); 256 257 if (vp8_read_bit(bc)) 258 { 259 int i = 0; 260 261 do 262 { 263 pbi->common.fc.ymode_prob[i] = (vp8_prob) vp8_read_literal(bc, 8); 264 } 265 while (++i < 4); 266 } 267 268 if (vp8_read_bit(bc)) 269 { 270 int i = 0; 271 272 do 273 { 274 pbi->common.fc.uv_mode_prob[i] = (vp8_prob) vp8_read_literal(bc, 8); 275 } 276 while (++i < 3); 277 } 278 279 read_mvcontexts(bc, mvc); 280 } 281} 282 283static void read_mb_modes_mv(VP8D_COMP *pbi, MODE_INFO *mi, MB_MODE_INFO *mbmi, 284 int mb_row, int mb_col) 285{ 286 const MV Zero = { 0, 0}; 287 vp8_reader *const bc = & pbi->bc; 288 MV_CONTEXT *const mvc = pbi->common.fc.mvc; 289 const int mis = pbi->common.mode_info_stride; 290 291 MV *const mv = & mbmi->mv.as_mv; 292 int mb_to_left_edge; 293 int mb_to_right_edge; 294 int mb_to_top_edge; 295 int mb_to_bottom_edge; 296 297 mb_to_top_edge = pbi->mb.mb_to_top_edge; 298 mb_to_bottom_edge = pbi->mb.mb_to_bottom_edge; 299 mb_to_top_edge -= LEFT_TOP_MARGIN; 300 mb_to_bottom_edge += RIGHT_BOTTOM_MARGIN; 301 302 mbmi->need_to_clamp_mvs = 0; 303 /* Distance of Mb to the various image edges. 304 * These specified to 8th pel as they are always compared to MV values that are in 1/8th pel units 305 */ 306 pbi->mb.mb_to_left_edge = 307 mb_to_left_edge = -((mb_col * 16) << 3); 308 mb_to_left_edge -= LEFT_TOP_MARGIN; 309 310 pbi->mb.mb_to_right_edge = 311 mb_to_right_edge = ((pbi->common.mb_cols - 1 - mb_col) * 16) << 3; 312 mb_to_right_edge += RIGHT_BOTTOM_MARGIN; 313 314 /* If required read in new segmentation data for this MB */ 315 if (pbi->mb.update_mb_segmentation_map) 316 vp8_read_mb_features(bc, mbmi, &pbi->mb); 317 318 /* Read the macroblock coeff skip flag if this feature is in use, else default to 0 */ 319 if (pbi->common.mb_no_coeff_skip) 320 mbmi->mb_skip_coeff = vp8_read(bc, pbi->prob_skip_false); 321 else 322 mbmi->mb_skip_coeff = 0; 323 324 if ((mbmi->ref_frame = (MV_REFERENCE_FRAME) vp8_read(bc, pbi->prob_intra))) /* inter MB */ 325 { 326 int rct[4]; 327 vp8_prob mv_ref_p [VP8_MVREFS-1]; 328 MV nearest, nearby, best_mv; 329 330 if (vp8_read(bc, pbi->prob_last)) 331 { 332 mbmi->ref_frame = (MV_REFERENCE_FRAME)((int)mbmi->ref_frame + (int)(1 + vp8_read(bc, pbi->prob_gf))); 333 } 334 335 vp8_find_near_mvs(&pbi->mb, mi, &nearest, &nearby, &best_mv, rct, mbmi->ref_frame, pbi->common.ref_frame_sign_bias); 336 337 vp8_mv_ref_probs(mv_ref_p, rct); 338 339 mbmi->uv_mode = DC_PRED; 340 switch (mbmi->mode = read_mv_ref(bc, mv_ref_p)) 341 { 342 case SPLITMV: 343 { 344 const int s = mbmi->partitioning = 345 vp8_treed_read(bc, vp8_mbsplit_tree, vp8_mbsplit_probs); 346 const int num_p = vp8_mbsplit_count [s]; 347 int j = 0; 348 349 do /* for each subset j */ 350 { 351 B_MODE_INFO bmi; 352 MV *const mv = & bmi.mv.as_mv; 353 354 int k; /* first block in subset j */ 355 int mv_contz; 356 k = vp8_mbsplit_offset[s][j]; 357 358 mv_contz = vp8_mv_cont(&(vp8_left_bmi(mi, k)->mv.as_mv), &(vp8_above_bmi(mi, k, mis)->mv.as_mv)); 359 360 switch (bmi.mode = (B_PREDICTION_MODE) sub_mv_ref(bc, vp8_sub_mv_ref_prob2 [mv_contz])) /*pc->fc.sub_mv_ref_prob))*/ 361 { 362 case NEW4X4: 363 read_mv(bc, mv, (const MV_CONTEXT *) mvc); 364 mv->row += best_mv.row; 365 mv->col += best_mv.col; 366 #ifdef VPX_MODE_COUNT 367 vp8_mv_cont_count[mv_contz][3]++; 368 #endif 369 break; 370 case LEFT4X4: 371 *mv = vp8_left_bmi(mi, k)->mv.as_mv; 372 #ifdef VPX_MODE_COUNT 373 vp8_mv_cont_count[mv_contz][0]++; 374 #endif 375 break; 376 case ABOVE4X4: 377 *mv = vp8_above_bmi(mi, k, mis)->mv.as_mv; 378 #ifdef VPX_MODE_COUNT 379 vp8_mv_cont_count[mv_contz][1]++; 380 #endif 381 break; 382 case ZERO4X4: 383 *mv = Zero; 384 #ifdef VPX_MODE_COUNT 385 vp8_mv_cont_count[mv_contz][2]++; 386 #endif 387 break; 388 default: 389 break; 390 } 391 392 mbmi->need_to_clamp_mvs |= (mv->col < mb_to_left_edge) ? 1 : 0; 393 mbmi->need_to_clamp_mvs |= (mv->col > mb_to_right_edge) ? 1 : 0; 394 mbmi->need_to_clamp_mvs |= (mv->row < mb_to_top_edge) ? 1 : 0; 395 mbmi->need_to_clamp_mvs |= (mv->row > mb_to_bottom_edge) ? 1 : 0; 396 397 { 398 /* Fill (uniform) modes, mvs of jth subset. 399 Must do it here because ensuing subsets can 400 refer back to us via "left" or "above". */ 401 const unsigned char *fill_offset; 402 unsigned int fill_count = mbsplit_fill_count[s]; 403 404 fill_offset = &mbsplit_fill_offset[s][(unsigned char)j * mbsplit_fill_count[s]]; 405 406 do { 407 mi->bmi[ *fill_offset] = bmi; 408 fill_offset++; 409 410 }while (--fill_count); 411 } 412 413 } 414 while (++j < num_p); 415 } 416 417 *mv = mi->bmi[15].mv.as_mv; 418 419 break; /* done with SPLITMV */ 420 421 case NEARMV: 422 *mv = nearby; 423 /* Clip "next_nearest" so that it does not extend to far out of image */ 424 mv->col = (mv->col < mb_to_left_edge) ? mb_to_left_edge : mv->col; 425 mv->col = (mv->col > mb_to_right_edge) ? mb_to_right_edge : mv->col; 426 mv->row = (mv->row < mb_to_top_edge) ? mb_to_top_edge : mv->row; 427 mv->row = (mv->row > mb_to_bottom_edge) ? mb_to_bottom_edge : mv->row; 428 goto propagate_mv; 429 430 case NEARESTMV: 431 *mv = nearest; 432 /* Clip "next_nearest" so that it does not extend to far out of image */ 433 mv->col = (mv->col < mb_to_left_edge) ? mb_to_left_edge : mv->col; 434 mv->col = (mv->col > mb_to_right_edge) ? mb_to_right_edge : mv->col; 435 mv->row = (mv->row < mb_to_top_edge) ? mb_to_top_edge : mv->row; 436 mv->row = (mv->row > mb_to_bottom_edge) ? mb_to_bottom_edge : mv->row; 437 goto propagate_mv; 438 439 case ZEROMV: 440 *mv = Zero; 441 goto propagate_mv; 442 443 case NEWMV: 444 read_mv(bc, mv, (const MV_CONTEXT *) mvc); 445 mv->row += best_mv.row; 446 mv->col += best_mv.col; 447 448 /* Don't need to check this on NEARMV and NEARESTMV modes 449 * since those modes clamp the MV. The NEWMV mode does not, 450 * so signal to the prediction stage whether special 451 * handling may be required. 452 */ 453 mbmi->need_to_clamp_mvs = (mv->col < mb_to_left_edge) ? 1 : 0; 454 mbmi->need_to_clamp_mvs |= (mv->col > mb_to_right_edge) ? 1 : 0; 455 mbmi->need_to_clamp_mvs |= (mv->row < mb_to_top_edge) ? 1 : 0; 456 mbmi->need_to_clamp_mvs |= (mv->row > mb_to_bottom_edge) ? 1 : 0; 457 458 propagate_mv: /* same MV throughout */ 459 { 460 /*int i=0; 461 do 462 { 463 mi->bmi[i].mv.as_mv = *mv; 464 } 465 while( ++i < 16);*/ 466 467 mi->bmi[0].mv.as_mv = *mv; 468 mi->bmi[1].mv.as_mv = *mv; 469 mi->bmi[2].mv.as_mv = *mv; 470 mi->bmi[3].mv.as_mv = *mv; 471 mi->bmi[4].mv.as_mv = *mv; 472 mi->bmi[5].mv.as_mv = *mv; 473 mi->bmi[6].mv.as_mv = *mv; 474 mi->bmi[7].mv.as_mv = *mv; 475 mi->bmi[8].mv.as_mv = *mv; 476 mi->bmi[9].mv.as_mv = *mv; 477 mi->bmi[10].mv.as_mv = *mv; 478 mi->bmi[11].mv.as_mv = *mv; 479 mi->bmi[12].mv.as_mv = *mv; 480 mi->bmi[13].mv.as_mv = *mv; 481 mi->bmi[14].mv.as_mv = *mv; 482 mi->bmi[15].mv.as_mv = *mv; 483 } 484 break; 485 default:; 486 #if CONFIG_DEBUG 487 assert(0); 488 #endif 489 } 490 } 491 else 492 { 493 /* MB is intra coded */ 494 int j = 0; 495 do 496 { 497 mi->bmi[j].mv.as_mv = Zero; 498 } 499 while (++j < 16); 500 501 if ((mbmi->mode = (MB_PREDICTION_MODE) vp8_read_ymode(bc, pbi->common.fc.ymode_prob)) == B_PRED) 502 { 503 j = 0; 504 do 505 { 506 mi->bmi[j].mode = (B_PREDICTION_MODE)vp8_read_bmode(bc, pbi->common.fc.bmode_prob); 507 } 508 while (++j < 16); 509 } 510 511 mbmi->uv_mode = (MB_PREDICTION_MODE)vp8_read_uv_mode(bc, pbi->common.fc.uv_mode_prob); 512 } 513 514} 515 516void vp8_decode_mode_mvs(VP8D_COMP *pbi) 517{ 518 MODE_INFO *mi = pbi->common.mi; 519 int mb_row = -1; 520 521 mb_mode_mv_init(pbi); 522 523 while (++mb_row < pbi->common.mb_rows) 524 { 525 int mb_col = -1; 526 int mb_to_top_edge; 527 int mb_to_bottom_edge; 528 529 pbi->mb.mb_to_top_edge = 530 mb_to_top_edge = -((mb_row * 16)) << 3; 531 mb_to_top_edge -= LEFT_TOP_MARGIN; 532 533 pbi->mb.mb_to_bottom_edge = 534 mb_to_bottom_edge = ((pbi->common.mb_rows - 1 - mb_row) * 16) << 3; 535 mb_to_bottom_edge += RIGHT_BOTTOM_MARGIN; 536 537 while (++mb_col < pbi->common.mb_cols) 538 { 539 /*read_mb_modes_mv(pbi, xd->mode_info_context, &xd->mode_info_context->mbmi, mb_row, mb_col);*/ 540 if(pbi->common.frame_type == KEY_FRAME) 541 vp8_kfread_modes(pbi, mi, mb_row, mb_col); 542 else 543 read_mb_modes_mv(pbi, mi, &mi->mbmi, mb_row, mb_col); 544 545 mi++; /* next macroblock */ 546 } 547 548 mi++; /* skip left predictor each row */ 549 } 550} 551 552