/src/ffmpeg/libavcodec/fitsenc.c
Line | Count | 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 | | static int fits_encode_frame(AVCodecContext *avctx, AVPacket *pkt, |
39 | | const AVFrame *p, int *got_packet) |
40 | 8.39k | { |
41 | 8.39k | uint8_t *bytestream; |
42 | 8.39k | const uint16_t flip = (1 << 15); |
43 | 8.39k | uint64_t data_size = 0, padded_data_size = 0; |
44 | 8.39k | int ret, bitpix, naxis3 = 1, i, j, k, bytes_left; |
45 | 8.39k | int map[] = {2, 0, 1, 3}; // mapping from GBRA -> RGBA as RGBA is to be stored in FITS file.. |
46 | | |
47 | 8.39k | switch (avctx->pix_fmt) { |
48 | 2.32k | case AV_PIX_FMT_GRAY8: |
49 | 7.07k | case AV_PIX_FMT_GRAY16BE: |
50 | 7.07k | map[0] = 0; // grayscale images should be directly mapped |
51 | 7.07k | if (avctx->pix_fmt == AV_PIX_FMT_GRAY8) { |
52 | 2.32k | bitpix = 8; |
53 | 4.74k | } else { |
54 | 4.74k | bitpix = 16; |
55 | 4.74k | } |
56 | 7.07k | break; |
57 | 286 | case AV_PIX_FMT_GBRP: |
58 | 755 | case AV_PIX_FMT_GBRAP: |
59 | 755 | bitpix = 8; |
60 | 755 | if (avctx->pix_fmt == AV_PIX_FMT_GBRP) { |
61 | 286 | naxis3 = 3; |
62 | 469 | } else { |
63 | 469 | naxis3 = 4; |
64 | 469 | } |
65 | 755 | break; |
66 | 278 | case AV_PIX_FMT_GBRP16BE: |
67 | 567 | case AV_PIX_FMT_GBRAP16BE: |
68 | 567 | bitpix = 16; |
69 | 567 | if (avctx->pix_fmt == AV_PIX_FMT_GBRP16BE) { |
70 | 278 | naxis3 = 3; |
71 | 289 | } else { |
72 | 289 | naxis3 = 4; |
73 | 289 | } |
74 | 567 | break; |
75 | 0 | default: |
76 | 0 | av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n"); |
77 | 0 | return AVERROR(EINVAL); |
78 | 8.39k | } |
79 | | |
80 | 8.39k | data_size = (bitpix >> 3) * avctx->height * avctx->width * naxis3; |
81 | 8.39k | padded_data_size = ((data_size + 2879) / 2880 ) * 2880; |
82 | | |
83 | 8.39k | if ((ret = ff_get_encode_buffer(avctx, pkt, padded_data_size, 0)) < 0) |
84 | 0 | return ret; |
85 | | |
86 | 8.39k | bytestream = pkt->data; |
87 | | |
88 | 20.1k | for (k = 0; k < naxis3; k++) { |
89 | 2.40M | for (i = 0; i < avctx->height; i++) { |
90 | 2.39M | const uint8_t *ptr = p->data[map[k]] + (avctx->height - i - 1) * p->linesize[map[k]]; |
91 | 2.39M | if (bitpix == 16) { |
92 | 15.5M | for (j = 0; j < avctx->width; j++) { |
93 | | // subtracting bzero is equivalent to first bit flip |
94 | 14.4M | bytestream_put_be16(&bytestream, AV_RB16(ptr) ^ flip); |
95 | 14.4M | ptr += 2; |
96 | 14.4M | } |
97 | 1.24M | } else { |
98 | 1.24M | memcpy(bytestream, ptr, avctx->width); |
99 | 1.24M | bytestream += avctx->width; |
100 | 1.24M | } |
101 | 2.39M | } |
102 | 11.7k | } |
103 | | |
104 | 8.39k | bytes_left = padded_data_size - data_size; |
105 | 8.39k | memset(bytestream, 0, bytes_left); |
106 | | |
107 | 8.39k | pkt->flags |= AV_PKT_FLAG_KEY; |
108 | 8.39k | *got_packet = 1; |
109 | | |
110 | 8.39k | return 0; |
111 | 8.39k | } |
112 | | |
113 | | const FFCodec ff_fits_encoder = { |
114 | | .p.name = "fits", |
115 | | CODEC_LONG_NAME("Flexible Image Transport System"), |
116 | | .p.type = AVMEDIA_TYPE_VIDEO, |
117 | | .p.id = AV_CODEC_ID_FITS, |
118 | | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE, |
119 | | FF_CODEC_ENCODE_CB(fits_encode_frame), |
120 | | CODEC_PIXFMTS(AV_PIX_FMT_GBRAP16BE, AV_PIX_FMT_GBRP16BE, AV_PIX_FMT_GBRP, |
121 | | AV_PIX_FMT_GBRAP, AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_GRAY8), |
122 | | }; |