1159b3361Sopenharmony_ci/*
2159b3361Sopenharmony_ci *      MP3 bitstream Output interface for LAME
3159b3361Sopenharmony_ci *
4159b3361Sopenharmony_ci *      Copyright (c) 1999-2000 Mark Taylor
5159b3361Sopenharmony_ci *      Copyright (c) 1999-2002 Takehiro Tominaga
6159b3361Sopenharmony_ci *
7159b3361Sopenharmony_ci * This library is free software; you can redistribute it and/or
8159b3361Sopenharmony_ci * modify it under the terms of the GNU Library General Public
9159b3361Sopenharmony_ci * License as published by the Free Software Foundation; either
10159b3361Sopenharmony_ci * version 2 of the License, or (at your option) any later version.
11159b3361Sopenharmony_ci *
12159b3361Sopenharmony_ci * This library is distributed in the hope that it will be useful,
13159b3361Sopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of
14159b3361Sopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15159b3361Sopenharmony_ci * Library General Public License for more details.
16159b3361Sopenharmony_ci *
17159b3361Sopenharmony_ci * You should have received a copy of the GNU Library General Public
18159b3361Sopenharmony_ci * License along with this library; if not, write to the
19159b3361Sopenharmony_ci * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20159b3361Sopenharmony_ci * Boston, MA 02111-1307, USA.
21159b3361Sopenharmony_ci *
22159b3361Sopenharmony_ci * $Id$
23159b3361Sopenharmony_ci */
24159b3361Sopenharmony_ci
25159b3361Sopenharmony_ci
26159b3361Sopenharmony_ci#ifdef HAVE_CONFIG_H
27159b3361Sopenharmony_ci#include <config.h>
28159b3361Sopenharmony_ci#endif
29159b3361Sopenharmony_ci
30159b3361Sopenharmony_ci#include <stdlib.h>
31159b3361Sopenharmony_ci#include <stdio.h>
32159b3361Sopenharmony_ci
33159b3361Sopenharmony_ci#include "lame.h"
34159b3361Sopenharmony_ci#include "machine.h"
35159b3361Sopenharmony_ci#include "encoder.h"
36159b3361Sopenharmony_ci#include "util.h"
37159b3361Sopenharmony_ci#include "tables.h"
38159b3361Sopenharmony_ci#include "quantize_pvt.h"
39159b3361Sopenharmony_ci#include "lame_global_flags.h"
40159b3361Sopenharmony_ci#include "gain_analysis.h"
41159b3361Sopenharmony_ci#include "VbrTag.h"
42159b3361Sopenharmony_ci#include "bitstream.h"
43159b3361Sopenharmony_ci#include "tables.h"
44159b3361Sopenharmony_ci
45159b3361Sopenharmony_ci
46159b3361Sopenharmony_ci
47159b3361Sopenharmony_ci/* unsigned int is at least this large:  */
48159b3361Sopenharmony_ci/* we work with ints, so when doing bit manipulation, we limit
49159b3361Sopenharmony_ci * ourselves to MAX_LENGTH-2 just to be on the safe side */
50159b3361Sopenharmony_ci#define MAX_LENGTH      32
51159b3361Sopenharmony_ci
52159b3361Sopenharmony_ci
53159b3361Sopenharmony_ci
54159b3361Sopenharmony_ci#ifdef DEBUG
55159b3361Sopenharmony_cistatic int hogege;
56159b3361Sopenharmony_ci#endif
57159b3361Sopenharmony_ci
58159b3361Sopenharmony_ci
59159b3361Sopenharmony_ci
60159b3361Sopenharmony_cistatic int
61159b3361Sopenharmony_cicalcFrameLength(SessionConfig_t const *const cfg, int kbps, int pad)
62159b3361Sopenharmony_ci{
63159b3361Sopenharmony_ci  return 8 * ((cfg->version + 1) * 72000 * kbps / cfg->samplerate_out + pad);
64159b3361Sopenharmony_ci}
65159b3361Sopenharmony_ci
66159b3361Sopenharmony_ci
67159b3361Sopenharmony_ci/***********************************************************************
68159b3361Sopenharmony_ci * compute bitsperframe and mean_bits for a layer III frame
69159b3361Sopenharmony_ci **********************************************************************/
70159b3361Sopenharmony_ciint
71159b3361Sopenharmony_cigetframebits(const lame_internal_flags * gfc)
72159b3361Sopenharmony_ci{
73159b3361Sopenharmony_ci    SessionConfig_t const *const cfg = &gfc->cfg;
74159b3361Sopenharmony_ci    EncResult_t const *const eov = &gfc->ov_enc;
75159b3361Sopenharmony_ci    int     bit_rate;
76159b3361Sopenharmony_ci
77159b3361Sopenharmony_ci    /* get bitrate in kbps [?] */
78159b3361Sopenharmony_ci    if (eov->bitrate_index)
79159b3361Sopenharmony_ci        bit_rate = bitrate_table[cfg->version][eov->bitrate_index];
80159b3361Sopenharmony_ci    else
81159b3361Sopenharmony_ci        bit_rate = cfg->avg_bitrate;
82159b3361Sopenharmony_ci    /*assert(bit_rate <= 550); */
83159b3361Sopenharmony_ci    assert(8 <= bit_rate && bit_rate <= 640);
84159b3361Sopenharmony_ci
85159b3361Sopenharmony_ci    /* main encoding routine toggles padding on and off */
86159b3361Sopenharmony_ci    /* one Layer3 Slot consists of 8 bits */
87159b3361Sopenharmony_ci    return calcFrameLength(cfg, bit_rate, eov->padding);
88159b3361Sopenharmony_ci}
89159b3361Sopenharmony_ci
90159b3361Sopenharmony_ciint
91159b3361Sopenharmony_ciget_max_frame_buffer_size_by_constraint(SessionConfig_t const * cfg, int constraint)
92159b3361Sopenharmony_ci{
93159b3361Sopenharmony_ci    int     maxmp3buf = 0;
94159b3361Sopenharmony_ci    if (cfg->avg_bitrate > 320) {
95159b3361Sopenharmony_ci        /* in freeformat the buffer is constant */
96159b3361Sopenharmony_ci        if (constraint == MDB_STRICT_ISO) {
97159b3361Sopenharmony_ci            maxmp3buf = calcFrameLength(cfg, cfg->avg_bitrate, 0);
98159b3361Sopenharmony_ci        }
99159b3361Sopenharmony_ci        else {
100159b3361Sopenharmony_ci            /* maximum allowed bits per granule are 7680 */
101159b3361Sopenharmony_ci            maxmp3buf = 7680 * (cfg->version + 1);
102159b3361Sopenharmony_ci        }
103159b3361Sopenharmony_ci    }
104159b3361Sopenharmony_ci    else {
105159b3361Sopenharmony_ci        int     max_kbps;
106159b3361Sopenharmony_ci        if (cfg->samplerate_out < 16000) {
107159b3361Sopenharmony_ci            max_kbps = bitrate_table[cfg->version][8]; /* default: allow 64 kbps (MPEG-2.5) */
108159b3361Sopenharmony_ci        }
109159b3361Sopenharmony_ci        else {
110159b3361Sopenharmony_ci            max_kbps = bitrate_table[cfg->version][14];
111159b3361Sopenharmony_ci        }
112159b3361Sopenharmony_ci        switch (constraint)
113159b3361Sopenharmony_ci        {
114159b3361Sopenharmony_ci        default:
115159b3361Sopenharmony_ci        case MDB_DEFAULT:
116159b3361Sopenharmony_ci            /* Bouvigne suggests this more lax interpretation of the ISO doc instead of using 8*960. */
117159b3361Sopenharmony_ci            /* All mp3 decoders should have enough buffer to handle this value: size of a 320kbps 32kHz frame */
118159b3361Sopenharmony_ci            maxmp3buf = 8 * 1440;
119159b3361Sopenharmony_ci            break;
120159b3361Sopenharmony_ci        case MDB_STRICT_ISO:
121159b3361Sopenharmony_ci            maxmp3buf = calcFrameLength(cfg, max_kbps, 0);
122159b3361Sopenharmony_ci            break;
123159b3361Sopenharmony_ci        case MDB_MAXIMUM:
124159b3361Sopenharmony_ci            maxmp3buf = 7680 * (cfg->version + 1);
125159b3361Sopenharmony_ci            break;
126159b3361Sopenharmony_ci        }
127159b3361Sopenharmony_ci    }
128159b3361Sopenharmony_ci    return maxmp3buf;
129159b3361Sopenharmony_ci}
130159b3361Sopenharmony_ci
131159b3361Sopenharmony_ci
132159b3361Sopenharmony_cistatic void
133159b3361Sopenharmony_ciputheader_bits(lame_internal_flags * gfc)
134159b3361Sopenharmony_ci{
135159b3361Sopenharmony_ci    SessionConfig_t const *const cfg = &gfc->cfg;
136159b3361Sopenharmony_ci    EncStateVar_t *const esv = &gfc->sv_enc;
137159b3361Sopenharmony_ci    Bit_stream_struc *bs = &gfc->bs;
138159b3361Sopenharmony_ci#ifdef DEBUG
139159b3361Sopenharmony_ci    hogege += cfg->sideinfo_len * 8;
140159b3361Sopenharmony_ci#endif
141159b3361Sopenharmony_ci    memcpy(&bs->buf[bs->buf_byte_idx], esv->header[esv->w_ptr].buf, cfg->sideinfo_len);
142159b3361Sopenharmony_ci    bs->buf_byte_idx += cfg->sideinfo_len;
143159b3361Sopenharmony_ci    bs->totbit += cfg->sideinfo_len * 8;
144159b3361Sopenharmony_ci    esv->w_ptr = (esv->w_ptr + 1) & (MAX_HEADER_BUF - 1);
145159b3361Sopenharmony_ci}
146159b3361Sopenharmony_ci
147159b3361Sopenharmony_ci
148159b3361Sopenharmony_ci
149159b3361Sopenharmony_ci
150159b3361Sopenharmony_ci/*write j bits into the bit stream */
151159b3361Sopenharmony_ciinline static void
152159b3361Sopenharmony_ciputbits2(lame_internal_flags * gfc, int val, int j)
153159b3361Sopenharmony_ci{
154159b3361Sopenharmony_ci    EncStateVar_t const *const esv = &gfc->sv_enc;
155159b3361Sopenharmony_ci    Bit_stream_struc *bs;
156159b3361Sopenharmony_ci    bs = &gfc->bs;
157159b3361Sopenharmony_ci
158159b3361Sopenharmony_ci    assert(j < MAX_LENGTH - 2);
159159b3361Sopenharmony_ci
160159b3361Sopenharmony_ci    while (j > 0) {
161159b3361Sopenharmony_ci        int     k;
162159b3361Sopenharmony_ci        if (bs->buf_bit_idx == 0) {
163159b3361Sopenharmony_ci            bs->buf_bit_idx = 8;
164159b3361Sopenharmony_ci            bs->buf_byte_idx++;
165159b3361Sopenharmony_ci            assert(bs->buf_byte_idx < BUFFER_SIZE);
166159b3361Sopenharmony_ci            assert(esv->header[esv->w_ptr].write_timing >= bs->totbit);
167159b3361Sopenharmony_ci            if (esv->header[esv->w_ptr].write_timing == bs->totbit) {
168159b3361Sopenharmony_ci                putheader_bits(gfc);
169159b3361Sopenharmony_ci            }
170159b3361Sopenharmony_ci            bs->buf[bs->buf_byte_idx] = 0;
171159b3361Sopenharmony_ci        }
172159b3361Sopenharmony_ci
173159b3361Sopenharmony_ci        k = Min(j, bs->buf_bit_idx);
174159b3361Sopenharmony_ci        j -= k;
175159b3361Sopenharmony_ci
176159b3361Sopenharmony_ci        bs->buf_bit_idx -= k;
177159b3361Sopenharmony_ci
178159b3361Sopenharmony_ci        assert(j < MAX_LENGTH); /* 32 too large on 32 bit machines */
179159b3361Sopenharmony_ci        assert(bs->buf_bit_idx < MAX_LENGTH);
180159b3361Sopenharmony_ci
181159b3361Sopenharmony_ci        bs->buf[bs->buf_byte_idx] |= ((val >> j) << bs->buf_bit_idx);
182159b3361Sopenharmony_ci        bs->totbit += k;
183159b3361Sopenharmony_ci    }
184159b3361Sopenharmony_ci}
185159b3361Sopenharmony_ci
186159b3361Sopenharmony_ci/*write j bits into the bit stream, ignoring frame headers */
187159b3361Sopenharmony_ciinline static void
188159b3361Sopenharmony_ciputbits_noheaders(lame_internal_flags * gfc, int val, int j)
189159b3361Sopenharmony_ci{
190159b3361Sopenharmony_ci    Bit_stream_struc *bs;
191159b3361Sopenharmony_ci    bs = &gfc->bs;
192159b3361Sopenharmony_ci
193159b3361Sopenharmony_ci    assert(j < MAX_LENGTH - 2);
194159b3361Sopenharmony_ci
195159b3361Sopenharmony_ci    while (j > 0) {
196159b3361Sopenharmony_ci        int     k;
197159b3361Sopenharmony_ci        if (bs->buf_bit_idx == 0) {
198159b3361Sopenharmony_ci            bs->buf_bit_idx = 8;
199159b3361Sopenharmony_ci            bs->buf_byte_idx++;
200159b3361Sopenharmony_ci            assert(bs->buf_byte_idx < BUFFER_SIZE);
201159b3361Sopenharmony_ci            bs->buf[bs->buf_byte_idx] = 0;
202159b3361Sopenharmony_ci        }
203159b3361Sopenharmony_ci
204159b3361Sopenharmony_ci        k = Min(j, bs->buf_bit_idx);
205159b3361Sopenharmony_ci        j -= k;
206159b3361Sopenharmony_ci
207159b3361Sopenharmony_ci        bs->buf_bit_idx -= k;
208159b3361Sopenharmony_ci
209159b3361Sopenharmony_ci        assert(j < MAX_LENGTH); /* 32 too large on 32 bit machines */
210159b3361Sopenharmony_ci        assert(bs->buf_bit_idx < MAX_LENGTH);
211159b3361Sopenharmony_ci
212159b3361Sopenharmony_ci        bs->buf[bs->buf_byte_idx] |= ((val >> j) << bs->buf_bit_idx);
213159b3361Sopenharmony_ci        bs->totbit += k;
214159b3361Sopenharmony_ci    }
215159b3361Sopenharmony_ci}
216159b3361Sopenharmony_ci
217159b3361Sopenharmony_ci
218159b3361Sopenharmony_ci/*
219159b3361Sopenharmony_ci  Some combinations of bitrate, Fs, and stereo make it impossible to stuff
220159b3361Sopenharmony_ci  out a frame using just main_data, due to the limited number of bits to
221159b3361Sopenharmony_ci  indicate main_data_length. In these situations, we put stuffing bits into
222159b3361Sopenharmony_ci  the ancillary data...
223159b3361Sopenharmony_ci*/
224159b3361Sopenharmony_ci
225159b3361Sopenharmony_ciinline static void
226159b3361Sopenharmony_cidrain_into_ancillary(lame_internal_flags * gfc, int remainingBits)
227159b3361Sopenharmony_ci{
228159b3361Sopenharmony_ci    SessionConfig_t const *const cfg = &gfc->cfg;
229159b3361Sopenharmony_ci    EncStateVar_t *const esv = &gfc->sv_enc;
230159b3361Sopenharmony_ci    int     i;
231159b3361Sopenharmony_ci    assert(remainingBits >= 0);
232159b3361Sopenharmony_ci
233159b3361Sopenharmony_ci    if (remainingBits >= 8) {
234159b3361Sopenharmony_ci        putbits2(gfc, 0x4c, 8);
235159b3361Sopenharmony_ci        remainingBits -= 8;
236159b3361Sopenharmony_ci    }
237159b3361Sopenharmony_ci    if (remainingBits >= 8) {
238159b3361Sopenharmony_ci        putbits2(gfc, 0x41, 8);
239159b3361Sopenharmony_ci        remainingBits -= 8;
240159b3361Sopenharmony_ci    }
241159b3361Sopenharmony_ci    if (remainingBits >= 8) {
242159b3361Sopenharmony_ci        putbits2(gfc, 0x4d, 8);
243159b3361Sopenharmony_ci        remainingBits -= 8;
244159b3361Sopenharmony_ci    }
245159b3361Sopenharmony_ci    if (remainingBits >= 8) {
246159b3361Sopenharmony_ci        putbits2(gfc, 0x45, 8);
247159b3361Sopenharmony_ci        remainingBits -= 8;
248159b3361Sopenharmony_ci    }
249159b3361Sopenharmony_ci
250159b3361Sopenharmony_ci    if (remainingBits >= 32) {
251159b3361Sopenharmony_ci        const char *const version = get_lame_short_version();
252159b3361Sopenharmony_ci        if (remainingBits >= 32)
253159b3361Sopenharmony_ci            for (i = 0; i < (int) strlen(version) && remainingBits >= 8; ++i) {
254159b3361Sopenharmony_ci                remainingBits -= 8;
255159b3361Sopenharmony_ci                putbits2(gfc, version[i], 8);
256159b3361Sopenharmony_ci            }
257159b3361Sopenharmony_ci    }
258159b3361Sopenharmony_ci
259159b3361Sopenharmony_ci    for (; remainingBits >= 1; remainingBits -= 1) {
260159b3361Sopenharmony_ci        putbits2(gfc, esv->ancillary_flag, 1);
261159b3361Sopenharmony_ci        esv->ancillary_flag ^= !cfg->disable_reservoir;
262159b3361Sopenharmony_ci    }
263159b3361Sopenharmony_ci
264159b3361Sopenharmony_ci    assert(remainingBits == 0);
265159b3361Sopenharmony_ci
266159b3361Sopenharmony_ci}
267159b3361Sopenharmony_ci
268159b3361Sopenharmony_ci/*write N bits into the header */
269159b3361Sopenharmony_ciinline static void
270159b3361Sopenharmony_ciwriteheader(lame_internal_flags * gfc, int val, int j)
271159b3361Sopenharmony_ci{
272159b3361Sopenharmony_ci    EncStateVar_t *const esv = &gfc->sv_enc;
273159b3361Sopenharmony_ci    int     ptr = esv->header[esv->h_ptr].ptr;
274159b3361Sopenharmony_ci
275159b3361Sopenharmony_ci    while (j > 0) {
276159b3361Sopenharmony_ci        int const k = Min(j, 8 - (ptr & 7));
277159b3361Sopenharmony_ci        j -= k;
278159b3361Sopenharmony_ci        assert(j < MAX_LENGTH); /* >> 32  too large for 32 bit machines */
279159b3361Sopenharmony_ci        esv->header[esv->h_ptr].buf[ptr >> 3]
280159b3361Sopenharmony_ci            |= ((val >> j)) << (8 - (ptr & 7) - k);
281159b3361Sopenharmony_ci        ptr += k;
282159b3361Sopenharmony_ci    }
283159b3361Sopenharmony_ci    esv->header[esv->h_ptr].ptr = ptr;
284159b3361Sopenharmony_ci}
285159b3361Sopenharmony_ci
286159b3361Sopenharmony_ci
287159b3361Sopenharmony_cistatic int
288159b3361Sopenharmony_ciCRC_update(int value, int crc)
289159b3361Sopenharmony_ci{
290159b3361Sopenharmony_ci    int     i;
291159b3361Sopenharmony_ci    value <<= 8;
292159b3361Sopenharmony_ci    for (i = 0; i < 8; i++) {
293159b3361Sopenharmony_ci        value <<= 1;
294159b3361Sopenharmony_ci        crc <<= 1;
295159b3361Sopenharmony_ci
296159b3361Sopenharmony_ci        if (((crc ^ value) & 0x10000))
297159b3361Sopenharmony_ci            crc ^= CRC16_POLYNOMIAL;
298159b3361Sopenharmony_ci    }
299159b3361Sopenharmony_ci    return crc;
300159b3361Sopenharmony_ci}
301159b3361Sopenharmony_ci
302159b3361Sopenharmony_ci
303159b3361Sopenharmony_civoid
304159b3361Sopenharmony_ciCRC_writeheader(lame_internal_flags const *gfc, char *header)
305159b3361Sopenharmony_ci{
306159b3361Sopenharmony_ci    SessionConfig_t const *const cfg = &gfc->cfg;
307159b3361Sopenharmony_ci    int     crc = 0xffff;    /* (jo) init crc16 for error_protection */
308159b3361Sopenharmony_ci    int     i;
309159b3361Sopenharmony_ci
310159b3361Sopenharmony_ci    crc = CRC_update(((unsigned char *) header)[2], crc);
311159b3361Sopenharmony_ci    crc = CRC_update(((unsigned char *) header)[3], crc);
312159b3361Sopenharmony_ci    for (i = 6; i < cfg->sideinfo_len; i++) {
313159b3361Sopenharmony_ci        crc = CRC_update(((unsigned char *) header)[i], crc);
314159b3361Sopenharmony_ci    }
315159b3361Sopenharmony_ci
316159b3361Sopenharmony_ci    header[4] = crc >> 8;
317159b3361Sopenharmony_ci    header[5] = crc & 255;
318159b3361Sopenharmony_ci}
319159b3361Sopenharmony_ci
320159b3361Sopenharmony_ciinline static void
321159b3361Sopenharmony_ciencodeSideInfo2(lame_internal_flags * gfc, int bitsPerFrame)
322159b3361Sopenharmony_ci{
323159b3361Sopenharmony_ci    SessionConfig_t const *const cfg = &gfc->cfg;
324159b3361Sopenharmony_ci    EncResult_t const *const eov = &gfc->ov_enc;
325159b3361Sopenharmony_ci    EncStateVar_t *const esv = &gfc->sv_enc;
326159b3361Sopenharmony_ci    III_side_info_t *l3_side;
327159b3361Sopenharmony_ci    int     gr, ch;
328159b3361Sopenharmony_ci
329159b3361Sopenharmony_ci    l3_side = &gfc->l3_side;
330159b3361Sopenharmony_ci    esv->header[esv->h_ptr].ptr = 0;
331159b3361Sopenharmony_ci    memset(esv->header[esv->h_ptr].buf, 0, cfg->sideinfo_len);
332159b3361Sopenharmony_ci    if (cfg->samplerate_out < 16000)
333159b3361Sopenharmony_ci        writeheader(gfc, 0xffe, 12);
334159b3361Sopenharmony_ci    else
335159b3361Sopenharmony_ci        writeheader(gfc, 0xfff, 12);
336159b3361Sopenharmony_ci    writeheader(gfc, (cfg->version), 1);
337159b3361Sopenharmony_ci    writeheader(gfc, 4 - 3, 2);
338159b3361Sopenharmony_ci    writeheader(gfc, (!cfg->error_protection), 1);
339159b3361Sopenharmony_ci    writeheader(gfc, (eov->bitrate_index), 4);
340159b3361Sopenharmony_ci    writeheader(gfc, (cfg->samplerate_index), 2);
341159b3361Sopenharmony_ci    writeheader(gfc, (eov->padding), 1);
342159b3361Sopenharmony_ci    writeheader(gfc, (cfg->extension), 1);
343159b3361Sopenharmony_ci    writeheader(gfc, (cfg->mode), 2);
344159b3361Sopenharmony_ci    writeheader(gfc, (eov->mode_ext), 2);
345159b3361Sopenharmony_ci    writeheader(gfc, (cfg->copyright), 1);
346159b3361Sopenharmony_ci    writeheader(gfc, (cfg->original), 1);
347159b3361Sopenharmony_ci    writeheader(gfc, (cfg->emphasis), 2);
348159b3361Sopenharmony_ci    if (cfg->error_protection) {
349159b3361Sopenharmony_ci        writeheader(gfc, 0, 16); /* dummy */
350159b3361Sopenharmony_ci    }
351159b3361Sopenharmony_ci
352159b3361Sopenharmony_ci    if (cfg->version == 1) {
353159b3361Sopenharmony_ci        /* MPEG1 */
354159b3361Sopenharmony_ci        assert(l3_side->main_data_begin >= 0);
355159b3361Sopenharmony_ci        writeheader(gfc, (l3_side->main_data_begin), 9);
356159b3361Sopenharmony_ci
357159b3361Sopenharmony_ci        if (cfg->channels_out == 2)
358159b3361Sopenharmony_ci            writeheader(gfc, l3_side->private_bits, 3);
359159b3361Sopenharmony_ci        else
360159b3361Sopenharmony_ci            writeheader(gfc, l3_side->private_bits, 5);
361159b3361Sopenharmony_ci
362159b3361Sopenharmony_ci        for (ch = 0; ch < cfg->channels_out; ch++) {
363159b3361Sopenharmony_ci            int     band;
364159b3361Sopenharmony_ci            for (band = 0; band < 4; band++) {
365159b3361Sopenharmony_ci                writeheader(gfc, l3_side->scfsi[ch][band], 1);
366159b3361Sopenharmony_ci            }
367159b3361Sopenharmony_ci        }
368159b3361Sopenharmony_ci
369159b3361Sopenharmony_ci        for (gr = 0; gr < 2; gr++) {
370159b3361Sopenharmony_ci            for (ch = 0; ch < cfg->channels_out; ch++) {
371159b3361Sopenharmony_ci                gr_info *const gi = &l3_side->tt[gr][ch];
372159b3361Sopenharmony_ci                writeheader(gfc, gi->part2_3_length + gi->part2_length, 12);
373159b3361Sopenharmony_ci                writeheader(gfc, gi->big_values / 2, 9);
374159b3361Sopenharmony_ci                writeheader(gfc, gi->global_gain, 8);
375159b3361Sopenharmony_ci                writeheader(gfc, gi->scalefac_compress, 4);
376159b3361Sopenharmony_ci
377159b3361Sopenharmony_ci                if (gi->block_type != NORM_TYPE) {
378159b3361Sopenharmony_ci                    writeheader(gfc, 1, 1); /* window_switching_flag */
379159b3361Sopenharmony_ci                    writeheader(gfc, gi->block_type, 2);
380159b3361Sopenharmony_ci                    writeheader(gfc, gi->mixed_block_flag, 1);
381159b3361Sopenharmony_ci
382159b3361Sopenharmony_ci                    if (gi->table_select[0] == 14)
383159b3361Sopenharmony_ci                        gi->table_select[0] = 16;
384159b3361Sopenharmony_ci                    writeheader(gfc, gi->table_select[0], 5);
385159b3361Sopenharmony_ci                    if (gi->table_select[1] == 14)
386159b3361Sopenharmony_ci                        gi->table_select[1] = 16;
387159b3361Sopenharmony_ci                    writeheader(gfc, gi->table_select[1], 5);
388159b3361Sopenharmony_ci
389159b3361Sopenharmony_ci                    writeheader(gfc, gi->subblock_gain[0], 3);
390159b3361Sopenharmony_ci                    writeheader(gfc, gi->subblock_gain[1], 3);
391159b3361Sopenharmony_ci                    writeheader(gfc, gi->subblock_gain[2], 3);
392159b3361Sopenharmony_ci                }
393159b3361Sopenharmony_ci                else {
394159b3361Sopenharmony_ci                    writeheader(gfc, 0, 1); /* window_switching_flag */
395159b3361Sopenharmony_ci                    if (gi->table_select[0] == 14)
396159b3361Sopenharmony_ci                        gi->table_select[0] = 16;
397159b3361Sopenharmony_ci                    writeheader(gfc, gi->table_select[0], 5);
398159b3361Sopenharmony_ci                    if (gi->table_select[1] == 14)
399159b3361Sopenharmony_ci                        gi->table_select[1] = 16;
400159b3361Sopenharmony_ci                    writeheader(gfc, gi->table_select[1], 5);
401159b3361Sopenharmony_ci                    if (gi->table_select[2] == 14)
402159b3361Sopenharmony_ci                        gi->table_select[2] = 16;
403159b3361Sopenharmony_ci                    writeheader(gfc, gi->table_select[2], 5);
404159b3361Sopenharmony_ci
405159b3361Sopenharmony_ci                    assert(0 <= gi->region0_count && gi->region0_count < 16);
406159b3361Sopenharmony_ci                    assert(0 <= gi->region1_count && gi->region1_count < 8);
407159b3361Sopenharmony_ci                    writeheader(gfc, gi->region0_count, 4);
408159b3361Sopenharmony_ci                    writeheader(gfc, gi->region1_count, 3);
409159b3361Sopenharmony_ci                }
410159b3361Sopenharmony_ci                writeheader(gfc, gi->preflag, 1);
411159b3361Sopenharmony_ci                writeheader(gfc, gi->scalefac_scale, 1);
412159b3361Sopenharmony_ci                writeheader(gfc, gi->count1table_select, 1);
413159b3361Sopenharmony_ci            }
414159b3361Sopenharmony_ci        }
415159b3361Sopenharmony_ci    }
416159b3361Sopenharmony_ci    else {
417159b3361Sopenharmony_ci        /* MPEG2 */
418159b3361Sopenharmony_ci        assert(l3_side->main_data_begin >= 0);
419159b3361Sopenharmony_ci        writeheader(gfc, (l3_side->main_data_begin), 8);
420159b3361Sopenharmony_ci        writeheader(gfc, l3_side->private_bits, cfg->channels_out);
421159b3361Sopenharmony_ci
422159b3361Sopenharmony_ci        gr = 0;
423159b3361Sopenharmony_ci        for (ch = 0; ch < cfg->channels_out; ch++) {
424159b3361Sopenharmony_ci            gr_info *const gi = &l3_side->tt[gr][ch];
425159b3361Sopenharmony_ci            writeheader(gfc, gi->part2_3_length + gi->part2_length, 12);
426159b3361Sopenharmony_ci            writeheader(gfc, gi->big_values / 2, 9);
427159b3361Sopenharmony_ci            writeheader(gfc, gi->global_gain, 8);
428159b3361Sopenharmony_ci            writeheader(gfc, gi->scalefac_compress, 9);
429159b3361Sopenharmony_ci
430159b3361Sopenharmony_ci            if (gi->block_type != NORM_TYPE) {
431159b3361Sopenharmony_ci                writeheader(gfc, 1, 1); /* window_switching_flag */
432159b3361Sopenharmony_ci                writeheader(gfc, gi->block_type, 2);
433159b3361Sopenharmony_ci                writeheader(gfc, gi->mixed_block_flag, 1);
434159b3361Sopenharmony_ci
435159b3361Sopenharmony_ci                if (gi->table_select[0] == 14)
436159b3361Sopenharmony_ci                    gi->table_select[0] = 16;
437159b3361Sopenharmony_ci                writeheader(gfc, gi->table_select[0], 5);
438159b3361Sopenharmony_ci                if (gi->table_select[1] == 14)
439159b3361Sopenharmony_ci                    gi->table_select[1] = 16;
440159b3361Sopenharmony_ci                writeheader(gfc, gi->table_select[1], 5);
441159b3361Sopenharmony_ci
442159b3361Sopenharmony_ci                writeheader(gfc, gi->subblock_gain[0], 3);
443159b3361Sopenharmony_ci                writeheader(gfc, gi->subblock_gain[1], 3);
444159b3361Sopenharmony_ci                writeheader(gfc, gi->subblock_gain[2], 3);
445159b3361Sopenharmony_ci            }
446159b3361Sopenharmony_ci            else {
447159b3361Sopenharmony_ci                writeheader(gfc, 0, 1); /* window_switching_flag */
448159b3361Sopenharmony_ci                if (gi->table_select[0] == 14)
449159b3361Sopenharmony_ci                    gi->table_select[0] = 16;
450159b3361Sopenharmony_ci                writeheader(gfc, gi->table_select[0], 5);
451159b3361Sopenharmony_ci                if (gi->table_select[1] == 14)
452159b3361Sopenharmony_ci                    gi->table_select[1] = 16;
453159b3361Sopenharmony_ci                writeheader(gfc, gi->table_select[1], 5);
454159b3361Sopenharmony_ci                if (gi->table_select[2] == 14)
455159b3361Sopenharmony_ci                    gi->table_select[2] = 16;
456159b3361Sopenharmony_ci                writeheader(gfc, gi->table_select[2], 5);
457159b3361Sopenharmony_ci
458159b3361Sopenharmony_ci                assert(0 <= gi->region0_count && gi->region0_count < 16);
459159b3361Sopenharmony_ci                assert(0 <= gi->region1_count && gi->region1_count < 8);
460159b3361Sopenharmony_ci                writeheader(gfc, gi->region0_count, 4);
461159b3361Sopenharmony_ci                writeheader(gfc, gi->region1_count, 3);
462159b3361Sopenharmony_ci            }
463159b3361Sopenharmony_ci
464159b3361Sopenharmony_ci            writeheader(gfc, gi->scalefac_scale, 1);
465159b3361Sopenharmony_ci            writeheader(gfc, gi->count1table_select, 1);
466159b3361Sopenharmony_ci        }
467159b3361Sopenharmony_ci    }
468159b3361Sopenharmony_ci
469159b3361Sopenharmony_ci    if (cfg->error_protection) {
470159b3361Sopenharmony_ci        /* (jo) error_protection: add crc16 information to header */
471159b3361Sopenharmony_ci        CRC_writeheader(gfc, esv->header[esv->h_ptr].buf);
472159b3361Sopenharmony_ci    }
473159b3361Sopenharmony_ci
474159b3361Sopenharmony_ci    {
475159b3361Sopenharmony_ci        int const old = esv->h_ptr;
476159b3361Sopenharmony_ci        assert(esv->header[old].ptr == cfg->sideinfo_len * 8);
477159b3361Sopenharmony_ci
478159b3361Sopenharmony_ci        esv->h_ptr = (old + 1) & (MAX_HEADER_BUF - 1);
479159b3361Sopenharmony_ci        esv->header[esv->h_ptr].write_timing = esv->header[old].write_timing + bitsPerFrame;
480159b3361Sopenharmony_ci
481159b3361Sopenharmony_ci        if (esv->h_ptr == esv->w_ptr) {
482159b3361Sopenharmony_ci            /* yikes! we are out of header buffer space */
483159b3361Sopenharmony_ci            ERRORF(gfc, "Error: MAX_HEADER_BUF too small in bitstream.c \n");
484159b3361Sopenharmony_ci        }
485159b3361Sopenharmony_ci
486159b3361Sopenharmony_ci    }
487159b3361Sopenharmony_ci}
488159b3361Sopenharmony_ci
489159b3361Sopenharmony_ci
490159b3361Sopenharmony_ciinline static int
491159b3361Sopenharmony_cihuffman_coder_count1(lame_internal_flags * gfc, gr_info const *gi)
492159b3361Sopenharmony_ci{
493159b3361Sopenharmony_ci    /* Write count1 area */
494159b3361Sopenharmony_ci    struct huffcodetab const *const h = &ht[gi->count1table_select + 32];
495159b3361Sopenharmony_ci    int     i, bits = 0;
496159b3361Sopenharmony_ci#ifdef DEBUG
497159b3361Sopenharmony_ci    int     gegebo = gfc->bs.totbit;
498159b3361Sopenharmony_ci#endif
499159b3361Sopenharmony_ci
500159b3361Sopenharmony_ci    int const *ix = &gi->l3_enc[gi->big_values];
501159b3361Sopenharmony_ci    FLOAT const *xr = &gi->xr[gi->big_values];
502159b3361Sopenharmony_ci    assert(gi->count1table_select < 2);
503159b3361Sopenharmony_ci
504159b3361Sopenharmony_ci    for (i = (gi->count1 - gi->big_values) / 4; i > 0; --i) {
505159b3361Sopenharmony_ci        int     huffbits = 0;
506159b3361Sopenharmony_ci        int     p = 0, v;
507159b3361Sopenharmony_ci
508159b3361Sopenharmony_ci        v = ix[0];
509159b3361Sopenharmony_ci        if (v) {
510159b3361Sopenharmony_ci            p += 8;
511159b3361Sopenharmony_ci            if (xr[0] < 0.0f)
512159b3361Sopenharmony_ci                huffbits++;
513159b3361Sopenharmony_ci            assert(v <= 1);
514159b3361Sopenharmony_ci        }
515159b3361Sopenharmony_ci
516159b3361Sopenharmony_ci        v = ix[1];
517159b3361Sopenharmony_ci        if (v) {
518159b3361Sopenharmony_ci            p += 4;
519159b3361Sopenharmony_ci            huffbits *= 2;
520159b3361Sopenharmony_ci            if (xr[1] < 0.0f)
521159b3361Sopenharmony_ci                huffbits++;
522159b3361Sopenharmony_ci            assert(v <= 1);
523159b3361Sopenharmony_ci        }
524159b3361Sopenharmony_ci
525159b3361Sopenharmony_ci        v = ix[2];
526159b3361Sopenharmony_ci        if (v) {
527159b3361Sopenharmony_ci            p += 2;
528159b3361Sopenharmony_ci            huffbits *= 2;
529159b3361Sopenharmony_ci            if (xr[2] < 0.0f)
530159b3361Sopenharmony_ci                huffbits++;
531159b3361Sopenharmony_ci            assert(v <= 1);
532159b3361Sopenharmony_ci        }
533159b3361Sopenharmony_ci
534159b3361Sopenharmony_ci        v = ix[3];
535159b3361Sopenharmony_ci        if (v) {
536159b3361Sopenharmony_ci            p++;
537159b3361Sopenharmony_ci            huffbits *= 2;
538159b3361Sopenharmony_ci            if (xr[3] < 0.0f)
539159b3361Sopenharmony_ci                huffbits++;
540159b3361Sopenharmony_ci            assert(v <= 1);
541159b3361Sopenharmony_ci        }
542159b3361Sopenharmony_ci
543159b3361Sopenharmony_ci        ix += 4;
544159b3361Sopenharmony_ci        xr += 4;
545159b3361Sopenharmony_ci        putbits2(gfc, huffbits + h->table[p], h->hlen[p]);
546159b3361Sopenharmony_ci        bits += h->hlen[p];
547159b3361Sopenharmony_ci    }
548159b3361Sopenharmony_ci#ifdef DEBUG
549159b3361Sopenharmony_ci    DEBUGF(gfc, "count1: real: %ld counted:%d (bigv %d count1len %d)\n",
550159b3361Sopenharmony_ci           gfc->bs.totbit - gegebo, gi->count1bits, gi->big_values, gi->count1);
551159b3361Sopenharmony_ci#endif
552159b3361Sopenharmony_ci    return bits;
553159b3361Sopenharmony_ci}
554159b3361Sopenharmony_ci
555159b3361Sopenharmony_ci
556159b3361Sopenharmony_ci
557159b3361Sopenharmony_ci/*
558159b3361Sopenharmony_ci  Implements the pseudocode of page 98 of the IS
559159b3361Sopenharmony_ci  */
560159b3361Sopenharmony_ciinline static int
561159b3361Sopenharmony_ciHuffmancode(lame_internal_flags * const gfc, const unsigned int tableindex,
562159b3361Sopenharmony_ci            int start, int end, gr_info const *gi)
563159b3361Sopenharmony_ci{
564159b3361Sopenharmony_ci    struct huffcodetab const *const h = &ht[tableindex];
565159b3361Sopenharmony_ci    unsigned int const linbits = h->xlen;
566159b3361Sopenharmony_ci    int     i, bits = 0;
567159b3361Sopenharmony_ci
568159b3361Sopenharmony_ci    assert(tableindex < 32u);
569159b3361Sopenharmony_ci    if (!tableindex)
570159b3361Sopenharmony_ci        return bits;
571159b3361Sopenharmony_ci
572159b3361Sopenharmony_ci    for (i = start; i < end; i += 2) {
573159b3361Sopenharmony_ci        int16_t  cbits = 0;
574159b3361Sopenharmony_ci        uint16_t xbits = 0;
575159b3361Sopenharmony_ci        unsigned int xlen = h->xlen;
576159b3361Sopenharmony_ci        unsigned int ext = 0;
577159b3361Sopenharmony_ci        unsigned int x1 = gi->l3_enc[i];
578159b3361Sopenharmony_ci        unsigned int x2 = gi->l3_enc[i + 1];
579159b3361Sopenharmony_ci
580159b3361Sopenharmony_ci        assert(gi->l3_enc[i] >= 0);
581159b3361Sopenharmony_ci        assert(gi->l3_enc[i+1] >= 0);
582159b3361Sopenharmony_ci
583159b3361Sopenharmony_ci        if (x1 != 0u) {
584159b3361Sopenharmony_ci            if (gi->xr[i] < 0.0f)
585159b3361Sopenharmony_ci                ext++;
586159b3361Sopenharmony_ci            cbits--;
587159b3361Sopenharmony_ci        }
588159b3361Sopenharmony_ci
589159b3361Sopenharmony_ci        if (tableindex > 15u) {
590159b3361Sopenharmony_ci            /* use ESC-words */
591159b3361Sopenharmony_ci            if (x1 >= 15u) {
592159b3361Sopenharmony_ci                uint16_t const linbits_x1 = x1 - 15u;
593159b3361Sopenharmony_ci                assert(linbits_x1 <= h->linmax);
594159b3361Sopenharmony_ci                ext |= linbits_x1 << 1u;
595159b3361Sopenharmony_ci                xbits = linbits;
596159b3361Sopenharmony_ci                x1 = 15u;
597159b3361Sopenharmony_ci            }
598159b3361Sopenharmony_ci
599159b3361Sopenharmony_ci            if (x2 >= 15u) {
600159b3361Sopenharmony_ci                uint16_t const linbits_x2 = x2 - 15u;
601159b3361Sopenharmony_ci                assert(linbits_x2 <= h->linmax);
602159b3361Sopenharmony_ci                ext <<= linbits;
603159b3361Sopenharmony_ci                ext |= linbits_x2;
604159b3361Sopenharmony_ci                xbits += linbits;
605159b3361Sopenharmony_ci                x2 = 15u;
606159b3361Sopenharmony_ci            }
607159b3361Sopenharmony_ci            xlen = 16;
608159b3361Sopenharmony_ci        }
609159b3361Sopenharmony_ci
610159b3361Sopenharmony_ci        if (x2 != 0u) {
611159b3361Sopenharmony_ci            ext <<= 1;
612159b3361Sopenharmony_ci            if (gi->xr[i + 1] < 0.0f)
613159b3361Sopenharmony_ci                ext++;
614159b3361Sopenharmony_ci            cbits--;
615159b3361Sopenharmony_ci        }
616159b3361Sopenharmony_ci
617159b3361Sopenharmony_ci        assert((x1 | x2) < 16u);
618159b3361Sopenharmony_ci
619159b3361Sopenharmony_ci        x1 = x1 * xlen + x2;
620159b3361Sopenharmony_ci        xbits -= cbits;
621159b3361Sopenharmony_ci        cbits += h->hlen[x1];
622159b3361Sopenharmony_ci
623159b3361Sopenharmony_ci        assert(cbits <= MAX_LENGTH);
624159b3361Sopenharmony_ci        assert(xbits <= MAX_LENGTH);
625159b3361Sopenharmony_ci
626159b3361Sopenharmony_ci        putbits2(gfc, h->table[x1], cbits);
627159b3361Sopenharmony_ci        putbits2(gfc, (int)ext, xbits);
628159b3361Sopenharmony_ci        bits += cbits + xbits;
629159b3361Sopenharmony_ci    }
630159b3361Sopenharmony_ci    return bits;
631159b3361Sopenharmony_ci}
632159b3361Sopenharmony_ci
633159b3361Sopenharmony_ci/*
634159b3361Sopenharmony_ci  Note the discussion of huffmancodebits() on pages 28
635159b3361Sopenharmony_ci  and 29 of the IS, as well as the definitions of the side
636159b3361Sopenharmony_ci  information on pages 26 and 27.
637159b3361Sopenharmony_ci  */
638159b3361Sopenharmony_cistatic int
639159b3361Sopenharmony_ciShortHuffmancodebits(lame_internal_flags * gfc, gr_info const *gi)
640159b3361Sopenharmony_ci{
641159b3361Sopenharmony_ci    int     bits;
642159b3361Sopenharmony_ci    int     region1Start;
643159b3361Sopenharmony_ci
644159b3361Sopenharmony_ci    region1Start = 3 * gfc->scalefac_band.s[3];
645159b3361Sopenharmony_ci    if (region1Start > gi->big_values)
646159b3361Sopenharmony_ci        region1Start = gi->big_values;
647159b3361Sopenharmony_ci
648159b3361Sopenharmony_ci    /* short blocks do not have a region2 */
649159b3361Sopenharmony_ci    bits = Huffmancode(gfc, gi->table_select[0], 0, region1Start, gi);
650159b3361Sopenharmony_ci    bits += Huffmancode(gfc, gi->table_select[1], region1Start, gi->big_values, gi);
651159b3361Sopenharmony_ci    return bits;
652159b3361Sopenharmony_ci}
653159b3361Sopenharmony_ci
654159b3361Sopenharmony_cistatic int
655159b3361Sopenharmony_ciLongHuffmancodebits(lame_internal_flags * gfc, gr_info const *gi)
656159b3361Sopenharmony_ci{
657159b3361Sopenharmony_ci    unsigned int i;
658159b3361Sopenharmony_ci    int     bigvalues, bits;
659159b3361Sopenharmony_ci    int     region1Start, region2Start;
660159b3361Sopenharmony_ci
661159b3361Sopenharmony_ci    bigvalues = gi->big_values;
662159b3361Sopenharmony_ci    assert(0 <= bigvalues && bigvalues <= 576);
663159b3361Sopenharmony_ci
664159b3361Sopenharmony_ci    assert(gi->region0_count >= -1);
665159b3361Sopenharmony_ci    assert(gi->region1_count >= -1);
666159b3361Sopenharmony_ci    i = gi->region0_count + 1;
667159b3361Sopenharmony_ci    assert((size_t) i < dimension_of(gfc->scalefac_band.l));
668159b3361Sopenharmony_ci    region1Start = gfc->scalefac_band.l[i];
669159b3361Sopenharmony_ci    i += gi->region1_count + 1;
670159b3361Sopenharmony_ci    assert((size_t) i < dimension_of(gfc->scalefac_band.l));
671159b3361Sopenharmony_ci    region2Start = gfc->scalefac_band.l[i];
672159b3361Sopenharmony_ci
673159b3361Sopenharmony_ci    if (region1Start > bigvalues)
674159b3361Sopenharmony_ci        region1Start = bigvalues;
675159b3361Sopenharmony_ci
676159b3361Sopenharmony_ci    if (region2Start > bigvalues)
677159b3361Sopenharmony_ci        region2Start = bigvalues;
678159b3361Sopenharmony_ci
679159b3361Sopenharmony_ci    bits = Huffmancode(gfc, gi->table_select[0], 0, region1Start, gi);
680159b3361Sopenharmony_ci    bits += Huffmancode(gfc, gi->table_select[1], region1Start, region2Start, gi);
681159b3361Sopenharmony_ci    bits += Huffmancode(gfc, gi->table_select[2], region2Start, bigvalues, gi);
682159b3361Sopenharmony_ci    return bits;
683159b3361Sopenharmony_ci}
684159b3361Sopenharmony_ci
685159b3361Sopenharmony_ciinline static int
686159b3361Sopenharmony_ciwriteMainData(lame_internal_flags * const gfc)
687159b3361Sopenharmony_ci{
688159b3361Sopenharmony_ci    SessionConfig_t const *const cfg = &gfc->cfg;
689159b3361Sopenharmony_ci    III_side_info_t const *const l3_side = &gfc->l3_side;
690159b3361Sopenharmony_ci    int     gr, ch, sfb, data_bits, tot_bits = 0;
691159b3361Sopenharmony_ci
692159b3361Sopenharmony_ci    if (cfg->version == 1) {
693159b3361Sopenharmony_ci        /* MPEG 1 */
694159b3361Sopenharmony_ci        for (gr = 0; gr < 2; gr++) {
695159b3361Sopenharmony_ci            for (ch = 0; ch < cfg->channels_out; ch++) {
696159b3361Sopenharmony_ci                gr_info const *const gi = &l3_side->tt[gr][ch];
697159b3361Sopenharmony_ci                int const slen1 = slen1_tab[gi->scalefac_compress];
698159b3361Sopenharmony_ci                int const slen2 = slen2_tab[gi->scalefac_compress];
699159b3361Sopenharmony_ci                data_bits = 0;
700159b3361Sopenharmony_ci#ifdef DEBUG
701159b3361Sopenharmony_ci                hogege = gfc->bs.totbit;
702159b3361Sopenharmony_ci#endif
703159b3361Sopenharmony_ci                for (sfb = 0; sfb < gi->sfbdivide; sfb++) {
704159b3361Sopenharmony_ci                    if (gi->scalefac[sfb] == -1)
705159b3361Sopenharmony_ci                        continue; /* scfsi is used */
706159b3361Sopenharmony_ci                    putbits2(gfc, gi->scalefac[sfb], slen1);
707159b3361Sopenharmony_ci                    data_bits += slen1;
708159b3361Sopenharmony_ci                }
709159b3361Sopenharmony_ci                for (; sfb < gi->sfbmax; sfb++) {
710159b3361Sopenharmony_ci                    if (gi->scalefac[sfb] == -1)
711159b3361Sopenharmony_ci                        continue; /* scfsi is used */
712159b3361Sopenharmony_ci                    putbits2(gfc, gi->scalefac[sfb], slen2);
713159b3361Sopenharmony_ci                    data_bits += slen2;
714159b3361Sopenharmony_ci                }
715159b3361Sopenharmony_ci                assert(data_bits == gi->part2_length);
716159b3361Sopenharmony_ci
717159b3361Sopenharmony_ci                if (gi->block_type == SHORT_TYPE) {
718159b3361Sopenharmony_ci                    data_bits += ShortHuffmancodebits(gfc, gi);
719159b3361Sopenharmony_ci                }
720159b3361Sopenharmony_ci                else {
721159b3361Sopenharmony_ci                    data_bits += LongHuffmancodebits(gfc, gi);
722159b3361Sopenharmony_ci                }
723159b3361Sopenharmony_ci                data_bits += huffman_coder_count1(gfc, gi);
724159b3361Sopenharmony_ci#ifdef DEBUG
725159b3361Sopenharmony_ci                DEBUGF(gfc, "<%ld> ", gfc->bs.totbit - hogege);
726159b3361Sopenharmony_ci#endif
727159b3361Sopenharmony_ci                /* does bitcount in quantize.c agree with actual bit count? */
728159b3361Sopenharmony_ci                assert(data_bits == gi->part2_3_length + gi->part2_length);
729159b3361Sopenharmony_ci                tot_bits += data_bits;
730159b3361Sopenharmony_ci            }           /* for ch */
731159b3361Sopenharmony_ci        }               /* for gr */
732159b3361Sopenharmony_ci    }
733159b3361Sopenharmony_ci    else {
734159b3361Sopenharmony_ci        /* MPEG 2 */
735159b3361Sopenharmony_ci        gr = 0;
736159b3361Sopenharmony_ci        for (ch = 0; ch < cfg->channels_out; ch++) {
737159b3361Sopenharmony_ci            gr_info const *const gi = &l3_side->tt[gr][ch];
738159b3361Sopenharmony_ci            int     i, sfb_partition, scale_bits = 0;
739159b3361Sopenharmony_ci            assert(gi->sfb_partition_table);
740159b3361Sopenharmony_ci            data_bits = 0;
741159b3361Sopenharmony_ci#ifdef DEBUG
742159b3361Sopenharmony_ci            hogege = gfc->bs.totbit;
743159b3361Sopenharmony_ci#endif
744159b3361Sopenharmony_ci            sfb = 0;
745159b3361Sopenharmony_ci            sfb_partition = 0;
746159b3361Sopenharmony_ci
747159b3361Sopenharmony_ci            if (gi->block_type == SHORT_TYPE) {
748159b3361Sopenharmony_ci                for (; sfb_partition < 4; sfb_partition++) {
749159b3361Sopenharmony_ci                    int const sfbs = gi->sfb_partition_table[sfb_partition] / 3;
750159b3361Sopenharmony_ci                    int const slen = gi->slen[sfb_partition];
751159b3361Sopenharmony_ci                    for (i = 0; i < sfbs; i++, sfb++) {
752159b3361Sopenharmony_ci                        putbits2(gfc, Max(gi->scalefac[sfb * 3 + 0], 0), slen);
753159b3361Sopenharmony_ci                        putbits2(gfc, Max(gi->scalefac[sfb * 3 + 1], 0), slen);
754159b3361Sopenharmony_ci                        putbits2(gfc, Max(gi->scalefac[sfb * 3 + 2], 0), slen);
755159b3361Sopenharmony_ci                        scale_bits += 3 * slen;
756159b3361Sopenharmony_ci                    }
757159b3361Sopenharmony_ci                }
758159b3361Sopenharmony_ci                data_bits += ShortHuffmancodebits(gfc, gi);
759159b3361Sopenharmony_ci            }
760159b3361Sopenharmony_ci            else {
761159b3361Sopenharmony_ci                for (; sfb_partition < 4; sfb_partition++) {
762159b3361Sopenharmony_ci                    int const sfbs = gi->sfb_partition_table[sfb_partition];
763159b3361Sopenharmony_ci                    int const slen = gi->slen[sfb_partition];
764159b3361Sopenharmony_ci                    for (i = 0; i < sfbs; i++, sfb++) {
765159b3361Sopenharmony_ci                        putbits2(gfc, Max(gi->scalefac[sfb], 0), slen);
766159b3361Sopenharmony_ci                        scale_bits += slen;
767159b3361Sopenharmony_ci                    }
768159b3361Sopenharmony_ci                }
769159b3361Sopenharmony_ci                data_bits += LongHuffmancodebits(gfc, gi);
770159b3361Sopenharmony_ci            }
771159b3361Sopenharmony_ci            data_bits += huffman_coder_count1(gfc, gi);
772159b3361Sopenharmony_ci#ifdef DEBUG
773159b3361Sopenharmony_ci            DEBUGF(gfc, "<%ld> ", gfc->bs.totbit - hogege);
774159b3361Sopenharmony_ci#endif
775159b3361Sopenharmony_ci            /* does bitcount in quantize.c agree with actual bit count? */
776159b3361Sopenharmony_ci            assert(data_bits == gi->part2_3_length);
777159b3361Sopenharmony_ci            assert(scale_bits == gi->part2_length);
778159b3361Sopenharmony_ci            tot_bits += scale_bits + data_bits;
779159b3361Sopenharmony_ci        }               /* for ch */
780159b3361Sopenharmony_ci    }                   /* for gf */
781159b3361Sopenharmony_ci    return tot_bits;
782159b3361Sopenharmony_ci}                       /* main_data */
783159b3361Sopenharmony_ci
784159b3361Sopenharmony_ci
785159b3361Sopenharmony_ci
786159b3361Sopenharmony_ci/* compute the number of bits required to flush all mp3 frames
787159b3361Sopenharmony_ci   currently in the buffer.  This should be the same as the
788159b3361Sopenharmony_ci   reservoir size.  Only call this routine between frames - i.e.
789159b3361Sopenharmony_ci   only after all headers and data have been added to the buffer
790159b3361Sopenharmony_ci   by format_bitstream().
791159b3361Sopenharmony_ci
792159b3361Sopenharmony_ci   Also compute total_bits_output =
793159b3361Sopenharmony_ci       size of mp3 buffer (including frame headers which may not
794159b3361Sopenharmony_ci       have yet been send to the mp3 buffer) +
795159b3361Sopenharmony_ci       number of bits needed to flush all mp3 frames.
796159b3361Sopenharmony_ci
797159b3361Sopenharmony_ci   total_bytes_output is the size of the mp3 output buffer if
798159b3361Sopenharmony_ci   lame_encode_flush_nogap() was called right now.
799159b3361Sopenharmony_ci
800159b3361Sopenharmony_ci */
801159b3361Sopenharmony_ciint
802159b3361Sopenharmony_cicompute_flushbits(const lame_internal_flags * gfc, int *total_bytes_output)
803159b3361Sopenharmony_ci{
804159b3361Sopenharmony_ci    SessionConfig_t const *const cfg = &gfc->cfg;
805159b3361Sopenharmony_ci    EncStateVar_t const *const esv = &gfc->sv_enc;
806159b3361Sopenharmony_ci    int     flushbits, remaining_headers;
807159b3361Sopenharmony_ci    int     bitsPerFrame;
808159b3361Sopenharmony_ci    int     last_ptr, first_ptr;
809159b3361Sopenharmony_ci    first_ptr = esv->w_ptr; /* first header to add to bitstream */
810159b3361Sopenharmony_ci    last_ptr = esv->h_ptr - 1; /* last header to add to bitstream */
811159b3361Sopenharmony_ci    if (last_ptr == -1)
812159b3361Sopenharmony_ci        last_ptr = MAX_HEADER_BUF - 1;
813159b3361Sopenharmony_ci
814159b3361Sopenharmony_ci    /* add this many bits to bitstream so we can flush all headers */
815159b3361Sopenharmony_ci    flushbits = esv->header[last_ptr].write_timing - gfc->bs.totbit;
816159b3361Sopenharmony_ci    *total_bytes_output = flushbits;
817159b3361Sopenharmony_ci
818159b3361Sopenharmony_ci    if (flushbits >= 0) {
819159b3361Sopenharmony_ci        /* if flushbits >= 0, some headers have not yet been written */
820159b3361Sopenharmony_ci        /* reduce flushbits by the size of the headers */
821159b3361Sopenharmony_ci        remaining_headers = 1 + last_ptr - first_ptr;
822159b3361Sopenharmony_ci        if (last_ptr < first_ptr)
823159b3361Sopenharmony_ci            remaining_headers = 1 + last_ptr - first_ptr + MAX_HEADER_BUF;
824159b3361Sopenharmony_ci        flushbits -= remaining_headers * 8 * cfg->sideinfo_len;
825159b3361Sopenharmony_ci    }
826159b3361Sopenharmony_ci
827159b3361Sopenharmony_ci
828159b3361Sopenharmony_ci    /* finally, add some bits so that the last frame is complete
829159b3361Sopenharmony_ci     * these bits are not necessary to decode the last frame, but
830159b3361Sopenharmony_ci     * some decoders will ignore last frame if these bits are missing
831159b3361Sopenharmony_ci     */
832159b3361Sopenharmony_ci    bitsPerFrame = getframebits(gfc);
833159b3361Sopenharmony_ci    flushbits += bitsPerFrame;
834159b3361Sopenharmony_ci    *total_bytes_output += bitsPerFrame;
835159b3361Sopenharmony_ci    /* round up:   */
836159b3361Sopenharmony_ci    if (*total_bytes_output % 8)
837159b3361Sopenharmony_ci        *total_bytes_output = 1 + (*total_bytes_output / 8);
838159b3361Sopenharmony_ci    else
839159b3361Sopenharmony_ci        *total_bytes_output = (*total_bytes_output / 8);
840159b3361Sopenharmony_ci    *total_bytes_output += gfc->bs.buf_byte_idx + 1;
841159b3361Sopenharmony_ci
842159b3361Sopenharmony_ci
843159b3361Sopenharmony_ci    if (flushbits < 0) {
844159b3361Sopenharmony_ci#if 0
845159b3361Sopenharmony_ci        /* if flushbits < 0, this would mean that the buffer looks like:
846159b3361Sopenharmony_ci         * (data...)  last_header  (data...)  (extra data that should not be here...)
847159b3361Sopenharmony_ci         */
848159b3361Sopenharmony_ci        DEBUGF(gfc, "last header write_timing = %i \n", esv->header[last_ptr].write_timing);
849159b3361Sopenharmony_ci        DEBUGF(gfc, "first header write_timing = %i \n", esv->header[first_ptr].write_timing);
850159b3361Sopenharmony_ci        DEBUGF(gfc, "bs.totbit:                 %i \n", gfc->bs.totbit);
851159b3361Sopenharmony_ci        DEBUGF(gfc, "first_ptr, last_ptr        %i %i \n", first_ptr, last_ptr);
852159b3361Sopenharmony_ci        DEBUGF(gfc, "remaining_headers =        %i \n", remaining_headers);
853159b3361Sopenharmony_ci        DEBUGF(gfc, "bitsperframe:              %i \n", bitsPerFrame);
854159b3361Sopenharmony_ci        DEBUGF(gfc, "sidelen:                   %i \n", cfg->sideinfo_len);
855159b3361Sopenharmony_ci#endif
856159b3361Sopenharmony_ci        ERRORF(gfc, "strange error flushing buffer ... \n");
857159b3361Sopenharmony_ci    }
858159b3361Sopenharmony_ci    return flushbits;
859159b3361Sopenharmony_ci}
860159b3361Sopenharmony_ci
861159b3361Sopenharmony_ci
862159b3361Sopenharmony_civoid
863159b3361Sopenharmony_ciflush_bitstream(lame_internal_flags * gfc)
864159b3361Sopenharmony_ci{
865159b3361Sopenharmony_ci    EncStateVar_t *const esv = &gfc->sv_enc;
866159b3361Sopenharmony_ci    III_side_info_t *l3_side;
867159b3361Sopenharmony_ci    int     nbytes;
868159b3361Sopenharmony_ci    int     flushbits;
869159b3361Sopenharmony_ci    int     last_ptr = esv->h_ptr - 1; /* last header to add to bitstream */
870159b3361Sopenharmony_ci    if (last_ptr == -1)
871159b3361Sopenharmony_ci        last_ptr = MAX_HEADER_BUF - 1;
872159b3361Sopenharmony_ci    l3_side = &gfc->l3_side;
873159b3361Sopenharmony_ci
874159b3361Sopenharmony_ci
875159b3361Sopenharmony_ci    if ((flushbits = compute_flushbits(gfc, &nbytes)) < 0)
876159b3361Sopenharmony_ci        return;
877159b3361Sopenharmony_ci    drain_into_ancillary(gfc, flushbits);
878159b3361Sopenharmony_ci
879159b3361Sopenharmony_ci    /* check that the 100% of the last frame has been written to bitstream */
880159b3361Sopenharmony_ci    assert(esv->header[last_ptr].write_timing + getframebits(gfc)
881159b3361Sopenharmony_ci           == gfc->bs.totbit);
882159b3361Sopenharmony_ci
883159b3361Sopenharmony_ci    /* we have padded out all frames with ancillary data, which is the
884159b3361Sopenharmony_ci       same as filling the bitreservoir with ancillary data, so : */
885159b3361Sopenharmony_ci    esv->ResvSize = 0;
886159b3361Sopenharmony_ci    l3_side->main_data_begin = 0;
887159b3361Sopenharmony_ci}
888159b3361Sopenharmony_ci
889159b3361Sopenharmony_ci
890159b3361Sopenharmony_ci
891159b3361Sopenharmony_ci
892159b3361Sopenharmony_civoid
893159b3361Sopenharmony_ciadd_dummy_byte(lame_internal_flags * gfc, unsigned char val, unsigned int n)
894159b3361Sopenharmony_ci{
895159b3361Sopenharmony_ci    EncStateVar_t *const esv = &gfc->sv_enc;
896159b3361Sopenharmony_ci    int     i;
897159b3361Sopenharmony_ci
898159b3361Sopenharmony_ci    while (n-- > 0u) {
899159b3361Sopenharmony_ci        putbits_noheaders(gfc, val, 8);
900159b3361Sopenharmony_ci
901159b3361Sopenharmony_ci        for (i = 0; i < MAX_HEADER_BUF; ++i)
902159b3361Sopenharmony_ci            esv->header[i].write_timing += 8;
903159b3361Sopenharmony_ci    }
904159b3361Sopenharmony_ci}
905159b3361Sopenharmony_ci
906159b3361Sopenharmony_ci
907159b3361Sopenharmony_ci/*
908159b3361Sopenharmony_ci  format_bitstream()
909159b3361Sopenharmony_ci
910159b3361Sopenharmony_ci  This is called after a frame of audio has been quantized and coded.
911159b3361Sopenharmony_ci  It will write the encoded audio to the bitstream. Note that
912159b3361Sopenharmony_ci  from a layer3 encoder's perspective the bit stream is primarily
913159b3361Sopenharmony_ci  a series of main_data() blocks, with header and side information
914159b3361Sopenharmony_ci  inserted at the proper locations to maintain framing. (See Figure A.7
915159b3361Sopenharmony_ci  in the IS).
916159b3361Sopenharmony_ci  */
917159b3361Sopenharmony_ciint
918159b3361Sopenharmony_ciformat_bitstream(lame_internal_flags * gfc)
919159b3361Sopenharmony_ci{
920159b3361Sopenharmony_ci    SessionConfig_t const *const cfg = &gfc->cfg;
921159b3361Sopenharmony_ci    EncStateVar_t *const esv = &gfc->sv_enc;
922159b3361Sopenharmony_ci    int     bits, nbytes;
923159b3361Sopenharmony_ci    III_side_info_t *l3_side;
924159b3361Sopenharmony_ci    int     bitsPerFrame;
925159b3361Sopenharmony_ci    l3_side = &gfc->l3_side;
926159b3361Sopenharmony_ci
927159b3361Sopenharmony_ci    bitsPerFrame = getframebits(gfc);
928159b3361Sopenharmony_ci    drain_into_ancillary(gfc, l3_side->resvDrain_pre);
929159b3361Sopenharmony_ci
930159b3361Sopenharmony_ci    encodeSideInfo2(gfc, bitsPerFrame);
931159b3361Sopenharmony_ci    bits = 8 * cfg->sideinfo_len;
932159b3361Sopenharmony_ci    bits += writeMainData(gfc);
933159b3361Sopenharmony_ci    drain_into_ancillary(gfc, l3_side->resvDrain_post);
934159b3361Sopenharmony_ci    bits += l3_side->resvDrain_post;
935159b3361Sopenharmony_ci
936159b3361Sopenharmony_ci    l3_side->main_data_begin += (bitsPerFrame - bits) / 8;
937159b3361Sopenharmony_ci
938159b3361Sopenharmony_ci    /* compare number of bits needed to clear all buffered mp3 frames
939159b3361Sopenharmony_ci     * with what we think the resvsize is: */
940159b3361Sopenharmony_ci    if (compute_flushbits(gfc, &nbytes) != esv->ResvSize) {
941159b3361Sopenharmony_ci        ERRORF(gfc, "Internal buffer inconsistency. flushbits <> ResvSize");
942159b3361Sopenharmony_ci    }
943159b3361Sopenharmony_ci
944159b3361Sopenharmony_ci
945159b3361Sopenharmony_ci    /* compare main_data_begin for the next frame with what we
946159b3361Sopenharmony_ci     * think the resvsize is: */
947159b3361Sopenharmony_ci    if ((l3_side->main_data_begin * 8) != esv->ResvSize) {
948159b3361Sopenharmony_ci        ERRORF(gfc, "bit reservoir error: \n"
949159b3361Sopenharmony_ci               "l3_side->main_data_begin: %i \n"
950159b3361Sopenharmony_ci               "Resvoir size:             %i \n"
951159b3361Sopenharmony_ci               "resv drain (post)         %i \n"
952159b3361Sopenharmony_ci               "resv drain (pre)          %i \n"
953159b3361Sopenharmony_ci               "header and sideinfo:      %i \n"
954159b3361Sopenharmony_ci               "data bits:                %i \n"
955159b3361Sopenharmony_ci               "total bits:               %i (remainder: %i) \n"
956159b3361Sopenharmony_ci               "bitsperframe:             %i \n",
957159b3361Sopenharmony_ci               8 * l3_side->main_data_begin,
958159b3361Sopenharmony_ci               esv->ResvSize,
959159b3361Sopenharmony_ci               l3_side->resvDrain_post,
960159b3361Sopenharmony_ci               l3_side->resvDrain_pre,
961159b3361Sopenharmony_ci               8 * cfg->sideinfo_len,
962159b3361Sopenharmony_ci               bits - l3_side->resvDrain_post - 8 * cfg->sideinfo_len,
963159b3361Sopenharmony_ci               bits, bits % 8, bitsPerFrame);
964159b3361Sopenharmony_ci
965159b3361Sopenharmony_ci        ERRORF(gfc, "This is a fatal error.  It has several possible causes:");
966159b3361Sopenharmony_ci        ERRORF(gfc, "90%%  LAME compiled with buggy version of gcc using advanced optimizations");
967159b3361Sopenharmony_ci        ERRORF(gfc, " 9%%  Your system is overclocked");
968159b3361Sopenharmony_ci        ERRORF(gfc, " 1%%  bug in LAME encoding library");
969159b3361Sopenharmony_ci
970159b3361Sopenharmony_ci        esv->ResvSize = l3_side->main_data_begin * 8;
971159b3361Sopenharmony_ci    };
972159b3361Sopenharmony_ci    assert(gfc->bs.totbit % 8 == 0);
973159b3361Sopenharmony_ci
974159b3361Sopenharmony_ci    if (gfc->bs.totbit > 1000000000) {
975159b3361Sopenharmony_ci        /* to avoid totbit overflow, (at 8h encoding at 128kbs) lets reset bit counter */
976159b3361Sopenharmony_ci        int     i;
977159b3361Sopenharmony_ci        for (i = 0; i < MAX_HEADER_BUF; ++i)
978159b3361Sopenharmony_ci            esv->header[i].write_timing -= gfc->bs.totbit;
979159b3361Sopenharmony_ci        gfc->bs.totbit = 0;
980159b3361Sopenharmony_ci    }
981159b3361Sopenharmony_ci
982159b3361Sopenharmony_ci
983159b3361Sopenharmony_ci    return 0;
984159b3361Sopenharmony_ci}
985159b3361Sopenharmony_ci
986159b3361Sopenharmony_ci
987159b3361Sopenharmony_cistatic int
988159b3361Sopenharmony_cido_gain_analysis(lame_internal_flags * gfc, unsigned char* buffer, int minimum)
989159b3361Sopenharmony_ci{
990159b3361Sopenharmony_ci    SessionConfig_t const *const cfg = &gfc->cfg;
991159b3361Sopenharmony_ci    RpgStateVar_t const *const rsv = &gfc->sv_rpg;
992159b3361Sopenharmony_ci    RpgResult_t *const rov = &gfc->ov_rpg;
993159b3361Sopenharmony_ci#ifdef DECODE_ON_THE_FLY
994159b3361Sopenharmony_ci    if (cfg->decode_on_the_fly) { /* decode the frame */
995159b3361Sopenharmony_ci        sample_t pcm_buf[2][1152];
996159b3361Sopenharmony_ci        int     mp3_in = minimum;
997159b3361Sopenharmony_ci        int     samples_out = -1;
998159b3361Sopenharmony_ci
999159b3361Sopenharmony_ci        /* re-synthesis to pcm.  Repeat until we get a samples_out=0 */
1000159b3361Sopenharmony_ci        while (samples_out != 0) {
1001159b3361Sopenharmony_ci
1002159b3361Sopenharmony_ci            samples_out = hip_decode1_unclipped(gfc->hip, buffer, mp3_in, pcm_buf[0], pcm_buf[1]);
1003159b3361Sopenharmony_ci            /* samples_out = 0:  need more data to decode
1004159b3361Sopenharmony_ci             * samples_out = -1:  error.  Lets assume 0 pcm output
1005159b3361Sopenharmony_ci             * samples_out = number of samples output */
1006159b3361Sopenharmony_ci
1007159b3361Sopenharmony_ci            /* set the lenght of the mp3 input buffer to zero, so that in the
1008159b3361Sopenharmony_ci             * next iteration of the loop we will be querying mpglib about
1009159b3361Sopenharmony_ci             * buffered data */
1010159b3361Sopenharmony_ci            mp3_in = 0;
1011159b3361Sopenharmony_ci
1012159b3361Sopenharmony_ci            if (samples_out == -1) {
1013159b3361Sopenharmony_ci                /* error decoding. Not fatal, but might screw up
1014159b3361Sopenharmony_ci                 * the ReplayGain tag. What should we do? Ignore for now */
1015159b3361Sopenharmony_ci                samples_out = 0;
1016159b3361Sopenharmony_ci            }
1017159b3361Sopenharmony_ci            if (samples_out > 0) {
1018159b3361Sopenharmony_ci                /* process the PCM data */
1019159b3361Sopenharmony_ci
1020159b3361Sopenharmony_ci                /* this should not be possible, and indicates we have
1021159b3361Sopenharmony_ci                 * overflown the pcm_buf buffer */
1022159b3361Sopenharmony_ci                assert(samples_out <= 1152);
1023159b3361Sopenharmony_ci
1024159b3361Sopenharmony_ci                if (cfg->findPeakSample) {
1025159b3361Sopenharmony_ci                    int     i;
1026159b3361Sopenharmony_ci                    /* FIXME: is this correct? maybe Max(fabs(pcm),PeakSample) */
1027159b3361Sopenharmony_ci                    for (i = 0; i < samples_out; i++) {
1028159b3361Sopenharmony_ci                        if (pcm_buf[0][i] > rov->PeakSample)
1029159b3361Sopenharmony_ci                            rov->PeakSample = pcm_buf[0][i];
1030159b3361Sopenharmony_ci                        else if (-pcm_buf[0][i] > rov->PeakSample)
1031159b3361Sopenharmony_ci                            rov->PeakSample = -pcm_buf[0][i];
1032159b3361Sopenharmony_ci                    }
1033159b3361Sopenharmony_ci                    if (cfg->channels_out > 1)
1034159b3361Sopenharmony_ci                        for (i = 0; i < samples_out; i++) {
1035159b3361Sopenharmony_ci                            if (pcm_buf[1][i] > rov->PeakSample)
1036159b3361Sopenharmony_ci                                rov->PeakSample = pcm_buf[1][i];
1037159b3361Sopenharmony_ci                            else if (-pcm_buf[1][i] > rov->PeakSample)
1038159b3361Sopenharmony_ci                                rov->PeakSample = -pcm_buf[1][i];
1039159b3361Sopenharmony_ci                        }
1040159b3361Sopenharmony_ci                }
1041159b3361Sopenharmony_ci
1042159b3361Sopenharmony_ci                if (cfg->findReplayGain)
1043159b3361Sopenharmony_ci                    if (AnalyzeSamples
1044159b3361Sopenharmony_ci                        (rsv->rgdata, pcm_buf[0], pcm_buf[1], samples_out,
1045159b3361Sopenharmony_ci                         cfg->channels_out) == GAIN_ANALYSIS_ERROR)
1046159b3361Sopenharmony_ci                        return -6;
1047159b3361Sopenharmony_ci
1048159b3361Sopenharmony_ci            }       /* if (samples_out>0) */
1049159b3361Sopenharmony_ci        }           /* while (samples_out!=0) */
1050159b3361Sopenharmony_ci    }               /* if (gfc->decode_on_the_fly) */
1051159b3361Sopenharmony_ci#endif
1052159b3361Sopenharmony_ci    return minimum;
1053159b3361Sopenharmony_ci}
1054159b3361Sopenharmony_ci
1055159b3361Sopenharmony_cistatic int
1056159b3361Sopenharmony_cido_copy_buffer(lame_internal_flags * gfc, unsigned char *buffer, int size)
1057159b3361Sopenharmony_ci{
1058159b3361Sopenharmony_ci    Bit_stream_struc *const bs = &gfc->bs;
1059159b3361Sopenharmony_ci    int const minimum = bs->buf_byte_idx + 1;
1060159b3361Sopenharmony_ci    if (minimum <= 0)
1061159b3361Sopenharmony_ci        return 0;
1062159b3361Sopenharmony_ci    if (minimum > size)
1063159b3361Sopenharmony_ci        return -1;      /* buffer is too small */
1064159b3361Sopenharmony_ci    memcpy(buffer, bs->buf, minimum);
1065159b3361Sopenharmony_ci    bs->buf_byte_idx = -1;
1066159b3361Sopenharmony_ci    bs->buf_bit_idx = 0;
1067159b3361Sopenharmony_ci    return minimum;
1068159b3361Sopenharmony_ci}
1069159b3361Sopenharmony_ci
1070159b3361Sopenharmony_ci/* copy data out of the internal MP3 bit buffer into a user supplied
1071159b3361Sopenharmony_ci   unsigned char buffer.
1072159b3361Sopenharmony_ci
1073159b3361Sopenharmony_ci   mp3data=0      indicates data in buffer is an id3tags and VBR tags
1074159b3361Sopenharmony_ci   mp3data=1      data is real mp3 frame data.
1075159b3361Sopenharmony_ci
1076159b3361Sopenharmony_ci
1077159b3361Sopenharmony_ci*/
1078159b3361Sopenharmony_ciint
1079159b3361Sopenharmony_cicopy_buffer(lame_internal_flags * gfc, unsigned char *buffer, int size, int mp3data)
1080159b3361Sopenharmony_ci{
1081159b3361Sopenharmony_ci    int const minimum = do_copy_buffer(gfc, buffer, size);
1082159b3361Sopenharmony_ci    if (minimum > 0 && mp3data) {
1083159b3361Sopenharmony_ci        UpdateMusicCRC(&gfc->nMusicCRC, buffer, minimum);
1084159b3361Sopenharmony_ci
1085159b3361Sopenharmony_ci        /** sum number of bytes belonging to the mp3 stream
1086159b3361Sopenharmony_ci         *  this info will be written into the Xing/LAME header for seeking
1087159b3361Sopenharmony_ci         */
1088159b3361Sopenharmony_ci        gfc->VBR_seek_table.nBytesWritten += minimum;
1089159b3361Sopenharmony_ci
1090159b3361Sopenharmony_ci        return do_gain_analysis(gfc, buffer, minimum);
1091159b3361Sopenharmony_ci    }                   /* if (mp3data) */
1092159b3361Sopenharmony_ci    return minimum;
1093159b3361Sopenharmony_ci}
1094159b3361Sopenharmony_ci
1095159b3361Sopenharmony_ci
1096159b3361Sopenharmony_civoid
1097159b3361Sopenharmony_ciinit_bit_stream_w(lame_internal_flags * gfc)
1098159b3361Sopenharmony_ci{
1099159b3361Sopenharmony_ci    EncStateVar_t *const esv = &gfc->sv_enc;
1100159b3361Sopenharmony_ci
1101159b3361Sopenharmony_ci    esv->h_ptr = esv->w_ptr = 0;
1102159b3361Sopenharmony_ci    esv->header[esv->h_ptr].write_timing = 0;
1103159b3361Sopenharmony_ci
1104159b3361Sopenharmony_ci    gfc->bs.buf = lame_calloc(unsigned char, BUFFER_SIZE);
1105159b3361Sopenharmony_ci    gfc->bs.buf_size = BUFFER_SIZE;
1106159b3361Sopenharmony_ci    gfc->bs.buf_byte_idx = -1;
1107159b3361Sopenharmony_ci    gfc->bs.buf_bit_idx = 0;
1108159b3361Sopenharmony_ci    gfc->bs.totbit = 0;
1109159b3361Sopenharmony_ci}
1110159b3361Sopenharmony_ci
1111159b3361Sopenharmony_ci/* end of bitstream.c */
1112