FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
j2kdec.c
Go to the documentation of this file.
1 /*
2  * JPEG 2000 image decoder
3  * Copyright (c) 2007 Kamil Nowosad
4  * Copyright (c) 2013 Nicolas Bertrand <nicoinattendu@gmail.com>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 /**
24  * JPEG2000 image decoder
25  * @file
26  * @author Kamil Nowosad
27  */
28 
29 #include "libavutil/avassert.h"
30 #include "libavutil/common.h"
31 #include "libavutil/opt.h"
32 #include "avcodec.h"
33 #include "bytestream.h"
34 #include "internal.h"
35 #include "thread.h"
36 #include "jpeg2000.h"
37 
38 #define JP2_SIG_TYPE 0x6A502020
39 #define JP2_SIG_VALUE 0x0D0A870A
40 #define JP2_CODESTREAM 0x6A703263
41 
42 #define HAD_COC 0x01
43 #define HAD_QCC 0x02
44 
45 typedef struct Jpeg2000Tile {
50 } Jpeg2000Tile;
51 
52 typedef struct Jpeg2000DecoderContext {
53  AVClass *class;
57 
58  int width, height;
61  uint8_t cbps[4]; // bits per sample in particular components
62  uint8_t sgnd[4]; // if a component is signed
64  int cdx[4], cdy[4];
65  int precision;
70 
73 
74  int bit_index;
75 
76  int curtileno;
77 
79 
80  /*options parameters*/
81  int lowres;
84 
85 /* get_bits functions for JPEG2000 packet bitstream
86  * It is a get_bit function with a bit-stuffing routine. If the value of the
87  * byte is 0xFF, the next byte includes an extra zero bit stuffed into the MSB.
88  * cf. ISO-15444-1:2002 / B.10.1 Bit-stuffing routine */
90 {
91  int res = 0;
92 
93  while (--n >= 0) {
94  res <<= 1;
95  if (s->bit_index == 0) {
96  s->bit_index = 7 + (bytestream2_get_byte(&s->g) != 0xFFu);
97  }
98  s->bit_index--;
99  res |= (bytestream2_peek_byte(&s->g) >> s->bit_index) & 1;
100  }
101  return res;
102 }
103 
105 {
106  if (bytestream2_get_byte(&s->g) == 0xff)
107  bytestream2_skip(&s->g, 1);
108  s->bit_index = 8;
109 }
110 
111 /* decode the value stored in node */
113  int threshold)
114 {
115  Jpeg2000TgtNode *stack[30];
116  int sp = -1, curval = 0;
117 
118  if (!node)
119  return AVERROR(EINVAL);
120 
121  while (node && !node->vis) {
122  stack[++sp] = node;
123  node = node->parent;
124  }
125 
126  if (node)
127  curval = node->val;
128  else
129  curval = stack[sp]->val;
130 
131  while (curval < threshold && sp >= 0) {
132  if (curval < stack[sp]->val)
133  curval = stack[sp]->val;
134  while (curval < threshold) {
135  int ret;
136  if ((ret = get_bits(s, 1)) > 0) {
137  stack[sp]->vis++;
138  break;
139  } else if (!ret)
140  curval++;
141  else
142  return ret;
143  }
144  stack[sp]->val = curval;
145  sp--;
146  }
147  return curval;
148 }
149 
150 /* marker segments */
151 /* get sizes and offsets of image, tiles; number of components */
153 {
154  int i, ret;
155  ThreadFrame frame = { .f = s->picture };
156 
157  if (bytestream2_get_bytes_left(&s->g) < 36)
158  return AVERROR(EINVAL);
159 
160  s->avctx->profile = bytestream2_get_be16u(&s->g); // Rsiz
161  s->width = bytestream2_get_be32u(&s->g); // Width
162  s->height = bytestream2_get_be32u(&s->g); // Height
163  s->image_offset_x = bytestream2_get_be32u(&s->g); // X0Siz
164  s->image_offset_y = bytestream2_get_be32u(&s->g); // Y0Siz
165  s->tile_width = bytestream2_get_be32u(&s->g); // XTSiz
166  s->tile_height = bytestream2_get_be32u(&s->g); // YTSiz
167  s->tile_offset_x = bytestream2_get_be32u(&s->g); // XT0Siz
168  s->tile_offset_y = bytestream2_get_be32u(&s->g); // YT0Siz
169  s->ncomponents = bytestream2_get_be16u(&s->g); // CSiz
170 
171  if (s->ncomponents <= 0 || s->ncomponents > 4) {
172  av_log(s->avctx, AV_LOG_ERROR, "unsupported/invalid ncomponents: %d\n", s->ncomponents);
173  return AVERROR(EINVAL);
174  }
175  if (s->tile_width<=0 || s->tile_height<=0)
176  return AVERROR(EINVAL);
177 
178  if (bytestream2_get_bytes_left(&s->g) < 3 * s->ncomponents)
179  return AVERROR(EINVAL);
180 
181  for (i = 0; i < s->ncomponents; i++) { // Ssiz_i XRsiz_i, YRsiz_i
182  uint8_t x = bytestream2_get_byteu(&s->g);
183  s->cbps[i] = (x & 0x7f) + 1;
184  s->precision = FFMAX(s->cbps[i], s->precision);
185  s->sgnd[i] = !!(x & 0x80);
186  s->cdx[i] = bytestream2_get_byteu(&s->g);
187  s->cdy[i] = bytestream2_get_byteu(&s->g);
188  if (s->cdx[i] != 1 || s->cdy[i] != 1) {
189  av_log(s->avctx, AV_LOG_ERROR, "unsupported/ CDxy values\n");
190  }
191  }
192 
195 
196  if (s->numXtiles * (uint64_t)s->numYtiles > INT_MAX/sizeof(Jpeg2000Tile))
197  return AVERROR(EINVAL);
198 
199  s->tile = av_mallocz(s->numXtiles * s->numYtiles * sizeof(*s->tile));
200  if (!s->tile)
201  return AVERROR(ENOMEM);
202 
203  for (i = 0; i < s->numXtiles * s->numYtiles; i++) {
204  Jpeg2000Tile *tile = s->tile + i;
205 
206  tile->comp = av_mallocz(s->ncomponents * sizeof(*tile->comp));
207  if (!tile->comp)
208  return AVERROR(ENOMEM);
209  }
210 
211  /* compute image size with reduction factor */
213  s->reduction_factor);
215  s->reduction_factor);
216 
217  switch(s->ncomponents) {
218  case 1:
219  if (s->precision > 8)
221  else
223  break;
224  case 3:
225  switch (s->avctx->profile) {
228  /* XYZ color-space for digital cinema profiles */
230  break;
231  default:
232  if (s->precision > 8)
234  else
236  break;
237  }
238  break;
239  case 4:
241  break;
242  default:
243  /* pixel format can not be identified */
245  break;
246  }
247 
248 
249  if ((ret = ff_thread_get_buffer(s->avctx, &frame, 0)) < 0)
250  return ret;
251 
253  s->picture->key_frame = 1;
254 
255  return 0;
256 }
257 
258 /* get common part for COD and COC segments */
260 {
261  uint8_t byte;
262 
263  if (bytestream2_get_bytes_left(&s->g) < 5)
264  return AVERROR(EINVAL);
265  c->nreslevels = bytestream2_get_byteu(&s->g) + 1; // num of resolution levels - 1
267  av_log(s->avctx, AV_LOG_ERROR, "nreslevels %d is invalid\n", c->nreslevels);
268  return AVERROR_INVALIDDATA;
269  }
270 
271  /* compute number of resolution levels to decode */
272  if (c->nreslevels < s->reduction_factor)
273  c->nreslevels2decode = 1;
274  else
276 
277  c->log2_cblk_width = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk width
278  c->log2_cblk_height = (bytestream2_get_byteu(&s->g) & 15) + 2; // cblk height
279 
280  if (c->log2_cblk_width > 10 || c->log2_cblk_height > 10 ||
281  c->log2_cblk_width + c->log2_cblk_height > 14) {
282  av_log(s->avctx, AV_LOG_ERROR, "cblk size invalid\n");
283  return AVERROR_INVALIDDATA;
284  }
285 
286  c->cblk_style = bytestream2_get_byteu(&s->g);
287  if (c->cblk_style != 0) { // cblk style
288  av_log(s->avctx, AV_LOG_WARNING, "extra cblk styles %X\n", c->cblk_style);
289  }
290  c->transform = bytestream2_get_byteu(&s->g); // DWT transformation type
291  /* set integer 9/7 DWT in case of BITEXACT flag */
292  if ((s->avctx->flags & CODEC_FLAG_BITEXACT) && (c->transform == FF_DWT97))
293  c->transform = FF_DWT97_INT;
294 
295  if (c->csty & JPEG2000_CSTY_PREC) {
296  int i;
297  for (i = 0; i < c->nreslevels; i++) {
298  byte = bytestream2_get_byte(&s->g);
299  c->log2_prec_widths[i] = byte & 0x0F; // precinct PPx
300  c->log2_prec_heights[i] = (byte >> 4) & 0x0F; // precinct PPy
301  }
302  } else {
303  memset(c->log2_prec_widths , 15, sizeof(c->log2_prec_widths ));
304  memset(c->log2_prec_heights, 15, sizeof(c->log2_prec_heights));
305  }
306  return 0;
307 }
308 
309 /* get coding parameters for a particular tile or whole image*/
311  uint8_t *properties)
312 {
314  int compno;
315 
316  if (bytestream2_get_bytes_left(&s->g) < 5)
317  return AVERROR(EINVAL);
318 
319  tmp.csty = bytestream2_get_byteu(&s->g);
320 
321  // get progression order
322  tmp.prog_order = bytestream2_get_byteu(&s->g);
323  if (tmp.prog_order) {
324  av_log(s->avctx, AV_LOG_ERROR, "only LRCP progression supported\n");
325  }
326 
327  tmp.nlayers = bytestream2_get_be16u(&s->g);
328  tmp.mct = bytestream2_get_byteu(&s->g); // multiple component transformation
329 
330  get_cox(s, &tmp);
331  for (compno = 0; compno < s->ncomponents; compno++)
332  if (!(properties[compno] & HAD_COC))
333  memcpy(c + compno, &tmp, sizeof(tmp));
334  return 0;
335 }
336 
337 /* Get coding parameters for a component in the whole image or a
338  * particular tile. */
340  uint8_t *properties)
341 {
342  int compno;
343 
344  if (bytestream2_get_bytes_left(&s->g) < 2)
345  return AVERROR(EINVAL);
346 
347  compno = bytestream2_get_byteu(&s->g);
348 
349  c += compno;
350  c->csty = bytestream2_get_byteu(&s->g);
351  get_cox(s, c);
352 
353  properties[compno] |= HAD_COC;
354  return 0;
355 }
356 
357 /* Get common part for QCD and QCC segments. */
359 {
360  int i, x;
361 
362  if (bytestream2_get_bytes_left(&s->g) < 1)
363  return AVERROR(EINVAL);
364 
365  x = bytestream2_get_byteu(&s->g); // Sqcd
366 
367  q->nguardbits = x >> 5;
368  q->quantsty = x & 0x1f;
369 
370  if (q->quantsty == JPEG2000_QSTY_NONE) {
371  n -= 3;
372  if (bytestream2_get_bytes_left(&s->g) < n || 32*3 < n)
373  return AVERROR(EINVAL);
374  for (i = 0; i < n; i++)
375  q->expn[i] = bytestream2_get_byteu(&s->g) >> 3;
376  } else if (q->quantsty == JPEG2000_QSTY_SI) {
377  if (bytestream2_get_bytes_left(&s->g) < 2)
378  return AVERROR(EINVAL);
379  x = bytestream2_get_be16u(&s->g);
380  q->expn[0] = x >> 11;
381  q->mant[0] = x & 0x7ff;
382  for (i = 1; i < 32 * 3; i++) {
383  int curexpn = FFMAX(0, q->expn[0] - (i - 1) / 3);
384  q->expn[i] = curexpn;
385  q->mant[i] = q->mant[0];
386  }
387  } else {
388  n = (n - 3) >> 1;
389  if (bytestream2_get_bytes_left(&s->g) < 2 * n || 32*3 < n)
390  return AVERROR(EINVAL);
391  for (i = 0; i < n; i++) {
392  x = bytestream2_get_be16u(&s->g);
393  q->expn[i] = x >> 11;
394  q->mant[i] = x & 0x7ff;
395  }
396  }
397  return 0;
398 }
399 
400 /* Get quantization parameters for a particular tile or a whole image. */
402  uint8_t *properties)
403 {
404  Jpeg2000QuantStyle tmp;
405  int compno;
406 
407  if (get_qcx(s, n, &tmp))
408  return -1;
409  for (compno = 0; compno < s->ncomponents; compno++)
410  if (!(properties[compno] & HAD_QCC))
411  memcpy(q + compno, &tmp, sizeof(tmp));
412  return 0;
413 }
414 
415 /* Get quantization parameters for a component in the whole image
416  * on in a particular tile. */
418  uint8_t *properties)
419 {
420  int compno;
421 
422  if (bytestream2_get_bytes_left(&s->g) < 1)
423  return AVERROR(EINVAL);
424 
425  compno = bytestream2_get_byteu(&s->g);
426  properties[compno] |= HAD_QCC;
427  return get_qcx(s, n - 1, q + compno);
428 }
429 
430 /* get start of tile segment */
432 {
433  if (bytestream2_get_bytes_left(&s->g) < 8)
434  return AVERROR(EINVAL);
435 
436  s->curtileno = bytestream2_get_be16u(&s->g); ///< Isot
437  if ((unsigned)s->curtileno >= s->numXtiles * s->numYtiles) {
438  s->curtileno=0;
439  return AVERROR(EINVAL);
440  }
441 
442  bytestream2_skipu(&s->g, 4); ///< Psot (ignored)
443 
444  if (!bytestream2_get_byteu(&s->g)) { ///< TPsot
445  Jpeg2000Tile *tile = s->tile + s->curtileno;
446 
447  /* copy defaults */
448  memcpy(tile->codsty, s->codsty, s->ncomponents * sizeof(Jpeg2000CodingStyle));
449  memcpy(tile->qntsty, s->qntsty, s->ncomponents * sizeof(Jpeg2000QuantStyle));
450  }
451  bytestream2_get_byteu(&s->g); ///< TNsot
452 
453  return 0;
454 }
455 
456 /* Tile-part lengths: see ISO 15444-1:2002, section A.7.1
457  * Used to know the number of tile parts and lengths.
458  * There may be multiple TLMs in the header.
459  * TODO: The function is not used for tile-parts management, nor anywhere else.
460  * It can be useful to allocate memory for tile parts, before managing the SOT
461  * markers. Parsing the TLM header is needed to increment the input header
462  * buffer.
463  * This marker is mandatory for DCI. */
465 {
466  uint8_t Stlm, ST, SP, tile_tlm, i;
467  bytestream2_get_byte(&s->g); /* Ztlm: skipped */
468  Stlm = bytestream2_get_byte(&s->g);
469 
470  // too complex ? ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
471  ST = (Stlm >> 4) & 0x03;
472  // TODO: Manage case of ST = 0b11 --> raise error
473  SP = (Stlm >> 6) & 0x01;
474  tile_tlm = (n - 4) / ((SP + 1) * 2 + ST);
475  for (i = 0; i < tile_tlm; i++) {
476  switch (ST) {
477  case 0:
478  break;
479  case 1:
480  bytestream2_get_byte(&s->g);
481  break;
482  case 2:
483  bytestream2_get_be16(&s->g);
484  break;
485  case 3:
486  bytestream2_get_be32(&s->g);
487  break;
488  }
489  if (SP == 0) {
490  bytestream2_get_be16(&s->g);
491  } else {
492  bytestream2_get_be32(&s->g);
493  }
494  }
495  return 0;
496 }
497 
498 static int init_tile(Jpeg2000DecoderContext *s, int tileno)
499 {
500  int compno;
501  int tilex = tileno % s->numXtiles;
502  int tiley = tileno / s->numXtiles;
503  Jpeg2000Tile *tile = s->tile + tileno;
504 
505  if (!tile->comp)
506  return AVERROR(ENOMEM);
507  for (compno = 0; compno < s->ncomponents; compno++) {
508  Jpeg2000Component *comp = tile->comp + compno;
509  Jpeg2000CodingStyle *codsty = tile->codsty + compno;
510  Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
511  int ret; // global bandno
512 
513  comp->coord_o[0][0] = FFMAX(tilex * s->tile_width + s->tile_offset_x, s->image_offset_x);
514  comp->coord_o[0][1] = FFMIN((tilex + 1) * s->tile_width + s->tile_offset_x, s->width);
515  comp->coord_o[1][0] = FFMAX(tiley * s->tile_height + s->tile_offset_y, s->image_offset_y);
516  comp->coord_o[1][1] = FFMIN((tiley + 1) * s->tile_height + s->tile_offset_y, s->height);
517 
518  comp->coord[0][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], s->reduction_factor);
519  comp->coord[0][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][1], s->reduction_factor);
520  comp->coord[1][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], s->reduction_factor);
521  comp->coord[1][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][1], s->reduction_factor);
522 
523  if (ret = ff_jpeg2000_init_component(comp, codsty, qntsty, s->cbps[compno], s->cdx[compno], s->cdy[compno], s->avctx))
524  return ret;
525  }
526  return 0;
527 }
528 
529 /* read the number of coding passes */
531 {
532  int num;
533  if (!get_bits(s, 1))
534  return 1;
535  if (!get_bits(s, 1))
536  return 2;
537  if ((num = get_bits(s, 2)) != 3)
538  return num < 0 ? num : 3 + num;
539  if ((num = get_bits(s, 5)) != 31)
540  return num < 0 ? num : 6 + num;
541  num = get_bits(s, 7);
542  return num < 0 ? num : 37 + num;
543 }
544 
546 {
547  int res = 0, ret;
548  while (ret = get_bits(s, 1)) {
549  if (ret < 0)
550  return ret;
551  res++;
552  }
553  return res;
554 }
555 
557  Jpeg2000CodingStyle *codsty,
558  Jpeg2000ResLevel *rlevel, int precno,
559  int layno, uint8_t *expn, int numgbits)
560 {
561  int bandno, cblkno, ret, nb_code_blocks;
562 
563  if (!(ret = get_bits(s, 1))) {
564  jpeg2000_flush(s);
565  return 0;
566  } else if (ret < 0)
567  return ret;
568 
569  for (bandno = 0; bandno < rlevel->nbands; bandno++) {
570  Jpeg2000Band *band = rlevel->band + bandno;
571  Jpeg2000Prec *prec = band->prec + precno;
572 
573  if (band->coord[0][0] == band->coord[0][1] ||
574  band->coord[1][0] == band->coord[1][1])
575  continue;
576 
577  nb_code_blocks = prec->nb_codeblocks_height *
578  prec->nb_codeblocks_width;
579  for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
580  Jpeg2000Cblk *cblk = prec->cblk + cblkno;
581  int incl, newpasses, llen;
582 
583  if (cblk->npasses)
584  incl = get_bits(s, 1);
585  else
586  incl = tag_tree_decode(s, prec->cblkincl + cblkno, layno + 1) == layno;
587  if (!incl)
588  continue;
589  else if (incl < 0)
590  return incl;
591 
592  if (!cblk->npasses)
593  cblk->nonzerobits = expn[bandno] + numgbits - 1 -
594  tag_tree_decode(s, prec->zerobits + cblkno,
595  100);
596  if ((newpasses = getnpasses(s)) < 0)
597  return newpasses;
598  if ((llen = getlblockinc(s)) < 0)
599  return llen;
600  cblk->lblock += llen;
601  if ((ret = get_bits(s, av_log2(newpasses) + cblk->lblock)) < 0)
602  return ret;
603  cblk->lengthinc = ret;
604  cblk->npasses += newpasses;
605  }
606  }
607  jpeg2000_flush(s);
608 
609  if (codsty->csty & JPEG2000_CSTY_EPH) {
610  if (bytestream2_peek_be16(&s->g) == JPEG2000_EPH)
611  bytestream2_skip(&s->g, 2);
612  else
613  av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found.\n");
614  }
615 
616  for (bandno = 0; bandno < rlevel->nbands; bandno++) {
617  Jpeg2000Band *band = rlevel->band + bandno;
618  Jpeg2000Prec *prec = band->prec + precno;
619 
620  nb_code_blocks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
621  for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
622  Jpeg2000Cblk *cblk = prec->cblk + cblkno;
623  if ( bytestream2_get_bytes_left(&s->g) < cblk->lengthinc
624  || sizeof(cblk->data) < cblk->lengthinc
625  )
626  return AVERROR(EINVAL);
627  /* Code-block data can be empty. In that case initialize data
628  * with 0xFFFF. */
629  if (cblk->lengthinc > 0) {
630  bytestream2_get_bufferu(&s->g, cblk->data, cblk->lengthinc);
631  } else {
632  cblk->data[0] = 0xFF;
633  cblk->data[1] = 0xFF;
634  }
635  cblk->length += cblk->lengthinc;
636  cblk->lengthinc = 0;
637  }
638  }
639  return 0;
640 }
641 
643 {
644  int layno, reslevelno, compno, precno, ok_reslevel;
645  s->bit_index = 8;
646  for (layno = 0; layno < tile->codsty[0].nlayers; layno++) {
647  ok_reslevel = 1;
648  for (reslevelno = 0; ok_reslevel; reslevelno++) {
649  ok_reslevel = 0;
650  for (compno = 0; compno < s->ncomponents; compno++) {
651  Jpeg2000CodingStyle *codsty = tile->codsty + compno;
652  Jpeg2000QuantStyle *qntsty = tile->qntsty + compno;
653  if (reslevelno < codsty->nreslevels) {
654  Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
655  reslevelno;
656  ok_reslevel = 1;
657  for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
659  codsty, rlevel,
660  precno, layno,
661  qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
662  qntsty->nguardbits))
663  return -1;
664  }
665  }
666  }
667  }
668  return 0;
669 }
670 
671 /* TIER-1 routines */
673  int bpno, int bandno, int bpass_csty_symbol,
674  int vert_causal_ctx_csty_symbol)
675 {
676  int mask = 3 << (bpno - 1), y0, x, y;
677 
678  for (y0 = 0; y0 < height; y0 += 4)
679  for (x = 0; x < width; x++)
680  for (y = y0; y < height && y < y0 + 4; y++) {
681  if ((t1->flags[y+1][x+1] & JPEG2000_T1_SIG_NB)
682  && !(t1->flags[y+1][x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
683  int flags_mask = -1;
684  if (vert_causal_ctx_csty_symbol && y == y0 + 3)
686  if (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[y+1][x+1] & flags_mask, bandno))) {
687  int xorbit, ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
688  if (bpass_csty_symbol)
689  t1->data[y][x] = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ? -mask : mask;
690  else
691  t1->data[y][x] = (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) ?
692  -mask : mask;
693 
695  t1->data[y][x] < 0);
696  }
697  t1->flags[y + 1][x + 1] |= JPEG2000_T1_VIS;
698  }
699  }
700 }
701 
703  int bpno)
704 {
705  int phalf, nhalf;
706  int y0, x, y;
707 
708  phalf = 1 << (bpno - 1);
709  nhalf = -phalf;
710 
711  for (y0 = 0; y0 < height; y0 += 4)
712  for (x = 0; x < width; x++)
713  for (y = y0; y < height && y < y0 + 4; y++)
714  if ((t1->flags[y + 1][x + 1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG) {
715  int ctxno = ff_jpeg2000_getrefctxno(t1->flags[y + 1][x + 1]);
716  int r = ff_mqc_decode(&t1->mqc,
717  t1->mqc.cx_states + ctxno)
718  ? phalf : nhalf;
719  t1->data[y][x] += t1->data[y][x] < 0 ? -r : r;
720  t1->flags[y + 1][x + 1] |= JPEG2000_T1_REF;
721  }
722 }
723 
725  int width, int height, int bpno, int bandno,
726  int seg_symbols, int vert_causal_ctx_csty_symbol)
727 {
728  int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
729 
730  for (y0 = 0; y0 < height; y0 += 4) {
731  for (x = 0; x < width; x++) {
732  if (y0 + 3 < height &&
733  !((t1->flags[y0 + 1][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
734  (t1->flags[y0 + 2][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
735  (t1->flags[y0 + 3][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
736  (t1->flags[y0 + 4][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)))) {
737  if (!ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL))
738  continue;
739  runlen = ff_mqc_decode(&t1->mqc,
740  t1->mqc.cx_states + MQC_CX_UNI);
741  runlen = (runlen << 1) | ff_mqc_decode(&t1->mqc,
742  t1->mqc.cx_states +
743  MQC_CX_UNI);
744  dec = 1;
745  } else {
746  runlen = 0;
747  dec = 0;
748  }
749 
750  for (y = y0 + runlen; y < y0 + 4 && y < height; y++) {
751  if (!dec) {
752  if (!(t1->flags[y+1][x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
753  int flags_mask = -1;
754  if (vert_causal_ctx_csty_symbol && y == y0 + 3)
756  dec = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[y+1][x+1] & flags_mask,
757  bandno));
758  }
759  }
760  if (dec) {
761  int xorbit;
762  int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y + 1][x + 1],
763  &xorbit);
764  t1->data[y][x] = (ff_mqc_decode(&t1->mqc,
765  t1->mqc.cx_states + ctxno) ^
766  xorbit)
767  ? -mask : mask;
768  ff_jpeg2000_set_significance(t1, x, y, t1->data[y][x] < 0);
769  }
770  dec = 0;
771  t1->flags[y + 1][x + 1] &= ~JPEG2000_T1_VIS;
772  }
773  }
774  }
775  if (seg_symbols) {
776  int val;
777  val = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
778  val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
779  val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
780  val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
781  if (val != 0xa)
783  "Segmentation symbol value incorrect\n");
784  }
785 }
786 
789  int width, int height, int bandpos)
790 {
791  int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1, y, clnpass_cnt = 0;
792  int bpass_csty_symbol = JPEG2000_CBLK_BYPASS & codsty->cblk_style;
793  int vert_causal_ctx_csty_symbol = JPEG2000_CBLK_VSC & codsty->cblk_style;
794 
795  for (y = 0; y < height+2; y++)
796  memset(t1->flags[y], 0, (width + 2)*sizeof(int));
797 
798  for (y = 0; y < height; y++)
799  memset(t1->data[y], 0, width*sizeof(int));
800 
801  cblk->data[cblk->length] = 0xff;
802  cblk->data[cblk->length+1] = 0xff;
803  ff_mqc_initdec(&t1->mqc, cblk->data);
804 
805  while (passno--) {
806  switch(pass_t) {
807  case 0:
808  decode_sigpass(t1, width, height, bpno + 1, bandpos,
809  bpass_csty_symbol && (clnpass_cnt >= 4), vert_causal_ctx_csty_symbol);
810  break;
811  case 1:
812  decode_refpass(t1, width, height, bpno + 1);
813  if (bpass_csty_symbol && clnpass_cnt >= 4)
814  ff_mqc_initdec(&t1->mqc, cblk->data);
815  break;
816  case 2:
817  decode_clnpass(s, t1, width, height, bpno + 1, bandpos,
818  codsty->cblk_style & JPEG2000_CBLK_SEGSYM, vert_causal_ctx_csty_symbol);
819  clnpass_cnt = clnpass_cnt + 1;
820  if (bpass_csty_symbol && clnpass_cnt >= 4)
821  ff_mqc_initdec(&t1->mqc, cblk->data);
822  break;
823  }
824 
825  pass_t++;
826  if (pass_t == 3) {
827  bpno--;
828  pass_t = 0;
829  }
830  }
831  return 0;
832 }
833 
834 /* Float dequantization of a codeblock.*/
835 static void dequantization_float(int x, int y, Jpeg2000Cblk *cblk,
838 {
839  int i, j, idx;
840  float *datap = &comp->f_data[(comp->coord[0][1] - comp->coord[0][0]) * y + x];
841  for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j)
842  for (i = 0; i < (cblk->coord[0][1] - cblk->coord[0][0]); ++i) {
843  idx = (comp->coord[0][1] - comp->coord[0][0]) * j + i;
844  datap[idx] = (float)(t1->data[j][i]) * band->f_stepsize;
845  }
846 }
847 
848 /* Integer dequantization of a codeblock.*/
849 static void dequantization_int(int x, int y, Jpeg2000Cblk *cblk,
852 {
853  int i, j, idx;
854  int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * y + x];
855  for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j)
856  for (i = 0; i < (cblk->coord[0][1] - cblk->coord[0][0]); ++i) {
857  idx = (comp->coord[0][1] - comp->coord[0][0]) * j + i;
858  datap[idx] =
859  ((int32_t)(t1->data[j][i]) * band->i_stepsize + (1 << 15)) >> 16;
860  }
861 }
862 
863 /* Inverse ICT parameters in float and integer.
864  * int value = (float value) * (1<<16) */
865 static const float f_ict_params[4] = {
866  1.402f,
867  0.34413f,
868  0.71414f,
869  1.772f
870 };
871 static const int i_ict_params[4] = {
872  91881,
873  22553,
874  46802,
875  116130
876 };
877 
879 {
880  int i, csize = 1;
881  int32_t *src[3], i0, i1, i2;
882  float *srcf[3], i0f, i1f, i2f;
883 
884  for (i = 0; i < 3; i++)
885  if (tile->codsty[0].transform == FF_DWT97)
886  srcf[i] = tile->comp[i].f_data;
887  else
888  src [i] = tile->comp[i].i_data;
889 
890  for (i = 0; i < 2; i++)
891  csize *= tile->comp[0].coord[i][1] - tile->comp[0].coord[i][0];
892 
893  switch (tile->codsty[0].transform) {
894  case FF_DWT97:
895  for (i = 0; i < csize; i++) {
896  i0f = *srcf[0] + (f_ict_params[0] * *srcf[2]);
897  i1f = *srcf[0] - (f_ict_params[1] * *srcf[1])
898  - (f_ict_params[2] * *srcf[2]);
899  i2f = *srcf[0] + (f_ict_params[3] * *srcf[1]);
900  *srcf[0]++ = i0f;
901  *srcf[1]++ = i1f;
902  *srcf[2]++ = i2f;
903  }
904  break;
905  case FF_DWT97_INT:
906  for (i = 0; i < csize; i++) {
907  i0 = *src[0] + (((i_ict_params[0] * *src[2]) + (1 << 15)) >> 16);
908  i1 = *src[0] - (((i_ict_params[1] * *src[1]) + (1 << 15)) >> 16)
909  - (((i_ict_params[2] * *src[2]) + (1 << 15)) >> 16);
910  i2 = *src[0] + (((i_ict_params[3] * *src[1]) + (1 << 15)) >> 16);
911  *src[0]++ = i0;
912  *src[1]++ = i1;
913  *src[2]++ = i2;
914  }
915  break;
916  case FF_DWT53:
917  for (i = 0; i < csize; i++) {
918  i1 = *src[0] - (*src[2] + *src[1] >> 2);
919  i0 = i1 + *src[2];
920  i2 = i1 + *src[1];
921  *src[0]++ = i0;
922  *src[1]++ = i1;
923  *src[2]++ = i2;
924  }
925  break;
926  }
927 }
928 
930  AVFrame *picture)
931 {
932  int compno, reslevelno, bandno;
933  int x, y;
934 
935  uint8_t *line;
937 
938  /* Loop on tile components */
939  for (compno = 0; compno < s->ncomponents; compno++) {
940  Jpeg2000Component *comp = tile->comp + compno;
941  Jpeg2000CodingStyle *codsty = tile->codsty + compno;
942 
943  /* Loop on resolution levels */
944  for (reslevelno = 0; reslevelno < codsty->nreslevels2decode; reslevelno++) {
945  Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
946  /* Loop on bands */
947  for (bandno = 0; bandno < rlevel->nbands; bandno++) {
948  int nb_precincts, precno;
949  Jpeg2000Band *band = rlevel->band + bandno;
950  int cblkno=0, bandpos;
951 
952  bandpos = bandno + (reslevelno > 0);
953 
954  if (band->coord[0][0] == band->coord[0][1] || band->coord[1][0] == band->coord[1][1])
955  continue;
956 
957  nb_precincts = rlevel->num_precincts_x * rlevel->num_precincts_y;
958  /* Loop on precincts */
959  for (precno = 0; precno < nb_precincts; precno++) {
960  Jpeg2000Prec *prec = band->prec + precno;
961 
962  /* Loop on codeblocks */
963  for (cblkno = 0; cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height; cblkno++) {
964  int x, y;
965  Jpeg2000Cblk *cblk = prec->cblk + cblkno;
966  decode_cblk(s, codsty, &t1, cblk,
967  cblk->coord[0][1] - cblk->coord[0][0],
968  cblk->coord[1][1] - cblk->coord[1][0],
969  bandpos);
970 
971  /* Manage band offsets */
972  x = cblk->coord[0][0];
973  y = cblk->coord[1][0];
974 
975  if (codsty->transform == FF_DWT97)
976  dequantization_float(x, y, cblk, comp, &t1, band);
977  else
978  dequantization_int(x, y, cblk, comp, &t1, band);
979  } /* end cblk */
980  } /*end prec */
981  } /* end band */
982  } /* end reslevel */
983 
984  ff_dwt_decode(&comp->dwt, codsty->transform == FF_DWT97 ? (void*)comp->f_data : (void*)comp->i_data);
985  } /*end comp */
986 
987  /* inverse MCT transformation */
988  if (tile->codsty[0].mct)
989  mct_decode(s, tile);
990 
991  if (s->precision <= 8) {
992  for (compno = 0; compno < s->ncomponents; compno++) {
993  Jpeg2000Component *comp = tile->comp + compno;
994  float *datap = comp->f_data;
995  int32_t *i_datap = comp->i_data;
996 
997  y = tile->comp[compno].coord[1][0] - s->image_offset_y;
998  line = picture->data[0] + y * picture->linesize[0];
999  for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) {
1000  uint8_t *dst;
1001 
1002  x = tile->comp[compno].coord[0][0] - s->image_offset_x;
1003  dst = line + x * s->ncomponents + compno;
1004 
1005  for (; x < tile->comp[compno].coord[0][1] - s->image_offset_x; x += s->cdx[compno]) {
1006  int val;
1007  /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
1008  if (tile->codsty->transform == FF_DWT97)
1009  val = lrintf(*datap) + (1 << (s->cbps[compno] - 1));
1010  else
1011  val = *i_datap + (1 << (s->cbps[compno] - 1));
1012  val = av_clip(val, 0, (1 << s->cbps[compno]) - 1);
1013  *dst = val << (8 - s->cbps[compno]);
1014  datap++;
1015  i_datap++;
1016  dst += s->ncomponents;
1017  }
1018  line += picture->linesize[0];
1019  }
1020  }
1021  } else {
1022  for (compno = 0; compno < s->ncomponents; compno++) {
1023  Jpeg2000Component *comp = tile->comp + compno;
1024  float *datap = comp->f_data;
1025  int32_t *i_datap = comp->i_data;
1026  uint16_t *linel;
1027 
1028  y = tile->comp[compno].coord[1][0] - s->image_offset_y;
1029  linel = (uint16_t*)picture->data[0] + y * (picture->linesize[0] >> 1);
1030  for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) {
1031  uint16_t *dst;
1032 
1033  x = tile->comp[compno].coord[0][0] - s->image_offset_x;
1034  dst = linel + (x * s->ncomponents + compno);
1035  for (; x < tile->comp[compno].coord[0][1] - s->image_offset_x; x += s-> cdx[compno]) {
1036  int val;
1037  /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
1038  if (tile->codsty->transform == FF_DWT97)
1039  val = lrintf(*datap) + (1 << (s->cbps[compno] - 1));
1040  else
1041  val = *i_datap + (1 << (s->cbps[compno] - 1));
1042  val = av_clip(val, 0, (1 << s->cbps[compno]) - 1);
1043  /* align 12 bit values in little-endian mode */
1044  *dst = val << (16 - s->cbps[compno]);
1045  datap++;
1046  i_datap++;
1047  dst += s->ncomponents;
1048  }
1049  linel += picture->linesize[0]>>1;
1050  }
1051  }
1052  }
1053  return 0;
1054 }
1055 
1057 {
1058  int tileno, compno;
1059  for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
1060  for (compno = 0; compno < s->ncomponents; compno++) {
1061  Jpeg2000Component *comp = s->tile[tileno].comp + compno;
1062  Jpeg2000CodingStyle *codsty = s->tile[tileno].codsty + compno;
1063 
1064  ff_jpeg2000_cleanup(comp, codsty);
1065  }
1066  av_freep(&s->tile[tileno].comp);
1067  }
1068  av_freep(&s->tile);
1069 }
1070 
1072 {
1073  Jpeg2000CodingStyle *codsty = s->codsty;
1074  Jpeg2000QuantStyle *qntsty = s->qntsty;
1075  uint8_t *properties = s->properties;
1076 
1077  for (;;) {
1078  int len, ret = 0;
1079  int marker;
1080  int oldpos;
1081 
1082  if (bytestream2_get_bytes_left(&s->g) < 2) {
1083  av_log(s->avctx, AV_LOG_ERROR, "Missing EOC\n");
1084  break;
1085  }
1086 
1087  marker = bytestream2_get_be16u(&s->g);
1088  oldpos = bytestream2_tell(&s->g);
1089 
1090  if (marker == JPEG2000_SOD) {
1091  Jpeg2000Tile *tile = s->tile + s->curtileno;
1092  if (ret = init_tile(s, s->curtileno)) {
1093  av_log(s->avctx, AV_LOG_ERROR, "tile initialization failed\n");
1094  return ret;
1095  }
1096  if (ret = jpeg2000_decode_packets(s, tile)) {
1097  av_log(s->avctx, AV_LOG_ERROR, "packets decoding failed\n");
1098  return ret;
1099  }
1100  continue;
1101  }
1102  if (marker == JPEG2000_EOC)
1103  break;
1104 
1105  if (bytestream2_get_bytes_left(&s->g) < 2)
1106  return AVERROR(EINVAL);
1107  len = bytestream2_get_be16u(&s->g);
1108  switch (marker) {
1109  case JPEG2000_SIZ:
1110  ret = get_siz(s);
1111  if (!s->tile)
1112  s->numXtiles = s->numYtiles = 0;
1113  break;
1114  case JPEG2000_COC:
1115  ret = get_coc(s, codsty, properties);
1116  break;
1117  case JPEG2000_COD:
1118  ret = get_cod(s, codsty, properties);
1119  break;
1120  case JPEG2000_QCC:
1121  ret = get_qcc(s, len, qntsty, properties);
1122  break;
1123  case JPEG2000_QCD:
1124  ret = get_qcd(s, len, qntsty, properties);
1125  break;
1126  case JPEG2000_SOT:
1127  if (!(ret = get_sot(s))) {
1128  codsty = s->tile[s->curtileno].codsty;
1129  qntsty = s->tile[s->curtileno].qntsty;
1130  properties = s->tile[s->curtileno].properties;
1131  }
1132  break;
1133  case JPEG2000_COM:
1134  // the comment is ignored
1135  bytestream2_skip(&s->g, len - 2);
1136  break;
1137  case JPEG2000_TLM:
1138  // Tile-part lengths
1139  ret = get_tlm(s, len);
1140  break;
1141  default:
1143  "unsupported marker 0x%.4X at pos 0x%X\n",
1144  marker, bytestream2_tell(&s->g) - 4);
1145  bytestream2_skip(&s->g, len - 2);
1146  break;
1147  }
1148  if (bytestream2_tell(&s->g) - oldpos != len || ret) {
1150  "error during processing marker segment %.4x\n", marker);
1151  return ret ? ret : -1;
1152  }
1153  }
1154  return 0;
1155 }
1156 
1158 {
1159  uint32_t atom_size, atom;
1160  int found_codestream = 0, search_range = 10;
1161 
1162  while (!found_codestream && search_range && bytestream2_get_bytes_left(&s->g) >= 8) {
1163  atom_size = bytestream2_get_be32u(&s->g);
1164  atom = bytestream2_get_be32u(&s->g);
1165  if (atom == JP2_CODESTREAM) {
1166  found_codestream = 1;
1167  } else {
1168  if (bytestream2_get_bytes_left(&s->g) < atom_size - 8)
1169  return 0;
1170  bytestream2_skipu(&s->g, atom_size - 8);
1171  search_range--;
1172  }
1173  }
1174 
1175  if (found_codestream)
1176  return 1;
1177  return 0;
1178 }
1179 
1180 static int jpeg2000_decode_frame(AVCodecContext *avctx, void *data,
1181  int *got_frame, AVPacket *avpkt)
1182 {
1184  AVFrame *picture = data;
1185  int tileno, ret;
1186 
1187  s->picture = picture;
1188 
1189  s->avctx = avctx;
1190  bytestream2_init(&s->g, avpkt->data, avpkt->size);
1191  s->curtileno = -1;
1192 
1193  // reduction factor, i.e number of resolution levels to skip
1194  s->reduction_factor = avctx->lowres;
1195 
1196  if (bytestream2_get_bytes_left(&s->g) < 2) {
1197  ret = AVERROR(EINVAL);
1198  goto err_out;
1199  }
1200 
1201  // check if the image is in jp2 format
1202  if (bytestream2_get_bytes_left(&s->g) >= 12 &&
1203  (bytestream2_get_be32u(&s->g) == 12) &&
1204  (bytestream2_get_be32u(&s->g) == JP2_SIG_TYPE) &&
1205  (bytestream2_get_be32u(&s->g) == JP2_SIG_VALUE)) {
1206  if (!jp2_find_codestream(s)) {
1207  av_log(avctx, AV_LOG_ERROR, "couldn't find jpeg2k codestream atom\n");
1208  ret = -1;
1209  goto err_out;
1210  }
1211  } else {
1212  bytestream2_seek(&s->g, 0, SEEK_SET);
1213  }
1214 
1215  if (bytestream2_get_be16u(&s->g) != JPEG2000_SOC) {
1216  av_log(avctx, AV_LOG_ERROR, "SOC marker not present\n");
1217  ret = -1;
1218  goto err_out;
1219  }
1220  if (ret = jpeg2000_decode_codestream(s))
1221  goto err_out;
1222 
1223  for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++)
1224  if (ret = jpeg2000_decode_tile(s, s->tile + tileno, s->picture))
1225  goto err_out;
1226 
1228 
1229  *got_frame = 1;
1230 
1231  return bytestream2_tell(&s->g);
1232 
1233 err_out:
1235  return ret;
1236 }
1237 
1239 {
1241 }
1242 
1243 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
1244 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1245 
1246 static const AVOption options[] = {
1247  { "lowres", "Lower the decoding resolution by a power of two",
1248  OFFSET(lowres), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, JPEG2000_MAX_RESLEVELS - 1, VD },
1249  { NULL },
1250 };
1251 
1252 static const AVProfile profiles[] = {
1253  { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_0, "JPEG 2000 codestream restriction 0" },
1254  { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_1, "JPEG 2000 codestream restriction 1" },
1255  { FF_PROFILE_JPEG2000_CSTREAM_NO_RESTRICTION, "JPEG 2000 no codestream restrictions" },
1256  { FF_PROFILE_JPEG2000_DCINEMA_2K, "JPEG 2000 digital cinema 2K" },
1257  { FF_PROFILE_JPEG2000_DCINEMA_4K, "JPEG 2000 digital cinema 4K" },
1258  { FF_PROFILE_UNKNOWN },
1259 };
1260 
1261 static const AVClass class = {
1262  .class_name = "j2k",
1263  .item_name = av_default_item_name,
1264  .option = options,
1266 };
1267 
1269  .name = "j2k",
1270  .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
1271  .type = AVMEDIA_TYPE_VIDEO,
1272  .id = AV_CODEC_ID_JPEG2000,
1274  .priv_data_size = sizeof(Jpeg2000DecoderContext),
1275  .init_static_data = jpeg2000_init_static_data,
1277  .priv_class = &class,
1278  .max_lowres = 5,
1279  .profiles = NULL_IF_CONFIG_SMALL(profiles)
1280 };