error_resilience.c 38 KB

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