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.

1262 lines
48KB

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