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.

993 lines
37KB

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