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.

1330 lines
50KB

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