⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 get_audio.c

📁 音频编码
💻 C
📖 第 1 页 / 共 4 页
字号:
    return is_aiff;}/************************************************************************** parse_file_header** PURPOSE: Read the header from a bytestream.  Try to determine whether*		   it's a WAV file or AIFF without rewinding, since rewind*		   doesn't work on pipes and there's a good chance we're reading*		   from stdin (otherwise we'd probably be using libsndfile).** When this function returns, the file offset will be positioned at the* beginning of the sound data.*************************************************************************/voidparse_file_header(lame_global_flags * gfp, FILE * sf){    int     type = Read32BitsHighLow(sf);    /*       DEBUGF(       "First word of input stream: %08x '%4.4s'\n", type, (char*) &type);      */    count_samples_carefully = 0;    input_format = sf_raw;    if (type == WAV_ID_RIFF) {        /* It's probably a WAV file */        if (parse_wave_header(gfp, sf)) {            input_format = sf_wave;            count_samples_carefully = 1;        } else {	    if( silent < 10 ) {		fprintf( stderr, "Warning: corrupt or unsupported WAVE format\n"); 	    }        }    }    else if (type == IFF_ID_FORM) {        /* It's probably an AIFF file */        if (parse_aiff_header(gfp, sf)) {            input_format = sf_aiff;            count_samples_carefully = 1;        }    }    if (input_format == sf_raw) {        /*           ** Assume it's raw PCM.  Since the audio data is assumed to begin           ** at byte zero, this will unfortunately require seeking.         */        if (fseek(sf, 0L, SEEK_SET) != 0) {            /* ignore errors */        }        input_format = sf_raw;    }}voidCloseSndFile(sound_file_format input, FILE * musicin){    if (fclose(musicin) != 0) {	if( silent < 10 ) {	    fprintf(stderr, "Could not close audio input file\n");	}        exit(2);    }}FILE   *OpenSndFile(lame_global_flags * gfp, char *inPath){    FILE   *musicin;    /* set the defaults from info incase we cannot determine them from file */    lame_set_num_samples( gfp, MAX_U_32_NUM );    if (!strcmp(inPath, "-")) {        lame_set_stream_binary_mode(musicin = stdin); /* Read from standard input. */    }    else {        if ((musicin = fopen(inPath, "rb")) == NULL) {	    if( silent < 10 ) {		fprintf(stderr, "Could not find \"%s\".\n", inPath);	    }	    exit(1);        }    }    if (input_format == sf_mp1 ||        input_format == sf_mp2 || input_format == sf_mp3) {#ifdef AMIGA_MPEGA        if (-1 == lame_decode_initfile(inPath, &mp3input_data)) {	    if( silent < 10 ) {		fprintf(stderr, "Error reading headers in mp3 input file %s.\n",			inPath);	    }            exit(1);        }#endif#ifdef HAVE_MPGLIB        if (-1 == lame_decode_initfile(musicin, &mp3input_data)) {	    if( silent < 10 ) {		fprintf(stderr, "Error reading headers in mp3 input file %s.\n",			inPath);	    }	    exit(1);        }#endif        if( -1 == lame_set_num_channels( gfp, mp3input_data.stereo ) ) {	    if( silent < 10 ) {		fprintf( stderr,			 "Unsupported number of channels: %ud\n",			 mp3input_data.stereo );	    }            exit( 1 );        }        (void) lame_set_in_samplerate( gfp, mp3input_data.samplerate );        (void) lame_set_num_samples( gfp, mp3input_data.nsamp );    }    else if (input_format == sf_ogg) {	if( silent < 10 ) {	    fprintf(stderr, "sorry, vorbis support in LAME is deprecated.\n");	}	exit(1);    }    else {        if (input_format != sf_raw) {            parse_file_header(gfp, musicin);        }        if (input_format == sf_raw) {            /* assume raw PCM */	    if( silent < 10 ) {		fprintf(stderr, "Assuming raw pcm input file");		if (swapbytes)		    fprintf(stderr, " : Forcing byte-swapping\n");		else		    fprintf(stderr, "\n");	    }            pcmswapbytes = swapbytes;        }    }    if (lame_get_num_samples( gfp ) == MAX_U_32_NUM && musicin != stdin) {        double  flen = lame_get_file_size(inPath); /* try to figure out num_samples */        if (flen >= 0) {            /* try file size, assume 2 bytes per sample */            if (input_format == sf_mp1 ||                input_format == sf_mp2 || input_format == sf_mp3) {                if (mp3input_data.bitrate > 0) {                    double  totalseconds =                        (flen * 8.0 / (1000.0 * mp3input_data.bitrate));                    unsigned long tmp_num_samples =                        (unsigned long) (totalseconds * lame_get_in_samplerate( gfp ));                    (void) lame_set_num_samples( gfp, tmp_num_samples );                    mp3input_data.nsamp = tmp_num_samples;                }            }            else {                (void) lame_set_num_samples( gfp,                    (unsigned long)(flen / (2 * lame_get_num_channels( gfp ))) );            }        }    }    return musicin;}#endif /* defined(LIBSNDFILE) */#if defined(HAVE_MPGLIB)static intcheck_aid(const unsigned char *header){    return 0 == memcmp(header, "AiD\1", 4);}/* * Please check this and don't kill me if there's a bug * This is a (nearly?) complete header analysis for a MPEG-1/2/2.5 Layer I, II or III * data stream */static intis_syncword_mp123(const void *const headerptr){    const unsigned char *const p = headerptr;    static const char abl2[16] =        { 0, 7, 7, 7, 0, 7, 0, 0, 0, 0, 0, 8, 8, 8, 8, 8 };    if ((p[0] & 0xFF) != 0xFF)        return 0;       /* first 8 bits must be '1' */    if ((p[1] & 0xE0) != 0xE0)        return 0;       /* next 3 bits are also */    if ((p[1] & 0x18) == 0x08)        return 0;       /* no MPEG-1, -2 or -2.5 */            if ((p[1] & 0x06) == 0x00)        return 0;       /* no Layer I, II and III */#ifndef USE_LAYER_1    if ((p[1] & 0x06) == 0x03*2)	return 0; /* layer1 is not supported */#endif#ifndef USE_LAYER_2    if ((p[1] & 0x06) == 0x02*2)	return 0; /* layer1 is not supported */#endif    if (!(((p[1] & 0x06) == 0x03*2 && input_format == sf_mp1)	  || ((p[1] & 0x06) == 0x02*2 && input_format == sf_mp2)	  || ((p[1] & 0x06) == 0x01*2 && input_format == sf_mp3)))	return 0; /* imcompatible layer with input file format */    if ((p[2] & 0xF0) == 0xF0)        return 0;       /* bad bitrate */    if ((p[2] & 0x0C) == 0x0C)        return 0;       /* no sample frequency with (32,44.1,48)/(1,2,4)     */    if ((p[1] & 0x18) == 0x18 && (p[1] & 0x06) == 0x04	&& abl2[p[2] >> 4] & (1 << (p[3] >> 6)))	return 0;    if ((p[3] & 3) == 2)	return 0;       /* reserved enphasis mode */    return 1;}intlame_decode_initfile(FILE * fd, mp3data_struct * mp3data){    /*  VBRTAGDATA pTagData; */    /* int xing_header,len2,num_frames; */    unsigned char buf[100];    int     ret;    int     len, aid_header;    short int pcm_l[1152], pcm_r[1152];    int freeformat = 0;    memset(mp3data, 0, sizeof(mp3data_struct));    lame_decode_init();    len = 4;    if (fread(buf, 1, len, fd) != len)        return -1;      /* failed */    if (buf[0] == 'I' && buf[1] == 'D' && buf[2] == '3') {	if( silent < 10 ) {	    fprintf(stderr, "ID3v2 found. "		    "Be aware that the ID3 tag is currently lost when transcoding.\n");	}	len = 6;	if (fread(&buf, 1, len, fd) != len)	    return -1;      /* failed */	buf[2] &= 127; buf[3] &= 127; buf[4] &= 127; buf[5] &= 127;	len = (((((buf[2] << 7) + buf[3]) << 7) + buf[4]) << 7) + buf[5];	fskip(fd, len, SEEK_CUR);	len = 4;	if (fread(&buf, 1, len, fd) != len)	    return -1;      /* failed */    }    aid_header = check_aid(buf);    if (aid_header) {        if (fread(&buf, 1, 2, fd) != 2)            return -1;  /* failed */        aid_header = (unsigned char) buf[0] + 256 * (unsigned char) buf[1];	if( silent < 10 ) {	    fprintf(stderr, "Album ID found.  length=%i \n", aid_header);	}        /* skip rest of AID, except for 6 bytes we have already read */        fskip(fd, aid_header - 6, SEEK_CUR);        /* read 4 more bytes to set up buffer for MP3 header check */	if (fread(&buf, 1, len, fd) != len)	    return -1;      /* failed */    }    len = 4;    while (!is_syncword_mp123(buf)) {        int     i;        for (i = 0; i < len - 1; i++)            buf[i] = buf[i + 1];        if (fread(buf + len - 1, 1, 1, fd) != 1)            return -1;  /* failed */    }    if ((buf[2] & 0xf0)==0) {	if( silent < 10 ) {	    fprintf(stderr,"Input file is freeformat.\n");	}	freeformat = 1;    }    /* now parse the current buffer looking for MP3 headers.    */    /* (as of 11/00: mpglib modified so that for the first frame where  */    /* headers are parsed, no data will be decoded.   */    /* However, for freeformat, we need to decode an entire frame, */    /* so mp3data->bitrate will be 0 until we have decoded the first */    /* frame.  Cannot decode first frame here because we are not */    /* yet prepared to handle the output. */    ret = lame_decode1_headersB(buf, len, pcm_l, pcm_r, mp3data,&enc_delay,&enc_padding);    if (-1 == ret)        return -1;    /* repeat until we decode a valid mp3 header.  */    while (!mp3data->header_parsed) {        len = fread(buf, 1, sizeof(buf), fd);        if (len != sizeof(buf))            return -1;        ret = lame_decode1_headersB(buf, len, pcm_l, pcm_r, mp3data,&enc_delay,&enc_padding);        if (-1 == ret)            return -1;    }    if (mp3data->bitrate==0 && !freeformat) {	if( silent < 10 ) {	    fprintf(stderr, "fail to sync...\n");	}	return lame_decode_initfile(fd, mp3data);    }    if (mp3data->totalframes > 0) {        /* mpglib found a Xing VBR header and computed nsamp & totalframes */    }    else {	/* set as unknown.  Later, we will take a guess based on file size	 * ant bitrate */        mp3data->nsamp = MAX_U_32_NUM;    }    /*       fprintf(stderr,"ret = %i NEED_MORE=%i \n",ret,MP3_NEED_MORE);       fprintf(stderr,"stereo = %i \n",mp.fr.stereo);       fprintf(stderr,"samp = %i  \n",freqs[mp.fr.sampling_frequency]);       fprintf(stderr,"framesize = %i  \n",framesize);       fprintf(stderr,"bitrate = %i  \n",mp3data->bitrate);       fprintf(stderr,"num frames = %ui  \n",num_frames);       fprintf(stderr,"num samp = %ui  \n",mp3data->nsamp);       fprintf(stderr,"mode     = %i  \n",mp.fr.mode);     */    return 0;}/*For lame_decode_fromfile:  return code  -1     error   n     number of samples output.  either 576 or 1152 depending on MP3 file.For lame_decode1_headers():  return code  -1     error   0     ok, but need more data before outputing any samples   n     number of samples output.  either 576 or 1152 depending on MP3 file.*/intlame_decode_fromfile(FILE * fd, short pcm_l[], short pcm_r[],                     mp3data_struct * mp3data){    int     ret = 0, len=0;    unsigned char buf[1024];    /* first see if we still have data buffered in the decoder: */    ret = lame_decode1_headers(buf, len, pcm_l, pcm_r, mp3data);    if (ret!=0) return ret;    /* read until we get a valid output frame */    while (1) {        len = fread(buf, 1, 1024, fd);        if (len == 0) {	    /* we are done reading the file, but check for buffered data */	    ret = lame_decode1_headers(buf, len, pcm_l, pcm_r, mp3data);	    if (ret<=0) {                lame_decode_exit(); /* release mp3decoder memory */                return -1;  /* done with file */            }	    break;	}        ret = lame_decode1_headers(buf, len, pcm_l, pcm_r, mp3data);        if (ret == -1) {            lame_decode_exit();  /* release mp3decoder memory */            return -1;        }	if (ret >0) break;    }    return ret;}#endif /* defined(HAVE_MPGLIB) *//* end of get_audio.c */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -