Coverage Report

Created: 2026-01-16 07:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/rtjpeg.c
Line
Count
Source
1
/*
2
 * RTJpeg decoding functions
3
 * Copyright (c) 2006 Reimar Doeffinger
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
#include "libavutil/attributes.h"
23
#include "libavutil/common.h"
24
#include "get_bits.h"
25
#include "rtjpeg.h"
26
27
#define PUT_COEFF(c) \
28
300M
    i = scan[coeff--]; \
29
300M
    block[i] = (c) * quant[i];
30
31
/// aligns the bitstream to the given power of two
32
#define ALIGN(a) \
33
21.7M
    n = (-get_bits_count(gb)) & (a - 1); \
34
21.7M
    if (n) {skip_bits(gb, n);}
35
36
/**
37
 * @brief read one block from stream
38
 * @param gb contains stream data
39
 * @param block where data is written to
40
 * @param scan array containing the mapping stream address -> block position
41
 * @param quant quantization factors
42
 * @return 0 means the block is not coded, < 0 means an error occurred.
43
 *
44
 * Note: GetBitContext is used to make the code simpler, since all data is
45
 * aligned this could be done faster in a different way, e.g. as it is done
46
 * in MPlayer libmpcodecs/native/rtjpegn.c.
47
 */
48
static inline int get_block(GetBitContext *gb, int16_t *block, const uint8_t *scan,
49
11.6M
                            const uint32_t *quant) {
50
11.6M
    int coeff, i, n;
51
11.6M
    int8_t ac;
52
11.6M
    uint8_t dc = get_bits(gb, 8);
53
54
    // block not coded
55
11.6M
    if (dc == 255)
56
821k
       return 0;
57
58
    // number of non-zero coefficients
59
10.8M
    coeff = get_bits(gb, 6);
60
10.8M
    if (get_bits_left(gb) < (coeff << 1))
61
3.12k
        return AVERROR_INVALIDDATA;
62
63
    // normally we would only need to clear the (63 - coeff) last values,
64
    // but since we do not know where they are we just clear the whole block
65
10.8M
    memset(block, 0, 64 * sizeof(int16_t));
66
67
    // 2 bits per coefficient
68
115M
    while (coeff) {
69
108M
        ac = get_sbits(gb, 2);
70
108M
        if (ac == -2)
71
3.88M
            break; // continue with more bits
72
104M
        PUT_COEFF(ac);
73
104M
    }
74
75
    // 4 bits per coefficient
76
10.8M
    ALIGN(4);
77
10.8M
    if (get_bits_left(gb) < (coeff << 2))
78
467
        return AVERROR_INVALIDDATA;
79
108M
    while (coeff) {
80
100M
        ac = get_sbits(gb, 4);
81
100M
        if (ac == -8)
82
2.44M
            break; // continue with more bits
83
97.9M
        PUT_COEFF(ac);
84
97.9M
    }
85
86
    // 8 bits per coefficient
87
10.8M
    ALIGN(8);
88
10.8M
    if (get_bits_left(gb) < (coeff << 3))
89
284
        return AVERROR_INVALIDDATA;
90
97.3M
    while (coeff) {
91
86.4M
        ac = get_sbits(gb, 8);
92
86.4M
        PUT_COEFF(ac);
93
86.4M
    }
94
95
10.8M
    PUT_COEFF(dc);
96
10.8M
    return 1;
97
10.8M
}
98
99
/**
100
 * @brief decode one rtjpeg YUV420 frame
101
 * @param c context, must be initialized via ff_rtjpeg_decode_init
102
 * @param f AVFrame to place decoded frame into. If parts of the frame
103
 *          are not coded they are left unchanged, so consider initializing it
104
 * @param buf buffer containing input data
105
 * @param buf_size length of input data in bytes
106
 * @return number of bytes consumed from the input buffer
107
 */
