fix obnoxious ogg_packet passing from encoder to muxer
[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"
10#include "oggvorbis.h"
11
6d8f985e
MN
12//#define OGGVORBIS_FRAME_SIZE 1024
13#define OGGVORBIS_FRAME_SIZE 64
81e0d0b4 14
6d8f985e 15#define BUFFER_SIZE (1024*64)
81e0d0b4
MH
16
17typedef struct OggVorbisContext {
18 vorbis_info vi ;
19 vorbis_dsp_state vd ;
20 vorbis_block vb ;
6d8f985e
MN
21 uint8_t buffer[BUFFER_SIZE];
22 int buffer_index;
3aca208a
MH
23
24 /* decoder */
25 vorbis_comment vc ;
d76f581f 26 ogg_packet op;
81e0d0b4
MH
27} OggVorbisContext ;
28
29
30int oggvorbis_init_encoder(vorbis_info *vi, AVCodecContext *avccontext) {
c55427f8
MH
31
32#ifdef OGGVORBIS_VBR_BY_ESTIMATE
33 /* variable bitrate by estimate */
34
35 return (vorbis_encode_setup_managed(vi, avccontext->channels,
36 avccontext->sample_rate, -1, avccontext->bit_rate, -1) ||
37 vorbis_encode_ctl(vi, OV_ECTL_RATEMANAGE_AVG, NULL) ||
38 vorbis_encode_setup_init(vi)) ;
39#else
40 /* constant bitrate */
81e0d0b4
MH
41
42 return vorbis_encode_init(vi, avccontext->channels,
43 avccontext->sample_rate, -1, avccontext->bit_rate, -1) ;
c55427f8 44#endif
81e0d0b4
MH
45}
46
47
48static int oggvorbis_encode_init(AVCodecContext *avccontext) {
49 OggVorbisContext *context = avccontext->priv_data ;
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
59 avccontext->frame_size = OGGVORBIS_FRAME_SIZE ;
492cd3a9
MN
60
61 avccontext->coded_frame= avcodec_alloc_frame();
62 avccontext->coded_frame->key_frame= 1;
81e0d0b4
MH
63
64 return 0 ;
65}
66
67
3aca208a
MH
68static int oggvorbis_encode_frame(AVCodecContext *avccontext,
69 unsigned char *packets,
81e0d0b4
MH
70 int buf_size, void *data)
71{
72 OggVorbisContext *context = avccontext->priv_data ;
73 float **buffer ;
74 ogg_packet op ;
75 signed char *audio = data ;
3aca208a 76 int l, samples = OGGVORBIS_FRAME_SIZE ;
81e0d0b4
MH
77
78 buffer = vorbis_analysis_buffer(&context->vd, samples) ;
79
80 if(context->vi.channels == 1) {
81 for(l = 0 ; l < samples ; l++)
82 buffer[0][l]=((audio[l*2+1]<<8)|(0x00ff&(int)audio[l*2]))/32768.f;
83 } else {
84 for(l = 0 ; l < samples ; l++){
85 buffer[0][l]=((audio[l*4+1]<<8)|(0x00ff&(int)audio[l*4]))/32768.f;
86 buffer[1][l]=((audio[l*4+3]<<8)|(0x00ff&(int)audio[l*4+2]))/32768.f;
87 }
88 }
89
90 vorbis_analysis_wrote(&context->vd, samples) ;
91
81e0d0b4
MH
92 while(vorbis_analysis_blockout(&context->vd, &context->vb) == 1) {
93 vorbis_analysis(&context->vb, NULL);
94 vorbis_bitrate_addblock(&context->vb) ;
95
96 while(vorbis_bitrate_flushpacket(&context->vd, &op)) {
6d8f985e
MN
97 memcpy(context->buffer + context->buffer_index, &op, sizeof(ogg_packet));
98 context->buffer_index += sizeof(ogg_packet);
99 memcpy(context->buffer + context->buffer_index, op.packet, op.bytes);
100 context->buffer_index += op.bytes;
101// av_log(avccontext, AV_LOG_DEBUG, "e%d / %d\n", context->buffer_index, op.bytes);
81e0d0b4
MH
102 }
103 }
104
6d8f985e
MN
105 if(context->buffer_index){
106 ogg_packet *op2= context->buffer;
107 op2->packet = context->buffer + sizeof(ogg_packet);
108 l= op2->bytes;
109
110 memcpy(packets, op2->packet, l);
111 context->buffer_index -= l + sizeof(ogg_packet);
112 memcpy(context->buffer, context->buffer + l + sizeof(ogg_packet), context->buffer_index);
113
114// av_log(avccontext, AV_LOG_DEBUG, "E%d\n", l);
115 return l;
116 }
117
118 return 0;
81e0d0b4
MH
119}
120
121
3aca208a 122static int oggvorbis_encode_close(AVCodecContext *avccontext) {
81e0d0b4
MH
123 OggVorbisContext *context = avccontext->priv_data ;
124/* ogg_packet op ; */
125
81e0d0b4
MH
126 vorbis_analysis_wrote(&context->vd, 0) ; /* notify vorbisenc this is EOF */
127
128 /* We need to write all the remaining packets into the stream
129 * on closing */
130
9b879566 131 av_log(avccontext, AV_LOG_ERROR, "fixme: not all packets written on oggvorbis_encode_close()\n") ;
3aca208a 132
81e0d0b4
MH
133/*
134 while(vorbis_bitrate_flushpacket(&context->vd, &op)) {
135 memcpy(packets + l, &op, sizeof(ogg_packet)) ;
136 memcpy(packets + l + sizeof(ogg_packet), op.packet, op.bytes) ;
137 l += sizeof(ogg_packet) + op.bytes ;
138 }
139*/
140
141 vorbis_block_clear(&context->vb);
142 vorbis_dsp_clear(&context->vd);
143 vorbis_info_clear(&context->vi);
492cd3a9
MN
144
145 av_freep(&avccontext->coded_frame);
81e0d0b4
MH
146
147 return 0 ;
148}
149
150
151AVCodec oggvorbis_encoder = {
152 "vorbis",
153 CODEC_TYPE_AUDIO,
154 CODEC_ID_VORBIS,
155 sizeof(OggVorbisContext),
156 oggvorbis_encode_init,
157 oggvorbis_encode_frame,
158 oggvorbis_encode_close
3aca208a
MH
159} ;
160
161
162static int oggvorbis_decode_init(AVCodecContext *avccontext) {
163 OggVorbisContext *context = avccontext->priv_data ;
164
165 vorbis_info_init(&context->vi) ;
166 vorbis_comment_init(&context->vc) ;
d76f581f 167 context->op.packetno= 0;
3aca208a
MH
168
169 return 0 ;
170}
171
172
173static inline int conv(int samples, float **pcm, char *buf, int channels) {
174 int i, j, val ;
175 ogg_int16_t *ptr, *data = (ogg_int16_t*)buf ;
176 float *mono ;
177
178 for(i = 0 ; i < channels ; i++){
179 ptr = &data[i];
180 mono = pcm[i] ;
181
182 for(j = 0 ; j < samples ; j++) {
183
184 val = mono[j] * 32767.f;
185
186 if(val > 32767) val = 32767 ;
187 if(val < -32768) val = -32768 ;
188
189 *ptr = val ;
190 ptr += channels;
191 }
192 }
193
194 return 0 ;
195}
196
197
198static int oggvorbis_decode_frame(AVCodecContext *avccontext,
199 void *data, int *data_size,
0c1a9eda 200 uint8_t *buf, int buf_size)
3aca208a
MH
201{
202 OggVorbisContext *context = avccontext->priv_data ;
3aca208a 203 float **pcm ;
d76f581f 204 ogg_packet *op= &context->op;
8bfed902 205 int samples, total_samples, total_bytes,i;
3aca208a 206
8bfed902
MN
207 if(!buf_size){
208 //FIXME flush
209 *data_size=0;
210 return 0;
211 }
212
d76f581f
MN
213 op->packet = buf;
214 op->bytes = buf_size;
215 op->b_o_s = op->packetno == 0;
3aca208a 216
8bfed902
MN
217// 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);
218
219/* for(i=0; i<op->bytes; i++)
220 av_log(avccontext, AV_LOG_DEBUG, "%02X ", op->packet[i]);
221 av_log(avccontext, AV_LOG_DEBUG, "\n");*/
3aca208a 222 if(op->packetno < 3) {
8bfed902
MN
223 if(vorbis_synthesis_headerin(&context->vi, &context->vc, op)<0){
224 av_log(avccontext, AV_LOG_ERROR, "%lld. vorbis header damaged\n", op->packetno+1);
225 return -1;
226 }
227 avccontext->channels = context->vi.channels ;
228 avccontext->sample_rate = context->vi.rate ;
d76f581f 229 op->packetno++;
3aca208a
MH
230 return buf_size ;
231 }
232
233 if(op->packetno == 3) {
8bfed902
MN
234// av_log(avccontext, AV_LOG_INFO, "vorbis_decode: %d channel, %ldHz, encoder `%s'\n",
235// context->vi.channels, context->vi.rate, context->vc.vendor);
81e0d0b4 236
3aca208a
MH
237 vorbis_synthesis_init(&context->vd, &context->vi) ;
238 vorbis_block_init(&context->vd, &context->vb);
239 }
240
241 if(vorbis_synthesis(&context->vb, op) == 0)
242 vorbis_synthesis_blockin(&context->vd, &context->vb) ;
243
244 total_samples = 0 ;
245 total_bytes = 0 ;
246
247 while((samples = vorbis_synthesis_pcmout(&context->vd, &pcm)) > 0) {
248 conv(samples, pcm, (char*)data + total_bytes, context->vi.channels) ;
249 total_bytes += samples * 2 * context->vi.channels ;
250 total_samples += samples ;
251 vorbis_synthesis_read(&context->vd, samples) ;
252 }
253
d76f581f 254 op->packetno++;
3aca208a
MH
255 *data_size = total_bytes ;
256 return buf_size ;
257}
81e0d0b4 258
3aca208a
MH
259
260static int oggvorbis_decode_close(AVCodecContext *avccontext) {
261 OggVorbisContext *context = avccontext->priv_data ;
262
263 vorbis_info_clear(&context->vi) ;
264 vorbis_comment_clear(&context->vc) ;
265
266 return 0 ;
267}
268
269
270AVCodec oggvorbis_decoder = {
271 "vorbis",
272 CODEC_TYPE_AUDIO,
273 CODEC_ID_VORBIS,
274 sizeof(OggVorbisContext),
275 oggvorbis_decode_init,
276 NULL,
277 oggvorbis_decode_close,
278 oggvorbis_decode_frame,
279} ;