Browse code

Add FITS Decoder

Signed-off-by: Paras Chadha <paraschadha18@gmail.com>

Paras Chadha authored on 2017/08/30 02:16:44
Showing 10 changed files
... ...
@@ -38,6 +38,7 @@ version <next>:
38 38
 - Some video filters with several inputs now use a common set of options:
39 39
   blend, libvmaf, lut3d, overlay, psnr, ssim.
40 40
   They must always be used by name.
41
+- FITS demuxer and decoder
41 42
 
42 43
 version 3.3:
43 44
 - CrystalHD decoder moved to new decode API
... ...
@@ -593,6 +593,8 @@ following image formats are supported:
593 593
     @tab Digital Picture Exchange
594 594
 @item EXR          @tab   @tab X
595 595
     @tab OpenEXR
596
+@item FITS         @tab   @tab X
597
+    @tab Flexible Image Transport System
596 598
 @item JPEG         @tab X @tab X
597 599
     @tab Progressive JPEG is not supported.
598 600
 @item JPEG 2000    @tab X @tab X
... ...
@@ -291,6 +291,7 @@ OBJS-$(CONFIG_FFV1_DECODER)            += ffv1dec.o ffv1.o
291 291
 OBJS-$(CONFIG_FFV1_ENCODER)            += ffv1enc.o ffv1.o
292 292
 OBJS-$(CONFIG_FFWAVESYNTH_DECODER)     += ffwavesynth.o
293 293
 OBJS-$(CONFIG_FIC_DECODER)             += fic.o
294
+OBJS-$(CONFIG_FITS_DECODER)            += fitsdec.o fits.o
294 295
 OBJS-$(CONFIG_FLAC_DECODER)            += flacdec.o flacdata.o flac.o
295 296
 OBJS-$(CONFIG_FLAC_ENCODER)            += flacenc.o flacdata.o flac.o vorbis_data.o
296 297
 OBJS-$(CONFIG_FLASHSV_DECODER)         += flashsv.o
... ...
@@ -847,6 +848,7 @@ OBJS-$(CONFIG_ISO_MEDIA)               += mpeg4audio.o mpegaudiodata.o
847 847
 OBJS-$(CONFIG_ADTS_MUXER)              += mpeg4audio.o
848 848
 OBJS-$(CONFIG_CAF_DEMUXER)             += ac3tab.o
849 849
 OBJS-$(CONFIG_DNXHD_DEMUXER)           += dnxhddata.o
850
+OBJS-$(CONFIG_FITS_DEMUXER)            += fits.o
850 851
 OBJS-$(CONFIG_FLV_DEMUXER)             += mpeg4audio.o
851 852
 OBJS-$(CONFIG_LATM_MUXER)              += mpeg4audio.o
852 853
 OBJS-$(CONFIG_MATROSKA_AUDIO_MUXER)    += mpeg4audio.o
... ...
@@ -192,6 +192,7 @@ static void register_all(void)
192 192
     REGISTER_ENCDEC (FFV1,              ffv1);
193 193
     REGISTER_ENCDEC (FFVHUFF,           ffvhuff);
194 194
     REGISTER_DECODER(FIC,               fic);
195
+    REGISTER_DECODER(FITS,              fits);
195 196
     REGISTER_ENCDEC (FLASHSV,           flashsv);
196 197
     REGISTER_ENCDEC (FLASHSV2,          flashsv2);
197 198
     REGISTER_DECODER(FLIC,              flic);
