2 * DPX (.dpx) image decoder
3 * Copyright (c) 2009 Jimmy Christensen
5 * This file is part of Libav.
7 * Libav 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.
12 * Libav 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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/intreadwrite.h"
23 #include "libavutil/imgutils.h"
24 #include "bytestream.h"
28 static unsigned int read32(const uint8_t **ptr, int is_big)
40 static inline unsigned make_16bit(unsigned value)
42 // mask away invalid bits
44 // correctly expand to 16 bits
45 return value + (value >> 10);
48 static int decode_frame(AVCodecContext *avctx,
53 const uint8_t *buf = avpkt->data;
54 const uint8_t *buf_end = avpkt->data + avpkt->size;
55 int buf_size = avpkt->size;
56 AVFrame *const p = data;
60 int magic_num, endian;
62 int w, h, stride, bits_per_color, descriptor, elements, target_packet_size, source_packet_size;
64 unsigned int rgbBuffer;
66 if (avpkt->size <= 1634) {
67 av_log(avctx, AV_LOG_ERROR, "Packet too small for DPX header\n");
68 return AVERROR_INVALIDDATA;
71 magic_num = AV_RB32(buf);
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")) {
78 } else if (magic_num == AV_RB32("SDPX")) {
81 av_log(avctx, AV_LOG_ERROR, "DPX marker not found\n");
82 return AVERROR_INVALIDDATA;
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;
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);
95 // Need to end in 0x320 to read the descriptor
99 // Need to end in 0x323 to read the bits per color
101 avctx->bits_per_raw_sample =
102 bits_per_color = buf[0];
105 avctx->sample_aspect_ratio.num = read32(&buf, endian);
106 avctx->sample_aspect_ratio.den = read32(&buf, endian);
108 switch (descriptor) {
116 av_log(avctx, AV_LOG_ERROR, "Unsupported descriptor %d\n", descriptor);
117 return AVERROR_INVALIDDATA;
120 switch (bits_per_color) {
123 avctx->pix_fmt = AV_PIX_FMT_RGBA;
125 avctx->pix_fmt = AV_PIX_FMT_RGB24;
127 source_packet_size = elements;
128 target_packet_size = elements;
131 avctx->pix_fmt = AV_PIX_FMT_RGB48;
132 target_packet_size = 6;
133 source_packet_size = 4;
138 avctx->pix_fmt = AV_PIX_FMT_RGB48BE;
140 avctx->pix_fmt = AV_PIX_FMT_RGB48LE;
142 target_packet_size = 6;
143 source_packet_size = elements * 2;
146 av_log(avctx, AV_LOG_ERROR, "Unsupported color depth : %d\n", bits_per_color);
147 return AVERROR_INVALIDDATA;
150 if ((ret = ff_set_dimensions(avctx, w, h)) < 0)
153 ff_set_sar(avctx, avctx->sample_aspect_ratio);
155 if ((ret = ff_get_buffer(avctx, p, 0)) < 0) {
156 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
160 // Move pointer to offset from start of file
161 buf = avpkt->data + offset;
164 stride = p->linesize[0];
166 if (source_packet_size*avctx->width*avctx->height > buf_end - buf) {
167 av_log(avctx, AV_LOG_ERROR, "Overread buffer. Invalid header?\n");
168 return AVERROR_INVALIDDATA;
170 switch (bits_per_color) {
172 for (x = 0; x < avctx->height; x++) {
173 uint16_t *dst = (uint16_t*)ptr;
174 for (y = 0; y < avctx->width; y++) {
175 rgbBuffer = read32(&buf, endian);
176 // Read out the 10-bit colors and convert to 16-bit
177 *dst++ = make_16bit(rgbBuffer >> 16);
178 *dst++ = make_16bit(rgbBuffer >> 6);
179 *dst++ = make_16bit(rgbBuffer << 4);
185 case 12: // Treat 12-bit as 16-bit
187 if (source_packet_size == target_packet_size) {
188 for (x = 0; x < avctx->height; x++) {
189 memcpy(ptr, buf, target_packet_size*avctx->width);
191 buf += source_packet_size*avctx->width;
194 for (x = 0; x < avctx->height; x++) {
196 for (y = 0; y < avctx->width; y++) {
197 memcpy(dst, buf, target_packet_size);
198 dst += target_packet_size;
199 buf += source_packet_size;
212 AVCodec ff_dpx_decoder = {
214 .long_name = NULL_IF_CONFIG_SMALL("DPX image"),
215 .type = AVMEDIA_TYPE_VIDEO,
216 .id = AV_CODEC_ID_DPX,
217 .decode = decode_frame,
218 .capabilities = AV_CODEC_CAP_DR1,