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.

1230 lines
47KB

  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 "mpegvideo.h"
  31. #include "rectangle.h"
  32. #include "thread.h"
  33. #include "version.h"
  34. /**
  35. * @param stride the number of MVs to get to the next row
  36. * @param mv_step the number of MVs per row or column in a macroblock
  37. */
  38. static void set_mv_strides(ERContext *s, int *mv_step, int *stride)
  39. {
  40. if (s->avctx->codec_id == AV_CODEC_ID_H264) {
  41. assert(s->quarter_sample);
  42. *mv_step = 4;
  43. *stride = s->mb_width * 4;
  44. } else {
  45. *mv_step = 2;
  46. *stride = s->b8_stride;
  47. }
  48. }
  49. /**
  50. * Replace the current MB with a flat dc-only version.
  51. */
  52. static void put_dc(ERContext *s, uint8_t *dest_y, uint8_t *dest_cb,
  53. uint8_t *dest_cr, int mb_x, int mb_y)
  54. {
  55. int *linesize = s->cur_pic->f.linesize;
  56. int dc, dcu, dcv, y, i;
  57. for (i = 0; i < 4; i++) {
  58. dc = s->dc_val[0][mb_x * 2 + (i & 1) + (mb_y * 2 + (i >> 1)) * s->b8_stride];
  59. if (dc < 0)
  60. dc = 0;
  61. else if (dc > 2040)
  62. dc = 2040;
  63. for (y = 0; y < 8; y++) {
  64. int x;
  65. for (x = 0; x < 8; x++)
  66. dest_y[x + (i & 1) * 8 + (y + (i >> 1) * 8) * linesize[0]] = dc / 8;
  67. }
  68. }
  69. dcu = s->dc_val[1][mb_x + mb_y * s->mb_stride];
  70. dcv = s->dc_val[2][mb_x + mb_y * s->mb_stride];
  71. if (dcu < 0)
  72. dcu = 0;
  73. else if (dcu > 2040)
  74. dcu = 2040;
  75. if (dcv < 0)
  76. dcv = 0;
  77. else if (dcv > 2040)
  78. dcv = 2040;
  79. for (y = 0; y < 8; y++) {
  80. int x;
  81. for (x = 0; x < 8; x++) {
  82. dest_cb[x + y * linesize[1]] = dcu / 8;
  83. dest_cr[x + y * linesize[2]] = dcv / 8;
  84. }
  85. }
  86. }
  87. static void filter181(int16_t *data, int width, int height, int stride)
  88. {
  89. int x, y;
  90. /* horizontal filter */
  91. for (y = 1; y < height - 1; y++) {
  92. int prev_dc = data[0 + y * stride];
  93. for (x = 1; x < width - 1; x++) {
  94. int dc;
  95. dc = -prev_dc +
  96. data[x + y * stride] * 8 -
  97. data[x + 1 + y * stride];
  98. dc = (dc * 10923 + 32768) >> 16;
  99. prev_dc = data[x + y * stride];
  100. data[x + y * stride] = dc;
  101. }
  102. }
  103. /* vertical filter */
  104. for (x = 1; x < width - 1; x++) {
  105. int prev_dc = data[x];
  106. for (y = 1; y < height - 1; y++) {
  107. int dc;
  108. dc = -prev_dc +
  109. data[x + y * stride] * 8 -
  110. data[x + (y + 1) * stride];
  111. dc = (dc * 10923 + 32768) >> 16;
  112. prev_dc = data[x + y * stride];
  113. data[x + y * stride] = dc;
  114. }
  115. }
  116. }
  117. /**
  118. * guess the dc of blocks which do not have an undamaged dc
  119. * @param w width in 8 pixel blocks
  120. * @param h height in 8 pixel blocks
  121. */
  122. static void guess_dc(ERContext *s, int16_t *dc, int w,
  123. int h, int stride, int is_luma)
  124. {
  125. int b_x, b_y;
  126. for (b_y = 0; b_y < h; b_y++) {
  127. for (b_x = 0; b_x < w; b_x++) {
  128. int color[4] = { 1024, 1024, 1024, 1024 };
  129. int distance[4] = { 9999, 9999, 9999, 9999 };
  130. int mb_index, error, j;
  131. int64_t guess, weight_sum;
  132. mb_index = (b_x >> is_luma) + (b_y >> is_luma) * s->mb_stride;
  133. error = s->error_status_table[mb_index];
  134. if (IS_INTER(s->cur_pic->mb_type[mb_index]))
  135. continue; // inter
  136. if (!(error & ER_DC_ERROR))
  137. continue; // dc-ok
  138. /* right block */
  139. for (j = b_x + 1; j < w; j++) {
  140. int mb_index_j = (j >> is_luma) + (b_y >> is_luma) * s->mb_stride;
  141. int error_j = s->error_status_table[mb_index_j];
  142. int intra_j = IS_INTRA(s->cur_pic->mb_type[mb_index_j]);
  143. if (intra_j == 0 || !(error_j & ER_DC_ERROR)) {
  144. color[0] = dc[j + b_y * stride];
  145. distance[0] = j - b_x;
  146. break;
  147. }
  148. }
  149. /* left block */
  150. for (j = b_x - 1; j >= 0; j--) {
  151. int mb_index_j = (j >> is_luma) + (b_y >> is_luma) * s->mb_stride;
  152. int error_j = s->error_status_table[mb_index_j];
  153. int intra_j = IS_INTRA(s->cur_pic->mb_type[mb_index_j]);
  154. if (intra_j == 0 || !(error_j & ER_DC_ERROR)) {
  155. color[1] = dc[j + b_y * stride];
  156. distance[1] = b_x - j;
  157. break;
  158. }
  159. }
  160. /* bottom block */
  161. for (j = b_y + 1; j < h; j++) {
  162. int mb_index_j = (b_x >> is_luma) + (j >> is_luma) * s->mb_stride;
  163. int error_j = s->error_status_table[mb_index_j];
  164. int intra_j = IS_INTRA(s->cur_pic->mb_type[mb_index_j]);
  165. if (intra_j == 0 || !(error_j & ER_DC_ERROR)) {
  166. color[2] = dc[b_x + j * stride];
  167. distance[2] = j - b_y;
  168. break;
  169. }
  170. }
  171. /* top block */
  172. for (j = b_y - 1; j >= 0; j--) {
  173. int mb_index_j = (b_x >> is_luma) + (j >> is_luma) * s->mb_stride;
  174. int error_j = s->error_status_table[mb_index_j];
  175. int intra_j = IS_INTRA(s->cur_pic->mb_type[mb_index_j]);
  176. if (intra_j == 0 || !(error_j & ER_DC_ERROR)) {
  177. color[3] = dc[b_x + j * stride];
  178. distance[3] = b_y - j;
  179. break;
  180. }
  181. }
  182. weight_sum = 0;
  183. guess = 0;
  184. for (j = 0; j < 4; j++) {
  185. int64_t weight = 256 * 256 * 256 * 16 / distance[j];
  186. guess += weight * (int64_t) color[j];
  187. weight_sum += weight;
  188. }
  189. guess = (guess + weight_sum / 2) / weight_sum;
  190. dc[b_x + b_y * stride] = guess;
  191. }
  192. }
  193. }
  194. /**
  195. * simple horizontal deblocking filter used for error resilience
  196. * @param w width in 8 pixel blocks
  197. * @param h height in 8 pixel blocks
  198. */
  199. static void h_block_filter(ERContext *s, uint8_t *dst, int w,
  200. int h, int stride, int is_luma)
  201. {
  202. int b_x, b_y, mvx_stride, mvy_stride;
  203. const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  204. set_mv_strides(s, &mvx_stride, &mvy_stride);
  205. mvx_stride >>= is_luma;
  206. mvy_stride *= mvx_stride;
  207. for (b_y = 0; b_y < h; b_y++) {
  208. for (b_x = 0; b_x < w - 1; b_x++) {
  209. int y;
  210. int left_status = s->error_status_table[( b_x >> is_luma) + (b_y >> is_luma) * s->mb_stride];
  211. int right_status = s->error_status_table[((b_x + 1) >> is_luma) + (b_y >> is_luma) * s->mb_stride];
  212. int left_intra = IS_INTRA(s->cur_pic->mb_type[( b_x >> is_luma) + (b_y >> is_luma) * s->mb_stride]);
  213. int right_intra = IS_INTRA(s->cur_pic->mb_type[((b_x + 1) >> is_luma) + (b_y >> is_luma) * s->mb_stride]);
  214. int left_damage = left_status & ER_MB_ERROR;
  215. int right_damage = right_status & ER_MB_ERROR;
  216. int offset = b_x * 8 + b_y * stride * 8;
  217. int16_t *left_mv = s->cur_pic->motion_val[0][mvy_stride * b_y + mvx_stride * b_x];
  218. int16_t *right_mv = s->cur_pic->motion_val[0][mvy_stride * b_y + mvx_stride * (b_x + 1)];
  219. if (!(left_damage || right_damage))
  220. continue; // both undamaged
  221. if ((!left_intra) && (!right_intra) &&
  222. FFABS(left_mv[0] - right_mv[0]) +
  223. FFABS(left_mv[1] + right_mv[1]) < 2)
  224. continue;
  225. for (y = 0; y < 8; y++) {
  226. int a, b, c, d;
  227. a = dst[offset + 7 + y * stride] - dst[offset + 6 + y * stride];
  228. b = dst[offset + 8 + y * stride] - dst[offset + 7 + y * stride];
  229. c = dst[offset + 9 + y * stride] - dst[offset + 8 + y * stride];
  230. d = FFABS(b) - ((FFABS(a) + FFABS(c) + 1) >> 1);
  231. d = FFMAX(d, 0);
  232. if (b < 0)
  233. d = -d;
  234. if (d == 0)
  235. continue;
  236. if (!(left_damage && right_damage))
  237. d = d * 16 / 9;
  238. if (left_damage) {
  239. dst[offset + 7 + y * stride] = cm[dst[offset + 7 + y * stride] + ((d * 7) >> 4)];
  240. dst[offset + 6 + y * stride] = cm[dst[offset + 6 + y * stride] + ((d * 5) >> 4)];
  241. dst[offset + 5 + y * stride] = cm[dst[offset + 5 + y * stride] + ((d * 3) >> 4)];
  242. dst[offset + 4 + y * stride] = cm[dst[offset + 4 + y * stride] + ((d * 1) >> 4)];
  243. }
  244. if (right_damage) {
  245. dst[offset + 8 + y * stride] = cm[dst[offset + 8 + y * stride] - ((d * 7) >> 4)];
  246. dst[offset + 9 + y * stride] = cm[dst[offset + 9 + y * stride] - ((d * 5) >> 4)];
  247. dst[offset + 10+ y * stride] = cm[dst[offset + 10 + y * stride] - ((d * 3) >> 4)];
  248. dst[offset + 11+ y * stride] = cm[dst[offset + 11 + y * stride] - ((d * 1) >> 4)];
  249. }
  250. }
  251. }
  252. }
  253. }
  254. /**
  255. * simple vertical deblocking filter used for error resilience
  256. * @param w width in 8 pixel blocks
  257. * @param h height in 8 pixel blocks
  258. */
  259. static void v_block_filter(ERContext *s, uint8_t *dst, int w, int h,
  260. int stride, int is_luma)
  261. {
  262. int b_x, b_y, mvx_stride, mvy_stride;
  263. const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  264. set_mv_strides(s, &mvx_stride, &mvy_stride);
  265. mvx_stride >>= is_luma;
  266. mvy_stride *= mvx_stride;
  267. for (b_y = 0; b_y < h - 1; b_y++) {
  268. for (b_x = 0; b_x < w; b_x++) {
  269. int x;
  270. int top_status = s->error_status_table[(b_x >> is_luma) + (b_y >> is_luma) * s->mb_stride];
  271. int bottom_status = s->error_status_table[(b_x >> is_luma) + ((b_y + 1) >> is_luma) * s->mb_stride];
  272. int top_intra = IS_INTRA(s->cur_pic->mb_type[(b_x >> is_luma) + ( b_y >> is_luma) * s->mb_stride]);
  273. int bottom_intra = IS_INTRA(s->cur_pic->mb_type[(b_x >> is_luma) + ((b_y + 1) >> is_luma) * s->mb_stride]);
  274. int top_damage = top_status & ER_MB_ERROR;
  275. int bottom_damage = bottom_status & ER_MB_ERROR;
  276. int offset = b_x * 8 + b_y * stride * 8;
  277. int16_t *top_mv = s->cur_pic->motion_val[0][mvy_stride * b_y + mvx_stride * b_x];
  278. int16_t *bottom_mv = s->cur_pic->motion_val[0][mvy_stride * (b_y + 1) + mvx_stride * b_x];
  279. if (!(top_damage || bottom_damage))
  280. continue; // both undamaged
  281. if ((!top_intra) && (!bottom_intra) &&
  282. FFABS(top_mv[0] - bottom_mv[0]) +
  283. FFABS(top_mv[1] + bottom_mv[1]) < 2)
  284. continue;
  285. for (x = 0; x < 8; x++) {
  286. int a, b, c, d;
  287. a = dst[offset + x + 7 * stride] - dst[offset + x + 6 * stride];
  288. b = dst[offset + x + 8 * stride] - dst[offset + x + 7 * stride];
  289. c = dst[offset + x + 9 * stride] - dst[offset + x + 8 * stride];
  290. d = FFABS(b) - ((FFABS(a) + FFABS(c) + 1) >> 1);
  291. d = FFMAX(d, 0);
  292. if (b < 0)
  293. d = -d;
  294. if (d == 0)
  295. continue;
  296. if (!(top_damage && bottom_damage))
  297. d = d * 16 / 9;
  298. if (top_damage) {
  299. dst[offset + x + 7 * stride] = cm[dst[offset + x + 7 * stride] + ((d * 7) >> 4)];
  300. dst[offset + x + 6 * stride] = cm[dst[offset + x + 6 * stride] + ((d * 5) >> 4)];
  301. dst[offset + x + 5 * stride] = cm[dst[offset + x + 5 * stride] + ((d * 3) >> 4)];
  302. dst[offset + x + 4 * stride] = cm[dst[offset + x + 4 * stride] + ((d * 1) >> 4)];
  303. }
  304. if (bottom_damage) {
  305. dst[offset + x + 8 * stride] = cm[dst[offset + x + 8 * stride] - ((d * 7) >> 4)];
  306. dst[offset + x + 9 * stride] = cm[dst[offset + x + 9 * stride] - ((d * 5) >> 4)];
  307. dst[offset + x + 10 * stride] = cm[dst[offset + x + 10 * stride] - ((d * 3) >> 4)];
  308. dst[offset + x + 11 * stride] = cm[dst[offset + x + 11 * stride] - ((d * 1) >> 4)];
  309. }
  310. }
  311. }
  312. }
  313. }
  314. static void guess_mv(ERContext *s)
  315. {
  316. uint8_t *fixed = s->er_temp_buffer;
  317. #define MV_FROZEN 3
  318. #define MV_CHANGED 2
  319. #define MV_UNCHANGED 1
  320. const int mb_stride = s->mb_stride;
  321. const int mb_width = s->mb_width;
  322. const int mb_height = s->mb_height;
  323. int i, depth, num_avail;
  324. int mb_x, mb_y, mot_step, mot_stride;
  325. set_mv_strides(s, &mot_step, &mot_stride);
  326. num_avail = 0;
  327. for (i = 0; i < s->mb_num; i++) {
  328. const int mb_xy = s->mb_index2xy[i];
  329. int f = 0;
  330. int error = s->error_status_table[mb_xy];
  331. if (IS_INTRA(s->cur_pic->mb_type[mb_xy]))
  332. f = MV_FROZEN; // intra // FIXME check
  333. if (!(error & ER_MV_ERROR))
  334. f = MV_FROZEN; // inter with undamaged MV
  335. fixed[mb_xy] = f;
  336. if (f == MV_FROZEN)
  337. num_avail++;
  338. }
  339. if ((!(s->avctx->error_concealment&FF_EC_GUESS_MVS)) ||
  340. num_avail <= mb_width / 2) {
  341. for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
  342. for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
  343. const int mb_xy = mb_x + mb_y * s->mb_stride;
  344. int mv_dir = (s->last_pic && s->last_pic->f.data[0]) ? MV_DIR_FORWARD : MV_DIR_BACKWARD;
  345. if (IS_INTRA(s->cur_pic->mb_type[mb_xy]))
  346. continue;
  347. if (!(s->error_status_table[mb_xy] & ER_MV_ERROR))
  348. continue;
  349. s->mv[0][0][0] = 0;
  350. s->mv[0][0][1] = 0;
  351. s->decode_mb(s->opaque, 0, mv_dir, MV_TYPE_16X16, &s->mv,
  352. mb_x, mb_y, 0, 0);
  353. }
  354. }
  355. return;
  356. }
  357. for (depth = 0; ; depth++) {
  358. int changed, pass, none_left;
  359. none_left = 1;
  360. changed = 1;
  361. for (pass = 0; (changed || pass < 2) && pass < 10; pass++) {
  362. int mb_x, mb_y;
  363. int score_sum = 0;
  364. changed = 0;
  365. for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
  366. for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
  367. const int mb_xy = mb_x + mb_y * s->mb_stride;
  368. int mv_predictor[8][2] = { { 0 } };
  369. int ref[8] = { 0 };
  370. int pred_count = 0;
  371. int j;
  372. int best_score = 256 * 256 * 256 * 64;
  373. int best_pred = 0;
  374. const int mot_index = (mb_x + mb_y * mot_stride) * mot_step;
  375. int prev_x, prev_y, prev_ref;
  376. if ((mb_x ^ mb_y ^ pass) & 1)
  377. continue;
  378. if (fixed[mb_xy] == MV_FROZEN)
  379. continue;
  380. assert(!IS_INTRA(s->cur_pic->mb_type[mb_xy]));
  381. assert(s->last_pic && s->last_pic->f.data[0]);
  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 || !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->dsp->sad[0](NULL, last_mb_ptr, mb_ptr,
  629. linesize[0], 16);
  630. is_intra_likely -= s->dsp->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. memset(s->error_status_table, ER_MB_ERROR | VP_START | ER_MB_END,
  648. s->mb_stride * s->mb_height * sizeof(uint8_t));
  649. s->error_count = 3 * s->mb_num;
  650. s->error_occurred = 0;
  651. }
  652. /**
  653. * Add a slice.
  654. * @param endx x component of the last macroblock, can be -1
  655. * for the last of the previous line
  656. * @param status the status at the end (ER_MV_END, ER_AC_ERROR, ...), it is
  657. * assumed that no earlier end or error of the same type occurred
  658. */
  659. void ff_er_add_slice(ERContext *s, int startx, int starty,
  660. int endx, int endy, int status)
  661. {
  662. const int start_i = av_clip(startx + starty * s->mb_width, 0, s->mb_num - 1);
  663. const int end_i = av_clip(endx + endy * s->mb_width, 0, s->mb_num);
  664. const int start_xy = s->mb_index2xy[start_i];
  665. const int end_xy = s->mb_index2xy[end_i];
  666. int mask = -1;
  667. if (s->avctx->hwaccel)
  668. return;
  669. if (start_i > end_i || start_xy > end_xy) {
  670. av_log(s->avctx, AV_LOG_ERROR,
  671. "internal error, slice end before start\n");
  672. return;
  673. }
  674. if (!s->avctx->error_concealment)
  675. return;
  676. mask &= ~VP_START;
  677. if (status & (ER_AC_ERROR | ER_AC_END)) {
  678. mask &= ~(ER_AC_ERROR | ER_AC_END);
  679. s->error_count -= end_i - start_i + 1;
  680. }
  681. if (status & (ER_DC_ERROR | ER_DC_END)) {
  682. mask &= ~(ER_DC_ERROR | ER_DC_END);
  683. s->error_count -= end_i - start_i + 1;
  684. }
  685. if (status & (ER_MV_ERROR | ER_MV_END)) {
  686. mask &= ~(ER_MV_ERROR | ER_MV_END);
  687. s->error_count -= end_i - start_i + 1;
  688. }
  689. if (status & ER_MB_ERROR) {
  690. s->error_occurred = 1;
  691. s->error_count = INT_MAX;
  692. }
  693. if (mask == ~0x7F) {
  694. memset(&s->error_status_table[start_xy], 0,
  695. (end_xy - start_xy) * sizeof(uint8_t));
  696. } else {
  697. int i;
  698. for (i = start_xy; i < end_xy; i++)
  699. s->error_status_table[i] &= mask;
  700. }
  701. if (end_i == s->mb_num)
  702. s->error_count = INT_MAX;
  703. else {
  704. s->error_status_table[end_xy] &= mask;
  705. s->error_status_table[end_xy] |= status;
  706. }
  707. s->error_status_table[start_xy] |= VP_START;
  708. if (start_xy > 0 && s->avctx->thread_count <= 1 &&
  709. s->avctx->skip_top * s->mb_width < start_i) {
  710. int prev_status = s->error_status_table[s->mb_index2xy[start_i - 1]];
  711. prev_status &= ~ VP_START;
  712. if (prev_status != (ER_MV_END | ER_DC_END | ER_AC_END))
  713. s->error_count = INT_MAX;
  714. }
  715. }
  716. void ff_er_frame_end(ERContext *s)
  717. {
  718. int *linesize = s->cur_pic->f.linesize;
  719. int i, mb_x, mb_y, error, error_type, dc_error, mv_error, ac_error;
  720. int distance;
  721. int threshold_part[4] = { 100, 100, 100 };
  722. int threshold = 50;
  723. int is_intra_likely;
  724. int size = s->b8_stride * 2 * s->mb_height;
  725. /* We do not support ER of field pictures yet,
  726. * though it should not crash if enabled. */
  727. if (!s->avctx->error_concealment || s->error_count == 0 ||
  728. s->avctx->hwaccel ||
  729. !s->cur_pic || s->cur_pic->field_picture ||
  730. s->error_count == 3 * s->mb_width *
  731. (s->avctx->skip_top + s->avctx->skip_bottom)) {
  732. return;
  733. };
  734. if (s->cur_pic->motion_val[0] == NULL) {
  735. av_log(s->avctx, AV_LOG_ERROR, "Warning MVs not available\n");
  736. for (i = 0; i < 2; i++) {
  737. s->cur_pic->ref_index_buf[i] = av_buffer_allocz(s->mb_stride * s->mb_height * 4 * sizeof(uint8_t));
  738. s->cur_pic->motion_val_buf[i] = av_buffer_allocz((size + 4) * 2 * sizeof(uint16_t));
  739. if (!s->cur_pic->ref_index_buf[i] || !s->cur_pic->motion_val_buf[i])
  740. break;
  741. s->cur_pic->ref_index[i] = s->cur_pic->ref_index_buf[i]->data;
  742. s->cur_pic->motion_val[i] = (int16_t (*)[2])s->cur_pic->motion_val_buf[i]->data + 4;
  743. }
  744. if (i < 2) {
  745. for (i = 0; i < 2; i++) {
  746. av_buffer_unref(&s->cur_pic->ref_index_buf[i]);
  747. av_buffer_unref(&s->cur_pic->motion_val_buf[i]);
  748. s->cur_pic->ref_index[i] = NULL;
  749. s->cur_pic->motion_val[i] = NULL;
  750. }
  751. return;
  752. }
  753. }
  754. if (s->avctx->debug & FF_DEBUG_ER) {
  755. for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
  756. for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
  757. int status = s->error_status_table[mb_x + mb_y * s->mb_stride];
  758. av_log(s->avctx, AV_LOG_DEBUG, "%2X ", status);
  759. }
  760. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  761. }
  762. }
  763. /* handle overlapping slices */
  764. for (error_type = 1; error_type <= 3; error_type++) {
  765. int end_ok = 0;
  766. for (i = s->mb_num - 1; i >= 0; i--) {
  767. const int mb_xy = s->mb_index2xy[i];
  768. int error = s->error_status_table[mb_xy];
  769. if (error & (1 << error_type))
  770. end_ok = 1;
  771. if (error & (8 << error_type))
  772. end_ok = 1;
  773. if (!end_ok)
  774. s->error_status_table[mb_xy] |= 1 << error_type;
  775. if (error & VP_START)
  776. end_ok = 0;
  777. }
  778. }
  779. /* handle slices with partitions of different length */
  780. if (s->partitioned_frame) {
  781. int end_ok = 0;
  782. for (i = s->mb_num - 1; i >= 0; i--) {
  783. const int mb_xy = s->mb_index2xy[i];
  784. int error = s->error_status_table[mb_xy];
  785. if (error & ER_AC_END)
  786. end_ok = 0;
  787. if ((error & ER_MV_END) ||
  788. (error & ER_DC_END) ||
  789. (error & ER_AC_ERROR))
  790. end_ok = 1;
  791. if (!end_ok)
  792. s->error_status_table[mb_xy]|= ER_AC_ERROR;
  793. if (error & VP_START)
  794. end_ok = 0;
  795. }
  796. }
  797. /* handle missing slices */
  798. if (s->avctx->err_recognition & AV_EF_EXPLODE) {
  799. int end_ok = 1;
  800. // FIXME + 100 hack
  801. for (i = s->mb_num - 2; i >= s->mb_width + 100; i--) {
  802. const int mb_xy = s->mb_index2xy[i];
  803. int error1 = s->error_status_table[mb_xy];
  804. int error2 = s->error_status_table[s->mb_index2xy[i + 1]];
  805. if (error1 & VP_START)
  806. end_ok = 1;
  807. if (error2 == (VP_START | ER_MB_ERROR | ER_MB_END) &&
  808. error1 != (VP_START | ER_MB_ERROR | ER_MB_END) &&
  809. ((error1 & ER_AC_END) || (error1 & ER_DC_END) ||
  810. (error1 & ER_MV_END))) {
  811. // end & uninit
  812. end_ok = 0;
  813. }
  814. if (!end_ok)
  815. s->error_status_table[mb_xy] |= ER_MB_ERROR;
  816. }
  817. }
  818. /* backward mark errors */
  819. distance = 9999999;
  820. for (error_type = 1; error_type <= 3; error_type++) {
  821. for (i = s->mb_num - 1; i >= 0; i--) {
  822. const int mb_xy = s->mb_index2xy[i];
  823. int error = s->error_status_table[mb_xy];
  824. if (!s->mbskip_table[mb_xy]) // FIXME partition specific
  825. distance++;
  826. if (error & (1 << error_type))
  827. distance = 0;
  828. if (s->partitioned_frame) {
  829. if (distance < threshold_part[error_type - 1])
  830. s->error_status_table[mb_xy] |= 1 << error_type;
  831. } else {
  832. if (distance < threshold)
  833. s->error_status_table[mb_xy] |= 1 << error_type;
  834. }
  835. if (error & VP_START)
  836. distance = 9999999;
  837. }
  838. }
  839. /* forward mark errors */
  840. error = 0;
  841. for (i = 0; i < s->mb_num; i++) {
  842. const int mb_xy = s->mb_index2xy[i];
  843. int old_error = s->error_status_table[mb_xy];
  844. if (old_error & VP_START) {
  845. error = old_error & ER_MB_ERROR;
  846. } else {
  847. error |= old_error & ER_MB_ERROR;
  848. s->error_status_table[mb_xy] |= error;
  849. }
  850. }
  851. /* handle not partitioned case */
  852. if (!s->partitioned_frame) {
  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_MB_ERROR)
  857. error |= ER_MB_ERROR;
  858. s->error_status_table[mb_xy] = error;
  859. }
  860. }
  861. dc_error = ac_error = mv_error = 0;
  862. for (i = 0; i < s->mb_num; i++) {
  863. const int mb_xy = s->mb_index2xy[i];
  864. error = s->error_status_table[mb_xy];
  865. if (error & ER_DC_ERROR)
  866. dc_error++;
  867. if (error & ER_AC_ERROR)
  868. ac_error++;
  869. if (error & ER_MV_ERROR)
  870. mv_error++;
  871. }
  872. av_log(s->avctx, AV_LOG_INFO, "concealing %d DC, %d AC, %d MV errors\n",
  873. dc_error, ac_error, mv_error);
  874. is_intra_likely = is_intra_more_likely(s);
  875. /* set unknown mb-type to most likely */
  876. for (i = 0; i < s->mb_num; i++) {
  877. const int mb_xy = s->mb_index2xy[i];
  878. error = s->error_status_table[mb_xy];
  879. if (!((error & ER_DC_ERROR) && (error & ER_MV_ERROR)))
  880. continue;
  881. if (is_intra_likely)
  882. s->cur_pic->mb_type[mb_xy] = MB_TYPE_INTRA4x4;
  883. else
  884. s->cur_pic->mb_type[mb_xy] = MB_TYPE_16x16 | MB_TYPE_L0;
  885. }
  886. // change inter to intra blocks if no reference frames are available
  887. if (!(s->last_pic && s->last_pic->f.data[0]) &&
  888. !(s->next_pic && s->next_pic->f.data[0]))
  889. for (i = 0; i < s->mb_num; i++) {
  890. const int mb_xy = s->mb_index2xy[i];
  891. if (!IS_INTRA(s->cur_pic->mb_type[mb_xy]))
  892. s->cur_pic->mb_type[mb_xy] = MB_TYPE_INTRA4x4;
  893. }
  894. /* handle inter blocks with damaged AC */
  895. for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
  896. for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
  897. const int mb_xy = mb_x + mb_y * s->mb_stride;
  898. const int mb_type = s->cur_pic->mb_type[mb_xy];
  899. const int dir = !(s->last_pic && s->last_pic->f.data[0]);
  900. const int mv_dir = dir ? MV_DIR_BACKWARD : MV_DIR_FORWARD;
  901. int mv_type;
  902. error = s->error_status_table[mb_xy];
  903. if (IS_INTRA(mb_type))
  904. continue; // intra
  905. if (error & ER_MV_ERROR)
  906. continue; // inter with damaged MV
  907. if (!(error & ER_AC_ERROR))
  908. continue; // undamaged inter
  909. if (IS_8X8(mb_type)) {
  910. int mb_index = mb_x * 2 + mb_y * 2 * s->b8_stride;
  911. int j;
  912. mv_type = MV_TYPE_8X8;
  913. for (j = 0; j < 4; j++) {
  914. s->mv[0][j][0] = s->cur_pic->motion_val[dir][mb_index + (j & 1) + (j >> 1) * s->b8_stride][0];
  915. s->mv[0][j][1] = s->cur_pic->motion_val[dir][mb_index + (j & 1) + (j >> 1) * s->b8_stride][1];
  916. }
  917. } else {
  918. mv_type = MV_TYPE_16X16;
  919. s->mv[0][0][0] = s->cur_pic->motion_val[dir][mb_x * 2 + mb_y * 2 * s->b8_stride][0];
  920. s->mv[0][0][1] = s->cur_pic->motion_val[dir][mb_x * 2 + mb_y * 2 * s->b8_stride][1];
  921. }
  922. s->decode_mb(s->opaque, 0 /* FIXME h264 partitioned slices need this set */,
  923. mv_dir, mv_type, &s->mv, mb_x, mb_y, 0, 0);
  924. }
  925. }
  926. /* guess MVs */
  927. if (s->cur_pic->f.pict_type == AV_PICTURE_TYPE_B) {
  928. for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
  929. for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
  930. int xy = mb_x * 2 + mb_y * 2 * s->b8_stride;
  931. const int mb_xy = mb_x + mb_y * s->mb_stride;
  932. const int mb_type = s->cur_pic->mb_type[mb_xy];
  933. int mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  934. error = s->error_status_table[mb_xy];
  935. if (IS_INTRA(mb_type))
  936. continue;
  937. if (!(error & ER_MV_ERROR))
  938. continue; // inter with undamaged MV
  939. if (!(error & ER_AC_ERROR))
  940. continue; // undamaged inter
  941. if (!(s->last_pic && s->last_pic->f.data[0]))
  942. mv_dir &= ~MV_DIR_FORWARD;
  943. if (!(s->next_pic && s->next_pic->f.data[0]))
  944. mv_dir &= ~MV_DIR_BACKWARD;
  945. if (s->pp_time) {
  946. int time_pp = s->pp_time;
  947. int time_pb = s->pb_time;
  948. ff_thread_await_progress(&s->next_pic->tf, mb_y, 0);
  949. s->mv[0][0][0] = s->next_pic->motion_val[0][xy][0] * time_pb / time_pp;
  950. s->mv[0][0][1] = s->next_pic->motion_val[0][xy][1] * time_pb / time_pp;
  951. s->mv[1][0][0] = s->next_pic->motion_val[0][xy][0] * (time_pb - time_pp) / time_pp;
  952. s->mv[1][0][1] = s->next_pic->motion_val[0][xy][1] * (time_pb - time_pp) / time_pp;
  953. } else {
  954. s->mv[0][0][0] = 0;
  955. s->mv[0][0][1] = 0;
  956. s->mv[1][0][0] = 0;
  957. s->mv[1][0][1] = 0;
  958. }
  959. s->decode_mb(s->opaque, 0, mv_dir, MV_TYPE_16X16, &s->mv,
  960. mb_x, mb_y, 0, 0);
  961. }
  962. }
  963. } else
  964. guess_mv(s);
  965. #if FF_API_XVMC
  966. FF_DISABLE_DEPRECATION_WARNINGS
  967. /* the filters below are not XvMC compatible, skip them */
  968. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
  969. goto ec_clean;
  970. FF_ENABLE_DEPRECATION_WARNINGS
  971. #endif /* FF_API_XVMC */
  972. /* fill DC for inter blocks */
  973. for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
  974. for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
  975. int dc, dcu, dcv, y, n;
  976. int16_t *dc_ptr;
  977. uint8_t *dest_y, *dest_cb, *dest_cr;
  978. const int mb_xy = mb_x + mb_y * s->mb_stride;
  979. const int mb_type = s->cur_pic->mb_type[mb_xy];
  980. error = s->error_status_table[mb_xy];
  981. if (IS_INTRA(mb_type) && s->partitioned_frame)
  982. continue;
  983. // if (error & ER_MV_ERROR)
  984. // continue; // inter data damaged FIXME is this good?
  985. dest_y = s->cur_pic->f.data[0] + mb_x * 16 + mb_y * 16 * linesize[0];
  986. dest_cb = s->cur_pic->f.data[1] + mb_x * 8 + mb_y * 8 * linesize[1];
  987. dest_cr = s->cur_pic->f.data[2] + mb_x * 8 + mb_y * 8 * linesize[2];
  988. dc_ptr = &s->dc_val[0][mb_x * 2 + mb_y * 2 * s->b8_stride];
  989. for (n = 0; n < 4; n++) {
  990. dc = 0;
  991. for (y = 0; y < 8; y++) {
  992. int x;
  993. for (x = 0; x < 8; x++)
  994. dc += dest_y[x + (n & 1) * 8 +
  995. (y + (n >> 1) * 8) * linesize[0]];
  996. }
  997. dc_ptr[(n & 1) + (n >> 1) * s->b8_stride] = (dc + 4) >> 3;
  998. }
  999. dcu = dcv = 0;
  1000. for (y = 0; y < 8; y++) {
  1001. int x;
  1002. for (x = 0; x < 8; x++) {
  1003. dcu += dest_cb[x + y * linesize[1]];
  1004. dcv += dest_cr[x + y * linesize[2]];
  1005. }
  1006. }
  1007. s->dc_val[1][mb_x + mb_y * s->mb_stride] = (dcu + 4) >> 3;
  1008. s->dc_val[2][mb_x + mb_y * s->mb_stride] = (dcv + 4) >> 3;
  1009. }
  1010. }
  1011. /* guess DC for damaged blocks */
  1012. guess_dc(s, s->dc_val[0], s->mb_width * 2, s->mb_height * 2, s->b8_stride, 1);
  1013. guess_dc(s, s->dc_val[1], s->mb_width, s->mb_height, s->mb_stride, 0);
  1014. guess_dc(s, s->dc_val[2], s->mb_width, s->mb_height, s->mb_stride, 0);
  1015. /* filter luma DC */
  1016. filter181(s->dc_val[0], s->mb_width * 2, s->mb_height * 2, s->b8_stride);
  1017. /* render DC only intra */
  1018. for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
  1019. for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
  1020. uint8_t *dest_y, *dest_cb, *dest_cr;
  1021. const int mb_xy = mb_x + mb_y * s->mb_stride;
  1022. const int mb_type = s->cur_pic->mb_type[mb_xy];
  1023. error = s->error_status_table[mb_xy];
  1024. if (IS_INTER(mb_type))
  1025. continue;
  1026. if (!(error & ER_AC_ERROR))
  1027. continue; // undamaged
  1028. dest_y = s->cur_pic->f.data[0] + mb_x * 16 + mb_y * 16 * linesize[0];
  1029. dest_cb = s->cur_pic->f.data[1] + mb_x * 8 + mb_y * 8 * linesize[1];
  1030. dest_cr = s->cur_pic->f.data[2] + mb_x * 8 + mb_y * 8 * linesize[2];
  1031. put_dc(s, dest_y, dest_cb, dest_cr, mb_x, mb_y);
  1032. }
  1033. }
  1034. if (s->avctx->error_concealment & FF_EC_DEBLOCK) {
  1035. /* filter horizontal block boundaries */
  1036. h_block_filter(s, s->cur_pic->f.data[0], s->mb_width * 2,
  1037. s->mb_height * 2, linesize[0], 1);
  1038. h_block_filter(s, s->cur_pic->f.data[1], s->mb_width,
  1039. s->mb_height, linesize[1], 0);
  1040. h_block_filter(s, s->cur_pic->f.data[2], s->mb_width,
  1041. s->mb_height, linesize[2], 0);
  1042. /* filter vertical block boundaries */
  1043. v_block_filter(s, s->cur_pic->f.data[0], s->mb_width * 2,
  1044. s->mb_height * 2, linesize[0], 1);
  1045. v_block_filter(s, s->cur_pic->f.data[1], s->mb_width,
  1046. s->mb_height, linesize[1], 0);
  1047. v_block_filter(s, s->cur_pic->f.data[2], s->mb_width,
  1048. s->mb_height, linesize[2], 0);
  1049. }
  1050. ec_clean:
  1051. /* clean a few tables */
  1052. for (i = 0; i < s->mb_num; i++) {
  1053. const int mb_xy = s->mb_index2xy[i];
  1054. int error = s->error_status_table[mb_xy];
  1055. if (s->cur_pic->f.pict_type != AV_PICTURE_TYPE_B &&
  1056. (error & (ER_DC_ERROR | ER_MV_ERROR | ER_AC_ERROR))) {
  1057. s->mbskip_table[mb_xy] = 0;
  1058. }
  1059. s->mbintra_table[mb_xy] = 1;
  1060. }
  1061. s->cur_pic = NULL;
  1062. s->next_pic = NULL;
  1063. s->last_pic = NULL;
  1064. }