You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
4.3KB

  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. * @file
  23. * FITS image encoder
  24. *
  25. * Specification: https://fits.gsfc.nasa.gov/fits_standard.html Version 3.0
  26. *
  27. * RGBA images are encoded as planes in RGBA order. So, NAXIS3 is 3 or 4 for them.
  28. * Also CTYPE3 = 'RGB ' is added to the header to distinguish them from 3d images.
  29. */
  30. #include "libavutil/intreadwrite.h"
  31. #include "avcodec.h"
  32. #include "bytestream.h"
  33. #include "internal.h"
  34. static int fits_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  35. const AVFrame *pict, int *got_packet)
  36. {
  37. AVFrame * const p = (AVFrame *)pict;
  38. uint8_t *bytestream, *bytestream_start, *ptr;
  39. const uint16_t flip = (1 << 15);
  40. uint64_t data_size = 0, padded_data_size = 0;
  41. int ret, bitpix, naxis3 = 1, i, j, k, bytes_left;
  42. int map[] = {2, 0, 1, 3}; // mapping from GBRA -> RGBA as RGBA is to be stored in FITS file..
  43. switch (avctx->pix_fmt) {
  44. case AV_PIX_FMT_GRAY8:
  45. case AV_PIX_FMT_GRAY16BE:
  46. map[0] = 0; // grayscale images should be directly mapped
  47. if (avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
  48. bitpix = 8;
  49. } else {
  50. bitpix = 16;
  51. }
  52. break;
  53. case AV_PIX_FMT_GBRP:
  54. case AV_PIX_FMT_GBRAP:
  55. bitpix = 8;
  56. if (avctx->pix_fmt == AV_PIX_FMT_GBRP) {
  57. naxis3 = 3;
  58. } else {
  59. naxis3 = 4;
  60. }
  61. break;
  62. case AV_PIX_FMT_GBRP16BE:
  63. case AV_PIX_FMT_GBRAP16BE:
  64. bitpix = 16;
  65. if (avctx->pix_fmt == AV_PIX_FMT_GBRP16BE) {
  66. naxis3 = 3;
  67. } else {
  68. naxis3 = 4;
  69. }
  70. break;
  71. default:
  72. av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n");
  73. return AVERROR(EINVAL);
  74. }
  75. data_size = (bitpix >> 3) * avctx->height * avctx->width * naxis3;
  76. padded_data_size = ((data_size + 2879) / 2880 ) * 2880;
  77. if ((ret = ff_alloc_packet2(avctx, pkt, padded_data_size, 0)) < 0)
  78. return ret;
  79. bytestream_start =
  80. bytestream = pkt->data;
  81. for (k = 0; k < naxis3; k++) {
  82. for (i = 0; i < avctx->height; i++) {
  83. ptr = p->data[map[k]] + (avctx->height - i - 1) * p->linesize[map[k]];
  84. if (bitpix == 16) {
  85. for (j = 0; j < avctx->width; j++) {
  86. // subtracting bzero is equivalent to first bit flip
  87. bytestream_put_be16(&bytestream, AV_RB16(ptr) ^ flip);
  88. ptr += 2;
  89. }
  90. } else {
  91. memcpy(bytestream, ptr, avctx->width);
  92. bytestream += avctx->width;
  93. }
  94. }
  95. }
  96. bytes_left = padded_data_size - data_size;
  97. memset(bytestream, 0, bytes_left);
  98. bytestream += bytes_left;
  99. pkt->size = bytestream - bytestream_start;
  100. pkt->flags |= AV_PKT_FLAG_KEY;
  101. *got_packet = 1;
  102. return 0;
  103. }
  104. AVCodec ff_fits_encoder = {
  105. .name = "fits",
  106. .long_name = NULL_IF_CONFIG_SMALL("Flexible Image Transport System"),
  107. .type = AVMEDIA_TYPE_VIDEO,
  108. .id = AV_CODEC_ID_FITS,
  109. .encode2 = fits_encode_frame,
  110. .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_GBRAP16BE,
  111. AV_PIX_FMT_GBRP16BE,
  112. AV_PIX_FMT_GBRP,
  113. AV_PIX_FMT_GBRAP,
  114. AV_PIX_FMT_GRAY16BE,
  115. AV_PIX_FMT_GRAY8,
  116. AV_PIX_FMT_NONE },
  117. };