]> git.sesse.net Git - ffmpeg/blob - libavformat/qtpalette.c
Merge commit '90b1b9350c0a97c4065ae9054b83e57f48a0de1f'
[ffmpeg] / libavformat / qtpalette.c
1 /*
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
6  *
7  * This file is part of FFmpeg.
8  *
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.
13  *
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.
18  *
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
22  */
23
24 #include <stdio.h>
25 #include <stdint.h>
26
27 #include "avformat.h"
28 #include "libavutil/intreadwrite.h"
29 #include "qtpalette.h"
30
31 int ff_get_qtpalette(int codec_id, AVIOContext *pb, uint32_t *palette)
32 {
33     int tmp, bit_depth, color_table_id, greyscale, i;
34
35     avio_seek(pb, 82, SEEK_CUR);
36
37     /* Get the bit depth and greyscale state */
38     tmp = avio_rb16(pb);
39     bit_depth = tmp & 0x1F;
40     greyscale = tmp & 0x20;
41
42     /* Get the color table ID */
43     color_table_id = avio_rb16(pb);
44
45     /* Do not create a greyscale palette for Cinepak */
46     if (greyscale && codec_id == AV_CODEC_ID_CINEPAK)
47         return 0;
48
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         int color_count, color_start, color_end;
52         uint32_t a, r, g, b;
53
54         if (greyscale) {
55             int color_index, color_dec;
56             /* compute the greyscale palette */
57             color_count = 1 << bit_depth;
58             color_index = 255;
59             color_dec   = 256 / (color_count - 1);
60             for (i = 0; i < color_count; i++) {
61                 r = g = b = color_index;
62                 palette[i] = (0xFFU << 24) | (r << 16) | (g << 8) | (b);
63                 color_index -= color_dec;
64                 if (color_index < 0)
65                     color_index = 0;
66             }
67         } else if (color_table_id) {
68             /* The color table ID is non-zero. Interpret this as
69              * being -1, which means use the default Macintosh
70              * color table */
71             const uint8_t *color_table;
72             color_count = 1 << bit_depth;
73             if (bit_depth == 1)
74                 color_table = ff_qt_default_palette_2;
75             else if (bit_depth == 2)
76                 color_table = ff_qt_default_palette_4;
77             else if (bit_depth == 4)
78                 color_table = ff_qt_default_palette_16;
79             else
80                 color_table = ff_qt_default_palette_256;
81             for (i = 0; i < color_count; i++) {
82                 r = color_table[i * 3 + 0];
83                 g = color_table[i * 3 + 1];
84                 b = color_table[i * 3 + 2];
85                 palette[i] = (0xFFU << 24) | (r << 16) | (g << 8) | (b);
86             }
87         } else {
88             /* The color table ID is 0; the color table is in the sample
89              * description */
90             color_start = avio_rb32(pb);
91             avio_rb16(pb); /* color table flags */
92             color_end = avio_rb16(pb);
93             if ((color_start <= 255) && (color_end <= 255)) {
94                 for (i = color_start; i <= color_end; i++) {
95                     /* each A, R, G, or B component is 16 bits;
96                      * only use the top 8 bits */
97                     a = avio_r8(pb);
98                     avio_r8(pb);
99                     r = avio_r8(pb);
100                     avio_r8(pb);
101                     g = avio_r8(pb);
102                     avio_r8(pb);
103                     b = avio_r8(pb);
104                     avio_r8(pb);
105                     palette[i] = (a << 24 ) | (r << 16) | (g << 8) | (b);
106                 }
107             }
108         }
109
110         return 1;
111     }
112
113     return 0;
114 }