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 *keyword, int value, int *lines_written)
53 len = strlen(keyword);
54 memset(header, ' ', sizeof(header));
55 memcpy(header, keyword, len);
60 ret = snprintf(header + 10, 70, "%d", value);
61 memset(&header[ret + 10], ' ', sizeof(header) - (ret + 10));
63 avio_write(s->pb, header, sizeof(header));
68 static int write_image_header(AVFormatContext *s)
70 AVStream *st = s->streams[0];
71 AVCodecParameters *encctx = st->codecpar;
72 FITSContext *fitsctx = s->priv_data;
74 int bitpix, naxis, naxis3 = 1, bzero = 0, rgb = 0, lines_written = 0, lines_left;
76 switch (encctx->format) {
77 case AV_PIX_FMT_GRAY8:
81 case AV_PIX_FMT_GRAY16BE:
87 case AV_PIX_FMT_GBRAP:
91 if (encctx->format == AV_PIX_FMT_GBRP) {
97 case AV_PIX_FMT_GBRP16BE:
98 case AV_PIX_FMT_GBRAP16BE:
102 if (encctx->format == AV_PIX_FMT_GBRP16BE) {
110 return AVERROR(EINVAL);
113 if (fitsctx->first_image) {
114 memcpy(buffer, "SIMPLE = ", 10);
115 memset(buffer + 10, ' ', 70);
117 avio_write(s->pb, buffer, sizeof(buffer));
119 memcpy(buffer, "XTENSION= 'IMAGE '", 20);
120 memset(buffer + 20, ' ', 60);
121 avio_write(s->pb, buffer, sizeof(buffer));
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
131 write_keyword_value(s, "NAXIS3", naxis3, &lines_written); // third dimension to store RGBA planes
133 if (!fitsctx->first_image) {
134 write_keyword_value(s, "PCOUNT", 0, &lines_written);
135 write_keyword_value(s, "GCOUNT", 1, &lines_written);
137 fitsctx->first_image = 0;
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.
146 write_keyword_value(s, "BZERO", bzero, &lines_written);
149 memcpy(buffer, "CTYPE3 = 'RGB '", 20);
150 memset(buffer + 20, ' ', 60);
151 avio_write(s->pb, buffer, sizeof(buffer));
155 memcpy(buffer, "END", 3);
156 memset(buffer + 3, ' ', 77);
157 avio_write(s->pb, buffer, sizeof(buffer));
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));
169 static int fits_write_packet(AVFormatContext *s, AVPacket *pkt)
171 int ret = write_image_header(s);
174 avio_write(s->pb, pkt->data, pkt->size);
178 AVOutputFormat ff_fits_muxer = {
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,
185 .write_header = fits_write_header,
186 .write_packet = fits_write_packet,