FFmpeg
qtpalette.c
Go to the documentation of this file.
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  uint32_t color_count, color_start, color_end;
52  uint32_t r, g, b;
53 
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;
60  color_index = 255;
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;
66  if (color_index < 0)
67  color_index = 0;
68  }
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
72  * color table */
73  const uint8_t *color_table;
74  color_count = 1 << bit_depth;
75  if (bit_depth == 1)
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;
81  else
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);
88  }
89  } else {
90  /* The color table ID is 0; the color table is in the sample
91  * description */
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 color is made of four unsigned 16 bit integers. The
98  * first integer is 0, the remaining integers are the red,
99  * the green and the blue values. We only use the top 8 bit. */
100  avio_skip(pb, 2);
101  r = avio_r8(pb);
102  avio_r8(pb);
103  g = avio_r8(pb);
104  avio_r8(pb);
105  b = avio_r8(pb);
106  avio_r8(pb);
107  palette[i] = (0xFFU << 24) | (r << 16) | (g << 8) | (b);
108  }
109  }
110  }
111 
112  return 1;
113  }
114 
115  return 0;
116 }
Bytestream IO Context.
Definition: avio.h:161
const char * g
Definition: vf_curves.c:115
enum AVCodecID codec_id
Definition: qsv.c:77
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:246
static const uint8_t ff_qt_default_palette_256[256 *3]
Definition: qtpalette.h:62
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:334
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:788
uint8_t
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:803
static const uint8_t ff_qt_default_palette_4[4 *3]
Definition: qtpalette.h:35
#define U(x)
Definition: vp56_arith.h:37
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
static const ColorEntry color_table[]
Definition: xpmdec.c:50
const char * r
Definition: vf_curves.c:114
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:641
#define b
Definition: input.c:41
static void bit_depth(AudioStatsContext *s, uint64_t mask, uint64_t imask, AVRational *depth)
Definition: af_astats.c:226
int ff_get_qtpalette(int codec_id, AVIOContext *pb, uint32_t *palette)
Retrieve the palette (or "color table" in QuickTime terms), either from the video sample description...
Definition: qtpalette.c:31
static const uint8_t ff_qt_default_palette_16[16 *3]
Definition: qtpalette.h:43
Main libavformat public API header.
static const uint8_t ff_qt_default_palette_2[2 *3]
Definition: qtpalette.h:29
static uint8_t tmp[11]
Definition: aes_ctr.c:26