File: mpeg.c

package info (click to toggle)
streamripper 1.61.7-1sarge1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,216 kB
  • ctags: 757
  • sloc: sh: 8,778; ansic: 6,138; makefile: 236
file content (259 lines) | stat: -rw-r--r-- 7,258 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/* mpeg.c - jonclegg@yahoo.com
 * various ripped off mpeg stuff, mainly for finding the first good header in a mp3 chunk
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <stdio.h>
#include <string.h>
#include "mpeg.h"

#define MPG_MD_MONO 3

/**********************************************************************************
 * Public functions
 **********************************************************************************/
error_code	mpeg_find_first_header(const char* buffer, int size, int min_good_frames, int *frame_pos);
error_code	mpeg_find_last_header(const char *buffer, int size, int min_good_frames, int *frame_pos);

/**********************************************************************************
 * Private functions
 **********************************************************************************/
static int get_frame_size(mp3_header_t *mh);
static BOOL get_header(unsigned char *buff, mp3_header_t *mh);
static BOOL header_sane(mp3_header_t *mh);
static BOOL sameHeader(mp3_header_t *mh1, mp3_header_t *mh2);
static unsigned int decode_bitrate(mp3_header_t *mh);

static unsigned int bitrates[3][3][15] =
{
  {
    {0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448},
    {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384},
    {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320}
  },
  {
    {0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256},
    {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160},
    {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160}
  },
  {
    {0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256},
    {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160},
    {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160}
  }
};

static unsigned int s_freq_mult[3][4] = 
{
    {882, 960, 640, 0},
    {441, 480, 320, 0},
    {441, 480, 320, 0}
};				// From FreeAmp Xing decoder

/*
static char *mode_names[5] = {"stereo", "j-stereo", "dual-ch", "single-ch", "multi-ch"};
static char *layer_names[3] = {"I", "II", "III"};
static char *version_names[3] = {"MPEG-1", "MPEG-2 LSF", "MPEG-2.5"};
static char *version_nums[3] = {"1", "2", "2.5"};	
*/


/*
 * Finds the first header good for min_good_frames and fills header_pos with  the position 
 */
error_code mpeg_find_first_header(const char* pbuff, int size, int min_good_frames, int *header_pos)
{
	int i, n;
	unsigned char *buff = (unsigned char*)pbuff;
	unsigned char *buff2;
	mp3_header_t mh1, mh2;
	int frame_size;
	int remaining;

	for(i = 0; i < size; i++, buff++)
	{
		if (*buff == 255)
		{
			get_header(buff, &mh1);	
			if (!header_sane(&mh1))
				continue;

			frame_size = get_frame_size(&mh1);
			if (frame_size < 21)
				continue;

			buff2 = buff + frame_size;
			remaining = size - i - frame_size;
			for(n = 1; (n < min_good_frames) && (remaining >= 4); n++)
			{
				get_header(buff2, &mh2);
				if (!header_sane(&mh2)) break;
				if (!sameHeader(&mh1, &mh2)) break;

				remaining -= frame_size;
				buff2 += frame_size;
			}
			if (n == min_good_frames)
			{
				*header_pos = i;		
				return SR_SUCCESS;
			}
		}	
	}

	return SR_ERROR_CANT_FIND_MPEG_HEADER;
}

/*
 * Finds the last good frame within <buffer>, places that frame pos in <header_pos>
 */
error_code mpeg_find_last_header(const char *buffer, int size, int min_good_frames, int *header_pos)
{
	int frame_pos = 0;
	int cur = 0;


	// Keep getting frames till we run out of space
	while(mpeg_find_first_header(&buffer[cur], size-cur, min_good_frames, &frame_pos))
		cur += (frame_pos + 4);

	*header_pos = (cur-4);
	return SR_SUCCESS;
}
		

/*
 * Checks if the headers are the same
 */
