FFmpeg
Loading...
Searching...
No Matches
astcdec.c
Go to the documentation of this file.
1/*
2 * ASTC demuxer (.astc container)
3 * Copyright (c) 2026 Jun Zhao
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 * ASTC (Adaptive Scalable Texture Compression) demuxer.
25 *
26 * Reads the 16-byte .astc header, exposes it as extradata to the decoder,
27 * and emits the raw ASTC bitstream as a single packet.
28 */
29
30#include "config_components.h"
31
32#include <inttypes.h>
33#include <limits.h>
34
35#include "avformat.h"
36#include "avio.h"
37#include "avio_internal.h"
38#include "demux.h"
39#include "internal.h"
41
42#define ASTC_HEADER_SIZE 16
43#define ASTC_BLOCK_BYTES 16
44static const uint8_t astc_magic[4] = { 0x13, 0xAB, 0xA1, 0x5C };
45
50
51static int astc_probe(const AVProbeData *p)
52{
53 if (p->buf_size >= ASTC_HEADER_SIZE &&
54 !memcmp(p->buf, astc_magic, sizeof(astc_magic)))
55 return AVPROBE_SCORE_MAX;
56 return 0;
57}
58
60{
61 AVIOContext *pb = s->pb;
62 ASTCDemuxerContext *astc = s->priv_data;
63 AVStream *st;
64 uint8_t hdr[ASTC_HEADER_SIZE];
65 unsigned int bx, by, bz, w, h, dz;
66 uint64_t expected;
67
68 if (ffio_read_size(pb, hdr, ASTC_HEADER_SIZE) < 0)
70
71 if (memcmp(hdr, astc_magic, sizeof(astc_magic))) {
72 av_log(s, AV_LOG_ERROR, "Not an ASTC file (bad magic).\n");
74 }
75
76 bx = hdr[4];
77 by = hdr[5];
78 bz = hdr[6];
79 w = AV_RL24(hdr + 7);
80 h = AV_RL24(hdr + 10);
81 dz = AV_RL24(hdr + 13);
82
83 /* Validate the geometry before trusting it to size the packet: the
84 * decoder only handles 2D images, and a zero block size would divide
85 * by zero when deriving the block count. */
86 if (!bx || !by || !bz || !w || !h || dz != 1) {
88 "Invalid ASTC header: block %ux%ux%u, image %ux%ux%u.\n",
89 bx, by, bz, w, h, dz);
91 }
92
93 /* Multiply the block counts in 64 bits: the dimensions fit 24 bits, but
94 * their product overflows 32 bits for large images, and a wrapped result
95 * would under-size the packet, or wrap to zero and look like EOF. */
96 expected = (((uint64_t)w + bx - 1) / bx) *
97 (((uint64_t)h + by - 1) / by) *
98 (((uint64_t)dz + bz - 1) / bz) * ASTC_BLOCK_BYTES;
99 if (expected > INT_MAX) {
100 av_log(s, AV_LOG_ERROR, "ASTC image is too large (%"PRIu64" bytes).\n",
101 expected);
102 return AVERROR_INVALIDDATA;
103 }
104 astc->image_size = expected;
105
107 if (!st)
108 return AVERROR(ENOMEM);
109
112 st->codecpar->width = w;
113 st->codecpar->height = h;
115
117 return AVERROR(ENOMEM);
118 memcpy(st->codecpar->extradata, hdr, ASTC_HEADER_SIZE);
119
120 /* Single image container. */
121 st->duration = 1;
122 avpriv_set_pts_info(st, 64, 1, 1);
123
124 return 0;
125}
126
128{
129 ASTCDemuxerContext *astc = s->priv_data;
130 int ret;
131
132 /* Keep reporting a detected truncation instead of falling through to EOF:
133 * a caller that stops at the first error must not see the broken image as
134 * a clean end of stream. */
135 if (astc->truncated)
136 return AVERROR_INVALIDDATA;
137
138 if (!astc->image_size)
139 return AVERROR_EOF;
140
141 /* Read exactly one image worth of blocks; a short read means the input
142 * is truncated, and any trailing data is not part of this image. A payload
143 * that is missing entirely reads as EOF, which has to be reported as the
144 * truncation it is rather than as the end of a valid stream. */
145 ret = av_get_packet(s->pb, pkt, astc->image_size);
146 if (ret == AVERROR_EOF)
147 ret = 0;
148 if (ret < 0)
149 return ret;
150 if (ret != astc->image_size) {
152 "Truncated ASTC image: got %d of %"PRIu64" bytes.\n",
153 ret, astc->image_size);
155 astc->truncated = 1;
156 return AVERROR_INVALIDDATA;
157 }
158
159 astc->image_size = 0;
160 pkt->stream_index = 0;
161 pkt->flags |= AV_PKT_FLAG_KEY;
162 return 0;
163}
164
166 .p.name = "astc",
167 .p.long_name = NULL_IF_CONFIG_SMALL("ASTC (Adaptive Scalable Texture Compression)"),
168 .p.mime_type = "image/astc",
169 .p.extensions = "astc",
170 .p.flags = AVFMT_NOTIMESTAMPS,
171 .priv_data_size = sizeof(ASTCDemuxerContext),
175};
const FFInputFormat ff_astc_demuxer
Definition astcdec.c:165
static int astc_read_header(AVFormatContext *s)
Definition astcdec.c:59
static const uint8_t astc_magic[4]
Definition astcdec.c:44
static int astc_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition astcdec.c:127
static int astc_probe(const AVProbeData *p)
Definition astcdec.c:51
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:834
Main libavformat public API header.
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:485
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition utils.c:98
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition avformat.h:500
Buffered I/O operations.
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:665
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
#define s(width, name)
Definition cbs_vp9.c:198
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
#define NULL
Definition coverity.c:32
static AVPacket * pkt
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
@ AV_CODEC_ID_ASTC
Definition codec_id.h:327
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition packet.c:434
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition packet.h:650
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#define AV_RL24(x)
#define ASTC_BLOCK_BYTES
Definition libastcdec.c:44
#define ASTC_HEADER_SIZE
Definition libastcdec.c:43
int ff_alloc_extradata(AVCodecParameters *par, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0.
Definition utils.c:237
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:97
uint8_t w
Definition llvidencdsp.c:39
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition pixfmt.h:100
uint64_t image_size
Definition astcdec.c:47
int height
The height of the video frame in pixels.
Definition codec_par.h:150
int width
The width of the video frame in pixels.
Definition codec_par.h:143
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition codec_par.h:71
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
Format I/O context.
Definition avformat.h:1335
Bytestream IO Context.
Definition avio.h:160
This structure stores compressed data.
Definition packet.h:580
This structure contains the data a format has to probe a file.
Definition avformat.h:473
Stream structure.
Definition avformat.h:768
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:791
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition avformat.h:827
#define av_log(a,...)