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.

190 lines
5.7KB

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