FFmpeg  4.3.9
j2kenc.c
Go to the documentation of this file.
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 
60 /**
61  * JPEG2000 image encoder
62  * @file
63  * @author Kamil Nowosad
64  */
65 
66 #include <float.h>
67 #include "avcodec.h"
68 #include "internal.h"
69 #include "bytestream.h"
70 #include "jpeg2000.h"
71 #include "libavutil/common.h"
72 #include "libavutil/pixdesc.h"
73 #include "libavutil/opt.h"
74 #include "libavutil/intreadwrite.h"
75 
76 #define NMSEDEC_BITS 7
77 #define NMSEDEC_FRACBITS (NMSEDEC_BITS-1)
78 #define WMSEDEC_SHIFT 13 ///< must be >= 13
79 #define LAMBDA_SCALE (100000000LL << (WMSEDEC_SHIFT - 13))
80 
81 #define CODEC_JP2 1
82 #define CODEC_J2K 0
83 
84 static int lut_nmsedec_ref [1<<NMSEDEC_BITS],
88 
89 static const int dwt_norms[2][4][10] = { // [dwt_type][band][rlevel] (multiplied by 10000)
90  {{10000, 19650, 41770, 84030, 169000, 338400, 676900, 1353000, 2706000, 5409000},
91  {20220, 39890, 83550, 170400, 342700, 686300, 1373000, 2746000, 5490000},
92  {20220, 39890, 83550, 170400, 342700, 686300, 1373000, 2746000, 5490000},
93  {20800, 38650, 83070, 171800, 347100, 695900, 1393000, 2786000, 5572000}},
94 
95  {{10000, 15000, 27500, 53750, 106800, 213400, 426700, 853300, 1707000, 3413000},
96  {10380, 15920, 29190, 57030, 113300, 226400, 452500, 904800, 1809000},
97  {10380, 15920, 29190, 57030, 113300, 226400, 452500, 904800, 1809000},
98  { 7186, 9218, 15860, 30430, 60190, 120100, 240000, 479700, 959300}}
99 };
100 
101 typedef struct {
103 } Jpeg2000Tile;
104 
105 typedef struct {
106  AVClass *class;
108  const AVFrame *picture;
109 
110  int width, height; ///< image width and height
111  uint8_t cbps[4]; ///< bits per sample in particular components
112  int chroma_shift[2];
115  int tile_width, tile_height; ///< tile size
116  int numXtiles, numYtiles;
117 
122 
124 
127 
129 
130  int format;
131  int pred;
133 
134 
135 /* debug */
136 #if 0
137 #undef ifprintf
138 #undef printf
139 
140 static void nspaces(FILE *fd, int n)
141 {
142  while(n--) putc(' ', fd);
143 }
144 
145 static void printcomp(Jpeg2000Component *comp)
146 {
147  int i;
148  for (i = 0; i < comp->y1 - comp->y0; i++)
149  ff_jpeg2000_printv(comp->i_data + i * (comp->x1 - comp->x0), comp->x1 - comp->x0);
150 }
151 
152 static void dump(Jpeg2000EncoderContext *s, FILE *fd)
153 {
154  int tileno, compno, reslevelno, bandno, precno;
155  fprintf(fd, "XSiz = %d, YSiz = %d, tile_width = %d, tile_height = %d\n"
156  "numXtiles = %d, numYtiles = %d, ncomponents = %d\n"
157  "tiles:\n",
158  s->width, s->height, s->tile_width, s->tile_height,
159  s->numXtiles, s->numYtiles, s->ncomponents);
160  for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
161  Jpeg2000Tile *tile = s->tile + tileno;
162  nspaces(fd, 2);
163  fprintf(fd, "tile %d:\n", tileno);
164  for(compno = 0; compno < s->ncomponents; compno++){
165  Jpeg2000Component *comp = tile->comp + compno;
166  nspaces(fd, 4);
167  fprintf(fd, "component %d:\n", compno);
168  nspaces(fd, 4);
169  fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d\n",
170  comp->x0, comp->x1, comp->y0, comp->y1);
171  for(reslevelno = 0; reslevelno < s->nreslevels; reslevelno++){
172  Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
173  nspaces(fd, 6);
174  fprintf(fd, "reslevel %d:\n", reslevelno);
175  nspaces(fd, 6);
176  fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d, nbands = %d\n",
177  reslevel->x0, reslevel->x1, reslevel->y0,
178  reslevel->y1, reslevel->nbands);
179  for(bandno = 0; bandno < reslevel->nbands; bandno++){
180  Jpeg2000Band *band = reslevel->band + bandno;
181  nspaces(fd, 8);
182  fprintf(fd, "band %d:\n", bandno);
183  nspaces(fd, 8);
184  fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d,"
185  "codeblock_width = %d, codeblock_height = %d cblknx = %d cblkny = %d\n",
186  band->x0, band->x1,
187  band->y0, band->y1,
188  band->codeblock_width, band->codeblock_height,
189  band->cblknx, band->cblkny);
190  for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
191  Jpeg2000Prec *prec = band->prec + precno;
192  nspaces(fd, 10);
193  fprintf(fd, "prec %d:\n", precno);
194  nspaces(fd, 10);
195  fprintf(fd, "xi0 = %d, xi1 = %d, yi0 = %d, yi1 = %d\n",
196  prec->xi0, prec->xi1, prec->yi0, prec->yi1);
197  }
198  }
199  }
200  }
201  }
202 }
203 #endif
204 
205 /* bitstream routines */
206 
207 /** put n times val bit */
208 static void put_bits(Jpeg2000EncoderContext *s, int val, int n) // TODO: optimize
209 {
210  while (n-- > 0){
211  if (s->bit_index == 8)
212  {
213  s->bit_index = *s->buf == 0xff;
214  *(++s->buf) = 0;
215  }
216  *s->buf |= val << (7 - s->bit_index++);
217  }
218 }
219 
220 /** put n least significant bits of a number num */
221 static void put_num(Jpeg2000EncoderContext *s, int num, int n)
222 {
223  while(--n >= 0)
224  put_bits(s, (num >> n) & 1, 1);
225 }
226 
227 /** flush the bitstream */
229 {
230  if (s->bit_index){
231  s->bit_index = 0;
232  s->buf++;
233  }
234 }
235 
236 /* tag tree routines */
237 
238 /** code the value stored in node */
239 static void tag_tree_code(Jpeg2000EncoderContext *s, Jpeg2000TgtNode *node, int threshold)
240 {
241  Jpeg2000TgtNode *stack[30];
242  int sp = 1, curval = 0;
243  stack[0] = node;
244 
245  node = node->parent;
246  while(node){
247  if (node->vis){
248  curval = node->val;
249  break;
250  }
251  node->vis++;
252  stack[sp++] = node;
253  node = node->parent;
254  }
255  while(--sp >= 0){
256  if (stack[sp]->val >= threshold){
257  put_bits(s, 0, threshold - curval);
258  break;
259  }
260  put_bits(s, 0, stack[sp]->val - curval);
261  put_bits(s, 1, 1);
262  curval = stack[sp]->val;
263  }
264 }
265 
266 /** update the value in node */
268 {
269  int lev = 0;
270  while (node->parent){
271  if (node->parent->val <= node->val)
272  break;
273  node->parent->val = node->val;
274  node = node->parent;
275  lev++;
276  }
277 }
278 
280 {
281  int i;
282 
283  if (s->buf_end - s->buf < 40 + 3 * s->ncomponents)
284  return -1;
285 
286  bytestream_put_be16(&s->buf, JPEG2000_SIZ);
287  bytestream_put_be16(&s->buf, 38 + 3 * s->ncomponents); // Lsiz
288  bytestream_put_be16(&s->buf, 0); // Rsiz
289  bytestream_put_be32(&s->buf, s->width); // width
290  bytestream_put_be32(&s->buf, s->height); // height
291  bytestream_put_be32(&s->buf, 0); // X0Siz
292  bytestream_put_be32(&s->buf, 0); // Y0Siz
293 
294  bytestream_put_be32(&s->buf, s->tile_width); // XTSiz
295  bytestream_put_be32(&s->buf, s->tile_height); // YTSiz
296  bytestream_put_be32(&s->buf, 0); // XT0Siz
297  bytestream_put_be32(&s->buf, 0); // YT0Siz
298  bytestream_put_be16(&s->buf, s->ncomponents); // CSiz
299 
300  for (i = 0; i < s->ncomponents; i++){ // Ssiz_i XRsiz_i, YRsiz_i
301  bytestream_put_byte(&s->buf, 7);
302  bytestream_put_byte(&s->buf, i?1<<s->chroma_shift[0]:1);
303  bytestream_put_byte(&s->buf, i?1<<s->chroma_shift[1]:1);
304  }
305  return 0;
306 }
307 
309 {
310  Jpeg2000CodingStyle *codsty = &s->codsty;
311 
312  if (s->buf_end - s->buf < 14)
313  return -1;
314 
315  bytestream_put_be16(&s->buf, JPEG2000_COD);
316  bytestream_put_be16(&s->buf, 12); // Lcod
317  bytestream_put_byte(&s->buf, 0); // Scod
318  // SGcod
319  bytestream_put_byte(&s->buf, 0); // progression level
320  bytestream_put_be16(&s->buf, 1); // num of layers
321  if(s->avctx->pix_fmt == AV_PIX_FMT_YUV444P){
322  bytestream_put_byte(&s->buf, 0); // unspecified
323  }else{
324  bytestream_put_byte(&s->buf, 0); // unspecified
325  }
326  // SPcod
327  bytestream_put_byte(&s->buf, codsty->nreslevels - 1); // num of decomp. levels
328  bytestream_put_byte(&s->buf, codsty->log2_cblk_width-2); // cblk width
329  bytestream_put_byte(&s->buf, codsty->log2_cblk_height-2); // cblk height
330  bytestream_put_byte(&s->buf, 0); // cblk style
331  bytestream_put_byte(&s->buf, codsty->transform == FF_DWT53); // transformation
332  return 0;
333 }
334 
335 static int put_qcd(Jpeg2000EncoderContext *s, int compno)
336 {
337  int i, size;
338  Jpeg2000CodingStyle *codsty = &s->codsty;
339  Jpeg2000QuantStyle *qntsty = &s->qntsty;
340 
341  if (qntsty->quantsty == JPEG2000_QSTY_NONE)
342  size = 4 + 3 * (codsty->nreslevels-1);
343  else // QSTY_SE
344  size = 5 + 6 * (codsty->nreslevels-1);
345 
346  if (s->buf_end - s->buf < size + 2)
347  return -1;
348 
349  bytestream_put_be16(&s->buf, JPEG2000_QCD);
350  bytestream_put_be16(&s->buf, size); // LQcd
351  bytestream_put_byte(&s->buf, (qntsty->nguardbits << 5) | qntsty->quantsty); // Sqcd
352  if (qntsty->quantsty == JPEG2000_QSTY_NONE)
353  for (i = 0; i < codsty->nreslevels * 3 - 2; i++)
354  bytestream_put_byte(&s->buf, qntsty->expn[i] << 3);
355  else // QSTY_SE
356  for (i = 0; i < codsty->nreslevels * 3 - 2; i++)
357  bytestream_put_be16(&s->buf, (qntsty->expn[i] << 11) | qntsty->mant[i]);
358  return 0;
359 }
360 
361 static int put_com(Jpeg2000EncoderContext *s, int compno)
362 {
363  int size = 4 + strlen(LIBAVCODEC_IDENT);
364 
366  return 0;
367 
368  if (s->buf_end - s->buf < size + 2)
369  return -1;
370 
371  bytestream_put_be16(&s->buf, JPEG2000_COM);
372  bytestream_put_be16(&s->buf, size);
373  bytestream_put_be16(&s->buf, 1); // General use (ISO/IEC 8859-15 (Latin) values)
374 
376 
377  return 0;
378 }
379 
380 static uint8_t *put_sot(Jpeg2000EncoderContext *s, int tileno)
381 {
382  uint8_t *psotptr;
383 
384  if (s->buf_end - s->buf < 12)
385  return NULL;
386 
387  bytestream_put_be16(&s->buf, JPEG2000_SOT);
388  bytestream_put_be16(&s->buf, 10); // Lsot
389  bytestream_put_be16(&s->buf, tileno); // Isot
390 
391  psotptr = s->buf;
392  bytestream_put_be32(&s->buf, 0); // Psot (filled in later)
393 
394  bytestream_put_byte(&s->buf, 0); // TPsot
395  bytestream_put_byte(&s->buf, 1); // TNsot
396  return psotptr;
397 }
398 
399 /**
400  * compute the sizes of tiles, resolution levels, bands, etc.
401  * allocate memory for them
402  * divide the input image into tile-components
403  */
405 {
406  int tileno, tilex, tiley, compno;
407  Jpeg2000CodingStyle *codsty = &s->codsty;
408  Jpeg2000QuantStyle *qntsty = &s->qntsty;
409 
412 
413  s->tile = av_calloc(s->numXtiles, s->numYtiles * sizeof(Jpeg2000Tile));
414  if (!s->tile)
415  return AVERROR(ENOMEM);
416  for (tileno = 0, tiley = 0; tiley < s->numYtiles; tiley++)
417  for (tilex = 0; tilex < s->numXtiles; tilex++, tileno++){
418  Jpeg2000Tile *tile = s->tile + tileno;
419 
420  tile->comp = av_mallocz_array(s->ncomponents, sizeof(Jpeg2000Component));
421  if (!tile->comp)
422  return AVERROR(ENOMEM);
423  for (compno = 0; compno < s->ncomponents; compno++){
424  Jpeg2000Component *comp = tile->comp + compno;
425  int ret, i, j;
426 
427  comp->coord[0][0] = comp->coord_o[0][0] = tilex * s->tile_width;
428  comp->coord[0][1] = comp->coord_o[0][1] = FFMIN((tilex+1)*s->tile_width, s->width);
429  comp->coord[1][0] = comp->coord_o[1][0] = tiley * s->tile_height;
430  comp->coord[1][1] = comp->coord_o[1][1] = FFMIN((tiley+1)*s->tile_height, s->height);
431  if (compno > 0)
432  for (i = 0; i < 2; i++)
433  for (j = 0; j < 2; j++)
434  comp->coord[i][j] = comp->coord_o[i][j] = ff_jpeg2000_ceildivpow2(comp->coord[i][j], s->chroma_shift[i]);
435 
436  if ((ret = ff_jpeg2000_init_component(comp,
437  codsty,
438  qntsty,
439  s->cbps[compno],
440  compno?1<<s->chroma_shift[0]:1,
441  compno?1<<s->chroma_shift[1]:1,
442  s->avctx
443  )) < 0)
444  return ret;
445  }
446  }
447  return 0;
448 }
449 
451 {
452  int tileno, compno, i, y, x;
453  uint8_t *line;
454  for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
455  Jpeg2000Tile *tile = s->tile + tileno;
456  if (s->planar){
457  for (compno = 0; compno < s->ncomponents; compno++){
458  Jpeg2000Component *comp = tile->comp + compno;
459  int *dst = comp->i_data;
460  line = s->picture->data[compno]
461  + comp->coord[1][0] * s->picture->linesize[compno]
462  + comp->coord[0][0];
463  for (y = comp->coord[1][0]; y < comp->coord[1][1]; y++){
464  uint8_t *ptr = line;
465  for (x = comp->coord[0][0]; x < comp->coord[0][1]; x++)
466  *dst++ = *ptr++ - (1 << 7);
467  line += s->picture->linesize[compno];
468  }
469  }
470  } else{
471  line = s->picture->data[0] + tile->comp[0].coord[1][0] * s->picture->linesize[0]
472  + tile->comp[0].coord[0][0] * s->ncomponents;
473 
474  i = 0;
475  for (y = tile->comp[0].coord[1][0]; y < tile->comp[0].coord[1][1]; y++){
476  uint8_t *ptr = line;
477  for (x = tile->comp[0].coord[0][0]; x < tile->comp[0].coord[0][1]; x++, i++){
478  for (compno = 0; compno < s->ncomponents; compno++){
479  tile->comp[compno].i_data[i] = *ptr++ - (1 << 7);
480  }
481  }
482  line += s->picture->linesize[0];
483  }
484  }
485  }
486 }
487 
489 {
490  int compno, reslevelno, bandno;
491  Jpeg2000QuantStyle *qntsty = &s->qntsty;
492  Jpeg2000CodingStyle *codsty = &s->codsty;
493 
494  for (compno = 0; compno < s->ncomponents; compno++){
495  int gbandno = 0;
496  for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
497  int nbands, lev = codsty->nreslevels - reslevelno - 1;
498  nbands = reslevelno ? 3 : 1;
499  for (bandno = 0; bandno < nbands; bandno++, gbandno++){
500  int expn, mant = 0;
501 
502  if (codsty->transform == FF_DWT97_INT){
503  int bandpos = bandno + (reslevelno>0),
504  ss = 81920000 / dwt_norms[0][bandpos][lev],
505  log = av_log2(ss);
506  mant = (11 - log < 0 ? ss >> log - 11 : ss << 11 - log) & 0x7ff;
507  expn = s->cbps[compno] - log + 13;
508  } else
509  expn = ((bandno&2)>>1) + (reslevelno>0) + s->cbps[compno];
510 
511  qntsty->expn[gbandno] = expn;
512  qntsty->mant[gbandno] = mant;
513  }
514  }
515  }
516 }
517 
518 static void init_luts(void)
519 {
520  int i, a,
521  mask = ~((1<<NMSEDEC_FRACBITS)-1);
522 
523  for (i = 0; i < (1 << NMSEDEC_BITS); i++){
524  lut_nmsedec_sig[i] = FFMAX((3 * i << (13 - NMSEDEC_FRACBITS)) - (9 << 11), 0);
525  lut_nmsedec_sig0[i] = FFMAX((i*i + (1<<NMSEDEC_FRACBITS-1) & mask) << 1, 0);
526 
527  a = (i >> (NMSEDEC_BITS-2)&2) + 1;
528  lut_nmsedec_ref[i] = FFMAX((a - 2) * (i << (13 - NMSEDEC_FRACBITS)) +
529  (1 << 13) - (a * a << 11), 0);
530  lut_nmsedec_ref0[i] = FFMAX(((i * i - (i << NMSEDEC_BITS) + (1 << 2 * NMSEDEC_FRACBITS) + (1 << (NMSEDEC_FRACBITS - 1))) & mask)
531  << 1, 0);
532  }
533 }
534 
535 /* tier-1 routines */
536 static int getnmsedec_sig(int x, int bpno)
537 {
538  if (bpno > NMSEDEC_FRACBITS)
539  return lut_nmsedec_sig[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
540  return lut_nmsedec_sig0[x & ((1 << NMSEDEC_BITS) - 1)];
541 }
542 
543 static int getnmsedec_ref(int x, int bpno)
544 {
545  if (bpno > NMSEDEC_FRACBITS)
546  return lut_nmsedec_ref[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
547  return lut_nmsedec_ref0[x & ((1 << NMSEDEC_BITS) - 1)];
548 }
549 
550 static void encode_sigpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
551 {
552  int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
553  for (y0 = 0; y0 < height; y0 += 4)
554  for (x = 0; x < width; x++)
555  for (y = y0; y < height && y < y0+4; y++){
556  if (!(t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG) && (t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG_NB)){
557  int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno),
558  bit = t1->data[(y) * t1->stride + x] & mask ? 1 : 0;
559  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, bit);
560  if (bit){
561  int xorbit;
562  int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
563  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
564  *nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
565  ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
566  }
567  t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_VIS;
568  }
569  }
570 }
571 
572 static void encode_refpass(Jpeg2000T1Context *t1, int width, int height, int *nmsedec, int bpno)
573 {
574  int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
575  for (y0 = 0; y0 < height; y0 += 4)
576  for (x = 0; x < width; x++)
577  for (y = y0; y < height && y < y0+4; y++)
578  if ((t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG){
579  int ctxno = ff_jpeg2000_getrefctxno(t1->flags[(y+1) * t1->stride + x+1]);
580  *nmsedec += getnmsedec_ref(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
581  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
582  t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_REF;
583  }
584 }
585 
586 static void encode_clnpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
587 {
588  int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
589  for (y0 = 0; y0 < height; y0 += 4)
590  for (x = 0; x < width; x++){
591  if (y0 + 3 < height && !(
592  (t1->flags[(y0+1) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
593  (t1->flags[(y0+2) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
594  (t1->flags[(y0+3) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
595  (t1->flags[(y0+4) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG))))
596  {
597  // aggregation mode
598  int rlen;
599  for (rlen = 0; rlen < 4; rlen++)
600  if (t1->data[(y0+rlen) * t1->stride + x] & mask)
601  break;
602  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL, rlen != 4);
603  if (rlen == 4)
604  continue;
605  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen >> 1);
606  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen & 1);
607  for (y = y0 + rlen; y < y0 + 4; y++){
608  if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))){
609  int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno);
610  if (y > y0 + rlen)
611  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
612  if (t1->data[(y) * t1->stride + x] & mask){ // newly significant
613  int xorbit;
614  int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
615  *nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
616  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
617  ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
618  }
619  }
620  t1->flags[(y+1) * t1->stride + x+1] &= ~JPEG2000_T1_VIS;
621  }
622  } else{
623  for (y = y0; y < y0 + 4 && y < height; y++){
624  if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))){
625  int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno);
626  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
627  if (t1->data[(y) * t1->stride + x] & mask){ // newly significant
628  int xorbit;
629  int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
630  *nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
631  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
632  ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
633  }
634  }
635  t1->flags[(y+1) * t1->stride + x+1] &= ~JPEG2000_T1_VIS;
636  }
637  }
638  }
639 }
640 
642  int width, int height, int bandpos, int lev)
643 {
644  int pass_t = 2, passno, x, y, max=0, nmsedec, bpno;
645  int64_t wmsedec = 0;
646 
647  memset(t1->flags, 0, t1->stride * (height + 2) * sizeof(*t1->flags));
648 
649  for (y = 0; y < height; y++){
650  for (x = 0; x < width; x++){
651  if (t1->data[(y) * t1->stride + x] < 0){
652  t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_SGN;
653  t1->data[(y) * t1->stride + x] = -t1->data[(y) * t1->stride + x];
654  }
655  max = FFMAX(max, t1->data[(y) * t1->stride + x]);
656  }
657  }
658 
659  if (max == 0){
660  cblk->nonzerobits = 0;
661  } else{
662  cblk->nonzerobits = av_log2(max) + 1 - NMSEDEC_FRACBITS;
663  }
664  bpno = cblk->nonzerobits - 1;
665 
666  cblk->data[0] = 0;
667  ff_mqc_initenc(&t1->mqc, cblk->data + 1);
668 
669  for (passno = 0; bpno >= 0; passno++){
670  nmsedec=0;
671 
672  switch(pass_t){
673  case 0: encode_sigpass(t1, width, height, bandpos, &nmsedec, bpno);
674  break;
675  case 1: encode_refpass(t1, width, height, &nmsedec, bpno);
676  break;
677  case 2: encode_clnpass(t1, width, height, bandpos, &nmsedec, bpno);
678  break;
679  }
680 
681  cblk->passes[passno].rate = ff_mqc_flush_to(&t1->mqc, cblk->passes[passno].flushed, &cblk->passes[passno].flushed_len);
682  wmsedec += (int64_t)nmsedec << (2*bpno);
683  cblk->passes[passno].disto = wmsedec;
684 
685  if (++pass_t == 3){
686  pass_t = 0;
687  bpno--;
688  }
689  }
690  cblk->npasses = passno;
691  cblk->ninclpasses = passno;
692 
693  if (passno)
694  cblk->passes[passno-1].rate = ff_mqc_flush_to(&t1->mqc, cblk->passes[passno-1].flushed, &cblk->passes[passno-1].flushed_len);
695 }
696 
697 /* tier-2 routines: */
698 
699 static void putnumpasses(Jpeg2000EncoderContext *s, int n)
700 {
701  if (n == 1)
702  put_num(s, 0, 1);
703  else if (n == 2)
704  put_num(s, 2, 2);
705  else if (n <= 5)
706  put_num(s, 0xc | (n-3), 4);
707  else if (n <= 36)
708  put_num(s, 0x1e0 | (n-6), 9);
709  else
710  put_num(s, 0xff80 | (n-37), 16);
711 }
712 
713 
714 static int encode_packet(Jpeg2000EncoderContext *s, Jpeg2000ResLevel *rlevel, int precno,
715  uint8_t *expn, int numgbits)
716 {
717  int bandno, empty = 1;
718 
719  // init bitstream
720  *s->buf = 0;
721  s->bit_index = 0;
722 
723  // header
724 
725  // is the packet empty?
726  for (bandno = 0; bandno < rlevel->nbands; bandno++){
727  if (rlevel->band[bandno].coord[0][0] < rlevel->band[bandno].coord[0][1]
728  && rlevel->band[bandno].coord[1][0] < rlevel->band[bandno].coord[1][1]){
729  empty = 0;
730  break;
731  }
732  }
733 
734  put_bits(s, !empty, 1);
735  if (empty){
736  j2k_flush(s);
737  return 0;
738  }
739 
740  for (bandno = 0; bandno < rlevel->nbands; bandno++){
741  Jpeg2000Band *band = rlevel->band + bandno;
742  Jpeg2000Prec *prec = band->prec + precno;
743  int yi, xi, pos;
744  int cblknw = prec->nb_codeblocks_width;
745 
746  if (band->coord[0][0] == band->coord[0][1]
747  || band->coord[1][0] == band->coord[1][1])
748  continue;
749 
750  for (pos=0, yi = 0; yi < prec->nb_codeblocks_height; yi++){
751  for (xi = 0; xi < cblknw; xi++, pos++){
752  prec->cblkincl[pos].val = prec->cblk[yi * cblknw + xi].ninclpasses == 0;
753  tag_tree_update(prec->cblkincl + pos);
754  prec->zerobits[pos].val = expn[bandno] + numgbits - 1 - prec->cblk[yi * cblknw + xi].nonzerobits;
755  tag_tree_update(prec->zerobits + pos);
756  }
757  }
758 
759  for (pos=0, yi = 0; yi < prec->nb_codeblocks_height; yi++){
760  for (xi = 0; xi < cblknw; xi++, pos++){
761  int pad = 0, llen, length;
762  Jpeg2000Cblk *cblk = prec->cblk + yi * cblknw + xi;
763 
764  if (s->buf_end - s->buf < 20) // approximately
765  return -1;
766 
767  // inclusion information
768  tag_tree_code(s, prec->cblkincl + pos, 1);
769  if (!cblk->ninclpasses)
770  continue;
771  // zerobits information
772  tag_tree_code(s, prec->zerobits + pos, 100);
773  // number of passes
774  putnumpasses(s, cblk->ninclpasses);
775 
776  length = cblk->passes[cblk->ninclpasses-1].rate;
777  llen = av_log2(length) - av_log2(cblk->ninclpasses) - 2;
778  if (llen < 0){
779  pad = -llen;
780  llen = 0;
781  }
782  // length of code block
783  put_bits(s, 1, llen);
784  put_bits(s, 0, 1);
785  put_num(s, length, av_log2(length)+1+pad);
786  }
787  }
788  }
789  j2k_flush(s);
790  for (bandno = 0; bandno < rlevel->nbands; bandno++){
791  Jpeg2000Band *band = rlevel->band + bandno;
792  Jpeg2000Prec *prec = band->prec + precno;
793  int yi, cblknw = prec->nb_codeblocks_width;
794  for (yi =0; yi < prec->nb_codeblocks_height; yi++){
795  int xi;
796  for (xi = 0; xi < cblknw; xi++){
797  Jpeg2000Cblk *cblk = prec->cblk + yi * cblknw + xi;
798  if (cblk->ninclpasses){
799  if (s->buf_end - s->buf < cblk->passes[cblk->ninclpasses-1].rate)
800  return -1;
801  bytestream_put_buffer(&s->buf, cblk->data + 1, cblk->passes[cblk->ninclpasses-1].rate
802  - cblk->passes[cblk->ninclpasses-1].flushed_len);
803  bytestream_put_buffer(&s->buf, cblk->passes[cblk->ninclpasses-1].flushed,
804  cblk->passes[cblk->ninclpasses-1].flushed_len);
805  }
806  }
807  }
808  }
809  return 0;
810 }
811 
812 static int encode_packets(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno)
813 {
814  int compno, reslevelno, ret;
815  Jpeg2000CodingStyle *codsty = &s->codsty;
816  Jpeg2000QuantStyle *qntsty = &s->qntsty;
817 
818  av_log(s->avctx, AV_LOG_DEBUG, "tier2\n");
819  // lay-rlevel-comp-pos progression
820  for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
821  for (compno = 0; compno < s->ncomponents; compno++){
822  int precno;
823  Jpeg2000ResLevel *reslevel = s->tile[tileno].comp[compno].reslevel + reslevelno;
824  for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
825  if ((ret = encode_packet(s, reslevel, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
826  qntsty->nguardbits)) < 0)
827  return ret;
828  }
829  }
830  }
831  av_log(s->avctx, AV_LOG_DEBUG, "after tier2\n");
832  return 0;
833 }
834 
835 static int getcut(Jpeg2000Cblk *cblk, int64_t lambda, int dwt_norm)
836 {
837  int passno, res = 0;
838  for (passno = 0; passno < cblk->npasses; passno++){
839  int dr;
840  int64_t dd;
841 
842  dr = cblk->passes[passno].rate
843  - (res ? cblk->passes[res-1].rate:0);
844  dd = cblk->passes[passno].disto
845  - (res ? cblk->passes[res-1].disto:0);
846 
847  if (((dd * dwt_norm) >> WMSEDEC_SHIFT) * dwt_norm >= dr * lambda)
848  res = passno+1;
849  }
850  return res;
851 }
852 
854 {
855  int precno, compno, reslevelno, bandno, cblkno, lev;
856  Jpeg2000CodingStyle *codsty = &s->codsty;
857 
858  for (compno = 0; compno < s->ncomponents; compno++){
859  Jpeg2000Component *comp = tile->comp + compno;
860 
861  for (reslevelno = 0, lev = codsty->nreslevels-1; reslevelno < codsty->nreslevels; reslevelno++, lev--){
862  Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
863 
864  for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
865  for (bandno = 0; bandno < reslevel->nbands ; bandno++){
866  int bandpos = bandno + (reslevelno > 0);
867  Jpeg2000Band *band = reslevel->band + bandno;
868  Jpeg2000Prec *prec = band->prec + precno;
869 
870  for (cblkno = 0; cblkno < prec->nb_codeblocks_height * prec->nb_codeblocks_width; cblkno++){
871  Jpeg2000Cblk *cblk = prec->cblk + cblkno;
872 
873  cblk->ninclpasses = getcut(cblk, s->lambda,
874  (int64_t)dwt_norms[codsty->transform == FF_DWT53][bandpos][lev] * (int64_t)band->i_stepsize >> 15);
875  }
876  }
877  }
878  }
879  }
880 }
881 
882 static int encode_tile(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno)
883 {
884  int compno, reslevelno, bandno, ret;
886  Jpeg2000CodingStyle *codsty = &s->codsty;
887  for (compno = 0; compno < s->ncomponents; compno++){
888  Jpeg2000Component *comp = s->tile[tileno].comp + compno;
889 
890  t1.stride = (1<<codsty->log2_cblk_width) + 2;
891 
892  av_log(s->avctx, AV_LOG_DEBUG,"dwt\n");
893  if ((ret = ff_dwt_encode(&comp->dwt, comp->i_data)) < 0)
894  return ret;
895  av_log(s->avctx, AV_LOG_DEBUG,"after dwt -> tier1\n");
896 
897  for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
898  Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
899 
900  for (bandno = 0; bandno < reslevel->nbands ; bandno++){
901  Jpeg2000Band *band = reslevel->band + bandno;
902  Jpeg2000Prec *prec = band->prec; // we support only 1 precinct per band ATM in the encoder
903  int cblkx, cblky, cblkno=0, xx0, x0, xx1, y0, yy0, yy1, bandpos;
904  yy0 = bandno == 0 ? 0 : comp->reslevel[reslevelno-1].coord[1][1] - comp->reslevel[reslevelno-1].coord[1][0];
905  y0 = yy0;
906  yy1 = FFMIN(ff_jpeg2000_ceildivpow2(band->coord[1][0] + 1, band->log2_cblk_height) << band->log2_cblk_height,
907  band->coord[1][1]) - band->coord[1][0] + yy0;
908 
909  if (band->coord[0][0] == band->coord[0][1] || band->coord[1][0] == band->coord[1][1])
910  continue;
911 
912  bandpos = bandno + (reslevelno > 0);
913 
914  for (cblky = 0; cblky < prec->nb_codeblocks_height; cblky++){
915  if (reslevelno == 0 || bandno == 1)
916  xx0 = 0;
917  else
918  xx0 = comp->reslevel[reslevelno-1].coord[0][1] - comp->reslevel[reslevelno-1].coord[0][0];
919  x0 = xx0;
920  xx1 = FFMIN(ff_jpeg2000_ceildivpow2(band->coord[0][0] + 1, band->log2_cblk_width) << band->log2_cblk_width,
921  band->coord[0][1]) - band->coord[0][0] + xx0;
922 
923  for (cblkx = 0; cblkx < prec->nb_codeblocks_width; cblkx++, cblkno++){
924  int y, x;
925  if (codsty->transform == FF_DWT53){
926  for (y = yy0; y < yy1; y++){
927  int *ptr = t1.data + (y-yy0)*t1.stride;
928  for (x = xx0; x < xx1; x++){
929  *ptr++ = comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * y + x] * (1 << NMSEDEC_FRACBITS);
930  }
931  }
932  } else{
933  for (y = yy0; y < yy1; y++){
934  int *ptr = t1.data + (y-yy0)*t1.stride;
935  for (x = xx0; x < xx1; x++){
936  *ptr = (comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * y + x]);
937  *ptr = (int64_t)*ptr * (int64_t)(16384 * 65536 / band->i_stepsize) >> 15 - NMSEDEC_FRACBITS;
938  ptr++;
939  }
940  }
941  }
942  if (!prec->cblk[cblkno].data)
943  prec->cblk[cblkno].data = av_malloc(1 + 8192);
944  if (!prec->cblk[cblkno].passes)
945  prec->cblk[cblkno].passes = av_malloc_array(JPEG2000_MAX_PASSES, sizeof (*prec->cblk[cblkno].passes));
946  if (!prec->cblk[cblkno].data || !prec->cblk[cblkno].passes)
947  return AVERROR(ENOMEM);
948  encode_cblk(s, &t1, prec->cblk + cblkno, tile, xx1 - xx0, yy1 - yy0,
949  bandpos, codsty->nreslevels - reslevelno - 1);
950  xx0 = xx1;
951  xx1 = FFMIN(xx1 + (1 << band->log2_cblk_width), band->coord[0][1] - band->coord[0][0] + x0);
952  }
953  yy0 = yy1;
954  yy1 = FFMIN(yy1 + (1 << band->log2_cblk_height), band->coord[1][1] - band->coord[1][0] + y0);
955  }
956  }
957  }
958  av_log(s->avctx, AV_LOG_DEBUG, "after tier1\n");
959  }
960 
961  av_log(s->avctx, AV_LOG_DEBUG, "rate control\n");
962  truncpasses(s, tile);
963  if ((ret = encode_packets(s, tile, tileno)) < 0)
964  return ret;
965  av_log(s->avctx, AV_LOG_DEBUG, "after rate control\n");
966  return 0;
967 }
968 
970 {
971  int tileno, compno;
972  Jpeg2000CodingStyle *codsty = &s->codsty;
973 
974  if (!s->tile)
975  return;
976  for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
977  if (s->tile[tileno].comp) {
978  for (compno = 0; compno < s->ncomponents; compno++){
979  Jpeg2000Component *comp = s->tile[tileno].comp + compno;
980  ff_jpeg2000_cleanup(comp, codsty);
981  }
982  av_freep(&s->tile[tileno].comp);
983  }
984  }
985  av_freep(&s->tile);
986 }
987 
989 {
990  int tileno, compno;
991  for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
992  Jpeg2000Tile *tile = s->tile + tileno;
993  for (compno = 0; compno < s->ncomponents; compno++)
994  ff_jpeg2000_reinit(tile->comp + compno, &s->codsty);
995  }
996 }
997 
998 static void update_size(uint8_t *size, const uint8_t *end)
999 {
1000  AV_WB32(size, end-size);
1001 }
1002 
1004  const AVFrame *pict, int *got_packet)
1005 {
1006  int tileno, ret;
1007  Jpeg2000EncoderContext *s = avctx->priv_data;
1008  uint8_t *chunkstart, *jp2cstart, *jp2hstart;
1010 
1011  if ((ret = ff_alloc_packet2(avctx, pkt, avctx->width*avctx->height*9 + AV_INPUT_BUFFER_MIN_SIZE, 0)) < 0)
1012  return ret;
1013 
1014  // init:
1015  s->buf = s->buf_start = pkt->data;
1016  s->buf_end = pkt->data + pkt->size;
1017 
1018  s->picture = pict;
1019 
1020  s->lambda = s->picture->quality * LAMBDA_SCALE;
1021 
1022  copy_frame(s);
1023  reinit(s);
1024 
1025  if (s->format == CODEC_JP2) {
1026  av_assert0(s->buf == pkt->data);
1027 
1028  bytestream_put_be32(&s->buf, 0x0000000C);
1029  bytestream_put_be32(&s->buf, 0x6A502020);
1030  bytestream_put_be32(&s->buf, 0x0D0A870A);
1031 
1032  chunkstart = s->buf;
1033  bytestream_put_be32(&s->buf, 0);
1034  bytestream_put_buffer(&s->buf, "ftyp", 4);
1035  bytestream_put_buffer(&s->buf, "jp2\040\040", 4);
1036  bytestream_put_be32(&s->buf, 0);
1037  bytestream_put_buffer(&s->buf, "jp2\040", 4);
1038  update_size(chunkstart, s->buf);
1039 
1040  jp2hstart = s->buf;
1041  bytestream_put_be32(&s->buf, 0);
1042  bytestream_put_buffer(&s->buf, "jp2h", 4);
1043 
1044  chunkstart = s->buf;
1045  bytestream_put_be32(&s->buf, 0);
1046  bytestream_put_buffer(&s->buf, "ihdr", 4);
1047  bytestream_put_be32(&s->buf, avctx->height);
1048  bytestream_put_be32(&s->buf, avctx->width);
1049  bytestream_put_be16(&s->buf, s->ncomponents);
1050  bytestream_put_byte(&s->buf, s->cbps[0]);
1051  bytestream_put_byte(&s->buf, 7);
1052  bytestream_put_byte(&s->buf, 0);
1053  bytestream_put_byte(&s->buf, 0);
1054  update_size(chunkstart, s->buf);
1055 
1056  chunkstart = s->buf;
1057  bytestream_put_be32(&s->buf, 0);
1058  bytestream_put_buffer(&s->buf, "colr", 4);
1059  bytestream_put_byte(&s->buf, 1);
1060  bytestream_put_byte(&s->buf, 0);
1061  bytestream_put_byte(&s->buf, 0);
1062  if ((desc->flags & AV_PIX_FMT_FLAG_RGB) || avctx->pix_fmt == AV_PIX_FMT_PAL8) {
1063  bytestream_put_be32(&s->buf, 16);
1064  } else if (s->ncomponents == 1) {
1065  bytestream_put_be32(&s->buf, 17);
1066  } else {
1067  bytestream_put_be32(&s->buf, 18);
1068  }
1069  update_size(chunkstart, s->buf);
1070  if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
1071  int i;
1072  uint8_t *palette = pict->data[1];
1073  chunkstart = s->buf;
1074  bytestream_put_be32(&s->buf, 0);
1075  bytestream_put_buffer(&s->buf, "pclr", 4);
1076  bytestream_put_be16(&s->buf, AVPALETTE_COUNT);
1077  bytestream_put_byte(&s->buf, 3); // colour channels
1078  bytestream_put_be24(&s->buf, 0x070707); //colour depths
1079  for (i = 0; i < AVPALETTE_COUNT; i++) {
1080  bytestream_put_be24(&s->buf, HAVE_BIGENDIAN ? AV_RB24(palette + 1) : AV_RL24(palette));
1081  palette += 4;
1082  }
1083  update_size(chunkstart, s->buf);
1084  chunkstart = s->buf;
1085  bytestream_put_be32(&s->buf, 0);
1086  bytestream_put_buffer(&s->buf, "cmap", 4);
1087  for (i = 0; i < 3; i++) {
1088  bytestream_put_be16(&s->buf, 0); // component
1089  bytestream_put_byte(&s->buf, 1); // palette mapping
1090  bytestream_put_byte(&s->buf, i); // index
1091  }
1092  update_size(chunkstart, s->buf);
1093  }
1094  update_size(jp2hstart, s->buf);
1095 
1096  jp2cstart = s->buf;
1097  bytestream_put_be32(&s->buf, 0);
1098  bytestream_put_buffer(&s->buf, "jp2c", 4);
1099  }
1100 
1101  if (s->buf_end - s->buf < 2)
1102  return -1;
1103  bytestream_put_be16(&s->buf, JPEG2000_SOC);
1104  if ((ret = put_siz(s)) < 0)
1105  return ret;
1106  if ((ret = put_cod(s)) < 0)
1107  return ret;
1108  if ((ret = put_qcd(s, 0)) < 0)
1109  return ret;
1110  if ((ret = put_com(s, 0)) < 0)
1111  return ret;
1112 
1113  for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
1114  uint8_t *psotptr;
1115  if (!(psotptr = put_sot(s, tileno)))
1116  return -1;
1117  if (s->buf_end - s->buf < 2)
1118  return -1;
1119  bytestream_put_be16(&s->buf, JPEG2000_SOD);
1120  if ((ret = encode_tile(s, s->tile + tileno, tileno)) < 0)
1121  return ret;
1122  bytestream_put_be32(&psotptr, s->buf - psotptr + 6);
1123  }
1124  if (s->buf_end - s->buf < 2)
1125  return -1;
1126  bytestream_put_be16(&s->buf, JPEG2000_EOC);
1127 
1128  if (s->format == CODEC_JP2)
1129  update_size(jp2cstart, s->buf);
1130 
1131  av_log(s->avctx, AV_LOG_DEBUG, "end\n");
1132  pkt->size = s->buf - s->buf_start;
1133  pkt->flags |= AV_PKT_FLAG_KEY;
1134  *got_packet = 1;
1135 
1136  return 0;
1137 }
1138 
1140 {
1141  int i, ret;
1142  Jpeg2000EncoderContext *s = avctx->priv_data;
1143  Jpeg2000CodingStyle *codsty = &s->codsty;
1144  Jpeg2000QuantStyle *qntsty = &s->qntsty;
1145 
1146  s->avctx = avctx;
1147  av_log(s->avctx, AV_LOG_DEBUG, "init\n");
1148 
1149 #if FF_API_PRIVATE_OPT
1151  if (avctx->prediction_method)
1152  s->pred = avctx->prediction_method;
1154 #endif
1155 
1156  if (avctx->pix_fmt == AV_PIX_FMT_PAL8 && (s->pred != FF_DWT97_INT || s->format != CODEC_JP2)) {
1157  av_log(s->avctx, AV_LOG_WARNING, "Forcing lossless jp2 for pal8\n");
1158  s->pred = 1;
1159  s->format = CODEC_JP2;
1160  }
1161 
1162  // defaults:
1163  // TODO: implement setting non-standard precinct size
1164  memset(codsty->log2_prec_widths , 15, sizeof(codsty->log2_prec_widths ));
1165  memset(codsty->log2_prec_heights, 15, sizeof(codsty->log2_prec_heights));
1166  codsty->nreslevels2decode=
1167  codsty->nreslevels = 7;
1168  codsty->log2_cblk_width = 4;
1169  codsty->log2_cblk_height = 4;
1170  codsty->transform = s->pred ? FF_DWT53 : FF_DWT97_INT;
1171 
1172  qntsty->nguardbits = 1;
1173 
1174  if ((s->tile_width & (s->tile_width -1)) ||
1175  (s->tile_height & (s->tile_height-1))) {
1176  av_log(avctx, AV_LOG_WARNING, "Tile dimension not a power of 2\n");
1177  }
1178 
1179  if (codsty->transform == FF_DWT53)
1180  qntsty->quantsty = JPEG2000_QSTY_NONE;
1181  else
1182  qntsty->quantsty = JPEG2000_QSTY_SE;
1183 
1184  s->width = avctx->width;
1185  s->height = avctx->height;
1186 
1187  for (i = 0; i < 3; i++)
1188  s->cbps[i] = 8;
1189 
1190  if (avctx->pix_fmt == AV_PIX_FMT_RGB24){
1191  s->ncomponents = 3;
1192  } else if (avctx->pix_fmt == AV_PIX_FMT_GRAY8 || avctx->pix_fmt == AV_PIX_FMT_PAL8){
1193  s->ncomponents = 1;
1194  } else{ // planar YUV
1195  s->planar = 1;
1196  s->ncomponents = 3;
1198  s->chroma_shift, s->chroma_shift + 1);
1199  if (ret)
1200  return ret;
1201  }
1202 
1205  init_luts();
1206 
1207  init_quantization(s);
1208  if ((ret=init_tiles(s)) < 0)
1209  return ret;
1210 
1211  av_log(s->avctx, AV_LOG_DEBUG, "after init\n");
1212 
1213  return 0;
1214 }
1215 
1216 static int j2kenc_destroy(AVCodecContext *avctx)
1217 {
1218  Jpeg2000EncoderContext *s = avctx->priv_data;
1219 
1220  cleanup(s);
1221  return 0;
1222 }
1223 
1224 // taken from the libopenjpeg wraper so it matches
1225 
1226 #define OFFSET(x) offsetof(Jpeg2000EncoderContext, x)
1227 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1228 static const AVOption options[] = {
1229  { "format", "Codec Format", OFFSET(format), AV_OPT_TYPE_INT, { .i64 = CODEC_JP2 }, CODEC_J2K, CODEC_JP2, VE, "format" },
1230  { "j2k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CODEC_J2K }, 0, 0, VE, "format" },
1231  { "jp2", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CODEC_JP2 }, 0, 0, VE, "format" },
1232  { "tile_width", "Tile Width", OFFSET(tile_width), AV_OPT_TYPE_INT, { .i64 = 256 }, 1, 1<<30, VE, },
1233  { "tile_height", "Tile Height", OFFSET(tile_height), AV_OPT_TYPE_INT, { .i64 = 256 }, 1, 1<<30, VE, },
1234  { "pred", "DWT Type", OFFSET(pred), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE, "pred" },
1235  { "dwt97int", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, VE, "pred" },
1236  { "dwt53", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, VE, "pred" },
1237 
1238  { NULL }
1239 };
1240 
1241 static const AVClass j2k_class = {
1242  .class_name = "jpeg 2000 encoder",
1243  .item_name = av_default_item_name,
1244  .option = options,
1245  .version = LIBAVUTIL_VERSION_INT,
1246 };
1247 
1249  .name = "jpeg2000",
1250  .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
1251  .type = AVMEDIA_TYPE_VIDEO,
1252  .id = AV_CODEC_ID_JPEG2000,
1253  .priv_data_size = sizeof(Jpeg2000EncoderContext),
1254  .init = j2kenc_init,
1255  .encode2 = encode_frame,
1256  .close = j2kenc_destroy,
1257  .pix_fmts = (const enum AVPixelFormat[]) {
1263  },
1264  .priv_class = &j2k_class,
1265  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
1266 };
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: internal.h:48
static int getcut(Jpeg2000Cblk *cblk, int64_t lambda, int dwt_norm)
Definition: j2kenc.c:835
uint8_t nguardbits
Definition: jpeg2000.h:153
#define NULL
Definition: coverity.c:32
#define NMSEDEC_BITS
Definition: j2kenc.c:76
void av_cold ff_jpeg2000_init_tier1_luts(void)
Definition: jpeg2000.c:159
void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
Definition: jpeg2000.c:585
static const char * format[]
Definition: af_aiir.c:339
int size
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2549
This structure describes decoded (raw) audio or video data.
Definition: frame.h:300
DWTContext dwt
Definition: jpeg2000.h:208
AVOption.
Definition: opt.h:246
Jpeg2000TgtNode * cblkincl
Definition: jpeg2000.h:184
static void cleanup(Jpeg2000EncoderContext *s)
Definition: j2kenc.c:969
#define JPEG2000_T1_SIG_NB
Definition: jpeg2000.h:85
AVCodec ff_jpeg2000_encoder
Definition: j2kenc.c:1248
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:208
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:68
const char * desc
Definition: nvenc.c:79
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
uint8_t ninclpasses
Definition: jpeg2000.h:165
Jpeg2000QuantStyle qntsty
Definition: j2kenc.c:126
static int lut_nmsedec_ref[1<< NMSEDEC_BITS]
Definition: j2kenc.c:84
static av_cold int j2kenc_init(AVCodecContext *avctx)
Definition: j2kenc.c:1139
int size
Definition: packet.h:356
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
int nb_codeblocks_width
Definition: jpeg2000.h:181
#define AV_RB24
Definition: intreadwrite.h:64
static int put_com(Jpeg2000EncoderContext *s, int compno)
Definition: j2kenc.c:361
AVCodecContext * avctx
Definition: j2kenc.c:107
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:736
uint16_t mant[JPEG2000_MAX_DECLEVELS *3]
Definition: jpeg2000.h:151
void ff_mqc_initenc(MqcState *mqc, uint8_t *bp)
initialize the encoder
Definition: mqcenc.c:69
static void tag_tree_code(Jpeg2000EncoderContext *s, Jpeg2000TgtNode *node, int threshold)
code the value stored in node
Definition: j2kenc.c:239
static AVPacket pkt
AVCodec.
Definition: codec.h:190
static void tag_tree_update(Jpeg2000TgtNode *node)
update the value in node
Definition: j2kenc.c:267
static void update_size(uint8_t *size, const uint8_t *end)
Definition: j2kenc.c:998
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
Definition: mem.c:245
static int getnmsedec_ref(int x, int bpno)
Definition: j2kenc.c:543
uint8_t npasses
Definition: jpeg2000.h:164
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
uint16_t log2_cblk_width
Definition: jpeg2000.h:192
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
Definition: encode.c:32
uint8_t
#define av_cold
Definition: attributes.h:88
#define av_malloc(s)
static int lut_nmsedec_ref0[1<< NMSEDEC_BITS]
Definition: j2kenc.c:84
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:77
AVOptions.
static int encode_packet(Jpeg2000EncoderContext *s, Jpeg2000ResLevel *rlevel, int precno, uint8_t *expn, int numgbits)
Definition: j2kenc.c:714
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:92
static const AVClass j2k_class
Definition: j2kenc.c:1241
Jpeg2000TgtNode * zerobits
Definition: jpeg2000.h:183
int64_t disto
Definition: jpeg2000.h:158
Jpeg2000Band * band
Definition: jpeg2000.h:203
uint8_t val
Definition: jpeg2000.h:129
int coord[2][2]
Definition: jpeg2000.h:211
#define height
uint8_t * data
Definition: packet.h:355
static void j2k_flush(Jpeg2000EncoderContext *s)
flush the bitstream
Definition: j2kenc.c:228
#define sp
Definition: regdef.h:63
static int init_tiles(Jpeg2000EncoderContext *s)
compute the sizes of tiles, resolution levels, bands, etc.
Definition: j2kenc.c:404
#define JPEG2000_T1_VIS
Definition: jpeg2000.h:95
#define max(a, b)
Definition: cuda_runtime.h:33
#define AV_INPUT_BUFFER_MIN_SIZE
minimum encoding buffer size Used to avoid some checks during header writing.
Definition: avcodec.h:222
uint16_t flags[6156]
Definition: jpeg2000.h:123
#define av_log(a,...)
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:388
uint8_t nonzerobits
Definition: jpeg2000.h:166
uint8_t log2_prec_widths[JPEG2000_MAX_RESLEVELS]
Definition: jpeg2000.h:145
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
static const uint16_t mask[17]
Definition: lzw.c:38
uint8_t * buf_end
Definition: j2kenc.c:120
#define OFFSET(x)
Definition: j2kenc.c:1226
void ff_jpeg2000_set_significance(Jpeg2000T1Context *t1, int x, int y, int negative)
Definition: jpeg2000.c:171
#define AVERROR(e)
Definition: error.h:43
static int put_cod(Jpeg2000EncoderContext *s)
Definition: j2kenc.c:308
#define AV_PIX_FMT_FLAG_RGB
The pixel format contains RGB-like data (as opposed to YUV/grayscale).
Definition: pixdesc.h:148
int nb_codeblocks_height
Definition: jpeg2000.h:182
#define CODEC_J2K
Definition: j2kenc.c:82
int av_pix_fmt_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
Utility function to access log2_chroma_w log2_chroma_h from the pixel format AVPixFmtDescriptor.
Definition: pixdesc.c:2577
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:188
unsigned int pos
Definition: spdifenc.c:412
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
uint8_t log2_prec_heights[JPEG2000_MAX_RESLEVELS]
Definition: jpeg2000.h:146
#define t1
Definition: regdef.h:29
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:606
#define JPEG2000_MAX_PASSES
Definition: jpeg2000.h:73
Definition: graph2dot.c:48
static void put_num(Jpeg2000EncoderContext *s, int num, int n)
put n least significant bits of a number num
Definition: j2kenc.c:221
const char * name
Name of the codec implementation.
Definition: codec.h:197
static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
Definition: j2kenc.c:1003
const AVFrame * picture
Definition: j2kenc.c:108
uint8_t flushed[4]
Definition: jpeg2000.h:159
Jpeg2000Cblk * cblk
Definition: jpeg2000.h:185
#define FFMAX(a, b)
Definition: common.h:94
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:361
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
static int put_siz(Jpeg2000EncoderContext *s)
Definition: j2kenc.c:279
static int lut_nmsedec_sig0[1<< NMSEDEC_BITS]
Definition: j2kenc.c:84
#define JPEG2000_T1_REF
Definition: jpeg2000.h:97
uint64_t flags
Combination of AV_PIX_FMT_FLAG_...
Definition: pixdesc.h:106
#define ss(width, name, subs,...)
Definition: cbs_vp9.c:261
int flushed_len
Definition: jpeg2000.h:160
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition: avcodec.h:333
static int put_qcd(Jpeg2000EncoderContext *s, int compno)
Definition: j2kenc.c:335
static int getnmsedec_sig(int x, int bpno)
Definition: j2kenc.c:536
int tile_height
tile size
Definition: j2kenc.c:115
#define FFMIN(a, b)
Definition: common.h:96
struct Jpeg2000TgtNode * parent
Definition: jpeg2000.h:131
#define xi(width, name, var, range_min, range_max, subs,...)
Definition: cbs_h2645.c:396
static void encode_sigpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
Definition: j2kenc.c:550
#define width
int width
picture width / height.
Definition: avcodec.h:699
JPEG 2000 structures and defines common to encoder and decoder.
int i_stepsize
Definition: jpeg2000.h:193
static int j2kenc_destroy(AVCodecContext *avctx)
Definition: j2kenc.c:1216
#define MQC_CX_RL
Definition: mqc.h:34
#define CODEC_JP2
Definition: j2kenc.c:81
#define s(width, name)
Definition: cbs_vp9.c:257
static int ff_jpeg2000_getsigctxno(int flag, int bandno)
Definition: jpeg2000.h:241
int quality
quality (between 1 (good) and FF_LAMBDA_MAX (bad))
Definition: frame.h:423
uint8_t cbps[4]
bits per sample in particular components
Definition: j2kenc.c:111
int ff_dwt_encode(DWTContext *s, void *t)
Definition: jpeg2000dwt.c:581
int coord[2][2]
Definition: jpeg2000.h:200
uint8_t * data
Definition: jpeg2000.h:171
#define av_log2
Definition: intmath.h:83
static int encode_tile(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno)
Definition: j2kenc.c:882
static int ff_jpeg2000_ceildivpow2(int a, int b)
Definition: jpeg2000.h:217
static const float pred[4]
Definition: siprdata.h:259
static void comp(unsigned char *dst, ptrdiff_t dst_stride, unsigned char *src, ptrdiff_t src_stride, int add)
Definition: eamad.c:83
int coord[2][2]
Definition: jpeg2000.h:191
int coord_o[2][2]
Definition: jpeg2000.h:212
Libavcodec external API header.
static void encode_refpass(Jpeg2000T1Context *t1, int width, int height, int *nmsedec, int bpno)
Definition: j2kenc.c:572
attribute_deprecated int prediction_method
Definition: avcodec.h:885
uint8_t * buf_start
Definition: j2kenc.c:118
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:331
Jpeg2000CodingStyle codsty
Definition: j2kenc.c:125
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
main external API structure.
Definition: avcodec.h:526
long long int64_t
Definition: coverity.c:34
uint8_t vis
Definition: jpeg2000.h:130
void av_cold ff_mqc_init_context_tables(void)
MQ-coder Initialize context tables (QE, NLPS, NMPS)
Definition: mqc.c:97
static void init_luts(void)
Definition: j2kenc.c:518
static void encode_cblk(Jpeg2000EncoderContext *s, Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk, Jpeg2000Tile *tile, int width, int height, int bandpos, int lev)
Definition: j2kenc.c:641
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:72
Describe the class of an AVClass context structure.
Definition: log.h:67
static const AVOption options[]
Definition: j2kenc.c:1228
#define AV_WB32(p, v)
Definition: intreadwrite.h:419
uint8_t nbands
Definition: jpeg2000.h:199
#define JPEG2000_T1_SGN
Definition: jpeg2000.h:99
int height
image width and height
Definition: j2kenc.c:110
Jpeg2000ResLevel * reslevel
Definition: jpeg2000.h:207
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:275
static int ff_jpeg2000_ceildiv(int a, int b)
Definition: jpeg2000.h:222
#define AVPALETTE_COUNT
Definition: pixfmt.h:33
Jpeg2000Component * comp
Definition: j2kenc.c:102
void ff_jpeg2000_reinit(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
Definition: jpeg2000.c:564
#define AV_RL24
Definition: intreadwrite.h:78
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:314
uint8_t expn[JPEG2000_MAX_DECLEVELS *3]
Definition: jpeg2000.h:150
static void reinit(Jpeg2000EncoderContext *s)
Definition: j2kenc.c:988
static uint8_t * put_sot(Jpeg2000EncoderContext *s, int tileno)
Definition: j2kenc.c:380
Jpeg2000Tile * tile
Definition: j2kenc.c:128
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
Y , 8bpp.
Definition: pixfmt.h:74
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:84
uint8_t quantsty
Definition: jpeg2000.h:152
common internal api header.
common internal and external API header
if(ret< 0)
Definition: vf_mcdeint.c:279
uint8_t log2_cblk_width
Definition: jpeg2000.h:137
#define bit(string, value)
Definition: cbs_mpeg2.c:58
static void copy_frame(Jpeg2000EncoderContext *s)
Definition: j2kenc.c:450
#define LAMBDA_SCALE
Definition: j2kenc.c:79
int data[6144]
Definition: jpeg2000.h:122
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:73
uint16_t log2_cblk_height
Definition: jpeg2000.h:192
#define VE
Definition: j2kenc.c:1227
static av_always_inline void bytestream_put_buffer(uint8_t **b, const uint8_t *src, unsigned int size)
Definition: bytestream.h:368
#define MQC_CX_UNI
Definition: mqc.h:33
void * priv_data
Definition: avcodec.h:553
int ff_jpeg2000_init_component(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty, Jpeg2000QuantStyle *qntsty, int cbps, int dx, int dy, AVCodecContext *avctx)
Definition: jpeg2000.c:451
uint16_t rate
Definition: jpeg2000.h:157
static void putnumpasses(Jpeg2000EncoderContext *s, int n)
Definition: j2kenc.c:699
int ff_mqc_flush_to(MqcState *mqc, uint8_t *dst, int *dst_len)
Definition: mqcenc.c:122
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:85
static void truncpasses(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile)
Definition: j2kenc.c:853
Jpeg2000Prec * prec
Definition: jpeg2000.h:195
static void encode_clnpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
Definition: j2kenc.c:586
#define JPEG2000_T1_SIG
Definition: jpeg2000.h:96
static int lut_nmsedec_sig[1<< NMSEDEC_BITS]
Definition: j2kenc.c:84
#define LIBAVCODEC_IDENT
Definition: version.h:42
#define av_freep(p)
MqcState mqc
Definition: jpeg2000.h:124
static int ff_jpeg2000_getrefctxno(int flag)
Definition: jpeg2000.h:250
uint8_t log2_cblk_height
Definition: jpeg2000.h:137
#define av_malloc_array(a, b)
#define HAVE_BIGENDIAN
Definition: config.h:199
#define NMSEDEC_FRACBITS
Definition: j2kenc.c:77
static void init_quantization(Jpeg2000EncoderContext *s)
Definition: j2kenc.c:488
Jpeg2000Pass * passes
Definition: jpeg2000.h:176
static const int dwt_norms[2][4][10]
Definition: j2kenc.c:89
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
static double val(void *priv, double ch)
Definition: aeval.c:76
This structure stores compressed data.
Definition: packet.h:332
for(j=16;j >0;--j)
uint8_t cx_states[19]
Definition: mqc.h:45
#define WMSEDEC_SHIFT
must be >= 13
Definition: j2kenc.c:78
static int encode_packets(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno)
Definition: j2kenc.c:812
void * av_mallocz_array(size_t nmemb, size_t size)
Allocate a memory block for an array with av_mallocz().
Definition: mem.c:190
static int ff_jpeg2000_getsgnctxno(int flag, int *xorbit)
Definition: jpeg2000.h:259
void ff_mqc_encode(MqcState *mqc, uint8_t *cxstate, int d)
code bit d with context cx
Definition: mqcenc.c:79