From fa034b44270bbddfc11c691e94a6b8e97c68f84b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Reimar=20D=C3=B6ffinger?= Date: Thu, 1 Apr 2010 17:54:15 +0000 Subject: [PATCH] Allow hardcoding of ulaw and alaw tables. Originally committed as revision 22762 to svn://svn.ffmpeg.org/ffmpeg/trunk --- libavcodec/Makefile | 1 + libavcodec/pcm.c | 79 +------------------------ libavcodec/pcm_tablegen.c | 45 ++++++++++++++ libavcodec/pcm_tablegen.h | 119 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 168 insertions(+), 76 deletions(-) create mode 100644 libavcodec/pcm_tablegen.c create mode 100644 libavcodec/pcm_tablegen.h diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 4667be2c8e..4fce1bb058 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -645,5 +645,6 @@ $(SUBDIR)dv.o: $(SUBDIR)dv_tables.h $(SUBDIR)mdct.o: $(SUBDIR)mdct_tables.h $(SUBDIR)mpegaudiodec.o: $(SUBDIR)mpegaudio_tables.h $(SUBDIR)motionpixels.o: $(SUBDIR)motionpixels_tables.h +$(SUBDIR)pcm.o: $(SUBDIR)pcm_tables.h $(SUBDIR)qdm2.o: $(SUBDIR)qdm2_tables.h endif diff --git a/libavcodec/pcm.c b/libavcodec/pcm.c index 1298121fac..b68da7c354 100644 --- a/libavcodec/pcm.c +++ b/libavcodec/pcm.c @@ -27,92 +27,19 @@ #include "avcodec.h" #include "libavutil/common.h" /* for av_reverse */ #include "bytestream.h" +#include "pcm_tablegen.h" #define MAX_CHANNELS 64 -/* from g711.c by SUN microsystems (unrestricted use) */ - -#define SIGN_BIT (0x80) /* Sign bit for a A-law byte. */ -#define QUANT_MASK (0xf) /* Quantization field mask. */ -#define NSEGS (8) /* Number of A-law segments. */ -#define SEG_SHIFT (4) /* Left shift for segment number. */ -#define SEG_MASK (0x70) /* Segment field mask. */ - -#define BIAS (0x84) /* Bias for linear code. */ - -/* - * alaw2linear() - Convert an A-law value to 16-bit linear PCM - * - */ -static av_cold int alaw2linear(unsigned char a_val) -{ - int t; - int seg; - - a_val ^= 0x55; - - t = a_val & QUANT_MASK; - seg = ((unsigned)a_val & SEG_MASK) >> SEG_SHIFT; - if(seg) t= (t + t + 1 + 32) << (seg + 2); - else t= (t + t + 1 ) << 3; - - return (a_val & SIGN_BIT) ? t : -t; -} - -static av_cold int ulaw2linear(unsigned char u_val) -{ - int t; - - /* Complement to obtain normal u-law value. */ - u_val = ~u_val; - - /* - * Extract and bias the quantization bits. Then - * shift up by the segment number and subtract out the bias. - */ - t = ((u_val & QUANT_MASK) << 3) + BIAS; - t <<= ((unsigned)u_val & SEG_MASK) >> SEG_SHIFT; - - return (u_val & SIGN_BIT) ? (BIAS - t) : (t - BIAS); -} - -/* 16384 entries per table */ -static uint8_t linear_to_alaw[16384]; -static uint8_t linear_to_ulaw[16384]; - -static av_cold void build_xlaw_table(uint8_t *linear_to_xlaw, - int (*xlaw2linear)(unsigned char), - int mask) -{ - int i, j, v, v1, v2; - - j = 0; - for(i=0;i<128;i++) { - if (i != 127) { - v1 = xlaw2linear(i ^ mask); - v2 = xlaw2linear((i + 1) ^ mask); - v = (v1 + v2 + 4) >> 3; - } else { - v = 8192; - } - for(;j 0) - linear_to_xlaw[8192 - j] = (i ^ (mask ^ 0x80)); - } - } - linear_to_xlaw[0] = linear_to_xlaw[1]; -} - static av_cold int pcm_encode_init(AVCodecContext *avctx) { avctx->frame_size = 1; switch(avctx->codec->id) { case CODEC_ID_PCM_ALAW: - build_xlaw_table(linear_to_alaw, alaw2linear, 0xd5); + pcm_alaw_tableinit(); break; case CODEC_ID_PCM_MULAW: - build_xlaw_table(linear_to_ulaw, ulaw2linear, 0xff); + pcm_ulaw_tableinit(); break; default: break; diff --git a/libavcodec/pcm_tablegen.c b/libavcodec/pcm_tablegen.c new file mode 100644 index 0000000000..57ecb43dc6 --- /dev/null +++ b/libavcodec/pcm_tablegen.c @@ -0,0 +1,45 @@ +/* + * Generate a header file for hardcoded PCM tables + * + * Copyright (c) 2010 Reimar Döffinger + * + * 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 +#define CONFIG_HARDCODED_TABLES 0 +#include "pcm_tablegen.h" +#include "tableprint.h" + +int main(void) +{ + pcm_alaw_tableinit(); + pcm_ulaw_tableinit(); + + write_fileheader(); + + printf("static const uint8_t linear_to_alaw[1 << 14] = {\n"); + write_uint8_array(linear_to_alaw, 1 << 14); + printf("};\n"); + + printf("static const uint8_t linear_to_ulaw[1 << 14] = {\n"); + write_uint8_array(linear_to_ulaw, 1 << 14); + printf("};\n"); + + return 0; +} + diff --git a/libavcodec/pcm_tablegen.h b/libavcodec/pcm_tablegen.h new file mode 100644 index 0000000000..8921baaba2 --- /dev/null +++ b/libavcodec/pcm_tablegen.h @@ -0,0 +1,119 @@ +/* + * Header file for hardcoded PCM tables + * + * Copyright (c) 2010 Reimar Döffinger + * + * 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 + */ + +#ifndef PCM_TABLEGEN_H +#define PCM_TABLEGEN_H + +#include +#include "../libavutil/attributes.h" + +/* from g711.c by SUN microsystems (unrestricted use) */ + +#define SIGN_BIT (0x80) /* Sign bit for a A-law byte. */ +#define QUANT_MASK (0xf) /* Quantization field mask. */ +#define NSEGS (8) /* Number of A-law segments. */ +#define SEG_SHIFT (4) /* Left shift for segment number. */ +#define SEG_MASK (0x70) /* Segment field mask. */ + +#define BIAS (0x84) /* Bias for linear code. */ + +/* + * alaw2linear() - Convert an A-law value to 16-bit linear PCM + * + */ +static av_cold int alaw2linear(unsigned char a_val) +{ + int t; + int seg; + + a_val ^= 0x55; + + t = a_val & QUANT_MASK; + seg = ((unsigned)a_val & SEG_MASK) >> SEG_SHIFT; + if(seg) t= (t + t + 1 + 32) << (seg + 2); + else t= (t + t + 1 ) << 3; + + return (a_val & SIGN_BIT) ? t : -t; +} + +static av_cold int ulaw2linear(unsigned char u_val) +{ + int t; + + /* Complement to obtain normal u-law value. */ + u_val = ~u_val; + + /* + * Extract and bias the quantization bits. Then + * shift up by the segment number and subtract out the bias. + */ + t = ((u_val & QUANT_MASK) << 3) + BIAS; + t <<= ((unsigned)u_val & SEG_MASK) >> SEG_SHIFT; + + return (u_val & SIGN_BIT) ? (BIAS - t) : (t - BIAS); +} + +#if CONFIG_HARDCODED_TABLES +#define pcm_alaw_tableinit() +#define pcm_ulaw_tableinit() +#include "libavcodec/pcm_tables.h" +#else +/* 16384 entries per table */ +static uint8_t linear_to_alaw[16384]; +static uint8_t linear_to_ulaw[16384]; + +static av_cold void build_xlaw_table(uint8_t *linear_to_xlaw, + int (*xlaw2linear)(unsigned char), + int mask) +{ + int i, j, v, v1, v2; + + j = 0; + for(i=0;i<128;i++) { + if (i != 127) { + v1 = xlaw2linear(i ^ mask); + v2 = xlaw2linear((i + 1) ^ mask); + v = (v1 + v2 + 4) >> 3; + } else { + v = 8192; + } + for(;j 0) + linear_to_xlaw[8192 - j] = (i ^ (mask ^ 0x80)); + } + } + linear_to_xlaw[0] = linear_to_xlaw[1]; +} + +static void pcm_alaw_tableinit(void) +{ + build_xlaw_table(linear_to_alaw, alaw2linear, 0xd5); +} + +static void pcm_ulaw_tableinit(void) +{ + build_xlaw_table(linear_to_ulaw, ulaw2linear, 0xff); +} +#endif /* CONFIG_HARDCODED_TABLES */ + +#endif /* PCM_TABLEGEN_H */