Coverage Report

Created: 2025-08-28 07:12

/src/ffmpeg/libavcodec/jpegls.c
Line
Count
Source
1
/*
2
 * JPEG-LS common code
3
 * Copyright (c) 2003 Michael Niedermayer
4
 * Copyright (c) 2006 Konstantin Shishkov
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-LS common code.
26
 */
27
28
#include <stddef.h>
29
#include "libavutil/internal.h"
30
#include "libavutil/intmath.h"
31
#include "libavutil/log.h"
32
#include "jpegls.h"
33
34
void ff_jpegls_init_state(JLSState *state)
35
160k
{
36
160k
    int i;
37
38
160k
    state->twonear = state->near * 2 + 1;
39
160k
    state->range   = (state->maxval + state->twonear - 1) / state->twonear + 1;
40
41
    // QBPP = ceil(log2(RANGE))
42
1.05M
    for (state->qbpp = 0; (1 << state->qbpp) < state->range; state->qbpp++)
43
896k
        ;
44
45
160k
    state->bpp   = FFMAX(av_log2(state->maxval) + 1, 2);
46
160k
    state->limit = 2*(state->bpp + FFMAX(state->bpp, 8)) - state->qbpp;
47
48
59.2M
    for (i = 0; i < 367; i++) {
49
59.0M
        state->A[i] = FFMAX(state->range + 32 >> 6, 2);
50
59.0M
        state->N[i] = 1;
51
59.0M
    }
52
160k
}
53
54
/**
55
 * Custom value clipping function used in T1, T2, T3 calculation
56
 */
57
static inline int iso_clip(int v, int vmin, int vmax)
58
510k
{
59
510k
    if (v > vmax || v < vmin)
60
154k
        return vmin;
61
355k
    else
62
355k
        return v;
63
510k
}
64
65
void ff_jpegls_reset_coding_parameters(JLSState *s, int reset_all)
66
201k
{
67
201k
    const int basic_t1 = 3;
68
201k
    const int basic_t2 = 7;
69
201k
    const int basic_t3 = 21;
70
201k
    int factor;
71
72
201k
    if (s->maxval == 0 || reset_all)
73
137k
        s->maxval = (1 << s->bpp) - 1;
74
75
201k
    if (s->maxval >= 128) {
76
130k
        factor = FFMIN(s->maxval, 4095) + 128 >> 8;
77
78
130k
        if (s->T1 == 0 || reset_all)
79
113k
            s->T1 = iso_clip(factor * (basic_t1 - 2) + 2 + 3 * s->near,
80
113k
                             s->near + 1, s->maxval);
81
130k
        if (s->T2 == 0 || reset_all)
82
116k
            s->T2 = iso_clip(factor * (basic_t2 - 3) + 3 + 5 * s->near,
83
116k
                             s->T1, s->maxval);
84
130k
        if (s->T3 == 0 || reset_all)
85
119k
            s->T3 = iso_clip(factor * (basic_t3 - 4) + 4 + 7 * s->near,
86
119k
                             s->T2, s->maxval);
87
130k
    } else {
88
71.5k
        factor = 256 / (s->maxval + 1);
89
90
71.5k
        if (s->T1 == 0 || reset_all)
91
34.8k
            s->T1 = iso_clip(FFMAX(2, basic_t1 / factor + 3 * s->near),
92
34.8k
                             s->near + 1, s->maxval);
93
71.5k
        if (s->T2 == 0 || reset_all)
94
61.3k
            s->T2 = iso_clip(FFMAX(3, basic_t2 / factor + 5 * s->near),
95
61.3k
                             s->T1, s->maxval);
96
71.5k
        if (s->T3 == 0 || reset_all)
97
64.3k
            s->T3 = iso_clip(FFMAX(4, basic_t3 / factor + 7 * s->near),
98
64.3k
                             s->T2, s->maxval);
99
71.5k
    }
100
101
201k
    if (s->reset == 0 || reset_all)
102
145k
        s->reset = 64;
103
201k
    ff_dlog(NULL, "[JPEG-LS RESET] T=%i,%i,%i\n", s->T1, s->T2, s->T3);
104
201k
}