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 "libavcore/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 int buf_size = avpkt->size;
59 DPXContext *const s = avctx->priv_data;
60 AVFrame *picture = data;
61 AVFrame *const p = &s->picture;
64 int magic_num, offset, endian;
66 int w, h, stride, bits_per_color, descriptor, elements, target_packet_size, source_packet_size;
68 unsigned int rgbBuffer;
70 magic_num = AV_RB32(buf);
73 /* Check if the files "magic number" is "SDPX" which means it uses
74 * big-endian or XPDS which is for little-endian files */
75 if (magic_num == AV_RL32("SDPX")) {
77 } else if (magic_num == AV_RB32("SDPX")) {
80 av_log(avctx, AV_LOG_ERROR, "DPX marker not found\n");
84 offset = read32(&buf, endian);
85 // Need to end in 0x304 offset from start of file
86 buf = avpkt->data + 0x304;
87 w = read32(&buf, endian);
88 h = read32(&buf, endian);
90 // Need to end in 0x320 to read the descriptor
94 // Need to end in 0x323 to read the bits per color
96 avctx->bits_per_raw_sample =
97 bits_per_color = buf[0];
107 av_log(avctx, AV_LOG_ERROR, "Unsupported descriptor %d\n", descriptor);
111 switch (bits_per_color) {
114 avctx->pix_fmt = PIX_FMT_RGBA;
116 avctx->pix_fmt = PIX_FMT_RGB24;
118 source_packet_size = elements;
119 target_packet_size = elements;
122 avctx->pix_fmt = PIX_FMT_RGB48;
123 target_packet_size = 6;
124 source_packet_size = elements * 2;
129 avctx->pix_fmt = PIX_FMT_RGB48BE;
131 avctx->pix_fmt = PIX_FMT_RGB48LE;
133 target_packet_size = 6;
134 source_packet_size = elements * 2;
137 av_log(avctx, AV_LOG_ERROR, "Unsupported color depth : %d\n", bits_per_color);
141 if (s->picture.data[0])
142 avctx->release_buffer(avctx, &s->picture);
143 if (av_image_check_size(w, h, 0, avctx))
145 if (w != avctx->width || h != avctx->height)
146 avcodec_set_dimensions(avctx, w, h);
147 if (avctx->get_buffer(avctx, p) < 0) {
148 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
152 // Move pointer to offset from start of file
153 buf = avpkt->data + offset;
156 stride = p->linesize[0];
158 switch (bits_per_color) {
160 for (x = 0; x < avctx->height; x++) {
161 uint16_t *dst = (uint16_t*)ptr;
162 for (y = 0; y < avctx->width; y++) {
163 rgbBuffer = read32(&buf, endian);
164 // Read out the 10-bit colors and convert to 16-bit
165 *dst++ = make_16bit(rgbBuffer >> 16);
166 *dst++ = make_16bit(rgbBuffer >> 6);
167 *dst++ = make_16bit(rgbBuffer << 4);
173 case 12: // Treat 12-bit as 16-bit
175 if (source_packet_size == target_packet_size) {
176 for (x = 0; x < avctx->height; x++) {
177 memcpy(ptr, buf, target_packet_size*avctx->width);
179 buf += source_packet_size*avctx->width;
182 for (x = 0; x < avctx->height; x++) {
184 for (y = 0; y < avctx->width; y++) {
185 memcpy(dst, buf, target_packet_size);
186 dst += target_packet_size;
187 buf += source_packet_size;
195 *picture = s->picture;
196 *data_size = sizeof(AVPicture);
201 static av_cold int decode_init(AVCodecContext *avctx)
203 DPXContext *s = avctx->priv_data;
204 avcodec_get_frame_defaults(&s->picture);
205 avctx->coded_frame = &s->picture;
209 static av_cold int decode_end(AVCodecContext *avctx)
211 DPXContext *s = avctx->priv_data;
212 if (s->picture.data[0])
213 avctx->release_buffer(avctx, &s->picture);
218 AVCodec dpx_decoder = {
229 .long_name = NULL_IF_CONFIG_SMALL("DPX image"),