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.

274 lines
8.5KB

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