Reduce amount of deprecated symbols used from FFmpeg
[blender.git] / intern / audaspace / ffmpeg / AUD_FFMPEGReader.cpp
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * Copyright 2009-2011 Jörg Hermann Müller
5  *
6  * This file is part of AudaSpace.
7  *
8  * Audaspace is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * AudaSpace is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with Audaspace; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  * ***** END GPL LICENSE BLOCK *****
23  */
24
25 /** \file audaspace/ffmpeg/AUD_FFMPEGReader.cpp
26  *  \ingroup audffmpeg
27  */
28
29
30 // needed for INT64_C
31 #ifndef __STDC_CONSTANT_MACROS
32 #define __STDC_CONSTANT_MACROS
33 #endif
34
35 #include "AUD_FFMPEGReader.h"
36
37 extern "C" {
38 #include <libavcodec/avcodec.h>
39 #include <libavformat/avformat.h>
40 #include <libavformat/avio.h>
41 #include "ffmpeg_compat.h"
42 }
43
44 int AUD_FFMPEGReader::decode(AVPacket* packet, AUD_Buffer& buffer)
45 {
46         // save packet parameters
47         uint8_t *audio_pkg_data = packet->data;
48         int audio_pkg_size = packet->size;
49
50         int buf_size = buffer.getSize();
51         int buf_pos = 0;
52
53         int read_length, data_size;
54
55         AVPacket tmp_pkt;
56         
57         av_init_packet(&tmp_pkt);
58
59         // as long as there is still data in the package
60         while(audio_pkg_size > 0)
61         {
62                 // resize buffer if needed
63                 if(buf_size - buf_pos < AVCODEC_MAX_AUDIO_FRAME_SIZE)
64                 {
65                         buffer.resize(buf_size + AVCODEC_MAX_AUDIO_FRAME_SIZE, true);
66                         buf_size += AVCODEC_MAX_AUDIO_FRAME_SIZE;
67                 }
68
69                 // read samples from the packet
70                 data_size = buf_size - buf_pos;
71
72                 tmp_pkt.data = audio_pkg_data;
73                 tmp_pkt.size = audio_pkg_size;
74
75                 read_length = avcodec_decode_audio3(
76                         m_codecCtx,
77                         (int16_t*)(((data_t*)buffer.getBuffer()) + buf_pos),
78                         &data_size, &tmp_pkt);
79
80                 // read error, next packet!
81                 if(read_length < 0)
82                         break;
83
84                 buf_pos += data_size;
85
86                 // move packet parameters
87                 audio_pkg_data += read_length;
88                 audio_pkg_size -= read_length;
89         }
90
91         return buf_pos;
92 }
93
94 static const char* streaminfo_error = "AUD_FFMPEGReader: Stream info couldn't "
95                                                                           "be found.";
96 static const char* noaudio_error = "AUD_FFMPEGReader: File doesn't include an "
97                                                                    "audio stream.";
98 static const char* nodecoder_error = "AUD_FFMPEGReader: No decoder found for "
99                                                                          "the audio stream.";
100 static const char* codecopen_error = "AUD_FFMPEGReader: Codec couldn't be "
101                                                                          "opened.";
102 static const char* format_error = "AUD_FFMPEGReader: Unsupported sample "
103                                                                   "format.";
104
105 void AUD_FFMPEGReader::init()
106 {
107         m_position = 0;
108         m_pkgbuf_left = 0;
109
110         if(av_find_stream_info(m_formatCtx)<0)
111                 AUD_THROW(AUD_ERROR_FFMPEG, streaminfo_error);
112
113         // find audio stream and codec
114         m_stream = -1;
115
116         for(unsigned int i = 0; i < m_formatCtx->nb_streams; i++)
117         {
118                 if((m_formatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
119                         && (m_stream < 0))
120                 {
121                         m_stream=i;
122                         break;
123                 }
124         }
125
126         if(m_stream == -1)
127                 AUD_THROW(AUD_ERROR_FFMPEG, noaudio_error);
128
129         m_codecCtx = m_formatCtx->streams[m_stream]->codec;
130
131         // get a decoder and open it
132         AVCodec *aCodec = avcodec_find_decoder(m_codecCtx->codec_id);
133         if(!aCodec)
134                 AUD_THROW(AUD_ERROR_FFMPEG, nodecoder_error);
135
136         if(avcodec_open(m_codecCtx, aCodec)<0)
137                 AUD_THROW(AUD_ERROR_FFMPEG, codecopen_error);
138
139         // XXX this prints file information to stdout:
140         //dump_format(m_formatCtx, 0, NULL, 0);
141
142         m_specs.channels = (AUD_Channels) m_codecCtx->channels;
143
144         switch(m_codecCtx->sample_fmt)
145         {
146         case AV_SAMPLE_FMT_U8:
147                 m_convert = AUD_convert_u8_float;
148                 m_specs.format = AUD_FORMAT_U8;
149                 break;
150         case AV_SAMPLE_FMT_S16:
151                 m_convert = AUD_convert_s16_float;
152                 m_specs.format = AUD_FORMAT_S16;
153                 break;
154         case AV_SAMPLE_FMT_S32:
155                 m_convert = AUD_convert_s32_float;
156                 m_specs.format = AUD_FORMAT_S32;
157                 break;
158         case AV_SAMPLE_FMT_FLT:
159                 m_convert = AUD_convert_copy<float>;
160                 m_specs.format = AUD_FORMAT_FLOAT32;
161                 break;
162         case AV_SAMPLE_FMT_DBL:
163                 m_convert = AUD_convert_double_float;
164                 m_specs.format = AUD_FORMAT_FLOAT64;
165                 break;
166         default:
167                 AUD_THROW(AUD_ERROR_FFMPEG, format_error);
168         }
169
170         m_specs.rate = (AUD_SampleRate) m_codecCtx->sample_rate;
171 }
172
173 static const char* fileopen_error = "AUD_FFMPEGReader: File couldn't be "
174                                                                         "opened.";
175
176 AUD_FFMPEGReader::AUD_FFMPEGReader(std::string filename) :
177         m_pkgbuf(AVCODEC_MAX_AUDIO_FRAME_SIZE<<1),
178         m_formatCtx(NULL),
179         m_aviocontext(NULL),
180         m_membuf(NULL)
181 {
182         // open file
183         if(avformat_open_input(&m_formatCtx, filename.c_str(), NULL, NULL)!=0)
184                 AUD_THROW(AUD_ERROR_FILE, fileopen_error);
185
186         try
187         {
188                 init();
189         }
190         catch(AUD_Exception&)
191         {
192                 avformat_close_input(&m_formatCtx);
193                 throw;
194         }
195 }
196
197 static const char* streamopen_error = "AUD_FFMPEGReader: Stream couldn't be "
198                                                                           "opened.";
199
200 AUD_FFMPEGReader::AUD_FFMPEGReader(AUD_Reference<AUD_Buffer> buffer) :
201                 m_pkgbuf(AVCODEC_MAX_AUDIO_FRAME_SIZE<<1),
202                 m_membuffer(buffer),
203                 m_membufferpos(0)
204 {
205         m_membuf = reinterpret_cast<data_t*>(av_malloc(FF_MIN_BUFFER_SIZE + FF_INPUT_BUFFER_PADDING_SIZE));
206
207         m_aviocontext = avio_alloc_context(m_membuf, FF_MIN_BUFFER_SIZE, 0, this,
208                                                                            read_packet, NULL, seek_packet);
209
210         if(!m_aviocontext)
211         {
212                 av_free(m_aviocontext);
213                 AUD_THROW(AUD_ERROR_FILE, fileopen_error);
214         }
215
216         m_formatCtx = avformat_alloc_context();
217         m_formatCtx->pb = m_aviocontext;
218         if(avformat_open_input(&m_formatCtx, "", NULL, NULL)!=0)
219         {
220                 av_free(m_aviocontext);
221                 AUD_THROW(AUD_ERROR_FILE, streamopen_error);
222         }
223
224         try
225         {
226                 init();
227         }
228         catch(AUD_Exception&)
229         {
230                 avformat_close_input(&m_formatCtx);
231                 av_free(m_aviocontext);
232                 throw;
233         }
234 }
235
236 AUD_FFMPEGReader::~AUD_FFMPEGReader()
237 {
238         avcodec_close(m_codecCtx);
239
240         if(m_aviocontext)
241         {
242                 avformat_close_input(&m_formatCtx);
243                 av_free(m_aviocontext);
244         }
245         else
246                 av_close_input_file(m_formatCtx);
247 }
248
249 int AUD_FFMPEGReader::read_packet(void* opaque, uint8_t* buf, int buf_size)
250 {
251         AUD_FFMPEGReader* reader = reinterpret_cast<AUD_FFMPEGReader*>(opaque);
252
253         int size = AUD_MIN(buf_size, reader->m_membuffer->getSize() - reader->m_membufferpos);
254
255         if(size < 0)
256                 return -1;
257
258         memcpy(buf, ((data_t*)reader->m_membuffer->getBuffer()) + reader->m_membufferpos, size);
259         reader->m_membufferpos += size;
260
261         return size;
262 }
263
264 int64_t AUD_FFMPEGReader::seek_packet(void* opaque, int64_t offset, int whence)
265 {
266         AUD_FFMPEGReader* reader = reinterpret_cast<AUD_FFMPEGReader*>(opaque);
267
268         switch(whence)
269         {
270         case SEEK_SET:
271                 reader->m_membufferpos = 0;
272                 break;
273         case SEEK_END:
274                 reader->m_membufferpos = reader->m_membuffer->getSize();
275                 break;
276         case AVSEEK_SIZE:
277                 return reader->m_membuffer->getSize();
278         }
279
280         return (reader->m_membufferpos += offset);
281 }
282
283 bool AUD_FFMPEGReader::isSeekable() const
284 {
285         return true;
286 }
287
288 void AUD_FFMPEGReader::seek(int position)
289 {
290         if(position >= 0)
291         {
292                 uint64_t st_time = m_formatCtx->start_time;
293                 uint64_t seek_pos = ((uint64_t)position) * ((uint64_t)AV_TIME_BASE) / ((uint64_t)m_specs.rate);
294
295                 if (seek_pos < 0) {
296                         seek_pos = 0;
297                 }
298
299                 if (st_time != AV_NOPTS_VALUE) {
300                         seek_pos += st_time;
301                 }
302
303                 double pts_time_base = 
304                         av_q2d(m_formatCtx->streams[m_stream]->time_base);
305                 uint64_t pts_st_time =
306                         ((st_time != AV_NOPTS_VALUE) ? st_time : 0)
307                         / pts_time_base / (uint64_t) AV_TIME_BASE;
308
309                 // a value < 0 tells us that seeking failed
310                 if(av_seek_frame(m_formatCtx, -1, seek_pos,
311                                  AVSEEK_FLAG_BACKWARD | AVSEEK_FLAG_ANY) >= 0)
312                 {
313                         avcodec_flush_buffers(m_codecCtx);
314                         m_position = position;
315
316                         AVPacket packet;
317                         bool search = true;
318
319                         while(search && av_read_frame(m_formatCtx, &packet) >= 0)
320                         {
321                                 // is it a frame from the audio stream?
322                                 if(packet.stream_index == m_stream)
323                                 {
324                                         // decode the package
325                                         m_pkgbuf_left = decode(&packet, m_pkgbuf);
326                                         search = false;
327
328                                         // check position
329                                         if(packet.pts != AV_NOPTS_VALUE)
330                                         {
331                                                 // calculate real position, and read to frame!
332                                                 m_position = (packet.pts - 
333                                                         pts_st_time) * pts_time_base * m_specs.rate;
334
335                                                 if(m_position < position)
336                                                 {
337                                                         // read until we're at the right position
338                                                         int length = AUD_DEFAULT_BUFFER_SIZE;
339                                                         AUD_Buffer buffer(length * AUD_SAMPLE_SIZE(m_specs));
340                                                         bool eos;
341                                                         for(int len = position - m_position;
342                                                                 length == AUD_DEFAULT_BUFFER_SIZE;
343                                                                 len -= AUD_DEFAULT_BUFFER_SIZE)
344                                                         {
345                                                                 if(len < AUD_DEFAULT_BUFFER_SIZE)
346                                                                         length = len;
347                                                                 read(length, eos, buffer.getBuffer());
348                                                         }
349                                                 }
350                                         }
351                                 }
352                                 av_free_packet(&packet);
353                         }
354                 }
355                 else
356                 {
357                         fprintf(stderr, "seeking failed!\n");
358                         // Seeking failed, do nothing.
359                 }
360         }
361 }
362
363 int AUD_FFMPEGReader::getLength() const
364 {
365         // return approximated remaning size
366         return (int)((m_formatCtx->duration * m_codecCtx->sample_rate)
367                                  / AV_TIME_BASE)-m_position;
368 }
369
370 int AUD_FFMPEGReader::getPosition() const
371 {
372         return m_position;
373 }
374
375 AUD_Specs AUD_FFMPEGReader::getSpecs() const
376 {
377         return m_specs.specs;
378 }
379
380 void AUD_FFMPEGReader::read(int& length, bool& eos, sample_t* buffer)
381 {
382         // read packages and decode them
383         AVPacket packet;
384         int data_size = 0;
385         int pkgbuf_pos;
386         int left = length;
387         int sample_size = AUD_DEVICE_SAMPLE_SIZE(m_specs);
388
389         sample_t* buf = buffer;
390         pkgbuf_pos = m_pkgbuf_left;
391         m_pkgbuf_left = 0;
392
393         // there may still be data in the buffer from the last call
394         if(pkgbuf_pos > 0)
395         {
396                 data_size = AUD_MIN(pkgbuf_pos, left * sample_size);
397                 m_convert((data_t*) buf, (data_t*) m_pkgbuf.getBuffer(),
398                           data_size / AUD_FORMAT_SIZE(m_specs.format));
399                 buf += data_size / AUD_FORMAT_SIZE(m_specs.format);
400                 left -= data_size/sample_size;
401         }
402
403         // for each frame read as long as there isn't enough data already
404         while((left > 0) && (av_read_frame(m_formatCtx, &packet) >= 0))
405         {
406                 // is it a frame from the audio stream?
407                 if(packet.stream_index == m_stream)
408                 {
409                         // decode the package
410                         pkgbuf_pos = decode(&packet, m_pkgbuf);
411
412                         // copy to output buffer
413                         data_size = AUD_MIN(pkgbuf_pos, left * sample_size);
414                         m_convert((data_t*) buf, (data_t*) m_pkgbuf.getBuffer(),
415                                           data_size / AUD_FORMAT_SIZE(m_specs.format));
416                         buf += data_size / AUD_FORMAT_SIZE(m_specs.format);
417                         left -= data_size/sample_size;
418                 }
419                 av_free_packet(&packet);
420         }
421         // read more data than necessary?
422         if(pkgbuf_pos > data_size)
423         {
424                 m_pkgbuf_left = pkgbuf_pos-data_size;
425                 memmove(m_pkgbuf.getBuffer(),
426                                 ((data_t*)m_pkgbuf.getBuffer())+data_size,
427                                 pkgbuf_pos-data_size);
428         }
429
430         if((eos = (left > 0)))
431                 length -= left;
432
433         m_position += length;
434 }