annotate pcm.c @ 354:167aa21aa250 libavcodec

patch by Alex Beregszaszi <alex@naxine.org> - AVID (AVRn) support (workaround) - print error instead of failing for unsupported SOF - fixed the 0<code<FF range checking
author arpi_esp
date Fri, 03 May 2002 16:34:40 +0000
parents 1d3eb6cdc6b5
children 19b6a1fa6f6d
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) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
112 linear_to_alaw = malloc(16384);
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) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
121 linear_to_ulaw = malloc(16384);
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)
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
139 free(linear_to_alaw);
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)
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
143 free(linear_to_ulaw);
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 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
240 return dst - frame;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
241 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
242
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
243 typedef struct PCMDecode {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
244 short table[256];
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
245 } PCMDecode;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
246
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
247 static int decode_init(AVCodecContext * avctx)
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
248 {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
249 PCMDecode *s = avctx->priv_data;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
250 int i;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
251
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
252 switch(avctx->codec->id) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
253 case CODEC_ID_PCM_ALAW:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
254 for(i=0;i<256;i++)
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
255 s->table[i] = alaw2linear(i);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
256 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
257 case CODEC_ID_PCM_MULAW:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
258 for(i=0;i<256;i++)
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
259 s->table[i] = ulaw2linear(i);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
260 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
261 default:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
262 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
263 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
264 return 0;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
265 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
266
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
267 static int decode_frame(AVCodecContext *avctx,
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
268 void *data, int *data_size,
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
269 UINT8 *buf, int buf_size)
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
270 {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
271 PCMDecode *s = avctx->priv_data;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
272 int n;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
273 short *samples;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
274 UINT8 *src;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
275
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
276 samples = data;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
277 src = buf;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
278
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
279 switch(avctx->codec->id) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
280 case CODEC_ID_PCM_S16LE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
281 n = buf_size >> 1;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
282 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
283 *samples++ = src[0] | (src[1] << 8);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
284 src += 2;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
285 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
286 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
287 case CODEC_ID_PCM_S16BE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
288 n = buf_size >> 1;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
289 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
290 *samples++ = (src[0] << 8) | src[1];
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
291 src += 2;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
292 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
293 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
294 case CODEC_ID_PCM_U16LE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
295 n = buf_size >> 1;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
296 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
297 *samples++ = (src[0] | (src[1] << 8)) - 0x8000;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
298 src += 2;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
299 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
300 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
301 case CODEC_ID_PCM_U16BE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
302 n = buf_size >> 1;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
303 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
304 *samples++ = ((src[0] << 8) | src[1]) - 0x8000;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
305 src += 2;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
306 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
307 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
308 case CODEC_ID_PCM_S8:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
309 n = buf_size;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
310 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
311 *samples++ = src[0] << 8;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
312 src++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
313 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
314 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
315 case CODEC_ID_PCM_U8:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
316 n = buf_size;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
317 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
318 *samples++ = ((int)src[0] - 128) << 8;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
319 src++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
320 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
321 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
322 case CODEC_ID_PCM_ALAW:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
323 case CODEC_ID_PCM_MULAW:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
324 n = buf_size;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
325 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
326 *samples++ = s->table[src[0]];
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
327 src++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
328 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
329 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
330 default:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
331 *data_size = 0;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
332 return -1;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
333 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
334 *data_size = (UINT8 *)samples - (UINT8 *)data;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
335 return src - buf;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
336 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
337
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
338 #define PCM_CODEC(id, name) \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
339 AVCodec name ## _encoder = { \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
340 #name, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
341 CODEC_TYPE_AUDIO, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
342 id, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
343 0, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
344 encode_init, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
345 encode_frame, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
346 encode_close, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
347 NULL, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
348 }; \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
349 AVCodec name ## _decoder = { \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
350 #name, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
351 CODEC_TYPE_AUDIO, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
352 id, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
353 sizeof(PCMDecode), \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
354 decode_init, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
355 NULL, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
356 NULL, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
357 decode_frame, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
358 };
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
359
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
360 PCM_CODEC(CODEC_ID_PCM_S16LE, pcm_s16le);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
361 PCM_CODEC(CODEC_ID_PCM_S16BE, pcm_s16be);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
362 PCM_CODEC(CODEC_ID_PCM_U16LE, pcm_u16le);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
363 PCM_CODEC(CODEC_ID_PCM_U16BE, pcm_u16be);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
364 PCM_CODEC(CODEC_ID_PCM_S8, pcm_s8);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
365 PCM_CODEC(CODEC_ID_PCM_U8, pcm_u8);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
366 PCM_CODEC(CODEC_ID_PCM_ALAW, pcm_alaw);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
367 PCM_CODEC(CODEC_ID_PCM_MULAW, pcm_mulaw);