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.

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