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