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.

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