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 "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 *keyword, int value, int *lines_written)
49 {
50  int len, ret;
51  uint8_t header[80];
52 
53  len = strlen(keyword);
54  memset(header, ' ', sizeof(header));
55  memcpy(header, keyword, len);
56 
57  header[8] = '=';
58  header[9] = ' ';
59 
60  ret = snprintf(header + 10, 70, "%d", value);
61  memset(&header[ret + 10], ' ', sizeof(header) - (ret + 10));
62 
63  avio_write(s->pb, header, sizeof(header));
64  *lines_written += 1;
65  return 0;
66 }
67 
69 {
70  AVStream *st = s->streams[0];
71  AVCodecParameters *encctx = st->codecpar;
72  FITSContext *fitsctx = s->priv_data;
73  uint8_t buffer[80];
74  int bitpix, naxis, naxis3 = 1, bzero = 0, rgb = 0, lines_written = 0, lines_left;
75 
76  switch (encctx->format) {
77  case AV_PIX_FMT_GRAY8:
78  bitpix = 8;
79  naxis = 2;
80  break;
82  bitpix = 16;
83  naxis = 2;
84  bzero = 32768;
85  break;
86  case AV_PIX_FMT_GBRP:
87  case AV_PIX_FMT_GBRAP:
88  bitpix = 8;
89  naxis = 3;
90  rgb = 1;
91  if (encctx->format == AV_PIX_FMT_GBRP) {
92  naxis3 = 3;
93  } else {
94  naxis3 = 4;
95  }
96  break;
99  bitpix = 16;
100  naxis = 3;
101  rgb = 1;
102  if (encctx->format == AV_PIX_FMT_GBRP16BE) {
103  naxis3 = 3;
104  } else {
105  naxis3 = 4;
106  }
107  bzero = 32768;
108  break;
109  default:
110  return AVERROR(EINVAL);
111  }
112 
113  if (fitsctx->first_image) {
114  memcpy(buffer, "SIMPLE = ", 10);
115  memset(buffer + 10, ' ', 70);
116  buffer[29] = 'T';
117  avio_write(s->pb, buffer, sizeof(buffer));
118  } else {
119  memcpy(buffer, "XTENSION= 'IMAGE '", 20);
120  memset(buffer + 20, ' ', 60);
121  avio_write(s->pb, buffer, sizeof(buffer));
122  }
123  lines_written++;
124 
125  write_keyword_value(s, "BITPIX", bitpix, &lines_written); // no of bits per pixel
126  write_keyword_value(s, "NAXIS", naxis, &lines_written); // no of dimensions of image
127  write_keyword_value(s, "NAXIS1", encctx->width, &lines_written); // first dimension i.e. width
128  write_keyword_value(s, "NAXIS2", encctx->height, &lines_written); // second dimension i.e. height
129 
130  if (rgb)
131  write_keyword_value(s, "NAXIS3", naxis3, &lines_written); // third dimension to store RGBA planes
132 
133  if (!fitsctx->first_image) {
134  write_keyword_value(s, "PCOUNT", 0, &lines_written);
135  write_keyword_value(s, "GCOUNT", 1, &lines_written);
136  } else {
137  fitsctx->first_image = 0;
138  }
139 
140  /*
141  * Since FITS does not support unsigned 16 bit integers,
142  * BZERO = 32768 is used to store unsigned 16 bit integers as
143  * signed integers so that it can be read properly.
144  */
145  if (bitpix == 16)
146  write_keyword_value(s, "BZERO", bzero, &lines_written);
147 
148  if (rgb) {
149  memcpy(buffer, "CTYPE3 = 'RGB '", 20);
150  memset(buffer + 20, ' ', 60);
151  avio_write(s->pb, buffer, sizeof(buffer));
152  lines_written++;
153  }
154 
155  memcpy(buffer, "END", 3);
156  memset(buffer + 3, ' ', 77);
157  avio_write(s->pb, buffer, sizeof(buffer));
158  lines_written++;
159 
160  lines_left = ((lines_written + 35) / 36) * 36 - lines_written;
161  memset(buffer, ' ', 80);
162  while (lines_left > 0) {
163  avio_write(s->pb, buffer, sizeof(buffer));
164  lines_left--;
165  }
166  return 0;
167 }
168 
170 {
171  int ret = write_image_header(s);
172  if (ret < 0)
173  return ret;
174  avio_write(s->pb, pkt->data, pkt->size);
175  return 0;
176 }
177 
179  .name = "fits",
180  .long_name = NULL_IF_CONFIG_SMALL("Flexible Image Transport System"),
181  .extensions = "fits",
182  .priv_data_size = sizeof(FITSContext),
183  .audio_codec = AV_CODEC_ID_NONE,
184  .video_codec = AV_CODEC_ID_FITS,
187 };
AVOutputFormat::name
const char * name
Definition: avformat.h:496
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:68
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: avcodec.h:3949
AV_PIX_FMT_GBRP16BE
@ AV_PIX_FMT_GBRP16BE
planar GBR 4:4:4 48bpp, big-endian
Definition: pixfmt.h:174
FITSContext
Definition: fitsdec.c:42
AVPacket::data
uint8_t * data
Definition: avcodec.h:1477
AV_PIX_FMT_GRAY16BE
@ AV_PIX_FMT_GRAY16BE
Y , 16bpp, big-endian.
Definition: pixfmt.h:97
AV_PIX_FMT_GBRAP
@ AV_PIX_FMT_GBRAP
planar GBRA 4:4:4:4 32bpp
Definition: pixfmt.h:215
fits_write_packet
static int fits_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: fitsenc.c:169
write_keyword_value
static int write_keyword_value(AVFormatContext *s, const char *keyword, int value, int *lines_written)
Write one header line comprising of keyword and value(int)
Definition: fitsenc.c:48
AV_PIX_FMT_GBRAP16BE
@ AV_PIX_FMT_GBRAP16BE
planar GBRA 4:4:4:4 64bpp, big-endian
Definition: pixfmt.h:216
s
#define s(width, name)
Definition: cbs_vp9.c:257
AVCodecParameters::width
int width
Video only.
Definition: avcodec.h:4023
AVFormatContext
Format I/O context.
Definition: avformat.h:1342
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1017
AV_CODEC_ID_FITS
@ AV_CODEC_ID_FITS
Definition: avcodec.h:449
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
AVPacket::size
int size
Definition: avcodec.h:1478
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:188
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:218
write_packet
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
Definition: ffmpeg.c:690
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: avcodec.h:216
AVOutputFormat
Definition: avformat.h:495
ff_fits_muxer
AVOutputFormat ff_fits_muxer
Definition: fitsenc.c:178
AVCodecParameters::height
int height
Definition: avcodec.h:4024
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
uint8_t
uint8_t
Definition: audio_convert.c:194
len
int len
Definition: vorbis_enc_data.h:452
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:870
FITSContext::first_image
int first_image
Definition: fitsdec.c:39
pkt
static AVPacket pkt
Definition: demuxing_decoding.c:54
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:168
fits_write_header
static int fits_write_header(AVFormatContext *s)
Definition: fitsenc.c:33
AVCodecParameters::format
int format
Definition: avcodec.h:3981
AVPacket
This structure stores compressed data.
Definition: avcodec.h:1454
write_header
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:337
snprintf
#define snprintf
Definition: snprintf.h:34