2 * Header file for hardcoded PCM tables
4 * Copyright (c) 2010 Reimar Döffinger <Reimar.Doeffinger@gmx.de>
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #ifndef AVCODEC_PCM_TABLEGEN_H
24 #define AVCODEC_PCM_TABLEGEN_H
27 #include "libavutil/attributes.h"
29 /* from g711.c by SUN microsystems (unrestricted use) */
31 #define SIGN_BIT (0x80) /* Sign bit for a A-law byte. */
32 #define QUANT_MASK (0xf) /* Quantization field mask. */
33 #define NSEGS (8) /* Number of A-law segments. */
34 #define SEG_SHIFT (4) /* Left shift for segment number. */
35 #define SEG_MASK (0x70) /* Segment field mask. */
37 #define BIAS (0x84) /* Bias for linear code. */
39 #define VIDC_SIGN_BIT (1)
40 #define VIDC_QUANT_MASK (0x1E)
41 #define VIDC_QUANT_SHIFT (1)
42 #define VIDC_SEG_SHIFT (5)
43 #define VIDC_SEG_MASK (0xE0)
45 /* alaw2linear() - Convert an A-law value to 16-bit linear PCM */
46 static av_cold int alaw2linear(unsigned char a_val)
53 t = a_val & QUANT_MASK;
54 seg = ((unsigned)a_val & SEG_MASK) >> SEG_SHIFT;
55 if(seg) t= (t + t + 1 + 32) << (seg + 2);
56 else t= (t + t + 1 ) << 3;
58 return (a_val & SIGN_BIT) ? t : -t;
61 static av_cold int ulaw2linear(unsigned char u_val)
65 /* Complement to obtain normal u-law value. */
69 * Extract and bias the quantization bits. Then
70 * shift up by the segment number and subtract out the bias.
72 t = ((u_val & QUANT_MASK) << 3) + BIAS;
73 t <<= ((unsigned)u_val & SEG_MASK) >> SEG_SHIFT;
75 return (u_val & SIGN_BIT) ? (BIAS - t) : (t - BIAS);
78 static av_cold int vidc2linear(unsigned char u_val)
83 * Extract and bias the quantization bits. Then
84 * shift up by the segment number and subtract out the bias.
86 t = (((u_val & VIDC_QUANT_MASK) >> VIDC_QUANT_SHIFT) << 3) + BIAS;
87 t <<= ((unsigned)u_val & VIDC_SEG_MASK) >> VIDC_SEG_SHIFT;
89 return (u_val & VIDC_SIGN_BIT) ? (BIAS - t) : (t - BIAS);
92 #if CONFIG_HARDCODED_TABLES
93 #define pcm_alaw_tableinit()
94 #define pcm_ulaw_tableinit()
95 #define pcm_vidc_tableinit()
96 #include "libavcodec/pcm_tables.h"
98 /* 16384 entries per table */
99 static uint8_t linear_to_alaw[16384];
100 static uint8_t linear_to_ulaw[16384];
101 static uint8_t linear_to_vidc[16384];
103 static av_cold void build_xlaw_table(uint8_t *linear_to_xlaw,
104 int (*xlaw2linear)(unsigned char),
110 linear_to_xlaw[8192] = mask;
112 v1 = xlaw2linear(i ^ mask);
113 v2 = xlaw2linear((i + 1) ^ mask);
114 v = (v1 + v2 + 4) >> 3;
116 linear_to_xlaw[8192 - j] = (i ^ (mask ^ 0x80));
117 linear_to_xlaw[8192 + j] = (i ^ mask);
121 linear_to_xlaw[8192 - j] = (127 ^ (mask ^ 0x80));
122 linear_to_xlaw[8192 + j] = (127 ^ mask);
124 linear_to_xlaw[0] = linear_to_xlaw[1];
127 static void pcm_alaw_tableinit(void)
129 build_xlaw_table(linear_to_alaw, alaw2linear, 0xd5);
132 static void pcm_ulaw_tableinit(void)
134 build_xlaw_table(linear_to_ulaw, ulaw2linear, 0xff);
137 static void pcm_vidc_tableinit(void)
139 build_xlaw_table(linear_to_vidc, vidc2linear, 0xff);
141 #endif /* CONFIG_HARDCODED_TABLES */
143 #endif /* AVCODEC_PCM_TABLEGEN_H */