Coverage Report

Created: 2026-01-16 07:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/fitsdec.c
Line
Count
Source
1
/*
2
 * FITS image decoder
3
 * Copyright (c) 2017 Paras Chadha
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
 * FITS image decoder
25
 *
26
 * Specification: https://fits.gsfc.nasa.gov/fits_standard.html Version 3.0
27
 *
28
 * Support all 2d images alongwith, bzero, bscale and blank keywords.
29
 * RGBA images are supported as NAXIS3 = 3 or 4 i.e. Planes in RGBA order. Also CTYPE = 'RGB ' should be present.
30
 * Also to interpret data, values are linearly scaled using min-max scaling but not RGB images.
31
 */
32
33
#include "avcodec.h"
34
#include "codec_internal.h"
35
#include "decode.h"
36
#include <float.h>
37
#include "libavutil/intreadwrite.h"
38
#include "libavutil/intfloat.h"
39
#include "libavutil/dict.h"
40
#include "libavutil/opt.h"
41
#include "fits.h"
42
43
typedef struct FITSContext {
44
    const AVClass *class;
45
    int blank_val;
46
} FITSContext;
47
48
/**
49
 * Calculate the data_min and data_max values from the data.
50
 * This is called if the values are not present in the header.
51
 * @param ptr8 pointer to the data
52
 * @param header pointer to the header
53
 * @param end pointer to end of packet
54
 * @return 0 if calculated successfully otherwise AVERROR_INVALIDDATA
55
 */
56
static int fill_data_min_max(const uint8_t *ptr8, FITSHeader *header, const uint8_t *end)
57
7.89k
{
58
7.89k
    uint8_t t8;
59
7.89k
    int16_t t16;
60
7.89k
    int32_t t32;
61
7.89k
    int64_t t64;
62
7.89k
    float tflt;
63
7.89k
    double tdbl;
64
7.89k
    int i, j;
65
66
7.89k
    header->data_min = DBL_MAX;
67
7.89k
    header->data_max = -DBL_MAX;
68
7.89k
    switch (header->bitpix) {
69
0
#define CASE_N(a, t, rd) \
70
7.89k
    case a: \
71
75.1k
        for (i = 0; i < header->naxisn[1]; i++) { \
72
612k
            for (j = 0; j < header->naxisn[0]; j++) { \
73
544k
                t = rd; \
74
544k
                if (!header->blank_found || t != header->blank) { \
75
540k
                    if (t > header->data_max) \
76
540k
                        header->data_max = t; \
77
540k
                    if (t < header->data_min) \
78
540k
                        header->data_min = t; \
79
540k
                } \
80
544k
                ptr8 += abs(a) >> 3; \
81
544k
            } \
82
67.2k
        } \
83
7.89k
        break
84
85
704
        CASE_N(-64, tdbl, av_int2double(AV_RB64(ptr8)));
86
799
        CASE_N(-32, tflt, av_int2float(AV_RB32(ptr8)));
87
4.57k
        CASE_N(8, t8, ptr8[0]);
88
621
        CASE_N(16, t16, AV_RB16(ptr8));
89
590
        CASE_N(32, t32, AV_RB32(ptr8));
90
605
        CASE_N(64, t64, AV_RB64(ptr8));
91
0
        default:
92
0
            return AVERROR_INVALIDDATA;
93
7.89k
    }
94
7.89k
    return 0;
95
7.89k
}
96
97
/**
98
 * Read the fits header and store the values in FITSHeader pointed by header
99
 * @param avctx AVCodec context
100
 * @param ptr pointer to pointer to the data
101
 * @param header pointer to the FITSHeader
102
 * @param end pointer to end of packet
103
 * @param metadata pointer to pointer to AVDictionary to store metadata
104
 * @return 0 if calculated successfully otherwise AVERROR_INVALIDDATA
105
 */
106
static int fits_read_header(AVCodecContext *avctx, const uint8_t **ptr, FITSHeader *header,
107
                            const uint8_t *end, AVDictionary **metadata)
