FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/speedhqenc.c
Date: 2022-01-18 10:05:52
Exec Total Coverage
Lines: 0 124 0.0%
Branches: 0 38 0.0%

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/pixdesc.h"
31 #include "libavutil/thread.h"
32
33 #include "avcodec.h"
34 #include "mpeg12.h"
35 #include "mpeg12data.h"
36 #include "mpegvideo.h"
37 #include "speedhqenc.h"
38
39 extern RLTable ff_rl_speedhq;
40 static uint8_t speedhq_static_rl_table_store[2][2*MAX_RUN + MAX_LEVEL + 3];
41
42 /* Exactly the same as MPEG-2, except little-endian. */
43 static const uint16_t mpeg12_vlc_dc_lum_code_reversed[12] = {
44 0x1, 0x0, 0x2, 0x5, 0x3, 0x7, 0xF, 0x1F, 0x3F, 0x7F, 0xFF, 0x1FF
45 };
46 static const uint16_t mpeg12_vlc_dc_chroma_code_reversed[12] = {
47 0x0, 0x2, 0x1, 0x3, 0x7, 0xF, 0x1F, 0x3F, 0x7F, 0xFF, 0x1FF, 0x3FF
48 };
49
50 /* simple include everything table for dc, first byte is bits
51 * number next 3 are code */
52 static uint32_t speedhq_lum_dc_uni[512];
53 static uint32_t speedhq_chr_dc_uni[512];
54
55 static uint8_t uni_speedhq_ac_vlc_len[64 * 64 * 2];
56
57 static av_cold void speedhq_init_static_data(void)
58 {
59 ff_rl_init(&ff_rl_speedhq, speedhq_static_rl_table_store);
60
61 /* build unified dc encoding tables */
62 for (int i = -255; i < 256; i++) {
63 int adiff, index;
64 int bits, code;
65 int diff = i;
66
67 adiff = FFABS(diff);
68 if (diff < 0)
69 diff--;
70 index = av_log2(2 * adiff);
71
72 bits = ff_mpeg12_vlc_dc_lum_bits[index] + index;
73 code = mpeg12_vlc_dc_lum_code_reversed[index] +
74 (av_mod_uintp2(diff, index) << ff_mpeg12_vlc_dc_lum_bits[index]);
75 speedhq_lum_dc_uni[i + 255] = bits + (code << 8);
76
77 bits = ff_mpeg12_vlc_dc_chroma_bits[index] + index;
78 code = mpeg12_vlc_dc_chroma_code_reversed[index] +
79 (av_mod_uintp2(diff, index) << ff_mpeg12_vlc_dc_chroma_bits[index]);
80 speedhq_chr_dc_uni[i + 255] = bits + (code << 8);
81 }
82
83 ff_mpeg1_init_uni_ac_vlc(&ff_rl_speedhq, uni_speedhq_ac_vlc_len);
84 }
85
86 av_cold int ff_speedhq_encode_init(MpegEncContext *s)
87 {
88 static AVOnce init_static_once = AV_ONCE_INIT;
89
90 av_assert0(s->slice_context_count == 1);
91
92 if (s->width > 65500 || s->height > 65500) {
93 av_log(s, AV_LOG_ERROR, "SpeedHQ does not support resolutions above 65500x65500\n");
94 return AVERROR(EINVAL);
95 }
96
97 s->min_qcoeff = -2048;
98 s->max_qcoeff = 2047;
99
100 ff_thread_once(&init_static_once, speedhq_init_static_data);
101
102 s->intra_ac_vlc_length =
103 s->intra_ac_vlc_last_length =
104 s->intra_chroma_ac_vlc_length =
105 s->intra_chroma_ac_vlc_last_length = uni_speedhq_ac_vlc_len;
106
107 switch (s->avctx->pix_fmt) {
108 case AV_PIX_FMT_YUV420P:
109 s->avctx->codec_tag = MKTAG('S','H','Q','0');
110 break;
111 case AV_PIX_FMT_YUV422P:
112 s->avctx->codec_tag = MKTAG('S','H','Q','2');
113 break;
114 case AV_PIX_FMT_YUV444P:
115 s->avctx->codec_tag = MKTAG('S','H','Q','4');
116 break;
117 default:
118 av_assert0(0);
119 }
120
121 return 0;
122 }
123
124 void ff_speedhq_encode_picture_header(MpegEncContext *s)
125 {
126 put_bits_le(&s->pb, 8, 100 - s->qscale * 2); /* FIXME why doubled */
127 put_bits_le(&s->pb, 24, 4); /* no second field */
128
129 /* length of first slice, will be filled out later */
130 s->slice_start = 4;
131 put_bits_le(&s->pb, 24, 0);
132 }
133
134 void ff_speedhq_end_slice(MpegEncContext *s)
135 {
136 int slice_len;
137
138 flush_put_bits_le(&s->pb);
139 slice_len = s->pb.buf_ptr - (s->pb.buf + s->slice_start);
140 AV_WL24(s->pb.buf + s->slice_start, slice_len);
141
142 /* length of next slice, will be filled out later */
143 s->slice_start = s->pb.buf_ptr - s->pb.buf;
144 put_bits_le(&s->pb, 24, 0);
145 }
146
147 static inline void encode_dc(PutBitContext *pb, int diff, int component)
148 {
149 unsigned int diff_u = diff + 255;
150 if (diff_u >= 511) {
151 int index;
152
153 if (diff < 0) {
154 index = av_log2_16bit(-2 * diff);
155 diff--;
156 } else {
157 index = av_log2_16bit(2 * diff);
158 }
159 if (component == 0)
160 put_bits_le(pb,
161 ff_mpeg12_vlc_dc_lum_bits[index] + index,
162 mpeg12_vlc_dc_lum_code_reversed[index] +
163 (av_mod_uintp2(diff, index) << ff_mpeg12_vlc_dc_lum_bits[index]));
164 else
165 put_bits_le(pb,
166 ff_mpeg12_vlc_dc_chroma_bits[index] + index,
167 mpeg12_vlc_dc_chroma_code_reversed[index] +
168 (av_mod_uintp2(diff, index) << ff_mpeg12_vlc_dc_chroma_bits[index]));
169 } else {
170 if (component == 0)
171 put_bits_le(pb,
172 speedhq_lum_dc_uni[diff + 255] & 0xFF,
173 speedhq_lum_dc_uni[diff + 255] >> 8);
174 else
175 put_bits_le(pb,
176 speedhq_chr_dc_uni[diff + 255] & 0xFF,
177 speedhq_chr_dc_uni[diff + 255] >> 8);
178 }
179 }
180
181 static void encode_block(MpegEncContext *s, int16_t *block, int n)
182 {
183 int alevel, level, last_non_zero, dc, i, j, run, last_index, sign;
184 int code;
185 int component, val;
186
187 /* DC coef */
188 component = (n <= 3 ? 0 : (n&1) + 1);
189 dc = block[0]; /* overflow is impossible */
190 val = s->last_dc[component] - dc; /* opposite of most codecs */
191 encode_dc(&s->pb, val, component);
192 s->last_dc[component] = dc;
193
194 /* now quantify & encode AC coefs */
195 last_non_zero = 0;
196 last_index = s->block_last_index[n];
197
198 for (i = 1; i <= last_index; i++) {
199 j = s->intra_scantable.permutated[i];
200 level = block[j];
201
202 /* encode using VLC */
203 if (level != 0) {
204 run = i - last_non_zero - 1;
205
206 alevel = level;
207 MASK_ABS(sign, alevel);
208 sign &= 1;
209
210 if (alevel <= ff_rl_speedhq.max_level[0][run]) {
211 code = ff_rl_speedhq.index_run[0][run] + alevel - 1;
212 /* store the VLC & sign at once */
213 put_bits_le(&s->pb, ff_rl_speedhq.table_vlc[code][1] + 1,
214 ff_rl_speedhq.table_vlc[code][0] + (sign << ff_rl_speedhq.table_vlc[code][1]));
215 } else {
216 /* escape seems to be pretty rare <5% so I do not optimize it;
217 * the values correspond to ff_rl_speedhq.table_vlc[121] */
218 put_bits_le(&s->pb, 6, 32);
219 /* escape: only clip in this case */
220 put_bits_le(&s->pb, 6, run);
221 put_bits_le(&s->pb, 12, level + 2048);
222 }
223 last_non_zero = i;
224 }
225 }
226 /* end of block; the values correspond to ff_rl_speedhq.table_vlc[122] */
227 put_bits_le(&s->pb, 4, 6);
228 }
229
230 void ff_speedhq_encode_mb(MpegEncContext *s, int16_t block[12][64])
231 {
232 int i;
233 for(i=0;i<6;i++) {
234 encode_block(s, block[i], i);
235 }
236 if (s->chroma_format == CHROMA_444) {
237 encode_block(s, block[8], 8);
238 encode_block(s, block[9], 9);
239
240 encode_block(s, block[6], 6);
241 encode_block(s, block[7], 7);
242
243 encode_block(s, block[10], 10);
244 encode_block(s, block[11], 11);
245 } else if (s->chroma_format == CHROMA_422) {
246 encode_block(s, block[6], 6);
247 encode_block(s, block[7], 7);
248 }
249
250 s->i_tex_bits += get_bits_diff(s);
251 }
252
253 static int ff_speedhq_mb_rows_in_slice(int slice_num, int mb_height)
254 {
255 return mb_height / 4 + (slice_num < (mb_height % 4));
256 }
257
258 int ff_speedhq_mb_y_order_to_mb(int mb_y_order, int mb_height, int *first_in_slice)
259 {
260 int slice_num = 0;
261 while (mb_y_order >= ff_speedhq_mb_rows_in_slice(slice_num, mb_height)) {
262 mb_y_order -= ff_speedhq_mb_rows_in_slice(slice_num, mb_height);
263 slice_num++;
264 }
265 *first_in_slice = (mb_y_order == 0);
266 return mb_y_order * 4 + slice_num;
267 }
268
269 #if CONFIG_SPEEDHQ_ENCODER
270 const AVCodec ff_speedhq_encoder = {
271 .name = "speedhq",
272 .long_name = NULL_IF_CONFIG_SMALL("NewTek SpeedHQ"),
273 .type = AVMEDIA_TYPE_VIDEO,
274 .id = AV_CODEC_ID_SPEEDHQ,
275 .priv_class = &ff_mpv_enc_class,
276 .priv_data_size = sizeof(MpegEncContext),
277 .init = ff_mpv_encode_init,
278 .encode2 = ff_mpv_encode_picture,
279 .close = ff_mpv_encode_end,
280 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
281 .pix_fmts = (const enum AVPixelFormat[]) {
282 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
283 AV_PIX_FMT_NONE
284 },
285 };
286 #endif
287