FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mjpegenc.c
Date: 2024-03-28 14:59:00
Exec Total Coverage
Lines: 285 317 89.9%
Functions: 15 15 100.0%
Branches: 93 118 78.8%

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