FFmpeg  2.8.17
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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  * All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  * notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  * notice, this list of conditions and the following disclaimer in the
44  * documentation and/or other materials provided with the distribution.
45  *
46  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
47  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
50  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
51  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
52  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
53  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
54  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
55  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
56  * POSSIBILITY OF SUCH DAMAGE.
57  */
58 
59 
60 /**
61  * JPEG2000 image encoder
62  * @file
63  * @author Kamil Nowosad
64  */
65 
66 #include <float.h>
67 #include "avcodec.h"
68 #include "internal.h"
69 #include "bytestream.h"
70 #include "jpeg2000.h"
71 #include "libavutil/common.h"
72 #include "libavutil/opt.h"
73 
74 #define NMSEDEC_BITS 7
75 #define NMSEDEC_FRACBITS (NMSEDEC_BITS-1)
76 #define WMSEDEC_SHIFT 13 ///< must be >= 13
77 #define LAMBDA_SCALE (100000000LL << (WMSEDEC_SHIFT - 13))
78 
79 #define CODEC_JP2 1
80 #define CODEC_J2K 0
81 
82 static int lut_nmsedec_ref [1<<NMSEDEC_BITS],
86 
87 static const int dwt_norms[2][4][10] = { // [dwt_type][band][rlevel] (multiplied by 10000)
88  {{10000, 19650, 41770, 84030, 169000, 338400, 676900, 1353000, 2706000, 5409000},
89  {20220, 39890, 83550, 170400, 342700, 686300, 1373000, 2746000, 5490000},
90  {20220, 39890, 83550, 170400, 342700, 686300, 1373000, 2746000, 5490000},
91  {20800, 38650, 83070, 171800, 347100, 695900, 1393000, 2786000, 5572000}},
92 
93  {{10000, 15000, 27500, 53750, 106800, 213400, 426700, 853300, 1707000, 3413000},
94  {10380, 15920, 29190, 57030, 113300, 226400, 452500, 904800, 1809000},
95  {10380, 15920, 29190, 57030, 113300, 226400, 452500, 904800, 1809000},
96  { 7186, 9218, 15860, 30430, 60190, 120100, 240000, 479700, 959300}}
97 };
98 
99 typedef struct {
101 } Jpeg2000Tile;
102 
103 typedef struct {
104  AVClass *class;
106  const AVFrame *picture;
107 
108  int width, height; ///< image width and height
109  uint8_t cbps[4]; ///< bits per sample in particular components
110  int chroma_shift[2];
113  int tile_width, tile_height; ///< tile size
114  int numXtiles, numYtiles;
115 
120 
121  int64_t lambda;
122 
125 
127 
128  int format;
130 
131 
132 /* debug */
133 #if 0
134 #undef ifprintf
135 #undef printf
136 
137 static void nspaces(FILE *fd, int n)
138 {
139  while(n--) putc(' ', fd);
140 }
141 
142 static void printcomp(Jpeg2000Component *comp)
143 {
144  int i;
145  for (i = 0; i < comp->y1 - comp->y0; i++)
146  ff_jpeg2000_printv(comp->i_data + i * (comp->x1 - comp->x0), comp->x1 - comp->x0);
147 }
148 
149 static void dump(Jpeg2000EncoderContext *s, FILE *fd)
150 {
151  int tileno, compno, reslevelno, bandno, precno;
152  fprintf(fd, "XSiz = %d, YSiz = %d, tile_width = %d, tile_height = %d\n"
153  "numXtiles = %d, numYtiles = %d, ncomponents = %d\n"
154  "tiles:\n",
155  s->width, s->height, s->tile_width, s->tile_height,
156  s->numXtiles, s->numYtiles, s->ncomponents);
157  for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
158  Jpeg2000Tile *tile = s->tile + tileno;
159  nspaces(fd, 2);
160  fprintf(fd, "tile %d:\n", tileno);
161  for(compno = 0; compno < s->ncomponents; compno++){
162  Jpeg2000Component *comp = tile->comp + compno;
163  nspaces(fd, 4);
164  fprintf(fd, "component %d:\n", compno);
165  nspaces(fd, 4);
166  fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d\n",
167  comp->x0, comp->x1, comp->y0, comp->y1);
168  for(reslevelno = 0; reslevelno < s->nreslevels; reslevelno++){
169  Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
170  nspaces(fd, 6);
171  fprintf(fd, "reslevel %d:\n", reslevelno);
172  nspaces(fd, 6);
173  fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d, nbands = %d\n",
174  reslevel->x0, reslevel->x1, reslevel->y0,
175  reslevel->y1, reslevel->nbands);
176  for(bandno = 0; bandno < reslevel->nbands; bandno++){
177  Jpeg2000Band *band = reslevel->band + bandno;
178  nspaces(fd, 8);
179  fprintf(fd, "band %d:\n", bandno);
180  nspaces(fd, 8);
181  fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d,"
182  "codeblock_width = %d, codeblock_height = %d cblknx = %d cblkny = %d\n",
183  band->x0, band->x1,
184  band->y0, band->y1,
185  band->codeblock_width, band->codeblock_height,
186  band->cblknx, band->cblkny);
187  for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
188  Jpeg2000Prec *prec = band->prec + precno;
189  nspaces(fd, 10);
190  fprintf(fd, "prec %d:\n", precno);
191  nspaces(fd, 10);
192  fprintf(fd, "xi0 = %d, xi1 = %d, yi0 = %d, yi1 = %d\n",
193  prec->xi0, prec->xi1, prec->yi0, prec->yi1);
194  }
195  }
196  }
197  }
198  }
199 }
200 #endif
201 
202 /* bitstream routines */
203 
204 /** put n times val bit */
205 static void put_bits(Jpeg2000EncoderContext *s, int val, int n) // TODO: optimize
206 {
207  while (n-- > 0){
208  if (s->bit_index == 8)
209  {
210  s->bit_index = *s->buf == 0xff;
211  *(++s->buf) = 0;
212  }
213  *s->buf |= val << (7 - s->bit_index++);
214  }
215 }
216 
217 /** put n least significant bits of a number num */
218 static void put_num(Jpeg2000EncoderContext *s, int num, int n)
219 {
220  while(--n >= 0)
221  put_bits(s, (num >> n) & 1, 1);
222 }
223 
224 /** flush the bitstream */
226 {
227  if (s->bit_index){
228  s->bit_index = 0;
229  s->buf++;
230  }
231 }
232 
233 /* tag tree routines */
234 
235 /** code the value stored in node */
236 static void tag_tree_code(Jpeg2000EncoderContext *s, Jpeg2000TgtNode *node, int threshold)
237 {
238  Jpeg2000TgtNode *stack[30];
239  int sp = 1, curval = 0;
240  stack[0] = node;
241 
242  node = node->parent;
243  while(node){
244  if (node->vis){
245  curval = node->val;
246  break;
247  }
248  node->vis++;
249  stack[sp++] = node;
250  node = node->parent;
251  }
252  while(--sp >= 0){
253  if (stack[sp]->val >= threshold){
254  put_bits(s, 0, threshold - curval);
255  break;
256  }
257  put_bits(s, 0, stack[sp]->val - curval);
258  put_bits(s, 1, 1);
259  curval = stack[sp]->val;
260  }
261 }
262 
263 /** update the value in node */
265 {
266  int lev = 0;
267  while (node->parent){
268  if (node->parent->val <= node->val)
269  break;
270  node->parent->val = node->val;
271  node = node->parent;
272  lev++;
273  }
274 }
275 
277 {
278  int i;
279 
280  if (s->buf_end - s->buf < 40 + 3 * s->ncomponents)
281  return -1;
282 
283  bytestream_put_be16(&s->buf, JPEG2000_SIZ);
284  bytestream_put_be16(&s->buf, 38 + 3 * s->ncomponents); // Lsiz
285  bytestream_put_be16(&s->buf, 0); // Rsiz
286  bytestream_put_be32(&s->buf, s->width); // width
287  bytestream_put_be32(&s->buf, s->height); // height
288  bytestream_put_be32(&s->buf, 0); // X0Siz
289  bytestream_put_be32(&s->buf, 0); // Y0Siz
290 
291  bytestream_put_be32(&s->buf, s->tile_width); // XTSiz
292  bytestream_put_be32(&s->buf, s->tile_height); // YTSiz
293  bytestream_put_be32(&s->buf, 0); // XT0Siz
294  bytestream_put_be32(&s->buf, 0); // YT0Siz
295  bytestream_put_be16(&s->buf, s->ncomponents); // CSiz
296 
297  for (i = 0; i < s->ncomponents; i++){ // Ssiz_i XRsiz_i, YRsiz_i
298  bytestream_put_byte(&s->buf, 7);
299  bytestream_put_byte(&s->buf, i?1<<s->chroma_shift[0]:1);
300  bytestream_put_byte(&s->buf, i?1<<s->chroma_shift[1]:1);
301  }
302  return 0;
303 }
304 
306 {
307  Jpeg2000CodingStyle *codsty = &s->codsty;
308 
309  if (s->buf_end - s->buf < 14)
310  return -1;
311 
312  bytestream_put_be16(&s->buf, JPEG2000_COD);
313  bytestream_put_be16(&s->buf, 12); // Lcod
314  bytestream_put_byte(&s->buf, 0); // Scod
315  // SGcod
316  bytestream_put_byte(&s->buf, 0); // progression level
317  bytestream_put_be16(&s->buf, 1); // num of layers
318  if(s->avctx->pix_fmt == AV_PIX_FMT_YUV444P){
319  bytestream_put_byte(&s->buf, 0); // unspecified
320  }else{
321  bytestream_put_byte(&s->buf, 0); // unspecified
322  }
323  // SPcod
324  bytestream_put_byte(&s->buf, codsty->nreslevels - 1); // num of decomp. levels
325  bytestream_put_byte(&s->buf, codsty->log2_cblk_width-2); // cblk width
326  bytestream_put_byte(&s->buf, codsty->log2_cblk_height-2); // cblk height
327  bytestream_put_byte(&s->buf, 0); // cblk style
328  bytestream_put_byte(&s->buf, codsty->transform == FF_DWT53); // transformation
329  return 0;
330 }
331 
332 static int put_qcd(Jpeg2000EncoderContext *s, int compno)
333 {
334  int i, size;
335  Jpeg2000CodingStyle *codsty = &s->codsty;
336  Jpeg2000QuantStyle *qntsty = &s->qntsty;
337 
338  if (qntsty->quantsty == JPEG2000_QSTY_NONE)
339  size = 4 + 3 * (codsty->nreslevels-1);
340  else // QSTY_SE
341  size = 5 + 6 * (codsty->nreslevels-1);
342 
343  if (s->buf_end - s->buf < size + 2)
344  return -1;
345 
346  bytestream_put_be16(&s->buf, JPEG2000_QCD);
347  bytestream_put_be16(&s->buf, size); // LQcd
348  bytestream_put_byte(&s->buf, (qntsty->nguardbits << 5) | qntsty->quantsty); // Sqcd
349  if (qntsty->quantsty == JPEG2000_QSTY_NONE)
350  for (i = 0; i < codsty->nreslevels * 3 - 2; i++)
351  bytestream_put_byte(&s->buf, qntsty->expn[i] << 3);
352  else // QSTY_SE
353  for (i = 0; i < codsty->nreslevels * 3 - 2; i++)
354  bytestream_put_be16(&s->buf, (qntsty->expn[i] << 11) | qntsty->mant[i]);
355  return 0;
356 }
357 
358 static int put_com(Jpeg2000EncoderContext *s, int compno)
359 {
360  int size = 4 + strlen(LIBAVCODEC_IDENT);
361 
363  return 0;
364 
365  if (s->buf_end - s->buf < size + 2)
366  return -1;
367 
368  bytestream_put_be16(&s->buf, JPEG2000_COM);
369  bytestream_put_be16(&s->buf, size);
370  bytestream_put_be16(&s->buf, 1); // General use (ISO/IEC 8859-15 (Latin) values)
371 
373 
374  return 0;
375 }
376 
377 static uint8_t *put_sot(Jpeg2000EncoderContext *s, int tileno)
378 {
379  uint8_t *psotptr;
380 
381  if (s->buf_end - s->buf < 12)
382  return NULL;
383 
384  bytestream_put_be16(&s->buf, JPEG2000_SOT);
385  bytestream_put_be16(&s->buf, 10); // Lsot
386  bytestream_put_be16(&s->buf, tileno); // Isot
387 
388  psotptr = s->buf;
389  bytestream_put_be32(&s->buf, 0); // Psot (filled in later)
390 
391  bytestream_put_byte(&s->buf, 0); // TPsot
392  bytestream_put_byte(&s->buf, 1); // TNsot
393  return psotptr;
394 }
395 
396 /**
397  * compute the sizes of tiles, resolution levels, bands, etc.
398  * allocate memory for them
399  * divide the input image into tile-components
400  */
402 {
403  int tileno, tilex, tiley, compno;
404  Jpeg2000CodingStyle *codsty = &s->codsty;
405  Jpeg2000QuantStyle *qntsty = &s->qntsty;
406 
409 
410  s->tile = av_malloc_array(s->numXtiles, s->numYtiles * sizeof(Jpeg2000Tile));
411  if (!s->tile)
412  return AVERROR(ENOMEM);
413  for (tileno = 0, tiley = 0; tiley < s->numYtiles; tiley++)
414  for (tilex = 0; tilex < s->numXtiles; tilex++, tileno++){
415  Jpeg2000Tile *tile = s->tile + tileno;
416 
417  tile->comp = av_mallocz_array(s->ncomponents, sizeof(Jpeg2000Component));
418  if (!tile->comp)
419  return AVERROR(ENOMEM);
420  for (compno = 0; compno < s->ncomponents; compno++){
421  Jpeg2000Component *comp = tile->comp + compno;
422  int ret, i, j;
423 
424  comp->coord[0][0] = comp->coord_o[0][0] = tilex * s->tile_width;
425  comp->coord[0][1] = comp->coord_o[0][1] = FFMIN((tilex+1)*s->tile_width, s->width);
426  comp->coord[1][0] = comp->coord_o[1][0] = tiley * s->tile_height;
427  comp->coord[1][1] = comp->coord_o[1][1] = FFMIN((tiley+1)*s->tile_height, s->height);
428  if (compno > 0)
429  for (i = 0; i < 2; i++)
430  for (j = 0; j < 2; j++)
431  comp->coord[i][j] = comp->coord_o[i][j] = ff_jpeg2000_ceildivpow2(comp->coord[i][j], s->chroma_shift[i]);
432 
433  if ((ret = ff_jpeg2000_init_component(comp,
434  codsty,
435  qntsty,
436  s->cbps[compno],
437  compno?1<<s->chroma_shift[0]:1,
438  compno?1<<s->chroma_shift[1]:1,
439  s->avctx
440  )) < 0)
441  return ret;
442  }
443  }
444  return 0;
445 }
446 
448 {
449  int tileno, compno, i, y, x;
450  uint8_t *line;
451  for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
452  Jpeg2000Tile *tile = s->tile + tileno;
453  if (s->planar){
454  for (compno = 0; compno < s->ncomponents; compno++){
455  Jpeg2000Component *comp = tile->comp + compno;
456  int *dst = comp->i_data;
457  line = s->picture->data[compno]
458  + comp->coord[1][0] * s->picture->linesize[compno]
459  + comp->coord[0][0];
460  for (y = comp->coord[1][0]; y < comp->coord[1][1]; y++){
461  uint8_t *ptr = line;
462  for (x = comp->coord[0][0]; x < comp->coord[0][1]; x++)
463  *dst++ = *ptr++ - (1 << 7);
464  line += s->picture->linesize[compno];
465  }
466  }
467  } else{
468  line = s->picture->data[0] + tile->comp[0].coord[1][0] * s->picture->linesize[0]
469  + tile->comp[0].coord[0][0] * s->ncomponents;
470 
471  i = 0;
472  for (y = tile->comp[0].coord[1][0]; y < tile->comp[0].coord[1][1]; y++){
473  uint8_t *ptr = line;
474  for (x = tile->comp[0].coord[0][0]; x < tile->comp[0].coord[0][1]; x++, i++){
475  for (compno = 0; compno < s->ncomponents; compno++){
476  tile->comp[compno].i_data[i] = *ptr++ - (1 << 7);
477  }
478  }
479  line += s->picture->linesize[0];
480  }
481  }
482  }
483 }
484 
486 {
487  int compno, reslevelno, bandno;
488  Jpeg2000QuantStyle *qntsty = &s->qntsty;
489  Jpeg2000CodingStyle *codsty = &s->codsty;
490 
491  for (compno = 0; compno < s->ncomponents; compno++){
492  int gbandno = 0;
493  for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
494  int nbands, lev = codsty->nreslevels - reslevelno - 1;
495  nbands = reslevelno ? 3 : 1;
496  for (bandno = 0; bandno < nbands; bandno++, gbandno++){
497  int expn, mant = 0;
498 
499  if (codsty->transform == FF_DWT97_INT){
500  int bandpos = bandno + (reslevelno>0),
501  ss = 81920000 / dwt_norms[0][bandpos][lev],
502  log = av_log2(ss);
503  mant = (11 - log < 0 ? ss >> log - 11 : ss << 11 - log) & 0x7ff;
504  expn = s->cbps[compno] - log + 13;
505  } else
506  expn = ((bandno&2)>>1) + (reslevelno>0) + s->cbps[compno];
507 
508  qntsty->expn[gbandno] = expn;
509  qntsty->mant[gbandno] = mant;
510  }
511  }
512  }
513 }
514 
515 static void init_luts(void)
516 {
517  int i, a,
518  mask = ~((1<<NMSEDEC_FRACBITS)-1);
519 
520  for (i = 0; i < (1 << NMSEDEC_BITS); i++){
521  lut_nmsedec_sig[i] = FFMAX(6*i - (9<<NMSEDEC_FRACBITS-1) << 12-NMSEDEC_FRACBITS, 0);
522  lut_nmsedec_sig0[i] = FFMAX((i*i + (1<<NMSEDEC_FRACBITS-1) & mask) << 1, 0);
523 
524  a = (i >> (NMSEDEC_BITS-2)&2) + 1;
525  lut_nmsedec_ref[i] = FFMAX((-2*i + (1<<NMSEDEC_FRACBITS) + a*i - (a*a<<NMSEDEC_FRACBITS-2))
526  << 13-NMSEDEC_FRACBITS, 0);
527  lut_nmsedec_ref0[i] = FFMAX(((i*i + (1-4*i << NMSEDEC_FRACBITS-1) + (1<<2*NMSEDEC_FRACBITS)) & mask)
528  << 1, 0);
529  }
530 }
531 
532 /* tier-1 routines */
533 static int getnmsedec_sig(int x, int bpno)
534 {
535  if (bpno > NMSEDEC_FRACBITS)
536  return lut_nmsedec_sig[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
537  return lut_nmsedec_sig0[x & ((1 << NMSEDEC_BITS) - 1)];
538 }
539 
540 static int getnmsedec_ref(int x, int bpno)
541 {
542  if (bpno > NMSEDEC_FRACBITS)
543  return lut_nmsedec_ref[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
544  return lut_nmsedec_ref0[x & ((1 << NMSEDEC_BITS) - 1)];
545 }
546 
547 static void encode_sigpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
548 {
549  int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
550  for (y0 = 0; y0 < height; y0 += 4)
551  for (x = 0; x < width; x++)
552  for (y = y0; y < height && y < y0+4; y++){
553  if (!(t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG) && (t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG_NB)){
554  int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno),
555  bit = t1->data[(y) * t1->stride + x] & mask ? 1 : 0;
556  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, bit);
557  if (bit){
558  int xorbit;
559  int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
560  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
561  *nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
562  ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
563  }
564  t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_VIS;
565  }
566  }
567 }
568 
569 static void encode_refpass(Jpeg2000T1Context *t1, int width, int height, int *nmsedec, int bpno)
570 {
571  int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
572  for (y0 = 0; y0 < height; y0 += 4)
573  for (x = 0; x < width; x++)
574  for (y = y0; y < height && y < y0+4; y++)
575  if ((t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG){
576  int ctxno = ff_jpeg2000_getrefctxno(t1->flags[(y+1) * t1->stride + x+1]);
577  *nmsedec += getnmsedec_ref(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
578  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
579  t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_REF;
580  }
581 }
582 
583 static void encode_clnpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
584 {
585  int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
586  for (y0 = 0; y0 < height; y0 += 4)
587  for (x = 0; x < width; x++){
588  if (y0 + 3 < height && !(
589  (t1->flags[(y0+1) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
590  (t1->flags[(y0+2) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
591  (t1->flags[(y0+3) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
592  (t1->flags[(y0+4) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG))))
593  {
594  // aggregation mode
595  int rlen;
596  for (rlen = 0; rlen < 4; rlen++)
597  if (t1->data[(y0+rlen) * t1->stride + x] & mask)
598  break;
599  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL, rlen != 4);
600  if (rlen == 4)
601  continue;
602  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen >> 1);
603  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen & 1);
604  for (y = y0 + rlen; y < y0 + 4; y++){
605  if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))){
606  int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno);
607  if (y > y0 + rlen)
608  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
609  if (t1->data[(y) * t1->stride + x] & mask){ // newly significant
610  int xorbit;
611  int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
612  *nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
613  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
614  ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
615  }
616  }
617  t1->flags[(y+1) * t1->stride + x+1] &= ~JPEG2000_T1_VIS;
618  }
619  } else{
620  for (y = y0; y < y0 + 4 && y < height; y++){
621  if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))){
622  int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno);
623  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
624  if (t1->data[(y) * t1->stride + x] & mask){ // newly significant
625  int xorbit;
626  int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
627  *nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
628  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
629  ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
630  }
631  }
632  t1->flags[(y+1) * t1->stride + x+1] &= ~JPEG2000_T1_VIS;
633  }
634  }
635  }
636 }
637 
639  int width, int height, int bandpos, int lev)
640 {
641  int pass_t = 2, passno, x, y, max=0, nmsedec, bpno;
642  int64_t wmsedec = 0;
643 
644  memset(t1->flags, 0, t1->stride * (height + 2) * sizeof(*t1->flags));
645 
646  for (y = 0; y < height; y++){
647  for (x = 0; x < width; x++){
648  if (t1->data[(y) * t1->stride + x] < 0){
649  t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_SGN;
650  t1->data[(y) * t1->stride + x] = -t1->data[(y) * t1->stride + x];
651  }
652  max = FFMAX(max, t1->data[(y) * t1->stride + x]);
653  }
654  }
655 
656  if (max == 0){
657  cblk->nonzerobits = 0;
658  bpno = 0;
659  } else{
660  cblk->nonzerobits = av_log2(max) + 1 - NMSEDEC_FRACBITS;
661  bpno = cblk->nonzerobits - 1;
662  }
663 
664  ff_mqc_initenc(&t1->mqc, cblk->data);
665 
666  for (passno = 0; bpno >= 0; passno++){
667  nmsedec=0;
668 
669  switch(pass_t){
670  case 0: encode_sigpass(t1, width, height, bandpos, &nmsedec, bpno);
671  break;
672  case 1: encode_refpass(t1, width, height, &nmsedec, bpno);
673  break;
674  case 2: encode_clnpass(t1, width, height, bandpos, &nmsedec, bpno);
675  break;
676  }
677 
678  cblk->passes[passno].rate = ff_mqc_flush_to(&t1->mqc, cblk->passes[passno].flushed, &cblk->passes[passno].flushed_len);
679  wmsedec += (int64_t)nmsedec << (2*bpno);
680  cblk->passes[passno].disto = wmsedec;
681 
682  if (++pass_t == 3){
683  pass_t = 0;
684  bpno--;
685  }
686  }
687  cblk->npasses = passno;
688  cblk->ninclpasses = passno;
689 
690  if (passno)
691  cblk->passes[passno-1].rate = ff_mqc_flush_to(&t1->mqc, cblk->passes[passno-1].flushed, &cblk->passes[passno-1].flushed_len);
692 }
693 
694 /* tier-2 routines: */
695 
697 {
698  if (n == 1)
699  put_num(s, 0, 1);
700  else if (n == 2)
701  put_num(s, 2, 2);
702  else if (n <= 5)
703  put_num(s, 0xc | (n-3), 4);
704  else if (n <= 36)
705  put_num(s, 0x1e0 | (n-6), 9);
706  else
707  put_num(s, 0xff80 | (n-37), 16);
708 }
709 
710 
711 static int encode_packet(Jpeg2000EncoderContext *s, Jpeg2000ResLevel *rlevel, int precno,
712  uint8_t *expn, int numgbits)
713 {
714  int bandno, empty = 1;
715 
716  // init bitstream
717  *s->buf = 0;
718  s->bit_index = 0;
719 
720  // header
721 
722  // is the packet empty?
723  for (bandno = 0; bandno < rlevel->nbands; bandno++){
724  if (rlevel->band[bandno].coord[0][0] < rlevel->band[bandno].coord[0][1]
725  && rlevel->band[bandno].coord[1][0] < rlevel->band[bandno].coord[1][1]){
726  empty = 0;
727  break;
728  }
729  }
730 
731  put_bits(s, !empty, 1);
732  if (empty){
733  j2k_flush(s);
734  return 0;
735  }
736 
737  for (bandno = 0; bandno < rlevel->nbands; bandno++){
738  Jpeg2000Band *band = rlevel->band + bandno;
739  Jpeg2000Prec *prec = band->prec + precno;
740  int yi, xi, pos;
741  int cblknw = prec->nb_codeblocks_width;
742 
743  if (band->coord[0][0] == band->coord[0][1]
744  || band->coord[1][0] == band->coord[1][1])
745  continue;
746 
747  for (pos=0, yi = 0; yi < prec->nb_codeblocks_height; yi++){
748  for (xi = 0; xi < cblknw; xi++, pos++){
749  prec->cblkincl[pos].val = prec->cblk[yi * cblknw + xi].ninclpasses == 0;
750  tag_tree_update(prec->cblkincl + pos);
751  prec->zerobits[pos].val = expn[bandno] + numgbits - 1 - prec->cblk[yi * cblknw + xi].nonzerobits;
752  tag_tree_update(prec->zerobits + pos);
753  }
754  }
755 
756  for (pos=0, yi = 0; yi < prec->nb_codeblocks_height; yi++){
757  for (xi = 0; xi < cblknw; xi++, pos++){
758  int pad = 0, llen, length;
759  Jpeg2000Cblk *cblk = prec->cblk + yi * cblknw + xi;
760 
761  if (s->buf_end - s->buf < 20) // approximately
762  return -1;
763 
764  // inclusion information
765  tag_tree_code(s, prec->cblkincl + pos, 1);
766  if (!cblk->ninclpasses)
767  continue;
768  // zerobits information
769  tag_tree_code(s, prec->zerobits + pos, 100);
770  // number of passes
771  putnumpasses(s, cblk->ninclpasses);
772 
773  length = cblk->passes[cblk->ninclpasses-1].rate;
774  llen = av_log2(length) - av_log2(cblk->ninclpasses) - 2;
775  if (llen < 0){
776  pad = -llen;
777  llen = 0;
778  }
779  // length of code block
780  put_bits(s, 1, llen);
781  put_bits(s, 0, 1);
782  put_num(s, length, av_log2(length)+1+pad);
783  }
784  }
785  }
786  j2k_flush(s);
787  for (bandno = 0; bandno < rlevel->nbands; bandno++){
788  Jpeg2000Band *band = rlevel->band + bandno;
789  Jpeg2000Prec *prec = band->prec + precno;
790  int yi, cblknw = prec->nb_codeblocks_width;
791  for (yi =0; yi < prec->nb_codeblocks_height; yi++){
792  int xi;
793  for (xi = 0; xi < cblknw; xi++){
794  Jpeg2000Cblk *cblk = prec->cblk + yi * cblknw + xi;
795  if (cblk->ninclpasses){
796  if (s->buf_end - s->buf < cblk->passes[cblk->ninclpasses-1].rate)
797  return -1;
798  bytestream_put_buffer(&s->buf, cblk->data, cblk->passes[cblk->ninclpasses-1].rate
799  - cblk->passes[cblk->ninclpasses-1].flushed_len);
800  bytestream_put_buffer(&s->buf, cblk->passes[cblk->ninclpasses-1].flushed,
801  cblk->passes[cblk->ninclpasses-1].flushed_len);
802  }
803  }
804  }
805  }
806  return 0;
807 }
808 
809 static int encode_packets(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno)
810 {
811  int compno, reslevelno, ret;
812  Jpeg2000CodingStyle *codsty = &s->codsty;
813  Jpeg2000QuantStyle *qntsty = &s->qntsty;
814 
815  av_log(s->avctx, AV_LOG_DEBUG, "tier2\n");
816  // lay-rlevel-comp-pos progression
817  for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
818  for (compno = 0; compno < s->ncomponents; compno++){
819  int precno;
820  Jpeg2000ResLevel *reslevel = s->tile[tileno].comp[compno].reslevel + reslevelno;
821  for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
822  if ((ret = encode_packet(s, reslevel, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
823  qntsty->nguardbits)) < 0)
824  return ret;
825  }
826  }
827  }
828  av_log(s->avctx, AV_LOG_DEBUG, "after tier2\n");
829  return 0;
830 }
831 
832 static int getcut(Jpeg2000Cblk *cblk, int64_t lambda, int dwt_norm)
833 {
834  int passno, res = 0;
835  for (passno = 0; passno < cblk->npasses; passno++){
836  int dr;
837  int64_t dd;
838 
839  dr = cblk->passes[passno].rate
840  - (res ? cblk->passes[res-1].rate:0);
841  dd = cblk->passes[passno].disto
842  - (res ? cblk->passes[res-1].disto:0);
843 
844  if (((dd * dwt_norm) >> WMSEDEC_SHIFT) * dwt_norm >= dr * lambda)
845  res = passno+1;
846  }
847  return res;
848 }
849 
851 {
852  int precno, compno, reslevelno, bandno, cblkno, lev;
853  Jpeg2000CodingStyle *codsty = &s->codsty;
854 
855  for (compno = 0; compno < s->ncomponents; compno++){
856  Jpeg2000Component *comp = tile->comp + compno;
857 
858  for (reslevelno = 0, lev = codsty->nreslevels-1; reslevelno < codsty->nreslevels; reslevelno++, lev--){
859  Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
860 
861  for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
862  for (bandno = 0; bandno < reslevel->nbands ; bandno++){
863  int bandpos = bandno + (reslevelno > 0);
864  Jpeg2000Band *band = reslevel->band + bandno;
865  Jpeg2000Prec *prec = band->prec + precno;
866 
867  for (cblkno = 0; cblkno < prec->nb_codeblocks_height * prec->nb_codeblocks_width; cblkno++){
868  Jpeg2000Cblk *cblk = prec->cblk + cblkno;
869 
870  cblk->ninclpasses = getcut(cblk, s->lambda,
871  (int64_t)dwt_norms[codsty->transform == FF_DWT53][bandpos][lev] * (int64_t)band->i_stepsize >> 15);
872  }
873  }
874  }
875  }
876  }
877 }
878 
879 static int encode_tile(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno)
880 {
881  int compno, reslevelno, bandno, ret;
883  Jpeg2000CodingStyle *codsty = &s->codsty;
884  for (compno = 0; compno < s->ncomponents; compno++){
885  Jpeg2000Component *comp = s->tile[tileno].comp + compno;
886 
887  t1.stride = (1<<codsty->log2_cblk_width) + 2;
888 
889  av_log(s->avctx, AV_LOG_DEBUG,"dwt\n");
890  if ((ret = ff_dwt_encode(&comp->dwt, comp->i_data)) < 0)
891  return ret;
892  av_log(s->avctx, AV_LOG_DEBUG,"after dwt -> tier1\n");
893 
894  for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
895  Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
896 
897  for (bandno = 0; bandno < reslevel->nbands ; bandno++){
898  Jpeg2000Band *band = reslevel->band + bandno;
899  Jpeg2000Prec *prec = band->prec; // we support only 1 precinct per band ATM in the encoder
900  int cblkx, cblky, cblkno=0, xx0, x0, xx1, y0, yy0, yy1, bandpos;
901  yy0 = bandno == 0 ? 0 : comp->reslevel[reslevelno-1].coord[1][1] - comp->reslevel[reslevelno-1].coord[1][0];
902  y0 = yy0;
903  yy1 = FFMIN(ff_jpeg2000_ceildivpow2(band->coord[1][0] + 1, band->log2_cblk_height) << band->log2_cblk_height,
904  band->coord[1][1]) - band->coord[1][0] + yy0;
905 
906  if (band->coord[0][0] == band->coord[0][1] || band->coord[1][0] == band->coord[1][1])
907  continue;
908 
909  bandpos = bandno + (reslevelno > 0);
910 
911  for (cblky = 0; cblky < prec->nb_codeblocks_height; cblky++){
912  if (reslevelno == 0 || bandno == 1)
913  xx0 = 0;
914  else
915  xx0 = comp->reslevel[reslevelno-1].coord[0][1] - comp->reslevel[reslevelno-1].coord[0][0];
916  x0 = xx0;
917  xx1 = FFMIN(ff_jpeg2000_ceildivpow2(band->coord[0][0] + 1, band->log2_cblk_width) << band->log2_cblk_width,
918  band->coord[0][1]) - band->coord[0][0] + xx0;
919 
920  for (cblkx = 0; cblkx < prec->nb_codeblocks_width; cblkx++, cblkno++){
921  int y, x;
922  if (codsty->transform == FF_DWT53){
923  for (y = yy0; y < yy1; y++){
924  int *ptr = t1.data + (y-yy0)*t1.stride;
925  for (x = xx0; x < xx1; x++){
926  *ptr++ = comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * y + x] << NMSEDEC_FRACBITS;
927  }
928  }
929  } else{
930  for (y = yy0; y < yy1; y++){
931  int *ptr = t1.data + (y-yy0)*t1.stride;
932  for (x = xx0; x < xx1; x++){
933  *ptr = (comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * y + x]);
934  *ptr = (int64_t)*ptr * (int64_t)(16384 * 65536 / band->i_stepsize) >> 15 - NMSEDEC_FRACBITS;
935  ptr++;
936  }
937  }
938  }
939  encode_cblk(s, &t1, prec->cblk + cblkno, tile, xx1 - xx0, yy1 - yy0,
940  bandpos, codsty->nreslevels - reslevelno - 1);
941  xx0 = xx1;
942  xx1 = FFMIN(xx1 + (1 << band->log2_cblk_width), band->coord[0][1] - band->coord[0][0] + x0);
943  }
944  yy0 = yy1;
945  yy1 = FFMIN(yy1 + (1 << band->log2_cblk_height), band->coord[1][1] - band->coord[1][0] + y0);
946  }
947  }
948  }
949  av_log(s->avctx, AV_LOG_DEBUG, "after tier1\n");
950  }
951 
952  av_log(s->avctx, AV_LOG_DEBUG, "rate control\n");
953  truncpasses(s, tile);
954  if ((ret = encode_packets(s, tile, tileno)) < 0)
955  return ret;
956  av_log(s->avctx, AV_LOG_DEBUG, "after rate control\n");
957  return 0;
958 }
959 
961 {
962  int tileno, compno;
963  Jpeg2000CodingStyle *codsty = &s->codsty;
964 
965  for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
966  for (compno = 0; compno < s->ncomponents; compno++){
967  Jpeg2000Component *comp = s->tile[tileno].comp + compno;
968  ff_jpeg2000_cleanup(comp, codsty);
969  }
970  av_freep(&s->tile[tileno].comp);
971  }
972  av_freep(&s->tile);
973 }
974 
976 {
977  int tileno, compno;
978  for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
979  Jpeg2000Tile *tile = s->tile + tileno;
980  for (compno = 0; compno < s->ncomponents; compno++)
981  ff_jpeg2000_reinit(tile->comp + compno, &s->codsty);
982  }
983 }
984 
985 static void update_size(uint8_t *size, const uint8_t *end)
986 {
987  AV_WB32(size, end-size);
988 }
989 
991  const AVFrame *pict, int *got_packet)
992 {
993  int tileno, ret;
994  Jpeg2000EncoderContext *s = avctx->priv_data;
995  uint8_t *chunkstart, *jp2cstart, *jp2hstart;
996 
997  if ((ret = ff_alloc_packet2(avctx, pkt, avctx->width*avctx->height*9 + AV_INPUT_BUFFER_MIN_SIZE, 0)) < 0)
998  return ret;
999 
1000  // init:
1001  s->buf = s->buf_start = pkt->data;
1002  s->buf_end = pkt->data + pkt->size;
1003 
1004  s->picture = pict;
1005 
1006  s->lambda = s->picture->quality * LAMBDA_SCALE;
1007 
1008  copy_frame(s);
1009  reinit(s);
1010 
1011  if (s->format == CODEC_JP2) {
1012  av_assert0(s->buf == pkt->data);
1013 
1014  bytestream_put_be32(&s->buf, 0x0000000C);
1015  bytestream_put_be32(&s->buf, 0x6A502020);
1016  bytestream_put_be32(&s->buf, 0x0D0A870A);
1017 
1018  chunkstart = s->buf;
1019  bytestream_put_be32(&s->buf, 0);
1020  bytestream_put_buffer(&s->buf, "ftyp", 4);
1021  bytestream_put_buffer(&s->buf, "jp2\040\040", 4);
1022  bytestream_put_be32(&s->buf, 0);
1023  bytestream_put_buffer(&s->buf, "jp2\040", 4);
1024  update_size(chunkstart, s->buf);
1025 
1026  jp2hstart = s->buf;
1027  bytestream_put_be32(&s->buf, 0);
1028  bytestream_put_buffer(&s->buf, "jp2h", 4);
1029 
1030  chunkstart = s->buf;
1031  bytestream_put_be32(&s->buf, 0);
1032  bytestream_put_buffer(&s->buf, "ihdr", 4);
1033  bytestream_put_be32(&s->buf, avctx->height);
1034  bytestream_put_be32(&s->buf, avctx->width);
1035  bytestream_put_be16(&s->buf, s->ncomponents);
1036  bytestream_put_byte(&s->buf, s->cbps[0]);
1037  bytestream_put_byte(&s->buf, 7);
1038  bytestream_put_byte(&s->buf, 0);
1039  bytestream_put_byte(&s->buf, 0);
1040  update_size(chunkstart, s->buf);
1041 
1042  chunkstart = s->buf;
1043  bytestream_put_be32(&s->buf, 0);
1044  bytestream_put_buffer(&s->buf, "colr", 4);
1045  bytestream_put_byte(&s->buf, 1);
1046  bytestream_put_byte(&s->buf, 0);
1047  bytestream_put_byte(&s->buf, 0);
1048  if (s->ncomponents == 1) {
1049  bytestream_put_be32(&s->buf, 17);
1050  } else if (avctx->pix_fmt == AV_PIX_FMT_RGB24) {
1051  bytestream_put_be32(&s->buf, 16);
1052  } else {
1053  bytestream_put_be32(&s->buf, 18);
1054  }
1055  update_size(chunkstart, s->buf);
1056  update_size(jp2hstart, s->buf);
1057 
1058  jp2cstart = s->buf;
1059  bytestream_put_be32(&s->buf, 0);
1060  bytestream_put_buffer(&s->buf, "jp2c", 4);
1061  }
1062 
1063  if (s->buf_end - s->buf < 2)
1064  return -1;
1065  bytestream_put_be16(&s->buf, JPEG2000_SOC);
1066  if ((ret = put_siz(s)) < 0)
1067  return ret;
1068  if ((ret = put_cod(s)) < 0)
1069  return ret;
1070  if ((ret = put_qcd(s, 0)) < 0)
1071  return ret;
1072  if ((ret = put_com(s, 0)) < 0)
1073  return ret;
1074 
1075  for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
1076  uint8_t *psotptr;
1077  if (!(psotptr = put_sot(s, tileno)))
1078  return -1;
1079  if (s->buf_end - s->buf < 2)
1080  return -1;
1081  bytestream_put_be16(&s->buf, JPEG2000_SOD);
1082  if ((ret = encode_tile(s, s->tile + tileno, tileno)) < 0)
1083  return ret;
1084  bytestream_put_be32(&psotptr, s->buf - psotptr + 6);
1085  }
1086  if (s->buf_end - s->buf < 2)
1087  return -1;
1088  bytestream_put_be16(&s->buf, JPEG2000_EOC);
1089 
1090  if (s->format == CODEC_JP2)
1091  update_size(jp2cstart, s->buf);
1092 
1093  av_log(s->avctx, AV_LOG_DEBUG, "end\n");
1094  pkt->size = s->buf - s->buf_start;
1095  pkt->flags |= AV_PKT_FLAG_KEY;
1096  *got_packet = 1;
1097 
1098  return 0;
1099 }
1100 
1102 {
1103  int i, ret;
1104  Jpeg2000EncoderContext *s = avctx->priv_data;
1105  Jpeg2000CodingStyle *codsty = &s->codsty;
1106  Jpeg2000QuantStyle *qntsty = &s->qntsty;
1107 
1108  s->avctx = avctx;
1109  av_log(s->avctx, AV_LOG_DEBUG, "init\n");
1110 
1111  // defaults:
1112  // TODO: implement setting non-standard precinct size
1113  memset(codsty->log2_prec_widths , 15, sizeof(codsty->log2_prec_widths ));
1114  memset(codsty->log2_prec_heights, 15, sizeof(codsty->log2_prec_heights));
1115  codsty->nreslevels2decode=
1116  codsty->nreslevels = 7;
1117  codsty->log2_cblk_width = 4;
1118  codsty->log2_cblk_height = 4;
1119  codsty->transform = avctx->prediction_method ? FF_DWT53 : FF_DWT97_INT;
1120 
1121  qntsty->nguardbits = 1;
1122 
1123  if ((s->tile_width & (s->tile_width -1)) ||
1124  (s->tile_height & (s->tile_height-1))) {
1125  av_log(avctx, AV_LOG_WARNING, "Tile dimension not a power of 2\n");
1126  }
1127 
1128  if (codsty->transform == FF_DWT53)
1129  qntsty->quantsty = JPEG2000_QSTY_NONE;
1130  else
1131  qntsty->quantsty = JPEG2000_QSTY_SE;
1132 
1133  s->width = avctx->width;
1134  s->height = avctx->height;
1135 
1136  for (i = 0; i < 3; i++)
1137  s->cbps[i] = 8;
1138 
1139  if (avctx->pix_fmt == AV_PIX_FMT_RGB24){
1140  s->ncomponents = 3;
1141  } else if (avctx->pix_fmt == AV_PIX_FMT_GRAY8){
1142  s->ncomponents = 1;
1143  } else{ // planar YUV
1144  s->planar = 1;
1145  s->ncomponents = 3;
1147  s->chroma_shift, s->chroma_shift + 1);
1148  }
1149 
1152  init_luts();
1153 
1154  init_quantization(s);
1155  if ((ret=init_tiles(s)) < 0)
1156  return ret;
1157 
1158  av_log(s->avctx, AV_LOG_DEBUG, "after init\n");
1159 
1160  return 0;
1161 }
1162 
1163 static int j2kenc_destroy(AVCodecContext *avctx)
1164 {
1165  Jpeg2000EncoderContext *s = avctx->priv_data;
1166 
1167  cleanup(s);
1168  return 0;
1169 }
1170 
1171 // taken from the libopenjpeg wraper so it matches
1172 
1173 #define OFFSET(x) offsetof(Jpeg2000EncoderContext, x)
1174 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1175 static const AVOption options[] = {
1176  { "format", "Codec Format", OFFSET(format), AV_OPT_TYPE_INT, { .i64 = CODEC_JP2 }, CODEC_J2K, CODEC_JP2, VE, "format" },
1177  { "j2k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CODEC_J2K }, 0, 0, VE, "format" },
1178  { "jp2", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CODEC_JP2 }, 0, 0, VE, "format" },
1179  { "tile_width", "Tile Width", OFFSET(tile_width), AV_OPT_TYPE_INT, { .i64 = 256 }, 1, 1<<30, VE, },
1180  { "tile_height", "Tile Height", OFFSET(tile_height), AV_OPT_TYPE_INT, { .i64 = 256 }, 1, 1<<30, VE, },
1181 
1182  { NULL }
1183 };
1184 
1185 static const AVClass j2k_class = {
1186  .class_name = "jpeg 2000 encoder",
1187  .item_name = av_default_item_name,
1188  .option = options,
1189  .version = LIBAVUTIL_VERSION_INT,
1190 };
1191 
1193  .name = "jpeg2000",
1194  .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
1195  .type = AVMEDIA_TYPE_VIDEO,
1196  .id = AV_CODEC_ID_JPEG2000,
1197  .priv_data_size = sizeof(Jpeg2000EncoderContext),
1198  .init = j2kenc_init,
1199  .encode2 = encode_frame,
1200  .close = j2kenc_destroy,
1201  .pix_fmts = (const enum AVPixelFormat[]) {
1206  },
1207  .priv_class = &j2k_class,
1208 };
static int getcut(Jpeg2000Cblk *cblk, int64_t lambda, int dwt_norm)
Definition: j2kenc.c:832
uint8_t nguardbits
Definition: jpeg2000.h:153
#define NULL
Definition: coverity.c:32
const char const char void * val
Definition: avisynth_c.h:634
const char * s
Definition: avisynth_c.h:631
#define NMSEDEC_BITS
Definition: j2kenc.c:74
void av_cold ff_jpeg2000_init_tier1_luts(void)
Definition: jpeg2000.c:158
void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
Definition: jpeg2000.c:532
This structure describes decoded (raw) audio or video data.
Definition: frame.h:171
DWTContext dwt
Definition: jpeg2000.h:208
AVOption.
Definition: opt.h:255
Jpeg2000TgtNode * cblkincl
Definition: jpeg2000.h:184
static void cleanup(Jpeg2000EncoderContext *s)
Definition: j2kenc.c:960
#define JPEG2000_T1_SIG_NB
Definition: jpeg2000.h:85
AVCodec ff_jpeg2000_encoder
Definition: j2kenc.c:1192
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:72
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:205
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
#define LIBAVUTIL_VERSION_INT
Definition: version.h:62
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:69
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
uint8_t ninclpasses
Definition: jpeg2000.h:165
Jpeg2000QuantStyle qntsty
Definition: j2kenc.c:124
static int lut_nmsedec_ref[1<< NMSEDEC_BITS]
Definition: j2kenc.c:82
static av_cold int j2kenc_init(AVCodecContext *avctx)
Definition: j2kenc.c:1101
int size
Definition: avcodec.h:1434
int nb_codeblocks_width
Definition: jpeg2000.h:181
static int put_com(Jpeg2000EncoderContext *s, int compno)
Definition: j2kenc.c:358
AVCodecContext * avctx
Definition: j2kenc.c:105
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1732
uint16_t mant[JPEG2000_MAX_DECLEVELS *3]
Definition: jpeg2000.h:151
void ff_mqc_initenc(MqcState *mqc, uint8_t *bp)
initialize the encoder
Definition: mqcenc.c:69
static void tag_tree_code(Jpeg2000EncoderContext *s, Jpeg2000TgtNode *node, int threshold)
code the value stored in node
Definition: j2kenc.c:236
static AVPacket pkt
AVCodec.
Definition: avcodec.h:3482
static void tag_tree_update(Jpeg2000TgtNode *node)
update the value in node
Definition: j2kenc.c:264
static void update_size(uint8_t *size, const uint8_t *end)
Definition: j2kenc.c:985
static int getnmsedec_ref(int x, int bpno)
Definition: j2kenc.c:540
uint8_t npasses
Definition: jpeg2000.h:164
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
uint16_t log2_cblk_width
Definition: jpeg2000.h:192
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
uint8_t
#define av_cold
Definition: attributes.h:74
static int lut_nmsedec_ref0[1<< NMSEDEC_BITS]
Definition: j2kenc.c:82
AVOptions.
static int encode_packet(Jpeg2000EncoderContext *s, Jpeg2000ResLevel *rlevel, int precno, uint8_t *expn, int numgbits)
Definition: j2kenc.c:711
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
static const AVClass j2k_class
Definition: j2kenc.c:1185
Jpeg2000TgtNode * zerobits
Definition: jpeg2000.h:183
int64_t disto
Definition: jpeg2000.h:158
Jpeg2000Band * band
Definition: jpeg2000.h:203
uint8_t val
Definition: jpeg2000.h:129
int coord[2][2]
Definition: jpeg2000.h:211
uint8_t * data
Definition: avcodec.h:1433
static void j2k_flush(Jpeg2000EncoderContext *s)
flush the bitstream
Definition: j2kenc.c:225
#define sp
Definition: regdef.h:63
static int init_tiles(Jpeg2000EncoderContext *s)
compute the sizes of tiles, resolution levels, bands, etc.
Definition: j2kenc.c:401
#define JPEG2000_T1_VIS
Definition: jpeg2000.h:95
ptrdiff_t size
Definition: opengl_enc.c:101
#define AV_INPUT_BUFFER_MIN_SIZE
minimum encoding buffer size Used to avoid some checks during header writing.
Definition: avcodec.h:643
uint16_t flags[6156]
Definition: jpeg2000.h:123
#define av_log(a,...)
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1479
uint8_t nonzerobits
Definition: jpeg2000.h:166
uint8_t log2_prec_widths[JPEG2000_MAX_RESLEVELS]
Definition: jpeg2000.h:145
static const uint16_t mask[17]
Definition: lzw.c:38
uint8_t * buf_end
Definition: j2kenc.c:118
#define OFFSET(x)
Definition: j2kenc.c:1173
void ff_jpeg2000_set_significance(Jpeg2000T1Context *t1, int x, int y, int negative)
Definition: jpeg2000.c:170
av_default_item_name
#define AVERROR(e)
Definition: error.h:43
static int put_cod(Jpeg2000EncoderContext *s)
Definition: j2kenc.c:305
int nb_codeblocks_height
Definition: jpeg2000.h:182
#define CODEC_J2K
Definition: j2kenc.c:80
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:178
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
uint8_t log2_prec_heights[JPEG2000_MAX_RESLEVELS]
Definition: jpeg2000.h:146
#define t1
Definition: regdef.h:29
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1607
Definition: graph2dot.c:48
static void put_num(Jpeg2000EncoderContext *s, int num, int n)
put n least significant bits of a number num
Definition: j2kenc.c:218
GLsizei GLsizei * length
Definition: opengl_enc.c:115
const char * name
Name of the codec implementation.
Definition: avcodec.h:3489
static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
Definition: j2kenc.c:990
const AVFrame * picture
Definition: j2kenc.c:106
uint8_t flushed[4]
Definition: jpeg2000.h:159
Jpeg2000Cblk * cblk
Definition: jpeg2000.h:185
#define FFMAX(a, b)
Definition: common.h:90
Libavcodec external API header.
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1439
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:71
static int put_siz(Jpeg2000EncoderContext *s)
Definition: j2kenc.c:276
static int lut_nmsedec_sig0[1<< NMSEDEC_BITS]
Definition: j2kenc.c:82
#define JPEG2000_T1_REF
Definition: jpeg2000.h:97
int flushed_len
Definition: jpeg2000.h:160
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition: avcodec.h:788
static int put_qcd(Jpeg2000EncoderContext *s, int compno)
Definition: j2kenc.c:332
static int getnmsedec_sig(int x, int bpno)
Definition: j2kenc.c:533
int tile_height
tile size
Definition: j2kenc.c:113
#define FFMIN(a, b)
Definition: common.h:92
uint8_t data[8192]
Definition: jpeg2000.h:172
struct Jpeg2000TgtNode * parent
Definition: jpeg2000.h:131
static void encode_sigpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
Definition: j2kenc.c:547
float y
int width
picture width / height.
Definition: avcodec.h:1691
JPEG 2000 structures and defines common to encoder and decoder.
int i_stepsize
Definition: jpeg2000.h:193
static int j2kenc_destroy(AVCodecContext *avctx)
Definition: j2kenc.c:1163
#define MQC_CX_RL
Definition: mqc.h:34
#define CODEC_JP2
Definition: j2kenc.c:79
static int ff_jpeg2000_getsigctxno(int flag, int bandno)
Definition: jpeg2000.h:240
int quality
quality (between 1 (good) and FF_LAMBDA_MAX (bad))
Definition: frame.h:283
int n
Definition: avisynth_c.h:547
uint8_t cbps[4]
bits per sample in particular components
Definition: j2kenc.c:109
int ff_dwt_encode(DWTContext *s, void *t)
Definition: jpeg2000dwt.c:581
void avcodec_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: imgconvert.c:43
int coord[2][2]
Definition: jpeg2000.h:200
#define av_log2
Definition: intmath.h:100
static int encode_tile(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno)
Definition: j2kenc.c:879
static int ff_jpeg2000_ceildivpow2(int a, int b)
Definition: jpeg2000.h:216
int coord[2][2]
Definition: jpeg2000.h:191
int coord_o[2][2]
Definition: jpeg2000.h:212
static void encode_refpass(Jpeg2000T1Context *t1, int width, int height, int *nmsedec, int bpno)
Definition: j2kenc.c:569
uint8_t * buf_start
Definition: j2kenc.c:116
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:199
Jpeg2000CodingStyle codsty
Definition: j2kenc.c:123
main external API structure.
Definition: avcodec.h:1512
uint8_t vis
Definition: jpeg2000.h:130
void av_cold ff_mqc_init_context_tables(void)
MQ-coder Initialize context tables (QE, NLPS, NMPS)
Definition: mqc.c:97
static void init_luts(void)
Definition: j2kenc.c:515
static void encode_cblk(Jpeg2000EncoderContext *s, Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk, Jpeg2000Tile *tile, int width, int height, int bandpos, int lev)
Definition: j2kenc.c:638
BYTE int const BYTE int int int height
Definition: avisynth_c.h:676
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:73
Jpeg2000Pass passes[JPEG2000_MAX_PASSES]
Definition: jpeg2000.h:176
Describe the class of an AVClass context structure.
Definition: log.h:67
static const AVOption options[]
Definition: j2kenc.c:1175
#define AV_WB32(p, v)
Definition: intreadwrite.h:419
uint8_t nbands
Definition: jpeg2000.h:199
#define JPEG2000_T1_SGN
Definition: jpeg2000.h:99
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
Definition: utils.c:1804
int height
image width and height
Definition: j2kenc.c:108
Jpeg2000ResLevel * reslevel
Definition: jpeg2000.h:207
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:245
static int ff_jpeg2000_ceildiv(int a, int b)
Definition: jpeg2000.h:221
Jpeg2000Component * comp
Definition: j2kenc.c:100
void ff_jpeg2000_reinit(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
Definition: jpeg2000.c:511
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:182
uint8_t expn[JPEG2000_MAX_DECLEVELS *3]
Definition: jpeg2000.h:150
static void reinit(Jpeg2000EncoderContext *s)
Definition: j2kenc.c:975
static uint8_t * put_sot(Jpeg2000EncoderContext *s, int tileno)
Definition: j2kenc.c:377
Jpeg2000Tile * tile
Definition: j2kenc.c:126
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:67
Y , 8bpp.
Definition: pixfmt.h:75
uint8_t quantsty
Definition: jpeg2000.h:152
common internal api header.
common internal and external API header
if(ret< 0)
Definition: vf_mcdeint.c:280
uint8_t log2_cblk_width
Definition: jpeg2000.h:137
static void copy_frame(Jpeg2000EncoderContext *s)
Definition: j2kenc.c:447
int prediction_method
prediction method (needed for huffyuv)
Definition: avcodec.h:1893
#define LAMBDA_SCALE
Definition: j2kenc.c:77
int data[6144]
Definition: jpeg2000.h:122
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:74
uint16_t log2_cblk_height
Definition: jpeg2000.h:192
#define VE
Definition: j2kenc.c:1174
static av_always_inline void bytestream_put_buffer(uint8_t **b, const uint8_t *src, unsigned int size)
Definition: bytestream.h:368
#define MQC_CX_UNI
Definition: mqc.h:33
void * priv_data
Definition: avcodec.h:1554
int ff_jpeg2000_init_component(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty, Jpeg2000QuantStyle *qntsty, int cbps, int dx, int dy, AVCodecContext *avctx)
Definition: jpeg2000.c:195
uint16_t rate
Definition: jpeg2000.h:157
static void putnumpasses(Jpeg2000EncoderContext *s, int n)
Definition: j2kenc.c:696
int ff_mqc_flush_to(MqcState *mqc, uint8_t *dst, int *dst_len)
Definition: mqcenc.c:122
static void truncpasses(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile)
Definition: j2kenc.c:850
Jpeg2000Prec * prec
Definition: jpeg2000.h:195
static void encode_clnpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
Definition: j2kenc.c:583
#define JPEG2000_T1_SIG
Definition: jpeg2000.h:96
static int lut_nmsedec_sig[1<< NMSEDEC_BITS]
Definition: j2kenc.c:82
#define LIBAVCODEC_IDENT
Definition: version.h:43
static void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.h:228
#define av_freep(p)
static void comp(unsigned char *dst, int dst_stride, unsigned char *src, int src_stride, int add)
Definition: eamad.c:83
MqcState mqc
Definition: jpeg2000.h:124
static int ff_jpeg2000_getrefctxno(int flag)
Definition: jpeg2000.h:249
uint8_t log2_cblk_height
Definition: jpeg2000.h:137
#define av_malloc_array(a, b)
#define NMSEDEC_FRACBITS
Definition: j2kenc.c:75
static void init_quantization(Jpeg2000EncoderContext *s)
Definition: j2kenc.c:485
static const int dwt_norms[2][4][10]
Definition: j2kenc.c:87
AVPixelFormat
Pixel format.
Definition: pixfmt.h:65
This structure stores compressed data.
Definition: avcodec.h:1410
for(j=16;j >0;--j)
uint8_t cx_states[19]
Definition: mqc.h:45
#define WMSEDEC_SHIFT
must be >= 13
Definition: j2kenc.c:76
static int encode_packets(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno)
Definition: j2kenc.c:809
static int ff_jpeg2000_getsgnctxno(int flag, int *xorbit)
Definition: jpeg2000.h:258
static int width
void ff_mqc_encode(MqcState *mqc, uint8_t *cxstate, int d)
code bit d with context cx
Definition: mqcenc.c:79