"Fossies" - the Fresh Open Source Software Archive

Member "quicktime4linux-2.3/thirdparty/faad2-2.0/plugins/QCDMp4/aacinfo.c" (31 May 2008, 6759 Bytes) of package /linux/privat/old/quicktime4linux-2.3-src.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /*
    2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
    3 ** Copyright (C) 2003 M. Bakker, Ahead Software AG, http://www.nero.com
    4 **  
    5 ** This program is free software; you can redistribute it and/or modify
    6 ** it under the terms of the GNU General Public License as published by
    7 ** the Free Software Foundation; either version 2 of the License, or
    8 ** (at your option) any later version.
    9 ** 
   10 ** This program is distributed in the hope that it will be useful,
   11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
   12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13 ** GNU General Public License for more details.
   14 ** 
   15 ** You should have received a copy of the GNU General Public License
   16 ** along with this program; if not, write to the Free Software 
   17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
   18 **
   19 ** Any non-GPL usage of this software or parts of this software is strictly
   20 ** forbidden.
   21 **
   22 ** Commercial non-GPL licensing of this software is possible.
   23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
   24 **
   25 ** $Id: aacinfo.c,v 1.3 2003/12/06 04:24:17 rjamorim Exp $
   26 **/
   27 
   28 #define WIN32_LEAN_AND_MEAN
   29 #include <windows.h>
   30 #include <malloc.h>
   31 #include <stdlib.h>
   32 #include <stdio.h>
   33 #include "aacinfo.h"
   34 #include "utils.h"
   35 
   36 #define ADIF_MAX_SIZE 30 /* Should be enough */
   37 #define ADTS_MAX_SIZE 10 /* Should be enough */
   38 
   39 static int sample_rates[] = {96000,88200,64000,48000,44100,32000,24000,22050,16000,12000,11025,8000};
   40 
   41 static int read_ADIF_header(FILE *file, faadAACInfo *info)
   42 {
   43     int bitstream;
   44     unsigned char buffer[ADIF_MAX_SIZE];
   45     int skip_size = 0;
   46     int sf_idx;
   47 
   48     /* Get ADIF header data */
   49     info->headertype = 1;
   50 
   51     if (fread(buffer, 1, ADIF_MAX_SIZE, file) != ADIF_MAX_SIZE)
   52         return -1;
   53 
   54     /* copyright string */
   55     if(buffer[0] & 0x80)
   56         skip_size += 9; /* skip 9 bytes */
   57 
   58     bitstream = buffer[0 + skip_size] & 0x10;
   59     info->bitrate = ((unsigned int)(buffer[0 + skip_size] & 0x0F)<<19)|
   60         ((unsigned int)buffer[1 + skip_size]<<11)|
   61         ((unsigned int)buffer[2 + skip_size]<<3)|
   62         ((unsigned int)buffer[3 + skip_size] & 0xE0);
   63 
   64     if (bitstream == 0)
   65     {
   66         info->object_type =  ((buffer[6 + skip_size]&0x01)<<1)|((buffer[7 + skip_size]&0x80)>>7);
   67         sf_idx = (buffer[7 + skip_size]&0x78)>>3;
   68     } else {
   69         info->object_type = (buffer[4 + skip_size] & 0x18)>>3;
   70         sf_idx = ((buffer[4 + skip_size] & 0x07)<<1)|((buffer[5 + skip_size] & 0x80)>>7);
   71     }
   72     info->sampling_rate = sample_rates[sf_idx];
   73 
   74     return 0;
   75 }
   76 
   77 static int read_ADTS_header(FILE *file, faadAACInfo *info)
   78 {
   79     /* Get ADTS header data */
   80     unsigned char buffer[ADTS_MAX_SIZE];
   81     int frames, t_framelength = 0, frame_length, sr_idx = 0, ID;
   82     int second = 0, pos;
   83     float frames_per_sec = 0;
   84     unsigned long bytes;
   85     unsigned long *tmp_seek_table = NULL;
   86 
   87     info->headertype = 2;
   88 
   89     /* Read all frames to ensure correct time and bitrate */
   90     for (frames = 0; /* */; frames++)
   91     {
   92         bytes = fread(buffer, 1, ADTS_MAX_SIZE, file);
   93 
   94         if (bytes != ADTS_MAX_SIZE)
   95             break;
   96 
   97         /* check syncword */
   98         if (!((buffer[0] == 0xFF)&&((buffer[1] & 0xF6) == 0xF0)))
   99             break;
  100 
  101         if (!frames)
  102         {
  103             /* fixed ADTS header is the same for every frame, so we read it only once */
  104             /* Syncword found, proceed to read in the fixed ADTS header */
  105             ID = buffer[1] & 0x08;
  106             info->object_type = (buffer[2]&0xC0)>>6;
  107             sr_idx = (buffer[2]&0x3C)>>2;
  108             info->channels = ((buffer[2]&0x01)<<2)|((buffer[3]&0xC0)>>6);
  109 
  110             frames_per_sec = sample_rates[sr_idx] / 1024.f;
  111         }
  112 
  113         /* ...and the variable ADTS header */
  114         if (ID == 0)
  115         {
  116             info->version = 4;
  117         } else { /* MPEG-2 */
  118             info->version = 2;
  119         }
  120         frame_length = ((((unsigned int)buffer[3] & 0x3)) << 11)
  121             | (((unsigned int)buffer[4]) << 3) | (buffer[5] >> 5);
  122 
  123         t_framelength += frame_length;
  124 
  125         pos = ftell(file) - ADTS_MAX_SIZE;
  126 
  127         fseek(file, frame_length - ADTS_MAX_SIZE, SEEK_CUR);
  128     }
  129 
  130     if (frames > 0)
  131     {
  132         float sec_per_frame, bytes_per_frame;
  133         info->sampling_rate = sample_rates[sr_idx];
  134         sec_per_frame = (float)info->sampling_rate/1024.0;
  135         bytes_per_frame = (float)t_framelength / (float)frames;
  136         info->bitrate = 8 * (int)floor(bytes_per_frame * sec_per_frame);
  137         info->length = (int)floor((float)frames/frames_per_sec)*1000;
  138     } else {
  139         info->sampling_rate = 4;
  140         info->bitrate = 128000;
  141         info->length = 0;
  142         info->channels = 0;
  143     }
  144 
  145     return 0;
  146 }
  147 
  148 int get_AAC_format(char *filename, faadAACInfo *info)
  149 {
  150     unsigned long tagsize;
  151     FILE *file;
  152     char buffer[10];
  153     unsigned long file_len;
  154     unsigned char adxx_id[5];
  155     unsigned long tmp;
  156 
  157     memset(info, 0, sizeof(faadAACInfo));
  158 
  159     file = fopen(filename, "rb");
  160 
  161     if(file == NULL)
  162         return -1;
  163 
  164     fseek(file, 0, SEEK_END);
  165     file_len = ftell(file);
  166     fseek(file, 0, SEEK_SET);
  167 
  168     /* Skip the tag, if it's there */
  169     tmp = fread(buffer, 10, 1, file);
  170 
  171     if (StringComp(buffer, "ID3", 3) == 0)
  172     {
  173         /* high bit is not used */
  174         tagsize = (buffer[6] << 21) | (buffer[7] << 14) |
  175             (buffer[8] <<  7) | (buffer[9] <<  0);
  176 
  177         fseek(file, tagsize, SEEK_CUR);
  178         tagsize += 10;
  179     } else {
  180         tagsize = 0;
  181         fseek(file, 0, SEEK_SET);
  182     }
  183 
  184     if (file_len)
  185         file_len -= tagsize;
  186 
  187     tmp = fread(adxx_id, 2, 1, file);
  188     adxx_id[5-1] = 0;
  189     info->length = 0;
  190 
  191     /* Determine the header type of the file, check the first two bytes */
  192     if (StringComp(adxx_id, "AD", 2) == 0)
  193     {
  194         /* We think its an ADIF header, but check the rest just to make sure */
  195         tmp = fread(adxx_id + 2, 2, 1, file);
  196 
  197         if (StringComp(adxx_id, "ADIF", 4) == 0)
  198         {
  199             read_ADIF_header(file, info);
  200 
  201             info->length = (int)((float)file_len*8000.0/((float)info->bitrate));
  202         }
  203     } else {
  204         /* No ADIF, check for ADTS header */
  205         if ((adxx_id[0] == 0xFF)&&((adxx_id[1] & 0xF6) == 0xF0))
  206         {
  207             /* ADTS  header located */
  208             fseek(file, tagsize, SEEK_SET);
  209             read_ADTS_header(file, info);
  210         } else {
  211             /* Unknown/headerless AAC file, assume format: */
  212             info->version = 2;
  213             info->bitrate = 128000;
  214             info->sampling_rate = 44100;
  215             info->channels = 2;
  216             info->headertype = 0;
  217             info->object_type = 1;
  218         }
  219     }
  220 
  221     fclose(file);
  222 
  223     return 0;
  224 }