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 FFmpeg.
7 *
8 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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
41
42 #define JP2_SIG_TYPE 0x6A502020
43 #define JP2_SIG_VALUE 0x0D0A870A
44 #define JP2_CODESTREAM 0x6A703263
45 #define JP2_HEADER 0x6A703268
46
49
55
56 /* RMK: For JPEG2000 DCINEMA 3 tile-parts in a tile
57 * one per component, so tile_part elements have a size of 3 */
66
71
88
91
93
95
98
99 /*options parameters*/
102
103 /* get_bits functions for JPEG2000 packet bitstream
104 * It is a get_bit function with a bit-stuffing routine. If the value of the
105 * byte is 0xFF, the next byte includes an extra zero bit stuffed into the MSB.
106 * cf. ISO-15444-1:2002 / B.10.1 Bit-stuffing routine */
108 {
109 int res = 0;
110
111 while (--n >= 0) {
112 res <<= 1;
114 s->
bit_index = 7 + (bytestream2_get_byte(&s->
g) != 0xFF
u);
115 }
117 res |= (bytestream2_peek_byte(&s->
g) >> s->
bit_index) & 1;
118 }
119 return res;
120 }
121
123 {
124 if (bytestream2_get_byte(&s->
g) == 0xff)
127 }
128
129 /* decode the value stored in node */
131 int threshold)
132 {
134 int sp = -1, curval = 0;
135
136 if (!node)
138
139 while (node && !node->
vis) {
142 }
143
144 if (node)
146 else
147 curval = stack[
sp]->
val;
148
149 while (curval < threshold && sp >= 0) {
150 if (curval < stack[sp]->
val)
151 curval = stack[
sp]->
val;
152 while (curval < threshold) {
156 break;
157 } else if (!ret)
158 curval++;
159 else
161 }
162 stack[
sp]->
val = curval;
163 sp--;
164 }
165 return curval;
166 }
167
169 int bpc, uint32_t log2_chroma_wh, int pal8)
170 {
171 int match = 1;
173
175 return 0;
176 }
177
178 switch (components) {
179 case 4:
181 (log2_chroma_wh >> 14 & 3) == 0 &&
182 (log2_chroma_wh >> 12 & 3) == 0;
183 case 3:
187 case 2:
191
192 case 1:
194 (log2_chroma_wh >> 2 & 3) == 0 &&
195 (log2_chroma_wh & 3) == 0 &&
197 }
198 return match;
199 }
200
201 // pix_fmts with lower bpp have to be listed before
202 // similar pix_fmts with higher bpp.
203 #define RGB_PIXEL_FORMATS AV_PIX_FMT_PAL8,AV_PIX_FMT_RGB24,AV_PIX_FMT_RGBA,AV_PIX_FMT_RGB48,AV_PIX_FMT_RGBA64
204 #define GRAY_PIXEL_FORMATS AV_PIX_FMT_GRAY8,AV_PIX_FMT_GRAY8A,AV_PIX_FMT_GRAY16,AV_PIX_FMT_YA16
205 #define YUV_PIXEL_FORMATS AV_PIX_FMT_YUV410P,AV_PIX_FMT_YUV411P,AV_PIX_FMT_YUVA420P, \
206 AV_PIX_FMT_YUV420P,AV_PIX_FMT_YUV422P,AV_PIX_FMT_YUVA422P, \
207 AV_PIX_FMT_YUV440P,AV_PIX_FMT_YUV444P,AV_PIX_FMT_YUVA444P, \
208 AV_PIX_FMT_YUV420P9,AV_PIX_FMT_YUV422P9,AV_PIX_FMT_YUV444P9, \
209 AV_PIX_FMT_YUVA420P9,AV_PIX_FMT_YUVA422P9,AV_PIX_FMT_YUVA444P9, \
210 AV_PIX_FMT_YUV420P10,AV_PIX_FMT_YUV422P10,AV_PIX_FMT_YUV444P10, \
211 AV_PIX_FMT_YUVA420P10,AV_PIX_FMT_YUVA422P10,AV_PIX_FMT_YUVA444P10, \
212 AV_PIX_FMT_YUV420P12,AV_PIX_FMT_YUV422P12,AV_PIX_FMT_YUV444P12, \
213 AV_PIX_FMT_YUV420P14,AV_PIX_FMT_YUV422P14,AV_PIX_FMT_YUV444P14, \
214 AV_PIX_FMT_YUV420P16,AV_PIX_FMT_YUV422P16,AV_PIX_FMT_YUV444P16, \
215 AV_PIX_FMT_YUVA420P16,AV_PIX_FMT_YUVA422P16,AV_PIX_FMT_YUVA444P16
216 #define XYZ_PIXEL_FORMATS AV_PIX_FMT_XYZ12
217
226
227 /* marker segments */
228 /* get sizes and offsets of image, tiles; number of components */
230 {
231 int i;
232 int ncomponents;
233 uint32_t log2_chroma_wh = 0;
235 int possible_fmts_nb = 0;
236
239
241 s->
width = bytestream2_get_be32u(&s->
g);
// Width
242 s->
height = bytestream2_get_be32u(&s->
g);
// Height
245 s->
tile_width = bytestream2_get_be32u(&s->
g);
// XTSiz
249 ncomponents = bytestream2_get_be16u(&s->
g);
// CSiz
250
254 }
255
256 if (ncomponents <= 0) {
260 }
261
262 if (ncomponents > 4) {
266 }
267
269
274 }
275
278
279 for (i = 0; i < s->
ncomponents; i++) {
// Ssiz_i XRsiz_i, YRsiz_i
280 uint8_t x = bytestream2_get_byteu(&s->
g);
281 s->
cbps[i] = (x & 0x7f) + 1;
283 s->
sgnd[i] = !!(x & 0x80);
284 s->
cdx[i] = bytestream2_get_byteu(&s->
g);
285 s->
cdy[i] = bytestream2_get_byteu(&s->
g);
286 if ( !s->
cdx[i] || s->
cdx[i] == 3 || s->
cdx[i] > 4
287 || !s->
cdy[i] || s->
cdy[i] == 3 || s->
cdy[i] > 4) {
290 }
291 log2_chroma_wh |= s->
cdy[i] >> 1 << i * 4 | s->
cdx[i] >> 1 << i * 4 + 2;
292 }
293
296
300 }
301
306 }
307
310
314 }
315
316 /* compute image size with reduction factor */
321
326 } else {
328 case 16:
331 break;
332 case 17:
335 break;
336 case 18:
339 break;
340 default:
343 break;
344 }
345 }
346 for (i = 0; i < possible_fmts_nb; ++i) {
349 break;
350 }
351 }
352 if (i == possible_fmts_nb) {
354 "Unknown pix_fmt, profile: %d, colour_space: %d, "
355 "components: %d, precision: %d, "
356 "cdx[1]: %d, cdy[1]: %d, cdx[2]: %d, cdy[2]: %d\n",
358 ncomponents > 2 ? s->
cdx[1] : 0,
359 ncomponents > 2 ? s->
cdy[1] : 0,
360 ncomponents > 2 ? s->
cdx[2] : 0,
361 ncomponents > 2 ? s->
cdy[2] : 0);
363 }
365 return 0;
366 }
367
368 /* get common part for COD and COC segments */
370 {
372
375
376 /* nreslevels = number of resolution levels
377 = number of decomposition level +1 */
382 }
383
385 /* we are forced to update reduction_factor as its requested value is
386 not compatible with this bitstream, and as we might have used it
387 already in setup earlier we have to fail this frame until
388 reinitialization is implemented */
392 }
393
394 /* compute number of resolution levels to decode */
396
399
404 }
405
409 }
410
414 }
415 c->
transform = bytestream2_get_byteu(&s->
g);
// DWT transformation type
416 /* set integer 9/7 DWT in case of BITEXACT flag */
419
421 int i;
423 byte = bytestream2_get_byte(&s->
g);
426 }
427 } else {
430 }
431 return 0;
432 }
433
434 /* get coding parameters for a particular tile or whole image*/
437 {
440
443
444 tmp.
csty = bytestream2_get_byteu(&s->
g);
445
446 // get progression order
448
449 tmp.
nlayers = bytestream2_get_be16u(&s->
g);
450 tmp.
mct = bytestream2_get_byteu(&s->
g);
// multiple component transformation
451
454 "MCT %"PRIu8" with too few components (%d)\n",
457 }
458
459 if ((ret =
get_cox(s, &tmp)) < 0)
461
462 for (compno = 0; compno < s->
ncomponents; compno++)
463 if (!(properties[compno] &
HAD_COC))
464 memcpy(c + compno, &tmp, sizeof(tmp));
465 return 0;
466 }
467
468 /* Get coding parameters for a component in the whole image or a
469 * particular tile. */
472 {
474
477
478 compno = bytestream2_get_byteu(&s->
g);
479
482 "Invalid compno %d. There are %d components in the image.\n",
485 }
486
487 c += compno;
488 c->
csty = bytestream2_get_byteu(&s->
g);
489
492
494 return 0;
495 }
496
497 /* Get common part for QCD and QCC segments. */
499 {
500 int i, x;
501
504
505 x = bytestream2_get_byteu(&s->
g);
// Sqcd
506
509
511 n -= 3;
515 for (i = 0; i <
n; i++)
516 q->
expn[i] = bytestream2_get_byteu(&s->
g) >> 3;
520 x = bytestream2_get_be16u(&s->
g);
521 q->
expn[0] = x >> 11;
522 q->
mant[0] = x & 0x7ff;
524 int curexpn =
FFMAX(0, q->
expn[0] - (i - 1) / 3);
525 q->
expn[i] = curexpn;
527 }
528 } else {
529 n = (n - 3) >> 1;
533 for (i = 0; i <
n; i++) {
534 x = bytestream2_get_be16u(&s->
g);
535 q->
expn[i] = x >> 11;
536 q->
mant[i] = x & 0x7ff;
537 }
538 }
539 return 0;
540 }
541
542 /* Get quantization parameters for a particular tile or a whole image. */
545 {
548
549 memset(&tmp, 0, sizeof(tmp));
550
551 if ((ret =
get_qcx(s, n, &tmp)) < 0)
553 for (compno = 0; compno < s->
ncomponents; compno++)
554 if (!(properties[compno] &
HAD_QCC))
555 memcpy(q + compno, &tmp, sizeof(tmp));
556 return 0;
557 }
558
559 /* Get quantization parameters for a component in the whole image
560 * on in a particular tile. */
563 {
564 int compno;
565
568
569 compno = bytestream2_get_byteu(&s->
g);
570
573 "Invalid compno %d. There are %d components in the image.\n",
576 }
577
579 return get_qcx(s, n - 1, q + compno);
580 }
581
582 /* Get start of tile segment. */
584 {
586 uint16_t Isot;
587 uint32_t Psot;
589
592
594 Isot = bytestream2_get_be16u(&s->
g);
// Isot
597
599 Psot = bytestream2_get_be32u(&s->
g);
// Psot
600 TPsot = bytestream2_get_byteu(&s->
g);
// TPsot
601
602 /* Read TNSot but not used */
603 bytestream2_get_byteu(&s->
g);
// TNsot
604
608 }
609
613 }
614
619
620 if (!TPsot) {
622
623 /* copy defaults */
626 }
627
628 return 0;
629 }
630
631 /* Tile-part lengths: see ISO 15444-1:2002, section A.7.1
632 * Used to know the number of tile parts and lengths.
633 * There may be multiple TLMs in the header.
634 * TODO: The function is not used for tile-parts management, nor anywhere else.
635 * It can be useful to allocate memory for tile parts, before managing the SOT
636 * markers. Parsing the TLM header is needed to increment the input header
637 * buffer.
638 * This marker is mandatory for DCI. */
640 {
642 bytestream2_get_byte(&s->
g);
/* Ztlm: skipped */
643 Stlm = bytestream2_get_byte(&s->
g);
644
645 // too complex ? ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
646 ST = (Stlm >> 4) & 0x03;
647 // TODO: Manage case of ST = 0b11 --> raise error
648 SP = (Stlm >> 6) & 0x01;
649 tile_tlm = (n - 4) / ((SP + 1) * 2 + ST);
650 for (i = 0; i < tile_tlm; i++) {
651 switch (ST) {
652 case 0:
653 break;
654 case 1:
655 bytestream2_get_byte(&s->
g);
656 break;
657 case 2:
658 bytestream2_get_be16(&s->
g);
659 break;
660 case 3:
661 bytestream2_get_be32(&s->
g);
662 break;
663 }
664 if (SP == 0) {
665 bytestream2_get_be16(&s->
g);
666 } else {
667 bytestream2_get_be32(&s->
g);
668 }
669 }
670 return 0;
671 }
672
674 {
675 int compno;
679
682
683 for (compno = 0; compno < s->
ncomponents; compno++) {
687 int ret;
// global bandno
688
693
698
700 s->
cbps[compno], s->
cdx[compno],
703 }
704 return 0;
705 }
706
707 /* Read the number of coding passes. */
709 {
710 int num;
712 return 1;
714 return 2;
716 return num < 0 ? num : 3 + num;
718 return num < 0 ? num : 6 + num;
720 return num < 0 ? num : 37 + num;
721 }
722
724 {
729 res++;
730 }
731 return res;
732 }
733
737 int layno,
uint8_t *expn,
int numgbits)
738 {
739 int bandno, cblkno,
ret, nb_code_blocks;
740
743 return 0;
744 } else if (ret < 0)
746
747 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
750
753 continue;
756 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
758 int incl, newpasses, llen;
759
762 else
764 if (!incl)
765 continue;
766 else if (incl < 0)
767 return incl;
768
770 int v = expn[bandno] + numgbits - 1 -
772 if (v < 0) {
774 "nonzerobits %d invalid\n", v);
776 }
778 }
780 return newpasses;
782 return llen;
786 if (ret >
sizeof(cblk->
data)) {
791 }
794 }
795 }
797
801 else
803 }
804
805 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
808
810 for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
814 ) {
816 "Block length %"PRIu16" or lengthinc %d is too large\n",
819 }
820
824 }
825 }
826 return 0;
827 }
828
830 {
832 int layno, reslevelno, compno, precno, ok_reslevel;
834
839
842 ok_reslevel = 1;
843 for (reslevelno = 0; ok_reslevel; reslevelno++) {
844 ok_reslevel = 0;
845 for (compno = 0; compno < s->
ncomponents; compno++) {
848 if (reslevelno < codsty->nreslevels) {
850 reslevelno;
851 ok_reslevel = 1;
854 codsty, rlevel,
855 precno, layno,
856 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
859 }
860 }
861 }
862 }
863 break;
864
866 for (compno = 0; compno < s->
ncomponents; compno++) {
869
870 /* Set bit stream buffer address according to tile-part.
871 * For DCinema one tile-part per component, so can be
872 * indexed by component. */
874
875 /* Position loop (y axis)
876 * TODO: Automate computing of step 256.
877 * Fixed here, but to be computed before entering here. */
878 for (y = 0; y < s->
height; y += 256) {
879 /* Position loop (y axis)
880 * TODO: automate computing of step 256.
881 * Fixed here, but to be computed before entering here. */
882 for (x = 0; x < s->
width; x += 256) {
883 for (reslevelno = 0; reslevelno < codsty->
nreslevels; reslevelno++) {
884 uint16_t prcx, prcy;
887
889 (y == 0))) // TODO: 2nd condition simplified as try0 always =0 for dcinema
890 continue;
891
893 (x == 0))) // TODO: 2nd condition simplified as try0 always =0 for dcinema
894 continue;
895
896 // check if a precinct exists
900
903
906 precno, layno,
907 qntsty->
expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
910 }
911 }
912 }
913 }
914 }
915 break;
916
920 break;
921
925 break;
926
927 default:
928 break;
929 }
930
931 /* EOC marker reached */
933
935 }
936
937 /* TIER-1 routines */
939 int bpno, int bandno, int bpass_csty_symbol,
940 int vert_causal_ctx_csty_symbol)
941 {
942 int mask = 3 << (bpno - 1), y0, x,
y;
943
944 for (y0 = 0; y0 <
height; y0 += 4)
945 for (x = 0; x <
width; x++)
946 for (
y = y0;
y < height &&
y < y0 + 4;
y++) {
949 int flags_mask = -1;
950 if (vert_causal_ctx_csty_symbol &&
y == y0 + 3)
954 if (bpass_csty_symbol)
956 else
958 -mask : mask;
959
962 }
964 }
965 }
966 }
967
969 int bpno)
970 {
971 int phalf, nhalf;
973
974 phalf = 1 << (bpno - 1);
975 nhalf = -phalf;
976
977 for (y0 = 0; y0 <
height; y0 += 4)
978 for (x = 0; x <
width; x++)
979 for (y = y0; y < height && y < y0 + 4; y++)
984 ? phalf : nhalf;
987 }
988 }
989
992 int seg_symbols, int vert_causal_ctx_csty_symbol)
993 {
994 int mask = 3 << (bpno - 1), y0, x,
y, runlen, dec;
995
996 for (y0 = 0; y0 <
height; y0 += 4) {
997 for (x = 0; x <
width; x++) {
998 if (y0 + 3 < height &&
1004 continue;
1010 dec = 1;
1011 } else {
1012 runlen = 0;
1013 dec = 0;
1014 }
1015
1016 for (
y = y0 + runlen;
y < y0 + 4 &&
y <
height;
y++) {
1017 if (!dec) {
1019 int flags_mask = -1;
1020 if (vert_causal_ctx_csty_symbol &&
y == y0 + 3)
1023 bandno));
1024 }
1025 }
1026 if (dec) {
1027 int xorbit;
1029 &xorbit);
1032 xorbit)
1033 ? -mask : mask;
1035 }
1036 dec = 0;
1038 }
1039 }
1040 }
1041 if (seg_symbols) {
1047 if (val != 0xa)
1049 "Segmentation symbol value incorrect\n");
1050 }
1051 }
1052
1056 {
1058 int clnpass_cnt = 0;
1061
1064
1066 memset(t1->
data[
y], 0, width *
sizeof(**t1->
data));
1067
1068 /* If code-block contains no compressed data: nothing to do. */
1070 return 0;
1071
1072 for (
y = 0;
y < height + 2;
y++)
1073 memset(t1->
flags[
y], 0, (width + 2) *
sizeof(**t1->
flags));
1074
1078
1079 while (passno--) {
1080 switch(pass_t) {
1081 case 0:
1083 bpass_csty_symbol && (clnpass_cnt >= 4),
1084 vert_causal_ctx_csty_symbol);
1085 break;
1086 case 1:
1088 if (bpass_csty_symbol && clnpass_cnt >= 4)
1090 break;
1091 case 2:
1094 vert_causal_ctx_csty_symbol);
1095 clnpass_cnt = clnpass_cnt + 1;
1096 if (bpass_csty_symbol && clnpass_cnt >= 4)
1098 break;
1099 }
1100
1101 pass_t++;
1102 if (pass_t == 3) {
1103 bpno--;
1104 pass_t = 0;
1105 }
1106 }
1107 return 0;
1108 }
1109
1110 /* TODO: Verify dequantization for lossless case
1111 * comp->data can be float or int
1112 * band->stepsize can be float or int
1113 * depending on the type of DWT transformation.
1114 * see ISO/IEC 15444-1:2002 A.6.1 */
1115
1116 /* Float dequantization of a codeblock.*/
1120 {
1121 int i, j;
1122 int w = cblk->
coord[0][1] - cblk->
coord[0][0];
1123 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
1124 float *datap = &comp->
f_data[(comp->
coord[0][1] - comp->
coord[0][0]) * (y + j) + x];
1126 for (i = 0; i < w; ++i)
1128 }
1129 }
1130
1131 /* Integer dequantization of a codeblock.*/
1135 {
1136 int i, j;
1137 int w = cblk->
coord[0][1] - cblk->
coord[0][0];
1138 for (j = 0; j < (cblk->
coord[1][1] - cblk->
coord[1][0]); ++j) {
1141 for (i = 0; i < w; ++i)
1142 datap[i] = (src[i] * band->
i_stepsize + (1 << 14)) >> 15;
1143 }
1144 }
1145
1147 {
1148 int i, csize = 1;
1150
1151 for (i = 1; i < 3; i++)
1154 return;
1155 }
1156
1157 for (i = 0; i < 3; i++)
1160 else
1162
1163 for (i = 0; i < 2; i++)
1165
1167 }
1168
1171 {
1173 int compno, reslevelno, bandno;
1177
1180
1181 /* Loop on tile components */
1182 for (compno = 0; compno < s->
ncomponents; compno++) {
1185
1186 /* Loop on resolution levels */
1189 /* Loop on bands */
1190 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
1191 int nb_precincts, precno;
1193 int cblkno = 0, bandpos;
1194
1195 bandpos = bandno + (reslevelno > 0);
1196
1197 if (band->
coord[0][0] == band->
coord[0][1] ||
1199 continue;
1200
1202 /* Loop on precincts */
1203 for (precno = 0; precno < nb_precincts; precno++) {
1205
1206 /* Loop on codeblocks */
1213 bandpos);
1214
1215 x = cblk->
coord[0][0];
1216 y = cblk->
coord[1][0];
1217
1220 else
1222 } /* end cblk */
1223 } /*end prec */
1224 } /* end band */
1225 } /* end reslevel */
1226
1227 /* inverse DWT */
1229 } /*end comp */
1230
1231 /* inverse MCT transformation */
1234
1235 if (s->
cdef[0] < 0) {
1240 }
1241
1243 for (compno = 0; compno < s->
ncomponents; compno++) {
1246 float *datap = comp->
f_data;
1248 int cbps = s->
cbps[compno];
1250 int plane = 0;
1251
1252 if (planar)
1254
1255
1257 line = picture->
data[plane] + y / s->
cdy[compno] * picture->
linesize[plane];
1260
1262 dst = line + x / s->
cdx[compno] * pixelsize + compno*!planar;
1263
1265 for (; x < w; x += s->
cdx[compno]) {
1266 int val =
lrintf(*datap) + (1 << (cbps - 1));
1267 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
1268 val = av_clip(val, 0, (1 << cbps) - 1);
1269 *dst = val << (8 - cbps);
1270 datap++;
1271 dst += pixelsize;
1272 }
1273 } else {
1274 for (; x < w; x += s->
cdx[compno]) {
1275 int val = *i_datap + (1 << (cbps - 1));
1276 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
1277 val = av_clip(val, 0, (1 << cbps) - 1);
1278 *dst = val << (8 - cbps);
1279 i_datap++;
1280 dst += pixelsize;
1281 }
1282 }
1284 }
1285 }
1286 } else {
1287 for (compno = 0; compno < s->
ncomponents; compno++) {
1290 float *datap = comp->
f_data;
1292 uint16_t *linel;
1293 int cbps = s->
cbps[compno];
1295 int plane = 0;
1296
1297 if (planar)
1299
1301 linel = (uint16_t *)picture->
data[plane] + y / s->
cdy[compno] * (picture->
linesize[plane] >> 1);
1303 uint16_t *dst;
1304
1306 dst = linel + (x / s->
cdx[compno] * pixelsize + compno*!planar);
1308 for (; x < w; x += s-> cdx[compno]) {
1309 int val =
lrintf(*datap) + (1 << (cbps - 1));
1310 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
1311 val = av_clip(val, 0, (1 << cbps) - 1);
1312 /* align 12 bit values in little-endian mode */
1313 *dst = val << (16 - cbps);
1314 datap++;
1315 dst += pixelsize;
1316 }
1317 } else {
1318 for (; x < w; x += s-> cdx[compno]) {
1319 int val = *i_datap + (1 << (cbps - 1));
1320 /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
1321 val = av_clip(val, 0, (1 << cbps) - 1);
1322 /* align 12 bit values in little-endian mode */
1323 *dst = val << (16 - cbps);
1324 i_datap++;
1325 dst += pixelsize;
1326 }
1327 }
1328 linel += picture->
linesize[plane] >> 1;
1329 }
1330 }
1331 }
1332
1333 return 0;
1334 }
1335
1337 {
1338 int tileno, compno;
1341 for (compno = 0; compno < s->
ncomponents; compno++) {
1344
1346 }
1348 }
1349 }
1354 }
1355
1357 {
1361
1362 for (;;) {
1364 uint16_t marker;
1365 int oldpos;
1366
1369 break;
1370 }
1371
1372 marker = bytestream2_get_be16u(&s->
g);
1374
1378
1382 }
1386 }
1387
1393 }
1396
1397 continue;
1398 }
1400 break;
1401
1402 len = bytestream2_get_be16(&s->
g);
1405
1406 switch (marker) {
1411 break;
1413 ret =
get_coc(s, codsty, properties);
1414 break;
1416 ret =
get_cod(s, codsty, properties);
1417 break;
1419 ret =
get_qcc(s, len, qntsty, properties);
1420 break;
1422 ret =
get_qcd(s, len, qntsty, properties);
1423 break;
1425 if (!(ret =
get_sot(s, len))) {
1430 }
1431 break;
1433 // the comment is ignored
1435 break;
1437 // Tile-part lengths
1439 break;
1440 default:
1442 "unsupported marker 0x%.4"PRIX16" at pos 0x%X\n",
1445 break;
1446 }
1449 "error during processing marker segment %.4"PRIx16"\n",
1450 marker);
1451 return ret ? ret : -1;
1452 }
1453 }
1454 return 0;
1455 }
1456
1457 /* Read bit stream packets --> T2 operation. */
1459 {
1461 int tileno;
1462
1465
1468
1472 }
1473
1474 return 0;
1475 }
1476
1478 {
1479 uint32_t atom_size, atom, atom_end;
1480 int search_range = 10;
1481
1482 while (search_range
1483 &&
1485 atom_size = bytestream2_get_be32u(&s->
g);
1486 atom = bytestream2_get_be32u(&s->
g);
1488
1490 return 1;
1491
1493 return 0;
1494
1496 atom_size >= 16) {
1497 uint32_t atom2_size, atom2, atom2_end;
1498 do {
1499 atom2_size = bytestream2_get_be32u(&s->
g);
1500 atom2 = bytestream2_get_be32u(&s->
g);
1502 if (atom2_size < 8 || atom2_end > atom_end || atom2_end < atom2_size)
1503 break;
1505 return 1;
1506 }
else if (atom2 ==
MKBETAG(
'c',
'o',
'l',
'r') && atom2_size >= 7) {
1507 int method = bytestream2_get_byteu(&s->
g);
1509 if (method == 1) {
1511 }
1512 }
else if (atom2 ==
MKBETAG(
'p',
'c',
'l',
'r') && atom2_size >= 6) {
1513 int i,
size, colour_count, colour_channels, colour_depth[3];
1515 colour_count = bytestream2_get_be16u(&s->
g);
1516 colour_channels = bytestream2_get_byteu(&s->
g);
1517 // FIXME: Do not ignore channel_sign
1518 colour_depth[0] = (bytestream2_get_byteu(&s->
g) & 0x7f) + 1;
1519 colour_depth[1] = (bytestream2_get_byteu(&s->
g) & 0x7f) + 1;
1520 colour_depth[2] = (bytestream2_get_byteu(&s->
g) & 0x7f) + 1;
1521 size = (colour_depth[0] + 7 >> 3) * colour_count +
1522 (colour_depth[1] + 7 >> 3) * colour_count +
1523 (colour_depth[2] + 7 >> 3) * colour_count;
1524 if (colour_count > 256 ||
1525 colour_channels != 3 ||
1526 colour_depth[0] > 16 ||
1527 colour_depth[1] > 16 ||
1528 colour_depth[2] > 16 ||
1529 atom2_size < size) {
1532 continue;
1533 }
1535 for (i = 0; i < colour_count; i++) {
1536 if (colour_depth[0] <= 8) {
1537 r = bytestream2_get_byteu(&s->
g) << 8 - colour_depth[0];
1538 r |= r >> colour_depth[0];
1539 } else {
1540 r = bytestream2_get_be16u(&s->
g) >> colour_depth[0] - 8;
1541 }
1542 if (colour_depth[1] <= 8) {
1543 g = bytestream2_get_byteu(&s->
g) << 8 - colour_depth[1];
1544 r |= r >> colour_depth[1];
1545 } else {
1546 g = bytestream2_get_be16u(&s->
g) >> colour_depth[1] - 8;
1547 }
1548 if (colour_depth[2] <= 8) {
1549 b = bytestream2_get_byteu(&s->
g) << 8 - colour_depth[2];
1550 r |= r >> colour_depth[2];
1551 } else {
1552 b = bytestream2_get_be16u(&s->
g) >> colour_depth[2] - 8;
1553 }
1554 s->
palette[i] = 0xff
u << 24 | r << 16 | g << 8 |
b;
1555 }
1556 }
else if (atom2 ==
MKBETAG(
'c',
'd',
'e',
'f') && atom2_size >= 2) {
1557 int n = bytestream2_get_be16u(&s->
g);
1558 for (; n>0; n--) {
1559 int cn = bytestream2_get_be16(&s->
g);
1560 int av_unused typ = bytestream2_get_be16(&s->
g);
1561 int asoc = bytestream2_get_be16(&s->
g);
1562 if (cn < 4 || asoc < 4)
1564 }
1565 }
1567 } while (atom_end - atom2_end >= 8);
1568 } else {
1569 search_range--;
1570 }
1572 }
1573
1574 return 0;
1575 }
1576
1578 {
1580
1582
1583 return 0;
1584 }
1585
1588 {
1593
1597 memset(s->
cdef, -1,
sizeof(s->
cdef));
1598
1602 }
1603
1604 // check if the image is in jp2 format
1606 (bytestream2_get_be32u(&s->
g) == 12) &&
1611 "Could not find Jpeg2000 codestream atom.\n");
1614 }
1615 } else {
1617 }
1618
1621
1626 }
1629
1630 /* get picture buffer */
1635
1638
1642
1644
1645 *got_frame = 1;
1646
1648 memcpy(picture->
data[1], s->
palette, 256 *
sizeof(uint32_t));
1649
1651
1655 }
1656
1658 {
1661 }
1662
1663 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
1664 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1665
1667 { "lowres", "Lower the decoding resolution by a power of two",
1670 };
1671
1679 };
1680
1686 };
1687
1698 .priv_class = &jpeg2000_class,
1699 .max_lowres = 5,
1701 };