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.

1041 lines
39KB

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