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.

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