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.

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