📄 towave.c
字号:
/*____________________________________________________________________________
FreeAmp - The Free MP3 Player
MP3 Decoder originally Copyright (C) 1995-1997 Xing Technology
Corp. http://www.xingtech.com
Portions Copyright (C) 1998-1999 EMusic.com
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.
$Id: towave.c,v 1.3 1999/10/19 07:13:09 elrod Exp $
____________________________________________________________________________*/
/* ------------------------------------------------------------------------
NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE
This file exists for reference only. It is not actually used
in the FreeAmp project. There is no need to mess with this
file. There is no need to flatten the beavers, either.
NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE
/*---- towave.c --------------------------------------------
32 bit version only
decode mpeg Layer I/II/III file using portable ANSI C decoder,
output to pcm wave file.
mod 8/19/98 decode 22 sf bands
mod 5/14/98 allow mpeg25 (dec8 not supported for mpeg25 samp rate)
mod 3/4/98 bs_trigger bs_bufbytes made signed, unsigned may
not terminate properly. Also extra test in bs_fill.
mod 8/6/96 add 8 bit output to standard decoder
ver 1.4 mods 7/18/96 32 bit and add asm option
mods 6/29/95 allow MS wave file for u-law. bugfix u-law table dec8.c
mods 2/95 add sample rate reduction, freq_limit and conversions.
add _decode8 for 8Ks output, 16bit 8bit, u-law output.
add additional control parameters to init.
add _info function
mod 5/12/95 add quick window cwinq.c
mod 5/19/95 change from stream io to handle io
mod 11/16/95 add Layer I
mod 1/5/95 integer overflow mod iup.c
ver 1.3
mod 2/5/96 portability mods
drop Tom and Gloria pcm file types
ver 2.0
mod 1/7/97 Layer 3 (float mpeg-1 only)
2/6/97 Layer 3 MPEG-2
ver 3.01 Layer III bugfix crc problem 8/18/97
ver 3.02 Layer III fix wannabe.mp3 problem 10/9/97
ver 3.03 allow mpeg 2.5 5/14/98
Decoder functions for _decode8 are defined in dec8.c. Useage
is same as regular decoder.
Towave illustrates use of decoder. Towave converts
mpeg audio files to 16 bit (short) pcm. Default pcm file
format is wave. Other formats can be accommodated by
adding alternative write_pcm_header and write_pcm_tailer
functions. The functions kbhit and getch used in towave.c
may not port to other systems.
The decoder handles all mpeg1 and mpeg2 Layer I/II bitstreams.
For compatability with the asm decoder and future C versions,
source code users are discouraged from making modifications
to the decoder proper. MS Windows applications can use wrapper
functions in a separate module if decoder functions need to
be exported.
NOTE additional control parameters.
mod 8/6/96 standard decoder adds 8 bit output
decode8 (8Ks output) convert_code:
convert_code = 4*bit_code + chan_code
bit_code: 1 = 16 bit linear pcm
2 = 8 bit (unsigned) linear pcm
3 = u-law (8 bits unsigned)
chan_code: 0 = convert two chan to mono
1 = convert two chan to mono
2 = convert two chan to left chan
3 = convert two chan to right chan
decode (standard decoder) convert_code:
0 = two chan output
1 = convert two chan to mono
2 = convert two chan to left chan
3 = convert two chan to right chan
or with 8 = 8 bit output
(other bits ignored)
decode (standard decoder) reduction_code:
0 = full sample rate output
1 = half rate
2 = quarter rate
-----------------------------------------------------------*/
#include <stdlib.h>
#include <stdio.h>
#include <float.h>
#include <math.h>
#include <string.h>
#ifdef WIN32
#include <io.h>
#endif
#include <fcntl.h> /* file open flags */
#include <sys/types.h> /* someone wants for port */
#include <sys/stat.h> /* forward slash for portability */
#include "mhead.h" /* mpeg header structure, decode protos */
#define NEED_KBHIT
#include "port.h"
// JDW
#ifdef __linux__
#include <sys/ioctl.h>
#include <sys/soundcard.h>
#include <fcntl.h>
#include <errno.h>
#endif
// JDW
/* time test Pentium only */
//#define TIME_TEST
int jdw = 0;
static char default_file[] = "TEST.MP3";
static char default_outfile[] = "TEST.WAV";
/*---- timing test ---*/
#ifdef TIME_TEST
static double tot_cycles;
static int tot_cycles_n;
unsigned int set_clock(void);
unsigned int get_clock(void);
extern unsigned int global_cycles;
#endif
/********* bitstream buffer */
#define BS_BUFBYTES 60000U
static unsigned char *bs_buffer = NULL;
static unsigned char *bs_bufptr;
static int bs_trigger;
static int bs_bufbytes;
static int handle = -1;
/********************** */
/******** pcm buffer ********/
#define PCM_BUFBYTES 60000U
static char *pcm_buffer;
static unsigned int pcm_bufbytes;
static unsigned int pcm_trigger = (PCM_BUFBYTES - 2500 * sizeof(short));
static int handout = -1;
/****************************/
static int bs_fill();
static int out_help();
static int out_mpeg_info(MPEG_HEAD * h, int bitrate_arg);
int ff_decode(char *filename,
char *fileout,
int reduction_code,
int convert_code,
int decode8_flag,
int freq_limit,
int integer_decode);
int cvt_to_wave_test();
int write_pcm_header_wave(int handout,
int samprate, int channels, int bits, int type);
int write_pcm_tailer_wave(int handout, unsigned int pcm_bytes);
/*------------------------------------------*/
main(int argc, char *argv[])
{
int i, k;
char *filename;
char *fileout;
int convert_code;
int reduction_code;
int freq_limit;
int integer_decode;
int decode8_flag; /* decode to 8KHz */
printf(
"\n file-file MPEG Layer I/II/III audio decode v3.04/32"
"\n Copyright 1995-98 Xing Technology Corp."
"\n"
"\n useage: towave mpeg_file pcm_file -Cconvert_code -Rreduction_code"
"\n -I -D8 -HELP "
);
/****** process command line args */
filename = default_file;
fileout = default_outfile;
convert_code = 0;
reduction_code = 0;
freq_limit = 24000;
integer_decode = 0;
decode8_flag = 0; /* decode to 8KHz */
for (k = 0, i = 1; i < argc; i++)
{
if (argv[i][0] != '-')
{
if (k == 0)
filename = argv[i];
if (k == 1)
fileout = argv[i];
k++;
continue;
}
switch (argv[i][1])
{
case 'h':
case 'H':
out_help();
return 0;
case 'c':
case 'C':
convert_code = atoi(argv[i] + 2);
break;
case 'r':
case 'R':
reduction_code = atoi(argv[i] + 2);
break;
case 'f':
case 'F':
freq_limit = atoi(argv[i] + 2);
break;
case 'i':
case 'I':
integer_decode = 1;
break;
case 'd':
case 'D':
if (atoi(argv[i] + 2) == 8)
decode8_flag = 1;
break;
}
}
printf("\n <press any key to stop decoder>");
/******** decode *********/
ff_decode(filename, fileout,
reduction_code, convert_code, decode8_flag,
freq_limit, integer_decode);
/*************************/
/*---- timing test --------*/
#ifdef TIME_TEST
if (tot_cycles_n)
{
// ms per frame
tot_cycles = (1.0 / 266000.0) * tot_cycles / tot_cycles_n;
if (tot_cycles > 5.0)
printf("\n ave frame time ms = %8.1lf", tot_cycles);
else
printf("\n ave frame time ms = %8.2lf", tot_cycles);
}
#endif
printf("\n");
return 0;
}
/*-------------------------------------------------------------*/
static int out_help()
{
printf("\n"
"\n -D8 option decode8 (8Ks output) convert_code:"
"\n convert_code = 4*bit_code + chan_code"
"\n bit_code: 1 = 16 bit linear pcm"
"\n 2 = 8 bit (unsigned) linear pcm"
"\n 3 = u-law (8 bits unsigned)"
"\n chan_code: 0 = convert two chan to mono"
"\n 1 = convert two chan to mono"
"\n 2 = convert two chan to left chan"
"\n 3 = convert two chan to right chan"
"\n decode (standard decoder) convert_code:"
"\n 0 = two chan output"
"\n 1 = convert two chan to mono"
"\n 2 = convert two chan to left chan"
"\n 3 = convert two chan to right chan"
"\n (higher bits ignored)"
"\n decode (standard decoder) reduction_code:"
"\n 0 = full sample rate output"
"\n 1 = half rate"
"\n 2 = quarter rate"
"\n -I option selects integer decoder"
);
return 1;
}
/*-------------------------------------------------------------*/
int ff_decode(char *filename, char *fileout,
int reduction_code, int convert_code, int decode8_flag,
int freq_limit, int integer)
{
int framebytes;
int u;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -