2011-05-23 21:13:34 +00:00
|
|
|
/*
|
2013-05-26 17:18:31 +00:00
|
|
|
* JPEG 2000 encoder and decoder common functions
|
2011-05-23 21:13:34 +00:00
|
|
|
* Copyright (c) 2007 Kamil Nowosad
|
2013-05-26 15:45:15 +00:00
|
|
|
* Copyright (c) 2013 Nicolas Bertrand <nicoinattendu@gmail.com>
|
2011-05-23 21:13:34 +00:00
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2013-05-26 17:18:31 +00:00
|
|
|
* JPEG 2000 image encoder and decoder common functions
|
2011-05-23 21:13:34 +00:00
|
|
|
* @file
|
|
|
|
* @author Kamil Nowosad
|
|
|
|
*/
|
|
|
|
|
2013-05-26 22:16:07 +00:00
|
|
|
#include "libavutil/avassert.h"
|
2013-05-26 17:09:05 +00:00
|
|
|
#include "libavutil/common.h"
|
|
|
|
#include "libavutil/mem.h"
|
2011-05-23 21:13:34 +00:00
|
|
|
#include "avcodec.h"
|
|
|
|
#include "j2k.h"
|
|
|
|
|
2013-05-26 17:00:56 +00:00
|
|
|
#define SHL(a, n) ((n) >= 0 ? (a) << (n) : (a) >> -(n))
|
2011-05-23 21:13:34 +00:00
|
|
|
|
|
|
|
/* tag tree routines */
|
|
|
|
|
2013-05-26 17:00:56 +00:00
|
|
|
/* allocate the memory for tag tree */
|
2011-05-23 21:13:34 +00:00
|
|
|
static int tag_tree_size(int w, int h)
|
|
|
|
{
|
|
|
|
int res = 0;
|
2013-05-26 17:00:56 +00:00
|
|
|
while (w > 1 || h > 1) {
|
2011-05-23 21:13:34 +00:00
|
|
|
res += w * h;
|
2013-05-26 17:00:56 +00:00
|
|
|
w = (w + 1) >> 1;
|
|
|
|
h = (h + 1) >> 1;
|
2011-05-23 21:13:34 +00:00
|
|
|
}
|
|
|
|
return res + 1;
|
|
|
|
}
|
|
|
|
|
2013-05-21 19:19:05 +00:00
|
|
|
Jpeg2000TgtNode *ff_j2k_tag_tree_init(int w, int h)
|
2011-05-23 21:13:34 +00:00
|
|
|
{
|
|
|
|
int pw = w, ph = h;
|
2013-05-21 19:19:05 +00:00
|
|
|
Jpeg2000TgtNode *res, *t, *t2;
|
2013-05-26 17:18:31 +00:00
|
|
|
int32_t tt_size;
|
2011-05-23 21:13:34 +00:00
|
|
|
|
2013-05-26 17:18:31 +00:00
|
|
|
tt_size = tag_tree_size(w, h);
|
2011-05-23 21:13:34 +00:00
|
|
|
|
2013-05-26 17:18:58 +00:00
|
|
|
t = res = av_mallocz_array(tt_size, sizeof(*t));
|
2013-05-26 17:18:31 +00:00
|
|
|
if (!res)
|
2011-05-23 21:13:34 +00:00
|
|
|
return NULL;
|
|
|
|
|
2013-05-26 17:00:56 +00:00
|
|
|
while (w > 1 || h > 1) {
|
2011-05-23 21:13:34 +00:00
|
|
|
int i, j;
|
|
|
|
pw = w;
|
|
|
|
ph = h;
|
|
|
|
|
2013-05-26 17:18:31 +00:00
|
|
|
w = (w + 1) >> 1;
|
|
|
|
h = (h + 1) >> 1;
|
2013-05-26 17:00:56 +00:00
|
|
|
t2 = t + pw * ph;
|
2011-05-23 21:13:34 +00:00
|
|
|
|
|
|
|
for (i = 0; i < ph; i++)
|
2013-05-26 17:18:31 +00:00
|
|
|
for (j = 0; j < pw; j++)
|
2013-05-26 17:00:56 +00:00
|
|
|
t[i * pw + j].parent = &t2[(i >> 1) * w + (j >> 1)];
|
2013-05-26 17:18:31 +00:00
|
|
|
|
2011-05-23 21:13:34 +00:00
|
|
|
t = t2;
|
|
|
|
}
|
|
|
|
t[0].parent = NULL;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2013-05-21 19:19:05 +00:00
|
|
|
static void tag_tree_zero(Jpeg2000TgtNode *t, int w, int h)
|
2011-05-23 21:13:34 +00:00
|
|
|
{
|
|
|
|
int i, siz = tag_tree_size(w, h);
|
|
|
|
|
2013-05-26 17:00:56 +00:00
|
|
|
for (i = 0; i < siz; i++) {
|
2011-05-23 21:13:34 +00:00
|
|
|
t[i].val = 0;
|
|
|
|
t[i].vis = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-26 15:33:08 +00:00
|
|
|
static int getsigctxno(int flag, int bandno)
|
2011-05-23 21:13:34 +00:00
|
|
|
{
|
|
|
|
int h, v, d;
|
|
|
|
|
2013-05-26 15:39:02 +00:00
|
|
|
h = ((flag & JPEG2000_T1_SIG_E) ? 1 : 0) +
|
|
|
|
((flag & JPEG2000_T1_SIG_W) ? 1 : 0);
|
|
|
|
v = ((flag & JPEG2000_T1_SIG_N) ? 1 : 0) +
|
|
|
|
((flag & JPEG2000_T1_SIG_S) ? 1 : 0);
|
|
|
|
d = ((flag & JPEG2000_T1_SIG_NE) ? 1 : 0) +
|
|
|
|
((flag & JPEG2000_T1_SIG_NW) ? 1 : 0) +
|
|
|
|
((flag & JPEG2000_T1_SIG_SE) ? 1 : 0) +
|
|
|
|
((flag & JPEG2000_T1_SIG_SW) ? 1 : 0);
|
|
|
|
|
2013-05-26 17:00:56 +00:00
|
|
|
if (bandno < 3) {
|
2013-05-26 15:39:02 +00:00
|
|
|
if (bandno == 1)
|
|
|
|
FFSWAP(int, h, v);
|
|
|
|
if (h == 2) return 8;
|
2013-05-26 17:00:56 +00:00
|
|
|
if (h == 1) {
|
2013-05-26 15:39:02 +00:00
|
|
|
if (v >= 1) return 7;
|
|
|
|
if (d >= 1) return 6;
|
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
if (v == 2) return 4;
|
|
|
|
if (v == 1) return 3;
|
|
|
|
if (d >= 2) return 2;
|
|
|
|
if (d == 1) return 1;
|
2013-05-26 20:40:25 +00:00
|
|
|
} else {
|
2013-05-26 15:39:02 +00:00
|
|
|
if (d >= 3) return 8;
|
2013-05-26 17:00:56 +00:00
|
|
|
if (d == 2) {
|
2013-05-26 15:39:02 +00:00
|
|
|
if (h+v >= 1) return 7;
|
|
|
|
return 6;
|
|
|
|
}
|
2013-05-26 17:00:56 +00:00
|
|
|
if (d == 1) {
|
2013-05-26 15:39:02 +00:00
|
|
|
if (h+v >= 2) return 5;
|
|
|
|
if (h+v == 1) return 4;
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
if (h+v >= 2) return 2;
|
|
|
|
if (h+v == 1) return 1;
|
2011-05-23 21:13:34 +00:00
|
|
|
}
|
2013-05-26 15:39:02 +00:00
|
|
|
return 0;
|
2011-05-23 21:13:34 +00:00
|
|
|
}
|
|
|
|
|
2013-05-26 17:18:31 +00:00
|
|
|
|
|
|
|
static const int contribtab[3][3] = { { 0, -1, 1 }, { -1, -1, 0 }, { 1, 0, 1 } };
|
|
|
|
static const int ctxlbltab[3][3] = { { 13, 12, 11 }, { 10, 9, 10 }, { 11, 12, 13 } };
|
|
|
|
static const int xorbittab[3][3] = { { 1, 1, 1 }, { 1, 0, 0 }, { 0, 0, 0 } };
|
|
|
|
|
2011-05-23 21:13:34 +00:00
|
|
|
static int getsgnctxno(int flag, uint8_t *xorbit)
|
|
|
|
{
|
|
|
|
int vcontrib, hcontrib;
|
2013-05-26 17:18:31 +00:00
|
|
|
|
|
|
|
hcontrib = contribtab[flag & JPEG2000_T1_SIG_E ? flag & JPEG2000_T1_SGN_E ? 1 : 2 : 0]
|
|
|
|
[flag & JPEG2000_T1_SIG_W ? flag & JPEG2000_T1_SGN_W ? 1 : 2 : 0] + 1;
|
|
|
|
vcontrib = contribtab[flag & JPEG2000_T1_SIG_S ? flag & JPEG2000_T1_SGN_S ? 1 : 2 : 0]
|
|
|
|
[flag & JPEG2000_T1_SIG_N ? flag & JPEG2000_T1_SGN_N ? 1 : 2 : 0] + 1;
|
2011-05-23 21:13:34 +00:00
|
|
|
*xorbit = xorbittab[hcontrib][vcontrib];
|
2013-05-26 17:18:31 +00:00
|
|
|
|
2011-05-23 21:13:34 +00:00
|
|
|
return ctxlbltab[hcontrib][vcontrib];
|
|
|
|
}
|
|
|
|
|
2013-05-26 17:00:56 +00:00
|
|
|
void ff_j2k_set_significant(Jpeg2000T1Context *t1, int x, int y,
|
|
|
|
int negative)
|
2011-05-23 21:13:34 +00:00
|
|
|
{
|
2013-05-26 17:18:31 +00:00
|
|
|
x++;
|
2013-05-26 17:00:56 +00:00
|
|
|
y++;
|
2013-05-21 19:27:56 +00:00
|
|
|
t1->flags[y][x] |= JPEG2000_T1_SIG;
|
2013-05-26 17:00:56 +00:00
|
|
|
if (negative) {
|
|
|
|
t1->flags[y][x + 1] |= JPEG2000_T1_SIG_W | JPEG2000_T1_SGN_W;
|
|
|
|
t1->flags[y][x - 1] |= JPEG2000_T1_SIG_E | JPEG2000_T1_SGN_E;
|
|
|
|
t1->flags[y + 1][x] |= JPEG2000_T1_SIG_N | JPEG2000_T1_SGN_N;
|
|
|
|
t1->flags[y - 1][x] |= JPEG2000_T1_SIG_S | JPEG2000_T1_SGN_S;
|
|
|
|
} else {
|
|
|
|
t1->flags[y][x + 1] |= JPEG2000_T1_SIG_W;
|
|
|
|
t1->flags[y][x - 1] |= JPEG2000_T1_SIG_E;
|
|
|
|
t1->flags[y + 1][x] |= JPEG2000_T1_SIG_N;
|
|
|
|
t1->flags[y - 1][x] |= JPEG2000_T1_SIG_S;
|
2011-05-23 21:13:34 +00:00
|
|
|
}
|
2013-05-26 17:00:56 +00:00
|
|
|
t1->flags[y + 1][x + 1] |= JPEG2000_T1_SIG_NW;
|
|
|
|
t1->flags[y + 1][x - 1] |= JPEG2000_T1_SIG_NE;
|
|
|
|
t1->flags[y - 1][x + 1] |= JPEG2000_T1_SIG_SW;
|
|
|
|
t1->flags[y - 1][x - 1] |= JPEG2000_T1_SIG_SE;
|
2011-05-23 21:13:34 +00:00
|
|
|
}
|
|
|
|
|
2013-05-26 20:40:25 +00:00
|
|
|
int ff_j2k_init_component(Jpeg2000Component *comp,
|
|
|
|
Jpeg2000CodingStyle *codsty,
|
|
|
|
Jpeg2000QuantStyle *qntsty,
|
|
|
|
int cbps, int dx, int dy)
|
2011-05-23 21:13:34 +00:00
|
|
|
{
|
2013-05-26 19:50:17 +00:00
|
|
|
uint8_t log2_band_prec_width, log2_band_prec_height;
|
2011-05-23 21:13:34 +00:00
|
|
|
int reslevelno, bandno, gbandno = 0, ret, i, j, csize = 1;
|
|
|
|
|
|
|
|
if (ret=ff_j2k_dwt_init(&comp->dwt, comp->coord, codsty->nreslevels-1, codsty->transform))
|
|
|
|
return ret;
|
|
|
|
for (i = 0; i < 2; i++)
|
|
|
|
csize *= comp->coord[i][1] - comp->coord[i][0];
|
|
|
|
|
2013-05-26 20:40:25 +00:00
|
|
|
comp->data = av_malloc_array(csize, sizeof(*comp->data));
|
2011-05-23 21:13:34 +00:00
|
|
|
if (!comp->data)
|
|
|
|
return AVERROR(ENOMEM);
|
2013-05-26 17:18:58 +00:00
|
|
|
comp->reslevel = av_malloc_array(codsty->nreslevels, sizeof(*comp->reslevel));
|
2011-05-23 21:13:34 +00:00
|
|
|
if (!comp->reslevel)
|
|
|
|
return AVERROR(ENOMEM);
|
2013-05-26 20:40:25 +00:00
|
|
|
/* LOOP on resolution levels */
|
2013-05-26 17:00:56 +00:00
|
|
|
for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
|
2013-05-26 20:40:25 +00:00
|
|
|
int declvl = codsty->nreslevels - reslevelno; // N_L -r see ISO/IEC 15444-1:2002 B.5
|
2013-05-21 19:19:05 +00:00
|
|
|
Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
|
2011-05-23 21:13:34 +00:00
|
|
|
|
2013-05-26 20:40:25 +00:00
|
|
|
/* Compute borders for each resolution level.
|
|
|
|
* Computation of trx_0, trx_1, try_0 and try_1.
|
|
|
|
* see ISO/IEC 15444-1:2002 eq. B.5 and B-14 */
|
2011-05-23 21:13:34 +00:00
|
|
|
for (i = 0; i < 2; i++)
|
|
|
|
for (j = 0; j < 2; j++)
|
|
|
|
reslevel->coord[i][j] =
|
2013-05-26 11:54:32 +00:00
|
|
|
ff_jpeg2000_ceildivpow2(comp->coord[i][j], declvl - 1);
|
2013-05-26 19:50:17 +00:00
|
|
|
// update precincts size: 2^n value
|
|
|
|
reslevel->log2_prec_width = codsty->log2_prec_widths[reslevelno];
|
|
|
|
reslevel->log2_prec_height = codsty->log2_prec_heights[reslevelno];
|
2011-05-23 21:13:34 +00:00
|
|
|
|
2013-05-26 20:40:25 +00:00
|
|
|
/* Number of bands for each resolution level */
|
2011-05-23 21:13:34 +00:00
|
|
|
if (reslevelno == 0)
|
|
|
|
reslevel->nbands = 1;
|
|
|
|
else
|
|
|
|
reslevel->nbands = 3;
|
|
|
|
|
2013-05-26 20:40:25 +00:00
|
|
|
/* Number of precincts wich span the tile for resolution level reslevelno
|
|
|
|
* see B.6 in ISO/IEC 15444-1:2002 eq. B-16
|
|
|
|
* num_precincts_x = |- trx_1 / 2 ^ log2_prec_width) -| - (trx_0 / 2 ^ log2_prec_width)
|
|
|
|
* num_precincts_y = |- try_1 / 2 ^ log2_prec_width) -| - (try_0 / 2 ^ log2_prec_width)
|
|
|
|
* for Dcinema profiles in JPEG 2000
|
|
|
|
* num_precincts_x = |- trx_1 / 2 ^ log2_prec_width) -|
|
|
|
|
* num_precincts_y = |- try_1 / 2 ^ log2_prec_width) -| */
|
2011-05-23 21:13:34 +00:00
|
|
|
if (reslevel->coord[0][1] == reslevel->coord[0][0])
|
|
|
|
reslevel->num_precincts_x = 0;
|
|
|
|
else
|
2013-05-26 20:40:25 +00:00
|
|
|
reslevel->num_precincts_x =
|
|
|
|
ff_jpeg2000_ceildivpow2(reslevel->coord[0][1],
|
|
|
|
reslevel->log2_prec_width) -
|
|
|
|
(reslevel->coord[0][0] >> reslevel->log2_prec_width);
|
2011-05-23 21:13:34 +00:00
|
|
|
|
|
|
|
if (reslevel->coord[1][1] == reslevel->coord[1][0])
|
|
|
|
reslevel->num_precincts_y = 0;
|
|
|
|
else
|
2013-05-26 20:40:25 +00:00
|
|
|
reslevel->num_precincts_y =
|
|
|
|
ff_jpeg2000_ceildivpow2(reslevel->coord[1][1],
|
|
|
|
reslevel->log2_prec_height) -
|
|
|
|
(reslevel->coord[1][0] >> reslevel->log2_prec_height);
|
2011-05-23 21:13:34 +00:00
|
|
|
|
2013-05-26 17:18:58 +00:00
|
|
|
reslevel->band = av_malloc_array(reslevel->nbands, sizeof(*reslevel->band));
|
2011-05-23 21:13:34 +00:00
|
|
|
if (!reslevel->band)
|
|
|
|
return AVERROR(ENOMEM);
|
2013-05-26 17:18:58 +00:00
|
|
|
|
2013-05-26 17:00:56 +00:00
|
|
|
for (bandno = 0; bandno < reslevel->nbands; bandno++, gbandno++) {
|
2013-05-21 19:19:05 +00:00
|
|
|
Jpeg2000Band *band = reslevel->band + bandno;
|
2013-05-26 22:16:07 +00:00
|
|
|
int precx, precy;
|
2011-05-23 21:13:34 +00:00
|
|
|
int x0, y0, x1, y1;
|
|
|
|
int xi0, yi0, xi1, yi1;
|
|
|
|
int cblkperprecw, cblkperprech;
|
2013-05-26 22:16:07 +00:00
|
|
|
int cblkno, precno;
|
|
|
|
int nb_precincts;
|
2011-05-23 21:13:34 +00:00
|
|
|
|
2013-05-26 17:00:56 +00:00
|
|
|
if (qntsty->quantsty != JPEG2000_QSTY_NONE) {
|
2012-09-20 00:13:52 +00:00
|
|
|
static const uint8_t lut_gain[2][4] = {{0, 0, 0, 0}, {0, 1, 1, 2}};
|
2011-05-23 21:13:34 +00:00
|
|
|
int numbps;
|
|
|
|
|
|
|
|
numbps = cbps + lut_gain[codsty->transform][bandno + reslevelno>0];
|
|
|
|
band->stepsize = SHL(2048 + qntsty->mant[gbandno], 2 + numbps - qntsty->expn[gbandno]);
|
|
|
|
} else
|
|
|
|
band->stepsize = 1 << 13;
|
|
|
|
|
2013-05-26 19:50:17 +00:00
|
|
|
if (reslevelno == 0) {
|
|
|
|
/* for reslevelno = 0, only one band, x0_b = y0_b = 0 */
|
2011-05-23 21:13:34 +00:00
|
|
|
for (i = 0; i < 2; i++)
|
|
|
|
for (j = 0; j < 2; j++)
|
2013-05-26 19:50:17 +00:00
|
|
|
band->coord[i][j] =
|
2013-05-27 12:31:05 +00:00
|
|
|
ff_jpeg2000_ceildivpow2(comp->coord[i][j] - comp->coord[i][0],
|
2013-05-26 19:50:17 +00:00
|
|
|
declvl - 1);
|
|
|
|
log2_band_prec_width = reslevel->log2_prec_width;
|
|
|
|
log2_band_prec_height = reslevel->log2_prec_height;
|
|
|
|
/* see ISO/IEC 15444-1:2002 eq. B-17 and eq. B-15 */
|
|
|
|
band->log2_cblk_width = FFMIN(codsty->log2_cblk_width,
|
|
|
|
reslevel->log2_prec_width);
|
|
|
|
band->log2_cblk_height = FFMIN(codsty->log2_cblk_height,
|
|
|
|
reslevel->log2_prec_height);
|
2013-05-26 20:40:25 +00:00
|
|
|
} else {
|
2013-05-26 19:50:17 +00:00
|
|
|
/* 3 bands x0_b = 1 y0_b = 0; x0_b = 0 y0_b = 1; x0_b = y0_b = 1 */
|
|
|
|
/* x0_b and y0_b are computed with ((bandno + 1 >> i) & 1) */
|
2011-05-23 21:13:34 +00:00
|
|
|
for (i = 0; i < 2; i++)
|
|
|
|
for (j = 0; j < 2; j++)
|
2013-05-26 19:50:17 +00:00
|
|
|
/* Formula example for tbx_0 = ceildiv((tcx_0 - 2 ^ (declvl - 1) * x0_b) / declvl) */
|
|
|
|
band->coord[i][j] =
|
2013-05-27 12:31:05 +00:00
|
|
|
ff_jpeg2000_ceildivpow2(comp->coord[i][j] - comp->coord[i][0] -
|
2013-05-26 19:50:17 +00:00
|
|
|
(((bandno + 1 >> i) & 1) << declvl - 1),
|
|
|
|
declvl);
|
|
|
|
/* TODO: Manage case of 3 band offsets here or
|
|
|
|
* in coding/decoding function? */
|
|
|
|
|
|
|
|
/* see ISO/IEC 15444-1:2002 eq. B-17 and eq. B-15 */
|
|
|
|
band->log2_cblk_width = FFMIN(codsty->log2_cblk_width,
|
|
|
|
reslevel->log2_prec_width - 1);
|
|
|
|
band->log2_cblk_height = FFMIN(codsty->log2_cblk_height,
|
|
|
|
reslevel->log2_prec_height - 1);
|
|
|
|
|
|
|
|
log2_band_prec_width = reslevel->log2_prec_width - 1;
|
|
|
|
log2_band_prec_height = reslevel->log2_prec_height - 1;
|
2011-05-23 21:13:34 +00:00
|
|
|
}
|
2013-05-26 19:50:17 +00:00
|
|
|
band->cblknx = ff_jpeg2000_ceildivpow2(band->coord[0][1], band->log2_cblk_width) - (band->coord[0][0] >> band->log2_cblk_width);
|
|
|
|
band->cblkny = ff_jpeg2000_ceildivpow2(band->coord[1][1], band->log2_cblk_height) - (band->coord[1][0] >> band->log2_cblk_height);
|
2011-05-23 21:13:34 +00:00
|
|
|
|
|
|
|
for (j = 0; j < 2; j++)
|
2013-05-26 11:54:32 +00:00
|
|
|
band->coord[0][j] = ff_jpeg2000_ceildiv(band->coord[0][j], dx);
|
2011-05-23 21:13:34 +00:00
|
|
|
for (j = 0; j < 2; j++)
|
2013-05-26 11:54:32 +00:00
|
|
|
band->coord[1][j] = ff_jpeg2000_ceildiv(band->coord[1][j], dy);
|
2011-05-23 21:13:34 +00:00
|
|
|
|
2013-05-26 11:54:32 +00:00
|
|
|
band->cblknx = ff_jpeg2000_ceildiv(band->cblknx, dx);
|
|
|
|
band->cblkny = ff_jpeg2000_ceildiv(band->cblkny, dy);
|
2011-05-23 21:13:34 +00:00
|
|
|
|
2013-05-26 17:18:58 +00:00
|
|
|
band->cblk = av_malloc_array(band->cblknx *
|
|
|
|
band->cblkny,
|
|
|
|
sizeof(*band->cblk));
|
2011-05-23 21:13:34 +00:00
|
|
|
if (!band->cblk)
|
|
|
|
return AVERROR(ENOMEM);
|
2013-05-26 17:18:58 +00:00
|
|
|
band->prec = av_malloc_array(reslevel->num_precincts_x *
|
|
|
|
reslevel->num_precincts_y,
|
|
|
|
sizeof(*band->prec));
|
2011-05-23 21:13:34 +00:00
|
|
|
if (!band->prec)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2013-05-26 22:16:07 +00:00
|
|
|
nb_precincts = reslevel->num_precincts_x * reslevel->num_precincts_y;
|
2011-05-23 21:13:34 +00:00
|
|
|
|
|
|
|
y0 = band->coord[1][0];
|
2013-05-26 19:50:17 +00:00
|
|
|
y1 = ((band->coord[1][0] + (1<<reslevel->log2_prec_height)) & ~((1<<reslevel->log2_prec_height)-1)) - y0;
|
2011-05-23 21:13:34 +00:00
|
|
|
yi0 = 0;
|
2013-05-26 11:54:32 +00:00
|
|
|
yi1 = ff_jpeg2000_ceildivpow2(y1 - y0, codsty->log2_cblk_height) << codsty->log2_cblk_height;
|
2011-05-23 21:13:34 +00:00
|
|
|
yi1 = FFMIN(yi1, band->cblkny);
|
2013-05-26 19:50:17 +00:00
|
|
|
cblkperprech = 1<<(reslevel->log2_prec_height - codsty->log2_cblk_height);
|
2013-05-26 17:00:56 +00:00
|
|
|
for (precy = 0, precno = 0; precy < reslevel->num_precincts_y; precy++) {
|
|
|
|
for (precx = 0; precx < reslevel->num_precincts_x; precx++, precno++) {
|
2011-05-23 21:13:34 +00:00
|
|
|
band->prec[precno].yi0 = yi0;
|
|
|
|
band->prec[precno].yi1 = yi1;
|
|
|
|
}
|
|
|
|
yi1 += cblkperprech;
|
|
|
|
yi0 = yi1 - cblkperprech;
|
|
|
|
yi1 = FFMIN(yi1, band->cblkny);
|
|
|
|
}
|
|
|
|
x0 = band->coord[0][0];
|
2013-05-26 19:50:17 +00:00
|
|
|
x1 = ((band->coord[0][0] + (1<<reslevel->log2_prec_width)) & ~((1<<reslevel->log2_prec_width)-1)) - x0;
|
2011-05-23 21:13:34 +00:00
|
|
|
xi0 = 0;
|
2013-05-26 11:54:32 +00:00
|
|
|
xi1 = ff_jpeg2000_ceildivpow2(x1 - x0, codsty->log2_cblk_width) << codsty->log2_cblk_width;
|
2011-05-23 21:13:34 +00:00
|
|
|
xi1 = FFMIN(xi1, band->cblknx);
|
|
|
|
|
2013-05-26 19:50:17 +00:00
|
|
|
cblkperprecw = 1<<(reslevel->log2_prec_width - codsty->log2_cblk_width);
|
2013-05-26 17:00:56 +00:00
|
|
|
for (precx = 0, precno = 0; precx < reslevel->num_precincts_x; precx++) {
|
|
|
|
for (precy = 0; precy < reslevel->num_precincts_y; precy++, precno = 0) {
|
2013-05-21 19:19:05 +00:00
|
|
|
Jpeg2000Prec *prec = band->prec + precno;
|
2011-05-23 21:13:34 +00:00
|
|
|
prec->xi0 = xi0;
|
|
|
|
prec->xi1 = xi1;
|
|
|
|
}
|
|
|
|
xi1 += cblkperprecw;
|
|
|
|
xi0 = xi1 - cblkperprecw;
|
|
|
|
xi1 = FFMIN(xi1, band->cblknx);
|
|
|
|
}
|
2013-05-26 22:16:07 +00:00
|
|
|
for (precno = 0; precno < nb_precincts; precno++) {
|
|
|
|
Jpeg2000Prec *prec = band->prec + precno;
|
|
|
|
|
|
|
|
/* TODO: Explain formula for JPEG200 DCINEMA. */
|
|
|
|
/* TODO: Verify with previous count of codeblocks per band */
|
|
|
|
|
|
|
|
/* Compute P_x0 */
|
|
|
|
prec->coord[0][0] = (precno % reslevel->num_precincts_x) *
|
|
|
|
(1 << log2_band_prec_width);
|
|
|
|
prec->coord[0][0] = FFMAX(prec->coord[0][0], band->coord[0][0]);
|
|
|
|
|
|
|
|
/* Compute P_y0 */
|
|
|
|
prec->coord[1][0] = (precno / reslevel->num_precincts_x) *
|
|
|
|
(1 << log2_band_prec_height);
|
|
|
|
prec->coord[1][0] = FFMAX(prec->coord[1][0], band->coord[1][0]);
|
|
|
|
|
|
|
|
/* Compute P_x1 */
|
|
|
|
prec->coord[0][1] = prec->coord[0][0] +
|
|
|
|
(1 << log2_band_prec_width);
|
|
|
|
prec->coord[0][1] = FFMIN(prec->coord[0][1], band->coord[0][1]);
|
|
|
|
|
|
|
|
/* Compute P_y1 */
|
|
|
|
prec->coord[1][1] = prec->coord[1][0] +
|
|
|
|
(1 << log2_band_prec_height);
|
|
|
|
prec->coord[1][1] = FFMIN(prec->coord[1][1], band->coord[1][1]);
|
|
|
|
|
|
|
|
prec->nb_codeblocks_width =
|
|
|
|
ff_jpeg2000_ceildivpow2(prec->coord[0][1] -
|
|
|
|
prec->coord[0][0],
|
|
|
|
band->log2_cblk_width);
|
|
|
|
prec->nb_codeblocks_height =
|
|
|
|
ff_jpeg2000_ceildivpow2(prec->coord[1][1] -
|
|
|
|
prec->coord[1][0],
|
|
|
|
band->log2_cblk_height);
|
|
|
|
|
|
|
|
/* Tag trees initialization */
|
|
|
|
prec->cblkincl =
|
|
|
|
ff_j2k_tag_tree_init(prec->nb_codeblocks_width,
|
|
|
|
prec->nb_codeblocks_height);
|
|
|
|
if (!prec->cblkincl)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
prec->zerobits =
|
|
|
|
ff_j2k_tag_tree_init(prec->nb_codeblocks_width,
|
|
|
|
prec->nb_codeblocks_height);
|
|
|
|
if (!prec->zerobits)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
// prec->cblk = av_malloc_array(prec->nb_codeblocks_width *
|
|
|
|
// prec->nb_codeblocks_height,
|
|
|
|
// sizeof(*prec->cblk));
|
|
|
|
prec->cblk = band->cblk;
|
|
|
|
av_assert0(nb_precincts == 1);
|
|
|
|
if (!prec->cblk)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
for (cblkno = 0; cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height; cblkno++) {
|
|
|
|
Jpeg2000Cblk *cblk = prec->cblk + cblkno;
|
|
|
|
uint16_t Cx0, Cy0;
|
|
|
|
|
|
|
|
/* Compute coordinates of codeblocks */
|
|
|
|
/* Compute Cx0*/
|
|
|
|
Cx0 = (prec->coord[0][0] >> band->log2_cblk_width) << band->log2_cblk_width;
|
|
|
|
Cx0 = Cx0 + ((cblkno % prec->nb_codeblocks_width) << band->log2_cblk_width);
|
|
|
|
cblk->coord[0][0] = FFMAX(Cx0, prec->coord[0][0]);
|
|
|
|
|
|
|
|
/* Compute Cy0*/
|
|
|
|
Cy0 = (prec->coord[1][0] >> band->log2_cblk_height) << band->log2_cblk_height;
|
|
|
|
Cy0 = Cy0 + ((cblkno / prec->nb_codeblocks_width) << band->log2_cblk_height);
|
|
|
|
cblk->coord[1][0] = FFMAX(Cy0, prec->coord[1][0]);
|
|
|
|
|
|
|
|
/* Compute Cx1 */
|
|
|
|
cblk->coord[0][1] = FFMIN(Cx0 + (1 << band->log2_cblk_width),
|
|
|
|
prec->coord[0][1]);
|
|
|
|
|
|
|
|
/* Compute Cy1 */
|
|
|
|
cblk->coord[1][1] = FFMIN(Cy0 + (1 << band->log2_cblk_height),
|
|
|
|
prec->coord[1][1]);
|
|
|
|
cblk->zero = 0;
|
|
|
|
cblk->lblock = 3;
|
|
|
|
cblk->length = 0;
|
|
|
|
cblk->lengthinc = 0;
|
|
|
|
cblk->npasses = 0;
|
|
|
|
}
|
|
|
|
}
|
2011-05-23 21:13:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-05-21 19:19:05 +00:00
|
|
|
void ff_j2k_reinit(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
|
2011-05-23 21:13:34 +00:00
|
|
|
{
|
|
|
|
int reslevelno, bandno, cblkno, precno;
|
2013-05-26 17:00:56 +00:00
|
|
|
for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
|
2013-05-21 19:19:05 +00:00
|
|
|
Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
|
2013-05-26 17:00:56 +00:00
|
|
|
for (bandno = 0; bandno < rlevel->nbands; bandno++) {
|
2013-05-21 19:19:05 +00:00
|
|
|
Jpeg2000Band *band = rlevel->band + bandno;
|
2013-05-26 17:00:56 +00:00
|
|
|
for(precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++) {
|
2013-05-21 19:19:05 +00:00
|
|
|
Jpeg2000Prec *prec = band->prec + precno;
|
2011-05-23 21:13:34 +00:00
|
|
|
tag_tree_zero(prec->zerobits, prec->xi1 - prec->xi0, prec->yi1 - prec->yi0);
|
|
|
|
tag_tree_zero(prec->cblkincl, prec->xi1 - prec->xi0, prec->yi1 - prec->yi0);
|
|
|
|
}
|
2013-05-26 17:00:56 +00:00
|
|
|
for (cblkno = 0; cblkno < band->cblknx * band->cblkny; cblkno++) {
|
2013-05-21 19:19:05 +00:00
|
|
|
Jpeg2000Cblk *cblk = band->cblk + cblkno;
|
2011-05-23 21:13:34 +00:00
|
|
|
cblk->length = 0;
|
|
|
|
cblk->lblock = 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-21 19:19:05 +00:00
|
|
|
void ff_j2k_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
|
2011-05-23 21:13:34 +00:00
|
|
|
{
|
|
|
|
int reslevelno, bandno, precno;
|
2013-05-26 17:00:56 +00:00
|
|
|
for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
|
2013-05-21 19:19:05 +00:00
|
|
|
Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
|
2011-05-23 21:13:34 +00:00
|
|
|
|
2013-05-26 20:40:25 +00:00
|
|
|
for (bandno = 0; bandno < reslevel->nbands; bandno++) {
|
2013-05-21 19:19:05 +00:00
|
|
|
Jpeg2000Band *band = reslevel->band + bandno;
|
2013-05-26 17:00:56 +00:00
|
|
|
for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++) {
|
2013-05-21 19:19:05 +00:00
|
|
|
Jpeg2000Prec *prec = band->prec + precno;
|
2011-05-23 21:13:34 +00:00
|
|
|
av_freep(&prec->zerobits);
|
|
|
|
av_freep(&prec->cblkincl);
|
|
|
|
}
|
|
|
|
av_freep(&band->cblk);
|
|
|
|
av_freep(&band->prec);
|
|
|
|
}
|
|
|
|
av_freep(&reslevel->band);
|
|
|
|
}
|
|
|
|
|
|
|
|
ff_j2k_dwt_destroy(&comp->dwt);
|
|
|
|
av_freep(&comp->reslevel);
|
|
|
|
av_freep(&comp->data);
|
|
|
|
}
|