Libav
dds.c
Go to the documentation of this file.
1 /*
2  * DirectDraw Surface image decoder
3  * Copyright (C) 2015 Vittorio Giovara <vittorio.giovara@gmail.com>
4  *
5  * This file is part of Libav.
6  *
7  * Libav is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * Libav is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
29 #include <stdint.h>
30 
31 #include "libavutil/imgutils.h"
32 
33 #include "avcodec.h"
34 #include "bytestream.h"
35 #include "internal.h"
36 #include "texturedsp.h"
37 #include "thread.h"
38 
39 #define DDPF_FOURCC (1 << 2)
40 #define DDPF_PALETTE (1 << 5)
41 #define DDPF_NORMALMAP (1 << 31)
42 
44  DDS_NONE = 0,
56 };
57 
65 
72 
95 };
96 
97 typedef struct DDSContext {
100 
102  int paletted;
104 
105  const uint8_t *tex_data; // Compressed texture
106  int tex_ratio; // Compression ratio
107  int slice_count; // Number of slices for threaded operations
108 
109  /* Pointer to the selected compress or decompress function. */
110  int (*tex_funct)(uint8_t *dst, ptrdiff_t stride, const uint8_t *block);
111 } DDSContext;
112 
114 {
115  DDSContext *ctx = avctx->priv_data;
116  GetByteContext *gbc = &ctx->gbc;
117  char buf[32];
118  uint32_t flags, fourcc, gimp_tag;
119  enum DDSDXGIFormat dxgi;
120  int size, bpp, r, g, b, a;
121  int alpha_exponent, ycocg_classic, ycocg_scaled, normal_map, array;
122 
123  /* Alternative DDS implementations use reserved1 as custom header. */
124  bytestream2_skip(gbc, 4 * 3);
125  gimp_tag = bytestream2_get_le32(gbc);
126  alpha_exponent = gimp_tag == MKTAG('A', 'E', 'X', 'P');
127  ycocg_classic = gimp_tag == MKTAG('Y', 'C', 'G', '1');
128  ycocg_scaled = gimp_tag == MKTAG('Y', 'C', 'G', '2');
129  bytestream2_skip(gbc, 4 * 7);
130 
131  /* Now the real DDPF starts. */
132  size = bytestream2_get_le32(gbc);
133  if (size != 32) {
134  av_log(avctx, AV_LOG_ERROR, "Invalid pixel format header %d.\n", size);
135  return AVERROR_INVALIDDATA;
136  }
137  flags = bytestream2_get_le32(gbc);
138  ctx->compressed = flags & DDPF_FOURCC;
139  ctx->paletted = flags & DDPF_PALETTE;
140  normal_map = flags & DDPF_NORMALMAP;
141  fourcc = bytestream2_get_le32(gbc);
142 
143  if (ctx->compressed && ctx->paletted) {
144  av_log(avctx, AV_LOG_WARNING,
145  "Disabling invalid palette flag for compressed dds.\n");
146  ctx->paletted = 0;
147  }
148 
149  bpp = bytestream2_get_le32(gbc); // rgbbitcount
150  r = bytestream2_get_le32(gbc); // rbitmask
151  g = bytestream2_get_le32(gbc); // gbitmask
152  b = bytestream2_get_le32(gbc); // bbitmask
153  a = bytestream2_get_le32(gbc); // abitmask
154 
155  bytestream2_skip(gbc, 4); // caps
156  bytestream2_skip(gbc, 4); // caps2
157  bytestream2_skip(gbc, 4); // caps3
158  bytestream2_skip(gbc, 4); // caps4
159  bytestream2_skip(gbc, 4); // reserved2
160 
161  av_get_codec_tag_string(buf, sizeof(buf), fourcc);
162  av_log(avctx, AV_LOG_VERBOSE, "fourcc %s bpp %d "
163  "r 0x%x g 0x%x b 0x%x a 0x%x\n", buf, bpp, r, g, b, a);
164  if (gimp_tag) {
165  av_get_codec_tag_string(buf, sizeof(buf), gimp_tag);
166  av_log(avctx, AV_LOG_VERBOSE, "and GIMP-DDS tag %s\n", buf);
167  }
168 
169  if (ctx->compressed)
170  avctx->pix_fmt = AV_PIX_FMT_RGBA;
171 
172  if (ctx->compressed) {
173  switch (fourcc) {
174  case MKTAG('D', 'X', 'T', '1'):
175  ctx->tex_ratio = 8;
176  ctx->tex_funct = ctx->texdsp.dxt1a_block;
177  break;
178  case MKTAG('D', 'X', 'T', '2'):
179  ctx->tex_ratio = 16;
180  ctx->tex_funct = ctx->texdsp.dxt2_block;
181  break;
182  case MKTAG('D', 'X', 'T', '3'):
183  ctx->tex_ratio = 16;
184  ctx->tex_funct = ctx->texdsp.dxt3_block;
185  break;
186  case MKTAG('D', 'X', 'T', '4'):
187  ctx->tex_ratio = 16;
188  ctx->tex_funct = ctx->texdsp.dxt4_block;
189  break;
190  case MKTAG('D', 'X', 'T', '5'):
191  ctx->tex_ratio = 16;
192  if (ycocg_scaled)
193  ctx->tex_funct = ctx->texdsp.dxt5ys_block;
194  else if (ycocg_classic)
195  ctx->tex_funct = ctx->texdsp.dxt5y_block;
196  else
197  ctx->tex_funct = ctx->texdsp.dxt5_block;
198  break;
199  case MKTAG('R', 'X', 'G', 'B'):
200  ctx->tex_ratio = 16;
201  ctx->tex_funct = ctx->texdsp.dxt5_block;
202  /* This format may be considered as a normal map,
203  * but it is handled differently in a separate postproc. */
204  ctx->postproc = DDS_SWIZZLE_RXGB;
205  normal_map = 0;
206  break;
207  case MKTAG('A', 'T', 'I', '1'):
208  case MKTAG('B', 'C', '4', 'U'):
209  ctx->tex_ratio = 8;
210  ctx->tex_funct = ctx->texdsp.rgtc1u_block;
211  break;
212  case MKTAG('B', 'C', '4', 'S'):
213  ctx->tex_ratio = 8;
214  ctx->tex_funct = ctx->texdsp.rgtc1s_block;
215  break;
216  case MKTAG('A', 'T', 'I', '2'):
217  /* RGT2 variant with swapped R and G (3Dc)*/
218  ctx->tex_ratio = 16;
219  ctx->tex_funct = ctx->texdsp.dxn3dc_block;
220  break;
221  case MKTAG('B', 'C', '5', 'U'):
222  ctx->tex_ratio = 16;
223  ctx->tex_funct = ctx->texdsp.rgtc2u_block;
224  break;
225  case MKTAG('B', 'C', '5', 'S'):
226  ctx->tex_ratio = 16;
227  ctx->tex_funct = ctx->texdsp.rgtc2s_block;
228  break;
229  case MKTAG('U', 'Y', 'V', 'Y'):
230  ctx->compressed = 0;
231  avctx->pix_fmt = AV_PIX_FMT_UYVY422;
232  break;
233  case MKTAG('Y', 'U', 'Y', '2'):
234  ctx->compressed = 0;
235  avctx->pix_fmt = AV_PIX_FMT_YUYV422;
236  break;
237  case MKTAG('P', '8', ' ', ' '):
238  /* ATI Palette8, same as normal palette */
239  ctx->compressed = 0;
240  ctx->paletted = 1;
241  avctx->pix_fmt = AV_PIX_FMT_PAL8;
242  break;
243  case MKTAG('G', '1', ' ', ' '):
244  ctx->compressed = 0;
245  avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
246  break;
247  case MKTAG('D', 'X', '1', '0'):
248  /* DirectX 10 extra header */
249  dxgi = bytestream2_get_le32(gbc);
250  bytestream2_skip(gbc, 4); // resourceDimension
251  bytestream2_skip(gbc, 4); // miscFlag
252  array = bytestream2_get_le32(gbc);
253  bytestream2_skip(gbc, 4); // miscFlag2
254 
255  if (array != 0)
256  av_log(avctx, AV_LOG_VERBOSE,
257  "Found array of size %d (ignored).\n", array);
258 
259  /* Only BC[1-5] are actually compressed. */
260  ctx->compressed = (dxgi >= 70) && (dxgi <= 84);
261 
262  av_log(avctx, AV_LOG_VERBOSE, "DXGI format %d.\n", dxgi);
263  switch (dxgi) {
264  /* RGB types. */
271  avctx->pix_fmt = AV_PIX_FMT_BGRA64;
272  break;
274  avctx->colorspace = AVCOL_SPC_RGB;
280  avctx->pix_fmt = AV_PIX_FMT_BGRA;
281  break;
283  avctx->colorspace = AVCOL_SPC_RGB;
286  avctx->pix_fmt = AV_PIX_FMT_RGBA;
287  break;
289  avctx->colorspace = AVCOL_SPC_RGB;
292  avctx->pix_fmt = AV_PIX_FMT_RGBA; // opaque
293  break;
295  avctx->pix_fmt = AV_PIX_FMT_RGB565LE;
296  break;
297  /* Texture types. */
299  avctx->colorspace = AVCOL_SPC_RGB;
302  ctx->tex_ratio = 8;
303  ctx->tex_funct = ctx->texdsp.dxt1a_block;
304  break;
306  avctx->colorspace = AVCOL_SPC_RGB;
309  ctx->tex_ratio = 16;
310  ctx->tex_funct = ctx->texdsp.dxt3_block;
311  break;
313  avctx->colorspace = AVCOL_SPC_RGB;
316  ctx->tex_ratio = 16;
317  ctx->tex_funct = ctx->texdsp.dxt5_block;
318  break;
321  ctx->tex_ratio = 8;
322  ctx->tex_funct = ctx->texdsp.rgtc1u_block;
323  break;
325  ctx->tex_ratio = 8;
326  ctx->tex_funct = ctx->texdsp.rgtc1s_block;
327  break;
330  ctx->tex_ratio = 16;
331  ctx->tex_funct = ctx->texdsp.rgtc2u_block;
332  break;
334  ctx->tex_ratio = 16;
335  ctx->tex_funct = ctx->texdsp.rgtc2s_block;
336  break;
337  default:
338  av_log(avctx, AV_LOG_ERROR,
339  "Unsupported DXGI format %d.\n", dxgi);
340  return AVERROR_INVALIDDATA;
341  }
342  break;
343  default:
344  av_log(avctx, AV_LOG_ERROR, "Unsupported %s fourcc.\n", buf);
345  return AVERROR_INVALIDDATA;
346  }
347  } else if (ctx->paletted) {
348  if (bpp == 8) {
349  avctx->pix_fmt = AV_PIX_FMT_PAL8;
350  } else {
351  av_log(avctx, AV_LOG_ERROR, "Unsupported palette bpp %d.\n", bpp);
352  return AVERROR_INVALIDDATA;
353  }
354  } else {
355  /* 8 bpp */
356  if (bpp == 8 && r == 0xff && g == 0 && b == 0 && a == 0)
357  avctx->pix_fmt = AV_PIX_FMT_GRAY8;
358  else if (bpp == 8 && r == 0 && g == 0 && b == 0 && a == 0xff)
359  avctx->pix_fmt = AV_PIX_FMT_GRAY8;
360  /* 16 bpp */
361  else if (bpp == 16 && r == 0xff && g == 0 && b == 0 && a == 0xff00)
362  avctx->pix_fmt = AV_PIX_FMT_YA8;
363  else if (bpp == 16 && r == 0xffff && g == 0 && b == 0 && a == 0)
364  avctx->pix_fmt = AV_PIX_FMT_GRAY16LE;
365  else if (bpp == 16 && r == 0x7c00 && g == 0x3e0 && b == 0x1f && a == 0)
366  avctx->pix_fmt = AV_PIX_FMT_RGB555LE;
367  else if (bpp == 16 && r == 0x7c00 && g == 0x3e0 && b == 0x1f && a == 0x8000)
368  avctx->pix_fmt = AV_PIX_FMT_RGB555LE; // alpha ignored
369  else if (bpp == 16 && r == 0xf800 && g == 0x7e0 && b == 0x1f && a == 0)
370  avctx->pix_fmt = AV_PIX_FMT_RGB565LE;
371  /* 24 bpp */
372  else if (bpp == 24 && r == 0xff0000 && g == 0xff00 && b == 0xff && a == 0)
373  avctx->pix_fmt = AV_PIX_FMT_BGR24;
374  /* 32 bpp */
375  else if (bpp == 32 && r == 0xff0000 && g == 0xff00 && b == 0xff && a == 0)
376  avctx->pix_fmt = AV_PIX_FMT_BGRA; // opaque
377  else if (bpp == 32 && r == 0xff && g == 0xff00 && b == 0xff0000 && a == 0)
378  avctx->pix_fmt = AV_PIX_FMT_RGBA; // opaque
379  else if (bpp == 32 && r == 0xff0000 && g == 0xff00 && b == 0xff && a == 0xff000000)
380  avctx->pix_fmt = AV_PIX_FMT_BGRA;
381  else if (bpp == 32 && r == 0xff && g == 0xff00 && b == 0xff0000 && a == 0xff000000)
382  avctx->pix_fmt = AV_PIX_FMT_RGBA;
383  /* give up */
384  else {
385  av_log(avctx, AV_LOG_ERROR, "Unknown pixel format "
386  "[bpp %d r 0x%x g 0x%x b 0x%x a 0x%x].\n", bpp, r, g, b, a);
387  return AVERROR_INVALIDDATA;
388  }
389  }
390 
391  /* Set any remaining post-proc that should happen before frame is ready. */
392  if (alpha_exponent)
393  ctx->postproc = DDS_ALPHA_EXP;
394  else if (normal_map)
395  ctx->postproc = DDS_NORMAL_MAP;
396  else if (ycocg_classic && !ctx->compressed)
397  ctx->postproc = DDS_RAW_YCOCG;
398 
399  /* ATI/NVidia variants sometimes add swizzling in bpp. */
400  switch (bpp) {
401  case MKTAG('A', '2', 'X', 'Y'):
402  ctx->postproc = DDS_SWIZZLE_A2XY;
403  break;
404  case MKTAG('x', 'G', 'B', 'R'):
405  ctx->postproc = DDS_SWIZZLE_XGBR;
406  break;
407  case MKTAG('x', 'R', 'B', 'G'):
408  ctx->postproc = DDS_SWIZZLE_XRBG;
409  break;
410  case MKTAG('R', 'B', 'x', 'G'):
411  ctx->postproc = DDS_SWIZZLE_RBXG;
412  break;
413  case MKTAG('R', 'G', 'x', 'B'):
414  ctx->postproc = DDS_SWIZZLE_RGXB;
415  break;
416  case MKTAG('R', 'x', 'B', 'G'):
417  ctx->postproc = DDS_SWIZZLE_RXBG;
418  break;
419  case MKTAG('x', 'G', 'x', 'R'):
420  ctx->postproc = DDS_SWIZZLE_XGXR;
421  break;
422  case MKTAG('A', '2', 'D', '5'):
423  ctx->postproc = DDS_NORMAL_MAP;
424  break;
425  }
426 
427  return 0;
428 }
429 
430 static int decompress_texture_thread(AVCodecContext *avctx, void *arg,
431  int slice, int thread_nb)
432 {
433  DDSContext *ctx = avctx->priv_data;
434  AVFrame *frame = arg;
435  const uint8_t *d = ctx->tex_data;
436  int w_block = avctx->coded_width / TEXTURE_BLOCK_W;
437  int h_block = avctx->coded_height / TEXTURE_BLOCK_H;
438  int x, y;
439  int start_slice, end_slice;
440  int base_blocks_per_slice = h_block / ctx->slice_count;
441  int remainder_blocks = h_block % ctx->slice_count;
442 
443  /* When the frame height (in blocks) doesn't divide evenly between the
444  * number of slices, spread the remaining blocks evenly between the first
445  * operations */
446  start_slice = slice * base_blocks_per_slice;
447  /* Add any extra blocks (one per slice) that have been added before this slice */
448  start_slice += FFMIN(slice, remainder_blocks);
449 
450  end_slice = start_slice + base_blocks_per_slice;
451  /* Add an extra block if there are still remainder blocks to be accounted for */
452  if (slice < remainder_blocks)
453  end_slice++;
454 
455  for (y = start_slice; y < end_slice; y++) {
456  uint8_t *p = frame->data[0] + y * frame->linesize[0] * TEXTURE_BLOCK_H;
457  int off = y * w_block;
458  for (x = 0; x < w_block; x++) {
459  ctx->tex_funct(p + x * 16, frame->linesize[0],
460  d + (off + x) * ctx->tex_ratio);
461  }
462  }
463 
464  return 0;
465 }
466 
467 static void do_swizzle(AVFrame *frame, int x, int y)
468 {
469  int i;
470  for (i = 0; i < frame->linesize[0] * frame->height; i += 4) {
471  uint8_t *src = frame->data[0] + i;
472  FFSWAP(uint8_t, src[x], src[y]);
473  }
474 }
475 
476 static void run_postproc(AVCodecContext *avctx, AVFrame *frame)
477 {
478  DDSContext *ctx = avctx->priv_data;
479  int i, x_off;
480 
481  switch (ctx->postproc) {
482  case DDS_ALPHA_EXP:
483  /* Alpha-exponential mode divides each channel by the maximum
484  * R, G or B value, and stores the multiplying factor in the
485  * alpha channel. */
486  av_log(avctx, AV_LOG_DEBUG, "Post-processing alpha exponent.\n");
487 
488  for (i = 0; i < frame->linesize[0] * frame->height; i += 4) {
489  uint8_t *src = frame->data[0] + i;
490  int r = src[0];
491  int g = src[1];
492  int b = src[2];
493  int a = src[3];
494 
495  src[0] = r * a / 255;
496  src[1] = g * a / 255;
497  src[2] = b * a / 255;
498  src[3] = 255;
499  }
500  break;
501  case DDS_NORMAL_MAP:
502  /* Normal maps work in the XYZ color space and they encode
503  * X in R or in A, depending on the texture type, Y in G and
504  * derive Z with a square root of the distance.
505  *
506  * http://www.realtimecollisiondetection.net/blog/?p=28 */
507  av_log(avctx, AV_LOG_DEBUG, "Post-processing normal map.\n");
508 
509  x_off = ctx->tex_ratio == 8 ? 0 : 3;
510  for (i = 0; i < frame->linesize[0] * frame->height; i += 4) {
511  uint8_t *src = frame->data[0] + i;
512  int x = src[x_off];
513  int y = src[1];
514  int z = 127;
515 
516  int d = (255 * 255 - x * x - y * y) / 2;
517  if (d > 0)
518  z = rint(sqrtf(d));
519 
520  src[0] = x;
521  src[1] = y;
522  src[2] = z;
523  src[3] = 255;
524  }
525  break;
526  case DDS_RAW_YCOCG:
527  /* Data is Y-Co-Cg-A and not RGBA, but they are represented
528  * with the same masks in the DDPF header. */
529  av_log(avctx, AV_LOG_DEBUG, "Post-processing raw YCoCg.\n");
530 
531  for (i = 0; i < frame->linesize[0] * frame->height; i += 4) {
532  uint8_t *src = frame->data[0] + i;
533  int a = src[0];
534  int cg = src[1] - 128;
535  int co = src[2] - 128;
536  int y = src[3];
537 
538  src[0] = av_clip_uint8(y + co - cg);
539  src[1] = av_clip_uint8(y + cg);
540  src[2] = av_clip_uint8(y - co - cg);
541  src[3] = a;
542  }
543  break;
544  case DDS_SWIZZLE_A2XY:
545  /* Swap R and G, often used to restore a standard RGTC2. */
546  av_log(avctx, AV_LOG_DEBUG, "Post-processing A2XY swizzle.\n");
547  do_swizzle(frame, 0, 1);
548  break;
549  case DDS_SWIZZLE_RBXG:
550  /* Swap G and A, then B and new A (G). */
551  av_log(avctx, AV_LOG_DEBUG, "Post-processing RBXG swizzle.\n");
552  do_swizzle(frame, 1, 3);
553  do_swizzle(frame, 2, 3);
554  break;
555  case DDS_SWIZZLE_RGXB:
556  /* Swap B and A. */
557  av_log(avctx, AV_LOG_DEBUG, "Post-processing RGXB swizzle.\n");
558  do_swizzle(frame, 2, 3);
559  break;
560  case DDS_SWIZZLE_RXBG:
561  /* Swap G and A. */
562  av_log(avctx, AV_LOG_DEBUG, "Post-processing RXBG swizzle.\n");
563  do_swizzle(frame, 1, 3);
564  break;
565  case DDS_SWIZZLE_RXGB:
566  /* Swap R and A (misleading name). */
567  av_log(avctx, AV_LOG_DEBUG, "Post-processing RXGB swizzle.\n");
568  do_swizzle(frame, 0, 3);
569  break;
570  case DDS_SWIZZLE_XGBR:
571  /* Swap B and A, then R and new A (B). */
572  av_log(avctx, AV_LOG_DEBUG, "Post-processing XGBR swizzle.\n");
573  do_swizzle(frame, 2, 3);
574  do_swizzle(frame, 0, 3);
575  break;
576  case DDS_SWIZZLE_XGXR:
577  /* Swap G and A, then R and new A (G), then new R (G) and new G (A).
578  * This variant does not store any B component. */
579  av_log(avctx, AV_LOG_DEBUG, "Post-processing XGXR swizzle.\n");
580  do_swizzle(frame, 1, 3);
581  do_swizzle(frame, 0, 3);
582  do_swizzle(frame, 0, 1);
583  break;
584  case DDS_SWIZZLE_XRBG:
585  /* Swap G and A, then R and new A (G). */
586  av_log(avctx, AV_LOG_DEBUG, "Post-processing XRBG swizzle.\n");
587  do_swizzle(frame, 1, 3);
588  do_swizzle(frame, 0, 3);
589  break;
590  }
591 }
592 
593 static int dds_decode(AVCodecContext *avctx, void *data,
594  int *got_frame, AVPacket *avpkt)
595 {
596  DDSContext *ctx = avctx->priv_data;
597  GetByteContext *gbc = &ctx->gbc;
598  AVFrame *frame = data;
599  int mipmap;
600  int ret;
601 
602  ff_texturedsp_init(&ctx->texdsp);
603  bytestream2_init(gbc, avpkt->data, avpkt->size);
604 
605  if (bytestream2_get_bytes_left(gbc) < 128) {
606  av_log(avctx, AV_LOG_ERROR, "Frame is too small (%d).\n",
608  return AVERROR_INVALIDDATA;
609  }
610 
611  if (bytestream2_get_le32(gbc) != MKTAG('D', 'D', 'S', ' ') ||
612  bytestream2_get_le32(gbc) != 124) { // header size
613  av_log(avctx, AV_LOG_ERROR, "Invalid DDS header.\n");
614  return AVERROR_INVALIDDATA;
615  }
616 
617  bytestream2_skip(gbc, 4); // flags
618 
619  avctx->height = bytestream2_get_le32(gbc);
620  avctx->width = bytestream2_get_le32(gbc);
621  ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
622  if (ret < 0) {
623  av_log(avctx, AV_LOG_ERROR, "Invalid image size %dx%d.\n",
624  avctx->width, avctx->height);
625  return ret;
626  }
627 
628  /* Since codec is based on 4x4 blocks, size is aligned to 4. */
629  avctx->coded_width = FFALIGN(avctx->width, TEXTURE_BLOCK_W);
630  avctx->coded_height = FFALIGN(avctx->height, TEXTURE_BLOCK_H);
631 
632  bytestream2_skip(gbc, 4); // pitch
633  bytestream2_skip(gbc, 4); // depth
634  mipmap = bytestream2_get_le32(gbc);
635  if (mipmap != 0)
636  av_log(avctx, AV_LOG_VERBOSE, "Found %d mipmaps (ignored).\n", mipmap);
637 
638  /* Extract pixel format information, considering additional elements
639  * in reserved1 and reserved2. */
640  ret = parse_pixel_format(avctx);
641  if (ret < 0)
642  return ret;
643 
644  ret = ff_get_buffer(avctx, frame, 0);
645  if (ret < 0)
646  return ret;
647 
648  if (ctx->compressed) {
649  int size = (avctx->coded_height / TEXTURE_BLOCK_H) *
650  (avctx->coded_width / TEXTURE_BLOCK_W) * ctx->tex_ratio;
651  ctx->slice_count = av_clip(avctx->thread_count, 1,
652  avctx->coded_height / TEXTURE_BLOCK_H);
653 
654  if (bytestream2_get_bytes_left(gbc) < size) {
655  av_log(avctx, AV_LOG_ERROR,
656  "Compressed Buffer is too small (%d < %d).\n",
657  bytestream2_get_bytes_left(gbc), size);
658  return AVERROR_INVALIDDATA;
659  }
660 
661  /* Use the decompress function on the texture, one block per thread. */
662  ctx->tex_data = gbc->buffer;
663  avctx->execute2(avctx, decompress_texture_thread, frame, NULL, ctx->slice_count);
664  } else {
665  int linesize = av_image_get_linesize(avctx->pix_fmt, frame->width, 0);
666 
667  if (ctx->paletted) {
668  int i;
669  uint32_t *p = (uint32_t*) frame->data[1];
670 
671  /* Use the first 1024 bytes as palette, then copy the rest. */
672  for (i = 0; i < 256; i++) {
673  uint32_t rgba = 0;
674  rgba |= bytestream2_get_byte(gbc) << 16;
675  rgba |= bytestream2_get_byte(gbc) << 8;
676  rgba |= bytestream2_get_byte(gbc) << 0;
677  rgba |= bytestream2_get_byte(gbc) << 24;
678  p[i] = rgba;
679  }
680 
681  frame->palette_has_changed = 1;
682  }
683 
684  if (bytestream2_get_bytes_left(gbc) < frame->height * linesize) {
685  av_log(avctx, AV_LOG_ERROR, "Buffer is too small (%d < %d).\n",
686  bytestream2_get_bytes_left(gbc), frame->height * linesize);
687  return AVERROR_INVALIDDATA;
688  }
689 
690  av_image_copy_plane(frame->data[0], frame->linesize[0],
691  gbc->buffer, linesize,
692  linesize, frame->height);
693  }
694 
695  /* Run any post processing here if needed. */
696  if (ctx->postproc != DDS_NONE)
697  run_postproc(avctx, frame);
698 
699  /* Frame is ready to be output. */
700  frame->pict_type = AV_PICTURE_TYPE_I;
701  frame->key_frame = 1;
702  *got_frame = 1;
703 
704  return avpkt->size;
705 }
706 
708  .name = "dds",
709  .long_name = NULL_IF_CONFIG_SMALL("DirectDraw Surface image decoder"),
710  .type = AVMEDIA_TYPE_VIDEO,
711  .id = AV_CODEC_ID_DDS,
712  .decode = dds_decode,
713  .priv_data_size = sizeof(DDSContext),
715  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE
716 };
packed YUV 4:2:2, 16bpp, Cb Y0 Cr Y1
Definition: pixfmt.h:78
int av_image_get_linesize(enum AVPixelFormat pix_fmt, int width, int plane)
Compute the size of an image line with format pix_fmt and width width for the plane plane...
Definition: imgutils.c:51
uint32_t fourcc
Definition: hwcontext_qsv.c:90
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
int compressed
Definition: dds.c:101
int size
int(* dxt5_block)(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
Definition: texturedsp.h:51
This structure describes decoded (raw) audio or video data.
Definition: frame.h:140
static double rint(double x)
Definition: libm.h:130
8 bits gray, 8 bits alpha
Definition: pixfmt.h:143
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:1595
misc image utilities
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:130
Texture block (4x4) module.
packed RGB 5:5:5, 16bpp, (msb)1A 5R 5G 5B(lsb), little-endian, most significant bit to 0 ...
Definition: pixfmt.h:112
int size
Definition: avcodec.h:1347
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
#define AV_PIX_FMT_BGRA64
Definition: pixfmt.h:258
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1621
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:132
int slice_count
Definition: dds.c:107
size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
Put a string representing the codec tag codec_tag in buf.
Definition: utils.c:2071
enum DDSPostProc postproc
Definition: dds.c:103
int tex_ratio
Definition: dds.c:106
static int parse_pixel_format(AVCodecContext *avctx)
Definition: dds.c:113
int stride
Definition: mace.c:144
AVCodec.
Definition: avcodec.h:3120
order of coefficients is actually GBR, also IEC 61966-2-1 (sRGB)
Definition: pixfmt.h:344
TextureDSPContext texdsp
Definition: dds.c:98
packed RGB 5:6:5, 16bpp, (msb) 5R 6G 5B(lsb), little-endian
Definition: pixfmt.h:110
static int16_t block[64]
Definition: dct.c:97
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:40
uint8_t
#define DDPF_NORMALMAP
Definition: dds.c:41
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:70
Definition: dds.c:44
Multithreading support functions.
#define b
Definition: input.c:52
int(* dxt5y_block)(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
Definition: texturedsp.h:52
#define TEXTURE_BLOCK_H
Definition: texturedsp.h:43
const char data[16]
Definition: mxf.c:70
uint8_t * data
Definition: avcodec.h:1346
int(* dxt3_block)(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
Definition: texturedsp.h:49
const uint8_t * buffer
Definition: bytestream.h:33
static int flags
Definition: log.c:50
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:140
#define DDPF_FOURCC
Definition: dds.c:39
int(* rgtc2s_block)(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
Definition: texturedsp.h:56
#define FFALIGN(x, a)
Definition: macros.h:48
#define r
Definition: input.c:51
DDSPostProc
Definition: dds.c:43
#define src
Definition: vp8dsp.c:254
int(* dxt5ys_block)(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
Definition: texturedsp.h:53
int width
width and height of the video frame
Definition: frame.h:179
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
AVCodec ff_dds_decoder
Definition: dds.c:707
int(* rgtc1u_block)(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
Definition: texturedsp.h:55
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:161
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:148
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:92
g
Definition: yuv2rgb.c:546
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:145
GetByteContext gbc
Definition: dds.c:99
Definition: dds.c:97
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:151
const char * name
Name of the codec implementation.
Definition: avcodec.h:3127
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:90
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:223
av_cold void ff_texturedsp_init(TextureDSPContext *c)
Definition: texturedsp.c:598
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:201
int(* dxt1a_block)(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
Definition: texturedsp.h:47
#define FFMIN(a, b)
Definition: common.h:66
int paletted
Definition: dds.c:102
int width
picture width / height.
Definition: avcodec.h:1580
AVFormatContext * ctx
Definition: movenc.c:48
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:62
int(* dxt2_block)(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
Definition: texturedsp.h:48
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
Definition: avcodec.h:2806
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition: avcodec.h:897
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:2866
NULL
Definition: eval.c:55
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:158
main external API structure.
Definition: avcodec.h:1409
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:589
packed YUV 4:2:2, 16bpp, Y0 Cb Y1 Cr
Definition: pixfmt.h:60
static void do_swizzle(AVFrame *frame, int x, int y)
Definition: dds.c:467
int coded_height
Definition: avcodec.h:1595
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:2120
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:273
int(* tex_funct)(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
Definition: dds.c:110
int(* dxt4_block)(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
Definition: texturedsp.h:50
int(* rgtc1s_block)(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
Definition: texturedsp.h:54
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:146
Y , 1bpp, 0 is black, 1 is white, in each byte pixels are ordered from the msb to the lsb...
Definition: pixfmt.h:69
Y , 8bpp.
Definition: pixfmt.h:67
common internal api header.
int(* rgtc2u_block)(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
Definition: texturedsp.h:57
void * priv_data
Definition: avcodec.h:1451
Y , 16bpp, little-endian.
Definition: pixfmt.h:95
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:196
static int decompress_texture_thread(AVCodecContext *avctx, void *arg, int slice, int thread_nb)
Definition: dds.c:430
#define DDPF_PALETTE
Definition: dds.c:40
int height
Definition: frame.h:179
#define FFSWAP(type, a, b)
Definition: common.h:69
void av_image_copy_plane(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize, int bytewidth, int height)
Copy image plane from src to dst.
Definition: imgutils.c:255
#define MKTAG(a, b, c, d)
Definition: common.h:256
DDSDXGIFormat
Definition: dds.c:58
static void run_postproc(AVCodecContext *avctx, AVFrame *frame)
Definition: dds.c:476
#define TEXTURE_BLOCK_W
Definition: texturedsp.h:42
const uint8_t * tex_data
Definition: dds.c:105
int(* dxn3dc_block)(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
Definition: texturedsp.h:58
This structure stores compressed data.
Definition: avcodec.h:1323
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:838
for(j=16;j >0;--j)
static int dds_decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: dds.c:593