OLD | NEW |
(Empty) | |
| 1 // Copyright 2010 Google Inc. |
| 2 // |
| 3 // This code is licensed under the same terms as WebM: |
| 4 // Software License Agreement: http://www.webmproject.org/license/software/ |
| 5 // Additional IP Rights Grant: http://www.webmproject.org/license/additional/ |
| 6 // ----------------------------------------------------------------------------- |
| 7 // |
| 8 // main entry for the decoder |
| 9 // |
| 10 // Author: Skal (pascal.massimino@gmail.com) |
| 11 |
| 12 #include <stdlib.h> |
| 13 #include "vp8i.h" |
| 14 |
| 15 //----------------------------------------------------------------------------- |
| 16 // VP8Decoder |
| 17 |
| 18 static void SetOk(VP8Decoder* const dec) { |
| 19 dec->status_ = 0; |
| 20 dec->error_msg_ = "OK"; |
| 21 } |
| 22 |
| 23 void VP8InitIo(VP8Io* const io) { |
| 24 if (io) { |
| 25 memset(io, 0, sizeof(*io)); |
| 26 } |
| 27 } |
| 28 |
| 29 VP8Decoder* VP8New() { |
| 30 VP8Decoder* dec = (VP8Decoder*)calloc(1, sizeof(VP8Decoder)); |
| 31 if (dec) { |
| 32 SetOk(dec); |
| 33 dec->ready_ = 0; |
| 34 } |
| 35 return dec; |
| 36 } |
| 37 |
| 38 int VP8Status(VP8Decoder* const dec) { |
| 39 if (!dec) return 2; |
| 40 return dec->status_; |
| 41 } |
| 42 |
| 43 const char* VP8StatusMessage(VP8Decoder* const dec) { |
| 44 if (!dec) return "no object"; |
| 45 if (!dec->error_msg_) return "OK"; |
| 46 return dec->error_msg_; |
| 47 } |
| 48 |
| 49 void VP8Delete(VP8Decoder* const dec) { |
| 50 if (dec) { |
| 51 VP8Clear(dec); |
| 52 free(dec); |
| 53 } |
| 54 } |
| 55 |
| 56 int VP8SetError(VP8Decoder* const dec, int error, const char *msg) { |
| 57 dec->status_ = error; |
| 58 dec->error_msg_ = msg; |
| 59 dec->ready_ = 0; |
| 60 return 0; |
| 61 } |
| 62 |
| 63 //----------------------------------------------------------------------------- |
| 64 // Header parsing |
| 65 |
| 66 static void ResetSegmentHeader(VP8SegmentHeader* const hdr) { |
| 67 assert(hdr); |
| 68 hdr->use_segment_ = 0; |
| 69 hdr->update_map_ = 0; |
| 70 hdr->absolute_delta_ = 1; |
| 71 memset(hdr->quantizer_, 0, sizeof(hdr->quantizer_)); |
| 72 memset(hdr->filter_strength_, 0, sizeof(hdr->filter_strength_)); |
| 73 } |
| 74 |
| 75 // Paragraph 9.3 |
| 76 static int ParseSegmentHeader(VP8BitReader* br, |
| 77 VP8SegmentHeader* hdr, VP8Proba* proba) { |
| 78 assert(br); |
| 79 assert(hdr); |
| 80 hdr->use_segment_ = VP8Get(br); |
| 81 if (hdr->use_segment_) { |
| 82 hdr->update_map_ = VP8Get(br); |
| 83 const int update_data = VP8Get(br); |
| 84 if (update_data) { |
| 85 hdr->absolute_delta_ = VP8Get(br); |
| 86 for (int s = 0; s < NUM_MB_SEGMENTS; ++s) { |
| 87 hdr->quantizer_[s] = VP8Get(br) ? VP8GetSignedValue(br, 7) : 0; |
| 88 } |
| 89 for (int s = 0; s < NUM_MB_SEGMENTS; ++s) { |
| 90 hdr->filter_strength_[s] = VP8Get(br) ? VP8GetSignedValue(br, 6) : 0; |
| 91 } |
| 92 } |
| 93 if (hdr->update_map_) { |
| 94 for (int s = 0; s < MB_FEATURE_TREE_PROBS; ++s) { |
| 95 proba->segments_[s] = VP8Get(br) ? VP8GetValue(br, 8) : 255u; |
| 96 } |
| 97 } |
| 98 } else { |
| 99 hdr->update_map_ = 0; |
| 100 } |
| 101 return 1; |
| 102 } |
| 103 |
| 104 // Paragraph 9.5 |
| 105 static int ParsePartitions(VP8Decoder* const dec, |
| 106 const uint8_t* buf, uint32_t size) { |
| 107 VP8BitReader* const br = &dec->br_; |
| 108 dec->num_parts_ = 1 << VP8GetValue(br, 2); |
| 109 const uint8_t* sz = buf; |
| 110 const int last_part = dec->num_parts_ - 1; |
| 111 uint32_t offset = last_part * 3; |
| 112 if (size <= offset) { |
| 113 return 0; |
| 114 } |
| 115 for (int p = 0; p < last_part; ++p) { |
| 116 const uint32_t psize = sz[0] | (sz[1] << 8) | (sz[2] << 16); |
| 117 if (offset + psize > size) { |
| 118 return 0; |
| 119 } |
| 120 VP8Init(dec->parts_ + p, buf + offset, psize); |
| 121 offset += psize; |
| 122 sz += 3; |
| 123 } |
| 124 size -= offset; |
| 125 VP8Init(dec->parts_ + last_part, buf + offset, size); |
| 126 return 1; |
| 127 } |
| 128 |
| 129 // Paragraph 9.4 |
| 130 static int ParseFilterHeader(VP8BitReader* br, VP8Decoder* const dec) { |
| 131 VP8FilterHeader* const hdr = &dec->filter_hdr_; |
| 132 hdr->simple_ = VP8Get(br); |
| 133 hdr->level_ = VP8GetValue(br, 6); |
| 134 hdr->sharpness_ = VP8GetValue(br, 3); |
| 135 hdr->use_lf_delta_ = VP8Get(br); |
| 136 if (hdr->use_lf_delta_) { |
| 137 if (VP8Get(br)) { // update lf-delta? |
| 138 for (int i = 0; i < NUM_REF_LF_DELTAS; ++i) { |
| 139 if (VP8Get(br)) { |
| 140 hdr->ref_lf_delta_[i] = VP8GetSignedValue(br, 6); |
| 141 } |
| 142 } |
| 143 for (int i = 0; i < NUM_MODE_LF_DELTAS; ++i) { |
| 144 if (VP8Get(br)) { |
| 145 hdr->mode_lf_delta_[i] = VP8GetSignedValue(br, 6); |
| 146 } |
| 147 } |
| 148 } |
| 149 } |
| 150 dec->filter_type_ = (hdr->level_ == 0) ? 0 : hdr->simple_ ? 1 : 2; |
| 151 if (dec->filter_type_ > 0) { // precompute filter levels per segment |
| 152 if (dec->segment_hdr_.use_segment_) { |
| 153 for (int s = 0; s < NUM_MB_SEGMENTS; ++s) { |
| 154 int strength = dec->segment_hdr_.filter_strength_[s]; |
| 155 if (!dec->segment_hdr_.absolute_delta_) { |
| 156 strength += hdr->level_; |
| 157 } |
| 158 dec->filter_levels_[s] = strength; |
| 159 } |
| 160 } else { |
| 161 dec->filter_levels_[0] = hdr->level_; |
| 162 } |
| 163 } |
| 164 return 1; |
| 165 } |
| 166 |
| 167 static inline uint32_t get_le32(const uint8_t* const data) { |
| 168 return data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24); |
| 169 } |
| 170 |
| 171 // Topmost call |
| 172 int VP8GetHeaders(VP8Decoder* const dec, VP8Io* const io) { |
| 173 if (dec == NULL) { |
| 174 return 0; |
| 175 } |
| 176 SetOk(dec); |
| 177 if (io == NULL || io->data == NULL || io->data_size <= 4) { |
| 178 return VP8SetError(dec, 2, "null VP8Io passed to VP8GetHeaders()"); |
| 179 } |
| 180 const uint8_t* buf = io->data; |
| 181 uint32_t buf_size = io->data_size; |
| 182 if (buf_size < 4) { |
| 183 return VP8SetError(dec, 2, "Not enough data to parse frame header"); |
| 184 } |
| 185 |
| 186 // Skip over valid RIFF headers |
| 187 if (!memcmp(buf, "RIFF", 4)) { |
| 188 if (buf_size < 20 + 4) { |
| 189 return VP8SetError(dec, 2, "RIFF: Truncated header."); |
| 190 } |
| 191 if (memcmp(buf + 8, "WEBP", 4)) { // wrong image file signature |
| 192 return VP8SetError(dec, 2, "RIFF: WEBP signature not found."); |
| 193 } |
| 194 const uint32_t riff_size = get_le32(buf + 4); |
| 195 if (memcmp(buf + 12, "VP8 ", 4)) { |
| 196 return VP8SetError(dec, 2, "RIFF: Invalid compression format."); |
| 197 } |
| 198 const uint32_t chunk_size = get_le32(buf + 16); |
| 199 if ((chunk_size > riff_size + 8) || (chunk_size & 1)) { |
| 200 return VP8SetError(dec, 2, "RIFF: Inconsistent size information."); |
| 201 } |
| 202 buf += 20; |
| 203 buf_size -= 20; |
| 204 } |
| 205 |
| 206 // Paragraph 9.1 |
| 207 VP8FrameHeader* const frm_hdr = &dec->frm_hdr_; |
| 208 const uint32_t bits = buf[0] | (buf[1] << 8) | (buf[2] << 16); |
| 209 frm_hdr->key_frame_ = !(bits & 1); |
| 210 frm_hdr->profile_ = (bits >> 1) & 7; |
| 211 frm_hdr->show_ = (bits >> 4) & 1; |
| 212 frm_hdr->partition_length_ = (bits >> 5); |
| 213 buf += 3; |
| 214 buf_size -= 3; |
| 215 |
| 216 VP8PictureHeader* const pic_hdr = &dec->pic_hdr_; |
| 217 if (frm_hdr->key_frame_) { |
| 218 // Paragraph 9.2 |
| 219 if (buf_size < 7) { |
| 220 return VP8SetError(dec, 2, "cannot parse picture header"); |
| 221 } |
| 222 if (buf[0] != 0x9d || buf[1] != 0x01 || buf[2] != 0x2a) { |
| 223 return VP8SetError(dec, 2, "Bad code word"); |
| 224 } |
| 225 pic_hdr->width_ = ((buf[4] << 8) | buf[3]) & 0x3fff; |
| 226 pic_hdr->xscale_ = buf[4] >> 6; // ratio: 1, 5/4 5/3 or 2 |
| 227 pic_hdr->height_ = ((buf[6] << 8) | buf[5]) & 0x3fff; |
| 228 pic_hdr->yscale_ = buf[6] >> 6; |
| 229 buf += 7; |
| 230 buf_size -= 7; |
| 231 |
| 232 dec->mb_w_ = (pic_hdr->width_ + 15) >> 4; |
| 233 dec->mb_h_ = (pic_hdr->height_ + 15) >> 4; |
| 234 io->width = pic_hdr->width_; |
| 235 io->height = pic_hdr->height_; |
| 236 |
| 237 VP8ResetProba(&dec->proba_); |
| 238 ResetSegmentHeader(&dec->segment_hdr_); |
| 239 dec->segment_ = 0; // default for intra |
| 240 } |
| 241 |
| 242 VP8BitReader* const br = &dec->br_; |
| 243 VP8Init(br, buf, buf_size); |
| 244 buf += frm_hdr->partition_length_; |
| 245 buf_size -= frm_hdr->partition_length_; |
| 246 if (frm_hdr->key_frame_) { |
| 247 pic_hdr->colorspace_ = VP8Get(br); |
| 248 pic_hdr->clamp_type_ = VP8Get(br); |
| 249 } |
| 250 if (!ParseSegmentHeader(br, &dec->segment_hdr_, &dec->proba_)) { |
| 251 return VP8SetError(dec, 2, "cannot parse segment header"); |
| 252 } |
| 253 // Filter specs |
| 254 if (!ParseFilterHeader(br, dec)) { |
| 255 return VP8SetError(dec, 2, "cannot parse filter header"); |
| 256 } |
| 257 if (!ParsePartitions(dec, buf, buf_size)) { |
| 258 return VP8SetError(dec, 2, "cannot parse partitions"); |
| 259 } |
| 260 |
| 261 // quantizer change |
| 262 VP8ParseQuant(dec); |
| 263 |
| 264 // Frame buffer marking |
| 265 if (!frm_hdr->key_frame_) { |
| 266 // Paragraph 9.7 |
| 267 #ifndef ONLY_KEYFRAME_CODE |
| 268 dec->buffer_flags_ = VP8Get(br) << 0; // update golden |
| 269 dec->buffer_flags_ |= VP8Get(br) << 1; // update alt ref |
| 270 if (!(dec->buffer_flags_ & 1)) { |
| 271 dec->buffer_flags_ |= VP8GetValue(br, 2) << 2; |
| 272 } |
| 273 if (!(dec->buffer_flags_ & 2)) { |
| 274 dec->buffer_flags_ |= VP8GetValue(br, 2) << 4; |
| 275 } |
| 276 dec->buffer_flags_ |= VP8Get(br) << 6; // sign bias golden |
| 277 dec->buffer_flags_ |= VP8Get(br) << 7; // sign bias alt ref |
| 278 #else |
| 279 return VP8SetError(dec, 2, "Not a key frame."); |
| 280 #endif |
| 281 } else { |
| 282 dec->buffer_flags_ = 0x003 | 0x100; |
| 283 } |
| 284 |
| 285 // Paragraph 9.8 |
| 286 dec->update_proba_ = VP8Get(br); |
| 287 if (!dec->update_proba_) { // save for later restore |
| 288 dec->proba_saved_ = dec->proba_; |
| 289 } |
| 290 |
| 291 #ifndef ONLY_KEYFRAME_CODE |
| 292 dec->buffer_flags_ &= 1 << 8; |
| 293 dec->buffer_flags_ |= |
| 294 (frm_hdr->key_frame_ || VP8Get(br)) << 8; // refresh last frame |
| 295 #endif |
| 296 |
| 297 VP8ParseProba(br, dec); |
| 298 |
| 299 // sanitized state |
| 300 dec->ready_ = 1; |
| 301 return 1; |
| 302 } |
| 303 |
| 304 //----------------------------------------------------------------------------- |
| 305 // Residual decoding (Paragraph 13.2 / 13.3) |
| 306 |
| 307 static const uint8_t kBands[16 + 1] = { |
| 308 0, 1, 2, 3, 6, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7, |
| 309 0 // extra entry as sentinel |
| 310 }; |
| 311 |
| 312 static const uint8_t kCat3[] = {173, 148, 140, 0}; |
| 313 static const uint8_t kCat4[] = {176, 155, 140, 135, 0}; |
| 314 static const uint8_t kCat5[] = {180, 157, 141, 134, 130, 0}; |
| 315 static const uint8_t kCat6[] = |
| 316 {254, 254, 243, 230, 196, 177, 153, 140, 133, 130, 129, 0}; |
| 317 static const uint8_t * const kCat3456[] = { kCat3, kCat4, kCat5, kCat6 }; |
| 318 static const uint8_t kZigzag[16] = { |
| 319 0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15 |
| 320 }; |
| 321 |
| 322 typedef const uint8_t PROBA_ARRAY[NUM_CTX][NUM_PROBAS]; |
| 323 |
| 324 static int GetCoeffs(VP8BitReader* const br, |
| 325 const uint8_t (*prob)[NUM_CTX][NUM_PROBAS], |
| 326 int ctx, const uint16_t dq[2], int n, int16_t* out) { |
| 327 const uint8_t* p = prob[kBands[n]][ctx]; |
| 328 if (!VP8GetBit(br, p[0])) { // first EOB is more a 'CBP' bit. |
| 329 return -1; |
| 330 } |
| 331 while (1) { |
| 332 ++n; |
| 333 if (!VP8GetBit(br, p[1])) { |
| 334 p = prob[kBands[n]][0]; |
| 335 } else { // non zero coeff |
| 336 int v; |
| 337 if (!VP8GetBit(br, p[2])) { |
| 338 p = prob[kBands[n]][1]; |
| 339 v = 1; |
| 340 } else { |
| 341 if (!VP8GetBit(br, p[3])) { |
| 342 if (!VP8GetBit(br, p[4])) { |
| 343 v = 2; |
| 344 } else { |
| 345 v = 3 + VP8GetBit(br, p[5]); |
| 346 } |
| 347 } else { |
| 348 if (!VP8GetBit(br, p[6])) { |
| 349 if (!VP8GetBit(br, p[7])) { |
| 350 v = 5 + VP8GetBit(br, 159); |
| 351 } else { |
| 352 v = 7 + 2 * VP8GetBit(br, 165) + VP8GetBit(br, 145); |
| 353 } |
| 354 } else { |
| 355 const int bit1 = VP8GetBit(br, p[8]); |
| 356 const int bit0 = VP8GetBit(br, p[9 + bit1]); |
| 357 const int cat = 2 * bit1 + bit0; |
| 358 v = 0; |
| 359 for (const uint8_t* tab = kCat3456[cat]; *tab; ++tab) { |
| 360 v += v + VP8GetBit(br, *tab); |
| 361 } |
| 362 v += 3 + (8 << cat); |
| 363 } |
| 364 } |
| 365 p = prob[kBands[n]][2]; |
| 366 } |
| 367 const int j = kZigzag[n - 1]; |
| 368 out[j] = VP8GetSigned(br, v) * dq[j > 0]; |
| 369 if (n == 16) break; |
| 370 if (!VP8GetBit(br, p[0])) { // EOB |
| 371 return n; |
| 372 } |
| 373 } |
| 374 } |
| 375 return 15; |
| 376 } |
| 377 |
| 378 // Table to unpack four bits into four bytes |
| 379 static const uint8_t kUnpackTab[16][4] = { |
| 380 {0, 0, 0, 0}, {1, 0, 0, 0}, {0, 1, 0, 0}, {1, 1, 0, 0}, |
| 381 {0, 0, 1, 0}, {1, 0, 1, 0}, {0, 1, 1, 0}, {1, 1, 1, 0}, |
| 382 {0, 0, 0, 1}, {1, 0, 0, 1}, {0, 1, 0, 1}, {1, 1, 0, 1}, |
| 383 {0, 0, 1, 1}, {1, 0, 1, 1}, {0, 1, 1, 1}, {1, 1, 1, 1} }; |
| 384 |
| 385 // Macro to pack four LSB of four bytes into four bits. |
| 386 #define PACK(X, S) \ |
| 387 ((((*(uint32_t*)(X)) * 0x01020408U) & 0xff000000) >> (S)) |
| 388 |
| 389 typedef const uint8_t (*Proba_t)[NUM_CTX][NUM_PROBAS]; // for const-casting |
| 390 static int ParseResiduals(VP8Decoder* const dec, |
| 391 VP8MB* const mb, VP8BitReader* const token_br) { |
| 392 int out_t_nz, out_l_nz, first; |
| 393 Proba_t ac_prob; |
| 394 const VP8QuantMatrix* q = &dec->dqm_[dec->segment_]; |
| 395 int16_t* dst = dec->coeffs_; |
| 396 VP8MB* const left_mb = dec->mb_info_ - 1; |
| 397 memset(dst, 0, 384 * sizeof(*dst)); |
| 398 if (!dec->is_i4x4_) { // parse DC |
| 399 int16_t dc[16] = { 0 }; |
| 400 const int ctx = mb->dc_nz_ + left_mb->dc_nz_; |
| 401 const int last = GetCoeffs(token_br, (Proba_t)dec->proba_.coeffs_[1], |
| 402 ctx, q->y2_mat_, 0, dc); |
| 403 mb->dc_nz_ = left_mb->dc_nz_ = (last >= 0); |
| 404 first = 1; |
| 405 ac_prob = (Proba_t)dec->proba_.coeffs_[0]; |
| 406 VP8TransformWHT(dc, dst); |
| 407 } else { |
| 408 first = 0; |
| 409 ac_prob = (Proba_t)dec->proba_.coeffs_[3]; |
| 410 } |
| 411 |
| 412 uint8_t nz_ac[4], nz_dc[4]; |
| 413 uint32_t non_zero_ac = 0; |
| 414 uint32_t non_zero_dc = 0; |
| 415 |
| 416 uint8_t tnz[4], lnz[4]; |
| 417 memcpy(tnz, kUnpackTab[mb->nz_ & 0xf], sizeof(tnz)); |
| 418 memcpy(lnz, kUnpackTab[left_mb->nz_ & 0xf], sizeof(lnz)); |
| 419 for (int y = 0; y < 4; ++y) { |
| 420 int l = lnz[y]; |
| 421 for (int x = 0; x < 4; ++x) { |
| 422 const int ctx = l + tnz[x]; |
| 423 const int last = GetCoeffs(token_br, ac_prob, ctx, |
| 424 q->y1_mat_, first, dst); |
| 425 nz_dc[x] = (dst[0] != 0); |
| 426 nz_ac[x] = (last > 0); |
| 427 tnz[x] = l = (last >= 0); |
| 428 dst += 16; |
| 429 } |
| 430 lnz[y] = l; |
| 431 non_zero_dc |= PACK(nz_dc, 24 - y * 4); |
| 432 non_zero_ac |= PACK(nz_ac, 24 - y * 4); |
| 433 } |
| 434 out_t_nz = PACK(tnz, 24); |
| 435 out_l_nz = PACK(lnz, 24); |
| 436 |
| 437 memcpy(tnz, kUnpackTab[mb->nz_ >> 4], sizeof(tnz)); |
| 438 memcpy(lnz, kUnpackTab[left_mb->nz_ >> 4], sizeof(lnz)); |
| 439 for (int ch = 0; ch < 4; ch += 2) { |
| 440 for (int y = 0; y < 2; ++y) { |
| 441 int l = lnz[ch + y]; |
| 442 for (int x = 0; x < 2; ++x) { |
| 443 const int ctx = l + tnz[ch + x]; |
| 444 const int last = |
| 445 GetCoeffs(token_br, (Proba_t)dec->proba_.coeffs_[2], |
| 446 ctx, q->uv_mat_, 0, dst); |
| 447 nz_dc[y * 2 + x] = (dst[0] != 0); |
| 448 nz_ac[y * 2 + x] = (last > 0); |
| 449 tnz[ch + x] = l = (last >= 0); |
| 450 dst += 16; |
| 451 } |
| 452 lnz[ch + y] = l; |
| 453 } |
| 454 non_zero_dc |= PACK(nz_dc, 8 - ch * 2); |
| 455 non_zero_ac |= PACK(nz_ac, 8 - ch * 2); |
| 456 } |
| 457 out_t_nz |= PACK(tnz, 20); |
| 458 out_l_nz |= PACK(lnz, 20); |
| 459 mb->nz_ = out_t_nz; |
| 460 left_mb->nz_ = out_l_nz; |
| 461 |
| 462 dec->non_zero_ac_ = non_zero_ac; |
| 463 dec->non_zero_ = non_zero_ac | non_zero_dc; |
| 464 mb->skip_ = !dec->non_zero_; |
| 465 |
| 466 return 1; |
| 467 } |
| 468 #undef PACK |
| 469 |
| 470 //----------------------------------------------------------------------------- |
| 471 // Main loop |
| 472 |
| 473 static void SendBlock(VP8Decoder* const dec, VP8Io* io) { |
| 474 if (io->put) { |
| 475 io->mb_x = dec->mb_x_ * 16; |
| 476 io->mb_y = dec->mb_y_ * 16; |
| 477 io->mb_w = io->width - io->mb_x; |
| 478 io->mb_h = io->height - io->mb_y; |
| 479 if (io->mb_w > 16) io->mb_w = 16; |
| 480 if (io->mb_h > 16) io->mb_h = 16; |
| 481 io->put(io); |
| 482 } |
| 483 } |
| 484 |
| 485 static int ParseFrame(VP8Decoder* const dec, VP8Io* io) { |
| 486 int ok = 1; |
| 487 VP8BitReader* const br = &dec->br_; |
| 488 |
| 489 for (dec->mb_y_ = 0; dec->mb_y_ < dec->mb_h_; ++dec->mb_y_) { |
| 490 memset(dec->intra_l_, B_DC_PRED, sizeof(dec->intra_l_)); |
| 491 VP8MB* const left = dec->mb_info_ - 1; |
| 492 left->nz_ = 0; |
| 493 left->dc_nz_ = 0; |
| 494 VP8BitReader* token_br = &dec->parts_[dec->mb_y_ & (dec->num_parts_ - 1)]; |
| 495 for (dec->mb_x_ = 0; dec->mb_x_ < dec->mb_w_; dec->mb_x_++) { |
| 496 VP8MB* const info = dec->mb_info_ + dec->mb_x_; |
| 497 |
| 498 // Note: we don't save segment map (yet), as we don't expect |
| 499 // to decode more than 1 keyframe. |
| 500 if (dec->segment_hdr_.update_map_) { |
| 501 // Hardcoded tree parsing |
| 502 dec->segment_ = !VP8GetBit(br, dec->proba_.segments_[0]) ? |
| 503 VP8GetBit(br, dec->proba_.segments_[1]) : |
| 504 2 + VP8GetBit(br, dec->proba_.segments_[2]); |
| 505 } |
| 506 info->skip_ = dec->use_skip_proba_ ? VP8GetBit(br, dec->skip_p_) : 0; |
| 507 |
| 508 VP8ParseIntraMode(br, dec); |
| 509 |
| 510 if (!info->skip_) { |
| 511 if (!ParseResiduals(dec, info, token_br)) { |
| 512 ok = 0; |
| 513 break; |
| 514 } |
| 515 } else { |
| 516 left->nz_ = info->nz_ = 0; |
| 517 if (!dec->is_i4x4_) { |
| 518 left->dc_nz_ = info->dc_nz_ = 0; |
| 519 } |
| 520 dec->non_zero_ = 0; |
| 521 dec->non_zero_ac_ = 0; |
| 522 } |
| 523 VP8ReconstructBlock(dec); |
| 524 |
| 525 // Store filter params |
| 526 if (dec->filter_type_ > 0) { |
| 527 VP8StoreBlock(dec); |
| 528 } else { // We're done. Send block to user at once. |
| 529 SendBlock(dec, io); |
| 530 } |
| 531 } |
| 532 if (!ok) { |
| 533 break; |
| 534 } |
| 535 if (dec->filter_type_ > 0) { // filter a row |
| 536 VP8FilterRow(dec, io); |
| 537 } |
| 538 if (dec->br_.eof_ || token_br->eof_) { |
| 539 ok = 0; |
| 540 break; |
| 541 } |
| 542 } |
| 543 |
| 544 // Finish |
| 545 if (!dec->update_proba_) { |
| 546 dec->proba_ = dec->proba_saved_; |
| 547 } |
| 548 return ok; |
| 549 } |
| 550 |
| 551 // Main entry point |
| 552 int VP8Decode(VP8Decoder* const dec, VP8Io* const io) { |
| 553 if (dec == NULL) { |
| 554 return 0; |
| 555 } |
| 556 if (io == NULL) { |
| 557 return VP8SetError(dec, 2, "NULL VP8Io parameter in VP8Decode()."); |
| 558 } |
| 559 |
| 560 if (!dec->ready_) { |
| 561 if (!VP8GetHeaders(dec, io)) { |
| 562 return 0; |
| 563 } |
| 564 } |
| 565 assert(dec->ready_); |
| 566 |
| 567 // will allocate memory and prepare everything. |
| 568 if (!VP8InitFrame(dec, io)) { |
| 569 VP8Clear(dec); |
| 570 return VP8SetError(dec, 3, "Allocation failed"); |
| 571 } |
| 572 |
| 573 // set-up |
| 574 if (io->setup) io->setup(io); |
| 575 |
| 576 // Main decoding loop |
| 577 if (!ParseFrame(dec, io)) { |
| 578 VP8Clear(dec); |
| 579 return VP8SetError(dec, 3, "Frame decoding failed"); |
| 580 } |
| 581 |
| 582 // tear-down |
| 583 if (io->teardown) io->teardown(io); |
| 584 |
| 585 dec->ready_ = 0; |
| 586 return 1; |
| 587 } |
| 588 |
| 589 void VP8Clear(VP8Decoder* const dec) { |
| 590 if (dec == NULL) { |
| 591 return; |
| 592 } |
| 593 if (dec->mem_) { |
| 594 free(dec->mem_); |
| 595 } |
| 596 dec->mem_ = NULL; |
| 597 dec->mem_size_ = 0; |
| 598 memset(&dec->br_, 0, sizeof(dec->br_)); |
| 599 dec->ready_ = 0; |
| 600 } |
OLD | NEW |