vp9_pred_common.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * Copyright (c) 2012 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. #include "vp9/common/vp9_common.h"
  11. #include "vp9/common/vp9_pred_common.h"
  12. #include "vp9/common/vp9_seg_common.h"
  13. // Returns a context number for the given MB prediction signal
  14. int vp9_get_pred_context_switchable_interp(const MACROBLOCKD *xd) {
  15. // Note:
  16. // The mode info data structure has a one element border above and to the
  17. // left of the entries corresponding to real macroblocks.
  18. // The prediction flags in these dummy entries are initialized to 0.
  19. const MODE_INFO *const left_mi = xd->left_mi;
  20. const int left_type = left_mi && is_inter_block(left_mi) ?
  21. left_mi->interp_filter : SWITCHABLE_FILTERS;
  22. const MODE_INFO *const above_mi = xd->above_mi;
  23. const int above_type = above_mi && is_inter_block(above_mi) ?
  24. above_mi->interp_filter : SWITCHABLE_FILTERS;
  25. if (left_type == above_type)
  26. return left_type;
  27. else if (left_type == SWITCHABLE_FILTERS)
  28. return above_type;
  29. else if (above_type == SWITCHABLE_FILTERS)
  30. return left_type;
  31. else
  32. return SWITCHABLE_FILTERS;
  33. }
  34. int vp9_get_reference_mode_context(const VP9_COMMON *cm,
  35. const MACROBLOCKD *xd) {
  36. int ctx;
  37. const MODE_INFO *const above_mi = xd->above_mi;
  38. const MODE_INFO *const left_mi = xd->left_mi;
  39. const int has_above = !!above_mi;
  40. const int has_left = !!left_mi;
  41. // Note:
  42. // The mode info data structure has a one element border above and to the
  43. // left of the entries corresponding to real macroblocks.
  44. // The prediction flags in these dummy entries are initialized to 0.
  45. if (has_above && has_left) { // both edges available
  46. if (!has_second_ref(above_mi) && !has_second_ref(left_mi))
  47. // neither edge uses comp pred (0/1)
  48. ctx = (above_mi->ref_frame[0] == cm->comp_fixed_ref) ^
  49. (left_mi->ref_frame[0] == cm->comp_fixed_ref);
  50. else if (!has_second_ref(above_mi))
  51. // one of two edges uses comp pred (2/3)
  52. ctx = 2 + (above_mi->ref_frame[0] == cm->comp_fixed_ref ||
  53. !is_inter_block(above_mi));
  54. else if (!has_second_ref(left_mi))
  55. // one of two edges uses comp pred (2/3)
  56. ctx = 2 + (left_mi->ref_frame[0] == cm->comp_fixed_ref ||
  57. !is_inter_block(left_mi));
  58. else // both edges use comp pred (4)
  59. ctx = 4;
  60. } else if (has_above || has_left) { // one edge available
  61. const MODE_INFO *edge_mi = has_above ? above_mi : left_mi;
  62. if (!has_second_ref(edge_mi))
  63. // edge does not use comp pred (0/1)
  64. ctx = edge_mi->ref_frame[0] == cm->comp_fixed_ref;
  65. else
  66. // edge uses comp pred (3)
  67. ctx = 3;
  68. } else { // no edges available (1)
  69. ctx = 1;
  70. }
  71. assert(ctx >= 0 && ctx < COMP_INTER_CONTEXTS);
  72. return ctx;
  73. }
  74. // Returns a context number for the given MB prediction signal
  75. int vp9_get_pred_context_comp_ref_p(const VP9_COMMON *cm,
  76. const MACROBLOCKD *xd) {
  77. int pred_context;
  78. const MODE_INFO *const above_mi = xd->above_mi;
  79. const MODE_INFO *const left_mi = xd->left_mi;
  80. const int above_in_image = !!above_mi;
  81. const int left_in_image = !!left_mi;
  82. // Note:
  83. // The mode info data structure has a one element border above and to the
  84. // left of the entries corresponding to real macroblocks.
  85. // The prediction flags in these dummy entries are initialized to 0.
  86. const int fix_ref_idx = cm->ref_frame_sign_bias[cm->comp_fixed_ref];
  87. const int var_ref_idx = !fix_ref_idx;
  88. if (above_in_image && left_in_image) { // both edges available
  89. const int above_intra = !is_inter_block(above_mi);
  90. const int left_intra = !is_inter_block(left_mi);
  91. if (above_intra && left_intra) { // intra/intra (2)
  92. pred_context = 2;
  93. } else if (above_intra || left_intra) { // intra/inter
  94. const MODE_INFO *edge_mi = above_intra ? left_mi : above_mi;
  95. if (!has_second_ref(edge_mi)) // single pred (1/3)
  96. pred_context = 1 + 2 * (edge_mi->ref_frame[0] != cm->comp_var_ref[1]);
  97. else // comp pred (1/3)
  98. pred_context = 1 + 2 * (edge_mi->ref_frame[var_ref_idx]
  99. != cm->comp_var_ref[1]);
  100. } else { // inter/inter
  101. const int l_sg = !has_second_ref(left_mi);
  102. const int a_sg = !has_second_ref(above_mi);
  103. const MV_REFERENCE_FRAME vrfa = a_sg ? above_mi->ref_frame[0]
  104. : above_mi->ref_frame[var_ref_idx];
  105. const MV_REFERENCE_FRAME vrfl = l_sg ? left_mi->ref_frame[0]
  106. : left_mi->ref_frame[var_ref_idx];
  107. if (vrfa == vrfl && cm->comp_var_ref[1] == vrfa) {
  108. pred_context = 0;
  109. } else if (l_sg && a_sg) { // single/single
  110. if ((vrfa == cm->comp_fixed_ref && vrfl == cm->comp_var_ref[0]) ||
  111. (vrfl == cm->comp_fixed_ref && vrfa == cm->comp_var_ref[0]))
  112. pred_context = 4;
  113. else if (vrfa == vrfl)
  114. pred_context = 3;
  115. else
  116. pred_context = 1;
  117. } else if (l_sg || a_sg) { // single/comp
  118. const MV_REFERENCE_FRAME vrfc = l_sg ? vrfa : vrfl;
  119. const MV_REFERENCE_FRAME rfs = a_sg ? vrfa : vrfl;
  120. if (vrfc == cm->comp_var_ref[1] && rfs != cm->comp_var_ref[1])
  121. pred_context = 1;
  122. else if (rfs == cm->comp_var_ref[1] && vrfc != cm->comp_var_ref[1])
  123. pred_context = 2;
  124. else
  125. pred_context = 4;
  126. } else if (vrfa == vrfl) { // comp/comp
  127. pred_context = 4;
  128. } else {
  129. pred_context = 2;
  130. }
  131. }
  132. } else if (above_in_image || left_in_image) { // one edge available
  133. const MODE_INFO *edge_mi = above_in_image ? above_mi : left_mi;
  134. if (!is_inter_block(edge_mi)) {
  135. pred_context = 2;
  136. } else {
  137. if (has_second_ref(edge_mi))
  138. pred_context = 4 * (edge_mi->ref_frame[var_ref_idx]
  139. != cm->comp_var_ref[1]);
  140. else
  141. pred_context = 3 * (edge_mi->ref_frame[0] != cm->comp_var_ref[1]);
  142. }
  143. } else { // no edges available (2)
  144. pred_context = 2;
  145. }
  146. assert(pred_context >= 0 && pred_context < REF_CONTEXTS);
  147. return pred_context;
  148. }
  149. int vp9_get_pred_context_single_ref_p1(const MACROBLOCKD *xd) {
  150. int pred_context;
  151. const MODE_INFO *const above_mi = xd->above_mi;
  152. const MODE_INFO *const left_mi = xd->left_mi;
  153. const int has_above = !!above_mi;
  154. const int has_left = !!left_mi;
  155. // Note:
  156. // The mode info data structure has a one element border above and to the
  157. // left of the entries corresponding to real macroblocks.
  158. // The prediction flags in these dummy entries are initialized to 0.
  159. if (has_above && has_left) { // both edges available
  160. const int above_intra = !is_inter_block(above_mi);
  161. const int left_intra = !is_inter_block(left_mi);
  162. if (above_intra && left_intra) { // intra/intra
  163. pred_context = 2;
  164. } else if (above_intra || left_intra) { // intra/inter or inter/intra
  165. const MODE_INFO *edge_mi = above_intra ? left_mi : above_mi;
  166. if (!has_second_ref(edge_mi))
  167. pred_context = 4 * (edge_mi->ref_frame[0] == LAST_FRAME);
  168. else
  169. pred_context = 1 + (edge_mi->ref_frame[0] == LAST_FRAME ||
  170. edge_mi->ref_frame[1] == LAST_FRAME);
  171. } else { // inter/inter
  172. const int above_has_second = has_second_ref(above_mi);
  173. const int left_has_second = has_second_ref(left_mi);
  174. const MV_REFERENCE_FRAME above0 = above_mi->ref_frame[0];
  175. const MV_REFERENCE_FRAME above1 = above_mi->ref_frame[1];
  176. const MV_REFERENCE_FRAME left0 = left_mi->ref_frame[0];
  177. const MV_REFERENCE_FRAME left1 = left_mi->ref_frame[1];
  178. if (above_has_second && left_has_second) {
  179. pred_context = 1 + (above0 == LAST_FRAME || above1 == LAST_FRAME ||
  180. left0 == LAST_FRAME || left1 == LAST_FRAME);
  181. } else if (above_has_second || left_has_second) {
  182. const MV_REFERENCE_FRAME rfs = !above_has_second ? above0 : left0;
  183. const MV_REFERENCE_FRAME crf1 = above_has_second ? above0 : left0;
  184. const MV_REFERENCE_FRAME crf2 = above_has_second ? above1 : left1;
  185. if (rfs == LAST_FRAME)
  186. pred_context = 3 + (crf1 == LAST_FRAME || crf2 == LAST_FRAME);
  187. else
  188. pred_context = (crf1 == LAST_FRAME || crf2 == LAST_FRAME);
  189. } else {
  190. pred_context = 2 * (above0 == LAST_FRAME) + 2 * (left0 == LAST_FRAME);
  191. }
  192. }
  193. } else if (has_above || has_left) { // one edge available
  194. const MODE_INFO *edge_mi = has_above ? above_mi : left_mi;
  195. if (!is_inter_block(edge_mi)) { // intra
  196. pred_context = 2;
  197. } else { // inter
  198. if (!has_second_ref(edge_mi))
  199. pred_context = 4 * (edge_mi->ref_frame[0] == LAST_FRAME);
  200. else
  201. pred_context = 1 + (edge_mi->ref_frame[0] == LAST_FRAME ||
  202. edge_mi->ref_frame[1] == LAST_FRAME);
  203. }
  204. } else { // no edges available
  205. pred_context = 2;
  206. }
  207. assert(pred_context >= 0 && pred_context < REF_CONTEXTS);
  208. return pred_context;
  209. }
  210. int vp9_get_pred_context_single_ref_p2(const MACROBLOCKD *xd) {
  211. int pred_context;
  212. const MODE_INFO *const above_mi = xd->above_mi;
  213. const MODE_INFO *const left_mi = xd->left_mi;
  214. const int has_above = !!above_mi;
  215. const int has_left = !!left_mi;
  216. // Note:
  217. // The mode info data structure has a one element border above and to the
  218. // left of the entries corresponding to real macroblocks.
  219. // The prediction flags in these dummy entries are initialized to 0.
  220. if (has_above && has_left) { // both edges available
  221. const int above_intra = !is_inter_block(above_mi);
  222. const int left_intra = !is_inter_block(left_mi);
  223. if (above_intra && left_intra) { // intra/intra
  224. pred_context = 2;
  225. } else if (above_intra || left_intra) { // intra/inter or inter/intra
  226. const MODE_INFO *edge_mi = above_intra ? left_mi : above_mi;
  227. if (!has_second_ref(edge_mi)) {
  228. if (edge_mi->ref_frame[0] == LAST_FRAME)
  229. pred_context = 3;
  230. else
  231. pred_context = 4 * (edge_mi->ref_frame[0] == GOLDEN_FRAME);
  232. } else {
  233. pred_context = 1 + 2 * (edge_mi->ref_frame[0] == GOLDEN_FRAME ||
  234. edge_mi->ref_frame[1] == GOLDEN_FRAME);
  235. }
  236. } else { // inter/inter
  237. const int above_has_second = has_second_ref(above_mi);
  238. const int left_has_second = has_second_ref(left_mi);
  239. const MV_REFERENCE_FRAME above0 = above_mi->ref_frame[0];
  240. const MV_REFERENCE_FRAME above1 = above_mi->ref_frame[1];
  241. const MV_REFERENCE_FRAME left0 = left_mi->ref_frame[0];
  242. const MV_REFERENCE_FRAME left1 = left_mi->ref_frame[1];
  243. if (above_has_second && left_has_second) {
  244. if (above0 == left0 && above1 == left1)
  245. pred_context = 3 * (above0 == GOLDEN_FRAME ||
  246. above1 == GOLDEN_FRAME ||
  247. left0 == GOLDEN_FRAME ||
  248. left1 == GOLDEN_FRAME);
  249. else
  250. pred_context = 2;
  251. } else if (above_has_second || left_has_second) {
  252. const MV_REFERENCE_FRAME rfs = !above_has_second ? above0 : left0;
  253. const MV_REFERENCE_FRAME crf1 = above_has_second ? above0 : left0;
  254. const MV_REFERENCE_FRAME crf2 = above_has_second ? above1 : left1;
  255. if (rfs == GOLDEN_FRAME)
  256. pred_context = 3 + (crf1 == GOLDEN_FRAME || crf2 == GOLDEN_FRAME);
  257. else if (rfs == ALTREF_FRAME)
  258. pred_context = crf1 == GOLDEN_FRAME || crf2 == GOLDEN_FRAME;
  259. else
  260. pred_context = 1 + 2 * (crf1 == GOLDEN_FRAME || crf2 == GOLDEN_FRAME);
  261. } else {
  262. if (above0 == LAST_FRAME && left0 == LAST_FRAME) {
  263. pred_context = 3;
  264. } else if (above0 == LAST_FRAME || left0 == LAST_FRAME) {
  265. const MV_REFERENCE_FRAME edge0 = (above0 == LAST_FRAME) ? left0
  266. : above0;
  267. pred_context = 4 * (edge0 == GOLDEN_FRAME);
  268. } else {
  269. pred_context = 2 * (above0 == GOLDEN_FRAME) +
  270. 2 * (left0 == GOLDEN_FRAME);
  271. }
  272. }
  273. }
  274. } else if (has_above || has_left) { // one edge available
  275. const MODE_INFO *edge_mi = has_above ? above_mi : left_mi;
  276. if (!is_inter_block(edge_mi) ||
  277. (edge_mi->ref_frame[0] == LAST_FRAME && !has_second_ref(edge_mi)))
  278. pred_context = 2;
  279. else if (!has_second_ref(edge_mi))
  280. pred_context = 4 * (edge_mi->ref_frame[0] == GOLDEN_FRAME);
  281. else
  282. pred_context = 3 * (edge_mi->ref_frame[0] == GOLDEN_FRAME ||
  283. edge_mi->ref_frame[1] == GOLDEN_FRAME);
  284. } else { // no edges available (2)
  285. pred_context = 2;
  286. }
  287. assert(pred_context >= 0 && pred_context < REF_CONTEXTS);
  288. return pred_context;
  289. }