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