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.

336 lines
9.5KB

  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 int decode_frame(AVCodecContext *avctx,
  62. void *data,
  63. int *got_frame,
  64. AVPacket *avpkt)
  65. {
  66. const uint8_t *buf = avpkt->data;
  67. int buf_size = avpkt->size;
  68. AVFrame *const p = data;
  69. uint8_t *ptr[AV_NUM_DATA_POINTERS];
  70. unsigned int offset;
  71. int magic_num, endian;
  72. int x, y, i, ret;
  73. int w, h, bits_per_color, descriptor, elements, packing, total_size;
  74. int encoding;
  75. unsigned int rgbBuffer = 0;
  76. int n_datum = 0;
  77. if (avpkt->size <= 1634) {
  78. av_log(avctx, AV_LOG_ERROR, "Packet too small for DPX header\n");
  79. return AVERROR_INVALIDDATA;
  80. }
  81. magic_num = AV_RB32(buf);
  82. buf += 4;
  83. /* Check if the files "magic number" is "SDPX" which means it uses
  84. * big-endian or XPDS which is for little-endian files */
  85. if (magic_num == AV_RL32("SDPX")) {
  86. endian = 0;
  87. } else if (magic_num == AV_RB32("SDPX")) {
  88. endian = 1;
  89. } else {
  90. av_log(avctx, AV_LOG_ERROR, "DPX marker not found\n");
  91. return AVERROR_INVALIDDATA;
  92. }
  93. offset = read32(&buf, endian);
  94. if (avpkt->size <= offset) {
  95. av_log(avctx, AV_LOG_ERROR, "Invalid data start offset\n");
  96. return AVERROR_INVALIDDATA;
  97. }
  98. // Need to end in 0x304 offset from start of file
  99. buf = avpkt->data + 0x304;
  100. w = read32(&buf, endian);
  101. h = read32(&buf, endian);
  102. if ((ret = ff_set_dimensions(avctx, w, h)) < 0)
  103. return ret;
  104. // Need to end in 0x320 to read the descriptor
  105. buf += 20;
  106. descriptor = buf[0];
  107. // Need to end in 0x323 to read the bits per color
  108. buf += 3;
  109. avctx->bits_per_raw_sample =
  110. bits_per_color = buf[0];
  111. buf++;
  112. packing = read16(&buf, endian);
  113. encoding = read16(&buf, endian);
  114. if (packing > 1) {
  115. avpriv_report_missing_feature(avctx, "Packing %d", packing);
  116. return AVERROR_PATCHWELCOME;
  117. }
  118. if (encoding) {
  119. avpriv_report_missing_feature(avctx, "Encoding %d", encoding);
  120. return AVERROR_PATCHWELCOME;
  121. }
  122. buf += 820;
  123. avctx->sample_aspect_ratio.num = read32(&buf, endian);
  124. avctx->sample_aspect_ratio.den = read32(&buf, endian);
  125. if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0)
  126. av_reduce(&avctx->sample_aspect_ratio.num, &avctx->sample_aspect_ratio.den,
  127. avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den,
  128. 0x10000);
  129. else
  130. avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
  131. if (offset >= 1724 + 4) {
  132. buf = avpkt->data + 1724;
  133. i = read32(&buf, endian);
  134. if(i) {
  135. AVRational q = av_d2q(av_int2float(i), 4096);
  136. if (q.num > 0 && q.den > 0)
  137. avctx->time_base = av_inv_q(q);
  138. }
  139. }
  140. switch (descriptor) {
  141. case 6: // Y
  142. elements = 1;
  143. break;
  144. case 52: // ABGR
  145. case 51: // RGBA
  146. elements = 4;
  147. break;
  148. case 50: // RGB
  149. elements = 3;
  150. break;
  151. default:
  152. avpriv_report_missing_feature(avctx, "Descriptor %d", descriptor);
  153. return AVERROR_PATCHWELCOME;
  154. }
  155. switch (bits_per_color) {
  156. case 8:
  157. total_size = avctx->width * avctx->height * elements;
  158. break;
  159. case 10:
  160. if (!packing) {
  161. av_log(avctx, AV_LOG_ERROR, "Packing to 32bit required\n");
  162. return -1;
  163. }
  164. total_size = (avctx->width * elements + 2) / 3 * 4 * avctx->height;
  165. break;
  166. case 12:
  167. if (!packing) {
  168. av_log(avctx, AV_LOG_ERROR, "Packing to 16bit required\n");
  169. return -1;
  170. }
  171. total_size = 2 * avctx->width * avctx->height * elements;
  172. break;
  173. case 16:
  174. total_size = 2 * avctx->width * avctx->height * elements;
  175. break;
  176. case 1:
  177. case 32:
  178. case 64:
  179. avpriv_report_missing_feature(avctx, "Depth %d", bits_per_color);
  180. return AVERROR_PATCHWELCOME;
  181. default:
  182. return AVERROR_INVALIDDATA;
  183. }
  184. switch (1000 * descriptor + 10 * bits_per_color + endian) {
  185. case 6081:
  186. case 6080:
  187. avctx->pix_fmt = AV_PIX_FMT_GRAY8;
  188. break;
  189. case 50081:
  190. case 50080:
  191. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  192. break;
  193. case 52081:
  194. case 52080:
  195. avctx->pix_fmt = AV_PIX_FMT_ABGR;
  196. break;
  197. case 51081:
  198. case 51080:
  199. avctx->pix_fmt = AV_PIX_FMT_RGBA;
  200. break;
  201. case 50100:
  202. case 51100:
  203. case 50101:
  204. case 51101:
  205. avctx->pix_fmt = AV_PIX_FMT_GBRP10;
  206. break;
  207. case 50120:
  208. case 51120:
  209. case 50121:
  210. case 51121:
  211. avctx->pix_fmt = AV_PIX_FMT_GBRP12;
  212. break;
  213. case 6161:
  214. avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
  215. break;
  216. case 6160:
  217. avctx->pix_fmt = AV_PIX_FMT_GRAY16LE;
  218. break;
  219. case 50161:
  220. avctx->pix_fmt = AV_PIX_FMT_RGB48BE;
  221. break;
  222. case 50160:
  223. avctx->pix_fmt = AV_PIX_FMT_RGB48LE;
  224. break;
  225. case 51161:
  226. avctx->pix_fmt = AV_PIX_FMT_RGBA64BE;
  227. break;
  228. case 51160:
  229. avctx->pix_fmt = AV_PIX_FMT_RGBA64LE;
  230. break;
  231. default:
  232. av_log(avctx, AV_LOG_ERROR, "Unsupported format\n");
  233. return AVERROR_PATCHWELCOME;
  234. }
  235. if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
  236. return ret;
  237. // Move pointer to offset from start of file
  238. buf = avpkt->data + offset;
  239. for (i=0; i<AV_NUM_DATA_POINTERS; i++)
  240. ptr[i] = p->data[i];
  241. if (total_size + (int64_t)offset > avpkt->size) {
  242. av_log(avctx, AV_LOG_ERROR, "Overread buffer. Invalid header?\n");
  243. return AVERROR_INVALIDDATA;
  244. }
  245. switch (bits_per_color) {
  246. case 10:
  247. for (x = 0; x < avctx->height; x++) {
  248. uint16_t *dst[3] = {(uint16_t*)ptr[0],
  249. (uint16_t*)ptr[1],
  250. (uint16_t*)ptr[2]};
  251. for (y = 0; y < avctx->width; y++) {
  252. *dst[2]++ = read10in32(&buf, &rgbBuffer,
  253. &n_datum, endian);
  254. *dst[0]++ = read10in32(&buf, &rgbBuffer,
  255. &n_datum, endian);
  256. *dst[1]++ = read10in32(&buf, &rgbBuffer,
  257. &n_datum, endian);
  258. // For 10 bit, ignore alpha
  259. if (elements == 4)
  260. read10in32(&buf, &rgbBuffer,
  261. &n_datum, endian);
  262. }
  263. n_datum = 0;
  264. for (i = 0; i < 3; i++)
  265. ptr[i] += p->linesize[i];
  266. }
  267. break;
  268. case 12:
  269. for (x = 0; x < avctx->height; x++) {
  270. uint16_t *dst[3] = {(uint16_t*)ptr[0],
  271. (uint16_t*)ptr[1],
  272. (uint16_t*)ptr[2]};
  273. for (y = 0; y < avctx->width; y++) {
  274. *dst[2] = read16(&buf, endian) >> 4;
  275. dst[2]++;
  276. *dst[0] = read16(&buf, endian) >> 4;
  277. dst[0]++;
  278. *dst[1] = read16(&buf, endian) >> 4;
  279. dst[1]++;
  280. // For 12 bit, ignore alpha
  281. if (elements == 4)
  282. buf += 2;
  283. }
  284. for (i = 0; i < 3; i++)
  285. ptr[i] += p->linesize[i];
  286. }
  287. break;
  288. case 16:
  289. elements *= 2;
  290. case 8:
  291. av_image_copy_plane(ptr[0], p->linesize[0],
  292. buf, elements * avctx->width,
  293. elements * avctx->width, avctx->height);
  294. break;
  295. }
  296. *got_frame = 1;
  297. return buf_size;
  298. }
  299. AVCodec ff_dpx_decoder = {
  300. .name = "dpx",
  301. .long_name = NULL_IF_CONFIG_SMALL("DPX (Digital Picture Exchange) image"),
  302. .type = AVMEDIA_TYPE_VIDEO,
  303. .id = AV_CODEC_ID_DPX,
  304. .decode = decode_frame,
  305. .capabilities = CODEC_CAP_DR1,
  306. };