Coverage Report

Created: 2026-10-28 08:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/mjpegdec.h
Line
Count
Source
1
/*
2
 * MJPEG decoder
3
 * Copyright (c) 2000, 2001 Fabrice Bellard
4
 * Copyright (c) 2003 Alex Beregszaszi
5
 * Copyright (c) 2003-2004 Michael Niedermayer
6
 *
7
 * This file is part of FFmpeg.
8
 *
9
 * FFmpeg is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU Lesser General Public
11
 * License as published by the Free Software Foundation; either
12
 * version 2.1 of the License, or (at your option) any later version.
13
 *
14
 * FFmpeg is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
 * Lesser General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Lesser General Public
20
 * License along with FFmpeg; if not, write to the Free Software
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22
 */
23
24
/**
25
 * @file
26
 * MJPEG decoder.
27
 */
28
29
#ifndef AVCODEC_MJPEGDEC_H
30
#define AVCODEC_MJPEGDEC_H
31
32
#include "libavutil/gain_map.h"
33
#include "libavutil/log.h"
34
#include "libavutil/mem_internal.h"
35
#include "libavutil/pixdesc.h"
36
#include "libavutil/stereo3d.h"
37
38
#include "avcodec.h"
39
#include "blockdsp.h"
40
#include "bytestream.h"
41
#include "exif.h"
42
#include "get_bits.h"
43
#include "hpeldsp.h"
44
#include "idctdsp.h"
45
46
#undef near /* This file uses struct member 'near' which in windows.h is defined as empty. */
47
48
4.43M
#define MAX_COMPONENTS 4
49
50
typedef struct ICCEntry {
51
    uint8_t *data;
52
    int    length;
53
} ICCEntry;
54
55
struct JLSState;
56
57
typedef struct MJpegDecodeContext {
58
    AVClass *class;
59
    AVCodecContext *avctx;
60
    GetBitContext gb;
61
    GetByteContext gB;
62
    int buf_size;
63
64
    int buffer_size;
65
    uint8_t *buffer;
66
67
    uint16_t quant_matrixes[4][64];
68
    VLC vlcs[3][4];
69
    int qscale[4];      ///< quantizer scale calculated from quant_matrixes
70
71
    int orig_height;  /* size given at codec init */
72
    int first_picture;    /* true if decoding first picture */
73
    int interlaced;     /* true if interlaced */
74
    int bottom_field;   /* true if bottom field */
75
    int lossless;
76
    int ls;
77
    int progressive;
78
    int bayer;          /* true if it's a bayer-encoded JPEG embedded in a DNG */
79
    int rgb;
80
    uint8_t upscale_h[4];
81
    uint8_t upscale_v[4];
82
    int rct;            /* standard rct */
83
    int pegasus_rct;    /* pegasus reversible colorspace transform */
84
    int bits;           /* bits per component */
85
    int colr;
86
    int xfrm;
87
    int adobe_transform;
88
89
    /* SOS fields */
90
    int nb_components_sos;
91
    int Ss;
92
    int Se;
93
    int Ah;
94
    int Al;
95
96
    int maxval;
97
    int near;         ///< near lossless bound (si 0 for lossless)
98
    int t1,t2,t3;
99
    int reset;        ///< context halfing interval ?rename
100
101
    int width, height;
102
    int mb_width, mb_height;
103
    int nb_components;
104
    int block_stride[MAX_COMPONENTS];
105
    int component_id[MAX_COMPONENTS];
106
    int h_count[MAX_COMPONENTS]; /* horizontal and vertical count for each component */
107
    int v_count[MAX_COMPONENTS];
108
    int comp_index[MAX_COMPONENTS];
109
    int dc_index[MAX_COMPONENTS];
110
    int ac_index[MAX_COMPONENTS];
111
    int nb_blocks[MAX_COMPONENTS];
112
    int h_scount[MAX_COMPONENTS];
113
    int v_scount[MAX_COMPONENTS];
114
    int quant_sindex[MAX_COMPONENTS];
115
    int h_max, v_max; /* maximum h and v counts */
116
    int quant_index[4];   /* quant table index for each component */
117
    int last_dc[MAX_COMPONENTS]; /* last DEQUANTIZED dc (XXX: am I right to do that ?) */
118
    AVFrame *picture; /* picture structure */
119
    AVFrame *picture_ptr; /* pointer to picture structure */
120
    int got_picture;                                ///< we found a SOF and picture is valid, too.
121
    int nb_seq_component_scans;                     ///< component scans decoded since the SOF (sequential images: <= nb_components)
122
    int linesize[MAX_COMPONENTS];                   ///< linesize << interlaced
123
    DECLARE_ALIGNED(32, int16_t, block)[64];
124
    int16_t (*blocks[MAX_COMPONENTS])[64]; ///< intermediate sums (progressive mode)
125
    uint8_t *last_nnz[MAX_COMPONENTS];
126
    uint64_t coefs_finished[MAX_COMPONENTS]; ///< bitmask of which coefs have been completely decoded (progressive mode)
127
    int palette_index;
128
    int force_pal8;
129
    uint8_t permutated_scantable[64];
130
    BlockDSPContext bdsp;
131
    IDCTDSPContext idsp;
132
    op_pixels_func copy_block;             ///< only set and used by mxpeg
133
134
    int restart_interval;
135
    int restart_count;
136
137
    int cs_itu601;
138
    int interlace_polarity;
139
    int multiscope;
140
141
    int mjpb_skiptosod;
142
143
    int cur_scan; /* current scan, used by JPEG-LS */
144
    int64_t total_ls_decoded_height; /* cumulative JPEG-LS rows decoded in the current packet */
145
    int flipped; /* true if picture is flipped */
146
147
    uint16_t (*ljpeg_buffer)[4];
148
    unsigned int ljpeg_buffer_size;
149
150
#if FF_API_MJPEG_EXTERN_HUFF
151
    int extern_huff;
152
#endif
153
    AVExifMetadata exif_metadata;
154
155
    AVStereo3D *stereo3d; ///!< stereoscopic information (cached, since it is read before frame allocation)
156
157
    AVGainMapParams *gain_map;
158
    size_t gain_map_size;
159
160
    const AVPixFmtDescriptor *pix_desc;
161
162
    ICCEntry *iccentries;
163
    int iccnum;
164
    int iccread;
165
166
    AVFrame *smv_frame;
167
    int smv_frames_per_jpeg;
168
    int smv_next_frame;
169
170
    // Raw stream data for hwaccel use.
171
    const uint8_t *raw_image_buffer;
172
    size_t         raw_image_buffer_size;
173
174
    uint8_t raw_huffman_lengths[2][4][16];
175
    uint8_t raw_huffman_values[2][4][256];
176
177
    enum AVPixelFormat hwaccel_sw_pix_fmt;
178
    enum AVPixelFormat hwaccel_pix_fmt;
179
    void *hwaccel_picture_private;
180
    struct JLSState *jls_state;
181
182
    const uint8_t *mb_bitmask;
183
    size_t mb_bitmask_size;
184
    const AVFrame *reference;
185
} MJpegDecodeContext;
186
187
int ff_mjpeg_build_vlc(VLC *vlc, const uint8_t *bits_table,
188
                       const uint8_t *val_table, int is_ac, void *logctx);
