2 * MP3 bitstream Output interface for LAME
4 * Copyright (c) 1999-2000 Mark Taylor
5 * Copyright (c) 1999-2002 Takehiro Tominaga
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.
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.
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.
22 * $Id: bitstream.c,v 1.97 2011/05/07 16:05:17 rbrito Exp $
38 #include "quantize_pvt.h"
39 #include "lame_global_flags.h"
40 #include "gain_analysis.h"
42 #include "bitstream.h"
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 */
61 calcFrameLength(SessionConfig_t const *const cfg, int kbps, int pad)
63 return 8 * ((cfg->version + 1) * 72000 * kbps / cfg->samplerate_out + pad);
67 /***********************************************************************
68 * compute bitsperframe and mean_bits for a layer III frame
69 **********************************************************************/
71 getframebits(const lame_internal_flags * gfc)
73 SessionConfig_t const *const cfg = &gfc->cfg;
74 EncResult_t const *const eov = &gfc->ov_enc;
77 /* get bitrate in kbps [?] */
78 if (eov->bitrate_index)
79 bit_rate = bitrate_table[cfg->version][eov->bitrate_index];
81 bit_rate = cfg->avg_bitrate;
82 /*assert(bit_rate <= 550); */
83 assert(8 <= bit_rate && bit_rate <= 640);
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);
91 get_max_frame_buffer_size_by_constraint(SessionConfig_t const * cfg, int constraint)
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);
100 /* maximum allowed bits per granule are 7680 */
101 maxmp3buf = 7680 * (cfg->version + 1);
106 if (cfg->samplerate_out < 16000) {
107 max_kbps = bitrate_table[cfg->version][8]; /* default: allow 64 kbps (MPEG-2.5) */
110 max_kbps = bitrate_table[cfg->version][14];
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;
121 maxmp3buf = calcFrameLength(cfg, max_kbps, 0);
124 maxmp3buf = 7680 * (cfg->version + 1);
133 putheader_bits(lame_internal_flags * gfc)
135 SessionConfig_t const *const cfg = &gfc->cfg;
136 EncStateVar_t *const esv = &gfc->sv_enc;
137 Bit_stream_struc *bs = &gfc->bs;
139 hogege += cfg->sideinfo_len * 8;
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);
150 /*write j bits into the bit stream */
152 putbits2(lame_internal_flags * gfc, int val, int j)
154 EncStateVar_t const *const esv = &gfc->sv_enc;
155 Bit_stream_struc *bs;
158 assert(j < MAX_LENGTH - 2);
162 if (bs->buf_bit_idx == 0) {
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) {
170 bs->buf[bs->buf_byte_idx] = 0;
173 k = Min(j, bs->buf_bit_idx);
176 bs->buf_bit_idx -= k;
178 assert(j < MAX_LENGTH); /* 32 too large on 32 bit machines */
179 assert(bs->buf_bit_idx < MAX_LENGTH);
181 bs->buf[bs->buf_byte_idx] |= ((val >> j) << bs->buf_bit_idx);
186 /*write j bits into the bit stream, ignoring frame headers */
188 putbits_noheaders(lame_internal_flags * gfc, int val, int j)
190 Bit_stream_struc *bs;
193 assert(j < MAX_LENGTH - 2);
197 if (bs->buf_bit_idx == 0) {
200 assert(bs->buf_byte_idx < BUFFER_SIZE);
201 bs->buf[bs->buf_byte_idx] = 0;
204 k = Min(j, bs->buf_bit_idx);
207 bs->buf_bit_idx -= k;
209 assert(j < MAX_LENGTH); /* 32 too large on 32 bit machines */
210 assert(bs->buf_bit_idx < MAX_LENGTH);
212 bs->buf[bs->buf_byte_idx] |= ((val >> j) << bs->buf_bit_idx);
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...
226 drain_into_ancillary(lame_internal_flags * gfc, int remainingBits)
228 SessionConfig_t const *const cfg = &gfc->cfg;
229 EncStateVar_t *const esv = &gfc->sv_enc;
231 assert(remainingBits >= 0);
233 if (remainingBits >= 8) {
234 putbits2(gfc, 0x4c, 8);
237 if (remainingBits >= 8) {
238 putbits2(gfc, 0x41, 8);
241 if (remainingBits >= 8) {
242 putbits2(gfc, 0x4d, 8);
245 if (remainingBits >= 8) {
246 putbits2(gfc, 0x45, 8);
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) {
255 putbits2(gfc, version[i], 8);
259 for (; remainingBits >= 1; remainingBits -= 1) {
260 putbits2(gfc, esv->ancillary_flag, 1);
261 esv->ancillary_flag ^= !cfg->disable_reservoir;
264 assert(remainingBits == 0);
268 /*write N bits into the header */
270 writeheader(lame_internal_flags * gfc, int val, int j)
272 EncStateVar_t *const esv = &gfc->sv_enc;
273 int ptr = esv->header[esv->h_ptr].ptr;
276 int const k = Min(j, 8 - (ptr & 7));
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);
283 esv->header[esv->h_ptr].ptr = ptr;
288 CRC_update(int value, int crc)
292 for (i = 0; i < 8; i++) {
296 if (((crc ^ value) & 0x10000))
297 crc ^= CRC16_POLYNOMIAL;
304 CRC_writeheader(lame_internal_flags const *gfc, char *header)
306 SessionConfig_t const *const cfg = &gfc->cfg;
307 int crc = 0xffff; /* (jo) init crc16 for error_protection */
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);
316 header[4] = crc >> 8;
317 header[5] = crc & 255;
321 encodeSideInfo2(lame_internal_flags * gfc, int bitsPerFrame)
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;
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);
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 */
352 if (cfg->version == 1) {
354 assert(l3_side->main_data_begin >= 0);
355 writeheader(gfc, (l3_side->main_data_begin), 9);
357 if (cfg->channels_out == 2)
358 writeheader(gfc, l3_side->private_bits, 3);
360 writeheader(gfc, l3_side->private_bits, 5);
362 for (ch = 0; ch < cfg->channels_out; ch++) {
364 for (band = 0; band < 4; band++) {
365 writeheader(gfc, l3_side->scfsi[ch][band], 1);
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);
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);
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);
389 writeheader(gfc, gi->subblock_gain[0], 3);
390 writeheader(gfc, gi->subblock_gain[1], 3);
391 writeheader(gfc, gi->subblock_gain[2], 3);
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);
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);
410 writeheader(gfc, gi->preflag, 1);
411 writeheader(gfc, gi->scalefac_scale, 1);
412 writeheader(gfc, gi->count1table_select, 1);
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);
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);
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);
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);
442 writeheader(gfc, gi->subblock_gain[0], 3);
443 writeheader(gfc, gi->subblock_gain[1], 3);
444 writeheader(gfc, gi->subblock_gain[2], 3);
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);
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);
464 writeheader(gfc, gi->scalefac_scale, 1);
465 writeheader(gfc, gi->count1table_select, 1);
469 if (cfg->error_protection) {
470 /* (jo) error_protection: add crc16 information to header */
471 CRC_writeheader(gfc, esv->header[esv->h_ptr].buf);
475 int const old = esv->h_ptr;
476 assert(esv->header[old].ptr == cfg->sideinfo_len * 8);
478 esv->h_ptr = (old + 1) & (MAX_HEADER_BUF - 1);
479 esv->header[esv->h_ptr].write_timing = esv->header[old].write_timing + bitsPerFrame;
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");
491 huffman_coder_count1(lame_internal_flags * gfc, gr_info const *gi)
493 /* Write count1 area */
494 struct huffcodetab const *const h = &ht[gi->count1table_select + 32];
497 int gegebo = gfc->bs.totbit;
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);
504 for (i = (gi->count1 - gi->big_values) / 4; i > 0; --i) {
545 putbits2(gfc, huffbits + h->table[p], h->hlen[p]);
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);
558 Implements the pseudocode of page 98 of the IS
561 Huffmancode(lame_internal_flags * const gfc, const unsigned int tableindex,
562 int start, int end, gr_info const *gi)
564 struct huffcodetab const *const h = &ht[tableindex];
565 unsigned int const linbits = h->xlen;
568 assert(tableindex < 32u);
572 for (i = start; i < end; i += 2) {
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];
580 assert(gi->l3_enc[i] >= 0);
581 assert(gi->l3_enc[i+1] >= 0);
584 if (gi->xr[i] < 0.0f)
589 if (tableindex > 15u) {
592 uint16_t const linbits_x1 = x1 - 15u;
593 assert(linbits_x1 <= h->linmax);
594 ext |= linbits_x1 << 1u;
600 uint16_t const linbits_x2 = x2 - 15u;
601 assert(linbits_x2 <= h->linmax);
612 if (gi->xr[i + 1] < 0.0f)
617 assert((x1 | x2) < 16u);
621 cbits += h->hlen[x1];
623 assert(cbits <= MAX_LENGTH);
624 assert(xbits <= MAX_LENGTH);
626 putbits2(gfc, h->table[x1], cbits);
627 putbits2(gfc, (int)ext, xbits);
628 bits += cbits + xbits;
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.
639 ShortHuffmancodebits(lame_internal_flags * gfc, gr_info const *gi)
644 region1Start = 3 * gfc->scalefac_band.s[3];
645 if (region1Start > gi->big_values)
646 region1Start = gi->big_values;
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);
655 LongHuffmancodebits(lame_internal_flags * gfc, gr_info const *gi)
659 int region1Start, region2Start;
661 bigvalues = gi->big_values;
662 assert(0 <= bigvalues && bigvalues <= 576);
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];
673 if (region1Start > bigvalues)
674 region1Start = bigvalues;
676 if (region2Start > bigvalues)
677 region2Start = bigvalues;
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);
686 writeMainData(lame_internal_flags * const gfc)
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;
692 if (cfg->version == 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];
701 hogege = gfc->bs.totbit;
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);
709 for (; sfb < gi->sfbmax; sfb++) {
710 if (gi->scalefac[sfb] == -1)
711 continue; /* scfsi is used */
712 putbits2(gfc, gi->scalefac[sfb], slen2);
715 assert(data_bits == gi->part2_length);
717 if (gi->block_type == SHORT_TYPE) {
718 data_bits += ShortHuffmancodebits(gfc, gi);
721 data_bits += LongHuffmancodebits(gfc, gi);
723 data_bits += huffman_coder_count1(gfc, gi);
725 DEBUGF(gfc, "<%ld> ", gfc->bs.totbit - hogege);
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;
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);
742 hogege = gfc->bs.totbit;
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;
758 data_bits += ShortHuffmancodebits(gfc, gi);
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);
769 data_bits += LongHuffmancodebits(gfc, gi);
771 data_bits += huffman_coder_count1(gfc, gi);
773 DEBUGF(gfc, "<%ld> ", gfc->bs.totbit - hogege);
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;
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().
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.
797 total_bytes_output is the size of the mp3 output buffer if
798 lame_encode_flush_nogap() was called right now.
802 compute_flushbits(const lame_internal_flags * gfc, int *total_bytes_output)
804 SessionConfig_t const *const cfg = &gfc->cfg;
805 EncStateVar_t const *const esv = &gfc->sv_enc;
806 int flushbits, remaining_headers;
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 */
812 last_ptr = MAX_HEADER_BUF - 1;
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;
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;
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
832 bitsPerFrame = getframebits(gfc);
833 flushbits += bitsPerFrame;
834 *total_bytes_output += bitsPerFrame;
836 if (*total_bytes_output % 8)
837 *total_bytes_output = 1 + (*total_bytes_output / 8);
839 *total_bytes_output = (*total_bytes_output / 8);
840 *total_bytes_output += gfc->bs.buf_byte_idx + 1;
845 /* if flushbits < 0, this would mean that the buffer looks like:
846 * (data...) last_header (data...) (extra data that should not be here...)
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);
856 ERRORF(gfc, "strange error flushing buffer ... \n");
863 flush_bitstream(lame_internal_flags * gfc)
865 EncStateVar_t *const esv = &gfc->sv_enc;
866 III_side_info_t *l3_side;
869 int last_ptr = esv->h_ptr - 1; /* last header to add to bitstream */
871 last_ptr = MAX_HEADER_BUF - 1;
872 l3_side = &gfc->l3_side;
875 if ((flushbits = compute_flushbits(gfc, &nbytes)) < 0)
877 drain_into_ancillary(gfc, flushbits);
879 /* check that the 100% of the last frame has been written to bitstream */
880 assert(esv->header[last_ptr].write_timing + getframebits(gfc)
883 /* we have padded out all frames with ancillary data, which is the
884 same as filling the bitreservoir with ancillary data, so : */
886 l3_side->main_data_begin = 0;
893 add_dummy_byte(lame_internal_flags * gfc, unsigned char val, unsigned int n)
895 EncStateVar_t *const esv = &gfc->sv_enc;
899 putbits_noheaders(gfc, val, 8);
901 for (i = 0; i < MAX_HEADER_BUF; ++i)
902 esv->header[i].write_timing += 8;
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
918 format_bitstream(lame_internal_flags * gfc)
920 SessionConfig_t const *const cfg = &gfc->cfg;
921 EncStateVar_t *const esv = &gfc->sv_enc;
923 III_side_info_t *l3_side;
925 l3_side = &gfc->l3_side;
927 bitsPerFrame = getframebits(gfc);
928 drain_into_ancillary(gfc, l3_side->resvDrain_pre);
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;
936 l3_side->main_data_begin += (bitsPerFrame - bits) / 8;
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");
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"
955 "total bits: %i (remainder: %i) \n"
956 "bitsperframe: %i \n",
957 8 * l3_side->main_data_begin,
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);
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");
970 esv->ResvSize = l3_side->main_data_begin * 8;
972 assert(gfc->bs.totbit % 8 == 0);
974 if (gfc->bs.totbit > 1000000000) {
975 /* to avoid totbit overflow, (at 8h encoding at 128kbs) lets reset bit counter */
977 for (i = 0; i < MAX_HEADER_BUF; ++i)
978 esv->header[i].write_timing -= gfc->bs.totbit;
988 do_gain_analysis(lame_internal_flags * gfc, unsigned char* buffer, int minimum)
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;
999 /* re-synthesis to pcm. Repeat until we get a samples_out=0 */
1000 while (samples_out != 0) {
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 */
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
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 */
1017 if (samples_out > 0) {
1018 /* process the PCM data */
1020 /* this should not be possible, and indicates we have
1021 * overflown the pcm_buf buffer */
1022 assert(samples_out <= 1152);
1024 if (cfg->findPeakSample) {
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];
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];
1042 if (cfg->findReplayGain)
1044 (rsv->rgdata, pcm_buf[0], pcm_buf[1], samples_out,
1045 cfg->channels_out) == GAIN_ANALYSIS_ERROR)
1048 } /* if (samples_out>0) */
1049 } /* while (samples_out!=0) */
1050 } /* if (gfc->decode_on_the_fly) */
1056 do_copy_buffer(lame_internal_flags * gfc, unsigned char *buffer, int size)
1058 Bit_stream_struc *const bs = &gfc->bs;
1059 int const minimum = bs->buf_byte_idx + 1;
1062 if (size != 0 && 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;
1070 /* copy data out of the internal MP3 bit buffer into a user supplied
1071 unsigned char buffer.
1073 mp3data=0 indicates data in buffer is an id3tags and VBR tags
1074 mp3data=1 data is real mp3 frame data.
1079 copy_buffer(lame_internal_flags * gfc, unsigned char *buffer, int size, int mp3data)
1081 int const minimum = do_copy_buffer(gfc, buffer, size);
1082 if (minimum > 0 && mp3data) {
1083 UpdateMusicCRC(&gfc->nMusicCRC, buffer, minimum);
1085 /** sum number of bytes belonging to the mp3 stream
1086 * this info will be written into the Xing/LAME header for seeking
1088 gfc->VBR_seek_table.nBytesWritten += minimum;
1090 return do_gain_analysis(gfc, buffer, minimum);
1091 } /* if (mp3data) */
1097 init_bit_stream_w(lame_internal_flags * gfc)
1099 EncStateVar_t *const esv = &gfc->sv_enc;
1101 esv->h_ptr = esv->w_ptr = 0;
1102 esv->header[esv->h_ptr].write_timing = 0;
1104 gfc->bs.buf = (unsigned char *) malloc(BUFFER_SIZE);
1105 gfc->bs.buf_size = BUFFER_SIZE;
1106 gfc->bs.buf_byte_idx = -1;
1107 gfc->bs.buf_bit_idx = 0;
1111 /* end of bitstream.c */