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