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.

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