idec.c 28 KB

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