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