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.

458 lines
13KB

  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. stride = avctx->width * elements;
  211. if (packing) {
  212. stride *= 2;
  213. } else {
  214. stride *= 3;
  215. if (stride % 8) {
  216. stride /= 8;
  217. stride++;
  218. stride *= 8;
  219. }
  220. stride /= 2;
  221. }
  222. break;
  223. case 16:
  224. stride = 2 * avctx->width * elements;
  225. break;
  226. case 1:
  227. case 32:
  228. case 64:
  229. avpriv_report_missing_feature(avctx, "Depth %d", bits_per_color);
  230. return AVERROR_PATCHWELCOME;
  231. default:
  232. return AVERROR_INVALIDDATA;
  233. }
  234. // Table 3c: Runs will always break at scan line boundaries. Packing
  235. // will always break to the next 32-bit word at scan-line boundaries.
  236. // Unfortunately, the encoder produced invalid files, so attempt
  237. // to detect it
  238. need_align = FFALIGN(stride, 4);
  239. if (need_align*avctx->height + (int64_t)offset > avpkt->size) {
  240. // Alignment seems unappliable, try without
  241. if (stride*avctx->height + (int64_t)offset > avpkt->size) {
  242. av_log(avctx, AV_LOG_ERROR, "Overread buffer. Invalid header?\n");
  243. return AVERROR_INVALIDDATA;
  244. } else {
  245. av_log(avctx, AV_LOG_INFO, "Decoding DPX without scanline "
  246. "alignment.\n");
  247. need_align = 0;
  248. }
  249. } else {
  250. need_align -= stride;
  251. stride = FFALIGN(stride, 4);
  252. }
  253. switch (1000 * descriptor + 10 * bits_per_color + endian) {
  254. case 6081:
  255. case 6080:
  256. avctx->pix_fmt = AV_PIX_FMT_GRAY8;
  257. break;
  258. case 6121:
  259. case 6120:
  260. avctx->pix_fmt = AV_PIX_FMT_GRAY12;
  261. break;
  262. case 50081:
  263. case 50080:
  264. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  265. break;
  266. case 52081:
  267. case 52080:
  268. avctx->pix_fmt = AV_PIX_FMT_ABGR;
  269. break;
  270. case 51081:
  271. case 51080:
  272. avctx->pix_fmt = AV_PIX_FMT_RGBA;
  273. break;
  274. case 50100:
  275. case 50101:
  276. avctx->pix_fmt = AV_PIX_FMT_GBRP10;
  277. break;
  278. case 51100:
  279. case 51101:
  280. avctx->pix_fmt = AV_PIX_FMT_GBRAP10;
  281. break;
  282. case 50120:
  283. case 50121:
  284. avctx->pix_fmt = AV_PIX_FMT_GBRP12;
  285. break;
  286. case 51120:
  287. case 51121:
  288. avctx->pix_fmt = AV_PIX_FMT_GBRAP12;
  289. break;
  290. case 6161:
  291. avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
  292. break;
  293. case 6160:
  294. avctx->pix_fmt = AV_PIX_FMT_GRAY16LE;
  295. break;
  296. case 50161:
  297. avctx->pix_fmt = AV_PIX_FMT_RGB48BE;
  298. break;
  299. case 50160:
  300. avctx->pix_fmt = AV_PIX_FMT_RGB48LE;
  301. break;
  302. case 51161:
  303. avctx->pix_fmt = AV_PIX_FMT_RGBA64BE;
  304. break;
  305. case 51160:
  306. avctx->pix_fmt = AV_PIX_FMT_RGBA64LE;
  307. break;
  308. case 100081:
  309. avctx->pix_fmt = AV_PIX_FMT_UYVY422;
  310. break;
  311. case 102081:
  312. avctx->pix_fmt = AV_PIX_FMT_YUV444P;
  313. break;
  314. case 103081:
  315. avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
  316. break;
  317. default:
  318. av_log(avctx, AV_LOG_ERROR, "Unsupported format\n");
  319. return AVERROR_PATCHWELCOME;
  320. }
  321. ff_set_sar(avctx, avctx->sample_aspect_ratio);
  322. if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
  323. return ret;
  324. // Move pointer to offset from start of file
  325. buf = avpkt->data + offset;
  326. for (i=0; i<AV_NUM_DATA_POINTERS; i++)
  327. ptr[i] = p->data[i];
  328. switch (bits_per_color) {
  329. case 10:
  330. for (x = 0; x < avctx->height; x++) {
  331. uint16_t *dst[4] = {(uint16_t*)ptr[0],
  332. (uint16_t*)ptr[1],
  333. (uint16_t*)ptr[2],
  334. (uint16_t*)ptr[3]};
  335. for (y = 0; y < avctx->width; y++) {
  336. *dst[2]++ = read10in32(&buf, &rgbBuffer,
  337. &n_datum, endian);
  338. *dst[0]++ = read10in32(&buf, &rgbBuffer,
  339. &n_datum, endian);
  340. *dst[1]++ = read10in32(&buf, &rgbBuffer,
  341. &n_datum, endian);
  342. if (elements == 4)
  343. *dst[3]++ =
  344. read10in32(&buf, &rgbBuffer,
  345. &n_datum, endian);
  346. }
  347. n_datum = 0;
  348. for (i = 0; i < elements; i++)
  349. ptr[i] += p->linesize[i];
  350. }
  351. break;
  352. case 12:
  353. for (x = 0; x < avctx->height; x++) {
  354. uint16_t *dst[4] = {(uint16_t*)ptr[0],
  355. (uint16_t*)ptr[1],
  356. (uint16_t*)ptr[2],
  357. (uint16_t*)ptr[3]};
  358. for (y = 0; y < avctx->width; y++) {
  359. if (packing) {
  360. if (elements >= 3)
  361. *dst[2]++ = read16(&buf, endian) >> 4;
  362. *dst[0] = read16(&buf, endian) >> 4;
  363. dst[0]++;
  364. if (elements >= 2)
  365. *dst[1]++ = read16(&buf, endian) >> 4;
  366. if (elements == 4)
  367. *dst[3]++ = read16(&buf, endian) >> 4;
  368. } else {
  369. *dst[2]++ = read12in32(&buf, &rgbBuffer,
  370. &n_datum, endian);
  371. *dst[0]++ = read12in32(&buf, &rgbBuffer,
  372. &n_datum, endian);
  373. *dst[1]++ = read12in32(&buf, &rgbBuffer,
  374. &n_datum, endian);
  375. if (elements == 4)
  376. *dst[3]++ = read12in32(&buf, &rgbBuffer,
  377. &n_datum, endian);
  378. }
  379. }
  380. n_datum = 0;
  381. for (i = 0; i < elements; i++)
  382. ptr[i] += p->linesize[i];
  383. // Jump to next aligned position
  384. buf += need_align;
  385. }
  386. break;
  387. case 16:
  388. elements *= 2;
  389. case 8:
  390. if ( avctx->pix_fmt == AV_PIX_FMT_YUVA444P
  391. || avctx->pix_fmt == AV_PIX_FMT_YUV444P) {
  392. for (x = 0; x < avctx->height; x++) {
  393. ptr[0] = p->data[0] + x * p->linesize[0];
  394. ptr[1] = p->data[1] + x * p->linesize[1];
  395. ptr[2] = p->data[2] + x * p->linesize[2];
  396. ptr[3] = p->data[3] + x * p->linesize[3];
  397. for (y = 0; y < avctx->width; y++) {
  398. *ptr[1]++ = *buf++;
  399. *ptr[0]++ = *buf++;
  400. *ptr[2]++ = *buf++;
  401. if (avctx->pix_fmt == AV_PIX_FMT_YUVA444P)
  402. *ptr[3]++ = *buf++;
  403. }
  404. }
  405. } else {
  406. av_image_copy_plane(ptr[0], p->linesize[0],
  407. buf, stride,
  408. elements * avctx->width, avctx->height);
  409. }
  410. break;
  411. }
  412. *got_frame = 1;
  413. return buf_size;
  414. }
  415. AVCodec ff_dpx_decoder = {
  416. .name = "dpx",
  417. .long_name = NULL_IF_CONFIG_SMALL("DPX (Digital Picture Exchange) image"),
  418. .type = AVMEDIA_TYPE_VIDEO,
  419. .id = AV_CODEC_ID_DPX,
  420. .decode = decode_frame,
  421. .capabilities = AV_CODEC_CAP_DR1,
  422. };