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.

279 lines
8.4KB

  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. #include "internal.h"
  26. static unsigned int read16(const uint8_t **ptr, int is_big)
  27. {
  28. unsigned int temp;
  29. if (is_big) {
  30. temp = AV_RB16(*ptr);
  31. } else {
  32. temp = AV_RL16(*ptr);
  33. }
  34. *ptr += 2;
  35. return temp;
  36. }
  37. static unsigned int read32(const uint8_t **ptr, int is_big)
  38. {
  39. unsigned int temp;
  40. if (is_big) {
  41. temp = AV_RB32(*ptr);
  42. } else {
  43. temp = AV_RL32(*ptr);
  44. }
  45. *ptr += 4;
  46. return temp;
  47. }
  48. static uint16_t read10in32(const uint8_t **ptr, uint32_t * lbuf,
  49. int * n_datum, int is_big)
  50. {
  51. if (*n_datum)
  52. (*n_datum)--;
  53. else {
  54. *lbuf = read32(ptr, is_big);
  55. *n_datum = 2;
  56. }
  57. *lbuf = (*lbuf << 10) | (*lbuf >> 22);
  58. return *lbuf & 0x3FF;
  59. }
  60. static int decode_frame(AVCodecContext *avctx,
  61. void *data,
  62. int *got_frame,
  63. AVPacket *avpkt)
  64. {
  65. const uint8_t *buf = avpkt->data;
  66. int buf_size = avpkt->size;
  67. AVFrame *const p = data;
  68. uint8_t *ptr[AV_NUM_DATA_POINTERS];
  69. unsigned int offset;
  70. int magic_num, endian;
  71. int x, y, i, ret;
  72. int w, h, bits_per_color, descriptor, elements, packing, total_size;
  73. int encoding;
  74. unsigned int rgbBuffer = 0;
  75. int n_datum = 0;
  76. if (avpkt->size <= 1634) {
  77. av_log(avctx, AV_LOG_ERROR, "Packet too small for DPX header\n");
  78. return AVERROR_INVALIDDATA;
  79. }
  80. magic_num = AV_RB32(buf);
  81. buf += 4;
  82. /* Check if the files "magic number" is "SDPX" which means it uses
  83. * big-endian or XPDS which is for little-endian files */
  84. if (magic_num == AV_RL32("SDPX")) {
  85. endian = 0;
  86. } else if (magic_num == AV_RB32("SDPX")) {
  87. endian = 1;
  88. } else {
  89. av_log(avctx, AV_LOG_ERROR, "DPX marker not found\n");
  90. return AVERROR_INVALIDDATA;
  91. }
  92. offset = read32(&buf, endian);
  93. if (avpkt->size <= offset) {
  94. av_log(avctx, AV_LOG_ERROR, "Invalid data start offset\n");
  95. return AVERROR_INVALIDDATA;
  96. }
  97. // Need to end in 0x304 offset from start of file
  98. buf = avpkt->data + 0x304;
  99. w = read32(&buf, endian);
  100. h = read32(&buf, endian);
  101. if ((ret = av_image_check_size(w, h, 0, avctx)) < 0)
  102. return ret;
  103. if (w != avctx->width || h != avctx->height)
  104. avcodec_set_dimensions(avctx, w, h);
  105. // Need to end in 0x320 to read the descriptor
  106. buf += 20;
  107. descriptor = buf[0];
  108. // Need to end in 0x323 to read the bits per color
  109. buf += 3;
  110. avctx->bits_per_raw_sample =
  111. bits_per_color = buf[0];
  112. buf++;
  113. packing = read16(&buf, endian);
  114. encoding = read16(&buf, endian);
  115. if (packing > 1) {
  116. avpriv_report_missing_feature(avctx, "Packing %d", packing);
  117. return AVERROR_PATCHWELCOME;
  118. }
  119. if (encoding) {
  120. avpriv_report_missing_feature(avctx, "Encoding %d", encoding);
  121. return AVERROR_PATCHWELCOME;
  122. }
  123. buf += 820;
  124. avctx->sample_aspect_ratio.num = read32(&buf, endian);
  125. avctx->sample_aspect_ratio.den = read32(&buf, endian);
  126. if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0)
  127. av_reduce(&avctx->sample_aspect_ratio.num, &avctx->sample_aspect_ratio.den,
  128. avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den,
  129. 0x10000);
  130. else
  131. avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
  132. switch (descriptor) {
  133. case 51: // RGBA
  134. elements = 4;
  135. break;
  136. case 50: // RGB
  137. elements = 3;
  138. break;
  139. default:
  140. avpriv_report_missing_feature(avctx, "Descriptor %d", descriptor);
  141. return AVERROR_PATCHWELCOME;
  142. }
  143. switch (bits_per_color) {
  144. case 8:
  145. if (elements == 4) {
  146. avctx->pix_fmt = AV_PIX_FMT_RGBA;
  147. } else {
  148. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  149. }
  150. total_size = avctx->width * avctx->height * elements;
  151. break;
  152. case 10:
  153. if (!packing) {
  154. av_log(avctx, AV_LOG_ERROR, "Packing to 32bit required\n");
  155. return -1;
  156. }
  157. avctx->pix_fmt = AV_PIX_FMT_GBRP10;
  158. total_size = (avctx->width * elements + 2) / 3 * 4 * avctx->height;
  159. break;
  160. case 12:
  161. if (!packing) {
  162. av_log(avctx, AV_LOG_ERROR, "Packing to 16bit required\n");
  163. return -1;
  164. }
  165. avctx->pix_fmt = AV_PIX_FMT_GBRP12;
  166. total_size = 2 * avctx->width * avctx->height * elements;
  167. break;
  168. case 16:
  169. if (endian) {
  170. avctx->pix_fmt = elements == 4 ? AV_PIX_FMT_RGBA64BE : AV_PIX_FMT_RGB48BE;
  171. } else {
  172. avctx->pix_fmt = elements == 4 ? AV_PIX_FMT_RGBA64LE : AV_PIX_FMT_RGB48LE;
  173. }
  174. total_size = 2 * avctx->width * avctx->height * elements;
  175. break;
  176. default:
  177. av_log(avctx, AV_LOG_ERROR, "Unsupported color depth : %d\n", bits_per_color);
  178. return AVERROR_INVALIDDATA;
  179. }
  180. if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
  181. return ret;
  182. // Move pointer to offset from start of file
  183. buf = avpkt->data + offset;
  184. for (i=0; i<AV_NUM_DATA_POINTERS; i++)
  185. ptr[i] = p->data[i];
  186. if (total_size + (int64_t)offset > avpkt->size) {
  187. av_log(avctx, AV_LOG_ERROR, "Overread buffer. Invalid header?\n");
  188. return AVERROR_INVALIDDATA;
  189. }
  190. switch (bits_per_color) {
  191. case 10:
  192. for (x = 0; x < avctx->height; x++) {
  193. uint16_t *dst[3] = {(uint16_t*)ptr[0],
  194. (uint16_t*)ptr[1],
  195. (uint16_t*)ptr[2]};
  196. for (y = 0; y < avctx->width; y++) {
  197. *dst[2]++ = read10in32(&buf, &rgbBuffer,
  198. &n_datum, endian);
  199. *dst[0]++ = read10in32(&buf, &rgbBuffer,
  200. &n_datum, endian);
  201. *dst[1]++ = read10in32(&buf, &rgbBuffer,
  202. &n_datum, endian);
  203. // For 10 bit, ignore alpha
  204. if (elements == 4)
  205. read10in32(&buf, &rgbBuffer,
  206. &n_datum, endian);
  207. }
  208. n_datum = 0;
  209. for (i = 0; i < 3; i++)
  210. ptr[i] += p->linesize[i];
  211. }
  212. break;
  213. case 12:
  214. for (x = 0; x < avctx->height; x++) {
  215. uint16_t *dst[3] = {(uint16_t*)ptr[0],
  216. (uint16_t*)ptr[1],
  217. (uint16_t*)ptr[2]};
  218. for (y = 0; y < avctx->width; y++) {
  219. *dst[2] = read16(&buf, endian) >> 4;
  220. dst[2]++;
  221. *dst[0] = read16(&buf, endian) >> 4;
  222. dst[0]++;
  223. *dst[1] = read16(&buf, endian) >> 4;
  224. dst[1]++;
  225. // For 12 bit, ignore alpha
  226. if (elements == 4)
  227. buf += 2;
  228. }
  229. for (i = 0; i < 3; i++)
  230. ptr[i] += p->linesize[i];
  231. }
  232. break;
  233. case 16:
  234. elements *= 2;
  235. case 8:
  236. av_image_copy_plane(ptr[0], p->linesize[0],
  237. buf, elements * avctx->width,
  238. elements * avctx->width, avctx->height);
  239. break;
  240. }
  241. *got_frame = 1;
  242. return buf_size;
  243. }
  244. AVCodec ff_dpx_decoder = {
  245. .name = "dpx",
  246. .long_name = NULL_IF_CONFIG_SMALL("DPX (Digital Picture Exchange) image"),
  247. .type = AVMEDIA_TYPE_VIDEO,
  248. .id = AV_CODEC_ID_DPX,
  249. .decode = decode_frame,
  250. .capabilities = CODEC_CAP_DR1,
  251. };