vp8.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. // Copyright 2010 Google Inc. All Rights Reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style license
  4. // that can be found in the COPYING file in the root of the source
  5. // tree. An additional intellectual property rights grant can be found
  6. // in the file PATENTS. All contributing project authors may
  7. // be found in the AUTHORS file in the root of the source tree.
  8. // -----------------------------------------------------------------------------
  9. //
  10. // main entry for the decoder
  11. //
  12. // Author: Skal ([email protected])
  13. #include <stdlib.h>
  14. #include "./alphai.h"
  15. #include "./vp8i.h"
  16. #include "./vp8li.h"
  17. #include "./webpi.h"
  18. #include "../utils/bit_reader_inl.h"
  19. #include "../utils/utils.h"
  20. //------------------------------------------------------------------------------
  21. int WebPGetDecoderVersion(void) {
  22. return (DEC_MAJ_VERSION << 16) | (DEC_MIN_VERSION << 8) | DEC_REV_VERSION;
  23. }
  24. //------------------------------------------------------------------------------
  25. // VP8Decoder
  26. static void SetOk(VP8Decoder* const dec) {
  27. dec->status_ = VP8_STATUS_OK;
  28. dec->error_msg_ = "OK";
  29. }
  30. int VP8InitIoInternal(VP8Io* const io, int version) {
  31. if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_DECODER_ABI_VERSION)) {
  32. return 0; // mismatch error
  33. }
  34. if (io != NULL) {
  35. memset(io, 0, sizeof(*io));
  36. }
  37. return 1;
  38. }
  39. VP8Decoder* VP8New(void) {
  40. VP8Decoder* const dec = (VP8Decoder*)WebPSafeCalloc(1ULL, sizeof(*dec));
  41. if (dec != NULL) {
  42. SetOk(dec);
  43. WebPGetWorkerInterface()->Init(&dec->worker_);
  44. dec->ready_ = 0;
  45. dec->num_parts_minus_one_ = 0;
  46. }
  47. return dec;
  48. }
  49. VP8StatusCode VP8Status(VP8Decoder* const dec) {
  50. if (!dec) return VP8_STATUS_INVALID_PARAM;
  51. return dec->status_;
  52. }
  53. const char* VP8StatusMessage(VP8Decoder* const dec) {
  54. if (dec == NULL) return "no object";
  55. if (!dec->error_msg_) return "OK";
  56. return dec->error_msg_;
  57. }
  58. void VP8Delete(VP8Decoder* const dec) {
  59. if (dec != NULL) {
  60. VP8Clear(dec);
  61. WebPSafeFree(dec);
  62. }
  63. }
  64. int VP8SetError(VP8Decoder* const dec,
  65. VP8StatusCode error, const char* const msg) {
  66. // The oldest error reported takes precedence over the new one.
  67. if (dec->status_ == VP8_STATUS_OK) {
  68. dec->status_ = error;
  69. dec->error_msg_ = msg;
  70. dec->ready_ = 0;
  71. }
  72. return 0;
  73. }
  74. //------------------------------------------------------------------------------
  75. int VP8CheckSignature(const uint8_t* const data, size_t data_size) {
  76. return (data_size >= 3 &&
  77. data[0] == 0x9d && data[1] == 0x01 && data[2] == 0x2a);
  78. }
  79. int VP8GetInfo(const uint8_t* data, size_t data_size, size_t chunk_size,
  80. int* const width, int* const height) {
  81. if (data == NULL || data_size < VP8_FRAME_HEADER_SIZE) {
  82. return 0; // not enough data
  83. }
  84. // check signature
  85. if (!VP8CheckSignature(data + 3, data_size - 3)) {
  86. return 0; // Wrong signature.
  87. } else {
  88. const uint32_t bits = data[0] | (data[1] << 8) | (data[2] << 16);
  89. const int key_frame = !(bits & 1);
  90. const int w = ((data[7] << 8) | data[6]) & 0x3fff;
  91. const int h = ((data[9] << 8) | data[8]) & 0x3fff;
  92. if (!key_frame) { // Not a keyframe.
  93. return 0;
  94. }
  95. if (((bits >> 1) & 7) > 3) {
  96. return 0; // unknown profile
  97. }
  98. if (!((bits >> 4) & 1)) {
  99. return 0; // first frame is invisible!
  100. }
  101. if (((bits >> 5)) >= chunk_size) { // partition_length
  102. return 0; // inconsistent size information.
  103. }
  104. if (w == 0 || h == 0) {
  105. return 0; // We don't support both width and height to be zero.
  106. }
  107. if (width) {
  108. *width = w;
  109. }
  110. if (height) {
  111. *height = h;
  112. }
  113. return 1;
  114. }
  115. }
  116. //------------------------------------------------------------------------------
  117. // Header parsing
  118. static void ResetSegmentHeader(VP8SegmentHeader* const hdr) {
  119. assert(hdr != NULL);
  120. hdr->use_segment_ = 0;
  121. hdr->update_map_ = 0;
  122. hdr->absolute_delta_ = 1;
  123. memset(hdr->quantizer_, 0, sizeof(hdr->quantizer_));
  124. memset(hdr->filter_strength_, 0, sizeof(hdr->filter_strength_));
  125. }
  126. // Paragraph 9.3
  127. static int ParseSegmentHeader(VP8BitReader* br,
  128. VP8SegmentHeader* hdr, VP8Proba* proba) {
  129. assert(br != NULL);
  130. assert(hdr != NULL);
  131. hdr->use_segment_ = VP8Get(br);
  132. if (hdr->use_segment_) {
  133. hdr->update_map_ = VP8Get(br);
  134. if (VP8Get(br)) { // update data
  135. int s;
  136. hdr->absolute_delta_ = VP8Get(br);
  137. for (s = 0; s < NUM_MB_SEGMENTS; ++s) {
  138. hdr->quantizer_[s] = VP8Get(br) ? VP8GetSignedValue(br, 7) : 0;
  139. }
  140. for (s = 0; s < NUM_MB_SEGMENTS; ++s) {
  141. hdr->filter_strength_[s] = VP8Get(br) ? VP8GetSignedValue(br, 6) : 0;
  142. }
  143. }
  144. if (hdr->update_map_) {
  145. int s;
  146. for (s = 0; s < MB_FEATURE_TREE_PROBS; ++s) {
  147. proba->segments_[s] = VP8Get(br) ? VP8GetValue(br, 8) : 255u;
  148. }
  149. }
  150. } else {
  151. hdr->update_map_ = 0;
  152. }
  153. return !br->eof_;
  154. }
  155. // Paragraph 9.5
  156. // This function returns VP8_STATUS_SUSPENDED if we don't have all the
  157. // necessary data in 'buf'.
  158. // This case is not necessarily an error (for incremental decoding).
  159. // Still, no bitreader is ever initialized to make it possible to read
  160. // unavailable memory.
  161. // If we don't even have the partitions' sizes, than VP8_STATUS_NOT_ENOUGH_DATA
  162. // is returned, and this is an unrecoverable error.
  163. // If the partitions were positioned ok, VP8_STATUS_OK is returned.
  164. static VP8StatusCode ParsePartitions(VP8Decoder* const dec,
  165. const uint8_t* buf, size_t size) {
  166. VP8BitReader* const br = &dec->br_;
  167. const uint8_t* sz = buf;
  168. const uint8_t* buf_end = buf + size;
  169. const uint8_t* part_start;
  170. size_t size_left = size;
  171. size_t last_part;
  172. size_t p;
  173. dec->num_parts_minus_one_ = (1 << VP8GetValue(br, 2)) - 1;
  174. last_part = dec->num_parts_minus_one_;
  175. if (size < 3 * last_part) {
  176. // we can't even read the sizes with sz[]! That's a failure.
  177. return VP8_STATUS_NOT_ENOUGH_DATA;
  178. }
  179. part_start = buf + last_part * 3;
  180. size_left -= last_part * 3;
  181. for (p = 0; p < last_part; ++p) {
  182. size_t psize = sz[0] | (sz[1] << 8) | (sz[2] << 16);
  183. if (psize > size_left) psize = size_left;
  184. VP8InitBitReader(dec->parts_ + p, part_start, psize);
  185. part_start += psize;
  186. size_left -= psize;
  187. sz += 3;
  188. }
  189. VP8InitBitReader(dec->parts_ + last_part, part_start, size_left);
  190. return (part_start < buf_end) ? VP8_STATUS_OK :
  191. VP8_STATUS_SUSPENDED; // Init is ok, but there's not enough data
  192. }
  193. // Paragraph 9.4
  194. static int ParseFilterHeader(VP8BitReader* br, VP8Decoder* const dec) {
  195. VP8FilterHeader* const hdr = &dec->filter_hdr_;
  196. hdr->simple_ = VP8Get(br);
  197. hdr->level_ = VP8GetValue(br, 6);
  198. hdr->sharpness_ = VP8GetValue(br, 3);
  199. hdr->use_lf_delta_ = VP8Get(br);
  200. if (hdr->use_lf_delta_) {
  201. if (VP8Get(br)) { // update lf-delta?
  202. int i;
  203. for (i = 0; i < NUM_REF_LF_DELTAS; ++i) {
  204. if (VP8Get(br)) {
  205. hdr->ref_lf_delta_[i] = VP8GetSignedValue(br, 6);
  206. }
  207. }
  208. for (i = 0; i < NUM_MODE_LF_DELTAS; ++i) {
  209. if (VP8Get(br)) {
  210. hdr->mode_lf_delta_[i] = VP8GetSignedValue(br, 6);
  211. }
  212. }
  213. }
  214. }
  215. dec->filter_type_ = (hdr->level_ == 0) ? 0 : hdr->simple_ ? 1 : 2;
  216. return !br->eof_;
  217. }
  218. // Topmost call
  219. int VP8GetHeaders(VP8Decoder* const dec, VP8Io* const io) {
  220. const uint8_t* buf;
  221. size_t buf_size;
  222. VP8FrameHeader* frm_hdr;
  223. VP8PictureHeader* pic_hdr;
  224. VP8BitReader* br;
  225. VP8StatusCode status;
  226. if (dec == NULL) {
  227. return 0;
  228. }
  229. SetOk(dec);
  230. if (io == NULL) {
  231. return VP8SetError(dec, VP8_STATUS_INVALID_PARAM,
  232. "null VP8Io passed to VP8GetHeaders()");
  233. }
  234. buf = io->data;
  235. buf_size = io->data_size;
  236. if (buf_size < 4) {
  237. return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA,
  238. "Truncated header.");
  239. }
  240. // Paragraph 9.1
  241. {
  242. const uint32_t bits = buf[0] | (buf[1] << 8) | (buf[2] << 16);
  243. frm_hdr = &dec->frm_hdr_;
  244. frm_hdr->key_frame_ = !(bits & 1);
  245. frm_hdr->profile_ = (bits >> 1) & 7;
  246. frm_hdr->show_ = (bits >> 4) & 1;
  247. frm_hdr->partition_length_ = (bits >> 5);
  248. if (frm_hdr->profile_ > 3)
  249. return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR,
  250. "Incorrect keyframe parameters.");
  251. if (!frm_hdr->show_)
  252. return VP8SetError(dec, VP8_STATUS_UNSUPPORTED_FEATURE,
  253. "Frame not displayable.");
  254. buf += 3;
  255. buf_size -= 3;
  256. }
  257. pic_hdr = &dec->pic_hdr_;
  258. if (frm_hdr->key_frame_) {
  259. // Paragraph 9.2
  260. if (buf_size < 7) {
  261. return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA,
  262. "cannot parse picture header");
  263. }
  264. if (!VP8CheckSignature(buf, buf_size)) {
  265. return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR,
  266. "Bad code word");
  267. }
  268. pic_hdr->width_ = ((buf[4] << 8) | buf[3]) & 0x3fff;
  269. pic_hdr->xscale_ = buf[4] >> 6; // ratio: 1, 5/4 5/3 or 2
  270. pic_hdr->height_ = ((buf[6] << 8) | buf[5]) & 0x3fff;
  271. pic_hdr->yscale_ = buf[6] >> 6;
  272. buf += 7;
  273. buf_size -= 7;
  274. dec->mb_w_ = (pic_hdr->width_ + 15) >> 4;
  275. dec->mb_h_ = (pic_hdr->height_ + 15) >> 4;
  276. // Setup default output area (can be later modified during io->setup())
  277. io->width = pic_hdr->width_;
  278. io->height = pic_hdr->height_;
  279. // IMPORTANT! use some sane dimensions in crop_* and scaled_* fields.
  280. // So they can be used interchangeably without always testing for
  281. // 'use_cropping'.
  282. io->use_cropping = 0;
  283. io->crop_top = 0;
  284. io->crop_left = 0;
  285. io->crop_right = io->width;
  286. io->crop_bottom = io->height;
  287. io->use_scaling = 0;
  288. io->scaled_width = io->width;
  289. io->scaled_height = io->height;
  290. io->mb_w = io->width; // sanity check
  291. io->mb_h = io->height; // ditto
  292. VP8ResetProba(&dec->proba_);
  293. ResetSegmentHeader(&dec->segment_hdr_);
  294. }
  295. // Check if we have all the partition #0 available, and initialize dec->br_
  296. // to read this partition (and this partition only).
  297. if (frm_hdr->partition_length_ > buf_size) {
  298. return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA,
  299. "bad partition length");
  300. }
  301. br = &dec->br_;
  302. VP8InitBitReader(br, buf, frm_hdr->partition_length_);
  303. buf += frm_hdr->partition_length_;
  304. buf_size -= frm_hdr->partition_length_;
  305. if (frm_hdr->key_frame_) {
  306. pic_hdr->colorspace_ = VP8Get(br);
  307. pic_hdr->clamp_type_ = VP8Get(br);
  308. }
  309. if (!ParseSegmentHeader(br, &dec->segment_hdr_, &dec->proba_)) {
  310. return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR,
  311. "cannot parse segment header");
  312. }
  313. // Filter specs
  314. if (!ParseFilterHeader(br, dec)) {
  315. return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR,
  316. "cannot parse filter header");
  317. }
  318. status = ParsePartitions(dec, buf, buf_size);
  319. if (status != VP8_STATUS_OK) {
  320. return VP8SetError(dec, status, "cannot parse partitions");
  321. }
  322. // quantizer change
  323. VP8ParseQuant(dec);
  324. // Frame buffer marking
  325. if (!frm_hdr->key_frame_) {
  326. return VP8SetError(dec, VP8_STATUS_UNSUPPORTED_FEATURE,
  327. "Not a key frame.");
  328. }
  329. VP8Get(br); // ignore the value of update_proba_
  330. VP8ParseProba(br, dec);
  331. // sanitized state
  332. dec->ready_ = 1;
  333. return 1;
  334. }
  335. //------------------------------------------------------------------------------
  336. // Residual decoding (Paragraph 13.2 / 13.3)
  337. static const uint8_t kCat3[] = { 173, 148, 140, 0 };
  338. static const uint8_t kCat4[] = { 176, 155, 140, 135, 0 };
  339. static const uint8_t kCat5[] = { 180, 157, 141, 134, 130, 0 };
  340. static const uint8_t kCat6[] =
  341. { 254, 254, 243, 230, 196, 177, 153, 140, 133, 130, 129, 0 };
  342. static const uint8_t* const kCat3456[] = { kCat3, kCat4, kCat5, kCat6 };
  343. static const uint8_t kZigzag[16] = {
  344. 0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15
  345. };
  346. // See section 13-2: http://tools.ietf.org/html/rfc6386#section-13.2
  347. static int GetLargeValue(VP8BitReader* const br, const uint8_t* const p) {
  348. int v;
  349. if (!VP8GetBit(br, p[3])) {
  350. if (!VP8GetBit(br, p[4])) {
  351. v = 2;
  352. } else {
  353. v = 3 + VP8GetBit(br, p[5]);
  354. }
  355. } else {
  356. if (!VP8GetBit(br, p[6])) {
  357. if (!VP8GetBit(br, p[7])) {
  358. v = 5 + VP8GetBit(br, 159);
  359. } else {
  360. v = 7 + 2 * VP8GetBit(br, 165);
  361. v += VP8GetBit(br, 145);
  362. }
  363. } else {
  364. const uint8_t* tab;
  365. const int bit1 = VP8GetBit(br, p[8]);
  366. const int bit0 = VP8GetBit(br, p[9 + bit1]);
  367. const int cat = 2 * bit1 + bit0;
  368. v = 0;
  369. for (tab = kCat3456[cat]; *tab; ++tab) {
  370. v += v + VP8GetBit(br, *tab);
  371. }
  372. v += 3 + (8 << cat);
  373. }
  374. }
  375. return v;
  376. }
  377. // Returns the position of the last non-zero coeff plus one
  378. static int GetCoeffs(VP8BitReader* const br, const VP8BandProbas* const prob[],
  379. int ctx, const quant_t dq, int n, int16_t* out) {
  380. const uint8_t* p = prob[n]->probas_[ctx];
  381. for (; n < 16; ++n) {
  382. if (!VP8GetBit(br, p[0])) {
  383. return n; // previous coeff was last non-zero coeff
  384. }
  385. while (!VP8GetBit(br, p[1])) { // sequence of zero coeffs
  386. p = prob[++n]->probas_[0];
  387. if (n == 16) return 16;
  388. }
  389. { // non zero coeff
  390. const VP8ProbaArray* const p_ctx = &prob[n + 1]->probas_[0];
  391. int v;
  392. if (!VP8GetBit(br, p[2])) {
  393. v = 1;
  394. p = p_ctx[1];
  395. } else {
  396. v = GetLargeValue(br, p);
  397. p = p_ctx[2];
  398. }
  399. out[kZigzag[n]] = VP8GetSigned(br, v) * dq[n > 0];
  400. }
  401. }
  402. return 16;
  403. }
  404. static WEBP_INLINE uint32_t NzCodeBits(uint32_t nz_coeffs, int nz, int dc_nz) {
  405. nz_coeffs <<= 2;
  406. nz_coeffs |= (nz > 3) ? 3 : (nz > 1) ? 2 : dc_nz;
  407. return nz_coeffs;
  408. }
  409. static int ParseResiduals(VP8Decoder* const dec,
  410. VP8MB* const mb, VP8BitReader* const token_br) {
  411. const VP8BandProbas* (* const bands)[16 + 1] = dec->proba_.bands_ptr_;
  412. const VP8BandProbas* const * ac_proba;
  413. VP8MBData* const block = dec->mb_data_ + dec->mb_x_;
  414. const VP8QuantMatrix* const q = &dec->dqm_[block->segment_];
  415. int16_t* dst = block->coeffs_;
  416. VP8MB* const left_mb = dec->mb_info_ - 1;
  417. uint8_t tnz, lnz;
  418. uint32_t non_zero_y = 0;
  419. uint32_t non_zero_uv = 0;
  420. int x, y, ch;
  421. uint32_t out_t_nz, out_l_nz;
  422. int first;
  423. memset(dst, 0, 384 * sizeof(*dst));
  424. if (!block->is_i4x4_) { // parse DC
  425. int16_t dc[16] = { 0 };
  426. const int ctx = mb->nz_dc_ + left_mb->nz_dc_;
  427. const int nz = GetCoeffs(token_br, bands[1], ctx, q->y2_mat_, 0, dc);
  428. mb->nz_dc_ = left_mb->nz_dc_ = (nz > 0);
  429. if (nz > 1) { // more than just the DC -> perform the full transform
  430. VP8TransformWHT(dc, dst);
  431. } else { // only DC is non-zero -> inlined simplified transform
  432. int i;
  433. const int dc0 = (dc[0] + 3) >> 3;
  434. for (i = 0; i < 16 * 16; i += 16) dst[i] = dc0;
  435. }
  436. first = 1;
  437. ac_proba = bands[0];
  438. } else {
  439. first = 0;
  440. ac_proba = bands[3];
  441. }
  442. tnz = mb->nz_ & 0x0f;
  443. lnz = left_mb->nz_ & 0x0f;
  444. for (y = 0; y < 4; ++y) {
  445. int l = lnz & 1;
  446. uint32_t nz_coeffs = 0;
  447. for (x = 0; x < 4; ++x) {
  448. const int ctx = l + (tnz & 1);
  449. const int nz = GetCoeffs(token_br, ac_proba, ctx, q->y1_mat_, first, dst);
  450. l = (nz > first);
  451. tnz = (tnz >> 1) | (l << 7);
  452. nz_coeffs = NzCodeBits(nz_coeffs, nz, dst[0] != 0);
  453. dst += 16;
  454. }
  455. tnz >>= 4;
  456. lnz = (lnz >> 1) | (l << 7);
  457. non_zero_y = (non_zero_y << 8) | nz_coeffs;
  458. }
  459. out_t_nz = tnz;
  460. out_l_nz = lnz >> 4;
  461. for (ch = 0; ch < 4; ch += 2) {
  462. uint32_t nz_coeffs = 0;
  463. tnz = mb->nz_ >> (4 + ch);
  464. lnz = left_mb->nz_ >> (4 + ch);
  465. for (y = 0; y < 2; ++y) {
  466. int l = lnz & 1;
  467. for (x = 0; x < 2; ++x) {
  468. const int ctx = l + (tnz & 1);
  469. const int nz = GetCoeffs(token_br, bands[2], ctx, q->uv_mat_, 0, dst);
  470. l = (nz > 0);
  471. tnz = (tnz >> 1) | (l << 3);
  472. nz_coeffs = NzCodeBits(nz_coeffs, nz, dst[0] != 0);
  473. dst += 16;
  474. }
  475. tnz >>= 2;
  476. lnz = (lnz >> 1) | (l << 5);
  477. }
  478. // Note: we don't really need the per-4x4 details for U/V blocks.
  479. non_zero_uv |= nz_coeffs << (4 * ch);
  480. out_t_nz |= (tnz << 4) << ch;
  481. out_l_nz |= (lnz & 0xf0) << ch;
  482. }
  483. mb->nz_ = out_t_nz;
  484. left_mb->nz_ = out_l_nz;
  485. block->non_zero_y_ = non_zero_y;
  486. block->non_zero_uv_ = non_zero_uv;
  487. // We look at the mode-code of each block and check if some blocks have less
  488. // than three non-zero coeffs (code < 2). This is to avoid dithering flat and
  489. // empty blocks.
  490. block->dither_ = (non_zero_uv & 0xaaaa) ? 0 : q->dither_;
  491. return !(non_zero_y | non_zero_uv); // will be used for further optimization
  492. }
  493. //------------------------------------------------------------------------------
  494. // Main loop
  495. int VP8DecodeMB(VP8Decoder* const dec, VP8BitReader* const token_br) {
  496. VP8MB* const left = dec->mb_info_ - 1;
  497. VP8MB* const mb = dec->mb_info_ + dec->mb_x_;
  498. VP8MBData* const block = dec->mb_data_ + dec->mb_x_;
  499. int skip = dec->use_skip_proba_ ? block->skip_ : 0;
  500. if (!skip) {
  501. skip = ParseResiduals(dec, mb, token_br);
  502. } else {
  503. left->nz_ = mb->nz_ = 0;
  504. if (!block->is_i4x4_) {
  505. left->nz_dc_ = mb->nz_dc_ = 0;
  506. }
  507. block->non_zero_y_ = 0;
  508. block->non_zero_uv_ = 0;
  509. block->dither_ = 0;
  510. }
  511. if (dec->filter_type_ > 0) { // store filter info
  512. VP8FInfo* const finfo = dec->f_info_ + dec->mb_x_;
  513. *finfo = dec->fstrengths_[block->segment_][block->is_i4x4_];
  514. finfo->f_inner_ |= !skip;
  515. }
  516. return !token_br->eof_;
  517. }
  518. void VP8InitScanline(VP8Decoder* const dec) {
  519. VP8MB* const left = dec->mb_info_ - 1;
  520. left->nz_ = 0;
  521. left->nz_dc_ = 0;
  522. memset(dec->intra_l_, B_DC_PRED, sizeof(dec->intra_l_));
  523. dec->mb_x_ = 0;
  524. }
  525. static int ParseFrame(VP8Decoder* const dec, VP8Io* io) {
  526. for (dec->mb_y_ = 0; dec->mb_y_ < dec->br_mb_y_; ++dec->mb_y_) {
  527. // Parse bitstream for this row.
  528. VP8BitReader* const token_br =
  529. &dec->parts_[dec->mb_y_ & dec->num_parts_minus_one_];
  530. if (!VP8ParseIntraModeRow(&dec->br_, dec)) {
  531. return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA,
  532. "Premature end-of-partition0 encountered.");
  533. }
  534. for (; dec->mb_x_ < dec->mb_w_; ++dec->mb_x_) {
  535. if (!VP8DecodeMB(dec, token_br)) {
  536. return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA,
  537. "Premature end-of-file encountered.");
  538. }
  539. }
  540. VP8InitScanline(dec); // Prepare for next scanline
  541. // Reconstruct, filter and emit the row.
  542. if (!VP8ProcessRow(dec, io)) {
  543. return VP8SetError(dec, VP8_STATUS_USER_ABORT, "Output aborted.");
  544. }
  545. }
  546. if (dec->mt_method_ > 0) {
  547. if (!WebPGetWorkerInterface()->Sync(&dec->worker_)) return 0;
  548. }
  549. return 1;
  550. }
  551. // Main entry point
  552. int VP8Decode(VP8Decoder* const dec, VP8Io* const io) {
  553. int ok = 0;
  554. if (dec == NULL) {
  555. return 0;
  556. }
  557. if (io == NULL) {
  558. return VP8SetError(dec, VP8_STATUS_INVALID_PARAM,
  559. "NULL VP8Io parameter in VP8Decode().");
  560. }
  561. if (!dec->ready_) {
  562. if (!VP8GetHeaders(dec, io)) {
  563. return 0;
  564. }
  565. }
  566. assert(dec->ready_);
  567. // Finish setting up the decoding parameter. Will call io->setup().
  568. ok = (VP8EnterCritical(dec, io) == VP8_STATUS_OK);
  569. if (ok) { // good to go.
  570. // Will allocate memory and prepare everything.
  571. if (ok) ok = VP8InitFrame(dec, io);
  572. // Main decoding loop
  573. if (ok) ok = ParseFrame(dec, io);
  574. // Exit.
  575. ok &= VP8ExitCritical(dec, io);
  576. }
  577. if (!ok) {
  578. VP8Clear(dec);
  579. return 0;
  580. }
  581. dec->ready_ = 0;
  582. return ok;
  583. }
  584. void VP8Clear(VP8Decoder* const dec) {
  585. if (dec == NULL) {
  586. return;
  587. }
  588. WebPGetWorkerInterface()->End(&dec->worker_);
  589. WebPDeallocateAlphaMemory(dec);
  590. WebPSafeFree(dec->mem_);
  591. dec->mem_ = NULL;
  592. dec->mem_size_ = 0;
  593. memset(&dec->br_, 0, sizeof(dec->br_));
  594. dec->ready_ = 0;
  595. }
  596. //------------------------------------------------------------------------------