FFmpeg  4.4.7
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 "avcodec.h"
23 #include "libavutil/dict.h"
24 #include "fits.h"
25 
27 {
28  header->state = state;
29  header->naxis_index = 0;
30  header->naxis = 0;
31  memset(header->naxisn, 0, sizeof(header->naxisn));
32  header->blank_found = 0;
33  header->pcount = 0;
34  header->gcount = 1;
35  header->groups = 0;
36  header->rgb = 0;
37  header->image_extension = 0;
38  header->bscale = 1.0;
39  header->bzero = 0;
40  header->data_min_found = 0;
41  header->data_max_found = 0;
42  return 0;
43 }
44 
45 static int dict_set_if_not_null(AVDictionary ***metadata, char *keyword, char *value)
46 {
47  if (metadata)
48  av_dict_set(*metadata, keyword, value, 0);
49  return 0;
50 }
51 
52 /**
53  * Extract keyword and value from a header line (80 bytes) and store them in keyword and value strings respectively
54  * @param ptr8 pointer to the data
55  * @param keyword pointer to the char array in which keyword is to be stored
56  * @param value pointer to the char array in which value is to be stored
57  * @return 0 if calculated successfully otherwise AVERROR_INVALIDDATA
58  */
59 static int read_keyword_value(const uint8_t *ptr8, char *keyword, char *value)
60 {
61  int i;
62 
63  for (i = 0; i < 8 && ptr8[i] != ' '; i++) {
64  keyword[i] = ptr8[i];
65  }
66  keyword[i] = '\0';
67 
68  if (ptr8[8] == '=') {
69  i = 10;
70  while (i < 80 && ptr8[i] == ' ') {
71  i++;
72  }
73 
74  if (i < 80) {
75  *value++ = ptr8[i];
76  i++;
77  if (ptr8[i-1] == '\'') {
78  for (; i < 80 && ptr8[i] != '\''; i++) {
79  *value++ = ptr8[i];
80  }
81  *value++ = '\'';
82  } else if (ptr8[i-1] == '(') {
83  for (; i < 80 && ptr8[i] != ')'; i++) {
84  *value++ = ptr8[i];
85  }
86  *value++ = ')';
87  } else {
88  for (; i < 80 && ptr8[i] != ' ' && ptr8[i] != '/'; i++) {
89  *value++ = ptr8[i];
90  }
91  }
92  }
93  }
94  *value = '\0';
95  return 0;
96 }
97 
98 #define CHECK_KEYWORD(key) \
99  if (strcmp(keyword, key)) { \
100  av_log(avcl, AV_LOG_ERROR, "expected %s keyword, found %s = %s\n", key, keyword, value); \
101  return AVERROR_INVALIDDATA; \
102  }
103 
104 #define CHECK_VALUE(key, val) \
105  if (sscanf(value, "%d", &header->val) != 1) { \
106  av_log(avcl, AV_LOG_ERROR, "invalid value of %s keyword, %s = %s\n", key, keyword, value); \
107  return AVERROR_INVALIDDATA; \
108  }
109 
110 int avpriv_fits_header_parse_line(void *avcl, FITSHeader *header, const uint8_t line[80], AVDictionary ***metadata)
111 {
112  int dim_no, ret;
113  int64_t t;
114  double d;
115  char keyword[10], value[72], c;
116 
117  read_keyword_value(line, keyword, value);
118  switch (header->state) {
119  case STATE_SIMPLE:
120  CHECK_KEYWORD("SIMPLE");
121 
122  if (value[0] == 'F') {
123  av_log(avcl, AV_LOG_WARNING, "not a standard FITS file\n");
124  } else if (value[0] != 'T') {
125  av_log(avcl, AV_LOG_ERROR, "invalid value of SIMPLE keyword, SIMPLE = %c\n", value[0]);
126  return AVERROR_INVALIDDATA;
127  }
128 
129  header->state = STATE_BITPIX;
130  break;
131  case STATE_XTENSION:
132  CHECK_KEYWORD("XTENSION");
133 
134  if (!strcmp(value, "'IMAGE '")) {
135  header->image_extension = 1;
136  }
137 
138  header->state = STATE_BITPIX;
139  break;
140  case STATE_BITPIX:
141  CHECK_KEYWORD("BITPIX");
142  CHECK_VALUE("BITPIX", bitpix);
143 
144  switch(header->bitpix) {
145  case 8:
146  case 16:
147  case 32: case -32:
148  case 64: case -64: break;
149  default:
150  av_log(avcl, AV_LOG_ERROR, "invalid value of BITPIX %d\n", header->bitpix); \
151  return AVERROR_INVALIDDATA;
152  }
153 
154  dict_set_if_not_null(metadata, keyword, value);
155 
156  header->state = STATE_NAXIS;
157  break;
158  case STATE_NAXIS:
159  CHECK_KEYWORD("NAXIS");
160  CHECK_VALUE("NAXIS", naxis);
161  dict_set_if_not_null(metadata, keyword, value);
162 
163  if (header->naxis) {
164  header->state = STATE_NAXIS_N;
165  } else {
166  header->state = STATE_REST;
167  }
168  break;
169  case STATE_NAXIS_N:
170  ret = sscanf(keyword, "NAXIS%d", &dim_no);
171  if (ret != 1 || dim_no != header->naxis_index + 1) {
172  av_log(avcl, AV_LOG_ERROR, "expected NAXIS%d keyword, found %s = %s\n", header->naxis_index + 1, keyword, value);
173  return AVERROR_INVALIDDATA;
174  }
175 
176  if (sscanf(value, "%d", &header->naxisn[header->naxis_index]) != 1) {
177  av_log(avcl, AV_LOG_ERROR, "invalid value of NAXIS%d keyword, %s = %s\n", header->naxis_index + 1, keyword, value);
178  return AVERROR_INVALIDDATA;
179  }
180 
181  dict_set_if_not_null(metadata, keyword, value);
182  header->naxis_index++;
183  if (header->naxis_index == header->naxis) {
184  header->state = STATE_REST;
185  }
186  break;
187  case STATE_REST:
188  if (!strcmp(keyword, "BLANK") && sscanf(value, "%"SCNd64"", &t) == 1) {
189  header->blank = t;
190  header->blank_found = 1;
191  } else if (!strcmp(keyword, "BSCALE") && sscanf(value, "%lf", &d) == 1) {
192  if (d <= 0)
193  return AVERROR_INVALIDDATA;
194  header->bscale = d;
195  } else if (!strcmp(keyword, "BZERO") && sscanf(value, "%lf", &d) == 1) {
196  header->bzero = d;
197  } else if (!strcmp(keyword, "CTYPE3") && !strncmp(value, "'RGB", 4)) {
198  header->rgb = 1;
199  } else if (!strcmp(keyword, "DATAMAX") && sscanf(value, "%lf", &d) == 1) {
200  header->data_max_found = 1;
201  header->data_max = d;
202  } else if (!strcmp(keyword, "DATAMIN") && sscanf(value, "%lf", &d) == 1) {
203  header->data_min_found = 1;
204  header->data_min = d;
205  } else if (!strcmp(keyword, "END")) {
206  return 1;
207  } else if (!strcmp(keyword, "GROUPS") && sscanf(value, "%c", &c) == 1) {
208  header->groups = (c == 'T');
209  } else if (!strcmp(keyword, "GCOUNT") && sscanf(value, "%"SCNd64"", &t) == 1) {
210  if (t < 0 || t > INT_MAX)
211  return AVERROR_INVALIDDATA;
212  header->gcount = t;
213  } else if (!strcmp(keyword, "PCOUNT") && sscanf(value, "%"SCNd64"", &t) == 1) {
214  if (t < 0 || t > INT_MAX)
215  return AVERROR_INVALIDDATA;
216  header->pcount = t;
217  }
218  dict_set_if_not_null(metadata, keyword, value);
219  break;
220  }
221  return 0;
222 }
uint8_t
Libavcodec external API header.
static struct @321 state
long long int64_t
Definition: coverity.c:34
Public dictionary API.
double value
Definition: eval.c:100
#define CHECK_VALUE(key, val)
Definition: fits.c:104
int avpriv_fits_header_init(FITSHeader *header, FITSHeaderState state)
Initialize a single header line.
Definition: fits.c:26
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:59
#define CHECK_KEYWORD(key)
Definition: fits.c:98
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:110
static int dict_set_if_not_null(AVDictionary ***metadata, char *keyword, char *value)
Definition: fits.c:45
FITSHeaderState
Definition: fits.h:29
@ STATE_XTENSION
Definition: fits.h:31
@ STATE_NAXIS_N
Definition: fits.h:34
@ STATE_BITPIX
Definition: fits.h:32
@ STATE_REST
Definition: fits.h:37
@ STATE_SIMPLE
Definition: fits.h:30
@ STATE_NAXIS
Definition: fits.h:33
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:70
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
int i
Definition: input.c:407
static const uint8_t header[24]
Definition: sdr2.c:67
Structure to store the header keywords in FITS file.
Definition: fits.h:43
Definition: graph2dot.c:48
#define av_log(a,...)
static double c[64]