tmp
[blender.git] / extern / audaspace / plugins / ffmpeg / FFMPEGReader.cpp
1 /*******************************************************************************
2  * Copyright 2009-2016 Jörg Müller
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  ******************************************************************************/
16
17 #include "FFMPEGReader.h"
18 #include "Exception.h"
19
20 #include <algorithm>
21
22 extern "C" {
23 #include <libavcodec/avcodec.h>
24 #include <libavformat/avio.h>
25 }
26
27 AUD_NAMESPACE_BEGIN
28
29 int FFMPEGReader::decode(AVPacket& packet, Buffer& buffer)
30 {
31         AVFrame* frame = nullptr;
32         int got_frame;
33         int read_length;
34         uint8_t* orig_data = packet.data;
35         int orig_size = packet.size;
36
37         int buf_size = buffer.getSize();
38         int buf_pos = 0;
39
40         while(packet.size > 0)
41         {
42                 got_frame = 0;
43
44                 if(!frame)
45                         frame = av_frame_alloc();
46                 else
47                         av_frame_unref(frame);
48
49                 read_length = avcodec_decode_audio4(m_codecCtx, frame, &got_frame, &packet);
50                 if(read_length < 0)
51                         break;
52
53                 if(got_frame)
54                 {
55                         int data_size = av_samples_get_buffer_size(nullptr, m_codecCtx->channels, frame->nb_samples, m_codecCtx->sample_fmt, 1);
56
57                         if(buf_size - buf_pos < data_size)
58                         {
59                                 buffer.resize(buf_size + data_size, true);
60                                 buf_size += data_size;
61                         }
62
63                         if(m_tointerleave)
64                         {
65                                 int single_size = data_size / m_codecCtx->channels / frame->nb_samples;
66                                 for(int channel = 0; channel < m_codecCtx->channels; channel++)
67                                 {
68                                         for(int i = 0; i < frame->nb_samples; i++)
69                                         {
70                                                 std::memcpy(((data_t*)buffer.getBuffer()) + buf_pos + ((m_codecCtx->channels * i) + channel) * single_size,
71                                                            frame->data[channel] + i * single_size, single_size);
72                                         }
73                                 }
74                         }
75                         else
76                                 std::memcpy(((data_t*)buffer.getBuffer()) + buf_pos, frame->data[0], data_size);
77
78                         buf_pos += data_size;
79                 }
80                 packet.size -= read_length;
81                 packet.data += read_length;
82         }
83
84         packet.data = orig_data;
85         packet.size = orig_size;
86         av_free(frame);
87
88         return buf_pos;
89 }
90
91 void FFMPEGReader::init()
92 {
93         m_position = 0;
94         m_pkgbuf_left = 0;
95
96         if(avformat_find_stream_info(m_formatCtx, nullptr) < 0)
97                 AUD_THROW(FileException, "File couldn't be read, ffmpeg couldn't find the stream info.");
98
99         // find audio stream and codec
100         m_stream = -1;
101
102         for(unsigned int i = 0; i < m_formatCtx->nb_streams; i++)
103         {
104                 if((m_formatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
105                         && (m_stream < 0))
106                 {
107                         m_stream=i;
108                         break;
109                 }
110         }
111
112         if(m_stream == -1)
113                 AUD_THROW(FileException, "File couldn't be read, no audio stream found by ffmpeg.");
114
115         m_codecCtx = m_formatCtx->streams[m_stream]->codec;
116
117         // get a decoder and open it
118         AVCodec* aCodec = avcodec_find_decoder(m_codecCtx->codec_id);
119         if(!aCodec)
120                 AUD_THROW(FileException, "File couldn't be read, no decoder found with ffmpeg.");
121
122         if(avcodec_open2(m_codecCtx, aCodec, nullptr) < 0)
123                 AUD_THROW(FileException, "File couldn't be read, ffmpeg codec couldn't be opened.");
124
125         m_specs.channels = (Channels) m_codecCtx->channels;
126         m_tointerleave = av_sample_fmt_is_planar(m_codecCtx->sample_fmt);
127
128         switch(av_get_packed_sample_fmt(m_codecCtx->sample_fmt))
129         {
130         case AV_SAMPLE_FMT_U8:
131                 m_convert = convert_u8_float;
132                 m_specs.format = FORMAT_U8;
133                 break;
134         case AV_SAMPLE_FMT_S16:
135                 m_convert = convert_s16_float;
136                 m_specs.format = FORMAT_S16;
137                 break;
138         case AV_SAMPLE_FMT_S32:
139                 m_convert = convert_s32_float;
140                 m_specs.format = FORMAT_S32;
141                 break;
142         case AV_SAMPLE_FMT_FLT:
143                 m_convert = convert_copy<float>;
144                 m_specs.format = FORMAT_FLOAT32;
145                 break;
146         case AV_SAMPLE_FMT_DBL:
147                 m_convert = convert_double_float;
148                 m_specs.format = FORMAT_FLOAT64;
149                 break;
150         default:
151                 AUD_THROW(FileException, "File couldn't be read, ffmpeg sample format unknown.");
152         }
153
154         m_specs.rate = (SampleRate) m_codecCtx->sample_rate;
155 }
156
157 FFMPEGReader::FFMPEGReader(std::string filename) :
158         m_pkgbuf(),
159         m_formatCtx(nullptr),
160         m_aviocontext(nullptr),
161         m_membuf(nullptr)
162 {
163         // open file
164         if(avformat_open_input(&m_formatCtx, filename.c_str(), nullptr, nullptr)!=0)
165                 AUD_THROW(FileException, "File couldn't be opened with ffmpeg.");
166
167         try
168         {
169                 init();
170         }
171         catch(Exception&)
172         {
173                 avformat_close_input(&m_formatCtx);
174                 throw;
175         }
176 }
177
178 FFMPEGReader::FFMPEGReader(std::shared_ptr<Buffer> buffer) :
179                 m_pkgbuf(),
180                 m_membuffer(buffer),
181                 m_membufferpos(0)
182 {
183         m_membuf = reinterpret_cast<data_t*>(av_malloc(FF_MIN_BUFFER_SIZE + FF_INPUT_BUFFER_PADDING_SIZE));
184
185         m_aviocontext = avio_alloc_context(m_membuf, FF_MIN_BUFFER_SIZE, 0, this, read_packet, nullptr, seek_packet);
186
187         if(!m_aviocontext)
188         {
189                 av_free(m_aviocontext);
190                 AUD_THROW(FileException, "Buffer reading context couldn't be created with ffmpeg.");
191         }
192
193         m_formatCtx = avformat_alloc_context();
194         m_formatCtx->pb = m_aviocontext;
195         if(avformat_open_input(&m_formatCtx, "", nullptr, nullptr)!=0)
196         {
197                 av_free(m_aviocontext);
198                 AUD_THROW(FileException, "Buffer couldn't be read with ffmpeg.");
199         }
200
201         try
202         {
203                 init();
204         }
205         catch(Exception&)
206         {
207                 avformat_close_input(&m_formatCtx);
208                 av_free(m_aviocontext);
209                 throw;
210         }
211 }
212
213 FFMPEGReader::~FFMPEGReader()
214 {
215         avcodec_close(m_codecCtx);
216         avformat_close_input(&m_formatCtx);
217 }
218
219 int FFMPEGReader::read_packet(void* opaque, uint8_t* buf, int buf_size)
220 {
221         FFMPEGReader* reader = reinterpret_cast<FFMPEGReader*>(opaque);
222
223         int size = std::min(buf_size, int(reader->m_membuffer->getSize() - reader->m_membufferpos));
224
225         if(size < 0)
226                 return -1;
227
228         std::memcpy(buf, ((data_t*)reader->m_membuffer->getBuffer()) + reader->m_membufferpos, size);
229         reader->m_membufferpos += size;
230
231         return size;
232 }
233
234 int64_t FFMPEGReader::seek_packet(void* opaque, int64_t offset, int whence)
235 {
236         FFMPEGReader* reader = reinterpret_cast<FFMPEGReader*>(opaque);
237
238         switch(whence)
239         {
240         case SEEK_SET:
241                 reader->m_membufferpos = 0;
242                 break;
243         case SEEK_END:
244                 reader->m_membufferpos = reader->m_membuffer->getSize();
245                 break;
246         case AVSEEK_SIZE:
247                 return reader->m_membuffer->getSize();
248         }
249
250         return (reader->m_membufferpos += offset);
251 }
252
253 bool FFMPEGReader::isSeekable() const
254 {
255         return true;
256 }
257
258 void FFMPEGReader::seek(int position)
259 {
260         if(position >= 0)
261         {
262                 uint64_t st_time = m_formatCtx->start_time;
263                 uint64_t seek_pos = ((uint64_t)position) * ((uint64_t)AV_TIME_BASE) / ((uint64_t)m_specs.rate);
264
265                 if(st_time != AV_NOPTS_VALUE) {
266                         seek_pos += st_time;
267                 }
268
269                 double pts_time_base = 
270                         av_q2d(m_formatCtx->streams[m_stream]->time_base);
271                 uint64_t pts_st_time =
272                         ((st_time != AV_NOPTS_VALUE) ? st_time : 0)
273                         / pts_time_base / (uint64_t) AV_TIME_BASE;
274
275                 // a value < 0 tells us that seeking failed
276                 if(av_seek_frame(m_formatCtx, -1, seek_pos,
277                                  AVSEEK_FLAG_BACKWARD | AVSEEK_FLAG_ANY) >= 0)
278                 {
279                         avcodec_flush_buffers(m_codecCtx);
280                         m_position = position;
281
282                         AVPacket packet;
283                         bool search = true;
284
285                         while(search && av_read_frame(m_formatCtx, &packet) >= 0)
286                         {
287                                 // is it a frame from the audio stream?
288                                 if(packet.stream_index == m_stream)
289                                 {
290                                         // decode the package
291                                         m_pkgbuf_left = decode(packet, m_pkgbuf);
292                                         search = false;
293
294                                         // check position
295                                         if(packet.pts != AV_NOPTS_VALUE)
296                                         {
297                                                 // calculate real position, and read to frame!
298                                                 m_position = (packet.pts - pts_st_time) * pts_time_base * m_specs.rate;
299
300                                                 if(m_position < position)
301                                                 {
302                                                         // read until we're at the right position
303                                                         int length = AUD_DEFAULT_BUFFER_SIZE;
304                                                         Buffer buffer(length * AUD_SAMPLE_SIZE(m_specs));
305                                                         bool eos;
306                                                         for(int len = position - m_position; len > 0; len -= AUD_DEFAULT_BUFFER_SIZE)
307                                                         {
308                                                                 if(len < AUD_DEFAULT_BUFFER_SIZE)
309                                                                         length = len;
310                                                                 read(length, eos, buffer.getBuffer());
311                                                         }
312                                                 }
313                                         }
314                                 }
315                                 av_free_packet(&packet);
316                         }
317                 }
318                 else
319                 {
320                         fprintf(stderr, "seeking failed!\n");
321                         // Seeking failed, do nothing.
322                 }
323         }
324 }
325
326 int FFMPEGReader::getLength() const
327 {
328         // return approximated remaning size
329         return (int)((m_formatCtx->duration * m_codecCtx->sample_rate)
330                                  / AV_TIME_BASE)-m_position;
331 }
332
333 int FFMPEGReader::getPosition() const
334 {
335         return m_position;
336 }
337
338 Specs FFMPEGReader::getSpecs() const
339 {
340         return m_specs.specs;
341 }
342
343 void FFMPEGReader::read(int& length, bool& eos, sample_t* buffer)
344 {
345         // read packages and decode them
346         AVPacket packet;
347         int data_size = 0;
348         int pkgbuf_pos;
349         int left = length;
350         int sample_size = AUD_DEVICE_SAMPLE_SIZE(m_specs);
351
352         sample_t* buf = buffer;
353         pkgbuf_pos = m_pkgbuf_left;
354         m_pkgbuf_left = 0;
355
356         // there may still be data in the buffer from the last call
357         if(pkgbuf_pos > 0)
358         {
359                 data_size = std::min(pkgbuf_pos, left * sample_size);
360                 m_convert((data_t*) buf, (data_t*) m_pkgbuf.getBuffer(), data_size / AUD_FORMAT_SIZE(m_specs.format));
361                 buf += data_size / AUD_FORMAT_SIZE(m_specs.format);
362                 left -= data_size/sample_size;
363         }
364
365         // for each frame read as long as there isn't enough data already
366         while((left > 0) && (av_read_frame(m_formatCtx, &packet) >= 0))
367         {
368                 // is it a frame from the audio stream?
369                 if(packet.stream_index == m_stream)
370                 {
371                         // decode the package
372                         pkgbuf_pos = decode(packet, m_pkgbuf);
373
374                         // copy to output buffer
375                         data_size = std::min(pkgbuf_pos, left * sample_size);
376                         m_convert((data_t*) buf, (data_t*) m_pkgbuf.getBuffer(), data_size / AUD_FORMAT_SIZE(m_specs.format));
377                         buf += data_size / AUD_FORMAT_SIZE(m_specs.format);
378                         left -= data_size/sample_size;
379                 }
380                 av_free_packet(&packet);
381         }
382         // read more data than necessary?
383         if(pkgbuf_pos > data_size)
384         {
385                 m_pkgbuf_left = pkgbuf_pos-data_size;
386                 memmove(m_pkgbuf.getBuffer(),
387                                 ((data_t*)m_pkgbuf.getBuffer())+data_size,
388                                 pkgbuf_pos-data_size);
389         }
390
391         if((eos = (left > 0)))
392                 length -= left;
393
394         m_position += length;
395 }
396
397 AUD_NAMESPACE_END