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, 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. #define MV_FROZEN 8
  338. #define MV_CHANGED 4
  339. #define MV_UNCHANGED 2
  340. #define MV_LISTED 1
  341. 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)
  342. {
  343. if (fixed[mb_xy])
  344. return;
  345. fixed[mb_xy] = MV_LISTED;
  346. blocklist[ *blocklist_length ][0] = mb_x;
  347. blocklist[(*blocklist_length)++][1] = mb_y;
  348. }
  349. static void guess_mv(ERContext *s)
  350. {
  351. int (*blocklist)[2], (*next_blocklist)[2];
  352. uint8_t *fixed;
  353. const int mb_stride = s->mb_stride;
  354. const int mb_width = s->mb_width;
  355. int mb_height = s->mb_height;
  356. int i, depth, num_avail;
  357. int mb_x, mb_y, mot_step, mot_stride;
  358. int blocklist_length, next_blocklist_length;
  359. if (s->last_pic.f && s->last_pic.f->data[0])
  360. mb_height = FFMIN(mb_height, (s->last_pic.f->height+15)>>4);
  361. if (s->next_pic.f && s->next_pic.f->data[0])
  362. mb_height = FFMIN(mb_height, (s->next_pic.f->height+15)>>4);
  363. blocklist = s->er_temp_buffer;
  364. next_blocklist = (s->er_temp_buffer + 2 * sizeof(int) * s->mb_stride * s->mb_height);
  365. fixed = s->er_temp_buffer + 4 * sizeof(int) * s->mb_stride * s->mb_height;
  366. set_mv_strides(s, &mot_step, &mot_stride);
  367. num_avail = 0;
  368. if (s->last_pic.motion_val[0])
  369. ff_thread_await_progress(s->last_pic.tf, mb_height-1, 0);
  370. for (i = 0; i < mb_width * mb_height; i++) {
  371. const int mb_xy = s->mb_index2xy[i];
  372. int f = 0;
  373. int error = s->error_status_table[mb_xy];
  374. if (IS_INTRA(s->cur_pic.mb_type[mb_xy]))
  375. f = MV_FROZEN; // intra // FIXME check
  376. if (!(error & ER_MV_ERROR))
  377. f = MV_FROZEN; // inter with undamaged MV
  378. fixed[mb_xy] = f;
  379. if (f == MV_FROZEN)
  380. num_avail++;
  381. else if(s->last_pic.f->data[0] && s->last_pic.motion_val[0]){
  382. const int mb_y= mb_xy / s->mb_stride;
  383. const int mb_x= mb_xy % s->mb_stride;
  384. const int mot_index= (mb_x + mb_y*mot_stride) * mot_step;
  385. s->cur_pic.motion_val[0][mot_index][0]= s->last_pic.motion_val[0][mot_index][0];
  386. s->cur_pic.motion_val[0][mot_index][1]= s->last_pic.motion_val[0][mot_index][1];
  387. s->cur_pic.ref_index[0][4*mb_xy] = s->last_pic.ref_index[0][4*mb_xy];
  388. }
  389. }
  390. if ((!(s->avctx->error_concealment&FF_EC_GUESS_MVS)) ||
  391. num_avail <= mb_width / 2) {
  392. for (mb_y = 0; mb_y < 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_dir = (s->last_pic.f && s->last_pic.f->data[0]) ? MV_DIR_FORWARD : MV_DIR_BACKWARD;
  396. if (IS_INTRA(s->cur_pic.mb_type[mb_xy]))
  397. continue;
  398. if (!(s->error_status_table[mb_xy] & ER_MV_ERROR))
  399. continue;
  400. s->mv[0][0][0] = 0;
  401. s->mv[0][0][1] = 0;
  402. s->decode_mb(s->opaque, 0, mv_dir, MV_TYPE_16X16, &s->mv,
  403. mb_x, mb_y, 0, 0);
  404. }
  405. }
  406. return;
  407. }
  408. blocklist_length = 0;
  409. for (mb_y = 0; mb_y < mb_height; mb_y++) {
  410. for (mb_x = 0; mb_x < mb_width; mb_x++) {
  411. const int mb_xy = mb_x + mb_y * mb_stride;
  412. if (fixed[mb_xy] == MV_FROZEN) {
  413. if (mb_x) add_blocklist(blocklist, &blocklist_length, fixed, mb_x - 1, mb_y, mb_xy - 1);
  414. if (mb_y) add_blocklist(blocklist, &blocklist_length, fixed, mb_x, mb_y - 1, mb_xy - mb_stride);
  415. if (mb_x+1 < mb_width) add_blocklist(blocklist, &blocklist_length, fixed, mb_x + 1, mb_y, mb_xy + 1);
  416. if (mb_y+1 < mb_height) add_blocklist(blocklist, &blocklist_length, fixed, mb_x, mb_y + 1, mb_xy + mb_stride);
  417. }
  418. }
  419. }
  420. for (depth = 0; ; depth++) {
  421. int changed, pass, none_left;
  422. int blocklist_index;
  423. none_left = 1;
  424. changed = 1;
  425. for (pass = 0; (changed || pass < 2) && pass < 10; pass++) {
  426. int score_sum = 0;
  427. changed = 0;
  428. for (blocklist_index = 0; blocklist_index < blocklist_length; blocklist_index++) {
  429. const int mb_x = blocklist[blocklist_index][0];
  430. const int mb_y = blocklist[blocklist_index][1];
  431. const int mb_xy = mb_x + mb_y * mb_stride;
  432. int mv_predictor[8][2];
  433. int ref[8];
  434. int pred_count;
  435. int j;
  436. int best_score;
  437. int best_pred;
  438. int mot_index;
  439. int prev_x, prev_y, prev_ref;
  440. if ((mb_x ^ mb_y ^ pass) & 1)
  441. continue;
  442. av_assert2(fixed[mb_xy] != MV_FROZEN);
  443. av_assert1(!IS_INTRA(s->cur_pic.mb_type[mb_xy]));
  444. av_assert1(s->last_pic.f && s->last_pic.f->data[0]);
  445. j = 0;
  446. if (mb_x > 0)
  447. j |= fixed[mb_xy - 1];
  448. if (mb_x + 1 < mb_width)
  449. j |= fixed[mb_xy + 1];
  450. if (mb_y > 0)
  451. j |= fixed[mb_xy - mb_stride];
  452. if (mb_y + 1 < mb_height)
  453. j |= fixed[mb_xy + mb_stride];
  454. av_assert2(j & MV_FROZEN);
  455. if (!(j & MV_CHANGED) && pass > 1)
  456. continue;
  457. none_left = 0;
  458. pred_count = 0;
  459. mot_index = (mb_x + mb_y * mot_stride) * mot_step;
  460. if (mb_x > 0 && fixed[mb_xy - 1] > 1) {
  461. mv_predictor[pred_count][0] =
  462. s->cur_pic.motion_val[0][mot_index - mot_step][0];
  463. mv_predictor[pred_count][1] =
  464. s->cur_pic.motion_val[0][mot_index - mot_step][1];
  465. ref[pred_count] =
  466. s->cur_pic.ref_index[0][4 * (mb_xy - 1)];
  467. pred_count++;
  468. }
  469. if (mb_x + 1 < mb_width && fixed[mb_xy + 1] > 1) {
  470. mv_predictor[pred_count][0] =
  471. s->cur_pic.motion_val[0][mot_index + mot_step][0];
  472. mv_predictor[pred_count][1] =
  473. s->cur_pic.motion_val[0][mot_index + mot_step][1];
  474. ref[pred_count] =
  475. s->cur_pic.ref_index[0][4 * (mb_xy + 1)];
  476. pred_count++;
  477. }
  478. if (mb_y > 0 && fixed[mb_xy - mb_stride] > 1) {
  479. mv_predictor[pred_count][0] =
  480. s->cur_pic.motion_val[0][mot_index - mot_stride * mot_step][0];
  481. mv_predictor[pred_count][1] =
  482. s->cur_pic.motion_val[0][mot_index - mot_stride * mot_step][1];
  483. ref[pred_count] =
  484. s->cur_pic.ref_index[0][4 * (mb_xy - s->mb_stride)];
  485. pred_count++;
  486. }
  487. if (mb_y + 1<mb_height && fixed[mb_xy + mb_stride] > 1) {
  488. mv_predictor[pred_count][0] =
  489. s->cur_pic.motion_val[0][mot_index + mot_stride * mot_step][0];
  490. mv_predictor[pred_count][1] =
  491. s->cur_pic.motion_val[0][mot_index + mot_stride * mot_step][1];
  492. ref[pred_count] =
  493. s->cur_pic.ref_index[0][4 * (mb_xy + s->mb_stride)];
  494. pred_count++;
  495. }
  496. if (pred_count == 0)
  497. continue;
  498. if (pred_count > 1) {
  499. int sum_x = 0, sum_y = 0, sum_r = 0;
  500. int max_x, max_y, min_x, min_y, max_r, min_r;
  501. for (j = 0; j < pred_count; j++) {
  502. sum_x += mv_predictor[j][0];
  503. sum_y += mv_predictor[j][1];
  504. sum_r += ref[j];
  505. if (j && ref[j] != ref[j - 1])
  506. goto skip_mean_and_median;
  507. }
  508. /* mean */
  509. mv_predictor[pred_count][0] = sum_x / j;
  510. mv_predictor[pred_count][1] = sum_y / j;
  511. ref[pred_count] = sum_r / j;
  512. /* median */
  513. if (pred_count >= 3) {
  514. min_y = min_x = min_r = 99999;
  515. max_y = max_x = max_r = -99999;
  516. } else {
  517. min_x = min_y = max_x = max_y = min_r = max_r = 0;
  518. }
  519. for (j = 0; j < pred_count; j++) {
  520. max_x = FFMAX(max_x, mv_predictor[j][0]);
  521. max_y = FFMAX(max_y, mv_predictor[j][1]);
  522. max_r = FFMAX(max_r, ref[j]);
  523. min_x = FFMIN(min_x, mv_predictor[j][0]);
  524. min_y = FFMIN(min_y, mv_predictor[j][1]);
  525. min_r = FFMIN(min_r, ref[j]);
  526. }
  527. mv_predictor[pred_count + 1][0] = sum_x - max_x - min_x;
  528. mv_predictor[pred_count + 1][1] = sum_y - max_y - min_y;
  529. ref[pred_count + 1] = sum_r - max_r - min_r;
  530. if (pred_count == 4) {
  531. mv_predictor[pred_count + 1][0] /= 2;
  532. mv_predictor[pred_count + 1][1] /= 2;
  533. ref[pred_count + 1] /= 2;
  534. }
  535. pred_count += 2;
  536. }
  537. skip_mean_and_median:
  538. /* zero MV */
  539. mv_predictor[pred_count][0] =
  540. mv_predictor[pred_count][1] =
  541. ref[pred_count] = 0;
  542. pred_count++;
  543. prev_x = s->cur_pic.motion_val[0][mot_index][0];
  544. prev_y = s->cur_pic.motion_val[0][mot_index][1];
  545. prev_ref = s->cur_pic.ref_index[0][4 * mb_xy];
  546. /* last MV */
  547. mv_predictor[pred_count][0] = prev_x;
  548. mv_predictor[pred_count][1] = prev_y;
  549. ref[pred_count] = prev_ref;
  550. pred_count++;
  551. best_pred = 0;
  552. best_score = 256 * 256 * 256 * 64;
  553. for (j = 0; j < pred_count; j++) {
  554. int *linesize = s->cur_pic.f->linesize;
  555. int score = 0;
  556. uint8_t *src = s->cur_pic.f->data[0] +
  557. mb_x * 16 + mb_y * 16 * linesize[0];
  558. s->cur_pic.motion_val[0][mot_index][0] =
  559. s->mv[0][0][0] = mv_predictor[j][0];
  560. s->cur_pic.motion_val[0][mot_index][1] =
  561. s->mv[0][0][1] = mv_predictor[j][1];
  562. // predictor intra or otherwise not available
  563. if (ref[j] < 0)
  564. continue;
  565. s->decode_mb(s->opaque, ref[j], MV_DIR_FORWARD,
  566. MV_TYPE_16X16, &s->mv, mb_x, mb_y, 0, 0);
  567. if (mb_x > 0 && fixed[mb_xy - 1] > 1) {
  568. int k;
  569. for (k = 0; k < 16; k++)
  570. score += FFABS(src[k * linesize[0] - 1] -
  571. src[k * linesize[0]]);
  572. }
  573. if (mb_x + 1 < mb_width && fixed[mb_xy + 1] > 1) {
  574. int k;
  575. for (k = 0; k < 16; k++)
  576. score += FFABS(src[k * linesize[0] + 15] -
  577. src[k * linesize[0] + 16]);
  578. }
  579. if (mb_y > 0 && fixed[mb_xy - mb_stride] > 1) {
  580. int k;
  581. for (k = 0; k < 16; k++)
  582. score += FFABS(src[k - linesize[0]] - src[k]);
  583. }
  584. if (mb_y + 1 < mb_height && fixed[mb_xy + mb_stride] > 1) {
  585. int k;
  586. for (k = 0; k < 16; k++)
  587. score += FFABS(src[k + linesize[0] * 15] -
  588. src[k + linesize[0] * 16]);
  589. }
  590. if (score <= best_score) { // <= will favor the last MV
  591. best_score = score;
  592. best_pred = j;
  593. }
  594. }
  595. score_sum += best_score;
  596. s->mv[0][0][0] = mv_predictor[best_pred][0];
  597. s->mv[0][0][1] = mv_predictor[best_pred][1];
  598. for (i = 0; i < mot_step; i++)
  599. for (j = 0; j < mot_step; j++) {
  600. s->cur_pic.motion_val[0][mot_index + i + j * mot_stride][0] = s->mv[0][0][0];
  601. s->cur_pic.motion_val[0][mot_index + i + j * mot_stride][1] = s->mv[0][0][1];
  602. }
  603. s->decode_mb(s->opaque, ref[best_pred], MV_DIR_FORWARD,
  604. MV_TYPE_16X16, &s->mv, mb_x, mb_y, 0, 0);
  605. if (s->mv[0][0][0] != prev_x || s->mv[0][0][1] != prev_y) {
  606. fixed[mb_xy] = MV_CHANGED;
  607. changed++;
  608. } else
  609. fixed[mb_xy] = MV_UNCHANGED;
  610. }
  611. }
  612. if (none_left)
  613. return;
  614. next_blocklist_length = 0;
  615. for (blocklist_index = 0; blocklist_index < blocklist_length; blocklist_index++) {
  616. const int mb_x = blocklist[blocklist_index][0];
  617. const int mb_y = blocklist[blocklist_index][1];
  618. const int mb_xy = mb_x + mb_y * mb_stride;
  619. if (fixed[mb_xy] & (MV_CHANGED|MV_UNCHANGED|MV_FROZEN)) {
  620. fixed[mb_xy] = MV_FROZEN;
  621. if (mb_x > 0)
  622. add_blocklist(next_blocklist, &next_blocklist_length, fixed, mb_x - 1, mb_y, mb_xy - 1);
  623. if (mb_y > 0)
  624. add_blocklist(next_blocklist, &next_blocklist_length, fixed, mb_x, mb_y - 1, mb_xy - mb_stride);
  625. if (mb_x + 1 < mb_width)
  626. add_blocklist(next_blocklist, &next_blocklist_length, fixed, mb_x + 1, mb_y, mb_xy + 1);
  627. if (mb_y + 1 < mb_height)
  628. add_blocklist(next_blocklist, &next_blocklist_length, fixed, mb_x, mb_y + 1, mb_xy + mb_stride);
  629. }
  630. }
  631. av_assert0(next_blocklist_length <= mb_height * mb_width);
  632. FFSWAP(int , blocklist_length, next_blocklist_length);
  633. FFSWAP(void*, blocklist, next_blocklist);
  634. }
  635. }
  636. static int is_intra_more_likely(ERContext *s)
  637. {
  638. int is_intra_likely, i, j, undamaged_count, skip_amount, mb_x, mb_y;
  639. if (!s->last_pic.f || !s->last_pic.f->data[0])
  640. return 1; // no previous frame available -> use spatial prediction
  641. if (s->avctx->error_concealment & FF_EC_FAVOR_INTER)
  642. return 0;
  643. undamaged_count = 0;
  644. for (i = 0; i < s->mb_num; i++) {
  645. const int mb_xy = s->mb_index2xy[i];
  646. const int error = s->error_status_table[mb_xy];
  647. if (!((error & ER_DC_ERROR) && (error & ER_MV_ERROR)))
  648. undamaged_count++;
  649. }
  650. if (undamaged_count < 5)
  651. return 0; // almost all MBs damaged -> use temporal prediction
  652. // prevent dsp.sad() check, that requires access to the image
  653. if (CONFIG_XVMC &&
  654. s->avctx->hwaccel && s->avctx->hwaccel->decode_mb &&
  655. s->cur_pic.f->pict_type == AV_PICTURE_TYPE_I)
  656. return 1;
  657. skip_amount = FFMAX(undamaged_count / 50, 1); // check only up to 50 MBs
  658. is_intra_likely = 0;
  659. j = 0;
  660. for (mb_y = 0; mb_y < s->mb_height - 1; mb_y++) {
  661. for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
  662. int error;
  663. const int mb_xy = mb_x + mb_y * s->mb_stride;
  664. error = s->error_status_table[mb_xy];
  665. if ((error & ER_DC_ERROR) && (error & ER_MV_ERROR))
  666. continue; // skip damaged
  667. j++;
  668. // skip a few to speed things up
  669. if ((j % skip_amount) != 0)
  670. continue;
  671. if (s->cur_pic.f->pict_type == AV_PICTURE_TYPE_I) {
  672. int *linesize = s->cur_pic.f->linesize;
  673. uint8_t *mb_ptr = s->cur_pic.f->data[0] +
  674. mb_x * 16 + mb_y * 16 * linesize[0];
  675. uint8_t *last_mb_ptr = s->last_pic.f->data[0] +
  676. mb_x * 16 + mb_y * 16 * linesize[0];
  677. if (s->avctx->codec_id == AV_CODEC_ID_H264) {
  678. // FIXME
  679. } else {
  680. ff_thread_await_progress(s->last_pic.tf, mb_y, 0);
  681. }
  682. is_intra_likely += s->mecc.sad[0](NULL, last_mb_ptr, mb_ptr,
  683. linesize[0], 16);
  684. // FIXME need await_progress() here
  685. is_intra_likely -= s->mecc.sad[0](NULL, last_mb_ptr,
  686. last_mb_ptr + linesize[0] * 16,
  687. linesize[0], 16);
  688. } else {
  689. if (IS_INTRA(s->cur_pic.mb_type[mb_xy]))
  690. is_intra_likely++;
  691. else
  692. is_intra_likely--;
  693. }
  694. }
  695. }
  696. // av_log(NULL, AV_LOG_ERROR, "is_intra_likely: %d type:%d\n", is_intra_likely, s->pict_type);
  697. return is_intra_likely > 0;
  698. }
  699. void ff_er_frame_start(ERContext *s)
  700. {
  701. if (!s->avctx->error_concealment)
  702. return;
  703. if (!s->mecc_inited) {
  704. ff_me_cmp_init(&s->mecc, s->avctx);
  705. s->mecc_inited = 1;
  706. }
  707. memset(s->error_status_table, ER_MB_ERROR | VP_START | ER_MB_END,
  708. s->mb_stride * s->mb_height * sizeof(uint8_t));
  709. s->error_count = 3 * s->mb_num;
  710. s->error_occurred = 0;
  711. }
  712. static int er_supported(ERContext *s)
  713. {
  714. if(s->avctx->hwaccel && s->avctx->hwaccel->decode_slice ||
  715. #if FF_API_CAP_VDPAU
  716. s->avctx->codec->capabilities&AV_CODEC_CAP_HWACCEL_VDPAU ||
  717. #endif
  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. avpriv_atomic_int_add_and_fetch(&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. avpriv_atomic_int_add_and_fetch(&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. avpriv_atomic_int_add_and_fetch(&s->error_count, start_i - end_i - 1);
  760. }
  761. if (status & ER_MB_ERROR) {
  762. s->error_occurred = 1;
  763. avpriv_atomic_int_set(&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. avpriv_atomic_int_set(&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. avpriv_atomic_int_set(&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 || s->error_count == 0 ||
  802. s->avctx->lowres ||
  803. !er_supported(s) ||
  804. 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. && 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. }