annotate pcm.c @ 1492:3518325146fc libavcodec

ac prediction cleanup rate distorted optimal ac prediction for mpeg4
author michaelni
date Wed, 01 Oct 2003 15:41:36 +0000
parents d7bb818768c5
children 934117474c3b
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
7fccaa0d699d AVVideoFrame -> AVFrame
michaelni
parents: 649
diff changeset
130 avctx->coded_frame= avcodec_alloc_frame();
7fccaa0d699d AVVideoFrame -> AVFrame
michaelni
parents: 649
diff changeset
131 avctx->coded_frame->key_frame= 1;
7fccaa0d699d AVVideoFrame -> AVFrame
michaelni
parents: 649
diff changeset
132
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
133 return 0;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
134 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
135
440
000aeeac27a2 * started to cleanup name clashes for onetime compilation
kabi
parents: 429
diff changeset
136 static int pcm_encode_close(AVCodecContext *avctx)
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
137 {
925
7fccaa0d699d AVVideoFrame -> AVFrame
michaelni
parents: 649
diff changeset
138 av_freep(&avctx->coded_frame);
7fccaa0d699d AVVideoFrame -> AVFrame
michaelni
parents: 649
diff changeset
139
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
140 switch(avctx->codec->id) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
141 case CODEC_ID_PCM_ALAW:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
142 if (--linear_to_alaw_ref == 0)
396
fce0a2520551 removed useless header includes - use av memory functions
glantau
parents: 381
diff changeset
143 av_free(linear_to_alaw);
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
144 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
145 case CODEC_ID_PCM_MULAW:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
146 if (--linear_to_ulaw_ref == 0)
396
fce0a2520551 removed useless header includes - use av memory functions
glantau
parents: 381
diff changeset
147 av_free(linear_to_ulaw);
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
148 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
149 default:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
150 /* nothing to free */
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
151 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
152 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
153 return 0;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
154 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
155
440
000aeeac27a2 * started to cleanup name clashes for onetime compilation
kabi
parents: 429
diff changeset
156 static int pcm_encode_frame(AVCodecContext *avctx,
000aeeac27a2 * started to cleanup name clashes for onetime compilation
kabi
parents: 429
diff changeset
157 unsigned char *frame, int buf_size, void *data)
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
158 {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
159 int n, sample_size, v;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
160 short *samples;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
161 unsigned char *dst;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
162
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
163 switch(avctx->codec->id) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
164 case CODEC_ID_PCM_S16LE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
165 case CODEC_ID_PCM_S16BE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
166 case CODEC_ID_PCM_U16LE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
167 case CODEC_ID_PCM_U16BE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
168 sample_size = 2;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
169 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
170 default:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
171 sample_size = 1;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
172 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
173 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
174 n = buf_size / sample_size;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
175 samples = data;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
176 dst = frame;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
177
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
178 switch(avctx->codec->id) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
179 case CODEC_ID_PCM_S16LE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
180 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
181 v = *samples++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
182 dst[0] = v & 0xff;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
183 dst[1] = v >> 8;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
184 dst += 2;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
185 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
186 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
187 case CODEC_ID_PCM_S16BE:
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 >> 8;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
191 dst[1] = v;
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_U16LE:
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 v += 0x8000;
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_U16BE:
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 v += 0x8000;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
208 dst[0] = v >> 8;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
209 dst[1] = v;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
210 dst += 2;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
211 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
212 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
213 case CODEC_ID_PCM_S8:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
214 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
215 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
216 dst[0] = v >> 8;
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
217 dst++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
218 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
219 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
220 case CODEC_ID_PCM_U8:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
221 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
222 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
223 dst[0] = (v >> 8) + 128;
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
224 dst++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
225 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
226 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
227 case CODEC_ID_PCM_ALAW:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
228 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
229 v = *samples++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
230 dst[0] = linear_to_alaw[(v + 32768) >> 2];
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
231 dst++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
232 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
233 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
234 case CODEC_ID_PCM_MULAW:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
235 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
236 v = *samples++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
237 dst[0] = linear_to_ulaw[(v + 32768) >> 2];
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
238 dst++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
239 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
240 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
241 default:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
242 return -1;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
243 }
381
0d6178e4d503 * Mea culpa: it seems that I broke encoding to 8-bit pcm files. This fixes it.
philipjsg
parents: 372
diff changeset
244 //avctx->frame_size = (dst - frame) / (sample_size * avctx->channels);
372
19b6a1fa6f6d * Every frame is a key_frame
philipjsg
parents: 92
diff changeset
245
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
246 return dst - frame;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
247 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
248
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
249 typedef struct PCMDecode {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
250 short table[256];
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
251 } PCMDecode;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
252
440
000aeeac27a2 * started to cleanup name clashes for onetime compilation
kabi
parents: 429
diff changeset
253 static int pcm_decode_init(AVCodecContext * avctx)
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
254 {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
255 PCMDecode *s = avctx->priv_data;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
256 int i;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
257
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
258 switch(avctx->codec->id) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
259 case CODEC_ID_PCM_ALAW:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
260 for(i=0;i<256;i++)
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
261 s->table[i] = alaw2linear(i);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
262 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
263 case CODEC_ID_PCM_MULAW:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
264 for(i=0;i<256;i++)
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
265 s->table[i] = ulaw2linear(i);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
266 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
267 default:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
268 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
269 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
270 return 0;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
271 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
272
440
000aeeac27a2 * started to cleanup name clashes for onetime compilation
kabi
parents: 429
diff changeset
273 static int pcm_decode_frame(AVCodecContext *avctx,
000aeeac27a2 * started to cleanup name clashes for onetime compilation
kabi
parents: 429
diff changeset
274 void *data, int *data_size,
1064
b32afefe7d33 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 1014
diff changeset
275 uint8_t *buf, int buf_size)
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
276 {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
277 PCMDecode *s = avctx->priv_data;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
278 int n;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
279 short *samples;
1064
b32afefe7d33 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 1014
diff changeset
280 uint8_t *src;
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
281
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
282 samples = data;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
283 src = buf;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
284
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
285 switch(avctx->codec->id) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
286 case CODEC_ID_PCM_S16LE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
287 n = buf_size >> 1;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
288 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
289 *samples++ = src[0] | (src[1] << 8);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
290 src += 2;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
291 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
292 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
293 case CODEC_ID_PCM_S16BE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
294 n = buf_size >> 1;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
295 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
296 *samples++ = (src[0] << 8) | src[1];
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
297 src += 2;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
298 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
299 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
300 case CODEC_ID_PCM_U16LE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
301 n = buf_size >> 1;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
302 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
303 *samples++ = (src[0] | (src[1] << 8)) - 0x8000;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
304 src += 2;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
305 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
306 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
307 case CODEC_ID_PCM_U16BE:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
308 n = buf_size >> 1;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
309 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
310 *samples++ = ((src[0] << 8) | src[1]) - 0x8000;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
311 src += 2;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
312 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
313 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
314 case CODEC_ID_PCM_S8:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
315 n = buf_size;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
316 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
317 *samples++ = src[0] << 8;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
318 src++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
319 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
320 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
321 case CODEC_ID_PCM_U8:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
322 n = buf_size;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
323 for(;n>0;n--) {
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
324 *samples++ = ((int)src[0] - 128) << 8;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
325 src++;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
326 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
327 break;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
328 case CODEC_ID_PCM_ALAW:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
329 case CODEC_ID_PCM_MULAW:
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++ = s->table[src[0]];
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 default:
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
337 *data_size = 0;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
338 return -1;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
339 }
1064
b32afefe7d33 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 1014
diff changeset
340 *data_size = (uint8_t *)samples - (uint8_t *)data;
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
341 return src - buf;
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
342 }
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
343
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
344 #define PCM_CODEC(id, name) \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
345 AVCodec name ## _encoder = { \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
346 #name, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
347 CODEC_TYPE_AUDIO, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
348 id, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
349 0, \
440
000aeeac27a2 * started to cleanup name clashes for onetime compilation
kabi
parents: 429
diff changeset
350 pcm_encode_init, \
000aeeac27a2 * started to cleanup name clashes for onetime compilation
kabi
parents: 429
diff changeset
351 pcm_encode_frame, \
000aeeac27a2 * started to cleanup name clashes for onetime compilation
kabi
parents: 429
diff changeset
352 pcm_encode_close, \
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
353 NULL, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
354 }; \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
355 AVCodec name ## _decoder = { \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
356 #name, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
357 CODEC_TYPE_AUDIO, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
358 id, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
359 sizeof(PCMDecode), \
440
000aeeac27a2 * started to cleanup name clashes for onetime compilation
kabi
parents: 429
diff changeset
360 pcm_decode_init, \
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
361 NULL, \
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
362 NULL, \
440
000aeeac27a2 * started to cleanup name clashes for onetime compilation
kabi
parents: 429
diff changeset
363 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
364 }
92
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
365
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
366 PCM_CODEC(CODEC_ID_PCM_S16LE, pcm_s16le);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
367 PCM_CODEC(CODEC_ID_PCM_S16BE, pcm_s16be);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
368 PCM_CODEC(CODEC_ID_PCM_U16LE, pcm_u16le);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
369 PCM_CODEC(CODEC_ID_PCM_U16BE, pcm_u16be);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
370 PCM_CODEC(CODEC_ID_PCM_S8, pcm_s8);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
371 PCM_CODEC(CODEC_ID_PCM_U8, pcm_u8);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
372 PCM_CODEC(CODEC_ID_PCM_ALAW, pcm_alaw);
1d3eb6cdc6b5 added pcm codecs
glantau
parents:
diff changeset
373 PCM_CODEC(CODEC_ID_PCM_MULAW, pcm_mulaw);
440
000aeeac27a2 * started to cleanup name clashes for onetime compilation
kabi
parents: 429
diff changeset
374
000aeeac27a2 * started to cleanup name clashes for onetime compilation
kabi
parents: 429
diff changeset
375 #undef PCM_CODEC