FFmpeg
Loading...
Searching...
No Matches
fitsdec.c
Go to the documentation of this file.
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
34typedef struct FITSContext {
35 const AVClass *class;
39
40static int fits_probe(const AVProbeData *p)
41{
42 const uint8_t *b = p->buf;
43 if (!memcmp(b, "SIMPLE = T", 30))
44 return AVPROBE_SCORE_MAX - 1;
45 return 0;
46}
47
49{
50 AVStream *st;
51 FITSContext * fits = s->priv_data;
52
54 if (!st)
55 return AVERROR(ENOMEM);
56
59
60 avpriv_set_pts_info(st, 64, fits->framerate.den, fits->framerate.num);
61 fits->first_image = 1;
62 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 */
76 AVPacket *pkt, uint64_t *data_size)
77{
78 int i, ret, image = 0;
79 int64_t size = 0, t;
80
81 do {
82 const uint8_t *buf, *buf_end;
84 if (ret < 0) {
85 return ret;
86 } else if (ret < FITS_BLOCK_SIZE) {
88 }
89
90 ret = 0;
91 buf_end = pkt->data + pkt->size;
92 buf = buf_end - FITS_BLOCK_SIZE;
93 while(!ret && buf < buf_end) {
95 buf += 80;
96 }
97 } while (!ret);
98 if (ret < 0)
99 return ret;
100
101 image = fits->first_image || header->image_extension;
102 fits->first_image = 0;
103
104 if (header->groups) {
105 image = 0;
106 if (header->naxis > 1)
107 size = 1;
108 } else if (header->naxis) {
109 size = header->naxisn[0];
110 } else {
111 image = 0;
112 }
113
114 for (i = 1; i < header->naxis; i++) {
115 if(size && header->naxisn[i] > UINT64_MAX / size)
116 return AVERROR_INVALIDDATA;
117 size *= header->naxisn[i];
118 }
119
120 if(header->pcount > UINT64_MAX - size)
121 return AVERROR_INVALIDDATA;
122 size += header->pcount;
123
124 t = (abs(header->bitpix) >> 3) * ((int64_t) header->gcount);
125 if(size && t > INT64_MAX / size)
126 return AVERROR_INVALIDDATA;
127 size *= t;
128
129 if (!size) {
130 image = 0;
131 } else {
132 if(FITS_BLOCK_SIZE - 1 > INT64_MAX - size)
133 return AVERROR_INVALIDDATA;
135 }
136 *data_size = size;
137 return image;
138}
139
141{
142 uint64_t size;
143 FITSContext *fits = s->priv_data;
145 int ret;
146
147 if (fits->first_image) {
149 } else {
151 }
152
153 while ((ret = is_image(s, fits, &header, pkt, &size)) == 0) {
154 int64_t pos = avio_skip(s->pb, size);
155 if (pos < 0)
156 return pos;
157
160 }
161 if (ret < 0)
162 return ret;
163
164 pkt->stream_index = 0;
165 pkt->flags |= AV_PKT_FLAG_KEY;
166 pkt->duration = 1;
167 // Header is sent with the first line removed...
168 pkt->data += 80;
169 pkt->size -= 80;
170
171 if (size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE - pkt->size)
172 return AVERROR(ERANGE);
173
174 ret = av_append_packet(s->pb, pkt, size);
175 if (ret < 0)
176 return ret;
177
178 return 0;
179}
180
181static 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
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
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),
203};
const FFInputFormat ff_fits_demuxer
Definition fitsdec.c:194
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:834
int av_append_packet(AVIOContext *s, AVPacket *pkt, int size)
Read data and append it to the current content of the AVPacket.
Definition utils.c:114
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:483
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition avformat.h:498
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
#define abs(x)
static AVPacket * pkt
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
int avpriv_fits_header_init(FITSHeader *header, FITSHeaderState state)
Initialize a single header line.
Definition fits.c:31
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:115
@ STATE_XTENSION
Definition fits.h:31
@ STATE_SIMPLE
Definition fits.h:30
#define AV_OPT_FLAG_DECODING_PARAM
A generic parameter which can be set by the user for demuxing or decoding.
Definition opt.h:355
@ AV_OPT_TYPE_VIDEO_RATE
Underlying C type is AVRational.
Definition opt.h:314
@ AV_CODEC_ID_FITS
Definition codec_id.h:281
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
Definition defs.h:40
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition packet.c:434
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition packet.h:650
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
#define b
Definition input.c:43
static const AVOption fits_options[]
Definition fitsdec.c:309
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:106
static const AVClass fits_demuxer_class
Definition fitsdec.c:186
static int is_image(AVFormatContext *s, FITSContext *fits, FITSHeader *header, AVPacket *pkt, uint64_t *data_size)
Parses header and checks that the current HDU contains image or not It also stores the header in the ...
Definition fitsdec.c:75
static int fits_probe(const AVProbeData *p)
Definition fitsdec.c:40
static int fits_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition fitsdec.c:140
#define FITS_BLOCK_SIZE
Definition fitsdec.c:32
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
@ AV_CLASS_CATEGORY_DEMUXER
Definition log.h:33
AVOptions.
static const uint8_t header[24]
Definition sdr2.c:68
unsigned int pos
Definition spdifenc.c:431
Describe the class of an AVClass context structure.
Definition log.h:76
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
Format I/O context.
Definition avformat.h:1333
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:580
This structure contains the data a format has to probe a file.
Definition avformat.h:471
Rational number (pair of numerator and denominator).
Definition rational.h:58
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
int first_image
Definition fitsdec.c:37
AVRational framerate
Definition fitsdec.c:36
Structure to store the header keywords in FITS file.
Definition fits.h:43
float framerate
Definition av1_levels.c:29
int size