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