Skip to content

Commit

Permalink
Removed global variables in the slotinfo structure
Browse files Browse the repository at this point in the history
(fixes possible thread safety problem)
  • Loading branch information
njh committed Nov 12, 2018
1 parent 44ea171 commit 86648c7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
30 changes: 13 additions & 17 deletions libtwolame/availbits.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,41 +28,37 @@
#include "availbits.h"


struct slotinfo {
FLOAT average;
FLOAT frac;
int whole;
FLOAT lag;
} slots;


/* function returns the number of available bits */
int available_bits(twolame_options * glopts)
{
frame_header *header = &glopts->header;
FLOAT average;
FLOAT frac;
int whole;
int adb;

slots.average = (1152.0 / ((FLOAT) glopts->samplerate_out / 1000.0))
* ((FLOAT) glopts->bitrate / 8.0);
average = (1152.0 / ((FLOAT) glopts->samplerate_out / 1000.0))
* ((FLOAT) glopts->bitrate / 8.0);

// fprintf(stderr,"availbits says: sampling freq is %i. version %i. bitrateindex %i slots
// %f\n",header->sampling_frequency, header->version, header->bitrate_index, slots.average);
// %f\n",header->sampling_frequency, header->version, header->bitrate_index, average);

slots.whole = (int) slots.average;
slots.frac = slots.average - (FLOAT) slots.whole;
whole = (int) average;
frac = average - (FLOAT) whole;

/* never allow padding for a VBR frame. Don't ask me why, I've forgotten why I set this */
if (slots.frac != 0 && glopts->padding && glopts->vbr == FALSE) {
if (slots.lag > (slots.frac - 1.0)) { /* no padding for this frame */
slots.lag -= slots.frac;
if (frac != 0 && glopts->padding && glopts->vbr == FALSE) {
if (glopts->slots_lag > (frac - 1.0)) { /* no padding for this frame */
glopts->slots_lag -= frac;
header->padding = 0;
} else { /* padding */
header->padding = 1;
slots.lag += (1 - slots.frac);
glopts->slots_lag += (1 - frac);
}
}

adb = slots.whole * 8;
adb = whole * 8;

return adb;
}
Expand Down
3 changes: 2 additions & 1 deletion libtwolame/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,8 @@ struct twolame_options_struct {
FLOAT scale_left;
FLOAT scale_right;


// Available Bits
FLOAT slots_lag;

// Bit allocation stuff
int lower_index;
Expand Down
2 changes: 2 additions & 0 deletions libtwolame/twolame.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ twolame_options *twolame_init(void)
newoptions->verbosity = 2;
newoptions->vbr_upper_index = 0;

newoptions->slots_lag = 0.0;

newoptions->scale = 1.0; // scaling disabled
newoptions->scale_left = 1.0; // scaling disabled
newoptions->scale_right = 1.0; // scaling disabled
Expand Down

0 comments on commit 86648c7

Please sign in to comment.