~ubuntu-branches/ubuntu/wily/ecasound/wily-proposed

« back to all changes in this revision

Viewing changes to libecasound/layer.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghedini
  • Date: 2011-05-12 17:58:03 UTC
  • Revision ID: james.westby@ubuntu.com-20110512175803-zy3lodjecabt9r3v
Tags: upstream-2.8.0
ImportĀ upstreamĀ versionĀ 2.8.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* layer.cpp
 
2
 
 
3
  Created by SMF aka Antoine Laydier <laydier@usa.net>.
 
4
  Minor modifications by Kai Vehmanen <k@eca.cx>.
 
5
 
 
6
  This program is free software; you can redistribute it and/or modify
 
7
  it under the terms of the GNU General Public License as published by
 
8
  the Free Software Foundation; either version 2 of the License, or
 
9
  (at your option) any later version.
 
10
 
 
11
  This program is distributed in the hope that it will be useful,
 
12
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
  GNU General Public License for more details.
 
15
 
 
16
  You should have received a copy of the GNU General Public License
 
17
  along with this program; if not, write to the Free Software
 
18
  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
19
 
 
20
 */
 
21
 
 
22
/*=============================================================================
 
23
  HEADERs
 
24
 =============================================================================*/
 
25
#include <stdio.h>
 
26
#include <stdlib.h>
 
27
#include <sys/stat.h>
 
28
#include <unistd.h>
 
29
#include "layer.h"
 
30
 
 
31
/*=============================================================================
 
32
 Class : Layers
 
33
 =============================================================================*/
 
34
 
 
35
const int Layer::MPG_MD_STEREO        = 0;
 
36
const int Layer::MPG_MD_JOINT_STEREO  = 1;
 
37
const int Layer::MPG_MD_DUAL_CHANNEL  = 2;
 
38
const int Layer::MPG_MD_MONO          = 3;
 
39
 
 
40
const int Layer::MPG_MD_LR_LR = 0;
 
41
const int Layer::MPG_MD_LR_I  = 1;
 
42
const int Layer::MPG_MD_MS_LR = 2;
 
43
const int Layer::MPG_MD_MS_I  = 3;
 
44
 
 
45
const char *Layer::mode_names[5] = {"stereo", "j-stereo", "dual-ch",
 
46
                                    "single-ch", "multi-ch"};
 
47
const char *Layer::layer_names[3] = {"I", "II", "III"};
 
48
const char *Layer::version_names[3] = {"MPEG-1", "MPEG-2 LSF", "MPEG-2.5"};
 
49
const char *Layer::version_nums[3] = {"1", "2", "2.5"};
 
50
const unsigned int Layer::bitrates[3][3][15] =
 
51
{
 
52
  {
 
53
    {0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448},
 
54
    {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384},
 
55
    {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320}
 
56
  },
 
57
  {
 
58
    {0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256},
 
59
    {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160},
 
60
    {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160}
 
61
  },
 
62
  {
 
63
    {0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256},
 
64
    {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160},
 
65
    {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160}
 
66
  }
 
67
};
 
68
 
 
69
const unsigned int Layer::s_freq[3][4] =
 
70
{
 
71
  {44100, 48000, 32000, 0},
 
72
  {22050, 24000, 16000, 0},
 
73
  {11025, 8000, 8000, 0}
 
74
};
 
75
 
 
76
const char * Layer::mode_name(void) {  return (Layer::mode_names[mode_rep]); }
 
77
const char * Layer::layer_name(void) {  return (Layer::layer_names[lay_rep - 1]); }
 
78
const char * Layer::version_name(void) {  return (Layer::version_names[version_rep]); }
 
79
const char * Layer::version_num(void) {  return (Layer::version_nums[version_rep]); }
 
80
int Layer::mode(void) { return(mode_rep); }
 
81
unsigned int Layer::bitrate(void) 
 
82
  { return (Layer::bitrates[version_rep][lay_rep - 1][bitrate_index_rep]); }
 
83
unsigned int Layer::sfreq(void)
 
