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.

281 lines
8.9KB

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