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.

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