]> git.sesse.net Git - ffmpeg/blob - libavcodec/j2kdec.c
Merge commit '0f689a3d97a1d3053ca0e9516b296909a72ff31e'
[ffmpeg] / libavcodec / j2kdec.c
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 "thread.h"
34 #include "j2k.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 {
45    J2kComponent *comp;
46    uint8_t properties[4];
47    J2kCodingStyle codsty[4];
48    J2kQuantStyle  qntsty[4];
49 } J2kTile;
50
51 typedef struct {
52     AVCodecContext *avctx;
53     AVFrame *picture;
54     GetByteContext g;
55
56     int width, height; ///< image width and height
57     int image_offset_x, image_offset_y;
58     int tile_offset_x, tile_offset_y;
59     uint8_t cbps[4]; ///< bits per sample in particular components
60     uint8_t sgnd[4]; ///< if a component is signed
61     uint8_t properties[4];
62     int cdx[4], cdy[4];
63     int precision;
64     int ncomponents;
65     int tile_width, tile_height; ///< tile size
66     int numXtiles, numYtiles;
67     int maxtilelen;
68
69     J2kCodingStyle codsty[4];
70     J2kQuantStyle  qntsty[4];
71
72     int bit_index;
73
74     int curtileno;
75
76     J2kTile *tile;
77 } J2kDecoderContext;
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
94 static void j2k_flush(J2kDecoderContext *s)
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
166 /** decode the value stored in node */
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 */
205 /** get sizes and offsets of image, tiles; number of components */
206 static int get_siz(J2kDecoderContext *s)
207 {
208     int i, ret;
209     ThreadFrame frame = { .f = s->picture };
210
211     if (bytestream2_get_bytes_left(&s->g) < 36)
212         return AVERROR(EINVAL);
213
214                         bytestream2_get_be16u(&s->g); // Rsiz (skipped)
215              s->width = bytestream2_get_be32u(&s->g); // width
216             s->height = bytestream2_get_be32u(&s->g); // height
217     s->image_offset_x = bytestream2_get_be32u(&s->g); // X0Siz
218     s->image_offset_y = bytestream2_get_be32u(&s->g); // Y0Siz
219
220         s->tile_width = bytestream2_get_be32u(&s->g); // XTSiz
221        s->tile_height = bytestream2_get_be32u(&s->g); // YTSiz
222      s->tile_offset_x = bytestream2_get_be32u(&s->g); // XT0Siz
223      s->tile_offset_y = bytestream2_get_be32u(&s->g); // YT0Siz
224        s->ncomponents = bytestream2_get_be16u(&s->g); // CSiz
225
226     if(s->ncomponents <= 0 || s->ncomponents > 4) {
227         av_log(s->avctx, AV_LOG_ERROR, "unsupported/invalid ncomponents: %d\n", s->ncomponents);
228         return AVERROR(EINVAL);
229     }
230     if(s->tile_width<=0 || s->tile_height<=0)
231         return AVERROR(EINVAL);
232
233     if (bytestream2_get_bytes_left(&s->g) < 3 * s->ncomponents)
234         return AVERROR(EINVAL);
235
236     for (i = 0; i < s->ncomponents; i++){ // Ssiz_i XRsiz_i, YRsiz_i
237         uint8_t x = bytestream2_get_byteu(&s->g);
238         s->cbps[i] = (x & 0x7f) + 1;
239         s->precision = FFMAX(s->cbps[i], s->precision);
240         s->sgnd[i] = !!(x & 0x80);
241         s->cdx[i] = bytestream2_get_byteu(&s->g);
242         s->cdy[i] = bytestream2_get_byteu(&s->g);
243     }
244
245     s->numXtiles = ff_j2k_ceildiv(s->width - s->tile_offset_x, s->tile_width);
246     s->numYtiles = ff_j2k_ceildiv(s->height - s->tile_offset_y, s->tile_height);
247
248     if(s->numXtiles * (uint64_t)s->numYtiles > INT_MAX/sizeof(J2kTile))
249         return AVERROR(EINVAL);
250
251     s->tile = av_mallocz(s->numXtiles * s->numYtiles * sizeof(J2kTile));
252     if (!s->tile)
253         return AVERROR(ENOMEM);
254
255     for (i = 0; i < s->numXtiles * s->numYtiles; i++){
256         J2kTile *tile = s->tile + i;
257
258         tile->comp = av_mallocz(s->ncomponents * sizeof(J2kComponent));
259         if (!tile->comp)
260             return AVERROR(ENOMEM);
261     }
262
263     s->avctx->width  = s->width  - s->image_offset_x;
264     s->avctx->height = s->height - s->image_offset_y;
265
266     switch(s->ncomponents){
267     case 1:
268         if (s->precision > 8) {
269             s->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
270         } else {
271             s->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
272         }
273         break;
274     case 3:
275         if (s->precision > 8) {
276             s->avctx->pix_fmt = AV_PIX_FMT_RGB48;
277         } else {
278             s->avctx->pix_fmt = AV_PIX_FMT_RGB24;
279         }
280         break;
281     case 4:
282         s->avctx->pix_fmt = AV_PIX_FMT_RGBA;
283         break;
284     }
285
286
287     if ((ret = ff_thread_get_buffer(s->avctx, &frame, 0)) < 0)
288         return ret;
289
290     s->picture->pict_type = AV_PICTURE_TYPE_I;
291     s->picture->key_frame = 1;
292
293     return 0;
294 }
295
296 /** get common part for COD and COC segments */
297 static int get_cox(J2kDecoderContext *s, J2kCodingStyle *c)
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 */
438 static uint8_t get_sot(J2kDecoderContext *s)
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 */
490 static int getnpasses(J2kDecoderContext *s)
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
505 static int getlblockinc(J2kDecoderContext *s)
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
591 static int decode_packets(J2kDecoderContext *s, J2kTile *tile)
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
665 static void decode_clnpass(J2kDecoderContext *s, J2kT1Context *t1, int width, int height,
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
715 static int decode_cblk(J2kDecoderContext *s, J2kCodingStyle *codsty, J2kT1Context *t1, J2kCblk *cblk,
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;
794     J2kT1Context t1;
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
921 static int decode_codestream(J2kDecoderContext *s)
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
997 static int jp2_find_codestream(J2kDecoderContext *s)
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     s->picture = picture;
1029
1030     bytestream2_init(&s->g, avpkt->data, avpkt->size);
1031     s->curtileno = -1;
1032
1033     if (bytestream2_get_bytes_left(&s->g) < 2) {
1034         ret = AVERROR(EINVAL);
1035         goto err_out;
1036     }
1037
1038     // check if the image is in jp2 format
1039     if (bytestream2_get_bytes_left(&s->g) >= 12 &&
1040        (bytestream2_get_be32u(&s->g) == 12) &&
1041        (bytestream2_get_be32u(&s->g) == JP2_SIG_TYPE) &&
1042        (bytestream2_get_be32u(&s->g) == JP2_SIG_VALUE)) {
1043         if(!jp2_find_codestream(s)) {
1044             av_log(avctx, AV_LOG_ERROR, "couldn't find jpeg2k codestream atom\n");
1045             ret = -1;
1046             goto err_out;
1047         }
1048     } else {
1049         bytestream2_seek(&s->g, 0, SEEK_SET);
1050     }
1051
1052     if (bytestream2_get_be16u(&s->g) != J2K_SOC){
1053         av_log(avctx, AV_LOG_ERROR, "SOC marker not present\n");
1054         ret = -1;
1055         goto err_out;
1056     }
1057     if (ret = decode_codestream(s))
1058         goto err_out;
1059
1060     for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++)
1061         if (ret = decode_tile(s, s->tile + tileno))
1062             goto err_out;
1063
1064     cleanup(s);
1065
1066     *got_frame = 1;
1067
1068     return bytestream2_tell(&s->g);
1069
1070 err_out:
1071     cleanup(s);
1072     return ret;
1073 }
1074
1075 static av_cold int j2kdec_init(AVCodecContext *avctx)
1076 {
1077     J2kDecoderContext *s = avctx->priv_data;
1078
1079     s->avctx = avctx;
1080
1081     ff_j2k_init_tier1_luts();
1082
1083     return 0;
1084 }
1085
1086 AVCodec ff_j2k_decoder = {
1087     .name           = "j2k",
1088     .type           = AVMEDIA_TYPE_VIDEO,
1089     .id             = AV_CODEC_ID_JPEG2000,
1090     .priv_data_size = sizeof(J2kDecoderContext),
1091     .init           = j2kdec_init,
1092     .decode         = decode_frame,
1093     .capabilities   = CODEC_CAP_EXPERIMENTAL | CODEC_CAP_FRAME_THREADS,
1094     .long_name      = NULL_IF_CONFIG_SMALL("JPEG 2000"),
1095 };