FFmpeg
fits.c
Go to the documentation of this file.
1 /*
2  * FITS implementation of common functions
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 #include <inttypes.h>
23 #include <limits.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include "libavutil/dict.h"
27 #include "libavutil/error.h"
28 #include "libavutil/log.h"
29 #include "fits.h"
30 
32 {
33  header->state = state;
34  header->naxis_index = 0;
35  header->blank_found = 0;
36  header->pcount = 0;
37  header->gcount = 1;
38  header->groups = 0;
39  header->rgb = 0;
40  header->image_extension = 0;
41  header->bscale = 1.0;
42  header->bzero = 0;
43  header->data_min_found = 0;
44  header->data_max_found = 0;
45  return 0;
46 }
47 
48 static int dict_set_if_not_null(AVDictionary ***metadata, char *keyword, char *value)
49 {
50  if (metadata)
51  av_dict_set(*metadata, keyword, value, 0);
52  return 0;
53 }
54 
55 /**
56  * Extract keyword and value from a header line (80 bytes) and store them in keyword and value strings respectively
57  * @param ptr8 pointer to the data
58  * @param keyword pointer to the char array in which keyword is to be stored
59  * @param value pointer to the char array in which value is to be stored
60  * @return 0 if calculated successfully otherwise AVERROR_INVALIDDATA
61  */
62 static int read_keyword_value(const uint8_t *ptr8, char *keyword, char *value)
63 {
64  int i;
65 
66  for (i = 0; i < 8 && ptr8[i] != ' '; i++) {
67  keyword[i] = ptr8[i];
68  }
69  keyword[i] = '\0';
70 
71  if (ptr8[8] == '=') {
72  i = 10;
73  while (i < 80 && ptr8[i] == ' ') {
74  i++;
75  }
76 
77  if (i < 80) {
78  *value++ = ptr8[i];
79  i++;
80  if (ptr8[i-1] == '\'') {
81  for (; i < 80 && ptr8[i] != '\''; i++) {
82  *value++ = ptr8[i];
83  }
84  *value++ = '\'';
85  } else if (ptr8[i-1] == '(') {
86  for (; i < 80 && ptr8[i] != ')'; i++) {
87  *value++ = ptr8[i];
88  }
89  *value++ = ')';
90  } else {
91  for (; i < 80 && ptr8[i] != ' ' && ptr8[i] != '/'; i++) {
92  *value++ = ptr8[i];
93  }
94  }
95  }
96  }
97  *value = '\0';
98  return 0;
99 }
100 
101 #define CHECK_KEYWORD(key) \
102  if (strcmp(keyword, key)) { \
103  av_log(avcl, AV_LOG_ERROR, "expected %s keyword, found %s = %s\n", key, keyword, value); \
104  return AVERROR_INVALIDDATA; \
105  }
106 
107 #define CHECK_VALUE(key, val) \
108  if (sscanf(value, "%d", &header->val) != 1) { \
109  av_log(avcl, AV_LOG_ERROR, "invalid value of %s keyword, %s = %s\n", key, keyword, value); \
110  return AVERROR_INVALIDDATA; \
111  }
112 
113 int avpriv_fits_header_parse_line(void *avcl, FITSHeader *header, const uint8_t line[80], AVDictionary ***metadata)
114 {
115  int dim_no, ret;
116  int64_t t;
117  double d;
118  char keyword[10], value[72], c;
119 
120  read_keyword_value(line, keyword, value);
121  switch (header->state) {
122  case STATE_SIMPLE:
123  CHECK_KEYWORD("SIMPLE");
124 
125  if (value[0] == 'F') {
126  av_log(avcl, AV_LOG_WARNING, "not a standard FITS file\n");
127  } else if (value[0] != 'T') {
128  av_log(avcl, AV_LOG_ERROR, "invalid value of SIMPLE keyword, SIMPLE = %c\n", value[0]);
129  return AVERROR_INVALIDDATA;
130  }
131 
132  header->state = STATE_BITPIX;
133  break;
134  case STATE_XTENSION:
135  CHECK_KEYWORD("XTENSION");
136 
137  if (!strcmp(value, "'IMAGE '")) {
138  header->image_extension = 1;
139  }
140 
141  header->state = STATE_BITPIX;
142  break;
143  case STATE_BITPIX:
144  CHECK_KEYWORD("BITPIX");
145  CHECK_VALUE("BITPIX", bitpix);
146 
147  switch(header->bitpix) {
148  case 8:
149  case 16:
150  case 32: case -32:
151  case 64: case -64: break;
152  default:
153  av_log(avcl, AV_LOG_ERROR, "invalid value of BITPIX %d\n", header->bitpix); \
154  return AVERROR_INVALIDDATA;
155  }
156 
157  dict_set_if_not_null(metadata, keyword, value);
158 
159  header->state = STATE_NAXIS;
160  break;
161  case STATE_NAXIS:
162  CHECK_KEYWORD("NAXIS");
163  CHECK_VALUE("NAXIS", naxis);
164  dict_set_if_not_null(metadata, keyword, value);
165 
166  if (header->naxis) {
167  header->state = STATE_NAXIS_N;
168  } else {
169  header->state = STATE_REST;
170  }
171  break;
172  case STATE_NAXIS_N:
173  ret = sscanf(keyword, "NAXIS%d", &dim_no);
174  if (ret != 1 || dim_no != header->naxis_index + 1) {
175  av_log(avcl, AV_LOG_ERROR, "expected NAXIS%d keyword, found %s = %s\n", header->naxis_index + 1, keyword, value);
176  return AVERROR_INVALIDDATA;
177  }
178 
179  if (sscanf(value, "%d", &header->naxisn[header->naxis_index]) != 1) {
180  av_log(avcl, AV_LOG_ERROR, "invalid value of NAXIS%d keyword, %s = %s\n", header->naxis_index + 1, keyword, value);
181  return AVERROR_INVALIDDATA;
182  }
183 
184  dict_set_if_not_null(metadata, keyword, value);
185  header->naxis_index++;
186  if (header->naxis_index == header->naxis) {
187  header->state = STATE_REST;
188  }
189  break;
190  case STATE_REST:
191  if (!strcmp(keyword, "BLANK") && sscanf(value, "%"SCNd64"", &t) == 1) {
192  header->blank = t;
193  header->blank_found = 1;
194  } else if (!strcmp(keyword, "BSCALE") && sscanf(value, "%lf", &d) == 1) {
195  if (d <= 0)
196  return AVERROR_INVALIDDATA;
197  header->bscale = d;
198  } else if (!strcmp(keyword, "BZERO") && sscanf(value, "%lf", &d) == 1) {
199  header->bzero = d;
200  } else if (!strcmp(keyword, "CTYPE3") && !strncmp(value, "'RGB", 4)) {
201  header->rgb = 1;
202  } else if (!strcmp(keyword, "DATAMAX") && sscanf(value, "%lf", &d) == 1) {
203  header->data_max_found = 1;
204  header->data_max = d;
205  } else if (!strcmp(keyword, "DATAMIN") && sscanf(value, "%lf", &d) == 1) {
206  header->data_min_found = 1;
207  header->data_min = d;
208  } else if (!strcmp(keyword, "END")) {
209  return 1;
210  } else if (!strcmp(keyword, "GROUPS") && sscanf(value, "%c", &c) == 1) {
211  header->groups = (c == 'T');
212  } else if (!strcmp(keyword, "GCOUNT") && sscanf(value, "%"SCNd64"", &t) == 1) {
213  if (t < 0 || t > INT_MAX)
214  return AVERROR_INVALIDDATA;
215  header->gcount = t;
216  } else if (!strcmp(keyword, "PCOUNT") && sscanf(value, "%"SCNd64"", &t) == 1) {
217  if (t < 0 || t > INT_MAX)
218  return AVERROR_INVALIDDATA;
219  header->pcount = t;
220  }
221  dict_set_if_not_null(metadata, keyword, value);
222  break;
223  }
224  return 0;
225 }
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:215
int64_t
long long int64_t
Definition: coverity.c:34
AVDictionary
Definition: dict.c:34
CHECK_VALUE
#define CHECK_VALUE(key, val)
Definition: fits.c:107
STATE_XTENSION
@ STATE_XTENSION
Definition: fits.h:31
fits.h
read_keyword_value
static int read_keyword_value(const uint8_t *ptr8, char *keyword, char *value)
Extract keyword and value from a header line (80 bytes) and store them in keyword and value strings r...
Definition: fits.c:62
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:113
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
STATE_REST
@ STATE_REST
Definition: fits.h:37
CHECK_KEYWORD
#define CHECK_KEYWORD(key)
Definition: fits.c:101
limits.h
dict_set_if_not_null
static int dict_set_if_not_null(AVDictionary ***metadata, char *keyword, char *value)
Definition: fits.c:48
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
error.h
header
static const uint8_t header[24]
Definition: sdr2.c:68
line
Definition: graph2dot.c:48
avpriv_fits_header_init
int avpriv_fits_header_init(FITSHeader *header, FITSHeaderState state)
Initialize a single header line.
Definition: fits.c:31
log.h
STATE_BITPIX
@ STATE_BITPIX
Definition: fits.h:32
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
state
static struct @457 state
STATE_SIMPLE
@ STATE_SIMPLE
Definition: fits.h:30
value
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
Definition: writing_filters.txt:86
ret
ret
Definition: filter_design.txt:187
dict.h
FITSHeaderState
FITSHeaderState
Definition: fits.h:29
FITSHeader
Structure to store the header keywords in FITS file.
Definition: fits.h:43
av_dict_set
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:88
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
STATE_NAXIS_N
@ STATE_NAXIS_N
Definition: fits.h:34
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
STATE_NAXIS
@ STATE_NAXIS
Definition: fits.h:33