dboolhuff.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "dboolhuff.h"
  11. #include "vp8/common/common.h"
  12. #include "vpx_dsp/vpx_dsp_common.h"
  13. int vp8dx_start_decode(BOOL_DECODER *br, const unsigned char *source,
  14. unsigned int source_sz, vpx_decrypt_cb decrypt_cb,
  15. void *decrypt_state) {
  16. br->user_buffer_end = source + source_sz;
  17. br->user_buffer = source;
  18. br->value = 0;
  19. br->count = -8;
  20. br->range = 255;
  21. br->decrypt_cb = decrypt_cb;
  22. br->decrypt_state = decrypt_state;
  23. if (source_sz && !source) return 1;
  24. /* Populate the buffer */
  25. vp8dx_bool_decoder_fill(br);
  26. return 0;
  27. }
  28. void vp8dx_bool_decoder_fill(BOOL_DECODER *br) {
  29. const unsigned char *bufptr = br->user_buffer;
  30. VP8_BD_VALUE value = br->value;
  31. int count = br->count;
  32. int shift = VP8_BD_VALUE_SIZE - CHAR_BIT - (count + CHAR_BIT);
  33. size_t bytes_left = br->user_buffer_end - bufptr;
  34. size_t bits_left = bytes_left * CHAR_BIT;
  35. int x = shift + CHAR_BIT - (int)bits_left;
  36. int loop_end = 0;
  37. unsigned char decrypted[sizeof(VP8_BD_VALUE) + 1];
  38. if (br->decrypt_cb) {
  39. size_t n = VPXMIN(sizeof(decrypted), bytes_left);
  40. br->decrypt_cb(br->decrypt_state, bufptr, decrypted, (int)n);
  41. bufptr = decrypted;
  42. }
  43. if (x >= 0) {
  44. count += VP8_LOTS_OF_BITS;
  45. loop_end = x;
  46. }
  47. if (x < 0 || bits_left) {
  48. while (shift >= loop_end) {
  49. count += CHAR_BIT;
  50. value |= (VP8_BD_VALUE)*bufptr << shift;
  51. ++bufptr;
  52. ++br->user_buffer;
  53. shift -= CHAR_BIT;
  54. }
  55. }
  56. br->value = value;
  57. br->count = count;
  58. }