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