le>
aboutsummaryrefslogblamecommitdiff
path: root/src/plugins/Input/ffmpeg/decoder_ffmpeg.cpp
blob: 274e051608f830ee084bb66a781f16a5ceb8cc99 (plain) (tree)






















                                                                             



                           
















                                                                                         







                    














                               
           





















































                                                                                  
                       






























                                                                  
                     

























                                                                          
                                                            


























                                                                           
                                               
                       








                               
                       


























                                                           

                                                                   



                            







                                        
                     
 

                        

                                                                                              
 

                         


                                 

                                     


                                   
                   

















































                                                                                      
                                  










                                                                               
/***************************************************************************
 *   Copyright (C) 2006 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 <QObject>
#include <QFile>

#include <qmmp/constants.h>
#include <qmmp/buffer.h>
#include <qmmp/output.h>
#include <qmmp/recycler.h>

#include "decoder_ffmpeg.h"

// Decoder class

DecoderFFmpeg::DecoderFFmpeg(QObject *parent, DecoderFactory *d, QIODevice *i, Output *o)
        : Decoder(parent, d, i, o)
{
    inited = FALSE;
    user_stop = FALSE;
    stat = 0;
    output_buf = 0;
    output_bytes = 0;
    output_at = 0;
    bks = 0;
    done = FALSE;
    finish = FALSE;
    freq = 0;
    bitrate = 0;
    seekTime = -1.0;
    totalTime = 0.0;
    chan = 0;
    output_size = 0;
    ic = 0;
    wma_outbuf = 0;
}


DecoderFFmpeg::~DecoderFFmpeg()
{
    deinit();
    if (wma_outbuf)
    {
        delete [] wma_outbuf;
        wma_outbuf = 0;
    }
    if (output_buf)
        delete [] output_buf;
    output_buf = 0;

    if (ic)
        av_close_input_file(ic);
}


void DecoderFFmpeg::stop()
{
    user_stop = TRUE;
}


void DecoderFFmpeg::flush(bool final)
{
    ulong min = final ? 0 : bks;

    while ((! done && ! finish) && output_bytes > min)
    {
        output()->recycler()->mutex()->lock ();

        while ((! done && ! finish) && output()->recycler()->full())
        {
            mutex()->unlock();

            output()->recycler()->cond()->wait(output()->recycler()->mutex());

            mutex()->lock ();
            done = user_stop;
        }

        if (user_stop || finish)
        {
            inited = FALSE;
            done = TRUE;
        }
        else
        {
            output_bytes -= produceSound(output_buf, output_bytes, bitrate, chan);
            output_size += bks;
            output_at = output_bytes;
        }

        if (output()->recycler()->full())
        {
            output()->recycler()->cond()->wakeOne();
        }

        output()->recycler()->mutex()->unlock();
    }
}


bool DecoderFFmpeg::initialize()
{
    bks = blockSize();
    inited = user_stop = done = finish = FALSE;
    freq = bitrate = 0;
    stat = chan = 0;
    output_size = 0;
    seekTime = -1.0;
    totalTime = 0.0;


    if (! input())
    {
        error("DecoderFFmpeg: cannot initialize.  No input.");

        return FALSE;
    }

    if (! output_buf)
        output_buf = new char[globalBufferSize];
    output_at = 0;
    output_bytes = 0;

    if (! input())
    {
        error("DecoderFFmpeg: cannot initialize.  No input.");

        return FALSE;
    }

    if (! output_buf)
        output_buf = new char[globalBufferSize];
    output_at = 0;
    output_bytes = 0;

    QString filename = qobject_cast<QFile*>(input())->fileName ();
    input()->close();
    avcodec_init();
    avcodec_register_all();
    av_register_all();

    AVCodec *codec;
    if (av_open_input_file(&ic, filename.toLocal8Bit(), NULL,0, NULL) < 0)
    {
        qDebug("DecoderFFmpeg: cannot open input file");
        return FALSE;
    }
    for (wma_idx = 0; wma_idx < ic->nb_streams; wma_idx++)
    {
        c = ic->streams[wma_idx]->codec;
        if (c->codec_type == CODEC_TYPE_AUDIO) break;
    }

    av_find_stream_info(ic);

    codec = avcodec_find_decoder(c->codec_id);

    if (!codec) return FALSE;
    if (avcodec_open(c, codec) < 0)
        return FALSE;

    totalTime = ic->duration/AV_TIME_BASE;

    configure(c->sample_rate, c->channels, 16, c->bit_rate);

    bitrate = c->bit_rate;
    chan = c->channels;
    wma_outbuf = new uint8_t[AVCODEC_MAX_AUDIO_FRAME_SIZE*sizeof(int16_t)];
    inited = TRUE;
    qDebug("DecoderFFmpeg: initialize succes");
    return TRUE;
}


double DecoderFFmpeg::lengthInSeconds()
{
    if (! inited)
        return 0;

    return totalTime;
}


void DecoderFFmpeg::seek(double pos)
{
    seekTime = pos;
}


void DecoderFFmpeg::deinit()
{
    inited = user_stop = done = finish = FALSE;
    freq = bitrate = 0;
    stat = chan = 0;
    output_size = 0;
}

void DecoderFFmpeg::run()
{
//     mpc_uint32_t vbrAcc = 0;
//     mpc_uint32_t vbrUpd = 0;
    uint8_t *inbuf_ptr;
    int out_size, size;
    AVPacket pkt;

    mutex()->lock ();