189
int ff_mjpeg_decode_init(AVCodecContext *avctx);
190
int ff_mjpeg_decode_end(AVCodecContext *avctx);
191
int ff_mjpeg_decode_frame(AVCodecContext *avctx,
192
                          AVFrame *frame, int *got_frame,
193
                          AVPacket *avpkt);
194
int ff_mjpeg_decode_frame_from_buf(AVCodecContext *avctx,
195
                                   AVFrame *frame, int *got_frame,
196
                                   const AVPacket *avpkt, const uint8_t *buf, int buf_size);
197
int ff_mjpeg_decode_dqt(MJpegDecodeContext *s);
198
int ff_mjpeg_decode_dht(MJpegDecodeContext *s);
199
int ff_mjpeg_decode_sof(MJpegDecodeContext *s);
200
int ff_mjpeg_decode_sos(MJpegDecodeContext *s);
201
int ff_mjpeg_find_marker(const uint8_t **buf_ptr, const uint8_t *buf_end);
202
int ff_mjpeg_unescape_sos(MJpegDecodeContext *s);
203
204
static inline int ff_mjpeg_should_restart(MJpegDecodeContext *s)
205
63.2M
{
206
63.2M
    int restart = 0;
207
63.2M
    if (s->restart_interval) {
208
13.6M
        if (s->restart_count <= 0) {
209
5.24M
            s->restart_count = s->restart_interval;
210
5.24M
            restart = 1;
211
5.24M
        }
212
13.6M
        s->restart_count--;
213
49.5M
    } else {
214
49.5M
        if (s->restart_count < 0) {
215
1.00M
            s->restart_count = 0;
216
1.00M
            restart = 1;
217
1.00M
        }
218
49.5M
    }
219
63.2M
    return restart;
220
63.2M
}
mjpegdec.c:ff_mjpeg_should_restart
Line
Count
Source
205
58.3M
{
206
58.3M
    int restart = 0;
207
58.3M
    if (s->restart_interval) {
208
9.55M
        if (s->restart_count <= 0) {
209
3.19M
            s->restart_count = s->restart_interval;
210
3.19M
            restart = 1;
211
3.19M
        }
212
9.55M
        s->restart_count--;
213
48.7M
    } else {
214
48.7M
        if (s->restart_count < 0) {
215
861k
            s->restart_count = 0;
216
861k
            restart = 1;
217
861k
        }
218
48.7M
    }
219
58.3M
    return restart;
220
58.3M
}
Unexecuted instantiation: mjpegdec_common.c:ff_mjpeg_should_restart
jpeglsdec.c:ff_mjpeg_should_restart
Line
Count
Source
205
4.89M
{
206
4.89M
    int restart = 0;
207
4.89M
    if (s->restart_interval) {
208
4.10M
        if (s->restart_count <= 0) {
209
2.05M
            s->restart_count = s->restart_interval;
210
2.05M
            restart = 1;
211
2.05M
        }
212
4.10M
        s->restart_count--;
213
4.10M
    } else {
214
796k
        if (s->restart_count < 0) {
215
144k
            s->restart_count = 0;
216
144k
            restart = 1;
217
144k
        }
218
796k
    }
219
4.89M
    return restart;
220
4.89M
}
Unexecuted instantiation: mxpegdec.c:ff_mjpeg_should_restart
Unexecuted instantiation: tiff.c:ff_mjpeg_should_restart
Unexecuted instantiation: mjpegbdec.c:ff_mjpeg_should_restart
Unexecuted instantiation: g2meet.c:ff_mjpeg_should_restart
Unexecuted instantiation: sp5xdec.c:ff_mjpeg_should_restart
221
222
static inline int ff_mjpeg_handle_restart(MJpegDecodeContext *s, int *restart)
223
63.2M
{
224
63.2M
    int first = s->restart_count < 0;
225
63.2M
    *restart = ff_mjpeg_should_restart(s);
226
63.2M
    if (*restart) {
227
6.25M
        if (s->avctx->codec_id == AV_CODEC_ID_THP && !first) {
228
3.65M
            align_get_bits(&s->gb);
229
3.65M
            return 0;
230
3.65M
        }
231
2.59M
        int ret = ff_mjpeg_unescape_sos(s);
232
2.59M
        if (ret < 0)
233
0
            return ret;
234
2.59M
    }
235
59.5M
    return 0;
236
63.2M
}
mjpegdec.c:ff_mjpeg_handle_restart
Line
Count
Source
223
58.3M
{
224
58.3M
    int first = s->restart_count < 0;
225
58.3M
    *restart = ff_mjpeg_should_restart(s);
226
58.3M
    if (*restart) {
227
4.05M
        if (s->avctx->codec_id == AV_CODEC_ID_THP && !first) {
228
1.64M
            align_get_bits(&s->gb);
229
1.64M
            return 0;
230
1.64M
        }
231
2.40M
        int ret = ff_mjpeg_unescape_sos(s);
232
2.40M
        if (ret < 0)
233
0
            return ret;
234
2.40M
    }
235
56.6M
    return 0;
236
58.3M
}
Unexecuted instantiation: mjpegdec_common.c:ff_mjpeg_handle_restart
jpeglsdec.c:ff_mjpeg_handle_restart
Line
Count
Source
223
4.89M
{
224
4.89M
    int first = s->restart_count < 0;
225
4.89M
    *restart = ff_mjpeg_should_restart(s);
226
4.89M
    if (*restart) {
227
2.20M
        if (s->avctx->codec_id == AV_CODEC_ID_THP && !first) {
228
2.00M
            align_get_bits(&s->gb);
229
2.00M
            return 0;
230
2.00M
        }
231
194k
        int ret = ff_mjpeg_unescape_sos(s);
232
194k
        if (ret < 0)
233
0
            return ret;
234
194k
    }
235
2.89M
    return 0;
236
4.89M
}
Unexecuted instantiation: mxpegdec.c:ff_mjpeg_handle_restart
Unexecuted instantiation: tiff.c:ff_mjpeg_handle_restart
Unexecuted instantiation: mjpegbdec.c:ff_mjpeg_handle_restart
Unexecuted instantiation: g2meet.c:ff_mjpeg_handle_restart
Unexecuted instantiation: sp5xdec.c:ff_mjpeg_handle_restart
237
238
#endif /* AVCODEC_MJPEGDEC_H */