FFmpeg  4.4.7
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  if (bytestream2_get_bytes_left(gbc) < 4) \
260  return AVERROR_INVALIDDATA; \
261  value = bytestream2_get_le32(gbc); \
262  state = 16; \
263  } \
264  op = value & 0x3; \
265  value >>= 2; \
266  state--; \
267  switch (op) { \
268  case 1: \
269  idx = x; \
270  break; \
271  case 2: \
272  idx = (bytestream2_get_byte(gbc) + 2) * x; \
273  if (idx > pos) { \
274  av_log(avctx, AV_LOG_ERROR, "idx %d > %d\n", idx, pos); \
275  return AVERROR_INVALIDDATA; \
276  } \
277  break; \
278  case 3: \
279  idx = (bytestream2_get_le16(gbc) + 0x102) * x; \
280  if (idx > pos) { \
281  av_log(avctx, AV_LOG_ERROR, "idx %d > %d\n", idx, pos); \
282  return AVERROR_INVALIDDATA; \
283  } \
284  break; \
285  } \
286  } while(0)
287 
289 {
290  DXVContext *ctx = avctx->priv_data;
291  GetByteContext *gbc = &ctx->gbc;
292  uint32_t value, prev, op;
293  int idx = 0, state = 0;
294  int pos = 2;
295 
296  /* Copy the first two elements */
297  AV_WL32(ctx->tex_data, bytestream2_get_le32(gbc));
298  AV_WL32(ctx->tex_data + 4, bytestream2_get_le32(gbc));
299 
300  /* Process input until the whole texture has been filled */
301  while (pos + 2 <= ctx->tex_size / 4) {
302  CHECKPOINT(2);
303 
304  /* Copy two elements from a previous offset or from the input buffer */
305  if (op) {
306  prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
307  AV_WL32(ctx->tex_data + 4 * pos, prev);
308  pos++;
309 
310  prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
311  AV_WL32(ctx->tex_data + 4 * pos, prev);
312  pos++;
313  } else {
314  CHECKPOINT(2);
315 
316  if (op)
317  prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
318  else
319  prev = bytestream2_get_le32(gbc);
320  AV_WL32(ctx->tex_data + 4 * pos, prev);
321  pos++;
322 
323  CHECKPOINT(2);
324 
325  if (op)
326  prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
327  else
328  prev = bytestream2_get_le32(gbc);
329  AV_WL32(ctx->tex_data + 4 * pos, prev);
330  pos++;
331  }
332  }
333 
334  return 0;
335 }
336 
337 typedef struct OpcodeTable {
338  int16_t next;
341 } OpcodeTable;
342 
343 static int fill_ltable(GetByteContext *gb, uint32_t *table, int *nb_elements)
344 {
345  unsigned half = 512, bits = 1023, left = 1024, input, mask;
346  int value, counter = 0, rshift = 10, lshift = 30;
347 
348  mask = bytestream2_get_le32(gb) >> 2;
349  while (left) {
350  if (counter >= 256)
351  return AVERROR_INVALIDDATA;
352  value = bits & mask;
353  left -= bits & mask;
354  mask >>= rshift;
355  lshift -= rshift;
356  table[counter++] = value;
357  if (lshift < 16) {
358  if (bytestream2_get_bytes_left(gb) <= 0)
359  return AVERROR_INVALIDDATA;
360 
361  input = bytestream2_get_le16(gb);
362  mask += input << lshift;
363  lshift += 16;
364  }
365  if (left < half) {
366  half >>= 1;
367  bits >>= 1;
368  rshift--;
369  }
370  }
371 
372  for (; !table[counter - 1]; counter--)
373  if (counter <= 0)
374  return AVERROR_INVALIDDATA;
375 
376  *nb_elements = counter;
377 
378  if (counter < 256)
379  memset(&table[counter], 0, 4 * (256 - counter));
380 
381  if (lshift >= 16)
382  bytestream2_seek(gb, -2, SEEK_CUR);
383 
384  return 0;
385 }
386 
387 static int fill_optable(unsigned *table0, OpcodeTable *table1, int nb_elements)
388 {
389  unsigned table2[256] = { 0 };
390  unsigned x = 0;
391  int val0, val1, i, j = 2, k = 0;
392 
393  table2[0] = table0[0];
394  for (i = 0; i < nb_elements - 1; i++, table2[i] = val0) {
395  val0 = table0[i + 1] + table2[i];
396  }
397 
398  if (!table2[0]) {
399  do {
400  k++;
401  } while (!table2[k]);
402  }
403 
404  j = 2;
405  for (i = 1024; i > 0; i--) {
406  for (table1[x].val1 = k; k < 256 && j > table2[k]; k++);
407  x = (x - 383) & 0x3FF;
408  j++;
409  }
410 
411  if (nb_elements > 0)
412  memcpy(&table2[0], table0, 4 * nb_elements);
413 
414  for (i = 0; i < 1024; i++) {
415  val0 = table1[i].val1;
416  val1 = table2[val0];
417  table2[val0]++;
418  x = 31 - ff_clz(val1);
419  if (x > 10)
420  return AVERROR_INVALIDDATA;
421  table1[i].val2 = 10 - x;
422  table1[i].next = (val1 << table1[i].val2) - 1024;
423  }
424 
425  return 0;
426 }
427 
428 static int get_opcodes(GetByteContext *gb, uint32_t *table, uint8_t *dst, int op_size, int nb_elements)
429 {
430  OpcodeTable optable[1024];
431  int sum, x, val, lshift, rshift, ret, i, idx;
432  int64_t size_in_bits;
433  unsigned endoffset, newoffset, offset;
434  unsigned next;
435  uint8_t *src = (uint8_t *)gb->buffer;
436 
437  ret = fill_optable(table, optable, nb_elements);
438  if (ret < 0)
439  return ret;
440 
441  size_in_bits = bytestream2_get_le32(gb);
442  endoffset = ((size_in_bits + 7) >> 3) - 4;
443  if ((int)endoffset <= 0 || bytestream2_get_bytes_left(gb) < endoffset)
444  return AVERROR_INVALIDDATA;
445 
446  offset = endoffset;
447  next = AV_RL32(src + endoffset);
448  rshift = (((size_in_bits & 0xFF) - 1) & 7) + 15;
449  lshift = 32 - rshift;
450  idx = (next >> rshift) & 0x3FF;
451  for (i = 0; i < op_size; i++) {
452  dst[i] = optable[idx].val1;
453  val = optable[idx].val2;
454  sum = val + lshift;
455  x = (next << lshift) >> 1 >> (31 - val);
456  newoffset = offset - (sum >> 3);
457  lshift = sum & 7;
458  idx = x + optable[idx].next;
459  offset = newoffset;
460  if (offset > endoffset)
461  return AVERROR_INVALIDDATA;
462  next = AV_RL32(src + offset);
463  }
464 
465  bytestream2_skip(gb, (size_in_bits + 7 >> 3) - 4);
466 
467  return 0;
468 }
469 
470 static int dxv_decompress_opcodes(GetByteContext *gb, void *dstp, size_t op_size)
471 {
472  int pos = bytestream2_tell(gb);
473  int flag = bytestream2_peek_byte(gb);
474 
475  if ((flag & 3) == 0) {
476  bytestream2_skip(gb, 1);
477  int read_size = bytestream2_get_buffer(gb, dstp, op_size);
478  if (read_size != op_size)
479  return AVERROR_INVALIDDATA;
480  } else if ((flag & 3) == 1) {
481  bytestream2_skip(gb, 1);
482  memset(dstp, bytestream2_get_byte(gb), op_size);
483  } else {
484  uint32_t table[256];
485  int ret, elements = 0;
486 
487  ret = fill_ltable(gb, table, &elements);
488  if (ret < 0)
489  return ret;
490  ret = get_opcodes(gb, table, dstp, op_size, elements);
491  if (ret < 0)
492  return ret;
493  }
494  return bytestream2_tell(gb) - pos;
495 }
496 
498  uint8_t *tex_data, int tex_size,
499  uint8_t *op_data, int *oindex,
500  int op_size,
501  uint8_t **dstp, int *statep,
502  uint8_t **tab0, uint8_t **tab1,
503  int offset)
504 {
505  uint8_t *dst = *dstp;
506  uint8_t *tptr0, *tptr1, *tptr3;
507  int oi = *oindex;
508  int state = *statep;
509  int opcode, v, vv;
510 
511  if (state <= 0) {
512  if (oi >= op_size)
513  return AVERROR_INVALIDDATA;
514  opcode = op_data[oi++];
515  if (!opcode) {
516  v = bytestream2_get_byte(gb);
517  if (v == 255) {
518  do {
519  if (bytestream2_get_bytes_left(gb) <= 0)
520  return AVERROR_INVALIDDATA;
521  opcode = bytestream2_get_le16(gb);
522  v += opcode;
523  } while (opcode == 0xFFFF);
524  }
525  AV_WL32(dst, AV_RL32(dst - (8 + offset)));
526  AV_WL32(dst + 4, AV_RL32(dst - (4 + offset)));
527  state = v + 4;
528  goto done;
529  }
530 
531  switch (opcode) {
532  case 1:
533  AV_WL32(dst, AV_RL32(dst - (8 + offset)));
534  AV_WL32(dst + 4, AV_RL32(dst - (4 + offset)));
535  break;
536  case 2:
537  vv = (8 + offset) * (bytestream2_get_le16(gb) + 1);
538  if (vv < 0 || vv > dst - tex_data)
539  return AVERROR_INVALIDDATA;
540  tptr0 = dst - vv;
541  v = AV_RL32(tptr0);
542  AV_WL32(dst, AV_RL32(tptr0));
543  AV_WL32(dst + 4, AV_RL32(tptr0 + 4));
544  tab0[0x9E3779B1 * (uint16_t)v >> 24] = dst;
545  tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
546  break;
547  case 3:
548  AV_WL32(dst, bytestream2_get_le32(gb));
549  AV_WL32(dst + 4, bytestream2_get_le32(gb));
550  tab0[0x9E3779B1 * AV_RL16(dst) >> 24] = dst;
551  tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
552  break;
553  case 4:
554  tptr3 = tab1[bytestream2_get_byte(gb)];
555  if (!tptr3)
556  return AVERROR_INVALIDDATA;
557  AV_WL16(dst, bytestream2_get_le16(gb));
558  AV_WL16(dst + 2, AV_RL16(tptr3));
559  dst[4] = tptr3[2];
560  AV_WL16(dst + 5, bytestream2_get_le16(gb));
561  dst[7] = bytestream2_get_byte(gb);
562  tab0[0x9E3779B1 * AV_RL16(dst) >> 24] = dst;
563  break;
564  case 5:
565  tptr3 = tab1[bytestream2_get_byte(gb)];
566  if (!tptr3)
567  return AVERROR_INVALIDDATA;
568  AV_WL16(dst, bytestream2_get_le16(gb));
569  AV_WL16(dst + 2, bytestream2_get_le16(gb));
570  dst[4] = bytestream2_get_byte(gb);
571  AV_WL16(dst + 5, AV_RL16(tptr3));
572  dst[7] = tptr3[2];
573  tab0[0x9E3779B1 * AV_RL16(dst) >> 24] = dst;
574  tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
575  break;
576  case 6:
577  tptr0 = tab1[bytestream2_get_byte(gb)];
578  if (!tptr0)
579  return AVERROR_INVALIDDATA;
580  tptr1 = tab1[bytestream2_get_byte(gb)];
581  if (!tptr1)
582  return AVERROR_INVALIDDATA;
583  AV_WL16(dst, bytestream2_get_le16(gb));
584  AV_WL16(dst + 2, AV_RL16(tptr0));
585  dst[4] = tptr0[2];
586  AV_WL16(dst + 5, AV_RL16(tptr1));
587  dst[7] = tptr1[2];
588  tab0[0x9E3779B1 * AV_RL16(dst) >> 24] = dst;
589  break;
590  case 7:
591  v = (8 + offset) * (bytestream2_get_le16(gb) + 1);
592  if (v < 0 || v > dst - tex_data)
593  return AVERROR_INVALIDDATA;
594  tptr0 = dst - v;
595  AV_WL16(dst, bytestream2_get_le16(gb));
596  AV_WL16(dst + 2, AV_RL16(tptr0 + 2));
597  AV_WL32(dst + 4, AV_RL32(tptr0 + 4));
598  tab0[0x9E3779B1 * AV_RL16(dst) >> 24] = dst;
599  tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
600  break;
601  case 8:
602  tptr1 = tab0[bytestream2_get_byte(gb)];
603  if (!tptr1)
604  return AVERROR_INVALIDDATA;
605  AV_WL16(dst, AV_RL16(tptr1));
606  AV_WL16(dst + 2, bytestream2_get_le16(gb));
607  AV_WL32(dst + 4, bytestream2_get_le32(gb));
608  tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
609  break;
610  case 9:
611  tptr1 = tab0[bytestream2_get_byte(gb)];
612  if (!tptr1)
613  return AVERROR_INVALIDDATA;
614  tptr3 = tab1[bytestream2_get_byte(gb)];
615  if (!tptr3)
616  return AVERROR_INVALIDDATA;
617  AV_WL16(dst, AV_RL16(tptr1));
618  AV_WL16(dst + 2, AV_RL16(tptr3));
619  dst[4] = tptr3[2];
620  AV_WL16(dst + 5, bytestream2_get_le16(gb));
621  dst[7] = bytestream2_get_byte(gb);
622  tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
623  break;
624  case 10:
625  tptr1 = tab0[bytestream2_get_byte(gb)];
626  if (!tptr1)
627  return AVERROR_INVALIDDATA;
628  tptr3 = tab1[bytestream2_get_byte(gb)];
629  if (!tptr3)
630  return AVERROR_INVALIDDATA;
631  AV_WL16(dst, AV_RL16(tptr1));
632  AV_WL16(dst + 2, bytestream2_get_le16(gb));
633  dst[4] = bytestream2_get_byte(gb);
634  AV_WL16(dst + 5, AV_RL16(tptr3));
635  dst[7] = tptr3[2];
636  tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
637  break;
638  case 11:
639  tptr0 = tab0[bytestream2_get_byte(gb)];
640  if (!tptr0)
641  return AVERROR_INVALIDDATA;
642  tptr3 = tab1[bytestream2_get_byte(gb)];
643  if (!tptr3)
644  return AVERROR_INVALIDDATA;
645  tptr1 = tab1[bytestream2_get_byte(gb)];
646  if (!tptr1)
647  return AVERROR_INVALIDDATA;
648  AV_WL16(dst, AV_RL16(tptr0));
649  AV_WL16(dst + 2, AV_RL16(tptr3));
650  dst[4] = tptr3[2];
651  AV_WL16(dst + 5, AV_RL16(tptr1));
652  dst[7] = tptr1[2];
653  break;
654  case 12:
655  tptr1 = tab0[bytestream2_get_byte(gb)];
656  if (!tptr1)
657  return AVERROR_INVALIDDATA;
658  v = (8 + offset) * (bytestream2_get_le16(gb) + 1);
659  if (v < 0 || v > dst - tex_data)
660  return AVERROR_INVALIDDATA;
661  tptr0 = dst - v;
662  AV_WL16(dst, AV_RL16(tptr1));
663  AV_WL16(dst + 2, AV_RL16(tptr0 + 2));
664  AV_WL32(dst + 4, AV_RL32(tptr0 + 4));
665  tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
666  break;
667  case 13:
668  AV_WL16(dst, AV_RL16(dst - (8 + offset)));
669  AV_WL16(dst + 2, bytestream2_get_le16(gb));
670  AV_WL32(dst + 4, bytestream2_get_le32(gb));
671  tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
672  break;
673  case 14:
674  tptr3 = tab1[bytestream2_get_byte(gb)];
675  if (!tptr3)
676  return AVERROR_INVALIDDATA;
677  AV_WL16(dst, AV_RL16(dst - (8 + offset)));
678  AV_WL16(dst + 2, AV_RL16(tptr3));
679  dst[4] = tptr3[2];
680  AV_WL16(dst + 5, bytestream2_get_le16(gb));
681  dst[7] = bytestream2_get_byte(gb);
682  tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
683  break;
684  case 15:
685  tptr3 = tab1[bytestream2_get_byte(gb)];
686  if (!tptr3)
687  return AVERROR_INVALIDDATA;
688  AV_WL16(dst, AV_RL16(dst - (8 + offset)));
689  AV_WL16(dst + 2, bytestream2_get_le16(gb));
690  dst[4] = bytestream2_get_byte(gb);
691  AV_WL16(dst + 5, AV_RL16(tptr3));
692  dst[7] = tptr3[2];
693  tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
694  break;
695  case 16:
696  tptr3 = tab1[bytestream2_get_byte(gb)];
697  if (!tptr3)
698  return AVERROR_INVALIDDATA;
699  tptr1 = tab1[bytestream2_get_byte(gb)];
700  if (!tptr1)
701  return AVERROR_INVALIDDATA;
702  AV_WL16(dst, AV_RL16(dst - (8 + offset)));
703  AV_WL16(dst + 2, AV_RL16(tptr3));
704  dst[4] = tptr3[2];
705  AV_WL16(dst + 5, AV_RL16(tptr1));
706  dst[7] = tptr1[2];
707  break;
708  case 17:
709  v = (8 + offset) * (bytestream2_get_le16(gb) + 1);
710  if (v < 0 || v > dst - tex_data)
711  return AVERROR_INVALIDDATA;
712  AV_WL16(dst, AV_RL16(dst - (8 + offset)));
713  AV_WL16(dst + 2, AV_RL16(&dst[-v + 2]));
714  AV_WL32(dst + 4, AV_RL32(&dst[-v + 4]));
715  tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFFu) >> 24] = dst + 2;
716  break;
717  default:
718  break;
719  }
720  } else {
721 done:
722  AV_WL32(dst, AV_RL32(dst - (8 + offset)));
723  AV_WL32(dst + 4, AV_RL32(dst - (4 + offset)));
724  state--;
725  }
726  if (dst - tex_data + 8 > tex_size)
727  return AVERROR_INVALIDDATA;
728  dst += 8;
729 
730  *oindex = oi;
731  *dstp = dst;
732  *statep = state;
733 
734  return 0;
735 }
736 
738  uint8_t *tex_data, int tex_size,
739  uint8_t *op_data0, uint8_t *op_data1,
740  int max_op_size0, int max_op_size1)
741 {
742  uint8_t *dst, *tab2[256] = { 0 }, *tab0[256] = { 0 }, *tab3[256] = { 0 }, *tab1[256] = { 0 };
743  int op_offset = bytestream2_get_le32(gb);
744  unsigned op_size0 = bytestream2_get_le32(gb);
745  unsigned op_size1 = bytestream2_get_le32(gb);
746  int data_start = bytestream2_tell(gb);
747  int skip0, skip1, oi0 = 0, oi1 = 0;
748  int ret, state0 = 0, state1 = 0;
749 
750  if (op_offset < 12 || op_offset - 12 > bytestream2_get_bytes_left(gb))
751  return AVERROR_INVALIDDATA;
752 
753  dst = tex_data;
754  bytestream2_skip(gb, op_offset - 12);
755  if (op_size0 > max_op_size0)
756  return AVERROR_INVALIDDATA;
757  skip0 = dxv_decompress_opcodes(gb, op_data0, op_size0);
758  if (skip0 < 0)
759  return skip0;
760  if (op_size1 > max_op_size1)
761  return AVERROR_INVALIDDATA;
762  skip1 = dxv_decompress_opcodes(gb, op_data1, op_size1);
763  if (skip1 < 0)
764  return skip1;
765  bytestream2_seek(gb, data_start, SEEK_SET);
766 
767  AV_WL32(dst, bytestream2_get_le32(gb));
768  AV_WL32(dst + 4, bytestream2_get_le32(gb));
769  AV_WL32(dst + 8, bytestream2_get_le32(gb));
770  AV_WL32(dst + 12, bytestream2_get_le32(gb));
771 
772  tab0[0x9E3779B1 * AV_RL16(dst) >> 24] = dst;
773  tab1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFF) >> 24] = dst + 2;
774  tab2[0x9E3779B1 * AV_RL16(dst + 8) >> 24] = dst + 8;
775  tab3[0x9E3779B1 * (AV_RL32(dst + 10) & 0xFFFFFF) >> 24] = dst + 10;
776  dst += 16;
777  while (dst + 10 < tex_data + tex_size) {
778  ret = dxv_decompress_cgo(ctx, gb, tex_data, tex_size, op_data0, &oi0, op_size0,
779  &dst, &state0, tab0, tab1, 8);
780  if (ret < 0)
781  return ret;
782  ret = dxv_decompress_cgo(ctx, gb, tex_data, tex_size, op_data1, &oi1, op_size1,
783  &dst, &state1, tab2, tab3, 8);
784  if (ret < 0)
785  return ret;
786  }
787 
788  bytestream2_seek(gb, data_start - 12 + op_offset + skip0 + skip1, SEEK_SET);
789 
790  return 0;
791 }
792 
794  uint8_t *tex_data, int tex_size,
795  uint8_t *op_data, int max_op_size)
796 {
797  int op_offset = bytestream2_get_le32(gb);
798  unsigned op_size = bytestream2_get_le32(gb);
799  int data_start = bytestream2_tell(gb);
800  uint8_t *dst, *table0[256] = { 0 }, *table1[256] = { 0 };
801  int ret, state = 0, skip, oi = 0, v, vv;
802 
803  if (op_offset < 8 || op_offset - 8 > bytestream2_get_bytes_left(gb))
804  return AVERROR_INVALIDDATA;
805 
806  dst = tex_data;
807  bytestream2_skip(gb, op_offset - 8);
808  if (op_size > max_op_size)
809  return AVERROR_INVALIDDATA;
810  skip = dxv_decompress_opcodes(gb, op_data, op_size);
811  if (skip < 0)
812  return skip;
813  bytestream2_seek(gb, data_start, SEEK_SET);
814 
815  v = bytestream2_get_le32(gb);
816  AV_WL32(dst, v);
817  vv = bytestream2_get_le32(gb);
818  table0[0x9E3779B1 * (uint16_t)v >> 24] = dst;
819  AV_WL32(dst + 4, vv);
820  table1[0x9E3779B1 * (AV_RL32(dst + 2) & 0xFFFFFF) >> 24] = dst + 2;
821  dst += 8;
822 
823  while (dst < tex_data + tex_size) {
824  ret = dxv_decompress_cgo(ctx, gb, tex_data, tex_size, op_data, &oi, op_size,
825  &dst, &state, table0, table1, 0);
826  if (ret < 0)
827  return ret;
828  }
829 
830  bytestream2_seek(gb, data_start + op_offset + skip - 8, SEEK_SET);
831 
832  return 0;
833 }
834 
836 {
837  DXVContext *ctx = avctx->priv_data;
838  GetByteContext *gb = &ctx->gbc;
839  int ret;
840 
841  ret = dxv_decompress_yo(ctx, gb, ctx->tex_data, ctx->tex_size,
842  ctx->op_data[0], ctx->op_size[0]);
843  if (ret < 0)
844  return ret;
845 
846  return dxv_decompress_cocg(ctx, gb, ctx->ctex_data, ctx->ctex_size,
847  ctx->op_data[1], ctx->op_data[2],
848  ctx->op_size[1], ctx->op_size[2]);
849 }
850 
852 {
853  DXVContext *ctx = avctx->priv_data;
854  GetByteContext *gb = &ctx->gbc;
855  int ret;
856 
857  ret = dxv_decompress_cocg(ctx, gb, ctx->tex_data, ctx->tex_size,
858  ctx->op_data[0], ctx->op_data[3],
859  ctx->op_size[0], ctx->op_size[3]);
860  if (ret < 0)
861  return ret;
862 
863  return dxv_decompress_cocg(ctx, gb, ctx->ctex_data, ctx->ctex_size,
864  ctx->op_data[1], ctx->op_data[2],
865  ctx->op_size[1], ctx->op_size[2]);
866 }
867 
869 {
870  DXVContext *ctx = avctx->priv_data;
871  GetByteContext *gbc = &ctx->gbc;
872  uint32_t value, op, prev;
873  int idx, state = 0;
874  int pos = 4;
875  int run = 0;
876  int probe, check;
877 
878  /* Copy the first four elements */
879  AV_WL32(ctx->tex_data + 0, bytestream2_get_le32(gbc));
880  AV_WL32(ctx->tex_data + 4, bytestream2_get_le32(gbc));
881  AV_WL32(ctx->tex_data + 8, bytestream2_get_le32(gbc));
882  AV_WL32(ctx->tex_data + 12, bytestream2_get_le32(gbc));
883 
884  /* Process input until the whole texture has been filled */
885  while (pos + 2 <= ctx->tex_size / 4) {
886  if (run) {
887  run--;
888 
889  prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
890  AV_WL32(ctx->tex_data + 4 * pos, prev);
891  pos++;
892  prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
893  AV_WL32(ctx->tex_data + 4 * pos, prev);
894  pos++;
895  } else {
896  if (bytestream2_get_bytes_left(gbc) < 1)
897  return AVERROR_INVALIDDATA;
898  if (state == 0) {
899  value = bytestream2_get_le32(gbc);
900  state = 16;
901  }
902  op = value & 0x3;
903  value >>= 2;
904  state--;
905 
906  switch (op) {
907  case 0:
908  /* Long copy */
909  check = bytestream2_get_byte(gbc) + 1;
910  if (check == 256) {
911  do {
912  probe = bytestream2_get_le16(gbc);
913  check += probe;
914  } while (probe == 0xFFFF);
915  }
916  while (check && pos + 4 <= ctx->tex_size / 4) {
917  prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
918  AV_WL32(ctx->tex_data + 4 * pos, prev);
919  pos++;
920 
921  prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
922  AV_WL32(ctx->tex_data + 4 * pos, prev);
923  pos++;
924 
925  prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
926  AV_WL32(ctx->tex_data + 4 * pos, prev);
927  pos++;
928 
929  prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
930  AV_WL32(ctx->tex_data + 4 * pos, prev);
931  pos++;
932 
933  check--;
934  }
935 
936  /* Restart (or exit) the loop */
937  continue;
938  break;
939  case 1:
940  /* Load new run value */
941  run = bytestream2_get_byte(gbc);
942  if (run == 255) {
943  do {
944  probe = bytestream2_get_le16(gbc);
945  run += probe;
946  } while (probe == 0xFFFF);
947  }
948 
949  /* Copy two dwords from previous data */
950  prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
951  AV_WL32(ctx->tex_data + 4 * pos, prev);
952  pos++;
953 
954  prev = AV_RL32(ctx->tex_data + 4 * (pos - 4));
955  AV_WL32(ctx->tex_data + 4 * pos, prev);
956  pos++;
957  break;
958  case 2:
959  /* Copy two dwords from a previous index */
960  idx = 8 + bytestream2_get_le16(gbc);
961  if (idx > pos || (unsigned int)(pos - idx) + 2 > ctx->tex_size / 4)
962  return AVERROR_INVALIDDATA;
963  prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
964  AV_WL32(ctx->tex_data + 4 * pos, prev);
965  pos++;
966 
967  prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
968  AV_WL32(ctx->tex_data + 4 * pos, prev);
969  pos++;
970  break;
971  case 3:
972  /* Copy two dwords from input */
973  prev = bytestream2_get_le32(gbc);
974  AV_WL32(ctx->tex_data + 4 * pos, prev);
975  pos++;
976 
977  prev = bytestream2_get_le32(gbc);
978  AV_WL32(ctx->tex_data + 4 * pos, prev);
979  pos++;
980  break;
981  }
982  }
983 
984  CHECKPOINT(4);
985  if (pos + 2 > ctx->tex_size / 4)
986  return AVERROR_INVALIDDATA;
987 
988  /* Copy two elements from a previous offset or from the input buffer */
989  if (op) {
990  if (idx > pos || (unsigned int)(pos - idx) + 2 > ctx->tex_size / 4)
991  return AVERROR_INVALIDDATA;
992  prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
993  AV_WL32(ctx->tex_data + 4 * pos, prev);
994  pos++;
995 
996  prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
997  AV_WL32(ctx->tex_data + 4 * pos, prev);
998  pos++;
999  } else {
1000  CHECKPOINT(4);
1001 
1002  if (op && (idx > pos || (unsigned int)(pos - idx) + 2 > ctx->tex_size / 4))
1003  return AVERROR_INVALIDDATA;
1004  if (op)
1005  prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
1006  else
1007  prev = bytestream2_get_le32(gbc);
1008  AV_WL32(ctx->tex_data + 4 * pos, prev);
1009  pos++;
1010 
1011  CHECKPOINT(4);
1012 
1013  if (op)
1014  prev = AV_RL32(ctx->tex_data + 4 * (pos - idx));
1015  else
1016  prev = bytestream2_get_le32(gbc);
1017  AV_WL32(ctx->tex_data + 4 * pos, prev);
1018  pos++;
1019  }
1020  }
1021 
1022  return 0;
1023 }
1024 
1026 {
1027  DXVContext *ctx = avctx->priv_data;
1028  return ff_lzf_uncompress(&ctx->gbc, &ctx->tex_data, &ctx->tex_size);
1029 }
1030 
1032 {
1033  DXVContext *ctx = avctx->priv_data;
1034  GetByteContext *gbc = &ctx->gbc;
1035 
1036  if (bytestream2_get_bytes_left(gbc) < ctx->tex_size)
1037  return AVERROR_INVALIDDATA;
1038 
1039  bytestream2_get_buffer(gbc, ctx->tex_data, ctx->tex_size);
1040  return 0;
1041 }
1042 
1043 static int dxv_decode(AVCodecContext *avctx, void *data,
1044  int *got_frame, AVPacket *avpkt)
1045 {
1046  DXVContext *ctx = avctx->priv_data;
1047  ThreadFrame tframe;
1048  GetByteContext *gbc = &ctx->gbc;
1049  int (*decompress_tex)(AVCodecContext *avctx);
1050  const char *msgcomp, *msgtext;
1051  uint32_t tag;
1052  int version_major, version_minor = 0;
1053  int size = 0, old_type = 0;
1054  int ret;
1055 
1056  bytestream2_init(gbc, avpkt->data, avpkt->size);
1057 
1058  ctx->texture_block_h = 4;
1059  ctx->texture_block_w = 4;
1060 
1061  avctx->pix_fmt = AV_PIX_FMT_RGBA;
1062  avctx->colorspace = AVCOL_SPC_RGB;
1063 
1064  ctx->tex_funct = NULL;
1065  ctx->tex_funct_planar[0] = NULL;
1066  ctx->tex_funct_planar[1] = NULL;
1067 
1068  tag = bytestream2_get_le32(gbc);
1069  switch (tag) {
1070  case MKBETAG('D', 'X', 'T', '1'):
1071  decompress_tex = dxv_decompress_dxt1;
1072  ctx->tex_funct = ctx->texdsp.dxt1_block;
1073  ctx->tex_rat = 8;
1074  ctx->tex_step = 8;
1075  msgcomp = "DXTR1";
1076  msgtext = "DXT1";
1077  break;
1078  case MKBETAG('D', 'X', 'T', '5'):
1079  decompress_tex = dxv_decompress_dxt5;
1080  ctx->tex_funct = ctx->texdsp.dxt5_block;
1081  ctx->tex_rat = 4;
1082  ctx->tex_step = 16;
1083  msgcomp = "DXTR5";
1084  msgtext = "DXT5";
1085  break;
1086  case MKBETAG('Y', 'C', 'G', '6'):
1087  decompress_tex = dxv_decompress_ycg6;
1088  ctx->tex_funct_planar[0] = yo_block;
1089  ctx->tex_funct_planar[1] = cocg_block;
1090  ctx->tex_rat = 8;
1091  ctx->tex_step = 32;
1092  ctx->ctex_step = 16;
1093  msgcomp = "YOCOCG6";
1094  msgtext = "YCG6";
1095  ctx->ctex_size = avctx->coded_width * avctx->coded_height / 4;
1096  ctx->texture_block_h = 4;
1097  ctx->texture_block_w = 16;
1098  ctx->ctexture_block_h = 4;
1099  ctx->ctexture_block_w = 4;
1100  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
1101  avctx->colorspace = AVCOL_SPC_YCOCG;
1102  break;
1103  case MKBETAG('Y', 'G', '1', '0'):
1104  decompress_tex = dxv_decompress_yg10;
1105  ctx->tex_funct_planar[0] = yao_block;
1106  ctx->tex_funct_planar[1] = cocg_block;
1107  ctx->tex_rat = 4;
1108  ctx->tex_step = 64;
1109  ctx->ctex_step = 16;
1110  msgcomp = "YAOCOCG10";
1111  msgtext = "YG10";
1112  ctx->ctex_size = avctx->coded_width * avctx->coded_height / 4;
1113  ctx->texture_block_h = 4;
1114  ctx->texture_block_w = 16;
1115  ctx->ctexture_block_h = 4;
1116  ctx->ctexture_block_w = 4;
1117  avctx->pix_fmt = AV_PIX_FMT_YUVA420P;
1118  avctx->colorspace = AVCOL_SPC_YCOCG;
1119  break;
1120  default:
1121  /* Old version does not have a real header, just size and type. */
1122  size = tag & 0x00FFFFFF;
1123  old_type = tag >> 24;
1124  version_major = (old_type & 0x0F) - 1;
1125 
1126  if (old_type & 0x80) {
1127  msgcomp = "RAW";
1128  decompress_tex = dxv_decompress_raw;
1129  } else {
1130  msgcomp = "LZF";
1131  decompress_tex = dxv_decompress_lzf;
1132  }
1133 
1134  if (old_type & 0x40) {
1135  msgtext = "DXT5";
1136 
1137  ctx->tex_funct = ctx->texdsp.dxt5_block;
1138  ctx->tex_step = 16;
1139  } else if (old_type & 0x20 || version_major == 1) {
1140  msgtext = "DXT1";
1141 
1142  ctx->tex_funct = ctx->texdsp.dxt1_block;
1143  ctx->tex_step = 8;
1144  } else {
1145  av_log(avctx, AV_LOG_ERROR, "Unsupported header (0x%08"PRIX32")\n.", tag);
1146  return AVERROR_INVALIDDATA;
1147  }
1148  ctx->tex_rat = 1;
1149  break;
1150  }
1151  if (avctx->coded_height / 2 / TEXTURE_BLOCK_H < 1)
1152  return AVERROR_INVALIDDATA;
1153 
1154  ctx->slice_count = av_clip(avctx->thread_count, 1,
1155  avctx->coded_height / FFMAX(ctx->texture_block_h,
1156  ctx->ctexture_block_h));
1157 
1158  /* New header is 12 bytes long. */
1159  if (!old_type) {
1160  version_major = bytestream2_get_byte(gbc) - 1;
1161  version_minor = bytestream2_get_byte(gbc);
1162 
1163  /* Encoder copies texture data when compression is not advantageous. */
1164  if (bytestream2_get_byte(gbc)) {
1165  msgcomp = "RAW";
1166  ctx->tex_rat = 1;
1167  decompress_tex = dxv_decompress_raw;
1168  }
1169 
1170  bytestream2_skip(gbc, 1); // unknown
1171  size = bytestream2_get_le32(gbc);
1172  }
1173  av_log(avctx, AV_LOG_DEBUG,
1174  "%s compression with %s texture (version %d.%d)\n",
1175  msgcomp, msgtext, version_major, version_minor);
1176 
1177  if (size != bytestream2_get_bytes_left(gbc)) {
1178  av_log(avctx, AV_LOG_ERROR,
1179  "Incomplete or invalid file (header %d, left %u).\n",
1181  return AVERROR_INVALIDDATA;
1182  }
1183 
1184  ctx->tex_size = avctx->coded_width * avctx->coded_height * 4 / ctx->tex_rat;
1185  ret = av_reallocp(&ctx->tex_data, ctx->tex_size + AV_INPUT_BUFFER_PADDING_SIZE);
1186  if (ret < 0)
1187  return ret;
1188 
1189  if (ctx->ctex_size) {
1190  int i;
1191 
1192  ctx->op_size[0] = avctx->coded_width * avctx->coded_height / 16;
1193  ctx->op_size[1] = avctx->coded_width * avctx->coded_height / 32;
1194  ctx->op_size[2] = avctx->coded_width * avctx->coded_height / 32;
1195  ctx->op_size[3] = avctx->coded_width * avctx->coded_height / 16;
1196 
1197  ret = av_reallocp(&ctx->ctex_data, ctx->ctex_size + AV_INPUT_BUFFER_PADDING_SIZE);
1198  if (ret < 0)
1199  return ret;
1200  for (i = 0; i < 4; i++) {
1201  ret = av_reallocp(&ctx->op_data[i], ctx->op_size[i]);
1202  if (ret < 0)
1203  return ret;
1204  }
1205  }
1206 
1207  /* Decompress texture out of the intermediate compression. */
1208  ret = decompress_tex(avctx);
1209  if (ret < 0)
1210  return ret;
1211  {
1212  int w_block = avctx->coded_width / ctx->texture_block_w;
1213  int h_block = avctx->coded_height / ctx->texture_block_h;
1214  if (w_block * h_block * ctx->tex_step > ctx->tex_size * 8LL)
1215  return AVERROR_INVALIDDATA;
1216  }
1217 
1218  tframe.f = data;
1219  ret = ff_thread_get_buffer(avctx, &tframe, 0);
1220  if (ret < 0)
1221  return ret;
1222 
1223  /* Now decompress the texture with the standard functions. */
1224  avctx->execute2(avctx, decompress_texture_thread,
1225  tframe.f, NULL, ctx->slice_count);
1226 
1227  /* Frame is ready to be output. */
1228  tframe.f->pict_type = AV_PICTURE_TYPE_I;
1229  tframe.f->key_frame = 1;
1230  *got_frame = 1;
1231 
1232  return avpkt->size;
1233 }
1234 
1235 static int dxv_init(AVCodecContext *avctx)
1236 {
1237  DXVContext *ctx = avctx->priv_data;
1238  int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
1239 
1240  if (ret < 0) {
1241  av_log(avctx, AV_LOG_ERROR, "Invalid image size %dx%d.\n",
1242  avctx->width, avctx->height);
1243  return ret;
1244  }
1245 
1246  /* Codec requires 16x16 alignment. */
1247  avctx->coded_width = FFALIGN(avctx->width, 16);
1248  avctx->coded_height = FFALIGN(avctx->height, 16);
1249 
1250  ff_texturedsp_init(&ctx->texdsp);
1251 
1252  return 0;
1253 }
1254 
1255 static int dxv_close(AVCodecContext *avctx)
1256 {
1257  DXVContext *ctx = avctx->priv_data;
1258 
1259  av_freep(&ctx->tex_data);
1260  av_freep(&ctx->ctex_data);
1261  av_freep(&ctx->op_data[0]);
1262  av_freep(&ctx->op_data[1]);
1263  av_freep(&ctx->op_data[2]);
1264  av_freep(&ctx->op_data[3]);
1265 
1266  return 0;
1267 }
1268 
1270  .name = "dxv",
1271  .long_name = NULL_IF_CONFIG_SMALL("Resolume DXV"),
1272  .type = AVMEDIA_TYPE_VIDEO,
1273  .id = AV_CODEC_ID_DXV,
1274  .init = dxv_init,
1275  .decode = dxv_decode,
1276  .close = dxv_close,
1277  .priv_data_size = sizeof(DXVContext),
1278  .capabilities = AV_CODEC_CAP_DR1 |
1281  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
1283 };
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 @321 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:851
static void decompress_indices(uint8_t *dst, const uint8_t *src)
Definition: dxv.c:66
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:497
static int dxv_close(AVCodecContext *avctx)
Definition: dxv.c:1255
static int dxv_decompress_dxt1(AVCodecContext *avctx)
Definition: dxv.c:288
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:737
static int extract_component(int yo0, int yo1, int code)
Definition: dxv.c:82
static int fill_optable(unsigned *table0, OpcodeTable *table1, int nb_elements)
Definition: dxv.c:387
static int dxv_decompress_dxt5(AVCodecContext *avctx)
Definition: dxv.c:868
static int yo_block(uint8_t *dst, ptrdiff_t stride, uint8_t *unused0, ptrdiff_t unused1, const uint8_t *block)
Definition: dxv.c:160
AVCodec ff_dxv_decoder
Definition: dxv.c:1269
static int dxv_decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: dxv.c:1043
static int get_opcodes(GetByteContext *gb, uint32_t *table, uint8_t *dst, int op_size, int nb_elements)
Definition: dxv.c:428
static int dxv_decompress_raw(AVCodecContext *avctx)
Definition: dxv.c:1031
static int decompress_texture_thread(AVCodecContext *avctx, void *arg, int slice, int thread_nb)
Definition: dxv.c:193
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:793
static int dxv_decompress_ycg6(AVCodecContext *avctx)
Definition: dxv.c:835
static int dxv_init(AVCodecContext *avctx)
Definition: dxv.c:1235
static int fill_ltable(GetByteContext *gb, uint32_t *table, int *nb_elements)
Definition: dxv.c:343
static int yao_block(uint8_t *plane0, ptrdiff_t stride0, uint8_t *plane3, ptrdiff_t stride1, const uint8_t *block)
Definition: dxv.c:174
static void yao_subblock(uint8_t *dst, uint8_t *yo_indices, ptrdiff_t stride, const uint8_t *block)
Definition: dxv.c:141
#define CHECKPOINT(x)
Definition: dxv.c:256
static int dxv_decompress_opcodes(GetByteContext *gb, void *dstp, size_t op_size)
Definition: dxv.c:470
static int cocg_block(uint8_t *plane0, ptrdiff_t stride0, uint8_t *plane1, ptrdiff_t stride1, const uint8_t *block)
Definition: dxv.c:111
static int dxv_decompress_lzf(AVCodecContext *avctx)
Definition: dxv.c:1025
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 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
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:317
@ 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:66
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, int64_t *size)
Definition: lzf.c:40
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:60
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
int ctex_step
Definition: dxv.c:43
uint8_t * tex_data
Definition: dxv.c:39
int slice_count
Definition: dxv.c:48
uint8_t * ctex_data
Definition: dxv.c:40
int64_t ctex_size
Definition: dxv.c:45
int64_t tex_size
Definition: dxv.c:44
uint8_t * op_data[4]
Definition: dxv.c:50
TextureDSPContext texdsp
Definition: dxv.c:36
int tex_rat
Definition: dxv.c:41
int ctexture_block_h
Definition: dxv.c:57
int texture_block_w
Definition: dxv.c:53
int texture_block_h
Definition: dxv.c:54
int64_t op_size[4]
Definition: dxv.c:51
int ctexture_block_w
Definition: dxv.c:56
GetByteContext gbc
Definition: dxv.c:37
int tex_step
Definition: dxv.c:42
const uint8_t * buffer
Definition: bytestream.h:34
uint8_t val1
Definition: dxv.c:339
int16_t next
Definition: dxv.c:338
uint8_t val2
Definition: dxv.c:340
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:637
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]