vp8_dec.c 22 KB

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