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.

249 lines
7.3KB

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