vp8l_dec.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746
  1. // Copyright 2012 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. // Authors: Vikas Arora (vikaas.arora@gmail.com)
  13. // Jyrki Alakuijala (jyrki@google.com)
  14. #include <stdlib.h>
  15. #include "src/dec/alphai_dec.h"
  16. #include "src/dec/vp8li_dec.h"
  17. #include "src/dsp/dsp.h"
  18. #include "src/dsp/lossless.h"
  19. #include "src/dsp/lossless_common.h"
  20. #include "src/dsp/yuv.h"
  21. #include "src/utils/endian_inl_utils.h"
  22. #include "src/utils/huffman_utils.h"
  23. #include "src/utils/utils.h"
  24. #define NUM_ARGB_CACHE_ROWS 16
  25. static const int kCodeLengthLiterals = 16;
  26. static const int kCodeLengthRepeatCode = 16;
  27. static const uint8_t kCodeLengthExtraBits[3] = { 2, 3, 7 };
  28. static const uint8_t kCodeLengthRepeatOffsets[3] = { 3, 3, 11 };
  29. // -----------------------------------------------------------------------------
  30. // Five Huffman codes are used at each meta code:
  31. // 1. green + length prefix codes + color cache codes,
  32. // 2. alpha,
  33. // 3. red,
  34. // 4. blue, and,
  35. // 5. distance prefix codes.
  36. typedef enum {
  37. GREEN = 0,
  38. RED = 1,
  39. BLUE = 2,
  40. ALPHA = 3,
  41. DIST = 4
  42. } HuffIndex;
  43. static const uint16_t kAlphabetSize[HUFFMAN_CODES_PER_META_CODE] = {
  44. NUM_LITERAL_CODES + NUM_LENGTH_CODES,
  45. NUM_LITERAL_CODES, NUM_LITERAL_CODES, NUM_LITERAL_CODES,
  46. NUM_DISTANCE_CODES
  47. };
  48. static const uint8_t kLiteralMap[HUFFMAN_CODES_PER_META_CODE] = {
  49. 0, 1, 1, 1, 0
  50. };
  51. #define NUM_CODE_LENGTH_CODES 19
  52. static const uint8_t kCodeLengthCodeOrder[NUM_CODE_LENGTH_CODES] = {
  53. 17, 18, 0, 1, 2, 3, 4, 5, 16, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
  54. };
  55. #define CODE_TO_PLANE_CODES 120
  56. static const uint8_t kCodeToPlane[CODE_TO_PLANE_CODES] = {
  57. 0x18, 0x07, 0x17, 0x19, 0x28, 0x06, 0x27, 0x29, 0x16, 0x1a,
  58. 0x26, 0x2a, 0x38, 0x05, 0x37, 0x39, 0x15, 0x1b, 0x36, 0x3a,
  59. 0x25, 0x2b, 0x48, 0x04, 0x47, 0x49, 0x14, 0x1c, 0x35, 0x3b,
  60. 0x46, 0x4a, 0x24, 0x2c, 0x58, 0x45, 0x4b, 0x34, 0x3c, 0x03,
  61. 0x57, 0x59, 0x13, 0x1d, 0x56, 0x5a, 0x23, 0x2d, 0x44, 0x4c,
  62. 0x55, 0x5b, 0x33, 0x3d, 0x68, 0x02, 0x67, 0x69, 0x12, 0x1e,
  63. 0x66, 0x6a, 0x22, 0x2e, 0x54, 0x5c, 0x43, 0x4d, 0x65, 0x6b,
  64. 0x32, 0x3e, 0x78, 0x01, 0x77, 0x79, 0x53, 0x5d, 0x11, 0x1f,
  65. 0x64, 0x6c, 0x42, 0x4e, 0x76, 0x7a, 0x21, 0x2f, 0x75, 0x7b,
  66. 0x31, 0x3f, 0x63, 0x6d, 0x52, 0x5e, 0x00, 0x74, 0x7c, 0x41,
  67. 0x4f, 0x10, 0x20, 0x62, 0x6e, 0x30, 0x73, 0x7d, 0x51, 0x5f,
  68. 0x40, 0x72, 0x7e, 0x61, 0x6f, 0x50, 0x71, 0x7f, 0x60, 0x70
  69. };
  70. // Memory needed for lookup tables of one Huffman tree group. Red, blue, alpha
  71. // and distance alphabets are constant (256 for red, blue and alpha, 40 for
  72. // distance) and lookup table sizes for them in worst case are 630 and 410
  73. // respectively. Size of green alphabet depends on color cache size and is equal
  74. // to 256 (green component values) + 24 (length prefix values)
  75. // + color_cache_size (between 0 and 2048).
  76. // All values computed for 8-bit first level lookup with Mark Adler's tool:
  77. // https://github.com/madler/zlib/blob/v1.2.5/examples/enough.c
  78. #define FIXED_TABLE_SIZE (630 * 3 + 410)
  79. static const uint16_t kTableSize[12] = {
  80. FIXED_TABLE_SIZE + 654,
  81. FIXED_TABLE_SIZE + 656,
  82. FIXED_TABLE_SIZE + 658,
  83. FIXED_TABLE_SIZE + 662,
  84. FIXED_TABLE_SIZE + 670,
  85. FIXED_TABLE_SIZE + 686,
  86. FIXED_TABLE_SIZE + 718,
  87. FIXED_TABLE_SIZE + 782,
  88. FIXED_TABLE_SIZE + 912,
  89. FIXED_TABLE_SIZE + 1168,
  90. FIXED_TABLE_SIZE + 1680,
  91. FIXED_TABLE_SIZE + 2704
  92. };
  93. static int DecodeImageStream(int xsize, int ysize,
  94. int is_level0,
  95. VP8LDecoder* const dec,
  96. uint32_t** const decoded_data);
  97. //------------------------------------------------------------------------------
  98. int VP8LCheckSignature(const uint8_t* const data, size_t size) {
  99. return (size >= VP8L_FRAME_HEADER_SIZE &&
  100. data[0] == VP8L_MAGIC_BYTE &&
  101. (data[4] >> 5) == 0); // version
  102. }
  103. static int ReadImageInfo(VP8LBitReader* const br,
  104. int* const width, int* const height,
  105. int* const has_alpha) {
  106. if (VP8LReadBits(br, 8) != VP8L_MAGIC_BYTE) return 0;
  107. *width = VP8LReadBits(br, VP8L_IMAGE_SIZE_BITS) + 1;
  108. *height = VP8LReadBits(br, VP8L_IMAGE_SIZE_BITS) + 1;
  109. *has_alpha = VP8LReadBits(br, 1);
  110. if (VP8LReadBits(br, VP8L_VERSION_BITS) != 0) return 0;
  111. return !br->eos_;
  112. }
  113. int VP8LGetInfo(const uint8_t* data, size_t data_size,
  114. int* const width, int* const height, int* const has_alpha) {
  115. if (data == NULL || data_size < VP8L_FRAME_HEADER_SIZE) {
  116. return 0; // not enough data
  117. } else if (!VP8LCheckSignature(data, data_size)) {
  118. return 0; // bad signature
  119. } else {
  120. int w, h, a;
  121. VP8LBitReader br;
  122. VP8LInitBitReader(&br, data, data_size);
  123. if (!ReadImageInfo(&br, &w, &h, &a)) {
  124. return 0;
  125. }
  126. if (width != NULL) *width = w;
  127. if (height != NULL) *height = h;
  128. if (has_alpha != NULL) *has_alpha = a;
  129. return 1;
  130. }
  131. }
  132. //------------------------------------------------------------------------------
  133. static WEBP_INLINE int GetCopyDistance(int distance_symbol,
  134. VP8LBitReader* const br) {
  135. int extra_bits, offset;
  136. if (distance_symbol < 4) {
  137. return distance_symbol + 1;
  138. }
  139. extra_bits = (distance_symbol - 2) >> 1;
  140. offset = (2 + (distance_symbol & 1)) << extra_bits;
  141. return offset + VP8LReadBits(br, extra_bits) + 1;
  142. }
  143. static WEBP_INLINE int GetCopyLength(int length_symbol,
  144. VP8LBitReader* const br) {
  145. // Length and distance prefixes are encoded the same way.
  146. return GetCopyDistance(length_symbol, br);
  147. }
  148. static WEBP_INLINE int PlaneCodeToDistance(int xsize, int plane_code) {
  149. if (plane_code > CODE_TO_PLANE_CODES) {
  150. return plane_code - CODE_TO_PLANE_CODES;
  151. } else {
  152. const int dist_code = kCodeToPlane[plane_code - 1];
  153. const int yoffset = dist_code >> 4;
  154. const int xoffset = 8 - (dist_code & 0xf);
  155. const int dist = yoffset * xsize + xoffset;
  156. return (dist >= 1) ? dist : 1; // dist<1 can happen if xsize is very small
  157. }
  158. }
  159. //------------------------------------------------------------------------------
  160. // Decodes the next Huffman code from bit-stream.
  161. // VP8LFillBitWindow(br) needs to be called at minimum every second call
  162. // to ReadSymbol, in order to pre-fetch enough bits.
  163. static WEBP_INLINE int ReadSymbol(const HuffmanCode* table,
  164. VP8LBitReader* const br) {
  165. int nbits;
  166. uint32_t val = VP8LPrefetchBits(br);
  167. table += val & HUFFMAN_TABLE_MASK;
  168. nbits = table->bits - HUFFMAN_TABLE_BITS;
  169. if (nbits > 0) {
  170. VP8LSetBitPos(br, br->bit_pos_ + HUFFMAN_TABLE_BITS);
  171. val = VP8LPrefetchBits(br);
  172. table += table->value;
  173. table += val & ((1 << nbits) - 1);
  174. }
  175. VP8LSetBitPos(br, br->bit_pos_ + table->bits);
  176. return table->value;
  177. }
  178. // Reads packed symbol depending on GREEN channel
  179. #define BITS_SPECIAL_MARKER 0x100 // something large enough (and a bit-mask)
  180. #define PACKED_NON_LITERAL_CODE 0 // must be < NUM_LITERAL_CODES
  181. static WEBP_INLINE int ReadPackedSymbols(const HTreeGroup* group,
  182. VP8LBitReader* const br,
  183. uint32_t* const dst) {
  184. const uint32_t val = VP8LPrefetchBits(br) & (HUFFMAN_PACKED_TABLE_SIZE - 1);
  185. const HuffmanCode32 code = group->packed_table[val];
  186. assert(group->use_packed_table);
  187. if (code.bits < BITS_SPECIAL_MARKER) {
  188. VP8LSetBitPos(br, br->bit_pos_ + code.bits);
  189. *dst = code.value;
  190. return PACKED_NON_LITERAL_CODE;
  191. } else {
  192. VP8LSetBitPos(br, br->bit_pos_ + code.bits - BITS_SPECIAL_MARKER);
  193. assert(code.value >= NUM_LITERAL_CODES);
  194. return code.value;
  195. }
  196. }
  197. static int AccumulateHCode(HuffmanCode hcode, int shift,
  198. HuffmanCode32* const huff) {
  199. huff->bits += hcode.bits;
  200. huff->value |= (uint32_t)hcode.value << shift;
  201. assert(huff->bits <= HUFFMAN_TABLE_BITS);
  202. return hcode.bits;
  203. }
  204. static void BuildPackedTable(HTreeGroup* const htree_group) {
  205. uint32_t code;
  206. for (code = 0; code < HUFFMAN_PACKED_TABLE_SIZE; ++code) {
  207. uint32_t bits = code;
  208. HuffmanCode32* const huff = &htree_group->packed_table[bits];
  209. HuffmanCode hcode = htree_group->htrees[GREEN][bits];
  210. if (hcode.value >= NUM_LITERAL_CODES) {
  211. huff->bits = hcode.bits + BITS_SPECIAL_MARKER;
  212. huff->value = hcode.value;
  213. } else {
  214. huff->bits = 0;
  215. huff->value = 0;
  216. bits >>= AccumulateHCode(hcode, 8, huff);
  217. bits >>= AccumulateHCode(htree_group->htrees[RED][bits], 16, huff);
  218. bits >>= AccumulateHCode(htree_group->htrees[BLUE][bits], 0, huff);
  219. bits >>= AccumulateHCode(htree_group->htrees[ALPHA][bits], 24, huff);
  220. (void)bits;
  221. }
  222. }
  223. }
  224. static int ReadHuffmanCodeLengths(
  225. VP8LDecoder* const dec, const int* const code_length_code_lengths,
  226. int num_symbols, int* const code_lengths) {
  227. int ok = 0;
  228. VP8LBitReader* const br = &dec->br_;
  229. int symbol;
  230. int max_symbol;
  231. int prev_code_len = DEFAULT_CODE_LENGTH;
  232. HuffmanTables tables;
  233. if (!VP8LHuffmanTablesAllocate(1 << LENGTHS_TABLE_BITS, &tables) ||
  234. !VP8LBuildHuffmanTable(&tables, LENGTHS_TABLE_BITS,
  235. code_length_code_lengths, NUM_CODE_LENGTH_CODES)) {
  236. goto End;
  237. }
  238. if (VP8LReadBits(br, 1)) { // use length
  239. const int length_nbits = 2 + 2 * VP8LReadBits(br, 3);
  240. max_symbol = 2 + VP8LReadBits(br, length_nbits);
  241. if (max_symbol > num_symbols) {
  242. goto End;
  243. }
  244. } else {
  245. max_symbol = num_symbols;
  246. }
  247. symbol = 0;
  248. while (symbol < num_symbols) {
  249. const HuffmanCode* p;
  250. int code_len;
  251. if (max_symbol-- == 0) break;
  252. VP8LFillBitWindow(br);
  253. p = &tables.curr_segment->start[VP8LPrefetchBits(br) & LENGTHS_TABLE_MASK];
  254. VP8LSetBitPos(br, br->bit_pos_ + p->bits);
  255. code_len = p->value;
  256. if (code_len < kCodeLengthLiterals) {
  257. code_lengths[symbol++] = code_len;
  258. if (code_len != 0) prev_code_len = code_len;
  259. } else {
  260. const int use_prev = (code_len == kCodeLengthRepeatCode);
  261. const int slot = code_len - kCodeLengthLiterals;
  262. const int extra_bits = kCodeLengthExtraBits[slot];
  263. const int repeat_offset = kCodeLengthRepeatOffsets[slot];
  264. int repeat = VP8LReadBits(br, extra_bits) + repeat_offset;
  265. if (symbol + repeat > num_symbols) {
  266. goto End;
  267. } else {
  268. const int length = use_prev ? prev_code_len : 0;
  269. while (repeat-- > 0) code_lengths[symbol++] = length;
  270. }
  271. }
  272. }
  273. ok = 1;
  274. End:
  275. VP8LHuffmanTablesDeallocate(&tables);
  276. if (!ok) dec->status_ = VP8_STATUS_BITSTREAM_ERROR;
  277. return ok;
  278. }
  279. // 'code_lengths' is pre-allocated temporary buffer, used for creating Huffman
  280. // tree.
  281. static int ReadHuffmanCode(int alphabet_size, VP8LDecoder* const dec,
  282. int* const code_lengths,
  283. HuffmanTables* const table) {
  284. int ok = 0;
  285. int size = 0;
  286. VP8LBitReader* const br = &dec->br_;
  287. const int simple_code = VP8LReadBits(br, 1);
  288. memset(code_lengths, 0, alphabet_size * sizeof(*code_lengths));
  289. if (simple_code) { // Read symbols, codes & code lengths directly.
  290. const int num_symbols = VP8LReadBits(br, 1) + 1;
  291. const int first_symbol_len_code = VP8LReadBits(br, 1);
  292. // The first code is either 1 bit or 8 bit code.
  293. int symbol = VP8LReadBits(br, (first_symbol_len_code == 0) ? 1 : 8);
  294. code_lengths[symbol] = 1;
  295. // The second code (if present), is always 8 bits long.
  296. if (num_symbols == 2) {
  297. symbol = VP8LReadBits(br, 8);
  298. code_lengths[symbol] = 1;
  299. }
  300. ok = 1;
  301. } else { // Decode Huffman-coded code lengths.
  302. int i;
  303. int code_length_code_lengths[NUM_CODE_LENGTH_CODES] = { 0 };
  304. const int num_codes = VP8LReadBits(br, 4) + 4;
  305. if (num_codes > NUM_CODE_LENGTH_CODES) {
  306. dec->status_ = VP8_STATUS_BITSTREAM_ERROR;
  307. return 0;
  308. }
  309. for (i = 0; i < num_codes; ++i) {
  310. code_length_code_lengths[kCodeLengthCodeOrder[i]] = VP8LReadBits(br, 3);
  311. }
  312. ok = ReadHuffmanCodeLengths(dec, code_length_code_lengths, alphabet_size,
  313. code_lengths);
  314. }
  315. ok = ok && !br->eos_;
  316. if (ok) {
  317. size = VP8LBuildHuffmanTable(table, HUFFMAN_TABLE_BITS,
  318. code_lengths, alphabet_size);
  319. }
  320. if (!ok || size == 0) {
  321. dec->status_ = VP8_STATUS_BITSTREAM_ERROR;
  322. return 0;
  323. }
  324. return size;
  325. }
  326. static int ReadHuffmanCodes(VP8LDecoder* const dec, int xsize, int ysize,
  327. int color_cache_bits, int allow_recursion) {
  328. int i, j;
  329. VP8LBitReader* const br = &dec->br_;
  330. VP8LMetadata* const hdr = &dec->hdr_;
  331. uint32_t* huffman_image = NULL;
  332. HTreeGroup* htree_groups = NULL;
  333. HuffmanTables* huffman_tables = &hdr->huffman_tables_;
  334. int num_htree_groups = 1;
  335. int num_htree_groups_max = 1;
  336. int max_alphabet_size = 0;
  337. int* code_lengths = NULL;
  338. const int table_size = kTableSize[color_cache_bits];
  339. int* mapping = NULL;
  340. int ok = 0;
  341. // Check the table has been 0 initialized (through InitMetadata).
  342. assert(huffman_tables->root.start == NULL);
  343. assert(huffman_tables->curr_segment == NULL);
  344. if (allow_recursion && VP8LReadBits(br, 1)) {
  345. // use meta Huffman codes.
  346. const int huffman_precision = VP8LReadBits(br, 3) + 2;
  347. const int huffman_xsize = VP8LSubSampleSize(xsize, huffman_precision);
  348. const int huffman_ysize = VP8LSubSampleSize(ysize, huffman_precision);
  349. const int huffman_pixs = huffman_xsize * huffman_ysize;
  350. if (!DecodeImageStream(huffman_xsize, huffman_ysize, 0, dec,
  351. &huffman_image)) {
  352. goto Error;
  353. }
  354. hdr->huffman_subsample_bits_ = huffman_precision;
  355. for (i = 0; i < huffman_pixs; ++i) {
  356. // The huffman data is stored in red and green bytes.
  357. const int group = (huffman_image[i] >> 8) & 0xffff;
  358. huffman_image[i] = group;
  359. if (group >= num_htree_groups_max) {
  360. num_htree_groups_max = group + 1;
  361. }
  362. }
  363. // Check the validity of num_htree_groups_max. If it seems too big, use a
  364. // smaller value for later. This will prevent big memory allocations to end
  365. // up with a bad bitstream anyway.
  366. // The value of 1000 is totally arbitrary. We know that num_htree_groups_max
  367. // is smaller than (1 << 16) and should be smaller than the number of pixels
  368. // (though the format allows it to be bigger).
  369. if (num_htree_groups_max > 1000 || num_htree_groups_max > xsize * ysize) {
  370. // Create a mapping from the used indices to the minimal set of used
  371. // values [0, num_htree_groups)
  372. mapping = (int*)WebPSafeMalloc(num_htree_groups_max, sizeof(*mapping));
  373. if (mapping == NULL) {
  374. dec->status_ = VP8_STATUS_OUT_OF_MEMORY;
  375. goto Error;
  376. }
  377. // -1 means a value is unmapped, and therefore unused in the Huffman
  378. // image.
  379. memset(mapping, 0xff, num_htree_groups_max * sizeof(*mapping));
  380. for (num_htree_groups = 0, i = 0; i < huffman_pixs; ++i) {
  381. // Get the current mapping for the group and remap the Huffman image.
  382. int* const mapped_group = &mapping[huffman_image[i]];
  383. if (*mapped_group == -1) *mapped_group = num_htree_groups++;
  384. huffman_image[i] = *mapped_group;
  385. }
  386. } else {
  387. num_htree_groups = num_htree_groups_max;
  388. }
  389. }
  390. if (br->eos_) goto Error;
  391. // Find maximum alphabet size for the htree group.
  392. for (j = 0; j < HUFFMAN_CODES_PER_META_CODE; ++j) {
  393. int alphabet_size = kAlphabetSize[j];
  394. if (j == 0 && color_cache_bits > 0) {
  395. alphabet_size += 1 << color_cache_bits;
  396. }
  397. if (max_alphabet_size < alphabet_size) {
  398. max_alphabet_size = alphabet_size;
  399. }
  400. }
  401. code_lengths = (int*)WebPSafeCalloc((uint64_t)max_alphabet_size,
  402. sizeof(*code_lengths));
  403. htree_groups = VP8LHtreeGroupsNew(num_htree_groups);
  404. if (htree_groups == NULL || code_lengths == NULL ||
  405. !VP8LHuffmanTablesAllocate(num_htree_groups * table_size,
  406. huffman_tables)) {
  407. dec->status_ = VP8_STATUS_OUT_OF_MEMORY;
  408. goto Error;
  409. }
  410. for (i = 0; i < num_htree_groups_max; ++i) {
  411. // If the index "i" is unused in the Huffman image, just make sure the
  412. // coefficients are valid but do not store them.
  413. if (mapping != NULL && mapping[i] == -1) {
  414. for (j = 0; j < HUFFMAN_CODES_PER_META_CODE; ++j) {
  415. int alphabet_size = kAlphabetSize[j];
  416. if (j == 0 && color_cache_bits > 0) {
  417. alphabet_size += (1 << color_cache_bits);
  418. }
  419. // Passing in NULL so that nothing gets filled.
  420. if (!ReadHuffmanCode(alphabet_size, dec, code_lengths, NULL)) {
  421. goto Error;
  422. }
  423. }
  424. } else {
  425. HTreeGroup* const htree_group =
  426. &htree_groups[(mapping == NULL) ? i : mapping[i]];
  427. HuffmanCode** const htrees = htree_group->htrees;
  428. int size;
  429. int total_size = 0;
  430. int is_trivial_literal = 1;
  431. int max_bits = 0;
  432. for (j = 0; j < HUFFMAN_CODES_PER_META_CODE; ++j) {
  433. int alphabet_size = kAlphabetSize[j];
  434. if (j == 0 && color_cache_bits > 0) {
  435. alphabet_size += (1 << color_cache_bits);
  436. }
  437. size =
  438. ReadHuffmanCode(alphabet_size, dec, code_lengths, huffman_tables);
  439. htrees[j] = huffman_tables->curr_segment->curr_table;
  440. if (size == 0) {
  441. goto Error;
  442. }
  443. if (is_trivial_literal && kLiteralMap[j] == 1) {
  444. is_trivial_literal = (htrees[j]->bits == 0);
  445. }
  446. total_size += htrees[j]->bits;
  447. huffman_tables->curr_segment->curr_table += size;
  448. if (j <= ALPHA) {
  449. int local_max_bits = code_lengths[0];
  450. int k;
  451. for (k = 1; k < alphabet_size; ++k) {
  452. if (code_lengths[k] > local_max_bits) {
  453. local_max_bits = code_lengths[k];
  454. }
  455. }
  456. max_bits += local_max_bits;
  457. }
  458. }
  459. htree_group->is_trivial_literal = is_trivial_literal;
  460. htree_group->is_trivial_code = 0;
  461. if (is_trivial_literal) {
  462. const int red = htrees[RED][0].value;
  463. const int blue = htrees[BLUE][0].value;
  464. const int alpha = htrees[ALPHA][0].value;
  465. htree_group->literal_arb = ((uint32_t)alpha << 24) | (red << 16) | blue;
  466. if (total_size == 0 && htrees[GREEN][0].value < NUM_LITERAL_CODES) {
  467. htree_group->is_trivial_code = 1;
  468. htree_group->literal_arb |= htrees[GREEN][0].value << 8;
  469. }
  470. }
  471. htree_group->use_packed_table =
  472. !htree_group->is_trivial_code && (max_bits < HUFFMAN_PACKED_BITS);
  473. if (htree_group->use_packed_table) BuildPackedTable(htree_group);
  474. }
  475. }
  476. ok = 1;
  477. // All OK. Finalize pointers.
  478. hdr->huffman_image_ = huffman_image;
  479. hdr->num_htree_groups_ = num_htree_groups;
  480. hdr->htree_groups_ = htree_groups;
  481. Error:
  482. WebPSafeFree(code_lengths);
  483. WebPSafeFree(mapping);
  484. if (!ok) {
  485. WebPSafeFree(huffman_image);
  486. VP8LHuffmanTablesDeallocate(huffman_tables);
  487. VP8LHtreeGroupsFree(htree_groups);
  488. }
  489. return ok;
  490. }
  491. //------------------------------------------------------------------------------
  492. // Scaling.
  493. #if !defined(WEBP_REDUCE_SIZE)
  494. static int AllocateAndInitRescaler(VP8LDecoder* const dec, VP8Io* const io) {
  495. const int num_channels = 4;
  496. const int in_width = io->mb_w;
  497. const int out_width = io->scaled_width;
  498. const int in_height = io->mb_h;
  499. const int out_height = io->scaled_height;
  500. const uint64_t work_size = 2 * num_channels * (uint64_t)out_width;
  501. rescaler_t* work; // Rescaler work area.
  502. const uint64_t scaled_data_size = (uint64_t)out_width;
  503. uint32_t* scaled_data; // Temporary storage for scaled BGRA data.
  504. const uint64_t memory_size = sizeof(*dec->rescaler) +
  505. work_size * sizeof(*work) +
  506. scaled_data_size * sizeof(*scaled_data);
  507. uint8_t* memory = (uint8_t*)WebPSafeMalloc(memory_size, sizeof(*memory));
  508. if (memory == NULL) {
  509. dec->status_ = VP8_STATUS_OUT_OF_MEMORY;
  510. return 0;
  511. }
  512. assert(dec->rescaler_memory == NULL);
  513. dec->rescaler_memory = memory;
  514. dec->rescaler = (WebPRescaler*)memory;
  515. memory += sizeof(*dec->rescaler);
  516. work = (rescaler_t*)memory;
  517. memory += work_size * sizeof(*work);
  518. scaled_data = (uint32_t*)memory;
  519. if (!WebPRescalerInit(dec->rescaler, in_width, in_height,
  520. (uint8_t*)scaled_data, out_width, out_height,
  521. 0, num_channels, work)) {
  522. return 0;
  523. }
  524. return 1;
  525. }
  526. #endif // WEBP_REDUCE_SIZE
  527. //------------------------------------------------------------------------------
  528. // Export to ARGB
  529. #if !defined(WEBP_REDUCE_SIZE)
  530. // We have special "export" function since we need to convert from BGRA
  531. static int Export(WebPRescaler* const rescaler, WEBP_CSP_MODE colorspace,
  532. int rgba_stride, uint8_t* const rgba) {
  533. uint32_t* const src = (uint32_t*)rescaler->dst;
  534. uint8_t* dst = rgba;
  535. const int dst_width = rescaler->dst_width;
  536. int num_lines_out = 0;
  537. while (WebPRescalerHasPendingOutput(rescaler)) {
  538. WebPRescalerExportRow(rescaler);
  539. WebPMultARGBRow(src, dst_width, 1);
  540. VP8LConvertFromBGRA(src, dst_width, colorspace, dst);
  541. dst += rgba_stride;
  542. ++num_lines_out;
  543. }
  544. return num_lines_out;
  545. }
  546. // Emit scaled rows.
  547. static int EmitRescaledRowsRGBA(const VP8LDecoder* const dec,
  548. uint8_t* in, int in_stride, int mb_h,
  549. uint8_t* const out, int out_stride) {
  550. const WEBP_CSP_MODE colorspace = dec->output_->colorspace;
  551. int num_lines_in = 0;
  552. int num_lines_out = 0;
  553. while (num_lines_in < mb_h) {
  554. uint8_t* const row_in = in + (uint64_t)num_lines_in * in_stride;
  555. uint8_t* const row_out = out + (uint64_t)num_lines_out * out_stride;
  556. const int lines_left = mb_h - num_lines_in;
  557. const int needed_lines = WebPRescaleNeededLines(dec->rescaler, lines_left);
  558. int lines_imported;
  559. assert(needed_lines > 0 && needed_lines <= lines_left);
  560. WebPMultARGBRows(row_in, in_stride,
  561. dec->rescaler->src_width, needed_lines, 0);
  562. lines_imported =
  563. WebPRescalerImport(dec->rescaler, lines_left, row_in, in_stride);
  564. assert(lines_imported == needed_lines);
  565. num_lines_in += lines_imported;
  566. num_lines_out += Export(dec->rescaler, colorspace, out_stride, row_out);
  567. }
  568. return num_lines_out;
  569. }
  570. #endif // WEBP_REDUCE_SIZE
  571. // Emit rows without any scaling.
  572. static int EmitRows(WEBP_CSP_MODE colorspace,
  573. const uint8_t* row_in, int in_stride,
  574. int mb_w, int mb_h,
  575. uint8_t* const out, int out_stride) {
  576. int lines = mb_h;
  577. uint8_t* row_out = out;
  578. while (lines-- > 0) {
  579. VP8LConvertFromBGRA((const uint32_t*)row_in, mb_w, colorspace, row_out);
  580. row_in += in_stride;
  581. row_out += out_stride;
  582. }
  583. return mb_h; // Num rows out == num rows in.
  584. }
  585. //------------------------------------------------------------------------------
  586. // Export to YUVA
  587. static void ConvertToYUVA(const uint32_t* const src, int width, int y_pos,
  588. const WebPDecBuffer* const output) {
  589. const WebPYUVABuffer* const buf = &output->u.YUVA;
  590. // first, the luma plane
  591. WebPConvertARGBToY(src, buf->y + y_pos * buf->y_stride, width);
  592. // then U/V planes
  593. {
  594. uint8_t* const u = buf->u + (y_pos >> 1) * buf->u_stride;
  595. uint8_t* const v = buf->v + (y_pos >> 1) * buf->v_stride;
  596. // even lines: store values
  597. // odd lines: average with previous values
  598. WebPConvertARGBToUV(src, u, v, width, !(y_pos & 1));
  599. }
  600. // Lastly, store alpha if needed.
  601. if (buf->a != NULL) {
  602. uint8_t* const a = buf->a + y_pos * buf->a_stride;
  603. #if defined(WORDS_BIGENDIAN)
  604. WebPExtractAlpha((uint8_t*)src + 0, 0, width, 1, a, 0);
  605. #else
  606. WebPExtractAlpha((uint8_t*)src + 3, 0, width, 1, a, 0);
  607. #endif
  608. }
  609. }
  610. static int ExportYUVA(const VP8LDecoder* const dec, int y_pos) {
  611. WebPRescaler* const rescaler = dec->rescaler;
  612. uint32_t* const src = (uint32_t*)rescaler->dst;
  613. const int dst_width = rescaler->dst_width;
  614. int num_lines_out = 0;
  615. while (WebPRescalerHasPendingOutput(rescaler)) {
  616. WebPRescalerExportRow(rescaler);
  617. WebPMultARGBRow(src, dst_width, 1);
  618. ConvertToYUVA(src, dst_width, y_pos, dec->output_);
  619. ++y_pos;
  620. ++num_lines_out;
  621. }
  622. return num_lines_out;
  623. }
  624. static int EmitRescaledRowsYUVA(const VP8LDecoder* const dec,
  625. uint8_t* in, int in_stride, int mb_h) {
  626. int num_lines_in = 0;
  627. int y_pos = dec->last_out_row_;
  628. while (num_lines_in < mb_h) {
  629. const int lines_left = mb_h - num_lines_in;
  630. const int needed_lines = WebPRescaleNeededLines(dec->rescaler, lines_left);
  631. int lines_imported;
  632. WebPMultARGBRows(in, in_stride, dec->rescaler->src_width, needed_lines, 0);
  633. lines_imported =
  634. WebPRescalerImport(dec->rescaler, lines_left, in, in_stride);
  635. assert(lines_imported == needed_lines);
  636. num_lines_in += lines_imported;
  637. in += needed_lines * in_stride;
  638. y_pos += ExportYUVA(dec, y_pos);
  639. }
  640. return y_pos;
  641. }
  642. static int EmitRowsYUVA(const VP8LDecoder* const dec,
  643. const uint8_t* in, int in_stride,
  644. int mb_w, int num_rows) {
  645. int y_pos = dec->last_out_row_;
  646. while (num_rows-- > 0) {
  647. ConvertToYUVA((const uint32_t*)in, mb_w, y_pos, dec->output_);
  648. in += in_stride;
  649. ++y_pos;
  650. }
  651. return y_pos;
  652. }
  653. //------------------------------------------------------------------------------
  654. // Cropping.
  655. // Sets io->mb_y, io->mb_h & io->mb_w according to start row, end row and
  656. // crop options. Also updates the input data pointer, so that it points to the
  657. // start of the cropped window. Note that pixels are in ARGB format even if
  658. // 'in_data' is uint8_t*.
  659. // Returns true if the crop window is not empty.
  660. static int SetCropWindow(VP8Io* const io, int y_start, int y_end,
  661. uint8_t** const in_data, int pixel_stride) {
  662. assert(y_start < y_end);
  663. assert(io->crop_left < io->crop_right);
  664. if (y_end > io->crop_bottom) {
  665. y_end = io->crop_bottom; // make sure we don't overflow on last row.
  666. }
  667. if (y_start < io->crop_top) {
  668. const int delta = io->crop_top - y_start;
  669. y_start = io->crop_top;
  670. *in_data += delta * pixel_stride;
  671. }
  672. if (y_start >= y_end) return 0; // Crop window is empty.
  673. *in_data += io->crop_left * sizeof(uint32_t);
  674. io->mb_y = y_start - io->crop_top;
  675. io->mb_w = io->crop_right - io->crop_left;
  676. io->mb_h = y_end - y_start;
  677. return 1; // Non-empty crop window.
  678. }
  679. //------------------------------------------------------------------------------
  680. static WEBP_INLINE int GetMetaIndex(
  681. const uint32_t* const image, int xsize, int bits, int x, int y) {
  682. if (bits == 0) return 0;
  683. return image[xsize * (y >> bits) + (x >> bits)];
  684. }
  685. static WEBP_INLINE HTreeGroup* GetHtreeGroupForPos(VP8LMetadata* const hdr,
  686. int x, int y) {
  687. const int meta_index = GetMetaIndex(hdr->huffman_image_, hdr->huffman_xsize_,
  688. hdr->huffman_subsample_bits_, x, y);
  689. assert(meta_index < hdr->num_htree_groups_);
  690. return hdr->htree_groups_ + meta_index;
  691. }
  692. //------------------------------------------------------------------------------
  693. // Main loop, with custom row-processing function
  694. typedef void (*ProcessRowsFunc)(VP8LDecoder* const dec, int row);
  695. static void ApplyInverseTransforms(VP8LDecoder* const dec,
  696. int start_row, int num_rows,
  697. const uint32_t* const rows) {
  698. int n = dec->next_transform_;
  699. const int cache_pixs = dec->width_ * num_rows;
  700. const int end_row = start_row + num_rows;
  701. const uint32_t* rows_in = rows;
  702. uint32_t* const rows_out = dec->argb_cache_;
  703. // Inverse transforms.
  704. while (n-- > 0) {
  705. VP8LTransform* const transform = &dec->transforms_[n];
  706. VP8LInverseTransform(transform, start_row, end_row, rows_in, rows_out);
  707. rows_in = rows_out;
  708. }
  709. if (rows_in != rows_out) {
  710. // No transform called, hence just copy.
  711. memcpy(rows_out, rows_in, cache_pixs * sizeof(*rows_out));
  712. }
  713. }
  714. // Processes (transforms, scales & color-converts) the rows decoded after the
  715. // last call.
  716. static void ProcessRows(VP8LDecoder* const dec, int row) {
  717. const uint32_t* const rows = dec->pixels_ + dec->width_ * dec->last_row_;
  718. const int num_rows = row - dec->last_row_;
  719. assert(row <= dec->io_->crop_bottom);
  720. // We can't process more than NUM_ARGB_CACHE_ROWS at a time (that's the size
  721. // of argb_cache_), but we currently don't need more than that.
  722. assert(num_rows <= NUM_ARGB_CACHE_ROWS);
  723. if (num_rows > 0) { // Emit output.
  724. VP8Io* const io = dec->io_;
  725. uint8_t* rows_data = (uint8_t*)dec->argb_cache_;
  726. const int in_stride = io->width * sizeof(uint32_t); // in unit of RGBA
  727. ApplyInverseTransforms(dec, dec->last_row_, num_rows, rows);
  728. if (!SetCropWindow(io, dec->last_row_, row, &rows_data, in_stride)) {
  729. // Nothing to output (this time).
  730. } else {
  731. const WebPDecBuffer* const output = dec->output_;
  732. if (WebPIsRGBMode(output->colorspace)) { // convert to RGBA
  733. const WebPRGBABuffer* const buf = &output->u.RGBA;
  734. uint8_t* const rgba =
  735. buf->rgba + (int64_t)dec->last_out_row_ * buf->stride;
  736. const int num_rows_out =
  737. #if !defined(WEBP_REDUCE_SIZE)
  738. io->use_scaling ?
  739. EmitRescaledRowsRGBA(dec, rows_data, in_stride, io->mb_h,
  740. rgba, buf->stride) :
  741. #endif // WEBP_REDUCE_SIZE
  742. EmitRows(output->colorspace, rows_data, in_stride,
  743. io->mb_w, io->mb_h, rgba, buf->stride);
  744. // Update 'last_out_row_'.
  745. dec->last_out_row_ += num_rows_out;
  746. } else { // convert to YUVA
  747. dec->last_out_row_ = io->use_scaling ?
  748. EmitRescaledRowsYUVA(dec, rows_data, in_stride, io->mb_h) :
  749. EmitRowsYUVA(dec, rows_data, in_stride, io->mb_w, io->mb_h);
  750. }
  751. assert(dec->last_out_row_ <= output->height);
  752. }
  753. }
  754. // Update 'last_row_'.
  755. dec->last_row_ = row;
  756. assert(dec->last_row_ <= dec->height_);
  757. }
  758. // Row-processing for the special case when alpha data contains only one
  759. // transform (color indexing), and trivial non-green literals.
  760. static int Is8bOptimizable(const VP8LMetadata* const hdr) {
  761. int i;
  762. if (hdr->color_cache_size_ > 0) return 0;
  763. // When the Huffman tree contains only one symbol, we can skip the
  764. // call to ReadSymbol() for red/blue/alpha channels.
  765. for (i = 0; i < hdr->num_htree_groups_; ++i) {
  766. HuffmanCode** const htrees = hdr->htree_groups_[i].htrees;
  767. if (htrees[RED][0].bits > 0) return 0;
  768. if (htrees[BLUE][0].bits > 0) return 0;
  769. if (htrees[ALPHA][0].bits > 0) return 0;
  770. }
  771. return 1;
  772. }
  773. static void AlphaApplyFilter(ALPHDecoder* const alph_dec,
  774. int first_row, int last_row,
  775. uint8_t* out, int stride) {
  776. if (alph_dec->filter_ != WEBP_FILTER_NONE) {
  777. int y;
  778. const uint8_t* prev_line = alph_dec->prev_line_;
  779. assert(WebPUnfilters[alph_dec->filter_] != NULL);
  780. for (y = first_row; y < last_row; ++y) {
  781. WebPUnfilters[alph_dec->filter_](prev_line, out, out, stride);
  782. prev_line = out;
  783. out += stride;
  784. }
  785. alph_dec->prev_line_ = prev_line;
  786. }
  787. }
  788. static void ExtractPalettedAlphaRows(VP8LDecoder* const dec, int last_row) {
  789. // For vertical and gradient filtering, we need to decode the part above the
  790. // crop_top row, in order to have the correct spatial predictors.
  791. ALPHDecoder* const alph_dec = (ALPHDecoder*)dec->io_->opaque;
  792. const int top_row =
  793. (alph_dec->filter_ == WEBP_FILTER_NONE ||
  794. alph_dec->filter_ == WEBP_FILTER_HORIZONTAL) ? dec->io_->crop_top
  795. : dec->last_row_;
  796. const int first_row = (dec->last_row_ < top_row) ? top_row : dec->last_row_;
  797. assert(last_row <= dec->io_->crop_bottom);
  798. if (last_row > first_row) {
  799. // Special method for paletted alpha data. We only process the cropped area.
  800. const int width = dec->io_->width;
  801. uint8_t* out = alph_dec->output_ + width * first_row;
  802. const uint8_t* const in =
  803. (uint8_t*)dec->pixels_ + dec->width_ * first_row;
  804. VP8LTransform* const transform = &dec->transforms_[0];
  805. assert(dec->next_transform_ == 1);
  806. assert(transform->type_ == COLOR_INDEXING_TRANSFORM);
  807. VP8LColorIndexInverseTransformAlpha(transform, first_row, last_row,
  808. in, out);
  809. AlphaApplyFilter(alph_dec, first_row, last_row, out, width);
  810. }
  811. dec->last_row_ = dec->last_out_row_ = last_row;
  812. }
  813. //------------------------------------------------------------------------------
  814. // Helper functions for fast pattern copy (8b and 32b)
  815. // cyclic rotation of pattern word
  816. static WEBP_INLINE uint32_t Rotate8b(uint32_t V) {
  817. #if defined(WORDS_BIGENDIAN)
  818. return ((V & 0xff000000u) >> 24) | (V << 8);
  819. #else
  820. return ((V & 0xffu) << 24) | (V >> 8);
  821. #endif
  822. }
  823. // copy 1, 2 or 4-bytes pattern
  824. static WEBP_INLINE void CopySmallPattern8b(const uint8_t* src, uint8_t* dst,
  825. int length, uint32_t pattern) {
  826. int i;
  827. // align 'dst' to 4-bytes boundary. Adjust the pattern along the way.
  828. while ((uintptr_t)dst & 3) {
  829. *dst++ = *src++;
  830. pattern = Rotate8b(pattern);
  831. --length;
  832. }
  833. // Copy the pattern 4 bytes at a time.
  834. for (i = 0; i < (length >> 2); ++i) {
  835. ((uint32_t*)dst)[i] = pattern;
  836. }
  837. // Finish with left-overs. 'pattern' is still correctly positioned,
  838. // so no Rotate8b() call is needed.
  839. for (i <<= 2; i < length; ++i) {
  840. dst[i] = src[i];
  841. }
  842. }
  843. static WEBP_INLINE void CopyBlock8b(uint8_t* const dst, int dist, int length) {
  844. const uint8_t* src = dst - dist;
  845. if (length >= 8) {
  846. uint32_t pattern = 0;
  847. switch (dist) {
  848. case 1:
  849. pattern = src[0];
  850. #if defined(__arm__) || defined(_M_ARM) // arm doesn't like multiply that much
  851. pattern |= pattern << 8;
  852. pattern |= pattern << 16;
  853. #elif defined(WEBP_USE_MIPS_DSP_R2)
  854. __asm__ volatile ("replv.qb %0, %0" : "+r"(pattern));
  855. #else
  856. pattern = 0x01010101u * pattern;
  857. #endif
  858. break;
  859. case 2:
  860. #if !defined(WORDS_BIGENDIAN)
  861. memcpy(&pattern, src, sizeof(uint16_t));
  862. #else
  863. pattern = ((uint32_t)src[0] << 8) | src[1];
  864. #endif
  865. #if defined(__arm__) || defined(_M_ARM)
  866. pattern |= pattern << 16;
  867. #elif defined(WEBP_USE_MIPS_DSP_R2)
  868. __asm__ volatile ("replv.ph %0, %0" : "+r"(pattern));
  869. #else
  870. pattern = 0x00010001u * pattern;
  871. #endif
  872. break;
  873. case 4:
  874. memcpy(&pattern, src, sizeof(uint32_t));
  875. break;
  876. default:
  877. goto Copy;
  878. }
  879. CopySmallPattern8b(src, dst, length, pattern);
  880. return;
  881. }
  882. Copy:
  883. if (dist >= length) { // no overlap -> use memcpy()
  884. memcpy(dst, src, length * sizeof(*dst));
  885. } else {
  886. int i;
  887. for (i = 0; i < length; ++i) dst[i] = src[i];
  888. }
  889. }
  890. // copy pattern of 1 or 2 uint32_t's
  891. static WEBP_INLINE void CopySmallPattern32b(const uint32_t* src,
  892. uint32_t* dst,
  893. int length, uint64_t pattern) {
  894. int i;
  895. if ((uintptr_t)dst & 4) { // Align 'dst' to 8-bytes boundary.
  896. *dst++ = *src++;
  897. pattern = (pattern >> 32) | (pattern << 32);
  898. --length;
  899. }
  900. assert(0 == ((uintptr_t)dst & 7));
  901. for (i = 0; i < (length >> 1); ++i) {
  902. ((uint64_t*)dst)[i] = pattern; // Copy the pattern 8 bytes at a time.
  903. }
  904. if (length & 1) { // Finish with left-over.
  905. dst[i << 1] = src[i << 1];
  906. }
  907. }
  908. static WEBP_INLINE void CopyBlock32b(uint32_t* const dst,
  909. int dist, int length) {
  910. const uint32_t* const src = dst - dist;
  911. if (dist <= 2 && length >= 4 && ((uintptr_t)dst & 3) == 0) {
  912. uint64_t pattern;
  913. if (dist == 1) {
  914. pattern = (uint64_t)src[0];
  915. pattern |= pattern << 32;
  916. } else {
  917. memcpy(&pattern, src, sizeof(pattern));
  918. }
  919. CopySmallPattern32b(src, dst, length, pattern);
  920. } else if (dist >= length) { // no overlap
  921. memcpy(dst, src, length * sizeof(*dst));
  922. } else {
  923. int i;
  924. for (i = 0; i < length; ++i) dst[i] = src[i];
  925. }
  926. }
  927. //------------------------------------------------------------------------------
  928. static int DecodeAlphaData(VP8LDecoder* const dec, uint8_t* const data,
  929. int width, int height, int last_row) {
  930. int ok = 1;
  931. int row = dec->last_pixel_ / width;
  932. int col = dec->last_pixel_ % width;
  933. VP8LBitReader* const br = &dec->br_;
  934. VP8LMetadata* const hdr = &dec->hdr_;
  935. int pos = dec->last_pixel_; // current position
  936. const int end = width * height; // End of data
  937. const int last = width * last_row; // Last pixel to decode
  938. const int len_code_limit = NUM_LITERAL_CODES + NUM_LENGTH_CODES;
  939. const int mask = hdr->huffman_mask_;
  940. const HTreeGroup* htree_group =
  941. (pos < last) ? GetHtreeGroupForPos(hdr, col, row) : NULL;
  942. assert(pos <= end);
  943. assert(last_row <= height);
  944. assert(Is8bOptimizable(hdr));
  945. while (!br->eos_ && pos < last) {
  946. int code;
  947. // Only update when changing tile.
  948. if ((col & mask) == 0) {
  949. htree_group = GetHtreeGroupForPos(hdr, col, row);
  950. }
  951. assert(htree_group != NULL);
  952. VP8LFillBitWindow(br);
  953. code = ReadSymbol(htree_group->htrees[GREEN], br);
  954. if (code < NUM_LITERAL_CODES) { // Literal
  955. data[pos] = code;
  956. ++pos;
  957. ++col;
  958. if (col >= width) {
  959. col = 0;
  960. ++row;
  961. if (row <= last_row && (row % NUM_ARGB_CACHE_ROWS == 0)) {
  962. ExtractPalettedAlphaRows(dec, row);
  963. }
  964. }
  965. } else if (code < len_code_limit) { // Backward reference
  966. int dist_code, dist;
  967. const int length_sym = code - NUM_LITERAL_CODES;
  968. const int length = GetCopyLength(length_sym, br);
  969. const int dist_symbol = ReadSymbol(htree_group->htrees[DIST], br);
  970. VP8LFillBitWindow(br);
  971. dist_code = GetCopyDistance(dist_symbol, br);
  972. dist = PlaneCodeToDistance(width, dist_code);
  973. if (pos >= dist && end - pos >= length) {
  974. CopyBlock8b(data + pos, dist, length);
  975. } else {
  976. ok = 0;
  977. goto End;
  978. }
  979. pos += length;
  980. col += length;
  981. while (col >= width) {
  982. col -= width;
  983. ++row;
  984. if (row <= last_row && (row % NUM_ARGB_CACHE_ROWS == 0)) {
  985. ExtractPalettedAlphaRows(dec, row);
  986. }
  987. }
  988. if (pos < last && (col & mask)) {
  989. htree_group = GetHtreeGroupForPos(hdr, col, row);
  990. }
  991. } else { // Not reached
  992. ok = 0;
  993. goto End;
  994. }
  995. br->eos_ = VP8LIsEndOfStream(br);
  996. }
  997. // Process the remaining rows corresponding to last row-block.
  998. ExtractPalettedAlphaRows(dec, row > last_row ? last_row : row);
  999. End:
  1000. br->eos_ = VP8LIsEndOfStream(br);
  1001. if (!ok || (br->eos_ && pos < end)) {
  1002. ok = 0;
  1003. dec->status_ = br->eos_ ? VP8_STATUS_SUSPENDED
  1004. : VP8_STATUS_BITSTREAM_ERROR;
  1005. } else {
  1006. dec->last_pixel_ = pos;
  1007. }
  1008. return ok;
  1009. }
  1010. static void SaveState(VP8LDecoder* const dec, int last_pixel) {
  1011. assert(dec->incremental_);
  1012. dec->saved_br_ = dec->br_;
  1013. dec->saved_last_pixel_ = last_pixel;
  1014. if (dec->hdr_.color_cache_size_ > 0) {
  1015. VP8LColorCacheCopy(&dec->hdr_.color_cache_, &dec->hdr_.saved_color_cache_);
  1016. }
  1017. }
  1018. static void RestoreState(VP8LDecoder* const dec) {
  1019. assert(dec->br_.eos_);
  1020. dec->status_ = VP8_STATUS_SUSPENDED;
  1021. dec->br_ = dec->saved_br_;
  1022. dec->last_pixel_ = dec->saved_last_pixel_;
  1023. if (dec->hdr_.color_cache_size_ > 0) {
  1024. VP8LColorCacheCopy(&dec->hdr_.saved_color_cache_, &dec->hdr_.color_cache_);
  1025. }
  1026. }
  1027. #define SYNC_EVERY_N_ROWS 8 // minimum number of rows between check-points
  1028. static int DecodeImageData(VP8LDecoder* const dec, uint32_t* const data,
  1029. int width, int height, int last_row,
  1030. ProcessRowsFunc process_func) {
  1031. int row = dec->last_pixel_ / width;
  1032. int col = dec->last_pixel_ % width;
  1033. VP8LBitReader* const br = &dec->br_;
  1034. VP8LMetadata* const hdr = &dec->hdr_;
  1035. uint32_t* src = data + dec->last_pixel_;
  1036. uint32_t* last_cached = src;
  1037. uint32_t* const src_end = data + width * height; // End of data
  1038. uint32_t* const src_last = data + width * last_row; // Last pixel to decode
  1039. const int len_code_limit = NUM_LITERAL_CODES + NUM_LENGTH_CODES;
  1040. const int color_cache_limit = len_code_limit + hdr->color_cache_size_;
  1041. int next_sync_row = dec->incremental_ ? row : 1 << 24;
  1042. VP8LColorCache* const color_cache =
  1043. (hdr->color_cache_size_ > 0) ? &hdr->color_cache_ : NULL;
  1044. const int mask = hdr->huffman_mask_;
  1045. const HTreeGroup* htree_group =
  1046. (src < src_last) ? GetHtreeGroupForPos(hdr, col, row) : NULL;
  1047. assert(dec->last_row_ < last_row);
  1048. assert(src_last <= src_end);
  1049. while (src < src_last) {
  1050. int code;
  1051. if (row >= next_sync_row) {
  1052. SaveState(dec, (int)(src - data));
  1053. next_sync_row = row + SYNC_EVERY_N_ROWS;
  1054. }
  1055. // Only update when changing tile. Note we could use this test:
  1056. // if "((((prev_col ^ col) | prev_row ^ row)) > mask)" -> tile changed
  1057. // but that's actually slower and needs storing the previous col/row.
  1058. if ((col & mask) == 0) {
  1059. htree_group = GetHtreeGroupForPos(hdr, col, row);
  1060. }
  1061. assert(htree_group != NULL);
  1062. if (htree_group->is_trivial_code) {
  1063. *src = htree_group->literal_arb;
  1064. goto AdvanceByOne;
  1065. }
  1066. VP8LFillBitWindow(br);
  1067. if (htree_group->use_packed_table) {
  1068. code = ReadPackedSymbols(htree_group, br, src);
  1069. if (VP8LIsEndOfStream(br)) break;
  1070. if (code == PACKED_NON_LITERAL_CODE) goto AdvanceByOne;
  1071. } else {
  1072. code = ReadSymbol(htree_group->htrees[GREEN], br);
  1073. }
  1074. if (VP8LIsEndOfStream(br)) break;
  1075. if (code < NUM_LITERAL_CODES) { // Literal
  1076. if (htree_group->is_trivial_literal) {
  1077. *src = htree_group->literal_arb | (code << 8);
  1078. } else {
  1079. int red, blue, alpha;
  1080. red = ReadSymbol(htree_group->htrees[RED], br);
  1081. VP8LFillBitWindow(br);
  1082. blue = ReadSymbol(htree_group->htrees[BLUE], br);
  1083. alpha = ReadSymbol(htree_group->htrees[ALPHA], br);
  1084. if (VP8LIsEndOfStream(br)) break;
  1085. *src = ((uint32_t)alpha << 24) | (red << 16) | (code << 8) | blue;
  1086. }
  1087. AdvanceByOne:
  1088. ++src;
  1089. ++col;
  1090. if (col >= width) {
  1091. col = 0;
  1092. ++row;
  1093. if (process_func != NULL) {
  1094. if (row <= last_row && (row % NUM_ARGB_CACHE_ROWS == 0)) {
  1095. process_func(dec, row);
  1096. }
  1097. }
  1098. if (color_cache != NULL) {
  1099. while (last_cached < src) {
  1100. VP8LColorCacheInsert(color_cache, *last_cached++);
  1101. }
  1102. }
  1103. }
  1104. } else if (code < len_code_limit) { // Backward reference
  1105. int dist_code, dist;
  1106. const int length_sym = code - NUM_LITERAL_CODES;
  1107. const int length = GetCopyLength(length_sym, br);
  1108. const int dist_symbol = ReadSymbol(htree_group->htrees[DIST], br);
  1109. VP8LFillBitWindow(br);
  1110. dist_code = GetCopyDistance(dist_symbol, br);
  1111. dist = PlaneCodeToDistance(width, dist_code);
  1112. if (VP8LIsEndOfStream(br)) break;
  1113. if (src - data < (ptrdiff_t)dist || src_end - src < (ptrdiff_t)length) {
  1114. goto Error;
  1115. } else {
  1116. CopyBlock32b(src, dist, length);
  1117. }
  1118. src += length;
  1119. col += length;
  1120. while (col >= width) {
  1121. col -= width;
  1122. ++row;
  1123. if (process_func != NULL) {
  1124. if (row <= last_row && (row % NUM_ARGB_CACHE_ROWS == 0)) {
  1125. process_func(dec, row);
  1126. }
  1127. }
  1128. }
  1129. // Because of the check done above (before 'src' was incremented by
  1130. // 'length'), the following holds true.
  1131. assert(src <= src_end);
  1132. if (col & mask) htree_group = GetHtreeGroupForPos(hdr, col, row);
  1133. if (color_cache != NULL) {
  1134. while (last_cached < src) {
  1135. VP8LColorCacheInsert(color_cache, *last_cached++);
  1136. }
  1137. }
  1138. } else if (code < color_cache_limit) { // Color cache
  1139. const int key = code - len_code_limit;
  1140. assert(color_cache != NULL);
  1141. while (last_cached < src) {
  1142. VP8LColorCacheInsert(color_cache, *last_cached++);
  1143. }
  1144. *src = VP8LColorCacheLookup(color_cache, key);
  1145. goto AdvanceByOne;
  1146. } else { // Not reached
  1147. goto Error;
  1148. }
  1149. }
  1150. br->eos_ = VP8LIsEndOfStream(br);
  1151. if (dec->incremental_ && br->eos_ && src < src_end) {
  1152. RestoreState(dec);
  1153. } else if (!br->eos_) {
  1154. // Process the remaining rows corresponding to last row-block.
  1155. if (process_func != NULL) {
  1156. process_func(dec, row > last_row ? last_row : row);
  1157. }
  1158. dec->status_ = VP8_STATUS_OK;
  1159. dec->last_pixel_ = (int)(src - data); // end-of-scan marker
  1160. } else {
  1161. // if not incremental, and we are past the end of buffer (eos_=1), then this
  1162. // is a real bitstream error.
  1163. goto Error;
  1164. }
  1165. return 1;
  1166. Error:
  1167. dec->status_ = VP8_STATUS_BITSTREAM_ERROR;
  1168. return 0;
  1169. }
  1170. // -----------------------------------------------------------------------------
  1171. // VP8LTransform
  1172. static void ClearTransform(VP8LTransform* const transform) {
  1173. WebPSafeFree(transform->data_);
  1174. transform->data_ = NULL;
  1175. }
  1176. // For security reason, we need to remap the color map to span
  1177. // the total possible bundled values, and not just the num_colors.
  1178. static int ExpandColorMap(int num_colors, VP8LTransform* const transform) {
  1179. int i;
  1180. const int final_num_colors = 1 << (8 >> transform->bits_);
  1181. uint32_t* const new_color_map =
  1182. (uint32_t*)WebPSafeMalloc((uint64_t)final_num_colors,
  1183. sizeof(*new_color_map));
  1184. if (new_color_map == NULL) {
  1185. return 0;
  1186. } else {
  1187. uint8_t* const data = (uint8_t*)transform->data_;
  1188. uint8_t* const new_data = (uint8_t*)new_color_map;
  1189. new_color_map[0] = transform->data_[0];
  1190. for (i = 4; i < 4 * num_colors; ++i) {
  1191. // Equivalent to VP8LAddPixels(), on a byte-basis.
  1192. new_data[i] = (data[i] + new_data[i - 4]) & 0xff;
  1193. }
  1194. for (; i < 4 * final_num_colors; ++i) {
  1195. new_data[i] = 0; // black tail.
  1196. }
  1197. WebPSafeFree(transform->data_);
  1198. transform->data_ = new_color_map;
  1199. }
  1200. return 1;
  1201. }
  1202. static int ReadTransform(int* const xsize, int const* ysize,
  1203. VP8LDecoder* const dec) {
  1204. int ok = 1;
  1205. VP8LBitReader* const br = &dec->br_;
  1206. VP8LTransform* transform = &dec->transforms_[dec->next_transform_];
  1207. const VP8LImageTransformType type =
  1208. (VP8LImageTransformType)VP8LReadBits(br, 2);
  1209. // Each transform type can only be present once in the stream.
  1210. if (dec->transforms_seen_ & (1U << type)) {
  1211. return 0; // Already there, let's not accept the second same transform.
  1212. }
  1213. dec->transforms_seen_ |= (1U << type);
  1214. transform->type_ = type;
  1215. transform->xsize_ = *xsize;
  1216. transform->ysize_ = *ysize;
  1217. transform->data_ = NULL;
  1218. ++dec->next_transform_;
  1219. assert(dec->next_transform_ <= NUM_TRANSFORMS);
  1220. switch (type) {
  1221. case PREDICTOR_TRANSFORM:
  1222. case CROSS_COLOR_TRANSFORM:
  1223. transform->bits_ = VP8LReadBits(br, 3) + 2;
  1224. ok = DecodeImageStream(VP8LSubSampleSize(transform->xsize_,
  1225. transform->bits_),
  1226. VP8LSubSampleSize(transform->ysize_,
  1227. transform->bits_),
  1228. 0, dec, &transform->data_);
  1229. break;
  1230. case COLOR_INDEXING_TRANSFORM: {
  1231. const int num_colors = VP8LReadBits(br, 8) + 1;
  1232. const int bits = (num_colors > 16) ? 0
  1233. : (num_colors > 4) ? 1
  1234. : (num_colors > 2) ? 2
  1235. : 3;
  1236. *xsize = VP8LSubSampleSize(transform->xsize_, bits);
  1237. transform->bits_ = bits;
  1238. ok = DecodeImageStream(num_colors, 1, 0, dec, &transform->data_);
  1239. ok = ok && ExpandColorMap(num_colors, transform);
  1240. break;
  1241. }
  1242. case SUBTRACT_GREEN_TRANSFORM:
  1243. break;
  1244. default:
  1245. assert(0); // can't happen
  1246. break;
  1247. }
  1248. return ok;
  1249. }
  1250. // -----------------------------------------------------------------------------
  1251. // VP8LMetadata
  1252. static void InitMetadata(VP8LMetadata* const hdr) {
  1253. assert(hdr != NULL);
  1254. memset(hdr, 0, sizeof(*hdr));
  1255. }
  1256. static void ClearMetadata(VP8LMetadata* const hdr) {
  1257. assert(hdr != NULL);
  1258. WebPSafeFree(hdr->huffman_image_);
  1259. VP8LHuffmanTablesDeallocate(&hdr->huffman_tables_);
  1260. VP8LHtreeGroupsFree(hdr->htree_groups_);
  1261. VP8LColorCacheClear(&hdr->color_cache_);
  1262. VP8LColorCacheClear(&hdr->saved_color_cache_);
  1263. InitMetadata(hdr);
  1264. }
  1265. // -----------------------------------------------------------------------------
  1266. // VP8LDecoder
  1267. VP8LDecoder* VP8LNew(void) {
  1268. VP8LDecoder* const dec = (VP8LDecoder*)WebPSafeCalloc(1ULL, sizeof(*dec));
  1269. if (dec == NULL) return NULL;
  1270. dec->status_ = VP8_STATUS_OK;
  1271. dec->state_ = READ_DIM;
  1272. VP8LDspInit(); // Init critical function pointers.
  1273. return dec;
  1274. }
  1275. void VP8LClear(VP8LDecoder* const dec) {
  1276. int i;
  1277. if (dec == NULL) return;
  1278. ClearMetadata(&dec->hdr_);
  1279. WebPSafeFree(dec->pixels_);
  1280. dec->pixels_ = NULL;
  1281. for (i = 0; i < dec->next_transform_; ++i) {
  1282. ClearTransform(&dec->transforms_[i]);
  1283. }
  1284. dec->next_transform_ = 0;
  1285. dec->transforms_seen_ = 0;
  1286. WebPSafeFree(dec->rescaler_memory);
  1287. dec->rescaler_memory = NULL;
  1288. dec->output_ = NULL; // leave no trace behind
  1289. }
  1290. void VP8LDelete(VP8LDecoder* const dec) {
  1291. if (dec != NULL) {
  1292. VP8LClear(dec);
  1293. WebPSafeFree(dec);
  1294. }
  1295. }
  1296. static void UpdateDecoder(VP8LDecoder* const dec, int width, int height) {
  1297. VP8LMetadata* const hdr = &dec->hdr_;
  1298. const int num_bits = hdr->huffman_subsample_bits_;
  1299. dec->width_ = width;
  1300. dec->height_ = height;
  1301. hdr->huffman_xsize_ = VP8LSubSampleSize(width, num_bits);
  1302. hdr->huffman_mask_ = (num_bits == 0) ? ~0 : (1 << num_bits) - 1;
  1303. }
  1304. static int DecodeImageStream(int xsize, int ysize,
  1305. int is_level0,
  1306. VP8LDecoder* const dec,
  1307. uint32_t** const decoded_data) {
  1308. int ok = 1;
  1309. int transform_xsize = xsize;
  1310. int transform_ysize = ysize;
  1311. VP8LBitReader* const br = &dec->br_;
  1312. VP8LMetadata* const hdr = &dec->hdr_;
  1313. uint32_t* data = NULL;
  1314. int color_cache_bits = 0;
  1315. // Read the transforms (may recurse).
  1316. if (is_level0) {
  1317. while (ok && VP8LReadBits(br, 1)) {
  1318. ok = ReadTransform(&transform_xsize, &transform_ysize, dec);
  1319. }
  1320. }
  1321. // Color cache
  1322. if (ok && VP8LReadBits(br, 1)) {
  1323. color_cache_bits = VP8LReadBits(br, 4);
  1324. ok = (color_cache_bits >= 1 && color_cache_bits <= MAX_CACHE_BITS);
  1325. if (!ok) {
  1326. dec->status_ = VP8_STATUS_BITSTREAM_ERROR;
  1327. goto End;
  1328. }
  1329. }
  1330. // Read the Huffman codes (may recurse).
  1331. ok = ok && ReadHuffmanCodes(dec, transform_xsize, transform_ysize,
  1332. color_cache_bits, is_level0);
  1333. if (!ok) {
  1334. dec->status_ = VP8_STATUS_BITSTREAM_ERROR;
  1335. goto End;
  1336. }
  1337. // Finish setting up the color-cache
  1338. if (color_cache_bits > 0) {
  1339. hdr->color_cache_size_ = 1 << color_cache_bits;
  1340. if (!VP8LColorCacheInit(&hdr->color_cache_, color_cache_bits)) {
  1341. dec->status_ = VP8_STATUS_OUT_OF_MEMORY;
  1342. ok = 0;
  1343. goto End;
  1344. }
  1345. } else {
  1346. hdr->color_cache_size_ = 0;
  1347. }
  1348. UpdateDecoder(dec, transform_xsize, transform_ysize);
  1349. if (is_level0) { // level 0 complete
  1350. dec->state_ = READ_HDR;
  1351. goto End;
  1352. }
  1353. {
  1354. const uint64_t total_size = (uint64_t)transform_xsize * transform_ysize;
  1355. data = (uint32_t*)WebPSafeMalloc(total_size, sizeof(*data));
  1356. if (data == NULL) {
  1357. dec->status_ = VP8_STATUS_OUT_OF_MEMORY;
  1358. ok = 0;
  1359. goto End;
  1360. }
  1361. }
  1362. // Use the Huffman trees to decode the LZ77 encoded data.
  1363. ok = DecodeImageData(dec, data, transform_xsize, transform_ysize,
  1364. transform_ysize, NULL);
  1365. ok = ok && !br->eos_;
  1366. End:
  1367. if (!ok) {
  1368. WebPSafeFree(data);
  1369. ClearMetadata(hdr);
  1370. } else {
  1371. if (decoded_data != NULL) {
  1372. *decoded_data = data;
  1373. } else {
  1374. // We allocate image data in this function only for transforms. At level 0
  1375. // (that is: not the transforms), we shouldn't have allocated anything.
  1376. assert(data == NULL);
  1377. assert(is_level0);
  1378. }
  1379. dec->last_pixel_ = 0; // Reset for future DECODE_DATA_FUNC() calls.
  1380. if (!is_level0) ClearMetadata(hdr); // Clean up temporary data behind.
  1381. }
  1382. return ok;
  1383. }
  1384. //------------------------------------------------------------------------------
  1385. // Allocate internal buffers dec->pixels_ and dec->argb_cache_.
  1386. static int AllocateInternalBuffers32b(VP8LDecoder* const dec, int final_width) {
  1387. const uint64_t num_pixels = (uint64_t)dec->width_ * dec->height_;
  1388. // Scratch buffer corresponding to top-prediction row for transforming the
  1389. // first row in the row-blocks. Not needed for paletted alpha.
  1390. const uint64_t cache_top_pixels = (uint16_t)final_width;
  1391. // Scratch buffer for temporary BGRA storage. Not needed for paletted alpha.
  1392. const uint64_t cache_pixels = (uint64_t)final_width * NUM_ARGB_CACHE_ROWS;
  1393. const uint64_t total_num_pixels =
  1394. num_pixels + cache_top_pixels + cache_pixels;
  1395. assert(dec->width_ <= final_width);
  1396. dec->pixels_ = (uint32_t*)WebPSafeMalloc(total_num_pixels, sizeof(uint32_t));
  1397. if (dec->pixels_ == NULL) {
  1398. dec->argb_cache_ = NULL; // for soundness
  1399. dec->status_ = VP8_STATUS_OUT_OF_MEMORY;
  1400. return 0;
  1401. }
  1402. dec->argb_cache_ = dec->pixels_ + num_pixels + cache_top_pixels;
  1403. return 1;
  1404. }
  1405. static int AllocateInternalBuffers8b(VP8LDecoder* const dec) {
  1406. const uint64_t total_num_pixels = (uint64_t)dec->width_ * dec->height_;
  1407. dec->argb_cache_ = NULL; // for soundness
  1408. dec->pixels_ = (uint32_t*)WebPSafeMalloc(total_num_pixels, sizeof(uint8_t));
  1409. if (dec->pixels_ == NULL) {
  1410. dec->status_ = VP8_STATUS_OUT_OF_MEMORY;
  1411. return 0;
  1412. }
  1413. return 1;
  1414. }
  1415. //------------------------------------------------------------------------------
  1416. // Special row-processing that only stores the alpha data.
  1417. static void ExtractAlphaRows(VP8LDecoder* const dec, int last_row) {
  1418. int cur_row = dec->last_row_;
  1419. int num_rows = last_row - cur_row;
  1420. const uint32_t* in = dec->pixels_ + dec->width_ * cur_row;
  1421. assert(last_row <= dec->io_->crop_bottom);
  1422. while (num_rows > 0) {
  1423. const int num_rows_to_process =
  1424. (num_rows > NUM_ARGB_CACHE_ROWS) ? NUM_ARGB_CACHE_ROWS : num_rows;
  1425. // Extract alpha (which is stored in the green plane).
  1426. ALPHDecoder* const alph_dec = (ALPHDecoder*)dec->io_->opaque;
  1427. uint8_t* const output = alph_dec->output_;
  1428. const int width = dec->io_->width; // the final width (!= dec->width_)
  1429. const int cache_pixs = width * num_rows_to_process;
  1430. uint8_t* const dst = output + width * cur_row;
  1431. const uint32_t* const src = dec->argb_cache_;
  1432. ApplyInverseTransforms(dec, cur_row, num_rows_to_process, in);
  1433. WebPExtractGreen(src, dst, cache_pixs);
  1434. AlphaApplyFilter(alph_dec,
  1435. cur_row, cur_row + num_rows_to_process, dst, width);
  1436. num_rows -= num_rows_to_process;
  1437. in += num_rows_to_process * dec->width_;
  1438. cur_row += num_rows_to_process;
  1439. }
  1440. assert(cur_row == last_row);
  1441. dec->last_row_ = dec->last_out_row_ = last_row;
  1442. }
  1443. int VP8LDecodeAlphaHeader(ALPHDecoder* const alph_dec,
  1444. const uint8_t* const data, size_t data_size) {
  1445. int ok = 0;
  1446. VP8LDecoder* dec = VP8LNew();
  1447. if (dec == NULL) return 0;
  1448. assert(alph_dec != NULL);
  1449. dec->width_ = alph_dec->width_;
  1450. dec->height_ = alph_dec->height_;
  1451. dec->io_ = &alph_dec->io_;
  1452. dec->io_->opaque = alph_dec;
  1453. dec->io_->width = alph_dec->width_;
  1454. dec->io_->height = alph_dec->height_;
  1455. dec->status_ = VP8_STATUS_OK;
  1456. VP8LInitBitReader(&dec->br_, data, data_size);
  1457. if (!DecodeImageStream(alph_dec->width_, alph_dec->height_, 1, dec, NULL)) {
  1458. goto Err;
  1459. }
  1460. // Special case: if alpha data uses only the color indexing transform and
  1461. // doesn't use color cache (a frequent case), we will use DecodeAlphaData()
  1462. // method that only needs allocation of 1 byte per pixel (alpha channel).
  1463. if (dec->next_transform_ == 1 &&
  1464. dec->transforms_[0].type_ == COLOR_INDEXING_TRANSFORM &&
  1465. Is8bOptimizable(&dec->hdr_)) {
  1466. alph_dec->use_8b_decode_ = 1;
  1467. ok = AllocateInternalBuffers8b(dec);
  1468. } else {
  1469. // Allocate internal buffers (note that dec->width_ may have changed here).
  1470. alph_dec->use_8b_decode_ = 0;
  1471. ok = AllocateInternalBuffers32b(dec, alph_dec->width_);
  1472. }
  1473. if (!ok) goto Err;
  1474. // Only set here, once we are sure it is valid (to avoid thread races).
  1475. alph_dec->vp8l_dec_ = dec;
  1476. return 1;
  1477. Err:
  1478. VP8LDelete(dec);
  1479. return 0;
  1480. }
  1481. int VP8LDecodeAlphaImageStream(ALPHDecoder* const alph_dec, int last_row) {
  1482. VP8LDecoder* const dec = alph_dec->vp8l_dec_;
  1483. assert(dec != NULL);
  1484. assert(last_row <= dec->height_);
  1485. if (dec->last_row_ >= last_row) {
  1486. return 1; // done
  1487. }
  1488. if (!alph_dec->use_8b_decode_) WebPInitAlphaProcessing();
  1489. // Decode (with special row processing).
  1490. return alph_dec->use_8b_decode_ ?
  1491. DecodeAlphaData(dec, (uint8_t*)dec->pixels_, dec->width_, dec->height_,
  1492. last_row) :
  1493. DecodeImageData(dec, dec->pixels_, dec->width_, dec->height_,
  1494. last_row, ExtractAlphaRows);
  1495. }
  1496. //------------------------------------------------------------------------------
  1497. int VP8LDecodeHeader(VP8LDecoder* const dec, VP8Io* const io) {
  1498. int width, height, has_alpha;
  1499. if (dec == NULL) return 0;
  1500. if (io == NULL) {
  1501. dec->status_ = VP8_STATUS_INVALID_PARAM;
  1502. return 0;
  1503. }
  1504. dec->io_ = io;
  1505. dec->status_ = VP8_STATUS_OK;
  1506. VP8LInitBitReader(&dec->br_, io->data, io->data_size);
  1507. if (!ReadImageInfo(&dec->br_, &width, &height, &has_alpha)) {
  1508. dec->status_ = VP8_STATUS_BITSTREAM_ERROR;
  1509. goto Error;
  1510. }
  1511. dec->state_ = READ_DIM;
  1512. io->width = width;
  1513. io->height = height;
  1514. if (!DecodeImageStream(width, height, 1, dec, NULL)) goto Error;
  1515. return 1;
  1516. Error:
  1517. VP8LClear(dec);
  1518. assert(dec->status_ != VP8_STATUS_OK);
  1519. return 0;
  1520. }
  1521. int VP8LDecodeImage(VP8LDecoder* const dec) {
  1522. VP8Io* io = NULL;
  1523. WebPDecParams* params = NULL;
  1524. if (dec == NULL) return 0;
  1525. assert(dec->hdr_.huffman_tables_.root.start != NULL);
  1526. assert(dec->hdr_.htree_groups_ != NULL);
  1527. assert(dec->hdr_.num_htree_groups_ > 0);
  1528. io = dec->io_;
  1529. assert(io != NULL);
  1530. params = (WebPDecParams*)io->opaque;
  1531. assert(params != NULL);
  1532. // Initialization.
  1533. if (dec->state_ != READ_DATA) {
  1534. dec->output_ = params->output;
  1535. assert(dec->output_ != NULL);
  1536. if (!WebPIoInitFromOptions(params->options, io, MODE_BGRA)) {
  1537. dec->status_ = VP8_STATUS_INVALID_PARAM;
  1538. goto Err;
  1539. }
  1540. if (!AllocateInternalBuffers32b(dec, io->width)) goto Err;
  1541. #if !defined(WEBP_REDUCE_SIZE)
  1542. if (io->use_scaling && !AllocateAndInitRescaler(dec, io)) goto Err;
  1543. #else
  1544. if (io->use_scaling) {
  1545. dec->status_ = VP8_STATUS_INVALID_PARAM;
  1546. goto Err;
  1547. }
  1548. #endif
  1549. if (io->use_scaling || WebPIsPremultipliedMode(dec->output_->colorspace)) {
  1550. // need the alpha-multiply functions for premultiplied output or rescaling
  1551. WebPInitAlphaProcessing();
  1552. }
  1553. if (!WebPIsRGBMode(dec->output_->colorspace)) {
  1554. WebPInitConvertARGBToYUV();
  1555. if (dec->output_->u.YUVA.a != NULL) WebPInitAlphaProcessing();
  1556. }
  1557. if (dec->incremental_) {
  1558. if (dec->hdr_.color_cache_size_ > 0 &&
  1559. dec->hdr_.saved_color_cache_.colors_ == NULL) {
  1560. if (!VP8LColorCacheInit(&dec->hdr_.saved_color_cache_,
  1561. dec->hdr_.color_cache_.hash_bits_)) {
  1562. dec->status_ = VP8_STATUS_OUT_OF_MEMORY;
  1563. goto Err;
  1564. }
  1565. }
  1566. }
  1567. dec->state_ = READ_DATA;
  1568. }
  1569. // Decode.
  1570. if (!DecodeImageData(dec, dec->pixels_, dec->width_, dec->height_,
  1571. io->crop_bottom, ProcessRows)) {
  1572. goto Err;
  1573. }
  1574. params->last_y = dec->last_out_row_;
  1575. return 1;
  1576. Err:
  1577. VP8LClear(dec);
  1578. assert(dec->status_ != VP8_STATUS_OK);
  1579. return 0;
  1580. }
  1581. //------------------------------------------------------------------------------