31#include <astcenc/astcenc.h>
42#define ASTC_MAGIC 0x5CA1AB13
43#define ASTC_HEADER_SIZE 16
44#define ASTC_BLOCK_BYTES 16
48 struct astcenc_config
cfg;
49 struct astcenc_context *
ctx;
58 enum astcenc_profile prf)
61 enum astcenc_error err;
64 astcenc_context_free(
s->ctx);
68 err = astcenc_config_init(prf,
s->block_x,
s->block_y,
s->block_z,
69 0, ASTCENC_FLG_DECOMPRESS_ONLY, &
s->cfg);
70 if (err != ASTCENC_SUCCESS) {
72 astcenc_get_error_string(err));
76 err = astcenc_context_alloc(&
s->cfg, 1, &
s->ctx,
NULL);
77 if (err != ASTCENC_SUCCESS) {
79 astcenc_get_error_string(err));
83 s->is_hdr = (prf == ASTCENC_PRF_HDR || prf == ASTCENC_PRF_HDR_RGB_LDR_A);
100 "astc decoder requires %d-byte .astc-style extradata.\n",
126 if (!
s->block_x || !
s->block_y || !
s->block_z ||
129 "Invalid ASTC header: block %dx%dx%d, image %dx%d.\n",
130 s->block_x,
s->block_y,
s->block_z,
141 enum astcenc_profile prf = (
enum astcenc_profile)
s->dec_profile;
142 if (
s->dec_profile < 0) {
145 prf = ASTCENC_PRF_LDR_SRGB;
148 prf = ASTCENC_PRF_LDR;
151 prf = ASTCENC_PRF_HDR_RGB_LDR_A;
154 prf = ASTCENC_PRF_HDR;
161 prf = ASTCENC_PRF_HDR_RGB_LDR_A;
166 prf = ASTCENC_PRF_LDR_SRGB;
178 const int w = avctx->
width;
180 const int elem =
s->is_hdr ? 2 : 1;
181 size_t row_bytes, buf_size, block_count, expected;
186 (
h - 1) /
s->block_y + 1, &block_count) < 0 ||
191 struct astcenc_swizzle swz = {
192 ASTCENC_SWZ_R, ASTCENC_SWZ_G, ASTCENC_SWZ_B, ASTCENC_SWZ_A
195 uint8_t *out_buf =
NULL;
197 struct astcenc_image
img;
198 enum astcenc_error err;
203 if ((
size_t)avpkt->
size != expected) {
205 "ASTC packet size %d does not match the %zu bytes required for "
206 "a %dx%d image with %dx%d blocks.\n",
207 avpkt->
size, expected,
w,
h,
s->block_x,
s->block_y);
219 if (
frame->linesize[0] == row_bytes) {
220 data_ptrs[0] =
frame->data[0];
225 data_ptrs[0] = out_buf;
231 img.data_type =
s->is_hdr ? ASTCENC_TYPE_F16 : ASTCENC_TYPE_U8;
232 img.data = data_ptrs;
234 err = astcenc_decompress_image(
s->ctx, avpkt->
data, avpkt->
size,
236 if (err != ASTCENC_SUCCESS) {
238 astcenc_get_error_string(err));
244 for (
int y = 0; y <
h; y++)
245 memcpy(
frame->data[0] + (
size_t)y *
frame->linesize[0],
246 out_buf + (
size_t)y * row_bytes, row_bytes);
261 astcenc_context_free(
s->ctx);
267#define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
268#define OFFSET(x) offsetof(LibAstcDecContext, x)
271 {
"dec_profile",
"Decoder color profile (must match encoder)",
OFFSET(dec_profile),
273 {
"ldr",
"Linear LDR", 0,
AV_OPT_TYPE_CONST, { .i64 = ASTCENC_PRF_LDR }, 0, 0,
VD, .unit =
"dec_profile" },
274 {
"ldr-srgb",
"sRGB LDR", 0,
AV_OPT_TYPE_CONST, { .i64 = ASTCENC_PRF_LDR_SRGB }, 0, 0,
VD, .unit =
"dec_profile" },
275 {
"hdr-ldr-a",
"HDR RGB, LDR alpha", 0,
AV_OPT_TYPE_CONST, { .i64 = ASTCENC_PRF_HDR_RGB_LDR_A }, 0, 0,
VD, .unit =
"dec_profile" },
276 {
"hdr",
"HDR", 0,
AV_OPT_TYPE_CONST, { .i64 = ASTCENC_PRF_HDR }, 0, 0,
VD, .unit =
"dec_profile" },
281 .class_name =
"libastcenc decoder",
288 .p.name =
"libastcenc",
289 CODEC_LONG_NAME(
"ASTC (Adaptive Scalable Texture Compression) image using astc-encoder"),
296 .p.wrapper_name =
"libastcenc",
const FFCodec ff_libastcenc_decoder
Libavcodec external API header.
#define CODEC_PIXFMTS(...)
#define FF_CODEC_CAP_NOT_INIT_THREADSAFE
The codec is not known to be init-threadsafe (i.e.
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
#define AV_PROFILE_ASTC_LDR
Linear LDR.
#define AV_PROFILE_ASTC_HDR
HDR RGB and alpha.
#define AV_PROFILE_ASTC_HDR_RGB_LDR_A
HDR RGB with LDR alpha.
#define AV_PROFILE_ASTC_LDR_SRGB
sRGB LDR.
#define AV_PROFILE_ASTC_LINEAR_ANY
int(* init)(AVBSFContext *ctx)
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
@ AV_OPT_TYPE_INT
Underlying C type is int.
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
const char * av_default_item_name(void *ptr)
Return the context name.
int av_size_mult(size_t a, size_t b, size_t *r)
Multiply two size_t values checking for overflow.
#define LIBAVUTIL_VERSION_INT
static av_cold int astcdec_configure(AVCodecContext *avctx, enum astcenc_profile prf)
static av_cold int libastcdec_init(AVCodecContext *avctx)
static const AVClass libastcenc_dec_class
static int libastcdec_decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *avpkt)
static av_cold int libastcdec_close(AVCodecContext *avctx)
static const AVOption dec_options[]
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Memory handling functions.
#define AV_PIX_FMT_RGBAF16
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
const AVProfile ff_astc_profiles[]
Describe the class of an AVClass context structure.
main external API structure.
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
int width
picture width / height.
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
This structure describes decoded (raw) audio or video data.
This structure stores compressed data.
struct astcenc_config cfg
struct astcenc_context * ctx