FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mjpegenc_common.c
Date: 2026-10-30 17:37:53
Exec Total Coverage
Lines: 280 326 85.9%
Functions: 13 13 100.0%
Branches: 126 184 68.5%

Line Branch Exec Source
1 /*
2 * lossless JPEG shared bits
3 * Copyright (c) 2000, 2001 Fabrice Bellard
4 * Copyright (c) 2003 Alex Beregszaszi
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 #include <stdint.h>
24 #include <string.h>
25
26 #include "libavutil/pixdesc.h"
27 #include "libavutil/pixfmt.h"
28
29 #include "avcodec.h"
30 #include "idctdsp.h"
31 #include "jpegtables.h"
32 #include "put_bits.h"
33 #include "mjpegenc.h"
34 #include "gain_map.h"
35 #include "mjpegenc_common.h"
36 #include "mjpeg.h"
37 #include "version.h"
38
39 /* table_class: 0 = DC coef, 1 = AC coefs */
40 6564 static int put_huffman_table(PutBitContext *p, int table_class, int table_id,
41 const uint8_t *bits_table, const uint8_t *value_table)
42 {
43 6564 int n = 0;
44
45 6564 put_bits(p, 4, table_class);
46 6564 put_bits(p, 4, table_id);
47
48
2/2
✓ Branch 0 taken 105024 times.
✓ Branch 1 taken 6564 times.
111588 for (int i = 1; i <= 16; i++) {
49 105024 n += bits_table[i];
50 105024 put_bits(p, 8, bits_table[i]);
51 }
52
53
2/2
✓ Branch 0 taken 353730 times.
✓ Branch 1 taken 6564 times.
360294 for (int i = 0; i < n; i++)
54 353730 put_bits(p, 8, value_table[i]);
55
56 6564 return n + 17;
57 }
58
59 1641 static void jpeg_table_header(AVCodecContext *avctx, PutBitContext *p,
60 const MJpegContext *m,
61 const uint8_t intra_matrix_permutation[64],
62 const uint16_t luma_intra_matrix[64],
63 const uint16_t chroma_intra_matrix[64],
64 int hsample[3], int use_slices, int matrices_differ)
65 {
66 int size;
67 uint8_t *ptr;
68
69
2/2
✓ Branch 0 taken 1441 times.
✓ Branch 1 taken 200 times.
1641 if (m) {
70 1441 int matrix_count = 1 + matrices_differ;
71
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1441 times.
1441 if (m->force_duplicated_matrix)
72 ✗ matrix_count = 2;
73 /* quant matrixes */
74 1441 put_marker(p, DQT);
75 1441 put_bits(p, 16, 2 + matrix_count * (1 + 64));
76 1441 put_bits(p, 4, 0); /* 8 bit precision */
77 1441 put_bits(p, 4, 0); /* table 0 */
78
2/2
✓ Branch 0 taken 92224 times.
✓ Branch 1 taken 1441 times.
93665 for (int i = 0; i < 64; i++) {
79 92224 uint8_t j = intra_matrix_permutation[i];
80 92224 put_bits(p, 8, luma_intra_matrix[j]);
81 }
82
83
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1441 times.
1441 if (matrix_count > 1) {
84 ✗ put_bits(p, 4, 0); /* 8 bit precision */
85 ✗ put_bits(p, 4, 1); /* table 1 */
86 ✗ for (int i = 0; i < 64; i++) {
87 ✗ uint8_t j = intra_matrix_permutation[i];
88 ✗ put_bits(p, 8, chroma_intra_matrix[j]);
89 }
90 }
91 }
92
93
2/2
✓ Branch 0 taken 400 times.
✓ Branch 1 taken 1241 times.
1641 if (use_slices) {
94 400 put_marker(p, DRI);
95 400 put_bits(p, 16, 4);
96 400 put_bits(p, 16, (avctx->width-1)/(8*hsample[0]) + 1);
97 }
98
99 /* huffman table */
100 1641 put_marker(p, DHT);
101 1641 flush_put_bits(p);
102 1641 ptr = put_bits_ptr(p);
103 1641 put_bits(p, 16, 0); /* patched later */
104 1641 size = 2;
105
106 // Only MJPEG can have a variable Huffman variable. All other
107 // formats use the default Huffman table.
108
4/4
✓ Branch 0 taken 1441 times.
✓ Branch 1 taken 200 times.
✓ Branch 2 taken 841 times.
✓ Branch 3 taken 600 times.
1641 if (m && m->huffman == HUFFMAN_TABLE_OPTIMAL) {
109 1682 size += put_huffman_table(p, 0, 0, m->bits_dc_luminance,
110 841 m->val_dc_luminance);
111 1682 size += put_huffman_table(p, 0, 1, m->bits_dc_chrominance,
112 841 m->val_dc_chrominance);
113
114 1682 size += put_huffman_table(p, 1, 0, m->bits_ac_luminance,
115 841 m->val_ac_luminance);
116 1682 size += put_huffman_table(p, 1, 1, m->bits_ac_chrominance,
117 841 m->val_ac_chrominance);
118 } else {
119 800 size += put_huffman_table(p, 0, 0, ff_mjpeg_bits_dc_luminance,
120 ff_mjpeg_val_dc);
121 800 size += put_huffman_table(p, 0, 1, ff_mjpeg_bits_dc_chrominance,
122 ff_mjpeg_val_dc);
123
124 800 size += put_huffman_table(p, 1, 0, ff_mjpeg_bits_ac_luminance,
125 ff_mjpeg_val_ac_luminance);
126 800 size += put_huffman_table(p, 1, 1, ff_mjpeg_bits_ac_chrominance,
127 ff_mjpeg_val_ac_chrominance);
128 }
129 1641 AV_WB16(ptr, size);
130 1641 }
131
132 enum {
133 ICC_HDR_SIZE = 16, /* ICC_PROFILE\0 tag + 4 bytes */
134 ICC_CHUNK_SIZE = UINT16_MAX - ICC_HDR_SIZE,
135 ICC_MAX_CHUNKS = UINT8_MAX,
136 };
137
138 1641 int ff_mjpeg_add_icc_profile_size(AVCodecContext *avctx, const AVFrame *frame,
139 size_t *max_pkt_size)
140 {
141 const AVFrameSideData *sd;
142 size_t new_pkt_size;
143 int nb_chunks;
144 1641 sd = av_frame_get_side_data(frame, AV_FRAME_DATA_ICC_PROFILE);
145
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1640 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1641 if (!sd || !sd->size)
146 1640 return 0;
147
148
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (sd->size > ICC_MAX_CHUNKS * ICC_CHUNK_SIZE) {
149 ✗ av_log(avctx, AV_LOG_ERROR, "Cannot store %zu byte ICC "
150 "profile: too large for JPEG\n",
151 ✗ sd->size);
152 ✗ return AVERROR_INVALIDDATA;
153 }
154
155 1 nb_chunks = (sd->size + ICC_CHUNK_SIZE - 1) / ICC_CHUNK_SIZE;
156 1 new_pkt_size = *max_pkt_size + nb_chunks * (UINT16_MAX + 2 /* APP2 marker */);
157
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (new_pkt_size < *max_pkt_size) /* overflow */
158 ✗ return AVERROR_INVALIDDATA;
159 1 *max_pkt_size = new_pkt_size;
160 1 return 0;
161 }
162
163 1 static int want_gain_map_xmp(const struct MJpegContext *m,
164 const AVGainMapParams *p)
165 {
166
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
1 return m && m->xmp_gain_map && ff_gain_map_params_check_xmp(p);
167 }
168
169 1441 int ff_mjpeg_add_gain_map_size(AVCodecContext *avctx, struct MJpegContext *m,
170 const AVFrame *frame, size_t *max_pkt_size)
171 {
172
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1441 times.
1441 if (avctx->codec_id != AV_CODEC_ID_MJPEG)
173 ✗ return 0;
174
175 AVFrameSideData *sd;
176 1441 sd = av_frame_get_side_data(frame, AV_FRAME_DATA_GAIN_MAP_PARAMS);
177
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1440 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1441 if (!sd || sd->size < sizeof(AVGainMapParams))
178 1440 return 0;
179
180 1 const AVGainMapParams *params = (const AVGainMapParams *) sd->data;
181 1 int ret = av_gain_map_params_validate(params);
182
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
183 ✗ return ret;
184
185 1 size_t extra_size = 4 + sizeof(AV_ISO21496_IDENTIFIER) + AV_GAIN_MAP_MAX_PAYLOAD_SIZE;
186
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 if (m && m->xmp_gain_map) {
187
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (!ff_gain_map_params_check_xmp(params)) {
188 ✗ const int level = m->xmp_gain_map > 0 ? AV_LOG_ERROR : AV_LOG_WARNING;
189 ✗ av_log_once(avctx, level, AV_LOG_VERBOSE, &m->xmp_warned,
190 "Gain map metadata is incompatible with Ultra HDR XMP\n");
191 ✗ if (m->xmp_gain_map > 0)
192 ✗ return AVERROR(ENOTSUP);
193 } else {
194 1 extra_size += 4 + sizeof(FF_GAIN_MAP_XMP_IDENT) + FF_GAIN_MAP_XMP_MAX_LEN;
195 }
196 }
197
198 1 size_t new_pkt_size = *max_pkt_size + extra_size;
199
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (new_pkt_size < *max_pkt_size) /* overflow */
200 ✗ return AVERROR_INVALIDDATA;
201 1 *max_pkt_size = new_pkt_size;
202 1 return 0;
203 }
204
205 1641 static void jpeg_put_comments(AVCodecContext *avctx, PutBitContext *p,
206 const AVFrame *frame, const struct MJpegContext *m)
207 {
208 1641 const AVFrameSideData *sd = NULL;
209 int size;
210 uint8_t *ptr;
211
212
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1638 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
1641 if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0) {
213 3 AVRational sar = avctx->sample_aspect_ratio;
214
215
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 if (sar.num > 65535 || sar.den > 65535) {
216 ✗ if (!av_reduce(&sar.num, &sar.den, avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den, 65535))
217 ✗ av_log(avctx, AV_LOG_WARNING,
218 "Cannot store exact aspect ratio %d:%d\n",
219 avctx->sample_aspect_ratio.num,
220 avctx->sample_aspect_ratio.den);
221 }
222
223 /* JFIF header */
224 3 put_marker(p, APP0);
225 3 put_bits(p, 16, 16);
226 3 ff_put_string(p, "JFIF", 1); /* this puts the trailing zero-byte too */
227 /* The most significant byte is used for major revisions, the least
228 * significant byte for minor revisions. Version 1.02 is the current
229 * released revision. */
230 3 put_bits(p, 16, 0x0102);
231 3 put_bits(p, 8, 0); /* units type: 0 - aspect ratio */
232 3 put_bits(p, 16, sar.num);
233 3 put_bits(p, 16, sar.den);
234 3 put_bits(p, 8, 0); /* thumbnail width */
235 3 put_bits(p, 8, 0); /* thumbnail height */
236 }
237
238 /* ICC profile */
239 1641 sd = av_frame_get_side_data(frame, AV_FRAME_DATA_ICC_PROFILE);
240
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1640 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1641 if (sd && sd->size) {
241 1 const int nb_chunks = (sd->size + ICC_CHUNK_SIZE - 1) / ICC_CHUNK_SIZE;
242 1 const uint8_t *data = sd->data;
243 1 size_t remaining = sd->size;
244 /* must already be checked by the packat allocation code */
245
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 av_assert0(remaining <= ICC_MAX_CHUNKS * ICC_CHUNK_SIZE);
246 1 flush_put_bits(p);
247
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 for (int i = 0; i < nb_chunks; i++) {
248 1 size = FFMIN(remaining, ICC_CHUNK_SIZE);
249 av_assert1(size > 0);
250 1 ptr = put_bits_ptr(p);
251 1 ptr[0] = 0xff; /* chunk marker, not part of ICC_HDR_SIZE */
252 1 ptr[1] = APP2;
253 1 AV_WB16(ptr+2, size + ICC_HDR_SIZE);
254 1 AV_WL32(ptr+4, MKTAG('I','C','C','_'));
255 1 AV_WL32(ptr+8, MKTAG('P','R','O','F'));
256 1 AV_WL32(ptr+12, MKTAG('I','L','E','\0'));
257 1 ptr[16] = i+1;
258 1 ptr[17] = nb_chunks;
259 1 memcpy(&ptr[18], data, size);
260 1 skip_put_bytes(p, size + ICC_HDR_SIZE + 2);
261 1 remaining -= size;
262 1 data += size;
263 }
264 av_assert1(!remaining);
265 }
266
267 /* Gain map metadata; the ISO 21496-1 blob is written directly after the
268 * XMP packet, as recommended by the Ultra HDR specification */
269 3282 sd = avctx->codec_id == AV_CODEC_ID_MJPEG ?
270
2/2
✓ Branch 0 taken 1441 times.
✓ Branch 1 taken 200 times.
1641 av_frame_get_side_data(frame, AV_FRAME_DATA_GAIN_MAP_PARAMS) : NULL;
271
272
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1640 times.
1641 const AVGainMapParams *gmp = sd ? (const AVGainMapParams *) sd->data : NULL;
273
4/6
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1640 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
1641 if (sd && sd->size >= sizeof(AVGainMapParams) && want_gain_map_xmp(m, gmp)) {
274 1 put_marker(p, APP1);
275 1 flush_put_bits(p);
276 1 ptr = put_bits_ptr(p);
277 1 put_bits(p, 16, 0); /* patched later */
278 1 ff_put_string(p, FF_GAIN_MAP_XMP_IDENT, 1);
279 1 flush_put_bits(p);
280
281 /* pre-validated by ff_mjpeg_add_gain_map_size() */
282 1 size = ff_gain_map_params_to_xmp(gmp, put_bits_ptr(p));
283
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 av_assert0(size >= 0);
284 1 skip_put_bytes(p, size);
285 1 AV_WB16(ptr, 2 + sizeof(FF_GAIN_MAP_XMP_IDENT) + size);
286 }
287
288
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1640 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1641 if (sd && sd->size >= sizeof(AVGainMapParams)) {
289 1 put_marker(p, APP2);
290 1 flush_put_bits(p);
291 1 ptr = put_bits_ptr(p);
292 1 put_bits(p, 16, 0); /* patched later */
293 1 ff_put_string(p, AV_ISO21496_IDENTIFIER, 1);
294 1 flush_put_bits(p);
295
296 /* pre-validated by ff_mjpeg_add_gain_map_size() */
297 1 size = av_gain_map_params_to_iso21496(gmp, put_bits_ptr(p));
298
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 av_assert0(size >= 0);
299 1 skip_put_bytes(p, size);
300 1 AV_WB16(ptr, 2 + sizeof(AV_ISO21496_IDENTIFIER) + size);
301 }
302
303 /* comment */
304
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1641 times.
1641 if (!(avctx->flags & AV_CODEC_FLAG_BITEXACT)) {
305 ✗ put_marker(p, COM);
306 ✗ flush_put_bits(p);
307 ✗ ptr = put_bits_ptr(p);
308 ✗ put_bits(p, 16, 0); /* patched later */
309 ✗ ff_put_string(p, LIBAVCODEC_IDENT, 1);
310 ✗ size = strlen(LIBAVCODEC_IDENT)+3;
311 ✗ AV_WB16(ptr, size);
312 }
313
314
2/2
✓ Branch 0 taken 1416 times.
✓ Branch 1 taken 225 times.
1641 if (((avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
315
1/2
✓ Branch 0 taken 1416 times.
✗ Branch 1 not taken.
1416 avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
316
4/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1415 times.
✓ Branch 2 taken 26 times.
✓ Branch 3 taken 200 times.
1641 avctx->pix_fmt == AV_PIX_FMT_YUV444P) && avctx->color_range != AVCOL_RANGE_JPEG)
317
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1441 times.
1441 || avctx->color_range == AVCOL_RANGE_MPEG) {
318 200 put_marker(p, COM);
319 200 flush_put_bits(p);
320 200 ptr = put_bits_ptr(p);
321 200 put_bits(p, 16, 0); /* patched later */
322 200 ff_put_string(p, "CS=ITU601", 1);
323 200 size = strlen("CS=ITU601")+3;
324 200 AV_WB16(ptr, size);
325 }
326 1641 }
327
328 1845 void ff_mjpeg_init_hvsample(const AVCodecContext *avctx, int hsample[4], int vsample[4])
329 {
330
2/2
✓ Branch 0 taken 204 times.
✓ Branch 1 taken 1641 times.
1845 if (avctx->codec_id == AV_CODEC_ID_LJPEG &&
331
1/2
✓ Branch 0 taken 204 times.
✗ Branch 1 not taken.
204 ( avctx->pix_fmt == AV_PIX_FMT_BGR0
332
1/2
✓ Branch 0 taken 204 times.
✗ Branch 1 not taken.
204 || avctx->pix_fmt == AV_PIX_FMT_BGRA
333
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 204 times.
204 || avctx->pix_fmt == AV_PIX_FMT_BGR24)) {
334 ✗ vsample[0] = hsample[0] =
335 ✗ vsample[1] = hsample[1] =
336 ✗ vsample[2] = hsample[2] =
337 ✗ vsample[3] = hsample[3] = 1;
338
4/4
✓ Branch 0 taken 1844 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 202 times.
✓ Branch 3 taken 1642 times.
1845 } else if (avctx->pix_fmt == AV_PIX_FMT_YUV444P || avctx->pix_fmt == AV_PIX_FMT_YUVJ444P) {
339 203 vsample[0] = vsample[1] = vsample[2] = 2;
340 203 hsample[0] = hsample[1] = hsample[2] = 1;
341 } else {
342 int chroma_h_shift, chroma_v_shift;
343 1642 av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift,
344 &chroma_v_shift);
345 1642 vsample[0] = 2;
346 1642 vsample[1] = 2 >> chroma_v_shift;
347 1642 vsample[2] = 2 >> chroma_v_shift;
348 1642 hsample[0] = 2;
349 1642 hsample[1] = 2 >> chroma_h_shift;
350 1642 hsample[2] = 2 >> chroma_h_shift;
351 }
352 1845 }
353
354 1841 void ff_mjpeg_encode_picture_header(AVCodecContext *avctx, PutBitContext *pb,
355 const AVFrame *frame, const struct MJpegContext *m,
356 const uint8_t intra_matrix_permutation[64], int pred,
357 const uint16_t luma_intra_matrix[64],
358 const uint16_t chroma_intra_matrix[64],
359 int use_slices)
360 {
361 1841 const int lossless = !m;
362 int hsample[4], vsample[4];
363
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1841 times.
1841 int components = 3 + (avctx->pix_fmt == AV_PIX_FMT_BGRA);
364 int chroma_matrix;
365
366 1841 ff_mjpeg_init_hvsample(avctx, hsample, vsample);
367
368 1841 put_marker(pb, SOI);
369
370 // hack for AMV mjpeg format
371
2/2
✓ Branch 0 taken 200 times.
✓ Branch 1 taken 1641 times.
1841 if (avctx->codec_id == AV_CODEC_ID_AMV)
372 200 return;
373
374 1641 jpeg_put_comments(avctx, pb, frame, m);
375
376
3/4
✓ Branch 0 taken 1441 times.
✓ Branch 1 taken 200 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1441 times.
1641 chroma_matrix = !lossless && !!memcmp(luma_intra_matrix,
377 chroma_intra_matrix,
378 sizeof(luma_intra_matrix[0]) * 64);
379 1641 jpeg_table_header(avctx, pb, m, intra_matrix_permutation,
380 luma_intra_matrix, chroma_intra_matrix, hsample,
381 use_slices, chroma_matrix);
382
383
2/3
✓ Branch 0 taken 1441 times.
✓ Branch 1 taken 200 times.
✗ Branch 2 not taken.
1641 switch (avctx->codec_id) {
384 1441 case AV_CODEC_ID_MJPEG: put_marker(pb, SOF0 ); break;
385 200 case AV_CODEC_ID_LJPEG: put_marker(pb, SOF3 ); break;
386 ✗ default: av_unreachable("ff_mjpeg_encode_picture_header only called by "
387 "AMV, LJPEG, MJPEG and the former has been ruled out");
388 }
389
390 1641 put_bits(pb, 16, 8 + 3 * components);
391
3/4
✓ Branch 0 taken 200 times.
✓ Branch 1 taken 1441 times.
✓ Branch 2 taken 200 times.
✗ Branch 3 not taken.
1641 if (lossless && ( avctx->pix_fmt == AV_PIX_FMT_BGR0
392
1/2
✓ Branch 0 taken 200 times.
✗ Branch 1 not taken.
200 || avctx->pix_fmt == AV_PIX_FMT_BGRA
393
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 200 times.
200 || avctx->pix_fmt == AV_PIX_FMT_BGR24))
394 ✗ put_bits(pb, 8, 9); /* 9 bits/component RCT */
395 else
396 1641 put_bits(pb, 8, 8); /* 8 bits/component */
397 1641 put_bits(pb, 16, avctx->height);
398 1641 put_bits(pb, 16, avctx->width);
399 1641 put_bits(pb, 8, components); /* 3 or 4 components */
400
401 /* Y component */
402 1641 put_bits(pb, 8, 1); /* component number */
403 1641 put_bits(pb, 4, hsample[0]); /* H factor */
404 1641 put_bits(pb, 4, vsample[0]); /* V factor */
405 1641 put_bits(pb, 8, 0); /* select matrix */
406
407 /* Cb component */
408 1641 put_bits(pb, 8, 2); /* component number */
409 1641 put_bits(pb, 4, hsample[1]); /* H factor */
410 1641 put_bits(pb, 4, vsample[1]); /* V factor */
411
2/2
✓ Branch 0 taken 1441 times.
✓ Branch 1 taken 200 times.
1641 put_bits(pb, 8, lossless ? 0 : chroma_matrix); /* select matrix */
412
413 /* Cr component */
414 1641 put_bits(pb, 8, 3); /* component number */
415 1641 put_bits(pb, 4, hsample[2]); /* H factor */
416 1641 put_bits(pb, 4, vsample[2]); /* V factor */
417
2/2
✓ Branch 0 taken 1441 times.
✓ Branch 1 taken 200 times.
1641 put_bits(pb, 8, lossless ? 0 : chroma_matrix); /* select matrix */
418
419
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1641 times.
1641 if (components == 4) {
420 ✗ put_bits(pb, 8, 4); /* component number */
421 ✗ put_bits(pb, 4, hsample[3]); /* H factor */
422 ✗ put_bits(pb, 4, vsample[3]); /* V factor */
423 ✗ put_bits(pb, 8, 0); /* select matrix */
424 }
425
426 /* scan header */
427 1641 put_marker(pb, SOS);
428 1641 put_bits(pb, 16, 6 + 2*components); /* length */
429 1641 put_bits(pb, 8, components); /* 3 components */
430
431 /* Y component */
432 1641 put_bits(pb, 8, 1); /* index */
433 1641 put_bits(pb, 4, 0); /* DC huffman table index */
434 1641 put_bits(pb, 4, 0); /* AC huffman table index */
435
436 /* Cb component */
437 1641 put_bits(pb, 8, 2); /* index */
438 1641 put_bits(pb, 4, 1); /* DC huffman table index */
439 1641 put_bits(pb, 4, lossless ? 0 : 1); /* AC huffman table index */
440
441 /* Cr component */
442 1641 put_bits(pb, 8, 3); /* index */
443 1641 put_bits(pb, 4, 1); /* DC huffman table index */
444 1641 put_bits(pb, 4, lossless ? 0 : 1); /* AC huffman table index */
445
446
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1641 times.
1641 if (components == 4) {
447 /* Alpha component */
448 ✗ put_bits(pb, 8, 4); /* index */
449 ✗ put_bits(pb, 4, 0); /* DC huffman table index */
450 ✗ put_bits(pb, 4, 0); /* AC huffman table index */
451 }
452
453 1641 put_bits(pb, 8, pred); /* Ss (not used); pred only nonzero for LJPEG */
454
455
2/3
✓ Branch 0 taken 1441 times.
✓ Branch 1 taken 200 times.
✗ Branch 2 not taken.
1641 switch (avctx->codec_id) {
456 1441 case AV_CODEC_ID_MJPEG: put_bits(pb, 8, 63); break; /* Se (not used) */
457 200 case AV_CODEC_ID_LJPEG: put_bits(pb, 8, 0); break; /* not used */
458 ✗ default: av_unreachable("Only LJPEG, MJPEG possible here");
459 }
460
461 1641 put_bits(pb, 8, 0); /* Ah/Al (not used) */
462 }
463
464 7141 void ff_mjpeg_escape_FF(PutBitContext *pb, int start)
465 {
466 int size;
467 int i, ff_count;
468 7141 uint8_t *buf = pb->buf + start;
469 7141 int align= (-(size_t)(buf))&3;
470 7141 int pad = (-put_bits_count(pb))&7;
471
472
2/2
✓ Branch 0 taken 6222 times.
✓ Branch 1 taken 919 times.
7141 if (pad)
473 6222 put_bits(pb, pad, (1<<pad)-1);
474
475 7141 flush_put_bits(pb);
476 7141 size = put_bytes_output(pb) - start;
477
478 7141 ff_count=0;
479
3/4
✓ Branch 0 taken 18420 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11279 times.
✓ Branch 3 taken 7141 times.
18420 for(i=0; i<size && i<align; i++){
480
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 11261 times.
11279 if(buf[i]==0xFF) ff_count++;
481 }
482
2/2
✓ Branch 0 taken 2540194 times.
✓ Branch 1 taken 7141 times.
2547335 for(; i<size-15; i+=16){
483 int acc, v;
484
485 2540194 v= *(uint32_t*)(&buf[i]);
486 2540194 acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
487 2540194 v= *(uint32_t*)(&buf[i+4]);
488 2540194 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
489 2540194 v= *(uint32_t*)(&buf[i+8]);
490 2540194 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
491 2540194 v= *(uint32_t*)(&buf[i+12]);
492 2540194 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
493
494 2540194 acc>>=4;
495 2540194 acc+= (acc>>16);
496 2540194 acc+= (acc>>8);
497 2540194 ff_count+= acc&0xFF;
498 }
499
2/2
✓ Branch 0 taken 53291 times.
✓ Branch 1 taken 7141 times.
60432 for(; i<size; i++){
500
2/2
✓ Branch 0 taken 541 times.
✓ Branch 1 taken 52750 times.
53291 if(buf[i]==0xFF) ff_count++;
501 }
502
503
2/2
✓ Branch 0 taken 124 times.
✓ Branch 1 taken 7017 times.
7141 if(ff_count==0) return;
504
505 7017 skip_put_bytes(pb, ff_count);
506
507
2/2
✓ Branch 0 taken 39150826 times.
✓ Branch 1 taken 7017 times.
39157843 for(i=size-1; ff_count; i--){
508 39150826 int v= buf[i];
509
510
2/2
✓ Branch 0 taken 200332 times.
✓ Branch 1 taken 38950494 times.
39150826 if(v==0xFF){
511 200332 buf[i+ff_count]= 0;
512 200332 ff_count--;
513 }
514
515 39150826 buf[i+ff_count]= v;
516 }
517 }
518
519 /* isn't this function nicer than the one in the libjpeg ? */
520 3520 void ff_mjpeg_build_huffman_codes(uint8_t *huff_size, uint16_t *huff_code,
521 const uint8_t *bits_table,
522 const uint8_t *val_table)
523 {
524 int k, code;
525
526 3520 k = 0;
527 3520 code = 0;
528
2/2
✓ Branch 0 taken 56320 times.
✓ Branch 1 taken 3520 times.
59840 for (int i = 1; i <= 16; i++) {
529 56320 int nb = bits_table[i];
530
2/2
✓ Branch 0 taken 88302 times.
✓ Branch 1 taken 56320 times.
144622 for (int j = 0; j < nb; j++) {
531 88302 int sym = val_table[k++];
532 88302 huff_size[sym] = i;
533 88302 huff_code[sym] = code;
534 88302 code++;
535 }
536 56320 code <<= 1;
537 }
538 3520 }
539
540 1841 void ff_mjpeg_encode_picture_trailer(PutBitContext *pb, int header_bits)
541 {
542 av_assert1((header_bits & 7) == 0);
543
544 1841 put_marker(pb, EOI);
545 1841 }
546
547 24332700 void ff_mjpeg_encode_dc(PutBitContext *pb, int val,
548 const uint8_t huff_size[], const uint16_t huff_code[])
549 {
550 int mant, nbits;
551
552
2/2
✓ Branch 0 taken 2726178 times.
✓ Branch 1 taken 21606522 times.
24332700 if (val == 0) {
553 2726178 put_bits(pb, huff_size[0], huff_code[0]);
554 } else {
555 21606522 mant = val;
556
2/2
✓ Branch 0 taken 9428715 times.
✓ Branch 1 taken 12177807 times.
21606522 if (val < 0) {
557 9428715 val = -val;
558 9428715 mant--;
559 }
560
561 21606522 nbits= av_log2_16bit(val) + 1;
562
563 21606522 put_bits(pb, huff_size[nbits], huff_code[nbits]);
564
565 21606522 put_sbits(pb, nbits, mant);
566 }
567 24332700 }
568
569 41 int ff_mjpeg_encode_check_pix_fmt(AVCodecContext *avctx)
570 {
571
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 8 times.
41 if (avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL &&
572
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 avctx->color_range != AVCOL_RANGE_JPEG &&
573 ✗ (avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
574 ✗ avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
575 ✗ avctx->pix_fmt == AV_PIX_FMT_YUV444P ||
576 ✗ avctx->color_range == AVCOL_RANGE_MPEG)) {
577 ✗ av_log(avctx, AV_LOG_ERROR,
578 "Non full-range YUV is non-standard, set strict_std_compliance "
579 "to at most unofficial to use it.\n");
580 ✗ return AVERROR(EINVAL);
581 }
582 41 return 0;
583 }
584