remove function call from muxer->encoder and cleanly pass global headers
[libav.git] / libavcodec / oggvorbis.c
CommitLineData
983e3246
MN
1/**
2 * @file oggvorbis.c
3 * Ogg Vorbis codec support via libvorbisenc.
4 * @author Mark Hills <mark@pogo.org.uk>
81e0d0b4
MH
5 */
6
81e0d0b4
MH
7#include <vorbis/vorbisenc.h>
8
9#include "avcodec.h"
81e0d0b4 10
6d8f985e
MN
11//#define OGGVORBIS_FRAME_SIZE 1024
12#define OGGVORBIS_FRAME_SIZE 64
81e0d0b4 13
6d8f985e 14#define BUFFER_SIZE (1024*64)
81e0d0b4
MH
15
16typedef struct OggVorbisContext {
17 vorbis_info vi ;
18 vorbis_dsp_state vd ;
19 vorbis_block vb ;
6d8f985e
MN
20 uint8_t buffer[BUFFER_SIZE];
21 int buffer_index;
3aca208a
MH
22
23 /* decoder */
24 vorbis_comment vc ;
d76f581f 25 ogg_packet op;
81e0d0b4
MH
26} OggVorbisContext ;
27
28
bbb77e7c 29static int oggvorbis_init_encoder(vorbis_info *vi, AVCodecContext *avccontext) {
c55427f8
MH
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 */
81e0d0b4
MH
40
41 return vorbis_encode_init(vi, avccontext->channels,
42 avccontext->sample_rate, -1, avccontext->bit_rate, -1) ;
c55427f8 43#endif
81e0d0b4
MH
44}
45
81e0d0b4
MH
46static int oggvorbis_encode_init(AVCodecContext *avccontext) {
47 OggVorbisContext *context = avccontext->priv_data ;
bbb77e7c
MN
48 ogg_packet header, header_comm, header_code;
49 uint8_t *p;
81e0d0b4 50
81e0d0b4 51 vorbis_info_init(&context->vi) ;
81e0d0b4 52 if(oggvorbis_init_encoder(&context->vi, avccontext) < 0) {
9b879566 53 av_log(avccontext, AV_LOG_ERROR, "oggvorbis_encode_init: init_encoder failed") ;
81e0d0b4
MH
54 return -1 ;
55 }
81e0d0b4
MH
56 vorbis_analysis_init(&context->vd, &context->vi) ;
57 vorbis_block_init(&context->vd, &context->vb) ;
58
bbb77e7c
MN
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
81e0d0b4 87 avccontext->frame_size = OGGVORBIS_FRAME_SIZE ;
492cd3a9
MN
88
89 avccontext->coded_frame= avcodec_alloc_frame();
90 avccontext->coded_frame->key_frame= 1;
81e0d0b4
MH
91
92 return 0 ;
93}
94
95
3aca208a
MH
96static int oggvorbis_encode_frame(AVCodecContext *avccontext,
97 unsigned char *packets,
81e0d0b4
MH
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 ;
3aca208a 104 int l, samples = OGGVORBIS_FRAME_SIZE ;
81e0d0b4
MH
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
81e0d0b4
MH
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)) {
6d8f985e
MN
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);
81e0d0b4
MH
130 }
131 }
132
6d8f985e 133 if(context->buffer_index){
bbb77e7c 134 ogg_packet *op2= (ogg_packet*)context->buffer;
6d8f985e
MN
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;
81e0d0b4
MH
147}
148
149
3aca208a 150static int oggvorbis_encode_close(AVCodecContext *avccontext) {
81e0d0b4
MH
151 OggVorbisContext *context = avccontext->priv_data ;
152/* ogg_packet op ; */
153
81e0d0b4
MH
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
9b879566 159 av_log(avccontext, AV_LOG_ERROR, "fixme: not all packets written on oggvorbis_encode_close()\n") ;
3aca208a 160
81e0d0b4
MH
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);
492cd3a9
MN
172
173 av_freep(&avccontext->coded_frame);
bbb77e7c 174 av_freep(&avccontext->extradata);
81e0d0b4
MH
175
176 return 0 ;
177}
178
179
180AVCodec 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
3aca208a
MH
188} ;
189
190
191static 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) ;
d76f581f 196 context->op.packetno= 0;
3aca208a
MH
197
198 return 0 ;
199}
200
201
202static 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
227static int oggvorbis_decode_frame(AVCodecContext *avccontext,
228 void *data, int *data_size,
0c1a9eda 229 uint8_t *buf, int buf_size)
3aca208a
MH
230{
231 OggVorbisContext *context = avccontext->priv_data ;
3aca208a 232 float **pcm ;
d76f581f 233 ogg_packet *op= &context->op;
8bfed902 234 int samples, total_samples, total_bytes,i;
3aca208a 235
8bfed902
MN
236 if(!buf_size){
237 //FIXME flush
238 *data_size=0;
239 return 0;
240 }
241
d76f581f
MN
242 op->packet = buf;
243 op->bytes = buf_size;
244 op->b_o_s = op->packetno == 0;
3aca208a 245
8bfed902
MN
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");*/
3aca208a 251 if(op->packetno < 3) {
8bfed902
MN
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 ;
d76f581f 258 op->packetno++;
3aca208a
MH
259 return buf_size ;
260 }
261
262 if(op->packetno == 3) {
8bfed902
MN
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);
81e0d0b4 265
3aca208a
MH
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
d76f581f 283 op->packetno++;
3aca208a
MH
284 *data_size = total_bytes ;
285 return buf_size ;
286}
81e0d0b4 287
3aca208a
MH
288
289static 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
299AVCodec 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} ;