webp_dec.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  1. // Copyright 2010 Google Inc. All Rights Reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style license
  4. // that can be found in the COPYING file in the root of the source
  5. // tree. An additional intellectual property rights grant can be found
  6. // in the file PATENTS. All contributing project authors may
  7. // be found in the AUTHORS file in the root of the source tree.
  8. // -----------------------------------------------------------------------------
  9. //
  10. // Main decoding functions for WEBP images.
  11. //
  12. // Author: Skal (pascal.massimino@gmail.com)
  13. #include <stdlib.h>
  14. #include "./vp8i_dec.h"
  15. #include "./vp8li_dec.h"
  16. #include "./webpi_dec.h"
  17. #include "../utils/utils.h"
  18. #include "../webp/mux_types.h" // ALPHA_FLAG
  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, ANMF, VP8, VP8L,
  40. // XMP, EXIF ...)
  41. // All sizes are in little-endian order.
  42. // Note: chunk data size must be padded to multiple of 2 when written.
  43. // Validates the RIFF container (if detected) and skips over it.
  44. // If a RIFF container is detected, returns:
  45. // VP8_STATUS_BITSTREAM_ERROR for invalid header,
  46. // VP8_STATUS_NOT_ENOUGH_DATA for truncated data if have_all_data is true,
  47. // and VP8_STATUS_OK otherwise.
  48. // In case there are not enough bytes (partial RIFF container), return 0 for
  49. // *riff_size. Else return the RIFF size extracted from the header.
  50. static VP8StatusCode ParseRIFF(const uint8_t** const data,
  51. size_t* const data_size, int have_all_data,
  52. size_t* const riff_size) {
  53. assert(data != NULL);
  54. assert(data_size != NULL);
  55. assert(riff_size != NULL);
  56. *riff_size = 0; // Default: no RIFF present.
  57. if (*data_size >= RIFF_HEADER_SIZE && !memcmp(*data, "RIFF", TAG_SIZE)) {
  58. if (memcmp(*data + 8, "WEBP", TAG_SIZE)) {
  59. return VP8_STATUS_BITSTREAM_ERROR; // Wrong image file signature.
  60. } else {
  61. const uint32_t size = GetLE32(*data + TAG_SIZE);
  62. // Check that we have at least one chunk (i.e "WEBP" + "VP8?nnnn").
  63. if (size < TAG_SIZE + CHUNK_HEADER_SIZE) {
  64. return VP8_STATUS_BITSTREAM_ERROR;
  65. }
  66. if (size > MAX_CHUNK_PAYLOAD) {
  67. return VP8_STATUS_BITSTREAM_ERROR;
  68. }
  69. if (have_all_data && (size > *data_size - CHUNK_HEADER_SIZE)) {
  70. return VP8_STATUS_NOT_ENOUGH_DATA; // Truncated bitstream.
  71. }
  72. // We have a RIFF container. Skip it.
  73. *riff_size = size;
  74. *data += RIFF_HEADER_SIZE;
  75. *data_size -= RIFF_HEADER_SIZE;
  76. }
  77. }
  78. return VP8_STATUS_OK;
  79. }
  80. // Validates the VP8X header and skips over it.
  81. // Returns VP8_STATUS_BITSTREAM_ERROR for invalid VP8X header,
  82. // VP8_STATUS_NOT_ENOUGH_DATA in case of insufficient data, and
  83. // VP8_STATUS_OK otherwise.
  84. // If a VP8X chunk is found, found_vp8x is set to true and *width_ptr,
  85. // *height_ptr and *flags_ptr are set to the corresponding values extracted
  86. // from the VP8X chunk.
  87. static VP8StatusCode ParseVP8X(const uint8_t** const data,
  88. size_t* const data_size,
  89. int* const found_vp8x,
  90. int* const width_ptr, int* const height_ptr,
  91. uint32_t* const flags_ptr) {
  92. const uint32_t vp8x_size = CHUNK_HEADER_SIZE + VP8X_CHUNK_SIZE;
  93. assert(data != NULL);
  94. assert(data_size != NULL);
  95. assert(found_vp8x != NULL);
  96. *found_vp8x = 0;
  97. if (*data_size < CHUNK_HEADER_SIZE) {
  98. return VP8_STATUS_NOT_ENOUGH_DATA; // Insufficient data.
  99. }
  100. if (!memcmp(*data, "VP8X", TAG_SIZE)) {
  101. int width, height;
  102. uint32_t flags;
  103. const uint32_t chunk_size = GetLE32(*data + TAG_SIZE);
  104. if (chunk_size != VP8X_CHUNK_SIZE) {
  105. return VP8_STATUS_BITSTREAM_ERROR; // Wrong chunk size.
  106. }
  107. // Verify if enough data is available to validate the VP8X chunk.
  108. if (*data_size < vp8x_size) {
  109. return VP8_STATUS_NOT_ENOUGH_DATA; // Insufficient data.
  110. }
  111. flags = GetLE32(*data + 8);
  112. width = 1 + GetLE24(*data + 12);
  113. height = 1 + GetLE24(*data + 15);
  114. if (width * (uint64_t)height >= MAX_IMAGE_AREA) {
  115. return VP8_STATUS_BITSTREAM_ERROR; // image is too large
  116. }
  117. if (flags_ptr != NULL) *flags_ptr = flags;
  118. if (width_ptr != NULL) *width_ptr = width;
  119. if (height_ptr != NULL) *height_ptr = height;
  120. // Skip over VP8X header bytes.
  121. *data += vp8x_size;
  122. *data_size -= vp8x_size;
  123. *found_vp8x = 1;
  124. }
  125. return VP8_STATUS_OK;
  126. }
  127. // Skips to the next VP8/VP8L chunk header in the data given the size of the
  128. // RIFF chunk 'riff_size'.
  129. // Returns VP8_STATUS_BITSTREAM_ERROR if any invalid chunk size is encountered,
  130. // VP8_STATUS_NOT_ENOUGH_DATA in case of insufficient data, and
  131. // VP8_STATUS_OK otherwise.
  132. // If an alpha chunk is found, *alpha_data and *alpha_size are set
  133. // appropriately.
  134. static VP8StatusCode ParseOptionalChunks(const uint8_t** const data,
  135. size_t* const data_size,
  136. size_t const riff_size,
  137. const uint8_t** const alpha_data,
  138. size_t* const alpha_size) {
  139. const uint8_t* buf;
  140. size_t buf_size;
  141. uint32_t total_size = TAG_SIZE + // "WEBP".
  142. CHUNK_HEADER_SIZE + // "VP8Xnnnn".
  143. VP8X_CHUNK_SIZE; // data.
  144. assert(data != NULL);
  145. assert(data_size != NULL);
  146. buf = *data;
  147. buf_size = *data_size;
  148. assert(alpha_data != NULL);
  149. assert(alpha_size != NULL);
  150. *alpha_data = NULL;
  151. *alpha_size = 0;
  152. while (1) {
  153. uint32_t chunk_size;
  154. uint32_t disk_chunk_size; // chunk_size with padding
  155. *data = buf;
  156. *data_size = buf_size;
  157. if (buf_size < CHUNK_HEADER_SIZE) { // Insufficient data.
  158. return VP8_STATUS_NOT_ENOUGH_DATA;
  159. }
  160. chunk_size = GetLE32(buf + TAG_SIZE);
  161. if (chunk_size > MAX_CHUNK_PAYLOAD) {
  162. return VP8_STATUS_BITSTREAM_ERROR; // Not a valid chunk size.
  163. }
  164. // For odd-sized chunk-payload, there's one byte padding at the end.
  165. disk_chunk_size = (CHUNK_HEADER_SIZE + chunk_size + 1) & ~1;
  166. total_size += disk_chunk_size;
  167. // Check that total bytes skipped so far does not exceed riff_size.
  168. if (riff_size > 0 && (total_size > riff_size)) {
  169. return VP8_STATUS_BITSTREAM_ERROR; // Not a valid chunk size.
  170. }
  171. // Start of a (possibly incomplete) VP8/VP8L chunk implies that we have
  172. // parsed all the optional chunks.
  173. // Note: This check must occur before the check 'buf_size < disk_chunk_size'
  174. // below to allow incomplete VP8/VP8L chunks.
  175. if (!memcmp(buf, "VP8 ", TAG_SIZE) ||
  176. !memcmp(buf, "VP8L", TAG_SIZE)) {
  177. return VP8_STATUS_OK;
  178. }
  179. if (buf_size < disk_chunk_size) { // Insufficient data.
  180. return VP8_STATUS_NOT_ENOUGH_DATA;
  181. }
  182. if (!memcmp(buf, "ALPH", TAG_SIZE)) { // A valid ALPH header.
  183. *alpha_data = buf + CHUNK_HEADER_SIZE;
  184. *alpha_size = chunk_size;
  185. }
  186. // We have a full and valid chunk; skip it.
  187. buf += disk_chunk_size;
  188. buf_size -= disk_chunk_size;
  189. }
  190. }
  191. // Validates the VP8/VP8L Header ("VP8 nnnn" or "VP8L nnnn") and skips over it.
  192. // Returns VP8_STATUS_BITSTREAM_ERROR for invalid (chunk larger than
  193. // riff_size) VP8/VP8L header,
  194. // VP8_STATUS_NOT_ENOUGH_DATA in case of insufficient data, and
  195. // VP8_STATUS_OK otherwise.
  196. // If a VP8/VP8L chunk is found, *chunk_size is set to the total number of bytes
  197. // extracted from the VP8/VP8L chunk header.
  198. // The flag '*is_lossless' is set to 1 in case of VP8L chunk / raw VP8L data.
  199. static VP8StatusCode ParseVP8Header(const uint8_t** const data_ptr,
  200. size_t* const data_size, int have_all_data,
  201. size_t riff_size, size_t* const chunk_size,
  202. int* const is_lossless) {
  203. const uint8_t* const data = *data_ptr;
  204. const int is_vp8 = !memcmp(data, "VP8 ", TAG_SIZE);
  205. const int is_vp8l = !memcmp(data, "VP8L", TAG_SIZE);
  206. const uint32_t minimal_size =
  207. TAG_SIZE + CHUNK_HEADER_SIZE; // "WEBP" + "VP8 nnnn" OR
  208. // "WEBP" + "VP8Lnnnn"
  209. assert(data != NULL);
  210. assert(data_size != NULL);
  211. assert(chunk_size != NULL);
  212. assert(is_lossless != NULL);
  213. if (*data_size < CHUNK_HEADER_SIZE) {
  214. return VP8_STATUS_NOT_ENOUGH_DATA; // Insufficient data.
  215. }
  216. if (is_vp8 || is_vp8l) {
  217. // Bitstream contains VP8/VP8L header.
  218. const uint32_t size = GetLE32(data + TAG_SIZE);
  219. if ((riff_size >= minimal_size) && (size > riff_size - minimal_size)) {
  220. return VP8_STATUS_BITSTREAM_ERROR; // Inconsistent size information.
  221. }
  222. if (have_all_data && (size > *data_size - CHUNK_HEADER_SIZE)) {
  223. return VP8_STATUS_NOT_ENOUGH_DATA; // Truncated bitstream.
  224. }
  225. // Skip over CHUNK_HEADER_SIZE bytes from VP8/VP8L Header.
  226. *chunk_size = size;
  227. *data_ptr += CHUNK_HEADER_SIZE;
  228. *data_size -= CHUNK_HEADER_SIZE;
  229. *is_lossless = is_vp8l;
  230. } else {
  231. // Raw VP8/VP8L bitstream (no header).
  232. *is_lossless = VP8LCheckSignature(data, *data_size);
  233. *chunk_size = *data_size;
  234. }
  235. return VP8_STATUS_OK;
  236. }
  237. //------------------------------------------------------------------------------
  238. // Fetch '*width', '*height', '*has_alpha' and fill out 'headers' based on
  239. // 'data'. All the output parameters may be NULL. If 'headers' is NULL only the
  240. // minimal amount will be read to fetch the remaining parameters.
  241. // If 'headers' is non-NULL this function will attempt to locate both alpha
  242. // data (with or without a VP8X chunk) and the bitstream chunk (VP8/VP8L).
  243. // Note: The following chunk sequences (before the raw VP8/VP8L data) are
  244. // considered valid by this function:
  245. // RIFF + VP8(L)
  246. // RIFF + VP8X + (optional chunks) + VP8(L)
  247. // ALPH + VP8 <-- Not a valid WebP format: only allowed for internal purpose.
  248. // VP8(L) <-- Not a valid WebP format: only allowed for internal purpose.
  249. static VP8StatusCode ParseHeadersInternal(const uint8_t* data,
  250. size_t data_size,
  251. int* const width,
  252. int* const height,
  253. int* const has_alpha,
  254. int* const has_animation,
  255. int* const format,
  256. WebPHeaderStructure* const headers) {
  257. int canvas_width = 0;
  258. int canvas_height = 0;
  259. int image_width = 0;
  260. int image_height = 0;
  261. int found_riff = 0;
  262. int found_vp8x = 0;
  263. int animation_present = 0;
  264. const int have_all_data = (headers != NULL) ? headers->have_all_data : 0;
  265. VP8StatusCode status;
  266. WebPHeaderStructure hdrs;
  267. if (data == NULL || data_size < RIFF_HEADER_SIZE) {
  268. return VP8_STATUS_NOT_ENOUGH_DATA;
  269. }
  270. memset(&hdrs, 0, sizeof(hdrs));
  271. hdrs.data = data;
  272. hdrs.data_size = data_size;
  273. // Skip over RIFF header.
  274. status = ParseRIFF(&data, &data_size, have_all_data, &hdrs.riff_size);
  275. if (status != VP8_STATUS_OK) {
  276. return status; // Wrong RIFF header / insufficient data.
  277. }
  278. found_riff = (hdrs.riff_size > 0);
  279. // Skip over VP8X.
  280. {
  281. uint32_t flags = 0;
  282. status = ParseVP8X(&data, &data_size, &found_vp8x,
  283. &canvas_width, &canvas_height, &flags);
  284. if (status != VP8_STATUS_OK) {
  285. return status; // Wrong VP8X / insufficient data.
  286. }
  287. animation_present = !!(flags & ANIMATION_FLAG);
  288. if (!found_riff && found_vp8x) {
  289. // Note: This restriction may be removed in the future, if it becomes
  290. // necessary to send VP8X chunk to the decoder.
  291. return VP8_STATUS_BITSTREAM_ERROR;
  292. }
  293. if (has_alpha != NULL) *has_alpha = !!(flags & ALPHA_FLAG);
  294. if (has_animation != NULL) *has_animation = animation_present;
  295. if (format != NULL) *format = 0; // default = undefined
  296. image_width = canvas_width;
  297. image_height = canvas_height;
  298. if (found_vp8x && animation_present && headers == NULL) {
  299. status = VP8_STATUS_OK;
  300. goto ReturnWidthHeight; // Just return features from VP8X header.
  301. }
  302. }
  303. if (data_size < TAG_SIZE) {
  304. status = VP8_STATUS_NOT_ENOUGH_DATA;
  305. goto ReturnWidthHeight;
  306. }
  307. // Skip over optional chunks if data started with "RIFF + VP8X" or "ALPH".
  308. if ((found_riff && found_vp8x) ||
  309. (!found_riff && !found_vp8x && !memcmp(data, "ALPH", TAG_SIZE))) {
  310. status = ParseOptionalChunks(&data, &data_size, hdrs.riff_size,
  311. &hdrs.alpha_data, &hdrs.alpha_data_size);
  312. if (status != VP8_STATUS_OK) {
  313. goto ReturnWidthHeight; // Invalid chunk size / insufficient data.
  314. }
  315. }
  316. // Skip over VP8/VP8L header.
  317. status = ParseVP8Header(&data, &data_size, have_all_data, hdrs.riff_size,
  318. &hdrs.compressed_size, &hdrs.is_lossless);
  319. if (status != VP8_STATUS_OK) {
  320. goto ReturnWidthHeight; // Wrong VP8/VP8L chunk-header / insufficient data.
  321. }
  322. if (hdrs.compressed_size > MAX_CHUNK_PAYLOAD) {
  323. return VP8_STATUS_BITSTREAM_ERROR;
  324. }
  325. if (format != NULL && !animation_present) {
  326. *format = hdrs.is_lossless ? 2 : 1;
  327. }
  328. if (!hdrs.is_lossless) {
  329. if (data_size < VP8_FRAME_HEADER_SIZE) {
  330. status = VP8_STATUS_NOT_ENOUGH_DATA;
  331. goto ReturnWidthHeight;
  332. }
  333. // Validates raw VP8 data.
  334. if (!VP8GetInfo(data, data_size, (uint32_t)hdrs.compressed_size,
  335. &image_width, &image_height)) {
  336. return VP8_STATUS_BITSTREAM_ERROR;
  337. }
  338. } else {
  339. if (data_size < VP8L_FRAME_HEADER_SIZE) {
  340. status = VP8_STATUS_NOT_ENOUGH_DATA;
  341. goto ReturnWidthHeight;
  342. }
  343. // Validates raw VP8L data.
  344. if (!VP8LGetInfo(data, data_size, &image_width, &image_height, has_alpha)) {
  345. return VP8_STATUS_BITSTREAM_ERROR;
  346. }
  347. }
  348. // Validates image size coherency.
  349. if (found_vp8x) {
  350. if (canvas_width != image_width || canvas_height != image_height) {
  351. return VP8_STATUS_BITSTREAM_ERROR;
  352. }
  353. }
  354. if (headers != NULL) {
  355. *headers = hdrs;
  356. headers->offset = data - headers->data;
  357. assert((uint64_t)(data - headers->data) < MAX_CHUNK_PAYLOAD);
  358. assert(headers->offset == headers->data_size - data_size);
  359. }
  360. ReturnWidthHeight:
  361. if (status == VP8_STATUS_OK ||
  362. (status == VP8_STATUS_NOT_ENOUGH_DATA && found_vp8x && headers == NULL)) {
  363. if (has_alpha != NULL) {
  364. // If the data did not contain a VP8X/VP8L chunk the only definitive way
  365. // to set this is by looking for alpha data (from an ALPH chunk).
  366. *has_alpha |= (hdrs.alpha_data != NULL);
  367. }
  368. if (width != NULL) *width = image_width;
  369. if (height != NULL) *height = image_height;
  370. return VP8_STATUS_OK;
  371. } else {
  372. return status;
  373. }
  374. }
  375. VP8StatusCode WebPParseHeaders(WebPHeaderStructure* const headers) {
  376. // status is marked volatile as a workaround for a clang-3.8 (aarch64) bug
  377. volatile VP8StatusCode status;
  378. int has_animation = 0;
  379. assert(headers != NULL);
  380. // fill out headers, ignore width/height/has_alpha.
  381. status = ParseHeadersInternal(headers->data, headers->data_size,
  382. NULL, NULL, NULL, &has_animation,
  383. NULL, headers);
  384. if (status == VP8_STATUS_OK || status == VP8_STATUS_NOT_ENOUGH_DATA) {
  385. // The WebPDemux API + libwebp can be used to decode individual
  386. // uncomposited frames or the WebPAnimDecoder can be used to fully
  387. // reconstruct them (see webp/demux.h).
  388. if (has_animation) {
  389. status = VP8_STATUS_UNSUPPORTED_FEATURE;
  390. }
  391. }
  392. return status;
  393. }
  394. //------------------------------------------------------------------------------
  395. // WebPDecParams
  396. void WebPResetDecParams(WebPDecParams* const params) {
  397. if (params != NULL) {
  398. memset(params, 0, sizeof(*params));
  399. }
  400. }
  401. //------------------------------------------------------------------------------
  402. // "Into" decoding variants
  403. // Main flow
  404. static VP8StatusCode DecodeInto(const uint8_t* const data, size_t data_size,
  405. WebPDecParams* const params) {
  406. VP8StatusCode status;
  407. VP8Io io;
  408. WebPHeaderStructure headers;
  409. headers.data = data;
  410. headers.data_size = data_size;
  411. headers.have_all_data = 1;
  412. status = WebPParseHeaders(&headers); // Process Pre-VP8 chunks.
  413. if (status != VP8_STATUS_OK) {
  414. return status;
  415. }
  416. assert(params != NULL);
  417. VP8InitIo(&io);
  418. io.data = headers.data + headers.offset;
  419. io.data_size = headers.data_size - headers.offset;
  420. WebPInitCustomIo(params, &io); // Plug the I/O functions.
  421. if (!headers.is_lossless) {
  422. VP8Decoder* const dec = VP8New();
  423. if (dec == NULL) {
  424. return VP8_STATUS_OUT_OF_MEMORY;
  425. }
  426. dec->alpha_data_ = headers.alpha_data;
  427. dec->alpha_data_size_ = headers.alpha_data_size;
  428. // Decode bitstream header, update io->width/io->height.
  429. if (!VP8GetHeaders(dec, &io)) {
  430. status = dec->status_; // An error occurred. Grab error status.
  431. } else {
  432. // Allocate/check output buffers.
  433. status = WebPAllocateDecBuffer(io.width, io.height, params->options,
  434. params->output);
  435. if (status == VP8_STATUS_OK) { // Decode
  436. // This change must be done before calling VP8Decode()
  437. dec->mt_method_ = VP8GetThreadMethod(params->options, &headers,
  438. io.width, io.height);
  439. VP8InitDithering(params->options, dec);
  440. if (!VP8Decode(dec, &io)) {
  441. status = dec->status_;
  442. }
  443. }
  444. }
  445. VP8Delete(dec);
  446. } else {
  447. VP8LDecoder* const dec = VP8LNew();
  448. if (dec == NULL) {
  449. return VP8_STATUS_OUT_OF_MEMORY;
  450. }
  451. if (!VP8LDecodeHeader(dec, &io)) {
  452. status = dec->status_; // An error occurred. Grab error status.
  453. } else {
  454. // Allocate/check output buffers.
  455. status = WebPAllocateDecBuffer(io.width, io.height, params->options,
  456. params->output);
  457. if (status == VP8_STATUS_OK) { // Decode
  458. if (!VP8LDecodeImage(dec)) {
  459. status = dec->status_;
  460. }
  461. }
  462. }
  463. VP8LDelete(dec);
  464. }
  465. if (status != VP8_STATUS_OK) {
  466. WebPFreeDecBuffer(params->output);
  467. } else {
  468. if (params->options != NULL && params->options->flip) {
  469. // This restores the original stride values if options->flip was used
  470. // during the call to WebPAllocateDecBuffer above.
  471. status = WebPFlipBuffer(params->output);
  472. }
  473. }
  474. return status;
  475. }
  476. // Helpers
  477. static uint8_t* DecodeIntoRGBABuffer(WEBP_CSP_MODE colorspace,
  478. const uint8_t* const data,
  479. size_t data_size,
  480. uint8_t* const rgba,
  481. int stride, size_t size) {
  482. WebPDecParams params;
  483. WebPDecBuffer buf;
  484. if (rgba == NULL) {
  485. return NULL;
  486. }
  487. WebPInitDecBuffer(&buf);
  488. WebPResetDecParams(&params);
  489. params.output = &buf;
  490. buf.colorspace = colorspace;
  491. buf.u.RGBA.rgba = rgba;
  492. buf.u.RGBA.stride = stride;
  493. buf.u.RGBA.size = size;
  494. buf.is_external_memory = 1;
  495. if (DecodeInto(data, data_size, &params) != VP8_STATUS_OK) {
  496. return NULL;
  497. }
  498. return rgba;
  499. }
  500. uint8_t* WebPDecodeRGBInto(const uint8_t* data, size_t data_size,
  501. uint8_t* output, size_t size, int stride) {
  502. return DecodeIntoRGBABuffer(MODE_RGB, data, data_size, output, stride, size);
  503. }
  504. uint8_t* WebPDecodeRGBAInto(const uint8_t* data, size_t data_size,
  505. uint8_t* output, size_t size, int stride) {
  506. return DecodeIntoRGBABuffer(MODE_RGBA, data, data_size, output, stride, size);
  507. }
  508. uint8_t* WebPDecodeARGBInto(const uint8_t* data, size_t data_size,
  509. uint8_t* output, size_t size, int stride) {
  510. return DecodeIntoRGBABuffer(MODE_ARGB, data, data_size, output, stride, size);
  511. }
  512. uint8_t* WebPDecodeBGRInto(const uint8_t* data, size_t data_size,
  513. uint8_t* output, size_t size, int stride) {
  514. return DecodeIntoRGBABuffer(MODE_BGR, data, data_size, output, stride, size);
  515. }
  516. uint8_t* WebPDecodeBGRAInto(const uint8_t* data, size_t data_size,
  517. uint8_t* output, size_t size, int stride) {
  518. return DecodeIntoRGBABuffer(MODE_BGRA, data, data_size, output, stride, size);
  519. }
  520. uint8_t* WebPDecodeYUVInto(const uint8_t* data, size_t data_size,
  521. uint8_t* luma, size_t luma_size, int luma_stride,
  522. uint8_t* u, size_t u_size, int u_stride,
  523. uint8_t* v, size_t v_size, int v_stride) {
  524. WebPDecParams params;
  525. WebPDecBuffer output;
  526. if (luma == NULL) return NULL;
  527. WebPInitDecBuffer(&output);
  528. WebPResetDecParams(&params);
  529. params.output = &output;
  530. output.colorspace = MODE_YUV;
  531. output.u.YUVA.y = luma;
  532. output.u.YUVA.y_stride = luma_stride;
  533. output.u.YUVA.y_size = luma_size;
  534. output.u.YUVA.u = u;
  535. output.u.YUVA.u_stride = u_stride;
  536. output.u.YUVA.u_size = u_size;
  537. output.u.YUVA.v = v;
  538. output.u.YUVA.v_stride = v_stride;
  539. output.u.YUVA.v_size = v_size;
  540. output.is_external_memory = 1;
  541. if (DecodeInto(data, data_size, &params) != VP8_STATUS_OK) {
  542. return NULL;
  543. }
  544. return luma;
  545. }
  546. //------------------------------------------------------------------------------
  547. static uint8_t* Decode(WEBP_CSP_MODE mode, const uint8_t* const data,
  548. size_t data_size, int* const width, int* const height,
  549. WebPDecBuffer* const keep_info) {
  550. WebPDecParams params;
  551. WebPDecBuffer output;
  552. WebPInitDecBuffer(&output);
  553. WebPResetDecParams(&params);
  554. params.output = &output;
  555. output.colorspace = mode;
  556. // Retrieve (and report back) the required dimensions from bitstream.
  557. if (!WebPGetInfo(data, data_size, &output.width, &output.height)) {
  558. return NULL;
  559. }
  560. if (width != NULL) *width = output.width;
  561. if (height != NULL) *height = output.height;
  562. // Decode
  563. if (DecodeInto(data, data_size, &params) != VP8_STATUS_OK) {
  564. return NULL;
  565. }
  566. if (keep_info != NULL) { // keep track of the side-info
  567. WebPCopyDecBuffer(&output, keep_info);
  568. }
  569. // return decoded samples (don't clear 'output'!)
  570. return WebPIsRGBMode(mode) ? output.u.RGBA.rgba : output.u.YUVA.y;
  571. }
  572. uint8_t* WebPDecodeRGB(const uint8_t* data, size_t data_size,
  573. int* width, int* height) {
  574. return Decode(MODE_RGB, data, data_size, width, height, NULL);
  575. }
  576. uint8_t* WebPDecodeRGBA(const uint8_t* data, size_t data_size,
  577. int* width, int* height) {
  578. return Decode(MODE_RGBA, data, data_size, width, height, NULL);
  579. }
  580. uint8_t* WebPDecodeARGB(const uint8_t* data, size_t data_size,
  581. int* width, int* height) {
  582. return Decode(MODE_ARGB, data, data_size, width, height, NULL);
  583. }
  584. uint8_t* WebPDecodeBGR(const uint8_t* data, size_t data_size,
  585. int* width, int* height) {
  586. return Decode(MODE_BGR, data, data_size, width, height, NULL);
  587. }
  588. uint8_t* WebPDecodeBGRA(const uint8_t* data, size_t data_size,
  589. int* width, int* height) {
  590. return Decode(MODE_BGRA, data, data_size, width, height, NULL);
  591. }
  592. uint8_t* WebPDecodeYUV(const uint8_t* data, size_t data_size,
  593. int* width, int* height, uint8_t** u, uint8_t** v,
  594. int* stride, int* uv_stride) {
  595. WebPDecBuffer output; // only to preserve the side-infos
  596. uint8_t* const out = Decode(MODE_YUV, data, data_size,
  597. width, height, &output);
  598. if (out != NULL) {
  599. const WebPYUVABuffer* const buf = &output.u.YUVA;
  600. *u = buf->u;
  601. *v = buf->v;
  602. *stride = buf->y_stride;
  603. *uv_stride = buf->u_stride;
  604. assert(buf->u_stride == buf->v_stride);
  605. }
  606. return out;
  607. }
  608. static void DefaultFeatures(WebPBitstreamFeatures* const features) {
  609. assert(features != NULL);
  610. memset(features, 0, sizeof(*features));
  611. }
  612. static VP8StatusCode GetFeatures(const uint8_t* const data, size_t data_size,
  613. WebPBitstreamFeatures* const features) {
  614. if (features == NULL || data == NULL) {
  615. return VP8_STATUS_INVALID_PARAM;
  616. }
  617. DefaultFeatures(features);
  618. // Only parse enough of the data to retrieve the features.
  619. return ParseHeadersInternal(data, data_size,
  620. &features->width, &features->height,
  621. &features->has_alpha, &features->has_animation,
  622. &features->format, NULL);
  623. }
  624. //------------------------------------------------------------------------------
  625. // WebPGetInfo()
  626. int WebPGetInfo(const uint8_t* data, size_t data_size,
  627. int* width, int* height) {
  628. WebPBitstreamFeatures features;
  629. if (GetFeatures(data, data_size, &features) != VP8_STATUS_OK) {
  630. return 0;
  631. }
  632. if (width != NULL) {
  633. *width = features.width;
  634. }
  635. if (height != NULL) {
  636. *height = features.height;
  637. }
  638. return 1;
  639. }
  640. //------------------------------------------------------------------------------
  641. // Advance decoding API
  642. int WebPInitDecoderConfigInternal(WebPDecoderConfig* config,
  643. int version) {
  644. if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_DECODER_ABI_VERSION)) {
  645. return 0; // version mismatch
  646. }
  647. if (config == NULL) {
  648. return 0;
  649. }
  650. memset(config, 0, sizeof(*config));
  651. DefaultFeatures(&config->input);
  652. WebPInitDecBuffer(&config->output);
  653. return 1;
  654. }
  655. VP8StatusCode WebPGetFeaturesInternal(const uint8_t* data, size_t data_size,
  656. WebPBitstreamFeatures* features,
  657. int version) {
  658. if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_DECODER_ABI_VERSION)) {
  659. return VP8_STATUS_INVALID_PARAM; // version mismatch
  660. }
  661. if (features == NULL) {
  662. return VP8_STATUS_INVALID_PARAM;
  663. }
  664. return GetFeatures(data, data_size, features);
  665. }
  666. VP8StatusCode WebPDecode(const uint8_t* data, size_t data_size,
  667. WebPDecoderConfig* config) {
  668. WebPDecParams params;
  669. VP8StatusCode status;
  670. if (config == NULL) {
  671. return VP8_STATUS_INVALID_PARAM;
  672. }
  673. status = GetFeatures(data, data_size, &config->input);
  674. if (status != VP8_STATUS_OK) {
  675. if (status == VP8_STATUS_NOT_ENOUGH_DATA) {
  676. return VP8_STATUS_BITSTREAM_ERROR; // Not-enough-data treated as error.
  677. }
  678. return status;
  679. }
  680. WebPResetDecParams(&params);
  681. params.options = &config->options;
  682. params.output = &config->output;
  683. if (WebPAvoidSlowMemory(params.output, &config->input)) {
  684. // decoding to slow memory: use a temporary in-mem buffer to decode into.
  685. WebPDecBuffer in_mem_buffer;
  686. WebPInitDecBuffer(&in_mem_buffer);
  687. in_mem_buffer.colorspace = config->output.colorspace;
  688. in_mem_buffer.width = config->input.width;
  689. in_mem_buffer.height = config->input.height;
  690. params.output = &in_mem_buffer;
  691. status = DecodeInto(data, data_size, &params);
  692. if (status == VP8_STATUS_OK) { // do the slow-copy
  693. status = WebPCopyDecBufferPixels(&in_mem_buffer, &config->output);
  694. }
  695. WebPFreeDecBuffer(&in_mem_buffer);
  696. } else {
  697. status = DecodeInto(data, data_size, &params);
  698. }
  699. return status;
  700. }
  701. //------------------------------------------------------------------------------
  702. // Cropping and rescaling.
  703. int WebPCheckCropDimensions(int image_width, int image_height,
  704. int x, int y, int w, int h) {
  705. return !(x < 0 || y < 0 || w <= 0 || h <= 0 ||
  706. x >= image_width || w > image_width || w > image_width - x ||
  707. y >= image_height || h > image_height || h > image_height - y);
  708. }
  709. int WebPIoInitFromOptions(const WebPDecoderOptions* const options,
  710. VP8Io* const io, WEBP_CSP_MODE src_colorspace) {
  711. const int W = io->width;
  712. const int H = io->height;
  713. int x = 0, y = 0, w = W, h = H;
  714. // Cropping
  715. io->use_cropping = (options != NULL) && options->use_cropping;
  716. if (io->use_cropping) {
  717. w = options->crop_width;
  718. h = options->crop_height;
  719. x = options->crop_left;
  720. y = options->crop_top;
  721. if (!WebPIsRGBMode(src_colorspace)) { // only snap for YUV420
  722. x &= ~1;
  723. y &= ~1;
  724. }
  725. if (!WebPCheckCropDimensions(W, H, x, y, w, h)) {
  726. return 0; // out of frame boundary error
  727. }
  728. }
  729. io->crop_left = x;
  730. io->crop_top = y;
  731. io->crop_right = x + w;
  732. io->crop_bottom = y + h;
  733. io->mb_w = w;
  734. io->mb_h = h;
  735. // Scaling
  736. io->use_scaling = (options != NULL) && options->use_scaling;
  737. if (io->use_scaling) {
  738. int scaled_width = options->scaled_width;
  739. int scaled_height = options->scaled_height;
  740. if (!WebPRescalerGetScaledDimensions(w, h, &scaled_width, &scaled_height)) {
  741. return 0;
  742. }
  743. io->scaled_width = scaled_width;
  744. io->scaled_height = scaled_height;
  745. }
  746. // Filter
  747. io->bypass_filtering = (options != NULL) && options->bypass_filtering;
  748. // Fancy upsampler
  749. #ifdef FANCY_UPSAMPLING
  750. io->fancy_upsampling = (options == NULL) || (!options->no_fancy_upsampling);
  751. #endif
  752. if (io->use_scaling) {
  753. // disable filter (only for large downscaling ratio).
  754. io->bypass_filtering |= (io->scaled_width < W * 3 / 4) &&
  755. (io->scaled_height < H * 3 / 4);
  756. io->fancy_upsampling = 0;
  757. }
  758. return 1;
  759. }
  760. //------------------------------------------------------------------------------