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.

478 lines
14KB

  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/avstring.h"
  22. #include "libavutil/intreadwrite.h"
  23. #include "libavutil/intfloat.h"
  24. #include "libavutil/imgutils.h"
  25. #include "bytestream.h"
  26. #include "avcodec.h"
  27. #include "internal.h"
  28. static unsigned int read16(const uint8_t **ptr, int is_big)
  29. {
  30. unsigned int temp;
  31. if (is_big) {
  32. temp = AV_RB16(*ptr);
  33. } else {
  34. temp = AV_RL16(*ptr);
  35. }
  36. *ptr += 2;
  37. return temp;
  38. }
  39. static unsigned int read32(const uint8_t **ptr, int is_big)
  40. {
  41. unsigned int temp;
  42. if (is_big) {
  43. temp = AV_RB32(*ptr);
  44. } else {
  45. temp = AV_RL32(*ptr);
  46. }
  47. *ptr += 4;
  48. return temp;
  49. }
  50. static uint16_t read10in32(const uint8_t **ptr, uint32_t * lbuf,
  51. int * n_datum, int is_big, int shift)
  52. {
  53. if (*n_datum)
  54. (*n_datum)--;
  55. else {
  56. *lbuf = read32(ptr, is_big);
  57. *n_datum = 2;
  58. }
  59. *lbuf = *lbuf << 10 | *lbuf >> shift & 0x3FFFFF;
  60. return *lbuf & 0x3FF;
  61. }
  62. static uint16_t read12in32(const uint8_t **ptr, uint32_t * lbuf,
  63. int * n_datum, int is_big)
  64. {
  65. if (*n_datum)
  66. (*n_datum)--;
  67. else {
  68. *lbuf = read32(ptr, is_big);
  69. *n_datum = 7;
  70. }
  71. switch (*n_datum){
  72. case 7: return *lbuf & 0xFFF;
  73. case 6: return (*lbuf >> 12) & 0xFFF;
  74. case 5: {
  75. uint32_t c = *lbuf >> 24;
  76. *lbuf = read32(ptr, is_big);
  77. c |= *lbuf << 8;
  78. return c & 0xFFF;
  79. }
  80. case 4: return (*lbuf >> 4) & 0xFFF;
  81. case 3: return (*lbuf >> 16) & 0xFFF;
  82. case 2: {
  83. uint32_t c = *lbuf >> 28;
  84. *lbuf = read32(ptr, is_big);
  85. c |= *lbuf << 4;
  86. return c & 0xFFF;
  87. }
  88. case 1: return (*lbuf >> 8) & 0xFFF;
  89. default: return *lbuf >> 20;
  90. }
  91. }
  92. static int decode_frame(AVCodecContext *avctx,
  93. void *data,
  94. int *got_frame,
  95. AVPacket *avpkt)
  96. {
  97. const uint8_t *buf = avpkt->data;
  98. int buf_size = avpkt->size;
  99. AVFrame *const p = data;
  100. uint8_t *ptr[AV_NUM_DATA_POINTERS];
  101. uint32_t header_version, version = 0;
  102. char creator[101];
  103. unsigned int offset;
  104. int magic_num, endian;
  105. int x, y, stride, i, ret;
  106. int w, h, bits_per_color, descriptor, elements, packing;
  107. int encoding, need_align = 0;
  108. unsigned int rgbBuffer = 0;
  109. int n_datum = 0;
  110. if (avpkt->size <= 1634) {
  111. av_log(avctx, AV_LOG_ERROR, "Packet too small for DPX header\n");
  112. return AVERROR_INVALIDDATA;
  113. }
  114. magic_num = AV_RB32(buf);
  115. buf += 4;
  116. /* Check if the files "magic number" is "SDPX" which means it uses
  117. * big-endian or XPDS which is for little-endian files */
  118. if (magic_num == AV_RL32("SDPX")) {
  119. endian = 0;
  120. } else if (magic_num == AV_RB32("SDPX")) {
  121. endian = 1;
  122. } else {
  123. av_log(avctx, AV_LOG_ERROR, "DPX marker not found\n");
  124. return AVERROR_INVALIDDATA;
  125. }
  126. offset = read32(&buf, endian);
  127. if (avpkt->size <= offset) {
  128. av_log(avctx, AV_LOG_ERROR, "Invalid data start offset\n");
  129. return AVERROR_INVALIDDATA;
  130. }
  131. header_version = read32(&buf, 0);
  132. if (header_version == MKTAG('V','1','.','0'))
  133. version = 1;
  134. if (header_version == MKTAG('V','2','.','0'))
  135. version = 2;
  136. if (!version)
  137. av_log(avctx, AV_LOG_WARNING, "Unknown header format version %s.\n",
  138. av_fourcc2str(header_version));
  139. // Check encryption
  140. buf = avpkt->data + 660;
  141. ret = read32(&buf, endian);
  142. if (ret != 0xFFFFFFFF) {
  143. avpriv_report_missing_feature(avctx, "Encryption");
  144. av_log(avctx, AV_LOG_WARNING, "The image is encrypted and may "
  145. "not properly decode.\n");
  146. }
  147. // Need to end in 0x304 offset from start of file
  148. buf = avpkt->data + 0x304;
  149. w = read32(&buf, endian);
  150. h = read32(&buf, endian);
  151. if ((ret = ff_set_dimensions(avctx, w, h)) < 0)
  152. return ret;
  153. // Need to end in 0x320 to read the descriptor
  154. buf += 20;
  155. descriptor = buf[0];
  156. // Need to end in 0x323 to read the bits per color
  157. buf += 3;
  158. avctx->bits_per_raw_sample =
  159. bits_per_color = buf[0];
  160. buf++;
  161. packing = read16(&buf, endian);
  162. encoding = read16(&buf, endian);
  163. if (encoding) {
  164. avpriv_report_missing_feature(avctx, "Encoding %d", encoding);
  165. return AVERROR_PATCHWELCOME;
  166. }
  167. buf += 820;
  168. avctx->sample_aspect_ratio.num = read32(&buf, endian);
  169. avctx->sample_aspect_ratio.den = read32(&buf, endian);
  170. if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0)
  171. av_reduce(&avctx->sample_aspect_ratio.num, &avctx->sample_aspect_ratio.den,
  172. avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den,
  173. 0x10000);
  174. else
  175. avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
  176. if (offset >= 1724 + 4) {
  177. buf = avpkt->data + 1724;
  178. i = read32(&buf, endian);
  179. if(i) {
  180. AVRational q = av_d2q(av_int2float(i), 4096);
  181. if (q.num > 0 && q.den > 0)
  182. avctx->framerate = q;
  183. }
  184. }
  185. switch (descriptor) {
  186. case 6: // Y
  187. elements = 1;
  188. break;
  189. case 52: // ABGR
  190. case 51: // RGBA
  191. case 103: // UYVA4444
  192. elements = 4;
  193. break;
  194. case 50: // RGB
  195. case 102: // UYV444
  196. elements = 3;
  197. break;
  198. case 100: // UYVY422
  199. elements = 2;
  200. break;
  201. default:
  202. avpriv_report_missing_feature(avctx, "Descriptor %d", descriptor);
  203. return AVERROR_PATCHWELCOME;
  204. }
  205. switch (bits_per_color) {
  206. case 8:
  207. stride = avctx->width * elements;
  208. break;
  209. case 10:
  210. if (!packing) {
  211. av_log(avctx, AV_LOG_ERROR, "Packing to 32bit required\n");
  212. return -1;
  213. }
  214. stride = (avctx->width * elements + 2) / 3 * 4;
  215. break;
  216. case 12:
  217. stride = avctx->width * elements;
  218. if (packing) {
  219. stride *= 2;
  220. } else {
  221. stride *= 3;
  222. if (stride % 8) {
  223. stride /= 8;
  224. stride++;
  225. stride *= 8;
  226. }
  227. stride /= 2;
  228. }
  229. break;
  230. case 16:
  231. stride = 2 * avctx->width * elements;
  232. break;
  233. case 1:
  234. case 32:
  235. case 64:
  236. avpriv_report_missing_feature(avctx, "Depth %d", bits_per_color);
  237. return AVERROR_PATCHWELCOME;
  238. default:
  239. return AVERROR_INVALIDDATA;
  240. }
  241. // Table 3c: Runs will always break at scan line boundaries. Packing
  242. // will always break to the next 32-bit word at scan-line boundaries.
  243. // Unfortunately, the encoder produced invalid files, so attempt
  244. // to detect it
  245. need_align = FFALIGN(stride, 4);
  246. if (need_align*avctx->height + (int64_t)offset > avpkt->size) {
  247. // Alignment seems unappliable, try without
  248. if (stride*avctx->height + (int64_t)offset > avpkt->size) {
  249. av_log(avctx, AV_LOG_ERROR, "Overread buffer. Invalid header?\n");
  250. return AVERROR_INVALIDDATA;
  251. } else {
  252. av_log(avctx, AV_LOG_INFO, "Decoding DPX without scanline "
  253. "alignment.\n");
  254. need_align = 0;
  255. }
  256. } else {
  257. need_align -= stride;
  258. stride = FFALIGN(stride, 4);
  259. }
  260. switch (1000 * descriptor + 10 * bits_per_color + endian) {
  261. case 6081:
  262. case 6080:
  263. avctx->pix_fmt = AV_PIX_FMT_GRAY8;
  264. break;
  265. case 6121:
  266. case 6120:
  267. avctx->pix_fmt = AV_PIX_FMT_GRAY12;
  268. break;
  269. case 50081:
  270. case 50080:
  271. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  272. break;
  273. case 52081:
  274. case 52080:
  275. avctx->pix_fmt = AV_PIX_FMT_ABGR;
  276. break;
  277. case 51081:
  278. case 51080:
  279. avctx->pix_fmt = AV_PIX_FMT_RGBA;
  280. break;
  281. case 50100:
  282. case 50101:
  283. avctx->pix_fmt = AV_PIX_FMT_GBRP10;
  284. break;
  285. case 51100:
  286. case 51101:
  287. avctx->pix_fmt = AV_PIX_FMT_GBRAP10;
  288. break;
  289. case 50120:
  290. case 50121:
  291. avctx->pix_fmt = AV_PIX_FMT_GBRP12;
  292. break;
  293. case 51120:
  294. case 51121:
  295. avctx->pix_fmt = AV_PIX_FMT_GBRAP12;
  296. break;
  297. case 6101:
  298. avctx->pix_fmt = AV_PIX_FMT_GRAY10;
  299. break;
  300. case 6161:
  301. avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
  302. break;
  303. case 6160:
  304. avctx->pix_fmt = AV_PIX_FMT_GRAY16LE;
  305. break;
  306. case 50161:
  307. avctx->pix_fmt = AV_PIX_FMT_RGB48BE;
  308. break;
  309. case 50160:
  310. avctx->pix_fmt = AV_PIX_FMT_RGB48LE;
  311. break;
  312. case 51161:
  313. avctx->pix_fmt = AV_PIX_FMT_RGBA64BE;
  314. break;
  315. case 51160:
  316. avctx->pix_fmt = AV_PIX_FMT_RGBA64LE;
  317. break;
  318. case 100081:
  319. avctx->pix_fmt = AV_PIX_FMT_UYVY422;
  320. break;
  321. case 102081:
  322. avctx->pix_fmt = AV_PIX_FMT_YUV444P;
  323. break;
  324. case 103081:
  325. avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
  326. break;
  327. default:
  328. av_log(avctx, AV_LOG_ERROR, "Unsupported format\n");
  329. return AVERROR_PATCHWELCOME;
  330. }
  331. ff_set_sar(avctx, avctx->sample_aspect_ratio);
  332. if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
  333. return ret;
  334. av_strlcpy(creator, avpkt->data + 160, 100);
  335. creator[100] = '\0';
  336. av_dict_set(&p->metadata, "Creator", creator, 0);
  337. // Move pointer to offset from start of file
  338. buf = avpkt->data + offset;
  339. for (i=0; i<AV_NUM_DATA_POINTERS; i++)
  340. ptr[i] = p->data[i];
  341. switch (bits_per_color) {
  342. case 10:
  343. for (x = 0; x < avctx->height; x++) {
  344. uint16_t *dst[4] = {(uint16_t*)ptr[0],
  345. (uint16_t*)ptr[1],
  346. (uint16_t*)ptr[2],
  347. (uint16_t*)ptr[3]};
  348. int shift = packing == 1 ? 22 : 20;
  349. for (y = 0; y < avctx->width; y++) {
  350. if (elements >= 3)
  351. *dst[2]++ = read10in32(&buf, &rgbBuffer,
  352. &n_datum, endian, shift);
  353. *dst[0]++ = read10in32(&buf, &rgbBuffer,
  354. &n_datum, endian, shift);
  355. if (elements >= 2)
  356. *dst[1]++ = read10in32(&buf, &rgbBuffer,
  357. &n_datum, endian, shift);
  358. if (elements == 4)
  359. *dst[3]++ =
  360. read10in32(&buf, &rgbBuffer,
  361. &n_datum, endian, shift);
  362. }
  363. n_datum = 0;
  364. for (i = 0; i < elements; i++)
  365. ptr[i] += p->linesize[i];
  366. }
  367. break;
  368. case 12:
  369. for (x = 0; x < avctx->height; x++) {
  370. uint16_t *dst[4] = {(uint16_t*)ptr[0],
  371. (uint16_t*)ptr[1],
  372. (uint16_t*)ptr[2],
  373. (uint16_t*)ptr[3]};
  374. int shift = packing == 1 ? 4 : 0;
  375. for (y = 0; y < avctx->width; y++) {
  376. if (packing) {
  377. if (elements >= 3)
  378. *dst[2]++ = read16(&buf, endian) >> shift & 0xFFF;
  379. *dst[0]++ = read16(&buf, endian) >> shift & 0xFFF;
  380. if (elements >= 2)
  381. *dst[1]++ = read16(&buf, endian) >> shift & 0xFFF;
  382. if (elements == 4)
  383. *dst[3]++ = read16(&buf, endian) >> shift & 0xFFF;
  384. } else {
  385. if (elements >= 3)
  386. *dst[2]++ = read12in32(&buf, &rgbBuffer,
  387. &n_datum, endian);
  388. *dst[0]++ = read12in32(&buf, &rgbBuffer,
  389. &n_datum, endian);
  390. if (elements >= 2)
  391. *dst[1]++ = read12in32(&buf, &rgbBuffer,
  392. &n_datum, endian);
  393. if (elements == 4)
  394. *dst[3]++ = read12in32(&buf, &rgbBuffer,
  395. &n_datum, endian);
  396. }
  397. }
  398. n_datum = 0;
  399. for (i = 0; i < elements; i++)
  400. ptr[i] += p->linesize[i];
  401. // Jump to next aligned position
  402. buf += need_align;
  403. }
  404. break;
  405. case 16:
  406. elements *= 2;
  407. case 8:
  408. if ( avctx->pix_fmt == AV_PIX_FMT_YUVA444P
  409. || avctx->pix_fmt == AV_PIX_FMT_YUV444P) {
  410. for (x = 0; x < avctx->height; x++) {
  411. ptr[0] = p->data[0] + x * p->linesize[0];
  412. ptr[1] = p->data[1] + x * p->linesize[1];
  413. ptr[2] = p->data[2] + x * p->linesize[2];
  414. ptr[3] = p->data[3] + x * p->linesize[3];
  415. for (y = 0; y < avctx->width; y++) {
  416. *ptr[1]++ = *buf++;
  417. *ptr[0]++ = *buf++;
  418. *ptr[2]++ = *buf++;
  419. if (avctx->pix_fmt == AV_PIX_FMT_YUVA444P)
  420. *ptr[3]++ = *buf++;
  421. }
  422. }
  423. } else {
  424. av_image_copy_plane(ptr[0], p->linesize[0],
  425. buf, stride,
  426. elements * avctx->width, avctx->height);
  427. }
  428. break;
  429. }
  430. *got_frame = 1;
  431. return buf_size;
  432. }
  433. AVCodec ff_dpx_decoder = {
  434. .name = "dpx",
  435. .long_name = NULL_IF_CONFIG_SMALL("DPX (Digital Picture Exchange) image"),
  436. .type = AVMEDIA_TYPE_VIDEO,
  437. .id = AV_CODEC_ID_DPX,
  438. .decode = decode_frame,
  439. .capabilities = AV_CODEC_CAP_DR1,
  440. };