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.

1248 lines
47KB

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