vp8.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  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 (pascal.massimino@gmail.com)
  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_ = 1;
  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. // TODO This check would be unnecessary if alpha decompression was separated
  67. // from VP8ProcessRow/FinishRow. This avoids setting 'dec->status_' to
  68. // something other than VP8_STATUS_BITSTREAM_ERROR on alpha decompression
  69. // failure.
  70. if (dec->status_ == VP8_STATUS_OK) {
  71. dec->status_ = error;
  72. dec->error_msg_ = msg;
  73. dec->ready_ = 0;
  74. }
  75. return 0;
  76. }
  77. //------------------------------------------------------------------------------
  78. int VP8CheckSignature(const uint8_t* const data, size_t data_size) {
  79. return (data_size >= 3 &&
  80. data[0] == 0x9d && data[1] == 0x01 && data[2] == 0x2a);
  81. }
  82. int VP8GetInfo(const uint8_t* data, size_t data_size, size_t chunk_size,
  83. int* const width, int* const height) {
  84. if (data == NULL || data_size < VP8_FRAME_HEADER_SIZE) {
  85. return 0; // not enough data
  86. }
  87. // check signature
  88. if (!VP8CheckSignature(data + 3, data_size - 3)) {
  89. return 0; // Wrong signature.
  90. } else {
  91. const uint32_t bits = data[0] | (data[1] << 8) | (data[2] << 16);
  92. const int key_frame = !(bits & 1);
  93. const int w = ((data[7] << 8) | data[6]) & 0x3fff;
  94. const int h = ((data[9] << 8) | data[8]) & 0x3fff;
  95. if (!key_frame) { // Not a keyframe.
  96. return 0;
  97. }
  98. if (((bits >> 1) & 7) > 3) {
  99. return 0; // unknown profile
  100. }
  101. if (!((bits >> 4) & 1)) {
  102. return 0; // first frame is invisible!
  103. }
  104. if (((bits >> 5)) >= chunk_size) { // partition_length
  105. return 0; // inconsistent size information.
  106. }
  107. if (w == 0 || h == 0) {
  108. return 0; // We don't support both width and height to be zero.
  109. }
  110. if (width) {
  111. *width = w;
  112. }
  113. if (height) {
  114. *height = h;
  115. }
  116. return 1;
  117. }
  118. }
  119. //------------------------------------------------------------------------------
  120. // Header parsing
  121. static void ResetSegmentHeader(VP8SegmentHeader* const hdr) {
  122. assert(hdr != NULL);
  123. hdr->use_segment_ = 0;
  124. hdr->update_map_ = 0;
  125. hdr->absolute_delta_ = 1;
  126. memset(hdr->quantizer_, 0, sizeof(hdr->quantizer_));
  127. memset(hdr->filter_strength_, 0, sizeof(hdr->filter_strength_));
  128. }
  129. // Paragraph 9.3
  130. static int ParseSegmentHeader(VP8BitReader* br,
  131. VP8SegmentHeader* hdr, VP8Proba* proba) {
  132. assert(br != NULL);
  133. assert(hdr != NULL);
  134. hdr->use_segment_ = VP8Get(br);
  135. if (hdr->use_segment_) {
  136. hdr->update_map_ = VP8Get(br);
  137. if (VP8Get(br)) { // update data
  138. int s;
  139. hdr->absolute_delta_ = VP8Get(br);
  140. for (s = 0; s < NUM_MB_SEGMENTS; ++s) {
  141. hdr->quantizer_[s] = VP8Get(br) ? VP8GetSignedValue(br, 7) : 0;
  142. }
  143. for (s = 0; s < NUM_MB_SEGMENTS; ++s) {
  144. hdr->filter_strength_[s] = VP8Get(br) ? VP8GetSignedValue(br, 6) : 0;
  145. }
  146. }
  147. if (hdr->update_map_) {
  148. int s;
  149. for (s = 0; s < MB_FEATURE_TREE_PROBS; ++s) {
  150. proba->segments_[s] = VP8Get(br) ? VP8GetValue(br, 8) : 255u;
  151. }
  152. }
  153. } else {
  154. hdr->update_map_ = 0;
  155. }
  156. return !br->eof_;
  157. }
  158. // Paragraph 9.5
  159. // This function returns VP8_STATUS_SUSPENDED if we don't have all the
  160. // necessary data in 'buf'.
  161. // This case is not necessarily an error (for incremental decoding).
  162. // Still, no bitreader is ever initialized to make it possible to read
  163. // unavailable memory.
  164. // If we don't even have the partitions' sizes, than VP8_STATUS_NOT_ENOUGH_DATA
  165. // is returned, and this is an unrecoverable error.
  166. // If the partitions were positioned ok, VP8_STATUS_OK is returned.
  167. static VP8StatusCode ParsePartitions(VP8Decoder* const dec,
  168. const uint8_t* buf, size_t size) {
  169. VP8BitReader* const br = &dec->br_;
  170. const uint8_t* sz = buf;
  171. const uint8_t* buf_end = buf + size;
  172. const uint8_t* part_start;
  173. int last_part;
  174. int p;
  175. dec->num_parts_ = 1 << VP8GetValue(br, 2);
  176. last_part = dec->num_parts_ - 1;
  177. part_start = buf + last_part * 3;
  178. if (buf_end < part_start) {
  179. // we can't even read the sizes with sz[]! That's a failure.
  180. return VP8_STATUS_NOT_ENOUGH_DATA;
  181. }
  182. for (p = 0; p < last_part; ++p) {
  183. const uint32_t psize = sz[0] | (sz[1] << 8) | (sz[2] << 16);
  184. const uint8_t* part_end = part_start + psize;
  185. if (part_end > buf_end) part_end = buf_end;
  186. VP8InitBitReader(dec->parts_ + p, part_start, part_end);
  187. part_start = part_end;
  188. sz += 3;
  189. }
  190. VP8InitBitReader(dec->parts_ + last_part, part_start, buf_end);
  191. return (part_start < buf_end) ? VP8_STATUS_OK :
  192. VP8_STATUS_SUSPENDED; // Init is ok, but there's not enough data
  193. }
  194. // Paragraph 9.4
  195. static int ParseFilterHeader(VP8BitReader* br, VP8Decoder* const dec) {
  196. VP8FilterHeader* const hdr = &dec->filter_hdr_;
  197. hdr->simple_ = VP8Get(br);
  198. hdr->level_ = VP8GetValue(br, 6);
  199. hdr->sharpness_ = VP8GetValue(br, 3);
  200. hdr->use_lf_delta_ = VP8Get(br);
  201. if (hdr->use_lf_delta_) {
  202. if (VP8Get(br)) { // update lf-delta?
  203. int i;
  204. for (i = 0; i < NUM_REF_LF_DELTAS; ++i) {
  205. if (VP8Get(br)) {
  206. hdr->ref_lf_delta_[i] = VP8GetSignedValue(br, 6);
  207. }
  208. }
  209. for (i = 0; i < NUM_MODE_LF_DELTAS; ++i) {
  210. if (VP8Get(br)) {
  211. hdr->mode_lf_delta_[i] = VP8GetSignedValue(br, 6);
  212. }
  213. }
  214. }
  215. }
  216. dec->filter_type_ = (hdr->level_ == 0) ? 0 : hdr->simple_ ? 1 : 2;
  217. return !br->eof_;
  218. }
  219. // Topmost call
  220. int VP8GetHeaders(VP8Decoder* const dec, VP8Io* const io) {
  221. const uint8_t* buf;
  222. size_t buf_size;
  223. VP8FrameHeader* frm_hdr;
  224. VP8PictureHeader* pic_hdr;
  225. VP8BitReader* br;
  226. VP8StatusCode status;
  227. if (dec == NULL) {
  228. return 0;
  229. }
  230. SetOk(dec);
  231. if (io == NULL) {
  232. return VP8SetError(dec, VP8_STATUS_INVALID_PARAM,
  233. "null VP8Io passed to VP8GetHeaders()");
  234. }
  235. buf = io->data;
  236. buf_size = io->data_size;
  237. if (buf_size < 4) {
  238. return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA,
  239. "Truncated header.");
  240. }
  241. // Paragraph 9.1
  242. {
  243. const uint32_t bits = buf[0] | (buf[1] << 8) | (buf[2] << 16);
  244. frm_hdr = &dec->frm_hdr_;
  245. frm_hdr->key_frame_ = !(bits & 1);
  246. frm_hdr->profile_ = (bits >> 1) & 7;
  247. frm_hdr->show_ = (bits >> 4) & 1;
  248. frm_hdr->partition_length_ = (bits >> 5);
  249. if (frm_hdr->profile_ > 3)
  250. return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR,
  251. "Incorrect keyframe parameters.");
  252. if (!frm_hdr->show_)
  253. return VP8SetError(dec, VP8_STATUS_UNSUPPORTED_FEATURE,
  254. "Frame not displayable.");
  255. buf += 3;
  256. buf_size -= 3;
  257. }
  258. pic_hdr = &dec->pic_hdr_;
  259. if (frm_hdr->key_frame_) {
  260. // Paragraph 9.2
  261. if (buf_size < 7) {
  262. return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA,
  263. "cannot parse picture header");
  264. }
  265. if (!VP8CheckSignature(buf, buf_size)) {
  266. return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR,
  267. "Bad code word");
  268. }
  269. pic_hdr->width_ = ((buf[4] << 8) | buf[3]) & 0x3fff;
  270. pic_hdr->xscale_ = buf[4] >> 6; // ratio: 1, 5/4 5/3 or 2
  271. pic_hdr->height_ = ((buf[6] << 8) | buf[5]) & 0x3fff;
  272. pic_hdr->yscale_ = buf[6] >> 6;
  273. buf += 7;
  274. buf_size -= 7;
  275. dec->mb_w_ = (pic_hdr->width_ + 15) >> 4;
  276. dec->mb_h_ = (pic_hdr->height_ + 15) >> 4;
  277. // Setup default output area (can be later modified during io->setup())
  278. io->width = pic_hdr->width_;
  279. io->height = pic_hdr->height_;
  280. io->use_scaling = 0;
  281. io->use_cropping = 0;
  282. io->crop_top = 0;
  283. io->crop_left = 0;
  284. io->crop_right = io->width;
  285. io->crop_bottom = io->height;
  286. io->mb_w = io->width; // sanity check
  287. io->mb_h = io->height; // ditto
  288. VP8ResetProba(&dec->proba_);
  289. ResetSegmentHeader(&dec->segment_hdr_);
  290. }
  291. // Check if we have all the partition #0 available, and initialize dec->br_
  292. // to read this partition (and this partition only).
  293. if (frm_hdr->partition_length_ > buf_size) {
  294. return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA,
  295. "bad partition length");
  296. }
  297. br = &dec->br_;
  298. VP8InitBitReader(br, buf, buf + frm_hdr->partition_length_);
  299. buf += frm_hdr->partition_length_;
  300. buf_size -= frm_hdr->partition_length_;
  301. if (frm_hdr->key_frame_) {
  302. pic_hdr->colorspace_ = VP8Get(br);
  303. pic_hdr->clamp_type_ = VP8Get(br);
  304. }
  305. if (!ParseSegmentHeader(br, &dec->segment_hdr_, &dec->proba_)) {
  306. return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR,
  307. "cannot parse segment header");
  308. }
  309. // Filter specs
  310. if (!ParseFilterHeader(br, dec)) {
  311. return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR,
  312. "cannot parse filter header");
  313. }
  314. status = ParsePartitions(dec, buf, buf_size);
  315. if (status != VP8_STATUS_OK) {
  316. return VP8SetError(dec, status, "cannot parse partitions");
  317. }
  318. // quantizer change
  319. VP8ParseQuant(dec);
  320. // Frame buffer marking
  321. if (!frm_hdr->key_frame_) {
  322. return VP8SetError(dec, VP8_STATUS_UNSUPPORTED_FEATURE,
  323. "Not a key frame.");
  324. }
  325. VP8Get(br); // ignore the value of update_proba_
  326. VP8ParseProba(br, dec);
  327. // sanitized state
  328. dec->ready_ = 1;
  329. return 1;
  330. }
  331. //------------------------------------------------------------------------------
  332. // Residual decoding (Paragraph 13.2 / 13.3)
  333. static const int kBands[16 + 1] = {
  334. 0, 1, 2, 3, 6, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7,
  335. 0 // extra entry as sentinel
  336. };
  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. // n is either 0 or 1 here. kBands[n] is not necessary for extracting '*p'.
  381. const uint8_t* p = prob[n].probas_[ctx];
  382. for (; n < 16; ++n) {
  383. if (!VP8GetBit(br, p[0])) {
  384. return n; // previous coeff was last non-zero coeff
  385. }
  386. while (!VP8GetBit(br, p[1])) { // sequence of zero coeffs
  387. p = prob[kBands[++n]].probas_[0];
  388. if (n == 16) return 16;
  389. }
  390. { // non zero coeff
  391. const VP8ProbaArray* const p_ctx = &prob[kBands[n + 1]].probas_[0];
  392. int v;
  393. if (!VP8GetBit(br, p[2])) {
  394. v = 1;
  395. p = p_ctx[1];
  396. } else {
  397. v = GetLargeValue(br, p);
  398. p = p_ctx[2];
  399. }
  400. out[kZigzag[n]] = VP8GetSigned(br, v) * dq[n > 0];
  401. }
  402. }
  403. return 16;
  404. }
  405. static WEBP_INLINE uint32_t NzCodeBits(uint32_t nz_coeffs, int nz, int dc_nz) {
  406. nz_coeffs <<= 2;
  407. nz_coeffs |= (nz > 3) ? 3 : (nz > 1) ? 2 : dc_nz;
  408. return nz_coeffs;
  409. }
  410. static int ParseResiduals(VP8Decoder* const dec,
  411. VP8MB* const mb, VP8BitReader* const token_br) {
  412. VP8BandProbas (* const bands)[NUM_BANDS] = dec->proba_.bands_;
  413. const VP8BandProbas* ac_proba;
  414. VP8MBData* const block = dec->mb_data_ + dec->mb_x_;
  415. const VP8QuantMatrix* const q = &dec->dqm_[block->segment_];
  416. int16_t* dst = block->coeffs_;
  417. VP8MB* const left_mb = dec->mb_info_ - 1;
  418. uint8_t tnz, lnz;
  419. uint32_t non_zero_y = 0;
  420. uint32_t non_zero_uv = 0;
  421. int x, y, ch;
  422. uint32_t out_t_nz, out_l_nz;
  423. int first;
  424. memset(dst, 0, 384 * sizeof(*dst));
  425. if (!block->is_i4x4_) { // parse DC
  426. int16_t dc[16] = { 0 };
  427. const int ctx = mb->nz_dc_ + left_mb->nz_dc_;
  428. const int nz = GetCoeffs(token_br, bands[1], ctx, q->y2_mat_, 0, dc);
  429. mb->nz_dc_ = left_mb->nz_dc_ = (nz > 0);
  430. if (nz > 1) { // more than just the DC -> perform the full transform
  431. VP8TransformWHT(dc, dst);
  432. } else { // only DC is non-zero -> inlined simplified transform
  433. int i;
  434. const int dc0 = (dc[0] + 3) >> 3;
  435. for (i = 0; i < 16 * 16; i += 16) dst[i] = dc0;
  436. }
  437. first = 1;
  438. ac_proba = bands[0];
  439. } else {
  440. first = 0;
  441. ac_proba = bands[3];
  442. }
  443. tnz = mb->nz_ & 0x0f;
  444. lnz = left_mb->nz_ & 0x0f;
  445. for (y = 0; y < 4; ++y) {
  446. int l = lnz & 1;
  447. uint32_t nz_coeffs = 0;
  448. for (x = 0; x < 4; ++x) {
  449. const int ctx = l + (tnz & 1);
  450. const int nz = GetCoeffs(token_br, ac_proba, ctx, q->y1_mat_, first, dst);
  451. l = (nz > first);
  452. tnz = (tnz >> 1) | (l << 7);
  453. nz_coeffs = NzCodeBits(nz_coeffs, nz, dst[0] != 0);
  454. dst += 16;
  455. }
  456. tnz >>= 4;
  457. lnz = (lnz >> 1) | (l << 7);
  458. non_zero_y = (non_zero_y << 8) | nz_coeffs;
  459. }
  460. out_t_nz = tnz;
  461. out_l_nz = lnz >> 4;
  462. for (ch = 0; ch < 4; ch += 2) {
  463. uint32_t nz_coeffs = 0;
  464. tnz = mb->nz_ >> (4 + ch);
  465. lnz = left_mb->nz_ >> (4 + ch);
  466. for (y = 0; y < 2; ++y) {
  467. int l = lnz & 1;
  468. for (x = 0; x < 2; ++x) {
  469. const int ctx = l + (tnz & 1);
  470. const int nz = GetCoeffs(token_br, bands[2], ctx, q->uv_mat_, 0, dst);
  471. l = (nz > 0);
  472. tnz = (tnz >> 1) | (l << 3);
  473. nz_coeffs = NzCodeBits(nz_coeffs, nz, dst[0] != 0);
  474. dst += 16;
  475. }
  476. tnz >>= 2;
  477. lnz = (lnz >> 1) | (l << 5);
  478. }
  479. // Note: we don't really need the per-4x4 details for U/V blocks.
  480. non_zero_uv |= nz_coeffs << (4 * ch);
  481. out_t_nz |= (tnz << 4) << ch;
  482. out_l_nz |= (lnz & 0xf0) << ch;
  483. }
  484. mb->nz_ = out_t_nz;
  485. left_mb->nz_ = out_l_nz;
  486. block->non_zero_y_ = non_zero_y;
  487. block->non_zero_uv_ = non_zero_uv;
  488. // We look at the mode-code of each block and check if some blocks have less
  489. // than three non-zero coeffs (code < 2). This is to avoid dithering flat and
  490. // empty blocks.
  491. block->dither_ = (non_zero_uv & 0xaaaa) ? 0 : q->dither_;
  492. return !(non_zero_y | non_zero_uv); // will be used for further optimization
  493. }
  494. //------------------------------------------------------------------------------
  495. // Main loop
  496. int VP8DecodeMB(VP8Decoder* const dec, VP8BitReader* const token_br) {
  497. VP8MB* const left = dec->mb_info_ - 1;
  498. VP8MB* const mb = dec->mb_info_ + dec->mb_x_;
  499. VP8MBData* const block = dec->mb_data_ + dec->mb_x_;
  500. int skip = dec->use_skip_proba_ ? block->skip_ : 0;
  501. if (!skip) {
  502. skip = ParseResiduals(dec, mb, token_br);
  503. } else {
  504. left->nz_ = mb->nz_ = 0;
  505. if (!block->is_i4x4_) {
  506. left->nz_dc_ = mb->nz_dc_ = 0;
  507. }
  508. block->non_zero_y_ = 0;
  509. block->non_zero_uv_ = 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_ - 1)];
  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. ALPHDelete(dec->alph_dec_);
  590. dec->alph_dec_ = NULL;
  591. WebPSafeFree(dec->mem_);
  592. dec->mem_ = NULL;
  593. dec->mem_size_ = 0;
  594. memset(&dec->br_, 0, sizeof(dec->br_));
  595. dec->ready_ = 0;
  596. }
  597. //------------------------------------------------------------------------------