1 /*
2 * DPX (.dpx) image decoder
3 * Copyright (c) 2009 Jimmy Christensen
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
31
46 };
47
51 /* 2 = N/A */
52 /* 3 = N/A */
60 /* 11 = N/A */
61 /* 12 = N/A */
62 };
63
64 static unsigned int read16(
const uint8_t **ptr,
int is_big)
65 {
67 if (is_big) {
69 } else {
71 }
72 *ptr += 2;
74 }
75
76 static unsigned int read32(
const uint8_t **ptr,
int is_big)
77 {
79 if (is_big) {
81 } else {
83 }
84 *ptr += 4;
86 }
87
89 int *n_datum,
int is_big,
int shift)
90 {
92
93 if (*n_datum)
94 (*n_datum)--;
95 else {
96 *lbuf =
read32(ptr, is_big);
97 *n_datum = 2;
98 }
99
101 *lbuf = *lbuf >> 10;
102
104 }
105
106 static uint16_t
read10in32(
const uint8_t **ptr, uint32_t *lbuf,
107 int *n_datum,
int is_big,
int shift)
108 {
109 if (*n_datum)
110 (*n_datum)--;
111 else {
112 *lbuf =
read32(ptr, is_big);
113 *n_datum = 2;
114 }
115
116 *lbuf = *lbuf << 10 | *lbuf >>
shift & 0x3FFFFF;
117
118 return *lbuf & 0x3FF;
119 }
120
121 static uint16_t
read12in32(
const uint8_t **ptr, uint32_t *lbuf,
122 int *n_datum, int is_big)
123 {
124 if (*n_datum)
125 (*n_datum)--;
126 else {
127 *lbuf =
read32(ptr, is_big);
128 *n_datum = 7;
129 }
130
131 switch (*n_datum){
132 case 7: return *lbuf & 0xFFF;
133 case 6: return (*lbuf >> 12) & 0xFFF;
134 case 5: {
135 uint32_t
c = *lbuf >> 24;
136 *lbuf =
read32(ptr, is_big);
139 }
140 case 4: return (*lbuf >> 4) & 0xFFF;
141 case 3: return (*lbuf >> 16) & 0xFFF;
142 case 2: {
143 uint32_t
c = *lbuf >> 28;
144 *lbuf =
read32(ptr, is_big);
147 }
148 case 1: return (*lbuf >> 8) & 0xFFF;
149 default: return *lbuf >> 20;
150 }
151 }
152
155 {
156 const uint8_t *buf = avpkt->
data;
157 int buf_size = avpkt->
size;
159 uint32_t header_version,
version = 0;
160 char creator[101] = { 0 };
161 char input_device[33] = { 0 };
162
164 int magic_num, endian;
166 int w,
h, bits_per_color, descriptor,
elements, packing;
167 int yuv, color_trc, color_spec;
168 int encoding, need_align = 0, unpadded_10bit = 0;
169
170 unsigned int rgbBuffer = 0;
171 int n_datum = 0;
172
173 if (avpkt->
size <= 1634) {
176 }
177
179 buf += 4;
180
181 /* Check if the files "magic number" is "SDPX" which means it uses
182 * big-endian or XPDS which is for little-endian files */
183 if (magic_num ==
AV_RL32(
"SDPX")) {
184 endian = 0;
185 }
else if (magic_num ==
AV_RB32(
"SDPX")) {
186 endian = 1;
187 } else {
190 }
191
196 }
197
198 header_version =
read32(&buf, 0);
199 if (header_version ==
MKTAG(
'V',
'1',
'.',
'0'))
201 if (header_version ==
MKTAG(
'V',
'2',
'.',
'0'))
206
207 // Check encryption
208 buf = avpkt->
data + 660;
210 if (
ret != 0xFFFFFFFF) {
213 "not properly decode.\n");
214 }
215
216 // Need to end in 0x304 offset from start of file
217 buf = avpkt->
data + 0x304;
220
223
224 // Need to end in 0x320 to read the descriptor
225 buf += 20;
226 descriptor = buf[0];
227 color_trc = buf[1];
228 color_spec = buf[2];
229
230 // Need to end in 0x323 to read the bits per color
231 buf += 3;
233 bits_per_color = buf[0];
234 buf++;
235 packing =
read16(&buf, endian);
236 encoding =
read16(&buf, endian);
237
238 if (encoding) {
241 }
242
243 if (bits_per_color > 31)
245
246 buf += 820;
252 0x10000);
253 else
255
256 /* preferred frame rate from Motion-picture film header */
258 buf = avpkt->
data + 1724;
260 if(
i &&
i != 0xFFFFFFFF) {
262 if (q.
num > 0 && q.
den > 0)
264 }
265 }
266
267 /* alternative frame rate from television header */
270 buf = avpkt->
data + 1940;
272 if(
i &&
i != 0xFFFFFFFF) {
274 if (q.
num > 0 && q.
den > 0)
276 }
277 }
278
279 /* SMPTE TC from television header */
282 uint32_t *tc_sd;
284
285 buf = avpkt->
data + 1920;
286 // read32 to native endian, av_bswap32 to opposite of native for
287 // compatibility with av_timecode_make_smpte_tc_string2 etc
289
290 if (
i != 0xFFFFFFFF) {
293 sizeof(uint32_t) * 4);
294 if (!tcside)
296
297 tc_sd = (uint32_t*)tcside->
data;
298 tc_sd[0] = 1;
300
302 tc_sd[1], 0, 0);
304 }
305 }
306
307 /* color range from television header */
309 buf = avpkt->
data + 1952;
311
312 buf = avpkt->
data + 1964;
314
315 if (
i != 0xFFFFFFFF && j != 0xFFFFFFFF) {
316 float minCV, maxCV;
319 if (bits_per_color >= 1 &&
320 minCV == 0.0
f && maxCV == ((1
U<<bits_per_color) - 1)) {
322 } else if (bits_per_color >= 8 &&
323 minCV == (1 <<(bits_per_color - 4)) &&
324 maxCV == (235<<(bits_per_color - 8))) {
326 }
327 }
328 }
329
330 switch (descriptor) {
331 case 1: // R
332 case 2: // G
333 case 3: // B
334 case 4: // A
335 case 6: // Y
337 yuv = 1;
338 break;
339 case 50: // RGB
341 yuv = 0;
342 break;
343 case 52: // ABGR
344 case 51: // RGBA
346 yuv = 0;
347 break;
348 case 100: // UYVY422
350 yuv = 1;
351 break;
352 case 102: // UYV444
354 yuv = 1;
355 break;
356 case 103: // UYVA4444
358 yuv = 1;
359 break;
360 default:
363 }
364
365 switch (bits_per_color) {
366 case 8:
368 break;
369 case 10:
370 if (!packing) {
372 return -1;
373 }
375 break;
376 case 12:
378 if (packing) {
380 } else {
386 }
388 }
389 break;
390 case 16:
392 break;
393 case 32:
395 break;
396 case 1:
397 case 64:
400 default:
402 }
403
404 switch (color_trc) {
407 break;
411 break;
416 break;
419 break;
422 /* Nothing to do */
423 break;
424 default:
426 "%d to color_trc.\n", color_trc);
427 break;
428 }
429
430 switch (color_spec) {
434 break;
438 break;
442 break;
445 /* Nothing to do */
446 break;
447 default:
449 "%d to color_primaries.\n", color_spec);
450 break;
451 }
452
453 if (yuv) {
454 switch (color_spec) {
458 break;
462 break;
466 break;
469 /* Nothing to do */
470 break;
471 default:
473 "%d to colorspace.\n", color_spec);
474 break;
475 }
476 } else {
478 }
479
480 // Table 3c: Runs will always break at scan line boundaries. Packing
481 // will always break to the next 32-bit word at scan-line boundaries.
482 // Unfortunately, the encoder produced invalid files, so attempt
483 // to detect it
486 // Alignment seems unappliable, try without
490 } else {
492 "alignment.\n");
493 need_align = 0;
494 }
495 } else {
498 }
499
500 switch (1000 * descriptor + 10 * bits_per_color + endian) {
501 case 1081:
502 case 1080:
503 case 2081:
504 case 2080:
505 case 3081:
506 case 3080:
507 case 4081:
508 case 4080:
509 case 6081:
510 case 6080:
512 break;
513 case 6121:
514 case 6120:
516 break;
517 case 1320:
518 case 2320:
519 case 3320:
520 case 4320:
521 case 6320:
523 break;
524 case 1321:
525 case 2321:
526 case 3321:
527 case 4321:
528 case 6321:
530 break;
531 case 50081:
532 case 50080:
534 break;
535 case 52081:
536 case 52080:
538 break;
539 case 51081:
540 case 51080:
542 break;
543 case 50100:
544 case 50101:
546 break;
547 case 51100:
548 case 51101:
550 break;
551 case 50120:
552 case 50121:
554 break;
555 case 51120:
556 case 51121:
558 break;
559 case 6100:
560 case 6101:
562 break;
563 case 6161:
565 break;
566 case 6160:
568 break;
569 case 50161:
571 break;
572 case 50160:
574 break;
575 case 51161:
577 break;
578 case 51160:
580 break;
581 case 50320:
583 break;
584 case 50321:
586 break;
587 case 51320:
589 break;
590 case 51321:
592 break;
593 case 100081:
595 break;
596 case 102081:
598 break;
599 case 103081:
601 break;
602 default:
604 1000 * descriptor + 10 * bits_per_color + endian);
606 }
607
609
612
614 creator[100] = '0円';
616
618 input_device[32] = '0円';
620
621 // Some devices do not pad 10bit samples to whole 32bit words per row
622 if (!memcmp(input_device, "Scanity", 7) ||
623 !memcmp(creator, "Lasergraphics Inc.", 18)) {
624 unpadded_10bit = 1;
625 }
626
627 // Move pointer to offset from start of file
629
632
633 switch (bits_per_color) {
634 case 10:
635 for (x = 0; x < avctx->
height; x++) {
636 uint16_t *dst[4] = {(uint16_t*)ptr[0],
637 (uint16_t*)ptr[1],
638 (uint16_t*)ptr[2],
639 (uint16_t*)ptr[3]};
640 int shift =
elements > 1 ? packing == 1 ? 22 : 20 : packing == 1 ? 2 : 0;
641 for (y = 0; y < avctx->
width; y++) {
644 &n_datum, endian,
shift);
647 &n_datum, endian,
shift);
648 else
650 &n_datum, endian,
shift);
653 &n_datum, endian,
shift);
655 *dst[3]++ =
657 &n_datum, endian,
shift);
658 }
659 if (!unpadded_10bit)
660 n_datum = 0;
663 }
664 break;
665 case 12:
666 for (x = 0; x < avctx->
height; x++) {
667 uint16_t *dst[4] = {(uint16_t*)ptr[0],
668 (uint16_t*)ptr[1],
669 (uint16_t*)ptr[2],
670 (uint16_t*)ptr[3]};
671 int shift = packing == 1 ? 4 : 0;
672 for (y = 0; y < avctx->
width; y++) {
673 if (packing) {
681 } else {
684 &n_datum, endian);
686 &n_datum, endian);
689 &n_datum, endian);
692 &n_datum, endian);
693 }
694 }
695 n_datum = 0;
698 // Jump to next aligned position
699 buf += need_align;
700 }
701 break;
702 case 32:
707 } else {
708 for (y = 0; y < avctx->
height; y++) {
713 for (x = 0; x < avctx->
width; x++) {
720 buf += 4;
721 ptr[3] += 4;
722 }
723
724 buf += 12;
725 ptr[2] += 4;
726 ptr[0] += 4;
727 ptr[1] += 4;
728 }
729 }
730 }
731 break;
732 case 16:
734 case 8:
737 for (x = 0; x < avctx->
height; x++) {
742 for (y = 0; y < avctx->
width; y++) {
743 *ptr[1]++ = *buf++;
744 *ptr[0]++ = *buf++;
745 *ptr[2]++ = *buf++;
747 *ptr[3]++ = *buf++;
748 }
749 }
750 } else {
754 }
755 break;
756 }
757
758 *got_frame = 1;
759
760 return buf_size;
761 }
762
770 };