FFmpeg
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 "encode.h"
70 #include "internal.h"
71 #include "bytestream.h"
72 #include "jpeg2000.h"
73 #include "libavutil/common.h"
74 #include "libavutil/pixdesc.h"
75 #include "libavutil/opt.h"
76 #include "libavutil/intreadwrite.h"
77 #include "libavutil/avstring.h"
78 #include "libavutil/thread.h"
79 
80 #define NMSEDEC_BITS 7
81 #define NMSEDEC_FRACBITS (NMSEDEC_BITS-1)
82 #define WMSEDEC_SHIFT 13 ///< must be >= 13
83 #define LAMBDA_SCALE (100000000LL << (WMSEDEC_SHIFT - 13))
84 
85 #define CODEC_JP2 1
86 #define CODEC_J2K 0
87 
88 static int lut_nmsedec_ref [1<<NMSEDEC_BITS],
92 
93 static const int dwt_norms[2][4][10] = { // [dwt_type][band][rlevel] (multiplied by 10000)
94  {{10000, 19650, 41770, 84030, 169000, 338400, 676900, 1353000, 2706000, 5409000},
95  {20220, 39890, 83550, 170400, 342700, 686300, 1373000, 2746000, 5490000},
96  {20220, 39890, 83550, 170400, 342700, 686300, 1373000, 2746000, 5490000},
97  {20800, 38650, 83070, 171800, 347100, 695900, 1393000, 2786000, 5572000}},
98 
99  {{10000, 15000, 27500, 53750, 106800, 213400, 426700, 853300, 1707000, 3413000},
100  {10380, 15920, 29190, 57030, 113300, 226400, 452500, 904800, 1809000},
101  {10380, 15920, 29190, 57030, 113300, 226400, 452500, 904800, 1809000},
102  { 7186, 9218, 15860, 30430, 60190, 120100, 240000, 479700, 959300}}
103 };
104 
105 typedef struct {
107  double *layer_rates;
108 } Jpeg2000Tile;
109 
110 typedef struct {
111  AVClass *class;
113  const AVFrame *picture;
114 
115  int width, height; ///< image width and height
116  uint8_t cbps[4]; ///< bits per sample in particular components
117  int chroma_shift[2];
118  uint8_t planar;
120  int tile_width, tile_height; ///< tile size
121  int numXtiles, numYtiles;
122 
123  uint8_t *buf_start;
124  uint8_t *buf;
125  uint8_t *buf_end;
127 
128  int64_t lambda;
129 
132 
134  int layer_rates[100];
135  uint8_t compression_rate_enc; ///< Is compression done using compression ratio?
136 
137  int format;
138  int pred;
139  int sop;
140  int eph;
141  int prog;
142  int nlayers;
143  char *lr_str;
145 
146 
147 /* debug */
148 #if 0
149 #undef ifprintf
150 #undef printf
151 
152 static void nspaces(FILE *fd, int n)
153 {
154  while(n--) putc(' ', fd);
155 }
156 
157 static void printcomp(Jpeg2000Component *comp)
158 {
159  int i;
160  for (i = 0; i < comp->y1 - comp->y0; i++)
161  ff_jpeg2000_printv(comp->i_data + i * (comp->x1 - comp->x0), comp->x1 - comp->x0);
162 }
163 
164 static void dump(Jpeg2000EncoderContext *s, FILE *fd)
165 {
166  int tileno, compno, reslevelno, bandno, precno;
167  fprintf(fd, "XSiz = %d, YSiz = %d, tile_width = %d, tile_height = %d\n"
168  "numXtiles = %d, numYtiles = %d, ncomponents = %d\n"
169  "tiles:\n",
170  s->width, s->height, s->tile_width, s->tile_height,
171  s->numXtiles, s->numYtiles, s->ncomponents);
172  for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
173  Jpeg2000Tile *tile = s->tile + tileno;
174  nspaces(fd, 2);
175  fprintf(fd, "tile %d:\n", tileno);
176  for(compno = 0; compno < s->ncomponents; compno++){
177  Jpeg2000Component *comp = tile->comp + compno;
178  nspaces(fd, 4);
179  fprintf(fd, "component %d:\n", compno);
180  nspaces(fd, 4);
181  fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d\n",
182  comp->x0, comp->x1, comp->y0, comp->y1);
183  for(reslevelno = 0; reslevelno < s->nreslevels; reslevelno++){
184  Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
185  nspaces(fd, 6);
186  fprintf(fd, "reslevel %d:\n", reslevelno);
187  nspaces(fd, 6);
188  fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d, nbands = %d\n",
189  reslevel->x0, reslevel->x1, reslevel->y0,
190  reslevel->y1, reslevel->nbands);
191  for(bandno = 0; bandno < reslevel->nbands; bandno++){
192  Jpeg2000Band *band = reslevel->band + bandno;
193  nspaces(fd, 8);
194  fprintf(fd, "band %d:\n", bandno);
195  nspaces(fd, 8);
196  fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d,"
197  "codeblock_width = %d, codeblock_height = %d cblknx = %d cblkny = %d\n",
198  band->x0, band->x1,
199  band->y0, band->y1,
200  band->codeblock_width, band->codeblock_height,
201  band->cblknx, band->cblkny);
202  for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
203  Jpeg2000Prec *prec = band->prec + precno;
204  nspaces(fd, 10);
205  fprintf(fd, "prec %d:\n", precno);
206  nspaces(fd, 10);
207  fprintf(fd, "xi0 = %d, xi1 = %d, yi0 = %d, yi1 = %d\n",
208  prec->xi0, prec->xi1, prec->yi0, prec->yi1);
209  }
210  }
211  }
212  }
213  }
214 }
215 #endif
216 
217 /* bitstream routines */
218 
219 /** put n times val bit */
220 static void put_bits(Jpeg2000EncoderContext *s, int val, int n) // TODO: optimize
221 {
222  while (n-- > 0){
223  if (s->bit_index == 8)
224  {
225  s->bit_index = *s->buf == 0xff;
226  *(++s->buf) = 0;
227  }
228  *s->buf |= val << (7 - s->bit_index++);
229  }
230 }
231 
232 /** put n least significant bits of a number num */
233 static void put_num(Jpeg2000EncoderContext *s, int num, int n)
234 {
235  while(--n >= 0)
236  put_bits(s, (num >> n) & 1, 1);
237 }
238 
239 /** flush the bitstream */
241 {
242  if (s->bit_index){
243  s->bit_index = 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 
406 static uint8_t *put_sot(Jpeg2000EncoderContext *s, int tileno)
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_calloc(s->ncomponents, sizeof(*tile->comp));
472  if (!tile->comp)
473  return AVERROR(ENOMEM);
474 
475  tile->layer_rates = av_calloc(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 
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 
548 COPY_FRAME(8, uint8_t)
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  }
597 }
598 
599 /* tier-1 routines */
600 static int getnmsedec_sig(int x, int bpno)
601 {
602  if (bpno > NMSEDEC_FRACBITS)
603  return lut_nmsedec_sig[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
604  return lut_nmsedec_sig0[x & ((1 << NMSEDEC_BITS) - 1)];
605 }
606 
607 static int getnmsedec_ref(int x, int bpno)
608 {
609  if (bpno > NMSEDEC_FRACBITS)
610  return lut_nmsedec_ref[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
611  return lut_nmsedec_ref0[x & ((1 << NMSEDEC_BITS) - 1)];
612 }
613 
614 static void encode_sigpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
615 {
616  int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
617  for (y0 = 0; y0 < height; y0 += 4)
618  for (x = 0; x < width; x++)
619  for (y = y0; y < height && y < y0+4; y++){
620  if (!(t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG) && (t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG_NB)){
621  int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno),
622  bit = t1->data[(y) * t1->stride + x] & mask ? 1 : 0;
623  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, bit);
624  if (bit){
625  int xorbit;
626  int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
627  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
628  *nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
629  ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
630  }
631  t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_VIS;
632  }
633  }
634 }
635 
636 static void encode_refpass(Jpeg2000T1Context *t1, int width, int height, int *nmsedec, int bpno)
637 {
638  int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
639  for (y0 = 0; y0 < height; y0 += 4)
640  for (x = 0; x < width; x++)
641  for (y = y0; y < height && y < y0+4; y++)
642  if ((t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG){
643  int ctxno = ff_jpeg2000_getrefctxno(t1->flags[(y+1) * t1->stride + x+1]);
644  *nmsedec += getnmsedec_ref(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
645  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
646  t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_REF;
647  }
648 }
649 
650 static void encode_clnpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
651 {
652  int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
653  for (y0 = 0; y0 < height; y0 += 4)
654  for (x = 0; x < width; x++){
655  if (y0 + 3 < height && !(
656  (t1->flags[(y0+1) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
657  (t1->flags[(y0+2) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
658  (t1->flags[(y0+3) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
659  (t1->flags[(y0+4) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG))))
660  {
661  // aggregation mode
662  int rlen;
663  for (rlen = 0; rlen < 4; rlen++)
664  if (t1->data[(y0+rlen) * t1->stride + x] & mask)
665  break;
666  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL, rlen != 4);
667  if (rlen == 4)
668  continue;
669  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen >> 1);
670  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen & 1);
671  for (y = y0 + rlen; y < y0 + 4; y++){
672  if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))){
673  int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno);
674  if (y > y0 + rlen)
675  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
676  if (t1->data[(y) * t1->stride + x] & mask){ // newly significant
677  int xorbit;
678  int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
679  *nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
680  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
681  ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
682  }
683  }
684  t1->flags[(y+1) * t1->stride + x+1] &= ~JPEG2000_T1_VIS;
685  }
686  } else{
687  for (y = y0; y < y0 + 4 && y < height; y++){
688  if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))){
689  int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno);
690  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
691  if (t1->data[(y) * t1->stride + x] & mask){ // newly significant
692  int xorbit;
693  int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
694  *nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
695  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
696  ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
697  }
698  }
699  t1->flags[(y+1) * t1->stride + x+1] &= ~JPEG2000_T1_VIS;
700  }
701  }
702  }
703 }
704 
706  int width, int height, int bandpos, int lev)
707 {
708  int pass_t = 2, passno, x, y, max=0, nmsedec, bpno;
709  int64_t wmsedec = 0;
710 
711  memset(t1->flags, 0, t1->stride * (height + 2) * sizeof(*t1->flags));
712 
713  for (y = 0; y < height; y++){
714  for (x = 0; x < width; x++){
715  if (t1->data[(y) * t1->stride + x] < 0){
716  t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_SGN;
717  t1->data[(y) * t1->stride + x] = -t1->data[(y) * t1->stride + x];
718  }
719  max = FFMAX(max, t1->data[(y) * t1->stride + x]);
720  }
721  }
722 
723  if (max == 0){
724  cblk->nonzerobits = 0;
725  bpno = 0;
726  } else{
727  cblk->nonzerobits = av_log2(max) + 1 - NMSEDEC_FRACBITS;
728  bpno = cblk->nonzerobits - 1;
729  }
730 
731  cblk->data[0] = 0;
732  ff_mqc_initenc(&t1->mqc, cblk->data + 1);
733 
734  for (passno = 0; bpno >= 0; passno++){
735  nmsedec=0;
736 
737  switch(pass_t){
738  case 0: encode_sigpass(t1, width, height, bandpos, &nmsedec, bpno);
739  break;
740  case 1: encode_refpass(t1, width, height, &nmsedec, bpno);
741  break;
742  case 2: encode_clnpass(t1, width, height, bandpos, &nmsedec, bpno);
743  break;
744  }
745 
746  cblk->passes[passno].rate = ff_mqc_flush_to(&t1->mqc, cblk->passes[passno].flushed, &cblk->passes[passno].flushed_len);
747  cblk->passes[passno].rate -= cblk->passes[passno].flushed_len;
748 
749  wmsedec += (int64_t)nmsedec << (2*bpno);
750  cblk->passes[passno].disto = wmsedec;
751 
752  if (++pass_t == 3){
753  pass_t = 0;
754  bpno--;
755  }
756  }
757  cblk->npasses = passno;
758  cblk->ninclpasses = passno;
759 
760  if (passno) {
761  cblk->passes[passno-1].rate = ff_mqc_flush_to(&t1->mqc, cblk->passes[passno-1].flushed, &cblk->passes[passno-1].flushed_len);
762  cblk->passes[passno-1].rate -= cblk->passes[passno-1].flushed_len;
763  }
764 }
765 
766 /* tier-2 routines: */
767 
769 {
770  if (n == 1)
771  put_num(s, 0, 1);
772  else if (n == 2)
773  put_num(s, 2, 2);
774  else if (n <= 5)
775  put_num(s, 0xc | (n-3), 4);
776  else if (n <= 36)
777  put_num(s, 0x1e0 | (n-6), 9);
778  else
779  put_num(s, 0xff80 | (n-37), 16);
780 }
781 
782 
783 static int encode_packet(Jpeg2000EncoderContext *s, Jpeg2000ResLevel *rlevel, int layno,
784  int precno, uint8_t *expn, int numgbits, int packetno,
785  int nlayers)
786 {
787  int bandno, empty = 1;
788  int i;
789  // init bitstream
790  *s->buf = 0;
791  s->bit_index = 0;
792 
793  if (s->sop) {
794  bytestream_put_be16(&s->buf, JPEG2000_SOP);
795  bytestream_put_be16(&s->buf, 4);
796  bytestream_put_be16(&s->buf, packetno);
797  }
798  // header
799 
800  if (!layno) {
801  for (bandno = 0; bandno < rlevel->nbands; bandno++) {
802  Jpeg2000Band *band = rlevel->band + bandno;
803  if (band->coord[0][0] < band->coord[0][1]
804  && band->coord[1][0] < band->coord[1][1]) {
805  Jpeg2000Prec *prec = band->prec + precno;
806  int nb_cblks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
807  int pos;
810  for (pos = 0; pos < nb_cblks; pos++) {
811  Jpeg2000Cblk *cblk = &prec->cblk[pos];
812  prec->zerobits[pos].val = expn[bandno] + numgbits - 1 - cblk->nonzerobits;
813  cblk->incl = 0;
814  cblk->lblock = 3;
815  tag_tree_update(prec->zerobits + pos);
816  for (i = 0; i < nlayers; i++) {
817  if (cblk->layers[i].npasses > 0) {
818  prec->cblkincl[pos].val = i;
819  break;
820  }
821  }
822  if (i == nlayers)
823  prec->cblkincl[pos].val = i;
824  tag_tree_update(prec->cblkincl + pos);
825  }
826  }
827  }
828  }
829 
830  // is the packet empty?
831  for (bandno = 0; bandno < rlevel->nbands; bandno++){
832  Jpeg2000Band *band = rlevel->band + bandno;
833  if (band->coord[0][0] < band->coord[0][1]
834  && band->coord[1][0] < band->coord[1][1]) {
835  Jpeg2000Prec *prec = band->prec + precno;
836  int nb_cblks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
837  int pos;
838  for (pos = 0; pos < nb_cblks; pos++) {
839  Jpeg2000Cblk *cblk = &prec->cblk[pos];
840  if (cblk->layers[layno].npasses) {
841  empty = 0;
842  break;
843  }
844  }
845  if (!empty)
846  break;
847  }
848  }
849 
850  put_bits(s, !empty, 1);
851  if (empty){
852  j2k_flush(s);
853  if (s->eph)
854  bytestream_put_be16(&s->buf, JPEG2000_EPH);
855  return 0;
856  }
857 
858  for (bandno = 0; bandno < rlevel->nbands; bandno++) {
859  Jpeg2000Band *band = rlevel->band + bandno;
860  Jpeg2000Prec *prec = band->prec + precno;
861  int yi, xi, pos;
862  int cblknw = prec->nb_codeblocks_width;
863 
864  if (band->coord[0][0] == band->coord[0][1]
865  || band->coord[1][0] == band->coord[1][1])
866  continue;
867 
868  for (pos=0, yi = 0; yi < prec->nb_codeblocks_height; yi++) {
869  for (xi = 0; xi < cblknw; xi++, pos++){
870  int llen = 0, length;
871  Jpeg2000Cblk *cblk = prec->cblk + yi * cblknw + xi;
872 
873  if (s->buf_end - s->buf < 20) // approximately
874  return -1;
875 
876  // inclusion information
877  if (!cblk->incl)
878  tag_tree_code(s, prec->cblkincl + pos, layno + 1);
879  else {
880  put_bits(s, cblk->layers[layno].npasses > 0, 1);
881  }
882 
883  if (!cblk->layers[layno].npasses)
884  continue;
885 
886  // zerobits information
887  if (!cblk->incl) {
888  tag_tree_code(s, prec->zerobits + pos, 100);
889  cblk->incl = 1;
890  }
891 
892  // number of passes
893  putnumpasses(s, cblk->layers[layno].npasses);
894 
895  length = cblk->layers[layno].data_len;
896  if (layno == nlayers - 1 && cblk->layers[layno].cum_passes){
897  length += cblk->passes[cblk->layers[layno].cum_passes-1].flushed_len;
898  }
899  if (cblk->lblock + av_log2(cblk->layers[layno].npasses) < av_log2(length) + 1) {
900  llen = av_log2(length) + 1 - cblk->lblock - av_log2(cblk->layers[layno].npasses);
901  }
902 
903  // length of code block
904  cblk->lblock += llen;
905  put_bits(s, 1, llen);
906  put_bits(s, 0, 1);
907  put_num(s, length, cblk->lblock + av_log2(cblk->layers[layno].npasses));
908  }
909  }
910  }
911  j2k_flush(s);
912  if (s->eph) {
913  bytestream_put_be16(&s->buf, JPEG2000_EPH);
914  }
915 
916  for (bandno = 0; bandno < rlevel->nbands; bandno++) {
917  Jpeg2000Band *band = rlevel->band + bandno;
918  Jpeg2000Prec *prec = band->prec + precno;
919  int yi, cblknw = prec->nb_codeblocks_width;
920  for (yi =0; yi < prec->nb_codeblocks_height; yi++) {
921  int xi;
922  for (xi = 0; xi < cblknw; xi++){
923  Jpeg2000Cblk *cblk = prec->cblk + yi * cblknw + xi;
924  if (cblk->layers[layno].npasses) {
925  if (s->buf_end - s->buf < cblk->layers[layno].data_len + 2)
926  return -1;
927  bytestream_put_buffer(&s->buf, cblk->layers[layno].data_start + 1, cblk->layers[layno].data_len);
928  if (layno == nlayers - 1 && cblk->layers[layno].cum_passes) {
929  bytestream_put_buffer(&s->buf, cblk->passes[cblk->layers[layno].cum_passes-1].flushed,
930  cblk->passes[cblk->layers[layno].cum_passes-1].flushed_len);
931  }
932  }
933  }
934  }
935  }
936  return 0;
937 }
938 
939 static int encode_packets(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno, int nlayers)
940 {
941  int compno, reslevelno, layno, ret;
942  Jpeg2000CodingStyle *codsty = &s->codsty;
943  Jpeg2000QuantStyle *qntsty = &s->qntsty;
944  int packetno = 0;
945  int step_x, step_y;
946  int x, y;
947  int tile_coord[2][2];
948  int col = tileno % s->numXtiles;
949  int row = tileno / s->numXtiles;
950 
951  tile_coord[0][0] = col * s->tile_width;
952  tile_coord[0][1] = FFMIN(tile_coord[0][0] + s->tile_width, s->width);
953  tile_coord[1][0] = row * s->tile_height;
954  tile_coord[1][1] = FFMIN(tile_coord[1][0] + s->tile_height, s->height);
955 
956  av_log(s->avctx, AV_LOG_DEBUG, "tier2\n");
957  // lay-rlevel-comp-pos progression
958  switch (s->prog) {
959  case JPEG2000_PGOD_LRCP:
960  for (layno = 0; layno < nlayers; layno++) {
961  for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
962  for (compno = 0; compno < s->ncomponents; compno++){
963  int precno;
964  Jpeg2000ResLevel *reslevel = s->tile[tileno].comp[compno].reslevel + reslevelno;
965  for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
966  if ((ret = encode_packet(s, reslevel, layno, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
967  qntsty->nguardbits, packetno++, nlayers)) < 0)
968  return ret;
969  }
970  }
971  }
972  }
973  break;
974  case JPEG2000_PGOD_RLCP:
975  for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
976  for (layno = 0; layno < nlayers; layno++) {
977  for (compno = 0; compno < s->ncomponents; compno++){
978  int precno;
979  Jpeg2000ResLevel *reslevel = s->tile[tileno].comp[compno].reslevel + reslevelno;
980  for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
981  if ((ret = encode_packet(s, reslevel, layno, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
982  qntsty->nguardbits, packetno++, nlayers)) < 0)
983  return ret;
984  }
985  }
986  }
987  }
988  break;
989  case JPEG2000_PGOD_RPCL:
990  for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
991  int precno;
992  step_x = 30;
993  step_y = 30;
994  for (compno = 0; compno < s->ncomponents; compno++) {
995  Jpeg2000Component *comp = tile->comp + compno;
996  if (reslevelno < codsty->nreslevels) {
997  uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
998  Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
999  step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
1000  step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1001  }
1002  }
1003 
1004  step_x = 1<<step_x;
1005  step_y = 1<<step_y;
1006  for (y = tile_coord[1][0]; y < tile_coord[1][1]; y = (y/step_y + 1)*step_y) {
1007  for (x = tile_coord[0][0]; x < tile_coord[0][1]; x = (x/step_x + 1)*step_x) {
1008  for (compno = 0; compno < s->ncomponents; compno++) {
1009  Jpeg2000Component *comp = tile->comp + compno;
1010  uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1011  Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
1012  int log_subsampling[2] = { compno?s->chroma_shift[0]:0, compno?s->chroma_shift[1]:0};
1013  unsigned prcx, prcy;
1014  int trx0, try0;
1015 
1016  trx0 = ff_jpeg2000_ceildivpow2(tile_coord[0][0], log_subsampling[0] + reducedresno);
1017  try0 = ff_jpeg2000_ceildivpow2(tile_coord[1][0], log_subsampling[1] + reducedresno);
1018 
1019  if (!(y % ((uint64_t)1 << (reslevel->log2_prec_height + reducedresno + log_subsampling[1])) == 0 ||
1020  (y == tile_coord[1][0] && (try0 << reducedresno) % (1U << (reducedresno + reslevel->log2_prec_height)))))
1021  continue;
1022 
1023  if (!(x % ((uint64_t)1 << (reslevel->log2_prec_width + reducedresno + log_subsampling[0])) == 0 ||
1024  (x == tile_coord[0][0] && (trx0 << reducedresno) % (1U << (reducedresno + reslevel->log2_prec_width)))))
1025  continue;
1026 
1027  // check if a precinct exists
1028  prcx = ff_jpeg2000_ceildivpow2(x, log_subsampling[0] + reducedresno) >> reslevel->log2_prec_width;
1029  prcy = ff_jpeg2000_ceildivpow2(y, log_subsampling[1] + reducedresno) >> reslevel->log2_prec_height;
1030  prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> reslevel->log2_prec_width;
1031  prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> reslevel->log2_prec_height;
1032  precno = prcx + reslevel->num_precincts_x * prcy;
1033 
1034  if (prcx >= reslevel->num_precincts_x || prcy >= reslevel->num_precincts_y) {
1035  av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1036  prcx, prcy, reslevel->num_precincts_x, reslevel->num_precincts_y);
1037  continue;
1038  }
1039  for (layno = 0; layno < nlayers; layno++) {
1040  if ((ret = encode_packet(s, reslevel, layno, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
1041  qntsty->nguardbits, packetno++, nlayers)) < 0)
1042  return ret;
1043  }
1044  }
1045  }
1046  }
1047  }
1048  break;
1049  case JPEG2000_PGOD_PCRL:
1050  step_x = 32;
1051  step_y = 32;
1052  for (compno = 0; compno < s->ncomponents; compno++) {
1053  Jpeg2000Component *comp = tile->comp + compno;
1054 
1055  for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
1056  uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1057  Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1058  step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
1059  step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1060  }
1061  }
1062  if (step_x >= 31 || step_y >= 31){
1063  avpriv_request_sample(s->avctx, "PCRL with large step");
1064  return AVERROR_PATCHWELCOME;
1065  }
1066  step_x = 1<<step_x;
1067  step_y = 1<<step_y;
1068 
1069  for (y = tile_coord[1][0]; y < tile_coord[1][1]; y = (y/step_y + 1)*step_y) {
1070  for (x = tile_coord[0][0]; x < tile_coord[0][1]; x = (x/step_x + 1)*step_x) {
1071  for (compno = 0; compno < s->ncomponents; compno++) {
1072  Jpeg2000Component *comp = tile->comp + compno;
1073  int log_subsampling[2] = { compno?s->chroma_shift[0]:0, compno?s->chroma_shift[1]:0};
1074 
1075  for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
1076  unsigned prcx, prcy;
1077  int precno;
1078  uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1079  Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
1080  int trx0, try0;
1081 
1082  trx0 = ff_jpeg2000_ceildivpow2(tile_coord[0][0], log_subsampling[0] + reducedresno);
1083  try0 = ff_jpeg2000_ceildivpow2(tile_coord[1][0], log_subsampling[1] + reducedresno);
1084 
1085  if (!(y % ((uint64_t)1 << (reslevel->log2_prec_height + reducedresno + log_subsampling[1])) == 0 ||
1086  (y == tile_coord[1][0] && (try0 << reducedresno) % (1U << (reducedresno + reslevel->log2_prec_height)))))
1087  continue;
1088 
1089  if (!(x % ((uint64_t)1 << (reslevel->log2_prec_width + reducedresno + log_subsampling[0])) == 0 ||
1090  (x == tile_coord[0][0] && (trx0 << reducedresno) % (1U << (reducedresno + reslevel->log2_prec_width)))))
1091  continue;
1092 
1093  // check if a precinct exists
1094  prcx = ff_jpeg2000_ceildivpow2(x, log_subsampling[0] + reducedresno) >> reslevel->log2_prec_width;
1095  prcy = ff_jpeg2000_ceildivpow2(y, log_subsampling[1] + reducedresno) >> reslevel->log2_prec_height;
1096  prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> reslevel->log2_prec_width;
1097  prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> reslevel->log2_prec_height;
1098 
1099  precno = prcx + reslevel->num_precincts_x * prcy;
1100 
1101  if (prcx >= reslevel->num_precincts_x || prcy >= reslevel->num_precincts_y) {
1102  av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1103  prcx, prcy, reslevel->num_precincts_x, reslevel->num_precincts_y);
1104  continue;
1105  }
1106  for (layno = 0; layno < nlayers; layno++) {
1107  if ((ret = encode_packet(s, reslevel, layno, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
1108  qntsty->nguardbits, packetno++, nlayers)) < 0)
1109  return ret;
1110  }
1111  }
1112  }
1113  }
1114  }
1115  break;
1116  case JPEG2000_PGOD_CPRL:
1117  for (compno = 0; compno < s->ncomponents; compno++) {
1118  Jpeg2000Component *comp = tile->comp + compno;
1119  int log_subsampling[2] = { compno?s->chroma_shift[0]:0, compno?s->chroma_shift[1]:0};
1120  step_x = 32;
1121  step_y = 32;
1122 
1123  for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
1124  uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1125  Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1126  step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
1127  step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1128  }
1129  if (step_x >= 31 || step_y >= 31){
1130  avpriv_request_sample(s->avctx, "CPRL with large step");
1131  return AVERROR_PATCHWELCOME;
1132  }
1133  step_x = 1<<step_x;
1134  step_y = 1<<step_y;
1135 
1136  for (y = tile_coord[1][0]; y < tile_coord[1][1]; y = (y/step_y + 1)*step_y) {
1137  for (x = tile_coord[0][0]; x < tile_coord[0][1]; x = (x/step_x + 1)*step_x) {
1138  for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
1139  unsigned prcx, prcy;
1140  int precno;
1141  int trx0, try0;
1142  uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1143  Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
1144 
1145  trx0 = ff_jpeg2000_ceildivpow2(tile_coord[0][0], log_subsampling[0] + reducedresno);
1146  try0 = ff_jpeg2000_ceildivpow2(tile_coord[1][0], log_subsampling[1] + reducedresno);
1147 
1148  if (!(y % ((uint64_t)1 << (reslevel->log2_prec_height + reducedresno + log_subsampling[1])) == 0 ||
1149  (y == tile_coord[1][0] && (try0 << reducedresno) % (1U << (reducedresno + reslevel->log2_prec_height)))))
1150  continue;
1151 
1152  if (!(x % ((uint64_t)1 << (reslevel->log2_prec_width + reducedresno + log_subsampling[0])) == 0 ||
1153  (x == tile_coord[0][0] && (trx0 << reducedresno) % (1U << (reducedresno + reslevel->log2_prec_width)))))
1154  continue;
1155 
1156  // check if a precinct exists
1157  prcx = ff_jpeg2000_ceildivpow2(x, log_subsampling[0] + reducedresno) >> reslevel->log2_prec_width;
1158  prcy = ff_jpeg2000_ceildivpow2(y, log_subsampling[1] + reducedresno) >> reslevel->log2_prec_height;
1159  prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> reslevel->log2_prec_width;
1160  prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> reslevel->log2_prec_height;
1161 
1162  precno = prcx + reslevel->num_precincts_x * prcy;
1163 
1164  if (prcx >= reslevel->num_precincts_x || prcy >= reslevel->num_precincts_y) {
1165  av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1166  prcx, prcy, reslevel->num_precincts_x, reslevel->num_precincts_y);
1167  continue;
1168  }
1169  for (layno = 0; layno < nlayers; layno++) {
1170  if ((ret = encode_packet(s, reslevel, layno, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
1171  qntsty->nguardbits, packetno++, nlayers)) < 0)
1172  return ret;
1173  }
1174  }
1175  }
1176  }
1177  }
1178 
1179  }
1180 
1181  av_log(s->avctx, AV_LOG_DEBUG, "after tier2\n");
1182  return 0;
1183 }
1184 
1185 static void makelayer(Jpeg2000EncoderContext *s, int layno, double thresh, Jpeg2000Tile* tile, int final)
1186 {
1187  int compno, resno, bandno, precno, cblkno;
1188  int passno;
1189 
1190  for (compno = 0; compno < s->ncomponents; compno++) {
1191  Jpeg2000Component *comp = &tile->comp[compno];
1192 
1193  for (resno = 0; resno < s->codsty.nreslevels; resno++) {
1194  Jpeg2000ResLevel *reslevel = comp->reslevel + resno;
1195 
1196  for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
1197  for (bandno = 0; bandno < reslevel->nbands ; bandno++){
1198  Jpeg2000Band *band = reslevel->band + bandno;
1199  Jpeg2000Prec *prec = band->prec + precno;
1200 
1201  for (cblkno = 0; cblkno < prec->nb_codeblocks_height * prec->nb_codeblocks_width; cblkno++){
1202  Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1203  Jpeg2000Layer *layer = &cblk->layers[layno];
1204  int n;
1205 
1206  if (layno == 0) {
1207  cblk->ninclpasses = 0;
1208  }
1209 
1210  n = cblk->ninclpasses;
1211 
1212  if (thresh < 0) {
1213  n = cblk->npasses;
1214  } else {
1215  for (passno = cblk->ninclpasses; passno < cblk->npasses; passno++) {
1216  int32_t dr;
1217  double dd;
1218  Jpeg2000Pass *pass = &cblk->passes[passno];
1219 
1220  if (n == 0) {
1221  dr = pass->rate;
1222  dd = pass->disto;
1223  } else {
1224  dr = pass->rate - cblk->passes[n - 1].rate;
1225  dd = pass->disto - cblk->passes[n-1].disto;
1226  }
1227 
1228  if (!dr) {
1229  if (dd != 0.0) {
1230  n = passno + 1;
1231  }
1232  continue;
1233  }
1234 
1235  if (thresh - (dd / dr) < DBL_EPSILON)
1236  n = passno + 1;
1237  }
1238  }
1239  layer->npasses = n - cblk->ninclpasses;
1240  layer->cum_passes = n;
1241 
1242  if (layer->npasses == 0) {
1243  layer->disto = 0;
1244  layer->data_len = 0;
1245  continue;
1246  }
1247 
1248  if (cblk->ninclpasses == 0) {
1249  layer->data_len = cblk->passes[n - 1].rate;
1250  layer->data_start = cblk->data;
1251  layer->disto = cblk->passes[n - 1].disto;
1252  } else {
1253  layer->data_len = cblk->passes[n - 1].rate - cblk->passes[cblk->ninclpasses - 1].rate;
1254  layer->data_start = cblk->data + cblk->passes[cblk->ninclpasses - 1].rate;
1255  layer->disto = cblk->passes[n - 1].disto -
1256  cblk->passes[cblk->ninclpasses - 1].disto;
1257  }
1258  if (final) {
1259  cblk->ninclpasses = n;
1260  }
1261  }
1262  }
1263  }
1264  }
1265  }
1266 }
1267 
1269 {
1270  int precno, compno, reslevelno, bandno, cblkno, lev, passno, layno;
1271  int i;
1272  double min = DBL_MAX;
1273  double max = 0;
1274  double thresh;
1275 
1276  Jpeg2000CodingStyle *codsty = &s->codsty;
1277 
1278  for (compno = 0; compno < s->ncomponents; compno++){
1279  Jpeg2000Component *comp = tile->comp + compno;
1280 
1281  for (reslevelno = 0, lev = codsty->nreslevels-1; reslevelno < codsty->nreslevels; reslevelno++, lev--){
1282  Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
1283 
1284  for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
1285  for (bandno = 0; bandno < reslevel->nbands ; bandno++){
1286  Jpeg2000Band *band = reslevel->band + bandno;
1287  Jpeg2000Prec *prec = band->prec + precno;
1288 
1289  for (cblkno = 0; cblkno < prec->nb_codeblocks_height * prec->nb_codeblocks_width; cblkno++){
1290  Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1291  for (passno = 0; passno < cblk->npasses; passno++) {
1292  Jpeg2000Pass *pass = &cblk->passes[passno];
1293  int dr;
1294  double dd, drslope;
1295 
1296  if (passno == 0) {
1297  dr = (int32_t)pass->rate;
1298  dd = pass->disto;
1299  } else {
1300  dr = (int32_t)(pass->rate - cblk->passes[passno - 1].rate);
1301  dd = pass->disto - cblk->passes[passno - 1].disto;
1302  }
1303 
1304  if (dr <= 0)
1305  continue;
1306 
1307  drslope = dd / dr;
1308  if (drslope < min)
1309  min = drslope;
1310 
1311  if (drslope > max)
1312  max = drslope;
1313  }
1314  }
1315  }
1316  }
1317  }
1318  }
1319 
1320  for (layno = 0; layno < s->nlayers; layno++) {
1321  double lo = min;
1322  double hi = max;
1323  double stable_thresh = 0.0;
1324  double good_thresh = 0.0;
1325  if (!s->layer_rates[layno]) {
1326  good_thresh = -1.0;
1327  } else {
1328  for (i = 0; i < 128; i++) {
1329  uint8_t *stream_pos = s->buf;
1330  int ret;
1331  thresh = (lo + hi) / 2;
1332  makelayer(s, layno, thresh, tile, 0);
1333  ret = encode_packets(s, tile, (int)(tile - s->tile), layno + 1);
1334  memset(stream_pos, 0, s->buf - stream_pos);
1335  if ((s->buf - stream_pos > ceil(tile->layer_rates[layno])) || ret < 0) {
1336  lo = thresh;
1337  s->buf = stream_pos;
1338  continue;
1339  }
1340  hi = thresh;
1341  stable_thresh = thresh;
1342  s->buf = stream_pos;
1343  }
1344  }
1345  if (good_thresh >= 0.0)
1346  good_thresh = stable_thresh == 0.0 ? thresh : stable_thresh;
1347  makelayer(s, layno, good_thresh, tile, 1);
1348  }
1349 }
1350 
1351 static int getcut(Jpeg2000Cblk *cblk, int64_t lambda, int dwt_norm)
1352 {
1353  int passno, res = 0;
1354  for (passno = 0; passno < cblk->npasses; passno++){
1355  int dr;
1356  int64_t dd;
1357 
1358  dr = cblk->passes[passno].rate
1359  - (res ? cblk->passes[res-1].rate : 0);
1360  dd = cblk->passes[passno].disto
1361  - (res ? cblk->passes[res-1].disto : 0);
1362 
1363  if (((dd * dwt_norm) >> WMSEDEC_SHIFT) * dwt_norm >= dr * lambda)
1364  res = passno+1;
1365  }
1366  return res;
1367 }
1368 
1370 {
1371  int precno, compno, reslevelno, bandno, cblkno, lev;
1372  Jpeg2000CodingStyle *codsty = &s->codsty;
1373 
1374  for (compno = 0; compno < s->ncomponents; compno++){
1375  Jpeg2000Component *comp = tile->comp + compno;
1376 
1377  for (reslevelno = 0, lev = codsty->nreslevels-1; reslevelno < codsty->nreslevels; reslevelno++, lev--){
1378  Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
1379 
1380  for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
1381  for (bandno = 0; bandno < reslevel->nbands ; bandno++){
1382  int bandpos = bandno + (reslevelno > 0);
1383  Jpeg2000Band *band = reslevel->band + bandno;
1384  Jpeg2000Prec *prec = band->prec + precno;
1385 
1386  for (cblkno = 0; cblkno < prec->nb_codeblocks_height * prec->nb_codeblocks_width; cblkno++){
1387  Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1388 
1389  cblk->ninclpasses = getcut(cblk, s->lambda,
1390  (int64_t)dwt_norms[codsty->transform == FF_DWT53][bandpos][lev] * (int64_t)band->i_stepsize >> 15);
1391  cblk->layers[0].data_start = cblk->data;
1392  cblk->layers[0].cum_passes = cblk->ninclpasses;
1393  cblk->layers[0].npasses = cblk->ninclpasses;
1394  if (cblk->ninclpasses)
1395  cblk->layers[0].data_len = cblk->passes[cblk->ninclpasses - 1].rate;
1396  }
1397  }
1398  }
1399  }
1400  }
1401 }
1402 
1403 static int encode_tile(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno)
1404 {
1405  int compno, reslevelno, bandno, ret;
1407  Jpeg2000CodingStyle *codsty = &s->codsty;
1408  for (compno = 0; compno < s->ncomponents; compno++){
1409  Jpeg2000Component *comp = s->tile[tileno].comp + compno;
1410 
1411  t1.stride = (1<<codsty->log2_cblk_width) + 2;
1412 
1413  av_log(s->avctx, AV_LOG_DEBUG,"dwt\n");
1414  if ((ret = ff_dwt_encode(&comp->dwt, comp->i_data)) < 0)
1415  return ret;
1416  av_log(s->avctx, AV_LOG_DEBUG,"after dwt -> tier1\n");
1417 
1418  for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
1419  Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
1420 
1421  for (bandno = 0; bandno < reslevel->nbands ; bandno++){
1422  Jpeg2000Band *band = reslevel->band + bandno;
1423  Jpeg2000Prec *prec = band->prec; // we support only 1 precinct per band ATM in the encoder
1424  int cblkx, cblky, cblkno=0, xx0, x0, xx1, y0, yy0, yy1, bandpos;
1425  yy0 = bandno == 0 ? 0 : comp->reslevel[reslevelno-1].coord[1][1] - comp->reslevel[reslevelno-1].coord[1][0];
1426  y0 = yy0;
1427  yy1 = FFMIN(ff_jpeg2000_ceildivpow2(band->coord[1][0] + 1, band->log2_cblk_height) << band->log2_cblk_height,
1428  band->coord[1][1]) - band->coord[1][0] + yy0;
1429 
1430  if (band->coord[0][0] == band->coord[0][1] || band->coord[1][0] == band->coord[1][1])
1431  continue;
1432 
1433  bandpos = bandno + (reslevelno > 0);
1434 
1435  for (cblky = 0; cblky < prec->nb_codeblocks_height; cblky++){
1436  if (reslevelno == 0 || bandno == 1)
1437  xx0 = 0;
1438  else
1439  xx0 = comp->reslevel[reslevelno-1].coord[0][1] - comp->reslevel[reslevelno-1].coord[0][0];
1440  x0 = xx0;
1441  xx1 = FFMIN(ff_jpeg2000_ceildivpow2(band->coord[0][0] + 1, band->log2_cblk_width) << band->log2_cblk_width,
1442  band->coord[0][1]) - band->coord[0][0] + xx0;
1443 
1444  for (cblkx = 0; cblkx < prec->nb_codeblocks_width; cblkx++, cblkno++){
1445  int y, x;
1446  if (codsty->transform == FF_DWT53){
1447  for (y = yy0; y < yy1; y++){
1448  int *ptr = t1.data + (y-yy0)*t1.stride;
1449  for (x = xx0; x < xx1; x++){
1450  *ptr++ = comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * y + x] * (1 << NMSEDEC_FRACBITS);
1451  }
1452  }
1453  } else{
1454  for (y = yy0; y < yy1; y++){
1455  int *ptr = t1.data + (y-yy0)*t1.stride;
1456  for (x = xx0; x < xx1; x++){
1457  *ptr = (comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * y + x]);
1458  *ptr = (int64_t)*ptr * (int64_t)(16384 * 65536 / band->i_stepsize) >> 15 - NMSEDEC_FRACBITS;
1459  ptr++;
1460  }
1461  }
1462  }
1463  if (!prec->cblk[cblkno].data)
1464  prec->cblk[cblkno].data = av_malloc(1 + 8192);
1465  if (!prec->cblk[cblkno].passes)
1466  prec->cblk[cblkno].passes = av_malloc_array(JPEG2000_MAX_PASSES, sizeof (*prec->cblk[cblkno].passes));
1467  if (!prec->cblk[cblkno].data || !prec->cblk[cblkno].passes)
1468  return AVERROR(ENOMEM);
1469  encode_cblk(s, &t1, prec->cblk + cblkno, tile, xx1 - xx0, yy1 - yy0,
1470  bandpos, codsty->nreslevels - reslevelno - 1);
1471  xx0 = xx1;
1472  xx1 = FFMIN(xx1 + (1 << band->log2_cblk_width), band->coord[0][1] - band->coord[0][0] + x0);
1473  }
1474  yy0 = yy1;
1475  yy1 = FFMIN(yy1 + (1 << band->log2_cblk_height), band->coord[1][1] - band->coord[1][0] + y0);
1476  }
1477  }
1478  }
1479  av_log(s->avctx, AV_LOG_DEBUG, "after tier1\n");
1480  }
1481 
1482  av_log(s->avctx, AV_LOG_DEBUG, "rate control\n");
1483  if (s->compression_rate_enc)
1484  makelayers(s, tile);
1485  else
1486  truncpasses(s, tile);
1487 
1488  if ((ret = encode_packets(s, tile, tileno, s->nlayers)) < 0)
1489  return ret;
1490  av_log(s->avctx, AV_LOG_DEBUG, "after rate control\n");
1491  return 0;
1492 }
1493 
1495 {
1496  int tileno, compno;
1497  Jpeg2000CodingStyle *codsty = &s->codsty;
1498 
1499  if (!s->tile)
1500  return;
1501  for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
1502  if (s->tile[tileno].comp) {
1503  for (compno = 0; compno < s->ncomponents; compno++){
1504  Jpeg2000Component *comp = s->tile[tileno].comp + compno;
1505  ff_jpeg2000_cleanup(comp, codsty);
1506  }
1507  av_freep(&s->tile[tileno].comp);
1508  }
1509  av_freep(&s->tile[tileno].layer_rates);
1510  }
1511  av_freep(&s->tile);
1512 }
1513 
1515 {
1516  int tileno, compno;
1517  for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
1518  Jpeg2000Tile *tile = s->tile + tileno;
1519  for (compno = 0; compno < s->ncomponents; compno++)
1520  ff_jpeg2000_reinit(tile->comp + compno, &s->codsty);
1521  }
1522 }
1523 
1524 static void update_size(uint8_t *size, const uint8_t *end)
1525 {
1526  AV_WB32(size, end-size);
1527 }
1528 
1530  const AVFrame *pict, int *got_packet)
1531 {
1532  int tileno, ret;
1534  uint8_t *chunkstart, *jp2cstart, *jp2hstart;
1535 
1536  if ((ret = ff_alloc_packet(avctx, pkt, avctx->width*avctx->height*9 + AV_INPUT_BUFFER_MIN_SIZE)) < 0)
1537  return ret;
1538 
1539  // init:
1540  s->buf = s->buf_start = pkt->data;
1541  s->buf_end = pkt->data + pkt->size;
1542 
1543  s->picture = pict;
1544 
1545  s->lambda = s->picture->quality * LAMBDA_SCALE;
1546 
1547  if (avctx->pix_fmt == AV_PIX_FMT_BGR48 || avctx->pix_fmt == AV_PIX_FMT_GRAY16)
1548  copy_frame_16(s);
1549  else
1550  copy_frame_8(s);
1551 
1552  reinit(s);
1553 
1554  if (s->format == CODEC_JP2) {
1555  av_assert0(s->buf == pkt->data);
1556 
1557  bytestream_put_be32(&s->buf, 0x0000000C);
1558  bytestream_put_be32(&s->buf, 0x6A502020);
1559  bytestream_put_be32(&s->buf, 0x0D0A870A);
1560 
1561  chunkstart = s->buf;
1562  bytestream_put_be32(&s->buf, 0);
1563  bytestream_put_buffer(&s->buf, "ftyp", 4);
1564  bytestream_put_buffer(&s->buf, "jp2\040\040", 4);
1565  bytestream_put_be32(&s->buf, 0);
1566  bytestream_put_buffer(&s->buf, "jp2\040", 4);
1567  update_size(chunkstart, s->buf);
1568 
1569  jp2hstart = s->buf;
1570  bytestream_put_be32(&s->buf, 0);
1571  bytestream_put_buffer(&s->buf, "jp2h", 4);
1572 
1573  chunkstart = s->buf;
1574  bytestream_put_be32(&s->buf, 0);
1575  bytestream_put_buffer(&s->buf, "ihdr", 4);
1576  bytestream_put_be32(&s->buf, avctx->height);
1577  bytestream_put_be32(&s->buf, avctx->width);
1578  bytestream_put_be16(&s->buf, s->ncomponents);
1579  bytestream_put_byte(&s->buf, s->cbps[0]);
1580  bytestream_put_byte(&s->buf, 7);
1581  bytestream_put_byte(&s->buf, 0);
1582  bytestream_put_byte(&s->buf, 0);
1583  update_size(chunkstart, s->buf);
1584 
1585  chunkstart = s->buf;
1586  bytestream_put_be32(&s->buf, 0);
1587  bytestream_put_buffer(&s->buf, "colr", 4);
1588  bytestream_put_byte(&s->buf, 1);
1589  bytestream_put_byte(&s->buf, 0);
1590  bytestream_put_byte(&s->buf, 0);
1591  if (avctx->pix_fmt == AV_PIX_FMT_RGB24 || avctx->pix_fmt == AV_PIX_FMT_PAL8) {
1592  bytestream_put_be32(&s->buf, 16);
1593  } else if (s->ncomponents == 1) {
1594  bytestream_put_be32(&s->buf, 17);
1595  } else {
1596  bytestream_put_be32(&s->buf, 18);
1597  }
1598  update_size(chunkstart, s->buf);
1599  if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
1600  int i;
1601  uint8_t *palette = pict->data[1];
1602  chunkstart = s->buf;
1603  bytestream_put_be32(&s->buf, 0);
1604  bytestream_put_buffer(&s->buf, "pclr", 4);
1605  bytestream_put_be16(&s->buf, AVPALETTE_COUNT);
1606  bytestream_put_byte(&s->buf, 3); // colour channels
1607  bytestream_put_be24(&s->buf, 0x070707); //colour depths
1608  for (i = 0; i < AVPALETTE_COUNT; i++) {
1609  bytestream_put_be24(&s->buf, HAVE_BIGENDIAN ? AV_RB24(palette + 1) : AV_RL24(palette));
1610  palette += 4;
1611  }
1612  update_size(chunkstart, s->buf);
1613  chunkstart = s->buf;
1614  bytestream_put_be32(&s->buf, 0);
1615  bytestream_put_buffer(&s->buf, "cmap", 4);
1616  for (i = 0; i < 3; i++) {
1617  bytestream_put_be16(&s->buf, 0); // component
1618  bytestream_put_byte(&s->buf, 1); // palette mapping
1619  bytestream_put_byte(&s->buf, i); // index
1620  }
1621  update_size(chunkstart, s->buf);
1622  }
1623  update_size(jp2hstart, s->buf);
1624 
1625  jp2cstart = s->buf;
1626  bytestream_put_be32(&s->buf, 0);
1627  bytestream_put_buffer(&s->buf, "jp2c", 4);
1628  }
1629 
1630  if (s->buf_end - s->buf < 2)
1631  return -1;
1632  bytestream_put_be16(&s->buf, JPEG2000_SOC);
1633  if ((ret = put_siz(s)) < 0)
1634  return ret;
1635  if ((ret = put_cod(s)) < 0)
1636  return ret;
1637  if ((ret = put_qcd(s, 0)) < 0)
1638  return ret;
1639  if ((ret = put_com(s, 0)) < 0)
1640  return ret;
1641 
1642  for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
1643  uint8_t *psotptr;
1644  if (!(psotptr = put_sot(s, tileno)))
1645  return -1;
1646  if (s->buf_end - s->buf < 2)
1647  return -1;
1648  bytestream_put_be16(&s->buf, JPEG2000_SOD);
1649  if ((ret = encode_tile(s, s->tile + tileno, tileno)) < 0)
1650  return ret;
1651  bytestream_put_be32(&psotptr, s->buf - psotptr + 6);
1652  }
1653  if (s->buf_end - s->buf < 2)
1654  return -1;
1655  bytestream_put_be16(&s->buf, JPEG2000_EOC);
1656 
1657  if (s->format == CODEC_JP2)
1658  update_size(jp2cstart, s->buf);
1659 
1660  av_log(s->avctx, AV_LOG_DEBUG, "end\n");
1661  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  static AVOnce init_static_once = AV_ONCE_INIT;
1717  int i, ret;
1719  Jpeg2000CodingStyle *codsty = &s->codsty;
1720  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 (avctx->pix_fmt == AV_PIX_FMT_PAL8 && (s->pred != FF_DWT97_INT || s->format != CODEC_JP2)) {
1732  av_log(s->avctx, AV_LOG_WARNING, "Forcing lossless jp2 for pal8\n");
1733  s->pred = FF_DWT97_INT;
1734  s->format = CODEC_JP2;
1735  }
1736 
1737  // defaults:
1738  // TODO: implement setting non-standard precinct size
1739  memset(codsty->log2_prec_widths , 15, sizeof(codsty->log2_prec_widths ));
1740  memset(codsty->log2_prec_heights, 15, sizeof(codsty->log2_prec_heights));
1741  codsty->nreslevels2decode=
1742  codsty->nreslevels = 7;
1743  codsty->nlayers = s->nlayers;
1744  codsty->log2_cblk_width = 4;
1745  codsty->log2_cblk_height = 4;
1746  codsty->transform = s->pred ? FF_DWT53 : FF_DWT97_INT;
1747 
1748  qntsty->nguardbits = 1;
1749 
1750  if ((s->tile_width & (s->tile_width -1)) ||
1751  (s->tile_height & (s->tile_height-1))) {
1752  av_log(avctx, AV_LOG_WARNING, "Tile dimension not a power of 2\n");
1753  }
1754 
1755  if (codsty->transform == FF_DWT53)
1756  qntsty->quantsty = JPEG2000_QSTY_NONE;
1757  else
1758  qntsty->quantsty = JPEG2000_QSTY_SE;
1759 
1760  s->width = avctx->width;
1761  s->height = avctx->height;
1762 
1763  for (i = 0; i < 3; i++) {
1764  if (avctx->pix_fmt == AV_PIX_FMT_GRAY16 || avctx->pix_fmt == AV_PIX_FMT_RGB48)
1765  s->cbps[i] = 16;
1766  else
1767  s->cbps[i] = 8;
1768  }
1769 
1770  if (avctx->pix_fmt == AV_PIX_FMT_RGB24 || avctx->pix_fmt == AV_PIX_FMT_RGB48){
1771  s->ncomponents = 3;
1772  } else if (avctx->pix_fmt == AV_PIX_FMT_GRAY8 || avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY16){
1773  s->ncomponents = 1;
1774  } else{ // planar YUV
1775  s->planar = 1;
1776  s->ncomponents = 3;
1778  s->chroma_shift, s->chroma_shift + 1);
1779  if (ret)
1780  return ret;
1781  }
1782 
1783  ff_thread_once(&init_static_once, init_luts);
1784 
1786  if ((ret=init_tiles(s)) < 0)
1787  return ret;
1788 
1789  av_log(s->avctx, AV_LOG_DEBUG, "after init\n");
1790 
1791  return 0;
1792 }
1793 
1794 static int j2kenc_destroy(AVCodecContext *avctx)
1795 {
1797 
1798  cleanup(s);
1799  return 0;
1800 }
1801 
1802 // taken from the libopenjpeg wraper so it matches
1803 
1804 #define OFFSET(x) offsetof(Jpeg2000EncoderContext, x)
1805 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1806 static const AVOption options[] = {
1807  { "format", "Codec Format", OFFSET(format), AV_OPT_TYPE_INT, { .i64 = CODEC_JP2 }, CODEC_J2K, CODEC_JP2, VE, "format" },
1808  { "j2k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CODEC_J2K }, 0, 0, VE, "format" },
1809  { "jp2", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CODEC_JP2 }, 0, 0, VE, "format" },
1810  { "tile_width", "Tile Width", OFFSET(tile_width), AV_OPT_TYPE_INT, { .i64 = 256 }, 1, 1<<30, VE, },
1811  { "tile_height", "Tile Height", OFFSET(tile_height), AV_OPT_TYPE_INT, { .i64 = 256 }, 1, 1<<30, VE, },
1812  { "pred", "DWT Type", OFFSET(pred), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE, "pred" },
1813  { "dwt97int", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, VE, "pred" },
1814  { "dwt53", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, VE, "pred" },
1815  { "sop", "SOP marker", OFFSET(sop), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE, },
1816  { "eph", "EPH marker", OFFSET(eph), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE, },
1817  { "prog", "Progression Order", OFFSET(prog), AV_OPT_TYPE_INT, { .i64 = 0 }, JPEG2000_PGOD_LRCP, JPEG2000_PGOD_CPRL, VE, "prog" },
1818  { "lrcp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = JPEG2000_PGOD_LRCP }, 0, 0, VE, "prog" },
1819  { "rlcp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = JPEG2000_PGOD_RLCP }, 0, 0, VE, "prog" },
1820  { "rpcl", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = JPEG2000_PGOD_RPCL }, 0, 0, VE, "prog" },
1821  { "pcrl", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = JPEG2000_PGOD_PCRL }, 0, 0, VE, "prog" },
1822  { "cprl", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = JPEG2000_PGOD_CPRL }, 0, 0, VE, "prog" },
1823  { "layer_rates", "Layer Rates", OFFSET(lr_str), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, VE },
1824  { NULL }
1825 };
1826 
1827 static const AVClass j2k_class = {
1828  .class_name = "jpeg 2000 encoder",
1829  .item_name = av_default_item_name,
1830  .option = options,
1831  .version = LIBAVUTIL_VERSION_INT,
1832 };
1833 
1835  .name = "jpeg2000",
1836  .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
1837  .type = AVMEDIA_TYPE_VIDEO,
1838  .id = AV_CODEC_ID_JPEG2000,
1839  .priv_data_size = sizeof(Jpeg2000EncoderContext),
1840  .init = j2kenc_init,
1841  .encode2 = encode_frame,
1842  .close = j2kenc_destroy,
1843  .pix_fmts = (const enum AVPixelFormat[]) {
1850  },
1851  .priv_class = &j2k_class,
1853 };
AVCodec
AVCodec.
Definition: codec.h:202
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
FF_CODEC_CAP_INIT_THREADSAFE
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:42
Jpeg2000Tile::layer_rates
double * layer_rates
Definition: j2kenc.c:107
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
tag_tree_code
static void tag_tree_code(Jpeg2000EncoderContext *s, Jpeg2000TgtNode *node, int threshold)
code the value stored in node
Definition: j2kenc.c:251
makelayer
static void makelayer(Jpeg2000EncoderContext *s, int layno, double thresh, Jpeg2000Tile *tile, int final)
Definition: j2kenc.c:1185
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
Jpeg2000QuantStyle::quantsty
uint8_t quantsty
Definition: jpeg2000.h:154
LIBAVCODEC_IDENT
#define LIBAVCODEC_IDENT
Definition: version.h:42
JPEG2000_EOC
@ JPEG2000_EOC
Definition: jpeg2000.h:58
options
static const AVOption options[]
Definition: j2kenc.c:1806
comp
static void comp(unsigned char *dst, ptrdiff_t dst_stride, unsigned char *src, ptrdiff_t src_stride, int add)
Definition: eamad.c:85
Jpeg2000EncoderContext::buf
uint8_t * buf
Definition: j2kenc.c:124
ff_jpeg2000_init_component
int ff_jpeg2000_init_component(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty, Jpeg2000QuantStyle *qntsty, int cbps, int dx, int dy, AVCodecContext *avctx)
Definition: jpeg2000.c:466
thread.h
JPEG2000_QSTY_NONE
@ JPEG2000_QSTY_NONE
Definition: jpeg2000.h:65
Jpeg2000Layer::disto
double disto
Definition: jpeg2000.h:169
Jpeg2000EncoderContext::bit_index
int bit_index
Definition: j2kenc.c:126
j2kenc_init
static av_cold int j2kenc_init(AVCodecContext *avctx)
Definition: j2kenc.c:1714
JPEG2000_QCD
@ JPEG2000_QCD
Definition: jpeg2000.h:46
Jpeg2000Prec::nb_codeblocks_height
int nb_codeblocks_height
Definition: jpeg2000.h:194
Jpeg2000Band::i_stepsize
int i_stepsize
Definition: jpeg2000.h:205
JPEG2000_SOP
@ JPEG2000_SOP
Definition: jpeg2000.h:55
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:317
put_bits
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:220
pixdesc.h
Jpeg2000Layer::cum_passes
int cum_passes
Definition: jpeg2000.h:170
compute_rates
static void compute_rates(Jpeg2000EncoderContext *s)
Definition: j2kenc.c:425
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:373
Jpeg2000Layer::data_len
int data_len
Definition: jpeg2000.h:167
j2k_class
static const AVClass j2k_class
Definition: j2kenc.c:1827
Jpeg2000Prec::zerobits
Jpeg2000TgtNode * zerobits
Definition: jpeg2000.h:195
CODEC_J2K
#define CODEC_J2K
Definition: j2kenc.c:86
AVOption
AVOption.
Definition: opt.h:247
encode.h
JPEG2000_SOD
@ JPEG2000_SOD
Definition: jpeg2000.h:57
JPEG2000_SOC
@ JPEG2000_SOC
Definition: jpeg2000.h:39
ff_dwt_encode
int ff_dwt_encode(DWTContext *s, void *t)
Definition: jpeg2000dwt.c:580
ff_jpeg2000_ceildiv
static int ff_jpeg2000_ceildiv(int a, int64_t b)
Definition: jpeg2000.h:234
Jpeg2000Prec
Definition: jpeg2000.h:192
float.h
JPEG2000_SOT
@ JPEG2000_SOT
Definition: jpeg2000.h:54
Jpeg2000TgtNode::parent
struct Jpeg2000TgtNode * parent
Definition: jpeg2000.h:132
Jpeg2000Band
Definition: jpeg2000.h:202
t1
#define t1
Definition: regdef.h:29
Jpeg2000EncoderContext::lambda
int64_t lambda
Definition: j2kenc.c:128
encode_frame
static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
Definition: j2kenc.c:1529
max
#define max(a, b)
Definition: cuda_runtime.h:33
Jpeg2000Pass::rate
uint16_t rate
Definition: jpeg2000.h:159
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
JPEG2000_CSTY_SOP
#define JPEG2000_CSTY_SOP
Definition: jpeg2000.h:111
LAMBDA_SCALE
#define LAMBDA_SCALE
Definition: j2kenc.c:83
Jpeg2000Tile
Definition: j2kenc.c:105
ff_jpeg2000_getrefctxno
static int ff_jpeg2000_getrefctxno(int flag)
Definition: jpeg2000.h:262
Jpeg2000Pass::flushed_len
int flushed_len
Definition: jpeg2000.h:162
j2kenc_destroy
static int j2kenc_destroy(AVCodecContext *avctx)
Definition: j2kenc.c:1794
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:338
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:31
Jpeg2000Cblk::incl
uint8_t incl
Definition: jpeg2000.h:177
init
static int init
Definition: av_tx.c:47
bit
#define bit(string, value)
Definition: cbs_mpeg2.c:58
tag_tree_update
static void tag_tree_update(Jpeg2000TgtNode *node)
update the value in node
Definition: j2kenc.c:288
Jpeg2000EncoderContext::lr_str
char * lr_str
Definition: j2kenc.c:143
Jpeg2000CodingStyle::log2_cblk_width
uint8_t log2_cblk_width
Definition: jpeg2000.h:138
U
#define U(x)
Definition: vp56_arith.h:37
Jpeg2000EncoderContext::buf_end
uint8_t * buf_end
Definition: j2kenc.c:125
Jpeg2000Cblk::passes
Jpeg2000Pass * passes
Definition: jpeg2000.h:187
Jpeg2000CodingStyle::log2_prec_heights
uint8_t log2_prec_heights[JPEG2000_MAX_RESLEVELS]
Definition: jpeg2000.h:147
val
static double val(void *priv, double ch)
Definition: aeval.c:76
av_pix_fmt_get_chroma_sub_sample
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:2688
scale
static av_always_inline float scale(float x, float s)
Definition: vf_v360.c:1388
MQC_CX_UNI
#define MQC_CX_UNI
Definition: mqc.h:33
AV_PIX_FMT_GRAY16
#define AV_PIX_FMT_GRAY16
Definition: pixfmt.h:388
j2k_flush
static void j2k_flush(Jpeg2000EncoderContext *s)
flush the bitstream
Definition: j2kenc.c:240
Jpeg2000EncoderContext::numYtiles
int numYtiles
Definition: j2kenc.c:121
ss
#define ss(width, name, subs,...)
Definition: cbs_vp9.c:261
put_cod
static int put_cod(Jpeg2000EncoderContext *s)
Definition: j2kenc.c:329
Jpeg2000T1Context
Definition: jpeg2000.h:121
ceil
static __device__ float ceil(float a)
Definition: cuda_runtime.h:176
ff_thread_once
static int ff_thread_once(char *control, void(*routine)(void))
Definition: thread.h:175
pkt
AVPacket * pkt
Definition: movenc.c:59
Jpeg2000ResLevel
Definition: jpeg2000.h:210
av_cold
#define av_cold
Definition: attributes.h:90
mask
static const uint16_t mask[17]
Definition: lzw.c:38
Jpeg2000Pass::disto
int64_t disto
Definition: jpeg2000.h:160
Jpeg2000QuantStyle::nguardbits
uint8_t nguardbits
Definition: jpeg2000.h:155
init_luts
static void init_luts(void)
Definition: j2kenc.c:581
Jpeg2000Tile::comp
Jpeg2000Component * comp
Definition: j2kenc.c:106
width
#define width
intreadwrite.h
Jpeg2000CodingStyle::transform
uint8_t transform
Definition: jpeg2000.h:140
s
#define s(width, name)
Definition: cbs_vp9.c:257
Jpeg2000Cblk::layers
Jpeg2000Layer * layers
Definition: jpeg2000.h:188
lut_nmsedec_sig0
static int lut_nmsedec_sig0[1<< NMSEDEC_BITS]
Definition: j2kenc.c:91
Jpeg2000ResLevel::band
Jpeg2000Band * band
Definition: jpeg2000.h:215
getcut
static int getcut(Jpeg2000Cblk *cblk, int64_t lambda, int dwt_norm)
Definition: j2kenc.c:1351
jpeg2000.h
Jpeg2000EncoderContext::eph
int eph
Definition: j2kenc.c:140
Jpeg2000EncoderContext::nlayers
int nlayers
Definition: j2kenc.c:142
av_strtok
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
JPEG2000_PGOD_RPCL
#define JPEG2000_PGOD_RPCL
Definition: jpeg2000.h:117
Jpeg2000Cblk::data
uint8_t * data
Definition: jpeg2000.h:182
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
pix_fmts
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:296
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
Jpeg2000Band::coord
int coord[2][2]
Definition: jpeg2000.h:203
Jpeg2000EncoderContext::planar
uint8_t planar
Definition: j2kenc.c:118
xi
#define xi(width, name, var, range_min, range_max, subs,...)
Definition: cbs_h2645.c:404
encode_sigpass
static void encode_sigpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
Definition: j2kenc.c:614
AV_INPUT_BUFFER_MIN_SIZE
#define AV_INPUT_BUFFER_MIN_SIZE
Definition: avcodec.h:185
encode_packet
static int encode_packet(Jpeg2000EncoderContext *s, Jpeg2000ResLevel *rlevel, int layno, int precno, uint8_t *expn, int numgbits, int packetno, int nlayers)
Definition: j2kenc.c:783
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
JPEG2000_PGOD_CPRL
#define JPEG2000_PGOD_CPRL
Definition: jpeg2000.h:119
JPEG2000_COM
@ JPEG2000_COM
Definition: jpeg2000.h:53
pass
#define pass
Definition: fft_template.c:601
ff_jpeg2000_encoder
const AVCodec ff_jpeg2000_encoder
Definition: j2kenc.c:1834
Jpeg2000Cblk::lblock
uint8_t lblock
Definition: jpeg2000.h:181
Jpeg2000CodingStyle::log2_prec_widths
uint8_t log2_prec_widths[JPEG2000_MAX_RESLEVELS]
Definition: jpeg2000.h:146
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AV_ONCE_INIT
#define AV_ONCE_INIT
Definition: thread.h:173
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
makelayers
static void makelayers(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile)
Definition: j2kenc.c:1268
AV_PIX_FMT_BGR48
#define AV_PIX_FMT_BGR48
Definition: pixfmt.h:395
NULL
#define NULL
Definition: coverity.c:32
put_num
static void put_num(Jpeg2000EncoderContext *s, int num, int n)
put n least significant bits of a number num
Definition: j2kenc.c:233
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
Jpeg2000EncoderContext::sop
int sop
Definition: j2kenc.c:139
Jpeg2000EncoderContext::codsty
Jpeg2000CodingStyle codsty
Definition: j2kenc.c:130
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
Jpeg2000Band::prec
Jpeg2000Prec * prec
Definition: jpeg2000.h:207
JPEG2000_T1_VIS
#define JPEG2000_T1_VIS
Definition: jpeg2000.h:95
Jpeg2000Layer::npasses
int npasses
Definition: jpeg2000.h:168
Jpeg2000ResLevel::num_precincts_y
int num_precincts_y
Definition: jpeg2000.h:213
JPEG2000_EPH
@ JPEG2000_EPH
Definition: jpeg2000.h:56
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
Jpeg2000Band::log2_cblk_height
uint16_t log2_cblk_height
Definition: jpeg2000.h:204
AVOnce
#define AVOnce
Definition: thread.h:172
AVPALETTE_COUNT
#define AVPALETTE_COUNT
Definition: pixfmt.h:33
truncpasses
static void truncpasses(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile)
Definition: j2kenc.c:1369
Jpeg2000EncoderContext::format
int format
Definition: j2kenc.c:137
getnmsedec_sig
static int getnmsedec_sig(int x, int bpno)
Definition: j2kenc.c:600
JPEG2000_T1_SIG_NB
#define JPEG2000_T1_SIG_NB
Definition: jpeg2000.h:85
for
for(j=16;j >0;--j)
Definition: h264pred_template.c:469
AV_WB32
#define AV_WB32(p, v)
Definition: intreadwrite.h:419
Jpeg2000Prec::nb_codeblocks_width
int nb_codeblocks_width
Definition: jpeg2000.h:193
JPEG2000_T1_SGN
#define JPEG2000_T1_SGN
Definition: jpeg2000.h:99
Jpeg2000ResLevel::log2_prec_height
uint8_t log2_prec_height
Definition: jpeg2000.h:214
ff_jpeg2000_cleanup
void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
Definition: jpeg2000.c:597
getnmsedec_ref
static int getnmsedec_ref(int x, int bpno)
Definition: j2kenc.c:607
Jpeg2000Component
Definition: jpeg2000.h:218
AV_PIX_FMT_RGB24
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:68
Jpeg2000Prec::cblkincl
Jpeg2000TgtNode * cblkincl
Definition: jpeg2000.h:196
encode_clnpass
static void encode_clnpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
Definition: j2kenc.c:650
Jpeg2000EncoderContext::picture
const AVFrame * picture
Definition: j2kenc.c:113
AVPacket::size
int size
Definition: packet.h:374
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
encode_tile
static int encode_tile(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno)
Definition: j2kenc.c:1403
Jpeg2000ResLevel::nbands
uint8_t nbands
Definition: jpeg2000.h:211
sp
#define sp
Definition: regdef.h:63
AV_PIX_FMT_RGB48
#define AV_PIX_FMT_RGB48
Definition: pixfmt.h:390
size
int size
Definition: twinvq_data.h:10344
Jpeg2000Cblk
Definition: jpeg2000.h:173
COPY_FRAME
#define COPY_FRAME(D, PIXEL)
Definition: j2kenc.c:507
Jpeg2000Cblk::ninclpasses
uint8_t ninclpasses
Definition: jpeg2000.h:175
NMSEDEC_FRACBITS
#define NMSEDEC_FRACBITS
Definition: j2kenc.c:81
format
ofilter format
Definition: ffmpeg_filter.c:172
AV_RL24
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_RL24
Definition: bytestream.h:93
height
#define height
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
JPEG2000_COD
@ JPEG2000_COD
Definition: jpeg2000.h:41
ff_jpeg2000_getsgnctxno
static int ff_jpeg2000_getsgnctxno(int flag, int *xorbit)
Definition: jpeg2000.h:271
JPEG2000_PGOD_LRCP
#define JPEG2000_PGOD_LRCP
Definition: jpeg2000.h:115
Jpeg2000TgtNode
Definition: jpeg2000.h:128
OFFSET
#define OFFSET(x)
Definition: j2kenc.c:1804
Jpeg2000CodingStyle::nlayers
uint8_t nlayers
Definition: jpeg2000.h:142
reinit
static void reinit(Jpeg2000EncoderContext *s)
Definition: j2kenc.c:1514
Jpeg2000CodingStyle::nreslevels
int nreslevels
Definition: jpeg2000.h:136
ff_jpeg2000_reinit
void ff_jpeg2000_reinit(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
Definition: jpeg2000.c:576
Jpeg2000TgtNode::temp_val
uint8_t temp_val
Definition: jpeg2000.h:130
Jpeg2000Pass
Definition: jpeg2000.h:158
bytestream_put_buffer
static av_always_inline void bytestream_put_buffer(uint8_t **b, const uint8_t *src, unsigned int size)
Definition: bytestream.h:372
Jpeg2000CodingStyle::log2_cblk_height
uint8_t log2_cblk_height
Definition: jpeg2000.h:139
put_sot
static uint8_t * put_sot(Jpeg2000EncoderContext *s, int tileno)
Definition: j2kenc.c:406
JPEG2000_PGOD_RLCP
#define JPEG2000_PGOD_RLCP
Definition: jpeg2000.h:116
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
parse_layer_rates
static int parse_layer_rates(Jpeg2000EncoderContext *s)
Definition: j2kenc.c:1667
FF_CODEC_CAP_INIT_CLEANUP
#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:50
Jpeg2000ResLevel::num_precincts_x
int num_precincts_x
Definition: jpeg2000.h:213
JPEG2000_T1_REF
#define JPEG2000_T1_REF
Definition: jpeg2000.h:97
cleanup
static void cleanup(Jpeg2000EncoderContext *s)
Definition: j2kenc.c:1494
Jpeg2000QuantStyle::expn
uint8_t expn[JPEG2000_MAX_DECLEVELS *3]
Definition: jpeg2000.h:152
av_malloc_array
#define av_malloc_array(a, b)
Definition: tableprint_vlc.h:32
dwt_norms
static const int dwt_norms[2][4][10]
Definition: j2kenc.c:93
common.h
Jpeg2000Layer
Definition: jpeg2000.h:165
encode_cblk
static void encode_cblk(Jpeg2000EncoderContext *s, Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk, Jpeg2000Tile *tile, int width, int height, int bandpos, int lev)
Definition: j2kenc.c:705
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
ff_mqc_initenc
void ff_mqc_initenc(MqcState *mqc, uint8_t *bp)
initialize the encoder
Definition: mqcenc.c:69
JPEG2000_SIZ
@ JPEG2000_SIZ
Definition: jpeg2000.h:40
Jpeg2000Band::log2_cblk_width
uint16_t log2_cblk_width
Definition: jpeg2000.h:204
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:209
ff_jpeg2000_getsigctxno
static int ff_jpeg2000_getsigctxno(int flag, int bandno)
Definition: jpeg2000.h:253
encode_packets
static int encode_packets(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno, int nlayers)
Definition: j2kenc.c:939
WMSEDEC_SHIFT
#define WMSEDEC_SHIFT
must be >= 13
Definition: j2kenc.c:82
lev
static LevelCodes lev[4+3+3]
Definition: clearvideo.c:85
AV_CODEC_ID_JPEG2000
@ AV_CODEC_ID_JPEG2000
Definition: codec_id.h:138
AVCodecContext::height
int height
Definition: avcodec.h:556
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:593
JPEG2000_MAX_PASSES
#define JPEG2000_MAX_PASSES
Definition: jpeg2000.h:73
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:271
Jpeg2000EncoderContext::compression_rate_enc
uint8_t compression_rate_enc
Is compression done using compression ratio?
Definition: j2kenc.c:135
Jpeg2000EncoderContext::tile_width
int tile_width
Definition: j2kenc.c:120
lut_nmsedec_ref
static int lut_nmsedec_ref[1<< NMSEDEC_BITS]
Definition: j2kenc.c:88
Jpeg2000EncoderContext::width
int width
Definition: j2kenc.c:115
Jpeg2000QuantStyle::mant
uint16_t mant[JPEG2000_MAX_DECLEVELS *3]
Definition: jpeg2000.h:153
avcodec.h
JPEG2000_T1_SIG
#define JPEG2000_T1_SIG
Definition: jpeg2000.h:96
AV_PIX_FMT_PAL8
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:77
ret
ret
Definition: filter_design.txt:187
put_qcd
static int put_qcd(Jpeg2000EncoderContext *s, int compno)
Definition: j2kenc.c:361
pred
static const float pred[4]
Definition: siprdata.h:259
AVClass::class_name
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:71
JPEG2000_QSTY_SE
@ JPEG2000_QSTY_SE
Definition: jpeg2000.h:67
NMSEDEC_BITS
#define NMSEDEC_BITS
Definition: j2kenc.c:80
Jpeg2000EncoderContext::buf_start
uint8_t * buf_start
Definition: j2kenc.c:123
encode_refpass
static void encode_refpass(Jpeg2000T1Context *t1, int width, int height, int *nmsedec, int bpno)
Definition: j2kenc.c:636
Jpeg2000Layer::data_start
uint8_t * data_start
Definition: jpeg2000.h:166
Jpeg2000Pass::flushed
uint8_t flushed[4]
Definition: jpeg2000.h:161
pos
unsigned int pos
Definition: spdifenc.c:412
Jpeg2000EncoderContext::prog
int prog
Definition: j2kenc.c:141
lut_nmsedec_sig
static int lut_nmsedec_sig[1<< NMSEDEC_BITS]
Definition: j2kenc.c:90
MQC_CX_RL
#define MQC_CX_RL
Definition: mqc.h:34
Jpeg2000Component::coord
int coord[2][2]
Definition: jpeg2000.h:223
AVCodecContext
main external API structure.
Definition: avcodec.h:383
FF_DWT53
@ FF_DWT53
Definition: jpeg2000dwt.h:38
ff_jpeg2000_ceildivpow2
static int ff_jpeg2000_ceildivpow2(int a, int b)
Definition: jpeg2000.h:229
put_siz
static int put_siz(Jpeg2000EncoderContext *s)
Definition: j2kenc.c:300
CODEC_JP2
#define CODEC_JP2
Definition: j2kenc.c:85
Jpeg2000ResLevel::log2_prec_width
uint8_t log2_prec_width
Definition: jpeg2000.h:214
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:224
ff_jpeg2000_init_tier1_luts
void av_cold ff_jpeg2000_init_tier1_luts(void)
Definition: jpeg2000.c:173
JPEG2000_CSTY_EPH
#define JPEG2000_CSTY_EPH
Definition: jpeg2000.h:112
AV_PIX_FMT_YUV444P
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
Jpeg2000EncoderContext::pred
int pred
Definition: j2kenc.c:138
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
Jpeg2000EncoderContext::qntsty
Jpeg2000QuantStyle qntsty
Definition: j2kenc.c:131
AV_PIX_FMT_YUV422P
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
VE
#define VE
Definition: j2kenc.c:1805
put_com
static int put_com(Jpeg2000EncoderContext *s, int compno)
Definition: j2kenc.c:387
JPEG2000_PGOD_PCRL
#define JPEG2000_PGOD_PCRL
Definition: jpeg2000.h:118
AV_CODEC_FLAG_BITEXACT
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition: avcodec.h:272
ff_mqc_flush_to
int ff_mqc_flush_to(MqcState *mqc, uint8_t *dst, int *dst_len)
flush the encoder [returns number of bytes encoded]
Definition: mqcenc.c:117
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:37
putnumpasses
static void putnumpasses(Jpeg2000EncoderContext *s, int n)
Definition: j2kenc.c:768
Jpeg2000Cblk::npasses
uint8_t npasses
Definition: jpeg2000.h:174
Jpeg2000CodingStyle::nreslevels2decode
int nreslevels2decode
Definition: jpeg2000.h:137
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:410
AVPacket
This structure stores compressed data.
Definition: packet.h:350
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
ff_jpeg2000_set_significance
void ff_jpeg2000_set_significance(Jpeg2000T1Context *t1, int x, int y, int negative)
Definition: jpeg2000.c:179
AV_PIX_FMT_YUV411P
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:73
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:556
int32_t
int32_t
Definition: audioconvert.c:56
bytestream.h
Jpeg2000TgtNode::val
uint8_t val
Definition: jpeg2000.h:129
AV_PIX_FMT_YUV410P
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:72
update_size
static void update_size(uint8_t *size, const uint8_t *end)
Definition: j2kenc.c:1524
Jpeg2000TgtNode::vis
uint8_t vis
Definition: jpeg2000.h:131
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
Jpeg2000CodingStyle
Definition: jpeg2000.h:135
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
Jpeg2000QuantStyle
Definition: jpeg2000.h:151
avstring.h
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Definition: opt.h:228
Jpeg2000Cblk::nonzerobits
uint8_t nonzerobits
Definition: jpeg2000.h:176
Jpeg2000Prec::cblk
Jpeg2000Cblk * cblk
Definition: jpeg2000.h:197
AV_RB24
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_RB24
Definition: bytestream.h:97
Jpeg2000EncoderContext::avctx
AVCodecContext * avctx
Definition: j2kenc.c:112
ff_mqc_encode
void ff_mqc_encode(MqcState *mqc, uint8_t *cxstate, int d)
code bit d with context cx
Definition: mqcenc.c:79
ff_tag_tree_zero
void ff_tag_tree_zero(Jpeg2000TgtNode *t, int w, int h, int val)
Definition: jpeg2000.c:86
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:233
av_log2
int av_log2(unsigned v)
Definition: intmath.c:26
ff_alloc_packet
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and allocate data.
Definition: encode.c:34
Jpeg2000EncoderContext::ncomponents
int ncomponents
Definition: j2kenc.c:119
init_tiles
static int init_tiles(Jpeg2000EncoderContext *s)
compute the sizes of tiles, resolution levels, bands, etc.
Definition: j2kenc.c:455
init_quantization
static void init_quantization(Jpeg2000EncoderContext *s)
Definition: j2kenc.c:551
lut_nmsedec_ref0
static int lut_nmsedec_ref0[1<< NMSEDEC_BITS]
Definition: j2kenc.c:89
FF_DWT97_INT
@ FF_DWT97_INT
Definition: jpeg2000dwt.h:39
Jpeg2000EncoderContext::tile
Jpeg2000Tile * tile
Definition: j2kenc.c:133
Jpeg2000EncoderContext
Definition: j2kenc.c:110
min
float min
Definition: vorbis_enc_data.h:429