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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
LibLAMEWrapper::LibLAMEWrapper(Info *i)
{
info = i;
if( (gfp = lame_init()) == NULL) {
info->error("LAME initialization failed (due to malloc failure!)");
return;
}
lame_set_in_samplerate(gfp, INPUT_SAMPLE_RATE);
lame_set_out_samplerate(gfp, OUTPUT_SAMPLE_RATE);
lame_set_num_channels(gfp, CHANNELS);
lame_set_quality(gfp, MIaV::config->readInt("mp3_quality"));
lame_set_mode(gfp, STEREO);
lame_set_brate(gfp, MIaV::config->readInt("mp3_bitrate"));
lame_set_strict_ISO(gfp, 1);
lame_set_bWriteVbrTag(gfp, 0);
lame_set_copyright(gfp, 0);
lame_set_original(gfp, 1);
lame_set_error_protection(gfp, 0);
lame_set_padding_type(gfp, PAD_NO);
lame_set_extension(gfp, 0);
if (lame_init_params(gfp) < 0) {
info->error("LAME parameter initialization failed.");
return;
}
audio_buffer[0] = new int16_t[AUDIO_BUFFER_SIZE];
audio_buffer[1] = new int16_t[AUDIO_BUFFER_SIZE];
decoder = NULL;
calc_bitrate = 0;
frame_number = 0;
}
LibLAMEWrapper::~LibLAMEWrapper()
{
delete audio_buffer[0];
delete audio_buffer[1];
}
Frame *LibLAMEWrapper::close(Frame *oldframe)
{
Frame *frame;
unsigned int offset = 0;
frame = new Frame(NULL, (int)(1.25 * SAMPLES + 7200) * 2);
if(oldframe) {
offset = oldframe->size;
frame->number = oldframe->number;
memcpy(frame->data, oldframe->data, oldframe->size);
delete oldframe;
}
int flush;
flush = lame_encode_finish(gfp, frame->data + offset, 7200);
frame->size = offset + flush;
calc_bitrate += flush;
frame->bitrate = (unsigned int)((double)calc_bitrate / (double)(frame_number)) * 25;
return frame;
}
static unsigned int sin_cnt = 0;
Frame *LibLAMEWrapper::encode(Frame *dvframe)
{
if(dvframe->mute) {
double volume = 1000;
double frequency = 440;
for(int cnt = 0; cnt < SAMPLES; cnt++) {
sin_cnt++;
double sin_val = (((double)sin_cnt / (double)OUTPUT_SAMPLE_RATE) * (double)M_PI) * frequency;
audio_buffer[0][cnt] = audio_buffer[1][cnt] = (short int)(sin(sin_val) * volume);
}
} else {
if(!decoder) {
decoder = dv_decoder_new(FALSE, FALSE, FALSE);
decoder->quality = DV_QUALITY_BEST;
dv_parse_header(decoder, dvframe->data);
decoder->system = e_dv_system_625_50;
decoder->sampling = e_dv_sample_422;
decoder->std = e_dv_std_iec_61834;
decoder->num_dif_seqs = 12;
}
dv_decode_full_audio( decoder, dvframe->data, audio_buffer );
}
Frame* audio_frame = new Frame(NULL, (int)(1.25 * SAMPLES + 7200));
const short int *buffer_l = audio_buffer[0];
const short int *buffer_r = audio_buffer[1];
const int nsamples = SAMPLES;
unsigned char* mp3buf = audio_frame->data;
const int mp3buf_size = audio_frame->size;
int val;
val = lame_encode_buffer(gfp, buffer_l, buffer_r, nsamples, mp3buf, mp3buf_size);
if(val < 0) {
switch(val) {
case -1:
info->error("Lame encoding failed, mp3buf was too small.");
break;
case -2:
info->error("Lame encoding failed, due to malloc() problem.");
break;
case -3:
info->error("Lame encoding failed, lame_init_params() not called.");
break;
case -4:
info->error("Lame encoding failed, due to psycho acoustic problems.");
break;
default:
info->error("Lame encoding failed, due to unknown error.");
break;
}
}
int flush_sz = 0;
audio_frame->size = val + flush_sz;
calc_bitrate += audio_frame->size;
frame_number ++;
audio_frame->bitrate = (unsigned int)((double)calc_bitrate / (double)(frame_number)) * 25;
return audio_frame;
}
|