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.

325 lines
9.2KB

  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 = ff_set_dimensions(avctx, w, h)) < 0)
  102. return ret;
  103. // Need to end in 0x320 to read the descriptor
  104. buf += 20;
  105. descriptor = buf[0];
  106. // Need to end in 0x323 to read the bits per color
  107. buf += 3;
  108. avctx->bits_per_raw_sample =
  109. bits_per_color = buf[0];
  110. buf++;
  111. packing = read16(&buf, endian);
  112. encoding = read16(&buf, endian);
  113. if (packing > 1) {
  114. avpriv_report_missing_feature(avctx, "Packing %d", packing);
  115. return AVERROR_PATCHWELCOME;
  116. }
  117. if (encoding) {
  118. avpriv_report_missing_feature(avctx, "Encoding %d", encoding);
  119. return AVERROR_PATCHWELCOME;
  120. }
  121. buf += 820;
  122. avctx->sample_aspect_ratio.num = read32(&buf, endian);
  123. avctx->sample_aspect_ratio.den = read32(&buf, endian);
  124. if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0)
  125. av_reduce(&avctx->sample_aspect_ratio.num, &avctx->sample_aspect_ratio.den,
  126. avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den,
  127. 0x10000);
  128. else
  129. avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
  130. switch (descriptor) {
  131. case 6: // Y
  132. elements = 1;
  133. break;
  134. case 52: // ABGR
  135. case 51: // RGBA
  136. elements = 4;
  137. break;
  138. case 50: // RGB
  139. elements = 3;
  140. break;
  141. default:
  142. avpriv_report_missing_feature(avctx, "Descriptor %d", descriptor);
  143. return AVERROR_PATCHWELCOME;
  144. }
  145. switch (bits_per_color) {
  146. case 8:
  147. total_size = avctx->width * avctx->height * elements;
  148. break;
  149. case 10:
  150. if (!packing) {
  151. av_log(avctx, AV_LOG_ERROR, "Packing to 32bit required\n");
  152. return -1;
  153. }
  154. total_size = (avctx->width * elements + 2) / 3 * 4 * avctx->height;
  155. break;
  156. case 12:
  157. if (!packing) {
  158. av_log(avctx, AV_LOG_ERROR, "Packing to 16bit required\n");
  159. return -1;
  160. }
  161. total_size = 2 * avctx->width * avctx->height * elements;
  162. break;
  163. case 16:
  164. total_size = 2 * avctx->width * avctx->height * elements;
  165. break;
  166. case 1:
  167. case 32:
  168. case 64:
  169. avpriv_report_missing_feature(avctx, "Depth %d", bits_per_color);
  170. return AVERROR_PATCHWELCOME;
  171. default:
  172. return AVERROR_INVALIDDATA;
  173. }
  174. switch (1000 * descriptor + 10 * bits_per_color + endian) {
  175. case 6081:
  176. case 6080:
  177. avctx->pix_fmt = AV_PIX_FMT_GRAY8;
  178. break;
  179. case 50081:
  180. case 50080:
  181. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  182. break;
  183. case 52081:
  184. case 52080:
  185. avctx->pix_fmt = AV_PIX_FMT_ABGR;
  186. break;
  187. case 51081:
  188. case 51080:
  189. avctx->pix_fmt = AV_PIX_FMT_RGBA;
  190. break;
  191. case 50100:
  192. case 51100:
  193. case 50101:
  194. case 51101:
  195. avctx->pix_fmt = AV_PIX_FMT_GBRP10;
  196. break;
  197. case 50120:
  198. case 51120:
  199. case 50121:
  200. case 51121:
  201. avctx->pix_fmt = AV_PIX_FMT_GBRP12;
  202. break;
  203. case 6161:
  204. avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
  205. break;
  206. case 6160:
  207. avctx->pix_fmt = AV_PIX_FMT_GRAY16LE;
  208. break;
  209. case 50161:
  210. avctx->pix_fmt = AV_PIX_FMT_RGB48BE;
  211. break;
  212. case 50160:
  213. avctx->pix_fmt = AV_PIX_FMT_RGB48LE;
  214. break;
  215. case 51161:
  216. avctx->pix_fmt = AV_PIX_FMT_RGBA64BE;
  217. break;
  218. case 51160:
  219. avctx->pix_fmt = AV_PIX_FMT_RGBA64LE;
  220. break;
  221. default:
  222. av_log(avctx, AV_LOG_ERROR, "Unsupported format\n");
  223. return AVERROR_PATCHWELCOME;
  224. }
  225. if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
  226. return ret;
  227. // Move pointer to offset from start of file
  228. buf = avpkt->data + offset;
  229. for (i=0; i<AV_NUM_DATA_POINTERS; i++)
  230. ptr[i] = p->data[i];
  231. if (total_size + (int64_t)offset > avpkt->size) {
  232. av_log(avctx, AV_LOG_ERROR, "Overread buffer. Invalid header?\n");
  233. return AVERROR_INVALIDDATA;
  234. }
  235. switch (bits_per_color) {
  236. case 10:
  237. for (x = 0; x < avctx->height; x++) {
  238. uint16_t *dst[3] = {(uint16_t*)ptr[0],
  239. (uint16_t*)ptr[1],
  240. (uint16_t*)ptr[2]};
  241. for (y = 0; y < avctx->width; y++) {
  242. *dst[2]++ = read10in32(&buf, &rgbBuffer,
  243. &n_datum, endian);
  244. *dst[0]++ = read10in32(&buf, &rgbBuffer,
  245. &n_datum, endian);
  246. *dst[1]++ = read10in32(&buf, &rgbBuffer,
  247. &n_datum, endian);
  248. // For 10 bit, ignore alpha
  249. if (elements == 4)
  250. read10in32(&buf, &rgbBuffer,
  251. &n_datum, endian);
  252. }
  253. n_datum = 0;
  254. for (i = 0; i < 3; i++)
  255. ptr[i] += p->linesize[i];
  256. }
  257. break;
  258. case 12:
  259. for (x = 0; x < avctx->height; x++) {
  260. uint16_t *dst[3] = {(uint16_t*)ptr[0],
  261. (uint16_t*)ptr[1],
  262. (uint16_t*)ptr[2]};
  263. for (y = 0; y < avctx->width; y++) {
  264. *dst[2] = read16(&buf, endian) >> 4;
  265. dst[2]++;
  266. *dst[0] = read16(&buf, endian) >> 4;
  267. dst[0]++;
  268. *dst[1] = read16(&buf, endian) >> 4;
  269. dst[1]++;
  270. // For 12 bit, ignore alpha
  271. if (elements == 4)
  272. buf += 2;
  273. }
  274. for (i = 0; i < 3; i++)
  275. ptr[i] += p->linesize[i];
  276. }
  277. break;
  278. case 16:
  279. elements *= 2;
  280. case 8:
  281. av_image_copy_plane(ptr[0], p->linesize[0],
  282. buf, elements * avctx->width,
  283. elements * avctx->width, avctx->height);
  284. break;
  285. }
  286. *got_frame = 1;
  287. return buf_size;
  288. }
  289. AVCodec ff_dpx_decoder = {
  290. .name = "dpx",
  291. .long_name = NULL_IF_CONFIG_SMALL("DPX (Digital Picture Exchange) image"),
  292. .type = AVMEDIA_TYPE_VIDEO,
  293. .id = AV_CODEC_ID_DPX,
  294. .decode = decode_frame,
  295. .capabilities = CODEC_CAP_DR1,
  296. };