1
/***************************************************************************
2
copyright : (C) 2006 by mean
3
email : fixounet@free.fr
4
***************************************************************************/
6
/***************************************************************************
8
* This program is free software; you can redistribute it and/or modify *
9
* it under the terms of the GNU General Public License as published by *
10
* the Free Software Foundation; either version 2 of the License, or *
11
* (at your option) any later version. *
13
***************************************************************************/
21
#include <ADM_assert.h>
22
#include "ADM_library/default.h"
24
#include "audioprocess.hxx"
25
#include "audioeng_process.h"
26
#include "audioencoder.h"
28
#include "ADM_audiofilter/audioencoder_vorbis_param.h"
29
#include "ADM_audiofilter/audioencoder_vorbis.h"
32
#include "vorbis/vorbisenc.h"
33
#include "ADM_toolkit/ADM_debugID.h"
34
#define MODULE_NAME MODULE_AUDIO_FILTER
35
#include "ADM_toolkit/ADM_debug.h"
38
#define OPTIONS (twolame_options_struct *)_twolameOptions
40
#define VD (((vorbisStruct *)_handle)->vd)
41
#define VI (((vorbisStruct *)_handle)->vi)
42
#define VB (((vorbisStruct *)_handle)->vb)
43
#define VC (((vorbisStruct *)_handle)->vc)
44
typedef struct vorbisStruct
53
AUDMEncoder_Vorbis::AUDMEncoder_Vorbis(AUDMAudioFilter * instream) :AUDMEncoder (instream)
55
printf("[Vorbis] Creating Vorbis\n");
57
_wavheader->encoding=WAV_OGG;
59
_handle=(void *)new vorbisStruct;
60
ch_order[0] = CH_FRONT_LEFT;
61
ch_order[1] = CH_FRONT_RIGHT;
62
ch_order[2] = CH_REAR_LEFT;
63
ch_order[3] = CH_REAR_RIGHT;
64
ch_order[4] = CH_FRONT_CENTER;
69
AUDMEncoder_Vorbis::~AUDMEncoder_Vorbis()
71
printf("[Vorbis] Deleting Vorbis\n");
74
vorbis_block_clear(&VB);
75
vorbis_dsp_clear(&VD);
76
vorbis_info_clear(&VI);
77
delete (vorbisStruct *)_handle;
84
//________________________________________________
86
// frequence : Impose frequency , 0 means reuse the incoming fq
87
// mode : ADM_STEREO etc...
88
// bitrate : Bitrate in kbps (96,192...)
89
// return 0 : init failed
91
//_______________________________________________
92
uint8_t AUDMEncoder_Vorbis::init(ADM_audioEncoderDescriptor *config)
95
VORBIS_encoderParam *vorbisConf=(VORBIS_encoderParam *)config->param;
96
ADM_assert(config->paramSize==sizeof(VORBIS_encoderParam));
98
ogg_packet header1,header2,header3;
103
vorbis_info_init(&VI) ;
105
switch(vorbisConf->mode)
108
err=vorbis_encode_init(&VI,
109
_wavheader->channels,
110
_wavheader->frequency,
112
config->bitrate*1000, //long nominal_bitrate,
113
-1 //long min_bitrate))
116
case ADM_VORBIS_VBR :
117
err=vorbis_encode_init_vbr(&VI,
118
_wavheader->channels,
119
_wavheader->frequency,
120
vorbisConf->quality/10
128
printf("[vorbis] init error %d\n",err);
131
vorbis_analysis_init(&VD, &VI) ;
132
vorbis_block_init(&VD, &VB);
133
vorbis_comment_init(&VC);
134
vorbis_comment_add_tag(&VC, "encoder", "AVIDEMUX2") ;
136
vorbis_analysis_headerout(&VD, &VC, &header1,
140
// Store all headers as extra data
141
// see ogg vorbis decode for details
144
_extraSize=header1.bytes+header2.bytes+header3.bytes+3*sizeof(uint32_t);
145
_extraData=new uint8_t[_extraSize];
147
uint32_t *ex=(uint32_t *)_extraData;
149
d=_extraData+sizeof(uint32_t)*3;
153
memcpy(d,header1.packet,ex[0]);
155
memcpy(d,header2.packet,ex[1]);
157
memcpy(d,header3.packet,ex[2]);
158
vorbis_comment_clear(&VC);
160
printf("\n[Vorbis]Vorbis encoder initialized\n");
161
switch(vorbisConf->mode)
164
printf("[Vorbis]CBR Bitrate:%lu\n",config->bitrate);
166
case ADM_VORBIS_VBR: //FIXME FIXME FIXME
167
printf("[Vorbis]VBR Quality:%.1f\n",vorbisConf->quality);
173
printf("[Vorbis]Channels :%lu\n",_wavheader->channels);
174
printf("[Vorbis]Frequency :%lu\n",_wavheader->frequency);
178
#define ROUNDMAX 3000
180
uint8_t AUDMEncoder_Vorbis::getPacket(uint8_t *dest, uint32_t *len, uint32_t *samples)
184
float **float_samples;
188
_chunk=1024*_wavheader->channels;
190
// Check that we have packet from previous pass
193
if(!refillBuffer(_chunk ))
198
if(tmptail-tmphead<_chunk)
203
//printf("Round %d\n",ROUNDMAX-count);
204
if(vorbis_analysis_blockout(&VD, &VB) == 1)
206
vorbis_analysis(&VB, NULL);
207
vorbis_bitrate_addblock(&VB) ;
208
//printf("Blockout\n");
210
if(vorbis_bitrate_flushpacket(&VD, &op))
212
if(op.bytes<2) continue; // avoid the 1byte sized packet
214
memcpy(dest, op.packet,op.bytes);
216
*samples=op.granulepos-_oldpos;
217
_oldpos=op.granulepos;
218
// aprintf("1st packet :sampl:%lu len :%lu sample:%lu abs:%llu\n",*samples,op.bytes,total,op.granulepos);
224
uint32_t nbSample=(tmptail-tmphead)/_wavheader->channels;
225
if(nbSample>1024) nbSample=1024;
226
float_samples=vorbis_analysis_buffer(&VD, nbSample) ;
228
// Put our samples in incoming buffer
229
for (int i = 0; i < nbSample; i++)
230
for (int j = 0; j < _wavheader->channels; j++) {
231
float_samples[j][i] = tmpbuffer[index++];
232
if (float_samples[j][i] > 1) float_samples[j][i] = 1;
233
if (float_samples[j][i] < -1) float_samples[j][i] = -1;
235
reorderChannels(&(tmpbuffer[tmphead]),tmptail-tmphead);
236
// Buffer full, go go go
237
vorbis_analysis_wrote(&VD, nbSample) ;
238
tmphead+=nbSample*_wavheader->channels;