FFmpeg
fitsdec.c
Go to the documentation of this file.
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 "internal.h"
35 #include <float.h>
36 #include "libavutil/intreadwrite.h"
37 #include "libavutil/intfloat.h"
38 #include "libavutil/dict.h"
39 #include "libavutil/opt.h"
40 #include "fits.h"
41 
42 typedef struct FITSContext {
43  const AVClass *class;
44  int blank_val;
45 } FITSContext;
46 
47 /**
48  * Calculate the data_min and data_max values from the data.
49  * This is called if the values are not present in the header.
50  * @param ptr8 pointer to the data
51  * @param header pointer to the header
52  * @param end pointer to end of packet
53  * @return 0 if calculated successfully otherwise AVERROR_INVALIDDATA
54  */
55 static int fill_data_min_max(const uint8_t *ptr8, FITSHeader *header, const uint8_t *end)
56 {
57  uint8_t t8;
58  int16_t t16;
59  int32_t t32;
60  int64_t t64;
61  float tflt;
62  double tdbl;
63  int i, j;
64 
65  header->data_min = DBL_MAX;
66  header->data_max = DBL_MIN;
67  switch (header->bitpix) {
68 #define CASE_N(a, t, rd) \
69  case a: \
70  for (i = 0; i < header->naxisn[1]; i++) { \
71  for (j = 0; j < header->naxisn[0]; j++) { \
72  t = rd; \
73  if (!header->blank_found || t != header->blank) { \
74  if (t > header->data_max) \
75  header->data_max = t; \
76  if (t < header->data_min) \
77  header->data_min = t; \
78  } \
79  ptr8 += abs(a) >> 3; \
80  } \
81  } \
82  break
83 
84  CASE_N(-64, tdbl, av_int2double(AV_RB64(ptr8)));
85  CASE_N(-32, tflt, av_int2float(AV_RB32(ptr8)));
86  CASE_N(8, t8, ptr8[0]);
87  CASE_N(16, t16, AV_RB16(ptr8));
88  CASE_N(32, t32, AV_RB32(ptr8));
89  CASE_N(64, t64, AV_RB64(ptr8));
90  default:
91  return AVERROR_INVALIDDATA;
92  }
93  return 0;
94 }
95 
96 /**
97  * Read the fits header and store the values in FITSHeader pointed by header
98  * @param avctx AVCodec context
99  * @param ptr pointer to pointer to the data
100  * @param header pointer to the FITSHeader
101  * @param end pointer to end of packet
102  * @param metadata pointer to pointer to AVDictionary to store metadata
103  * @return 0 if calculated successfully otherwise AVERROR_INVALIDDATA
104  */
105 static int fits_read_header(AVCodecContext *avctx, const uint8_t **ptr, FITSHeader *header,
106  const uint8_t *end, AVDictionary **metadata)
107 {
108  const uint8_t *ptr8 = *ptr;
109  int lines_read, bytes_left, i, ret;
110  size_t size;
111 
112  lines_read = 1; // to account for first header line, SIMPLE or XTENSION which is not included in packet...
114  do {
115  if (end - ptr8 < 80)
116  return AVERROR_INVALIDDATA;
117  ret = avpriv_fits_header_parse_line(avctx, header, ptr8, &metadata);
118  ptr8 += 80;
119  lines_read++;
120  } while (!ret);
121  if (ret < 0)
122  return ret;
123 
124  bytes_left = (((lines_read + 35) / 36) * 36 - lines_read) * 80;
125  if (end - ptr8 < bytes_left)
126  return AVERROR_INVALIDDATA;
127  ptr8 += bytes_left;
128 
129  if (header->rgb && (header->naxis != 3 || (header->naxisn[2] != 3 && header->naxisn[2] != 4))) {
130  av_log(avctx, AV_LOG_ERROR, "File contains RGB image but NAXIS = %d and NAXIS3 = %d\n", header->naxis, header->naxisn[2]);
131  return AVERROR_INVALIDDATA;
132  }
133 
134  if (!header->rgb && header->naxis != 2) {
135  av_log(avctx, AV_LOG_ERROR, "unsupported number of dimensions, NAXIS = %d\n", header->naxis);
136  return AVERROR_INVALIDDATA;
137  }
138 
139  if (header->blank_found && (header->bitpix == -32 || header->bitpix == -64)) {
140  av_log(avctx, AV_LOG_WARNING, "BLANK keyword found but BITPIX = %d\n. Ignoring BLANK", header->bitpix);
141  header->blank_found = 0;
142  }
143 
144  size = abs(header->bitpix) >> 3;
145  for (i = 0; i < header->naxis; i++) {
146  if (size == 0 || header->naxisn[i] > SIZE_MAX / size) {
147  av_log(avctx, AV_LOG_ERROR, "unsupported size of FITS image");
148  return AVERROR_INVALIDDATA;
149  }
150  size *= header->naxisn[i];
151  }
152 
153  if (end - ptr8 < size)
154  return AVERROR_INVALIDDATA;
155  *ptr = ptr8;
156 
157  if (!header->rgb && (!header->data_min_found || !header->data_max_found)) {
158  ret = fill_data_min_max(ptr8, header, end);
159  if (ret < 0) {
160  av_log(avctx, AV_LOG_ERROR, "invalid BITPIX, %d\n", header->bitpix);
161  return ret;
162  }
163  } else {
164  /*
165  * instead of applying bscale and bzero to every element,
166  * we can do inverse transformation on data_min and data_max
167  */
168  header->data_min = (header->data_min - header->bzero) / header->bscale;
169  header->data_max = (header->data_max - header->bzero) / header->bscale;
170  }
171  if (!header->rgb && header->data_min >= header->data_max) {
172  if (header->data_min > header->data_max) {
173  av_log(avctx, AV_LOG_ERROR, "data min/max (%g %g) is invalid\n", header->data_min, header->data_max);
174  return AVERROR_INVALIDDATA;
175  }
176  av_log(avctx, AV_LOG_WARNING, "data min/max indicates a blank image\n");
177  header->data_max ++;
178  }
179 
180  return 0;
181 }
182 
183 static int fits_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
184 {
185  AVFrame *p=data;
186  const uint8_t *ptr8 = avpkt->data, *end;
187  uint8_t t8;
188  int16_t t16;
189  int32_t t32;
190  int64_t t64;
191  float tflt;
192  double tdbl;
193  int ret, i, j, k;
194  const int map[] = {2, 0, 1, 3}; // mapping from GBRA -> RGBA as RGBA is to be stored in FITS file..
195  uint8_t *dst8;
196  uint16_t *dst16;
197  uint64_t t;
199  FITSContext * fitsctx = avctx->priv_data;
200 
201  end = ptr8 + avpkt->size;
202  p->metadata = NULL;
203  ret = fits_read_header(avctx, &ptr8, &header, end, &p->metadata);
204  if (ret < 0)
205  return ret;
206 
207  if (header.rgb) {
208  if (header.bitpix == 8) {
209  if (header.naxisn[2] == 3) {
210  avctx->pix_fmt = AV_PIX_FMT_GBRP;
211  } else {
212  avctx->pix_fmt = AV_PIX_FMT_GBRAP;
213  }
214  } else if (header.bitpix == 16) {
215  if (header.naxisn[2] == 3) {
216  avctx->pix_fmt = AV_PIX_FMT_GBRP16;
217  } else {
218  avctx->pix_fmt = AV_PIX_FMT_GBRAP16;
219  }
220  } else {
221  av_log(avctx, AV_LOG_ERROR, "unsupported BITPIX = %d\n", header.bitpix);
222  return AVERROR_INVALIDDATA;
223  }
224  } else {
225  if (header.bitpix == 8) {
226  avctx->pix_fmt = AV_PIX_FMT_GRAY8;
227  } else {
228  avctx->pix_fmt = AV_PIX_FMT_GRAY16;
229  }
230  }
231 
232  if ((ret = ff_set_dimensions(avctx, header.naxisn[0], header.naxisn[1])) < 0)
233  return ret;
234 
235  if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
236  return ret;
237 
238  /*
239  * FITS stores images with bottom row first. Therefore we have
240  * to fill the image from bottom to top.
241  */
242  if (header.rgb) {
243  switch(header.bitpix) {
244 #define CASE_RGB(cas, dst, type, dref) \
245  case cas: \
246  for (k = 0; k < header.naxisn[2]; k++) { \
247  for (i = 0; i < avctx->height; i++) { \
248  dst = (type *) (p->data[map[k]] + (avctx->height - i - 1) * p->linesize[map[k]]); \
249  for (j = 0; j < avctx->width; j++) { \
250  t32 = dref(ptr8); \
251  if (!header.blank_found || t32 != header.blank) { \
252  t = t32 * header.bscale + header.bzero; \
253  } else { \
254  t = fitsctx->blank_val; \
255  } \
256  *dst++ = (type) t; \
257  ptr8 += cas >> 3; \
258  } \
259  } \
260  } \
261  break
262 
263  CASE_RGB(8, dst8, uint8_t, *);
264  CASE_RGB(16, dst16, uint16_t, AV_RB16);
265  }
266  } else {
267  double scale = header.data_max - header.data_min;
268 
269  if (scale <= 0 || !isfinite(scale)) {
270  scale = 1;
271  }
272  scale = 1/scale;
273 
274  switch (header.bitpix) {
275 #define CASE_GRAY(cas, dst, type, t, rd) \
276  case cas: \
277  for (i = 0; i < avctx->height; i++) { \
278  dst = (type *) (p->data[0] + (avctx->height-i-1)* p->linesize[0]); \
279  for (j = 0; j < avctx->width; j++) { \
280  t = rd; \
281  if (!header.blank_found || t != header.blank) { \
282  *dst++ = lrint(((t - header.data_min) * ((1 << (sizeof(type) * 8)) - 1)) * scale); \
283  } else { \
284  *dst++ = fitsctx->blank_val; \
285  } \
286  ptr8 += abs(cas) >> 3; \
287  } \
288  } \
289  break
290 
291  CASE_GRAY(-64, dst16, uint16_t, tdbl, av_int2double(AV_RB64(ptr8)));
292  CASE_GRAY(-32, dst16, uint16_t, tflt, av_int2float(AV_RB32(ptr8)));
293  CASE_GRAY(8, dst8, uint8_t, t8, ptr8[0]);
294  CASE_GRAY(16, dst16, uint16_t, t16, AV_RB16(ptr8));
295  CASE_GRAY(32, dst16, uint16_t, t32, AV_RB32(ptr8));
296  CASE_GRAY(64, dst16, uint16_t, t64, AV_RB64(ptr8));
297  default:
298  av_log(avctx, AV_LOG_ERROR, "invalid BITPIX, %d\n", header.bitpix);
299  return AVERROR_INVALIDDATA;
300  }
301  }
302 
303  p->key_frame = 1;
305 
306  *got_frame = 1;
307 
308  return avpkt->size;
309 }
310 
311 static const AVOption fits_options[] = {
312  { "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},
313  { NULL },
314 };
315 
316 static const AVClass fits_decoder_class = {
317  .class_name = "FITS decoder",
318  .item_name = av_default_item_name,
319  .option = fits_options,
320  .version = LIBAVUTIL_VERSION_INT,
321 };
322 
324  .name = "fits",
325  .type = AVMEDIA_TYPE_VIDEO,
326  .id = AV_CODEC_ID_FITS,
327  .priv_data_size = sizeof(FITSContext),
329  .capabilities = AV_CODEC_CAP_DR1,
330  .long_name = NULL_IF_CONFIG_SMALL("Flexible Image Transport System"),
331  .priv_class = &fits_decoder_class
332 };
AV_PIX_FMT_GBRAP16
#define AV_PIX_FMT_GBRAP16
Definition: pixfmt.h:419
AVCodec
AVCodec.
Definition: codec.h:190
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
opt.h
AV_OPT_FLAG_VIDEO_PARAM
#define AV_OPT_FLAG_VIDEO_PARAM
Definition: opt.h:279
av_int2double
static av_always_inline double av_int2double(uint64_t i)
Reinterpret a 64-bit integer as a double.
Definition: intfloat.h:60
end
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:92
FITSContext
Definition: fitsdec.c:42
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:300
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:355
AVOption
AVOption.
Definition: opt.h:246
data
const char data[16]
Definition: mxf.c:91
float.h
AVDictionary
Definition: dict.c:30
intfloat.h
CASE_RGB
#define CASE_RGB(cas, dst, type, dref)
fits_decode_frame
static int fits_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: fitsdec.c:183
fits.h
AV_PIX_FMT_GBRAP
@ AV_PIX_FMT_GBRAP
planar GBRA 4:4:4:4 32bpp
Definition: pixfmt.h:215
av_int2float
static av_always_inline float av_int2float(uint32_t i)
Reinterpret a 32-bit integer as a float.
Definition: intfloat.h:40
avpriv_fits_header_parse_line
int avpriv_fits_header_parse_line(void *avcl, FITSHeader *header, const uint8_t line[80], AVDictionary ***metadata)
Parse a single header line.
Definition: fits.c:108
AVFrame::key_frame
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:378
AV_PIX_FMT_GRAY16
#define AV_PIX_FMT_GRAY16
Definition: pixfmt.h:381
fits_read_header
static int fits_read_header(AVCodecContext *avctx, const uint8_t **ptr, FITSHeader *header, const uint8_t *end, AVDictionary **metadata)
Read the fits header and store the values in FITSHeader pointed by header.
Definition: fitsdec.c:105
fill_data_min_max
static int fill_data_min_max(const uint8_t *ptr8, FITSHeader *header, const uint8_t *end)
Calculate the data_min and data_max values from the data.
Definition: fitsdec.c:55
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
CASE_GRAY
#define CASE_GRAY(cas, dst, type, t, rd)
decode
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
intreadwrite.h
isfinite
#define isfinite(x)
Definition: libm.h:359
int32_t
int32_t
Definition: audio_convert.c:194
AV_PIX_FMT_GBRP16
#define AV_PIX_FMT_GBRP16
Definition: pixfmt.h:416
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
NULL
#define NULL
Definition: coverity.c:32
AV_CODEC_ID_FITS
@ AV_CODEC_ID_FITS
Definition: codec_id.h:280
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
FITSContext::blank_val
int blank_val
Definition: fitsdec.c:44
abs
#define abs(x)
Definition: cuda_runtime.h:35
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
AVFrame::pict_type
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:383
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1854
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:50
AVPacket::size
int size
Definition: packet.h:356
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:188
size
int size
Definition: twinvq_data.h:11134
AV_RB32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:92
t8
#define t8
Definition: regdef.h:53
header
static const uint8_t header[24]
Definition: sdr2.c:67
avpriv_fits_header_init
int avpriv_fits_header_init(FITSHeader *header, FITSHeaderState state)
Initialize a single header line.
Definition: fits.c:26
STATE_BITPIX
@ STATE_BITPIX
Definition: fits.h:32
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
AV_OPT_FLAG_DECODING_PARAM
#define AV_OPT_FLAG_DECODING_PARAM
a generic parameter which can be set by the user for demuxing or decoding
Definition: opt.h:277
uint8_t
uint8_t
Definition: audio_convert.c:194
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:197
CASE_N
#define CASE_N(a, t, rd)
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:736
avcodec.h
ret
ret
Definition: filter_design.txt:187
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
dict.h
AVCodecContext
main external API structure.
Definition: avcodec.h:526
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:223
AVFrame::metadata
AVDictionary * metadata
metadata.
Definition: frame.h:586
FITSHeader
Structure to store the header keywords in FITS file.
Definition: fits.h:43
fits_decoder_class
static const AVClass fits_decoder_class
Definition: fitsdec.c:316
AV_PIX_FMT_GBRP
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:168
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
ff_set_dimensions
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
Definition: utils.c:104
map
const VDPAUPixFmtMap * map
Definition: hwcontext_vdpau.c:85
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:553
AVPacket
This structure stores compressed data.
Definition: packet.h:332
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
ff_fits_decoder
AVCodec ff_fits_decoder
Definition: fitsdec.c:323
AV_RB64
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_RB64
Definition: bytestream.h:91
fits_options
static const AVOption fits_options[]
Definition: fitsdec.c:311
AV_RB16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:94