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.

1186 lines
46KB

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