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.

200 lines
6.0KB

  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. avctx->coded_frame = av_frame_alloc();
  35. if (!avctx->coded_frame)
  36. return AVERROR(ENOMEM);
  37. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  38. avctx->coded_frame->key_frame = 1;
  39. s->big_endian = 1;
  40. s->bits_per_component = 8;
  41. s->descriptor = 50; /* RGB */
  42. switch (avctx->pix_fmt) {
  43. case AV_PIX_FMT_RGB24:
  44. break;
  45. case AV_PIX_FMT_RGBA:
  46. s->descriptor = 51; /* RGBA */
  47. break;
  48. case AV_PIX_FMT_RGB48LE:
  49. s->big_endian = 0;
  50. case AV_PIX_FMT_RGB48BE:
  51. s->bits_per_component = avctx->bits_per_raw_sample ? avctx->bits_per_raw_sample : 16;
  52. break;
  53. default:
  54. av_log(avctx, AV_LOG_INFO, "unsupported pixel format\n");
  55. return -1;
  56. }
  57. return 0;
  58. }
  59. #define write16(p, value) \
  60. do { \
  61. if (s->big_endian) AV_WB16(p, value); \
  62. else AV_WL16(p, value); \
  63. } while(0)
  64. #define write32(p, value) \
  65. do { \
  66. if (s->big_endian) AV_WB32(p, value); \
  67. else AV_WL32(p, value); \
  68. } while(0)
  69. static void encode_rgb48_10bit(AVCodecContext *avctx, const AVPicture *pic,
  70. uint8_t *dst)
  71. {
  72. DPXContext *s = avctx->priv_data;
  73. const uint8_t *src = pic->data[0];
  74. int x, y;
  75. for (y = 0; y < avctx->height; y++) {
  76. for (x = 0; x < avctx->width; x++) {
  77. int value;
  78. if ((avctx->pix_fmt & 1)) {
  79. value = ((AV_RB16(src + 6*x + 4) & 0xFFC0) >> 4)
  80. | ((AV_RB16(src + 6*x + 2) & 0xFFC0) << 6)
  81. | ((AV_RB16(src + 6*x + 0) & 0xFFC0) << 16);
  82. } else {
  83. value = ((AV_RL16(src + 6*x + 4) & 0xFFC0) >> 4)
  84. | ((AV_RL16(src + 6*x + 2) & 0xFFC0) << 6)
  85. | ((AV_RL16(src + 6*x + 0) & 0xFFC0) << 16);
  86. }
  87. write32(dst, value);
  88. dst += 4;
  89. }
  90. src += pic->linesize[0];
  91. }
  92. }
  93. static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  94. const AVFrame *frame, int *got_packet)
  95. {
  96. DPXContext *s = avctx->priv_data;
  97. int size, ret;
  98. uint8_t *buf;
  99. #define HEADER_SIZE 1664 /* DPX Generic header */
  100. if (s->bits_per_component == 10)
  101. size = avctx->height * avctx->width * 4;
  102. else
  103. size = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
  104. if ((ret = ff_alloc_packet(pkt, size + HEADER_SIZE)) < 0) {
  105. av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
  106. return ret;
  107. }
  108. buf = pkt->data;
  109. memset(buf, 0, HEADER_SIZE);
  110. /* File information header */
  111. write32(buf, MKBETAG('S','D','P','X'));
  112. write32(buf + 4, HEADER_SIZE);
  113. memcpy (buf + 8, "V1.0", 4);
  114. write32(buf + 20, 1); /* new image */
  115. write32(buf + 24, HEADER_SIZE);
  116. if (!(avctx->flags & CODEC_FLAG_BITEXACT))
  117. memcpy (buf + 160, LIBAVCODEC_IDENT, FFMIN(sizeof(LIBAVCODEC_IDENT), 100));
  118. write32(buf + 660, 0xFFFFFFFF); /* unencrypted */
  119. /* Image information header */
  120. write16(buf + 768, 0); /* orientation; left to right, top to bottom */
  121. write16(buf + 770, 1); /* number of elements */
  122. write32(buf + 772, avctx->width);
  123. write32(buf + 776, avctx->height);
  124. buf[800] = s->descriptor;
  125. buf[801] = 2; /* linear transfer */
  126. buf[802] = 2; /* linear colorimetric */
  127. buf[803] = s->bits_per_component;
  128. write16(buf + 804, s->bits_per_component == 10 ? 1 : 0); /* packing method */
  129. /* Image source information header */
  130. write32(buf + 1628, avctx->sample_aspect_ratio.num);
  131. write32(buf + 1632, avctx->sample_aspect_ratio.den);
  132. switch (s->bits_per_component) {
  133. case 8:
  134. case 16:
  135. size = avpicture_layout((const AVPicture*)frame, avctx->pix_fmt,
  136. avctx->width, avctx->height,
  137. buf + HEADER_SIZE, pkt->size - HEADER_SIZE);
  138. if (size < 0)
  139. return size;
  140. break;
  141. case 10:
  142. encode_rgb48_10bit(avctx, (const AVPicture*)frame, buf + HEADER_SIZE);
  143. break;
  144. default:
  145. av_log(avctx, AV_LOG_ERROR, "Unsupported bit depth: %d\n", s->bits_per_component);
  146. return -1;
  147. }
  148. size += HEADER_SIZE;
  149. write32(buf + 16, size); /* file size */
  150. pkt->flags |= AV_PKT_FLAG_KEY;
  151. *got_packet = 1;
  152. return 0;
  153. }
  154. static av_cold int encode_close(AVCodecContext *avctx)
  155. {
  156. av_frame_free(&avctx->coded_frame);
  157. return 0;
  158. }
  159. AVCodec ff_dpx_encoder = {
  160. .name = "dpx",
  161. .long_name = NULL_IF_CONFIG_SMALL("DPX image"),
  162. .type = AVMEDIA_TYPE_VIDEO,
  163. .id = AV_CODEC_ID_DPX,
  164. .priv_data_size = sizeof(DPXContext),
  165. .init = encode_init,
  166. .encode2 = encode_frame,
  167. .close = encode_close,
  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. };