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.

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