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);
114 if ((ret = av_image_check_size(w, h, 0, avctx)) < 0)
117 if (w != avctx->width || h != avctx->height)
118 avcodec_set_dimensions(avctx, w, h);
120 // Need to end in 0x320 to read the descriptor
124 // Need to end in 0x323 to read the bits per color
126 avctx->bits_per_raw_sample =
127 bits_per_color = buf[0];
129 packing = read16(&buf, endian);
130 encoding = read16(&buf, endian);
133 avpriv_report_missing_feature(avctx, "Packing %d", packing);
134 return AVERROR_PATCHWELCOME;
137 avpriv_report_missing_feature(avctx, "Encoding %d", encoding);
138 return AVERROR_PATCHWELCOME;
142 avctx->sample_aspect_ratio.num = read32(&buf, endian);
143 avctx->sample_aspect_ratio.den = read32(&buf, endian);
144 if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0)
145 av_reduce(&avctx->sample_aspect_ratio.num, &avctx->sample_aspect_ratio.den,
146 avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den,
149 avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
151 switch (descriptor) {
163 avpriv_report_missing_feature(avctx, "Descriptor %d", descriptor);
164 return AVERROR_PATCHWELCOME;
167 switch (bits_per_color) {
169 total_size = avctx->width * avctx->height * elements;
173 av_log(avctx, AV_LOG_ERROR, "Packing to 32bit required\n");
176 total_size = (avctx->width * elements + 2) / 3 * 4 * avctx->height;
180 av_log(avctx, AV_LOG_ERROR, "Packing to 16bit required\n");
183 total_size = 2 * avctx->width * avctx->height * elements;
186 total_size = 2 * avctx->width * avctx->height * elements;
191 avpriv_report_missing_feature(avctx, "Depth %d", bits_per_color);
192 return AVERROR_PATCHWELCOME;
194 return AVERROR_INVALIDDATA;
197 switch (1000 * descriptor + 10 * bits_per_color + endian) {
200 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
204 avctx->pix_fmt = AV_PIX_FMT_RGB24;
208 avctx->pix_fmt = AV_PIX_FMT_ABGR;
212 avctx->pix_fmt = AV_PIX_FMT_RGBA;
218 avctx->pix_fmt = AV_PIX_FMT_GBRP10;
224 avctx->pix_fmt = AV_PIX_FMT_GBRP12;
227 avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
230 avctx->pix_fmt = AV_PIX_FMT_GRAY16LE;
233 avctx->pix_fmt = AV_PIX_FMT_RGB48BE;
236 avctx->pix_fmt = AV_PIX_FMT_RGB48LE;
239 avctx->pix_fmt = AV_PIX_FMT_RGBA64BE;
242 avctx->pix_fmt = AV_PIX_FMT_RGBA64LE;
245 av_log(avctx, AV_LOG_ERROR, "Unsupported format\n");
246 return AVERROR_PATCHWELCOME;
249 if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
252 // Move pointer to offset from start of file
253 buf = avpkt->data + offset;
255 for (i=0; i<AV_NUM_DATA_POINTERS; i++)
258 if (total_size + (int64_t)offset > avpkt->size) {
259 av_log(avctx, AV_LOG_ERROR, "Overread buffer. Invalid header?\n");
260 return AVERROR_INVALIDDATA;
262 switch (bits_per_color) {
264 for (x = 0; x < avctx->height; x++) {
265 uint16_t *dst[3] = {(uint16_t*)ptr[0],
268 for (y = 0; y < avctx->width; y++) {
269 *dst[2]++ = read10in32(&buf, &rgbBuffer,
271 *dst[0]++ = read10in32(&buf, &rgbBuffer,
273 *dst[1]++ = read10in32(&buf, &rgbBuffer,
275 // For 10 bit, ignore alpha
277 read10in32(&buf, &rgbBuffer,
281 for (i = 0; i < 3; i++)
282 ptr[i] += p->linesize[i];
286 for (x = 0; x < avctx->height; x++) {
287 uint16_t *dst[3] = {(uint16_t*)ptr[0],
290 for (y = 0; y < avctx->width; y++) {
291 *dst[2] = read16(&buf, endian) >> 4;
293 *dst[0] = read16(&buf, endian) >> 4;
295 *dst[1] = read16(&buf, endian) >> 4;
297 // For 12 bit, ignore alpha
301 for (i = 0; i < 3; i++)
302 ptr[i] += p->linesize[i];
308 av_image_copy_plane(ptr[0], p->linesize[0],
309 buf, elements * avctx->width,
310 elements * avctx->width, avctx->height);
319 AVCodec ff_dpx_decoder = {
321 .long_name = NULL_IF_CONFIG_SMALL("DPX (Digital Picture Exchange) image"),
322 .type = AVMEDIA_TYPE_VIDEO,
323 .id = AV_CODEC_ID_DPX,
324 .decode = decode_frame,
325 .capabilities = CODEC_CAP_DR1,