bitpack.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* SPDX-License-Identifier: LGPL-2.1 OR BSD-3-Clause */
  2. /********************************************************************
  3. Copyright (C) 2002-2009 Xiph.org Foundation
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. - Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. - Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. - Neither the name of the Xiph.org Foundation nor the names of its
  13. contributors may be used to endorse or promote products derived from
  14. this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION
  19. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. ********************************************************************/
  27. #if !defined(_bitpack_H)
  28. # define _bitpack_H (1)
  29. # include <stddef.h>
  30. # include <limits.h>
  31. #include "stdint.h"
  32. #include "codec.h"
  33. typedef uint16_t oc_quant_table[64];
  34. typedef size_t oc_pb_window;
  35. typedef struct oc_pack_buf oc_pack_buf;
  36. /*Custom bitpacker implementations.*/
  37. # if defined(OC_ARM_ASM)
  38. # include "arm/armbits.h"
  39. # endif
  40. # if !defined(oc_pack_read)
  41. # define oc_pack_read oc_pack_read_c
  42. # endif
  43. # if !defined(oc_pack_read1)
  44. # define oc_pack_read1 oc_pack_read1_c
  45. # endif
  46. # if !defined(oc_huff_token_decode)
  47. # define oc_huff_token_decode oc_huff_token_decode_c
  48. # endif
  49. /*Maximum scaled quantizer value.*/
  50. #define OC_QUANT_MAX (1024<<2)
  51. #define OC_PB_WINDOW_SIZE ((int32_t)sizeof(oc_pb_window)*CHAR_BIT)
  52. #define OC_LOTS_OF_BITS (0x40000000)
  53. #define OC_DCT_VAL_RANGE (580)
  54. #define OC_NDCT_TOKEN_BITS (5)
  55. #define OC_DCT_EOB1_TOKEN (0)
  56. #define OC_DCT_EOB2_TOKEN (1)
  57. #define OC_DCT_EOB3_TOKEN (2)
  58. #define OC_DCT_REPEAT_RUN0_TOKEN (3)
  59. #define OC_DCT_REPEAT_RUN1_TOKEN (4)
  60. #define OC_DCT_REPEAT_RUN2_TOKEN (5)
  61. #define OC_DCT_REPEAT_RUN3_TOKEN (6)
  62. #define OC_DCT_SHORT_ZRL_TOKEN (7)
  63. #define OC_DCT_ZRL_TOKEN (8)
  64. #define OC_ONE_TOKEN (9)
  65. #define OC_MINUS_ONE_TOKEN (10)
  66. #define OC_TWO_TOKEN (11)
  67. #define OC_MINUS_TWO_TOKEN (12)
  68. #define OC_DCT_VAL_CAT2 (13)
  69. #define OC_DCT_VAL_CAT3 (17)
  70. #define OC_DCT_VAL_CAT4 (18)
  71. #define OC_DCT_VAL_CAT5 (19)
  72. #define OC_DCT_VAL_CAT6 (20)
  73. #define OC_DCT_VAL_CAT7 (21)
  74. #define OC_DCT_VAL_CAT8 (22)
  75. #define OC_DCT_RUN_CAT1A (23)
  76. #define OC_DCT_RUN_CAT1B (28)
  77. #define OC_DCT_RUN_CAT1C (29)
  78. #define OC_DCT_RUN_CAT2A (30)
  79. #define OC_DCT_RUN_CAT2B (31)
  80. #define OC_NDCT_EOB_TOKEN_MAX (7)
  81. #define OC_NDCT_ZRL_TOKEN_MAX (9)
  82. #define OC_NDCT_VAL_MAX (23)
  83. #define OC_NDCT_VAL_CAT1_MAX (13)
  84. #define OC_NDCT_VAL_CAT2_MAX (17)
  85. #define OC_NDCT_VAL_CAT2_SIZE (OC_NDCT_VAL_CAT2_MAX-OC_DCT_VAL_CAT2)
  86. #define OC_NDCT_RUN_MAX (32)
  87. #define OC_NDCT_RUN_CAT1A_MAX (28)
  88. #define TH_NHUFFMAN_TABLES (80)
  89. #define TH_NDCT_TOKENS (32)
  90. struct oc_pack_buf
  91. {
  92. const unsigned char *stop;
  93. const unsigned char *ptr;
  94. oc_pb_window window;
  95. int32_t bits;
  96. int32_t eof;
  97. };
  98. /**A Huffman code for a Theora DCT token.
  99. * Each set of Huffman codes in a given table must form a complete, prefix-free
  100. * code.
  101. * There is no requirement that all the tokens in a table have a valid code,
  102. * but the current encoder is not optimized to take advantage of this.
  103. * If each of the five grouops of 16 tables does not contain at least one table
  104. * with a code for every token, then the encoder may fail to encode certain
  105. * frames.
  106. * The complete table in the first group of 16 does not have to be in the same
  107. * place as the complete table in the other groups, but the complete tables in
  108. * the remaining four groups must all be in the same place.*/
  109. typedef struct
  110. {
  111. /**The bit pattern for the code, with the LSbit of the pattern aligned in
  112. * the LSbit of the word.*/
  113. uint32_t pattern;
  114. /**The number of bits in the code.
  115. * This must be between 0 and 32, inclusive.*/
  116. int32_t nbits;
  117. }th_huff_code;
  118. extern const unsigned char OC_DCT_TOKEN_EXTRA_BITS[TH_NDCT_TOKENS];
  119. // Huffman decoder
  120. int32_t oc_huff_trees_unpack(oc_pack_buf *_opb,int16_t *_nodes[TH_NHUFFMAN_TABLES]);
  121. int32_t oc_huff_trees_copy(int16_t *_dst[TH_NHUFFMAN_TABLES], const int16_t *const _src[TH_NHUFFMAN_TABLES]);
  122. void oc_huff_trees_clear(int16_t *_nodes[TH_NHUFFMAN_TABLES]);
  123. int32_t oc_huff_token_decode_c(oc_pack_buf *_opb,const int16_t *_node);
  124. // pack read
  125. void oc_pack_readinit(oc_pack_buf *_b,unsigned char *_buf,long _bytes);
  126. int32_t oc_pack_look1(oc_pack_buf *_b);
  127. void oc_pack_adv1(oc_pack_buf *_b);
  128. long oc_pack_read_c(oc_pack_buf *_b,int32_t _bits);
  129. int32_t oc_pack_read1_c(oc_pack_buf *_b);
  130. long oc_pack_bytes_left(oc_pack_buf *_b);
  131. void oc_dequant_tables_init(uint16_t *_dequant[64][3][2], int32_t _pp_dc_scale[64],const th_quant_info *_qinfo);
  132. int32_t oc_quant_params_unpack(oc_pack_buf *_opb, th_quant_info *_qinfo);
  133. void oc_quant_params_clear(th_quant_info *_qinfo);
  134. #endif