3D Audio GSoC:
[blender.git] / intern / audaspace / ffmpeg / AUD_FFMPEGReader.cpp
index b9d5d30436879d3c9dd833ac361cdd73a92bf62e..b7690d55383bdaac82f9a07dcb9cbc80a3d1cca5 100644 (file)
@@ -1,28 +1,34 @@
 /*
  * $Id$
  *
- * ***** BEGIN LGPL LICENSE BLOCK *****
+ * ***** BEGIN GPL LICENSE BLOCK *****
  *
- * Copyright 2009 Jörg Hermann Müller
+ * Copyright 2009-2011 Jörg Hermann Müller
  *
  * This file is part of AudaSpace.
  *
- * AudaSpace is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
+ * Audaspace 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.
  *
  * AudaSpace 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 Lesser General Public License for more details.
+ * GNU General Public License for more details.
  *
- * You should have received a copy of the GNU Lesser General Public License
- * along with AudaSpace.  If not, see <http://www.gnu.org/licenses/>.
+ * You should have received a copy of the GNU General Public License
+ * along with Audaspace; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  *
- * ***** END LGPL LICENSE BLOCK *****
+ * ***** END GPL LICENSE BLOCK *****
  */
 
+/** \file audaspace/ffmpeg/AUD_FFMPEGReader.cpp
+ *  \ingroup audffmpeg
+ */
+
+
 // needed for INT64_C
 #ifndef __STDC_CONSTANT_MACROS
 #define __STDC_CONSTANT_MACROS
@@ -33,6 +39,7 @@
 extern "C" {
 #include <libavcodec/avcodec.h>
 #include <libavformat/avformat.h>
+#include "ffmpeg_compat.h"
 }
 
 int AUD_FFMPEGReader::decode(AVPacket* packet, AUD_Buffer& buffer)
@@ -46,6 +53,10 @@ int AUD_FFMPEGReader::decode(AVPacket* packet, AUD_Buffer& buffer)
 
        int read_length, data_size;
 
