FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
j2kdec.c
Go to the documentation of this file.
1 /*
2  * JPEG2000 image decoder
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  * JPEG2000 image decoder
24  * @file
25  * @author Kamil Nowosad
26  */
27 
28 // #define DEBUG
29 
30 #include "avcodec.h"
31 #include "bytestream.h"
32 #include "internal.h"
33 #include "j2k.h"
34 #include "libavutil/common.h"
35 
36 #define JP2_SIG_TYPE 0x6A502020
37 #define JP2_SIG_VALUE 0x0D0A870A
38 #define JP2_CODESTREAM 0x6A703263
39 
40 #define HAD_COC 0x01
41 #define HAD_QCC 0x02
42 
43 typedef struct {
45  uint8_t properties[4];
46  J2kCodingStyle codsty[4];
47  J2kQuantStyle qntsty[4];
48 } J2kTile;
49 
50 typedef struct {
54 
55  int width, height; ///< image width and height
56  int image_offset_x, image_offset_y;
57  int tile_offset_x, tile_offset_y;
58  uint8_t cbps[4]; ///< bits per sample in particular components
59  uint8_t sgnd[4]; ///< if a component is signed
60  uint8_t properties[4];
61  int cdx[4], cdy[4];
62  int precision;
64  int tile_width, tile_height; ///< tile size
65  int numXtiles, numYtiles;
67 
68  J2kCodingStyle codsty[4];
69  J2kQuantStyle qntsty[4];
70 
71  int bit_index;
72 
73  int curtileno;
74 
77 
78 static int get_bits(J2kDecoderContext *s, int n)
79 {
80  int res = 0;
81 
82  while (--n >= 0){
83  res <<= 1;
84  if (s->bit_index == 0) {
85  s->bit_index = 7 + (bytestream2_get_byte(&s->g) != 0xFFu);
86  }
87  s->bit_index--;
88  res |= (bytestream2_peek_byte(&s->g) >> s->bit_index) & 1;
89  }
90  return res;
91 }
92 
94 {
95  if (bytestream2_get_byte(&s->g) == 0xff)
96  bytestream2_skip(&s->g, 1);
97  s->bit_index = 8;
98 }
99 #if 0
100 void printcomp(J2kComponent *comp)
101 {
102  int i;
103  for (i = 0; i < comp->y1 - comp->y0; i++)
104  ff_j2k_printv(comp->data + i * (comp->x1 - comp->x0), comp->x1 - comp->x0);
105 }
106 
107 static void nspaces(FILE *fd, int n)
108 {
109  while(n--) putc(' ', fd);
110 }
111 
112 static void dump(J2kDecoderContext *s, FILE *fd)
113 {
114  int tileno, compno, reslevelno, bandno, precno;
115  fprintf(fd, "XSiz = %d, YSiz = %d, tile_width = %d, tile_height = %d\n"
116  "numXtiles = %d, numYtiles = %d, ncomponents = %d\n"
117  "tiles:\n",
118  s->width, s->height, s->tile_width, s->tile_height,
119  s->numXtiles, s->numYtiles, s->ncomponents);
120  for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
121  J2kTile *tile = s->tile + tileno;
122  nspaces(fd, 2);
123  fprintf(fd, "tile %d:\n", tileno);
124  for(compno = 0; compno < s->ncomponents; compno++){
125  J2kComponent *comp = tile->comp + compno;
126  nspaces(fd, 4);
127  fprintf(fd, "component %d:\n", compno);
128  nspaces(fd, 4);
129  fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d\n",
130  comp->x0, comp->x1, comp->y0, comp->y1);
131  for(reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
132  J2kResLevel *reslevel = comp->reslevel + reslevelno;
133  nspaces(fd, 6);
134  fprintf(fd, "reslevel %d:\n", reslevelno);
135  nspaces(fd, 6);
136  fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d, nbands = %d\n",
137  reslevel->x0, reslevel->x1, reslevel->y0,
138  reslevel->y1, reslevel->nbands);
139  for(bandno = 0; bandno < reslevel->nbands; bandno++){
140  J2kBand *band = reslevel->band + bandno;
141  nspaces(fd, 8);
142  fprintf(fd, "band %d:\n", bandno);
143  nspaces(fd, 8);
144  fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d,"
145  "codeblock_width = %d, codeblock_height = %d cblknx = %d cblkny = %d\n",
146  band->x0, band->x1,
147  band->y0, band->y1,
148  band->codeblock_width, band->codeblock_height,
149  band->cblknx, band->cblkny);
150  for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
151  J2kPrec *prec = band->prec + precno;
152  nspaces(fd, 10);
153  fprintf(fd, "prec %d:\n", precno);
154  nspaces(fd, 10);
155  fprintf(fd, "xi0 = %d, xi1 = %d, yi0 = %d, yi1 = %d\n",
156  prec->xi0, prec->xi1, prec->yi0, prec->yi1);
157  }
158  }
159  }
160  }
161  }
162 }
163 #endif
164 
165 /** decode the value stored in node */
166 static int tag_tree_decode(J2kDecoderContext *s, J2kTgtNode *node, int threshold)
167 {
168  J2kTgtNode *stack[30];
169  int sp = -1, curval = 0;
170 
171  if(!node)
172  return AVERROR(EINVAL);
173 
174  while(node && !node->vis){
175  stack[++sp] = node;
176  node = node->parent;
177  }
178 
179  if (node)
180  curval = node->val;
181  else
182  curval = stack[sp]->val;
183 
184  while(curval < threshold && sp >= 0){
185  if (curval < stack[sp]->val)
186  curval = stack[sp]->val;
187  while (curval < threshold){
188  int ret;
189  if ((ret = get_bits(s, 1)) > 0){
190  stack[sp]->vis++;
191  break;
192  } else if (!ret)
193  curval++;
194  else
195  return ret;
196  }
197  stack[sp]->val = curval;
198  sp--;
199  }
200  return curval;
201 }
202 
203 /* marker segments */
204 /** get sizes and offsets of image, tiles; number of components */
206 {
207  int i, ret;
208 
209  if (bytestream2_get_bytes_left(&s->g) < 36)
210  return AVERROR(EINVAL);
211 
212  bytestream2_get_be16u(&s->g); // Rsiz (skipped)
213  s->width = bytestream2_get_be32u(&s->g); // width
214  s->height = bytestream2_get_be32u(&s->g); // height
215  s->image_offset_x = bytestream2_get_be32u(&s->g); // X0Siz
216  s->image_offset_y = bytestream2_get_be32u(&s->g); // Y0Siz
217 
218  s->tile_width = bytestream2_get_be32u(&s->g); // XTSiz
219  s->tile_height = bytestream2_get_be32u(&s->g); // YTSiz
220  s->tile_offset_x = bytestream2_get_be32u(&s->g); // XT0Siz
221  s->tile_offset_y = bytestream2_get_be32u(&s->g); // YT0Siz
222  s->ncomponents = bytestream2_get_be16u(&s->g); // CSiz
223 
224  if(s->ncomponents <= 0 || s->ncomponents > 4) {
225  av_log(s->avctx, AV_LOG_ERROR, "unsupported/invalid ncomponents: %d\n", s->ncomponents);
226  return AVERROR(EINVAL);
227  }
228  if(s->tile_width<=0 || s->tile_height<=0)
229  return AVERROR(EINVAL);
230 
231  if (bytestream2_get_bytes_left(&s->g) < 3 * s->ncomponents)
232  return AVERROR(EINVAL);
233 
234  for (i = 0; i < s->ncomponents; i++){ // Ssiz_i XRsiz_i, YRsiz_i
235  uint8_t x = bytestream2_get_byteu(&s->g);
236  s->cbps[i] = (x & 0x7f) + 1;
237  s->precision = FFMAX(s->cbps[i], s->precision);
238  s->sgnd[i] = !!(x & 0x80);
239  s->cdx[i] = bytestream2_get_byteu(&s->g);
240  s->cdy[i] = bytestream2_get_byteu(&s->g);
241  }
242 
245 
246  if(s->numXtiles * (uint64_t)s->numYtiles > INT_MAX/sizeof(J2kTile))
247  return AVERROR(EINVAL);
248 
249  s->tile = av_mallocz(s->numXtiles * s->numYtiles * sizeof(J2kTile));
250  if (!s->tile)
251  return AVERROR(ENOMEM);
252 
253  for (i = 0; i < s->numXtiles * s->numYtiles; i++){
254  J2kTile *tile = s->tile + i;
255 
256  tile->comp = av_mallocz(s->ncomponents * sizeof(J2kComponent));
257  if (!tile->comp)
258  return AVERROR(ENOMEM);
259  }
260 
261  s->avctx->width = s->width - s->image_offset_x;
262  s->avctx->height = s->height - s->image_offset_y;
263 
264  switch(s->ncomponents){
265  case 1:
266  if (s->precision > 8) {
268  } else {
270  }
271  break;
272  case 3:
273  if (s->precision > 8) {
275  } else {
277  }
278  break;
279  case 4:
281  break;
282  }
283 
284  if (s->picture.data[0])
285  s->avctx->release_buffer(s->avctx, &s->picture);
286 
287  if ((ret = ff_get_buffer(s->avctx, &s->picture)) < 0)
288  return ret;
289 
291  s->picture.key_frame = 1;
292 
293  return 0;
294 }
295 
296 /** get common part for COD and COC segments */
298 {
299  if (bytestream2_get_bytes_left(&s->g) < 5)
300  return AVERROR(EINVAL);
301  c->nreslevels = bytestream2_get_byteu(&s->g) + 1; // num of resolution levels - 1
302  c->log2_cblk_width = bytestream2_get_byteu(&s->g) + 2; // cblk width
303  c->log2_cblk_height = bytestream2_get_byteu(&s->g) + 2; // cblk height
304 
305  c->cblk_style = bytestream2_get_byteu(&s->g);
306  if (c->cblk_style != 0){ // cblk style
307  av_log(s->avctx, AV_LOG_WARNING, "extra cblk styles %X\n", c->cblk_style);
308  }
309  c->transform = bytestream2_get_byteu(&s->g); // transformation
310  if (c->csty & J2K_CSTY_PREC) {
311  int i;
312 
313  for (i = 0; i < c->nreslevels; i++)
314  bytestream2_get_byte(&s->g);
315  }
316  return 0;
317 }
318 
319 /** get coding parameters for a particular tile or whole image*/
320 static int get_cod(J2kDecoderContext *s, J2kCodingStyle *c, uint8_t *properties)
321 {
322  J2kCodingStyle tmp;
323  int compno;
324 
325  if (bytestream2_get_bytes_left(&s->g) < 5)
326  return AVERROR(EINVAL);
327 
328  tmp.log2_prec_width =
329  tmp.log2_prec_height = 15;
330 
331  tmp.csty = bytestream2_get_byteu(&s->g);
332 
333  if (bytestream2_get_byteu(&s->g)){ // progression level
334  av_log(s->avctx, AV_LOG_ERROR, "only LRCP progression supported\n");
335  return -1;
336  }
337 
338  tmp.nlayers = bytestream2_get_be16u(&s->g);
339  tmp.mct = bytestream2_get_byteu(&s->g); // multiple component transformation
340 
341  get_cox(s, &tmp);
342  for (compno = 0; compno < s->ncomponents; compno++){
343  if (!(properties[compno] & HAD_COC))
344  memcpy(c + compno, &tmp, sizeof(J2kCodingStyle));
345  }
346  return 0;
347 }
348 
349 /** get coding parameters for a component in the whole image on a particular tile */
350 static int get_coc(J2kDecoderContext *s, J2kCodingStyle *c, uint8_t *properties)
351 {
352  int compno;
353 
354  if (bytestream2_get_bytes_left(&s->g) < 2)
355  return AVERROR(EINVAL);
356 
357  compno = bytestream2_get_byteu(&s->g);
358 
359  c += compno;
360  c->csty = bytestream2_get_byte(&s->g);
361  get_cox(s, c);
362 
363  properties[compno] |= HAD_COC;
364  return 0;
365 }
366 
367 /** get common part for QCD and QCC segments */
368 static int get_qcx(J2kDecoderContext *s, int n, J2kQuantStyle *q)
369 {
370  int i, x;
371 
372  if (bytestream2_get_bytes_left(&s->g) < 1)
373  return AVERROR(EINVAL);
374 
375  x = bytestream2_get_byteu(&s->g); // Sqcd
376 
377  q->nguardbits = x >> 5;
378  q->quantsty = x & 0x1f;
379 
380  if (q->quantsty == J2K_QSTY_NONE){
381  n -= 3;
382  if (bytestream2_get_bytes_left(&s->g) < n || 32*3 < n)
383  return AVERROR(EINVAL);
384  for (i = 0; i < n; i++)
385  q->expn[i] = bytestream2_get_byteu(&s->g) >> 3;
386  } else if (q->quantsty == J2K_QSTY_SI){
387  if (bytestream2_get_bytes_left(&s->g) < 2)
388  return AVERROR(EINVAL);
389  x = bytestream2_get_be16u(&s->g);
390  q->expn[0] = x >> 11;
391  q->mant[0] = x & 0x7ff;
392  for (i = 1; i < 32 * 3; i++){
393  int curexpn = FFMAX(0, q->expn[0] - (i-1)/3);
394  q->expn[i] = curexpn;
395  q->mant[i] = q->mant[0];
396  }
397  } else{
398  n = (n - 3) >> 1;
399  if (bytestream2_get_bytes_left(&s->g) < 2 * n || 32*3 < n)
400  return AVERROR(EINVAL);
401  for (i = 0; i < n; i++){
402  x = bytestream2_get_be16u(&s->g);
403  q->expn[i] = x >> 11;
404  q->mant[i] = x & 0x7ff;
405  }
406  }
407  return 0;
408 }
409 
410 /** get quantization parameters for a particular tile or a whole image */
411 static int get_qcd(J2kDecoderContext *s, int n, J2kQuantStyle *q, uint8_t *properties)
412 {
413  J2kQuantStyle tmp;
414  int compno;
415 
416  if (get_qcx(s, n, &tmp))
417  return -1;
418  for (compno = 0; compno < s->ncomponents; compno++)
419  if (!(properties[compno] & HAD_QCC))
420  memcpy(q + compno, &tmp, sizeof(J2kQuantStyle));
421  return 0;
422 }
423 
424 /** get quantization parameters for a component in the whole image on in a particular tile */
425 static int get_qcc(J2kDecoderContext *s, int n, J2kQuantStyle *q, uint8_t *properties)
426 {
427  int compno;
428 
429  if (bytestream2_get_bytes_left(&s->g) < 1)
430  return AVERROR(EINVAL);
431 
432  compno = bytestream2_get_byteu(&s->g);
433  properties[compno] |= HAD_QCC;
434  return get_qcx(s, n-1, q+compno);
435 }
436 
437 /** get start of tile segment */
439 {
440  if (bytestream2_get_bytes_left(&s->g) < 8)
441  return AVERROR(EINVAL);
442 
443  s->curtileno = bytestream2_get_be16u(&s->g); ///< Isot
444  if((unsigned)s->curtileno >= s->numXtiles * s->numYtiles){
445  s->curtileno=0;
446  return AVERROR(EINVAL);
447  }
448 
449  bytestream2_skipu(&s->g, 4); ///< Psot (ignored)
450 
451  if (!bytestream2_get_byteu(&s->g)){ ///< TPsot
452  J2kTile *tile = s->tile + s->curtileno;
453 
454  /* copy defaults */
455  memcpy(tile->codsty, s->codsty, s->ncomponents * sizeof(J2kCodingStyle));
456  memcpy(tile->qntsty, s->qntsty, s->ncomponents * sizeof(J2kQuantStyle));
457  }
458  bytestream2_get_byteu(&s->g); ///< TNsot
459 
460  return 0;
461 }
462 
463 static int init_tile(J2kDecoderContext *s, int tileno)
464 {
465  int compno,
466  tilex = tileno % s->numXtiles,
467  tiley = tileno / s->numXtiles;
468  J2kTile *tile = s->tile + tileno;
469 
470  if (!tile->comp)
471  return AVERROR(ENOMEM);
472  for (compno = 0; compno < s->ncomponents; compno++){
473  J2kComponent *comp = tile->comp + compno;
474  J2kCodingStyle *codsty = tile->codsty + compno;
475  J2kQuantStyle *qntsty = tile->qntsty + compno;
476  int ret; // global bandno
477 
478  comp->coord[0][0] = FFMAX(tilex * s->tile_width + s->tile_offset_x, s->image_offset_x);
479  comp->coord[0][1] = FFMIN((tilex+1)*s->tile_width + s->tile_offset_x, s->width);
480  comp->coord[1][0] = FFMAX(tiley * s->tile_height + s->tile_offset_y, s->image_offset_y);
481  comp->coord[1][1] = FFMIN((tiley+1)*s->tile_height + s->tile_offset_y, s->height);
482 
483  if (ret = ff_j2k_init_component(comp, codsty, qntsty, s->cbps[compno], s->cdx[compno], s->cdy[compno]))
484  return ret;
485  }
486  return 0;
487 }
488 
489 /** read the number of coding passes */
491 {
492  int num;
493  if (!get_bits(s, 1))
494  return 1;
495  if (!get_bits(s, 1))
496  return 2;
497  if ((num = get_bits(s, 2)) != 3)
498  return num < 0 ? num : 3 + num;
499  if ((num = get_bits(s, 5)) != 31)
500  return num < 0 ? num : 6 + num;
501  num = get_bits(s, 7);
502  return num < 0 ? num : 37 + num;
503 }
504 
506 {
507  int res = 0, ret;
508  while (ret = get_bits(s, 1)){
509  if (ret < 0)
510  return ret;
511  res++;
512  }
513  return res;
514 }
515 
516 static int decode_packet(J2kDecoderContext *s, J2kCodingStyle *codsty, J2kResLevel *rlevel, int precno,
517  int layno, uint8_t *expn, int numgbits)
518 {
519  int bandno, cblkny, cblknx, cblkno, ret;
520 
521  if (!(ret = get_bits(s, 1))){
522  j2k_flush(s);
523  return 0;
524  } else if (ret < 0)
525  return ret;
526 
527  for (bandno = 0; bandno < rlevel->nbands; bandno++){
528  J2kBand *band = rlevel->band + bandno;
529  J2kPrec *prec = band->prec + precno;
530  int pos = 0;
531 
532  if (band->coord[0][0] == band->coord[0][1]
533  || band->coord[1][0] == band->coord[1][1])
534  continue;
535 
536  for (cblkny = prec->yi0; cblkny < prec->yi1; cblkny++)
537  for(cblknx = prec->xi0, cblkno = cblkny * band->cblknx + cblknx; cblknx < prec->xi1; cblknx++, cblkno++, pos++){
538  J2kCblk *cblk = band->cblk + cblkno;
539  int incl, newpasses, llen;
540 
541  if (cblk->npasses)
542  incl = get_bits(s, 1);
543  else
544  incl = tag_tree_decode(s, prec->cblkincl + pos, layno+1) == layno;
545  if (!incl)
546  continue;
547  else if (incl < 0)
548  return incl;
549 
550  if (!cblk->npasses)
551  cblk->nonzerobits = expn[bandno] + numgbits - 1 - tag_tree_decode(s, prec->zerobits + pos, 100);
552  if ((newpasses = getnpasses(s)) < 0)
553  return newpasses;
554  if ((llen = getlblockinc(s)) < 0)
555  return llen;
556  cblk->lblock += llen;
557  if ((ret = get_bits(s, av_log2(newpasses) + cblk->lblock)) < 0)
558  return ret;
559  cblk->lengthinc = ret;
560  cblk->npasses += newpasses;
561  }
562  }
563  j2k_flush(s);
564 
565  if (codsty->csty & J2K_CSTY_EPH) {
566  if (bytestream2_peek_be16(&s->g) == J2K_EPH) {
567  bytestream2_skip(&s->g, 2);
568  } else {
569  av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found.\n");
570  }
571  }
572 
573  for (bandno = 0; bandno < rlevel->nbands; bandno++){
574  J2kBand *band = rlevel->band + bandno;
575  int yi, cblknw = band->prec[precno].xi1 - band->prec[precno].xi0;
576  for (yi = band->prec[precno].yi0; yi < band->prec[precno].yi1; yi++){
577  int xi;
578  for (xi = band->prec[precno].xi0; xi < band->prec[precno].xi1; xi++){
579  J2kCblk *cblk = band->cblk + yi * cblknw + xi;
580  if (bytestream2_get_bytes_left(&s->g) < cblk->lengthinc)
581  return AVERROR(EINVAL);
582  bytestream2_get_bufferu(&s->g, cblk->data, cblk->lengthinc);
583  cblk->length += cblk->lengthinc;
584  cblk->lengthinc = 0;
585  }
586  }
587  }
588  return 0;
589 }
590 
592 {
593  int layno, reslevelno, compno, precno, ok_reslevel;
594  s->bit_index = 8;
595  for (layno = 0; layno < tile->codsty[0].nlayers; layno++){
596  ok_reslevel = 1;
597  for (reslevelno = 0; ok_reslevel; reslevelno++){
598  ok_reslevel = 0;
599  for (compno = 0; compno < s->ncomponents; compno++){
600  J2kCodingStyle *codsty = tile->codsty + compno;
601  J2kQuantStyle *qntsty = tile->qntsty + compno;
602  if (reslevelno < codsty->nreslevels){
603  J2kResLevel *rlevel = tile->comp[compno].reslevel + reslevelno;
604  ok_reslevel = 1;
605  for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++){
606  if (decode_packet(s, codsty, rlevel, precno, layno, qntsty->expn +
607  (reslevelno ? 3*(reslevelno-1)+1 : 0), qntsty->nguardbits))
608  return -1;
609  }
610  }
611  }
612  }
613  }
614  return 0;
615 }
616 
617 /* TIER-1 routines */
618 static void decode_sigpass(J2kT1Context *t1, int width, int height, int bpno, int bandno, int bpass_csty_symbol,
619  int vert_causal_ctx_csty_symbol)
620 {
621  int mask = 3 << (bpno - 1), y0, x, y;
622 
623  for (y0 = 0; y0 < height; y0 += 4)
624  for (x = 0; x < width; x++)
625  for (y = y0; y < height && y < y0+4; y++){
626  if ((t1->flags[y+1][x+1] & J2K_T1_SIG_NB)
627  && !(t1->flags[y+1][x+1] & (J2K_T1_SIG | J2K_T1_VIS))){
628  int vert_causal_ctx_csty_loc_symbol = vert_causal_ctx_csty_symbol && (x == 3 && y == 3);
629  if (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_j2k_getnbctxno(t1->flags[y+1][x+1], bandno,
630  vert_causal_ctx_csty_loc_symbol))){
631  int xorbit, ctxno = ff_j2k_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
632  if (bpass_csty_symbol)
633  t1->data[y][x] = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ? -mask : mask;
634  else
635  t1->data[y][x] = (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) ?
636  -mask : mask;
637 
638  ff_j2k_set_significant(t1, x, y, t1->data[y][x] < 0);
639  }
640  t1->flags[y+1][x+1] |= J2K_T1_VIS;
641  }
642  }
643 }
644 
645 static void decode_refpass(J2kT1Context *t1, int width, int height, int bpno)
646 {
647  int phalf, nhalf;
648  int y0, x, y;
649 
650  phalf = 1 << (bpno - 1);
651  nhalf = -phalf;
652 
653  for (y0 = 0; y0 < height; y0 += 4)
654  for (x = 0; x < width; x++)
655  for (y = y0; y < height && y < y0+4; y++){
656  if ((t1->flags[y+1][x+1] & (J2K_T1_SIG | J2K_T1_VIS)) == J2K_T1_SIG){
657  int ctxno = ff_j2k_getrefctxno(t1->flags[y+1][x+1]);
658  int r = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ? phalf : nhalf;
659  t1->data[y][x] += t1->data[y][x] < 0 ? -r : r;
660  t1->flags[y+1][x+1] |= J2K_T1_REF;
661  }
662  }
663 }
664 
666  int bpno, int bandno, int seg_symbols)
667 {
668  int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
669 
670  for (y0 = 0; y0 < height; y0 += 4) {
671  for (x = 0; x < width; x++){
672  if (y0 + 3 < height && !(
673  (t1->flags[y0+1][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG)) ||
674  (t1->flags[y0+2][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG)) ||
675  (t1->flags[y0+3][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG)) ||
676  (t1->flags[y0+4][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG)))){
677  if (!ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL))
678  continue;
679  runlen = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
680  runlen = (runlen << 1) | ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
681  dec = 1;
682  } else{
683  runlen = 0;
684  dec = 0;
685  }
686 
687  for (y = y0 + runlen; y < y0 + 4 && y < height; y++){
688  if (!dec){
689  if (!(t1->flags[y+1][x+1] & (J2K_T1_SIG | J2K_T1_VIS)))
690  dec = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_j2k_getnbctxno(t1->flags[y+1][x+1],
691  bandno, 0));
692  }
693  if (dec){
694  int xorbit, ctxno = ff_j2k_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
695  t1->data[y][x] = (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) ? -mask : mask;
696  ff_j2k_set_significant(t1, x, y, t1->data[y][x] < 0);
697  }
698  dec = 0;
699  t1->flags[y+1][x+1] &= ~J2K_T1_VIS;
700  }
701  }
702  }
703  if (seg_symbols) {
704  int val;
705  val = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
706  val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
707  val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
708  val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
709  if (val != 0xa) {
710  av_log(s->avctx, AV_LOG_ERROR,"Segmentation symbol value incorrect\n");
711  }
712  }
713 }
714 
716  int width, int height, int bandpos)
717 {
718  int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1, y, clnpass_cnt = 0;
719  int bpass_csty_symbol = J2K_CBLK_BYPASS & codsty->cblk_style;
720  int vert_causal_ctx_csty_symbol = J2K_CBLK_VSC & codsty->cblk_style;
721 
722  for (y = 0; y < height+2; y++)
723  memset(t1->flags[y], 0, (width+2)*sizeof(int));
724 
725  for (y = 0; y < height; y++)
726  memset(t1->data[y], 0, width*sizeof(int));
727 
728  cblk->data[cblk->length] = 0xff;
729  cblk->data[cblk->length+1] = 0xff;
730  ff_mqc_initdec(&t1->mqc, cblk->data);
731 
732  while(passno--){
733  switch(pass_t){
734  case 0: decode_sigpass(t1, width, height, bpno+1, bandpos,
735  bpass_csty_symbol && (clnpass_cnt >= 4), vert_causal_ctx_csty_symbol);
736  break;
737  case 1: decode_refpass(t1, width, height, bpno+1);
738  if (bpass_csty_symbol && clnpass_cnt >= 4)
739  ff_mqc_initdec(&t1->mqc, cblk->data);
740  break;
741  case 2: decode_clnpass(s, t1, width, height, bpno+1, bandpos,
742  codsty->cblk_style & J2K_CBLK_SEGSYM);
743  clnpass_cnt = clnpass_cnt + 1;
744  if (bpass_csty_symbol && clnpass_cnt >= 4)
745  ff_mqc_initdec(&t1->mqc, cblk->data);
746  break;
747  }
748 
749  pass_t++;
750  if (pass_t == 3){
751  bpno--;
752  pass_t = 0;
753  }
754  }
755  return 0;
756 }
757 
758 static void mct_decode(J2kDecoderContext *s, J2kTile *tile)
759 {
760  int i, *src[3], i0, i1, i2, csize = 1;
761 
762  for (i = 0; i < 3; i++)
763  src[i] = tile->comp[i].data;
764 
765  for (i = 0; i < 2; i++)
766  csize *= tile->comp[0].coord[i][1] - tile->comp[0].coord[i][0];
767 
768  if (tile->codsty[0].transform == FF_DWT97){
769  for (i = 0; i < csize; i++){
770  i0 = *src[0] + (*src[2] * 46802 >> 16);
771  i1 = *src[0] - (*src[1] * 22553 + *src[2] * 46802 >> 16);
772  i2 = *src[0] + (116130 * *src[1] >> 16);
773  *src[0]++ = i0;
774  *src[1]++ = i1;
775  *src[2]++ = i2;
776  }
777  } else{
778  for (i = 0; i < csize; i++){
779  i1 = *src[0] - (*src[2] + *src[1] >> 2);
780  i0 = i1 + *src[2];
781  i2 = i1 + *src[1];
782  *src[0]++ = i0;
783  *src[1]++ = i1;
784  *src[2]++ = i2;
785  }
786  }
787 }
788 
789 static int decode_tile(J2kDecoderContext *s, J2kTile *tile)
790 {
791  int compno, reslevelno, bandno;
792  int x, y, *src[4];
793  uint8_t *line;
795 
796  for (compno = 0; compno < s->ncomponents; compno++){
797  J2kComponent *comp = tile->comp + compno;
798  J2kCodingStyle *codsty = tile->codsty + compno;
799 
800  for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
801  J2kResLevel *rlevel = comp->reslevel + reslevelno;
802  for (bandno = 0; bandno < rlevel->nbands; bandno++){
803  J2kBand *band = rlevel->band + bandno;
804  int cblkx, cblky, cblkno=0, xx0, x0, xx1, y0, yy0, yy1, bandpos;
805 
806  bandpos = bandno + (reslevelno > 0);
807 
808  yy0 = bandno == 0 ? 0 : comp->reslevel[reslevelno-1].coord[1][1] - comp->reslevel[reslevelno-1].coord[1][0];
809  y0 = yy0;
810  yy1 = FFMIN(ff_j2k_ceildiv(band->coord[1][0] + 1, band->codeblock_height) * band->codeblock_height,
811  band->coord[1][1]) - band->coord[1][0] + yy0;
812 
813  if (band->coord[0][0] == band->coord[0][1] || band->coord[1][0] == band->coord[1][1])
814  continue;
815 
816  for (cblky = 0; cblky < band->cblkny; cblky++){
817  if (reslevelno == 0 || bandno == 1)
818  xx0 = 0;
819  else
820  xx0 = comp->reslevel[reslevelno-1].coord[0][1] - comp->reslevel[reslevelno-1].coord[0][0];
821  x0 = xx0;
822  xx1 = FFMIN(ff_j2k_ceildiv(band->coord[0][0] + 1, band->codeblock_width) * band->codeblock_width,
823  band->coord[0][1]) - band->coord[0][0] + xx0;
824 
825  for (cblkx = 0; cblkx < band->cblknx; cblkx++, cblkno++){
826  int y, x;
827  decode_cblk(s, codsty, &t1, band->cblk + cblkno, xx1 - xx0, yy1 - yy0, bandpos);
828  if (codsty->transform == FF_DWT53){
829  for (y = yy0; y < yy1; y+=s->cdy[compno]){
830  int *ptr = t1.data[y-yy0];
831  for (x = xx0; x < xx1; x+=s->cdx[compno]){
832  comp->data[(comp->coord[0][1] - comp->coord[0][0]) * y + x] = *ptr++ >> 1;
833  }
834  }
835  } else{
836  for (y = yy0; y < yy1; y+=s->cdy[compno]){
837  int *ptr = t1.data[y-yy0];
838  for (x = xx0; x < xx1; x+=s->cdx[compno]){
839  int tmp = ((int64_t)*ptr++) * ((int64_t)band->stepsize) >> 13, tmp2;
840  tmp2 = FFABS(tmp>>1) + (tmp&1);
841  comp->data[(comp->coord[0][1] - comp->coord[0][0]) * y + x] = tmp < 0 ? -tmp2 : tmp2;
842  }
843  }
844  }
845  xx0 = xx1;
846  xx1 = FFMIN(xx1 + band->codeblock_width, band->coord[0][1] - band->coord[0][0] + x0);
847  }
848  yy0 = yy1;
849  yy1 = FFMIN(yy1 + band->codeblock_height, band->coord[1][1] - band->coord[1][0] + y0);
850  }
851  }
852  }
853  ff_j2k_dwt_decode(&comp->dwt, comp->data);
854  src[compno] = comp->data;
855  }
856  if (tile->codsty[0].mct)
857  mct_decode(s, tile);
858 
859  if (s->precision <= 8) {
860  for (compno = 0; compno < s->ncomponents; compno++){
861  y = tile->comp[compno].coord[1][0] - s->image_offset_y;
862  line = s->picture.data[0] + y * s->picture.linesize[0];
863  for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]){
864  uint8_t *dst;
865 
866  x = tile->comp[compno].coord[0][0] - s->image_offset_x;
867  dst = line + x * s->ncomponents + compno;
868 
869  for (; x < tile->comp[compno].coord[0][1] - s->image_offset_x; x += s->cdx[compno]) {
870  *src[compno] += 1 << (s->cbps[compno]-1);
871  if (*src[compno] < 0)
872  *src[compno] = 0;
873  else if (*src[compno] >= (1 << s->cbps[compno]))
874  *src[compno] = (1 << s->cbps[compno]) - 1;
875  *dst = *src[compno]++;
876  dst += s->ncomponents;
877  }
878  line += s->picture.linesize[0];
879  }
880  }
881  } else {
882  for (compno = 0; compno < s->ncomponents; compno++) {
883  y = tile->comp[compno].coord[1][0] - s->image_offset_y;
884  line = s->picture.data[0] + y * s->picture.linesize[0];
885  for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) {
886  uint16_t *dst;
887 
888  x = tile->comp[compno].coord[0][0] - s->image_offset_x;
889  dst = (uint16_t *)(line + (x * s->ncomponents + compno) * 2);
890  for (; x < tile->comp[compno].coord[0][1] - s->image_offset_x; x += s-> cdx[compno]) {
891  int32_t val;
892 
893  val = *src[compno]++ << (16 - s->cbps[compno]);
894  val += 1 << 15;
895  val = av_clip(val, 0, (1 << 16) - 1);
896  *dst = val;
897  dst += s->ncomponents;
898  }
899  line += s->picture.linesize[0];
900  }
901  }
902  }
903  return 0;
904 }
905 
906 static void cleanup(J2kDecoderContext *s)
907 {
908  int tileno, compno;
909  for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
910  for (compno = 0; compno < s->ncomponents; compno++){
911  J2kComponent *comp = s->tile[tileno].comp + compno;
912  J2kCodingStyle *codsty = s->tile[tileno].codsty + compno;
913 
914  ff_j2k_cleanup(comp, codsty);
915  }
916  av_freep(&s->tile[tileno].comp);
917  }
918  av_freep(&s->tile);
919 }
920 
922 {
923  J2kCodingStyle *codsty = s->codsty;
924  J2kQuantStyle *qntsty = s->qntsty;
925  uint8_t *properties = s->properties;
926 
927  for (;;){
928  int oldpos, marker, len, ret = 0;
929 
930  if (bytestream2_get_bytes_left(&s->g) < 2){
931  av_log(s->avctx, AV_LOG_ERROR, "Missing EOC\n");
932  break;
933  }
934 
935  marker = bytestream2_get_be16u(&s->g);
936  av_dlog(s->avctx, "marker 0x%.4X at pos 0x%x\n", marker, bytestream2_tell(&s->g) - 4);
937  oldpos = bytestream2_tell(&s->g);
938 
939  if (marker == J2K_SOD){
940  J2kTile *tile = s->tile + s->curtileno;
941  if (ret = init_tile(s, s->curtileno)) {
942  av_log(s->avctx, AV_LOG_ERROR, "tile initialization failed\n");
943  return ret;
944  }
945  if (ret = decode_packets(s, tile)) {
946  av_log(s->avctx, AV_LOG_ERROR, "packets decoding failed\n");
947  return ret;
948  }
949  continue;
950  }
951  if (marker == J2K_EOC)
952  break;
953 
954  if (bytestream2_get_bytes_left(&s->g) < 2)
955  return AVERROR(EINVAL);
956  len = bytestream2_get_be16u(&s->g);
957  switch (marker){
958  case J2K_SIZ:
959  ret = get_siz(s);
960  break;
961  case J2K_COC:
962  ret = get_coc(s, codsty, properties);
963  break;
964  case J2K_COD:
965  ret = get_cod(s, codsty, properties);
966  break;
967  case J2K_QCC:
968  ret = get_qcc(s, len, qntsty, properties);
969  break;
970  case J2K_QCD:
971  ret = get_qcd(s, len, qntsty, properties);
972  break;
973  case J2K_SOT:
974  if (!(ret = get_sot(s))){
975  codsty = s->tile[s->curtileno].codsty;
976  qntsty = s->tile[s->curtileno].qntsty;
977  properties = s->tile[s->curtileno].properties;
978  }
979  break;
980  case J2K_COM:
981  // the comment is ignored
982  bytestream2_skip(&s->g, len - 2);
983  break;
984  default:
985  av_log(s->avctx, AV_LOG_ERROR, "unsupported marker 0x%.4X at pos 0x%x\n", marker, bytestream2_tell(&s->g) - 4);
986  bytestream2_skip(&s->g, len - 2);
987  break;
988  }
989  if (bytestream2_tell(&s->g) - oldpos != len || ret){
990  av_log(s->avctx, AV_LOG_ERROR, "error during processing marker segment %.4x\n", marker);
991  return ret ? ret : -1;
992  }
993  }
994  return 0;
995 }
996 
998 {
999  uint32_t atom_size, atom;
1000  int found_codestream = 0, search_range = 10;
1001 
1002  while(!found_codestream && search_range && bytestream2_get_bytes_left(&s->g) >= 8) {
1003  atom_size = bytestream2_get_be32u(&s->g);
1004  atom = bytestream2_get_be32u(&s->g);
1005  if (atom == JP2_CODESTREAM) {
1006  found_codestream = 1;
1007  } else {
1008  if (bytestream2_get_bytes_left(&s->g) < atom_size - 8)
1009  return 0;
1010  bytestream2_skipu(&s->g, atom_size - 8);
1011  search_range--;
1012  }
1013  }
1014 
1015  if (found_codestream)
1016  return 1;
1017  return 0;
1018 }
1019 
1020 static int decode_frame(AVCodecContext *avctx,
1021  void *data, int *got_frame,
1022  AVPacket *avpkt)
1023 {
1024  J2kDecoderContext *s = avctx->priv_data;
1025  AVFrame *picture = data;
1026  int tileno, ret;
1027 
1028  bytestream2_init(&s->g, avpkt->data, avpkt->size);
1029  s->curtileno = -1;
1030 
1031  if (bytestream2_get_bytes_left(&s->g) < 2) {
1032  ret = AVERROR(EINVAL);
1033  goto err_out;
1034  }
1035 
1036  // check if the image is in jp2 format
1037  if (bytestream2_get_bytes_left(&s->g) >= 12 &&
1038  (bytestream2_get_be32u(&s->g) == 12) &&
1039  (bytestream2_get_be32u(&s->g) == JP2_SIG_TYPE) &&
1040  (bytestream2_get_be32u(&s->g) == JP2_SIG_VALUE)) {
1041  if(!jp2_find_codestream(s)) {
1042  av_log(avctx, AV_LOG_ERROR, "couldn't find jpeg2k codestream atom\n");
1043  ret = -1;
1044  goto err_out;
1045  }
1046  } else {
1047  bytestream2_seek(&s->g, 0, SEEK_SET);
1048  }
1049 
1050  if (bytestream2_get_be16u(&s->g) != J2K_SOC){
1051  av_log(avctx, AV_LOG_ERROR, "SOC marker not present\n");
1052  ret = -1;
1053  goto err_out;
1054  }
1055  if (ret = decode_codestream(s))
1056  goto err_out;
1057 
1058  for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++)
1059  if (ret = decode_tile(s, s->tile + tileno))
1060  goto err_out;
1061 
1062  cleanup(s);
1063 
1064  *got_frame = 1;
1065  *picture = s->picture;
1066 
1067  return bytestream2_tell(&s->g);
1068 
1069 err_out:
1070  cleanup(s);
1071  return ret;
1072 }
1073 
1075 {
1076  J2kDecoderContext *s = avctx->priv_data;
1077 
1078  s->avctx = avctx;
1080  avctx->coded_frame = (AVFrame*)&s->picture;
1081 
1083 
1084  return 0;
1085 }
1086 
1088 {
1089  J2kDecoderContext *s = avctx->priv_data;
1090 
1091  if (s->picture.data[0])
1092  avctx->release_buffer(avctx, &s->picture);
1093 
1094  return 0;
1095 }
1096 
1098  .name = "j2k",
1099  .type = AVMEDIA_TYPE_VIDEO,
1100  .id = AV_CODEC_ID_JPEG2000,
1101  .priv_data_size = sizeof(J2kDecoderContext),
1102  .init = j2kdec_init,
1103  .close = decode_end,
1104  .decode = decode_frame,
1105  .capabilities = CODEC_CAP_EXPERIMENTAL,
1106  .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
1107 };