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
8 * This file is part of FFmpeg.
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.
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.
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
30 #include "libavutil/pixdesc.h"
31 #include "libavutil/thread.h"
35 #include "mpegvideo.h"
36 #include "speedhqenc.h"
38 extern RLTable ff_rl_speedhq;
39 static uint8_t speedhq_static_rl_table_store[2][2*MAX_RUN + MAX_LEVEL + 3];
41 static uint16_t mpeg12_vlc_dc_lum_code_reversed[12];
42 static uint16_t mpeg12_vlc_dc_chroma_code_reversed[12];
44 /* simple include everything table for dc, first byte is bits
45 * number next 3 are code */
46 static uint32_t speedhq_lum_dc_uni[512];
47 static uint32_t speedhq_chr_dc_uni[512];
49 static uint8_t speedhq_index_run[2][64];
50 static int8_t speedhq_max_level[2][64];
52 static uint8_t uni_speedhq_ac_vlc_len[64 * 64 * 2];
54 static uint32_t reverse(uint32_t num, int bits)
56 return bitswap_32(num) >> (32 - bits);
59 static void reverse_code(const uint16_t *code, const uint8_t *bits,
60 uint16_t *reversed_code, int num_entries)
62 for (int i = 0; i < num_entries; i++)
63 reversed_code[i] = reverse(code[i], bits[i]);
66 static av_cold void speedhq_init_static_data(void)
68 /* Exactly the same as MPEG-2, except little-endian. */
69 reverse_code(ff_mpeg12_vlc_dc_lum_code,
70 ff_mpeg12_vlc_dc_lum_bits,
71 mpeg12_vlc_dc_lum_code_reversed,
73 reverse_code(ff_mpeg12_vlc_dc_chroma_code,
74 ff_mpeg12_vlc_dc_chroma_bits,
75 mpeg12_vlc_dc_chroma_code_reversed,
78 ff_rl_init(&ff_rl_speedhq, speedhq_static_rl_table_store);
80 for (int i = 0; i < 64; i++) {
81 speedhq_max_level[0][i] = ff_rl_speedhq.max_level[0][i];
82 speedhq_index_run[0][i] = ff_rl_speedhq.index_run[0][i];
85 /* build unified dc encoding tables */
86 for (int i = -255; i < 256; i++) {
94 index = av_log2(2 * adiff);
96 bits = ff_mpeg12_vlc_dc_lum_bits[index] + index;
97 code = mpeg12_vlc_dc_lum_code_reversed[index] +
98 (av_mod_uintp2(diff, index) << ff_mpeg12_vlc_dc_lum_bits[index]);
99 speedhq_lum_dc_uni[i + 255] = bits + (code << 8);
101 bits = ff_mpeg12_vlc_dc_chroma_bits[index] + index;
102 code = mpeg12_vlc_dc_chroma_code_reversed[index] +
103 (av_mod_uintp2(diff, index) << ff_mpeg12_vlc_dc_chroma_bits[index]);
104 speedhq_chr_dc_uni[i + 255] = bits + (code << 8);
107 ff_init_uni_ac_vlc(&ff_rl_speedhq, uni_speedhq_ac_vlc_len);
110 av_cold int ff_speedhq_encode_init(MpegEncContext *s)
112 static AVOnce init_static_once = AV_ONCE_INIT;
114 av_assert0(s->slice_context_count == 1);
116 if (s->width > 65500 || s->height > 65500) {
117 av_log(s, AV_LOG_ERROR, "SpeedHQ does not support resolutions above 65500x65500\n");
118 return AVERROR(EINVAL);
121 s->min_qcoeff = -2048;
122 s->max_qcoeff = 2047;
124 ff_thread_once(&init_static_once, speedhq_init_static_data);
126 s->intra_ac_vlc_length =
127 s->intra_ac_vlc_last_length =
128 s->intra_chroma_ac_vlc_length =
129 s->intra_chroma_ac_vlc_last_length = uni_speedhq_ac_vlc_len;
131 switch (s->avctx->pix_fmt) {
132 case AV_PIX_FMT_YUV420P:
133 s->avctx->codec_tag = MKTAG('S','H','Q','0');
135 case AV_PIX_FMT_YUV422P:
136 s->avctx->codec_tag = MKTAG('S','H','Q','2');
138 case AV_PIX_FMT_YUV444P:
139 s->avctx->codec_tag = MKTAG('S','H','Q','4');
148 void ff_speedhq_encode_picture_header(MpegEncContext *s)
150 put_bits_le(&s->pb, 8, 100 - s->qscale * 2); /* FIXME why doubled */
151 put_bits_le(&s->pb, 24, 4); /* no second field */
153 /* length of first slice, will be filled out later */
155 put_bits_le(&s->pb, 24, 0);
158 void ff_speedhq_end_slice(MpegEncContext *s)
162 flush_put_bits_le(&s->pb);
163 slice_len = s->pb.buf_ptr - (s->pb.buf + s->slice_start);
164 AV_WL24(s->pb.buf + s->slice_start, slice_len);
166 /* length of next slice, will be filled out later */
167 s->slice_start = s->pb.buf_ptr - s->pb.buf;
168 put_bits_le(&s->pb, 24, 0);
171 static inline void encode_dc(PutBitContext *pb, int diff, int component)
173 unsigned int diff_u = diff + 255;
178 index = av_log2_16bit(-2 * diff);
181 index = av_log2_16bit(2 * diff);
185 ff_mpeg12_vlc_dc_lum_bits[index] + index,
186 mpeg12_vlc_dc_lum_code_reversed[index] +
187 (av_mod_uintp2(diff, index) << ff_mpeg12_vlc_dc_lum_bits[index]));
190 ff_mpeg12_vlc_dc_chroma_bits[index] + index,
191 mpeg12_vlc_dc_chroma_code_reversed[index] +
192 (av_mod_uintp2(diff, index) << ff_mpeg12_vlc_dc_chroma_bits[index]));
196 speedhq_lum_dc_uni[diff + 255] & 0xFF,
197 speedhq_lum_dc_uni[diff + 255] >> 8);
200 speedhq_chr_dc_uni[diff + 255] & 0xFF,
201 speedhq_chr_dc_uni[diff + 255] >> 8);
205 static void encode_block(MpegEncContext *s, int16_t *block, int n)
207 int alevel, level, last_non_zero, dc, i, j, run, last_index, sign;
212 component = (n <= 3 ? 0 : (n&1) + 1);
213 dc = block[0]; /* overflow is impossible */
214 val = s->last_dc[component] - dc; /* opposite of most codecs */
215 encode_dc(&s->pb, val, component);
216 s->last_dc[component] = dc;
218 /* now quantify & encode AC coefs */
220 last_index = s->block_last_index[n];
222 for (i = 1; i <= last_index; i++) {
223 j = s->intra_scantable.permutated[i];
226 /* encode using VLC */
228 run = i - last_non_zero - 1;
231 MASK_ABS(sign, alevel);
234 if (alevel <= speedhq_max_level[0][run]) {
235 code = speedhq_index_run[0][run] + alevel - 1;
236 /* store the VLC & sign at once */
237 put_bits_le(&s->pb, ff_rl_speedhq.table_vlc[code][1] + 1,
238 ff_rl_speedhq.table_vlc[code][0] + (sign << ff_rl_speedhq.table_vlc[code][1]));
240 /* escape seems to be pretty rare <5% so I do not optimize it */
241 put_bits_le(&s->pb, ff_rl_speedhq.table_vlc[121][1], ff_rl_speedhq.table_vlc[121][0]);
242 /* escape: only clip in this case */
243 put_bits_le(&s->pb, 6, run);
244 put_bits_le(&s->pb, 12, level + 2048);
250 put_bits_le(&s->pb, ff_rl_speedhq.table_vlc[122][1], ff_rl_speedhq.table_vlc[122][0]);
253 void ff_speedhq_encode_mb(MpegEncContext *s, int16_t block[12][64])
257 encode_block(s, block[i], i);
259 if (s->chroma_format == CHROMA_444) {
260 encode_block(s, block[8], 8);
261 encode_block(s, block[9], 9);
263 encode_block(s, block[6], 6);
264 encode_block(s, block[7], 7);
266 encode_block(s, block[10], 10);
267 encode_block(s, block[11], 11);
268 } else if (s->chroma_format == CHROMA_422) {
269 encode_block(s, block[6], 6);
270 encode_block(s, block[7], 7);
273 s->i_tex_bits += get_bits_diff(s);
276 static int ff_speedhq_mb_rows_in_slice(int slice_num, int mb_height)
278 return mb_height / 4 + (slice_num < (mb_height % 4));
281 int ff_speedhq_mb_y_order_to_mb(int mb_y_order, int mb_height, int *first_in_slice)
284 while (mb_y_order >= ff_speedhq_mb_rows_in_slice(slice_num, mb_height)) {
285 mb_y_order -= ff_speedhq_mb_rows_in_slice(slice_num, mb_height);
288 *first_in_slice = (mb_y_order == 0);
289 return mb_y_order * 4 + slice_num;
292 #if CONFIG_SPEEDHQ_ENCODER
293 static const AVClass speedhq_class = {
294 .class_name = "speedhq encoder",
295 .item_name = av_default_item_name,
296 .option = ff_mpv_generic_options,
297 .version = LIBAVUTIL_VERSION_INT,
300 AVCodec ff_speedhq_encoder = {
302 .long_name = NULL_IF_CONFIG_SMALL("NewTek SpeedHQ"),
303 .type = AVMEDIA_TYPE_VIDEO,
304 .id = AV_CODEC_ID_SPEEDHQ,
305 .priv_data_size = sizeof(MpegEncContext),
306 .init = ff_mpv_encode_init,
307 .encode2 = ff_mpv_encode_picture,
308 .close = ff_mpv_encode_end,
309 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
310 .pix_fmts = (const enum AVPixelFormat[]) {
311 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
314 .priv_class = &speedhq_class,