Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * SpeedHQ encoder | ||
3 | * Copyright (c) 2000, 2001 Fabrice Bellard | ||
4 | * Copyright (c) 2003 Alex Beregszaszi | ||
5 | * Copyright (c) 2003-2004 Michael Niedermayer | ||
6 | * Copyright (c) 2020 FFmpeg | ||
7 | * | ||
8 | * This file is part of FFmpeg. | ||
9 | * | ||
10 | * FFmpeg is free software; you can redistribute it and/or | ||
11 | * modify it under the terms of the GNU Lesser General Public | ||
12 | * License as published by the Free Software Foundation; either | ||
13 | * version 2.1 of the License, or (at your option) any later version. | ||
14 | * | ||
15 | * FFmpeg is distributed in the hope that it will be useful, | ||
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
18 | * Lesser General Public License for more details. | ||
19 | * | ||
20 | * You should have received a copy of the GNU Lesser General Public | ||
21 | * License along with FFmpeg; if not, write to the Free Software | ||
22 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
23 | */ | ||
24 | |||
25 | /** | ||
26 | * @file | ||
27 | * SpeedHQ encoder. | ||
28 | */ | ||
29 | |||
30 | #include "libavutil/avassert.h" | ||
31 | #include "libavutil/thread.h" | ||
32 | |||
33 | #include "avcodec.h" | ||
34 | #include "codec_internal.h" | ||
35 | #include "mathops.h" | ||
36 | #include "mpeg12data.h" | ||
37 | #include "mpeg12vlc.h" | ||
38 | #include "mpegvideo.h" | ||
39 | #include "mpegvideodata.h" | ||
40 | #include "mpegvideoenc.h" | ||
41 | #include "put_bits.h" | ||
42 | #include "rl.h" | ||
43 | #include "speedhq.h" | ||
44 | #include "speedhqenc.h" | ||
45 | |||
46 | static uint8_t speedhq_max_level[MAX_LEVEL + 1]; | ||
47 | static uint8_t speedhq_index_run[MAX_RUN + 1]; | ||
48 | |||
49 | /* Exactly the same as MPEG-2, except little-endian. */ | ||
50 | static const uint16_t mpeg12_vlc_dc_lum_code_reversed[12] = { | ||
51 | 0x1, 0x0, 0x2, 0x5, 0x3, 0x7, 0xF, 0x1F, 0x3F, 0x7F, 0xFF, 0x1FF | ||
52 | }; | ||
53 | static const uint16_t mpeg12_vlc_dc_chroma_code_reversed[12] = { | ||
54 | 0x0, 0x2, 0x1, 0x3, 0x7, 0xF, 0x1F, 0x3F, 0x7F, 0xFF, 0x1FF, 0x3FF | ||
55 | }; | ||
56 | |||
57 | /* simple include everything table for dc, first byte is bits | ||
58 | * number next 3 are code */ | ||
59 | static uint32_t speedhq_lum_dc_uni[512]; | ||
60 | static uint32_t speedhq_chr_dc_uni[512]; | ||
61 | |||
62 | static uint8_t uni_speedhq_ac_vlc_len[64 * 64 * 2]; | ||
63 | |||
64 | typedef struct SpeedHQEncContext { | ||
65 | MPVMainEncContext m; | ||
66 | |||
67 | int slice_start; | ||
68 | } SpeedHQEncContext; | ||
69 | |||
70 | 9 | static av_cold void speedhq_init_static_data(void) | |
71 | { | ||
72 | 9 | ff_rl_init_level_run(speedhq_max_level, speedhq_index_run, | |
73 | ff_speedhq_run, ff_speedhq_level, SPEEDHQ_RL_NB_ELEMS); | ||
74 | |||
75 | /* build unified dc encoding tables */ | ||
76 |
2/2✓ Branch 0 taken 4599 times.
✓ Branch 1 taken 9 times.
|
4608 | for (int i = -255; i < 256; i++) { |
77 | int adiff, index; | ||
78 | int bits, code; | ||
79 | 4599 | int diff = i; | |
80 | |||
81 | 4599 | adiff = FFABS(diff); | |
82 |
2/2✓ Branch 0 taken 2295 times.
✓ Branch 1 taken 2304 times.
|
4599 | if (diff < 0) |
83 | 2295 | diff--; | |
84 | 4599 | index = av_log2(2 * adiff); | |
85 | |||
86 | 4599 | bits = ff_mpeg12_vlc_dc_lum_bits[index] + index; | |
87 | 4599 | code = mpeg12_vlc_dc_lum_code_reversed[index] + | |
88 | 4599 | (av_zero_extend(diff, index) << ff_mpeg12_vlc_dc_lum_bits[index]); | |
89 | 4599 | speedhq_lum_dc_uni[i + 255] = bits + (code << 8); | |
90 | |||
91 | 4599 | bits = ff_mpeg12_vlc_dc_chroma_bits[index] + index; | |
92 | 4599 | code = mpeg12_vlc_dc_chroma_code_reversed[index] + | |
93 | 4599 | (av_zero_extend(diff, index) << ff_mpeg12_vlc_dc_chroma_bits[index]); | |
94 | 4599 | speedhq_chr_dc_uni[i + 255] = bits + (code << 8); | |
95 | } | ||
96 | |||
97 | 9 | ff_mpeg1_init_uni_ac_vlc(speedhq_max_level, speedhq_index_run, | |
98 | ff_speedhq_vlc_table, uni_speedhq_ac_vlc_len); | ||
99 | 9 | } | |
100 | |||
101 | 450 | static int speedhq_encode_picture_header(MPVMainEncContext *const m) | |
102 | { | ||
103 | 450 | SpeedHQEncContext *const ctx = (SpeedHQEncContext*)m; | |
104 | 450 | MPVEncContext *const s = &m->s; | |
105 | |||
106 | 450 | put_bits_assume_flushed(&s->pb); | |
107 | |||
108 | 450 | put_bits_le(&s->pb, 8, 100 - s->c.qscale * 2); /* FIXME why doubled */ | |
109 | 450 | put_bits_le(&s->pb, 24, 4); /* no second field */ | |
110 | |||
111 | 450 | ctx->slice_start = 4; | |
112 | /* length of first slice, will be filled out later */ | ||
113 | 450 | put_bits_le(&s->pb, 24, 0); | |
114 | |||
115 | 450 | return 0; | |
116 | } | ||
117 | |||
118 | 1800 | void ff_speedhq_end_slice(MPVEncContext *const s) | |
119 | { | ||
120 | 1800 | SpeedHQEncContext *ctx = (SpeedHQEncContext*)s; | |
121 | int slice_len; | ||
122 | |||
123 | 1800 | flush_put_bits_le(&s->pb); | |
124 | 1800 | slice_len = put_bytes_output(&s->pb) - ctx->slice_start; | |
125 | 1800 | AV_WL24(s->pb.buf + ctx->slice_start, slice_len); | |
126 | |||
127 | /* length of next slice, will be filled out later */ | ||
128 | 1800 | ctx->slice_start = put_bytes_output(&s->pb); | |
129 | 1800 | put_bits_le(&s->pb, 24, 0); | |
130 | 1800 | } | |
131 | |||
132 | 1544400 | static inline void encode_dc(PutBitContext *pb, int diff, int component) | |
133 | { | ||
134 | 1544400 | unsigned int diff_u = diff + 255; | |
135 |
2/2✓ Branch 0 taken 329487 times.
✓ Branch 1 taken 1214913 times.
|
1544400 | if (diff_u >= 511) { |
136 | int index; | ||
137 | |||
138 |
2/2✓ Branch 0 taken 169357 times.
✓ Branch 1 taken 160130 times.
|
329487 | if (diff < 0) { |
139 | 169357 | index = av_log2_16bit(-2 * diff); | |
140 | 169357 | diff--; | |
141 | } else { | ||
142 | 160130 | index = av_log2_16bit(2 * diff); | |
143 | } | ||
144 |
2/2✓ Branch 0 taken 239838 times.
✓ Branch 1 taken 89649 times.
|
329487 | if (component == 0) |
145 | 239838 | put_bits_le(pb, | |
146 | 239838 | ff_mpeg12_vlc_dc_lum_bits[index] + index, | |
147 | 239838 | mpeg12_vlc_dc_lum_code_reversed[index] + | |
148 | 239838 | (av_zero_extend(diff, index) << ff_mpeg12_vlc_dc_lum_bits[index])); | |
149 | else | ||
150 | 89649 | put_bits_le(pb, | |
151 | 89649 | ff_mpeg12_vlc_dc_chroma_bits[index] + index, | |
152 | 89649 | mpeg12_vlc_dc_chroma_code_reversed[index] + | |
153 | 89649 | (av_zero_extend(diff, index) << ff_mpeg12_vlc_dc_chroma_bits[index])); | |
154 | } else { | ||
155 |
2/2✓ Branch 0 taken 472962 times.
✓ Branch 1 taken 741951 times.
|
1214913 | if (component == 0) |
156 | 472962 | put_bits_le(pb, | |
157 | 472962 | speedhq_lum_dc_uni[diff + 255] & 0xFF, | |
158 | 472962 | speedhq_lum_dc_uni[diff + 255] >> 8); | |
159 | else | ||
160 | 741951 | put_bits_le(pb, | |
161 | 741951 | speedhq_chr_dc_uni[diff + 255] & 0xFF, | |
162 | 741951 | speedhq_chr_dc_uni[diff + 255] >> 8); | |
163 | } | ||
164 | 1544400 | } | |
165 | |||
166 | 1544400 | static void encode_block(MPVEncContext *const s, const int16_t block[], int n) | |
167 | { | ||
168 | int alevel, level, last_non_zero, dc, i, j, run, last_index, sign; | ||
169 | int code; | ||
170 | int component, val; | ||
171 | |||
172 | /* DC coef */ | ||
173 |
2/2✓ Branch 0 taken 831600 times.
✓ Branch 1 taken 712800 times.
|
1544400 | component = (n <= 3 ? 0 : (n&1) + 1); |
174 | 1544400 | dc = block[0]; /* overflow is impossible */ | |
175 | 1544400 | val = s->c.last_dc[component] - dc; /* opposite of most codecs */ | |
176 | 1544400 | encode_dc(&s->pb, val, component); | |
177 | 1544400 | s->c.last_dc[component] = dc; | |
178 | |||
179 | /* now quantify & encode AC coefs */ | ||
180 | 1544400 | last_non_zero = 0; | |
181 | 1544400 | last_index = s->c.block_last_index[n]; | |
182 | |||
183 |
2/2✓ Branch 0 taken 11960496 times.
✓ Branch 1 taken 1544400 times.
|
13504896 | for (i = 1; i <= last_index; i++) { |
184 | 11960496 | j = s->c.intra_scantable.permutated[i]; | |
185 | 11960496 | level = block[j]; | |
186 | |||
187 | /* encode using VLC */ | ||
188 |
2/2✓ Branch 0 taken 5766974 times.
✓ Branch 1 taken 6193522 times.
|
11960496 | if (level != 0) { |
189 | 5766974 | run = i - last_non_zero - 1; | |
190 | |||
191 | 5766974 | alevel = level; | |
192 | 5766974 | MASK_ABS(sign, alevel); | |
193 | 5766974 | sign &= 1; | |
194 | |||
195 |
2/2✓ Branch 0 taken 5741265 times.
✓ Branch 1 taken 25709 times.
|
5766974 | if (alevel <= speedhq_max_level[run]) { |
196 | 5741265 | code = speedhq_index_run[run] + alevel - 1; | |
197 | /* store the VLC & sign at once */ | ||
198 | 5741265 | put_bits_le(&s->pb, ff_speedhq_vlc_table[code][1] + 1, | |
199 | 5741265 | ff_speedhq_vlc_table[code][0] | (sign << ff_speedhq_vlc_table[code][1])); | |
200 | } else { | ||
201 | /* escape seems to be pretty rare <5% so I do not optimize it; | ||
202 | * The following encodes the escape value 100000b together with | ||
203 | * run and level. */ | ||
204 | 25709 | put_bits_le(&s->pb, 6 + 6 + 12, 0x20 | run << 6 | | |
205 | 25709 | (level + 2048) << 12); | |
206 | } | ||
207 | 5766974 | last_non_zero = i; | |
208 | } | ||
209 | } | ||
210 | /* end of block; the values correspond to ff_speedhq_vlc_table[122] */ | ||
211 | 1544400 | put_bits_le(&s->pb, 4, 6); | |
212 | 1544400 | } | |
213 | |||
214 | 178200 | static void speedhq_encode_mb(MPVEncContext *const s, int16_t block[12][64], | |
215 | int unused_x, int unused_y) | ||
216 | { | ||
217 | int i; | ||
218 |
2/2✓ Branch 0 taken 1069200 times.
✓ Branch 1 taken 178200 times.
|
1247400 | for(i=0;i<6;i++) { |
219 | 1069200 | encode_block(s, block[i], i); | |
220 | } | ||
221 |
2/2✓ Branch 0 taken 59400 times.
✓ Branch 1 taken 118800 times.
|
178200 | if (s->c.chroma_format == CHROMA_444) { |
222 | 59400 | encode_block(s, block[8], 8); | |
223 | 59400 | encode_block(s, block[9], 9); | |
224 | |||
225 | 59400 | encode_block(s, block[6], 6); | |
226 | 59400 | encode_block(s, block[7], 7); | |
227 | |||
228 | 59400 | encode_block(s, block[10], 10); | |
229 | 59400 | encode_block(s, block[11], 11); | |
230 |
2/2✓ Branch 0 taken 59400 times.
✓ Branch 1 taken 59400 times.
|
118800 | } else if (s->c.chroma_format == CHROMA_422) { |
231 | 59400 | encode_block(s, block[6], 6); | |
232 | 59400 | encode_block(s, block[7], 7); | |
233 | } | ||
234 | |||
235 | 178200 | s->i_tex_bits += get_bits_diff(s); | |
236 | 178200 | } | |
237 | |||
238 | 9 | static av_cold int speedhq_encode_init(AVCodecContext *avctx) | |
239 | { | ||
240 | static AVOnce init_static_once = AV_ONCE_INIT; | ||
241 | 9 | MPVMainEncContext *const m = avctx->priv_data; | |
242 | 9 | MPVEncContext *const s = &m->s; | |
243 | int ret; | ||
244 | |||
245 |
2/4✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 9 times.
|
9 | if (avctx->width > 65500 || avctx->height > 65500) { |
246 | ✗ | av_log(avctx, AV_LOG_ERROR, "SpeedHQ does not support resolutions above 65500x65500\n"); | |
247 | ✗ | return AVERROR(EINVAL); | |
248 | } | ||
249 | |||
250 | // border is not implemented correctly at the moment, see ticket #10078 | ||
251 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
|
9 | if (avctx->width % 16) { |
252 | ✗ | av_log(avctx, AV_LOG_ERROR, "width must be a multiple of 16\n"); | |
253 | ✗ | return AVERROR_PATCHWELCOME; | |
254 | } | ||
255 | |||
256 |
3/4✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
9 | switch (avctx->pix_fmt) { |
257 | 3 | case AV_PIX_FMT_YUV420P: | |
258 | 3 | avctx->codec_tag = MKTAG('S','H','Q','0'); | |
259 | 3 | break; | |
260 | 3 | case AV_PIX_FMT_YUV422P: | |
261 | 3 | avctx->codec_tag = MKTAG('S','H','Q','2'); | |
262 | 3 | break; | |
263 | 3 | case AV_PIX_FMT_YUV444P: | |
264 | 3 | avctx->codec_tag = MKTAG('S','H','Q','4'); | |
265 | 3 | break; | |
266 | ✗ | default: | |
267 | ✗ | av_unreachable("Already checked via CODEC_PIXFMTS"); | |
268 | } | ||
269 | |||
270 | 9 | m->encode_picture_header = speedhq_encode_picture_header; | |
271 | 9 | s->encode_mb = speedhq_encode_mb; | |
272 | |||
273 | 9 | s->min_qcoeff = -2048; | |
274 | 9 | s->max_qcoeff = 2047; | |
275 | |||
276 | 9 | s->intra_ac_vlc_length = | |
277 | 9 | s->intra_ac_vlc_last_length = | |
278 | 9 | s->intra_chroma_ac_vlc_length = | |
279 | 9 | s->intra_chroma_ac_vlc_last_length = uni_speedhq_ac_vlc_len; | |
280 | |||
281 | 9 | s->c.y_dc_scale_table = | |
282 | 9 | s->c.c_dc_scale_table = ff_mpeg12_dc_scale_table[3]; | |
283 | |||
284 | 9 | ret = ff_mpv_encode_init(avctx); | |
285 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
|
9 | if (ret < 0) |
286 | ✗ | return ret; | |
287 | |||
288 | 9 | ff_thread_once(&init_static_once, speedhq_init_static_data); | |
289 | |||
290 | 9 | return 0; | |
291 | } | ||
292 | |||
293 | const FFCodec ff_speedhq_encoder = { | ||
294 | .p.name = "speedhq", | ||
295 | CODEC_LONG_NAME("NewTek SpeedHQ"), | ||
296 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
297 | .p.id = AV_CODEC_ID_SPEEDHQ, | ||
298 | .p.priv_class = &ff_mpv_enc_class, | ||
299 | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE, | ||
300 | .priv_data_size = sizeof(SpeedHQEncContext), | ||
301 | .init = speedhq_encode_init, | ||
302 | FF_CODEC_ENCODE_CB(ff_mpv_encode_picture), | ||
303 | .close = ff_mpv_encode_end, | ||
304 | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, | ||
305 | .color_ranges = AVCOL_RANGE_MPEG, | ||
306 | CODEC_PIXFMTS(AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P), | ||
307 | }; | ||
308 |