1 /*
2 * JPEG 2000 encoder and decoder common functions
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 encoder and decoder common functions
26 */
27
34
35 #define SHL(a, n) ((n) >= 0 ? (a) << (n) : (a) >> -(n))
36
37 /* tag tree routines */
38
39 /* allocate the memory for tag tree */
41 {
42 uint32_t res = 0;
43 while (w > 1 || h > 1) {
44 res += w * h;
46 w = (w + 1) >> 1;
47 h = (h + 1) >> 1;
48 }
50 }
51
53 {
54 int pw = w, ph = h;
57
59
61 if (!res)
63
64 while (w > 1 || h > 1) {
65 int i, j;
66 pw = w;
67 ph = h;
68
69 w = (w + 1) >> 1;
70 h = (h + 1) >> 1;
71 t2 = t + pw * ph;
72
73 for (i = 0; i < ph; i++)
74 for (j = 0; j < pw; j++)
75 t[i * pw + j].parent = &t2[(i >> 1) * w + (j >> 1)];
76
78 }
80 return res;
81 }
82
84 {
86
87 for (i = 0; i < siz; i++) {
90 }
91 }
92
94
96 {
98
107
108 if (bandno < 3) {
109 if (bandno == 1)
111 if (h == 2) return 8;
112 if (h == 1) {
113 if (v >= 1) return 7;
114 if (d >= 1) return 6;
115 return 5;
116 }
117 if (v == 2) return 4;
118 if (v == 1) return 3;
119 if (d >= 2) return 2;
120 if (d == 1) return 1;
121 } else {
122 if (d >= 3) return 8;
123 if (d == 2) {
124 if (h+v >= 1) return 7;
125 return 6;
126 }
127 if (d == 1) {
128 if (h+v >= 2) return 5;
129 if (h+v == 1) return 4;
130 return 3;
131 }
132 if (h+v >= 2) return 2;
133 if (h+v == 1) return 1;
134 }
135 return 0;
136 }
137
139
140 static const int contribtab[3][3] = { { 0, -1, 1 }, { -1, -1, 0 }, { 1, 0, 1 } };
141 static const int ctxlbltab[3][3] = { { 13, 12, 11 }, { 10, 9, 10 }, { 11, 12, 13 } };
142 static const int xorbittab[3][3] = { { 1, 1, 1 }, { 1, 0, 0 }, { 0, 0, 0 } };
143
145 {
146 int vcontrib, hcontrib;
147
153
155 }
156
158 {
159 int i, j;
160 for (i = 0; i < 256; i++)
161 for (j = 0; j < 4; j++)
163 for (i = 0; i < 16; i++)
164 for (j = 0; j < 16; j++)
167 }
168
170 int negative)
171 {
172 x++;
173 y++;
175 if (negative) {
180 } else {
185 }
190 }
191
193
197 int cbps, int dx, int dy,
199 {
200 uint8_t log2_band_prec_width, log2_band_prec_height;
201 int reslevelno, bandno, gbandno = 0,
ret, i, j;
202 uint32_t csize;
203
207 }
208
213 // component size comp->coord is uint16_t so ir cannot overflow
214 csize = (comp->
coord[0][1] - comp->
coord[0][0]) *
216
222 } else {
227 }
231 /* LOOP on resolution levels */
232 for (reslevelno = 0; reslevelno < codsty->
nreslevels; reslevelno++) {
233 int declvl = codsty->
nreslevels - reslevelno;
// N_L -r see ISO/IEC 15444-1:2002 B.5
235
236 /* Compute borders for each resolution level.
237 * Computation of trx_0, trx_1, try_0 and try_1.
238 * see ISO/IEC 15444-1:2002 eq. B.5 and B-14 */
239 for (i = 0; i < 2; i++)
240 for (j = 0; j < 2; j++)
241 reslevel->
coord[i][j] =
243 // update precincts size: 2^n value
246
247 /* Number of bands for each resolution level */
248 if (reslevelno == 0)
250 else
252
253 /* Number of precincts which span the tile for resolution level reslevelno
254 * see B.6 in ISO/IEC 15444-1:2002 eq. B-16
255 * num_precincts_x = |- trx_1 / 2 ^ log2_prec_width) -| - (trx_0 / 2 ^ log2_prec_width)
256 * num_precincts_y = |- try_1 / 2 ^ log2_prec_width) -| - (try_0 / 2 ^ log2_prec_width)
257 * for Dcinema profiles in JPEG 2000
258 * num_precincts_x = |- trx_1 / 2 ^ log2_prec_width) -|
259 * num_precincts_y = |- try_1 / 2 ^ log2_prec_width) -| */
260 if (reslevel->
coord[0][1] == reslevel->
coord[0][0])
262 else
267
268 if (reslevel->
coord[1][1] == reslevel->
coord[1][0])
270 else
275
279
280 for (bandno = 0; bandno < reslevel->
nbands; bandno++, gbandno++) {
282 int cblkno, precno;
283 int nb_precincts;
284
285 /* TODO: Implementation of quantization step not finished,
286 * see ISO/IEC 15444-1:2002 E.1 and A.6.4. */
289 int numbps;
291 /* TODO: to verify. No quantization in this case */
293 break;
295 /*TODO: Compute formula to implement. */
296 numbps = cbps +
299 2 + numbps - qntsty->
expn[gbandno]);
300 break;
302 /* Exponent quantization step.
303 * Formula:
304 * delta_b = 2 ^ (R_b - expn_b) * (1 + (mant_b / 2 ^ 11))
305 * R_b = R_I + log2 (gain_b )
306 * see ISO/IEC 15444-1:2002 E.1.1 eqn. E-3 and E-4 */
307 /* TODO/WARN: value of log2 (gain_b ) not taken into account
308 * but it works (compared to OpenJPEG). Why?
309 * Further investigation needed. */
310 gain = cbps;
313 break;
314 default:
317 break;
318 }
319 /* FIXME: In openjepg code stespize = stepsize * 0.5. Why?
320 * If not set output of entropic decoder is not correct. */
323
325
326 /* computation of tbx_0, tbx_1, tby_0, tby_1
327 * see ISO/IEC 15444-1:2002 B.5 eq. B-15 and tbl B.1
328 * codeblock width and height is computed for
329 * DCI JPEG 2000 codeblock_width = codeblock_width = 32 = 2 ^ 5 */
330 if (reslevelno == 0) {
331 /* for reslevelno = 0, only one band, x0_b = y0_b = 0 */
332 for (i = 0; i < 2; i++)
333 for (j = 0; j < 2; j++)
336 declvl - 1);
339 /* see ISO/IEC 15444-1:2002 eq. B-17 and eq. B-15 */
344 } else {
345 /* 3 bands x0_b = 1 y0_b = 0; x0_b = 0 y0_b = 1; x0_b = y0_b = 1 */
346 /* x0_b and y0_b are computed with ((bandno + 1 >> i) & 1) */
347 for (i = 0; i < 2; i++)
348 for (j = 0; j < 2; j++)
349 /* Formula example for tbx_0 = ceildiv((tcx_0 - 2 ^ (declvl - 1) * x0_b) / declvl) */
352 (((bandno + 1 >> i) & 1) << declvl - 1),
353 declvl);
354 /* TODO: Manage case of 3 band offsets here or
355 * in coding/decoding function? */
356
357 /* see ISO/IEC 15444-1:2002 eq. B-17 and eq. B-15 */
362
365 }
366
367 for (j = 0; j < 2; j++)
369 for (j = 0; j < 2; j++)
371
375 }
380
381 for (precno = 0; precno < nb_precincts; precno++) {
383 int nb_codeblocks;
384
385 /* TODO: Explain formula for JPEG200 DCINEMA. */
386 /* TODO: Verify with previous count of codeblocks per band */
387
388 /* Compute P_x0 */
390 (1 << log2_band_prec_width);
392
393 /* Compute P_y0 */
395 (1 << log2_band_prec_height);
397
398 /* Compute P_x1 */
400 (1 << log2_band_prec_width);
402
403 /* Compute P_y1 */
405 (1 << log2_band_prec_height);
407
416
417 /* Tag trees initialization */
423
429
433 }
438 for (cblkno = 0; cblkno < nb_codeblocks; cblkno++) {
440 uint16_t Cx0, Cy0;
441
442 /* Compute coordinates of codeblocks */
443 /* Compute Cx0*/
447
448 /* Compute Cy0*/
452
453 /* Compute Cx1 */
456
457 /* Compute Cy1 */
460 /* Update code-blocks coordinates according sub-band position */
461 if ((bandno + !!reslevelno) & 1) {
466 }
467 if ((bandno + !!reslevelno) & 2) {
472 }
473
479 }
480 }
481 }
482 }
483 return 0;
484 }
485
487 {
488 int reslevelno, bandno, cblkno, precno;
489 for (reslevelno = 0; reslevelno < codsty->
nreslevels; reslevelno++) {
491 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
501 }
502 }
503 }
504 }
505 }
506
508 {
509 int reslevelno, bandno, precno;
510 for (reslevelno = 0;
512 reslevelno++) {
514
516 continue;
517
518 reslevel = comp->
reslevel + reslevelno;
519 for (bandno = 0; bandno < reslevel->
nbands; bandno++) {
521
523 continue;
524
525 band = reslevel->
band + bandno;
532 }
533 }
534
536 }
538 }
539
544 }