vp9_decodemv.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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 "vp9/decoder/vp9_reader.h"
  22. static PREDICTION_MODE read_intra_mode(vp9_reader *r, const vp9_prob *p) {
  23. return (PREDICTION_MODE)vp9_read_tree(r, vp9_intra_mode_tree, p);
  24. }
  25. static PREDICTION_MODE read_intra_mode_y(VP9_COMMON *cm, MACROBLOCKD *xd,
  26. vp9_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)
  31. ++counts->y_mode[size_group][y_mode];
  32. return y_mode;
  33. }
  34. static PREDICTION_MODE read_intra_mode_uv(VP9_COMMON *cm, MACROBLOCKD *xd,
  35. vp9_reader *r,
  36. PREDICTION_MODE y_mode) {
  37. const PREDICTION_MODE uv_mode = read_intra_mode(r,
  38. cm->fc->uv_mode_prob[y_mode]);
  39. FRAME_COUNTS *counts = xd->counts;
  40. if (counts)
  41. ++counts->uv_mode[y_mode][uv_mode];
  42. return uv_mode;
  43. }
  44. static PREDICTION_MODE read_inter_mode(VP9_COMMON *cm, MACROBLOCKD *xd,
  45. vp9_reader *r, int ctx) {
  46. const int mode = vp9_read_tree(r, vp9_inter_mode_tree,
  47. cm->fc->inter_mode_probs[ctx]);
  48. FRAME_COUNTS *counts = xd->counts;
  49. if (counts)
  50. ++counts->inter_mode[ctx][mode];
  51. return NEARESTMV + mode;
  52. }
  53. static int read_segment_id(vp9_reader *r, const struct segmentation *seg) {
  54. return vp9_read_tree(r, vp9_segment_tree, seg->tree_probs);
  55. }
  56. static TX_SIZE read_selected_tx_size(VP9_COMMON *cm, MACROBLOCKD *xd,
  57. TX_SIZE max_tx_size, vp9_reader *r) {
  58. FRAME_COUNTS *counts = xd->counts;
  59. const int ctx = vp9_get_tx_size_context(xd);
  60. const vp9_prob *tx_probs = get_tx_probs(max_tx_size, ctx, &cm->fc->tx_probs);
  61. int tx_size = vp9_read(r, tx_probs[0]);
  62. if (tx_size != TX_4X4 && max_tx_size >= TX_16X16) {
  63. tx_size += vp9_read(r, tx_probs[1]);
  64. if (tx_size != TX_8X8 && max_tx_size >= TX_32X32)
  65. tx_size += vp9_read(r, tx_probs[2]);
  66. }
  67. if (counts)
  68. ++get_tx_counts(max_tx_size, ctx, &counts->tx)[tx_size];
  69. return (TX_SIZE)tx_size;
  70. }
  71. static TX_SIZE read_tx_size(VP9_COMMON *cm, MACROBLOCKD *xd,
  72. int allow_select, vp9_reader *r) {
  73. TX_MODE tx_mode = cm->tx_mode;
  74. BLOCK_SIZE bsize = xd->mi[0]->mbmi.sb_type;
  75. const TX_SIZE max_tx_size = max_txsize_lookup[bsize];
  76. if (allow_select && tx_mode == TX_MODE_SELECT && bsize >= BLOCK_8X8)
  77. return read_selected_tx_size(cm, xd, max_tx_size, r);
  78. else
  79. return MIN(max_tx_size, tx_mode_to_biggest_tx_size[tx_mode]);
  80. }
  81. static void set_segment_id(VP9_COMMON *cm, BLOCK_SIZE bsize,
  82. int mi_row, int mi_col, int segment_id) {
  83. const int mi_offset = mi_row * cm->mi_cols + mi_col;
  84. const int bw = num_8x8_blocks_wide_lookup[bsize];
  85. const int bh = num_8x8_blocks_high_lookup[bsize];
  86. const int xmis = MIN(cm->mi_cols - mi_col, bw);
  87. const int ymis = MIN(cm->mi_rows - mi_row, bh);
  88. int x, y;
  89. assert(segment_id >= 0 && segment_id < MAX_SEGMENTS);
  90. for (y = 0; y < ymis; y++)
  91. for (x = 0; x < xmis; x++)
  92. cm->current_frame_seg_map[mi_offset + y * cm->mi_cols + x] = segment_id;
  93. }
  94. static void copy_segment_id(const VP9_COMMON *cm,
  95. const uint8_t *last_segment_ids,
  96. uint8_t *current_segment_ids,
  97. BLOCK_SIZE bsize, int mi_row, int mi_col) {
  98. const int mi_offset = mi_row * cm->mi_cols + mi_col;
  99. const int bw = num_8x8_blocks_wide_lookup[bsize];
  100. const int bh = num_8x8_blocks_high_lookup[bsize];
  101. const int xmis = MIN(cm->mi_cols - mi_col, bw);
  102. const int ymis = MIN(cm->mi_rows - mi_row, bh);
  103. int x, y;
  104. for (y = 0; y < ymis; y++)
  105. for (x = 0; x < xmis; x++)
  106. current_segment_ids[mi_offset + y * cm->mi_cols + x] = last_segment_ids ?
  107. last_segment_ids[mi_offset + y * cm->mi_cols + x] : 0;
  108. }
  109. static int read_intra_segment_id(VP9_COMMON *const cm, BLOCK_SIZE bsize,
  110. int mi_row, int mi_col,
  111. vp9_reader *r) {
  112. struct segmentation *const seg = &cm->seg;
  113. int segment_id;
  114. if (!seg->enabled)
  115. return 0; // Default for disabled segmentation
  116. if (!seg->update_map) {
  117. copy_segment_id(cm, cm->last_frame_seg_map, cm->current_frame_seg_map,
  118. bsize, mi_row, mi_col);
  119. return 0;
  120. }
  121. segment_id = read_segment_id(r, seg);
  122. set_segment_id(cm, bsize, mi_row, mi_col, segment_id);
  123. return segment_id;
  124. }
  125. static int read_inter_segment_id(VP9_COMMON *const cm, MACROBLOCKD *const xd,
  126. int mi_row, int mi_col, vp9_reader *r) {
  127. struct segmentation *const seg = &cm->seg;
  128. MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
  129. const BLOCK_SIZE bsize = mbmi->sb_type;
  130. int predicted_segment_id, segment_id;
  131. if (!seg->enabled)
  132. return 0; // Default for disabled segmentation
  133. predicted_segment_id = cm->last_frame_seg_map ?
  134. vp9_get_segment_id(cm, cm->last_frame_seg_map, bsize, mi_row, mi_col) : 0;
  135. if (!seg->update_map) {
  136. copy_segment_id(cm, cm->last_frame_seg_map, cm->current_frame_seg_map,
  137. bsize, mi_row, mi_col);
  138. return predicted_segment_id;
  139. }
  140. if (seg->temporal_update) {
  141. const vp9_prob pred_prob = vp9_get_pred_prob_seg_id(seg, xd);
  142. mbmi->seg_id_predicted = vp9_read(r, pred_prob);
  143. segment_id = mbmi->seg_id_predicted ? predicted_segment_id
  144. : read_segment_id(r, seg);
  145. } else {
  146. segment_id = read_segment_id(r, seg);
  147. }
  148. set_segment_id(cm, bsize, mi_row, mi_col, segment_id);
  149. return segment_id;
  150. }
  151. static int read_skip(VP9_COMMON *cm, const MACROBLOCKD *xd,
  152. int segment_id, vp9_reader *r) {
  153. if (vp9_segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) {
  154. return 1;
  155. } else {
  156. const int ctx = vp9_get_skip_context(xd);
  157. const int skip = vp9_read(r, cm->fc->skip_probs[ctx]);
  158. FRAME_COUNTS *counts = xd->counts;
  159. if (counts)
  160. ++counts->skip[ctx][skip];
  161. return skip;
  162. }
  163. }
  164. static void read_intra_frame_mode_info(VP9_COMMON *const cm,
  165. MACROBLOCKD *const xd,
  166. int mi_row, int mi_col, vp9_reader *r) {
  167. MODE_INFO *const mi = xd->mi[0];
  168. MB_MODE_INFO *const mbmi = &mi->mbmi;
  169. const MODE_INFO *above_mi = xd->above_mi;
  170. const MODE_INFO *left_mi = xd->left_mi;
  171. const BLOCK_SIZE bsize = mbmi->sb_type;
  172. int i;
  173. mbmi->segment_id = read_intra_segment_id(cm, bsize, mi_row, mi_col, r);
  174. mbmi->skip = read_skip(cm, xd, mbmi->segment_id, r);
  175. mbmi->tx_size = read_tx_size(cm, xd, 1, r);
  176. mbmi->ref_frame[0] = INTRA_FRAME;
  177. mbmi->ref_frame[1] = NONE;
  178. switch (bsize) {
  179. case BLOCK_4X4:
  180. for (i = 0; i < 4; ++i)
  181. mi->bmi[i].as_mode =
  182. read_intra_mode(r, get_y_mode_probs(mi, above_mi, left_mi, i));
  183. mbmi->mode = mi->bmi[3].as_mode;
  184. break;
  185. case BLOCK_4X8:
  186. mi->bmi[0].as_mode = mi->bmi[2].as_mode =
  187. read_intra_mode(r, get_y_mode_probs(mi, above_mi, left_mi, 0));
  188. mi->bmi[1].as_mode = mi->bmi[3].as_mode = mbmi->mode =
  189. read_intra_mode(r, get_y_mode_probs(mi, above_mi, left_mi, 1));
  190. break;
  191. case BLOCK_8X4:
  192. mi->bmi[0].as_mode = mi->bmi[1].as_mode =
  193. read_intra_mode(r, get_y_mode_probs(mi, above_mi, left_mi, 0));
  194. mi->bmi[2].as_mode = mi->bmi[3].as_mode = mbmi->mode =
  195. read_intra_mode(r, get_y_mode_probs(mi, above_mi, left_mi, 2));
  196. break;
  197. default:
  198. mbmi->mode = read_intra_mode(r,
  199. get_y_mode_probs(mi, above_mi, left_mi, 0));
  200. }
  201. mbmi->uv_mode = read_intra_mode(r, vp9_kf_uv_mode_prob[mbmi->mode]);
  202. }
  203. static int read_mv_component(vp9_reader *r,
  204. const nmv_component *mvcomp, int usehp) {
  205. int mag, d, fr, hp;
  206. const int sign = vp9_read(r, mvcomp->sign);
  207. const int mv_class = vp9_read_tree(r, vp9_mv_class_tree, mvcomp->classes);
  208. const int class0 = mv_class == MV_CLASS_0;
  209. // Integer part
  210. if (class0) {
  211. d = vp9_read_tree(r, vp9_mv_class0_tree, mvcomp->class0);
  212. } else {
  213. int i;
  214. const int n = mv_class + CLASS0_BITS - 1; // number of bits
  215. d = 0;
  216. for (i = 0; i < n; ++i)
  217. d |= vp9_read(r, mvcomp->bits[i]) << i;
  218. }
  219. // Fractional part
  220. fr = vp9_read_tree(r, vp9_mv_fp_tree, class0 ? mvcomp->class0_fp[d]
  221. : mvcomp->fp);
  222. // High precision part (if hp is not used, the default value of the hp is 1)
  223. hp = usehp ? vp9_read(r, class0 ? mvcomp->class0_hp : mvcomp->hp)
  224. : 1;
  225. // Result
  226. mag = vp9_get_mv_mag(mv_class, (d << 3) | (fr << 1) | hp) + 1;
  227. return sign ? -mag : mag;
  228. }
  229. static INLINE void read_mv(vp9_reader *r, MV *mv, const MV *ref,
  230. const nmv_context *ctx,
  231. nmv_context_counts *counts, int allow_hp) {
  232. const MV_JOINT_TYPE joint_type =
  233. (MV_JOINT_TYPE)vp9_read_tree(r, vp9_mv_joint_tree, ctx->joints);
  234. const int use_hp = allow_hp && vp9_use_mv_hp(ref);
  235. MV diff = {0, 0};
  236. if (mv_joint_vertical(joint_type))
  237. diff.row = read_mv_component(r, &ctx->comps[0], use_hp);
  238. if (mv_joint_horizontal(joint_type))
  239. diff.col = read_mv_component(r, &ctx->comps[1], use_hp);
  240. vp9_inc_mv(&diff, counts);
  241. mv->row = ref->row + diff.row;
  242. mv->col = ref->col + diff.col;
  243. }
  244. static REFERENCE_MODE read_block_reference_mode(VP9_COMMON *cm,
  245. const MACROBLOCKD *xd,
  246. vp9_reader *r) {
  247. if (cm->reference_mode == REFERENCE_MODE_SELECT) {
  248. const int ctx = vp9_get_reference_mode_context(cm, xd);
  249. const REFERENCE_MODE mode =
  250. (REFERENCE_MODE)vp9_read(r, cm->fc->comp_inter_prob[ctx]);
  251. FRAME_COUNTS *counts = xd->counts;
  252. if (counts)
  253. ++counts->comp_inter[ctx][mode];
  254. return mode; // SINGLE_REFERENCE or COMPOUND_REFERENCE
  255. } else {
  256. return cm->reference_mode;
  257. }
  258. }
  259. // Read the referncence frame
  260. static void read_ref_frames(VP9_COMMON *const cm, MACROBLOCKD *const xd,
  261. vp9_reader *r,
  262. int segment_id, MV_REFERENCE_FRAME ref_frame[2]) {
  263. FRAME_CONTEXT *const fc = cm->fc;
  264. FRAME_COUNTS *counts = xd->counts;
  265. if (vp9_segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) {
  266. ref_frame[0] = (MV_REFERENCE_FRAME)vp9_get_segdata(&cm->seg, segment_id,
  267. SEG_LVL_REF_FRAME);
  268. ref_frame[1] = NONE;
  269. } else {
  270. const REFERENCE_MODE mode = read_block_reference_mode(cm, xd, r);
  271. // FIXME(rbultje) I'm pretty sure this breaks segmentation ref frame coding
  272. if (mode == COMPOUND_REFERENCE) {
  273. const int idx = cm->ref_frame_sign_bias[cm->comp_fixed_ref];
  274. const int ctx = vp9_get_pred_context_comp_ref_p(cm, xd);
  275. const int bit = vp9_read(r, fc->comp_ref_prob[ctx]);
  276. if (counts)
  277. ++counts->comp_ref[ctx][bit];
  278. ref_frame[idx] = cm->comp_fixed_ref;
  279. ref_frame[!idx] = cm->comp_var_ref[bit];
  280. } else if (mode == SINGLE_REFERENCE) {
  281. const int ctx0 = vp9_get_pred_context_single_ref_p1(xd);
  282. const int bit0 = vp9_read(r, fc->single_ref_prob[ctx0][0]);
  283. if (counts)
  284. ++counts->single_ref[ctx0][0][bit0];
  285. if (bit0) {
  286. const int ctx1 = vp9_get_pred_context_single_ref_p2(xd);
  287. const int bit1 = vp9_read(r, fc->single_ref_prob[ctx1][1]);
  288. if (counts)
  289. ++counts->single_ref[ctx1][1][bit1];
  290. ref_frame[0] = bit1 ? ALTREF_FRAME : GOLDEN_FRAME;
  291. } else {
  292. ref_frame[0] = LAST_FRAME;
  293. }
  294. ref_frame[1] = NONE;
  295. } else {
  296. assert(0 && "Invalid prediction mode.");
  297. }
  298. }
  299. }
  300. static INLINE INTERP_FILTER read_switchable_interp_filter(
  301. VP9_COMMON *const cm, MACROBLOCKD *const xd,
  302. vp9_reader *r) {
  303. const int ctx = vp9_get_pred_context_switchable_interp(xd);
  304. const INTERP_FILTER type =
  305. (INTERP_FILTER)vp9_read_tree(r, vp9_switchable_interp_tree,
  306. cm->fc->switchable_interp_prob[ctx]);
  307. FRAME_COUNTS *counts = xd->counts;
  308. if (counts)
  309. ++counts->switchable_interp[ctx][type];
  310. return type;
  311. }
  312. static void read_intra_block_mode_info(VP9_COMMON *const cm,
  313. MACROBLOCKD *const xd, MODE_INFO *mi,
  314. vp9_reader *r) {
  315. MB_MODE_INFO *const mbmi = &mi->mbmi;
  316. const BLOCK_SIZE bsize = mi->mbmi.sb_type;
  317. int i;
  318. mbmi->ref_frame[0] = INTRA_FRAME;
  319. mbmi->ref_frame[1] = NONE;
  320. switch (bsize) {
  321. case BLOCK_4X4:
  322. for (i = 0; i < 4; ++i)
  323. mi->bmi[i].as_mode = read_intra_mode_y(cm, xd, r, 0);
  324. mbmi->mode = mi->bmi[3].as_mode;
  325. break;
  326. case BLOCK_4X8:
  327. mi->bmi[0].as_mode = mi->bmi[2].as_mode = read_intra_mode_y(cm, xd,
  328. r, 0);
  329. mi->bmi[1].as_mode = mi->bmi[3].as_mode = mbmi->mode =
  330. read_intra_mode_y(cm, xd, r, 0);
  331. break;
  332. case BLOCK_8X4:
  333. mi->bmi[0].as_mode = mi->bmi[1].as_mode = read_intra_mode_y(cm, xd,
  334. r, 0);
  335. mi->bmi[2].as_mode = mi->bmi[3].as_mode = mbmi->mode =
  336. read_intra_mode_y(cm, xd, r, 0);
  337. break;
  338. default:
  339. mbmi->mode = read_intra_mode_y(cm, xd, r, size_group_lookup[bsize]);
  340. }
  341. mbmi->uv_mode = read_intra_mode_uv(cm, xd, r, mbmi->mode);
  342. }
  343. static INLINE int is_mv_valid(const MV *mv) {
  344. return mv->row > MV_LOW && mv->row < MV_UPP &&
  345. mv->col > MV_LOW && mv->col < MV_UPP;
  346. }
  347. static INLINE int assign_mv(VP9_COMMON *cm, MACROBLOCKD *xd,
  348. PREDICTION_MODE mode,
  349. int_mv mv[2], int_mv ref_mv[2],
  350. int_mv nearest_mv[2], int_mv near_mv[2],
  351. int is_compound, int allow_hp, vp9_reader *r) {
  352. int i;
  353. int ret = 1;
  354. switch (mode) {
  355. case NEWMV: {
  356. FRAME_COUNTS *counts = xd->counts;
  357. nmv_context_counts *const mv_counts = counts ? &counts->mv : NULL;
  358. for (i = 0; i < 1 + is_compound; ++i) {
  359. read_mv(r, &mv[i].as_mv, &ref_mv[i].as_mv, &cm->fc->nmvc, mv_counts,
  360. allow_hp);
  361. ret = ret && is_mv_valid(&mv[i].as_mv);
  362. }
  363. break;
  364. }
  365. case NEARESTMV: {
  366. mv[0].as_int = nearest_mv[0].as_int;
  367. if (is_compound)
  368. mv[1].as_int = nearest_mv[1].as_int;
  369. break;
  370. }
  371. case NEARMV: {
  372. mv[0].as_int = near_mv[0].as_int;
  373. if (is_compound)
  374. mv[1].as_int = near_mv[1].as_int;
  375. break;
  376. }
  377. case ZEROMV: {
  378. mv[0].as_int = 0;
  379. if (is_compound)
  380. mv[1].as_int = 0;
  381. break;
  382. }
  383. default: {
  384. return 0;
  385. }
  386. }
  387. return ret;
  388. }
  389. static int read_is_inter_block(VP9_COMMON *const cm, MACROBLOCKD *const xd,
  390. int segment_id, vp9_reader *r) {
  391. if (vp9_segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) {
  392. return vp9_get_segdata(&cm->seg, segment_id, SEG_LVL_REF_FRAME) !=
  393. INTRA_FRAME;
  394. } else {
  395. const int ctx = vp9_get_intra_inter_context(xd);
  396. const int is_inter = vp9_read(r, cm->fc->intra_inter_prob[ctx]);
  397. FRAME_COUNTS *counts = xd->counts;
  398. if (counts)
  399. ++counts->intra_inter[ctx][is_inter];
  400. return is_inter;
  401. }
  402. }
  403. static void fpm_sync(void *const data, int mi_row) {
  404. VP9Decoder *const pbi = (VP9Decoder *)data;
  405. vp9_frameworker_wait(pbi->frame_worker_owner, pbi->common.prev_frame,
  406. mi_row << MI_BLOCK_SIZE_LOG2);
  407. }
  408. static void read_inter_block_mode_info(VP9Decoder *const pbi,
  409. MACROBLOCKD *const xd,
  410. const TileInfo *const tile,
  411. MODE_INFO *const mi,
  412. int mi_row, int mi_col, vp9_reader *r) {
  413. VP9_COMMON *const cm = &pbi->common;
  414. MB_MODE_INFO *const mbmi = &mi->mbmi;
  415. const BLOCK_SIZE bsize = mbmi->sb_type;
  416. const int allow_hp = cm->allow_high_precision_mv;
  417. int_mv nearestmv[2], nearmv[2];
  418. int inter_mode_ctx, ref, is_compound;
  419. read_ref_frames(cm, xd, r, mbmi->segment_id, mbmi->ref_frame);
  420. is_compound = has_second_ref(mbmi);
  421. for (ref = 0; ref < 1 + is_compound; ++ref) {
  422. const MV_REFERENCE_FRAME frame = mbmi->ref_frame[ref];
  423. RefBuffer *ref_buf = &cm->frame_refs[frame - LAST_FRAME];
  424. xd->block_refs[ref] = ref_buf;
  425. if ((!vp9_is_valid_scale(&ref_buf->sf)))
  426. vpx_internal_error(xd->error_info, VPX_CODEC_UNSUP_BITSTREAM,
  427. "Reference frame has invalid dimensions");
  428. vp9_setup_pre_planes(xd, ref, ref_buf->buf, mi_row, mi_col,
  429. &ref_buf->sf);
  430. vp9_find_mv_refs(cm, xd, tile, mi, frame, mbmi->ref_mvs[frame],
  431. mi_row, mi_col, fpm_sync, (void *)pbi);
  432. }
  433. inter_mode_ctx = mbmi->mode_context[mbmi->ref_frame[0]];
  434. if (vp9_segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
  435. mbmi->mode = ZEROMV;
  436. if (bsize < BLOCK_8X8) {
  437. vpx_internal_error(xd->error_info, VPX_CODEC_UNSUP_BITSTREAM,
  438. "Invalid usage of segement feature on small blocks");
  439. return;
  440. }
  441. } else {
  442. if (bsize >= BLOCK_8X8)
  443. mbmi->mode = read_inter_mode(cm, xd, r, inter_mode_ctx);
  444. }
  445. if (bsize < BLOCK_8X8 || mbmi->mode != ZEROMV) {
  446. for (ref = 0; ref < 1 + is_compound; ++ref) {
  447. vp9_find_best_ref_mvs(xd, allow_hp, mbmi->ref_mvs[mbmi->ref_frame[ref]],
  448. &nearestmv[ref], &nearmv[ref]);
  449. }
  450. }
  451. mbmi->interp_filter = (cm->interp_filter == SWITCHABLE)
  452. ? read_switchable_interp_filter(cm, xd, r)
  453. : cm->interp_filter;
  454. if (bsize < BLOCK_8X8) {
  455. const int num_4x4_w = num_4x4_blocks_wide_lookup[bsize]; // 1 or 2
  456. const int num_4x4_h = num_4x4_blocks_high_lookup[bsize]; // 1 or 2
  457. int idx, idy;
  458. PREDICTION_MODE b_mode;
  459. int_mv nearest_sub8x8[2], near_sub8x8[2];
  460. for (idy = 0; idy < 2; idy += num_4x4_h) {
  461. for (idx = 0; idx < 2; idx += num_4x4_w) {
  462. int_mv block[2];
  463. const int j = idy * 2 + idx;
  464. b_mode = read_inter_mode(cm, xd, r, inter_mode_ctx);
  465. if (b_mode == NEARESTMV || b_mode == NEARMV)
  466. for (ref = 0; ref < 1 + is_compound; ++ref)
  467. vp9_append_sub8x8_mvs_for_idx(cm, xd, tile, j, ref, mi_row, mi_col,
  468. &nearest_sub8x8[ref],
  469. &near_sub8x8[ref]);
  470. if (!assign_mv(cm, xd, b_mode, block, nearestmv,
  471. nearest_sub8x8, near_sub8x8,
  472. is_compound, allow_hp, r)) {
  473. xd->corrupted |= 1;
  474. break;
  475. };
  476. mi->bmi[j].as_mv[0].as_int = block[0].as_int;
  477. if (is_compound)
  478. mi->bmi[j].as_mv[1].as_int = block[1].as_int;
  479. if (num_4x4_h == 2)
  480. mi->bmi[j + 2] = mi->bmi[j];
  481. if (num_4x4_w == 2)
  482. mi->bmi[j + 1] = mi->bmi[j];
  483. }
  484. }
  485. mi->mbmi.mode = b_mode;
  486. mbmi->mv[0].as_int = mi->bmi[3].as_mv[0].as_int;
  487. mbmi->mv[1].as_int = mi->bmi[3].as_mv[1].as_int;
  488. } else {
  489. xd->corrupted |= !assign_mv(cm, xd, mbmi->mode, mbmi->mv, nearestmv,
  490. nearestmv, nearmv, is_compound, allow_hp, r);
  491. }
  492. }
  493. static void read_inter_frame_mode_info(VP9Decoder *const pbi,
  494. MACROBLOCKD *const xd,
  495. const TileInfo *const tile,
  496. int mi_row, int mi_col, vp9_reader *r) {
  497. VP9_COMMON *const cm = &pbi->common;
  498. MODE_INFO *const mi = xd->mi[0];
  499. MB_MODE_INFO *const mbmi = &mi->mbmi;
  500. int inter_block;
  501. mbmi->mv[0].as_int = 0;
  502. mbmi->mv[1].as_int = 0;
  503. mbmi->segment_id = read_inter_segment_id(cm, xd, mi_row, mi_col, r);
  504. mbmi->skip = read_skip(cm, xd, mbmi->segment_id, r);
  505. inter_block = read_is_inter_block(cm, xd, mbmi->segment_id, r);
  506. mbmi->tx_size = read_tx_size(cm, xd, !mbmi->skip || !inter_block, r);
  507. if (inter_block)
  508. read_inter_block_mode_info(pbi, xd, tile, mi, mi_row, mi_col, r);
  509. else
  510. read_intra_block_mode_info(cm, xd, mi, r);
  511. }
  512. void vp9_read_mode_info(VP9Decoder *const pbi, MACROBLOCKD *xd,
  513. const TileInfo *const tile,
  514. int mi_row, int mi_col, vp9_reader *r) {
  515. VP9_COMMON *const cm = &pbi->common;
  516. MODE_INFO *const mi = xd->mi[0];
  517. const int bw = num_8x8_blocks_wide_lookup[mi->mbmi.sb_type];
  518. const int bh = num_8x8_blocks_high_lookup[mi->mbmi.sb_type];
  519. const int x_mis = MIN(bw, cm->mi_cols - mi_col);
  520. const int y_mis = MIN(bh, cm->mi_rows - mi_row);
  521. MV_REF* frame_mvs = cm->cur_frame->mvs + mi_row * cm->mi_cols + mi_col;
  522. int w, h;
  523. if (frame_is_intra_only(cm))
  524. read_intra_frame_mode_info(cm, xd, mi_row, mi_col, r);
  525. else
  526. read_inter_frame_mode_info(pbi, xd, tile, mi_row, mi_col, r);
  527. for (h = 0; h < y_mis; ++h) {
  528. MV_REF *const frame_mv = frame_mvs + h * cm->mi_cols;
  529. for (w = 0; w < x_mis; ++w) {
  530. MV_REF *const mv = frame_mv + w;
  531. mv->ref_frame[0] = mi->mbmi.ref_frame[0];
  532. mv->ref_frame[1] = mi->mbmi.ref_frame[1];
  533. mv->mv[0].as_int = mi->mbmi.mv[0].as_int;
  534. mv->mv[1].as_int = mi->mbmi.mv[1].as_int;
  535. }
  536. }
  537. }