home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 13 / AACD13.ISO / AACD / Sound / LAME / src / mpglib / common.c < prev    next >
C/C++ Source or Header  |  2000-07-17  |  7KB  |  283 lines

  1. #ifdef HAVEMPGLIB
  2. #include <ctype.h>
  3. #include <stdlib.h>
  4. #include <signal.h>
  5.  
  6. #ifdef macintosh
  7. #include   <types.h>
  8. #include   <stat.h>
  9. #else
  10. #include  <sys/types.h>
  11. #include  <sys/stat.h>
  12. #endif
  13. #include <fcntl.h>
  14.  
  15. #include "mpg123.h"
  16.  
  17. struct parameter param = { 1 , 1 , 0 , 0 };
  18.  
  19. int tabsel_123[2][3][16] = {
  20.    { {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,},
  21.      {0,32,48,56, 64, 80, 96,112,128,160,192,224,256,320,384,},
  22.      {0,32,40,48, 56, 64, 80, 96,112,128,160,192,224,256,320,} },
  23.  
  24.    { {0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,},
  25.      {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,},
  26.      {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,} }
  27. };
  28.  
  29. long freqs[9] = { 44100, 48000, 32000,
  30.                   22050, 24000, 16000 ,
  31.                   11025 , 12000 , 8000 };
  32.  
  33. int bitindex;
  34. unsigned char *wordpointer;
  35. unsigned char *pcm_sample;
  36. int pcm_point = 0;
  37.  
  38.  
  39. #if 0
  40. static void get_II_stuff(struct frame *fr)
  41. {
  42.   static int translate[3][2][16] = 
  43.    { { { 0,2,2,2,2,2,2,0,0,0,1,1,1,1,1,0 } ,
  44.        { 0,2,2,0,0,0,1,1,1,1,1,1,1,1,1,0 } } ,
  45.      { { 0,2,2,2,2,2,2,0,0,0,0,0,0,0,0,0 } ,
  46.        { 0,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0 } } ,
  47.      { { 0,3,3,3,3,3,3,0,0,0,1,1,1,1,1,0 } ,
  48.        { 0,3,3,0,0,0,1,1,1,1,1,1,1,1,1,0 } } };
  49.  
  50.   int table,sblim;
  51.   static struct al_table *tables[5] = 
  52.        { alloc_0, alloc_1, alloc_2, alloc_3 , alloc_4 };
  53.   static int sblims[5] = { 27 , 30 , 8, 12 , 30 };
  54.  
  55.   if(fr->lsf)
  56.     table = 4;
  57.   else
  58.     table = translate[fr->sampling_frequency][2-fr->stereo][fr->bitrate_index];
  59.   sblim = sblims[table];
  60.  
  61.   fr->alloc = tables[table];
  62.   fr->II_sblimit = sblim;
  63. }
  64. #endif
  65.  
  66. #define HDRCMPMASK 0xfffffd00
  67.  
  68.  
  69. int head_check(unsigned long head)
  70. {
  71.   if( (head & 0xffe00000) != 0xffe00000) {
  72.     /* syncword */
  73.     return FALSE;
  74.   }
  75. #if 0
  76.   if(!((head>>17)&3)) {
  77.     /* bits 13-14 = layer 3 */
  78.     return FALSE;
  79.   }
  80. #endif
  81.   if (3 !=  4-((head>>17)&3)) {
  82.     /* bits 13-14 = layer 3 */
  83.     return FALSE;
  84.   }
  85.   if( ((head>>12)&0xf) == 0xf) {
  86.     /* bits 16,17,18,19 = 1111  invalid bitrate */
  87.     return FALSE;
  88.   }
  89.   if( ((head>>10)&0x3) == 0x3 ) {
  90.     /* bits 20,21 = 11  invalid sampling freq */
  91.     return FALSE;
  92.   }
  93.   return TRUE;
  94. }
  95.  
  96.  
  97. /*
  98.  * the code a header and write the information
  99.  * into the frame structure
  100.  */
  101. int decode_header(struct frame *fr,unsigned long newhead)
  102. {
  103.  
  104.  
  105.     if( newhead & (1<<20) ) {
  106.       fr->lsf = (newhead & (1<<19)) ? 0x0 : 0x1;
  107.       fr->mpeg25 = 0;
  108.     }
  109.     else {
  110.       fr->lsf = 1;
  111.       fr->mpeg25 = 1;
  112.     }
  113.  
  114.     
  115.     fr->lay = 4-((newhead>>17)&3);
  116.     if( ((newhead>>10)&0x3) == 0x3) {
  117.       fprintf(stderr,"Stream error\n");
  118.       exit(1);
  119.     }
  120.     if(fr->mpeg25) {
  121.       fr->sampling_frequency = 6 + ((newhead>>10)&0x3);
  122.     }
  123.     else
  124.       fr->sampling_frequency = ((newhead>>10)&0x3) + (fr->lsf*3);
  125.     fr->error_protection = ((newhead>>16)&0x1)^0x1;
  126.  
  127.     if(fr->mpeg25) /* allow Bitrate change for 2.5 ... */
  128.       fr->bitrate_index = ((newhead>>12)&0xf);
  129.  
  130.     fr->bitrate_index = ((newhead>>12)&0xf);
  131.     fr->padding   = ((newhead>>9)&0x1);
  132.     fr->extension = ((newhead>>8)&0x1);
  133.     fr->mode      = ((newhead>>6)&0x3);
  134.     fr->mode_ext  = ((newhead>>4)&0x3);
  135.     fr->copyright = ((newhead>>3)&0x1);
  136.     fr->original  = ((newhead>>2)&0x1);
  137.     fr->emphasis  = newhead & 0x3;
  138.  
  139.     fr->stereo    = (fr->mode == MPG_MD_MONO) ? 1 : 2;
  140.  
  141.  
  142.     switch(fr->lay)
  143.     {
  144.       case 1:
  145. #if 0
  146.         fr->do_layer = do_layer1;
  147.         fr->jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ? 
  148.                          (fr->mode_ext<<2)+4 : 32;
  149.         fr->framesize  = (long) tabsel_123[fr->lsf][0][fr->bitrate_index] * 12000;
  150.         fr->framesize /= freqs[fr->sampling_frequency];
  151.         fr->framesize  = ((fr->framesize+fr->padding)<<2)-4;
  152. #else
  153.         fprintf(stderr,"layer=1 Not supported!\n");
  154. #endif
  155.         break;
  156.       case 2:
  157. #if 0
  158.         fr->do_layer = do_layer2;
  159.         get_II_stuff(fr);
  160.         fr->jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ?
  161.                          (fr->mode_ext<<2)+4 : fr->II_sblimit;
  162.         fr->framesize = (long) tabsel_123[fr->lsf][1][fr->bitrate_index] * 144000;
  163.         fr->framesize /= freqs[fr->sampling_frequency];
  164.         fr->framesize += fr->padding - 4;
  165. #else
  166.         fprintf(stderr,"layer=2 Not supported!\n");
  167. #endif
  168.         break;
  169.       case 3:
  170. #if 0
  171.         fr->do_layer = do_layer3;
  172.         if(fr->lsf)
  173.           ssize = (fr->stereo == 1) ? 9 : 17;
  174.         else
  175.           ssize = (fr->stereo == 1) ? 17 : 32;
  176. #endif
  177.  
  178. #if 0
  179.         if(fr->error_protection)
  180.           ssize += 2;
  181. #endif
  182.     if (fr->bitrate_index==0)
  183.       fr->framesize=0;
  184.     else{
  185.           fr->framesize  = (long) tabsel_123[fr->lsf][2][fr->bitrate_index] * 144000;
  186.           fr->framesize /= freqs[fr->sampling_frequency]<<(fr->lsf);
  187.           fr->framesize = fr->framesize + fr->padding - 4;
  188.     }
  189.         break; 
  190.       default:
  191.         fprintf(stderr,"Sorry, unknown layer type.\n"); 
  192.         return (0);
  193.     }
  194.     /*    print_header(fr); */
  195.  
  196.     return 1;
  197. }
  198.  
  199.  
  200. #if 1
  201. void print_header(struct frame *fr)
  202. {
  203.     static char *modes[4] = { "Stereo", "Joint-Stereo", "Dual-Channel", "Single-Channel" };
  204.     static char *layers[4] = { "Unknown" , "I", "II", "III" };
  205.  
  206.     fprintf(stderr,"MPEG %s, Layer: %s, Freq: %ld, mode: %s, modext: %d, BPF : %d\n", 
  207.         fr->mpeg25 ? "2.5" : (fr->lsf ? "2.0" : "1.0"),
  208.         layers[fr->lay],freqs[fr->sampling_frequency],
  209.         modes[fr->mode],fr->mode_ext,fr->framesize+4);
  210.     fprintf(stderr,"Channels: %d, copyright: %s, original: %s, CRC: %s, emphasis: %d.\n",
  211.         fr->stereo,fr->copyright?"Yes":"No",
  212.         fr->original?"Yes":"No",fr->error_protection?"Yes":"No",
  213.         fr->emphasis);
  214.     fprintf(stderr,"Bitrate: %d Kbits/s, Extension value: %d\n",
  215.         tabsel_123[fr->lsf][fr->lay-1][fr->bitrate_index],fr->extension);
  216. }
  217.  
  218. void print_header_compact(struct frame *fr)
  219. {
  220.     static char *modes[4] = { "stereo", "joint-stereo", "dual-channel", "mono" };
  221.     static char *layers[4] = { "Unknown" , "I", "II", "III" };
  222.  
  223.     fprintf(stderr,"MPEG %s layer %s, %d kbit/s, %ld Hz %s\n",
  224.         fr->mpeg25 ? "2.5" : (fr->lsf ? "2.0" : "1.0"),
  225.         layers[fr->lay],
  226.         tabsel_123[fr->lsf][fr->lay-1][fr->bitrate_index],
  227.         freqs[fr->sampling_frequency], modes[fr->mode]);
  228. }
  229.  
  230. #endif
  231.  
  232. unsigned int getbits(int number_of_bits)
  233. {
  234.   unsigned long rval;
  235.  
  236.   if(!number_of_bits)
  237.     return 0;
  238.  
  239.   {
  240.     rval = wordpointer[0];
  241.     rval <<= 8;
  242.     rval |= wordpointer[1];
  243.     rval <<= 8;
  244.     rval |= wordpointer[2];
  245.     rval <<= bitindex;
  246.     rval &= 0xffffff;
  247.  
  248.     bitindex += number_of_bits;
  249.  
  250.     rval >>= (24-number_of_bits);
  251.  
  252.     wordpointer += (bitindex>>3);
  253.     bitindex &= 7;
  254.   }
  255.   return rval;
  256. }
  257.  
  258. unsigned int getbits_fast(int number_of_bits)
  259. {
  260.   unsigned long rval;
  261.  
  262.   {
  263.     rval = wordpointer[0];
  264.     rval <<= 8;    
  265.     rval |= wordpointer[1];
  266.     rval <<= bitindex;
  267.     rval &= 0xffff;
  268.     bitindex += number_of_bits;
  269.  
  270.     rval >>= (16-number_of_bits);
  271.  
  272.     wordpointer += (bitindex>>3);
  273.     bitindex &= 7;
  274.   }
  275.   return rval;
  276. }
  277.  
  278.  
  279.  
  280.  
  281.  
  282. #endif
  283.