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.

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