You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1225 lines
46KB

  1. /*
  2. * Error resilience / concealment
  3. *
  4. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * Libav is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * Error resilience / concealment.
  25. */
  26. #include <limits.h>
  27. #include "libavutil/internal.h"
  28. #include "avcodec.h"
  29. #include "error_resilience.h"
  30. #include "me_cmp.h"
  31. #include "mpegutils.h"
  32. #include "mpegvideo.h"
  33. #include "rectangle.h"
  34. #include "thread.h"
  35. #include "version.h"
  36. /**
  37. * @param stride the number of MVs to get to the next row
  38. * @param mv_step the number of MVs per row or column in a macroblock
  39. */
  40. static void set_mv_strides(ERContext *s, ptrdiff_t *mv_step, ptrdiff_t *stride)
  41. {
  42. if (s->avctx->codec_id == AV_CODEC_ID_H264) {
  43. assert(s->quarter_sample);
  44. *mv_step = 4;
  45. *stride = s->mb_width * 4;
  46. } else {
  47. *mv_step = 2;
  48. *stride = s->b8_stride;
  49. }
  50. }
  51. /**
  52. * Replace the current MB with a flat dc-only version.
  53. */
  54. static void put_dc(ERContext *s, uint8_t *dest_y, uint8_t *dest_cb,
  55. uint8_t *dest_cr, int mb_x, int mb_y)
  56. {
  57. int *linesize = s->cur_pic.f->linesize;
  58. int dc, dcu, dcv, y, i;
  59. for (i = 0; i < 4; i++) {
  60. dc = s->dc_val[0][mb_x * 2 + (i & 1) + (mb_y * 2 + (i >> 1)) * s->b8_stride];
  61. if (dc < 0)
  62. dc = 0;
  63. else if (dc > 2040)
  64. dc = 2040;
  65. for (y = 0; y < 8; y++) {
  66. int x;
  67. for (x = 0; x < 8; x++)
  68. dest_y[x + (i & 1) * 8 + (y + (i >> 1) * 8) * linesize[0]] = dc / 8;
  69. }
  70. }
  71. dcu = s->dc_val[1][mb_x + mb_y * s->mb_stride];
  72. dcv = s->dc_val[2][mb_x + mb_y * s->mb_stride];
  73. if (dcu < 0)
  74. dcu = 0;
  75. else if (dcu > 2040)
  76. dcu = 2040;
  77. if (dcv < 0)
  78. dcv = 0;
  79. else if (dcv > 2040)
  80. dcv = 2040;
  81. for (y = 0; y < 8; y++) {
  82. int x;
  83. for (x = 0; x < 8; x++) {
  84. dest_cb[x + y * linesize[1]] = dcu / 8;
  85. dest_cr[x + y * linesize[2]] = dcv / 8;
  86. }
  87. }
  88. }
  89. static void filter181(int16_t *data, int width, int height, ptrdiff_t stride)
  90. {
  91. int x, y;
  92. /* horizontal filter */
  93. for (y = 1; y < height - 1; y++) {
  94. int prev_dc = data[0 + y * stride];
  95. for (x = 1; x < width - 1; x++) {
  96. int dc;
  97. dc = -prev_dc +
  98. data[x + y * stride] * 8 -
  99. data[x + 1 + y * stride];
  100. dc = (dc * 10923 + 32768) >> 16;
  101. prev_dc = data[x + y * stride];
  102. data[x + y * stride] = dc;
  103. }
  104. }
  105. /* vertical filter */
  106. for (x = 1; x < width - 1; x++) {
  107. int prev_dc = data[x];
  108. for (y = 1; y < height - 1; y++) {
  109. int dc;
  110. dc = -prev_dc +
  111. data[x + y * stride] * 8 -
  112. data[x + (y + 1) * stride];
  113. dc = (dc * 10923 + 32768) >> 16;
  114. prev_dc = data[x + y * stride];
  115. data[x + y * stride] = dc;
  116. }
  117. }
  118. }
  119. /**
  120. * guess the dc of blocks which do not have an undamaged dc
  121. * @param w width in 8 pixel blocks
  122. * @param h height in 8 pixel blocks
  123. */
  124. static void guess_dc(ERContext *s, int16_t *dc, int w,
  125. int h, ptrdiff_t stride, int is_luma)
  126. {
  127. int b_x, b_y;
  128. for (b_y = 0; b_y < h; b_y++) {
  129. for (b_x = 0; b_x < w; b_x++) {
  130. int color[4] = { 1024, 1024, 1024, 1024 };
  131. int distance[4] = { 9999, 9999, 9999, 9999 };
  132. int mb_index, error, j;
  133. int64_t guess, weight_sum;
  134. mb_index = (b_x >> is_luma) + (b_y >> is_luma) * s->mb_stride;
  135. error = s->error_status_table[mb_index];
  136. if (IS_INTER(s->cur_pic.mb_type[mb_index]))
  137. continue; // inter
  138. if (!(error & ER_DC_ERROR))
  139. continue; // dc-ok
  140. /* right block */
  141. for (j = b_x + 1; j < w; j++) {
  142. int mb_index_j = (j >> is_luma) + (b_y >> is_luma) * s->mb_stride;
  143. int error_j = s->error_status_table[mb_index_j];
  144. int intra_j = IS_INTRA(s->cur_pic.mb_type[mb_index_j]);
  145. if (intra_j == 0 || !(error_j & ER_DC_ERROR)) {
  146. color[0] = dc[j + b_y * stride];
  147. distance[0] = j - b_x;
  148. break;
  149. }
  150. }
  151. /* left block */
  152. for (j = b_x - 1; j >= 0; j--) {
  153. int mb_index_j = (j >> is_luma) + (b_y >> is_luma) * s->mb_stride;
  154. int error_j = s->error_status_table[mb_index_j];
  155. int intra_j = IS_INTRA(s->cur_pic.mb_type[mb_index_j]);
  156. if (intra_j == 0 || !(error_j & ER_DC_ERROR)) {
  157. color[1] = dc[j + b_y * stride];
  158. distance[1] = b_x - j;
  159. break;
  160. }
  161. }
  162. /* bottom block */
  163. for (j = b_y + 1; j < h; j++) {
  164. int mb_index_j = (b_x >> is_luma) + (j >> is_luma) * s->mb_stride;
  165. int error_j = s->error_status_table[mb_index_j];
  166. int intra_j = IS_INTRA(s->cur_pic.mb_type[mb_index_j]);
  167. if (intra_j == 0 || !(error_j & ER_DC_ERROR)) {
  168. color[2] = dc[b_x + j * stride];
  169. distance[2] = j - b_y;
  170. break;
  171. }
  172. }
  173. /* top block */
  174. for (j = b_y - 1; j >= 0; j--) {
  175. int mb_index_j = (b_x >> is_luma) + (j >> is_luma) * s->mb_stride;
  176. int error_j = s->error_status_table[mb_index_j];
  177. int intra_j = IS_INTRA(s->cur_pic.mb_type[mb_index_j]);
  178. if (intra_j == 0 || !(error_j & ER_DC_ERROR)) {
  179. color[3] = dc[b_x + j * stride];
  180. distance[3] = b_y - j;
  181. break;
  182. }
  183. }
  184. weight_sum = 0;
  185. guess = 0;
  186. for (j = 0; j < 4; j++) {
  187. int64_t weight = 256 * 256 * 256 * 16 / distance[j];
  188. guess += weight * (int64_t) color[j];
  189. weight_sum += weight;
  190. }
  191. guess = (guess + weight_sum / 2) / weight_sum;
  192. dc[b_x + b_y * stride] = guess;
  193. }
  194. }
  195. }
  196. /**
  197. * simple horizontal deblocking filter used for error resilience
  198. * @param w width in 8 pixel blocks
  199. * @param h height in 8 pixel blocks
  200. */
  201. static void h_block_filter(ERContext *s, uint8_t *dst, int w,
  202. int h, ptrdiff_t stride, int is_luma)
  203. {
  204. int b_x, b_y;
  205. ptrdiff_t mvx_stride, mvy_stride;
  206. const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;
  207. set_mv_strides(s, &mvx_stride, &mvy_stride);
  208. mvx_stride >>= is_luma;
  209. mvy_stride *= mvx_stride;
  210. for (b_y = 0; b_y < h; b_y++) {
  211. for (b_x = 0; b_x < w - 1; b_x++) {
  212. int y;
  213. int left_status = s->error_status_table[( b_x >> is_luma) + (b_y >> is_luma) * s->mb_stride];
  214. int right_status = s->error_status_table[((b_x + 1) >> is_luma) + (b_y >> is_luma) * s->mb_stride];
  215. int left_intra = IS_INTRA(s->cur_pic.mb_type[( b_x >> is_luma) + (b_y >> is_luma) * s->mb_stride]);
  216. int right_intra = IS_INTRA(s->cur_pic.mb_type[((b_x + 1) >> is_luma) + (b_y >> is_luma) * s->mb_stride]);
  217. int left_damage = left_status & ER_MB_ERROR;
  218. int right_damage = right_status & ER_MB_ERROR;
  219. int offset = b_x * 8 + b_y * stride * 8;
  220. int16_t *left_mv = s->cur_pic.motion_val[0][mvy_stride * b_y + mvx_stride * b_x];
  221. int16_t *right_mv = s->cur_pic.motion_val[0][mvy_stride * b_y + mvx_stride * (b_x + 1)];
  222. if (!(left_damage || right_damage))
  223. continue; // both undamaged
  224. if ((!left_intra) && (!right_intra) &&
  225. FFABS(left_mv[0] - right_mv[0]) +
  226. FFABS(left_mv[1] + right_mv[1]) < 2)
  227. continue;
  228. for (y = 0; y < 8; y++) {
  229. int a, b, c, d;
  230. a = dst[offset + 7 + y * stride] - dst[offset + 6 + y * stride];
  231. b = dst[offset + 8 + y * stride] - dst[offset + 7 + y * stride];
  232. c = dst[offset + 9 + y * stride] - dst[offset + 8 + y * stride];
  233. d = FFABS(b) - ((FFABS(a) + FFABS(c) + 1) >> 1);
  234. d = FFMAX(d, 0);
  235. if (b < 0)
  236. d = -d;
  237. if (d == 0)
  238. continue;
  239. if (!(left_damage && right_damage))
  240. d = d * 16 / 9;
  241. if (left_damage) {
  242. dst[offset + 7 + y * stride] = cm[dst[offset + 7 + y * stride] + ((d * 7) >> 4)];
  243. dst[offset + 6 + y * stride] = cm[dst[offset + 6 + y * stride] + ((d * 5) >> 4)];
  244. dst[offset + 5 + y * stride] = cm[dst[offset + 5 + y * stride] + ((d * 3) >> 4)];
  245. dst[offset + 4 + y * stride] = cm[dst[offset + 4 + y * stride] + ((d * 1) >> 4)];
  246. }
  247. if (right_damage) {
  248. dst[offset + 8 + y * stride] = cm[dst[offset + 8 + y * stride] - ((d * 7) >> 4)];
  249. dst[offset + 9 + y * stride] = cm[dst[offset + 9 + y * stride] - ((d * 5) >> 4)];
  250. dst[offset + 10+ y * stride] = cm[dst[offset + 10 + y * stride] - ((d * 3) >> 4)];
  251. dst[offset + 11+ y * stride] = cm[dst[offset + 11 + y * stride] - ((d * 1) >> 4)];
  252. }
  253. }
  254. }
  255. }
  256. }
  257. /**
  258. * simple vertical deblocking filter used for error resilience
  259. * @param w width in 8 pixel blocks
  260. * @param h height in 8 pixel blocks
  261. */
  262. static void v_block_filter(ERContext *s, uint8_t *dst, int w, int h,
  263. ptrdiff_t stride, int is_luma)
  264. {
  265. int b_x, b_y;
  266. ptrdiff_t mvx_stride, mvy_stride;
  267. const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;
  268. set_mv_strides(s, &mvx_stride, &mvy_stride);
  269. mvx_stride >>= is_luma;
  270. mvy_stride *= mvx_stride;
  271. for (b_y = 0; b_y < h - 1; b_y++) {
  272. for (b_x = 0; b_x < w; b_x++) {
  273. int x;
  274. int top_status = s->error_status_table[(b_x >> is_luma) + (b_y >> is_luma) * s->mb_stride];
  275. int bottom_status = s->error_status_table[(b_x >> is_luma) + ((b_y + 1) >> is_luma) * s->mb_stride];
  276. int top_intra = IS_INTRA(s->cur_pic.mb_type[(b_x >> is_luma) + ( b_y >> is_luma) * s->mb_stride]);
  277. int bottom_intra = IS_INTRA(s->cur_pic.mb_type[(b_x >> is_luma) + ((b_y + 1) >> is_luma) * s->mb_stride]);
  278. int top_damage = top_status & ER_MB_ERROR;
  279. int bottom_damage = bottom_status & ER_MB_ERROR;
  280. int offset = b_x * 8 + b_y * stride * 8;
  281. int16_t *top_mv = s->cur_pic.motion_val[0][mvy_stride * b_y + mvx_stride * b_x];
  282. int16_t *bottom_mv = s->cur_pic.motion_val[0][mvy_stride * (b_y + 1) + mvx_stride * b_x];
  283. if (!(top_damage || bottom_damage))
  284. continue; // both undamaged
  285. if ((!top_intra) && (!bottom_intra) &&
  286. FFABS(top_mv[0] - bottom_mv[0]) +
  287. FFABS(top_mv[1] + bottom_mv[1]) < 2)
  288. continue;
  289. for (x = 0; x < 8; x++) {
  290. int a, b, c, d;
  291. a = dst[offset + x + 7 * stride] - dst[offset + x + 6 * stride];
  292. b = dst[offset + x + 8 * stride] - dst[offset + x + 7 * stride];
  293. c = dst[offset + x + 9 * stride] - dst[offset + x + 8 * stride];
  294. d = FFABS(b) - ((FFABS(a) + FFABS(c) + 1) >> 1);
  295. d = FFMAX(d, 0);
  296. if (b < 0)
  297. d = -d;
  298. if (d == 0)
  299. continue;
  300. if (!(top_damage && bottom_damage))
  301. d = d * 16 / 9;
  302. if (top_damage) {
  303. dst[offset + x + 7 * stride] = cm[dst[offset + x + 7 * stride] + ((d * 7) >> 4)];
  304. dst[offset + x + 6 * stride] = cm[dst[offset + x + 6 * stride] + ((d * 5) >> 4)];
  305. dst[offset + x + 5 * stride] = cm[dst[offset + x + 5 * stride] + ((d * 3) >> 4)];
  306. dst[offset + x + 4 * stride] = cm[dst[offset + x + 4 * stride] + ((d * 1) >> 4)];
  307. }
  308. if (bottom_damage) {
  309. dst[offset + x + 8 * stride] = cm[dst[offset + x + 8 * stride] - ((d * 7) >> 4)];
  310. dst[offset + x + 9 * stride] = cm[dst[offset + x + 9 * stride] - ((d * 5) >> 4)];
  311. dst[offset + x + 10 * stride] = cm[dst[offset + x + 10 * stride] - ((d * 3) >> 4)];
  312. dst[offset + x + 11 * stride] = cm[dst[offset + x + 11 * stride] - ((d * 1) >> 4)];
  313. }
  314. }
  315. }
  316. }
  317. }
  318. static void guess_mv(ERContext *s)
  319. {
  320. uint8_t *fixed = s->er_temp_buffer;
  321. #define MV_FROZEN 3
  322. #define MV_CHANGED 2
  323. #define MV_UNCHANGED 1
  324. const ptrdiff_t mb_stride = s->mb_stride;
  325. const int mb_width = s->mb_width;
  326. const int mb_height = s->mb_height;
  327. int i, depth, num_avail;
  328. int mb_x, mb_y;
  329. ptrdiff_t mot_step, mot_stride;
  330. set_mv_strides(s, &mot_step, &mot_stride);
  331. num_avail = 0;
  332. for (i = 0; i < s->mb_num; i++) {
  333. const int mb_xy = s->mb_index2xy[i];
  334. int f = 0;
  335. int error = s->error_status_table[mb_xy];
  336. if (IS_INTRA(s->cur_pic.mb_type[mb_xy]))
  337. f = MV_FROZEN; // intra // FIXME check
  338. if (!(error & ER_MV_ERROR))
  339. f = MV_FROZEN; // inter with undamaged MV
  340. fixed[mb_xy] = f;
  341. if (f == MV_FROZEN)
  342. num_avail++;
  343. }
  344. if ((!(s->avctx->error_concealment&FF_EC_GUESS_MVS)) ||
  345. num_avail <= mb_width / 2) {
  346. for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
  347. for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
  348. const int mb_xy = mb_x + mb_y * s->mb_stride;
  349. int mv_dir = (s->last_pic.f && s->last_pic.f->data[0]) ? MV_DIR_FORWARD : MV_DIR_BACKWARD;
  350. if (IS_INTRA(s->cur_pic.mb_type[mb_xy]))
  351. continue;
  352. if (!(s->error_status_table[mb_xy] & ER_MV_ERROR))
  353. continue;
  354. s->mv[0][0][0] = 0;
  355. s->mv[0][0][1] = 0;
  356. s->decode_mb(s->opaque, 0, mv_dir, MV_TYPE_16X16, &s->mv,
  357. mb_x, mb_y, 0, 0);
  358. }
  359. }
  360. return;
  361. }
  362. for (depth = 0; ; depth++) {
  363. int changed, pass, none_left;
  364. none_left = 1;
  365. changed = 1;
  366. for (pass = 0; (changed || pass < 2) && pass < 10; pass++) {
  367. int mb_x, mb_y;
  368. int score_sum = 0;
  369. changed = 0;
  370. for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
  371. for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
  372. const int mb_xy = mb_x + mb_y * s->mb_stride;
  373. int mv_predictor[8][2] = { { 0 } };
  374. int ref[8] = { 0 };
  375. int pred_count = 0;
  376. int j;
  377. int best_score = 256 * 256 * 256 * 64;
  378. int best_pred = 0;
  379. const int mot_index = (mb_x + mb_y * mot_stride) * mot_step;
  380. int prev_x = 0, prev_y = 0, prev_ref = 0;
  381. if ((mb_x ^ mb_y ^ pass) & 1)
  382. continue;
  383. if (fixed[mb_xy] == MV_FROZEN)
  384. continue;
  385. j = 0;
  386. if (mb_x > 0 && fixed[mb_xy - 1] == MV_FROZEN)
  387. j = 1;
  388. if (mb_x + 1 < mb_width && fixed[mb_xy + 1] == MV_FROZEN)
  389. j = 1;
  390. if (mb_y > 0 && fixed[mb_xy - mb_stride] == MV_FROZEN)
  391. j = 1;
  392. if (mb_y + 1 < mb_height && fixed[mb_xy + mb_stride] == MV_FROZEN)
  393. j = 1;
  394. if (j == 0)
  395. continue;
  396. j = 0;
  397. if (mb_x > 0 && fixed[mb_xy - 1 ] == MV_CHANGED)
  398. j = 1;
  399. if (mb_x + 1 < mb_width && fixed[mb_xy + 1 ] == MV_CHANGED)
  400. j = 1;
  401. if (mb_y > 0 && fixed[mb_xy - mb_stride] == MV_CHANGED)
  402. j = 1;
  403. if (mb_y + 1 < mb_height && fixed[mb_xy + mb_stride] == MV_CHANGED)
  404. j = 1;
  405. if (j == 0 && pass > 1)
  406. continue;
  407. none_left = 0;
  408. if (mb_x > 0 && fixed[mb_xy - 1]) {
  409. mv_predictor[pred_count][0] =
  410. s->cur_pic.motion_val[0][mot_index - mot_step][0];
  411. mv_predictor[pred_count][1] =
  412. s->cur_pic.motion_val[0][mot_index - mot_step][1];
  413. ref[pred_count] =
  414. s->cur_pic.ref_index[0][4 * (mb_xy - 1)];
  415. pred_count++;
  416. }
  417. if (mb_x + 1 < mb_width && fixed[mb_xy + 1]) {
  418. mv_predictor[pred_count][0] =
  419. s->cur_pic.motion_val[0][mot_index + mot_step][0];
  420. mv_predictor[pred_count][1] =
  421. s->cur_pic.motion_val[0][mot_index + mot_step][1];
  422. ref[pred_count] =
  423. s->cur_pic.ref_index[0][4 * (mb_xy + 1)];
  424. pred_count++;
  425. }
  426. if (mb_y > 0 && fixed[mb_xy - mb_stride]) {
  427. mv_predictor[pred_count][0] =
  428. s->cur_pic.motion_val[0][mot_index - mot_stride * mot_step][0];
  429. mv_predictor[pred_count][1] =
  430. s->cur_pic.motion_val[0][mot_index - mot_stride * mot_step][1];
  431. ref[pred_count] =
  432. s->cur_pic.ref_index[0][4 * (mb_xy - s->mb_stride)];
  433. pred_count++;
  434. }
  435. if (mb_y + 1<mb_height && fixed[mb_xy + mb_stride]) {
  436. mv_predictor[pred_count][0] =
  437. s->cur_pic.motion_val[0][mot_index + mot_stride * mot_step][0];
  438. mv_predictor[pred_count][1] =
  439. s->cur_pic.motion_val[0][mot_index + mot_stride * mot_step][1];
  440. ref[pred_count] =
  441. s->cur_pic.ref_index[0][4 * (mb_xy + s->mb_stride)];
  442. pred_count++;
  443. }
  444. if (pred_count == 0)
  445. continue;
  446. if (pred_count > 1) {
  447. int sum_x = 0, sum_y = 0, sum_r = 0;
  448. int max_x, max_y, min_x, min_y, max_r, min_r;
  449. for (j = 0; j < pred_count; j++) {
  450. sum_x += mv_predictor[j][0];
  451. sum_y += mv_predictor[j][1];
  452. sum_r += ref[j];
  453. if (j && ref[j] != ref[j - 1])
  454. goto skip_mean_and_median;
  455. }
  456. /* mean */
  457. mv_predictor[pred_count][0] = sum_x / j;
  458. mv_predictor[pred_count][1] = sum_y / j;
  459. ref[pred_count] = sum_r / j;
  460. /* median */
  461. if (pred_count >= 3) {
  462. min_y = min_x = min_r = 99999;
  463. max_y = max_x = max_r = -99999;
  464. } else {
  465. min_x = min_y = max_x = max_y = min_r = max_r = 0;
  466. }
  467. for (j = 0; j < pred_count; j++) {
  468. max_x = FFMAX(max_x, mv_predictor[j][0]);
  469. max_y = FFMAX(max_y, mv_predictor[j][1]);
  470. max_r = FFMAX(max_r, ref[j]);
  471. min_x = FFMIN(min_x, mv_predictor[j][0]);
  472. min_y = FFMIN(min_y, mv_predictor[j][1]);
  473. min_r = FFMIN(min_r, ref[j]);
  474. }
  475. mv_predictor[pred_count + 1][0] = sum_x - max_x - min_x;
  476. mv_predictor[pred_count + 1][1] = sum_y - max_y - min_y;
  477. ref[pred_count + 1] = sum_r - max_r - min_r;
  478. if (pred_count == 4) {
  479. mv_predictor[pred_count + 1][0] /= 2;
  480. mv_predictor[pred_count + 1][1] /= 2;
  481. ref[pred_count + 1] /= 2;
  482. }
  483. pred_count += 2;
  484. }
  485. skip_mean_and_median:
  486. /* zero MV */
  487. pred_count++;
  488. if (!fixed[mb_xy]) {
  489. if (s->avctx->codec_id == AV_CODEC_ID_H264) {
  490. // FIXME
  491. } else {
  492. ff_thread_await_progress(s->last_pic.tf,
  493. mb_y, 0);
  494. }
  495. if (!s->last_pic.motion_val[0] ||
  496. !s->last_pic.ref_index[0])
  497. goto skip_last_mv;
  498. prev_x = s->last_pic.motion_val[0][mot_index][0];
  499. prev_y = s->last_pic.motion_val[0][mot_index][1];
  500. prev_ref = s->last_pic.ref_index[0][4 * mb_xy];
  501. } else {
  502. prev_x = s->cur_pic.motion_val[0][mot_index][0];
  503. prev_y = s->cur_pic.motion_val[0][mot_index][1];
  504. prev_ref = s->cur_pic.ref_index[0][4 * mb_xy];
  505. }
  506. /* last MV */
  507. mv_predictor[pred_count][0] = prev_x;
  508. mv_predictor[pred_count][1] = prev_y;
  509. ref[pred_count] = prev_ref;
  510. pred_count++;
  511. skip_last_mv:
  512. for (j = 0; j < pred_count; j++) {
  513. int *linesize = s->cur_pic.f->linesize;
  514. int score = 0;
  515. uint8_t *src = s->cur_pic.f->data[0] +
  516. mb_x * 16 + mb_y * 16 * linesize[0];
  517. s->cur_pic.motion_val[0][mot_index][0] =
  518. s->mv[0][0][0] = mv_predictor[j][0];
  519. s->cur_pic.motion_val[0][mot_index][1] =
  520. s->mv[0][0][1] = mv_predictor[j][1];
  521. // predictor intra or otherwise not available
  522. if (ref[j] < 0)
  523. continue;
  524. s->decode_mb(s->opaque, ref[j], MV_DIR_FORWARD,
  525. MV_TYPE_16X16, &s->mv, mb_x, mb_y, 0, 0);
  526. if (mb_x > 0 && fixed[mb_xy - 1]) {
  527. int k;
  528. for (k = 0; k < 16; k++)
  529. score += FFABS(src[k * linesize[0] - 1] -
  530. src[k * linesize[0]]);
  531. }
  532. if (mb_x + 1 < mb_width && fixed[mb_xy + 1]) {
  533. int k;
  534. for (k = 0; k < 16; k++)
  535. score += FFABS(src[k * linesize[0] + 15] -
  536. src[k * linesize[0] + 16]);
  537. }
  538. if (mb_y > 0 && fixed[mb_xy - mb_stride]) {
  539. int k;
  540. for (k = 0; k < 16; k++)
  541. score += FFABS(src[k - linesize[0]] - src[k]);
  542. }
  543. if (mb_y + 1 < mb_height && fixed[mb_xy + mb_stride]) {
  544. int k;
  545. for (k = 0; k < 16; k++)
  546. score += FFABS(src[k + linesize[0] * 15] -
  547. src[k + linesize[0] * 16]);
  548. }
  549. if (score <= best_score) { // <= will favor the last MV
  550. best_score = score;
  551. best_pred = j;
  552. }
  553. }
  554. score_sum += best_score;
  555. s->mv[0][0][0] = mv_predictor[best_pred][0];
  556. s->mv[0][0][1] = mv_predictor[best_pred][1];
  557. for (i = 0; i < mot_step; i++)
  558. for (j = 0; j < mot_step; j++) {
  559. s->cur_pic.motion_val[0][mot_index + i + j * mot_stride][0] = s->mv[0][0][0];
  560. s->cur_pic.motion_val[0][mot_index + i + j * mot_stride][1] = s->mv[0][0][1];
  561. }
  562. s->decode_mb(s->opaque, ref[best_pred], MV_DIR_FORWARD,
  563. MV_TYPE_16X16, &s->mv, mb_x, mb_y, 0, 0);
  564. if (s->mv[0][0][0] != prev_x || s->mv[0][0][1] != prev_y) {
  565. fixed[mb_xy] = MV_CHANGED;
  566. changed++;
  567. } else
  568. fixed[mb_xy] = MV_UNCHANGED;
  569. }
  570. }
  571. }
  572. if (none_left)
  573. return;
  574. for (i = 0; i < s->mb_num; i++) {
  575. int mb_xy = s->mb_index2xy[i];
  576. if (fixed[mb_xy])
  577. fixed[mb_xy] = MV_FROZEN;
  578. }
  579. }
  580. }
  581. static int is_intra_more_likely(ERContext *s)
  582. {
  583. int is_intra_likely, i, j, undamaged_count, skip_amount, mb_x, mb_y;
  584. if (!s->last_pic.f || !s->last_pic.f->data[0])
  585. return 1; // no previous frame available -> use spatial prediction
  586. undamaged_count = 0;
  587. for (i = 0; i < s->mb_num; i++) {
  588. const int mb_xy = s->mb_index2xy[i];
  589. const int error = s->error_status_table[mb_xy];
  590. if (!((error & ER_DC_ERROR) && (error & ER_MV_ERROR)))
  591. undamaged_count++;
  592. }
  593. if (s->avctx->codec_id == AV_CODEC_ID_H264 && s->ref_count <= 0)
  594. return 1;
  595. if (undamaged_count < 5)
  596. return 0; // almost all MBs damaged -> use temporal prediction
  597. #if FF_API_XVMC
  598. FF_DISABLE_DEPRECATION_WARNINGS
  599. // prevent dsp.sad() check, that requires access to the image
  600. if (CONFIG_MPEG_XVMC_DECODER &&
  601. s->avctx->xvmc_acceleration &&
  602. s->cur_pic.f->pict_type == AV_PICTURE_TYPE_I)
  603. return 1;
  604. FF_ENABLE_DEPRECATION_WARNINGS
  605. #endif /* FF_API_XVMC */
  606. skip_amount = FFMAX(undamaged_count / 50, 1); // check only up to 50 MBs
  607. is_intra_likely = 0;
  608. j = 0;
  609. for (mb_y = 0; mb_y < s->mb_height - 1; mb_y++) {
  610. for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
  611. int error;
  612. const int mb_xy = mb_x + mb_y * s->mb_stride;
  613. error = s->error_status_table[mb_xy];
  614. if ((error & ER_DC_ERROR) && (error & ER_MV_ERROR))
  615. continue; // skip damaged
  616. j++;
  617. // skip a few to speed things up
  618. if ((j % skip_amount) != 0)
  619. continue;
  620. if (s->cur_pic.f->pict_type == AV_PICTURE_TYPE_I) {
  621. int *linesize = s->cur_pic.f->linesize;
  622. uint8_t *mb_ptr = s->cur_pic.f->data[0] +
  623. mb_x * 16 + mb_y * 16 * linesize[0];
  624. uint8_t *last_mb_ptr = s->last_pic.f->data[0] +
  625. mb_x * 16 + mb_y * 16 * linesize[0];
  626. if (s->avctx->codec_id == AV_CODEC_ID_H264) {
  627. // FIXME
  628. } else {
  629. ff_thread_await_progress(s->last_pic.tf, mb_y, 0);
  630. }
  631. is_intra_likely += s->mecc.sad[0](NULL, last_mb_ptr, mb_ptr,
  632. linesize[0], 16);
  633. is_intra_likely -= s->mecc.sad[0](NULL, last_mb_ptr,
  634. last_mb_ptr + linesize[0] * 16,
  635. linesize[0], 16);
  636. } else {
  637. if (IS_INTRA(s->cur_pic.mb_type[mb_xy]))
  638. is_intra_likely++;
  639. else
  640. is_intra_likely--;
  641. }
  642. }
  643. }
  644. return is_intra_likely > 0;
  645. }
  646. void ff_er_frame_start(ERContext *s)
  647. {
  648. if (!s->avctx->error_concealment)
  649. return;
  650. if (!s->mecc_inited) {
  651. ff_me_cmp_init(&s->mecc, s->avctx);
  652. s->mecc_inited = 1;
  653. }
  654. memset(s->error_status_table, ER_MB_ERROR | VP_START | ER_MB_END,
  655. s->mb_stride * s->mb_height * sizeof(uint8_t));
  656. s->error_count = 3 * s->mb_num;
  657. s->error_occurred = 0;
  658. }
  659. /**
  660. * Add a slice.
  661. * @param endx x component of the last macroblock, can be -1
  662. * for the last of the previous line
  663. * @param status the status at the end (ER_MV_END, ER_AC_ERROR, ...), it is
  664. * assumed that no earlier end or error of the same type occurred
  665. */
  666. void ff_er_add_slice(ERContext *s, int startx, int starty,
  667. int endx, int endy, int status)
  668. {
  669. const int start_i = av_clip(startx + starty * s->mb_width, 0, s->mb_num - 1);
  670. const int end_i = av_clip(endx + endy * s->mb_width, 0, s->mb_num);
  671. const int start_xy = s->mb_index2xy[start_i];
  672. const int end_xy = s->mb_index2xy[end_i];
  673. int mask = -1;
  674. if (s->avctx->hwaccel)
  675. return;
  676. if (start_i > end_i || start_xy > end_xy) {
  677. av_log(s->avctx, AV_LOG_ERROR,
  678. "internal error, slice end before start\n");
  679. return;
  680. }
  681. if (!s->avctx->error_concealment)
  682. return;
  683. mask &= ~VP_START;
  684. if (status & (ER_AC_ERROR | ER_AC_END)) {
  685. mask &= ~(ER_AC_ERROR | ER_AC_END);
  686. s->error_count -= end_i - start_i + 1;
  687. }
  688. if (status & (ER_DC_ERROR | ER_DC_END)) {
  689. mask &= ~(ER_DC_ERROR | ER_DC_END);
  690. s->error_count -= end_i - start_i + 1;
  691. }
  692. if (status & (ER_MV_ERROR | ER_MV_END)) {
  693. mask &= ~(ER_MV_ERROR | ER_MV_END);
  694. s->error_count -= end_i - start_i + 1;
  695. }
  696. if (status & ER_MB_ERROR) {
  697. s->error_occurred = 1;
  698. s->error_count = INT_MAX;
  699. }
  700. if (mask == ~0x7F) {
  701. memset(&s->error_status_table[start_xy], 0,
  702. (end_xy - start_xy) * sizeof(uint8_t));
  703. } else {
  704. int i;
  705. for (i = start_xy; i < end_xy; i++)
  706. s->error_status_table[i] &= mask;
  707. }
  708. if (end_i == s->mb_num)
  709. s->error_count = INT_MAX;
  710. else {
  711. s->error_status_table[end_xy] &= mask;
  712. s->error_status_table[end_xy] |= status;
  713. }
  714. s->error_status_table[start_xy] |= VP_START;
  715. if (start_xy > 0 && s->avctx->thread_count <= 1 &&
  716. s->avctx->skip_top * s->mb_width < start_i) {
  717. int prev_status = s->error_status_table[s->mb_index2xy[start_i - 1]];
  718. prev_status &= ~ VP_START;
  719. if (prev_status != (ER_MV_END | ER_DC_END | ER_AC_END))
  720. s->error_count = INT_MAX;
  721. }
  722. }
  723. void ff_er_frame_end(ERContext *s)
  724. {
  725. int *linesize = s->cur_pic.f->linesize;
  726. int i, mb_x, mb_y, error, error_type, dc_error, mv_error, ac_error;
  727. int distance;
  728. int threshold_part[4] = { 100, 100, 100 };
  729. int threshold = 50;
  730. int is_intra_likely;
  731. /* We do not support ER of field pictures yet,
  732. * though it should not crash if enabled. */
  733. if (!s->avctx->error_concealment || s->error_count == 0 ||
  734. s->avctx->hwaccel ||
  735. !s->cur_pic.f ||
  736. s->cur_pic.field_picture ||
  737. s->error_count == 3 * s->mb_width *
  738. (s->avctx->skip_top + s->avctx->skip_bottom)) {
  739. return;
  740. };
  741. if (!s->cur_pic.motion_val[0] || !s->cur_pic.ref_index[0]) {
  742. av_log(s->avctx, AV_LOG_ERROR, "MVs not available, ER not possible.\n");
  743. return;
  744. }
  745. if (s->avctx->debug & FF_DEBUG_ER) {
  746. for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
  747. for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
  748. int status = s->error_status_table[mb_x + mb_y * s->mb_stride];
  749. av_log(s->avctx, AV_LOG_DEBUG, "%2X ", status);
  750. }
  751. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  752. }
  753. }
  754. /* handle overlapping slices */
  755. for (error_type = 1; error_type <= 3; error_type++) {
  756. int end_ok = 0;
  757. for (i = s->mb_num - 1; i >= 0; i--) {
  758. const int mb_xy = s->mb_index2xy[i];
  759. int error = s->error_status_table[mb_xy];
  760. if (error & (1 << error_type))
  761. end_ok = 1;
  762. if (error & (8 << error_type))
  763. end_ok = 1;
  764. if (!end_ok)
  765. s->error_status_table[mb_xy] |= 1 << error_type;
  766. if (error & VP_START)
  767. end_ok = 0;
  768. }
  769. }
  770. /* handle slices with partitions of different length */
  771. if (s->partitioned_frame) {
  772. int end_ok = 0;
  773. for (i = s->mb_num - 1; i >= 0; i--) {
  774. const int mb_xy = s->mb_index2xy[i];
  775. int error = s->error_status_table[mb_xy];
  776. if (error & ER_AC_END)
  777. end_ok = 0;
  778. if ((error & ER_MV_END) ||
  779. (error & ER_DC_END) ||
  780. (error & ER_AC_ERROR))
  781. end_ok = 1;
  782. if (!end_ok)
  783. s->error_status_table[mb_xy]|= ER_AC_ERROR;
  784. if (error & VP_START)
  785. end_ok = 0;
  786. }
  787. }
  788. /* handle missing slices */
  789. if (s->avctx->err_recognition & AV_EF_EXPLODE) {
  790. int end_ok = 1;
  791. // FIXME + 100 hack
  792. for (i = s->mb_num - 2; i >= s->mb_width + 100; i--) {
  793. const int mb_xy = s->mb_index2xy[i];
  794. int error1 = s->error_status_table[mb_xy];
  795. int error2 = s->error_status_table[s->mb_index2xy[i + 1]];
  796. if (error1 & VP_START)
  797. end_ok = 1;
  798. if (error2 == (VP_START | ER_MB_ERROR | ER_MB_END) &&
  799. error1 != (VP_START | ER_MB_ERROR | ER_MB_END) &&
  800. ((error1 & ER_AC_END) || (error1 & ER_DC_END) ||
  801. (error1 & ER_MV_END))) {
  802. // end & uninit
  803. end_ok = 0;
  804. }
  805. if (!end_ok)
  806. s->error_status_table[mb_xy] |= ER_MB_ERROR;
  807. }
  808. }
  809. /* backward mark errors */
  810. distance = 9999999;
  811. for (error_type = 1; error_type <= 3; error_type++) {
  812. for (i = s->mb_num - 1; i >= 0; i--) {
  813. const int mb_xy = s->mb_index2xy[i];
  814. int error = s->error_status_table[mb_xy];
  815. if (s->mbskip_table && !s->mbskip_table[mb_xy]) // FIXME partition specific
  816. distance++;
  817. if (error & (1 << error_type))
  818. distance = 0;
  819. if (s->partitioned_frame) {
  820. if (distance < threshold_part[error_type - 1])
  821. s->error_status_table[mb_xy] |= 1 << error_type;
  822. } else {
  823. if (distance < threshold)
  824. s->error_status_table[mb_xy] |= 1 << error_type;
  825. }
  826. if (error & VP_START)
  827. distance = 9999999;
  828. }
  829. }
  830. /* forward mark errors */
  831. error = 0;
  832. for (i = 0; i < s->mb_num; i++) {
  833. const int mb_xy = s->mb_index2xy[i];
  834. int old_error = s->error_status_table[mb_xy];
  835. if (old_error & VP_START) {
  836. error = old_error & ER_MB_ERROR;
  837. } else {
  838. error |= old_error & ER_MB_ERROR;
  839. s->error_status_table[mb_xy] |= error;
  840. }
  841. }
  842. /* handle not partitioned case */
  843. if (!s->partitioned_frame) {
  844. for (i = 0; i < s->mb_num; i++) {
  845. const int mb_xy = s->mb_index2xy[i];
  846. error = s->error_status_table[mb_xy];
  847. if (error & ER_MB_ERROR)
  848. error |= ER_MB_ERROR;
  849. s->error_status_table[mb_xy] = error;
  850. }
  851. }
  852. dc_error = ac_error = mv_error = 0;
  853. for (i = 0; i < s->mb_num; i++) {
  854. const int mb_xy = s->mb_index2xy[i];
  855. error = s->error_status_table[mb_xy];
  856. if (error & ER_DC_ERROR)
  857. dc_error++;
  858. if (error & ER_AC_ERROR)
  859. ac_error++;
  860. if (error & ER_MV_ERROR)
  861. mv_error++;
  862. }
  863. av_log(s->avctx, AV_LOG_INFO, "concealing %d DC, %d AC, %d MV errors\n",
  864. dc_error, ac_error, mv_error);
  865. is_intra_likely = is_intra_more_likely(s);
  866. /* set unknown mb-type to most likely */
  867. for (i = 0; i < s->mb_num; i++) {
  868. const int mb_xy = s->mb_index2xy[i];
  869. error = s->error_status_table[mb_xy];
  870. if (!((error & ER_DC_ERROR) && (error & ER_MV_ERROR)))
  871. continue;
  872. if (is_intra_likely)
  873. s->cur_pic.mb_type[mb_xy] = MB_TYPE_INTRA4x4;
  874. else
  875. s->cur_pic.mb_type[mb_xy] = MB_TYPE_16x16 | MB_TYPE_L0;
  876. }
  877. // change inter to intra blocks if no reference frames are available
  878. if (!(s->last_pic.f && s->last_pic.f->data[0]) &&
  879. !(s->next_pic.f && s->next_pic.f->data[0]))
  880. for (i = 0; i < s->mb_num; i++) {
  881. const int mb_xy = s->mb_index2xy[i];
  882. if (!IS_INTRA(s->cur_pic.mb_type[mb_xy]))
  883. s->cur_pic.mb_type[mb_xy] = MB_TYPE_INTRA4x4;
  884. }
  885. /* handle inter blocks with damaged AC */
  886. for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
  887. for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
  888. const int mb_xy = mb_x + mb_y * s->mb_stride;
  889. const int mb_type = s->cur_pic.mb_type[mb_xy];
  890. const int dir = !(s->last_pic.f && s->last_pic.f->data[0]);
  891. const int mv_dir = dir ? MV_DIR_BACKWARD : MV_DIR_FORWARD;
  892. int mv_type;
  893. error = s->error_status_table[mb_xy];
  894. if (IS_INTRA(mb_type))
  895. continue; // intra
  896. if (error & ER_MV_ERROR)
  897. continue; // inter with damaged MV
  898. if (!(error & ER_AC_ERROR))
  899. continue; // undamaged inter
  900. if (IS_8X8(mb_type)) {
  901. int mb_index = mb_x * 2 + mb_y * 2 * s->b8_stride;
  902. int j;
  903. mv_type = MV_TYPE_8X8;
  904. for (j = 0; j < 4; j++) {
  905. s->mv[0][j][0] = s->cur_pic.motion_val[dir][mb_index + (j & 1) + (j >> 1) * s->b8_stride][0];
  906. s->mv[0][j][1] = s->cur_pic.motion_val[dir][mb_index + (j & 1) + (j >> 1) * s->b8_stride][1];
  907. }
  908. } else {
  909. mv_type = MV_TYPE_16X16;
  910. s->mv[0][0][0] = s->cur_pic.motion_val[dir][mb_x * 2 + mb_y * 2 * s->b8_stride][0];
  911. s->mv[0][0][1] = s->cur_pic.motion_val[dir][mb_x * 2 + mb_y * 2 * s->b8_stride][1];
  912. }
  913. s->decode_mb(s->opaque, 0 /* FIXME H.264 partitioned slices need this set */,
  914. mv_dir, mv_type, &s->mv, mb_x, mb_y, 0, 0);
  915. }
  916. }
  917. /* guess MVs */
  918. if (s->cur_pic.f->pict_type == AV_PICTURE_TYPE_B) {
  919. for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
  920. for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
  921. int xy = mb_x * 2 + mb_y * 2 * s->b8_stride;
  922. const int mb_xy = mb_x + mb_y * s->mb_stride;
  923. const int mb_type = s->cur_pic.mb_type[mb_xy];
  924. int mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  925. error = s->error_status_table[mb_xy];
  926. if (IS_INTRA(mb_type))
  927. continue;
  928. if (!(error & ER_MV_ERROR))
  929. continue; // inter with undamaged MV
  930. if (!(error & ER_AC_ERROR))
  931. continue; // undamaged inter
  932. if (!(s->last_pic.f && s->last_pic.f->data[0]))
  933. mv_dir &= ~MV_DIR_FORWARD;
  934. if (!(s->next_pic.f && s->next_pic.f->data[0]))
  935. mv_dir &= ~MV_DIR_BACKWARD;
  936. if (s->pp_time) {
  937. int time_pp = s->pp_time;
  938. int time_pb = s->pb_time;
  939. ff_thread_await_progress(s->next_pic.tf, mb_y, 0);
  940. s->mv[0][0][0] = s->next_pic.motion_val[0][xy][0] * time_pb / time_pp;
  941. s->mv[0][0][1] = s->next_pic.motion_val[0][xy][1] * time_pb / time_pp;
  942. s->mv[1][0][0] = s->next_pic.motion_val[0][xy][0] * (time_pb - time_pp) / time_pp;
  943. s->mv[1][0][1] = s->next_pic.motion_val[0][xy][1] * (time_pb - time_pp) / time_pp;
  944. } else {
  945. s->mv[0][0][0] = 0;
  946. s->mv[0][0][1] = 0;
  947. s->mv[1][0][0] = 0;
  948. s->mv[1][0][1] = 0;
  949. }
  950. s->decode_mb(s->opaque, 0, mv_dir, MV_TYPE_16X16, &s->mv,
  951. mb_x, mb_y, 0, 0);
  952. }
  953. }
  954. } else
  955. guess_mv(s);
  956. #if FF_API_XVMC
  957. FF_DISABLE_DEPRECATION_WARNINGS
  958. /* the filters below are not XvMC compatible, skip them */
  959. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
  960. goto ec_clean;
  961. FF_ENABLE_DEPRECATION_WARNINGS
  962. #endif /* FF_API_XVMC */
  963. /* fill DC for inter blocks */
  964. for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
  965. for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
  966. int dc, dcu, dcv, y, n;
  967. int16_t *dc_ptr;
  968. uint8_t *dest_y, *dest_cb, *dest_cr;
  969. const int mb_xy = mb_x + mb_y * s->mb_stride;
  970. const int mb_type = s->cur_pic.mb_type[mb_xy];
  971. error = s->error_status_table[mb_xy];
  972. if (IS_INTRA(mb_type) && s->partitioned_frame)
  973. continue;
  974. // if (error & ER_MV_ERROR)
  975. // continue; // inter data damaged FIXME is this good?
  976. dest_y = s->cur_pic.f->data[0] + mb_x * 16 + mb_y * 16 * linesize[0];
  977. dest_cb = s->cur_pic.f->data[1] + mb_x * 8 + mb_y * 8 * linesize[1];
  978. dest_cr = s->cur_pic.f->data[2] + mb_x * 8 + mb_y * 8 * linesize[2];
  979. dc_ptr = &s->dc_val[0][mb_x * 2 + mb_y * 2 * s->b8_stride];
  980. for (n = 0; n < 4; n++) {
  981. dc = 0;
  982. for (y = 0; y < 8; y++) {
  983. int x;
  984. for (x = 0; x < 8; x++)
  985. dc += dest_y[x + (n & 1) * 8 +
  986. (y + (n >> 1) * 8) * linesize[0]];
  987. }
  988. dc_ptr[(n & 1) + (n >> 1) * s->b8_stride] = (dc + 4) >> 3;
  989. }
  990. dcu = dcv = 0;
  991. for (y = 0; y < 8; y++) {
  992. int x;
  993. for (x = 0; x < 8; x++) {
  994. dcu += dest_cb[x + y * linesize[1]];
  995. dcv += dest_cr[x + y * linesize[2]];
  996. }
  997. }
  998. s->dc_val[1][mb_x + mb_y * s->mb_stride] = (dcu + 4) >> 3;
  999. s->dc_val[2][mb_x + mb_y * s->mb_stride] = (dcv + 4) >> 3;
  1000. }
  1001. }
  1002. /* guess DC for damaged blocks */
  1003. guess_dc(s, s->dc_val[0], s->mb_width * 2, s->mb_height * 2, s->b8_stride, 1);
  1004. guess_dc(s, s->dc_val[1], s->mb_width, s->mb_height, s->mb_stride, 0);
  1005. guess_dc(s, s->dc_val[2], s->mb_width, s->mb_height, s->mb_stride, 0);
  1006. /* filter luma DC */
  1007. filter181(s->dc_val[0], s->mb_width * 2, s->mb_height * 2, s->b8_stride);
  1008. /* render DC only intra */
  1009. for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
  1010. for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
  1011. uint8_t *dest_y, *dest_cb, *dest_cr;
  1012. const int mb_xy = mb_x + mb_y * s->mb_stride;
  1013. const int mb_type = s->cur_pic.mb_type[mb_xy];
  1014. error = s->error_status_table[mb_xy];
  1015. if (IS_INTER(mb_type))
  1016. continue;
  1017. if (!(error & ER_AC_ERROR))
  1018. continue; // undamaged
  1019. dest_y = s->cur_pic.f->data[0] + mb_x * 16 + mb_y * 16 * linesize[0];
  1020. dest_cb = s->cur_pic.f->data[1] + mb_x * 8 + mb_y * 8 * linesize[1];
  1021. dest_cr = s->cur_pic.f->data[2] + mb_x * 8 + mb_y * 8 * linesize[2];
  1022. put_dc(s, dest_y, dest_cb, dest_cr, mb_x, mb_y);
  1023. }
  1024. }
  1025. if (s->avctx->error_concealment & FF_EC_DEBLOCK) {
  1026. /* filter horizontal block boundaries */
  1027. h_block_filter(s, s->cur_pic.f->data[0], s->mb_width * 2,
  1028. s->mb_height * 2, linesize[0], 1);
  1029. h_block_filter(s, s->cur_pic.f->data[1], s->mb_width,
  1030. s->mb_height, linesize[1], 0);
  1031. h_block_filter(s, s->cur_pic.f->data[2], s->mb_width,
  1032. s->mb_height, linesize[2], 0);
  1033. /* filter vertical block boundaries */
  1034. v_block_filter(s, s->cur_pic.f->data[0], s->mb_width * 2,
  1035. s->mb_height * 2, linesize[0], 1);
  1036. v_block_filter(s, s->cur_pic.f->data[1], s->mb_width,
  1037. s->mb_height, linesize[1], 0);
  1038. v_block_filter(s, s->cur_pic.f->data[2], s->mb_width,
  1039. s->mb_height, linesize[2], 0);
  1040. }
  1041. #if FF_API_XVMC
  1042. ec_clean:
  1043. #endif
  1044. /* clean a few tables */
  1045. for (i = 0; i < s->mb_num; i++) {
  1046. const int mb_xy = s->mb_index2xy[i];
  1047. int error = s->error_status_table[mb_xy];
  1048. if (s->mbskip_table && s->cur_pic.f->pict_type != AV_PICTURE_TYPE_B &&
  1049. (error & (ER_DC_ERROR | ER_MV_ERROR | ER_AC_ERROR))) {
  1050. s->mbskip_table[mb_xy] = 0;
  1051. }
  1052. if (s->mbintra_table)
  1053. s->mbintra_table[mb_xy] = 1;
  1054. }
  1055. memset(&s->cur_pic, 0, sizeof(ERPicture));
  1056. memset(&s->last_pic, 0, sizeof(ERPicture));
  1057. memset(&s->next_pic, 0, sizeof(ERPicture));
  1058. }