filemp3.c

Go to the documentation of this file.
00001 
00002 /*  $Id: filemp3.c 1254 2006-04-12 20:27:14Z sethdill $    */
00003 
00004 /******************************************************************************
00005 
00006     UserLand Frontier(tm) -- High performance Web content management,
00007     object database, system-level and Internet scripting environment,
00008     including source code editing and debugging.
00009 
00010     Copyright (C) 1992-2004 UserLand Software, Inc.
00011 
00012     This program is free software; you can redistribute it and/or modify
00013     it under the terms of the GNU General Public License as published by
00014     the Free Software Foundation; either version 2 of the License, or
00015     (at your option) any later version.
00016 
00017     This program is distributed in the hope that it will be useful,
00018     but WITHOUT ANY WARRANTY; without even the implied warranty of
00019     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020     GNU General Public License for more details.
00021 
00022     You should have received a copy of the GNU General Public License
00023     along with this program; if not, write to the Free Software
00024     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025 
00026 ******************************************************************************/
00027 
00028 /*
00029     MPEG Audio Layer I/II/III frame header
00030 
00031     There is no main file header in an MPEG audio file. An MPEG
00032     audio file is built up from a succession of smaller parts called
00033     frames. A frame is a datablock with its own header and audio
00034     information.
00035 
00036     In the case of Layer I or Layer II, frames are some totally
00037     independent items, so you can cut any part of MPEG file and play
00038     it correctly. The player will then play the music starting to
00039     the first plain valid frame founded. However, in the case of
00040     Layer III, frames are not always independant. Due to the possible
00041     use of the "byte reservoir", wich is a kind of buffer, frames
00042     are often dependent of each other. In the worst case, 9 frames
00043     may be needed before beeing able to decode one frame.
00044 
00045     When you want to read info about an MPEG audio file, it is usually
00046     enough to find the first frame, read its header and assume that
00047     the other frames are the same. But this is not always the case,
00048     as variable bitrate (VBR) files may be encountered. In a VBR
00049     file, the bitrate can be changed in each frame. It can be used,
00050     as an exemple to keep a constant sound quality during the whole
00051     file, by using more bits where the music need more to be
00052     encoded.
00053 
00054     The frame header is 32 bits (4 bytes) length. The first eleven
00055     bits (or first twelve bits in the case of the MPEG 2.5 extension) of
00056     a frame header are always set to 1 and are called "frame sync".
00057 
00058     Frames may have an optional CRC checksum. It is 16 bits long and, if
00059     it exists, follows the frame header. After the CRC comes the
00060     audio data. By re-calculating the CRC and comparing its value to
00061     the sored one, you can check if the frame has been altered during
00062     transmission of the bitstream.
00063 
00064     Here is a presentation of the frame header content. Characters A
00065     to M are used to indicate different fields. In the table below,
00066     you can see details about the content of each field.
00067 
00068             AAAAAAAA AAABBCCD EEEEFFGH IIJJKLMM
00069             
00070     
00071     Sign    Length (bits)   Position (bits)     Description
00072     
00073       A        11               (31-21)         Frame sync (all bits must be set)
00074       
00075       B         2               (20,19)         MPEG Audio version ID
00076       
00077                                                   00 - MPEG Version 2.5 (later extension of MPEG 2)
00078                                                   01 - reserved
00079                                                   10 - MPEG Version 2 (ISO/IEC 13818-3)
00080                                                   11 - MPEG Version 1 (ISO/IEC 11172-3) 
00081                                                   
00082                                                 Note: MPEG Version 2.5 was added lately to the MPEG 2 standard.
00083                                                 It is an extension used for very low bitrate files, allowing the
00084                                                 use of lower sampling frequencies. If your decoder does not
00085                                                 support this extension, it is recommended for you to use 12 bits
00086                                                 for synchronization instead of 11 bits.
00087                                                   
00088       C         2               (18,17)         Layer description
00089       
00090                                                   00 - reserved
00091                                                   01 - Layer III
00092                                                   10 - Layer II
00093                                                   11 - Layer I
00094     
00095       D         1                 (16)          Protection bit
00096       
00097                                                   0 - Protected by CRC (16bit CRC follows header)
00098                                                   1 - Not protected
00099                                                   
00100       E         4               (15,12)         Bitrate index
00101     
00102                                                     bits    V1,L1   V1,L2   V1,L3   V2,L1   V2, L2 & L3
00103                                                     
00104                                                     0000    free    free    free    free    free
00105                                                     0001     32      32       32     32       8
00106                                                     0010     64      48       40     48      16
00107                                                     0011     96      56       48     56      24
00108                                                     0100    128      64       56     64      32
00109                                                     0101    160      80       64     80      40
00110                                                     0110    192      96       80     96      48
00111                                                     0111    224     112       96    112      56
00112                                                     1000    256     128      112    128      64
00113                                                     1001    288     160      128    144      80
00114                                                     1010    320     192      160    160      96
00115                                                     1011    352     224      192    176     112
00116                                                     1100    384     256      224    192     128
00117                                                     1101    416     320      256    224     144
00118                                                     1110    448     384      320    256     160
00119                                                     1111    bad     bad      bad    bad     bad
00120     
00121                                                     NOTES: All values are in kbps
00122                                                     V1 - MPEG Version 1
00123                                                     V2 - MPEG Version 2 and Version 2.5
00124                                                     L1 - Layer I
00125                                                     L2 - Layer II
00126                                                     L3 - Layer III
00127     
00128                                                 "free" means free format. The free bitrate must remain constant,
00129                                                 an must be lower than the maximum allowed bitrate. Decoders are
00130                                                 not required to support decoding of free bitrate streams.
00131                                                 
00132                                                 "bad" means that the value is unallowed. 
00133 
00134                                                 MPEG files may feature variable bitrate (VBR). Each frame may then
00135                                                 be created with a different bitrate. It may be used in all layers.
00136                                                 Layer III decoders must support this method.
00137                                                 Layer I & II decoders may support it. 
00138 
00139                                                 For Layer II there are some combinations of bitrate and mode
00140                                                 which are not allowed. Here is a list of allowed combinations. 
00141     
00142                                                     bitrate   single channel   stereo   intensity stereo   dual channel
00143                                                     
00144                                                     free           yes          yes             yes           yes
00145                                                      32            yes          no              no            no 
00146                                                      48            yes          no              no            no 
00147                                                      56            yes          no              no            no 
00148                                                      64            yes          yes             yes           yes
00149                                                      80            yes          no              no            no 
00150                                                      96            yes          yes             yes           yes
00151                                                     112            yes          yes             yes           yes
00152                                                     128            yes          yes             yes           yes
00153                                                     160            yes          yes             yes           yes
00154                                                     192            yes          yes             yes           yes
00155                                                     224            no           yes             yes           yes
00156                                                     256            no           yes             yes           yes
00157                                                     320            no           yes             yes           yes
00158                                                     384            no           yes             yes           yes
00159     
00160       F         2               (11,10)         Sampling rate frequency index
00161       
00162                                                     bits   MPEG1      MPEG2      MPEG2.5
00163                                                     
00164                                                      00    44100 Hz   22050 Hz   11025 Hz
00165                                                      01    48000 Hz   24000 Hz   12000 Hz
00166                                                      10    32000 Hz   16000 Hz    8000 Hz
00167                                                      11    reserv.    reserv.     reserv.
00168     
00169       G         1                 (9)           Padding bit
00170       
00171                                                     0 - frame is not padded
00172                                                     1 - frame is padded with one extra slot
00173     
00174                                                 Padding is used to exactly fit the bitrate.
00175                                                 As an example: 128kbps 44.1kHz layer II uses a lot of 418 bytes
00176                                                 and some of 417 bytes long frames to get the exact 128k bitrate.
00177                                                 For Layer I slot is 32 bits long, for Layer II and Layer III slot is 8 bits long.
00178     
00179       H         1                 (8)           Private bit. This one is only informative.
00180       
00181       I         2                (7,6)          Channel Mode
00182       
00183                                                     00 - Stereo
00184                                                     01 - Joint stereo (Stereo)
00185                                                     10 - Dual channel (2 mono channels)
00186                                                     11 - Single channel (Mono)
00187                                                     
00188                                                 Note: Dual channel files are made of two independant mono channel.
00189                                                 Each one uses exactly half the bitrate of the file. Most decoders
00190                                                 output them as stereo, but it might not always be the case.
00191                                                 One example of use would be some speech in two different languages
00192                                                 carried in the same bitstream, and then an appropriate decoder would
00193                                                 decode only the choosen language. 
00194     
00195       J         2                (5,4)          Mode extension (Only used in Joint stereo) 
00196     
00197                                                 Mode extension is used to join informations that are of no use
00198                                                 for stereo effect, thus reducing needed bits. These bits are dynamically
00199                                                 determined by an encoder in Joint stereo mode, and Joint Stereo can be
00200                                                 changed from one frame to another, or even switched on or off. 
00201                                                     
00202                                                 Complete frequency range of MPEG file is divided in subbands.
00203                                                 There are 32 subbands. For Layer I & II these two bits determine
00204                                                 frequency range (bands) where intensity stereo is applied.
00205                                                 For Layer III these two bits determine which type of joint stereo
00206                                                 is used (intensity stereo or m/s stereo).
00207                                                 Frequency range is determined within decompression algorithm.
00208                                                     
00209                                                                                         Layer III
00210                                                     value   Layer I & II    Intensity stereo  MS stereo
00211                                                     
00212                                                      00     bands  4 to 31          off          off
00213                                                      01     bands  8 to 31          on           off
00214                                                      10     bands 12 to 31          off          on
00215                                                      11     bands 16 to 31          on           on
00216          
00217       K         1                 (3)           Copyright
00218       
00219                                                     0 - Audio is not copyrighted
00220                                                     1 - Audio is copyrighted
00221     
00222                                                 The copyright has the same meaning as the copyright bit on CDs and DAT tapes,
00223                                                 i.e. telling that it is illegal to copy the contents if the bit is set.
00224     
00225       L         1                 (2)           Original
00226       
00227                                                     0 - Copy of original media
00228                                                     1 - Original media
00229     
00230                                                 The original bit indicates, if it is set, that the frame is located
00231                                                 on its original media.
00232     
00233       M         2                (1,0)          Emphasis
00234       
00235                                                     00 - none
00236                                                     01 - 50/15 ms
00237                                                     10 - reserved
00238                                                     11 - CCIT J.17
00239     
00240                                                 The emphasis indication is here to tell the decoder that the file
00241                                                 must be de-emphasized, ie the decoder must 're-equalize' the sound
00242                                                 after a Dolby-like noise supression. It is rarely used. 
00243     
00244       (c) 1999-2000 Gabriel Bouvigne for MP3'Tech - www.mp3-tech.org
00245       
00246       Source: http://www.mp3-tech.org/programmer/frame_header.html
00247  */
00248 
00249 
00250 #include "frontier.h"
00251 #include "standard.h"
00252 
00253 #include "file.h"
00254 #include "shell.h"
00255 
00256 
00257 typedef struct {
00258     long version;
00259     long layer;
00260     long bitrate;
00261     long frequency;
00262     long channelmode;
00263     long modeextension;
00264     long emphasis;
00265     boolean flchecksum;
00266     boolean flpadding;
00267     boolean flprivate;
00268     boolean flcopyright;
00269     boolean floriginal;
00270     } tympeginfostruct;
00271 
00272 
00273 static const long mpegsamplingfrequency[3][4] = {
00274         {44100, 48000, 32000, 0}, // MPEG 1
00275         {22050, 24000, 16000, 0}, // MPEG 2
00276         {11025, 12000,  8000, 0}  // MPEG 2.5
00277     };
00278     
00279 
00280 static const long mpegbitrate[2][3][16] = {
00281     { // MPEG 1
00282         {0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448, 0}, // Layer 1
00283         {0, 32, 48, 56,  64,  80,  96, 112, 128, 160, 192, 224, 256, 320, 384, 0}, // Layer 2
00284         {0, 32, 40, 48,  56,  64,  80,  96, 112, 128, 160, 192, 224, 256, 320, 0}  // Layer 3
00285     },{ // MPEG 2
00286         {0, 32, 48, 56,  64,  80,  96, 112, 128, 144, 160, 176, 192, 224, 256, 0}, // Layer 1
00287         {0,  8, 16, 24,  32,  40,  48,  56,  64,  80,  96, 112, 128, 144, 160, 0}, // Layer 2
00288         {0,  8, 16, 24,  32,  40,  48,  56,  64,  80,  96, 112, 128, 144, 160, 0}  // Layer 3
00289     }
00290 };
00291 
00292 
00293 static boolean decodempegheader (unsigned long header, tympeginfostruct *info) {
00294     
00295     if ((header >> 20) != 0x00000fff) // bits 20-31 must be set, we don't recognize MPEG 2.5
00296         return (false);
00297     
00298     info->version = 2 - ((header >> 19) & 0x00000001); // 1 = MPEG1, 2 = MPEG2
00299     
00300     info->layer = 4 - ((header >> 17) & 0x00000003); // 1 = Layer I, 2 = Layer II, 3 = Layer III
00301 
00302     info->bitrate = mpegbitrate [info->version - 1] [info->layer - 1] [(header >> 12) & 0x0000000f];
00303 
00304     info->frequency = mpegsamplingfrequency [info->version - 1] [(header >> 10) & 0x00000003];
00305 
00306     info->channelmode =  (header >> 6) & 0x00000003;
00307 
00308     info->modeextension =  (header >> 4) & 0x00000003;
00309 
00310     info->emphasis = header & 0x00000003;
00311 
00312     info->flchecksum = ((header >> 16) & 0x00000001) ? false : true;
00313 
00314     info->flpadding = ((header >> 9) & 0x00000001) ? true : false;
00315 
00316     info->flprivate = ((header >> 8) & 0x00000001) ? true : false;
00317 
00318     info->flcopyright = ((header >> 3) & 0x00000001) ? true : false;
00319     
00320     info->floriginal =  ((header >> 2) & 0x00000001) ? true : false;
00321 
00322     return (true);
00323     }/*decodempegheader*/
00324 
00325 
00326 static long extractfourbytes (Handle h, long ix) {
00327     
00328     long n;
00329     
00330     n = (unsigned char) (*h)[ix++];
00331     
00332     n <<= 8;
00333 
00334     n |= (unsigned char) (*h)[ix++];
00335     
00336     n <<= 8;
00337 
00338     n |= (unsigned char) (*h)[ix++];
00339     
00340     n <<= 8;
00341 
00342     n |= (unsigned char) (*h)[ix++];
00343 
00344     return (n);
00345     }/*extractfourbytes*/
00346 
00347 
00348 static boolean getxingheader (tympeginfostruct *info, Handle h, long ix, long *ctframes, long *ctbytes) {
00349 #pragma unused (info)
00350 
00351     long flags;
00352     
00353     ix += 32; //MPEG 1, Layer 3
00354     
00355     if ((*h)[ix++] != 'X')
00356         return (false);
00357     
00358     if ((*h)[ix++] != 'i')
00359         return (false);
00360     
00361     if ((*h)[ix++] != 'n')
00362         return (false);
00363     
00364     if ((*h)[ix++] != 'g')
00365         return (false);
00366     
00367     flags = extractfourbytes (h, ix);
00368     
00369     ix += sizeof(long);
00370     
00371     if ((flags & 0x00000001) == 0) //check for frame count
00372         return (false);
00373         
00374     *ctframes = extractfourbytes (h, ix);
00375     
00376     ix += sizeof(long);
00377     
00378     if (flags & 0x00000002) { //check for byte count
00379         *ctbytes = extractfourbytes (h, ix);
00380         ix += sizeof(long);
00381         }
00382     
00383     return (true);
00384     }/*getxingheader*/
00385     
00386 
00387 boolean getmp3info (const tyfilespec *fs, long *seconds, long *bitrate, long *frequency, long *offset, boolean *flvariablebitrate) {
00388 
00389     tympeginfostruct mp3;
00390     long ix = 0;
00391     long header = 0;
00392     long oldpos, ctbytes, ctframes;
00393     Handle h = nil;
00394     unsigned char c1, c2;
00395     const long buffersize = 32767;
00396     
00397     if (!fifgetposition (fs, &oldpos))
00398         return (false);
00399 
00400     if (!fifsetposition (fs, 0))
00401         return (false);
00402     
00403     if (!fifreadhandle (fs, buffersize, &h)) //read the first 32k of data
00404         return (false);
00405     
00406     for (ix = 0; ix < buffersize - (long) sizeof (header) - 1; ix++) {
00407     
00408         c1 = (unsigned char) (*h)[ix];
00409             
00410         if (c1 == 0xff) {
00411         
00412             c2 = (unsigned char) (*h)[ix+1];
00413 
00414             c2 >>= 4;
00415                     
00416             if (c2 == 0x0f) {
00417                 header = extractfourbytes (h, ix);
00418                 break;
00419                 }
00420             }
00421         }
00422     
00423     if (decodempegheader (header, &mp3) && mp3.version == 1 && mp3.layer == 3) {
00424 
00425         if (!fifgetendoffile (fs, &ctbytes))
00426             goto exit;
00427         
00428         ctbytes -= ix;
00429         
00430         *offset = ix;
00431         
00432         *frequency = mp3.frequency;
00433         
00434         *seconds = (mp3.bitrate != 0) ? (ctbytes / (125 * mp3.bitrate)) : 0;            
00435 
00436         *bitrate = mp3.bitrate;
00437 
00438         *flvariablebitrate = getxingheader (&mp3, h, ix + sizeof (header), &ctframes, &ctbytes);
00439         
00440         if (*flvariablebitrate) {
00441             *seconds = (ctframes * 26) / 1000;          
00442             *bitrate = (*seconds) ? ((ctbytes) / (*seconds * 125)) : 0;
00443             }
00444         }
00445     else {
00446         
00447         shellerrormessage (BIGSTRING ("\x4b" "Can't get the MP3 file info because an MP3 frame header could not be found."));
00448 
00449         goto exit;
00450         }
00451     
00452     if (!fifsetposition (fs, oldpos))
00453         goto exit;
00454 
00455     return (true);
00456 
00457 exit:
00458 
00459     fifsetposition (fs, oldpos);
00460     
00461     return (false);
00462     }/*getmp3info*/

Generated on Wed May 31 18:19:46 2006 for frontierkernel 10.1.10a by  doxygen 1.4.6