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.

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