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.

220 lines
6.6KB

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