FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/fitsenc.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 98 101 97.0%
Functions: 4 4 100.0%
Branches: 21 23 91.3%

Line Branch Exec Source
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
31 typedef struct FITSContext {
32 int first_image;
33 } FITSContext;
34
35 11 static int fits_write_header(AVFormatContext *s)
36 {
37 11 FITSContext *fitsctx = s->priv_data;
38 11 fitsctx->first_image = 1;
39 11 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 */
50 1435 static 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 1435 len = strlen(keyword);
57 1435 memset(header, ' ', sizeof(header));
58 1435 memcpy(header, keyword, len);
59
60 1435 header[8] = '=';
61 1435 header[9] = ' ';
62
63
2/2
✓ Branch 0 taken 1117 times.
✓ Branch 1 taken 318 times.
1435 if (!strcmp(fmt, "%d")) {
64 1117 ret = snprintf(header + 10, 70, fmt, *(int *)value);
65 } else {
66 318 ret = snprintf(header + 10, 70, fmt, *(float *)value);
67 }
68
69 1435 memset(&header[ret + 10], ' ', sizeof(header) - (ret + 10));
70
71 1435 avio_write(s->pb, header, sizeof(header));
72 1435 *lines_written += 1;
73 1435 return 0;
74 }
75
76 159 static int write_image_header(AVFormatContext *s)
77 {
78 159 AVStream *st = s->streams[0];
79 159 AVCodecParameters *encctx = st->codecpar;
80 159 FITSContext *fitsctx = s->priv_data;
81 uint8_t buffer[80];
82 159 int bitpix, naxis, naxis3 = 1, bzero = 0, rgb = 0, lines_written = 0, lines_left;
83 159 int pcount = 0, gcount = 1;
84 float datamax, datamin;
85
86
4/5
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 25 times.
✓ Branch 2 taken 56 times.
✓ Branch 3 taken 52 times.
✗ Branch 4 not taken.
159 switch (encctx->format) {
87 26 case AV_PIX_FMT_GRAY8:
88 26 bitpix = 8;
89 26 naxis = 2;
90 26 datamin = 0;
91 26 datamax = 255;
92 26 break;
93 25 case AV_PIX_FMT_GRAY16BE:
94 25 bitpix = 16;
95 25 naxis = 2;
96 25 bzero = 32768;
97 25 datamin = 0;
98 25 datamax = 65535;
99 25 break;
100 56 case AV_PIX_FMT_GBRP:
101 case AV_PIX_FMT_GBRAP:
102 56 bitpix = 8;
103 56 naxis = 3;
104 56 rgb = 1;
105
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 30 times.
56 if (encctx->format == AV_PIX_FMT_GBRP) {
106 26 naxis3 = 3;
107 } else {
108 30 naxis3 = 4;
109 }
110 56 datamin = 0;
111 56 datamax = 255;
112 56 break;
113 52 case AV_PIX_FMT_GBRP16BE:
114 case AV_PIX_FMT_GBRAP16BE:
115 52 bitpix = 16;
116 52 naxis = 3;
117 52 rgb = 1;
118
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 26 times.
52 if (encctx->format == AV_PIX_FMT_GBRP16BE) {
119 26 naxis3 = 3;
120 } else {
121 26 naxis3 = 4;
122 }
123 52 bzero = 32768;
124 52 datamin = 0;
125 52 datamax = 65535;
126 52 break;
127 default:
128 return AVERROR(EINVAL);
129 }
130
131
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 148 times.
159 if (fitsctx->first_image) {
132 11 memcpy(buffer, "SIMPLE = ", 10);
133 11 memset(buffer + 10, ' ', 70);
134 11 buffer[29] = 'T';
135 11 avio_write(s->pb, buffer, sizeof(buffer));
136 } else {
137 148 memcpy(buffer, "XTENSION= 'IMAGE '", 20);
138 148 memset(buffer + 20, ' ', 60);
139 148 avio_write(s->pb, buffer, sizeof(buffer));
140 }
141 159 lines_written++;
142
143 159 write_keyword_value(s, "%d", "BITPIX", &bitpix, &lines_written); // no of bits per pixel
144 159 write_keyword_value(s, "%d", "NAXIS", &naxis, &lines_written); // no of dimensions of image
145 159 write_keyword_value(s, "%d", "NAXIS1", &encctx->width, &lines_written); // first dimension i.e. width
146 159 write_keyword_value(s, "%d", "NAXIS2", &encctx->height, &lines_written); // second dimension i.e. height
147
148
2/2
✓ Branch 0 taken 108 times.
✓ Branch 1 taken 51 times.
159 if (rgb)
149 108 write_keyword_value(s, "%d", "NAXIS3", &naxis3, &lines_written); // third dimension to store RGBA planes
150
151
2/2
✓ Branch 0 taken 148 times.
✓ Branch 1 taken 11 times.
159 if (!fitsctx->first_image) {
152 148 write_keyword_value(s, "%d", "PCOUNT", &pcount, &lines_written);
153 148 write_keyword_value(s, "%d", "GCOUNT", &gcount, &lines_written);
154 } else {
155 11 fitsctx->first_image = 0;
156 }
157
158 159 write_keyword_value(s, "%g", "DATAMIN", &datamin, &lines_written);
159 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
2/2
✓ Branch 0 taken 77 times.
✓ Branch 1 taken 82 times.
159 if (bitpix == 16)
167 77 write_keyword_value(s, "%d", "BZERO", &bzero, &lines_written);
168
169
2/2
✓ Branch 0 taken 108 times.
✓ Branch 1 taken 51 times.
159 if (rgb) {
170 108 memcpy(buffer, "CTYPE3 = 'RGB '", 20);
171 108 memset(buffer + 20, ' ', 60);
172 108 avio_write(s->pb, buffer, sizeof(buffer));
173 108 lines_written++;
174 }
175
176 159 memcpy(buffer, "END", 3);
177 159 memset(buffer + 3, ' ', 77);
178 159 avio_write(s->pb, buffer, sizeof(buffer));
179 159 lines_written++;
180
181 159 lines_left = ((lines_written + 35) / 36) * 36 - lines_written;
182 159 ffio_fill(s->pb, ' ', sizeof(buffer) * lines_left);
183 159 return 0;
184 }
185
186 159 static int fits_write_packet(AVFormatContext *s, AVPacket *pkt)
187 {
188 159 int ret = write_image_header(s);
189
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 159 times.
159 if (ret < 0)
190 return ret;
191 159 avio_write(s->pb, pkt->data, pkt->size);
192 159 return 0;
193 }
194
195 const FFOutputFormat ff_fits_muxer = {
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 |
203 FF_OFMT_FLAG_ONLY_DEFAULT_CODECS,
204 .priv_data_size = sizeof(FITSContext),
205 .write_header = fits_write_header,
206 .write_packet = fits_write_packet,
207 .p.flags = AVFMT_NOTIMESTAMPS,
208 };
209