00001 /* 00002 * toolame - an optimized mpeg 1/2 layer 2 audio encoder 00003 * Copyright (C) 2001 Michael Cheng 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License as published by 00007 * the Free Software Foundation; either version 2 of the License, or 00008 * (at your option) any later version. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU General Public License 00016 * along with this program; if not, write to the Free Software 00017 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00018 */ 00019 #include <stdio.h> 00020 #include "common.h" 00021 #include "encoder.h" 00022 #include "musicin.h" 00023 #include "options.h" 00024 #include "availbits.h" 00025 00026 00027 struct slotinfo { 00028 double average; 00029 double frac; 00030 int whole; 00031 double lag; 00032 int extra; 00033 } slots; 00034 00035 /* function returns the number of available bits */ 00036 int available_bits (frame_header *header, options * glopts) 00037 { 00038 int adb; 00039 00040 slots.extra = 0; /* be default, no extra slots */ 00041 00042 slots.average = 00043 (1152.0 / s_freq[header->version][header->sampling_frequency]) * 00044 ((double) bitrate[header->version][header->bitrate_index] / 8.0); 00045 00046 slots.whole = (int) slots.average; 00047 slots.frac = slots.average - (double) slots.whole; 00048 00049 /* never allow padding for a VBR frame. 00050 Don't ask me why, I've forgotten why I set this */ 00051 if (slots.frac != 0 && glopts->usepadbit && glopts->vbr == FALSE) { 00052 if (slots.lag > (slots.frac - 1.0)) { /* no padding for this frame */ 00053 slots.lag -= slots.frac; 00054 slots.extra = 0; 00055 header->padding = 0; 00056 } else { /* padding */ 00057 00058 slots.extra = 1; 00059 header->padding = 1; 00060 slots.lag += (1 - slots.frac); 00061 } 00062 } 00063 00064 adb = (slots.whole + slots.extra) * 8; 00065 00066 return adb; 00067 }
1.5.5