vp9_decodemv.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  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. static void fpm_sync(void *const data, int mi_row) {
  394. VP9Decoder *const pbi = (VP9Decoder *)data;
  395. vp9_frameworker_wait(pbi->frame_worker_owner, pbi->common.prev_frame,
  396. mi_row << MI_BLOCK_SIZE_LOG2);
  397. }
  398. // This macro is used to add a motion vector mv_ref list if it isn't
  399. // already in the list. If it's the second motion vector or early_break
  400. // it will also skip all additional processing and jump to Done!
  401. #define ADD_MV_REF_LIST_EB(mv, refmv_count, mv_ref_list, Done) \
  402. do { \
  403. if (refmv_count) { \
  404. if ((mv).as_int != (mv_ref_list)[0].as_int) { \
  405. (mv_ref_list)[(refmv_count)] = (mv); \
  406. refmv_count++; \
  407. goto Done; \
  408. } \
  409. } else { \
  410. (mv_ref_list)[(refmv_count)++] = (mv); \
  411. if (early_break) goto Done; \
  412. } \
  413. } while (0)
  414. // If either reference frame is different, not INTRA, and they
  415. // are different from each other scale and add the mv to our list.
  416. #define IF_DIFF_REF_FRAME_ADD_MV_EB(mbmi, ref_frame, ref_sign_bias, \
  417. refmv_count, mv_ref_list, Done) \
  418. do { \
  419. if (is_inter_block(mbmi)) { \
  420. if ((mbmi)->ref_frame[0] != ref_frame) \
  421. ADD_MV_REF_LIST_EB(scale_mv((mbmi), 0, ref_frame, ref_sign_bias), \
  422. refmv_count, mv_ref_list, Done); \
  423. if (has_second_ref(mbmi) && (mbmi)->ref_frame[1] != ref_frame && \
  424. (mbmi)->mv[1].as_int != (mbmi)->mv[0].as_int) \
  425. ADD_MV_REF_LIST_EB(scale_mv((mbmi), 1, ref_frame, ref_sign_bias), \
  426. refmv_count, mv_ref_list, Done); \
  427. } \
  428. } while (0)
  429. // This function searches the neighborhood of a given MB/SB
  430. // to try and find candidate reference vectors.
  431. static int dec_find_mv_refs(const VP9_COMMON *cm, const MACROBLOCKD *xd,
  432. PREDICTION_MODE mode, MV_REFERENCE_FRAME ref_frame,
  433. const POSITION *const mv_ref_search,
  434. int_mv *mv_ref_list, int mi_row, int mi_col,
  435. int block, int is_sub8x8, find_mv_refs_sync sync,
  436. void *const data) {
  437. const int *ref_sign_bias = cm->ref_frame_sign_bias;
  438. int i, refmv_count = 0;
  439. int different_ref_found = 0;
  440. const MV_REF *const prev_frame_mvs =
  441. cm->use_prev_frame_mvs
  442. ? cm->prev_frame->mvs + mi_row * cm->mi_cols + mi_col
  443. : NULL;
  444. const TileInfo *const tile = &xd->tile;
  445. // If mode is nearestmv or newmv (uses nearestmv as a reference) then stop
  446. // searching after the first mv is found.
  447. const int early_break = (mode != NEARMV);
  448. // Blank the reference vector list
  449. memset(mv_ref_list, 0, sizeof(*mv_ref_list) * MAX_MV_REF_CANDIDATES);
  450. i = 0;
  451. if (is_sub8x8) {
  452. // If the size < 8x8 we get the mv from the bmi substructure for the
  453. // nearest two blocks.
  454. for (i = 0; i < 2; ++i) {
  455. const POSITION *const mv_ref = &mv_ref_search[i];
  456. if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
  457. const MODE_INFO *const candidate_mi =
  458. xd->mi[mv_ref->col + mv_ref->row * xd->mi_stride];
  459. different_ref_found = 1;
  460. if (candidate_mi->ref_frame[0] == ref_frame)
  461. ADD_MV_REF_LIST_EB(
  462. get_sub_block_mv(candidate_mi, 0, mv_ref->col, block),
  463. refmv_count, mv_ref_list, Done);
  464. else if (candidate_mi->ref_frame[1] == ref_frame)
  465. ADD_MV_REF_LIST_EB(
  466. get_sub_block_mv(candidate_mi, 1, mv_ref->col, block),
  467. refmv_count, mv_ref_list, Done);
  468. }
  469. }
  470. }
  471. // Check the rest of the neighbors in much the same way
  472. // as before except we don't need to keep track of sub blocks or
  473. // mode counts.
  474. for (; i < MVREF_NEIGHBOURS; ++i) {
  475. const POSITION *const mv_ref = &mv_ref_search[i];
  476. if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
  477. const MODE_INFO *const candidate =
  478. xd->mi[mv_ref->col + mv_ref->row * xd->mi_stride];
  479. different_ref_found = 1;
  480. if (candidate->ref_frame[0] == ref_frame)
  481. ADD_MV_REF_LIST_EB(candidate->mv[0], refmv_count, mv_ref_list, Done);
  482. else if (candidate->ref_frame[1] == ref_frame)
  483. ADD_MV_REF_LIST_EB(candidate->mv[1], refmv_count, mv_ref_list, Done);
  484. }
  485. }
  486. // TODO(hkuang): Remove this sync after fixing pthread_cond_broadcast
  487. // on windows platform. The sync here is unnecessary if use_prev_frame_mvs
  488. // is 0. But after removing it, there will be hang in the unit test on windows
  489. // due to several threads waiting for a thread's signal.
  490. #if defined(_WIN32) && !HAVE_PTHREAD_H
  491. if (cm->frame_parallel_decode && sync != NULL) {
  492. sync(data, mi_row);
  493. }
  494. #endif
  495. // Check the last frame's mode and mv info.
  496. if (prev_frame_mvs) {
  497. // Synchronize here for frame parallel decode if sync function is provided.
  498. if (cm->frame_parallel_decode && sync != NULL) {
  499. sync(data, mi_row);
  500. }
  501. if (prev_frame_mvs->ref_frame[0] == ref_frame) {
  502. ADD_MV_REF_LIST_EB(prev_frame_mvs->mv[0], refmv_count, mv_ref_list, Done);
  503. } else if (prev_frame_mvs->ref_frame[1] == ref_frame) {
  504. ADD_MV_REF_LIST_EB(prev_frame_mvs->mv[1], refmv_count, mv_ref_list, Done);
  505. }
  506. }
  507. // Since we couldn't find 2 mvs from the same reference frame
  508. // go back through the neighbors and find motion vectors from
  509. // different reference frames.
  510. if (different_ref_found) {
  511. for (i = 0; i < MVREF_NEIGHBOURS; ++i) {
  512. const POSITION *mv_ref = &mv_ref_search[i];
  513. if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
  514. const MODE_INFO *const candidate =
  515. xd->mi[mv_ref->col + mv_ref->row * xd->mi_stride];
  516. // If the candidate is INTRA we don't want to consider its mv.
  517. IF_DIFF_REF_FRAME_ADD_MV_EB(candidate, ref_frame, ref_sign_bias,
  518. refmv_count, mv_ref_list, Done);
  519. }
  520. }
  521. }
  522. // Since we still don't have a candidate we'll try the last frame.
  523. if (prev_frame_mvs) {
  524. if (prev_frame_mvs->ref_frame[0] != ref_frame &&
  525. prev_frame_mvs->ref_frame[0] > INTRA_FRAME) {
  526. int_mv mv = prev_frame_mvs->mv[0];
  527. if (ref_sign_bias[prev_frame_mvs->ref_frame[0]] !=
  528. ref_sign_bias[ref_frame]) {
  529. mv.as_mv.row *= -1;
  530. mv.as_mv.col *= -1;
  531. }
  532. ADD_MV_REF_LIST_EB(mv, refmv_count, mv_ref_list, Done);
  533. }
  534. if (prev_frame_mvs->ref_frame[1] > INTRA_FRAME &&
  535. prev_frame_mvs->ref_frame[1] != ref_frame &&
  536. prev_frame_mvs->mv[1].as_int != prev_frame_mvs->mv[0].as_int) {
  537. int_mv mv = prev_frame_mvs->mv[1];
  538. if (ref_sign_bias[prev_frame_mvs->ref_frame[1]] !=
  539. ref_sign_bias[ref_frame]) {
  540. mv.as_mv.row *= -1;
  541. mv.as_mv.col *= -1;
  542. }
  543. ADD_MV_REF_LIST_EB(mv, refmv_count, mv_ref_list, Done);
  544. }
  545. }
  546. if (mode == NEARMV)
  547. refmv_count = MAX_MV_REF_CANDIDATES;
  548. else
  549. // we only care about the nearestmv for the remaining modes
  550. refmv_count = 1;
  551. Done:
  552. // Clamp vectors
  553. for (i = 0; i < refmv_count; ++i) clamp_mv_ref(&mv_ref_list[i].as_mv, xd);
  554. return refmv_count;
  555. }
  556. static void append_sub8x8_mvs_for_idx(VP9_COMMON *cm, MACROBLOCKD *xd,
  557. const POSITION *const mv_ref_search,
  558. PREDICTION_MODE b_mode, int block,
  559. int ref, int mi_row, int mi_col,
  560. int_mv *best_sub8x8) {
  561. int_mv mv_list[MAX_MV_REF_CANDIDATES];
  562. MODE_INFO *const mi = xd->mi[0];
  563. b_mode_info *bmi = mi->bmi;
  564. int n;
  565. int refmv_count;
  566. assert(MAX_MV_REF_CANDIDATES == 2);
  567. refmv_count =
  568. dec_find_mv_refs(cm, xd, b_mode, mi->ref_frame[ref], mv_ref_search,
  569. mv_list, mi_row, mi_col, block, 1, NULL, NULL);
  570. switch (block) {
  571. case 0: best_sub8x8->as_int = mv_list[refmv_count - 1].as_int; break;
  572. case 1:
  573. case 2:
  574. if (b_mode == NEARESTMV) {
  575. best_sub8x8->as_int = bmi[0].as_mv[ref].as_int;
  576. } else {
  577. best_sub8x8->as_int = 0;
  578. for (n = 0; n < refmv_count; ++n)
  579. if (bmi[0].as_mv[ref].as_int != mv_list[n].as_int) {
  580. best_sub8x8->as_int = mv_list[n].as_int;
  581. break;
  582. }
  583. }
  584. break;
  585. case 3:
  586. if (b_mode == NEARESTMV) {
  587. best_sub8x8->as_int = bmi[2].as_mv[ref].as_int;
  588. } else {
  589. int_mv candidates[2 + MAX_MV_REF_CANDIDATES];
  590. candidates[0] = bmi[1].as_mv[ref];
  591. candidates[1] = bmi[0].as_mv[ref];
  592. candidates[2] = mv_list[0];
  593. candidates[3] = mv_list[1];
  594. best_sub8x8->as_int = 0;
  595. for (n = 0; n < 2 + MAX_MV_REF_CANDIDATES; ++n)
  596. if (bmi[2].as_mv[ref].as_int != candidates[n].as_int) {
  597. best_sub8x8->as_int = candidates[n].as_int;
  598. break;
  599. }
  600. }
  601. break;
  602. default: assert(0 && "Invalid block index.");
  603. }
  604. }
  605. static uint8_t get_mode_context(const VP9_COMMON *cm, const MACROBLOCKD *xd,
  606. const POSITION *const mv_ref_search, int mi_row,
  607. int mi_col) {
  608. int i;
  609. int context_counter = 0;
  610. const TileInfo *const tile = &xd->tile;
  611. // Get mode count from nearest 2 blocks
  612. for (i = 0; i < 2; ++i) {
  613. const POSITION *const mv_ref = &mv_ref_search[i];
  614. if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
  615. const MODE_INFO *const candidate =
  616. xd->mi[mv_ref->col + mv_ref->row * xd->mi_stride];
  617. // Keep counts for entropy encoding.
  618. context_counter += mode_2_counter[candidate->mode];
  619. }
  620. }
  621. return counter_to_context[context_counter];
  622. }
  623. static void read_inter_block_mode_info(VP9Decoder *const pbi,
  624. MACROBLOCKD *const xd,
  625. MODE_INFO *const mi, int mi_row,
  626. int mi_col, vpx_reader *r) {
  627. VP9_COMMON *const cm = &pbi->common;
  628. const BLOCK_SIZE bsize = mi->sb_type;
  629. const int allow_hp = cm->allow_high_precision_mv;
  630. int_mv best_ref_mvs[2];
  631. int ref, is_compound;
  632. uint8_t inter_mode_ctx;
  633. const POSITION *const mv_ref_search = mv_ref_blocks[bsize];
  634. read_ref_frames(cm, xd, r, mi->segment_id, mi->ref_frame);
  635. is_compound = has_second_ref(mi);
  636. inter_mode_ctx = get_mode_context(cm, xd, mv_ref_search, mi_row, mi_col);
  637. if (segfeature_active(&cm->seg, mi->segment_id, SEG_LVL_SKIP)) {
  638. mi->mode = ZEROMV;
  639. if (bsize < BLOCK_8X8) {
  640. vpx_internal_error(xd->error_info, VPX_CODEC_UNSUP_BITSTREAM,
  641. "Invalid usage of segement feature on small blocks");
  642. return;
  643. }
  644. } else {
  645. if (bsize >= BLOCK_8X8)
  646. mi->mode = read_inter_mode(cm, xd, r, inter_mode_ctx);
  647. else
  648. // Sub 8x8 blocks use the nearestmv as a ref_mv if the b_mode is NEWMV.
  649. // Setting mode to NEARESTMV forces the search to stop after the nearestmv
  650. // has been found. After b_modes have been read, mode will be overwritten
  651. // by the last b_mode.
  652. mi->mode = NEARESTMV;
  653. if (mi->mode != ZEROMV) {
  654. for (ref = 0; ref < 1 + is_compound; ++ref) {
  655. int_mv tmp_mvs[MAX_MV_REF_CANDIDATES];
  656. const MV_REFERENCE_FRAME frame = mi->ref_frame[ref];
  657. int refmv_count;
  658. refmv_count =
  659. dec_find_mv_refs(cm, xd, mi->mode, frame, mv_ref_search, tmp_mvs,
  660. mi_row, mi_col, -1, 0, fpm_sync, (void *)pbi);
  661. dec_find_best_ref_mvs(allow_hp, tmp_mvs, &best_ref_mvs[ref],
  662. refmv_count);
  663. }
  664. }
  665. }
  666. mi->interp_filter = (cm->interp_filter == SWITCHABLE)
  667. ? read_switchable_interp_filter(cm, xd, r)
  668. : cm->interp_filter;
  669. if (bsize < BLOCK_8X8) {
  670. const int num_4x4_w = 1 << xd->bmode_blocks_wl;
  671. const int num_4x4_h = 1 << xd->bmode_blocks_hl;
  672. int idx, idy;
  673. PREDICTION_MODE b_mode;
  674. int_mv best_sub8x8[2];
  675. for (idy = 0; idy < 2; idy += num_4x4_h) {
  676. for (idx = 0; idx < 2; idx += num_4x4_w) {
  677. const int j = idy * 2 + idx;
  678. b_mode = read_inter_mode(cm, xd, r, inter_mode_ctx);
  679. if (b_mode == NEARESTMV || b_mode == NEARMV) {
  680. for (ref = 0; ref < 1 + is_compound; ++ref)
  681. append_sub8x8_mvs_for_idx(cm, xd, mv_ref_search, b_mode, j, ref,
  682. mi_row, mi_col, &best_sub8x8[ref]);
  683. }
  684. if (!assign_mv(cm, xd, b_mode, mi->bmi[j].as_mv, best_ref_mvs,
  685. best_sub8x8, is_compound, allow_hp, r)) {
  686. xd->corrupted |= 1;
  687. break;
  688. }
  689. if (num_4x4_h == 2) mi->bmi[j + 2] = mi->bmi[j];
  690. if (num_4x4_w == 2) mi->bmi[j + 1] = mi->bmi[j];
  691. }
  692. }
  693. mi->mode = b_mode;
  694. copy_mv_pair(mi->mv, mi->bmi[3].as_mv);
  695. } else {
  696. xd->corrupted |= !assign_mv(cm, xd, mi->mode, mi->mv, best_ref_mvs,
  697. best_ref_mvs, is_compound, allow_hp, r);
  698. }
  699. }
  700. static void read_inter_frame_mode_info(VP9Decoder *const pbi,
  701. MACROBLOCKD *const xd, int mi_row,
  702. int mi_col, vpx_reader *r, int x_mis,
  703. int y_mis) {
  704. VP9_COMMON *const cm = &pbi->common;
  705. MODE_INFO *const mi = xd->mi[0];
  706. int inter_block;
  707. mi->segment_id =
  708. read_inter_segment_id(cm, xd, mi_row, mi_col, r, x_mis, y_mis);
  709. mi->skip = read_skip(cm, xd, mi->segment_id, r);
  710. inter_block = read_is_inter_block(cm, xd, mi->segment_id, r);
  711. mi->tx_size = read_tx_size(cm, xd, !mi->skip || !inter_block, r);
  712. if (inter_block)
  713. read_inter_block_mode_info(pbi, xd, mi, mi_row, mi_col, r);
  714. else
  715. read_intra_block_mode_info(cm, xd, mi, r);
  716. }
  717. static INLINE void copy_ref_frame_pair(MV_REFERENCE_FRAME *dst,
  718. const MV_REFERENCE_FRAME *src) {
  719. memcpy(dst, src, sizeof(*dst) * 2);
  720. }
  721. void vp9_read_mode_info(TileWorkerData *twd, VP9Decoder *const pbi, int mi_row,
  722. int mi_col, int x_mis, int y_mis) {
  723. vpx_reader *r = &twd->bit_reader;
  724. MACROBLOCKD *const xd = &twd->xd;
  725. VP9_COMMON *const cm = &pbi->common;
  726. MODE_INFO *const mi = xd->mi[0];
  727. MV_REF *frame_mvs = cm->cur_frame->mvs + mi_row * cm->mi_cols + mi_col;
  728. int w, h;
  729. if (frame_is_intra_only(cm)) {
  730. read_intra_frame_mode_info(cm, xd, mi_row, mi_col, r, x_mis, y_mis);
  731. } else {
  732. read_inter_frame_mode_info(pbi, xd, mi_row, mi_col, r, x_mis, y_mis);
  733. for (h = 0; h < y_mis; ++h) {
  734. for (w = 0; w < x_mis; ++w) {
  735. MV_REF *const mv = frame_mvs + w;
  736. copy_ref_frame_pair(mv->ref_frame, mi->ref_frame);
  737. copy_mv_pair(mv->mv, mi->mv);
  738. }
  739. frame_mvs += cm->mi_cols;
  740. }
  741. }
  742. #if 0 // CONFIG_BETTER_HW_COMPATIBILITY && CONFIG_VP9_HIGHBITDEPTH
  743. if ((xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) &&
  744. (xd->above_mi == NULL || xd->left_mi == NULL) &&
  745. !is_inter_block(mi) && need_top_left[mi->uv_mode])
  746. assert(0);
  747. #endif // CONFIG_BETTER_HW_COMPATIBILITY && CONFIG_VP9_HIGHBITDEPTH
  748. }