You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

235 lines
7.2KB

  1. /*
  2. * JPEG2000 tables
  3. * Copyright (c) 2007 Kamil Nowosad
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #ifndef AVCODEC_J2K_H
  22. #define AVCODEC_J2K_H
  23. /**
  24. * JPEG2000 tables
  25. * @file
  26. * @author Kamil Nowosad
  27. */
  28. #include "mqc.h"
  29. #include "j2k_dwt.h"
  30. enum J2kMarkers{
  31. J2K_SOC = 0xff4f, ///< start of codestream
  32. J2K_SIZ = 0xff51, ///< image and tile size
  33. J2K_COD, ///< coding style default
  34. J2K_COC, ///< coding style component
  35. J2K_TLM = 0xff55, ///< packed packet headers, tile-part header
  36. J2K_PLM = 0xff57, ///< tile-part lengths
  37. J2K_PLT, ///< packet length, main header
  38. J2K_QCD = 0xff5c, ///< quantization default
  39. J2K_QCC, ///< quantization component
  40. J2K_RGN, ///< region of interest
  41. J2K_POC, ///< progression order change
  42. J2K_PPM, ///< packet length, tile-part header
  43. J2K_PPT, ///< packed packet headers, main header
  44. J2K_CRG = 0xff63, ///< component registration
  45. J2K_COM, ///< comment
  46. J2K_SOT = 0xff90, ///< start of tile-part
  47. J2K_SOP, ///< start of packet
  48. J2K_EPH, ///< end of packet header
  49. J2K_SOD, ///< start of data
  50. J2K_EOC = 0xffd9, ///< end of codestream
  51. };
  52. enum J2kQuantsty{ ///< quantization style
  53. J2K_QSTY_NONE, ///< no quantization
  54. J2K_QSTY_SI, ///< scalar derived
  55. J2K_QSTY_SE ///< scalar expoounded
  56. };
  57. #define J2K_MAX_CBLKW 64
  58. #define J2K_MAX_CBLKH 64
  59. // T1 flags
  60. // flags determining significance of neighbour coefficients
  61. #define J2K_T1_SIG_N 0x0001
  62. #define J2K_T1_SIG_E 0x0002
  63. #define J2K_T1_SIG_W 0x0004
  64. #define J2K_T1_SIG_S 0x0008
  65. #define J2K_T1_SIG_NE 0x0010
  66. #define J2K_T1_SIG_NW 0x0020
  67. #define J2K_T1_SIG_SE 0x0040
  68. #define J2K_T1_SIG_SW 0x0080
  69. #define J2K_T1_SIG_NB (J2K_T1_SIG_N | J2K_T1_SIG_E | J2K_T1_SIG_S | J2K_T1_SIG_W \
  70. |J2K_T1_SIG_NE | J2K_T1_SIG_NW | J2K_T1_SIG_SE | J2K_T1_SIG_SW)
  71. // flags determining sign bit of neighbour coefficients
  72. #define J2K_T1_SGN_N 0x0100
  73. #define J2K_T1_SGN_S 0x0200
  74. #define J2K_T1_SGN_W 0x0400
  75. #define J2K_T1_SGN_E 0x0800
  76. #define J2K_T1_VIS 0x1000
  77. #define J2K_T1_SIG 0x2000
  78. #define J2K_T1_REF 0x4000
  79. #define J2K_T1_SGN 0x8000
  80. // Codeblock coding styles
  81. #define J2K_CBLK_BYPASS 0x01 // Selective arithmetic coding bypass
  82. #define J2K_CBLK_RESET 0x02 // Reset context probabilities
  83. #define J2K_CBLK_TERMALL 0x04 // Terminate after each coding pass
  84. #define J2K_CBLK_VSC 0x08 // Vertical stripe causal context formation
  85. #define J2K_CBLK_PREDTERM 0x10 // Predictable termination
  86. #define J2K_CBLK_SEGSYM 0x20 // Segmentation symbols present
  87. // Coding styles
  88. #define J2K_CSTY_PREC 0x01 // Precincts defined in coding style
  89. #define J2K_CSTY_SOP 0x02 // SOP marker present
  90. #define J2K_CSTY_EPH 0x04 // EPH marker present
  91. typedef struct {
  92. int data[J2K_MAX_CBLKW][J2K_MAX_CBLKH];
  93. int flags[J2K_MAX_CBLKW+2][J2K_MAX_CBLKH+2];
  94. MqcState mqc;
  95. } J2kT1Context;
  96. typedef struct J2kTgtNode {
  97. uint8_t val;
  98. uint8_t vis;
  99. struct J2kTgtNode *parent;
  100. } J2kTgtNode;
  101. typedef struct {
  102. uint8_t nreslevels; ///< number of resolution levels
  103. uint8_t log2_cblk_width,
  104. log2_cblk_height; ///< exponent of codeblock size
  105. uint8_t transform; ///< DWT type
  106. uint8_t csty; ///< coding style
  107. uint8_t log2_prec_width,
  108. log2_prec_height; ///< precinct size
  109. uint8_t nlayers; ///< number of layers
  110. uint8_t mct; ///< multiple component transformation
  111. uint8_t cblk_style; ///< codeblock coding style
  112. } J2kCodingStyle;
  113. typedef struct {
  114. uint8_t expn[32 * 3]; ///< quantization exponent
  115. uint16_t mant[32 * 3]; ///< quantization mantissa
  116. uint8_t quantsty; ///< quantization style
  117. uint8_t nguardbits; ///< number of guard bits
  118. } J2kQuantStyle;
  119. typedef struct {
  120. uint16_t rate;
  121. int64_t disto;
  122. } J2kPass;
  123. typedef struct {
  124. uint8_t npasses;
  125. uint8_t ninclpasses; ///< number coding of passes included in codestream
  126. uint8_t nonzerobits;
  127. uint16_t length;
  128. uint16_t lengthinc;
  129. uint8_t lblock;
  130. uint8_t zero;
  131. uint8_t data[8192];
  132. J2kPass passes[100];
  133. } J2kCblk; ///< code block
  134. typedef struct {
  135. uint16_t xi0, xi1, yi0, yi1; ///< codeblock indexes ([xi0, xi1))
  136. J2kTgtNode *zerobits;
  137. J2kTgtNode *cblkincl;
  138. } J2kPrec; ///< precinct
  139. typedef struct {
  140. uint16_t coord[2][2]; ///< border coordinates {{x0, x1}, {y0, y1}}
  141. uint16_t codeblock_width, codeblock_height;
  142. uint16_t cblknx, cblkny;
  143. uint32_t stepsize; ///< quantization stepsize (* 2^13)
  144. J2kPrec *prec;
  145. J2kCblk *cblk;
  146. } J2kBand; ///< subband
  147. typedef struct {
  148. uint8_t nbands;
  149. uint16_t coord[2][2]; ///< border coordinates {{x0, x1}, {y0, y1}}
  150. uint16_t num_precincts_x, num_precincts_y; ///< number of precincts in x/y direction
  151. uint8_t log2_prec_width, log2_prec_height; ///< exponent of precinct size
  152. J2kBand *band;
  153. } J2kResLevel; ///< resolution level
  154. typedef struct {
  155. J2kResLevel *reslevel;
  156. DWTContext dwt;
  157. int *data;
  158. uint16_t coord[2][2]; ///< border coordinates {{x0, x1}, {y0, y1}}
  159. } J2kComponent;
  160. /* debug routines */
  161. #if 0
  162. #undef fprintf
  163. #undef printf
  164. void ff_j2k_printv(int *tab, int l);
  165. void ff_j2k_printu(uint8_t *tab, int l);
  166. #endif
  167. /* misc tools */
  168. static inline int ff_j2k_ceildivpow2(int a, int b)
  169. {
  170. return (a + (1 << b) - 1)>> b;
  171. }
  172. static inline int ff_j2k_ceildiv(int a, int b)
  173. {
  174. return (a + b - 1) / b;
  175. }
  176. /* tag tree routines */
  177. J2kTgtNode *ff_j2k_tag_tree_init(int w, int h);
  178. /* TIER-1 routines */
  179. void ff_j2k_init_tier1_luts(void);
  180. void ff_j2k_set_significant(J2kT1Context *t1, int x, int y, int negative);
  181. extern uint8_t ff_j2k_nbctxno_lut[256][4];
  182. static inline int ff_j2k_getnbctxno(int flag, int bandno, int vert_causal_ctx_csty_symbol)
  183. {
  184. return ff_j2k_nbctxno_lut[flag&255][bandno];
  185. }
  186. static inline int ff_j2k_getrefctxno(int flag)
  187. {
  188. static const uint8_t refctxno_lut[2][2] = {{14, 15}, {16, 16}};
  189. return refctxno_lut[(flag>>14)&1][(flag & 255) != 0];
  190. }
  191. extern uint8_t ff_j2k_sgnctxno_lut[16][16], ff_j2k_xorbit_lut[16][16];
  192. static inline int ff_j2k_getsgnctxno(int flag, int *xorbit)
  193. {
  194. *xorbit = ff_j2k_xorbit_lut[flag&15][(flag>>8)&15];
  195. return ff_j2k_sgnctxno_lut[flag&15][(flag>>8)&15];
  196. }
  197. int ff_j2k_init_component(J2kComponent *comp, J2kCodingStyle *codsty, J2kQuantStyle *qntsty, int cbps, int dx, int dy);
  198. void ff_j2k_reinit(J2kComponent *comp, J2kCodingStyle *codsty);
  199. void ff_j2k_cleanup(J2kComponent *comp, J2kCodingStyle *codsty);
  200. #endif /* AVCODEC_J2K_H */