From ce5773ca73462a9319c16d0afc692eb38e35af8d Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Wed, 17 Feb 2016 20:16:12 +0100 Subject: Many libtoolame-dab fixes, at least filesize is right now --- libtoolame-dab/bitstream.c | 79 +++++++--------------------------------------- libtoolame-dab/common.h | 7 ++-- libtoolame-dab/toolame.c | 35 +++++++++++++++++++- libtoolame-dab/toolame.h | 17 ++++++++-- 4 files changed, 65 insertions(+), 73 deletions(-) (limited to 'libtoolame-dab') diff --git a/libtoolame-dab/bitstream.c b/libtoolame-dab/bitstream.c index 410ef5b..d426ffc 100644 --- a/libtoolame-dab/bitstream.c +++ b/libtoolame-dab/bitstream.c @@ -42,89 +42,35 @@ /* in conformity of the norme ETS 300 401 http://www.etsi.org */ /* see toollame.c */ int minimum = MINIMUM; -int refill_buffer (Bit_stream_struc * bs) -{ - register int i = bs->buf_size - 2 - bs->buf_byte_idx; - register unsigned long n = 1; - register int index = 0; - char val[2]; - - while ((i >= 0) && (!bs->eob)) { - - if (bs->format == BINARY) - n = fread (&bs->buf[i--], sizeof (unsigned char), 1, bs->pt); - - else { - while ((index < 2) && n) { - n = fread (&val[index], sizeof (char), 1, bs->pt); - switch (val[index]) { - case 0x30: - case 0x31: - case 0x32: - case 0x33: - case 0x34: - case 0x35: - case 0x36: - case 0x37: - case 0x38: - case 0x39: - case 0x41: - case 0x42: - case 0x43: - case 0x44: - case 0x45: - case 0x46: - index++; - break; - default: - break; - } - } - - if (val[0] <= 0x39) - bs->buf[i] = (val[0] - 0x30) << 4; - else - bs->buf[i] = (val[0] - 0x37) << 4; - if (val[1] <= 0x39) - bs->buf[i--] |= (val[1] - 0x30); - else - bs->buf[i--] |= (val[1] - 0x37); - index = 0; - } - - if (!n) { - bs->eob = i + 1; - } - - } - return 0; -} /* empty the buffer to the output device when the buffer becomes full */ void empty_buffer (Bit_stream_struc * bs, int minimum) { - int i; - - if (bs->pt) { - for (i = bs->buf_size - 1; i >= minimum; i--) - fwrite (&bs->buf[i], sizeof (unsigned char), 1, bs->pt); + int j = 0; + for (int i = bs->buf_size - 1; i >= minimum; i--) { + if (j >= bs->output_buffer_size) { + fprintf(stderr, "Warning: libtoolame output buffer too small (%d vs %d)!\n", + bs->output_buffer_size, bs->buf_size - minimum); + break; + } - fflush (bs->pt); /* NEW SS to assist in debugging */ + bs->output_buffer[j] = bs->buf[i]; + j++; } + bs->output_buffer_written = j; - for (i = minimum - 1; i >= 0; i--) + for (int i = minimum - 1; i >= 0; i--) { bs->buf[bs->buf_size - minimum + i] = bs->buf[i]; + } bs->buf_byte_idx = bs->buf_size - 1 - minimum; bs->buf_bit_idx = 8; - } /* open the device to write the bit stream into it */ void open_bit_stream_w (Bit_stream_struc * bs, int size) { - bs->pt = NULL; // we're not using file output alloc_buffer (bs, size); bs->buf_byte_idx = size - 1; bs->buf_bit_idx = 8; @@ -139,7 +85,6 @@ void close_bit_stream_w (Bit_stream_struc * bs) { putbits (bs, 0, 7); empty_buffer (bs, bs->buf_byte_idx + 1); - if (bs->pt) fclose(bs->pt); desalloc_buffer (bs); } diff --git a/libtoolame-dab/common.h b/libtoolame-dab/common.h index 8551483..0a19af6 100644 --- a/libtoolame-dab/common.h +++ b/libtoolame-dab/common.h @@ -142,9 +142,10 @@ frame_info; typedef struct bit_stream_struc { - FILE *pt; /* pointer to bit stream device */ - void *zmq_sock; /* zmq socket */ - int zmq_framesize; /* zmq frame size */ + unsigned char *output_buffer; /* output buffer */ + int output_buffer_size; + int output_buffer_written; + unsigned char *buf; /* bit stream buffer */ int buf_size; /* size of buffer (in number of bytes) */ long totbit; /* bit counter of bit stream */ diff --git a/libtoolame-dab/toolame.c b/libtoolame-dab/toolame.c index a626552..bea186c 100644 --- a/libtoolame-dab/toolame.c +++ b/libtoolame-dab/toolame.c @@ -154,6 +154,19 @@ int toolame_init(void) return 0; } +int toolame_finish( + unsigned char *output_buffer, + size_t output_buffer_size) +{ + bs.output_buffer = output_buffer; + bs.output_buffer_size = output_buffer_size; + bs.output_buffer_written = 0; + + close_bit_stream_w(&bs); + + return bs.output_buffer_written; +} + int toolame_enable_downmix_stereo(void) { glopts.downmix = TRUE; @@ -233,6 +246,17 @@ int toolame_set_bitrate(int brate) return err; } +int toolame_set_samplerate(long sample_rate) +{ + int s_freq = SmpFrqIndex(sample_rate, &header.version); + if (s_freq < 0) { + return s_freq; + } + + header.sampling_frequency = s_freq; + return 0; +} + int toolame_set_pad(int pad_len) { header.dab_length = pad_len; @@ -247,13 +271,20 @@ int toolame_set_pad(int pad_len) int toolame_encode_frame( short buffer[2][1152], unsigned char *xpad_data, - unsigned char *output_buffer) + unsigned char *output_buffer, + size_t output_buffer_size) { extern int minimum; const int nch = frame.nch; const int error_protection = header.error_protection; + bs.output_buffer = output_buffer; + bs.output_buffer_size = output_buffer_size; + bs.output_buffer_written = 0; + +#ifdef REFERENCECODE short *win_buf[2] = {&buffer[0][0], &buffer[1][0]}; +#endif int adb = available_bits (&header, &glopts); int lg_frame = adb / 8; @@ -503,6 +534,8 @@ int toolame_encode_frame( else { putbits (&bs, 0, 16); // FPAD is all-zero } + + return bs.output_buffer_written; } void smr_dump(double smr[2][SBLIMIT], int nch) { diff --git a/libtoolame-dab/toolame.h b/libtoolame-dab/toolame.h index d7f8198..1c29e24 100644 --- a/libtoolame-dab/toolame.h +++ b/libtoolame-dab/toolame.h @@ -7,6 +7,13 @@ /* Initialise toolame encoding library. */ int toolame_init(void); +/* Finish encoding the pending samples. + * Returns number of bytes written to output_buffer + */ +int toolame_finish( + unsigned char *output_buffer, + size_t output_buffer_size); + int toolame_enable_downmix_stereo(void); int toolame_enable_byteswap(void); @@ -20,13 +27,19 @@ int toolame_set_psy_model(int new_model); int toolame_set_bitrate(int brate); +/* Set sample rate in Hz */ +int toolame_set_samplerate(long sample_rate); + /* Enable PAD insertion from the specified file with length */ int toolame_set_pad(int pad_len); +/* Encodes one frame. Returns number of bytes written to output_buffer + */ int toolame_encode_frame( short buffer[2][1152], - unsigned char* xpad_data, - unsigned char *output_buffer); + unsigned char *xpad_data, + unsigned char *output_buffer, + size_t output_buffer_size); #endif // __TOOLAME_H_ -- cgit v1.2.3