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.

231 lines
6.4KB

  1. /*
  2. * DPX (.dpx) image decoder
  3. * Copyright (c) 2009 Jimmy Christensen
  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/intreadwrite.h"
  22. #include "libavcore/imgutils.h"
  23. #include "bytestream.h"
  24. #include "avcodec.h"
  25. typedef struct DPXContext {
  26. AVFrame picture;
  27. } DPXContext;
  28. static unsigned int read32(const uint8_t **ptr, int is_big)
  29. {
  30. unsigned int temp;
  31. if (is_big) {
  32. temp = AV_RB32(*ptr);
  33. } else {
  34. temp = AV_RL32(*ptr);
  35. }
  36. *ptr += 4;
  37. return temp;
  38. }
  39. static inline unsigned make_16bit(unsigned value)
  40. {
  41. // mask away invalid bits
  42. value &= 0xFFC0;
  43. // correctly expand to 16 bits
  44. return value + (value >> 10);
  45. }
  46. static int decode_frame(AVCodecContext *avctx,
  47. void *data,
  48. int *data_size,
  49. AVPacket *avpkt)
  50. {
  51. const uint8_t *buf = avpkt->data;
  52. int buf_size = avpkt->size;
  53. DPXContext *const s = avctx->priv_data;
  54. AVFrame *picture = data;
  55. AVFrame *const p = &s->picture;
  56. uint8_t *ptr;
  57. int magic_num, offset, endian;
  58. int x, y;
  59. int w, h, stride, bits_per_color, descriptor, elements, target_packet_size, source_packet_size;
  60. unsigned int rgbBuffer;
  61. magic_num = AV_RB32(buf);
  62. buf += 4;
  63. /* Check if the files "magic number" is "SDPX" which means it uses
  64. * big-endian or XPDS which is for little-endian files */
  65. if (magic_num == AV_RL32("SDPX")) {
  66. endian = 0;
  67. } else if (magic_num == AV_RB32("SDPX")) {
  68. endian = 1;
  69. } else {
  70. av_log(avctx, AV_LOG_ERROR, "DPX marker not found\n");
  71. return -1;
  72. }
  73. offset = read32(&buf, endian);
  74. // Need to end in 0x304 offset from start of file
  75. buf = avpkt->data + 0x304;
  76. w = read32(&buf, endian);
  77. h = read32(&buf, endian);
  78. // Need to end in 0x320 to read the descriptor
  79. buf += 20;
  80. descriptor = buf[0];
  81. // Need to end in 0x323 to read the bits per color
  82. buf += 3;
  83. avctx->bits_per_raw_sample =
  84. bits_per_color = buf[0];
  85. switch (descriptor) {
  86. case 51: // RGBA
  87. elements = 4;
  88. break;
  89. case 50: // RGB
  90. elements = 3;
  91. break;
  92. default:
  93. av_log(avctx, AV_LOG_ERROR, "Unsupported descriptor %d\n", descriptor);
  94. return -1;
  95. }
  96. switch (bits_per_color) {
  97. case 8:
  98. if (elements == 4) {
  99. avctx->pix_fmt = PIX_FMT_RGBA;
  100. } else {
  101. avctx->pix_fmt = PIX_FMT_RGB24;
  102. }
  103. source_packet_size = elements;
  104. target_packet_size = elements;
  105. break;
  106. case 10:
  107. avctx->pix_fmt = PIX_FMT_RGB48;
  108. target_packet_size = 6;
  109. source_packet_size = elements * 2;
  110. break;
  111. case 12:
  112. case 16:
  113. if (endian) {
  114. avctx->pix_fmt = PIX_FMT_RGB48BE;
  115. } else {
  116. avctx->pix_fmt = PIX_FMT_RGB48LE;
  117. }
  118. target_packet_size = 6;
  119. source_packet_size = elements * 2;
  120. break;
  121. default:
  122. av_log(avctx, AV_LOG_ERROR, "Unsupported color depth : %d\n", bits_per_color);
  123. return -1;
  124. }
  125. if (s->picture.data[0])
  126. avctx->release_buffer(avctx, &s->picture);
  127. if (av_image_check_size(w, h, 0, avctx))
  128. return -1;
  129. if (w != avctx->width || h != avctx->height)
  130. avcodec_set_dimensions(avctx, w, h);
  131. if (avctx->get_buffer(avctx, p) < 0) {
  132. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  133. return -1;
  134. }
  135. // Move pointer to offset from start of file
  136. buf = avpkt->data + offset;
  137. ptr = p->data[0];
  138. stride = p->linesize[0];
  139. switch (bits_per_color) {
  140. case 10:
  141. for (x = 0; x < avctx->height; x++) {
  142. uint16_t *dst = (uint16_t*)ptr;
  143. for (y = 0; y < avctx->width; y++) {
  144. rgbBuffer = read32(&buf, endian);
  145. // Read out the 10-bit colors and convert to 16-bit
  146. *dst++ = make_16bit(rgbBuffer >> 16);
  147. *dst++ = make_16bit(rgbBuffer >> 6);
  148. *dst++ = make_16bit(rgbBuffer << 4);
  149. }
  150. ptr += stride;
  151. }
  152. break;
  153. case 8:
  154. case 12: // Treat 12-bit as 16-bit
  155. case 16:
  156. if (source_packet_size == target_packet_size) {
  157. for (x = 0; x < avctx->height; x++) {
  158. memcpy(ptr, buf, target_packet_size*avctx->width);
  159. ptr += stride;
  160. buf += source_packet_size*avctx->width;
  161. }
  162. } else {
  163. for (x = 0; x < avctx->height; x++) {
  164. uint8_t *dst = ptr;
  165. for (y = 0; y < avctx->width; y++) {
  166. memcpy(dst, buf, target_packet_size);
  167. dst += target_packet_size;
  168. buf += source_packet_size;
  169. }
  170. ptr += stride;
  171. }
  172. }
  173. break;
  174. }
  175. *picture = s->picture;
  176. *data_size = sizeof(AVPicture);
  177. return buf_size;
  178. }
  179. static av_cold int decode_init(AVCodecContext *avctx)
  180. {
  181. DPXContext *s = avctx->priv_data;
  182. avcodec_get_frame_defaults(&s->picture);
  183. avctx->coded_frame = &s->picture;
  184. return 0;
  185. }
  186. static av_cold int decode_end(AVCodecContext *avctx)
  187. {
  188. DPXContext *s = avctx->priv_data;
  189. if (s->picture.data[0])
  190. avctx->release_buffer(avctx, &s->picture);
  191. return 0;
  192. }
  193. AVCodec dpx_decoder = {
  194. "dpx",
  195. AVMEDIA_TYPE_VIDEO,
  196. CODEC_ID_DPX,
  197. sizeof(DPXContext),
  198. decode_init,
  199. NULL,
  200. decode_end,
  201. decode_frame,
  202. 0,
  203. NULL,
  204. .long_name = NULL_IF_CONFIG_SMALL("DPX image"),
  205. };