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.

229 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. bits_per_color = buf[0];
  83. switch (descriptor) {
  84. case 51: // RGBA
  85. elements = 4;
  86. break;
  87. case 50: // RGB
  88. elements = 3;
  89. break;
  90. default:
  91. av_log(avctx, AV_LOG_ERROR, "Unsupported descriptor %d\n", descriptor);
  92. return -1;
  93. }
  94. switch (bits_per_color) {
  95. case 8:
  96. if (elements == 4) {
  97. avctx->pix_fmt = PIX_FMT_RGBA;
  98. } else {
  99. avctx->pix_fmt = PIX_FMT_RGB24;
  100. }
  101. source_packet_size = elements;
  102. target_packet_size = elements;
  103. break;
  104. case 10:
  105. avctx->pix_fmt = PIX_FMT_RGB48;
  106. target_packet_size = 6;
  107. source_packet_size = elements * 2;
  108. break;
  109. case 12:
  110. case 16:
  111. if (endian) {
  112. avctx->pix_fmt = PIX_FMT_RGB48BE;
  113. } else {
  114. avctx->pix_fmt = PIX_FMT_RGB48LE;
  115. }
  116. target_packet_size = 6;
  117. source_packet_size = elements * 2;
  118. break;
  119. default:
  120. av_log(avctx, AV_LOG_ERROR, "Unsupported color depth : %d\n", bits_per_color);
  121. return -1;
  122. }
  123. if (s->picture.data[0])
  124. avctx->release_buffer(avctx, &s->picture);
  125. if (avcodec_check_dimensions(avctx, w, h))
  126. return -1;
  127. if (w != avctx->width || h != avctx->height)
  128. avcodec_set_dimensions(avctx, w, h);
  129. if (avctx->get_buffer(avctx, p) < 0) {
  130. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  131. return -1;
  132. }
  133. // Move pointer to offset from start of file
  134. buf = avpkt->data + offset;
  135. ptr = p->data[0];
  136. stride = p->linesize[0];
  137. switch (bits_per_color) {
  138. case 10:
  139. for (x = 0; x < avctx->height; x++) {
  140. uint16_t *dst = (uint16_t*)ptr;
  141. for (y = 0; y < avctx->width; y++) {
  142. rgbBuffer = read32(&buf, endian);
  143. // Read out the 10-bit colors and convert to 16-bit
  144. *dst++ = make_16bit(rgbBuffer >> 16);
  145. *dst++ = make_16bit(rgbBuffer >> 6);
  146. *dst++ = make_16bit(rgbBuffer << 4);
  147. }
  148. ptr += stride;
  149. }
  150. break;
  151. case 8:
  152. case 12: // Treat 12-bit as 16-bit
  153. case 16:
  154. if (source_packet_size == target_packet_size) {
  155. for (x = 0; x < avctx->height; x++) {
  156. memcpy(ptr, buf, target_packet_size*avctx->width);
  157. ptr += stride;
  158. buf += source_packet_size*avctx->width;
  159. }
  160. } else {
  161. for (x = 0; x < avctx->height; x++) {
  162. uint8_t *dst = ptr;
  163. for (y = 0; y < avctx->width; y++) {
  164. memcpy(dst, buf, target_packet_size);
  165. dst += target_packet_size;
  166. buf += source_packet_size;
  167. }
  168. ptr += stride;
  169. }
  170. }
  171. break;
  172. }
  173. *picture = s->picture;
  174. *data_size = sizeof(AVPicture);
  175. return buf_size;
  176. }
  177. static av_cold int decode_init(AVCodecContext *avctx)
  178. {
  179. DPXContext *s = avctx->priv_data;
  180. avcodec_get_frame_defaults(&s->picture);
  181. avctx->coded_frame = &s->picture;
  182. return 0;
  183. }
  184. static av_cold int decode_end(AVCodecContext *avctx)
  185. {
  186. DPXContext *s = avctx->priv_data;
  187. if (s->picture.data[0])
  188. avctx->release_buffer(avctx, &s->picture);
  189. return 0;
  190. }
  191. AVCodec dpx_decoder = {
  192. "dpx",
  193. CODEC_TYPE_VIDEO,
  194. CODEC_ID_DPX,
  195. sizeof(DPXContext),
  196. decode_init,
  197. NULL,
  198. decode_end,
  199. decode_frame,
  200. 0,
  201. NULL,
  202. .long_name = NULL_IF_CONFIG_SMALL("DPX image"),
  203. };