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.

893 lines
33KB

  1. /*
  2. * Error resilience / concealment
  3. *
  4. * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. /**
  21. * @file error_resilience.c
  22. * Error resilience / concealment.
  23. */
  24. #include "avcodec.h"
  25. #include "dsputil.h"
  26. #include "mpegvideo.h"
  27. /**
  28. * replaces the current MB with a flat dc only version.
  29. */
  30. static void put_dc(MpegEncContext *s, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, int mb_x, int mb_y)
  31. {
  32. int dc, dcu, dcv, y, i;
  33. for(i=0; i<4; i++){
  34. dc= s->dc_val[0][mb_x*2+1 + (i&1) + (mb_y*2+1 + (i>>1))*(s->mb_width*2+2)];
  35. if(dc<0) dc=0;
  36. else if(dc>2040) dc=2040;
  37. for(y=0; y<8; y++){
  38. int x;
  39. for(x=0; x<8; x++){
  40. dest_y[x + (i&1)*8 + (y + (i>>1)*8)*s->linesize]= dc/8;
  41. }
  42. }
  43. }
  44. dcu = s->dc_val[1][mb_x+1 + (mb_y+1)*(s->mb_width+2)];
  45. dcv = s->dc_val[2][mb_x+1 + (mb_y+1)*(s->mb_width+2)];
  46. if (dcu<0 ) dcu=0;
  47. else if(dcu>2040) dcu=2040;
  48. if (dcv<0 ) dcv=0;
  49. else if(dcv>2040) dcv=2040;
  50. for(y=0; y<8; y++){
  51. int x;
  52. for(x=0; x<8; x++){
  53. dest_cb[x + y*(s->uvlinesize)]= dcu/8;
  54. dest_cr[x + y*(s->uvlinesize)]= dcv/8;
  55. }
  56. }
  57. }
  58. static void filter181(int16_t *data, int width, int height, int stride){
  59. int x,y;
  60. /* horizontal filter */
  61. for(y=1; y<height-1; y++){
  62. int prev_dc= data[0 + y*stride];
  63. for(x=1; x<width-1; x++){
  64. int dc;
  65. dc= - prev_dc
  66. + data[x + y*stride]*8
  67. - data[x + 1 + y*stride];
  68. dc= (dc*10923 + 32768)>>16;
  69. prev_dc= data[x + y*stride];
  70. data[x + y*stride]= dc;
  71. }
  72. }
  73. /* vertical filter */
  74. for(x=1; x<width-1; x++){
  75. int prev_dc= data[x];
  76. for(y=1; y<height-1; y++){
  77. int dc;
  78. dc= - prev_dc
  79. + data[x + y *stride]*8
  80. - data[x + (y+1)*stride];
  81. dc= (dc*10923 + 32768)>>16;
  82. prev_dc= data[x + y*stride];
  83. data[x + y*stride]= dc;
  84. }
  85. }
  86. }
  87. /**
  88. * guess the dc of blocks which dont have a undamaged dc
  89. * @param w width in 8 pixel blocks
  90. * @param h height in 8 pixel blocks
  91. */
  92. static void guess_dc(MpegEncContext *s, int16_t *dc, int w, int h, int stride, int is_luma){
  93. int b_x, b_y;
  94. for(b_y=0; b_y<h; b_y++){
  95. for(b_x=0; b_x<w; b_x++){
  96. int color[4]={1024,1024,1024,1024};
  97. int distance[4]={9999,9999,9999,9999};
  98. int mb_index, error, j;
  99. int64_t guess, weight_sum;
  100. mb_index= (b_x>>is_luma) + (b_y>>is_luma)*s->mb_width;
  101. error= s->error_status_table[mb_index];
  102. if(!(s->mb_type[mb_index]&MB_TYPE_INTRA)) continue; //inter
  103. if(!(error&DC_ERROR)) continue; //dc-ok
  104. /* right block */
  105. for(j=b_x+1; j<w; j++){
  106. int mb_index_j= (j>>is_luma) + (b_y>>is_luma)*s->mb_width;
  107. int error_j= s->error_status_table[mb_index_j];
  108. int intra_j= s->mb_type[mb_index_j]&MB_TYPE_INTRA;
  109. if(intra_j==0 || !(error_j&DC_ERROR)){
  110. color[0]= dc[j + b_y*stride];
  111. distance[0]= j-b_x;
  112. break;
  113. }
  114. }
  115. /* left block */
  116. for(j=b_x-1; j>=0; j--){
  117. int mb_index_j= (j>>is_luma) + (b_y>>is_luma)*s->mb_width;
  118. int error_j= s->error_status_table[mb_index_j];
  119. int intra_j= s->mb_type[mb_index_j]&MB_TYPE_INTRA;
  120. if(intra_j==0 || !(error_j&DC_ERROR)){
  121. color[1]= dc[j + b_y*stride];
  122. distance[1]= b_x-j;
  123. break;
  124. }
  125. }
  126. /* bottom block */
  127. for(j=b_y+1; j<h; j++){
  128. int mb_index_j= (b_x>>is_luma) + (j>>is_luma)*s->mb_width;
  129. int error_j= s->error_status_table[mb_index_j];
  130. int intra_j= s->mb_type[mb_index_j]&MB_TYPE_INTRA;
  131. if(intra_j==0 || !(error_j&DC_ERROR)){
  132. color[2]= dc[b_x + j*stride];
  133. distance[2]= j-b_y;
  134. break;
  135. }
  136. }
  137. /* top block */
  138. for(j=b_y-1; j>=0; j--){
  139. int mb_index_j= (b_x>>is_luma) + (j>>is_luma)*s->mb_width;
  140. int error_j= s->error_status_table[mb_index_j];
  141. int intra_j= s->mb_type[mb_index_j]&MB_TYPE_INTRA;
  142. if(intra_j==0 || !(error_j&DC_ERROR)){
  143. color[3]= dc[b_x + j*stride];
  144. distance[3]= b_y-j;
  145. break;
  146. }
  147. }
  148. weight_sum=0;
  149. guess=0;
  150. for(j=0; j<4; j++){
  151. int64_t weight= 256*256*256*16/distance[j];
  152. guess+= weight*(int64_t)color[j];
  153. weight_sum+= weight;
  154. }
  155. guess= (guess + weight_sum/2) / weight_sum;
  156. dc[b_x + b_y*stride]= guess;
  157. }
  158. }
  159. }
  160. /**
  161. * simple horizontal deblocking filter used for error resilience
  162. * @param w width in 8 pixel blocks
  163. * @param h height in 8 pixel blocks
  164. */
  165. static void h_block_filter(MpegEncContext *s, uint8_t *dst, int w, int h, int stride, int is_luma){
  166. int b_x, b_y;
  167. uint8_t *cm = cropTbl + MAX_NEG_CROP;
  168. for(b_y=0; b_y<h; b_y++){
  169. for(b_x=0; b_x<w-1; b_x++){
  170. int y;
  171. int left_status = s->error_status_table[( b_x >>is_luma) + (b_y>>is_luma)*s->mb_width];
  172. int right_status= s->error_status_table[((b_x+1)>>is_luma) + (b_y>>is_luma)*s->mb_width];
  173. int left_intra= s->mb_type [( b_x >>is_luma) + (b_y>>is_luma)*s->mb_width]&MB_TYPE_INTRA;
  174. int right_intra= s->mb_type [((b_x+1)>>is_luma) + (b_y>>is_luma)*s->mb_width]&MB_TYPE_INTRA;
  175. int left_damage = left_status&(DC_ERROR|AC_ERROR|MV_ERROR);
  176. int right_damage= right_status&(DC_ERROR|AC_ERROR|MV_ERROR);
  177. int offset= b_x*8 + b_y*stride*8;
  178. int16_t *left_mv= s->motion_val[s->block_wrap[0]*((b_y<<(1-is_luma)) + 1) + ( b_x <<(1-is_luma))];
  179. int16_t *right_mv= s->motion_val[s->block_wrap[0]*((b_y<<(1-is_luma)) + 1) + ((b_x+1)<<(1-is_luma))];
  180. if(!(left_damage||right_damage)) continue; // both undamaged
  181. if( (!left_intra) && (!right_intra)
  182. && ABS(left_mv[0]-right_mv[0]) + ABS(left_mv[1]+right_mv[1]) < 2) continue;
  183. for(y=0; y<8; y++){
  184. int a,b,c,d;
  185. a= dst[offset + 7 + y*stride] - dst[offset + 6 + y*stride];
  186. b= dst[offset + 8 + y*stride] - dst[offset + 7 + y*stride];
  187. c= dst[offset + 9 + y*stride] - dst[offset + 8 + y*stride];
  188. d= ABS(b) - ((ABS(a) + ABS(c) + 1)>>1);
  189. d= FFMAX(d, 0);
  190. if(b<0) d= -d;
  191. if(d==0) continue;
  192. if(!(left_damage && right_damage))
  193. d= d*16/9;
  194. if(left_damage){
  195. dst[offset + 7 + y*stride] = cm[dst[offset + 7 + y*stride] + ((d*7)>>4)];
  196. dst[offset + 6 + y*stride] = cm[dst[offset + 6 + y*stride] + ((d*5)>>4)];
  197. dst[offset + 5 + y*stride] = cm[dst[offset + 5 + y*stride] + ((d*3)>>4)];
  198. dst[offset + 4 + y*stride] = cm[dst[offset + 4 + y*stride] + ((d*1)>>4)];
  199. }
  200. if(right_damage){
  201. dst[offset + 8 + y*stride] = cm[dst[offset + 8 + y*stride] - ((d*7)>>4)];
  202. dst[offset + 9 + y*stride] = cm[dst[offset + 9 + y*stride] - ((d*5)>>4)];
  203. dst[offset + 10+ y*stride] = cm[dst[offset +10 + y*stride] - ((d*3)>>4)];
  204. dst[offset + 11+ y*stride] = cm[dst[offset +11 + y*stride] - ((d*1)>>4)];
  205. }
  206. }
  207. }
  208. }
  209. }
  210. /**
  211. * simple vertical deblocking filter used for error resilience
  212. * @param w width in 8 pixel blocks
  213. * @param h height in 8 pixel blocks
  214. */
  215. static void v_block_filter(MpegEncContext *s, uint8_t *dst, int w, int h, int stride, int is_luma){
  216. int b_x, b_y;
  217. uint8_t *cm = cropTbl + MAX_NEG_CROP;
  218. for(b_y=0; b_y<h-1; b_y++){
  219. for(b_x=0; b_x<w; b_x++){
  220. int x;
  221. int top_status = s->error_status_table[(b_x>>is_luma) + ( b_y >>is_luma)*s->mb_width];
  222. int bottom_status= s->error_status_table[(b_x>>is_luma) + ((b_y+1)>>is_luma)*s->mb_width];
  223. int top_intra= s->mb_type [(b_x>>is_luma) + ( b_y >>is_luma)*s->mb_width]&MB_TYPE_INTRA;
  224. int bottom_intra= s->mb_type [(b_x>>is_luma) + ((b_y+1)>>is_luma)*s->mb_width]&MB_TYPE_INTRA;
  225. int top_damage = top_status&(DC_ERROR|AC_ERROR|MV_ERROR);
  226. int bottom_damage= bottom_status&(DC_ERROR|AC_ERROR|MV_ERROR);
  227. int offset= b_x*8 + b_y*stride*8;
  228. int16_t *top_mv= s->motion_val[s->block_wrap[0]*(( b_y <<(1-is_luma)) + 1) + (b_x<<(1-is_luma))];
  229. int16_t *bottom_mv= s->motion_val[s->block_wrap[0]*(((b_y+1)<<(1-is_luma)) + 1) + (b_x<<(1-is_luma))];
  230. if(!(top_damage||bottom_damage)) continue; // both undamaged
  231. if( (!top_intra) && (!bottom_intra)
  232. && ABS(top_mv[0]-bottom_mv[0]) + ABS(top_mv[1]+bottom_mv[1]) < 2) continue;
  233. for(x=0; x<8; x++){
  234. int a,b,c,d;
  235. a= dst[offset + x + 7*stride] - dst[offset + x + 6*stride];
  236. b= dst[offset + x + 8*stride] - dst[offset + x + 7*stride];
  237. c= dst[offset + x + 9*stride] - dst[offset + x + 8*stride];
  238. d= ABS(b) - ((ABS(a) + ABS(c)+1)>>1);
  239. d= FFMAX(d, 0);
  240. if(b<0) d= -d;
  241. if(d==0) continue;
  242. if(!(top_damage && bottom_damage))
  243. d= d*16/9;
  244. if(top_damage){
  245. dst[offset + x + 7*stride] = cm[dst[offset + x + 7*stride] + ((d*7)>>4)];
  246. dst[offset + x + 6*stride] = cm[dst[offset + x + 6*stride] + ((d*5)>>4)];
  247. dst[offset + x + 5*stride] = cm[dst[offset + x + 5*stride] + ((d*3)>>4)];
  248. dst[offset + x + 4*stride] = cm[dst[offset + x + 4*stride] + ((d*1)>>4)];
  249. }
  250. if(bottom_damage){
  251. dst[offset + x + 8*stride] = cm[dst[offset + x + 8*stride] - ((d*7)>>4)];
  252. dst[offset + x + 9*stride] = cm[dst[offset + x + 9*stride] - ((d*5)>>4)];
  253. dst[offset + x + 10*stride] = cm[dst[offset + x + 10*stride] - ((d*3)>>4)];
  254. dst[offset + x + 11*stride] = cm[dst[offset + x + 11*stride] - ((d*1)>>4)];
  255. }
  256. }
  257. }
  258. }
  259. }
  260. static void guess_mv(MpegEncContext *s){
  261. uint8_t fixed[s->mb_num];
  262. #define MV_FROZEN 3
  263. #define MV_CHANGED 2
  264. #define MV_UNCHANGED 1
  265. const int mb_width = s->mb_width;
  266. const int mb_height= s->mb_height;
  267. int i, depth, num_avail;
  268. num_avail=0;
  269. for(i=0; i<s->mb_num; i++){
  270. int f=0;
  271. int error= s->error_status_table[i];
  272. if(s->mb_type[i]&MB_TYPE_INTRA) f=MV_FROZEN; //intra //FIXME check
  273. if(!(error&MV_ERROR)) f=MV_FROZEN; //inter with undamaged MV
  274. fixed[i]= f;
  275. if(f==MV_FROZEN)
  276. num_avail++;
  277. }
  278. if((!(s->avctx->error_concealment&FF_EC_GUESS_MVS)) || num_avail <= mb_width/2){
  279. int mb_x, mb_y;
  280. i= -1;
  281. for(mb_y=0; mb_y<s->mb_height; mb_y++){
  282. for(mb_x=0; mb_x<s->mb_width; mb_x++){
  283. i++;
  284. if(s->mb_type[i]&MB_TYPE_INTRA) continue;
  285. if(!(s->error_status_table[i]&MV_ERROR)) continue;
  286. s->mv_dir = MV_DIR_FORWARD;
  287. s->mb_intra=0;
  288. s->mv_type = MV_TYPE_16X16;
  289. s->mb_skiped=0;
  290. s->dsp.clear_blocks(s->block[0]);
  291. s->mb_x= mb_x;
  292. s->mb_y= mb_y;
  293. s->mv[0][0][0]= 0;
  294. s->mv[0][0][1]= 0;
  295. MPV_decode_mb(s, s->block);
  296. }
  297. }
  298. return;
  299. }
  300. for(depth=0;; depth++){
  301. int changed, pass, none_left;
  302. none_left=1;
  303. changed=1;
  304. for(pass=0; (changed || pass<2) && pass<10; pass++){
  305. int i,mb_x, mb_y;
  306. int score_sum=0;
  307. changed=0;
  308. i= -1;
  309. for(mb_y=0; mb_y<s->mb_height; mb_y++){
  310. for(mb_x=0; mb_x<s->mb_width; mb_x++){
  311. int mv_predictor[8][2]={{0}};
  312. int pred_count=0;
  313. int j;
  314. int best_score=256*256*256*64;
  315. int best_pred=0;
  316. const int mot_stride= mb_width*2+2;
  317. const int mot_index= mb_x*2 + 1 + (mb_y*2+1)*mot_stride;
  318. int prev_x= s->motion_val[mot_index][0];
  319. int prev_y= s->motion_val[mot_index][1];
  320. i++;
  321. if((mb_x^mb_y^pass)&1) continue;
  322. if(fixed[i]==MV_FROZEN) continue;
  323. j=0;
  324. if(mb_x>0 && fixed[i-1 ]==MV_FROZEN) j=1;
  325. if(mb_x+1<mb_width && fixed[i+1 ]==MV_FROZEN) j=1;
  326. if(mb_y>0 && fixed[i-mb_width]==MV_FROZEN) j=1;
  327. if(mb_y+1<mb_height && fixed[i+mb_width]==MV_FROZEN) j=1;
  328. if(j==0) continue;
  329. j=0;
  330. if(mb_x>0 && fixed[i-1 ]==MV_CHANGED) j=1;
  331. if(mb_x+1<mb_width && fixed[i+1 ]==MV_CHANGED) j=1;
  332. if(mb_y>0 && fixed[i-mb_width]==MV_CHANGED) j=1;
  333. if(mb_y+1<mb_height && fixed[i+mb_width]==MV_CHANGED) j=1;
  334. if(j==0 && pass>1) continue;
  335. none_left=0;
  336. if(mb_x>0 && fixed[i-1]){
  337. mv_predictor[pred_count][0]= s->motion_val[mot_index - 2][0];
  338. mv_predictor[pred_count][1]= s->motion_val[mot_index - 2][1];
  339. pred_count++;
  340. }
  341. if(mb_x+1<mb_width && fixed[i+1]){
  342. mv_predictor[pred_count][0]= s->motion_val[mot_index + 2][0];
  343. mv_predictor[pred_count][1]= s->motion_val[mot_index + 2][1];
  344. pred_count++;
  345. }
  346. if(mb_y>0 && fixed[i-mb_width]){
  347. mv_predictor[pred_count][0]= s->motion_val[mot_index - mot_stride*2][0];
  348. mv_predictor[pred_count][1]= s->motion_val[mot_index - mot_stride*2][1];
  349. pred_count++;
  350. }
  351. if(mb_y+1<mb_height && fixed[i+mb_width]){
  352. mv_predictor[pred_count][0]= s->motion_val[mot_index + mot_stride*2][0];
  353. mv_predictor[pred_count][1]= s->motion_val[mot_index + mot_stride*2][1];
  354. pred_count++;
  355. }
  356. if(pred_count==0) continue;
  357. if(pred_count>1){
  358. int sum_x=0, sum_y=0;
  359. int max_x, max_y, min_x, min_y;
  360. for(j=0; j<pred_count; j++){
  361. sum_x+= mv_predictor[j][0];
  362. sum_y+= mv_predictor[j][1];
  363. }
  364. /* mean */
  365. mv_predictor[pred_count][0] = sum_x/j;
  366. mv_predictor[pred_count][1] = sum_y/j;
  367. /* median */
  368. if(pred_count>=3){
  369. min_y= min_x= 99999;
  370. max_y= max_x=-99999;
  371. }else{
  372. min_x=min_y=max_x=max_y=0;
  373. }
  374. for(j=0; j<pred_count; j++){
  375. max_x= FFMAX(max_x, mv_predictor[j][0]);
  376. max_y= FFMAX(max_y, mv_predictor[j][1]);
  377. min_x= FFMIN(min_x, mv_predictor[j][0]);
  378. min_y= FFMIN(min_y, mv_predictor[j][1]);
  379. }
  380. mv_predictor[pred_count+1][0] = sum_x - max_x - min_x;
  381. mv_predictor[pred_count+1][1] = sum_y - max_y - min_y;
  382. if(pred_count==4){
  383. mv_predictor[pred_count+1][0] /= 2;
  384. mv_predictor[pred_count+1][1] /= 2;
  385. }
  386. pred_count+=2;
  387. }
  388. /* zero MV */
  389. pred_count++;
  390. /* last MV */
  391. mv_predictor[pred_count][0]= s->motion_val[mot_index][0];
  392. mv_predictor[pred_count][1]= s->motion_val[mot_index][1];
  393. pred_count++;
  394. s->mv_dir = MV_DIR_FORWARD;
  395. s->mb_intra=0;
  396. s->mv_type = MV_TYPE_16X16;
  397. s->mb_skiped=0;
  398. s->dsp.clear_blocks(s->block[0]);
  399. s->mb_x= mb_x;
  400. s->mb_y= mb_y;
  401. for(j=0; j<pred_count; j++){
  402. int score=0;
  403. uint8_t *src= s->current_picture.data[0] + mb_x*16 + mb_y*16*s->linesize;
  404. s->motion_val[mot_index][0]= s->mv[0][0][0]= mv_predictor[j][0];
  405. s->motion_val[mot_index][1]= s->mv[0][0][1]= mv_predictor[j][1];
  406. MPV_decode_mb(s, s->block);
  407. if(mb_x>0 && fixed[i-1]){
  408. int k;
  409. for(k=0; k<16; k++)
  410. score += ABS(src[k*s->linesize-1 ]-src[k*s->linesize ]);
  411. }
  412. if(mb_x+1<mb_width && fixed[i+1]){
  413. int k;
  414. for(k=0; k<16; k++)
  415. score += ABS(src[k*s->linesize+15]-src[k*s->linesize+16]);
  416. }
  417. if(mb_y>0 && fixed[i-mb_width]){
  418. int k;
  419. for(k=0; k<16; k++)
  420. score += ABS(src[k-s->linesize ]-src[k ]);
  421. }
  422. if(mb_y+1<mb_height && fixed[i+mb_width]){
  423. int k;
  424. for(k=0; k<16; k++)
  425. score += ABS(src[k+s->linesize*15]-src[k+s->linesize*16]);
  426. }
  427. if(score <= best_score){ // <= will favor the last MV
  428. best_score= score;
  429. best_pred= j;
  430. }
  431. }
  432. score_sum+= best_score;
  433. //FIXME no need to set s->motion_val[mot_index][0] explicit
  434. s->motion_val[mot_index][0]= s->mv[0][0][0]= mv_predictor[best_pred][0];
  435. s->motion_val[mot_index][1]= s->mv[0][0][1]= mv_predictor[best_pred][1];
  436. MPV_decode_mb(s, s->block);
  437. if(s->mv[0][0][0] != prev_x || s->mv[0][0][1] != prev_y){
  438. fixed[i]=MV_CHANGED;
  439. changed++;
  440. }else
  441. fixed[i]=MV_UNCHANGED;
  442. }
  443. }
  444. // printf(".%d/%d", changed, score_sum); fflush(stdout);
  445. }
  446. if(none_left)
  447. return;
  448. for(i=0; i<s->mb_num; i++){
  449. if(fixed[i])
  450. fixed[i]=MV_FROZEN;
  451. }
  452. // printf(":"); fflush(stdout);
  453. }
  454. }
  455. static int is_intra_more_likely(MpegEncContext *s){
  456. int is_intra_likely, i, j, undamaged_count, skip_amount, mb_x, mb_y;
  457. if(s->last_picture.data[0]==NULL) return 1; //no previous frame available -> use spatial prediction
  458. undamaged_count=0;
  459. for(i=0; i<s->mb_num; i++){
  460. int error= s->error_status_table[i];
  461. if(!((error&DC_ERROR) && (error&MV_ERROR)))
  462. undamaged_count++;
  463. }
  464. if(undamaged_count < 5) return 0; //allmost all MBs damaged -> use temporal prediction
  465. skip_amount= FFMAX(undamaged_count/50, 1); //check only upto 50 MBs
  466. is_intra_likely=0;
  467. j=0;
  468. i=-1;
  469. for(mb_y= 0; mb_y<s->mb_height-1; mb_y++){
  470. for(mb_x= 0; mb_x<s->mb_width; mb_x++){
  471. int error;
  472. i++;
  473. error= s->error_status_table[i];
  474. if((error&DC_ERROR) && (error&MV_ERROR))
  475. continue; //skip damaged
  476. j++;
  477. if((j%skip_amount) != 0) continue; //skip a few to speed things up
  478. if(s->pict_type==I_TYPE){
  479. uint8_t *mb_ptr = s->current_picture.data[0] + mb_x*16 + mb_y*16*s->linesize;
  480. uint8_t *last_mb_ptr= s->last_picture.data [0] + mb_x*16 + mb_y*16*s->linesize;
  481. is_intra_likely += s->dsp.pix_abs16x16(last_mb_ptr, mb_ptr , s->linesize);
  482. is_intra_likely -= s->dsp.pix_abs16x16(last_mb_ptr, last_mb_ptr+s->linesize*16, s->linesize);
  483. }else{
  484. if(s->mbintra_table[i]) //HACK (this is allways inited but we should use mb_type[])
  485. is_intra_likely++;
  486. else
  487. is_intra_likely--;
  488. }
  489. }
  490. }
  491. //printf("is_intra_likely: %d type:%d\n", is_intra_likely, s->pict_type);
  492. return is_intra_likely > 0;
  493. }
  494. void ff_error_resilience(MpegEncContext *s){
  495. int i, mb_x, mb_y, error, error_type;
  496. int distance;
  497. int threshold_part[4]= {100,100,100};
  498. int threshold= 50;
  499. int is_intra_likely;
  500. #if 1
  501. /* handle overlapping slices */
  502. for(error_type=1; error_type<=3; error_type++){
  503. int end_ok=0;
  504. for(i=s->mb_num-1; i>=0; i--){
  505. int error= s->error_status_table[i];
  506. if(error&(1<<error_type))
  507. end_ok=1;
  508. if(error&(8<<error_type))
  509. end_ok=1;
  510. if(!end_ok)
  511. s->error_status_table[i]|= 1<<error_type;
  512. if(error&VP_START)
  513. end_ok=0;
  514. }
  515. }
  516. #endif
  517. #if 1
  518. /* handle slices with partitions of different length */
  519. if(s->partitioned_frame){
  520. int end_ok=0;
  521. for(i=s->mb_num-1; i>=0; i--){
  522. int error= s->error_status_table[i];
  523. if(error&AC_END)
  524. end_ok=0;
  525. if((error&MV_END) || (error&DC_END) || (error&AC_ERROR))
  526. end_ok=1;
  527. if(!end_ok)
  528. s->error_status_table[i]|= AC_ERROR;
  529. if(error&VP_START)
  530. end_ok=0;
  531. }
  532. }
  533. #endif
  534. /* handle missing slices */
  535. if(s->error_resilience>=4){
  536. int end_ok=1;
  537. for(i=s->mb_num-2; i>=s->mb_width+100; i--){ //FIXME +100 hack
  538. int error1= s->error_status_table[i ];
  539. int error2= s->error_status_table[i+1];
  540. if(error1&VP_START)
  541. end_ok=1;
  542. if( error2==(VP_START|DC_ERROR|AC_ERROR|MV_ERROR|AC_END|DC_END|MV_END)
  543. && error1!=(VP_START|DC_ERROR|AC_ERROR|MV_ERROR|AC_END|DC_END|MV_END)
  544. && ((error1&AC_END) || (error1&DC_END) || (error1&MV_END))){ //end & uninited
  545. end_ok=0;
  546. }
  547. if(!end_ok)
  548. s->error_status_table[i]|= DC_ERROR|AC_ERROR|MV_ERROR;
  549. }
  550. }
  551. #if 1
  552. /* backward mark errors */
  553. distance=9999999;
  554. for(error_type=1; error_type<=3; error_type++){
  555. for(i=s->mb_num-1; i>=0; i--){
  556. int error= s->error_status_table[i];
  557. if(!s->mbskip_table[i]) //FIXME partition specific
  558. distance++;
  559. if(error&(1<<error_type))
  560. distance= 0;
  561. if(s->partitioned_frame){
  562. if(distance < threshold_part[error_type-1])
  563. s->error_status_table[i]|= 1<<error_type;
  564. }else{
  565. if(distance < threshold)
  566. s->error_status_table[i]|= 1<<error_type;
  567. }
  568. if(error&VP_START)
  569. distance= 9999999;
  570. }
  571. }
  572. #endif
  573. /* forward mark errors */
  574. error=0;
  575. for(i=0; i<s->mb_num; i++){
  576. int old_error= s->error_status_table[i];
  577. if(old_error&VP_START)
  578. error= old_error& (DC_ERROR|AC_ERROR|MV_ERROR);
  579. else{
  580. error|= old_error& (DC_ERROR|AC_ERROR|MV_ERROR);
  581. s->error_status_table[i]|= error;
  582. }
  583. }
  584. #if 1
  585. /* handle not partitioned case */
  586. if(!s->partitioned_frame){
  587. for(i=0; i<s->mb_num; i++){
  588. error= s->error_status_table[i];
  589. if(error&(AC_ERROR|DC_ERROR|MV_ERROR))
  590. error|= AC_ERROR|DC_ERROR|MV_ERROR;
  591. s->error_status_table[i]= error;
  592. }
  593. }
  594. #endif
  595. is_intra_likely= is_intra_more_likely(s);
  596. /* set unknown mb-type to most likely */
  597. for(i=0; i<s->mb_num; i++){
  598. int intra;
  599. error= s->error_status_table[i];
  600. if((error&DC_ERROR) && (error&MV_ERROR))
  601. intra= is_intra_likely;
  602. else
  603. intra= s->mbintra_table[i];
  604. if(intra)
  605. s->mb_type[i]|= MB_TYPE_INTRA;
  606. else
  607. s->mb_type[i]&= ~MB_TYPE_INTRA;
  608. }
  609. /* handle inter blocks with damaged AC */
  610. i= -1;
  611. for(mb_y=0; mb_y<s->mb_height; mb_y++){
  612. for(mb_x=0; mb_x<s->mb_width; mb_x++){
  613. i++;
  614. error= s->error_status_table[i];
  615. if(s->mb_type[i]&MB_TYPE_INTRA) continue; //intra
  616. if(error&MV_ERROR) continue; //inter with damaged MV
  617. if(!(error&AC_ERROR)) continue; //undamaged inter
  618. s->mv_dir = MV_DIR_FORWARD;
  619. s->mb_intra=0;
  620. s->mb_skiped=0;
  621. if(s->mb_type[i]&MB_TYPE_INTER4V){
  622. int mb_index= mb_x*2+1 + (mb_y*2+1)*s->block_wrap[0];
  623. int j;
  624. s->mv_type = MV_TYPE_8X8;
  625. for(j=0; j<4; j++){
  626. s->mv[0][j][0] = s->motion_val[ mb_index + (j&1) + (j>>1)*s->block_wrap[0] ][0];
  627. s->mv[0][j][1] = s->motion_val[ mb_index + (j&1) + (j>>1)*s->block_wrap[0] ][1];
  628. }
  629. }else{
  630. s->mv_type = MV_TYPE_16X16;
  631. s->mv[0][0][0] = s->motion_val[ mb_x*2+1 + (mb_y*2+1)*s->block_wrap[0] ][0];
  632. s->mv[0][0][1] = s->motion_val[ mb_x*2+1 + (mb_y*2+1)*s->block_wrap[0] ][1];
  633. }
  634. s->dsp.clear_blocks(s->block[0]);
  635. s->mb_x= mb_x;
  636. s->mb_y= mb_y;
  637. MPV_decode_mb(s, s->block);
  638. }
  639. }
  640. /* guess MVs */
  641. if(s->pict_type==B_TYPE){
  642. i= -1;
  643. for(mb_y=0; mb_y<s->mb_height; mb_y++){
  644. for(mb_x=0; mb_x<s->mb_width; mb_x++){
  645. int xy= mb_x*2+1 + (mb_y*2+1)*s->block_wrap[0];
  646. i++;
  647. error= s->error_status_table[i];
  648. if(s->mb_type[i]&MB_TYPE_INTRA) continue; //intra
  649. if(!(error&MV_ERROR)) continue; //inter with undamaged MV
  650. if(!(error&AC_ERROR)) continue; //undamaged inter
  651. s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD;
  652. s->mb_intra=0;
  653. s->mv_type = MV_TYPE_16X16;
  654. s->mb_skiped=0;
  655. if(s->pp_time){
  656. int time_pp= s->pp_time;
  657. int time_pb= s->pb_time;
  658. s->mv[0][0][0] = s->motion_val[xy][0]*time_pb/time_pp;
  659. s->mv[0][0][1] = s->motion_val[xy][1]*time_pb/time_pp;
  660. s->mv[1][0][0] = s->motion_val[xy][0]*(time_pb - time_pp)/time_pp;
  661. s->mv[1][0][1] = s->motion_val[xy][1]*(time_pb - time_pp)/time_pp;
  662. }else{
  663. s->mv[0][0][0]= 0;
  664. s->mv[0][0][1]= 0;
  665. s->mv[1][0][0]= 0;
  666. s->mv[1][0][1]= 0;
  667. }
  668. s->dsp.clear_blocks(s->block[0]);
  669. s->mb_x= mb_x;
  670. s->mb_y= mb_y;
  671. MPV_decode_mb(s, s->block);
  672. }
  673. }
  674. }else
  675. guess_mv(s);
  676. /* fill DC for inter blocks */
  677. i= -1;
  678. for(mb_y=0; mb_y<s->mb_height; mb_y++){
  679. for(mb_x=0; mb_x<s->mb_width; mb_x++){
  680. int dc, dcu, dcv, y, n;
  681. int16_t *dc_ptr;
  682. uint8_t *dest_y, *dest_cb, *dest_cr;
  683. i++;
  684. error= s->error_status_table[i];
  685. if(s->mb_type[i]&MB_TYPE_INTRA) continue; //intra
  686. // if(error&MV_ERROR) continue; //inter data damaged FIXME is this good?
  687. dest_y = s->current_picture.data[0] + mb_x*16 + mb_y*16*s->linesize;
  688. dest_cb= s->current_picture.data[1] + mb_x*8 + mb_y*8 *s->uvlinesize;
  689. dest_cr= s->current_picture.data[2] + mb_x*8 + mb_y*8 *s->uvlinesize;
  690. dc_ptr= &s->dc_val[0][mb_x*2+1 + (mb_y*2+1)*(s->mb_width*2+2)];
  691. for(n=0; n<4; n++){
  692. dc=0;
  693. for(y=0; y<8; y++){
  694. int x;
  695. for(x=0; x<8; x++){
  696. dc+= dest_y[x + (n&1)*8 + (y + (n>>1)*8)*s->linesize];
  697. }
  698. }
  699. dc_ptr[(n&1) + (n>>1)*(s->mb_width*2+2)]= (dc+4)>>3;
  700. }
  701. dcu=dcv=0;
  702. for(y=0; y<8; y++){
  703. int x;
  704. for(x=0; x<8; x++){
  705. dcu+=dest_cb[x + y*(s->uvlinesize)];
  706. dcv+=dest_cr[x + y*(s->uvlinesize)];
  707. }
  708. }
  709. s->dc_val[1][mb_x+1 + (mb_y+1)*(s->mb_width+2)]= (dcu+4)>>3;
  710. s->dc_val[2][mb_x+1 + (mb_y+1)*(s->mb_width+2)]= (dcv+4)>>3;
  711. }
  712. }
  713. #if 1
  714. /* guess DC for damaged blocks */
  715. guess_dc(s, s->dc_val[0] + s->mb_width*2+3, s->mb_width*2, s->mb_height*2, s->mb_width*2+2, 1);
  716. guess_dc(s, s->dc_val[1] + s->mb_width +3, s->mb_width , s->mb_height , s->mb_width +2, 0);
  717. guess_dc(s, s->dc_val[2] + s->mb_width +3, s->mb_width , s->mb_height , s->mb_width +2, 0);
  718. #endif
  719. /* filter luma DC */
  720. filter181(s->dc_val[0] + s->mb_width*2+3, s->mb_width*2, s->mb_height*2, s->mb_width*2+2);
  721. #if 1
  722. /* render DC only intra */
  723. i= -1;
  724. for(mb_y=0; mb_y<s->mb_height; mb_y++){
  725. for(mb_x=0; mb_x<s->mb_width; mb_x++){
  726. uint8_t *dest_y, *dest_cb, *dest_cr;
  727. i++;
  728. error= s->error_status_table[i];
  729. if(!(s->mb_type[i]&MB_TYPE_INTRA)) continue; //inter
  730. if(!(error&AC_ERROR)) continue; //undamaged
  731. dest_y = s->current_picture.data[0] + mb_x*16 + mb_y*16*s->linesize;
  732. dest_cb= s->current_picture.data[1] + mb_x*8 + mb_y*8 *s->uvlinesize;
  733. dest_cr= s->current_picture.data[2] + mb_x*8 + mb_y*8 *s->uvlinesize;
  734. put_dc(s, dest_y, dest_cb, dest_cr, mb_x, mb_y);
  735. }
  736. }
  737. #endif
  738. if(s->avctx->error_concealment&FF_EC_DEBLOCK){
  739. /* filter horizontal block boundaries */
  740. h_block_filter(s, s->current_picture.data[0], s->mb_width*2, s->mb_height*2, s->linesize , 1);
  741. h_block_filter(s, s->current_picture.data[1], s->mb_width , s->mb_height , s->uvlinesize, 0);
  742. h_block_filter(s, s->current_picture.data[2], s->mb_width , s->mb_height , s->uvlinesize, 0);
  743. /* filter vertical block boundaries */
  744. v_block_filter(s, s->current_picture.data[0], s->mb_width*2, s->mb_height*2, s->linesize , 1);
  745. v_block_filter(s, s->current_picture.data[1], s->mb_width , s->mb_height , s->uvlinesize, 0);
  746. v_block_filter(s, s->current_picture.data[2], s->mb_width , s->mb_height , s->uvlinesize, 0);
  747. }
  748. /* clean a few tables */
  749. for(i=0; i<s->mb_num; i++){
  750. int error= s->error_status_table[i];
  751. if(s->pict_type!=B_TYPE && (error&(DC_ERROR|MV_ERROR|AC_ERROR))){
  752. s->mbskip_table[i]=0;
  753. }
  754. s->mbintra_table[i]=1;
  755. }
  756. }