+       AVPacket tmp_pkt;
+       
+       av_init_packet(&tmp_pkt);
+
        // as long as there is still data in the package
        while(audio_pkg_size > 0)
        {
@@ -58,10 +69,14 @@ int AUD_FFMPEGReader::decode(AVPacket* packet, AUD_Buffer& buffer)
 
                // read samples from the packet
                data_size = buf_size - buf_pos;
-               read_length = avcodec_decode_audio3(m_codecCtx,
-                       (int16_t*)(((data_t*)buffer.getBuffer())+buf_pos),
-                       &data_size,
-                       packet);
+
+               tmp_pkt.data = audio_pkg_data;
+               tmp_pkt.size = audio_pkg_size;
+
+               read_length = avcodec_decode_audio3(
+                       m_codecCtx,
+                       (int16_t*)(((data_t*)buffer.getBuffer()) + buf_pos),
+                       &data_size, &tmp_pkt);
 
                // read error, next packet!
                if(read_length < 0)
@@ -101,7 +116,7 @@ void AUD_FFMPEGReader::init()
 
        for(unsigned int i = 0; i < m_formatCtx->nb_streams; i++)
        {
-               if((m_formatCtx->streams[i]->codec->codec_type == CODEC_TYPE_AUDIO)
+               if((m_formatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
                        && (m_stream < 0))
                {
                        m_stream=i;
@@ -161,7 +176,8 @@ static const char* fileopen_error = "AUD_FFMPEGReader: File couldn't be "
 
 AUD_FFMPEGReader::AUD_FFMPEGReader(std::string filename) :
        m_pkgbuf(AVCODEC_MAX_AUDIO_FRAME_SIZE<<1),
-       m_byteiocontext(NULL)
+       m_byteiocontext(NULL),
+       m_membuf(NULL)
 {
        // open file
        if(av_open_input_file(&m_formatCtx, filename.c_str(), NULL, 0, NULL)!=0)
@@ -183,12 +199,15 @@ static const char* streamopen_error = "AUD_FFMPEGReader: Stream couldn't be "
 
 AUD_FFMPEGReader::AUD_FFMPEGReader(AUD_Reference<AUD_Buffer> buffer) :
                m_pkgbuf(AVCODEC_MAX_AUDIO_FRAME_SIZE<<1),
-               m_membuffer(buffer)
+               m_membuffer(buffer),
+               m_membufferpos(0)
 {
-       m_byteiocontext = (ByteIOContext*)av_mallocz(sizeof(ByteIOContext));
+       m_membuf = reinterpret_cast<data_t*>(av_malloc(FF_MIN_BUFFER_SIZE + FF_INPUT_BUFFER_PADDING_SIZE));
 
-       if(init_put_byte(m_byteiocontext, (data_t*)buffer.get()->getBuffer(),
-                                        buffer.get()->getSize(), 0, NULL, NULL, NULL, NULL) != 0)
+       m_byteiocontext = av_alloc_put_byte(m_membuf, FF_MIN_BUFFER_SIZE, 0, this,
+                                                                               read_packet, NULL, seek_packet);
+
+       if(!m_byteiocontext)
        {
                av_free(m_byteiocontext);
                AUD_THROW(AUD_ERROR_FILE, fileopen_error);
@@ -196,7 +215,7 @@ AUD_FFMPEGReader::AUD_FFMPEGReader(AUD_Reference<AUD_Buffer> buffer) :
 
        AVProbeData probe_data;
        probe_data.filename = "";
-       probe_data.buf = (data_t*)buffer.get()->getBuffer();
+       probe_data.buf = reinterpret_cast<data_t*>(buffer.get()->getBuffer());
        probe_data.buf_size = buffer.get()->getSize();
        AVInputFormat* fmt = av_probe_input_format(&probe_data, 1);
 
@@ -232,6 +251,40 @@ AUD_FFMPEGReader::~AUD_FFMPEGReader()
                av_close_input_file(m_formatCtx);
 }
 
+int AUD_FFMPEGReader::read_packet(void* opaque, uint8_t* buf, int buf_size)
+{
+       AUD_FFMPEGReader* reader = reinterpret_cast<AUD_FFMPEGReader*>(opaque);
+
+       int size = AUD_MIN(buf_size, reader->m_membuffer->getSize() - reader->m_membufferpos);
+
+       if(size < 0)
+               return -1;
+
+       memcpy(buf, ((data_t*)reader->m_membuffer->getBuffer()) + reader->m_membufferpos, size);
+       reader->m_membufferpos += size;
+
+       return size;
+}
+
+int64_t AUD_FFMPEGReader::seek_packet(void* opaque, int64_t offset, int whence)
+{
+       AUD_FFMPEGReader* reader = reinterpret_cast<AUD_FFMPEGReader*>(opaque);
+
+       switch(whence)
+       {
+       case SEEK_SET:
+               reader->m_membufferpos = 0;
+               break;
+       case SEEK_END:
+               reader->m_membufferpos = reader->m_membuffer->getSize();
+               break;
+       case AVSEEK_SIZE:
+               return reader->m_membuffer->getSize();
+       }
+
+       return (reader->m_membufferpos += offset);
+}
+
 bool AUD_FFMPEGReader::isSeekable() const
 {
        return true;
@@ -241,13 +294,26 @@ void AUD_FFMPEGReader::seek(int position)
 {
        if(position >= 0)
        {
+               uint64_t st_time = m_formatCtx->start_time;
+               uint64_t seek_pos = ((uint64_t)position) * ((uint64_t)AV_TIME_BASE) / ((uint64_t)m_specs.rate);
+
+               if (seek_pos < 0) {
+                       seek_pos = 0;
+               }
+
+               if (st_time != AV_NOPTS_VALUE) {
+                       seek_pos += st_time;
+               }
+
+               double pts_time_base = 
+                       av_q2d(m_formatCtx->streams[m_stream]->time_base);
+               uint64_t pts_st_time =
+                       ((st_time != AV_NOPTS_VALUE) ? st_time : 0)
+                       / pts_time_base / (uint64_t) AV_TIME_BASE;
+
                // a value < 0 tells us that seeking failed
-               if(av_seek_frame(m_formatCtx,
-                                                -1,
-                                                (uint64_t)(((uint64_t)position *
-                                                                        (uint64_t)AV_TIME_BASE) /
-                                                                       (uint64_t)m_specs.rate),
-                                                AVSEEK_FLAG_BACKWARD | AVSEEK_FLAG_ANY) >= 0)
+               if(av_seek_frame(m_formatCtx, -1, seek_pos,
+                                AVSEEK_FLAG_BACKWARD | AVSEEK_FLAG_ANY) >= 0)
                {
                        avcodec_flush_buffers(m_codecCtx);
                        m_position = position;
@@ -268,22 +334,22 @@ void AUD_FFMPEGReader::seek(int position)
                                        if(packet.pts != AV_NOPTS_VALUE)
                                        {
                                                // calculate real position, and read to frame!
-                                               m_position = packet.pts *
-                                                       av_q2d(m_formatCtx->streams[m_stream]->time_base) *
-                                                       m_specs.rate;
+                                               m_position = (packet.pts - 
+                                                       pts_st_time) * pts_time_base * m_specs.rate;
 
                                                if(m_position < position)
                                                {
                                                        // read until we're at the right position
                                                        int length = AUD_DEFAULT_BUFFER_SIZE;
-                                                       sample_t* buffer;
+                                                       AUD_Buffer buffer(length * AUD_SAMPLE_SIZE(m_specs));
+                                                       bool eos;
                                                        for(int len = position - m_position;
                                                                length == AUD_DEFAULT_BUFFER_SIZE;
                                                                len -= AUD_DEFAULT_BUFFER_SIZE)
                                                        {
                                                                if(len < AUD_DEFAULT_BUFFER_SIZE)
                                                                        length = len;
-                                                               read(length, buffer);
+                                                               read(length, eos, buffer.getBuffer());
                                                        }
                                                }
                                        }
@@ -293,6 +359,7 @@ void AUD_FFMPEGReader::seek(int position)
                }
                else
                {
+                       fprintf(stderr, "seeking failed!\n");
                        // Seeking failed, do nothing.
                }
        }
@@ -315,7 +382,7 @@ AUD_Specs AUD_FFMPEGReader::getSpecs() const
        return m_specs.specs;
 }
 
-void AUD_FFMPEGReader::read(int & length, sample_t* & buffer)
+void AUD_FFMPEGReader::read(int& length, bool& eos, sample_t* buffer)
 {
        // read packages and decode them
        AVPacket packet;
@@ -324,11 +391,7 @@ void AUD_FFMPEGReader::read(int & length, sample_t* & buffer)
        int left = length;
        int sample_size = AUD_DEVICE_SAMPLE_SIZE(m_specs);
 
-       // resize output buffer if necessary
-       if(m_buffer.getSize() < length * AUD_SAMPLE_SIZE(m_specs))
-               m_buffer.resize(length * AUD_SAMPLE_SIZE(m_specs));
-
-       buffer = m_buffer.getBuffer();
+       sample_t* buf = buffer;
        pkgbuf_pos = m_pkgbuf_left;
        m_pkgbuf_left = 0;
 
@@ -336,9 +399,9 @@ void AUD_FFMPEGReader::read(int & length, sample_t* & buffer)
        if(pkgbuf_pos > 0)
        {
                data_size = AUD_MIN(pkgbuf_pos, left * sample_size);
-               m_convert((data_t*) buffer, (data_t*) m_pkgbuf.getBuffer(),
+               m_convert((data_t*) buf, (data_t*) m_pkgbuf.getBuffer(),
                                  data_size / AUD_FORMAT_SIZE(m_specs.format));
-               buffer += data_size / AUD_FORMAT_SIZE(m_specs.format);
+               buf += data_size / AUD_FORMAT_SIZE(m_specs.format);
                left -= data_size/sample_size;
        }
 
@@ -353,9 +416,9 @@ void AUD_FFMPEGReader::read(int & length, sample_t* & buffer)
 
                        // copy to output buffer
                        data_size = AUD_MIN(pkgbuf_pos, left * sample_size);
-                       m_convert((data_t*) buffer, (data_t*) m_pkgbuf.getBuffer(),
+                       m_convert((data_t*) buf, (data_t*) m_pkgbuf.getBuffer(),
                                          data_size / AUD_FORMAT_SIZE(m_specs.format));
-                       buffer += data_size / AUD_FORMAT_SIZE(m_specs.format);
+                       buf += data_size / AUD_FORMAT_SIZE(m_specs.format);
                        left -= data_size/sample_size;
                }
                av_free_packet(&packet);
@@ -369,9 +432,8 @@ void AUD_FFMPEGReader::read(int & length, sample_t* & buffer)
                                pkgbuf_pos-data_size);
        }
 
-       buffer = m_buffer.getBuffer();
-
-       if(left > 0)
+       if((eos = (left > 0)))
                length -= left;
+
        m_position += length;
 }