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