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/intfloat.h"
24 #include "libavutil/imgutils.h"
25 #include "bytestream.h"
29 static unsigned int read16(const uint8_t **ptr, int is_big)
41 static unsigned int read32(const uint8_t **ptr, int is_big)
53 static uint16_t read10in32(const uint8_t **ptr, uint32_t * lbuf,
54 int * n_datum, int is_big)
59 *lbuf = read32(ptr, is_big);
63 *lbuf = (*lbuf << 10) | (*lbuf >> 22);
68 static int decode_frame(AVCodecContext *avctx,
73 const uint8_t *buf = avpkt->data;
74 int buf_size = avpkt->size;
75 AVFrame *const p = data;
76 uint8_t *ptr[AV_NUM_DATA_POINTERS];
79 int magic_num, endian;
81 int w, h, bits_per_color, descriptor, elements, packing, total_size;
84 unsigned int rgbBuffer = 0;
87 if (avpkt->size <= 1634) {
88 av_log(avctx, AV_LOG_ERROR, "Packet too small for DPX header\n");
89 return AVERROR_INVALIDDATA;
92 magic_num = AV_RB32(buf);
95 /* Check if the files "magic number" is "SDPX" which means it uses
96 * big-endian or XPDS which is for little-endian files */
97 if (magic_num == AV_RL32("SDPX")) {
99 } else if (magic_num == AV_RB32("SDPX")) {
102 av_log(avctx, AV_LOG_ERROR, "DPX marker not found\n");
103 return AVERROR_INVALIDDATA;
106 offset = read32(&buf, endian);
107 if (avpkt->size <= offset) {
108 av_log(avctx, AV_LOG_ERROR, "Invalid data start offset\n");
109 return AVERROR_INVALIDDATA;
111 // Need to end in 0x304 offset from start of file
112 buf = avpkt->data + 0x304;
113 w = read32(&buf, endian);
114 h = read32(&buf, endian);
116 if ((ret = ff_set_dimensions(avctx, w, h)) < 0)
119 // Need to end in 0x320 to read the descriptor
123 // Need to end in 0x323 to read the bits per color
125 avctx->bits_per_raw_sample =
126 bits_per_color = buf[0];
128 packing = read16(&buf, endian);
129 encoding = read16(&buf, endian);
132 avpriv_report_missing_feature(avctx, "Packing %d", packing);
133 return AVERROR_PATCHWELCOME;
136 avpriv_report_missing_feature(avctx, "Encoding %d", encoding);
137 return AVERROR_PATCHWELCOME;
141 avctx->sample_aspect_ratio.num = read32(&buf, endian);
142 avctx->sample_aspect_ratio.den = read32(&buf, endian);
143 if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0)
144 av_reduce(&avctx->sample_aspect_ratio.num, &avctx->sample_aspect_ratio.den,
145 avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den,
148 avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
150 if (offset >= 1724 + 4) {
151 buf = avpkt->data + 1724;
152 i = read32(&buf, endian);
154 AVRational q = av_d2q(av_int2float(i), 4096);
155 if (q.num > 0 && q.den > 0)
156 avctx->time_base = av_inv_q(q);
160 switch (descriptor) {
172 avpriv_report_missing_feature(avctx, "Descriptor %d", descriptor);
173 return AVERROR_PATCHWELCOME;
176 switch (bits_per_color) {
178 total_size = avctx->width * avctx->height * elements;
182 av_log(avctx, AV_LOG_ERROR, "Packing to 32bit required\n");
185 total_size = (avctx->width * elements + 2) / 3 * 4 * avctx->height;
189 av_log(avctx, AV_LOG_ERROR, "Packing to 16bit required\n");
192 total_size = 2 * avctx->width * avctx->height * elements;
195 total_size = 2 * avctx->width * avctx->height * elements;
200 avpriv_report_missing_feature(avctx, "Depth %d", bits_per_color);
201 return AVERROR_PATCHWELCOME;
203 return AVERROR_INVALIDDATA;
206 switch (1000 * descriptor + 10 * bits_per_color + endian) {
209 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
213 avctx->pix_fmt = AV_PIX_FMT_RGB24;
217 avctx->pix_fmt = AV_PIX_FMT_ABGR;
221 avctx->pix_fmt = AV_PIX_FMT_RGBA;
227 avctx->pix_fmt = AV_PIX_FMT_GBRP10;
233 avctx->pix_fmt = AV_PIX_FMT_GBRP12;
236 avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
239 avctx->pix_fmt = AV_PIX_FMT_GRAY16LE;
242 avctx->pix_fmt = AV_PIX_FMT_RGB48BE;
245 avctx->pix_fmt = AV_PIX_FMT_RGB48LE;
248 avctx->pix_fmt = AV_PIX_FMT_RGBA64BE;
251 avctx->pix_fmt = AV_PIX_FMT_RGBA64LE;
254 av_log(avctx, AV_LOG_ERROR, "Unsupported format\n");
255 return AVERROR_PATCHWELCOME;
258 if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
261 // Move pointer to offset from start of file
262 buf = avpkt->data + offset;
264 for (i=0; i<AV_NUM_DATA_POINTERS; i++)
267 if (total_size + (int64_t)offset > avpkt->size) {
268 av_log(avctx, AV_LOG_ERROR, "Overread buffer. Invalid header?\n");
269 return AVERROR_INVALIDDATA;
271 switch (bits_per_color) {
273 for (x = 0; x < avctx->height; x++) {
274 uint16_t *dst[3] = {(uint16_t*)ptr[0],
277 for (y = 0; y < avctx->width; y++) {
278 *dst[2]++ = read10in32(&buf, &rgbBuffer,
280 *dst[0]++ = read10in32(&buf, &rgbBuffer,
282 *dst[1]++ = read10in32(&buf, &rgbBuffer,
284 // For 10 bit, ignore alpha
286 read10in32(&buf, &rgbBuffer,
290 for (i = 0; i < 3; i++)
291 ptr[i] += p->linesize[i];
295 for (x = 0; x < avctx->height; x++) {
296 uint16_t *dst[3] = {(uint16_t*)ptr[0],
299 for (y = 0; y < avctx->width; y++) {
300 *dst[2] = read16(&buf, endian) >> 4;
302 *dst[0] = read16(&buf, endian) >> 4;
304 *dst[1] = read16(&buf, endian) >> 4;
306 // For 12 bit, ignore alpha
310 for (i = 0; i < 3; i++)
311 ptr[i] += p->linesize[i];
317 av_image_copy_plane(ptr[0], p->linesize[0],
318 buf, elements * avctx->width,
319 elements * avctx->width, avctx->height);
328 AVCodec ff_dpx_decoder = {
330 .long_name = NULL_IF_CONFIG_SMALL("DPX (Digital Picture Exchange) image"),
331 .type = AVMEDIA_TYPE_VIDEO,
332 .id = AV_CODEC_ID_DPX,
333 .decode = decode_frame,
334 .capabilities = CODEC_CAP_DR1,