2
0

webp.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. // Copyright 2010 Google Inc. All Rights Reserved.
  2. //
  3. // This code is licensed under the same terms as WebM:
  4. // Software License Agreement: http://www.webmproject.org/license/software/
  5. // Additional IP Rights Grant: http://www.webmproject.org/license/additional/
  6. // -----------------------------------------------------------------------------
  7. //
  8. // Main decoding functions for WEBP images.
  9. //
  10. // Author: Skal ([email protected])
  11. #include <stdlib.h>
  12. #include "./vp8i.h"
  13. #include "./vp8li.h"
  14. #include "./webpi.h"
  15. #include "../format_constants.h"
  16. #if defined(__cplusplus) || defined(c_plusplus)
  17. extern "C" {
  18. #endif
  19. //------------------------------------------------------------------------------
  20. // RIFF layout is:
  21. // Offset tag
  22. // 0...3 "RIFF" 4-byte tag
  23. // 4...7 size of image data (including metadata) starting at offset 8
  24. // 8...11 "WEBP" our form-type signature
  25. // The RIFF container (12 bytes) is followed by appropriate chunks:
  26. // 12..15 "VP8 ": 4-bytes tags, signaling the use of VP8 video format
  27. // 16..19 size of the raw VP8 image data, starting at offset 20
  28. // 20.... the VP8 bytes
  29. // Or,
  30. // 12..15 "VP8L": 4-bytes tags, signaling the use of VP8L lossless format
  31. // 16..19 size of the raw VP8L image data, starting at offset 20
  32. // 20.... the VP8L bytes
  33. // Or,
  34. // 12..15 "VP8X": 4-bytes tags, describing the extended-VP8 chunk.
  35. // 16..19 size of the VP8X chunk starting at offset 20.
  36. // 20..23 VP8X flags bit-map corresponding to the chunk-types present.
  37. // 24..26 Width of the Canvas Image.
  38. // 27..29 Height of the Canvas Image.
  39. // There can be extra chunks after the "VP8X" chunk (ICCP, TILE, FRM, VP8,
  40. // META ...)
  41. // All sizes are in little-endian order.
  42. // Note: chunk data size must be padded to multiple of 2 when written.
  43. static WEBP_INLINE uint32_t get_le24(const uint8_t* const data) {
  44. return data[0] | (data[1] << 8) | (data[2] << 16);
  45. }
  46. static WEBP_INLINE uint32_t get_le32(const uint8_t* const data) {
  47. return (uint32_t)get_le24(data) | (data[3] << 24);
  48. }
  49. // Validates the RIFF container (if detected) and skips over it.
  50. // If a RIFF container is detected,
  51. // Returns VP8_STATUS_BITSTREAM_ERROR for invalid header, and
  52. // VP8_STATUS_OK otherwise.
  53. // In case there are not enough bytes (partial RIFF container), return 0 for
  54. // *riff_size. Else return the RIFF size extracted from the header.
  55. static VP8StatusCode ParseRIFF(const uint8_t** const data,
  56. size_t* const data_size,
  57. size_t* const riff_size) {
  58. assert(data != NULL);
  59. assert(data_size != NULL);
  60. assert(riff_size != NULL);
  61. *riff_size = 0; // Default: no RIFF present.
  62. if (*data_size >= RIFF_HEADER_SIZE && !memcmp(*data, "RIFF", TAG_SIZE)) {
  63. if (memcmp(*data + 8, "WEBP", TAG_SIZE)) {
  64. return VP8_STATUS_BITSTREAM_ERROR; // Wrong image file signature.
  65. } else {
  66. const uint32_t size = get_le32(*data + TAG_SIZE);
  67. // Check that we have at least one chunk (i.e "WEBP" + "VP8?nnnn").
  68. if (size < TAG_SIZE + CHUNK_HEADER_SIZE) {
  69. return VP8_STATUS_BITSTREAM_ERROR;
  70. }
  71. // We have a RIFF container. Skip it.
  72. *riff_size = size;
  73. *data += RIFF_HEADER_SIZE;
  74. *data_size -= RIFF_HEADER_SIZE;
  75. }
  76. }
  77. return VP8_STATUS_OK;
  78. }
  79. // Validates the VP8X header and skips over it.
  80. // Returns VP8_STATUS_BITSTREAM_ERROR for invalid VP8X header,
  81. // VP8_STATUS_NOT_ENOUGH_DATA in case of insufficient data, and
  82. // VP8_STATUS_OK otherwise.
  83. // If a VP8X chunk is found, found_vp8x is set to true and *width_ptr,
  84. // *height_ptr and *flags_ptr are set to the corresponding values extracted
  85. // from the VP8X chunk.
  86. static VP8StatusCode ParseVP8X(const uint8_t** const data,
  87. size_t* const data_size,
  88. int* const found_vp8x,
  89. int* const width_ptr, int* const height_ptr,
  90. uint32_t* const flags_ptr) {
  91. const uint32_t vp8x_size = CHUNK_HEADER_SIZE + VP8X_CHUNK_SIZE;
  92. assert(data != NULL);
  93. assert(data_size != NULL);
  94. assert(found_vp8x != NULL);
  95. *found_vp8x = 0;
  96. if (*data_size < CHUNK_HEADER_SIZE) {
  97. return VP8_STATUS_NOT_ENOUGH_DATA; // Insufficient data.
  98. }
  99. if (!memcmp(*data, "VP8X", TAG_SIZE)) {
  100. int width, height;
  101. uint32_t flags;
  102. const uint32_t chunk_size = get_le32(*data + TAG_SIZE);
  103. if (chunk_size != VP8X_CHUNK_SIZE) {
  104. return VP8_STATUS_BITSTREAM_ERROR; // Wrong chunk size.
  105. }
  106. // Verify if enough data is available to validate the VP8X chunk.
  107. if (*data_size < vp8x_size) {
  108. return VP8_STATUS_NOT_ENOUGH_DATA; // Insufficient data.
  109. }
  110. flags = get_le32(*data + 8);
  111. width = 1 + get_le24(*data + 12);
  112. height = 1 + get_le24(*data + 15);
  113. if (width * (uint64_t)height >= MAX_IMAGE_AREA) {
  114. return VP8_STATUS_BITSTREAM_ERROR; // image is too large
  115. }
  116. if (flags_ptr != NULL) *flags_ptr = flags;
  117. if (width_ptr != NULL) *width_ptr = width;
  118. if (height_ptr != NULL) *height_ptr = height;
  119. // Skip over VP8X header bytes.
  120. *data += vp8x_size;
  121. *data_size -= vp8x_size;
  122. *found_vp8x = 1;
  123. }
  124. return VP8_STATUS_OK;
  125. }
  126. // Skips to the next VP8/VP8L chunk header in the data given the size of the
  127. // RIFF chunk 'riff_size'.
  128. // Returns VP8_STATUS_BITSTREAM_ERROR if any invalid chunk size is encountered,
  129. // VP8_STATUS_NOT_ENOUGH_DATA in case of insufficient data, and
  130. // VP8_STATUS_OK otherwise.
  131. // If an alpha chunk is found, *alpha_data and *alpha_size are set
  132. // appropriately.
  133. static VP8StatusCode ParseOptionalChunks(const uint8_t** const data,
  134. size_t* const data_size,
  135. size_t const riff_size,
  136. const uint8_t** const alpha_data,
  137. size_t* const alpha_size) {
  138. const uint8_t* buf;
  139. size_t buf_size;
  140. uint32_t total_size = TAG_SIZE + // "WEBP".
  141. CHUNK_HEADER_SIZE + // "VP8Xnnnn".
  142. VP8X_CHUNK_SIZE; // data.
  143. assert(data != NULL);
  144. assert(data_size != NULL);
  145. buf = *data;
  146. buf_size = *data_size;
  147. assert(alpha_data != NULL);
  148. assert(alpha_size != NULL);
  149. *alpha_data = NULL;
  150. *alpha_size = 0;
  151. while (1) {
  152. uint32_t chunk_size;
  153. uint32_t disk_chunk_size; // chunk_size with padding
  154. *data = buf;
  155. *data_size = buf_size;
  156. if (buf_size < CHUNK_HEADER_SIZE) { // Insufficient data.
  157. return VP8_STATUS_NOT_ENOUGH_DATA;
  158. }
  159. chunk_size = get_le32(buf + TAG_SIZE);
  160. // For odd-sized chunk-payload, there's one byte padding at the end.
  161. disk_chunk_size = (CHUNK_HEADER_SIZE + chunk_size + 1) & ~1;
  162. total_size += disk_chunk_size;
  163. // Check that total bytes skipped so far does not exceed riff_size.
  164. if (riff_size > 0 && (total_size > riff_size)) {
  165. return VP8_STATUS_BITSTREAM_ERROR; // Not a valid chunk size.
  166. }
  167. if (buf_size < disk_chunk_size) { // Insufficient data.
  168. return VP8_STATUS_NOT_ENOUGH_DATA;
  169. }
  170. if (!memcmp(buf, "ALPH", TAG_SIZE)) { // A valid ALPH header.
  171. *alpha_data = buf + CHUNK_HEADER_SIZE;
  172. *alpha_size = chunk_size;
  173. } else if (!memcmp(buf, "VP8 ", TAG_SIZE) ||
  174. !memcmp(buf, "VP8L", TAG_SIZE)) { // A valid VP8/VP8L header.
  175. return VP8_STATUS_OK; // Found.
  176. }
  177. // We have a full and valid chunk; skip it.
  178. buf += disk_chunk_size;
  179. buf_size -= disk_chunk_size;
  180. }
  181. }
  182. // Validates the VP8/VP8L Header ("VP8 nnnn" or "VP8L nnnn") and skips over it.
  183. // Returns VP8_STATUS_BITSTREAM_ERROR for invalid (chunk larger than
  184. // riff_size) VP8/VP8L header,
  185. // VP8_STATUS_NOT_ENOUGH_DATA in case of insufficient data, and
  186. // VP8_STATUS_OK otherwise.
  187. // If a VP8/VP8L chunk is found, *chunk_size is set to the total number of bytes
  188. // extracted from the VP8/VP8L chunk header.
  189. // The flag '*is_lossless' is set to 1 in case of VP8L chunk / raw VP8L data.
  190. static VP8StatusCode ParseVP8Header(const uint8_t** const data_ptr,
  191. size_t* const data_size,
  192. size_t riff_size,
  193. size_t* const chunk_size,
  194. int* const is_lossless) {
  195. const uint8_t* const data = *data_ptr;
  196. const int is_vp8 = !memcmp(data, "VP8 ", TAG_SIZE);
  197. const int is_vp8l = !memcmp(data, "VP8L", TAG_SIZE);
  198. const uint32_t minimal_size =
  199. TAG_SIZE + CHUNK_HEADER_SIZE; // "WEBP" + "VP8 nnnn" OR
  200. // "WEBP" + "VP8Lnnnn"
  201. assert(data != NULL);
  202. assert(data_size != NULL);
  203. assert(chunk_size != NULL);
  204. assert(is_lossless != NULL);
  205. if (*data_size < CHUNK_HEADER_SIZE) {
  206. return VP8_STATUS_NOT_ENOUGH_DATA; // Insufficient data.
  207. }
  208. if (is_vp8 || is_vp8l) {
  209. // Bitstream contains VP8/VP8L header.
  210. const uint32_t size = get_le32(data + TAG_SIZE);
  211. if ((riff_size >= minimal_size) && (size > riff_size - minimal_size)) {
  212. return VP8_STATUS_BITSTREAM_ERROR; // Inconsistent size information.
  213. }
  214. // Skip over CHUNK_HEADER_SIZE bytes from VP8/VP8L Header.
  215. *chunk_size = size;
  216. *data_ptr += CHUNK_HEADER_SIZE;
  217. *data_size -= CHUNK_HEADER_SIZE;
  218. *is_lossless = is_vp8l;
  219. } else {
  220. // Raw VP8/VP8L bitstream (no header).
  221. *is_lossless = VP8LCheckSignature(data, *data_size);
  222. *chunk_size = *data_size;
  223. }
  224. return VP8_STATUS_OK;
  225. }
  226. //------------------------------------------------------------------------------
  227. // Fetch '*width', '*height', '*has_alpha' and fill out 'headers' based on
  228. // 'data'. All the output parameters may be NULL. If 'headers' is NULL only the
  229. // minimal amount will be read to fetch the remaining parameters.
  230. // If 'headers' is non-NULL this function will attempt to locate both alpha
  231. // data (with or without a VP8X chunk) and the bitstream chunk (VP8/VP8L).
  232. // Note: The following chunk sequences (before the raw VP8/VP8L data) are
  233. // considered valid by this function:
  234. // RIFF + VP8(L)
  235. // RIFF + VP8X + (optional chunks) + VP8(L)
  236. // ALPH + VP8 <-- Not a valid WebP format: only allowed for internal purpose.
  237. // VP8(L) <-- Not a valid WebP format: only allowed for internal purpose.
  238. static VP8StatusCode ParseHeadersInternal(const uint8_t* data,
  239. size_t data_size,
  240. int* const width,
  241. int* const height,
  242. int* const has_alpha,
  243. WebPHeaderStructure* const headers) {
  244. int found_riff = 0;
  245. int found_vp8x = 0;
  246. VP8StatusCode status;
  247. WebPHeaderStructure hdrs;
  248. if (data == NULL || data_size < RIFF_HEADER_SIZE) {
  249. return VP8_STATUS_NOT_ENOUGH_DATA;
  250. }
  251. memset(&hdrs, 0, sizeof(hdrs));
  252. hdrs.data = data;
  253. hdrs.data_size = data_size;
  254. // Skip over RIFF header.
  255. status = ParseRIFF(&data, &data_size, &hdrs.riff_size);
  256. if (status != VP8_STATUS_OK) {
  257. return status; // Wrong RIFF header / insufficient data.
  258. }
  259. found_riff = (hdrs.riff_size > 0);
  260. // Skip over VP8X.
  261. {
  262. uint32_t flags = 0;
  263. status = ParseVP8X(&data, &data_size, &found_vp8x, width, height, &flags);
  264. if (status != VP8_STATUS_OK) {
  265. return status; // Wrong VP8X / insufficient data.
  266. }
  267. if (!found_riff && found_vp8x) {
  268. // Note: This restriction may be removed in the future, if it becomes
  269. // necessary to send VP8X chunk to the decoder.
  270. return VP8_STATUS_BITSTREAM_ERROR;
  271. }
  272. if (has_alpha != NULL) *has_alpha = !!(flags & ALPHA_FLAG_BIT);
  273. if (found_vp8x && headers == NULL) {
  274. return VP8_STATUS_OK; // Return features from VP8X header.
  275. }
  276. }
  277. if (data_size < TAG_SIZE) return VP8_STATUS_NOT_ENOUGH_DATA;
  278. // Skip over optional chunks if data started with "RIFF + VP8X" or "ALPH".
  279. if ((found_riff && found_vp8x) ||
  280. (!found_riff && !found_vp8x && !memcmp(data, "ALPH", TAG_SIZE))) {
  281. status = ParseOptionalChunks(&data, &data_size, hdrs.riff_size,
  282. &hdrs.alpha_data, &hdrs.alpha_data_size);
  283. if (status != VP8_STATUS_OK) {
  284. return status; // Found an invalid chunk size / insufficient data.
  285. }
  286. }
  287. // Skip over VP8/VP8L header.
  288. status = ParseVP8Header(&data, &data_size, hdrs.riff_size,
  289. &hdrs.compressed_size, &hdrs.is_lossless);
  290. if (status != VP8_STATUS_OK) {
  291. return status; // Wrong VP8/VP8L chunk-header / insufficient data.
  292. }
  293. if (hdrs.compressed_size > MAX_CHUNK_PAYLOAD) {
  294. return VP8_STATUS_BITSTREAM_ERROR;
  295. }
  296. if (!hdrs.is_lossless) {
  297. if (data_size < VP8_FRAME_HEADER_SIZE) {
  298. return VP8_STATUS_NOT_ENOUGH_DATA;
  299. }
  300. // Validates raw VP8 data.
  301. if (!VP8GetInfo(data, data_size,
  302. (uint32_t)hdrs.compressed_size, width, height)) {
  303. return VP8_STATUS_BITSTREAM_ERROR;
  304. }
  305. } else {
  306. if (data_size < VP8L_FRAME_HEADER_SIZE) {
  307. return VP8_STATUS_NOT_ENOUGH_DATA;
  308. }
  309. // Validates raw VP8L data.
  310. if (!VP8LGetInfo(data, data_size, width, height, has_alpha)) {
  311. return VP8_STATUS_BITSTREAM_ERROR;
  312. }
  313. }
  314. if (has_alpha != NULL) {
  315. // If the data did not contain a VP8X/VP8L chunk the only definitive way
  316. // to set this is by looking for alpha data (from an ALPH chunk).
  317. *has_alpha |= (hdrs.alpha_data != NULL);
  318. }
  319. if (headers != NULL) {
  320. *headers = hdrs;
  321. headers->offset = data - headers->data;
  322. assert((uint64_t)(data - headers->data) < MAX_CHUNK_PAYLOAD);
  323. assert(headers->offset == headers->data_size - data_size);
  324. }
  325. return VP8_STATUS_OK; // Return features from VP8 header.
  326. }
  327. VP8StatusCode WebPParseHeaders(WebPHeaderStructure* const headers) {
  328. assert(headers != NULL);
  329. // fill out headers, ignore width/height/has_alpha.
  330. return ParseHeadersInternal(headers->data, headers->data_size,
  331. NULL, NULL, NULL, headers);
  332. }
  333. //------------------------------------------------------------------------------
  334. // WebPDecParams
  335. void WebPResetDecParams(WebPDecParams* const params) {
  336. if (params) {
  337. memset(params, 0, sizeof(*params));
  338. }
  339. }
  340. //------------------------------------------------------------------------------
  341. // "Into" decoding variants
  342. // Main flow
  343. static VP8StatusCode DecodeInto(const uint8_t* const data, size_t data_size,
  344. WebPDecParams* const params) {
  345. VP8StatusCode status;
  346. VP8Io io;
  347. WebPHeaderStructure headers;
  348. headers.data = data;
  349. headers.data_size = data_size;
  350. status = WebPParseHeaders(&headers); // Process Pre-VP8 chunks.
  351. if (status != VP8_STATUS_OK) {
  352. return status;
  353. }
  354. assert(params != NULL);
  355. VP8InitIo(&io);
  356. io.data = headers.data + headers.offset;
  357. io.data_size = headers.data_size - headers.offset;
  358. WebPInitCustomIo(params, &io); // Plug the I/O functions.
  359. if (!headers.is_lossless) {
  360. VP8Decoder* const dec = VP8New();
  361. if (dec == NULL) {
  362. return VP8_STATUS_OUT_OF_MEMORY;
  363. }
  364. #ifdef WEBP_USE_THREAD
  365. dec->use_threads_ = params->options && (params->options->use_threads > 0);
  366. #else
  367. dec->use_threads_ = 0;
  368. #endif
  369. dec->alpha_data_ = headers.alpha_data;
  370. dec->alpha_data_size_ = headers.alpha_data_size;
  371. // Decode bitstream header, update io->width/io->height.
  372. if (!VP8GetHeaders(dec, &io)) {
  373. status = dec->status_; // An error occurred. Grab error status.
  374. } else {
  375. // Allocate/check output buffers.
  376. status = WebPAllocateDecBuffer(io.width, io.height, params->options,
  377. params->output);
  378. if (status == VP8_STATUS_OK) { // Decode
  379. if (!VP8Decode(dec, &io)) {
  380. status = dec->status_;
  381. }
  382. }
  383. }
  384. VP8Delete(dec);
  385. } else {
  386. VP8LDecoder* const dec = VP8LNew();
  387. if (dec == NULL) {
  388. return VP8_STATUS_OUT_OF_MEMORY;
  389. }
  390. if (!VP8LDecodeHeader(dec, &io)) {
  391. status = dec->status_; // An error occurred. Grab error status.
  392. } else {
  393. // Allocate/check output buffers.
  394. status = WebPAllocateDecBuffer(io.width, io.height, params->options,
  395. params->output);
  396. if (status == VP8_STATUS_OK) { // Decode
  397. if (!VP8LDecodeImage(dec)) {
  398. status = dec->status_;
  399. }
  400. }
  401. }
  402. VP8LDelete(dec);
  403. }
  404. if (status != VP8_STATUS_OK) {
  405. WebPFreeDecBuffer(params->output);
  406. }
  407. return status;
  408. }
  409. // Helpers
  410. static uint8_t* DecodeIntoRGBABuffer(WEBP_CSP_MODE colorspace,
  411. const uint8_t* const data,
  412. size_t data_size,
  413. uint8_t* const rgba,
  414. int stride, size_t size) {
  415. WebPDecParams params;
  416. WebPDecBuffer buf;
  417. if (rgba == NULL) {
  418. return NULL;
  419. }
  420. WebPInitDecBuffer(&buf);
  421. WebPResetDecParams(&params);
  422. params.output = &buf;
  423. buf.colorspace = colorspace;
  424. buf.u.RGBA.rgba = rgba;
  425. buf.u.RGBA.stride = stride;
  426. buf.u.RGBA.size = size;
  427. buf.is_external_memory = 1;
  428. if (DecodeInto(data, data_size, &params) != VP8_STATUS_OK) {
  429. return NULL;
  430. }
  431. return rgba;
  432. }
  433. uint8_t* WebPDecodeRGBInto(const uint8_t* data, size_t data_size,
  434. uint8_t* output, size_t size, int stride) {
  435. return DecodeIntoRGBABuffer(MODE_RGB, data, data_size, output, stride, size);
  436. }
  437. uint8_t* WebPDecodeRGBAInto(const uint8_t* data, size_t data_size,
  438. uint8_t* output, size_t size, int stride) {
  439. return DecodeIntoRGBABuffer(MODE_RGBA, data, data_size, output, stride, size);
  440. }
  441. uint8_t* WebPDecodeARGBInto(const uint8_t* data, size_t data_size,
  442. uint8_t* output, size_t size, int stride) {
  443. return DecodeIntoRGBABuffer(MODE_ARGB, data, data_size, output, stride, size);
  444. }
  445. uint8_t* WebPDecodeBGRInto(const uint8_t* data, size_t data_size,
  446. uint8_t* output, size_t size, int stride) {
  447. return DecodeIntoRGBABuffer(MODE_BGR, data, data_size, output, stride, size);
  448. }
  449. uint8_t* WebPDecodeBGRAInto(const uint8_t* data, size_t data_size,
  450. uint8_t* output, size_t size, int stride) {
  451. return DecodeIntoRGBABuffer(MODE_BGRA, data, data_size, output, stride, size);
  452. }
  453. uint8_t* WebPDecodeYUVInto(const uint8_t* data, size_t data_size,
  454. uint8_t* luma, size_t luma_size, int luma_stride,
  455. uint8_t* u, size_t u_size, int u_stride,
  456. uint8_t* v, size_t v_size, int v_stride) {
  457. WebPDecParams params;
  458. WebPDecBuffer output;
  459. if (luma == NULL) return NULL;
  460. WebPInitDecBuffer(&output);
  461. WebPResetDecParams(&params);
  462. params.output = &output;
  463. output.colorspace = MODE_YUV;
  464. output.u.YUVA.y = luma;
  465. output.u.YUVA.y_stride = luma_stride;
  466. output.u.YUVA.y_size = luma_size;
  467. output.u.YUVA.u = u;
  468. output.u.YUVA.u_stride = u_stride;
  469. output.u.YUVA.u_size = u_size;
  470. output.u.YUVA.v = v;
  471. output.u.YUVA.v_stride = v_stride;
  472. output.u.YUVA.v_size = v_size;
  473. output.is_external_memory = 1;
  474. if (DecodeInto(data, data_size, &params) != VP8_STATUS_OK) {
  475. return NULL;
  476. }
  477. return luma;
  478. }
  479. //------------------------------------------------------------------------------
  480. static uint8_t* Decode(WEBP_CSP_MODE mode, const uint8_t* const data,
  481. size_t data_size, int* const width, int* const height,
  482. WebPDecBuffer* const keep_info) {
  483. WebPDecParams params;
  484. WebPDecBuffer output;
  485. WebPInitDecBuffer(&output);
  486. WebPResetDecParams(&params);
  487. params.output = &output;
  488. output.colorspace = mode;
  489. // Retrieve (and report back) the required dimensions from bitstream.
  490. if (!WebPGetInfo(data, data_size, &output.width, &output.height)) {
  491. return NULL;
  492. }
  493. if (width != NULL) *width = output.width;
  494. if (height != NULL) *height = output.height;
  495. // Decode
  496. if (DecodeInto(data, data_size, &params) != VP8_STATUS_OK) {
  497. return NULL;
  498. }
  499. if (keep_info != NULL) { // keep track of the side-info
  500. WebPCopyDecBuffer(&output, keep_info);
  501. }
  502. // return decoded samples (don't clear 'output'!)
  503. return WebPIsRGBMode(mode) ? output.u.RGBA.rgba : output.u.YUVA.y;
  504. }
  505. uint8_t* WebPDecodeRGB(const uint8_t* data, size_t data_size,
  506. int* width, int* height) {
  507. return Decode(MODE_RGB, data, data_size, width, height, NULL);
  508. }
  509. uint8_t* WebPDecodeRGBA(const uint8_t* data, size_t data_size,
  510. int* width, int* height) {
  511. return Decode(MODE_RGBA, data, data_size, width, height, NULL);
  512. }
  513. uint8_t* WebPDecodeARGB(const uint8_t* data, size_t data_size,
  514. int* width, int* height) {
  515. return Decode(MODE_ARGB, data, data_size, width, height, NULL);
  516. }
  517. uint8_t* WebPDecodeBGR(const uint8_t* data, size_t data_size,
  518. int* width, int* height) {
  519. return Decode(MODE_BGR, data, data_size, width, height, NULL);
  520. }
  521. uint8_t* WebPDecodeBGRA(const uint8_t* data, size_t data_size,
  522. int* width, int* height) {
  523. return Decode(MODE_BGRA, data, data_size, width, height, NULL);
  524. }
  525. uint8_t* WebPDecodeYUV(const uint8_t* data, size_t data_size,
  526. int* width, int* height, uint8_t** u, uint8_t** v,
  527. int* stride, int* uv_stride) {
  528. WebPDecBuffer output; // only to preserve the side-infos
  529. uint8_t* const out = Decode(MODE_YUV, data, data_size,
  530. width, height, &output);
  531. if (out != NULL) {
  532. const WebPYUVABuffer* const buf = &output.u.YUVA;
  533. *u = buf->u;
  534. *v = buf->v;
  535. *stride = buf->y_stride;
  536. *uv_stride = buf->u_stride;
  537. assert(buf->u_stride == buf->v_stride);
  538. }
  539. return out;
  540. }
  541. static void DefaultFeatures(WebPBitstreamFeatures* const features) {
  542. assert(features != NULL);
  543. memset(features, 0, sizeof(*features));
  544. features->bitstream_version = 0;
  545. }
  546. static VP8StatusCode GetFeatures(const uint8_t* const data, size_t data_size,
  547. WebPBitstreamFeatures* const features) {
  548. if (features == NULL || data == NULL) {
  549. return VP8_STATUS_INVALID_PARAM;
  550. }
  551. DefaultFeatures(features);
  552. // Only parse enough of the data to retrieve width/height/has_alpha.
  553. return ParseHeadersInternal(data, data_size,
  554. &features->width, &features->height,
  555. &features->has_alpha, NULL);
  556. }
  557. //------------------------------------------------------------------------------
  558. // WebPGetInfo()
  559. int WebPGetInfo(const uint8_t* data, size_t data_size,
  560. int* width, int* height) {
  561. WebPBitstreamFeatures features;
  562. if (GetFeatures(data, data_size, &features) != VP8_STATUS_OK) {
  563. return 0;
  564. }
  565. if (width != NULL) {
  566. *width = features.width;
  567. }
  568. if (height != NULL) {
  569. *height = features.height;
  570. }
  571. return 1;
  572. }
  573. //------------------------------------------------------------------------------
  574. // Advance decoding API
  575. int WebPInitDecoderConfigInternal(WebPDecoderConfig* config,
  576. int version) {
  577. if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_DECODER_ABI_VERSION)) {
  578. return 0; // version mismatch
  579. }
  580. if (config == NULL) {
  581. return 0;
  582. }
  583. memset(config, 0, sizeof(*config));
  584. DefaultFeatures(&config->input);
  585. WebPInitDecBuffer(&config->output);
  586. return 1;
  587. }
  588. VP8StatusCode WebPGetFeaturesInternal(const uint8_t* data, size_t data_size,
  589. WebPBitstreamFeatures* features,
  590. int version) {
  591. VP8StatusCode status;
  592. if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_DECODER_ABI_VERSION)) {
  593. return VP8_STATUS_INVALID_PARAM; // version mismatch
  594. }
  595. if (features == NULL) {
  596. return VP8_STATUS_INVALID_PARAM;
  597. }
  598. status = GetFeatures(data, data_size, features);
  599. if (status == VP8_STATUS_NOT_ENOUGH_DATA) {
  600. return VP8_STATUS_BITSTREAM_ERROR; // Not-enough-data treated as error.
  601. }
  602. return status;
  603. }
  604. VP8StatusCode WebPDecode(const uint8_t* data, size_t data_size,
  605. WebPDecoderConfig* config) {
  606. WebPDecParams params;
  607. VP8StatusCode status;
  608. if (config == NULL) {
  609. return VP8_STATUS_INVALID_PARAM;
  610. }
  611. status = GetFeatures(data, data_size, &config->input);
  612. if (status != VP8_STATUS_OK) {
  613. if (status == VP8_STATUS_NOT_ENOUGH_DATA) {
  614. return VP8_STATUS_BITSTREAM_ERROR; // Not-enough-data treated as error.
  615. }
  616. return status;
  617. }
  618. WebPResetDecParams(&params);
  619. params.output = &config->output;
  620. params.options = &config->options;
  621. status = DecodeInto(data, data_size, &params);
  622. return status;
  623. }
  624. //------------------------------------------------------------------------------
  625. // Cropping and rescaling.
  626. int WebPIoInitFromOptions(const WebPDecoderOptions* const options,
  627. VP8Io* const io, WEBP_CSP_MODE src_colorspace) {
  628. const int W = io->width;
  629. const int H = io->height;
  630. int x = 0, y = 0, w = W, h = H;
  631. // Cropping
  632. io->use_cropping = (options != NULL) && (options->use_cropping > 0);
  633. if (io->use_cropping) {
  634. w = options->crop_width;
  635. h = options->crop_height;
  636. x = options->crop_left;
  637. y = options->crop_top;
  638. if (!WebPIsRGBMode(src_colorspace)) { // only snap for YUV420 or YUV422
  639. x &= ~1;
  640. y &= ~1; // TODO(later): only for YUV420, not YUV422.
  641. }
  642. if (x < 0 || y < 0 || w <= 0 || h <= 0 || x + w > W || y + h > H) {
  643. return 0; // out of frame boundary error
  644. }
  645. }
  646. io->crop_left = x;
  647. io->crop_top = y;
  648. io->crop_right = x + w;
  649. io->crop_bottom = y + h;
  650. io->mb_w = w;
  651. io->mb_h = h;
  652. // Scaling
  653. io->use_scaling = (options != NULL) && (options->use_scaling > 0);
  654. if (io->use_scaling) {
  655. if (options->scaled_width <= 0 || options->scaled_height <= 0) {
  656. return 0;
  657. }
  658. io->scaled_width = options->scaled_width;
  659. io->scaled_height = options->scaled_height;
  660. }
  661. // Filter
  662. io->bypass_filtering = options && options->bypass_filtering;
  663. // Fancy upsampler
  664. #ifdef FANCY_UPSAMPLING
  665. io->fancy_upsampling = (options == NULL) || (!options->no_fancy_upsampling);
  666. #endif
  667. if (io->use_scaling) {
  668. // disable filter (only for large downscaling ratio).
  669. io->bypass_filtering = (io->scaled_width < W * 3 / 4) &&
  670. (io->scaled_height < H * 3 / 4);
  671. io->fancy_upsampling = 0;
  672. }
  673. return 1;
  674. }
  675. //------------------------------------------------------------------------------
  676. #if defined(__cplusplus) || defined(c_plusplus)
  677. } // extern "C"
  678. #endif