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
42
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 */
57 {
58 uint8_t t8;
59 int16_t t16;
62 float tflt;
63 double tdbl;
65
66 header->data_min = DBL_MAX;
67 header->data_max = -DBL_MAX;
69 #define CASE_N(a, t, rd) \
70 case a: \
71 for (i = 0; i < header->naxisn[1]; i++) { \
72 for (j = 0; j < header->naxisn[0]; j++) { \
73 t = rd; \
74 if (!header->blank_found || t != header->blank) { \
75 if (t > header->data_max) \
76 header->data_max = t; \
77 if (t < header->data_min) \
78 header->data_min = t; \
79 } \
80 ptr8 += abs(a) >> 3; \
81 } \
82 } \
83 break
84
91 default:
93 }
94 return 0;
95 }
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 */
108 {
109 const uint8_t *ptr8 = *ptr;
110 int lines_read, bytes_left,
i,
ret;
112
113 lines_read = 1; // to account for first header line, SIMPLE or XTENSION which is not included in packet...
115 do {
116 if (end - ptr8 < 80)
119 ptr8 += 80;
120 lines_read++;
124
125 bytes_left = (((lines_read + 35) / 36) * 36 - lines_read) * 80;
126 if (end - ptr8 < bytes_left)
128 ptr8 += bytes_left;
129
133 }
134
138 }
139
143 }
144
150 }
152 }
153
154 if (end - ptr8 <
size)
156 *ptr = ptr8;
157
163 }
164 } 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 */
171 }
176 }
179 }
180
181 return 0;
182 }
183
186 {
187 const uint8_t *ptr8 = avpkt->
data, *end;
188 uint8_t t8;
189 int16_t t16;
192 float tflt;
193 double tdbl;
195 const int map[] = {2, 0, 1, 3};
// mapping from GBRA -> RGBA as RGBA is to be stored in FITS file..
196 uint8_t *dst8;
197 uint16_t *dst16;
198 uint64_t t;
201
202 end = ptr8 + avpkt->
size;
207
210 if (
header.naxisn[2] == 3) {
212 } else {
214 }
215 }
else if (
header.bitpix == 16) {
216 if (
header.naxisn[2] == 3) {
218 } else {
220 }
221 } else {
224 }
225 } else {
228 } else {
230 }
231 }
232
235
238
239 /*
240 * FITS stores images with bottom row first. Therefore we have
241 * to fill the image from bottom to top.
242 */
245 #define CASE_RGB(cas, dst, type, dref) \
246 case cas: \
247 for (k = 0; k < header.naxisn[2]; k++) { \
248 for (i = 0; i < avctx->height; i++) { \
249 dst = (type *) (p->data[map[k]] + (avctx->height - i - 1) * p->linesize[map[k]]); \
250 for (j = 0; j < avctx->width; j++) { \
251 t32 = dref(ptr8); \
252 if (!header.blank_found || t32 != header.blank) { \
253 t = t32 * header.bscale + header.bzero; \
254 } else { \
255 t = fitsctx->blank_val; \
256 } \
257 *dst++ = (type) t; \
258 ptr8 += cas >> 3; \
259 } \
260 } \
261 } \
262 break
263
266 }
267 } else {
269
272 }
274
276 #define CASE_GRAY(cas, dst, type, t, rd) \
277 case cas: \
278 for (i = 0; i < avctx->height; i++) { \
279 dst = (type *) (p->data[0] + (avctx->height-i-1)* p->linesize[0]); \
280 for (j = 0; j < avctx->width; j++) { \
281 t = rd; \
282 if (!header.blank_found || t != header.blank) { \
283 *dst++ = lrint(((t - header.data_min) * ((1 << (sizeof(type) * 8)) - 1)) * scale); \
284 } else { \
285 *dst++ = fitsctx->blank_val; \
286 } \
287 ptr8 += abs(cas) >> 3; \
288 } \
289 } \
290 break
291
294 CASE_GRAY(8, dst8, uint8_t, t8, ptr8[0]);
298 default:
301 }
302 }
303
304 *got_frame = 1;
305
307 }
308
312 };
313
320 };
321
331 };