3 * Ogg Vorbis codec support via libvorbisenc.
4 * @author Mark Hills <mark@pogo.org.uk>
7 #include <vorbis/vorbisenc.h>
11 //#define OGGVORBIS_FRAME_SIZE 1024
12 #define OGGVORBIS_FRAME_SIZE 64
14 #define BUFFER_SIZE (1024*64)
16 typedef struct OggVorbisContext {
20 uint8_t buffer[BUFFER_SIZE];
29 static int oggvorbis_init_encoder(vorbis_info *vi, AVCodecContext *avccontext) {
31 #ifdef OGGVORBIS_VBR_BY_ESTIMATE
32 /* variable bitrate by estimate */
34 return (vorbis_encode_setup_managed(vi, avccontext->channels,
35 avccontext->sample_rate, -1, avccontext->bit_rate, -1) ||
36 vorbis_encode_ctl(vi, OV_ECTL_RATEMANAGE_AVG, NULL) ||
37 vorbis_encode_setup_init(vi)) ;
39 /* constant bitrate */
41 return vorbis_encode_init(vi, avccontext->channels,
42 avccontext->sample_rate, -1, avccontext->bit_rate, -1) ;
46 static int oggvorbis_encode_init(AVCodecContext *avccontext) {
47 OggVorbisContext *context = avccontext->priv_data ;
48 ogg_packet header, header_comm, header_code;
51 vorbis_info_init(&context->vi) ;
52 if(oggvorbis_init_encoder(&context->vi, avccontext) < 0) {
53 av_log(avccontext, AV_LOG_ERROR, "oggvorbis_encode_init: init_encoder failed") ;
56 vorbis_analysis_init(&context->vd, &context->vi) ;
57 vorbis_block_init(&context->vd, &context->vb) ;
59 vorbis_comment_init(&context->vc);
60 vorbis_comment_add_tag(&context->vc, "encoder", LIBAVCODEC_IDENT) ;
62 vorbis_analysis_headerout(&context->vd, &context->vc, &header,
63 &header_comm, &header_code);
65 avccontext->extradata_size= 3*2 + header.bytes + header_comm.bytes + header_code.bytes;
66 p= avccontext->extradata= av_mallocz(avccontext->extradata_size);
68 *(p++) = header.bytes>>8;
69 *(p++) = header.bytes&0xFF;
70 memcpy(p, header.packet, header.bytes);
73 *(p++) = header_comm.bytes>>8;
74 *(p++) = header_comm.bytes&0xFF;
75 memcpy(p, header_comm.packet, header_comm.bytes);
76 p += header_comm.bytes;
78 *(p++) = header_code.bytes>>8;
79 *(p++) = header_code.bytes&0xFF;
80 memcpy(p, header_code.packet, header_code.bytes);
82 /* vorbis_block_clear(&context->vb);
83 vorbis_dsp_clear(&context->vd);
84 vorbis_info_clear(&context->vi);*/
85 vorbis_comment_clear(&context->vc);
87 avccontext->frame_size = OGGVORBIS_FRAME_SIZE ;
89 avccontext->coded_frame= avcodec_alloc_frame();
90 avccontext->coded_frame->key_frame= 1;
96 static int oggvorbis_encode_frame(AVCodecContext *avccontext,
97 unsigned char *packets,
98 int buf_size, void *data)
100 OggVorbisContext *context = avccontext->priv_data ;
103 signed char *audio = data ;
104 int l, samples = OGGVORBIS_FRAME_SIZE ;
106 buffer = vorbis_analysis_buffer(&context->vd, samples) ;
108 if(context->vi.channels == 1) {
109 for(l = 0 ; l < samples ; l++)
110 buffer[0][l]=((audio[l*2+1]<<8)|(0x00ff&(int)audio[l*2]))/32768.f;
112 for(l = 0 ; l < samples ; l++){
113 buffer[0][l]=((audio[l*4+1]<<8)|(0x00ff&(int)audio[l*4]))/32768.f;
114 buffer[1][l]=((audio[l*4+3]<<8)|(0x00ff&(int)audio[l*4+2]))/32768.f;
118 vorbis_analysis_wrote(&context->vd, samples) ;
120 while(vorbis_analysis_blockout(&context->vd, &context->vb) == 1) {
121 vorbis_analysis(&context->vb, NULL);
122 vorbis_bitrate_addblock(&context->vb) ;
124 while(vorbis_bitrate_flushpacket(&context->vd, &op)) {
125 memcpy(context->buffer + context->buffer_index, &op, sizeof(ogg_packet));
126 context->buffer_index += sizeof(ogg_packet);
127 memcpy(context->buffer + context->buffer_index, op.packet, op.bytes);
128 context->buffer_index += op.bytes;
129 // av_log(avccontext, AV_LOG_DEBUG, "e%d / %d\n", context->buffer_index, op.bytes);
133 if(context->buffer_index){
134 ogg_packet *op2= (ogg_packet*)context->buffer;
135 op2->packet = context->buffer + sizeof(ogg_packet);
138 memcpy(packets, op2->packet, l);
139 context->buffer_index -= l + sizeof(ogg_packet);
140 memcpy(context->buffer, context->buffer + l + sizeof(ogg_packet), context->buffer_index);
142 // av_log(avccontext, AV_LOG_DEBUG, "E%d\n", l);
150 static int oggvorbis_encode_close(AVCodecContext *avccontext) {
151 OggVorbisContext *context = avccontext->priv_data ;
152 /* ogg_packet op ; */
154 vorbis_analysis_wrote(&context->vd, 0) ; /* notify vorbisenc this is EOF */
156 /* We need to write all the remaining packets into the stream
159 av_log(avccontext, AV_LOG_ERROR, "fixme: not all packets written on oggvorbis_encode_close()\n") ;
162 while(vorbis_bitrate_flushpacket(&context->vd, &op)) {
163 memcpy(packets + l, &op, sizeof(ogg_packet)) ;
164 memcpy(packets + l + sizeof(ogg_packet), op.packet, op.bytes) ;
165 l += sizeof(ogg_packet) + op.bytes ;
169 vorbis_block_clear(&context->vb);
170 vorbis_dsp_clear(&context->vd);
171 vorbis_info_clear(&context->vi);
173 av_freep(&avccontext->coded_frame);
174 av_freep(&avccontext->extradata);
180 AVCodec oggvorbis_encoder = {
184 sizeof(OggVorbisContext),
185 oggvorbis_encode_init,
186 oggvorbis_encode_frame,
187 oggvorbis_encode_close
191 static int oggvorbis_decode_init(AVCodecContext *avccontext) {
192 OggVorbisContext *context = avccontext->priv_data ;
194 vorbis_info_init(&context->vi) ;
195 vorbis_comment_init(&context->vc) ;
196 context->op.packetno= 0;
202 static inline int conv(int samples, float **pcm, char *buf, int channels) {
204 ogg_int16_t *ptr, *data = (ogg_int16_t*)buf ;
207 for(i = 0 ; i < channels ; i++){
211 for(j = 0 ; j < samples ; j++) {
213 val = mono[j] * 32767.f;
215 if(val > 32767) val = 32767 ;
216 if(val < -32768) val = -32768 ;
227 static int oggvorbis_decode_frame(AVCodecContext *avccontext,
228 void *data, int *data_size,
229 uint8_t *buf, int buf_size)
231 OggVorbisContext *context = avccontext->priv_data ;
233 ogg_packet *op= &context->op;
234 int samples, total_samples, total_bytes,i;
243 op->bytes = buf_size;
244 op->b_o_s = op->packetno == 0;
246 // av_log(avccontext, AV_LOG_DEBUG, "%d %d %d %lld %lld %d %d\n", op->bytes, op->b_o_s, op->e_o_s, op->granulepos, op->packetno, buf_size, context->vi.rate);
248 /* for(i=0; i<op->bytes; i++)
249 av_log(avccontext, AV_LOG_DEBUG, "%02X ", op->packet[i]);
250 av_log(avccontext, AV_LOG_DEBUG, "\n");*/
251 if(op->packetno < 3) {
252 if(vorbis_synthesis_headerin(&context->vi, &context->vc, op)<0){
253 av_log(avccontext, AV_LOG_ERROR, "%lld. vorbis header damaged\n", op->packetno+1);
256 avccontext->channels = context->vi.channels ;
257 avccontext->sample_rate = context->vi.rate ;
262 if(op->packetno == 3) {
263 // av_log(avccontext, AV_LOG_INFO, "vorbis_decode: %d channel, %ldHz, encoder `%s'\n",
264 // context->vi.channels, context->vi.rate, context->vc.vendor);
266 vorbis_synthesis_init(&context->vd, &context->vi) ;
267 vorbis_block_init(&context->vd, &context->vb);
270 if(vorbis_synthesis(&context->vb, op) == 0)
271 vorbis_synthesis_blockin(&context->vd, &context->vb) ;
276 while((samples = vorbis_synthesis_pcmout(&context->vd, &pcm)) > 0) {
277 conv(samples, pcm, (char*)data + total_bytes, context->vi.channels) ;
278 total_bytes += samples * 2 * context->vi.channels ;
279 total_samples += samples ;
280 vorbis_synthesis_read(&context->vd, samples) ;
284 *data_size = total_bytes ;
289 static int oggvorbis_decode_close(AVCodecContext *avccontext) {
290 OggVorbisContext *context = avccontext->priv_data ;
292 vorbis_info_clear(&context->vi) ;
293 vorbis_comment_clear(&context->vc) ;
299 AVCodec oggvorbis_decoder = {
303 sizeof(OggVorbisContext),
304 oggvorbis_decode_init,
306 oggvorbis_decode_close,
307 oggvorbis_decode_frame,