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;
106 
107  const uint8_t *tex_data; // Compressed texture
108  int tex_ratio; // Compression ratio
109  int slice_count; // Number of slices for threaded operations
110 
111  /* Pointer to the selected compress or decompress function. */
112  int (*tex_funct)(uint8_t *dst, ptrdiff_t stride, const uint8_t *block);
113 } DDSContext;
114 
116 {
117  DDSContext *ctx = avctx->priv_data;
118  GetByteContext *gbc = &ctx->gbc;
119  char buf[32];
120  uint32_t flags, fourcc, gimp_tag;
121  enum DDSDXGIFormat dxgi;
122  int size, bpp, r, g, b, a;
123  int alpha_exponent, ycocg_classic, ycocg_scaled, normal_map, array;
124 
125  /* Alternative DDS implementations use reserved1 as custom header. */
126  bytestream2_skip(gbc, 4 * 3);
127  gimp_tag = bytestream2_get_le32(gbc);
128  alpha_exponent = gimp_tag == MKTAG('A', 'E', 'X', 'P');
129  ycocg_classic = gimp_tag == MKTAG('Y', 'C', 'G', '1');
130  ycocg_scaled = gimp_tag == MKTAG('Y', 'C', 'G', '2');
131  bytestream2_skip(gbc, 4 * 7);
132 
133  /* Now the real DDPF starts. */
134  size = bytestream2_get_le32(gbc);
135  if (size != 32) {
136  av_log(avctx, AV_LOG_ERROR, "Invalid pixel format header %d.\n", size);
137  return AVERROR_INVALIDDATA;
138  }
139  flags = bytestream2_get_le32(gbc);
140  ctx->compressed = flags & DDPF_FOURCC;
141  ctx->paletted = flags & DDPF_PALETTE;
142  normal_map = flags & DDPF_NORMALMAP;
143  fourcc = bytestream2_get_le32(gbc);
144 
145  if (ctx->compressed && ctx->paletted) {
146  av_log(avctx, AV_LOG_WARNING,
147  "Disabling invalid palette flag for compressed dds.\n");
148  ctx->paletted = 0;
149  }
150 
151  bpp = bytestream2_get_le32(gbc); // rgbbitcount
152  r = bytestream2_get_le32(gbc); // rbitmask
153  g = bytestream2_get_le32(gbc); // gbitmask
154  b = bytestream2_get_le32(gbc); // bbitmask
155  a = bytestream2_get_le32(gbc); // abitmask
156 
157  bytestream2_skip(gbc, 4); // caps
158  bytestream2_skip(gbc, 4); // caps2
159  bytestream2_skip(gbc, 4); // caps3
160  bytestream2_skip(gbc, 4); // caps4
161  bytestream2_skip(gbc, 4); // reserved2
162 
163  av_get_codec_tag_string(buf, sizeof(buf), fourcc);
164  av_log(avctx, AV_LOG_VERBOSE, "fourcc %s bpp %d "
165  "r 0x%x g 0x%x b 0x%x a 0x%x\n", buf, bpp, r, g, b, a);
166  if (gimp_tag) {
167  av_get_codec_tag_string(buf, sizeof(buf), gimp_tag);
168  av_log(avctx, AV_LOG_VERBOSE, "and GIMP-DDS tag %s\n", buf);
169  }
170 
171  if (ctx->compressed)
172  avctx->pix_fmt = AV_PIX_FMT_RGBA;
173 
174  if (ctx->compressed) {
175  switch (fourcc) {
176  case MKTAG('D', 'X', 'T', '1'):
177  ctx->tex_ratio = 8;
178  ctx->tex_funct = ctx->texdsp.dxt1a_block;
179  break;
180  case MKTAG('D', 'X', 'T', '2'):
181  ctx->tex_ratio = 16;
182  ctx->tex_funct = ctx->texdsp.dxt2_block;
183  break;
184  case MKTAG('D', 'X', 'T', '3'):
185  ctx->tex_ratio = 16;
186  ctx->tex_funct = ctx->texdsp.dxt3_block;
187  break;
188  case MKTAG('D', 'X', 'T', '4'):
189  ctx->tex_ratio = 16;
190  ctx->tex_funct = ctx->texdsp.dxt4_block;
191  break;
192  case MKTAG('D', 'X', 'T', '5'):
193  ctx->tex_ratio = 16;
194  if (ycocg_scaled)
195  ctx->tex_funct = ctx->texdsp.dxt5ys_block;
196  else if (ycocg_classic)
197  ctx->tex_funct = ctx->texdsp.dxt5y_block;
198  else
199  ctx->tex_funct = ctx->texdsp.dxt5_block;
200  break;
201  case MKTAG('R', 'X', 'G', 'B'):
202  ctx->tex_ratio = 16;
203  ctx->tex_funct = ctx->texdsp.dxt5_block;
204  /* This format may be considered as a normal map,
205  * but it is handled differently in a separate postproc. */
206  ctx->postproc = DDS_SWIZZLE_RXGB;
207  normal_map = 0;
208  break;
209  case MKTAG('A', 'T', 'I', '1'):
210  case MKTAG('B', 'C', '4', 'U'):
211  ctx->tex_ratio = 8;
212  ctx->tex_funct = ctx->texdsp.rgtc1u_block;
213  break;
214  case MKTAG('B', 'C', '4', 'S'):
215  ctx->tex_ratio = 8;
216  ctx->tex_funct = ctx->texdsp.rgtc1s_block;
217  break;
218  case MKTAG('A', 'T', 'I', '2'):
219  /* RGT2 variant with swapped R and G (3Dc)*/
220  ctx->tex_ratio = 16;
221  ctx->tex_funct = ctx->texdsp.dxn3dc_block;
222  break;
223  case MKTAG('B', 'C', '5', 'U'):
224  ctx->tex_ratio = 16;
225  ctx->tex_funct = ctx->texdsp.rgtc2u_block;
226  break;
227  case MKTAG('B', 'C', '5', 'S'):
228  ctx->tex_ratio = 16;
229  ctx->tex_funct = ctx->texdsp.rgtc2s_block;
230  break;
231  case MKTAG('U', 'Y', 'V', 'Y'):
232  ctx->compressed = 0;
233  avctx->pix_fmt = AV_PIX_FMT_UYVY422;
234  break;
235  case MKTAG('Y', 'U', 'Y', '2'):
236  ctx->compressed = 0;
237  avctx->pix_fmt = AV_PIX_FMT_YUYV422;
238  break;
239  case MKTAG('P', '8', ' ', ' '):
240  /* ATI Palette8, same as normal palette */
241  ctx->compressed = 0;
242  ctx->paletted = 1;
243  avctx->pix_fmt = AV_PIX_FMT_PAL8;
244  break;
245  case MKTAG('D', 'X', '1', '0'):
246  /* DirectX 10 extra header */
247  dxgi = bytestream2_get_le32(gbc);
248  bytestream2_skip(gbc, 4); // resourceDimension
249  bytestream2_skip(gbc, 4); // miscFlag
250  array = bytestream2_get_le32(gbc);
251  bytestream2_skip(gbc, 4); // miscFlag2
252 
253  if (array != 0)
254  av_log(avctx, AV_LOG_VERBOSE,
255  "Found array of size %d (ignored).\n", array);
256 
257  /* Only BC[1-5] are actually compressed. */
258  ctx->compressed = (dxgi >= 70) && (dxgi <= 84);
259 
260  av_log(avctx, AV_LOG_VERBOSE, "DXGI format %d.\n", dxgi);
261  switch (dxgi) {
262  /* RGB types. */
269  avctx->pix_fmt = AV_PIX_FMT_BGRA64;
270  break;
272  avctx->colorspace = AVCOL_SPC_RGB;
278  avctx->pix_fmt = AV_PIX_FMT_BGRA;
279  break;
281  avctx->colorspace = AVCOL_SPC_RGB;
284  avctx->pix_fmt = AV_PIX_FMT_RGBA;
285  break;
287  avctx->colorspace = AVCOL_SPC_RGB;
290  avctx->pix_fmt = AV_PIX_FMT_RGBA; // opaque
291  break;
293  avctx->pix_fmt = AV_PIX_FMT_RGB565LE;
294  break;
295  /* Texture types. */
297  avctx->colorspace = AVCOL_SPC_RGB;
300  ctx->tex_ratio = 8;
301  ctx->tex_funct = ctx->texdsp.dxt1a_block;
302  break;
304  avctx->colorspace = AVCOL_SPC_RGB;
307  ctx->tex_ratio = 16;
308  ctx->tex_funct = ctx->texdsp.dxt3_block;
309  break;
311  avctx->colorspace = AVCOL_SPC_RGB;
314  ctx->tex_ratio = 16;
315  ctx->tex_funct = ctx->texdsp.dxt5_block;
316  break;
319  ctx->tex_ratio = 8;
320  ctx->tex_funct = ctx->texdsp.rgtc1u_block;
321  break;
323  ctx->tex_ratio = 8;
324  ctx->tex_funct = ctx->texdsp.rgtc1s_block;
325  break;
328  ctx->tex_ratio = 16;
329  ctx->tex_funct = ctx->texdsp.rgtc2u_block;
330  break;
332  ctx->tex_ratio = 16;
333  ctx->tex_funct = ctx->texdsp.rgtc2s_block;
334  break;
335  default:
336  av_log(avctx, AV_LOG_ERROR,
337  "Unsupported DXGI format %d.\n", dxgi);
338  return AVERROR_INVALIDDATA;
339  }
340  break;
341  default:
342  av_log(avctx, AV_LOG_ERROR, "Unsupported %s fourcc.\n", buf);
343  return AVERROR_INVALIDDATA;
344  }
345  } else if (ctx->paletted) {
346  if (bpp == 8) {
347  avctx->pix_fmt = AV_PIX_FMT_PAL8;
348  } else {
349  av_log(avctx, AV_LOG_ERROR, "Unsupported palette bpp %d.\n", bpp);
350  return AVERROR_INVALIDDATA;
351  }
352  } else {
353  /* 8 bpp */
354  if (bpp == 8 && r == 0xff && g == 0 && b == 0 && a == 0)
355  avctx->pix_fmt = AV_PIX_FMT_GRAY8;
356  /* 16 bpp */
357  else if (bpp == 16 && r == 0xff && g == 0 && b == 0 && a == 0xff00)
358  avctx->pix_fmt = AV_PIX_FMT_YA8;
359  else if (bpp == 16 && r == 0xffff && g == 0 && b == 0 && a == 0)
360  avctx->pix_fmt = AV_PIX_FMT_GRAY16LE;
361  else if (bpp == 16 && r == 0xf800 && g == 0x7e0 && b == 0x1f && a == 0)
362  avctx->pix_fmt = AV_PIX_FMT_RGB565LE;
363  /* 24 bpp */
364  else if (bpp == 24 && r == 0xff0000 && g == 0xff00 && b == 0xff && a == 0)
365  avctx->pix_fmt = AV_PIX_FMT_BGR24;
366  /* 32 bpp */
367  else if (bpp == 32 && r == 0xff0000 && g == 0xff00 && b == 0xff && a == 0)
368  avctx->pix_fmt = AV_PIX_FMT_BGR0; // opaque
369  else if (bpp == 32 && r == 0xff && g == 0xff00 && b == 0xff0000 && a == 0)
370  avctx->pix_fmt = AV_PIX_FMT_RGB0; // opaque
371  else if (bpp == 32 && r == 0xff0000 && g == 0xff00 && b == 0xff && a == 0xff000000)
372  avctx->pix_fmt = AV_PIX_FMT_BGRA;
373  else if (bpp == 32 && r == 0xff && g == 0xff00 && b == 0xff0000 && a == 0xff000000)
374  avctx->pix_fmt = AV_PIX_FMT_RGBA;
375  /* give up */
376  else {
377  av_log(avctx, AV_LOG_ERROR, "Unknown pixel format "
378  "[bpp %d r 0x%x g 0x%x b 0x%x a 0x%x].\n", bpp, r, g, b, a);
379  return AVERROR_INVALIDDATA;
380  }
381  }
382 
383  /* Set any remaining post-proc that should happen before frame is ready. */
384  if (alpha_exponent)
385  ctx->postproc = DDS_ALPHA_EXP;
386  else if (normal_map)
387  ctx->postproc = DDS_NORMAL_MAP;
388  else if (ycocg_classic && !ctx->compressed)
389  ctx->postproc = DDS_RAW_YCOCG;
390  else if (avctx->pix_fmt == AV_PIX_FMT_YA8)
391  ctx->postproc = DDS_SWAP_ALPHA;
392 
393  /* ATI/NVidia variants sometimes add swizzling in bpp. */
394  switch (bpp) {
395  case MKTAG('A', '2', 'X', 'Y'):
396  ctx->postproc = DDS_SWIZZLE_A2XY;
397  break;
398  case MKTAG('x', 'G', 'B', 'R'):
399  ctx->postproc = DDS_SWIZZLE_XGBR;
400  break;
401  case MKTAG('x', 'R', 'B', 'G'):
402  ctx->postproc = DDS_SWIZZLE_XRBG;
403  break;
404  case MKTAG('R', 'B', 'x', 'G'):
405  ctx->postproc = DDS_SWIZZLE_RBXG;
406  break;
407  case MKTAG('R', 'G', 'x', 'B'):
408  ctx->postproc = DDS_SWIZZLE_RGXB;
409  break;
410  case MKTAG('R', 'x', 'B', 'G'):
411  ctx->postproc = DDS_SWIZZLE_RXBG;
412  break;
413  case MKTAG('x', 'G', 'x', 'R'):
414  ctx->postproc = DDS_SWIZZLE_XGXR;
415  break;
416  case MKTAG('A', '2', 'D', '5'):
417  ctx->postproc = DDS_NORMAL_MAP;
418  break;
419  }
420 
421  return 0;
422 }
423 
425  int slice, int thread_nb)
426 {
427  DDSContext *ctx = avctx->priv_data;
428  AVFrame *frame = arg;
429  const uint8_t *d = ctx->tex_data;
430  int w_block = avctx->coded_width / TEXTURE_BLOCK_W;
431  int h_block = avctx->coded_height / TEXTURE_BLOCK_H;
432  int x, y;
433  int start_slice, end_slice;
434  int base_blocks_per_slice = h_block / ctx->slice_count;
435  int remainder_blocks = h_block % ctx->slice_count;
436 
437  /* When the frame height (in blocks) doesn't divide evenly between the
438  * number of slices, spread the remaining blocks evenly between the first
439  * operations */
440  start_slice = slice * base_blocks_per_slice;
441  /* Add any extra blocks (one per slice) that have been added before this slice */
442  start_slice += FFMIN(slice, remainder_blocks);
443 
444  end_slice = start_slice + base_blocks_per_slice;
445  /* Add an extra block if there are still remainder blocks to be accounted for */
446  if (slice < remainder_blocks)
447  end_slice++;
448 
449  for (y = start_slice; y < end_slice; y++) {
450  uint8_t *p = frame->data[0] + y * frame->linesize[0] * TEXTURE_BLOCK_H;
451  int off = y * w_block;
452  for (x = 0; x < w_block; x++) {
453  ctx->tex_funct(p + x * 16, frame->linesize[0],
454  d + (off + x) * ctx->tex_ratio);
455  }
456  }
457 
458  return 0;
459 }
460 
461 static void do_swizzle(AVFrame *frame, int x, int y)
462 {
463  int i;
464  for (i = 0; i < frame->linesize[0] * frame->height; i += 4) {
465  uint8_t *src = frame->data[0] + i;
466  FFSWAP(uint8_t, src[x], src[y]);
467  }
468 }
469 
471 {
472  DDSContext *ctx = avctx->priv_data;
473  int i, x_off;
474 
475  switch (ctx->postproc) {
476  case DDS_ALPHA_EXP:
477  /* Alpha-exponential mode divides each channel by the maximum
478  * R, G or B value, and stores the multiplying factor in the
479  * alpha channel. */
480  av_log(avctx, AV_LOG_DEBUG, "Post-processing alpha exponent.\n");
481 
482  for (i = 0; i < frame->linesize[0] * frame->height; i += 4) {
483  uint8_t *src = frame->data[0] + i;
484  int r = src[0];
485  int g = src[1];
486  int b = src[2];
487  int a = src[3];
488 
489  src[0] = r * a / 255;
490  src[1] = g * a / 255;
491  src[2] = b * a / 255;
492  src[3] = 255;
493  }
494  break;
495  case DDS_NORMAL_MAP:
496  /* Normal maps work in the XYZ color space and they encode
497  * X in R or in A, depending on the texture type, Y in G and
498  * derive Z with a square root of the distance.
499  *
500  * http://www.realtimecollisiondetection.net/blog/?p=28 */
501  av_log(avctx, AV_LOG_DEBUG, "Post-processing normal map.\n");
502 
503  x_off = ctx->tex_ratio == 8 ? 0 : 3;
504  for (i = 0; i < frame->linesize[0] * frame->height; i += 4) {
505  uint8_t *src = frame->data[0] + i;
506  int x = src[x_off];
507  int y = src[1];
508  int z = 127;
509 
510  int d = (255 * 255 - x * x - y * y) / 2;
511  if (d > 0)
512  z = lrint(sqrtf(d));
513 
514  src[0] = x;
515  src[1] = y;
516  src[2] = z;
517  src[3] = 255;
518  }
519  break;
520  case DDS_RAW_YCOCG:
521  /* Data is Y-Co-Cg-A and not RGBA, but they are represented
522  * with the same masks in the DDPF header. */
523  av_log(avctx, AV_LOG_DEBUG, "Post-processing raw YCoCg.\n");
524 
525  for (i = 0; i < frame->linesize[0] * frame->height; i += 4) {
526  uint8_t *src = frame->data[0] + i;
527  int a = src[0];
528  int cg = src[1] - 128;
529  int co = src[2] - 128;
530  int y = src[3];
531 
532  src[0] = av_clip_uint8(y + co - cg);
533  src[1] = av_clip_uint8(y + cg);
534  src[2] = av_clip_uint8(y - co - cg);
535  src[3] = a;
536  }
537  break;
538  case DDS_SWAP_ALPHA:
539  /* Alpha and Luma are stored swapped. */
540  av_log(avctx, AV_LOG_DEBUG, "Post-processing swapped Luma/Alpha.\n");
541 
542  for (i = 0; i < frame->linesize[0] * frame->height; i += 2) {
543  uint8_t *src = frame->data[0] + i;
544  FFSWAP(uint8_t, src[0], src[1]);
545  }
546  break;
547  case DDS_SWIZZLE_A2XY:
548  /* Swap R and G, often used to restore a standard RGTC2. */
549  av_log(avctx, AV_LOG_DEBUG, "Post-processing A2XY swizzle.\n");
550  do_swizzle(frame, 0, 1);
551  break;
552  case DDS_SWIZZLE_RBXG:
553  /* Swap G and A, then B and new A (G). */
554  av_log(avctx, AV_LOG_DEBUG, "Post-processing RBXG swizzle.\n");
555  do_swizzle(frame, 1, 3);
556  do_swizzle(frame, 2, 3);
557  break;
558  case DDS_SWIZZLE_RGXB:
559  /* Swap B and A. */
560  av_log(avctx, AV_LOG_DEBUG, "Post-processing RGXB swizzle.\n");
561  do_swizzle(frame, 2, 3);
562  break;
563  case DDS_SWIZZLE_RXBG:
564  /* Swap G and A. */
565  av_log(avctx, AV_LOG_DEBUG, "Post-processing RXBG swizzle.\n");
566  do_swizzle(frame, 1, 3);
567  break;
568  case DDS_SWIZZLE_RXGB:
569  /* Swap R and A (misleading name). */
570  av_log(avctx, AV_LOG_DEBUG, "Post-processing RXGB swizzle.\n");
571  do_swizzle(frame, 0, 3);
572  break;
573  case DDS_SWIZZLE_XGBR:
574  /* Swap B and A, then R and new A (B). */
575  av_log(avctx, AV_LOG_DEBUG, "Post-processing XGBR swizzle.\n");
576  do_swizzle(frame, 2, 3);
577  do_swizzle(frame, 0, 3);
578  break;
579  case DDS_SWIZZLE_XGXR:
580  /* Swap G and A, then R and new A (G), then new R (G) and new G (A).
581  * This variant does not store any B component. */
582  av_log(avctx, AV_LOG_DEBUG, "Post-processing XGXR swizzle.\n");
583  do_swizzle(frame, 1, 3);
584  do_swizzle(frame, 0, 3);
585  do_swizzle(frame, 0, 1);
586  break;
587  case DDS_SWIZZLE_XRBG:
588  /* Swap G and A, then R and new A (G). */
589  av_log(avctx, AV_LOG_DEBUG, "Post-processing XRBG swizzle.\n");
590  do_swizzle(frame, 1, 3);
591  do_swizzle(frame, 0, 3);
592  break;
593  }
594 }
595 
596 static int dds_decode(AVCodecContext *avctx, void *data,
597  int *got_frame, AVPacket *avpkt)
598 {
599  DDSContext *ctx = avctx->priv_data;
600  GetByteContext *gbc = &ctx->gbc;
601  AVFrame *frame = data;
602  int mipmap;
603  int ret;
604 
605  ff_texturedsp_init(&ctx->texdsp);
606  bytestream2_init(gbc, avpkt->data, avpkt->size);
607 
608  if (bytestream2_get_bytes_left(gbc) < 128) {
609  av_log(avctx, AV_LOG_ERROR, "Frame is too small (%d).\n",
611  return AVERROR_INVALIDDATA;
612  }
613 
614  if (bytestream2_get_le32(gbc) != MKTAG('D', 'D', 'S', ' ') ||
615  bytestream2_get_le32(gbc) != 124) { // header size
616  av_log(avctx, AV_LOG_ERROR, "Invalid DDS header.\n");
617  return AVERROR_INVALIDDATA;
618  }
619 
620  bytestream2_skip(gbc, 4); // flags
621 
622  avctx->height = bytestream2_get_le32(gbc);
623  avctx->width = bytestream2_get_le32(gbc);
624  ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
625  if (ret < 0) {
626  av_log(avctx, AV_LOG_ERROR, "Invalid image size %dx%d.\n",
627  avctx->width, avctx->height);
628  return ret;
629  }
630 
631  /* Since codec is based on 4x4 blocks, size is aligned to 4. */
632  avctx->coded_width = FFALIGN(avctx->width, TEXTURE_BLOCK_W);
633  avctx->coded_height = FFALIGN(avctx->height, TEXTURE_BLOCK_H);
634 
635  bytestream2_skip(gbc, 4); // pitch
636  bytestream2_skip(gbc, 4); // depth
637  mipmap = bytestream2_get_le32(gbc);
638  if (mipmap != 0)
639  av_log(avctx, AV_LOG_VERBOSE, "Found %d mipmaps (ignored).\n", mipmap);
640 
641  /* Extract pixel format information, considering additional elements
642  * in reserved1 and reserved2. */
643  ret = parse_pixel_format(avctx);
644  if (ret < 0)
645  return ret;
646 
647  ret = ff_get_buffer(avctx, frame, 0);
648  if (ret < 0)
649  return ret;
650 
651  if (ctx->compressed) {
652  int size = (avctx->coded_height / TEXTURE_BLOCK_H) *
653  (avctx->coded_width / TEXTURE_BLOCK_W) * ctx->tex_ratio;
654  ctx->slice_count = av_clip(avctx->thread_count, 1,
655  avctx->coded_height / TEXTURE_BLOCK_H);
656 
657  if (bytestream2_get_bytes_left(gbc) < size) {
658  av_log(avctx, AV_LOG_ERROR,
659  "Compressed Buffer is too small (%d < %d).\n",
660  bytestream2_get_bytes_left(gbc), size);
661  return AVERROR_INVALIDDATA;
662  }
663 
664  /* Use the decompress function on the texture, one block per thread. */
665  ctx->tex_data = gbc->buffer;
666  avctx->execute2(avctx, decompress_texture_thread, frame, NULL, ctx->slice_count);
667  } else {
668  int linesize = av_image_get_linesize(avctx->pix_fmt, frame->width, 0);
669 
670  if (ctx->paletted) {
671  int i;
672  /* Use the first 1024 bytes as palette, then copy the rest. */
673  bytestream2_get_buffer(gbc, frame->data[1], 256 * 4);
674  for (i = 0; i < 256; i++)
675  AV_WN32(frame->data[1] + i*4,
676  (frame->data[1][2+i*4]<<0)+
677  (frame->data[1][1+i*4]<<8)+
678  (frame->data[1][0+i*4]<<16)+
679  (frame->data[1][3+i*4]<<24)
680  );
681 
682  frame->palette_has_changed = 1;
683  }
684 
685  if (bytestream2_get_bytes_left(gbc) < frame->height * linesize) {
686  av_log(avctx, AV_LOG_ERROR, "Buffer is too small (%d < %d).\n",
687  bytestream2_get_bytes_left(gbc), frame->height * linesize);
688  return AVERROR_INVALIDDATA;
689  }
690 
691  av_image_copy_plane(frame->data[0], frame->linesize[0],
692  gbc->buffer, linesize,
693  linesize, frame->height);
694  }
695 
696  /* Run any post processing here if needed. */
697  if (avctx->pix_fmt == AV_PIX_FMT_BGRA ||
698  avctx->pix_fmt == AV_PIX_FMT_RGBA ||
699  avctx->pix_fmt == AV_PIX_FMT_RGB0 ||
700  avctx->pix_fmt == AV_PIX_FMT_BGR0 ||
701  avctx->pix_fmt == AV_PIX_FMT_YA8)
702  run_postproc(avctx, frame);
703 
704  /* Frame is ready to be output. */
705  frame->pict_type = AV_PICTURE_TYPE_I;
706  frame->key_frame = 1;
707  *got_frame = 1;
708 
709  return avpkt->size;
710 }
711 
713  .name = "dds",
714  .long_name = NULL_IF_CONFIG_SMALL("DirectDraw Surface image decoder"),
715  .type = AVMEDIA_TYPE_VIDEO,
716  .id = AV_CODEC_ID_DDS,
717  .decode = dds_decode,
718  .priv_data_size = sizeof(DDSContext),
720  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE
721 };
packed YUV 4:2:2, 16bpp, Cb Y0 Cr Y1
Definition: pixfmt.h:83
#define NULL
Definition: coverity.c:32
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
#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:181
int(* tex_funct)(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
Definition: dds.c:112
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
8bit gray, 8bit alpha
Definition: pixfmt.h:155
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:1726
AVFormatContext * ctx
Definition: movenc-test.c:48
misc image utilities
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
const char * g
Definition: vf_curves.c:108
Texture block (4x4) module.
int size
Definition: avcodec.h:1468
const char * b
Definition: vf_curves.c:109
#define AV_PIX_FMT_BGRA64
Definition: pixfmt.h:325
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1752
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:109
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:2668
enum DDSPostProc postproc
Definition: dds.c:105
int tex_ratio
Definition: dds.c:108
static int parse_pixel_format(AVCodecContext *avctx)
Definition: dds.c:115
AVCodec.
Definition: avcodec.h:3392
order of coefficients is actually GBR, also IEC 61966-2-1 (sRGB)
Definition: pixfmt.h:421
TextureDSPContext texdsp
Definition: dds.c:100
packed RGB 5:6:5, 16bpp, (msb) 5R 6G 5B(lsb), little-endian
Definition: pixfmt.h:115
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:40
uint8_t
#define DDPF_NORMALMAP
Definition: dds.c:42
packed RGB 8:8:8, 32bpp, RGBXRGBX... X=unused/undefined
Definition: pixfmt.h:246
8 bit with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:74
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:1467
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:230
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
AVCodec ff_dds_decoder
Definition: dds.c:712
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:97
const char * r
Definition: vf_curves.c:107
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:3399
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:95
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:252
#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:1711
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:66
#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:2954
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition: avcodec.h:923
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:209
main external API structure.
Definition: avcodec.h:1532
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:894
packed YUV 4:2:2, 16bpp, Y0 Cb Y1 Cr
Definition: pixfmt.h:64
static void do_swizzle(AVFrame *frame, int x, int y)
Definition: dds.c:461
void * buf
Definition: avisynth_c.h:553
Replacements for frequently missing libm functions.
int coded_height
Definition: avcodec.h:1726
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:2255
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:322
packed BGR 8:8:8, 32bpp, BGRXBGRX... X=unused/undefined
Definition: pixfmt.h:248
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:192
Y , 8bpp.
Definition: pixfmt.h:71
common internal api header.
#define AV_WN32(p, v)
Definition: intreadwrite.h:376
void * priv_data
Definition: avcodec.h:1574
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:3014
int(* rgtc2s_block)(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
Definition: texturedsp.h:56
Y , 16bpp, little-endian.
Definition: pixfmt.h:100
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:247
static int decompress_texture_thread(AVCodecContext *avctx, void *arg, int slice, int thread_nb)
Definition: dds.c:424
#define lrint
Definition: tablegen.h:53
#define DDPF_PALETTE
Definition: dds.c:41
int height
Definition: frame.h:230
#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:470
#define TEXTURE_BLOCK_W
Definition: texturedsp.h:42
const uint8_t * tex_data
Definition: dds.c:107
This structure stores compressed data.
Definition: avcodec.h:1444
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:856
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:596
static int16_t block[64]
Definition: dct-test.c:112