aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/Input/cdaudio/decodercdaudiofactory.cpp
diff options
context:
space:
mode:
authortrialuser02 <trialuser02@90c681e8-e032-0410-971d-27865f9a5e38>2009-06-19 22:03:07 +0000
committertrialuser02 <trialuser02@90c681e8-e032-0410-971d-27865f9a5e38>2009-06-19 22:03:07 +0000
commit5bf1064719a6e0584ad44fcb18a8c2f1c9ca1803 (patch)
tree88538467a399026521a2344fc0b93e76d7666168 /src/plugins/Input/cdaudio/decodercdaudiofactory.cpp
parent31d3fbf7548209bcbf0c2b6ab3fce8d4b8ce7f0c (diff)
downloadqmmp-5bf1064719a6e0584ad44fcb18a8c2f1c9ca1803.tar.gz
qmmp-5bf1064719a6e0584ad44fcb18a8c2f1c9ca1803.tar.bz2
qmmp-5bf1064719a6e0584ad44fcb18a8c2f1c9ca1803.zip
fixed audio skipping when using soft volume
git-svn-id: http://svn.code.sf.net/p/qmmp-dev/code/trunk/qmmp@973 90c681e8-e032-0410-971d-27865f9a5e38
Diffstat (limited to 'src/plugins/Input/cdaudio/decodercdaudiofactory.cpp')
0 files changed, 0 insertions, 0 deletions
ef='#n133'>133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
/***************************************************************************
 *   Copyright (C) 2008-2012 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.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 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, bool adts)
{
    m_isValid = false;
    m_length = 0;
    m_bitrate = 0;
    m_samplerate = 0;
    m_input = i;
    m_offset = 0;
    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);
        buf_at -= tag_size;
        m_offset = tag_size;

        if (metaData)
            parseID3v2(); //parse id3v2 tags
    }

    int adts_offset = 0;

    while(adts_offset < buf_at - 6)
    {
        //try to determnate header type;
        if (buf[adts_offset] == 0xff && ((buf[adts_offset+1] & 0xf6) == 0xf0))
        {
            int frame_length = ((((unsigned int)buf[adts_offset+3] & 0x3)) << 11)
                    | (((unsigned int)buf[adts_offset+4]) << 3) | (buf[adts_offset+5] >> 5);

            if(adts_offset + frame_length > buf_at - 6)
                break;

            //check second sync word
            if ((buf[adts_offset + frame_length] == 0xFF) &&
                    ((buf[adts_offset + frame_length + 1] & 0xF6) == 0xF0))
            {
                qDebug("AACFile: ADTS header found");
            }
            else
                break;

            if (!i->isSequential() && adts)
                parseADTS();
            m_isValid = true;
            m_offset += adts_offset;
            return;
        }
        adts_offset++;
    }

    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()
{}

qint64 AACFile::length()
{
    return m_length;
}

quint32 AACFile::bitrate()
{
    return m_bitrate;
}

quint32 AACFile::samplerate()
{
    return m_samplerate;
}

int AACFile::offset() const
{
    return m_offset;
}

bool AACFile::isValid()
{
    return m_isValid;
}

const QMap<Qmmp::MetaData, QString> AACFile::metaData()
{
    return m_metaData;
}

void AACFile::parseADTS()
{
    uchar buf[FAAD_MIN_STREAMSIZE*MAX_CHANNELS];
    qint64 buf_at = 0;
    int frames, frame_length;
    int t_framelength = 0;
    float frames_per_sec, bytes_per_frame;
    qint64 pos = m_input->pos();

    m_input->seek(0);

    buf_at = m_input->read((char *)buf, FAAD_MIN_STREAMSIZE*MAX_CHANNELS);

    for (int i = 0; i < buf_at - 1; i++)
    {
        if (buf[i] == 0xff && (buf[i+1]&0xf6) == 0xf0)
        {
            memmove (buf, buf + i, buf_at - i);
            buf_at -= i;
            break;
        }
    }

    /* Read all frames to ensure correct time and bitrate */
    for (frames = 0; /* */; frames++)
    {
        //qDebug("frame header = %d", buf[0]);
        buf_at += m_input->read((char *)buf + buf_at, FAAD_MIN_STREAMSIZE*MAX_CHANNELS - buf_at);

        if (buf_at > 7)
        {
            /* check syncword */
            if (!((buf[0] == 0xFF)&&((buf[1] & 0xF6) == 0xF0)))
                break;

            if (frames == 0)
                m_samplerate = adts_sample_rates[(buf[2]&0x3c)>>2];

            frame_length = ((((unsigned int)buf[3] & 0x3)) << 11)
                           | (((unsigned int)buf[4]) << 3) | (buf[5] >> 5);

            t_framelength += frame_length;

            if (frame_length > buf_at)
                break;

            buf_at -= frame_length;
            memmove(buf, buf + frame_length, buf_at);
        }
        else
        {
            break;
        }
    }
    m_input->seek(pos);
    frames_per_sec = (float) m_samplerate/1024.0f;
    if (frames != 0)
        bytes_per_frame = (float)t_framelength/(float)(frames*1000);
    else
        bytes_per_frame = 0;
    m_bitrate = (quint32)(8. * bytes_per_frame * frames_per_sec + 0.5);

    if (frames_per_sec != 0)
        m_length = frames/frames_per_sec;
    else
        m_length = 1;
}

void AACFile::parseID3v2()
{
    QByteArray array = m_input->peek(2048);
    int offset = array.indexOf("ID3");
    if (offset < 0)
        return;
    ID3v2Tag taglib_tag(&array, offset);
    if (taglib_tag.isEmpty())