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.

1145 lines
42KB

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