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
36
40 int i, hsizes[3], ret;
41 unsigned char *headers[3], *extradata = avccontext->
extradata;
42
46 }
47
48 vorbis_info_init(&context->
vi) ;
49 vorbis_comment_init(&context->
vc) ;
50
51 if(p[0] == 0 && p[1] == 30) {
52 for(i = 0; i < 3; i++){
53 hsizes[i] = bytestream_get_be16((
const uint8_t **)&p);
54 headers[i] = p;
55 p += hsizes[i];
56 }
57 } else if(*p == 2) {
59 p++;
60 for(i=0; i<2; i++) {
61 hsizes[i] = 0;
62 while((*p == 0xFF) && (offset < avccontext->extradata_size)) {
63 hsizes[i] += 0xFF;
64 offset++;
65 p++;
66 }
69 "vorbis header sizes damaged\n");
71 goto error;
72 }
73 hsizes[i] += *p;
74 offset++;
75 p++;
76 }
78 #if 0
80 "vorbis header sizes: %d, %d, %d, / extradata_len is %d \n",
82 #endif
83 headers[0] = extradata +
offset;
84 headers[1] = extradata + offset + hsizes[0];
85 headers[2] = extradata + offset + hsizes[0] + hsizes[1];
86 } else {
88 "vorbis initial header len is wrong: %d\n", *p);
90 goto error;
91 }
92
93 for(i=0; i<3; i++){
94 context->
op.b_o_s= i==0;
95 context->
op.bytes = hsizes[i];
96 context->
op.packet = headers[i];
97 if(vorbis_synthesis_headerin(&context->
vi, &context->
vc, &context->
op)<0){
100 goto error;
101 }
102 }
103
108
109 vorbis_synthesis_init(&context->
vd, &context->
vi);
110 vorbis_block_init(&context->
vd, &context->
vb);
111
112 return 0 ;
113
114 error:
116 return ret;
117 }
118
119
120 static inline int conv(
int samples,
float **pcm,
char *
buf,
int channels) {
121 int i, j;
122 ogg_int16_t *ptr, *
data = (ogg_int16_t*)buf ;
123 float *mono ;
124
125 for(i = 0 ; i < channels ; i++){
126 ptr = &data[i];
127 mono = pcm[i] ;
128
129 for(j = 0 ; j < samples ; j++) {
130 *ptr = av_clip_int16(mono[j] * 32767.f);
131 ptr += channels;
132 }
133 }
134
135 return 0 ;
136 }
137
139 int *got_frame_ptr,
AVPacket *avpkt)
140 {
143 float **pcm ;
145 int samples, total_samples, total_bytes;
146 int ret;
147 int16_t *output;
148
150 //FIXME flush
151 return 0;
152 }
153
156 return ret;
157 output = (int16_t *)frame->
data[0];
158
159
160 op->packet = avpkt->
data;
161 op->bytes = avpkt->
size;
162
163 // 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);
164
165 /* for(i=0; i<op->bytes; i++)
166 av_log(avccontext, AV_LOG_DEBUG, "%02X ", op->packet[i]);
167 av_log(avccontext, AV_LOG_DEBUG, "\n");*/
168
169 if(vorbis_synthesis(&context->
vb, op) == 0)
170 vorbis_synthesis_blockin(&context->
vd, &context->
vb) ;
171
172 total_samples = 0 ;
173 total_bytes = 0 ;
174
175 while((samples = vorbis_synthesis_pcmout(&context->
vd, &pcm)) > 0) {
176 conv(samples, pcm, (
char*)output + total_bytes, context->
vi.channels) ;
177 total_bytes += samples * 2 * context->
vi.channels ;
178 total_samples += samples ;
179 vorbis_synthesis_read(&context->
vd, samples) ;
180 }
181
183 *got_frame_ptr = total_samples > 0;
185 }
186
187
190
191 vorbis_block_clear(&context->
vb);
192 vorbis_dsp_clear(&context->
vd);
193 vorbis_info_clear(&context->
vi) ;
194 vorbis_comment_clear(&context->
vc) ;
195
196 return 0 ;
197 }
198
199
210 };
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
This structure describes decoded (raw) audio or video data.
ptrdiff_t const GLvoid * data
static int conv(int samples, float **pcm, char *buf, int channels)
static av_cold int init(AVCodecContext *avctx)
vorbis_dsp_state vd
DSP state used for analysis.
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
vorbis_block vb
vorbis_block used for analysis
enum AVSampleFormat sample_fmt
audio sample format
static int oggvorbis_decode_init(AVCodecContext *avccontext)
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
vorbis_comment vc
VorbisComment info.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
const char * name
Name of the codec implementation.
static const uint8_t offset[127][2]
vorbis_info vi
vorbis_info used during init
Libavcodec external API header.
static int oggvorbis_decode_frame(AVCodecContext *avccontext, void *data, int *got_frame_ptr, AVPacket *avpkt)
int sample_rate
samples per second
static int ogg_packet(AVFormatContext *s, int *sid, int *dstart, int *dsize, int64_t *fpos)
find the next Ogg packet
main external API structure.
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
static int oggvorbis_decode_close(AVCodecContext *avccontext)
rational number numerator/denominator
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avpkt)
static int op(uint8_t **dst, const uint8_t *dst_end, GetByteContext *gb, int pixel, int count, int *x, int width, int linesize)
Perform decode operation.
common internal api header.
AVCodec ff_libvorbis_decoder
int channels
number of audio channels
This structure stores compressed data.
int nb_samples
number of audio samples (per channel) described by this frame