Browse code

* Ogg/Vorbis patch by Mark Hills

Originally committed as revision 1268 to svn://svn.ffmpeg.org/ffmpeg/trunk

Mark Hills authored on 2002/11/22 16:27:13
Showing 5 changed files
... ...
@@ -15,12 +15,18 @@
15 15
 #include "avformat.h"
16 16
 #include "oggvorbis.h"
17 17
 
18
+#define DECODER_BUFFER_SIZE 4096
19
+
18 20
 
19 21
 typedef struct OggContext {
22
+    /* output */
20 23
     ogg_stream_state os ;
21
-    int header_written ;
24
+    int header_handled ;
22 25
     ogg_int64_t base_packet_no ;
23 26
     ogg_int64_t base_granule_pos ;
27
+
28
+    /* input */
29
+    ogg_sync_state oy ;
24 30
 } OggContext ;
25 31
 
26 32
 
... ...
@@ -34,8 +40,6 @@ static int ogg_write_header(AVFormatContext *avfcontext) {
34 34
     ogg_packet header, header_comm, header_code ; 
35 35
     int n ;
36 36
     
37
-    fprintf(stderr, "ogg_write_header\n") ;
38
-    
39 37
     if(!(context = malloc(sizeof(OggContext))))
40 38
 	return -1 ;
41 39
     avfcontext->priv_data = context ;
... ...
@@ -75,7 +79,7 @@ static int ogg_write_header(AVFormatContext *avfcontext) {
75 75
 
76 76
 	/* end of vorbis specific code */
77 77
 
78
-	context->header_written = 0 ;
78
+	context->header_handled = 0 ;
79 79
 	context->base_packet_no = 0 ;
80 80
     }
81 81
     
... ...
@@ -94,13 +98,13 @@ static int ogg_write_packet(AVFormatContext *avfcontext,
94 94
     
95 95
     /* flush header packets so audio starts on a new page */
96 96
 
97
-    if(!context->header_written) {
97
+    if(!context->header_handled) {
98 98
 	while(ogg_stream_flush(&context->os, &og)) {
99 99
 	    put_buffer(&avfcontext->pb, og.header, og.header_len) ;
100 100
 	    put_buffer(&avfcontext->pb, og.body, og.body_len) ;
101 101
 	    put_flush_packet(&avfcontext->pb);
102 102
 	}
103
-	context->header_written = 1 ;
103
+	context->header_handled = 1 ;
104 104
     }
105 105
 
106 106
     while(l < size) {
... ...
@@ -135,8 +139,6 @@ static int ogg_write_trailer(AVFormatContext *avfcontext) {
135 135
     OggContext *context = avfcontext->priv_data ;
136 136
     ogg_page og ;
137 137
 
138
-    fprintf(stderr, "ogg_write_trailer\n") ;
139
-
140 138
     while(ogg_stream_flush(&context->os, &og)) {
141 139
 	put_buffer(&avfcontext->pb, og.header, og.header_len) ;
142 140
 	put_buffer(&avfcontext->pb, og.body, og.body_len) ;
... ...
@@ -159,10 +161,109 @@ static AVOutputFormat ogg_oformat = {
159 159
     ogg_write_header,
160 160
     ogg_write_packet,
161 161
     ogg_write_trailer,
162
-};
162
+} ;
163
+
164
+
165
+static int next_packet(AVFormatContext *avfcontext, ogg_packet *op) {
166
+    OggContext *context = avfcontext->priv_data ;
167
+    ogg_page og ;
168
+    char *buf ;
169
+
170
+    while(ogg_stream_packetout(&context->os, op) != 1) {
171
+
172
+	/* while no pages are available, read in more data to the sync */
173
+	while(ogg_sync_pageout(&context->oy, &og) != 1) {
174
+	    buf = ogg_sync_buffer(&context->oy, DECODER_BUFFER_SIZE) ;
175
+	    if(get_buffer(&avfcontext->pb, buf, DECODER_BUFFER_SIZE) <= 0)
176
+		return 1 ;
177
+	    ogg_sync_wrote(&context->oy, DECODER_BUFFER_SIZE) ; 
178
+	}	
179
+	
180
+	/* got a page. Feed it into the stream and get the packet */
181
+	if(ogg_stream_pagein(&context->os, &og) != 0)
182
+	    return 1 ;
183
+    }
184
+
185
+    return 0 ;
186
+}
187
+
188
+
189
+static int ogg_read_header(AVFormatContext *avfcontext, AVFormatParameters *ap)
190
+{
191
+    OggContext *context ;
192
+    char *buf ;
193
+    ogg_page og ;
194
+    AVStream *ast ;
195
+    
196
+    if(!(context = malloc(sizeof(OggContext)))) {
197
+	perror("malloc") ;
198
+	return -1 ;
199
+    }
200
+    avfcontext->priv_data = context ;
201
+
202
+    ogg_sync_init(&context->oy) ;
203
+    buf = ogg_sync_buffer(&context->oy, DECODER_BUFFER_SIZE) ;
204
+
205
+    if(get_buffer(&avfcontext->pb, buf, DECODER_BUFFER_SIZE) <= 0)
206
+	return -EIO ;
207
+    
208
+    ogg_sync_wrote(&context->oy, DECODER_BUFFER_SIZE) ;   
209
+    ogg_sync_pageout(&context->oy, &og) ;
210
+    ogg_stream_init(&context->os, ogg_page_serialno(&og)) ;
211
+    ogg_stream_pagein(&context->os, &og) ;
212
+  
213
+    /* currently only one vorbis stream supported */
214
+
215
+    ast = av_new_stream(avfcontext, 0) ;
216
+    if(!ast)
217
+	return AVERROR_NOMEM ;
218
+
219
+    ast->codec.codec_type = CODEC_TYPE_AUDIO ;
220
+    ast->codec.codec_id = CODEC_ID_VORBIS ;
221
+    
222
+    return 0 ;
223
+}
224
+
225
+
226
+static int ogg_read_packet(AVFormatContext *avfcontext, AVPacket *pkt) {
227
+    ogg_packet op ;
228
+
229
+    if(next_packet(avfcontext, &op)) 
230
+	return -EIO ;
231
+    if(av_new_packet(pkt, sizeof(ogg_packet) + op.bytes) < 0)
232
+	return -EIO ;
233
+    pkt->stream_index = 0 ;
234
+    memcpy(pkt->data, &op, sizeof(ogg_packet)) ;
235
+    memcpy(pkt->data + sizeof(ogg_packet), op.packet, op.bytes) ;
236
+
237
+    return sizeof(ogg_packet) + op.bytes ;
238
+}
239
+
240
+
241
+static int ogg_read_close(AVFormatContext *avfcontext) {
242
+    OggContext *context = avfcontext->priv_data ;
243
+
244
+    ogg_stream_clear(&context->os) ;
245
+    ogg_sync_clear(&context->oy) ;
246
+
247
+    return 0 ;
248
+}
249
+
250
+
251
+static AVInputFormat ogg_iformat = {
252
+    "ogg",
253
+    "Ogg Vorbis",
254
+    sizeof(OggContext),
255
+    NULL,
256
+    ogg_read_header,
257
+    ogg_read_packet,
258
+    ogg_read_close,
259
+    .extensions = "ogg",
260
+} ;
163 261
 
164 262
 
165 263
 int ogg_init(void) {
166 264
     av_register_output_format(&ogg_oformat) ;
265
+    av_register_input_format(&ogg_iformat);
167 266
     return 0 ;
168 267
 }
... ...
@@ -41,6 +41,7 @@ void avcodec_register_all(void)
41 41
 #endif
42 42
 #ifdef CONFIG_VORBIS
43 43
     register_avcodec(&oggvorbis_encoder);
44
+    register_avcodec(&oggvorbis_decoder);
44 45
 #endif
45 46
     register_avcodec(&mpeg1video_encoder);
46 47
     register_avcodec(&h263_encoder);
... ...
@@ -865,6 +865,7 @@ extern AVCodec mp3_decoder;
865 865
 extern AVCodec mace3_decoder;
866 866
 extern AVCodec mace6_decoder;
867 867
 extern AVCodec huffyuv_decoder;
868
+extern AVCodec oggvorbis_decoder;
868 869
 
869 870
 /* pcm codecs */
870 871
 #define PCM_CODEC(id, name) \
... ...
@@ -17,22 +17,16 @@ typedef struct OggVorbisContext {
17 17
     vorbis_info vi ;
18 18
     vorbis_dsp_state vd ;
19 19
     vorbis_block vb ;
20
+
21
+    /* decoder */
22
+    vorbis_comment vc ;
20 23
 } OggVorbisContext ;
21 24
 
22 25
 
23 26
 int oggvorbis_init_encoder(vorbis_info *vi, AVCodecContext *avccontext) {
24
-    if(avccontext->quality) { /* VBR requested */
25
-
26
-	fprintf(stderr, "init_encode: channels=%d quality=%d\n", 
27
-		avccontext->channels, avccontext->quality) ;
28
-
27
+    if(avccontext->quality) /* VBR requested */
29 28
 	return vorbis_encode_init_vbr(vi, avccontext->channels,
30 29
 		  avccontext->sample_rate, (float)avccontext->quality / 1000) ;
31
-    }
32
-
33
-    fprintf(stderr, "init_encoder: channels=%d bitrate=%d tolerance=%d\n", 
34
-	    avccontext->channels, avccontext->bit_rate,
35
-	    avccontext->bit_rate_tolerance) ;
36 30
 
37 31
     return vorbis_encode_init(vi, avccontext->channels,
38 32
 	          avccontext->sample_rate, -1, avccontext->bit_rate, -1) ;
... ...
@@ -42,15 +36,11 @@ int oggvorbis_init_encoder(vorbis_info *vi, AVCodecContext *avccontext) {
42 42
 static int oggvorbis_encode_init(AVCodecContext *avccontext) {
43 43
     OggVorbisContext *context = avccontext->priv_data ;
44 44
 
45
-    fprintf(stderr, "oggvorbis_encode_init\n") ;
46
-
47 45
     vorbis_info_init(&context->vi) ;
48
-
49 46
     if(oggvorbis_init_encoder(&context->vi, avccontext) < 0) {
50 47
 	fprintf(stderr, "oggvorbis_encode_init: init_encoder failed") ;
51 48
 	return -1 ;
52 49
     }
53
-
54 50
     vorbis_analysis_init(&context->vd, &context->vi) ;
55 51
     vorbis_block_init(&context->vd, &context->vb) ;
56 52
 
... ...
@@ -60,14 +50,15 @@ static int oggvorbis_encode_init(AVCodecContext *avccontext) {
60 60
 }
61 61
 
62 62
 
63
-int oggvorbis_encode_frame(AVCodecContext *avccontext, unsigned char *packets,
63
+static int oggvorbis_encode_frame(AVCodecContext *avccontext,
64
+				  unsigned char *packets,
64 65
 			   int buf_size, void *data)
65 66
 {
66 67
     OggVorbisContext *context = avccontext->priv_data ;
67 68
     float **buffer ;
68 69
     ogg_packet op ;
69 70
     signed char *audio = data ;
70
-    int l, samples = buf_size / 16 ; /* samples = OGGVORBIS_FRAME_SIZE */ ;
71
+    int l, samples = OGGVORBIS_FRAME_SIZE ;
71 72
 
72 73
     buffer = vorbis_analysis_buffer(&context->vd, samples) ;
73 74
 
... ...
@@ -100,17 +91,17 @@ int oggvorbis_encode_frame(AVCodecContext *avccontext, unsigned char *packets,
100 100
 }
101 101
 
102 102
 
103
-int oggvorbis_encode_close(AVCodecContext *avccontext) {
103
+static int oggvorbis_encode_close(AVCodecContext *avccontext) {
104 104
     OggVorbisContext *context = avccontext->priv_data ;
105 105
 /*  ogg_packet op ; */
106 106
     
107
-    fprintf(stderr, "oggvorbis_encode_close\n") ;
108
-    
109 107
     vorbis_analysis_wrote(&context->vd, 0) ; /* notify vorbisenc this is EOF */
110 108
 
111 109
     /* We need to write all the remaining packets into the stream
112 110
      * on closing */
113 111
     
112
+    fprintf(stderr, "fixme: not all packets written on oggvorbis_encode_close()\n") ;
113
+
114 114
 /*
115 115
     while(vorbis_bitrate_flushpacket(&context->vd, &op)) {
116 116
 	memcpy(packets + l, &op, sizeof(ogg_packet)) ;
... ...
@@ -135,6 +126,106 @@ AVCodec oggvorbis_encoder = {
135 135
     oggvorbis_encode_init,
136 136
     oggvorbis_encode_frame,
137 137
     oggvorbis_encode_close
138
-};
138
+} ;
139
+
140
+
141
+static int oggvorbis_decode_init(AVCodecContext *avccontext) {
142
+    OggVorbisContext *context = avccontext->priv_data ;
143
+
144
+    vorbis_info_init(&context->vi) ;
145
+    vorbis_comment_init(&context->vc) ;
146
+
147
+    return 0 ;
148
+}
149
+
150
+
151
+static inline int conv(int samples, float **pcm, char *buf, int channels) {
152
+    int i, j, val ;
153
+    ogg_int16_t *ptr, *data = (ogg_int16_t*)buf ;
154
+    float *mono ;
155
+ 
156
+    for(i = 0 ; i < channels ; i++){
157
+	ptr = &data[i];
158
+	mono = pcm[i] ;
159
+	
160
+	for(j = 0 ; j < samples ; j++) {
161
+	    
162
+	    val = mono[j] * 32767.f;
163
+	    
164
+	    if(val > 32767) val = 32767 ;
165
+	    if(val < -32768) val = -32768 ;
166
+	   	    
167
+	    *ptr = val ;
168
+	    ptr += channels;
169
+	}
170
+    }
171
+    
172
+    return 0 ;
173
+}
174
+	   
175
+	
176
+static int oggvorbis_decode_frame(AVCodecContext *avccontext,
177
+                        void *data, int *data_size,
178
+                        UINT8 *buf, int buf_size)
179
+{
180
+    OggVorbisContext *context = avccontext->priv_data ;
181
+    ogg_packet *op = (ogg_packet*)buf ;
182
+    float **pcm ;
183
+    int samples, total_samples, total_bytes ;
184
+ 
185
+    op->packet = (char*)op + sizeof(ogg_packet) ; /* correct data pointer */
186
+
187
+    if(op->packetno < 3) {
188
+	vorbis_synthesis_headerin(&context->vi, &context->vc, op) ;
189
+	return buf_size ;
190
+    }
191
+
192
+    if(op->packetno == 3) {
193
+	fprintf(stderr, "vorbis_decode: %d channel, %ldHz, encoder `%s'\n",
194
+		context->vi.channels, context->vi.rate, context->vc.vendor);
139 195
 
196
+	avccontext->channels = context->vi.channels ;
197
+	avccontext->sample_rate = context->vi.rate ;
198
+	
199
+	vorbis_synthesis_init(&context->vd, &context->vi) ;
200
+	vorbis_block_init(&context->vd, &context->vb); 
201
+    }
202
+
203
+    if(vorbis_synthesis(&context->vb, op) == 0)
204
+	vorbis_synthesis_blockin(&context->vd, &context->vb) ;
205
+    
206
+    total_samples = 0 ;
207
+    total_bytes = 0 ;
208
+
209
+    while((samples = vorbis_synthesis_pcmout(&context->vd, &pcm)) > 0) {
210
+	conv(samples, pcm, (char*)data + total_bytes, context->vi.channels) ;
211
+	total_bytes += samples * 2 * context->vi.channels ;
212
+	total_samples += samples ;
213
+        vorbis_synthesis_read(&context->vd, samples) ;
214
+    }
215
+
216
+    *data_size = total_bytes ;   
217
+    return buf_size ;
218
+}
140 219
 
220
+
221
+static int oggvorbis_decode_close(AVCodecContext *avccontext) {
222
+    OggVorbisContext *context = avccontext->priv_data ;
223
+   
224
+    vorbis_info_clear(&context->vi) ;
225
+    vorbis_comment_clear(&context->vc) ;
226
+
227
+    return 0 ;
228
+}
229
+
230
+
231
+AVCodec oggvorbis_decoder = {
232
+    "vorbis",
233
+    CODEC_TYPE_AUDIO,
234
+    CODEC_ID_VORBIS,
235
+    sizeof(OggVorbisContext),
236
+    oggvorbis_decode_init,
237
+    NULL,
238
+    oggvorbis_decode_close,
239
+    oggvorbis_decode_frame,
240
+} ;
... ...
@@ -36,7 +36,7 @@ fi
36 36
 
37 37
 
38 38
 # various files
39
-ffmpeg="../ffmpeg"
39
+ffmpeg="../ffmpeg_g"
40 40
 outfile="$datadir/a-"
41 41
 reffile="$2"
42 42
 benchfile="$datadir/ffmpeg.bench"