1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
/* $Id: VorbisEnc.c,v 1.9 2004/05/02 16:25:00 titer Exp $
This file is part of the HandBrake source code.
Homepage: <http://handbrake.m0k.org/>.
It may be used under the terms of the GNU General Public License. */
#include "HBInternal.h"
/* libvorbis */
#include <vorbis/vorbisenc.h>
#define OGGVORBIS_FRAME_SIZE 1024
struct HBWork
{
HB_WORK_COMMON_MEMBERS
HBHandle * handle;
HBAudio * audio;
int inited;
vorbis_info vi;
vorbis_comment vc;
vorbis_dsp_state vd;
vorbis_block vb;
float position;
int32_t * inputBuffer;
};
/* Local prototypes */
static int VorbisEncWork( HBWork * );
HBWork * HBVorbisEncInit ( HBHandle * handle, HBAudio * audio )
{
HBWork * w = calloc( sizeof( HBWork ), 1 );
w->name = strdup( "VorbisEnc" );
w->work = VorbisEncWork;
w->handle = handle;
w->audio = audio;
w->inputBuffer = malloc( 2 * OGGVORBIS_FRAME_SIZE * sizeof( int32_t ) );
return w;
}
void HBVorbisEncClose( HBWork ** _w )
{
HBWork * w = *_w;
if( w->inited )
{
vorbis_block_clear( &w->vb );
vorbis_dsp_clear( &w->vd );
vorbis_comment_clear( &w->vc );
vorbis_info_clear( &w->vi );
}
free( w->name );
free( w );
*_w = NULL;
}
static HBBuffer *PacketToBuffer( ogg_packet *op )
{
HBBuffer *buf = HBBufferInit( sizeof( ogg_packet ) + op->bytes );
memcpy( buf->data, op, sizeof( ogg_packet ) );
memcpy( buf->data + sizeof( ogg_packet ), op->packet, op->bytes );
return buf;
}
static int VorbisEncWork( HBWork * w )
{
HBAudio * audio = w->audio;
float **buffer;
int i;
float inputBuffer[OGGVORBIS_FRAME_SIZE * 2];
HBBuffer * vorbisBuffer;
if( HBFifoIsHalfFull( audio->outFifo ) )
{
return 0;
}
if( !w->inited )
{
ogg_packet header[3];
if( !HBFifoSize( audio->resampleFifo ) )
{
return 0;
}
w->inited = 1;
/* init */
vorbis_info_init( &w->vi );
if( vorbis_encode_setup_managed( &w->vi, 2,
audio->outSampleRate, -1, 1000 * audio->outBitrate, -1 ) ||
vorbis_encode_ctl( &w->vi, OV_ECTL_RATEMANAGE_AVG, NULL ) ||
vorbis_encode_setup_init( &w->vi ) )
{
HBLog( "HBVorbisEnc: vorbis_encode_setup_managed failed" );
return 0;
}
/* add a comment */
vorbis_comment_init( &w->vc );
vorbis_comment_add_tag( &w->vc, "ENCODER", "HandBrake");
/* set up the analysis state and auxiliary encoding storage */
vorbis_analysis_init( &w->vd, &w->vi);
vorbis_block_init( &w->vd, &w->vb);
/* get the 3 headers */
vorbis_analysis_headerout( &w->vd, &w->vc,
&header[0], &header[1], &header[2] );
for( i = 0; i < 3; i++ )
{
vorbisBuffer = PacketToBuffer( &header[i] );
if( !HBFifoPush( audio->outFifo, &vorbisBuffer ) )
{
HBLog( "HBVorbisEnc: HBFifoPush failed" );
}
}
}
/* Try to extract more data */
if( vorbis_analysis_blockout( &w->vd, &w->vb ) == 1 )
{
ogg_packet op;
vorbis_analysis( &w->vb, NULL );
vorbis_bitrate_addblock( &w->vb );
if( vorbis_bitrate_flushpacket( &w->vd, &op ) )
{
vorbisBuffer = PacketToBuffer( &op );
vorbisBuffer->position = w->position;
if( !HBFifoPush( audio->outFifo, &vorbisBuffer ) )
{
HBLog( "HBVorbisEnc: HBFifoPush failed" );
}
return 1;
}
}
if( !HBFifoGetBytes( audio->resampleFifo, (uint8_t*) inputBuffer,
OGGVORBIS_FRAME_SIZE * 2 * sizeof( float ),
&w->position ) )
{
return 0;
}
buffer = vorbis_analysis_buffer( &w->vd, OGGVORBIS_FRAME_SIZE );
for( i = 0; i < OGGVORBIS_FRAME_SIZE; i++ )
{
buffer[0][i] = inputBuffer[2*i] / 32768.f;
buffer[1][i] = inputBuffer[2*i+1] / 32768.f;
}
vorbis_analysis_wrote( &w->vd, OGGVORBIS_FRAME_SIZE );
return 1;
}
|