~akirad/cinecutie/trunk

« back to all changes in this revision

Viewing changes to toolame-02l/availbits.c

  • Committer: Paolo Rampino
  • Date: 2010-02-17 19:46:21 UTC
  • Revision ID: git-v1:c39ff77ffa6ae08441c12e7d7f54e3897ddde7f1
Initial Merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * toolame - an optimized mpeg 1/2 layer 2 audio encoder
 
3
 * Copyright (C) 2001 Michael Cheng
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 * 
 
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
 
18
 */
 
19
#include <stdio.h>
 
20
#include "common.h"
 
21
#include "encoder.h"
 
22
#include "musicin.h"
 
23
#include "options.h"
 
24
#include "availbits.h"
 
25
 
 
26
 
 
27
struct slotinfo {
 
28
  double average;
 
29
  double frac;
 
30
  int whole;
 
31
  double lag;
 
32
  int extra;
 
33
} slots;
 
34
 
 
35
/* function returns the number of available bits */
 
36
int available_bits (frame_header *header, options * glopts)
 
37
{
 
38
  int adb;
 
39
 
 
40
  slots.extra = 0;              /* be default, no extra slots */
 
41
 
 
42
  slots.average =
 
43
    (1152.0 / s_freq[header->version][header->sampling_frequency]) *
 
44
    ((double) bitrate[header->version][header->bitrate_index] / 8.0);
 
45
 
 
46
  slots.whole = (int) slots.average;
 
47
  slots.frac = slots.average - (double) slots.whole;
 
48
 
 
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 */
 
57
 
 
58
      slots.extra = 1;
 
59
      header->padding = 1;
 
60
      slots.lag += (1 - slots.frac);
 
61
    }
 
62
  }
 
63
 
 
64
  adb = (slots.whole + slots.extra) * 8;
 
65
 
 
66
  return adb;
 
67
}