FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mjpegdec.h
Date: 2026-11-01 05:14:56
Exec Total Coverage
Lines: 19 22 86.4%
Functions: 2 2 100.0%
Branches: 12 14 85.7%

Line Branch Exec 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 #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 4740767 static inline int ff_mjpeg_should_restart(MJpegDecodeContext *s)
205 {
206 4740767 int restart = 0;
207
2/2
✓ Branch 0 taken 120404 times.
✓ Branch 1 taken 4620363 times.
4740767 if (s->restart_interval) {
208
2/2
✓ Branch 0 taken 6404 times.
✓ Branch 1 taken 114000 times.
120404 if (s->restart_count <= 0) {
209 6404 s->restart_count = s->restart_interval;
210 6404 restart = 1;
211 }
212 120404 s->restart_count--;
213 } else {
214
2/2
✓ Branch 0 taken 2429 times.
✓ Branch 1 taken 4617934 times.
4620363 if (s->restart_count < 0) {
215 2429 s->restart_count = 0;
216 2429 restart = 1;
217 }
218 }
219 4740767 return restart;
220 }
221
222 4740767 static inline int ff_mjpeg_handle_restart(MJpegDecodeContext *s, int *restart)
223 {
224 4740767 int first = s->restart_count < 0;
225 4740767 *restart = ff_mjpeg_should_restart(s);
226
2/2
✓ Branch 0 taken 8833 times.
✓ Branch 1 taken 4731934 times.
4740767 if (*restart) {
227
3/4
✓ Branch 0 taken 74 times.
✓ Branch 1 taken 8759 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 74 times.
8833 if (s->avctx->codec_id == AV_CODEC_ID_THP && !first) {
228 ✗ align_get_bits(&s->gb);
229 ✗ return 0;
230 }
231 8833 int ret = ff_mjpeg_unescape_sos(s);
232
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8833 times.
8833 if (ret < 0)
233 ✗ return ret;
234 }
235 4740767 return 0;
236 }
237
238 #endif /* AVCODEC_MJPEGDEC_H */
239