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.

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