idec_dec.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  1. // Copyright 2011 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. // Incremental decoding
  11. //
  12. // Author: somnath@google.com (Somnath Banerjee)
  13. #include <assert.h>
  14. #include <string.h>
  15. #include <stdlib.h>
  16. #include "./alphai_dec.h"
  17. #include "./webpi_dec.h"
  18. #include "./vp8i_dec.h"
  19. #include "../utils/utils.h"
  20. // In append mode, buffer allocations increase as multiples of this value.
  21. // Needs to be a power of 2.
  22. #define CHUNK_SIZE 4096
  23. #define MAX_MB_SIZE 4096
  24. //------------------------------------------------------------------------------
  25. // Data structures for memory and states
  26. // Decoding states. State normally flows as:
  27. // WEBP_HEADER->VP8_HEADER->VP8_PARTS0->VP8_DATA->DONE for a lossy image, and
  28. // WEBP_HEADER->VP8L_HEADER->VP8L_DATA->DONE for a lossless image.
  29. // If there is any error the decoder goes into state ERROR.
  30. typedef enum {
  31. STATE_WEBP_HEADER, // All the data before that of the VP8/VP8L chunk.
  32. STATE_VP8_HEADER, // The VP8 Frame header (within the VP8 chunk).
  33. STATE_VP8_PARTS0,
  34. STATE_VP8_DATA,
  35. STATE_VP8L_HEADER,
  36. STATE_VP8L_DATA,
  37. STATE_DONE,
  38. STATE_ERROR
  39. } DecState;
  40. // Operating state for the MemBuffer
  41. typedef enum {
  42. MEM_MODE_NONE = 0,
  43. MEM_MODE_APPEND,
  44. MEM_MODE_MAP
  45. } MemBufferMode;
  46. // storage for partition #0 and partial data (in a rolling fashion)
  47. typedef struct {
  48. MemBufferMode mode_; // Operation mode
  49. size_t start_; // start location of the data to be decoded
  50. size_t end_; // end location
  51. size_t buf_size_; // size of the allocated buffer
  52. uint8_t* buf_; // We don't own this buffer in case WebPIUpdate()
  53. size_t part0_size_; // size of partition #0
  54. const uint8_t* part0_buf_; // buffer to store partition #0
  55. } MemBuffer;
  56. struct WebPIDecoder {
  57. DecState state_; // current decoding state
  58. WebPDecParams params_; // Params to store output info
  59. int is_lossless_; // for down-casting 'dec_'.
  60. void* dec_; // either a VP8Decoder or a VP8LDecoder instance
  61. VP8Io io_;
  62. MemBuffer mem_; // input memory buffer.
  63. WebPDecBuffer output_; // output buffer (when no external one is supplied,
  64. // or if the external one has slow-memory)
  65. WebPDecBuffer* final_output_; // Slow-memory output to copy to eventually.
  66. size_t chunk_size_; // Compressed VP8/VP8L size extracted from Header.
  67. int last_mb_y_; // last row reached for intra-mode decoding
  68. };
  69. // MB context to restore in case VP8DecodeMB() fails
  70. typedef struct {
  71. VP8MB left_;
  72. VP8MB info_;
  73. VP8BitReader token_br_;
  74. } MBContext;
  75. //------------------------------------------------------------------------------
  76. // MemBuffer: incoming data handling
  77. static WEBP_INLINE size_t MemDataSize(const MemBuffer* mem) {
  78. return (mem->end_ - mem->start_);
  79. }
  80. // Check if we need to preserve the compressed alpha data, as it may not have
  81. // been decoded yet.
  82. static int NeedCompressedAlpha(const WebPIDecoder* const idec) {
  83. if (idec->state_ == STATE_WEBP_HEADER) {
  84. // We haven't parsed the headers yet, so we don't know whether the image is
  85. // lossy or lossless. This also means that we haven't parsed the ALPH chunk.
  86. return 0;
  87. }
  88. if (idec->is_lossless_) {
  89. return 0; // ALPH chunk is not present for lossless images.
  90. } else {
  91. const VP8Decoder* const dec = (VP8Decoder*)idec->dec_;
  92. assert(dec != NULL); // Must be true as idec->state_ != STATE_WEBP_HEADER.
  93. return (dec->alpha_data_ != NULL) && !dec->is_alpha_decoded_;
  94. }
  95. }
  96. static void DoRemap(WebPIDecoder* const idec, ptrdiff_t offset) {
  97. MemBuffer* const mem = &idec->mem_;
  98. const uint8_t* const new_base = mem->buf_ + mem->start_;
  99. // note: for VP8, setting up idec->io_ is only really needed at the beginning
  100. // of the decoding, till partition #0 is complete.
  101. idec->io_.data = new_base;
  102. idec->io_.data_size = MemDataSize(mem);
  103. if (idec->dec_ != NULL) {
  104. if (!idec->is_lossless_) {
  105. VP8Decoder* const dec = (VP8Decoder*)idec->dec_;
  106. const uint32_t last_part = dec->num_parts_minus_one_;
  107. if (offset != 0) {
  108. uint32_t p;
  109. for (p = 0; p <= last_part; ++p) {
  110. VP8RemapBitReader(dec->parts_ + p, offset);
  111. }
  112. // Remap partition #0 data pointer to new offset, but only in MAP
  113. // mode (in APPEND mode, partition #0 is copied into a fixed memory).
  114. if (mem->mode_ == MEM_MODE_MAP) {
  115. VP8RemapBitReader(&dec->br_, offset);
  116. }
  117. }
  118. {
  119. const uint8_t* const last_start = dec->parts_[last_part].buf_;
  120. VP8BitReaderSetBuffer(&dec->parts_[last_part], last_start,
  121. mem->buf_ + mem->end_ - last_start);
  122. }
  123. if (NeedCompressedAlpha(idec)) {
  124. ALPHDecoder* const alph_dec = dec->alph_dec_;
  125. dec->alpha_data_ += offset;
  126. if (alph_dec != NULL) {
  127. if (alph_dec->method_ == ALPHA_LOSSLESS_COMPRESSION) {
  128. VP8LDecoder* const alph_vp8l_dec = alph_dec->vp8l_dec_;
  129. assert(alph_vp8l_dec != NULL);
  130. assert(dec->alpha_data_size_ >= ALPHA_HEADER_LEN);
  131. VP8LBitReaderSetBuffer(&alph_vp8l_dec->br_,
  132. dec->alpha_data_ + ALPHA_HEADER_LEN,
  133. dec->alpha_data_size_ - ALPHA_HEADER_LEN);
  134. } else { // alph_dec->method_ == ALPHA_NO_COMPRESSION
  135. // Nothing special to do in this case.
  136. }
  137. }
  138. }
  139. } else { // Resize lossless bitreader
  140. VP8LDecoder* const dec = (VP8LDecoder*)idec->dec_;
  141. VP8LBitReaderSetBuffer(&dec->br_, new_base, MemDataSize(mem));
  142. }
  143. }
  144. }
  145. // Appends data to the end of MemBuffer->buf_. It expands the allocated memory
  146. // size if required and also updates VP8BitReader's if new memory is allocated.
  147. static int AppendToMemBuffer(WebPIDecoder* const idec,
  148. const uint8_t* const data, size_t data_size) {
  149. VP8Decoder* const dec = (VP8Decoder*)idec->dec_;
  150. MemBuffer* const mem = &idec->mem_;
  151. const int need_compressed_alpha = NeedCompressedAlpha(idec);
  152. const uint8_t* const old_start = mem->buf_ + mem->start_;
  153. const uint8_t* const old_base =
  154. need_compressed_alpha ? dec->alpha_data_ : old_start;
  155. assert(mem->mode_ == MEM_MODE_APPEND);
  156. if (data_size > MAX_CHUNK_PAYLOAD) {
  157. // security safeguard: trying to allocate more than what the format
  158. // allows for a chunk should be considered a smoke smell.
  159. return 0;
  160. }
  161. if (mem->end_ + data_size > mem->buf_size_) { // Need some free memory
  162. const size_t new_mem_start = old_start - old_base;
  163. const size_t current_size = MemDataSize(mem) + new_mem_start;
  164. const uint64_t new_size = (uint64_t)current_size + data_size;
  165. const uint64_t extra_size = (new_size + CHUNK_SIZE - 1) & ~(CHUNK_SIZE - 1);
  166. uint8_t* const new_buf =
  167. (uint8_t*)WebPSafeMalloc(extra_size, sizeof(*new_buf));
  168. if (new_buf == NULL) return 0;
  169. memcpy(new_buf, old_base, current_size);
  170. WebPSafeFree(mem->buf_);
  171. mem->buf_ = new_buf;
  172. mem->buf_size_ = (size_t)extra_size;
  173. mem->start_ = new_mem_start;
  174. mem->end_ = current_size;
  175. }
  176. memcpy(mem->buf_ + mem->end_, data, data_size);
  177. mem->end_ += data_size;
  178. assert(mem->end_ <= mem->buf_size_);
  179. DoRemap(idec, mem->buf_ + mem->start_ - old_start);
  180. return 1;
  181. }
  182. static int RemapMemBuffer(WebPIDecoder* const idec,
  183. const uint8_t* const data, size_t data_size) {
  184. MemBuffer* const mem = &idec->mem_;
  185. const uint8_t* const old_buf = mem->buf_;
  186. const uint8_t* const old_start = old_buf + mem->start_;
  187. assert(mem->mode_ == MEM_MODE_MAP);
  188. if (data_size < mem->buf_size_) return 0; // can't remap to a shorter buffer!
  189. mem->buf_ = (uint8_t*)data;
  190. mem->end_ = mem->buf_size_ = data_size;
  191. DoRemap(idec, mem->buf_ + mem->start_ - old_start);
  192. return 1;
  193. }
  194. static void InitMemBuffer(MemBuffer* const mem) {
  195. mem->mode_ = MEM_MODE_NONE;
  196. mem->buf_ = NULL;
  197. mem->buf_size_ = 0;
  198. mem->part0_buf_ = NULL;
  199. mem->part0_size_ = 0;
  200. }
  201. static void ClearMemBuffer(MemBuffer* const mem) {
  202. assert(mem);
  203. if (mem->mode_ == MEM_MODE_APPEND) {
  204. WebPSafeFree(mem->buf_);
  205. WebPSafeFree((void*)mem->part0_buf_);
  206. }
  207. }
  208. static int CheckMemBufferMode(MemBuffer* const mem, MemBufferMode expected) {
  209. if (mem->mode_ == MEM_MODE_NONE) {
  210. mem->mode_ = expected; // switch to the expected mode
  211. } else if (mem->mode_ != expected) {
  212. return 0; // we mixed the modes => error
  213. }
  214. assert(mem->mode_ == expected); // mode is ok
  215. return 1;
  216. }
  217. // To be called last.
  218. static VP8StatusCode FinishDecoding(WebPIDecoder* const idec) {
  219. const WebPDecoderOptions* const options = idec->params_.options;
  220. WebPDecBuffer* const output = idec->params_.output;
  221. idec->state_ = STATE_DONE;
  222. if (options != NULL && options->flip) {
  223. const VP8StatusCode status = WebPFlipBuffer(output);
  224. if (status != VP8_STATUS_OK) return status;
  225. }
  226. if (idec->final_output_ != NULL) {
  227. WebPCopyDecBufferPixels(output, idec->final_output_); // do the slow-copy
  228. WebPFreeDecBuffer(&idec->output_);
  229. *output = *idec->final_output_;
  230. idec->final_output_ = NULL;
  231. }
  232. return VP8_STATUS_OK;
  233. }
  234. //------------------------------------------------------------------------------
  235. // Macroblock-decoding contexts
  236. static void SaveContext(const VP8Decoder* dec, const VP8BitReader* token_br,
  237. MBContext* const context) {
  238. context->left_ = dec->mb_info_[-1];
  239. context->info_ = dec->mb_info_[dec->mb_x_];
  240. context->token_br_ = *token_br;
  241. }
  242. static void RestoreContext(const MBContext* context, VP8Decoder* const dec,
  243. VP8BitReader* const token_br) {
  244. dec->mb_info_[-1] = context->left_;
  245. dec->mb_info_[dec->mb_x_] = context->info_;
  246. *token_br = context->token_br_;
  247. }
  248. //------------------------------------------------------------------------------
  249. static VP8StatusCode IDecError(WebPIDecoder* const idec, VP8StatusCode error) {
  250. if (idec->state_ == STATE_VP8_DATA) {
  251. VP8Io* const io = &idec->io_;
  252. if (io->teardown != NULL) {
  253. io->teardown(io);
  254. }
  255. }
  256. idec->state_ = STATE_ERROR;
  257. return error;
  258. }
  259. static void ChangeState(WebPIDecoder* const idec, DecState new_state,
  260. size_t consumed_bytes) {
  261. MemBuffer* const mem = &idec->mem_;
  262. idec->state_ = new_state;
  263. mem->start_ += consumed_bytes;
  264. assert(mem->start_ <= mem->end_);
  265. idec->io_.data = mem->buf_ + mem->start_;
  266. idec->io_.data_size = MemDataSize(mem);
  267. }
  268. // Headers
  269. static VP8StatusCode DecodeWebPHeaders(WebPIDecoder* const idec) {
  270. MemBuffer* const mem = &idec->mem_;
  271. const uint8_t* data = mem->buf_ + mem->start_;
  272. size_t curr_size = MemDataSize(mem);
  273. VP8StatusCode status;
  274. WebPHeaderStructure headers;
  275. headers.data = data;
  276. headers.data_size = curr_size;
  277. headers.have_all_data = 0;
  278. status = WebPParseHeaders(&headers);
  279. if (status == VP8_STATUS_NOT_ENOUGH_DATA) {
  280. return VP8_STATUS_SUSPENDED; // We haven't found a VP8 chunk yet.
  281. } else if (status != VP8_STATUS_OK) {
  282. return IDecError(idec, status);
  283. }
  284. idec->chunk_size_ = headers.compressed_size;
  285. idec->is_lossless_ = headers.is_lossless;
  286. if (!idec->is_lossless_) {
  287. VP8Decoder* const dec = VP8New();
  288. if (dec == NULL) {
  289. return VP8_STATUS_OUT_OF_MEMORY;
  290. }
  291. idec->dec_ = dec;
  292. dec->alpha_data_ = headers.alpha_data;
  293. dec->alpha_data_size_ = headers.alpha_data_size;
  294. ChangeState(idec, STATE_VP8_HEADER, headers.offset);
  295. } else {
  296. VP8LDecoder* const dec = VP8LNew();
  297. if (dec == NULL) {
  298. return VP8_STATUS_OUT_OF_MEMORY;
  299. }
  300. idec->dec_ = dec;
  301. ChangeState(idec, STATE_VP8L_HEADER, headers.offset);
  302. }
  303. return VP8_STATUS_OK;
  304. }
  305. static VP8StatusCode DecodeVP8FrameHeader(WebPIDecoder* const idec) {
  306. const uint8_t* data = idec->mem_.buf_ + idec->mem_.start_;
  307. const size_t curr_size = MemDataSize(&idec->mem_);
  308. int width, height;
  309. uint32_t bits;
  310. if (curr_size < VP8_FRAME_HEADER_SIZE) {
  311. // Not enough data bytes to extract VP8 Frame Header.
  312. return VP8_STATUS_SUSPENDED;
  313. }
  314. if (!VP8GetInfo(data, curr_size, idec->chunk_size_, &width, &height)) {
  315. return IDecError(idec, VP8_STATUS_BITSTREAM_ERROR);
  316. }
  317. bits = data[0] | (data[1] << 8) | (data[2] << 16);
  318. idec->mem_.part0_size_ = (bits >> 5) + VP8_FRAME_HEADER_SIZE;
  319. idec->io_.data = data;
  320. idec->io_.data_size = curr_size;
  321. idec->state_ = STATE_VP8_PARTS0;
  322. return VP8_STATUS_OK;
  323. }
  324. // Partition #0
  325. static VP8StatusCode CopyParts0Data(WebPIDecoder* const idec) {
  326. VP8Decoder* const dec = (VP8Decoder*)idec->dec_;
  327. VP8BitReader* const br = &dec->br_;
  328. const size_t part_size = br->buf_end_ - br->buf_;
  329. MemBuffer* const mem = &idec->mem_;
  330. assert(!idec->is_lossless_);
  331. assert(mem->part0_buf_ == NULL);
  332. // the following is a format limitation, no need for runtime check:
  333. assert(part_size <= mem->part0_size_);
  334. if (part_size == 0) { // can't have zero-size partition #0
  335. return VP8_STATUS_BITSTREAM_ERROR;
  336. }
  337. if (mem->mode_ == MEM_MODE_APPEND) {
  338. // We copy and grab ownership of the partition #0 data.
  339. uint8_t* const part0_buf = (uint8_t*)WebPSafeMalloc(1ULL, part_size);
  340. if (part0_buf == NULL) {
  341. return VP8_STATUS_OUT_OF_MEMORY;
  342. }
  343. memcpy(part0_buf, br->buf_, part_size);
  344. mem->part0_buf_ = part0_buf;
  345. VP8BitReaderSetBuffer(br, part0_buf, part_size);
  346. } else {
  347. // Else: just keep pointers to the partition #0's data in dec_->br_.
  348. }
  349. mem->start_ += part_size;
  350. return VP8_STATUS_OK;
  351. }
  352. static VP8StatusCode DecodePartition0(WebPIDecoder* const idec) {
  353. VP8Decoder* const dec = (VP8Decoder*)idec->dec_;
  354. VP8Io* const io = &idec->io_;
  355. const WebPDecParams* const params = &idec->params_;
  356. WebPDecBuffer* const output = params->output;
  357. // Wait till we have enough data for the whole partition #0
  358. if (MemDataSize(&idec->mem_) < idec->mem_.part0_size_) {
  359. return VP8_STATUS_SUSPENDED;
  360. }
  361. if (!VP8GetHeaders(dec, io)) {
  362. const VP8StatusCode status = dec->status_;
  363. if (status == VP8_STATUS_SUSPENDED ||
  364. status == VP8_STATUS_NOT_ENOUGH_DATA) {
  365. // treating NOT_ENOUGH_DATA as SUSPENDED state
  366. return VP8_STATUS_SUSPENDED;
  367. }
  368. return IDecError(idec, status);
  369. }
  370. // Allocate/Verify output buffer now
  371. dec->status_ = WebPAllocateDecBuffer(io->width, io->height, params->options,
  372. output);
  373. if (dec->status_ != VP8_STATUS_OK) {
  374. return IDecError(idec, dec->status_);
  375. }
  376. // This change must be done before calling VP8InitFrame()
  377. dec->mt_method_ = VP8GetThreadMethod(params->options, NULL,
  378. io->width, io->height);
  379. VP8InitDithering(params->options, dec);
  380. dec->status_ = CopyParts0Data(idec);
  381. if (dec->status_ != VP8_STATUS_OK) {
  382. return IDecError(idec, dec->status_);
  383. }
  384. // Finish setting up the decoding parameters. Will call io->setup().
  385. if (VP8EnterCritical(dec, io) != VP8_STATUS_OK) {
  386. return IDecError(idec, dec->status_);
  387. }
  388. // Note: past this point, teardown() must always be called
  389. // in case of error.
  390. idec->state_ = STATE_VP8_DATA;
  391. // Allocate memory and prepare everything.
  392. if (!VP8InitFrame(dec, io)) {
  393. return IDecError(idec, dec->status_);
  394. }
  395. return VP8_STATUS_OK;
  396. }
  397. // Remaining partitions
  398. static VP8StatusCode DecodeRemaining(WebPIDecoder* const idec) {
  399. VP8Decoder* const dec = (VP8Decoder*)idec->dec_;
  400. VP8Io* const io = &idec->io_;
  401. assert(dec->ready_);
  402. for (; dec->mb_y_ < dec->mb_h_; ++dec->mb_y_) {
  403. if (idec->last_mb_y_ != dec->mb_y_) {
  404. if (!VP8ParseIntraModeRow(&dec->br_, dec)) {
  405. // note: normally, error shouldn't occur since we already have the whole
  406. // partition0 available here in DecodeRemaining(). Reaching EOF while
  407. // reading intra modes really means a BITSTREAM_ERROR.
  408. return IDecError(idec, VP8_STATUS_BITSTREAM_ERROR);
  409. }
  410. idec->last_mb_y_ = dec->mb_y_;
  411. }
  412. for (; dec->mb_x_ < dec->mb_w_; ++dec->mb_x_) {
  413. VP8BitReader* const token_br =
  414. &dec->parts_[dec->mb_y_ & dec->num_parts_minus_one_];
  415. MBContext context;
  416. SaveContext(dec, token_br, &context);
  417. if (!VP8DecodeMB(dec, token_br)) {
  418. // We shouldn't fail when MAX_MB data was available
  419. if (dec->num_parts_minus_one_ == 0 &&
  420. MemDataSize(&idec->mem_) > MAX_MB_SIZE) {
  421. return IDecError(idec, VP8_STATUS_BITSTREAM_ERROR);
  422. }
  423. RestoreContext(&context, dec, token_br);
  424. return VP8_STATUS_SUSPENDED;
  425. }
  426. // Release buffer only if there is only one partition
  427. if (dec->num_parts_minus_one_ == 0) {
  428. idec->mem_.start_ = token_br->buf_ - idec->mem_.buf_;
  429. assert(idec->mem_.start_ <= idec->mem_.end_);
  430. }
  431. }
  432. VP8InitScanline(dec); // Prepare for next scanline
  433. // Reconstruct, filter and emit the row.
  434. if (!VP8ProcessRow(dec, io)) {
  435. return IDecError(idec, VP8_STATUS_USER_ABORT);
  436. }
  437. }
  438. // Synchronize the thread and check for errors.
  439. if (!VP8ExitCritical(dec, io)) {
  440. return IDecError(idec, VP8_STATUS_USER_ABORT);
  441. }
  442. dec->ready_ = 0;
  443. return FinishDecoding(idec);
  444. }
  445. static VP8StatusCode ErrorStatusLossless(WebPIDecoder* const idec,
  446. VP8StatusCode status) {
  447. if (status == VP8_STATUS_SUSPENDED || status == VP8_STATUS_NOT_ENOUGH_DATA) {
  448. return VP8_STATUS_SUSPENDED;
  449. }
  450. return IDecError(idec, status);
  451. }
  452. static VP8StatusCode DecodeVP8LHeader(WebPIDecoder* const idec) {
  453. VP8Io* const io = &idec->io_;
  454. VP8LDecoder* const dec = (VP8LDecoder*)idec->dec_;
  455. const WebPDecParams* const params = &idec->params_;
  456. WebPDecBuffer* const output = params->output;
  457. size_t curr_size = MemDataSize(&idec->mem_);
  458. assert(idec->is_lossless_);
  459. // Wait until there's enough data for decoding header.
  460. if (curr_size < (idec->chunk_size_ >> 3)) {
  461. dec->status_ = VP8_STATUS_SUSPENDED;
  462. return ErrorStatusLossless(idec, dec->status_);
  463. }
  464. if (!VP8LDecodeHeader(dec, io)) {
  465. if (dec->status_ == VP8_STATUS_BITSTREAM_ERROR &&
  466. curr_size < idec->chunk_size_) {
  467. dec->status_ = VP8_STATUS_SUSPENDED;
  468. }
  469. return ErrorStatusLossless(idec, dec->status_);
  470. }
  471. // Allocate/verify output buffer now.
  472. dec->status_ = WebPAllocateDecBuffer(io->width, io->height, params->options,
  473. output);
  474. if (dec->status_ != VP8_STATUS_OK) {
  475. return IDecError(idec, dec->status_);
  476. }
  477. idec->state_ = STATE_VP8L_DATA;
  478. return VP8_STATUS_OK;
  479. }
  480. static VP8StatusCode DecodeVP8LData(WebPIDecoder* const idec) {
  481. VP8LDecoder* const dec = (VP8LDecoder*)idec->dec_;
  482. const size_t curr_size = MemDataSize(&idec->mem_);
  483. assert(idec->is_lossless_);
  484. // Switch to incremental decoding if we don't have all the bytes available.
  485. dec->incremental_ = (curr_size < idec->chunk_size_);
  486. if (!VP8LDecodeImage(dec)) {
  487. return ErrorStatusLossless(idec, dec->status_);
  488. }
  489. assert(dec->status_ == VP8_STATUS_OK || dec->status_ == VP8_STATUS_SUSPENDED);
  490. return (dec->status_ == VP8_STATUS_SUSPENDED) ? dec->status_
  491. : FinishDecoding(idec);
  492. }
  493. // Main decoding loop
  494. static VP8StatusCode IDecode(WebPIDecoder* idec) {
  495. VP8StatusCode status = VP8_STATUS_SUSPENDED;
  496. if (idec->state_ == STATE_WEBP_HEADER) {
  497. status = DecodeWebPHeaders(idec);
  498. } else {
  499. if (idec->dec_ == NULL) {
  500. return VP8_STATUS_SUSPENDED; // can't continue if we have no decoder.
  501. }
  502. }
  503. if (idec->state_ == STATE_VP8_HEADER) {
  504. status = DecodeVP8FrameHeader(idec);
  505. }
  506. if (idec->state_ == STATE_VP8_PARTS0) {
  507. status = DecodePartition0(idec);
  508. }
  509. if (idec->state_ == STATE_VP8_DATA) {
  510. status = DecodeRemaining(idec);
  511. }
  512. if (idec->state_ == STATE_VP8L_HEADER) {
  513. status = DecodeVP8LHeader(idec);
  514. }
  515. if (idec->state_ == STATE_VP8L_DATA) {
  516. status = DecodeVP8LData(idec);
  517. }
  518. return status;
  519. }
  520. //------------------------------------------------------------------------------
  521. // Internal constructor
  522. static WebPIDecoder* NewDecoder(WebPDecBuffer* const output_buffer,
  523. const WebPBitstreamFeatures* const features) {
  524. WebPIDecoder* idec = (WebPIDecoder*)WebPSafeCalloc(1ULL, sizeof(*idec));
  525. if (idec == NULL) {
  526. return NULL;
  527. }
  528. idec->state_ = STATE_WEBP_HEADER;
  529. idec->chunk_size_ = 0;
  530. idec->last_mb_y_ = -1;
  531. InitMemBuffer(&idec->mem_);
  532. WebPInitDecBuffer(&idec->output_);
  533. VP8InitIo(&idec->io_);
  534. WebPResetDecParams(&idec->params_);
  535. if (output_buffer == NULL || WebPAvoidSlowMemory(output_buffer, features)) {
  536. idec->params_.output = &idec->output_;
  537. idec->final_output_ = output_buffer;
  538. if (output_buffer != NULL) {
  539. idec->params_.output->colorspace = output_buffer->colorspace;
  540. }
  541. } else {
  542. idec->params_.output = output_buffer;
  543. idec->final_output_ = NULL;
  544. }
  545. WebPInitCustomIo(&idec->params_, &idec->io_); // Plug the I/O functions.
  546. return idec;
  547. }
  548. //------------------------------------------------------------------------------
  549. // Public functions
  550. WebPIDecoder* WebPINewDecoder(WebPDecBuffer* output_buffer) {
  551. return NewDecoder(output_buffer, NULL);
  552. }
  553. WebPIDecoder* WebPIDecode(const uint8_t* data, size_t data_size,
  554. WebPDecoderConfig* config) {
  555. WebPIDecoder* idec;
  556. WebPBitstreamFeatures tmp_features;
  557. WebPBitstreamFeatures* const features =
  558. (config == NULL) ? &tmp_features : &config->input;
  559. memset(&tmp_features, 0, sizeof(tmp_features));
  560. // Parse the bitstream's features, if requested:
  561. if (data != NULL && data_size > 0) {
  562. if (WebPGetFeatures(data, data_size, features) != VP8_STATUS_OK) {
  563. return NULL;
  564. }
  565. }
  566. // Create an instance of the incremental decoder
  567. idec = (config != NULL) ? NewDecoder(&config->output, features)
  568. : NewDecoder(NULL, features);
  569. if (idec == NULL) {
  570. return NULL;
  571. }
  572. // Finish initialization
  573. if (config != NULL) {
  574. idec->params_.options = &config->options;
  575. }
  576. return idec;
  577. }
  578. void WebPIDelete(WebPIDecoder* idec) {
  579. if (idec == NULL) return;
  580. if (idec->dec_ != NULL) {
  581. if (!idec->is_lossless_) {
  582. if (idec->state_ == STATE_VP8_DATA) {
  583. // Synchronize the thread, clean-up and check for errors.
  584. VP8ExitCritical((VP8Decoder*)idec->dec_, &idec->io_);
  585. }
  586. VP8Delete((VP8Decoder*)idec->dec_);
  587. } else {
  588. VP8LDelete((VP8LDecoder*)idec->dec_);
  589. }
  590. }
  591. ClearMemBuffer(&idec->mem_);
  592. WebPFreeDecBuffer(&idec->output_);
  593. WebPSafeFree(idec);
  594. }
  595. //------------------------------------------------------------------------------
  596. // Wrapper toward WebPINewDecoder
  597. WebPIDecoder* WebPINewRGB(WEBP_CSP_MODE mode, uint8_t* output_buffer,
  598. size_t output_buffer_size, int output_stride) {
  599. const int is_external_memory = (output_buffer != NULL) ? 1 : 0;
  600. WebPIDecoder* idec;
  601. if (mode >= MODE_YUV) return NULL;
  602. if (is_external_memory == 0) { // Overwrite parameters to sane values.
  603. output_buffer_size = 0;
  604. output_stride = 0;
  605. } else { // A buffer was passed. Validate the other params.
  606. if (output_stride == 0 || output_buffer_size == 0) {
  607. return NULL; // invalid parameter.
  608. }
  609. }
  610. idec = WebPINewDecoder(NULL);
  611. if (idec == NULL) return NULL;
  612. idec->output_.colorspace = mode;
  613. idec->output_.is_external_memory = is_external_memory;
  614. idec->output_.u.RGBA.rgba = output_buffer;
  615. idec->output_.u.RGBA.stride = output_stride;
  616. idec->output_.u.RGBA.size = output_buffer_size;
  617. return idec;
  618. }
  619. WebPIDecoder* WebPINewYUVA(uint8_t* luma, size_t luma_size, int luma_stride,
  620. uint8_t* u, size_t u_size, int u_stride,
  621. uint8_t* v, size_t v_size, int v_stride,
  622. uint8_t* a, size_t a_size, int a_stride) {
  623. const int is_external_memory = (luma != NULL) ? 1 : 0;
  624. WebPIDecoder* idec;
  625. WEBP_CSP_MODE colorspace;
  626. if (is_external_memory == 0) { // Overwrite parameters to sane values.
  627. luma_size = u_size = v_size = a_size = 0;
  628. luma_stride = u_stride = v_stride = a_stride = 0;
  629. u = v = a = NULL;
  630. colorspace = MODE_YUVA;
  631. } else { // A luma buffer was passed. Validate the other parameters.
  632. if (u == NULL || v == NULL) return NULL;
  633. if (luma_size == 0 || u_size == 0 || v_size == 0) return NULL;
  634. if (luma_stride == 0 || u_stride == 0 || v_stride == 0) return NULL;
  635. if (a != NULL) {
  636. if (a_size == 0 || a_stride == 0) return NULL;
  637. }
  638. colorspace = (a == NULL) ? MODE_YUV : MODE_YUVA;
  639. }
  640. idec = WebPINewDecoder(NULL);
  641. if (idec == NULL) return NULL;
  642. idec->output_.colorspace = colorspace;
  643. idec->output_.is_external_memory = is_external_memory;
  644. idec->output_.u.YUVA.y = luma;
  645. idec->output_.u.YUVA.y_stride = luma_stride;
  646. idec->output_.u.YUVA.y_size = luma_size;
  647. idec->output_.u.YUVA.u = u;
  648. idec->output_.u.YUVA.u_stride = u_stride;
  649. idec->output_.u.YUVA.u_size = u_size;
  650. idec->output_.u.YUVA.v = v;
  651. idec->output_.u.YUVA.v_stride = v_stride;
  652. idec->output_.u.YUVA.v_size = v_size;
  653. idec->output_.u.YUVA.a = a;
  654. idec->output_.u.YUVA.a_stride = a_stride;
  655. idec->output_.u.YUVA.a_size = a_size;
  656. return idec;
  657. }
  658. WebPIDecoder* WebPINewYUV(uint8_t* luma, size_t luma_size, int luma_stride,
  659. uint8_t* u, size_t u_size, int u_stride,
  660. uint8_t* v, size_t v_size, int v_stride) {
  661. return WebPINewYUVA(luma, luma_size, luma_stride,
  662. u, u_size, u_stride,
  663. v, v_size, v_stride,
  664. NULL, 0, 0);
  665. }
  666. //------------------------------------------------------------------------------
  667. static VP8StatusCode IDecCheckStatus(const WebPIDecoder* const idec) {
  668. assert(idec);
  669. if (idec->state_ == STATE_ERROR) {
  670. return VP8_STATUS_BITSTREAM_ERROR;
  671. }
  672. if (idec->state_ == STATE_DONE) {
  673. return VP8_STATUS_OK;
  674. }
  675. return VP8_STATUS_SUSPENDED;
  676. }
  677. VP8StatusCode WebPIAppend(WebPIDecoder* idec,
  678. const uint8_t* data, size_t data_size) {
  679. VP8StatusCode status;
  680. if (idec == NULL || data == NULL) {
  681. return VP8_STATUS_INVALID_PARAM;
  682. }
  683. status = IDecCheckStatus(idec);
  684. if (status != VP8_STATUS_SUSPENDED) {
  685. return status;
  686. }
  687. // Check mixed calls between RemapMemBuffer and AppendToMemBuffer.
  688. if (!CheckMemBufferMode(&idec->mem_, MEM_MODE_APPEND)) {
  689. return VP8_STATUS_INVALID_PARAM;
  690. }
  691. // Append data to memory buffer
  692. if (!AppendToMemBuffer(idec, data, data_size)) {
  693. return VP8_STATUS_OUT_OF_MEMORY;
  694. }
  695. return IDecode(idec);
  696. }
  697. VP8StatusCode WebPIUpdate(WebPIDecoder* idec,
  698. const uint8_t* data, size_t data_size) {
  699. VP8StatusCode status;
  700. if (idec == NULL || data == NULL) {
  701. return VP8_STATUS_INVALID_PARAM;
  702. }
  703. status = IDecCheckStatus(idec);
  704. if (status != VP8_STATUS_SUSPENDED) {
  705. return status;
  706. }
  707. // Check mixed calls between RemapMemBuffer and AppendToMemBuffer.
  708. if (!CheckMemBufferMode(&idec->mem_, MEM_MODE_MAP)) {
  709. return VP8_STATUS_INVALID_PARAM;
  710. }
  711. // Make the memory buffer point to the new buffer
  712. if (!RemapMemBuffer(idec, data, data_size)) {
  713. return VP8_STATUS_INVALID_PARAM;
  714. }
  715. return IDecode(idec);
  716. }
  717. //------------------------------------------------------------------------------
  718. static const WebPDecBuffer* GetOutputBuffer(const WebPIDecoder* const idec) {
  719. if (idec == NULL || idec->dec_ == NULL) {
  720. return NULL;
  721. }
  722. if (idec->state_ <= STATE_VP8_PARTS0) {
  723. return NULL;
  724. }
  725. if (idec->final_output_ != NULL) {
  726. return NULL; // not yet slow-copied
  727. }
  728. return idec->params_.output;
  729. }
  730. const WebPDecBuffer* WebPIDecodedArea(const WebPIDecoder* idec,
  731. int* left, int* top,
  732. int* width, int* height) {
  733. const WebPDecBuffer* const src = GetOutputBuffer(idec);
  734. if (left != NULL) *left = 0;
  735. if (top != NULL) *top = 0;
  736. if (src != NULL) {
  737. if (width != NULL) *width = src->width;
  738. if (height != NULL) *height = idec->params_.last_y;
  739. } else {
  740. if (width != NULL) *width = 0;
  741. if (height != NULL) *height = 0;
  742. }
  743. return src;
  744. }
  745. uint8_t* WebPIDecGetRGB(const WebPIDecoder* idec, int* last_y,
  746. int* width, int* height, int* stride) {
  747. const WebPDecBuffer* const src = GetOutputBuffer(idec);
  748. if (src == NULL) return NULL;
  749. if (src->colorspace >= MODE_YUV) {
  750. return NULL;
  751. }
  752. if (last_y != NULL) *last_y = idec->params_.last_y;
  753. if (width != NULL) *width = src->width;
  754. if (height != NULL) *height = src->height;
  755. if (stride != NULL) *stride = src->u.RGBA.stride;
  756. return src->u.RGBA.rgba;
  757. }
  758. uint8_t* WebPIDecGetYUVA(const WebPIDecoder* idec, int* last_y,
  759. uint8_t** u, uint8_t** v, uint8_t** a,
  760. int* width, int* height,
  761. int* stride, int* uv_stride, int* a_stride) {
  762. const WebPDecBuffer* const src = GetOutputBuffer(idec);
  763. if (src == NULL) return NULL;
  764. if (src->colorspace < MODE_YUV) {
  765. return NULL;
  766. }
  767. if (last_y != NULL) *last_y = idec->params_.last_y;
  768. if (u != NULL) *u = src->u.YUVA.u;
  769. if (v != NULL) *v = src->u.YUVA.v;
  770. if (a != NULL) *a = src->u.YUVA.a;
  771. if (width != NULL) *width = src->width;
  772. if (height != NULL) *height = src->height;
  773. if (stride != NULL) *stride = src->u.YUVA.y_stride;
  774. if (uv_stride != NULL) *uv_stride = src->u.YUVA.u_stride;
  775. if (a_stride != NULL) *a_stride = src->u.YUVA.a_stride;
  776. return src->u.YUVA.y;
  777. }
  778. int WebPISetIOHooks(WebPIDecoder* const idec,
  779. VP8IoPutHook put,
  780. VP8IoSetupHook setup,
  781. VP8IoTeardownHook teardown,
  782. void* user_data) {
  783. if (idec == NULL || idec->state_ > STATE_WEBP_HEADER) {
  784. return 0;
  785. }
  786. idec->io_.put = put;
  787. idec->io_.setup = setup;
  788. idec->io_.teardown = teardown;
  789. idec->io_.opaque = user_data;
  790. return 1;
  791. }