error_resilience.c 43 KB

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