Commit 61e4db4b authored by Paras Chadha's avatar Paras Chadha Committed by Paul B Mahol

Add FITS Decoder

Signed-off-by: 's avatarParas Chadha <paraschadha18@gmail.com>
parent 7fbc0825
......@@ -38,6 +38,7 @@ version <next>:
- Some video filters with several inputs now use a common set of options:
blend, libvmaf, lut3d, overlay, psnr, ssim.
They must always be used by name.
- FITS demuxer and decoder
version 3.3:
- CrystalHD decoder moved to new decode API
......
......@@ -593,6 +593,8 @@ following image formats are supported:
@tab Digital Picture Exchange
@item EXR @tab @tab X
@tab OpenEXR
@item FITS @tab @tab X
@tab Flexible Image Transport System
@item JPEG @tab X @tab X
@tab Progressive JPEG is not supported.
@item JPEG 2000 @tab X @tab X
......
......@@ -291,6 +291,7 @@ OBJS-$(CONFIG_FFV1_DECODER) += ffv1dec.o ffv1.o
OBJS-$(CONFIG_FFV1_ENCODER) += ffv1enc.o ffv1.o
OBJS-$(CONFIG_FFWAVESYNTH_DECODER) += ffwavesynth.o
OBJS-$(CONFIG_FIC_DECODER) += fic.o
OBJS-$(CONFIG_FITS_DECODER) += fitsdec.o fits.o
OBJS-$(CONFIG_FLAC_DECODER) += flacdec.o flacdata.o flac.o
OBJS-$(CONFIG_FLAC_ENCODER) += flacenc.o flacdata.o flac.o vorbis_data.o
OBJS-$(CONFIG_FLASHSV_DECODER) += flashsv.o
......@@ -847,6 +848,7 @@ OBJS-$(CONFIG_ISO_MEDIA) += mpeg4audio.o mpegaudiodata.o
OBJS-$(CONFIG_ADTS_MUXER) += mpeg4audio.o
OBJS-$(CONFIG_CAF_DEMUXER) += ac3tab.o
OBJS-$(CONFIG_DNXHD_DEMUXER) += dnxhddata.o
OBJS-$(CONFIG_FITS_DEMUXER) += fits.o
OBJS-$(CONFIG_FLV_DEMUXER) += mpeg4audio.o
OBJS-$(CONFIG_LATM_MUXER) += mpeg4audio.o
OBJS-$(CONFIG_MATROSKA_AUDIO_MUXER) += mpeg4audio.o
......
......@@ -192,6 +192,7 @@ static void register_all(void)
REGISTER_ENCDEC (FFV1, ffv1);
REGISTER_ENCDEC (FFVHUFF, ffvhuff);
REGISTER_DECODER(FIC, fic);
REGISTER_DECODER(FITS, fits);
REGISTER_ENCDEC (FLASHSV, flashsv);
REGISTER_ENCDEC (FLASHSV2, flashsv2);
REGISTER_DECODER(FLIC, flic);
......
......@@ -447,6 +447,7 @@ enum AVCodecID {
AV_CODEC_ID_SRGC,
AV_CODEC_ID_SVG,
AV_CODEC_ID_GDV,
AV_CODEC_ID_FITS,
/* various PCM "codecs" */
AV_CODEC_ID_FIRST_AUDIO = 0x10000, ///< A dummy id pointing at the start of audio codecs
......
......@@ -1463,6 +1463,13 @@ static const AVCodecDescriptor codec_descriptors[] = {
.props = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSY |
AV_CODEC_PROP_LOSSLESS,
},
{
.id = AV_CODEC_ID_FITS,
.type = AVMEDIA_TYPE_VIDEO,
.name = "fits",
.long_name = NULL_IF_CONFIG_SMALL("FITS image"),
.props = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSLESS,
},
{
.id = AV_CODEC_ID_GIF,
.type = AVMEDIA_TYPE_VIDEO,
......
/*
* FITS implementation of common functions
* 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
*/
#include "avcodec.h"
#include "libavutil/dict.h"
#include "fits.h"
int avpriv_fits_header_init(FITSHeader *header, FITSHeaderState state)
{
header->state = state;
header->naxis_index = 0;
header->blank_found = 0;
header->pcount = 0;
header->gcount = 1;
header->groups = 0;
header->rgb = 0;
header->image_extension = 0;
header->bscale = 1.0;
header->bzero = 0;
header->data_min_found = 0;
header->data_max_found = 0;
return 0;
}
static int dict_set_if_not_null(AVDictionary ***metadata, char *keyword, char *value)
{
if (metadata)
av_dict_set(*metadata, keyword, value, 0);
return 0;
}
/**
* Extract keyword and value from a header line (80 bytes) and store them in keyword and value strings respectively
* @param ptr8 pointer to the data
* @param keyword pointer to the char array in which keyword is to be stored
* @param value pointer to the char array in which value is to be stored
* @return 0 if calculated successfully otherwise AVERROR_INVALIDDATA
*/
static int read_keyword_value(const uint8_t *ptr8, char *keyword, char *value)
{
int i;
for (i = 0; i < 8 && ptr8[i] != ' '; i++) {
keyword[i] = ptr8[i];
}
keyword[i] = '\0';
if (ptr8[8] == '=') {
i = 10;
while (i < 80 && ptr8[i] == ' ') {
i++;
}
if (i < 80) {
*value++ = ptr8[i];
i++;
if (ptr8[i-1] == '\'') {
for (; i < 80 && ptr8[i] != '\''; i++) {
*value++ = ptr8[i];
}
*value++ = '\'';
} else if (ptr8[i-1] == '(') {
for (; i < 80 && ptr8[i] != ')'; i++) {
*value++ = ptr8[i];
}
*value++ = ')';
} else {
for (; i < 80 && ptr8[i] != ' ' && ptr8[i] != '/'; i++) {
*value++ = ptr8[i];
}
}
}
}
*value = '\0';
return 0;
}
#define CHECK_KEYWORD(key) \
if (strcmp(keyword, key)) { \
av_log(avcl, AV_LOG_ERROR, "expected %s keyword, found %s = %s\n", key, keyword, value); \
return AVERROR_INVALIDDATA; \
}
#define CHECK_VALUE(key, val) \
if (sscanf(value, "%d", &header->val) != 1) { \
av_log(avcl, AV_LOG_ERROR, "invalid value of %s keyword, %s = %s\n", key, keyword, value); \
return AVERROR_INVALIDDATA; \
}
int avpriv_fits_header_parse_line(void *avcl, FITSHeader *header, const uint8_t line[80], AVDictionary ***metadata)
{
int dim_no, ret;
int64_t t;
double d;
char keyword[10], value[72], c;
read_keyword_value(line, keyword, value);
switch (header->state) {
case STATE_SIMPLE:
CHECK_KEYWORD("SIMPLE");
if (value[0] == 'F') {
av_log(avcl, AV_LOG_WARNING, "not a standard FITS file\n");
} else if (value[0] != 'T') {
av_log(avcl, AV_LOG_ERROR, "invalid value of SIMPLE keyword, SIMPLE = %c\n", value[0]);
return AVERROR_INVALIDDATA;
}
header->state = STATE_BITPIX;
break;
case STATE_XTENSION:
CHECK_KEYWORD("XTENSION");
if (!strcmp(value, "'IMAGE '")) {
header->image_extension = 1;
}
header->state = STATE_BITPIX;
break;
case STATE_BITPIX:
CHECK_KEYWORD("BITPIX");
CHECK_VALUE("BITPIX", bitpix);
dict_set_if_not_null(metadata, keyword, value);
header->state = STATE_NAXIS;
break;
case STATE_NAXIS:
CHECK_KEYWORD("NAXIS");
CHECK_VALUE("NAXIS", naxis);
dict_set_if_not_null(metadata, keyword, value);
if (header->naxis) {
header->state = STATE_NAXIS_N;
} else {
header->state = STATE_REST;
}
break;
case STATE_NAXIS_N:
ret = sscanf(keyword, "NAXIS%d", &dim_no);
if (ret != 1 || dim_no != header->naxis_index + 1) {
av_log(avcl, AV_LOG_ERROR, "expected NAXIS%d keyword, found %s = %s\n", header->naxis_index + 1, keyword, value);
return AVERROR_INVALIDDATA;
}
if (sscanf(value, "%d", &header->naxisn[header->naxis_index]) != 1) {
av_log(avcl, AV_LOG_ERROR, "invalid value of NAXIS%d keyword, %s = %s\n", header->naxis_index + 1, keyword, value);
return AVERROR_INVALIDDATA;
}
dict_set_if_not_null(metadata, keyword, value);
header->naxis_index++;
if (header->naxis_index == header->naxis) {
header->state = STATE_REST;
}
break;
case STATE_REST:
if (!strcmp(keyword, "BLANK") && sscanf(value, "%"SCNd64"", &t) == 1) {
header->blank = t;
header->blank_found = 1;
} else if (!strcmp(keyword, "BSCALE") && sscanf(value, "%lf", &d) == 1) {
header->bscale = d;
} else if (!strcmp(keyword, "BZERO") && sscanf(value, "%lf", &d) == 1) {
header->bzero = d;
} else if (!strcmp(keyword, "CTYPE3") && !strncmp(value, "'RGB", 4)) {
header->rgb = 1;
} else if (!strcmp(keyword, "DATAMAX") && sscanf(value, "%lf", &d) == 1) {
header->data_max_found = 1;
header->data_max = d;
} else if (!strcmp(keyword, "DATAMIN") && sscanf(value, "%lf", &d) == 1) {
header->data_min_found = 1;
header->data_min = d;
} else if (!strcmp(keyword, "END")) {
return 1;
} else if (!strcmp(keyword, "GROUPS") && sscanf(value, "%c", &c) == 1) {
header->groups = (c == 'T');
} else if (!strcmp(keyword, "GCOUNT") && sscanf(value, "%"SCNd64"", &t) == 1) {
header->gcount = t;
} else if (!strcmp(keyword, "PCOUNT") && sscanf(value, "%"SCNd64"", &t) == 1) {
header->pcount = t;
}
dict_set_if_not_null(metadata, keyword, value);
break;
}
return 0;
}
/*
* FITS image format common prototypes and structures
* 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
*/
#ifndef AVCODEC_FITS_H
#define AVCODEC_FITS_H
typedef enum FITSHeaderState {
STATE_SIMPLE,
STATE_XTENSION,
STATE_BITPIX,
STATE_NAXIS,
STATE_NAXIS_N,
STATE_PCOUNT,
STATE_GCOUNT,
STATE_REST,
} FITSHeaderState;
/**
* Structure to store the header keywords in FITS file
*/
typedef struct FITSHeader {
FITSHeaderState state;
unsigned naxis_index;
int bitpix;
int64_t blank;
int blank_found;
int naxis;
int naxisn[999];
int pcount;
int gcount;
int groups;
int rgb; /**< 1 if file contains RGB image, 0 otherwise */
int image_extension;
double bscale;
double bzero;
int data_min_found;
double data_min;
int data_max_found;
double data_max;
} FITSHeader;
/**
* Initialize a single header line
* @param header pointer to the header
* @param state current state of parsing the header
* @return 0 if successful otherwise AVERROR_INVALIDDATA
*/
int avpriv_fits_header_init(FITSHeader *header, FITSHeaderState state);
/**
* Parse a single header line
* @param avcl used in av_log
* @param header pointer to the header
* @param line one header line
* @param metadata used to store metadata while decoding
* @return 0 if successful otherwise AVERROR_INVALIDDATA
*/
int avpriv_fits_header_parse_line(void *avcl, FITSHeader *header, const uint8_t line[80], AVDictionary ***metadata);
#endif /* AVCODEC_FITS_H */
This diff is collapsed.
......@@ -28,8 +28,8 @@
#include "libavutil/version.h"
#define LIBAVCODEC_VERSION_MAJOR 57
#define LIBAVCODEC_VERSION_MINOR 103
#define LIBAVCODEC_VERSION_MICRO 101
#define LIBAVCODEC_VERSION_MINOR 104
#define LIBAVCODEC_VERSION_MICRO 100
#define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
LIBAVCODEC_VERSION_MINOR, \
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment