5889f805760be7cafe4e97d5a8714b606b482797
[libav.git] / libavcodec / jpeg2000.c
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 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 encoder and decoder common functions
26 */
27
28 #include "libavutil/common.h"
29 #include "libavutil/mem.h"
30 #include "avcodec.h"
31 #include "jpeg2000.h"
32
33 #define SHL(a, n) ((n) >= 0 ? (a) << (n) : (a) >> -(n))
34
35 /* tag tree routines */
36
37 /* allocate the memory for tag tree */
38 static int32_t tag_tree_size(uint16_t w, uint16_t h)
39 {
40 uint32_t res = 0;
41 while (w > 1 || h > 1) {
42 res += w * h;
43 if (res + 1 >= INT32_MAX)
44 return -1;
45 w = (w + 1) >> 1;
46 h = (h + 1) >> 1;
47 }
48 return (int32_t)(res + 1);
49 }
50
51 static Jpeg2000TgtNode *ff_jpeg2000_tag_tree_init(int w, int h)
52 {
53 int pw = w, ph = h;
54 Jpeg2000TgtNode *res, *t, *t2;
55 int32_t tt_size;
56
57 tt_size = tag_tree_size(w, h);
58 if (tt_size == -1)
59 return NULL;
60
61 t = res = av_mallocz_array(tt_size, sizeof(*t));
62 if (!res)
63 return NULL;
64
65 while (w > 1 || h > 1) {
66 int i, j;
67 pw = w;
68 ph = h;
69
70 w = (w + 1) >> 1;
71 h = (h + 1) >> 1;
72 t2 = t + pw * ph;
73
74 for (i = 0; i < ph; i++)
75 for (j = 0; j < pw; j++)
76 t[i * pw + j].parent = &t2[(i >> 1) * w + (j >> 1)];
77
78 t = t2;
79 }
80 t[0].parent = NULL;
81 return res;
82 }
83
84 uint8_t ff_jpeg2000_sigctxno_lut[256][4];
85
86 static int getsigctxno(int flag, int bandno)
87 {
88 int h, v, d;
89
90 h = ((flag & JPEG2000_T1_SIG_E) ? 1 : 0) +
91 ((flag & JPEG2000_T1_SIG_W) ? 1 : 0);
92 v = ((flag & JPEG2000_T1_SIG_N) ? 1 : 0) +
93 ((flag & JPEG2000_T1_SIG_S) ? 1 : 0);
94 d = ((flag & JPEG2000_T1_SIG_NE) ? 1 : 0) +
95 ((flag & JPEG2000_T1_SIG_NW) ? 1 : 0) +
96 ((flag & JPEG2000_T1_SIG_SE) ? 1 : 0) +
97 ((flag & JPEG2000_T1_SIG_SW) ? 1 : 0);
98 if (bandno < 3) {
99 if (bandno == 1)
100 FFSWAP(int, h, v);
101 if (h == 2)
102 return 8;
103 if (h == 1) {
104 if (v >= 1)
105 return 7;
106 if (d >= 1)
107 return 6;
108 return 5;
109 }
110 if (v == 2)
111 return 4;
112 if (v == 1)
113 return 3;
114 if (d >= 2)
115 return 2;
116 if (d == 1)
117 return 1;
118 return 0;
119 } else {
120 if (d >= 3)
121 return 8;
122 if (d == 2) {
123 if (h + v >= 1)
124 return 7;
125 return 6;
126 }
127 if (d == 1) {
128 if (h + v >= 2)
129 return 5;
130 if (h + v == 1)
131 return 4;
132 return 3;
133 }
134 if (h + v >= 2)
135 return 2;
136 if (h + v == 1)
137 return 1;
138 return 0;
139 }
140 return 0;
141 }
142
143 uint8_t ff_jpeg2000_sgnctxno_lut[16][16], ff_jpeg2000_xorbit_lut[16][16];
144
145 static const int contribtab[3][3] = { { 0, -1, 1 }, { -1, -1, 0 }, { 1, 0, 1 } };
146 static const int ctxlbltab[3][3] = { { 13, 12, 11 }, { 10, 9, 10 }, { 11, 12, 13 } };
147 static const int xorbittab[3][3] = { { 1, 1, 1 }, { 1, 0, 0 }, { 0, 0, 0 } };
148
149 static int getsgnctxno(int flag, uint8_t *xorbit)
150 {
151 int vcontrib, hcontrib;
152
153 hcontrib = contribtab[flag & JPEG2000_T1_SIG_E ? flag & JPEG2000_T1_SGN_E ? 1 : 2 : 0]
154 [flag & JPEG2000_T1_SIG_W ? flag & JPEG2000_T1_SGN_W ? 1 : 2 : 0] + 1;
155 vcontrib = contribtab[flag & JPEG2000_T1_SIG_S ? flag & JPEG2000_T1_SGN_S ? 1 : 2 : 0]
156 [flag & JPEG2000_T1_SIG_N ? flag & JPEG2000_T1_SGN_N ? 1 : 2 : 0] + 1;
157 *xorbit = xorbittab[hcontrib][vcontrib];
158
159 return ctxlbltab[hcontrib][vcontrib];
160 }
161
162 void ff_jpeg2000_init_tier1_luts(void)
163 {
164 int i, j;
165 for (i = 0; i < 256; i++)
166 for (j = 0; j < 4; j++)
167 ff_jpeg2000_sigctxno_lut[i][j] = getsigctxno(i, j);
168 for (i = 0; i < 16; i++)
169 for (j = 0; j < 16; j++)
170 ff_jpeg2000_sgnctxno_lut[i][j] =
171 getsgnctxno(i + (j << 8), &ff_jpeg2000_xorbit_lut[i][j]);
172 }
173
174 void ff_jpeg2000_set_significance(Jpeg2000T1Context *t1, int x, int y,
175 int negative)
176 {
177 x++;
178 y++;
179 t1->flags[y][x] |= JPEG2000_T1_SIG;
180 if (negative) {
181 t1->flags[y][x + 1] |= JPEG2000_T1_SIG_W | JPEG2000_T1_SGN_W;
182 t1->flags[y][x - 1] |= JPEG2000_T1_SIG_E | JPEG2000_T1_SGN_E;
183 t1->flags[y + 1][x] |= JPEG2000_T1_SIG_N | JPEG2000_T1_SGN_N;
184 t1->flags[y - 1][x] |= JPEG2000_T1_SIG_S | JPEG2000_T1_SGN_S;
185 } else {
186 t1->flags[y][x + 1] |= JPEG2000_T1_SIG_W;
187 t1->flags[y][x - 1] |= JPEG2000_T1_SIG_E;
188 t1->flags[y + 1][x] |= JPEG2000_T1_SIG_N;
189 t1->flags[y - 1][x] |= JPEG2000_T1_SIG_S;
190 }
191 t1->flags[y + 1][x + 1] |= JPEG2000_T1_SIG_NW;
192 t1->flags[y + 1][x - 1] |= JPEG2000_T1_SIG_NE;
193 t1->flags[y - 1][x + 1] |= JPEG2000_T1_SIG_SW;
194 t1->flags[y - 1][x - 1] |= JPEG2000_T1_SIG_SE;
195 }
196
197 static const uint8_t lut_gain[2][4] = { { 0, 0, 0, 0 }, { 0, 1, 1, 2 } };
198
199 int ff_jpeg2000_init_component(Jpeg2000Component *comp,
200 Jpeg2000CodingStyle *codsty,
201 Jpeg2000QuantStyle *qntsty,
202 int cbps, int dx, int dy,
203 AVCodecContext *avctx)
204 {
205 uint8_t log2_band_prec_width, log2_band_prec_height;
206 int reslevelno, bandno, gbandno = 0, ret, i, j;
207 uint32_t csize = 1;
208
209 if (!codsty->nreslevels2decode) {
210 av_log(avctx, AV_LOG_ERROR, "nreslevels2decode uninitialized\n");
211 return AVERROR_INVALIDDATA;
212 }
213
214 if (ret = ff_jpeg2000_dwt_init(&comp->dwt, comp->coord,
215 codsty->nreslevels2decode - 1,
216 codsty->transform))
217 return ret;
218 // component size comp->coord is uint16_t so ir cannot overflow
219 csize = (comp->coord[0][1] - comp->coord[0][0]) *
220 (comp->coord[1][1] - comp->coord[1][0]);
221
222 comp->data = av_malloc_array(csize, sizeof(*comp->data));
223 if (!comp->data)
224 return AVERROR(ENOMEM);
225 comp->reslevel = av_malloc_array(codsty->nreslevels, sizeof(*comp->reslevel));
226 if (!comp->reslevel)
227 return AVERROR(ENOMEM);
228 /* LOOP on resolution levels */
229 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
230 int declvl = codsty->nreslevels - reslevelno; // N_L -r see ISO/IEC 15444-1:2002 B.5
231 Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
232
233 /* Compute borders for each resolution level.
234 * Computation of trx_0, trx_1, try_0 and try_1.
235 * see ISO/IEC 15444-1:2002 eq. B.5 and B-14 */
236 for (i = 0; i < 2; i++)
237 for (j = 0; j < 2; j++)
238 reslevel->coord[i][j] =
239 ff_jpeg2000_ceildivpow2(comp->coord_o[i][j], declvl - 1);
240 // update precincts size: 2^n value
241 reslevel->log2_prec_width = codsty->log2_prec_widths[reslevelno];
242 reslevel->log2_prec_height = codsty->log2_prec_heights[reslevelno];
243
244 /* Number of bands for each resolution level */
245 if (reslevelno == 0)
246 reslevel->nbands = 1;
247 else
248 reslevel->nbands = 3;
249
250 /* Number of precincts wich span the tile for resolution level reslevelno
251 * see B.6 in ISO/IEC 15444-1:2002 eq. B-16
252 * num_precincts_x = |- trx_1 / 2 ^ log2_prec_width) -| - (trx_0 / 2 ^ log2_prec_width)
253 * num_precincts_y = |- try_1 / 2 ^ log2_prec_width) -| - (try_0 / 2 ^ log2_prec_width)
254 * for Dcinema profiles in JPEG 2000
255 * num_precincts_x = |- trx_1 / 2 ^ log2_prec_width) -|
256 * num_precincts_y = |- try_1 / 2 ^ log2_prec_width) -| */
257 if (reslevel->coord[0][1] == reslevel->coord[0][0])
258 reslevel->num_precincts_x = 0;
259 else
260 reslevel->num_precincts_x =
261 ff_jpeg2000_ceildivpow2(reslevel->coord[0][1],
262 reslevel->log2_prec_width) -
263 (reslevel->coord[0][0] >> reslevel->log2_prec_width);
264
265 if (reslevel->coord[1][1] == reslevel->coord[1][0])
266 reslevel->num_precincts_y = 0;
267 else
268 reslevel->num_precincts_y =
269 ff_jpeg2000_ceildivpow2(reslevel->coord[1][1],
270 reslevel->log2_prec_height) -
271 (reslevel->coord[1][0] >> reslevel->log2_prec_height);
272
273 reslevel->band = av_malloc_array(reslevel->nbands, sizeof(*reslevel->band));
274 if (!reslevel->band)
275 return AVERROR(ENOMEM);
276
277 for (bandno = 0; bandno < reslevel->nbands; bandno++, gbandno++) {
278 Jpeg2000Band *band = reslevel->band + bandno;
279 int cblkno, precno;
280 int nb_precincts;
281
282 /* TODO: Implementation of quantization step not finished,
283 * see ISO/IEC 15444-1:2002 E.1 and A.6.4. */
284 switch (qntsty->quantsty) {
285 uint8_t gain;
286 int numbps;
287 case JPEG2000_QSTY_NONE:
288 /* TODO: to verify. No quantization in this case */
289 numbps = cbps +
290 lut_gain[codsty->transform][bandno + reslevelno > 0];
291 band->stepsize = (float)SHL(2048 + qntsty->mant[gbandno],
292 2 + numbps - qntsty->expn[gbandno]);
293 break;
294 case JPEG2000_QSTY_SI:
295 /*TODO: Compute formula to implement. */
296 band->stepsize = (float) (1 << 13);
297 break;
298 case JPEG2000_QSTY_SE:
299 /* Exponent quantization step.
300 * Formula:
301 * delta_b = 2 ^ (R_b - expn_b) * (1 + (mant_b / 2 ^ 11))
302 * R_b = R_I + log2 (gain_b )
303 * see ISO/IEC 15444-1:2002 E.1.1 eqn. E-3 and E-4 */
304 /* TODO/WARN: value of log2 (gain_b ) not taken into account
305 * but it works (compared to OpenJPEG). Why?
306 * Further investigation needed. */
307 gain = cbps;
308 band->stepsize = pow(2.0, gain - qntsty->expn[gbandno]);
309 band->stepsize *= (float)qntsty->mant[gbandno] / 2048.0 + 1.0;
310 /* FIXME: In openjepg code stespize = stepsize * 0.5. Why?
311 * If not set output of entropic decoder is not correct. */
312 band->stepsize *= 0.5;
313 break;
314 default:
315 band->stepsize = 0;
316 av_log(avctx, AV_LOG_ERROR, "Unknown quantization format\n");
317 break;
318 }
319 /* BITEXACT computing case --> convert to int */
320 if (avctx->flags & CODEC_FLAG_BITEXACT)
321 band->stepsize = (int32_t)(band->stepsize * (1 << 16));
322
323 /* computation of tbx_0, tbx_1, tby_0, tby_1
324 * see ISO/IEC 15444-1:2002 B.5 eq. B-15 and tbl B.1
325 * codeblock width and height is computed for
326 * DCI JPEG 2000 codeblock_width = codeblock_width = 32 = 2 ^ 5 */
327 if (reslevelno == 0) {
328 /* for reslevelno = 0, only one band, x0_b = y0_b = 0 */
329 for (i = 0; i < 2; i++)
330 for (j = 0; j < 2; j++)
331 band->coord[i][j] =
332 ff_jpeg2000_ceildivpow2(comp->coord_o[i][j],
333 declvl - 1);
334
335 log2_band_prec_width = reslevel->log2_prec_width;
336 log2_band_prec_height = reslevel->log2_prec_height;
337 /* see ISO/IEC 15444-1:2002 eq. B-17 and eq. B-15 */
338 band->log2_cblk_width = FFMIN(codsty->log2_cblk_width,
339 reslevel->log2_prec_width);
340 band->log2_cblk_height = FFMIN(codsty->log2_cblk_height,
341 reslevel->log2_prec_height);
342 } else {
343 /* 3 bands x0_b = 1 y0_b = 0; x0_b = 0 y0_b = 1; x0_b = y0_b = 1 */
344 /* x0_b and y0_b are computed with ((bandno + 1 >> i) & 1) */
345 for (i = 0; i < 2; i++)
346 for (j = 0; j < 2; j++)
347 /* Formula example for tbx_0 = ceildiv((tcx_0 - 2 ^ (declvl - 1) * x0_b) / declvl) */
348 band->coord[i][j] =
349 ff_jpeg2000_ceildivpow2(comp->coord_o[i][j] -
350 (((bandno + 1 >> i) & 1) << declvl - 1),
351 declvl);
352 /* TODO: Manage case of 3 band offsets here or
353 * in coding/decoding function? */
354
355 /* see ISO/IEC 15444-1:2002 eq. B-17 and eq. B-15 */
356 band->log2_cblk_width = FFMIN(codsty->log2_cblk_width,
357 reslevel->log2_prec_width - 1);
358 band->log2_cblk_height = FFMIN(codsty->log2_cblk_height,
359 reslevel->log2_prec_height - 1);
360
361 log2_band_prec_width = reslevel->log2_prec_width - 1;
362 log2_band_prec_height = reslevel->log2_prec_height - 1;
363 }
364
365 band->prec = av_malloc_array(reslevel->num_precincts_x *
366 reslevel->num_precincts_y,
367 sizeof(*band->prec));
368 if (!band->prec)
369 return AVERROR(ENOMEM);
370
371 nb_precincts = reslevel->num_precincts_x * reslevel->num_precincts_y;
372
373 for (precno = 0; precno < nb_precincts; precno++) {
374 Jpeg2000Prec *prec = band->prec + precno;
375
376 /* TODO: Explain formula for JPEG200 DCINEMA. */
377 /* TODO: Verify with previous count of codeblocks per band */
378
379 /* Compute P_x0 */
380 prec->coord[0][0] = (precno % reslevel->num_precincts_x) *
381 (1 << log2_band_prec_width);
382 prec->coord[0][0] = FFMAX(prec->coord[0][0], band->coord[0][0]);
383
384 /* Compute P_y0 */
385 prec->coord[1][0] = (precno / reslevel->num_precincts_x) *
386 (1 << log2_band_prec_height);
387 prec->coord[1][0] = FFMAX(prec->coord[1][0], band->coord[1][0]);
388
389 /* Compute P_x1 */
390 prec->coord[0][1] = prec->coord[0][0] +
391 (1 << log2_band_prec_width);
392 prec->coord[0][1] = FFMIN(prec->coord[0][1], band->coord[0][1]);
393
394 /* Compute P_y1 */
395 prec->coord[1][1] = prec->coord[1][0] +
396 (1 << log2_band_prec_height);
397 prec->coord[1][1] = FFMIN(prec->coord[1][1], band->coord[1][1]);
398
399 prec->nb_codeblocks_width =
400 ff_jpeg2000_ceildivpow2(prec->coord[0][1] -
401 prec->coord[0][0],
402 band->log2_cblk_width);
403 prec->nb_codeblocks_height =
404 ff_jpeg2000_ceildivpow2(prec->coord[1][1] -
405 prec->coord[1][0],
406 band->log2_cblk_height);
407
408 /* Tag trees initialization */
409 prec->cblkincl =
410 ff_jpeg2000_tag_tree_init(prec->nb_codeblocks_width,
411 prec->nb_codeblocks_height);
412 if (!prec->cblkincl)
413 return AVERROR(ENOMEM);
414
415 prec->zerobits =
416 ff_jpeg2000_tag_tree_init(prec->nb_codeblocks_width,
417 prec->nb_codeblocks_height);
418 if (!prec->zerobits)
419 return AVERROR(ENOMEM);
420
421 prec->cblk = av_malloc_array(prec->nb_codeblocks_width *
422 prec->nb_codeblocks_height,
423 sizeof(*prec->cblk));
424 if (!prec->cblk)
425 return AVERROR(ENOMEM);
426 for (cblkno = 0; cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height; cblkno++) {
427 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
428 uint16_t Cx0, Cy0;
429
430 /* Compute coordinates of codeblocks */
431 /* Compute Cx0*/
432 Cx0 = (prec->coord[0][0] >> band->log2_cblk_width) << band->log2_cblk_width;
433 Cx0 = Cx0 + ((cblkno % prec->nb_codeblocks_width) << band->log2_cblk_width);
434 cblk->coord[0][0] = FFMAX(Cx0, prec->coord[0][0]);
435
436 /* Compute Cy0*/
437 Cy0 = (prec->coord[1][0] >> band->log2_cblk_height) << band->log2_cblk_height;
438 Cy0 = Cy0 + ((cblkno / prec->nb_codeblocks_width) << band->log2_cblk_height);
439 cblk->coord[1][0] = FFMAX(Cy0, prec->coord[1][0]);
440
441 /* Compute Cx1 */
442 cblk->coord[0][1] = FFMIN(Cx0 + (1 << band->log2_cblk_width),
443 prec->coord[0][1]);
444
445 /* Compute Cy1 */
446 cblk->coord[1][1] = FFMIN(Cy0 + (1 << band->log2_cblk_height),
447 prec->coord[1][1]);
448 cblk->zero = 0;
449 cblk->lblock = 3;
450 cblk->length = 0;
451 cblk->lengthinc = 0;
452 cblk->npasses = 0;
453 }
454 }
455 }
456 }
457 return 0;
458 }
459
460 void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
461 {
462 int reslevelno, bandno, precno;
463 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
464 Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
465
466 for (bandno = 0; bandno < reslevel->nbands; bandno++) {
467 Jpeg2000Band *band = reslevel->band + bandno;
468 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++) {
469 Jpeg2000Prec *prec = band->prec + precno;
470 av_freep(&prec->zerobits);
471 av_freep(&prec->cblkincl);
472 av_freep(&prec->cblk);
473 }
474
475 av_freep(&band->prec);
476 }
477 av_freep(&reslevel->band);
478 }
479
480 ff_dwt_destroy(&comp->dwt);
481 av_freep(&comp->reslevel);
482 av_freep(&comp->data);
483 }