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 read16(const uint8_t **ptr, int is_big)
40 static unsigned int read32(const uint8_t **ptr, int is_big)
52 static uint16_t read10in32(const uint8_t **ptr, uint32_t * lbuf,
53 int * n_datum, int is_big)
58 *lbuf = read32(ptr, is_big);
62 *lbuf = (*lbuf << 10) | (*lbuf >> 22);
67 static int decode_frame(AVCodecContext *avctx,
72 const uint8_t *buf = avpkt->data;
73 int buf_size = avpkt->size;
74 AVFrame *const p = data;
75 uint8_t *ptr[AV_NUM_DATA_POINTERS];
78 int magic_num, endian;
80 int w, h, bits_per_color, descriptor, elements, packing, total_size;
83 unsigned int rgbBuffer = 0;
86 if (avpkt->size <= 1634) {
87 av_log(avctx, AV_LOG_ERROR, "Packet too small for DPX header\n");
88 return AVERROR_INVALIDDATA;
91 magic_num = AV_RB32(buf);
94 /* Check if the files "magic number" is "SDPX" which means it uses
95 * big-endian or XPDS which is for little-endian files */
96 if (magic_num == AV_RL32("SDPX")) {
98 } else if (magic_num == AV_RB32("SDPX")) {
101 av_log(avctx, AV_LOG_ERROR, "DPX marker not found\n");
102 return AVERROR_INVALIDDATA;
105 offset = read32(&buf, endian);
106 if (avpkt->size <= offset) {
107 av_log(avctx, AV_LOG_ERROR, "Invalid data start offset\n");
108 return AVERROR_INVALIDDATA;
110 // Need to end in 0x304 offset from start of file
111 buf = avpkt->data + 0x304;
112 w = read32(&buf, endian);
113 h = read32(&buf, endian);
115 if ((ret = ff_set_dimensions(avctx, w, h)) < 0)
118 // Need to end in 0x320 to read the descriptor
122 // Need to end in 0x323 to read the bits per color
124 avctx->bits_per_raw_sample =
125 bits_per_color = buf[0];
127 packing = read16(&buf, endian);
128 encoding = read16(&buf, endian);
131 avpriv_report_missing_feature(avctx, "Packing %d", packing);
132 return AVERROR_PATCHWELCOME;
135 avpriv_report_missing_feature(avctx, "Encoding %d", encoding);
136 return AVERROR_PATCHWELCOME;
140 avctx->sample_aspect_ratio.num = read32(&buf, endian);
141 avctx->sample_aspect_ratio.den = read32(&buf, endian);
142 if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0)
143 av_reduce(&avctx->sample_aspect_ratio.num, &avctx->sample_aspect_ratio.den,
144 avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den,
147 avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
149 switch (descriptor) {
161 avpriv_report_missing_feature(avctx, "Descriptor %d", descriptor);
162 return AVERROR_PATCHWELCOME;
165 switch (bits_per_color) {
167 total_size = avctx->width * avctx->height * elements;
171 av_log(avctx, AV_LOG_ERROR, "Packing to 32bit required\n");
174 total_size = (avctx->width * elements + 2) / 3 * 4 * avctx->height;
178 av_log(avctx, AV_LOG_ERROR, "Packing to 16bit required\n");
181 total_size = 2 * avctx->width * avctx->height * elements;
184 total_size = 2 * avctx->width * avctx->height * elements;
189 avpriv_report_missing_feature(avctx, "Depth %d", bits_per_color);
190 return AVERROR_PATCHWELCOME;
192 return AVERROR_INVALIDDATA;
195 switch (1000 * descriptor + 10 * bits_per_color + endian) {
198 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
202 avctx->pix_fmt = AV_PIX_FMT_RGB24;
206 avctx->pix_fmt = AV_PIX_FMT_ABGR;
210 avctx->pix_fmt = AV_PIX_FMT_RGBA;
216 avctx->pix_fmt = AV_PIX_FMT_GBRP10;
222 avctx->pix_fmt = AV_PIX_FMT_GBRP12;
225 avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
228 avctx->pix_fmt = AV_PIX_FMT_GRAY16LE;
231 avctx->pix_fmt = AV_PIX_FMT_RGB48BE;
234 avctx->pix_fmt = AV_PIX_FMT_RGB48LE;
237 avctx->pix_fmt = AV_PIX_FMT_RGBA64BE;
240 avctx->pix_fmt = AV_PIX_FMT_RGBA64LE;
243 av_log(avctx, AV_LOG_ERROR, "Unsupported format\n");
244 return AVERROR_PATCHWELCOME;
247 if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
250 // Move pointer to offset from start of file
251 buf = avpkt->data + offset;
253 for (i=0; i<AV_NUM_DATA_POINTERS; i++)
256 if (total_size + (int64_t)offset > avpkt->size) {
257 av_log(avctx, AV_LOG_ERROR, "Overread buffer. Invalid header?\n");
258 return AVERROR_INVALIDDATA;
260 switch (bits_per_color) {
262 for (x = 0; x < avctx->height; x++) {
263 uint16_t *dst[3] = {(uint16_t*)ptr[0],
266 for (y = 0; y < avctx->width; y++) {
267 *dst[2]++ = read10in32(&buf, &rgbBuffer,
269 *dst[0]++ = read10in32(&buf, &rgbBuffer,
271 *dst[1]++ = read10in32(&buf, &rgbBuffer,
273 // For 10 bit, ignore alpha
275 read10in32(&buf, &rgbBuffer,
279 for (i = 0; i < 3; i++)
280 ptr[i] += p->linesize[i];
284 for (x = 0; x < avctx->height; x++) {
285 uint16_t *dst[3] = {(uint16_t*)ptr[0],
288 for (y = 0; y < avctx->width; y++) {
289 *dst[2] = read16(&buf, endian) >> 4;
291 *dst[0] = read16(&buf, endian) >> 4;
293 *dst[1] = read16(&buf, endian) >> 4;
295 // For 12 bit, ignore alpha
299 for (i = 0; i < 3; i++)
300 ptr[i] += p->linesize[i];
306 av_image_copy_plane(ptr[0], p->linesize[0],
307 buf, elements * avctx->width,
308 elements * avctx->width, avctx->height);
317 AVCodec ff_dpx_decoder = {
319 .long_name = NULL_IF_CONFIG_SMALL("DPX (Digital Picture Exchange) image"),
320 .type = AVMEDIA_TYPE_VIDEO,
321 .id = AV_CODEC_ID_DPX,
322 .decode = decode_frame,
323 .capabilities = CODEC_CAP_DR1,