108
178k
{
109
178k
    const uint8_t *ptr8 = *ptr;
110
178k
    int lines_read, bytes_left, i, ret;
111
178k
    size_t size;
112
113
178k
    lines_read = 1; // to account for first header line, SIMPLE or XTENSION which is not included in packet...
114
178k
    avpriv_fits_header_init(header, STATE_BITPIX);
115
338k
    do {
116
338k
        if (end - ptr8 < 80)
117
162k
            return AVERROR_INVALIDDATA;
118
176k
        ret = avpriv_fits_header_parse_line(avctx, header, ptr8, &metadata);
119
176k
        ptr8 += 80;
120
176k
        lines_read++;
121
176k
    } while (!ret);
122
15.8k
    if (ret < 0)
123
6.09k
        return ret;
124
125
9.78k
    bytes_left = (((lines_read + 35) / 36) * 36 - lines_read) * 80;
126
9.78k
    if (end - ptr8 < bytes_left)
127
418
        return AVERROR_INVALIDDATA;
128
9.36k
    ptr8 += bytes_left;
129
130
9.36k
    if (header->rgb && (header->naxis != 3 || (header->naxisn[2] != 3 && header->naxisn[2] != 4))) {
131
197
        av_log(avctx, AV_LOG_ERROR, "File contains RGB image but NAXIS = %d and NAXIS3 = %d\n", header->naxis, header->naxisn[2]);
132
197
        return AVERROR_INVALIDDATA;
133
197
    }
134
135
9.16k
    if (!header->rgb && header->naxis != 2) {
136
209
        av_log(avctx, AV_LOG_ERROR, "unsupported number of dimensions, NAXIS = %d\n", header->naxis);
137
209
        return AVERROR_INVALIDDATA;
138
209
    }
139
140
8.96k
    if (header->blank_found && (header->bitpix == -32 || header->bitpix == -64)) {
141
465
        av_log(avctx, AV_LOG_WARNING, "BLANK keyword found but BITPIX = %d\n. Ignoring BLANK", header->bitpix);
142
465
        header->blank_found = 0;
143
465
    }
144
145
8.96k
    size = abs(header->bitpix) >> 3;
146
26.4k
    for (i = 0; i < header->naxis; i++) {
147
17.9k
        if (size == 0 || header->naxisn[i] > SIZE_MAX / size) {
148
394
            av_log(avctx, AV_LOG_ERROR, "unsupported size of FITS image");
149
394
            return AVERROR_INVALIDDATA;
150
394
        }
151
17.5k
        size *= header->naxisn[i];
152
17.5k
    }
153
154
8.56k
    if (end - ptr8 < size)
155
437
        return AVERROR_INVALIDDATA;
156
8.12k
    *ptr = ptr8;
157
158
8.12k
    if (!header->rgb && (!header->data_min_found || !header->data_max_found)) {
159
7.89k
        ret = fill_data_min_max(ptr8, header, end);
160
7.89k
        if (ret < 0) {
161
0
            av_log(avctx, AV_LOG_ERROR, "invalid BITPIX, %d\n", header->bitpix);
162
0
            return ret;
163
0
        }
164
7.89k
    } else {
165
        /*
166
         * instead of applying bscale and bzero to every element,
167
         * we can do inverse transformation on data_min and data_max
168
         */
169
233
        header->data_min = (header->data_min - header->bzero) / header->bscale;
170
233
        header->data_max = (header->data_max - header->bzero) / header->bscale;
171
233
    }
172
8.12k
    if (!header->rgb && header->data_min >= header->data_max) {
173
1.52k
        if (header->data_min > header->data_max) {
174
1.21k
            av_log(avctx, AV_LOG_ERROR, "data min/max (%g %g) is invalid\n", header->data_min, header->data_max);
175
1.21k
            return AVERROR_INVALIDDATA;
176
1.21k
        }
177
317
        av_log(avctx, AV_LOG_WARNING, "data min/max indicates a blank image\n");
178
317
        header->data_max ++;
179
317
    }
180
181
6.91k
    return 0;
182
8.12k
}
183
184
static int fits_decode_frame(AVCodecContext *avctx, AVFrame *p,
185
                             int *got_frame, AVPacket *avpkt)