... ...
@@ -447,6 +447,7 @@ enum AVCodecID {
447 447
     AV_CODEC_ID_SRGC,
448 448
     AV_CODEC_ID_SVG,
449 449
     AV_CODEC_ID_GDV,
450
+    AV_CODEC_ID_FITS,
450 451
 
451 452
     /* various PCM "codecs" */
452 453
     AV_CODEC_ID_FIRST_AUDIO = 0x10000,     ///< A dummy id pointing at the start of audio codecs
... ...
@@ -1464,6 +1464,13 @@ static const AVCodecDescriptor codec_descriptors[] = {
1464 1464
                      AV_CODEC_PROP_LOSSLESS,
1465 1465
     },
1466 1466
     {
1467
+        .id        = AV_CODEC_ID_FITS,
1468
+        .type      = AVMEDIA_TYPE_VIDEO,
1469
+        .name      = "fits",
1470
+        .long_name = NULL_IF_CONFIG_SMALL("FITS image"),
1471
+        .props     = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSLESS,
1472
+    },
1473
+    {
1467 1474
         .id        = AV_CODEC_ID_GIF,
1468 1475
         .type      = AVMEDIA_TYPE_VIDEO,
1469 1476
         .name      = "gif",
1470 1477
new file mode 100644
... ...
@@ -0,0 +1,203 @@
0
+/*
1
+ * FITS implementation of common functions
2
+ * Copyright (c) 2017 Paras Chadha
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#include "avcodec.h"
22
+#include "libavutil/dict.h"
23
+#include "fits.h"
24
+
25
+int avpriv_fits_header_init(FITSHeader *header, FITSHeaderState state)
26
+{
27
+    header->state = state;
28
+    header->naxis_index = 0;
29
+    header->blank_found = 0;
30
+    header->pcount = 0;
31
+    header->gcount = 1;
32
+    header->groups = 0;
33
+    header->rgb = 0;
34
+    header->image_extension = 0;
35
+    header->bscale = 1.0;
36
+    header->bzero = 0;
37
+    header->data_min_found = 0;
38
+    header->data_max_found = 0;
39
+    return 0;
40
+}
41
+
42
+static int dict_set_if_not_null(AVDictionary ***metadata, char *keyword, char *value)
43
+{
44
+    if (metadata)
45
+        av_dict_set(*metadata, keyword, value, 0);
46
+    return 0;
47
+}
48
+
49
+/**
50
+ * Extract keyword and value from a header line (80 bytes) and store them in keyword and value strings respectively
51
+ * @param ptr8 pointer to the data
52
+ * @param keyword pointer to the char array in which keyword is to be stored
53
+ * @param value pointer to the char array in which value is to be stored
54
+ * @return 0 if calculated successfully otherwise AVERROR_INVALIDDATA
55
+ */
56
+static int read_keyword_value(const uint8_t *ptr8, char *keyword, char *value)
57
+{
58
+    int i;
59
+
60
+    for (i = 0; i < 8 && ptr8[i] != ' '; i++) {
61
+        keyword[i] = ptr8[i];
62
+    }
63
+    keyword[i] = '\0';
64
+
65
+    if (ptr8[8] == '=') {
66
+        i = 10;
67
+        while (i < 80 && ptr8[i] == ' ') {
68
+            i++;
69
+        }
70
+
71
+        if (i < 80) {
72
+            *value++ = ptr8[i];
73
+            i++;
74
+            if (ptr8[i-1] == '\'') {
75
+                for (; i < 80 && ptr8[i] != '\''; i++) {
76
+                    *value++ = ptr8[i];
77
+                }
78
+                *value++ = '\'';
79
+            } else if (ptr8[i-1] == '(') {
80
+                for (; i < 80 && ptr8[i] != ')'; i++) {
81
+                    *value++ = ptr8[i];
82
+                }
83
+                *value++ = ')';
84
+            } else {
85
+                for (; i < 80 && ptr8[i] != ' ' && ptr8[i] != '/'; i++) {
86
+                    *value++ = ptr8[i];
87
+                }
88
+            }
89
+        }
90
+    }
91
+    *value = '\0';
92
+    return 0;
93
+}
94
+
95
+#define CHECK_KEYWORD(key) \
96
+    if (strcmp(keyword, key)) { \
97
+        av_log(avcl, AV_LOG_ERROR, "expected %s keyword, found %s = %s\n", key, keyword, value); \
98
+        return AVERROR_INVALIDDATA; \
99
+    }
100
+
101
+#define CHECK_VALUE(key, val) \
102
+    if (sscanf(value, "%d", &header->val) != 1) { \
103
+        av_log(avcl, AV_LOG_ERROR, "invalid value of %s keyword, %s = %s\n", key, keyword, value); \
104
+        return AVERROR_INVALIDDATA; \
105
+    }
106
+
107
+int avpriv_fits_header_parse_line(void *avcl, FITSHeader *header, const uint8_t line[80], AVDictionary ***metadata)
108
+{
109
+    int dim_no, ret;
110
+    int64_t t;
111
+    double d;
112
+    char keyword[10], value[72], c;
113
+
114
+    read_keyword_value(line, keyword, value);
115
+    switch (header->state) {
116
+    case STATE_SIMPLE:
117
+        CHECK_KEYWORD("SIMPLE");
118
+
119
+        if (value[0] == 'F') {
120
+            av_log(avcl, AV_LOG_WARNING, "not a standard FITS file\n");
121
+        } else if (value[0] != 'T') {
122
+            av_log(avcl, AV_LOG_ERROR, "invalid value of SIMPLE keyword, SIMPLE = %c\n", value[0]);
123
+            return AVERROR_INVALIDDATA;
124
+        }
125
+
126
+        header->state = STATE_BITPIX;
127
+        break;
128
+    case STATE_XTENSION:
129
+        CHECK_KEYWORD("XTENSION");
130
+
131
+        if (!strcmp(value, "'IMAGE   '")) {
132
+            header->image_extension = 1;
133
+        }
134
+
135
+        header->state = STATE_BITPIX;
136
+        break;
137
+    case STATE_BITPIX:
138
+        CHECK_KEYWORD("BITPIX");
139
+        CHECK_VALUE("BITPIX", bitpix);
140
+        dict_set_if_not_null(metadata, keyword, value);
141
+
142
+        header->state = STATE_NAXIS;
143
+        break;
144
+    case STATE_NAXIS:
145
+        CHECK_KEYWORD("NAXIS");
146
+        CHECK_VALUE("NAXIS", naxis);
147
+        dict_set_if_not_null(metadata, keyword, value);
148
+
149
+        if (header->naxis) {
150
+            header->state = STATE_NAXIS_N;
151
+        } else {
152
+            header->state = STATE_REST;
153
+        }
154
+        break;
155
+    case STATE_NAXIS_N:
156
+        ret = sscanf(keyword, "NAXIS%d", &dim_no);
157
+        if (ret != 1 || dim_no != header->naxis_index + 1) {
158
+            av_log(avcl, AV_LOG_ERROR, "expected NAXIS%d keyword, found %s = %s\n", header->naxis_index + 1, keyword, value);
159
+            return AVERROR_INVALIDDATA;
160
+        }
161
+
162
+        if (sscanf(value, "%d", &header->naxisn[header->naxis_index]) != 1) {
163
+            av_log(avcl, AV_LOG_ERROR, "invalid value of NAXIS%d keyword, %s = %s\n", header->naxis_index + 1, keyword, value);
164
+            return AVERROR_INVALIDDATA;
165
+        }
166
+
167
+        dict_set_if_not_null(metadata, keyword, value);
168
+        header->naxis_index++;
169
+        if (header->naxis_index == header->naxis) {
170
+            header->state = STATE_REST;
171
+        }
172
+        break;
173
+    case STATE_REST:
174
+        if (!strcmp(keyword, "BLANK") && sscanf(value, "%"SCNd64"", &t) == 1) {
175
+            header->blank = t;
176
+            header->blank_found = 1;
177
+        } else if (!strcmp(keyword, "BSCALE") && sscanf(value, "%lf", &d) == 1) {
178
+            header->bscale = d;
179
+        } else if (!strcmp(keyword, "BZERO") && sscanf(value, "%lf", &d) == 1) {
180
+            header->bzero = d;
181
+        } else if (!strcmp(keyword, "CTYPE3") && !strncmp(value, "'RGB", 4)) {
182
+            header->rgb = 1;
183
+        } else if (!strcmp(keyword, "DATAMAX") && sscanf(value, "%lf", &d) == 1) {
184
+            header->data_max_found = 1;
185
+            header->data_max = d;
186
+        } else if (!strcmp(keyword, "DATAMIN") && sscanf(value, "%lf", &d) == 1) {
187
+            header->data_min_found = 1;
188
+            header->data_min = d;
189
+        } else if (!strcmp(keyword, "END")) {
190
+            return 1;
191
+        } else if (!strcmp(keyword, "GROUPS") && sscanf(value, "%c", &c) == 1) {
192
+            header->groups = (c == 'T');
193
+        } else if (!strcmp(keyword, "GCOUNT") && sscanf(value, "%"SCNd64"", &t) == 1) {
194
+            header->gcount = t;
195
+        } else if (!strcmp(keyword, "PCOUNT") && sscanf(value, "%"SCNd64"", &t) == 1) {
196
+            header->pcount = t;
197
+        }
198
+        dict_set_if_not_null(metadata, keyword, value);
199
+        break;
200
+    }
201
+    return 0;
202
+}
0 203
new file mode 100644
... ...
@@ -0,0 +1,79 @@
0
+/*
1
+ * FITS image format common prototypes and structures
2
+ * Copyright (c) 2017 Paras Chadha
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#ifndef AVCODEC_FITS_H
22
+#define AVCODEC_FITS_H
23
+
24
+typedef enum FITSHeaderState {
25
+    STATE_SIMPLE,
26
+    STATE_XTENSION,
27
+    STATE_BITPIX,
28
+    STATE_NAXIS,
29
+    STATE_NAXIS_N,
30
+    STATE_PCOUNT,
31
+    STATE_GCOUNT,
32
+    STATE_REST,
33
+} FITSHeaderState;
34
+
35
+/**
36
+ * Structure to store the header keywords in FITS file
37
+ */
38
+typedef struct FITSHeader {
39
+    FITSHeaderState state;
40
+    unsigned naxis_index;
41
+    int bitpix;
42
+    int64_t blank;
43
+    int blank_found;
44
+    int naxis;
45
+    int naxisn[999];
46
+    int pcount;
47
+    int gcount;
48
+    int groups;
49
+    int rgb; /**< 1 if file contains RGB image, 0 otherwise */
50
+    int image_extension;
51
+    double bscale;
52
+    double bzero;
53
+    int data_min_found;
54
+    double data_min;
55
+    int data_max_found;
56
+    double data_max;
57
+} FITSHeader;
58
+
59
+
60
+/**
61
+ * Initialize a single header line
62
+ * @param header pointer to the header
63
+ * @param state current state of parsing the header
64
+ * @return 0 if successful otherwise AVERROR_INVALIDDATA
65
+ */
66
+int avpriv_fits_header_init(FITSHeader *header, FITSHeaderState state);
67
+
68
+/**
69
+ * Parse a single header line
70
+ * @param avcl used in av_log
71
+ * @param header pointer to the header
72
+ * @param line one header line
73
+ * @param metadata used to store metadata while decoding
74
+ * @return 0 if successful otherwise AVERROR_INVALIDDATA
75
+ */
76
+int avpriv_fits_header_parse_line(void *avcl, FITSHeader *header, const uint8_t line[80], AVDictionary ***metadata);
77
+
78
+#endif /* AVCODEC_FITS_H */
0 79
new file mode 100644
... ...
@@ -0,0 +1,318 @@
0
+/*
1
+ * FITS image decoder
2
+ * Copyright (c) 2017 Paras Chadha
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+/**
22
+ * @file
23
+ * FITS image decoder
24
+ *
25
+ * Specification: https://fits.gsfc.nasa.gov/fits_standard.html Version 3.0
26
+ *
27
+ * Support all 2d images alongwith, bzero, bscale and blank keywords.
28
+ * RGBA images are supported as NAXIS3 = 3 or 4 i.e. Planes in RGBA order. Also CTYPE = 'RGB ' should be present.
29
+ * Also to interpret data, values are linearly scaled using min-max scaling but not RGB images.
30
+ */
31
+
32
+#include "avcodec.h"
33
+#include "internal.h"
34
+#include <float.h>
35
+#include "libavutil/intreadwrite.h"
36
+#include "libavutil/intfloat.h"
37
+#include "libavutil/dict.h"
38
+#include "libavutil/opt.h"
39
+#include "fits.h"
40
+
41
+typedef struct FITSContext {
42
+    const AVClass *class;
43
+    int blank_val;
44
+} FITSContext;
45
+
46
+/**
47
+ * Calculate the data_min and data_max values from the data.
48
+ * This is called if the values are not present in the header.
49
+ * @param ptr8 pointer to the data
50
+ * @param header pointer to the header
51
+ * @param end pointer to end of packet
52
+ * @return 0 if calculated successfully otherwise AVERROR_INVALIDDATA
53
+ */
54
+static int fill_data_min_max(const uint8_t *ptr8, FITSHeader *header, const uint8_t *end)
55
+{
56
+    uint8_t t8;
57
+    int16_t t16;
58
+    int32_t t32;
59
+    int64_t t64;
60
+    float tflt;
61
+    double tdbl;
62
+    int i, j;
63
+
64
+    header->data_min = DBL_MAX;
65
+    header->data_max = DBL_MIN;
66
+    switch (header->bitpix) {
67
+#define CASE_N(a, t, rd) \
68
+    case a: \
69
+        for (i = 0; i < header->naxisn[1]; i++) { \
70
+            for (j = 0; j < header->naxisn[0]; j++) { \
71
+                t = rd; \
72
+                if (!header->blank_found || t != header->blank) { \
73
+                    if (t > header->data_max) \
74
+                        header->data_max = t; \
75
+                    if (t < header->data_min) \
76
+                        header->data_min = t; \
77
+                } \
78
+                ptr8 += abs(a) >> 3; \
79
+            } \
80
+        } \
81
+        break
82
+
83
+        CASE_N(-64, tdbl, av_int2double(AV_RB64(ptr8)));
84
+        CASE_N(-32, tflt, av_int2float(AV_RB32(ptr8)));
85
+        CASE_N(8, t8, ptr8[0]);
86
+        CASE_N(16, t16, AV_RB16(ptr8));
87
+        CASE_N(32, t32, AV_RB32(ptr8));
88
+        CASE_N(64, t64, AV_RB64(ptr8));
89
+        default:
90
+            return AVERROR_INVALIDDATA;
91
+    }
92
+    return 0;
93
+}
94
+
95
+/**
96
+ * Read the fits header and store the values in FITSHeader pointed by header
97
+ * @param avctx AVCodec context
98
+ * @param ptr pointer to pointer to the data
99
+ * @param header pointer to the FITSHeader
100
+ * @param end pointer to end of packet
101
+ * @param metadata pointer to pointer to AVDictionary to store metadata
102
+ * @return 0 if calculated successfully otherwise AVERROR_INVALIDDATA
103
+ */
104
+static int fits_read_header(AVCodecContext *avctx, const uint8_t **ptr, FITSHeader *header,
105
+                            const uint8_t *end, AVDictionary **metadata)
106
+{
107
+    const uint8_t *ptr8 = *ptr;
108
+    int lines_read, bytes_left, i, ret;
109
+    size_t size;
110
+
111
+    lines_read = 1; // to account for first header line, SIMPLE or XTENSION which is not included in packet...
112
+    avpriv_fits_header_init(header, STATE_BITPIX);
113
+    do {
114
+        if (end - ptr8 < 80)
115
+            return AVERROR_INVALIDDATA;
116
+        ret = avpriv_fits_header_parse_line(avctx, header, ptr8, &metadata);
117
+        ptr8 += 80;
118
+        lines_read++;
119
+    } while (!ret);
120
+    if (ret < 0)
121
+        return ret;
122
+
123
+    bytes_left = (((lines_read + 35) / 36) * 36 - lines_read) * 80;
124
+    if (end - ptr8 < bytes_left)
125
+        return AVERROR_INVALIDDATA;
126
+    ptr8 += bytes_left;
127
+
128
+    if (header->rgb && (header->naxis != 3 || (header->naxisn[2] != 3 && header->naxisn[2] != 4))) {
129
+        av_log(avctx, AV_LOG_ERROR, "File contains RGB image but NAXIS = %d and NAXIS3 = %d\n", header->naxis, header->naxisn[2]);
130
+        return AVERROR_INVALIDDATA;
131
+    }
132
+
133
+    if (!header->rgb && header->naxis != 2) {
134
+        av_log(avctx, AV_LOG_ERROR, "unsupported number of dimensions, NAXIS = %d\n", header->naxis);
135
+        return AVERROR_INVALIDDATA;
136
+    }
137
+
138
+    if (header->blank_found && (header->bitpix == -32 || header->bitpix == -64)) {
139
+        av_log(avctx, AV_LOG_WARNING, "BLANK keyword found but BITPIX = %d\n. Ignoring BLANK", header->bitpix);
140
+        header->blank_found = 0;
141
+    }
142
+
143
+    size = abs(header->bitpix) >> 3;
144
+    for (i = 0; i < header->naxis; i++) {
145
+        if (header->naxisn[i] > SIZE_MAX / size) {
146
+            av_log(avctx, AV_LOG_ERROR, "unsupported size of FITS image");
147
+            return AVERROR_INVALIDDATA;
148
+        }
149
+        size *= header->naxisn[i];
150
+    }
151
+
152
+    if (end - ptr8 < size)
153
+        return AVERROR_INVALIDDATA;
154
+    *ptr = ptr8;
155
+
156
+    if (!header->rgb && (!header->data_min_found || !header->data_max_found)) {
157
+        ret = fill_data_min_max(ptr8, header, end);
158
+        if (ret < 0) {
159
+            av_log(avctx, AV_LOG_ERROR, "invalid BITPIX, %d\n", header->bitpix);
160
+            return ret;
161
+        }
162
+    } else {
163
+        /*
164
+         * instead of applying bscale and bzero to every element,
165
+         * we can do inverse transformation on data_min and data_max
166
+         */
167
+        header->data_min = (header->data_min - header->bzero) / header->bscale;
168
+        header->data_max = (header->data_max - header->bzero) / header->bscale;
169
+    }
170
+
171
+    return 0;
172
+}
173
+
174
+static int fits_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
175
+{
176
+    AVFrame *p=data;
177
+    const uint8_t *ptr8 = avpkt->data, *end;
178
+    uint8_t t8;
179
+    int16_t t16;
180
+    int32_t t32;
181
+    int64_t t64;
182
+    float   tflt;
183
+    double  tdbl;
184
+    int ret, i, j, k;
185
+    const int map[] = {2, 0, 1, 3}; // mapping from GBRA -> RGBA as RGBA is to be stored in FITS file..
186
+    uint8_t *dst8;
187
+    uint16_t *dst16;
188
+    uint64_t t;
189
+    FITSHeader header;
190
+    FITSContext * fitsctx = avctx->priv_data;
191
+
192
+    end = ptr8 + avpkt->size;
193
+    p->metadata = NULL;
194
+    ret = fits_read_header(avctx, &ptr8, &header, end, &p->metadata);
195
+    if (ret < 0)
196
+        return ret;
197
+
198
+    if (header.rgb) {
199
+        if (header.bitpix == 8) {
200
+            if (header.naxisn[2] == 3) {
201
+                avctx->pix_fmt = AV_PIX_FMT_GBRP;
202
+            } else {
203
+                avctx->pix_fmt = AV_PIX_FMT_GBRAP;
204
+            }
205
+        } else if (header.bitpix == 16) {
206
+            if (header.naxisn[2] == 3) {
207
+                avctx->pix_fmt = AV_PIX_FMT_GBRP16;
208
+            } else {
209
+                avctx->pix_fmt = AV_PIX_FMT_GBRAP16;
210
+            }
211
+        } else {
212
+            av_log(avctx, AV_LOG_ERROR, "unsupported BITPIX = %d\n", header.bitpix);
213
+            return AVERROR_INVALIDDATA;
214
+        }
215
+    } else {
216
+        if (header.bitpix == 8) {
217
+            avctx->pix_fmt = AV_PIX_FMT_GRAY8;
218
+        } else {
219
+            avctx->pix_fmt = AV_PIX_FMT_GRAY16;
220
+        }
221
+    }
222
+
223
+    if ((ret = ff_set_dimensions(avctx, header.naxisn[0], header.naxisn[1])) < 0)
224
+        return ret;
225
+
226
+    if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
227
+        return ret;
228
+
229
+    /*
230
+     * FITS stores images with bottom row first. Therefore we have
231
+     * to fill the image from bottom to top.
232
+     */
233
+    if (header.rgb) {
234
+        switch(header.bitpix) {
235
+#define CASE_RGB(cas, dst, type, dref) \
236
+    case cas: \
237
+        for (k = 0; k < header.naxisn[2]; k++) { \
238
+            for (i = 0; i < avctx->height; i++) { \
239
+                dst = (type *) (p->data[map[k]] + (avctx->height - i - 1) * p->linesize[map[k]]); \
240
+                for (j = 0; j < avctx->width; j++) { \
241
+                    t32 = dref(ptr8); \
242
+                    if (!header.blank_found || t32 != header.blank) { \
243
+                        t = t32 * header.bscale + header.bzero; \
244
+                    } else { \
245
+                        t = fitsctx->blank_val; \
246
+                    } \
247
+                    *dst++ = (type) t; \
248
+                    ptr8 += cas >> 3; \
249
+                } \
250
+            } \
251
+        } \
252
+        break
253
+
254
+            CASE_RGB(8, dst8, uint8_t, *);
255
+            CASE_RGB(16, dst16, uint16_t, AV_RB16);
256
+        }
257
+    } else {
258
+        switch (header.bitpix) {
259
+#define CASE_GRAY(cas, dst, type, t, rd) \
260
+    case cas: \
261
+        for (i = 0; i < avctx->height; i++) { \
262
+            dst = (type *) (p->data[0] + (avctx->height-i-1)* p->linesize[0]); \
263
+            for (j = 0; j < avctx->width; j++) { \
264
+                t = rd; \
265
+                if (!header.blank_found || t != header.blank) { \
266
+                    t = ((t - header.data_min) * ((1 << (sizeof(type) * 8)) - 1)) / (header.data_max - header.data_min); \
267
+                } else { \
268
+                    t = fitsctx->blank_val; \
269
+                } \
270
+                *dst++ = (type) t; \
271
+                ptr8 += abs(cas) >> 3; \
272
+            } \
273
+        } \
274
+        break
275
+
276
+            CASE_GRAY(-64, dst16, uint16_t, tdbl, av_int2double(AV_RB64(ptr8)));
277
+            CASE_GRAY(-32, dst16, uint16_t, tflt, av_int2float(AV_RB32(ptr8)));
278
+            CASE_GRAY(8, dst8, uint8_t, t8, ptr8[0]);
279
+            CASE_GRAY(16, dst16, uint16_t, t16, AV_RB16(ptr8));
280
+            CASE_GRAY(32, dst16, uint16_t, t32, AV_RB32(ptr8));
281
+            CASE_GRAY(64, dst16, uint16_t, t64, AV_RB64(ptr8));
282
+            default:
283
+                av_log(avctx, AV_LOG_ERROR, "invalid BITPIX, %d\n", header.bitpix);
284
+                return AVERROR_INVALIDDATA;
285
+        }
286
+    }
287
+
288
+    p->key_frame = 1;
289
+    p->pict_type = AV_PICTURE_TYPE_I;
290
+
291
+    *got_frame = 1;
292
+
293
+    return avpkt->size;
294
+}
295
+
296
+static const AVOption fits_options[] = {
297
+    { "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},
298
+    { NULL },
299
+};
300
+
301
+static const AVClass fits_decoder_class = {
302
+    .class_name = "FITS decoder",
303
+    .item_name  = av_default_item_name,
304
+    .option     = fits_options,
305
+    .version    = LIBAVUTIL_VERSION_INT,
306
+};
307
+
308
+AVCodec ff_fits_decoder = {
309
+    .name           = "fits",
310
+    .type           = AVMEDIA_TYPE_VIDEO,
311
+    .id             = AV_CODEC_ID_FITS,
312
+    .priv_data_size = sizeof(FITSContext),
313
+    .decode         = fits_decode_frame,
314
+    .capabilities   = AV_CODEC_CAP_DR1,
315
+    .long_name      = NULL_IF_CONFIG_SMALL("Flexible Image Transport System"),
316
+    .priv_class     = &fits_decoder_class
317
+};
... ...
@@ -28,8 +28,8 @@
28 28
 #include "libavutil/version.h"
29 29
 
30 30
 #define LIBAVCODEC_VERSION_MAJOR  57
31
-#define LIBAVCODEC_VERSION_MINOR 103
32
-#define LIBAVCODEC_VERSION_MICRO 101
31
+#define LIBAVCODEC_VERSION_MINOR 104
32
+#define LIBAVCODEC_VERSION_MICRO 100
33 33
 
34 34
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
35 35
                                                LIBAVCODEC_VERSION_MINOR, \