2017-08-29 19:12:26 +02:00
|
|
|
/*
|
|
|
|
* FITS demuxer
|
|
|
|
* Copyright (c) 2017 Paras Chadha
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* FITS demuxer.
|
|
|
|
*/
|
|
|
|
|
2024-02-10 16:50:43 +02:00
|
|
|
#include "demux.h"
|
2017-08-29 19:12:26 +02:00
|
|
|
#include "internal.h"
|
|
|
|
#include "libavutil/opt.h"
|
|
|
|
#include "libavcodec/fits.h"
|
|
|
|
|
|
|
|
#define FITS_BLOCK_SIZE 2880
|
|
|
|
|
|
|
|
typedef struct FITSContext {
|
|
|
|
const AVClass *class;
|
|
|
|
AVRational framerate;
|
|
|
|
int first_image;
|
|
|
|
} FITSContext;
|
|
|
|
|
2019-03-21 02:18:37 +02:00
|
|
|
static int fits_probe(const AVProbeData *p)
|
2017-08-29 19:12:26 +02:00
|
|
|
{
|
|
|
|
const uint8_t *b = p->buf;
|
|
|
|
if (!memcmp(b, "SIMPLE = T", 30))
|
|
|
|
return AVPROBE_SCORE_MAX - 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fits_read_header(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
AVStream *st;
|
|
|
|
FITSContext * fits = s->priv_data;
|
|
|
|
|
|
|
|
st = avformat_new_stream(s, NULL);
|
|
|
|
if (!st)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
|
|
|
|
st->codecpar->codec_id = AV_CODEC_ID_FITS;
|
|
|
|
|
|
|
|
avpriv_set_pts_info(st, 64, fits->framerate.den, fits->framerate.num);
|
|
|
|
fits->first_image = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses header and checks that the current HDU contains image or not
|
|
|
|
* It also stores the header in the avbuf and stores the size of data part in data_size
|
|
|
|
* @param s pointer to AVFormat Context
|
|
|
|
* @param fits pointer to FITSContext
|
|
|
|
* @param header pointer to FITSHeader
|
2024-03-22 20:22:25 +02:00
|
|
|
* @param pkt pointer to AVPacket to store the header
|
2017-08-29 19:12:26 +02:00
|
|
|
* @param data_size to store the size of data part
|
2024-03-22 20:22:25 +02:00
|
|
|
* @return 1 if image found, 0 if any other extension and AVERROR code otherwise
|
2017-08-29 19:12:26 +02:00
|
|
|
*/
|
2024-03-22 20:22:25 +02:00
|
|
|
static int is_image(AVFormatContext *s, FITSContext *fits, FITSHeader *header,
|
|
|
|
AVPacket *pkt, uint64_t *data_size)
|
2017-08-29 19:12:26 +02:00
|
|
|
{
|
|
|
|
int i, ret, image = 0;
|
2024-03-22 20:22:25 +02:00
|
|
|
int64_t size = 0, t;
|
2017-08-29 19:12:26 +02:00
|
|
|
|
|
|
|
do {
|
2024-03-22 20:22:25 +02:00
|
|
|
const uint8_t *buf, *buf_end;
|
|
|
|
ret = av_append_packet(s->pb, pkt, FITS_BLOCK_SIZE);
|
2017-08-29 19:12:26 +02:00
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
} else if (ret < FITS_BLOCK_SIZE) {
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
2024-03-22 20:22:25 +02:00
|
|
|
buf_end = pkt->data + pkt->size;
|
|
|
|
buf = buf_end - FITS_BLOCK_SIZE;
|
|
|
|
while(!ret && buf < buf_end) {
|
|
|
|
ret = avpriv_fits_header_parse_line(s, header, buf, NULL);
|
|
|
|
buf += 80;
|
2017-08-29 19:12:26 +02:00
|
|
|
}
|
|
|
|
} while (!ret);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
image = fits->first_image || header->image_extension;
|
|
|
|
fits->first_image = 0;
|
|
|
|
|
|
|
|
if (header->groups) {
|
|
|
|
image = 0;
|
|
|
|
if (header->naxis > 1)
|
|
|
|
size = 1;
|
|
|
|
} else if (header->naxis) {
|
|
|
|
size = header->naxisn[0];
|
|
|
|
} else {
|
|
|
|
image = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 1; i < header->naxis; i++) {
|
|
|
|
if(size && header->naxisn[i] > UINT64_MAX / size)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
size *= header->naxisn[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
if(header->pcount > UINT64_MAX - size)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
size += header->pcount;
|
|
|
|
|
|
|
|
t = (abs(header->bitpix) >> 3) * ((int64_t) header->gcount);
|
2020-11-04 00:41:36 +02:00
|
|
|
if(size && t > INT64_MAX / size)
|
2017-08-29 19:12:26 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
size *= t;
|
|
|
|
|
|
|
|
if (!size) {
|
|
|
|
image = 0;
|
|
|
|
} else {
|
2020-11-04 00:41:36 +02:00
|
|
|
if(FITS_BLOCK_SIZE - 1 > INT64_MAX - size)
|
2017-08-29 19:12:26 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
size = ((size + FITS_BLOCK_SIZE - 1) / FITS_BLOCK_SIZE) * FITS_BLOCK_SIZE;
|
|
|
|
}
|
|
|
|
*data_size = size;
|
|
|
|
return image;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fits_read_packet(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
uint64_t size;
|
|
|
|
FITSContext *fits = s->priv_data;
|
|
|
|
FITSHeader header;
|
2024-03-22 20:22:25 +02:00
|
|
|
int ret;
|
2017-08-29 19:12:26 +02:00
|
|
|
|
|
|
|
if (fits->first_image) {
|
|
|
|
avpriv_fits_header_init(&header, STATE_SIMPLE);
|
|
|
|
} else {
|
|
|
|
avpriv_fits_header_init(&header, STATE_XTENSION);
|
|
|
|
}
|
|
|
|
|
2024-03-22 20:22:25 +02:00
|
|
|
while ((ret = is_image(s, fits, &header, pkt, &size)) == 0) {
|
|
|
|
int64_t pos = avio_skip(s->pb, size);
|
2017-08-29 19:12:26 +02:00
|
|
|
if (pos < 0)
|
|
|
|
return pos;
|
|
|
|
|
|
|
|
avpriv_fits_header_init(&header, STATE_XTENSION);
|
2024-03-22 20:22:25 +02:00
|
|
|
av_packet_unref(pkt);
|
2017-08-29 19:12:26 +02:00
|
|
|
}
|
|
|
|
if (ret < 0)
|
2024-03-22 20:22:25 +02:00
|
|
|
return ret;
|
2017-08-29 19:12:26 +02:00
|
|
|
|
|
|
|
pkt->stream_index = 0;
|
2024-03-22 20:22:25 +02:00
|
|
|
pkt->flags |= AV_PKT_FLAG_KEY;
|
|
|
|
pkt->duration = 1;
|
|
|
|
// Header is sent with the first line removed...
|
|
|
|
pkt->data += 80;
|
|
|
|
pkt->size -= 80;
|
2017-08-29 19:12:26 +02:00
|
|
|
|
2024-03-22 20:22:25 +02:00
|
|
|
if (size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE - pkt->size)
|
|
|
|
return AVERROR(ERANGE);
|
2017-08-29 19:12:26 +02:00
|
|
|
|
2024-03-22 20:22:25 +02:00
|
|
|
ret = av_append_packet(s->pb, pkt, size);
|
2023-05-21 20:55:32 +02:00
|
|
|
if (ret < 0)
|
2017-08-29 19:12:26 +02:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const AVOption fits_options[] = {
|
|
|
|
{ "framerate", "set the framerate", offsetof(FITSContext, framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "1"}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM},
|
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVClass fits_demuxer_class = {
|
|
|
|
.class_name = "FITS demuxer",
|
2024-01-19 14:33:28 +02:00
|
|
|
.item_name = av_default_item_name,
|
2017-08-29 19:12:26 +02:00
|
|
|
.option = fits_options,
|
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
2023-05-21 20:40:57 +02:00
|
|
|
.category = AV_CLASS_CATEGORY_DEMUXER,
|
2017-08-29 19:12:26 +02:00
|
|
|
};
|
|
|
|
|
2024-02-10 16:50:43 +02:00
|
|
|
const FFInputFormat ff_fits_demuxer = {
|
|
|
|
.p.name = "fits",
|
|
|
|
.p.long_name = NULL_IF_CONFIG_SMALL("Flexible Image Transport System"),
|
|
|
|
.p.priv_class = &fits_demuxer_class,
|
|
|
|
.p.flags = AVFMT_NOTIMESTAMPS,
|
2017-08-29 19:12:26 +02:00
|
|
|
.priv_data_size = sizeof(FITSContext),
|
|
|
|
.read_probe = fits_probe,
|
|
|
|
.read_header = fits_read_header,
|
|
|
|
.read_packet = fits_read_packet,
|
|
|
|
};
|