FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/speedhqenc.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 130 136 95.6%
Functions: 9 9 100.0%
Branches: 34 38 89.5%

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/thread.h"
31
32 #include "avcodec.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"
39 #include "rl.h"
40 #include "speedhq.h"
41 #include "speedhqenc.h"
42
43 static uint8_t speedhq_max_level[MAX_LEVEL + 1];
44 static uint8_t speedhq_index_run[MAX_RUN + 1];
45
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
49 };
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
52 };
53
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];
58
59 static uint8_t uni_speedhq_ac_vlc_len[64 * 64 * 2];
60
61 typedef struct SpeedHQEncContext {
62 MpegEncContext m;
63
64 int slice_start;
65 } SpeedHQEncContext;
66
67 9 static av_cold void speedhq_init_static_data(void)
68 {
69 9 ff_rl_init_level_run(speedhq_max_level, speedhq_index_run,
70 ff_speedhq_run, ff_speedhq_level, SPEEDHQ_RL_NB_ELEMS);
71
72 /* build unified dc encoding tables */
73
2/2
✓ Branch 0 taken 4599 times.
✓ Branch 1 taken 9 times.
4608 for (int i = -255; i < 256; i++) {
74 int adiff, index;
75 int bits, code;
76 4599 int diff = i;
77
78 4599 adiff = FFABS(diff);
79
2/2
✓ Branch 0 taken 2295 times.
✓ Branch 1 taken 2304 times.
4599 if (diff < 0)
80 2295 diff--;
81 4599 index = av_log2(2 * adiff);
82
83 4599 bits = ff_mpeg12_vlc_dc_lum_bits[index] + index;
84 4599 code = mpeg12_vlc_dc_lum_code_reversed[index] +
85 4599 (av_zero_extend(diff, index) << ff_mpeg12_vlc_dc_lum_bits[index]);
86 4599 speedhq_lum_dc_uni[i + 255] = bits + (code << 8);
87
88 4599 bits = ff_mpeg12_vlc_dc_chroma_bits[index] + index;
89 4599 code = mpeg12_vlc_dc_chroma_code_reversed[index] +
90 4599 (av_zero_extend(diff, index) << ff_mpeg12_vlc_dc_chroma_bits[index]);
91 4599 speedhq_chr_dc_uni[i + 255] = bits + (code << 8);
92 }
93
94 9 ff_mpeg1_init_uni_ac_vlc(speedhq_max_level, speedhq_index_run,
95 ff_speedhq_vlc_table, uni_speedhq_ac_vlc_len);
96 9 }
97
98 9 av_cold int ff_speedhq_encode_init(MpegEncContext *s)
99 {
100 static AVOnce init_static_once = AV_ONCE_INIT;
101
102
2/4
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 9 times.
9 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);
105 }
106
107 // border is not implemented correctly at the moment, see ticket #10078
108
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (s->width % 16) {
109 av_log(s, AV_LOG_ERROR, "width must be a multiple of 16\n");
110 return AVERROR_PATCHWELCOME;
111 }
112
113 9 s->min_qcoeff = -2048;
114 9 s->max_qcoeff = 2047;
115
116 9 ff_thread_once(&init_static_once, speedhq_init_static_data);
117
118 9 s->intra_ac_vlc_length =
119 9 s->intra_ac_vlc_last_length =
120 9 s->intra_chroma_ac_vlc_length =
121 9 s->intra_chroma_ac_vlc_last_length = uni_speedhq_ac_vlc_len;
122
123 9 s->y_dc_scale_table =
124 9 s->c_dc_scale_table = ff_mpeg12_dc_scale_table[3];
125
126
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
9 switch (s->avctx->pix_fmt) {
127 3 case AV_PIX_FMT_YUV420P:
128 3 s->avctx->codec_tag = MKTAG('S','H','Q','0');
129 3 break;
130 3 case AV_PIX_FMT_YUV422P:
131 3 s->avctx->codec_tag = MKTAG('S','H','Q','2');
132 3 break;
133 3 case AV_PIX_FMT_YUV444P:
134 3 s->avctx->codec_tag = MKTAG('S','H','Q','4');
135 3 break;
136 default:
137 av_assert0(0);
138 }
139
140 9 return 0;
141 }
142
143 450 void ff_speedhq_encode_picture_header(MpegEncContext *s)
144 {
145 450 SpeedHQEncContext *ctx = (SpeedHQEncContext*)s;
146
147 450 put_bits_le(&s->pb, 8, 100 - s->qscale * 2); /* FIXME why doubled */
148 450 put_bits_le(&s->pb, 24, 4); /* no second field */
149
150 450 ctx->slice_start = 4;
151 /* length of first slice, will be filled out later */
152 450 put_bits_le(&s->pb, 24, 0);
153 450 }
154
155 1800 void ff_speedhq_end_slice(MpegEncContext *s)
156 {
157 1800 SpeedHQEncContext *ctx = (SpeedHQEncContext*)s;
158 int slice_len;
159
160 1800 flush_put_bits_le(&s->pb);
161 1800 slice_len = put_bytes_output(&s->pb) - ctx->slice_start;
162 1800 AV_WL24(s->pb.buf + ctx->slice_start, slice_len);
163
164 /* length of next slice, will be filled out later */
165 1800 ctx->slice_start = put_bytes_output(&s->pb);
166 1800 put_bits_le(&s->pb, 24, 0);
167 1800 }
168
169 1544400 static inline void encode_dc(PutBitContext *pb, int diff, int component)
170 {
171 1544400 unsigned int diff_u = diff + 255;
172
2/2
✓ Branch 0 taken 329487 times.
✓ Branch 1 taken 1214913 times.
1544400 if (diff_u >= 511) {
173 int index;
174
175
2/2
✓ Branch 0 taken 169357 times.
✓ Branch 1 taken 160130 times.
329487 if (diff < 0) {
176 169357 index = av_log2_16bit(-2 * diff);
177 169357 diff--;
178 } else {
179 160130 index = av_log2_16bit(2 * diff);
180 }
181
2/2
✓ Branch 0 taken 239838 times.
✓ Branch 1 taken 89649 times.
329487 if (component == 0)
182 239838 put_bits_le(pb,
183 239838 ff_mpeg12_vlc_dc_lum_bits[index] + index,
184 239838 mpeg12_vlc_dc_lum_code_reversed[index] +
185 239838 (av_zero_extend(diff, index) << ff_mpeg12_vlc_dc_lum_bits[index]));
186 else
187 89649 put_bits_le(pb,
188 89649 ff_mpeg12_vlc_dc_chroma_bits[index] + index,
189 89649 mpeg12_vlc_dc_chroma_code_reversed[index] +
190 89649 (av_zero_extend(diff, index) << ff_mpeg12_vlc_dc_chroma_bits[index]));
191 } else {
192
2/2
✓ Branch 0 taken 472962 times.
✓ Branch 1 taken 741951 times.
1214913 if (component == 0)
193 472962 put_bits_le(pb,
194 472962 speedhq_lum_dc_uni[diff + 255] & 0xFF,
195 472962 speedhq_lum_dc_uni[diff + 255] >> 8);
196 else
197 741951 put_bits_le(pb,
198 741951 speedhq_chr_dc_uni[diff + 255] & 0xFF,
199 741951 speedhq_chr_dc_uni[diff + 255] >> 8);
200 }
201 1544400 }
202
203 1544400 static void encode_block(MpegEncContext *s, int16_t *block, int n)
204 {
205 int alevel, level, last_non_zero, dc, i, j, run, last_index, sign;
206 int code;
207 int component, val;
208
209 /* DC coef */
210
2/2
✓ Branch 0 taken 831600 times.
✓ Branch 1 taken 712800 times.
1544400 component = (n <= 3 ? 0 : (n&1) + 1);
211 1544400 dc = block[0]; /* overflow is impossible */
212 1544400 val = s->last_dc[component] - dc; /* opposite of most codecs */
213 1544400 encode_dc(&s->pb, val, component);
214 1544400 s->last_dc[component] = dc;
215
216 /* now quantify & encode AC coefs */
217 1544400 last_non_zero = 0;
218 1544400 last_index = s->block_last_index[n];
219
220
2/2
✓ Branch 0 taken 11960496 times.
✓ Branch 1 taken 1544400 times.
13504896 for (i = 1; i <= last_index; i++) {
221 11960496 j = s->intra_scantable.permutated[i];
222 11960496 level = block[j];
223
224 /* encode using VLC */
225
2/2
✓ Branch 0 taken 5766974 times.
✓ Branch 1 taken 6193522 times.
11960496 if (level != 0) {
226 5766974 run = i - last_non_zero - 1;
227
228 5766974 alevel = level;
229 5766974 MASK_ABS(sign, alevel);
230 5766974 sign &= 1;
231
232
2/2
✓ Branch 0 taken 5741265 times.
✓ Branch 1 taken 25709 times.
5766974 if (alevel <= speedhq_max_level[run]) {
233 5741265 code = speedhq_index_run[run] + alevel - 1;
234 /* store the VLC & sign at once */
235 5741265 put_bits_le(&s->pb, ff_speedhq_vlc_table[code][1] + 1,
236 5741265 ff_speedhq_vlc_table[code][0] | (sign << ff_speedhq_vlc_table[code][1]));
237 } else {
238 /* escape seems to be pretty rare <5% so I do not optimize it;
239 * the values correspond to ff_speedhq_vlc_table[121] */
240 25709 put_bits_le(&s->pb, 6, 32);
241 /* escape: only clip in this case */
242 25709 put_bits_le(&s->pb, 6, run);
243 25709 put_bits_le(&s->pb, 12, level + 2048);
244 }
245 5766974 last_non_zero = i;
246 }
247 }
248 /* end of block; the values correspond to ff_speedhq_vlc_table[122] */
249 1544400 put_bits_le(&s->pb, 4, 6);
250 1544400 }
251
252 178200 void ff_speedhq_encode_mb(MpegEncContext *s, int16_t block[12][64])
253 {
254 int i;
255
2/2
✓ Branch 0 taken 1069200 times.
✓ Branch 1 taken 178200 times.
1247400 for(i=0;i<6;i++) {
256 1069200 encode_block(s, block[i], i);
257 }
258
2/2
✓ Branch 0 taken 59400 times.
✓ Branch 1 taken 118800 times.
178200 if (s->chroma_format == CHROMA_444) {
259 59400 encode_block(s, block[8], 8);
260 59400 encode_block(s, block[9], 9);
261
262 59400 encode_block(s, block[6], 6);
263 59400 encode_block(s, block[7], 7);
264
265 59400 encode_block(s, block[10], 10);
266 59400 encode_block(s, block[11], 11);
267
2/2
✓ Branch 0 taken 59400 times.
✓ Branch 1 taken 59400 times.
118800 } else if (s->chroma_format == CHROMA_422) {
268 59400 encode_block(s, block[6], 6);
269 59400 encode_block(s, block[7], 7);
270 }
271
272 178200 s->i_tex_bits += get_bits_diff(s);
273 178200 }
274
275 30600 static int ff_speedhq_mb_rows_in_slice(int slice_num, int mb_height)
276 {
277 30600 return mb_height / 4 + (slice_num < (mb_height % 4));
278 }
279
280 8100 int ff_speedhq_mb_y_order_to_mb(int mb_y_order, int mb_height, int *first_in_slice)
281 {
282 8100 int slice_num = 0;
283
2/2
✓ Branch 1 taken 11250 times.
✓ Branch 2 taken 8100 times.
19350 while (mb_y_order >= ff_speedhq_mb_rows_in_slice(slice_num, mb_height)) {
284 11250 mb_y_order -= ff_speedhq_mb_rows_in_slice(slice_num, mb_height);
285 11250 slice_num++;
286 }
287 8100 *first_in_slice = (mb_y_order == 0);
288 8100 return mb_y_order * 4 + slice_num;
289 }
290
291 const FFCodec ff_speedhq_encoder = {
292 .p.name = "speedhq",
293 CODEC_LONG_NAME("NewTek SpeedHQ"),
294 .p.type = AVMEDIA_TYPE_VIDEO,
295 .p.id = AV_CODEC_ID_SPEEDHQ,
296 .p.priv_class = &ff_mpv_enc_class,
297 .p.capabilities = AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
298 .priv_data_size = sizeof(SpeedHQEncContext),
299 .init = ff_mpv_encode_init,
300 FF_CODEC_ENCODE_CB(ff_mpv_encode_picture),
301 .close = ff_mpv_encode_end,
302 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
303 .color_ranges = AVCOL_RANGE_MPEG,
304 .p.pix_fmts = (const enum AVPixelFormat[]) {
305 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
306 AV_PIX_FMT_NONE
307 },
308 };
309