FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mjpegenc.c
Date: 2022-01-18 10:05:52
Exec Total Coverage
Lines: 282 313 90.1%
Branches: 92 114 80.7%

Line Branch Exec Source
1 /*
2 * MJPEG encoder
3 * Copyright (c) 2000, 2001 Fabrice Bellard
4 * Copyright (c) 2003 Alex Beregszaszi
5 * Copyright (c) 2003-2004 Michael Niedermayer
6 *
7 * Support for external huffman table, various fixes (AVID workaround),
8 * aspecting, new decode_frame mechanism and apple mjpeg-b support
9 * by Alex Beregszaszi
10 *
11 * This file is part of FFmpeg.
12 *
13 * FFmpeg is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
17 *
18 * FFmpeg is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
22 *
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with FFmpeg; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 */
27
28 /**
29 * @file
30 * MJPEG encoder.
31 */
32
33 #include "libavutil/pixdesc.h"
34
35 #include "avcodec.h"
36 #include "jpegtables.h"
37 #include "mjpegenc_common.h"
38 #include "mjpegenc_huffman.h"
39 #include "mpegvideo.h"
40 #include "mjpeg.h"
41 #include "mjpegenc.h"
42 #include "profiles.h"
43
44 /* The following is the private context of MJPEG/AMV decoder.
45 * Note that when using slice threading only the main thread's
46 * MpegEncContext is followed by a MjpegContext; the other threads
47 * can access this shared context via MpegEncContext.mjpeg. */
48 typedef struct MJPEGEncContext {
49 MpegEncContext mpeg;
50 MJpegContext mjpeg;
51 } MJPEGEncContext;
52
53 2536 static av_cold void init_uni_ac_vlc(const uint8_t huff_size_ac[256],
54 uint8_t *uni_ac_vlc_len)
55 {
56
2/2
✓ Branch 0 taken 324608 times.
✓ Branch 1 taken 2536 times.
327144 for (int i = 0; i < 128; i++) {
57 324608 int level = i - 64;
58
2/2
✓ Branch 0 taken 2536 times.
✓ Branch 1 taken 322072 times.
324608 if (!level)
59 2536 continue;
60
2/2
✓ Branch 0 taken 20612608 times.
✓ Branch 1 taken 322072 times.
20934680 for (int run = 0; run < 64; run++) {
61 int len, code, nbits;
62 20612608 int alevel = FFABS(level);
63
64 20612608 len = (run >> 4) * huff_size_ac[0xf0];
65
66 20612608 nbits= av_log2_16bit(alevel) + 1;
67 20612608 code = ((15&run) << 4) | nbits;
68
69 20612608 len += huff_size_ac[code] + nbits;
70
71 20612608 uni_ac_vlc_len[UNI_AC_ENC_INDEX(run, i)] = len;
72 // We ignore EOB as its just a constant which does not change generally
73 }
74 }
75 2536 }
76
77 1438 static void mjpeg_encode_picture_header(MpegEncContext *s)
78 {
79 1438 ff_mjpeg_encode_picture_header(s->avctx, &s->pb, s->mjpeg_ctx,
80 &s->intra_scantable, 0,
81 1438 s->intra_matrix, s->chroma_intra_matrix);
82
83 1438 s->esc_pos = put_bytes_count(&s->pb, 0);
84
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1438 times.
1438 for (int i = 1; i < s->slice_context_count; i++)
85 s->thread_context[i]->esc_pos = 0;
86 1438 }
87
88 1438 void ff_mjpeg_amv_encode_picture_header(MpegEncContext *s)
89 {
90 1438 MJPEGEncContext *const m = (MJPEGEncContext*)s;
91 av_assert2(s->mjpeg_ctx == &m->mjpeg);
92 /* s->huffman == HUFFMAN_TABLE_OPTIMAL can only be true for MJPEG. */
93
2/2
✓ Branch 0 taken 200 times.
✓ Branch 1 taken 1238 times.
1438 if (!CONFIG_MJPEG_ENCODER || m->mjpeg.huffman != HUFFMAN_TABLE_OPTIMAL)
94 200 mjpeg_encode_picture_header(s);
95 1438 }
96
97 #if CONFIG_MJPEG_ENCODER
98 /**
99 * Encodes and outputs the entire frame in the JPEG format.
100 *
101 * @param s The MpegEncContext.
102 */
103 1238 static void mjpeg_encode_picture_frame(MpegEncContext *s)
104 {
105 int nbits, code, table_id;
106 1238 MJpegContext *m = s->mjpeg_ctx;
107 1238 uint8_t *huff_size[4] = { m->huff_size_dc_luminance,
108 1238 m->huff_size_dc_chrominance,
109 1238 m->huff_size_ac_luminance,
110 1238 m->huff_size_ac_chrominance };
111 1238 uint16_t *huff_code[4] = { m->huff_code_dc_luminance,
112 1238 m->huff_code_dc_chrominance,
113 1238 m->huff_code_ac_luminance,
114 1238 m->huff_code_ac_chrominance };
115 1238 size_t total_bits = 0;
116 size_t bytes_needed;
117
118 1238 s->header_bits = get_bits_diff(s);
119 // Estimate the total size first
120
2/2
✓ Branch 0 taken 28811107 times.
✓ Branch 1 taken 1238 times.
28812345 for (int i = 0; i < m->huff_ncode; i++) {
121 28811107 table_id = m->huff_buffer[i].table_id;
122 28811107 code = m->huff_buffer[i].code;
123 28811107 nbits = code & 0xf;
124
125 28811107 total_bits += huff_size[table_id][code] + nbits;
126 }
127
128 1238 bytes_needed = (total_bits + 7) / 8;
129 1238 ff_mpv_reallocate_putbitbuffer(s, bytes_needed, bytes_needed);
130
131
2/2
✓ Branch 0 taken 28811107 times.
✓ Branch 1 taken 1238 times.
28812345 for (int i = 0; i < m->huff_ncode; i++) {
132 28811107 table_id = m->huff_buffer[i].table_id;
133 28811107 code = m->huff_buffer[i].code;
134 28811107 nbits = code & 0xf;
135
136 28811107 put_bits(&s->pb, huff_size[table_id][code], huff_code[table_id][code]);
137
2/2
✓ Branch 0 taken 26007642 times.
✓ Branch 1 taken 2803465 times.
28811107 if (nbits != 0) {
138 26007642 put_sbits(&s->pb, nbits, m->huff_buffer[i].mant);
139 }
140 }
141
142 1238 m->huff_ncode = 0;
143 1238 s->i_tex_bits = get_bits_diff(s);
144 1238 }
145
146 /**
147 * Builds all 4 optimal Huffman tables.
148 *
149 * Uses the data stored in the JPEG buffer to compute the tables.
150 * Stores the Huffman tables in the bits_* and val_* arrays in the MJpegContext.
151 *
152 * @param m MJpegContext containing the JPEG buffer.
153 */
154 1238 static void mjpeg_build_optimal_huffman(MJpegContext *m)
155 {
156 MJpegEncHuffmanContext dc_luminance_ctx;
157 MJpegEncHuffmanContext dc_chrominance_ctx;
158 MJpegEncHuffmanContext ac_luminance_ctx;
159 MJpegEncHuffmanContext ac_chrominance_ctx;
160 1238 MJpegEncHuffmanContext *ctx[4] = { &dc_luminance_ctx,
161 &dc_chrominance_ctx,
162 &ac_luminance_ctx,
163 &ac_chrominance_ctx };
164
2/2
✓ Branch 0 taken 4952 times.
✓ Branch 1 taken 1238 times.
6190 for (int i = 0; i < 4; i++)
165 4952 ff_mjpeg_encode_huffman_init(ctx[i]);
166
167
2/2
✓ Branch 0 taken 28811107 times.
✓ Branch 1 taken 1238 times.
28812345 for (int i = 0; i < m->huff_ncode; i++) {
168 28811107 int table_id = m->huff_buffer[i].table_id;
169 28811107 int code = m->huff_buffer[i].code;
170
171 28811107 ff_mjpeg_encode_huffman_increment(ctx[table_id], code);
172 }
173
174 1238 ff_mjpeg_encode_huffman_close(&dc_luminance_ctx,
175 1238 m->bits_dc_luminance,
176 1238 m->val_dc_luminance, 12);
177 1238 ff_mjpeg_encode_huffman_close(&dc_chrominance_ctx,
178 1238 m->bits_dc_chrominance,
179 1238 m->val_dc_chrominance, 12);
180 1238 ff_mjpeg_encode_huffman_close(&ac_luminance_ctx,
181 1238 m->bits_ac_luminance,
182 1238 m->val_ac_luminance, 256);
183 1238 ff_mjpeg_encode_huffman_close(&ac_chrominance_ctx,
184 1238 m->bits_ac_chrominance,
185 1238 m->val_ac_chrominance, 256);
186
187 1238 ff_mjpeg_build_huffman_codes(m->huff_size_dc_luminance,
188 1238 m->huff_code_dc_luminance,
189 1238 m->bits_dc_luminance,
190 1238 m->val_dc_luminance);
191 1238 ff_mjpeg_build_huffman_codes(m->huff_size_dc_chrominance,
192 1238 m->huff_code_dc_chrominance,
193 1238 m->bits_dc_chrominance,
194 1238 m->val_dc_chrominance);
195 1238 ff_mjpeg_build_huffman_codes(m->huff_size_ac_luminance,
196 1238 m->huff_code_ac_luminance,
197 1238 m->bits_ac_luminance,
198 1238 m->val_ac_luminance);
199 1238 ff_mjpeg_build_huffman_codes(m->huff_size_ac_chrominance,
200 1238 m->huff_code_ac_chrominance,
201 1238 m->bits_ac_chrominance,
202 1238 m->val_ac_chrominance);
203 1238 }
204 #endif
205
206 /**
207 * Writes the complete JPEG frame when optimal huffman tables are enabled,
208 * otherwise writes the stuffing.
209 *
210 * Header + values + stuffing.
211 *
212 * @param s The MpegEncContext.
213 * @return int Error code, 0 if successful.
214 */
215 1438 int ff_mjpeg_encode_stuffing(MpegEncContext *s)
216 {
217 1438 MJpegContext *const m = s->mjpeg_ctx;
218 1438 PutBitContext *pbc = &s->pb;
219 1438 int mb_y = s->mb_y - !s->mb_x;
220 int ret;
221
222 #if CONFIG_MJPEG_ENCODER
223
2/2
✓ Branch 0 taken 1238 times.
✓ Branch 1 taken 200 times.
1438 if (m->huffman == HUFFMAN_TABLE_OPTIMAL) {
224
225 1238 mjpeg_build_optimal_huffman(m);
226
227 // Replace the VLCs with the optimal ones.
228 // The default ones may be used for trellis during quantization.
229 1238 init_uni_ac_vlc(m->huff_size_ac_luminance, m->uni_ac_vlc_len);
230 1238 init_uni_ac_vlc(m->huff_size_ac_chrominance, m->uni_chroma_ac_vlc_len);
231 1238 s->intra_ac_vlc_length =
232 1238 s->intra_ac_vlc_last_length = m->uni_ac_vlc_len;
233 1238 s->intra_chroma_ac_vlc_length =
234 1238 s->intra_chroma_ac_vlc_last_length = m->uni_chroma_ac_vlc_len;
235
236 1238 mjpeg_encode_picture_header(s);
237 1238 mjpeg_encode_picture_frame(s);
238 }
239 #endif
240
241 1438 ret = ff_mpv_reallocate_putbitbuffer(s, put_bits_count(&s->pb) / 8 + 100,
242 1438 put_bits_count(&s->pb) / 4 + 1000);
243
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1438 times.
1438 if (ret < 0) {
244 av_log(s->avctx, AV_LOG_ERROR, "Buffer reallocation failed\n");
245 goto fail;
246 }
247
248 1438 ff_mjpeg_escape_FF(pbc, s->esc_pos);
249
250
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1438 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1438 if ((s->avctx->active_thread_type & FF_THREAD_SLICE) && mb_y < s->mb_height - 1)
251 put_marker(pbc, RST0 + (mb_y&7));
252 1438 s->esc_pos = put_bytes_count(pbc, 0);
253
254 1438 fail:
255
2/2
✓ Branch 0 taken 4314 times.
✓ Branch 1 taken 1438 times.
5752 for (int i = 0; i < 3; i++)
256 4314 s->last_dc[i] = 128 << s->intra_dc_precision;
257
258 1438 return ret;
259 }
260
261 26 static int alloc_huffman(MpegEncContext *s)
262 {
263 26 MJpegContext *m = s->mjpeg_ctx;
264 size_t num_mbs, num_blocks, num_codes;
265 int blocks_per_mb;
266
267 // We need to init this here as the mjpeg init is called before the common init,
268 26 s->mb_width = (s->width + 15) / 16;
269 26 s->mb_height = (s->height + 15) / 16;
270
271
3/4
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
26 switch (s->chroma_format) {
272 18 case CHROMA_420: blocks_per_mb = 6; break;
273 4 case CHROMA_422: blocks_per_mb = 8; break;
274 4 case CHROMA_444: blocks_per_mb = 12; break;
275 default: av_assert0(0);
276 };
277
278 // Make sure we have enough space to hold this frame.
279 26 num_mbs = s->mb_width * s->mb_height;
280 26 num_blocks = num_mbs * blocks_per_mb;
281 26 num_codes = num_blocks * 64;
282
283 26 m->huff_buffer = av_malloc_array(num_codes, sizeof(MJpegHuffmanCode));
284
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 if (!m->huff_buffer)
285 return AVERROR(ENOMEM);
286 26 return 0;
287 }
288
289 30 av_cold int ff_mjpeg_encode_init(MpegEncContext *s)
290 {
291 30 MJpegContext *const m = &((MJPEGEncContext*)s)->mjpeg;
292 int ret;
293
294 30 s->mjpeg_ctx = m;
295
296
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 av_assert0(s->slice_context_count == 1);
297
298
3/4
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 26 times.
30 if (s->codec_id == AV_CODEC_ID_AMV || (s->avctx->active_thread_type & FF_THREAD_SLICE))
299 4 m->huffman = HUFFMAN_TABLE_DEFAULT;
300
301
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (s->mpv_flags & FF_MPV_FLAG_QP_RD) {
302 // Used to produce garbage with MJPEG.
303 av_log(s->avctx, AV_LOG_ERROR,
304 "QP RD is no longer compatible with MJPEG or AMV\n");
305 return AVERROR(EINVAL);
306 }
307
308 /* The following check is automatically true for AMV,
309 * but it doesn't hurt either. */
310 30 ret = ff_mjpeg_encode_check_pix_fmt(s->avctx);
311
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (ret < 0)
312 return ret;
313
314
2/4
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 30 times.
30 if (s->width > 65500 || s->height > 65500) {
315 av_log(s, AV_LOG_ERROR, "JPEG does not support resolutions above 65500x65500\n");
316 return AVERROR(EINVAL);
317 }
318
319 30 s->min_qcoeff=-1023;
320 30 s->max_qcoeff= 1023;
321
322 // Build default Huffman tables.
323 // These may be overwritten later with more optimal Huffman tables, but
324 // they are needed at least right now for some processes like trellis.
325 30 ff_mjpeg_build_huffman_codes(m->huff_size_dc_luminance,
326 30 m->huff_code_dc_luminance,
327 ff_mjpeg_bits_dc_luminance,
328 ff_mjpeg_val_dc);
329 30 ff_mjpeg_build_huffman_codes(m->huff_size_dc_chrominance,
330 30 m->huff_code_dc_chrominance,
331 ff_mjpeg_bits_dc_chrominance,
332 ff_mjpeg_val_dc);
333 30 ff_mjpeg_build_huffman_codes(m->huff_size_ac_luminance,
334 30 m->huff_code_ac_luminance,
335 ff_mjpeg_bits_ac_luminance,
336 ff_mjpeg_val_ac_luminance);
337 30 ff_mjpeg_build_huffman_codes(m->huff_size_ac_chrominance,
338 30 m->huff_code_ac_chrominance,
339 ff_mjpeg_bits_ac_chrominance,
340 ff_mjpeg_val_ac_chrominance);
341
342 30 init_uni_ac_vlc(m->huff_size_ac_luminance, m->uni_ac_vlc_len);
343 30 init_uni_ac_vlc(m->huff_size_ac_chrominance, m->uni_chroma_ac_vlc_len);
344 30 s->intra_ac_vlc_length =
345 30 s->intra_ac_vlc_last_length = m->uni_ac_vlc_len;
346 30 s->intra_chroma_ac_vlc_length =
347 30 s->intra_chroma_ac_vlc_last_length = m->uni_chroma_ac_vlc_len;
348
349 // Buffers start out empty.
350 30 m->huff_ncode = 0;
351
352
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 4 times.
30 if (m->huffman == HUFFMAN_TABLE_OPTIMAL)
353 26 return alloc_huffman(s);
354
355 4 return 0;
356 }
357
358 30 static av_cold int mjpeg_encode_close(AVCodecContext *avctx)
359 {
360 30 MJPEGEncContext *const mjpeg = avctx->priv_data;
361 30 av_freep(&mjpeg->mjpeg.huff_buffer);
362 30 ff_mpv_encode_end(avctx);
363 30 return 0;
364 }
365
366 /**
367 * Add code and table_id to the JPEG buffer.
368 *
369 * @param s The MJpegContext which contains the JPEG buffer.
370 * @param table_id Which Huffman table the code belongs to.
371 * @param code The encoded exponent of the coefficients and the run-bits.
372 */
373 28811107 static inline void ff_mjpeg_encode_code(MJpegContext *s, uint8_t table_id, int code)
374 {
375 28811107 MJpegHuffmanCode *c = &s->huff_buffer[s->huff_ncode++];
376 28811107 c->table_id = table_id;
377 28811107 c->code = code;
378 28811107 }
379
380 /**
381 * Add the coefficient's data to the JPEG buffer.
382 *
383 * @param s The MJpegContext which contains the JPEG buffer.
384 * @param table_id Which Huffman table the code belongs to.
385 * @param val The coefficient.
386 * @param run The run-bits.
387 */
388 26111467 static void ff_mjpeg_encode_coef(MJpegContext *s, uint8_t table_id, int val, int run)
389 {
390 int mant, code;
391
392
2/2
✓ Branch 0 taken 103825 times.
✓ Branch 1 taken 26007642 times.
26111467 if (val == 0) {
393
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 103825 times.
103825 av_assert0(run == 0);
394 103825 ff_mjpeg_encode_code(s, table_id, 0);
395 } else {
396 26007642 mant = val;
397
2/2
✓ Branch 0 taken 12984441 times.
✓ Branch 1 taken 13023201 times.
26007642 if (val < 0) {
398 12984441 val = -val;
399 12984441 mant--;
400 }
401
402 26007642 code = (run << 4) | (av_log2_16bit(val) + 1);
403
404 26007642 s->huff_buffer[s->huff_ncode].mant = mant;
405 26007642 ff_mjpeg_encode_code(s, table_id, code);
406 }
407 26111467 }
408
409 /**
410 * Add the block's data into the JPEG buffer.
411 *
412 * @param s The MpegEncContext that contains the JPEG buffer.
413 * @param block The block.
414 * @param n The block's index or number.
415 */
416 2722788 static void record_block(MpegEncContext *s, int16_t *block, int n)
417 {
418 int i, j, table_id;
419 int component, dc, last_index, val, run;
420 2722788 MJpegContext *m = s->mjpeg_ctx;
421
422 /* DC coef */
423
2/2
✓ Branch 0 taken 1226496 times.
✓ Branch 1 taken 1496292 times.
2722788 component = (n <= 3 ? 0 : (n&1) + 1);
424 2722788 table_id = (n <= 3 ? 0 : 1);
425 2722788 dc = block[0]; /* overflow is impossible */
426 2722788 val = dc - s->last_dc[component];
427
428 2722788 ff_mjpeg_encode_coef(m, table_id, val, 0);
429
430 2722788 s->last_dc[component] = dc;
431
432 /* AC coefs */
433
434 2722788 run = 0;
435 2722788 last_index = s->block_last_index[n];
436 2722788 table_id |= 2;
437
438
2/2
✓ Branch 0 taken 50025404 times.
✓ Branch 1 taken 2722788 times.
52748192 for(i=1;i<=last_index;i++) {
439 50025404 j = s->intra_scantable.permutated[i];
440 50025404 val = block[j];
441
442
2/2
✓ Branch 0 taken 26636725 times.
✓ Branch 1 taken 23388679 times.
50025404 if (val == 0) {
443 26636725 run++;
444 } else {
445
2/2
✓ Branch 0 taken 56638 times.
✓ Branch 1 taken 23388679 times.
23445317 while (run >= 16) {
446 56638 ff_mjpeg_encode_code(m, table_id, 0xf0);
447 56638 run -= 16;
448 }
449 23388679 ff_mjpeg_encode_coef(m, table_id, val, run);
450 23388679 run = 0;
451 }
452 }
453
454 /* output EOB only if not already 64 values */
455
3/4
✓ Branch 0 taken 79786 times.
✓ Branch 1 taken 2643002 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 79786 times.
2722788 if (last_index < 63 || run != 0)
456 2643002 ff_mjpeg_encode_code(m, table_id, 0);
457 2722788 }
458
459 359100 static void encode_block(MpegEncContext *s, int16_t *block, int n)
460 {
461 int mant, nbits, code, i, j;
462 int component, dc, run, last_index, val;
463 359100 MJpegContext *m = s->mjpeg_ctx;
464 uint8_t *huff_size_ac;
465 uint16_t *huff_code_ac;
466
467 /* DC coef */
468
2/2
✓ Branch 0 taken 119700 times.
✓ Branch 1 taken 239400 times.
359100 component = (n <= 3 ? 0 : (n&1) + 1);
469 359100 dc = block[0]; /* overflow is impossible */
470 359100 val = dc - s->last_dc[component];
471
2/2
✓ Branch 0 taken 239400 times.
✓ Branch 1 taken 119700 times.
359100 if (n < 4) {
472 239400 ff_mjpeg_encode_dc(&s->pb, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
473 239400 huff_size_ac = m->huff_size_ac_luminance;
474 239400 huff_code_ac = m->huff_code_ac_luminance;
475 } else {
476 119700 ff_mjpeg_encode_dc(&s->pb, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
477 119700 huff_size_ac = m->huff_size_ac_chrominance;
478 119700 huff_code_ac = m->huff_code_ac_chrominance;
479 }
480 359100 s->last_dc[component] = dc;
481
482 /* AC coefs */
483
484 359100 run = 0;
485 359100 last_index = s->block_last_index[n];
486
2/2
✓ Branch 0 taken 6520168 times.
✓ Branch 1 taken 359100 times.
6879268 for(i=1;i<=last_index;i++) {
487 6520168 j = s->intra_scantable.permutated[i];
488 6520168 val = block[j];
489
2/2
✓ Branch 0 taken 3000879 times.
✓ Branch 1 taken 3519289 times.
6520168 if (val == 0) {
490 3000879 run++;
491 } else {
492
2/2
✓ Branch 0 taken 3018 times.
✓ Branch 1 taken 3519289 times.
3522307 while (run >= 16) {
493 3018 put_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
494 3018 run -= 16;
495 }
496 3519289 mant = val;
497
2/2
✓ Branch 0 taken 1776140 times.
✓ Branch 1 taken 1743149 times.
3519289 if (val < 0) {
498 1776140 val = -val;
499 1776140 mant--;
500 }
501
502 3519289 nbits= av_log2_16bit(val) + 1;
503 3519289 code = (run << 4) | nbits;
504
505 3519289 put_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
506
507 3519289 put_sbits(&s->pb, nbits, mant);
508 3519289 run = 0;
509 }
510 }
511
512 /* output EOB only if not already 64 values */
513
3/4
✓ Branch 0 taken 14683 times.
✓ Branch 1 taken 344417 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 14683 times.
359100 if (last_index < 63 || run != 0)
514 344417 put_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
515 359100 }
516
517 433998 void ff_mjpeg_encode_mb(MpegEncContext *s, int16_t block[12][64])
518 {
519 int i;
520
2/2
✓ Branch 0 taken 374148 times.
✓ Branch 1 taken 59850 times.
433998 if (s->mjpeg_ctx->huffman == HUFFMAN_TABLE_OPTIMAL) {
521
2/2
✓ Branch 0 taken 59850 times.
✓ Branch 1 taken 314298 times.
374148 if (s->chroma_format == CHROMA_444) {
522 59850 record_block(s, block[0], 0);
523 59850 record_block(s, block[2], 2);
524 59850 record_block(s, block[4], 4);
525 59850 record_block(s, block[8], 8);
526 59850 record_block(s, block[5], 5);
527 59850 record_block(s, block[9], 9);
528
529
2/2
✓ Branch 0 taken 59700 times.
✓ Branch 1 taken 150 times.
59850 if (16*s->mb_x+8 < s->width) {
530 59700 record_block(s, block[1], 1);
531 59700 record_block(s, block[3], 3);
532 59700 record_block(s, block[6], 6);
533 59700 record_block(s, block[10], 10);
534 59700 record_block(s, block[7], 7);
535 59700 record_block(s, block[11], 11);
536 }
537 } else {
538
2/2
✓ Branch 0 taken 1571490 times.
✓ Branch 1 taken 314298 times.
1885788 for(i=0;i<5;i++) {
539 1571490 record_block(s, block[i], i);
540 }
541
2/2
✓ Branch 0 taken 254448 times.
✓ Branch 1 taken 59850 times.
314298 if (s->chroma_format == CHROMA_420) {
542 254448 record_block(s, block[5], 5);
543 } else {
544 59850 record_block(s, block[6], 6);
545 59850 record_block(s, block[5], 5);
546 59850 record_block(s, block[7], 7);
547 }
548 }
549 } else {
550
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 59850 times.
59850 if (s->chroma_format == CHROMA_444) {
551 encode_block(s, block[0], 0);
552 encode_block(s, block[2], 2);
553 encode_block(s, block[4], 4);
554 encode_block(s, block[8], 8);
555 encode_block(s, block[5], 5);
556 encode_block(s, block[9], 9);
557
558 if (16*s->mb_x+8 < s->width) {
559 encode_block(s, block[1], 1);
560 encode_block(s, block[3], 3);
561 encode_block(s, block[6], 6);
562 encode_block(s, block[10], 10);
563 encode_block(s, block[7], 7);
564 encode_block(s, block[11], 11);
565 }
566 } else {
567
2/2
✓ Branch 0 taken 299250 times.
✓ Branch 1 taken 59850 times.
359100 for(i=0;i<5;i++) {
568 299250 encode_block(s, block[i], i);
569 }
570
1/2
✓ Branch 0 taken 59850 times.
✗ Branch 1 not taken.
59850 if (s->chroma_format == CHROMA_420) {
571 59850 encode_block(s, block[5], 5);
572 } else {
573 encode_block(s, block[6], 6);
574 encode_block(s, block[5], 5);
575 encode_block(s, block[7], 7);
576 }
577 }
578
579 59850 s->i_tex_bits += get_bits_diff(s);
580 }
581 433998 }
582
583 #if CONFIG_AMV_ENCODER
584 // maximum over s->mjpeg_vsample[i]
585 #define V_MAX 2
586 200 static int amv_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
587 const AVFrame *pic_arg, int *got_packet)
588 {
589 200 MpegEncContext *s = avctx->priv_data;
590 AVFrame *pic;
591 int i, ret;
592 int chroma_h_shift, chroma_v_shift;
593
594 200 av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift, &chroma_v_shift);
595
596
3/4
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 150 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 50 times.
200 if ((avctx->height & 15) && avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) {
597 av_log(avctx, AV_LOG_ERROR,
598 "Heights which are not a multiple of 16 might fail with some decoders, "
599 "use vstrict=-1 / -strict -1 to use %d anyway.\n", avctx->height);
600 av_log(avctx, AV_LOG_WARNING, "If you have a device that plays AMV videos, please test if videos "
601 "with such heights work with it and report your findings to ffmpeg-devel@ffmpeg.org\n");
602 return AVERROR_EXPERIMENTAL;
603 }
604
605 200 pic = av_frame_clone(pic_arg);
606
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 200 times.
200 if (!pic)
607 return AVERROR(ENOMEM);
608 //picture should be flipped upside-down
609
2/2
✓ Branch 0 taken 600 times.
✓ Branch 1 taken 200 times.
800 for(i=0; i < 3; i++) {
610
2/2
✓ Branch 0 taken 400 times.
✓ Branch 1 taken 200 times.
600 int vsample = i ? 2 >> chroma_v_shift : 2;
611 600 pic->data[i] += pic->linesize[i] * (vsample * s->height / V_MAX - 1);
612 600 pic->linesize[i] *= -1;
613 }
614 200 ret = ff_mpv_encode_picture(avctx, pkt, pic, got_packet);
615 200 av_frame_free(&pic);
616 200 return ret;
617 }
618 #endif
619
620 #define OFFSET(x) offsetof(MJPEGEncContext, mjpeg.x)
621 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
622 static const AVOption options[] = {
623 FF_MPV_COMMON_OPTS
624 #if FF_API_MJPEG_PRED
625 { "pred", "Deprecated, does nothing", FF_MPV_OFFSET(dummy), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, 3, VE, "pred" },
626 { "left", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, VE, "pred" },
627 { "plane", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 2 }, INT_MIN, INT_MAX, VE, "pred" },
628 { "median", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 3 }, INT_MIN, INT_MAX, VE, "pred" },
629 #endif
630 { "huffman", "Huffman table strategy", OFFSET(huffman), AV_OPT_TYPE_INT, { .i64 = HUFFMAN_TABLE_OPTIMAL }, 0, NB_HUFFMAN_TABLE_OPTION - 1, VE, "huffman" },
631 { "default", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = HUFFMAN_TABLE_DEFAULT }, INT_MIN, INT_MAX, VE, "huffman" },
632 { "optimal", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = HUFFMAN_TABLE_OPTIMAL }, INT_MIN, INT_MAX, VE, "huffman" },
633 { "force_duplicated_matrix", "Always write luma and chroma matrix for mjpeg, useful for rtp streaming.", OFFSET(force_duplicated_matrix), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, VE },
634 #if FF_API_MPEGVIDEO_OPTS
635 FF_MPV_DEPRECATED_MPEG_QUANT_OPT
636 FF_MPV_DEPRECATED_A53_CC_OPT
637 FF_MPV_DEPRECATED_BFRAME_OPTS
638 #endif
639 { NULL},
640 };
641
642 #if CONFIG_MJPEG_ENCODER
643 static const AVClass mjpeg_class = {
644 .class_name = "mjpeg encoder",
645 .item_name = av_default_item_name,
646 .option = options,
647 .version = LIBAVUTIL_VERSION_INT,
648 };
649
650 const AVCodec ff_mjpeg_encoder = {
651 .name = "mjpeg",
652 .long_name = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
653 .type = AVMEDIA_TYPE_VIDEO,
654 .id = AV_CODEC_ID_MJPEG,
655 .priv_data_size = sizeof(MJPEGEncContext),
656 .init = ff_mpv_encode_init,
657 .encode2 = ff_mpv_encode_picture,
658 .close = mjpeg_encode_close,
659 .capabilities = AV_CODEC_CAP_SLICE_THREADS | AV_CODEC_CAP_FRAME_THREADS,
660 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
661 .pix_fmts = (const enum AVPixelFormat[]) {
662 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P,
663 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
664 AV_PIX_FMT_NONE
665 },
666 .priv_class = &mjpeg_class,
667 .profiles = NULL_IF_CONFIG_SMALL(ff_mjpeg_profiles),
668 };
669 #endif
670
671 #if CONFIG_AMV_ENCODER
672 static const AVClass amv_class = {
673 .class_name = "amv encoder",
674 .item_name = av_default_item_name,
675 .option = options,
676 .version = LIBAVUTIL_VERSION_INT,
677 };
678
679 const AVCodec ff_amv_encoder = {
680 .name = "amv",
681 .long_name = NULL_IF_CONFIG_SMALL("AMV Video"),
682 .type = AVMEDIA_TYPE_VIDEO,
683 .id = AV_CODEC_ID_AMV,
684 .priv_data_size = sizeof(MJPEGEncContext),
685 .init = ff_mpv_encode_init,
686 .encode2 = amv_encode_picture,
687 .close = mjpeg_encode_close,
688 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
689 .pix_fmts = (const enum AVPixelFormat[]) {
690 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_NONE
691 },
692 .priv_class = &amv_class,
693 };
694 #endif
695