4 * ***** BEGIN LGPL LICENSE BLOCK *****
6 * Copyright 2009 Jörg Hermann Müller
8 * This file is part of AudaSpace.
10 * AudaSpace is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
15 * AudaSpace is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with AudaSpace. If not, see <http://www.gnu.org/licenses/>.
23 * ***** END LGPL LICENSE BLOCK *****
27 #ifndef __STDC_CONSTANT_MACROS
28 #define __STDC_CONSTANT_MACROS
31 #include "AUD_FFMPEGReader.h"
34 #include <libavcodec/avcodec.h>
35 #include <libavformat/avformat.h>
38 int AUD_FFMPEGReader::decode(AVPacket* packet, AUD_Buffer& buffer)
40 // save packet parameters
41 uint8_t *audio_pkg_data = packet->data;
42 int audio_pkg_size = packet->size;
44 int buf_size = buffer.getSize();
47 int read_length, data_size;
49 // as long as there is still data in the package
50 while(audio_pkg_size > 0)
52 // resize buffer if needed
53 if(buf_size - buf_pos < AVCODEC_MAX_AUDIO_FRAME_SIZE)
55 buffer.resize(buf_size + AVCODEC_MAX_AUDIO_FRAME_SIZE, true);
56 buf_size += AVCODEC_MAX_AUDIO_FRAME_SIZE;
59 // read samples from the packet
60 data_size = buf_size - buf_pos;
61 /*read_length = avcodec_decode_audio3(m_codecCtx,
62 (int16_t*)(((data_t*)buffer.getBuffer())+buf_pos),
65 read_length = avcodec_decode_audio2(m_codecCtx,
66 (int16_t*)(((data_t*)buffer.getBuffer()) + buf_pos),
71 // read error, next packet!
77 // move packet parameters
78 audio_pkg_data += read_length;
79 audio_pkg_size -= read_length;
85 static const char* streaminfo_error = "AUD_FFMPEGReader: Stream info couldn't "
87 static const char* noaudio_error = "AUD_FFMPEGReader: File doesn't include an "
89 static const char* nodecoder_error = "AUD_FFMPEGReader: No decoder found for "
91 static const char* codecopen_error = "AUD_FFMPEGReader: Codec couldn't be "
93 static const char* format_error = "AUD_FFMPEGReader: Unsupported sample "
96 void AUD_FFMPEGReader::init()
101 if(av_find_stream_info(m_formatCtx)<0)
102 AUD_THROW(AUD_ERROR_FFMPEG, streaminfo_error);
104 // find audio stream and codec
107 for(unsigned int i = 0; i < m_formatCtx->nb_streams; i++)
109 if((m_formatCtx->streams[i]->codec->codec_type == CODEC_TYPE_AUDIO)
118 AUD_THROW(AUD_ERROR_FFMPEG, noaudio_error);
120 m_codecCtx = m_formatCtx->streams[m_stream]->codec;
122 // get a decoder and open it
123 AVCodec *aCodec = avcodec_find_decoder(m_codecCtx->codec_id);
125 AUD_THROW(AUD_ERROR_FFMPEG, nodecoder_error);
127 if(avcodec_open(m_codecCtx, aCodec)<0)
128 AUD_THROW(AUD_ERROR_FFMPEG, codecopen_error);
130 // XXX this prints file information to stdout:
131 //dump_format(m_formatCtx, 0, NULL, 0);
133 m_specs.channels = (AUD_Channels) m_codecCtx->channels;
135 switch(m_codecCtx->sample_fmt)
138 m_convert = AUD_convert_u8_float;
139 m_specs.format = AUD_FORMAT_U8;
142 m_convert = AUD_convert_s16_float;
143 m_specs.format = AUD_FORMAT_S16;
146 m_convert = AUD_convert_s32_float;
147 m_specs.format = AUD_FORMAT_S32;
150 m_convert = AUD_convert_copy<float>;
151 m_specs.format = AUD_FORMAT_FLOAT32;
154 m_convert = AUD_convert_double_float;
155 m_specs.format = AUD_FORMAT_FLOAT64;
158 AUD_THROW(AUD_ERROR_FFMPEG, format_error);
161 m_specs.rate = (AUD_SampleRate) m_codecCtx->sample_rate;
164 static const char* fileopen_error = "AUD_FFMPEGReader: File couldn't be "
167 AUD_FFMPEGReader::AUD_FFMPEGReader(std::string filename) :
168 m_pkgbuf(AVCODEC_MAX_AUDIO_FRAME_SIZE<<1),
169 m_byteiocontext(NULL)
172 if(av_open_input_file(&m_formatCtx, filename.c_str(), NULL, 0, NULL)!=0)
173 AUD_THROW(AUD_ERROR_FILE, fileopen_error);
179 catch(AUD_Exception&)
181 av_close_input_file(m_formatCtx);
186 static const char* streamopen_error = "AUD_FFMPEGReader: Stream couldn't be "
189 AUD_FFMPEGReader::AUD_FFMPEGReader(AUD_Reference<AUD_Buffer> buffer) :
190 m_pkgbuf(AVCODEC_MAX_AUDIO_FRAME_SIZE<<1),
193 m_byteiocontext = (ByteIOContext*)av_mallocz(sizeof(ByteIOContext));
195 if(init_put_byte(m_byteiocontext, (data_t*)buffer.get()->getBuffer(),
196 buffer.get()->getSize(), 0, NULL, NULL, NULL, NULL) != 0)
198 av_free(m_byteiocontext);
199 AUD_THROW(AUD_ERROR_FILE, fileopen_error);
202 AVProbeData probe_data;
203 probe_data.filename = "";
204 probe_data.buf = (data_t*)buffer.get()->getBuffer();
205 probe_data.buf_size = buffer.get()->getSize();
206 AVInputFormat* fmt = av_probe_input_format(&probe_data, 1);
209 if(av_open_input_stream(&m_formatCtx, m_byteiocontext, "", fmt, NULL)!=0)
211 av_free(m_byteiocontext);
212 AUD_THROW(AUD_ERROR_FILE, streamopen_error);
219 catch(AUD_Exception&)
221 av_close_input_stream(m_formatCtx);
222 av_free(m_byteiocontext);
227 AUD_FFMPEGReader::~AUD_FFMPEGReader()
229 avcodec_close(m_codecCtx);
233 av_close_input_stream(m_formatCtx);
234 av_free(m_byteiocontext);
237 av_close_input_file(m_formatCtx);
240 bool AUD_FFMPEGReader::isSeekable() const
245 void AUD_FFMPEGReader::seek(int position)
249 uint64_t st_time = m_formatCtx->start_time;
250 uint64_t seek_pos = position * AV_TIME_BASE / m_specs.rate;
256 if (st_time != AV_NOPTS_VALUE) {
260 double pts_time_base =
261 av_q2d(m_formatCtx->streams[m_stream]->time_base);
262 uint64_t pts_st_time =
263 ((st_time != AV_NOPTS_VALUE) ? st_time : 0)
264 / pts_time_base / (uint64_t) AV_TIME_BASE;
266 // a value < 0 tells us that seeking failed
267 if(av_seek_frame(m_formatCtx, -1, seek_pos,
268 AVSEEK_FLAG_BACKWARD | AVSEEK_FLAG_ANY) >= 0)
270 avcodec_flush_buffers(m_codecCtx);
271 m_position = position;
276 while(search && av_read_frame(m_formatCtx, &packet) >= 0)
278 // is it a frame from the audio stream?
279 if(packet.stream_index == m_stream)
281 // decode the package
282 m_pkgbuf_left = decode(&packet, m_pkgbuf);
286 if(packet.pts != AV_NOPTS_VALUE)
288 // calculate real position, and read to frame!
289 m_position = (packet.pts -
290 pts_st_time) * pts_time_base * m_specs.rate;
292 if(m_position < position)
294 // read until we're at the right position
295 int length = AUD_DEFAULT_BUFFER_SIZE;
297 for(int len = position - m_position;
298 length == AUD_DEFAULT_BUFFER_SIZE;
299 len -= AUD_DEFAULT_BUFFER_SIZE)
301 if(len < AUD_DEFAULT_BUFFER_SIZE)
303 read(length, buffer);
308 av_free_packet(&packet);
313 fprintf(stderr, "seeking failed!\n");
314 // Seeking failed, do nothing.
319 int AUD_FFMPEGReader::getLength() const
321 // return approximated remaning size
322 return (int)((m_formatCtx->duration * m_codecCtx->sample_rate)
323 / AV_TIME_BASE)-m_position;
326 int AUD_FFMPEGReader::getPosition() const
331 AUD_Specs AUD_FFMPEGReader::getSpecs() const
333 return m_specs.specs;
336 void AUD_FFMPEGReader::read(int & length, sample_t* & buffer)
338 // read packages and decode them
343 int sample_size = AUD_DEVICE_SAMPLE_SIZE(m_specs);
345 // resize output buffer if necessary
346 if(m_buffer.getSize() < length * AUD_SAMPLE_SIZE(m_specs))
347 m_buffer.resize(length * AUD_SAMPLE_SIZE(m_specs));
349 buffer = m_buffer.getBuffer();
350 pkgbuf_pos = m_pkgbuf_left;
353 // there may still be data in the buffer from the last call
356 data_size = AUD_MIN(pkgbuf_pos, left * sample_size);
357 m_convert((data_t*) buffer, (data_t*) m_pkgbuf.getBuffer(),
358 data_size / AUD_FORMAT_SIZE(m_specs.format));
359 buffer += data_size / AUD_FORMAT_SIZE(m_specs.format);
360 left -= data_size/sample_size;
363 // for each frame read as long as there isn't enough data already
364 while((left > 0) && (av_read_frame(m_formatCtx, &packet) >= 0))
366 // is it a frame from the audio stream?
367 if(packet.stream_index == m_stream)
369 // decode the package
370 pkgbuf_pos = decode(&packet, m_pkgbuf);
372 // copy to output buffer
373 data_size = AUD_MIN(pkgbuf_pos, left * sample_size);
374 m_convert((data_t*) buffer, (data_t*) m_pkgbuf.getBuffer(),
375 data_size / AUD_FORMAT_SIZE(m_specs.format));
376 buffer += data_size / AUD_FORMAT_SIZE(m_specs.format);
377 left -= data_size/sample_size;
379 av_free_packet(&packet);
381 // read more data than necessary?
382 if(pkgbuf_pos > data_size)
384 m_pkgbuf_left = pkgbuf_pos-data_size;
385 memmove(m_pkgbuf.getBuffer(),
386 ((data_t*)m_pkgbuf.getBuffer())+data_size,
387 pkgbuf_pos-data_size);
390 buffer = m_buffer.getBuffer();
394 m_position += length;