Mercurial > libavcodec.hg
comparison oggvorbis.c @ 2979:bfabfdf9ce55 libavcodec
COSMETICS: tabs --> spaces, some prettyprinting
author | diego |
---|---|
date | Thu, 22 Dec 2005 01:10:11 +0000 |
parents | ef2149182f1c |
children | 8936371f5a5c |
comparison
equal
deleted
inserted
replaced
2978:403183bbb505 | 2979:bfabfdf9ce55 |
---|---|
38 #ifdef OGGVORBIS_VBR_BY_ESTIMATE | 38 #ifdef OGGVORBIS_VBR_BY_ESTIMATE |
39 /* variable bitrate by estimate */ | 39 /* variable bitrate by estimate */ |
40 | 40 |
41 return (vorbis_encode_setup_managed(vi, avccontext->channels, | 41 return (vorbis_encode_setup_managed(vi, avccontext->channels, |
42 avccontext->sample_rate, -1, avccontext->bit_rate, -1) || | 42 avccontext->sample_rate, -1, avccontext->bit_rate, -1) || |
43 vorbis_encode_ctl(vi, OV_ECTL_RATEMANAGE_AVG, NULL) || | 43 vorbis_encode_ctl(vi, OV_ECTL_RATEMANAGE_AVG, NULL) || |
44 vorbis_encode_setup_init(vi)) ; | 44 vorbis_encode_setup_init(vi)) ; |
45 #else | 45 #else |
46 /* constant bitrate */ | 46 /* constant bitrate */ |
47 | 47 |
48 return vorbis_encode_init(vi, avccontext->channels, | 48 return vorbis_encode_init(vi, avccontext->channels, |
49 avccontext->sample_rate, -1, avccontext->bit_rate, -1) ; | 49 avccontext->sample_rate, -1, avccontext->bit_rate, -1) ; |
50 #endif | 50 #endif |
51 } | 51 } |
52 | 52 |
53 static int oggvorbis_encode_init(AVCodecContext *avccontext) { | 53 static int oggvorbis_encode_init(AVCodecContext *avccontext) { |
54 OggVorbisContext *context = avccontext->priv_data ; | 54 OggVorbisContext *context = avccontext->priv_data ; |
56 uint8_t *p; | 56 uint8_t *p; |
57 unsigned int offset, len; | 57 unsigned int offset, len; |
58 | 58 |
59 vorbis_info_init(&context->vi) ; | 59 vorbis_info_init(&context->vi) ; |
60 if(oggvorbis_init_encoder(&context->vi, avccontext) < 0) { | 60 if(oggvorbis_init_encoder(&context->vi, avccontext) < 0) { |
61 av_log(avccontext, AV_LOG_ERROR, "oggvorbis_encode_init: init_encoder failed") ; | 61 av_log(avccontext, AV_LOG_ERROR, "oggvorbis_encode_init: init_encoder failed") ; |
62 return -1 ; | 62 return -1 ; |
63 } | 63 } |
64 vorbis_analysis_init(&context->vd, &context->vi) ; | 64 vorbis_analysis_init(&context->vd, &context->vi) ; |
65 vorbis_block_init(&context->vd, &context->vb) ; | 65 vorbis_block_init(&context->vd, &context->vb) ; |
66 | 66 |
67 vorbis_comment_init(&context->vc); | 67 vorbis_comment_init(&context->vc); |
99 return 0 ; | 99 return 0 ; |
100 } | 100 } |
101 | 101 |
102 | 102 |
103 static int oggvorbis_encode_frame(AVCodecContext *avccontext, | 103 static int oggvorbis_encode_frame(AVCodecContext *avccontext, |
104 unsigned char *packets, | 104 unsigned char *packets, |
105 int buf_size, void *data) | 105 int buf_size, void *data) |
106 { | 106 { |
107 OggVorbisContext *context = avccontext->priv_data ; | 107 OggVorbisContext *context = avccontext->priv_data ; |
108 float **buffer ; | 108 float **buffer ; |
109 ogg_packet op ; | 109 ogg_packet op ; |
110 signed short *audio = data ; | 110 signed short *audio = data ; |
111 int l, samples = data ? OGGVORBIS_FRAME_SIZE : 0; | 111 int l, samples = data ? OGGVORBIS_FRAME_SIZE : 0; |
112 | 112 |
113 buffer = vorbis_analysis_buffer(&context->vd, samples) ; | 113 buffer = vorbis_analysis_buffer(&context->vd, samples) ; |
114 | 114 |
115 if(context->vi.channels == 1) { | 115 if(context->vi.channels == 1) { |
116 for(l = 0 ; l < samples ; l++) | 116 for(l = 0 ; l < samples ; l++) |
117 buffer[0][l]=audio[l]/32768.f; | 117 buffer[0][l]=audio[l]/32768.f; |
118 } else { | 118 } else { |
119 for(l = 0 ; l < samples ; l++){ | 119 for(l = 0 ; l < samples ; l++){ |
120 buffer[0][l]=audio[l*2]/32768.f; | 120 buffer[0][l]=audio[l*2]/32768.f; |
121 buffer[1][l]=audio[l*2+1]/32768.f; | 121 buffer[1][l]=audio[l*2+1]/32768.f; |
122 } | 122 } |
123 } | 123 } |
124 | 124 |
125 vorbis_analysis_wrote(&context->vd, samples) ; | 125 vorbis_analysis_wrote(&context->vd, samples) ; |
126 | 126 |
127 while(vorbis_analysis_blockout(&context->vd, &context->vb) == 1) { | 127 while(vorbis_analysis_blockout(&context->vd, &context->vb) == 1) { |
128 vorbis_analysis(&context->vb, NULL); | 128 vorbis_analysis(&context->vb, NULL); |
129 vorbis_bitrate_addblock(&context->vb) ; | 129 vorbis_bitrate_addblock(&context->vb) ; |
130 | 130 |
131 while(vorbis_bitrate_flushpacket(&context->vd, &op)) { | 131 while(vorbis_bitrate_flushpacket(&context->vd, &op)) { |
132 if(op.bytes==1) //id love to say this is a hack, bad sadly its not, appearently the end of stream decission is in libogg | 132 if(op.bytes==1) //id love to say this is a hack, bad sadly its not, appearently the end of stream decission is in libogg |
133 continue; | 133 continue; |
134 memcpy(context->buffer + context->buffer_index, &op, sizeof(ogg_packet)); | 134 memcpy(context->buffer + context->buffer_index, &op, sizeof(ogg_packet)); |
135 context->buffer_index += sizeof(ogg_packet); | 135 context->buffer_index += sizeof(ogg_packet); |
136 memcpy(context->buffer + context->buffer_index, op.packet, op.bytes); | 136 memcpy(context->buffer + context->buffer_index, op.packet, op.bytes); |
137 context->buffer_index += op.bytes; | 137 context->buffer_index += op.bytes; |
138 // av_log(avccontext, AV_LOG_DEBUG, "e%d / %d\n", context->buffer_index, op.bytes); | 138 // av_log(avccontext, AV_LOG_DEBUG, "e%d / %d\n", context->buffer_index, op.bytes); |
139 } | 139 } |
140 } | 140 } |
141 | 141 |
142 l=0; | 142 l=0; |
143 if(context->buffer_index){ | 143 if(context->buffer_index){ |
144 ogg_packet *op2= (ogg_packet*)context->buffer; | 144 ogg_packet *op2= (ogg_packet*)context->buffer; |
266 int i, j, val ; | 266 int i, j, val ; |
267 ogg_int16_t *ptr, *data = (ogg_int16_t*)buf ; | 267 ogg_int16_t *ptr, *data = (ogg_int16_t*)buf ; |
268 float *mono ; | 268 float *mono ; |
269 | 269 |
270 for(i = 0 ; i < channels ; i++){ | 270 for(i = 0 ; i < channels ; i++){ |
271 ptr = &data[i]; | 271 ptr = &data[i]; |
272 mono = pcm[i] ; | 272 mono = pcm[i] ; |
273 | 273 |
274 for(j = 0 ; j < samples ; j++) { | 274 for(j = 0 ; j < samples ; j++) { |
275 | 275 |
276 val = mono[j] * 32767.f; | 276 val = mono[j] * 32767.f; |
277 | 277 |
278 if(val > 32767) val = 32767 ; | 278 if(val > 32767) val = 32767 ; |
279 if(val < -32768) val = -32768 ; | 279 if(val < -32768) val = -32768 ; |
280 | 280 |
281 *ptr = val ; | 281 *ptr = val ; |
282 ptr += channels; | 282 ptr += channels; |
283 } | 283 } |
284 } | 284 } |
285 | 285 |
286 return 0 ; | 286 return 0 ; |
287 } | 287 } |
288 | 288 |
309 /* for(i=0; i<op->bytes; i++) | 309 /* for(i=0; i<op->bytes; i++) |
310 av_log(avccontext, AV_LOG_DEBUG, "%02X ", op->packet[i]); | 310 av_log(avccontext, AV_LOG_DEBUG, "%02X ", op->packet[i]); |
311 av_log(avccontext, AV_LOG_DEBUG, "\n");*/ | 311 av_log(avccontext, AV_LOG_DEBUG, "\n");*/ |
312 | 312 |
313 if(vorbis_synthesis(&context->vb, op) == 0) | 313 if(vorbis_synthesis(&context->vb, op) == 0) |
314 vorbis_synthesis_blockin(&context->vd, &context->vb) ; | 314 vorbis_synthesis_blockin(&context->vd, &context->vb) ; |
315 | 315 |
316 total_samples = 0 ; | 316 total_samples = 0 ; |
317 total_bytes = 0 ; | 317 total_bytes = 0 ; |
318 | 318 |
319 while((samples = vorbis_synthesis_pcmout(&context->vd, &pcm)) > 0) { | 319 while((samples = vorbis_synthesis_pcmout(&context->vd, &pcm)) > 0) { |
320 conv(samples, pcm, (char*)data + total_bytes, context->vi.channels) ; | 320 conv(samples, pcm, (char*)data + total_bytes, context->vi.channels) ; |
321 total_bytes += samples * 2 * context->vi.channels ; | 321 total_bytes += samples * 2 * context->vi.channels ; |
322 total_samples += samples ; | 322 total_samples += samples ; |
323 vorbis_synthesis_read(&context->vd, samples) ; | 323 vorbis_synthesis_read(&context->vd, samples) ; |
324 } | 324 } |
325 | 325 |
326 *data_size = total_bytes ; | 326 *data_size = total_bytes ; |
327 return buf_size ; | 327 return buf_size ; |