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.

1293 lines
52KB

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