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.

248 lines
7.2KB

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