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.

973 lines
35KB

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