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"
28 static unsigned int read32(const uint8_t **ptr, int is_big)
40 static uint16_t read10in32(const uint8_t **ptr, uint32_t * lbuf,
41 int * n_datum, int is_big)
46 *lbuf = read32(ptr, is_big);
50 *lbuf = (*lbuf << 10) | (*lbuf >> 22);
55 static int decode_frame(AVCodecContext *avctx,
60 const uint8_t *buf = avpkt->data;
61 int buf_size = avpkt->size;
62 AVFrame *const p = data;
63 uint8_t *ptr[AV_NUM_DATA_POINTERS];
66 int magic_num, endian;
68 int w, h, bits_per_color, descriptor, elements, packing, total_size;
70 unsigned int rgbBuffer = 0;
73 if (avpkt->size <= 1634) {
74 av_log(avctx, AV_LOG_ERROR, "Packet too small for DPX header\n");
75 return AVERROR_INVALIDDATA;
78 magic_num = AV_RB32(buf);
81 /* Check if the files "magic number" is "SDPX" which means it uses
82 * big-endian or XPDS which is for little-endian files */
83 if (magic_num == AV_RL32("SDPX")) {
85 } else if (magic_num == AV_RB32("SDPX")) {
88 av_log(avctx, AV_LOG_ERROR, "DPX marker not found\n");
89 return AVERROR_INVALIDDATA;
92 offset = read32(&buf, endian);
93 if (avpkt->size <= offset) {
94 av_log(avctx, AV_LOG_ERROR, "Invalid data start offset\n");
95 return AVERROR_INVALIDDATA;
97 // Need to end in 0x304 offset from start of file
98 buf = avpkt->data + 0x304;
99 w = read32(&buf, endian);
100 h = read32(&buf, endian);
101 if ((ret = av_image_check_size(w, h, 0, avctx)) < 0)
104 if (w != avctx->width || h != avctx->height)
105 avcodec_set_dimensions(avctx, w, h);
107 // Need to end in 0x320 to read the descriptor
111 // Need to end in 0x323 to read the bits per color
113 avctx->bits_per_raw_sample =
114 bits_per_color = buf[0];
116 packing = *((uint16_t*)buf);
119 avctx->sample_aspect_ratio.num = read32(&buf, endian);
120 avctx->sample_aspect_ratio.den = read32(&buf, endian);
121 if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0)
122 av_reduce(&avctx->sample_aspect_ratio.num, &avctx->sample_aspect_ratio.den,
123 avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den,
126 avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
128 switch (descriptor) {
136 av_log(avctx, AV_LOG_ERROR, "Unsupported descriptor %d\n", descriptor);
137 return AVERROR_INVALIDDATA;
140 switch (bits_per_color) {
143 avctx->pix_fmt = AV_PIX_FMT_RGBA;
145 avctx->pix_fmt = AV_PIX_FMT_RGB24;
147 total_size = avctx->width * avctx->height * elements;
151 av_log(avctx, AV_LOG_ERROR, "Packing to 32bit required\n");
154 avctx->pix_fmt = AV_PIX_FMT_GBRP10;
155 total_size = (avctx->width * elements + 2) / 3 * 4 * avctx->height;
159 av_log(avctx, AV_LOG_ERROR, "Packing to 16bit required\n");
163 avctx->pix_fmt = AV_PIX_FMT_GBRP12BE;
165 avctx->pix_fmt = AV_PIX_FMT_GBRP12LE;
167 total_size = 2 * avctx->width * avctx->height * elements;
171 avctx->pix_fmt = elements == 4 ? AV_PIX_FMT_RGBA64BE : AV_PIX_FMT_RGB48BE;
173 avctx->pix_fmt = elements == 4 ? AV_PIX_FMT_RGBA64LE : AV_PIX_FMT_RGB48LE;
175 total_size = 2 * avctx->width * avctx->height * elements;
178 av_log(avctx, AV_LOG_ERROR, "Unsupported color depth : %d\n", bits_per_color);
179 return AVERROR_INVALIDDATA;
182 if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
185 // Move pointer to offset from start of file
186 buf = avpkt->data + offset;
188 for (i=0; i<AV_NUM_DATA_POINTERS; i++)
191 if (total_size + (int64_t)offset > avpkt->size) {
192 av_log(avctx, AV_LOG_ERROR, "Overread buffer. Invalid header?\n");
193 return AVERROR_INVALIDDATA;
195 switch (bits_per_color) {
197 for (x = 0; x < avctx->height; x++) {
198 uint16_t *dst[3] = {(uint16_t*)ptr[0],
201 for (y = 0; y < avctx->width; y++) {
202 *dst[2]++ = read10in32(&buf, &rgbBuffer,
204 *dst[0]++ = read10in32(&buf, &rgbBuffer,
206 *dst[1]++ = read10in32(&buf, &rgbBuffer,
208 // For 10 bit, ignore alpha
210 read10in32(&buf, &rgbBuffer,
214 for (i = 0; i < 3; i++)
215 ptr[i] += p->linesize[i];
219 for (x = 0; x < avctx->height; x++) {
220 uint16_t *dst[3] = {(uint16_t*)ptr[0],
223 for (y = 0; y < avctx->width; y++) {
224 *dst[2] = *((uint16_t*)buf);
225 *dst[2] = (*dst[2] >> 4) | (*dst[2] << 12);
228 *dst[0] = *((uint16_t*)buf);
229 *dst[0] = (*dst[0] >> 4) | (*dst[0] << 12);
232 *dst[1] = *((uint16_t*)buf);
233 *dst[1] = (*dst[1] >> 4) | (*dst[1] << 12);
236 // For 12 bit, ignore alpha
240 for (i = 0; i < 3; i++)
241 ptr[i] += p->linesize[i];
247 for (x = 0; x < avctx->height; x++) {
248 memcpy(ptr[0], buf, elements*avctx->width);
249 ptr[0] += p->linesize[0];
250 buf += elements*avctx->width;
260 AVCodec ff_dpx_decoder = {
262 .type = AVMEDIA_TYPE_VIDEO,
263 .id = AV_CODEC_ID_DPX,
264 .decode = decode_frame,
265 .long_name = NULL_IF_CONFIG_SMALL("DPX image"),
266 .capabilities = CODEC_CAP_DR1,