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.

254 lines
7.6KB

  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 "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. if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0)
  99. av_reduce(&avctx->sample_aspect_ratio.num, &avctx->sample_aspect_ratio.den,
  100. avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den,
  101. 0x10000);
  102. else
  103. avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
  104. switch (descriptor) {
  105. case 51: // RGBA
  106. elements = 4;
  107. break;
  108. case 50: // RGB
  109. elements = 3;
  110. break;
  111. default:
  112. av_log(avctx, AV_LOG_ERROR, "Unsupported descriptor %d\n", descriptor);
  113. return -1;
  114. }
  115. switch (bits_per_color) {
  116. case 8:
  117. if (elements == 4) {
  118. avctx->pix_fmt = PIX_FMT_RGBA;
  119. } else {
  120. avctx->pix_fmt = PIX_FMT_RGB24;
  121. }
  122. source_packet_size = elements;
  123. target_packet_size = elements;
  124. break;
  125. case 10:
  126. avctx->pix_fmt = PIX_FMT_RGB48;
  127. target_packet_size = 6;
  128. source_packet_size = 4;
  129. break;
  130. case 12:
  131. case 16:
  132. if (endian) {
  133. avctx->pix_fmt = elements == 4 ? PIX_FMT_RGBA64BE : PIX_FMT_RGB48BE;
  134. } else {
  135. avctx->pix_fmt = elements == 4 ? PIX_FMT_RGBA64LE : PIX_FMT_RGB48LE;
  136. }
  137. target_packet_size =
  138. source_packet_size = elements * 2;
  139. break;
  140. default:
  141. av_log(avctx, AV_LOG_ERROR, "Unsupported color depth : %d\n", bits_per_color);
  142. return -1;
  143. }
  144. if (s->picture.data[0])
  145. avctx->release_buffer(avctx, &s->picture);
  146. if (av_image_check_size(w, h, 0, avctx))
  147. return -1;
  148. if (w != avctx->width || h != avctx->height)
  149. avcodec_set_dimensions(avctx, w, h);
  150. if (avctx->get_buffer(avctx, p) < 0) {
  151. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  152. return -1;
  153. }
  154. // Move pointer to offset from start of file
  155. buf = avpkt->data + offset;
  156. ptr = p->data[0];
  157. stride = p->linesize[0];
  158. if (source_packet_size*avctx->width*avctx->height > buf_end - buf) {
  159. av_log(avctx, AV_LOG_ERROR, "Overread buffer. Invalid header?\n");
  160. return -1;
  161. }
  162. switch (bits_per_color) {
  163. case 10:
  164. for (x = 0; x < avctx->height; x++) {
  165. uint16_t *dst = (uint16_t*)ptr;
  166. for (y = 0; y < avctx->width; y++) {
  167. rgbBuffer = read32(&buf, endian);
  168. // Read out the 10-bit colors and convert to 16-bit
  169. *dst++ = make_16bit(rgbBuffer >> 16);
  170. *dst++ = make_16bit(rgbBuffer >> 6);
  171. *dst++ = make_16bit(rgbBuffer << 4);
  172. }
  173. ptr += stride;
  174. }
  175. break;
  176. case 8:
  177. case 12: // Treat 12-bit as 16-bit
  178. case 16:
  179. if (source_packet_size == target_packet_size) {
  180. for (x = 0; x < avctx->height; x++) {
  181. memcpy(ptr, buf, target_packet_size*avctx->width);
  182. ptr += stride;
  183. buf += source_packet_size*avctx->width;
  184. }
  185. } else {
  186. for (x = 0; x < avctx->height; x++) {
  187. uint8_t *dst = ptr;
  188. for (y = 0; y < avctx->width; y++) {
  189. memcpy(dst, buf, target_packet_size);
  190. dst += target_packet_size;
  191. buf += source_packet_size;
  192. }
  193. ptr += stride;
  194. }
  195. }
  196. break;
  197. }
  198. *picture = s->picture;
  199. *data_size = sizeof(AVPicture);
  200. return buf_size;
  201. }
  202. static av_cold int decode_init(AVCodecContext *avctx)
  203. {
  204. DPXContext *s = avctx->priv_data;
  205. avcodec_get_frame_defaults(&s->picture);
  206. avctx->coded_frame = &s->picture;
  207. return 0;
  208. }
  209. static av_cold int decode_end(AVCodecContext *avctx)
  210. {
  211. DPXContext *s = avctx->priv_data;
  212. if (s->picture.data[0])
  213. avctx->release_buffer(avctx, &s->picture);
  214. return 0;
  215. }
  216. AVCodec ff_dpx_decoder = {
  217. .name = "dpx",
  218. .type = AVMEDIA_TYPE_VIDEO,
  219. .id = AV_CODEC_ID_DPX,
  220. .priv_data_size = sizeof(DPXContext),
  221. .init = decode_init,
  222. .close = decode_end,
  223. .decode = decode_frame,
  224. .capabilities = CODEC_CAP_DR1,
  225. .long_name = NULL_IF_CONFIG_SMALL("DPX image"),
  226. };