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