decodeframe.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273
  1. /*
  2. * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "vpx_config.h"
  11. #include "vp8_rtcd.h"
  12. #include "./vpx_scale_rtcd.h"
  13. #include "onyxd_int.h"
  14. #include "vp8/common/header.h"
  15. #include "vp8/common/reconintra4x4.h"
  16. #include "vp8/common/reconinter.h"
  17. #include "detokenize.h"
  18. #include "vp8/common/common.h"
  19. #include "vp8/common/invtrans.h"
  20. #include "vp8/common/alloccommon.h"
  21. #include "vp8/common/entropymode.h"
  22. #include "vp8/common/quant_common.h"
  23. #include "vpx_scale/vpx_scale.h"
  24. #include "vp8/common/reconintra.h"
  25. #include "vp8/common/setupintrarecon.h"
  26. #include "decodemv.h"
  27. #include "vp8/common/extend.h"
  28. #if CONFIG_ERROR_CONCEALMENT
  29. #include "error_concealment.h"
  30. #endif
  31. #include "vpx_mem/vpx_mem.h"
  32. #include "vp8/common/threading.h"
  33. #include "decoderthreading.h"
  34. #include "dboolhuff.h"
  35. #include "vpx_dsp/vpx_dsp_common.h"
  36. #include <assert.h>
  37. #include <stdio.h>
  38. void vp8cx_init_de_quantizer(VP8D_COMP *pbi) {
  39. int Q;
  40. VP8_COMMON *const pc = &pbi->common;
  41. for (Q = 0; Q < QINDEX_RANGE; ++Q) {
  42. pc->Y1dequant[Q][0] = (short)vp8_dc_quant(Q, pc->y1dc_delta_q);
  43. pc->Y2dequant[Q][0] = (short)vp8_dc2quant(Q, pc->y2dc_delta_q);
  44. pc->UVdequant[Q][0] = (short)vp8_dc_uv_quant(Q, pc->uvdc_delta_q);
  45. pc->Y1dequant[Q][1] = (short)vp8_ac_yquant(Q);
  46. pc->Y2dequant[Q][1] = (short)vp8_ac2quant(Q, pc->y2ac_delta_q);
  47. pc->UVdequant[Q][1] = (short)vp8_ac_uv_quant(Q, pc->uvac_delta_q);
  48. }
  49. }
  50. void vp8_mb_init_dequantizer(VP8D_COMP *pbi, MACROBLOCKD *xd) {
  51. int i;
  52. int QIndex;
  53. MB_MODE_INFO *mbmi = &xd->mode_info_context->mbmi;
  54. VP8_COMMON *const pc = &pbi->common;
  55. /* Decide whether to use the default or alternate baseline Q value. */
  56. if (xd->segmentation_enabled) {
  57. /* Abs Value */
  58. if (xd->mb_segement_abs_delta == SEGMENT_ABSDATA) {
  59. QIndex = xd->segment_feature_data[MB_LVL_ALT_Q][mbmi->segment_id];
  60. /* Delta Value */
  61. } else {
  62. QIndex = pc->base_qindex +
  63. xd->segment_feature_data[MB_LVL_ALT_Q][mbmi->segment_id];
  64. }
  65. QIndex = (QIndex >= 0) ? ((QIndex <= MAXQ) ? QIndex : MAXQ)
  66. : 0; /* Clamp to valid range */
  67. } else {
  68. QIndex = pc->base_qindex;
  69. }
  70. /* Set up the macroblock dequant constants */
  71. xd->dequant_y1_dc[0] = 1;
  72. xd->dequant_y1[0] = pc->Y1dequant[QIndex][0];
  73. xd->dequant_y2[0] = pc->Y2dequant[QIndex][0];
  74. xd->dequant_uv[0] = pc->UVdequant[QIndex][0];
  75. for (i = 1; i < 16; ++i) {
  76. xd->dequant_y1_dc[i] = xd->dequant_y1[i] = pc->Y1dequant[QIndex][1];
  77. xd->dequant_y2[i] = pc->Y2dequant[QIndex][1];
  78. xd->dequant_uv[i] = pc->UVdequant[QIndex][1];
  79. }
  80. }
  81. static void decode_macroblock(VP8D_COMP *pbi, MACROBLOCKD *xd,
  82. unsigned int mb_idx) {
  83. MB_PREDICTION_MODE mode;
  84. int i;
  85. #if CONFIG_ERROR_CONCEALMENT
  86. int corruption_detected = 0;
  87. #else
  88. (void)mb_idx;
  89. #endif
  90. if (xd->mode_info_context->mbmi.mb_skip_coeff) {
  91. vp8_reset_mb_tokens_context(xd);
  92. } else if (!vp8dx_bool_error(xd->current_bc)) {
  93. int eobtotal;
  94. eobtotal = vp8_decode_mb_tokens(pbi, xd);
  95. /* Special case: Force the loopfilter to skip when eobtotal is zero */
  96. xd->mode_info_context->mbmi.mb_skip_coeff = (eobtotal == 0);
  97. }
  98. mode = xd->mode_info_context->mbmi.mode;
  99. if (xd->segmentation_enabled) vp8_mb_init_dequantizer(pbi, xd);
  100. #if CONFIG_ERROR_CONCEALMENT
  101. if (pbi->ec_active) {
  102. int throw_residual;
  103. /* When we have independent partitions we can apply residual even
  104. * though other partitions within the frame are corrupt.
  105. */
  106. throw_residual =
  107. (!pbi->independent_partitions && pbi->frame_corrupt_residual);
  108. throw_residual = (throw_residual || vp8dx_bool_error(xd->current_bc));
  109. if ((mb_idx >= pbi->mvs_corrupt_from_mb || throw_residual)) {
  110. /* MB with corrupt residuals or corrupt mode/motion vectors.
  111. * Better to use the predictor as reconstruction.
  112. */
  113. pbi->frame_corrupt_residual = 1;
  114. memset(xd->qcoeff, 0, sizeof(xd->qcoeff));
  115. corruption_detected = 1;
  116. /* force idct to be skipped for B_PRED and use the
  117. * prediction only for reconstruction
  118. * */
  119. memset(xd->eobs, 0, 25);
  120. }
  121. }
  122. #endif
  123. /* do prediction */
  124. if (xd->mode_info_context->mbmi.ref_frame == INTRA_FRAME) {
  125. vp8_build_intra_predictors_mbuv_s(
  126. xd, xd->recon_above[1], xd->recon_above[2], xd->recon_left[1],
  127. xd->recon_left[2], xd->recon_left_stride[1], xd->dst.u_buffer,
  128. xd->dst.v_buffer, xd->dst.uv_stride);
  129. if (mode != B_PRED) {
  130. vp8_build_intra_predictors_mby_s(
  131. xd, xd->recon_above[0], xd->recon_left[0], xd->recon_left_stride[0],
  132. xd->dst.y_buffer, xd->dst.y_stride);
  133. } else {
  134. short *DQC = xd->dequant_y1;
  135. int dst_stride = xd->dst.y_stride;
  136. /* clear out residual eob info */
  137. if (xd->mode_info_context->mbmi.mb_skip_coeff) memset(xd->eobs, 0, 25);
  138. intra_prediction_down_copy(xd, xd->recon_above[0] + 16);
  139. for (i = 0; i < 16; ++i) {
  140. BLOCKD *b = &xd->block[i];
  141. unsigned char *dst = xd->dst.y_buffer + b->offset;
  142. B_PREDICTION_MODE b_mode = xd->mode_info_context->bmi[i].as_mode;
  143. unsigned char *Above = dst - dst_stride;
  144. unsigned char *yleft = dst - 1;
  145. int left_stride = dst_stride;
  146. unsigned char top_left = Above[-1];
  147. vp8_intra4x4_predict(Above, yleft, left_stride, b_mode, dst, dst_stride,
  148. top_left);
  149. if (xd->eobs[i]) {
  150. if (xd->eobs[i] > 1) {
  151. vp8_dequant_idct_add(b->qcoeff, DQC, dst, dst_stride);
  152. } else {
  153. vp8_dc_only_idct_add(b->qcoeff[0] * DQC[0], dst, dst_stride, dst,
  154. dst_stride);
  155. memset(b->qcoeff, 0, 2 * sizeof(b->qcoeff[0]));
  156. }
  157. }
  158. }
  159. }
  160. } else {
  161. vp8_build_inter_predictors_mb(xd);
  162. }
  163. #if CONFIG_ERROR_CONCEALMENT
  164. if (corruption_detected) {
  165. return;
  166. }
  167. #endif
  168. if (!xd->mode_info_context->mbmi.mb_skip_coeff) {
  169. /* dequantization and idct */
  170. if (mode != B_PRED) {
  171. short *DQC = xd->dequant_y1;
  172. if (mode != SPLITMV) {
  173. BLOCKD *b = &xd->block[24];
  174. /* do 2nd order transform on the dc block */
  175. if (xd->eobs[24] > 1) {
  176. vp8_dequantize_b(b, xd->dequant_y2);
  177. vp8_short_inv_walsh4x4(&b->dqcoeff[0], xd->qcoeff);
  178. memset(b->qcoeff, 0, 16 * sizeof(b->qcoeff[0]));
  179. } else {
  180. b->dqcoeff[0] = b->qcoeff[0] * xd->dequant_y2[0];
  181. vp8_short_inv_walsh4x4_1(&b->dqcoeff[0], xd->qcoeff);
  182. memset(b->qcoeff, 0, 2 * sizeof(b->qcoeff[0]));
  183. }
  184. /* override the dc dequant constant in order to preserve the
  185. * dc components
  186. */
  187. DQC = xd->dequant_y1_dc;
  188. }
  189. vp8_dequant_idct_add_y_block(xd->qcoeff, DQC, xd->dst.y_buffer,
  190. xd->dst.y_stride, xd->eobs);
  191. }
  192. vp8_dequant_idct_add_uv_block(xd->qcoeff + 16 * 16, xd->dequant_uv,
  193. xd->dst.u_buffer, xd->dst.v_buffer,
  194. xd->dst.uv_stride, xd->eobs + 16);
  195. }
  196. }
  197. static int get_delta_q(vp8_reader *bc, int prev, int *q_update) {
  198. int ret_val = 0;
  199. if (vp8_read_bit(bc)) {
  200. ret_val = vp8_read_literal(bc, 4);
  201. if (vp8_read_bit(bc)) ret_val = -ret_val;
  202. }
  203. /* Trigger a quantizer update if the delta-q value has changed */
  204. if (ret_val != prev) *q_update = 1;
  205. return ret_val;
  206. }
  207. #ifdef PACKET_TESTING
  208. #include <stdio.h>
  209. FILE *vpxlog = 0;
  210. #endif
  211. static void yv12_extend_frame_top_c(YV12_BUFFER_CONFIG *ybf) {
  212. int i;
  213. unsigned char *src_ptr1;
  214. unsigned char *dest_ptr1;
  215. unsigned int Border;
  216. int plane_stride;
  217. /***********/
  218. /* Y Plane */
  219. /***********/
  220. Border = ybf->border;
  221. plane_stride = ybf->y_stride;
  222. src_ptr1 = ybf->y_buffer - Border;
  223. dest_ptr1 = src_ptr1 - (Border * plane_stride);
  224. for (i = 0; i < (int)Border; ++i) {
  225. memcpy(dest_ptr1, src_ptr1, plane_stride);
  226. dest_ptr1 += plane_stride;
  227. }
  228. /***********/
  229. /* U Plane */
  230. /***********/
  231. plane_stride = ybf->uv_stride;
  232. Border /= 2;
  233. src_ptr1 = ybf->u_buffer - Border;
  234. dest_ptr1 = src_ptr1 - (Border * plane_stride);
  235. for (i = 0; i < (int)(Border); ++i) {
  236. memcpy(dest_ptr1, src_ptr1, plane_stride);
  237. dest_ptr1 += plane_stride;
  238. }
  239. /***********/
  240. /* V Plane */
  241. /***********/
  242. src_ptr1 = ybf->v_buffer - Border;
  243. dest_ptr1 = src_ptr1 - (Border * plane_stride);
  244. for (i = 0; i < (int)(Border); ++i) {
  245. memcpy(dest_ptr1, src_ptr1, plane_stride);
  246. dest_ptr1 += plane_stride;
  247. }
  248. }
  249. static void yv12_extend_frame_bottom_c(YV12_BUFFER_CONFIG *ybf) {
  250. int i;
  251. unsigned char *src_ptr1, *src_ptr2;
  252. unsigned char *dest_ptr2;
  253. unsigned int Border;
  254. int plane_stride;
  255. int plane_height;
  256. /***********/
  257. /* Y Plane */
  258. /***********/
  259. Border = ybf->border;
  260. plane_stride = ybf->y_stride;
  261. plane_height = ybf->y_height;
  262. src_ptr1 = ybf->y_buffer - Border;
  263. src_ptr2 = src_ptr1 + (plane_height * plane_stride) - plane_stride;
  264. dest_ptr2 = src_ptr2 + plane_stride;
  265. for (i = 0; i < (int)Border; ++i) {
  266. memcpy(dest_ptr2, src_ptr2, plane_stride);
  267. dest_ptr2 += plane_stride;
  268. }
  269. /***********/
  270. /* U Plane */
  271. /***********/
  272. plane_stride = ybf->uv_stride;
  273. plane_height = ybf->uv_height;
  274. Border /= 2;
  275. src_ptr1 = ybf->u_buffer - Border;
  276. src_ptr2 = src_ptr1 + (plane_height * plane_stride) - plane_stride;
  277. dest_ptr2 = src_ptr2 + plane_stride;
  278. for (i = 0; i < (int)(Border); ++i) {
  279. memcpy(dest_ptr2, src_ptr2, plane_stride);
  280. dest_ptr2 += plane_stride;
  281. }
  282. /***********/
  283. /* V Plane */
  284. /***********/
  285. src_ptr1 = ybf->v_buffer - Border;
  286. src_ptr2 = src_ptr1 + (plane_height * plane_stride) - plane_stride;
  287. dest_ptr2 = src_ptr2 + plane_stride;
  288. for (i = 0; i < (int)(Border); ++i) {
  289. memcpy(dest_ptr2, src_ptr2, plane_stride);
  290. dest_ptr2 += plane_stride;
  291. }
  292. }
  293. static void yv12_extend_frame_left_right_c(YV12_BUFFER_CONFIG *ybf,
  294. unsigned char *y_src,
  295. unsigned char *u_src,
  296. unsigned char *v_src) {
  297. int i;
  298. unsigned char *src_ptr1, *src_ptr2;
  299. unsigned char *dest_ptr1, *dest_ptr2;
  300. unsigned int Border;
  301. int plane_stride;
  302. int plane_height;
  303. int plane_width;
  304. /***********/
  305. /* Y Plane */
  306. /***********/
  307. Border = ybf->border;
  308. plane_stride = ybf->y_stride;
  309. plane_height = 16;
  310. plane_width = ybf->y_width;
  311. /* copy the left and right most columns out */
  312. src_ptr1 = y_src;
  313. src_ptr2 = src_ptr1 + plane_width - 1;
  314. dest_ptr1 = src_ptr1 - Border;
  315. dest_ptr2 = src_ptr2 + 1;
  316. for (i = 0; i < plane_height; ++i) {
  317. memset(dest_ptr1, src_ptr1[0], Border);
  318. memset(dest_ptr2, src_ptr2[0], Border);
  319. src_ptr1 += plane_stride;
  320. src_ptr2 += plane_stride;
  321. dest_ptr1 += plane_stride;
  322. dest_ptr2 += plane_stride;
  323. }
  324. /***********/
  325. /* U Plane */
  326. /***********/
  327. plane_stride = ybf->uv_stride;
  328. plane_height = 8;
  329. plane_width = ybf->uv_width;
  330. Border /= 2;
  331. /* copy the left and right most columns out */
  332. src_ptr1 = u_src;
  333. src_ptr2 = src_ptr1 + plane_width - 1;
  334. dest_ptr1 = src_ptr1 - Border;
  335. dest_ptr2 = src_ptr2 + 1;
  336. for (i = 0; i < plane_height; ++i) {
  337. memset(dest_ptr1, src_ptr1[0], Border);
  338. memset(dest_ptr2, src_ptr2[0], Border);
  339. src_ptr1 += plane_stride;
  340. src_ptr2 += plane_stride;
  341. dest_ptr1 += plane_stride;
  342. dest_ptr2 += plane_stride;
  343. }
  344. /***********/
  345. /* V Plane */
  346. /***********/
  347. /* copy the left and right most columns out */
  348. src_ptr1 = v_src;
  349. src_ptr2 = src_ptr1 + plane_width - 1;
  350. dest_ptr1 = src_ptr1 - Border;
  351. dest_ptr2 = src_ptr2 + 1;
  352. for (i = 0; i < plane_height; ++i) {
  353. memset(dest_ptr1, src_ptr1[0], Border);
  354. memset(dest_ptr2, src_ptr2[0], Border);
  355. src_ptr1 += plane_stride;
  356. src_ptr2 += plane_stride;
  357. dest_ptr1 += plane_stride;
  358. dest_ptr2 += plane_stride;
  359. }
  360. }
  361. static void decode_mb_rows(VP8D_COMP *pbi) {
  362. VP8_COMMON *const pc = &pbi->common;
  363. MACROBLOCKD *const xd = &pbi->mb;
  364. MODE_INFO *lf_mic = xd->mode_info_context;
  365. int ibc = 0;
  366. int num_part = 1 << pc->multi_token_partition;
  367. int recon_yoffset, recon_uvoffset;
  368. int mb_row, mb_col;
  369. int mb_idx = 0;
  370. YV12_BUFFER_CONFIG *yv12_fb_new = pbi->dec_fb_ref[INTRA_FRAME];
  371. int recon_y_stride = yv12_fb_new->y_stride;
  372. int recon_uv_stride = yv12_fb_new->uv_stride;
  373. unsigned char *ref_buffer[MAX_REF_FRAMES][3];
  374. unsigned char *dst_buffer[3];
  375. unsigned char *lf_dst[3];
  376. unsigned char *eb_dst[3];
  377. int i;
  378. int ref_fb_corrupted[MAX_REF_FRAMES];
  379. ref_fb_corrupted[INTRA_FRAME] = 0;
  380. for (i = 1; i < MAX_REF_FRAMES; ++i) {
  381. YV12_BUFFER_CONFIG *this_fb = pbi->dec_fb_ref[i];
  382. ref_buffer[i][0] = this_fb->y_buffer;
  383. ref_buffer[i][1] = this_fb->u_buffer;
  384. ref_buffer[i][2] = this_fb->v_buffer;
  385. ref_fb_corrupted[i] = this_fb->corrupted;
  386. }
  387. /* Set up the buffer pointers */
  388. eb_dst[0] = lf_dst[0] = dst_buffer[0] = yv12_fb_new->y_buffer;
  389. eb_dst[1] = lf_dst[1] = dst_buffer[1] = yv12_fb_new->u_buffer;
  390. eb_dst[2] = lf_dst[2] = dst_buffer[2] = yv12_fb_new->v_buffer;
  391. xd->up_available = 0;
  392. /* Initialize the loop filter for this frame. */
  393. if (pc->filter_level) vp8_loop_filter_frame_init(pc, xd, pc->filter_level);
  394. vp8_setup_intra_recon_top_line(yv12_fb_new);
  395. /* Decode the individual macro block */
  396. for (mb_row = 0; mb_row < pc->mb_rows; ++mb_row) {
  397. if (num_part > 1) {
  398. xd->current_bc = &pbi->mbc[ibc];
  399. ibc++;
  400. if (ibc == num_part) ibc = 0;
  401. }
  402. recon_yoffset = mb_row * recon_y_stride * 16;
  403. recon_uvoffset = mb_row * recon_uv_stride * 8;
  404. /* reset contexts */
  405. xd->above_context = pc->above_context;
  406. memset(xd->left_context, 0, sizeof(ENTROPY_CONTEXT_PLANES));
  407. xd->left_available = 0;
  408. xd->mb_to_top_edge = -((mb_row * 16) << 3);
  409. xd->mb_to_bottom_edge = ((pc->mb_rows - 1 - mb_row) * 16) << 3;
  410. xd->recon_above[0] = dst_buffer[0] + recon_yoffset;
  411. xd->recon_above[1] = dst_buffer[1] + recon_uvoffset;
  412. xd->recon_above[2] = dst_buffer[2] + recon_uvoffset;
  413. xd->recon_left[0] = xd->recon_above[0] - 1;
  414. xd->recon_left[1] = xd->recon_above[1] - 1;
  415. xd->recon_left[2] = xd->recon_above[2] - 1;
  416. xd->recon_above[0] -= xd->dst.y_stride;
  417. xd->recon_above[1] -= xd->dst.uv_stride;
  418. xd->recon_above[2] -= xd->dst.uv_stride;
  419. /* TODO: move to outside row loop */
  420. xd->recon_left_stride[0] = xd->dst.y_stride;
  421. xd->recon_left_stride[1] = xd->dst.uv_stride;
  422. setup_intra_recon_left(xd->recon_left[0], xd->recon_left[1],
  423. xd->recon_left[2], xd->dst.y_stride,
  424. xd->dst.uv_stride);
  425. for (mb_col = 0; mb_col < pc->mb_cols; ++mb_col) {
  426. /* Distance of Mb to the various image edges.
  427. * These are specified to 8th pel as they are always compared to values
  428. * that are in 1/8th pel units
  429. */
  430. xd->mb_to_left_edge = -((mb_col * 16) << 3);
  431. xd->mb_to_right_edge = ((pc->mb_cols - 1 - mb_col) * 16) << 3;
  432. #if CONFIG_ERROR_CONCEALMENT
  433. {
  434. int corrupt_residual =
  435. (!pbi->independent_partitions && pbi->frame_corrupt_residual) ||
  436. vp8dx_bool_error(xd->current_bc);
  437. if (pbi->ec_active &&
  438. xd->mode_info_context->mbmi.ref_frame == INTRA_FRAME &&
  439. corrupt_residual) {
  440. /* We have an intra block with corrupt coefficients, better to
  441. * conceal with an inter block. Interpolate MVs from neighboring
  442. * MBs.
  443. *
  444. * Note that for the first mb with corrupt residual in a frame,
  445. * we might not discover that before decoding the residual. That
  446. * happens after this check, and therefore no inter concealment
  447. * will be done.
  448. */
  449. vp8_interpolate_motion(xd, mb_row, mb_col, pc->mb_rows, pc->mb_cols);
  450. }
  451. }
  452. #endif
  453. xd->dst.y_buffer = dst_buffer[0] + recon_yoffset;
  454. xd->dst.u_buffer = dst_buffer[1] + recon_uvoffset;
  455. xd->dst.v_buffer = dst_buffer[2] + recon_uvoffset;
  456. if (xd->mode_info_context->mbmi.ref_frame >= LAST_FRAME) {
  457. const MV_REFERENCE_FRAME ref = xd->mode_info_context->mbmi.ref_frame;
  458. xd->pre.y_buffer = ref_buffer[ref][0] + recon_yoffset;
  459. xd->pre.u_buffer = ref_buffer[ref][1] + recon_uvoffset;
  460. xd->pre.v_buffer = ref_buffer[ref][2] + recon_uvoffset;
  461. } else {
  462. // ref_frame is INTRA_FRAME, pre buffer should not be used.
  463. xd->pre.y_buffer = 0;
  464. xd->pre.u_buffer = 0;
  465. xd->pre.v_buffer = 0;
  466. }
  467. /* propagate errors from reference frames */
  468. xd->corrupted |= ref_fb_corrupted[xd->mode_info_context->mbmi.ref_frame];
  469. decode_macroblock(pbi, xd, mb_idx);
  470. mb_idx++;
  471. xd->left_available = 1;
  472. /* check if the boolean decoder has suffered an error */
  473. xd->corrupted |= vp8dx_bool_error(xd->current_bc);
  474. xd->recon_above[0] += 16;
  475. xd->recon_above[1] += 8;
  476. xd->recon_above[2] += 8;
  477. xd->recon_left[0] += 16;
  478. xd->recon_left[1] += 8;
  479. xd->recon_left[2] += 8;
  480. recon_yoffset += 16;
  481. recon_uvoffset += 8;
  482. ++xd->mode_info_context; /* next mb */
  483. xd->above_context++;
  484. }
  485. /* adjust to the next row of mbs */
  486. vp8_extend_mb_row(yv12_fb_new, xd->dst.y_buffer + 16, xd->dst.u_buffer + 8,
  487. xd->dst.v_buffer + 8);
  488. ++xd->mode_info_context; /* skip prediction column */
  489. xd->up_available = 1;
  490. if (pc->filter_level) {
  491. if (mb_row > 0) {
  492. if (pc->filter_type == NORMAL_LOOPFILTER) {
  493. vp8_loop_filter_row_normal(pc, lf_mic, mb_row - 1, recon_y_stride,
  494. recon_uv_stride, lf_dst[0], lf_dst[1],
  495. lf_dst[2]);
  496. } else {
  497. vp8_loop_filter_row_simple(pc, lf_mic, mb_row - 1, recon_y_stride,
  498. recon_uv_stride, lf_dst[0], lf_dst[1],
  499. lf_dst[2]);
  500. }
  501. if (mb_row > 1) {
  502. yv12_extend_frame_left_right_c(yv12_fb_new, eb_dst[0], eb_dst[1],
  503. eb_dst[2]);
  504. eb_dst[0] += recon_y_stride * 16;
  505. eb_dst[1] += recon_uv_stride * 8;
  506. eb_dst[2] += recon_uv_stride * 8;
  507. }
  508. lf_dst[0] += recon_y_stride * 16;
  509. lf_dst[1] += recon_uv_stride * 8;
  510. lf_dst[2] += recon_uv_stride * 8;
  511. lf_mic += pc->mb_cols;
  512. lf_mic++; /* Skip border mb */
  513. }
  514. } else {
  515. if (mb_row > 0) {
  516. /**/
  517. yv12_extend_frame_left_right_c(yv12_fb_new, eb_dst[0], eb_dst[1],
  518. eb_dst[2]);
  519. eb_dst[0] += recon_y_stride * 16;
  520. eb_dst[1] += recon_uv_stride * 8;
  521. eb_dst[2] += recon_uv_stride * 8;
  522. }
  523. }
  524. }
  525. if (pc->filter_level) {
  526. if (pc->filter_type == NORMAL_LOOPFILTER) {
  527. vp8_loop_filter_row_normal(pc, lf_mic, mb_row - 1, recon_y_stride,
  528. recon_uv_stride, lf_dst[0], lf_dst[1],
  529. lf_dst[2]);
  530. } else {
  531. vp8_loop_filter_row_simple(pc, lf_mic, mb_row - 1, recon_y_stride,
  532. recon_uv_stride, lf_dst[0], lf_dst[1],
  533. lf_dst[2]);
  534. }
  535. yv12_extend_frame_left_right_c(yv12_fb_new, eb_dst[0], eb_dst[1],
  536. eb_dst[2]);
  537. eb_dst[0] += recon_y_stride * 16;
  538. eb_dst[1] += recon_uv_stride * 8;
  539. eb_dst[2] += recon_uv_stride * 8;
  540. }
  541. yv12_extend_frame_left_right_c(yv12_fb_new, eb_dst[0], eb_dst[1], eb_dst[2]);
  542. yv12_extend_frame_top_c(yv12_fb_new);
  543. yv12_extend_frame_bottom_c(yv12_fb_new);
  544. }
  545. static unsigned int read_partition_size(VP8D_COMP *pbi,
  546. const unsigned char *cx_size) {
  547. unsigned char temp[3];
  548. if (pbi->decrypt_cb) {
  549. pbi->decrypt_cb(pbi->decrypt_state, cx_size, temp, 3);
  550. cx_size = temp;
  551. }
  552. return cx_size[0] + (cx_size[1] << 8) + (cx_size[2] << 16);
  553. }
  554. static int read_is_valid(const unsigned char *start, size_t len,
  555. const unsigned char *end) {
  556. return len != 0 && end > start && len <= (size_t)(end - start);
  557. }
  558. static unsigned int read_available_partition_size(
  559. VP8D_COMP *pbi, const unsigned char *token_part_sizes,
  560. const unsigned char *fragment_start,
  561. const unsigned char *first_fragment_end, const unsigned char *fragment_end,
  562. int i, int num_part) {
  563. VP8_COMMON *pc = &pbi->common;
  564. const unsigned char *partition_size_ptr = token_part_sizes + i * 3;
  565. unsigned int partition_size = 0;
  566. ptrdiff_t bytes_left = fragment_end - fragment_start;
  567. if (bytes_left < 0) {
  568. vpx_internal_error(
  569. &pc->error, VPX_CODEC_CORRUPT_FRAME,
  570. "Truncated packet or corrupt partition. No bytes left %d.",
  571. (int)bytes_left);
  572. }
  573. /* Calculate the length of this partition. The last partition
  574. * size is implicit. If the partition size can't be read, then
  575. * either use the remaining data in the buffer (for EC mode)
  576. * or throw an error.
  577. */
  578. if (i < num_part - 1) {
  579. if (read_is_valid(partition_size_ptr, 3, first_fragment_end)) {
  580. partition_size = read_partition_size(pbi, partition_size_ptr);
  581. } else if (pbi->ec_active) {
  582. partition_size = (unsigned int)bytes_left;
  583. } else {
  584. vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
  585. "Truncated partition size data");
  586. }
  587. } else {
  588. partition_size = (unsigned int)bytes_left;
  589. }
  590. /* Validate the calculated partition length. If the buffer
  591. * described by the partition can't be fully read, then restrict
  592. * it to the portion that can be (for EC mode) or throw an error.
  593. */
  594. if (!read_is_valid(fragment_start, partition_size, fragment_end)) {
  595. if (pbi->ec_active) {
  596. partition_size = (unsigned int)bytes_left;
  597. } else {
  598. vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
  599. "Truncated packet or corrupt partition "
  600. "%d length",
  601. i + 1);
  602. }
  603. }
  604. return partition_size;
  605. }
  606. static void setup_token_decoder(VP8D_COMP *pbi,
  607. const unsigned char *token_part_sizes) {
  608. vp8_reader *bool_decoder = &pbi->mbc[0];
  609. unsigned int partition_idx;
  610. unsigned int fragment_idx;
  611. unsigned int num_token_partitions;
  612. const unsigned char *first_fragment_end =
  613. pbi->fragments.ptrs[0] + pbi->fragments.sizes[0];
  614. TOKEN_PARTITION multi_token_partition =
  615. (TOKEN_PARTITION)vp8_read_literal(&pbi->mbc[8], 2);
  616. if (!vp8dx_bool_error(&pbi->mbc[8])) {
  617. pbi->common.multi_token_partition = multi_token_partition;
  618. }
  619. num_token_partitions = 1 << pbi->common.multi_token_partition;
  620. /* Check for partitions within the fragments and unpack the fragments
  621. * so that each fragment pointer points to its corresponding partition. */
  622. for (fragment_idx = 0; fragment_idx < pbi->fragments.count; ++fragment_idx) {
  623. unsigned int fragment_size = pbi->fragments.sizes[fragment_idx];
  624. const unsigned char *fragment_end =
  625. pbi->fragments.ptrs[fragment_idx] + fragment_size;
  626. /* Special case for handling the first partition since we have already
  627. * read its size. */
  628. if (fragment_idx == 0) {
  629. /* Size of first partition + token partition sizes element */
  630. ptrdiff_t ext_first_part_size = token_part_sizes -
  631. pbi->fragments.ptrs[0] +
  632. 3 * (num_token_partitions - 1);
  633. if (fragment_size < (unsigned int)ext_first_part_size)
  634. vpx_internal_error(&pbi->common.error, VPX_CODEC_CORRUPT_FRAME,
  635. "Corrupted fragment size %d", fragment_size);
  636. fragment_size -= (unsigned int)ext_first_part_size;
  637. if (fragment_size > 0) {
  638. pbi->fragments.sizes[0] = (unsigned int)ext_first_part_size;
  639. /* The fragment contains an additional partition. Move to
  640. * next. */
  641. fragment_idx++;
  642. pbi->fragments.ptrs[fragment_idx] =
  643. pbi->fragments.ptrs[0] + pbi->fragments.sizes[0];
  644. }
  645. }
  646. /* Split the chunk into partitions read from the bitstream */
  647. while (fragment_size > 0) {
  648. ptrdiff_t partition_size = read_available_partition_size(
  649. pbi, token_part_sizes, pbi->fragments.ptrs[fragment_idx],
  650. first_fragment_end, fragment_end, fragment_idx - 1,
  651. num_token_partitions);
  652. pbi->fragments.sizes[fragment_idx] = (unsigned int)partition_size;
  653. if (fragment_size < (unsigned int)partition_size)
  654. vpx_internal_error(&pbi->common.error, VPX_CODEC_CORRUPT_FRAME,
  655. "Corrupted fragment size %d", fragment_size);
  656. fragment_size -= (unsigned int)partition_size;
  657. assert(fragment_idx <= num_token_partitions);
  658. if (fragment_size > 0) {
  659. /* The fragment contains an additional partition.
  660. * Move to next. */
  661. fragment_idx++;
  662. pbi->fragments.ptrs[fragment_idx] =
  663. pbi->fragments.ptrs[fragment_idx - 1] + partition_size;
  664. }
  665. }
  666. }
  667. pbi->fragments.count = num_token_partitions + 1;
  668. for (partition_idx = 1; partition_idx < pbi->fragments.count;
  669. ++partition_idx) {
  670. if (vp8dx_start_decode(bool_decoder, pbi->fragments.ptrs[partition_idx],
  671. pbi->fragments.sizes[partition_idx], pbi->decrypt_cb,
  672. pbi->decrypt_state)) {
  673. vpx_internal_error(&pbi->common.error, VPX_CODEC_MEM_ERROR,
  674. "Failed to allocate bool decoder %d", partition_idx);
  675. }
  676. bool_decoder++;
  677. }
  678. #if CONFIG_MULTITHREAD
  679. /* Clamp number of decoder threads */
  680. if (pbi->decoding_thread_count > num_token_partitions - 1) {
  681. pbi->decoding_thread_count = num_token_partitions - 1;
  682. }
  683. if ((int)pbi->decoding_thread_count > pbi->common.mb_rows - 1) {
  684. assert(pbi->common.mb_rows > 0);
  685. pbi->decoding_thread_count = pbi->common.mb_rows - 1;
  686. }
  687. #endif
  688. }
  689. static void init_frame(VP8D_COMP *pbi) {
  690. VP8_COMMON *const pc = &pbi->common;
  691. MACROBLOCKD *const xd = &pbi->mb;
  692. if (pc->frame_type == KEY_FRAME) {
  693. /* Various keyframe initializations */
  694. memcpy(pc->fc.mvc, vp8_default_mv_context, sizeof(vp8_default_mv_context));
  695. vp8_init_mbmode_probs(pc);
  696. vp8_default_coef_probs(pc);
  697. /* reset the segment feature data to 0 with delta coding (Default state). */
  698. memset(xd->segment_feature_data, 0, sizeof(xd->segment_feature_data));
  699. xd->mb_segement_abs_delta = SEGMENT_DELTADATA;
  700. /* reset the mode ref deltasa for loop filter */
  701. memset(xd->ref_lf_deltas, 0, sizeof(xd->ref_lf_deltas));
  702. memset(xd->mode_lf_deltas, 0, sizeof(xd->mode_lf_deltas));
  703. /* All buffers are implicitly updated on key frames. */
  704. pc->refresh_golden_frame = 1;
  705. pc->refresh_alt_ref_frame = 1;
  706. pc->copy_buffer_to_gf = 0;
  707. pc->copy_buffer_to_arf = 0;
  708. /* Note that Golden and Altref modes cannot be used on a key frame so
  709. * ref_frame_sign_bias[] is undefined and meaningless
  710. */
  711. pc->ref_frame_sign_bias[GOLDEN_FRAME] = 0;
  712. pc->ref_frame_sign_bias[ALTREF_FRAME] = 0;
  713. } else {
  714. /* To enable choice of different interploation filters */
  715. if (!pc->use_bilinear_mc_filter) {
  716. xd->subpixel_predict = vp8_sixtap_predict4x4;
  717. xd->subpixel_predict8x4 = vp8_sixtap_predict8x4;
  718. xd->subpixel_predict8x8 = vp8_sixtap_predict8x8;
  719. xd->subpixel_predict16x16 = vp8_sixtap_predict16x16;
  720. } else {
  721. xd->subpixel_predict = vp8_bilinear_predict4x4;
  722. xd->subpixel_predict8x4 = vp8_bilinear_predict8x4;
  723. xd->subpixel_predict8x8 = vp8_bilinear_predict8x8;
  724. xd->subpixel_predict16x16 = vp8_bilinear_predict16x16;
  725. }
  726. if (pbi->decoded_key_frame && pbi->ec_enabled && !pbi->ec_active) {
  727. pbi->ec_active = 1;
  728. }
  729. }
  730. xd->left_context = &pc->left_context;
  731. xd->mode_info_context = pc->mi;
  732. xd->frame_type = pc->frame_type;
  733. xd->mode_info_context->mbmi.mode = DC_PRED;
  734. xd->mode_info_stride = pc->mode_info_stride;
  735. xd->corrupted = 0; /* init without corruption */
  736. xd->fullpixel_mask = 0xffffffff;
  737. if (pc->full_pixel) xd->fullpixel_mask = 0xfffffff8;
  738. }
  739. int vp8_decode_frame(VP8D_COMP *pbi) {
  740. vp8_reader *const bc = &pbi->mbc[8];
  741. VP8_COMMON *const pc = &pbi->common;
  742. MACROBLOCKD *const xd = &pbi->mb;
  743. const unsigned char *data = pbi->fragments.ptrs[0];
  744. const unsigned int data_sz = pbi->fragments.sizes[0];
  745. const unsigned char *data_end = data + data_sz;
  746. ptrdiff_t first_partition_length_in_bytes;
  747. int i, j, k, l;
  748. const int *const mb_feature_data_bits = vp8_mb_feature_data_bits;
  749. int corrupt_tokens = 0;
  750. int prev_independent_partitions = pbi->independent_partitions;
  751. YV12_BUFFER_CONFIG *yv12_fb_new = pbi->dec_fb_ref[INTRA_FRAME];
  752. /* start with no corruption of current frame */
  753. xd->corrupted = 0;
  754. yv12_fb_new->corrupted = 0;
  755. if (data_end - data < 3) {
  756. if (!pbi->ec_active) {
  757. vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
  758. "Truncated packet");
  759. }
  760. /* Declare the missing frame as an inter frame since it will
  761. be handled as an inter frame when we have estimated its
  762. motion vectors. */
  763. pc->frame_type = INTER_FRAME;
  764. pc->version = 0;
  765. pc->show_frame = 1;
  766. first_partition_length_in_bytes = 0;
  767. } else {
  768. unsigned char clear_buffer[10];
  769. const unsigned char *clear = data;
  770. if (pbi->decrypt_cb) {
  771. int n = (int)VPXMIN(sizeof(clear_buffer), data_sz);
  772. pbi->decrypt_cb(pbi->decrypt_state, data, clear_buffer, n);
  773. clear = clear_buffer;
  774. }
  775. pc->frame_type = (FRAME_TYPE)(clear[0] & 1);
  776. pc->version = (clear[0] >> 1) & 7;
  777. pc->show_frame = (clear[0] >> 4) & 1;
  778. first_partition_length_in_bytes =
  779. (clear[0] | (clear[1] << 8) | (clear[2] << 16)) >> 5;
  780. if (!pbi->ec_active && (data + first_partition_length_in_bytes > data_end ||
  781. data + first_partition_length_in_bytes < data)) {
  782. vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
  783. "Truncated packet or corrupt partition 0 length");
  784. }
  785. data += 3;
  786. clear += 3;
  787. vp8_setup_version(pc);
  788. if (pc->frame_type == KEY_FRAME) {
  789. /* vet via sync code */
  790. /* When error concealment is enabled we should only check the sync
  791. * code if we have enough bits available
  792. */
  793. if (data + 3 < data_end) {
  794. if (clear[0] != 0x9d || clear[1] != 0x01 || clear[2] != 0x2a) {
  795. vpx_internal_error(&pc->error, VPX_CODEC_UNSUP_BITSTREAM,
  796. "Invalid frame sync code");
  797. }
  798. }
  799. /* If error concealment is enabled we should only parse the new size
  800. * if we have enough data. Otherwise we will end up with the wrong
  801. * size.
  802. */
  803. if (data + 6 < data_end) {
  804. pc->Width = (clear[3] | (clear[4] << 8)) & 0x3fff;
  805. pc->horiz_scale = clear[4] >> 6;
  806. pc->Height = (clear[5] | (clear[6] << 8)) & 0x3fff;
  807. pc->vert_scale = clear[6] >> 6;
  808. data += 7;
  809. } else if (!pbi->ec_active) {
  810. vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
  811. "Truncated key frame header");
  812. } else {
  813. /* Error concealment is active, clear the frame. */
  814. data = data_end;
  815. }
  816. } else {
  817. memcpy(&xd->pre, yv12_fb_new, sizeof(YV12_BUFFER_CONFIG));
  818. memcpy(&xd->dst, yv12_fb_new, sizeof(YV12_BUFFER_CONFIG));
  819. }
  820. }
  821. if ((!pbi->decoded_key_frame && pc->frame_type != KEY_FRAME)) {
  822. return -1;
  823. }
  824. init_frame(pbi);
  825. if (vp8dx_start_decode(bc, data, (unsigned int)(data_end - data),
  826. pbi->decrypt_cb, pbi->decrypt_state)) {
  827. vpx_internal_error(&pc->error, VPX_CODEC_MEM_ERROR,
  828. "Failed to allocate bool decoder 0");
  829. }
  830. if (pc->frame_type == KEY_FRAME) {
  831. (void)vp8_read_bit(bc); // colorspace
  832. pc->clamp_type = (CLAMP_TYPE)vp8_read_bit(bc);
  833. }
  834. /* Is segmentation enabled */
  835. xd->segmentation_enabled = (unsigned char)vp8_read_bit(bc);
  836. if (xd->segmentation_enabled) {
  837. /* Signal whether or not the segmentation map is being explicitly updated
  838. * this frame. */
  839. xd->update_mb_segmentation_map = (unsigned char)vp8_read_bit(bc);
  840. xd->update_mb_segmentation_data = (unsigned char)vp8_read_bit(bc);
  841. if (xd->update_mb_segmentation_data) {
  842. xd->mb_segement_abs_delta = (unsigned char)vp8_read_bit(bc);
  843. memset(xd->segment_feature_data, 0, sizeof(xd->segment_feature_data));
  844. /* For each segmentation feature (Quant and loop filter level) */
  845. for (i = 0; i < MB_LVL_MAX; ++i) {
  846. for (j = 0; j < MAX_MB_SEGMENTS; ++j) {
  847. /* Frame level data */
  848. if (vp8_read_bit(bc)) {
  849. xd->segment_feature_data[i][j] =
  850. (signed char)vp8_read_literal(bc, mb_feature_data_bits[i]);
  851. if (vp8_read_bit(bc)) {
  852. xd->segment_feature_data[i][j] = -xd->segment_feature_data[i][j];
  853. }
  854. } else {
  855. xd->segment_feature_data[i][j] = 0;
  856. }
  857. }
  858. }
  859. }
  860. if (xd->update_mb_segmentation_map) {
  861. /* Which macro block level features are enabled */
  862. memset(xd->mb_segment_tree_probs, 255, sizeof(xd->mb_segment_tree_probs));
  863. /* Read the probs used to decode the segment id for each macro block. */
  864. for (i = 0; i < MB_FEATURE_TREE_PROBS; ++i) {
  865. /* If not explicitly set value is defaulted to 255 by memset above */
  866. if (vp8_read_bit(bc)) {
  867. xd->mb_segment_tree_probs[i] = (vp8_prob)vp8_read_literal(bc, 8);
  868. }
  869. }
  870. }
  871. } else {
  872. /* No segmentation updates on this frame */
  873. xd->update_mb_segmentation_map = 0;
  874. xd->update_mb_segmentation_data = 0;
  875. }
  876. /* Read the loop filter level and type */
  877. pc->filter_type = (LOOPFILTERTYPE)vp8_read_bit(bc);
  878. pc->filter_level = vp8_read_literal(bc, 6);
  879. pc->sharpness_level = vp8_read_literal(bc, 3);
  880. /* Read in loop filter deltas applied at the MB level based on mode or ref
  881. * frame. */
  882. xd->mode_ref_lf_delta_update = 0;
  883. xd->mode_ref_lf_delta_enabled = (unsigned char)vp8_read_bit(bc);
  884. if (xd->mode_ref_lf_delta_enabled) {
  885. /* Do the deltas need to be updated */
  886. xd->mode_ref_lf_delta_update = (unsigned char)vp8_read_bit(bc);
  887. if (xd->mode_ref_lf_delta_update) {
  888. /* Send update */
  889. for (i = 0; i < MAX_REF_LF_DELTAS; ++i) {
  890. if (vp8_read_bit(bc)) {
  891. /*sign = vp8_read_bit( bc );*/
  892. xd->ref_lf_deltas[i] = (signed char)vp8_read_literal(bc, 6);
  893. if (vp8_read_bit(bc)) { /* Apply sign */
  894. xd->ref_lf_deltas[i] = xd->ref_lf_deltas[i] * -1;
  895. }
  896. }
  897. }
  898. /* Send update */
  899. for (i = 0; i < MAX_MODE_LF_DELTAS; ++i) {
  900. if (vp8_read_bit(bc)) {
  901. /*sign = vp8_read_bit( bc );*/
  902. xd->mode_lf_deltas[i] = (signed char)vp8_read_literal(bc, 6);
  903. if (vp8_read_bit(bc)) { /* Apply sign */
  904. xd->mode_lf_deltas[i] = xd->mode_lf_deltas[i] * -1;
  905. }
  906. }
  907. }
  908. }
  909. }
  910. setup_token_decoder(pbi, data + first_partition_length_in_bytes);
  911. xd->current_bc = &pbi->mbc[0];
  912. /* Read the default quantizers. */
  913. {
  914. int Q, q_update;
  915. Q = vp8_read_literal(bc, 7); /* AC 1st order Q = default */
  916. pc->base_qindex = Q;
  917. q_update = 0;
  918. pc->y1dc_delta_q = get_delta_q(bc, pc->y1dc_delta_q, &q_update);
  919. pc->y2dc_delta_q = get_delta_q(bc, pc->y2dc_delta_q, &q_update);
  920. pc->y2ac_delta_q = get_delta_q(bc, pc->y2ac_delta_q, &q_update);
  921. pc->uvdc_delta_q = get_delta_q(bc, pc->uvdc_delta_q, &q_update);
  922. pc->uvac_delta_q = get_delta_q(bc, pc->uvac_delta_q, &q_update);
  923. if (q_update) vp8cx_init_de_quantizer(pbi);
  924. /* MB level dequantizer setup */
  925. vp8_mb_init_dequantizer(pbi, &pbi->mb);
  926. }
  927. /* Determine if the golden frame or ARF buffer should be updated and how.
  928. * For all non key frames the GF and ARF refresh flags and sign bias
  929. * flags must be set explicitly.
  930. */
  931. if (pc->frame_type != KEY_FRAME) {
  932. /* Should the GF or ARF be updated from the current frame */
  933. pc->refresh_golden_frame = vp8_read_bit(bc);
  934. #if CONFIG_ERROR_CONCEALMENT
  935. /* Assume we shouldn't refresh golden if the bit is missing */
  936. xd->corrupted |= vp8dx_bool_error(bc);
  937. if (pbi->ec_active && xd->corrupted) pc->refresh_golden_frame = 0;
  938. #endif
  939. pc->refresh_alt_ref_frame = vp8_read_bit(bc);
  940. #if CONFIG_ERROR_CONCEALMENT
  941. /* Assume we shouldn't refresh altref if the bit is missing */
  942. xd->corrupted |= vp8dx_bool_error(bc);
  943. if (pbi->ec_active && xd->corrupted) pc->refresh_alt_ref_frame = 0;
  944. #endif
  945. /* Buffer to buffer copy flags. */
  946. pc->copy_buffer_to_gf = 0;
  947. if (!pc->refresh_golden_frame) {
  948. pc->copy_buffer_to_gf = vp8_read_literal(bc, 2);
  949. }
  950. #if CONFIG_ERROR_CONCEALMENT
  951. /* Assume we shouldn't copy to the golden if the bit is missing */
  952. xd->corrupted |= vp8dx_bool_error(bc);
  953. if (pbi->ec_active && xd->corrupted) pc->copy_buffer_to_gf = 0;
  954. #endif
  955. pc->copy_buffer_to_arf = 0;
  956. if (!pc->refresh_alt_ref_frame) {
  957. pc->copy_buffer_to_arf = vp8_read_literal(bc, 2);
  958. }
  959. #if CONFIG_ERROR_CONCEALMENT
  960. /* Assume we shouldn't copy to the alt-ref if the bit is missing */
  961. xd->corrupted |= vp8dx_bool_error(bc);
  962. if (pbi->ec_active && xd->corrupted) pc->copy_buffer_to_arf = 0;
  963. #endif
  964. pc->ref_frame_sign_bias[GOLDEN_FRAME] = vp8_read_bit(bc);
  965. pc->ref_frame_sign_bias[ALTREF_FRAME] = vp8_read_bit(bc);
  966. }
  967. pc->refresh_entropy_probs = vp8_read_bit(bc);
  968. #if CONFIG_ERROR_CONCEALMENT
  969. /* Assume we shouldn't refresh the probabilities if the bit is
  970. * missing */
  971. xd->corrupted |= vp8dx_bool_error(bc);
  972. if (pbi->ec_active && xd->corrupted) pc->refresh_entropy_probs = 0;
  973. #endif
  974. if (pc->refresh_entropy_probs == 0) {
  975. memcpy(&pc->lfc, &pc->fc, sizeof(pc->fc));
  976. }
  977. pc->refresh_last_frame = pc->frame_type == KEY_FRAME || vp8_read_bit(bc);
  978. #if CONFIG_ERROR_CONCEALMENT
  979. /* Assume we should refresh the last frame if the bit is missing */
  980. xd->corrupted |= vp8dx_bool_error(bc);
  981. if (pbi->ec_active && xd->corrupted) pc->refresh_last_frame = 1;
  982. #endif
  983. if (0) {
  984. FILE *z = fopen("decodestats.stt", "a");
  985. fprintf(z, "%6d F:%d,G:%d,A:%d,L:%d,Q:%d\n", pc->current_video_frame,
  986. pc->frame_type, pc->refresh_golden_frame, pc->refresh_alt_ref_frame,
  987. pc->refresh_last_frame, pc->base_qindex);
  988. fclose(z);
  989. }
  990. {
  991. pbi->independent_partitions = 1;
  992. /* read coef probability tree */
  993. for (i = 0; i < BLOCK_TYPES; ++i) {
  994. for (j = 0; j < COEF_BANDS; ++j) {
  995. for (k = 0; k < PREV_COEF_CONTEXTS; ++k) {
  996. for (l = 0; l < ENTROPY_NODES; ++l) {
  997. vp8_prob *const p = pc->fc.coef_probs[i][j][k] + l;
  998. if (vp8_read(bc, vp8_coef_update_probs[i][j][k][l])) {
  999. *p = (vp8_prob)vp8_read_literal(bc, 8);
  1000. }
  1001. if (k > 0 && *p != pc->fc.coef_probs[i][j][k - 1][l]) {
  1002. pbi->independent_partitions = 0;
  1003. }
  1004. }
  1005. }
  1006. }
  1007. }
  1008. }
  1009. /* clear out the coeff buffer */
  1010. memset(xd->qcoeff, 0, sizeof(xd->qcoeff));
  1011. vp8_decode_mode_mvs(pbi);
  1012. #if CONFIG_ERROR_CONCEALMENT
  1013. if (pbi->ec_active &&
  1014. pbi->mvs_corrupt_from_mb < (unsigned int)pc->mb_cols * pc->mb_rows) {
  1015. /* Motion vectors are missing in this frame. We will try to estimate
  1016. * them and then continue decoding the frame as usual */
  1017. vp8_estimate_missing_mvs(pbi);
  1018. }
  1019. #endif
  1020. memset(pc->above_context, 0, sizeof(ENTROPY_CONTEXT_PLANES) * pc->mb_cols);
  1021. pbi->frame_corrupt_residual = 0;
  1022. #if CONFIG_MULTITHREAD
  1023. if (vpx_atomic_load_acquire(&pbi->b_multithreaded_rd) &&
  1024. pc->multi_token_partition != ONE_PARTITION) {
  1025. unsigned int thread;
  1026. if (vp8mt_decode_mb_rows(pbi, xd)) {
  1027. vp8_decoder_remove_threads(pbi);
  1028. pbi->restart_threads = 1;
  1029. vpx_internal_error(&pbi->common.error, VPX_CODEC_CORRUPT_FRAME, NULL);
  1030. }
  1031. vp8_yv12_extend_frame_borders(yv12_fb_new);
  1032. for (thread = 0; thread < pbi->decoding_thread_count; ++thread) {
  1033. corrupt_tokens |= pbi->mb_row_di[thread].mbd.corrupted;
  1034. }
  1035. } else
  1036. #endif
  1037. {
  1038. decode_mb_rows(pbi);
  1039. corrupt_tokens |= xd->corrupted;
  1040. }
  1041. /* Collect information about decoder corruption. */
  1042. /* 1. Check first boolean decoder for errors. */
  1043. yv12_fb_new->corrupted = vp8dx_bool_error(bc);
  1044. /* 2. Check the macroblock information */
  1045. yv12_fb_new->corrupted |= corrupt_tokens;
  1046. if (!pbi->decoded_key_frame) {
  1047. if (pc->frame_type == KEY_FRAME && !yv12_fb_new->corrupted) {
  1048. pbi->decoded_key_frame = 1;
  1049. } else {
  1050. vpx_internal_error(&pbi->common.error, VPX_CODEC_CORRUPT_FRAME,
  1051. "A stream must start with a complete key frame");
  1052. }
  1053. }
  1054. /* vpx_log("Decoder: Frame Decoded, Size Roughly:%d bytes
  1055. * \n",bc->pos+pbi->bc2.pos); */
  1056. if (pc->refresh_entropy_probs == 0) {
  1057. memcpy(&pc->fc, &pc->lfc, sizeof(pc->fc));
  1058. pbi->independent_partitions = prev_independent_partitions;
  1059. }
  1060. #ifdef PACKET_TESTING
  1061. {
  1062. FILE *f = fopen("decompressor.VP8", "ab");
  1063. unsigned int size = pbi->bc2.pos + pbi->bc.pos + 8;
  1064. fwrite((void *)&size, 4, 1, f);
  1065. fwrite((void *)pbi->Source, size, 1, f);
  1066. fclose(f);
  1067. }
  1068. #endif
  1069. return 0;
  1070. }