FFmpeg  4.4.4
fitsenc.c
Go to the documentation of this file.
1 /*
2  * FITS muxer
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 muxer.
25  */
26 
27 #include "internal.h"
28 
29 typedef struct FITSContext {
30  int first_image;
31 } FITSContext;
32 
34 {
35  FITSContext *fitsctx = s->priv_data;
36  fitsctx->first_image = 1;
37  return 0;
38 }
39 
40 /**
41  * Write one header line comprising of keyword and value(int)
42  * @param s AVFormat Context
43  * @param keyword pointer to the char array in which keyword is stored
44  * @param value the value corresponding to the keyword
45  * @param lines_written to keep track of lines written so far
46  * @return 0
47  */
48 static int write_keyword_value(AVFormatContext *s, const char *fmt,
49  const char *keyword, void *value, int *lines_written)
50 {
51  int len, ret;
52  uint8_t header[80];
53 
54  len = strlen(keyword);
55  memset(header, ' ', sizeof(header));
56  memcpy(header, keyword, len);
57 
58  header[8] = '=';
59  header[9] = ' ';
60 
61  if (!strcmp(fmt, "%d")) {
62  ret = snprintf(header + 10, 70, fmt, *(int *)value);
63  } else {
64  ret = snprintf(header + 10, 70, fmt, *(float *)value);
65  }
66 
67  memset(&header[ret + 10], ' ', sizeof(header) - (ret + 10));
68 
69  avio_write(s->pb, header, sizeof(header));
70  *lines_written += 1;
71  return 0;
72 }
73 
75 {
76  AVStream *st = s->streams[0];
77  AVCodecParameters *encctx = st->codecpar;
78  FITSContext *fitsctx = s->priv_data;
79  uint8_t buffer[80];
80  int bitpix, naxis, naxis3 = 1, bzero = 0, rgb = 0, lines_written = 0, lines_left;
81  int pcount = 0, gcount = 1;
82  float datamax, datamin;
83 
84  switch (encctx->format) {
85  case AV_PIX_FMT_GRAY8:
86  bitpix = 8;
87  naxis = 2;
88  datamin = 0;
89  datamax = 255;
90  break;
92  bitpix = 16;
93  naxis = 2;
94  bzero = 32768;
95  datamin = 0;
96  datamax = 65535;
97  break;
98  case AV_PIX_FMT_GBRP:
99  case AV_PIX_FMT_GBRAP:
100  bitpix = 8;
101  naxis = 3;
102  rgb = 1;
103  if (encctx->format == AV_PIX_FMT_GBRP) {
104  naxis3 = 3;
105  } else {
106  naxis3 = 4;
107  }
108  datamin = 0;
109  datamax = 255;
110  break;
111  case AV_PIX_FMT_GBRP16BE:
113  bitpix = 16;
114  naxis = 3;
115  rgb = 1;
116  if (encctx->format == AV_PIX_FMT_GBRP16BE) {
117  naxis3 = 3;
118  } else {
119  naxis3 = 4;
120  }
121  bzero = 32768;
122  datamin = 0;
123  datamax = 65535;
124  break;
125  default:
126  return AVERROR(EINVAL);
127  }
128 
129  if (fitsctx->first_image) {
130  memcpy(buffer, "SIMPLE = ", 10);
131  memset(buffer + 10, ' ', 70);
132  buffer[29] = 'T';
133  avio_write(s->pb, buffer, sizeof(buffer));
134  } else {
135  memcpy(buffer, "XTENSION= 'IMAGE '", 20);
136  memset(buffer + 20, ' ', 60);
137  avio_write(s->pb, buffer, sizeof(buffer));
138  }
139  lines_written++;
140 
141  write_keyword_value(s, "%d", "BITPIX", &bitpix, &lines_written); // no of bits per pixel
142  write_keyword_value(s, "%d", "NAXIS", &naxis, &lines_written); // no of dimensions of image
143  write_keyword_value(s, "%d", "NAXIS1", &encctx->width, &lines_written); // first dimension i.e. width
144  write_keyword_value(s, "%d", "NAXIS2", &encctx->height, &lines_written); // second dimension i.e. height
145 
146  if (rgb)
147  write_keyword_value(s, "%d", "NAXIS3", &naxis3, &lines_written); // third dimension to store RGBA planes
148 
149  if (!fitsctx->first_image) {
150  write_keyword_value(s, "%d", "PCOUNT", &pcount, &lines_written);
151  write_keyword_value(s, "%d", "GCOUNT", &gcount, &lines_written);
152  } else {
153  fitsctx->first_image = 0;
154  }
155 
156  write_keyword_value(s, "%g", "DATAMIN", &datamin, &lines_written);
157  write_keyword_value(s, "%g", "DATAMAX", &datamax, &lines_written);
158 
159  /*
160  * Since FITS does not support unsigned 16 bit integers,
161  * BZERO = 32768 is used to store unsigned 16 bit integers as
162  * signed integers so that it can be read properly.
163  */
164  if (bitpix == 16)
165  write_keyword_value(s, "%d", "BZERO", &bzero, &lines_written);
166 
167  if (rgb) {
168  memcpy(buffer, "CTYPE3 = 'RGB '", 20);
169  memset(buffer + 20, ' ', 60);
170  avio_write(s->pb, buffer, sizeof(buffer));
171  lines_written++;
172  }
173 
174  memcpy(buffer, "END", 3);
175  memset(buffer + 3, ' ', 77);
176  avio_write(s->pb, buffer, sizeof(buffer));
177  lines_written++;
178 
179  lines_left = ((lines_written + 35) / 36) * 36 - lines_written;
180  memset(buffer, ' ', 80);
181  while (lines_left > 0) {
182  avio_write(s->pb, buffer, sizeof(buffer));
183  lines_left--;
184  }
185  return 0;
186 }
187 
189 {
190  int ret = write_image_header(s);
191  if (ret < 0)
192  return ret;
193  avio_write(s->pb, pkt->data, pkt->size);
194  return 0;
195 }
196 
198  .name = "fits",
199  .long_name = NULL_IF_CONFIG_SMALL("Flexible Image Transport System"),
200  .extensions = "fits",
201  .priv_data_size = sizeof(FITSContext),
202  .audio_codec = AV_CODEC_ID_NONE,
203  .video_codec = AV_CODEC_ID_FITS,
206 };
uint8_t
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:225
#define s(width, name)
Definition: cbs_vp9.c:257
double value
Definition: eval.c:98
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
Definition: ffmpeg.c:729
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:346
@ AV_CODEC_ID_NONE
Definition: codec_id.h:47
@ AV_CODEC_ID_FITS
Definition: codec_id.h:285
#define AVERROR(e)
Definition: error.h:43
static int fits_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: fitsenc.c:188
AVOutputFormat ff_fits_muxer
Definition: fitsenc.c:197
static int write_keyword_value(AVFormatContext *s, const char *fmt, const char *keyword, void *value, int *lines_written)
Write one header line comprising of keyword and value(int)
Definition: fitsenc.c:48
static int fits_write_header(AVFormatContext *s)
Definition: fitsenc.c:33
static int write_image_header(AVFormatContext *s)
Definition: fitsenc.c:74
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
@ AV_PIX_FMT_GRAY16BE
Y , 16bpp, big-endian.
Definition: pixfmt.h:97
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
@ AV_PIX_FMT_GBRAP16BE
planar GBRA 4:4:4:4 64bpp, big-endian
Definition: pixfmt.h:216
@ AV_PIX_FMT_GBRAP
planar GBRA 4:4:4:4 32bpp
Definition: pixfmt.h:215
@ AV_PIX_FMT_GBRP16BE
planar GBR 4:4:4 48bpp, big-endian
Definition: pixfmt.h:174
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:168
static const uint8_t header[24]
Definition: sdr2.c:67
static char buffer[20]
Definition: seek.c:32
#define snprintf
Definition: snprintf.h:34
This struct describes the properties of an encoded stream.
Definition: codec_par.h:52
int width
Video only.
Definition: codec_par.h:126
Format I/O context.
Definition: avformat.h:1232
const char * name
Definition: avformat.h:491
This structure stores compressed data.
Definition: packet.h:346
int size
Definition: packet.h:370
uint8_t * data
Definition: packet.h:369
Stream structure.
Definition: avformat.h:873
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1038
int first_image
Definition: fitsdec.c:39
Definition: rpzaenc.c:58
AVPacket * pkt
Definition: movenc.c:59
int len