FFmpeg
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 "avio_internal.h"
28 #include "internal.h"
29 
30 typedef struct FITSContext {
31  int first_image;
32 } FITSContext;
33 
35 {
36  FITSContext *fitsctx = s->priv_data;
37  fitsctx->first_image = 1;
38  return 0;
39 }
40 
41 /**
42  * Write one header line comprising of keyword and value(int)
43  * @param s AVFormat Context
44  * @param keyword pointer to the char array in which keyword is stored
45  * @param value the value corresponding to the keyword
46  * @param lines_written to keep track of lines written so far
47  * @return 0
48  */
49 static int write_keyword_value(AVFormatContext *s, const char *fmt,
50  const char *keyword, void *value, int *lines_written)
51 {
52  int len, ret;
53  uint8_t header[80];
54 
55  len = strlen(keyword);
56  memset(header, ' ', sizeof(header));
57  memcpy(header, keyword, len);
58 
59  header[8] = '=';
60  header[9] = ' ';
61 
62  if (!strcmp(fmt, "%d")) {
63  ret = snprintf(header + 10, 70, fmt, *(int *)value);
64  } else {
65  ret = snprintf(header + 10, 70, fmt, *(float *)value);
66  }
67 
68  memset(&header[ret + 10], ' ', sizeof(header) - (ret + 10));
69 
70  avio_write(s->pb, header, sizeof(header));
71  *lines_written += 1;
72  return 0;
73 }
74 
76 {
77  AVStream *st = s->streams[0];
78  AVCodecParameters *encctx = st->codecpar;
79  FITSContext *fitsctx = s->priv_data;
80  uint8_t buffer[80];
81  int bitpix, naxis, naxis3 = 1, bzero = 0, rgb = 0, lines_written = 0, lines_left;
82  int pcount = 0, gcount = 1;
83  float datamax, datamin;
84 
85  switch (encctx->format) {
86  case AV_PIX_FMT_GRAY8:
87  bitpix = 8;
88  naxis = 2;
89  datamin = 0;
90  datamax = 255;
91  break;
93  bitpix = 16;
94  naxis = 2;
95  bzero = 32768;
96  datamin = 0;
97  datamax = 65535;
98  break;
99  case AV_PIX_FMT_GBRP:
100  case AV_PIX_FMT_GBRAP:
101  bitpix = 8;
102  naxis = 3;
103  rgb = 1;
104  if (encctx->format == AV_PIX_FMT_GBRP) {
105  naxis3 = 3;
106  } else {
107  naxis3 = 4;
108  }
109  datamin = 0;
110  datamax = 255;
111  break;
112  case AV_PIX_FMT_GBRP16BE:
114  bitpix = 16;
115  naxis = 3;
116  rgb = 1;
117  if (encctx->format == AV_PIX_FMT_GBRP16BE) {
118  naxis3 = 3;
119  } else {
120  naxis3 = 4;
121  }
122  bzero = 32768;
123  datamin = 0;
124  datamax = 65535;
125  break;
126  default:
127  return AVERROR(EINVAL);
128  }
129 
130  if (fitsctx->first_image) {
131  memcpy(buffer, "SIMPLE = ", 10);
132  memset(buffer + 10, ' ', 70);
133  buffer[29] = 'T';
134  avio_write(s->pb, buffer, sizeof(buffer));
135  } else {
136  memcpy(buffer, "XTENSION= 'IMAGE '", 20);
137  memset(buffer + 20, ' ', 60);
138  avio_write(s->pb, buffer, sizeof(buffer));
139  }
140  lines_written++;
141 
142  write_keyword_value(s, "%d", "BITPIX", &bitpix, &lines_written); // no of bits per pixel
143  write_keyword_value(s, "%d", "NAXIS", &naxis, &lines_written); // no of dimensions of image
144  write_keyword_value(s, "%d", "NAXIS1", &encctx->width, &lines_written); // first dimension i.e. width
145  write_keyword_value(s, "%d", "NAXIS2", &encctx->height, &lines_written); // second dimension i.e. height
146 
147  if (rgb)
148  write_keyword_value(s, "%d", "NAXIS3", &naxis3, &lines_written); // third dimension to store RGBA planes
149 
150  if (!fitsctx->first_image) {
151  write_keyword_value(s, "%d", "PCOUNT", &pcount, &lines_written);
152  write_keyword_value(s, "%d", "GCOUNT", &gcount, &lines_written);
153  } else {
154  fitsctx->first_image = 0;
155  }
156 
157  write_keyword_value(s, "%g", "DATAMIN", &datamin, &lines_written);
158  write_keyword_value(s, "%g", "DATAMAX", &datamax, &lines_written);
159 
160  /*
161  * Since FITS does not support unsigned 16 bit integers,
162  * BZERO = 32768 is used to store unsigned 16 bit integers as
163  * signed integers so that it can be read properly.
164  */
165  if (bitpix == 16)
166  write_keyword_value(s, "%d", "BZERO", &bzero, &lines_written);
167 
168  if (rgb) {
169  memcpy(buffer, "CTYPE3 = 'RGB '", 20);
170  memset(buffer + 20, ' ', 60);
171  avio_write(s->pb, buffer, sizeof(buffer));
172  lines_written++;
173  }
174 
175  memcpy(buffer, "END", 3);
176  memset(buffer + 3, ' ', 77);
177  avio_write(s->pb, buffer, sizeof(buffer));
178  lines_written++;
179 
180  lines_left = ((lines_written + 35) / 36) * 36 - lines_written;
181  ffio_fill(s->pb, ' ', sizeof(buffer) * lines_left);
182  return 0;
183 }
184 
186 {
187  int ret = write_image_header(s);
188  if (ret < 0)
189  return ret;
190  avio_write(s->pb, pkt->data, pkt->size);
191  return 0;
192 }
193 
195  .name = "fits",
196  .long_name = NULL_IF_CONFIG_SMALL("Flexible Image Transport System"),
197  .extensions = "fits",
198  .priv_data_size = sizeof(FITSContext),
199  .audio_codec = AV_CODEC_ID_NONE,
200  .video_codec = AV_CODEC_ID_FITS,
203 };
AVOutputFormat::name
const char * name
Definition: avformat.h:510
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
write_image_header
static int write_image_header(AVFormatContext *s)
Definition: fitsenc.c:75
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:53
AV_PIX_FMT_GBRP16BE
@ AV_PIX_FMT_GBRP16BE
planar GBR 4:4:4 48bpp, big-endian
Definition: pixfmt.h:164
FITSContext
Definition: fitsdec.c:43
AVPacket::data
uint8_t * data
Definition: packet.h:374
AV_PIX_FMT_GRAY16BE
@ AV_PIX_FMT_GRAY16BE
Y , 16bpp, big-endian.
Definition: pixfmt.h:97
rgb
Definition: rpzaenc.c:59
write_keyword_value
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:49
AV_PIX_FMT_GBRAP
@ AV_PIX_FMT_GBRAP
planar GBRA 4:4:4:4 32bpp
Definition: pixfmt.h:205
fits_write_packet
static int fits_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: fitsenc.c:185
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_PIX_FMT_GBRAP16BE
@ AV_PIX_FMT_GBRAP16BE
planar GBRA 4:4:4:4 64bpp, big-endian
Definition: pixfmt.h:206
s
#define s(width, name)
Definition: cbs_vp9.c:256
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:127
AVFormatContext
Format I/O context.
Definition: avformat.h:1213
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1108
AV_CODEC_ID_FITS
@ AV_CODEC_ID_FITS
Definition: codec_id.h:285
ffio_fill
void ffio_fill(AVIOContext *s, int b, int64_t count)
Definition: aviobuf.c:218
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
AVPacket::size
int size
Definition: packet.h:375
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:116
header
static const uint8_t header[24]
Definition: sdr2.c:67
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:232
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:48
AVOutputFormat
Definition: avformat.h:509
avio_internal.h
AVCodecParameters::height
int height
Definition: codec_par.h:128
value
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
Definition: writing_filters.txt:86
len
int len
Definition: vorbis_enc_data.h:426
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:948
FITSContext::first_image
int first_image
Definition: fitsdec.c:39
buffer
the frame and frame reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFrame structures Several references can point to the same frame buffer
Definition: filter_design.txt:49
AV_PIX_FMT_GBRP
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:158
fits_write_header
static int fits_write_header(AVFormatContext *s)
Definition: fitsenc.c:34
AVCodecParameters::format
int format
Definition: codec_par.h:85
AVPacket
This structure stores compressed data.
Definition: packet.h:351
ff_fits_muxer
const AVOutputFormat ff_fits_muxer
Definition: fitsenc.c:194
write_packet
static int write_packet(AVFormatContext *s1, AVPacket *pkt)
Definition: v4l2enc.c:92
write_header
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:347
snprintf
#define snprintf
Definition: snprintf.h:34