1 /*
2 * Copyright (c) 2002 Mark Hills <mark@pogo.org.uk>
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <vorbis/vorbisenc.h>
22
26
28 vorbis_info
vi;
/**< vorbis_info used during init */
29 vorbis_dsp_state
vd;
/**< DSP state used for analysis */
30 vorbis_block
vb;
/**< vorbis_block used for analysis */
31 vorbis_comment
vc;
/**< VorbisComment info */
34
38 int i, hsizes[3],
ret;
39 unsigned char *headers[3], *extradata = avccontext->
extradata;
40
44 }
45
46 vorbis_info_init(&context->
vi) ;
47 vorbis_comment_init(&context->
vc) ;
48
49 if(p[0] == 0 && p[1] == 30) {
50 for(i = 0; i < 3; i++){
51 hsizes[i] = bytestream_get_be16((
const uint8_t **)&p);
52 headers[i] = p;
53 p += hsizes[i];
54 }
55 } else if(*p == 2) {
57 p++;
58 for(i=0; i<2; i++) {
59 hsizes[i] = 0;
60 while((*p == 0xFF) && (offset < avccontext->extradata_size)) {
61 hsizes[i] += 0xFF;
62 offset++;
63 p++;
64 }
67 "vorbis header sizes damaged\n");
69 goto error;
70 }
71 hsizes[i] += *p;
72 offset++;
73 p++;
74 }
76 #if 0
78 "vorbis header sizes: %d, %d, %d, / extradata_len is %d \n",
80 #endif
81 headers[0] = extradata +
offset;
82 headers[1] = extradata + offset + hsizes[0];
83 headers[2] = extradata + offset + hsizes[0] + hsizes[1];
84 } else {
86 "vorbis initial header len is wrong: %d\n", *p);
88 goto error;
89 }
90
91 for(i=0; i<3; i++){
92 context->
op.b_o_s= i==0;
93 context->
op.bytes = hsizes[i];
94 context->
op.packet = headers[i];
95 if(vorbis_synthesis_headerin(&context->
vi, &context->
vc, &context->
op)<0){
98 goto error;
99 }
100 }
101
106
107 vorbis_synthesis_init(&context->
vd, &context->
vi);
108 vorbis_block_init(&context->
vd, &context->
vb);
109
110 return 0 ;
111
112 error:
113 vorbis_info_clear(&context->
vi);
114 vorbis_comment_clear(&context->
vc) ;
116 }
117
118
119 static inline int conv(
int samples,
float **pcm,
char *
buf,
int channels) {
120 int i, j;
121 ogg_int16_t *ptr, *
data = (ogg_int16_t*)buf ;
122 float *mono ;
123
124 for(i = 0 ; i < channels ; i++){
125 ptr = &data[i];
126 mono = pcm[i] ;
127
128 for(j = 0 ; j < samples ; j++) {
129 *ptr = av_clip_int16(mono[j] * 32767.f);
130 ptr += channels;
131 }
132 }
133
134 return 0 ;
135 }
136
138 int *got_frame_ptr,
AVPacket *avpkt)
139 {
142 float **pcm ;
144 int samples, total_samples, total_bytes;
146 int16_t *output;
147
149 //FIXME flush
150 return 0;
151 }
152
156 output = (int16_t *)frame->
data[0];
157
158
159 op->packet = avpkt->
data;
160 op->bytes = avpkt->
size;
161
162 // av_log(avccontext, AV_LOG_DEBUG, "%d %d %d %"PRId64" %"PRId64" %d %d\n", op->bytes, op->b_o_s, op->e_o_s, op->granulepos, op->packetno, buf_size, context->vi.rate);
163
164 /* for(i=0; i<op->bytes; i++)
165 av_log(avccontext, AV_LOG_DEBUG, "%02X ", op->packet[i]);
166 av_log(avccontext, AV_LOG_DEBUG, "\n");*/
167
168 if(vorbis_synthesis(&context->
vb, op) == 0)
169 vorbis_synthesis_blockin(&context->
vd, &context->
vb) ;
170
171 total_samples = 0 ;
172 total_bytes = 0 ;
173
174 while((samples = vorbis_synthesis_pcmout(&context->
vd, &pcm)) > 0) {
175 conv(samples, pcm, (
char*)output + total_bytes, context->
vi.channels) ;
176 total_bytes += samples * 2 * context->
vi.channels ;
177 total_samples += samples ;
178 vorbis_synthesis_read(&context->
vd, samples) ;
179 }
180
182 *got_frame_ptr = total_samples > 0;
184 }
185
186
189
190 vorbis_info_clear(&context->
vi) ;
191 vorbis_comment_clear(&context->
vc) ;
192
193 return 0 ;
194 }
195
196
207 };