FFmpeg  4.4.6
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  s->buf++;
243  }
244 }
245 
246 /* tag tree routines */
247 
248 /** code the value stored in node */
249 static void tag_tree_code(Jpeg2000EncoderContext *s, Jpeg2000TgtNode *node, int threshold)
250 {
251  Jpeg2000TgtNode *stack[30];
252  int sp = -1, curval = 0;
253 
254  while(node->parent){
255  stack[++sp] = node;
256  node = node->parent;
257  }
258 
259  while (1) {
260  if (curval > node->temp_val)
261  node->temp_val = curval;
262  else {
263  curval = node->temp_val;
264  }
265 
266  if (node->val >= threshold) {
267  put_bits(s, 0, threshold - curval);
268  curval = threshold;
269  } else {
270  put_bits(s, 0, node->val - curval);
271  curval = node->val;
272  if (!node->vis) {
273  put_bits(s, 1, 1);
274  node->vis = 1;
275  }
276  }
277 
278  node->temp_val = curval;
279  if (sp < 0)
280  break;
281  node = stack[sp--];
282  }
283 }
284 
285 /** update the value in node */
287 {
288  int lev = 0;
289  while (node->parent){
290  if (node->parent->val <= node->val)
291  break;
292  node->parent->val = node->val;
293  node = node->parent;
294  lev++;
295  }
296 }
297 
299 {
300  int i;
301 
302  if (s->buf_end - s->buf < 40 + 3 * s->ncomponents)
303  return -1;
304 
305  bytestream_put_be16(&s->buf, JPEG2000_SIZ);
306  bytestream_put_be16(&s->buf, 38 + 3 * s->ncomponents); // Lsiz
307  bytestream_put_be16(&s->buf, 0); // Rsiz
308  bytestream_put_be32(&s->buf, s->width); // width
309  bytestream_put_be32(&s->buf, s->height); // height
310  bytestream_put_be32(&s->buf, 0); // X0Siz
311  bytestream_put_be32(&s->buf, 0); // Y0Siz
312 
313  bytestream_put_be32(&s->buf, s->tile_width); // XTSiz
314  bytestream_put_be32(&s->buf, s->tile_height); // YTSiz
315  bytestream_put_be32(&s->buf, 0); // XT0Siz
316  bytestream_put_be32(&s->buf, 0); // YT0Siz
317  bytestream_put_be16(&s->buf, s->ncomponents); // CSiz
318 
319  for (i = 0; i < s->ncomponents; i++){ // Ssiz_i XRsiz_i, YRsiz_i
320  bytestream_put_byte(&s->buf, s->cbps[i] - 1);
321  bytestream_put_byte(&s->buf, i?1<<s->chroma_shift[0]:1);
322  bytestream_put_byte(&s->buf, i?1<<s->chroma_shift[1]:1);
323  }
324  return 0;
325 }
326 
328 {
329  Jpeg2000CodingStyle *codsty = &s->codsty;
330  uint8_t scod = 0;
331 
332  if (s->buf_end - s->buf < 14)
333  return -1;
334 
335  bytestream_put_be16(&s->buf, JPEG2000_COD);
336  bytestream_put_be16(&s->buf, 12); // Lcod
337  if (s->sop)
338  scod |= JPEG2000_CSTY_SOP;
339  if (s->eph)
340  scod |= JPEG2000_CSTY_EPH;
341  bytestream_put_byte(&s->buf, scod); // Scod
342  // SGcod
343  bytestream_put_byte(&s->buf, s->prog); // progression level
344  bytestream_put_be16(&s->buf, s->nlayers); // num of layers
345  if(s->avctx->pix_fmt == AV_PIX_FMT_YUV444P){
346  bytestream_put_byte(&s->buf, 0); // unspecified
347  }else{
348  bytestream_put_byte(&s->buf, 0); // unspecified
349  }
350  // SPcod
351  bytestream_put_byte(&s->buf, codsty->nreslevels - 1); // num of decomp. levels
352  bytestream_put_byte(&s->buf, codsty->log2_cblk_width-2); // cblk width
353  bytestream_put_byte(&s->buf, codsty->log2_cblk_height-2); // cblk height
354  bytestream_put_byte(&s->buf, 0); // cblk style
355  bytestream_put_byte(&s->buf, codsty->transform == FF_DWT53); // transformation
356  return 0;
357 }
358 
359 static int put_qcd(Jpeg2000EncoderContext *s, int compno)
360 {
361  int i, size;
362  Jpeg2000CodingStyle *codsty = &s->codsty;
363  Jpeg2000QuantStyle *qntsty = &s->qntsty;
364 
365  if (qntsty->quantsty == JPEG2000_QSTY_NONE)
366  size = 4 + 3 * (codsty->nreslevels-1);
367  else // QSTY_SE
368  size = 5 + 6 * (codsty->nreslevels-1);
369 
370  if (s->buf_end - s->buf < size + 2)
371  return -1;
372 
373  bytestream_put_be16(&s->buf, JPEG2000_QCD);
374  bytestream_put_be16(&s->buf, size); // LQcd
375  bytestream_put_byte(&s->buf, (qntsty->nguardbits << 5) | qntsty->quantsty); // Sqcd
376  if (qntsty->quantsty == JPEG2000_QSTY_NONE)
377  for (i = 0; i < codsty->nreslevels * 3 - 2; i++)
378  bytestream_put_byte(&s->buf, qntsty->expn[i] << 3);
379  else // QSTY_SE
380  for (i = 0; i < codsty->nreslevels * 3 - 2; i++)
381  bytestream_put_be16(&s->buf, (qntsty->expn[i] << 11) | qntsty->mant[i]);
382  return 0;
383 }
384 
385 static int put_com(Jpeg2000EncoderContext *s, int compno)
386 {
387  int size = 4 + strlen(LIBAVCODEC_IDENT);
388 
389  if (s->avctx->flags & AV_CODEC_FLAG_BITEXACT)
390  return 0;
391 
392  if (s->buf_end - s->buf < size + 2)
393  return -1;
394 
395  bytestream_put_be16(&s->buf, JPEG2000_COM);
396  bytestream_put_be16(&s->buf, size);
397  bytestream_put_be16(&s->buf, 1); // General use (ISO/IEC 8859-15 (Latin) values)
398 
400 
401  return 0;
402 }
403 
405 {
406  uint8_t *psotptr;
407 
408  if (s->buf_end - s->buf < 12)
409  return NULL;
410 
411  bytestream_put_be16(&s->buf, JPEG2000_SOT);
412  bytestream_put_be16(&s->buf, 10); // Lsot
413  bytestream_put_be16(&s->buf, tileno); // Isot
414 
415  psotptr = s->buf;
416  bytestream_put_be32(&s->buf, 0); // Psot (filled in later)
417 
418  bytestream_put_byte(&s->buf, 0); // TPsot
419  bytestream_put_byte(&s->buf, 1); // TNsot
420  return psotptr;
421 }
422 
424 {
425  int i, j;
426  int layno, compno;
427  for (i = 0; i < s->numYtiles; i++) {
428  for (j = 0; j < s->numXtiles; j++) {
429  Jpeg2000Tile *tile = &s->tile[s->numXtiles * i + j];
430  for (compno = 0; compno < s->ncomponents; compno++) {
431  int tilew = tile->comp[compno].coord[0][1] - tile->comp[compno].coord[0][0];
432  int tileh = tile->comp[compno].coord[1][1] - tile->comp[compno].coord[1][0];
433  int scale = (compno?1 << s->chroma_shift[0]:1) * (compno?1 << s->chroma_shift[1]:1);
434  for (layno = 0; layno < s->nlayers; layno++) {
435  if (s->layer_rates[layno] > 0) {
436  tile->layer_rates[layno] += (double)(tilew * tileh) * s->ncomponents * s->cbps[compno] /
437  (double)(s->layer_rates[layno] * 8 * scale);
438  } else {
439  tile->layer_rates[layno] = 0.0;
440  }
441  }
442  }
443  }
444  }
445 
446 }
447 
448 /**
449  * compute the sizes of tiles, resolution levels, bands, etc.
450  * allocate memory for them
451  * divide the input image into tile-components
452  */
454 {
455  int tileno, tilex, tiley, compno;
456  Jpeg2000CodingStyle *codsty = &s->codsty;
457  Jpeg2000QuantStyle *qntsty = &s->qntsty;
458 
459  s->numXtiles = ff_jpeg2000_ceildiv(s->width, s->tile_width);
460  s->numYtiles = ff_jpeg2000_ceildiv(s->height, s->tile_height);
461 
462  s->tile = av_calloc(s->numXtiles, s->numYtiles * sizeof(Jpeg2000Tile));
463  if (!s->tile)
464  return AVERROR(ENOMEM);
465  for (tileno = 0, tiley = 0; tiley < s->numYtiles; tiley++)
466  for (tilex = 0; tilex < s->numXtiles; tilex++, tileno++){
467  Jpeg2000Tile *tile = s->tile + tileno;
468 
469  tile->comp = av_mallocz_array(s->ncomponents, sizeof(Jpeg2000Component));
470  if (!tile->comp)
471  return AVERROR(ENOMEM);
472 
473  tile->layer_rates = av_mallocz_array(s->nlayers, sizeof(*tile->layer_rates));
474  if (!tile->layer_rates)
475  return AVERROR(ENOMEM);
476 
477  for (compno = 0; compno < s->ncomponents; compno++){
478  Jpeg2000Component *comp = tile->comp + compno;
479  int ret, i, j;
480 
481  comp->coord[0][0] = comp->coord_o[0][0] = tilex * s->tile_width;
482  comp->coord[0][1] = comp->coord_o[0][1] = FFMIN((tilex+1)*s->tile_width, s->width);
483  comp->coord[1][0] = comp->coord_o[1][0] = tiley * s->tile_height;
484  comp->coord[1][1] = comp->coord_o[1][1] = FFMIN((tiley+1)*s->tile_height, s->height);
485  if (compno > 0)
486  for (i = 0; i < 2; i++)
487  for (j = 0; j < 2; j++)
488  comp->coord[i][j] = comp->coord_o[i][j] = ff_jpeg2000_ceildivpow2(comp->coord[i][j], s->chroma_shift[i]);
489 
490  if ((ret = ff_jpeg2000_init_component(comp,
491  codsty,
492  qntsty,
493  s->cbps[compno],
494  compno?1<<s->chroma_shift[0]:1,
495  compno?1<<s->chroma_shift[1]:1,
496  s->avctx
497  )) < 0)
498  return ret;
499  }
500  }
501  compute_rates(s);
502  return 0;
503 }
504 
505 #define COPY_FRAME(D, PIXEL) \
506  static void copy_frame_ ##D(Jpeg2000EncoderContext *s) \
507  { \
508  int tileno, compno, i, y, x; \
509  PIXEL *line; \
510  for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){ \
511  Jpeg2000Tile *tile = s->tile + tileno; \
512  if (s->planar){ \
513  for (compno = 0; compno < s->ncomponents; compno++){ \
514  Jpeg2000Component *comp = tile->comp + compno; \
515  int *dst = comp->i_data; \
516  int cbps = s->cbps[compno]; \
517  line = (PIXEL*)s->picture->data[compno] \
518  + comp->coord[1][0] * (s->picture->linesize[compno] / sizeof(PIXEL)) \
519  + comp->coord[0][0]; \
520  for (y = comp->coord[1][0]; y < comp->coord[1][1]; y++){ \
521  PIXEL *ptr = line; \
522  for (x = comp->coord[0][0]; x < comp->coord[0][1]; x++) \
523  *dst++ = *ptr++ - (1 << (cbps - 1)); \
524  line += s->picture->linesize[compno] / sizeof(PIXEL); \
525  } \
526  } \
527  } else{ \
528  line = (PIXEL*)s->picture->data[0] + tile->comp[0].coord[1][0] * (s->picture->linesize[0] / sizeof(PIXEL)) \
529  + tile->comp[0].coord[0][0] * s->ncomponents; \
530  \
531  i = 0; \
532  for (y = tile->comp[0].coord[1][0]; y < tile->comp[0].coord[1][1]; y++){ \
533  PIXEL *ptr = line; \
534  for (x = tile->comp[0].coord[0][0]; x < tile->comp[0].coord[0][1]; x++, i++){ \
535  for (compno = 0; compno < s->ncomponents; compno++){ \
536  int cbps = s->cbps[compno]; \
537  tile->comp[compno].i_data[i] = *ptr++ - (1 << (cbps - 1)); \
538  } \
539  } \
540  line += s->picture->linesize[0] / sizeof(PIXEL); \
541  } \
542  } \
543  } \
544  }
545 
547 COPY_FRAME(16, uint16_t)
548 
550 {
551  int compno, reslevelno, bandno;
552  Jpeg2000QuantStyle *qntsty = &s->qntsty;
553  Jpeg2000CodingStyle *codsty = &s->codsty;
554 
555  for (compno = 0; compno < s->ncomponents; compno++){
556  int gbandno = 0;
557  for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
558  int nbands, lev = codsty->nreslevels - reslevelno - 1;
559  nbands = reslevelno ? 3 : 1;
560  for (bandno = 0; bandno < nbands; bandno++, gbandno++){
561  int expn, mant = 0;
562 
563  if (codsty->transform == FF_DWT97_INT){
564  int bandpos = bandno + (reslevelno>0),
565  ss = 81920000 / dwt_norms[0][bandpos][lev],
566  log = av_log2(ss);
567  mant = (11 - log < 0 ? ss >> log - 11 : ss << 11 - log) & 0x7ff;
568  expn = s->cbps[compno] - log + 13;
569  } else
570  expn = ((bandno&2)>>1) + (reslevelno>0) + s->cbps[compno];
571 
572  qntsty->expn[gbandno] = expn;
573  qntsty->mant[gbandno] = mant;
574  }
575  }
576  }
577 }
578 
579 static void init_luts(void)
580 {
581  int i, a,
582  mask = ~((1<<NMSEDEC_FRACBITS)-1);
583 
584  for (i = 0; i < (1 << NMSEDEC_BITS); i++){
585  lut_nmsedec_sig[i] = FFMAX((3 * i << (13 - NMSEDEC_FRACBITS)) - (9 << 11), 0);
586  lut_nmsedec_sig0[i] = FFMAX((i*i + (1<<NMSEDEC_FRACBITS-1) & mask) << 1, 0);
587 
588  a = (i >> (NMSEDEC_BITS-2)&2) + 1;
589  lut_nmsedec_ref[i] = FFMAX((a - 2) * (i << (13 - NMSEDEC_FRACBITS)) +
590  (1 << 13) - (a * a << 11), 0);
591  lut_nmsedec_ref0[i] = FFMAX(((i * i - (i << NMSEDEC_BITS) + (1 << 2 * NMSEDEC_FRACBITS) + (1 << (NMSEDEC_FRACBITS - 1))) & mask)
592  << 1, 0);
593  }
594 }
595 
596 /* tier-1 routines */
597 static int getnmsedec_sig(int x, int bpno)
598 {
599  if (bpno > NMSEDEC_FRACBITS)
600  return lut_nmsedec_sig[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
601  return lut_nmsedec_sig0[x & ((1 << NMSEDEC_BITS) - 1)];
602 }
603 
604 static int getnmsedec_ref(int x, int bpno)
605 {
606  if (bpno > NMSEDEC_FRACBITS)
607  return lut_nmsedec_ref[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
608  return lut_nmsedec_ref0[x & ((1 << NMSEDEC_BITS) - 1)];
609 }
610 
611 static void encode_sigpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
612 {
613  int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
614  for (y0 = 0; y0 < height; y0 += 4)
615  for (x = 0; x < width; x++)
616  for (y = y0; y < height && y < y0+4; y++){
617  if (!(t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG) && (t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG_NB)){
618  int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno),
619  bit = t1->data[(y) * t1->stride + x] & mask ? 1 : 0;
620  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, bit);
621  if (bit){
622  int xorbit;
623  int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
624  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
625  *nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
626  ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
627  }
628  t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_VIS;
629  }
630  }
631 }
632 
633 static void encode_refpass(Jpeg2000T1Context *t1, int width, int height, int *nmsedec, int bpno)
634 {
635  int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
636  for (y0 = 0; y0 < height; y0 += 4)
637  for (x = 0; x < width; x++)
638  for (y = y0; y < height && y < y0+4; y++)
639  if ((t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG){
640  int ctxno = ff_jpeg2000_getrefctxno(t1->flags[(y+1) * t1->stride + x+1]);
641  *nmsedec += getnmsedec_ref(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
642  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
643  t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_REF;
644  }
645 }
646 
647 static void encode_clnpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
648 {
649  int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
650  for (y0 = 0; y0 < height; y0 += 4)
651  for (x = 0; x < width; x++){
652  if (y0 + 3 < height && !(
653  (t1->flags[(y0+1) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
654  (t1->flags[(y0+2) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
655  (t1->flags[(y0+3) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
656  (t1->flags[(y0+4) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG))))
657  {
658  // aggregation mode
659  int rlen;
660  for (rlen = 0; rlen < 4; rlen++)
661  if (t1->data[(y0+rlen) * t1->stride + x] & mask)
662  break;
663  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL, rlen != 4);
664  if (rlen == 4)
665  continue;
666  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen >> 1);
667  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen & 1);
668  for (y = y0 + rlen; y < y0 + 4; y++){
669  if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))){
670  int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno);
671  if (y > y0 + rlen)
672  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
673  if (t1->data[(y) * t1->stride + x] & mask){ // newly significant
674  int xorbit;
675  int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
676  *nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
677  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
678  ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
679  }
680  }
681  t1->flags[(y+1) * t1->stride + x+1] &= ~JPEG2000_T1_VIS;
682  }
683  } else{
684  for (y = y0; y < y0 + 4 && y < height; y++){
685  if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))){
686  int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno);
687  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
688  if (t1->data[(y) * t1->stride + x] & mask){ // newly significant
689  int xorbit;
690  int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
691  *nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
692  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
693  ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
694  }
695  }
696  t1->flags[(y+1) * t1->stride + x+1] &= ~JPEG2000_T1_VIS;
697  }
698  }
699  }
700 }
701 
703  int width, int height, int bandpos, int lev)
704 {
705  int pass_t = 2, passno, x, y, max=0, nmsedec, bpno;
706  int64_t wmsedec = 0;
707 
708  memset(t1->flags, 0, t1->stride * (height + 2) * sizeof(*t1->flags));
709 
710  for (y = 0; y < height; y++){
711  for (x = 0; x < width; x++){
712  if (t1->data[(y) * t1->stride + x] < 0){
713  t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_SGN;
714  t1->data[(y) * t1->stride + x] = -t1->data[(y) * t1->stride + x];
715  }
716  max = FFMAX(max, t1->data[(y) * t1->stride + x]);
717  }
718  }
719 
720  if (max == 0){
721  cblk->nonzerobits = 0;
722  } else{
723  cblk->nonzerobits = av_log2(max) + 1 - NMSEDEC_FRACBITS;
724  }
725  bpno = cblk->nonzerobits - 1;
726 
727  cblk->data[0] = 0;
728  ff_mqc_initenc(&t1->mqc, cblk->data + 1);
729 
730  for (passno = 0; bpno >= 0; passno++){
731  nmsedec=0;
732 
733  switch(pass_t){
734  case 0: encode_sigpass(t1, width, height, bandpos, &nmsedec, bpno);
735  break;
736  case 1: encode_refpass(t1, width, height, &nmsedec, bpno);
737  break;
738  case 2: encode_clnpass(t1, width, height, bandpos, &nmsedec, bpno);
739  break;
740  }
741 
742  cblk->passes[passno].rate = ff_mqc_flush_to(&t1->mqc, cblk->passes[passno].flushed, &cblk->passes[passno].flushed_len);
743  cblk->passes[passno].rate -= cblk->passes[passno].flushed_len;
744 
745  wmsedec += (int64_t)nmsedec << (2*bpno);
746  cblk->passes[passno].disto = wmsedec;
747 
748  if (++pass_t == 3){
749  pass_t = 0;
750  bpno--;
751  }
752  }
753  cblk->npasses = passno;
754  cblk->ninclpasses = passno;
755 
756  if (passno) {
757  cblk->passes[passno-1].rate = ff_mqc_flush_to(&t1->mqc, cblk->passes[passno-1].flushed, &cblk->passes[passno-1].flushed_len);
758  cblk->passes[passno-1].rate -= cblk->passes[passno-1].flushed_len;
759  }
760 }
761 
762 /* tier-2 routines: */
763 
765 {
766  if (n == 1)
767  put_num(s, 0, 1);
768  else if (n == 2)
769  put_num(s, 2, 2);
770  else if (n <= 5)
771  put_num(s, 0xc | (n-3), 4);
772  else if (n <= 36)
773  put_num(s, 0x1e0 | (n-6), 9);
774  else
775  put_num(s, 0xff80 | (n-37), 16);
776 }
777 
778 
779 static int encode_packet(Jpeg2000EncoderContext *s, Jpeg2000ResLevel *rlevel, int layno,
780  int precno, uint8_t *expn, int numgbits, int packetno,
781  int nlayers)
782 {
783  int bandno, empty = 1;
784  int i;
785  // init bitstream
786  *s->buf = 0;
787  s->bit_index = 0;
788 
789  if (s->sop) {
790  bytestream_put_be16(&s->buf, JPEG2000_SOP);
791  bytestream_put_be16(&s->buf, 4);
792  bytestream_put_be16(&s->buf, packetno);
793  }
794  // header
795 
796  if (!layno) {
797  for (bandno = 0; bandno < rlevel->nbands; bandno++) {
798  Jpeg2000Band *band = rlevel->band + bandno;
799  if (band->coord[0][0] < band->coord[0][1]
800  && band->coord[1][0] < band->coord[1][1]) {
801  Jpeg2000Prec *prec = band->prec + precno;
802  int nb_cblks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
803  int pos;
806  for (pos = 0; pos < nb_cblks; pos++) {
807  Jpeg2000Cblk *cblk = &prec->cblk[pos];
808  prec->zerobits[pos].val = expn[bandno] + numgbits - 1 - cblk->nonzerobits;
809  cblk->incl = 0;
810  cblk->lblock = 3;
811  tag_tree_update(prec->zerobits + pos);
812  for (i = 0; i < nlayers; i++) {
813  if (cblk->layers[i].npasses > 0) {
814  prec->cblkincl[pos].val = i;
815  break;
816  }
817  }
818  if (i == nlayers)
819  prec->cblkincl[pos].val = i;
820  tag_tree_update(prec->cblkincl + pos);
821  }
822  }
823  }
824  }
825 
826  // is the packet empty?
827  for (bandno = 0; bandno < rlevel->nbands; bandno++){
828  Jpeg2000Band *band = rlevel->band + bandno;
829  if (band->coord[0][0] < band->coord[0][1]
830  && band->coord[1][0] < band->coord[1][1]) {
831  Jpeg2000Prec *prec = band->prec + precno;
832  int nb_cblks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
833  int pos;
834  for (pos = 0; pos < nb_cblks; pos++) {
835  Jpeg2000Cblk *cblk = &prec->cblk[pos];
836  if (cblk->layers[layno].npasses) {
837  empty = 0;
838  break;
839  }
840  }
841  if (!empty)
842  break;
843  }
844  }
845 
846  put_bits(s, !empty, 1);
847  if (empty){
848  j2k_flush(s);
849  if (s->eph)
850  bytestream_put_be16(&s->buf, JPEG2000_EPH);
851  return 0;
852  }
853 
854  for (bandno = 0; bandno < rlevel->nbands; bandno++) {
855  Jpeg2000Band *band = rlevel->band + bandno;
856  Jpeg2000Prec *prec = band->prec + precno;
857  int yi, xi, pos;
858  int cblknw = prec->nb_codeblocks_width;
859 
860  if (band->coord[0][0] == band->coord[0][1]
861  || band->coord[1][0] == band->coord[1][1])
862  continue;
863 
864  for (pos=0, yi = 0; yi < prec->nb_codeblocks_height; yi++) {
865  for (xi = 0; xi < cblknw; xi++, pos++){
866  int llen = 0, length;
867  Jpeg2000Cblk *cblk = prec->cblk + yi * cblknw + xi;
868 
869  if (s->buf_end - s->buf < 20) // approximately
870  return -1;
871 
872  // inclusion information
873  if (!cblk->incl)
874  tag_tree_code(s, prec->cblkincl + pos, layno + 1);
875  else {
876  put_bits(s, cblk->layers[layno].npasses > 0, 1);
877  }
878 
879  if (!cblk->layers[layno].npasses)
880  continue;
881 
882  // zerobits information
883  if (!cblk->incl) {
884  tag_tree_code(s, prec->zerobits + pos, 100);
885  cblk->incl = 1;
886  }
887 
888  // number of passes
889  putnumpasses(s, cblk->layers[layno].npasses);
890 
891  length = cblk->layers[layno].data_len;
892  if (layno == nlayers - 1 && cblk->layers[layno].cum_passes){
893  length += cblk->passes[cblk->layers[layno].cum_passes-1].flushed_len;
894  }
895  if (cblk->lblock + av_log2(cblk->layers[layno].npasses) < av_log2(length) + 1) {
896  llen = av_log2(length) + 1 - cblk->lblock - av_log2(cblk->layers[layno].npasses);
897  }
898 
899  // length of code block
900  cblk->lblock += llen;
901  put_bits(s, 1, llen);
902  put_bits(s, 0, 1);
903  put_num(s, length, cblk->lblock + av_log2(cblk->layers[layno].npasses));
904  }
905  }
906  }
907  j2k_flush(s);
908  if (s->eph) {
909  bytestream_put_be16(&s->buf, JPEG2000_EPH);
910  }
911 
912  for (bandno = 0; bandno < rlevel->nbands; bandno++) {
913  Jpeg2000Band *band = rlevel->band + bandno;
914  Jpeg2000Prec *prec = band->prec + precno;
915  int yi, cblknw = prec->nb_codeblocks_width;
916  for (yi =0; yi < prec->nb_codeblocks_height; yi++) {
917  int xi;
918  for (xi = 0; xi < cblknw; xi++){
919  Jpeg2000Cblk *cblk = prec->cblk + yi * cblknw + xi;
920  if (cblk->layers[layno].npasses) {
921  if (s->buf_end - s->buf < cblk->layers[layno].data_len + 2)
922  return -1;
923  bytestream_put_buffer(&s->buf, cblk->layers[layno].data_start + 1, cblk->layers[layno].data_len);
924  if (layno == nlayers - 1 && cblk->layers[layno].cum_passes) {
925  bytestream_put_buffer(&s->buf, cblk->passes[cblk->layers[layno].cum_passes-1].flushed,
926  cblk->passes[cblk->layers[layno].cum_passes-1].flushed_len);
927  }
928  }
929  }
930  }
931  }
932  return 0;
933 }
934 
935 static int encode_packets(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno, int nlayers)
936 {
937  int compno, reslevelno, layno, ret;
938  Jpeg2000CodingStyle *codsty = &s->codsty;
939  Jpeg2000QuantStyle *qntsty = &s->qntsty;
940  int packetno = 0;
941  int step_x, step_y;
942  int x, y;
943  int tile_coord[2][2];
944  int col = tileno % s->numXtiles;
945  int row = tileno / s->numXtiles;
946 
947  tile_coord[0][0] = col * s->tile_width;
948  tile_coord[0][1] = FFMIN(tile_coord[0][0] + s->tile_width, s->width);
949  tile_coord[1][0] = row * s->tile_height;
950  tile_coord[1][1] = FFMIN(tile_coord[1][0] + s->tile_height, s->height);
951 
952  av_log(s->avctx, AV_LOG_DEBUG, "tier2\n");
953  // lay-rlevel-comp-pos progression
954  switch (s->prog) {
955  case JPEG2000_PGOD_LRCP:
956  for (layno = 0; layno < nlayers; layno++) {
957  for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
958  for (compno = 0; compno < s->ncomponents; compno++){
959  int precno;
960  Jpeg2000ResLevel *reslevel = s->tile[tileno].comp[compno].reslevel + reslevelno;
961  for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
962  if ((ret = encode_packet(s, reslevel, layno, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
963  qntsty->nguardbits, packetno++, nlayers)) < 0)
964  return ret;
965  }
966  }
967  }
968  }
969  break;
970  case JPEG2000_PGOD_RLCP:
971  for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
972  for (layno = 0; layno < nlayers; layno++) {
973  for (compno = 0; compno < s->ncomponents; compno++){
974  int precno;
975  Jpeg2000ResLevel *reslevel = s->tile[tileno].comp[compno].reslevel + reslevelno;
976  for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
977  if ((ret = encode_packet(s, reslevel, layno, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
978  qntsty->nguardbits, packetno++, nlayers)) < 0)
979  return ret;
980  }
981  }
982  }
983  }
984  break;
985  case JPEG2000_PGOD_RPCL:
986  for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
987  int precno;
988  step_x = 30;
989  step_y = 30;
990  for (compno = 0; compno < s->ncomponents; compno++) {
991  Jpeg2000Component *comp = tile->comp + compno;
992  if (reslevelno < codsty->nreslevels) {
993  uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
994  Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
995  step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
996  step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
997  }
998  }
999 
1000  step_x = 1<<step_x;
1001  step_y = 1<<step_y;
1002  for (y = tile_coord[1][0]; y < tile_coord[1][1]; y = (y/step_y + 1)*step_y) {
1003  for (x = tile_coord[0][0]; x < tile_coord[0][1]; x = (x/step_x + 1)*step_x) {
1004  for (compno = 0; compno < s->ncomponents; compno++) {
1005  Jpeg2000Component *comp = tile->comp + compno;
1006  uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1007  Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
1008  int log_subsampling[2] = { compno?s->chroma_shift[0]:0, compno?s->chroma_shift[1]:0};
1009  unsigned prcx, prcy;
1010  int trx0, try0;
1011 
1012  trx0 = ff_jpeg2000_ceildivpow2(tile_coord[0][0], log_subsampling[0] + reducedresno);
1013  try0 = ff_jpeg2000_ceildivpow2(tile_coord[1][0], log_subsampling[1] + reducedresno);
1014 
1015  if (!(y % ((uint64_t)1 << (reslevel->log2_prec_height + reducedresno + log_subsampling[1])) == 0 ||
1016  (y == tile_coord[1][0] && (try0 << reducedresno) % (1U << (reducedresno + reslevel->log2_prec_height)))))
1017  continue;
1018 
1019  if (!(x % ((uint64_t)1 << (reslevel->log2_prec_width + reducedresno + log_subsampling[0])) == 0 ||
1020  (x == tile_coord[0][0] && (trx0 << reducedresno) % (1U << (reducedresno + reslevel->log2_prec_width)))))
1021  continue;
1022 
1023  // check if a precinct exists
1024  prcx = ff_jpeg2000_ceildivpow2(x, log_subsampling[0] + reducedresno) >> reslevel->log2_prec_width;
1025  prcy = ff_jpeg2000_ceildivpow2(y, log_subsampling[1] + reducedresno) >> reslevel->log2_prec_height;
1026  prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> reslevel->log2_prec_width;
1027  prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> reslevel->log2_prec_height;
1028  precno = prcx + reslevel->num_precincts_x * prcy;
1029 
1030  if (prcx >= reslevel->num_precincts_x || prcy >= reslevel->num_precincts_y) {
1031  av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1032  prcx, prcy, reslevel->num_precincts_x, reslevel->num_precincts_y);
1033  continue;
1034  }
1035  for (layno = 0; layno < nlayers; layno++) {
1036  if ((ret = encode_packet(s, reslevel, layno, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
1037  qntsty->nguardbits, packetno++, nlayers)) < 0)
1038  return ret;
1039  }
1040  }
1041  }
1042  }
1043  }
1044  break;
1045  case JPEG2000_PGOD_PCRL:
1046  step_x = 32;
1047  step_y = 32;
1048  for (compno = 0; compno < s->ncomponents; compno++) {
1049  Jpeg2000Component *comp = tile->comp + compno;
1050 
1051  for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
1052  uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1053  Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1054  step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
1055  step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1056  }
1057  }
1058  if (step_x >= 31 || step_y >= 31){
1059  avpriv_request_sample(s->avctx, "PCRL with large step");
1060  return AVERROR_PATCHWELCOME;
1061  }
1062  step_x = 1<<step_x;
1063  step_y = 1<<step_y;
1064 
1065  for (y = tile_coord[1][0]; y < tile_coord[1][1]; y = (y/step_y + 1)*step_y) {
1066  for (x = tile_coord[0][0]; x < tile_coord[0][1]; x = (x/step_x + 1)*step_x) {
1067  for (compno = 0; compno < s->ncomponents; compno++) {
1068  Jpeg2000Component *comp = tile->comp + compno;
1069  int log_subsampling[2] = { compno?s->chroma_shift[0]:0, compno?s->chroma_shift[1]:0};
1070 
1071  for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
1072  unsigned prcx, prcy;
1073  int precno;
1074  uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1075  Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
1076  int trx0, try0;
1077 
1078  trx0 = ff_jpeg2000_ceildivpow2(tile_coord[0][0], log_subsampling[0] + reducedresno);
1079  try0 = ff_jpeg2000_ceildivpow2(tile_coord[1][0], log_subsampling[1] + reducedresno);
1080 
1081  if (!(y % ((uint64_t)1 << (reslevel->log2_prec_height + reducedresno + log_subsampling[1])) == 0 ||
1082  (y == tile_coord[1][0] && (try0 << reducedresno) % (1U << (reducedresno + reslevel->log2_prec_height)))))
1083  continue;
1084 
1085  if (!(x % ((uint64_t)1 << (reslevel->log2_prec_width + reducedresno + log_subsampling[0])) == 0 ||
1086  (x == tile_coord[0][0] && (trx0 << reducedresno) % (1U << (reducedresno + reslevel->log2_prec_width)))))
1087  continue;
1088 
1089  // check if a precinct exists
1090  prcx = ff_jpeg2000_ceildivpow2(x, log_subsampling[0] + reducedresno) >> reslevel->log2_prec_width;
1091  prcy = ff_jpeg2000_ceildivpow2(y, log_subsampling[1] + reducedresno) >> reslevel->log2_prec_height;
1092  prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> reslevel->log2_prec_width;
1093  prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> reslevel->log2_prec_height;
1094 
1095  precno = prcx + reslevel->num_precincts_x * prcy;
1096 
1097  if (prcx >= reslevel->num_precincts_x || prcy >= reslevel->num_precincts_y) {
1098  av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1099  prcx, prcy, reslevel->num_precincts_x, reslevel->num_precincts_y);
1100  continue;
1101  }
1102  for (layno = 0; layno < nlayers; layno++) {
1103  if ((ret = encode_packet(s, reslevel, layno, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
1104  qntsty->nguardbits, packetno++, nlayers)) < 0)
1105  return ret;
1106  }
1107  }
1108  }
1109  }
1110  }
1111  break;
1112  case JPEG2000_PGOD_CPRL:
1113  for (compno = 0; compno < s->ncomponents; compno++) {
1114  Jpeg2000Component *comp = tile->comp + compno;
1115  int log_subsampling[2] = { compno?s->chroma_shift[0]:0, compno?s->chroma_shift[1]:0};
1116  step_x = 32;
1117  step_y = 32;
1118 
1119  for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
1120  uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1121  Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1122  step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
1123  step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1124  }
1125  if (step_x >= 31 || step_y >= 31){
1126  avpriv_request_sample(s->avctx, "CPRL with large step");
1127  return AVERROR_PATCHWELCOME;
1128  }
1129  step_x = 1<<step_x;
1130  step_y = 1<<step_y;
1131 
1132  for (y = tile_coord[1][0]; y < tile_coord[1][1]; y = (y/step_y + 1)*step_y) {
1133  for (x = tile_coord[0][0]; x < tile_coord[0][1]; x = (x/step_x + 1)*step_x) {
1134  for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
1135  unsigned prcx, prcy;
1136  int precno;
1137  int trx0, try0;
1138  uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1139  Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
1140 
1141  trx0 = ff_jpeg2000_ceildivpow2(tile_coord[0][0], log_subsampling[0] + reducedresno);
1142  try0 = ff_jpeg2000_ceildivpow2(tile_coord[1][0], log_subsampling[1] + reducedresno);
1143 
1144  if (!(y % ((uint64_t)1 << (reslevel->log2_prec_height + reducedresno + log_subsampling[1])) == 0 ||
1145  (y == tile_coord[1][0] && (try0 << reducedresno) % (1U << (reducedresno + reslevel->log2_prec_height)))))
1146  continue;
1147 
1148  if (!(x % ((uint64_t)1 << (reslevel->log2_prec_width + reducedresno + log_subsampling[0])) == 0 ||
1149  (x == tile_coord[0][0] && (trx0 << reducedresno) % (1U << (reducedresno + reslevel->log2_prec_width)))))
1150  continue;
1151 
1152  // check if a precinct exists
1153  prcx = ff_jpeg2000_ceildivpow2(x, log_subsampling[0] + reducedresno) >> reslevel->log2_prec_width;
1154  prcy = ff_jpeg2000_ceildivpow2(y, log_subsampling[1] + reducedresno) >> reslevel->log2_prec_height;
1155  prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> reslevel->log2_prec_width;
1156  prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> reslevel->log2_prec_height;
1157 
1158  precno = prcx + reslevel->num_precincts_x * prcy;
1159 
1160  if (prcx >= reslevel->num_precincts_x || prcy >= reslevel->num_precincts_y) {
1161  av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1162  prcx, prcy, reslevel->num_precincts_x, reslevel->num_precincts_y);
1163  continue;
1164  }
1165  for (layno = 0; layno < nlayers; layno++) {
1166  if ((ret = encode_packet(s, reslevel, layno, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
1167  qntsty->nguardbits, packetno++, nlayers)) < 0)
1168  return ret;
1169  }
1170  }
1171  }
1172  }
1173  }
1174 
1175  }
1176 
1177  av_log(s->avctx, AV_LOG_DEBUG, "after tier2\n");
1178  return 0;
1179 }
1180 
1181 static void makelayer(Jpeg2000EncoderContext *s, int layno, double thresh, Jpeg2000Tile* tile, int final)
1182 {
1183  int compno, resno, bandno, precno, cblkno;
1184  int passno;
1185 
1186  for (compno = 0; compno < s->ncomponents; compno++) {
1187  Jpeg2000Component *comp = &tile->comp[compno];
1188 
1189  for (resno = 0; resno < s->codsty.nreslevels; resno++) {
1190  Jpeg2000ResLevel *reslevel = comp->reslevel + resno;
1191 
1192  for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
1193  for (bandno = 0; bandno < reslevel->nbands ; bandno++){
1194  Jpeg2000Band *band = reslevel->band + bandno;
1195  Jpeg2000Prec *prec = band->prec + precno;
1196 
1197  for (cblkno = 0; cblkno < prec->nb_codeblocks_height * prec->nb_codeblocks_width; cblkno++){
1198  Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1199  Jpeg2000Layer *layer = &cblk->layers[layno];
1200  int n;
1201 
1202  if (layno == 0) {
1203  cblk->ninclpasses = 0;
1204  }
1205 
1206  n = cblk->ninclpasses;
1207 
1208  if (thresh < 0) {
1209  n = cblk->npasses;
1210  } else {
1211  for (passno = cblk->ninclpasses; passno < cblk->npasses; passno++) {
1212  int32_t dr;
1213  double dd;
1214  Jpeg2000Pass *pass = &cblk->passes[passno];
1215 
1216  if (n == 0) {
1217  dr = pass->rate;
1218  dd = pass->disto;
1219  } else {
1220  dr = pass->rate - cblk->passes[n - 1].rate;
1221  dd = pass->disto - cblk->passes[n-1].disto;
1222  }
1223 
1224  if (!dr) {
1225  if (dd != 0.0) {
1226  n = passno + 1;
1227  }
1228  continue;
1229  }
1230 
1231  if (thresh - (dd / dr) < DBL_EPSILON)
1232  n = passno + 1;
1233  }
1234  }
1235  layer->npasses = n - cblk->ninclpasses;
1236  layer->cum_passes = n;
1237 
1238  if (layer->npasses == 0) {
1239  layer->disto = 0;
1240  layer->data_len = 0;
1241  continue;
1242  }
1243 
1244  if (cblk->ninclpasses == 0) {
1245  layer->data_len = cblk->passes[n - 1].rate;
1246  layer->data_start = cblk->data;
1247  layer->disto = cblk->passes[n - 1].disto;
1248  } else {
1249  layer->data_len = cblk->passes[n - 1].rate - cblk->passes[cblk->ninclpasses - 1].rate;
1250  layer->data_start = cblk->data + cblk->passes[cblk->ninclpasses - 1].rate;
1251  layer->disto = cblk->passes[n - 1].disto -
1252  cblk->passes[cblk->ninclpasses - 1].disto;
1253  }
1254  if (final) {
1255  cblk->ninclpasses = n;
1256  }
1257  }
1258  }
1259  }
1260  }
1261  }
1262 }
1263 
1265 {
1266  int precno, compno, reslevelno, bandno, cblkno, lev, passno, layno;
1267  int i;
1268  double min = DBL_MAX;
1269  double max = 0;
1270  double thresh;
1271  int tile_disto = 0;
1272 
1273  Jpeg2000CodingStyle *codsty = &s->codsty;
1274 
1275  for (compno = 0; compno < s->ncomponents; compno++){
1276  Jpeg2000Component *comp = tile->comp + compno;
1277 
1278  for (reslevelno = 0, lev = codsty->nreslevels-1; reslevelno < codsty->nreslevels; reslevelno++, lev--){
1279  Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
1280 
1281  for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
1282  for (bandno = 0; bandno < reslevel->nbands ; bandno++){
1283  Jpeg2000Band *band = reslevel->band + bandno;
1284  Jpeg2000Prec *prec = band->prec + precno;
1285 
1286  for (cblkno = 0; cblkno < prec->nb_codeblocks_height * prec->nb_codeblocks_width; cblkno++){
1287  Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1288  for (passno = 0; passno < cblk->npasses; passno++) {
1289  Jpeg2000Pass *pass = &cblk->passes[passno];
1290  int dr;
1291  double dd, drslope;
1292 
1293  tile_disto += pass->disto;
1294  if (passno == 0) {
1295  dr = (int32_t)pass->rate;
1296  dd = pass->disto;
1297  } else {
1298  dr = (int32_t)(pass->rate - cblk->passes[passno - 1].rate);
1299  dd = pass->disto - cblk->passes[passno - 1].disto;
1300  }
1301 
1302  if (dr <= 0)
1303  continue;
1304 
1305  drslope = dd / dr;
1306  if (drslope < min)
1307  min = drslope;
1308 
1309  if (drslope > max)
1310  max = drslope;
1311  }
1312  }
1313  }
1314  }
1315  }
1316  }
1317 
1318  for (layno = 0; layno < s->nlayers; layno++) {
1319  double lo = min;
1320  double hi = max;
1321  double stable_thresh = 0.0;
1322  double good_thresh = 0.0;
1323  if (!s->layer_rates[layno]) {
1324  good_thresh = -1.0;
1325  } else {
1326  for (i = 0; i < 128; i++) {
1327  uint8_t *stream_pos = s->buf;
1328  int ret;
1329  thresh = (lo + hi) / 2;
1330  makelayer(s, layno, thresh, tile, 0);
1331  ret = encode_packets(s, tile, (int)(tile - s->tile), layno + 1);
1332  memset(stream_pos, 0, s->buf - stream_pos);
1333  if ((s->buf - stream_pos > ceil(tile->layer_rates[layno])) || ret < 0) {
1334  lo = thresh;
1335  s->buf = stream_pos;
1336  continue;
1337  }
1338  hi = thresh;
1339  stable_thresh = thresh;
1340  s->buf = stream_pos;
1341  }
1342  }
1343  if (good_thresh >= 0.0)
1344  good_thresh = stable_thresh == 0.0 ? thresh : stable_thresh;
1345  makelayer(s, layno, good_thresh, tile, 1);
1346  }
1347 }
1348 
1349 static int getcut(Jpeg2000Cblk *cblk, int64_t lambda, int dwt_norm)
1350 {
1351  int passno, res = 0;
1352  for (passno = 0; passno < cblk->npasses; passno++){
1353  int dr;
1354  int64_t dd;
1355 
1356  dr = cblk->passes[passno].rate
1357  - (res ? cblk->passes[res-1].rate : 0);
1358  dd = cblk->passes[passno].disto
1359  - (res ? cblk->passes[res-1].disto : 0);
1360 
1361  if (((dd * dwt_norm) >> WMSEDEC_SHIFT) * dwt_norm >= dr * lambda)
1362  res = passno+1;
1363  }
1364  return res;
1365 }
1366 
1368 {
1369  int precno, compno, reslevelno, bandno, cblkno, lev;
1370  Jpeg2000CodingStyle *codsty = &s->codsty;
1371 
1372  for (compno = 0; compno < s->ncomponents; compno++){
1373  Jpeg2000Component *comp = tile->comp + compno;
1374 
1375  for (reslevelno = 0, lev = codsty->nreslevels-1; reslevelno < codsty->nreslevels; reslevelno++, lev--){
1376  Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
1377 
1378  for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
1379  for (bandno = 0; bandno < reslevel->nbands ; bandno++){
1380  int bandpos = bandno + (reslevelno > 0);
1381  Jpeg2000Band *band = reslevel->band + bandno;
1382  Jpeg2000Prec *prec = band->prec + precno;
1383 
1384  for (cblkno = 0; cblkno < prec->nb_codeblocks_height * prec->nb_codeblocks_width; cblkno++){
1385  Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1386 
1387  cblk->ninclpasses = getcut(cblk, s->lambda,
1388  (int64_t)dwt_norms[codsty->transform == FF_DWT53][bandpos][lev] * (int64_t)band->i_stepsize >> 15);
1389  cblk->layers[0].data_start = cblk->data;
1390  cblk->layers[0].cum_passes = cblk->ninclpasses;
1391  cblk->layers[0].npasses = cblk->ninclpasses;
1392  if (cblk->ninclpasses)
1393  cblk->layers[0].data_len = cblk->passes[cblk->ninclpasses - 1].rate;
1394  }
1395  }
1396  }
1397  }
1398  }
1399 }
1400 
1401 static int encode_tile(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno)
1402 {
1403  int compno, reslevelno, bandno, ret;
1405  Jpeg2000CodingStyle *codsty = &s->codsty;
1406  for (compno = 0; compno < s->ncomponents; compno++){
1407  Jpeg2000Component *comp = s->tile[tileno].comp + compno;
1408 
1409  t1.stride = (1<<codsty->log2_cblk_width) + 2;
1410 
1411  av_log(s->avctx, AV_LOG_DEBUG,"dwt\n");
1412  if ((ret = ff_dwt_encode(&comp->dwt, comp->i_data)) < 0)
1413  return ret;
1414  av_log(s->avctx, AV_LOG_DEBUG,"after dwt -> tier1\n");
1415 
1416  for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
1417  Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
1418 
1419  for (bandno = 0; bandno < reslevel->nbands ; bandno++){
1420  Jpeg2000Band *band = reslevel->band + bandno;
1421  Jpeg2000Prec *prec = band->prec; // we support only 1 precinct per band ATM in the encoder
1422  int cblkx, cblky, cblkno=0, xx0, x0, xx1, y0, yy0, yy1, bandpos;
1423  yy0 = bandno == 0 ? 0 : comp->reslevel[reslevelno-1].coord[1][1] - comp->reslevel[reslevelno-1].coord[1][0];
1424  y0 = yy0;
1425  yy1 = FFMIN(ff_jpeg2000_ceildivpow2(band->coord[1][0] + 1, band->log2_cblk_height) << band->log2_cblk_height,
1426  band->coord[1][1]) - band->coord[1][0] + yy0;
1427 
1428  if (band->coord[0][0] == band->coord[0][1] || band->coord[1][0] == band->coord[1][1])
1429  continue;
1430 
1431  bandpos = bandno + (reslevelno > 0);
1432 
1433  for (cblky = 0; cblky < prec->nb_codeblocks_height; cblky++){
1434  if (reslevelno == 0 || bandno == 1)
1435  xx0 = 0;
1436  else
1437  xx0 = comp->reslevel[reslevelno-1].coord[0][1] - comp->reslevel[reslevelno-1].coord[0][0];
1438  x0 = xx0;
1439  xx1 = FFMIN(ff_jpeg2000_ceildivpow2(band->coord[0][0] + 1, band->log2_cblk_width) << band->log2_cblk_width,
1440  band->coord[0][1]) - band->coord[0][0] + xx0;
1441 
1442  for (cblkx = 0; cblkx < prec->nb_codeblocks_width; cblkx++, cblkno++){
1443  int y, x;
1444  if (codsty->transform == FF_DWT53){
1445  for (y = yy0; y < yy1; y++){
1446  int *ptr = t1.data + (y-yy0)*t1.stride;
1447  for (x = xx0; x < xx1; x++){
1448  *ptr++ = comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * y + x] * (1 << NMSEDEC_FRACBITS);
1449  }
1450  }
1451  } else{
1452  for (y = yy0; y < yy1; y++){
1453  int *ptr = t1.data + (y-yy0)*t1.stride;
1454  for (x = xx0; x < xx1; x++){
1455  *ptr = (comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * y + x]);
1456  *ptr = (int64_t)*ptr * (int64_t)(16384 * 65536 / band->i_stepsize) >> 15 - NMSEDEC_FRACBITS;
1457  ptr++;
1458  }
1459  }
1460  }
1461  if (!prec->cblk[cblkno].data)
1462  prec->cblk[cblkno].data = av_malloc(1 + 8192);
1463  if (!prec->cblk[cblkno].passes)
1464  prec->cblk[cblkno].passes = av_malloc_array(JPEG2000_MAX_PASSES, sizeof (*prec->cblk[cblkno].passes));
1465  if (!prec->cblk[cblkno].data || !prec->cblk[cblkno].passes)
1466  return AVERROR(ENOMEM);
1467  encode_cblk(s, &t1, prec->cblk + cblkno, tile, xx1 - xx0, yy1 - yy0,
1468  bandpos, codsty->nreslevels - reslevelno - 1);
1469  xx0 = xx1;
1470  xx1 = FFMIN(xx1 + (1 << band->log2_cblk_width), band->coord[0][1] - band->coord[0][0] + x0);
1471  }
1472  yy0 = yy1;
1473  yy1 = FFMIN(yy1 + (1 << band->log2_cblk_height), band->coord[1][1] - band->coord[1][0] + y0);
1474  }
1475  }
1476  }
1477  av_log(s->avctx, AV_LOG_DEBUG, "after tier1\n");
1478  }
1479 
1480  av_log(s->avctx, AV_LOG_DEBUG, "rate control\n");
1481  if (s->compression_rate_enc)
1482  makelayers(s, tile);
1483  else
1484  truncpasses(s, tile);
1485 
1486  if ((ret = encode_packets(s, tile, tileno, s->nlayers)) < 0)
1487  return ret;
1488  av_log(s->avctx, AV_LOG_DEBUG, "after rate control\n");
1489  return 0;
1490 }
1491 
1493 {
1494  int tileno, compno;
1495  Jpeg2000CodingStyle *codsty = &s->codsty;
1496 
1497  if (!s->tile)
1498  return;
1499  for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
1500  if (s->tile[tileno].comp) {
1501  for (compno = 0; compno < s->ncomponents; compno++){
1502  Jpeg2000Component *comp = s->tile[tileno].comp + compno;
1503  ff_jpeg2000_cleanup(comp, codsty);
1504  }
1505  av_freep(&s->tile[tileno].comp);
1506  }
1507  av_freep(&s->tile[tileno].layer_rates);
1508  }
1509  av_freep(&s->tile);
1510 }
1511 
1513 {
1514  int tileno, compno;
1515  for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
1516  Jpeg2000Tile *tile = s->tile + tileno;
1517  for (compno = 0; compno < s->ncomponents; compno++)
1518  ff_jpeg2000_reinit(tile->comp + compno, &s->codsty);
1519  }
1520 }
1521 
1522 static void update_size(uint8_t *size, const uint8_t *end)
1523 {
1524  AV_WB32(size, end-size);
1525 }
1526 
1528  const AVFrame *pict, int *got_packet)
1529 {
1530  int tileno, ret;
1532  uint8_t *chunkstart, *jp2cstart, *jp2hstart;
1534 
1535  if ((ret = ff_alloc_packet2(avctx, pkt, avctx->width*avctx->height*9 + AV_INPUT_BUFFER_MIN_SIZE, 0)) < 0)
1536  return ret;
1537 
1538  // init:
1539  s->buf = s->buf_start = pkt->data;
1540  s->buf_end = pkt->data + pkt->size;
1541 
1542  s->picture = pict;
1543 
1544  s->lambda = s->picture->quality * LAMBDA_SCALE;
1545 
1546  if (s->cbps[0] > 8)
1547  copy_frame_16(s);
1548  else
1549  copy_frame_8(s);
1550 
1551  reinit(s);
1552 
1553  if (s->format == CODEC_JP2) {
1554  av_assert0(s->buf == pkt->data);
1555 
1556  bytestream_put_be32(&s->buf, 0x0000000C);
1557  bytestream_put_be32(&s->buf, 0x6A502020);
1558  bytestream_put_be32(&s->buf, 0x0D0A870A);
1559 
1560  chunkstart = s->buf;
1561  bytestream_put_be32(&s->buf, 0);
1562  bytestream_put_buffer(&s->buf, "ftyp", 4);
1563  bytestream_put_buffer(&s->buf, "jp2\040\040", 4);
1564  bytestream_put_be32(&s->buf, 0);
1565  bytestream_put_buffer(&s->buf, "jp2\040", 4);
1566  update_size(chunkstart, s->buf);
1567 
1568  jp2hstart = s->buf;
1569  bytestream_put_be32(&s->buf, 0);
1570  bytestream_put_buffer(&s->buf, "jp2h", 4);
1571 
1572  chunkstart = s->buf;
1573  bytestream_put_be32(&s->buf, 0);
1574  bytestream_put_buffer(&s->buf, "ihdr", 4);
1575  bytestream_put_be32(&s->buf, avctx->height);
1576  bytestream_put_be32(&s->buf, avctx->width);
1577  bytestream_put_be16(&s->buf, s->ncomponents);
1578  bytestream_put_byte(&s->buf, s->cbps[0]);
1579  bytestream_put_byte(&s->buf, 7);
1580  bytestream_put_byte(&s->buf, 0);
1581  bytestream_put_byte(&s->buf, 0);
1582  update_size(chunkstart, s->buf);
1583 
1584  chunkstart = s->buf;
1585  bytestream_put_be32(&s->buf, 0);
1586  bytestream_put_buffer(&s->buf, "colr", 4);
1587  bytestream_put_byte(&s->buf, 1);
1588  bytestream_put_byte(&s->buf, 0);
1589  bytestream_put_byte(&s->buf, 0);
1590  if ((desc->flags & AV_PIX_FMT_FLAG_RGB) || avctx->pix_fmt == AV_PIX_FMT_PAL8) {
1591  bytestream_put_be32(&s->buf, 16);
1592  } else if (s->ncomponents == 1) {
1593  bytestream_put_be32(&s->buf, 17);
1594  } else {
1595  bytestream_put_be32(&s->buf, 18);
1596  }
1597  update_size(chunkstart, s->buf);
1598  if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
1599  int i;
1600  uint8_t *palette = pict->data[1];
1601  chunkstart = s->buf;
1602  bytestream_put_be32(&s->buf, 0);
1603  bytestream_put_buffer(&s->buf, "pclr", 4);
1604  bytestream_put_be16(&s->buf, AVPALETTE_COUNT);
1605  bytestream_put_byte(&s->buf, 3); // colour channels
1606  bytestream_put_be24(&s->buf, 0x070707); //colour depths
1607  for (i = 0; i < AVPALETTE_COUNT; i++) {
1608  bytestream_put_be24(&s->buf, HAVE_BIGENDIAN ? AV_RB24(palette + 1) : AV_RL24(palette));
1609  palette += 4;
1610  }
1611  update_size(chunkstart, s->buf);
1612  chunkstart = s->buf;
1613  bytestream_put_be32(&s->buf, 0);
1614  bytestream_put_buffer(&s->buf, "cmap", 4);
1615  for (i = 0; i < 3; i++) {
1616  bytestream_put_be16(&s->buf, 0); // component
1617  bytestream_put_byte(&s->buf, 1); // palette mapping
1618  bytestream_put_byte(&s->buf, i); // index
1619  }
1620  update_size(chunkstart, s->buf);
1621  }
1622  update_size(jp2hstart, s->buf);
1623 
1624  jp2cstart = s->buf;
1625  bytestream_put_be32(&s->buf, 0);
1626  bytestream_put_buffer(&s->buf, "jp2c", 4);
1627  }
1628 
1629  if (s->buf_end - s->buf < 2)
1630  return -1;
1631  bytestream_put_be16(&s->buf, JPEG2000_SOC);
1632  if ((ret = put_siz(s)) < 0)
1633  return ret;
1634  if ((ret = put_cod(s)) < 0)
1635  return ret;
1636  if ((ret = put_qcd(s, 0)) < 0)
1637  return ret;
1638  if ((ret = put_com(s, 0)) < 0)
1639  return ret;
1640 
1641  for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
1642  uint8_t *psotptr;
1643  if (!(psotptr = put_sot(s, tileno)))
1644  return -1;
1645  if (s->buf_end - s->buf < 2)
1646  return -1;
1647  bytestream_put_be16(&s->buf, JPEG2000_SOD);
1648  if ((ret = encode_tile(s, s->tile + tileno, tileno)) < 0)
1649  return ret;
1650  bytestream_put_be32(&psotptr, s->buf - psotptr + 6);
1651  }
1652  if (s->buf_end - s->buf < 2)
1653  return -1;
1654  bytestream_put_be16(&s->buf, JPEG2000_EOC);
1655 
1656  if (s->format == CODEC_JP2)
1657  update_size(jp2cstart, s->buf);
1658 
1659  av_log(s->avctx, AV_LOG_DEBUG, "end\n");
1660  pkt->size = s->buf - s->buf_start;
1662  *got_packet = 1;
1663 
1664  return 0;
1665 }
1666 
1668 {
1669  int i;
1670  char *token;
1671  char *saveptr = NULL;
1672  int rate;
1673  int nlayers = 0;
1674  if (!s->lr_str) {
1675  s->nlayers = 1;
1676  s->layer_rates[0] = 0;
1677  s->compression_rate_enc = 0;
1678  return 0;
1679  }
1680 
1681  token = av_strtok(s->lr_str, ",", &saveptr);
1682  if (token && (rate = strtol(token, NULL, 10))) {
1683  s->layer_rates[0] = rate <= 1 ? 0:rate;
1684  nlayers++;
1685  } else {
1686  return AVERROR_INVALIDDATA;
1687  }
1688 
1689  while (1) {
1690  token = av_strtok(NULL, ",", &saveptr);
1691  if (!token)
1692  break;
1693  if (rate = strtol(token, NULL, 10)) {
1694  if (nlayers >= 100) {
1695  return AVERROR_INVALIDDATA;
1696  }
1697  s->layer_rates[nlayers] = rate <= 1 ? 0:rate;
1698  nlayers++;
1699  } else {
1700  return AVERROR_INVALIDDATA;
1701  }
1702  }
1703 
1704  for (i = 1; i < nlayers; i++) {
1705  if (s->layer_rates[i] >= s->layer_rates[i-1]) {
1706  return AVERROR_INVALIDDATA;
1707  }
1708  }
1709  s->nlayers = nlayers;
1710  s->compression_rate_enc = 1;
1711  return 0;
1712 }
1713 
1715 {
1716  int i, ret;
1718  Jpeg2000CodingStyle *codsty = &s->codsty;
1719  Jpeg2000QuantStyle *qntsty = &s->qntsty;
1721 
1722  s->avctx = avctx;
1723  av_log(s->avctx, AV_LOG_DEBUG, "init\n");
1724  if (parse_layer_rates(s)) {
1725  av_log(s, AV_LOG_WARNING, "Layer rates invalid. Encoding with 1 layer based on quality metric.\n");
1726  s->nlayers = 1;
1727  s->layer_rates[0] = 0;
1728  s->compression_rate_enc = 0;
1729  }
1730 
1731 #if FF_API_PRIVATE_OPT
1733  if (avctx->prediction_method)
1734  s->pred = avctx->prediction_method;
1736 #endif
1737 
1738  if (avctx->pix_fmt == AV_PIX_FMT_PAL8 && (s->pred != FF_DWT97_INT || s->format != CODEC_JP2)) {
1739  av_log(s->avctx, AV_LOG_WARNING, "Forcing lossless jp2 for pal8\n");
1740  s->pred = 1;
1741  s->format = CODEC_JP2;
1742  }
1743 
1744  // defaults:
1745  // TODO: implement setting non-standard precinct size
1746  memset(codsty->log2_prec_widths , 15, sizeof(codsty->log2_prec_widths ));
1747  memset(codsty->log2_prec_heights, 15, sizeof(codsty->log2_prec_heights));
1748  codsty->nreslevels2decode=
1749  codsty->nreslevels = 7;
1750  codsty->nlayers = s->nlayers;
1751  codsty->log2_cblk_width = 4;
1752  codsty->log2_cblk_height = 4;
1753  codsty->transform = s->pred ? FF_DWT53 : FF_DWT97_INT;
1754 
1755  qntsty->nguardbits = 1;
1756 
1757  if ((s->tile_width & (s->tile_width -1)) ||
1758  (s->tile_height & (s->tile_height-1))) {
1759  av_log(avctx, AV_LOG_WARNING, "Tile dimension not a power of 2\n");
1760  }
1761 
1762  if (codsty->transform == FF_DWT53)
1763  qntsty->quantsty = JPEG2000_QSTY_NONE;
1764  else
1765  qntsty->quantsty = JPEG2000_QSTY_SE;
1766 
1767  s->width = avctx->width;
1768  s->height = avctx->height;
1769 
1770  s->ncomponents = desc->nb_components;
1771  for (i = 0; i < 3; i++) {
1772  s->cbps[i] = desc->comp[i].depth;
1773  }
1774 
1775  if ((desc->flags & AV_PIX_FMT_FLAG_PLANAR) && s->ncomponents > 1) {
1776  s->planar = 1;
1778  s->chroma_shift, s->chroma_shift + 1);
1779  if (ret)
1780  return ret;
1781  }
1782 
1785  init_luts();
1786 
1788  if ((ret=init_tiles(s)) < 0)
1789  return ret;
1790 
1791  av_log(s->avctx, AV_LOG_DEBUG, "after init\n");
1792 
1793  return 0;
1794 }
1795 
1796 static int j2kenc_destroy(AVCodecContext *avctx)
1797 {
1799 
1800  cleanup(s);
1801  return 0;
1802 }
1803 
1804 // taken from the libopenjpeg wraper so it matches
1805 
1806 #define OFFSET(x) offsetof(Jpeg2000EncoderContext, x)
1807 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1808 static const AVOption options[] = {
1809  { "format", "Codec Format", OFFSET(format), AV_OPT_TYPE_INT, { .i64 = CODEC_JP2 }, CODEC_J2K, CODEC_JP2, VE, "format" },
1810  { "j2k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CODEC_J2K }, 0, 0, VE, "format" },
1811  { "jp2", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CODEC_JP2 }, 0, 0, VE, "format" },
1812  { "tile_width", "Tile Width", OFFSET(tile_width), AV_OPT_TYPE_INT, { .i64 = 256 }, 1, 1<<30, VE, },
1813  { "tile_height", "Tile Height", OFFSET(tile_height), AV_OPT_TYPE_INT, { .i64 = 256 }, 1, 1<<30, VE, },
1814  { "pred", "DWT Type", OFFSET(pred), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE, "pred" },
1815  { "dwt97int", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, VE, "pred" },
1816  { "dwt53", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, VE, "pred" },
1817  { "sop", "SOP marker", OFFSET(sop), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE, },
1818  { "eph", "EPH marker", OFFSET(eph), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE, },
1819  { "prog", "Progression Order", OFFSET(prog), AV_OPT_TYPE_INT, { .i64 = 0 }, JPEG2000_PGOD_LRCP, JPEG2000_PGOD_CPRL, VE, "prog" },
1820  { "lrcp", NULL, OFFSET(prog), AV_OPT_TYPE_CONST, { .i64 = JPEG2000_PGOD_LRCP }, 0, 0, VE, "prog" },
1821  { "rlcp", NULL, OFFSET(prog), AV_OPT_TYPE_CONST, { .i64 = JPEG2000_PGOD_RLCP }, 0, 0, VE, "prog" },
1822  { "rpcl", NULL, OFFSET(prog), AV_OPT_TYPE_CONST, { .i64 = JPEG2000_PGOD_RPCL }, 0, 0, VE, "prog" },
1823  { "pcrl", NULL, OFFSET(prog), AV_OPT_TYPE_CONST, { .i64 = JPEG2000_PGOD_PCRL }, 0, 0, VE, "prog" },
1824  { "cprl", NULL, OFFSET(prog), AV_OPT_TYPE_CONST, { .i64 = JPEG2000_PGOD_CPRL }, 0, 0, VE, "prog" },
1825  { "layer_rates", "Layer Rates", OFFSET(lr_str), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, VE },
1826  { NULL }
1827 };
1828 
1829 static const AVClass j2k_class = {
1830  .class_name = "jpeg 2000 encoder",
1831  .item_name = av_default_item_name,
1832  .option = options,
1833  .version = LIBAVUTIL_VERSION_INT,
1834 };
1835 
1837  .name = "jpeg2000",
1838  .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
1839  .type = AVMEDIA_TYPE_VIDEO,
1840  .id = AV_CODEC_ID_JPEG2000,
1841  .priv_data_size = sizeof(Jpeg2000EncoderContext),
1842  .init = j2kenc_init,
1843  .encode2 = encode_frame,
1844  .close = j2kenc_destroy,
1845  .pix_fmts = (const enum AVPixelFormat[]) {
1852  },
1853  .priv_class = &j2k_class,
1854  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
1855 };
static double val(void *priv, double ch)
Definition: aeval.c:76
static const char *const format[]
Definition: af_aiir.c:456
#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:404
#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:203
#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:33
@ 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:453
static int put_com(Jpeg2000EncoderContext *s, int compno)
Definition: j2kenc.c:385
static int parse_layer_rates(Jpeg2000EncoderContext *s)
Definition: j2kenc.c:1667
static uint8_t * put_sot(Jpeg2000EncoderContext *s, int tileno)
Definition: j2kenc.c:404
#define WMSEDEC_SHIFT
must be >= 13
Definition: j2kenc.c:80
static void update_size(uint8_t *size, const uint8_t *end)
Definition: j2kenc.c:1522
static void makelayers(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile)
Definition: j2kenc.c:1264
AVCodec ff_jpeg2000_encoder
Definition: j2kenc.c:1836
static void makelayer(Jpeg2000EncoderContext *s, int layno, double thresh, Jpeg2000Tile *tile, int final)
Definition: j2kenc.c:1181
#define NMSEDEC_FRACBITS
Definition: j2kenc.c:79
static void truncpasses(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile)
Definition: j2kenc.c:1367
static void init_luts(void)
Definition: j2kenc.c:579
static const AVOption options[]
Definition: j2kenc.c:1808
static void putnumpasses(Jpeg2000EncoderContext *s, int n)
Definition: j2kenc.c:764
static int encode_tile(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno)
Definition: j2kenc.c:1401
#define VE
Definition: j2kenc.c:1807
static int encode_packet(Jpeg2000EncoderContext *s, Jpeg2000ResLevel *rlevel, int layno, int precno, uint8_t *expn, int numgbits, int packetno, int nlayers)
Definition: j2kenc.c:779
static void encode_refpass(Jpeg2000T1Context *t1, int width, int height, int *nmsedec, int bpno)
Definition: j2kenc.c:633
static void reinit(Jpeg2000EncoderContext *s)
Definition: j2kenc.c:1512
static int encode_packets(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno, int nlayers)
Definition: j2kenc.c:935
static av_cold int j2kenc_init(AVCodecContext *avctx)
Definition: j2kenc.c:1714
static int j2kenc_destroy(AVCodecContext *avctx)
Definition: j2kenc.c:1796
static void encode_sigpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
Definition: j2kenc.c:611
static void init_quantization(Jpeg2000EncoderContext *s)
Definition: j2kenc.c:549
static int lut_nmsedec_sig0[1<< NMSEDEC_BITS]
Definition: j2kenc.c:89
static void compute_rates(Jpeg2000EncoderContext *s)
Definition: j2kenc.c:423
static void tag_tree_update(Jpeg2000TgtNode *node)
update the value in node
Definition: j2kenc.c:286
static void cleanup(Jpeg2000EncoderContext *s)
Definition: j2kenc.c:1492
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:1527
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:702
static int lut_nmsedec_ref[1<< NMSEDEC_BITS]
Definition: j2kenc.c:86
static int put_qcd(Jpeg2000EncoderContext *s, int compno)
Definition: j2kenc.c:359
static int getnmsedec_sig(int x, int bpno)
Definition: j2kenc.c:597
static int getnmsedec_ref(int x, int bpno)
Definition: j2kenc.c:604
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:249
static int getcut(Jpeg2000Cblk *cblk, int64_t lambda, int dwt_norm)
Definition: j2kenc.c:1349
static const int dwt_norms[2][4][10]
Definition: j2kenc.c:91
static const AVClass j2k_class
Definition: j2kenc.c:1829
#define OFFSET(x)
Definition: j2kenc.c:1806
static void encode_clnpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
Definition: j2kenc.c:647
static int put_siz(Jpeg2000EncoderContext *s)
Definition: j2kenc.c:298
#define CODEC_J2K
Definition: j2kenc.c:84
#define COPY_FRAME(D, PIXEL)
Definition: j2kenc.c:505
static int put_cod(Jpeg2000EncoderContext *s)
Definition: j2kenc.c:327
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:79
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