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
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/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. case PIX_FMT_RGBA64LE:
  52. s->big_endian = 0;
  53. case PIX_FMT_RGBA64BE:
  54. s->descriptor = 51;
  55. s->bits_per_component = 16;
  56. break;
  57. default:
  58. av_log(avctx, AV_LOG_INFO, "unsupported pixel format\n");
  59. return -1;
  60. }
  61. return 0;
  62. }
  63. #define write16(p, value) \
  64. do { \
  65. if (s->big_endian) AV_WB16(p, value); \
  66. else AV_WL16(p, value); \
  67. } while(0)
  68. #define write32(p, value) \
  69. do { \
  70. if (s->big_endian) AV_WB32(p, value); \
  71. else AV_WL32(p, value); \
  72. } while(0)
  73. static void encode_rgb48_10bit(AVCodecContext *avctx, const AVPicture *pic, uint8_t *dst)
  74. {
  75. DPXContext *s = avctx->priv_data;
  76. const uint8_t *src = pic->data[0];
  77. int x, y;
  78. for (y = 0; y < avctx->height; y++) {
  79. for (x = 0; x < avctx->width; x++) {
  80. int value;
  81. if ((avctx->pix_fmt & 1)) {
  82. value = ((AV_RB16(src + 6*x + 4) & 0xFFC0) >> 4)
  83. | ((AV_RB16(src + 6*x + 2) & 0xFFC0) << 6)
  84. | ((AV_RB16(src + 6*x + 0) & 0xFFC0) << 16);
  85. } else {
  86. value = ((AV_RL16(src + 6*x + 4) & 0xFFC0) >> 4)
  87. | ((AV_RL16(src + 6*x + 2) & 0xFFC0) << 6)
  88. | ((AV_RL16(src + 6*x + 0) & 0xFFC0) << 16);
  89. }
  90. write32(dst, value);
  91. dst += 4;
  92. }
  93. src += pic->linesize[0];
  94. }
  95. }
  96. static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  97. const AVFrame *frame, int *got_packet)
  98. {
  99. DPXContext *s = avctx->priv_data;
  100. int size, ret;
  101. uint8_t *buf;
  102. #define HEADER_SIZE 1664 /* DPX Generic header */
  103. if (s->bits_per_component == 10)
  104. size = avctx->height * avctx->width * 4;
  105. else
  106. size = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
  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 & CODEC_FLAG_BITEXACT)){
  120. memcpy (buf + 160, LIBAVCODEC_IDENT, FFMIN(sizeof(LIBAVCODEC_IDENT), 100));
  121. }
  122. write32(buf + 660, 0xFFFFFFFF); /* unencrypted */
  123. /* Image information header */
  124. write16(buf + 768, 0); /* orientation; left to right, top to bottom */
  125. write16(buf + 770, 1); /* number of elements */
  126. write32(buf + 772, avctx->width);
  127. write32(buf + 776, avctx->height);
  128. buf[800] = s->descriptor;
  129. buf[801] = 2; /* linear transfer */
  130. buf[802] = 2; /* linear colorimetric */
  131. buf[803] = s->bits_per_component;
  132. write16(buf + 804, s->bits_per_component == 10 ? 1 : 0); /* packing method */
  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 = avpicture_layout((const AVPicture*)frame, avctx->pix_fmt,
  140. avctx->width, avctx->height,
  141. buf + HEADER_SIZE, pkt->size - HEADER_SIZE);
  142. if (size < 0)
  143. return size;
  144. break;
  145. case 10:
  146. encode_rgb48_10bit(avctx, (const AVPicture*)frame, buf + HEADER_SIZE);
  147. break;
  148. default:
  149. av_log(avctx, AV_LOG_ERROR, "Unsupported bit depth: %d\n", s->bits_per_component);
  150. return -1;
  151. }
  152. size += HEADER_SIZE;
  153. write32(buf + 16, size); /* file size */
  154. pkt->flags |= AV_PKT_FLAG_KEY;
  155. *got_packet = 1;
  156. return 0;
  157. }
  158. AVCodec ff_dpx_encoder = {
  159. .name = "dpx",
  160. .type = AVMEDIA_TYPE_VIDEO,
  161. .id = CODEC_ID_DPX,
  162. .priv_data_size = sizeof(DPXContext),
  163. .init = encode_init,
  164. .encode2 = encode_frame,
  165. .pix_fmts = (const enum PixelFormat[]){
  166. PIX_FMT_RGB24,
  167. PIX_FMT_RGBA,
  168. PIX_FMT_RGB48LE,
  169. PIX_FMT_RGB48BE,
  170. PIX_FMT_RGBA64LE,
  171. PIX_FMT_RGBA64BE,
  172. PIX_FMT_NONE},
  173. .long_name = NULL_IF_CONFIG_SMALL("DPX image"),
  174. };