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.

1310 lines
50KB

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