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.

572 lines
17KB

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