| OLD | NEW |
| (Empty) | |
| 1 |
| 2 /* |
| 3 * Copyright (c) 2012 The WebM project authors. All Rights Reserved. |
| 4 * |
| 5 * Use of this source code is governed by a BSD-style license |
| 6 * that can be found in the LICENSE file in the root of the source |
| 7 * tree. An additional intellectual property rights grant can be found |
| 8 * in the file PATENTS. All contributing project authors may |
| 9 * be found in the AUTHORS file in the root of the source tree. |
| 10 */ |
| 11 |
| 12 #include <limits.h> |
| 13 |
| 14 #include "vp9/common/vp9_common.h" |
| 15 #include "vp9/common/vp9_pred_common.h" |
| 16 #include "vp9/common/vp9_seg_common.h" |
| 17 |
| 18 // Returns a context number for the given MB prediction signal |
| 19 int vp9_get_pred_context_switchable_interp(const MACROBLOCKD *xd) { |
| 20 // Note: |
| 21 // The mode info data structure has a one element border above and to the |
| 22 // left of the entries correpsonding to real macroblocks. |
| 23 // The prediction flags in these dummy entries are initialised to 0. |
| 24 const MB_MODE_INFO *const left_mbmi = xd->left_mbmi; |
| 25 const int left_type = xd->left_available && is_inter_block(left_mbmi) ? |
| 26 left_mbmi->interp_filter : SWITCHABLE_FILTERS; |
| 27 const MB_MODE_INFO *const above_mbmi = xd->above_mbmi; |
| 28 const int above_type = xd->up_available && is_inter_block(above_mbmi) ? |
| 29 above_mbmi->interp_filter : SWITCHABLE_FILTERS; |
| 30 |
| 31 if (left_type == above_type) |
| 32 return left_type; |
| 33 else if (left_type == SWITCHABLE_FILTERS && above_type != SWITCHABLE_FILTERS) |
| 34 return above_type; |
| 35 else if (left_type != SWITCHABLE_FILTERS && above_type == SWITCHABLE_FILTERS) |
| 36 return left_type; |
| 37 else |
| 38 return SWITCHABLE_FILTERS; |
| 39 } |
| 40 |
| 41 // The mode info data structure has a one element border above and to the |
| 42 // left of the entries corresponding to real macroblocks. |
| 43 // The prediction flags in these dummy entries are initialized to 0. |
| 44 // 0 - inter/inter, inter/--, --/inter, --/-- |
| 45 // 1 - intra/inter, inter/intra |
| 46 // 2 - intra/--, --/intra |
| 47 // 3 - intra/intra |
| 48 int vp9_get_intra_inter_context(const MACROBLOCKD *xd) { |
| 49 const MB_MODE_INFO *const above_mbmi = xd->above_mbmi; |
| 50 const MB_MODE_INFO *const left_mbmi = xd->left_mbmi; |
| 51 const int has_above = xd->up_available; |
| 52 const int has_left = xd->left_available; |
| 53 |
| 54 if (has_above && has_left) { // both edges available |
| 55 const int above_intra = !is_inter_block(above_mbmi); |
| 56 const int left_intra = !is_inter_block(left_mbmi); |
| 57 return left_intra && above_intra ? 3 |
| 58 : left_intra || above_intra; |
| 59 } else if (has_above || has_left) { // one edge available |
| 60 return 2 * !is_inter_block(has_above ? above_mbmi : left_mbmi); |
| 61 } else { |
| 62 return 0; |
| 63 } |
| 64 } |
| 65 |
| 66 int vp9_get_reference_mode_context(const VP9_COMMON *cm, |
| 67 const MACROBLOCKD *xd) { |
| 68 int ctx; |
| 69 const MB_MODE_INFO *const above_mbmi = xd->above_mbmi; |
| 70 const MB_MODE_INFO *const left_mbmi = xd->left_mbmi; |
| 71 const int has_above = xd->up_available; |
| 72 const int has_left = xd->left_available; |
| 73 // Note: |
| 74 // The mode info data structure has a one element border above and to the |
| 75 // left of the entries correpsonding to real macroblocks. |
| 76 // The prediction flags in these dummy entries are initialised to 0. |
| 77 if (has_above && has_left) { // both edges available |
| 78 if (!has_second_ref(above_mbmi) && !has_second_ref(left_mbmi)) |
| 79 // neither edge uses comp pred (0/1) |
| 80 ctx = (above_mbmi->ref_frame[0] == cm->comp_fixed_ref) ^ |
| 81 (left_mbmi->ref_frame[0] == cm->comp_fixed_ref); |
| 82 else if (!has_second_ref(above_mbmi)) |
| 83 // one of two edges uses comp pred (2/3) |
| 84 ctx = 2 + (above_mbmi->ref_frame[0] == cm->comp_fixed_ref || |
| 85 !is_inter_block(above_mbmi)); |
| 86 else if (!has_second_ref(left_mbmi)) |
| 87 // one of two edges uses comp pred (2/3) |
| 88 ctx = 2 + (left_mbmi->ref_frame[0] == cm->comp_fixed_ref || |
| 89 !is_inter_block(left_mbmi)); |
| 90 else // both edges use comp pred (4) |
| 91 ctx = 4; |
| 92 } else if (has_above || has_left) { // one edge available |
| 93 const MB_MODE_INFO *edge_mbmi = has_above ? above_mbmi : left_mbmi; |
| 94 |
| 95 if (!has_second_ref(edge_mbmi)) |
| 96 // edge does not use comp pred (0/1) |
| 97 ctx = edge_mbmi->ref_frame[0] == cm->comp_fixed_ref; |
| 98 else |
| 99 // edge uses comp pred (3) |
| 100 ctx = 3; |
| 101 } else { // no edges available (1) |
| 102 ctx = 1; |
| 103 } |
| 104 assert(ctx >= 0 && ctx < COMP_INTER_CONTEXTS); |
| 105 return ctx; |
| 106 } |
| 107 |
| 108 // Returns a context number for the given MB prediction signal |
| 109 int vp9_get_pred_context_comp_ref_p(const VP9_COMMON *cm, |
| 110 const MACROBLOCKD *xd) { |
| 111 int pred_context; |
| 112 const MB_MODE_INFO *const above_mbmi = xd->above_mbmi; |
| 113 const MB_MODE_INFO *const left_mbmi = xd->left_mbmi; |
| 114 const int above_in_image = xd->up_available; |
| 115 const int left_in_image = xd->left_available; |
| 116 |
| 117 // Note: |
| 118 // The mode info data structure has a one element border above and to the |
| 119 // left of the entries correpsonding to real macroblocks. |
| 120 // The prediction flags in these dummy entries are initialised to 0. |
| 121 const int fix_ref_idx = cm->ref_frame_sign_bias[cm->comp_fixed_ref]; |
| 122 const int var_ref_idx = !fix_ref_idx; |
| 123 |
| 124 if (above_in_image && left_in_image) { // both edges available |
| 125 const int above_intra = !is_inter_block(above_mbmi); |
| 126 const int left_intra = !is_inter_block(left_mbmi); |
| 127 |
| 128 if (above_intra && left_intra) { // intra/intra (2) |
| 129 pred_context = 2; |
| 130 } else if (above_intra || left_intra) { // intra/inter |
| 131 const MB_MODE_INFO *edge_mbmi = above_intra ? left_mbmi : above_mbmi; |
| 132 |
| 133 if (!has_second_ref(edge_mbmi)) // single pred (1/3) |
| 134 pred_context = 1 + 2 * (edge_mbmi->ref_frame[0] != cm->comp_var_ref[1]); |
| 135 else // comp pred (1/3) |
| 136 pred_context = 1 + 2 * (edge_mbmi->ref_frame[var_ref_idx] |
| 137 != cm->comp_var_ref[1]); |
| 138 } else { // inter/inter |
| 139 const int l_sg = !has_second_ref(left_mbmi); |
| 140 const int a_sg = !has_second_ref(above_mbmi); |
| 141 const MV_REFERENCE_FRAME vrfa = a_sg ? above_mbmi->ref_frame[0] |
| 142 : above_mbmi->ref_frame[var_ref_idx]; |
| 143 const MV_REFERENCE_FRAME vrfl = l_sg ? left_mbmi->ref_frame[0] |
| 144 : left_mbmi->ref_frame[var_ref_idx]; |
| 145 |
| 146 if (vrfa == vrfl && cm->comp_var_ref[1] == vrfa) { |
| 147 pred_context = 0; |
| 148 } else if (l_sg && a_sg) { // single/single |
| 149 if ((vrfa == cm->comp_fixed_ref && vrfl == cm->comp_var_ref[0]) || |
| 150 (vrfl == cm->comp_fixed_ref && vrfa == cm->comp_var_ref[0])) |
| 151 pred_context = 4; |
| 152 else if (vrfa == vrfl) |
| 153 pred_context = 3; |
| 154 else |
| 155 pred_context = 1; |
| 156 } else if (l_sg || a_sg) { // single/comp |
| 157 const MV_REFERENCE_FRAME vrfc = l_sg ? vrfa : vrfl; |
| 158 const MV_REFERENCE_FRAME rfs = a_sg ? vrfa : vrfl; |
| 159 if (vrfc == cm->comp_var_ref[1] && rfs != cm->comp_var_ref[1]) |
| 160 pred_context = 1; |
| 161 else if (rfs == cm->comp_var_ref[1] && vrfc != cm->comp_var_ref[1]) |
| 162 pred_context = 2; |
| 163 else |
| 164 pred_context = 4; |
| 165 } else if (vrfa == vrfl) { // comp/comp |
| 166 pred_context = 4; |
| 167 } else { |
| 168 pred_context = 2; |
| 169 } |
| 170 } |
| 171 } else if (above_in_image || left_in_image) { // one edge available |
| 172 const MB_MODE_INFO *edge_mbmi = above_in_image ? above_mbmi : left_mbmi; |
| 173 |
| 174 if (!is_inter_block(edge_mbmi)) { |
| 175 pred_context = 2; |
| 176 } else { |
| 177 if (has_second_ref(edge_mbmi)) |
| 178 pred_context = 4 * (edge_mbmi->ref_frame[var_ref_idx] |
| 179 != cm->comp_var_ref[1]); |
| 180 else |
| 181 pred_context = 3 * (edge_mbmi->ref_frame[0] != cm->comp_var_ref[1]); |
| 182 } |
| 183 } else { // no edges available (2) |
| 184 pred_context = 2; |
| 185 } |
| 186 assert(pred_context >= 0 && pred_context < REF_CONTEXTS); |
| 187 |
| 188 return pred_context; |
| 189 } |
| 190 |
| 191 int vp9_get_pred_context_single_ref_p1(const MACROBLOCKD *xd) { |
| 192 int pred_context; |
| 193 const MB_MODE_INFO *const above_mbmi = xd->above_mbmi; |
| 194 const MB_MODE_INFO *const left_mbmi = xd->left_mbmi; |
| 195 const int has_above = xd->up_available; |
| 196 const int has_left = xd->left_available; |
| 197 // Note: |
| 198 // The mode info data structure has a one element border above and to the |
| 199 // left of the entries correpsonding to real macroblocks. |
| 200 // The prediction flags in these dummy entries are initialised to 0. |
| 201 if (has_above && has_left) { // both edges available |
| 202 const int above_intra = !is_inter_block(above_mbmi); |
| 203 const int left_intra = !is_inter_block(left_mbmi); |
| 204 |
| 205 if (above_intra && left_intra) { // intra/intra |
| 206 pred_context = 2; |
| 207 } else if (above_intra || left_intra) { // intra/inter or inter/intra |
| 208 const MB_MODE_INFO *edge_mbmi = above_intra ? left_mbmi : above_mbmi; |
| 209 if (!has_second_ref(edge_mbmi)) |
| 210 pred_context = 4 * (edge_mbmi->ref_frame[0] == LAST_FRAME); |
| 211 else |
| 212 pred_context = 1 + (edge_mbmi->ref_frame[0] == LAST_FRAME || |
| 213 edge_mbmi->ref_frame[1] == LAST_FRAME); |
| 214 } else { // inter/inter |
| 215 const int above_has_second = has_second_ref(above_mbmi); |
| 216 const int left_has_second = has_second_ref(left_mbmi); |
| 217 const MV_REFERENCE_FRAME above0 = above_mbmi->ref_frame[0]; |
| 218 const MV_REFERENCE_FRAME above1 = above_mbmi->ref_frame[1]; |
| 219 const MV_REFERENCE_FRAME left0 = left_mbmi->ref_frame[0]; |
| 220 const MV_REFERENCE_FRAME left1 = left_mbmi->ref_frame[1]; |
| 221 |
| 222 if (above_has_second && left_has_second) { |
| 223 pred_context = 1 + (above0 == LAST_FRAME || above1 == LAST_FRAME || |
| 224 left0 == LAST_FRAME || left1 == LAST_FRAME); |
| 225 } else if (above_has_second || left_has_second) { |
| 226 const MV_REFERENCE_FRAME rfs = !above_has_second ? above0 : left0; |
| 227 const MV_REFERENCE_FRAME crf1 = above_has_second ? above0 : left0; |
| 228 const MV_REFERENCE_FRAME crf2 = above_has_second ? above1 : left1; |
| 229 |
| 230 if (rfs == LAST_FRAME) |
| 231 pred_context = 3 + (crf1 == LAST_FRAME || crf2 == LAST_FRAME); |
| 232 else |
| 233 pred_context = (crf1 == LAST_FRAME || crf2 == LAST_FRAME); |
| 234 } else { |
| 235 pred_context = 2 * (above0 == LAST_FRAME) + 2 * (left0 == LAST_FRAME); |
| 236 } |
| 237 } |
| 238 } else if (has_above || has_left) { // one edge available |
| 239 const MB_MODE_INFO *edge_mbmi = has_above ? above_mbmi : left_mbmi; |
| 240 if (!is_inter_block(edge_mbmi)) { // intra |
| 241 pred_context = 2; |
| 242 } else { // inter |
| 243 if (!has_second_ref(edge_mbmi)) |
| 244 pred_context = 4 * (edge_mbmi->ref_frame[0] == LAST_FRAME); |
| 245 else |
| 246 pred_context = 1 + (edge_mbmi->ref_frame[0] == LAST_FRAME || |
| 247 edge_mbmi->ref_frame[1] == LAST_FRAME); |
| 248 } |
| 249 } else { // no edges available |
| 250 pred_context = 2; |
| 251 } |
| 252 |
| 253 assert(pred_context >= 0 && pred_context < REF_CONTEXTS); |
| 254 return pred_context; |
| 255 } |
| 256 |
| 257 int vp9_get_pred_context_single_ref_p2(const MACROBLOCKD *xd) { |
| 258 int pred_context; |
| 259 const MB_MODE_INFO *const above_mbmi = xd->above_mbmi; |
| 260 const MB_MODE_INFO *const left_mbmi = xd->left_mbmi; |
| 261 const int has_above = xd->up_available; |
| 262 const int has_left = xd->left_available; |
| 263 |
| 264 // Note: |
| 265 // The mode info data structure has a one element border above and to the |
| 266 // left of the entries correpsonding to real macroblocks. |
| 267 // The prediction flags in these dummy entries are initialised to 0. |
| 268 if (has_above && has_left) { // both edges available |
| 269 const int above_intra = !is_inter_block(above_mbmi); |
| 270 const int left_intra = !is_inter_block(left_mbmi); |
| 271 |
| 272 if (above_intra && left_intra) { // intra/intra |
| 273 pred_context = 2; |
| 274 } else if (above_intra || left_intra) { // intra/inter or inter/intra |
| 275 const MB_MODE_INFO *edge_mbmi = above_intra ? left_mbmi : above_mbmi; |
| 276 if (!has_second_ref(edge_mbmi)) { |
| 277 if (edge_mbmi->ref_frame[0] == LAST_FRAME) |
| 278 pred_context = 3; |
| 279 else |
| 280 pred_context = 4 * (edge_mbmi->ref_frame[0] == GOLDEN_FRAME); |
| 281 } else { |
| 282 pred_context = 1 + 2 * (edge_mbmi->ref_frame[0] == GOLDEN_FRAME || |
| 283 edge_mbmi->ref_frame[1] == GOLDEN_FRAME); |
| 284 } |
| 285 } else { // inter/inter |
| 286 const int above_has_second = has_second_ref(above_mbmi); |
| 287 const int left_has_second = has_second_ref(left_mbmi); |
| 288 const MV_REFERENCE_FRAME above0 = above_mbmi->ref_frame[0]; |
| 289 const MV_REFERENCE_FRAME above1 = above_mbmi->ref_frame[1]; |
| 290 const MV_REFERENCE_FRAME left0 = left_mbmi->ref_frame[0]; |
| 291 const MV_REFERENCE_FRAME left1 = left_mbmi->ref_frame[1]; |
| 292 |
| 293 if (above_has_second && left_has_second) { |
| 294 if (above0 == left0 && above1 == left1) |
| 295 pred_context = 3 * (above0 == GOLDEN_FRAME || |
| 296 above1 == GOLDEN_FRAME || |
| 297 left0 == GOLDEN_FRAME || |
| 298 left1 == GOLDEN_FRAME); |
| 299 else |
| 300 pred_context = 2; |
| 301 } else if (above_has_second || left_has_second) { |
| 302 const MV_REFERENCE_FRAME rfs = !above_has_second ? above0 : left0; |
| 303 const MV_REFERENCE_FRAME crf1 = above_has_second ? above0 : left0; |
| 304 const MV_REFERENCE_FRAME crf2 = above_has_second ? above1 : left1; |
| 305 |
| 306 if (rfs == GOLDEN_FRAME) |
| 307 pred_context = 3 + (crf1 == GOLDEN_FRAME || crf2 == GOLDEN_FRAME); |
| 308 else if (rfs == ALTREF_FRAME) |
| 309 pred_context = crf1 == GOLDEN_FRAME || crf2 == GOLDEN_FRAME; |
| 310 else |
| 311 pred_context = 1 + 2 * (crf1 == GOLDEN_FRAME || crf2 == GOLDEN_FRAME); |
| 312 } else { |
| 313 if (above0 == LAST_FRAME && left0 == LAST_FRAME) { |
| 314 pred_context = 3; |
| 315 } else if (above0 == LAST_FRAME || left0 == LAST_FRAME) { |
| 316 const MV_REFERENCE_FRAME edge0 = (above0 == LAST_FRAME) ? left0 |
| 317 : above0; |
| 318 pred_context = 4 * (edge0 == GOLDEN_FRAME); |
| 319 } else { |
| 320 pred_context = 2 * (above0 == GOLDEN_FRAME) + |
| 321 2 * (left0 == GOLDEN_FRAME); |
| 322 } |
| 323 } |
| 324 } |
| 325 } else if (has_above || has_left) { // one edge available |
| 326 const MB_MODE_INFO *edge_mbmi = has_above ? above_mbmi : left_mbmi; |
| 327 |
| 328 if (!is_inter_block(edge_mbmi) || |
| 329 (edge_mbmi->ref_frame[0] == LAST_FRAME && !has_second_ref(edge_mbmi))) |
| 330 pred_context = 2; |
| 331 else if (!has_second_ref(edge_mbmi)) |
| 332 pred_context = 4 * (edge_mbmi->ref_frame[0] == GOLDEN_FRAME); |
| 333 else |
| 334 pred_context = 3 * (edge_mbmi->ref_frame[0] == GOLDEN_FRAME || |
| 335 edge_mbmi->ref_frame[1] == GOLDEN_FRAME); |
| 336 } else { // no edges available (2) |
| 337 pred_context = 2; |
| 338 } |
| 339 assert(pred_context >= 0 && pred_context < REF_CONTEXTS); |
| 340 return pred_context; |
| 341 } |
| 342 // Returns a context number for the given MB prediction signal |
| 343 // The mode info data structure has a one element border above and to the |
| 344 // left of the entries corresponding to real blocks. |
| 345 // The prediction flags in these dummy entries are initialized to 0. |
| 346 int vp9_get_tx_size_context(const MACROBLOCKD *xd) { |
| 347 const int max_tx_size = max_txsize_lookup[xd->mi[0]->mbmi.sb_type]; |
| 348 const MB_MODE_INFO *const above_mbmi = xd->above_mbmi; |
| 349 const MB_MODE_INFO *const left_mbmi = xd->left_mbmi; |
| 350 const int has_above = xd->up_available; |
| 351 const int has_left = xd->left_available; |
| 352 int above_ctx = (has_above && !above_mbmi->skip) ? (int)above_mbmi->tx_size |
| 353 : max_tx_size; |
| 354 int left_ctx = (has_left && !left_mbmi->skip) ? (int)left_mbmi->tx_size |
| 355 : max_tx_size; |
| 356 if (!has_left) |
| 357 left_ctx = above_ctx; |
| 358 |
| 359 if (!has_above) |
| 360 above_ctx = left_ctx; |
| 361 |
| 362 return (above_ctx + left_ctx) > max_tx_size; |
| 363 } |
| 364 |
| 365 int vp9_get_segment_id(const VP9_COMMON *cm, const uint8_t *segment_ids, |
| 366 BLOCK_SIZE bsize, int mi_row, int mi_col) { |
| 367 const int mi_offset = mi_row * cm->mi_cols + mi_col; |
| 368 const int bw = num_8x8_blocks_wide_lookup[bsize]; |
| 369 const int bh = num_8x8_blocks_high_lookup[bsize]; |
| 370 const int xmis = MIN(cm->mi_cols - mi_col, bw); |
| 371 const int ymis = MIN(cm->mi_rows - mi_row, bh); |
| 372 int x, y, segment_id = INT_MAX; |
| 373 |
| 374 for (y = 0; y < ymis; y++) |
| 375 for (x = 0; x < xmis; x++) |
| 376 segment_id = MIN(segment_id, |
| 377 segment_ids[mi_offset + y * cm->mi_cols + x]); |
| 378 |
| 379 assert(segment_id >= 0 && segment_id < MAX_SEGMENTS); |
| 380 return segment_id; |
| 381 } |
| OLD | NEW |