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.

188 lines
5.5KB

  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. * @file
  23. * FITS muxer.
  24. */
  25. #include "internal.h"
  26. typedef struct FITSContext {
  27. int first_image;
  28. } FITSContext;
  29. static int fits_write_header(AVFormatContext *s)
  30. {
  31. FITSContext *fitsctx = s->priv_data;
  32. fitsctx->first_image = 1;
  33. return 0;
  34. }
  35. /**
  36. * Write one header line comprising of keyword and value(int)
  37. * @param s AVFormat Context
  38. * @param keyword pointer to the char array in which keyword is stored
  39. * @param value the value corresponding to the keyword
  40. * @param lines_written to keep track of lines written so far
  41. * @return 0
  42. */
  43. static int write_keyword_value(AVFormatContext *s, const char *keyword, int value, int *lines_written)
  44. {
  45. int len, ret;
  46. uint8_t header[80];
  47. len = strlen(keyword);
  48. memset(header, ' ', sizeof(header));
  49. memcpy(header, keyword, len);
  50. header[8] = '=';
  51. header[9] = ' ';
  52. ret = snprintf(header + 10, 70, "%d", value);
  53. memset(&header[ret + 10], ' ', sizeof(header) - (ret + 10));
  54. avio_write(s->pb, header, sizeof(header));
  55. *lines_written += 1;
  56. return 0;
  57. }
  58. static int write_image_header(AVFormatContext *s)
  59. {
  60. AVStream *st = s->streams[0];
  61. AVCodecParameters *encctx = st->codecpar;
  62. FITSContext *fitsctx = s->priv_data;
  63. uint8_t buffer[80];
  64. int bitpix, naxis, naxis3 = 1, bzero = 0, rgb = 0, lines_written = 0, lines_left;
  65. switch (encctx->format) {
  66. case AV_PIX_FMT_GRAY8:
  67. bitpix = 8;
  68. naxis = 2;
  69. break;
  70. case AV_PIX_FMT_GRAY16BE:
  71. bitpix = 16;
  72. naxis = 2;
  73. bzero = 32768;
  74. break;
  75. case AV_PIX_FMT_GBRP:
  76. case AV_PIX_FMT_GBRAP:
  77. bitpix = 8;
  78. naxis = 3;
  79. rgb = 1;
  80. if (encctx->format == AV_PIX_FMT_GBRP) {
  81. naxis3 = 3;
  82. } else {
  83. naxis3 = 4;
  84. }
  85. break;
  86. case AV_PIX_FMT_GBRP16BE:
  87. case AV_PIX_FMT_GBRAP16BE:
  88. bitpix = 16;
  89. naxis = 3;
  90. rgb = 1;
  91. if (encctx->format == AV_PIX_FMT_GBRP16BE) {
  92. naxis3 = 3;
  93. } else {
  94. naxis3 = 4;
  95. }
  96. bzero = 32768;
  97. break;
  98. default:
  99. return AVERROR(EINVAL);
  100. }
  101. if (fitsctx->first_image) {
  102. memcpy(buffer, "SIMPLE = ", 10);
  103. memset(buffer + 10, ' ', 70);
  104. buffer[29] = 'T';
  105. avio_write(s->pb, buffer, sizeof(buffer));
  106. } else {
  107. memcpy(buffer, "XTENSION= 'IMAGE '", 20);
  108. memset(buffer + 20, ' ', 60);
  109. avio_write(s->pb, buffer, sizeof(buffer));
  110. }
  111. lines_written++;
  112. write_keyword_value(s, "BITPIX", bitpix, &lines_written); // no of bits per pixel
  113. write_keyword_value(s, "NAXIS", naxis, &lines_written); // no of dimensions of image
  114. write_keyword_value(s, "NAXIS1", encctx->width, &lines_written); // first dimension i.e. width
  115. write_keyword_value(s, "NAXIS2", encctx->height, &lines_written); // second dimension i.e. height
  116. if (rgb)
  117. write_keyword_value(s, "NAXIS3", naxis3, &lines_written); // third dimension to store RGBA planes
  118. if (!fitsctx->first_image) {
  119. write_keyword_value(s, "PCOUNT", 0, &lines_written);
  120. write_keyword_value(s, "GCOUNT", 1, &lines_written);
  121. } else {
  122. fitsctx->first_image = 0;
  123. }
  124. /*
  125. * Since FITS does not support unsigned 16 bit integers,
  126. * BZERO = 32768 is used to store unsigned 16 bit integers as
  127. * signed integers so that it can be read properly.
  128. */
  129. if (bitpix == 16)
  130. write_keyword_value(s, "BZERO", bzero, &lines_written);
  131. if (rgb) {
  132. memcpy(buffer, "CTYPE3 = 'RGB '", 20);
  133. memset(buffer + 20, ' ', 60);
  134. avio_write(s->pb, buffer, sizeof(buffer));
  135. lines_written++;
  136. }
  137. memcpy(buffer, "END", 3);
  138. memset(buffer + 3, ' ', 77);
  139. avio_write(s->pb, buffer, sizeof(buffer));
  140. lines_written++;
  141. lines_left = ((lines_written + 35) / 36) * 36 - lines_written;
  142. memset(buffer, ' ', 80);
  143. while (lines_left > 0) {
  144. avio_write(s->pb, buffer, sizeof(buffer));
  145. lines_left--;
  146. }
  147. return 0;
  148. }
  149. static int fits_write_packet(AVFormatContext *s, AVPacket *pkt)
  150. {
  151. int ret = write_image_header(s);
  152. if (ret < 0)
  153. return ret;
  154. avio_write(s->pb, pkt->data, pkt->size);
  155. return 0;
  156. }
  157. AVOutputFormat ff_fits_muxer = {
  158. .name = "fits",
  159. .long_name = NULL_IF_CONFIG_SMALL("Flexible Image Transport System"),
  160. .extensions = "fits",
  161. .priv_data_size = sizeof(FITSContext),
  162. .audio_codec = AV_CODEC_ID_NONE,
  163. .video_codec = AV_CODEC_ID_FITS,
  164. .write_header = fits_write_header,
  165. .write_packet = fits_write_packet,
  166. };