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.

297 lines
9.0KB

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