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.

1088 lines
41KB

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