FFmpeg
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 "libavutil/avassert.h"
28 #include "libavutil/intreadwrite.h"
29 #include "internal.h"
30 #include "libavutil/opt.h"
31 #include "libavcodec/fits.h"
32 #include "libavutil/bprint.h"
33 
34 #define FITS_BLOCK_SIZE 2880
35 
36 typedef struct FITSContext {
37  const AVClass *class;
40  int64_t pts;
41 } FITSContext;
42 
43 static int fits_probe(const AVProbeData *p)
44 {
45  const uint8_t *b = p->buf;
46  if (!memcmp(b, "SIMPLE = T", 30))
47  return AVPROBE_SCORE_MAX - 1;
48  return 0;
49 }
50 
52 {
53  AVStream *st;
54  FITSContext * fits = s->priv_data;
55 
56  st = avformat_new_stream(s, NULL);
57  if (!st)
58  return AVERROR(ENOMEM);
59 
62 
63  avpriv_set_pts_info(st, 64, fits->framerate.den, fits->framerate.num);
64  fits->pts = 0;
65  fits->first_image = 1;
66  return 0;
67 }
68 
69 /**
70  * Parses header and checks that the current HDU contains image or not
71  * It also stores the header in the avbuf and stores the size of data part in data_size
72  * @param s pointer to AVFormat Context
73  * @param fits pointer to FITSContext
74  * @param header pointer to FITSHeader
75  * @param avbuf pointer to AVBPrint to store the header
76  * @param data_size to store the size of data part
77  * @return 1 if image found, 0 if any other extension and AVERROR_INVALIDDATA otherwise
78  */
80  AVBPrint *avbuf, uint64_t *data_size)
81 {
82  int i, ret, image = 0;
83  char buf[FITS_BLOCK_SIZE] = { 0 };
84  int64_t buf_size = 0, size = 0, t;
85 
86  do {
88  if (ret < 0) {
89  return ret;
90  } else if (ret < FITS_BLOCK_SIZE) {
91  return AVERROR_INVALIDDATA;
92  }
93 
95  ret = 0;
96  buf_size = 0;
97  while(!ret && buf_size < FITS_BLOCK_SIZE) {
99  buf_size += 80;
100  }
101  } while (!ret);
102  if (ret < 0)
103  return ret;
104 
105  image = fits->first_image || header->image_extension;
106  fits->first_image = 0;
107 
108  if (header->groups) {
109  image = 0;
110  if (header->naxis > 1)
111  size = 1;
112  } else if (header->naxis) {
113  size = header->naxisn[0];
114  } else {
115  image = 0;
116  }
117 
118  for (i = 1; i < header->naxis; i++) {
119  if(size && header->naxisn[i] > UINT64_MAX / size)
120  return AVERROR_INVALIDDATA;
121  size *= header->naxisn[i];
122  }
123 
124  if(header->pcount > UINT64_MAX - size)
125  return AVERROR_INVALIDDATA;
126  size += header->pcount;
127 
128  t = (abs(header->bitpix) >> 3) * ((int64_t) header->gcount);
129  if(size && t > INT64_MAX / size)
130  return AVERROR_INVALIDDATA;
131  size *= t;
132 
133  if (!size) {
134  image = 0;
135  } else {
136  if(FITS_BLOCK_SIZE - 1 > INT64_MAX - size)
137  return AVERROR_INVALIDDATA;
139  }
140  *data_size = size;
141  return image;
142 }
143 
145 {
146  int64_t pos, ret;
147  uint64_t size;
148  FITSContext *fits = s->priv_data;
150  AVBPrint avbuf;
151  char *buf;
152 
153  if (fits->first_image) {
155  } else {
157  }
158 
160  while ((ret = is_image(s, fits, &header, &avbuf, &size)) == 0) {
161  av_bprint_finalize(&avbuf, NULL);
162  pos = avio_skip(s->pb, size);
163  if (pos < 0)
164  return pos;
165 
168  }
169  if (ret < 0)
170  goto fail;
171 
172  if (!av_bprint_is_complete(&avbuf)) {
173  ret = AVERROR(ENOMEM);
174  goto fail;
175  }
176 
177  av_assert0(avbuf.len <= INT64_MAX && size <= INT64_MAX);
178  if (avbuf.len + size > INT_MAX - 80) {
180  goto fail;
181  }
182  // Header is sent with the first line removed...
183  ret = av_new_packet(pkt, avbuf.len - 80 + size);
184  if (ret < 0)
185  goto fail;
186 
187  pkt->stream_index = 0;
189 
190  ret = av_bprint_finalize(&avbuf, &buf);
191  if (ret < 0) {
193  return ret;
194  }
195 
196  memcpy(pkt->data, buf + 80, avbuf.len - 80);
197  pkt->size = avbuf.len - 80;
198  av_freep(&buf);
199  ret = avio_read(s->pb, pkt->data + pkt->size, size);
200  if (ret < 0) {
202  return ret;
203  }
204 
205  pkt->size += ret;
206  pkt->pts = fits->pts;
207  fits->pts++;
208 
209  return 0;
210 
211 fail:
212  av_bprint_finalize(&avbuf, NULL);
213  return ret;
214 }
215 
216 static const AVOption fits_options[] = {
217  { "framerate", "set the framerate", offsetof(FITSContext, framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "1"}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM},
218  { NULL },
219 };
220 
221 static const AVClass fits_demuxer_class = {
222  .class_name = "FITS demuxer",
223  .item_name = av_default_item_name,
224  .option = fits_options,
225  .version = LIBAVUTIL_VERSION_INT,
226 };
227 
229  .name = "fits",
230  .long_name = NULL_IF_CONFIG_SMALL("Flexible Image Transport System"),
231  .priv_data_size = sizeof(FITSContext),
235  .priv_class = &fits_demuxer_class,
236  .raw_codec_id = AV_CODEC_ID_FITS,
237 };
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:599
AV_BPRINT_SIZE_UNLIMITED
#define AV_BPRINT_SIZE_UNLIMITED
fits_demuxer_class
static const AVClass fits_demuxer_class
Definition: fitsdec.c:221
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4480
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3953
av_bprint_finalize
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition: bprint.c:235
av_bprint_init
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition: bprint.c:69
AV_OPT_TYPE_VIDEO_RATE
@ AV_OPT_TYPE_VIDEO_RATE
offset must point to AVRational
Definition: opt.h:236
FITSContext
Definition: fitsdec.c:42
AVPacket::data
uint8_t * data
Definition: avcodec.h:1477
AVOption
AVOption.
Definition: opt.h:246
b
#define b
Definition: input.c:41
av_bprint_append_data
void av_bprint_append_data(AVBPrint *buf, const char *data, unsigned size)
Append data to a print buffer.
Definition: bprint.c:158
FITSContext::framerate
AVRational framerate
Definition: fitsdec.c:38
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1509
STATE_XTENSION
@ STATE_XTENSION
Definition: fits.h:31
fits.h
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:458
framerate
int framerate
Definition: h264_levels.c:65
fits_options
static const AVOption fits_options[]
Definition: fitsdec.c:216
fail
#define fail()
Definition: checkasm.h:120
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:108
fits_probe
static int fits_probe(const AVProbeData *p)
Definition: fitsdec.c:43
FITSContext::pts
int64_t pts
Definition: fitsdec.c:40
AVRational::num
int num
Numerator.
Definition: rational.h:59
avassert.h
buf
void * buf
Definition: avisynth_c.h:766
AVInputFormat
Definition: avformat.h:640
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:257
av_new_packet
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:86
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:645
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:448
is_image
static int64_t is_image(AVFormatContext *s, FITSContext *fits, FITSHeader *header, AVBPrint *avbuf, 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:79
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
AVFormatContext
Format I/O context.
Definition: avformat.h:1342
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1017
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:530
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
NULL
#define NULL
Definition: coverity.c:32
read_probe
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
AV_CODEC_ID_FITS
@ AV_CODEC_ID_FITS
Definition: avcodec.h:449
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:191
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:446
abs
#define abs(x)
Definition: cuda_runtime.h:35
av_bprint_is_complete
static int av_bprint_is_complete(const AVBPrint *buf)
Test if the print buffer is complete (not truncated).
Definition: bprint.h:185
fits_read_packet
static int fits_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: fitsdec.c:144
AVPacket::size
int size
Definition: avcodec.h:1478
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:188
ff_fits_demuxer
AVInputFormat ff_fits_demuxer
Definition: fitsdec.c:228
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4910
size
int size
Definition: twinvq_data.h:11134
fits_read_header
static int fits_read_header(AVFormatContext *s)
Definition: fitsdec.c:51
header
static const uint8_t header[24]
Definition: sdr2.c:67
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1483
avpriv_fits_header_init
int avpriv_fits_header_init(FITSHeader *header, FITSHeaderState state)
Initialize a single header line.
Definition: fits.c:26
bprint.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1470
STATE_SIMPLE
@ STATE_SIMPLE
Definition: fits.h:30
AV_OPT_FLAG_DECODING_PARAM
#define AV_OPT_FLAG_DECODING_PARAM
a generic parameter which can be set by the user for demuxing or decoding
Definition: opt.h:277
uint8_t
uint8_t
Definition: audio_convert.c:194
ret
ret
Definition: filter_design.txt:187
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
AVStream
Stream structure.
Definition: avformat.h:870
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
FITSContext::first_image
int first_image
Definition: fitsdec.c:39
pkt
static AVPacket pkt
Definition: demuxing_decoding.c:54
AVRational::den
int den
Denominator.
Definition: rational.h:60
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:647
FITSHeader
Structure to store the header keywords in FITS file.
Definition: fits.h:43
AVPacket::stream_index
int stream_index
Definition: avcodec.h:1479
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:331
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3957
AVPacket
This structure stores compressed data.
Definition: avcodec.h:1454
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
FITS_BLOCK_SIZE
#define FITS_BLOCK_SIZE
Definition: fitsdec.c:34
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59