Directory: | ../../../ffmpeg/ |
---|---|
File: | src/libavcodec/fitsenc.c |
Date: | 2022-07-05 19:52:29 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 42 | 46 | 91.3% |
Branches: | 18 | 20 | 90.0% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * FITS image encoder | ||
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 image encoder | ||
25 | * | ||
26 | * Specification: https://fits.gsfc.nasa.gov/fits_standard.html Version 3.0 | ||
27 | * | ||
28 | * RGBA images are encoded as planes in RGBA order. So, NAXIS3 is 3 or 4 for them. | ||
29 | * Also CTYPE3 = 'RGB ' is added to the header to distinguish them from 3d images. | ||
30 | */ | ||
31 | |||
32 | #include "libavutil/intreadwrite.h" | ||
33 | #include "avcodec.h" | ||
34 | #include "bytestream.h" | ||
35 | #include "codec_internal.h" | ||
36 | #include "encode.h" | ||
37 | |||
38 | 189 | static int fits_encode_frame(AVCodecContext *avctx, AVPacket *pkt, | |
39 | const AVFrame *pict, int *got_packet) | ||
40 | { | ||
41 | 189 | AVFrame * const p = (AVFrame *)pict; | |
42 | uint8_t *bytestream, *ptr; | ||
43 | 189 | const uint16_t flip = (1 << 15); | |
44 | 189 | uint64_t data_size = 0, padded_data_size = 0; | |
45 | 189 | int ret, bitpix, naxis3 = 1, i, j, k, bytes_left; | |
46 | 189 | int map[] = {2, 0, 1, 3}; // mapping from GBRA -> RGBA as RGBA is to be stored in FITS file.. | |
47 | |||
48 |
3/4✓ Branch 0 taken 61 times.
✓ Branch 1 taken 66 times.
✓ Branch 2 taken 62 times.
✗ Branch 3 not taken.
|
189 | switch (avctx->pix_fmt) { |
49 | 61 | case AV_PIX_FMT_GRAY8: | |
50 | case AV_PIX_FMT_GRAY16BE: | ||
51 | 61 | map[0] = 0; // grayscale images should be directly mapped | |
52 |
2/2✓ Branch 0 taken 31 times.
✓ Branch 1 taken 30 times.
|
61 | if (avctx->pix_fmt == AV_PIX_FMT_GRAY8) { |
53 | 31 | bitpix = 8; | |
54 | } else { | ||
55 | 30 | bitpix = 16; | |
56 | } | ||
57 | 61 | break; | |
58 | 66 | case AV_PIX_FMT_GBRP: | |
59 | case AV_PIX_FMT_GBRAP: | ||
60 | 66 | bitpix = 8; | |
61 |
2/2✓ Branch 0 taken 31 times.
✓ Branch 1 taken 35 times.
|
66 | if (avctx->pix_fmt == AV_PIX_FMT_GBRP) { |
62 | 31 | naxis3 = 3; | |
63 | } else { | ||
64 | 35 | naxis3 = 4; | |
65 | } | ||
66 | 66 | break; | |
67 | 62 | case AV_PIX_FMT_GBRP16BE: | |
68 | case AV_PIX_FMT_GBRAP16BE: | ||
69 | 62 | bitpix = 16; | |
70 |
2/2✓ Branch 0 taken 31 times.
✓ Branch 1 taken 31 times.
|
62 | if (avctx->pix_fmt == AV_PIX_FMT_GBRP16BE) { |
71 | 31 | naxis3 = 3; | |
72 | } else { | ||
73 | 31 | naxis3 = 4; | |
74 | } | ||
75 | 62 | break; | |
76 | ✗ | default: | |
77 | ✗ | av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n"); | |
78 | ✗ | return AVERROR(EINVAL); | |
79 | } | ||
80 | |||
81 | 189 | data_size = (bitpix >> 3) * avctx->height * avctx->width * naxis3; | |
82 | 189 | padded_data_size = ((data_size + 2879) / 2880 ) * 2880; | |
83 | |||
84 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 189 times.
|
189 | if ((ret = ff_get_encode_buffer(avctx, pkt, padded_data_size, 0)) < 0) |
85 | ✗ | return ret; | |
86 | |||
87 | 189 | bytestream = pkt->data; | |
88 | |||
89 |
2/2✓ Branch 0 taken 511 times.
✓ Branch 1 taken 189 times.
|
700 | for (k = 0; k < naxis3; k++) { |
90 |
2/2✓ Branch 0 taken 120208 times.
✓ Branch 1 taken 511 times.
|
120719 | for (i = 0; i < avctx->height; i++) { |
91 | 120208 | ptr = p->data[map[k]] + (avctx->height - i - 1) * p->linesize[map[k]]; | |
92 |
2/2✓ Branch 0 taken 59936 times.
✓ Branch 1 taken 60272 times.
|
120208 | if (bitpix == 16) { |
93 |
2/2✓ Branch 0 taken 20493568 times.
✓ Branch 1 taken 59936 times.
|
20553504 | for (j = 0; j < avctx->width; j++) { |
94 | // subtracting bzero is equivalent to first bit flip | ||
95 | 20493568 | bytestream_put_be16(&bytestream, AV_RB16(ptr) ^ flip); | |
96 | 20493568 | ptr += 2; | |
97 | } | ||
98 | } else { | ||
99 | 60272 | memcpy(bytestream, ptr, avctx->width); | |
100 | 60272 | bytestream += avctx->width; | |
101 | } | ||
102 | } | ||
103 | } | ||
104 | |||
105 | 189 | bytes_left = padded_data_size - data_size; | |
106 | 189 | memset(bytestream, 0, bytes_left); | |
107 | |||
108 | 189 | *got_packet = 1; | |
109 | |||
110 | 189 | return 0; | |
111 | } | ||
112 | |||
113 | const FFCodec ff_fits_encoder = { | ||
114 | .p.name = "fits", | ||
115 | .p.long_name = NULL_IF_CONFIG_SMALL("Flexible Image Transport System"), | ||
116 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
117 | .p.id = AV_CODEC_ID_FITS, | ||
118 | .p.capabilities = AV_CODEC_CAP_DR1, | ||
119 | FF_CODEC_ENCODE_CB(fits_encode_frame), | ||
120 | .p.pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_GBRAP16BE, | ||
121 | AV_PIX_FMT_GBRP16BE, | ||
122 | AV_PIX_FMT_GBRP, | ||
123 | AV_PIX_FMT_GBRAP, | ||
124 | AV_PIX_FMT_GRAY16BE, | ||
125 | AV_PIX_FMT_GRAY8, | ||
126 | AV_PIX_FMT_NONE }, | ||
127 | }; | ||
128 |