avformat: factor ff_get_qtpalette() out of mov.c

This consists mainly of moving the palette handling from
the mov_parse_stsd_video() function to a new ff_get_qtpalette() function
in the new file qtpalette.c, which will be shared by both matroskadec.c and
mov.c.

Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
This commit is contained in:
Mats Peterson 2015-12-27 21:28:09 +01:00 committed by Michael Niedermayer
parent c48122d731
commit 57631f1851
4 changed files with 135 additions and 70 deletions

View File

@ -18,6 +18,7 @@ OBJS = allformats.o \
mux.o \
options.o \
os_support.o \
qtpalette.o \
riff.o \
sdp.o \
url.o \

View File

@ -1755,9 +1755,12 @@ static void mov_parse_stsd_video(MOVContext *c, AVIOContext *pb,
AVStream *st, MOVStreamContext *sc)
{
uint8_t codec_name[32];
unsigned int color_depth, len, j;
int color_greyscale;
int color_table_id;
int64_t stsd_start;
unsigned int len;
/* The first 16 bytes of the video sample description are already
* read in ff_mov_read_stsd_entries() */
stsd_start = avio_tell(pb) - 16;
avio_rb16(pb); /* version */
avio_rb16(pb); /* revision level */
@ -1795,74 +1798,11 @@ static void mov_parse_stsd_video(MOVContext *c, AVIOContext *pb,
st->codec->codec_id = AV_CODEC_ID_FLV1;
st->codec->bits_per_coded_sample = avio_rb16(pb); /* depth */
color_table_id = avio_rb16(pb); /* colortable id */
av_log(c->fc, AV_LOG_TRACE, "depth %d, ctab id %d\n",
st->codec->bits_per_coded_sample, color_table_id);
/* figure out the palette situation */
color_depth = st->codec->bits_per_coded_sample & 0x1F;
color_greyscale = st->codec->bits_per_coded_sample & 0x20;
/* Do not create a greyscale palette for cinepak */
if (color_greyscale && st->codec->codec_id == AV_CODEC_ID_CINEPAK)
return;
/* if the depth is 2, 4, or 8 bpp, file is palettized */
if ((color_depth == 2) || (color_depth == 4) || (color_depth == 8)) {
/* for palette traversal */
unsigned int color_start, color_count, color_end;
unsigned int a, r, g, b;
avio_seek(pb, stsd_start, SEEK_SET);
if (color_greyscale) {
int color_index, color_dec;
/* compute the greyscale palette */
st->codec->bits_per_coded_sample = color_depth;
color_count = 1 << color_depth;
color_index = 255;
color_dec = 256 / (color_count - 1);
for (j = 0; j < color_count; j++) {
r = g = b = color_index;
sc->palette[j] = (0xFFU << 24) | (r << 16) | (g << 8) | (b);
color_index -= color_dec;
if (color_index < 0)
color_index = 0;
}
} else if (color_table_id) {
const uint8_t *color_table;
/* if flag bit 3 is set, use the default palette */
color_count = 1 << color_depth;
if (color_depth == 2)
color_table = ff_qt_default_palette_4;
else if (color_depth == 4)
color_table = ff_qt_default_palette_16;
else
color_table = ff_qt_default_palette_256;
for (j = 0; j < color_count; j++) {
r = color_table[j * 3 + 0];
g = color_table[j * 3 + 1];
b = color_table[j * 3 + 2];
sc->palette[j] = (0xFFU << 24) | (r << 16) | (g << 8) | (b);
}
} else {
/* load the palette from the file */
color_start = avio_rb32(pb);
color_count = avio_rb16(pb);
color_end = avio_rb16(pb);
if ((color_start <= 255) && (color_end <= 255)) {
for (j = color_start; j <= color_end; j++) {
/* each A, R, G, or B component is 16 bits;
* only use the top 8 bits */
a = avio_r8(pb);
avio_r8(pb);
r = avio_r8(pb);
avio_r8(pb);
g = avio_r8(pb);
avio_r8(pb);
b = avio_r8(pb);
avio_r8(pb);
sc->palette[j] = (a << 24 ) | (r << 16) | (g << 8) | (b);
}
}
}
if (ff_get_qtpalette(st->codec->codec_id, pb, sc->palette)) {
st->codec->bits_per_coded_sample &= 0x1F;
sc->has_palette = 1;
}
}

121
libavformat/qtpalette.c Normal file
View File

@ -0,0 +1,121 @@
/*
* QuickTime palette handling
* Copyright (c) 2001 Fabrice Bellard
* Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
* Copyright (c) 2015 Mats Peterson
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <stdint.h>
#include "avformat.h"
#include "libavutil/intreadwrite.h"
#include "qtpalette.h"
/**
* Retrieve the palette (or "color table" in QuickTime terms), either
* from the video sample description, or from the default Macintosh
* palette.
*
* The file offset of the AVIOContext pointed to by the 'pb' variable
* should be the start of the video sample description (the sample
* description size and the data format).
*/
int ff_get_qtpalette(int codec_id, AVIOContext *pb, uint32_t *palette)
{
int tmp, bit_depth, color_table_id, greyscale, i;
avio_seek(pb, 82, SEEK_CUR);
/* Get the bit depth and greyscale state */
tmp = avio_rb16(pb);
bit_depth = tmp & 0x1F;
greyscale = tmp & 0x20;
/* Get the color table ID */
color_table_id = avio_rb16(pb);
/* Do not create a greyscale palette for Cinepak */
if (greyscale && codec_id == AV_CODEC_ID_CINEPAK)
return 0;
/* If the depth is 2, 4, or 8 bpp, file is palettized. */
if ((bit_depth == 2 || bit_depth == 4 || bit_depth == 8)) {
int color_count, color_start, color_end;
uint32_t a, r, g, b;
if (greyscale) {
int color_index, color_dec;
/* compute the greyscale palette */
color_count = 1 << bit_depth;
color_index = 255;
color_dec = 256 / (color_count - 1);
for (i = 0; i < color_count; i++) {
r = g = b = color_index;
palette[i] = (0xFFU << 24) | (r << 16) | (g << 8) | (b);
color_index -= color_dec;
if (color_index < 0)
color_index = 0;
}
} else if (color_table_id) {
/* The color table ID is non-zero. Interpret this as
* being -1, which means use the default Macintosh
* color table */
const uint8_t *color_table;
color_count = 1 << bit_depth;
if (bit_depth == 2)
color_table = ff_qt_default_palette_4;
else if (bit_depth == 4)
color_table = ff_qt_default_palette_16;
else
color_table = ff_qt_default_palette_256;
for (i = 0; i < color_count; i++) {
r = color_table[i * 3 + 0];
g = color_table[i * 3 + 1];
b = color_table[i * 3 + 2];
palette[i] = (0xFFU << 24) | (r << 16) | (g << 8) | (b);
}
} else {
/* The color table ID is 0; the color table is in the sample
* description */
color_start = avio_rb32(pb);
avio_rb16(pb); /* color table flags */
color_end = avio_rb16(pb);
if ((color_start <= 255) && (color_end <= 255)) {
for (i = color_start; i <= color_end; i++) {
/* each A, R, G, or B component is 16 bits;
* only use the top 8 bits */
a = avio_r8(pb);
avio_r8(pb);
r = avio_r8(pb);
avio_r8(pb);
g = avio_r8(pb);
avio_r8(pb);
b = avio_r8(pb);
avio_r8(pb);
palette[i] = (a << 24 ) | (r << 16) | (g << 8) | (b);
}
}
}
return 1;
}
return 0;
}

View File

@ -23,7 +23,8 @@
#ifndef AVFORMAT_QTPALETTE_H
#define AVFORMAT_QTPALETTE_H
#include <inttypes.h>
#include <stdint.h>
#include "avformat.h"
static const uint8_t ff_qt_default_palette_4[4 * 3] = {
0x93, 0x65, 0x5E,
@ -310,4 +311,6 @@ static const uint8_t ff_qt_default_palette_256[256 * 3] = {
/* 255, 0xFF */ 0x00, 0x00, 0x00
};
int ff_get_qtpalette(int codec_id, AVIOContext *pb, uint32_t *palette);
#endif /* AVFORMAT_QTPALETTE_H */