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.

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