📄 towave.c
字号:
/*-----------------------*/
printf("\nMPEG input file: %s", filename);
printf("\n output file: %s", fileout);
/*-----select decoder --------------*/
audio = audio_table[integer & 1][0];
in_bytes = out_bytes = 0;
#ifndef __TMS320C55X__
pcm_buffer = NULL;
#endif
pcm_bufbytes = 0;
/*-----------------------*/
#ifndef __TMS320C55X__
bs_buffer = NULL;
#endif
handle = NULL;
bs_bufbytes = 0;
bs_bufptr = bs_buffer;
bs_trigger = 2500;
/*------ open mpeg file --------*/
handle = fopen(filename, "rb");
if (handle == NULL)
{
printf("\n CANNOT_OPEN_INPUT_FILE\n");
goto abort;
}
/*--- allocate bs buffer ----*/
#ifndef __TMS320C55X__
bs_buffer = malloc(BS_BUFBYTES);
if (bs_buffer == NULL)
{
printf("\n CANNOT_ALLOCATE_BUFFER\n");
goto abort;
}
#endif
/*--- fill bs buffer ----*/
if (!bs_fill())
goto abort;
/*---- parse mpeg header -------*/
framebytes = head_info2(bs_buffer, bs_bufbytes, &head, &bitrate);
if (framebytes == 0)
{
printf("\n BAD OR UNSUPPORTED MPEG FILE\n");
goto abort;
}
/*--- display mpeg info --*/
out_mpeg_info(&head, bitrate);
/*---- allocate pcm buffer --------*/
#ifndef __TMS320C55X__
pcm_buffer = malloc(PCM_BUFBYTES);
if (pcm_buffer == NULL)
{
printf("\n CANNOT ALLOCATE PCM BUFFER\n");
goto abort;
}
#endif
/*---- init decoder -------*/
if (!audio.decode_init(&head, framebytes,
reduction_code, 0, convert_code, freq_limit))
{
printf("\n DECODER INIT FAIL \n");
goto abort;
}
/*---- get info -------*/
audio.decode_info(&decinfo);
/*---- info display -------*/
printf("\n output samprate = %6ld", decinfo.samprate);
printf("\n output channels = %6d", decinfo.channels);
printf("\n output bits = %6d", decinfo.bits);
printf("\n output type = %6d", decinfo.type);
/*--- init wave converter ---*/
cvt_to_wave_init(decinfo.bits);
printf("\n");
/*----- DECODE -----*/
for (u = 0;;)
{
if (!bs_fill())
break;
if (bs_bufbytes < framebytes)
break; /* end of file */
x = audio.decode(bs_bufptr, (short *) (pcm_buffer + pcm_bufbytes));
if (x.in_bytes <= 0)
{
printf("\n BAD SYNC IN MPEG FILE\n");
break;
}
bs_bufptr += x.in_bytes;
bs_bufbytes -= x.in_bytes;
pcm_bufbytes += x.out_bytes;
u++;
pcm_bufbytes = cvt_to_wave((unsigned char *)pcm_buffer, pcm_bufbytes);
frame_cnt++;
printf("Completed frame %d\n", frame_cnt);
out_bytes += pcm_bufbytes;
pcm_bufbytes = 0;
if (kbhit())
break;
in_bytes += x.in_bytes;
if (u == FRAME_COUNT)
break;
}
if (pcm_bufbytes > 0)
{
pcm_bufbytes = cvt_to_wave((unsigned char *)pcm_buffer, pcm_bufbytes);
out_bytes += pcm_bufbytes;
pcm_bufbytes = 0;
}
abort:
fclose(handle);
free(bs_buffer);
free(pcm_buffer);
while (kbhit())
getch(); /* purge key board */
return 1;
}
/*-------------------------------------------------------------*/
static int bs_fill()
{
unsigned int nread;
if (bs_bufbytes < 0)
bs_bufbytes = 0; // signed var could be negative
if (bs_bufbytes < bs_trigger)
{
memmove(bs_buffer, bs_bufptr, bs_bufbytes);
nread = fread(bs_buffer+bs_bufbytes, 1, BS_BUFBYTES-bs_bufbytes,
handle);
if (nread+1 == 0)
{
/*-- test for -1 = error --*/
bs_trigger = 0;
printf("\n FILE_READ_ERROR\n");
return 0;
}
bs_bufbytes += nread;
bs_bufptr = bs_buffer;
}
return 1;
}
/*------------------------------------------------------------------*/
static int out_mpeg_info(MPEG_HEAD * h, INT32 bitrate_arg) /* info only */
{
int bitrate;
unsigned int samprate;
static char *Layer_msg[] =
{"INVALID", "III", "II", "I"};
static char *mode_msg[] =
{"STEREO", "JOINT", "DUAL", "MONO"};
static unsigned int sr_table[8] =
{22050L, 24000L, 16000L, 1L,
44100L, 48000L, 32000L, 1L};
bitrate = bitrate_arg / 1000;
printf("\n Layer %s ", Layer_msg[h->option]);
printf(" %s ", mode_msg[h->mode]);
samprate = sr_table[4 * h->id + h->sr_index];
if ((h->sync & 1) == 0)
samprate = samprate / 2; // mpeg25
printf(" %u ", samprate);
printf(" %dKb ", bitrate);
if ((h->mode == 1) && (h->option != 1))
printf(" %d stereo bands ", 4 + 4 * h->mode_ext);
if (h->prot == 0)
printf(" (CRC)");
return 0;
}
/*------------------------------------------------------------------*/
int dummy()
{
return 0;
}
/* ***********************************************************
* THIS PROGRAM IS PROVIDED "AS IS". TI MAKES NO WARRANTIES OR
* REPRESENTATIONS, EITHER EXPRESS, IMPLIED OR STATUTORY,
* INCLUDING ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE, LACK OF VIRUSES, ACCURACY OR
* COMPLETENESS OF RESPONSES, RESULTS AND LACK OF NEGLIGENCE.
* TI DISCLAIMS ANY WARRANTY OF TITLE, QUIET ENJOYMENT, QUIET
* POSSESSION, AND NON-INFRINGEMENT OF ANY THIRD PARTY
* INTELLECTUAL PROPERTY RIGHTS WITH REGARD TO THE PROGRAM OR
* YOUR USE OF THE PROGRAM.
*
* IN NO EVENT SHALL TI BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
* CONSEQUENTIAL OR INDIRECT DAMAGES, HOWEVER CAUSED, ON ANY
* THEORY OF LIABILITY AND WHETHER OR NOT TI HAS BEEN ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGES, ARISING IN ANY WAY OUT
* OF THIS AGREEMENT, THE PROGRAM, OR YOUR USE OF THE PROGRAM.
* EXCLUDED DAMAGES INCLUDE, BUT ARE NOT LIMITED TO, COST OF
* REMOVAL OR REINSTALLATION, COMPUTER TIME, LABOR COSTS, LOSS
* OF GOODWILL, LOSS OF PROFITS, LOSS OF SAVINGS, OR LOSS OF
* USE OR INTERRUPTION OF BUSINESS. IN NO EVENT WILL TI'S
* AGGREGATE LIABILITY UNDER THIS AGREEMENT OR ARISING OUT OF
* YOUR USE OF THE PROGRAM EXCEED FIVE HUNDRED DOLLARS
* (U.S.$500).
*
* Unless otherwise stated, the Program written and copyrighted
* by Texas Instruments is distributed as "freeware". You may,
* only under TI's copyright in the Program, use and modify the
* Program without any charge or restriction. You may
* distribute to third parties, provided that you transfer a
* copy of this license to the third party and the third party
* agrees to these terms by its first use of the Program. You
* must reproduce the copyright notice and any other legend of
* ownership on each copy or partial copy, of the Program.
*
* You acknowledge and agree that the Program contains
* copyrighted material, trade secrets and other TI proprietary
* information and is protected by copyright laws,
* international copyright treaties, and trade secret laws, as
* well as other intellectual property laws. To protect TI's
* rights in the Program, you agree not to decompile, reverse
* engineer, disassemble or otherwise translate any object code
* versions of the Program to a human-readable form. You agree
* that in no event will you alter, remove or destroy any
* copyright notice included in the Program. TI reserves all
* rights not specifically granted under this license. Except
* as specifically provided herein, nothing in this agreement
* shall be construed as conferring by implication, estoppel,
* or otherwise, upon you, any license or other right under any
* TI patents, copyrights or trade secrets.
*
* You may not use the Program in non-TI devices.
* ********************************************************* */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -