3 * Copyright (C) 2015 Vittorio Giovara <vittorio.giovara@gmail.com>
5 * This file is part of Libav.
7 * Libav 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.
12 * Libav 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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "libavutil/imgutils.h"
27 #include "bytestream.h"
30 #include "texturedsp.h"
33 typedef struct DXVContext {
34 TextureDSPContext texdsp;
37 uint8_t *tex_data; // Compressed texture
38 int tex_rat; // Compression ratio
39 int tex_step; // Distance between blocks
40 int64_t tex_size; // Texture size
42 /* Optimal number of slices for parallel decoding */
45 /* Pointer to the selected decompression function */
46 int (*tex_funct)(uint8_t *dst, ptrdiff_t stride, const uint8_t *block);
49 static int decompress_texture_thread(AVCodecContext *avctx, void *arg,
50 int slice, int thread_nb)
52 DXVContext *ctx = avctx->priv_data;
54 const uint8_t *d = ctx->tex_data;
55 int w_block = avctx->coded_width / TEXTURE_BLOCK_W;
56 int h_block = avctx->coded_height / TEXTURE_BLOCK_H;
58 int start_slice, end_slice;
59 int base_blocks_per_slice = h_block / ctx->slice_count;
60 int remainder_blocks = h_block % ctx->slice_count;
62 /* When the frame height (in blocks) doesn't divide evenly between the
63 * number of slices, spread the remaining blocks evenly between the first
65 start_slice = slice * base_blocks_per_slice;
66 /* Add any extra blocks (one per slice) that have been added
67 * before this slice */
68 start_slice += FFMIN(slice, remainder_blocks);
70 end_slice = start_slice + base_blocks_per_slice;
71 /* Add an extra block if there are remainder blocks to be accounted for */
72 if (slice < remainder_blocks)
75 for (y = start_slice; y < end_slice; y++) {
76 uint8_t *p = frame->data[0] + y * frame->linesize[0] * TEXTURE_BLOCK_H;
77 int off = y * w_block;
78 for (x = 0; x < w_block; x++) {
79 ctx->tex_funct(p + x * 16, frame->linesize[0],
80 d + (off + x) * ctx->tex_step);
87 /* This scheme addresses already decoded elements depending on 2-bit status:
88 * 0 -> copy new element
89 * 1 -> copy one element from position -x
90 * 2 -> copy one element from position -(get_byte() + 2) * x
91 * 3 -> copy one element from position -(get_16le() + 0x102) * x
92 * x is always 2 for dxt1 and 4 for dxt5. */
93 #define CHECKPOINT(x) \
96 value = bytestream2_get_le32(gbc); \
107 idx = (bytestream2_get_byte(gbc) + 2) * x; \
110 idx = (bytestream2_get_le16(gbc) + 0x102) * x; \
115 static int dxv_decompress_dxt1(AVCodecContext *avctx)
117 DXVContext *ctx = avctx->priv_data;
118 GetByteContext *gbc = &ctx->gbc;
119 uint32_t value, prev, op;
120 int idx = 0, state = 0;
123 /* Copy the first two elements */
124 AV_WL32(ctx->tex_data, bytestream2_get_le32(gbc));
125 AV_WL32(ctx->tex_data + 4, bytestream2_get_le32(gbc));
127 /* Process input until the whole texture has been filled */
128 while (pos + 2 <= ctx->tex_size / 4) {
131 /* Copy two elements from a previous offset or from the input buffer */
133 prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
134 AV_WL32(ctx->tex_data + 4 * pos, prev);
137 prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
138 AV_WL32(ctx->tex_data + 4 * pos, prev);
144 prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
146 prev = bytestream2_get_le32(gbc);
147 AV_WL32(ctx->tex_data + 4 * pos, prev);
153 prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
155 prev = bytestream2_get_le32(gbc);
156 AV_WL32(ctx->tex_data + 4 * pos, prev);
164 static int dxv_decompress_dxt5(AVCodecContext *avctx)
166 DXVContext *ctx = avctx->priv_data;
167 GetByteContext *gbc = &ctx->gbc;
169 int idx, prev, state = 0;
174 /* Copy the first four elements */
175 AV_WL32(ctx->tex_data + 0, bytestream2_get_le32(gbc));
176 AV_WL32(ctx->tex_data + 4, bytestream2_get_le32(gbc));
177 AV_WL32(ctx->tex_data + 8, bytestream2_get_le32(gbc));
178 AV_WL32(ctx->tex_data + 12, bytestream2_get_le32(gbc));
180 /* Process input until the whole texture has been filled */
181 while (pos + 2 <= ctx->tex_size / 4) {
185 prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
186 AV_WL32(ctx->tex_data + 4 * pos, prev);
188 prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
189 AV_WL32(ctx->tex_data + 4 * pos, prev);
193 value = bytestream2_get_le32(gbc);
203 check = bytestream2_get_byte(gbc) + 1;
206 probe = bytestream2_get_le16(gbc);
208 } while (probe == 0xFFFF);
210 while (check && pos + 4 <= ctx->tex_size / 4) {
211 prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
212 AV_WL32(ctx->tex_data + 4 * pos, prev);
215 prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
216 AV_WL32(ctx->tex_data + 4 * pos, prev);
219 prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
220 AV_WL32(ctx->tex_data + 4 * pos, prev);
223 prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
224 AV_WL32(ctx->tex_data + 4 * pos, prev);
230 /* Restart (or exit) the loop */
234 /* Load new run value */
235 run = bytestream2_get_byte(gbc);
238 probe = bytestream2_get_le16(gbc);
240 } while (probe == 0xFFFF);
243 /* Copy two dwords from previous data */
244 prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
245 AV_WL32(ctx->tex_data + 4 * pos, prev);
248 prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
249 AV_WL32(ctx->tex_data + 4 * pos, prev);
253 /* Copy two dwords from a previous index */
254 idx = 8 + bytestream2_get_le16(gbc);
255 if (idx > pos || (unsigned int)(pos - idx) + 2 > ctx->tex_size / 4)
256 return AVERROR_INVALIDDATA;
257 prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
258 AV_WL32(ctx->tex_data + 4 * pos, prev);
261 prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
262 AV_WL32(ctx->tex_data + 4 * pos, prev);
266 /* Copy two dwords from input */
267 prev = bytestream2_get_le32(gbc);
268 AV_WL32(ctx->tex_data + 4 * pos, prev);
271 prev = bytestream2_get_le32(gbc);
272 AV_WL32(ctx->tex_data + 4 * pos, prev);
279 if (pos + 2 > ctx->tex_size / 4)
280 return AVERROR_INVALIDDATA;
282 /* Copy two elements from a previous offset or from the input buffer */
284 if (idx > pos || (unsigned int)(pos - idx) + 2 > ctx->tex_size / 4)
285 return AVERROR_INVALIDDATA;
286 prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
287 AV_WL32(ctx->tex_data + 4 * pos, prev);
290 prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
291 AV_WL32(ctx->tex_data + 4 * pos, prev);
296 if (op && (idx > pos || (unsigned int)(pos - idx) + 2 > ctx->tex_size / 4))
297 return AVERROR_INVALIDDATA;
299 prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
301 prev = bytestream2_get_le32(gbc);
302 AV_WL32(ctx->tex_data + 4 * pos, prev);
308 prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
310 prev = bytestream2_get_le32(gbc);
311 AV_WL32(ctx->tex_data + 4 * pos, prev);
319 static int dxv_decompress_lzf(AVCodecContext *avctx)
321 DXVContext *ctx = avctx->priv_data;
322 return ff_lzf_uncompress(&ctx->gbc, &ctx->tex_data, &ctx->tex_size);
325 static int dxv_decompress_raw(AVCodecContext *avctx)
327 DXVContext *ctx = avctx->priv_data;
328 GetByteContext *gbc = &ctx->gbc;
330 bytestream2_get_buffer(gbc, ctx->tex_data, ctx->tex_size);
334 static int dxv_decode(AVCodecContext *avctx, void *data,
335 int *got_frame, AVPacket *avpkt)
337 DXVContext *ctx = avctx->priv_data;
339 GetByteContext *gbc = &ctx->gbc;
340 int (*decompress_tex)(AVCodecContext *avctx);
341 const char *msgcomp, *msgtext;
343 int version_major, version_minor = 0;
344 int size = 0, old_type = 0;
347 bytestream2_init(gbc, avpkt->data, avpkt->size);
349 tag = bytestream2_get_le32(gbc);
351 case MKBETAG('D', 'X', 'T', '1'):
352 decompress_tex = dxv_decompress_dxt1;
353 ctx->tex_funct = ctx->texdsp.dxt1_block;
359 case MKBETAG('D', 'X', 'T', '5'):
360 decompress_tex = dxv_decompress_dxt5;
361 ctx->tex_funct = ctx->texdsp.dxt5_block;
367 case MKBETAG('Y', 'C', 'G', '6'):
368 case MKBETAG('Y', 'G', '1', '0'):
369 avpriv_report_missing_feature(avctx, "Tag 0x%08X", tag);
370 return AVERROR_PATCHWELCOME;
372 /* Old version does not have a real header, just size and type. */
373 size = tag & 0x00FFFFFF;
374 old_type = tag >> 24;
375 version_major = (old_type & 0x0F) - 1;
377 if (old_type & 0x80) {
379 decompress_tex = dxv_decompress_raw;
382 decompress_tex = dxv_decompress_lzf;
385 if (old_type & 0x40) {
388 ctx->tex_funct = ctx->texdsp.dxt5_block;
390 } else if (old_type & 0x20 || version_major == 1) {
393 ctx->tex_funct = ctx->texdsp.dxt1_block;
396 av_log(avctx, AV_LOG_ERROR, "Unsupported header (0x%08X)\n.", tag);
397 return AVERROR_INVALIDDATA;
403 /* New header is 12 bytes long. */
405 version_major = bytestream2_get_byte(gbc) - 1;
406 version_minor = bytestream2_get_byte(gbc);
408 /* Encoder copies texture data when compression is not advantageous. */
409 if (bytestream2_get_byte(gbc)) {
412 decompress_tex = dxv_decompress_raw;
415 bytestream2_skip(gbc, 1); // unknown
416 size = bytestream2_get_le32(gbc);
418 av_log(avctx, AV_LOG_DEBUG,
419 "%s compression with %s texture (version %d.%d)\n",
420 msgcomp, msgtext, version_major, version_minor);
422 if (size != bytestream2_get_bytes_left(gbc)) {
423 av_log(avctx, AV_LOG_ERROR,
424 "Incomplete or invalid file (header %d, left %u).\n",
425 size, bytestream2_get_bytes_left(gbc));
426 return AVERROR_INVALIDDATA;
429 ctx->tex_size = avctx->coded_width * avctx->coded_height * 4 / ctx->tex_rat;
430 ret = av_reallocp(&ctx->tex_data, ctx->tex_size);
434 /* Decompress texture out of the intermediate compression. */
435 ret = decompress_tex(avctx);
440 ret = ff_thread_get_buffer(avctx, &tframe, 0);
443 ff_thread_finish_setup(avctx);
445 /* Now decompress the texture with the standard functions. */
446 avctx->execute2(avctx, decompress_texture_thread,
447 tframe.f, NULL, ctx->slice_count);
449 /* Frame is ready to be output. */
450 tframe.f->pict_type = AV_PICTURE_TYPE_I;
451 tframe.f->key_frame = 1;
457 static int dxv_init(AVCodecContext *avctx)
459 DXVContext *ctx = avctx->priv_data;
460 int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
463 av_log(avctx, AV_LOG_ERROR, "Invalid image size %dx%d.\n",
464 avctx->width, avctx->height);
468 /* Codec requires 16x16 alignment. */
469 avctx->coded_width = FFALIGN(avctx->width, 16);
470 avctx->coded_height = FFALIGN(avctx->height, 16);
472 ff_texturedsp_init(&ctx->texdsp);
473 avctx->pix_fmt = AV_PIX_FMT_RGBA;
475 ctx->slice_count = av_clip(avctx->thread_count, 1,
476 avctx->coded_height / TEXTURE_BLOCK_H);
481 static int dxv_close(AVCodecContext *avctx)
483 DXVContext *ctx = avctx->priv_data;
485 av_freep(&ctx->tex_data);
490 AVCodec ff_dxv_decoder = {
492 .long_name = NULL_IF_CONFIG_SMALL("Resolume DXV"),
493 .type = AVMEDIA_TYPE_VIDEO,
494 .id = AV_CODEC_ID_DXV,
496 .decode = dxv_decode,
498 .priv_data_size = sizeof(DXVContext),
499 .capabilities = AV_CODEC_CAP_DR1 |
500 AV_CODEC_CAP_SLICE_THREADS |
501 AV_CODEC_CAP_FRAME_THREADS,
502 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
503 FF_CODEC_CAP_INIT_CLEANUP,