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 "entropymv.h" 14 #include "entropymode.h" 15 #include "onyxd_int.h" 16 #include "findnearmv.h" 17 18 #if CONFIG_DEBUG 19 #include <assert.h> 20 #endif 21 static 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 29 static 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 36 static 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 45 static 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 52 static 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 65 static 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 139 static 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 174 static 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 181 static 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 206 static 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 213 static 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 221 unsigned 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 231 unsigned char vp8_mbsplit_offset[4][16] = { 232 { 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 233 { 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 234 { 0, 2, 8, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 235 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15} 236 }; 237 238 unsigned char vp8_mbsplit_fill_count[4] = {8, 8, 4, 1}; 239 unsigned char vp8_mbsplit_fill_offset[4][16] = { 240 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, 241 { 0, 1, 4, 5, 8, 9, 12, 13, 2, 3, 6, 7, 10, 11, 14, 15}, 242 { 0, 1, 4, 5, 2, 3, 6, 7, 8, 9, 12, 13, 10, 11, 14, 15}, 243 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15} 244 }; 245 246 247 248 249 void vp8_mb_mode_mv_init(VP8D_COMP *pbi) 250 { 251 vp8_reader *const bc = & pbi->bc; 252 MV_CONTEXT *const mvc = pbi->common.fc.mvc; 253 254 pbi->prob_skip_false = 0; 255 if (pbi->common.mb_no_coeff_skip) 256 pbi->prob_skip_false = (vp8_prob)vp8_read_literal(bc, 8); 257 258 if(pbi->common.frame_type != KEY_FRAME) 259 { 260 pbi->prob_intra = (vp8_prob)vp8_read_literal(bc, 8); 261 pbi->prob_last = (vp8_prob)vp8_read_literal(bc, 8); 262 pbi->prob_gf = (vp8_prob)vp8_read_literal(bc, 8); 263 264 if (vp8_read_bit(bc)) 265 { 266 int i = 0; 267 268 do 269 { 270 pbi->common.fc.ymode_prob[i] = (vp8_prob) vp8_read_literal(bc, 8); 271 } 272 while (++i < 4); 273 } 274 275 if (vp8_read_bit(bc)) 276 { 277 int i = 0; 278 279 do 280 { 281 pbi->common.fc.uv_mode_prob[i] = (vp8_prob) vp8_read_literal(bc, 8); 282 } 283 while (++i < 3); 284 } 285 286 read_mvcontexts(bc, mvc); 287 } 288 } 289 290 void vp8_read_mb_modes_mv(VP8D_COMP *pbi, MODE_INFO *mi, MB_MODE_INFO *mbmi, 291 int mb_row, int mb_col) 292 { 293 const MV Zero = { 0, 0}; 294 vp8_reader *const bc = & pbi->bc; 295 MV_CONTEXT *const mvc = pbi->common.fc.mvc; 296 const int mis = pbi->common.mode_info_stride; 297 298 MV *const mv = & mbmi->mv.as_mv; 299 int mb_to_left_edge; 300 int mb_to_right_edge; 301 int mb_to_top_edge; 302 int mb_to_bottom_edge; 303 304 mb_to_top_edge = pbi->mb.mb_to_top_edge; 305 mb_to_bottom_edge = pbi->mb.mb_to_bottom_edge; 306 mb_to_top_edge -= LEFT_TOP_MARGIN; 307 mb_to_bottom_edge += RIGHT_BOTTOM_MARGIN; 308 309 mbmi->need_to_clamp_mvs = 0; 310 /* Distance of Mb to the various image edges. 311 * These specified to 8th pel as they are always compared to MV values that are in 1/8th pel units 312 */ 313 pbi->mb.mb_to_left_edge = 314 mb_to_left_edge = -((mb_col * 16) << 3); 315 mb_to_left_edge -= LEFT_TOP_MARGIN; 316 317 pbi->mb.mb_to_right_edge = 318 mb_to_right_edge = ((pbi->common.mb_cols - 1 - mb_col) * 16) << 3; 319 mb_to_right_edge += RIGHT_BOTTOM_MARGIN; 320 321 /* If required read in new segmentation data for this MB */ 322 if (pbi->mb.update_mb_segmentation_map) 323 vp8_read_mb_features(bc, mbmi, &pbi->mb); 324 325 /* Read the macroblock coeff skip flag if this feature is in use, else default to 0 */ 326 if (pbi->common.mb_no_coeff_skip) 327 mbmi->mb_skip_coeff = vp8_read(bc, pbi->prob_skip_false); 328 else 329 mbmi->mb_skip_coeff = 0; 330 331 if ((mbmi->ref_frame = (MV_REFERENCE_FRAME) vp8_read(bc, pbi->prob_intra))) /* inter MB */ 332 { 333 int rct[4]; 334 vp8_prob mv_ref_p [VP8_MVREFS-1]; 335 MV nearest, nearby, best_mv; 336 337 if (vp8_read(bc, pbi->prob_last)) 338 { 339 mbmi->ref_frame = (MV_REFERENCE_FRAME)((int)mbmi->ref_frame + (int)(1 + vp8_read(bc, pbi->prob_gf))); 340 } 341 342 vp8_find_near_mvs(&pbi->mb, mi, &nearest, &nearby, &best_mv, rct, mbmi->ref_frame, pbi->common.ref_frame_sign_bias); 343 344 vp8_mv_ref_probs(mv_ref_p, rct); 345 346 mbmi->uv_mode = DC_PRED; 347 switch (mbmi->mode = read_mv_ref(bc, mv_ref_p)) 348 { 349 case SPLITMV: 350 { 351 const int s = mbmi->partitioning = 352 vp8_treed_read(bc, vp8_mbsplit_tree, vp8_mbsplit_probs); 353 const int num_p = vp8_mbsplit_count [s]; 354 int j = 0; 355 356 do /* for each subset j */ 357 { 358 B_MODE_INFO bmi; 359 MV *const mv = & bmi.mv.as_mv; 360 361 int k; /* first block in subset j */ 362 int mv_contz; 363 k = vp8_mbsplit_offset[s][j]; 364 365 mv_contz = vp8_mv_cont(&(vp8_left_bmi(mi, k)->mv.as_mv), &(vp8_above_bmi(mi, k, mis)->mv.as_mv)); 366 367 switch (bmi.mode = (B_PREDICTION_MODE) sub_mv_ref(bc, vp8_sub_mv_ref_prob2 [mv_contz])) /*pc->fc.sub_mv_ref_prob))*/ 368 { 369 case NEW4X4: 370 read_mv(bc, mv, (const MV_CONTEXT *) mvc); 371 mv->row += best_mv.row; 372 mv->col += best_mv.col; 373 #ifdef VPX_MODE_COUNT 374 vp8_mv_cont_count[mv_contz][3]++; 375 #endif 376 break; 377 case LEFT4X4: 378 *mv = vp8_left_bmi(mi, k)->mv.as_mv; 379 #ifdef VPX_MODE_COUNT 380 vp8_mv_cont_count[mv_contz][0]++; 381 #endif 382 break; 383 case ABOVE4X4: 384 *mv = vp8_above_bmi(mi, k, mis)->mv.as_mv; 385 #ifdef VPX_MODE_COUNT 386 vp8_mv_cont_count[mv_contz][1]++; 387 #endif 388 break; 389 case ZERO4X4: 390 *mv = Zero; 391 #ifdef VPX_MODE_COUNT 392 vp8_mv_cont_count[mv_contz][2]++; 393 #endif 394 break; 395 default: 396 break; 397 } 398 399 mbmi->need_to_clamp_mvs |= (mv->col < mb_to_left_edge) ? 1 : 0; 400 mbmi->need_to_clamp_mvs |= (mv->col > mb_to_right_edge) ? 1 : 0; 401 mbmi->need_to_clamp_mvs |= (mv->row < mb_to_top_edge) ? 1 : 0; 402 mbmi->need_to_clamp_mvs |= (mv->row > mb_to_bottom_edge) ? 1 : 0; 403 404 { 405 /* Fill (uniform) modes, mvs of jth subset. 406 Must do it here because ensuing subsets can 407 refer back to us via "left" or "above". */ 408 unsigned char *fill_offset; 409 unsigned int fill_count = vp8_mbsplit_fill_count[s]; 410 411 fill_offset = &vp8_mbsplit_fill_offset[s][(unsigned char)j * vp8_mbsplit_fill_count[s]]; 412 413 do { 414 mi->bmi[ *fill_offset] = bmi; 415 fill_offset++; 416 417 }while (--fill_count); 418 } 419 420 } 421 while (++j < num_p); 422 } 423 424 *mv = mi->bmi[15].mv.as_mv; 425 426 break; /* done with SPLITMV */ 427 428 case NEARMV: 429 *mv = nearby; 430 /* Clip "next_nearest" so that it does not extend to far out of image */ 431 mv->col = (mv->col < mb_to_left_edge) ? mb_to_left_edge : mv->col; 432 mv->col = (mv->col > mb_to_right_edge) ? mb_to_right_edge : mv->col; 433 mv->row = (mv->row < mb_to_top_edge) ? mb_to_top_edge : mv->row; 434 mv->row = (mv->row > mb_to_bottom_edge) ? mb_to_bottom_edge : mv->row; 435 goto propagate_mv; 436 437 case NEARESTMV: 438 *mv = nearest; 439 /* Clip "next_nearest" so that it does not extend to far out of image */ 440 mv->col = (mv->col < mb_to_left_edge) ? mb_to_left_edge : mv->col; 441 mv->col = (mv->col > mb_to_right_edge) ? mb_to_right_edge : mv->col; 442 mv->row = (mv->row < mb_to_top_edge) ? mb_to_top_edge : mv->row; 443 mv->row = (mv->row > mb_to_bottom_edge) ? mb_to_bottom_edge : mv->row; 444 goto propagate_mv; 445 446 case ZEROMV: 447 *mv = Zero; 448 goto propagate_mv; 449 450 case NEWMV: 451 read_mv(bc, mv, (const MV_CONTEXT *) mvc); 452 mv->row += best_mv.row; 453 mv->col += best_mv.col; 454 455 /* Don't need to check this on NEARMV and NEARESTMV modes 456 * since those modes clamp the MV. The NEWMV mode does not, 457 * so signal to the prediction stage whether special 458 * handling may be required. 459 */ 460 mbmi->need_to_clamp_mvs = (mv->col < mb_to_left_edge) ? 1 : 0; 461 mbmi->need_to_clamp_mvs |= (mv->col > mb_to_right_edge) ? 1 : 0; 462 mbmi->need_to_clamp_mvs |= (mv->row < mb_to_top_edge) ? 1 : 0; 463 mbmi->need_to_clamp_mvs |= (mv->row > mb_to_bottom_edge) ? 1 : 0; 464 465 propagate_mv: /* same MV throughout */ 466 { 467 /*int i=0; 468 do 469 { 470 mi->bmi[i].mv.as_mv = *mv; 471 } 472 while( ++i < 16);*/ 473 474 mi->bmi[0].mv.as_mv = *mv; 475 mi->bmi[1].mv.as_mv = *mv; 476 mi->bmi[2].mv.as_mv = *mv; 477 mi->bmi[3].mv.as_mv = *mv; 478 mi->bmi[4].mv.as_mv = *mv; 479 mi->bmi[5].mv.as_mv = *mv; 480 mi->bmi[6].mv.as_mv = *mv; 481 mi->bmi[7].mv.as_mv = *mv; 482 mi->bmi[8].mv.as_mv = *mv; 483 mi->bmi[9].mv.as_mv = *mv; 484 mi->bmi[10].mv.as_mv = *mv; 485 mi->bmi[11].mv.as_mv = *mv; 486 mi->bmi[12].mv.as_mv = *mv; 487 mi->bmi[13].mv.as_mv = *mv; 488 mi->bmi[14].mv.as_mv = *mv; 489 mi->bmi[15].mv.as_mv = *mv; 490 } 491 break; 492 default:; 493 #if CONFIG_DEBUG 494 assert(0); 495 #endif 496 } 497 } 498 else 499 { 500 /* MB is intra coded */ 501 int j = 0; 502 do 503 { 504 mi->bmi[j].mv.as_mv = Zero; 505 } 506 while (++j < 16); 507 508 if ((mbmi->mode = (MB_PREDICTION_MODE) vp8_read_ymode(bc, pbi->common.fc.ymode_prob)) == B_PRED) 509 { 510 j = 0; 511 do 512 { 513 mi->bmi[j].mode = (B_PREDICTION_MODE)vp8_read_bmode(bc, pbi->common.fc.bmode_prob); 514 } 515 while (++j < 16); 516 } 517 518 mbmi->uv_mode = (MB_PREDICTION_MODE)vp8_read_uv_mode(bc, pbi->common.fc.uv_mode_prob); 519 } 520 521 } 522 523 void vp8_decode_mode_mvs(VP8D_COMP *pbi) 524 { 525 MODE_INFO *mi = pbi->common.mi; 526 int mb_row = -1; 527 528 vp8_mb_mode_mv_init(pbi); 529 530 while (++mb_row < pbi->common.mb_rows) 531 { 532 int mb_col = -1; 533 int mb_to_top_edge; 534 int mb_to_bottom_edge; 535 536 pbi->mb.mb_to_top_edge = 537 mb_to_top_edge = -((mb_row * 16)) << 3; 538 mb_to_top_edge -= LEFT_TOP_MARGIN; 539 540 pbi->mb.mb_to_bottom_edge = 541 mb_to_bottom_edge = ((pbi->common.mb_rows - 1 - mb_row) * 16) << 3; 542 mb_to_bottom_edge += RIGHT_BOTTOM_MARGIN; 543 544 while (++mb_col < pbi->common.mb_cols) 545 { 546 /*vp8_read_mb_modes_mv(pbi, xd->mode_info_context, &xd->mode_info_context->mbmi, mb_row, mb_col);*/ 547 if(pbi->common.frame_type == KEY_FRAME) 548 vp8_kfread_modes(pbi, mi, mb_row, mb_col); 549 else 550 vp8_read_mb_modes_mv(pbi, mi, &mi->mbmi, mb_row, mb_col); 551 552 mi++; /* next macroblock */ 553 } 554 555 mi++; /* skip left predictor each row */ 556 } 557 } 558 559