vp9_decodemv.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  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. #include <assert.h>
  11. #include "vp9/common/vp9_common.h"
  12. #include "vp9/common/vp9_entropy.h"
  13. #include "vp9/common/vp9_entropymode.h"
  14. #include "vp9/common/vp9_entropymv.h"
  15. #include "vp9/common/vp9_mvref_common.h"
  16. #include "vp9/common/vp9_pred_common.h"
  17. #include "vp9/common/vp9_reconinter.h"
  18. #include "vp9/common/vp9_seg_common.h"
  19. #include "vp9/decoder/vp9_decodemv.h"
  20. #include "vp9/decoder/vp9_decodeframe.h"
  21. #include "vpx_dsp/vpx_dsp_common.h"
  22. static PREDICTION_MODE read_intra_mode(vpx_reader *r, const vpx_prob *p) {
  23. return (PREDICTION_MODE)vpx_read_tree(r, vp9_intra_mode_tree, p);
  24. }
  25. static PREDICTION_MODE read_intra_mode_y(VP9_COMMON *cm, MACROBLOCKD *xd,
  26. vpx_reader *r, int size_group) {
  27. const PREDICTION_MODE y_mode =
  28. read_intra_mode(r, cm->fc->y_mode_prob[size_group]);
  29. FRAME_COUNTS *counts = xd->counts;
  30. if (counts) ++counts->y_mode[size_group][y_mode];
  31. return y_mode;
  32. }
  33. static PREDICTION_MODE read_intra_mode_uv(VP9_COMMON *cm, MACROBLOCKD *xd,
  34. vpx_reader *r,
  35. PREDICTION_MODE y_mode) {
  36. const PREDICTION_MODE uv_mode =
  37. read_intra_mode(r, cm->fc->uv_mode_prob[y_mode]);
  38. FRAME_COUNTS *counts = xd->counts;
  39. if (counts) ++counts->uv_mode[y_mode][uv_mode];
  40. return uv_mode;
  41. }
  42. static PREDICTION_MODE read_inter_mode(VP9_COMMON *cm, MACROBLOCKD *xd,
  43. vpx_reader *r, int ctx) {
  44. const int mode =
  45. vpx_read_tree(r, vp9_inter_mode_tree, cm->fc->inter_mode_probs[ctx]);
  46. FRAME_COUNTS *counts = xd->counts;
  47. if (counts) ++counts->inter_mode[ctx][mode];
  48. return NEARESTMV + mode;
  49. }
  50. static int read_segment_id(vpx_reader *r, const struct segmentation *seg) {
  51. return vpx_read_tree(r, vp9_segment_tree, seg->tree_probs);
  52. }
  53. static TX_SIZE read_selected_tx_size(VP9_COMMON *cm, MACROBLOCKD *xd,
  54. TX_SIZE max_tx_size, vpx_reader *r) {
  55. FRAME_COUNTS *counts = xd->counts;
  56. const int ctx = get_tx_size_context(xd);
  57. const vpx_prob *tx_probs = get_tx_probs(max_tx_size, ctx, &cm->fc->tx_probs);
  58. int tx_size = vpx_read(r, tx_probs[0]);
  59. if (tx_size != TX_4X4 && max_tx_size >= TX_16X16) {
  60. tx_size += vpx_read(r, tx_probs[1]);
  61. if (tx_size != TX_8X8 && max_tx_size >= TX_32X32)
  62. tx_size += vpx_read(r, tx_probs[2]);
  63. }
  64. if (counts) ++get_tx_counts(max_tx_size, ctx, &counts->tx)[tx_size];
  65. return (TX_SIZE)tx_size;
  66. }
  67. static INLINE TX_SIZE read_tx_size(VP9_COMMON *cm, MACROBLOCKD *xd,
  68. int allow_select, vpx_reader *r) {
  69. TX_MODE tx_mode = cm->tx_mode;
  70. BLOCK_SIZE bsize = xd->mi[0]->sb_type;
  71. const TX_SIZE max_tx_size = max_txsize_lookup[bsize];
  72. if (allow_select && tx_mode == TX_MODE_SELECT && bsize >= BLOCK_8X8)
  73. return read_selected_tx_size(cm, xd, max_tx_size, r);
  74. else
  75. return VPXMIN(max_tx_size, tx_mode_to_biggest_tx_size[tx_mode]);
  76. }
  77. static int dec_get_segment_id(const VP9_COMMON *cm, const uint8_t *segment_ids,
  78. int mi_offset, int x_mis, int y_mis) {
  79. int x, y, segment_id = INT_MAX;
  80. for (y = 0; y < y_mis; y++)
  81. for (x = 0; x < x_mis; x++)
  82. segment_id =
  83. VPXMIN(segment_id, segment_ids[mi_offset + y * cm->mi_cols + x]);
  84. assert(segment_id >= 0 && segment_id < MAX_SEGMENTS);
  85. return segment_id;
  86. }
  87. static void set_segment_id(VP9_COMMON *cm, int mi_offset, int x_mis, int y_mis,
  88. int segment_id) {
  89. int x, y;
  90. assert(segment_id >= 0 && segment_id < MAX_SEGMENTS);
  91. for (y = 0; y < y_mis; y++)
  92. for (x = 0; x < x_mis; x++)
  93. cm->current_frame_seg_map[mi_offset + y * cm->mi_cols + x] = segment_id;
  94. }
  95. static void copy_segment_id(const VP9_COMMON *cm,
  96. const uint8_t *last_segment_ids,
  97. uint8_t *current_segment_ids, int mi_offset,
  98. int x_mis, int y_mis) {
  99. int x, y;
  100. for (y = 0; y < y_mis; y++)
  101. for (x = 0; x < x_mis; x++)
  102. current_segment_ids[mi_offset + y * cm->mi_cols + x] =
  103. last_segment_ids ? last_segment_ids[mi_offset + y * cm->mi_cols + x]
  104. : 0;
  105. }
  106. static int read_intra_segment_id(VP9_COMMON *const cm, int mi_offset, int x_mis,
  107. int y_mis, vpx_reader *r) {
  108. struct segmentation *const seg = &cm->seg;
  109. int segment_id;
  110. if (!seg->enabled) return 0; // Default for disabled segmentation
  111. if (!seg->update_map) {
  112. copy_segment_id(cm, cm->last_frame_seg_map, cm->current_frame_seg_map,
  113. mi_offset, x_mis, y_mis);
  114. return 0;
  115. }
  116. segment_id = read_segment_id(r, seg);
  117. set_segment_id(cm, mi_offset, x_mis, y_mis, segment_id);
  118. return segment_id;
  119. }
  120. static int read_inter_segment_id(VP9_COMMON *const cm, MACROBLOCKD *const xd,
  121. int mi_row, int mi_col, vpx_reader *r,
  122. int x_mis, int y_mis) {
  123. struct segmentation *const seg = &cm->seg;
  124. MODE_INFO *const mi = xd->mi[0];
  125. int predicted_segment_id, segment_id;
  126. const int mi_offset = mi_row * cm->mi_cols + mi_col;
  127. if (!seg->enabled) return 0; // Default for disabled segmentation
  128. predicted_segment_id = cm->last_frame_seg_map
  129. ? dec_get_segment_id(cm, cm->last_frame_seg_map,
  130. mi_offset, x_mis, y_mis)
  131. : 0;
  132. if (!seg->update_map) {
  133. copy_segment_id(cm, cm->last_frame_seg_map, cm->current_frame_seg_map,
  134. mi_offset, x_mis, y_mis);
  135. return predicted_segment_id;
  136. }
  137. if (seg->temporal_update) {
  138. const vpx_prob pred_prob = vp9_get_pred_prob_seg_id(seg, xd);
  139. mi->seg_id_predicted = vpx_read(r, pred_prob);
  140. segment_id =
  141. mi->seg_id_predicted ? predicted_segment_id : read_segment_id(r, seg);
  142. } else {
  143. segment_id = read_segment_id(r, seg);
  144. }
  145. set_segment_id(cm, mi_offset, x_mis, y_mis, segment_id);
  146. return segment_id;
  147. }
  148. static int read_skip(VP9_COMMON *cm, const MACROBLOCKD *xd, int segment_id,
  149. vpx_reader *r) {
  150. if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) {
  151. return 1;
  152. } else {
  153. const int ctx = vp9_get_skip_context(xd);
  154. const int skip = vpx_read(r, cm->fc->skip_probs[ctx]);
  155. FRAME_COUNTS *counts = xd->counts;
  156. if (counts) ++counts->skip[ctx][skip];
  157. return skip;
  158. }
  159. }
  160. static void read_intra_frame_mode_info(VP9_COMMON *const cm,
  161. MACROBLOCKD *const xd, int mi_row,
  162. int mi_col, vpx_reader *r, int x_mis,
  163. int y_mis) {
  164. MODE_INFO *const mi = xd->mi[0];
  165. const MODE_INFO *above_mi = xd->above_mi;
  166. const MODE_INFO *left_mi = xd->left_mi;
  167. const BLOCK_SIZE bsize = mi->sb_type;
  168. int i;
  169. const int mi_offset = mi_row * cm->mi_cols + mi_col;
  170. mi->segment_id = read_intra_segment_id(cm, mi_offset, x_mis, y_mis, r);
  171. mi->skip = read_skip(cm, xd, mi->segment_id, r);
  172. mi->tx_size = read_tx_size(cm, xd, 1, r);
  173. mi->ref_frame[0] = INTRA_FRAME;
  174. mi->ref_frame[1] = NONE;
  175. switch (bsize) {
  176. case BLOCK_4X4:
  177. for (i = 0; i < 4; ++i)
  178. mi->bmi[i].as_mode =
  179. read_intra_mode(r, get_y_mode_probs(mi, above_mi, left_mi, i));
  180. mi->mode = mi->bmi[3].as_mode;
  181. break;
  182. case BLOCK_4X8:
  183. mi->bmi[0].as_mode = mi->bmi[2].as_mode =
  184. read_intra_mode(r, get_y_mode_probs(mi, above_mi, left_mi, 0));
  185. mi->bmi[1].as_mode = mi->bmi[3].as_mode = mi->mode =
  186. read_intra_mode(r, get_y_mode_probs(mi, above_mi, left_mi, 1));
  187. break;
  188. case BLOCK_8X4:
  189. mi->bmi[0].as_mode = mi->bmi[1].as_mode =
  190. read_intra_mode(r, get_y_mode_probs(mi, above_mi, left_mi, 0));
  191. mi->bmi[2].as_mode = mi->bmi[3].as_mode = mi->mode =
  192. read_intra_mode(r, get_y_mode_probs(mi, above_mi, left_mi, 2));
  193. break;
  194. default:
  195. mi->mode = read_intra_mode(r, get_y_mode_probs(mi, above_mi, left_mi, 0));
  196. }
  197. mi->uv_mode = read_intra_mode(r, vp9_kf_uv_mode_prob[mi->mode]);
  198. }
  199. static int read_mv_component(vpx_reader *r, const nmv_component *mvcomp,
  200. int usehp) {
  201. int mag, d, fr, hp;
  202. const int sign = vpx_read(r, mvcomp->sign);
  203. const int mv_class = vpx_read_tree(r, vp9_mv_class_tree, mvcomp->classes);
  204. const int class0 = mv_class == MV_CLASS_0;
  205. // Integer part
  206. if (class0) {
  207. d = vpx_read(r, mvcomp->class0[0]);
  208. mag = 0;
  209. } else {
  210. int i;
  211. const int n = mv_class + CLASS0_BITS - 1; // number of bits
  212. d = 0;
  213. for (i = 0; i < n; ++i) d |= vpx_read(r, mvcomp->bits[i]) << i;
  214. mag = CLASS0_SIZE << (mv_class + 2);
  215. }
  216. // Fractional part
  217. fr = vpx_read_tree(r, vp9_mv_fp_tree,
  218. class0 ? mvcomp->class0_fp[d] : mvcomp->fp);
  219. // High precision part (if hp is not used, the default value of the hp is 1)
  220. hp = usehp ? vpx_read(r, class0 ? mvcomp->class0_hp : mvcomp->hp) : 1;
  221. // Result
  222. mag += ((d << 3) | (fr << 1) | hp) + 1;
  223. return sign ? -mag : mag;
  224. }
  225. static INLINE void read_mv(vpx_reader *r, MV *mv, const MV *ref,
  226. const nmv_context *ctx, nmv_context_counts *counts,
  227. int allow_hp) {
  228. const MV_JOINT_TYPE joint_type =
  229. (MV_JOINT_TYPE)vpx_read_tree(r, vp9_mv_joint_tree, ctx->joints);
  230. const int use_hp = allow_hp && use_mv_hp(ref);
  231. MV diff = { 0, 0 };
  232. if (mv_joint_vertical(joint_type))
  233. diff.row = read_mv_component(r, &ctx->comps[0], use_hp);
  234. if (mv_joint_horizontal(joint_type))
  235. diff.col = read_mv_component(r, &ctx->comps[1], use_hp);
  236. vp9_inc_mv(&diff, counts);
  237. mv->row = ref->row + diff.row;
  238. mv->col = ref->col + diff.col;
  239. }
  240. static REFERENCE_MODE read_block_reference_mode(VP9_COMMON *cm,
  241. const MACROBLOCKD *xd,
  242. vpx_reader *r) {
  243. if (cm->reference_mode == REFERENCE_MODE_SELECT) {
  244. const int ctx = vp9_get_reference_mode_context(cm, xd);
  245. const REFERENCE_MODE mode =
  246. (REFERENCE_MODE)vpx_read(r, cm->fc->comp_inter_prob[ctx]);
  247. FRAME_COUNTS *counts = xd->counts;
  248. if (counts) ++counts->comp_inter[ctx][mode];
  249. return mode; // SINGLE_REFERENCE or COMPOUND_REFERENCE
  250. } else {
  251. return cm->reference_mode;
  252. }
  253. }
  254. // Read the referncence frame
  255. static void read_ref_frames(VP9_COMMON *const cm, MACROBLOCKD *const xd,
  256. vpx_reader *r, int segment_id,
  257. MV_REFERENCE_FRAME ref_frame[2]) {
  258. FRAME_CONTEXT *const fc = cm->fc;
  259. FRAME_COUNTS *counts = xd->counts;
  260. if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) {
  261. ref_frame[0] = (MV_REFERENCE_FRAME)get_segdata(&cm->seg, segment_id,
  262. SEG_LVL_REF_FRAME);
  263. ref_frame[1] = NONE;
  264. } else {
  265. const REFERENCE_MODE mode = read_block_reference_mode(cm, xd, r);
  266. // FIXME(rbultje) I'm pretty sure this breaks segmentation ref frame coding
  267. if (mode == COMPOUND_REFERENCE) {
  268. const int idx = cm->ref_frame_sign_bias[cm->comp_fixed_ref];
  269. const int ctx = vp9_get_pred_context_comp_ref_p(cm, xd);
  270. const int bit = vpx_read(r, fc->comp_ref_prob[ctx]);
  271. if (counts) ++counts->comp_ref[ctx][bit];
  272. ref_frame[idx] = cm->comp_fixed_ref;
  273. ref_frame[!idx] = cm->comp_var_ref[bit];
  274. } else if (mode == SINGLE_REFERENCE) {
  275. const int ctx0 = vp9_get_pred_context_single_ref_p1(xd);
  276. const int bit0 = vpx_read(r, fc->single_ref_prob[ctx0][0]);
  277. if (counts) ++counts->single_ref[ctx0][0][bit0];
  278. if (bit0) {
  279. const int ctx1 = vp9_get_pred_context_single_ref_p2(xd);
  280. const int bit1 = vpx_read(r, fc->single_ref_prob[ctx1][1]);
  281. if (counts) ++counts->single_ref[ctx1][1][bit1];
  282. ref_frame[0] = bit1 ? ALTREF_FRAME : GOLDEN_FRAME;
  283. } else {
  284. ref_frame[0] = LAST_FRAME;
  285. }
  286. ref_frame[1] = NONE;
  287. } else {
  288. assert(0 && "Invalid prediction mode.");
  289. }
  290. }
  291. }
  292. static INLINE INTERP_FILTER read_switchable_interp_filter(VP9_COMMON *const cm,
  293. MACROBLOCKD *const xd,
  294. vpx_reader *r) {
  295. const int ctx = get_pred_context_switchable_interp(xd);
  296. const INTERP_FILTER type = (INTERP_FILTER)vpx_read_tree(
  297. r, vp9_switchable_interp_tree, cm->fc->switchable_interp_prob[ctx]);
  298. FRAME_COUNTS *counts = xd->counts;
  299. if (counts) ++counts->switchable_interp[ctx][type];
  300. return type;
  301. }
  302. static void read_intra_block_mode_info(VP9_COMMON *const cm,
  303. MACROBLOCKD *const xd, MODE_INFO *mi,
  304. vpx_reader *r) {
  305. const BLOCK_SIZE bsize = mi->sb_type;
  306. int i;
  307. switch (bsize) {
  308. case BLOCK_4X4:
  309. for (i = 0; i < 4; ++i)
  310. mi->bmi[i].as_mode = read_intra_mode_y(cm, xd, r, 0);
  311. mi->mode = mi->bmi[3].as_mode;
  312. break;
  313. case BLOCK_4X8:
  314. mi->bmi[0].as_mode = mi->bmi[2].as_mode = read_intra_mode_y(cm, xd, r, 0);
  315. mi->bmi[1].as_mode = mi->bmi[3].as_mode = mi->mode =
  316. read_intra_mode_y(cm, xd, r, 0);
  317. break;
  318. case BLOCK_8X4:
  319. mi->bmi[0].as_mode = mi->bmi[1].as_mode = read_intra_mode_y(cm, xd, r, 0);
  320. mi->bmi[2].as_mode = mi->bmi[3].as_mode = mi->mode =
  321. read_intra_mode_y(cm, xd, r, 0);
  322. break;
  323. default: mi->mode = read_intra_mode_y(cm, xd, r, size_group_lookup[bsize]);
  324. }
  325. mi->uv_mode = read_intra_mode_uv(cm, xd, r, mi->mode);
  326. // Initialize interp_filter here so we do not have to check for inter block
  327. // modes in get_pred_context_switchable_interp()
  328. mi->interp_filter = SWITCHABLE_FILTERS;
  329. mi->ref_frame[0] = INTRA_FRAME;
  330. mi->ref_frame[1] = NONE;
  331. }
  332. static INLINE int is_mv_valid(const MV *mv) {
  333. return mv->row > MV_LOW && mv->row < MV_UPP && mv->col > MV_LOW &&
  334. mv->col < MV_UPP;
  335. }
  336. static INLINE void copy_mv_pair(int_mv *dst, const int_mv *src) {
  337. memcpy(dst, src, sizeof(*dst) * 2);
  338. }
  339. static INLINE void zero_mv_pair(int_mv *dst) {
  340. memset(dst, 0, sizeof(*dst) * 2);
  341. }
  342. static INLINE int assign_mv(VP9_COMMON *cm, MACROBLOCKD *xd,
  343. PREDICTION_MODE mode, int_mv mv[2],
  344. int_mv ref_mv[2], int_mv near_nearest_mv[2],
  345. int is_compound, int allow_hp, vpx_reader *r) {
  346. int i;
  347. int ret = 1;
  348. switch (mode) {
  349. case NEWMV: {
  350. FRAME_COUNTS *counts = xd->counts;
  351. nmv_context_counts *const mv_counts = counts ? &counts->mv : NULL;
  352. for (i = 0; i < 1 + is_compound; ++i) {
  353. read_mv(r, &mv[i].as_mv, &ref_mv[i].as_mv, &cm->fc->nmvc, mv_counts,
  354. allow_hp);
  355. ret = ret && is_mv_valid(&mv[i].as_mv);
  356. }
  357. break;
  358. }
  359. case NEARMV:
  360. case NEARESTMV: {
  361. copy_mv_pair(mv, near_nearest_mv);
  362. break;
  363. }
  364. case ZEROMV: {
  365. zero_mv_pair(mv);
  366. break;
  367. }
  368. default: { return 0; }
  369. }
  370. return ret;
  371. }
  372. static int read_is_inter_block(VP9_COMMON *const cm, MACROBLOCKD *const xd,
  373. int segment_id, vpx_reader *r) {
  374. if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) {
  375. return get_segdata(&cm->seg, segment_id, SEG_LVL_REF_FRAME) != INTRA_FRAME;
  376. } else {
  377. const int ctx = get_intra_inter_context(xd);
  378. const int is_inter = vpx_read(r, cm->fc->intra_inter_prob[ctx]);
  379. FRAME_COUNTS *counts = xd->counts;
  380. if (counts) ++counts->intra_inter[ctx][is_inter];
  381. return is_inter;
  382. }
  383. }
  384. static void dec_find_best_ref_mvs(int allow_hp, int_mv *mvlist, int_mv *best_mv,
  385. int refmv_count) {
  386. int i;
  387. // Make sure all the candidates are properly clamped etc
  388. for (i = 0; i < refmv_count; ++i) {
  389. lower_mv_precision(&mvlist[i].as_mv, allow_hp);
  390. *best_mv = mvlist[i];
  391. }
  392. }
  393. // This macro is used to add a motion vector mv_ref list if it isn't
  394. // already in the list. If it's the second motion vector or early_break
  395. // it will also skip all additional processing and jump to Done!
  396. #define ADD_MV_REF_LIST_EB(mv, refmv_count, mv_ref_list, Done) \
  397. do { \
  398. if (refmv_count) { \
  399. if ((mv).as_int != (mv_ref_list)[0].as_int) { \
  400. (mv_ref_list)[(refmv_count)] = (mv); \
  401. refmv_count++; \
  402. goto Done; \
  403. } \
  404. } else { \
  405. (mv_ref_list)[(refmv_count)++] = (mv); \
  406. if (early_break) goto Done; \
  407. } \
  408. } while (0)
  409. // If either reference frame is different, not INTRA, and they
  410. // are different from each other scale and add the mv to our list.
  411. #define IF_DIFF_REF_FRAME_ADD_MV_EB(mbmi, ref_frame, ref_sign_bias, \
  412. refmv_count, mv_ref_list, Done) \
  413. do { \
  414. if (is_inter_block(mbmi)) { \
  415. if ((mbmi)->ref_frame[0] != ref_frame) \
  416. ADD_MV_REF_LIST_EB(scale_mv((mbmi), 0, ref_frame, ref_sign_bias), \
  417. refmv_count, mv_ref_list, Done); \
  418. if (has_second_ref(mbmi) && (mbmi)->ref_frame[1] != ref_frame && \
  419. (mbmi)->mv[1].as_int != (mbmi)->mv[0].as_int) \
  420. ADD_MV_REF_LIST_EB(scale_mv((mbmi), 1, ref_frame, ref_sign_bias), \
  421. refmv_count, mv_ref_list, Done); \
  422. } \
  423. } while (0)
  424. // This function searches the neighborhood of a given MB/SB
  425. // to try and find candidate reference vectors.
  426. static int dec_find_mv_refs(const VP9_COMMON *cm, const MACROBLOCKD *xd,
  427. PREDICTION_MODE mode, MV_REFERENCE_FRAME ref_frame,
  428. const POSITION *const mv_ref_search,
  429. int_mv *mv_ref_list, int mi_row, int mi_col,
  430. int block, int is_sub8x8) {
  431. const int *ref_sign_bias = cm->ref_frame_sign_bias;
  432. int i, refmv_count = 0;
  433. int different_ref_found = 0;
  434. const MV_REF *const prev_frame_mvs =
  435. cm->use_prev_frame_mvs
  436. ? cm->prev_frame->mvs + mi_row * cm->mi_cols + mi_col
  437. : NULL;
  438. const TileInfo *const tile = &xd->tile;
  439. // If mode is nearestmv or newmv (uses nearestmv as a reference) then stop
  440. // searching after the first mv is found.
  441. const int early_break = (mode != NEARMV);
  442. // Blank the reference vector list
  443. memset(mv_ref_list, 0, sizeof(*mv_ref_list) * MAX_MV_REF_CANDIDATES);
  444. i = 0;
  445. if (is_sub8x8) {
  446. // If the size < 8x8 we get the mv from the bmi substructure for the
  447. // nearest two blocks.
  448. for (i = 0; i < 2; ++i) {
  449. const POSITION *const mv_ref = &mv_ref_search[i];
  450. if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
  451. const MODE_INFO *const candidate_mi =
  452. xd->mi[mv_ref->col + mv_ref->row * xd->mi_stride];
  453. different_ref_found = 1;
  454. if (candidate_mi->ref_frame[0] == ref_frame)
  455. ADD_MV_REF_LIST_EB(
  456. get_sub_block_mv(candidate_mi, 0, mv_ref->col, block),
  457. refmv_count, mv_ref_list, Done);
  458. else if (candidate_mi->ref_frame[1] == ref_frame)
  459. ADD_MV_REF_LIST_EB(
  460. get_sub_block_mv(candidate_mi, 1, mv_ref->col, block),
  461. refmv_count, mv_ref_list, Done);
  462. }
  463. }
  464. }
  465. // Check the rest of the neighbors in much the same way
  466. // as before except we don't need to keep track of sub blocks or
  467. // mode counts.
  468. for (; i < MVREF_NEIGHBOURS; ++i) {
  469. const POSITION *const mv_ref = &mv_ref_search[i];
  470. if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
  471. const MODE_INFO *const candidate =
  472. xd->mi[mv_ref->col + mv_ref->row * xd->mi_stride];
  473. different_ref_found = 1;
  474. if (candidate->ref_frame[0] == ref_frame)
  475. ADD_MV_REF_LIST_EB(candidate->mv[0], refmv_count, mv_ref_list, Done);
  476. else if (candidate->ref_frame[1] == ref_frame)
  477. ADD_MV_REF_LIST_EB(candidate->mv[1], refmv_count, mv_ref_list, Done);
  478. }
  479. }
  480. // Check the last frame's mode and mv info.
  481. if (prev_frame_mvs) {
  482. if (prev_frame_mvs->ref_frame[0] == ref_frame) {
  483. ADD_MV_REF_LIST_EB(prev_frame_mvs->mv[0], refmv_count, mv_ref_list, Done);
  484. } else if (prev_frame_mvs->ref_frame[1] == ref_frame) {
  485. ADD_MV_REF_LIST_EB(prev_frame_mvs->mv[1], refmv_count, mv_ref_list, Done);
  486. }
  487. }
  488. // Since we couldn't find 2 mvs from the same reference frame
  489. // go back through the neighbors and find motion vectors from
  490. // different reference frames.
  491. if (different_ref_found) {
  492. for (i = 0; i < MVREF_NEIGHBOURS; ++i) {
  493. const POSITION *mv_ref = &mv_ref_search[i];
  494. if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
  495. const MODE_INFO *const candidate =
  496. xd->mi[mv_ref->col + mv_ref->row * xd->mi_stride];
  497. // If the candidate is INTRA we don't want to consider its mv.
  498. IF_DIFF_REF_FRAME_ADD_MV_EB(candidate, ref_frame, ref_sign_bias,
  499. refmv_count, mv_ref_list, Done);
  500. }
  501. }
  502. }
  503. // Since we still don't have a candidate we'll try the last frame.
  504. if (prev_frame_mvs) {
  505. if (prev_frame_mvs->ref_frame[0] != ref_frame &&
  506. prev_frame_mvs->ref_frame[0] > INTRA_FRAME) {
  507. int_mv mv = prev_frame_mvs->mv[0];
  508. if (ref_sign_bias[prev_frame_mvs->ref_frame[0]] !=
  509. ref_sign_bias[ref_frame]) {
  510. mv.as_mv.row *= -1;
  511. mv.as_mv.col *= -1;
  512. }
  513. ADD_MV_REF_LIST_EB(mv, refmv_count, mv_ref_list, Done);
  514. }
  515. if (prev_frame_mvs->ref_frame[1] > INTRA_FRAME &&
  516. prev_frame_mvs->ref_frame[1] != ref_frame &&
  517. prev_frame_mvs->mv[1].as_int != prev_frame_mvs->mv[0].as_int) {
  518. int_mv mv = prev_frame_mvs->mv[1];
  519. if (ref_sign_bias[prev_frame_mvs->ref_frame[1]] !=
  520. ref_sign_bias[ref_frame]) {
  521. mv.as_mv.row *= -1;
  522. mv.as_mv.col *= -1;
  523. }
  524. ADD_MV_REF_LIST_EB(mv, refmv_count, mv_ref_list, Done);
  525. }
  526. }
  527. if (mode == NEARMV)
  528. refmv_count = MAX_MV_REF_CANDIDATES;
  529. else
  530. // we only care about the nearestmv for the remaining modes
  531. refmv_count = 1;
  532. Done:
  533. // Clamp vectors
  534. for (i = 0; i < refmv_count; ++i) clamp_mv_ref(&mv_ref_list[i].as_mv, xd);
  535. return refmv_count;
  536. }
  537. static void append_sub8x8_mvs_for_idx(VP9_COMMON *cm, MACROBLOCKD *xd,
  538. const POSITION *const mv_ref_search,
  539. PREDICTION_MODE b_mode, int block,
  540. int ref, int mi_row, int mi_col,
  541. int_mv *best_sub8x8) {
  542. int_mv mv_list[MAX_MV_REF_CANDIDATES];
  543. MODE_INFO *const mi = xd->mi[0];
  544. b_mode_info *bmi = mi->bmi;
  545. int n;
  546. int refmv_count;
  547. assert(MAX_MV_REF_CANDIDATES == 2);
  548. refmv_count =
  549. dec_find_mv_refs(cm, xd, b_mode, mi->ref_frame[ref], mv_ref_search,
  550. mv_list, mi_row, mi_col, block, 1);
  551. switch (block) {
  552. case 0: best_sub8x8->as_int = mv_list[refmv_count - 1].as_int; break;
  553. case 1:
  554. case 2:
  555. if (b_mode == NEARESTMV) {
  556. best_sub8x8->as_int = bmi[0].as_mv[ref].as_int;
  557. } else {
  558. best_sub8x8->as_int = 0;
  559. for (n = 0; n < refmv_count; ++n)
  560. if (bmi[0].as_mv[ref].as_int != mv_list[n].as_int) {
  561. best_sub8x8->as_int = mv_list[n].as_int;
  562. break;
  563. }
  564. }
  565. break;
  566. case 3:
  567. if (b_mode == NEARESTMV) {
  568. best_sub8x8->as_int = bmi[2].as_mv[ref].as_int;
  569. } else {
  570. int_mv candidates[2 + MAX_MV_REF_CANDIDATES];
  571. candidates[0] = bmi[1].as_mv[ref];
  572. candidates[1] = bmi[0].as_mv[ref];
  573. candidates[2] = mv_list[0];
  574. candidates[3] = mv_list[1];
  575. best_sub8x8->as_int = 0;
  576. for (n = 0; n < 2 + MAX_MV_REF_CANDIDATES; ++n)
  577. if (bmi[2].as_mv[ref].as_int != candidates[n].as_int) {
  578. best_sub8x8->as_int = candidates[n].as_int;
  579. break;
  580. }
  581. }
  582. break;
  583. default: assert(0 && "Invalid block index.");
  584. }
  585. }
  586. static uint8_t get_mode_context(const VP9_COMMON *cm, const MACROBLOCKD *xd,
  587. const POSITION *const mv_ref_search, int mi_row,
  588. int mi_col) {
  589. int i;
  590. int context_counter = 0;
  591. const TileInfo *const tile = &xd->tile;
  592. // Get mode count from nearest 2 blocks
  593. for (i = 0; i < 2; ++i) {
  594. const POSITION *const mv_ref = &mv_ref_search[i];
  595. if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
  596. const MODE_INFO *const candidate =
  597. xd->mi[mv_ref->col + mv_ref->row * xd->mi_stride];
  598. // Keep counts for entropy encoding.
  599. context_counter += mode_2_counter[candidate->mode];
  600. }
  601. }
  602. return counter_to_context[context_counter];
  603. }
  604. static void read_inter_block_mode_info(VP9Decoder *const pbi,
  605. MACROBLOCKD *const xd,
  606. MODE_INFO *const mi, int mi_row,
  607. int mi_col, vpx_reader *r) {
  608. VP9_COMMON *const cm = &pbi->common;
  609. const BLOCK_SIZE bsize = mi->sb_type;
  610. const int allow_hp = cm->allow_high_precision_mv;
  611. int_mv best_ref_mvs[2] = { { 0 }, { 0 } };
  612. int ref, is_compound;
  613. uint8_t inter_mode_ctx;
  614. const POSITION *const mv_ref_search = mv_ref_blocks[bsize];
  615. read_ref_frames(cm, xd, r, mi->segment_id, mi->ref_frame);
  616. is_compound = has_second_ref(mi);
  617. inter_mode_ctx = get_mode_context(cm, xd, mv_ref_search, mi_row, mi_col);
  618. if (segfeature_active(&cm->seg, mi->segment_id, SEG_LVL_SKIP)) {
  619. mi->mode = ZEROMV;
  620. if (bsize < BLOCK_8X8) {
  621. vpx_internal_error(xd->error_info, VPX_CODEC_UNSUP_BITSTREAM,
  622. "Invalid usage of segement feature on small blocks");
  623. return;
  624. }
  625. } else {
  626. if (bsize >= BLOCK_8X8)
  627. mi->mode = read_inter_mode(cm, xd, r, inter_mode_ctx);
  628. else
  629. // Sub 8x8 blocks use the nearestmv as a ref_mv if the b_mode is NEWMV.
  630. // Setting mode to NEARESTMV forces the search to stop after the nearestmv
  631. // has been found. After b_modes have been read, mode will be overwritten
  632. // by the last b_mode.
  633. mi->mode = NEARESTMV;
  634. if (mi->mode != ZEROMV) {
  635. for (ref = 0; ref < 1 + is_compound; ++ref) {
  636. int_mv tmp_mvs[MAX_MV_REF_CANDIDATES];
  637. const MV_REFERENCE_FRAME frame = mi->ref_frame[ref];
  638. int refmv_count;
  639. refmv_count = dec_find_mv_refs(cm, xd, mi->mode, frame, mv_ref_search,
  640. tmp_mvs, mi_row, mi_col, -1, 0);
  641. dec_find_best_ref_mvs(allow_hp, tmp_mvs, &best_ref_mvs[ref],
  642. refmv_count);
  643. }
  644. }
  645. }
  646. mi->interp_filter = (cm->interp_filter == SWITCHABLE)
  647. ? read_switchable_interp_filter(cm, xd, r)
  648. : cm->interp_filter;
  649. if (bsize < BLOCK_8X8) {
  650. const int num_4x4_w = 1 << xd->bmode_blocks_wl;
  651. const int num_4x4_h = 1 << xd->bmode_blocks_hl;
  652. int idx, idy;
  653. PREDICTION_MODE b_mode;
  654. int_mv best_sub8x8[2];
  655. const uint32_t invalid_mv = 0x80008000;
  656. // Initialize the 2nd element as even though it won't be used meaningfully
  657. // if is_compound is false, copying/clamping it may trigger a MSan warning.
  658. best_sub8x8[1].as_int = invalid_mv;
  659. for (idy = 0; idy < 2; idy += num_4x4_h) {
  660. for (idx = 0; idx < 2; idx += num_4x4_w) {
  661. const int j = idy * 2 + idx;
  662. b_mode = read_inter_mode(cm, xd, r, inter_mode_ctx);
  663. if (b_mode == NEARESTMV || b_mode == NEARMV) {
  664. for (ref = 0; ref < 1 + is_compound; ++ref)
  665. append_sub8x8_mvs_for_idx(cm, xd, mv_ref_search, b_mode, j, ref,
  666. mi_row, mi_col, &best_sub8x8[ref]);
  667. }
  668. if (!assign_mv(cm, xd, b_mode, mi->bmi[j].as_mv, best_ref_mvs,
  669. best_sub8x8, is_compound, allow_hp, r)) {
  670. xd->corrupted |= 1;
  671. break;
  672. }
  673. if (num_4x4_h == 2) mi->bmi[j + 2] = mi->bmi[j];
  674. if (num_4x4_w == 2) mi->bmi[j + 1] = mi->bmi[j];
  675. }
  676. }
  677. mi->mode = b_mode;
  678. copy_mv_pair(mi->mv, mi->bmi[3].as_mv);
  679. } else {
  680. xd->corrupted |= !assign_mv(cm, xd, mi->mode, mi->mv, best_ref_mvs,
  681. best_ref_mvs, is_compound, allow_hp, r);
  682. }
  683. }
  684. static void read_inter_frame_mode_info(VP9Decoder *const pbi,
  685. MACROBLOCKD *const xd, int mi_row,
  686. int mi_col, vpx_reader *r, int x_mis,
  687. int y_mis) {
  688. VP9_COMMON *const cm = &pbi->common;
  689. MODE_INFO *const mi = xd->mi[0];
  690. int inter_block;
  691. mi->segment_id =
  692. read_inter_segment_id(cm, xd, mi_row, mi_col, r, x_mis, y_mis);
  693. mi->skip = read_skip(cm, xd, mi->segment_id, r);
  694. inter_block = read_is_inter_block(cm, xd, mi->segment_id, r);
  695. mi->tx_size = read_tx_size(cm, xd, !mi->skip || !inter_block, r);
  696. if (inter_block)
  697. read_inter_block_mode_info(pbi, xd, mi, mi_row, mi_col, r);
  698. else
  699. read_intra_block_mode_info(cm, xd, mi, r);
  700. }
  701. static INLINE void copy_ref_frame_pair(MV_REFERENCE_FRAME *dst,
  702. const MV_REFERENCE_FRAME *src) {
  703. memcpy(dst, src, sizeof(*dst) * 2);
  704. }
  705. void vp9_read_mode_info(TileWorkerData *twd, VP9Decoder *const pbi, int mi_row,
  706. int mi_col, int x_mis, int y_mis) {
  707. vpx_reader *r = &twd->bit_reader;
  708. MACROBLOCKD *const xd = &twd->xd;
  709. VP9_COMMON *const cm = &pbi->common;
  710. MODE_INFO *const mi = xd->mi[0];
  711. MV_REF *frame_mvs = cm->cur_frame->mvs + mi_row * cm->mi_cols + mi_col;
  712. int w, h;
  713. if (frame_is_intra_only(cm)) {
  714. read_intra_frame_mode_info(cm, xd, mi_row, mi_col, r, x_mis, y_mis);
  715. } else {
  716. read_inter_frame_mode_info(pbi, xd, mi_row, mi_col, r, x_mis, y_mis);
  717. for (h = 0; h < y_mis; ++h) {
  718. for (w = 0; w < x_mis; ++w) {
  719. MV_REF *const mv = frame_mvs + w;
  720. copy_ref_frame_pair(mv->ref_frame, mi->ref_frame);
  721. copy_mv_pair(mv->mv, mi->mv);
  722. }
  723. frame_mvs += cm->mi_cols;
  724. }
  725. }
  726. #if 0 // CONFIG_BETTER_HW_COMPATIBILITY && CONFIG_VP9_HIGHBITDEPTH
  727. if ((xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) &&
  728. (xd->above_mi == NULL || xd->left_mi == NULL) &&
  729. !is_inter_block(mi) && need_top_left[mi->uv_mode])
  730. assert(0);
  731. #endif // CONFIG_BETTER_HW_COMPATIBILITY && CONFIG_VP9_HIGHBITDEPTH
  732. }