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