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.

1326 lines
51KB

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