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.

196 lines
5.9KB

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