error_resilience.c 45 KB

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