3 * Copyright (C) 2015 Vittorio Giovara <vittorio.giovara@gmail.com>
4 * Copyright (C) 2018 Paul B Mahol
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "libavutil/imgutils.h"
29 #include "bytestream.h"
32 #include "texturedsp.h"
35 typedef struct DXVContext {
36 TextureDSPContext texdsp;
39 uint8_t *tex_data; // Compressed texture
40 uint8_t *ctex_data; // Compressed texture
41 int tex_rat; // Compression ratio
42 int tex_step; // Distance between blocks
43 int ctex_step; // Distance between blocks
44 int64_t tex_size; // Texture size
45 int64_t ctex_size; // Texture size
47 /* Optimal number of slices for parallel decoding */
50 uint8_t *op_data[4]; // Opcodes
51 int64_t op_size[4]; // Opcodes size
59 /* Pointer to the selected decompression function */
60 int (*tex_funct)(uint8_t *dst, ptrdiff_t stride, const uint8_t *block);
61 int (*tex_funct_planar[2])(uint8_t *plane0, ptrdiff_t stride0,
62 uint8_t *plane1, ptrdiff_t stride1,
63 const uint8_t *block);
66 static void decompress_indices(uint8_t *dst, const uint8_t *src)
70 for (block = 0; block < 2; block++) {
71 int tmp = AV_RL24(src);
73 /* Unpack 8x3 bit from last 3 byte block */
74 for (i = 0; i < 8; i++)
75 dst[i] = (tmp >> (i * 3)) & 0x7;
82 static int extract_component(int yo0, int yo1, int code)
88 } else if (code == 0) {
90 } else if (code == 1) {
94 yo = (uint8_t) (((8 - code) * yo0 +
95 (code - 1) * yo1) / 7);
99 } else if (code == 7) {
102 yo = (uint8_t) (((6 - code) * yo0 +
103 (code - 1) * yo1) / 5);
111 static int cocg_block(uint8_t *plane0, ptrdiff_t stride0,
112 uint8_t *plane1, ptrdiff_t stride1,
113 const uint8_t *block)
115 uint8_t co_indices[16];
116 uint8_t cg_indices[16];
117 uint8_t co0 = *(block);
118 uint8_t co1 = *(block + 1);
119 uint8_t cg0 = *(block + 8);
120 uint8_t cg1 = *(block + 9);
123 decompress_indices(co_indices, block + 2);
124 decompress_indices(cg_indices, block + 10);
126 for (y = 0; y < 4; y++) {
127 for (x = 0; x < 4; x++) {
128 int co_code = co_indices[x + y * 4];
129 int cg_code = cg_indices[x + y * 4];
131 plane0[x] = extract_component(cg0, cg1, cg_code);
132 plane1[x] = extract_component(co0, co1, co_code);
141 static void yao_subblock(uint8_t *dst, uint8_t *yo_indices,
142 ptrdiff_t stride, const uint8_t *block)
144 uint8_t yo0 = *(block);
145 uint8_t yo1 = *(block + 1);
148 decompress_indices(yo_indices, block + 2);
150 for (y = 0; y < 4; y++) {
151 for (x = 0; x < 4; x++) {
152 int yo_code = yo_indices[x + y * 4];
154 dst[x] = extract_component(yo0, yo1, yo_code);
160 static int yo_block(uint8_t *dst, ptrdiff_t stride,
161 uint8_t *unused0, ptrdiff_t unused1,
162 const uint8_t *block)
164 uint8_t yo_indices[16];
166 yao_subblock(dst, yo_indices, stride, block);
167 yao_subblock(dst + 4, yo_indices, stride, block + 8);
168 yao_subblock(dst + 8, yo_indices, stride, block + 16);
169 yao_subblock(dst + 12, yo_indices, stride, block + 24);
174 static int yao_block(uint8_t *plane0, ptrdiff_t stride0,
175 uint8_t *plane3, ptrdiff_t stride1,
176 const uint8_t *block)
178 uint8_t yo_indices[16];
179 uint8_t a_indices[16];
181 yao_subblock(plane0, yo_indices, stride0, block);
182 yao_subblock(plane3, a_indices, stride1, block + 8);
183 yao_subblock(plane0 + 4, yo_indices, stride0, block + 16);
184 yao_subblock(plane3 + 4, a_indices, stride1, block + 24);
185 yao_subblock(plane0 + 8, yo_indices, stride0, block + 32);
186 yao_subblock(plane3 + 8, a_indices, stride1, block + 40);
187 yao_subblock(plane0 + 12, yo_indices, stride0, block + 48);
188 yao_subblock(plane3 + 12, a_indices, stride1, block + 56);
193 static int decompress_texture_thread(AVCodecContext *avctx, void *arg,
194 int slice, int thread_nb)
196 DXVContext *ctx = avctx->priv_data;
197 AVFrame *frame = arg;
198 const uint8_t *d = ctx->tex_data;
199 int w_block = avctx->coded_width / ctx->texture_block_w;
200 int h_block = avctx->coded_height / ctx->texture_block_h;
202 int start_slice, end_slice;
204 start_slice = h_block * slice / ctx->slice_count;
205 end_slice = h_block * (slice + 1) / ctx->slice_count;
207 if (ctx->tex_funct) {
208 for (y = start_slice; y < end_slice; y++) {
209 uint8_t *p = frame->data[0] + y * frame->linesize[0] * ctx->texture_block_h;
210 int off = y * w_block;
211 for (x = 0; x < w_block; x++) {
212 ctx->tex_funct(p + x * 4 * ctx->texture_block_w, frame->linesize[0],
213 d + (off + x) * ctx->tex_step);
217 const uint8_t *c = ctx->ctex_data;
219 for (y = start_slice; y < end_slice; y++) {
220 uint8_t *p0 = frame->data[0] + y * frame->linesize[0] * ctx->texture_block_h;
221 uint8_t *p3 = ctx->tex_step != 64 ? NULL : frame->data[3] + y * frame->linesize[3] * ctx->texture_block_h;
222 int off = y * w_block;
223 for (x = 0; x < w_block; x++) {
224 ctx->tex_funct_planar[0](p0 + x * ctx->texture_block_w, frame->linesize[0],
225 p3 != NULL ? p3 + x * ctx->texture_block_w : NULL, frame->linesize[3],
226 d + (off + x) * ctx->tex_step);
230 w_block = (avctx->coded_width / 2) / ctx->ctexture_block_w;
231 h_block = (avctx->coded_height / 2) / ctx->ctexture_block_h;
232 start_slice = h_block * slice / ctx->slice_count;
233 end_slice = h_block * (slice + 1) / ctx->slice_count;
235 for (y = start_slice; y < end_slice; y++) {
236 uint8_t *p0 = frame->data[1] + y * frame->linesize[1] * ctx->ctexture_block_h;
237 uint8_t *p1 = frame->data[2] + y * frame->linesize[2] * ctx->ctexture_block_h;
238 int off = y * w_block;
239 for (x = 0; x < w_block; x++) {
240 ctx->tex_funct_planar[1](p0 + x * ctx->ctexture_block_w, frame->linesize[1],
241 p1 + x * ctx->ctexture_block_w, frame->linesize[2],
242 c + (off + x) * ctx->ctex_step);
250 /* This scheme addresses already decoded elements depending on 2-bit status:
251 * 0 -> copy new element
252 * 1 -> copy one element from position -x
253 * 2 -> copy one element from position -(get_byte() + 2) * x
254 * 3 -> copy one element from position -(get_16le() + 0x102) * x
255 * x is always 2 for dxt1 and 4 for dxt5. */
256 #define CHECKPOINT(x) \
259 value = bytestream2_get_le32(gbc); \
270 idx = (bytestream2_get_byte(gbc) + 2) * x; \
272 av_log(avctx, AV_LOG_ERROR, "idx %d > %d\n", idx, pos); \
273 return AVERROR_INVALIDDATA; \
277 idx = (bytestream2_get_le16(gbc) + 0x102) * x; \
279 av_log(avctx, AV_LOG_ERROR, "idx %d > %d\n", idx, pos); \
280 return AVERROR_INVALIDDATA; \
286 static int dxv_decompress_dxt1(AVCodecContext *avctx)
288 DXVContext *ctx = avctx->priv_data;
289 GetByteContext *gbc = &ctx->gbc;
290 uint32_t value, prev, op;
291 int idx = 0, state = 0;
294 /* Copy the first two elements */
295 AV_WL32(ctx->tex_data, bytestream2_get_le32(gbc));
296 AV_WL32(ctx->tex_data + 4, bytestream2_get_le32(gbc));
298 /* Process input until the whole texture has been filled */
299 while (pos + 2 <= ctx->tex_size / 4) {
302 /* Copy two elements from a previous offset or from the input buffer */
304 prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
305 AV_WL32(ctx->tex_data + 4 * pos, prev);
308 prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
309 AV_WL32(ctx->tex_data + 4 * pos, prev);
315 prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
317 prev = bytestream2_get_le32(gbc);
318 AV_WL32(ctx->tex_data + 4 * pos, prev);
324 prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
326 prev = bytestream2_get_le32(gbc);
327 AV_WL32(ctx->tex_data + 4 * pos, prev);
335 typedef struct OpcodeTable {
341 static int fill_ltable(GetByteContext *gb, uint32_t *table, int *nb_elements)
343 unsigned half = 512, bits = 1023, left = 1024, input, mask;
344 int value, counter = 0, rshift = 10, lshift = 30;
346 mask = bytestream2_get_le32(gb) >> 2;
349 return AVERROR_INVALIDDATA;
354 table[counter++] = value;
356 if (bytestream2_get_bytes_left(gb) <= 0)
357 return AVERROR_INVALIDDATA;
359 input = bytestream2_get_le16(gb);
360 mask += input << lshift;
370 for (; !table[counter - 1]; counter--)
372 return AVERROR_INVALIDDATA;
374 *nb_elements = counter;
377 memset(&table[counter], 0, 4 * (256 - counter));
380 bytestream2_seek(gb, -2, SEEK_CUR);
385 static int fill_optable(unsigned *table0, OpcodeTable *table1, int nb_elements)
387 unsigned table2[256] = { 0 };
389 int val0, val1, i, j = 2, k = 0;
391 table2[0] = table0[0];
392 for (i = 0; i < nb_elements - 1; i++, table2[i] = val0) {
393 val0 = table0[i + 1] + table2[i];
399 } while (!table2[k]);
403 for (i = 1024; i > 0; i--) {
404 for (table1[x].val1 = k; k < 256 && j > table2[k]; k++);
405 x = (x - 383) & 0x3FF;
410 memcpy(&table2[0], table0, 4 * nb_elements);
412 for (i = 0; i < 1024; i++) {
413 val0 = table1[i].val1;
416 x = 31 - ff_clz(val1);
418 return AVERROR_INVALIDDATA;
419 table1[i].val2 = 10 - x;
420 table1[i].next = (val1 << table1[i].val2) - 1024;
426 static int get_opcodes(GetByteContext *gb, uint32_t *table, uint8_t *dst, int op_size, int nb_elements)
428 OpcodeTable optable[1024];
429 int sum, x, val, lshift, rshift, ret, i, idx;
430 int64_t size_in_bits;
431 unsigned endoffset, newoffset, offset;
433 uint8_t *src = (uint8_t *)gb->buffer;
435 ret = fill_optable(table, optable, nb_elements);
439 size_in_bits = bytestream2_get_le32(gb);
440 endoffset = ((size_in_bits + 7) >> 3) - 4;
441 if (endoffset <= 0 || bytestream2_get_bytes_left(gb) < endoffset)
442 return AVERROR_INVALIDDATA;
445 next = AV_RL32(src + endoffset);
446 rshift = (((size_in_bits & 0xFF) - 1) & 7) + 15;
447 lshift = 32 - rshift;
448 idx = (next >> rshift) & 0x3FF;
449 for (i = 0; i < op_size; i++) {
450 dst[i] = optable[idx].val1;
451 val = optable[idx].val2;
453 x = (next << lshift) >> 1 >> (31 - val);
454 newoffset = offset - (sum >> 3);
456 idx = x + optable[idx].next;
458 if (offset > endoffset)
459 return AVERROR_INVALIDDATA;
460 next = AV_RL32(src + offset);
463 bytestream2_skip(gb, (size_in_bits + 7 >> 3) - 4);
468 static int dxv_decompress_opcodes(GetByteContext *gb, void *dstp, size_t op_size)
470 int pos = bytestream2_tell(gb);
471 int flag = bytestream2_peek_byte(gb);
473 if ((flag & 3) == 0) {
474 bytestream2_skip(gb, 1);
475 bytestream2_get_buffer(gb, dstp, op_size);
476 } else if ((flag & 3) == 1) {
477 bytestream2_skip(gb, 1);
478 memset(dstp, bytestream2_get_byte(gb), op_size);
481 int ret, elements = 0;
483 ret = fill_ltable(gb, table, &elements);
486 ret = get_opcodes(gb, table, dstp, op_size, elements);
490 return bytestream2_tell(gb) - pos;
493 static int dxv_decompress_cgo(DXVContext *ctx, GetByteContext *gb,
494 uint8_t *tex_data, int tex_size,
495 uint8_t *op_data, int *oindex,
497 uint8_t **dstp, int *statep,
498 uint8_t **tab0, uint8_t **tab1,
501 uint8_t *dst = *dstp;
502 uint8_t *tptr0, *tptr1, *tptr3;
509 return AVERROR_INVALIDDATA;
510 opcode = op_data[oi++];
512 v = bytestream2_get_byte(gb);
515 if (bytestream2_get_bytes_left(gb) <= 0)
516 return AVERROR_INVALIDDATA;
517 opcode = bytestream2_get_le16(gb);
519 } while (opcode == 0xFFFF);
521 AV_WL32(dst, AV_RL32(dst - (8 + offset)));
522 AV_WL32(dst + 4, AV_RL32(dst - (4 + offset)));
529 AV_WL32(dst, AV_RL32(dst - (8 + offset)));
530 AV_WL32(dst + 4, AV_RL32(dst - (4 + offset)));
533 vv = (8 + offset) * (bytestream2_get_le16(gb) + 1);
534 if (vv < 0 || vv > dst - tex_data)
535 return AVERROR_INVALIDDATA;
538 AV_WL32(dst, AV_RL32(tptr0));
539 AV_WL32(dst + 4, AV_RL32(tptr0 + 4));
540 tab0[0x9E3779B1 * (uint16_t)v >> 24] = dst;
541 tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
544 AV_WL32(dst, bytestream2_get_le32(gb));
545 AV_WL32(dst + 4, bytestream2_get_le32(gb));
546 tab0[0x9E3779B1 * AV_RL16(dst) >> 24] = dst;
547 tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
550 tptr3 = tab1[bytestream2_get_byte(gb)];
552 return AVERROR_INVALIDDATA;
553 AV_WL16(dst, bytestream2_get_le16(gb));
554 AV_WL16(dst + 2, AV_RL16(tptr3));
556 AV_WL16(dst + 5, bytestream2_get_le16(gb));
557 dst[7] = bytestream2_get_byte(gb);
558 tab0[0x9E3779B1 * AV_RL16(dst) >> 24] = dst;
561 tptr3 = tab1[bytestream2_get_byte(gb)];
563 return AVERROR_INVALIDDATA;
564 AV_WL16(dst, bytestream2_get_le16(gb));
565 AV_WL16(dst + 2, bytestream2_get_le16(gb));
566 dst[4] = bytestream2_get_byte(gb);
567 AV_WL16(dst + 5, AV_RL16(tptr3));
569 tab0[0x9E3779B1 * AV_RL16(dst) >> 24] = dst;
570 tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
573 tptr0 = tab1[bytestream2_get_byte(gb)];
575 return AVERROR_INVALIDDATA;
576 tptr1 = tab1[bytestream2_get_byte(gb)];
578 return AVERROR_INVALIDDATA;
579 AV_WL16(dst, bytestream2_get_le16(gb));
580 AV_WL16(dst + 2, AV_RL16(tptr0));
582 AV_WL16(dst + 5, AV_RL16(tptr1));
584 tab0[0x9E3779B1 * AV_RL16(dst) >> 24] = dst;
587 v = (8 + offset) * (bytestream2_get_le16(gb) + 1);
588 if (v < 0 || v > dst - tex_data)
589 return AVERROR_INVALIDDATA;
591 AV_WL16(dst, bytestream2_get_le16(gb));
592 AV_WL16(dst + 2, AV_RL16(tptr0 + 2));
593 AV_WL32(dst + 4, AV_RL32(tptr0 + 4));
594 tab0[0x9E3779B1 * AV_RL16(dst) >> 24] = dst;
595 tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
598 tptr1 = tab0[bytestream2_get_byte(gb)];
600 return AVERROR_INVALIDDATA;
601 AV_WL16(dst, AV_RL16(tptr1));
602 AV_WL16(dst + 2, bytestream2_get_le16(gb));
603 AV_WL32(dst + 4, bytestream2_get_le32(gb));
604 tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
607 tptr1 = tab0[bytestream2_get_byte(gb)];
609 return AVERROR_INVALIDDATA;
610 tptr3 = tab1[bytestream2_get_byte(gb)];
612 return AVERROR_INVALIDDATA;
613 AV_WL16(dst, AV_RL16(tptr1));
614 AV_WL16(dst + 2, AV_RL16(tptr3));
616 AV_WL16(dst + 5, bytestream2_get_le16(gb));
617 dst[7] = bytestream2_get_byte(gb);
618 tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
621 tptr1 = tab0[bytestream2_get_byte(gb)];
623 return AVERROR_INVALIDDATA;
624 tptr3 = tab1[bytestream2_get_byte(gb)];
626 return AVERROR_INVALIDDATA;
627 AV_WL16(dst, AV_RL16(tptr1));
628 AV_WL16(dst + 2, bytestream2_get_le16(gb));
629 dst[4] = bytestream2_get_byte(gb);
630 AV_WL16(dst + 5, AV_RL16(tptr3));
632 tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
635 tptr0 = tab0[bytestream2_get_byte(gb)];
637 return AVERROR_INVALIDDATA;
638 tptr3 = tab1[bytestream2_get_byte(gb)];
640 return AVERROR_INVALIDDATA;
641 tptr1 = tab1[bytestream2_get_byte(gb)];
643 return AVERROR_INVALIDDATA;
644 AV_WL16(dst, AV_RL16(tptr0));
645 AV_WL16(dst + 2, AV_RL16(tptr3));
647 AV_WL16(dst + 5, AV_RL16(tptr1));
651 tptr1 = tab0[bytestream2_get_byte(gb)];
653 return AVERROR_INVALIDDATA;
654 v = (8 + offset) * (bytestream2_get_le16(gb) + 1);
655 if (v < 0 || v > dst - tex_data)
656 return AVERROR_INVALIDDATA;
658 AV_WL16(dst, AV_RL16(tptr1));
659 AV_WL16(dst + 2, AV_RL16(tptr0 + 2));
660 AV_WL32(dst + 4, AV_RL32(tptr0 + 4));
661 tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
664 AV_WL16(dst, AV_RL16(dst - (8 + offset)));
665 AV_WL16(dst + 2, bytestream2_get_le16(gb));
666 AV_WL32(dst + 4, bytestream2_get_le32(gb));
667 tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
670 tptr3 = tab1[bytestream2_get_byte(gb)];
672 return AVERROR_INVALIDDATA;
673 AV_WL16(dst, AV_RL16(dst - (8 + offset)));
674 AV_WL16(dst + 2, AV_RL16(tptr3));
676 AV_WL16(dst + 5, bytestream2_get_le16(gb));
677 dst[7] = bytestream2_get_byte(gb);
678 tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
681 tptr3 = tab1[bytestream2_get_byte(gb)];
683 return AVERROR_INVALIDDATA;
684 AV_WL16(dst, AV_RL16(dst - (8 + offset)));
685 AV_WL16(dst + 2, bytestream2_get_le16(gb));
686 dst[4] = bytestream2_get_byte(gb);
687 AV_WL16(dst + 5, AV_RL16(tptr3));
689 tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
692 tptr3 = tab1[bytestream2_get_byte(gb)];
694 return AVERROR_INVALIDDATA;
695 tptr1 = tab1[bytestream2_get_byte(gb)];
697 return AVERROR_INVALIDDATA;
698 AV_WL16(dst, AV_RL16(dst - (8 + offset)));
699 AV_WL16(dst + 2, AV_RL16(tptr3));
701 AV_WL16(dst + 5, AV_RL16(tptr1));
705 v = (8 + offset) * (bytestream2_get_le16(gb) + 1);
706 if (v < 0 || v > dst - tex_data)
707 return AVERROR_INVALIDDATA;
708 AV_WL16(dst, AV_RL16(dst - (8 + offset)));
709 AV_WL16(dst + 2, AV_RL16(&dst[-v + 2]));
710 AV_WL32(dst + 4, AV_RL32(&dst[-v + 4]));
711 tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
718 AV_WL32(dst, AV_RL32(dst - (8 + offset)));
719 AV_WL32(dst + 4, AV_RL32(dst - (4 + offset)));
722 if (dst - tex_data + 8 > tex_size)
723 return AVERROR_INVALIDDATA;
733 static int dxv_decompress_cocg(DXVContext *ctx, GetByteContext *gb,
734 uint8_t *tex_data, int tex_size,
735 uint8_t *op_data0, uint8_t *op_data1,
736 int max_op_size0, int max_op_size1)
738 uint8_t *dst, *tab2[256] = { 0 }, *tab0[256] = { 0 }, *tab3[256] = { 0 }, *tab1[256] = { 0 };
739 int op_offset = bytestream2_get_le32(gb);
740 unsigned op_size0 = bytestream2_get_le32(gb);
741 unsigned op_size1 = bytestream2_get_le32(gb);
742 int data_start = bytestream2_tell(gb);
743 int skip0, skip1, oi0 = 0, oi1 = 0;
744 int ret, state0 = 0, state1 = 0;
747 bytestream2_skip(gb, op_offset - 12);
748 if (op_size0 > max_op_size0)
749 return AVERROR_INVALIDDATA;
750 skip0 = dxv_decompress_opcodes(gb, op_data0, op_size0);
753 bytestream2_seek(gb, data_start + op_offset + skip0 - 12, SEEK_SET);
754 if (op_size1 > max_op_size1)
755 return AVERROR_INVALIDDATA;
756 skip1 = dxv_decompress_opcodes(gb, op_data1, op_size1);
759 bytestream2_seek(gb, data_start, SEEK_SET);
761 AV_WL32(dst, bytestream2_get_le32(gb));
762 AV_WL32(dst + 4, bytestream2_get_le32(gb));
763 AV_WL32(dst + 8, bytestream2_get_le32(gb));
764 AV_WL32(dst + 12, bytestream2_get_le32(gb));
766 tab0[0x9E3779B1 * AV_RL16(dst) >> 24] = dst;
767 tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFF) >> 24] = dst + 2;
768 tab2[0x9E3779B1 * AV_RL16(dst + 8) >> 24] = dst + 8;
769 tab3[0x9E3779B1 * (AV_RL32(dst + 10) & 0xFFFFFF) >> 24] = dst + 10;
771 while (dst + 10 < tex_data + tex_size) {
772 ret = dxv_decompress_cgo(ctx, gb, tex_data, tex_size, op_data0, &oi0, op_size0,
773 &dst, &state0, tab0, tab1, 8);
776 ret = dxv_decompress_cgo(ctx, gb, tex_data, tex_size, op_data1, &oi1, op_size1,
777 &dst, &state1, tab2, tab3, 8);
782 bytestream2_seek(gb, data_start + op_offset + skip0 + skip1 - 12, SEEK_SET);
787 static int dxv_decompress_yo(DXVContext *ctx, GetByteContext *gb,
788 uint8_t *tex_data, int tex_size,
789 uint8_t *op_data, int max_op_size)
791 int op_offset = bytestream2_get_le32(gb);
792 unsigned op_size = bytestream2_get_le32(gb);
793 int data_start = bytestream2_tell(gb);
794 uint8_t *dst, *table0[256] = { 0 }, *table1[256] = { 0 };
795 int ret, state = 0, skip, oi = 0, v, vv;
798 bytestream2_skip(gb, op_offset - 8);
799 if (op_size > max_op_size)
800 return AVERROR_INVALIDDATA;
801 skip = dxv_decompress_opcodes(gb, op_data, op_size);
804 bytestream2_seek(gb, data_start, SEEK_SET);
806 v = bytestream2_get_le32(gb);
808 vv = bytestream2_get_le32(gb);
809 table0[0x9E3779B1 * (uint16_t)v >> 24] = dst;
810 AV_WL32(dst + 4, vv);
811 table1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFF) >> 24] = dst + 2;
814 while (dst < tex_data + tex_size) {
815 ret = dxv_decompress_cgo(ctx, gb, tex_data, tex_size, op_data, &oi, op_size,
816 &dst, &state, table0, table1, 0);
821 bytestream2_seek(gb, data_start + op_offset + skip - 8, SEEK_SET);
826 static int dxv_decompress_ycg6(AVCodecContext *avctx)
828 DXVContext *ctx = avctx->priv_data;
829 GetByteContext *gb = &ctx->gbc;
832 ret = dxv_decompress_yo(ctx, gb, ctx->tex_data, ctx->tex_size,
833 ctx->op_data[0], ctx->op_size[0]);
837 return dxv_decompress_cocg(ctx, gb, ctx->ctex_data, ctx->ctex_size,
838 ctx->op_data[1], ctx->op_data[2],
839 ctx->op_size[1], ctx->op_size[2]);
842 static int dxv_decompress_yg10(AVCodecContext *avctx)
844 DXVContext *ctx = avctx->priv_data;
845 GetByteContext *gb = &ctx->gbc;
848 ret = dxv_decompress_cocg(ctx, gb, ctx->tex_data, ctx->tex_size,
849 ctx->op_data[0], ctx->op_data[3],
850 ctx->op_size[0], ctx->op_size[3]);
854 return dxv_decompress_cocg(ctx, gb, ctx->ctex_data, ctx->ctex_size,
855 ctx->op_data[1], ctx->op_data[2],
856 ctx->op_size[1], ctx->op_size[2]);
859 static int dxv_decompress_dxt5(AVCodecContext *avctx)
861 DXVContext *ctx = avctx->priv_data;
862 GetByteContext *gbc = &ctx->gbc;
864 int idx, prev, state = 0;
869 /* Copy the first four elements */
870 AV_WL32(ctx->tex_data + 0, bytestream2_get_le32(gbc));
871 AV_WL32(ctx->tex_data + 4, bytestream2_get_le32(gbc));
872 AV_WL32(ctx->tex_data + 8, bytestream2_get_le32(gbc));
873 AV_WL32(ctx->tex_data + 12, bytestream2_get_le32(gbc));
875 /* Process input until the whole texture has been filled */
876 while (pos + 2 <= ctx->tex_size / 4) {
880 prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
881 AV_WL32(ctx->tex_data + 4 * pos, prev);
883 prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
884 AV_WL32(ctx->tex_data + 4 * pos, prev);
887 if (bytestream2_get_bytes_left(gbc) < 1)
888 return AVERROR_INVALIDDATA;
890 value = bytestream2_get_le32(gbc);
900 check = bytestream2_get_byte(gbc) + 1;
903 probe = bytestream2_get_le16(gbc);
905 } while (probe == 0xFFFF);
907 while (check && pos + 4 <= ctx->tex_size / 4) {
908 prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
909 AV_WL32(ctx->tex_data + 4 * pos, prev);
912 prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
913 AV_WL32(ctx->tex_data + 4 * pos, prev);
916 prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
917 AV_WL32(ctx->tex_data + 4 * pos, prev);
920 prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
921 AV_WL32(ctx->tex_data + 4 * pos, prev);
927 /* Restart (or exit) the loop */
931 /* Load new run value */
932 run = bytestream2_get_byte(gbc);
935 probe = bytestream2_get_le16(gbc);
937 } while (probe == 0xFFFF);
940 /* Copy two dwords from previous data */
941 prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
942 AV_WL32(ctx->tex_data + 4 * pos, prev);
945 prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
946 AV_WL32(ctx->tex_data + 4 * pos, prev);
950 /* Copy two dwords from a previous index */
951 idx = 8 + bytestream2_get_le16(gbc);
952 if (idx > pos || (unsigned int)(pos - idx) + 2 > ctx->tex_size / 4)
953 return AVERROR_INVALIDDATA;
954 prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
955 AV_WL32(ctx->tex_data + 4 * pos, prev);
958 prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
959 AV_WL32(ctx->tex_data + 4 * pos, prev);
963 /* Copy two dwords from input */
964 prev = bytestream2_get_le32(gbc);
965 AV_WL32(ctx->tex_data + 4 * pos, prev);
968 prev = bytestream2_get_le32(gbc);
969 AV_WL32(ctx->tex_data + 4 * pos, prev);
976 if (pos + 2 > ctx->tex_size / 4)
977 return AVERROR_INVALIDDATA;
979 /* Copy two elements from a previous offset or from the input buffer */
981 if (idx > pos || (unsigned int)(pos - idx) + 2 > ctx->tex_size / 4)
982 return AVERROR_INVALIDDATA;
983 prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
984 AV_WL32(ctx->tex_data + 4 * pos, prev);
987 prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
988 AV_WL32(ctx->tex_data + 4 * pos, prev);
993 if (op && (idx > pos || (unsigned int)(pos - idx) + 2 > ctx->tex_size / 4))
994 return AVERROR_INVALIDDATA;
996 prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
998 prev = bytestream2_get_le32(gbc);
999 AV_WL32(ctx->tex_data + 4 * pos, prev);
1005 prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
1007 prev = bytestream2_get_le32(gbc);
1008 AV_WL32(ctx->tex_data + 4 * pos, prev);
1016 static int dxv_decompress_lzf(AVCodecContext *avctx)
1018 DXVContext *ctx = avctx->priv_data;
1019 return ff_lzf_uncompress(&ctx->gbc, &ctx->tex_data, &ctx->tex_size);
1022 static int dxv_decompress_raw(AVCodecContext *avctx)
1024 DXVContext *ctx = avctx->priv_data;
1025 GetByteContext *gbc = &ctx->gbc;
1027 if (bytestream2_get_bytes_left(gbc) < ctx->tex_size)
1028 return AVERROR_INVALIDDATA;
1030 bytestream2_get_buffer(gbc, ctx->tex_data, ctx->tex_size);
1034 static int dxv_decode(AVCodecContext *avctx, void *data,
1035 int *got_frame, AVPacket *avpkt)
1037 DXVContext *ctx = avctx->priv_data;
1039 GetByteContext *gbc = &ctx->gbc;
1040 int (*decompress_tex)(AVCodecContext *avctx);
1041 const char *msgcomp, *msgtext;
1043 int version_major, version_minor = 0;
1044 int size = 0, old_type = 0;
1047 bytestream2_init(gbc, avpkt->data, avpkt->size);
1049 ctx->texture_block_h = 4;
1050 ctx->texture_block_w = 4;
1052 avctx->pix_fmt = AV_PIX_FMT_RGBA;
1053 avctx->colorspace = AVCOL_SPC_RGB;
1055 tag = bytestream2_get_le32(gbc);
1057 case MKBETAG('D', 'X', 'T', '1'):
1058 decompress_tex = dxv_decompress_dxt1;
1059 ctx->tex_funct = ctx->texdsp.dxt1_block;
1065 case MKBETAG('D', 'X', 'T', '5'):
1066 decompress_tex = dxv_decompress_dxt5;
1067 ctx->tex_funct = ctx->texdsp.dxt5_block;
1073 case MKBETAG('Y', 'C', 'G', '6'):
1074 decompress_tex = dxv_decompress_ycg6;
1075 ctx->tex_funct_planar[0] = yo_block;
1076 ctx->tex_funct_planar[1] = cocg_block;
1079 ctx->ctex_step = 16;
1080 msgcomp = "YOCOCG6";
1082 ctx->ctex_size = avctx->coded_width * avctx->coded_height / 4;
1083 ctx->texture_block_h = 4;
1084 ctx->texture_block_w = 16;
1085 ctx->ctexture_block_h = 4;
1086 ctx->ctexture_block_w = 4;
1087 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
1088 avctx->colorspace = AVCOL_SPC_YCOCG;
1090 case MKBETAG('Y', 'G', '1', '0'):
1091 decompress_tex = dxv_decompress_yg10;
1092 ctx->tex_funct_planar[0] = yao_block;
1093 ctx->tex_funct_planar[1] = cocg_block;
1096 ctx->ctex_step = 16;
1097 msgcomp = "YAOCOCG10";
1099 ctx->ctex_size = avctx->coded_width * avctx->coded_height / 4;
1100 ctx->texture_block_h = 4;
1101 ctx->texture_block_w = 16;
1102 ctx->ctexture_block_h = 4;
1103 ctx->ctexture_block_w = 4;
1104 avctx->pix_fmt = AV_PIX_FMT_YUVA420P;
1105 avctx->colorspace = AVCOL_SPC_YCOCG;
1108 /* Old version does not have a real header, just size and type. */
1109 size = tag & 0x00FFFFFF;
1110 old_type = tag >> 24;
1111 version_major = (old_type & 0x0F) - 1;
1113 if (old_type & 0x80) {
1115 decompress_tex = dxv_decompress_raw;
1118 decompress_tex = dxv_decompress_lzf;
1121 if (old_type & 0x40) {
1124 ctx->tex_funct = ctx->texdsp.dxt5_block;
1126 } else if (old_type & 0x20 || version_major == 1) {
1129 ctx->tex_funct = ctx->texdsp.dxt1_block;
1132 av_log(avctx, AV_LOG_ERROR, "Unsupported header (0x%08"PRIX32")\n.", tag);
1133 return AVERROR_INVALIDDATA;
1139 ctx->slice_count = av_clip(avctx->thread_count, 1,
1140 avctx->coded_height / FFMAX(ctx->texture_block_h,
1141 ctx->ctexture_block_h));
1143 /* New header is 12 bytes long. */
1145 version_major = bytestream2_get_byte(gbc) - 1;
1146 version_minor = bytestream2_get_byte(gbc);
1148 /* Encoder copies texture data when compression is not advantageous. */
1149 if (bytestream2_get_byte(gbc)) {
1152 decompress_tex = dxv_decompress_raw;
1155 bytestream2_skip(gbc, 1); // unknown
1156 size = bytestream2_get_le32(gbc);
1158 av_log(avctx, AV_LOG_DEBUG,
1159 "%s compression with %s texture (version %d.%d)\n",
1160 msgcomp, msgtext, version_major, version_minor);
1162 if (size != bytestream2_get_bytes_left(gbc)) {
1163 av_log(avctx, AV_LOG_ERROR,
1164 "Incomplete or invalid file (header %d, left %u).\n",
1165 size, bytestream2_get_bytes_left(gbc));
1166 return AVERROR_INVALIDDATA;
1169 ctx->tex_size = avctx->coded_width * avctx->coded_height * 4 / ctx->tex_rat;
1170 ret = av_reallocp(&ctx->tex_data, ctx->tex_size + AV_INPUT_BUFFER_PADDING_SIZE);
1174 if (ctx->ctex_size) {
1177 ctx->op_size[0] = avctx->coded_width * avctx->coded_height / 16;
1178 ctx->op_size[1] = avctx->coded_width * avctx->coded_height / 32;
1179 ctx->op_size[2] = avctx->coded_width * avctx->coded_height / 32;
1180 ctx->op_size[3] = avctx->coded_width * avctx->coded_height / 16;
1182 ret = av_reallocp(&ctx->ctex_data, ctx->ctex_size + AV_INPUT_BUFFER_PADDING_SIZE);
1185 for (i = 0; i < 4; i++) {
1186 ret = av_reallocp(&ctx->op_data[i], ctx->op_size[i]);
1192 /* Decompress texture out of the intermediate compression. */
1193 ret = decompress_tex(avctx);
1197 int w_block = avctx->coded_width / ctx->texture_block_w;
1198 int h_block = avctx->coded_height / ctx->texture_block_h;
1199 if (w_block * h_block * ctx->tex_step > ctx->tex_size * 8LL)
1200 return AVERROR_INVALIDDATA;
1204 ret = ff_thread_get_buffer(avctx, &tframe, 0);
1208 /* Now decompress the texture with the standard functions. */
1209 avctx->execute2(avctx, decompress_texture_thread,
1210 tframe.f, NULL, ctx->slice_count);
1212 /* Frame is ready to be output. */
1213 tframe.f->pict_type = AV_PICTURE_TYPE_I;
1214 tframe.f->key_frame = 1;
1220 static int dxv_init(AVCodecContext *avctx)
1222 DXVContext *ctx = avctx->priv_data;
1223 int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
1226 av_log(avctx, AV_LOG_ERROR, "Invalid image size %dx%d.\n",
1227 avctx->width, avctx->height);
1231 /* Codec requires 16x16 alignment. */
1232 avctx->coded_width = FFALIGN(avctx->width, 16);
1233 avctx->coded_height = FFALIGN(avctx->height, 16);
1235 ff_texturedsp_init(&ctx->texdsp);
1240 static int dxv_close(AVCodecContext *avctx)
1242 DXVContext *ctx = avctx->priv_data;
1244 av_freep(&ctx->tex_data);
1245 av_freep(&ctx->ctex_data);
1246 av_freep(&ctx->op_data[0]);
1247 av_freep(&ctx->op_data[1]);
1248 av_freep(&ctx->op_data[2]);
1249 av_freep(&ctx->op_data[3]);
1254 AVCodec ff_dxv_decoder = {
1256 .long_name = NULL_IF_CONFIG_SMALL("Resolume DXV"),
1257 .type = AVMEDIA_TYPE_VIDEO,
1258 .id = AV_CODEC_ID_DXV,
1260 .decode = dxv_decode,
1262 .priv_data_size = sizeof(DXVContext),
1263 .capabilities = AV_CODEC_CAP_DR1 |
1264 AV_CODEC_CAP_SLICE_THREADS |
1265 AV_CODEC_CAP_FRAME_THREADS,
1266 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
1267 FF_CODEC_CAP_INIT_CLEANUP,