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.

1057 lines
38KB

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