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.

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