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.

327 lines
9.3KB

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