FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/pcm_tablegen.h
Date: 2025-10-10 03:51:19
Exec Total Coverage
Lines: 33 40 82.5%
Functions: 5 7 71.4%
Branches: 12 14 85.7%

Line Branch Exec Source
1 /*
2 * Header file for hardcoded PCM tables
3 *
4 * Copyright (c) 2010 Reimar Döffinger <Reimar.Doeffinger@gmx.de>
5 *
6 * This file is part of FFmpeg.
7 *
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.
12 *
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.
17 *
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
21 */
22
23 #ifndef AVCODEC_PCM_TABLEGEN_H
24 #define AVCODEC_PCM_TABLEGEN_H
25
26 #include <stdint.h>
27 #include "config_components.h"
28 #include "libavutil/attributes.h"
29
30 /* from g711.c by SUN microsystems (unrestricted use) */
31
32 #define SIGN_BIT (0x80) /* Sign bit for a A-law byte. */
33 #define QUANT_MASK (0xf) /* Quantization field mask. */
34 #define NSEGS (8) /* Number of A-law segments. */
35 #define SEG_SHIFT (4) /* Left shift for segment number. */
36 #define SEG_MASK (0x70) /* Segment field mask. */
37
38 #define BIAS (0x84) /* Bias for linear code. */
39
40 #define VIDC_SIGN_BIT (1)
41 #define VIDC_QUANT_MASK (0x1E)
42 #define VIDC_QUANT_SHIFT (1)
43 #define VIDC_SEG_SHIFT (5)
44 #define VIDC_SEG_MASK (0xE0)
45
46 #if CONFIG_PCM_ALAW_DECODER || CONFIG_PCM_ALAW_ENCODER
47 /* alaw2linear() - Convert an A-law value to 16-bit linear PCM */
48 8690 static av_cold int alaw2linear(unsigned char a_val)
49 {
50 int t;
51 int seg;
52
53 8690 a_val ^= 0x55;
54
55 8690 t = a_val & QUANT_MASK;
56 8690 seg = ((unsigned)a_val & SEG_MASK) >> SEG_SHIFT;
57
2/2
✓ Branch 0 taken 7609 times.
✓ Branch 1 taken 1081 times.
8690 if(seg) t= (t + t + 1 + 32) << (seg + 2);
58 1081 else t= (t + t + 1 ) << 3;
59
60
2/2
✓ Branch 0 taken 3456 times.
✓ Branch 1 taken 5234 times.
8690 return (a_val & SIGN_BIT) ? t : -t;
61 }
62 #endif
63
64 #if CONFIG_PCM_MULAW_DECODER || CONFIG_PCM_MULAW_ENCODER
65 4092 static av_cold int ulaw2linear(unsigned char u_val)
66 {
67 int t;
68
69 /* Complement to obtain normal u-law value. */
70 4092 u_val = ~u_val;
71
72 /*
73 * Extract and bias the quantization bits. Then
74 * shift up by the segment number and subtract out the bias.
75 */
76 4092 t = ((u_val & QUANT_MASK) << 3) + BIAS;
77 4092 t <<= ((unsigned)u_val & SEG_MASK) >> SEG_SHIFT;
78
79
2/2
✓ Branch 0 taken 1792 times.
✓ Branch 1 taken 2300 times.
4092 return (u_val & SIGN_BIT) ? (BIAS - t) : (t - BIAS);
80 }
81 #endif
82
83 #if CONFIG_PCM_VIDC_DECODER || CONFIG_PCM_VIDC_ENCODER
84 static av_cold int vidc2linear(unsigned char u_val)
85 {
86 int t;
87
88 /*
89 * Extract and bias the quantization bits. Then
90 * shift up by the segment number and subtract out the bias.
91 */
92 t = (((u_val & VIDC_QUANT_MASK) >> VIDC_QUANT_SHIFT) << 3) + BIAS;
93 t <<= ((unsigned)u_val & VIDC_SEG_MASK) >> VIDC_SEG_SHIFT;
94
95 return (u_val & VIDC_SIGN_BIT) ? (BIAS - t) : (t - BIAS);
96 }
97 #endif
98
99 #if CONFIG_HARDCODED_TABLES
100 #define pcm_alaw_tableinit()
101 #define pcm_ulaw_tableinit()
102 #define pcm_vidc_tableinit()
103 #include "libavcodec/pcm_tables.h"
104 #else
105
106 #if CONFIG_PCM_ALAW_ENCODER || CONFIG_PCM_MULAW_ENCODER || \
107 CONFIG_PCM_VIDC_ENCODER
108 9 static av_cold void build_xlaw_table(uint8_t *linear_to_xlaw,
109 int (*xlaw2linear)(unsigned char),
110 int mask)
111 {
112 int i, j, v, v1, v2;
113
114 9 j = 1;
115 9 linear_to_xlaw[8192] = mask;
116
2/2
✓ Branch 0 taken 1143 times.
✓ Branch 1 taken 9 times.
1152 for(i=0;i<127;i++) {
117 1143 v1 = xlaw2linear(i ^ mask);
118 1143 v2 = xlaw2linear((i + 1) ^ mask);
119 1143 v = (v1 + v2 + 4) >> 3;
120
2/2
✓ Branch 0 taken 71349 times.
✓ Branch 1 taken 1143 times.
72492 for(;j<v;j+=1) {
121 71349 linear_to_xlaw[8192 - j] = (i ^ (mask ^ 0x80));
122 71349 linear_to_xlaw[8192 + j] = (i ^ mask);
123 }
124 }
125
2/2
✓ Branch 0 taken 2370 times.
✓ Branch 1 taken 9 times.
2379 for(;j<8192;j++) {
126 2370 linear_to_xlaw[8192 - j] = (127 ^ (mask ^ 0x80));
127 2370 linear_to_xlaw[8192 + j] = (127 ^ mask);
128 }
129 9 linear_to_xlaw[0] = linear_to_xlaw[1];
130 9 }
131 #endif
132
133 #if CONFIG_PCM_ALAW_ENCODER
134 static uint8_t linear_to_alaw[16384];
135 7 static void pcm_alaw_tableinit(void)
136 {
137 7 build_xlaw_table(linear_to_alaw, alaw2linear, 0xd5);
138 7 }
139 #endif
140
141 #if CONFIG_PCM_MULAW_ENCODER
142 static uint8_t linear_to_ulaw[16384];
143 2 static void pcm_ulaw_tableinit(void)
144 {
145 2 build_xlaw_table(linear_to_ulaw, ulaw2linear, 0xff);
146 2 }
147 #endif
148
149 #if CONFIG_PCM_VIDC_ENCODER
150 static uint8_t linear_to_vidc[16384];
151 static void pcm_vidc_tableinit(void)
152 {
153 build_xlaw_table(linear_to_vidc, vidc2linear, 0xff);
154 }
155 #endif
156 #endif /* CONFIG_HARDCODED_TABLES */
157
158 #endif /* AVCODEC_PCM_TABLEGEN_H */
159