annotate pcm.c @ 2422:18b8b2dcc037 libavcodec

various security fixes and precautionary checks
author michael
date Wed, 12 Jan 2005 00:16:25 +0000
parents a2073e67cb19
children 9404bbf9de07
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
1485
d7bb818768c5 simpler
michaelni
parents: 1108
diff changeset
48 t = a_val & QUANT_MASK;
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
49 seg = ((unsigned)a_val & SEG_MASK) >> SEG_SHIFT;
1485
d7bb818768c5 simpler
michaelni
parents: 1108
diff changeset
50 if(seg) t= (t + t + 1 + 32) << (seg + 2);
d7bb818768c5 simpler
michaelni
parents: 1108
diff changeset
51 else t= (t + t + 1 ) << 3;
d7bb818768c5 simpler
michaelni
parents: 1108
diff changeset
52
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
53 return ((a_val & SIGN_BIT) ? t : -t);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
54 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
55
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
56 static int ulaw2linear(unsigned char u_val)
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
57 {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
58 int t;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
59
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
60 /* Complement to obtain normal u-law value. */
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
61 u_val = ~u_val;
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 * Extract and bias the quantization bits. Then
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
65 * shift up by the segment number and subtract out the bias.
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
66 */
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
67 t = ((u_val & QUANT_MASK) << 3) + BIAS;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
68 t <<= ((unsigned)u_val & SEG_MASK) >> SEG_SHIFT;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
69
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
70 return ((u_val & SIGN_BIT) ? (BIAS - t) : (t - BIAS));
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
71 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
72
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
73 /* 16384 entries per table */
1064
b32afefe7d33 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 1014
diff changeset
74 static uint8_t *linear_to_alaw = NULL;
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
75 static int linear_to_alaw_ref = 0;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
76
1064
b32afefe7d33 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 1014
diff changeset
77 static uint8_t *linear_to_ulaw = NULL;
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
78 static int linear_to_ulaw_ref = 0;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
79
1064
b32afefe7d33 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 1014
diff changeset
80 static void build_xlaw_table(uint8_t *linear_to_xlaw,
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
81 int (*xlaw2linear)(unsigned char),
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
82 int mask)
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
83 {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
84 int i, j, v, v1, v2;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
85
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
86 j = 0;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
87 for(i=0;i<128;i++) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
88 if (i != 127) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
89 v1 = xlaw2linear(i ^ mask);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
90 v2 = xlaw2linear((i + 1) ^ mask);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
91 v = (v1 + v2 + 4) >> 3;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
92 } else {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
93 v = 8192;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
94 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
95 for(;j<v;j++) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
96 linear_to_xlaw[8192 + j] = (i ^ mask);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
97 if (j > 0)
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
98 linear_to_xlaw[8192 - j] = (i ^ (mask ^ 0x80));
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
99 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
100 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
101 linear_to_xlaw[0] = linear_to_xlaw[1];
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
102 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
103
440
000aeeac27a2 * started to cleanup name clashes for onetime compilation
kabi
parents: 429
diff changeset
104 static int pcm_encode_init(AVCodecContext *avctx)
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
105 {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
106 avctx->frame_size = 1;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
107 switch(avctx->codec->id) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
108 case CODEC_ID_PCM_ALAW:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
109 if (linear_to_alaw_ref == 0) {
396
fce0a2520551 removed useless header includes - use av memory functions
glantau
parents: 381
diff changeset
110 linear_to_alaw = av_malloc(16384);
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
111 if (!linear_to_alaw)
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
112 return -1;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
113 build_xlaw_table(linear_to_alaw, alaw2linear, 0xd5);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
114 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
115 linear_to_alaw_ref++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
116 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
117 case CODEC_ID_PCM_MULAW:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
118 if (linear_to_ulaw_ref == 0) {
396
fce0a2520551 removed useless header includes - use av memory functions
glantau
parents: 381
diff changeset
119 linear_to_ulaw = av_malloc(16384);
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
120 if (!linear_to_ulaw)
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
121 return -1;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
122 build_xlaw_table(linear_to_ulaw, ulaw2linear, 0xff);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
123 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
124 linear_to_ulaw_ref++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
125 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
126 default:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
127 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
128 }
925
7fccaa0d699d AVVideoFrame -> AVFrame
michaelni
parents: 649
diff changeset
129
2340
a2073e67cb19 cbr audio muxing fix
michael
parents: 2029
diff changeset
130 switch(avctx->codec->id) {
a2073e67cb19 cbr audio muxing fix
michael
parents: 2029
diff changeset
131 case CODEC_ID_PCM_S16LE:
a2073e67cb19 cbr audio muxing fix
michael
parents: 2029
diff changeset
132 case CODEC_ID_PCM_S16BE:
a2073e67cb19 cbr audio muxing fix
michael
parents: 2029
diff changeset
133 case CODEC_ID_PCM_U16LE:
a2073e67cb19 cbr audio muxing fix
michael
parents: 2029
diff changeset
134 case CODEC_ID_PCM_U16BE:
a2073e67cb19 cbr audio muxing fix
michael
parents: 2029
diff changeset
135 avctx->block_align = 2 * avctx->channels;
a2073e67cb19 cbr audio muxing fix
michael
parents: 2029
diff changeset
136 break;
a2073e67cb19 cbr audio muxing fix
michael
parents: 2029
diff changeset
137 case CODEC_ID_PCM_S8:
a2073e67cb19 cbr audio muxing fix
michael
parents: 2029
diff changeset
138 case CODEC_ID_PCM_U8:
a2073e67cb19 cbr audio muxing fix
michael
parents: 2029
diff changeset
139 case CODEC_ID_PCM_MULAW:
a2073e67cb19 cbr audio muxing fix
michael
parents: 2029
diff changeset
140 case CODEC_ID_PCM_ALAW:
a2073e67cb19 cbr audio muxing fix
michael
parents: 2029
diff changeset
141 avctx->block_align = avctx->channels;
a2073e67cb19 cbr audio muxing fix
michael
parents: 2029
diff changeset
142 break;
a2073e67cb19 cbr audio muxing fix
michael
parents: 2029
diff changeset
143 default:
a2073e67cb19 cbr audio muxing fix
michael
parents: 2029
diff changeset
144 break;
a2073e67cb19 cbr audio muxing fix
michael
parents: 2029
diff changeset
145 }
a2073e67cb19 cbr audio muxing fix
michael
parents: 2029
diff changeset
146
925
7fccaa0d699d AVVideoFrame -> AVFrame
michaelni
parents: 649
diff changeset
147 avctx->coded_frame= avcodec_alloc_frame();
7fccaa0d699d AVVideoFrame -> AVFrame
michaelni
parents: 649
diff changeset
148 avctx->coded_frame->key_frame= 1;
7fccaa0d699d AVVideoFrame -> AVFrame
michaelni
parents: 649
diff changeset
149
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
150 return 0;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
151 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
152
440
000aeeac27a2 * started to cleanup name clashes for onetime compilation
kabi
parents: 429
diff changeset
153 static int pcm_encode_close(AVCodecContext *avctx)
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
154 {
925
7fccaa0d699d AVVideoFrame -> AVFrame
michaelni
parents: 649
diff changeset
155 av_freep(&avctx->coded_frame);
7fccaa0d699d AVVideoFrame -> AVFrame
michaelni
parents: 649
diff changeset
156
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
157 switch(avctx->codec->id) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
158 case CODEC_ID_PCM_ALAW:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
159 if (--linear_to_alaw_ref == 0)
396
fce0a2520551 removed useless header includes - use av memory functions
glantau
parents: 381
diff changeset
160 av_free(linear_to_alaw);
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
161 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
162 case CODEC_ID_PCM_MULAW:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
163 if (--linear_to_ulaw_ref == 0)
396
fce0a2520551 removed useless header includes - use av memory functions
glantau
parents: 381
diff changeset
164 av_free(linear_to_ulaw);
92
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 /* nothing to free */
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 return 0;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
171 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
172
440
000aeeac27a2 * started to cleanup name clashes for onetime compilation
kabi
parents: 429
diff changeset
173 static int pcm_encode_frame(AVCodecContext *avctx,
000aeeac27a2 * started to cleanup name clashes for onetime compilation
kabi
parents: 429
diff changeset
174 unsigned char *frame, int buf_size, void *data)
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
175 {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
176 int n, sample_size, v;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
177 short *samples;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
178 unsigned char *dst;
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 case CODEC_ID_PCM_S16BE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
183 case CODEC_ID_PCM_U16LE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
184 case CODEC_ID_PCM_U16BE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
185 sample_size = 2;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
186 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
187 default:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
188 sample_size = 1;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
189 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
190 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
191 n = buf_size / sample_size;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
192 samples = data;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
193 dst = frame;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
194
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
195 switch(avctx->codec->id) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
196 case CODEC_ID_PCM_S16LE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
197 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
198 v = *samples++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
199 dst[0] = v & 0xff;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
200 dst[1] = v >> 8;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
201 dst += 2;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
202 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
203 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
204 case CODEC_ID_PCM_S16BE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
205 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
206 v = *samples++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
207 dst[0] = v >> 8;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
208 dst[1] = v;
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_U16LE:
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 & 0xff;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
217 dst[1] = v >> 8;
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_U16BE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
222 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
223 v = *samples++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
224 v += 0x8000;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
225 dst[0] = v >> 8;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
226 dst[1] = v;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
227 dst += 2;
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_S8:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
231 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
232 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
233 dst[0] = v >> 8;
92
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 case CODEC_ID_PCM_U8:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
238 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
239 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
240 dst[0] = (v >> 8) + 128;
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
241 dst++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
242 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
243 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
244 case CODEC_ID_PCM_ALAW:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
245 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
246 v = *samples++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
247 dst[0] = linear_to_alaw[(v + 32768) >> 2];
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
248 dst++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
249 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
250 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
251 case CODEC_ID_PCM_MULAW:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
252 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
253 v = *samples++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
254 dst[0] = linear_to_ulaw[(v + 32768) >> 2];
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
255 dst++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
256 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
257 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
258 default:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
259 return -1;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
260 }
381
0d6178e4d503 * Mea culpa: it seems that I broke encoding to 8-bit pcm files. This fixes it.
philipjsg
parents: 372
diff changeset
261 //avctx->frame_size = (dst - frame) / (sample_size * avctx->channels);
372
19b6a1fa6f6d * Every frame is a key_frame
philipjsg
parents: 92
diff changeset
262
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
263 return dst - frame;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
264 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
265
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
266 typedef struct PCMDecode {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
267 short table[256];
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
268 } PCMDecode;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
269
440
000aeeac27a2 * started to cleanup name clashes for onetime compilation
kabi
parents: 429
diff changeset
270 static int pcm_decode_init(AVCodecContext * avctx)
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
271 {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
272 PCMDecode *s = avctx->priv_data;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
273 int i;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
274
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
275 switch(avctx->codec->id) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
276 case CODEC_ID_PCM_ALAW:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
277 for(i=0;i<256;i++)
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
278 s->table[i] = alaw2linear(i);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
279 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
280 case CODEC_ID_PCM_MULAW:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
281 for(i=0;i<256;i++)
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
282 s->table[i] = ulaw2linear(i);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
283 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
284 default:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
285 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
286 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
287 return 0;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
288 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
289
440
000aeeac27a2 * started to cleanup name clashes for onetime compilation
kabi
parents: 429
diff changeset
290 static int pcm_decode_frame(AVCodecContext *avctx,
000aeeac27a2 * started to cleanup name clashes for onetime compilation
kabi
parents: 429
diff changeset
291 void *data, int *data_size,
1064
b32afefe7d33 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 1014
diff changeset
292 uint8_t *buf, int buf_size)
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
293 {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
294 PCMDecode *s = avctx->priv_data;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
295 int n;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
296 short *samples;
1064
b32afefe7d33 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 1014
diff changeset
297 uint8_t *src;
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
298
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
299 samples = data;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
300 src = buf;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
301
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
302 switch(avctx->codec->id) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
303 case CODEC_ID_PCM_S16LE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
304 n = buf_size >> 1;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
305 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
306 *samples++ = src[0] | (src[1] << 8);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
307 src += 2;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
308 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
309 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
310 case CODEC_ID_PCM_S16BE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
311 n = buf_size >> 1;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
312 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
313 *samples++ = (src[0] << 8) | src[1];
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
314 src += 2;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
315 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
316 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
317 case CODEC_ID_PCM_U16LE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
318 n = buf_size >> 1;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
319 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
320 *samples++ = (src[0] | (src[1] << 8)) - 0x8000;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
321 src += 2;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
322 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
323 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
324 case CODEC_ID_PCM_U16BE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
325 n = buf_size >> 1;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
326 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
327 *samples++ = ((src[0] << 8) | src[1]) - 0x8000;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
328 src += 2;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
329 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
330 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
331 case CODEC_ID_PCM_S8:
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++ = src[0] << 8;
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 case CODEC_ID_PCM_U8:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
339 n = buf_size;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
340 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
341 *samples++ = ((int)src[0] - 128) << 8;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
342 src++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
343 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
344 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
345 case CODEC_ID_PCM_ALAW:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
346 case CODEC_ID_PCM_MULAW:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
347 n = buf_size;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
348 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
349 *samples++ = s->table[src[0]];
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
350 src++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
351 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
352 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
353 default:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
354 return -1;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
355 }
1064
b32afefe7d33 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 1014
diff changeset
356 *data_size = (uint8_t *)samples - (uint8_t *)data;
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
357 return src - buf;
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 #define PCM_CODEC(id, name) \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
361 AVCodec name ## _encoder = { \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
362 #name, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
363 CODEC_TYPE_AUDIO, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
364 id, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
365 0, \
440
000aeeac27a2 * started to cleanup name clashes for onetime compilation
kabi
parents: 429
diff changeset
366 pcm_encode_init, \
000aeeac27a2 * started to cleanup name clashes for onetime compilation
kabi
parents: 429
diff changeset
367 pcm_encode_frame, \
000aeeac27a2 * started to cleanup name clashes for onetime compilation
kabi
parents: 429
diff changeset
368 pcm_encode_close, \
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
369 NULL, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
370 }; \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
371 AVCodec name ## _decoder = { \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
372 #name, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
373 CODEC_TYPE_AUDIO, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
374 id, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
375 sizeof(PCMDecode), \
440
000aeeac27a2 * started to cleanup name clashes for onetime compilation
kabi
parents: 429
diff changeset
376 pcm_decode_init, \
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
377 NULL, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
378 NULL, \
440
000aeeac27a2 * started to cleanup name clashes for onetime compilation
kabi
parents: 429
diff changeset
379 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
380 }
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
381
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
382 PCM_CODEC(CODEC_ID_PCM_S16LE, pcm_s16le);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
383 PCM_CODEC(CODEC_ID_PCM_S16BE, pcm_s16be);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
384 PCM_CODEC(CODEC_ID_PCM_U16LE, pcm_u16le);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
385 PCM_CODEC(CODEC_ID_PCM_U16BE, pcm_u16be);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
386 PCM_CODEC(CODEC_ID_PCM_S8, pcm_s8);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
387 PCM_CODEC(CODEC_ID_PCM_U8, pcm_u8);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
388 PCM_CODEC(CODEC_ID_PCM_ALAW, pcm_alaw);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
389 PCM_CODEC(CODEC_ID_PCM_MULAW, pcm_mulaw);
440
000aeeac27a2 * started to cleanup name clashes for onetime compilation
kabi
parents: 429
diff changeset
390
000aeeac27a2 * started to cleanup name clashes for onetime compilation
kabi
parents: 429
diff changeset
391 #undef PCM_CODEC