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.

1366 lines
51KB

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