2 * QuickTime palette handling
3 * Copyright (c) 2001 Fabrice Bellard
4 * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
5 * Copyright (c) 2015 Mats Peterson
7 * This file is part of FFmpeg.
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 #include "libavutil/intreadwrite.h"
29 #include "qtpalette.h"
31 int ff_get_qtpalette(int codec_id
, AVIOContext
*pb
, uint32_t *palette
)
33 int tmp
, bit_depth
, color_table_id
, greyscale
, i
;
35 avio_seek(pb
, 82, SEEK_CUR
);
37 /* Get the bit depth and greyscale state */
39 bit_depth
= tmp
& 0x1F;
40 greyscale
= tmp
& 0x20;
42 /* Get the color table ID */
43 color_table_id
= avio_rb16(pb
);
45 /* Do not create a greyscale palette for Cinepak */
46 if (greyscale
&& codec_id
== AV_CODEC_ID_CINEPAK
)
49 /* If the depth is 1, 2, 4, or 8 bpp, file is palettized. */
50 if ((bit_depth
== 1 || bit_depth
== 2 || bit_depth
== 4 || bit_depth
== 8)) {
51 uint32_t color_count
, color_start
, color_end
;
54 /* Ignore the greyscale bit for 1-bit video and sample
55 * descriptions containing a color table. */
56 if (greyscale
&& bit_depth
> 1 && color_table_id
) {
57 int color_index
, color_dec
;
58 /* compute the greyscale palette */
59 color_count
= 1 << bit_depth
;
61 color_dec
= 256 / (color_count
- 1);
62 for (i
= 0; i
< color_count
; i
++) {
63 r
= g
= b
= color_index
;
64 palette
[i
] = (0xFFU
<< 24) | (r
<< 16) | (g
<< 8) | (b
);
65 color_index
-= color_dec
;
69 } else if (color_table_id
) {
70 /* The color table ID is non-zero. Interpret this as
71 * being -1, which means use the default Macintosh
73 const uint8_t *color_table
;
74 color_count
= 1 << bit_depth
;
76 color_table
= ff_qt_default_palette_2
;
77 else if (bit_depth
== 2)
78 color_table
= ff_qt_default_palette_4
;
79 else if (bit_depth
== 4)
80 color_table
= ff_qt_default_palette_16
;
82 color_table
= ff_qt_default_palette_256
;
83 for (i
= 0; i
< color_count
; i
++) {
84 r
= color_table
[i
* 3 + 0];
85 g
= color_table
[i
* 3 + 1];
86 b
= color_table
[i
* 3 + 2];
87 palette
[i
] = (0xFFU
<< 24) | (r
<< 16) | (g
<< 8) | (b
);
90 /* The color table ID is 0; the color table is in the sample
92 color_start
= avio_rb32(pb
);
93 avio_rb16(pb
); /* color table flags */
94 color_end
= avio_rb16(pb
);
95 if ((color_start
<= 255) && (color_end
<= 255)) {
96 for (i
= color_start
; i
<= color_end
; i
++) {
97 /* each A, R, G, or B component is 16 bits;
98 * only use the top 8 bits */
107 palette
[i
] = (a
<< 24 ) | (r
<< 16) | (g
<< 8) | (b
);