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.

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