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.

1047 lines
37KB

  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. J2kComponent *comp;
  52. } J2kTile;
  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. J2kCodingStyle codsty;
  69. J2kQuantStyle qntsty;
  70. J2kTile *tile;
  71. } J2kEncoderContext;
  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(J2kComponent *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(J2kEncoderContext *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. J2kTile *tile = s->tile + tileno;
  96. nspaces(fd, 2);
  97. fprintf(fd, "tile %d:\n", tileno);
  98. for(compno = 0; compno < s->ncomponents; compno++){
  99. J2kComponent *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. J2kResLevel *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. J2kBand *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. J2kPrec *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(J2kEncoderContext *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(J2kEncoderContext *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(J2kEncoderContext *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(J2kEncoderContext *s, J2kTgtNode *node, int threshold)
  168. {
  169. J2kTgtNode *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(J2kTgtNode *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(J2kEncoderContext *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, J2K_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(J2kEncoderContext *s)
  229. {
  230. J2kCodingStyle *codsty = &s->codsty;
  231. if (s->buf_end - s->buf < 14)
  232. return -1;
  233. bytestream_put_be16(&s->buf, J2K_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(J2kEncoderContext *s, int compno)
  253. {
  254. int i, size;
  255. J2kCodingStyle *codsty = &s->codsty;
  256. J2kQuantStyle *qntsty = &s->qntsty;
  257. if (qntsty->quantsty == J2K_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, J2K_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 == J2K_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(J2kEncoderContext *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, J2K_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(J2kEncoderContext *s)
  294. {
  295. int tileno, tilex, tiley, compno;
  296. J2kCodingStyle *codsty = &s->codsty;
  297. J2kQuantStyle *qntsty = &s->qntsty;
  298. s->numXtiles = ff_j2k_ceildiv(s->width, s->tile_width);
  299. s->numYtiles = ff_j2k_ceildiv(s->height, s->tile_height);
  300. s->tile = av_malloc(s->numXtiles * s->numYtiles * sizeof(J2kTile));
  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. J2kTile *tile = s->tile + tileno;
  306. tile->comp = av_malloc(s->ncomponents * sizeof(J2kComponent));
  307. if (!tile->comp)
  308. return AVERROR(ENOMEM);
  309. for (compno = 0; compno < s->ncomponents; compno++){
  310. J2kComponent *comp = tile->comp + compno;
  311. int ret, i, j;
  312. comp->coord[0][0] = tilex * s->tile_width;
  313. comp->coord[0][1] = FFMIN((tilex+1)*s->tile_width, s->width);
  314. comp->coord[1][0] = tiley * s->tile_height;
  315. comp->coord[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] = ff_j2k_ceildivpow2(comp->coord[i][j], s->chroma_shift[i]);
  320. if (ret = ff_j2k_init_component(comp, codsty, qntsty, s->cbps[compno], compno?1<<s->chroma_shift[0]:1, compno?1<<s->chroma_shift[1]:1))
  321. return ret;
  322. }
  323. }
  324. return 0;
  325. }
  326. static void copy_frame(J2kEncoderContext *s)
  327. {
  328. int tileno, compno, i, y, x;
  329. uint8_t *line;
  330. for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
  331. J2kTile *tile = s->tile + tileno;
  332. if (s->planar){
  333. for (compno = 0; compno < s->ncomponents; compno++){
  334. J2kComponent *comp = tile->comp + compno;
  335. int *dst = comp->data;
  336. line = s->picture.data[compno]
  337. + comp->coord[1][0] * s->picture.linesize[compno]
  338. + comp->coord[0][0];
  339. for (y = comp->coord[1][0]; y < comp->coord[1][1]; y++){
  340. uint8_t *ptr = line;
  341. for (x = comp->coord[0][0]; x < comp->coord[0][1]; x++)
  342. *dst++ = *ptr++ - (1 << 7);
  343. line += s->picture.linesize[compno];
  344. }
  345. }
  346. } else{
  347. line = s->picture.data[0] + tile->comp[0].coord[1][0] * s->picture.linesize[0]
  348. + tile->comp[0].coord[0][0] * s->ncomponents;
  349. i = 0;
  350. for (y = tile->comp[0].coord[1][0]; y < tile->comp[0].coord[1][1]; y++){
  351. uint8_t *ptr = line;
  352. for (x = tile->comp[0].coord[0][0]; x < tile->comp[0].coord[0][1]; x++, i++){
  353. for (compno = 0; compno < s->ncomponents; compno++){
  354. tile->comp[compno].data[i] = *ptr++ - (1 << 7);
  355. }
  356. }
  357. line += s->picture.linesize[0];
  358. }
  359. }
  360. }
  361. }
  362. static void init_quantization(J2kEncoderContext *s)
  363. {
  364. int compno, reslevelno, bandno;
  365. J2kQuantStyle *qntsty = &s->qntsty;
  366. J2kCodingStyle *codsty = &s->codsty;
  367. for (compno = 0; compno < s->ncomponents; compno++){
  368. int gbandno = 0;
  369. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
  370. int nbands, lev = codsty->nreslevels - reslevelno - 1;
  371. nbands = reslevelno ? 3 : 1;
  372. for (bandno = 0; bandno < nbands; bandno++, gbandno++){
  373. int expn, mant;
  374. if (codsty->transform == FF_DWT97){
  375. int bandpos = bandno + (reslevelno>0),
  376. ss = 81920000 / dwt_norms[0][bandpos][lev],
  377. log = av_log2(ss);
  378. mant = (11 - log < 0 ? ss >> log - 11 : ss << 11 - log) & 0x7ff;
  379. expn = s->cbps[compno] - log + 13;
  380. } else
  381. expn = ((bandno&2)>>1) + (reslevelno>0) + s->cbps[compno];
  382. qntsty->expn[gbandno] = expn;
  383. qntsty->mant[gbandno] = mant;
  384. }
  385. }
  386. }
  387. }
  388. static void init_luts(void)
  389. {
  390. int i, a,
  391. mask = ~((1<<NMSEDEC_FRACBITS)-1);
  392. for (i = 0; i < (1 << NMSEDEC_BITS); i++){
  393. lut_nmsedec_sig[i] = FFMAX(6*i - (9<<NMSEDEC_FRACBITS-1) << 12-NMSEDEC_FRACBITS, 0);
  394. lut_nmsedec_sig0[i] = FFMAX((i*i + (1<<NMSEDEC_FRACBITS-1) & mask) << 1, 0);
  395. a = (i >> (NMSEDEC_BITS-2)&2) + 1;
  396. lut_nmsedec_ref[i] = FFMAX((-2*i + (1<<NMSEDEC_FRACBITS) + a*i - (a*a<<NMSEDEC_FRACBITS-2))
  397. << 13-NMSEDEC_FRACBITS, 0);
  398. lut_nmsedec_ref0[i] = FFMAX(((i*i + (1-4*i << NMSEDEC_FRACBITS-1) + (1<<2*NMSEDEC_FRACBITS)) & mask)
  399. << 1, 0);
  400. }
  401. }
  402. /* tier-1 routines */
  403. static int getnmsedec_sig(int x, int bpno)
  404. {
  405. if (bpno > NMSEDEC_FRACBITS)
  406. return lut_nmsedec_sig[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
  407. return lut_nmsedec_sig0[x & ((1 << NMSEDEC_BITS) - 1)];
  408. }
  409. static int getnmsedec_ref(int x, int bpno)
  410. {
  411. if (bpno > NMSEDEC_FRACBITS)
  412. return lut_nmsedec_ref[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
  413. return lut_nmsedec_ref0[x & ((1 << NMSEDEC_BITS) - 1)];
  414. }
  415. static void encode_sigpass(J2kT1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
  416. {
  417. int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
  418. int vert_causal_ctx_csty_loc_symbol;
  419. for (y0 = 0; y0 < height; y0 += 4)
  420. for (x = 0; x < width; x++)
  421. for (y = y0; y < height && y < y0+4; y++){
  422. if (!(t1->flags[y+1][x+1] & J2K_T1_SIG) && (t1->flags[y+1][x+1] & J2K_T1_SIG_NB)){
  423. int ctxno = ff_j2k_getnbctxno(t1->flags[y+1][x+1], bandno, vert_causal_ctx_csty_loc_symbol),
  424. bit = t1->data[y][x] & mask ? 1 : 0;
  425. ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, bit);
  426. if (bit){
  427. int xorbit;
  428. int ctxno = ff_j2k_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
  429. ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[y+1][x+1] >> 15) ^ xorbit);
  430. *nmsedec += getnmsedec_sig(t1->data[y][x], bpno + NMSEDEC_FRACBITS);
  431. ff_j2k_set_significant(t1, x, y, t1->flags[y+1][x+1] >> 15);
  432. }
  433. t1->flags[y+1][x+1] |= J2K_T1_VIS;
  434. }
  435. }
  436. }
  437. static void encode_refpass(J2kT1Context *t1, int width, int height, int *nmsedec, int bpno)
  438. {
  439. int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
  440. for (y0 = 0; y0 < height; y0 += 4)
  441. for (x = 0; x < width; x++)
  442. for (y = y0; y < height && y < y0+4; y++)
  443. if ((t1->flags[y+1][x+1] & (J2K_T1_SIG | J2K_T1_VIS)) == J2K_T1_SIG){
  444. int ctxno = ff_j2k_getrefctxno(t1->flags[y+1][x+1]);
  445. *nmsedec += getnmsedec_ref(t1->data[y][x], bpno + NMSEDEC_FRACBITS);
  446. ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[y][x] & mask ? 1:0);
  447. t1->flags[y+1][x+1] |= J2K_T1_REF;
  448. }
  449. }
  450. static void encode_clnpass(J2kT1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
  451. {
  452. int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
  453. int vert_causal_ctx_csty_loc_symbol;
  454. for (y0 = 0; y0 < height; y0 += 4)
  455. for (x = 0; x < width; x++){
  456. if (y0 + 3 < height && !(
  457. (t1->flags[y0+1][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG)) ||
  458. (t1->flags[y0+2][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG)) ||
  459. (t1->flags[y0+3][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG)) ||
  460. (t1->flags[y0+4][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG))))
  461. {
  462. // aggregation mode
  463. int rlen;
  464. for (rlen = 0; rlen < 4; rlen++)
  465. if (t1->data[y0+rlen][x] & mask)
  466. break;
  467. ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL, rlen != 4);
  468. if (rlen == 4)
  469. continue;
  470. ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen >> 1);
  471. ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen & 1);
  472. for (y = y0 + rlen; y < y0 + 4; y++){
  473. if (!(t1->flags[y+1][x+1] & (J2K_T1_SIG | J2K_T1_VIS))){
  474. int ctxno = ff_j2k_getnbctxno(t1->flags[y+1][x+1], bandno, vert_causal_ctx_csty_loc_symbol);
  475. if (y > y0 + rlen)
  476. ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[y][x] & mask ? 1:0);
  477. if (t1->data[y][x] & mask){ // newly significant
  478. int xorbit;
  479. int ctxno = ff_j2k_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
  480. *nmsedec += getnmsedec_sig(t1->data[y][x], bpno + NMSEDEC_FRACBITS);
  481. ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[y+1][x+1] >> 15) ^ xorbit);
  482. ff_j2k_set_significant(t1, x, y, t1->flags[y+1][x+1] >> 15);
  483. }
  484. }
  485. t1->flags[y+1][x+1] &= ~J2K_T1_VIS;
  486. }
  487. } else{
  488. for (y = y0; y < y0 + 4 && y < height; y++){
  489. if (!(t1->flags[y+1][x+1] & (J2K_T1_SIG | J2K_T1_VIS))){
  490. int ctxno = ff_j2k_getnbctxno(t1->flags[y+1][x+1], bandno, vert_causal_ctx_csty_loc_symbol);
  491. ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[y][x] & mask ? 1:0);
  492. if (t1->data[y][x] & mask){ // newly significant
  493. int xorbit;
  494. int ctxno = ff_j2k_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
  495. *nmsedec += getnmsedec_sig(t1->data[y][x], bpno + NMSEDEC_FRACBITS);
  496. ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[y+1][x+1] >> 15) ^ xorbit);
  497. ff_j2k_set_significant(t1, x, y, t1->flags[y+1][x+1] >> 15);
  498. }
  499. }
  500. t1->flags[y+1][x+1] &= ~J2K_T1_VIS;
  501. }
  502. }
  503. }
  504. }
  505. static void encode_cblk(J2kEncoderContext *s, J2kT1Context *t1, J2kCblk *cblk, J2kTile *tile,
  506. int width, int height, int bandpos, int lev)
  507. {
  508. int pass_t = 2, passno, x, y, max=0, nmsedec, bpno;
  509. int64_t wmsedec = 0;
  510. for (y = 0; y < height+2; y++)
  511. memset(t1->flags[y], 0, (width+2)*sizeof(int));
  512. for (y = 0; y < height; y++){
  513. for (x = 0; x < width; x++){
  514. if (t1->data[y][x] < 0){
  515. t1->flags[y+1][x+1] |= J2K_T1_SGN;
  516. t1->data[y][x] = -t1->data[y][x];
  517. }
  518. max = FFMAX(max, t1->data[y][x]);
  519. }
  520. }
  521. if (max == 0){
  522. cblk->nonzerobits = 0;
  523. bpno = 0;
  524. } else{
  525. cblk->nonzerobits = av_log2(max) + 1 - NMSEDEC_FRACBITS;
  526. bpno = cblk->nonzerobits - 1;
  527. }
  528. ff_mqc_initenc(&t1->mqc, cblk->data);
  529. for (passno = 0; bpno >= 0; passno++){
  530. nmsedec=0;
  531. switch(pass_t){
  532. case 0: encode_sigpass(t1, width, height, bandpos, &nmsedec, bpno);
  533. break;
  534. case 1: encode_refpass(t1, width, height, &nmsedec, bpno);
  535. break;
  536. case 2: encode_clnpass(t1, width, height, bandpos, &nmsedec, bpno);
  537. break;
  538. }
  539. cblk->passes[passno].rate = 3 + ff_mqc_length(&t1->mqc);
  540. wmsedec += (int64_t)nmsedec << (2*bpno);
  541. cblk->passes[passno].disto = wmsedec;
  542. if (++pass_t == 3){
  543. pass_t = 0;
  544. bpno--;
  545. }
  546. }
  547. cblk->npasses = passno;
  548. cblk->ninclpasses = passno;
  549. // TODO: optional flush on each pass
  550. cblk->passes[passno-1].rate = ff_mqc_flush(&t1->mqc);
  551. }
  552. /* tier-2 routines: */
  553. static void putnumpasses(J2kEncoderContext *s, int n)
  554. {
  555. if (n == 1)
  556. put_num(s, 0, 1);
  557. else if (n == 2)
  558. put_num(s, 2, 2);
  559. else if (n <= 5)
  560. put_num(s, 0xc | (n-3), 4);
  561. else if (n <= 36)
  562. put_num(s, 0x1e0 | (n-6), 9);
  563. else
  564. put_num(s, 0xff80 | (n-37), 16);
  565. }
  566. static int encode_packet(J2kEncoderContext *s, J2kResLevel *rlevel, int precno,
  567. uint8_t *expn, int numgbits)
  568. {
  569. int bandno, empty = 1;
  570. // init bitstream
  571. *s->buf = 0;
  572. s->bit_index = 0;
  573. // header
  574. // is the packet empty?
  575. for (bandno = 0; bandno < rlevel->nbands; bandno++){
  576. if (rlevel->band[bandno].coord[0][0] < rlevel->band[bandno].coord[0][1]
  577. && rlevel->band[bandno].coord[1][0] < rlevel->band[bandno].coord[1][1]){
  578. empty = 0;
  579. break;
  580. }
  581. }
  582. put_bits(s, !empty, 1);
  583. if (empty){
  584. j2k_flush(s);
  585. return 0;
  586. }
  587. for (bandno = 0; bandno < rlevel->nbands; bandno++){
  588. J2kBand *band = rlevel->band + bandno;
  589. J2kPrec *prec = band->prec + precno;
  590. int yi, xi, pos;
  591. int cblknw = prec->xi1 - prec->xi0;
  592. if (band->coord[0][0] == band->coord[0][1]
  593. || band->coord[1][0] == band->coord[1][1])
  594. continue;
  595. for (pos=0, yi = prec->yi0; yi < prec->yi1; yi++){
  596. for (xi = prec->xi0; xi < prec->xi1; xi++, pos++){
  597. prec->cblkincl[pos].val = band->cblk[yi * cblknw + xi].ninclpasses == 0;
  598. tag_tree_update(prec->cblkincl + pos);
  599. prec->zerobits[pos].val = expn[bandno] + numgbits - 1 - band->cblk[yi * cblknw + xi].nonzerobits;
  600. tag_tree_update(prec->zerobits + pos);
  601. }
  602. }
  603. for (pos=0, yi = prec->yi0; yi < prec->yi1; yi++){
  604. for (xi = prec->xi0; xi < prec->xi1; xi++, pos++){
  605. int pad = 0, llen, length;
  606. J2kCblk *cblk = band->cblk + yi * cblknw + xi;
  607. if (s->buf_end - s->buf < 20) // approximately
  608. return -1;
  609. // inclusion information
  610. tag_tree_code(s, prec->cblkincl + pos, 1);
  611. if (!cblk->ninclpasses)
  612. continue;
  613. // zerobits information
  614. tag_tree_code(s, prec->zerobits + pos, 100);
  615. // number of passes
  616. putnumpasses(s, cblk->ninclpasses);
  617. length = cblk->passes[cblk->ninclpasses-1].rate;
  618. llen = av_log2(length) - av_log2(cblk->ninclpasses) - 2;
  619. if (llen < 0){
  620. pad = -llen;
  621. llen = 0;
  622. }
  623. // length of code block
  624. put_bits(s, 1, llen);
  625. put_bits(s, 0, 1);
  626. put_num(s, length, av_log2(length)+1+pad);
  627. }
  628. }
  629. }
  630. j2k_flush(s);
  631. for (bandno = 0; bandno < rlevel->nbands; bandno++){
  632. J2kBand *band = rlevel->band + bandno;
  633. J2kPrec *prec = band->prec + precno;
  634. int yi, cblknw = prec->xi1 - prec->xi0;
  635. for (yi = prec->yi0; yi < prec->yi1; yi++){
  636. int xi;
  637. for (xi = prec->xi0; xi < prec->xi1; xi++){
  638. J2kCblk *cblk = band->cblk + yi * cblknw + xi;
  639. if (cblk->ninclpasses){
  640. if (s->buf_end - s->buf < cblk->passes[cblk->ninclpasses-1].rate)
  641. return -1;
  642. bytestream_put_buffer(&s->buf, cblk->data, cblk->passes[cblk->ninclpasses-1].rate);
  643. }
  644. }
  645. }
  646. }
  647. return 0;
  648. }
  649. static int encode_packets(J2kEncoderContext *s, J2kTile *tile, int tileno)
  650. {
  651. int compno, reslevelno, ret;
  652. J2kCodingStyle *codsty = &s->codsty;
  653. J2kQuantStyle *qntsty = &s->qntsty;
  654. av_log(s->avctx, AV_LOG_DEBUG, "tier2\n");
  655. // lay-rlevel-comp-pos progression
  656. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
  657. for (compno = 0; compno < s->ncomponents; compno++){
  658. int precno;
  659. J2kResLevel *reslevel = s->tile[tileno].comp[compno].reslevel + reslevelno;
  660. for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
  661. if (ret = encode_packet(s, reslevel, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
  662. qntsty->nguardbits))
  663. return ret;
  664. }
  665. }
  666. }
  667. av_log(s->avctx, AV_LOG_DEBUG, "after tier2\n");
  668. return 0;
  669. }
  670. static int getcut(J2kCblk *cblk, int64_t lambda, int dwt_norm)
  671. {
  672. int passno, res = 0;
  673. for (passno = 0; passno < cblk->npasses; passno++){
  674. int dr;
  675. int64_t dd;
  676. dr = cblk->passes[passno].rate
  677. - (res ? cblk->passes[res-1].rate:0);
  678. dd = cblk->passes[passno].disto
  679. - (res ? cblk->passes[res-1].disto:0);
  680. if (((dd * dwt_norm) >> WMSEDEC_SHIFT) * dwt_norm >= dr * lambda)
  681. res = passno+1;
  682. }
  683. return res;
  684. }
  685. static void truncpasses(J2kEncoderContext *s, J2kTile *tile)
  686. {
  687. int compno, reslevelno, bandno, cblkno, lev;
  688. J2kCodingStyle *codsty = &s->codsty;
  689. for (compno = 0; compno < s->ncomponents; compno++){
  690. J2kComponent *comp = tile->comp + compno;
  691. for (reslevelno = 0, lev = codsty->nreslevels-1; reslevelno < codsty->nreslevels; reslevelno++, lev--){
  692. J2kResLevel *reslevel = comp->reslevel + reslevelno;
  693. for (bandno = 0; bandno < reslevel->nbands ; bandno++){
  694. int bandpos = bandno + (reslevelno > 0);
  695. J2kBand *band = reslevel->band + bandno;
  696. for (cblkno = 0; cblkno < band->cblknx * band->cblkny; cblkno++){
  697. J2kCblk *cblk = band->cblk + cblkno;
  698. cblk->ninclpasses = getcut(cblk, s->lambda,
  699. (int64_t)dwt_norms[codsty->transform][bandpos][lev] * (int64_t)band->stepsize >> 13);
  700. }
  701. }
  702. }
  703. }
  704. }
  705. static int encode_tile(J2kEncoderContext *s, J2kTile *tile, int tileno)
  706. {
  707. int compno, reslevelno, bandno, ret;
  708. J2kT1Context t1;
  709. J2kCodingStyle *codsty = &s->codsty;
  710. for (compno = 0; compno < s->ncomponents; compno++){
  711. J2kComponent *comp = s->tile[tileno].comp + compno;
  712. av_log(s->avctx, AV_LOG_DEBUG,"dwt\n");
  713. if (ret = ff_j2k_dwt_encode(&comp->dwt, comp->data))
  714. return ret;
  715. av_log(s->avctx, AV_LOG_DEBUG,"after dwt -> tier1\n");
  716. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
  717. J2kResLevel *reslevel = comp->reslevel + reslevelno;
  718. for (bandno = 0; bandno < reslevel->nbands ; bandno++){
  719. J2kBand *band = reslevel->band + bandno;
  720. int cblkx, cblky, cblkno=0, xx0, x0, xx1, y0, yy0, yy1, bandpos;
  721. yy0 = bandno == 0 ? 0 : comp->reslevel[reslevelno-1].coord[1][1] - comp->reslevel[reslevelno-1].coord[1][0];
  722. y0 = yy0;
  723. yy1 = FFMIN(ff_j2k_ceildiv(band->coord[1][0] + 1, band->codeblock_height) * band->codeblock_height,
  724. band->coord[1][1]) - band->coord[1][0] + yy0;
  725. if (band->coord[0][0] == band->coord[0][1] || band->coord[1][0] == band->coord[1][1])
  726. continue;
  727. bandpos = bandno + (reslevelno > 0);
  728. for (cblky = 0; cblky < band->cblkny; cblky++){
  729. if (reslevelno == 0 || bandno == 1)
  730. xx0 = 0;
  731. else
  732. xx0 = comp->reslevel[reslevelno-1].coord[0][1] - comp->reslevel[reslevelno-1].coord[0][0];
  733. x0 = xx0;
  734. xx1 = FFMIN(ff_j2k_ceildiv(band->coord[0][0] + 1, band->codeblock_width) * band->codeblock_width,
  735. band->coord[0][1]) - band->coord[0][0] + xx0;
  736. for (cblkx = 0; cblkx < band->cblknx; cblkx++, cblkno++){
  737. int y, x;
  738. if (codsty->transform == FF_DWT53){
  739. for (y = yy0; y < yy1; y++){
  740. int *ptr = t1.data[y-yy0];
  741. for (x = xx0; x < xx1; x++){
  742. *ptr++ = comp->data[(comp->coord[0][1] - comp->coord[0][0]) * y + x] << NMSEDEC_FRACBITS;
  743. }
  744. }
  745. } else{
  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]);
  750. *ptr = (int64_t)*ptr * (int64_t)(8192 * 8192 / band->stepsize) >> 13 - NMSEDEC_FRACBITS;
  751. ptr++;
  752. }
  753. }
  754. }
  755. encode_cblk(s, &t1, band->cblk + cblkno, tile, xx1 - xx0, yy1 - yy0,
  756. bandpos, codsty->nreslevels - reslevelno - 1);
  757. xx0 = xx1;
  758. xx1 = FFMIN(xx1 + band->codeblock_width, band->coord[0][1] - band->coord[0][0] + x0);
  759. }
  760. yy0 = yy1;
  761. yy1 = FFMIN(yy1 + band->codeblock_height, band->coord[1][1] - band->coord[1][0] + y0);
  762. }
  763. }
  764. }
  765. av_log(s->avctx, AV_LOG_DEBUG, "after tier1\n");
  766. }
  767. av_log(s->avctx, AV_LOG_DEBUG, "rate control\n");
  768. truncpasses(s, tile);
  769. if (ret = encode_packets(s, tile, tileno))
  770. return ret;
  771. av_log(s->avctx, AV_LOG_DEBUG, "after rate control\n");
  772. return 0;
  773. }
  774. static void cleanup(J2kEncoderContext *s)
  775. {
  776. int tileno, compno;
  777. J2kCodingStyle *codsty = &s->codsty;
  778. for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
  779. for (compno = 0; compno < s->ncomponents; compno++){
  780. J2kComponent *comp = s->tile[tileno].comp + compno;
  781. ff_j2k_cleanup(comp, codsty);
  782. }
  783. av_freep(&s->tile[tileno].comp);
  784. }
  785. av_freep(&s->tile);
  786. }
  787. static void reinit(J2kEncoderContext *s)
  788. {
  789. int tileno, compno;
  790. for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
  791. J2kTile *tile = s->tile + tileno;
  792. for (compno = 0; compno < s->ncomponents; compno++)
  793. ff_j2k_reinit(tile->comp + compno, &s->codsty);
  794. }
  795. }
  796. static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  797. const AVFrame *pict, int *got_packet)
  798. {
  799. int tileno, ret;
  800. J2kEncoderContext *s = avctx->priv_data;
  801. if ((ret = ff_alloc_packet2(avctx, pkt, avctx->width*avctx->height*9 + FF_MIN_BUFFER_SIZE)) < 0)
  802. return ret;
  803. // init:
  804. s->buf = s->buf_start = pkt->data;
  805. s->buf_end = pkt->data + pkt->size;
  806. s->picture = *pict;
  807. avctx->coded_frame= &s->picture;
  808. s->lambda = s->picture.quality * LAMBDA_SCALE;
  809. copy_frame(s);
  810. reinit(s);
  811. if (s->buf_end - s->buf < 2)
  812. return -1;
  813. bytestream_put_be16(&s->buf, J2K_SOC);
  814. if (ret = put_siz(s))
  815. return ret;
  816. if (ret = put_cod(s))
  817. return ret;
  818. if (ret = put_qcd(s, 0))
  819. return ret;
  820. for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
  821. uint8_t *psotptr;
  822. if (!(psotptr = put_sot(s, tileno)))
  823. return -1;
  824. if (s->buf_end - s->buf < 2)
  825. return -1;
  826. bytestream_put_be16(&s->buf, J2K_SOD);
  827. if (ret = encode_tile(s, s->tile + tileno, tileno))
  828. return ret;
  829. bytestream_put_be32(&psotptr, s->buf - psotptr + 6);
  830. }
  831. if (s->buf_end - s->buf < 2)
  832. return -1;
  833. bytestream_put_be16(&s->buf, J2K_EOC);
  834. av_log(s->avctx, AV_LOG_DEBUG, "end\n");
  835. pkt->size = s->buf - s->buf_start;
  836. pkt->flags |= AV_PKT_FLAG_KEY;
  837. *got_packet = 1;
  838. return 0;
  839. }
  840. static av_cold int j2kenc_init(AVCodecContext *avctx)
  841. {
  842. int i, ret;
  843. J2kEncoderContext *s = avctx->priv_data;
  844. J2kCodingStyle *codsty = &s->codsty;
  845. J2kQuantStyle *qntsty = &s->qntsty;
  846. s->avctx = avctx;
  847. av_log(s->avctx, AV_LOG_DEBUG, "init\n");
  848. // defaults:
  849. // TODO: implement setting non-standard precinct size
  850. codsty->log2_prec_width = 15;
  851. codsty->log2_prec_height = 15;
  852. codsty->nreslevels = 7;
  853. codsty->log2_cblk_width = 4;
  854. codsty->log2_cblk_height = 4;
  855. codsty->transform = 1;
  856. qntsty->nguardbits = 1;
  857. s->tile_width = 256;
  858. s->tile_height = 256;
  859. if (codsty->transform == FF_DWT53)
  860. qntsty->quantsty = J2K_QSTY_NONE;
  861. else
  862. qntsty->quantsty = J2K_QSTY_SE;
  863. s->width = avctx->width;
  864. s->height = avctx->height;
  865. for (i = 0; i < 3; i++)
  866. s->cbps[i] = 8;
  867. if (avctx->pix_fmt == AV_PIX_FMT_RGB24){
  868. s->ncomponents = 3;
  869. } else if (avctx->pix_fmt == AV_PIX_FMT_GRAY8){
  870. s->ncomponents = 1;
  871. } else{ // planar YUV
  872. s->planar = 1;
  873. s->ncomponents = 3;
  874. avcodec_get_chroma_sub_sample(avctx->pix_fmt,
  875. s->chroma_shift, s->chroma_shift + 1);
  876. }
  877. ff_j2k_init_tier1_luts();
  878. init_luts();
  879. init_quantization(s);
  880. if (ret=init_tiles(s))
  881. return ret;
  882. av_log(s->avctx, AV_LOG_DEBUG, "after init\n");
  883. return 0;
  884. }
  885. static int j2kenc_destroy(AVCodecContext *avctx)
  886. {
  887. J2kEncoderContext *s = avctx->priv_data;
  888. cleanup(s);
  889. return 0;
  890. }
  891. AVCodec ff_jpeg2000_encoder = {
  892. .name = "j2k",
  893. .type = AVMEDIA_TYPE_VIDEO,
  894. .id = AV_CODEC_ID_JPEG2000,
  895. .priv_data_size = sizeof(J2kEncoderContext),
  896. .init = j2kenc_init,
  897. .encode2 = encode_frame,
  898. .close = j2kenc_destroy,
  899. .capabilities = CODEC_CAP_EXPERIMENTAL,
  900. .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
  901. .pix_fmts = (const enum AVPixelFormat[]) {
  902. AV_PIX_FMT_RGB24, AV_PIX_FMT_YUV444P, AV_PIX_FMT_GRAY8,
  903. /* AV_PIX_FMT_YUV420P,
  904. AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
  905. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,*/
  906. AV_PIX_FMT_NONE
  907. }
  908. };