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.

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