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.

1367 lines
52KB

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