annotate pcm.c @ 1064:b32afefe7d33 libavcodec

* UINTX -> uintx_t INTX -> intx_t
author kabi
date Tue, 11 Feb 2003 16:35:48 +0000
parents 48349e11c9b2
children bfc18110d4b6
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
429
718a22dc121f license/copyright change
glantau
parents: 396
diff changeset
3 * Copyright (c) 2001 Fabrice Bellard.
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
4 *
429
718a22dc121f license/copyright change
glantau
parents: 396
diff changeset
5 * This library is free software; you can redistribute it and/or
718a22dc121f license/copyright change
glantau
parents: 396
diff changeset
6 * modify it under the terms of the GNU Lesser General Public
718a22dc121f license/copyright change
glantau
parents: 396
diff changeset
7 * License as published by the Free Software Foundation; either
718a22dc121f license/copyright change
glantau
parents: 396
diff changeset
8 * version 2 of the License, or (at your option) any later version.
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
9 *
429
718a22dc121f license/copyright change
glantau
parents: 396
diff changeset
10 * This library is distributed in the hope that it will be useful,
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
429
718a22dc121f license/copyright change
glantau
parents: 396
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
718a22dc121f license/copyright change
glantau
parents: 396
diff changeset
13 * Lesser General Public License for more details.
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
14 *
429
718a22dc121f license/copyright change
glantau
parents: 396
diff changeset
15 * You should have received a copy of the GNU Lesser General Public
718a22dc121f license/copyright change
glantau
parents: 396
diff changeset
16 * License along with this library; if not, write to the Free Software
718a22dc121f license/copyright change
glantau
parents: 396
diff changeset
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
92
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 */
1064
b32afefe7d33 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 1014
diff changeset
76 static uint8_t *linear_to_alaw = NULL;
92
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
1064
b32afefe7d33 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 1014
diff changeset
79 static uint8_t *linear_to_ulaw = NULL;
92
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
1064
b32afefe7d33 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 1014
diff changeset
82 static void build_xlaw_table(uint8_t *linear_to_xlaw,
92
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
440
000aeeac27a2 * started to cleanup name clashes for onetime compilation
kabi
parents: 429
diff changeset
106 static int pcm_encode_init(AVCodecContext *avctx)
92
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 }
925
7fccaa0d699d AVVideoFrame -> AVFrame
michaelni
parents: 649
diff changeset
131
7fccaa0d699d AVVideoFrame -> AVFrame
michaelni
parents: 649
diff changeset
132 avctx->coded_frame= avcodec_alloc_frame();
7fccaa0d699d AVVideoFrame -> AVFrame
michaelni
parents: 649
diff changeset
133 avctx->coded_frame->key_frame= 1;
7fccaa0d699d AVVideoFrame -> AVFrame
michaelni
parents: 649
diff changeset
134
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
135 return 0;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
136 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
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
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
139 {
925
7fccaa0d699d AVVideoFrame -> AVFrame
michaelni
parents: 649
diff changeset
140 av_freep(&avctx->coded_frame);
7fccaa0d699d AVVideoFrame -> AVFrame
michaelni
parents: 649
diff changeset
141
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
142 switch(avctx->codec->id) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
143 case CODEC_ID_PCM_ALAW:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
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
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
146 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
147 case CODEC_ID_PCM_MULAW:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
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
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
150 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
151 default:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
152 /* nothing to free */
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
153 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
154 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
155 return 0;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
156 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
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
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
160 {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
161 int n, sample_size, v;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
162 short *samples;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
163 unsigned char *dst;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
164
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
165 switch(avctx->codec->id) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
166 case CODEC_ID_PCM_S16LE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
167 case CODEC_ID_PCM_S16BE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
168 case CODEC_ID_PCM_U16LE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
169 case CODEC_ID_PCM_U16BE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
170 sample_size = 2;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
171 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
172 default:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
173 sample_size = 1;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
174 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
175 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
176 n = buf_size / sample_size;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
177 samples = data;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
178 dst = frame;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
179
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
180 switch(avctx->codec->id) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
181 case CODEC_ID_PCM_S16LE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
182 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
183 v = *samples++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
184 dst[0] = v & 0xff;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
185 dst[1] = v >> 8;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
186 dst += 2;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
187 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
188 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
189 case CODEC_ID_PCM_S16BE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
190 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
191 v = *samples++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
192 dst[0] = v >> 8;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
193 dst[1] = v;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
194 dst += 2;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
195 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
196 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
197 case CODEC_ID_PCM_U16LE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
198 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
199 v = *samples++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
200 v += 0x8000;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
201 dst[0] = v & 0xff;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
202 dst[1] = v >> 8;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
203 dst += 2;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
204 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
205 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
206 case CODEC_ID_PCM_U16BE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
207 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
208 v = *samples++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
209 v += 0x8000;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
210 dst[0] = v >> 8;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
211 dst[1] = v;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
212 dst += 2;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
213 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
214 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
215 case CODEC_ID_PCM_S8:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
216 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
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
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
219 dst++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
220 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
221 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
222 case CODEC_ID_PCM_U8:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
223 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
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
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
226 dst++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
227 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
228 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
229 case CODEC_ID_PCM_ALAW:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
230 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
231 v = *samples++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
232 dst[0] = linear_to_alaw[(v + 32768) >> 2];
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
233 dst++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
234 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
235 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
236 case CODEC_ID_PCM_MULAW:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
237 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
238 v = *samples++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
239 dst[0] = linear_to_ulaw[(v + 32768) >> 2];
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
240 dst++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
241 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
242 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
243 default:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
244 return -1;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
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
19b6a1fa6f6d * Every frame is a key_frame
philipjsg
parents: 92
diff changeset
247
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
248 return dst - frame;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
249 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
250
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
251 typedef struct PCMDecode {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
252 short table[256];
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
253 } PCMDecode;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
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
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
256 {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
257 PCMDecode *s = avctx->priv_data;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
258 int i;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
259
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
260 switch(avctx->codec->id) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
261 case CODEC_ID_PCM_ALAW:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
262 for(i=0;i<256;i++)
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
263 s->table[i] = alaw2linear(i);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
264 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
265 case CODEC_ID_PCM_MULAW:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
266 for(i=0;i<256;i++)
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
267 s->table[i] = ulaw2linear(i);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
268 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
269 default:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
270 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
271 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
272 return 0;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
273 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
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,
1064
b32afefe7d33 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 1014
diff changeset
277 uint8_t *buf, int buf_size)
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
278 {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
279 PCMDecode *s = avctx->priv_data;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
280 int n;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
281 short *samples;
1064
b32afefe7d33 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 1014
diff changeset
282 uint8_t *src;
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
283
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
284 samples = data;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
285 src = buf;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
286
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
287 switch(avctx->codec->id) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
288 case CODEC_ID_PCM_S16LE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
289 n = buf_size >> 1;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
290 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
291 *samples++ = src[0] | (src[1] << 8);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
292 src += 2;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
293 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
294 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
295 case CODEC_ID_PCM_S16BE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
296 n = buf_size >> 1;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
297 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
298 *samples++ = (src[0] << 8) | src[1];
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
299 src += 2;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
300 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
301 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
302 case CODEC_ID_PCM_U16LE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
303 n = buf_size >> 1;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
304 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
305 *samples++ = (src[0] | (src[1] << 8)) - 0x8000;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
306 src += 2;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
307 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
308 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
309 case CODEC_ID_PCM_U16BE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
310 n = buf_size >> 1;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
311 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
312 *samples++ = ((src[0] << 8) | src[1]) - 0x8000;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
313 src += 2;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
314 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
315 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
316 case CODEC_ID_PCM_S8:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
317 n = buf_size;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
318 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
319 *samples++ = src[0] << 8;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
320 src++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
321 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
322 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
323 case CODEC_ID_PCM_U8:
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++ = ((int)src[0] - 128) << 8;
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 case CODEC_ID_PCM_ALAW:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
331 case CODEC_ID_PCM_MULAW:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
332 n = buf_size;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
333 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
334 *samples++ = s->table[src[0]];
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
335 src++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
336 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
337 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
338 default:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
339 *data_size = 0;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
340 return -1;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
341 }
1064
b32afefe7d33 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 1014
diff changeset
342 *data_size = (uint8_t *)samples - (uint8_t *)data;
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
343 return src - buf;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
344 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
345
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
346 #define PCM_CODEC(id, name) \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
347 AVCodec name ## _encoder = { \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
348 #name, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
349 CODEC_TYPE_AUDIO, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
350 id, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
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
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
355 NULL, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
356 }; \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
357 AVCodec name ## _decoder = { \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
358 #name, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
359 CODEC_TYPE_AUDIO, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
360 id, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
361 sizeof(PCMDecode), \
440
000aeeac27a2 * started to cleanup name clashes for onetime compilation
kabi
parents: 429
diff changeset
362 pcm_decode_init, \
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
363 NULL, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
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
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
367
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
368 PCM_CODEC(CODEC_ID_PCM_S16LE, pcm_s16le);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
369 PCM_CODEC(CODEC_ID_PCM_S16BE, pcm_s16be);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
370 PCM_CODEC(CODEC_ID_PCM_U16LE, pcm_u16le);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
371 PCM_CODEC(CODEC_ID_PCM_U16BE, pcm_u16be);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
372 PCM_CODEC(CODEC_ID_PCM_S8, pcm_s8);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
373 PCM_CODEC(CODEC_ID_PCM_U8, pcm_u8);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
374 PCM_CODEC(CODEC_ID_PCM_ALAW, pcm_alaw);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
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