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.

187 lines
5.6KB

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