1 /*
2 * MP3 bitstream Output interface for LAME
3 *
4 * Copyright (c) 1999-2000 Mark Taylor
5 * Copyright (c) 1999-2002 Takehiro Tominaga
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
21 *
22 * $Id$
23 */
24
25
26 #ifdef HAVE_CONFIG_H
27 #include <config.h>
28 #endif
29
30 #include <stdlib.h>
31 #include <stdio.h>
32
33 #include "lame.h"
34 #include "machine.h"
35 #include "encoder.h"
36 #include "util.h"
37 #include "tables.h"
38 #include "quantize_pvt.h"
39 #include "lame_global_flags.h"
40 #include "gain_analysis.h"
41 #include "VbrTag.h"
42 #include "bitstream.h"
43 #include "tables.h"
44
45
46
47 /* unsigned int is at least this large: */
48 /* we work with ints, so when doing bit manipulation, we limit
49 * ourselves to MAX_LENGTH-2 just to be on the safe side */
50 #define MAX_LENGTH 32
51
52
53
54 #ifdef DEBUG
55 static int hogege;
56 #endif
57
58
59
60 static int
calcFrameLength(SessionConfig_t const * const cfg,int kbps,int pad)61 calcFrameLength(SessionConfig_t const *const cfg, int kbps, int pad)
62 {
63 return 8 * ((cfg->version + 1) * 72000 * kbps / cfg->samplerate_out + pad);
64 }
65
66
67 /***********************************************************************
68 * compute bitsperframe and mean_bits for a layer III frame
69 **********************************************************************/
70 int
getframebits(const lame_internal_flags * gfc)71 getframebits(const lame_internal_flags * gfc)
72 {
73 SessionConfig_t const *const cfg = &gfc->cfg;
74 EncResult_t const *const eov = &gfc->ov_enc;
75 int bit_rate;
76
77 /* get bitrate in kbps [?] */
78 if (eov->bitrate_index)
79 bit_rate = bitrate_table[cfg->version][eov->bitrate_index];
80 else
81 bit_rate = cfg->avg_bitrate;
82 /*assert(bit_rate <= 550); */
83 assert(8 <= bit_rate && bit_rate <= 640);
84
85 /* main encoding routine toggles padding on and off */
86 /* one Layer3 Slot consists of 8 bits */
87 return calcFrameLength(cfg, bit_rate, eov->padding);
88 }
89
90 int
get_max_frame_buffer_size_by_constraint(SessionConfig_t const * cfg,int constraint)91 get_max_frame_buffer_size_by_constraint(SessionConfig_t const * cfg, int constraint)
92 {
93 int maxmp3buf = 0;
94 if (cfg->avg_bitrate > 320) {
95 /* in freeformat the buffer is constant */
96 if (constraint == MDB_STRICT_ISO) {
97 maxmp3buf = calcFrameLength(cfg, cfg->avg_bitrate, 0);
98 }
99 else {
100 /* maximum allowed bits per granule are 7680 */
101 maxmp3buf = 7680 * (cfg->version + 1);
102 }
103 }
104 else {
105 int max_kbps;
106 if (cfg->samplerate_out < 16000) {
107 max_kbps = bitrate_table[cfg->version][8]; /* default: allow 64 kbps (MPEG-2.5) */
108 }
109 else {
110 max_kbps = bitrate_table[cfg->version][14];
111 }
112 switch (constraint)
113 {
114 default:
115 case MDB_DEFAULT:
116 /* Bouvigne suggests this more lax interpretation of the ISO doc instead of using 8*960. */
117 /* All mp3 decoders should have enough buffer to handle this value: size of a 320kbps 32kHz frame */
118 maxmp3buf = 8 * 1440;
119 break;
120 case MDB_STRICT_ISO:
121 maxmp3buf = calcFrameLength(cfg, max_kbps, 0);
122 break;
123 case MDB_MAXIMUM:
124 maxmp3buf = 7680 * (cfg->version + 1);
125 break;
126 }
127 }
128 return maxmp3buf;
129 }
130
131
132 static void
putheader_bits(lame_internal_flags * gfc)133 putheader_bits(lame_internal_flags * gfc)
134 {
135 SessionConfig_t const *const cfg = &gfc->cfg;
136 EncStateVar_t *const esv = &gfc->sv_enc;
137 Bit_stream_struc *bs = &gfc->bs;
138 #ifdef DEBUG
139 hogege += cfg->sideinfo_len * 8;
140 #endif
141 memcpy(&bs->buf[bs->buf_byte_idx], esv->header[esv->w_ptr].buf, cfg->sideinfo_len);
142 bs->buf_byte_idx += cfg->sideinfo_len;
143 bs->totbit += cfg->sideinfo_len * 8;
144 esv->w_ptr = (esv->w_ptr + 1) & (MAX_HEADER_BUF - 1);
145 }
146
147
148
149
150 /*write j bits into the bit stream */
151 inline static void
putbits2(lame_internal_flags * gfc,int val,int j)152 putbits2(lame_internal_flags * gfc, int val, int j)
153 {
154 EncStateVar_t const *const esv = &gfc->sv_enc;
155 Bit_stream_struc *bs;
156 bs = &gfc->bs;
157
158 assert(j < MAX_LENGTH - 2);
159
160 while (j > 0) {
161 int k;
162 if (bs->buf_bit_idx == 0) {
163 bs->buf_bit_idx = 8;
164 bs->buf_byte_idx++;
165 assert(bs->buf_byte_idx < BUFFER_SIZE);
166 assert(esv->header[esv->w_ptr].write_timing >= bs->totbit);
167 if (esv->header[esv->w_ptr].write_timing == bs->totbit) {
168 putheader_bits(gfc);
169 }
170 bs->buf[bs->buf_byte_idx] = 0;
171 }
172
173 k = Min(j, bs->buf_bit_idx);
174 j -= k;
175
176 bs->buf_bit_idx -= k;
177
178 assert(j < MAX_LENGTH); /* 32 too large on 32 bit machines */
179 assert(bs->buf_bit_idx < MAX_LENGTH);
180
181 bs->buf[bs->buf_byte_idx] |= ((val >> j) << bs->buf_bit_idx);
182 bs->totbit += k;
183 }
184 }
185
186 /*write j bits into the bit stream, ignoring frame headers */
187 inline static void
putbits_noheaders(lame_internal_flags * gfc,int val,int j)188 putbits_noheaders(lame_internal_flags * gfc, int val, int j)
189 {
190 Bit_stream_struc *bs;
191 bs = &gfc->bs;
192
193 assert(j < MAX_LENGTH - 2);
194
195 while (j > 0) {
196 int k;
197 if (bs->buf_bit_idx == 0) {
198 bs->buf_bit_idx = 8;
199 bs->buf_byte_idx++;
200 assert(bs->buf_byte_idx < BUFFER_SIZE);
201 bs->buf[bs->buf_byte_idx] = 0;
202 }
203
204 k = Min(j, bs->buf_bit_idx);
205 j -= k;
206
207 bs->buf_bit_idx -= k;
208
209 assert(j < MAX_LENGTH); /* 32 too large on 32 bit machines */
210 assert(bs->buf_bit_idx < MAX_LENGTH);
211
212 bs->buf[bs->buf_byte_idx] |= ((val >> j) << bs->buf_bit_idx);
213 bs->totbit += k;
214 }
215 }
216
217
218 /*
219 Some combinations of bitrate, Fs, and stereo make it impossible to stuff
220 out a frame using just main_data, due to the limited number of bits to
221 indicate main_data_length. In these situations, we put stuffing bits into
222 the ancillary data...
223 */
224
225 inline static void
drain_into_ancillary(lame_internal_flags * gfc,int remainingBits)226 drain_into_ancillary(lame_internal_flags * gfc, int remainingBits)
227 {
228 SessionConfig_t const *const cfg = &gfc->cfg;
229 EncStateVar_t *const esv = &gfc->sv_enc;
230 int i;
231 assert(remainingBits >= 0);
232
233 if (remainingBits >= 8) {
234 putbits2(gfc, 0x4c, 8);
235 remainingBits -= 8;
236 }
237 if (remainingBits >= 8) {
238 putbits2(gfc, 0x41, 8);
239 remainingBits -= 8;
240 }
241 if (remainingBits >= 8) {
242 putbits2(gfc, 0x4d, 8);
243 remainingBits -= 8;
244 }
245 if (remainingBits >= 8) {
246 putbits2(gfc, 0x45, 8);
247 remainingBits -= 8;
248 }
249
250 if (remainingBits >= 32) {
251 const char *const version = get_lame_short_version();
252 if (remainingBits >= 32)
253 for (i = 0; i < (int) strlen(version) && remainingBits >= 8; ++i) {
254 remainingBits -= 8;
255 putbits2(gfc, version[i], 8);
256 }
257 }
258
259 for (; remainingBits >= 1; remainingBits -= 1) {
260 putbits2(gfc, esv->ancillary_flag, 1);
261 esv->ancillary_flag ^= !cfg->disable_reservoir;
262 }
263
264 assert(remainingBits == 0);
265
266 }
267
268 /*write N bits into the header */
269 inline static void
writeheader(lame_internal_flags * gfc,int val,int j)270 writeheader(lame_internal_flags * gfc, int val, int j)
271 {
272 EncStateVar_t *const esv = &gfc->sv_enc;
273 int ptr = esv->header[esv->h_ptr].ptr;
274
275 while (j > 0) {
276 int const k = Min(j, 8 - (ptr & 7));
277 j -= k;
278 assert(j < MAX_LENGTH); /* >> 32 too large for 32 bit machines */
279 esv->header[esv->h_ptr].buf[ptr >> 3]
280 |= ((val >> j)) << (8 - (ptr & 7) - k);
281 ptr += k;
282 }
283 esv->header[esv->h_ptr].ptr = ptr;
284 }
285
286
287 static int
CRC_update(int value,int crc)288 CRC_update(int value, int crc)
289 {
290 int i;
291 value <<= 8;
292 for (i = 0; i < 8; i++) {
293 value <<= 1;
294 crc <<= 1;
295
296 if (((crc ^ value) & 0x10000))
297 crc ^= CRC16_POLYNOMIAL;
298 }
299 return crc;
300 }
301
302
303 void
CRC_writeheader(lame_internal_flags const * gfc,char * header)304 CRC_writeheader(lame_internal_flags const *gfc, char *header)
305 {
306 SessionConfig_t const *const cfg = &gfc->cfg;
307 int crc = 0xffff; /* (jo) init crc16 for error_protection */
308 int i;
309
310 crc = CRC_update(((unsigned char *) header)[2], crc);
311 crc = CRC_update(((unsigned char *) header)[3], crc);
312 for (i = 6; i < cfg->sideinfo_len; i++) {
313 crc = CRC_update(((unsigned char *) header)[i], crc);
314 }
315
316 header[4] = crc >> 8;
317 header[5] = crc & 255;
318 }
319
320 inline static void
encodeSideInfo2(lame_internal_flags * gfc,int bitsPerFrame)321 encodeSideInfo2(lame_internal_flags * gfc, int bitsPerFrame)
322 {
323 SessionConfig_t const *const cfg = &gfc->cfg;
324 EncResult_t const *const eov = &gfc->ov_enc;
325 EncStateVar_t *const esv = &gfc->sv_enc;
326 III_side_info_t *l3_side;
327 int gr, ch;
328
329 l3_side = &gfc->l3_side;
330 esv->header[esv->h_ptr].ptr = 0;
331 memset(esv->header[esv->h_ptr].buf, 0, cfg->sideinfo_len);
332 if (cfg->samplerate_out < 16000)
333 writeheader(gfc, 0xffe, 12);
334 else
335 writeheader(gfc, 0xfff, 12);
336 writeheader(gfc, (cfg->version), 1);
337 writeheader(gfc, 4 - 3, 2);
338 writeheader(gfc, (!cfg->error_protection), 1);
339 writeheader(gfc, (eov->bitrate_index), 4);
340 writeheader(gfc, (cfg->samplerate_index), 2);
341 writeheader(gfc, (eov->padding), 1);
342 writeheader(gfc, (cfg->extension), 1);
343 writeheader(gfc, (cfg->mode), 2);
344 writeheader(gfc, (eov->mode_ext), 2);
345 writeheader(gfc, (cfg->copyright), 1);
346 writeheader(gfc, (cfg->original), 1);
347 writeheader(gfc, (cfg->emphasis), 2);
348 if (cfg->error_protection) {
349 writeheader(gfc, 0, 16); /* dummy */
350 }
351
352 if (cfg->version == 1) {
353 /* MPEG1 */
354 assert(l3_side->main_data_begin >= 0);
355 writeheader(gfc, (l3_side->main_data_begin), 9);
356
357 if (cfg->channels_out == 2)
358 writeheader(gfc, l3_side->private_bits, 3);
359 else
360 writeheader(gfc, l3_side->private_bits, 5);
361
362 for (ch = 0; ch < cfg->channels_out; ch++) {
363 int band;
364 for (band = 0; band < 4; band++) {
365 writeheader(gfc, l3_side->scfsi[ch][band], 1);
366 }
367 }
368
369 for (gr = 0; gr < 2; gr++) {
370 for (ch = 0; ch < cfg->channels_out; ch++) {
371 gr_info *const gi = &l3_side->tt[gr][ch];
372 writeheader(gfc, gi->part2_3_length + gi->part2_length, 12);
373 writeheader(gfc, gi->big_values / 2, 9);
374 writeheader(gfc, gi->global_gain, 8);
375 writeheader(gfc, gi->scalefac_compress, 4);
376
377 if (gi->block_type != NORM_TYPE) {
378 writeheader(gfc, 1, 1); /* window_switching_flag */
379 writeheader(gfc, gi->block_type, 2);
380 writeheader(gfc, gi->mixed_block_flag, 1);
381
382 if (gi->table_select[0] == 14)
383 gi->table_select[0] = 16;
384 writeheader(gfc, gi->table_select[0], 5);
385 if (gi->table_select[1] == 14)
386 gi->table_select[1] = 16;
387 writeheader(gfc, gi->table_select[1], 5);
388
389 writeheader(gfc, gi->subblock_gain[0], 3);
390 writeheader(gfc, gi->subblock_gain[1], 3);
391 writeheader(gfc, gi->subblock_gain[2], 3);
392 }
393 else {
394 writeheader(gfc, 0, 1); /* window_switching_flag */
395 if (gi->table_select[0] == 14)
396 gi->table_select[0] = 16;
397 writeheader(gfc, gi->table_select[0], 5);
398 if (gi->table_select[1] == 14)
399 gi->table_select[1] = 16;
400 writeheader(gfc, gi->table_select[1], 5);
401 if (gi->table_select[2] == 14)
402 gi->table_select[2] = 16;
403 writeheader(gfc, gi->table_select[2], 5);
404
405 assert(0 <= gi->region0_count && gi->region0_count < 16);
406 assert(0 <= gi->region1_count && gi->region1_count < 8);
407 writeheader(gfc, gi->region0_count, 4);
408 writeheader(gfc, gi->region1_count, 3);
409 }
410 writeheader(gfc, gi->preflag, 1);
411 writeheader(gfc, gi->scalefac_scale, 1);
412 writeheader(gfc, gi->count1table_select, 1);
413 }
414 }
415 }
416 else {
417 /* MPEG2 */
418 assert(l3_side->main_data_begin >= 0);
419 writeheader(gfc, (l3_side->main_data_begin), 8);
420 writeheader(gfc, l3_side->private_bits, cfg->channels_out);
421
422 gr = 0;
423 for (ch = 0; ch < cfg->channels_out; ch++) {
424 gr_info *const gi = &l3_side->tt[gr][ch];
425 writeheader(gfc, gi->part2_3_length + gi->part2_length, 12);
426 writeheader(gfc, gi->big_values / 2, 9);
427 writeheader(gfc, gi->global_gain, 8);
428 writeheader(gfc, gi->scalefac_compress, 9);
429
430 if (gi->block_type != NORM_TYPE) {
431 writeheader(gfc, 1, 1); /* window_switching_flag */
432 writeheader(gfc, gi->block_type, 2);
433 writeheader(gfc, gi->mixed_block_flag, 1);
434
435 if (gi->table_select[0] == 14)
436 gi->table_select[0] = 16;
437 writeheader(gfc, gi->table_select[0], 5);
438 if (gi->table_select[1] == 14)
439 gi->table_select[1] = 16;
440 writeheader(gfc, gi->table_select[1], 5);
441
442 writeheader(gfc, gi->subblock_gain[0], 3);
443 writeheader(gfc, gi->subblock_gain[1], 3);
444 writeheader(gfc, gi->subblock_gain[2], 3);
445 }
446 else {
447 writeheader(gfc, 0, 1); /* window_switching_flag */
448 if (gi->table_select[0] == 14)
449 gi->table_select[0] = 16;
450 writeheader(gfc, gi->table_select[0], 5);
451 if (gi->table_select[1] == 14)
452 gi->table_select[1] = 16;
453 writeheader(gfc, gi->table_select[1], 5);
454 if (gi->table_select[2] == 14)
455 gi->table_select[2] = 16;
456 writeheader(gfc, gi->table_select[2], 5);
457
458 assert(0 <= gi->region0_count && gi->region0_count < 16);
459 assert(0 <= gi->region1_count && gi->region1_count < 8);
460 writeheader(gfc, gi->region0_count, 4);
461 writeheader(gfc, gi->region1_count, 3);
462 }
463
464 writeheader(gfc, gi->scalefac_scale, 1);
465 writeheader(gfc, gi->count1table_select, 1);
466 }
467 }
468
469 if (cfg->error_protection) {
470 /* (jo) error_protection: add crc16 information to header */
471 CRC_writeheader(gfc, esv->header[esv->h_ptr].buf);
472 }
473
474 {
475 int const old = esv->h_ptr;
476 assert(esv->header[old].ptr == cfg->sideinfo_len * 8);
477
478 esv->h_ptr = (old + 1) & (MAX_HEADER_BUF - 1);
479 esv->header[esv->h_ptr].write_timing = esv->header[old].write_timing + bitsPerFrame;
480
481 if (esv->h_ptr == esv->w_ptr) {
482 /* yikes! we are out of header buffer space */
483 ERRORF(gfc, "Error: MAX_HEADER_BUF too small in bitstream.c \n");
484 }
485
486 }
487 }
488
489
490 inline static int
huffman_coder_count1(lame_internal_flags * gfc,gr_info const * gi)491 huffman_coder_count1(lame_internal_flags * gfc, gr_info const *gi)
492 {
493 /* Write count1 area */
494 struct huffcodetab const *const h = &ht[gi->count1table_select + 32];
495 int i, bits = 0;
496 #ifdef DEBUG
497 int gegebo = gfc->bs.totbit;
498 #endif
499
500 int const *ix = &gi->l3_enc[gi->big_values];
501 FLOAT const *xr = &gi->xr[gi->big_values];
502 assert(gi->count1table_select < 2);
503
504 for (i = (gi->count1 - gi->big_values) / 4; i > 0; --i) {
505 int huffbits = 0;
506 int p = 0, v;
507
508 v = ix[0];
509 if (v) {
510 p += 8;
511 if (xr[0] < 0.0f)
512 huffbits++;
513 assert(v <= 1);
514 }
515
516 v = ix[1];
517 if (v) {
518 p += 4;
519 huffbits *= 2;
520 if (xr[1] < 0.0f)
521 huffbits++;
522 assert(v <= 1);
523 }
524
525 v = ix[2];
526 if (v) {
527 p += 2;
528 huffbits *= 2;
529 if (xr[2] < 0.0f)
530 huffbits++;
531 assert(v <= 1);
532 }
533
534 v = ix[3];
535 if (v) {
536 p++;
537 huffbits *= 2;
538 if (xr[3] < 0.0f)
539 huffbits++;
540 assert(v <= 1);
541 }
542
543 ix += 4;
544 xr += 4;
545 putbits2(gfc, huffbits + h->table[p], h->hlen[p]);
546 bits += h->hlen[p];
547 }
548 #ifdef DEBUG
549 DEBUGF(gfc, "count1: real: %ld counted:%d (bigv %d count1len %d)\n",
550 gfc->bs.totbit - gegebo, gi->count1bits, gi->big_values, gi->count1);
551 #endif
552 return bits;
553 }
554
555
556
557 /*
558 Implements the pseudocode of page 98 of the IS
559 */
560 inline static int
Huffmancode(lame_internal_flags * const gfc,const unsigned int tableindex,int start,int end,gr_info const * gi)561 Huffmancode(lame_internal_flags * const gfc, const unsigned int tableindex,
562 int start, int end, gr_info const *gi)
563 {
564 struct huffcodetab const *const h = &ht[tableindex];
565 unsigned int const linbits = h->xlen;
566 int i, bits = 0;
567
568 assert(tableindex < 32u);
569 if (!tableindex)
570 return bits;
571
572 for (i = start; i < end; i += 2) {
573 int16_t cbits = 0;
574 uint16_t xbits = 0;
575 unsigned int xlen = h->xlen;
576 unsigned int ext = 0;
577 unsigned int x1 = gi->l3_enc[i];
578 unsigned int x2 = gi->l3_enc[i + 1];
579
580 assert(gi->l3_enc[i] >= 0);
581 assert(gi->l3_enc[i+1] >= 0);
582
583 if (x1 != 0u) {
584 if (gi->xr[i] < 0.0f)
585 ext++;
586 cbits--;
587 }
588
589 if (tableindex > 15u) {
590 /* use ESC-words */
591 if (x1 >= 15u) {
592 uint16_t const linbits_x1 = x1 - 15u;
593 assert(linbits_x1 <= h->linmax);
594 ext |= linbits_x1 << 1u;
595 xbits = linbits;
596 x1 = 15u;
597 }
598
599 if (x2 >= 15u) {
600 uint16_t const linbits_x2 = x2 - 15u;
601 assert(linbits_x2 <= h->linmax);
602 ext <<= linbits;
603 ext |= linbits_x2;
604 xbits += linbits;
605 x2 = 15u;
606 }
607 xlen = 16;
608 }
609
610 if (x2 != 0u) {
611 ext <<= 1;
612 if (gi->xr[i + 1] < 0.0f)
613 ext++;
614 cbits--;
615 }
616
617 assert((x1 | x2) < 16u);
618
619 x1 = x1 * xlen + x2;
620 xbits -= cbits;
621 cbits += h->hlen[x1];
622
623 assert(cbits <= MAX_LENGTH);
624 assert(xbits <= MAX_LENGTH);
625
626 putbits2(gfc, h->table[x1], cbits);
627 putbits2(gfc, (int)ext, xbits);
628 bits += cbits + xbits;
629 }
630 return bits;
631 }
632
633 /*
634 Note the discussion of huffmancodebits() on pages 28
635 and 29 of the IS, as well as the definitions of the side
636 information on pages 26 and 27.
637 */
638 static int
ShortHuffmancodebits(lame_internal_flags * gfc,gr_info const * gi)639 ShortHuffmancodebits(lame_internal_flags * gfc, gr_info const *gi)
640 {
641 int bits;
642 int region1Start;
643
644 region1Start = 3 * gfc->scalefac_band.s[3];
645 if (region1Start > gi->big_values)
646 region1Start = gi->big_values;
647
648 /* short blocks do not have a region2 */
649 bits = Huffmancode(gfc, gi->table_select[0], 0, region1Start, gi);
650 bits += Huffmancode(gfc, gi->table_select[1], region1Start, gi->big_values, gi);
651 return bits;
652 }
653
654 static int
LongHuffmancodebits(lame_internal_flags * gfc,gr_info const * gi)655 LongHuffmancodebits(lame_internal_flags * gfc, gr_info const *gi)
656 {
657 unsigned int i;
658 int bigvalues, bits;
659 int region1Start, region2Start;
660
661 bigvalues = gi->big_values;
662 assert(0 <= bigvalues && bigvalues <= 576);
663
664 assert(gi->region0_count >= -1);
665 assert(gi->region1_count >= -1);
666 i = gi->region0_count + 1;
667 assert((size_t) i < dimension_of(gfc->scalefac_band.l));
668 region1Start = gfc->scalefac_band.l[i];
669 i += gi->region1_count + 1;
670 assert((size_t) i < dimension_of(gfc->scalefac_band.l));
671 region2Start = gfc->scalefac_band.l[i];
672
673 if (region1Start > bigvalues)
674 region1Start = bigvalues;
675
676 if (region2Start > bigvalues)
677 region2Start = bigvalues;
678
679 bits = Huffmancode(gfc, gi->table_select[0], 0, region1Start, gi);
680 bits += Huffmancode(gfc, gi->table_select[1], region1Start, region2Start, gi);
681 bits += Huffmancode(gfc, gi->table_select[2], region2Start, bigvalues, gi);
682 return bits;
683 }
684
685 inline static int
writeMainData(lame_internal_flags * const gfc)686 writeMainData(lame_internal_flags * const gfc)
687 {
688 SessionConfig_t const *const cfg = &gfc->cfg;
689 III_side_info_t const *const l3_side = &gfc->l3_side;
690 int gr, ch, sfb, data_bits, tot_bits = 0;
691
692 if (cfg->version == 1) {
693 /* MPEG 1 */
694 for (gr = 0; gr < 2; gr++) {
695 for (ch = 0; ch < cfg->channels_out; ch++) {
696 gr_info const *const gi = &l3_side->tt[gr][ch];
697 int const slen1 = slen1_tab[gi->scalefac_compress];
698 int const slen2 = slen2_tab[gi->scalefac_compress];
699 data_bits = 0;
700 #ifdef DEBUG
701 hogege = gfc->bs.totbit;
702 #endif
703 for (sfb = 0; sfb < gi->sfbdivide; sfb++) {
704 if (gi->scalefac[sfb] == -1)
705 continue; /* scfsi is used */
706 putbits2(gfc, gi->scalefac[sfb], slen1);
707 data_bits += slen1;
708 }
709 for (; sfb < gi->sfbmax; sfb++) {
710 if (gi->scalefac[sfb] == -1)
711 continue; /* scfsi is used */
712 putbits2(gfc, gi->scalefac[sfb], slen2);
713 data_bits += slen2;
714 }
715 assert(data_bits == gi->part2_length);
716
717 if (gi->block_type == SHORT_TYPE) {
718 data_bits += ShortHuffmancodebits(gfc, gi);
719 }
720 else {
721 data_bits += LongHuffmancodebits(gfc, gi);
722 }
723 data_bits += huffman_coder_count1(gfc, gi);
724 #ifdef DEBUG
725 DEBUGF(gfc, "<%ld> ", gfc->bs.totbit - hogege);
726 #endif
727 /* does bitcount in quantize.c agree with actual bit count? */
728 assert(data_bits == gi->part2_3_length + gi->part2_length);
729 tot_bits += data_bits;
730 } /* for ch */
731 } /* for gr */
732 }
733 else {
734 /* MPEG 2 */
735 gr = 0;
736 for (ch = 0; ch < cfg->channels_out; ch++) {
737 gr_info const *const gi = &l3_side->tt[gr][ch];
738 int i, sfb_partition, scale_bits = 0;
739 assert(gi->sfb_partition_table);
740 data_bits = 0;
741 #ifdef DEBUG
742 hogege = gfc->bs.totbit;
743 #endif
744 sfb = 0;
745 sfb_partition = 0;
746
747 if (gi->block_type == SHORT_TYPE) {
748 for (; sfb_partition < 4; sfb_partition++) {
749 int const sfbs = gi->sfb_partition_table[sfb_partition] / 3;
750 int const slen = gi->slen[sfb_partition];
751 for (i = 0; i < sfbs; i++, sfb++) {
752 putbits2(gfc, Max(gi->scalefac[sfb * 3 + 0], 0), slen);
753 putbits2(gfc, Max(gi->scalefac[sfb * 3 + 1], 0), slen);
754 putbits2(gfc, Max(gi->scalefac[sfb * 3 + 2], 0), slen);
755 scale_bits += 3 * slen;
756 }
757 }
758 data_bits += ShortHuffmancodebits(gfc, gi);
759 }
760 else {
761 for (; sfb_partition < 4; sfb_partition++) {
762 int const sfbs = gi->sfb_partition_table[sfb_partition];
763 int const slen = gi->slen[sfb_partition];
764 for (i = 0; i < sfbs; i++, sfb++) {
765 putbits2(gfc, Max(gi->scalefac[sfb], 0), slen);
766 scale_bits += slen;
767 }
768 }
769 data_bits += LongHuffmancodebits(gfc, gi);
770 }
771 data_bits += huffman_coder_count1(gfc, gi);
772 #ifdef DEBUG
773 DEBUGF(gfc, "<%ld> ", gfc->bs.totbit - hogege);
774 #endif
775 /* does bitcount in quantize.c agree with actual bit count? */
776 assert(data_bits == gi->part2_3_length);
777 assert(scale_bits == gi->part2_length);
778 tot_bits += scale_bits + data_bits;
779 } /* for ch */
780 } /* for gf */
781 return tot_bits;
782 } /* main_data */
783
784
785
786 /* compute the number of bits required to flush all mp3 frames
787 currently in the buffer. This should be the same as the
788 reservoir size. Only call this routine between frames - i.e.
789 only after all headers and data have been added to the buffer
790 by format_bitstream().
791
792 Also compute total_bits_output =
793 size of mp3 buffer (including frame headers which may not
794 have yet been send to the mp3 buffer) +
795 number of bits needed to flush all mp3 frames.
796
797 total_bytes_output is the size of the mp3 output buffer if
798 lame_encode_flush_nogap() was called right now.
799
800 */
801 int
compute_flushbits(const lame_internal_flags * gfc,int * total_bytes_output)802 compute_flushbits(const lame_internal_flags * gfc, int *total_bytes_output)
803 {
804 SessionConfig_t const *const cfg = &gfc->cfg;
805 EncStateVar_t const *const esv = &gfc->sv_enc;
806 int flushbits, remaining_headers;
807 int bitsPerFrame;
808 int last_ptr, first_ptr;
809 first_ptr = esv->w_ptr; /* first header to add to bitstream */
810 last_ptr = esv->h_ptr - 1; /* last header to add to bitstream */
811 if (last_ptr == -1)
812 last_ptr = MAX_HEADER_BUF - 1;
813
814 /* add this many bits to bitstream so we can flush all headers */
815 flushbits = esv->header[last_ptr].write_timing - gfc->bs.totbit;
816 *total_bytes_output = flushbits;
817
818 if (flushbits >= 0) {
819 /* if flushbits >= 0, some headers have not yet been written */
820 /* reduce flushbits by the size of the headers */
821 remaining_headers = 1 + last_ptr - first_ptr;
822 if (last_ptr < first_ptr)
823 remaining_headers = 1 + last_ptr - first_ptr + MAX_HEADER_BUF;
824 flushbits -= remaining_headers * 8 * cfg->sideinfo_len;
825 }
826
827
828 /* finally, add some bits so that the last frame is complete
829 * these bits are not necessary to decode the last frame, but
830 * some decoders will ignore last frame if these bits are missing
831 */
832 bitsPerFrame = getframebits(gfc);
833 flushbits += bitsPerFrame;
834 *total_bytes_output += bitsPerFrame;
835 /* round up: */
836 if (*total_bytes_output % 8)
837 *total_bytes_output = 1 + (*total_bytes_output / 8);
838 else
839 *total_bytes_output = (*total_bytes_output / 8);
840 *total_bytes_output += gfc->bs.buf_byte_idx + 1;
841
842
843 if (flushbits < 0) {
844 #if 0
845 /* if flushbits < 0, this would mean that the buffer looks like:
846 * (data...) last_header (data...) (extra data that should not be here...)
847 */
848 DEBUGF(gfc, "last header write_timing = %i \n", esv->header[last_ptr].write_timing);
849 DEBUGF(gfc, "first header write_timing = %i \n", esv->header[first_ptr].write_timing);
850 DEBUGF(gfc, "bs.totbit: %i \n", gfc->bs.totbit);
851 DEBUGF(gfc, "first_ptr, last_ptr %i %i \n", first_ptr, last_ptr);
852 DEBUGF(gfc, "remaining_headers = %i \n", remaining_headers);
853 DEBUGF(gfc, "bitsperframe: %i \n", bitsPerFrame);
854 DEBUGF(gfc, "sidelen: %i \n", cfg->sideinfo_len);
855 #endif
856 ERRORF(gfc, "strange error flushing buffer ... \n");
857 }
858 return flushbits;
859 }
860
861
862 void
flush_bitstream(lame_internal_flags * gfc)863 flush_bitstream(lame_internal_flags * gfc)
864 {
865 EncStateVar_t *const esv = &gfc->sv_enc;
866 III_side_info_t *l3_side;
867 int nbytes;
868 int flushbits;
869 int last_ptr = esv->h_ptr - 1; /* last header to add to bitstream */
870 if (last_ptr == -1)
871 last_ptr = MAX_HEADER_BUF - 1;
872 l3_side = &gfc->l3_side;
873
874
875 if ((flushbits = compute_flushbits(gfc, &nbytes)) < 0)
876 return;
877 drain_into_ancillary(gfc, flushbits);
878
879 /* check that the 100% of the last frame has been written to bitstream */
880 assert(esv->header[last_ptr].write_timing + getframebits(gfc)
881 == gfc->bs.totbit);
882
883 /* we have padded out all frames with ancillary data, which is the
884 same as filling the bitreservoir with ancillary data, so : */
885 esv->ResvSize = 0;
886 l3_side->main_data_begin = 0;
887 }
888
889
890
891
892 void
add_dummy_byte(lame_internal_flags * gfc,unsigned char val,unsigned int n)893 add_dummy_byte(lame_internal_flags * gfc, unsigned char val, unsigned int n)
894 {
895 EncStateVar_t *const esv = &gfc->sv_enc;
896 int i;
897
898 while (n-- > 0u) {
899 putbits_noheaders(gfc, val, 8);
900
901 for (i = 0; i < MAX_HEADER_BUF; ++i)
902 esv->header[i].write_timing += 8;
903 }
904 }
905
906
907 /*
908 format_bitstream()
909
910 This is called after a frame of audio has been quantized and coded.
911 It will write the encoded audio to the bitstream. Note that
912 from a layer3 encoder's perspective the bit stream is primarily
913 a series of main_data() blocks, with header and side information
914 inserted at the proper locations to maintain framing. (See Figure A.7
915 in the IS).
916 */
917 int
format_bitstream(lame_internal_flags * gfc)918 format_bitstream(lame_internal_flags * gfc)
919 {
920 SessionConfig_t const *const cfg = &gfc->cfg;
921 EncStateVar_t *const esv = &gfc->sv_enc;
922 int bits, nbytes;
923 III_side_info_t *l3_side;
924 int bitsPerFrame;
925 l3_side = &gfc->l3_side;
926
927 bitsPerFrame = getframebits(gfc);
928 drain_into_ancillary(gfc, l3_side->resvDrain_pre);
929
930 encodeSideInfo2(gfc, bitsPerFrame);
931 bits = 8 * cfg->sideinfo_len;
932 bits += writeMainData(gfc);
933 drain_into_ancillary(gfc, l3_side->resvDrain_post);
934 bits += l3_side->resvDrain_post;
935
936 l3_side->main_data_begin += (bitsPerFrame - bits) / 8;
937
938 /* compare number of bits needed to clear all buffered mp3 frames
939 * with what we think the resvsize is: */
940 if (compute_flushbits(gfc, &nbytes) != esv->ResvSize) {
941 ERRORF(gfc, "Internal buffer inconsistency. flushbits <> ResvSize");
942 }
943
944
945 /* compare main_data_begin for the next frame with what we
946 * think the resvsize is: */
947 if ((l3_side->main_data_begin * 8) != esv->ResvSize) {
948 ERRORF(gfc, "bit reservoir error: \n"
949 "l3_side->main_data_begin: %i \n"
950 "Resvoir size: %i \n"
951 "resv drain (post) %i \n"
952 "resv drain (pre) %i \n"
953 "header and sideinfo: %i \n"
954 "data bits: %i \n"
955 "total bits: %i (remainder: %i) \n"
956 "bitsperframe: %i \n",
957 8 * l3_side->main_data_begin,
958 esv->ResvSize,
959 l3_side->resvDrain_post,
960 l3_side->resvDrain_pre,
961 8 * cfg->sideinfo_len,
962 bits - l3_side->resvDrain_post - 8 * cfg->sideinfo_len,
963 bits, bits % 8, bitsPerFrame);
964
965 ERRORF(gfc, "This is a fatal error. It has several possible causes:");
966 ERRORF(gfc, "90%% LAME compiled with buggy version of gcc using advanced optimizations");
967 ERRORF(gfc, " 9%% Your system is overclocked");
968 ERRORF(gfc, " 1%% bug in LAME encoding library");
969
970 esv->ResvSize = l3_side->main_data_begin * 8;
971 };
972 assert(gfc->bs.totbit % 8 == 0);
973
974 if (gfc->bs.totbit > 1000000000) {
975 /* to avoid totbit overflow, (at 8h encoding at 128kbs) lets reset bit counter */
976 int i;
977 for (i = 0; i < MAX_HEADER_BUF; ++i)
978 esv->header[i].write_timing -= gfc->bs.totbit;
979 gfc->bs.totbit = 0;
980 }
981
982
983 return 0;
984 }
985
986
987 static int
do_gain_analysis(lame_internal_flags * gfc,unsigned char * buffer,int minimum)988 do_gain_analysis(lame_internal_flags * gfc, unsigned char* buffer, int minimum)
989 {
990 SessionConfig_t const *const cfg = &gfc->cfg;
991 RpgStateVar_t const *const rsv = &gfc->sv_rpg;
992 RpgResult_t *const rov = &gfc->ov_rpg;
993 #ifdef DECODE_ON_THE_FLY
994 if (cfg->decode_on_the_fly) { /* decode the frame */
995 sample_t pcm_buf[2][1152];
996 int mp3_in = minimum;
997 int samples_out = -1;
998
999 /* re-synthesis to pcm. Repeat until we get a samples_out=0 */
1000 while (samples_out != 0) {
1001
1002 samples_out = hip_decode1_unclipped(gfc->hip, buffer, mp3_in, pcm_buf[0], pcm_buf[1]);
1003 /* samples_out = 0: need more data to decode
1004 * samples_out = -1: error. Lets assume 0 pcm output
1005 * samples_out = number of samples output */
1006
1007 /* set the lenght of the mp3 input buffer to zero, so that in the
1008 * next iteration of the loop we will be querying mpglib about
1009 * buffered data */
1010 mp3_in = 0;
1011
1012 if (samples_out == -1) {
1013 /* error decoding. Not fatal, but might screw up
1014 * the ReplayGain tag. What should we do? Ignore for now */
1015 samples_out = 0;
1016 }
1017 if (samples_out > 0) {
1018 /* process the PCM data */
1019
1020 /* this should not be possible, and indicates we have
1021 * overflown the pcm_buf buffer */
1022 assert(samples_out <= 1152);
1023
1024 if (cfg->findPeakSample) {
1025 int i;
1026 /* FIXME: is this correct? maybe Max(fabs(pcm),PeakSample) */
1027 for (i = 0; i < samples_out; i++) {
1028 if (pcm_buf[0][i] > rov->PeakSample)
1029 rov->PeakSample = pcm_buf[0][i];
1030 else if (-pcm_buf[0][i] > rov->PeakSample)
1031 rov->PeakSample = -pcm_buf[0][i];
1032 }
1033 if (cfg->channels_out > 1)
1034 for (i = 0; i < samples_out; i++) {
1035 if (pcm_buf[1][i] > rov->PeakSample)
1036 rov->PeakSample = pcm_buf[1][i];
1037 else if (-pcm_buf[1][i] > rov->PeakSample)
1038 rov->PeakSample = -pcm_buf[1][i];
1039 }
1040 }
1041
1042 if (cfg->findReplayGain)
1043 if (AnalyzeSamples
1044 (rsv->rgdata, pcm_buf[0], pcm_buf[1], samples_out,
1045 cfg->channels_out) == GAIN_ANALYSIS_ERROR)
1046 return -6;
1047
1048 } /* if (samples_out>0) */
1049 } /* while (samples_out!=0) */
1050 } /* if (gfc->decode_on_the_fly) */
1051 #endif
1052 return minimum;
1053 }
1054
1055 static int
do_copy_buffer(lame_internal_flags * gfc,unsigned char * buffer,int size)1056 do_copy_buffer(lame_internal_flags * gfc, unsigned char *buffer, int size)
1057 {
1058 Bit_stream_struc *const bs = &gfc->bs;
1059 int const minimum = bs->buf_byte_idx + 1;
1060 if (minimum <= 0)
1061 return 0;
1062 if (minimum > size)
1063 return -1; /* buffer is too small */
1064 memcpy(buffer, bs->buf, minimum);
1065 bs->buf_byte_idx = -1;
1066 bs->buf_bit_idx = 0;
1067 return minimum;
1068 }
1069
1070 /* copy data out of the internal MP3 bit buffer into a user supplied
1071 unsigned char buffer.
1072
1073 mp3data=0 indicates data in buffer is an id3tags and VBR tags
1074 mp3data=1 data is real mp3 frame data.
1075
1076
1077 */
1078 int
copy_buffer(lame_internal_flags * gfc,unsigned char * buffer,int size,int mp3data)1079 copy_buffer(lame_internal_flags * gfc, unsigned char *buffer, int size, int mp3data)
1080 {
1081 int const minimum = do_copy_buffer(gfc, buffer, size);
1082 if (minimum > 0 && mp3data) {
1083 UpdateMusicCRC(&gfc->nMusicCRC, buffer, minimum);
1084
1085 /** sum number of bytes belonging to the mp3 stream
1086 * this info will be written into the Xing/LAME header for seeking
1087 */
1088 gfc->VBR_seek_table.nBytesWritten += minimum;
1089
1090 return do_gain_analysis(gfc, buffer, minimum);
1091 } /* if (mp3data) */
1092 return minimum;
1093 }
1094
1095
1096 void
init_bit_stream_w(lame_internal_flags * gfc)1097 init_bit_stream_w(lame_internal_flags * gfc)
1098 {
1099 EncStateVar_t *const esv = &gfc->sv_enc;
1100
1101 esv->h_ptr = esv->w_ptr = 0;
1102 esv->header[esv->h_ptr].write_timing = 0;
1103
1104 gfc->bs.buf = lame_calloc(unsigned char, BUFFER_SIZE);
1105 gfc->bs.buf_size = BUFFER_SIZE;
1106 gfc->bs.buf_byte_idx = -1;
1107 gfc->bs.buf_bit_idx = 0;
1108 gfc->bs.totbit = 0;
1109 }
1110
1111 /* end of bitstream.c */
1112