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.

299 lines
9.1KB

  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. typedef struct DPXContext {
  27. AVFrame picture;
  28. } DPXContext;
  29. static unsigned int read32(const uint8_t **ptr, int is_big)
  30. {
  31. unsigned int temp;
  32. if (is_big) {
  33. temp = AV_RB32(*ptr);
  34. } else {
  35. temp = AV_RL32(*ptr);
  36. }
  37. *ptr += 4;
  38. return temp;
  39. }
  40. static uint16_t read10in32(const uint8_t **ptr, uint32_t * lbuf,
  41. int * n_datum, int is_big)
  42. {
  43. if (*n_datum)
  44. (*n_datum)--;
  45. else {
  46. *lbuf = read32(ptr, is_big);
  47. *n_datum = 2;
  48. }
  49. *lbuf = (*lbuf << 10) | (*lbuf >> 22);
  50. return *lbuf & 0x3FF;
  51. }
  52. static int decode_frame(AVCodecContext *avctx,
  53. void *data,
  54. int *got_frame,
  55. AVPacket *avpkt)
  56. {
  57. const uint8_t *buf = avpkt->data;
  58. int buf_size = avpkt->size;
  59. DPXContext *const s = avctx->priv_data;
  60. AVFrame *picture = data;
  61. AVFrame *const p = &s->picture;
  62. uint8_t *ptr[AV_NUM_DATA_POINTERS];
  63. unsigned int offset;
  64. int magic_num, endian;
  65. int x, y, i, ret;
  66. int w, h, bits_per_color, descriptor, elements, packing, total_size;
  67. unsigned int rgbBuffer = 0;
  68. int n_datum = 0;
  69. if (avpkt->size <= 1634) {
  70. av_log(avctx, AV_LOG_ERROR, "Packet too small for DPX header\n");
  71. return AVERROR_INVALIDDATA;
  72. }
  73. magic_num = AV_RB32(buf);
  74. buf += 4;
  75. /* Check if the files "magic number" is "SDPX" which means it uses
  76. * big-endian or XPDS which is for little-endian files */
  77. if (magic_num == AV_RL32("SDPX")) {
  78. endian = 0;
  79. } else if (magic_num == AV_RB32("SDPX")) {
  80. endian = 1;
  81. } else {
  82. av_log(avctx, AV_LOG_ERROR, "DPX marker not found\n");
  83. return AVERROR_INVALIDDATA;
  84. }
  85. offset = read32(&buf, endian);
  86. if (avpkt->size <= offset) {
  87. av_log(avctx, AV_LOG_ERROR, "Invalid data start offset\n");
  88. return AVERROR_INVALIDDATA;
  89. }
  90. // Need to end in 0x304 offset from start of file
  91. buf = avpkt->data + 0x304;
  92. w = read32(&buf, endian);
  93. h = read32(&buf, endian);
  94. if ((ret = av_image_check_size(w, h, 0, avctx)) < 0)
  95. return ret;
  96. if (w != avctx->width || h != avctx->height)
  97. avcodec_set_dimensions(avctx, w, h);
  98. // Need to end in 0x320 to read the descriptor
  99. buf += 20;
  100. descriptor = buf[0];
  101. // Need to end in 0x323 to read the bits per color
  102. buf += 3;
  103. avctx->bits_per_raw_sample =
  104. bits_per_color = buf[0];
  105. buf++;
  106. packing = *((uint16_t*)buf);
  107. buf += 824;
  108. avctx->sample_aspect_ratio.num = read32(&buf, endian);
  109. avctx->sample_aspect_ratio.den = read32(&buf, endian);
  110. if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0)
  111. av_reduce(&avctx->sample_aspect_ratio.num, &avctx->sample_aspect_ratio.den,
  112. avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den,
  113. 0x10000);
  114. else
  115. avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
  116. switch (descriptor) {
  117. case 51: // RGBA
  118. elements = 4;
  119. break;
  120. case 50: // RGB
  121. elements = 3;
  122. break;
  123. default:
  124. av_log(avctx, AV_LOG_ERROR, "Unsupported descriptor %d\n", descriptor);
  125. return AVERROR_INVALIDDATA;
  126. }
  127. switch (bits_per_color) {
  128. case 8:
  129. if (elements == 4) {
  130. avctx->pix_fmt = AV_PIX_FMT_RGBA;
  131. } else {
  132. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  133. }
  134. total_size = avctx->width * avctx->height * elements;
  135. break;
  136. case 10:
  137. if (!packing) {
  138. av_log(avctx, AV_LOG_ERROR, "Packing to 32bit required\n");
  139. return -1;
  140. }
  141. avctx->pix_fmt = AV_PIX_FMT_GBRP10;
  142. total_size = (4 * avctx->width * avctx->height * elements) / 3;
  143. break;
  144. case 12:
  145. if (!packing) {
  146. av_log(avctx, AV_LOG_ERROR, "Packing to 16bit required\n");
  147. return -1;
  148. }
  149. if (endian) {
  150. avctx->pix_fmt = AV_PIX_FMT_GBRP12BE;
  151. } else {
  152. avctx->pix_fmt = AV_PIX_FMT_GBRP12LE;
  153. }
  154. total_size = 2 * avctx->width * avctx->height * elements;
  155. break;
  156. case 16:
  157. if (endian) {
  158. avctx->pix_fmt = elements == 4 ? AV_PIX_FMT_RGBA64BE : AV_PIX_FMT_RGB48BE;
  159. } else {
  160. avctx->pix_fmt = elements == 4 ? AV_PIX_FMT_RGBA64LE : AV_PIX_FMT_RGB48LE;
  161. }
  162. total_size = 2 * avctx->width * avctx->height * elements;
  163. break;
  164. default:
  165. av_log(avctx, AV_LOG_ERROR, "Unsupported color depth : %d\n", bits_per_color);
  166. return AVERROR_INVALIDDATA;
  167. }
  168. if (s->picture.data[0])
  169. avctx->release_buffer(avctx, &s->picture);
  170. if ((ret = ff_get_buffer(avctx, p)) < 0) {
  171. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  172. return ret;
  173. }
  174. // Move pointer to offset from start of file
  175. buf = avpkt->data + offset;
  176. for (i=0; i<AV_NUM_DATA_POINTERS; i++)
  177. ptr[i] = p->data[i];
  178. if (total_size > avpkt->size) {
  179. av_log(avctx, AV_LOG_ERROR, "Overread buffer. Invalid header?\n");
  180. return AVERROR_INVALIDDATA;
  181. }
  182. switch (bits_per_color) {
  183. case 10:
  184. for (x = 0; x < avctx->height; x++) {
  185. uint16_t *dst[3] = {(uint16_t*)ptr[0],
  186. (uint16_t*)ptr[1],
  187. (uint16_t*)ptr[2]};
  188. for (y = 0; y < avctx->width; y++) {
  189. *dst[2]++ = read10in32(&buf, &rgbBuffer,
  190. &n_datum, endian);
  191. *dst[0]++ = read10in32(&buf, &rgbBuffer,
  192. &n_datum, endian);
  193. *dst[1]++ = read10in32(&buf, &rgbBuffer,
  194. &n_datum, endian);
  195. // For 10 bit, ignore alpha
  196. if (elements == 4)
  197. read10in32(&buf, &rgbBuffer,
  198. &n_datum, endian);
  199. }
  200. for (i = 0; i < 3; i++)
  201. ptr[i] += p->linesize[i];
  202. }
  203. break;
  204. case 12:
  205. for (x = 0; x < avctx->height; x++) {
  206. uint16_t *dst[3] = {(uint16_t*)ptr[0],
  207. (uint16_t*)ptr[1],
  208. (uint16_t*)ptr[2]};
  209. for (y = 0; y < avctx->width; y++) {
  210. *dst[2] = *((uint16_t*)buf);
  211. *dst[2] = (*dst[2] >> 4) | (*dst[2] << 12);
  212. dst[2]++;
  213. buf += 2;
  214. *dst[0] = *((uint16_t*)buf);
  215. *dst[0] = (*dst[0] >> 4) | (*dst[0] << 12);
  216. dst[0]++;
  217. buf += 2;
  218. *dst[1] = *((uint16_t*)buf);
  219. *dst[1] = (*dst[1] >> 4) | (*dst[1] << 12);
  220. dst[1]++;
  221. buf += 2;
  222. // For 12 bit, ignore alpha
  223. if (elements == 4)
  224. buf += 2;
  225. }
  226. for (i = 0; i < 3; i++)
  227. ptr[i] += p->linesize[i];
  228. }
  229. break;
  230. case 16:
  231. elements *= 2;
  232. case 8:
  233. for (x = 0; x < avctx->height; x++) {
  234. memcpy(ptr[0], buf, elements*avctx->width);
  235. ptr[0] += p->linesize[0];
  236. buf += elements*avctx->width;
  237. }
  238. break;
  239. }
  240. *picture = s->picture;
  241. *got_frame = 1;
  242. return buf_size;
  243. }
  244. static av_cold int decode_init(AVCodecContext *avctx)
  245. {
  246. DPXContext *s = avctx->priv_data;
  247. avcodec_get_frame_defaults(&s->picture);
  248. avctx->coded_frame = &s->picture;
  249. return 0;
  250. }
  251. static av_cold int decode_end(AVCodecContext *avctx)
  252. {
  253. DPXContext *s = avctx->priv_data;
  254. if (s->picture.data[0])
  255. avctx->release_buffer(avctx, &s->picture);
  256. return 0;
  257. }
  258. AVCodec ff_dpx_decoder = {
  259. .name = "dpx",
  260. .type = AVMEDIA_TYPE_VIDEO,
  261. .id = AV_CODEC_ID_DPX,
  262. .priv_data_size = sizeof(DPXContext),
  263. .init = decode_init,
  264. .close = decode_end,
  265. .decode = decode_frame,
  266. .long_name = NULL_IF_CONFIG_SMALL("DPX image"),
  267. .capabilities = CODEC_CAP_DR1,
  268. };