FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mjpegenc.c
Date: 2025-04-25 22:50:00
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 1740 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 222720 times.
✓ Branch 1 taken 1740 times.
224460 for (int i = 0; i < 128; i++) {
75 222720 int level = i - 64;
76
2/2
✓ Branch 0 taken 1740 times.
✓ Branch 1 taken 220980 times.
222720 if (!level)
77 1740 continue;
78
2/2
✓ Branch 0 taken 14142720 times.
✓ Branch 1 taken 220980 times.
14363700 for (int run = 0; run < 64; run++) {
79 int len, code, nbits;
80 14142720 int alevel = FFABS(level);
81
82 14142720 len = (run >> 4) * huff_size_ac[0xf0];
83
84 14142720 nbits= av_log2_16bit(alevel) + 1;
85 14142720 code = ((15&run) << 4) | nbits;
86
87 14142720 len += huff_size_ac[code] + nbits;
88
89 14142720 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 1740 }
94
95 1439 static void mjpeg_encode_picture_header(MPVEncContext *const s)
96 {
97 1439 ff_mjpeg_encode_picture_header(s->c.avctx, &s->pb, s->c.cur_pic.ptr->f, s->mjpeg_ctx,
98 1439 s->c.intra_scantable.permutated, 0,
99 1439 s->c.intra_matrix, s->c.chroma_intra_matrix,
100 1439 s->c.slice_context_count > 1);
101
102 1439 s->esc_pos = put_bytes_count(&s->pb, 0);
103
2/2
✓ Branch 0 taken 700 times.
✓ Branch 1 taken 1439 times.
2139 for (int i = 1; i < s->c.slice_context_count; i++)
104 700 s->c.enc_contexts[i]->esc_pos = 0;
105 1439 }
106
107 1439 static int mjpeg_amv_encode_picture_header(MPVMainEncContext *const m)
108 {
109 1439 MJPEGEncContext *const m2 = (MJPEGEncContext*)m;
110 1439 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 839 times.
1439 if (!CONFIG_MJPEG_ENCODER || m2->mjpeg.huffman != HUFFMAN_TABLE_OPTIMAL)
114 600 mjpeg_encode_picture_header(s);
115
116 1439 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 839 static void mjpeg_encode_picture_frame(MPVMainEncContext *const main)
126 {
127 839 MPVEncContext *const s = &main->s;
128 int nbits, code, table_id;
129 839 MJpegContext *m = s->mjpeg_ctx;
130 839 uint8_t *huff_size[4] = { m->huff_size_dc_luminance,
131 839 m->huff_size_dc_chrominance,
132 839 m->huff_size_ac_luminance,
133 839 m->huff_size_ac_chrominance };
134 839 uint16_t *huff_code[4] = { m->huff_code_dc_luminance,
135 839 m->huff_code_dc_chrominance,
136 839 m->huff_code_ac_luminance,
137 839 m->huff_code_ac_chrominance };
138 839 size_t total_bits = 0;
139 size_t bytes_needed;
140
141 839 main->header_bits = get_bits_diff(s);
142 // Estimate the total size first
143
2/2
✓ Branch 0 taken 20394735 times.
✓ Branch 1 taken 839 times.
20395574 for (int i = 0; i < m->huff_ncode; i++) {
144 20394735 table_id = m->huff_buffer[i].table_id;
145 20394735 code = m->huff_buffer[i].code;
146 20394735 nbits = code & 0xf;
147
148 20394735 total_bits += huff_size[table_id][code] + nbits;
149 }
150
151 839 bytes_needed = (total_bits + 7) / 8;
152 839 ff_mpv_reallocate_putbitbuffer(s, bytes_needed, bytes_needed);
153
154
2/2
✓ Branch 0 taken 20394735 times.
✓ Branch 1 taken 839 times.
20395574 for (int i = 0; i < m->huff_ncode; i++) {
155 20394735 table_id = m->huff_buffer[i].table_id;
156 20394735 code = m->huff_buffer[i].code;
157 20394735 nbits = code & 0xf;
158
159 20394735 put_bits(&s->pb, huff_size[table_id][code], huff_code[table_id][code]);
160
2/2
✓ Branch 0 taken 18318883 times.
✓ Branch 1 taken 2075852 times.
20394735 if (nbits != 0) {
161 18318883 put_sbits(&s->pb, nbits, m->huff_buffer[i].mant);
162 }
163 }
164
165 839 m->huff_ncode = 0;
166 839 s->i_tex_bits = get_bits_diff(s);
167 839 }
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 839 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 839 MJpegEncHuffmanContext *ctx[4] = { &dc_luminance_ctx,
184 &dc_chrominance_ctx,
185 &ac_luminance_ctx,
186 &ac_chrominance_ctx };
187
2/2
✓ Branch 0 taken 3356 times.
✓ Branch 1 taken 839 times.
4195 for (int i = 0; i < 4; i++)
188 3356 ff_mjpeg_encode_huffman_init(ctx[i]);
189
190
2/2
✓ Branch 0 taken 20394735 times.
✓ Branch 1 taken 839 times.
20395574 for (int i = 0; i < m->huff_ncode; i++) {
191 20394735 int table_id = m->huff_buffer[i].table_id;
192 20394735 int code = m->huff_buffer[i].code;
193
194 20394735 ff_mjpeg_encode_huffman_increment(ctx[table_id], code);
195 }
196
197 839 ff_mjpeg_encode_huffman_close(&dc_luminance_ctx,
198 839 m->bits_dc_luminance,
199 839 m->val_dc_luminance, 12);
200 839 ff_mjpeg_encode_huffman_close(&dc_chrominance_ctx,
201 839 m->bits_dc_chrominance,
202 839 m->val_dc_chrominance, 12);
203 839 ff_mjpeg_encode_huffman_close(&ac_luminance_ctx,
204 839 m->bits_ac_luminance,
205 839 m->val_ac_luminance, 256);
206 839 ff_mjpeg_encode_huffman_close(&ac_chrominance_ctx,
207 839 m->bits_ac_chrominance,
208 839 m->val_ac_chrominance, 256);
209
210 839 ff_mjpeg_build_huffman_codes(m->huff_size_dc_luminance,
211 839 m->huff_code_dc_luminance,
212 839 m->bits_dc_luminance,
213 839 m->val_dc_luminance);
214 839 ff_mjpeg_build_huffman_codes(m->huff_size_dc_chrominance,
215 839 m->huff_code_dc_chrominance,
216 839 m->bits_dc_chrominance,
217 839 m->val_dc_chrominance);
218 839 ff_mjpeg_build_huffman_codes(m->huff_size_ac_luminance,
219 839 m->huff_code_ac_luminance,
220 839 m->bits_ac_luminance,
221 839 m->val_ac_luminance);
222 839 ff_mjpeg_build_huffman_codes(m->huff_size_ac_chrominance,
223 839 m->huff_code_ac_chrominance,
224 839 m->bits_ac_chrominance,
225 839 m->val_ac_chrominance);
226 839 }
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 4089 int ff_mjpeg_encode_stuffing(MPVEncContext *const s)
239 {
240 4089 MJpegContext *const m = s->mjpeg_ctx;
241 4089 PutBitContext *pbc = &s->pb;
242 4089 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 839 times.
✓ Branch 1 taken 3250 times.
4089 if (m->huffman == HUFFMAN_TABLE_OPTIMAL) {
247 /* HUFFMAN_TABLE_OPTIMAL is incompatible with slice threading,
248 * therefore the following cast is allowed. */
249 839 MPVMainEncContext *const main = (MPVMainEncContext*)s;
250
251 839 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 839 init_uni_ac_vlc(m->huff_size_ac_luminance, m->uni_ac_vlc_len);
256 839 init_uni_ac_vlc(m->huff_size_ac_chrominance, m->uni_chroma_ac_vlc_len);
257 839 s->intra_ac_vlc_length =
258 839 s->intra_ac_vlc_last_length = m->uni_ac_vlc_len;
259 839 s->intra_chroma_ac_vlc_length =
260 839 s->intra_chroma_ac_vlc_last_length = m->uni_chroma_ac_vlc_len;
261
262 839 mjpeg_encode_picture_header(s);
263 839 mjpeg_encode_picture_frame(main);
264 }
265 #endif
266
267 4089 ret = ff_mpv_reallocate_putbitbuffer(s, put_bits_count(&s->pb) / 8 + 100,
268 4089 put_bits_count(&s->pb) / 4 + 1000);
269
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4089 times.
4089 if (ret < 0) {
270 av_log(s->c.avctx, AV_LOG_ERROR, "Buffer reallocation failed\n");
271 goto fail;
272 }
273
274 4089 ff_mjpeg_escape_FF(pbc, s->esc_pos);
275
276
4/4
✓ Branch 0 taken 2850 times.
✓ Branch 1 taken 1239 times.
✓ Branch 2 taken 2650 times.
✓ Branch 3 taken 200 times.
4089 if (s->c.slice_context_count > 1 && mb_y < s->c.mb_height - 1)
277 2650 put_marker(pbc, RST0 + (mb_y&7));
278 4089 s->esc_pos = put_bytes_count(pbc, 0);
279
280 4089 fail:
281
2/2
✓ Branch 0 taken 12267 times.
✓ Branch 1 taken 4089 times.
16356 for (int i = 0; i < 3; i++)
282 12267 s->c.last_dc[i] = 128 << s->c.intra_dc_precision;
283
284 4089 return ret;
285 }
286
287 19 static int alloc_huffman(MJPEGEncContext *const m2)
288 {
289 19 MJpegContext *const m = &m2->mjpeg;
290 19 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 19 num_blocks = s->c.mb_num * blocks_per_mb[s->c.chroma_format];
298
299 19 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 19 times.
19 if (!m->huff_buffer)
302 return AVERROR(ENOMEM);
303 19 return 0;
304 }
305
306 31 static av_cold int mjpeg_encode_close(AVCodecContext *avctx)
307 {
308 31 MJPEGEncContext *const mjpeg = avctx->priv_data;
309 31 av_freep(&mjpeg->mjpeg.huff_buffer);
310 31 ff_mpv_encode_end(avctx);
311 31 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 20394735 static inline void mjpeg_encode_code(MJpegContext *s, uint8_t table_id, int code)
322 {
323 20394735 MJpegHuffmanCode *c = &s->huff_buffer[s->huff_ncode++];
324 20394735 c->table_id = table_id;
325 20394735 c->code = code;
326 20394735 }
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 18399604 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 80721 times.
✓ Branch 1 taken 18318883 times.
18399604 if (val == 0) {
341
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 80721 times.
80721 av_assert0(run == 0);
342 80721 mjpeg_encode_code(s, table_id, 0);
343 } else {
344 18318883 mant = val;
345
2/2
✓ Branch 0 taken 9146326 times.
✓ Branch 1 taken 9172557 times.
18318883 if (val < 0) {
346 9146326 val = -val;
347 9146326 mant--;
348 }
349
350 18318883 code = (run << 4) | (av_log2_16bit(val) + 1);
351
352 18318883 s->huff_buffer[s->huff_ncode].mant = mant;
353 18318883 mjpeg_encode_code(s, table_id, code);
354 }
355 18399604 }
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 2005356 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 2005356 MJpegContext *m = s->mjpeg_ctx;
369
370 /* DC coef */
371
2/2
✓ Branch 0 taken 987608 times.
✓ Branch 1 taken 1017748 times.
2005356 component = (n <= 3 ? 0 : (n&1) + 1);
372 2005356 table_id = (n <= 3 ? 0 : 1);
373 2005356 dc = block[0]; /* overflow is impossible */
374 2005356 val = dc - s->c.last_dc[component];
375
376 2005356 mjpeg_encode_coef(m, table_id, val, 0);
377
378 2005356 s->c.last_dc[component] = dc;
379
380 /* AC coefs */
381
382 2005356 run = 0;
383 2005356 last_index = s->c.block_last_index[n];
384 2005356 table_id |= 2;
385
386
2/2
✓ Branch 0 taken 35322338 times.
✓ Branch 1 taken 2005356 times.
37327694 for(i=1;i<=last_index;i++) {
387 35322338 j = s->c.intra_scantable.permutated[i];
388 35322338 val = block[j];
389
390
2/2
✓ Branch 0 taken 18928090 times.
✓ Branch 1 taken 16394248 times.
35322338 if (val == 0) {
391 18928090 run++;
392 } else {
393
2/2
✓ Branch 0 taken 43994 times.
✓ Branch 1 taken 16394248 times.
16438242 while (run >= 16) {
394 43994 mjpeg_encode_code(m, table_id, 0xf0);
395 43994 run -= 16;
396 }
397 16394248 mjpeg_encode_coef(m, table_id, val, run);
398 16394248 run = 0;
399 }
400 }
401
402 /* output EOB only if not already 64 values */
403
3/4
✓ Branch 0 taken 54219 times.
✓ Branch 1 taken 1951137 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 54219 times.
2005356 if (last_index < 63 || run != 0)
404 1951137 mjpeg_encode_code(m, table_id, 0);
405 2005356 }
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->c.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->c.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 21120946 times.
✓ Branch 1 taken 1077300 times.
22198246 for(i=1;i<=last_index;i++) {
435 21120946 j = s->c.intra_scantable.permutated[i];
436 21120946 val = block[j];
437
2/2
✓ Branch 0 taken 10604708 times.
✓ Branch 1 taken 10516238 times.
21120946 if (val == 0) {
438 10604708 run++;
439 } else {
440
2/2
✓ Branch 0 taken 19627 times.
✓ Branch 1 taken 10516238 times.
10535865 while (run >= 16) {
441 19627 put_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
442 19627 run -= 16;
443 }
444 10516238 mant = val;
445
2/2
✓ Branch 0 taken 5281413 times.
✓ Branch 1 taken 5234825 times.
10516238 if (val < 0) {
446 5281413 val = -val;
447 5281413 mant--;
448 }
449
450 10516238 nbits= av_log2_16bit(val) + 1;
451 10516238 code = (run << 4) | nbits;
452
453 10516238 put_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
454
455 10516238 put_sbits(&s->pb, nbits, mant);
456 10516238 run = 0;
457 }
458 }
459
460 /* output EOB only if not already 64 values */
461
3/4
✓ Branch 0 taken 40270 times.
✓ Branch 1 taken 1037030 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 40270 times.
1077300 if (last_index < 63 || run != 0)
462 1037030 put_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
463 1077300 }
464
465 254512 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 59914 times.
✓ Branch 1 taken 194598 times.
254512 if (s->c.chroma_format == CHROMA_444) {
469 59914 record_block(s, block[0], 0);
470 59914 record_block(s, block[2], 2);
471 59914 record_block(s, block[4], 4);
472 59914 record_block(s, block[8], 8);
473 59914 record_block(s, block[5], 5);
474 59914 record_block(s, block[9], 9);
475
476
2/2
✓ Branch 0 taken 59764 times.
✓ Branch 1 taken 150 times.
59914 if (16*s->c.mb_x+8 < s->c.width) {
477 59764 record_block(s, block[1], 1);
478 59764 record_block(s, block[3], 3);
479 59764 record_block(s, block[6], 6);
480 59764 record_block(s, block[10], 10);
481 59764 record_block(s, block[7], 7);
482 59764 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 254512 }
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 31 static av_cold int mjpeg_encode_init(AVCodecContext *avctx)
532 {
533 31 MJPEGEncContext *const m2 = avctx->priv_data;
534 31 MJpegContext *const m = &m2->mjpeg;
535 31 MPVEncContext *const s = &m2->mpeg.s;
536 int ret;
537
538 31 s->mjpeg_ctx = m;
539 31 m2->mpeg.encode_picture_header = mjpeg_amv_encode_picture_header;
540 // May be overridden below
541 31 s->encode_mb = mjpeg_encode_mb;
542
543
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 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 31 ret = ff_mjpeg_encode_check_pix_fmt(avctx);
553
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 if (ret < 0)
554 return ret;
555
556
2/4
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 31 times.
31 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 31 ff_mjpeg_build_huffman_codes(m->huff_size_dc_luminance,
565 31 m->huff_code_dc_luminance,
566 ff_mjpeg_bits_dc_luminance,
567 ff_mjpeg_val_dc);
568 31 ff_mjpeg_build_huffman_codes(m->huff_size_dc_chrominance,
569 31 m->huff_code_dc_chrominance,
570 ff_mjpeg_bits_dc_chrominance,
571 ff_mjpeg_val_dc);
572 31 ff_mjpeg_build_huffman_codes(m->huff_size_ac_luminance,
573 31 m->huff_code_ac_luminance,
574 ff_mjpeg_bits_ac_luminance,
575 ff_mjpeg_val_ac_luminance);
576 31 ff_mjpeg_build_huffman_codes(m->huff_size_ac_chrominance,
577 31 m->huff_code_ac_chrominance,
578 ff_mjpeg_bits_ac_chrominance,
579 ff_mjpeg_val_ac_chrominance);
580
581 31 init_uni_ac_vlc(m->huff_size_ac_luminance, m->uni_ac_vlc_len);
582 31 init_uni_ac_vlc(m->huff_size_ac_chrominance, m->uni_chroma_ac_vlc_len);
583
584 31 s->min_qcoeff = -1023;
585 31 s->max_qcoeff = 1023;
586
587 31 s->intra_ac_vlc_length =
588 31 s->intra_ac_vlc_last_length = m->uni_ac_vlc_len;
589 31 s->intra_chroma_ac_vlc_length =
590 31 s->intra_chroma_ac_vlc_last_length = m->uni_chroma_ac_vlc_len;
591
592 31 ret = ff_mpv_encode_init(avctx);
593
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 if (ret < 0)
594 return ret;
595
596 // Buffers start out empty.
597 31 m->huff_ncode = 0;
598
599
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 27 times.
31 if (s->c.slice_context_count > 1)
600 4 m->huffman = HUFFMAN_TABLE_DEFAULT;
601
602
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 12 times.
31 if (m->huffman == HUFFMAN_TABLE_OPTIMAL) {
603 // If we are here, we have only one slice_context. So no loop necessary.
604 19 s->encode_mb = mjpeg_record_mb;
605 19 return alloc_huffman(m2);
606 }
607
608 12 return 0;
609 }
610
611 #if CONFIG_AMV_ENCODER
612 // maximum over s->mjpeg_vsample[i]
613 #define V_MAX 2
614 200 static int amv_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
615 const AVFrame *pic_arg, int *got_packet)
616 {
617 200 MPVEncContext *const s = avctx->priv_data;
618 AVFrame *pic;
619 int i, ret;
620 200 int chroma_v_shift = 1; /* AMV is 420-only */
621
622
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) {
623 av_log(avctx, AV_LOG_ERROR,
624 "Heights which are not a multiple of 16 might fail with some decoders, "
625 "use vstrict=-1 / -strict -1 to use %d anyway.\n", avctx->height);
626 av_log(avctx, AV_LOG_WARNING, "If you have a device that plays AMV videos, please test if videos "
627 "with such heights work with it and report your findings to ffmpeg-devel@ffmpeg.org\n");
628 return AVERROR_EXPERIMENTAL;
629 }
630
631 200 pic = av_frame_clone(pic_arg);
632
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 200 times.
200 if (!pic)
633 return AVERROR(ENOMEM);
634 //picture should be flipped upside-down
635
2/2
✓ Branch 0 taken 600 times.
✓ Branch 1 taken 200 times.
800 for(i=0; i < 3; i++) {
636
2/2
✓ Branch 0 taken 400 times.
✓ Branch 1 taken 200 times.
600 int vsample = i ? 2 >> chroma_v_shift : 2;
637 600 pic->data[i] += pic->linesize[i] * (vsample * s->c.height / V_MAX - 1);
638 600 pic->linesize[i] *= -1;
639 }
640 200 ret = ff_mpv_encode_picture(avctx, pkt, pic, got_packet);
641 200 av_frame_free(&pic);
642 200 return ret;
643 }
644 #endif
645
646 #define OFFSET(x) offsetof(MJPEGEncContext, mjpeg.x)
647 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
648 static const AVOption options[] = {
649 #define AMV_OPTIONS_OFFSET 4
650 { "huffman", "Huffman table strategy", OFFSET(huffman), AV_OPT_TYPE_INT, { .i64 = HUFFMAN_TABLE_OPTIMAL }, 0, NB_HUFFMAN_TABLE_OPTION - 1, VE, .unit = "huffman" },
651 { "default", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = HUFFMAN_TABLE_DEFAULT }, INT_MIN, INT_MAX, VE, .unit = "huffman" },
652 { "optimal", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = HUFFMAN_TABLE_OPTIMAL }, INT_MIN, INT_MAX, VE, .unit = "huffman" },
653 { "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 },
654 FF_MPV_COMMON_OPTS
655 { NULL},
656 };
657
658 #if CONFIG_MJPEG_ENCODER
659 static const AVClass mjpeg_class = {
660 .class_name = "mjpeg encoder",
661 .item_name = av_default_item_name,
662 .option = options,
663 .version = LIBAVUTIL_VERSION_INT,
664 };
665
666 160 static int mjpeg_get_supported_config(const AVCodecContext *avctx,
667 const AVCodec *codec,
668 enum AVCodecConfig config,
669 unsigned flags, const void **out,
670 int *out_num)
671 {
672
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 133 times.
160 if (config == AV_CODEC_CONFIG_COLOR_RANGE) {
673 static const enum AVColorRange mjpeg_ranges[] = {
674 AVCOL_RANGE_MPEG, AVCOL_RANGE_JPEG, AVCOL_RANGE_UNSPECIFIED,
675 };
676
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 int strict = avctx ? avctx->strict_std_compliance : 0;
677 27 int index = strict > FF_COMPLIANCE_UNOFFICIAL ? 1 : 0;
678 27 *out = &mjpeg_ranges[index];
679 27 *out_num = FF_ARRAY_ELEMS(mjpeg_ranges) - index - 1;
680 27 return 0;
681 }
682
683 133 return ff_default_get_supported_config(avctx, codec, config, flags, out, out_num);
684 }
685
686 const FFCodec ff_mjpeg_encoder = {
687 .p.name = "mjpeg",
688 CODEC_LONG_NAME("MJPEG (Motion JPEG)"),
689 .p.type = AVMEDIA_TYPE_VIDEO,
690 .p.id = AV_CODEC_ID_MJPEG,
691 .priv_data_size = sizeof(MJPEGEncContext),
692 .init = mjpeg_encode_init,
693 FF_CODEC_ENCODE_CB(ff_mpv_encode_picture),
694 .close = mjpeg_encode_close,
695 .p.capabilities = AV_CODEC_CAP_DR1 |
696 AV_CODEC_CAP_SLICE_THREADS | AV_CODEC_CAP_FRAME_THREADS |
697 AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
698 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP | FF_CODEC_CAP_ICC_PROFILES,
699 CODEC_PIXFMTS(AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P,
700 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P),
701 .p.priv_class = &mjpeg_class,
702 .p.profiles = NULL_IF_CONFIG_SMALL(ff_mjpeg_profiles),
703 .get_supported_config = mjpeg_get_supported_config,
704 };
705 #endif
706
707 #if CONFIG_AMV_ENCODER
708 static const AVClass amv_class = {
709 .class_name = "amv encoder",
710 .item_name = av_default_item_name,
711 .option = options + AMV_OPTIONS_OFFSET,
712 .version = LIBAVUTIL_VERSION_INT,
713 };
714
715 const FFCodec ff_amv_encoder = {
716 .p.name = "amv",
717 CODEC_LONG_NAME("AMV Video"),
718 .p.type = AVMEDIA_TYPE_VIDEO,
719 .p.id = AV_CODEC_ID_AMV,
720 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
721 .priv_data_size = sizeof(MJPEGEncContext),
722 .init = mjpeg_encode_init,
723 FF_CODEC_ENCODE_CB(amv_encode_picture),
724 .close = mjpeg_encode_close,
725 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
726 CODEC_PIXFMTS(AV_PIX_FMT_YUVJ420P),
727 .color_ranges = AVCOL_RANGE_JPEG,
728 .p.priv_class = &amv_class,
729 };
730 #endif
731