]> git.sesse.net Git - ffmpeg/blob - libavformat/qtpalette.c
avformat/qtpalette: Move ff_get_qtpalette() doxy to header
[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 2, 4, or 8 bpp, file is palettized. */
50     if ((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 == 2)
74                 color_table = ff_qt_default_palette_4;
75             else if (bit_depth == 4)
76                 color_table = ff_qt_default_palette_16;
77             else
78                 color_table = ff_qt_default_palette_256;
79             for (i = 0; i < color_count; i++) {
80                 r = color_table[i * 3 + 0];
81                 g = color_table[i * 3 + 1];
82                 b = color_table[i * 3 + 2];
83                 palette[i] = (0xFFU << 24) | (r << 16) | (g << 8) | (b);
84             }
85         } else {
86             /* The color table ID is 0; the color table is in the sample
87              * description */
88             color_start = avio_rb32(pb);
89             avio_rb16(pb); /* color table flags */
90             color_end = avio_rb16(pb);
91             if ((color_start <= 255) && (color_end <= 255)) {
92                 for (i = color_start; i <= color_end; i++) {
93                     /* each A, R, G, or B component is 16 bits;
94                      * only use the top 8 bits */
95                     a = avio_r8(pb);
96                     avio_r8(pb);
97                     r = avio_r8(pb);
98                     avio_r8(pb);
99                     g = avio_r8(pb);
100                     avio_r8(pb);
101                     b = avio_r8(pb);
102                     avio_r8(pb);
103                     palette[i] = (a << 24 ) | (r << 16) | (g << 8) | (b);
104                 }
105             }
106         }
107
108         return 1;
109     }
110
111     return 0;
112 }