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
30
45 };
46
50 /* 2 = N/A */
51 /* 3 = N/A */
59 /* 11 = N/A */
60 /* 12 = N/A */
61 };
62
63 static unsigned int read16(
const uint8_t **ptr,
int is_big)
64 {
66 if (is_big) {
68 } else {
70 }
71 *ptr += 2;
73 }
74
75 static unsigned int read32(
const uint8_t **ptr,
int is_big)
76 {
78 if (is_big) {
80 } else {
82 }
83 *ptr += 4;
85 }
86
88 int *n_datum,
int is_big,
int shift)
89 {
91
92 if (*n_datum)
93 (*n_datum)--;
94 else {
95 *lbuf =
read32(ptr, is_big);
96 *n_datum = 2;
97 }
98
100 *lbuf = *lbuf >> 10;
101
103 }
104
105 static uint16_t
read10in32(
const uint8_t **ptr, uint32_t *lbuf,
106 int *n_datum,
int is_big,
int shift)
107 {
108 if (*n_datum)
109 (*n_datum)--;
110 else {
111 *lbuf =
read32(ptr, is_big);
112 *n_datum = 2;
113 }
114
115 *lbuf = *lbuf << 10 | *lbuf >>
shift & 0x3FFFFF;
116
117 return *lbuf & 0x3FF;
118 }
119
120 static uint16_t
read12in32(
const uint8_t **ptr, uint32_t *lbuf,
121 int *n_datum, int is_big)
122 {
123 if (*n_datum)
124 (*n_datum)--;
125 else {
126 *lbuf =
read32(ptr, is_big);
127 *n_datum = 7;
128 }
129
130 switch (*n_datum){
131 case 7: return *lbuf & 0xFFF;
132 case 6: return (*lbuf >> 12) & 0xFFF;
133 case 5: {
134 uint32_t
c = *lbuf >> 24;
135 *lbuf =
read32(ptr, is_big);
138 }
139 case 4: return (*lbuf >> 4) & 0xFFF;
140 case 3: return (*lbuf >> 16) & 0xFFF;
141 case 2: {
142 uint32_t
c = *lbuf >> 28;
143 *lbuf =
read32(ptr, is_big);
146 }
147 case 1: return (*lbuf >> 8) & 0xFFF;
148 default: return *lbuf >> 20;
149 }
150 }
151
154 {
155 const uint8_t *buf = avpkt->
data;
156 int buf_size = avpkt->
size;
158 uint32_t header_version,
version = 0;
159 char creator[101] = { 0 };
160 char input_device[33] = { 0 };
161
163 int magic_num, endian;
165 int w,
h, bits_per_color, descriptor,
elements, packing;
166 int yuv, color_trc, color_spec;
167 int encoding, need_align = 0, unpadded_10bit = 0;
168
169 unsigned int rgbBuffer = 0;
170 int n_datum = 0;
171
172 if (avpkt->
size <= 1634) {
175 }
176
178 buf += 4;
179
180 /* Check if the files "magic number" is "SDPX" which means it uses
181 * big-endian or XPDS which is for little-endian files */
182 if (magic_num ==
AV_RL32(
"SDPX")) {
183 endian = 0;
184 }
else if (magic_num ==
AV_RB32(
"SDPX")) {
185 endian = 1;
186 } else {
189 }
190
195 }
196
197 header_version =
read32(&buf, 0);
198 if (header_version ==
MKTAG(
'V',
'1',
'.',
'0'))
200 if (header_version ==
MKTAG(
'V',
'2',
'.',
'0'))
205
206 // Check encryption
207 buf = avpkt->
data + 660;
209 if (
ret != 0xFFFFFFFF) {
212 "not properly decode.\n");
213 }
214
215 // Need to end in 0x304 offset from start of file
216 buf = avpkt->
data + 0x304;
219
222
223 // Need to end in 0x320 to read the descriptor
224 buf += 20;
225 descriptor = buf[0];
226 color_trc = buf[1];
227 color_spec = buf[2];
228
229 // Need to end in 0x323 to read the bits per color
230 buf += 3;
232 bits_per_color = buf[0];
233 buf++;
234 packing =
read16(&buf, endian);
235 encoding =
read16(&buf, endian);
236
237 if (encoding) {
240 }
241
242 if (bits_per_color > 31)
244
245 buf += 820;
251 0x10000);
252 else
254
255 /* preferred frame rate from Motion-picture film header */
257 buf = avpkt->
data + 1724;
259 if(
i &&
i != 0xFFFFFFFF) {
261 if (q.
num > 0 && q.
den > 0)
263 }
264 }
265
266 /* alternative frame rate from television header */
269 buf = avpkt->
data + 1940;
271 if(
i &&
i != 0xFFFFFFFF) {
273 if (q.
num > 0 && q.
den > 0)
275 }
276 }
277
278 /* SMPTE TC from television header */
281 uint32_t *tc_sd;
283
284 buf = avpkt->
data + 1920;
285 // read32 to native endian, av_bswap32 to opposite of native for
286 // compatibility with av_timecode_make_smpte_tc_string2 etc
288
289 if (
i != 0xFFFFFFFF) {
292 sizeof(uint32_t) * 4, &tcside);
295
296 if (tcside) {
297 tc_sd = (uint32_t*)tcside->
data;
298 tc_sd[0] = 1;
300
302 tc_sd[1], 0, 0);
304 }
305 }
306 }
307
308 /* color range from television header */
310 buf = avpkt->
data + 1952;
312
313 buf = avpkt->
data + 1964;
315
316 if (
i != 0xFFFFFFFF && j != 0xFFFFFFFF) {
317 float minCV, maxCV;
320 if (bits_per_color >= 1 &&
321 minCV == 0.0
f && maxCV == ((1
U<<bits_per_color) - 1)) {
323 } else if (bits_per_color >= 8 &&
324 minCV == (1 <<(bits_per_color - 4)) &&
325 maxCV == (235<<(bits_per_color - 8))) {
327 }
328 }
329 }
330
331 switch (descriptor) {
332 case 1: // R
333 case 2: // G
334 case 3: // B
335 case 4: // A
336 case 6: // Y
338 yuv = 1;
339 break;
340 case 50: // RGB
342 yuv = 0;
343 break;
344 case 52: // ABGR
345 case 51: // RGBA
347 yuv = 0;
348 break;
349 case 100: // UYVY422
351 yuv = 1;
352 break;
353 case 102: // UYV444
355 yuv = 1;
356 break;
357 case 103: // UYVA4444
359 yuv = 1;
360 break;
361 default:
364 }
365
366 switch (bits_per_color) {
367 case 8:
369 break;
370 case 10:
371 if (!packing) {
373 return -1;
374 }
376 break;
377 case 12:
379 if (packing) {
381 } else {
387 }
389 }
390 break;
391 case 16:
393 break;
394 case 32:
396 break;
397 case 1:
398 case 64:
401 default:
403 }
404
405 switch (color_trc) {
408 break;
412 break;
417 break;
420 break;
423 /* Nothing to do */
424 break;
425 default:
427 "%d to color_trc.\n", color_trc);
428 break;
429 }
430
431 switch (color_spec) {
435 break;
439 break;
443 break;
446 /* Nothing to do */
447 break;
448 default:
450 "%d to color_primaries.\n", color_spec);
451 break;
452 }
453
454 if (yuv) {
455 switch (color_spec) {
459 break;
463 break;
467 break;
470 /* Nothing to do */
471 break;
472 default:
474 "%d to colorspace.\n", color_spec);
475 break;
476 }
477 } else {
479 }
480
482 creator[100] = '0円';
484
486 input_device[32] = '0円';
488
489 // Some devices do not pad 10bit samples to whole 32bit words per row
490 if (!memcmp(input_device, "Scanity", 7) ||
491 !memcmp(creator, "Lasergraphics Inc.", 18)) {
492 if (bits_per_color == 10)
493 unpadded_10bit = 1;
494 }
495
496 // Table 3c: Runs will always break at scan line boundaries. Packing
497 // will always break to the next 32-bit word at scan-line boundaries.
498 // Unfortunately, the encoder produced invalid files, so attempt
499 // to detect it
500 // Also handle special case with unpadded content
504 // Alignment seems unappliable, try without
508 } else {
510 "alignment.\n");
511 need_align = 0;
512 }
513 } else {
516 }
517
518 switch (1000 * descriptor + 10 * bits_per_color + endian) {
519 case 1081:
520 case 1080:
521 case 2081:
522 case 2080:
523 case 3081:
524 case 3080:
525 case 4081:
526 case 4080:
527 case 6081:
528 case 6080:
530 break;
531 case 6121:
532 case 6120:
534 break;
535 case 1320:
536 case 2320:
537 case 3320:
538 case 4320:
539 case 6320:
541 break;
542 case 1321:
543 case 2321:
544 case 3321:
545 case 4321:
546 case 6321:
548 break;
549 case 50081:
550 case 50080:
552 break;
553 case 52081:
554 case 52080:
556 break;
557 case 51081:
558 case 51080:
560 break;
561 case 50100:
562 case 50101:
564 break;
565 case 51100:
566 case 51101:
568 break;
569 case 50120:
570 case 50121:
572 break;
573 case 51120:
574 case 51121:
576 break;
577 case 6100:
578 case 6101:
580 break;
581 case 6161:
583 break;
584 case 6160:
586 break;
587 case 50161:
589 break;
590 case 50160:
592 break;
593 case 51161:
595 break;
596 case 51160:
598 break;
599 case 50320:
601 break;
602 case 50321:
604 break;
605 case 51320:
607 break;
608 case 51321:
610 break;
611 case 100081:
613 break;
614 case 102081:
616 break;
617 case 103081:
619 break;
620 default:
622 1000 * descriptor + 10 * bits_per_color + endian);
624 }
625
627
630
631 // Move pointer to offset from start of file
633
636
637 switch (bits_per_color) {
638 case 10:
639 for (x = 0; x < avctx->
height; x++) {
640 uint16_t *dst[4] = {(uint16_t*)ptr[0],
641 (uint16_t*)ptr[1],
642 (uint16_t*)ptr[2],
643 (uint16_t*)ptr[3]};
644 int shift =
elements > 1 ? packing == 1 ? 22 : 20 : packing == 1 ? 2 : 0;
645 for (y = 0; y < avctx->
width; y++) {
648 &n_datum, endian,
shift);
651 &n_datum, endian,
shift);
652 else
654 &n_datum, endian,
shift);
657 &n_datum, endian,
shift);
659 *dst[3]++ =
661 &n_datum, endian,
shift);
662 }
663 if (!unpadded_10bit)
664 n_datum = 0;
667 }
668 break;
669 case 12:
670 for (x = 0; x < avctx->
height; x++) {
671 uint16_t *dst[4] = {(uint16_t*)ptr[0],
672 (uint16_t*)ptr[1],
673 (uint16_t*)ptr[2],
674 (uint16_t*)ptr[3]};
675 int shift = packing == 1 ? 4 : 0;
676 for (y = 0; y < avctx->
width; y++) {
677 if (packing) {
685 } else {
688 &n_datum, endian);
690 &n_datum, endian);
693 &n_datum, endian);
696 &n_datum, endian);
697 }
698 }
699 n_datum = 0;
702 // Jump to next aligned position
703 buf += need_align;
704 }
705 break;
706 case 32:
711 } else {
712 for (y = 0; y < avctx->
height; y++) {
717 for (x = 0; x < avctx->
width; x++) {
724 buf += 4;
725 ptr[3] += 4;
726 }
727
728 buf += 12;
729 ptr[2] += 4;
730 ptr[0] += 4;
731 ptr[1] += 4;
732 }
733 }
734 }
735 break;
736 case 16:
738 case 8:
741 for (x = 0; x < avctx->
height; x++) {
746 for (y = 0; y < avctx->
width; y++) {
747 *ptr[1]++ = *buf++;
748 *ptr[0]++ = *buf++;
749 *ptr[2]++ = *buf++;
751 *ptr[3]++ = *buf++;
752 }
753 }
754 } else {
758 }
759 break;
760 }
761
762 *got_frame = 1;
763
764 return buf_size;
765 }
766
774 };