jpeg2000.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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_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, // tile-part length, main header
  39. JPEG2000_PLM = 0xff57, // packet length, main header
  40. JPEG2000_PLT, // packet length, tile-part 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, // packed packet headers, main header
  46. JPEG2000_PPT, // packed packet headers, tile-part 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. #define JPEG2000_SOP_FIXED_BYTES 0xFF910004
  56. #define JPEG2000_SOP_BYTE_LENGTH 6
  57. enum Jpeg2000Quantsty { // quantization style
  58. JPEG2000_QSTY_NONE, // no quantization
  59. JPEG2000_QSTY_SI, // scalar derived
  60. JPEG2000_QSTY_SE // scalar expounded
  61. };
  62. #define JPEG2000_MAX_DECLEVELS 33
  63. #define JPEG2000_MAX_RESLEVELS (JPEG2000_MAX_DECLEVELS + 1)
  64. #define JPEG2000_MAX_PASSES 100
  65. // T1 flags
  66. // flags determining significance of neighbor coefficients
  67. #define JPEG2000_T1_SIG_N 0x0001
  68. #define JPEG2000_T1_SIG_E 0x0002
  69. #define JPEG2000_T1_SIG_W 0x0004
  70. #define JPEG2000_T1_SIG_S 0x0008
  71. #define JPEG2000_T1_SIG_NE 0x0010
  72. #define JPEG2000_T1_SIG_NW 0x0020
  73. #define JPEG2000_T1_SIG_SE 0x0040
  74. #define JPEG2000_T1_SIG_SW 0x0080
  75. #define JPEG2000_T1_SIG_NB (JPEG2000_T1_SIG_N | JPEG2000_T1_SIG_E | \
  76. JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_W | \
  77. JPEG2000_T1_SIG_NE | JPEG2000_T1_SIG_NW | \
  78. JPEG2000_T1_SIG_SE | JPEG2000_T1_SIG_SW)
  79. // flags determining sign bit of neighbor coefficients
  80. #define JPEG2000_T1_SGN_N 0x0100
  81. #define JPEG2000_T1_SGN_S 0x0200
  82. #define JPEG2000_T1_SGN_W 0x0400
  83. #define JPEG2000_T1_SGN_E 0x0800
  84. #define JPEG2000_T1_VIS 0x1000
  85. #define JPEG2000_T1_SIG 0x2000
  86. #define JPEG2000_T1_REF 0x4000
  87. #define JPEG2000_T1_SGN 0x8000
  88. // Codeblock coding styles
  89. #define JPEG2000_CBLK_BYPASS 0x01 // Selective arithmetic coding bypass
  90. #define JPEG2000_CBLK_RESET 0x02 // Reset context probabilities
  91. #define JPEG2000_CBLK_TERMALL 0x04 // Terminate after each coding pass
  92. #define JPEG2000_CBLK_VSC 0x08 // Vertical stripe causal context formation
  93. #define JPEG2000_CBLK_PREDTERM 0x10 // Predictable termination
  94. #define JPEG2000_CBLK_SEGSYM 0x20 // Segmentation symbols present
  95. // Coding styles
  96. #define JPEG2000_CSTY_PREC 0x01 // Precincts defined in coding style
  97. #define JPEG2000_CSTY_SOP 0x02 // SOP marker present
  98. #define JPEG2000_CSTY_EPH 0x04 // EPH marker present
  99. // Progression orders
  100. #define JPEG2000_PGOD_LRCP 0x00 // Layer-resolution level-component-position progression
  101. #define JPEG2000_PGOD_RLCP 0x01 // Resolution level-layer-component-position progression
  102. #define JPEG2000_PGOD_RPCL 0x02 // Resolution level-position-component-layer progression
  103. #define JPEG2000_PGOD_PCRL 0x03 // Position-component-resolution level-layer progression
  104. #define JPEG2000_PGOD_CPRL 0x04 // Component-position-resolution level-layer progression
  105. typedef struct Jpeg2000T1Context {
  106. int data[6144];
  107. uint16_t flags[6156];
  108. MqcState mqc;
  109. int stride;
  110. } Jpeg2000T1Context;
  111. typedef struct Jpeg2000TgtNode {
  112. uint8_t val;
  113. uint8_t vis;
  114. struct Jpeg2000TgtNode *parent;
  115. } Jpeg2000TgtNode;
  116. typedef struct Jpeg2000CodingStyle {
  117. int nreslevels; // number of resolution levels
  118. int nreslevels2decode; // number of resolution levels to decode
  119. uint8_t log2_cblk_width,
  120. log2_cblk_height; // exponent of codeblock size
  121. uint8_t transform; // DWT type
  122. uint8_t csty; // coding style
  123. uint8_t nlayers; // number of layers
  124. uint8_t mct; // multiple component transformation
  125. uint8_t cblk_style; // codeblock coding style
  126. uint8_t prog_order; // progression order
  127. uint8_t log2_prec_widths[JPEG2000_MAX_RESLEVELS]; // precincts size according resolution levels
  128. uint8_t log2_prec_heights[JPEG2000_MAX_RESLEVELS]; // TODO: initialize prec_size array with 0?
  129. uint8_t init;
  130. } Jpeg2000CodingStyle;
  131. typedef struct Jpeg2000QuantStyle {
  132. uint8_t expn[JPEG2000_MAX_DECLEVELS * 3]; // quantization exponent
  133. uint16_t mant[JPEG2000_MAX_DECLEVELS * 3]; // quantization mantissa
  134. uint8_t quantsty; // quantization style
  135. uint8_t nguardbits; // number of guard bits
  136. } Jpeg2000QuantStyle;
  137. typedef struct Jpeg2000Pass {
  138. uint16_t rate;
  139. int64_t disto;
  140. uint8_t flushed[4];
  141. int flushed_len;
  142. } Jpeg2000Pass;
  143. typedef struct Jpeg2000Cblk {
  144. uint8_t npasses;
  145. uint8_t ninclpasses; // number coding of passes included in codestream
  146. uint8_t nonzerobits;
  147. uint16_t length;
  148. uint16_t *lengthinc;
  149. uint8_t nb_lengthinc;
  150. uint8_t lblock;
  151. uint8_t *data;
  152. size_t data_allocated;
  153. int nb_terminations;
  154. int nb_terminationsinc;
  155. int *data_start;
  156. Jpeg2000Pass *passes;
  157. int coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}}
  158. } Jpeg2000Cblk; // code block
  159. typedef struct Jpeg2000Prec {
  160. int nb_codeblocks_width;
  161. int nb_codeblocks_height;
  162. Jpeg2000TgtNode *zerobits;
  163. Jpeg2000TgtNode *cblkincl;
  164. Jpeg2000Cblk *cblk;
  165. int decoded_layers;
  166. int coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}}
  167. } Jpeg2000Prec; // precinct
  168. typedef struct Jpeg2000Band {
  169. int coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}}
  170. uint16_t log2_cblk_width, log2_cblk_height;
  171. int i_stepsize; // quantization stepsize
  172. float f_stepsize; // quantization stepsize
  173. Jpeg2000Prec *prec;
  174. } Jpeg2000Band; // subband
  175. typedef struct Jpeg2000ResLevel {
  176. uint8_t nbands;
  177. int coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}}
  178. int num_precincts_x, num_precincts_y; // number of precincts in x/y direction
  179. uint8_t log2_prec_width, log2_prec_height; // exponent of precinct size
  180. Jpeg2000Band *band;
  181. } Jpeg2000ResLevel; // resolution level
  182. typedef struct Jpeg2000Component {
  183. Jpeg2000ResLevel *reslevel;
  184. DWTContext dwt;
  185. float *f_data;
  186. int *i_data;
  187. int coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}} -- can be reduced with lowres option
  188. int coord_o[2][2]; // border coordinates {{x0, x1}, {y0, y1}} -- original values from jpeg2000 headers
  189. uint8_t roi_shift; // ROI scaling value for the component
  190. } Jpeg2000Component;
  191. /* misc tools */
  192. static inline int ff_jpeg2000_ceildivpow2(int a, int b)
  193. {
  194. return -((-(int64_t)a) >> b);
  195. }
  196. static inline int ff_jpeg2000_ceildiv(int a, int64_t b)
  197. {
  198. return (a + b - 1) / b;
  199. }
  200. /* TIER-1 routines */
  201. /* Set up lookup tables used in TIER-1. */
  202. void ff_jpeg2000_init_tier1_luts(void);
  203. /* Update significance of a coefficient at current position (x,y) and
  204. * for neighbors. */
  205. void ff_jpeg2000_set_significance(Jpeg2000T1Context *t1,
  206. int x, int y, int negative);
  207. extern uint8_t ff_jpeg2000_sigctxno_lut[256][4];
  208. /* Get context label (number in range[0..8]) of a coefficient for significance
  209. * propagation and cleanup coding passes. */
  210. static inline int ff_jpeg2000_getsigctxno(int flag, int bandno)
  211. {
  212. return ff_jpeg2000_sigctxno_lut[flag & 255][bandno];
  213. }
  214. static const uint8_t refctxno_lut[2][2] = { { 14, 15 }, { 16, 16 } };
  215. /* Get context label (number in range[14..16]) of a coefficient for magnitude
  216. * refinement pass. */
  217. static inline int ff_jpeg2000_getrefctxno(int flag)
  218. {
  219. return refctxno_lut[(flag >> 14) & 1][(flag & 255) != 0];
  220. }
  221. extern uint8_t ff_jpeg2000_sgnctxno_lut[16][16];
  222. extern uint8_t ff_jpeg2000_xorbit_lut[16][16];
  223. /* Get context label (number in range[9..13]) for sign decoding. */
  224. static inline int ff_jpeg2000_getsgnctxno(int flag, int *xorbit)
  225. {
  226. *xorbit = ff_jpeg2000_xorbit_lut[flag & 15][(flag >> 8) & 15];
  227. return ff_jpeg2000_sgnctxno_lut[flag & 15][(flag >> 8) & 15];
  228. }
  229. int ff_jpeg2000_init_component(Jpeg2000Component *comp,
  230. Jpeg2000CodingStyle *codsty,
  231. Jpeg2000QuantStyle *qntsty,
  232. int cbps, int dx, int dy,
  233. AVCodecContext *ctx);
  234. void ff_jpeg2000_reinit(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty);
  235. void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty);
  236. static inline int needs_termination(int style, int passno) {
  237. if (style & JPEG2000_CBLK_BYPASS) {
  238. int type = passno % 3;
  239. passno /= 3;
  240. if (type == 0 && passno > 2)
  241. return 2;
  242. if (type == 2 && passno > 2)
  243. return 1;
  244. if (style & JPEG2000_CBLK_TERMALL) {
  245. return passno > 2 ? 2 : 1;
  246. }
  247. }
  248. if (style & JPEG2000_CBLK_TERMALL)
  249. return 1;
  250. return 0;
  251. }
  252. #endif /* AVCODEC_JPEG2000_H */