error_concealment.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /*
  2. * Copyright (c) 2011 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 "error_concealment.h"
  12. #include "onyxd_int.h"
  13. #include "decodemv.h"
  14. #include "vpx_mem/vpx_mem.h"
  15. #include "vp8/common/findnearmv.h"
  16. #include "vp8/common/common.h"
  17. #include "vpx_dsp/vpx_dsp_common.h"
  18. #define FLOOR(x, q) ((x) & -(1 << (q)))
  19. #define NUM_NEIGHBORS 20
  20. typedef struct ec_position {
  21. int row;
  22. int col;
  23. } EC_POS;
  24. /*
  25. * Regenerate the table in Matlab with:
  26. * x = meshgrid((1:4), (1:4));
  27. * y = meshgrid((1:4), (1:4))';
  28. * W = round((1./(sqrt(x.^2 + y.^2))*2^7));
  29. * W(1,1) = 0;
  30. */
  31. static const int weights_q7[5][5] = { { 0, 128, 64, 43, 32 },
  32. { 128, 91, 57, 40, 31 },
  33. { 64, 57, 45, 36, 29 },
  34. { 43, 40, 36, 30, 26 },
  35. { 32, 31, 29, 26, 23 } };
  36. int vp8_alloc_overlap_lists(VP8D_COMP *pbi) {
  37. if (pbi->overlaps != NULL) {
  38. vpx_free(pbi->overlaps);
  39. pbi->overlaps = NULL;
  40. }
  41. pbi->overlaps =
  42. vpx_calloc(pbi->common.mb_rows * pbi->common.mb_cols, sizeof(MB_OVERLAP));
  43. if (pbi->overlaps == NULL) return -1;
  44. return 0;
  45. }
  46. void vp8_de_alloc_overlap_lists(VP8D_COMP *pbi) {
  47. vpx_free(pbi->overlaps);
  48. pbi->overlaps = NULL;
  49. }
  50. /* Inserts a new overlap area value to the list of overlaps of a block */
  51. static void assign_overlap(OVERLAP_NODE *overlaps, union b_mode_info *bmi,
  52. int overlap) {
  53. int i;
  54. if (overlap <= 0) return;
  55. /* Find and assign to the next empty overlap node in the list of overlaps.
  56. * Empty is defined as bmi == NULL */
  57. for (i = 0; i < MAX_OVERLAPS; ++i) {
  58. if (overlaps[i].bmi == NULL) {
  59. overlaps[i].bmi = bmi;
  60. overlaps[i].overlap = overlap;
  61. break;
  62. }
  63. }
  64. }
  65. /* Calculates the overlap area between two 4x4 squares, where the first
  66. * square has its upper-left corner at (b1_row, b1_col) and the second
  67. * square has its upper-left corner at (b2_row, b2_col). Doesn't
  68. * properly handle squares which do not overlap.
  69. */
  70. static int block_overlap(int b1_row, int b1_col, int b2_row, int b2_col) {
  71. const int int_top = VPXMAX(b1_row, b2_row); // top
  72. const int int_left = VPXMAX(b1_col, b2_col); // left
  73. /* Since each block is 4x4 pixels, adding 4 (Q3) to the left/top edge
  74. * gives us the right/bottom edge.
  75. */
  76. const int int_right = VPXMIN(b1_col + (4 << 3), b2_col + (4 << 3)); // right
  77. const int int_bottom =
  78. VPXMIN(b1_row + (4 << 3), b2_row + (4 << 3)); // bottom
  79. return (int_bottom - int_top) * (int_right - int_left);
  80. }
  81. /* Calculates the overlap area for all blocks in a macroblock at position
  82. * (mb_row, mb_col) in macroblocks, which are being overlapped by a given
  83. * overlapping block at position (new_row, new_col) (in pixels, Q3). The
  84. * first block being overlapped in the macroblock has position (first_blk_row,
  85. * first_blk_col) in blocks relative the upper-left corner of the image.
  86. */
  87. static void calculate_overlaps_mb(B_OVERLAP *b_overlaps, union b_mode_info *bmi,
  88. int new_row, int new_col, int mb_row,
  89. int mb_col, int first_blk_row,
  90. int first_blk_col) {
  91. /* Find the blocks within this MB (defined by mb_row, mb_col) which are
  92. * overlapped by bmi and calculate and assign overlap for each of those
  93. * blocks. */
  94. /* Block coordinates relative the upper-left block */
  95. const int rel_ol_blk_row = first_blk_row - mb_row * 4;
  96. const int rel_ol_blk_col = first_blk_col - mb_col * 4;
  97. /* If the block partly overlaps any previous MB, these coordinates
  98. * can be < 0. We don't want to access blocks in previous MBs.
  99. */
  100. const int blk_idx = VPXMAX(rel_ol_blk_row, 0) * 4 + VPXMAX(rel_ol_blk_col, 0);
  101. /* Upper left overlapping block */
  102. B_OVERLAP *b_ol_ul = &(b_overlaps[blk_idx]);
  103. /* Calculate and assign overlaps for all blocks in this MB
  104. * which the motion compensated block overlaps
  105. */
  106. /* Avoid calculating overlaps for blocks in later MBs */
  107. int end_row = VPXMIN(4 + mb_row * 4 - first_blk_row, 2);
  108. int end_col = VPXMIN(4 + mb_col * 4 - first_blk_col, 2);
  109. int row, col;
  110. /* Check if new_row and new_col are evenly divisible by 4 (Q3),
  111. * and if so we shouldn't check neighboring blocks
  112. */
  113. if (new_row >= 0 && (new_row & 0x1F) == 0) end_row = 1;
  114. if (new_col >= 0 && (new_col & 0x1F) == 0) end_col = 1;
  115. /* Check if the overlapping block partly overlaps a previous MB
  116. * and if so, we're overlapping fewer blocks in this MB.
  117. */
  118. if (new_row < (mb_row * 16) << 3) end_row = 1;
  119. if (new_col < (mb_col * 16) << 3) end_col = 1;
  120. for (row = 0; row < end_row; ++row) {
  121. for (col = 0; col < end_col; ++col) {
  122. /* input in Q3, result in Q6 */
  123. const int overlap =
  124. block_overlap(new_row, new_col, (((first_blk_row + row) * 4) << 3),
  125. (((first_blk_col + col) * 4) << 3));
  126. assign_overlap(b_ol_ul[row * 4 + col].overlaps, bmi, overlap);
  127. }
  128. }
  129. }
  130. static void calculate_overlaps(MB_OVERLAP *overlap_ul, int mb_rows, int mb_cols,
  131. union b_mode_info *bmi, int b_row, int b_col) {
  132. MB_OVERLAP *mb_overlap;
  133. int row, col, rel_row, rel_col;
  134. int new_row, new_col;
  135. int end_row, end_col;
  136. int overlap_b_row, overlap_b_col;
  137. int overlap_mb_row, overlap_mb_col;
  138. /* mb subpixel position */
  139. row = (4 * b_row) << 3; /* Q3 */
  140. col = (4 * b_col) << 3; /* Q3 */
  141. /* reverse compensate for motion */
  142. new_row = row - bmi->mv.as_mv.row;
  143. new_col = col - bmi->mv.as_mv.col;
  144. if (new_row >= ((16 * mb_rows) << 3) || new_col >= ((16 * mb_cols) << 3)) {
  145. /* the new block ended up outside the frame */
  146. return;
  147. }
  148. if (new_row <= -32 || new_col <= -32) {
  149. /* outside the frame */
  150. return;
  151. }
  152. /* overlapping block's position in blocks */
  153. overlap_b_row = FLOOR(new_row / 4, 3) >> 3;
  154. overlap_b_col = FLOOR(new_col / 4, 3) >> 3;
  155. /* overlapping block's MB position in MBs
  156. * operations are done in Q3
  157. */
  158. overlap_mb_row = FLOOR((overlap_b_row << 3) / 4, 3) >> 3;
  159. overlap_mb_col = FLOOR((overlap_b_col << 3) / 4, 3) >> 3;
  160. end_row = VPXMIN(mb_rows - overlap_mb_row, 2);
  161. end_col = VPXMIN(mb_cols - overlap_mb_col, 2);
  162. /* Don't calculate overlap for MBs we don't overlap */
  163. /* Check if the new block row starts at the last block row of the MB */
  164. if (abs(new_row - ((16 * overlap_mb_row) << 3)) < ((3 * 4) << 3)) end_row = 1;
  165. /* Check if the new block col starts at the last block col of the MB */
  166. if (abs(new_col - ((16 * overlap_mb_col) << 3)) < ((3 * 4) << 3)) end_col = 1;
  167. /* find the MB(s) this block is overlapping */
  168. for (rel_row = 0; rel_row < end_row; ++rel_row) {
  169. for (rel_col = 0; rel_col < end_col; ++rel_col) {
  170. if (overlap_mb_row + rel_row < 0 || overlap_mb_col + rel_col < 0)
  171. continue;
  172. mb_overlap = overlap_ul + (overlap_mb_row + rel_row) * mb_cols +
  173. overlap_mb_col + rel_col;
  174. calculate_overlaps_mb(mb_overlap->overlaps, bmi, new_row, new_col,
  175. overlap_mb_row + rel_row, overlap_mb_col + rel_col,
  176. overlap_b_row + rel_row, overlap_b_col + rel_col);
  177. }
  178. }
  179. }
  180. /* Estimates a motion vector given the overlapping blocks' motion vectors.
  181. * Filters out all overlapping blocks which do not refer to the correct
  182. * reference frame type.
  183. */
  184. static void estimate_mv(const OVERLAP_NODE *overlaps, union b_mode_info *bmi) {
  185. int i;
  186. int overlap_sum = 0;
  187. int row_acc = 0;
  188. int col_acc = 0;
  189. bmi->mv.as_int = 0;
  190. for (i = 0; i < MAX_OVERLAPS; ++i) {
  191. if (overlaps[i].bmi == NULL) break;
  192. col_acc += overlaps[i].overlap * overlaps[i].bmi->mv.as_mv.col;
  193. row_acc += overlaps[i].overlap * overlaps[i].bmi->mv.as_mv.row;
  194. overlap_sum += overlaps[i].overlap;
  195. }
  196. if (overlap_sum > 0) {
  197. /* Q9 / Q6 = Q3 */
  198. bmi->mv.as_mv.col = col_acc / overlap_sum;
  199. bmi->mv.as_mv.row = row_acc / overlap_sum;
  200. } else {
  201. bmi->mv.as_mv.col = 0;
  202. bmi->mv.as_mv.row = 0;
  203. }
  204. }
  205. /* Estimates all motion vectors for a macroblock given the lists of
  206. * overlaps for each block. Decides whether or not the MVs must be clamped.
  207. */
  208. static void estimate_mb_mvs(const B_OVERLAP *block_overlaps, MODE_INFO *mi,
  209. int mb_to_left_edge, int mb_to_right_edge,
  210. int mb_to_top_edge, int mb_to_bottom_edge) {
  211. int row, col;
  212. int non_zero_count = 0;
  213. MV *const filtered_mv = &(mi->mbmi.mv.as_mv);
  214. union b_mode_info *const bmi = mi->bmi;
  215. filtered_mv->col = 0;
  216. filtered_mv->row = 0;
  217. mi->mbmi.need_to_clamp_mvs = 0;
  218. for (row = 0; row < 4; ++row) {
  219. int this_b_to_top_edge = mb_to_top_edge + ((row * 4) << 3);
  220. int this_b_to_bottom_edge = mb_to_bottom_edge - ((row * 4) << 3);
  221. for (col = 0; col < 4; ++col) {
  222. int i = row * 4 + col;
  223. int this_b_to_left_edge = mb_to_left_edge + ((col * 4) << 3);
  224. int this_b_to_right_edge = mb_to_right_edge - ((col * 4) << 3);
  225. /* Estimate vectors for all blocks which are overlapped by this */
  226. /* type. Interpolate/extrapolate the rest of the block's MVs */
  227. estimate_mv(block_overlaps[i].overlaps, &(bmi[i]));
  228. mi->mbmi.need_to_clamp_mvs |= vp8_check_mv_bounds(
  229. &bmi[i].mv, this_b_to_left_edge, this_b_to_right_edge,
  230. this_b_to_top_edge, this_b_to_bottom_edge);
  231. if (bmi[i].mv.as_int != 0) {
  232. ++non_zero_count;
  233. filtered_mv->col += bmi[i].mv.as_mv.col;
  234. filtered_mv->row += bmi[i].mv.as_mv.row;
  235. }
  236. }
  237. }
  238. if (non_zero_count > 0) {
  239. filtered_mv->col /= non_zero_count;
  240. filtered_mv->row /= non_zero_count;
  241. }
  242. }
  243. static void calc_prev_mb_overlaps(MB_OVERLAP *overlaps, MODE_INFO *prev_mi,
  244. int mb_row, int mb_col, int mb_rows,
  245. int mb_cols) {
  246. int sub_row;
  247. int sub_col;
  248. for (sub_row = 0; sub_row < 4; ++sub_row) {
  249. for (sub_col = 0; sub_col < 4; ++sub_col) {
  250. calculate_overlaps(overlaps, mb_rows, mb_cols,
  251. &(prev_mi->bmi[sub_row * 4 + sub_col]),
  252. 4 * mb_row + sub_row, 4 * mb_col + sub_col);
  253. }
  254. }
  255. }
  256. /* Estimate all missing motion vectors. This function does the same as the one
  257. * above, but has different input arguments. */
  258. static void estimate_missing_mvs(MB_OVERLAP *overlaps, MODE_INFO *mi,
  259. MODE_INFO *prev_mi, int mb_rows, int mb_cols,
  260. unsigned int first_corrupt) {
  261. int mb_row, mb_col;
  262. memset(overlaps, 0, sizeof(MB_OVERLAP) * mb_rows * mb_cols);
  263. /* First calculate the overlaps for all blocks */
  264. for (mb_row = 0; mb_row < mb_rows; ++mb_row) {
  265. for (mb_col = 0; mb_col < mb_cols; ++mb_col) {
  266. /* We're only able to use blocks referring to the last frame
  267. * when extrapolating new vectors.
  268. */
  269. if (prev_mi->mbmi.ref_frame == LAST_FRAME) {
  270. calc_prev_mb_overlaps(overlaps, prev_mi, mb_row, mb_col, mb_rows,
  271. mb_cols);
  272. }
  273. ++prev_mi;
  274. }
  275. ++prev_mi;
  276. }
  277. mb_row = first_corrupt / mb_cols;
  278. mb_col = first_corrupt - mb_row * mb_cols;
  279. mi += mb_row * (mb_cols + 1) + mb_col;
  280. /* Go through all macroblocks in the current image with missing MVs
  281. * and calculate new MVs using the overlaps.
  282. */
  283. for (; mb_row < mb_rows; ++mb_row) {
  284. int mb_to_top_edge = -((mb_row * 16)) << 3;
  285. int mb_to_bottom_edge = ((mb_rows - 1 - mb_row) * 16) << 3;
  286. for (; mb_col < mb_cols; ++mb_col) {
  287. int mb_to_left_edge = -((mb_col * 16) << 3);
  288. int mb_to_right_edge = ((mb_cols - 1 - mb_col) * 16) << 3;
  289. const B_OVERLAP *block_overlaps =
  290. overlaps[mb_row * mb_cols + mb_col].overlaps;
  291. mi->mbmi.ref_frame = LAST_FRAME;
  292. mi->mbmi.mode = SPLITMV;
  293. mi->mbmi.uv_mode = DC_PRED;
  294. mi->mbmi.partitioning = 3;
  295. mi->mbmi.segment_id = 0;
  296. estimate_mb_mvs(block_overlaps, mi, mb_to_left_edge, mb_to_right_edge,
  297. mb_to_top_edge, mb_to_bottom_edge);
  298. ++mi;
  299. }
  300. mb_col = 0;
  301. ++mi;
  302. }
  303. }
  304. void vp8_estimate_missing_mvs(VP8D_COMP *pbi) {
  305. VP8_COMMON *const pc = &pbi->common;
  306. estimate_missing_mvs(pbi->overlaps, pc->mi, pc->prev_mi, pc->mb_rows,
  307. pc->mb_cols, pbi->mvs_corrupt_from_mb);
  308. }
  309. static void assign_neighbor(EC_BLOCK *neighbor, MODE_INFO *mi, int block_idx) {
  310. assert(mi->mbmi.ref_frame < MAX_REF_FRAMES);
  311. neighbor->ref_frame = mi->mbmi.ref_frame;
  312. neighbor->mv = mi->bmi[block_idx].mv.as_mv;
  313. }
  314. /* Finds the neighboring blocks of a macroblocks. In the general case
  315. * 20 blocks are found. If a fewer number of blocks are found due to
  316. * image boundaries, those positions in the EC_BLOCK array are left "empty".
  317. * The neighbors are enumerated with the upper-left neighbor as the first
  318. * element, the second element refers to the neighbor to right of the previous
  319. * neighbor, and so on. The last element refers to the neighbor below the first
  320. * neighbor.
  321. */
  322. static void find_neighboring_blocks(MODE_INFO *mi, EC_BLOCK *neighbors,
  323. int mb_row, int mb_col, int mb_rows,
  324. int mb_cols, int mi_stride) {
  325. int i = 0;
  326. int j;
  327. if (mb_row > 0) {
  328. /* upper left */
  329. if (mb_col > 0) assign_neighbor(&neighbors[i], mi - mi_stride - 1, 15);
  330. ++i;
  331. /* above */
  332. for (j = 12; j < 16; ++j, ++i)
  333. assign_neighbor(&neighbors[i], mi - mi_stride, j);
  334. } else
  335. i += 5;
  336. if (mb_col < mb_cols - 1) {
  337. /* upper right */
  338. if (mb_row > 0) assign_neighbor(&neighbors[i], mi - mi_stride + 1, 12);
  339. ++i;
  340. /* right */
  341. for (j = 0; j <= 12; j += 4, ++i) assign_neighbor(&neighbors[i], mi + 1, j);
  342. } else
  343. i += 5;
  344. if (mb_row < mb_rows - 1) {
  345. /* lower right */
  346. if (mb_col < mb_cols - 1)
  347. assign_neighbor(&neighbors[i], mi + mi_stride + 1, 0);
  348. ++i;
  349. /* below */
  350. for (j = 0; j < 4; ++j, ++i)
  351. assign_neighbor(&neighbors[i], mi + mi_stride, j);
  352. } else
  353. i += 5;
  354. if (mb_col > 0) {
  355. /* lower left */
  356. if (mb_row < mb_rows - 1)
  357. assign_neighbor(&neighbors[i], mi + mi_stride - 1, 4);
  358. ++i;
  359. /* left */
  360. for (j = 3; j < 16; j += 4, ++i) {
  361. assign_neighbor(&neighbors[i], mi - 1, j);
  362. }
  363. } else
  364. i += 5;
  365. assert(i == 20);
  366. }
  367. /* Interpolates all motion vectors for a macroblock from the neighboring blocks'
  368. * motion vectors.
  369. */
  370. static void interpolate_mvs(MACROBLOCKD *mb, EC_BLOCK *neighbors,
  371. MV_REFERENCE_FRAME dom_ref_frame) {
  372. int row, col, i;
  373. MODE_INFO *const mi = mb->mode_info_context;
  374. /* Table with the position of the neighboring blocks relative the position
  375. * of the upper left block of the current MB. Starting with the upper left
  376. * neighbor and going to the right.
  377. */
  378. const EC_POS neigh_pos[NUM_NEIGHBORS] = {
  379. { -1, -1 }, { -1, 0 }, { -1, 1 }, { -1, 2 }, { -1, 3 }, { -1, 4 }, { 0, 4 },
  380. { 1, 4 }, { 2, 4 }, { 3, 4 }, { 4, 4 }, { 4, 3 }, { 4, 2 }, { 4, 1 },
  381. { 4, 0 }, { 4, -1 }, { 3, -1 }, { 2, -1 }, { 1, -1 }, { 0, -1 }
  382. };
  383. mi->mbmi.need_to_clamp_mvs = 0;
  384. for (row = 0; row < 4; ++row) {
  385. int mb_to_top_edge = mb->mb_to_top_edge + ((row * 4) << 3);
  386. int mb_to_bottom_edge = mb->mb_to_bottom_edge - ((row * 4) << 3);
  387. for (col = 0; col < 4; ++col) {
  388. int mb_to_left_edge = mb->mb_to_left_edge + ((col * 4) << 3);
  389. int mb_to_right_edge = mb->mb_to_right_edge - ((col * 4) << 3);
  390. int w_sum = 0;
  391. int mv_row_sum = 0;
  392. int mv_col_sum = 0;
  393. int_mv *const mv = &(mi->bmi[row * 4 + col].mv);
  394. mv->as_int = 0;
  395. for (i = 0; i < NUM_NEIGHBORS; ++i) {
  396. /* Calculate the weighted sum of neighboring MVs referring
  397. * to the dominant frame type.
  398. */
  399. const int w = weights_q7[abs(row - neigh_pos[i].row)]
  400. [abs(col - neigh_pos[i].col)];
  401. if (neighbors[i].ref_frame != dom_ref_frame) continue;
  402. w_sum += w;
  403. /* Q7 * Q3 = Q10 */
  404. mv_row_sum += w * neighbors[i].mv.row;
  405. mv_col_sum += w * neighbors[i].mv.col;
  406. }
  407. if (w_sum > 0) {
  408. /* Avoid division by zero.
  409. * Normalize with the sum of the coefficients
  410. * Q3 = Q10 / Q7
  411. */
  412. mv->as_mv.row = mv_row_sum / w_sum;
  413. mv->as_mv.col = mv_col_sum / w_sum;
  414. mi->mbmi.need_to_clamp_mvs |=
  415. vp8_check_mv_bounds(mv, mb_to_left_edge, mb_to_right_edge,
  416. mb_to_top_edge, mb_to_bottom_edge);
  417. }
  418. }
  419. }
  420. }
  421. void vp8_interpolate_motion(MACROBLOCKD *mb, int mb_row, int mb_col,
  422. int mb_rows, int mb_cols) {
  423. /* Find relevant neighboring blocks */
  424. EC_BLOCK neighbors[NUM_NEIGHBORS];
  425. int i;
  426. /* Initialize the array. MAX_REF_FRAMES is interpreted as "doesn't exist" */
  427. for (i = 0; i < NUM_NEIGHBORS; ++i) {
  428. neighbors[i].ref_frame = MAX_REF_FRAMES;
  429. neighbors[i].mv.row = neighbors[i].mv.col = 0;
  430. }
  431. find_neighboring_blocks(mb->mode_info_context, neighbors, mb_row, mb_col,
  432. mb_rows, mb_cols, mb->mode_info_stride);
  433. /* Interpolate MVs for the missing blocks from the surrounding
  434. * blocks which refer to the last frame. */
  435. interpolate_mvs(mb, neighbors, LAST_FRAME);
  436. mb->mode_info_context->mbmi.ref_frame = LAST_FRAME;
  437. mb->mode_info_context->mbmi.mode = SPLITMV;
  438. mb->mode_info_context->mbmi.uv_mode = DC_PRED;
  439. mb->mode_info_context->mbmi.partitioning = 3;
  440. mb->mode_info_context->mbmi.segment_id = 0;
  441. }