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.

236 lines
6.7KB

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