annotate pcm.c @ 1447:7c73537bc994 libavcodec

changed my mind about CODEC_ID_MPEGVIDEO - CODEC_ID_MP3LAME is absolete
author bellard
date Mon, 08 Sep 2003 22:03:29 +0000
parents bfc18110d4b6
children d7bb818768c5
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 */
1108
bfc18110d4b6 typos & cosmetics
michaelni
parents: 1064
diff changeset
19
bfc18110d4b6 typos & cosmetics
michaelni
parents: 1064
diff changeset
20 /**
bfc18110d4b6 typos & cosmetics
michaelni
parents: 1064
diff changeset
21 * @file pcm.c
bfc18110d4b6 typos & cosmetics
michaelni
parents: 1064
diff changeset
22 * PCM codecs
bfc18110d4b6 typos & cosmetics
michaelni
parents: 1064
diff changeset
23 */
bfc18110d4b6 typos & cosmetics
michaelni
parents: 1064
diff changeset
24
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
25 #include "avcodec.h"
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
26
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
27 /* from g711.c by SUN microsystems (unrestricted use) */
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
28
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
29 #define SIGN_BIT (0x80) /* Sign bit for a A-law byte. */
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
30 #define QUANT_MASK (0xf) /* Quantization field mask. */
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
31 #define NSEGS (8) /* Number of A-law segments. */
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
32 #define SEG_SHIFT (4) /* Left shift for segment number. */
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
33 #define SEG_MASK (0x70) /* Segment field mask. */
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
34
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
35 #define BIAS (0x84) /* Bias for linear code. */
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
36
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
37 /*
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
38 * alaw2linear() - Convert an A-law value to 16-bit linear PCM
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
39 *
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
40 */
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
41 static int alaw2linear(unsigned char a_val)
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
42 {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
43 int t;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
44 int seg;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
45
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
46 a_val ^= 0x55;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
47
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
48 t = (a_val & QUANT_MASK) << 4;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
49 seg = ((unsigned)a_val & SEG_MASK) >> SEG_SHIFT;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
50 switch (seg) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
51 case 0:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
52 t += 8;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
53 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
54 case 1:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
55 t += 0x108;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
56 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
57 default:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
58 t += 0x108;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
59 t <<= seg - 1;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
60 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
61 return ((a_val & SIGN_BIT) ? t : -t);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
62 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
63
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
64 static int ulaw2linear(unsigned char u_val)
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
65 {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
66 int t;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
67
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
68 /* Complement to obtain normal u-law value. */
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
69 u_val = ~u_val;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
70
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
71 /*
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
72 * Extract and bias the quantization bits. Then
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
73 * shift up by the segment number and subtract out the bias.
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
74 */
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
75 t = ((u_val & QUANT_MASK) << 3) + BIAS;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
76 t <<= ((unsigned)u_val & SEG_MASK) >> SEG_SHIFT;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
77
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
78 return ((u_val & SIGN_BIT) ? (BIAS - t) : (t - BIAS));
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
79 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
80
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
81 /* 16384 entries per table */
1064
b32afefe7d33 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 1014
diff changeset
82 static uint8_t *linear_to_alaw = NULL;
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
83 static int linear_to_alaw_ref = 0;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
84
1064
b32afefe7d33 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 1014
diff changeset
85 static uint8_t *linear_to_ulaw = NULL;
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
86 static int linear_to_ulaw_ref = 0;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
87
1064
b32afefe7d33 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 1014
diff changeset
88 static void build_xlaw_table(uint8_t *linear_to_xlaw,
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
89 int (*xlaw2linear)(unsigned char),
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
90 int mask)
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
91 {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
92 int i, j, v, v1, v2;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
93
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
94 j = 0;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
95 for(i=0;i<128;i++) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
96 if (i != 127) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
97 v1 = xlaw2linear(i ^ mask);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
98 v2 = xlaw2linear((i + 1) ^ mask);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
99 v = (v1 + v2 + 4) >> 3;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
100 } else {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
101 v = 8192;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
102 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
103 for(;j<v;j++) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
104 linear_to_xlaw[8192 + j] = (i ^ mask);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
105 if (j > 0)
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
106 linear_to_xlaw[8192 - j] = (i ^ (mask ^ 0x80));
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
107 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
108 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
109 linear_to_xlaw[0] = linear_to_xlaw[1];
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
110 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
111
440
000aeeac27a2 * started to cleanup name clashes for onetime compilation
kabi
parents: 429
diff changeset
112 static int pcm_encode_init(AVCodecContext *avctx)
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
113 {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
114 avctx->frame_size = 1;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
115 switch(avctx->codec->id) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
116 case CODEC_ID_PCM_ALAW:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
117 if (linear_to_alaw_ref == 0) {
396
fce0a2520551 removed useless header includes - use av memory functions
glantau
parents: 381
diff changeset
118 linear_to_alaw = av_malloc(16384);
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
119 if (!linear_to_alaw)
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
120 return -1;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
121 build_xlaw_table(linear_to_alaw, alaw2linear, 0xd5);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
122 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
123 linear_to_alaw_ref++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
124 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
125 case CODEC_ID_PCM_MULAW:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
126 if (linear_to_ulaw_ref == 0) {
396
fce0a2520551 removed useless header includes - use av memory functions
glantau
parents: 381
diff changeset
127 linear_to_ulaw = av_malloc(16384);
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
128 if (!linear_to_ulaw)
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
129 return -1;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
130 build_xlaw_table(linear_to_ulaw, ulaw2linear, 0xff);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
131 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
132 linear_to_ulaw_ref++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
133 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
134 default:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
135 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
136 }
925
7fccaa0d699d AVVideoFrame -> AVFrame
michaelni
parents: 649
diff changeset
137
7fccaa0d699d AVVideoFrame -> AVFrame
michaelni
parents: 649
diff changeset
138 avctx->coded_frame= avcodec_alloc_frame();
7fccaa0d699d AVVideoFrame -> AVFrame
michaelni
parents: 649
diff changeset
139 avctx->coded_frame->key_frame= 1;
7fccaa0d699d AVVideoFrame -> AVFrame
michaelni
parents: 649
diff changeset
140
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
141 return 0;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
142 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
143
440
000aeeac27a2 * started to cleanup name clashes for onetime compilation
kabi
parents: 429
diff changeset
144 static int pcm_encode_close(AVCodecContext *avctx)
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
145 {
925
7fccaa0d699d AVVideoFrame -> AVFrame
michaelni
parents: 649
diff changeset
146 av_freep(&avctx->coded_frame);
7fccaa0d699d AVVideoFrame -> AVFrame
michaelni
parents: 649
diff changeset
147
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
148 switch(avctx->codec->id) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
149 case CODEC_ID_PCM_ALAW:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
150 if (--linear_to_alaw_ref == 0)
396
fce0a2520551 removed useless header includes - use av memory functions
glantau
parents: 381
diff changeset
151 av_free(linear_to_alaw);
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
152 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
153 case CODEC_ID_PCM_MULAW:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
154 if (--linear_to_ulaw_ref == 0)
396
fce0a2520551 removed useless header includes - use av memory functions
glantau
parents: 381
diff changeset
155 av_free(linear_to_ulaw);
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
156 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
157 default:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
158 /* nothing to free */
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
159 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
160 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
161 return 0;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
162 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
163
440
000aeeac27a2 * started to cleanup name clashes for onetime compilation
kabi
parents: 429
diff changeset
164 static int pcm_encode_frame(AVCodecContext *avctx,
000aeeac27a2 * started to cleanup name clashes for onetime compilation
kabi
parents: 429
diff changeset
165 unsigned char *frame, int buf_size, void *data)
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
166 {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
167 int n, sample_size, v;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
168 short *samples;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
169 unsigned char *dst;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
170
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
171 switch(avctx->codec->id) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
172 case CODEC_ID_PCM_S16LE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
173 case CODEC_ID_PCM_S16BE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
174 case CODEC_ID_PCM_U16LE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
175 case CODEC_ID_PCM_U16BE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
176 sample_size = 2;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
177 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
178 default:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
179 sample_size = 1;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
180 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
181 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
182 n = buf_size / sample_size;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
183 samples = data;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
184 dst = frame;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
185
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
186 switch(avctx->codec->id) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
187 case CODEC_ID_PCM_S16LE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
188 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
189 v = *samples++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
190 dst[0] = v & 0xff;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
191 dst[1] = v >> 8;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
192 dst += 2;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
193 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
194 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
195 case CODEC_ID_PCM_S16BE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
196 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
197 v = *samples++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
198 dst[0] = v >> 8;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
199 dst[1] = v;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
200 dst += 2;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
201 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
202 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
203 case CODEC_ID_PCM_U16LE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
204 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
205 v = *samples++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
206 v += 0x8000;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
207 dst[0] = v & 0xff;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
208 dst[1] = v >> 8;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
209 dst += 2;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
210 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
211 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
212 case CODEC_ID_PCM_U16BE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
213 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
214 v = *samples++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
215 v += 0x8000;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
216 dst[0] = v >> 8;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
217 dst[1] = v;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
218 dst += 2;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
219 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
220 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
221 case CODEC_ID_PCM_S8:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
222 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
223 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
224 dst[0] = v >> 8;
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
225 dst++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
226 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
227 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
228 case CODEC_ID_PCM_U8:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
229 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
230 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
231 dst[0] = (v >> 8) + 128;
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
232 dst++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
233 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
234 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
235 case CODEC_ID_PCM_ALAW:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
236 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
237 v = *samples++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
238 dst[0] = linear_to_alaw[(v + 32768) >> 2];
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
239 dst++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
240 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
241 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
242 case CODEC_ID_PCM_MULAW:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
243 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
244 v = *samples++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
245 dst[0] = linear_to_ulaw[(v + 32768) >> 2];
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
246 dst++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
247 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
248 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
249 default:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
250 return -1;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
251 }
381
0d6178e4d503 * Mea culpa: it seems that I broke encoding to 8-bit pcm files. This fixes it.
philipjsg
parents: 372
diff changeset
252 //avctx->frame_size = (dst - frame) / (sample_size * avctx->channels);
372
19b6a1fa6f6d * Every frame is a key_frame
philipjsg
parents: 92
diff changeset
253
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
254 return dst - frame;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
255 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
256
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
257 typedef struct PCMDecode {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
258 short table[256];
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
259 } PCMDecode;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
260
440
000aeeac27a2 * started to cleanup name clashes for onetime compilation
kabi
parents: 429
diff changeset
261 static int pcm_decode_init(AVCodecContext * avctx)
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
262 {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
263 PCMDecode *s = avctx->priv_data;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
264 int i;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
265
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
266 switch(avctx->codec->id) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
267 case CODEC_ID_PCM_ALAW:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
268 for(i=0;i<256;i++)
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
269 s->table[i] = alaw2linear(i);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
270 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
271 case CODEC_ID_PCM_MULAW:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
272 for(i=0;i<256;i++)
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
273 s->table[i] = ulaw2linear(i);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
274 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
275 default:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
276 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
277 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
278 return 0;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
279 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
280
440
000aeeac27a2 * started to cleanup name clashes for onetime compilation
kabi
parents: 429
diff changeset
281 static int pcm_decode_frame(AVCodecContext *avctx,
000aeeac27a2 * started to cleanup name clashes for onetime compilation
kabi
parents: 429
diff changeset
282 void *data, int *data_size,
1064
b32afefe7d33 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 1014
diff changeset
283 uint8_t *buf, int buf_size)
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
284 {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
285 PCMDecode *s = avctx->priv_data;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
286 int n;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
287 short *samples;
1064
b32afefe7d33 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 1014
diff changeset
288 uint8_t *src;
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
289
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
290 samples = data;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
291 src = buf;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
292
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
293 switch(avctx->codec->id) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
294 case CODEC_ID_PCM_S16LE:
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);
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_S16BE:
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];
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_U16LE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
309 n = buf_size >> 1;
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] | (src[1] << 8)) - 0x8000;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
312 src += 2;
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_U16BE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
316 n = buf_size >> 1;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
317 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
318 *samples++ = ((src[0] << 8) | src[1]) - 0x8000;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
319 src += 2;
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_S8:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
323 n = buf_size;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
324 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
325 *samples++ = src[0] << 8;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
326 src++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
327 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
328 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
329 case CODEC_ID_PCM_U8:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
330 n = buf_size;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
331 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
332 *samples++ = ((int)src[0] - 128) << 8;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
333 src++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
334 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
335 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
336 case CODEC_ID_PCM_ALAW:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
337 case CODEC_ID_PCM_MULAW:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
338 n = buf_size;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
339 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
340 *samples++ = s->table[src[0]];
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
341 src++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
342 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
343 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
344 default:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
345 *data_size = 0;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
346 return -1;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
347 }
1064
b32afefe7d33 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 1014
diff changeset
348 *data_size = (uint8_t *)samples - (uint8_t *)data;
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
349 return src - buf;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
350 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
351
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
352 #define PCM_CODEC(id, name) \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
353 AVCodec name ## _encoder = { \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
354 #name, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
355 CODEC_TYPE_AUDIO, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
356 id, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
357 0, \
440
000aeeac27a2 * started to cleanup name clashes for onetime compilation
kabi
parents: 429
diff changeset
358 pcm_encode_init, \
000aeeac27a2 * started to cleanup name clashes for onetime compilation
kabi
parents: 429
diff changeset
359 pcm_encode_frame, \
000aeeac27a2 * started to cleanup name clashes for onetime compilation
kabi
parents: 429
diff changeset
360 pcm_encode_close, \
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
361 NULL, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
362 }; \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
363 AVCodec name ## _decoder = { \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
364 #name, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
365 CODEC_TYPE_AUDIO, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
366 id, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
367 sizeof(PCMDecode), \
440
000aeeac27a2 * started to cleanup name clashes for onetime compilation
kabi
parents: 429
diff changeset
368 pcm_decode_init, \
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
369 NULL, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
370 NULL, \
440
000aeeac27a2 * started to cleanup name clashes for onetime compilation
kabi
parents: 429
diff changeset
371 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
372 }
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
373
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
374 PCM_CODEC(CODEC_ID_PCM_S16LE, pcm_s16le);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
375 PCM_CODEC(CODEC_ID_PCM_S16BE, pcm_s16be);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
376 PCM_CODEC(CODEC_ID_PCM_U16LE, pcm_u16le);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
377 PCM_CODEC(CODEC_ID_PCM_U16BE, pcm_u16be);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
378 PCM_CODEC(CODEC_ID_PCM_S8, pcm_s8);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
379 PCM_CODEC(CODEC_ID_PCM_U8, pcm_u8);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
380 PCM_CODEC(CODEC_ID_PCM_ALAW, pcm_alaw);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
381 PCM_CODEC(CODEC_ID_PCM_MULAW, pcm_mulaw);
440
000aeeac27a2 * started to cleanup name clashes for onetime compilation
kabi
parents: 429
diff changeset
382
000aeeac27a2 * started to cleanup name clashes for onetime compilation
kabi
parents: 429
diff changeset
383 #undef PCM_CODEC