FFmpeg  4.4.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  * Copyright (c) 2020, Gautam Ramakrishnan <gautamramk@gmail.com>
36  * All rights reserved.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  * 1. Redistributions of source code must retain the above copyright
42  * notice, this list of conditions and the following disclaimer.
43  * 2. Redistributions in binary form must reproduce the above copyright
44  * notice, this list of conditions and the following disclaimer in the
45  * documentation and/or other materials provided with the distribution.
46  *
47  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
48  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
51  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
52  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
53  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
54  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
55  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
56  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
57  * POSSIBILITY OF SUCH DAMAGE.
58  */
59 
60 
61 /**
62  * JPEG2000 image encoder
63  * @file
64  * @author Kamil Nowosad
65  */
66 
67 #include <float.h>
68 #include "avcodec.h"
69 #include "internal.h"
70 #include "bytestream.h"
71 #include "jpeg2000.h"
72 #include "libavutil/common.h"
73 #include "libavutil/pixdesc.h"
74 #include "libavutil/opt.h"
75 #include "libavutil/intreadwrite.h"
76 #include "libavutil/avstring.h"
77 
78 #define NMSEDEC_BITS 7
79 #define NMSEDEC_FRACBITS (NMSEDEC_BITS-1)
80 #define WMSEDEC_SHIFT 13 ///< must be >= 13
81 #define LAMBDA_SCALE (100000000LL << (WMSEDEC_SHIFT - 13))
82 
83 #define CODEC_JP2 1
84 #define CODEC_J2K 0
85 
86 static int lut_nmsedec_ref [1<<NMSEDEC_BITS],
90 
91 static const int dwt_norms[2][4][10] = { // [dwt_type][band][rlevel] (multiplied by 10000)
92  {{10000, 19650, 41770, 84030, 169000, 338400, 676900, 1353000, 2706000, 5409000},
93  {20220, 39890, 83550, 170400, 342700, 686300, 1373000, 2746000, 5490000},
94  {20220, 39890, 83550, 170400, 342700, 686300, 1373000, 2746000, 5490000},
95  {20800, 38650, 83070, 171800, 347100, 695900, 1393000, 2786000, 5572000}},
96 
97  {{10000, 15000, 27500, 53750, 106800, 213400, 426700, 853300, 1707000, 3413000},
98  {10380, 15920, 29190, 57030, 113300, 226400, 452500, 904800, 1809000},
99  {10380, 15920, 29190, 57030, 113300, 226400, 452500, 904800, 1809000},
100  { 7186, 9218, 15860, 30430, 60190, 120100, 240000, 479700, 959300}}
101 };
102 
103 typedef struct {
105  double *layer_rates;
106 } Jpeg2000Tile;
107 
108 typedef struct {
109  AVClass *class;
111  const AVFrame *picture;
112 
113  int width, height; ///< image width and height
114  uint8_t cbps[4]; ///< bits per sample in particular components
115  int chroma_shift[2];
118  int tile_width, tile_height; ///< tile size
119  int numXtiles, numYtiles;
120 
125 
127 
130 
132  int layer_rates[100];
133  uint8_t compression_rate_enc; ///< Is compression done using compression ratio?
134 
135  int format;
136  int pred;
137  int sop;
138  int eph;
139  int prog;
140  int nlayers;
141  char *lr_str;
143 
144 
145 /* debug */
146 #if 0
147 #undef ifprintf
148 #undef printf
149 
150 static void nspaces(FILE *fd, int n)
151 {
152  while(n--) putc(' ', fd);
153 }
154 
155 static void printcomp(Jpeg2000Component *comp)
156 {
157  int i;
158  for (i = 0; i < comp->y1 - comp->y0; i++)
159  ff_jpeg2000_printv(comp->i_data + i * (comp->x1 - comp->x0), comp->x1 - comp->x0);
160 }
161 
162 static void dump(Jpeg2000EncoderContext *s, FILE *fd)
163 {
164  int tileno, compno, reslevelno, bandno, precno;
165  fprintf(fd, "XSiz = %d, YSiz = %d, tile_width = %d, tile_height = %d\n"
166  "numXtiles = %d, numYtiles = %d, ncomponents = %d\n"
167  "tiles:\n",
168  s->width, s->height, s->tile_width, s->tile_height,
169  s->numXtiles, s->numYtiles, s->ncomponents);
170  for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
171  Jpeg2000Tile *tile = s->tile + tileno;
172  nspaces(fd, 2);
173  fprintf(fd, "tile %d:\n", tileno);
174  for(compno = 0; compno < s->ncomponents; compno++){
175  Jpeg2000Component *comp = tile->comp + compno;
176  nspaces(fd, 4);
177  fprintf(fd, "component %d:\n", compno);
178  nspaces(fd, 4);
179  fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d\n",
180  comp->x0, comp->x1, comp->y0, comp->y1);
181  for(reslevelno = 0; reslevelno < s->nreslevels; reslevelno++){
182  Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
183  nspaces(fd, 6);
184  fprintf(fd, "reslevel %d:\n", reslevelno);
185  nspaces(fd, 6);
186  fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d, nbands = %d\n",
187  reslevel->x0, reslevel->x1, reslevel->y0,
188  reslevel->y1, reslevel->nbands);
189  for(bandno = 0; bandno < reslevel->nbands; bandno++){
190  Jpeg2000Band *band = reslevel->band + bandno;
191  nspaces(fd, 8);
192  fprintf(fd, "band %d:\n", bandno);
193  nspaces(fd, 8);
194  fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d,"
195  "codeblock_width = %d, codeblock_height = %d cblknx = %d cblkny = %d\n",
196  band->x0, band->x1,
197  band->y0, band->y1,
198  band->codeblock_width, band->codeblock_height,
199  band->cblknx, band->cblkny);
200  for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
201  Jpeg2000Prec *prec = band->prec + precno;
202  nspaces(fd, 10);
203  fprintf(fd, "prec %d:\n", precno);
204  nspaces(fd, 10);
205  fprintf(fd, "xi0 = %d, xi1 = %d, yi0 = %d, yi1 = %d\n",
206  prec->xi0, prec->xi1, prec->yi0, prec->yi1);
207  }
208  }
209  }
210  }
211  }
212 }
213 #endif
214 
215 /* bitstream routines */
216 
217 /** put n times val bit */
218 static void put_bits(Jpeg2000EncoderContext *s, int val, int n) // TODO: optimize
219 {
220  while (n-- > 0){
221  if (s->bit_index == 8)
222  {
223  s->bit_index = *s->buf == 0xff;
224  *(++s->buf) = 0;
225  }
226  *s->buf |= val << (7 - s->bit_index++);
227  }
228 }
229 
230 /** put n least significant bits of a number num */
231 static void put_num(Jpeg2000EncoderContext *s, int num, int n)
232 {
233  while(--n >= 0)
234  put_bits(s, (num >> n) & 1, 1);
235 }
236 
237 /** flush the bitstream */
239 {
240  if (s->bit_index){
241  s->bit_index = 0;
242  if (*s->buf == 0xff)
243  *(++s->buf) = 0;
244  s->buf++;
245  }
246 }
247 
248 /* tag tree routines */
249 
250 /** code the value stored in node */
251 static void tag_tree_code(Jpeg2000EncoderContext *s, Jpeg2000TgtNode *node, int threshold)
252 {
253  Jpeg2000TgtNode *stack[30];
254  int sp = -1, curval = 0;
255 
256  while(node->parent){
257  stack[++sp] = node;
258  node = node->parent;
259  }
260 
261  while (1) {
262  if (curval > node->temp_val)
263  node->temp_val = curval;
264  else {
265  curval = node->temp_val;
266  }
267 
268  if (node->val >= threshold) {
269  put_bits(s, 0, threshold - curval);
270  curval = threshold;
271  } else {
272  put_bits(s, 0, node->val - curval);
273  curval = node->val;
274  if (!node->vis) {
275  put_bits(s, 1, 1);
276  node->vis = 1;
277  }
278  }
279 
280  node->temp_val = curval;
281  if (sp < 0)
282  break;
283  node = stack[sp--];
284  }
285 }
286 
287 /** update the value in node */
289 {
290  int lev = 0;
291  while (node->parent){
292  if (node->parent->val <= node->val)
293  break;
294  node->parent->val = node->val;
295  node = node->parent;
296  lev++;
297  }
298 }
299 
301 {
302  int i;
303 
304  if (s->buf_end - s->buf < 40 + 3 * s->ncomponents)
305  return -1;
306 
307  bytestream_put_be16(&s->buf, JPEG2000_SIZ);
308  bytestream_put_be16(&s->buf, 38 + 3 * s->ncomponents); // Lsiz
309  bytestream_put_be16(&s->buf, 0); // Rsiz
310  bytestream_put_be32(&s->buf, s->width); // width
311  bytestream_put_be32(&s->buf, s->height); // height
312  bytestream_put_be32(&s->buf, 0); // X0Siz
313  bytestream_put_be32(&s->buf, 0); // Y0Siz
314 
315  bytestream_put_be32(&s->buf, s->tile_width); // XTSiz
316  bytestream_put_be32(&s->buf, s->tile_height); // YTSiz
317  bytestream_put_be32(&s->buf, 0); // XT0Siz
318  bytestream_put_be32(&s->buf, 0); // YT0Siz
319  bytestream_put_be16(&s->buf, s->ncomponents); // CSiz
320 
321  for (i = 0; i < s->ncomponents; i++){ // Ssiz_i XRsiz_i, YRsiz_i
322  bytestream_put_byte(&s->buf, s->cbps[i] - 1);
323  bytestream_put_byte(&s->buf, i?1<<s->chroma_shift[0]:1);
324  bytestream_put_byte(&s->buf, i?1<<s->chroma_shift[1]:1);
325  }
326  return 0;
327 }
328 
330 {
331  Jpeg2000CodingStyle *codsty = &s->codsty;
332  uint8_t scod = 0;
333 
334  if (s->buf_end - s->buf < 14)
335  return -1;
336 
337  bytestream_put_be16(&s->buf, JPEG2000_COD);
338  bytestream_put_be16(&s->buf, 12); // Lcod
339  if (s->sop)
340  scod |= JPEG2000_CSTY_SOP;
341  if (s->eph)
342  scod |= JPEG2000_CSTY_EPH;
343  bytestream_put_byte(&s->buf, scod); // Scod
344  // SGcod
345  bytestream_put_byte(&s->buf, s->prog); // progression level
346  bytestream_put_be16(&s->buf, s->nlayers); // num of layers
347  if(s->avctx->pix_fmt == AV_PIX_FMT_YUV444P){
348  bytestream_put_byte(&s->buf, 0); // unspecified
349  }else{
350  bytestream_put_byte(&s->buf, 0); // unspecified
351  }
352  // SPcod
353  bytestream_put_byte(&s->buf, codsty->nreslevels - 1); // num of decomp. levels
354  bytestream_put_byte(&s->buf, codsty->log2_cblk_width-2); // cblk width
355  bytestream_put_byte(&s->buf, codsty->log2_cblk_height-2); // cblk height
356  bytestream_put_byte(&s->buf, 0); // cblk style
357  bytestream_put_byte(&s->buf, codsty->transform == FF_DWT53); // transformation
358  return 0;
359 }
360 
361 static int put_qcd(Jpeg2000EncoderContext *s, int compno)
362 {
363  int i, size;
364  Jpeg2000CodingStyle *codsty = &s->codsty;
365  Jpeg2000QuantStyle *qntsty = &s->qntsty;
366 
367  if (qntsty->quantsty == JPEG2000_QSTY_NONE)
368  size = 4 + 3 * (codsty->nreslevels-1);
369  else // QSTY_SE
370  size = 5 + 6 * (codsty->nreslevels-1);
371 
372  if (s->buf_end - s->buf < size + 2)
373  return -1;
374 
375  bytestream_put_be16(&s->buf, JPEG2000_QCD);
376  bytestream_put_be16(&s->buf, size); // LQcd
377  bytestream_put_byte(&s->buf, (qntsty->nguardbits << 5) | qntsty->quantsty); // Sqcd
378  if (qntsty->quantsty == JPEG2000_QSTY_NONE)
379  for (i = 0; i < codsty->nreslevels * 3 - 2; i++)
380  bytestream_put_byte(&s->buf, qntsty->expn[i] << 3);
381  else // QSTY_SE
382  for (i = 0; i < codsty->nreslevels * 3 - 2; i++)
383  bytestream_put_be16(&s->buf, (qntsty->expn[i] << 11) | qntsty->mant[i]);
384  return 0;
385 }
386 
387 static int put_com(Jpeg2000EncoderContext *s, int compno)
388 {
389  int size = 4 + strlen(LIBAVCODEC_IDENT);
390 
391  if (s->avctx->flags & AV_CODEC_FLAG_BITEXACT)
392  return 0;
393 
394  if (s->buf_end - s->buf < size + 2)
395  return -1;
396 
397  bytestream_put_be16(&s->buf, JPEG2000_COM);
398  bytestream_put_be16(&s->buf, size);
399  bytestream_put_be16(&s->buf, 1); // General use (ISO/IEC 8859-15 (Latin) values)
400 
402 
403  return 0;
404 }
405 
407 {
408  uint8_t *psotptr;
409 
410  if (s->buf_end - s->buf < 12)
411  return NULL;
412 
413  bytestream_put_be16(&s->buf, JPEG2000_SOT);
414  bytestream_put_be16(&s->buf, 10); // Lsot
415  bytestream_put_be16(&s->buf, tileno); // Isot
416 
417  psotptr = s->buf;
418  bytestream_put_be32(&s->buf, 0); // Psot (filled in later)
419 
420  bytestream_put_byte(&s->buf, 0); // TPsot
421  bytestream_put_byte(&s->buf, 1); // TNsot
422  return psotptr;
423 }
424 
426 {
427  int i, j;
428  int layno, compno;
429  for (i = 0; i < s->numYtiles; i++) {
430  for (j = 0; j < s->numXtiles; j++) {
431  Jpeg2000Tile *tile = &s->tile[s->numXtiles * i + j];
432  for (compno = 0; compno < s->ncomponents; compno++) {
433  int tilew = tile->comp[compno].coord[0][1] - tile->comp[compno].coord[0][0];
434  int tileh = tile->comp[compno].coord[1][1] - tile->comp[compno].coord[1][0];
435  int scale = (compno?1 << s->chroma_shift[0]:1) * (compno?1 << s->chroma_shift[1]:1);
436  for (layno = 0; layno < s->nlayers; layno++) {
437  if (s->layer_rates[layno] > 0) {
438  tile->layer_rates[layno] += (double)(tilew * tileh) * s->ncomponents * s->cbps[compno] /
439  (double)(s->layer_rates[layno] * 8 * scale);
440  } else {
441  tile->layer_rates[layno] = 0.0;
442  }
443  }
444  }
445  }
446  }
447 
448 }
449 
450 /**
451  * compute the sizes of tiles, resolution levels, bands, etc.
452  * allocate memory for them
453  * divide the input image into tile-components
454  */
456 {
457  int tileno, tilex, tiley, compno;
458  Jpeg2000CodingStyle *codsty = &s->codsty;
459  Jpeg2000QuantStyle *qntsty = &s->qntsty;
460 
461  s->numXtiles = ff_jpeg2000_ceildiv(s->width, s->tile_width);
462  s->numYtiles = ff_jpeg2000_ceildiv(s->height, s->tile_height);
463 
464  s->tile = av_calloc(s->numXtiles, s->numYtiles * sizeof(Jpeg2000Tile));
465  if (!s->tile)
466  return AVERROR(ENOMEM);
467  for (tileno = 0, tiley = 0; tiley < s->numYtiles; tiley++)
468  for (tilex = 0; tilex < s->numXtiles; tilex++, tileno++){
469  Jpeg2000Tile *tile = s->tile + tileno;
470 
471  tile->comp = av_mallocz_array(s->ncomponents, sizeof(Jpeg2000Component));
472  if (!tile->comp)
473  return AVERROR(ENOMEM);
474 
475  tile->layer_rates = av_mallocz_array(s->nlayers, sizeof(*tile->layer_rates));
476  if (!tile->layer_rates)
477  return AVERROR(ENOMEM);
478 
479  for (compno = 0; compno < s->ncomponents; compno++){
480  Jpeg2000Component *comp = tile->comp + compno;
481  int ret, i, j;
482 
483  comp->coord[0][0] = comp->coord_o[0][0] = tilex * s->tile_width;
484  comp->coord[0][1] = comp->coord_o[0][1] = FFMIN((tilex+1)*s->tile_width, s->width);
485  comp->coord[1][0] = comp->coord_o[1][0] = tiley * s->tile_height;
486  comp->coord[1][1] = comp->coord_o[1][1] = FFMIN((tiley+1)*s->tile_height, s->height);
487  if (compno > 0)
488  for (i = 0; i < 2; i++)
489  for (j = 0; j < 2; j++)
490  comp->coord[i][j] = comp->coord_o[i][j] = ff_jpeg2000_ceildivpow2(comp->coord[i][j], s->chroma_shift[i]);
491 
492  if ((ret = ff_jpeg2000_init_component(comp,
493  codsty,
494  qntsty,
495  s->cbps[compno],
496  compno?1<<s->chroma_shift[0]:1,
497  compno?1<<s->chroma_shift[1]:1,
498  s->avctx
499  )) < 0)
500  return ret;
501  }
502  }
503  compute_rates(s);
504  return 0;
505 }
506 
507 #define COPY_FRAME(D, PIXEL) \
508  static void copy_frame_ ##D(Jpeg2000EncoderContext *s) \
509  { \
510  int tileno, compno, i, y, x; \
511  PIXEL *line; \
512  for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){ \
513  Jpeg2000Tile *tile = s->tile + tileno; \
514  if (s->planar){ \
515  for (compno = 0; compno < s->ncomponents; compno++){ \
516  Jpeg2000Component *comp = tile->comp + compno; \
517  int *dst = comp->i_data; \
518  int cbps = s->cbps[compno]; \
519  line = (PIXEL*)s->picture->data[compno] \
520  + comp->coord[1][0] * (s->picture->linesize[compno] / sizeof(PIXEL)) \
521  + comp->coord[0][0]; \
522  for (y = comp->coord[1][0]; y < comp->coord[1][1]; y++){ \
523  PIXEL *ptr = line; \
524  for (x = comp->coord[0][0]; x < comp->coord[0][1]; x++) \
525  *dst++ = *ptr++ - (1 << (cbps - 1)); \
526  line += s->picture->linesize[compno] / sizeof(PIXEL); \
527  } \
528  } \
529  } else{ \
530  line = (PIXEL*)s->picture->data[0] + tile->comp[0].coord[1][0] * (s->picture->linesize[0] / sizeof(PIXEL)) \
531  + tile->comp[0].coord[0][0] * s->ncomponents; \
532  \
533  i = 0; \
534  for (y = tile->comp[0].coord[1][0]; y < tile->comp[0].coord[1][1]; y++){ \
535  PIXEL *ptr = line; \
536  for (x = tile->comp[0].coord[0][0]; x < tile->comp[0].coord[0][1]; x++, i++){ \
537  for (compno = 0; compno < s->ncomponents; compno++){ \
538  int cbps = s->cbps[compno]; \
539  tile->comp[compno].i_data[i] = *ptr++ - (1 << (cbps - 1)); \
540  } \
541  } \
542  line += s->picture->linesize[0] / sizeof(PIXEL); \
543  } \
544  } \
545  } \
546  }
547 
549 COPY_FRAME(16, uint16_t)
550 
552 {
553  int compno, reslevelno, bandno;
554  Jpeg2000QuantStyle *qntsty = &s->qntsty;
555  Jpeg2000CodingStyle *codsty = &s->codsty;
556 
557  for (compno = 0; compno < s->ncomponents; compno++){
558  int gbandno = 0;
559  for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
560  int nbands, lev = codsty->nreslevels - reslevelno - 1;
561  nbands = reslevelno ? 3 : 1;
562  for (bandno = 0; bandno < nbands; bandno++, gbandno++){
563  int expn, mant = 0;
564 
565  if (codsty->transform == FF_DWT97_INT){
566  int bandpos = bandno + (reslevelno>0),
567  ss = 81920000 / dwt_norms[0][bandpos][lev],
568  log = av_log2(ss);
569  mant = (11 - log < 0 ? ss >> log - 11 : ss << 11 - log) & 0x7ff;
570  expn = s->cbps[compno] - log + 13;
571  } else
572  expn = ((bandno&2)>>1) + (reslevelno>0) + s->cbps[compno];
573 
574  qntsty->expn[gbandno] = expn;
575  qntsty->mant[gbandno] = mant;
576  }
577  }
578  }
579 }
580 
581 static void init_luts(void)
582 {
583  int i, a,
584  mask = ~((1<<NMSEDEC_FRACBITS)-1);
585 
586  for (i = 0; i < (1 << NMSEDEC_BITS); i++){
587  lut_nmsedec_sig[i] = FFMAX((3 * i << (13 - NMSEDEC_FRACBITS)) - (9 << 11), 0);
588  lut_nmsedec_sig0[i] = FFMAX((i*i + (1<<NMSEDEC_FRACBITS-1) & mask) << 1, 0);
589 
590  a = (i >> (NMSEDEC_BITS-2)&2) + 1;
591  lut_nmsedec_ref[i] = FFMAX((a - 2) * (i << (13 - NMSEDEC_FRACBITS)) +
592  (1 << 13) - (a * a << 11), 0);
593  lut_nmsedec_ref0[i] = FFMAX(((i * i - (i << NMSEDEC_BITS) + (1 << 2 * NMSEDEC_FRACBITS) + (1 << (NMSEDEC_FRACBITS - 1))) & mask)
594  << 1, 0);
595  }
596 }
597 
598 /* tier-1 routines */
599 static int getnmsedec_sig(int x, int bpno)
600 {
601  if (bpno > NMSEDEC_FRACBITS)
602  return lut_nmsedec_sig[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
603  return lut_nmsedec_sig0[x & ((1 << NMSEDEC_BITS) - 1)];
604 }
605 
606 static int getnmsedec_ref(int x, int bpno)
607 {
608  if (bpno > NMSEDEC_FRACBITS)
609  return lut_nmsedec_ref[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
610  return lut_nmsedec_ref0[x & ((1 << NMSEDEC_BITS) - 1)];
611 }
612 
613 static void encode_sigpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
614 {
615  int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
616  for (y0 = 0; y0 < height; y0 += 4)
617  for (x = 0; x < width; x++)
618  for (y = y0; y < height && y < y0+4; y++){
619  if (!(t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG) && (t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG_NB)){
620  int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno),
621  bit = t1->data[(y) * t1->stride + x] & mask ? 1 : 0;
622  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, bit);
623  if (bit){
624  int xorbit;
625  int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
626  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
627  *nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
628  ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
629  }
630  t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_VIS;
631  }
632  }
633 }
634 
635 static void encode_refpass(Jpeg2000T1Context *t1, int width, int height, int *nmsedec, int bpno)
636 {
637  int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
638  for (y0 = 0; y0 < height; y0 += 4)
639  for (x = 0; x < width; x++)
640  for (y = y0; y < height && y < y0+4; y++)
641  if ((t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG){
642  int ctxno = ff_jpeg2000_getrefctxno(t1->flags[(y+1) * t1->stride + x+1]);
643  *nmsedec += getnmsedec_ref(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
644  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
645  t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_REF;
646  }
647 }
648 
649 static void encode_clnpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
650 {
651  int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
652  for (y0 = 0; y0 < height; y0 += 4)
653  for (x = 0; x < width; x++){
654  if (y0 + 3 < height && !(
655  (t1->flags[(y0+1) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
656  (t1->flags[(y0+2) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
657  (t1->flags[(y0+3) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
658  (t1->flags[(y0+4) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG))))
659  {
660  // aggregation mode
661  int rlen;
662  for (rlen = 0; rlen < 4; rlen++)
663  if (t1->data[(y0+rlen) * t1->stride + x] & mask)
664  break;
665  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL, rlen != 4);
666  if (rlen == 4)
667  continue;
668  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen >> 1);
669  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen & 1);
670  for (y = y0 + rlen; y < y0 + 4; y++){
671  if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))){
672  int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno);
673  if (y > y0 + rlen)
674  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
675  if (t1->data[(y) * t1->stride + x] & mask){ // newly significant
676  int xorbit;
677  int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
678  *nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
679  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
680  ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
681  }
682  }
683  t1->flags[(y+1) * t1->stride + x+1] &= ~JPEG2000_T1_VIS;
684  }
685  } else{
686  for (y = y0; y < y0 + 4 && y < height; y++){
687  if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))){
688  int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno);
689  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
690  if (t1->data[(y) * t1->stride + x] & mask){ // newly significant
691  int xorbit;
692  int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
693  *nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
694  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
695  ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
696  }
697  }
698  t1->flags[(y+1) * t1->stride + x+1] &= ~JPEG2000_T1_VIS;
699  }
700  }
701  }
702 }
703 
705  int width, int height, int bandpos, int lev)
706 {
707  int pass_t = 2, passno, x, y, max=0, nmsedec, bpno;
708  int64_t wmsedec = 0;
709 
710  memset(t1->flags, 0, t1->stride * (height + 2) * sizeof(*t1->flags));
711 
712  for (y = 0; y < height; y++){
713  for (x = 0; x < width; x++){
714  if (t1->data[(y) * t1->stride + x] < 0){
715  t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_SGN;
716  t1->data[(y) * t1->stride + x] = -t1->data[(y) * t1->stride + x];
717  }
718  max = FFMAX(max, t1->data[(y) * t1->stride + x]);
719  }
720  }
721 
722  if (max == 0){
723  cblk->nonzerobits = 0;
724  } else{
725  cblk->nonzerobits = av_log2(max) + 1 - NMSEDEC_FRACBITS;
726  }
727  bpno = cblk->nonzerobits - 1;
728 
729  cblk->data[0] = 0;
730  ff_mqc_initenc(&t1->mqc, cblk->data + 1);
731 
732  for (passno = 0; bpno >= 0; passno++){
733  nmsedec=0;
734 
735  switch(pass_t){
736  case 0: encode_sigpass(t1, width, height, bandpos, &nmsedec, bpno);
737  break;
738  case 1: encode_refpass(t1, width, height, &nmsedec, bpno);
739  break;
740  case 2: encode_clnpass(t1, width, height, bandpos, &nmsedec, bpno);
741  break;
742  }
743 
744  cblk->passes[passno].rate = ff_mqc_flush_to(&t1->mqc, cblk->passes[passno].flushed, &cblk->passes[passno].flushed_len);
745  cblk->passes[passno].rate -= cblk->passes[passno].flushed_len;
746 
747  wmsedec += (int64_t)nmsedec << (2*bpno);
748  cblk->passes[passno].disto = wmsedec;
749 
750  if (++pass_t == 3){
751  pass_t = 0;
752  bpno--;
753  }
754  }
755  cblk->npasses = passno;
756  cblk->ninclpasses = passno;
757 
758  if (passno) {
759  cblk->passes[passno-1].rate = ff_mqc_flush_to(&t1->mqc, cblk->passes[passno-1].flushed, &cblk->passes[passno-1].flushed_len);
760  cblk->passes[passno-1].rate -= cblk->passes[passno-1].flushed_len;
761  }
762 }
763 
764 /* tier-2 routines: */
765 
767 {
768  if (n == 1)
769  put_num(s, 0, 1);
770  else if (n == 2)
771  put_num(s, 2, 2);
772  else if (n <= 5)
773  put_num(s, 0xc | (n-3), 4);
774  else if (n <= 36)
775  put_num(s, 0x1e0 | (n-6), 9);
776  else
777  put_num(s, 0xff80 | (n-37), 16);
778 }
779 
780 
781 static int encode_packet(Jpeg2000EncoderContext *s, Jpeg2000ResLevel *rlevel, int layno,
782  int precno, uint8_t *expn, int numgbits, int packetno,
783  int nlayers)
784 {
785  int bandno, empty = 1;
786  int i;
787  if (s->sop) {
788  bytestream_put_be16(&s->buf, JPEG2000_SOP);
789  bytestream_put_be16(&s->buf, 4);
790  bytestream_put_be16(&s->buf, packetno);
791  }
792 
793  // init bitstream
794  *s->buf = 0;
795  s->bit_index = 0;
796 
797  // header
798 
799  if (!layno) {
800  for (bandno = 0; bandno < rlevel->nbands; bandno++) {
801  Jpeg2000Band *band = rlevel->band + bandno;
802  if (band->coord[0][0] < band->coord[0][1]
803  && band->coord[1][0] < band->coord[1][1]) {
804  Jpeg2000Prec *prec = band->prec + precno;
805  int nb_cblks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
806  int pos;
809  for (pos = 0; pos < nb_cblks; pos++) {
810  Jpeg2000Cblk *cblk = &prec->cblk[pos];
811  prec->zerobits[pos].val = expn[bandno] + numgbits - 1 - cblk->nonzerobits;
812  cblk->incl = 0;
813  cblk->lblock = 3;
814  tag_tree_update(prec->zerobits + pos);
815  for (i = 0; i < nlayers; i++) {
816  if (cblk->layers[i].npasses > 0) {
817  prec->cblkincl[pos].val = i;
818  break;
819  }
820  }
821  if (i == nlayers)
822  prec->cblkincl[pos].val = i;
823  tag_tree_update(prec->cblkincl + pos);
824  }
825  }
826  }
827  }
828 
829  // is the packet empty?
830  for (bandno = 0; bandno < rlevel->nbands; bandno++){
831  Jpeg2000Band *band = rlevel->band + bandno;
832  if (band->coord[0][0] < band->coord[0][1]
833  && band->coord[1][0] < band->coord[1][1]) {
834  Jpeg2000Prec *prec = band->prec + precno;
835  int nb_cblks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
836  int pos;
837  for (pos = 0; pos < nb_cblks; pos++) {
838  Jpeg2000Cblk *cblk = &prec->cblk[pos];
839  if (cblk->layers[layno].npasses) {
840  empty = 0;
841  break;
842  }
843  }
844  if (!empty)
845  break;
846  }
847  }
848 
849  if (s->buf_end - s->buf < 3)
850  return -1;
851 
852  put_bits(s, !empty, 1);
853  if (empty){
854  j2k_flush(s);
855  if (s->eph)
856  bytestream_put_be16(&s->buf, JPEG2000_EPH);
857  return 0;
858  }
859 
860  for (bandno = 0; bandno < rlevel->nbands; bandno++) {
861  Jpeg2000Band *band = rlevel->band + bandno;
862  Jpeg2000Prec *prec = band->prec + precno;
863  int yi, xi, pos;
864  int cblknw = prec->nb_codeblocks_width;
865 
866  if (band->coord[0][0] == band->coord[0][1]
867  || band->coord[1][0] == band->coord[1][1])
868  continue;
869 
870  for (pos=0, yi = 0; yi < prec->nb_codeblocks_height; yi++) {
871  for (xi = 0; xi < cblknw; xi++, pos++){
872  int llen = 0, length;
873  Jpeg2000Cblk *cblk = prec->cblk + yi * cblknw + xi;
874 
875  if (s->buf_end - s->buf < 20) // approximately
876  return -1;
877 
878  // inclusion information
879  if (!cblk->incl)
880  tag_tree_code(s, prec->cblkincl + pos, layno + 1);
881  else {
882  put_bits(s, cblk->layers[layno].npasses > 0, 1);
883  }
884 
885  if (!cblk->layers[layno].npasses)
886  continue;
887 
888  // zerobits information
889  if (!cblk->incl) {
890  tag_tree_code(s, prec->zerobits + pos, 100);
891  cblk->incl = 1;
892  }
893 
894  // number of passes
895  putnumpasses(s, cblk->layers[layno].npasses);
896 
897  length = cblk->layers[layno].data_len;
898  if (layno == nlayers - 1 && cblk->layers[layno].cum_passes){
899  length += cblk->passes[cblk->layers[layno].cum_passes-1].flushed_len;
900  }
901  if (cblk->lblock + av_log2(cblk->layers[layno].npasses) < av_log2(length) + 1) {
902  llen = av_log2(length) + 1 - cblk->lblock - av_log2(cblk->layers[layno].npasses);
903  }
904 
905  // length of code block
906  cblk->lblock += llen;
907  put_bits(s, 1, llen);
908  put_bits(s, 0, 1);
909  put_num(s, length, cblk->lblock + av_log2(cblk->layers[layno].npasses));
910  }
911  }
912  }
913 
914  if (s->buf_end - s->buf < 2 + 2 * s->eph)
915  return -1;
916 
917  j2k_flush(s);
918  if (s->eph) {
919  bytestream_put_be16(&s->buf, JPEG2000_EPH);
920  }
921 
922  for (bandno = 0; bandno < rlevel->nbands; bandno++) {
923  Jpeg2000Band *band = rlevel->band + bandno;
924  Jpeg2000Prec *prec = band->prec + precno;
925  int yi, cblknw = prec->nb_codeblocks_width;
926  for (yi =0; yi < prec->nb_codeblocks_height; yi++) {
927  int xi;
928  for (xi = 0; xi < cblknw; xi++){
929  Jpeg2000Cblk *cblk = prec->cblk + yi * cblknw + xi;
930  if (cblk->layers[layno].npasses) {
931  if (s->buf_end - s->buf < cblk->layers[layno].data_len + 2)
932  return -1;
933  bytestream_put_buffer(&s->buf, cblk->layers[layno].data_start + 1, cblk->layers[layno].data_len);
934  if (layno == nlayers - 1 && cblk->layers[layno].cum_passes) {
935  bytestream_put_buffer(&s->buf, cblk->passes[cblk->layers[layno].cum_passes-1].flushed,
936  cblk->passes[cblk->layers[layno].cum_passes-1].flushed_len);
937  }
938  }
939  }
940  }
941  }
942  return 0;
943 }
944 
945 static int encode_packets(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno, int nlayers)
946 {
947  int compno, reslevelno, layno, ret;
948  Jpeg2000CodingStyle *codsty = &s->codsty;
949  Jpeg2000QuantStyle *qntsty = &s->qntsty;
950  int packetno = 0;
951  int step_x, step_y;
952  int x, y;
953  int tile_coord[2][2];
954  int col = tileno % s->numXtiles;
955  int row = tileno / s->numXtiles;
956 
957  tile_coord[0][0] = col * s->tile_width;
958  tile_coord[0][1] = FFMIN(tile_coord[0][0] + s->tile_width, s->width);
959  tile_coord[1][0] = row * s->tile_height;
960  tile_coord[1][1] = FFMIN(tile_coord[1][0] + s->tile_height, s->height);
961 
962  av_log(s->avctx, AV_LOG_DEBUG, "tier2\n");
963  // lay-rlevel-comp-pos progression
964  switch (s->prog) {
965  case JPEG2000_PGOD_LRCP:
966  for (layno = 0; layno < nlayers; layno++) {
967  for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
968  for (compno = 0; compno < s->ncomponents; compno++){
969  int precno;
970  Jpeg2000ResLevel *reslevel = s->tile[tileno].comp[compno].reslevel + reslevelno;
971  for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
972  if ((ret = encode_packet(s, reslevel, layno, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
973  qntsty->nguardbits, packetno++, nlayers)) < 0)
974  return ret;
975  }
976  }
977  }
978  }
979  break;
980  case JPEG2000_PGOD_RLCP:
981  for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
982  for (layno = 0; layno < nlayers; layno++) {
983  for (compno = 0; compno < s->ncomponents; compno++){
984  int precno;
985  Jpeg2000ResLevel *reslevel = s->tile[tileno].comp[compno].reslevel + reslevelno;
986  for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
987  if ((ret = encode_packet(s, reslevel, layno, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
988  qntsty->nguardbits, packetno++, nlayers)) < 0)
989  return ret;
990  }
991  }
992  }
993  }
994  break;
995  case JPEG2000_PGOD_RPCL:
996  for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
997  int precno;
998  step_x = 30;
999  step_y = 30;
1000  for (compno = 0; compno < s->ncomponents; compno++) {
1001  Jpeg2000Component *comp = tile->comp + compno;
1002  if (reslevelno < codsty->nreslevels) {
1003  uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1004  Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1005  step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
1006  step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1007  }
1008  }
1009 
1010  step_x = 1<<step_x;
1011  step_y = 1<<step_y;
1012  for (y = tile_coord[1][0]; y < tile_coord[1][1]; y = (y/step_y + 1)*step_y) {
1013  for (x = tile_coord[0][0]; x < tile_coord[0][1]; x = (x/step_x + 1)*step_x) {
1014  for (compno = 0; compno < s->ncomponents; compno++) {
1015  Jpeg2000Component *comp = tile->comp + compno;
1016  uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1017  Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
1018  int log_subsampling[2] = { compno?s->chroma_shift[0]:0, compno?s->chroma_shift[1]:0};
1019  unsigned prcx, prcy;
1020  int trx0, try0;
1021 
1022  trx0 = ff_jpeg2000_ceildivpow2(tile_coord[0][0], log_subsampling[0] + reducedresno);
1023  try0 = ff_jpeg2000_ceildivpow2(tile_coord[1][0], log_subsampling[1] + reducedresno);
1024 
1025  if (!(y % ((uint64_t)1 << (reslevel->log2_prec_height + reducedresno + log_subsampling[1])) == 0 ||
1026  (y == tile_coord[1][0] && (try0 << reducedresno) % (1U << (reducedresno + reslevel->log2_prec_height)))))
1027  continue;
1028 
1029  if (!(x % ((uint64_t)1 << (reslevel->log2_prec_width + reducedresno + log_subsampling[0])) == 0 ||
1030  (x == tile_coord[0][0] && (trx0 << reducedresno) % (1U << (reducedresno + reslevel->log2_prec_width)))))
1031  continue;
1032 
1033  // check if a precinct exists
1034  prcx = ff_jpeg2000_ceildivpow2(x, log_subsampling[0] + reducedresno) >> reslevel->log2_prec_width;
1035  prcy = ff_jpeg2000_ceildivpow2(y, log_subsampling[1] + reducedresno) >> reslevel->log2_prec_height;
1036  prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> reslevel->log2_prec_width;
1037  prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> reslevel->log2_prec_height;
1038  precno = prcx + reslevel->num_precincts_x * prcy;
1039 
1040  if (prcx >= reslevel->num_precincts_x || prcy >= reslevel->num_precincts_y) {
1041  av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1042  prcx, prcy, reslevel->num_precincts_x, reslevel->num_precincts_y);
1043  continue;
1044  }
1045  for (layno = 0; layno < nlayers; layno++) {
1046  if ((ret = encode_packet(s, reslevel, layno, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
1047  qntsty->nguardbits, packetno++, nlayers)) < 0)
1048  return ret;
1049  }
1050  }
1051  }
1052  }
1053  }
1054  break;
1055  case JPEG2000_PGOD_PCRL:
1056  step_x = 32;
1057  step_y = 32;
1058  for (compno = 0; compno < s->ncomponents; compno++) {
1059  Jpeg2000Component *comp = tile->comp + compno;
1060 
1061  for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
1062  uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1063  Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1064  step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
1065  step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1066  }
1067  }
1068  if (step_x >= 31 || step_y >= 31){
1069  avpriv_request_sample(s->avctx, "PCRL with large step");
1070  return AVERROR_PATCHWELCOME;
1071  }
1072  step_x = 1<<step_x;
1073  step_y = 1<<step_y;
1074 
1075  for (y = tile_coord[1][0]; y < tile_coord[1][1]; y = (y/step_y + 1)*step_y) {
1076  for (x = tile_coord[0][0]; x < tile_coord[0][1]; x = (x/step_x + 1)*step_x) {
1077  for (compno = 0; compno < s->ncomponents; compno++) {
1078  Jpeg2000Component *comp = tile->comp + compno;
1079  int log_subsampling[2] = { compno?s->chroma_shift[0]:0, compno?s->chroma_shift[1]:0};
1080 
1081  for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
1082  unsigned prcx, prcy;
1083  int precno;
1084  uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1085  Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
1086  int trx0, try0;
1087 
1088  trx0 = ff_jpeg2000_ceildivpow2(tile_coord[0][0], log_subsampling[0] + reducedresno);
1089  try0 = ff_jpeg2000_ceildivpow2(tile_coord[1][0], log_subsampling[1] + reducedresno);
1090 
1091  if (!(y % ((uint64_t)1 << (reslevel->log2_prec_height + reducedresno + log_subsampling[1])) == 0 ||
1092  (y == tile_coord[1][0] && (try0 << reducedresno) % (1U << (reducedresno + reslevel->log2_prec_height)))))
1093  continue;
1094 
1095  if (!(x % ((uint64_t)1 << (reslevel->log2_prec_width + reducedresno + log_subsampling[0])) == 0 ||
1096  (x == tile_coord[0][0] && (trx0 << reducedresno) % (1U << (reducedresno + reslevel->log2_prec_width)))))
1097  continue;
1098 
1099  // check if a precinct exists
1100  prcx = ff_jpeg2000_ceildivpow2(x, log_subsampling[0] + reducedresno) >> reslevel->log2_prec_width;
1101  prcy = ff_jpeg2000_ceildivpow2(y, log_subsampling[1] + reducedresno) >> reslevel->log2_prec_height;
1102  prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> reslevel->log2_prec_width;
1103  prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> reslevel->log2_prec_height;
1104 
1105  precno = prcx + reslevel->num_precincts_x * prcy;
1106 
1107  if (prcx >= reslevel->num_precincts_x || prcy >= reslevel->num_precincts_y) {
1108  av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1109  prcx, prcy, reslevel->num_precincts_x, reslevel->num_precincts_y);
1110  continue;
1111  }
1112  for (layno = 0; layno < nlayers; layno++) {
1113  if ((ret = encode_packet(s, reslevel, layno, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
1114  qntsty->nguardbits, packetno++, nlayers)) < 0)
1115  return ret;
1116  }
1117  }
1118  }
1119  }
1120  }
1121  break;
1122  case JPEG2000_PGOD_CPRL:
1123  for (compno = 0; compno < s->ncomponents; compno++) {
1124  Jpeg2000Component *comp = tile->comp + compno;
1125  int log_subsampling[2] = { compno?s->chroma_shift[0]:0, compno?s->chroma_shift[1]:0};
1126  step_x = 32;
1127  step_y = 32;
1128 
1129  for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
1130  uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1131  Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1132  step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
1133  step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1134  }
1135  if (step_x >= 31 || step_y >= 31){
1136  avpriv_request_sample(s->avctx, "CPRL with large step");
1137  return AVERROR_PATCHWELCOME;
1138  }
1139  step_x = 1<<step_x;
1140  step_y = 1<<step_y;
1141 
1142  for (y = tile_coord[1][0]; y < tile_coord[1][1]; y = (y/step_y + 1)*step_y) {
1143  for (x = tile_coord[0][0]; x < tile_coord[0][1]; x = (x/step_x + 1)*step_x) {
1144  for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
1145  unsigned prcx, prcy;
1146  int precno;
1147  int trx0, try0;
1148  uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1149  Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
1150 
1151  trx0 = ff_jpeg2000_ceildivpow2(tile_coord[0][0], log_subsampling[0] + reducedresno);
1152  try0 = ff_jpeg2000_ceildivpow2(tile_coord[1][0], log_subsampling[1] + reducedresno);
1153 
1154  if (!(y % ((uint64_t)1 << (reslevel->log2_prec_height + reducedresno + log_subsampling[1])) == 0 ||
1155  (y == tile_coord[1][0] && (try0 << reducedresno) % (1U << (reducedresno + reslevel->log2_prec_height)))))
1156  continue;
1157 
1158  if (!(x % ((uint64_t)1 << (reslevel->log2_prec_width + reducedresno + log_subsampling[0])) == 0 ||
1159  (x == tile_coord[0][0] && (trx0 << reducedresno) % (1U << (reducedresno + reslevel->log2_prec_width)))))
1160  continue;
1161 
1162  // check if a precinct exists
1163  prcx = ff_jpeg2000_ceildivpow2(x, log_subsampling[0] + reducedresno) >> reslevel->log2_prec_width;
1164  prcy = ff_jpeg2000_ceildivpow2(y, log_subsampling[1] + reducedresno) >> reslevel->log2_prec_height;
1165  prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> reslevel->log2_prec_width;
1166  prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> reslevel->log2_prec_height;
1167 
1168  precno = prcx + reslevel->num_precincts_x * prcy;
1169 
1170  if (prcx >= reslevel->num_precincts_x || prcy >= reslevel->num_precincts_y) {
1171  av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1172  prcx, prcy, reslevel->num_precincts_x, reslevel->num_precincts_y);
1173  continue;
1174  }
1175  for (layno = 0; layno < nlayers; layno++) {
1176  if ((ret = encode_packet(s, reslevel, layno, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
1177  qntsty->nguardbits, packetno++, nlayers)) < 0)
1178  return ret;
1179  }
1180  }
1181  }
1182  }
1183  }
1184 
1185  }
1186 
1187  av_log(s->avctx, AV_LOG_DEBUG, "after tier2\n");
1188  return 0;
1189 }
1190 
1191 static void makelayer(Jpeg2000EncoderContext *s, int layno, double thresh, Jpeg2000Tile* tile, int final)
1192 {
1193  int compno, resno, bandno, precno, cblkno;
1194  int passno;
1195 
1196  for (compno = 0; compno < s->ncomponents; compno++) {
1197  Jpeg2000Component *comp = &tile->comp[compno];
1198 
1199  for (resno = 0; resno < s->codsty.nreslevels; resno++) {
1200  Jpeg2000ResLevel *reslevel = comp->reslevel + resno;
1201 
1202  for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
1203  for (bandno = 0; bandno < reslevel->nbands ; bandno++){
1204  Jpeg2000Band *band = reslevel->band + bandno;
1205  Jpeg2000Prec *prec = band->prec + precno;
1206 
1207  for (cblkno = 0; cblkno < prec->nb_codeblocks_height * prec->nb_codeblocks_width; cblkno++){
1208  Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1209  Jpeg2000Layer *layer = &cblk->layers[layno];
1210  int n;
1211 
1212  if (layno == 0) {
1213  cblk->ninclpasses = 0;
1214  }
1215 
1216  n = cblk->ninclpasses;
1217 
1218  if (thresh < 0) {
1219  n = cblk->npasses;
1220  } else {
1221  for (passno = cblk->ninclpasses; passno < cblk->npasses; passno++) {
1222  int32_t dr;
1223  double dd;
1224  Jpeg2000Pass *pass = &cblk->passes[passno];
1225 
1226  if (n == 0) {
1227  dr = pass->rate;
1228  dd = pass->disto;
1229  } else {
1230  dr = pass->rate - cblk->passes[n - 1].rate;
1231  dd = pass->disto - cblk->passes[n-1].disto;
1232  }
1233 
1234  if (!dr) {
1235  if (dd != 0.0) {
1236  n = passno + 1;
1237  }
1238  continue;
1239  }
1240 
1241  if (thresh - (dd / dr) < DBL_EPSILON)
1242  n = passno + 1;
1243  }
1244  }
1245  layer->npasses = n - cblk->ninclpasses;
1246  layer->cum_passes = n;
1247 
1248  if (layer->npasses == 0) {
1249  layer->disto = 0;
1250  layer->data_len = 0;
1251  continue;
1252  }
1253 
1254  if (cblk->ninclpasses == 0) {
1255  layer->data_len = cblk->passes[n - 1].rate;
1256  layer->data_start = cblk->data;
1257  layer->disto = cblk->passes[n - 1].disto;
1258  } else {
1259  layer->data_len = cblk->passes[n - 1].rate - cblk->passes[cblk->ninclpasses - 1].rate;
1260  layer->data_start = cblk->data + cblk->passes[cblk->ninclpasses - 1].rate;
1261  layer->disto = cblk->passes[n - 1].disto -
1262  cblk->passes[cblk->ninclpasses - 1].disto;
1263  }
1264  if (final) {
1265  cblk->ninclpasses = n;
1266  }
1267  }
1268  }
1269  }
1270  }
1271  }
1272 }
1273 
1275 {
1276  int precno, compno, reslevelno, bandno, cblkno, lev, passno, layno;
1277  int i;
1278  double min = DBL_MAX;
1279  double max = 0;
1280  double thresh;
1281  int tile_disto = 0;
1282 
1283  Jpeg2000CodingStyle *codsty = &s->codsty;
1284 
1285  for (compno = 0; compno < s->ncomponents; compno++){
1286  Jpeg2000Component *comp = tile->comp + compno;
1287 
1288  for (reslevelno = 0, lev = codsty->nreslevels-1; reslevelno < codsty->nreslevels; reslevelno++, lev--){
1289  Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
1290 
1291  for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
1292  for (bandno = 0; bandno < reslevel->nbands ; bandno++){
1293  Jpeg2000Band *band = reslevel->band + bandno;
1294  Jpeg2000Prec *prec = band->prec + precno;
1295 
1296  for (cblkno = 0; cblkno < prec->nb_codeblocks_height * prec->nb_codeblocks_width; cblkno++){
1297  Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1298  for (passno = 0; passno < cblk->npasses; passno++) {
1299  Jpeg2000Pass *pass = &cblk->passes[passno];
1300  int dr;
1301  double dd, drslope;
1302 
1303  tile_disto += pass->disto;
1304  if (passno == 0) {
1305  dr = (int32_t)pass->rate;
1306  dd = pass->disto;
1307  } else {
1308  dr = (int32_t)(pass->rate - cblk->passes[passno - 1].rate);
1309  dd = pass->disto - cblk->passes[passno - 1].disto;
1310  }
1311 
1312  if (dr <= 0)
1313  continue;
1314 
1315  drslope = dd / dr;
1316  if (drslope < min)
1317  min = drslope;
1318 
1319  if (drslope > max)
1320  max = drslope;
1321  }
1322  }
1323  }
1324  }
1325  }
1326  }
1327 
1328  for (layno = 0; layno < s->nlayers; layno++) {
1329  double lo = min;
1330  double hi = max;
1331  double stable_thresh = 0.0;
1332  double good_thresh = 0.0;
1333  if (!s->layer_rates[layno]) {
1334  good_thresh = -1.0;
1335  } else {
1336  for (i = 0; i < 128; i++) {
1337  uint8_t *stream_pos = s->buf;
1338  int ret;
1339  thresh = (lo + hi) / 2;
1340  makelayer(s, layno, thresh, tile, 0);
1341  ret = encode_packets(s, tile, (int)(tile - s->tile), layno + 1);
1342  memset(stream_pos, 0, s->buf - stream_pos);
1343  if ((s->buf - stream_pos > ceil(tile->layer_rates[layno])) || ret < 0) {
1344  lo = thresh;
1345  s->buf = stream_pos;
1346  continue;
1347  }
1348  hi = thresh;
1349  stable_thresh = thresh;
1350  s->buf = stream_pos;
1351  }
1352  }
1353  if (good_thresh >= 0.0)
1354  good_thresh = stable_thresh == 0.0 ? thresh : stable_thresh;
1355  makelayer(s, layno, good_thresh, tile, 1);
1356  }
1357 }
1358 
1359 static int getcut(Jpeg2000Cblk *cblk, int64_t lambda, int dwt_norm)
1360 {
1361  int passno, res = 0;
1362  for (passno = 0; passno < cblk->npasses; passno++){
1363  int dr;
1364  int64_t dd;
1365 
1366  dr = cblk->passes[passno].rate
1367  - (res ? cblk->passes[res-1].rate : 0);
1368  dd = cblk->passes[passno].disto
1369  - (res ? cblk->passes[res-1].disto : 0);
1370 
1371  if (((dd * dwt_norm) >> WMSEDEC_SHIFT) * dwt_norm >= dr * lambda)
1372  res = passno+1;
1373  }
1374  return res;
1375 }
1376 
1378 {
1379  int precno, compno, reslevelno, bandno, cblkno, lev;
1380  Jpeg2000CodingStyle *codsty = &s->codsty;
1381 
1382  for (compno = 0; compno < s->ncomponents; compno++){
1383  Jpeg2000Component *comp = tile->comp + compno;
1384 
1385  for (reslevelno = 0, lev = codsty->nreslevels-1; reslevelno < codsty->nreslevels; reslevelno++, lev--){
1386  Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
1387 
1388  for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
1389  for (bandno = 0; bandno < reslevel->nbands ; bandno++){
1390  int bandpos = bandno + (reslevelno > 0);
1391  Jpeg2000Band *band = reslevel->band + bandno;
1392  Jpeg2000Prec *prec = band->prec + precno;
1393 
1394  for (cblkno = 0; cblkno < prec->nb_codeblocks_height * prec->nb_codeblocks_width; cblkno++){
1395  Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1396 
1397  cblk->ninclpasses = getcut(cblk, s->lambda,
1398  (int64_t)dwt_norms[codsty->transform == FF_DWT53][bandpos][lev] * (int64_t)band->i_stepsize >> 15);
1399  cblk->layers[0].data_start = cblk->data;
1400  cblk->layers[0].cum_passes = cblk->ninclpasses;
1401  cblk->layers[0].npasses = cblk->ninclpasses;
1402  if (cblk->ninclpasses)
1403  cblk->layers[0].data_len = cblk->passes[cblk->ninclpasses - 1].rate;
1404  }
1405  }
1406  }
1407  }
1408  }
1409 }
1410 
1411 static int encode_tile(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno)
1412 {
1413  int compno, reslevelno, bandno, ret;
1415  Jpeg2000CodingStyle *codsty = &s->codsty;
1416  for (compno = 0; compno < s->ncomponents; compno++){
1417  Jpeg2000Component *comp = s->tile[tileno].comp + compno;
1418 
1419  t1.stride = (1<<codsty->log2_cblk_width) + 2;
1420 
1421  av_log(s->avctx, AV_LOG_DEBUG,"dwt\n");
1422  if ((ret = ff_dwt_encode(&comp->dwt, comp->i_data)) < 0)
1423  return ret;
1424  av_log(s->avctx, AV_LOG_DEBUG,"after dwt -> tier1\n");
1425 
1426  for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
1427  Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
1428 
1429  for (bandno = 0; bandno < reslevel->nbands ; bandno++){
1430  Jpeg2000Band *band = reslevel->band + bandno;
1431  Jpeg2000Prec *prec = band->prec; // we support only 1 precinct per band ATM in the encoder
1432  int cblkx, cblky, cblkno=0, xx0, x0, xx1, y0, yy0, yy1, bandpos;
1433  yy0 = bandno == 0 ? 0 : comp->reslevel[reslevelno-1].coord[1][1] - comp->reslevel[reslevelno-1].coord[1][0];
1434  y0 = yy0;
1435  yy1 = FFMIN(ff_jpeg2000_ceildivpow2(band->coord[1][0] + 1, band->log2_cblk_height) << band->log2_cblk_height,
1436  band->coord[1][1]) - band->coord[1][0] + yy0;
1437 
1438  if (band->coord[0][0] == band->coord[0][1] || band->coord[1][0] == band->coord[1][1])
1439  continue;
1440 
1441  bandpos = bandno + (reslevelno > 0);
1442 
1443  for (cblky = 0; cblky < prec->nb_codeblocks_height; cblky++){
1444  if (reslevelno == 0 || bandno == 1)
1445  xx0 = 0;
1446  else
1447  xx0 = comp->reslevel[reslevelno-1].coord[0][1] - comp->reslevel[reslevelno-1].coord[0][0];
1448  x0 = xx0;
1449  xx1 = FFMIN(ff_jpeg2000_ceildivpow2(band->coord[0][0] + 1, band->log2_cblk_width) << band->log2_cblk_width,
1450  band->coord[0][1]) - band->coord[0][0] + xx0;
1451 
1452  for (cblkx = 0; cblkx < prec->nb_codeblocks_width; cblkx++, cblkno++){
1453  int y, x;
1454  if (codsty->transform == FF_DWT53){
1455  for (y = yy0; y < yy1; y++){
1456  int *ptr = t1.data + (y-yy0)*t1.stride;
1457  for (x = xx0; x < xx1; x++){
1458  *ptr++ = comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * y + x] * (1 << NMSEDEC_FRACBITS);
1459  }
1460  }
1461  } else{
1462  for (y = yy0; y < yy1; y++){
1463  int *ptr = t1.data + (y-yy0)*t1.stride;
1464  for (x = xx0; x < xx1; x++){
1465  *ptr = (comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * y + x]);
1466  *ptr = (int64_t)*ptr * (int64_t)(16384 * 65536 / band->i_stepsize) >> 15 - NMSEDEC_FRACBITS;
1467  ptr++;
1468  }
1469  }
1470  }
1471  if (!prec->cblk[cblkno].data)
1472  prec->cblk[cblkno].data = av_malloc(1 + 8192);
1473  if (!prec->cblk[cblkno].passes)
1474  prec->cblk[cblkno].passes = av_malloc_array(JPEG2000_MAX_PASSES, sizeof (*prec->cblk[cblkno].passes));
1475  if (!prec->cblk[cblkno].data || !prec->cblk[cblkno].passes)
1476  return AVERROR(ENOMEM);
1477  encode_cblk(s, &t1, prec->cblk + cblkno, tile, xx1 - xx0, yy1 - yy0,
1478  bandpos, codsty->nreslevels - reslevelno - 1);
1479  xx0 = xx1;
1480  xx1 = FFMIN(xx1 + (1 << band->log2_cblk_width), band->coord[0][1] - band->coord[0][0] + x0);
1481  }
1482  yy0 = yy1;
1483  yy1 = FFMIN(yy1 + (1 << band->log2_cblk_height), band->coord[1][1] - band->coord[1][0] + y0);
1484  }
1485  }
1486  }
1487  av_log(s->avctx, AV_LOG_DEBUG, "after tier1\n");
1488  }
1489 
1490  av_log(s->avctx, AV_LOG_DEBUG, "rate control\n");
1491  if (s->compression_rate_enc)
1492  makelayers(s, tile);
1493  else
1494  truncpasses(s, tile);
1495 
1496  if ((ret = encode_packets(s, tile, tileno, s->nlayers)) < 0)
1497  return ret;
1498  av_log(s->avctx, AV_LOG_DEBUG, "after rate control\n");
1499  return 0;
1500 }
1501 
1503 {
1504  int tileno, compno;
1505  Jpeg2000CodingStyle *codsty = &s->codsty;
1506 
1507  if (!s->tile)
1508  return;
1509  for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
1510  if (s->tile[tileno].comp) {
1511  for (compno = 0; compno < s->ncomponents; compno++){
1512  Jpeg2000Component *comp = s->tile[tileno].comp + compno;
1513  ff_jpeg2000_cleanup(comp, codsty);
1514  }
1515  av_freep(&s->tile[tileno].comp);
1516  }
1517  av_freep(&s->tile[tileno].layer_rates);
1518  }
1519  av_freep(&s->tile);
1520 }
1521 
1523 {
1524  int tileno, compno;
1525  for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
1526  Jpeg2000Tile *tile = s->tile + tileno;
1527  for (compno = 0; compno < s->ncomponents; compno++)
1528  ff_jpeg2000_reinit(tile->comp + compno, &s->codsty);
1529  }
1530 }
1531 
1532 static void update_size(uint8_t *size, const uint8_t *end)
1533 {
1534  AV_WB32(size, end-size);
1535 }
1536 
1538  const AVFrame *pict, int *got_packet)
1539 {
1540  int tileno, ret;
1542  uint8_t *chunkstart, *jp2cstart, *jp2hstart;
1544 
1545  if ((ret = ff_alloc_packet2(avctx, pkt, avctx->width*avctx->height*9 + AV_INPUT_BUFFER_MIN_SIZE, 0)) < 0)
1546  return ret;
1547 
1548  // init:
1549  s->buf = s->buf_start = pkt->data;
1550  s->buf_end = pkt->data + pkt->size;
1551 
1552  s->picture = pict;
1553 
1554  s->lambda = s->picture->quality * LAMBDA_SCALE;
1555 
1556  if (s->cbps[0] > 8)
1557  copy_frame_16(s);
1558  else
1559  copy_frame_8(s);
1560 
1561  reinit(s);
1562 
1563  if (s->format == CODEC_JP2) {
1564  av_assert0(s->buf == pkt->data);
1565 
1566  bytestream_put_be32(&s->buf, 0x0000000C);
1567  bytestream_put_be32(&s->buf, 0x6A502020);
1568  bytestream_put_be32(&s->buf, 0x0D0A870A);
1569 
1570  chunkstart = s->buf;
1571  bytestream_put_be32(&s->buf, 0);
1572  bytestream_put_buffer(&s->buf, "ftyp", 4);
1573  bytestream_put_buffer(&s->buf, "jp2\040\040", 4);
1574  bytestream_put_be32(&s->buf, 0);
1575  bytestream_put_buffer(&s->buf, "jp2\040", 4);
1576  update_size(chunkstart, s->buf);
1577 
1578  jp2hstart = s->buf;
1579  bytestream_put_be32(&s->buf, 0);
1580  bytestream_put_buffer(&s->buf, "jp2h", 4);
1581 
1582  chunkstart = s->buf;
1583  bytestream_put_be32(&s->buf, 0);
1584  bytestream_put_buffer(&s->buf, "ihdr", 4);
1585  bytestream_put_be32(&s->buf, avctx->height);
1586  bytestream_put_be32(&s->buf, avctx->width);
1587  bytestream_put_be16(&s->buf, s->ncomponents);
1588  bytestream_put_byte(&s->buf, s->cbps[0]);
1589  bytestream_put_byte(&s->buf, 7);
1590  bytestream_put_byte(&s->buf, 0);
1591  bytestream_put_byte(&s->buf, 0);
1592  update_size(chunkstart, s->buf);
1593 
1594  chunkstart = s->buf;
1595  bytestream_put_be32(&s->buf, 0);
1596  bytestream_put_buffer(&s->buf, "colr", 4);
1597  bytestream_put_byte(&s->buf, 1);
1598  bytestream_put_byte(&s->buf, 0);
1599  bytestream_put_byte(&s->buf, 0);
1600  if ((desc->flags & AV_PIX_FMT_FLAG_RGB) || avctx->pix_fmt == AV_PIX_FMT_PAL8) {
1601  bytestream_put_be32(&s->buf, 16);
1602  } else if (s->ncomponents == 1) {
1603  bytestream_put_be32(&s->buf, 17);
1604  } else {
1605  bytestream_put_be32(&s->buf, 18);
1606  }
1607  update_size(chunkstart, s->buf);
1608  if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
1609  int i;
1610  uint8_t *palette = pict->data[1];
1611  chunkstart = s->buf;
1612  bytestream_put_be32(&s->buf, 0);
1613  bytestream_put_buffer(&s->buf, "pclr", 4);
1614  bytestream_put_be16(&s->buf, AVPALETTE_COUNT);
1615  bytestream_put_byte(&s->buf, 3); // colour channels
1616  bytestream_put_be24(&s->buf, 0x070707); //colour depths
1617  for (i = 0; i < AVPALETTE_COUNT; i++) {
1618  bytestream_put_be24(&s->buf, HAVE_BIGENDIAN ? AV_RB24(palette + 1) : AV_RL24(palette));
1619  palette += 4;
1620  }
1621  update_size(chunkstart, s->buf);
1622  chunkstart = s->buf;
1623  bytestream_put_be32(&s->buf, 0);
1624  bytestream_put_buffer(&s->buf, "cmap", 4);
1625  for (i = 0; i < 3; i++) {
1626  bytestream_put_be16(&s->buf, 0); // component
1627  bytestream_put_byte(&s->buf, 1); // palette mapping
1628  bytestream_put_byte(&s->buf, i); // index
1629  }
1630  update_size(chunkstart, s->buf);
1631  }
1632  update_size(jp2hstart, s->buf);
1633 
1634  jp2cstart = s->buf;
1635  bytestream_put_be32(&s->buf, 0);
1636  bytestream_put_buffer(&s->buf, "jp2c", 4);
1637  }
1638 
1639  if (s->buf_end - s->buf < 2)
1640  return -1;
1641  bytestream_put_be16(&s->buf, JPEG2000_SOC);
1642  if ((ret = put_siz(s)) < 0)
1643  return ret;
1644  if ((ret = put_cod(s)) < 0)
1645  return ret;
1646  if ((ret = put_qcd(s, 0)) < 0)
1647  return ret;
1648  if ((ret = put_com(s, 0)) < 0)
1649  return ret;
1650 
1651  for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
1652  uint8_t *psotptr;
1653  if (!(psotptr = put_sot(s, tileno)))
1654  return -1;
1655  if (s->buf_end - s->buf < 2)
1656  return -1;
1657  bytestream_put_be16(&s->buf, JPEG2000_SOD);
1658  if ((ret = encode_tile(s, s->tile + tileno, tileno)) < 0)
1659  return ret;
1660  bytestream_put_be32(&psotptr, s->buf - psotptr + 6);
1661  }
1662  if (s->buf_end - s->buf < 2)
1663  return -1;
1664  bytestream_put_be16(&s->buf, JPEG2000_EOC);
1665 
1666  if (s->format == CODEC_JP2)
1667  update_size(jp2cstart, s->buf);
1668 
1669  av_log(s->avctx, AV_LOG_DEBUG, "end\n");
1670  pkt->size = s->buf - s->buf_start;
1672  *got_packet = 1;
1673 
1674  return 0;
1675 }
1676 
1678 {
1679  int i;
1680  char *token;
1681  char *saveptr = NULL;
1682  int rate;
1683  int nlayers = 0;
1684  if (!s->lr_str) {
1685  s->nlayers = 1;
1686  s->layer_rates[0] = 0;
1687  s->compression_rate_enc = 0;
1688  return 0;
1689  }
1690 
1691  token = av_strtok(s->lr_str, ",", &saveptr);
1692  if (token && (rate = strtol(token, NULL, 10))) {
1693  s->layer_rates[0] = rate <= 1 ? 0:rate;
1694  nlayers++;
1695  } else {
1696  return AVERROR_INVALIDDATA;
1697  }
1698 
1699  while (1) {
1700  token = av_strtok(NULL, ",", &saveptr);
1701  if (!token)
1702  break;
1703  if (rate = strtol(token, NULL, 10)) {
1704  if (nlayers >= 100) {
1705  return AVERROR_INVALIDDATA;
1706  }
1707  s->layer_rates[nlayers] = rate <= 1 ? 0:rate;
1708  nlayers++;
1709  } else {
1710  return AVERROR_INVALIDDATA;
1711  }
1712  }
1713 
1714  for (i = 1; i < nlayers; i++) {
1715  if (s->layer_rates[i] >= s->layer_rates[i-1]) {
1716  return AVERROR_INVALIDDATA;
1717  }
1718  }
1719  s->nlayers = nlayers;
1720  s->compression_rate_enc = 1;
1721  return 0;
1722 }
1723 
1725 {
1726  int i, ret;
1728  Jpeg2000CodingStyle *codsty = &s->codsty;
1729  Jpeg2000QuantStyle *qntsty = &s->qntsty;
1731 
1732  s->avctx = avctx;
1733  av_log(s->avctx, AV_LOG_DEBUG, "init\n");
1734  if (parse_layer_rates(s)) {
1735  av_log(s, AV_LOG_WARNING, "Layer rates invalid. Encoding with 1 layer based on quality metric.\n");
1736  s->nlayers = 1;
1737  s->layer_rates[0] = 0;
1738  s->compression_rate_enc = 0;
1739  }
1740 
1741 #if FF_API_PRIVATE_OPT
1743  if (avctx->prediction_method)
1744  s->pred = avctx->prediction_method;
1746 #endif
1747 
1748  if (avctx->pix_fmt == AV_PIX_FMT_PAL8 && (s->pred != FF_DWT97_INT || s->format != CODEC_JP2)) {
1749  av_log(s->avctx, AV_LOG_WARNING, "Forcing lossless jp2 for pal8\n");
1750  s->pred = 1;
1751  s->format = CODEC_JP2;
1752  }
1753 
1754  // defaults:
1755  // TODO: implement setting non-standard precinct size
1756  memset(codsty->log2_prec_widths , 15, sizeof(codsty->log2_prec_widths ));
1757  memset(codsty->log2_prec_heights, 15, sizeof(codsty->log2_prec_heights));
1758  codsty->nreslevels2decode=
1759  codsty->nreslevels = 7;
1760  codsty->nlayers = s->nlayers;
1761  codsty->log2_cblk_width = 4;
1762  codsty->log2_cblk_height = 4;
1763  codsty->transform = s->pred ? FF_DWT53 : FF_DWT97_INT;
1764 
1765  qntsty->nguardbits = 1;
1766 
1767  if ((s->tile_width & (s->tile_width -1)) ||
1768  (s->tile_height & (s->tile_height-1))) {
1769  av_log(avctx, AV_LOG_WARNING, "Tile dimension not a power of 2\n");
1770  }
1771 
1772  if (codsty->transform == FF_DWT53)
1773  qntsty->quantsty = JPEG2000_QSTY_NONE;
1774  else
1775  qntsty->quantsty = JPEG2000_QSTY_SE;
1776 
1777  s->width = avctx->width;
1778  s->height = avctx->height;
1779 
1780  s->ncomponents = desc->nb_components;
1781  for (i = 0; i < 3; i++) {
1782  s->cbps[i] = desc->comp[i].depth;
1783  }
1784 
1785  if ((desc->flags & AV_PIX_FMT_FLAG_PLANAR) && s->ncomponents > 1) {
1786  s->planar = 1;
1788  s->chroma_shift, s->chroma_shift + 1);
1789  if (ret)
1790  return ret;
1791  }
1792 
1795  init_luts();
1796 
1798  if ((ret=init_tiles(s)) < 0)
1799  return ret;
1800 
1801  av_log(s->avctx, AV_LOG_DEBUG, "after init\n");
1802 
1803  return 0;
1804 }
1805 
1806 static int j2kenc_destroy(AVCodecContext *avctx)
1807 {
1809 
1810  cleanup(s);
1811  return 0;
1812 }
1813 
1814 // taken from the libopenjpeg wraper so it matches
1815 
1816 #define OFFSET(x) offsetof(Jpeg2000EncoderContext, x)
1817 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1818 static const AVOption options[] = {
1819  { "format", "Codec Format", OFFSET(format), AV_OPT_TYPE_INT, { .i64 = CODEC_JP2 }, CODEC_J2K, CODEC_JP2, VE, "format" },
1820  { "j2k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CODEC_J2K }, 0, 0, VE, "format" },
1821  { "jp2", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CODEC_JP2 }, 0, 0, VE, "format" },
1822  { "tile_width", "Tile Width", OFFSET(tile_width), AV_OPT_TYPE_INT, { .i64 = 256 }, 1, 1<<30, VE, },
1823  { "tile_height", "Tile Height", OFFSET(tile_height), AV_OPT_TYPE_INT, { .i64 = 256 }, 1, 1<<30, VE, },
1824  { "pred", "DWT Type", OFFSET(pred), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE, "pred" },
1825  { "dwt97int", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, VE, "pred" },
1826  { "dwt53", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, VE, "pred" },
1827  { "sop", "SOP marker", OFFSET(sop), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE, },
1828  { "eph", "EPH marker", OFFSET(eph), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE, },
1829  { "prog", "Progression Order", OFFSET(prog), AV_OPT_TYPE_INT, { .i64 = 0 }, JPEG2000_PGOD_LRCP, JPEG2000_PGOD_CPRL, VE, "prog" },
1830  { "lrcp", NULL, OFFSET(prog), AV_OPT_TYPE_CONST, { .i64 = JPEG2000_PGOD_LRCP }, 0, 0, VE, "prog" },
1831  { "rlcp", NULL, OFFSET(prog), AV_OPT_TYPE_CONST, { .i64 = JPEG2000_PGOD_RLCP }, 0, 0, VE, "prog" },
1832  { "rpcl", NULL, OFFSET(prog), AV_OPT_TYPE_CONST, { .i64 = JPEG2000_PGOD_RPCL }, 0, 0, VE, "prog" },
1833  { "pcrl", NULL, OFFSET(prog), AV_OPT_TYPE_CONST, { .i64 = JPEG2000_PGOD_PCRL }, 0, 0, VE, "prog" },
1834  { "cprl", NULL, OFFSET(prog), AV_OPT_TYPE_CONST, { .i64 = JPEG2000_PGOD_CPRL }, 0, 0, VE, "prog" },
1835  { "layer_rates", "Layer Rates", OFFSET(lr_str), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, VE },
1836  { NULL }
1837 };
1838 
1839 static const AVClass j2k_class = {
1840  .class_name = "jpeg 2000 encoder",
1841  .item_name = av_default_item_name,
1842  .option = options,
1843  .version = LIBAVUTIL_VERSION_INT,
1844 };
1845 
1847  .name = "jpeg2000",
1848  .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
1849  .type = AVMEDIA_TYPE_VIDEO,
1850  .id = AV_CODEC_ID_JPEG2000,
1851  .priv_data_size = sizeof(Jpeg2000EncoderContext),
1852  .init = j2kenc_init,
1853  .encode2 = encode_frame,
1854  .close = j2kenc_destroy,
1855  .pix_fmts = (const enum AVPixelFormat[]) {
1862  },
1863  .priv_class = &j2k_class,
1864  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
1865 };
static double val(void *priv, double ch)
Definition: aeval.c:76
static const char *const format[]
Definition: af_aiir.c:457
#define av_cold
Definition: attributes.h:88
uint8_t
int32_t
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
Libavcodec external API header.
#define AV_RB24
Definition: intreadwrite.h:64
#define AV_RL24
Definition: intreadwrite.h:78
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
static av_always_inline void bytestream_put_buffer(uint8_t **b, const uint8_t *src, unsigned int size)
Definition: bytestream.h:372
#define xi(width, name, var, range_min, range_max, subs,...)
Definition: cbs_h2645.c:414
#define bit(string, value)
Definition: cbs_mpeg2.c:58
#define ss(width, name, subs,...)
Definition: cbs_vp9.c:261
#define s(width, name)
Definition: cbs_vp9.c:257
static LevelCodes lev[4+3+3]
Definition: clearvideo.c:85
common internal and external API header
#define FFMIN(a, b)
Definition: common.h:105
#define FFMAX(a, b)
Definition: common.h:103
#define HAVE_BIGENDIAN
Definition: config.h:202
#define NULL
Definition: coverity.c:32
long long int64_t
Definition: coverity.c:34
static __device__ float ceil(float a)
Definition: cuda_runtime.h:176
#define max(a, b)
Definition: cuda_runtime.h:33
static void comp(unsigned char *dst, ptrdiff_t dst_stride, unsigned char *src, ptrdiff_t src_stride, int add)
Definition: eamad.c:85
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:34
@ AV_OPT_TYPE_CONST
Definition: opt.h:234
@ AV_OPT_TYPE_INT
Definition: opt.h:225
@ AV_OPT_TYPE_STRING
Definition: opt.h:229
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition: avcodec.h:333
@ AV_CODEC_ID_JPEG2000
Definition: codec_id.h:137
#define AV_INPUT_BUFFER_MIN_SIZE
minimum encoding buffer size Used to avoid some checks during header writing.
Definition: avcodec.h:222
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:410
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:215
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
Definition: mem.c:245
void * av_mallocz_array(size_t nmemb, size_t size)
Allocate a memory block for an array with av_mallocz().
Definition: mem.c:190
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
char * av_strtok(char *s, const char *delim, char **saveptr)
Split the string into several tokens which can be accessed by successive calls to av_strtok().
Definition: avstring.c:186
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
for(j=16;j >0;--j)
int i
Definition: input.c:407
#define av_log2
Definition: intmath.h:83
#define AV_WB32(p, v)
Definition: intreadwrite.h:419
static int init_tiles(Jpeg2000EncoderContext *s)
compute the sizes of tiles, resolution levels, bands, etc.
Definition: j2kenc.c:455
static int put_com(Jpeg2000EncoderContext *s, int compno)
Definition: j2kenc.c:387
static int parse_layer_rates(Jpeg2000EncoderContext *s)
Definition: j2kenc.c:1677
static uint8_t * put_sot(Jpeg2000EncoderContext *s, int tileno)
Definition: j2kenc.c:406
#define WMSEDEC_SHIFT
must be >= 13
Definition: j2kenc.c:80
static void update_size(uint8_t *size, const uint8_t *end)
Definition: j2kenc.c:1532
static void makelayers(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile)
Definition: j2kenc.c:1274
AVCodec ff_jpeg2000_encoder
Definition: j2kenc.c:1846
static void makelayer(Jpeg2000EncoderContext *s, int layno, double thresh, Jpeg2000Tile *tile, int final)
Definition: j2kenc.c:1191
#define NMSEDEC_FRACBITS
Definition: j2kenc.c:79
static void truncpasses(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile)
Definition: j2kenc.c:1377
static void init_luts(void)
Definition: j2kenc.c:581
static const AVOption options[]
Definition: j2kenc.c:1818
static void putnumpasses(Jpeg2000EncoderContext *s, int n)
Definition: j2kenc.c:766
static int encode_tile(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno)
Definition: j2kenc.c:1411
#define VE
Definition: j2kenc.c:1817
static int encode_packet(Jpeg2000EncoderContext *s, Jpeg2000ResLevel *rlevel, int layno, int precno, uint8_t *expn, int numgbits, int packetno, int nlayers)
Definition: j2kenc.c:781
static void encode_refpass(Jpeg2000T1Context *t1, int width, int height, int *nmsedec, int bpno)
Definition: j2kenc.c:635
static void reinit(Jpeg2000EncoderContext *s)
Definition: j2kenc.c:1522
static int encode_packets(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno, int nlayers)
Definition: j2kenc.c:945
static av_cold int j2kenc_init(AVCodecContext *avctx)
Definition: j2kenc.c:1724
static int j2kenc_destroy(AVCodecContext *avctx)
Definition: j2kenc.c:1806
static void encode_sigpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
Definition: j2kenc.c:613
static void init_quantization(Jpeg2000EncoderContext *s)
Definition: j2kenc.c:551
static int lut_nmsedec_sig0[1<< NMSEDEC_BITS]
Definition: j2kenc.c:89
static void compute_rates(Jpeg2000EncoderContext *s)
Definition: j2kenc.c:425
static void tag_tree_update(Jpeg2000TgtNode *node)
update the value in node
Definition: j2kenc.c:288
static void cleanup(Jpeg2000EncoderContext *s)
Definition: j2kenc.c:1502
static void put_num(Jpeg2000EncoderContext *s, int num, int n)
put n least significant bits of a number num
Definition: j2kenc.c:231
static void j2k_flush(Jpeg2000EncoderContext *s)
flush the bitstream
Definition: j2kenc.c:238
#define LAMBDA_SCALE
Definition: j2kenc.c:81
static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
Definition: j2kenc.c:1537
static int lut_nmsedec_sig[1<< NMSEDEC_BITS]
Definition: j2kenc.c:88
#define CODEC_JP2
Definition: j2kenc.c:83
static int lut_nmsedec_ref0[1<< NMSEDEC_BITS]
Definition: j2kenc.c:87
#define NMSEDEC_BITS
Definition: j2kenc.c:78
static void encode_cblk(Jpeg2000EncoderContext *s, Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk, Jpeg2000Tile *tile, int width, int height, int bandpos, int lev)
Definition: j2kenc.c:704
static int lut_nmsedec_ref[1<< NMSEDEC_BITS]
Definition: j2kenc.c:86
static int put_qcd(Jpeg2000EncoderContext *s, int compno)
Definition: j2kenc.c:361
static int getnmsedec_sig(int x, int bpno)
Definition: j2kenc.c:599
static int getnmsedec_ref(int x, int bpno)
Definition: j2kenc.c:606
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:218
static void tag_tree_code(Jpeg2000EncoderContext *s, Jpeg2000TgtNode *node, int threshold)
code the value stored in node
Definition: j2kenc.c:251
static int getcut(Jpeg2000Cblk *cblk, int64_t lambda, int dwt_norm)
Definition: j2kenc.c:1359
static const int dwt_norms[2][4][10]
Definition: j2kenc.c:91
static const AVClass j2k_class
Definition: j2kenc.c:1839
#define OFFSET(x)
Definition: j2kenc.c:1816
static void encode_clnpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
Definition: j2kenc.c:649
static int put_siz(Jpeg2000EncoderContext *s)
Definition: j2kenc.c:300
#define CODEC_J2K
Definition: j2kenc.c:84
#define COPY_FRAME(D, PIXEL)
Definition: j2kenc.c:507
static int put_cod(Jpeg2000EncoderContext *s)
Definition: j2kenc.c:329
int ff_jpeg2000_init_component(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty, Jpeg2000QuantStyle *qntsty, int cbps, int dx, int dy, AVCodecContext *avctx)
Definition: jpeg2000.c:459
void ff_tag_tree_zero(Jpeg2000TgtNode *t, int w, int h, int val)
Definition: jpeg2000.c:85
void av_cold ff_jpeg2000_init_tier1_luts(void)
Definition: jpeg2000.c:160
void ff_jpeg2000_reinit(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
Definition: jpeg2000.c:569
void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
Definition: jpeg2000.c:590
void ff_jpeg2000_set_significance(Jpeg2000T1Context *t1, int x, int y, int negative)
Definition: jpeg2000.c:172
JPEG 2000 structures and defines common to encoder and decoder.
#define JPEG2000_T1_SIG_NB
Definition: jpeg2000.h:85
#define JPEG2000_CSTY_SOP
Definition: jpeg2000.h:111
static int ff_jpeg2000_ceildivpow2(int a, int b)
Definition: jpeg2000.h:229
#define JPEG2000_PGOD_PCRL
Definition: jpeg2000.h:118
#define JPEG2000_T1_REF
Definition: jpeg2000.h:97
@ JPEG2000_QSTY_NONE
Definition: jpeg2000.h:65
@ JPEG2000_QSTY_SE
Definition: jpeg2000.h:67
#define JPEG2000_T1_VIS
Definition: jpeg2000.h:95
static int ff_jpeg2000_ceildiv(int a, int64_t b)
Definition: jpeg2000.h:234
#define JPEG2000_CSTY_EPH
Definition: jpeg2000.h:112
#define JPEG2000_PGOD_CPRL
Definition: jpeg2000.h:119
#define JPEG2000_PGOD_RPCL
Definition: jpeg2000.h:117
#define JPEG2000_MAX_PASSES
Definition: jpeg2000.h:73
#define JPEG2000_PGOD_RLCP
Definition: jpeg2000.h:116
static int ff_jpeg2000_getsgnctxno(int flag, int *xorbit)
Definition: jpeg2000.h:271
static int ff_jpeg2000_getrefctxno(int flag)
Definition: jpeg2000.h:262
@ JPEG2000_SOT
Definition: jpeg2000.h:54
@ JPEG2000_COM
Definition: jpeg2000.h:53
@ JPEG2000_SOP
Definition: jpeg2000.h:55
@ JPEG2000_QCD
Definition: jpeg2000.h:46
@ JPEG2000_SOD
Definition: jpeg2000.h:57
@ JPEG2000_EPH
Definition: jpeg2000.h:56
@ JPEG2000_COD
Definition: jpeg2000.h:41
@ JPEG2000_SIZ
Definition: jpeg2000.h:40
@ JPEG2000_EOC
Definition: jpeg2000.h:58
@ JPEG2000_SOC
Definition: jpeg2000.h:39
#define JPEG2000_T1_SGN
Definition: jpeg2000.h:99
static int ff_jpeg2000_getsigctxno(int flag, int bandno)
Definition: jpeg2000.h:253
#define JPEG2000_T1_SIG
Definition: jpeg2000.h:96
#define JPEG2000_PGOD_LRCP
Definition: jpeg2000.h:115
int ff_dwt_encode(DWTContext *s, void *t)
Definition: jpeg2000dwt.c:581
@ FF_DWT97_INT
Definition: jpeg2000dwt.h:39
@ FF_DWT53
Definition: jpeg2000dwt.h:38
#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:49
#define LIBAVCODEC_IDENT
Definition: version.h:42
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:83
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:84
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:309
const char * desc
Definition: libsvtav1.c:83
static const uint16_t mask[17]
Definition: lzw.c:38
void av_cold ff_mqc_init_context_tables(void)
MQ-coder Initialize context tables (QE, NLPS, NMPS)
Definition: mqc.c:97
void ff_mqc_encode(MqcState *mqc, uint8_t *cxstate, int d)
code bit d with context cx
Definition: mqcenc.c:79
#define MQC_CX_UNI
Definition: mqc.h:33
int ff_mqc_flush_to(MqcState *mqc, uint8_t *dst, int *dst_len)
Definition: mqcenc.c:117
#define MQC_CX_RL
Definition: mqc.h:34
void ff_mqc_initenc(MqcState *mqc, uint8_t *bp)
initialize the encoder
Definition: mqcenc.c:69
AVOptions.
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:2601
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2573
#define AV_PIX_FMT_FLAG_RGB
The pixel format contains RGB-like data (as opposed to YUV/grayscale).
Definition: pixdesc.h:148
#define AV_PIX_FMT_FLAG_PLANAR
At least one pixel component is not in the first data plane.
Definition: pixdesc.h:144
#define AV_PIX_FMT_RGB48
Definition: pixfmt.h:385
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:68
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:72
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:73
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:77
#define AVPALETTE_COUNT
Definition: pixfmt.h:33
#define AV_PIX_FMT_GRAY16
Definition: pixfmt.h:383
#define t1
Definition: regdef.h:29
#define sp
Definition: regdef.h:63
static const float pred[4]
Definition: siprdata.h:259
unsigned int pos
Definition: spdifenc.c:412
Describe the class of an AVClass context structure.
Definition: log.h:67
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
main external API structure.
Definition: avcodec.h:536
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:746
int width
picture width / height.
Definition: avcodec.h:709
attribute_deprecated int prediction_method
Definition: avcodec.h:895
void * priv_data
Definition: avcodec.h:563
AVCodec.
Definition: codec.h:197
const char * name
Name of the codec implementation.
Definition: codec.h:204
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:332
AVOption.
Definition: opt.h:248
This structure stores compressed data.
Definition: packet.h:346
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:375
int size
Definition: packet.h:370
uint8_t * data
Definition: packet.h:369
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
uint16_t log2_cblk_height
Definition: jpeg2000.h:204
Jpeg2000Prec * prec
Definition: jpeg2000.h:207
uint16_t log2_cblk_width
Definition: jpeg2000.h:204
int coord[2][2]
Definition: jpeg2000.h:203
int i_stepsize
Definition: jpeg2000.h:205
uint8_t * data
Definition: jpeg2000.h:182
uint8_t nonzerobits
Definition: jpeg2000.h:176
uint8_t ninclpasses
Definition: jpeg2000.h:175
Jpeg2000Pass * passes
Definition: jpeg2000.h:187
uint8_t npasses
Definition: jpeg2000.h:174
Jpeg2000Layer * layers
Definition: jpeg2000.h:188
uint8_t lblock
Definition: jpeg2000.h:181
uint8_t incl
Definition: jpeg2000.h:177
uint8_t log2_cblk_width
Definition: jpeg2000.h:138
uint8_t log2_prec_widths[JPEG2000_MAX_RESLEVELS]
Definition: jpeg2000.h:146
uint8_t log2_cblk_height
Definition: jpeg2000.h:139
uint8_t log2_prec_heights[JPEG2000_MAX_RESLEVELS]
Definition: jpeg2000.h:147
int coord[2][2]
Definition: jpeg2000.h:223
AVCodecContext * avctx
Definition: j2kenc.c:110
Jpeg2000Tile * tile
Definition: j2kenc.c:131
int tile_height
tile size
Definition: j2kenc.c:118
uint8_t * buf_end
Definition: j2kenc.c:123
Jpeg2000CodingStyle codsty
Definition: j2kenc.c:128
uint8_t * buf_start
Definition: j2kenc.c:121
Jpeg2000QuantStyle qntsty
Definition: j2kenc.c:129
const AVFrame * picture
Definition: j2kenc.c:111
int height
image width and height
Definition: j2kenc.c:113
uint8_t compression_rate_enc
Is compression done using compression ratio?
Definition: j2kenc.c:133
double disto
Definition: jpeg2000.h:169
uint8_t * data_start
Definition: jpeg2000.h:166
int cum_passes
Definition: jpeg2000.h:170
uint8_t flushed[4]
Definition: jpeg2000.h:161
int flushed_len
Definition: jpeg2000.h:162
uint16_t rate
Definition: jpeg2000.h:159
int64_t disto
Definition: jpeg2000.h:160
Jpeg2000Cblk * cblk
Definition: jpeg2000.h:197
Jpeg2000TgtNode * zerobits
Definition: jpeg2000.h:195
Jpeg2000TgtNode * cblkincl
Definition: jpeg2000.h:196
int nb_codeblocks_height
Definition: jpeg2000.h:194
int nb_codeblocks_width
Definition: jpeg2000.h:193
uint16_t mant[JPEG2000_MAX_DECLEVELS *3]
Definition: jpeg2000.h:153
uint8_t expn[JPEG2000_MAX_DECLEVELS *3]
Definition: jpeg2000.h:152
uint8_t nguardbits
Definition: jpeg2000.h:155
uint8_t quantsty
Definition: jpeg2000.h:154
uint8_t log2_prec_width
Definition: jpeg2000.h:214
uint8_t nbands
Definition: jpeg2000.h:211
Jpeg2000Band * band
Definition: jpeg2000.h:215
uint8_t log2_prec_height
Definition: jpeg2000.h:214
uint8_t temp_val
Definition: jpeg2000.h:130
uint8_t vis
Definition: jpeg2000.h:131
struct Jpeg2000TgtNode * parent
Definition: jpeg2000.h:132
uint8_t val
Definition: jpeg2000.h:129
double * layer_rates
Definition: j2kenc.c:105
Jpeg2000Component * comp
Definition: j2kenc.c:104
#define av_malloc_array(a, b)
#define avpriv_request_sample(...)
#define av_freep(p)
#define av_malloc(s)
#define av_log(a,...)
AVPacket * pkt
Definition: movenc.c:59
#define height
#define width
int size
#define pass
Definition: tx_template.c:347
float min