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.

1220 lines
46KB

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