📄 parse.c
字号:
/* parse: spawned from common; clustering around stream/frame parsing copyright ?-2008 by the mpg123 project - free software under the terms of the LGPL 2.1 see COPYING and AUTHORS files in distribution or http://mpg123.org initially written by Michael Hipp & Thomas Orgis*/#include "mpg123lib_intern.h"#include <sys/stat.h>#include <fcntl.h>#include "getbits.h"#ifdef WIN32#include <winsock.h>#endif/* a limit for number of frames in a track; beyond that unsigned long may not be enough to hold byte addresses */#ifdef HAVE_LIMITS_H#include <limits.h>#endif#ifndef ULONG_MAX/* hm, is this portable across preprocessors? */#define ULONG_MAX ((unsigned long)-1)#endif#define TRACK_MAX_FRAMES ULONG_MAX/4/1152#include "debug.h"#define bsbufid(fr) (fr)->bsbuf==(fr)->bsspace[0] ? 0 : ((fr)->bsbuf==fr->bsspace[1] ? 1 : ( (fr)->bsbuf==(fr)->bsspace[0]+512 ? 2 : ((fr)->bsbuf==fr->bsspace[1]+512 ? 3 : -1) ) )/* AAAAAAAA AAABBCCD EEEEFFGH IIJJKLMM A: sync B: mpeg version C: layer D: CRC E: bitrate F:sampling rate G: padding H: private I: channel mode J: mode ext K: copyright L: original M: emphasis old compare mask 0xfffffd00: 11111111 11111111 11111101 00000000 means: everything must match excluding padding and channel mode, ext mode, ... But a vbr stream's headers will differ in bitrate! We are already strict in allowing only frames of same type in stream, we should at least watch out for VBR while being strict. So a better mask is: 11111111 11111111 00001101 00000000 Even more, I'll allow varying crc bit. 11111111 11111110 00001101 00000000 (still unsure about this private bit)*/#define HDRCMPMASK 0xfffe0d00#define HDRCHANMASK 0xc0 /* 11000000, selecting II bits (channel mode) */#define HDRSAMPMASK 0xc00 /* 1100 00000000, FF bits (sample rate) *//* bitrates for [mpeg1/2][layer] */static const int tabsel_123[2][3][16] = { { {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,} }};const long freqs[9] = { 44100, 48000, 32000, 22050, 24000, 16000 , 11025 , 12000 , 8000 };#ifdef VARMODESUPPORT /* * This is a dirty hack! It might burn your PC and kill your cat! * When in "varmode", specially formatted layer-3 mpeg files are * expected as input -- it will NOT work with standard mpeg files. * The reason for this: * Varmode mpeg files enable my own GUI player to perform fast * forward and backward functions, and to jump to an arbitrary * timestamp position within the file. This would be possible * with standard mpeg files, too, but it would be a lot harder to * implement. * A filter for converting standard mpeg to varmode mpeg is * available on request, but it's really not useful on its own. * * Oliver Fromme <oliver.fromme@heim3.tu-clausthal.de> * Mon Mar 24 00:04:24 MET 1997 */int varmode = FALSE;int playlimit;#endifstatic int decode_header(mpg123_handle *fr,unsigned long newhead);int read_frame_init(mpg123_handle* fr){ if(frame_reset(fr) != 0) return -1; return 0;}/* These two are to be replaced by one function that gives all the frame parameters (for outsiders).*/int frame_bitrate(mpg123_handle *fr){ return tabsel_123[fr->lsf][fr->lay-1][fr->bitrate_index];}long frame_freq(mpg123_handle *fr){ return freqs[fr->sampling_frequency];}#define free_format_header(head) ( ((head & 0xffe00000) == 0xffe00000) && ((head>>17)&3) && (((head>>12)&0xf) == 0x0) && (((head>>10)&0x3) != 0x3 ))/* compiler is smart enought to inline this one or should I really do it as macro...? */int head_check(unsigned long head){ if ( /* first 11 bits are set to 1 for frame sync */ ((head & 0xffe00000) != 0xffe00000) || /* layer: 01,10,11 is 1,2,3; 00 is reserved */ (!((head>>17)&3)) || /* 1111 means bad bitrate */ (((head>>12)&0xf) == 0xf) || /* 0000 means free format... which should be supported in future. */ (((head>>12)&0xf) == 0x0) || /* sampling freq: 11 is reserved */ (((head>>10)&0x3) == 0x3 ) /* here used to be a mpeg 2.5 check... re-enabled 2.5 decoding due to lack of evidence that it is really not good */ ) { return FALSE; } /* if no check failed, the header is valid (hopefully)*/ else { return TRUE; }}int read_frame_recover(mpg123_handle* fr){ int ret; fr->do_recover = 1; ret = read_frame(fr); fr->do_recover = 0; return ret;}static int check_lame_tag(mpg123_handle *fr){ /* going to look for Xing or Info at some position after the header MPEG 1 MPEG 2/2.5 (LSF) Stereo, Joint Stereo, Dual Channel 32 17 Mono 17 9 Also, how to avoid false positives? I guess I should interpret more of the header to rule that out(?). I hope that ensuring all zeros until tag start is enough. */ size_t lame_offset = (fr->stereo == 2) ? (fr->lsf ? 17 : 32 ) : (fr->lsf ? 9 : 17); /* At least skip the decoder delay. */#ifdef GAPLESS if(fr->begin_s == 0) frame_gapless_init(fr, GAPLESS_DELAY, 0);#endif if(fr->framesize >= 120+lame_offset) /* traditional Xing header is 120 bytes */ { size_t i; int lame_type = 0; debug("do we have lame tag?"); /* only search for tag when all zero before it (apart from checksum) */ for(i=2; i < lame_offset; ++i) if(fr->bsbuf[i] != 0) break; if(i == lame_offset) { debug("possibly..."); if ( (fr->bsbuf[lame_offset] == 'I') && (fr->bsbuf[lame_offset+1] == 'n') && (fr->bsbuf[lame_offset+2] == 'f') && (fr->bsbuf[lame_offset+3] == 'o') ) { lame_type = 1; /* We still have to see what there is */ } else if ( (fr->bsbuf[lame_offset] == 'X') && (fr->bsbuf[lame_offset+1] == 'i') && (fr->bsbuf[lame_offset+2] == 'n') && (fr->bsbuf[lame_offset+3] == 'g') ) { lame_type = 2; fr->vbr = MPG123_VBR; /* Xing header means always VBR */ } if(lame_type) { unsigned long xing_flags; /* we have one of these headers... */ if(VERBOSE2) fprintf(stderr, "Note: Xing/Lame/Info header detected\n"); /* now interpret the Xing part, I have 120 bytes total for sure */ /* there are 4 bytes for flags, but only the last byte contains known ones */ lame_offset += 4; /* now first byte after Xing/Name */ /* 4 bytes dword for flags */ #define make_long(a, o) ((((unsigned long) a[o]) << 24) | (((unsigned long) a[o+1]) << 16) | (((unsigned long) a[o+2]) << 8) | ((unsigned long) a[o+3])) /* 16 bit */ #define make_short(a,o) ((((unsigned short) a[o]) << 8) | ((unsigned short) a[o+1])) xing_flags = make_long(fr->bsbuf, lame_offset); lame_offset += 4; debug1("Xing: flags 0x%08lx", xing_flags); if(xing_flags & 1) /* frames */ { /* In theory, one should use that value for skipping... When I know the exact number of samples I could simply count in flush_output, but that's problematic with seeking and such. I still miss the real solution for detecting the end. */ fr->track_frames = (off_t) make_long(fr->bsbuf, lame_offset); if(fr->track_frames > TRACK_MAX_FRAMES) fr->track_frames = 0; /* endless stream? */ #ifdef GAPLESS /* if no further info there, remove/add at least the decoder delay */ if(fr->p.flags & MPG123_GAPLESS) { off_t length = fr->track_frames * spf(fr); if(length > 1) frame_gapless_init(fr, GAPLESS_DELAY, length+GAPLESS_DELAY); } #endif if(VERBOSE3) fprintf(stderr, "Note: Xing: %lu frames\n", (long unsigned)fr->track_frames); lame_offset += 4; } if(xing_flags & 0x2) /* bytes */ { if(VERBOSE3) { unsigned long xing_bytes = make_long(fr->bsbuf, lame_offset); fprintf(stderr, "Note: Xing: %lu bytes\n", (long unsigned)xing_bytes); } lame_offset += 4; } if(xing_flags & 0x4) /* TOC */ { lame_offset += 100; /* just skip */ } if(xing_flags & 0x8) /* VBR quality */ { if(VERBOSE3) { unsigned long xing_quality = make_long(fr->bsbuf, lame_offset); fprintf(stderr, "Note: Xing: quality = %lu\n", (long unsigned)xing_quality); } lame_offset += 4; } /* I guess that either 0 or LAME extra data follows */ /* there may this crc16 be floating around... (?) */ if(fr->bsbuf[lame_offset] != 0) { unsigned char lame_vbr; float replay_gain[2] = {0,0}; float peak = 0; float gain_offset = 0; /* going to be +6 for old lame that used 83dB */ char nb[10]; memcpy(nb, fr->bsbuf+lame_offset, 9); nb[9] = 0; if(VERBOSE3) fprintf(stderr, "Note: Info: Encoder: %s\n", nb); if(!strncmp("LAME", nb, 4)) { gain_offset = 6; debug("TODO: finish lame detetcion..."); } lame_offset += 9; /* the 4 big bits are tag revision, the small bits vbr method */ lame_vbr = fr->bsbuf[lame_offset] & 15; if(VERBOSE3) { fprintf(stderr, "Note: Info: rev %u\n", fr->bsbuf[lame_offset] >> 4); fprintf(stderr, "Note: Info: vbr mode %u\n", lame_vbr); } lame_offset += 1; switch(lame_vbr) { /* from rev1 proposal... not sure if all good in practice */ case 1: case 8: fr->vbr = MPG123_CBR; break; case 2: case 9: fr->vbr = MPG123_ABR; break; default: fr->vbr = MPG123_VBR; /* 00==unknown is taken as VBR */ } /* skipping: lowpass filter value */ lame_offset += 1; /* replaygain */ /* 32bit float: peak amplitude -- why did I parse it as int before??*/ /* Ah, yes, lame seems to store it as int since some day in 2003; I've only seen zeros anyway until now, bah! */ if ( (fr->bsbuf[lame_offset] != 0) || (fr->bsbuf[lame_offset+1] != 0) || (fr->bsbuf[lame_offset+2] != 0) || (fr->bsbuf[lame_offset+3] != 0) ) { debug("Wow! Is there _really_ a non-zero peak value? Now is it stored as float or int - how should I know?"); peak = *(float*) (fr->bsbuf+lame_offset); } if(VERBOSE3) fprintf(stderr, "Note: Info: peak = %f (I won't use this)\n", peak); peak = 0; /* until better times arrived */ lame_offset += 4; /* ReplayGain values - lame only writes radio mode gain... 16bit gain, 3 bits name, 3 bits originator, sign (1=-, 0=+), dB value*10 in 9 bits (fixed point) ignore the setting if name or originator == 000! radio 0 0 1 0 1 1 1 0 0 1 1 1 1 1 0 1 audiophile 0 1 0 0 1 0 0 0 0 0 0 1 0 1 0 0 */ for(i =0; i < 2; ++i) { unsigned char origin = (fr->bsbuf[lame_offset] >> 2) & 0x7; /* the 3 bits after that... */ if(origin != 0) { unsigned char gt = fr->bsbuf[lame_offset] >> 5; /* only first 3 bits */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -