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