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.

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