FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/dxvenc.c
Date: 2024-02-29 09:57:37
Exec Total Coverage
Lines: 126 144 87.5%
Functions: 7 7 100.0%
Branches: 75 98 76.5%

Line Branch Exec Source
1 /*
2 * Resolume DXV encoder
3 * Copyright (C) 2024 Connor Worley <connorbworley@gmail.com>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <stdint.h>
23
24 #include "libavutil/crc.h"
25 #include "libavutil/imgutils.h"
26 #include "libavutil/opt.h"
27
28 #include "bytestream.h"
29 #include "codec_internal.h"
30 #include "dxv.h"
31 #include "encode.h"
32 #include "texturedsp.h"
33
34 #define DXV_HEADER_LENGTH 12
35
36 /*
37 * DXV uses LZ-like back-references to avoid copying words that have already
38 * appeared in the decompressed stream. Using a simple hash table (HT)
39 * significantly speeds up the lookback process while encoding.
40 */
41 #define LOOKBACK_HT_ELEMS 0x40000
42 #define LOOKBACK_WORDS 0x20202
43
44 typedef struct HTEntry {
45 uint32_t key;
46 uint32_t pos;
47 } HTEntry;
48
49 2 static void ht_init(HTEntry *ht)
50 {
51
2/2
✓ Branch 0 taken 524288 times.
✓ Branch 1 taken 2 times.
524290 for (size_t i = 0; i < LOOKBACK_HT_ELEMS; i++) {
52 524288 ht[i].pos = -1;
53 }
54 2 }
55
56 135986 static uint32_t ht_lookup_and_upsert(HTEntry *ht, const AVCRC *hash_ctx,
57 uint32_t key, uint32_t pos)
58 {
59 135986 uint32_t ret = -1;
60 135986 size_t hash = av_crc(hash_ctx, 0, (uint8_t*)&key, 4) % LOOKBACK_HT_ELEMS;
61
1/2
✓ Branch 0 taken 136009 times.
✗ Branch 1 not taken.
136009 for (size_t i = hash; i < hash + LOOKBACK_HT_ELEMS; i++) {
62 136009 size_t wrapped_index = i % LOOKBACK_HT_ELEMS;
63 136009 HTEntry *entry = &ht[wrapped_index];
64
4/4
✓ Branch 0 taken 5904 times.
✓ Branch 1 taken 130105 times.
✓ Branch 2 taken 5881 times.
✓ Branch 3 taken 23 times.
136009 if (entry->key == key || entry->pos == -1) {
65 135986 ret = entry->pos;
66 135986 entry->key = key;
67 135986 entry->pos = pos;
68 135986 break;
69 }
70 }
71 135986 return ret;
72 }
73
74 127614 static void ht_delete(HTEntry *ht, const AVCRC *hash_ctx,
75 uint32_t key, uint32_t pos)
76 {
77 127614 HTEntry *removed_entry = NULL;
78 size_t removed_hash;
79 127614 size_t hash = av_crc(hash_ctx, 0, (uint8_t*)&key, 4) % LOOKBACK_HT_ELEMS;
80
81
1/2
✓ Branch 0 taken 128361 times.
✗ Branch 1 not taken.
128361 for (size_t i = hash; i < hash + LOOKBACK_HT_ELEMS; i++) {
82 128361 size_t wrapped_index = i % LOOKBACK_HT_ELEMS;
83 128361 HTEntry *entry = &ht[wrapped_index];
84
2/2
✓ Branch 0 taken 796 times.
✓ Branch 1 taken 127565 times.
128361 if (entry->pos == -1)
85 796 return;
86
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 127560 times.
127565 if (removed_entry) {
87 5 size_t candidate_hash = av_crc(hash_ctx, 0, (uint8_t*)&entry->key, 4) % LOOKBACK_HT_ELEMS;
88
4/8
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 5 times.
5 if ((wrapped_index > removed_hash && (candidate_hash <= removed_hash || candidate_hash > wrapped_index)) ||
89 (wrapped_index < removed_hash && (candidate_hash <= removed_hash && candidate_hash > wrapped_index))) {
90 *removed_entry = *entry;
91 entry->pos = -1;
92 removed_entry = entry;
93 removed_hash = wrapped_index;
94 }
95
1/2
✓ Branch 0 taken 127560 times.
✗ Branch 1 not taken.
127560 } else if (entry->key == key) {
96
2/2
✓ Branch 0 taken 742 times.
✓ Branch 1 taken 126818 times.
127560 if (entry->pos <= pos) {
97 742 entry->pos = -1;
98 742 removed_entry = entry;
99 742 removed_hash = wrapped_index;
100 } else {
101 126818 return;
102 }
103 }
104 }
105 }
106
107 typedef struct DXVEncContext {
108 AVClass *class;
109
110 PutByteContext pbc;
111
112 uint8_t *tex_data; // Compressed texture
113 int64_t tex_size; // Texture size
114
115 /* Optimal number of slices for parallel decoding */
116 int slice_count;
117
118 TextureDSPThreadContext enc;
119
120 DXVTextureFormat tex_fmt;
121 int (*compress_tex)(AVCodecContext *avctx);
122
123 const AVCRC *crc_ctx;
124
125 HTEntry color_lookback_ht[LOOKBACK_HT_ELEMS];
126 HTEntry lut_lookback_ht[LOOKBACK_HT_ELEMS];
127 } DXVEncContext;
128
129 /* Converts an index offset value to a 2-bit opcode and pushes it to a stream.
130 * Inverse of CHECKPOINT in dxv.c. */
131 #define PUSH_OP(x) \
132 do { \
133 if (state == 16) { \
134 if (bytestream2_get_bytes_left_p(pbc) < 4) { \
135 return AVERROR_INVALIDDATA; \
136 } \
137 value = pbc->buffer; \
138 bytestream2_put_le32(pbc, 0); \
139 state = 0; \
140 } \
141 if (idx >= 0x102 * x) { \
142 op = 3; \
143 bytestream2_put_le16(pbc, (idx / x) - 0x102); \
144 } else if (idx >= 2 * x) { \
145 op = 2; \
146 bytestream2_put_byte(pbc, (idx / x) - 2); \
147 } else if (idx == x) { \
148 op = 1; \
149 } else { \
150 op = 0; \
151 } \
152 AV_WL32(value, AV_RL32(value) | (op << (state * 2))); \
153 state++; \
154 } while (0)
155
156 1 static int dxv_compress_dxt1(AVCodecContext *avctx)
157 {
158 1 DXVEncContext *ctx = avctx->priv_data;
159 1 PutByteContext *pbc = &ctx->pbc;
160 void *value;
161 1 uint32_t color, lut, idx, color_idx, lut_idx, prev_pos, state = 16, pos = 2, op = 0;
162
163 1 ht_init(ctx->color_lookback_ht);
164 1 ht_init(ctx->lut_lookback_ht);
165
166 1 bytestream2_put_le32(pbc, AV_RL32(ctx->tex_data));
167 1 bytestream2_put_le32(pbc, AV_RL32(ctx->tex_data + 4));
168
169 1 ht_lookup_and_upsert(ctx->color_lookback_ht, ctx->crc_ctx, AV_RL32(ctx->tex_data), 0);
170 1 ht_lookup_and_upsert(ctx->lut_lookback_ht, ctx->crc_ctx, AV_RL32(ctx->tex_data + 4), 1);
171
172
2/2
✓ Branch 0 taken 129599 times.
✓ Branch 1 taken 1 times.
129600 while (pos + 2 <= ctx->tex_size / 4) {
173 129599 idx = 0;
174
175 129599 color = AV_RL32(ctx->tex_data + pos * 4);
176 129599 prev_pos = ht_lookup_and_upsert(ctx->color_lookback_ht, ctx->crc_ctx, color, pos);
177
2/2
✓ Branch 0 taken 127924 times.
✓ Branch 1 taken 1675 times.
129599 color_idx = prev_pos != -1 ? pos - prev_pos : 0;
178
2/2
✓ Branch 0 taken 63807 times.
✓ Branch 1 taken 65792 times.
129599 if (pos >= LOOKBACK_WORDS) {
179 63807 uint32_t old_pos = pos - LOOKBACK_WORDS;
180 63807 uint32_t old_color = AV_RL32(ctx->tex_data + old_pos * 4);
181 63807 ht_delete(ctx->color_lookback_ht, ctx->crc_ctx, old_color, old_pos);
182 }
183 129599 pos++;
184
185 129599 lut = AV_RL32(ctx->tex_data + pos * 4);
186
4/4
✓ Branch 0 taken 127924 times.
✓ Branch 1 taken 1675 times.
✓ Branch 2 taken 123214 times.
✓ Branch 3 taken 4710 times.
129599 if (color_idx && lut == AV_RL32(ctx->tex_data + (pos - color_idx) * 4)) {
187 123214 idx = color_idx;
188 } else {
189 6385 idx = 0;
190 6385 prev_pos = ht_lookup_and_upsert(ctx->lut_lookback_ht, ctx->crc_ctx, lut, pos);
191
2/2
✓ Branch 0 taken 2179 times.
✓ Branch 1 taken 4206 times.
6385 lut_idx = prev_pos != -1 ? pos - prev_pos : 0;
192 }
193
2/2
✓ Branch 0 taken 63807 times.
✓ Branch 1 taken 65792 times.
129599 if (pos >= LOOKBACK_WORDS) {
194 63807 uint32_t old_pos = pos - LOOKBACK_WORDS;
195 63807 uint32_t old_lut = AV_RL32(ctx->tex_data + old_pos * 4);
196 63807 ht_delete(ctx->lut_lookback_ht, ctx->crc_ctx, old_lut, old_pos);
197 }
198 129599 pos++;
199
200
9/10
✓ Branch 0 taken 8111 times.
✓ Branch 1 taken 121488 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8111 times.
✓ Branch 6 taken 2535 times.
✓ Branch 7 taken 127064 times.
✓ Branch 9 taken 798 times.
✓ Branch 10 taken 126266 times.
✓ Branch 12 taken 119881 times.
✓ Branch 13 taken 6385 times.
129599 PUSH_OP(2);
201
202
2/2
✓ Branch 0 taken 6385 times.
✓ Branch 1 taken 123214 times.
129599 if (!idx) {
203 6385 idx = color_idx;
204
9/10
✓ Branch 0 taken 385 times.
✓ Branch 1 taken 6000 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 385 times.
✓ Branch 6 taken 3456 times.
✓ Branch 7 taken 2929 times.
✓ Branch 9 taken 730 times.
✓ Branch 10 taken 2199 times.
✓ Branch 12 taken 524 times.
✓ Branch 13 taken 1675 times.
6385 PUSH_OP(2);
205
2/2
✓ Branch 0 taken 1675 times.
✓ Branch 1 taken 4710 times.
6385 if (!idx)
206 1675 bytestream2_put_le32(pbc, color);
207
208 6385 idx = lut_idx;
209
9/10
✓ Branch 0 taken 403 times.
✓ Branch 1 taken 5982 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 403 times.
✓ Branch 6 taken 1797 times.
✓ Branch 7 taken 4588 times.
✓ Branch 9 taken 277 times.
✓ Branch 10 taken 4311 times.
✓ Branch 12 taken 105 times.
✓ Branch 13 taken 4206 times.
6385 PUSH_OP(2);
210
2/2
✓ Branch 0 taken 4206 times.
✓ Branch 1 taken 2179 times.
6385 if (!idx)
211 4206 bytestream2_put_le32(pbc, lut);
212 }
213 }
214
215 1 return 0;
216 }
217
218 1 static int dxv_encode(AVCodecContext *avctx, AVPacket *pkt,
219 const AVFrame *frame, int *got_packet)
220 {
221 1 DXVEncContext *ctx = avctx->priv_data;
222 1 PutByteContext *pbc = &ctx->pbc;
223 int ret;
224
225 /* unimplemented: needs to depend on compression ratio of tex format */
226 /* under DXT1, we need 3 words to encode load ops for 32 words.
227 * the first 2 words of the texture do not need load ops. */
228 1 ret = ff_alloc_packet(avctx, pkt, DXV_HEADER_LENGTH + ctx->tex_size + AV_CEIL_RSHIFT(ctx->tex_size - 8, 7) * 12);
229
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
230 return ret;
231
232
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (ctx->enc.tex_funct) {
233 1 ctx->enc.tex_data.out = ctx->tex_data;
234 1 ctx->enc.frame_data.in = frame->data[0];
235 1 ctx->enc.stride = frame->linesize[0];
236 1 ctx->enc.width = avctx->width;
237 1 ctx->enc.height = avctx->height;
238 1 ff_texturedsp_exec_compress_threads(avctx, &ctx->enc);
239 } else {
240 /* unimplemented: YCoCg formats */
241 return AVERROR_INVALIDDATA;
242 }
243
244 1 bytestream2_init_writer(pbc, pkt->data, pkt->size);
245
246 1 bytestream2_put_le32(pbc, ctx->tex_fmt);
247 1 bytestream2_put_byte(pbc, 4);
248 1 bytestream2_put_byte(pbc, 0);
249 1 bytestream2_put_byte(pbc, 0);
250 1 bytestream2_put_byte(pbc, 0);
251 /* Fill in compressed size later */
252 1 bytestream2_skip_p(pbc, 4);
253
254 1 ret = ctx->compress_tex(avctx);
255
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
256 return ret;
257
258 1 AV_WL32(pkt->data + 8, bytestream2_tell_p(pbc) - DXV_HEADER_LENGTH);
259 1 av_shrink_packet(pkt, bytestream2_tell_p(pbc));
260
261 1 *got_packet = 1;
262 1 return 0;
263 }
264
265 9 static av_cold int dxv_init(AVCodecContext *avctx)
266 {
267 9 DXVEncContext *ctx = avctx->priv_data;
268 TextureDSPEncContext texdsp;
269 9 int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
270
271
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (ret < 0) {
272 av_log(avctx, AV_LOG_ERROR, "Invalid image size %dx%d.\n",
273 avctx->width, avctx->height);
274 return ret;
275 }
276
277
2/4
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 9 times.
9 if (avctx->width % TEXTURE_BLOCK_W || avctx->height % TEXTURE_BLOCK_H) {
278 av_log(avctx,
279 AV_LOG_ERROR,
280 "Video size %dx%d is not multiple of "AV_STRINGIFY(TEXTURE_BLOCK_W)"x"AV_STRINGIFY(TEXTURE_BLOCK_H)".\n",
281 avctx->width, avctx->height);
282 return AVERROR_INVALIDDATA;
283 }
284
285 9 ff_texturedspenc_init(&texdsp);
286
287
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 switch (ctx->tex_fmt) {
288 9 case DXV_FMT_DXT1:
289 9 ctx->compress_tex = dxv_compress_dxt1;
290 9 ctx->enc.tex_funct = texdsp.dxt1_block;
291 9 ctx->enc.tex_ratio = 8;
292 9 break;
293 default:
294 av_log(avctx, AV_LOG_ERROR, "Invalid format %08X\n", ctx->tex_fmt);
295 return AVERROR_INVALIDDATA;
296 }
297 9 ctx->enc.raw_ratio = 16;
298 9 ctx->tex_size = avctx->width / TEXTURE_BLOCK_W *
299 9 avctx->height / TEXTURE_BLOCK_H *
300 9 ctx->enc.tex_ratio;
301 9 ctx->enc.slice_count = av_clip(avctx->thread_count, 1, avctx->height / TEXTURE_BLOCK_H);
302
303 9 ctx->tex_data = av_malloc(ctx->tex_size);
304
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (!ctx->tex_data) {
305 return AVERROR(ENOMEM);
306 }
307
308 9 ctx->crc_ctx = av_crc_get_table(AV_CRC_32_IEEE);
309
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (!ctx->crc_ctx) {
310 av_log(avctx, AV_LOG_ERROR, "Could not initialize CRC table.\n");
311 return AVERROR_BUG;
312 }
313
314 9 return 0;
315 }
316
317 9 static av_cold int dxv_close(AVCodecContext *avctx)
318 {
319 9 DXVEncContext *ctx = avctx->priv_data;
320
321 9 av_freep(&ctx->tex_data);
322
323 9 return 0;
324 }
325
326 #define OFFSET(x) offsetof(DXVEncContext, x)
327 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
328 static const AVOption options[] = {
329 { "format", NULL, OFFSET(tex_fmt), AV_OPT_TYPE_INT, { .i64 = DXV_FMT_DXT1 }, DXV_FMT_DXT1, DXV_FMT_DXT1, FLAGS, .unit = "format" },
330 { "dxt1", "DXT1 (Normal Quality, No Alpha)", 0, AV_OPT_TYPE_CONST, { .i64 = DXV_FMT_DXT1 }, 0, 0, FLAGS, .unit = "format" },
331 { NULL },
332 };
333
334 static const AVClass dxvenc_class = {
335 .class_name = "DXV encoder",
336 .option = options,
337 .version = LIBAVUTIL_VERSION_INT,
338 };
339
340 const FFCodec ff_dxv_encoder = {
341 .p.name = "dxv",
342 CODEC_LONG_NAME("Resolume DXV"),
343 .p.type = AVMEDIA_TYPE_VIDEO,
344 .p.id = AV_CODEC_ID_DXV,
345 .init = dxv_init,
346 FF_CODEC_ENCODE_CB(dxv_encode),
347 .close = dxv_close,
348 .priv_data_size = sizeof(DXVEncContext),
349 .p.capabilities = AV_CODEC_CAP_DR1 |
350 AV_CODEC_CAP_SLICE_THREADS |
351 AV_CODEC_CAP_FRAME_THREADS,
352 .p.priv_class = &dxvenc_class,
353 .p.pix_fmts = (const enum AVPixelFormat[]) {
354 AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE,
355 },
356 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
357 };
358