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.

252 lines
8.3KB

  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. int big_endian;
  28. int bits_per_component;
  29. int descriptor;
  30. int planar;
  31. } DPXContext;
  32. static av_cold int encode_init(AVCodecContext *avctx)
  33. {
  34. DPXContext *s = avctx->priv_data;
  35. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  36. s->big_endian = !!(desc->flags & AV_PIX_FMT_FLAG_BE);
  37. s->bits_per_component = desc->comp[0].depth_minus1 + 1;
  38. s->descriptor = (desc->flags & AV_PIX_FMT_FLAG_ALPHA) ? 51 : 50;
  39. s->planar = !!(desc->flags & AV_PIX_FMT_FLAG_PLANAR);
  40. switch (avctx->pix_fmt) {
  41. case AV_PIX_FMT_GBRP10BE:
  42. case AV_PIX_FMT_GBRP10LE:
  43. case AV_PIX_FMT_GBRP12BE:
  44. case AV_PIX_FMT_GBRP12LE:
  45. case AV_PIX_FMT_RGB24:
  46. case AV_PIX_FMT_RGBA64BE:
  47. case AV_PIX_FMT_RGBA64LE:
  48. case AV_PIX_FMT_RGBA:
  49. break;
  50. case AV_PIX_FMT_RGB48LE:
  51. case AV_PIX_FMT_RGB48BE:
  52. if (avctx->bits_per_raw_sample)
  53. s->bits_per_component = avctx->bits_per_raw_sample;
  54. break;
  55. default:
  56. av_log(avctx, AV_LOG_INFO, "unsupported pixel format\n");
  57. return -1;
  58. }
  59. return 0;
  60. }
  61. #define write16(p, value) \
  62. do { \
  63. if (s->big_endian) AV_WB16(p, value); \
  64. else AV_WL16(p, value); \
  65. } while(0)
  66. #define write32(p, value) \
  67. do { \
  68. if (s->big_endian) AV_WB32(p, value); \
  69. else AV_WL32(p, value); \
  70. } while(0)
  71. static void encode_rgb48_10bit(AVCodecContext *avctx, const AVPicture *pic, uint8_t *dst)
  72. {
  73. DPXContext *s = avctx->priv_data;
  74. const uint8_t *src = pic->data[0];
  75. int x, y;
  76. for (y = 0; y < avctx->height; y++) {
  77. for (x = 0; x < avctx->width; x++) {
  78. int value;
  79. if (s->big_endian) {
  80. value = ((AV_RB16(src + 6*x + 4) & 0xFFC0U) >> 4)
  81. | ((AV_RB16(src + 6*x + 2) & 0xFFC0U) << 6)
  82. | ((AV_RB16(src + 6*x + 0) & 0xFFC0U) << 16);
  83. } else {
  84. value = ((AV_RL16(src + 6*x + 4) & 0xFFC0U) >> 4)
  85. | ((AV_RL16(src + 6*x + 2) & 0xFFC0U) << 6)
  86. | ((AV_RL16(src + 6*x + 0) & 0xFFC0U) << 16);
  87. }
  88. write32(dst, value);
  89. dst += 4;
  90. }
  91. src += pic->linesize[0];
  92. }
  93. }
  94. static void encode_gbrp10(AVCodecContext *avctx, const AVPicture *pic, uint8_t *dst)
  95. {
  96. DPXContext *s = avctx->priv_data;
  97. const uint8_t *src[3] = {pic->data[0], pic->data[1], pic->data[2]};
  98. int x, y, i;
  99. for (y = 0; y < avctx->height; y++) {
  100. for (x = 0; x < avctx->width; x++) {
  101. int value;
  102. if (s->big_endian) {
  103. value = (AV_RB16(src[0] + 2*x) << 12)
  104. | (AV_RB16(src[1] + 2*x) << 2)
  105. | ((unsigned)AV_RB16(src[2] + 2*x) << 22);
  106. } else {
  107. value = (AV_RL16(src[0] + 2*x) << 12)
  108. | (AV_RL16(src[1] + 2*x) << 2)
  109. | ((unsigned)AV_RL16(src[2] + 2*x) << 22);
  110. }
  111. write32(dst, value);
  112. dst += 4;
  113. }
  114. for (i = 0; i < 3; i++)
  115. src[i] += pic->linesize[i];
  116. }
  117. }
  118. static void encode_gbrp12(AVCodecContext *avctx, const AVPicture *pic, uint16_t *dst)
  119. {
  120. DPXContext *s = avctx->priv_data;
  121. const uint16_t *src[3] = {(uint16_t*)pic->data[0],
  122. (uint16_t*)pic->data[1],
  123. (uint16_t*)pic->data[2]};
  124. int x, y, i;
  125. for (y = 0; y < avctx->height; y++) {
  126. for (x = 0; x < avctx->width; x++) {
  127. uint16_t value[3];
  128. if (s->big_endian) {
  129. value[1] = AV_RB16(src[0] + x) << 4;
  130. value[2] = AV_RB16(src[1] + x) << 4;
  131. value[0] = AV_RB16(src[2] + x) << 4;
  132. } else {
  133. value[1] = AV_RL16(src[0] + x) << 4;
  134. value[2] = AV_RL16(src[1] + x) << 4;
  135. value[0] = AV_RL16(src[2] + x) << 4;
  136. }
  137. for (i = 0; i < 3; i++)
  138. write16(dst++, value[i]);
  139. }
  140. for (i = 0; i < 3; i++)
  141. src[i] += pic->linesize[i]/2;
  142. }
  143. }
  144. static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  145. const AVFrame *frame, int *got_packet)
  146. {
  147. DPXContext *s = avctx->priv_data;
  148. int size, ret;
  149. uint8_t *buf;
  150. #define HEADER_SIZE 1664 /* DPX Generic header */
  151. if (s->bits_per_component == 10)
  152. size = avctx->height * avctx->width * 4;
  153. else
  154. size = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
  155. if ((ret = ff_alloc_packet2(avctx, pkt, size + HEADER_SIZE)) < 0)
  156. return ret;
  157. buf = pkt->data;
  158. memset(buf, 0, HEADER_SIZE);
  159. /* File information header */
  160. write32(buf, MKBETAG('S','D','P','X'));
  161. write32(buf + 4, HEADER_SIZE);
  162. memcpy (buf + 8, "V1.0", 4);
  163. write32(buf + 20, 1); /* new image */
  164. write32(buf + 24, HEADER_SIZE);
  165. if (!(avctx->flags & CODEC_FLAG_BITEXACT))
  166. memcpy (buf + 160, LIBAVCODEC_IDENT, FFMIN(sizeof(LIBAVCODEC_IDENT), 100));
  167. write32(buf + 660, 0xFFFFFFFF); /* unencrypted */
  168. /* Image information header */
  169. write16(buf + 768, 0); /* orientation; left to right, top to bottom */
  170. write16(buf + 770, 1); /* number of elements */
  171. write32(buf + 772, avctx->width);
  172. write32(buf + 776, avctx->height);
  173. buf[800] = s->descriptor;
  174. buf[801] = 2; /* linear transfer */
  175. buf[802] = 2; /* linear colorimetric */
  176. buf[803] = s->bits_per_component;
  177. write16(buf + 804, (s->bits_per_component == 10 || s->bits_per_component == 12) ?
  178. 1 : 0); /* packing method */
  179. write32(buf + 808, HEADER_SIZE); /* data offset */
  180. /* Image source information header */
  181. write32(buf + 1628, avctx->sample_aspect_ratio.num);
  182. write32(buf + 1632, avctx->sample_aspect_ratio.den);
  183. switch(s->bits_per_component) {
  184. case 8:
  185. case 16:
  186. size = avpicture_layout((const AVPicture*)frame, avctx->pix_fmt,
  187. avctx->width, avctx->height,
  188. buf + HEADER_SIZE, pkt->size - HEADER_SIZE);
  189. if (size < 0)
  190. return size;
  191. break;
  192. case 10:
  193. if (s->planar)
  194. encode_gbrp10(avctx, (const AVPicture*)frame, buf + HEADER_SIZE);
  195. else
  196. encode_rgb48_10bit(avctx, (const AVPicture*)frame, buf + HEADER_SIZE);
  197. break;
  198. case 12:
  199. encode_gbrp12(avctx, (const AVPicture*)frame, (uint16_t*)(buf + HEADER_SIZE));
  200. break;
  201. default:
  202. av_log(avctx, AV_LOG_ERROR, "Unsupported bit depth: %d\n", s->bits_per_component);
  203. return -1;
  204. }
  205. size += HEADER_SIZE;
  206. write32(buf + 16, size); /* file size */
  207. pkt->flags |= AV_PKT_FLAG_KEY;
  208. *got_packet = 1;
  209. return 0;
  210. }
  211. AVCodec ff_dpx_encoder = {
  212. .name = "dpx",
  213. .long_name = NULL_IF_CONFIG_SMALL("DPX (Digital Picture Exchange) image"),
  214. .type = AVMEDIA_TYPE_VIDEO,
  215. .id = AV_CODEC_ID_DPX,
  216. .priv_data_size = sizeof(DPXContext),
  217. .init = encode_init,
  218. .encode2 = encode_frame,
  219. .pix_fmts = (const enum AVPixelFormat[]){
  220. AV_PIX_FMT_RGB24, AV_PIX_FMT_RGBA,
  221. AV_PIX_FMT_RGB48LE, AV_PIX_FMT_RGB48BE,
  222. AV_PIX_FMT_RGBA64LE, AV_PIX_FMT_RGBA64BE,
  223. AV_PIX_FMT_GBRP10LE, AV_PIX_FMT_GBRP10BE,
  224. AV_PIX_FMT_GBRP12LE, AV_PIX_FMT_GBRP12BE,
  225. AV_PIX_FMT_NONE},
  226. };