eta name='generator' content='cgit v1.2.3-13-gbd6f'/>
aboutsummaryrefslogblamecommitdiff
path: root/src/plugins/Input/aac/aacfile.cpp
blob: 6d1a67f487441cdb10f4261f50456f16cfa8f2fa (plain) (tree)




















                                                                             

                     





                      
                            


                                                                                                                     
                                             
 
                      

                  
                     
                
















                                                           
 

                                            
     
                                    




                                                    
                         














                                                                                   
                         
     


                   
  










                          




                             









                                                       

                         
                                                


                             





























                                                                                                 
                                                                   

















                                                                           
                                                  










                                                                       
 
                          
 














                                                           
                    














                                                                          
 
 








                                                                         
 










                                                                                
 
/***************************************************************************
 *   Copyright (C) 2008 by Ilya Kotov                                      *
 *   forkotov02@hotmail.ru                                                 *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <QIODevice>
#include <QBuffer>
#include <QTextCodec>

#include <neaacdec.h>

#include "aacfile.h"

#define MAX_CHANNELS 6
#define AAC_BUFFER_SIZE 4096

static int adts_sample_rates[] = {96000,88200,64000,48000,44100,32000,24000,22050,16000,12000,11025,8000,7350,0,0,0};

AACFile::AACFile(QIODevice *i, bool metaData)
{
    m_isValid = false;
    m_length = 0;
    m_bitrate = 0;
    m_samplerate = 0;
    m_input = i;
    uchar buf[AAC_BUFFER_SIZE];
    qint64 buf_at = i->peek((char *) buf, AAC_BUFFER_SIZE);

    int tag_size = 0;
    if (!memcmp(buf, "ID3", 3)) //TODO parse id3 tag
    {
        /* high bit is not used */
        tag_size = (buf[6] << 21) | (buf[7] << 14) |
                   (buf[8] <<  7) | (buf[9] <<  0);

        tag_size += 10;
        if (buf_at - tag_size < 4)
        {
            qWarning("AACFile: invalid tag size");
            return;
        }
        memmove (buf, buf + tag_size, buf_at - tag_size);

        if (metaData)
            parseID3v2(); //parse id3v2 tags
    }
    //try to determnate header type;
    if (buf[0] == 0xff && ((buf[1] & 0xf6) == 0xf0))
    {
        qDebug("AACFile: ADTS header found");
        if (!i->isSequential())
            parseADTS();
        m_isValid = true;
    }
    else if (memcmp(buf, "ADIF", 4) == 0)
    {
        qDebug("AACFile: ADIF header found");
        int skip_size = (buf[4] & 0x80) ? 9 : 0;
        m_bitrate = ((buf[4 + skip_size] & 0x0F)<<19) |
                    (buf[5 + skip_size]<<11) |
                    (buf[6 + skip_size]<<3) |
                    (buf[7 + skip_size] & 0xE0);

        if (!i->isSequential ())
            m_length = (qint64) (((float)i->size()*8.f)/((float)m_bitrate) + 0.5f);
        else
            m_length = 0;
        m_bitrate = (int)((float)m_bitrate/1000.0f + 0.5f);
        m_isValid = true;
    }
}

AACFile::~AACFile()
{}