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.

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