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