FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/fitsdec.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 68 83 81.9%
Functions: 4 4 100.0%
Branches: 37 54 68.5%

Line Branch Exec Source
1 /*
2 * FITS demuxer
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 demuxer.
25 */
26
27 #include "demux.h"
28 #include "internal.h"
29 #include "libavutil/opt.h"
30 #include "libavcodec/fits.h"
31
32 #define FITS_BLOCK_SIZE 2880
33
34 typedef struct FITSContext {
35 const AVClass *class;
36 AVRational framerate;
37 int first_image;
38 } FITSContext;
39
40 7186 static int fits_probe(const AVProbeData *p)
41 {
42 7186 const uint8_t *b = p->buf;
43
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 7164 times.
7186 if (!memcmp(b, "SIMPLE = T", 30))
44 22 return AVPROBE_SCORE_MAX - 1;
45 7164 return 0;
46 }
47
48 22 static int fits_read_header(AVFormatContext *s)
49 {
50 AVStream *st;
51 22 FITSContext * fits = s->priv_data;
52
53 22 st = avformat_new_stream(s, NULL);
54
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (!st)
55 return AVERROR(ENOMEM);
56
57 22 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
58 22 st->codecpar->codec_id = AV_CODEC_ID_FITS;
59
60 22 avpriv_set_pts_info(st, 64, fits->framerate.den, fits->framerate.num);
61 22 fits->first_image = 1;
62 22 return 0;
63 }
64
65 /**
66 * Parses header and checks that the current HDU contains image or not
67 * It also stores the header in the avbuf and stores the size of data part in data_size
68 * @param s pointer to AVFormat Context
69 * @param fits pointer to FITSContext
70 * @param header pointer to FITSHeader
71 * @param pkt pointer to AVPacket to store the header
72 * @param data_size to store the size of data part
73 * @return 1 if image found, 0 if any other extension and AVERROR code otherwise
74 */
75 237 static int is_image(AVFormatContext *s, FITSContext *fits, FITSHeader *header,
76 AVPacket *pkt, uint64_t *data_size)
77 {
78 237 int i, ret, image = 0;
79 237 int64_t size = 0, t;
80
81 do {
82 const uint8_t *buf, *buf_end;
83 244 ret = av_append_packet(s->pb, pkt, FITS_BLOCK_SIZE);
84
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 206 times.
244 if (ret < 0) {
85 38 return ret;
86
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 206 times.
206 } else if (ret < FITS_BLOCK_SIZE) {
87 return AVERROR_INVALIDDATA;
88 }
89
90 206 ret = 0;
91 206 buf_end = pkt->data + pkt->size;
92 206 buf = buf_end - FITS_BLOCK_SIZE;
93
4/4
✓ Branch 0 taken 2604 times.
✓ Branch 1 taken 199 times.
✓ Branch 2 taken 2597 times.
✓ Branch 3 taken 7 times.
2803 while(!ret && buf < buf_end) {
94 2597 ret = avpriv_fits_header_parse_line(s, header, buf, NULL);
95 2597 buf += 80;
96 }
97
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 199 times.
206 } while (!ret);
98
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 199 times.
199 if (ret < 0)
99 return ret;
100
101
4/4
✓ Branch 0 taken 177 times.
✓ Branch 1 taken 22 times.
✓ Branch 2 taken 176 times.
✓ Branch 3 taken 1 times.
199 image = fits->first_image || header->image_extension;
102 199 fits->first_image = 0;
103
104
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 199 times.
199 if (header->groups) {
105 image = 0;
106 if (header->naxis > 1)
107 size = 1;
108
1/2
✓ Branch 0 taken 199 times.
✗ Branch 1 not taken.
199 } else if (header->naxis) {
109 199 size = header->naxisn[0];
110 } else {
111 image = 0;
112 }
113
114
2/2
✓ Branch 0 taken 342 times.
✓ Branch 1 taken 199 times.
541 for (i = 1; i < header->naxis; i++) {
115
2/4
✓ Branch 0 taken 342 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 342 times.
342 if(size && header->naxisn[i] > UINT64_MAX / size)
116 return AVERROR_INVALIDDATA;
117 342 size *= header->naxisn[i];
118 }
119
120
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 199 times.
199 if(header->pcount > UINT64_MAX - size)
121 return AVERROR_INVALIDDATA;
122 199 size += header->pcount;
123
124 199 t = (abs(header->bitpix) >> 3) * ((int64_t) header->gcount);
125
2/4
✓ Branch 0 taken 199 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 199 times.
199 if(size && t > INT64_MAX / size)
126 return AVERROR_INVALIDDATA;
127 199 size *= t;
128
129
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 199 times.
199 if (!size) {
130 image = 0;
131 } else {
132
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 199 times.
199 if(FITS_BLOCK_SIZE - 1 > INT64_MAX - size)
133 return AVERROR_INVALIDDATA;
134 199 size = ((size + FITS_BLOCK_SIZE - 1) / FITS_BLOCK_SIZE) * FITS_BLOCK_SIZE;
135 }
136 199 *data_size = size;
137 199 return image;
138 }
139
140 236 static int fits_read_packet(AVFormatContext *s, AVPacket *pkt)
141 {
142 uint64_t size;
143 236 FITSContext *fits = s->priv_data;
144 FITSHeader header;
145 int ret;
146
147
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 214 times.
236 if (fits->first_image) {
148 22 avpriv_fits_header_init(&header, STATE_SIMPLE);
149 } else {
150 214 avpriv_fits_header_init(&header, STATE_XTENSION);
151 }
152
153
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 236 times.
237 while ((ret = is_image(s, fits, &header, pkt, &size)) == 0) {
154 1 int64_t pos = avio_skip(s->pb, size);
155
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (pos < 0)
156 return pos;
157
158 1 avpriv_fits_header_init(&header, STATE_XTENSION);
159 1 av_packet_unref(pkt);
160 }
161
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 198 times.
236 if (ret < 0)
162 38 return ret;
163
164 198 pkt->stream_index = 0;
165 198 pkt->flags |= AV_PKT_FLAG_KEY;
166 198 pkt->duration = 1;
167 // Header is sent with the first line removed...
168 198 pkt->data += 80;
169 198 pkt->size -= 80;
170
171
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 198 times.
198 if (size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE - pkt->size)
172 return AVERROR(ERANGE);
173
174 198 ret = av_append_packet(s->pb, pkt, size);
175
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 198 times.
198 if (ret < 0)
176 return ret;
177
178 198 return 0;
179 }
180
181 static const AVOption fits_options[] = {
182 { "framerate", "set the framerate", offsetof(FITSContext, framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "1"}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM},
183 { NULL },
184 };
185
186 static const AVClass fits_demuxer_class = {
187 .class_name = "FITS demuxer",
188 .item_name = av_default_item_name,
189 .option = fits_options,
190 .version = LIBAVUTIL_VERSION_INT,
191 .category = AV_CLASS_CATEGORY_DEMUXER,
192 };
193
194 const FFInputFormat ff_fits_demuxer = {
195 .p.name = "fits",
196 .p.long_name = NULL_IF_CONFIG_SMALL("Flexible Image Transport System"),
197 .p.priv_class = &fits_demuxer_class,
198 .p.flags = AVFMT_NOTIMESTAMPS,
199 .priv_data_size = sizeof(FITSContext),
200 .read_probe = fits_probe,
201 .read_header = fits_read_header,
202 .read_packet = fits_read_packet,
203 };
204