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