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.

1046 lines
36KB

  1. /*
  2. * JPEG2000 image encoder
  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. /**
  22. * JPEG2000 image encoder
  23. * @file
  24. * @author Kamil Nowosad
  25. */
  26. #include <float.h>
  27. #include "avcodec.h"
  28. #include "bytestream.h"
  29. #include "j2k.h"
  30. #include "libavutil/common.h"
  31. #define NMSEDEC_BITS 7
  32. #define NMSEDEC_FRACBITS (NMSEDEC_BITS-1)
  33. #define WMSEDEC_SHIFT 13 ///< must be >= 13
  34. #define LAMBDA_SCALE (100000000LL << (WMSEDEC_SHIFT - 13))
  35. static int lut_nmsedec_ref [1<<NMSEDEC_BITS],
  36. lut_nmsedec_ref0[1<<NMSEDEC_BITS],
  37. lut_nmsedec_sig [1<<NMSEDEC_BITS],
  38. lut_nmsedec_sig0[1<<NMSEDEC_BITS];
  39. static const int dwt_norms[2][4][10] = { // [dwt_type][band][rlevel] (multiplied by 10000)
  40. {{10000, 19650, 41770, 84030, 169000, 338400, 676900, 1353000, 2706000, 5409000},
  41. {20220, 39890, 83550, 170400, 342700, 686300, 1373000, 2746000, 5490000},
  42. {20220, 39890, 83550, 170400, 342700, 686300, 1373000, 2746000, 5490000},
  43. {20800, 38650, 83070, 171800, 347100, 695900, 1393000, 2786000, 5572000}},
  44. {{10000, 15000, 27500, 53750, 106800, 213400, 426700, 853300, 1707000, 3413000},
  45. {10380, 15920, 29190, 57030, 113300, 226400, 452500, 904800, 1809000},
  46. {10380, 15920, 29190, 57030, 113300, 226400, 452500, 904800, 1809000},
  47. { 7186, 9218, 15860, 30430, 60190, 120100, 240000, 479700, 959300}}
  48. };
  49. typedef struct {
  50. J2kComponent *comp;
  51. } J2kTile;
  52. typedef struct {
  53. AVCodecContext *avctx;
  54. AVFrame *picture;
  55. int width, height; ///< image width and height
  56. uint8_t cbps[4]; ///< bits per sample in particular components
  57. int chroma_shift[2];
  58. uint8_t planar;
  59. int ncomponents;
  60. int tile_width, tile_height; ///< tile size
  61. int numXtiles, numYtiles;
  62. uint8_t *buf_start;
  63. uint8_t *buf;
  64. uint8_t *buf_end;
  65. int bit_index;
  66. int64_t lambda;
  67. J2kCodingStyle codsty;
  68. J2kQuantStyle qntsty;
  69. J2kTile *tile;
  70. } J2kEncoderContext;
  71. /* debug */
  72. #if 0
  73. #undef ifprintf
  74. #undef printf
  75. static void nspaces(FILE *fd, int n)
  76. {
  77. while(n--) putc(' ', fd);
  78. }
  79. static void printv(int *tab, int l)
  80. {
  81. int i;
  82. for (i = 0; i < l; i++)
  83. printf("%.3d ", tab[i]);
  84. printf("\n");
  85. }
  86. static void printu(uint8_t *tab, int l)
  87. {
  88. int i;
  89. for (i = 0; i < l; i++)
  90. printf("%.3hd ", tab[i]);
  91. printf("\n");
  92. }
  93. static void printcomp(J2kComponent *comp)
  94. {
  95. int i;
  96. for (i = 0; i < comp->y1 - comp->y0; i++)
  97. printv(comp->data + i * (comp->x1 - comp->x0), comp->x1 - comp->x0);
  98. }
  99. static void dump(J2kEncoderContext *s, FILE *fd)
  100. {
  101. int tileno, compno, reslevelno, bandno, precno;
  102. fprintf(fd, "XSiz = %d, YSiz = %d, tile_width = %d, tile_height = %d\n"
  103. "numXtiles = %d, numYtiles = %d, ncomponents = %d\n"
  104. "tiles:\n",
  105. s->width, s->height, s->tile_width, s->tile_height,
  106. s->numXtiles, s->numYtiles, s->ncomponents);
  107. for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
  108. J2kTile *tile = s->tile + tileno;
  109. nspaces(fd, 2);
  110. fprintf(fd, "tile %d:\n", tileno);
  111. for(compno = 0; compno < s->ncomponents; compno++){
  112. J2kComponent *comp = tile->comp + compno;
  113. nspaces(fd, 4);
  114. fprintf(fd, "component %d:\n", compno);
  115. nspaces(fd, 4);
  116. fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d\n",
  117. comp->x0, comp->x1, comp->y0, comp->y1);
  118. for(reslevelno = 0; reslevelno < s->nreslevels; reslevelno++){
  119. J2kResLevel *reslevel = comp->reslevel + reslevelno;
  120. nspaces(fd, 6);
  121. fprintf(fd, "reslevel %d:\n", reslevelno);
  122. nspaces(fd, 6);
  123. fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d, nbands = %d\n",
  124. reslevel->x0, reslevel->x1, reslevel->y0,
  125. reslevel->y1, reslevel->nbands);
  126. for(bandno = 0; bandno < reslevel->nbands; bandno++){
  127. J2kBand *band = reslevel->band + bandno;
  128. nspaces(fd, 8);
  129. fprintf(fd, "band %d:\n", bandno);
  130. nspaces(fd, 8);
  131. fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d,"
  132. "codeblock_width = %d, codeblock_height = %d cblknx = %d cblkny = %d\n",
  133. band->x0, band->x1,
  134. band->y0, band->y1,
  135. band->codeblock_width, band->codeblock_height,
  136. band->cblknx, band->cblkny);
  137. for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
  138. J2kPrec *prec = band->prec + precno;
  139. nspaces(fd, 10);
  140. fprintf(fd, "prec %d:\n", precno);
  141. nspaces(fd, 10);
  142. fprintf(fd, "xi0 = %d, xi1 = %d, yi0 = %d, yi1 = %d\n",
  143. prec->xi0, prec->xi1, prec->yi0, prec->yi1);
  144. }
  145. }
  146. }
  147. }
  148. }
  149. }
  150. #endif
  151. /* bitstream routines */
  152. /** put n times val bit */
  153. static void put_bits(J2kEncoderContext *s, int val, int n) // TODO: optimize
  154. {
  155. while (n-- > 0){
  156. if (s->bit_index == 8)
  157. {
  158. s->bit_index = *s->buf == 0xff;
  159. *(++s->buf) = 0;
  160. }
  161. *s->buf |= val << (7 - s->bit_index++);
  162. }
  163. }
  164. /** put n least significant bits of a number num */
  165. static void put_num(J2kEncoderContext *s, int num, int n)
  166. {
  167. while(--n >= 0)
  168. put_bits(s, (num >> n) & 1, 1);
  169. }
  170. /** flush the bitstream */
  171. static void j2k_flush(J2kEncoderContext *s)
  172. {
  173. if (s->bit_index){
  174. s->bit_index = 0;
  175. s->buf++;
  176. }
  177. }
  178. /* tag tree routines */
  179. /** code the value stored in node */
  180. static void tag_tree_code(J2kEncoderContext *s, J2kTgtNode *node, int threshold)
  181. {
  182. J2kTgtNode *stack[30];
  183. int sp = 1, curval = 0;
  184. stack[0] = node;
  185. node = node->parent;
  186. while(node){
  187. if (node->vis){
  188. curval = node->val;
  189. break;
  190. }
  191. node->vis++;
  192. stack[sp++] = node;
  193. node = node->parent;
  194. }
  195. while(--sp >= 0){
  196. if (stack[sp]->val >= threshold){
  197. put_bits(s, 0, threshold - curval);
  198. break;
  199. }
  200. put_bits(s, 0, stack[sp]->val - curval);
  201. put_bits(s, 1, 1);
  202. curval = stack[sp]->val;
  203. }
  204. }
  205. /** update the value in node */
  206. static void tag_tree_update(J2kTgtNode *node)
  207. {
  208. int lev = 0;
  209. while (node->parent){
  210. if (node->parent->val <= node->val)
  211. break;
  212. node->parent->val = node->val;
  213. node = node->parent;
  214. lev++;
  215. }
  216. }
  217. static int put_siz(J2kEncoderContext *s)
  218. {
  219. int i;
  220. if (s->buf_end - s->buf < 40 + 3 * s->ncomponents)
  221. return -1;
  222. bytestream_put_be16(&s->buf, J2K_SIZ);
  223. bytestream_put_be16(&s->buf, 38 + 3 * s->ncomponents); // Lsiz
  224. bytestream_put_be16(&s->buf, 0); // Rsiz
  225. bytestream_put_be32(&s->buf, s->width); // width
  226. bytestream_put_be32(&s->buf, s->height); // height
  227. bytestream_put_be32(&s->buf, 0); // X0Siz
  228. bytestream_put_be32(&s->buf, 0); // Y0Siz
  229. bytestream_put_be32(&s->buf, s->tile_width); // XTSiz
  230. bytestream_put_be32(&s->buf, s->tile_height); // YTSiz
  231. bytestream_put_be32(&s->buf, 0); // XT0Siz
  232. bytestream_put_be32(&s->buf, 0); // YT0Siz
  233. bytestream_put_be16(&s->buf, s->ncomponents); // CSiz
  234. for (i = 0; i < s->ncomponents; i++){ // Ssiz_i XRsiz_i, YRsiz_i
  235. bytestream_put_byte(&s->buf, 7);
  236. bytestream_put_byte(&s->buf, i?1<<s->chroma_shift[0]:1);
  237. bytestream_put_byte(&s->buf, i?1<<s->chroma_shift[1]:1);
  238. }
  239. return 0;
  240. }
  241. static int put_cod(J2kEncoderContext *s)
  242. {
  243. J2kCodingStyle *codsty = &s->codsty;
  244. if (s->buf_end - s->buf < 14)
  245. return -1;
  246. bytestream_put_be16(&s->buf, J2K_COD);
  247. bytestream_put_be16(&s->buf, 12); // Lcod
  248. bytestream_put_byte(&s->buf, 0); // Scod
  249. // SGcod
  250. bytestream_put_byte(&s->buf, 0); // progression level
  251. bytestream_put_be16(&s->buf, 1); // num of layers
  252. bytestream_put_byte(&s->buf, 0); // multiple component transformation
  253. // SPcod
  254. bytestream_put_byte(&s->buf, codsty->nreslevels - 1); // num of decomp. levels
  255. bytestream_put_byte(&s->buf, codsty->log2_cblk_width-2); // cblk width
  256. bytestream_put_byte(&s->buf, codsty->log2_cblk_height-2); // cblk height
  257. bytestream_put_byte(&s->buf, 0); // cblk style
  258. bytestream_put_byte(&s->buf, codsty->transform); // transformation
  259. return 0;
  260. }
  261. static int put_qcd(J2kEncoderContext *s, int compno)
  262. {
  263. int i, size;
  264. J2kCodingStyle *codsty = &s->codsty;
  265. J2kQuantStyle *qntsty = &s->qntsty;
  266. if (qntsty->quantsty == J2K_QSTY_NONE)
  267. size = 4 + 3 * (codsty->nreslevels-1);
  268. else // QSTY_SE
  269. size = 5 + 6 * (codsty->nreslevels-1);
  270. if (s->buf_end - s->buf < size + 2)
  271. return -1;
  272. bytestream_put_be16(&s->buf, J2K_QCD);
  273. bytestream_put_be16(&s->buf, size); // LQcd
  274. bytestream_put_byte(&s->buf, (qntsty->nguardbits << 5) | qntsty->quantsty); // Sqcd
  275. if (qntsty->quantsty == J2K_QSTY_NONE)
  276. for (i = 0; i < codsty->nreslevels * 3 - 2; i++)
  277. bytestream_put_byte(&s->buf, qntsty->expn[i] << 3);
  278. else // QSTY_SE
  279. for (i = 0; i < codsty->nreslevels * 3 - 2; i++)
  280. bytestream_put_be16(&s->buf, (qntsty->expn[i] << 11) | qntsty->mant[i]);
  281. return 0;
  282. }
  283. static uint8_t *put_sot(J2kEncoderContext *s, int tileno)
  284. {
  285. uint8_t *psotptr;
  286. if (s->buf_end - s->buf < 12)
  287. return -1;
  288. bytestream_put_be16(&s->buf, J2K_SOT);
  289. bytestream_put_be16(&s->buf, 10); // Lsot
  290. bytestream_put_be16(&s->buf, tileno); // Isot
  291. psotptr = s->buf;
  292. bytestream_put_be32(&s->buf, 0); // Psot (filled in later)
  293. bytestream_put_byte(&s->buf, 0); // TPsot
  294. bytestream_put_byte(&s->buf, 1); // TNsot
  295. return psotptr;
  296. }
  297. /**
  298. * compute the sizes of tiles, resolution levels, bands, etc.
  299. * allocate memory for them
  300. * divide the input image into tile-components
  301. */
  302. static int init_tiles(J2kEncoderContext *s)
  303. {
  304. int tileno, tilex, tiley, compno;
  305. J2kCodingStyle *codsty = &s->codsty;
  306. J2kQuantStyle *qntsty = &s->qntsty;
  307. s->numXtiles = ff_j2k_ceildiv(s->width, s->tile_width);
  308. s->numYtiles = ff_j2k_ceildiv(s->height, s->tile_height);
  309. s->tile = av_malloc(s->numXtiles * s->numYtiles * sizeof(J2kTile));
  310. if (!s->tile)
  311. return AVERROR(ENOMEM);
  312. for (tileno = 0, tiley = 0; tiley < s->numYtiles; tiley++)
  313. for (tilex = 0; tilex < s->numXtiles; tilex++, tileno++){
  314. J2kTile *tile = s->tile + tileno;
  315. tile->comp = av_malloc(s->ncomponents * sizeof(J2kComponent));
  316. if (!tile->comp)
  317. return AVERROR(ENOMEM);
  318. for (compno = 0; compno < s->ncomponents; compno++){
  319. J2kComponent *comp = tile->comp + compno;
  320. int ret, i, j;
  321. comp->coord[0][0] = tilex * s->tile_width;
  322. comp->coord[0][1] = FFMIN((tilex+1)*s->tile_width, s->width);
  323. comp->coord[1][0] = tiley * s->tile_height;
  324. comp->coord[1][1] = FFMIN((tiley+1)*s->tile_height, s->height);
  325. if (compno > 0)
  326. for (i = 0; i < 2; i++)
  327. for (j = 0; j < 2; j++)
  328. comp->coord[i][j] = ff_j2k_ceildivpow2(comp->coord[i][j], s->chroma_shift[i]);
  329. if (ret = ff_j2k_init_component(comp, codsty, qntsty, s->cbps[compno]))
  330. return ret;
  331. }
  332. }
  333. return 0;
  334. }
  335. static void copy_frame(J2kEncoderContext *s)
  336. {
  337. int tileno, compno, i, y, x;
  338. uint8_t *line;
  339. for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
  340. J2kTile *tile = s->tile + tileno;
  341. if (s->planar){
  342. for (compno = 0; compno < s->ncomponents; compno++){
  343. J2kComponent *comp = tile->comp + compno;
  344. int *dst = comp->data;
  345. line = s->picture->data[compno]
  346. + comp->coord[1][0] * s->picture->linesize[compno]
  347. + comp->coord[0][0];
  348. for (y = comp->coord[1][0]; y < comp->coord[1][1]; y++){
  349. uint8_t *ptr = line;
  350. for (x = comp->coord[0][0]; x < comp->coord[0][1]; x++)
  351. *dst++ = *ptr++ - (1 << 7);
  352. line += s->picture->linesize[compno];
  353. }
  354. }
  355. } else{
  356. line = s->picture->data[0] + tile->comp[0].coord[1][0] * s->picture->linesize[0]
  357. + tile->comp[0].coord[0][0] * s->ncomponents;
  358. i = 0;
  359. for (y = tile->comp[0].coord[1][0]; y < tile->comp[0].coord[1][1]; y++){
  360. uint8_t *ptr = line;
  361. for (x = tile->comp[0].coord[0][0]; x < tile->comp[0].coord[0][1]; x++, i++){
  362. for (compno = 0; compno < s->ncomponents; compno++){
  363. tile->comp[compno].data[i] = *ptr++ - (1 << 7);
  364. }
  365. }
  366. line += s->picture->linesize[0];
  367. }
  368. }
  369. }
  370. }
  371. static void init_quantization(J2kEncoderContext *s)
  372. {
  373. int compno, reslevelno, bandno;
  374. J2kQuantStyle *qntsty = &s->qntsty;
  375. J2kCodingStyle *codsty = &s->codsty;
  376. for (compno = 0; compno < s->ncomponents; compno++){
  377. int gbandno = 0;
  378. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
  379. int nbands, lev = codsty->nreslevels - reslevelno - 1;
  380. nbands = reslevelno ? 3 : 1;
  381. for (bandno = 0; bandno < nbands; bandno++, gbandno++){
  382. int expn, mant;
  383. if (codsty->transform == FF_DWT97){
  384. int bandpos = bandno + (reslevelno>0),
  385. ss = 81920000 / dwt_norms[0][bandpos][lev],
  386. log = av_log2(ss);
  387. mant = (11 - log < 0 ? ss >> log - 11 : ss << 11 - log) & 0x7ff;
  388. expn = s->cbps[compno] - log + 13;
  389. } else
  390. expn = ((bandno&2)>>1) + (reslevelno>0) + s->cbps[compno];
  391. qntsty->expn[gbandno] = expn;
  392. qntsty->mant[gbandno] = mant;
  393. }
  394. }
  395. }
  396. }
  397. static void init_luts()
  398. {
  399. int i, a,
  400. mask = ~((1<<NMSEDEC_FRACBITS)-1);
  401. for (i = 0; i < (1 << NMSEDEC_BITS); i++){
  402. lut_nmsedec_sig[i] = FFMAX(6*i - (9<<NMSEDEC_FRACBITS-1) << 12-NMSEDEC_FRACBITS, 0);
  403. lut_nmsedec_sig0[i] = FFMAX((i*i + (1<<NMSEDEC_FRACBITS-1) & mask) << 1, 0);
  404. a = (i >> (NMSEDEC_BITS-2)&2) + 1;
  405. lut_nmsedec_ref[i] = FFMAX((-2*i + (1<<NMSEDEC_FRACBITS) + a*i - (a*a<<NMSEDEC_FRACBITS-2))
  406. << 13-NMSEDEC_FRACBITS, 0);
  407. lut_nmsedec_ref0[i] = FFMAX(((i*i + (1-4*i << NMSEDEC_FRACBITS-1) + (1<<2*NMSEDEC_FRACBITS)) & mask)
  408. << 1, 0);
  409. }
  410. }
  411. /* tier-1 routines */
  412. static int getnmsedec_sig(int x, int bpno)
  413. {
  414. if (bpno > NMSEDEC_FRACBITS)
  415. return lut_nmsedec_sig[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
  416. return lut_nmsedec_sig0[x & ((1 << NMSEDEC_BITS) - 1)];
  417. }
  418. static int getnmsedec_ref(int x, int bpno)
  419. {
  420. if (bpno > NMSEDEC_FRACBITS)
  421. return lut_nmsedec_ref[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
  422. return lut_nmsedec_ref0[x & ((1 << NMSEDEC_BITS) - 1)];
  423. }
  424. static void encode_sigpass(J2kT1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
  425. {
  426. int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
  427. for (y0 = 0; y0 < height; y0 += 4)
  428. for (x = 0; x < width; x++)
  429. for (y = y0; y < height && y < y0+4; y++){
  430. if (!(t1->flags[y+1][x+1] & J2K_T1_SIG) && (t1->flags[y+1][x+1] & J2K_T1_SIG_NB)){
  431. int ctxno = ff_j2k_getnbctxno(t1->flags[y+1][x+1], bandno),
  432. bit = t1->data[y][x] & mask ? 1 : 0;
  433. ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, bit);
  434. if (bit){
  435. int xorbit;
  436. int ctxno = ff_j2k_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
  437. ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[y+1][x+1] >> 15) ^ xorbit);
  438. *nmsedec += getnmsedec_sig(t1->data[y][x], bpno + NMSEDEC_FRACBITS);
  439. ff_j2k_set_significant(t1, x, y, t1->flags[y+1][x+1] >> 15);
  440. }
  441. t1->flags[y+1][x+1] |= J2K_T1_VIS;
  442. }
  443. }
  444. }
  445. static void encode_refpass(J2kT1Context *t1, int width, int height, int *nmsedec, int bpno)
  446. {
  447. int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
  448. for (y0 = 0; y0 < height; y0 += 4)
  449. for (x = 0; x < width; x++)
  450. for (y = y0; y < height && y < y0+4; y++)
  451. if ((t1->flags[y+1][x+1] & (J2K_T1_SIG | J2K_T1_VIS)) == J2K_T1_SIG){
  452. int ctxno = ff_j2k_getrefctxno(t1->flags[y+1][x+1]);
  453. *nmsedec += getnmsedec_ref(t1->data[y][x], bpno + NMSEDEC_FRACBITS);
  454. ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[y][x] & mask ? 1:0);
  455. t1->flags[y+1][x+1] |= J2K_T1_REF;
  456. }
  457. }
  458. static void encode_clnpass(J2kT1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
  459. {
  460. int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
  461. for (y0 = 0; y0 < height; y0 += 4)
  462. for (x = 0; x < width; x++){
  463. if (y0 + 3 < height && !(
  464. (t1->flags[y0+1][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG)) ||
  465. (t1->flags[y0+2][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG)) ||
  466. (t1->flags[y0+3][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG)) ||
  467. (t1->flags[y0+4][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG))))
  468. {
  469. // aggregation mode
  470. int rlen;
  471. for (rlen = 0; rlen < 4; rlen++)
  472. if (t1->data[y0+rlen][x] & mask)
  473. break;
  474. ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL, rlen != 4);
  475. if (rlen == 4)
  476. continue;
  477. ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen >> 1);
  478. ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen & 1);
  479. for (y = y0 + rlen; y < y0 + 4; y++){
  480. if (!(t1->flags[y+1][x+1] & (J2K_T1_SIG | J2K_T1_VIS))){
  481. int ctxno = ff_j2k_getnbctxno(t1->flags[y+1][x+1], bandno);
  482. if (y > y0 + rlen)
  483. ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[y][x] & mask ? 1:0);
  484. if (t1->data[y][x] & mask){ // newly significant
  485. int xorbit;
  486. int ctxno = ff_j2k_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
  487. *nmsedec += getnmsedec_sig(t1->data[y][x], bpno + NMSEDEC_FRACBITS);
  488. ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[y+1][x+1] >> 15) ^ xorbit);
  489. ff_j2k_set_significant(t1, x, y, t1->flags[y+1][x+1] >> 15);
  490. }
  491. }
  492. t1->flags[y+1][x+1] &= ~J2K_T1_VIS;
  493. }
  494. } else{
  495. for (y = y0; y < y0 + 4 && y < height; y++){
  496. if (!(t1->flags[y+1][x+1] & (J2K_T1_SIG | J2K_T1_VIS))){
  497. int ctxno = ff_j2k_getnbctxno(t1->flags[y+1][x+1], bandno);
  498. ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[y][x] & mask ? 1:0);
  499. if (t1->data[y][x] & mask){ // newly significant
  500. int xorbit;
  501. int ctxno = ff_j2k_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
  502. *nmsedec += getnmsedec_sig(t1->data[y][x], bpno + NMSEDEC_FRACBITS);
  503. ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[y+1][x+1] >> 15) ^ xorbit);
  504. ff_j2k_set_significant(t1, x, y, t1->flags[y+1][x+1] >> 15);
  505. }
  506. }
  507. t1->flags[y+1][x+1] &= ~J2K_T1_VIS;
  508. }
  509. }
  510. }
  511. }
  512. static void encode_cblk(J2kEncoderContext *s, J2kT1Context *t1, J2kCblk *cblk, J2kTile *tile,
  513. int width, int height, int bandpos, int lev)
  514. {
  515. int pass_t = 2, passno, x, y, max=0, nmsedec, bpno;
  516. int64_t wmsedec = 0;
  517. for (y = 0; y < height+2; y++)
  518. memset(t1->flags[y], 0, (width+2)*sizeof(int));
  519. for (y = 0; y < height; y++){
  520. for (x = 0; x < width; x++){
  521. if (t1->data[y][x] < 0){
  522. t1->flags[y+1][x+1] |= J2K_T1_SGN;
  523. t1->data[y][x] = -t1->data[y][x];
  524. }
  525. max = FFMAX(max, t1->data[y][x]);
  526. }
  527. }
  528. if (max == 0){
  529. cblk->nonzerobits = 0;
  530. bpno = 0;
  531. } else{
  532. cblk->nonzerobits = av_log2(max) + 1 - NMSEDEC_FRACBITS;
  533. bpno = cblk->nonzerobits - 1;
  534. }
  535. ff_mqc_initenc(&t1->mqc, cblk->data);
  536. for (passno = 0; bpno >= 0; passno++){
  537. nmsedec=0;
  538. switch(pass_t){
  539. case 0: encode_sigpass(t1, width, height, bandpos, &nmsedec, bpno);
  540. break;
  541. case 1: encode_refpass(t1, width, height, &nmsedec, bpno);
  542. break;
  543. case 2: encode_clnpass(t1, width, height, bandpos, &nmsedec, bpno);
  544. break;
  545. }
  546. cblk->passes[passno].rate = 3 + ff_mqc_length(&t1->mqc);
  547. wmsedec += (int64_t)nmsedec << (2*bpno);
  548. cblk->passes[passno].disto = wmsedec;
  549. if (++pass_t == 3){
  550. pass_t = 0;
  551. bpno--;
  552. }
  553. }
  554. cblk->npasses = passno;
  555. cblk->ninclpasses = passno;
  556. // TODO: optional flush on each pass
  557. cblk->passes[passno-1].rate = ff_mqc_flush(&t1->mqc);
  558. }
  559. /* tier-2 routines: */
  560. static void putnumpasses(J2kEncoderContext *s, int n)
  561. {
  562. if (n == 1)
  563. put_num(s, 0, 1);
  564. else if (n == 2)
  565. put_num(s, 2, 2);
  566. else if (n <= 5)
  567. put_num(s, 0xc | (n-3), 4);
  568. else if (n <= 36)
  569. put_num(s, 0x1e0 | (n-6), 9);
  570. else
  571. put_num(s, 0xff80 | (n-37), 16);
  572. }
  573. static int encode_packet(J2kEncoderContext *s, J2kResLevel *rlevel, int precno,
  574. uint8_t *expn, int numgbits)
  575. {
  576. int bandno, empty = 1;
  577. // init bitstream
  578. *s->buf = 0;
  579. s->bit_index = 0;
  580. // header
  581. // is the packet empty?
  582. for (bandno = 0; bandno < rlevel->nbands; bandno++){
  583. if (rlevel->band[bandno].coord[0][0] < rlevel->band[bandno].coord[0][1]
  584. && rlevel->band[bandno].coord[1][0] < rlevel->band[bandno].coord[1][1]){
  585. empty = 0;
  586. break;
  587. }
  588. }
  589. put_bits(s, !empty, 1);
  590. if (empty){
  591. j2k_flush(s);
  592. return 0;
  593. }
  594. for (bandno = 0; bandno < rlevel->nbands; bandno++){
  595. J2kBand *band = rlevel->band + bandno;
  596. J2kPrec *prec = band->prec + precno;
  597. int yi, xi, pos;
  598. int cblknw = prec->xi1 - prec->xi0;
  599. if (band->coord[0][0] == band->coord[0][1]
  600. || band->coord[1][0] == band->coord[1][1])
  601. continue;
  602. for (pos=0, yi = prec->yi0; yi < prec->yi1; yi++){
  603. for (xi = prec->xi0; xi < prec->xi1; xi++, pos++){
  604. prec->cblkincl[pos].val = band->cblk[yi * cblknw + xi].ninclpasses == 0;
  605. tag_tree_update(prec->cblkincl + pos);
  606. prec->zerobits[pos].val = expn[bandno] + numgbits - 1 - band->cblk[yi * cblknw + xi].nonzerobits;
  607. tag_tree_update(prec->zerobits + pos);
  608. }
  609. }
  610. for (pos=0, yi = prec->yi0; yi < prec->yi1; yi++){
  611. for (xi = prec->xi0; xi < prec->xi1; xi++, pos++){
  612. int pad = 0, llen, length;
  613. J2kCblk *cblk = band->cblk + yi * cblknw + xi;
  614. if (s->buf_end - s->buf < 20) // approximately
  615. return -1;
  616. // inclusion information
  617. tag_tree_code(s, prec->cblkincl + pos, 1);
  618. if (!cblk->ninclpasses)
  619. continue;
  620. // zerobits information
  621. tag_tree_code(s, prec->zerobits + pos, 100);
  622. // number of passes
  623. putnumpasses(s, cblk->ninclpasses);
  624. length = cblk->passes[cblk->ninclpasses-1].rate;
  625. llen = av_log2(length) - av_log2(cblk->ninclpasses) - 2;
  626. if (llen < 0){
  627. pad = -llen;
  628. llen = 0;
  629. }
  630. // length of code block
  631. put_bits(s, 1, llen);
  632. put_bits(s, 0, 1);
  633. put_num(s, length, av_log2(length)+1+pad);
  634. }
  635. }
  636. }
  637. j2k_flush(s);
  638. for (bandno = 0; bandno < rlevel->nbands; bandno++){
  639. J2kBand *band = rlevel->band + bandno;
  640. J2kPrec *prec = band->prec + precno;
  641. int yi, cblknw = prec->xi1 - prec->xi0;
  642. for (yi = prec->yi0; yi < prec->yi1; yi++){
  643. int xi;
  644. for (xi = prec->xi0; xi < prec->xi1; xi++){
  645. J2kCblk *cblk = band->cblk + yi * cblknw + xi;
  646. if (cblk->ninclpasses){
  647. if (s->buf_end - s->buf < cblk->passes[cblk->ninclpasses-1].rate)
  648. return -1;
  649. bytestream_put_buffer(&s->buf, cblk->data, cblk->passes[cblk->ninclpasses-1].rate);
  650. }
  651. }
  652. }
  653. }
  654. return 0;
  655. }
  656. static int encode_packets(J2kEncoderContext *s, J2kTile *tile, int tileno)
  657. {
  658. int compno, reslevelno, ret;
  659. J2kCodingStyle *codsty = &s->codsty;
  660. J2kQuantStyle *qntsty = &s->qntsty;
  661. av_log(s->avctx, AV_LOG_DEBUG, "tier2\n");
  662. // lay-rlevel-comp-pos progression
  663. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
  664. for (compno = 0; compno < s->ncomponents; compno++){
  665. int precno;
  666. J2kResLevel *reslevel = s->tile[tileno].comp[compno].reslevel + reslevelno;
  667. for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
  668. if (ret = encode_packet(s, reslevel, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
  669. qntsty->nguardbits))
  670. return ret;
  671. }
  672. }
  673. }
  674. av_log(s->avctx, AV_LOG_DEBUG, "after tier2\n");
  675. return 0;
  676. }
  677. static int getcut(J2kCblk *cblk, int64_t lambda, int dwt_norm)
  678. {
  679. int passno, res = 0;
  680. for (passno = 0; passno < cblk->npasses; passno++){
  681. int dr;
  682. int64_t dd;
  683. dr = cblk->passes[passno].rate
  684. - (res ? cblk->passes[res-1].rate:0);
  685. dd = cblk->passes[passno].disto
  686. - (res ? cblk->passes[res-1].disto:0);
  687. if (((dd * dwt_norm) >> WMSEDEC_SHIFT) * dwt_norm >= dr * lambda)
  688. res = passno+1;
  689. }
  690. return res;
  691. }
  692. static void truncpasses(J2kEncoderContext *s, J2kTile *tile)
  693. {
  694. int compno, reslevelno, bandno, cblkno, lev;
  695. J2kCodingStyle *codsty = &s->codsty;
  696. for (compno = 0; compno < s->ncomponents; compno++){
  697. J2kComponent *comp = tile->comp + compno;
  698. for (reslevelno = 0, lev = codsty->nreslevels-1; reslevelno < codsty->nreslevels; reslevelno++, lev--){
  699. J2kResLevel *reslevel = comp->reslevel + reslevelno;
  700. for (bandno = 0; bandno < reslevel->nbands ; bandno++){
  701. int bandpos = bandno + (reslevelno > 0);
  702. J2kBand *band = reslevel->band + bandno;
  703. for (cblkno = 0; cblkno < band->cblknx * band->cblkny; cblkno++){
  704. J2kCblk *cblk = band->cblk + cblkno;
  705. cblk->ninclpasses = getcut(cblk, s->lambda,
  706. (int64_t)dwt_norms[codsty->transform][bandpos][lev] * (int64_t)band->stepsize >> 13);
  707. }
  708. }
  709. }
  710. }
  711. }
  712. static int encode_tile(J2kEncoderContext *s, J2kTile *tile, int tileno)
  713. {
  714. int compno, reslevelno, bandno, ret;
  715. J2kT1Context t1;
  716. J2kCodingStyle *codsty = &s->codsty;
  717. for (compno = 0; compno < s->ncomponents; compno++){
  718. J2kComponent *comp = s->tile[tileno].comp + compno;
  719. av_log(s->avctx, AV_LOG_DEBUG,"dwt\n");
  720. if (ret = ff_dwt_encode(&comp->dwt, comp->data))
  721. return ret;
  722. av_log(s->avctx, AV_LOG_DEBUG,"after dwt -> tier1\n");
  723. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
  724. J2kResLevel *reslevel = comp->reslevel + reslevelno;
  725. for (bandno = 0; bandno < reslevel->nbands ; bandno++){
  726. J2kBand *band = reslevel->band + bandno;
  727. int cblkx, cblky, cblkno=0, xx0, x0, xx1, y0, yy0, yy1, bandpos;
  728. yy0 = bandno == 0 ? 0 : comp->reslevel[reslevelno-1].coord[1][1] - comp->reslevel[reslevelno-1].coord[1][0];
  729. y0 = yy0;
  730. yy1 = FFMIN(ff_j2k_ceildiv(band->coord[1][0] + 1, band->codeblock_height) * band->codeblock_height,
  731. band->coord[1][1]) - band->coord[1][0] + yy0;
  732. if (band->coord[0][0] == band->coord[0][1] || band->coord[1][0] == band->coord[1][1])
  733. continue;
  734. bandpos = bandno + (reslevelno > 0);
  735. for (cblky = 0; cblky < band->cblkny; cblky++){
  736. if (reslevelno == 0 || bandno == 1)
  737. xx0 = 0;
  738. else
  739. xx0 = comp->reslevel[reslevelno-1].coord[0][1] - comp->reslevel[reslevelno-1].coord[0][0];
  740. x0 = xx0;
  741. xx1 = FFMIN(ff_j2k_ceildiv(band->coord[0][0] + 1, band->codeblock_width) * band->codeblock_width,
  742. band->coord[0][1]) - band->coord[0][0] + xx0;
  743. for (cblkx = 0; cblkx < band->cblknx; cblkx++, cblkno++){
  744. int y, x;
  745. if (codsty->transform == FF_DWT53){
  746. for (y = yy0; y < yy1; y++){
  747. int *ptr = t1.data[y-yy0];
  748. for (x = xx0; x < xx1; x++){
  749. *ptr++ = comp->data[(comp->coord[0][1] - comp->coord[0][0]) * y + x] << NMSEDEC_FRACBITS;
  750. }
  751. }
  752. } else{
  753. for (y = yy0; y < yy1; y++){
  754. int *ptr = t1.data[y-yy0];
  755. for (x = xx0; x < xx1; x++){
  756. *ptr = (comp->data[(comp->coord[0][1] - comp->coord[0][0]) * y + x]);
  757. *ptr++ = (int64_t)*ptr * (int64_t)(8192 * 8192 / band->stepsize) >> 13 - NMSEDEC_FRACBITS;
  758. }
  759. }
  760. }
  761. encode_cblk(s, &t1, band->cblk + cblkno, tile, xx1 - xx0, yy1 - yy0,
  762. bandpos, codsty->nreslevels - reslevelno - 1);
  763. xx0 = xx1;
  764. xx1 = FFMIN(xx1 + band->codeblock_width, band->coord[0][1] - band->coord[0][0] + x0);
  765. }
  766. yy0 = yy1;
  767. yy1 = FFMIN(yy1 + band->codeblock_height, band->coord[1][1] - band->coord[1][0] + y0);
  768. }
  769. }
  770. }
  771. av_log(s->avctx, AV_LOG_DEBUG, "after tier1\n");
  772. }
  773. av_log(s->avctx, AV_LOG_DEBUG, "rate control\n");
  774. truncpasses(s, tile);
  775. if (ret = encode_packets(s, tile, tileno))
  776. return ret;
  777. av_log(s->avctx, AV_LOG_DEBUG, "after rate control\n");
  778. return 0;
  779. }
  780. void cleanup(J2kEncoderContext *s)
  781. {
  782. int tileno, compno;
  783. J2kCodingStyle *codsty = &s->codsty;
  784. for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
  785. for (compno = 0; compno < s->ncomponents; compno++){
  786. J2kComponent *comp = s->tile[tileno].comp + compno;
  787. ff_j2k_cleanup(comp, codsty);
  788. }
  789. av_freep(&s->tile[tileno].comp);
  790. }
  791. av_freep(&s->tile);
  792. }
  793. static void reinit(J2kEncoderContext *s)
  794. {
  795. int tileno, compno;
  796. for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
  797. J2kTile *tile = s->tile + tileno;
  798. for (compno = 0; compno < s->ncomponents; compno++)
  799. ff_j2k_reinit(tile->comp + compno, &s->codsty);
  800. }
  801. }
  802. static int encode_frame(AVCodecContext *avctx,
  803. uint8_t *buf, int buf_size,
  804. void *data)
  805. {
  806. int tileno, ret;
  807. J2kEncoderContext *s = avctx->priv_data;
  808. // init:
  809. s->buf = s->buf_start = buf;
  810. s->buf_end = buf + buf_size;
  811. s->picture = data;
  812. s->lambda = s->picture->quality * LAMBDA_SCALE;
  813. copy_frame(s);
  814. reinit(s);
  815. if (s->buf_end - s->buf < 2)
  816. return -1;
  817. bytestream_put_be16(&s->buf, J2K_SOC);
  818. if (ret = put_siz(s))
  819. return ret;
  820. if (ret = put_cod(s))
  821. return ret;
  822. if (ret = put_qcd(s, 0))
  823. return ret;
  824. for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
  825. uint8_t *psotptr;
  826. if ((psotptr = put_sot(s, tileno)) < 0)
  827. return psotptr;
  828. if (s->buf_end - s->buf < 2)
  829. return -1;
  830. bytestream_put_be16(&s->buf, J2K_SOD);
  831. if (ret = encode_tile(s, s->tile + tileno, tileno))
  832. return ret;
  833. bytestream_put_be32(&psotptr, s->buf - psotptr + 6);
  834. }
  835. if (s->buf_end - s->buf < 2)
  836. return -1;
  837. bytestream_put_be16(&s->buf, J2K_EOC);
  838. av_log(s->avctx, AV_LOG_DEBUG, "end\n");
  839. return s->buf - s->buf_start;
  840. }
  841. static av_cold int j2kenc_init(AVCodecContext *avctx)
  842. {
  843. int i, ret;
  844. J2kEncoderContext *s = avctx->priv_data;
  845. J2kCodingStyle *codsty = &s->codsty;
  846. J2kQuantStyle *qntsty = &s->qntsty;
  847. s->avctx = avctx;
  848. av_log(s->avctx, AV_LOG_DEBUG, "init\n");
  849. // defaults:
  850. // TODO: implement setting non-standard precinct size
  851. codsty->log2_prec_width = 15;
  852. codsty->log2_prec_height = 15;
  853. codsty->nreslevels = 7;
  854. codsty->log2_cblk_width = 4;
  855. codsty->log2_cblk_height = 4;
  856. codsty->transform = 1;
  857. qntsty->nguardbits = 1;
  858. s->tile_width = 256;
  859. s->tile_height = 256;
  860. if (codsty->transform == FF_DWT53)
  861. qntsty->quantsty = J2K_QSTY_NONE;
  862. else
  863. qntsty->quantsty = J2K_QSTY_SE;
  864. s->width = avctx->width;
  865. s->height = avctx->height;
  866. for (i = 0; i < 3; i++)
  867. s->cbps[i] = 8;
  868. if (avctx->pix_fmt == PIX_FMT_RGB24){
  869. s->ncomponents = 3;
  870. } else if (avctx->pix_fmt == PIX_FMT_GRAY8){
  871. s->ncomponents = 1;
  872. } else{ // planar YUV
  873. s->planar = 1;
  874. s->ncomponents = 3;
  875. avcodec_get_chroma_sub_sample(avctx->pix_fmt,
  876. s->chroma_shift, s->chroma_shift + 1);
  877. }
  878. ff_j2k_init_tier1_luts();
  879. init_luts();
  880. init_quantization(s);
  881. if (ret=init_tiles(s))
  882. return ret;
  883. av_log(s->avctx, AV_LOG_DEBUG, "after init\n");
  884. return 0;
  885. }
  886. static int j2kenc_destroy(AVCodecContext *avctx)
  887. {
  888. J2kEncoderContext *s = avctx->priv_data;
  889. cleanup(s);
  890. return 0;
  891. }
  892. AVCodec jpeg2000_encoder = {
  893. "j2k",
  894. CODEC_TYPE_VIDEO,
  895. CODEC_ID_JPEG2000,
  896. sizeof(J2kEncoderContext),
  897. j2kenc_init,
  898. encode_frame,
  899. j2kenc_destroy,
  900. NULL,
  901. 0,
  902. .pix_fmts =
  903. (enum PixelFormat[]) {PIX_FMT_GRAY8, PIX_FMT_RGB24,
  904. PIX_FMT_YUV422P, PIX_FMT_YUV444P,
  905. PIX_FMT_YUV410P, PIX_FMT_YUV411P,
  906. -1}
  907. };