186
178k
{
187
178k
    const uint8_t *ptr8 = avpkt->data, *end;
188
178k
    uint8_t t8;
189
178k
    int16_t t16;
190
178k
    int32_t t32;
191
178k
    int64_t t64;
192
178k
    float   tflt;
193
178k
    double  tdbl;
194
178k
    int ret, i, j, k;
195
178k
    const int map[] = {2, 0, 1, 3}; // mapping from GBRA -> RGBA as RGBA is to be stored in FITS file..
196
178k
    uint8_t *dst8;
197
178k
    uint16_t *dst16;
198
178k
    uint64_t t;
199
178k
    FITSHeader header;
200
178k
    FITSContext * fitsctx = avctx->priv_data;
201
202
178k
    end = ptr8 + avpkt->size;
203
178k
    p->metadata = NULL;
204
178k
    ret = fits_read_header(avctx, &ptr8, &header, end, &p->metadata);
205
178k
    if (ret < 0)
206
171k
        return ret;
207
208
6.91k
    if (header.rgb) {
209
0
        if (header.bitpix == 8) {
210
0
            if (header.naxisn[2] == 3) {
211
0
                avctx->pix_fmt = AV_PIX_FMT_GBRP;
212
0
            } else {
213
0
                avctx->pix_fmt = AV_PIX_FMT_GBRAP;
214
0
            }
215
0
        } else if (header.bitpix == 16) {
216
0
            if (header.naxisn[2] == 3) {
217
0
                avctx->pix_fmt = AV_PIX_FMT_GBRP16;
218
0
            } else {
219
0
                avctx->pix_fmt = AV_PIX_FMT_GBRAP16;
220
0
            }
221
0
        } else {
222
0
            av_log(avctx, AV_LOG_ERROR, "unsupported BITPIX = %d\n", header.bitpix);
223
0
            return AVERROR_INVALIDDATA;
224
0
        }
225
6.91k
    } else {
226
6.91k
        if (header.bitpix == 8) {
227
4.38k
            avctx->pix_fmt = AV_PIX_FMT_GRAY8;
228
4.38k
        } else {
229
2.53k
            avctx->pix_fmt = AV_PIX_FMT_GRAY16;
230
2.53k
        }
231
6.91k
    }
232
233
6.91k
    if ((ret = ff_set_dimensions(avctx, header.naxisn[0], header.naxisn[1])) < 0)
234
233
        return ret;
235
236
6.68k
    if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
237
0
        return ret;
238
239
    /*
240
     * FITS stores images with bottom row first. Therefore we have
241
     * to fill the image from bottom to top.
242
     */
243
6.68k
    if (header.rgb) {
244
0
        switch(header.bitpix) {
245
0
#define CASE_RGB(cas, dst, type, dref) \
246
0
    case cas: \
247
0
        for (k = 0; k < header.naxisn[2]; k++) { \
248
0
            for (i = 0; i < avctx->height; i++) { \
249
0
                dst = (type *) (p->data[map[k]] + (avctx->height - i - 1) * p->linesize[map[k]]); \
250
0
                for (j = 0; j < avctx->width; j++) { \
251
0
                    t32 = dref(ptr8); \
252
0
                    if (!header.blank_found || t32 != header.blank) { \
253
0
                        t = t32 * header.bscale + header.bzero; \
254
0
                    } else { \
255
0
                        t = fitsctx->blank_val; \
256
0
                    } \
257
0
                    *dst++ = (type) t; \
258
0
                    ptr8 += cas >> 3; \
259
0
                } \
260
0
            } \
261
0
        } \
262
0
        break
263
264
0
            CASE_RGB(8, dst8, uint8_t, *);
265
0
            CASE_RGB(16, dst16, uint16_t, AV_RB16);
266
0
        }
267
6.68k
    } else {
268
6.68k
        double scale = header.data_max - header.data_min;
269
270
6.68k
        if (scale <= 0 || !isfinite(scale)) {
271
408
            scale = 1;
272
408
        }
273
6.68k
        scale = 1/scale;
274
275
6.68k
        switch (header.bitpix) {
276
0
#define CASE_GRAY(cas, dst, type, t, rd) \
277
6.68k
    case cas: \
278
73.9k
        for (i = 0; i < avctx->height; i++) { \
279
67.2k
            dst = (type *) (p->data[0] + (avctx->height-i-1)* p->linesize[0]); \
280
612k
            for (j = 0; j < avctx->width; j++) { \
281
544k
                t = rd; \
282
544k
                if (!header.blank_found || t != header.blank) { \
283
540k
                    *dst++ = lrint(((t - header.data_min) * ((1 << (sizeof(type) * 8)) - 1)) * scale); \
284
540k
                } else { \
285
4.62k
                    *dst++ = fitsctx->blank_val; \
286
4.62k
                } \
287
544k
                ptr8 += abs(cas) >> 3; \
288
544k
            } \
289
67.2k
        } \
290
6.68k
        break
291
292
468
            CASE_GRAY(-64, dst16, uint16_t, tdbl, av_int2double(AV_RB64(ptr8)));
293
605
            CASE_GRAY(-32, dst16, uint16_t, tflt, av_int2float(AV_RB32(ptr8)));
294
4.38k
            CASE_GRAY(8, dst8, uint8_t, t8, ptr8[0]);
295
424
            CASE_GRAY(16, dst16, uint16_t, t16, AV_RB16(ptr8));
296
393
            CASE_GRAY(32, dst16, uint16_t, t32, AV_RB32(ptr8));
297
411
            CASE_GRAY(64, dst16, uint16_t, t64, AV_RB64(ptr8));
298
0
            default:
299
0
                av_log(avctx, AV_LOG_ERROR, "invalid BITPIX, %d\n", header.bitpix);
300
0
                return AVERROR_INVALIDDATA;
301
6.68k
        }
302
6.68k
    }
303
304
6.68k
    *got_frame = 1;
305
306
6.68k
    return avpkt->size;
307
6.68k
}
308
309
static const AVOption fits_options[] = {
310
    { "blank_value", "value that is used to replace BLANK pixels in data array", offsetof(FITSContext, blank_val), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 65535, AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM},
311
    { NULL },
312
};
313
314
static const AVClass fits_decoder_class = {
315
    .class_name = "FITS decoder",
316
    .item_name  = av_default_item_name,
317
    .option     = fits_options,
318
    .version    = LIBAVUTIL_VERSION_INT,
319
    .category   = AV_CLASS_CATEGORY_DECODER,
320
};
321
322
const FFCodec ff_fits_decoder = {
323
    .p.name         = "fits",
324
    .p.type         = AVMEDIA_TYPE_VIDEO,
325
    .p.id           = AV_CODEC_ID_FITS,
326
    .p.capabilities = AV_CODEC_CAP_DR1,
327
    CODEC_LONG_NAME("Flexible Image Transport System"),
328
    .p.priv_class   = &fits_decoder_class,
329
    .priv_data_size = sizeof(FITSContext),
330
    FF_CODEC_DECODE_CB(fits_decode_frame),
331
};