webp_dec.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  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. // TODO(jzern): full support of animation frames will require API additions.
  386. if (has_animation) {
  387. status = VP8_STATUS_UNSUPPORTED_FEATURE;
  388. }
  389. }
  390. return status;
  391. }
  392. //------------------------------------------------------------------------------
  393. // WebPDecParams
  394. void WebPResetDecParams(WebPDecParams* const params) {
  395. if (params != NULL) {
  396. memset(params, 0, sizeof(*params));
  397. }
  398. }
  399. //------------------------------------------------------------------------------
  400. // "Into" decoding variants
  401. // Main flow
  402. static VP8StatusCode DecodeInto(const uint8_t* const data, size_t data_size,
  403. WebPDecParams* const params) {
  404. VP8StatusCode status;
  405. VP8Io io;
  406. WebPHeaderStructure headers;
  407. headers.data = data;
  408. headers.data_size = data_size;
  409. headers.have_all_data = 1;
  410. status = WebPParseHeaders(&headers); // Process Pre-VP8 chunks.
  411. if (status != VP8_STATUS_OK) {
  412. return status;
  413. }
  414. assert(params != NULL);
  415. VP8InitIo(&io);
  416. io.data = headers.data + headers.offset;
  417. io.data_size = headers.data_size - headers.offset;
  418. WebPInitCustomIo(params, &io); // Plug the I/O functions.
  419. if (!headers.is_lossless) {
  420. VP8Decoder* const dec = VP8New();
  421. if (dec == NULL) {
  422. return VP8_STATUS_OUT_OF_MEMORY;
  423. }
  424. dec->alpha_data_ = headers.alpha_data;
  425. dec->alpha_data_size_ = headers.alpha_data_size;
  426. // Decode bitstream header, update io->width/io->height.
  427. if (!VP8GetHeaders(dec, &io)) {
  428. status = dec->status_; // An error occurred. Grab error status.
  429. } else {
  430. // Allocate/check output buffers.
  431. status = WebPAllocateDecBuffer(io.width, io.height, params->options,
  432. params->output);
  433. if (status == VP8_STATUS_OK) { // Decode
  434. // This change must be done before calling VP8Decode()
  435. dec->mt_method_ = VP8GetThreadMethod(params->options, &headers,
  436. io.width, io.height);
  437. VP8InitDithering(params->options, dec);
  438. if (!VP8Decode(dec, &io)) {
  439. status = dec->status_;
  440. }
  441. }
  442. }
  443. VP8Delete(dec);
  444. } else {
  445. VP8LDecoder* const dec = VP8LNew();
  446. if (dec == NULL) {
  447. return VP8_STATUS_OUT_OF_MEMORY;
  448. }
  449. if (!VP8LDecodeHeader(dec, &io)) {
  450. status = dec->status_; // An error occurred. Grab error status.
  451. } else {
  452. // Allocate/check output buffers.
  453. status = WebPAllocateDecBuffer(io.width, io.height, params->options,
  454. params->output);
  455. if (status == VP8_STATUS_OK) { // Decode
  456. if (!VP8LDecodeImage(dec)) {
  457. status = dec->status_;
  458. }
  459. }
  460. }
  461. VP8LDelete(dec);
  462. }
  463. if (status != VP8_STATUS_OK) {
  464. WebPFreeDecBuffer(params->output);
  465. } else {
  466. if (params->options != NULL && params->options->flip) {
  467. // This restores the original stride values if options->flip was used
  468. // during the call to WebPAllocateDecBuffer above.
  469. status = WebPFlipBuffer(params->output);
  470. }
  471. }
  472. return status;
  473. }
  474. // Helpers
  475. static uint8_t* DecodeIntoRGBABuffer(WEBP_CSP_MODE colorspace,
  476. const uint8_t* const data,
  477. size_t data_size,
  478. uint8_t* const rgba,
  479. int stride, size_t size) {
  480. WebPDecParams params;
  481. WebPDecBuffer buf;
  482. if (rgba == NULL) {
  483. return NULL;
  484. }
  485. WebPInitDecBuffer(&buf);
  486. WebPResetDecParams(&params);
  487. params.output = &buf;
  488. buf.colorspace = colorspace;
  489. buf.u.RGBA.rgba = rgba;
  490. buf.u.RGBA.stride = stride;
  491. buf.u.RGBA.size = size;
  492. buf.is_external_memory = 1;
  493. if (DecodeInto(data, data_size, &params) != VP8_STATUS_OK) {
  494. return NULL;
  495. }
  496. return rgba;
  497. }
  498. uint8_t* WebPDecodeRGBInto(const uint8_t* data, size_t data_size,
  499. uint8_t* output, size_t size, int stride) {
  500. return DecodeIntoRGBABuffer(MODE_RGB, data, data_size, output, stride, size);
  501. }
  502. uint8_t* WebPDecodeRGBAInto(const uint8_t* data, size_t data_size,
  503. uint8_t* output, size_t size, int stride) {
  504. return DecodeIntoRGBABuffer(MODE_RGBA, data, data_size, output, stride, size);
  505. }
  506. uint8_t* WebPDecodeARGBInto(const uint8_t* data, size_t data_size,
  507. uint8_t* output, size_t size, int stride) {
  508. return DecodeIntoRGBABuffer(MODE_ARGB, data, data_size, output, stride, size);
  509. }
  510. uint8_t* WebPDecodeBGRInto(const uint8_t* data, size_t data_size,
  511. uint8_t* output, size_t size, int stride) {
  512. return DecodeIntoRGBABuffer(MODE_BGR, data, data_size, output, stride, size);
  513. }
  514. uint8_t* WebPDecodeBGRAInto(const uint8_t* data, size_t data_size,
  515. uint8_t* output, size_t size, int stride) {
  516. return DecodeIntoRGBABuffer(MODE_BGRA, data, data_size, output, stride, size);
  517. }
  518. uint8_t* WebPDecodeYUVInto(const uint8_t* data, size_t data_size,
  519. uint8_t* luma, size_t luma_size, int luma_stride,
  520. uint8_t* u, size_t u_size, int u_stride,
  521. uint8_t* v, size_t v_size, int v_stride) {
  522. WebPDecParams params;
  523. WebPDecBuffer output;
  524. if (luma == NULL) return NULL;
  525. WebPInitDecBuffer(&output);
  526. WebPResetDecParams(&params);
  527. params.output = &output;
  528. output.colorspace = MODE_YUV;
  529. output.u.YUVA.y = luma;
  530. output.u.YUVA.y_stride = luma_stride;
  531. output.u.YUVA.y_size = luma_size;
  532. output.u.YUVA.u = u;
  533. output.u.YUVA.u_stride = u_stride;
  534. output.u.YUVA.u_size = u_size;
  535. output.u.YUVA.v = v;
  536. output.u.YUVA.v_stride = v_stride;
  537. output.u.YUVA.v_size = v_size;
  538. output.is_external_memory = 1;
  539. if (DecodeInto(data, data_size, &params) != VP8_STATUS_OK) {
  540. return NULL;
  541. }
  542. return luma;
  543. }
  544. //------------------------------------------------------------------------------
  545. static uint8_t* Decode(WEBP_CSP_MODE mode, const uint8_t* const data,
  546. size_t data_size, int* const width, int* const height,
  547. WebPDecBuffer* const keep_info) {
  548. WebPDecParams params;
  549. WebPDecBuffer output;
  550. WebPInitDecBuffer(&output);
  551. WebPResetDecParams(&params);
  552. params.output = &output;
  553. output.colorspace = mode;
  554. // Retrieve (and report back) the required dimensions from bitstream.
  555. if (!WebPGetInfo(data, data_size, &output.width, &output.height)) {
  556. return NULL;
  557. }
  558. if (width != NULL) *width = output.width;
  559. if (height != NULL) *height = output.height;
  560. // Decode
  561. if (DecodeInto(data, data_size, &params) != VP8_STATUS_OK) {
  562. return NULL;
  563. }
  564. if (keep_info != NULL) { // keep track of the side-info
  565. WebPCopyDecBuffer(&output, keep_info);
  566. }
  567. // return decoded samples (don't clear 'output'!)
  568. return WebPIsRGBMode(mode) ? output.u.RGBA.rgba : output.u.YUVA.y;
  569. }
  570. uint8_t* WebPDecodeRGB(const uint8_t* data, size_t data_size,
  571. int* width, int* height) {
  572. return Decode(MODE_RGB, data, data_size, width, height, NULL);
  573. }
  574. uint8_t* WebPDecodeRGBA(const uint8_t* data, size_t data_size,
  575. int* width, int* height) {
  576. return Decode(MODE_RGBA, data, data_size, width, height, NULL);
  577. }
  578. uint8_t* WebPDecodeARGB(const uint8_t* data, size_t data_size,
  579. int* width, int* height) {
  580. return Decode(MODE_ARGB, data, data_size, width, height, NULL);
  581. }
  582. uint8_t* WebPDecodeBGR(const uint8_t* data, size_t data_size,
  583. int* width, int* height) {
  584. return Decode(MODE_BGR, data, data_size, width, height, NULL);
  585. }
  586. uint8_t* WebPDecodeBGRA(const uint8_t* data, size_t data_size,
  587. int* width, int* height) {
  588. return Decode(MODE_BGRA, data, data_size, width, height, NULL);
  589. }
  590. uint8_t* WebPDecodeYUV(const uint8_t* data, size_t data_size,
  591. int* width, int* height, uint8_t** u, uint8_t** v,
  592. int* stride, int* uv_stride) {
  593. WebPDecBuffer output; // only to preserve the side-infos
  594. uint8_t* const out = Decode(MODE_YUV, data, data_size,
  595. width, height, &output);
  596. if (out != NULL) {
  597. const WebPYUVABuffer* const buf = &output.u.YUVA;
  598. *u = buf->u;
  599. *v = buf->v;
  600. *stride = buf->y_stride;
  601. *uv_stride = buf->u_stride;
  602. assert(buf->u_stride == buf->v_stride);
  603. }
  604. return out;
  605. }
  606. static void DefaultFeatures(WebPBitstreamFeatures* const features) {
  607. assert(features != NULL);
  608. memset(features, 0, sizeof(*features));
  609. }
  610. static VP8StatusCode GetFeatures(const uint8_t* const data, size_t data_size,
  611. WebPBitstreamFeatures* const features) {
  612. if (features == NULL || data == NULL) {
  613. return VP8_STATUS_INVALID_PARAM;
  614. }
  615. DefaultFeatures(features);
  616. // Only parse enough of the data to retrieve the features.
  617. return ParseHeadersInternal(data, data_size,
  618. &features->width, &features->height,
  619. &features->has_alpha, &features->has_animation,
  620. &features->format, NULL);
  621. }
  622. //------------------------------------------------------------------------------
  623. // WebPGetInfo()
  624. int WebPGetInfo(const uint8_t* data, size_t data_size,
  625. int* width, int* height) {
  626. WebPBitstreamFeatures features;
  627. if (GetFeatures(data, data_size, &features) != VP8_STATUS_OK) {
  628. return 0;
  629. }
  630. if (width != NULL) {
  631. *width = features.width;
  632. }
  633. if (height != NULL) {
  634. *height = features.height;
  635. }
  636. return 1;
  637. }
  638. //------------------------------------------------------------------------------
  639. // Advance decoding API
  640. int WebPInitDecoderConfigInternal(WebPDecoderConfig* config,
  641. int version) {
  642. if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_DECODER_ABI_VERSION)) {
  643. return 0; // version mismatch
  644. }
  645. if (config == NULL) {
  646. return 0;
  647. }
  648. memset(config, 0, sizeof(*config));
  649. DefaultFeatures(&config->input);
  650. WebPInitDecBuffer(&config->output);
  651. return 1;
  652. }
  653. VP8StatusCode WebPGetFeaturesInternal(const uint8_t* data, size_t data_size,
  654. WebPBitstreamFeatures* features,
  655. int version) {
  656. if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_DECODER_ABI_VERSION)) {
  657. return VP8_STATUS_INVALID_PARAM; // version mismatch
  658. }
  659. if (features == NULL) {
  660. return VP8_STATUS_INVALID_PARAM;
  661. }
  662. return GetFeatures(data, data_size, features);
  663. }
  664. VP8StatusCode WebPDecode(const uint8_t* data, size_t data_size,
  665. WebPDecoderConfig* config) {
  666. WebPDecParams params;
  667. VP8StatusCode status;
  668. if (config == NULL) {
  669. return VP8_STATUS_INVALID_PARAM;
  670. }
  671. status = GetFeatures(data, data_size, &config->input);
  672. if (status != VP8_STATUS_OK) {
  673. if (status == VP8_STATUS_NOT_ENOUGH_DATA) {
  674. return VP8_STATUS_BITSTREAM_ERROR; // Not-enough-data treated as error.
  675. }
  676. return status;
  677. }
  678. WebPResetDecParams(&params);
  679. params.options = &config->options;
  680. params.output = &config->output;
  681. if (WebPAvoidSlowMemory(params.output, &config->input)) {
  682. // decoding to slow memory: use a temporary in-mem buffer to decode into.
  683. WebPDecBuffer in_mem_buffer;
  684. WebPInitDecBuffer(&in_mem_buffer);
  685. in_mem_buffer.colorspace = config->output.colorspace;
  686. in_mem_buffer.width = config->input.width;
  687. in_mem_buffer.height = config->input.height;
  688. params.output = &in_mem_buffer;
  689. status = DecodeInto(data, data_size, &params);
  690. if (status == VP8_STATUS_OK) { // do the slow-copy
  691. status = WebPCopyDecBufferPixels(&in_mem_buffer, &config->output);
  692. }
  693. WebPFreeDecBuffer(&in_mem_buffer);
  694. } else {
  695. status = DecodeInto(data, data_size, &params);
  696. }
  697. return status;
  698. }
  699. //------------------------------------------------------------------------------
  700. // Cropping and rescaling.
  701. int WebPIoInitFromOptions(const WebPDecoderOptions* const options,
  702. VP8Io* const io, WEBP_CSP_MODE src_colorspace) {
  703. const int W = io->width;
  704. const int H = io->height;
  705. int x = 0, y = 0, w = W, h = H;
  706. // Cropping
  707. io->use_cropping = (options != NULL) && (options->use_cropping > 0);
  708. if (io->use_cropping) {
  709. w = options->crop_width;
  710. h = options->crop_height;
  711. x = options->crop_left;
  712. y = options->crop_top;
  713. if (!WebPIsRGBMode(src_colorspace)) { // only snap for YUV420
  714. x &= ~1;
  715. y &= ~1;
  716. }
  717. if (x < 0 || y < 0 || w <= 0 || h <= 0 || x + w > W || y + h > H) {
  718. return 0; // out of frame boundary error
  719. }
  720. }
  721. io->crop_left = x;
  722. io->crop_top = y;
  723. io->crop_right = x + w;
  724. io->crop_bottom = y + h;
  725. io->mb_w = w;
  726. io->mb_h = h;
  727. // Scaling
  728. io->use_scaling = (options != NULL) && (options->use_scaling > 0);
  729. if (io->use_scaling) {
  730. int scaled_width = options->scaled_width;
  731. int scaled_height = options->scaled_height;
  732. if (!WebPRescalerGetScaledDimensions(w, h, &scaled_width, &scaled_height)) {
  733. return 0;
  734. }
  735. io->scaled_width = scaled_width;
  736. io->scaled_height = scaled_height;
  737. }
  738. // Filter
  739. io->bypass_filtering = (options != NULL) && options->bypass_filtering;
  740. // Fancy upsampler
  741. #ifdef FANCY_UPSAMPLING
  742. io->fancy_upsampling = (options == NULL) || (!options->no_fancy_upsampling);
  743. #endif
  744. if (io->use_scaling) {
  745. // disable filter (only for large downscaling ratio).
  746. io->bypass_filtering = (io->scaled_width < W * 3 / 4) &&
  747. (io->scaled_height < H * 3 / 4);
  748. io->fancy_upsampling = 0;
  749. }
  750. return 1;
  751. }
  752. //------------------------------------------------------------------------------