decodeframe.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257
  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 (start + len > start && start + len <= end);
  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. /* Calculate the length of this partition. The last partition
  568. * size is implicit. If the partition size can't be read, then
  569. * either use the remaining data in the buffer (for EC mode)
  570. * or throw an error.
  571. */
  572. if (i < num_part - 1) {
  573. if (read_is_valid(partition_size_ptr, 3, first_fragment_end)) {
  574. partition_size = read_partition_size(pbi, partition_size_ptr);
  575. } else if (pbi->ec_active) {
  576. partition_size = (unsigned int)bytes_left;
  577. } else {
  578. vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
  579. "Truncated partition size data");
  580. }
  581. } else {
  582. partition_size = (unsigned int)bytes_left;
  583. }
  584. /* Validate the calculated partition length. If the buffer
  585. * described by the partition can't be fully read, then restrict
  586. * it to the portion that can be (for EC mode) or throw an error.
  587. */
  588. if (!read_is_valid(fragment_start, partition_size, fragment_end)) {
  589. if (pbi->ec_active) {
  590. partition_size = (unsigned int)bytes_left;
  591. } else {
  592. vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
  593. "Truncated packet or corrupt partition "
  594. "%d length",
  595. i + 1);
  596. }
  597. }
  598. return partition_size;
  599. }
  600. static void setup_token_decoder(VP8D_COMP *pbi,
  601. const unsigned char *token_part_sizes) {
  602. vp8_reader *bool_decoder = &pbi->mbc[0];
  603. unsigned int partition_idx;
  604. unsigned int fragment_idx;
  605. unsigned int num_token_partitions;
  606. const unsigned char *first_fragment_end =
  607. pbi->fragments.ptrs[0] + pbi->fragments.sizes[0];
  608. TOKEN_PARTITION multi_token_partition =
  609. (TOKEN_PARTITION)vp8_read_literal(&pbi->mbc[8], 2);
  610. if (!vp8dx_bool_error(&pbi->mbc[8])) {
  611. pbi->common.multi_token_partition = multi_token_partition;
  612. }
  613. num_token_partitions = 1 << pbi->common.multi_token_partition;
  614. /* Check for partitions within the fragments and unpack the fragments
  615. * so that each fragment pointer points to its corresponding partition. */
  616. for (fragment_idx = 0; fragment_idx < pbi->fragments.count; ++fragment_idx) {
  617. unsigned int fragment_size = pbi->fragments.sizes[fragment_idx];
  618. const unsigned char *fragment_end =
  619. pbi->fragments.ptrs[fragment_idx] + fragment_size;
  620. /* Special case for handling the first partition since we have already
  621. * read its size. */
  622. if (fragment_idx == 0) {
  623. /* Size of first partition + token partition sizes element */
  624. ptrdiff_t ext_first_part_size = token_part_sizes -
  625. pbi->fragments.ptrs[0] +
  626. 3 * (num_token_partitions - 1);
  627. fragment_size -= (unsigned int)ext_first_part_size;
  628. if (fragment_size > 0) {
  629. pbi->fragments.sizes[0] = (unsigned int)ext_first_part_size;
  630. /* The fragment contains an additional partition. Move to
  631. * next. */
  632. fragment_idx++;
  633. pbi->fragments.ptrs[fragment_idx] =
  634. pbi->fragments.ptrs[0] + pbi->fragments.sizes[0];
  635. }
  636. }
  637. /* Split the chunk into partitions read from the bitstream */
  638. while (fragment_size > 0) {
  639. ptrdiff_t partition_size = read_available_partition_size(
  640. pbi, token_part_sizes, pbi->fragments.ptrs[fragment_idx],
  641. first_fragment_end, fragment_end, fragment_idx - 1,
  642. num_token_partitions);
  643. pbi->fragments.sizes[fragment_idx] = (unsigned int)partition_size;
  644. fragment_size -= (unsigned int)partition_size;
  645. assert(fragment_idx <= num_token_partitions);
  646. if (fragment_size > 0) {
  647. /* The fragment contains an additional partition.
  648. * Move to next. */
  649. fragment_idx++;
  650. pbi->fragments.ptrs[fragment_idx] =
  651. pbi->fragments.ptrs[fragment_idx - 1] + partition_size;
  652. }
  653. }
  654. }
  655. pbi->fragments.count = num_token_partitions + 1;
  656. for (partition_idx = 1; partition_idx < pbi->fragments.count;
  657. ++partition_idx) {
  658. if (vp8dx_start_decode(bool_decoder, pbi->fragments.ptrs[partition_idx],
  659. pbi->fragments.sizes[partition_idx], pbi->decrypt_cb,
  660. pbi->decrypt_state)) {
  661. vpx_internal_error(&pbi->common.error, VPX_CODEC_MEM_ERROR,
  662. "Failed to allocate bool decoder %d", partition_idx);
  663. }
  664. bool_decoder++;
  665. }
  666. #if CONFIG_MULTITHREAD
  667. /* Clamp number of decoder threads */
  668. if (pbi->decoding_thread_count > num_token_partitions - 1) {
  669. pbi->decoding_thread_count = num_token_partitions - 1;
  670. }
  671. if ((int)pbi->decoding_thread_count > pbi->common.mb_rows - 1) {
  672. assert(pbi->common.mb_rows > 0);
  673. pbi->decoding_thread_count = pbi->common.mb_rows - 1;
  674. }
  675. #endif
  676. }
  677. static void init_frame(VP8D_COMP *pbi) {
  678. VP8_COMMON *const pc = &pbi->common;
  679. MACROBLOCKD *const xd = &pbi->mb;
  680. if (pc->frame_type == KEY_FRAME) {
  681. /* Various keyframe initializations */
  682. memcpy(pc->fc.mvc, vp8_default_mv_context, sizeof(vp8_default_mv_context));
  683. vp8_init_mbmode_probs(pc);
  684. vp8_default_coef_probs(pc);
  685. /* reset the segment feature data to 0 with delta coding (Default state). */
  686. memset(xd->segment_feature_data, 0, sizeof(xd->segment_feature_data));
  687. xd->mb_segement_abs_delta = SEGMENT_DELTADATA;
  688. /* reset the mode ref deltasa for loop filter */
  689. memset(xd->ref_lf_deltas, 0, sizeof(xd->ref_lf_deltas));
  690. memset(xd->mode_lf_deltas, 0, sizeof(xd->mode_lf_deltas));
  691. /* All buffers are implicitly updated on key frames. */
  692. pc->refresh_golden_frame = 1;
  693. pc->refresh_alt_ref_frame = 1;
  694. pc->copy_buffer_to_gf = 0;
  695. pc->copy_buffer_to_arf = 0;
  696. /* Note that Golden and Altref modes cannot be used on a key frame so
  697. * ref_frame_sign_bias[] is undefined and meaningless
  698. */
  699. pc->ref_frame_sign_bias[GOLDEN_FRAME] = 0;
  700. pc->ref_frame_sign_bias[ALTREF_FRAME] = 0;
  701. } else {
  702. /* To enable choice of different interploation filters */
  703. if (!pc->use_bilinear_mc_filter) {
  704. xd->subpixel_predict = vp8_sixtap_predict4x4;
  705. xd->subpixel_predict8x4 = vp8_sixtap_predict8x4;
  706. xd->subpixel_predict8x8 = vp8_sixtap_predict8x8;
  707. xd->subpixel_predict16x16 = vp8_sixtap_predict16x16;
  708. } else {
  709. xd->subpixel_predict = vp8_bilinear_predict4x4;
  710. xd->subpixel_predict8x4 = vp8_bilinear_predict8x4;
  711. xd->subpixel_predict8x8 = vp8_bilinear_predict8x8;
  712. xd->subpixel_predict16x16 = vp8_bilinear_predict16x16;
  713. }
  714. if (pbi->decoded_key_frame && pbi->ec_enabled && !pbi->ec_active) {
  715. pbi->ec_active = 1;
  716. }
  717. }
  718. xd->left_context = &pc->left_context;
  719. xd->mode_info_context = pc->mi;
  720. xd->frame_type = pc->frame_type;
  721. xd->mode_info_context->mbmi.mode = DC_PRED;
  722. xd->mode_info_stride = pc->mode_info_stride;
  723. xd->corrupted = 0; /* init without corruption */
  724. xd->fullpixel_mask = 0xffffffff;
  725. if (pc->full_pixel) xd->fullpixel_mask = 0xfffffff8;
  726. }
  727. int vp8_decode_frame(VP8D_COMP *pbi) {
  728. vp8_reader *const bc = &pbi->mbc[8];
  729. VP8_COMMON *const pc = &pbi->common;
  730. MACROBLOCKD *const xd = &pbi->mb;
  731. const unsigned char *data = pbi->fragments.ptrs[0];
  732. const unsigned int data_sz = pbi->fragments.sizes[0];
  733. const unsigned char *data_end = data + data_sz;
  734. ptrdiff_t first_partition_length_in_bytes;
  735. int i, j, k, l;
  736. const int *const mb_feature_data_bits = vp8_mb_feature_data_bits;
  737. int corrupt_tokens = 0;
  738. int prev_independent_partitions = pbi->independent_partitions;
  739. YV12_BUFFER_CONFIG *yv12_fb_new = pbi->dec_fb_ref[INTRA_FRAME];
  740. /* start with no corruption of current frame */
  741. xd->corrupted = 0;
  742. yv12_fb_new->corrupted = 0;
  743. if (data_end - data < 3) {
  744. if (!pbi->ec_active) {
  745. vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
  746. "Truncated packet");
  747. }
  748. /* Declare the missing frame as an inter frame since it will
  749. be handled as an inter frame when we have estimated its
  750. motion vectors. */
  751. pc->frame_type = INTER_FRAME;
  752. pc->version = 0;
  753. pc->show_frame = 1;
  754. first_partition_length_in_bytes = 0;
  755. } else {
  756. unsigned char clear_buffer[10];
  757. const unsigned char *clear = data;
  758. if (pbi->decrypt_cb) {
  759. int n = (int)VPXMIN(sizeof(clear_buffer), data_sz);
  760. pbi->decrypt_cb(pbi->decrypt_state, data, clear_buffer, n);
  761. clear = clear_buffer;
  762. }
  763. pc->frame_type = (FRAME_TYPE)(clear[0] & 1);
  764. pc->version = (clear[0] >> 1) & 7;
  765. pc->show_frame = (clear[0] >> 4) & 1;
  766. first_partition_length_in_bytes =
  767. (clear[0] | (clear[1] << 8) | (clear[2] << 16)) >> 5;
  768. if (!pbi->ec_active && (data + first_partition_length_in_bytes > data_end ||
  769. data + first_partition_length_in_bytes < data)) {
  770. vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
  771. "Truncated packet or corrupt partition 0 length");
  772. }
  773. data += 3;
  774. clear += 3;
  775. vp8_setup_version(pc);
  776. if (pc->frame_type == KEY_FRAME) {
  777. /* vet via sync code */
  778. /* When error concealment is enabled we should only check the sync
  779. * code if we have enough bits available
  780. */
  781. if (data + 3 < data_end) {
  782. if (clear[0] != 0x9d || clear[1] != 0x01 || clear[2] != 0x2a) {
  783. vpx_internal_error(&pc->error, VPX_CODEC_UNSUP_BITSTREAM,
  784. "Invalid frame sync code");
  785. }
  786. }
  787. /* If error concealment is enabled we should only parse the new size
  788. * if we have enough data. Otherwise we will end up with the wrong
  789. * size.
  790. */
  791. if (data + 6 < data_end) {
  792. pc->Width = (clear[3] | (clear[4] << 8)) & 0x3fff;
  793. pc->horiz_scale = clear[4] >> 6;
  794. pc->Height = (clear[5] | (clear[6] << 8)) & 0x3fff;
  795. pc->vert_scale = clear[6] >> 6;
  796. data += 7;
  797. } else if (!pbi->ec_active) {
  798. vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
  799. "Truncated key frame header");
  800. } else {
  801. /* Error concealment is active, clear the frame. */
  802. data = data_end;
  803. }
  804. } else {
  805. memcpy(&xd->pre, yv12_fb_new, sizeof(YV12_BUFFER_CONFIG));
  806. memcpy(&xd->dst, yv12_fb_new, sizeof(YV12_BUFFER_CONFIG));
  807. }
  808. }
  809. if ((!pbi->decoded_key_frame && pc->frame_type != KEY_FRAME)) {
  810. return -1;
  811. }
  812. init_frame(pbi);
  813. if (vp8dx_start_decode(bc, data, (unsigned int)(data_end - data),
  814. pbi->decrypt_cb, pbi->decrypt_state)) {
  815. vpx_internal_error(&pc->error, VPX_CODEC_MEM_ERROR,
  816. "Failed to allocate bool decoder 0");
  817. }
  818. if (pc->frame_type == KEY_FRAME) {
  819. (void)vp8_read_bit(bc); // colorspace
  820. pc->clamp_type = (CLAMP_TYPE)vp8_read_bit(bc);
  821. }
  822. /* Is segmentation enabled */
  823. xd->segmentation_enabled = (unsigned char)vp8_read_bit(bc);
  824. if (xd->segmentation_enabled) {
  825. /* Signal whether or not the segmentation map is being explicitly updated
  826. * this frame. */
  827. xd->update_mb_segmentation_map = (unsigned char)vp8_read_bit(bc);
  828. xd->update_mb_segmentation_data = (unsigned char)vp8_read_bit(bc);
  829. if (xd->update_mb_segmentation_data) {
  830. xd->mb_segement_abs_delta = (unsigned char)vp8_read_bit(bc);
  831. memset(xd->segment_feature_data, 0, sizeof(xd->segment_feature_data));
  832. /* For each segmentation feature (Quant and loop filter level) */
  833. for (i = 0; i < MB_LVL_MAX; ++i) {
  834. for (j = 0; j < MAX_MB_SEGMENTS; ++j) {
  835. /* Frame level data */
  836. if (vp8_read_bit(bc)) {
  837. xd->segment_feature_data[i][j] =
  838. (signed char)vp8_read_literal(bc, mb_feature_data_bits[i]);
  839. if (vp8_read_bit(bc)) {
  840. xd->segment_feature_data[i][j] = -xd->segment_feature_data[i][j];
  841. }
  842. } else {
  843. xd->segment_feature_data[i][j] = 0;
  844. }
  845. }
  846. }
  847. }
  848. if (xd->update_mb_segmentation_map) {
  849. /* Which macro block level features are enabled */
  850. memset(xd->mb_segment_tree_probs, 255, sizeof(xd->mb_segment_tree_probs));
  851. /* Read the probs used to decode the segment id for each macro block. */
  852. for (i = 0; i < MB_FEATURE_TREE_PROBS; ++i) {
  853. /* If not explicitly set value is defaulted to 255 by memset above */
  854. if (vp8_read_bit(bc)) {
  855. xd->mb_segment_tree_probs[i] = (vp8_prob)vp8_read_literal(bc, 8);
  856. }
  857. }
  858. }
  859. } else {
  860. /* No segmentation updates on this frame */
  861. xd->update_mb_segmentation_map = 0;
  862. xd->update_mb_segmentation_data = 0;
  863. }
  864. /* Read the loop filter level and type */
  865. pc->filter_type = (LOOPFILTERTYPE)vp8_read_bit(bc);
  866. pc->filter_level = vp8_read_literal(bc, 6);
  867. pc->sharpness_level = vp8_read_literal(bc, 3);
  868. /* Read in loop filter deltas applied at the MB level based on mode or ref
  869. * frame. */
  870. xd->mode_ref_lf_delta_update = 0;
  871. xd->mode_ref_lf_delta_enabled = (unsigned char)vp8_read_bit(bc);
  872. if (xd->mode_ref_lf_delta_enabled) {
  873. /* Do the deltas need to be updated */
  874. xd->mode_ref_lf_delta_update = (unsigned char)vp8_read_bit(bc);
  875. if (xd->mode_ref_lf_delta_update) {
  876. /* Send update */
  877. for (i = 0; i < MAX_REF_LF_DELTAS; ++i) {
  878. if (vp8_read_bit(bc)) {
  879. /*sign = vp8_read_bit( bc );*/
  880. xd->ref_lf_deltas[i] = (signed char)vp8_read_literal(bc, 6);
  881. if (vp8_read_bit(bc)) { /* Apply sign */
  882. xd->ref_lf_deltas[i] = xd->ref_lf_deltas[i] * -1;
  883. }
  884. }
  885. }
  886. /* Send update */
  887. for (i = 0; i < MAX_MODE_LF_DELTAS; ++i) {
  888. if (vp8_read_bit(bc)) {
  889. /*sign = vp8_read_bit( bc );*/
  890. xd->mode_lf_deltas[i] = (signed char)vp8_read_literal(bc, 6);
  891. if (vp8_read_bit(bc)) { /* Apply sign */
  892. xd->mode_lf_deltas[i] = xd->mode_lf_deltas[i] * -1;
  893. }
  894. }
  895. }
  896. }
  897. }
  898. setup_token_decoder(pbi, data + first_partition_length_in_bytes);
  899. xd->current_bc = &pbi->mbc[0];
  900. /* Read the default quantizers. */
  901. {
  902. int Q, q_update;
  903. Q = vp8_read_literal(bc, 7); /* AC 1st order Q = default */
  904. pc->base_qindex = Q;
  905. q_update = 0;
  906. pc->y1dc_delta_q = get_delta_q(bc, pc->y1dc_delta_q, &q_update);
  907. pc->y2dc_delta_q = get_delta_q(bc, pc->y2dc_delta_q, &q_update);
  908. pc->y2ac_delta_q = get_delta_q(bc, pc->y2ac_delta_q, &q_update);
  909. pc->uvdc_delta_q = get_delta_q(bc, pc->uvdc_delta_q, &q_update);
  910. pc->uvac_delta_q = get_delta_q(bc, pc->uvac_delta_q, &q_update);
  911. if (q_update) vp8cx_init_de_quantizer(pbi);
  912. /* MB level dequantizer setup */
  913. vp8_mb_init_dequantizer(pbi, &pbi->mb);
  914. }
  915. /* Determine if the golden frame or ARF buffer should be updated and how.
  916. * For all non key frames the GF and ARF refresh flags and sign bias
  917. * flags must be set explicitly.
  918. */
  919. if (pc->frame_type != KEY_FRAME) {
  920. /* Should the GF or ARF be updated from the current frame */
  921. pc->refresh_golden_frame = vp8_read_bit(bc);
  922. #if CONFIG_ERROR_CONCEALMENT
  923. /* Assume we shouldn't refresh golden if the bit is missing */
  924. xd->corrupted |= vp8dx_bool_error(bc);
  925. if (pbi->ec_active && xd->corrupted) pc->refresh_golden_frame = 0;
  926. #endif
  927. pc->refresh_alt_ref_frame = vp8_read_bit(bc);
  928. #if CONFIG_ERROR_CONCEALMENT
  929. /* Assume we shouldn't refresh altref if the bit is missing */
  930. xd->corrupted |= vp8dx_bool_error(bc);
  931. if (pbi->ec_active && xd->corrupted) pc->refresh_alt_ref_frame = 0;
  932. #endif
  933. /* Buffer to buffer copy flags. */
  934. pc->copy_buffer_to_gf = 0;
  935. if (!pc->refresh_golden_frame) {
  936. pc->copy_buffer_to_gf = vp8_read_literal(bc, 2);
  937. }
  938. #if CONFIG_ERROR_CONCEALMENT
  939. /* Assume we shouldn't copy to the golden if the bit is missing */
  940. xd->corrupted |= vp8dx_bool_error(bc);
  941. if (pbi->ec_active && xd->corrupted) pc->copy_buffer_to_gf = 0;
  942. #endif
  943. pc->copy_buffer_to_arf = 0;
  944. if (!pc->refresh_alt_ref_frame) {
  945. pc->copy_buffer_to_arf = vp8_read_literal(bc, 2);
  946. }
  947. #if CONFIG_ERROR_CONCEALMENT
  948. /* Assume we shouldn't copy to the alt-ref if the bit is missing */
  949. xd->corrupted |= vp8dx_bool_error(bc);
  950. if (pbi->ec_active && xd->corrupted) pc->copy_buffer_to_arf = 0;
  951. #endif
  952. pc->ref_frame_sign_bias[GOLDEN_FRAME] = vp8_read_bit(bc);
  953. pc->ref_frame_sign_bias[ALTREF_FRAME] = vp8_read_bit(bc);
  954. }
  955. pc->refresh_entropy_probs = vp8_read_bit(bc);
  956. #if CONFIG_ERROR_CONCEALMENT
  957. /* Assume we shouldn't refresh the probabilities if the bit is
  958. * missing */
  959. xd->corrupted |= vp8dx_bool_error(bc);
  960. if (pbi->ec_active && xd->corrupted) pc->refresh_entropy_probs = 0;
  961. #endif
  962. if (pc->refresh_entropy_probs == 0) {
  963. memcpy(&pc->lfc, &pc->fc, sizeof(pc->fc));
  964. }
  965. pc->refresh_last_frame = pc->frame_type == KEY_FRAME || vp8_read_bit(bc);
  966. #if CONFIG_ERROR_CONCEALMENT
  967. /* Assume we should refresh the last frame if the bit is missing */
  968. xd->corrupted |= vp8dx_bool_error(bc);
  969. if (pbi->ec_active && xd->corrupted) pc->refresh_last_frame = 1;
  970. #endif
  971. if (0) {
  972. FILE *z = fopen("decodestats.stt", "a");
  973. fprintf(z, "%6d F:%d,G:%d,A:%d,L:%d,Q:%d\n", pc->current_video_frame,
  974. pc->frame_type, pc->refresh_golden_frame, pc->refresh_alt_ref_frame,
  975. pc->refresh_last_frame, pc->base_qindex);
  976. fclose(z);
  977. }
  978. {
  979. pbi->independent_partitions = 1;
  980. /* read coef probability tree */
  981. for (i = 0; i < BLOCK_TYPES; ++i) {
  982. for (j = 0; j < COEF_BANDS; ++j) {
  983. for (k = 0; k < PREV_COEF_CONTEXTS; ++k) {
  984. for (l = 0; l < ENTROPY_NODES; ++l) {
  985. vp8_prob *const p = pc->fc.coef_probs[i][j][k] + l;
  986. if (vp8_read(bc, vp8_coef_update_probs[i][j][k][l])) {
  987. *p = (vp8_prob)vp8_read_literal(bc, 8);
  988. }
  989. if (k > 0 && *p != pc->fc.coef_probs[i][j][k - 1][l]) {
  990. pbi->independent_partitions = 0;
  991. }
  992. }
  993. }
  994. }
  995. }
  996. }
  997. /* clear out the coeff buffer */
  998. memset(xd->qcoeff, 0, sizeof(xd->qcoeff));
  999. vp8_decode_mode_mvs(pbi);
  1000. #if CONFIG_ERROR_CONCEALMENT
  1001. if (pbi->ec_active &&
  1002. pbi->mvs_corrupt_from_mb < (unsigned int)pc->mb_cols * pc->mb_rows) {
  1003. /* Motion vectors are missing in this frame. We will try to estimate
  1004. * them and then continue decoding the frame as usual */
  1005. vp8_estimate_missing_mvs(pbi);
  1006. }
  1007. #endif
  1008. memset(pc->above_context, 0, sizeof(ENTROPY_CONTEXT_PLANES) * pc->mb_cols);
  1009. pbi->frame_corrupt_residual = 0;
  1010. #if CONFIG_MULTITHREAD
  1011. if (vpx_atomic_load_acquire(&pbi->b_multithreaded_rd) &&
  1012. pc->multi_token_partition != ONE_PARTITION) {
  1013. unsigned int thread;
  1014. vp8mt_decode_mb_rows(pbi, xd);
  1015. vp8_yv12_extend_frame_borders(yv12_fb_new);
  1016. for (thread = 0; thread < pbi->decoding_thread_count; ++thread) {
  1017. corrupt_tokens |= pbi->mb_row_di[thread].mbd.corrupted;
  1018. }
  1019. } else
  1020. #endif
  1021. {
  1022. decode_mb_rows(pbi);
  1023. corrupt_tokens |= xd->corrupted;
  1024. }
  1025. /* Collect information about decoder corruption. */
  1026. /* 1. Check first boolean decoder for errors. */
  1027. yv12_fb_new->corrupted = vp8dx_bool_error(bc);
  1028. /* 2. Check the macroblock information */
  1029. yv12_fb_new->corrupted |= corrupt_tokens;
  1030. if (!pbi->decoded_key_frame) {
  1031. if (pc->frame_type == KEY_FRAME && !yv12_fb_new->corrupted) {
  1032. pbi->decoded_key_frame = 1;
  1033. } else {
  1034. vpx_internal_error(&pbi->common.error, VPX_CODEC_CORRUPT_FRAME,
  1035. "A stream must start with a complete key frame");
  1036. }
  1037. }
  1038. /* vpx_log("Decoder: Frame Decoded, Size Roughly:%d bytes
  1039. * \n",bc->pos+pbi->bc2.pos); */
  1040. if (pc->refresh_entropy_probs == 0) {
  1041. memcpy(&pc->fc, &pc->lfc, sizeof(pc->fc));
  1042. pbi->independent_partitions = prev_independent_partitions;
  1043. }
  1044. #ifdef PACKET_TESTING
  1045. {
  1046. FILE *f = fopen("decompressor.VP8", "ab");
  1047. unsigned int size = pbi->bc2.pos + pbi->bc.pos + 8;
  1048. fwrite((void *)&size, 4, 1, f);
  1049. fwrite((void *)pbi->Source, size, 1, f);
  1050. fclose(f);
  1051. }
  1052. #endif
  1053. return 0;
  1054. }