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.

1035 lines
38KB

  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==NULL) 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; //allmost all MBs damaged -> use temporal prediction
  478. skip_amount= FFMAX(undamaged_count/50, 1); //check only upto 50 MBs
  479. is_intra_likely=0;
  480. j=0;
  481. for(mb_y= 0; mb_y<s->mb_height-1; mb_y++){
  482. for(mb_x= 0; mb_x<s->mb_width; mb_x++){
  483. int error;
  484. const int mb_xy= mb_x + mb_y*s->mb_stride;
  485. error= s->error_status_table[mb_xy];
  486. if((error&DC_ERROR) && (error&MV_ERROR))
  487. continue; //skip damaged
  488. j++;
  489. if((j%skip_amount) != 0) continue; //skip a few to speed things up
  490. if(s->pict_type==I_TYPE){
  491. uint8_t *mb_ptr = s->current_picture.data[0] + mb_x*16 + mb_y*16*s->linesize;
  492. uint8_t *last_mb_ptr= s->last_picture.data [0] + mb_x*16 + mb_y*16*s->linesize;
  493. is_intra_likely += s->dsp.sad[0](NULL, last_mb_ptr, mb_ptr , s->linesize, 16);
  494. is_intra_likely -= s->dsp.sad[0](NULL, last_mb_ptr, last_mb_ptr+s->linesize*16, s->linesize, 16);
  495. }else{
  496. if(IS_INTRA(s->current_picture.mb_type[mb_xy]))
  497. is_intra_likely++;
  498. else
  499. is_intra_likely--;
  500. }
  501. }
  502. }
  503. //printf("is_intra_likely: %d type:%d\n", is_intra_likely, s->pict_type);
  504. return is_intra_likely > 0;
  505. }
  506. void ff_er_frame_start(MpegEncContext *s){
  507. if(!s->error_resilience) return;
  508. 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));
  509. s->error_count= 3*s->mb_num;
  510. }
  511. /**
  512. * adds a slice.
  513. * @param endx x component of the last macroblock, can be -1 for the last of the previous line
  514. * @param status the status at the end (MV_END, AC_ERROR, ...), it is assumed that no earlier end or
  515. * error of the same type occured
  516. */
  517. void ff_er_add_slice(MpegEncContext *s, int startx, int starty, int endx, int endy, int status){
  518. const int start_i= av_clip(startx + starty * s->mb_width , 0, s->mb_num-1);
  519. const int end_i = av_clip(endx + endy * s->mb_width , 0, s->mb_num);
  520. const int start_xy= s->mb_index2xy[start_i];
  521. const int end_xy = s->mb_index2xy[end_i];
  522. int mask= -1;
  523. if(start_i > end_i || start_xy > end_xy){
  524. av_log(s->avctx, AV_LOG_ERROR, "internal error, slice end before start\n");
  525. return;
  526. }
  527. if(!s->error_resilience) return;
  528. mask &= ~VP_START;
  529. if(status & (AC_ERROR|AC_END)){
  530. mask &= ~(AC_ERROR|AC_END);
  531. s->error_count -= end_i - start_i + 1;
  532. }
  533. if(status & (DC_ERROR|DC_END)){
  534. mask &= ~(DC_ERROR|DC_END);
  535. s->error_count -= end_i - start_i + 1;
  536. }
  537. if(status & (MV_ERROR|MV_END)){
  538. mask &= ~(MV_ERROR|MV_END);
  539. s->error_count -= end_i - start_i + 1;
  540. }
  541. if(status & (AC_ERROR|DC_ERROR|MV_ERROR)) s->error_count= INT_MAX;
  542. if(mask == ~0x7F){
  543. memset(&s->error_status_table[start_xy], 0, (end_xy - start_xy) * sizeof(uint8_t));
  544. }else{
  545. int i;
  546. for(i=start_xy; i<end_xy; i++){
  547. s->error_status_table[ i ] &= mask;
  548. }
  549. }
  550. if(end_i == s->mb_num)
  551. s->error_count= INT_MAX;
  552. else{
  553. s->error_status_table[end_xy] &= mask;
  554. s->error_status_table[end_xy] |= status;
  555. }
  556. s->error_status_table[start_xy] |= VP_START;
  557. if(start_xy > 0 && s->avctx->thread_count <= 1 && s->avctx->skip_top*s->mb_width < start_i){
  558. int prev_status= s->error_status_table[ s->mb_index2xy[start_i - 1] ];
  559. prev_status &= ~ VP_START;
  560. if(prev_status != (MV_END|DC_END|AC_END)) s->error_count= INT_MAX;
  561. }
  562. }
  563. void ff_er_frame_end(MpegEncContext *s){
  564. int i, mb_x, mb_y, error, error_type, dc_error, mv_error, ac_error;
  565. int distance;
  566. int threshold_part[4]= {100,100,100};
  567. int threshold= 50;
  568. int is_intra_likely;
  569. int size = s->b8_stride * 2 * s->mb_height;
  570. Picture *pic= s->current_picture_ptr;
  571. if(!s->error_resilience || s->error_count==0 ||
  572. s->error_count==3*s->mb_width*(s->avctx->skip_top + s->avctx->skip_bottom)) return;
  573. if(s->current_picture.motion_val[0] == NULL){
  574. av_log(s->avctx, AV_LOG_ERROR, "Warning MVs not available\n");
  575. for(i=0; i<2; i++){
  576. pic->ref_index[i]= av_mallocz(size * sizeof(uint8_t));
  577. pic->motion_val_base[i]= av_mallocz((size+4) * 2 * sizeof(uint16_t));
  578. pic->motion_val[i]= pic->motion_val_base[i]+4;
  579. }
  580. pic->motion_subsample_log2= 3;
  581. s->current_picture= *s->current_picture_ptr;
  582. }
  583. for(i=0; i<2; i++){
  584. if(pic->ref_index[i])
  585. memset(pic->ref_index[i], 0, size * sizeof(uint8_t));
  586. }
  587. if(s->avctx->debug&FF_DEBUG_ER){
  588. for(mb_y=0; mb_y<s->mb_height; mb_y++){
  589. for(mb_x=0; mb_x<s->mb_width; mb_x++){
  590. int status= s->error_status_table[mb_x + mb_y*s->mb_stride];
  591. av_log(s->avctx, AV_LOG_DEBUG, "%2X ", status);
  592. }
  593. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  594. }
  595. }
  596. #if 1
  597. /* handle overlapping slices */
  598. for(error_type=1; error_type<=3; error_type++){
  599. int end_ok=0;
  600. for(i=s->mb_num-1; i>=0; i--){
  601. const int mb_xy= s->mb_index2xy[i];
  602. int error= s->error_status_table[mb_xy];
  603. if(error&(1<<error_type))
  604. end_ok=1;
  605. if(error&(8<<error_type))
  606. end_ok=1;
  607. if(!end_ok)
  608. s->error_status_table[mb_xy]|= 1<<error_type;
  609. if(error&VP_START)
  610. end_ok=0;
  611. }
  612. }
  613. #endif
  614. #if 1
  615. /* handle slices with partitions of different length */
  616. if(s->partitioned_frame){
  617. int end_ok=0;
  618. for(i=s->mb_num-1; i>=0; i--){
  619. const int mb_xy= s->mb_index2xy[i];
  620. int error= s->error_status_table[mb_xy];
  621. if(error&AC_END)
  622. end_ok=0;
  623. if((error&MV_END) || (error&DC_END) || (error&AC_ERROR))
  624. end_ok=1;
  625. if(!end_ok)
  626. s->error_status_table[mb_xy]|= AC_ERROR;
  627. if(error&VP_START)
  628. end_ok=0;
  629. }
  630. }
  631. #endif
  632. /* handle missing slices */
  633. if(s->error_resilience>=4){
  634. int end_ok=1;
  635. for(i=s->mb_num-2; i>=s->mb_width+100; i--){ //FIXME +100 hack
  636. const int mb_xy= s->mb_index2xy[i];
  637. int error1= s->error_status_table[mb_xy ];
  638. int error2= s->error_status_table[s->mb_index2xy[i+1]];
  639. if(error1&VP_START)
  640. end_ok=1;
  641. if( error2==(VP_START|DC_ERROR|AC_ERROR|MV_ERROR|AC_END|DC_END|MV_END)
  642. && error1!=(VP_START|DC_ERROR|AC_ERROR|MV_ERROR|AC_END|DC_END|MV_END)
  643. && ((error1&AC_END) || (error1&DC_END) || (error1&MV_END))){ //end & uninited
  644. end_ok=0;
  645. }
  646. if(!end_ok)
  647. s->error_status_table[mb_xy]|= DC_ERROR|AC_ERROR|MV_ERROR;
  648. }
  649. }
  650. #if 1
  651. /* backward mark errors */
  652. distance=9999999;
  653. for(error_type=1; error_type<=3; error_type++){
  654. for(i=s->mb_num-1; i>=0; i--){
  655. const int mb_xy= s->mb_index2xy[i];
  656. int error= s->error_status_table[mb_xy];
  657. if(!s->mbskip_table[mb_xy]) //FIXME partition specific
  658. distance++;
  659. if(error&(1<<error_type))
  660. distance= 0;
  661. if(s->partitioned_frame){
  662. if(distance < threshold_part[error_type-1])
  663. s->error_status_table[mb_xy]|= 1<<error_type;
  664. }else{
  665. if(distance < threshold)
  666. s->error_status_table[mb_xy]|= 1<<error_type;
  667. }
  668. if(error&VP_START)
  669. distance= 9999999;
  670. }
  671. }
  672. #endif
  673. /* forward mark errors */
  674. error=0;
  675. for(i=0; i<s->mb_num; i++){
  676. const int mb_xy= s->mb_index2xy[i];
  677. int old_error= s->error_status_table[mb_xy];
  678. if(old_error&VP_START)
  679. error= old_error& (DC_ERROR|AC_ERROR|MV_ERROR);
  680. else{
  681. error|= old_error& (DC_ERROR|AC_ERROR|MV_ERROR);
  682. s->error_status_table[mb_xy]|= error;
  683. }
  684. }
  685. #if 1
  686. /* handle not partitioned case */
  687. if(!s->partitioned_frame){
  688. for(i=0; i<s->mb_num; i++){
  689. const int mb_xy= s->mb_index2xy[i];
  690. error= s->error_status_table[mb_xy];
  691. if(error&(AC_ERROR|DC_ERROR|MV_ERROR))
  692. error|= AC_ERROR|DC_ERROR|MV_ERROR;
  693. s->error_status_table[mb_xy]= error;
  694. }
  695. }
  696. #endif
  697. dc_error= ac_error= mv_error=0;
  698. for(i=0; i<s->mb_num; i++){
  699. const int mb_xy= s->mb_index2xy[i];
  700. error= s->error_status_table[mb_xy];
  701. if(error&DC_ERROR) dc_error ++;
  702. if(error&AC_ERROR) ac_error ++;
  703. if(error&MV_ERROR) mv_error ++;
  704. }
  705. av_log(s->avctx, AV_LOG_INFO, "concealing %d DC, %d AC, %d MV errors\n", dc_error, ac_error, mv_error);
  706. is_intra_likely= is_intra_more_likely(s);
  707. /* set unknown mb-type to most likely */
  708. for(i=0; i<s->mb_num; i++){
  709. const int mb_xy= s->mb_index2xy[i];
  710. error= s->error_status_table[mb_xy];
  711. if(!((error&DC_ERROR) && (error&MV_ERROR)))
  712. continue;
  713. if(is_intra_likely)
  714. s->current_picture.mb_type[mb_xy]= MB_TYPE_INTRA4x4;
  715. else
  716. s->current_picture.mb_type[mb_xy]= MB_TYPE_16x16 | MB_TYPE_L0;
  717. }
  718. /* handle inter blocks with damaged AC */
  719. for(mb_y=0; mb_y<s->mb_height; mb_y++){
  720. for(mb_x=0; mb_x<s->mb_width; mb_x++){
  721. const int mb_xy= mb_x + mb_y * s->mb_stride;
  722. const int mb_type= s->current_picture.mb_type[mb_xy];
  723. error= s->error_status_table[mb_xy];
  724. if(IS_INTRA(mb_type)) continue; //intra
  725. if(error&MV_ERROR) continue; //inter with damaged MV
  726. if(!(error&AC_ERROR)) continue; //undamaged inter
  727. s->mv_dir = MV_DIR_FORWARD;
  728. s->mb_intra=0;
  729. s->mb_skipped=0;
  730. if(IS_8X8(mb_type)){
  731. int mb_index= mb_x*2 + mb_y*2*s->b8_stride;
  732. int j;
  733. s->mv_type = MV_TYPE_8X8;
  734. for(j=0; j<4; j++){
  735. s->mv[0][j][0] = s->current_picture.motion_val[0][ mb_index + (j&1) + (j>>1)*s->b8_stride ][0];
  736. s->mv[0][j][1] = s->current_picture.motion_val[0][ mb_index + (j&1) + (j>>1)*s->b8_stride ][1];
  737. }
  738. }else{
  739. s->mv_type = MV_TYPE_16X16;
  740. s->mv[0][0][0] = s->current_picture.motion_val[0][ mb_x*2 + mb_y*2*s->b8_stride ][0];
  741. s->mv[0][0][1] = s->current_picture.motion_val[0][ mb_x*2 + mb_y*2*s->b8_stride ][1];
  742. }
  743. s->dsp.clear_blocks(s->block[0]);
  744. s->mb_x= mb_x;
  745. s->mb_y= mb_y;
  746. decode_mb(s);
  747. }
  748. }
  749. /* guess MVs */
  750. if(s->pict_type==B_TYPE){
  751. for(mb_y=0; mb_y<s->mb_height; mb_y++){
  752. for(mb_x=0; mb_x<s->mb_width; mb_x++){
  753. int xy= mb_x*2 + mb_y*2*s->b8_stride;
  754. const int mb_xy= mb_x + mb_y * s->mb_stride;
  755. const int mb_type= s->current_picture.mb_type[mb_xy];
  756. error= s->error_status_table[mb_xy];
  757. if(IS_INTRA(mb_type)) continue;
  758. if(!(error&MV_ERROR)) continue; //inter with undamaged MV
  759. if(!(error&AC_ERROR)) continue; //undamaged inter
  760. s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD;
  761. s->mb_intra=0;
  762. s->mv_type = MV_TYPE_16X16;
  763. s->mb_skipped=0;
  764. if(s->pp_time){
  765. int time_pp= s->pp_time;
  766. int time_pb= s->pb_time;
  767. s->mv[0][0][0] = s->next_picture.motion_val[0][xy][0]*time_pb/time_pp;
  768. s->mv[0][0][1] = s->next_picture.motion_val[0][xy][1]*time_pb/time_pp;
  769. s->mv[1][0][0] = s->next_picture.motion_val[0][xy][0]*(time_pb - time_pp)/time_pp;
  770. s->mv[1][0][1] = s->next_picture.motion_val[0][xy][1]*(time_pb - time_pp)/time_pp;
  771. }else{
  772. s->mv[0][0][0]= 0;
  773. s->mv[0][0][1]= 0;
  774. s->mv[1][0][0]= 0;
  775. s->mv[1][0][1]= 0;
  776. }
  777. s->dsp.clear_blocks(s->block[0]);
  778. s->mb_x= mb_x;
  779. s->mb_y= mb_y;
  780. decode_mb(s);
  781. }
  782. }
  783. }else
  784. guess_mv(s);
  785. #ifdef HAVE_XVMC
  786. /* the filters below are not XvMC compatible, skip them */
  787. if(s->avctx->xvmc_acceleration) goto ec_clean;
  788. #endif
  789. /* fill DC for inter blocks */
  790. for(mb_y=0; mb_y<s->mb_height; mb_y++){
  791. for(mb_x=0; mb_x<s->mb_width; mb_x++){
  792. int dc, dcu, dcv, y, n;
  793. int16_t *dc_ptr;
  794. uint8_t *dest_y, *dest_cb, *dest_cr;
  795. const int mb_xy= mb_x + mb_y * s->mb_stride;
  796. const int mb_type= s->current_picture.mb_type[mb_xy];
  797. error= s->error_status_table[mb_xy];
  798. if(IS_INTRA(mb_type) && s->partitioned_frame) continue;
  799. // if(error&MV_ERROR) continue; //inter data damaged FIXME is this good?
  800. dest_y = s->current_picture.data[0] + mb_x*16 + mb_y*16*s->linesize;
  801. dest_cb= s->current_picture.data[1] + mb_x*8 + mb_y*8 *s->uvlinesize;
  802. dest_cr= s->current_picture.data[2] + mb_x*8 + mb_y*8 *s->uvlinesize;
  803. dc_ptr= &s->dc_val[0][mb_x*2 + mb_y*2*s->b8_stride];
  804. for(n=0; n<4; n++){
  805. dc=0;
  806. for(y=0; y<8; y++){
  807. int x;
  808. for(x=0; x<8; x++){
  809. dc+= dest_y[x + (n&1)*8 + (y + (n>>1)*8)*s->linesize];
  810. }
  811. }
  812. dc_ptr[(n&1) + (n>>1)*s->b8_stride]= (dc+4)>>3;
  813. }
  814. dcu=dcv=0;
  815. for(y=0; y<8; y++){
  816. int x;
  817. for(x=0; x<8; x++){
  818. dcu+=dest_cb[x + y*(s->uvlinesize)];
  819. dcv+=dest_cr[x + y*(s->uvlinesize)];
  820. }
  821. }
  822. s->dc_val[1][mb_x + mb_y*s->mb_stride]= (dcu+4)>>3;
  823. s->dc_val[2][mb_x + mb_y*s->mb_stride]= (dcv+4)>>3;
  824. }
  825. }
  826. #if 1
  827. /* guess DC for damaged blocks */
  828. guess_dc(s, s->dc_val[0], s->mb_width*2, s->mb_height*2, s->b8_stride, 1);
  829. guess_dc(s, s->dc_val[1], s->mb_width , s->mb_height , s->mb_stride, 0);
  830. guess_dc(s, s->dc_val[2], s->mb_width , s->mb_height , s->mb_stride, 0);
  831. #endif
  832. /* filter luma DC */
  833. filter181(s->dc_val[0], s->mb_width*2, s->mb_height*2, s->b8_stride);
  834. #if 1
  835. /* render DC only intra */
  836. for(mb_y=0; mb_y<s->mb_height; mb_y++){
  837. for(mb_x=0; mb_x<s->mb_width; mb_x++){
  838. uint8_t *dest_y, *dest_cb, *dest_cr;
  839. const int mb_xy= mb_x + mb_y * s->mb_stride;
  840. const int mb_type= s->current_picture.mb_type[mb_xy];
  841. error= s->error_status_table[mb_xy];
  842. if(IS_INTER(mb_type)) continue;
  843. if(!(error&AC_ERROR)) continue; //undamaged
  844. dest_y = s->current_picture.data[0] + mb_x*16 + mb_y*16*s->linesize;
  845. dest_cb= s->current_picture.data[1] + mb_x*8 + mb_y*8 *s->uvlinesize;
  846. dest_cr= s->current_picture.data[2] + mb_x*8 + mb_y*8 *s->uvlinesize;
  847. put_dc(s, dest_y, dest_cb, dest_cr, mb_x, mb_y);
  848. }
  849. }
  850. #endif
  851. if(s->avctx->error_concealment&FF_EC_DEBLOCK){
  852. /* filter horizontal block boundaries */
  853. h_block_filter(s, s->current_picture.data[0], s->mb_width*2, s->mb_height*2, s->linesize , 1);
  854. h_block_filter(s, s->current_picture.data[1], s->mb_width , s->mb_height , s->uvlinesize, 0);
  855. h_block_filter(s, s->current_picture.data[2], s->mb_width , s->mb_height , s->uvlinesize, 0);
  856. /* filter vertical block boundaries */
  857. v_block_filter(s, s->current_picture.data[0], s->mb_width*2, s->mb_height*2, s->linesize , 1);
  858. v_block_filter(s, s->current_picture.data[1], s->mb_width , s->mb_height , s->uvlinesize, 0);
  859. v_block_filter(s, s->current_picture.data[2], s->mb_width , s->mb_height , s->uvlinesize, 0);
  860. }
  861. #ifdef HAVE_XVMC
  862. ec_clean:
  863. #endif
  864. /* clean a few tables */
  865. for(i=0; i<s->mb_num; i++){
  866. const int mb_xy= s->mb_index2xy[i];
  867. int error= s->error_status_table[mb_xy];
  868. if(s->pict_type!=B_TYPE && (error&(DC_ERROR|MV_ERROR|AC_ERROR))){
  869. s->mbskip_table[mb_xy]=0;
  870. }
  871. s->mbintra_table[mb_xy]=1;
  872. }
  873. }