BOOL sameHeader(mp3_header_t *mh1, mp3_header_t *mh2)
{
	if ((mh1->lay 			== mh2->lay) &&
	    (mh1->version 		== mh2->version) &&
	    (mh1->error_protection	== mh2->error_protection) &&
	    (mh1->bitrate_index		== mh2->bitrate_index) &&
	    (mh1->sampling_frequency	== mh2->sampling_frequency) &&
	//  (mh1->padding		== mh2->padding) &&
	    (mh1->extension		== mh2->extension) &&
	    (mh1->mode			== mh2->mode) &&
	    (mh1->mode_ext		== mh2->mode_ext) &&
	    (mh1->copyright		== mh2->copyright) &&
	    (mh1->original		== mh2->original) &&
	    (mh1->emphasis		== mh2->emphasis) &&
	    (mh1->stereo		== mh2->stereo))
		return TRUE;
	else
		return FALSE;
}            	

/*
 * Calcualtes the frame size based of a mp3_header
 */
int get_frame_size(mp3_header_t *mh)
{
	long mult = 0;

	if (decode_bitrate(mh) == 0)
		return 0;

	if (mh->lay == 1)
		return (240 * (decode_bitrate(mh) / 
	        	s_freq_mult[mh->version][mh->sampling_frequency])) * 4;
	else if (mh->lay == 2)
		mult = 2880;
	else if (mh->lay == 3)
	{
		if (mh->version == 0)
			mult = 2880;
		else if (mh->version == 1)
			mult = 1440;
		else if (mh->version == 2)	// MPEG 2.5	
			mult = 2880;
	}

	return mult * decode_bitrate(mh) / 
	       s_freq_mult[mh->version][mh->sampling_frequency] + mh->padding;
}

/* 
 * Extracts the mp3 header from <buff>
 */
BOOL get_header(unsigned char *buff, mp3_header_t *mh)
{
	unsigned char *buffer;
 	size_t 	temp;


	memset(mh, 0, sizeof(mh)); 
	buffer = buff;
	temp = ((buffer[0] << 4) & 0xFF0) | ((buffer[1] >> 4) & 0xE);
  
	if (temp != 0xFFE) 
	{
     		return FALSE;
	} 
	else 
	{
		switch ((buffer[1] >> 3 & 0x3))
		{
			case 3:
				mh->version = 0;
	  			break;
			case 2:
	  			mh->version = 1;
	  			break;
			case 0:
	  			mh->version = 2;
	  			break;
			default:
	  			return -1;
	  			break;
      		}
      		mh->lay = 4 - ((buffer[1] >> 1) & 0x3);
      		mh->error_protection = !(buffer[1] & 0x1);
      		mh->bitrate_index = (buffer[2] >> 4) & 0x0F;
      		mh->sampling_frequency = (buffer[2] >> 2) & 0x3;
      		mh->padding = (buffer[2] >> 1) & 0x01;
      		mh->extension = buffer[2] & 0x01;
      		mh->mode = (buffer[3] >> 6) & 0x3;
      		mh->mode_ext = (buffer[3] >> 4) & 0x03;
      		mh->copyright = (buffer[3] >> 3) & 0x01;
      		mh->original = (buffer[3] >> 2) & 0x1;
      		mh->emphasis = (buffer[3]) & 0x3;
      		mh->stereo = (mh->mode == MPG_MD_MONO) ? 1 : 2;

		return TRUE;
    }
}

unsigned int decode_bitrate(mp3_header_t *mh)
{
	return bitrates[mh->version][mh->lay-1][mh->bitrate_index];
}

/*
 * Checks if the header is even somewhat valid
 */
BOOL header_sane(mp3_header_t *mh)
{
	if ( ((mh->lay > 1) && (mh->lay < 4)) &&
	     ((mh->bitrate_index > 0) && (mh->bitrate_index < 15)) &&
	     ((mh->sampling_frequency >= 0) && (mh->sampling_frequency < 3)) &&
	     ((mh->version >= 0) && (mh->version < 3)))
		return TRUE;

	return FALSE;	
}