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.

1167 lines
43KB

  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 int put_com(Jpeg2000EncoderContext *s, int compno)
  280. {
  281. int i;
  282. int size = 4 + strlen(LIBAVCODEC_IDENT);
  283. if (s->avctx->flags & CODEC_FLAG_BITEXACT)
  284. return 0;
  285. if (s->buf_end - s->buf < size + 2)
  286. return -1;
  287. bytestream_put_be16(&s->buf, JPEG2000_COM);
  288. bytestream_put_be16(&s->buf, size);
  289. bytestream_put_be16(&s->buf, 1); // General use (ISO/IEC 8859-15 (Latin) values)
  290. bytestream_put_buffer(&s->buf, LIBAVCODEC_IDENT, strlen(LIBAVCODEC_IDENT));
  291. return 0;
  292. }
  293. static uint8_t *put_sot(Jpeg2000EncoderContext *s, int tileno)
  294. {
  295. uint8_t *psotptr;
  296. if (s->buf_end - s->buf < 12)
  297. return NULL;
  298. bytestream_put_be16(&s->buf, JPEG2000_SOT);
  299. bytestream_put_be16(&s->buf, 10); // Lsot
  300. bytestream_put_be16(&s->buf, tileno); // Isot
  301. psotptr = s->buf;
  302. bytestream_put_be32(&s->buf, 0); // Psot (filled in later)
  303. bytestream_put_byte(&s->buf, 0); // TPsot
  304. bytestream_put_byte(&s->buf, 1); // TNsot
  305. return psotptr;
  306. }
  307. /**
  308. * compute the sizes of tiles, resolution levels, bands, etc.
  309. * allocate memory for them
  310. * divide the input image into tile-components
  311. */
  312. static int init_tiles(Jpeg2000EncoderContext *s)
  313. {
  314. int tileno, tilex, tiley, compno;
  315. Jpeg2000CodingStyle *codsty = &s->codsty;
  316. Jpeg2000QuantStyle *qntsty = &s->qntsty;
  317. s->numXtiles = ff_jpeg2000_ceildiv(s->width, s->tile_width);
  318. s->numYtiles = ff_jpeg2000_ceildiv(s->height, s->tile_height);
  319. s->tile = av_malloc_array(s->numXtiles, s->numYtiles * sizeof(Jpeg2000Tile));
  320. if (!s->tile)
  321. return AVERROR(ENOMEM);
  322. for (tileno = 0, tiley = 0; tiley < s->numYtiles; tiley++)
  323. for (tilex = 0; tilex < s->numXtiles; tilex++, tileno++){
  324. Jpeg2000Tile *tile = s->tile + tileno;
  325. tile->comp = av_mallocz_array(s->ncomponents, sizeof(Jpeg2000Component));
  326. if (!tile->comp)
  327. return AVERROR(ENOMEM);
  328. for (compno = 0; compno < s->ncomponents; compno++){
  329. Jpeg2000Component *comp = tile->comp + compno;
  330. int ret, i, j;
  331. comp->coord[0][0] = comp->coord_o[0][0] = tilex * s->tile_width;
  332. comp->coord[0][1] = comp->coord_o[0][1] = FFMIN((tilex+1)*s->tile_width, s->width);
  333. comp->coord[1][0] = comp->coord_o[1][0] = tiley * s->tile_height;
  334. comp->coord[1][1] = comp->coord_o[1][1] = FFMIN((tiley+1)*s->tile_height, s->height);
  335. if (compno > 0)
  336. for (i = 0; i < 2; i++)
  337. for (j = 0; j < 2; j++)
  338. comp->coord[i][j] = comp->coord_o[i][j] = ff_jpeg2000_ceildivpow2(comp->coord[i][j], s->chroma_shift[i]);
  339. if ((ret = ff_jpeg2000_init_component(comp,
  340. codsty,
  341. qntsty,
  342. s->cbps[compno],
  343. compno?1<<s->chroma_shift[0]:1,
  344. compno?1<<s->chroma_shift[1]:1,
  345. s->avctx
  346. )) < 0)
  347. return ret;
  348. }
  349. }
  350. return 0;
  351. }
  352. static void copy_frame(Jpeg2000EncoderContext *s)
  353. {
  354. int tileno, compno, i, y, x;
  355. uint8_t *line;
  356. for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
  357. Jpeg2000Tile *tile = s->tile + tileno;
  358. if (s->planar){
  359. for (compno = 0; compno < s->ncomponents; compno++){
  360. Jpeg2000Component *comp = tile->comp + compno;
  361. int *dst = comp->i_data;
  362. line = s->picture->data[compno]
  363. + comp->coord[1][0] * s->picture->linesize[compno]
  364. + comp->coord[0][0];
  365. for (y = comp->coord[1][0]; y < comp->coord[1][1]; y++){
  366. uint8_t *ptr = line;
  367. for (x = comp->coord[0][0]; x < comp->coord[0][1]; x++)
  368. *dst++ = *ptr++ - (1 << 7);
  369. line += s->picture->linesize[compno];
  370. }
  371. }
  372. } else{
  373. line = s->picture->data[0] + tile->comp[0].coord[1][0] * s->picture->linesize[0]
  374. + tile->comp[0].coord[0][0] * s->ncomponents;
  375. i = 0;
  376. for (y = tile->comp[0].coord[1][0]; y < tile->comp[0].coord[1][1]; y++){
  377. uint8_t *ptr = line;
  378. for (x = tile->comp[0].coord[0][0]; x < tile->comp[0].coord[0][1]; x++, i++){
  379. for (compno = 0; compno < s->ncomponents; compno++){
  380. tile->comp[compno].i_data[i] = *ptr++ - (1 << 7);
  381. }
  382. }
  383. line += s->picture->linesize[0];
  384. }
  385. }
  386. }
  387. }
  388. static void init_quantization(Jpeg2000EncoderContext *s)
  389. {
  390. int compno, reslevelno, bandno;
  391. Jpeg2000QuantStyle *qntsty = &s->qntsty;
  392. Jpeg2000CodingStyle *codsty = &s->codsty;
  393. for (compno = 0; compno < s->ncomponents; compno++){
  394. int gbandno = 0;
  395. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
  396. int nbands, lev = codsty->nreslevels - reslevelno - 1;
  397. nbands = reslevelno ? 3 : 1;
  398. for (bandno = 0; bandno < nbands; bandno++, gbandno++){
  399. int expn, mant = 0;
  400. if (codsty->transform == FF_DWT97_INT){
  401. int bandpos = bandno + (reslevelno>0),
  402. ss = 81920000 / dwt_norms[0][bandpos][lev],
  403. log = av_log2(ss);
  404. mant = (11 - log < 0 ? ss >> log - 11 : ss << 11 - log) & 0x7ff;
  405. expn = s->cbps[compno] - log + 13;
  406. } else
  407. expn = ((bandno&2)>>1) + (reslevelno>0) + s->cbps[compno];
  408. qntsty->expn[gbandno] = expn;
  409. qntsty->mant[gbandno] = mant;
  410. }
  411. }
  412. }
  413. }
  414. static void init_luts(void)
  415. {
  416. int i, a,
  417. mask = ~((1<<NMSEDEC_FRACBITS)-1);
  418. for (i = 0; i < (1 << NMSEDEC_BITS); i++){
  419. lut_nmsedec_sig[i] = FFMAX(6*i - (9<<NMSEDEC_FRACBITS-1) << 12-NMSEDEC_FRACBITS, 0);
  420. lut_nmsedec_sig0[i] = FFMAX((i*i + (1<<NMSEDEC_FRACBITS-1) & mask) << 1, 0);
  421. a = (i >> (NMSEDEC_BITS-2)&2) + 1;
  422. lut_nmsedec_ref[i] = FFMAX((-2*i + (1<<NMSEDEC_FRACBITS) + a*i - (a*a<<NMSEDEC_FRACBITS-2))
  423. << 13-NMSEDEC_FRACBITS, 0);
  424. lut_nmsedec_ref0[i] = FFMAX(((i*i + (1-4*i << NMSEDEC_FRACBITS-1) + (1<<2*NMSEDEC_FRACBITS)) & mask)
  425. << 1, 0);
  426. }
  427. }
  428. /* tier-1 routines */
  429. static int getnmsedec_sig(int x, int bpno)
  430. {
  431. if (bpno > NMSEDEC_FRACBITS)
  432. return lut_nmsedec_sig[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
  433. return lut_nmsedec_sig0[x & ((1 << NMSEDEC_BITS) - 1)];
  434. }
  435. static int getnmsedec_ref(int x, int bpno)
  436. {
  437. if (bpno > NMSEDEC_FRACBITS)
  438. return lut_nmsedec_ref[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
  439. return lut_nmsedec_ref0[x & ((1 << NMSEDEC_BITS) - 1)];
  440. }
  441. static void encode_sigpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
  442. {
  443. int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
  444. for (y0 = 0; y0 < height; y0 += 4)
  445. for (x = 0; x < width; x++)
  446. for (y = y0; y < height && y < y0+4; y++){
  447. if (!(t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG) && (t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG_NB)){
  448. int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno),
  449. bit = t1->data[(y) * t1->stride + x] & mask ? 1 : 0;
  450. ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, bit);
  451. if (bit){
  452. int xorbit;
  453. int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
  454. ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
  455. *nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
  456. ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
  457. }
  458. t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_VIS;
  459. }
  460. }
  461. }
  462. static void encode_refpass(Jpeg2000T1Context *t1, int width, int height, int *nmsedec, int bpno)
  463. {
  464. int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
  465. for (y0 = 0; y0 < height; y0 += 4)
  466. for (x = 0; x < width; x++)
  467. for (y = y0; y < height && y < y0+4; y++)
  468. if ((t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG){
  469. int ctxno = ff_jpeg2000_getrefctxno(t1->flags[(y+1) * t1->stride + x+1]);
  470. *nmsedec += getnmsedec_ref(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
  471. ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
  472. t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_REF;
  473. }
  474. }
  475. static void encode_clnpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
  476. {
  477. int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
  478. for (y0 = 0; y0 < height; y0 += 4)
  479. for (x = 0; x < width; x++){
  480. if (y0 + 3 < height && !(
  481. (t1->flags[(y0+1) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
  482. (t1->flags[(y0+2) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
  483. (t1->flags[(y0+3) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
  484. (t1->flags[(y0+4) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG))))
  485. {
  486. // aggregation mode
  487. int rlen;
  488. for (rlen = 0; rlen < 4; rlen++)
  489. if (t1->data[(y0+rlen) * t1->stride + x] & mask)
  490. break;
  491. ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL, rlen != 4);
  492. if (rlen == 4)
  493. continue;
  494. ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen >> 1);
  495. ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen & 1);
  496. for (y = y0 + rlen; y < y0 + 4; y++){
  497. if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))){
  498. int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno);
  499. if (y > y0 + rlen)
  500. ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
  501. if (t1->data[(y) * t1->stride + x] & mask){ // newly significant
  502. int xorbit;
  503. int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
  504. *nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
  505. ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
  506. ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
  507. }
  508. }
  509. t1->flags[(y+1) * t1->stride + x+1] &= ~JPEG2000_T1_VIS;
  510. }
  511. } else{
  512. for (y = y0; y < y0 + 4 && y < height; y++){
  513. if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))){
  514. int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno);
  515. ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
  516. if (t1->data[(y) * t1->stride + x] & mask){ // newly significant
  517. int xorbit;
  518. int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
  519. *nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
  520. ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
  521. ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
  522. }
  523. }
  524. t1->flags[(y+1) * t1->stride + x+1] &= ~JPEG2000_T1_VIS;
  525. }
  526. }
  527. }
  528. }
  529. static void encode_cblk(Jpeg2000EncoderContext *s, Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk, Jpeg2000Tile *tile,
  530. int width, int height, int bandpos, int lev)
  531. {
  532. int pass_t = 2, passno, x, y, max=0, nmsedec, bpno;
  533. int64_t wmsedec = 0;
  534. memset(t1->flags, 0, t1->stride * (height + 2) * sizeof(*t1->flags));
  535. for (y = 0; y < height; y++){
  536. for (x = 0; x < width; x++){
  537. if (t1->data[(y) * t1->stride + x] < 0){
  538. t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_SGN;
  539. t1->data[(y) * t1->stride + x] = -t1->data[(y) * t1->stride + x];
  540. }
  541. max = FFMAX(max, t1->data[(y) * t1->stride + x]);
  542. }
  543. }
  544. if (max == 0){
  545. cblk->nonzerobits = 0;
  546. bpno = 0;
  547. } else{
  548. cblk->nonzerobits = av_log2(max) + 1 - NMSEDEC_FRACBITS;
  549. bpno = cblk->nonzerobits - 1;
  550. }
  551. ff_mqc_initenc(&t1->mqc, cblk->data);
  552. for (passno = 0; bpno >= 0; passno++){
  553. nmsedec=0;
  554. switch(pass_t){
  555. case 0: encode_sigpass(t1, width, height, bandpos, &nmsedec, bpno);
  556. break;
  557. case 1: encode_refpass(t1, width, height, &nmsedec, bpno);
  558. break;
  559. case 2: encode_clnpass(t1, width, height, bandpos, &nmsedec, bpno);
  560. break;
  561. }
  562. cblk->passes[passno].rate = ff_mqc_flush_to(&t1->mqc, cblk->passes[passno].flushed, &cblk->passes[passno].flushed_len);
  563. wmsedec += (int64_t)nmsedec << (2*bpno);
  564. cblk->passes[passno].disto = wmsedec;
  565. if (++pass_t == 3){
  566. pass_t = 0;
  567. bpno--;
  568. }
  569. }
  570. cblk->npasses = passno;
  571. cblk->ninclpasses = passno;
  572. cblk->passes[passno-1].rate = ff_mqc_flush_to(&t1->mqc, cblk->passes[passno-1].flushed, &cblk->passes[passno-1].flushed_len);
  573. }
  574. /* tier-2 routines: */
  575. static void putnumpasses(Jpeg2000EncoderContext *s, int n)
  576. {
  577. if (n == 1)
  578. put_num(s, 0, 1);
  579. else if (n == 2)
  580. put_num(s, 2, 2);
  581. else if (n <= 5)
  582. put_num(s, 0xc | (n-3), 4);
  583. else if (n <= 36)
  584. put_num(s, 0x1e0 | (n-6), 9);
  585. else
  586. put_num(s, 0xff80 | (n-37), 16);
  587. }
  588. static int encode_packet(Jpeg2000EncoderContext *s, Jpeg2000ResLevel *rlevel, int precno,
  589. uint8_t *expn, int numgbits)
  590. {
  591. int bandno, empty = 1;
  592. // init bitstream
  593. *s->buf = 0;
  594. s->bit_index = 0;
  595. // header
  596. // is the packet empty?
  597. for (bandno = 0; bandno < rlevel->nbands; bandno++){
  598. if (rlevel->band[bandno].coord[0][0] < rlevel->band[bandno].coord[0][1]
  599. && rlevel->band[bandno].coord[1][0] < rlevel->band[bandno].coord[1][1]){
  600. empty = 0;
  601. break;
  602. }
  603. }
  604. put_bits(s, !empty, 1);
  605. if (empty){
  606. j2k_flush(s);
  607. return 0;
  608. }
  609. for (bandno = 0; bandno < rlevel->nbands; bandno++){
  610. Jpeg2000Band *band = rlevel->band + bandno;
  611. Jpeg2000Prec *prec = band->prec + precno;
  612. int yi, xi, pos;
  613. int cblknw = prec->nb_codeblocks_width;
  614. if (band->coord[0][0] == band->coord[0][1]
  615. || band->coord[1][0] == band->coord[1][1])
  616. continue;
  617. for (pos=0, yi = 0; yi < prec->nb_codeblocks_height; yi++){
  618. for (xi = 0; xi < cblknw; xi++, pos++){
  619. prec->cblkincl[pos].val = prec->cblk[yi * cblknw + xi].ninclpasses == 0;
  620. tag_tree_update(prec->cblkincl + pos);
  621. prec->zerobits[pos].val = expn[bandno] + numgbits - 1 - prec->cblk[yi * cblknw + xi].nonzerobits;
  622. tag_tree_update(prec->zerobits + pos);
  623. }
  624. }
  625. for (pos=0, yi = 0; yi < prec->nb_codeblocks_height; yi++){
  626. for (xi = 0; xi < cblknw; xi++, pos++){
  627. int pad = 0, llen, length;
  628. Jpeg2000Cblk *cblk = prec->cblk + yi * cblknw + xi;
  629. if (s->buf_end - s->buf < 20) // approximately
  630. return -1;
  631. // inclusion information
  632. tag_tree_code(s, prec->cblkincl + pos, 1);
  633. if (!cblk->ninclpasses)
  634. continue;
  635. // zerobits information
  636. tag_tree_code(s, prec->zerobits + pos, 100);
  637. // number of passes
  638. putnumpasses(s, cblk->ninclpasses);
  639. length = cblk->passes[cblk->ninclpasses-1].rate;
  640. llen = av_log2(length) - av_log2(cblk->ninclpasses) - 2;
  641. if (llen < 0){
  642. pad = -llen;
  643. llen = 0;
  644. }
  645. // length of code block
  646. put_bits(s, 1, llen);
  647. put_bits(s, 0, 1);
  648. put_num(s, length, av_log2(length)+1+pad);
  649. }
  650. }
  651. }
  652. j2k_flush(s);
  653. for (bandno = 0; bandno < rlevel->nbands; bandno++){
  654. Jpeg2000Band *band = rlevel->band + bandno;
  655. Jpeg2000Prec *prec = band->prec + precno;
  656. int yi, cblknw = prec->nb_codeblocks_width;
  657. for (yi =0; yi < prec->nb_codeblocks_height; yi++){
  658. int xi;
  659. for (xi = 0; xi < cblknw; xi++){
  660. Jpeg2000Cblk *cblk = prec->cblk + yi * cblknw + xi;
  661. if (cblk->ninclpasses){
  662. if (s->buf_end - s->buf < cblk->passes[cblk->ninclpasses-1].rate)
  663. return -1;
  664. bytestream_put_buffer(&s->buf, cblk->data, cblk->passes[cblk->ninclpasses-1].rate
  665. - cblk->passes[cblk->ninclpasses-1].flushed_len);
  666. bytestream_put_buffer(&s->buf, cblk->passes[cblk->ninclpasses-1].flushed,
  667. cblk->passes[cblk->ninclpasses-1].flushed_len);
  668. }
  669. }
  670. }
  671. }
  672. return 0;
  673. }
  674. static int encode_packets(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno)
  675. {
  676. int compno, reslevelno, ret;
  677. Jpeg2000CodingStyle *codsty = &s->codsty;
  678. Jpeg2000QuantStyle *qntsty = &s->qntsty;
  679. av_log(s->avctx, AV_LOG_DEBUG, "tier2\n");
  680. // lay-rlevel-comp-pos progression
  681. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
  682. for (compno = 0; compno < s->ncomponents; compno++){
  683. int precno;
  684. Jpeg2000ResLevel *reslevel = s->tile[tileno].comp[compno].reslevel + reslevelno;
  685. for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
  686. if ((ret = encode_packet(s, reslevel, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
  687. qntsty->nguardbits)) < 0)
  688. return ret;
  689. }
  690. }
  691. }
  692. av_log(s->avctx, AV_LOG_DEBUG, "after tier2\n");
  693. return 0;
  694. }
  695. static int getcut(Jpeg2000Cblk *cblk, int64_t lambda, int dwt_norm)
  696. {
  697. int passno, res = 0;
  698. for (passno = 0; passno < cblk->npasses; passno++){
  699. int dr;
  700. int64_t dd;
  701. dr = cblk->passes[passno].rate
  702. - (res ? cblk->passes[res-1].rate:0);
  703. dd = cblk->passes[passno].disto
  704. - (res ? cblk->passes[res-1].disto:0);
  705. if (((dd * dwt_norm) >> WMSEDEC_SHIFT) * dwt_norm >= dr * lambda)
  706. res = passno+1;
  707. }
  708. return res;
  709. }
  710. static void truncpasses(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile)
  711. {
  712. int precno, compno, reslevelno, bandno, cblkno, lev;
  713. Jpeg2000CodingStyle *codsty = &s->codsty;
  714. for (compno = 0; compno < s->ncomponents; compno++){
  715. Jpeg2000Component *comp = tile->comp + compno;
  716. for (reslevelno = 0, lev = codsty->nreslevels-1; reslevelno < codsty->nreslevels; reslevelno++, lev--){
  717. Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
  718. for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
  719. for (bandno = 0; bandno < reslevel->nbands ; bandno++){
  720. int bandpos = bandno + (reslevelno > 0);
  721. Jpeg2000Band *band = reslevel->band + bandno;
  722. Jpeg2000Prec *prec = band->prec + precno;
  723. for (cblkno = 0; cblkno < prec->nb_codeblocks_height * prec->nb_codeblocks_width; cblkno++){
  724. Jpeg2000Cblk *cblk = prec->cblk + cblkno;
  725. cblk->ninclpasses = getcut(cblk, s->lambda,
  726. (int64_t)dwt_norms[codsty->transform == FF_DWT53][bandpos][lev] * (int64_t)band->i_stepsize >> 15);
  727. }
  728. }
  729. }
  730. }
  731. }
  732. }
  733. static int encode_tile(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno)
  734. {
  735. int compno, reslevelno, bandno, ret;
  736. Jpeg2000T1Context t1;
  737. Jpeg2000CodingStyle *codsty = &s->codsty;
  738. for (compno = 0; compno < s->ncomponents; compno++){
  739. Jpeg2000Component *comp = s->tile[tileno].comp + compno;
  740. t1.stride = (1<<codsty->log2_cblk_width) + 2;
  741. av_log(s->avctx, AV_LOG_DEBUG,"dwt\n");
  742. if ((ret = ff_dwt_encode(&comp->dwt, comp->i_data)) < 0)
  743. return ret;
  744. av_log(s->avctx, AV_LOG_DEBUG,"after dwt -> tier1\n");
  745. for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
  746. Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
  747. for (bandno = 0; bandno < reslevel->nbands ; bandno++){
  748. Jpeg2000Band *band = reslevel->band + bandno;
  749. Jpeg2000Prec *prec = band->prec; // we support only 1 precinct per band ATM in the encoder
  750. int cblkx, cblky, cblkno=0, xx0, x0, xx1, y0, yy0, yy1, bandpos;
  751. yy0 = bandno == 0 ? 0 : comp->reslevel[reslevelno-1].coord[1][1] - comp->reslevel[reslevelno-1].coord[1][0];
  752. y0 = yy0;
  753. yy1 = FFMIN(ff_jpeg2000_ceildivpow2(band->coord[1][0] + 1, band->log2_cblk_height) << band->log2_cblk_height,
  754. band->coord[1][1]) - band->coord[1][0] + yy0;
  755. if (band->coord[0][0] == band->coord[0][1] || band->coord[1][0] == band->coord[1][1])
  756. continue;
  757. bandpos = bandno + (reslevelno > 0);
  758. for (cblky = 0; cblky < prec->nb_codeblocks_height; cblky++){
  759. if (reslevelno == 0 || bandno == 1)
  760. xx0 = 0;
  761. else
  762. xx0 = comp->reslevel[reslevelno-1].coord[0][1] - comp->reslevel[reslevelno-1].coord[0][0];
  763. x0 = xx0;
  764. xx1 = FFMIN(ff_jpeg2000_ceildivpow2(band->coord[0][0] + 1, band->log2_cblk_width) << band->log2_cblk_width,
  765. band->coord[0][1]) - band->coord[0][0] + xx0;
  766. for (cblkx = 0; cblkx < prec->nb_codeblocks_width; cblkx++, cblkno++){
  767. int y, x;
  768. if (codsty->transform == FF_DWT53){
  769. for (y = yy0; y < yy1; y++){
  770. int *ptr = t1.data + (y-yy0)*t1.stride;
  771. for (x = xx0; x < xx1; x++){
  772. *ptr++ = comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * y + x] << NMSEDEC_FRACBITS;
  773. }
  774. }
  775. } else{
  776. for (y = yy0; y < yy1; y++){
  777. int *ptr = t1.data + (y-yy0)*t1.stride;
  778. for (x = xx0; x < xx1; x++){
  779. *ptr = (comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * y + x]);
  780. *ptr = (int64_t)*ptr * (int64_t)(16384 * 65536 / band->i_stepsize) >> 15 - NMSEDEC_FRACBITS;
  781. ptr++;
  782. }
  783. }
  784. }
  785. encode_cblk(s, &t1, prec->cblk + cblkno, tile, xx1 - xx0, yy1 - yy0,
  786. bandpos, codsty->nreslevels - reslevelno - 1);
  787. xx0 = xx1;
  788. xx1 = FFMIN(xx1 + (1 << band->log2_cblk_width), band->coord[0][1] - band->coord[0][0] + x0);
  789. }
  790. yy0 = yy1;
  791. yy1 = FFMIN(yy1 + (1 << band->log2_cblk_height), band->coord[1][1] - band->coord[1][0] + y0);
  792. }
  793. }
  794. }
  795. av_log(s->avctx, AV_LOG_DEBUG, "after tier1\n");
  796. }
  797. av_log(s->avctx, AV_LOG_DEBUG, "rate control\n");
  798. truncpasses(s, tile);
  799. if ((ret = encode_packets(s, tile, tileno)) < 0)
  800. return ret;
  801. av_log(s->avctx, AV_LOG_DEBUG, "after rate control\n");
  802. return 0;
  803. }
  804. static void cleanup(Jpeg2000EncoderContext *s)
  805. {
  806. int tileno, compno;
  807. Jpeg2000CodingStyle *codsty = &s->codsty;
  808. for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
  809. for (compno = 0; compno < s->ncomponents; compno++){
  810. Jpeg2000Component *comp = s->tile[tileno].comp + compno;
  811. ff_jpeg2000_cleanup(comp, codsty);
  812. }
  813. av_freep(&s->tile[tileno].comp);
  814. }
  815. av_freep(&s->tile);
  816. }
  817. static void reinit(Jpeg2000EncoderContext *s)
  818. {
  819. int tileno, compno;
  820. for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
  821. Jpeg2000Tile *tile = s->tile + tileno;
  822. for (compno = 0; compno < s->ncomponents; compno++)
  823. ff_jpeg2000_reinit(tile->comp + compno, &s->codsty);
  824. }
  825. }
  826. static void update_size(uint8_t *size, const uint8_t *end)
  827. {
  828. AV_WB32(size, end-size);
  829. }
  830. static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  831. const AVFrame *pict, int *got_packet)
  832. {
  833. int tileno, ret;
  834. Jpeg2000EncoderContext *s = avctx->priv_data;
  835. uint8_t *chunkstart, *jp2cstart, *jp2hstart;
  836. if ((ret = ff_alloc_packet2(avctx, pkt, avctx->width*avctx->height*9 + FF_MIN_BUFFER_SIZE)) < 0)
  837. return ret;
  838. // init:
  839. s->buf = s->buf_start = pkt->data;
  840. s->buf_end = pkt->data + pkt->size;
  841. s->picture = pict;
  842. s->lambda = s->picture->quality * LAMBDA_SCALE;
  843. copy_frame(s);
  844. reinit(s);
  845. if (s->format == CODEC_JP2) {
  846. av_assert0(s->buf == pkt->data);
  847. bytestream_put_be32(&s->buf, 0x0000000C);
  848. bytestream_put_be32(&s->buf, 0x6A502020);
  849. bytestream_put_be32(&s->buf, 0x0D0A870A);
  850. chunkstart = s->buf;
  851. bytestream_put_be32(&s->buf, 0);
  852. bytestream_put_buffer(&s->buf, "ftyp", 4);
  853. bytestream_put_buffer(&s->buf, "jp2\040\040", 4);
  854. bytestream_put_be32(&s->buf, 0);
  855. update_size(chunkstart, s->buf);
  856. jp2hstart = s->buf;
  857. bytestream_put_be32(&s->buf, 0);
  858. bytestream_put_buffer(&s->buf, "jp2h", 4);
  859. chunkstart = s->buf;
  860. bytestream_put_be32(&s->buf, 0);
  861. bytestream_put_buffer(&s->buf, "ihdr", 4);
  862. bytestream_put_be32(&s->buf, avctx->height);
  863. bytestream_put_be32(&s->buf, avctx->width);
  864. bytestream_put_be16(&s->buf, s->ncomponents);
  865. bytestream_put_byte(&s->buf, s->cbps[0]);
  866. bytestream_put_byte(&s->buf, 7);
  867. bytestream_put_byte(&s->buf, 0);
  868. bytestream_put_byte(&s->buf, 0);
  869. update_size(chunkstart, s->buf);
  870. chunkstart = s->buf;
  871. bytestream_put_be32(&s->buf, 0);
  872. bytestream_put_buffer(&s->buf, "colr", 4);
  873. bytestream_put_byte(&s->buf, 1);
  874. bytestream_put_byte(&s->buf, 0);
  875. bytestream_put_byte(&s->buf, 0);
  876. if (s->ncomponents == 1) {
  877. bytestream_put_be32(&s->buf, 17);
  878. } else if (avctx->pix_fmt == AV_PIX_FMT_RGB24) {
  879. bytestream_put_be32(&s->buf, 16);
  880. } else {
  881. bytestream_put_be32(&s->buf, 18);
  882. }
  883. update_size(chunkstart, s->buf);
  884. update_size(jp2hstart, s->buf);
  885. jp2cstart = s->buf;
  886. bytestream_put_be32(&s->buf, 0);
  887. bytestream_put_buffer(&s->buf, "jp2c", 4);
  888. }
  889. if (s->buf_end - s->buf < 2)
  890. return -1;
  891. bytestream_put_be16(&s->buf, JPEG2000_SOC);
  892. if ((ret = put_siz(s)) < 0)
  893. return ret;
  894. if ((ret = put_cod(s)) < 0)
  895. return ret;
  896. if ((ret = put_qcd(s, 0)) < 0)
  897. return ret;
  898. if ((ret = put_com(s, 0)) < 0)
  899. return ret;
  900. for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
  901. uint8_t *psotptr;
  902. if (!(psotptr = put_sot(s, tileno)))
  903. return -1;
  904. if (s->buf_end - s->buf < 2)
  905. return -1;
  906. bytestream_put_be16(&s->buf, JPEG2000_SOD);
  907. if ((ret = encode_tile(s, s->tile + tileno, tileno)) < 0)
  908. return ret;
  909. bytestream_put_be32(&psotptr, s->buf - psotptr + 6);
  910. }
  911. if (s->buf_end - s->buf < 2)
  912. return -1;
  913. bytestream_put_be16(&s->buf, JPEG2000_EOC);
  914. if (s->format == CODEC_JP2)
  915. update_size(jp2cstart, s->buf);
  916. av_log(s->avctx, AV_LOG_DEBUG, "end\n");
  917. pkt->size = s->buf - s->buf_start;
  918. pkt->flags |= AV_PKT_FLAG_KEY;
  919. *got_packet = 1;
  920. return 0;
  921. }
  922. static av_cold int j2kenc_init(AVCodecContext *avctx)
  923. {
  924. int i, ret;
  925. Jpeg2000EncoderContext *s = avctx->priv_data;
  926. Jpeg2000CodingStyle *codsty = &s->codsty;
  927. Jpeg2000QuantStyle *qntsty = &s->qntsty;
  928. s->avctx = avctx;
  929. av_log(s->avctx, AV_LOG_DEBUG, "init\n");
  930. // defaults:
  931. // TODO: implement setting non-standard precinct size
  932. memset(codsty->log2_prec_widths , 15, sizeof(codsty->log2_prec_widths ));
  933. memset(codsty->log2_prec_heights, 15, sizeof(codsty->log2_prec_heights));
  934. codsty->nreslevels2decode=
  935. codsty->nreslevels = 7;
  936. codsty->log2_cblk_width = 4;
  937. codsty->log2_cblk_height = 4;
  938. codsty->transform = avctx->prediction_method ? FF_DWT53 : FF_DWT97_INT;
  939. qntsty->nguardbits = 1;
  940. s->tile_width = 256;
  941. s->tile_height = 256;
  942. if (codsty->transform == FF_DWT53)
  943. qntsty->quantsty = JPEG2000_QSTY_NONE;
  944. else
  945. qntsty->quantsty = JPEG2000_QSTY_SE;
  946. s->width = avctx->width;
  947. s->height = avctx->height;
  948. for (i = 0; i < 3; i++)
  949. s->cbps[i] = 8;
  950. if (avctx->pix_fmt == AV_PIX_FMT_RGB24){
  951. s->ncomponents = 3;
  952. } else if (avctx->pix_fmt == AV_PIX_FMT_GRAY8){
  953. s->ncomponents = 1;
  954. } else{ // planar YUV
  955. s->planar = 1;
  956. s->ncomponents = 3;
  957. avcodec_get_chroma_sub_sample(avctx->pix_fmt,
  958. s->chroma_shift, s->chroma_shift + 1);
  959. }
  960. ff_jpeg2000_init_tier1_luts();
  961. ff_mqc_init_context_tables();
  962. init_luts();
  963. init_quantization(s);
  964. if ((ret=init_tiles(s)) < 0)
  965. return ret;
  966. av_log(s->avctx, AV_LOG_DEBUG, "after init\n");
  967. return 0;
  968. }
  969. static int j2kenc_destroy(AVCodecContext *avctx)
  970. {
  971. Jpeg2000EncoderContext *s = avctx->priv_data;
  972. cleanup(s);
  973. return 0;
  974. }
  975. // taken from the libopenjpeg wraper so it matches
  976. #define OFFSET(x) offsetof(Jpeg2000EncoderContext, x)
  977. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  978. static const AVOption options[] = {
  979. { "format", "Codec Format", OFFSET(format), AV_OPT_TYPE_INT, { .i64 = CODEC_JP2 }, CODEC_J2K, CODEC_JP2, VE, "format" },
  980. { "j2k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CODEC_J2K }, 0, 0, VE, "format" },
  981. { "jp2", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CODEC_JP2 }, 0, 0, VE, "format" },
  982. { NULL }
  983. };
  984. static const AVClass j2k_class = {
  985. .class_name = "jpeg 2000 encoder",
  986. .item_name = av_default_item_name,
  987. .option = options,
  988. .version = LIBAVUTIL_VERSION_INT,
  989. };
  990. AVCodec ff_jpeg2000_encoder = {
  991. .name = "jpeg2000",
  992. .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
  993. .type = AVMEDIA_TYPE_VIDEO,
  994. .id = AV_CODEC_ID_JPEG2000,
  995. .priv_data_size = sizeof(Jpeg2000EncoderContext),
  996. .init = j2kenc_init,
  997. .encode2 = encode_frame,
  998. .close = j2kenc_destroy,
  999. .capabilities = CODEC_CAP_EXPERIMENTAL,
  1000. .pix_fmts = (const enum AVPixelFormat[]) {
  1001. AV_PIX_FMT_RGB24, AV_PIX_FMT_YUV444P, AV_PIX_FMT_GRAY8,
  1002. /* AV_PIX_FMT_YUV420P,
  1003. AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
  1004. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,*/
  1005. AV_PIX_FMT_NONE
  1006. },
  1007. .priv_class = &j2k_class,
  1008. };