]> git.sesse.net Git - ffmpeg/blob - libavcodec/oggvorbis.c
38145cc1b17e2cc2f51a1a26b8f5a6f8c8ed3531
[ffmpeg] / libavcodec / oggvorbis.c
1 /**
2  * @file oggvorbis.c
3  * Ogg Vorbis codec support via libvorbisenc.
4  * @author Mark Hills <mark@pogo.org.uk>
5  */
6
7 #include <vorbis/vorbisenc.h>
8
9 #include "avcodec.h"
10
11 //#define OGGVORBIS_FRAME_SIZE 1024
12 #define OGGVORBIS_FRAME_SIZE 64
13
14 #define BUFFER_SIZE (1024*64)
15
16 typedef struct OggVorbisContext {
17     vorbis_info vi ;
18     vorbis_dsp_state vd ;
19     vorbis_block vb ;
20     uint8_t buffer[BUFFER_SIZE];
21     int buffer_index;
22
23     /* decoder */
24     vorbis_comment vc ;
25     ogg_packet op;
26 } OggVorbisContext ;
27
28
29 static int oggvorbis_init_encoder(vorbis_info *vi, AVCodecContext *avccontext) {
30
31 #ifdef OGGVORBIS_VBR_BY_ESTIMATE
32     /* variable bitrate by estimate */
33
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)) ;
38 #else
39     /* constant bitrate */
40
41     return vorbis_encode_init(vi, avccontext->channels,
42                   avccontext->sample_rate, -1, avccontext->bit_rate, -1) ;
43 #endif
44 }
45
46 static int oggvorbis_encode_init(AVCodecContext *avccontext) {
47     OggVorbisContext *context = avccontext->priv_data ;
48     ogg_packet header, header_comm, header_code;
49     uint8_t *p;
50
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") ;
54         return -1 ;
55     }
56     vorbis_analysis_init(&context->vd, &context->vi) ;
57     vorbis_block_init(&context->vd, &context->vb) ;
58
59     vorbis_comment_init(&context->vc);
60     vorbis_comment_add_tag(&context->vc, "encoder", LIBAVCODEC_IDENT) ;
61
62     vorbis_analysis_headerout(&context->vd, &context->vc, &header,
63                                 &header_comm, &header_code);
64     
65     avccontext->extradata_size= 3*2 + header.bytes + header_comm.bytes +  header_code.bytes;
66     p= avccontext->extradata= av_mallocz(avccontext->extradata_size);
67     
68     *(p++) = header.bytes>>8;
69     *(p++) = header.bytes&0xFF;
70     memcpy(p, header.packet, header.bytes);
71     p += header.bytes;
72     
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;
77     
78     *(p++) = header_code.bytes>>8;
79     *(p++) = header_code.bytes&0xFF;
80     memcpy(p, header_code.packet, header_code.bytes);
81                                 
82 /*    vorbis_block_clear(&context->vb);
83     vorbis_dsp_clear(&context->vd);
84     vorbis_info_clear(&context->vi);*/
85     vorbis_comment_clear(&context->vc);
86        
87     avccontext->frame_size = OGGVORBIS_FRAME_SIZE ;
88  
89     avccontext->coded_frame= avcodec_alloc_frame();
90     avccontext->coded_frame->key_frame= 1;
91     
92     return 0 ;
93 }
94
95
96 static int oggvorbis_encode_frame(AVCodecContext *avccontext,
97                                   unsigned char *packets,
98                            int buf_size, void *data)
99 {
100     OggVorbisContext *context = avccontext->priv_data ;
101     float **buffer ;
102     ogg_packet op ;
103     signed char *audio = data ;
104     int l, samples = OGGVORBIS_FRAME_SIZE ;
105
106     buffer = vorbis_analysis_buffer(&context->vd, samples) ;
107
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;
111     } else {
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;
115         }
116     }
117     
118     vorbis_analysis_wrote(&context->vd, samples) ; 
119
120     while(vorbis_analysis_blockout(&context->vd, &context->vb) == 1) {
121         vorbis_analysis(&context->vb, NULL);
122         vorbis_bitrate_addblock(&context->vb) ;
123
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);
130         }
131     }
132
133     if(context->buffer_index){
134         ogg_packet *op2= (ogg_packet*)context->buffer;
135         op2->packet = context->buffer + sizeof(ogg_packet);
136         l=  op2->bytes;
137         
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);
141         
142 //        av_log(avccontext, AV_LOG_DEBUG, "E%d\n", l);
143         return l;
144     }
145
146     return 0;
147 }
148
149
150 static int oggvorbis_encode_close(AVCodecContext *avccontext) {
151     OggVorbisContext *context = avccontext->priv_data ;
152 /*  ogg_packet op ; */
153     
154     vorbis_analysis_wrote(&context->vd, 0) ; /* notify vorbisenc this is EOF */
155
156     /* We need to write all the remaining packets into the stream
157      * on closing */
158     
159     av_log(avccontext, AV_LOG_ERROR, "fixme: not all packets written on oggvorbis_encode_close()\n") ;
160
161 /*
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 ;    
166     }
167 */
168
169     vorbis_block_clear(&context->vb);
170     vorbis_dsp_clear(&context->vd);
171     vorbis_info_clear(&context->vi);
172
173     av_freep(&avccontext->coded_frame);
174     av_freep(&avccontext->extradata);
175   
176     return 0 ;
177 }
178
179
180 AVCodec oggvorbis_encoder = {
181     "vorbis",
182     CODEC_TYPE_AUDIO,
183     CODEC_ID_VORBIS,
184     sizeof(OggVorbisContext),
185     oggvorbis_encode_init,
186     oggvorbis_encode_frame,
187     oggvorbis_encode_close
188 } ;
189
190
191 static int oggvorbis_decode_init(AVCodecContext *avccontext) {
192     OggVorbisContext *context = avccontext->priv_data ;
193
194     vorbis_info_init(&context->vi) ;
195     vorbis_comment_init(&context->vc) ;
196     context->op.packetno= 0;
197
198     return 0 ;
199 }
200
201
202 static inline int conv(int samples, float **pcm, char *buf, int channels) {
203     int i, j, val ;
204     ogg_int16_t *ptr, *data = (ogg_int16_t*)buf ;
205     float *mono ;
206  
207     for(i = 0 ; i < channels ; i++){
208         ptr = &data[i];
209         mono = pcm[i] ;
210         
211         for(j = 0 ; j < samples ; j++) {
212             
213             val = mono[j] * 32767.f;
214             
215             if(val > 32767) val = 32767 ;
216             if(val < -32768) val = -32768 ;
217                     
218             *ptr = val ;
219             ptr += channels;
220         }
221     }
222     
223     return 0 ;
224 }
225            
226         
227 static int oggvorbis_decode_frame(AVCodecContext *avccontext,
228                         void *data, int *data_size,
229                         uint8_t *buf, int buf_size)
230 {
231     OggVorbisContext *context = avccontext->priv_data ;
232     float **pcm ;
233     ogg_packet *op= &context->op;    
234     int samples, total_samples, total_bytes,i;
235  
236     if(!buf_size){
237     //FIXME flush
238         *data_size=0;
239         return 0;
240     }
241     
242     op->packet = buf;
243     op->bytes  = buf_size;
244     op->b_o_s  = op->packetno == 0;
245
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);
247     
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);
254             return -1;
255         }
256         avccontext->channels = context->vi.channels ;
257         avccontext->sample_rate = context->vi.rate ;
258         op->packetno++;
259         return buf_size ;
260     }
261
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);
265
266         vorbis_synthesis_init(&context->vd, &context->vi) ;
267         vorbis_block_init(&context->vd, &context->vb); 
268     }
269
270     if(vorbis_synthesis(&context->vb, op) == 0)
271         vorbis_synthesis_blockin(&context->vd, &context->vb) ;
272     
273     total_samples = 0 ;
274     total_bytes = 0 ;
275
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) ;
281     }
282
283     op->packetno++;
284     *data_size = total_bytes ;   
285     return buf_size ;
286 }
287
288
289 static int oggvorbis_decode_close(AVCodecContext *avccontext) {
290     OggVorbisContext *context = avccontext->priv_data ;
291    
292     vorbis_info_clear(&context->vi) ;
293     vorbis_comment_clear(&context->vc) ;
294
295     return 0 ;
296 }
297
298
299 AVCodec oggvorbis_decoder = {
300     "vorbis",
301     CODEC_TYPE_AUDIO,
302     CODEC_ID_VORBIS,
303     sizeof(OggVorbisContext),
304     oggvorbis_decode_init,
305     NULL,
306     oggvorbis_decode_close,
307     oggvorbis_decode_frame,
308 } ;