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.

258 lines
9.4KB

  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 Libav.
  7. *
  8. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #ifndef AVCODEC_JPEG2000_H
  23. #define AVCODEC_JPEG2000_H
  24. /**
  25. * @file
  26. * JPEG 2000 structures and defines common
  27. * to encoder and decoder
  28. */
  29. #include <stdint.h>
  30. #include "avcodec.h"
  31. #include "mqc.h"
  32. #include "jpeg2000dwt.h"
  33. enum Jpeg2000Markers {
  34. JPEG2000_SOC = 0xff4f, // start of codestream
  35. JPEG2000_SIZ = 0xff51, // image and tile size
  36. JPEG2000_COD, // coding style default
  37. JPEG2000_COC, // coding style component
  38. JPEG2000_TLM = 0xff55, // packed packet headers, tile-part header
  39. JPEG2000_PLM = 0xff57, // tile-part lengths
  40. JPEG2000_PLT, // packet length, main header
  41. JPEG2000_QCD = 0xff5c, // quantization default
  42. JPEG2000_QCC, // quantization component
  43. JPEG2000_RGN, // region of interest
  44. JPEG2000_POC, // progression order change
  45. JPEG2000_PPM, // packet length, tile-part header
  46. JPEG2000_PPT, // packed packet headers, main header
  47. JPEG2000_CRG = 0xff63, // component registration
  48. JPEG2000_COM, // comment
  49. JPEG2000_SOT = 0xff90, // start of tile-part
  50. JPEG2000_SOP, // start of packet
  51. JPEG2000_EPH, // end of packet header
  52. JPEG2000_SOD, // start of data
  53. JPEG2000_EOC = 0xffd9, // end of codestream
  54. };
  55. enum Jpeg2000Quantsty { // quantization style
  56. JPEG2000_QSTY_NONE, // no quantization
  57. JPEG2000_QSTY_SI, // scalar derived
  58. JPEG2000_QSTY_SE // scalar expounded
  59. };
  60. #define JPEG2000_MAX_CBLKW 64
  61. #define JPEG2000_MAX_CBLKH 64
  62. #define JPEG2000_MAX_DECLEVELS 32
  63. #define JPEG2000_MAX_RESLEVELS (JPEG2000_MAX_DECLEVELS + 1)
  64. // T1 flags
  65. // flags determining significance of neighbor coefficients
  66. #define JPEG2000_T1_SIG_N 0x0001
  67. #define JPEG2000_T1_SIG_E 0x0002
  68. #define JPEG2000_T1_SIG_W 0x0004
  69. #define JPEG2000_T1_SIG_S 0x0008
  70. #define JPEG2000_T1_SIG_NE 0x0010
  71. #define JPEG2000_T1_SIG_NW 0x0020
  72. #define JPEG2000_T1_SIG_SE 0x0040
  73. #define JPEG2000_T1_SIG_SW 0x0080
  74. #define JPEG2000_T1_SIG_NB (JPEG2000_T1_SIG_N | JPEG2000_T1_SIG_E | \
  75. JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_W | \
  76. JPEG2000_T1_SIG_NE | JPEG2000_T1_SIG_NW | \
  77. JPEG2000_T1_SIG_SE | JPEG2000_T1_SIG_SW)
  78. // flags determining sign bit of neighbor coefficients
  79. #define JPEG2000_T1_SGN_N 0x0100
  80. #define JPEG2000_T1_SGN_S 0x0200
  81. #define JPEG2000_T1_SGN_W 0x0400
  82. #define JPEG2000_T1_SGN_E 0x0800
  83. #define JPEG2000_T1_VIS 0x1000
  84. #define JPEG2000_T1_SIG 0x2000
  85. #define JPEG2000_T1_REF 0x4000
  86. #define JPEG2000_T1_SGN 0x8000
  87. // Codeblock coding styles
  88. #define JPEG2000_CBLK_BYPASS 0x01 // Selective arithmetic coding bypass
  89. #define JPEG2000_CBLK_RESET 0x02 // Reset context probabilities
  90. #define JPEG2000_CBLK_TERMALL 0x04 // Terminate after each coding pass
  91. #define JPEG2000_CBLK_VSC 0x08 // Vertical stripe causal context formation
  92. #define JPEG2000_CBLK_PREDTERM 0x10 // Predictable termination
  93. #define JPEG2000_CBLK_SEGSYM 0x20 // Segmentation symbols present
  94. // Coding styles
  95. #define JPEG2000_CSTY_PREC 0x01 // Precincts defined in coding style
  96. #define JPEG2000_CSTY_SOP 0x02 // SOP marker present
  97. #define JPEG2000_CSTY_EPH 0x04 // EPH marker present
  98. // Progression orders
  99. #define JPEG2000_PGOD_LRCP 0x00 // Layer-resolution level-component-position progression
  100. #define JPEG2000_PGOD_RLCP 0x01 // Resolution level-layer-component-position progression
  101. #define JPEG2000_PGOD_RPCL 0x02 // Resolution level-position-component-layer progression
  102. #define JPEG2000_PGOD_PCRL 0x03 // Position-component-resolution level-layer progression
  103. #define JPEG2000_PGOD_CPRL 0x04 // Component-position-resolution level-layer progression
  104. typedef struct Jpeg2000T1Context {
  105. int data[JPEG2000_MAX_CBLKW][JPEG2000_MAX_CBLKH];
  106. int flags[JPEG2000_MAX_CBLKW + 2][JPEG2000_MAX_CBLKH + 2];
  107. MqcState mqc;
  108. } Jpeg2000T1Context;
  109. typedef struct Jpeg2000TgtNode {
  110. uint8_t val;
  111. uint8_t vis;
  112. struct Jpeg2000TgtNode *parent;
  113. } Jpeg2000TgtNode;
  114. typedef struct Jpeg2000CodingStyle {
  115. uint8_t nreslevels; // number of resolution levels
  116. uint8_t nreslevels2decode; // number of resolution levels to decode
  117. uint8_t log2_cblk_width,
  118. log2_cblk_height; // exponent of codeblock size
  119. uint8_t transform; // DWT type
  120. uint8_t csty; // coding style
  121. uint8_t nlayers; // number of layers
  122. uint8_t mct; // multiple component transformation
  123. uint8_t cblk_style; // codeblock coding style
  124. uint8_t prog_order; // progression order
  125. uint8_t log2_prec_widths[JPEG2000_MAX_RESLEVELS]; // precincts size according resolution levels
  126. uint8_t log2_prec_heights[JPEG2000_MAX_RESLEVELS]; // TODO: initialize prec_size array with 0?
  127. } Jpeg2000CodingStyle;
  128. typedef struct Jpeg2000QuantStyle {
  129. uint8_t expn[JPEG2000_MAX_DECLEVELS * 3]; // quantization exponent
  130. uint32_t mant[JPEG2000_MAX_DECLEVELS * 3]; // quantization mantissa
  131. uint8_t quantsty; // quantization style
  132. uint8_t nguardbits; // number of guard bits
  133. } Jpeg2000QuantStyle;
  134. typedef struct Jpeg2000Cblk {
  135. uint8_t npasses;
  136. uint8_t ninclpasses; // number coding of passes included in codestream
  137. uint8_t nonzerobits;
  138. uint16_t length;
  139. uint16_t lengthinc;
  140. uint8_t lblock;
  141. uint8_t zero;
  142. uint8_t data[8192];
  143. uint16_t coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}}
  144. } Jpeg2000Cblk; // code block
  145. typedef struct Jpeg2000Prec {
  146. uint16_t nb_codeblocks_width;
  147. uint16_t nb_codeblocks_height;
  148. Jpeg2000TgtNode *zerobits;
  149. Jpeg2000TgtNode *cblkincl;
  150. Jpeg2000Cblk *cblk;
  151. uint16_t coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}}
  152. } Jpeg2000Prec; // precinct
  153. typedef struct Jpeg2000Band {
  154. uint16_t coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}}
  155. uint16_t log2_cblk_width, log2_cblk_height;
  156. int i_stepsize; // quantization stepsize
  157. float f_stepsize; // quantization stepsize
  158. Jpeg2000Prec *prec;
  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. float *f_data;
  171. int *i_data;
  172. uint16_t coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}} -- can be reduced with lowres option
  173. uint16_t coord_o[2][2]; // border coordinates {{x0, x1}, {y0, y1}} -- original values from jpeg2000 headers
  174. } Jpeg2000Component;
  175. /* misc tools */
  176. static inline int ff_jpeg2000_ceildivpow2(int a, int b)
  177. {
  178. return (a + (1 << b) - 1) >> b;
  179. }
  180. static inline int ff_jpeg2000_ceildiv(int a, int b)
  181. {
  182. return (a + b - 1) / b;
  183. }
  184. /* TIER-1 routines */
  185. /* Set up lookup tables used in TIER-1. */
  186. void ff_jpeg2000_init_tier1_luts(void);
  187. /* Update significance of a coefficient at current position (x,y) and
  188. * for neighbors. */
  189. void ff_jpeg2000_set_significance(Jpeg2000T1Context *t1,
  190. int x, int y, int negative);
  191. extern uint8_t ff_jpeg2000_sigctxno_lut[256][4];
  192. /* Get context label (number in range[0..8]) of a coefficient for significance
  193. * propagation and cleanup coding passes. */
  194. static inline int ff_jpeg2000_getsigctxno(int flag, int bandno)
  195. {
  196. return ff_jpeg2000_sigctxno_lut[flag & 255][bandno];
  197. }
  198. static const uint8_t refctxno_lut[2][2] = { { 14, 15 }, { 16, 16 } };
  199. /* Get context label (number in range[14..16]) of a coefficient for magnitude
  200. * refinement pass. */
  201. static inline int ff_jpeg2000_getrefctxno(int flag)
  202. {
  203. return refctxno_lut[(flag >> 14) & 1][(flag & 255) != 0];
  204. }
  205. extern uint8_t ff_jpeg2000_sgnctxno_lut[16][16];
  206. extern uint8_t ff_jpeg2000_xorbit_lut[16][16];
  207. /* Get context label (number in range[9..13]) for sign decoding. */
  208. static inline int ff_jpeg2000_getsgnctxno(int flag, int *xorbit)
  209. {
  210. *xorbit = ff_jpeg2000_xorbit_lut[flag & 15][(flag >> 8) & 15];
  211. return ff_jpeg2000_sgnctxno_lut[flag & 15][(flag >> 8) & 15];
  212. }
  213. int ff_jpeg2000_init_component(Jpeg2000Component *comp,
  214. Jpeg2000CodingStyle *codsty,
  215. Jpeg2000QuantStyle *qntsty,
  216. int cbps, int dx, int dy,
  217. AVCodecContext *ctx);
  218. void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty);
  219. #endif /* AVCODEC_JPEG2000_H */