FFmpeg coverage


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