FFmpeg
fitsenc.c
Go to the documentation of this file.
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 
39  const AVFrame *p, int *got_packet)
40 {
41  uint8_t *bytestream;
42  const uint16_t flip = (1 << 15);
43  uint64_t data_size = 0, padded_data_size = 0;
44  int ret, bitpix, naxis3 = 1, i, j, k, bytes_left;
45  int map[] = {2, 0, 1, 3}; // mapping from GBRA -> RGBA as RGBA is to be stored in FITS file..
46 
47  switch (avctx->pix_fmt) {
48  case AV_PIX_FMT_GRAY8:
50  map[0] = 0; // grayscale images should be directly mapped
51  if (avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
52  bitpix = 8;
53  } else {
54  bitpix = 16;
55  }
56  break;
57  case AV_PIX_FMT_GBRP:
58  case AV_PIX_FMT_GBRAP:
59  bitpix = 8;
60  if (avctx->pix_fmt == AV_PIX_FMT_GBRP) {
61  naxis3 = 3;
62  } else {
63  naxis3 = 4;
64  }
65  break;
68  bitpix = 16;
69  if (avctx->pix_fmt == AV_PIX_FMT_GBRP16BE) {
70  naxis3 = 3;
71  } else {
72  naxis3 = 4;
73  }
74  break;
75  default:
76  av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n");
77  return AVERROR(EINVAL);
78  }
79 
80  data_size = (bitpix >> 3) * avctx->height * avctx->width * naxis3;
81  padded_data_size = ((data_size + 2879) / 2880 ) * 2880;
82 
83  if ((ret = ff_get_encode_buffer(avctx, pkt, padded_data_size, 0)) < 0)
84  return ret;
85 
86  bytestream = pkt->data;
87 
88  for (k = 0; k < naxis3; k++) {
89  for (i = 0; i < avctx->height; i++) {
90  const uint8_t *ptr = p->data[map[k]] + (avctx->height - i - 1) * p->linesize[map[k]];
91  if (bitpix == 16) {
92  for (j = 0; j < avctx->width; j++) {
93  // subtracting bzero is equivalent to first bit flip
94  bytestream_put_be16(&bytestream, AV_RB16(ptr) ^ flip);
95  ptr += 2;
96  }
97  } else {
98  memcpy(bytestream, ptr, avctx->width);
99  bytestream += avctx->width;
100  }
101  }
102  }
103 
104  bytes_left = padded_data_size - data_size;
105  memset(bytestream, 0, bytes_left);
106 
108  *got_packet = 1;
109 
110  return 0;
111 }
112 
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,
120  .p.pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_GBRAP16BE,
126  AV_PIX_FMT_NONE },
127 };
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AV_PIX_FMT_GBRP16BE
@ AV_PIX_FMT_GBRP16BE
planar GBR 4:4:4 48bpp, big-endian
Definition: pixfmt.h:171
ff_fits_encoder
const FFCodec ff_fits_encoder
Definition: fitsenc.c:113
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
AVPacket::data
uint8_t * data
Definition: packet.h:539
encode.h
FFCodec
Definition: codec_internal.h:127
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:594
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:410
AV_PIX_FMT_GRAY16BE
@ AV_PIX_FMT_GRAY16BE
Y , 16bpp, big-endian.
Definition: pixfmt.h:104
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
AV_PIX_FMT_GBRAP
@ AV_PIX_FMT_GBRAP
planar GBRA 4:4:4:4 32bpp
Definition: pixfmt.h:212
FF_CODEC_ENCODE_CB
#define FF_CODEC_ENCODE_CB(func)
Definition: codec_internal.h:320
pkt
AVPacket * pkt
Definition: movenc.c:60
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
AV_PIX_FMT_GBRAP16BE
@ AV_PIX_FMT_GBRAP16BE
planar GBRA 4:4:4:4 64bpp, big-endian
Definition: pixfmt.h:213
intreadwrite.h
AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
Definition: codec.h:159
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:296
if
if(ret)
Definition: filter_design.txt:179
AV_CODEC_ID_FITS
@ AV_CODEC_ID_FITS
Definition: codec_id.h:290
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:81
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
flip
static void flip(AVCodecContext *avctx, AVFrame *frame)
Definition: rawdec.c:131
fits_encode_frame
static int fits_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *p, int *got_packet)
Definition: fitsenc.c:38
codec_internal.h
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:545
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
AVCodecContext::height
int height
Definition: avcodec.h:624
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:663
avcodec.h
ret
ret
Definition: filter_design.txt:187
AVCodecContext
main external API structure.
Definition: avcodec.h:451
ff_get_encode_buffer
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
Definition: encode.c:106
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
AV_PIX_FMT_GBRP
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:165
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
map
const VDPAUPixFmtMap * map
Definition: hwcontext_vdpau.c:71
AVPacket
This structure stores compressed data.
Definition: packet.h:516
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:624
bytestream.h
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:434
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AV_RB16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:98