2
* Musepack audio compression
3
* Copyright (c) 2005-2009, The Musepack Development Team
4
* Copyright (C) 1999-2004 Buschmann/Klemm/Piecha/Wolf
6
* This library 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.
11
* This library 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.
16
* You should have received a copy of the GNU Lesser General Public
17
* License along with this library; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24
#include "libmpcenc.h"
25
#include <mpc/minimax.h>
28
void Init_Skalenfaktoren ( void );
31
extern Huffman_t const HuffBands [33];
32
extern Huffman_t const HuffRes [2][17];
33
extern Huffman_t const HuffSCFI_1 [4]; // contains tables for SV7-scalefactor select
34
extern Huffman_t const HuffSCFI_2 [16]; // contains tables for SV7-scalefactor select
35
extern Huffman_t const HuffDSCF_1 [64]; // contains tables for SV7-scalefactor coding
36
extern Huffman_t const HuffDSCF_2 [65]; // contains tables for SV7-scalefactor coding
37
extern Huffman_t const * const HuffQ [2][8]; // points to tables for SV7-sample coding
38
extern Huffman_t const HuffQ9up [256];
41
* SV1: DATE 13.12.1998
42
* SV2: DATE 12.06.1999
43
* SV3: DATE 19.10.1999
44
* SV4: DATE 20.10.1999
45
* SV5: DATE 18.06.2000
46
* SV6: DATE 10.08.2000
47
* SV7: DATE 23.08.2000
48
* SV7.f: DATE 20.07.2002
53
mpc_encoder_init ( mpc_encoder_t * e,
54
mpc_uint64_t SamplesInWAVE,
55
unsigned int FramesBlockPwr,
56
unsigned int SeekDistance )
58
Init_Skalenfaktoren ();
61
memset(e, 0, sizeof(*e));
63
if (SeekDistance > 15)
65
if (FramesBlockPwr > 14)
68
e->seek_pwr = SeekDistance;
69
e->frames_per_block_pwr = FramesBlockPwr;
71
if (SamplesInWAVE == 0)
72
e->seek_table = malloc((1 << 16) * sizeof(mpc_uint32_t));
74
e->seek_table = malloc((size_t)(2 + SamplesInWAVE / (MPC_FRAME_LENGTH << (e->seek_pwr + e->frames_per_block_pwr))) * sizeof(mpc_uint32_t));
76
e->buffer = malloc(MAX_FRAME_SIZE * (1 << e->frames_per_block_pwr) * sizeof(mpc_uint8_t));
80
mpc_encoder_exit ( mpc_encoder_t * e )
86
// writes replay gain info
87
void writeGainInfo ( mpc_encoder_t * e,
88
unsigned short t_gain,
89
unsigned short t_peak,
90
unsigned short a_gain,
91
unsigned short a_peak)
93
writeBits ( e, 1, 8 ); // version
94
writeBits ( e, t_gain, 16 ); // Title gain
95
writeBits ( e, t_peak, 16 ); // Title peak
96
writeBits ( e, a_gain, 16 ); // Album gain
97
writeBits ( e, a_peak, 16 ); // Album peak
102
writeStreamInfo ( mpc_encoder_t*e,
103
const unsigned int MaxBand,
104
const unsigned int MS_on,
105
const unsigned int SamplesCount,
106
const unsigned int SamplesSkip,
107
const unsigned int SampleFreq,
108
const unsigned int ChannelCount)
110
unsigned char tmp[10];
113
writeBits ( e, 8, 8 ); // StreamVersion
115
len = encodeSize(SamplesCount, (char *)tmp, MPC_FALSE);
116
for( i = 0; i < len; i++) // nb of samples
117
writeBits ( e, tmp[i], 8 );
118
len = encodeSize(SamplesSkip, (char *)tmp, MPC_FALSE);
119
for( i = 0; i < len; i++) // nb of samples to skip at beginning
120
writeBits ( e, tmp[i], 8 );
122
switch ( SampleFreq ) {
123
case 44100: writeBits ( e, 0, 3 ); break;
124
case 48000: writeBits ( e, 1, 3 ); break;
125
case 37800: writeBits ( e, 2, 3 ); break;
126
case 32000: writeBits ( e, 3, 3 ); break;
127
default : fprintf(stderr, "Internal error\n");// FIXME : stderr_printf ( "Internal error\n");
131
writeBits ( e, MaxBand - 1 , 5 ); // Bandwidth
132
writeBits ( e, ChannelCount - 1 , 4 ); // Channels
133
writeBits ( e, MS_on , 1 ); // MS-Coding Flag
134
writeBits ( e, e->frames_per_block_pwr >> 1, 3 ); // frames per block (log4 unit)
137
// writes encoder signature
138
void writeEncoderInfo ( mpc_encoder_t * e,
141
const int version_major,
142
const int version_minor,
143
const int version_build )
145
writeBits ( e, (mpc_uint32_t)(profile * 8 + .5), 7 );
146
writeBits ( e, PNS_on, 1 );
147
writeBits ( e, version_major, 8 );
148
writeBits ( e, version_minor, 8 );
149
writeBits ( e, version_build, 8 );
152
// formatting and writing SV8-bitstream for one frame
154
writeBitstream_SV8 ( mpc_encoder_t* e, int MaxBand)
157
const Huffman_t * Table, * Tables[2];
158
mpc_int32_t * Res_L = e->Res_L;
159
mpc_int32_t * Res_R = e->Res_R;
160
mpc_bool_t * DSCF_Flag_L = e->DSCF_Flag_L;
161
mpc_bool_t * DSCF_Flag_R = e->DSCF_Flag_R;
162
mpc_int32_t * SCF_Last_L = e->SCF_Last_L;
163
mpc_int32_t * SCF_Last_R = e->SCF_Last_R;
165
for( n = MaxBand; n >= 0; n--)
166
if (Res_L[n] != 0 || Res_R[n] != 0) break;
169
if (e->framesInBlock == 0) {
170
encodeLog(e, n, MaxBand + 1);
171
MaxBand = e->MaxBand = n;
174
MaxBand = e->MaxBand = n + e->MaxBand;
176
writeBits(e, HuffBands[n].Code, HuffBands[n].Length);
179
/************************************ Resolution *********************************/
183
int tmp = Res_L[MaxBand - 1];
184
if (tmp < 0) tmp += 17;
185
writeBits(e, HuffRes[0][tmp].Code, HuffRes[0][tmp].Length);
186
tmp = Res_R[MaxBand - 1];
187
if (tmp < 0) tmp += 17;
188
writeBits(e, HuffRes[0][tmp].Code, HuffRes[0][tmp].Length);
190
for ( n = MaxBand - 2; n >= 0; n--) {
191
int tmp = Res_L[n] - Res_L[n + 1];
192
if (tmp < 0) tmp += 17;
193
writeBits(e, HuffRes[Res_L[n + 1] > 2][tmp].Code, HuffRes[Res_L[n + 1] > 2][tmp].Length);
195
tmp = Res_R[n] - Res_R[n + 1];
196
if (tmp < 0) tmp += 17;
197
writeBits(e, HuffRes[Res_R[n + 1] > 2][tmp].Code, HuffRes[Res_R[n + 1] > 2][tmp].Length);
200
if (e->MS_Channelmode > 0) {
201
mpc_uint32_t tmp = 0;
202
int cnt = 0, tot = 0;
203
mpc_bool_t * MS_Flag = e->MS_Flag;
204
for( n = 0; n < MaxBand; n++) {
205
if ( Res_L[n] != 0 || Res_R[n] != 0 ) {
206
tmp = (tmp << 1) | MS_Flag[n];
211
encodeLog(e, cnt, tot);
212
if (cnt * 2 > tot) tmp = ~tmp;
213
encodeEnum(e, tmp, tot);
217
/************************************ SCF encoding type ***********************************/
219
if (e->framesInBlock == 0){
220
for( n = 0; n < 32; n++)
221
DSCF_Flag_L[n] = DSCF_Flag_R[n] = 1; // new block -> force key frame
224
Tables[0] = HuffSCFI_1;
225
Tables[1] = HuffSCFI_2;
226
for ( n = 0; n < MaxBand; n++ ) {
227
int tmp = 0, cnt = -1;
229
tmp = (e->SCF_Index_L[n][1] == e->SCF_Index_L[n][0]) * 2 + (e->SCF_Index_L[n][2] == e->SCF_Index_L[n][1]);
233
tmp = (tmp << 2) | ((e->SCF_Index_R[n][1] == e->SCF_Index_R[n][0]) * 2 + (e->SCF_Index_R[n][2] == e->SCF_Index_R[n][1]));
237
writeBits(e, Tables[cnt][tmp].Code, Tables[cnt][tmp].Length);
240
/************************************* SCF **********************************/
242
for ( n = 0; n < MaxBand; n++ ) {
245
mpc_int32_t * SCFI_L_n = e->SCF_Index_L[n];
246
if (DSCF_Flag_L[n] == 1) {
247
writeBits(e, SCFI_L_n[0] + 6, 7);
250
unsigned int tmp = (SCFI_L_n[0] - SCF_Last_L[n] + 31) & 127;
252
writeBits(e, HuffDSCF_2[tmp].Code, HuffDSCF_2[tmp].Length);
254
writeBits(e, HuffDSCF_2[64].Code, HuffDSCF_2[64].Length);
255
writeBits(e, tmp - 64, 6);
258
for( m = 0; m < 2; m++){
259
if (SCFI_L_n[m+1] != SCFI_L_n[m]) {
260
unsigned int tmp = (SCFI_L_n[m+1] - SCFI_L_n[m] + 31) & 127;
262
writeBits(e, HuffDSCF_1[tmp].Code, HuffDSCF_1[tmp].Length);
264
writeBits(e, HuffDSCF_1[31].Code, HuffDSCF_1[31].Length);
265
writeBits(e, tmp - 64, 6);
269
SCF_Last_L[n] = SCFI_L_n[2];
273
mpc_int32_t * SCFI_R_n = e->SCF_Index_R[n];
274
if (DSCF_Flag_R[n] == 1) {
275
writeBits(e, SCFI_R_n[0] + 6, 7);
278
unsigned int tmp = (SCFI_R_n[0] - SCF_Last_R[n] + 31) & 127;
280
writeBits(e, HuffDSCF_2[tmp].Code, HuffDSCF_2[tmp].Length);
282
writeBits(e, HuffDSCF_2[64].Code, HuffDSCF_2[64].Length);
283
writeBits(e, tmp - 64, 6);
286
for( m = 0; m < 2; m++){
287
if (SCFI_R_n[m+1] != SCFI_R_n[m]) {
288
unsigned int tmp = (SCFI_R_n[m+1] - SCFI_R_n[m] + 31) & 127;
290
writeBits(e, HuffDSCF_1[tmp].Code, HuffDSCF_1[tmp].Length);
292
writeBits(e, HuffDSCF_1[31].Code, HuffDSCF_1[31].Length);
293
writeBits(e, tmp - 64, 6);
297
SCF_Last_R[n] = SCFI_R_n[2];
301
/*********************************** Samples *********************************/
302
for ( n = 0; n < MaxBand; n++ ) {
304
const mpc_int16_t * q = e->Q[n].L;
305
static const unsigned int thres[] = {0, 0, 3, 7, 9, 1, 3, 4, 8};
306
static const int HuffQ2_var[5*5*5] =
307
{6, 5, 4, 5, 6, 5, 4, 3, 4, 5, 4, 3, 2, 3, 4, 5, 4, 3, 4, 5, 6, 5, 4, 5, 6, 5, 4, 3, 4, 5, 4, 3, 2, 3, 4, 3, 2, 1, 2, 3, 4, 3, 2, 3, 4, 5, 4, 3, 4, 5, 4, 3, 2, 3, 4, 3, 2, 1, 2, 3, 2, 1, 0, 1, 2, 3, 2, 1, 2, 3, 4, 3, 2, 3, 4, 5, 4, 3, 4, 5, 4, 3, 2, 3, 4, 3, 2, 1, 2, 3, 4, 3, 2, 3, 4, 5, 4, 3, 4, 5, 6, 5, 4, 5, 6, 5, 4, 3, 4, 5, 4, 3, 2, 3, 4, 5, 4, 3, 4, 5, 6, 5, 4, 5, 6};
310
int k = 0, idx = 1, cnt = 0, sng;
316
Table = HuffQ [0][0];
320
for ( ; k < kmax; k++) {
325
sng = (sng << 1) | (q[k] >> 1);
328
writeBits(e, Table[cnt].Code, Table[cnt].Length);
330
if (cnt > 9) idx = ~idx;
331
encodeEnum(e, idx, 18);
332
writeBits(e, sng, cnt);
337
Tables[0] = HuffQ [0][1];
338
Tables[1] = HuffQ [1][1];
339
idx = 2 * thres[Res];
340
for ( ; k < 36; k += 3) {
341
int tmp = q[k] + 5*q[k+1] + 25*q[k+2];
342
writeBits ( e, Tables[idx > thres[Res]][tmp].Code,
343
Tables[idx > thres[Res]][tmp].Length );
344
idx = (idx >> 1) + HuffQ2_var[tmp];
349
Table = HuffQ [0][Res - 1];
350
for ( ; k < 36; k += 2 ) {
351
int tmp = q[k] + thres[Res]*q[k+1];
352
writeBits ( e, Table[tmp].Code, Table[tmp].Length );
359
Tables[0] = HuffQ [0][Res - 1];
360
Tables[1] = HuffQ [1][Res - 1];
361
idx = 2 * thres[Res];
362
for ( ; k < 36; k++ ) {
363
int tmp = q[k] - (1 << (Res - 2)) + 1;
364
writeBits ( e, Tables[idx > thres[Res]][q[k]].Code,
365
Tables[idx > thres[Res]][q[k]].Length );
366
if (tmp < 0) tmp = -tmp;
367
idx = (idx >> 1) + tmp;
371
for ( ; k < 36; k++ ) {
372
writeBits ( e, HuffQ9up[q[k] >> (Res - 9)].Code,
373
HuffQ9up[q[k] >> (Res - 9)].Length );
375
writeBits ( e, q[k] & ((1 << (Res - 9)) - 1), Res - 9);
381
} while (q == e->Q[n].L && (q = e->Q[n].R));
385
if (e->framesInBlock == (1 << e->frames_per_block_pwr)) {
386
if ((e->block_cnt & ((1 << e->seek_pwr) - 1)) == 0) {
387
e->seek_table[e->seek_pos] = ftell(e->outputFile);
391
writeBlock(e, "AP", MPC_FALSE, 0);
404
void _Huffman_MakeTree( huff_sym_t *sym, unsigned int num_symbols);
405
void _Huffman_PrintCodes(huff_sym_t * sym, unsigned int num_symbols, int print_type, int offset);
407
void print_histo(void)
410
huff_sym_t sym[HISTO_NB][HISTO_LEN];
411
unsigned int dist[HISTO_NB];
412
unsigned int size[HISTO_NB];
413
unsigned int cnt[HISTO_NB];
414
unsigned int total_cnt, total_size, full_count = 0, full_size = 0;
415
double optim_size, full_optim = 0;
419
memset(dist, 1, sizeof dist);
420
memset(sym, 0, sizeof(huff_sym_t) * HISTO_LEN * HISTO_NB);
422
for(j = 0 ; j < HISTO_NB ; j++) {
423
for(i = 0 ; i < HISTO_LEN; i++) {
424
sym[j][i].Symbol = i;
425
sym[j][i].Count = histo[j][i];
426
if (sym[j][i].Count == 0)
429
_Huffman_MakeTree(sym[j], HISTO_LEN);
430
_Huffman_PrintCodes(sym[j], HISTO_LEN, 3, 0);
431
_Huffman_PrintCodes(sym[j], HISTO_LEN, 0, 0);
432
_Huffman_PrintCodes(sym[j], HISTO_LEN, 1, 0);
436
for( i = 0; i < HISTO_LEN; i++) {
437
total_cnt += sym[j][i].Count;
438
total_size += sym[j][i].Count * sym[j][i].Bits;
439
if (sym[j][i].Count != 0)
440
optim_size += sym[j][i].Count * __builtin_log2(sym[j][i].Count);
442
full_count += total_cnt;
443
full_size += total_size;
444
optim_size = total_cnt * __builtin_log2(total_cnt) - optim_size;
445
full_optim += optim_size;
446
size[j] = total_size;
448
printf("%u count : %u huff : %f bps ", j, total_cnt, (float)total_size / total_cnt);
449
printf("opt : %f bps ", (float)optim_size / total_cnt);
450
printf("loss : %f bps (%f %%)\n", (float)(total_size - optim_size) / total_cnt, (float)(total_size - optim_size) * 100 / optim_size);
451
for( i = 0; i < HISTO_LEN; i++){
452
printf("%u ", sym[j][i].Bits);
457
printf("cnt : %u size %f optim %f\n", full_count, (float)full_size / full_count, (float)full_optim / full_count);
458
printf("loss : %f bps (%f %%)\n", (float)(full_size - full_optim) / full_count, (float)(full_size - full_optim) * 100 / full_optim);
465
Dump ( const unsigned int* q, const int Res )
469
for ( k = 0; k < 36; k++, q++ )
470
printf ("%2d%c", *q-1, k==35?'\n':' ');
473
for ( k = 0; k < 36; k++, q++ )
474
printf ("%2d%c", *q-2, k==35?'\n':' ');
476
case 3: case 4: case 5: case 6: case 7:
478
for ( k = 0; k < 36; k++, q++ )
479
printf ("%2d%c", *q-7, k==35?'\n':' ');
481
case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17:
482
printf ("%2u: ", Res-1 );
483
for ( k = 0; k < 36; k++, q++ ) {
484
printf ("%6d", *q - (1 << (Res-2)) );
492
/* end of encode_sv7.c */