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