3 * Copyright (C) 2015 Vittorio Giovara <vittorio.giovara@gmail.com>
5 * This file is part of FFmpeg.
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.
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.
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
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; \
109 av_log(avctx, AV_LOG_ERROR, "idx %d > %d\n", idx, pos); \
110 return AVERROR_INVALIDDATA; \
114 idx = (bytestream2_get_le16(gbc) + 0x102) * x; \
116 av_log(avctx, AV_LOG_ERROR, "idx %d > %d\n", idx, pos); \
117 return AVERROR_INVALIDDATA; \
123 static int dxv_decompress_dxt1(AVCodecContext *avctx)
125 DXVContext *ctx = avctx->priv_data;
126 GetByteContext *gbc = &ctx->gbc;
127 uint32_t value, prev, op;
128 int idx = 0, state = 0;
131 /* Copy the first two elements */
132 AV_WL32(ctx->tex_data, bytestream2_get_le32(gbc));
133 AV_WL32(ctx->tex_data + 4, bytestream2_get_le32(gbc));
135 /* Process input until the whole texture has been filled */
136 while (pos < ctx->tex_size / 4) {
139 /* Copy two elements from a previous offset or from the input buffer */
141 prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
142 AV_WL32(ctx->tex_data + 4 * pos, prev);
145 prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
146 AV_WL32(ctx->tex_data + 4 * pos, prev);
152 prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
154 prev = bytestream2_get_le32(gbc);
155 AV_WL32(ctx->tex_data + 4 * pos, prev);
161 prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
163 prev = bytestream2_get_le32(gbc);
164 AV_WL32(ctx->tex_data + 4 * pos, prev);
172 static int dxv_decompress_dxt5(AVCodecContext *avctx)
174 DXVContext *ctx = avctx->priv_data;
175 GetByteContext *gbc = &ctx->gbc;
177 int idx, prev, state = 0;
182 /* Copy the first four elements */
183 AV_WL32(ctx->tex_data + 0, bytestream2_get_le32(gbc));
184 AV_WL32(ctx->tex_data + 4, bytestream2_get_le32(gbc));
185 AV_WL32(ctx->tex_data + 8, bytestream2_get_le32(gbc));
186 AV_WL32(ctx->tex_data + 12, bytestream2_get_le32(gbc));
188 /* Process input until the whole texture has been filled */
189 while (pos < ctx->tex_size / 4) {
193 prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
194 AV_WL32(ctx->tex_data + 4 * pos, prev);
196 prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
197 AV_WL32(ctx->tex_data + 4 * pos, prev);
201 value = bytestream2_get_le32(gbc);
211 check = bytestream2_get_byte(gbc) + 1;
214 probe = bytestream2_get_le16(gbc);
216 } while (probe == 0xFFFF);
218 while (check && pos < ctx->tex_size / 4) {
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);
227 prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
228 AV_WL32(ctx->tex_data + 4 * pos, prev);
231 prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
232 AV_WL32(ctx->tex_data + 4 * pos, prev);
238 /* Restart (or exit) the loop */
242 /* Load new run value */
243 run = bytestream2_get_byte(gbc);
246 probe = bytestream2_get_le16(gbc);
248 } while (probe == 0xFFFF);
251 /* Copy two dwords from previous data */
252 prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
253 AV_WL32(ctx->tex_data + 4 * pos, prev);
256 prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
257 AV_WL32(ctx->tex_data + 4 * pos, prev);
261 /* Copy two dwords from a previous index */
262 idx = 8 + bytestream2_get_le16(gbc);
264 av_log(avctx, AV_LOG_ERROR, "idx %d > %d\n", idx, pos);
265 return AVERROR_INVALIDDATA;
267 prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
268 AV_WL32(ctx->tex_data + 4 * pos, prev);
271 prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
272 AV_WL32(ctx->tex_data + 4 * pos, prev);
276 /* Copy two dwords from input */
277 prev = bytestream2_get_le32(gbc);
278 AV_WL32(ctx->tex_data + 4 * pos, prev);
281 prev = bytestream2_get_le32(gbc);
282 AV_WL32(ctx->tex_data + 4 * pos, prev);
290 /* Copy two elements from a previous offset or from the input buffer */
292 prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
293 AV_WL32(ctx->tex_data + 4 * pos, prev);
296 prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
297 AV_WL32(ctx->tex_data + 4 * pos, prev);
303 prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
305 prev = bytestream2_get_le32(gbc);
306 AV_WL32(ctx->tex_data + 4 * pos, prev);
312 prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
314 prev = bytestream2_get_le32(gbc);
315 AV_WL32(ctx->tex_data + 4 * pos, prev);
323 static int dxv_decompress_lzf(AVCodecContext *avctx)
325 DXVContext *ctx = avctx->priv_data;
326 return ff_lzf_uncompress(&ctx->gbc, &ctx->tex_data, &ctx->tex_size);
329 static int dxv_decompress_raw(AVCodecContext *avctx)
331 DXVContext *ctx = avctx->priv_data;
332 GetByteContext *gbc = &ctx->gbc;
334 bytestream2_get_buffer(gbc, ctx->tex_data, ctx->tex_size);
338 static int dxv_decode(AVCodecContext *avctx, void *data,
339 int *got_frame, AVPacket *avpkt)
341 DXVContext *ctx = avctx->priv_data;
343 GetByteContext *gbc = &ctx->gbc;
344 int (*decompress_tex)(AVCodecContext *avctx);
345 const char *msgcomp, *msgtext;
347 int version_major, version_minor = 0;
348 int size = 0, old_type = 0;
351 bytestream2_init(gbc, avpkt->data, avpkt->size);
353 tag = bytestream2_get_le32(gbc);
355 case MKBETAG('D', 'X', 'T', '1'):
356 decompress_tex = dxv_decompress_dxt1;
357 ctx->tex_funct = ctx->texdsp.dxt1_block;
363 case MKBETAG('D', 'X', 'T', '5'):
364 decompress_tex = dxv_decompress_dxt5;
365 ctx->tex_funct = ctx->texdsp.dxt5_block;
371 case MKBETAG('Y', 'C', 'G', '6'):
372 case MKBETAG('Y', 'G', '1', '0'):
373 avpriv_report_missing_feature(avctx, "Tag 0x%08X", tag);
374 return AVERROR_PATCHWELCOME;
376 /* Old version does not have a real header, just size and type. */
377 size = tag & 0x00FFFFFF;
378 old_type = tag >> 24;
379 version_major = (old_type & 0x0F) - 1;
381 if (old_type & 0x80) {
383 decompress_tex = dxv_decompress_raw;
386 decompress_tex = dxv_decompress_lzf;
389 if (old_type & 0x40) {
392 ctx->tex_funct = ctx->texdsp.dxt5_block;
394 } else if (old_type & 0x20 || version_major == 1) {
397 ctx->tex_funct = ctx->texdsp.dxt1_block;
400 av_log(avctx, AV_LOG_ERROR, "Unsupported header (0x%08X)\n.", tag);
401 return AVERROR_INVALIDDATA;
407 /* New header is 12 bytes long. */
409 version_major = bytestream2_get_byte(gbc) - 1;
410 version_minor = bytestream2_get_byte(gbc);
412 /* Encoder copies texture data when compression is not advantageous. */
413 if (bytestream2_get_byte(gbc)) {
416 decompress_tex = dxv_decompress_raw;
419 bytestream2_skip(gbc, 1); // unknown
420 size = bytestream2_get_le32(gbc);
422 av_log(avctx, AV_LOG_DEBUG,
423 "%s compression with %s texture (version %d.%d)\n",
424 msgcomp, msgtext, version_major, version_minor);
426 if (size != bytestream2_get_bytes_left(gbc)) {
427 av_log(avctx, AV_LOG_ERROR,
428 "Incomplete or invalid file (header %d, left %d).\n",
429 size, bytestream2_get_bytes_left(gbc));
430 return AVERROR_INVALIDDATA;
433 ctx->tex_size = avctx->coded_width * avctx->coded_height * 4 / ctx->tex_rat;
434 ret = av_reallocp(&ctx->tex_data, ctx->tex_size);
438 /* Decompress texture out of the intermediate compression. */
439 ret = decompress_tex(avctx);
444 ret = ff_thread_get_buffer(avctx, &tframe, 0);
448 /* Now decompress the texture with the standard functions. */
449 avctx->execute2(avctx, decompress_texture_thread,
450 tframe.f, NULL, ctx->slice_count);
452 /* Frame is ready to be output. */
453 tframe.f->pict_type = AV_PICTURE_TYPE_I;
454 tframe.f->key_frame = 1;
460 static int dxv_init(AVCodecContext *avctx)
462 DXVContext *ctx = avctx->priv_data;
463 int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
466 av_log(avctx, AV_LOG_ERROR, "Invalid image size %dx%d.\n",
467 avctx->width, avctx->height);
471 /* Codec requires 16x16 alignment. */
472 avctx->coded_width = FFALIGN(avctx->width, 16);
473 avctx->coded_height = FFALIGN(avctx->height, 16);
475 ff_texturedsp_init(&ctx->texdsp);
476 avctx->pix_fmt = AV_PIX_FMT_RGBA;
478 ctx->slice_count = av_clip(avctx->thread_count, 1,
479 avctx->coded_height / TEXTURE_BLOCK_H);
484 static int dxv_close(AVCodecContext *avctx)
486 DXVContext *ctx = avctx->priv_data;
488 av_freep(&ctx->tex_data);
493 AVCodec ff_dxv_decoder = {
495 .long_name = NULL_IF_CONFIG_SMALL("Resolume DXV"),
496 .type = AVMEDIA_TYPE_VIDEO,
497 .id = AV_CODEC_ID_DXV,
499 .decode = dxv_decode,
501 .priv_data_size = sizeof(DXVContext),
502 .capabilities = AV_CODEC_CAP_DR1 |
503 AV_CODEC_CAP_SLICE_THREADS |
504 AV_CODEC_CAP_FRAME_THREADS,
505 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
506 FF_CODEC_CAP_INIT_CLEANUP,