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.

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