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, j, 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 /* color range from television header */
272 if (offset >= 1964 + 4) {
273 buf = avpkt->data + 1952;
274 i = read32(&buf, endian);
276 buf = avpkt->data + 1964;
277 j = read32(&buf, endian);
279 if (i != 0xFFFFFFFF && j != 0xFFFFFFFF) {
281 minCV = av_int2float(i);
282 maxCV = av_int2float(j);
283 if (bits_per_color >= 1 &&
284 minCV == 0.0f && maxCV == ((1<<bits_per_color) - 1)) {
285 avctx->color_range = AVCOL_RANGE_JPEG;
286 } else if (bits_per_color >= 8 &&
287 minCV == (1 <<(bits_per_color - 4)) &&
288 maxCV == (235<<(bits_per_color - 8))) {
289 avctx->color_range = AVCOL_RANGE_MPEG;
294 switch (descriptor) {
300 case 103: // UYVA4444
311 avpriv_report_missing_feature(avctx, "Descriptor %d", descriptor);
312 return AVERROR_PATCHWELCOME;
315 switch (bits_per_color) {
317 stride = avctx->width * elements;
321 av_log(avctx, AV_LOG_ERROR, "Packing to 32bit required\n");
324 stride = (avctx->width * elements + 2) / 3 * 4;
327 stride = avctx->width * elements;
341 stride = 2 * avctx->width * elements;
346 avpriv_report_missing_feature(avctx, "Depth %d", bits_per_color);
347 return AVERROR_PATCHWELCOME;
349 return AVERROR_INVALIDDATA;
352 // Table 3c: Runs will always break at scan line boundaries. Packing
353 // will always break to the next 32-bit word at scan-line boundaries.
354 // Unfortunately, the encoder produced invalid files, so attempt
356 need_align = FFALIGN(stride, 4);
357 if (need_align*avctx->height + (int64_t)offset > avpkt->size) {
358 // Alignment seems unappliable, try without
359 if (stride*avctx->height + (int64_t)offset > avpkt->size) {
360 av_log(avctx, AV_LOG_ERROR, "Overread buffer. Invalid header?\n");
361 return AVERROR_INVALIDDATA;
363 av_log(avctx, AV_LOG_INFO, "Decoding DPX without scanline "
368 need_align -= stride;
369 stride = FFALIGN(stride, 4);
372 switch (1000 * descriptor + 10 * bits_per_color + endian) {
375 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
379 avctx->pix_fmt = AV_PIX_FMT_GRAY12;
383 avctx->pix_fmt = AV_PIX_FMT_RGB24;
387 avctx->pix_fmt = AV_PIX_FMT_ABGR;
391 avctx->pix_fmt = AV_PIX_FMT_RGBA;
395 avctx->pix_fmt = AV_PIX_FMT_GBRP10;
399 avctx->pix_fmt = AV_PIX_FMT_GBRAP10;
403 avctx->pix_fmt = AV_PIX_FMT_GBRP12;
407 avctx->pix_fmt = AV_PIX_FMT_GBRAP12;
411 avctx->pix_fmt = AV_PIX_FMT_GRAY10;
414 avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
417 avctx->pix_fmt = AV_PIX_FMT_GRAY16LE;
420 avctx->pix_fmt = AV_PIX_FMT_RGB48BE;
423 avctx->pix_fmt = AV_PIX_FMT_RGB48LE;
426 avctx->pix_fmt = AV_PIX_FMT_RGBA64BE;
429 avctx->pix_fmt = AV_PIX_FMT_RGBA64LE;
432 avctx->pix_fmt = AV_PIX_FMT_UYVY422;
435 avctx->pix_fmt = AV_PIX_FMT_YUV444P;
438 avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
441 av_log(avctx, AV_LOG_ERROR, "Unsupported format\n");
442 return AVERROR_PATCHWELCOME;
445 ff_set_sar(avctx, avctx->sample_aspect_ratio);
447 if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
450 av_strlcpy(creator, avpkt->data + 160, 100);
452 av_dict_set(&p->metadata, "Creator", creator, 0);
454 av_strlcpy(input_device, avpkt->data + 1556, 32);
455 input_device[32] = '\0';
456 av_dict_set(&p->metadata, "Input Device", input_device, 0);
458 // Move pointer to offset from start of file
459 buf = avpkt->data + offset;
461 for (i=0; i<AV_NUM_DATA_POINTERS; i++)
464 switch (bits_per_color) {
466 for (x = 0; x < avctx->height; x++) {
467 uint16_t *dst[4] = {(uint16_t*)ptr[0],
471 int shift = elements > 1 ? packing == 1 ? 22 : 20 : packing == 1 ? 2 : 0;
472 for (y = 0; y < avctx->width; y++) {
474 *dst[2]++ = read10in32(&buf, &rgbBuffer,
475 &n_datum, endian, shift);
477 *dst[0]++ = read10in32_gray(&buf, &rgbBuffer,
478 &n_datum, endian, shift);
480 *dst[0]++ = read10in32(&buf, &rgbBuffer,
481 &n_datum, endian, shift);
483 *dst[1]++ = read10in32(&buf, &rgbBuffer,
484 &n_datum, endian, shift);
487 read10in32(&buf, &rgbBuffer,
488 &n_datum, endian, shift);
490 if (memcmp(input_device, "Scanity", 7))
492 for (i = 0; i < elements; i++)
493 ptr[i] += p->linesize[i];
497 for (x = 0; x < avctx->height; x++) {
498 uint16_t *dst[4] = {(uint16_t*)ptr[0],
502 int shift = packing == 1 ? 4 : 0;
503 for (y = 0; y < avctx->width; y++) {
506 *dst[2]++ = read16(&buf, endian) >> shift & 0xFFF;
507 *dst[0]++ = read16(&buf, endian) >> shift & 0xFFF;
509 *dst[1]++ = read16(&buf, endian) >> shift & 0xFFF;
511 *dst[3]++ = read16(&buf, endian) >> shift & 0xFFF;
514 *dst[2]++ = read12in32(&buf, &rgbBuffer,
516 *dst[0]++ = read12in32(&buf, &rgbBuffer,
519 *dst[1]++ = read12in32(&buf, &rgbBuffer,
522 *dst[3]++ = read12in32(&buf, &rgbBuffer,
527 for (i = 0; i < elements; i++)
528 ptr[i] += p->linesize[i];
529 // Jump to next aligned position
536 if ( avctx->pix_fmt == AV_PIX_FMT_YUVA444P
537 || avctx->pix_fmt == AV_PIX_FMT_YUV444P) {
538 for (x = 0; x < avctx->height; x++) {
539 ptr[0] = p->data[0] + x * p->linesize[0];
540 ptr[1] = p->data[1] + x * p->linesize[1];
541 ptr[2] = p->data[2] + x * p->linesize[2];
542 ptr[3] = p->data[3] + x * p->linesize[3];
543 for (y = 0; y < avctx->width; y++) {
547 if (avctx->pix_fmt == AV_PIX_FMT_YUVA444P)
552 av_image_copy_plane(ptr[0], p->linesize[0],
554 elements * avctx->width, avctx->height);
564 AVCodec ff_dpx_decoder = {
566 .long_name = NULL_IF_CONFIG_SMALL("DPX (Digital Picture Exchange) image"),
567 .type = AVMEDIA_TYPE_VIDEO,
568 .id = AV_CODEC_ID_DPX,
569 .decode = decode_frame,
570 .capabilities = AV_CODEC_CAP_DR1,