comparison oggvorbis.c @ 883:b0d29bf1cecd libavcodec

* Ogg/Vorbis patch by Mark Hills
author kabi
date Fri, 22 Nov 2002 07:27:13 +0000
parents 57b9a37546a0
children 7fccaa0d699d
comparison
equal deleted inserted replaced
882:34943cb0c2fb 883:b0d29bf1cecd
15 15
16 typedef struct OggVorbisContext { 16 typedef struct OggVorbisContext {
17 vorbis_info vi ; 17 vorbis_info vi ;
18 vorbis_dsp_state vd ; 18 vorbis_dsp_state vd ;
19 vorbis_block vb ; 19 vorbis_block vb ;
20
21 /* decoder */
22 vorbis_comment vc ;
20 } OggVorbisContext ; 23 } OggVorbisContext ;
21 24
22 25
23 int oggvorbis_init_encoder(vorbis_info *vi, AVCodecContext *avccontext) { 26 int oggvorbis_init_encoder(vorbis_info *vi, AVCodecContext *avccontext) {
24 if(avccontext->quality) { /* VBR requested */ 27 if(avccontext->quality) /* VBR requested */
25
26 fprintf(stderr, "init_encode: channels=%d quality=%d\n",
27 avccontext->channels, avccontext->quality) ;
28
29 return vorbis_encode_init_vbr(vi, avccontext->channels, 28 return vorbis_encode_init_vbr(vi, avccontext->channels,
30 avccontext->sample_rate, (float)avccontext->quality / 1000) ; 29 avccontext->sample_rate, (float)avccontext->quality / 1000) ;
31 }
32
33 fprintf(stderr, "init_encoder: channels=%d bitrate=%d tolerance=%d\n",
34 avccontext->channels, avccontext->bit_rate,
35 avccontext->bit_rate_tolerance) ;
36 30
37 return vorbis_encode_init(vi, avccontext->channels, 31 return vorbis_encode_init(vi, avccontext->channels,
38 avccontext->sample_rate, -1, avccontext->bit_rate, -1) ; 32 avccontext->sample_rate, -1, avccontext->bit_rate, -1) ;
39 } 33 }
40 34
41 35
42 static int oggvorbis_encode_init(AVCodecContext *avccontext) { 36 static int oggvorbis_encode_init(AVCodecContext *avccontext) {
43 OggVorbisContext *context = avccontext->priv_data ; 37 OggVorbisContext *context = avccontext->priv_data ;
44 38
45 fprintf(stderr, "oggvorbis_encode_init\n") ;
46
47 vorbis_info_init(&context->vi) ; 39 vorbis_info_init(&context->vi) ;
48
49 if(oggvorbis_init_encoder(&context->vi, avccontext) < 0) { 40 if(oggvorbis_init_encoder(&context->vi, avccontext) < 0) {
50 fprintf(stderr, "oggvorbis_encode_init: init_encoder failed") ; 41 fprintf(stderr, "oggvorbis_encode_init: init_encoder failed") ;
51 return -1 ; 42 return -1 ;
52 } 43 }
53
54 vorbis_analysis_init(&context->vd, &context->vi) ; 44 vorbis_analysis_init(&context->vd, &context->vi) ;
55 vorbis_block_init(&context->vd, &context->vb) ; 45 vorbis_block_init(&context->vd, &context->vb) ;
56 46
57 avccontext->frame_size = OGGVORBIS_FRAME_SIZE ; 47 avccontext->frame_size = OGGVORBIS_FRAME_SIZE ;
58 48
59 return 0 ; 49 return 0 ;
60 } 50 }
61 51
62 52
63 int oggvorbis_encode_frame(AVCodecContext *avccontext, unsigned char *packets, 53 static int oggvorbis_encode_frame(AVCodecContext *avccontext,
54 unsigned char *packets,
64 int buf_size, void *data) 55 int buf_size, void *data)
65 { 56 {
66 OggVorbisContext *context = avccontext->priv_data ; 57 OggVorbisContext *context = avccontext->priv_data ;
67 float **buffer ; 58 float **buffer ;
68 ogg_packet op ; 59 ogg_packet op ;
69 signed char *audio = data ; 60 signed char *audio = data ;
70 int l, samples = buf_size / 16 ; /* samples = OGGVORBIS_FRAME_SIZE */ ; 61 int l, samples = OGGVORBIS_FRAME_SIZE ;
71 62
72 buffer = vorbis_analysis_buffer(&context->vd, samples) ; 63 buffer = vorbis_analysis_buffer(&context->vd, samples) ;
73 64
74 if(context->vi.channels == 1) { 65 if(context->vi.channels == 1) {
75 for(l = 0 ; l < samples ; l++) 66 for(l = 0 ; l < samples ; l++)
98 89
99 return l ; 90 return l ;
100 } 91 }
101 92
102 93
103 int oggvorbis_encode_close(AVCodecContext *avccontext) { 94 static int oggvorbis_encode_close(AVCodecContext *avccontext) {
104 OggVorbisContext *context = avccontext->priv_data ; 95 OggVorbisContext *context = avccontext->priv_data ;
105 /* ogg_packet op ; */ 96 /* ogg_packet op ; */
106
107 fprintf(stderr, "oggvorbis_encode_close\n") ;
108 97
109 vorbis_analysis_wrote(&context->vd, 0) ; /* notify vorbisenc this is EOF */ 98 vorbis_analysis_wrote(&context->vd, 0) ; /* notify vorbisenc this is EOF */
110 99
111 /* We need to write all the remaining packets into the stream 100 /* We need to write all the remaining packets into the stream
112 * on closing */ 101 * on closing */
113 102
103 fprintf(stderr, "fixme: not all packets written on oggvorbis_encode_close()\n") ;
104
114 /* 105 /*
115 while(vorbis_bitrate_flushpacket(&context->vd, &op)) { 106 while(vorbis_bitrate_flushpacket(&context->vd, &op)) {
116 memcpy(packets + l, &op, sizeof(ogg_packet)) ; 107 memcpy(packets + l, &op, sizeof(ogg_packet)) ;
117 memcpy(packets + l + sizeof(ogg_packet), op.packet, op.bytes) ; 108 memcpy(packets + l + sizeof(ogg_packet), op.packet, op.bytes) ;
118 l += sizeof(ogg_packet) + op.bytes ; 109 l += sizeof(ogg_packet) + op.bytes ;
133 CODEC_ID_VORBIS, 124 CODEC_ID_VORBIS,
134 sizeof(OggVorbisContext), 125 sizeof(OggVorbisContext),
135 oggvorbis_encode_init, 126 oggvorbis_encode_init,
136 oggvorbis_encode_frame, 127 oggvorbis_encode_frame,
137 oggvorbis_encode_close 128 oggvorbis_encode_close
138 }; 129 } ;
139 130
140 131
132 static int oggvorbis_decode_init(AVCodecContext *avccontext) {
133 OggVorbisContext *context = avccontext->priv_data ;
134
135 vorbis_info_init(&context->vi) ;
136 vorbis_comment_init(&context->vc) ;
137
138 return 0 ;
139 }
140
141
142 static inline int conv(int samples, float **pcm, char *buf, int channels) {
143 int i, j, val ;
144 ogg_int16_t *ptr, *data = (ogg_int16_t*)buf ;
145 float *mono ;
146
147 for(i = 0 ; i < channels ; i++){
148 ptr = &data[i];
149 mono = pcm[i] ;
150
151 for(j = 0 ; j < samples ; j++) {
152
153 val = mono[j] * 32767.f;
154
155 if(val > 32767) val = 32767 ;
156 if(val < -32768) val = -32768 ;
157
158 *ptr = val ;
159 ptr += channels;
160 }
161 }
162
163 return 0 ;
164 }
165
166
167 static int oggvorbis_decode_frame(AVCodecContext *avccontext,
168 void *data, int *data_size,
169 UINT8 *buf, int buf_size)
170 {
171 OggVorbisContext *context = avccontext->priv_data ;
172 ogg_packet *op = (ogg_packet*)buf ;
173 float **pcm ;
174 int samples, total_samples, total_bytes ;
175
176 op->packet = (char*)op + sizeof(ogg_packet) ; /* correct data pointer */
177
178 if(op->packetno < 3) {
179 vorbis_synthesis_headerin(&context->vi, &context->vc, op) ;
180 return buf_size ;
181 }
182
183 if(op->packetno == 3) {
184 fprintf(stderr, "vorbis_decode: %d channel, %ldHz, encoder `%s'\n",
185 context->vi.channels, context->vi.rate, context->vc.vendor);
186
187 avccontext->channels = context->vi.channels ;
188 avccontext->sample_rate = context->vi.rate ;
189
190 vorbis_synthesis_init(&context->vd, &context->vi) ;
191 vorbis_block_init(&context->vd, &context->vb);
192 }
193
194 if(vorbis_synthesis(&context->vb, op) == 0)
195 vorbis_synthesis_blockin(&context->vd, &context->vb) ;
196
197 total_samples = 0 ;
198 total_bytes = 0 ;
199
200 while((samples = vorbis_synthesis_pcmout(&context->vd, &pcm)) > 0) {
201 conv(samples, pcm, (char*)data + total_bytes, context->vi.channels) ;
202 total_bytes += samples * 2 * context->vi.channels ;
203 total_samples += samples ;
204 vorbis_synthesis_read(&context->vd, samples) ;
205 }
206
207 *data_size = total_bytes ;
208 return buf_size ;
209 }
210
211
212 static int oggvorbis_decode_close(AVCodecContext *avccontext) {
213 OggVorbisContext *context = avccontext->priv_data ;
214
215 vorbis_info_clear(&context->vi) ;
216 vorbis_comment_clear(&context->vc) ;
217
218 return 0 ;
219 }
220
221
222 AVCodec oggvorbis_decoder = {
223 "vorbis",
224 CODEC_TYPE_AUDIO,
225 CODEC_ID_VORBIS,
226 sizeof(OggVorbisContext),
227 oggvorbis_decode_init,
228 NULL,
229 oggvorbis_decode_close,
230 oggvorbis_decode_frame,
231 } ;