]> git.sesse.net Git - ffmpeg/blob - libavcodec/j2k.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavcodec / j2k.c
1 /*
2  * JPEG2000 encoder and decoder common functions
3  * Copyright (c) 2007 Kamil Nowosad
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * JPEG2000 image encoder and decoder common functions
24  * @file
25  * @author Kamil Nowosad
26  */
27
28
29 #include "avcodec.h"
30 #include "j2k.h"
31
32 #define SHL(a, n) ((n)>=0 ? (a) << (n) : (a) >> -(n))
33
34 #if 0
35 void ff_j2k_printv(int *tab, int l)
36 {
37     int i;
38     for (i = 0; i < l; i++)
39         printf("%.3d ", tab[i]);
40     printf("\n");
41 }
42
43 void ff_j2k_printu(uint8_t *tab, int l)
44 {
45     int i;
46     for (i = 0; i < l; i++)
47         printf("%.3hd ", tab[i]);
48     printf("\n");
49 }
50 #endif
51
52 /* tag tree routines */
53
54 /** allocate the memory for tag tree */
55
56 static int tag_tree_size(int w, int h)
57 {
58     int res = 0;
59     while (w > 1 || h > 1){
60         res += w * h;
61         w = (w+1) >> 1;
62         h = (h+1) >> 1;
63     }
64     return res + 1;
65 }
66
67 J2kTgtNode *ff_j2k_tag_tree_init(int w, int h)
68 {
69     int pw = w, ph = h;
70     J2kTgtNode *res, *t, *t2;
71
72     t = res = av_mallocz(tag_tree_size(w, h)*sizeof(J2kTgtNode));
73
74     if (res == NULL)
75         return NULL;
76
77     while (w > 1 || h > 1){
78         int i, j;
79         pw = w;
80         ph = h;
81
82         w = (w+1) >> 1;
83         h = (h+1) >> 1;
84         t2 = t + pw*ph;
85
86         for (i = 0; i < ph; i++)
87             for (j = 0; j < pw; j++){
88                 t[i*pw + j].parent = &t2[(i>>1)*w + (j>>1)];
89             }
90         t = t2;
91     }
92     t[0].parent = NULL;
93     return res;
94 }
95
96 static void tag_tree_zero(J2kTgtNode *t, int w, int h)
97 {
98     int i, siz = tag_tree_size(w, h);
99
100     for (i = 0; i < siz; i++){
101         t[i].val = 0;
102         t[i].vis = 0;
103     }
104 }
105
106 uint8_t ff_j2k_nbctxno_lut[256][4];
107
108 static int getnbctxno(int flag, int bandno, int vert_causal_ctx_csty_symbol)
109 {
110     int h, v, d;
111
112     h = ((flag & J2K_T1_SIG_E) ? 1:0)+
113         ((flag & J2K_T1_SIG_W) ? 1:0);
114     v = ((flag & J2K_T1_SIG_N) ? 1:0);
115     if (!vert_causal_ctx_csty_symbol)
116          v = v + ((flag & J2K_T1_SIG_S) ? 1:0);
117     d = ((flag & J2K_T1_SIG_NE) ? 1:0)+
118         ((flag & J2K_T1_SIG_NW) ? 1:0);
119     if (!vert_causal_ctx_csty_symbol)
120         d = d + ((flag & J2K_T1_SIG_SE) ? 1:0)+
121                 ((flag & J2K_T1_SIG_SW) ? 1:0);
122     if (bandno < 3){
123             if (bandno == 1)
124                 FFSWAP(int, h, v);
125             if (h == 2) return 8;
126             if (h == 1){
127                 if (v >= 1) return 7;
128                 if (d >= 1) return 6;
129                 return 5;
130             }
131             if (v == 2) return 4;
132             if (v == 1) return 3;
133             if (d >= 2) return 2;
134             if (d == 1) return 1;
135             return 0;
136     } else{
137             if (d >= 3) return 8;
138             if (d == 2){
139                 if (h+v >= 1) return 7;
140                 return 6;
141             }
142             if (d == 1){
143                 if (h+v >= 2) return 5;
144                 if (h+v == 1) return 4;
145                 return 3;
146             }
147             if (h+v >= 2) return 2;
148             if (h+v == 1) return 1;
149             return 0;
150     }
151     assert(0);
152 }
153
154 uint8_t ff_j2k_sgnctxno_lut[16][16], ff_j2k_xorbit_lut[16][16];
155
156 static int getsgnctxno(int flag, uint8_t *xorbit)
157 {
158     int vcontrib, hcontrib;
159     static const int contribtab[3][3] = {{0, -1, 1}, {-1, -1, 0}, {1, 0, 1}};
160     static const int ctxlbltab[3][3] = {{13, 12, 11}, {10, 9, 10}, {11, 12, 13}};
161     static const int xorbittab[3][3] = {{1, 1, 1,}, {1, 0, 0}, {0, 0, 0}};
162
163     hcontrib = contribtab[flag & J2K_T1_SIG_E ? flag & J2K_T1_SGN_E ? 1:2:0]
164                          [flag & J2K_T1_SIG_W ? flag & J2K_T1_SGN_W ? 1:2:0]+1;
165     vcontrib = contribtab[flag & J2K_T1_SIG_S ? flag & J2K_T1_SGN_S ? 1:2:0]
166                          [flag & J2K_T1_SIG_N ? flag & J2K_T1_SGN_N ? 1:2:0]+1;
167     *xorbit = xorbittab[hcontrib][vcontrib];
168     return ctxlbltab[hcontrib][vcontrib];
169 }
170
171 void ff_j2k_init_tier1_luts(void)
172 {
173     int i, j;
174     for (i = 0; i < 256; i++)
175         for (j = 0; j < 4; j++)
176             ff_j2k_nbctxno_lut[i][j] = getnbctxno(i, j, 0);
177     for (i = 0; i < 16; i++)
178         for (j = 0; j < 16; j++)
179             ff_j2k_sgnctxno_lut[i][j] = getsgnctxno(i + (j << 8), &ff_j2k_xorbit_lut[i][j]);
180 }
181
182 void ff_j2k_set_significant(J2kT1Context *t1, int x, int y, int negative)
183 {
184     x++; y++;
185     t1->flags[y][x] |= J2K_T1_SIG;
186     if (negative){
187         t1->flags[y][x+1] |= J2K_T1_SIG_W | J2K_T1_SGN_W;
188         t1->flags[y][x-1] |= J2K_T1_SIG_E | J2K_T1_SGN_E;
189         t1->flags[y+1][x] |= J2K_T1_SIG_N | J2K_T1_SGN_N;
190         t1->flags[y-1][x] |= J2K_T1_SIG_S | J2K_T1_SGN_S;
191     } else{
192         t1->flags[y][x+1] |= J2K_T1_SIG_W;
193         t1->flags[y][x-1] |= J2K_T1_SIG_E;
194         t1->flags[y+1][x] |= J2K_T1_SIG_N;
195         t1->flags[y-1][x] |= J2K_T1_SIG_S;
196     }
197     t1->flags[y+1][x+1] |= J2K_T1_SIG_NW;
198     t1->flags[y+1][x-1] |= J2K_T1_SIG_NE;
199     t1->flags[y-1][x+1] |= J2K_T1_SIG_SW;
200     t1->flags[y-1][x-1] |= J2K_T1_SIG_SE;
201 }
202
203 int ff_j2k_init_component(J2kComponent *comp, J2kCodingStyle *codsty, J2kQuantStyle *qntsty, int cbps, int dx, int dy)
204 {
205     int reslevelno, bandno, gbandno = 0, ret, i, j, csize = 1;
206
207     if (ret=ff_j2k_dwt_init(&comp->dwt, comp->coord, codsty->nreslevels-1, codsty->transform))
208         return ret;
209     for (i = 0; i < 2; i++)
210         csize *= comp->coord[i][1] - comp->coord[i][0];
211
212     comp->data = av_malloc(csize * sizeof(int));
213     if (!comp->data)
214         return AVERROR(ENOMEM);
215     comp->reslevel = av_malloc(codsty->nreslevels * sizeof(J2kResLevel));
216
217     if (!comp->reslevel)
218         return AVERROR(ENOMEM);
219     for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
220         int declvl = codsty->nreslevels - reslevelno;
221         J2kResLevel *reslevel = comp->reslevel + reslevelno;
222
223         for (i = 0; i < 2; i++)
224             for (j = 0; j < 2; j++)
225                 reslevel->coord[i][j] =
226                     ff_j2k_ceildivpow2(comp->coord[i][j], declvl - 1);
227
228         if (reslevelno == 0)
229             reslevel->nbands = 1;
230         else
231             reslevel->nbands = 3;
232
233         if (reslevel->coord[0][1] == reslevel->coord[0][0])
234             reslevel->num_precincts_x = 0;
235         else
236             reslevel->num_precincts_x = ff_j2k_ceildivpow2(reslevel->coord[0][1], codsty->log2_prec_width)
237                                         - (reslevel->coord[0][0] >> codsty->log2_prec_width);
238
239         if (reslevel->coord[1][1] == reslevel->coord[1][0])
240             reslevel->num_precincts_y = 0;
241         else
242             reslevel->num_precincts_y = ff_j2k_ceildivpow2(reslevel->coord[1][1], codsty->log2_prec_height)
243                                         - (reslevel->coord[1][0] >> codsty->log2_prec_height);
244
245         reslevel->band = av_malloc(reslevel->nbands * sizeof(J2kBand));
246         if (!reslevel->band)
247             return AVERROR(ENOMEM);
248         for (bandno = 0; bandno < reslevel->nbands; bandno++, gbandno++){
249             J2kBand *band = reslevel->band + bandno;
250             int cblkno, precx, precy, precno;
251             int x0, y0, x1, y1;
252             int xi0, yi0, xi1, yi1;
253             int cblkperprecw, cblkperprech;
254
255             if (qntsty->quantsty != J2K_QSTY_NONE){
256                 const static uint8_t lut_gain[2][4] = {{0, 0, 0, 0}, {0, 1, 1, 2}};
257                 int numbps;
258
259                 numbps = cbps + lut_gain[codsty->transform][bandno + reslevelno>0];
260                 band->stepsize = SHL(2048 + qntsty->mant[gbandno], 2 + numbps - qntsty->expn[gbandno]);
261             } else
262                 band->stepsize = 1 << 13;
263
264             if (reslevelno == 0){  // the same everywhere
265                 band->codeblock_width = 1 << FFMIN(codsty->log2_cblk_width, codsty->log2_prec_width-1);
266                 band->codeblock_height = 1 << FFMIN(codsty->log2_cblk_height, codsty->log2_prec_height-1);
267                 for (i = 0; i < 2; i++)
268                     for (j = 0; j < 2; j++)
269                         band->coord[i][j] = ff_j2k_ceildivpow2(comp->coord[i][j], declvl-1);
270             } else{
271                 band->codeblock_width = 1 << FFMIN(codsty->log2_cblk_width, codsty->log2_prec_width);
272                 band->codeblock_height = 1 << FFMIN(codsty->log2_cblk_height, codsty->log2_prec_height);
273
274                 for (i = 0; i < 2; i++)
275                     for (j = 0; j < 2; j++)
276                         band->coord[i][j] = ff_j2k_ceildivpow2(comp->coord[i][j] - (((bandno+1>>i)&1) << declvl-1), declvl);
277             }
278             band->cblknx = ff_j2k_ceildiv(band->coord[0][1], band->codeblock_width)  - band->coord[0][0] / band->codeblock_width;
279             band->cblkny = ff_j2k_ceildiv(band->coord[1][1], band->codeblock_height) - band->coord[1][0] / band->codeblock_height;
280
281             for (j = 0; j < 2; j++)
282                 band->coord[0][j] = ff_j2k_ceildiv(band->coord[0][j], dx);
283             for (j = 0; j < 2; j++)
284                 band->coord[1][j] = ff_j2k_ceildiv(band->coord[1][j], dy);
285
286             band->cblknx = ff_j2k_ceildiv(band->cblknx, dx);
287             band->cblkny = ff_j2k_ceildiv(band->cblkny, dy);
288
289             band->cblk = av_malloc(band->cblknx * band->cblkny * sizeof(J2kCblk));
290             if (!band->cblk)
291                 return AVERROR(ENOMEM);
292             band->prec = av_malloc(reslevel->num_precincts_x * reslevel->num_precincts_y * sizeof(J2kPrec));
293             if (!band->prec)
294                 return AVERROR(ENOMEM);
295
296             for (cblkno = 0; cblkno < band->cblknx * band->cblkny; cblkno++){
297                 J2kCblk *cblk = band->cblk + cblkno;
298                 cblk->zero = 0;
299                 cblk->lblock = 3;
300                 cblk->length = 0;
301                 cblk->lengthinc = 0;
302                 cblk->npasses = 0;
303             }
304
305             y0 = band->coord[1][0];
306             y1 = ((band->coord[1][0] + (1<<codsty->log2_prec_height)) & ~((1<<codsty->log2_prec_height)-1)) - y0;
307             yi0 = 0;
308             yi1 = ff_j2k_ceildivpow2(y1 - y0, codsty->log2_cblk_height) << codsty->log2_cblk_height;
309             yi1 = FFMIN(yi1, band->cblkny);
310             cblkperprech = 1<<(codsty->log2_prec_height - codsty->log2_cblk_height);
311             for (precy = 0, precno = 0; precy < reslevel->num_precincts_y; precy++){
312                 for (precx = 0; precx < reslevel->num_precincts_x; precx++, precno++){
313                     band->prec[precno].yi0 = yi0;
314                     band->prec[precno].yi1 = yi1;
315                 }
316                 yi1 += cblkperprech;
317                 yi0 = yi1 - cblkperprech;
318                 yi1 = FFMIN(yi1, band->cblkny);
319             }
320             x0 = band->coord[0][0];
321             x1 = ((band->coord[0][0] + (1<<codsty->log2_prec_width)) & ~((1<<codsty->log2_prec_width)-1)) - x0;
322             xi0 = 0;
323             xi1 = ff_j2k_ceildivpow2(x1 - x0, codsty->log2_cblk_width) << codsty->log2_cblk_width;
324             xi1 = FFMIN(xi1, band->cblknx);
325
326             cblkperprecw = 1<<(codsty->log2_prec_width - codsty->log2_cblk_width);
327             for (precx = 0, precno = 0; precx < reslevel->num_precincts_x; precx++){
328                 for (precy = 0; precy < reslevel->num_precincts_y; precy++, precno = 0){
329                     J2kPrec *prec = band->prec + precno;
330                     prec->xi0 = xi0;
331                     prec->xi1 = xi1;
332                     prec->cblkincl = ff_j2k_tag_tree_init(prec->xi1 - prec->xi0,
333                                                           prec->yi1 - prec->yi0);
334                     prec->zerobits = ff_j2k_tag_tree_init(prec->xi1 - prec->xi0,
335                                                           prec->yi1 - prec->yi0);
336                     if (!prec->cblkincl || !prec->zerobits)
337                         return AVERROR(ENOMEM);
338
339                 }
340                 xi1 += cblkperprecw;
341                 xi0 = xi1 - cblkperprecw;
342                 xi1 = FFMIN(xi1, band->cblknx);
343             }
344         }
345     }
346     return 0;
347 }
348
349 void ff_j2k_reinit(J2kComponent *comp, J2kCodingStyle *codsty)
350 {
351     int reslevelno, bandno, cblkno, precno;
352     for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
353         J2kResLevel *rlevel = comp->reslevel + reslevelno;
354         for (bandno = 0; bandno < rlevel->nbands; bandno++){
355             J2kBand *band = rlevel->band + bandno;
356             for(precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++){
357                 J2kPrec *prec = band->prec + precno;
358                 tag_tree_zero(prec->zerobits, prec->xi1 - prec->xi0, prec->yi1 - prec->yi0);
359                 tag_tree_zero(prec->cblkincl, prec->xi1 - prec->xi0, prec->yi1 - prec->yi0);
360             }
361             for (cblkno = 0; cblkno < band->cblknx * band->cblkny; cblkno++){
362                 J2kCblk *cblk = band->cblk + cblkno;
363                 cblk->length = 0;
364                 cblk->lblock = 3;
365             }
366         }
367     }
368 }
369
370 void ff_j2k_cleanup(J2kComponent *comp, J2kCodingStyle *codsty)
371 {
372     int reslevelno, bandno, precno;
373     for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
374         J2kResLevel *reslevel = comp->reslevel + reslevelno;
375
376         for (bandno = 0; bandno < reslevel->nbands ; bandno++){
377             J2kBand *band = reslevel->band + bandno;
378                 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
379                     J2kPrec *prec = band->prec + precno;
380                     av_freep(&prec->zerobits);
381                     av_freep(&prec->cblkincl);
382                 }
383                 av_freep(&band->cblk);
384                 av_freep(&band->prec);
385             }
386         av_freep(&reslevel->band);
387     }
388
389     ff_j2k_dwt_destroy(&comp->dwt);
390     av_freep(&comp->reslevel);
391     av_freep(&comp->data);
392 }