FFmpeg  4.1.4
dxv.c
Go to the documentation of this file.
1 /*
2  * Resolume DXV decoder
3  * Copyright (C) 2015 Vittorio Giovara <vittorio.giovara@gmail.com>
4  * Copyright (C) 2018 Paul B Mahol
5  *
6  * This file is part of FFmpeg.
7  *
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.
12  *
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.
17  *
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
21  */
22 
23 #include <stdint.h>
24 
25 #include "libavutil/imgutils.h"
26 
27 #include "mathops.h"
28 #include "avcodec.h"
29 #include "bytestream.h"
30 #include "internal.h"
31 #include "lzf.h"
32 #include "texturedsp.h"
33 #include "thread.h"
34 
35 typedef struct DXVContext {
38 
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
46 
47  /* Optimal number of slices for parallel decoding */
49 
50  uint8_t *op_data[4]; // Opcodes
51  int64_t op_size[4]; // Opcodes size
52 
55 
58 
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);
64 } DXVContext;
65 
66 static void decompress_indices(uint8_t *dst, const uint8_t *src)
67 {
68  int block, i;
69 
70  for (block = 0; block < 2; block++) {
71  int tmp = AV_RL24(src);
72 
73  /* Unpack 8x3 bit from last 3 byte block */
74  for (i = 0; i < 8; i++)
75  dst[i] = (tmp >> (i * 3)) & 0x7;
76 
77  src += 3;
78  dst += 8;
79  }
80 }
81 
82 static int extract_component(int yo0, int yo1, int code)
83 {
84  int yo;
85 
86  if (yo0 == yo1) {
87  yo = yo0;
88  } else if (code == 0) {
89  yo = yo0;
90  } else if (code == 1) {
91  yo = yo1;
92  } else {
93  if (yo0 > yo1) {
94  yo = (uint8_t) (((8 - code) * yo0 +
95  (code - 1) * yo1) / 7);
96  } else {
97  if (code == 6) {
98  yo = 0;
99  } else if (code == 7) {
100  yo = 255;
101  } else {
102  yo = (uint8_t) (((6 - code) * yo0 +
103  (code - 1) * yo1) / 5);
104  }
105  }
106  }
107 
108  return yo;
109 }
110 
111 static int cocg_block(uint8_t *plane0, ptrdiff_t stride0,
112  uint8_t *plane1, ptrdiff_t stride1,
113  const uint8_t *block)
114 {
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);
121  int x, y;
122 
123  decompress_indices(co_indices, block + 2);
124  decompress_indices(cg_indices, block + 10);
125 
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];
130 
131  plane0[x] = extract_component(cg0, cg1, cg_code);
132  plane1[x] = extract_component(co0, co1, co_code);
133  }
134  plane0 += stride0;
135  plane1 += stride1;
136  }
137 
138  return 16;
139 }
140 
141 static void yao_subblock(uint8_t *dst, uint8_t *yo_indices,
142  ptrdiff_t stride, const uint8_t *block)
143 {
144  uint8_t yo0 = *(block);
145  uint8_t yo1 = *(block + 1);
146  int x, y;
147 
148  decompress_indices(yo_indices, block + 2);
149 
150  for (y = 0; y < 4; y++) {
151  for (x = 0; x < 4; x++) {
152  int yo_code = yo_indices[x + y * 4];
153 
154  dst[x] = extract_component(yo0, yo1, yo_code);
155  }
156  dst += stride;
157  }
158 }
159 
160 static int yo_block(uint8_t *dst, ptrdiff_t stride,
161  uint8_t *unused0, ptrdiff_t unused1,
162  const uint8_t *block)
163 {
164  uint8_t yo_indices[16];
165 
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);
170 
171  return 32;
172 }
173 
174 static int yao_block(uint8_t *plane0, ptrdiff_t stride0,
175  uint8_t *plane3, ptrdiff_t stride1,
176  const uint8_t *block)
177 {
178  uint8_t yo_indices[16];
179  uint8_t a_indices[16];
180 
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);
189 
190  return 64;
191 }
192 
194  int slice, int thread_nb)
195 {
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;
201  int x, y;
202  int start_slice, end_slice;
203 
204  start_slice = h_block * slice / ctx->slice_count;
205  end_slice = h_block * (slice + 1) / ctx->slice_count;
206 
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);
214  }
215  }
216  } else {
217  const uint8_t *c = ctx->ctex_data;
218 
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);
227  }
228  }
229 
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;
234 
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);
243  }
244  }
245  }
246 
247  return 0;
248 }
249 
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) \
257  do { \
258  if (state == 0) { \
259  value = bytestream2_get_le32(gbc); \
260  state = 16; \
261  } \
262  op = value & 0x3; \
263  value >>= 2; \
264  state--; \
265  switch (op) { \
266  case 1: \
267  idx = x; \
268  break; \
269  case 2: \
270  idx = (bytestream2_get_byte(gbc) + 2) * x; \
271  if (idx > pos) { \
272  av_log(avctx, AV_LOG_ERROR, "idx %d > %d\n", idx, pos); \
273  return AVERROR_INVALIDDATA; \
274  } \
275  break; \
276  case 3: \
277  idx = (bytestream2_get_le16(gbc) + 0x102) * x; \
278  if (idx > pos) { \
279  av_log(avctx, AV_LOG_ERROR, "idx %d > %d\n", idx, pos); \
280  return AVERROR_INVALIDDATA; \
281  } \
282  break; \
283  } \
284  } while(0)
285 
287 {
288  DXVContext *ctx = avctx->priv_data;
289  GetByteContext *gbc = &ctx->gbc;
290  uint32_t value, prev, op;
291  int idx = 0, state = 0;
292  int pos = 2;
293 
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));
297 
298  /* Process input until the whole texture has been filled */
299  while (pos + 2 <= ctx->tex_size / 4) {
300  CHECKPOINT(2);
301 
302  /* Copy two elements from a previous offset or from the input buffer */
303  if (op) {
304  prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
305  AV_WL32(ctx->tex_data + 4 * pos, prev);
306  pos++;
307 
308  prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
309  AV_WL32(ctx->tex_data + 4 * pos, prev);
310  pos++;
311  } else {
312  CHECKPOINT(2);
313 
314  if (op)
315  prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
316  else
317  prev = bytestream2_get_le32(gbc);
318  AV_WL32(ctx->tex_data + 4 * pos, prev);
319  pos++;
320 
321  CHECKPOINT(2);
322 
323  if (op)
324  prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
325  else
326  prev = bytestream2_get_le32(gbc);
327  AV_WL32(ctx->tex_data + 4 * pos, prev);
328  pos++;
329  }
330  }
331 
332  return 0;
333 }
334 
335 typedef struct OpcodeTable {
336  int16_t next;
339 } OpcodeTable;
340 
341 static int fill_ltable(GetByteContext *gb, uint32_t *table, int *nb_elements)
342 {
343  unsigned half = 512, bits = 1023, left = 1024, input, mask;
344  int value, counter = 0, rshift = 10, lshift = 30;
345 
346  mask = bytestream2_get_le32(gb) >> 2;
347  while (left) {
348  if (counter >= 256)
349  return AVERROR_INVALIDDATA;
350  value = bits & mask;
351  left -= bits & mask;
352  mask >>= rshift;
353  lshift -= rshift;
354  table[counter++] = value;
355  if (lshift < 16) {
356  if (bytestream2_get_bytes_left(gb) <= 0)
357  return AVERROR_INVALIDDATA;
358 
359  input = bytestream2_get_le16(gb);
360  mask += input << lshift;
361  lshift += 16;
362  }
363  if (left < half) {
364  half >>= 1;
365  bits >>= 1;
366  rshift--;
367  }
368  }
369 
370  for (; !table[counter - 1]; counter--)
371  if (counter <= 0)
372  return AVERROR_INVALIDDATA;
373 
374  *nb_elements = counter;
375 
376  if (counter < 256)
377  memset(&table[counter], 0, 4 * (256 - counter));
378 
379  if (lshift >= 16)
380  bytestream2_seek(gb, -2, SEEK_CUR);
381 
382  return 0;
383 }
384 
385 static int fill_optable(unsigned *table0, OpcodeTable *table1, int nb_elements)
386 {
387  unsigned table2[256] = { 0 };
388  unsigned x = 0;
389  int val0, val1, i, j = 2, k = 0;
390 
391  table2[0] = table0[0];
392  for (i = 0; i < nb_elements - 1; i++, table2[i] = val0) {
393  val0 = table0[i + 1] + table2[i];
394  }
395 
396  if (!table2[0]) {
397  do {
398  k++;
399  } while (!table2[k]);
400  }
401 
402  j = 2;
403  for (i = 1024; i > 0; i--) {
404  for (table1[x].val1 = k; k < 256 && j > table2[k]; k++);
405  x = (x - 383) & 0x3FF;
406  j++;
407  }
408 
409  if (nb_elements > 0)
410  memcpy(&table2[0], table0, 4 * nb_elements);
411 
412  for (i = 0; i < 1024; i++) {
413  val0 = table1[i].val1;
414  val1 = table2[val0];
415  table2[val0]++;
416  x = 31 - ff_clz(val1);
417  if (x > 10)
418  return AVERROR_INVALIDDATA;
419  table1[i].val2 = 10 - x;
420  table1[i].next = (val1 << table1[i].val2) - 1024;
421  }
422 
423  return 0;
424 }
425 
426 static int get_opcodes(GetByteContext *gb, uint32_t *table, uint8_t *dst, int op_size, int nb_elements)
427 {
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;
432  unsigned next;
433  uint8_t *src = (uint8_t *)gb->buffer;
434 
435  ret = fill_optable(table, optable, nb_elements);
436  if (ret < 0)
437  return ret;
438 
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;
443 
444  offset = endoffset;
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;
452  sum = val + lshift;
453  x = (next << lshift) >> 1 >> (31 - val);
454  newoffset = offset - (sum >> 3);
455  lshift = sum & 7;
456  idx = x + optable[idx].next;
457  offset = newoffset;
458  if (offset > endoffset)
459  return AVERROR_INVALIDDATA;
460  next = AV_RL32(src + offset);
461  }
462 
463  bytestream2_skip(gb, (size_in_bits + 7 >> 3) - 4);
464 
465  return 0;
466 }
467 
468 static int dxv_decompress_opcodes(GetByteContext *gb, void *dstp, size_t op_size)
469 {
470  int pos = bytestream2_tell(gb);
471  int flag = bytestream2_peek_byte(gb);
472 
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);
479  } else {
480  uint32_t table[256];
481  int ret, elements = 0;
482 
483  ret = fill_ltable(gb, table, &elements);
484  if (ret < 0)
485  return ret;
486  ret = get_opcodes(gb, table, dstp, op_size, elements);
487  if (ret < 0)
488  return ret;
489  }
490  return bytestream2_tell(gb) - pos;
491 }
492 
494  uint8_t *tex_data, int tex_size,
495  uint8_t *op_data, int *oindex,
496  int op_size,
497  uint8_t **dstp, int *statep,
498  uint8_t **tab0, uint8_t **tab1,
499  int offset)
500 {
501  uint8_t *dst = *dstp;
502  uint8_t *tptr0, *tptr1, *tptr3;
503  int oi = *oindex;
504  int state = *statep;
505  int opcode, v, vv;
506 
507  if (state <= 0) {
508  if (oi >= op_size)
509  return AVERROR_INVALIDDATA;
510  opcode = op_data[oi++];
511  if (!opcode) {
512  v = bytestream2_get_byte(gb);
513  if (v == 255) {
514  do {
515  if (bytestream2_get_bytes_left(gb) <= 0)
516  return AVERROR_INVALIDDATA;
517  opcode = bytestream2_get_le16(gb);
518  v += opcode;
519  } while (opcode == 0xFFFF);
520  }
521  AV_WL32(dst, AV_RL32(dst - (8 + offset)));
522  AV_WL32(dst + 4, AV_RL32(dst - (4 + offset)));
523  state = v + 4;
524  goto done;
525  }
526 
527  switch (opcode) {
528  case 1:
529  AV_WL32(dst, AV_RL32(dst - (8 + offset)));
530  AV_WL32(dst + 4, AV_RL32(dst - (4 + offset)));
531  break;
532  case 2:
533  vv = (8 + offset) * (bytestream2_get_le16(gb) + 1);
534  if (vv < 0 || vv > dst - tex_data)
535  return AVERROR_INVALIDDATA;
536  tptr0 = dst - vv;
537  v = AV_RL32(tptr0);
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;
542  break;
543  case 3:
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;
548  break;
549  case 4:
550  tptr3 = tab1[bytestream2_get_byte(gb)];
551  if (!tptr3)
552  return AVERROR_INVALIDDATA;
553  AV_WL16(dst, bytestream2_get_le16(gb));
554  AV_WL16(dst + 2, AV_RL16(tptr3));
555  dst[4] = tptr3[2];
556  AV_WL16(dst + 5, bytestream2_get_le16(gb));
557  dst[7] = bytestream2_get_byte(gb);
558  tab0[0x9E3779B1 * AV_RL16(dst) >> 24] = dst;
559  break;
560  case 5:
561  tptr3 = tab1[bytestream2_get_byte(gb)];
562  if (!tptr3)
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));
568  dst[7] = tptr3[2];
569  tab0[0x9E3779B1 * AV_RL16(dst) >> 24] = dst;
570  tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
571  break;
572  case 6:
573  tptr0 = tab1[bytestream2_get_byte(gb)];
574  if (!tptr0)
575  return AVERROR_INVALIDDATA;
576  tptr1 = tab1[bytestream2_get_byte(gb)];
577  if (!tptr1)
578  return AVERROR_INVALIDDATA;
579  AV_WL16(dst, bytestream2_get_le16(gb));
580  AV_WL16(dst + 2, AV_RL16(tptr0));
581  dst[4] = tptr0[2];
582  AV_WL16(dst + 5, AV_RL16(tptr1));
583  dst[7] = tptr1[2];
584  tab0[0x9E3779B1 * AV_RL16(dst) >> 24] = dst;
585  break;
586  case 7:
587  v = (8 + offset) * (bytestream2_get_le16(gb) + 1);
588  if (v < 0 || v > dst - tex_data)
589  return AVERROR_INVALIDDATA;
590  tptr0 = dst - v;
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;
596  break;
597  case 8:
598  tptr1 = tab0[bytestream2_get_byte(gb)];
599  if (!tptr1)
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;
605  break;
606  case 9:
607  tptr1 = tab0[bytestream2_get_byte(gb)];
608  if (!tptr1)
609  return AVERROR_INVALIDDATA;
610  tptr3 = tab1[bytestream2_get_byte(gb)];
611  if (!tptr3)
612  return AVERROR_INVALIDDATA;
613  AV_WL16(dst, AV_RL16(tptr1));
614  AV_WL16(dst + 2, AV_RL16(tptr3));
615  dst[4] = tptr3[2];
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;
619  break;
620  case 10:
621  tptr1 = tab0[bytestream2_get_byte(gb)];
622  if (!tptr1)
623  return AVERROR_INVALIDDATA;
624  tptr3 = tab1[bytestream2_get_byte(gb)];
625  if (!tptr3)
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));
631  dst[7] = tptr3[2];
632  tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
633  break;
634  case 11:
635  tptr0 = tab0[bytestream2_get_byte(gb)];
636  if (!tptr0)
637  return AVERROR_INVALIDDATA;
638  tptr3 = tab1[bytestream2_get_byte(gb)];
639  if (!tptr3)
640  return AVERROR_INVALIDDATA;
641  tptr1 = tab1[bytestream2_get_byte(gb)];
642  if (!tptr1)
643  return AVERROR_INVALIDDATA;
644  AV_WL16(dst, AV_RL16(tptr0));
645  AV_WL16(dst + 2, AV_RL16(tptr3));
646  dst[4] = tptr3[2];
647  AV_WL16(dst + 5, AV_RL16(tptr1));
648  dst[7] = tptr1[2];
649  break;
650  case 12:
651  tptr1 = tab0[bytestream2_get_byte(gb)];
652  if (!tptr1)
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;
657  tptr0 = dst - v;
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;
662  break;
663  case 13:
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;
668  break;
669  case 14:
670  tptr3 = tab1[bytestream2_get_byte(gb)];
671  if (!tptr3)
672  return AVERROR_INVALIDDATA;
673  AV_WL16(dst, AV_RL16(dst - (8 + offset)));
674  AV_WL16(dst + 2, AV_RL16(tptr3));
675  dst[4] = tptr3[2];
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;
679  break;
680  case 15:
681  tptr3 = tab1[bytestream2_get_byte(gb)];
682  if (!tptr3)
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));
688  dst[7] = tptr3[2];
689  tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
690  break;
691  case 16:
692  tptr3 = tab1[bytestream2_get_byte(gb)];
693  if (!tptr3)
694  return AVERROR_INVALIDDATA;
695  tptr1 = tab1[bytestream2_get_byte(gb)];
696  if (!tptr1)
697  return AVERROR_INVALIDDATA;
698  AV_WL16(dst, AV_RL16(dst - (8 + offset)));
699  AV_WL16(dst + 2, AV_RL16(tptr3));
700  dst[4] = tptr3[2];
701  AV_WL16(dst + 5, AV_RL16(tptr1));
702  dst[7] = tptr1[2];
703  break;
704  case 17:
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;
712  break;
713  default:
714  break;
715  }
716  } else {
717 done:
718  AV_WL32(dst, AV_RL32(dst - (8 + offset)));
719  AV_WL32(dst + 4, AV_RL32(dst - (4 + offset)));
720  state--;
721  }
722  if (dst - tex_data + 8 > tex_size)
723  return AVERROR_INVALIDDATA;
724  dst += 8;
725 
726  *oindex = oi;
727  *dstp = dst;
728  *statep = state;
729 
730  return 0;
731 }
732 
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)
737 {
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;
745 
746  if (op_offset < 12)
747  return AVERROR_INVALIDDATA;
748 
749  dst = tex_data;
750  bytestream2_skip(gb, op_offset - 12);
751  if (op_size0 > max_op_size0)
752  return AVERROR_INVALIDDATA;
753  skip0 = dxv_decompress_opcodes(gb, op_data0, op_size0);
754  if (skip0 < 0)
755  return skip0;
756  bytestream2_seek(gb, data_start + op_offset + skip0 - 12, SEEK_SET);
757  if (op_size1 > max_op_size1)
758  return AVERROR_INVALIDDATA;
759  skip1 = dxv_decompress_opcodes(gb, op_data1, op_size1);
760  if (skip1 < 0)
761  return skip1;
762  bytestream2_seek(gb, data_start, SEEK_SET);
763 
764  AV_WL32(dst, bytestream2_get_le32(gb));
765  AV_WL32(dst + 4, bytestream2_get_le32(gb));
766  AV_WL32(dst + 8, bytestream2_get_le32(gb));
767  AV_WL32(dst + 12, bytestream2_get_le32(gb));
768 
769  tab0[0x9E3779B1 * AV_RL16(dst) >> 24] = dst;
770  tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFF) >> 24] = dst + 2;
771  tab2[0x9E3779B1 * AV_RL16(dst + 8) >> 24] = dst + 8;
772  tab3[0x9E3779B1 * (AV_RL32(dst + 10) & 0xFFFFFF) >> 24] = dst + 10;
773  dst += 16;
774  while (dst + 10 < tex_data + tex_size) {
775  ret = dxv_decompress_cgo(ctx, gb, tex_data, tex_size, op_data0, &oi0, op_size0,
776  &dst, &state0, tab0, tab1, 8);
777  if (ret < 0)
778  return ret;
779  ret = dxv_decompress_cgo(ctx, gb, tex_data, tex_size, op_data1, &oi1, op_size1,
780  &dst, &state1, tab2, tab3, 8);
781  if (ret < 0)
782  return ret;
783  }
784 
785  bytestream2_seek(gb, data_start + op_offset + skip0 + skip1 - 12, SEEK_SET);
786 
787  return 0;
788 }
789 
791  uint8_t *tex_data, int tex_size,
792  uint8_t *op_data, int max_op_size)
793 {
794  int op_offset = bytestream2_get_le32(gb);
795  unsigned op_size = bytestream2_get_le32(gb);
796  int data_start = bytestream2_tell(gb);
797  uint8_t *dst, *table0[256] = { 0 }, *table1[256] = { 0 };
798  int ret, state = 0, skip, oi = 0, v, vv;
799 
800  dst = tex_data;
801  bytestream2_skip(gb, op_offset - 8);
802  if (op_size > max_op_size)
803  return AVERROR_INVALIDDATA;
804  skip = dxv_decompress_opcodes(gb, op_data, op_size);
805  if (skip < 0)
806  return skip;
807  bytestream2_seek(gb, data_start, SEEK_SET);
808 
809  v = bytestream2_get_le32(gb);
810  AV_WL32(dst, v);
811  vv = bytestream2_get_le32(gb);
812  table0[0x9E3779B1 * (uint16_t)v >> 24] = dst;
813  AV_WL32(dst + 4, vv);
814  table1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFF) >> 24] = dst + 2;
815  dst += 8;
816 
817  while (dst < tex_data + tex_size) {
818  ret = dxv_decompress_cgo(ctx, gb, tex_data, tex_size, op_data, &oi, op_size,
819  &dst, &state, table0, table1, 0);
820  if (ret < 0)
821  return ret;
822  }
823 
824  bytestream2_seek(gb, data_start + op_offset + skip - 8, SEEK_SET);
825 
826  return 0;
827 }
828 
830 {
831  DXVContext *ctx = avctx->priv_data;
832  GetByteContext *gb = &ctx->gbc;
833  int ret;
834 
835  ret = dxv_decompress_yo(ctx, gb, ctx->tex_data, ctx->tex_size,
836  ctx->op_data[0], ctx->op_size[0]);
837  if (ret < 0)
838  return ret;
839 
840  return dxv_decompress_cocg(ctx, gb, ctx->ctex_data, ctx->ctex_size,
841  ctx->op_data[1], ctx->op_data[2],
842  ctx->op_size[1], ctx->op_size[2]);
843 }
844 
846 {
847  DXVContext *ctx = avctx->priv_data;
848  GetByteContext *gb = &ctx->gbc;
849  int ret;
850 
851  ret = dxv_decompress_cocg(ctx, gb, ctx->tex_data, ctx->tex_size,
852  ctx->op_data[0], ctx->op_data[3],
853  ctx->op_size[0], ctx->op_size[3]);
854  if (ret < 0)
855  return ret;
856 
857  return dxv_decompress_cocg(ctx, gb, ctx->ctex_data, ctx->ctex_size,
858  ctx->op_data[1], ctx->op_data[2],
859  ctx->op_size[1], ctx->op_size[2]);
860 }
861 
863 {
864  DXVContext *ctx = avctx->priv_data;
865  GetByteContext *gbc = &ctx->gbc;
866  uint32_t value, op;
867  int idx, prev, state = 0;
868  int pos = 4;
869  int run = 0;
870  int probe, check;
871 
872  /* Copy the first four elements */
873  AV_WL32(ctx->tex_data + 0, bytestream2_get_le32(gbc));
874  AV_WL32(ctx->tex_data + 4, bytestream2_get_le32(gbc));
875  AV_WL32(ctx->tex_data + 8, bytestream2_get_le32(gbc));
876  AV_WL32(ctx->tex_data + 12, bytestream2_get_le32(gbc));
877 
878  /* Process input until the whole texture has been filled */
879  while (pos + 2 <= ctx->tex_size / 4) {
880  if (run) {
881  run--;
882 
883  prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
884  AV_WL32(ctx->tex_data + 4 * pos, prev);
885  pos++;
886  prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
887  AV_WL32(ctx->tex_data + 4 * pos, prev);
888  pos++;
889  } else {
890  if (bytestream2_get_bytes_left(gbc) < 1)
891  return AVERROR_INVALIDDATA;
892  if (state == 0) {
893  value = bytestream2_get_le32(gbc);
894  state = 16;
895  }
896  op = value & 0x3;
897  value >>= 2;
898  state--;
899 
900  switch (op) {
901  case 0:
902  /* Long copy */
903  check = bytestream2_get_byte(gbc) + 1;
904  if (check == 256) {
905  do {
906  probe = bytestream2_get_le16(gbc);
907  check += probe;
908  } while (probe == 0xFFFF);
909  }
910  while (check && pos + 4 <= ctx->tex_size / 4) {
911  prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
912  AV_WL32(ctx->tex_data + 4 * pos, prev);
913  pos++;
914 
915  prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
916  AV_WL32(ctx->tex_data + 4 * pos, prev);
917  pos++;
918 
919  prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
920  AV_WL32(ctx->tex_data + 4 * pos, prev);
921  pos++;
922 
923  prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
924  AV_WL32(ctx->tex_data + 4 * pos, prev);
925  pos++;
926 
927  check--;
928  }
929 
930  /* Restart (or exit) the loop */
931  continue;
932  break;
933  case 1:
934  /* Load new run value */
935  run = bytestream2_get_byte(gbc);
936  if (run == 255) {
937  do {
938  probe = bytestream2_get_le16(gbc);
939  run += probe;
940  } while (probe == 0xFFFF);
941  }
942 
943  /* Copy two dwords from previous data */
944  prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
945  AV_WL32(ctx->tex_data + 4 * pos, prev);
946  pos++;
947 
948  prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
949  AV_WL32(ctx->tex_data + 4 * pos, prev);
950  pos++;
951  break;
952  case 2:
953  /* Copy two dwords from a previous index */
954  idx = 8 + bytestream2_get_le16(gbc);
955  if (idx > pos || (unsigned int)(pos - idx) + 2 > ctx->tex_size / 4)
956  return AVERROR_INVALIDDATA;
957  prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
958  AV_WL32(ctx->tex_data + 4 * pos, prev);
959  pos++;
960 
961  prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
962  AV_WL32(ctx->tex_data + 4 * pos, prev);
963  pos++;
964  break;
965  case 3:
966  /* Copy two dwords from input */
967  prev = bytestream2_get_le32(gbc);
968  AV_WL32(ctx->tex_data + 4 * pos, prev);
969  pos++;
970 
971  prev = bytestream2_get_le32(gbc);
972  AV_WL32(ctx->tex_data + 4 * pos, prev);
973  pos++;
974  break;
975  }
976  }
977 
978  CHECKPOINT(4);
979  if (pos + 2 > ctx->tex_size / 4)
980  return AVERROR_INVALIDDATA;
981 
982  /* Copy two elements from a previous offset or from the input buffer */
983  if (op) {
984  if (idx > pos || (unsigned int)(pos - idx) + 2 > ctx->tex_size / 4)
985  return AVERROR_INVALIDDATA;
986  prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
987  AV_WL32(ctx->tex_data + 4 * pos, prev);
988  pos++;
989 
990  prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
991  AV_WL32(ctx->tex_data + 4 * pos, prev);
992  pos++;
993  } else {
994  CHECKPOINT(4);
995 
996  if (op && (idx > pos || (unsigned int)(pos - idx) + 2 > ctx->tex_size / 4))
997  return AVERROR_INVALIDDATA;
998  if (op)
999  prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
1000  else
1001  prev = bytestream2_get_le32(gbc);
1002  AV_WL32(ctx->tex_data + 4 * pos, prev);
1003  pos++;
1004 
1005  CHECKPOINT(4);
1006 
1007  if (op)
1008  prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
1009  else
1010  prev = bytestream2_get_le32(gbc);
1011  AV_WL32(ctx->tex_data + 4 * pos, prev);
1012  pos++;
1013  }
1014  }
1015 
1016  return 0;
1017 }
1018 
1020 {
1021  DXVContext *ctx = avctx->priv_data;
1022  return ff_lzf_uncompress(&ctx->gbc, &ctx->tex_data, &ctx->tex_size);
1023 }
1024 
1026 {
1027  DXVContext *ctx = avctx->priv_data;
1028  GetByteContext *gbc = &ctx->gbc;
1029 
1030  if (bytestream2_get_bytes_left(gbc) < ctx->tex_size)
1031  return AVERROR_INVALIDDATA;
1032 
1033  bytestream2_get_buffer(gbc, ctx->tex_data, ctx->tex_size);
1034  return 0;
1035 }
1036 
1037 static int dxv_decode(AVCodecContext *avctx, void *data,
1038  int *got_frame, AVPacket *avpkt)
1039 {
1040  DXVContext *ctx = avctx->priv_data;
1041  ThreadFrame tframe;
1042  GetByteContext *gbc = &ctx->gbc;
1043  int (*decompress_tex)(AVCodecContext *avctx);
1044  const char *msgcomp, *msgtext;
1045  uint32_t tag;
1046  int version_major, version_minor = 0;
1047  int size = 0, old_type = 0;
1048  int ret;
1049 
1050  bytestream2_init(gbc, avpkt->data, avpkt->size);
1051 
1052  ctx->texture_block_h = 4;
1053  ctx->texture_block_w = 4;
1054 
1055  avctx->pix_fmt = AV_PIX_FMT_RGBA;
1056  avctx->colorspace = AVCOL_SPC_RGB;
1057 
1058  ctx->tex_funct = NULL;
1059  ctx->tex_funct_planar[0] = NULL;
1060  ctx->tex_funct_planar[1] = NULL;
1061 
1062  tag = bytestream2_get_le32(gbc);
1063  switch (tag) {
1064  case MKBETAG('D', 'X', 'T', '1'):
1065  decompress_tex = dxv_decompress_dxt1;
1066  ctx->tex_funct = ctx->texdsp.dxt1_block;
1067  ctx->tex_rat = 8;
1068  ctx->tex_step = 8;
1069  msgcomp = "DXTR1";
1070  msgtext = "DXT1";
1071  break;
1072  case MKBETAG('D', 'X', 'T', '5'):
1073  decompress_tex = dxv_decompress_dxt5;
1074  ctx->tex_funct = ctx->texdsp.dxt5_block;
1075  ctx->tex_rat = 4;
1076  ctx->tex_step = 16;
1077  msgcomp = "DXTR5";
1078  msgtext = "DXT5";
1079  break;
1080  case MKBETAG('Y', 'C', 'G', '6'):
1081  decompress_tex = dxv_decompress_ycg6;
1082  ctx->tex_funct_planar[0] = yo_block;
1083  ctx->tex_funct_planar[1] = cocg_block;
1084  ctx->tex_rat = 8;
1085  ctx->tex_step = 32;
1086  ctx->ctex_step = 16;
1087  msgcomp = "YOCOCG6";
1088  msgtext = "YCG6";
1089  ctx->ctex_size = avctx->coded_width * avctx->coded_height / 4;
1090  ctx->texture_block_h = 4;
1091  ctx->texture_block_w = 16;
1092  ctx->ctexture_block_h = 4;
1093  ctx->ctexture_block_w = 4;
1094  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
1095  avctx->colorspace = AVCOL_SPC_YCOCG;
1096  break;
1097  case MKBETAG('Y', 'G', '1', '0'):
1098  decompress_tex = dxv_decompress_yg10;
1099  ctx->tex_funct_planar[0] = yao_block;
1100  ctx->tex_funct_planar[1] = cocg_block;
1101  ctx->tex_rat = 4;
1102  ctx->tex_step = 64;
1103  ctx->ctex_step = 16;
1104  msgcomp = "YAOCOCG10";
1105  msgtext = "YG10";
1106  ctx->ctex_size = avctx->coded_width * avctx->coded_height / 4;
1107  ctx->texture_block_h = 4;
1108  ctx->texture_block_w = 16;
1109  ctx->ctexture_block_h = 4;
1110  ctx->ctexture_block_w = 4;
1111  avctx->pix_fmt = AV_PIX_FMT_YUVA420P;
1112  avctx->colorspace = AVCOL_SPC_YCOCG;
1113  break;
1114  default:
1115  /* Old version does not have a real header, just size and type. */
1116  size = tag & 0x00FFFFFF;
1117  old_type = tag >> 24;
1118  version_major = (old_type & 0x0F) - 1;
1119 
1120  if (old_type & 0x80) {
1121  msgcomp = "RAW";
1122  decompress_tex = dxv_decompress_raw;
1123  } else {
1124  msgcomp = "LZF";
1125  decompress_tex = dxv_decompress_lzf;
1126  }
1127 
1128  if (old_type & 0x40) {
1129  msgtext = "DXT5";
1130 
1131  ctx->tex_funct = ctx->texdsp.dxt5_block;
1132  ctx->tex_step = 16;
1133  } else if (old_type & 0x20 || version_major == 1) {
1134  msgtext = "DXT1";
1135 
1136  ctx->tex_funct = ctx->texdsp.dxt1_block;
1137  ctx->tex_step = 8;
1138  } else {
1139  av_log(avctx, AV_LOG_ERROR, "Unsupported header (0x%08"PRIX32")\n.", tag);
1140  return AVERROR_INVALIDDATA;
1141  }
1142  ctx->tex_rat = 1;
1143  break;
1144  }
1145 
1146  ctx->slice_count = av_clip(avctx->thread_count, 1,
1147  avctx->coded_height / FFMAX(ctx->texture_block_h,
1148  ctx->ctexture_block_h));
1149 
1150  /* New header is 12 bytes long. */
1151  if (!old_type) {
1152  version_major = bytestream2_get_byte(gbc) - 1;
1153  version_minor = bytestream2_get_byte(gbc);
1154 
1155  /* Encoder copies texture data when compression is not advantageous. */
1156  if (bytestream2_get_byte(gbc)) {
1157  msgcomp = "RAW";
1158  ctx->tex_rat = 1;
1159  decompress_tex = dxv_decompress_raw;
1160  }
1161 
1162  bytestream2_skip(gbc, 1); // unknown
1163  size = bytestream2_get_le32(gbc);
1164  }
1165  av_log(avctx, AV_LOG_DEBUG,
1166  "%s compression with %s texture (version %d.%d)\n",
1167  msgcomp, msgtext, version_major, version_minor);
1168 
1169  if (size != bytestream2_get_bytes_left(gbc)) {
1170  av_log(avctx, AV_LOG_ERROR,
1171  "Incomplete or invalid file (header %d, left %u).\n",
1172  size, bytestream2_get_bytes_left(gbc));
1173  return AVERROR_INVALIDDATA;
1174  }
1175 
1176  ctx->tex_size = avctx->coded_width * avctx->coded_height * 4 / ctx->tex_rat;
1178  if (ret < 0)
1179  return ret;
1180 
1181  if (ctx->ctex_size) {
1182  int i;
1183 
1184  ctx->op_size[0] = avctx->coded_width * avctx->coded_height / 16;
1185  ctx->op_size[1] = avctx->coded_width * avctx->coded_height / 32;
1186  ctx->op_size[2] = avctx->coded_width * avctx->coded_height / 32;
1187  ctx->op_size[3] = avctx->coded_width * avctx->coded_height / 16;
1188 
1190  if (ret < 0)
1191  return ret;
1192  for (i = 0; i < 4; i++) {
1193  ret = av_reallocp(&ctx->op_data[i], ctx->op_size[i]);
1194  if (ret < 0)
1195  return ret;
1196  }
1197  }
1198 
1199  /* Decompress texture out of the intermediate compression. */
1200  ret = decompress_tex(avctx);
1201  if (ret < 0)
1202  return ret;
1203  {
1204  int w_block = avctx->coded_width / ctx->texture_block_w;
1205  int h_block = avctx->coded_height / ctx->texture_block_h;
1206  if (w_block * h_block * ctx->tex_step > ctx->tex_size * 8LL)
1207  return AVERROR_INVALIDDATA;
1208  }
1209 
1210  tframe.f = data;
1211  ret = ff_thread_get_buffer(avctx, &tframe, 0);
1212  if (ret < 0)
1213  return ret;
1214 
1215  /* Now decompress the texture with the standard functions. */
1216  avctx->execute2(avctx, decompress_texture_thread,
1217  tframe.f, NULL, ctx->slice_count);
1218 
1219  /* Frame is ready to be output. */
1220  tframe.f->pict_type = AV_PICTURE_TYPE_I;
1221  tframe.f->key_frame = 1;
1222  *got_frame = 1;
1223 
1224  return avpkt->size;
1225 }
1226 
1227 static int dxv_init(AVCodecContext *avctx)
1228 {
1229  DXVContext *ctx = avctx->priv_data;
1230  int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
1231 
1232  if (ret < 0) {
1233  av_log(avctx, AV_LOG_ERROR, "Invalid image size %dx%d.\n",
1234  avctx->width, avctx->height);
1235  return ret;
1236  }
1237 
1238  /* Codec requires 16x16 alignment. */
1239  avctx->coded_width = FFALIGN(avctx->width, 16);
1240  avctx->coded_height = FFALIGN(avctx->height, 16);
1241 
1242  ff_texturedsp_init(&ctx->texdsp);
1243 
1244  return 0;
1245 }
1246 
1247 static int dxv_close(AVCodecContext *avctx)
1248 {
1249  DXVContext *ctx = avctx->priv_data;
1250 
1251  av_freep(&ctx->tex_data);
1252  av_freep(&ctx->ctex_data);
1253  av_freep(&ctx->op_data[0]);
1254  av_freep(&ctx->op_data[1]);
1255  av_freep(&ctx->op_data[2]);
1256  av_freep(&ctx->op_data[3]);
1257 
1258  return 0;
1259 }
1260 
1262  .name = "dxv",
1263  .long_name = NULL_IF_CONFIG_SMALL("Resolume DXV"),
1264  .type = AVMEDIA_TYPE_VIDEO,
1265  .id = AV_CODEC_ID_DXV,
1266  .init = dxv_init,
1267  .decode = dxv_decode,
1268  .close = dxv_close,
1269  .priv_data_size = sizeof(DXVContext),
1270  .capabilities = AV_CODEC_CAP_DR1 |
1273  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
1275 };
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: internal.h:48
#define NULL
Definition: coverity.c:32
const char const char void * val
Definition: avisynth_c.h:771
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int size
int(* dxt5_block)(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
Definition: texturedsp.h:51
This structure describes decoded (raw) audio or video data.
Definition: frame.h:226
uint8_t val2
Definition: dxv.c:338
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:1721
int ctex_step
Definition: dxv.c:43
misc image utilities
AVFrame * f
Definition: thread.h:35
Texture block (4x4) module.
int size
Definition: avcodec.h:1446
int ctexture_block_w
Definition: dxv.c:56
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1743
static int dxv_decompress_dxt1(AVCodecContext *avctx)
Definition: dxv.c:286
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:133
#define AV_RL16
Definition: intreadwrite.h:42
static int dxv_decompress_ycg6(AVCodecContext *avctx)
Definition: dxv.c:829
uint8_t run
Definition: svq3.c:206
#define src
Definition: vp8dsp.c:254
int stride
Definition: mace.c:144
AVCodec.
Definition: avcodec.h:3424
order of coefficients is actually GBR, also IEC 61966-2-1 (sRGB)
Definition: pixfmt.h:487
static int get_opcodes(GetByteContext *gb, uint32_t *table, uint8_t *dst, int op_size, int nb_elements)
Definition: dxv.c:426
int64_t ctex_size
Definition: dxv.c:45
static int16_t block[64]
Definition: dct.c:115
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:40
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:101
uint8_t
uint8_t * tex_data
Definition: dxv.c:39
static int dxv_decompress_dxt5(AVCodecContext *avctx)
Definition: dxv.c:862
int(* tex_funct_planar[2])(uint8_t *plane0, ptrdiff_t stride0, uint8_t *plane1, ptrdiff_t stride1, const uint8_t *block)
Definition: dxv.c:61
int64_t tex_size
Definition: dxv.c:44
static int yao_block(uint8_t *plane0, ptrdiff_t stride0, uint8_t *plane3, ptrdiff_t stride1, const uint8_t *block)
Definition: dxv.c:174
Multithreading support functions.
#define CHECKPOINT(x)
Definition: dxv.c:256
#define u(width, name, range_min, range_max)
Definition: cbs_h2645.c:253
static AVFrame * frame
int16_t next
Definition: dxv.c:336
const char data[16]
Definition: mxf.c:91
uint8_t * data
Definition: avcodec.h:1445
const uint8_t * buffer
Definition: bytestream.h:34
uint32_t tag
Definition: movenc.c:1478
static int dxv_decompress_yg10(AVCodecContext *avctx)
Definition: dxv.c:845
static int dxv_decompress_yo(DXVContext *ctx, GetByteContext *gb, uint8_t *tex_data, int tex_size, uint8_t *op_data, int max_op_size)
Definition: dxv.c:790
static int fill_ltable(GetByteContext *gb, uint32_t *table, int *nb_elements)
Definition: dxv.c:341
static int probe(AVProbeData *p)
Definition: act.c:36
#define FFALIGN(x, a)
Definition: macros.h:48
#define av_log(a,...)
static const uint16_t table[]
Definition: prosumer.c:206
TextureDSPContext texdsp
Definition: dxv.c:36
int ff_lzf_uncompress(GetByteContext *gb, uint8_t **buf, int64_t *size)
Definition: lzf.c:40
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
static int decompress_texture_thread(AVCodecContext *avctx, void *arg, int slice, int thread_nb)
Definition: dxv.c:193
static const uint16_t mask[17]
Definition: lzw.c:38
BYTE * dstp
Definition: avisynth_c.h:813
uint8_t val1
Definition: dxv.c:337
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:164
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
AVCodec ff_dxv_decoder
Definition: dxv.c:1261
GetByteContext gbc
Definition: dxv.c:37
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:263
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:154
const char * arg
Definition: jacosubdec.c:66
static int dxv_close(AVCodecContext *avctx)
Definition: dxv.c:1247
const char * name
Name of the codec implementation.
Definition: avcodec.h:3431
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
#define FFMAX(a, b)
Definition: common.h:94
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: avcodec.h:1024
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:93
static int dxv_decompress_lzf(AVCodecContext *avctx)
Definition: dxv.c:1019
Definition: dxv.c:35
static struct @303 state
static int yo_block(uint8_t *dst, ptrdiff_t stride, uint8_t *unused0, ptrdiff_t unused1, const uint8_t *block)
Definition: dxv.c:160
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
Definition: imgutils.c:282
av_cold void ff_texturedsp_init(TextureDSPContext *c)
Definition: texturedsp.c:637
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:309
#define ff_clz
Definition: intmath.h:142
int64_t op_size[4]
Definition: dxv.c:51
int width
picture width / height.
Definition: avcodec.h:1706
static const ElemCat * elements[ELEMENT_COUNT]
Definition: signature.h:566
int tex_rat
Definition: dxv.c:41
AVFormatContext * ctx
Definition: movenc.c:48
static int dxv_decompress_opcodes(GetByteContext *gb, void *dstp, size_t op_size)
Definition: dxv.c:468
static void decompress_indices(uint8_t *dst, const uint8_t *src)
Definition: dxv.c:66
int slice_count
Definition: dxv.c:48
#define AV_RL32
Definition: intreadwrite.h:146
static int dxv_decompress_cocg(DXVContext *ctx, GetByteContext *gb, uint8_t *tex_data, int tex_size, uint8_t *op_data0, uint8_t *op_data1, int max_op_size0, int max_op_size1)
Definition: dxv.c:733
int texture_block_h
Definition: dxv.c:54
if(ret< 0)
Definition: vf_mcdeint.c:279
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
Definition: avcodec.h:2785
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition: avcodec.h:1028
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:188
int(* execute2)(struct AVCodecContext *c, int(*func)(struct AVCodecContext *c2, void *arg, int jobnr, int threadnr), void *arg2, int *ret, int count)
The codec may call this to execute several independent things.
Definition: avcodec.h:2845
int av_reallocp(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory through a pointer to a pointer.
Definition: mem.c:163
Libavcodec external API header.
static int cocg_block(uint8_t *plane0, ptrdiff_t stride0, uint8_t *plane1, ptrdiff_t stride1, const uint8_t *block)
Definition: dxv.c:111
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:257
int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
main external API structure.
Definition: avcodec.h:1533
static void yao_subblock(uint8_t *dst, uint8_t *yo_indices, ptrdiff_t stride, const uint8_t *block)
Definition: dxv.c:141
int(* dxt1_block)(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
Definition: texturedsp.h:46
const int16_t * tab1
Definition: mace.c:144
double value
Definition: eval.c:98
int coded_height
Definition: avcodec.h:1721
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:2157
static int dxv_init(AVCodecContext *avctx)
Definition: dxv.c:1227
#define AV_RL24
Definition: intreadwrite.h:78
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:240
int ctexture_block_h
Definition: dxv.c:57
static int op(uint8_t **dst, const uint8_t *dst_end, GetByteContext *gb, int pixel, int count, int *x, int width, int linesize)
Perform decode operation.
Definition: anm.c:78
int
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
static int dxv_decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: dxv.c:1037
common internal api header.
#define flag(name)
Definition: cbs_av1.c:598
static double c[64]
#define AV_WL16(p, v)
Definition: intreadwrite.h:412
#define MKBETAG(a, b, c, d)
Definition: common.h:367
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:782
void * priv_data
Definition: avcodec.h:1560
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:304
int texture_block_w
Definition: dxv.c:53
static av_always_inline int bytestream2_seek(GetByteContext *g, int offset, int whence)
Definition: bytestream.h:208
int(* tex_funct)(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
Definition: dxv.c:60
#define av_freep(p)
static int fill_optable(unsigned *table0, OpcodeTable *table1, int nb_elements)
Definition: dxv.c:385
static int dxv_decompress_cgo(DXVContext *ctx, GetByteContext *gb, uint8_t *tex_data, int tex_size, uint8_t *op_data, int *oindex, int op_size, uint8_t **dstp, int *statep, uint8_t **tab0, uint8_t **tab1, int offset)
Definition: dxv.c:493
static int extract_component(int yo0, int yo1, int code)
Definition: dxv.c:82
uint8_t * op_data[4]
Definition: dxv.c:50
This structure stores compressed data.
Definition: avcodec.h:1422
uint8_t * ctex_data
Definition: dxv.c:40
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:968
for(j=16;j >0;--j)
const int16_t * tab2
Definition: mace.c:144
int tex_step
Definition: dxv.c:42
#define AV_WL32(p, v)
Definition: intreadwrite.h:426
static int dxv_decompress_raw(AVCodecContext *avctx)
Definition: dxv.c:1025
#define check(x, y, S, v)
static uint8_t tmp[11]
Definition: aes_ctr.c:26