2 * MP3 bitstream Output interface for LAME
4 * Copyright (c) 1999 Takehiro TOMINAGA
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 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 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
21 * $Id: bitstream.c,v 1.2 2002/05/19 18:09:36 kramm Exp $
25 #include "config_static.h"
31 #include "bitstream.h"
33 #include "quantize_pvt.h"
41 /* This is the scfsi_band table from 2.4.2.7 of the IS */
42 const int scfsi_band[5] = { 0, 6, 11, 16, 21 };
45 /* unsigned int is at least this large: */
46 /* we work with ints, so when doing bit manipulation, we limit
47 * ourselves to MAX_LENGTH-2 just to be on the safe side */
53 static int hoge, hogege;
60 void putheader_bits(lame_internal_flags *gfc,int w_ptr)
65 hoge += gfc->sideinfo_len * 8;
66 hogege += gfc->sideinfo_len * 8;
68 memcpy(&bs->buf[bs->buf_byte_idx], gfc->header[gfc->w_ptr].buf,
70 bs->buf_byte_idx += gfc->sideinfo_len;
71 bs->totbit += gfc->sideinfo_len * 8;
72 gfc->w_ptr = (gfc->w_ptr + 1) & (MAX_HEADER_BUF - 1);
78 /*write j bits into the bit stream */
80 putbits2(lame_global_flags *gfp, int val, int j)
82 lame_internal_flags *gfc=gfp->internal_flags;
86 assert(j < MAX_LENGTH-2);
90 if (bs->buf_bit_idx == 0) {
93 assert(bs->buf_byte_idx < BUFFER_SIZE);
94 assert(gfc->header[gfc->w_ptr].write_timing >= bs->totbit);
95 if (gfc->header[gfc->w_ptr].write_timing == bs->totbit) {
96 putheader_bits(gfc,gfc->w_ptr);
98 bs->buf[bs->buf_byte_idx] = 0;
101 k = Min(j, bs->buf_bit_idx);
104 bs->buf_bit_idx -= k;
106 assert (j < MAX_LENGTH); /* 32 too large on 32 bit machines */
107 assert (bs->buf_bit_idx < MAX_LENGTH);
109 bs->buf[bs->buf_byte_idx] |= ((val >> j) << bs->buf_bit_idx);
114 /*write j bits into the bit stream, ignoring frame headers */
116 putbits_noheaders(lame_global_flags *gfp, int val, int j)
118 lame_internal_flags *gfc=gfp->internal_flags;
119 Bit_stream_struc *bs;
122 assert(j < MAX_LENGTH-2);
126 if (bs->buf_bit_idx == 0) {
129 assert(bs->buf_byte_idx < BUFFER_SIZE);
130 bs->buf[bs->buf_byte_idx] = 0;
133 k = Min(j, bs->buf_bit_idx);
136 bs->buf_bit_idx -= k;
138 assert (j < MAX_LENGTH); /* 32 too large on 32 bit machines */
139 assert (bs->buf_bit_idx < MAX_LENGTH);
141 bs->buf[bs->buf_byte_idx] |= ((val >> j) << bs->buf_bit_idx);
148 Some combinations of bitrate, Fs, and stereo make it impossible to stuff
149 out a frame using just main_data, due to the limited number of bits to
150 indicate main_data_length. In these situations, we put stuffing bits into
151 the ancillary data...
155 drain_into_ancillary(lame_global_flags *gfp,int remainingBits)
157 lame_internal_flags *gfc=gfp->internal_flags;
159 assert(remainingBits >= 0);
161 if (remainingBits >= 8) {
162 putbits2(gfp,0x4c,8);
165 if (remainingBits >= 8) {
166 putbits2(gfp,0x41,8);
169 if (remainingBits >= 8) {
170 putbits2(gfp,0x4d,8);
173 if (remainingBits >= 8) {
174 putbits2(gfp,0x45,8);
178 if (remainingBits >= 32) {
179 const char *version = get_lame_short_version ();
180 if (remainingBits >= 32)
181 for (i=0; i<(int)strlen(version) && remainingBits >=8 ; ++i) {
183 putbits2(gfp,version[i],8);
187 for (; remainingBits >= 1; remainingBits -= 1 ) {
188 putbits2 ( gfp, gfc->ancillary_flag, 1 );
189 gfc->ancillary_flag ^= 1;
192 assert (remainingBits == 0);
196 /*write N bits into the header */
198 writeheader(lame_internal_flags *gfc,int val, int j)
200 int ptr = gfc->header[gfc->h_ptr].ptr;
203 int k = Min(j, 8 - (ptr & 7));
205 assert (j < MAX_LENGTH); /* >> 32 too large for 32 bit machines */
206 gfc->header[gfc->h_ptr].buf[ptr >> 3]
207 |= ((val >> j)) << (8 - (ptr & 7) - k);
210 gfc->header[gfc->h_ptr].ptr = ptr;
215 CRC_update(int value, int crc)
219 for (i = 0; i < 8; i++) {
223 if (((crc ^ value) & 0x10000))
224 crc ^= CRC16_POLYNOMIAL;
231 CRC_writeheader(lame_internal_flags *gfc, char *header)
233 int crc = 0xffff; /* (jo) init crc16 for error_protection */
236 crc = CRC_update(((unsigned char*)header)[2], crc);
237 crc = CRC_update(((unsigned char*)header)[3], crc);
238 for (i = 6; i < gfc->sideinfo_len; i++) {
239 crc = CRC_update(((unsigned char*)header)[i], crc);
242 header[4] = crc >> 8;
243 header[5] = crc & 255;
247 encodeSideInfo2(lame_global_flags *gfp,int bitsPerFrame)
249 lame_internal_flags *gfc=gfp->internal_flags;
250 III_side_info_t *l3_side;
253 l3_side = &gfc->l3_side;
254 gfc->header[gfc->h_ptr].ptr = 0;
255 memset(gfc->header[gfc->h_ptr].buf, 0, gfc->sideinfo_len);
256 if (gfp->out_samplerate < 16000)
257 writeheader(gfc,0xffe, 12);
259 writeheader(gfc,0xfff, 12);
260 writeheader(gfc,(gfp->version), 1);
261 writeheader(gfc,4 - 3, 2);
262 writeheader(gfc,(!gfp->error_protection), 1);
263 writeheader(gfc,(gfc->bitrate_index), 4);
264 writeheader(gfc,(gfc->samplerate_index), 2);
265 writeheader(gfc,(gfc->padding), 1);
266 writeheader(gfc,(gfp->extension), 1);
267 writeheader(gfc,(gfp->mode), 2);
268 writeheader(gfc,(gfc->mode_ext), 2);
269 writeheader(gfc,(gfp->copyright), 1);
270 writeheader(gfc,(gfp->original), 1);
271 writeheader(gfc,(gfp->emphasis), 2);
272 if (gfp->error_protection) {
273 writeheader(gfc,0, 16); /* dummy */
276 if (gfp->version == 1) {
278 assert(l3_side->main_data_begin >= 0);
279 writeheader(gfc,(l3_side->main_data_begin), 9);
281 if (gfc->channels_out == 2)
282 writeheader(gfc,l3_side->private_bits, 3);
284 writeheader(gfc,l3_side->private_bits, 5);
286 for (ch = 0; ch < gfc->channels_out; ch++) {
288 for (band = 0; band < 4; band++) {
289 writeheader(gfc,l3_side->scfsi[ch][band], 1);
293 for (gr = 0; gr < 2; gr++) {
294 for (ch = 0; ch < gfc->channels_out; ch++) {
295 gr_info *gi = &l3_side->gr[gr].ch[ch].tt;
296 writeheader(gfc,gi->part2_3_length, 12);
297 writeheader(gfc,gi->big_values / 2, 9);
298 writeheader(gfc,gi->global_gain, 8);
299 writeheader(gfc,gi->scalefac_compress, 4);
300 writeheader(gfc,gi->window_switching_flag, 1);
302 if (gi->window_switching_flag) {
303 writeheader(gfc,gi->block_type, 2);
304 writeheader(gfc,gi->mixed_block_flag, 1);
306 if (gi->table_select[0] == 14)
307 gi->table_select[0] = 16;
308 writeheader(gfc,gi->table_select[0], 5);
309 if (gi->table_select[1] == 14)
310 gi->table_select[1] = 16;
311 writeheader(gfc,gi->table_select[1], 5);
313 writeheader(gfc,gi->subblock_gain[0], 3);
314 writeheader(gfc,gi->subblock_gain[1], 3);
315 writeheader(gfc,gi->subblock_gain[2], 3);
317 assert(gi->block_type == NORM_TYPE);
318 if (gi->table_select[0] == 14)
319 gi->table_select[0] = 16;
320 writeheader(gfc,gi->table_select[0], 5);
321 if (gi->table_select[1] == 14)
322 gi->table_select[1] = 16;
323 writeheader(gfc,gi->table_select[1], 5);
324 if (gi->table_select[2] == 14)
325 gi->table_select[2] = 16;
326 writeheader(gfc,gi->table_select[2], 5);
328 assert(gi->region0_count < 16U);
329 assert(gi->region1_count < 8U);
330 writeheader(gfc,gi->region0_count, 4);
331 writeheader(gfc,gi->region1_count, 3);
333 writeheader(gfc,gi->preflag, 1);
334 writeheader(gfc,gi->scalefac_scale, 1);
335 writeheader(gfc,gi->count1table_select, 1);
340 assert(l3_side->main_data_begin >= 0);
341 writeheader(gfc,(l3_side->main_data_begin), 8);
342 writeheader(gfc,l3_side->private_bits, gfc->channels_out);
345 for (ch = 0; ch < gfc->channels_out; ch++) {
346 gr_info *gi = &l3_side->gr[gr].ch[ch].tt;
347 writeheader(gfc,gi->part2_3_length, 12);
348 writeheader(gfc,gi->big_values / 2, 9);
349 writeheader(gfc,gi->global_gain, 8);
350 writeheader(gfc,gi->scalefac_compress, 9);
351 writeheader(gfc,gi->window_switching_flag, 1);
353 if (gi->window_switching_flag) {
354 writeheader(gfc,gi->block_type, 2);
355 writeheader(gfc,gi->mixed_block_flag, 1);
357 if (gi->table_select[0] == 14)
358 gi->table_select[0] = 16;
359 writeheader(gfc,gi->table_select[0], 5);
360 if (gi->table_select[1] == 14)
361 gi->table_select[1] = 16;
362 writeheader(gfc,gi->table_select[1], 5);
364 writeheader(gfc,gi->subblock_gain[0], 3);
365 writeheader(gfc,gi->subblock_gain[1], 3);
366 writeheader(gfc,gi->subblock_gain[2], 3);
368 if (gi->table_select[0] == 14)
369 gi->table_select[0] = 16;
370 writeheader(gfc,gi->table_select[0], 5);
371 if (gi->table_select[1] == 14)
372 gi->table_select[1] = 16;
373 writeheader(gfc,gi->table_select[1], 5);
374 if (gi->table_select[2] == 14)
375 gi->table_select[2] = 16;
376 writeheader(gfc,gi->table_select[2], 5);
378 assert(gi->region0_count < 16U);
379 assert(gi->region1_count < 8U);
380 writeheader(gfc,gi->region0_count, 4);
381 writeheader(gfc,gi->region1_count, 3);
384 writeheader(gfc,gi->scalefac_scale, 1);
385 writeheader(gfc,gi->count1table_select, 1);
389 if (gfp->error_protection) {
390 /* (jo) error_protection: add crc16 information to header */
391 CRC_writeheader(gfc, gfc->header[gfc->h_ptr].buf);
395 int old = gfc->h_ptr;
396 assert(gfc->header[old].ptr == gfc->sideinfo_len * 8);
398 gfc->h_ptr = (old + 1) & (MAX_HEADER_BUF - 1);
399 gfc->header[gfc->h_ptr].write_timing =
400 gfc->header[old].write_timing + bitsPerFrame;
402 if (gfc->h_ptr == gfc->w_ptr) {
403 /* yikes! we are out of header buffer space */
404 ERRORF(gfc,"Error: MAX_HEADER_BUF too small in bitstream.c \n");
412 huffman_coder_count1(lame_global_flags *gfp,int *ix, gr_info *gi)
415 lame_internal_flags *gfc = gfp->internal_flags;
417 /* Write count1 area */
418 const struct huffcodetab *h = &ht[gi->count1table_select + 32];
421 int gegebo = gfc->bs.totbit;
424 ix += gi->big_values;
425 assert(gi->count1table_select < 2);
428 for (i = (gi->count1 - gi->big_values) / 4; i > 0; --i) {
437 assert(-1 <= v && v <= 1);
446 assert(-1 <= v && v <= 1);
455 assert(-1 <= v && v <= 1);
464 assert(-1 <= v && v <= 1);
468 putbits2(gfp,huffbits + h->table[p], h->hlen[p]);
472 DEBUGF(gfc,"%ld %d %d %d\n",gfc->bs.totbit -gegebo, gi->count1bits, gi->big_values, gi->count1);
480 Implements the pseudocode of page 98 of the IS
484 HuffmanCode ( lame_global_flags* const gfp, const int table_select, int x1, int x2 )
486 const struct huffcodetab* h = ht + table_select;
492 int linbits = h->xlen;
496 assert ( table_select > 0 );
510 if (table_select > 15) {
513 int linbits_x1 = x1 - 15;
514 assert ( linbits_x1 <= h->linmax );
515 ext |= linbits_x1 << 1;
521 int linbits_x2 = x2 - 15;
522 assert ( linbits_x2 <= h->linmax );
543 assert ( (x1|x2) < 16u );
547 code = h->table [x1];
548 cbits += h->hlen [x1];
550 assert ( cbits <= MAX_LENGTH );
551 assert ( xbits <= MAX_LENGTH );
553 putbits2 ( gfp, code, cbits );
554 putbits2 ( gfp, ext, xbits );
556 return cbits + xbits;
560 Huffmancodebits(lame_global_flags *gfp, int tableindex, int start, int end, int *ix)
564 assert(tableindex < 32);
565 if (!tableindex) return 0;
568 for (i = start; i < end; i += 2) {
569 bits +=HuffmanCode(gfp,tableindex, ix[i], ix[i + 1]);
577 Note the discussion of huffmancodebits() on pages 28
578 and 29 of the IS, as well as the definitions of the side
579 information on pages 26 and 27.
582 ShortHuffmancodebits(lame_global_flags *gfp,int *ix, gr_info *gi)
584 lame_internal_flags *gfc=gfp->internal_flags;
588 region1Start = 3*gfc->scalefac_band.s[3];
589 if (region1Start > gi->big_values)
590 region1Start = gi->big_values;
592 /* short blocks do not have a region2 */
593 bits = Huffmancodebits(gfp,gi->table_select[0], 0, region1Start, ix);
594 bits += Huffmancodebits(gfp,gi->table_select[1], region1Start, gi->big_values, ix);
599 LongHuffmancodebits(lame_global_flags *gfp,int *ix, gr_info *gi)
601 lame_internal_flags *gfc=gfp->internal_flags;
602 int i, bigvalues,bits=0;
603 int region1Start, region2Start;
605 bigvalues = gi->big_values;
606 assert(0 <= bigvalues && bigvalues <= 576);
608 i = gi->region0_count + 1;
610 region1Start = gfc->scalefac_band.l[i];
611 i += gi->region1_count + 1;
613 region2Start = gfc->scalefac_band.l[i];
615 if (region1Start > bigvalues)
616 region1Start = bigvalues;
618 if (region2Start > bigvalues)
619 region2Start = bigvalues;
621 bits +=Huffmancodebits(gfp,gi->table_select[0], 0, region1Start, ix);
622 bits +=Huffmancodebits(gfp,gi->table_select[1], region1Start, region2Start, ix);
623 bits +=Huffmancodebits(gfp,gi->table_select[2], region2Start, bigvalues, ix);
628 writeMainData ( lame_global_flags * const gfp,
629 int l3_enc [2] [2] [576],
630 III_scalefac_t scalefac [2] [2] )
632 int gr, ch, sfb,data_bits,scale_bits,tot_bits=0;
633 lame_internal_flags *gfc=gfp->internal_flags;
634 III_side_info_t *l3_side;
636 l3_side = &gfc->l3_side;
637 if (gfp->version == 1) {
639 for (gr = 0; gr < 2; gr++) {
640 for (ch = 0; ch < gfc->channels_out; ch++) {
641 gr_info *gi = &l3_side->gr[gr].ch[ch].tt;
642 int slen1 = slen1_tab[gi->scalefac_compress];
643 int slen2 = slen2_tab[gi->scalefac_compress];
648 hogege = gfc->bs.totbit;
650 if (gi->block_type == SHORT_TYPE) {
651 for (sfb = 0; sfb < SBPSY_s; sfb++) {
652 int slen = sfb < 6 ? slen1 : slen2;
654 assert(scalefac[gr][ch].s[sfb][0]>=0);
655 assert(scalefac[gr][ch].s[sfb][1]>=0);
656 assert(scalefac[gr][ch].s[sfb][2]>=0);
658 putbits2(gfp,scalefac[gr][ch].s[sfb][0], slen);
659 putbits2(gfp,scalefac[gr][ch].s[sfb][1], slen);
660 putbits2(gfp,scalefac[gr][ch].s[sfb][2], slen);
661 scale_bits += 3*slen;
663 data_bits += ShortHuffmancodebits(gfp,l3_enc[gr][ch], gi);
666 for (i = 0; i < sizeof(scfsi_band) / sizeof(int) - 1;
668 if (gr != 0 && l3_side->scfsi[ch][i])
671 for (sfb = scfsi_band[i]; sfb < scfsi_band[i + 1];
674 assert(scalefac[gr][ch].l[sfb]>=0);
675 putbits2(gfp,scalefac[gr][ch].l[sfb],
676 sfb < 11 ? slen1 : slen2);
677 scale_bits += sfb < 11 ? slen1 : slen2;
680 data_bits +=LongHuffmancodebits(gfp,l3_enc[gr][ch], gi);
682 data_bits +=huffman_coder_count1(gfp,l3_enc[gr][ch], gi);
684 DEBUGF(gfc,"<%ld> ", gfc->bs.totbit-hogege);
686 /* does bitcount in quantize.c agree with actual bit count?*/
687 assert(data_bits==gi->part2_3_length-gi->part2_length);
688 assert(scale_bits==gi->part2_length);
689 tot_bits += scale_bits + data_bits;
696 for (ch = 0; ch < gfc->channels_out; ch++) {
697 gr_info *gi = &l3_side->gr[gr].ch[ch].tt;
698 int i, sfb_partition;
699 assert(gi->sfb_partition_table);
705 if (gi->block_type == SHORT_TYPE) {
706 for (; sfb_partition < 4; sfb_partition++) {
707 int sfbs = gi->sfb_partition_table[sfb_partition] / 3;
708 int slen = gi->slen[sfb_partition];
709 for (i = 0; i < sfbs; i++, sfb++) {
710 putbits2(gfp,Max(scalefac[gr][ch].s[sfb][0], 0U), slen);
711 putbits2(gfp,Max(scalefac[gr][ch].s[sfb][1], 0U), slen);
712 putbits2(gfp,Max(scalefac[gr][ch].s[sfb][2], 0U), slen);
713 scale_bits += 3*slen;
716 data_bits += ShortHuffmancodebits(gfp,l3_enc[gr][ch], gi);
718 for (; sfb_partition < 4; sfb_partition++) {
719 int sfbs = gi->sfb_partition_table[sfb_partition];
720 int slen = gi->slen[sfb_partition];
721 for (i = 0; i < sfbs; i++, sfb++) {
722 putbits2(gfp,Max(scalefac[gr][ch].l[sfb], 0U), slen);
726 data_bits +=LongHuffmancodebits(gfp,l3_enc[gr][ch], gi);
728 data_bits +=huffman_coder_count1(gfp,l3_enc[gr][ch], gi);
730 /* does bitcount in quantize.c agree with actual bit count?*/
731 assert(data_bits==gi->part2_3_length-gi->part2_length);
732 assert(scale_bits==gi->part2_length);
733 tot_bits += scale_bits + data_bits;
741 /* compute the number of bits required to flush all mp3 frames
742 currently in the buffer. This should be the same as the
743 reservoir size. Only call this routine between frames - i.e.
744 only after all headers and data have been added to the buffer
745 by format_bitstream().
747 Also compute total_bits_output =
748 size of mp3 buffer (including frame headers which may not
749 have yet been send to the mp3 buffer) +
750 number of bits needed to flush all mp3 frames.
752 total_bytes_output is the size of the mp3 output buffer if
753 lame_encode_flush_nogap() was called right now.
757 compute_flushbits( const lame_global_flags * gfp, int *total_bytes_output )
759 lame_internal_flags *gfc=gfp->internal_flags;
760 int flushbits,remaining_headers;
761 int bitsPerFrame, mean_bits;
762 int last_ptr,first_ptr;
763 first_ptr=gfc->w_ptr; /* first header to add to bitstream */
764 last_ptr = gfc->h_ptr - 1; /* last header to add to bitstream */
765 if (last_ptr==-1) last_ptr=MAX_HEADER_BUF-1;
767 /* add this many bits to bitstream so we can flush all headers */
768 flushbits = gfc->header[last_ptr].write_timing - gfc->bs.totbit;
769 *total_bytes_output=flushbits;
771 if (flushbits >= 0) {
772 /* if flushbits >= 0, some headers have not yet been written */
773 /* reduce flushbits by the size of the headers */
774 remaining_headers= 1+last_ptr - first_ptr;
775 if (last_ptr < first_ptr)
776 remaining_headers= 1+last_ptr - first_ptr + MAX_HEADER_BUF;
777 flushbits -= remaining_headers*8*gfc->sideinfo_len;
781 /* finally, add some bits so that the last frame is complete
782 * these bits are not necessary to decode the last frame, but
783 * some decoders will ignore last frame if these bits are missing
785 getframebits(gfp,&bitsPerFrame,&mean_bits);
786 flushbits += bitsPerFrame;
787 *total_bytes_output += bitsPerFrame;
789 if (*total_bytes_output % 8)
790 *total_bytes_output = 1 + (*total_bytes_output/8);
792 *total_bytes_output = (*total_bytes_output/8);
793 *total_bytes_output += gfc->bs.buf_byte_idx + 1;
798 /* if flushbits < 0, this would mean that the buffer looks like:
799 * (data...) last_header (data...) (extra data that should not be here...)
801 DEBUGF(gfc,"last header write_timing = %i \n",gfc->header[last_ptr].write_timing);
802 DEBUGF(gfc,"first header write_timing = %i \n",gfc->header[first_ptr].write_timing);
803 DEBUGF(gfc,"bs.totbit: %i \n",gfc->bs.totbit);
804 DEBUGF(gfc,"first_ptr, last_ptr %i %i \n",first_ptr,last_ptr);
805 DEBUGF(gfc,"remaining_headers = %i \n",remaining_headers);
806 DEBUGF(gfc,"bitsperframe: %i \n",bitsPerFrame);
807 DEBUGF(gfc,"sidelen: %i \n",gfc->sideinfo_len);
809 // ERRORF(gfc,"strange error flushing buffer ... \n");
817 flush_bitstream(lame_global_flags *gfp)
819 lame_internal_flags *gfc=gfp->internal_flags;
820 III_side_info_t *l3_side;
823 int bitsPerFrame, mean_bits;
824 int last_ptr,first_ptr;
825 first_ptr=gfc->w_ptr; /* first header to add to bitstream */
826 last_ptr = gfc->h_ptr - 1; /* last header to add to bitstream */
827 if (last_ptr==-1) last_ptr=MAX_HEADER_BUF-1;
828 l3_side = &gfc->l3_side;
831 if ((flushbits = compute_flushbits(gfp,&nbytes)) < 0) return;
832 drain_into_ancillary(gfp,flushbits);
834 /* check that the 100% of the last frame has been written to bitstream */
835 getframebits(gfp,&bitsPerFrame,&mean_bits);
836 assert (gfc->header[last_ptr].write_timing + bitsPerFrame == gfc->bs.totbit);
838 /* we have padded out all frames with ancillary data, which is the
839 same as filling the bitreservoir with ancillary data, so : */
841 l3_side->main_data_begin = 0;
848 void add_dummy_byte ( lame_global_flags* const gfp, unsigned char val )
850 lame_internal_flags *gfc = gfp->internal_flags;
853 putbits_noheaders(gfp,val,8);
855 for (i=0 ; i< MAX_HEADER_BUF ; ++i)
856 gfc->header[i].write_timing += 8;
863 This is called after a frame of audio has been quantized and coded.
864 It will write the encoded audio to the bitstream. Note that
865 from a layer3 encoder's perspective the bit stream is primarily
866 a series of main_data() blocks, with header and side information
867 inserted at the proper locations to maintain framing. (See Figure A.7
871 format_bitstream(lame_global_flags *gfp, int bitsPerFrame,
872 int l3_enc[2][2][576],
873 III_scalefac_t scalefac[2][2] )
875 lame_internal_flags *gfc=gfp->internal_flags;
877 III_side_info_t *l3_side;
878 l3_side = &gfc->l3_side;
880 drain_into_ancillary(gfp,l3_side->resvDrain_pre);
882 encodeSideInfo2(gfp,bitsPerFrame);
883 bits = 8*gfc->sideinfo_len;
884 bits+=writeMainData(gfp,l3_enc,scalefac);
885 drain_into_ancillary(gfp,l3_side->resvDrain_post);
886 bits += l3_side->resvDrain_post;
888 l3_side->main_data_begin += (bitsPerFrame-bits)/8;
890 /* compare number of bits needed to clear all buffered mp3 frames
891 * with what we think the resvsize is: */
892 if (compute_flushbits(gfp,&nbytes) != gfc->ResvSize) {
893 ERRORF(gfc,"Internal buffer inconsistency. flushbits <> ResvSize");
897 /* compare main_data_begin for the next frame with what we
898 * think the resvsize is: */
899 if ((l3_side->main_data_begin * 8) != gfc->ResvSize ) {
900 ERRORF(gfc,"bit reservoir error: \n"
901 "l3_side->main_data_begin: %i \n"
902 "Resvoir size: %i \n"
903 "resv drain (post) %i \n"
904 "resv drain (pre) %i \n"
905 "header and sideinfo: %i \n"
907 "total bits: %i (remainder: %i) \n"
908 "bitsperframe: %i \n",
910 8*l3_side->main_data_begin,
912 l3_side->resvDrain_post,
913 l3_side->resvDrain_pre,
915 bits-l3_side->resvDrain_post-8*gfc->sideinfo_len,
920 gfc->ResvSize = l3_side->main_data_begin*8;
922 assert(gfc->bs.totbit % 8 == 0);
924 if (gfc->bs.totbit > 1000000000 ) {
925 /* to avoid totbit overflow, (at 8h encoding at 128kbs) lets reset bit counter*/
927 for (i=0 ; i< MAX_HEADER_BUF ; ++i)
928 gfc->header[i].write_timing -= gfc->bs.totbit;
940 /* copy data out of the internal MP3 bit buffer into a user supplied
941 unsigned char buffer.
943 mp3data=0 indicates data in buffer is an id3tags and VBR tags
944 mp3data=1 data is real mp3 frame data.
948 int copy_buffer(lame_internal_flags *gfc,unsigned char *buffer,int size,int mp3data)
950 Bit_stream_struc *bs=&gfc->bs;
951 int minimum = bs->buf_byte_idx + 1;
952 if (minimum <= 0) return 0;
953 if (size!=0 && minimum>size) return -1; /* buffer is too small */
954 memcpy(buffer,bs->buf,minimum);
955 bs->buf_byte_idx = -1;
959 UpdateMusicCRC(&gfc->nMusicCRC,buffer,minimum);
965 void init_bit_stream_w(lame_internal_flags *gfc)
967 gfc->bs.buf = (unsigned char *) malloc(BUFFER_SIZE);
968 gfc->bs.buf_size = BUFFER_SIZE;
970 gfc->h_ptr = gfc->w_ptr = 0;
971 gfc->header[gfc->h_ptr].write_timing = 0;
972 gfc->bs.buf_byte_idx = -1;
973 gfc->bs.buf_bit_idx = 0;
977 /* end of bitstream.c */