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.

1294 lines
49KB

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