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 if (bytestream2_get_bytes_left(gbc) < 4) \
260 return AVERROR_INVALIDDATA; \
261 value = bytestream2_get_le32(gbc); \
272 idx = (bytestream2_get_byte(gbc) + 2) * x; \
274 av_log(avctx, AV_LOG_ERROR, "idx %d > %d\n", idx, pos); \
275 return AVERROR_INVALIDDATA; \
279 idx = (bytestream2_get_le16(gbc) + 0x102) * x; \
281 av_log(avctx, AV_LOG_ERROR, "idx %d > %d\n", idx, pos); \
282 return AVERROR_INVALIDDATA; \
288 static int dxv_decompress_dxt1(AVCodecContext *avctx)
290 DXVContext *ctx = avctx->priv_data;
291 GetByteContext *gbc = &ctx->gbc;
292 uint32_t value, prev, op;
293 int idx = 0, state = 0;
296 /* Copy the first two elements */
297 AV_WL32(ctx->tex_data, bytestream2_get_le32(gbc));
298 AV_WL32(ctx->tex_data + 4, bytestream2_get_le32(gbc));
300 /* Process input until the whole texture has been filled */
301 while (pos + 2 <= ctx->tex_size / 4) {
304 /* Copy two elements from a previous offset or from the input buffer */
306 prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
307 AV_WL32(ctx->tex_data + 4 * pos, prev);
310 prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
311 AV_WL32(ctx->tex_data + 4 * pos, prev);
317 prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
319 prev = bytestream2_get_le32(gbc);
320 AV_WL32(ctx->tex_data + 4 * pos, prev);
326 prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
328 prev = bytestream2_get_le32(gbc);
329 AV_WL32(ctx->tex_data + 4 * pos, prev);
337 typedef struct OpcodeTable {
343 static int fill_ltable(GetByteContext *gb, uint32_t *table, int *nb_elements)
345 unsigned half = 512, bits = 1023, left = 1024, input, mask;
346 int value, counter = 0, rshift = 10, lshift = 30;
348 mask = bytestream2_get_le32(gb) >> 2;
351 return AVERROR_INVALIDDATA;
356 table[counter++] = value;
358 if (bytestream2_get_bytes_left(gb) <= 0)
359 return AVERROR_INVALIDDATA;
361 input = bytestream2_get_le16(gb);
362 mask += input << lshift;
372 for (; !table[counter - 1]; counter--)
374 return AVERROR_INVALIDDATA;
376 *nb_elements = counter;
379 memset(&table[counter], 0, 4 * (256 - counter));
382 bytestream2_seek(gb, -2, SEEK_CUR);
387 static int fill_optable(unsigned *table0, OpcodeTable *table1, int nb_elements)
389 unsigned table2[256] = { 0 };
391 int val0, val1, i, j = 2, k = 0;
393 table2[0] = table0[0];
394 for (i = 0; i < nb_elements - 1; i++, table2[i] = val0) {
395 val0 = table0[i + 1] + table2[i];
401 } while (!table2[k]);
405 for (i = 1024; i > 0; i--) {
406 for (table1[x].val1 = k; k < 256 && j > table2[k]; k++);
407 x = (x - 383) & 0x3FF;
412 memcpy(&table2[0], table0, 4 * nb_elements);
414 for (i = 0; i < 1024; i++) {
415 val0 = table1[i].val1;
418 x = 31 - ff_clz(val1);
420 return AVERROR_INVALIDDATA;
421 table1[i].val2 = 10 - x;
422 table1[i].next = (val1 << table1[i].val2) - 1024;
428 static int get_opcodes(GetByteContext *gb, uint32_t *table, uint8_t *dst, int op_size, int nb_elements)
430 OpcodeTable optable[1024];
431 int sum, x, val, lshift, rshift, ret, i, idx;
432 int64_t size_in_bits;
433 unsigned endoffset, newoffset, offset;
435 uint8_t *src = (uint8_t *)gb->buffer;
437 ret = fill_optable(table, optable, nb_elements);
441 size_in_bits = bytestream2_get_le32(gb);
442 endoffset = ((size_in_bits + 7) >> 3) - 4;
443 if (endoffset <= 0 || bytestream2_get_bytes_left(gb) < endoffset)
444 return AVERROR_INVALIDDATA;
447 next = AV_RL32(src + endoffset);
448 rshift = (((size_in_bits & 0xFF) - 1) & 7) + 15;
449 lshift = 32 - rshift;
450 idx = (next >> rshift) & 0x3FF;
451 for (i = 0; i < op_size; i++) {
452 dst[i] = optable[idx].val1;
453 val = optable[idx].val2;
455 x = (next << lshift) >> 1 >> (31 - val);
456 newoffset = offset - (sum >> 3);
458 idx = x + optable[idx].next;
460 if (offset > endoffset)
461 return AVERROR_INVALIDDATA;
462 next = AV_RL32(src + offset);
465 bytestream2_skip(gb, (size_in_bits + 7 >> 3) - 4);
470 static int dxv_decompress_opcodes(GetByteContext *gb, void *dstp, size_t op_size)
472 int pos = bytestream2_tell(gb);
473 int flag = bytestream2_peek_byte(gb);
475 if ((flag & 3) == 0) {
476 bytestream2_skip(gb, 1);
477 bytestream2_get_buffer(gb, dstp, op_size);
478 } else if ((flag & 3) == 1) {
479 bytestream2_skip(gb, 1);
480 memset(dstp, bytestream2_get_byte(gb), op_size);
483 int ret, elements = 0;
485 ret = fill_ltable(gb, table, &elements);
488 ret = get_opcodes(gb, table, dstp, op_size, elements);
492 return bytestream2_tell(gb) - pos;
495 static int dxv_decompress_cgo(DXVContext *ctx, GetByteContext *gb,
496 uint8_t *tex_data, int tex_size,
497 uint8_t *op_data, int *oindex,
499 uint8_t **dstp, int *statep,
500 uint8_t **tab0, uint8_t **tab1,
503 uint8_t *dst = *dstp;
504 uint8_t *tptr0, *tptr1, *tptr3;
511 return AVERROR_INVALIDDATA;
512 opcode = op_data[oi++];
514 v = bytestream2_get_byte(gb);
517 if (bytestream2_get_bytes_left(gb) <= 0)
518 return AVERROR_INVALIDDATA;
519 opcode = bytestream2_get_le16(gb);
521 } while (opcode == 0xFFFF);
523 AV_WL32(dst, AV_RL32(dst - (8 + offset)));
524 AV_WL32(dst + 4, AV_RL32(dst - (4 + offset)));
531 AV_WL32(dst, AV_RL32(dst - (8 + offset)));
532 AV_WL32(dst + 4, AV_RL32(dst - (4 + offset)));
535 vv = (8 + offset) * (bytestream2_get_le16(gb) + 1);
536 if (vv < 0 || vv > dst - tex_data)
537 return AVERROR_INVALIDDATA;
540 AV_WL32(dst, AV_RL32(tptr0));
541 AV_WL32(dst + 4, AV_RL32(tptr0 + 4));
542 tab0[0x9E3779B1 * (uint16_t)v >> 24] = dst;
543 tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
546 AV_WL32(dst, bytestream2_get_le32(gb));
547 AV_WL32(dst + 4, bytestream2_get_le32(gb));
548 tab0[0x9E3779B1 * AV_RL16(dst) >> 24] = dst;
549 tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
552 tptr3 = tab1[bytestream2_get_byte(gb)];
554 return AVERROR_INVALIDDATA;
555 AV_WL16(dst, bytestream2_get_le16(gb));
556 AV_WL16(dst + 2, AV_RL16(tptr3));
558 AV_WL16(dst + 5, bytestream2_get_le16(gb));
559 dst[7] = bytestream2_get_byte(gb);
560 tab0[0x9E3779B1 * AV_RL16(dst) >> 24] = dst;
563 tptr3 = tab1[bytestream2_get_byte(gb)];
565 return AVERROR_INVALIDDATA;
566 AV_WL16(dst, bytestream2_get_le16(gb));
567 AV_WL16(dst + 2, bytestream2_get_le16(gb));
568 dst[4] = bytestream2_get_byte(gb);
569 AV_WL16(dst + 5, AV_RL16(tptr3));
571 tab0[0x9E3779B1 * AV_RL16(dst) >> 24] = dst;
572 tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
575 tptr0 = tab1[bytestream2_get_byte(gb)];
577 return AVERROR_INVALIDDATA;
578 tptr1 = tab1[bytestream2_get_byte(gb)];
580 return AVERROR_INVALIDDATA;
581 AV_WL16(dst, bytestream2_get_le16(gb));
582 AV_WL16(dst + 2, AV_RL16(tptr0));
584 AV_WL16(dst + 5, AV_RL16(tptr1));
586 tab0[0x9E3779B1 * AV_RL16(dst) >> 24] = dst;
589 v = (8 + offset) * (bytestream2_get_le16(gb) + 1);
590 if (v < 0 || v > dst - tex_data)
591 return AVERROR_INVALIDDATA;
593 AV_WL16(dst, bytestream2_get_le16(gb));
594 AV_WL16(dst + 2, AV_RL16(tptr0 + 2));
595 AV_WL32(dst + 4, AV_RL32(tptr0 + 4));
596 tab0[0x9E3779B1 * AV_RL16(dst) >> 24] = dst;
597 tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
600 tptr1 = tab0[bytestream2_get_byte(gb)];
602 return AVERROR_INVALIDDATA;
603 AV_WL16(dst, AV_RL16(tptr1));
604 AV_WL16(dst + 2, bytestream2_get_le16(gb));
605 AV_WL32(dst + 4, bytestream2_get_le32(gb));
606 tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
609 tptr1 = tab0[bytestream2_get_byte(gb)];
611 return AVERROR_INVALIDDATA;
612 tptr3 = tab1[bytestream2_get_byte(gb)];
614 return AVERROR_INVALIDDATA;
615 AV_WL16(dst, AV_RL16(tptr1));
616 AV_WL16(dst + 2, AV_RL16(tptr3));
618 AV_WL16(dst + 5, bytestream2_get_le16(gb));
619 dst[7] = bytestream2_get_byte(gb);
620 tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
623 tptr1 = tab0[bytestream2_get_byte(gb)];
625 return AVERROR_INVALIDDATA;
626 tptr3 = tab1[bytestream2_get_byte(gb)];
628 return AVERROR_INVALIDDATA;
629 AV_WL16(dst, AV_RL16(tptr1));
630 AV_WL16(dst + 2, bytestream2_get_le16(gb));
631 dst[4] = bytestream2_get_byte(gb);
632 AV_WL16(dst + 5, AV_RL16(tptr3));
634 tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
637 tptr0 = tab0[bytestream2_get_byte(gb)];
639 return AVERROR_INVALIDDATA;
640 tptr3 = tab1[bytestream2_get_byte(gb)];
642 return AVERROR_INVALIDDATA;
643 tptr1 = tab1[bytestream2_get_byte(gb)];
645 return AVERROR_INVALIDDATA;
646 AV_WL16(dst, AV_RL16(tptr0));
647 AV_WL16(dst + 2, AV_RL16(tptr3));
649 AV_WL16(dst + 5, AV_RL16(tptr1));
653 tptr1 = tab0[bytestream2_get_byte(gb)];
655 return AVERROR_INVALIDDATA;
656 v = (8 + offset) * (bytestream2_get_le16(gb) + 1);
657 if (v < 0 || v > dst - tex_data)
658 return AVERROR_INVALIDDATA;
660 AV_WL16(dst, AV_RL16(tptr1));
661 AV_WL16(dst + 2, AV_RL16(tptr0 + 2));
662 AV_WL32(dst + 4, AV_RL32(tptr0 + 4));
663 tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
666 AV_WL16(dst, AV_RL16(dst - (8 + offset)));
667 AV_WL16(dst + 2, bytestream2_get_le16(gb));
668 AV_WL32(dst + 4, bytestream2_get_le32(gb));
669 tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
672 tptr3 = tab1[bytestream2_get_byte(gb)];
674 return AVERROR_INVALIDDATA;
675 AV_WL16(dst, AV_RL16(dst - (8 + offset)));
676 AV_WL16(dst + 2, AV_RL16(tptr3));
678 AV_WL16(dst + 5, bytestream2_get_le16(gb));
679 dst[7] = bytestream2_get_byte(gb);
680 tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
683 tptr3 = tab1[bytestream2_get_byte(gb)];
685 return AVERROR_INVALIDDATA;
686 AV_WL16(dst, AV_RL16(dst - (8 + offset)));
687 AV_WL16(dst + 2, bytestream2_get_le16(gb));
688 dst[4] = bytestream2_get_byte(gb);
689 AV_WL16(dst + 5, AV_RL16(tptr3));
691 tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
694 tptr3 = tab1[bytestream2_get_byte(gb)];
696 return AVERROR_INVALIDDATA;
697 tptr1 = tab1[bytestream2_get_byte(gb)];
699 return AVERROR_INVALIDDATA;
700 AV_WL16(dst, AV_RL16(dst - (8 + offset)));
701 AV_WL16(dst + 2, AV_RL16(tptr3));
703 AV_WL16(dst + 5, AV_RL16(tptr1));
707 v = (8 + offset) * (bytestream2_get_le16(gb) + 1);
708 if (v < 0 || v > dst - tex_data)
709 return AVERROR_INVALIDDATA;
710 AV_WL16(dst, AV_RL16(dst - (8 + offset)));
711 AV_WL16(dst + 2, AV_RL16(&dst[-v + 2]));
712 AV_WL32(dst + 4, AV_RL32(&dst[-v + 4]));
713 tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
720 AV_WL32(dst, AV_RL32(dst - (8 + offset)));
721 AV_WL32(dst + 4, AV_RL32(dst - (4 + offset)));
724 if (dst - tex_data + 8 > tex_size)
725 return AVERROR_INVALIDDATA;
735 static int dxv_decompress_cocg(DXVContext *ctx, GetByteContext *gb,
736 uint8_t *tex_data, int tex_size,
737 uint8_t *op_data0, uint8_t *op_data1,
738 int max_op_size0, int max_op_size1)
740 uint8_t *dst, *tab2[256] = { 0 }, *tab0[256] = { 0 }, *tab3[256] = { 0 }, *tab1[256] = { 0 };
741 int op_offset = bytestream2_get_le32(gb);
742 unsigned op_size0 = bytestream2_get_le32(gb);
743 unsigned op_size1 = bytestream2_get_le32(gb);
744 int data_start = bytestream2_tell(gb);
745 int skip0, skip1, oi0 = 0, oi1 = 0;
746 int ret, state0 = 0, state1 = 0;
748 if (op_offset < 12 || op_offset - 12 > bytestream2_get_bytes_left(gb))
749 return AVERROR_INVALIDDATA;
752 bytestream2_skip(gb, op_offset - 12);
753 if (op_size0 > max_op_size0)
754 return AVERROR_INVALIDDATA;
755 skip0 = dxv_decompress_opcodes(gb, op_data0, op_size0);
758 if (op_size1 > max_op_size1)
759 return AVERROR_INVALIDDATA;
760 skip1 = dxv_decompress_opcodes(gb, op_data1, op_size1);
763 bytestream2_seek(gb, data_start, SEEK_SET);
765 AV_WL32(dst, bytestream2_get_le32(gb));
766 AV_WL32(dst + 4, bytestream2_get_le32(gb));
767 AV_WL32(dst + 8, bytestream2_get_le32(gb));
768 AV_WL32(dst + 12, bytestream2_get_le32(gb));
770 tab0[0x9E3779B1 * AV_RL16(dst) >> 24] = dst;
771 tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFF) >> 24] = dst + 2;
772 tab2[0x9E3779B1 * AV_RL16(dst + 8) >> 24] = dst + 8;
773 tab3[0x9E3779B1 * (AV_RL32(dst + 10) & 0xFFFFFF) >> 24] = dst + 10;
775 while (dst + 10 < tex_data + tex_size) {
776 ret = dxv_decompress_cgo(ctx, gb, tex_data, tex_size, op_data0, &oi0, op_size0,
777 &dst, &state0, tab0, tab1, 8);
780 ret = dxv_decompress_cgo(ctx, gb, tex_data, tex_size, op_data1, &oi1, op_size1,
781 &dst, &state1, tab2, tab3, 8);
786 bytestream2_seek(gb, data_start - 12 + op_offset + skip0 + skip1, SEEK_SET);
791 static int dxv_decompress_yo(DXVContext *ctx, GetByteContext *gb,
792 uint8_t *tex_data, int tex_size,
793 uint8_t *op_data, int max_op_size)
795 int op_offset = bytestream2_get_le32(gb);
796 unsigned op_size = bytestream2_get_le32(gb);
797 int data_start = bytestream2_tell(gb);
798 uint8_t *dst, *table0[256] = { 0 }, *table1[256] = { 0 };
799 int ret, state = 0, skip, oi = 0, v, vv;
801 if (op_offset < 8 || op_offset - 8 > bytestream2_get_bytes_left(gb))
802 return AVERROR_INVALIDDATA;
805 bytestream2_skip(gb, op_offset - 8);
806 if (op_size > max_op_size)
807 return AVERROR_INVALIDDATA;
808 skip = dxv_decompress_opcodes(gb, op_data, op_size);
811 bytestream2_seek(gb, data_start, SEEK_SET);
813 v = bytestream2_get_le32(gb);
815 vv = bytestream2_get_le32(gb);
816 table0[0x9E3779B1 * (uint16_t)v >> 24] = dst;
817 AV_WL32(dst + 4, vv);
818 table1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFF) >> 24] = dst + 2;
821 while (dst < tex_data + tex_size) {
822 ret = dxv_decompress_cgo(ctx, gb, tex_data, tex_size, op_data, &oi, op_size,
823 &dst, &state, table0, table1, 0);
828 bytestream2_seek(gb, data_start + op_offset + skip - 8, SEEK_SET);
833 static int dxv_decompress_ycg6(AVCodecContext *avctx)
835 DXVContext *ctx = avctx->priv_data;
836 GetByteContext *gb = &ctx->gbc;
839 ret = dxv_decompress_yo(ctx, gb, ctx->tex_data, ctx->tex_size,
840 ctx->op_data[0], ctx->op_size[0]);
844 return dxv_decompress_cocg(ctx, gb, ctx->ctex_data, ctx->ctex_size,
845 ctx->op_data[1], ctx->op_data[2],
846 ctx->op_size[1], ctx->op_size[2]);
849 static int dxv_decompress_yg10(AVCodecContext *avctx)
851 DXVContext *ctx = avctx->priv_data;
852 GetByteContext *gb = &ctx->gbc;
855 ret = dxv_decompress_cocg(ctx, gb, ctx->tex_data, ctx->tex_size,
856 ctx->op_data[0], ctx->op_data[3],
857 ctx->op_size[0], ctx->op_size[3]);
861 return dxv_decompress_cocg(ctx, gb, ctx->ctex_data, ctx->ctex_size,
862 ctx->op_data[1], ctx->op_data[2],
863 ctx->op_size[1], ctx->op_size[2]);
866 static int dxv_decompress_dxt5(AVCodecContext *avctx)
868 DXVContext *ctx = avctx->priv_data;
869 GetByteContext *gbc = &ctx->gbc;
870 uint32_t value, op, prev;
876 /* Copy the first four elements */
877 AV_WL32(ctx->tex_data + 0, bytestream2_get_le32(gbc));
878 AV_WL32(ctx->tex_data + 4, bytestream2_get_le32(gbc));
879 AV_WL32(ctx->tex_data + 8, bytestream2_get_le32(gbc));
880 AV_WL32(ctx->tex_data + 12, bytestream2_get_le32(gbc));
882 /* Process input until the whole texture has been filled */
883 while (pos + 2 <= ctx->tex_size / 4) {
887 prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
888 AV_WL32(ctx->tex_data + 4 * pos, prev);
890 prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
891 AV_WL32(ctx->tex_data + 4 * pos, prev);
894 if (bytestream2_get_bytes_left(gbc) < 1)
895 return AVERROR_INVALIDDATA;
897 value = bytestream2_get_le32(gbc);
907 check = bytestream2_get_byte(gbc) + 1;
910 probe = bytestream2_get_le16(gbc);
912 } while (probe == 0xFFFF);
914 while (check && pos + 4 <= ctx->tex_size / 4) {
915 prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
916 AV_WL32(ctx->tex_data + 4 * pos, prev);
919 prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
920 AV_WL32(ctx->tex_data + 4 * pos, prev);
923 prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
924 AV_WL32(ctx->tex_data + 4 * pos, prev);
927 prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
928 AV_WL32(ctx->tex_data + 4 * pos, prev);
934 /* Restart (or exit) the loop */
938 /* Load new run value */
939 run = bytestream2_get_byte(gbc);
942 probe = bytestream2_get_le16(gbc);
944 } while (probe == 0xFFFF);
947 /* Copy two dwords from previous data */
948 prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
949 AV_WL32(ctx->tex_data + 4 * pos, prev);
952 prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
953 AV_WL32(ctx->tex_data + 4 * pos, prev);
957 /* Copy two dwords from a previous index */
958 idx = 8 + bytestream2_get_le16(gbc);
959 if (idx > pos || (unsigned int)(pos - idx) + 2 > ctx->tex_size / 4)
960 return AVERROR_INVALIDDATA;
961 prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
962 AV_WL32(ctx->tex_data + 4 * pos, prev);
965 prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
966 AV_WL32(ctx->tex_data + 4 * pos, prev);
970 /* Copy two dwords from input */
971 prev = bytestream2_get_le32(gbc);
972 AV_WL32(ctx->tex_data + 4 * pos, prev);
975 prev = bytestream2_get_le32(gbc);
976 AV_WL32(ctx->tex_data + 4 * pos, prev);
983 if (pos + 2 > ctx->tex_size / 4)
984 return AVERROR_INVALIDDATA;
986 /* Copy two elements from a previous offset or from the input buffer */
988 if (idx > pos || (unsigned int)(pos - idx) + 2 > ctx->tex_size / 4)
989 return AVERROR_INVALIDDATA;
990 prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
991 AV_WL32(ctx->tex_data + 4 * pos, prev);
994 prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
995 AV_WL32(ctx->tex_data + 4 * pos, prev);
1000 if (op && (idx > pos || (unsigned int)(pos - idx) + 2 > ctx->tex_size / 4))
1001 return AVERROR_INVALIDDATA;
1003 prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
1005 prev = bytestream2_get_le32(gbc);
1006 AV_WL32(ctx->tex_data + 4 * pos, prev);
1012 prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
1014 prev = bytestream2_get_le32(gbc);
1015 AV_WL32(ctx->tex_data + 4 * pos, prev);
1023 static int dxv_decompress_lzf(AVCodecContext *avctx)
1025 DXVContext *ctx = avctx->priv_data;
1026 return ff_lzf_uncompress(&ctx->gbc, &ctx->tex_data, &ctx->tex_size);
1029 static int dxv_decompress_raw(AVCodecContext *avctx)
1031 DXVContext *ctx = avctx->priv_data;
1032 GetByteContext *gbc = &ctx->gbc;
1034 if (bytestream2_get_bytes_left(gbc) < ctx->tex_size)
1035 return AVERROR_INVALIDDATA;
1037 bytestream2_get_buffer(gbc, ctx->tex_data, ctx->tex_size);
1041 static int dxv_decode(AVCodecContext *avctx, void *data,
1042 int *got_frame, AVPacket *avpkt)
1044 DXVContext *ctx = avctx->priv_data;
1046 GetByteContext *gbc = &ctx->gbc;
1047 int (*decompress_tex)(AVCodecContext *avctx);
1048 const char *msgcomp, *msgtext;
1050 int version_major, version_minor = 0;
1051 int size = 0, old_type = 0;
1054 bytestream2_init(gbc, avpkt->data, avpkt->size);
1056 ctx->texture_block_h = 4;
1057 ctx->texture_block_w = 4;
1059 avctx->pix_fmt = AV_PIX_FMT_RGBA;
1060 avctx->colorspace = AVCOL_SPC_RGB;
1062 ctx->tex_funct = NULL;
1063 ctx->tex_funct_planar[0] = NULL;
1064 ctx->tex_funct_planar[1] = NULL;
1066 tag = bytestream2_get_le32(gbc);
1068 case MKBETAG('D', 'X', 'T', '1'):
1069 decompress_tex = dxv_decompress_dxt1;
1070 ctx->tex_funct = ctx->texdsp.dxt1_block;
1076 case MKBETAG('D', 'X', 'T', '5'):
1077 decompress_tex = dxv_decompress_dxt5;
1078 ctx->tex_funct = ctx->texdsp.dxt5_block;
1084 case MKBETAG('Y', 'C', 'G', '6'):
1085 decompress_tex = dxv_decompress_ycg6;
1086 ctx->tex_funct_planar[0] = yo_block;
1087 ctx->tex_funct_planar[1] = cocg_block;
1090 ctx->ctex_step = 16;
1091 msgcomp = "YOCOCG6";
1093 ctx->ctex_size = avctx->coded_width * avctx->coded_height / 4;
1094 ctx->texture_block_h = 4;
1095 ctx->texture_block_w = 16;
1096 ctx->ctexture_block_h = 4;
1097 ctx->ctexture_block_w = 4;
1098 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
1099 avctx->colorspace = AVCOL_SPC_YCOCG;
1101 case MKBETAG('Y', 'G', '1', '0'):
1102 decompress_tex = dxv_decompress_yg10;
1103 ctx->tex_funct_planar[0] = yao_block;
1104 ctx->tex_funct_planar[1] = cocg_block;
1107 ctx->ctex_step = 16;
1108 msgcomp = "YAOCOCG10";
1110 ctx->ctex_size = avctx->coded_width * avctx->coded_height / 4;
1111 ctx->texture_block_h = 4;
1112 ctx->texture_block_w = 16;
1113 ctx->ctexture_block_h = 4;
1114 ctx->ctexture_block_w = 4;
1115 avctx->pix_fmt = AV_PIX_FMT_YUVA420P;
1116 avctx->colorspace = AVCOL_SPC_YCOCG;
1119 /* Old version does not have a real header, just size and type. */
1120 size = tag & 0x00FFFFFF;
1121 old_type = tag >> 24;
1122 version_major = (old_type & 0x0F) - 1;
1124 if (old_type & 0x80) {
1126 decompress_tex = dxv_decompress_raw;
1129 decompress_tex = dxv_decompress_lzf;
1132 if (old_type & 0x40) {
1135 ctx->tex_funct = ctx->texdsp.dxt5_block;
1137 } else if (old_type & 0x20 || version_major == 1) {
1140 ctx->tex_funct = ctx->texdsp.dxt1_block;
1143 av_log(avctx, AV_LOG_ERROR, "Unsupported header (0x%08"PRIX32")\n.", tag);
1144 return AVERROR_INVALIDDATA;
1150 ctx->slice_count = av_clip(avctx->thread_count, 1,
1151 avctx->coded_height / FFMAX(ctx->texture_block_h,
1152 ctx->ctexture_block_h));
1154 /* New header is 12 bytes long. */
1156 version_major = bytestream2_get_byte(gbc) - 1;
1157 version_minor = bytestream2_get_byte(gbc);
1159 /* Encoder copies texture data when compression is not advantageous. */
1160 if (bytestream2_get_byte(gbc)) {
1163 decompress_tex = dxv_decompress_raw;
1166 bytestream2_skip(gbc, 1); // unknown
1167 size = bytestream2_get_le32(gbc);
1169 av_log(avctx, AV_LOG_DEBUG,
1170 "%s compression with %s texture (version %d.%d)\n",
1171 msgcomp, msgtext, version_major, version_minor);
1173 if (size != bytestream2_get_bytes_left(gbc)) {
1174 av_log(avctx, AV_LOG_ERROR,
1175 "Incomplete or invalid file (header %d, left %u).\n",
1176 size, bytestream2_get_bytes_left(gbc));
1177 return AVERROR_INVALIDDATA;
1180 ctx->tex_size = avctx->coded_width * avctx->coded_height * 4 / ctx->tex_rat;
1181 ret = av_reallocp(&ctx->tex_data, ctx->tex_size + AV_INPUT_BUFFER_PADDING_SIZE);
1185 if (ctx->ctex_size) {
1188 ctx->op_size[0] = avctx->coded_width * avctx->coded_height / 16;
1189 ctx->op_size[1] = avctx->coded_width * avctx->coded_height / 32;
1190 ctx->op_size[2] = avctx->coded_width * avctx->coded_height / 32;
1191 ctx->op_size[3] = avctx->coded_width * avctx->coded_height / 16;
1193 ret = av_reallocp(&ctx->ctex_data, ctx->ctex_size + AV_INPUT_BUFFER_PADDING_SIZE);
1196 for (i = 0; i < 4; i++) {
1197 ret = av_reallocp(&ctx->op_data[i], ctx->op_size[i]);
1203 /* Decompress texture out of the intermediate compression. */
1204 ret = decompress_tex(avctx);
1208 int w_block = avctx->coded_width / ctx->texture_block_w;
1209 int h_block = avctx->coded_height / ctx->texture_block_h;
1210 if (w_block * h_block * ctx->tex_step > ctx->tex_size * 8LL)
1211 return AVERROR_INVALIDDATA;
1215 ret = ff_thread_get_buffer(avctx, &tframe, 0);
1219 /* Now decompress the texture with the standard functions. */
1220 avctx->execute2(avctx, decompress_texture_thread,
1221 tframe.f, NULL, ctx->slice_count);
1223 /* Frame is ready to be output. */
1224 tframe.f->pict_type = AV_PICTURE_TYPE_I;
1225 tframe.f->key_frame = 1;
1231 static int dxv_init(AVCodecContext *avctx)
1233 DXVContext *ctx = avctx->priv_data;
1234 int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
1237 av_log(avctx, AV_LOG_ERROR, "Invalid image size %dx%d.\n",
1238 avctx->width, avctx->height);
1242 /* Codec requires 16x16 alignment. */
1243 avctx->coded_width = FFALIGN(avctx->width, 16);
1244 avctx->coded_height = FFALIGN(avctx->height, 16);
1246 ff_texturedsp_init(&ctx->texdsp);
1251 static int dxv_close(AVCodecContext *avctx)
1253 DXVContext *ctx = avctx->priv_data;
1255 av_freep(&ctx->tex_data);
1256 av_freep(&ctx->ctex_data);
1257 av_freep(&ctx->op_data[0]);
1258 av_freep(&ctx->op_data[1]);
1259 av_freep(&ctx->op_data[2]);
1260 av_freep(&ctx->op_data[3]);
1265 AVCodec ff_dxv_decoder = {
1267 .long_name = NULL_IF_CONFIG_SMALL("Resolume DXV"),
1268 .type = AVMEDIA_TYPE_VIDEO,
1269 .id = AV_CODEC_ID_DXV,
1271 .decode = dxv_decode,
1273 .priv_data_size = sizeof(DXVContext),
1274 .capabilities = AV_CODEC_CAP_DR1 |
1275 AV_CODEC_CAP_SLICE_THREADS |
1276 AV_CODEC_CAP_FRAME_THREADS,
1277 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
1278 FF_CODEC_CAP_INIT_CLEANUP,