2 * DPX (.dpx) image decoder
3 * Copyright (c) 2009 Jimmy Christensen
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavutil/intreadwrite.h"
23 #include "libavutil/imgutils.h"
24 #include "bytestream.h"
27 typedef struct DPXContext {
32 static unsigned int read32(const uint8_t **ptr, int is_big)
44 static inline unsigned make_16bit(unsigned value)
46 // mask away invalid bits
48 // correctly expand to 16 bits
49 return value + (value >> 10);
52 static int decode_frame(AVCodecContext *avctx,
57 const uint8_t *buf = avpkt->data;
58 const uint8_t *buf_end = avpkt->data + avpkt->size;
59 int buf_size = avpkt->size;
60 DPXContext *const s = avctx->priv_data;
61 AVFrame *picture = data;
62 AVFrame *const p = &s->picture;
66 int magic_num, endian;
68 int w, h, stride, bits_per_color, descriptor, elements, target_packet_size, source_packet_size;
70 unsigned int rgbBuffer;
72 if (avpkt->size <= 1634) {
73 av_log(avctx, AV_LOG_ERROR, "Packet too small for DPX header\n");
74 return AVERROR_INVALIDDATA;
77 magic_num = AV_RB32(buf);
80 /* Check if the files "magic number" is "SDPX" which means it uses
81 * big-endian or XPDS which is for little-endian files */
82 if (magic_num == AV_RL32("SDPX")) {
84 } else if (magic_num == AV_RB32("SDPX")) {
87 av_log(avctx, AV_LOG_ERROR, "DPX marker not found\n");
91 offset = read32(&buf, endian);
92 if (avpkt->size <= offset) {
93 av_log(avctx, AV_LOG_ERROR, "Invalid data start offset\n");
94 return AVERROR_INVALIDDATA;
96 // Need to end in 0x304 offset from start of file
97 buf = avpkt->data + 0x304;
98 w = read32(&buf, endian);
99 h = read32(&buf, endian);
101 // Need to end in 0x320 to read the descriptor
105 // Need to end in 0x323 to read the bits per color
107 avctx->bits_per_raw_sample =
108 bits_per_color = buf[0];
111 avctx->sample_aspect_ratio.num = read32(&buf, endian);
112 avctx->sample_aspect_ratio.den = read32(&buf, endian);
113 if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0)
114 av_reduce(&avctx->sample_aspect_ratio.num, &avctx->sample_aspect_ratio.den,
115 avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den,
118 avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
120 switch (descriptor) {
128 av_log(avctx, AV_LOG_ERROR, "Unsupported descriptor %d\n", descriptor);
132 switch (bits_per_color) {
135 avctx->pix_fmt = PIX_FMT_RGBA;
137 avctx->pix_fmt = PIX_FMT_RGB24;
139 source_packet_size = elements;
140 target_packet_size = elements;
143 avctx->pix_fmt = PIX_FMT_RGB48;
144 target_packet_size = 6;
145 source_packet_size = 4;
150 avctx->pix_fmt = elements == 4 ? PIX_FMT_RGBA64BE : PIX_FMT_RGB48BE;
152 avctx->pix_fmt = elements == 4 ? PIX_FMT_RGBA64LE : PIX_FMT_RGB48LE;
155 source_packet_size = elements * 2;
158 av_log(avctx, AV_LOG_ERROR, "Unsupported color depth : %d\n", bits_per_color);
162 if (s->picture.data[0])
163 avctx->release_buffer(avctx, &s->picture);
164 if (av_image_check_size(w, h, 0, avctx))
166 if (w != avctx->width || h != avctx->height)
167 avcodec_set_dimensions(avctx, w, h);
168 if (avctx->get_buffer(avctx, p) < 0) {
169 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
173 // Move pointer to offset from start of file
174 buf = avpkt->data + offset;
177 stride = p->linesize[0];
179 if (source_packet_size*avctx->width*avctx->height > buf_end - buf) {
180 av_log(avctx, AV_LOG_ERROR, "Overread buffer. Invalid header?\n");
183 switch (bits_per_color) {
185 for (x = 0; x < avctx->height; x++) {
186 uint16_t *dst = (uint16_t*)ptr;
187 for (y = 0; y < avctx->width; y++) {
188 rgbBuffer = read32(&buf, endian);
189 // Read out the 10-bit colors and convert to 16-bit
190 *dst++ = make_16bit(rgbBuffer >> 16);
191 *dst++ = make_16bit(rgbBuffer >> 6);
192 *dst++ = make_16bit(rgbBuffer << 4);
198 case 12: // Treat 12-bit as 16-bit
200 if (source_packet_size == target_packet_size) {
201 for (x = 0; x < avctx->height; x++) {
202 memcpy(ptr, buf, target_packet_size*avctx->width);
204 buf += source_packet_size*avctx->width;
207 for (x = 0; x < avctx->height; x++) {
209 for (y = 0; y < avctx->width; y++) {
210 memcpy(dst, buf, target_packet_size);
211 dst += target_packet_size;
212 buf += source_packet_size;
220 *picture = s->picture;
221 *data_size = sizeof(AVPicture);
226 static av_cold int decode_init(AVCodecContext *avctx)
228 DPXContext *s = avctx->priv_data;
229 avcodec_get_frame_defaults(&s->picture);
230 avctx->coded_frame = &s->picture;
234 static av_cold int decode_end(AVCodecContext *avctx)
236 DPXContext *s = avctx->priv_data;
237 if (s->picture.data[0])
238 avctx->release_buffer(avctx, &s->picture);
243 AVCodec ff_dpx_decoder = {
245 .type = AVMEDIA_TYPE_VIDEO,
246 .id = AV_CODEC_ID_DPX,
247 .priv_data_size = sizeof(DPXContext),
250 .decode = decode_frame,
251 .capabilities = CODEC_CAP_DR1,
252 .long_name = NULL_IF_CONFIG_SMALL("DPX image"),