FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mjpegenc_common.c
Date: 2022-01-18 10:05:52
Exec Total Coverage
Lines: 191 240 79.6%
Branches: 79 122 64.8%

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 "mjpegenc_common.h"
35 #include "mjpeg.h"
36
37 /* table_class: 0 = DC coef, 1 = AC coefs */
38 5752 static int put_huffman_table(PutBitContext *p, int table_class, int table_id,
39 const uint8_t *bits_table, const uint8_t *value_table)
40 {
41 int n, i;
42
43 5752 put_bits(p, 4, table_class);
44 5752 put_bits(p, 4, table_id);
45
46 5752 n = 0;
47
2/2
✓ Branch 0 taken 92032 times.
✓ Branch 1 taken 5752 times.
97784 for(i=1;i<=16;i++) {
48 92032 n += bits_table[i];
49 92032 put_bits(p, 8, bits_table[i]);
50 }
51
52
2/2
✓ Branch 0 taken 179183 times.
✓ Branch 1 taken 5752 times.
184935 for(i=0;i<n;i++)
53 179183 put_bits(p, 8, value_table[i]);
54
55 5752 return n + 17;
56 }
57
58 1438 static void jpeg_table_header(AVCodecContext *avctx, PutBitContext *p,
59 MJpegContext *m,
60 ScanTable *intra_scantable,
61 uint16_t luma_intra_matrix[64],
62 uint16_t chroma_intra_matrix[64],
63 int hsample[3])
64 {
65 int i, j, size;
66 uint8_t *ptr;
67
68
2/2
✓ Branch 0 taken 1238 times.
✓ Branch 1 taken 200 times.
1438 if (m) {
69
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1238 times.
1238 int matrix_count = 1 + !!memcmp(luma_intra_matrix,
70 chroma_intra_matrix,
71 sizeof(luma_intra_matrix[0]) * 64);
72
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1238 times.
1238 if (m->force_duplicated_matrix)
73 matrix_count = 2;
74 /* quant matrixes */
75 1238 put_marker(p, DQT);
76 1238 put_bits(p, 16, 2 + matrix_count * (1 + 64));
77 1238 put_bits(p, 4, 0); /* 8 bit precision */
78 1238 put_bits(p, 4, 0); /* table 0 */
79
2/2
✓ Branch 0 taken 79232 times.
✓ Branch 1 taken 1238 times.
80470 for (int i = 0; i < 64; i++) {
80 79232 uint8_t j = intra_scantable->permutated[i];
81 79232 put_bits(p, 8, luma_intra_matrix[j]);
82 }
83
84
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1238 times.
1238 if (matrix_count > 1) {
85 put_bits(p, 4, 0); /* 8 bit precision */
86 put_bits(p, 4, 1); /* table 1 */
87 for(i=0;i<64;i++) {
88 j = intra_scantable->permutated[i];
89 put_bits(p, 8, chroma_intra_matrix[j]);
90 }
91 }
92 }
93
94
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1438 times.
1438 if(avctx->active_thread_type & FF_THREAD_SLICE){
95 put_marker(p, DRI);
96 put_bits(p, 16, 4);
97 put_bits(p, 16, (avctx->width-1)/(8*hsample[0]) + 1);
98 }
99
100 /* huffman table */
101 1438 put_marker(p, DHT);
102 1438 flush_put_bits(p);
103 1438 ptr = put_bits_ptr(p);
104 1438 put_bits(p, 16, 0); /* patched later */
105 1438 size = 2;
106
107 // Only MJPEG can have a variable Huffman variable. All other
108 // formats use the default Huffman table.
109
3/4
✓ Branch 0 taken 1238 times.
✓ Branch 1 taken 200 times.
✓ Branch 2 taken 1238 times.
✗ Branch 3 not taken.
1438 if (m && m->huffman == HUFFMAN_TABLE_OPTIMAL) {
110 2476 size += put_huffman_table(p, 0, 0, m->bits_dc_luminance,
111 1238 m->val_dc_luminance);
112 2476 size += put_huffman_table(p, 0, 1, m->bits_dc_chrominance,
113 1238 m->val_dc_chrominance);
114
115 2476 size += put_huffman_table(p, 1, 0, m->bits_ac_luminance,
116 1238 m->val_ac_luminance);
117 2476 size += put_huffman_table(p, 1, 1, m->bits_ac_chrominance,
118 1238 m->val_ac_chrominance);
119 } else {
120 200 size += put_huffman_table(p, 0, 0, ff_mjpeg_bits_dc_luminance,
121 ff_mjpeg_val_dc);
122 200 size += put_huffman_table(p, 0, 1, ff_mjpeg_bits_dc_chrominance,
123 ff_mjpeg_val_dc);
124
125 200 size += put_huffman_table(p, 1, 0, ff_mjpeg_bits_ac_luminance,
126 ff_mjpeg_val_ac_luminance);
127 200 size += put_huffman_table(p, 1, 1, ff_mjpeg_bits_ac_chrominance,
128 ff_mjpeg_val_ac_chrominance);
129 }
130 1438 AV_WB16(ptr, size);
131 1438 }
132
133 1438 static void jpeg_put_comments(AVCodecContext *avctx, PutBitContext *p)
134 {
135 int size;
136 uint8_t *ptr;
137
138
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1438 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1438 if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0) {
139 AVRational sar = avctx->sample_aspect_ratio;
140
141 if (sar.num > 65535 || sar.den > 65535) {
142 if (!av_reduce(&sar.num, &sar.den, avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den, 65535))
143 av_log(avctx, AV_LOG_WARNING,
144 "Cannot store exact aspect ratio %d:%d\n",
145 avctx->sample_aspect_ratio.num,
146 avctx->sample_aspect_ratio.den);
147 }
148
149 /* JFIF header */
150 put_marker(p, APP0);
151 put_bits(p, 16, 16);
152 ff_put_string(p, "JFIF", 1); /* this puts the trailing zero-byte too */
153 /* The most significant byte is used for major revisions, the least
154 * significant byte for minor revisions. Version 1.02 is the current
155 * released revision. */
156 put_bits(p, 16, 0x0102);
157 put_bits(p, 8, 0); /* units type: 0 - aspect ratio */
158 put_bits(p, 16, sar.num);
159 put_bits(p, 16, sar.den);
160 put_bits(p, 8, 0); /* thumbnail width */
161 put_bits(p, 8, 0); /* thumbnail height */
162 }
163
164 /* comment */
165
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1438 times.
1438 if (!(avctx->flags & AV_CODEC_FLAG_BITEXACT)) {
166 put_marker(p, COM);
167 flush_put_bits(p);
168 ptr = put_bits_ptr(p);
169 put_bits(p, 16, 0); /* patched later */
170 ff_put_string(p, LIBAVCODEC_IDENT, 1);
171 size = strlen(LIBAVCODEC_IDENT)+3;
172 AV_WB16(ptr, size);
173 }
174
175
2/2
✓ Branch 0 taken 1238 times.
✓ Branch 1 taken 200 times.
1438 if (((avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
176
1/2
✓ Branch 0 taken 1238 times.
✗ Branch 1 not taken.
1238 avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
177
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1238 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 200 times.
1438 avctx->pix_fmt == AV_PIX_FMT_YUV444P) && avctx->color_range != AVCOL_RANGE_JPEG)
178
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1238 times.
1238 || avctx->color_range == AVCOL_RANGE_MPEG) {
179 200 put_marker(p, COM);
180 200 flush_put_bits(p);
181 200 ptr = put_bits_ptr(p);
182 200 put_bits(p, 16, 0); /* patched later */
183 200 ff_put_string(p, "CS=ITU601", 1);
184 200 size = strlen("CS=ITU601")+3;
185 200 AV_WB16(ptr, size);
186 }
187 1438 }
188
189 1642 void ff_mjpeg_init_hvsample(AVCodecContext *avctx, int hsample[4], int vsample[4])
190 {
191
2/2
✓ Branch 0 taken 204 times.
✓ Branch 1 taken 1438 times.
1642 if (avctx->codec_id == AV_CODEC_ID_LJPEG &&
192
1/2
✓ Branch 0 taken 204 times.
✗ Branch 1 not taken.
204 ( avctx->pix_fmt == AV_PIX_FMT_BGR0
193
1/2
✓ Branch 0 taken 204 times.
✗ Branch 1 not taken.
204 || avctx->pix_fmt == AV_PIX_FMT_BGRA
194
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 204 times.
204 || avctx->pix_fmt == AV_PIX_FMT_BGR24)) {
195 vsample[0] = hsample[0] =
196 vsample[1] = hsample[1] =
197 vsample[2] = hsample[2] =
198 vsample[3] = hsample[3] = 1;
199
3/4
✓ Branch 0 taken 1642 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 200 times.
✓ Branch 3 taken 1442 times.
1642 } else if (avctx->pix_fmt == AV_PIX_FMT_YUV444P || avctx->pix_fmt == AV_PIX_FMT_YUVJ444P) {
200 200 vsample[0] = vsample[1] = vsample[2] = 2;
201 200 hsample[0] = hsample[1] = hsample[2] = 1;
202 } else {
203 int chroma_h_shift, chroma_v_shift;
204 1442 av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift,
205 &chroma_v_shift);
206 1442 vsample[0] = 2;
207 1442 vsample[1] = 2 >> chroma_v_shift;
208 1442 vsample[2] = 2 >> chroma_v_shift;
209 1442 hsample[0] = 2;
210 1442 hsample[1] = 2 >> chroma_h_shift;
211 1442 hsample[2] = 2 >> chroma_h_shift;
212 }
213 1642 }
214
215 1638 void ff_mjpeg_encode_picture_header(AVCodecContext *avctx, PutBitContext *pb,
216 MJpegContext *m,
217 ScanTable *intra_scantable, int pred,
218 uint16_t luma_intra_matrix[64],
219 uint16_t chroma_intra_matrix[64])
220 {
221 1638 const int lossless = !m;
222 int hsample[4], vsample[4];
223
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1638 times.
1638 int components = 3 + (avctx->pix_fmt == AV_PIX_FMT_BGRA);
224 1638 int chroma_matrix = !!memcmp(luma_intra_matrix,
225 chroma_intra_matrix,
226 sizeof(luma_intra_matrix[0])*64);
227
228 1638 ff_mjpeg_init_hvsample(avctx, hsample, vsample);
229
230 1638 put_marker(pb, SOI);
231
232 // hack for AMV mjpeg format
233
2/2
✓ Branch 0 taken 200 times.
✓ Branch 1 taken 1438 times.
1638 if (avctx->codec_id == AV_CODEC_ID_AMV)
234 200 return;
235
236 1438 jpeg_put_comments(avctx, pb);
237
238 1438 jpeg_table_header(avctx, pb, m, intra_scantable,
239 luma_intra_matrix, chroma_intra_matrix, hsample);
240
241
2/3
✓ Branch 0 taken 1238 times.
✓ Branch 1 taken 200 times.
✗ Branch 2 not taken.
1438 switch (avctx->codec_id) {
242 1238 case AV_CODEC_ID_MJPEG: put_marker(pb, SOF0 ); break;
243 200 case AV_CODEC_ID_LJPEG: put_marker(pb, SOF3 ); break;
244 default: av_assert0(0);
245 }
246
247 1438 put_bits(pb, 16, 17);
248
3/4
✓ Branch 0 taken 200 times.
✓ Branch 1 taken 1238 times.
✓ Branch 2 taken 200 times.
✗ Branch 3 not taken.
1438 if (lossless && ( avctx->pix_fmt == AV_PIX_FMT_BGR0
249
1/2
✓ Branch 0 taken 200 times.
✗ Branch 1 not taken.
200 || avctx->pix_fmt == AV_PIX_FMT_BGRA
250
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 200 times.
200 || avctx->pix_fmt == AV_PIX_FMT_BGR24))
251 put_bits(pb, 8, 9); /* 9 bits/component RCT */
252 else
253 1438 put_bits(pb, 8, 8); /* 8 bits/component */
254 1438 put_bits(pb, 16, avctx->height);
255 1438 put_bits(pb, 16, avctx->width);
256 1438 put_bits(pb, 8, components); /* 3 or 4 components */
257
258 /* Y component */
259 1438 put_bits(pb, 8, 1); /* component number */
260 1438 put_bits(pb, 4, hsample[0]); /* H factor */
261 1438 put_bits(pb, 4, vsample[0]); /* V factor */
262 1438 put_bits(pb, 8, 0); /* select matrix */
263
264 /* Cb component */
265 1438 put_bits(pb, 8, 2); /* component number */
266 1438 put_bits(pb, 4, hsample[1]); /* H factor */
267 1438 put_bits(pb, 4, vsample[1]); /* V factor */
268
2/2
✓ Branch 0 taken 1238 times.
✓ Branch 1 taken 200 times.
1438 put_bits(pb, 8, lossless ? 0 : chroma_matrix); /* select matrix */
269
270 /* Cr component */
271 1438 put_bits(pb, 8, 3); /* component number */
272 1438 put_bits(pb, 4, hsample[2]); /* H factor */
273 1438 put_bits(pb, 4, vsample[2]); /* V factor */
274
2/2
✓ Branch 0 taken 1238 times.
✓ Branch 1 taken 200 times.
1438 put_bits(pb, 8, lossless ? 0 : chroma_matrix); /* select matrix */
275
276
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1438 times.
1438 if (components == 4) {
277 put_bits(pb, 8, 4); /* component number */
278 put_bits(pb, 4, hsample[3]); /* H factor */
279 put_bits(pb, 4, vsample[3]); /* V factor */
280 put_bits(pb, 8, 0); /* select matrix */
281 }
282
283 /* scan header */
284 1438 put_marker(pb, SOS);
285 1438 put_bits(pb, 16, 6 + 2*components); /* length */
286 1438 put_bits(pb, 8, components); /* 3 components */
287
288 /* Y component */
289 1438 put_bits(pb, 8, 1); /* index */
290 1438 put_bits(pb, 4, 0); /* DC huffman table index */
291 1438 put_bits(pb, 4, 0); /* AC huffman table index */
292
293 /* Cb component */
294 1438 put_bits(pb, 8, 2); /* index */
295 1438 put_bits(pb, 4, 1); /* DC huffman table index */
296 1438 put_bits(pb, 4, lossless ? 0 : 1); /* AC huffman table index */
297
298 /* Cr component */
299 1438 put_bits(pb, 8, 3); /* index */
300 1438 put_bits(pb, 4, 1); /* DC huffman table index */
301 1438 put_bits(pb, 4, lossless ? 0 : 1); /* AC huffman table index */
302
303
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1438 times.
1438 if (components == 4) {
304 /* Alpha component */
305 put_bits(pb, 8, 4); /* index */
306 put_bits(pb, 4, 0); /* DC huffman table index */
307 put_bits(pb, 4, 0); /* AC huffman table index */
308 }
309
310 1438 put_bits(pb, 8, pred); /* Ss (not used); pred only nonzero for LJPEG */
311
312
2/3
✓ Branch 0 taken 1238 times.
✓ Branch 1 taken 200 times.
✗ Branch 2 not taken.
1438 switch (avctx->codec_id) {
313 1238 case AV_CODEC_ID_MJPEG: put_bits(pb, 8, 63); break; /* Se (not used) */
314 200 case AV_CODEC_ID_LJPEG: put_bits(pb, 8, 0); break; /* not used */
315 default: av_assert0(0);
316 }
317
318 1438 put_bits(pb, 8, 0); /* Ah/Al (not used) */
319 }
320
321 1638 void ff_mjpeg_escape_FF(PutBitContext *pb, int start)
322 {
323 int size;
324 int i, ff_count;
325 1638 uint8_t *buf = pb->buf + start;
326 1638 int align= (-(size_t)(buf))&3;
327 1638 int pad = (-put_bits_count(pb))&7;
328
329
2/2
✓ Branch 0 taken 1419 times.
✓ Branch 1 taken 219 times.
1638 if (pad)
330 1419 put_bits(pb, pad, (1<<pad)-1);
331
332 1638 flush_put_bits(pb);
333 1638 size = put_bytes_output(pb) - start;
334
335 1638 ff_count=0;
336
3/4
✓ Branch 0 taken 4487 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2849 times.
✓ Branch 3 taken 1638 times.
4487 for(i=0; i<size && i<align; i++){
337
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 2840 times.
2849 if(buf[i]==0xFF) ff_count++;
338 }
339
2/2
✓ Branch 0 taken 2329637 times.
✓ Branch 1 taken 1638 times.
2331275 for(; i<size-15; i+=16){
340 int acc, v;
341
342 2329637 v= *(uint32_t*)(&buf[i]);
343 2329637 acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
344 2329637 v= *(uint32_t*)(&buf[i+4]);
345 2329637 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
346 2329637 v= *(uint32_t*)(&buf[i+8]);
347 2329637 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
348 2329637 v= *(uint32_t*)(&buf[i+12]);
349 2329637 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
350
351 2329637 acc>>=4;
352 2329637 acc+= (acc>>16);
353 2329637 acc+= (acc>>8);
354 2329637 ff_count+= acc&0xFF;
355 }
356
2/2
✓ Branch 0 taken 12046 times.
✓ Branch 1 taken 1638 times.
13684 for(; i<size; i++){
357
2/2
✓ Branch 0 taken 85 times.
✓ Branch 1 taken 11961 times.
12046 if(buf[i]==0xFF) ff_count++;
358 }
359
360
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 1607 times.
1638 if(ff_count==0) return;
361
362 1607 flush_put_bits(pb);
363 1607 skip_put_bytes(pb, ff_count);
364
365
2/2
✓ Branch 0 taken 36520016 times.
✓ Branch 1 taken 1607 times.
36521623 for(i=size-1; ff_count; i--){
366 36520016 int v= buf[i];
367
368
2/2
✓ Branch 0 taken 138316 times.
✓ Branch 1 taken 36381700 times.
36520016 if(v==0xFF){
369 138316 buf[i+ff_count]= 0;
370 138316 ff_count--;
371 }
372
373 36520016 buf[i+ff_count]= v;
374 }
375 }
376
377 /* isn't this function nicer than the one in the libjpeg ? */
378 5080 void ff_mjpeg_build_huffman_codes(uint8_t *huff_size, uint16_t *huff_code,
379 const uint8_t *bits_table,
380 const uint8_t *val_table)
381 {
382 int k, code;
383
384 5080 k = 0;
385 5080 code = 0;
386
2/2
✓ Branch 0 taken 81280 times.
✓ Branch 1 taken 5080 times.
86360 for (int i = 1; i <= 16; i++) {
387 81280 int nb = bits_table[i];
388
2/2
✓ Branch 0 taken 120119 times.
✓ Branch 1 taken 81280 times.
201399 for (int j = 0; j < nb; j++) {
389 120119 int sym = val_table[k++];
390 120119 huff_size[sym] = i;
391 120119 huff_code[sym] = code;
392 120119 code++;
393 }
394 81280 code <<= 1;
395 }
396 5080 }
397
398 1638 void ff_mjpeg_encode_picture_trailer(PutBitContext *pb, int header_bits)
399 {
400 av_assert1((header_bits & 7) == 0);
401
402 1638 put_marker(pb, EOI);
403 1638 }
404
405 23255400 void ff_mjpeg_encode_dc(PutBitContext *pb, int val,
406 uint8_t *huff_size, uint16_t *huff_code)
407 {
408 int mant, nbits;
409
410
2/2
✓ Branch 0 taken 2701595 times.
✓ Branch 1 taken 20553805 times.
23255400 if (val == 0) {
411 2701595 put_bits(pb, huff_size[0], huff_code[0]);
412 } else {
413 20553805 mant = val;
414
2/2
✓ Branch 0 taken 8922845 times.
✓ Branch 1 taken 11630960 times.
20553805 if (val < 0) {
415 8922845 val = -val;
416 8922845 mant--;
417 }
418
419 20553805 nbits= av_log2_16bit(val) + 1;
420
421 20553805 put_bits(pb, huff_size[nbits], huff_code[nbits]);
422
423 20553805 put_sbits(pb, nbits, mant);
424 }
425 23255400 }
426
427 34 int ff_mjpeg_encode_check_pix_fmt(AVCodecContext *avctx)
428 {
429
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 8 times.
34 if (avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL &&
430
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 avctx->color_range != AVCOL_RANGE_JPEG &&
431 (avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
432 avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
433 avctx->pix_fmt == AV_PIX_FMT_YUV444P ||
434 avctx->color_range == AVCOL_RANGE_MPEG)) {
435 av_log(avctx, AV_LOG_ERROR,
436 "Non full-range YUV is non-standard, set strict_std_compliance "
437 "to at most unofficial to use it.\n");
438 return AVERROR(EINVAL);
439 }
440 34 return 0;
441 }
442