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.

1275 lines
49KB

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