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.

198 lines
6.1KB

  1. /*
  2. * DPX (.dpx) image encoder
  3. * Copyright (c) 2011 Peter Ross <pross@xvid.org>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/common.h"
  22. #include "libavutil/intreadwrite.h"
  23. #include "libavutil/imgutils.h"
  24. #include "avcodec.h"
  25. #include "internal.h"
  26. typedef struct DPXContext {
  27. int big_endian;
  28. int bits_per_component;
  29. int descriptor;
  30. } DPXContext;
  31. static av_cold int encode_init(AVCodecContext *avctx)
  32. {
  33. DPXContext *s = avctx->priv_data;
  34. #if FF_API_CODED_FRAME
  35. FF_DISABLE_DEPRECATION_WARNINGS
  36. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  37. avctx->coded_frame->key_frame = 1;
  38. FF_ENABLE_DEPRECATION_WARNINGS
  39. #endif
  40. s->big_endian = 1;
  41. s->bits_per_component = 8;
  42. s->descriptor = 50; /* RGB */
  43. switch (avctx->pix_fmt) {
  44. case AV_PIX_FMT_RGB24:
  45. break;
  46. case AV_PIX_FMT_RGBA:
  47. s->descriptor = 51; /* RGBA */
  48. break;
  49. case AV_PIX_FMT_RGB48LE:
  50. s->big_endian = 0;
  51. /* fall-through */
  52. case AV_PIX_FMT_RGB48BE:
  53. s->bits_per_component = avctx->bits_per_raw_sample ? avctx->bits_per_raw_sample : 16;
  54. break;
  55. default:
  56. av_log(avctx, AV_LOG_INFO, "unsupported pixel format\n");
  57. return -1;
  58. }
  59. return 0;
  60. }
  61. #define write16(p, value) \
  62. do { \
  63. if (s->big_endian) AV_WB16(p, value); \
  64. else AV_WL16(p, value); \
  65. } while(0)
  66. #define write32(p, value) \
  67. do { \
  68. if (s->big_endian) AV_WB32(p, value); \
  69. else AV_WL32(p, value); \
  70. } while(0)
  71. static void encode_rgb48_10bit(AVCodecContext *avctx, const AVFrame *pic,
  72. uint8_t *dst)
  73. {
  74. DPXContext *s = avctx->priv_data;
  75. const uint8_t *src = pic->data[0];
  76. int x, y;
  77. for (y = 0; y < avctx->height; y++) {
  78. for (x = 0; x < avctx->width; x++) {
  79. int value;
  80. if ((avctx->pix_fmt & 1)) {
  81. value = ((AV_RB16(src + 6*x + 4) & 0xFFC0) >> 4)
  82. | ((AV_RB16(src + 6*x + 2) & 0xFFC0) << 6)
  83. | ((AV_RB16(src + 6*x + 0) & 0xFFC0) << 16);
  84. } else {
  85. value = ((AV_RL16(src + 6*x + 4) & 0xFFC0) >> 4)
  86. | ((AV_RL16(src + 6*x + 2) & 0xFFC0) << 6)
  87. | ((AV_RL16(src + 6*x + 0) & 0xFFC0) << 16);
  88. }
  89. write32(dst, value);
  90. dst += 4;
  91. }
  92. src += pic->linesize[0];
  93. }
  94. }
  95. static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  96. const AVFrame *frame, int *got_packet)
  97. {
  98. DPXContext *s = avctx->priv_data;
  99. int size, ret;
  100. uint8_t *buf;
  101. #define HEADER_SIZE 1664 /* DPX Generic header */
  102. if (s->bits_per_component == 10)
  103. size = avctx->height * avctx->width * 4;
  104. else
  105. size = av_image_get_buffer_size(avctx->pix_fmt,
  106. avctx->width, avctx->height, 1);
  107. if ((ret = ff_alloc_packet(pkt, size + HEADER_SIZE)) < 0) {
  108. av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
  109. return ret;
  110. }
  111. buf = pkt->data;
  112. memset(buf, 0, HEADER_SIZE);
  113. /* File information header */
  114. write32(buf, MKBETAG('S','D','P','X'));
  115. write32(buf + 4, HEADER_SIZE);
  116. memcpy (buf + 8, "V1.0", 4);
  117. write32(buf + 20, 1); /* new image */
  118. write32(buf + 24, HEADER_SIZE);
  119. if (!(avctx->flags & AV_CODEC_FLAG_BITEXACT))
  120. memcpy (buf + 160, LIBAVCODEC_IDENT, FFMIN(sizeof(LIBAVCODEC_IDENT), 100));
  121. write32(buf + 660, 0xFFFFFFFF); /* unencrypted */
  122. /* Image information header */
  123. write16(buf + 768, 0); /* orientation; left to right, top to bottom */
  124. write16(buf + 770, 1); /* number of elements */
  125. write32(buf + 772, avctx->width);
  126. write32(buf + 776, avctx->height);
  127. buf[800] = s->descriptor;
  128. buf[801] = 2; /* linear transfer */
  129. buf[802] = 2; /* linear colorimetric */
  130. buf[803] = s->bits_per_component;
  131. write16(buf + 804, s->bits_per_component == 10 ? 1 : 0); /* packing method */
  132. write32(buf + 808, HEADER_SIZE); /* data offset */
  133. /* Image source information header */
  134. write32(buf + 1628, avctx->sample_aspect_ratio.num);
  135. write32(buf + 1632, avctx->sample_aspect_ratio.den);
  136. switch (s->bits_per_component) {
  137. case 8:
  138. case 16:
  139. size = av_image_copy_to_buffer(buf + HEADER_SIZE,
  140. pkt->size - HEADER_SIZE,
  141. frame->data, frame->linesize,
  142. avctx->pix_fmt,
  143. avctx->width, avctx->height, 1);
  144. if (size < 0)
  145. return size;
  146. break;
  147. case 10:
  148. encode_rgb48_10bit(avctx, frame, buf + HEADER_SIZE);
  149. break;
  150. default:
  151. av_log(avctx, AV_LOG_ERROR, "Unsupported bit depth: %d\n", s->bits_per_component);
  152. return -1;
  153. }
  154. size += HEADER_SIZE;
  155. write32(buf + 16, size); /* file size */
  156. pkt->flags |= AV_PKT_FLAG_KEY;
  157. *got_packet = 1;
  158. return 0;
  159. }
  160. AVCodec ff_dpx_encoder = {
  161. .name = "dpx",
  162. .long_name = NULL_IF_CONFIG_SMALL("DPX image"),
  163. .type = AVMEDIA_TYPE_VIDEO,
  164. .id = AV_CODEC_ID_DPX,
  165. .priv_data_size = sizeof(DPXContext),
  166. .init = encode_init,
  167. .encode2 = encode_frame,
  168. .pix_fmts = (const enum AVPixelFormat[]){
  169. AV_PIX_FMT_RGB24,
  170. AV_PIX_FMT_RGBA,
  171. AV_PIX_FMT_RGB48LE,
  172. AV_PIX_FMT_RGB48BE,
  173. AV_PIX_FMT_NONE},
  174. };