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.

397 lines
12KB

  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->framerate = q;
  146. }
  147. }
  148. switch (descriptor) {
  149. case 6: // Y
  150. elements = 1;
  151. break;
  152. case 52: // ABGR
  153. case 51: // RGBA
  154. case 103: // UYVA4444
  155. elements = 4;
  156. break;
  157. case 50: // RGB
  158. case 102: // UYV444
  159. elements = 3;
  160. break;
  161. case 100: // UYVY422
  162. elements = 2;
  163. break;
  164. default:
  165. avpriv_report_missing_feature(avctx, "Descriptor %d", descriptor);
  166. return AVERROR_PATCHWELCOME;
  167. }
  168. switch (bits_per_color) {
  169. case 8:
  170. stride = avctx->width * elements;
  171. break;
  172. case 10:
  173. if (!packing) {
  174. av_log(avctx, AV_LOG_ERROR, "Packing to 32bit required\n");
  175. return -1;
  176. }
  177. stride = (avctx->width * elements + 2) / 3 * 4;
  178. break;
  179. case 12:
  180. if (!packing) {
  181. av_log(avctx, AV_LOG_ERROR, "Packing to 16bit required\n");
  182. return -1;
  183. }
  184. stride = 2 * avctx->width * elements;
  185. break;
  186. case 16:
  187. stride = 2 * avctx->width * elements;
  188. break;
  189. case 1:
  190. case 32:
  191. case 64:
  192. avpriv_report_missing_feature(avctx, "Depth %d", bits_per_color);
  193. return AVERROR_PATCHWELCOME;
  194. default:
  195. return AVERROR_INVALIDDATA;
  196. }
  197. // Table 3c: Runs will always break at scan line boundaries. Packing
  198. // will always break to the next 32-bit word at scan-line boundaries.
  199. // Unfortunately, the encoder produced invalid files, so attempt
  200. // to detect it
  201. need_align = FFALIGN(stride, 4);
  202. if (need_align*avctx->height + (int64_t)offset > avpkt->size) {
  203. // Alignment seems unappliable, try without
  204. if (stride*avctx->height + (int64_t)offset > avpkt->size) {
  205. av_log(avctx, AV_LOG_ERROR, "Overread buffer. Invalid header?\n");
  206. return AVERROR_INVALIDDATA;
  207. } else {
  208. av_log(avctx, AV_LOG_INFO, "Decoding DPX without scanline "
  209. "alignment.\n");
  210. need_align = 0;
  211. }
  212. } else {
  213. need_align -= stride;
  214. stride = FFALIGN(stride, 4);
  215. }
  216. switch (1000 * descriptor + 10 * bits_per_color + endian) {
  217. case 6081:
  218. case 6080:
  219. avctx->pix_fmt = AV_PIX_FMT_GRAY8;
  220. break;
  221. case 50081:
  222. case 50080:
  223. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  224. break;
  225. case 52081:
  226. case 52080:
  227. avctx->pix_fmt = AV_PIX_FMT_ABGR;
  228. break;
  229. case 51081:
  230. case 51080:
  231. avctx->pix_fmt = AV_PIX_FMT_RGBA;
  232. break;
  233. case 50100:
  234. case 51100:
  235. case 50101:
  236. case 51101:
  237. avctx->pix_fmt = AV_PIX_FMT_GBRP10;
  238. break;
  239. case 50120:
  240. case 51120:
  241. case 50121:
  242. case 51121:
  243. avctx->pix_fmt = AV_PIX_FMT_GBRP12;
  244. break;
  245. case 6161:
  246. avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
  247. break;
  248. case 6160:
  249. avctx->pix_fmt = AV_PIX_FMT_GRAY16LE;
  250. break;
  251. case 50161:
  252. avctx->pix_fmt = AV_PIX_FMT_RGB48BE;
  253. break;
  254. case 50160:
  255. avctx->pix_fmt = AV_PIX_FMT_RGB48LE;
  256. break;
  257. case 51161:
  258. avctx->pix_fmt = AV_PIX_FMT_RGBA64BE;
  259. break;
  260. case 51160:
  261. avctx->pix_fmt = AV_PIX_FMT_RGBA64LE;
  262. break;
  263. case 100081:
  264. avctx->pix_fmt = AV_PIX_FMT_UYVY422;
  265. break;
  266. case 102081:
  267. avctx->pix_fmt = AV_PIX_FMT_YUV444P;
  268. break;
  269. case 103081:
  270. avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
  271. break;
  272. default:
  273. av_log(avctx, AV_LOG_ERROR, "Unsupported format\n");
  274. return AVERROR_PATCHWELCOME;
  275. }
  276. ff_set_sar(avctx, avctx->sample_aspect_ratio);
  277. if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
  278. return ret;
  279. // Move pointer to offset from start of file
  280. buf = avpkt->data + offset;
  281. for (i=0; i<AV_NUM_DATA_POINTERS; i++)
  282. ptr[i] = p->data[i];
  283. switch (bits_per_color) {
  284. case 10:
  285. for (x = 0; x < avctx->height; x++) {
  286. uint16_t *dst[3] = {(uint16_t*)ptr[0],
  287. (uint16_t*)ptr[1],
  288. (uint16_t*)ptr[2]};
  289. for (y = 0; y < avctx->width; y++) {
  290. *dst[2]++ = read10in32(&buf, &rgbBuffer,
  291. &n_datum, endian);
  292. *dst[0]++ = read10in32(&buf, &rgbBuffer,
  293. &n_datum, endian);
  294. *dst[1]++ = read10in32(&buf, &rgbBuffer,
  295. &n_datum, endian);
  296. // For 10 bit, ignore alpha
  297. if (elements == 4)
  298. read10in32(&buf, &rgbBuffer,
  299. &n_datum, endian);
  300. }
  301. n_datum = 0;
  302. for (i = 0; i < 3; i++)
  303. ptr[i] += p->linesize[i];
  304. }
  305. break;
  306. case 12:
  307. for (x = 0; x < avctx->height; x++) {
  308. uint16_t *dst[3] = {(uint16_t*)ptr[0],
  309. (uint16_t*)ptr[1],
  310. (uint16_t*)ptr[2]};
  311. for (y = 0; y < avctx->width; y++) {
  312. *dst[2] = read16(&buf, endian) >> 4;
  313. dst[2]++;
  314. *dst[0] = read16(&buf, endian) >> 4;
  315. dst[0]++;
  316. *dst[1] = read16(&buf, endian) >> 4;
  317. dst[1]++;
  318. // For 12 bit, ignore alpha
  319. if (elements == 4)
  320. buf += 2;
  321. // Jump to next aligned position
  322. buf += need_align;
  323. }
  324. for (i = 0; i < 3; i++)
  325. ptr[i] += p->linesize[i];
  326. }
  327. break;
  328. case 16:
  329. elements *= 2;
  330. case 8:
  331. if ( avctx->pix_fmt == AV_PIX_FMT_YUVA444P
  332. || avctx->pix_fmt == AV_PIX_FMT_YUV444P) {
  333. for (x = 0; x < avctx->height; x++) {
  334. ptr[0] = p->data[0] + x * p->linesize[0];
  335. ptr[1] = p->data[1] + x * p->linesize[1];
  336. ptr[2] = p->data[2] + x * p->linesize[2];
  337. ptr[3] = p->data[3] + x * p->linesize[3];
  338. for (y = 0; y < avctx->width; y++) {
  339. *ptr[1]++ = *buf++;
  340. *ptr[0]++ = *buf++;
  341. *ptr[2]++ = *buf++;
  342. if (avctx->pix_fmt == AV_PIX_FMT_YUVA444P)
  343. *ptr[3]++ = *buf++;
  344. }
  345. }
  346. } else {
  347. av_image_copy_plane(ptr[0], p->linesize[0],
  348. buf, stride,
  349. elements * avctx->width, avctx->height);
  350. }
  351. break;
  352. }
  353. *got_frame = 1;
  354. return buf_size;
  355. }
  356. AVCodec ff_dpx_decoder = {
  357. .name = "dpx",
  358. .long_name = NULL_IF_CONFIG_SMALL("DPX (Digital Picture Exchange) image"),
  359. .type = AVMEDIA_TYPE_VIDEO,
  360. .id = AV_CODEC_ID_DPX,
  361. .decode = decode_frame,
  362. .capabilities = AV_CODEC_CAP_DR1,
  363. };