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.

297 lines
9.8KB

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