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.

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