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.

1053 lines
40KB

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