LCOV - code coverage report
Current view: top level - libavcodec - fitsdec.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 75 101 74.3 %
Date: 2017-12-13 10:57:33 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /*
       2             :  * FITS image decoder
       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 image decoder
      25             :  *
      26             :  * Specification: https://fits.gsfc.nasa.gov/fits_standard.html Version 3.0
      27             :  *
      28             :  * Support all 2d images alongwith, bzero, bscale and blank keywords.
      29             :  * RGBA images are supported as NAXIS3 = 3 or 4 i.e. Planes in RGBA order. Also CTYPE = 'RGB ' should be present.
      30             :  * Also to interpret data, values are linearly scaled using min-max scaling but not RGB images.
      31             :  */
      32             : 
      33             : #include "avcodec.h"
      34             : #include "internal.h"
      35             : #include <float.h>
      36             : #include "libavutil/intreadwrite.h"
      37             : #include "libavutil/intfloat.h"
      38             : #include "libavutil/dict.h"
      39             : #include "libavutil/opt.h"
      40             : #include "fits.h"
      41             : 
      42             : typedef struct FITSContext {
      43             :     const AVClass *class;
      44             :     int blank_val;
      45             : } FITSContext;
      46             : 
      47             : /**
      48             :  * Calculate the data_min and data_max values from the data.
      49             :  * This is called if the values are not present in the header.
      50             :  * @param ptr8 pointer to the data
      51             :  * @param header pointer to the header
      52             :  * @param end pointer to end of packet
      53             :  * @return 0 if calculated successfully otherwise AVERROR_INVALIDDATA
      54             :  */
      55         110 : static int fill_data_min_max(const uint8_t *ptr8, FITSHeader *header, const uint8_t *end)
      56             : {
      57             :     uint8_t t8;
      58             :     int16_t t16;
      59             :     int32_t t32;
      60             :     int64_t t64;
      61             :     float tflt;
      62             :     double tdbl;
      63             :     int i, j;
      64             : 
      65         110 :     header->data_min = DBL_MAX;
      66         110 :     header->data_max = DBL_MIN;
      67         110 :     switch (header->bitpix) {
      68             : #define CASE_N(a, t, rd) \
      69             :     case a: \
      70             :         for (i = 0; i < header->naxisn[1]; i++) { \
      71             :             for (j = 0; j < header->naxisn[0]; j++) { \
      72             :                 t = rd; \
      73             :                 if (!header->blank_found || t != header->blank) { \
      74             :                     if (t > header->data_max) \
      75             :                         header->data_max = t; \
      76             :                     if (t < header->data_min) \
      77             :                         header->data_min = t; \
      78             :                 } \
      79             :                 ptr8 += abs(a) >> 3; \
      80             :             } \
      81             :         } \
      82             :         break
      83             : 
      84           2 :         CASE_N(-64, tdbl, av_int2double(AV_RB64(ptr8)));
      85           2 :         CASE_N(-32, tflt, av_int2float(AV_RB32(ptr8)));
      86          53 :         CASE_N(8, t8, ptr8[0]);
      87          51 :         CASE_N(16, t16, AV_RB16(ptr8));
      88           2 :         CASE_N(32, t32, AV_RB32(ptr8));
      89           0 :         CASE_N(64, t64, AV_RB64(ptr8));
      90           0 :         default:
      91           0 :             return AVERROR_INVALIDDATA;
      92             :     }
      93         110 :     return 0;
      94             : }
      95             : 
      96             : /**
      97             :  * Read the fits header and store the values in FITSHeader pointed by header
      98             :  * @param avctx AVCodec context
      99             :  * @param ptr pointer to pointer to the data
     100             :  * @param header pointer to the FITSHeader
     101             :  * @param end pointer to end of packet
     102             :  * @param metadata pointer to pointer to AVDictionary to store metadata
     103             :  * @return 0 if calculated successfully otherwise AVERROR_INVALIDDATA
     104             :  */
     105         365 : static int fits_read_header(AVCodecContext *avctx, const uint8_t **ptr, FITSHeader *header,
     106             :                             const uint8_t *end, AVDictionary **metadata)
     107             : {
     108         365 :     const uint8_t *ptr8 = *ptr;
     109             :     int lines_read, bytes_left, i, ret;
     110             :     size_t size;
     111             : 
     112         365 :     lines_read = 1; // to account for first header line, SIMPLE or XTENSION which is not included in packet...
     113         365 :     avpriv_fits_header_init(header, STATE_BITPIX);
     114             :     do {
     115        3524 :         if (end - ptr8 < 80)
     116           0 :             return AVERROR_INVALIDDATA;
     117        3524 :         ret = avpriv_fits_header_parse_line(avctx, header, ptr8, &metadata);
     118        3524 :         ptr8 += 80;
     119        3524 :         lines_read++;
     120        3524 :     } while (!ret);
     121         365 :     if (ret < 0)
     122           0 :         return ret;
     123             : 
     124         365 :     bytes_left = (((lines_read + 35) / 36) * 36 - lines_read) * 80;
     125         365 :     if (end - ptr8 < bytes_left)
     126           0 :         return AVERROR_INVALIDDATA;
     127         365 :     ptr8 += bytes_left;
     128             : 
     129         365 :     if (header->rgb && (header->naxis != 3 || (header->naxisn[2] != 3 && header->naxisn[2] != 4))) {
     130           0 :         av_log(avctx, AV_LOG_ERROR, "File contains RGB image but NAXIS = %d and NAXIS3 = %d\n", header->naxis, header->naxisn[2]);
     131           0 :         return AVERROR_INVALIDDATA;
     132             :     }
     133             : 
     134         365 :     if (!header->rgb && header->naxis != 2) {
     135           0 :         av_log(avctx, AV_LOG_ERROR, "unsupported number of dimensions, NAXIS = %d\n", header->naxis);
     136           0 :         return AVERROR_INVALIDDATA;
     137             :     }
     138             : 
     139         365 :     if (header->blank_found && (header->bitpix == -32 || header->bitpix == -64)) {
     140           0 :         av_log(avctx, AV_LOG_WARNING, "BLANK keyword found but BITPIX = %d\n. Ignoring BLANK", header->bitpix);
     141           0 :         header->blank_found = 0;
     142             :     }
     143             : 
     144         365 :     size = abs(header->bitpix) >> 3;
     145        1348 :     for (i = 0; i < header->naxis; i++) {
     146         983 :         if (header->naxisn[i] > SIZE_MAX / size) {
     147           0 :             av_log(avctx, AV_LOG_ERROR, "unsupported size of FITS image");
     148           0 :             return AVERROR_INVALIDDATA;
     149             :         }
     150         983 :         size *= header->naxisn[i];
     151             :     }
     152             : 
     153         365 :     if (end - ptr8 < size)
     154           0 :         return AVERROR_INVALIDDATA;
     155         365 :     *ptr = ptr8;
     156             : 
     157         365 :     if (!header->rgb && (!header->data_min_found || !header->data_max_found)) {
     158         110 :         ret = fill_data_min_max(ptr8, header, end);
     159         220 :         if (ret < 0) {
     160           0 :             av_log(avctx, AV_LOG_ERROR, "invalid BITPIX, %d\n", header->bitpix);
     161           0 :             return ret;
     162             :         }
     163             :     } else {
     164             :         /*
     165             :          * instead of applying bscale and bzero to every element,
     166             :          * we can do inverse transformation on data_min and data_max
     167             :          */
     168         255 :         header->data_min = (header->data_min - header->bzero) / header->bscale;
     169         255 :         header->data_max = (header->data_max - header->bzero) / header->bscale;
     170             :     }
     171             : 
     172         365 :     return 0;
     173             : }
     174             : 
     175         365 : static int fits_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
     176             : {
     177         365 :     AVFrame *p=data;
     178         365 :     const uint8_t *ptr8 = avpkt->data, *end;
     179             :     uint8_t t8;
     180             :     int16_t t16;
     181             :     int32_t t32;
     182             :     int64_t t64;
     183             :     float   tflt;
     184             :     double  tdbl;
     185             :     int ret, i, j, k;
     186         365 :     const int map[] = {2, 0, 1, 3}; // mapping from GBRA -> RGBA as RGBA is to be stored in FITS file..
     187             :     uint8_t *dst8;
     188             :     uint16_t *dst16;
     189             :     uint64_t t;
     190             :     FITSHeader header;
     191         365 :     FITSContext * fitsctx = avctx->priv_data;
     192             : 
     193         365 :     end = ptr8 + avpkt->size;
     194         365 :     p->metadata = NULL;
     195         365 :     ret = fits_read_header(avctx, &ptr8, &header, end, &p->metadata);
     196         365 :     if (ret < 0)
     197           0 :         return ret;
     198             : 
     199         365 :     if (header.rgb) {
     200         253 :         if (header.bitpix == 8) {
     201         147 :             if (header.naxisn[2] == 3) {
     202          53 :                 avctx->pix_fmt = AV_PIX_FMT_GBRP;
     203             :             } else {
     204          94 :                 avctx->pix_fmt = AV_PIX_FMT_GBRAP;
     205             :             }
     206         106 :         } else if (header.bitpix == 16) {
     207         106 :             if (header.naxisn[2] == 3) {
     208          53 :                 avctx->pix_fmt = AV_PIX_FMT_GBRP16;
     209             :             } else {
     210          53 :                 avctx->pix_fmt = AV_PIX_FMT_GBRAP16;
     211             :             }
     212             :         } else {
     213           0 :             av_log(avctx, AV_LOG_ERROR, "unsupported BITPIX = %d\n", header.bitpix);
     214           0 :             return AVERROR_INVALIDDATA;
     215             :         }
     216             :     } else {
     217         112 :         if (header.bitpix == 8) {
     218          53 :             avctx->pix_fmt = AV_PIX_FMT_GRAY8;
     219             :         } else {
     220          59 :             avctx->pix_fmt = AV_PIX_FMT_GRAY16;
     221             :         }
     222             :     }
     223             : 
     224         365 :     if ((ret = ff_set_dimensions(avctx, header.naxisn[0], header.naxisn[1])) < 0)
     225           0 :         return ret;
     226             : 
     227         365 :     if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
     228           0 :         return ret;
     229             : 
     230             :     /*
     231             :      * FITS stores images with bottom row first. Therefore we have
     232             :      * to fill the image from bottom to top.
     233             :      */
     234         365 :     if (header.rgb) {
     235         253 :         switch(header.bitpix) {
     236             : #define CASE_RGB(cas, dst, type, dref) \
     237             :     case cas: \
     238             :         for (k = 0; k < header.naxisn[2]; k++) { \
     239             :             for (i = 0; i < avctx->height; i++) { \
     240             :                 dst = (type *) (p->data[map[k]] + (avctx->height - i - 1) * p->linesize[map[k]]); \
     241             :                 for (j = 0; j < avctx->width; j++) { \
     242             :                     t32 = dref(ptr8); \
     243             :                     if (!header.blank_found || t32 != header.blank) { \
     244             :                         t = t32 * header.bscale + header.bzero; \
     245             :                     } else { \
     246             :                         t = fitsctx->blank_val; \
     247             :                     } \
     248             :                     *dst++ = (type) t; \
     249             :                     ptr8 += cas >> 3; \
     250             :                 } \
     251             :             } \
     252             :         } \
     253             :         break
     254             : 
     255         147 :             CASE_RGB(8, dst8, uint8_t, *);
     256         106 :             CASE_RGB(16, dst16, uint16_t, AV_RB16);
     257             :         }
     258             :     } else {
     259         112 :         switch (header.bitpix) {
     260             : #define CASE_GRAY(cas, dst, type, t, rd) \
     261             :     case cas: \
     262             :         for (i = 0; i < avctx->height; i++) { \
     263             :             dst = (type *) (p->data[0] + (avctx->height-i-1)* p->linesize[0]); \
     264             :             for (j = 0; j < avctx->width; j++) { \
     265             :                 t = rd; \
     266             :                 if (!header.blank_found || t != header.blank) { \
     267             :                     *dst++ = ((t - header.data_min) * ((1 << (sizeof(type) * 8)) - 1)) / (header.data_max - header.data_min); \
     268             :                 } else { \
     269             :                     *dst++ = fitsctx->blank_val; \
     270             :                 } \
     271             :                 ptr8 += abs(cas) >> 3; \
     272             :             } \
     273             :         } \
     274             :         break
     275             : 
     276           2 :             CASE_GRAY(-64, dst16, uint16_t, tdbl, av_int2double(AV_RB64(ptr8)));
     277           2 :             CASE_GRAY(-32, dst16, uint16_t, tflt, av_int2float(AV_RB32(ptr8)));
     278          53 :             CASE_GRAY(8, dst8, uint8_t, t8, ptr8[0]);
     279          53 :             CASE_GRAY(16, dst16, uint16_t, t16, AV_RB16(ptr8));
     280           2 :             CASE_GRAY(32, dst16, uint16_t, t32, AV_RB32(ptr8));
     281           0 :             CASE_GRAY(64, dst16, uint16_t, t64, AV_RB64(ptr8));
     282           0 :             default:
     283           0 :                 av_log(avctx, AV_LOG_ERROR, "invalid BITPIX, %d\n", header.bitpix);
     284           0 :                 return AVERROR_INVALIDDATA;
     285             :         }
     286             :     }
     287             : 
     288         365 :     p->key_frame = 1;
     289         365 :     p->pict_type = AV_PICTURE_TYPE_I;
     290             : 
     291         365 :     *got_frame = 1;
     292             : 
     293         365 :     return avpkt->size;
     294             : }
     295             : 
     296             : static const AVOption fits_options[] = {
     297             :     { "blank_value", "value that is used to replace BLANK pixels in data array", offsetof(FITSContext, blank_val), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 65535, AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM},
     298             :     { NULL },
     299             : };
     300             : 
     301             : static const AVClass fits_decoder_class = {
     302             :     .class_name = "FITS decoder",
     303             :     .item_name  = av_default_item_name,
     304             :     .option     = fits_options,
     305             :     .version    = LIBAVUTIL_VERSION_INT,
     306             : };
     307             : 
     308             : AVCodec ff_fits_decoder = {
     309             :     .name           = "fits",
     310             :     .type           = AVMEDIA_TYPE_VIDEO,
     311             :     .id             = AV_CODEC_ID_FITS,
     312             :     .priv_data_size = sizeof(FITSContext),
     313             :     .decode         = fits_decode_frame,
     314             :     .capabilities   = AV_CODEC_CAP_DR1,
     315             :     .long_name      = NULL_IF_CONFIG_SMALL("Flexible Image Transport System"),
     316             :     .priv_class     = &fits_decoder_class
     317             : };

Generated by: LCOV version 1.13