84
  {  return (Layer::s_freq[version_rep][sampling_frequency_rep]); }
 
85
unsigned long Layer::length(void) { return  bitrate() ? (fileSize_rep / (unsigned long)bitrate() /125) : 0; }
 
86
unsigned int Layer::pcmPerFrame(void) { return pcm_rep; }
 
87
 
 
88
bool Layer::get(const char* filename)
 
89
{
 
90
  unsigned char *buff = new unsigned char[1024];
 
91
  unsigned char *buffer;
 
92
  size_t temp;
 
93
  size_t readsize;
 
94
  struct stat buf;
 
95
  FILE *file;
 
96
 
 
97
 // --
 
98
 // 22.3.2000 - added the second parameter for getting 
 
99
 // around FILE* compatibility issues, k@eca.cx 
 
100
 
 
101
  stat(filename, &buf);
 
102
  fileSize_rep = (unsigned long)buf.st_size;
 
103
 
 
104
  /* Theoretically reading 1024 instead of just 4 means a performance hit
 
105
   * if we transfer over net filesystems... However, no filesystem I know
 
106
   * of uses block sizes under 1024 bytes.
 
107
   */
 
108
  file = fopen(filename,"r");
 
109
  if (!file) return(false);
 
110
  
 
111
  fseek(file, 0, SEEK_SET);
 
112
  readsize = fread(buff, 1, 1024, file);
 
113
  fclose(file);
 
114
  readsize -= 4;
 
115
  if (readsize <= 0) {
 
116
    delete[] buff;
 
117
    return (false);
 
118
  }
 
119
  
 
120
  buffer = buff-1;
 
121
  
 
122
  /* Scan through the block looking for a header */
 
123
  do {
 
124
    buffer++;
 
125
    temp = ((buffer[0] << 4) & 0xFF0) | ((buffer[1] >> 4) & 0xE);
 
126
  } while ((temp != 0xFFE) && ((size_t)(buffer-buff)<readsize));
 
127
 
 
128
  if (temp != 0xFFE) {
 
129
    delete[] buff;
 
130
    return (false);
 
131
  } else {
 
132
    switch ((buffer[1] >> 3 & 0x3)) {
 
133
    case 3:
 
134
      version_rep = 0;
 
135
      break;
 
136
    case 2:
 
137
      version_rep = 1;
 
138
      break;
 
139
    case 0:
 
140
      version_rep = 2;
 
141
      break;
 
142
    default:
 
143
      delete[] buff;
 
144
      return (false);
 
145
    }
 
146
    lay_rep = 4 - ((buffer[1] >> 1) & 0x3);
 
147
    error_protection_rep = !(buffer[1] & 0x1);
 
148
    bitrate_index_rep = (buffer[2] >> 4) & 0x0F;
 
149
    sampling_frequency_rep = (buffer[2] >> 2) & 0x3;
 
150
    padding_rep = (buffer[2] >> 1) & 0x01;
 
151
    extension_rep = buffer[2] & 0x01;
 
152
    mode_rep = (buffer[3] >> 6) & 0x3;
 
153
    mode_ext_rep = (buffer[3] >> 4) & 0x03;
 
154
    copyright_rep = (buffer[3] >> 3) & 0x01;
 
155
    original_rep = (buffer[3] >> 2) & 0x1;
 
156
    emphasis_rep = (buffer[3]) & 0x3;
 
157
    stereo_rep = (mode_rep == Layer::MPG_MD_MONO) ? 1 : 2;
 
158
 
 
159
    // Added by Cp
 
160
    pcm_rep = 32;
 
161
    if (lay_rep == 3) {
 
162
      pcm_rep *= 18;
 
163
      if (version_rep == 0)
 
164
        pcm_rep *= 2;
 
165
    }
 
166
    else{
 
167
      pcm_rep *= 12;
 
168
      if (lay_rep == 2)
 
169
        pcm_rep *= 3;
 
170
    }
 
171
 
 
172
    delete[] buff;
 
173
    return (true);
 
174
  }
 
175
}