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/thread.h"
33 #include "codec_internal.h"
34 #include "mpeg12data.h"
35 #include "mpeg12vlc.h"
36 #include "mpegvideo.h"
37 #include "mpegvideodata.h"
38 #include "mpegvideoenc.h"
41 #include "speedhqenc.h"
43 static uint8_t speedhq_max_level[MAX_LEVEL + 1];
44 static uint8_t speedhq_index_run[MAX_RUN + 1];
46 /* Exactly the same as MPEG-2, except little-endian. */
47 static const uint16_t mpeg12_vlc_dc_lum_code_reversed[12] = {
48 0x1, 0x0, 0x2, 0x5, 0x3, 0x7, 0xF, 0x1F, 0x3F, 0x7F, 0xFF, 0x1FF
50 static const uint16_t mpeg12_vlc_dc_chroma_code_reversed[12] = {
51 0x0, 0x2, 0x1, 0x3, 0x7, 0xF, 0x1F, 0x3F, 0x7F, 0xFF, 0x1FF, 0x3FF
54 /* simple include everything table for dc, first byte is bits
55 * number next 3 are code */
56 static uint32_t speedhq_lum_dc_uni[512];
57 static uint32_t speedhq_chr_dc_uni[512];
59 static uint8_t uni_speedhq_ac_vlc_len[64 * 64 * 2];
61 typedef struct SpeedHQEncContext {
67 static av_cold void speedhq_init_static_data(void)
69 ff_rl_init_level_run(speedhq_max_level, speedhq_index_run,
70 ff_speedhq_run, ff_speedhq_level, SPEEDHQ_RL_NB_ELEMS);
72 /* build unified dc encoding tables */
73 for (int i = -255; i < 256; i++) {
81 index = av_log2(2 * adiff);
83 bits = ff_mpeg12_vlc_dc_lum_bits[index] + index;
84 code = mpeg12_vlc_dc_lum_code_reversed[index] +
85 (av_mod_uintp2(diff, index) << ff_mpeg12_vlc_dc_lum_bits[index]);
86 speedhq_lum_dc_uni[i + 255] = bits + (code << 8);
88 bits = ff_mpeg12_vlc_dc_chroma_bits[index] + index;
89 code = mpeg12_vlc_dc_chroma_code_reversed[index] +
90 (av_mod_uintp2(diff, index) << ff_mpeg12_vlc_dc_chroma_bits[index]);
91 speedhq_chr_dc_uni[i + 255] = bits + (code << 8);
94 ff_mpeg1_init_uni_ac_vlc(speedhq_max_level, speedhq_index_run,
95 ff_speedhq_vlc_table, uni_speedhq_ac_vlc_len);
98 av_cold int ff_speedhq_encode_init(MpegEncContext *s)
100 static AVOnce init_static_once = AV_ONCE_INIT;
102 if (s->width > 65500 || s->height > 65500) {
103 av_log(s, AV_LOG_ERROR, "SpeedHQ does not support resolutions above 65500x65500\n");
104 return AVERROR(EINVAL);
107 s->min_qcoeff = -2048;
108 s->max_qcoeff = 2047;
110 ff_thread_once(&init_static_once, speedhq_init_static_data);
112 s->intra_ac_vlc_length =
113 s->intra_ac_vlc_last_length =
114 s->intra_chroma_ac_vlc_length =
115 s->intra_chroma_ac_vlc_last_length = uni_speedhq_ac_vlc_len;
117 s->y_dc_scale_table =
118 s->c_dc_scale_table = ff_mpeg12_dc_scale_table[3];
120 switch (s->avctx->pix_fmt) {
121 case AV_PIX_FMT_YUV420P:
122 s->avctx->codec_tag = MKTAG('S','H','Q','0');
124 case AV_PIX_FMT_YUV422P:
125 s->avctx->codec_tag = MKTAG('S','H','Q','2');
127 case AV_PIX_FMT_YUV444P:
128 s->avctx->codec_tag = MKTAG('S','H','Q','4');
137 void ff_speedhq_encode_picture_header(MpegEncContext *s)
139 SpeedHQEncContext *ctx = (SpeedHQEncContext*)s;
141 put_bits_le(&s->pb, 8, 100 - s->qscale * 2); /* FIXME why doubled */
142 put_bits_le(&s->pb, 24, 4); /* no second field */
144 ctx->slice_start = 4;
145 /* length of first slice, will be filled out later */
146 put_bits_le(&s->pb, 24, 0);
149 void ff_speedhq_end_slice(MpegEncContext *s)
151 SpeedHQEncContext *ctx = (SpeedHQEncContext*)s;
154 flush_put_bits_le(&s->pb);
155 slice_len = put_bytes_output(&s->pb) - ctx->slice_start;
156 AV_WL24(s->pb.buf + ctx->slice_start, slice_len);
158 /* length of next slice, will be filled out later */
159 ctx->slice_start = put_bytes_output(&s->pb);
160 put_bits_le(&s->pb, 24, 0);
163 static inline void encode_dc(PutBitContext *pb, int diff, int component)
165 unsigned int diff_u = diff + 255;
170 index = av_log2_16bit(-2 * diff);
173 index = av_log2_16bit(2 * diff);
177 ff_mpeg12_vlc_dc_lum_bits[index] + index,
178 mpeg12_vlc_dc_lum_code_reversed[index] +
179 (av_mod_uintp2(diff, index) << ff_mpeg12_vlc_dc_lum_bits[index]));
182 ff_mpeg12_vlc_dc_chroma_bits[index] + index,
183 mpeg12_vlc_dc_chroma_code_reversed[index] +
184 (av_mod_uintp2(diff, index) << ff_mpeg12_vlc_dc_chroma_bits[index]));
188 speedhq_lum_dc_uni[diff + 255] & 0xFF,
189 speedhq_lum_dc_uni[diff + 255] >> 8);
192 speedhq_chr_dc_uni[diff + 255] & 0xFF,
193 speedhq_chr_dc_uni[diff + 255] >> 8);
197 static void encode_block(MpegEncContext *s, int16_t *block, int n)
199 int alevel, level, last_non_zero, dc, i, j, run, last_index, sign;
204 component = (n <= 3 ? 0 : (n&1) + 1);
205 dc = block[0]; /* overflow is impossible */
206 val = s->last_dc[component] - dc; /* opposite of most codecs */
207 encode_dc(&s->pb, val, component);
208 s->last_dc[component] = dc;
210 /* now quantify & encode AC coefs */
212 last_index = s->block_last_index[n];
214 for (i = 1; i <= last_index; i++) {
215 j = s->intra_scantable.permutated[i];
218 /* encode using VLC */
220 run = i - last_non_zero - 1;
223 MASK_ABS(sign, alevel);
226 if (alevel <= speedhq_max_level[run]) {
227 code = speedhq_index_run[run] + alevel - 1;
228 /* store the VLC & sign at once */
229 put_bits_le(&s->pb, ff_speedhq_vlc_table[code][1] + 1,
230 ff_speedhq_vlc_table[code][0] | (sign << ff_speedhq_vlc_table[code][1]));
232 /* escape seems to be pretty rare <5% so I do not optimize it;
233 * the values correspond to ff_speedhq_vlc_table[121] */
234 put_bits_le(&s->pb, 6, 32);
235 /* escape: only clip in this case */
236 put_bits_le(&s->pb, 6, run);
237 put_bits_le(&s->pb, 12, level + 2048);
242 /* end of block; the values correspond to ff_speedhq_vlc_table[122] */
243 put_bits_le(&s->pb, 4, 6);
246 void ff_speedhq_encode_mb(MpegEncContext *s, int16_t block[12][64])
250 encode_block(s, block[i], i);
252 if (s->chroma_format == CHROMA_444) {
253 encode_block(s, block[8], 8);
254 encode_block(s, block[9], 9);
256 encode_block(s, block[6], 6);
257 encode_block(s, block[7], 7);
259 encode_block(s, block[10], 10);
260 encode_block(s, block[11], 11);
261 } else if (s->chroma_format == CHROMA_422) {
262 encode_block(s, block[6], 6);
263 encode_block(s, block[7], 7);
266 s->i_tex_bits += get_bits_diff(s);
269 static int ff_speedhq_mb_rows_in_slice(int slice_num, int mb_height)
271 return mb_height / 4 + (slice_num < (mb_height % 4));
274 int ff_speedhq_mb_y_order_to_mb(int mb_y_order, int mb_height, int *first_in_slice)
277 while (mb_y_order >= ff_speedhq_mb_rows_in_slice(slice_num, mb_height)) {
278 mb_y_order -= ff_speedhq_mb_rows_in_slice(slice_num, mb_height);
281 *first_in_slice = (mb_y_order == 0);
282 return mb_y_order * 4 + slice_num;
285 const FFCodec ff_speedhq_encoder = {
287 CODEC_LONG_NAME("NewTek SpeedHQ"),
288 .p.type = AVMEDIA_TYPE_VIDEO,
289 .p.id = AV_CODEC_ID_SPEEDHQ,
290 .p.priv_class = &ff_mpv_enc_class,
291 .p.capabilities = AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
292 .priv_data_size = sizeof(SpeedHQEncContext),
293 .init = ff_mpv_encode_init,
294 FF_CODEC_ENCODE_CB(ff_mpv_encode_picture),
295 .close = ff_mpv_encode_end,
296 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
297 .p.pix_fmts = (const enum AVPixelFormat[]) {
298 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,