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/avstring.h"
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/intfloat.h"
25 #include "libavutil/imgutils.h"
26 #include "bytestream.h"
30 static unsigned int read16(const uint8_t **ptr, int is_big)
42 static unsigned int read32(const uint8_t **ptr, int is_big)
54 static uint16_t read10in32_gray(const uint8_t **ptr, uint32_t *lbuf,
55 int *n_datum, int is_big, int shift)
62 *lbuf = read32(ptr, is_big);
66 temp = *lbuf >> shift & 0x3FF;
72 static uint16_t read10in32(const uint8_t **ptr, uint32_t *lbuf,
73 int *n_datum, int is_big, int shift)
78 *lbuf = read32(ptr, is_big);
82 *lbuf = *lbuf << 10 | *lbuf >> shift & 0x3FFFFF;
87 static uint16_t read12in32(const uint8_t **ptr, uint32_t *lbuf,
88 int *n_datum, int is_big)
93 *lbuf = read32(ptr, is_big);
98 case 7: return *lbuf & 0xFFF;
99 case 6: return (*lbuf >> 12) & 0xFFF;
101 uint32_t c = *lbuf >> 24;
102 *lbuf = read32(ptr, is_big);
106 case 4: return (*lbuf >> 4) & 0xFFF;
107 case 3: return (*lbuf >> 16) & 0xFFF;
109 uint32_t c = *lbuf >> 28;
110 *lbuf = read32(ptr, is_big);
114 case 1: return (*lbuf >> 8) & 0xFFF;
115 default: return *lbuf >> 20;
119 static int decode_frame(AVCodecContext *avctx,
124 const uint8_t *buf = avpkt->data;
125 int buf_size = avpkt->size;
126 AVFrame *const p = data;
127 uint8_t *ptr[AV_NUM_DATA_POINTERS];
128 uint32_t header_version, version = 0;
130 char input_device[33];
133 int magic_num, endian;
134 int x, y, stride, i, ret;
135 int w, h, bits_per_color, descriptor, elements, packing;
136 int encoding, need_align = 0;
138 unsigned int rgbBuffer = 0;
141 if (avpkt->size <= 1634) {
142 av_log(avctx, AV_LOG_ERROR, "Packet too small for DPX header\n");
143 return AVERROR_INVALIDDATA;
146 magic_num = AV_RB32(buf);
149 /* Check if the files "magic number" is "SDPX" which means it uses
150 * big-endian or XPDS which is for little-endian files */
151 if (magic_num == AV_RL32("SDPX")) {
153 } else if (magic_num == AV_RB32("SDPX")) {
156 av_log(avctx, AV_LOG_ERROR, "DPX marker not found\n");
157 return AVERROR_INVALIDDATA;
160 offset = read32(&buf, endian);
161 if (avpkt->size <= offset) {
162 av_log(avctx, AV_LOG_ERROR, "Invalid data start offset\n");
163 return AVERROR_INVALIDDATA;
166 header_version = read32(&buf, 0);
167 if (header_version == MKTAG('V','1','.','0'))
169 if (header_version == MKTAG('V','2','.','0'))
172 av_log(avctx, AV_LOG_WARNING, "Unknown header format version %s.\n",
173 av_fourcc2str(header_version));
176 buf = avpkt->data + 660;
177 ret = read32(&buf, endian);
178 if (ret != 0xFFFFFFFF) {
179 avpriv_report_missing_feature(avctx, "Encryption");
180 av_log(avctx, AV_LOG_WARNING, "The image is encrypted and may "
181 "not properly decode.\n");
184 // Need to end in 0x304 offset from start of file
185 buf = avpkt->data + 0x304;
186 w = read32(&buf, endian);
187 h = read32(&buf, endian);
189 if ((ret = ff_set_dimensions(avctx, w, h)) < 0)
192 // Need to end in 0x320 to read the descriptor
196 // Need to end in 0x323 to read the bits per color
198 avctx->bits_per_raw_sample =
199 bits_per_color = buf[0];
201 packing = read16(&buf, endian);
202 encoding = read16(&buf, endian);
205 avpriv_report_missing_feature(avctx, "Encoding %d", encoding);
206 return AVERROR_PATCHWELCOME;
210 avctx->sample_aspect_ratio.num = read32(&buf, endian);
211 avctx->sample_aspect_ratio.den = read32(&buf, endian);
212 if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0)
213 av_reduce(&avctx->sample_aspect_ratio.num, &avctx->sample_aspect_ratio.den,
214 avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den,
217 avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
219 if (offset >= 1724 + 4) {
220 buf = avpkt->data + 1724;
221 i = read32(&buf, endian);
223 AVRational q = av_d2q(av_int2float(i), 4096);
224 if (q.num > 0 && q.den > 0)
225 avctx->framerate = q;
229 switch (descriptor) {
235 case 103: // UYVA4444
246 avpriv_report_missing_feature(avctx, "Descriptor %d", descriptor);
247 return AVERROR_PATCHWELCOME;
250 switch (bits_per_color) {
252 stride = avctx->width * elements;
256 av_log(avctx, AV_LOG_ERROR, "Packing to 32bit required\n");
259 stride = (avctx->width * elements + 2) / 3 * 4;
262 stride = avctx->width * elements;
276 stride = 2 * avctx->width * elements;
281 avpriv_report_missing_feature(avctx, "Depth %d", bits_per_color);
282 return AVERROR_PATCHWELCOME;
284 return AVERROR_INVALIDDATA;
287 // Table 3c: Runs will always break at scan line boundaries. Packing
288 // will always break to the next 32-bit word at scan-line boundaries.
289 // Unfortunately, the encoder produced invalid files, so attempt
291 need_align = FFALIGN(stride, 4);
292 if (need_align*avctx->height + (int64_t)offset > avpkt->size) {
293 // Alignment seems unappliable, try without
294 if (stride*avctx->height + (int64_t)offset > avpkt->size) {
295 av_log(avctx, AV_LOG_ERROR, "Overread buffer. Invalid header?\n");
296 return AVERROR_INVALIDDATA;
298 av_log(avctx, AV_LOG_INFO, "Decoding DPX without scanline "
303 need_align -= stride;
304 stride = FFALIGN(stride, 4);
307 switch (1000 * descriptor + 10 * bits_per_color + endian) {
310 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
314 avctx->pix_fmt = AV_PIX_FMT_GRAY12;
318 avctx->pix_fmt = AV_PIX_FMT_RGB24;
322 avctx->pix_fmt = AV_PIX_FMT_ABGR;
326 avctx->pix_fmt = AV_PIX_FMT_RGBA;
330 avctx->pix_fmt = AV_PIX_FMT_GBRP10;
334 avctx->pix_fmt = AV_PIX_FMT_GBRAP10;
338 avctx->pix_fmt = AV_PIX_FMT_GBRP12;
342 avctx->pix_fmt = AV_PIX_FMT_GBRAP12;
346 avctx->pix_fmt = AV_PIX_FMT_GRAY10;
349 avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
352 avctx->pix_fmt = AV_PIX_FMT_GRAY16LE;
355 avctx->pix_fmt = AV_PIX_FMT_RGB48BE;
358 avctx->pix_fmt = AV_PIX_FMT_RGB48LE;
361 avctx->pix_fmt = AV_PIX_FMT_RGBA64BE;
364 avctx->pix_fmt = AV_PIX_FMT_RGBA64LE;
367 avctx->pix_fmt = AV_PIX_FMT_UYVY422;
370 avctx->pix_fmt = AV_PIX_FMT_YUV444P;
373 avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
376 av_log(avctx, AV_LOG_ERROR, "Unsupported format\n");
377 return AVERROR_PATCHWELCOME;
380 ff_set_sar(avctx, avctx->sample_aspect_ratio);
382 if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
385 av_strlcpy(creator, avpkt->data + 160, 100);
387 av_dict_set(&p->metadata, "Creator", creator, 0);
389 av_strlcpy(input_device, avpkt->data + 1556, 32);
390 input_device[32] = '\0';
391 av_dict_set(&p->metadata, "Input Device", input_device, 0);
393 // Move pointer to offset from start of file
394 buf = avpkt->data + offset;
396 for (i=0; i<AV_NUM_DATA_POINTERS; i++)
399 switch (bits_per_color) {
401 for (x = 0; x < avctx->height; x++) {
402 uint16_t *dst[4] = {(uint16_t*)ptr[0],
406 int shift = elements > 1 ? packing == 1 ? 22 : 20 : packing == 1 ? 2 : 0;
407 for (y = 0; y < avctx->width; y++) {
409 *dst[2]++ = read10in32(&buf, &rgbBuffer,
410 &n_datum, endian, shift);
412 *dst[0]++ = read10in32_gray(&buf, &rgbBuffer,
413 &n_datum, endian, shift);
415 *dst[0]++ = read10in32(&buf, &rgbBuffer,
416 &n_datum, endian, shift);
418 *dst[1]++ = read10in32(&buf, &rgbBuffer,
419 &n_datum, endian, shift);
422 read10in32(&buf, &rgbBuffer,
423 &n_datum, endian, shift);
425 if (memcmp(input_device, "Scanity", 7))
427 for (i = 0; i < elements; i++)
428 ptr[i] += p->linesize[i];
432 for (x = 0; x < avctx->height; x++) {
433 uint16_t *dst[4] = {(uint16_t*)ptr[0],
437 int shift = packing == 1 ? 4 : 0;
438 for (y = 0; y < avctx->width; y++) {
441 *dst[2]++ = read16(&buf, endian) >> shift & 0xFFF;
442 *dst[0]++ = read16(&buf, endian) >> shift & 0xFFF;
444 *dst[1]++ = read16(&buf, endian) >> shift & 0xFFF;
446 *dst[3]++ = read16(&buf, endian) >> shift & 0xFFF;
449 *dst[2]++ = read12in32(&buf, &rgbBuffer,
451 *dst[0]++ = read12in32(&buf, &rgbBuffer,
454 *dst[1]++ = read12in32(&buf, &rgbBuffer,
457 *dst[3]++ = read12in32(&buf, &rgbBuffer,
462 for (i = 0; i < elements; i++)
463 ptr[i] += p->linesize[i];
464 // Jump to next aligned position
471 if ( avctx->pix_fmt == AV_PIX_FMT_YUVA444P
472 || avctx->pix_fmt == AV_PIX_FMT_YUV444P) {
473 for (x = 0; x < avctx->height; x++) {
474 ptr[0] = p->data[0] + x * p->linesize[0];
475 ptr[1] = p->data[1] + x * p->linesize[1];
476 ptr[2] = p->data[2] + x * p->linesize[2];
477 ptr[3] = p->data[3] + x * p->linesize[3];
478 for (y = 0; y < avctx->width; y++) {
482 if (avctx->pix_fmt == AV_PIX_FMT_YUVA444P)
487 av_image_copy_plane(ptr[0], p->linesize[0],
489 elements * avctx->width, avctx->height);
499 AVCodec ff_dpx_decoder = {
501 .long_name = NULL_IF_CONFIG_SMALL("DPX (Digital Picture Exchange) image"),
502 .type = AVMEDIA_TYPE_VIDEO,
503 .id = AV_CODEC_ID_DPX,
504 .decode = decode_frame,
505 .capabilities = AV_CODEC_CAP_DR1,