]> git.sesse.net Git - ffmpeg/blob - libavcodec/jpeg2000dec.c
xsubdec: Convert to the new bitstream reader
[ffmpeg] / libavcodec / jpeg2000dec.c
1 /*
2  * JPEG 2000 image decoder
3  * Copyright (c) 2007 Kamil Nowosad
4  * Copyright (c) 2013 Nicolas Bertrand <nicoinattendu@gmail.com>
5  *
6  * This file is part of Libav.
7  *
8  * Libav is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * Libav is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 /**
24  * @file
25  * JPEG 2000 image decoder
26  */
27
28 #include <inttypes.h>
29
30 #include "libavutil/attributes.h"
31 #include "libavutil/common.h"
32 #include "libavutil/opt.h"
33 #include "avcodec.h"
34 #include "bytestream.h"
35 #include "internal.h"
36 #include "thread.h"
37 #include "jpeg2000.h"
38 #include "jpeg2000dsp.h"
39 #include "profiles.h"
40
41 #define JP2_SIG_TYPE    0x6A502020
42 #define JP2_SIG_VALUE   0x0D0A870A
43 #define JP2_CODESTREAM  0x6A703263
44
45 #define HAD_COC 0x01
46 #define HAD_QCC 0x02
47
48 typedef struct Jpeg2000TilePart {
49     uint8_t tile_index;                 // Tile index who refers the tile-part
50     const uint8_t *tp_end;
51     GetByteContext tpg;                 // bit stream in tile-part
52 } Jpeg2000TilePart;
53
54 /* RMK: For JPEG2000 DCINEMA 3 tile-parts in a tile
55  * one per component, so tile_part elements have a size of 3 */
56 typedef struct Jpeg2000Tile {
57     Jpeg2000Component   *comp;
58     uint8_t             properties[4];
59     Jpeg2000CodingStyle codsty[4];
60     Jpeg2000QuantStyle  qntsty[4];
61     Jpeg2000TilePart    tile_part[3];
62     uint16_t tp_idx;                    // Tile-part index
63 } Jpeg2000Tile;
64
65 typedef struct Jpeg2000DecoderContext {
66     AVClass         *class;
67     AVCodecContext  *avctx;
68     GetByteContext g;
69
70     int             width, height;
71     int             image_offset_x, image_offset_y;
72     int             tile_offset_x, tile_offset_y;
73     uint8_t         cbps[4];    // bits per sample in particular components
74     uint8_t         sgnd[4];    // if a component is signed
75     uint8_t         properties[4];
76     int             cdx[4], cdy[4];
77     int             precision;
78     int             ncomponents;
79     int             tile_width, tile_height;
80     unsigned        numXtiles, numYtiles;
81     int             maxtilelen;
82
83     Jpeg2000CodingStyle codsty[4];
84     Jpeg2000QuantStyle  qntsty[4];
85
86     int             bit_index;
87
88     int16_t         curtileno;
89     Jpeg2000Tile    *tile;
90     Jpeg2000DSPContext dsp;
91
92     /*options parameters*/
93     int             reduction_factor;
94 } Jpeg2000DecoderContext;
95
96 /* get_bits functions for JPEG2000 packet bitstream
97  * It is a get_bit function with a bit-stuffing routine. If the value of the
98  * byte is 0xFF, the next byte includes an extra zero bit stuffed into the MSB.
99  * cf. ISO-15444-1:2002 / B.10.1 Bit-stuffing routine */
100 static int get_bits(Jpeg2000DecoderContext *s, int n)
101 {
102     int res = 0;
103     while (--n >= 0) {
104         res <<= 1;
105         if (s->bit_index == 0) {
106             s->bit_index = 7 + (bytestream2_get_byte(&s->g) != 0xFFu);
107         }
108         s->bit_index--;
109         res |= (bytestream2_peek_byte(&s->g) >> s->bit_index) & 1;
110     }
111     return res;
112 }
113
114 static void jpeg2000_flush(Jpeg2000DecoderContext *s)
115 {
116     if (bytestream2_get_byte(&s->g) == 0xff)
117         bytestream2_skip(&s->g, 1);
118     s->bit_index = 8;
119 }
120
121 /* decode the value stored in node */
122 static int tag_tree_decode(Jpeg2000DecoderContext *s, Jpeg2000TgtNode *node,
123                            int threshold)
124 {
125     Jpeg2000TgtNode *stack[30];
126     int sp = -1, curval = 0;
127
128     if (!node)
129         return AVERROR_INVALIDDATA;
130
131     while (node && !node->vis) {
132         stack[++sp] = node;
133         node        = node->parent;
134     }
135
136     if (node)
137         curval = node->val;
138     else
139         curval = stack[sp]->val;
140
141     while (curval < threshold && sp >= 0) {
142         if (curval < stack[sp]->val)
143             curval = stack[sp]->val;
144         while (curval < threshold) {
145             int ret;
146             if ((ret = get_bits(s, 1)) > 0) {
147                 stack[sp]->vis++;
148                 break;
149             } else if (!ret)
150                 curval++;
151             else
152                 return ret;
153         }
154         stack[sp]->val = curval;
155         sp--;
156     }
157     return curval;
158 }
159
160 /* marker segments */
161 /* get sizes and offsets of image, tiles; number of components */
162 static int get_siz(Jpeg2000DecoderContext *s)
163 {
164     int i;
165     int ncomponents;
166
167     if (bytestream2_get_bytes_left(&s->g) < 36)
168         return AVERROR_INVALIDDATA;
169
170     s->avctx->profile = bytestream2_get_be16u(&s->g); // Rsiz
171     s->width          = bytestream2_get_be32u(&s->g); // Width
172     s->height         = bytestream2_get_be32u(&s->g); // Height
173     s->image_offset_x = bytestream2_get_be32u(&s->g); // X0Siz
174     s->image_offset_y = bytestream2_get_be32u(&s->g); // Y0Siz
175     s->tile_width     = bytestream2_get_be32u(&s->g); // XTSiz
176     s->tile_height    = bytestream2_get_be32u(&s->g); // YTSiz
177     s->tile_offset_x  = bytestream2_get_be32u(&s->g); // XT0Siz
178     s->tile_offset_y  = bytestream2_get_be32u(&s->g); // YT0Siz
179     ncomponents       = bytestream2_get_be16u(&s->g); // CSiz
180
181     if (ncomponents <= 0) {
182         av_log(s->avctx, AV_LOG_ERROR, "Invalid number of components: %d\n",
183                s->ncomponents);
184         return AVERROR_INVALIDDATA;
185     }
186
187     if (ncomponents > 4) {
188         avpriv_request_sample(s->avctx, "Support for %d components",
189                               s->ncomponents);
190         return AVERROR_PATCHWELCOME;
191     }
192
193     s->ncomponents = ncomponents;
194
195     if (s->tile_width <= 0 || s->tile_height <= 0 ||
196         s->tile_width > s->width || s->tile_height > s->height) {
197         av_log(s->avctx, AV_LOG_ERROR, "Invalid tile dimension %dx%d.\n",
198                s->tile_width, s->tile_height);
199         return AVERROR_INVALIDDATA;
200     }
201
202     if (bytestream2_get_bytes_left(&s->g) < 3 * s->ncomponents)
203         return AVERROR_INVALIDDATA;
204
205     for (i = 0; i < s->ncomponents; i++) { // Ssiz_i XRsiz_i, YRsiz_i
206         uint8_t x    = bytestream2_get_byteu(&s->g);
207         s->cbps[i]   = (x & 0x7f) + 1;
208         s->precision = FFMAX(s->cbps[i], s->precision);
209         s->sgnd[i]   = !!(x & 0x80);
210         s->cdx[i]    = bytestream2_get_byteu(&s->g);
211         s->cdy[i]    = bytestream2_get_byteu(&s->g);
212
213         if (s->cdx[i] != 1 || s->cdy[i] != 1) {
214             avpriv_request_sample(s->avctx,
215                                   "CDxy values %d %d for component %d",
216                                   s->cdx[i], s->cdy[i], i);
217             if (!s->cdx[i] || !s->cdy[i])
218                 return AVERROR_INVALIDDATA;
219             else
220                 return AVERROR_PATCHWELCOME;
221         }
222     }
223
224     s->numXtiles = ff_jpeg2000_ceildiv(s->width  - s->tile_offset_x, s->tile_width);
225     s->numYtiles = ff_jpeg2000_ceildiv(s->height - s->tile_offset_y, s->tile_height);
226
227     s->tile = av_mallocz_array(s->numXtiles * s->numYtiles, sizeof(*s->tile));
228     if (!s->tile) {
229         s->numXtiles = s->numYtiles = 0;
230         return AVERROR(ENOMEM);
231     }
232
233     for (i = 0; i < s->numXtiles * s->numYtiles; i++) {
234         Jpeg2000Tile *tile = s->tile + i;
235
236         tile->comp = av_mallocz(s->ncomponents * sizeof(*tile->comp));
237         if (!tile->comp)
238             return AVERROR(ENOMEM);
239     }
240
241     /* compute image size with reduction factor */
242     s->avctx->width  = ff_jpeg2000_ceildivpow2(s->width  - s->image_offset_x,
243                                                s->reduction_factor);
244     s->avctx->height = ff_jpeg2000_ceildivpow2(s->height - s->image_offset_y,
245                                                s->reduction_factor);
246
247     switch (s->ncomponents) {
248     case 1:
249         if (s->precision > 8)
250             s->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
251         else
252             s->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
253         break;
254     case 3:
255         switch (s->avctx->profile) {
256         case FF_PROFILE_JPEG2000_DCINEMA_2K:
257         case FF_PROFILE_JPEG2000_DCINEMA_4K:
258             /* XYZ color-space for digital cinema profiles */
259             s->avctx->pix_fmt = AV_PIX_FMT_XYZ12;
260             break;
261         default:
262             if (s->precision > 8)
263                 s->avctx->pix_fmt = AV_PIX_FMT_RGB48;
264             else
265                 s->avctx->pix_fmt = AV_PIX_FMT_RGB24;
266             break;
267         }
268         break;
269     case 4:
270         s->avctx->pix_fmt = AV_PIX_FMT_RGBA;
271         break;
272     default:
273         /* pixel format can not be identified */
274         s->avctx->pix_fmt = AV_PIX_FMT_NONE;
275         break;
276     }
277     return 0;
278 }
279
280 /* get common part for COD and COC segments */
281 static int get_cox(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c)
282 {
283     uint8_t byte;
284
285     if (bytestream2_get_bytes_left(&s->g) < 5)
286         return AVERROR_INVALIDDATA;
287
288     /*  nreslevels = number of resolution levels
289                    = number of decomposition level +1 */
290     c->nreslevels = bytestream2_get_byteu(&s->g) + 1;
291
292     if (c->nreslevels > JPEG2000_MAX_RESLEVELS)
293         return AVERROR_INVALIDDATA;
294
295     /* compute number of resolution levels to decode */
296     if (c->nreslevels < s->reduction_factor)
297         c->nreslevels2decode = 1;
298     else
299         c->nreslevels2decode = c->nreslevels - s->reduction_factor;
300
301     c->log2_cblk_width  = bytestream2_get_byteu(&s->g) + 2; // cblk width
302     c->log2_cblk_height = bytestream2_get_byteu(&s->g) + 2; // cblk height
303
304     if (c->log2_cblk_width > 10 || c->log2_cblk_height > 10 ||
305         c->log2_cblk_width + c->log2_cblk_height > 12) {
306         av_log(s->avctx, AV_LOG_ERROR, "cblk size invalid\n");
307         return AVERROR_INVALIDDATA;
308     }
309
310     c->cblk_style = bytestream2_get_byteu(&s->g);
311     if (c->cblk_style != 0) { // cblk style
312         avpriv_request_sample(s->avctx, "Support for extra cblk styles");
313         return AVERROR_PATCHWELCOME;
314     }
315     c->transform = bytestream2_get_byteu(&s->g); // DWT transformation type
316     /* set integer 9/7 DWT in case of BITEXACT flag */
317     if ((s->avctx->flags & AV_CODEC_FLAG_BITEXACT) && (c->transform == FF_DWT97))
318         c->transform = FF_DWT97_INT;
319
320     if (c->csty & JPEG2000_CSTY_PREC) {
321         int i;
322         for (i = 0; i < c->nreslevels; i++) {
323             byte = bytestream2_get_byte(&s->g);
324             c->log2_prec_widths[i]  =  byte       & 0x0F;    // precinct PPx
325             c->log2_prec_heights[i] = (byte >> 4) & 0x0F;    // precinct PPy
326         }
327     } else {
328         memset(c->log2_prec_widths , 15, sizeof(c->log2_prec_widths ));
329         memset(c->log2_prec_heights, 15, sizeof(c->log2_prec_heights));
330     }
331     return 0;
332 }
333
334 /* get coding parameters for a particular tile or whole image*/
335 static int get_cod(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
336                    uint8_t *properties)
337 {
338     Jpeg2000CodingStyle tmp;
339     int compno, ret;
340
341     if (bytestream2_get_bytes_left(&s->g) < 5)
342         return AVERROR_INVALIDDATA;
343
344     tmp.csty = bytestream2_get_byteu(&s->g);
345
346     // get progression order
347     tmp.prog_order = bytestream2_get_byteu(&s->g);
348
349     tmp.nlayers    = bytestream2_get_be16u(&s->g);
350     tmp.mct        = bytestream2_get_byteu(&s->g); // multiple component transformation
351
352     if (tmp.mct && s->ncomponents < 3) {
353         av_log(s->avctx, AV_LOG_ERROR,
354                "MCT %"PRIu8" with too few components (%d)\n",
355                tmp.mct, s->ncomponents);
356         return AVERROR_INVALIDDATA;
357     }
358
359     if ((ret = get_cox(s, &tmp)) < 0)
360         return ret;
361
362     for (compno = 0; compno < s->ncomponents; compno++)
363         if (!(properties[compno] & HAD_COC))
364             memcpy(c + compno, &tmp, sizeof(tmp));
365     return 0;
366 }
367
368 /* Get coding parameters for a component in the whole image or a
369  * particular tile. */
370 static int get_coc(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
371                    uint8_t *properties)
372 {
373     int compno, ret;
374
375     if (bytestream2_get_bytes_left(&s->g) < 2)
376         return AVERROR_INVALIDDATA;
377
378     compno = bytestream2_get_byteu(&s->g);
379
380     if (compno >= s->ncomponents) {
381         av_log(s->avctx, AV_LOG_ERROR,
382                "Invalid compno %d. There are %d components in the image.\n",
383                compno, s->ncomponents);
384         return AVERROR_INVALIDDATA;
385     }
386
387     c      += compno;
388     c->csty = bytestream2_get_byteu(&s->g);
389
390     if ((ret = get_cox(s, c)) < 0)
391         return ret;
392
393     properties[compno] |= HAD_COC;
394     return 0;
395 }
396
397 /* Get common part for QCD and QCC segments. */
398 static int get_qcx(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q)
399 {
400     int i, x;
401
402     if (bytestream2_get_bytes_left(&s->g) < 1)
403         return AVERROR_INVALIDDATA;
404
405     x = bytestream2_get_byteu(&s->g); // Sqcd
406
407     q->nguardbits = x >> 5;
408     q->quantsty   = x & 0x1f;
409
410     if (q->quantsty == JPEG2000_QSTY_NONE) {
411         n -= 3;
412         if (bytestream2_get_bytes_left(&s->g) < n ||
413             n > JPEG2000_MAX_DECLEVELS)
414             return AVERROR_INVALIDDATA;
415         for (i = 0; i < n; i++)
416             q->expn[i] = bytestream2_get_byteu(&s->g) >> 3;
417     } else if (q->quantsty == JPEG2000_QSTY_SI) {
418         if (bytestream2_get_bytes_left(&s->g) < 2)
419             return AVERROR_INVALIDDATA;
420         x          = bytestream2_get_be16u(&s->g);
421         q->expn[0] = x >> 11;
422         q->mant[0] = x & 0x7ff;
423         for (i = 1; i < JPEG2000_MAX_DECLEVELS * 3; i++) {
424             int curexpn = FFMAX(0, q->expn[0] - (i - 1) / 3);
425             q->expn[i] = curexpn;
426             q->mant[i] = q->mant[0];
427         }
428     } else {
429         n = (n - 3) >> 1;
430         if (bytestream2_get_bytes_left(&s->g) < 2 * n ||
431             n > JPEG2000_MAX_DECLEVELS)
432             return AVERROR_INVALIDDATA;
433         for (i = 0; i < n; i++) {
434             x          = bytestream2_get_be16u(&s->g);
435             q->expn[i] = x >> 11;
436             q->mant[i] = x & 0x7ff;
437         }
438     }
439     return 0;
440 }
441
442 /* Get quantization parameters for a particular tile or a whole image. */
443 static int get_qcd(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
444                    uint8_t *properties)
445 {
446     Jpeg2000QuantStyle tmp;
447     int compno, ret;
448
449     if ((ret = get_qcx(s, n, &tmp)) < 0)
450         return ret;
451     for (compno = 0; compno < s->ncomponents; compno++)
452         if (!(properties[compno] & HAD_QCC))
453             memcpy(q + compno, &tmp, sizeof(tmp));
454     return 0;
455 }
456
457 /* Get quantization parameters for a component in the whole image
458  * on in a particular tile. */
459 static int get_qcc(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
460                    uint8_t *properties)
461 {
462     int compno;
463
464     if (bytestream2_get_bytes_left(&s->g) < 1)
465         return AVERROR_INVALIDDATA;
466
467     compno = bytestream2_get_byteu(&s->g);
468
469     if (compno >= s->ncomponents) {
470         av_log(s->avctx, AV_LOG_ERROR,
471                "Invalid compno %d. There are %d components in the image.\n",
472                compno, s->ncomponents);
473         return AVERROR_INVALIDDATA;
474     }
475
476     properties[compno] |= HAD_QCC;
477     return get_qcx(s, n - 1, q + compno);
478 }
479
480 /* Get start of tile segment. */
481 static int get_sot(Jpeg2000DecoderContext *s, int n)
482 {
483     Jpeg2000TilePart *tp;
484     uint16_t Isot;
485     uint32_t Psot;
486     uint8_t TPsot;
487
488     if (bytestream2_get_bytes_left(&s->g) < 8)
489         return AVERROR_INVALIDDATA;
490
491     Isot = bytestream2_get_be16u(&s->g);        // Isot
492     if (Isot >= s->numXtiles * s->numYtiles)
493         return AVERROR_INVALIDDATA;
494
495     if (Isot) {
496         avpriv_request_sample(s->avctx, "Support for more than one tile");
497         return AVERROR_PATCHWELCOME;
498     }
499     Psot  = bytestream2_get_be32u(&s->g);       // Psot
500     TPsot = bytestream2_get_byteu(&s->g);       // TPsot
501
502     /* Read TNSot but not used */
503     bytestream2_get_byteu(&s->g);               // TNsot
504
505     if (Psot > bytestream2_get_bytes_left(&s->g) + n + 2) {
506         av_log(s->avctx, AV_LOG_ERROR, "Psot %"PRIu32" too big\n", Psot);
507         return AVERROR_INVALIDDATA;
508     }
509
510     if (TPsot >= FF_ARRAY_ELEMS(s->tile[Isot].tile_part)) {
511         avpriv_request_sample(s->avctx, "Support for %"PRIu8" components", TPsot);
512         return AVERROR_PATCHWELCOME;
513     }
514
515     s->tile[Isot].tp_idx = TPsot;
516     tp             = s->tile[Isot].tile_part + TPsot;
517     tp->tile_index = Isot;
518     tp->tp_end     = s->g.buffer + Psot - n - 2;
519
520     if (!TPsot) {
521         Jpeg2000Tile *tile = s->tile + s->curtileno;
522
523         /* copy defaults */
524         memcpy(tile->codsty, s->codsty, s->ncomponents * sizeof(Jpeg2000CodingStyle));
525         memcpy(tile->qntsty, s->qntsty, s->ncomponents * sizeof(Jpeg2000QuantStyle));
526     }
527
528     return 0;
529 }
530
531 /* Tile-part lengths: see ISO 15444-1:2002, section A.7.1
532  * Used to know the number of tile parts and lengths.
533  * There may be multiple TLMs in the header.
534  * TODO: The function is not used for tile-parts management, nor anywhere else.
535  * It can be useful to allocate memory for tile parts, before managing the SOT
536  * markers. Parsing the TLM header is needed to increment the input header
537  * buffer.
538  * This marker is mandatory for DCI. */
539 static uint8_t get_tlm(Jpeg2000DecoderContext *s, int n)
540 {
541     uint8_t Stlm, ST, SP, tile_tlm, i;
542     bytestream2_get_byte(&s->g);               /* Ztlm: skipped */
543     Stlm = bytestream2_get_byte(&s->g);
544
545     // too complex ? ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
546     ST = (Stlm >> 4) & 0x03;
547     // TODO: Manage case of ST = 0b11 --> raise error
548     SP       = (Stlm >> 6) & 0x01;
549     tile_tlm = (n - 4) / ((SP + 1) * 2 + ST);
550     for (i = 0; i < tile_tlm; i++) {
551         switch (ST) {
552         case 0:
553             break;
554         case 1:
555             bytestream2_get_byte(&s->g);
556             break;
557         case 2:
558             bytestream2_get_be16(&s->g);
559             break;
560         case 3:
561             bytestream2_get_be32(&s->g);
562             break;
563         }
564         if (SP == 0) {
565             bytestream2_get_be16(&s->g);
566         } else {
567             bytestream2_get_be32(&s->g);
568         }
569     }
570     return 0;
571 }
572
573 static int init_tile(Jpeg2000DecoderContext *s, int tileno)
574 {
575     int compno;
576     int tilex = tileno % s->numXtiles;
577     int tiley = tileno / s->numXtiles;
578     Jpeg2000Tile *tile = s->tile + tileno;
579
580     if (!tile->comp)
581         return AVERROR(ENOMEM);
582
583     for (compno = 0; compno < s->ncomponents; compno++) {
584         Jpeg2000Component *comp = tile->comp + compno;
585         Jpeg2000CodingStyle *codsty = tile->codsty + compno;
586         Jpeg2000QuantStyle  *qntsty = tile->qntsty + compno;
587         int ret; // global bandno
588
589         comp->coord_o[0][0] = FFMAX(tilex       * s->tile_width  + s->tile_offset_x, s->image_offset_x);
590         comp->coord_o[0][1] = FFMIN((tilex + 1) * s->tile_width  + s->tile_offset_x, s->width);
591         comp->coord_o[1][0] = FFMAX(tiley       * s->tile_height + s->tile_offset_y, s->image_offset_y);
592         comp->coord_o[1][1] = FFMIN((tiley + 1) * s->tile_height + s->tile_offset_y, s->height);
593
594         comp->coord[0][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], s->reduction_factor);
595         comp->coord[0][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][1], s->reduction_factor);
596         comp->coord[1][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], s->reduction_factor);
597         comp->coord[1][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][1], s->reduction_factor);
598
599         if (ret = ff_jpeg2000_init_component(comp, codsty, qntsty,
600                                              s->cbps[compno], s->cdx[compno],
601                                              s->cdy[compno], s->avctx))
602             return ret;
603     }
604     return 0;
605 }
606
607 /* Read the number of coding passes. */
608 static int getnpasses(Jpeg2000DecoderContext *s)
609 {
610     int num;
611     if (!get_bits(s, 1))
612         return 1;
613     if (!get_bits(s, 1))
614         return 2;
615     if ((num = get_bits(s, 2)) != 3)
616         return num < 0 ? num : 3 + num;
617     if ((num = get_bits(s, 5)) != 31)
618         return num < 0 ? num : 6 + num;
619     num = get_bits(s, 7);
620     return num < 0 ? num : 37 + num;
621 }
622
623 static int getlblockinc(Jpeg2000DecoderContext *s)
624 {
625     int res = 0, ret;
626     while (ret = get_bits(s, 1)) {
627         if (ret < 0)
628             return ret;
629         res++;
630     }
631     return res;
632 }
633
634 static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s,
635                                   Jpeg2000CodingStyle *codsty,
636                                   Jpeg2000ResLevel *rlevel, int precno,
637                                   int layno, uint8_t *expn, int numgbits)
638 {
639     int bandno, cblkno, ret, nb_code_blocks;
640
641     if (!(ret = get_bits(s, 1))) {
642         jpeg2000_flush(s);
643         return 0;
644     } else if (ret < 0)
645         return ret;
646
647     for (bandno = 0; bandno < rlevel->nbands; bandno++) {
648         Jpeg2000Band *band = rlevel->band + bandno;
649         Jpeg2000Prec *prec = band->prec + precno;
650
651         if (band->coord[0][0] == band->coord[0][1] ||
652             band->coord[1][0] == band->coord[1][1])
653             continue;
654         nb_code_blocks =  prec->nb_codeblocks_height *
655                           prec->nb_codeblocks_width;
656         for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
657             Jpeg2000Cblk *cblk = prec->cblk + cblkno;
658             int incl, newpasses, llen;
659
660             if (cblk->npasses)
661                 incl = get_bits(s, 1);
662             else
663                 incl = tag_tree_decode(s, prec->cblkincl + cblkno, layno + 1) == layno;
664             if (!incl)
665                 continue;
666             else if (incl < 0)
667                 return incl;
668
669             if (!cblk->npasses) {
670                 int v = expn[bandno] + numgbits - 1 -
671                         tag_tree_decode(s, prec->zerobits + cblkno, 100);
672                 if (v < 0) {
673                     av_log(s->avctx, AV_LOG_ERROR,
674                            "nonzerobits %d invalid\n", v);
675                     return AVERROR_INVALIDDATA;
676                 }
677                 cblk->nonzerobits = v;
678             }
679             if ((newpasses = getnpasses(s)) < 0)
680                 return newpasses;
681             if ((llen = getlblockinc(s)) < 0)
682                 return llen;
683             cblk->lblock += llen;
684             if ((ret = get_bits(s, av_log2(newpasses) + cblk->lblock)) < 0)
685                 return ret;
686             if (ret > sizeof(cblk->data)) {
687                 avpriv_request_sample(s->avctx,
688                                       "Block with lengthinc greater than %zu",
689                                       sizeof(cblk->data));
690                 return AVERROR_PATCHWELCOME;
691             }
692             cblk->lengthinc = ret;
693             cblk->npasses  += newpasses;
694         }
695     }
696     jpeg2000_flush(s);
697
698     if (codsty->csty & JPEG2000_CSTY_EPH) {
699         if (bytestream2_peek_be16(&s->g) == JPEG2000_EPH)
700             bytestream2_skip(&s->g, 2);
701         else
702             av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found.\n");
703     }
704
705     for (bandno = 0; bandno < rlevel->nbands; bandno++) {
706         Jpeg2000Band *band = rlevel->band + bandno;
707         Jpeg2000Prec *prec = band->prec + precno;
708
709         nb_code_blocks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
710         for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
711             Jpeg2000Cblk *cblk = prec->cblk + cblkno;
712             if (bytestream2_get_bytes_left(&s->g) < cblk->lengthinc)
713                 return AVERROR_INVALIDDATA;
714             /* Code-block data can be empty. In that case initialize data
715              * with 0xFFFF. */
716             if (cblk->lengthinc > 0) {
717                 bytestream2_get_bufferu(&s->g, cblk->data, cblk->lengthinc);
718             } else {
719                 cblk->data[0] = 0xFF;
720                 cblk->data[1] = 0xFF;
721             }
722             cblk->length   += cblk->lengthinc;
723             cblk->lengthinc = 0;
724
725             if (cblk->length > sizeof(cblk->data)) {
726                 av_log(s->avctx, AV_LOG_ERROR,
727                        "Block length %"PRIu16" > data size %zd\n",
728                        cblk->length, sizeof(cblk->data));
729                 return AVERROR_INVALIDDATA;
730             }
731         }
732     }
733     return 0;
734 }
735
736 static int decode_pgod_lrcp(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
737 {
738     int layno, reslevelno, compno, precno, ok_reslevel;
739     int ret;
740
741     for (layno = 0; layno < tile->codsty[0].nlayers; layno++) {
742         ok_reslevel = 1;
743         for (reslevelno = 0; ok_reslevel; reslevelno++) {
744             ok_reslevel = 0;
745             for (compno = 0; compno < s->ncomponents; compno++) {
746                 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
747                 Jpeg2000QuantStyle *qntsty  = tile->qntsty + compno;
748                 if (reslevelno < codsty->nreslevels) {
749                     Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
750                                                reslevelno;
751                     ok_reslevel = 1;
752                     for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
753                         if ((ret = jpeg2000_decode_packet(s,
754                                                           codsty, rlevel,
755                                                           precno, layno,
756                                                           qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
757                                                           qntsty->nguardbits)) < 0)
758                             return ret;
759                 }
760             }
761         }
762     }
763
764     return 0;
765 }
766
767 static int decode_pgod_cprl(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
768 {
769     int layno, reslevelno, compno, precno;
770     int ret, x, y;
771
772     for (compno = 0; compno < s->ncomponents; compno++) {
773         Jpeg2000CodingStyle *codsty = tile->codsty + compno;
774         Jpeg2000QuantStyle *qntsty  = tile->qntsty + compno;
775
776         /* Set bit stream buffer address according to tile-part.
777          * For DCinema one tile-part per component, so can be
778          * indexed by component. */
779         s->g = tile->tile_part[compno].tpg;
780
781         /* Position loop (y axis)
782          * TODO: Automate computing of step 256.
783          * Fixed here, but to be computed before entering here. */
784         for (y = 0; y < s->height; y += 256) {
785             /* Position loop (y axis)
786              * TODO: automate computing of step 256.
787              * Fixed here, but to be computed before entering here. */
788             for (x = 0; x < s->width; x += 256) {
789                 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
790                     uint16_t prcx, prcy;
791                     uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; //  ==> N_L - r
792                     Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel + reslevelno;
793
794                     if (!((y % (1 << (rlevel->log2_prec_height + reducedresno)) == 0) ||
795                           (y == 0))) // TODO: 2nd condition simplified as try0 always =0 for dcinema
796                         continue;
797
798                     if (!((x % (1 << (rlevel->log2_prec_width + reducedresno)) == 0) ||
799                           (x == 0))) // TODO: 2nd condition simplified as try0 always =0 for dcinema
800                         continue;
801
802                     // check if a precinct exists
803                     prcx   = ff_jpeg2000_ceildivpow2(x, reducedresno) >> rlevel->log2_prec_width;
804                     prcy   = ff_jpeg2000_ceildivpow2(y, reducedresno) >> rlevel->log2_prec_height;
805                     precno = prcx + rlevel->num_precincts_x * prcy;
806                     for (layno = 0; layno < tile->codsty[0].nlayers; layno++) {
807                         if ((ret = jpeg2000_decode_packet(s, codsty, rlevel,
808                                                           precno, layno,
809                                                           qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
810                                                           qntsty->nguardbits)) < 0)
811                             return ret;
812                     }
813                 }
814             }
815         }
816     }
817
818     return 0;
819 }
820
821 static int jpeg2000_decode_packets(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
822 {
823     int ret = 0;
824
825     s->bit_index = 8;
826     switch (tile->codsty[0].prog_order) {
827     case JPEG2000_PGOD_LRCP:
828         ret = decode_pgod_lrcp(s, tile);
829         break;
830
831     case JPEG2000_PGOD_CPRL:
832         ret = decode_pgod_cprl(s, tile);
833         break;
834
835     case JPEG2000_PGOD_RLCP:
836         avpriv_request_sample(s->avctx, "Progression order RLCP");
837         ret = AVERROR_PATCHWELCOME;
838         break;
839
840     case JPEG2000_PGOD_RPCL:
841         avpriv_request_sample(s->avctx, "Progression order RPCL");
842         ret = AVERROR_PATCHWELCOME;
843         break;
844
845     case JPEG2000_PGOD_PCRL:
846         avpriv_request_sample(s->avctx, "Progression order PCRL");
847         ret = AVERROR_PATCHWELCOME;
848         break;
849
850     default:
851         break;
852     }
853
854     /* EOC marker reached */
855     bytestream2_skip(&s->g, 2);
856
857     return ret;
858 }
859
860 /* TIER-1 routines */
861 static void decode_sigpass(Jpeg2000T1Context *t1, int width, int height,
862                            int bpno, int bandno, int bpass_csty_symbol,
863                            int vert_causal_ctx_csty_symbol)
864 {
865     int mask = 3 << (bpno - 1), y0, x, y;
866
867     for (y0 = 0; y0 < height; y0 += 4)
868         for (x = 0; x < width; x++)
869             for (y = y0; y < height && y < y0 + 4; y++) {
870                 if ((t1->flags[y+1][x+1] & JPEG2000_T1_SIG_NB)
871                 && !(t1->flags[y+1][x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
872                     int flags_mask = -1;
873                     if (vert_causal_ctx_csty_symbol && y == y0 + 3)
874                         flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE);
875                     if (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[y+1][x+1] & flags_mask, bandno))) {
876                         int xorbit, ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
877                         if (bpass_csty_symbol)
878                              t1->data[y][x] = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ? -mask : mask;
879                         else
880                              t1->data[y][x] = (ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ctxno) ^ xorbit) ?
881                                                -mask : mask;
882
883                         ff_jpeg2000_set_significance(t1, x, y,
884                                                      t1->data[y][x] < 0);
885                     }
886                     t1->flags[y + 1][x + 1] |= JPEG2000_T1_VIS;
887                 }
888             }
889 }
890
891 static void decode_refpass(Jpeg2000T1Context *t1, int width, int height,
892                            int bpno)
893 {
894     int phalf, nhalf;
895     int y0, x, y;
896
897     phalf = 1 << (bpno - 1);
898     nhalf = -phalf;
899
900     for (y0 = 0; y0 < height; y0 += 4)
901         for (x = 0; x < width; x++)
902             for (y = y0; y < height && y < y0 + 4; y++)
903                 if ((t1->flags[y + 1][x + 1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG) {
904                     int ctxno = ff_jpeg2000_getrefctxno(t1->flags[y + 1][x + 1]);
905                     int r     = ff_mqc_decode(&t1->mqc,
906                                               t1->mqc.cx_states + ctxno)
907                                 ? phalf : nhalf;
908                     t1->data[y][x]          += t1->data[y][x] < 0 ? -r : r;
909                     t1->flags[y + 1][x + 1] |= JPEG2000_T1_REF;
910                 }
911 }
912
913 static void decode_clnpass(Jpeg2000DecoderContext *s, Jpeg2000T1Context *t1,
914                            int width, int height, int bpno, int bandno,
915                            int seg_symbols, int vert_causal_ctx_csty_symbol)
916 {
917     int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
918
919     for (y0 = 0; y0 < height; y0 += 4) {
920         for (x = 0; x < width; x++) {
921             if (y0 + 3 < height &&
922                 !((t1->flags[y0 + 1][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
923                   (t1->flags[y0 + 2][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
924                   (t1->flags[y0 + 3][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
925                   (t1->flags[y0 + 4][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)))) {
926                 if (!ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL))
927                     continue;
928                 runlen = ff_mqc_decode(&t1->mqc,
929                                        t1->mqc.cx_states + MQC_CX_UNI);
930                 runlen = (runlen << 1) | ff_mqc_decode(&t1->mqc,
931                                                        t1->mqc.cx_states +
932                                                        MQC_CX_UNI);
933                 dec = 1;
934             } else {
935                 runlen = 0;
936                 dec    = 0;
937             }
938
939             for (y = y0 + runlen; y < y0 + 4 && y < height; y++) {
940                 if (!dec) {
941                     if (!(t1->flags[y+1][x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
942                         int flags_mask = -1;
943                         if (vert_causal_ctx_csty_symbol && y == y0 + 3)
944                             flags_mask &= ~(JPEG2000_T1_SIG_S | JPEG2000_T1_SIG_SW | JPEG2000_T1_SIG_SE);
945                         dec = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + ff_jpeg2000_getsigctxno(t1->flags[y+1][x+1] & flags_mask,
946                                                                                              bandno));
947                     }
948                 }
949                 if (dec) {
950                     int xorbit;
951                     int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y + 1][x + 1],
952                                                         &xorbit);
953                     t1->data[y][x] = (ff_mqc_decode(&t1->mqc,
954                                                     t1->mqc.cx_states + ctxno) ^
955                                       xorbit)
956                                      ? -mask : mask;
957                     ff_jpeg2000_set_significance(t1, x, y, t1->data[y][x] < 0);
958                 }
959                 dec = 0;
960                 t1->flags[y + 1][x + 1] &= ~JPEG2000_T1_VIS;
961             }
962         }
963     }
964     if (seg_symbols) {
965         int val;
966         val = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
967         val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
968         val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
969         val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
970         if (val != 0xa)
971             av_log(s->avctx, AV_LOG_ERROR,
972                    "Segmentation symbol value incorrect\n");
973     }
974 }
975
976 static int decode_cblk(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty,
977                        Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk,
978                        int width, int height, int bandpos)
979 {
980     int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1, y;
981     int clnpass_cnt = 0;
982     int bpass_csty_symbol           = codsty->cblk_style & JPEG2000_CBLK_BYPASS;
983     int vert_causal_ctx_csty_symbol = codsty->cblk_style & JPEG2000_CBLK_VSC;
984
985     for (y = 0; y < height; y++)
986         memset(t1->data[y], 0, width * sizeof(**t1->data));
987
988     /* If code-block contains no compressed data: nothing to do. */
989     if (!cblk->length)
990         return 0;
991     for (y = 0; y < height + 2; y++)
992         memset(t1->flags[y], 0, (width + 2) * sizeof(**t1->flags));
993
994     ff_mqc_initdec(&t1->mqc, cblk->data);
995     cblk->data[cblk->length]     = 0xff;
996     cblk->data[cblk->length + 1] = 0xff;
997
998     while (passno--) {
999         switch (pass_t) {
1000         case 0:
1001             decode_sigpass(t1, width, height, bpno + 1, bandpos,
1002                            bpass_csty_symbol && (clnpass_cnt >= 4),
1003                            vert_causal_ctx_csty_symbol);
1004             break;
1005         case 1:
1006             decode_refpass(t1, width, height, bpno + 1);
1007             if (bpass_csty_symbol && clnpass_cnt >= 4)
1008                 ff_mqc_initdec(&t1->mqc, cblk->data);
1009             break;
1010         case 2:
1011             decode_clnpass(s, t1, width, height, bpno + 1, bandpos,
1012                            codsty->cblk_style & JPEG2000_CBLK_SEGSYM,
1013                            vert_causal_ctx_csty_symbol);
1014             clnpass_cnt = clnpass_cnt + 1;
1015             if (bpass_csty_symbol && clnpass_cnt >= 4)
1016                 ff_mqc_initdec(&t1->mqc, cblk->data);
1017             break;
1018         }
1019
1020         pass_t++;
1021         if (pass_t == 3) {
1022             bpno--;
1023             pass_t = 0;
1024         }
1025     }
1026     return 0;
1027 }
1028
1029 /* TODO: Verify dequantization for lossless case
1030  * comp->data can be float or int
1031  * band->stepsize can be float or int
1032  * depending on the type of DWT transformation.
1033  * see ISO/IEC 15444-1:2002 A.6.1 */
1034
1035 /* Float dequantization of a codeblock.*/
1036 static void dequantization_float(int x, int y, Jpeg2000Cblk *cblk,
1037                                  Jpeg2000Component *comp,
1038                                  Jpeg2000T1Context *t1, Jpeg2000Band *band)
1039 {
1040     int i, j;
1041     int w = cblk->coord[0][1] - cblk->coord[0][0];
1042     for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1043         float *datap = &comp->f_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1044         int *src = t1->data[j];
1045         for (i = 0; i < w; ++i)
1046             datap[i] = src[i] * band->f_stepsize;
1047     }
1048 }
1049
1050 /* Integer dequantization of a codeblock.*/
1051 static void dequantization_int(int x, int y, Jpeg2000Cblk *cblk,
1052                                Jpeg2000Component *comp,
1053                                Jpeg2000T1Context *t1, Jpeg2000Band *band)
1054 {
1055     int i, j;
1056     int w = cblk->coord[0][1] - cblk->coord[0][0];
1057     for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
1058         int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
1059         int *src = t1->data[j];
1060         for (i = 0; i < w; ++i)
1061             datap[i] = (src[i] * band->i_stepsize + (1 << 15)) >> 16;
1062     }
1063 }
1064
1065 static inline void mct_decode(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
1066 {
1067     int i, csize = 1;
1068     void *src[3];
1069
1070     for (i = 0; i < 3; i++)
1071         if (tile->codsty[0].transform == FF_DWT97)
1072             src[i] = tile->comp[i].f_data;
1073         else
1074             src[i] = tile->comp[i].i_data;
1075
1076     for (i = 0; i < 2; i++)
1077         csize *= tile->comp[0].coord[i][1] - tile->comp[0].coord[i][0];
1078
1079     s->dsp.mct_decode[tile->codsty[0].transform](src[0], src[1], src[2], csize);
1080 }
1081
1082 static inline void tile_codeblocks(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
1083 {
1084     Jpeg2000T1Context t1;
1085
1086     int compno, reslevelno, bandno;
1087
1088     /* Loop on tile components */
1089
1090     for (compno = 0; compno < s->ncomponents; compno++) {
1091         Jpeg2000Component *comp     = tile->comp + compno;
1092         Jpeg2000CodingStyle *codsty = tile->codsty + compno;
1093         /* Loop on resolution levels */
1094         for (reslevelno = 0; reslevelno < codsty->nreslevels2decode; reslevelno++) {
1095             Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1096             /* Loop on bands */
1097             for (bandno = 0; bandno < rlevel->nbands; bandno++) {
1098                 uint16_t nb_precincts, precno;
1099                 Jpeg2000Band *band = rlevel->band + bandno;
1100                 int cblkno = 0, bandpos;
1101                 bandpos = bandno + (reslevelno > 0);
1102
1103                 if (band->coord[0][0] == band->coord[0][1] ||
1104                     band->coord[1][0] == band->coord[1][1])
1105                     continue;
1106
1107                 nb_precincts = rlevel->num_precincts_x * rlevel->num_precincts_y;
1108                 /* Loop on precincts */
1109                 for (precno = 0; precno < nb_precincts; precno++) {
1110                     Jpeg2000Prec *prec = band->prec + precno;
1111
1112                     /* Loop on codeblocks */
1113                     for (cblkno = 0;
1114                          cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height;
1115                          cblkno++) {
1116                         int x, y;
1117                         Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1118                         decode_cblk(s, codsty, &t1, cblk,
1119                                     cblk->coord[0][1] - cblk->coord[0][0],
1120                                     cblk->coord[1][1] - cblk->coord[1][0],
1121                                     bandpos);
1122
1123                         x = cblk->coord[0][0];
1124                         y = cblk->coord[1][0];
1125
1126                         if (codsty->transform == FF_DWT97)
1127                             dequantization_float(x, y, cblk, comp, &t1, band);
1128                         else
1129                             dequantization_int(x, y, cblk, comp, &t1, band);
1130                    } /* end cblk */
1131                 } /*end prec */
1132             } /* end band */
1133         } /* end reslevel */
1134
1135         /* inverse DWT */
1136         ff_dwt_decode(&comp->dwt, codsty->transform == FF_DWT97 ? (void*)comp->f_data : (void*)comp->i_data);
1137     } /*end comp */
1138 }
1139
1140 #define WRITE_FRAME(D, PIXEL)                                                                     \
1141     static inline void write_frame_ ## D(Jpeg2000DecoderContext * s, Jpeg2000Tile * tile,         \
1142                                          AVFrame * picture)                                       \
1143     {                                                                                             \
1144         int linesize = picture->linesize[0] / sizeof(PIXEL);                                      \
1145         int compno;                                                                               \
1146         int x, y;                                                                                 \
1147                                                                                                   \
1148         for (compno = 0; compno < s->ncomponents; compno++) {                                     \
1149             Jpeg2000Component *comp     = tile->comp + compno;                                    \
1150             Jpeg2000CodingStyle *codsty = tile->codsty + compno;                                  \
1151             PIXEL *line;                                                                          \
1152             float *datap     = comp->f_data;                                                      \
1153             int32_t *i_datap = comp->i_data;                                                      \
1154             int cbps         = s->cbps[compno];                                                   \
1155             int w            = tile->comp[compno].coord[0][1] - s->image_offset_x;                \
1156                                                                                                   \
1157             y    = tile->comp[compno].coord[1][0] - s->image_offset_y;                            \
1158             line = (PIXEL *)picture->data[0] + y * linesize;                                      \
1159             for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) { \
1160                 PIXEL *dst;                                                                       \
1161                                                                                                   \
1162                 x   = tile->comp[compno].coord[0][0] - s->image_offset_x;                         \
1163                 dst = line + x * s->ncomponents + compno;                                         \
1164                                                                                                   \
1165                 if (codsty->transform == FF_DWT97) {                                              \
1166                     for (; x < w; x += s->cdx[compno]) {                                          \
1167                         int val = lrintf(*datap) + (1 << (cbps - 1));                             \
1168                         /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */                  \
1169                         val  = av_clip(val, 0, (1 << cbps) - 1);                                  \
1170                         *dst = val << (8 * sizeof(PIXEL) - cbps);                                 \
1171                         datap++;                                                                  \
1172                         dst += s->ncomponents;                                                    \
1173                     }                                                                             \
1174                 } else {                                                                          \
1175                     for (; x < w; x += s->cdx[compno]) {                                          \
1176                         int val = *i_datap + (1 << (cbps - 1));                                   \
1177                         /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */                  \
1178                         val  = av_clip(val, 0, (1 << cbps) - 1);                                  \
1179                         *dst = val << (8 * sizeof(PIXEL) - cbps);                                 \
1180                         i_datap++;                                                                \
1181                         dst += s->ncomponents;                                                    \
1182                     }                                                                             \
1183                 }                                                                                 \
1184                 line += linesize;                                                                 \
1185             }                                                                                     \
1186         }                                                                                         \
1187                                                                                                   \
1188     }
1189
1190 WRITE_FRAME(8, uint8_t)
1191 WRITE_FRAME(16, uint16_t)
1192
1193 #undef WRITE_FRAME
1194
1195 static int jpeg2000_decode_tile(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
1196                                 AVFrame *picture)
1197 {
1198     tile_codeblocks(s, tile);
1199
1200     /* inverse MCT transformation */
1201     if (tile->codsty[0].mct)
1202         mct_decode(s, tile);
1203
1204     if (s->precision <= 8) {
1205         write_frame_8(s, tile, picture);
1206     } else {
1207         write_frame_16(s, tile, picture);
1208     }
1209
1210     return 0;
1211 }
1212
1213 static void jpeg2000_dec_cleanup(Jpeg2000DecoderContext *s)
1214 {
1215     int tileno, compno;
1216     for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
1217         for (compno = 0; compno < s->ncomponents; compno++) {
1218             Jpeg2000Component *comp     = s->tile[tileno].comp   + compno;
1219             Jpeg2000CodingStyle *codsty = s->tile[tileno].codsty + compno;
1220
1221             ff_jpeg2000_cleanup(comp, codsty);
1222         }
1223         av_freep(&s->tile[tileno].comp);
1224     }
1225     av_freep(&s->tile);
1226     s->numXtiles = s->numYtiles = 0;
1227 }
1228
1229 static int jpeg2000_read_main_headers(Jpeg2000DecoderContext *s)
1230 {
1231     Jpeg2000CodingStyle *codsty = s->codsty;
1232     Jpeg2000QuantStyle *qntsty  = s->qntsty;
1233     uint8_t *properties         = s->properties;
1234
1235     for (;;) {
1236         int len, ret = 0;
1237         uint16_t marker;
1238         int oldpos;
1239
1240         if (bytestream2_get_bytes_left(&s->g) < 2) {
1241             av_log(s->avctx, AV_LOG_ERROR, "Missing EOC\n");
1242             break;
1243         }
1244
1245         marker = bytestream2_get_be16u(&s->g);
1246         oldpos = bytestream2_tell(&s->g);
1247
1248         if (marker == JPEG2000_SOD) {
1249             Jpeg2000Tile *tile;
1250             Jpeg2000TilePart *tp;
1251
1252             if (s->curtileno < 0) {
1253                 av_log(s->avctx, AV_LOG_ERROR, "Missing SOT\n");
1254                 return AVERROR_INVALIDDATA;
1255             }
1256             if (!s->tile) {
1257                 av_log(s->avctx, AV_LOG_ERROR, "Missing SIZ\n");
1258                 return AVERROR_INVALIDDATA;
1259             }
1260
1261             tile = s->tile + s->curtileno;
1262             tp = tile->tile_part + tile->tp_idx;
1263             bytestream2_init(&tp->tpg, s->g.buffer, tp->tp_end - s->g.buffer);
1264             bytestream2_skip(&s->g, tp->tp_end - s->g.buffer);
1265
1266             continue;
1267         }
1268         if (marker == JPEG2000_EOC)
1269             break;
1270
1271         len = bytestream2_get_be16u(&s->g);
1272         if (len < 2 || bytestream2_get_bytes_left(&s->g) < len - 2)
1273             return AVERROR_INVALIDDATA;
1274
1275         switch (marker) {
1276         case JPEG2000_SIZ:
1277             ret = get_siz(s);
1278             break;
1279         case JPEG2000_COC:
1280             ret = get_coc(s, codsty, properties);
1281             break;
1282         case JPEG2000_COD:
1283             ret = get_cod(s, codsty, properties);
1284             break;
1285         case JPEG2000_QCC:
1286             ret = get_qcc(s, len, qntsty, properties);
1287             break;
1288         case JPEG2000_QCD:
1289             ret = get_qcd(s, len, qntsty, properties);
1290             break;
1291         case JPEG2000_SOT:
1292             if (!(ret = get_sot(s, len))) {
1293                 codsty = s->tile[s->curtileno].codsty;
1294                 qntsty = s->tile[s->curtileno].qntsty;
1295                 properties = s->tile[s->curtileno].properties;
1296             }
1297             break;
1298         case JPEG2000_PLT:
1299             // the PLT marker is ignored
1300         case JPEG2000_PLM:
1301             // the PLM marker is ignored
1302         case JPEG2000_COM:
1303             // the comment is ignored
1304             bytestream2_skip(&s->g, len - 2);
1305             break;
1306         case JPEG2000_TLM:
1307             // Tile-part lengths
1308             ret = get_tlm(s, len);
1309             break;
1310         default:
1311             av_log(s->avctx, AV_LOG_ERROR,
1312                    "unsupported marker 0x%.4"PRIX16" at pos 0x%X\n",
1313                    marker, bytestream2_tell(&s->g) - 4);
1314             bytestream2_skip(&s->g, len - 2);
1315             break;
1316         }
1317         if (bytestream2_tell(&s->g) - oldpos != len || ret) {
1318             av_log(s->avctx, AV_LOG_ERROR,
1319                    "error during processing marker segment %.4"PRIx16"\n",
1320                    marker);
1321             return ret ? ret : -1;
1322         }
1323     }
1324     return 0;
1325 }
1326
1327 /* Read bit stream packets --> T2 operation. */
1328 static int jpeg2000_read_bitstream_packets(Jpeg2000DecoderContext *s)
1329 {
1330     int ret = 0;
1331     int tileno;
1332
1333     for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
1334         Jpeg2000Tile *tile = s->tile + tileno;
1335
1336         if (ret = init_tile(s, tileno))
1337             return ret;
1338
1339         s->g = tile->tile_part[0].tpg;
1340         if (ret = jpeg2000_decode_packets(s, tile))
1341             return ret;
1342     }
1343
1344     return 0;
1345 }
1346
1347 static int jp2_find_codestream(Jpeg2000DecoderContext *s)
1348 {
1349     uint32_t atom_size, atom;
1350     int found_codestream = 0, search_range = 10;
1351
1352     while(!found_codestream && search_range
1353           &&
1354           bytestream2_get_bytes_left(&s->g) >= 8) {
1355         atom_size = bytestream2_get_be32u(&s->g);
1356         atom      = bytestream2_get_be32u(&s->g);
1357         if (atom == JP2_CODESTREAM) {
1358             found_codestream = 1;
1359         } else {
1360             if (bytestream2_get_bytes_left(&s->g) < atom_size - 8)
1361                 return 0;
1362             bytestream2_skipu(&s->g, atom_size - 8);
1363             search_range--;
1364         }
1365     }
1366
1367     if (found_codestream)
1368         return 1;
1369     return 0;
1370 }
1371
1372 static av_cold int jpeg2000_decode_init(AVCodecContext *avctx)
1373 {
1374     Jpeg2000DecoderContext *s = avctx->priv_data;
1375
1376     ff_jpeg2000dsp_init(&s->dsp);
1377
1378     return 0;
1379 }
1380
1381 static int jpeg2000_decode_frame(AVCodecContext *avctx, void *data,
1382                                  int *got_frame, AVPacket *avpkt)
1383 {
1384     Jpeg2000DecoderContext *s = avctx->priv_data;
1385     ThreadFrame frame = { .f = data };
1386     AVFrame *picture = data;
1387     int tileno, ret;
1388
1389     s->avctx     = avctx;
1390     bytestream2_init(&s->g, avpkt->data, avpkt->size);
1391     s->curtileno = 0; // TODO: only one tile in DCI JP2K. to implement for more tiles
1392
1393     if (bytestream2_get_bytes_left(&s->g) < 2) {
1394         ret = AVERROR_INVALIDDATA;
1395         goto end;
1396     }
1397
1398     // check if the image is in jp2 format
1399     if (bytestream2_get_bytes_left(&s->g) >= 12 &&
1400        (bytestream2_get_be32u(&s->g) == 12) &&
1401        (bytestream2_get_be32u(&s->g) == JP2_SIG_TYPE) &&
1402        (bytestream2_get_be32u(&s->g) == JP2_SIG_VALUE)) {
1403         if (!jp2_find_codestream(s)) {
1404             av_log(avctx, AV_LOG_ERROR,
1405                    "Could not find Jpeg2000 codestream atom.\n");
1406             ret = AVERROR_INVALIDDATA;
1407             goto end;
1408         }
1409     } else {
1410         bytestream2_seek(&s->g, 0, SEEK_SET);
1411     }
1412
1413     if (bytestream2_get_be16u(&s->g) != JPEG2000_SOC) {
1414         av_log(avctx, AV_LOG_ERROR, "SOC marker not present\n");
1415         ret = AVERROR_INVALIDDATA;
1416         goto end;
1417     }
1418     if (ret = jpeg2000_read_main_headers(s))
1419         goto end;
1420
1421     /* get picture buffer */
1422     if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0) {
1423         av_log(avctx, AV_LOG_ERROR, "ff_thread_get_buffer() failed.\n");
1424         goto end;
1425     }
1426     picture->pict_type = AV_PICTURE_TYPE_I;
1427     picture->key_frame = 1;
1428
1429     if (ret = jpeg2000_read_bitstream_packets(s))
1430         goto end;
1431     for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++)
1432         if (ret = jpeg2000_decode_tile(s, s->tile + tileno, picture))
1433             goto end;
1434
1435     jpeg2000_dec_cleanup(s);
1436
1437     *got_frame = 1;
1438
1439     return bytestream2_tell(&s->g);
1440
1441 end:
1442     jpeg2000_dec_cleanup(s);
1443     return ret;
1444 }
1445
1446 static av_cold void jpeg2000_init_static_data(AVCodec *codec)
1447 {
1448     ff_jpeg2000_init_tier1_luts();
1449     ff_mqc_init_context_tables();
1450 }
1451
1452 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
1453 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1454
1455 static const AVOption options[] = {
1456     { "lowres",  "Lower the decoding resolution by a power of two",
1457         OFFSET(reduction_factor), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, JPEG2000_MAX_RESLEVELS - 1, VD },
1458     { NULL },
1459 };
1460
1461 static const AVClass class = {
1462     .class_name = "jpeg2000",
1463     .item_name  = av_default_item_name,
1464     .option     = options,
1465     .version    = LIBAVUTIL_VERSION_INT,
1466 };
1467
1468 AVCodec ff_jpeg2000_decoder = {
1469     .name             = "jpeg2000",
1470     .long_name        = NULL_IF_CONFIG_SMALL("JPEG 2000"),
1471     .type             = AVMEDIA_TYPE_VIDEO,
1472     .id               = AV_CODEC_ID_JPEG2000,
1473     .capabilities     = AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_DR1,
1474     .priv_data_size   = sizeof(Jpeg2000DecoderContext),
1475     .init_static_data = jpeg2000_init_static_data,
1476     .init             = jpeg2000_decode_init,
1477     .decode           = jpeg2000_decode_frame,
1478     .priv_class       = &class,
1479     .profiles         = NULL_IF_CONFIG_SMALL(ff_jpeg2000_profiles)
1480 };