r125: This commit was manufactured by cvs2svn to create tag 'r1_1_7-last'.
[cinelerra_cv/mob.git] / hvirtual / toolame-02l / availbits.c
blob809c28e2021ae14b70b87a048489523f9b5068f4
1 /*
2 * toolame - an optimized mpeg 1/2 layer 2 audio encoder
3 * Copyright (C) 2001 Michael Cheng
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include <stdio.h>
20 #include "common.h"
21 #include "encoder.h"
22 #include "musicin.h"
23 #include "options.h"
24 #include "availbits.h"
27 struct slotinfo {
28 double average;
29 double frac;
30 int whole;
31 double lag;
32 int extra;
33 } slots;
35 /* function returns the number of available bits */
36 int available_bits (frame_header *header, options * glopts)
38 int adb;
40 slots.extra = 0; /* be default, no extra slots */
42 slots.average =
43 (1152.0 / s_freq[header->version][header->sampling_frequency]) *
44 ((double) bitrate[header->version][header->bitrate_index] / 8.0);
46 slots.whole = (int) slots.average;
47 slots.frac = slots.average - (double) slots.whole;
49 /* never allow padding for a VBR frame.
50 Don't ask me why, I've forgotten why I set this */
51 if (slots.frac != 0 && glopts->usepadbit && glopts->vbr == FALSE) {
52 if (slots.lag > (slots.frac - 1.0)) { /* no padding for this frame */
53 slots.lag -= slots.frac;
54 slots.extra = 0;
55 header->padding = 0;
56 } else { /* padding */
58 slots.extra = 1;
59 header->padding = 1;
60 slots.lag += (1 - slots.frac);
64 adb = (slots.whole + slots.extra) * 8;
66 return adb;