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;
80 int x, y, stride, i, ret;
81 int w, h, bits_per_color, descriptor, elements, packing;
82 int encoding, need_align = 0;
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;
113 buf = avpkt->data + 660;
114 ret = read32(&buf, endian);
115 if (ret != 0xFFFFFFFF) {
116 avpriv_report_missing_feature(avctx, "Encryption");
117 av_log(avctx, AV_LOG_WARNING, "The image is encrypted and may "
118 "not properly decode.\n");
121 // Need to end in 0x304 offset from start of file
122 buf = avpkt->data + 0x304;
123 w = read32(&buf, endian);
124 h = read32(&buf, endian);
126 if ((ret = ff_set_dimensions(avctx, w, h)) < 0)
129 // Need to end in 0x320 to read the descriptor
133 // Need to end in 0x323 to read the bits per color
135 avctx->bits_per_raw_sample =
136 bits_per_color = buf[0];
138 packing = read16(&buf, endian);
139 encoding = read16(&buf, endian);
142 avpriv_report_missing_feature(avctx, "Packing %d", packing);
143 return AVERROR_PATCHWELCOME;
146 avpriv_report_missing_feature(avctx, "Encoding %d", encoding);
147 return AVERROR_PATCHWELCOME;
151 avctx->sample_aspect_ratio.num = read32(&buf, endian);
152 avctx->sample_aspect_ratio.den = read32(&buf, endian);
153 if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0)
154 av_reduce(&avctx->sample_aspect_ratio.num, &avctx->sample_aspect_ratio.den,
155 avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den,
158 avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
160 if (offset >= 1724 + 4) {
161 buf = avpkt->data + 1724;
162 i = read32(&buf, endian);
164 AVRational q = av_d2q(av_int2float(i), 4096);
165 if (q.num > 0 && q.den > 0)
166 avctx->framerate = q;
170 switch (descriptor) {
176 case 103: // UYVA4444
187 avpriv_report_missing_feature(avctx, "Descriptor %d", descriptor);
188 return AVERROR_PATCHWELCOME;
191 switch (bits_per_color) {
193 stride = avctx->width * elements;
197 av_log(avctx, AV_LOG_ERROR, "Packing to 32bit required\n");
200 stride = (avctx->width * elements + 2) / 3 * 4;
204 av_log(avctx, AV_LOG_ERROR, "Packing to 16bit required\n");
207 stride = 2 * avctx->width * elements;
210 stride = 2 * avctx->width * elements;
215 avpriv_report_missing_feature(avctx, "Depth %d", bits_per_color);
216 return AVERROR_PATCHWELCOME;
218 return AVERROR_INVALIDDATA;
221 // Table 3c: Runs will always break at scan line boundaries. Packing
222 // will always break to the next 32-bit word at scan-line boundaries.
223 // Unfortunately, the encoder produced invalid files, so attempt
225 need_align = FFALIGN(stride, 4);
226 if (need_align*avctx->height + (int64_t)offset > avpkt->size) {
227 // Alignment seems unappliable, try without
228 if (stride*avctx->height + (int64_t)offset > avpkt->size) {
229 av_log(avctx, AV_LOG_ERROR, "Overread buffer. Invalid header?\n");
230 return AVERROR_INVALIDDATA;
232 av_log(avctx, AV_LOG_INFO, "Decoding DPX without scanline "
237 need_align -= stride;
238 stride = FFALIGN(stride, 4);
241 switch (1000 * descriptor + 10 * bits_per_color + endian) {
244 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
248 avctx->pix_fmt = AV_PIX_FMT_GRAY12;
252 avctx->pix_fmt = AV_PIX_FMT_RGB24;
256 avctx->pix_fmt = AV_PIX_FMT_ABGR;
260 avctx->pix_fmt = AV_PIX_FMT_RGBA;
264 avctx->pix_fmt = AV_PIX_FMT_GBRP10;
268 avctx->pix_fmt = AV_PIX_FMT_GBRAP10;
272 avctx->pix_fmt = AV_PIX_FMT_GBRP12;
276 avctx->pix_fmt = AV_PIX_FMT_GBRAP12;
279 avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
282 avctx->pix_fmt = AV_PIX_FMT_GRAY16LE;
285 avctx->pix_fmt = AV_PIX_FMT_RGB48BE;
288 avctx->pix_fmt = AV_PIX_FMT_RGB48LE;
291 avctx->pix_fmt = AV_PIX_FMT_RGBA64BE;
294 avctx->pix_fmt = AV_PIX_FMT_RGBA64LE;
297 avctx->pix_fmt = AV_PIX_FMT_UYVY422;
300 avctx->pix_fmt = AV_PIX_FMT_YUV444P;
303 avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
306 av_log(avctx, AV_LOG_ERROR, "Unsupported format\n");
307 return AVERROR_PATCHWELCOME;
310 ff_set_sar(avctx, avctx->sample_aspect_ratio);
312 if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
315 // Move pointer to offset from start of file
316 buf = avpkt->data + offset;
318 for (i=0; i<AV_NUM_DATA_POINTERS; i++)
321 switch (bits_per_color) {
323 for (x = 0; x < avctx->height; x++) {
324 uint16_t *dst[4] = {(uint16_t*)ptr[0],
328 for (y = 0; y < avctx->width; y++) {
329 *dst[2]++ = read10in32(&buf, &rgbBuffer,
331 *dst[0]++ = read10in32(&buf, &rgbBuffer,
333 *dst[1]++ = read10in32(&buf, &rgbBuffer,
337 read10in32(&buf, &rgbBuffer,
341 for (i = 0; i < elements; i++)
342 ptr[i] += p->linesize[i];
346 for (x = 0; x < avctx->height; x++) {
347 uint16_t *dst[4] = {(uint16_t*)ptr[0],
351 for (y = 0; y < avctx->width; y++) {
353 *dst[2]++ = read16(&buf, endian) >> 4;
354 *dst[0] = read16(&buf, endian) >> 4;
357 *dst[1]++ = read16(&buf, endian) >> 4;
359 *dst[3]++ = read16(&buf, endian) >> 4;
361 for (i = 0; i < elements; i++)
362 ptr[i] += p->linesize[i];
363 // Jump to next aligned position
370 if ( avctx->pix_fmt == AV_PIX_FMT_YUVA444P
371 || avctx->pix_fmt == AV_PIX_FMT_YUV444P) {
372 for (x = 0; x < avctx->height; x++) {
373 ptr[0] = p->data[0] + x * p->linesize[0];
374 ptr[1] = p->data[1] + x * p->linesize[1];
375 ptr[2] = p->data[2] + x * p->linesize[2];
376 ptr[3] = p->data[3] + x * p->linesize[3];
377 for (y = 0; y < avctx->width; y++) {
381 if (avctx->pix_fmt == AV_PIX_FMT_YUVA444P)
386 av_image_copy_plane(ptr[0], p->linesize[0],
388 elements * avctx->width, avctx->height);
398 AVCodec ff_dpx_decoder = {
400 .long_name = NULL_IF_CONFIG_SMALL("DPX (Digital Picture Exchange) image"),
401 .type = AVMEDIA_TYPE_VIDEO,
402 .id = AV_CODEC_ID_DPX,
403 .decode = decode_frame,
404 .capabilities = AV_CODEC_CAP_DR1,