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.

1211 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 "avcodec.h"
  28. #include "dsputil.h"
  29. #include "error_resilience.h"
  30. #include "mpegvideo.h"
  31. #include "rectangle.h"
  32. #include "thread.h"
  33. /**
  34. * @param stride the number of MVs to get to the next row
  35. * @param mv_step the number of MVs per row or column in a macroblock
  36. */
  37. static void set_mv_strides(ERContext *s, int *mv_step, int *stride)
  38. {
  39. if (s->avctx->codec_id == AV_CODEC_ID_H264) {
  40. assert(s->quarter_sample);
  41. *mv_step = 4;
  42. *stride = s->mb_width * 4;
  43. } else {
  44. *mv_step = 2;
  45. *stride = s->b8_stride;
  46. }
  47. }
  48. /**
  49. * Replace the current MB with a flat dc-only version.
  50. */
  51. static void put_dc(ERContext *s, uint8_t *dest_y, uint8_t *dest_cb,
  52. uint8_t *dest_cr, int mb_x, int mb_y)
  53. {
  54. int *linesize = s->cur_pic->f.linesize;
  55. int dc, dcu, dcv, y, i;
  56. for (i = 0; i < 4; i++) {
  57. dc = s->dc_val[0][mb_x * 2 + (i & 1) + (mb_y * 2 + (i >> 1)) * s->b8_stride];
  58. if (dc < 0)
  59. dc = 0;
  60. else if (dc > 2040)
  61. dc = 2040;
  62. for (y = 0; y < 8; y++) {
  63. int x;
  64. for (x = 0; x < 8; x++)
  65. dest_y[x + (i & 1) * 8 + (y + (i >> 1) * 8) * linesize[0]] = dc / 8;
  66. }
  67. }
  68. dcu = s->dc_val[1][mb_x + mb_y * s->mb_stride];
  69. dcv = s->dc_val[2][mb_x + mb_y * s->mb_stride];
  70. if (dcu < 0)
  71. dcu = 0;
  72. else if (dcu > 2040)
  73. dcu = 2040;
  74. if (dcv < 0)
  75. dcv = 0;
  76. else if (dcv > 2040)
  77. dcv = 2040;
  78. for (y = 0; y < 8; y++) {
  79. int x;
  80. for (x = 0; x < 8; x++) {
  81. dest_cb[x + y * linesize[1]] = dcu / 8;
  82. dest_cr[x + y * linesize[2]] = dcv / 8;
  83. }
  84. }
  85. }
  86. static void filter181(int16_t *data, int width, int height, int stride)
  87. {
  88. int x, y;
  89. /* horizontal filter */
  90. for (y = 1; y < height - 1; y++) {
  91. int prev_dc = data[0 + y * stride];
  92. for (x = 1; x < width - 1; x++) {
  93. int dc;
  94. dc = -prev_dc +
  95. data[x + y * stride] * 8 -
  96. data[x + 1 + y * stride];
  97. dc = (dc * 10923 + 32768) >> 16;
  98. prev_dc = data[x + y * stride];
  99. data[x + y * stride] = dc;
  100. }
  101. }
  102. /* vertical filter */
  103. for (x = 1; x < width - 1; x++) {
  104. int prev_dc = data[x];
  105. for (y = 1; y < height - 1; y++) {
  106. int dc;
  107. dc = -prev_dc +
  108. data[x + y * stride] * 8 -
  109. data[x + (y + 1) * stride];
  110. dc = (dc * 10923 + 32768) >> 16;
  111. prev_dc = data[x + y * stride];
  112. data[x + y * stride] = dc;
  113. }
  114. }
  115. }
  116. /**
  117. * guess the dc of blocks which do not have an undamaged dc
  118. * @param w width in 8 pixel blocks
  119. * @param h height in 8 pixel blocks
  120. */
  121. static void guess_dc(ERContext *s, int16_t *dc, int w,
  122. int h, int stride, int is_luma)
  123. {
  124. int b_x, b_y;
  125. for (b_y = 0; b_y < h; b_y++) {
  126. for (b_x = 0; b_x < w; b_x++) {
  127. int color[4] = { 1024, 1024, 1024, 1024 };
  128. int distance[4] = { 9999, 9999, 9999, 9999 };
  129. int mb_index, error, j;
  130. int64_t guess, weight_sum;
  131. mb_index = (b_x >> is_luma) + (b_y >> is_luma) * s->mb_stride;
  132. error = s->error_status_table[mb_index];
  133. if (IS_INTER(s->cur_pic->f.mb_type[mb_index]))
  134. continue; // inter
  135. if (!(error & ER_DC_ERROR))
  136. continue; // dc-ok
  137. /* right block */
  138. for (j = b_x + 1; j < w; j++) {
  139. int mb_index_j = (j >> is_luma) + (b_y >> is_luma) * s->mb_stride;
  140. int error_j = s->error_status_table[mb_index_j];
  141. int intra_j = IS_INTRA(s->cur_pic->f.mb_type[mb_index_j]);
  142. if (intra_j == 0 || !(error_j & ER_DC_ERROR)) {
  143. color[0] = dc[j + b_y * stride];
  144. distance[0] = j - b_x;
  145. break;
  146. }
  147. }
  148. /* left block */
  149. for (j = b_x - 1; j >= 0; j--) {
  150. int mb_index_j = (j >> is_luma) + (b_y >> is_luma) * s->mb_stride;
  151. int error_j = s->error_status_table[mb_index_j];
  152. int intra_j = IS_INTRA(s->cur_pic->f.mb_type[mb_index_j]);
  153. if (intra_j == 0 || !(error_j & ER_DC_ERROR)) {
  154. color[1] = dc[j + b_y * stride];
  155. distance[1] = b_x - j;
  156. break;
  157. }
  158. }
  159. /* bottom block */
  160. for (j = b_y + 1; j < h; j++) {
  161. int mb_index_j = (b_x >> is_luma) + (j >> is_luma) * s->mb_stride;
  162. int error_j = s->error_status_table[mb_index_j];
  163. int intra_j = IS_INTRA(s->cur_pic->f.mb_type[mb_index_j]);
  164. if (intra_j == 0 || !(error_j & ER_DC_ERROR)) {
  165. color[2] = dc[b_x + j * stride];
  166. distance[2] = j - b_y;
  167. break;
  168. }
  169. }
  170. /* top block */
  171. for (j = b_y - 1; j >= 0; j--) {
  172. int mb_index_j = (b_x >> is_luma) + (j >> is_luma) * s->mb_stride;
  173. int error_j = s->error_status_table[mb_index_j];
  174. int intra_j = IS_INTRA(s->cur_pic->f.mb_type[mb_index_j]);
  175. if (intra_j == 0 || !(error_j & ER_DC_ERROR)) {
  176. color[3] = dc[b_x + j * stride];
  177. distance[3] = b_y - j;
  178. break;
  179. }
  180. }
  181. weight_sum = 0;
  182. guess = 0;
  183. for (j = 0; j < 4; j++) {
  184. int64_t weight = 256 * 256 * 256 * 16 / distance[j];
  185. guess += weight * (int64_t) color[j];
  186. weight_sum += weight;
  187. }
  188. guess = (guess + weight_sum / 2) / weight_sum;
  189. dc[b_x + b_y * stride] = guess;
  190. }
  191. }
  192. }
  193. /**
  194. * simple horizontal deblocking filter used for error resilience
  195. * @param w width in 8 pixel blocks
  196. * @param h height in 8 pixel blocks
  197. */
  198. static void h_block_filter(ERContext *s, uint8_t *dst, int w,
  199. int h, int stride, int is_luma)
  200. {
  201. int b_x, b_y, mvx_stride, mvy_stride;
  202. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  203. set_mv_strides(s, &mvx_stride, &mvy_stride);
  204. mvx_stride >>= is_luma;
  205. mvy_stride *= mvx_stride;
  206. for (b_y = 0; b_y < h; b_y++) {
  207. for (b_x = 0; b_x < w - 1; b_x++) {
  208. int y;
  209. int left_status = s->error_status_table[( b_x >> is_luma) + (b_y >> is_luma) * s->mb_stride];
  210. int right_status = s->error_status_table[((b_x + 1) >> is_luma) + (b_y >> is_luma) * s->mb_stride];
  211. int left_intra = IS_INTRA(s->cur_pic->f.mb_type[( b_x >> is_luma) + (b_y >> is_luma) * s->mb_stride]);
  212. int right_intra = IS_INTRA(s->cur_pic->f.mb_type[((b_x + 1) >> is_luma) + (b_y >> is_luma) * s->mb_stride]);
  213. int left_damage = left_status & ER_MB_ERROR;
  214. int right_damage = right_status & ER_MB_ERROR;
  215. int offset = b_x * 8 + b_y * stride * 8;
  216. int16_t *left_mv = s->cur_pic->f.motion_val[0][mvy_stride * b_y + mvx_stride * b_x];
  217. int16_t *right_mv = s->cur_pic->f.motion_val[0][mvy_stride * b_y + mvx_stride * (b_x + 1)];
  218. if (!(left_damage || right_damage))
  219. continue; // both undamaged
  220. if ((!left_intra) && (!right_intra) &&
  221. FFABS(left_mv[0] - right_mv[0]) +
  222. FFABS(left_mv[1] + right_mv[1]) < 2)
  223. continue;
  224. for (y = 0; y < 8; y++) {
  225. int a, b, c, d;
  226. a = dst[offset + 7 + y * stride] - dst[offset + 6 + y * stride];
  227. b = dst[offset + 8 + y * stride] - dst[offset + 7 + y * stride];
  228. c = dst[offset + 9 + y * stride] - dst[offset + 8 + y * stride];
  229. d = FFABS(b) - ((FFABS(a) + FFABS(c) + 1) >> 1);
  230. d = FFMAX(d, 0);
  231. if (b < 0)
  232. d = -d;
  233. if (d == 0)
  234. continue;
  235. if (!(left_damage && right_damage))
  236. d = d * 16 / 9;
  237. if (left_damage) {
  238. dst[offset + 7 + y * stride] = cm[dst[offset + 7 + y * stride] + ((d * 7) >> 4)];
  239. dst[offset + 6 + y * stride] = cm[dst[offset + 6 + y * stride] + ((d * 5) >> 4)];
  240. dst[offset + 5 + y * stride] = cm[dst[offset + 5 + y * stride] + ((d * 3) >> 4)];
  241. dst[offset + 4 + y * stride] = cm[dst[offset + 4 + y * stride] + ((d * 1) >> 4)];
  242. }
  243. if (right_damage) {
  244. dst[offset + 8 + y * stride] = cm[dst[offset + 8 + y * stride] - ((d * 7) >> 4)];
  245. dst[offset + 9 + y * stride] = cm[dst[offset + 9 + y * stride] - ((d * 5) >> 4)];
  246. dst[offset + 10+ y * stride] = cm[dst[offset + 10 + y * stride] - ((d * 3) >> 4)];
  247. dst[offset + 11+ y * stride] = cm[dst[offset + 11 + y * stride] - ((d * 1) >> 4)];
  248. }
  249. }
  250. }
  251. }
  252. }
  253. /**
  254. * simple vertical deblocking filter used for error resilience
  255. * @param w width in 8 pixel blocks
  256. * @param h height in 8 pixel blocks
  257. */
  258. static void v_block_filter(ERContext *s, uint8_t *dst, int w, int h,
  259. int stride, int is_luma)
  260. {
  261. int b_x, b_y, mvx_stride, mvy_stride;
  262. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  263. set_mv_strides(s, &mvx_stride, &mvy_stride);
  264. mvx_stride >>= is_luma;
  265. mvy_stride *= mvx_stride;
  266. for (b_y = 0; b_y < h - 1; b_y++) {
  267. for (b_x = 0; b_x < w; b_x++) {
  268. int x;
  269. int top_status = s->error_status_table[(b_x >> is_luma) + (b_y >> is_luma) * s->mb_stride];
  270. int bottom_status = s->error_status_table[(b_x >> is_luma) + ((b_y + 1) >> is_luma) * s->mb_stride];
  271. int top_intra = IS_INTRA(s->cur_pic->f.mb_type[(b_x >> is_luma) + ( b_y >> is_luma) * s->mb_stride]);
  272. int bottom_intra = IS_INTRA(s->cur_pic->f.mb_type[(b_x >> is_luma) + ((b_y + 1) >> is_luma) * s->mb_stride]);
  273. int top_damage = top_status & ER_MB_ERROR;
  274. int bottom_damage = bottom_status & ER_MB_ERROR;
  275. int offset = b_x * 8 + b_y * stride * 8;
  276. int16_t *top_mv = s->cur_pic->f.motion_val[0][mvy_stride * b_y + mvx_stride * b_x];
  277. int16_t *bottom_mv = s->cur_pic->f.motion_val[0][mvy_stride * (b_y + 1) + mvx_stride * b_x];
  278. if (!(top_damage || bottom_damage))
  279. continue; // both undamaged
  280. if ((!top_intra) && (!bottom_intra) &&
  281. FFABS(top_mv[0] - bottom_mv[0]) +
  282. FFABS(top_mv[1] + bottom_mv[1]) < 2)
  283. continue;
  284. for (x = 0; x < 8; x++) {
  285. int a, b, c, d;
  286. a = dst[offset + x + 7 * stride] - dst[offset + x + 6 * stride];
  287. b = dst[offset + x + 8 * stride] - dst[offset + x + 7 * stride];
  288. c = dst[offset + x + 9 * stride] - dst[offset + x + 8 * stride];
  289. d = FFABS(b) - ((FFABS(a) + FFABS(c) + 1) >> 1);
  290. d = FFMAX(d, 0);
  291. if (b < 0)
  292. d = -d;
  293. if (d == 0)
  294. continue;
  295. if (!(top_damage && bottom_damage))
  296. d = d * 16 / 9;
  297. if (top_damage) {
  298. dst[offset + x + 7 * stride] = cm[dst[offset + x + 7 * stride] + ((d * 7) >> 4)];
  299. dst[offset + x + 6 * stride] = cm[dst[offset + x + 6 * stride] + ((d * 5) >> 4)];
  300. dst[offset + x + 5 * stride] = cm[dst[offset + x + 5 * stride] + ((d * 3) >> 4)];
  301. dst[offset + x + 4 * stride] = cm[dst[offset + x + 4 * stride] + ((d * 1) >> 4)];
  302. }
  303. if (bottom_damage) {
  304. dst[offset + x + 8 * stride] = cm[dst[offset + x + 8 * stride] - ((d * 7) >> 4)];
  305. dst[offset + x + 9 * stride] = cm[dst[offset + x + 9 * stride] - ((d * 5) >> 4)];
  306. dst[offset + x + 10 * stride] = cm[dst[offset + x + 10 * stride] - ((d * 3) >> 4)];
  307. dst[offset + x + 11 * stride] = cm[dst[offset + x + 11 * stride] - ((d * 1) >> 4)];
  308. }
  309. }
  310. }
  311. }
  312. }
  313. static void guess_mv(ERContext *s)
  314. {
  315. uint8_t *fixed = s->er_temp_buffer;
  316. #define MV_FROZEN 3
  317. #define MV_CHANGED 2
  318. #define MV_UNCHANGED 1
  319. const int mb_stride = s->mb_stride;
  320. const int mb_width = s->mb_width;
  321. const int mb_height = s->mb_height;
  322. int i, depth, num_avail;
  323. int mb_x, mb_y, mot_step, mot_stride;
  324. set_mv_strides(s, &mot_step, &mot_stride);
  325. num_avail = 0;
  326. for (i = 0; i < s->mb_num; i++) {
  327. const int mb_xy = s->mb_index2xy[i];
  328. int f = 0;
  329. int error = s->error_status_table[mb_xy];
  330. if (IS_INTRA(s->cur_pic->f.mb_type[mb_xy]))
  331. f = MV_FROZEN; // intra // FIXME check
  332. if (!(error & ER_MV_ERROR))
  333. f = MV_FROZEN; // inter with undamaged MV
  334. fixed[mb_xy] = f;
  335. if (f == MV_FROZEN)
  336. num_avail++;
  337. }
  338. if ((!(s->avctx->error_concealment&FF_EC_GUESS_MVS)) ||
  339. num_avail <= mb_width / 2) {
  340. for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
  341. for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
  342. const int mb_xy = mb_x + mb_y * s->mb_stride;
  343. int mv_dir = (s->last_pic && s->last_pic->f.data[0]) ? MV_DIR_FORWARD : MV_DIR_BACKWARD;
  344. if (IS_INTRA(s->cur_pic->f.mb_type[mb_xy]))
  345. continue;
  346. if (!(s->error_status_table[mb_xy] & ER_MV_ERROR))
  347. continue;
  348. s->mv[0][0][0] = 0;
  349. s->mv[0][0][1] = 0;
  350. s->decode_mb(s->opaque, 0, mv_dir, MV_TYPE_16X16, &s->mv,
  351. mb_x, mb_y, 0, 0);
  352. }
  353. }
  354. return;
  355. }
  356. for (depth = 0; ; depth++) {
  357. int changed, pass, none_left;
  358. none_left = 1;
  359. changed = 1;
  360. for (pass = 0; (changed || pass < 2) && pass < 10; pass++) {
  361. int mb_x, mb_y;
  362. int score_sum = 0;
  363. changed = 0;
  364. for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
  365. for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
  366. const int mb_xy = mb_x + mb_y * s->mb_stride;
  367. int mv_predictor[8][2] = { { 0 } };
  368. int ref[8] = { 0 };
  369. int pred_count = 0;
  370. int j;
  371. int best_score = 256 * 256 * 256 * 64;
  372. int best_pred = 0;
  373. const int mot_index = (mb_x + mb_y * mot_stride) * mot_step;
  374. int prev_x, prev_y, prev_ref;
  375. if ((mb_x ^ mb_y ^ pass) & 1)
  376. continue;
  377. if (fixed[mb_xy] == MV_FROZEN)
  378. continue;
  379. assert(!IS_INTRA(s->cur_pic->f.mb_type[mb_xy]));
  380. assert(s->last_pic && s->last_pic->f.data[0]);
  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->f.motion_val[0][mot_index - mot_step][0];
  407. mv_predictor[pred_count][1] =
  408. s->cur_pic->f.motion_val[0][mot_index - mot_step][1];
  409. ref[pred_count] =
  410. s->cur_pic->f.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->f.motion_val[0][mot_index + mot_step][0];
  416. mv_predictor[pred_count][1] =
  417. s->cur_pic->f.motion_val[0][mot_index + mot_step][1];
  418. ref[pred_count] =
  419. s->cur_pic->f.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->f.motion_val[0][mot_index - mot_stride * mot_step][0];
  425. mv_predictor[pred_count][1] =
  426. s->cur_pic->f.motion_val[0][mot_index - mot_stride * mot_step][1];
  427. ref[pred_count] =
  428. s->cur_pic->f.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->f.motion_val[0][mot_index + mot_stride * mot_step][0];
  434. mv_predictor[pred_count][1] =
  435. s->cur_pic->f.motion_val[0][mot_index + mot_stride * mot_step][1];
  436. ref[pred_count] =
  437. s->cur_pic->f.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->f,
  489. mb_y, 0);
  490. }
  491. if (!s->last_pic->f.motion_val[0] ||
  492. !s->last_pic->f.ref_index[0])
  493. goto skip_last_mv;
  494. prev_x = s->last_pic->f.motion_val[0][mot_index][0];
  495. prev_y = s->last_pic->f.motion_val[0][mot_index][1];
  496. prev_ref = s->last_pic->f.ref_index[0][4 * mb_xy];
  497. } else {
  498. prev_x = s->cur_pic->f.motion_val[0][mot_index][0];
  499. prev_y = s->cur_pic->f.motion_val[0][mot_index][1];
  500. prev_ref = s->cur_pic->f.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->f.motion_val[0][mot_index][0] =
  514. s->mv[0][0][0] = mv_predictor[j][0];
  515. s->cur_pic->f.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->f.motion_val[0][mot_index + i + j * mot_stride][0] = s->mv[0][0][0];
  556. s->cur_pic->f.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 || !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. // prevent dsp.sad() check, that requires access to the image
  594. if (CONFIG_MPEG_XVMC_DECODER &&
  595. s->avctx->xvmc_acceleration &&
  596. s->cur_pic->f.pict_type == AV_PICTURE_TYPE_I)
  597. return 1;
  598. skip_amount = FFMAX(undamaged_count / 50, 1); // check only up to 50 MBs
  599. is_intra_likely = 0;
  600. j = 0;
  601. for (mb_y = 0; mb_y < s->mb_height - 1; mb_y++) {
  602. for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
  603. int error;
  604. const int mb_xy = mb_x + mb_y * s->mb_stride;
  605. error = s->error_status_table[mb_xy];
  606. if ((error & ER_DC_ERROR) && (error & ER_MV_ERROR))
  607. continue; // skip damaged
  608. j++;
  609. // skip a few to speed things up
  610. if ((j % skip_amount) != 0)
  611. continue;
  612. if (s->cur_pic->f.pict_type == AV_PICTURE_TYPE_I) {
  613. int *linesize = s->cur_pic->f.linesize;
  614. uint8_t *mb_ptr = s->cur_pic->f.data[0] +
  615. mb_x * 16 + mb_y * 16 * linesize[0];
  616. uint8_t *last_mb_ptr = s->last_pic->f.data[0] +
  617. mb_x * 16 + mb_y * 16 * linesize[0];
  618. if (s->avctx->codec_id == AV_CODEC_ID_H264) {
  619. // FIXME
  620. } else {
  621. ff_thread_await_progress(&s->last_pic->f, mb_y, 0);
  622. }
  623. is_intra_likely += s->dsp->sad[0](NULL, last_mb_ptr, mb_ptr,
  624. linesize[0], 16);
  625. is_intra_likely -= s->dsp->sad[0](NULL, last_mb_ptr,
  626. last_mb_ptr + linesize[0] * 16,
  627. linesize[0], 16);
  628. } else {
  629. if (IS_INTRA(s->cur_pic->f.mb_type[mb_xy]))
  630. is_intra_likely++;
  631. else
  632. is_intra_likely--;
  633. }
  634. }
  635. }
  636. return is_intra_likely > 0;
  637. }
  638. void ff_er_frame_start(ERContext *s)
  639. {
  640. if (!s->avctx->err_recognition)
  641. return;
  642. memset(s->error_status_table, ER_MB_ERROR | VP_START | ER_MB_END,
  643. s->mb_stride * s->mb_height * sizeof(uint8_t));
  644. s->error_count = 3 * s->mb_num;
  645. s->error_occurred = 0;
  646. }
  647. /**
  648. * Add a slice.
  649. * @param endx x component of the last macroblock, can be -1
  650. * for the last of the previous line
  651. * @param status the status at the end (ER_MV_END, ER_AC_ERROR, ...), it is
  652. * assumed that no earlier end or error of the same type occurred
  653. */
  654. void ff_er_add_slice(ERContext *s, int startx, int starty,
  655. int endx, int endy, int status)
  656. {
  657. const int start_i = av_clip(startx + starty * s->mb_width, 0, s->mb_num - 1);
  658. const int end_i = av_clip(endx + endy * s->mb_width, 0, s->mb_num);
  659. const int start_xy = s->mb_index2xy[start_i];
  660. const int end_xy = s->mb_index2xy[end_i];
  661. int mask = -1;
  662. if (s->avctx->hwaccel)
  663. return;
  664. if (start_i > end_i || start_xy > end_xy) {
  665. av_log(s->avctx, AV_LOG_ERROR,
  666. "internal error, slice end before start\n");
  667. return;
  668. }
  669. if (!s->avctx->err_recognition)
  670. return;
  671. mask &= ~VP_START;
  672. if (status & (ER_AC_ERROR | ER_AC_END)) {
  673. mask &= ~(ER_AC_ERROR | ER_AC_END);
  674. s->error_count -= end_i - start_i + 1;
  675. }
  676. if (status & (ER_DC_ERROR | ER_DC_END)) {
  677. mask &= ~(ER_DC_ERROR | ER_DC_END);
  678. s->error_count -= end_i - start_i + 1;
  679. }
  680. if (status & (ER_MV_ERROR | ER_MV_END)) {
  681. mask &= ~(ER_MV_ERROR | ER_MV_END);
  682. s->error_count -= end_i - start_i + 1;
  683. }
  684. if (status & ER_MB_ERROR) {
  685. s->error_occurred = 1;
  686. s->error_count = INT_MAX;
  687. }
  688. if (mask == ~0x7F) {
  689. memset(&s->error_status_table[start_xy], 0,
  690. (end_xy - start_xy) * sizeof(uint8_t));
  691. } else {
  692. int i;
  693. for (i = start_xy; i < end_xy; i++)
  694. s->error_status_table[i] &= mask;
  695. }
  696. if (end_i == s->mb_num)
  697. s->error_count = INT_MAX;
  698. else {
  699. s->error_status_table[end_xy] &= mask;
  700. s->error_status_table[end_xy] |= status;
  701. }
  702. s->error_status_table[start_xy] |= VP_START;
  703. if (start_xy > 0 && s->avctx->thread_count <= 1 &&
  704. s->avctx->skip_top * s->mb_width < start_i) {
  705. int prev_status = s->error_status_table[s->mb_index2xy[start_i - 1]];
  706. prev_status &= ~ VP_START;
  707. if (prev_status != (ER_MV_END | ER_DC_END | ER_AC_END))
  708. s->error_count = INT_MAX;
  709. }
  710. }
  711. void ff_er_frame_end(ERContext *s)
  712. {
  713. int *linesize = s->cur_pic->f.linesize;
  714. int i, mb_x, mb_y, error, error_type, dc_error, mv_error, ac_error;
  715. int distance;
  716. int threshold_part[4] = { 100, 100, 100 };
  717. int threshold = 50;
  718. int is_intra_likely;
  719. int size = s->b8_stride * 2 * s->mb_height;
  720. /* We do not support ER of field pictures yet,
  721. * though it should not crash if enabled. */
  722. if (!s->avctx->err_recognition || s->error_count == 0 ||
  723. s->avctx->hwaccel ||
  724. s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU ||
  725. !s->cur_pic || s->cur_pic->field_picture ||
  726. s->error_count == 3 * s->mb_width *
  727. (s->avctx->skip_top + s->avctx->skip_bottom)) {
  728. return;
  729. };
  730. if (s->cur_pic->f.motion_val[0] == NULL) {
  731. av_log(s->avctx, AV_LOG_ERROR, "Warning MVs not available\n");
  732. for (i = 0; i < 2; i++) {
  733. s->cur_pic->f.ref_index[i] = av_mallocz(s->mb_stride * s->mb_height * 4 * sizeof(uint8_t));
  734. s->cur_pic->motion_val_base[i] = av_mallocz((size + 4) * 2 * sizeof(uint16_t));
  735. s->cur_pic->f.motion_val[i] = s->cur_pic->motion_val_base[i] + 4;
  736. }
  737. s->cur_pic->f.motion_subsample_log2 = 3;
  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->f.mb_type[mb_xy] = MB_TYPE_INTRA4x4;
  868. else
  869. s->cur_pic->f.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 && s->last_pic->f.data[0]) &&
  873. !(s->next_pic && 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->f.mb_type[mb_xy]))
  877. s->cur_pic->f.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->f.mb_type[mb_xy];
  884. const int dir = !(s->last_pic && 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->f.motion_val[dir][mb_index + (j & 1) + (j >> 1) * s->b8_stride][0];
  900. s->mv[0][j][1] = s->cur_pic->f.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->f.motion_val[dir][mb_x * 2 + mb_y * 2 * s->b8_stride][0];
  905. s->mv[0][0][1] = s->cur_pic->f.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->f.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 && s->last_pic->f.data[0]))
  927. mv_dir &= ~MV_DIR_FORWARD;
  928. if (!(s->next_pic && 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->f, mb_y, 0);
  934. s->mv[0][0][0] = s->next_pic->f.motion_val[0][xy][0] * time_pb / time_pp;
  935. s->mv[0][0][1] = s->next_pic->f.motion_val[0][xy][1] * time_pb / time_pp;
  936. s->mv[1][0][0] = s->next_pic->f.motion_val[0][xy][0] * (time_pb - time_pp) / time_pp;
  937. s->mv[1][0][1] = s->next_pic->f.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. /* the filters below are not XvMC compatible, skip them */
  951. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
  952. goto ec_clean;
  953. /* fill DC for inter blocks */
  954. for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
  955. for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
  956. int dc, dcu, dcv, y, n;
  957. int16_t *dc_ptr;
  958. uint8_t *dest_y, *dest_cb, *dest_cr;
  959. const int mb_xy = mb_x + mb_y * s->mb_stride;
  960. const int mb_type = s->cur_pic->f.mb_type[mb_xy];
  961. error = s->error_status_table[mb_xy];
  962. if (IS_INTRA(mb_type) && s->partitioned_frame)
  963. continue;
  964. // if (error & ER_MV_ERROR)
  965. // continue; // inter data damaged FIXME is this good?
  966. dest_y = s->cur_pic->f.data[0] + mb_x * 16 + mb_y * 16 * linesize[0];
  967. dest_cb = s->cur_pic->f.data[1] + mb_x * 8 + mb_y * 8 * linesize[1];
  968. dest_cr = s->cur_pic->f.data[2] + mb_x * 8 + mb_y * 8 * linesize[2];
  969. dc_ptr = &s->dc_val[0][mb_x * 2 + mb_y * 2 * s->b8_stride];
  970. for (n = 0; n < 4; n++) {
  971. dc = 0;
  972. for (y = 0; y < 8; y++) {
  973. int x;
  974. for (x = 0; x < 8; x++)
  975. dc += dest_y[x + (n & 1) * 8 +
  976. (y + (n >> 1) * 8) * linesize[0]];
  977. }
  978. dc_ptr[(n & 1) + (n >> 1) * s->b8_stride] = (dc + 4) >> 3;
  979. }
  980. dcu = dcv = 0;
  981. for (y = 0; y < 8; y++) {
  982. int x;
  983. for (x = 0; x < 8; x++) {
  984. dcu += dest_cb[x + y * linesize[1]];
  985. dcv += dest_cr[x + y * linesize[2]];
  986. }
  987. }
  988. s->dc_val[1][mb_x + mb_y * s->mb_stride] = (dcu + 4) >> 3;
  989. s->dc_val[2][mb_x + mb_y * s->mb_stride] = (dcv + 4) >> 3;
  990. }
  991. }
  992. /* guess DC for damaged blocks */
  993. guess_dc(s, s->dc_val[0], s->mb_width * 2, s->mb_height * 2, s->b8_stride, 1);
  994. guess_dc(s, s->dc_val[1], s->mb_width, s->mb_height, s->mb_stride, 0);
  995. guess_dc(s, s->dc_val[2], s->mb_width, s->mb_height, s->mb_stride, 0);
  996. /* filter luma DC */
  997. filter181(s->dc_val[0], s->mb_width * 2, s->mb_height * 2, s->b8_stride);
  998. /* render DC only intra */
  999. for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
  1000. for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
  1001. uint8_t *dest_y, *dest_cb, *dest_cr;
  1002. const int mb_xy = mb_x + mb_y * s->mb_stride;
  1003. const int mb_type = s->cur_pic->f.mb_type[mb_xy];
  1004. error = s->error_status_table[mb_xy];
  1005. if (IS_INTER(mb_type))
  1006. continue;
  1007. if (!(error & ER_AC_ERROR))
  1008. continue; // undamaged
  1009. dest_y = s->cur_pic->f.data[0] + mb_x * 16 + mb_y * 16 * linesize[0];
  1010. dest_cb = s->cur_pic->f.data[1] + mb_x * 8 + mb_y * 8 * linesize[1];
  1011. dest_cr = s->cur_pic->f.data[2] + mb_x * 8 + mb_y * 8 * linesize[2];
  1012. put_dc(s, dest_y, dest_cb, dest_cr, mb_x, mb_y);
  1013. }
  1014. }
  1015. if (s->avctx->error_concealment & FF_EC_DEBLOCK) {
  1016. /* filter horizontal block boundaries */
  1017. h_block_filter(s, s->cur_pic->f.data[0], s->mb_width * 2,
  1018. s->mb_height * 2, linesize[0], 1);
  1019. h_block_filter(s, s->cur_pic->f.data[1], s->mb_width,
  1020. s->mb_height, linesize[1], 0);
  1021. h_block_filter(s, s->cur_pic->f.data[2], s->mb_width,
  1022. s->mb_height, linesize[2], 0);
  1023. /* filter vertical block boundaries */
  1024. v_block_filter(s, s->cur_pic->f.data[0], s->mb_width * 2,
  1025. s->mb_height * 2, linesize[0], 1);
  1026. v_block_filter(s, s->cur_pic->f.data[1], s->mb_width,
  1027. s->mb_height, linesize[1], 0);
  1028. v_block_filter(s, s->cur_pic->f.data[2], s->mb_width,
  1029. s->mb_height, linesize[2], 0);
  1030. }
  1031. ec_clean:
  1032. /* clean a few tables */
  1033. for (i = 0; i < s->mb_num; i++) {
  1034. const int mb_xy = s->mb_index2xy[i];
  1035. int error = s->error_status_table[mb_xy];
  1036. if (s->cur_pic->f.pict_type != AV_PICTURE_TYPE_B &&
  1037. (error & (ER_DC_ERROR | ER_MV_ERROR | ER_AC_ERROR))) {
  1038. s->mbskip_table[mb_xy] = 0;
  1039. }
  1040. s->mbintra_table[mb_xy] = 1;
  1041. }
  1042. s->cur_pic = NULL;
  1043. s->next_pic = NULL;
  1044. s->last_pic = NULL;
  1045. }