annotate pcm.c @ 408:e5a0daeb3b58 libavcodec

* restored previous non '..' version
author kabi
date Tue, 21 May 2002 21:50:05 +0000
parents fce0a2520551
children 718a22dc121f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
1 /*
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
2 * PCM codecs
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
3 * Copyright (c) 2001 Gerard Lantau.
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
4 *
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
5 * This program is free software; you can redistribute it and/or modify
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
7 * the Free Software Foundation; either version 2 of the License, or
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
8 * (at your option) any later version.
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
9 *
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
13 * GNU General Public License for more details.
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
14 *
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
16 * along with this program; if not, write to the Free Software
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
18 */
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
19 #include "avcodec.h"
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
20
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
21 /* from g711.c by SUN microsystems (unrestricted use) */
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
22
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
23 #define SIGN_BIT (0x80) /* Sign bit for a A-law byte. */
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
24 #define QUANT_MASK (0xf) /* Quantization field mask. */
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
25 #define NSEGS (8) /* Number of A-law segments. */
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
26 #define SEG_SHIFT (4) /* Left shift for segment number. */
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
27 #define SEG_MASK (0x70) /* Segment field mask. */
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
28
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
29 #define BIAS (0x84) /* Bias for linear code. */
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
30
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
31 /*
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
32 * alaw2linear() - Convert an A-law value to 16-bit linear PCM
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
33 *
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
34 */
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
35 static int alaw2linear(unsigned char a_val)
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
36 {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
37 int t;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
38 int seg;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
39
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
40 a_val ^= 0x55;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
41
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
42 t = (a_val & QUANT_MASK) << 4;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
43 seg = ((unsigned)a_val & SEG_MASK) >> SEG_SHIFT;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
44 switch (seg) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
45 case 0:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
46 t += 8;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
47 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
48 case 1:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
49 t += 0x108;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
50 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
51 default:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
52 t += 0x108;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
53 t <<= seg - 1;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
54 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
55 return ((a_val & SIGN_BIT) ? t : -t);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
56 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
57
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
58 static int ulaw2linear(unsigned char u_val)
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
59 {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
60 int t;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
61
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
62 /* Complement to obtain normal u-law value. */
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
63 u_val = ~u_val;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
64
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
65 /*
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
66 * Extract and bias the quantization bits. Then
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
67 * shift up by the segment number and subtract out the bias.
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
68 */
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
69 t = ((u_val & QUANT_MASK) << 3) + BIAS;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
70 t <<= ((unsigned)u_val & SEG_MASK) >> SEG_SHIFT;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
71
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
72 return ((u_val & SIGN_BIT) ? (BIAS - t) : (t - BIAS));
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
73 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
74
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
75 /* 16384 entries per table */
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
76 static UINT8 *linear_to_alaw = NULL;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
77 static int linear_to_alaw_ref = 0;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
78
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
79 static UINT8 *linear_to_ulaw = NULL;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
80 static int linear_to_ulaw_ref = 0;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
81
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
82 static void build_xlaw_table(UINT8 *linear_to_xlaw,
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
83 int (*xlaw2linear)(unsigned char),
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
84 int mask)
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
85 {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
86 int i, j, v, v1, v2;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
87
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
88 j = 0;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
89 for(i=0;i<128;i++) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
90 if (i != 127) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
91 v1 = xlaw2linear(i ^ mask);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
92 v2 = xlaw2linear((i + 1) ^ mask);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
93 v = (v1 + v2 + 4) >> 3;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
94 } else {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
95 v = 8192;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
96 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
97 for(;j<v;j++) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
98 linear_to_xlaw[8192 + j] = (i ^ mask);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
99 if (j > 0)
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
100 linear_to_xlaw[8192 - j] = (i ^ (mask ^ 0x80));
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
101 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
102 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
103 linear_to_xlaw[0] = linear_to_xlaw[1];
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
104 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
105
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
106 static int encode_init(AVCodecContext *avctx)
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
107 {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
108 avctx->frame_size = 1;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
109 switch(avctx->codec->id) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
110 case CODEC_ID_PCM_ALAW:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
111 if (linear_to_alaw_ref == 0) {
396
fce0a2520551 removed useless header includes - use av memory functions
glantau
parents: 381
diff changeset
112 linear_to_alaw = av_malloc(16384);
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
113 if (!linear_to_alaw)
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
114 return -1;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
115 build_xlaw_table(linear_to_alaw, alaw2linear, 0xd5);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
116 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
117 linear_to_alaw_ref++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
118 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
119 case CODEC_ID_PCM_MULAW:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
120 if (linear_to_ulaw_ref == 0) {
396
fce0a2520551 removed useless header includes - use av memory functions
glantau
parents: 381
diff changeset
121 linear_to_ulaw = av_malloc(16384);
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
122 if (!linear_to_ulaw)
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
123 return -1;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
124 build_xlaw_table(linear_to_ulaw, ulaw2linear, 0xff);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
125 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
126 linear_to_ulaw_ref++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
127 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
128 default:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
129 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
130 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
131 return 0;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
132 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
133
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
134 static int encode_close(AVCodecContext *avctx)
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
135 {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
136 switch(avctx->codec->id) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
137 case CODEC_ID_PCM_ALAW:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
138 if (--linear_to_alaw_ref == 0)
396
fce0a2520551 removed useless header includes - use av memory functions
glantau
parents: 381
diff changeset
139 av_free(linear_to_alaw);
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
140 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
141 case CODEC_ID_PCM_MULAW:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
142 if (--linear_to_ulaw_ref == 0)
396
fce0a2520551 removed useless header includes - use av memory functions
glantau
parents: 381
diff changeset
143 av_free(linear_to_ulaw);
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
144 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
145 default:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
146 /* nothing to free */
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
147 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
148 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
149 return 0;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
150 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
151
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
152 static int encode_frame(AVCodecContext *avctx,
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
153 unsigned char *frame, int buf_size, void *data)
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
154 {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
155 int n, sample_size, v;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
156 short *samples;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
157 unsigned char *dst;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
158
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
159 switch(avctx->codec->id) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
160 case CODEC_ID_PCM_S16LE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
161 case CODEC_ID_PCM_S16BE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
162 case CODEC_ID_PCM_U16LE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
163 case CODEC_ID_PCM_U16BE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
164 sample_size = 2;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
165 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
166 default:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
167 sample_size = 1;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
168 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
169 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
170 n = buf_size / sample_size;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
171 samples = data;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
172 dst = frame;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
173
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
174 switch(avctx->codec->id) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
175 case CODEC_ID_PCM_S16LE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
176 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
177 v = *samples++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
178 dst[0] = v & 0xff;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
179 dst[1] = v >> 8;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
180 dst += 2;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
181 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
182 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
183 case CODEC_ID_PCM_S16BE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
184 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
185 v = *samples++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
186 dst[0] = v >> 8;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
187 dst[1] = v;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
188 dst += 2;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
189 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
190 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
191 case CODEC_ID_PCM_U16LE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
192 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
193 v = *samples++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
194 v += 0x8000;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
195 dst[0] = v & 0xff;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
196 dst[1] = v >> 8;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
197 dst += 2;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
198 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
199 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
200 case CODEC_ID_PCM_U16BE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
201 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
202 v = *samples++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
203 v += 0x8000;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
204 dst[0] = v >> 8;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
205 dst[1] = v;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
206 dst += 2;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
207 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
208 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
209 case CODEC_ID_PCM_S8:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
210 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
211 v = *samples++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
212 dst[0] = (v + 128) >> 8;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
213 dst++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
214 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
215 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
216 case CODEC_ID_PCM_U8:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
217 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
218 v = *samples++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
219 dst[0] = ((v + 128) >> 8) + 128;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
220 dst++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
221 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
222 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
223 case CODEC_ID_PCM_ALAW:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
224 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
225 v = *samples++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
226 dst[0] = linear_to_alaw[(v + 32768) >> 2];
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
227 dst++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
228 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
229 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
230 case CODEC_ID_PCM_MULAW:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
231 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
232 v = *samples++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
233 dst[0] = linear_to_ulaw[(v + 32768) >> 2];
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
234 dst++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
235 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
236 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
237 default:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
238 return -1;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
239 }
372
19b6a1fa6f6d * Every frame is a key_frame
philipjsg
parents: 92
diff changeset
240 avctx->key_frame = 1;
381
0d6178e4d503 * Mea culpa: it seems that I broke encoding to 8-bit pcm files. This fixes it.
philipjsg
parents: 372
diff changeset
241 //avctx->frame_size = (dst - frame) / (sample_size * avctx->channels);
372
19b6a1fa6f6d * Every frame is a key_frame
philipjsg
parents: 92
diff changeset
242
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
243 return dst - frame;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
244 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
245
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
246 typedef struct PCMDecode {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
247 short table[256];
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
248 } PCMDecode;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
249
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
250 static int decode_init(AVCodecContext * avctx)
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
251 {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
252 PCMDecode *s = avctx->priv_data;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
253 int i;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
254
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
255 switch(avctx->codec->id) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
256 case CODEC_ID_PCM_ALAW:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
257 for(i=0;i<256;i++)
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
258 s->table[i] = alaw2linear(i);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
259 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
260 case CODEC_ID_PCM_MULAW:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
261 for(i=0;i<256;i++)
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
262 s->table[i] = ulaw2linear(i);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
263 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
264 default:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
265 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
266 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
267 return 0;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
268 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
269
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
270 static int decode_frame(AVCodecContext *avctx,
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
271 void *data, int *data_size,
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
272 UINT8 *buf, int buf_size)
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
273 {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
274 PCMDecode *s = avctx->priv_data;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
275 int n;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
276 short *samples;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
277 UINT8 *src;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
278
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
279 samples = data;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
280 src = buf;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
281
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
282 switch(avctx->codec->id) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
283 case CODEC_ID_PCM_S16LE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
284 n = buf_size >> 1;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
285 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
286 *samples++ = src[0] | (src[1] << 8);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
287 src += 2;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
288 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
289 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
290 case CODEC_ID_PCM_S16BE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
291 n = buf_size >> 1;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
292 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
293 *samples++ = (src[0] << 8) | src[1];
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
294 src += 2;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
295 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
296 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
297 case CODEC_ID_PCM_U16LE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
298 n = buf_size >> 1;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
299 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
300 *samples++ = (src[0] | (src[1] << 8)) - 0x8000;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
301 src += 2;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
302 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
303 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
304 case CODEC_ID_PCM_U16BE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
305 n = buf_size >> 1;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
306 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
307 *samples++ = ((src[0] << 8) | src[1]) - 0x8000;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
308 src += 2;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
309 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
310 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
311 case CODEC_ID_PCM_S8:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
312 n = buf_size;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
313 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
314 *samples++ = src[0] << 8;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
315 src++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
316 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
317 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
318 case CODEC_ID_PCM_U8:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
319 n = buf_size;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
320 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
321 *samples++ = ((int)src[0] - 128) << 8;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
322 src++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
323 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
324 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
325 case CODEC_ID_PCM_ALAW:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
326 case CODEC_ID_PCM_MULAW:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
327 n = buf_size;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
328 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
329 *samples++ = s->table[src[0]];
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
330 src++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
331 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
332 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
333 default:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
334 *data_size = 0;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
335 return -1;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
336 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
337 *data_size = (UINT8 *)samples - (UINT8 *)data;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
338 return src - buf;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
339 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
340
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
341 #define PCM_CODEC(id, name) \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
342 AVCodec name ## _encoder = { \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
343 #name, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
344 CODEC_TYPE_AUDIO, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
345 id, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
346 0, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
347 encode_init, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
348 encode_frame, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
349 encode_close, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
350 NULL, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
351 }; \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
352 AVCodec name ## _decoder = { \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
353 #name, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
354 CODEC_TYPE_AUDIO, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
355 id, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
356 sizeof(PCMDecode), \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
357 decode_init, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
358 NULL, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
359 NULL, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
360 decode_frame, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
361 };
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
362
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
363 PCM_CODEC(CODEC_ID_PCM_S16LE, pcm_s16le);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
364 PCM_CODEC(CODEC_ID_PCM_S16BE, pcm_s16be);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
365 PCM_CODEC(CODEC_ID_PCM_U16LE, pcm_u16le);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
366 PCM_CODEC(CODEC_ID_PCM_U16BE, pcm_u16be);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
367 PCM_CODEC(CODEC_ID_PCM_S8, pcm_s8);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
368 PCM_CODEC(CODEC_ID_PCM_U8, pcm_u8);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
369 PCM_CODEC(CODEC_ID_PCM_ALAW, pcm_alaw);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
370 PCM_CODEC(CODEC_ID_PCM_MULAW, pcm_mulaw);