2007-03-13 02:02:28 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include "mpg123.h"
|
|
|
|
#include "mpglib.h"
|
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
struct parameter param = { 1, 1, 0, 0 };
|
2007-03-13 02:02:28 +00:00
|
|
|
|
|
|
|
int tabsel_123[2][3][16] = {
|
2007-03-29 22:31:56 +00:00
|
|
|
{{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,}},
|
2007-03-13 02:02:28 +00:00
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
{{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,}}
|
2007-03-13 02:02:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
long freqs[9] = { 44100, 48000, 32000,
|
2007-03-29 22:31:56 +00:00
|
|
|
22050, 24000, 16000,
|
|
|
|
11025, 12000, 8000
|
|
|
|
};
|
2007-03-13 02:02:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
static void get_II_stuff(struct frame *fr)
|
|
|
|
{
|
2007-03-29 22:31:56 +00:00
|
|
|
static int translate[3][2][16] = { {{0, 2, 2, 2, 2, 2, 2, 0, 0, 0, 1, 1, 1, 1, 1, 0},
|
|
|
|
{0, 2, 2, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0}},
|
|
|
|
{{0, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0},
|
|
|
|
{0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
|
|
|
|
{{0, 3, 3, 3, 3, 3, 3, 0, 0, 0, 1, 1, 1, 1, 1, 0},
|
|
|
|
{0, 3, 3, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0}}
|
|
|
|
};
|
|
|
|
|
|
|
|
int table, sblim;
|
|
|
|
static struct al_table *tables[5] = { alloc_0, alloc_1, alloc_2, alloc_3, alloc_4 };
|
|
|
|
static int sblims[5] = { 27, 30, 8, 12, 30 };
|
|
|
|
|
|
|
|
if (fr->lsf)
|
|
|
|
table = 4;
|
|
|
|
else
|
|
|
|
table = translate[fr->sampling_frequency][2 - fr->stereo][fr->bitrate_index];
|
|
|
|
sblim = sblims[table];
|
|
|
|
|
|
|
|
fr->alloc = tables[table];
|
|
|
|
fr->II_sblimit = sblim;
|
2007-03-13 02:02:28 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define HDRCMPMASK 0xfffffd00
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
int head_check(unsigned long head)
|
|
|
|
{
|
2007-03-29 22:31:56 +00:00
|
|
|
if ((head & 0xffe00000) != 0xffe00000)
|
|
|
|
return FALSE;
|
|
|
|
if (!((head >> 17) & 3))
|
|
|
|
return FALSE;
|
|
|
|
if (((head >> 12) & 0xf) == 0xf)
|
|
|
|
return FALSE;
|
|
|
|
if (((head >> 10) & 0x3) == 0x3)
|
|
|
|
return FALSE;
|
|
|
|
return TRUE;
|
2007-03-13 02:02:28 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* the code a header and write the information
|
|
|
|
* into the frame structure
|
|
|
|
*/
|
2007-03-29 22:31:56 +00:00
|
|
|
int decode_header(struct frame *fr, unsigned long newhead)
|
2007-03-13 02:02:28 +00:00
|
|
|
{
|
2007-03-29 22:31:56 +00:00
|
|
|
if (newhead & (1 << 20)) {
|
|
|
|
fr->lsf = (newhead & (1 << 19)) ? 0x0 : 0x1;
|
|
|
|
fr->mpeg25 = 0;
|
|
|
|
} else {
|
|
|
|
fr->lsf = 1;
|
|
|
|
fr->mpeg25 = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fr->lay = 4 - ((newhead >> 17) & 3);
|
|
|
|
if (((newhead >> 10) & 0x3) == 0x3) {
|
|
|
|
debug_printf("%d Stream error\n", __LINE__);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
if (fr->mpeg25) {
|
|
|
|
fr->sampling_frequency = 6 + ((newhead >> 10) & 0x3);
|
|
|
|
} else
|
|
|
|
fr->sampling_frequency = ((newhead >> 10) & 0x3) + (fr->lsf * 3);
|
|
|
|
fr->error_protection = ((newhead >> 16) & 0x1) ^ 0x1;
|
|
|
|
|
|
|
|
if (fr->mpeg25) /* allow Bitrate change for 2.5 ... */
|
|
|
|
fr->bitrate_index = ((newhead >> 12) & 0xf);
|
|
|
|
|
|
|
|
fr->bitrate_index = ((newhead >> 12) & 0xf);
|
|
|
|
fr->padding = ((newhead >> 9) & 0x1);
|
|
|
|
fr->extension = ((newhead >> 8) & 0x1);
|
|
|
|
fr->mode = ((newhead >> 6) & 0x3);
|
|
|
|
fr->mode_ext = ((newhead >> 4) & 0x3);
|
|
|
|
fr->copyright = ((newhead >> 3) & 0x1);
|
|
|
|
fr->original = ((newhead >> 2) & 0x1);
|
|
|
|
fr->emphasis = newhead & 0x3;
|
|
|
|
|
|
|
|
fr->stereo = (fr->mode == MPG_MD_MONO) ? 1 : 2;
|
|
|
|
|
|
|
|
if (!fr->bitrate_index) {
|
|
|
|
debug_printf("%d Free format not supported.\n", __LINE__);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (fr->lay) {
|
|
|
|
case 1:
|
2007-03-13 02:02:28 +00:00
|
|
|
#if 0
|
|
|
|
fr->do_layer = do_layer1;
|
2007-03-29 22:31:56 +00:00
|
|
|
fr->jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ? (fr->mode_ext << 2) + 4 : 32;
|
|
|
|
fr->framesize = (long) tabsel_123[fr->lsf][0][fr->bitrate_index] * 12000;
|
|
|
|
fr->framesize /= freqs[fr->sampling_frequency];
|
|
|
|
fr->framesize = ((fr->framesize + fr->padding) << 2) - 4;
|
2007-03-13 02:02:28 +00:00
|
|
|
#else
|
2007-03-29 22:31:56 +00:00
|
|
|
debug_printf("%d Layer 1 not supported!\n", __LINE__);
|
2007-03-13 02:02:28 +00:00
|
|
|
#endif
|
2007-03-29 22:31:56 +00:00
|
|
|
break;
|
|
|
|
case 2:
|
2007-03-13 02:02:28 +00:00
|
|
|
#if 0
|
|
|
|
fr->do_layer = do_layer2;
|
2007-03-29 22:31:56 +00:00
|
|
|
get_II_stuff(fr);
|
|
|
|
fr->jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ? (fr->mode_ext << 2) + 4 : fr->II_sblimit;
|
|
|
|
fr->framesize = (long) tabsel_123[fr->lsf][1][fr->bitrate_index] * 144000;
|
|
|
|
fr->framesize /= freqs[fr->sampling_frequency];
|
|
|
|
fr->framesize += fr->padding - 4;
|
2007-03-13 02:02:28 +00:00
|
|
|
#else
|
2007-03-29 22:31:56 +00:00
|
|
|
debug_printf("%d Layer 2 not supported!\n", __LINE__);
|
2007-03-13 02:02:28 +00:00
|
|
|
#endif
|
2007-03-29 22:31:56 +00:00
|
|
|
break;
|
|
|
|
case 3:
|
2007-03-13 02:02:28 +00:00
|
|
|
#if 0
|
2007-03-29 22:31:56 +00:00
|
|
|
fr->do_layer = do_layer3;
|
|
|
|
if (fr->lsf)
|
|
|
|
ssize = (fr->stereo == 1) ? 9 : 17;
|
|
|
|
else
|
|
|
|
ssize = (fr->stereo == 1) ? 17 : 32;
|
2007-03-13 02:02:28 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if 0
|
2007-03-29 22:31:56 +00:00
|
|
|
if (fr->error_protection)
|
|
|
|
ssize += 2;
|
2007-03-13 02:02:28 +00:00
|
|
|
#endif
|
2007-03-29 22:31:56 +00:00
|
|
|
fr->framesize = (long) tabsel_123[fr->lsf][2][fr->bitrate_index] * 144000;
|
|
|
|
fr->framesize /= freqs[fr->sampling_frequency] << (fr->lsf);
|
|
|
|
fr->framesize = fr->framesize + fr->padding - 4;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
debug_printf("%d Sorry, unknown layer type.\n", __LINE__);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
return 1;
|
2007-03-13 02:02:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
void print_header(struct frame *fr)
|
|
|
|
{
|
|
|
|
static char *modes[4] = { "Stereo", "Joint-Stereo", "Dual-Channel", "Single-Channel" };
|
2007-03-29 22:31:56 +00:00
|
|
|
static char *layers[4] = { "Unknown", "I", "II", "III" };
|
2007-03-13 02:02:28 +00:00
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
debug_printf("MPEG %s, Layer: %s, Freq: %ld, mode: %s, modext: %d, BPF : %d\n",
|
|
|
|
fr->mpeg25 ? "2.5" : (fr->lsf ? "2.0" : "1.0"),
|
|
|
|
layers[fr->lay], freqs[fr->sampling_frequency], modes[fr->mode], fr->mode_ext, fr->framesize + 4);
|
2007-03-14 18:53:32 +00:00
|
|
|
debug_printf("Channels: %d, copyright: %s, original: %s, CRC: %s, emphasis: %d.\n",
|
2007-03-29 22:31:56 +00:00
|
|
|
fr->stereo, fr->copyright ? "Yes" : "No",
|
|
|
|
fr->original ? "Yes" : "No", fr->error_protection ? "Yes" : "No", fr->emphasis);
|
2007-03-14 18:53:32 +00:00
|
|
|
debug_printf("Bitrate: %d Kbits/s, Extension value: %d\n",
|
2007-03-29 22:31:56 +00:00
|
|
|
tabsel_123[fr->lsf][fr->lay - 1][fr->bitrate_index], fr->extension);
|
2007-03-13 02:02:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void print_header_compact(struct frame *fr)
|
|
|
|
{
|
|
|
|
static char *modes[4] = { "stereo", "joint-stereo", "dual-channel", "mono" };
|
2007-03-29 22:31:56 +00:00
|
|
|
static char *layers[4] = { "Unknown", "I", "II", "III" };
|
|
|
|
|
2007-03-14 18:53:32 +00:00
|
|
|
debug_printf("MPEG %s layer %s, %d kbit/s, %ld Hz %s\n",
|
2007-03-29 22:31:56 +00:00
|
|
|
fr->mpeg25 ? "2.5" : (fr->lsf ? "2.0" : "1.0"),
|
|
|
|
layers[fr->lay],
|
|
|
|
tabsel_123[fr->lsf][fr->lay - 1][fr->bitrate_index], freqs[fr->sampling_frequency], modes[fr->mode]);
|
2007-03-13 02:02:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
unsigned int getbits(struct mpstr *mp, int number_of_bits)
|
|
|
|
{
|
2007-03-29 22:31:56 +00:00
|
|
|
unsigned long rval;
|
2007-03-13 02:02:28 +00:00
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
if (!number_of_bits)
|
|
|
|
return 0;
|
2007-03-13 02:02:28 +00:00
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
{
|
|
|
|
rval = (mp->worksample).wordpointer[0];
|
|
|
|
rval <<= 8;
|
|
|
|
rval |= (mp->worksample).wordpointer[1];
|
|
|
|
rval <<= 8;
|
|
|
|
rval |= (mp->worksample).wordpointer[2];
|
|
|
|
rval <<= (mp->worksample).bitindex;
|
|
|
|
rval &= 0xffffff;
|
2007-03-13 02:02:28 +00:00
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
(mp->worksample).bitindex += number_of_bits;
|
2007-03-13 02:02:28 +00:00
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
rval >>= (24 - number_of_bits);
|
2007-03-13 02:02:28 +00:00
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
(mp->worksample).wordpointer += ((mp->worksample).bitindex >> 3);
|
|
|
|
(mp->worksample).bitindex &= 7;
|
|
|
|
}
|
|
|
|
return rval;
|
2007-03-13 02:02:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int getbits_fast(struct mpstr *mp, int number_of_bits)
|
|
|
|
{
|
2007-03-29 22:31:56 +00:00
|
|
|
unsigned long rval;
|
|
|
|
|
|
|
|
{
|
|
|
|
rval = (mp->worksample).wordpointer[0];
|
|
|
|
rval <<= 8;
|
|
|
|
rval |= (mp->worksample).wordpointer[1];
|
|
|
|
rval <<= (mp->worksample).bitindex;
|
|
|
|
rval &= 0xffff;
|
|
|
|
(mp->worksample).bitindex += number_of_bits;
|
|
|
|
|
|
|
|
rval >>= (16 - number_of_bits);
|
|
|
|
|
|
|
|
(mp->worksample).wordpointer += ((mp->worksample).bitindex >> 3);
|
|
|
|
(mp->worksample).bitindex &= 7;
|
|
|
|
}
|
|
|
|
return rval;
|
2007-03-13 02:02:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int get1bit(struct mpstr *mp)
|
|
|
|
{
|
2007-03-29 22:31:56 +00:00
|
|
|
unsigned char rval;
|
2007-03-13 02:02:28 +00:00
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
rval = *((mp->worksample).wordpointer) << (mp->worksample).bitindex;
|
2007-03-13 02:02:28 +00:00
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
(mp->worksample).bitindex++;
|
|
|
|
(mp->worksample).wordpointer += ((mp->worksample).bitindex >> 3);
|
|
|
|
(mp->worksample).bitindex &= 7;
|
2007-03-13 02:02:28 +00:00
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
return rval >> 7;
|
2007-03-13 02:02:28 +00:00
|
|
|
}
|