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.

366 lines
10KB

  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/intfloat.h"
  23. #include "libavutil/imgutils.h"
  24. #include "bytestream.h"
  25. #include "avcodec.h"
  26. #include "internal.h"
  27. static unsigned int read16(const uint8_t **ptr, int is_big)
  28. {
  29. unsigned int temp;
  30. if (is_big) {
  31. temp = AV_RB16(*ptr);
  32. } else {
  33. temp = AV_RL16(*ptr);
  34. }
  35. *ptr += 2;
  36. return temp;
  37. }
  38. static unsigned int read32(const uint8_t **ptr, int is_big)
  39. {
  40. unsigned int temp;
  41. if (is_big) {
  42. temp = AV_RB32(*ptr);
  43. } else {
  44. temp = AV_RL32(*ptr);
  45. }
  46. *ptr += 4;
  47. return temp;
  48. }
  49. static uint16_t read10in32(const uint8_t **ptr, uint32_t * lbuf,
  50. int * n_datum, int is_big)
  51. {
  52. if (*n_datum)
  53. (*n_datum)--;
  54. else {
  55. *lbuf = read32(ptr, is_big);
  56. *n_datum = 2;
  57. }
  58. *lbuf = (*lbuf << 10) | (*lbuf >> 22);
  59. return *lbuf & 0x3FF;
  60. }
  61. static int decode_frame(AVCodecContext *avctx,
  62. void *data,
  63. int *got_frame,
  64. AVPacket *avpkt)
  65. {
  66. const uint8_t *buf = avpkt->data;
  67. int buf_size = avpkt->size;
  68. AVFrame *const p = data;
  69. uint8_t *ptr[AV_NUM_DATA_POINTERS];
  70. unsigned int offset;
  71. int magic_num, endian;
  72. int x, y, stride, i, ret;
  73. int w, h, bits_per_color, descriptor, elements, packing;
  74. int encoding, need_align = 0;
  75. unsigned int rgbBuffer = 0;
  76. int n_datum = 0;
  77. if (avpkt->size <= 1634) {
  78. av_log(avctx, AV_LOG_ERROR, "Packet too small for DPX header\n");
  79. return AVERROR_INVALIDDATA;
  80. }
  81. magic_num = AV_RB32(buf);
  82. buf += 4;
  83. /* Check if the files "magic number" is "SDPX" which means it uses
  84. * big-endian or XPDS which is for little-endian files */
  85. if (magic_num == AV_RL32("SDPX")) {
  86. endian = 0;
  87. } else if (magic_num == AV_RB32("SDPX")) {
  88. endian = 1;
  89. } else {
  90. av_log(avctx, AV_LOG_ERROR, "DPX marker not found\n");
  91. return AVERROR_INVALIDDATA;
  92. }
  93. offset = read32(&buf, endian);
  94. if (avpkt->size <= offset) {
  95. av_log(avctx, AV_LOG_ERROR, "Invalid data start offset\n");
  96. return AVERROR_INVALIDDATA;
  97. }
  98. // Check encryption
  99. buf = avpkt->data + 660;
  100. ret = read32(&buf, endian);
  101. if (ret != 0xFFFFFFFF) {
  102. avpriv_report_missing_feature(avctx, "Encryption");
  103. av_log(avctx, AV_LOG_WARNING, "The image is encrypted and may "
  104. "not properly decode.\n");
  105. }
  106. // Need to end in 0x304 offset from start of file
  107. buf = avpkt->data + 0x304;
  108. w = read32(&buf, endian);
  109. h = read32(&buf, endian);
  110. if ((ret = ff_set_dimensions(avctx, w, h)) < 0)
  111. return ret;
  112. // Need to end in 0x320 to read the descriptor
  113. buf += 20;
  114. descriptor = buf[0];
  115. // Need to end in 0x323 to read the bits per color
  116. buf += 3;
  117. avctx->bits_per_raw_sample =
  118. bits_per_color = buf[0];
  119. buf++;
  120. packing = read16(&buf, endian);
  121. encoding = read16(&buf, endian);
  122. if (packing > 1) {
  123. avpriv_report_missing_feature(avctx, "Packing %d", packing);
  124. return AVERROR_PATCHWELCOME;
  125. }
  126. if (encoding) {
  127. avpriv_report_missing_feature(avctx, "Encoding %d", encoding);
  128. return AVERROR_PATCHWELCOME;
  129. }
  130. buf += 820;
  131. avctx->sample_aspect_ratio.num = read32(&buf, endian);
  132. avctx->sample_aspect_ratio.den = read32(&buf, endian);
  133. if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0)
  134. av_reduce(&avctx->sample_aspect_ratio.num, &avctx->sample_aspect_ratio.den,
  135. avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den,
  136. 0x10000);
  137. else
  138. avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
  139. if (offset >= 1724 + 4) {
  140. buf = avpkt->data + 1724;
  141. i = read32(&buf, endian);
  142. if(i) {
  143. AVRational q = av_d2q(av_int2float(i), 4096);
  144. if (q.num > 0 && q.den > 0)
  145. avctx->time_base = av_inv_q(q);
  146. }
  147. }
  148. switch (descriptor) {
  149. case 6: // Y
  150. elements = 1;
  151. break;
  152. case 52: // ABGR
  153. case 51: // RGBA
  154. elements = 4;
  155. break;
  156. case 50: // RGB
  157. elements = 3;
  158. break;
  159. default:
  160. avpriv_report_missing_feature(avctx, "Descriptor %d", descriptor);
  161. return AVERROR_PATCHWELCOME;
  162. }
  163. switch (bits_per_color) {
  164. case 8:
  165. stride = avctx->width * elements;
  166. break;
  167. case 10:
  168. if (!packing) {
  169. av_log(avctx, AV_LOG_ERROR, "Packing to 32bit required\n");
  170. return -1;
  171. }
  172. stride = (avctx->width * elements + 2) / 3 * 4;
  173. break;
  174. case 12:
  175. if (!packing) {
  176. av_log(avctx, AV_LOG_ERROR, "Packing to 16bit required\n");
  177. return -1;
  178. }
  179. stride = 2 * avctx->width * elements;
  180. break;
  181. case 16:
  182. stride = 2 * avctx->width * elements;
  183. break;
  184. case 1:
  185. case 32:
  186. case 64:
  187. avpriv_report_missing_feature(avctx, "Depth %d", bits_per_color);
  188. return AVERROR_PATCHWELCOME;
  189. default:
  190. return AVERROR_INVALIDDATA;
  191. }
  192. // Table 3c: Runs will always break at scan line boundaries. Packing
  193. // will always break to the next 32-bit word at scan-line boundaries.
  194. // Unfortunately, the encoder produced invalid files, so attempt
  195. // to detect it
  196. need_align = FFALIGN(stride, 4);
  197. if (need_align*avctx->height + (int64_t)offset > avpkt->size) {
  198. // Alignment seems unappliable, try without
  199. if (stride*avctx->height + (int64_t)offset > avpkt->size) {
  200. av_log(avctx, AV_LOG_ERROR, "Overread buffer. Invalid header?\n");
  201. return AVERROR_INVALIDDATA;
  202. } else {
  203. av_log(avctx, AV_LOG_INFO, "Decoding DPX without scanline "
  204. "alignment.\n");
  205. need_align = 0;
  206. }
  207. } else {
  208. need_align -= stride;
  209. stride = FFALIGN(stride, 4);
  210. }
  211. switch (1000 * descriptor + 10 * bits_per_color + endian) {
  212. case 6081:
  213. case 6080:
  214. avctx->pix_fmt = AV_PIX_FMT_GRAY8;
  215. break;
  216. case 50081:
  217. case 50080:
  218. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  219. break;
  220. case 52081:
  221. case 52080:
  222. avctx->pix_fmt = AV_PIX_FMT_ABGR;
  223. break;
  224. case 51081:
  225. case 51080:
  226. avctx->pix_fmt = AV_PIX_FMT_RGBA;
  227. break;
  228. case 50100:
  229. case 51100:
  230. case 50101:
  231. case 51101:
  232. avctx->pix_fmt = AV_PIX_FMT_GBRP10;
  233. break;
  234. case 50120:
  235. case 51120:
  236. case 50121:
  237. case 51121:
  238. avctx->pix_fmt = AV_PIX_FMT_GBRP12;
  239. break;
  240. case 6161:
  241. avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
  242. break;
  243. case 6160:
  244. avctx->pix_fmt = AV_PIX_FMT_GRAY16LE;
  245. break;
  246. case 50161:
  247. avctx->pix_fmt = AV_PIX_FMT_RGB48BE;
  248. break;
  249. case 50160:
  250. avctx->pix_fmt = AV_PIX_FMT_RGB48LE;
  251. break;
  252. case 51161:
  253. avctx->pix_fmt = AV_PIX_FMT_RGBA64BE;
  254. break;
  255. case 51160:
  256. avctx->pix_fmt = AV_PIX_FMT_RGBA64LE;
  257. break;
  258. default:
  259. av_log(avctx, AV_LOG_ERROR, "Unsupported format\n");
  260. return AVERROR_PATCHWELCOME;
  261. }
  262. ff_set_sar(avctx, avctx->sample_aspect_ratio);
  263. if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
  264. return ret;
  265. // Move pointer to offset from start of file
  266. buf = avpkt->data + offset;
  267. for (i=0; i<AV_NUM_DATA_POINTERS; i++)
  268. ptr[i] = p->data[i];
  269. switch (bits_per_color) {
  270. case 10:
  271. for (x = 0; x < avctx->height; x++) {
  272. uint16_t *dst[3] = {(uint16_t*)ptr[0],
  273. (uint16_t*)ptr[1],
  274. (uint16_t*)ptr[2]};
  275. for (y = 0; y < avctx->width; y++) {
  276. *dst[2]++ = read10in32(&buf, &rgbBuffer,
  277. &n_datum, endian);
  278. *dst[0]++ = read10in32(&buf, &rgbBuffer,
  279. &n_datum, endian);
  280. *dst[1]++ = read10in32(&buf, &rgbBuffer,
  281. &n_datum, endian);
  282. // For 10 bit, ignore alpha
  283. if (elements == 4)
  284. read10in32(&buf, &rgbBuffer,
  285. &n_datum, endian);
  286. }
  287. n_datum = 0;
  288. for (i = 0; i < 3; i++)
  289. ptr[i] += p->linesize[i];
  290. }
  291. break;
  292. case 12:
  293. for (x = 0; x < avctx->height; x++) {
  294. uint16_t *dst[3] = {(uint16_t*)ptr[0],
  295. (uint16_t*)ptr[1],
  296. (uint16_t*)ptr[2]};
  297. for (y = 0; y < avctx->width; y++) {
  298. *dst[2] = read16(&buf, endian) >> 4;
  299. dst[2]++;
  300. *dst[0] = read16(&buf, endian) >> 4;
  301. dst[0]++;
  302. *dst[1] = read16(&buf, endian) >> 4;
  303. dst[1]++;
  304. // For 12 bit, ignore alpha
  305. if (elements == 4)
  306. buf += 2;
  307. // Jump to next aligned position
  308. buf += need_align;
  309. }
  310. for (i = 0; i < 3; i++)
  311. ptr[i] += p->linesize[i];
  312. }
  313. break;
  314. case 16:
  315. elements *= 2;
  316. case 8:
  317. av_image_copy_plane(ptr[0], p->linesize[0],
  318. buf, stride,
  319. elements * avctx->width, avctx->height);
  320. break;
  321. }
  322. *got_frame = 1;
  323. return buf_size;
  324. }
  325. AVCodec ff_dpx_decoder = {
  326. .name = "dpx",
  327. .long_name = NULL_IF_CONFIG_SMALL("DPX (Digital Picture Exchange) image"),
  328. .type = AVMEDIA_TYPE_VIDEO,
  329. .id = AV_CODEC_ID_DPX,
  330. .decode = decode_frame,
  331. .capabilities = CODEC_CAP_DR1,
  332. };