FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mjpegenc.c
Date: 2026-10-12 23:00:26
Exec Total Coverage
Lines: 297 326 91.1%
Functions: 17 17 100.0%
Branches: 93 110 84.5%

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 "config_components.h"
34
35 #include "libavutil/mem.h"
36
37 #include "avcodec.h"
38 #include "codec_internal.h"
39 #include "jpegtables.h"
40 #include "mjpegenc_common.h"
41 #include "mjpegenc_huffman.h"
42 #include "mpegvideo.h"
43 #include "mjpeg.h"
44 #include "mjpegenc.h"
45 #include "mpegvideoenc.h"
46 #include "profiles.h"
47
48 /**
49 * Buffer of JPEG frame data.
50 *
51 * Optimal Huffman table generation requires the frame data to be loaded into
52 * a buffer so that the tables can be computed.
53 * There are at most mb_width*mb_height*12*64 of these per frame.
54 */
55 typedef struct MJpegHuffmanCode {
56 // 0=DC lum, 1=DC chrom, 2=AC lum, 3=AC chrom
57 uint8_t table_id; ///< The Huffman table id associated with the data.
58 uint8_t code; ///< The exponent.
59 uint16_t mant; ///< The mantissa.
60 } MJpegHuffmanCode;
61
62 /* The following is the private context of MJPEG/AMV decoder.
63 * Note that when using slice threading only the main thread's
64 * MPVEncContext is followed by a MjpegContext; the other threads
65 * can access this shared context via MPVEncContext.mjpeg. */
66 typedef struct MJPEGEncContext {
67 MPVMainEncContext mpeg;
68 MJpegContext mjpeg;
69 } MJPEGEncContext;
70
71 1748 static av_cold void init_uni_ac_vlc(const uint8_t huff_size_ac[256],
72 uint8_t *uni_ac_vlc_len)
73 {
74
2/2
✓ Branch 0 taken 223744 times.
✓ Branch 1 taken 1748 times.
225492 for (int i = 0; i < 128; i++) {
75 223744 int level = i - 64;
76
2/2
✓ Branch 0 taken 1748 times.
✓ Branch 1 taken 221996 times.
223744 if (!level)
77 1748 continue;
78
2/2
✓ Branch 0 taken 14207744 times.
✓ Branch 1 taken 221996 times.
14429740 for (int run = 0; run < 64; run++) {
79 int len, code, nbits;
80 14207744 int alevel = FFABS(level);
81
82 14207744 len = (run >> 4) * huff_size_ac[0xf0];
83
84 14207744 nbits= av_log2_16bit(alevel) + 1;
85 14207744 code = ((15&run) << 4) | nbits;
86
87 14207744 len += huff_size_ac[code] + nbits;
88
89 14207744 uni_ac_vlc_len[UNI_AC_ENC_INDEX(run, i)] = len;
90 // We ignore EOB as its just a constant which does not change generally
91 }
92 }
93 1748 }
94
95 1441 static void mjpeg_encode_picture_header(MPVEncContext *const s)
96 {
97 1441 ff_mjpeg_encode_picture_header(s->c.avctx, &s->pb, s->c.cur_pic.ptr->f, s->mjpeg_ctx,
98 1441 s->c.intra_scantable.permutated, 0,
99 1441 s->c.intra_matrix, s->c.chroma_intra_matrix,
100 1441 s->c.slice_context_count > 1);
101
102 1441 s->esc_pos = put_bytes_count(&s->pb, 0);
103
2/2
✓ Branch 0 taken 700 times.
✓ Branch 1 taken 1441 times.
2141 for (int i = 1; i < s->c.slice_context_count; i++)
104 700 s->c.enc_contexts[i]->esc_pos = 0;
105 1441 }
106
107 1441 static int mjpeg_amv_encode_picture_header(MPVMainEncContext *const m)
108 {
109 1441 MJPEGEncContext *const m2 = (MJPEGEncContext*)m;
110 1441 MPVEncContext *const s = &m->s;
111 av_assert2(s->mjpeg_ctx == &m2->mjpeg);
112 /* s->huffman == HUFFMAN_TABLE_OPTIMAL can only be true for MJPEG. */
113
2/2
✓ Branch 0 taken 600 times.
✓ Branch 1 taken 841 times.
1441 if (!CONFIG_MJPEG_ENCODER || m2->mjpeg.huffman != HUFFMAN_TABLE_OPTIMAL)
114 600 mjpeg_encode_picture_header(s);
115
116 1441 return 0;
117 }
118
119 #if CONFIG_MJPEG_ENCODER
120 /**
121 * Encodes and outputs the entire frame in the JPEG format.
122 *
123 * @param main The MPVMainEncContext.
124 */
125 841 static void mjpeg_encode_picture_frame(MPVMainEncContext *const main)
126 {
127 841 MPVEncContext *const s = &main->s;
128 int nbits, code, table_id;
129 841 MJpegContext *m = s->mjpeg_ctx;
130 841 uint8_t *huff_size[4] = { m->huff_size_dc_luminance,
131 841 m->huff_size_dc_chrominance,
132 841 m->huff_size_ac_luminance,
133 841 m->huff_size_ac_chrominance };
134 841 uint16_t *huff_code[4] = { m->huff_code_dc_luminance,
135 841 m->huff_code_dc_chrominance,
136 841 m->huff_code_ac_luminance,
137 841 m->huff_code_ac_chrominance };
138 841 size_t total_bits = 0;
139 size_t bytes_needed;
140
141 841 main->header_bits = get_bits_diff(s);
142 // Estimate the total size first
143
2/2
✓ Branch 0 taken 20399240 times.
✓ Branch 1 taken 841 times.
20400081 for (int i = 0; i < m->huff_ncode; i++) {
144 20399240 table_id = m->huff_buffer[i].table_id;
145 20399240 code = m->huff_buffer[i].code;
146 20399240 nbits = code & 0xf;
147
148 20399240 total_bits += huff_size[table_id][code] + nbits;
149 }
150
151 841 bytes_needed = (total_bits + 7) / 8;
152 841 ff_mpv_reallocate_putbitbuffer(s, bytes_needed, bytes_needed);
153
154
2/2
✓ Branch 0 taken 20399240 times.
✓ Branch 1 taken 841 times.
20400081 for (int i = 0; i < m->huff_ncode; i++) {
155 20399240 table_id = m->huff_buffer[i].table_id;
156 20399240 code = m->huff_buffer[i].code;
157 20399240 nbits = code & 0xf;
158
159 20399240 put_bits(&s->pb, huff_size[table_id][code], huff_code[table_id][code]);
160
2/2
✓ Branch 0 taken 18322968 times.
✓ Branch 1 taken 2076272 times.
20399240 if (nbits != 0) {
161 18322968 put_sbits(&s->pb, nbits, m->huff_buffer[i].mant);
162 }
163 }
164
165 841 m->huff_ncode = 0;
166 841 s->i_tex_bits = get_bits_diff(s);
167 841 }
168
169 /**
170 * Builds all 4 optimal Huffman tables.
171 *
172 * Uses the data stored in the JPEG buffer to compute the tables.
173 * Stores the Huffman tables in the bits_* and val_* arrays in the MJpegContext.
174 *
175 * @param m MJpegContext containing the JPEG buffer.
176 */
177 841 static void mjpeg_build_optimal_huffman(MJpegContext *m)
178 {
179 MJpegEncHuffmanContext dc_luminance_ctx;
180 MJpegEncHuffmanContext dc_chrominance_ctx;
181 MJpegEncHuffmanContext ac_luminance_ctx;
182 MJpegEncHuffmanContext ac_chrominance_ctx;
183 841 MJpegEncHuffmanContext *ctx[4] = { &dc_luminance_ctx,
184 &dc_chrominance_ctx,
185 &ac_luminance_ctx,
186 &ac_chrominance_ctx };
187
2/2
✓ Branch 0 taken 3364 times.
✓ Branch 1 taken 841 times.
4205 for (int i = 0; i < 4; i++)
188 3364 ff_mjpeg_encode_huffman_init(ctx[i]);
189
190
2/2
✓ Branch 0 taken 20399240 times.
✓ Branch 1 taken 841 times.
20400081 for (int i = 0; i < m->huff_ncode; i++) {
191 20399240 int table_id = m->huff_buffer[i].table_id;
192 20399240 int code = m->huff_buffer[i].code;
193
194 20399240 ff_mjpeg_encode_huffman_increment(ctx[table_id], code);
195 }
196
197 841 ff_mjpeg_encode_huffman_close(&dc_luminance_ctx,
198 841 m->bits_dc_luminance,
199 841 m->val_dc_luminance, 12);
200 841 ff_mjpeg_encode_huffman_close(&dc_chrominance_ctx,
201 841 m->bits_dc_chrominance,
202 841 m->val_dc_chrominance, 12);
203 841 ff_mjpeg_encode_huffman_close(&ac_luminance_ctx,
204 841 m->bits_ac_luminance,
205 841 m->val_ac_luminance, 256);
206 841 ff_mjpeg_encode_huffman_close(&ac_chrominance_ctx,
207 841 m->bits_ac_chrominance,
208 841 m->val_ac_chrominance, 256);
209
210 841 ff_mjpeg_build_huffman_codes(m->huff_size_dc_luminance,
211 841 m->huff_code_dc_luminance,
212 841 m->bits_dc_luminance,
213 841 m->val_dc_luminance);
214 841 ff_mjpeg_build_huffman_codes(m->huff_size_dc_chrominance,
215 841 m->huff_code_dc_chrominance,
216 841 m->bits_dc_chrominance,
217 841 m->val_dc_chrominance);
218 841 ff_mjpeg_build_huffman_codes(m->huff_size_ac_luminance,
219 841 m->huff_code_ac_luminance,
220 841 m->bits_ac_luminance,
221 841 m->val_ac_luminance);
222 841 ff_mjpeg_build_huffman_codes(m->huff_size_ac_chrominance,
223 841 m->huff_code_ac_chrominance,
224 841 m->bits_ac_chrominance,
225 841 m->val_ac_chrominance);
226 841 }
227 #endif
228
229 /**
230 * Writes the complete JPEG frame when optimal huffman tables are enabled,
231 * otherwise writes the stuffing.
232 *
233 * Header + values + stuffing.
234 *
235 * @param s The MPVEncContext.
236 * @return int Error code, 0 if successful.
237 */
238 4091 int ff_mjpeg_encode_stuffing(MPVEncContext *const s)
239 {
240 4091 MJpegContext *const m = s->mjpeg_ctx;
241 4091 PutBitContext *pbc = &s->pb;
242 4091 int mb_y = s->c.mb_y - !s->c.mb_x;
243 int ret;
244
245 #if CONFIG_MJPEG_ENCODER
246
2/2
✓ Branch 0 taken 841 times.
✓ Branch 1 taken 3250 times.
4091 if (m->huffman == HUFFMAN_TABLE_OPTIMAL) {
247 /* HUFFMAN_TABLE_OPTIMAL is incompatible with slice threading,
248 * therefore the following cast is allowed. */
249 841 MPVMainEncContext *const main = (MPVMainEncContext*)s;
250
251 841 mjpeg_build_optimal_huffman(m);
252
253 // Replace the VLCs with the optimal ones.
254 // The default ones may be used for trellis during quantization.
255 841 init_uni_ac_vlc(m->huff_size_ac_luminance, m->uni_ac_vlc_len);
256 841 init_uni_ac_vlc(m->huff_size_ac_chrominance, m->uni_chroma_ac_vlc_len);
257 841 s->intra_ac_vlc_length =
258 841 s->intra_ac_vlc_last_length = m->uni_ac_vlc_len;
259 841 s->intra_chroma_ac_vlc_length =
260 841 s->intra_chroma_ac_vlc_last_length = m->uni_chroma_ac_vlc_len;
261
262 841 mjpeg_encode_picture_header(s);
263 841 mjpeg_encode_picture_frame(main);
264 }
265 #endif
266
267 4091 ret = ff_mpv_reallocate_putbitbuffer(s, put_bits_count(&s->pb) / 8 + 100,
268 4091 put_bits_count(&s->pb) / 4 + 1000);
269
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4091 times.
4091 if (ret < 0) {
270 ✗ av_log(s->c.avctx, AV_LOG_ERROR, "Buffer reallocation failed\n");
271 ✗ goto fail;
272 }
273
274 4091 ff_mjpeg_escape_FF(pbc, s->esc_pos);
275
276
4/4
✓ Branch 0 taken 2850 times.
✓ Branch 1 taken 1241 times.
✓ Branch 2 taken 2650 times.
✓ Branch 3 taken 200 times.
4091 if (s->c.slice_context_count > 1 && mb_y < s->c.mb_height - 1)
277 2650 put_marker(pbc, RST0 + (mb_y&7));
278 4091 s->esc_pos = put_bytes_count(pbc, 0);
279
280 4091 fail:
281
2/2
✓ Branch 0 taken 12273 times.
✓ Branch 1 taken 4091 times.
16364 for (int i = 0; i < 3; i++)
282 12273 s->last_dc[i] = 128;
283
284 4091 return ret;
285 }
286
287 21 static int alloc_huffman(MJPEGEncContext *const m2)
288 {
289 21 MJpegContext *const m = &m2->mjpeg;
290 21 MPVEncContext *const s = &m2->mpeg.s;
291 static const char blocks_per_mb[] = {
292 [CHROMA_420] = 6, [CHROMA_422] = 8, [CHROMA_444] = 12
293 };
294 size_t num_blocks;
295
296 // Make sure we have enough space to hold this frame.
297 21 num_blocks = s->c.mb_num * blocks_per_mb[s->c.chroma_format];
298
299 21 m->huff_buffer = av_malloc_array(num_blocks,
300 64 /* codes per MB */ * sizeof(MJpegHuffmanCode));
301
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (!m->huff_buffer)
302 ✗ return AVERROR(ENOMEM);
303 21 return 0;
304 }
305
306 33 static av_cold int mjpeg_encode_close(AVCodecContext *avctx)
307 {
308 33 MJPEGEncContext *const mjpeg = avctx->priv_data;
309 33 av_freep(&mjpeg->mjpeg.huff_buffer);
310 33 ff_mpv_encode_end(avctx);
311 33 return 0;
312 }
313
314 /**
315 * Add code and table_id to the JPEG buffer.
316 *
317 * @param s The MJpegContext which contains the JPEG buffer.
318 * @param table_id Which Huffman table the code belongs to.
319 * @param code The encoded exponent of the coefficients and the run-bits.
320 */
321 20399240 static inline void mjpeg_encode_code(MJpegContext *s, uint8_t table_id, int code)
322 {
323 20399240 MJpegHuffmanCode *c = &s->huff_buffer[s->huff_ncode++];
324 20399240 c->table_id = table_id;
325 20399240 c->code = code;
326 20399240 }
327
328 /**
329 * Add the coefficient's data to the JPEG buffer.
330 *
331 * @param s The MJpegContext which contains the JPEG buffer.
332 * @param table_id Which Huffman table the code belongs to.
333 * @param val The coefficient.
334 * @param run The run-bits.
335 */
336 18403830 static void mjpeg_encode_coef(MJpegContext *s, uint8_t table_id, int val, int run)
337 {
338 int mant, code;
339
340
2/2
✓ Branch 0 taken 80862 times.
✓ Branch 1 taken 18322968 times.
18403830 if (val == 0) {
341
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 80862 times.
80862 av_assert0(run == 0);
342 80862 mjpeg_encode_code(s, table_id, 0);
343 } else {
344 18322968 mant = val;
345
2/2
✓ Branch 0 taken 9148245 times.
✓ Branch 1 taken 9174723 times.
18322968 if (val < 0) {
346 9148245 val = -val;
347 9148245 mant--;
348 }
349
350 18322968 code = (run << 4) | (av_log2_16bit(val) + 1);
351
352 18322968 s->huff_buffer[s->huff_ncode].mant = mant;
353 18322968 mjpeg_encode_code(s, table_id, code);
354 }
355 18403830 }
356
357 /**
358 * Add the block's data into the JPEG buffer.
359 *
360 * @param s The MPVEncContext that contains the JPEG buffer.
361 * @param block The block.
362 * @param n The block's index or number.
363 */
364 2005644 static void record_block(MPVEncContext *const s, int16_t block[], int n)
365 {
366 int i, j, table_id;
367 int component, dc, last_index, val, run;
368 2005644 MJpegContext *m = s->mjpeg_ctx;
369
370 /* DC coef */
371
2/2
✓ Branch 0 taken 987800 times.
✓ Branch 1 taken 1017844 times.
2005644 component = (n <= 3 ? 0 : (n&1) + 1);
372 2005644 table_id = (n <= 3 ? 0 : 1);
373 2005644 dc = block[0]; /* overflow is impossible */
374 2005644 val = dc - s->last_dc[component];
375
376 2005644 mjpeg_encode_coef(m, table_id, val, 0);
377
378 2005644 s->last_dc[component] = dc;
379
380 /* AC coefs */
381
382 2005644 run = 0;
383 2005644 last_index = s->c.block_last_index[n];
384 2005644 table_id |= 2;
385
386
2/2
✓ Branch 0 taken 35328155 times.
✓ Branch 1 taken 2005644 times.
37333799 for(i=1;i<=last_index;i++) {
387 35328155 j = s->c.intra_scantable.permutated[i];
388 35328155 val = block[j];
389
390
2/2
✓ Branch 0 taken 18929969 times.
✓ Branch 1 taken 16398186 times.
35328155 if (val == 0) {
391 18929969 run++;
392 } else {
393
2/2
✓ Branch 0 taken 43987 times.
✓ Branch 1 taken 16398186 times.
16442173 while (run >= 16) {
394 43987 mjpeg_encode_code(m, table_id, 0xf0);
395 43987 run -= 16;
396 }
397 16398186 mjpeg_encode_coef(m, table_id, val, run);
398 16398186 run = 0;
399 }
400 }
401
402 /* output EOB only if not already 64 values */
403
3/4
✓ Branch 0 taken 54221 times.
✓ Branch 1 taken 1951423 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 54221 times.
2005644 if (last_index < 63 || run != 0)
404 1951423 mjpeg_encode_code(m, table_id, 0);
405 2005644 }
406
407 1077300 static void encode_block(MPVEncContext *const s, int16_t block[], int n)
408 {
409 int mant, nbits, code, i, j;
410 int component, dc, run, last_index, val;
411 1077300 const MJpegContext *const m = s->mjpeg_ctx;
412 const uint16_t *huff_code_ac;
413 const uint8_t *huff_size_ac;
414
415 /* DC coef */
416
2/2
✓ Branch 0 taken 359100 times.
✓ Branch 1 taken 718200 times.
1077300 component = (n <= 3 ? 0 : (n&1) + 1);
417 1077300 dc = block[0]; /* overflow is impossible */
418 1077300 val = dc - s->last_dc[component];
419
2/2
✓ Branch 0 taken 718200 times.
✓ Branch 1 taken 359100 times.
1077300 if (n < 4) {
420 718200 ff_mjpeg_encode_dc(&s->pb, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
421 718200 huff_size_ac = m->huff_size_ac_luminance;
422 718200 huff_code_ac = m->huff_code_ac_luminance;
423 } else {
424 359100 ff_mjpeg_encode_dc(&s->pb, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
425 359100 huff_size_ac = m->huff_size_ac_chrominance;
426 359100 huff_code_ac = m->huff_code_ac_chrominance;
427 }
428 1077300 s->last_dc[component] = dc;
429
430 /* AC coefs */
431
432 1077300 run = 0;
433 1077300 last_index = s->c.block_last_index[n];
434
2/2
✓ Branch 0 taken 21120443 times.
✓ Branch 1 taken 1077300 times.
22197743 for(i=1;i<=last_index;i++) {
435 21120443 j = s->c.intra_scantable.permutated[i];
436 21120443 val = block[j];
437
2/2
✓ Branch 0 taken 10604386 times.
✓ Branch 1 taken 10516057 times.
21120443 if (val == 0) {
438 10604386 run++;
439 } else {
440
2/2
✓ Branch 0 taken 19613 times.
✓ Branch 1 taken 10516057 times.
10535670 while (run >= 16) {
441 19613 put_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
442 19613 run -= 16;
443 }
444 10516057 mant = val;
445
2/2
✓ Branch 0 taken 5281274 times.
✓ Branch 1 taken 5234783 times.
10516057 if (val < 0) {
446 5281274 val = -val;
447 5281274 mant--;
448 }
449
450 10516057 nbits= av_log2_16bit(val) + 1;
451 10516057 code = (run << 4) | nbits;
452
453 10516057 put_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
454
455 10516057 put_sbits(&s->pb, nbits, mant);
456 10516057 run = 0;
457 }
458 }
459
460 /* output EOB only if not already 64 values */
461
3/4
✓ Branch 0 taken 40269 times.
✓ Branch 1 taken 1037031 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 40269 times.
1077300 if (last_index < 63 || run != 0)
462 1037031 put_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
463 1077300 }
464
465 254536 static void mjpeg_record_mb(MPVEncContext *const s, int16_t block[][64],
466 int unused_x, int unused_y)
467 {
468
2/2
✓ Branch 0 taken 59938 times.
✓ Branch 1 taken 194598 times.
254536 if (s->c.chroma_format == CHROMA_444) {
469 59938 record_block(s, block[0], 0);
470 59938 record_block(s, block[2], 2);
471 59938 record_block(s, block[4], 4);
472 59938 record_block(s, block[8], 8);
473 59938 record_block(s, block[5], 5);
474 59938 record_block(s, block[9], 9);
475
476
2/2
✓ Branch 0 taken 59788 times.
✓ Branch 1 taken 150 times.
59938 if (16*s->c.mb_x+8 < s->c.width) {
477 59788 record_block(s, block[1], 1);
478 59788 record_block(s, block[3], 3);
479 59788 record_block(s, block[6], 6);
480 59788 record_block(s, block[10], 10);
481 59788 record_block(s, block[7], 7);
482 59788 record_block(s, block[11], 11);
483 }
484 } else {
485
2/2
✓ Branch 0 taken 972990 times.
✓ Branch 1 taken 194598 times.
1167588 for (int i = 0; i < 5; i++)
486 972990 record_block(s, block[i], i);
487
2/2
✓ Branch 0 taken 134748 times.
✓ Branch 1 taken 59850 times.
194598 if (s->c.chroma_format == CHROMA_420) {
488 134748 record_block(s, block[5], 5);
489 } else {
490 59850 record_block(s, block[6], 6);
491 59850 record_block(s, block[5], 5);
492 59850 record_block(s, block[7], 7);
493 }
494 }
495 254536 }
496
497 179550 static void mjpeg_encode_mb(MPVEncContext *const s, int16_t block[][64],
498 int unused_x, int unused_y)
499 {
500
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 179550 times.
179550 if (s->c.chroma_format == CHROMA_444) {
501 ✗ encode_block(s, block[0], 0);
502 ✗ encode_block(s, block[2], 2);
503 ✗ encode_block(s, block[4], 4);
504 ✗ encode_block(s, block[8], 8);
505 ✗ encode_block(s, block[5], 5);
506 ✗ encode_block(s, block[9], 9);
507
508 ✗ if (16 * s->c.mb_x + 8 < s->c.width) {
509 ✗ encode_block(s, block[1], 1);
510 ✗ encode_block(s, block[3], 3);
511 ✗ encode_block(s, block[6], 6);
512 ✗ encode_block(s, block[10], 10);
513 ✗ encode_block(s, block[7], 7);
514 ✗ encode_block(s, block[11], 11);
515 }
516 } else {
517
2/2
✓ Branch 0 taken 897750 times.
✓ Branch 1 taken 179550 times.
1077300 for (int i = 0; i < 5; i++)
518 897750 encode_block(s, block[i], i);
519
1/2
✓ Branch 0 taken 179550 times.
✗ Branch 1 not taken.
179550 if (s->c.chroma_format == CHROMA_420) {
520 179550 encode_block(s, block[5], 5);
521 } else {
522 ✗ encode_block(s, block[6], 6);
523 ✗ encode_block(s, block[5], 5);
524 ✗ encode_block(s, block[7], 7);
525 }
526 }
527
528 179550 s->i_tex_bits += get_bits_diff(s);
529 179550 }
530
531 33 static av_cold int mjpeg_encode_init(AVCodecContext *avctx)
532 {
533 33 MJPEGEncContext *const m2 = avctx->priv_data;
534 33 MJpegContext *const m = &m2->mjpeg;
535 33 MPVEncContext *const s = &m2->mpeg.s;
536 int ret;
537
538 33 s->mjpeg_ctx = m;
539 33 m2->mpeg.encode_picture_header = mjpeg_amv_encode_picture_header;
540 // May be overridden below
541 33 s->encode_mb = mjpeg_encode_mb;
542
543
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if (s->mpv_flags & FF_MPV_FLAG_QP_RD) {
544 // Used to produce garbage with MJPEG.
545 ✗ av_log(avctx, AV_LOG_ERROR,
546 "QP RD is no longer compatible with MJPEG or AMV\n");
547 ✗ return AVERROR(EINVAL);
548 }
549
550 /* The following check is automatically true for AMV,
551 * but it doesn't hurt either. */
552 33 ret = ff_mjpeg_encode_check_pix_fmt(avctx);
553
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if (ret < 0)
554 ✗ return ret;
555
556
2/4
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 33 times.
33 if (avctx->width > 65500 || avctx->height > 65500) {
557 ✗ av_log(avctx, AV_LOG_ERROR, "JPEG does not support resolutions above 65500x65500\n");
558 ✗ return AVERROR(EINVAL);
559 }
560
561 // Build default Huffman tables.
562 // These may be overwritten later with more optimal Huffman tables, but
563 // they are needed at least right now for some processes like trellis.
564 33 ff_mjpeg_build_huffman_codes(m->huff_size_dc_luminance,
565 33 m->huff_code_dc_luminance,
566 ff_mjpeg_bits_dc_luminance,
567 ff_mjpeg_val_dc);
568 33 ff_mjpeg_build_huffman_codes(m->huff_size_dc_chrominance,
569 33 m->huff_code_dc_chrominance,
570 ff_mjpeg_bits_dc_chrominance,
571 ff_mjpeg_val_dc);
572 33 ff_mjpeg_build_huffman_codes(m->huff_size_ac_luminance,
573 33 m->huff_code_ac_luminance,
574 ff_mjpeg_bits_ac_luminance,
575 ff_mjpeg_val_ac_luminance);
576 33 ff_mjpeg_build_huffman_codes(m->huff_size_ac_chrominance,
577 33 m->huff_code_ac_chrominance,
578 ff_mjpeg_bits_ac_chrominance,
579 ff_mjpeg_val_ac_chrominance);
580
581 33 init_uni_ac_vlc(m->huff_size_ac_luminance, m->uni_ac_vlc_len);
582 33 init_uni_ac_vlc(m->huff_size_ac_chrominance, m->uni_chroma_ac_vlc_len);
583
584 33 s->min_qcoeff = -1023;
585 33 s->max_qcoeff = 1023;
586
587 33 s->intra_ac_vlc_length =
588 33 s->intra_ac_vlc_last_length = m->uni_ac_vlc_len;
589 33 s->intra_chroma_ac_vlc_length =
590 33 s->intra_chroma_ac_vlc_last_length = m->uni_chroma_ac_vlc_len;
591
592 33 ret = ff_mpv_encode_init(avctx);
593
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if (ret < 0)
594 ✗ return ret;
595
596 // Buffers start out empty.
597 33 m->huff_ncode = 0;
598
599
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 29 times.
33 if (s->c.slice_context_count > 1)
600 4 m->huffman = HUFFMAN_TABLE_DEFAULT;
601
602
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 12 times.
33 if (m->huffman == HUFFMAN_TABLE_OPTIMAL) {
603 // If we are here, we have only one slice_context. So no loop necessary.
604 21 s->encode_mb = mjpeg_record_mb;
605 21 return alloc_huffman(m2);
606 }
607
608 12 return 0;
609 }
610
611 #if CONFIG_AMV_ENCODER
612 200 static int amv_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
613 const AVFrame *pic_arg, int *got_packet)
614 {
615 200 MPVEncContext *const s = avctx->priv_data;
616 AVFrame *pic;
617 int i, ret;
618 200 int chroma_v_shift = 1; /* AMV is 420-only */
619
620
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) {
621 ✗ av_log(avctx, AV_LOG_ERROR,
622 "Heights which are not a multiple of 16 might fail with some decoders, "
623 "use vstrict=-1 / -strict -1 to use %d anyway.\n", avctx->height);
624 ✗ av_log(avctx, AV_LOG_WARNING, "If you have a device that plays AMV videos, please test if videos "
625 "with such heights work with it and report your findings to ffmpeg-devel@ffmpeg.org\n");
626 ✗ return AVERROR_EXPERIMENTAL;
627 }
628
629 200 pic = av_frame_clone(pic_arg);
630
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 200 times.
200 if (!pic)
631 ✗ return AVERROR(ENOMEM);
632 //picture should be flipped upside-down
633
2/2
✓ Branch 0 taken 600 times.
✓ Branch 1 taken 200 times.
800 for(i=0; i < 3; i++) {
634
2/2
✓ Branch 0 taken 400 times.
✓ Branch 1 taken 200 times.
600 int v_shift = i ? chroma_v_shift : 0;
635 600 pic->data[i] += pic->linesize[i] * (AV_CEIL_RSHIFT(s->c.height, v_shift) - 1);
636 600 pic->linesize[i] *= -1;
637 }
638 200 ret = ff_mpv_encode_picture(avctx, pkt, pic, got_packet);
639 200 av_frame_free(&pic);
640 200 return ret;
641 }
642 #endif
643
644 #define OFFSET(x) offsetof(MJPEGEncContext, mjpeg.x)
645 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
646 static const AVOption options[] = {
647 #define AMV_OPTIONS_OFFSET 5
648 { "huffman", "Huffman table strategy", OFFSET(huffman), AV_OPT_TYPE_INT, { .i64 = HUFFMAN_TABLE_OPTIMAL }, 0, NB_HUFFMAN_TABLE_OPTION - 1, VE, .unit = "huffman" },
649 { "default", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = HUFFMAN_TABLE_DEFAULT }, INT_MIN, INT_MAX, VE, .unit = "huffman" },
650 { "optimal", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = HUFFMAN_TABLE_OPTIMAL }, INT_MIN, INT_MAX, VE, .unit = "huffman" },
651 { "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 },
652 { "xmp_gain_map", "Write Ultra HDR gain map metadata as an XMP packet", OFFSET(xmp_gain_map), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VE },
653 FF_MPV_COMMON_OPTS
654 { NULL},
655 };
656
657 #if CONFIG_MJPEG_ENCODER
658 static const AVClass mjpeg_class = {
659 .class_name = "mjpeg encoder",
660 .item_name = av_default_item_name,
661 .option = options,
662 .version = LIBAVUTIL_VERSION_INT,
663 };
664
665 230 static int mjpeg_get_supported_config(const AVCodecContext *avctx,
666 const AVCodec *codec,
667 enum AVCodecConfig config,
668 unsigned flags, const void **out,
669 int *out_num)
670 {
671
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 201 times.
230 if (config == AV_CODEC_CONFIG_COLOR_RANGE) {
672 static const enum AVColorRange mjpeg_ranges[] = {
673 AVCOL_RANGE_MPEG, AVCOL_RANGE_JPEG, AVCOL_RANGE_UNSPECIFIED,
674 };
675
1/2
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
29 int strict = avctx ? avctx->strict_std_compliance : 0;
676 29 int index = strict > FF_COMPLIANCE_UNOFFICIAL ? 1 : 0;
677 29 *out = &mjpeg_ranges[index];
678 29 *out_num = FF_ARRAY_ELEMS(mjpeg_ranges) - index - 1;
679 29 return 0;
680 }
681
682 201 return ff_default_get_supported_config(avctx, codec, config, flags, out, out_num);
683 }
684
685 const FFCodec ff_mjpeg_encoder = {
686 .p.name = "mjpeg",
687 CODEC_LONG_NAME("MJPEG (Motion JPEG)"),
688 .p.type = AVMEDIA_TYPE_VIDEO,
689 .p.id = AV_CODEC_ID_MJPEG,
690 .priv_data_size = sizeof(MJPEGEncContext),
691 .init = mjpeg_encode_init,
692 FF_CODEC_ENCODE_CB(ff_mpv_encode_picture),
693 .close = mjpeg_encode_close,
694 .p.capabilities = AV_CODEC_CAP_DR1 |
695 AV_CODEC_CAP_SLICE_THREADS | AV_CODEC_CAP_FRAME_THREADS |
696 AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
697 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP | FF_CODEC_CAP_ICC_PROFILES,
698 CODEC_PIXFMTS(AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P,
699 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P),
700 .p.priv_class = &mjpeg_class,
701 .p.profiles = NULL_IF_CONFIG_SMALL(ff_mjpeg_profiles),
702 .get_supported_config = mjpeg_get_supported_config,
703 };
704 #endif
705
706 #if CONFIG_AMV_ENCODER
707 static const AVClass amv_class = {
708 .class_name = "amv encoder",
709 .item_name = av_default_item_name,
710 .option = options + AMV_OPTIONS_OFFSET,
711 .version = LIBAVUTIL_VERSION_INT,
712 };
713
714 const FFCodec ff_amv_encoder = {
715 .p.name = "amv",
716 CODEC_LONG_NAME("AMV Video"),
717 .p.type = AVMEDIA_TYPE_VIDEO,
718 .p.id = AV_CODEC_ID_AMV,
719 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
720 .priv_data_size = sizeof(MJPEGEncContext),
721 .init = mjpeg_encode_init,
722 FF_CODEC_ENCODE_CB(amv_encode_picture),
723 .close = mjpeg_encode_close,
724 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
725 CODEC_PIXFMTS(AV_PIX_FMT_YUVJ420P),
726 .color_ranges = AVCOL_RANGE_JPEG,
727 .p.priv_class = &amv_class,
728 };
729 #endif
730