108
int ff_rtjpeg_decode_frame_yuv420(RTJpegContext *c, AVFrame *f,
109
5.31k
                                  const uint8_t *buf, int buf_size) {
110
5.31k
    GetBitContext gb;
111
5.31k
    int w = c->w / 16, h = c->h / 16;
112
5.31k
    int x, y, ret;
113
5.31k
    uint8_t *y1 = f->data[0], *y2 = f->data[0] + 8 * f->linesize[0];
114
5.31k
    uint8_t *u = f->data[1], *v = f->data[2];
115
116
5.31k
    if ((ret = init_get_bits8(&gb, buf, buf_size)) < 0)
117
0
        return ret;
118
119
17.7k
    for (y = 0; y < h; y++) {
120
1.96M
        for (x = 0; x < w; x++) {
121
11.6M
#define BLOCK(quant, dst, stride) do { \
122
11.6M
    int res = get_block(&gb, block, c->scan, quant); \
123
11.6M
    if (res < 0) \
124
11.6M
        return res; \
125
11.6M
    if (res > 0) \
126
11.6M
        c->idsp.idct_put(dst, stride, block); \
127
11.6M
} while (0)
128
1.95M
            int16_t *block = c->block;
129
1.95M
            BLOCK(c->lquant, y1, f->linesize[0]);
130
1.94M
            y1 += 8;
131
1.94M
            BLOCK(c->lquant, y1, f->linesize[0]);
132
1.94M
            y1 += 8;
133
1.94M
            BLOCK(c->lquant, y2, f->linesize[0]);
134
1.94M
            y2 += 8;
135
1.94M
            BLOCK(c->lquant, y2, f->linesize[0]);
136
1.94M
            y2 += 8;
137
1.94M
            BLOCK(c->cquant, u,  f->linesize[1]);
138
1.94M
            u += 8;
139
1.94M
            BLOCK(c->cquant, v,  f->linesize[2]);
140
1.94M
            v += 8;
141
1.94M
        }
142
12.4k
        y1 += 2 * 8 * (f->linesize[0] - w);
143
12.4k
        y2 += 2 * 8 * (f->linesize[0] - w);
144
12.4k
        u += 8 * (f->linesize[1] - w);
145
12.4k
        v += 8 * (f->linesize[2] - w);
146
12.4k
    }
147
1.43k
    return get_bits_count(&gb) / 8;
148
5.31k
}
149
150
/**
151
 * @brief initialize an RTJpegContext, may be called multiple times
152
 * @param c context to initialize
153
 * @param width width of image, will be rounded down to the nearest multiple
154
 *              of 16 for decoding
155
 * @param height height of image, will be rounded down to the nearest multiple
156
 *              of 16 for decoding
157
 * @param lquant luma quantization table to use
158
 * @param cquant chroma quantization table to use
159
 */
160
void ff_rtjpeg_decode_init(RTJpegContext *c, int width, int height,
161
14.0k
                           const uint32_t *lquant, const uint32_t *cquant) {
162
14.0k
    int i;
163
913k
    for (i = 0; i < 64; i++) {
164
899k
        int p = c->idsp.idct_permutation[i];
165
899k
        c->lquant[p] = lquant[i];
166
899k
        c->cquant[p] = cquant[i];
167
899k
    }
168
14.0k
    c->w = width;
169
14.0k
    c->h = height;
170
14.0k
}
171
172
av_cold void ff_rtjpeg_init(RTJpegContext *c, struct AVCodecContext *avctx)
173
1.52k
{
174
1.52k
    int i;
175
176
1.52k
    ff_idctdsp_init(&c->idsp, avctx);
177
178
99.1k
    for (i = 0; i < 64; i++) {
179
97.6k
        int z = ff_zigzag_direct[i];
180
97.6k
        z = ((z << 3) | (z >> 3)) & 63; // rtjpeg uses a transposed variant
181
182
        // permute the scan and quantization tables for the chosen idct
183
97.6k
        c->scan[i] = c->idsp.idct_permutation[z];
184
97.6k
    }
185
1.52k
}