3 * Copyright (c) 2017 Paras Chadha
5 * This file is part of FFmpeg.
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.
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.
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
29 typedef struct FITSContext {
33 static int fits_write_header(AVFormatContext *s)
35 FITSContext *fitsctx = s->priv_data;
36 fitsctx->first_image = 1;
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
48 static int write_keyword_value(AVFormatContext *s, const char *fmt,
49 const char *keyword, void *value, int *lines_written)
54 len = strlen(keyword);
55 memset(header, ' ', sizeof(header));
56 memcpy(header, keyword, len);
61 if (!strcmp(fmt, "%d")) {
62 ret = snprintf(header + 10, 70, fmt, *(int *)value);
64 ret = snprintf(header + 10, 70, fmt, *(float *)value);
67 memset(&header[ret + 10], ' ', sizeof(header) - (ret + 10));
69 avio_write(s->pb, header, sizeof(header));
74 static int write_image_header(AVFormatContext *s)
76 AVStream *st = s->streams[0];
77 AVCodecParameters *encctx = st->codecpar;
78 FITSContext *fitsctx = s->priv_data;
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;
84 switch (encctx->format) {
85 case AV_PIX_FMT_GRAY8:
91 case AV_PIX_FMT_GRAY16BE:
99 case AV_PIX_FMT_GBRAP:
103 if (encctx->format == AV_PIX_FMT_GBRP) {
111 case AV_PIX_FMT_GBRP16BE:
112 case AV_PIX_FMT_GBRAP16BE:
116 if (encctx->format == AV_PIX_FMT_GBRP16BE) {
126 return AVERROR(EINVAL);
129 if (fitsctx->first_image) {
130 memcpy(buffer, "SIMPLE = ", 10);
131 memset(buffer + 10, ' ', 70);
133 avio_write(s->pb, buffer, sizeof(buffer));
135 memcpy(buffer, "XTENSION= 'IMAGE '", 20);
136 memset(buffer + 20, ' ', 60);
137 avio_write(s->pb, buffer, sizeof(buffer));
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
147 write_keyword_value(s, "%d", "NAXIS3", &naxis3, &lines_written); // third dimension to store RGBA planes
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);
153 fitsctx->first_image = 0;
156 write_keyword_value(s, "%g", "DATAMIN", &datamin, &lines_written);
157 write_keyword_value(s, "%g", "DATAMAX", &datamax, &lines_written);
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.
165 write_keyword_value(s, "%d", "BZERO", &bzero, &lines_written);
168 memcpy(buffer, "CTYPE3 = 'RGB '", 20);
169 memset(buffer + 20, ' ', 60);
170 avio_write(s->pb, buffer, sizeof(buffer));
174 memcpy(buffer, "END", 3);
175 memset(buffer + 3, ' ', 77);
176 avio_write(s->pb, buffer, sizeof(buffer));
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));
188 static int fits_write_packet(AVFormatContext *s, AVPacket *pkt)
190 int ret = write_image_header(s);
193 avio_write(s->pb, pkt->data, pkt->size);
197 const AVOutputFormat ff_fits_muxer = {
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,
204 .write_header = fits_write_header,
205 .write_packet = fits_write_packet,