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 "libavutil/timecode.h"
27 #include "bytestream.h"
31 static unsigned int read16(const uint8_t **ptr, int is_big)
43 static unsigned int read32(const uint8_t **ptr, int is_big)
55 static uint16_t read10in32_gray(const uint8_t **ptr, uint32_t *lbuf,
56 int *n_datum, int is_big, int shift)
63 *lbuf = read32(ptr, is_big);
67 temp = *lbuf >> shift & 0x3FF;
73 static uint16_t read10in32(const uint8_t **ptr, uint32_t *lbuf,
74 int *n_datum, int is_big, int shift)
79 *lbuf = read32(ptr, is_big);
83 *lbuf = *lbuf << 10 | *lbuf >> shift & 0x3FFFFF;
88 static uint16_t read12in32(const uint8_t **ptr, uint32_t *lbuf,
89 int *n_datum, int is_big)
94 *lbuf = read32(ptr, is_big);
99 case 7: return *lbuf & 0xFFF;
100 case 6: return (*lbuf >> 12) & 0xFFF;
102 uint32_t c = *lbuf >> 24;
103 *lbuf = read32(ptr, is_big);
107 case 4: return (*lbuf >> 4) & 0xFFF;
108 case 3: return (*lbuf >> 16) & 0xFFF;
110 uint32_t c = *lbuf >> 28;
111 *lbuf = read32(ptr, is_big);
115 case 1: return (*lbuf >> 8) & 0xFFF;
116 default: return *lbuf >> 20;
120 static int decode_frame(AVCodecContext *avctx,
125 const uint8_t *buf = avpkt->data;
126 int buf_size = avpkt->size;
127 AVFrame *const p = data;
128 uint8_t *ptr[AV_NUM_DATA_POINTERS];
129 uint32_t header_version, version = 0;
131 char input_device[33];
134 int magic_num, endian;
135 int x, y, stride, i, ret;
136 int w, h, bits_per_color, descriptor, elements, packing;
137 int encoding, need_align = 0;
139 unsigned int rgbBuffer = 0;
142 if (avpkt->size <= 1634) {
143 av_log(avctx, AV_LOG_ERROR, "Packet too small for DPX header\n");
144 return AVERROR_INVALIDDATA;
147 magic_num = AV_RB32(buf);
150 /* Check if the files "magic number" is "SDPX" which means it uses
151 * big-endian or XPDS which is for little-endian files */
152 if (magic_num == AV_RL32("SDPX")) {
154 } else if (magic_num == AV_RB32("SDPX")) {
157 av_log(avctx, AV_LOG_ERROR, "DPX marker not found\n");
158 return AVERROR_INVALIDDATA;
161 offset = read32(&buf, endian);
162 if (avpkt->size <= offset) {
163 av_log(avctx, AV_LOG_ERROR, "Invalid data start offset\n");
164 return AVERROR_INVALIDDATA;
167 header_version = read32(&buf, 0);
168 if (header_version == MKTAG('V','1','.','0'))
170 if (header_version == MKTAG('V','2','.','0'))
173 av_log(avctx, AV_LOG_WARNING, "Unknown header format version %s.\n",
174 av_fourcc2str(header_version));
177 buf = avpkt->data + 660;
178 ret = read32(&buf, endian);
179 if (ret != 0xFFFFFFFF) {
180 avpriv_report_missing_feature(avctx, "Encryption");
181 av_log(avctx, AV_LOG_WARNING, "The image is encrypted and may "
182 "not properly decode.\n");
185 // Need to end in 0x304 offset from start of file
186 buf = avpkt->data + 0x304;
187 w = read32(&buf, endian);
188 h = read32(&buf, endian);
190 if ((ret = ff_set_dimensions(avctx, w, h)) < 0)
193 // Need to end in 0x320 to read the descriptor
197 // Need to end in 0x323 to read the bits per color
199 avctx->bits_per_raw_sample =
200 bits_per_color = buf[0];
202 packing = read16(&buf, endian);
203 encoding = read16(&buf, endian);
206 avpriv_report_missing_feature(avctx, "Encoding %d", encoding);
207 return AVERROR_PATCHWELCOME;
211 avctx->sample_aspect_ratio.num = read32(&buf, endian);
212 avctx->sample_aspect_ratio.den = read32(&buf, endian);
213 if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0)
214 av_reduce(&avctx->sample_aspect_ratio.num, &avctx->sample_aspect_ratio.den,
215 avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den,
218 avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
220 /* preferred frame rate from Motion-picture film header */
221 if (offset >= 1724 + 4) {
222 buf = avpkt->data + 1724;
223 i = read32(&buf, endian);
224 if(i && i != 0xFFFFFFFF) {
225 AVRational q = av_d2q(av_int2float(i), 4096);
226 if (q.num > 0 && q.den > 0)
227 avctx->framerate = q;
231 /* alternative frame rate from television header */
232 if (offset >= 1940 + 4 &&
233 !(avctx->framerate.num && avctx->framerate.den)) {
234 buf = avpkt->data + 1940;
235 i = read32(&buf, endian);
236 if(i && i != 0xFFFFFFFF) {
237 AVRational q = av_d2q(av_int2float(i), 4096);
238 if (q.num > 0 && q.den > 0)
239 avctx->framerate = q;
243 /* SMPTE TC from television header */
244 if (offset >= 1920 + 4) {
247 char tcbuf[AV_TIMECODE_STR_SIZE];
249 buf = avpkt->data + 1920;
250 // read32 to native endian, av_bswap32 to opposite of native for
251 // compatibility with av_timecode_make_smpte_tc_string2 etc
252 tc = av_bswap32(read32(&buf, endian));
254 if (i != 0xFFFFFFFF) {
255 AVFrameSideData *tcside =
256 av_frame_new_side_data(p, AV_FRAME_DATA_S12M_TIMECODE,
257 sizeof(uint32_t) * 4);
259 return AVERROR(ENOMEM);
261 tc_sd = (uint32_t*)tcside->data;
265 av_timecode_make_smpte_tc_string2(tcbuf, avctx->framerate,
267 av_dict_set(&p->metadata, "timecode", tcbuf, 0);
271 switch (descriptor) {
277 case 103: // UYVA4444
288 avpriv_report_missing_feature(avctx, "Descriptor %d", descriptor);
289 return AVERROR_PATCHWELCOME;
292 switch (bits_per_color) {
294 stride = avctx->width * elements;
298 av_log(avctx, AV_LOG_ERROR, "Packing to 32bit required\n");
301 stride = (avctx->width * elements + 2) / 3 * 4;
304 stride = avctx->width * elements;
318 stride = 2 * avctx->width * elements;
323 avpriv_report_missing_feature(avctx, "Depth %d", bits_per_color);
324 return AVERROR_PATCHWELCOME;
326 return AVERROR_INVALIDDATA;
329 // Table 3c: Runs will always break at scan line boundaries. Packing
330 // will always break to the next 32-bit word at scan-line boundaries.
331 // Unfortunately, the encoder produced invalid files, so attempt
333 need_align = FFALIGN(stride, 4);
334 if (need_align*avctx->height + (int64_t)offset > avpkt->size) {
335 // Alignment seems unappliable, try without
336 if (stride*avctx->height + (int64_t)offset > avpkt->size) {
337 av_log(avctx, AV_LOG_ERROR, "Overread buffer. Invalid header?\n");
338 return AVERROR_INVALIDDATA;
340 av_log(avctx, AV_LOG_INFO, "Decoding DPX without scanline "
345 need_align -= stride;
346 stride = FFALIGN(stride, 4);
349 switch (1000 * descriptor + 10 * bits_per_color + endian) {
352 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
356 avctx->pix_fmt = AV_PIX_FMT_GRAY12;
360 avctx->pix_fmt = AV_PIX_FMT_RGB24;
364 avctx->pix_fmt = AV_PIX_FMT_ABGR;
368 avctx->pix_fmt = AV_PIX_FMT_RGBA;
372 avctx->pix_fmt = AV_PIX_FMT_GBRP10;
376 avctx->pix_fmt = AV_PIX_FMT_GBRAP10;
380 avctx->pix_fmt = AV_PIX_FMT_GBRP12;
384 avctx->pix_fmt = AV_PIX_FMT_GBRAP12;
388 avctx->pix_fmt = AV_PIX_FMT_GRAY10;
391 avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
394 avctx->pix_fmt = AV_PIX_FMT_GRAY16LE;
397 avctx->pix_fmt = AV_PIX_FMT_RGB48BE;
400 avctx->pix_fmt = AV_PIX_FMT_RGB48LE;
403 avctx->pix_fmt = AV_PIX_FMT_RGBA64BE;
406 avctx->pix_fmt = AV_PIX_FMT_RGBA64LE;
409 avctx->pix_fmt = AV_PIX_FMT_UYVY422;
412 avctx->pix_fmt = AV_PIX_FMT_YUV444P;
415 avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
418 av_log(avctx, AV_LOG_ERROR, "Unsupported format\n");
419 return AVERROR_PATCHWELCOME;
422 ff_set_sar(avctx, avctx->sample_aspect_ratio);
424 if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
427 av_strlcpy(creator, avpkt->data + 160, 100);
429 av_dict_set(&p->metadata, "Creator", creator, 0);
431 av_strlcpy(input_device, avpkt->data + 1556, 32);
432 input_device[32] = '\0';
433 av_dict_set(&p->metadata, "Input Device", input_device, 0);
435 // Move pointer to offset from start of file
436 buf = avpkt->data + offset;
438 for (i=0; i<AV_NUM_DATA_POINTERS; i++)
441 switch (bits_per_color) {
443 for (x = 0; x < avctx->height; x++) {
444 uint16_t *dst[4] = {(uint16_t*)ptr[0],
448 int shift = elements > 1 ? packing == 1 ? 22 : 20 : packing == 1 ? 2 : 0;
449 for (y = 0; y < avctx->width; y++) {
451 *dst[2]++ = read10in32(&buf, &rgbBuffer,
452 &n_datum, endian, shift);
454 *dst[0]++ = read10in32_gray(&buf, &rgbBuffer,
455 &n_datum, endian, shift);
457 *dst[0]++ = read10in32(&buf, &rgbBuffer,
458 &n_datum, endian, shift);
460 *dst[1]++ = read10in32(&buf, &rgbBuffer,
461 &n_datum, endian, shift);
464 read10in32(&buf, &rgbBuffer,
465 &n_datum, endian, shift);
467 if (memcmp(input_device, "Scanity", 7))
469 for (i = 0; i < elements; i++)
470 ptr[i] += p->linesize[i];
474 for (x = 0; x < avctx->height; x++) {
475 uint16_t *dst[4] = {(uint16_t*)ptr[0],
479 int shift = packing == 1 ? 4 : 0;
480 for (y = 0; y < avctx->width; y++) {
483 *dst[2]++ = read16(&buf, endian) >> shift & 0xFFF;
484 *dst[0]++ = read16(&buf, endian) >> shift & 0xFFF;
486 *dst[1]++ = read16(&buf, endian) >> shift & 0xFFF;
488 *dst[3]++ = read16(&buf, endian) >> shift & 0xFFF;
491 *dst[2]++ = read12in32(&buf, &rgbBuffer,
493 *dst[0]++ = read12in32(&buf, &rgbBuffer,
496 *dst[1]++ = read12in32(&buf, &rgbBuffer,
499 *dst[3]++ = read12in32(&buf, &rgbBuffer,
504 for (i = 0; i < elements; i++)
505 ptr[i] += p->linesize[i];
506 // Jump to next aligned position
513 if ( avctx->pix_fmt == AV_PIX_FMT_YUVA444P
514 || avctx->pix_fmt == AV_PIX_FMT_YUV444P) {
515 for (x = 0; x < avctx->height; x++) {
516 ptr[0] = p->data[0] + x * p->linesize[0];
517 ptr[1] = p->data[1] + x * p->linesize[1];
518 ptr[2] = p->data[2] + x * p->linesize[2];
519 ptr[3] = p->data[3] + x * p->linesize[3];
520 for (y = 0; y < avctx->width; y++) {
524 if (avctx->pix_fmt == AV_PIX_FMT_YUVA444P)
529 av_image_copy_plane(ptr[0], p->linesize[0],
531 elements * avctx->width, avctx->height);
541 AVCodec ff_dpx_decoder = {
543 .long_name = NULL_IF_CONFIG_SMALL("DPX (Digital Picture Exchange) image"),
544 .type = AVMEDIA_TYPE_VIDEO,
545 .id = AV_CODEC_ID_DPX,
546 .decode = decode_frame,
547 .capabilities = AV_CODEC_CAP_DR1,