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.

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