jpegls.c 3.13 KB
Newer Older
1 2 3 4 5
/*
 * JPEG-LS common code
 * Copyright (c) 2003 Michael Niedermayer
 * Copyright (c) 2006 Konstantin Shishkov
 *
6
 * This file is part of Libav.
7
 *
8
 * Libav is free software; you can redistribute it and/or
9 10 11 12
 * 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.
 *
13
 * Libav is distributed in the hope that it will be useful,
14 15 16 17 18
 * 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
19
 * License along with Libav; if not, write to the Free Software
20 21 22 23
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
24
 * @file
25 26 27
 * JPEG-LS common code.
 */

28
#include "internal.h"
29 30
#include "jpegls.h"

31 32
void ff_jpegls_init_state(JLSState *state)
{
33 34 35
    int i;

    state->twonear = state->near * 2 + 1;
36
    state->range   = (state->maxval + state->twonear - 1) / state->twonear + 1;
37 38

    // QBPP = ceil(log2(RANGE))
39 40
    for (state->qbpp = 0; (1 << state->qbpp) < state->range; state->qbpp++)
        ;
41

42 43
    if (state->bpp < 8)
        state->limit = 2 * state->bpp - state->qbpp + 16;
44
    else
45
        state->limit = 4 * state->bpp - state->qbpp;
46

47
    for (i = 0; i < 367; i++) {
48
        state->A[i] = FFMAX(state->range + 32 >> 6, 2);
49 50 51 52 53 54 55
        state->N[i] = 1;
    }
}

/**
 * Custom value clipping function used in T1, T2, T3 calculation
 */
56 57 58 59 60 61
static inline int iso_clip(int v, int vmin, int vmax)
{
    if (v > vmax || v < vmin)
        return vmin;
    else
        return v;
62 63
}

64 65 66 67 68
void ff_jpegls_reset_coding_parameters(JLSState *s, int reset_all)
{
    const int basic_t1 = 3;
    const int basic_t2 = 7;
    const int basic_t3 = 21;
69 70
    int factor;

71 72
    if (s->maxval == 0 || reset_all)
        s->maxval = (1 << s->bpp) - 1;
73

74
    if (s->maxval >= 128) {
75
        factor = FFMIN(s->maxval, 4095) + 128 >> 8;
76

77 78 79 80 81 82 83 84 85 86 87
        if (s->T1 == 0 || reset_all)
            s->T1 = iso_clip(factor * (basic_t1 - 2) + 2 + 3 * s->near,
                             s->near + 1, s->maxval);
        if (s->T2 == 0 || reset_all)
            s->T2 = iso_clip(factor * (basic_t2 - 3) + 3 + 5 * s->near,
                             s->T1, s->maxval);
        if (s->T3 == 0 || reset_all)
            s->T3 = iso_clip(factor * (basic_t3 - 4) + 4 + 7 * s->near,
                             s->T2, s->maxval);
    } else {
        factor = 256 / (s->maxval + 1);
88

89 90 91 92 93 94 95 96 97
        if (s->T1 == 0 || reset_all)
            s->T1 = iso_clip(FFMAX(2, basic_t1 / factor + 3 * s->near),
                             s->near + 1, s->maxval);
        if (s->T2 == 0 || reset_all)
            s->T2 = iso_clip(FFMAX(3, basic_t2 / factor + 5 * s->near),
                             s->T1, s->maxval);
        if (s->T3 == 0 || reset_all)
            s->T3 = iso_clip(FFMAX(4, basic_t3 / factor + 7 * s->near),
                             s->T2, s->maxval);
98 99
    }

100 101
    if (s->reset == 0 || reset_all)
        s->reset = 64;
102
    ff_dlog(NULL, "[JPEG-LS RESET] T=%i,%i,%i\n", s->T1, s->T2, s->T3);
103
}