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.

1016 lines
39KB

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