FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/fits.c
Date: 2025-08-19 23:55:23
Exec Total Coverage
Lines: 109 127 85.8%
Functions: 4 4 100.0%
Branches: 80 117 68.4%

Line Branch Exec Source
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
31 452 int avpriv_fits_header_init(FITSHeader *header, FITSHeaderState state)
32 {
33 452 header->state = state;
34 452 header->naxis_index = 0;
35 452 header->naxis = 0;
36 452 memset(header->naxisn, 0, sizeof(header->naxisn));
37 452 header->blank_found = 0;
38 452 header->pcount = 0;
39 452 header->gcount = 1;
40 452 header->groups = 0;
41 452 header->rgb = 0;
42 452 header->image_extension = 0;
43 452 header->bscale = 1.0;
44 452 header->bzero = 0;
45 452 header->data_min_found = 0;
46 452 header->data_max_found = 0;
47 452 return 0;
48 }
49
50 4597 static int dict_set_if_not_null(AVDictionary ***metadata, char *keyword, char *value)
51 {
52
2/2
✓ Branch 0 taken 2398 times.
✓ Branch 1 taken 2199 times.
4597 if (metadata)
53 2398 av_dict_set(*metadata, keyword, value, 0);
54 4597 return 0;
55 }
56
57 /**
58 * Extract keyword and value from a header line (80 bytes) and store them in keyword and value strings respectively
59 * @param ptr8 pointer to the data
60 * @param keyword pointer to the char array in which keyword is to be stored
61 * @param value pointer to the char array in which value is to be stored
62 * @return 0 if calculated successfully otherwise AVERROR_INVALIDDATA
63 */
64 5210 static int read_keyword_value(const uint8_t *ptr8, char *keyword, char *value)
65 {
66 int i;
67
68
4/4
✓ Branch 0 taken 35233 times.
✓ Branch 1 taken 319 times.
✓ Branch 2 taken 30342 times.
✓ Branch 3 taken 4891 times.
35552 for (i = 0; i < 8 && ptr8[i] != ' '; i++) {
69 30342 keyword[i] = ptr8[i];
70 }
71 5210 keyword[i] = '\0';
72
73
2/2
✓ Branch 0 taken 4625 times.
✓ Branch 1 taken 585 times.
5210 if (ptr8[8] == '=') {
74 4625 i = 10;
75
3/4
✓ Branch 0 taken 9110 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4485 times.
✓ Branch 3 taken 4625 times.
9110 while (i < 80 && ptr8[i] == ' ') {
76 4485 i++;
77 }
78
79
1/2
✓ Branch 0 taken 4625 times.
✗ Branch 1 not taken.
4625 if (i < 80) {
80 4625 *value++ = ptr8[i];
81 4625 i++;
82
2/2
✓ Branch 0 taken 749 times.
✓ Branch 1 taken 3876 times.
4625 if (ptr8[i-1] == '\'') {
83
3/4
✓ Branch 0 taken 9063 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8314 times.
✓ Branch 3 taken 749 times.
9063 for (; i < 80 && ptr8[i] != '\''; i++) {
84 8314 *value++ = ptr8[i];
85 }
86 749 *value++ = '\'';
87
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3876 times.
3876 } else if (ptr8[i-1] == '(') {
88 for (; i < 80 && ptr8[i] != ')'; i++) {
89 *value++ = ptr8[i];
90 }
91 *value++ = ')';
92 } else {
93
4/6
✓ Branch 0 taken 8769 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4893 times.
✓ Branch 3 taken 3876 times.
✓ Branch 4 taken 4893 times.
✗ Branch 5 not taken.
8769 for (; i < 80 && ptr8[i] != ' ' && ptr8[i] != '/'; i++) {
94 4893 *value++ = ptr8[i];
95 }
96 }
97 }
98 }
99 5210 *value = '\0';
100 5210 return 0;
101 }
102
103 #define CHECK_KEYWORD(key) \
104 if (strcmp(keyword, key)) { \
105 av_log(avcl, AV_LOG_ERROR, "expected %s keyword, found %s = %s\n", key, keyword, value); \
106 return AVERROR_INVALIDDATA; \
107 }
108
109 #define CHECK_VALUE(key, val) \
110 if (sscanf(value, "%d", &header->val) != 1) { \
111 av_log(avcl, AV_LOG_ERROR, "invalid value of %s keyword, %s = %s\n", key, keyword, value); \
112 return AVERROR_INVALIDDATA; \
113 }
114
115 5210 int avpriv_fits_header_parse_line(void *avcl, FITSHeader *header, const uint8_t line[80], AVDictionary ***metadata)
116 {
117 int dim_no, ret;
118 int64_t t;
119 double d;
120 char keyword[10], value[72], c;
121
122 5210 read_keyword_value(line, keyword, value);
123
6/7
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 177 times.
✓ Branch 2 taken 414 times.
✓ Branch 3 taken 414 times.
✓ Branch 4 taken 1124 times.
✓ Branch 5 taken 3059 times.
✗ Branch 6 not taken.
5210 switch (header->state) {
124 22 case STATE_SIMPLE:
125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 CHECK_KEYWORD("SIMPLE");
126
127
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (value[0] == 'F') {
128 av_log(avcl, AV_LOG_WARNING, "not a standard FITS file\n");
129
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 } else if (value[0] != 'T') {
130 av_log(avcl, AV_LOG_ERROR, "invalid value of SIMPLE keyword, SIMPLE = %c\n", value[0]);
131 return AVERROR_INVALIDDATA;
132 }
133
134 22 header->state = STATE_BITPIX;
135 22 break;
136 177 case STATE_XTENSION:
137
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 177 times.
177 CHECK_KEYWORD("XTENSION");
138
139
2/2
✓ Branch 0 taken 176 times.
✓ Branch 1 taken 1 times.
177 if (!strcmp(value, "'IMAGE '")) {
140 176 header->image_extension = 1;
141 }
142
143 177 header->state = STATE_BITPIX;
144 177 break;
145 414 case STATE_BITPIX:
146
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 414 times.
414 CHECK_KEYWORD("BITPIX");
147
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 414 times.
414 CHECK_VALUE("BITPIX", bitpix);
148
149
1/2
✓ Branch 0 taken 414 times.
✗ Branch 1 not taken.
414 switch(header->bitpix) {
150 414 case 8:
151 case 16:
152 case 32: case -32:
153 414 case 64: case -64: break;
154 default:
155 av_log(avcl, AV_LOG_ERROR, "invalid value of BITPIX %d\n", header->bitpix); \
156 return AVERROR_INVALIDDATA;
157 }
158
159 414 dict_set_if_not_null(metadata, keyword, value);
160
161 414 header->state = STATE_NAXIS;
162 414 break;
163 414 case STATE_NAXIS:
164
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 414 times.
414 CHECK_KEYWORD("NAXIS");
165
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 414 times.
414 CHECK_VALUE("NAXIS", naxis);
166 414 dict_set_if_not_null(metadata, keyword, value);
167
168
1/2
✓ Branch 0 taken 414 times.
✗ Branch 1 not taken.
414 if (header->naxis) {
169 414 header->state = STATE_NAXIS_N;
170 } else {
171 header->state = STATE_REST;
172 }
173 414 break;
174 1124 case STATE_NAXIS_N:
175 1124 ret = sscanf(keyword, "NAXIS%d", &dim_no);
176
2/4
✓ Branch 0 taken 1124 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1124 times.
1124 if (ret != 1 || dim_no != header->naxis_index + 1) {
177 av_log(avcl, AV_LOG_ERROR, "expected NAXIS%d keyword, found %s = %s\n", header->naxis_index + 1, keyword, value);
178 return AVERROR_INVALIDDATA;
179 }
180
181
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1124 times.
1124 if (sscanf(value, "%d", &header->naxisn[header->naxis_index]) != 1) {
182 av_log(avcl, AV_LOG_ERROR, "invalid value of NAXIS%d keyword, %s = %s\n", header->naxis_index + 1, keyword, value);
183 return AVERROR_INVALIDDATA;
184 }
185
186 1124 dict_set_if_not_null(metadata, keyword, value);
187 1124 header->naxis_index++;
188
2/2
✓ Branch 0 taken 414 times.
✓ Branch 1 taken 710 times.
1124 if (header->naxis_index == header->naxis) {
189 414 header->state = STATE_REST;
190 }
191 1124 break;
192 3059 case STATE_REST:
193
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3056 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3059 if (!strcmp(keyword, "BLANK") && sscanf(value, "%"SCNd64"", &t) == 1) {
194 3 header->blank = t;
195 3 header->blank_found = 1;
196
3/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3050 times.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
3056 } else if (!strcmp(keyword, "BSCALE") && sscanf(value, "%lf", &d) == 1) {
197
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (d <= 0)
198 return AVERROR_INVALIDDATA;
199 6 header->bscale = d;
200
3/4
✓ Branch 0 taken 165 times.
✓ Branch 1 taken 2885 times.
✓ Branch 2 taken 165 times.
✗ Branch 3 not taken.
3050 } else if (!strcmp(keyword, "BZERO") && sscanf(value, "%lf", &d) == 1) {
201 165 header->bzero = d;
202
4/4
✓ Branch 0 taken 300 times.
✓ Branch 1 taken 2585 times.
✓ Branch 2 taken 296 times.
✓ Branch 3 taken 4 times.
2885 } else if (!strcmp(keyword, "CTYPE3") && !strncmp(value, "'RGB", 4)) {
203 296 header->rgb = 1;
204
4/4
✓ Branch 0 taken 405 times.
✓ Branch 1 taken 2184 times.
✓ Branch 2 taken 404 times.
✓ Branch 3 taken 1 times.
2589 } else if (!strcmp(keyword, "DATAMAX") && sscanf(value, "%lf", &d) == 1) {
205 404 header->data_max_found = 1;
206 404 header->data_max = d;
207
4/4
✓ Branch 0 taken 405 times.
✓ Branch 1 taken 1780 times.
✓ Branch 2 taken 404 times.
✓ Branch 3 taken 1 times.
2185 } else if (!strcmp(keyword, "DATAMIN") && sscanf(value, "%lf", &d) == 1) {
208 404 header->data_min_found = 1;
209 404 header->data_min = d;
210
2/2
✓ Branch 0 taken 414 times.
✓ Branch 1 taken 1367 times.
1781 } else if (!strcmp(keyword, "END")) {
211 414 return 1;
212
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1367 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1367 } else if (!strcmp(keyword, "GROUPS") && sscanf(value, "%c", &c) == 1) {
213 header->groups = (c == 'T');
214
3/4
✓ Branch 0 taken 349 times.
✓ Branch 1 taken 1018 times.
✓ Branch 2 taken 349 times.
✗ Branch 3 not taken.
1367 } else if (!strcmp(keyword, "GCOUNT") && sscanf(value, "%"SCNd64"", &t) == 1) {
215
2/4
✓ Branch 0 taken 349 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 349 times.
349 if (t < 0 || t > INT_MAX)
216 return AVERROR_INVALIDDATA;
217 349 header->gcount = t;
218
3/4
✓ Branch 0 taken 349 times.
✓ Branch 1 taken 669 times.
✓ Branch 2 taken 349 times.
✗ Branch 3 not taken.
1018 } else if (!strcmp(keyword, "PCOUNT") && sscanf(value, "%"SCNd64"", &t) == 1) {
219
2/4
✓ Branch 0 taken 349 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 349 times.
349 if (t < 0 || t > INT_MAX)
220 return AVERROR_INVALIDDATA;
221 349 header->pcount = t;
222 }
223 2645 dict_set_if_not_null(metadata, keyword, value);
224 2645 break;
225 }
226 4796 return 0;
227 }
228