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.

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