2
0

error_resilience.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364
  1. /*
  2. * Error resilience / concealment
  3. *
  4. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * Error resilience / concealment.
  25. */
  26. #include <limits.h>
  27. #include "libavutil/internal.h"
  28. #include "avcodec.h"
  29. #include "error_resilience.h"
  30. #include "me_cmp.h"
  31. #include "mpegutils.h"
  32. #include "mpegvideo.h"
  33. #include "rectangle.h"
  34. #include "thread.h"
  35. #include "version.h"
  36. /**
  37. * @param stride the number of MVs to get to the next row
  38. * @param mv_step the number of MVs per row or column in a macroblock
  39. */
  40. static void set_mv_strides(ERContext *s, ptrdiff_t *mv_step, ptrdiff_t *stride)
  41. {
  42. if (s->avctx->codec_id == AV_CODEC_ID_H264) {
  43. av_assert0(s->quarter_sample);
  44. *mv_step = 4;
  45. *stride = s->mb_width * 4;
  46. } else {
  47. *mv_step = 2;
  48. *stride = s->b8_stride;
  49. }
  50. }
  51. /**
  52. * Replace the current MB with a flat dc-only version.
  53. */
  54. static void put_dc(ERContext *s, uint8_t *dest_y, uint8_t *dest_cb,
  55. uint8_t *dest_cr, int mb_x, int mb_y)
  56. {
  57. int *linesize = s->cur_pic.f->linesize;
  58. int dc, dcu, dcv, y, i;
  59. for (i = 0; i < 4; i++) {
  60. dc = s->dc_val[0][mb_x * 2 + (i & 1) + (mb_y * 2 + (i >> 1)) * s->b8_stride];
  61. if (dc < 0)
  62. dc = 0;
  63. else if (dc > 2040)
  64. dc = 2040;
  65. for (y = 0; y < 8; y++) {
  66. int x;
  67. for (x = 0; x < 8; x++)
  68. dest_y[x + (i & 1) * 8 + (y + (i >> 1) * 8) * linesize[0]] = dc / 8;
  69. }
  70. }
  71. dcu = s->dc_val[1][mb_x + mb_y * s->mb_stride];
  72. dcv = s->dc_val[2][mb_x + mb_y * s->mb_stride];
  73. if (dcu < 0)
  74. dcu = 0;
  75. else if (dcu > 2040)
  76. dcu = 2040;
  77. if (dcv < 0)
  78. dcv = 0;
  79. else if (dcv > 2040)
  80. dcv = 2040;
  81. if (dest_cr)
  82. for (y = 0; y < 8; y++) {
  83. int x;
  84. for (x = 0; x < 8; x++) {
  85. dest_cb[x + y * linesize[1]] = dcu / 8;
  86. dest_cr[x + y * linesize[2]] = dcv / 8;
  87. }
  88. }
  89. }
  90. static void filter181(int16_t *data, int width, int height, ptrdiff_t stride)
  91. {
  92. int x, y;
  93. /* horizontal filter */
  94. for (y = 1; y < height - 1; y++) {
  95. int prev_dc = data[0 + y * stride];
  96. for (x = 1; x < width - 1; x++) {
  97. int dc;
  98. dc = -prev_dc +
  99. data[x + y * stride] * 8 -
  100. data[x + 1 + y * stride];
  101. dc = (av_clip(dc, INT_MIN/10923, INT_MAX/10923 - 32768) * 10923 + 32768) >> 16;
  102. prev_dc = data[x + y * stride];
  103. data[x + y * stride] = dc;
  104. }
  105. }
  106. /* vertical filter */
  107. for (x = 1; x < width - 1; x++) {
  108. int prev_dc = data[x];
  109. for (y = 1; y < height - 1; y++) {
  110. int dc;
  111. dc = -prev_dc +
  112. data[x + y * stride] * 8 -
  113. data[x + (y + 1) * stride];
  114. dc = (av_clip(dc, INT_MIN/10923, INT_MAX/10923 - 32768) * 10923 + 32768) >> 16;
  115. prev_dc = data[x + y * stride];
  116. data[x + y * stride] = dc;
  117. }
  118. }
  119. }
  120. /**
  121. * guess the dc of blocks which do not have an undamaged dc
  122. * @param w width in 8 pixel blocks
  123. * @param h height in 8 pixel blocks
  124. */
  125. static void guess_dc(ERContext *s, int16_t *dc, int w,
  126. int h, ptrdiff_t stride, int is_luma)
  127. {
  128. int b_x, b_y;
  129. int16_t (*col )[4] = av_malloc_array(stride, h*sizeof( int16_t)*4);
  130. uint32_t (*dist)[4] = av_malloc_array(stride, h*sizeof(uint32_t)*4);
  131. if(!col || !dist) {
  132. av_log(s->avctx, AV_LOG_ERROR, "guess_dc() is out of memory\n");
  133. goto fail;
  134. }
  135. for(b_y=0; b_y<h; b_y++){
  136. int color= 1024;
  137. int distance= -1;
  138. for(b_x=0; b_x<w; b_x++){
  139. int mb_index_j= (b_x>>is_luma) + (b_y>>is_luma)*s->mb_stride;
  140. int error_j= s->error_status_table[mb_index_j];
  141. int intra_j = IS_INTRA(s->cur_pic.mb_type[mb_index_j]);
  142. if(intra_j==0 || !(error_j&ER_DC_ERROR)){
  143. color= dc[b_x + b_y*stride];
  144. distance= b_x;
  145. }
  146. col [b_x + b_y*stride][1]= color;
  147. dist[b_x + b_y*stride][1]= distance >= 0 ? b_x-distance : 9999;
  148. }
  149. color= 1024;
  150. distance= -1;
  151. for(b_x=w-1; b_x>=0; b_x--){
  152. int mb_index_j= (b_x>>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->cur_pic.mb_type[mb_index_j]);
  155. if(intra_j==0 || !(error_j&ER_DC_ERROR)){
  156. color= dc[b_x + b_y*stride];
  157. distance= b_x;
  158. }
  159. col [b_x + b_y*stride][0]= color;
  160. dist[b_x + b_y*stride][0]= distance >= 0 ? distance-b_x : 9999;
  161. }
  162. }
  163. for(b_x=0; b_x<w; b_x++){
  164. int color= 1024;
  165. int distance= -1;
  166. for(b_y=0; b_y<h; b_y++){
  167. int mb_index_j= (b_x>>is_luma) + (b_y>>is_luma)*s->mb_stride;
  168. int error_j= s->error_status_table[mb_index_j];
  169. int intra_j = IS_INTRA(s->cur_pic.mb_type[mb_index_j]);
  170. if(intra_j==0 || !(error_j&ER_DC_ERROR)){
  171. color= dc[b_x + b_y*stride];
  172. distance= b_y;
  173. }
  174. col [b_x + b_y*stride][3]= color;
  175. dist[b_x + b_y*stride][3]= distance >= 0 ? b_y-distance : 9999;
  176. }
  177. color= 1024;
  178. distance= -1;
  179. for(b_y=h-1; b_y>=0; b_y--){
  180. int mb_index_j= (b_x>>is_luma) + (b_y>>is_luma)*s->mb_stride;
  181. int error_j= s->error_status_table[mb_index_j];
  182. int intra_j = IS_INTRA(s->cur_pic.mb_type[mb_index_j]);
  183. if(intra_j==0 || !(error_j&ER_DC_ERROR)){
  184. color= dc[b_x + b_y*stride];
  185. distance= b_y;
  186. }
  187. col [b_x + b_y*stride][2]= color;
  188. dist[b_x + b_y*stride][2]= distance >= 0 ? distance-b_y : 9999;
  189. }
  190. }
  191. for (b_y = 0; b_y < h; b_y++) {
  192. for (b_x = 0; b_x < w; b_x++) {
  193. int mb_index, error, j;
  194. int64_t guess, weight_sum;
  195. mb_index = (b_x >> is_luma) + (b_y >> is_luma) * s->mb_stride;
  196. error = s->error_status_table[mb_index];
  197. if (IS_INTER(s->cur_pic.mb_type[mb_index]))
  198. continue; // inter
  199. if (!(error & ER_DC_ERROR))
  200. continue; // dc-ok
  201. weight_sum = 0;
  202. guess = 0;
  203. for (j = 0; j < 4; j++) {
  204. int64_t weight = 256 * 256 * 256 * 16 / FFMAX(dist[b_x + b_y*stride][j], 1);
  205. guess += weight*(int64_t)col[b_x + b_y*stride][j];
  206. weight_sum += weight;
  207. }
  208. guess = (guess + weight_sum / 2) / weight_sum;
  209. dc[b_x + b_y * stride] = guess;
  210. }
  211. }
  212. fail:
  213. av_freep(&col);
  214. av_freep(&dist);
  215. }
  216. /**
  217. * simple horizontal deblocking filter used for error resilience
  218. * @param w width in 8 pixel blocks
  219. * @param h height in 8 pixel blocks
  220. */
  221. static void h_block_filter(ERContext *s, uint8_t *dst, int w,
  222. int h, ptrdiff_t stride, int is_luma)
  223. {
  224. int b_x, b_y;
  225. ptrdiff_t mvx_stride, mvy_stride;
  226. const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;
  227. set_mv_strides(s, &mvx_stride, &mvy_stride);
  228. mvx_stride >>= is_luma;
  229. mvy_stride *= mvx_stride;
  230. for (b_y = 0; b_y < h; b_y++) {
  231. for (b_x = 0; b_x < w - 1; b_x++) {
  232. int y;
  233. int left_status = s->error_status_table[( b_x >> is_luma) + (b_y >> is_luma) * s->mb_stride];
  234. int right_status = s->error_status_table[((b_x + 1) >> is_luma) + (b_y >> is_luma) * s->mb_stride];
  235. int left_intra = IS_INTRA(s->cur_pic.mb_type[( b_x >> is_luma) + (b_y >> is_luma) * s->mb_stride]);
  236. int right_intra = IS_INTRA(s->cur_pic.mb_type[((b_x + 1) >> is_luma) + (b_y >> is_luma) * s->mb_stride]);
  237. int left_damage = left_status & ER_MB_ERROR;
  238. int right_damage = right_status & ER_MB_ERROR;
  239. int offset = b_x * 8 + b_y * stride * 8;
  240. int16_t *left_mv = s->cur_pic.motion_val[0][mvy_stride * b_y + mvx_stride * b_x];
  241. int16_t *right_mv = s->cur_pic.motion_val[0][mvy_stride * b_y + mvx_stride * (b_x + 1)];
  242. if (!(left_damage || right_damage))
  243. continue; // both undamaged
  244. if ((!left_intra) && (!right_intra) &&
  245. FFABS(left_mv[0] - right_mv[0]) +
  246. FFABS(left_mv[1] + right_mv[1]) < 2)
  247. continue;
  248. for (y = 0; y < 8; y++) {
  249. int a, b, c, d;
  250. a = dst[offset + 7 + y * stride] - dst[offset + 6 + y * stride];
  251. b = dst[offset + 8 + y * stride] - dst[offset + 7 + y * stride];
  252. c = dst[offset + 9 + y * stride] - dst[offset + 8 + y * stride];
  253. d = FFABS(b) - ((FFABS(a) + FFABS(c) + 1) >> 1);
  254. d = FFMAX(d, 0);
  255. if (b < 0)
  256. d = -d;
  257. if (d == 0)
  258. continue;
  259. if (!(left_damage && right_damage))
  260. d = d * 16 / 9;
  261. if (left_damage) {
  262. dst[offset + 7 + y * stride] = cm[dst[offset + 7 + y * stride] + ((d * 7) >> 4)];
  263. dst[offset + 6 + y * stride] = cm[dst[offset + 6 + y * stride] + ((d * 5) >> 4)];
  264. dst[offset + 5 + y * stride] = cm[dst[offset + 5 + y * stride] + ((d * 3) >> 4)];
  265. dst[offset + 4 + y * stride] = cm[dst[offset + 4 + y * stride] + ((d * 1) >> 4)];
  266. }
  267. if (right_damage) {
  268. dst[offset + 8 + y * stride] = cm[dst[offset + 8 + y * stride] - ((d * 7) >> 4)];
  269. dst[offset + 9 + y * stride] = cm[dst[offset + 9 + y * stride] - ((d * 5) >> 4)];
  270. dst[offset + 10+ y * stride] = cm[dst[offset + 10 + y * stride] - ((d * 3) >> 4)];
  271. dst[offset + 11+ y * stride] = cm[dst[offset + 11 + y * stride] - ((d * 1) >> 4)];
  272. }
  273. }
  274. }
  275. }
  276. }
  277. /**
  278. * simple vertical deblocking filter used for error resilience
  279. * @param w width in 8 pixel blocks
  280. * @param h height in 8 pixel blocks
  281. */
  282. static void v_block_filter(ERContext *s, uint8_t *dst, int w, int h,
  283. ptrdiff_t stride, int is_luma)
  284. {
  285. int b_x, b_y;
  286. ptrdiff_t mvx_stride, mvy_stride;
  287. const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;
  288. set_mv_strides(s, &mvx_stride, &mvy_stride);
  289. mvx_stride >>= is_luma;
  290. mvy_stride *= mvx_stride;
  291. for (b_y = 0; b_y < h - 1; b_y++) {
  292. for (b_x = 0; b_x < w; b_x++) {
  293. int x;
  294. int top_status = s->error_status_table[(b_x >> is_luma) + (b_y >> is_luma) * s->mb_stride];
  295. int bottom_status = s->error_status_table[(b_x >> is_luma) + ((b_y + 1) >> is_luma) * s->mb_stride];
  296. int top_intra = IS_INTRA(s->cur_pic.mb_type[(b_x >> is_luma) + ( b_y >> is_luma) * s->mb_stride]);
  297. int bottom_intra = IS_INTRA(s->cur_pic.mb_type[(b_x >> is_luma) + ((b_y + 1) >> is_luma) * s->mb_stride]);
  298. int top_damage = top_status & ER_MB_ERROR;
  299. int bottom_damage = bottom_status & ER_MB_ERROR;
  300. int offset = b_x * 8 + b_y * stride * 8;
  301. int16_t *top_mv = s->cur_pic.motion_val[0][mvy_stride * b_y + mvx_stride * b_x];
  302. int16_t *bottom_mv = s->cur_pic.motion_val[0][mvy_stride * (b_y + 1) + mvx_stride * b_x];
  303. if (!(top_damage || bottom_damage))
  304. continue; // both undamaged
  305. if ((!top_intra) && (!bottom_intra) &&
  306. FFABS(top_mv[0] - bottom_mv[0]) +
  307. FFABS(top_mv[1] + bottom_mv[1]) < 2)
  308. continue;
  309. for (x = 0; x < 8; x++) {
  310. int a, b, c, d;
  311. a = dst[offset + x + 7 * stride] - dst[offset + x + 6 * stride];
  312. b = dst[offset + x + 8 * stride] - dst[offset + x + 7 * stride];
  313. c = dst[offset + x + 9 * stride] - dst[offset + x + 8 * stride];
  314. d = FFABS(b) - ((FFABS(a) + FFABS(c) + 1) >> 1);
  315. d = FFMAX(d, 0);
  316. if (b < 0)
  317. d = -d;
  318. if (d == 0)
  319. continue;
  320. if (!(top_damage && bottom_damage))
  321. d = d * 16 / 9;
  322. if (top_damage) {
  323. dst[offset + x + 7 * stride] = cm[dst[offset + x + 7 * stride] + ((d * 7) >> 4)];
  324. dst[offset + x + 6 * stride] = cm[dst[offset + x + 6 * stride] + ((d * 5) >> 4)];
  325. dst[offset + x + 5 * stride] = cm[dst[offset + x + 5 * stride] + ((d * 3) >> 4)];
  326. dst[offset + x + 4 * stride] = cm[dst[offset + x + 4 * stride] + ((d * 1) >> 4)];
  327. }
  328. if (bottom_damage) {
  329. dst[offset + x + 8 * stride] = cm[dst[offset + x + 8 * stride] - ((d * 7) >> 4)];
  330. dst[offset + x + 9 * stride] = cm[dst[offset + x + 9 * stride] - ((d * 5) >> 4)];
  331. dst[offset + x + 10 * stride] = cm[dst[offset + x + 10 * stride] - ((d * 3) >> 4)];
  332. dst[offset + x + 11 * stride] = cm[dst[offset + x + 11 * stride] - ((d * 1) >> 4)];
  333. }
  334. }
  335. }
  336. }
  337. }
  338. #define MV_FROZEN 8
  339. #define MV_CHANGED 4
  340. #define MV_UNCHANGED 2
  341. #define MV_LISTED 1
  342. static av_always_inline void add_blocklist(int (*blocklist)[2], int *blocklist_length, uint8_t *fixed, int mb_x, int mb_y, int mb_xy)
  343. {
  344. if (fixed[mb_xy])
  345. return;
  346. fixed[mb_xy] = MV_LISTED;
  347. blocklist[ *blocklist_length ][0] = mb_x;
  348. blocklist[(*blocklist_length)++][1] = mb_y;
  349. }
  350. static void guess_mv(ERContext *s)
  351. {
  352. int (*blocklist)[2], (*next_blocklist)[2];
  353. uint8_t *fixed;
  354. const ptrdiff_t mb_stride = s->mb_stride;
  355. const int mb_width = s->mb_width;
  356. int mb_height = s->mb_height;
  357. int i, depth, num_avail;
  358. int mb_x, mb_y;
  359. ptrdiff_t mot_step, mot_stride;
  360. int blocklist_length, next_blocklist_length;
  361. if (s->last_pic.f && s->last_pic.f->data[0])
  362. mb_height = FFMIN(mb_height, (s->last_pic.f->height+15)>>4);
  363. if (s->next_pic.f && s->next_pic.f->data[0])
  364. mb_height = FFMIN(mb_height, (s->next_pic.f->height+15)>>4);
  365. blocklist = (int (*)[2])s->er_temp_buffer;
  366. next_blocklist = blocklist + s->mb_stride * s->mb_height;
  367. fixed = (uint8_t *)(next_blocklist + s->mb_stride * s->mb_height);
  368. set_mv_strides(s, &mot_step, &mot_stride);
  369. num_avail = 0;
  370. if (s->last_pic.motion_val[0])
  371. ff_thread_await_progress(s->last_pic.tf, mb_height-1, 0);
  372. for (i = 0; i < mb_width * mb_height; i++) {
  373. const int mb_xy = s->mb_index2xy[i];
  374. int f = 0;
  375. int error = s->error_status_table[mb_xy];
  376. if (IS_INTRA(s->cur_pic.mb_type[mb_xy]))
  377. f = MV_FROZEN; // intra // FIXME check
  378. if (!(error & ER_MV_ERROR))
  379. f = MV_FROZEN; // inter with undamaged MV
  380. fixed[mb_xy] = f;
  381. if (f == MV_FROZEN)
  382. num_avail++;
  383. else if(s->last_pic.f->data[0] && s->last_pic.motion_val[0]){
  384. const int mb_y= mb_xy / s->mb_stride;
  385. const int mb_x= mb_xy % s->mb_stride;
  386. const int mot_index= (mb_x + mb_y*mot_stride) * mot_step;
  387. s->cur_pic.motion_val[0][mot_index][0]= s->last_pic.motion_val[0][mot_index][0];
  388. s->cur_pic.motion_val[0][mot_index][1]= s->last_pic.motion_val[0][mot_index][1];
  389. s->cur_pic.ref_index[0][4*mb_xy] = s->last_pic.ref_index[0][4*mb_xy];
  390. }
  391. }
  392. if ((!(s->avctx->error_concealment&FF_EC_GUESS_MVS)) ||
  393. num_avail <= FFMAX(mb_width, mb_height) / 2) {
  394. for (mb_y = 0; mb_y < mb_height; mb_y++) {
  395. for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
  396. const int mb_xy = mb_x + mb_y * s->mb_stride;
  397. int mv_dir = (s->last_pic.f && s->last_pic.f->data[0]) ? MV_DIR_FORWARD : MV_DIR_BACKWARD;
  398. if (IS_INTRA(s->cur_pic.mb_type[mb_xy]))
  399. continue;
  400. if (!(s->error_status_table[mb_xy] & ER_MV_ERROR))
  401. continue;
  402. s->mv[0][0][0] = 0;
  403. s->mv[0][0][1] = 0;
  404. s->decode_mb(s->opaque, 0, mv_dir, MV_TYPE_16X16, &s->mv,
  405. mb_x, mb_y, 0, 0);
  406. }
  407. }
  408. return;
  409. }
  410. blocklist_length = 0;
  411. for (mb_y = 0; mb_y < mb_height; mb_y++) {
  412. for (mb_x = 0; mb_x < mb_width; mb_x++) {
  413. const int mb_xy = mb_x + mb_y * mb_stride;
  414. if (fixed[mb_xy] == MV_FROZEN) {
  415. if (mb_x) add_blocklist(blocklist, &blocklist_length, fixed, mb_x - 1, mb_y, mb_xy - 1);
  416. if (mb_y) add_blocklist(blocklist, &blocklist_length, fixed, mb_x, mb_y - 1, mb_xy - mb_stride);
  417. if (mb_x+1 < mb_width) add_blocklist(blocklist, &blocklist_length, fixed, mb_x + 1, mb_y, mb_xy + 1);
  418. if (mb_y+1 < mb_height) add_blocklist(blocklist, &blocklist_length, fixed, mb_x, mb_y + 1, mb_xy + mb_stride);
  419. }
  420. }
  421. }
  422. for (depth = 0; ; depth++) {
  423. int changed, pass, none_left;
  424. int blocklist_index;
  425. none_left = 1;
  426. changed = 1;
  427. for (pass = 0; (changed || pass < 2) && pass < 10; pass++) {
  428. int score_sum = 0;
  429. changed = 0;
  430. for (blocklist_index = 0; blocklist_index < blocklist_length; blocklist_index++) {
  431. const int mb_x = blocklist[blocklist_index][0];
  432. const int mb_y = blocklist[blocklist_index][1];
  433. const int mb_xy = mb_x + mb_y * mb_stride;
  434. int mv_predictor[8][2];
  435. int ref[8];
  436. int pred_count;
  437. int j;
  438. int best_score;
  439. int best_pred;
  440. int mot_index;
  441. int prev_x, prev_y, prev_ref;
  442. if ((mb_x ^ mb_y ^ pass) & 1)
  443. continue;
  444. av_assert2(fixed[mb_xy] != MV_FROZEN);
  445. av_assert1(!IS_INTRA(s->cur_pic.mb_type[mb_xy]));
  446. av_assert1(s->last_pic.f && s->last_pic.f->data[0]);
  447. j = 0;
  448. if (mb_x > 0)
  449. j |= fixed[mb_xy - 1];
  450. if (mb_x + 1 < mb_width)
  451. j |= fixed[mb_xy + 1];
  452. if (mb_y > 0)
  453. j |= fixed[mb_xy - mb_stride];
  454. if (mb_y + 1 < mb_height)
  455. j |= fixed[mb_xy + mb_stride];
  456. av_assert2(j & MV_FROZEN);
  457. if (!(j & MV_CHANGED) && pass > 1)
  458. continue;
  459. none_left = 0;
  460. pred_count = 0;
  461. mot_index = (mb_x + mb_y * mot_stride) * mot_step;
  462. if (mb_x > 0 && fixed[mb_xy - 1] > 1) {
  463. mv_predictor[pred_count][0] =
  464. s->cur_pic.motion_val[0][mot_index - mot_step][0];
  465. mv_predictor[pred_count][1] =
  466. s->cur_pic.motion_val[0][mot_index - mot_step][1];
  467. ref[pred_count] =
  468. s->cur_pic.ref_index[0][4 * (mb_xy - 1)];
  469. pred_count++;
  470. }
  471. if (mb_x + 1 < mb_width && fixed[mb_xy + 1] > 1) {
  472. mv_predictor[pred_count][0] =
  473. s->cur_pic.motion_val[0][mot_index + mot_step][0];
  474. mv_predictor[pred_count][1] =
  475. s->cur_pic.motion_val[0][mot_index + mot_step][1];
  476. ref[pred_count] =
  477. s->cur_pic.ref_index[0][4 * (mb_xy + 1)];
  478. pred_count++;
  479. }
  480. if (mb_y > 0 && fixed[mb_xy - mb_stride] > 1) {
  481. mv_predictor[pred_count][0] =
  482. s->cur_pic.motion_val[0][mot_index - mot_stride * mot_step][0];
  483. mv_predictor[pred_count][1] =
  484. s->cur_pic.motion_val[0][mot_index - mot_stride * mot_step][1];
  485. ref[pred_count] =
  486. s->cur_pic.ref_index[0][4 * (mb_xy - s->mb_stride)];
  487. pred_count++;
  488. }
  489. if (mb_y + 1<mb_height && fixed[mb_xy + mb_stride] > 1) {
  490. mv_predictor[pred_count][0] =
  491. s->cur_pic.motion_val[0][mot_index + mot_stride * mot_step][0];
  492. mv_predictor[pred_count][1] =
  493. s->cur_pic.motion_val[0][mot_index + mot_stride * mot_step][1];
  494. ref[pred_count] =
  495. s->cur_pic.ref_index[0][4 * (mb_xy + s->mb_stride)];
  496. pred_count++;
  497. }
  498. if (pred_count == 0)
  499. continue;
  500. if (pred_count > 1) {
  501. int sum_x = 0, sum_y = 0, sum_r = 0;
  502. int max_x, max_y, min_x, min_y, max_r, min_r;
  503. for (j = 0; j < pred_count; j++) {
  504. sum_x += mv_predictor[j][0];
  505. sum_y += mv_predictor[j][1];
  506. sum_r += ref[j];
  507. if (j && ref[j] != ref[j - 1])
  508. goto skip_mean_and_median;
  509. }
  510. /* mean */
  511. mv_predictor[pred_count][0] = sum_x / j;
  512. mv_predictor[pred_count][1] = sum_y / j;
  513. ref[pred_count] = sum_r / j;
  514. /* median */
  515. if (pred_count >= 3) {
  516. min_y = min_x = min_r = 99999;
  517. max_y = max_x = max_r = -99999;
  518. } else {
  519. min_x = min_y = max_x = max_y = min_r = max_r = 0;
  520. }
  521. for (j = 0; j < pred_count; j++) {
  522. max_x = FFMAX(max_x, mv_predictor[j][0]);
  523. max_y = FFMAX(max_y, mv_predictor[j][1]);
  524. max_r = FFMAX(max_r, ref[j]);
  525. min_x = FFMIN(min_x, mv_predictor[j][0]);
  526. min_y = FFMIN(min_y, mv_predictor[j][1]);
  527. min_r = FFMIN(min_r, ref[j]);
  528. }
  529. mv_predictor[pred_count + 1][0] = sum_x - max_x - min_x;
  530. mv_predictor[pred_count + 1][1] = sum_y - max_y - min_y;
  531. ref[pred_count + 1] = sum_r - max_r - min_r;
  532. if (pred_count == 4) {
  533. mv_predictor[pred_count + 1][0] /= 2;
  534. mv_predictor[pred_count + 1][1] /= 2;
  535. ref[pred_count + 1] /= 2;
  536. }
  537. pred_count += 2;
  538. }
  539. skip_mean_and_median:
  540. /* zero MV */
  541. mv_predictor[pred_count][0] =
  542. mv_predictor[pred_count][1] =
  543. ref[pred_count] = 0;
  544. pred_count++;
  545. prev_x = s->cur_pic.motion_val[0][mot_index][0];
  546. prev_y = s->cur_pic.motion_val[0][mot_index][1];
  547. prev_ref = s->cur_pic.ref_index[0][4 * mb_xy];
  548. /* last MV */
  549. mv_predictor[pred_count][0] = prev_x;
  550. mv_predictor[pred_count][1] = prev_y;
  551. ref[pred_count] = prev_ref;
  552. pred_count++;
  553. best_pred = 0;
  554. best_score = 256 * 256 * 256 * 64;
  555. for (j = 0; j < pred_count; j++) {
  556. int *linesize = s->cur_pic.f->linesize;
  557. int score = 0;
  558. uint8_t *src = s->cur_pic.f->data[0] +
  559. mb_x * 16 + mb_y * 16 * linesize[0];
  560. s->cur_pic.motion_val[0][mot_index][0] =
  561. s->mv[0][0][0] = mv_predictor[j][0];
  562. s->cur_pic.motion_val[0][mot_index][1] =
  563. s->mv[0][0][1] = mv_predictor[j][1];
  564. // predictor intra or otherwise not available
  565. if (ref[j] < 0)
  566. continue;
  567. s->decode_mb(s->opaque, ref[j], MV_DIR_FORWARD,
  568. MV_TYPE_16X16, &s->mv, mb_x, mb_y, 0, 0);
  569. if (mb_x > 0 && fixed[mb_xy - 1] > 1) {
  570. int k;
  571. for (k = 0; k < 16; k++)
  572. score += FFABS(src[k * linesize[0] - 1] -
  573. src[k * linesize[0]]);
  574. }
  575. if (mb_x + 1 < mb_width && fixed[mb_xy + 1] > 1) {
  576. int k;
  577. for (k = 0; k < 16; k++)
  578. score += FFABS(src[k * linesize[0] + 15] -
  579. src[k * linesize[0] + 16]);
  580. }
  581. if (mb_y > 0 && fixed[mb_xy - mb_stride] > 1) {
  582. int k;
  583. for (k = 0; k < 16; k++)
  584. score += FFABS(src[k - linesize[0]] - src[k]);
  585. }
  586. if (mb_y + 1 < mb_height && fixed[mb_xy + mb_stride] > 1) {
  587. int k;
  588. for (k = 0; k < 16; k++)
  589. score += FFABS(src[k + linesize[0] * 15] -
  590. src[k + linesize[0] * 16]);
  591. }
  592. if (score <= best_score) { // <= will favor the last MV
  593. best_score = score;
  594. best_pred = j;
  595. }
  596. }
  597. score_sum += best_score;
  598. s->mv[0][0][0] = mv_predictor[best_pred][0];
  599. s->mv[0][0][1] = mv_predictor[best_pred][1];
  600. for (i = 0; i < mot_step; i++)
  601. for (j = 0; j < mot_step; j++) {
  602. s->cur_pic.motion_val[0][mot_index + i + j * mot_stride][0] = s->mv[0][0][0];
  603. s->cur_pic.motion_val[0][mot_index + i + j * mot_stride][1] = s->mv[0][0][1];
  604. }
  605. s->decode_mb(s->opaque, ref[best_pred], MV_DIR_FORWARD,
  606. MV_TYPE_16X16, &s->mv, mb_x, mb_y, 0, 0);
  607. if (s->mv[0][0][0] != prev_x || s->mv[0][0][1] != prev_y) {
  608. fixed[mb_xy] = MV_CHANGED;
  609. changed++;
  610. } else
  611. fixed[mb_xy] = MV_UNCHANGED;
  612. }
  613. }
  614. if (none_left)
  615. return;
  616. next_blocklist_length = 0;
  617. for (blocklist_index = 0; blocklist_index < blocklist_length; blocklist_index++) {
  618. const int mb_x = blocklist[blocklist_index][0];
  619. const int mb_y = blocklist[blocklist_index][1];
  620. const int mb_xy = mb_x + mb_y * mb_stride;
  621. if (fixed[mb_xy] & (MV_CHANGED|MV_UNCHANGED|MV_FROZEN)) {
  622. fixed[mb_xy] = MV_FROZEN;
  623. if (mb_x > 0)
  624. add_blocklist(next_blocklist, &next_blocklist_length, fixed, mb_x - 1, mb_y, mb_xy - 1);
  625. if (mb_y > 0)
  626. add_blocklist(next_blocklist, &next_blocklist_length, fixed, mb_x, mb_y - 1, mb_xy - mb_stride);
  627. if (mb_x + 1 < mb_width)
  628. add_blocklist(next_blocklist, &next_blocklist_length, fixed, mb_x + 1, mb_y, mb_xy + 1);
  629. if (mb_y + 1 < mb_height)
  630. add_blocklist(next_blocklist, &next_blocklist_length, fixed, mb_x, mb_y + 1, mb_xy + mb_stride);
  631. }
  632. }
  633. av_assert0(next_blocklist_length <= mb_height * mb_width);
  634. FFSWAP(int , blocklist_length, next_blocklist_length);
  635. FFSWAP(void*, blocklist, next_blocklist);
  636. }
  637. }
  638. static int is_intra_more_likely(ERContext *s)
  639. {
  640. int is_intra_likely, i, j, undamaged_count, skip_amount, mb_x, mb_y;
  641. if (!s->last_pic.f || !s->last_pic.f->data[0])
  642. return 1; // no previous frame available -> use spatial prediction
  643. if (s->avctx->error_concealment & FF_EC_FAVOR_INTER)
  644. return 0;
  645. undamaged_count = 0;
  646. for (i = 0; i < s->mb_num; i++) {
  647. const int mb_xy = s->mb_index2xy[i];
  648. const int error = s->error_status_table[mb_xy];
  649. if (!((error & ER_DC_ERROR) && (error & ER_MV_ERROR)))
  650. undamaged_count++;
  651. }
  652. if (undamaged_count < 5)
  653. return 0; // almost all MBs damaged -> use temporal prediction
  654. // prevent dsp.sad() check, that requires access to the image
  655. if (CONFIG_XVMC &&
  656. s->avctx->hwaccel && s->avctx->hwaccel->decode_mb &&
  657. s->cur_pic.f->pict_type == AV_PICTURE_TYPE_I)
  658. return 1;
  659. skip_amount = FFMAX(undamaged_count / 50, 1); // check only up to 50 MBs
  660. is_intra_likely = 0;
  661. j = 0;
  662. for (mb_y = 0; mb_y < s->mb_height - 1; mb_y++) {
  663. for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
  664. int error;
  665. const int mb_xy = mb_x + mb_y * s->mb_stride;
  666. error = s->error_status_table[mb_xy];
  667. if ((error & ER_DC_ERROR) && (error & ER_MV_ERROR))
  668. continue; // skip damaged
  669. j++;
  670. // skip a few to speed things up
  671. if ((j % skip_amount) != 0)
  672. continue;
  673. if (s->cur_pic.f->pict_type == AV_PICTURE_TYPE_I) {
  674. int *linesize = s->cur_pic.f->linesize;
  675. uint8_t *mb_ptr = s->cur_pic.f->data[0] +
  676. mb_x * 16 + mb_y * 16 * linesize[0];
  677. uint8_t *last_mb_ptr = s->last_pic.f->data[0] +
  678. mb_x * 16 + mb_y * 16 * linesize[0];
  679. if (s->avctx->codec_id == AV_CODEC_ID_H264) {
  680. // FIXME
  681. } else {
  682. ff_thread_await_progress(s->last_pic.tf, mb_y, 0);
  683. }
  684. is_intra_likely += s->mecc.sad[0](NULL, last_mb_ptr, mb_ptr,
  685. linesize[0], 16);
  686. // FIXME need await_progress() here
  687. is_intra_likely -= s->mecc.sad[0](NULL, last_mb_ptr,
  688. last_mb_ptr + linesize[0] * 16,
  689. linesize[0], 16);
  690. } else {
  691. if (IS_INTRA(s->cur_pic.mb_type[mb_xy]))
  692. is_intra_likely++;
  693. else
  694. is_intra_likely--;
  695. }
  696. }
  697. }
  698. // av_log(NULL, AV_LOG_ERROR, "is_intra_likely: %d type:%d\n", is_intra_likely, s->pict_type);
  699. return is_intra_likely > 0;
  700. }
  701. void ff_er_frame_start(ERContext *s)
  702. {
  703. if (!s->avctx->error_concealment)
  704. return;
  705. if (!s->mecc_inited) {
  706. ff_me_cmp_init(&s->mecc, s->avctx);
  707. s->mecc_inited = 1;
  708. }
  709. memset(s->error_status_table, ER_MB_ERROR | VP_START | ER_MB_END,
  710. s->mb_stride * s->mb_height * sizeof(uint8_t));
  711. atomic_init(&s->error_count, 3 * s->mb_num);
  712. s->error_occurred = 0;
  713. }
  714. static int er_supported(ERContext *s)
  715. {
  716. if(s->avctx->hwaccel && s->avctx->hwaccel->decode_slice ||
  717. !s->cur_pic.f ||
  718. s->cur_pic.field_picture
  719. )
  720. return 0;
  721. return 1;
  722. }
  723. /**
  724. * Add a slice.
  725. * @param endx x component of the last macroblock, can be -1
  726. * for the last of the previous line
  727. * @param status the status at the end (ER_MV_END, ER_AC_ERROR, ...), it is
  728. * assumed that no earlier end or error of the same type occurred
  729. */
  730. void ff_er_add_slice(ERContext *s, int startx, int starty,
  731. int endx, int endy, int status)
  732. {
  733. const int start_i = av_clip(startx + starty * s->mb_width, 0, s->mb_num - 1);
  734. const int end_i = av_clip(endx + endy * s->mb_width, 0, s->mb_num);
  735. const int start_xy = s->mb_index2xy[start_i];
  736. const int end_xy = s->mb_index2xy[end_i];
  737. int mask = -1;
  738. if (s->avctx->hwaccel && s->avctx->hwaccel->decode_slice)
  739. return;
  740. if (start_i > end_i || start_xy > end_xy) {
  741. av_log(s->avctx, AV_LOG_ERROR,
  742. "internal error, slice end before start\n");
  743. return;
  744. }
  745. if (!s->avctx->error_concealment)
  746. return;
  747. mask &= ~VP_START;
  748. if (status & (ER_AC_ERROR | ER_AC_END)) {
  749. mask &= ~(ER_AC_ERROR | ER_AC_END);
  750. atomic_fetch_add(&s->error_count, start_i - end_i - 1);
  751. }
  752. if (status & (ER_DC_ERROR | ER_DC_END)) {
  753. mask &= ~(ER_DC_ERROR | ER_DC_END);
  754. atomic_fetch_add(&s->error_count, start_i - end_i - 1);
  755. }
  756. if (status & (ER_MV_ERROR | ER_MV_END)) {
  757. mask &= ~(ER_MV_ERROR | ER_MV_END);
  758. atomic_fetch_add(&s->error_count, start_i - end_i - 1);
  759. }
  760. if (status & ER_MB_ERROR) {
  761. s->error_occurred = 1;
  762. atomic_store(&s->error_count, INT_MAX);
  763. }
  764. if (mask == ~0x7F) {
  765. memset(&s->error_status_table[start_xy], 0,
  766. (end_xy - start_xy) * sizeof(uint8_t));
  767. } else {
  768. int i;
  769. for (i = start_xy; i < end_xy; i++)
  770. s->error_status_table[i] &= mask;
  771. }
  772. if (end_i == s->mb_num)
  773. atomic_store(&s->error_count, INT_MAX);
  774. else {
  775. s->error_status_table[end_xy] &= mask;
  776. s->error_status_table[end_xy] |= status;
  777. }
  778. s->error_status_table[start_xy] |= VP_START;
  779. if (start_xy > 0 && !(s->avctx->active_thread_type & FF_THREAD_SLICE) &&
  780. er_supported(s) && s->avctx->skip_top * s->mb_width < start_i) {
  781. int prev_status = s->error_status_table[s->mb_index2xy[start_i - 1]];
  782. prev_status &= ~ VP_START;
  783. if (prev_status != (ER_MV_END | ER_DC_END | ER_AC_END)) {
  784. s->error_occurred = 1;
  785. atomic_store(&s->error_count, INT_MAX);
  786. }
  787. }
  788. }
  789. void ff_er_frame_end(ERContext *s)
  790. {
  791. int *linesize = NULL;
  792. int i, mb_x, mb_y, error, error_type, dc_error, mv_error, ac_error;
  793. int distance;
  794. int threshold_part[4] = { 100, 100, 100 };
  795. int threshold = 50;
  796. int is_intra_likely;
  797. int size = s->b8_stride * 2 * s->mb_height;
  798. /* We do not support ER of field pictures yet,
  799. * though it should not crash if enabled. */
  800. if (!s->avctx->error_concealment || !atomic_load(&s->error_count) ||
  801. s->avctx->lowres ||
  802. !er_supported(s) ||
  803. atomic_load(&s->error_count) == 3 * s->mb_width *
  804. (s->avctx->skip_top + s->avctx->skip_bottom)) {
  805. return;
  806. }
  807. linesize = s->cur_pic.f->linesize;
  808. for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
  809. int status = s->error_status_table[mb_x + (s->mb_height - 1) * s->mb_stride];
  810. if (status != 0x7F)
  811. break;
  812. }
  813. if ( mb_x == s->mb_width
  814. && s->avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO
  815. && (FFALIGN(s->avctx->height, 16)&16)
  816. && atomic_load(&s->error_count) == 3 * s->mb_width * (s->avctx->skip_top + s->avctx->skip_bottom + 1)
  817. ) {
  818. av_log(s->avctx, AV_LOG_DEBUG, "ignoring last missing slice\n");
  819. return;
  820. }
  821. if (s->last_pic.f) {
  822. if (s->last_pic.f->width != s->cur_pic.f->width ||
  823. s->last_pic.f->height != s->cur_pic.f->height ||
  824. s->last_pic.f->format != s->cur_pic.f->format) {
  825. av_log(s->avctx, AV_LOG_WARNING, "Cannot use previous picture in error concealment\n");
  826. memset(&s->last_pic, 0, sizeof(s->last_pic));
  827. }
  828. }
  829. if (s->next_pic.f) {
  830. if (s->next_pic.f->width != s->cur_pic.f->width ||
  831. s->next_pic.f->height != s->cur_pic.f->height ||
  832. s->next_pic.f->format != s->cur_pic.f->format) {
  833. av_log(s->avctx, AV_LOG_WARNING, "Cannot use next picture in error concealment\n");
  834. memset(&s->next_pic, 0, sizeof(s->next_pic));
  835. }
  836. }
  837. if (!s->cur_pic.motion_val[0] || !s->cur_pic.ref_index[0]) {
  838. av_log(s->avctx, AV_LOG_ERROR, "Warning MVs not available\n");
  839. for (i = 0; i < 2; i++) {
  840. s->ref_index_buf[i] = av_buffer_allocz(s->mb_stride * s->mb_height * 4 * sizeof(uint8_t));
  841. s->motion_val_buf[i] = av_buffer_allocz((size + 4) * 2 * sizeof(uint16_t));
  842. if (!s->ref_index_buf[i] || !s->motion_val_buf[i])
  843. break;
  844. s->cur_pic.ref_index[i] = s->ref_index_buf[i]->data;
  845. s->cur_pic.motion_val[i] = (int16_t (*)[2])s->motion_val_buf[i]->data + 4;
  846. }
  847. if (i < 2) {
  848. for (i = 0; i < 2; i++) {
  849. av_buffer_unref(&s->ref_index_buf[i]);
  850. av_buffer_unref(&s->motion_val_buf[i]);
  851. s->cur_pic.ref_index[i] = NULL;
  852. s->cur_pic.motion_val[i] = NULL;
  853. }
  854. return;
  855. }
  856. }
  857. if (s->avctx->debug & FF_DEBUG_ER) {
  858. for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
  859. for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
  860. int status = s->error_status_table[mb_x + mb_y * s->mb_stride];
  861. av_log(s->avctx, AV_LOG_DEBUG, "%2X ", status);
  862. }
  863. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  864. }
  865. }
  866. #if 1
  867. /* handle overlapping slices */
  868. for (error_type = 1; error_type <= 3; error_type++) {
  869. int end_ok = 0;
  870. for (i = s->mb_num - 1; i >= 0; i--) {
  871. const int mb_xy = s->mb_index2xy[i];
  872. int error = s->error_status_table[mb_xy];
  873. if (error & (1 << error_type))
  874. end_ok = 1;
  875. if (error & (8 << error_type))
  876. end_ok = 1;
  877. if (!end_ok)
  878. s->error_status_table[mb_xy] |= 1 << error_type;
  879. if (error & VP_START)
  880. end_ok = 0;
  881. }
  882. }
  883. #endif
  884. #if 1
  885. /* handle slices with partitions of different length */
  886. if (s->partitioned_frame) {
  887. int end_ok = 0;
  888. for (i = s->mb_num - 1; i >= 0; i--) {
  889. const int mb_xy = s->mb_index2xy[i];
  890. int error = s->error_status_table[mb_xy];
  891. if (error & ER_AC_END)
  892. end_ok = 0;
  893. if ((error & ER_MV_END) ||
  894. (error & ER_DC_END) ||
  895. (error & ER_AC_ERROR))
  896. end_ok = 1;
  897. if (!end_ok)
  898. s->error_status_table[mb_xy]|= ER_AC_ERROR;
  899. if (error & VP_START)
  900. end_ok = 0;
  901. }
  902. }
  903. #endif
  904. /* handle missing slices */
  905. if (s->avctx->err_recognition & AV_EF_EXPLODE) {
  906. int end_ok = 1;
  907. // FIXME + 100 hack
  908. for (i = s->mb_num - 2; i >= s->mb_width + 100; i--) {
  909. const int mb_xy = s->mb_index2xy[i];
  910. int error1 = s->error_status_table[mb_xy];
  911. int error2 = s->error_status_table[s->mb_index2xy[i + 1]];
  912. if (error1 & VP_START)
  913. end_ok = 1;
  914. if (error2 == (VP_START | ER_MB_ERROR | ER_MB_END) &&
  915. error1 != (VP_START | ER_MB_ERROR | ER_MB_END) &&
  916. ((error1 & ER_AC_END) || (error1 & ER_DC_END) ||
  917. (error1 & ER_MV_END))) {
  918. // end & uninit
  919. end_ok = 0;
  920. }
  921. if (!end_ok)
  922. s->error_status_table[mb_xy] |= ER_MB_ERROR;
  923. }
  924. }
  925. #if 1
  926. /* backward mark errors */
  927. distance = 9999999;
  928. for (error_type = 1; error_type <= 3; error_type++) {
  929. for (i = s->mb_num - 1; i >= 0; i--) {
  930. const int mb_xy = s->mb_index2xy[i];
  931. int error = s->error_status_table[mb_xy];
  932. if (!s->mbskip_table || !s->mbskip_table[mb_xy]) // FIXME partition specific
  933. distance++;
  934. if (error & (1 << error_type))
  935. distance = 0;
  936. if (s->partitioned_frame) {
  937. if (distance < threshold_part[error_type - 1])
  938. s->error_status_table[mb_xy] |= 1 << error_type;
  939. } else {
  940. if (distance < threshold)
  941. s->error_status_table[mb_xy] |= 1 << error_type;
  942. }
  943. if (error & VP_START)
  944. distance = 9999999;
  945. }
  946. }
  947. #endif
  948. /* forward mark errors */
  949. error = 0;
  950. for (i = 0; i < s->mb_num; i++) {
  951. const int mb_xy = s->mb_index2xy[i];
  952. int old_error = s->error_status_table[mb_xy];
  953. if (old_error & VP_START) {
  954. error = old_error & ER_MB_ERROR;
  955. } else {
  956. error |= old_error & ER_MB_ERROR;
  957. s->error_status_table[mb_xy] |= error;
  958. }
  959. }
  960. #if 1
  961. /* handle not partitioned case */
  962. if (!s->partitioned_frame) {
  963. for (i = 0; i < s->mb_num; i++) {
  964. const int mb_xy = s->mb_index2xy[i];
  965. int error = s->error_status_table[mb_xy];
  966. if (error & ER_MB_ERROR)
  967. error |= ER_MB_ERROR;
  968. s->error_status_table[mb_xy] = error;
  969. }
  970. }
  971. #endif
  972. dc_error = ac_error = mv_error = 0;
  973. for (i = 0; i < s->mb_num; i++) {
  974. const int mb_xy = s->mb_index2xy[i];
  975. int error = s->error_status_table[mb_xy];
  976. if (error & ER_DC_ERROR)
  977. dc_error++;
  978. if (error & ER_AC_ERROR)
  979. ac_error++;
  980. if (error & ER_MV_ERROR)
  981. mv_error++;
  982. }
  983. av_log(s->avctx, AV_LOG_INFO, "concealing %d DC, %d AC, %d MV errors in %c frame\n",
  984. dc_error, ac_error, mv_error, av_get_picture_type_char(s->cur_pic.f->pict_type));
  985. is_intra_likely = is_intra_more_likely(s);
  986. /* set unknown mb-type to most likely */
  987. for (i = 0; i < s->mb_num; i++) {
  988. const int mb_xy = s->mb_index2xy[i];
  989. int error = s->error_status_table[mb_xy];
  990. if (!((error & ER_DC_ERROR) && (error & ER_MV_ERROR)))
  991. continue;
  992. if (is_intra_likely)
  993. s->cur_pic.mb_type[mb_xy] = MB_TYPE_INTRA4x4;
  994. else
  995. s->cur_pic.mb_type[mb_xy] = MB_TYPE_16x16 | MB_TYPE_L0;
  996. }
  997. // change inter to intra blocks if no reference frames are available
  998. if (!(s->last_pic.f && s->last_pic.f->data[0]) &&
  999. !(s->next_pic.f && s->next_pic.f->data[0]))
  1000. for (i = 0; i < s->mb_num; i++) {
  1001. const int mb_xy = s->mb_index2xy[i];
  1002. if (!IS_INTRA(s->cur_pic.mb_type[mb_xy]))
  1003. s->cur_pic.mb_type[mb_xy] = MB_TYPE_INTRA4x4;
  1004. }
  1005. /* handle inter blocks with damaged AC */
  1006. for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
  1007. for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
  1008. const int mb_xy = mb_x + mb_y * s->mb_stride;
  1009. const int mb_type = s->cur_pic.mb_type[mb_xy];
  1010. const int dir = !(s->last_pic.f && s->last_pic.f->data[0]);
  1011. const int mv_dir = dir ? MV_DIR_BACKWARD : MV_DIR_FORWARD;
  1012. int mv_type;
  1013. int error = s->error_status_table[mb_xy];
  1014. if (IS_INTRA(mb_type))
  1015. continue; // intra
  1016. if (error & ER_MV_ERROR)
  1017. continue; // inter with damaged MV
  1018. if (!(error & ER_AC_ERROR))
  1019. continue; // undamaged inter
  1020. if (IS_8X8(mb_type)) {
  1021. int mb_index = mb_x * 2 + mb_y * 2 * s->b8_stride;
  1022. int j;
  1023. mv_type = MV_TYPE_8X8;
  1024. for (j = 0; j < 4; j++) {
  1025. s->mv[0][j][0] = s->cur_pic.motion_val[dir][mb_index + (j & 1) + (j >> 1) * s->b8_stride][0];
  1026. s->mv[0][j][1] = s->cur_pic.motion_val[dir][mb_index + (j & 1) + (j >> 1) * s->b8_stride][1];
  1027. }
  1028. } else {
  1029. mv_type = MV_TYPE_16X16;
  1030. s->mv[0][0][0] = s->cur_pic.motion_val[dir][mb_x * 2 + mb_y * 2 * s->b8_stride][0];
  1031. s->mv[0][0][1] = s->cur_pic.motion_val[dir][mb_x * 2 + mb_y * 2 * s->b8_stride][1];
  1032. }
  1033. s->decode_mb(s->opaque, 0 /* FIXME H.264 partitioned slices need this set */,
  1034. mv_dir, mv_type, &s->mv, mb_x, mb_y, 0, 0);
  1035. }
  1036. }
  1037. /* guess MVs */
  1038. if (s->cur_pic.f->pict_type == AV_PICTURE_TYPE_B) {
  1039. for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
  1040. for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
  1041. int xy = mb_x * 2 + mb_y * 2 * s->b8_stride;
  1042. const int mb_xy = mb_x + mb_y * s->mb_stride;
  1043. const int mb_type = s->cur_pic.mb_type[mb_xy];
  1044. int mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  1045. int error = s->error_status_table[mb_xy];
  1046. if (IS_INTRA(mb_type))
  1047. continue;
  1048. if (!(error & ER_MV_ERROR))
  1049. continue; // inter with undamaged MV
  1050. if (!(error & ER_AC_ERROR))
  1051. continue; // undamaged inter
  1052. if (!(s->last_pic.f && s->last_pic.f->data[0]))
  1053. mv_dir &= ~MV_DIR_FORWARD;
  1054. if (!(s->next_pic.f && s->next_pic.f->data[0]))
  1055. mv_dir &= ~MV_DIR_BACKWARD;
  1056. if (s->pp_time) {
  1057. int time_pp = s->pp_time;
  1058. int time_pb = s->pb_time;
  1059. av_assert0(s->avctx->codec_id != AV_CODEC_ID_H264);
  1060. ff_thread_await_progress(s->next_pic.tf, mb_y, 0);
  1061. s->mv[0][0][0] = s->next_pic.motion_val[0][xy][0] * time_pb / time_pp;
  1062. s->mv[0][0][1] = s->next_pic.motion_val[0][xy][1] * time_pb / time_pp;
  1063. s->mv[1][0][0] = s->next_pic.motion_val[0][xy][0] * (time_pb - time_pp) / time_pp;
  1064. s->mv[1][0][1] = s->next_pic.motion_val[0][xy][1] * (time_pb - time_pp) / time_pp;
  1065. } else {
  1066. s->mv[0][0][0] = 0;
  1067. s->mv[0][0][1] = 0;
  1068. s->mv[1][0][0] = 0;
  1069. s->mv[1][0][1] = 0;
  1070. }
  1071. s->decode_mb(s->opaque, 0, mv_dir, MV_TYPE_16X16, &s->mv,
  1072. mb_x, mb_y, 0, 0);
  1073. }
  1074. }
  1075. } else
  1076. guess_mv(s);
  1077. /* the filters below manipulate raw image, skip them */
  1078. if (CONFIG_XVMC && s->avctx->hwaccel && s->avctx->hwaccel->decode_mb)
  1079. goto ec_clean;
  1080. /* fill DC for inter blocks */
  1081. for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
  1082. for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
  1083. int dc, dcu, dcv, y, n;
  1084. int16_t *dc_ptr;
  1085. uint8_t *dest_y, *dest_cb, *dest_cr;
  1086. const int mb_xy = mb_x + mb_y * s->mb_stride;
  1087. const int mb_type = s->cur_pic.mb_type[mb_xy];
  1088. // error = s->error_status_table[mb_xy];
  1089. if (IS_INTRA(mb_type) && s->partitioned_frame)
  1090. continue;
  1091. // if (error & ER_MV_ERROR)
  1092. // continue; // inter data damaged FIXME is this good?
  1093. dest_y = s->cur_pic.f->data[0] + mb_x * 16 + mb_y * 16 * linesize[0];
  1094. dest_cb = s->cur_pic.f->data[1] + mb_x * 8 + mb_y * 8 * linesize[1];
  1095. dest_cr = s->cur_pic.f->data[2] + mb_x * 8 + mb_y * 8 * linesize[2];
  1096. dc_ptr = &s->dc_val[0][mb_x * 2 + mb_y * 2 * s->b8_stride];
  1097. for (n = 0; n < 4; n++) {
  1098. dc = 0;
  1099. for (y = 0; y < 8; y++) {
  1100. int x;
  1101. for (x = 0; x < 8; x++)
  1102. dc += dest_y[x + (n & 1) * 8 +
  1103. (y + (n >> 1) * 8) * linesize[0]];
  1104. }
  1105. dc_ptr[(n & 1) + (n >> 1) * s->b8_stride] = (dc + 4) >> 3;
  1106. }
  1107. if (!s->cur_pic.f->data[2])
  1108. continue;
  1109. dcu = dcv = 0;
  1110. for (y = 0; y < 8; y++) {
  1111. int x;
  1112. for (x = 0; x < 8; x++) {
  1113. dcu += dest_cb[x + y * linesize[1]];
  1114. dcv += dest_cr[x + y * linesize[2]];
  1115. }
  1116. }
  1117. s->dc_val[1][mb_x + mb_y * s->mb_stride] = (dcu + 4) >> 3;
  1118. s->dc_val[2][mb_x + mb_y * s->mb_stride] = (dcv + 4) >> 3;
  1119. }
  1120. }
  1121. #if 1
  1122. /* guess DC for damaged blocks */
  1123. guess_dc(s, s->dc_val[0], s->mb_width*2, s->mb_height*2, s->b8_stride, 1);
  1124. guess_dc(s, s->dc_val[1], s->mb_width , s->mb_height , s->mb_stride, 0);
  1125. guess_dc(s, s->dc_val[2], s->mb_width , s->mb_height , s->mb_stride, 0);
  1126. #endif
  1127. /* filter luma DC */
  1128. filter181(s->dc_val[0], s->mb_width * 2, s->mb_height * 2, s->b8_stride);
  1129. #if 1
  1130. /* render DC only intra */
  1131. for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
  1132. for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
  1133. uint8_t *dest_y, *dest_cb, *dest_cr;
  1134. const int mb_xy = mb_x + mb_y * s->mb_stride;
  1135. const int mb_type = s->cur_pic.mb_type[mb_xy];
  1136. int error = s->error_status_table[mb_xy];
  1137. if (IS_INTER(mb_type))
  1138. continue;
  1139. if (!(error & ER_AC_ERROR))
  1140. continue; // undamaged
  1141. dest_y = s->cur_pic.f->data[0] + mb_x * 16 + mb_y * 16 * linesize[0];
  1142. dest_cb = s->cur_pic.f->data[1] + mb_x * 8 + mb_y * 8 * linesize[1];
  1143. dest_cr = s->cur_pic.f->data[2] + mb_x * 8 + mb_y * 8 * linesize[2];
  1144. if (!s->cur_pic.f->data[2])
  1145. dest_cb = dest_cr = NULL;
  1146. put_dc(s, dest_y, dest_cb, dest_cr, mb_x, mb_y);
  1147. }
  1148. }
  1149. #endif
  1150. if (s->avctx->error_concealment & FF_EC_DEBLOCK) {
  1151. /* filter horizontal block boundaries */
  1152. h_block_filter(s, s->cur_pic.f->data[0], s->mb_width * 2,
  1153. s->mb_height * 2, linesize[0], 1);
  1154. /* filter vertical block boundaries */
  1155. v_block_filter(s, s->cur_pic.f->data[0], s->mb_width * 2,
  1156. s->mb_height * 2, linesize[0], 1);
  1157. if (s->cur_pic.f->data[2]) {
  1158. h_block_filter(s, s->cur_pic.f->data[1], s->mb_width,
  1159. s->mb_height, linesize[1], 0);
  1160. h_block_filter(s, s->cur_pic.f->data[2], s->mb_width,
  1161. s->mb_height, linesize[2], 0);
  1162. v_block_filter(s, s->cur_pic.f->data[1], s->mb_width,
  1163. s->mb_height, linesize[1], 0);
  1164. v_block_filter(s, s->cur_pic.f->data[2], s->mb_width,
  1165. s->mb_height, linesize[2], 0);
  1166. }
  1167. }
  1168. ec_clean:
  1169. /* clean a few tables */
  1170. for (i = 0; i < s->mb_num; i++) {
  1171. const int mb_xy = s->mb_index2xy[i];
  1172. int error = s->error_status_table[mb_xy];
  1173. if (s->mbskip_table && s->cur_pic.f->pict_type != AV_PICTURE_TYPE_B &&
  1174. (error & (ER_DC_ERROR | ER_MV_ERROR | ER_AC_ERROR))) {
  1175. s->mbskip_table[mb_xy] = 0;
  1176. }
  1177. if (s->mbintra_table)
  1178. s->mbintra_table[mb_xy] = 1;
  1179. }
  1180. for (i = 0; i < 2; i++) {
  1181. av_buffer_unref(&s->ref_index_buf[i]);
  1182. av_buffer_unref(&s->motion_val_buf[i]);
  1183. s->cur_pic.ref_index[i] = NULL;
  1184. s->cur_pic.motion_val[i] = NULL;
  1185. }
  1186. memset(&s->cur_pic, 0, sizeof(ERPicture));
  1187. memset(&s->last_pic, 0, sizeof(ERPicture));
  1188. memset(&s->next_pic, 0, sizeof(ERPicture));
  1189. }