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.

251 lines
8.9KB

  1. /*
  2. * JPEG 2000 common defines, structures and functions
  3. * Copyright (c) 2007 Kamil Nowosad
  4. * Copyright (c) 2013 Nicolas Bertrand <nicoinattendu@gmail.com>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #ifndef AVCODEC_J2K_H
  23. #define AVCODEC_J2K_H
  24. /**
  25. * JPEG2000 tables
  26. * @file
  27. * @author Kamil Nowosad
  28. */
  29. #include "mqc.h"
  30. #include "j2k_dwt.h"
  31. enum Jpeg2000Markers {
  32. JPEG2000_SOC = 0xff4f, // start of codestream
  33. JPEG2000_SIZ = 0xff51, // image and tile size
  34. JPEG2000_COD, // coding style default
  35. JPEG2000_COC, // coding style component
  36. JPEG2000_TLM = 0xff55, // packed packet headers, tile-part header
  37. JPEG2000_PLM = 0xff57, // tile-part lengths
  38. JPEG2000_PLT, // packet length, main header
  39. JPEG2000_QCD = 0xff5c, // quantization default
  40. JPEG2000_QCC, // quantization component
  41. JPEG2000_RGN, // region of interest
  42. JPEG2000_POC, // progression order change
  43. JPEG2000_PPM, // packet length, tile-part header
  44. JPEG2000_PPT, // packed packet headers, main header
  45. JPEG2000_CRG = 0xff63, // component registration
  46. JPEG2000_COM, // comment
  47. JPEG2000_SOT = 0xff90, // start of tile-part
  48. JPEG2000_SOP, // start of packet
  49. JPEG2000_EPH, // end of packet header
  50. JPEG2000_SOD, // start of data
  51. JPEG2000_EOC = 0xffd9, // end of codestream
  52. };
  53. enum Jpeg2000Quantsty { // quantization style
  54. JPEG2000_QSTY_NONE, // no quantization
  55. JPEG2000_QSTY_SI, // scalar derived
  56. JPEG2000_QSTY_SE // scalar expounded
  57. };
  58. #define JPEG2000_MAX_CBLKW 64
  59. #define JPEG2000_MAX_CBLKH 64
  60. #define JPEG2000_MAX_RESLEVELS 33
  61. // T1 flags
  62. // flags determining significance of neighbor coefficients
  63. #define JPEG2000_T1_SIG_N 0x0001
  64. #define JPEG2000_T1_SIG_E 0x0002
  65. #define JPEG2000_T1_SIG_W 0x0004
  66. #define JPEG2000_T1_SIG_S 0x0008
  67. #define JPEG2000_T1_SIG_NE 0x0010
  68. #define JPEG2000_T1_SIG_NW 0x0020
  69. #define JPEG2000_T1_SIG_SE 0x0040
  70. #define JPEG2000_T1_SIG_SW 0x0080
  71. #define JPEG2000_T1_SIG_NB (JPEG2000_T1_SIG_N | JPEG2000_T1_SIG_E | \
  72. JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_W | \
  73. JPEG2000_T1_SIG_NE | JPEG2000_T1_SIG_NW | \
  74. JPEG2000_T1_SIG_SE | JPEG2000_T1_SIG_SW)
  75. // flags determining sign bit of neighbor coefficients
  76. #define JPEG2000_T1_SGN_N 0x0100
  77. #define JPEG2000_T1_SGN_S 0x0200
  78. #define JPEG2000_T1_SGN_W 0x0400
  79. #define JPEG2000_T1_SGN_E 0x0800
  80. #define JPEG2000_T1_VIS 0x1000
  81. #define JPEG2000_T1_SIG 0x2000
  82. #define JPEG2000_T1_REF 0x4000
  83. #define JPEG2000_T1_SGN 0x8000
  84. // Codeblock coding styles
  85. #define JPEG2000_CBLK_BYPASS 0x01 // Selective arithmetic coding bypass
  86. #define JPEG2000_CBLK_RESET 0x02 // Reset context probabilities
  87. #define JPEG2000_CBLK_TERMALL 0x04 // Terminate after each coding pass
  88. #define JPEG2000_CBLK_VSC 0x08 // Vertical stripe causal context formation
  89. #define JPEG2000_CBLK_PREDTERM 0x10 // Predictable termination
  90. #define JPEG2000_CBLK_SEGSYM 0x20 // Segmentation symbols present
  91. // Coding styles
  92. #define JPEG2000_CSTY_PREC 0x01 // Precincts defined in coding style
  93. #define JPEG2000_CSTY_SOP 0x02 // SOP marker present
  94. #define JPEG2000_CSTY_EPH 0x04 // EPH marker present
  95. // Progression orders
  96. #define JPEG2000_PGOD_LRCP 0x00 // Layer-resolution level-component-position progression
  97. #define JPEG2000_PGOD_RLCP 0x01 // Resolution level-layer-component-position progression
  98. #define JPEG2000_PGOD_RPCL 0x02 // Resolution level-position-component-layer progression
  99. #define JPEG2000_PGOD_PCRL 0x03 // Position-component-resolution level-layer progression
  100. #define JPEG2000_PGOD_CPRL 0x04 // Component-position-resolution level-layer progression
  101. typedef struct Jpeg2000T1Context {
  102. int data[JPEG2000_MAX_CBLKW][JPEG2000_MAX_CBLKH];
  103. int flags[JPEG2000_MAX_CBLKW+2][JPEG2000_MAX_CBLKH+2];
  104. MqcState mqc;
  105. } Jpeg2000T1Context;
  106. typedef struct Jpeg2000TgtNode {
  107. uint8_t val;
  108. uint8_t vis;
  109. struct Jpeg2000TgtNode *parent;
  110. } Jpeg2000TgtNode;
  111. typedef struct Jpeg2000CodingStyle {
  112. uint8_t nreslevels; // number of resolution levels
  113. uint8_t log2_cblk_width,
  114. log2_cblk_height; // exponent of codeblock size
  115. uint8_t transform; // DWT type
  116. uint8_t csty; // coding style
  117. uint8_t log2_prec_width,
  118. log2_prec_height; // precinct size
  119. uint8_t nlayers; // number of layers
  120. uint8_t mct; // multiple component transformation
  121. uint8_t cblk_style; // codeblock coding style
  122. uint8_t prog_order; // progression order
  123. uint8_t log2_prec_widths[JPEG2000_MAX_RESLEVELS]; // precincts size according resolution levels
  124. uint8_t log2_prec_heights[JPEG2000_MAX_RESLEVELS]; // TODO: initialize prec_size array with 0?
  125. } Jpeg2000CodingStyle;
  126. typedef struct Jpeg2000QuantStyle {
  127. uint8_t expn[32 * 3]; // quantization exponent
  128. uint16_t mant[32 * 3]; // quantization mantissa
  129. uint8_t quantsty; // quantization style
  130. uint8_t nguardbits; // number of guard bits
  131. } Jpeg2000QuantStyle;
  132. typedef struct Jpeg2000Pass {
  133. uint16_t rate;
  134. int64_t disto;
  135. } Jpeg2000Pass;
  136. typedef struct Jpeg2000Cblk {
  137. uint8_t npasses;
  138. uint8_t ninclpasses; // number coding of passes included in codestream
  139. uint8_t nonzerobits;
  140. uint16_t length;
  141. uint16_t lengthinc;
  142. uint8_t lblock;
  143. uint8_t zero;
  144. uint8_t data[8192];
  145. Jpeg2000Pass passes[100];
  146. } Jpeg2000Cblk; // code block
  147. typedef struct Jpeg2000Prec {
  148. uint16_t xi0, xi1, yi0, yi1; // codeblock indexes ([xi0, xi1))
  149. Jpeg2000TgtNode *zerobits;
  150. Jpeg2000TgtNode *cblkincl;
  151. } Jpeg2000Prec; // precinct
  152. typedef struct Jpeg2000Band {
  153. uint16_t coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}}
  154. uint16_t codeblock_width, codeblock_height;
  155. uint16_t cblknx, cblkny;
  156. uint32_t stepsize; // quantization stepsize (* 2^13)
  157. Jpeg2000Prec *prec;
  158. Jpeg2000Cblk *cblk;
  159. } Jpeg2000Band; // subband
  160. typedef struct Jpeg2000ResLevel {
  161. uint8_t nbands;
  162. uint16_t coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}}
  163. uint16_t num_precincts_x, num_precincts_y; // number of precincts in x/y direction
  164. uint8_t log2_prec_width, log2_prec_height; // exponent of precinct size
  165. Jpeg2000Band *band;
  166. } Jpeg2000ResLevel; // resolution level
  167. typedef struct Jpeg2000Component {
  168. Jpeg2000ResLevel *reslevel;
  169. DWTContext dwt;
  170. int *data;
  171. uint16_t coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}}
  172. } Jpeg2000Component;
  173. /* misc tools */
  174. static inline int ff_jpeg2000_ceildivpow2(int a, int b)
  175. {
  176. return (a + (1 << b) - 1) >> b;
  177. }
  178. static inline int ff_jpeg2000_ceildiv(int a, int b)
  179. {
  180. return (a + b - 1) / b;
  181. }
  182. /* tag tree routines */
  183. Jpeg2000TgtNode *ff_j2k_tag_tree_init(int w, int h);
  184. /* TIER-1 routines */
  185. void ff_jpeg2000_init_tier1_luts(void);
  186. void ff_j2k_set_significant(Jpeg2000T1Context *t1, int x, int y, int negative);
  187. extern uint8_t ff_jpeg2000_sigctxno_lut[256][4];
  188. /* Get context label (number in range[0..8]) of a coefficient for significance
  189. * propagation and cleanup coding passes. */
  190. static inline int ff_jpeg2000_getsigctxno(int flag, int bandno)
  191. {
  192. return ff_jpeg2000_sigctxno_lut[flag & 255][bandno];
  193. }
  194. /* Get context label (number in range[14..16]) of a coefficient for magnitude
  195. * refinement pass. */
  196. static inline int ff_jpeg2000_getrefctxno(int flag)
  197. {
  198. static const uint8_t refctxno_lut[2][2] = { { 14, 15 }, { 16, 16 } };
  199. return refctxno_lut[(flag >> 14) & 1][(flag & 255) != 0];
  200. }
  201. extern uint8_t ff_jpeg2000_sgnctxno_lut[16][16];
  202. extern uint8_t ff_jpeg2000_xorbit_lut[16][16];
  203. /* Get context label (number in range[9..13]) for sign decoding. */
  204. static inline int ff_jpeg2000_getsgnctxno(int flag, int *xorbit)
  205. {
  206. *xorbit = ff_jpeg2000_xorbit_lut[flag & 15][(flag >> 8) & 15];
  207. return ff_jpeg2000_sgnctxno_lut[flag & 15][(flag >> 8) & 15];
  208. }
  209. int ff_j2k_init_component(Jpeg2000Component *comp,
  210. Jpeg2000CodingStyle *codsty,
  211. Jpeg2000QuantStyle *qntsty,
  212. int cbps, int dx, int dy);
  213. void ff_j2k_reinit(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty);
  214. void ff_j2k_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty);
  215. #endif /* AVCODEC_J2K_H */