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.

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