1 #ifdef MAKE_FFMPEG_PLAYER
2
3 #include "ffmpegaudio.hh"
4
5 #include <math.h>
6 #include <errno.h>
7
8 #ifndef INT64_C
9 #define INT64_C(c) (c ## LL)
10 #endif
11
12 #ifndef UINT64_C
13 #define UINT64_C(c) (c ## ULL)
14 #endif
15
16 #include <ao/ao.h>
17
18 extern "C" {
19 #include <libavcodec/avcodec.h>
20 #include <libavformat/avformat.h>
21 #include <libavutil/avutil.h>
22 #include "libswresample/swresample.h"
23 }
24
25 #include <QString>
26 #include <QDataStream>
27 #include <QDebug>
28
29 #include <vector>
30
31 #include "qt4x5.hh"
32
33 using std::vector;
34
35 namespace Ffmpeg
36 {
37
38 QMutex DecoderThread::deviceMutex_;
39
avErrorString(int errnum)40 static inline QString avErrorString( int errnum )
41 {
42 char buf[64];
43 av_strerror( errnum, buf, 64 );
44 return QString::fromLatin1( buf );
45 }
46
instance()47 AudioService & AudioService::instance()
48 {
49 static AudioService a;
50 return a;
51 }
52
AudioService()53 AudioService::AudioService()
54 {
55 #if LIBAVFORMAT_VERSION_MAJOR < 58 || ( LIBAVFORMAT_VERSION_MAJOR == 58 && LIBAVFORMAT_VERSION_MINOR < 9 )
56 av_register_all();
57 #endif
58 ao_initialize();
59 }
60
~AudioService()61 AudioService::~AudioService()
62 {
63 emit cancelPlaying( true );
64 ao_shutdown();
65 }
66
playMemory(const char * ptr,int size)67 void AudioService::playMemory( const char * ptr, int size )
68 {
69 emit cancelPlaying( false );
70 QByteArray audioData( ptr, size );
71 DecoderThread * thread = new DecoderThread( audioData, this );
72
73 connect( thread, SIGNAL( error( QString ) ), this, SIGNAL( error( QString ) ) );
74 connect( this, SIGNAL( cancelPlaying( bool ) ), thread, SLOT( cancel( bool ) ), Qt::DirectConnection );
75 connect( thread, SIGNAL( finished() ), thread, SLOT( deleteLater() ) );
76
77 thread->start();
78 }
79
stop()80 void AudioService::stop()
81 {
82 emit cancelPlaying( false );
83 }
84
85 struct DecoderContext
86 {
87 enum
88 {
89 kBufferSize = 32768
90 };
91
92 static QMutex deviceMutex_;
93 QAtomicInt & isCancelled_;
94 QByteArray audioData_;
95 QDataStream audioDataStream_;
96 AVFormatContext * formatContext_;
97 AVCodec * codec_;
98 AVCodecContext * codecContext_;
99 AVIOContext * avioContext_;
100 AVStream * audioStream_;
101 ao_device * aoDevice_;
102 bool avformatOpened_;
103
104 SwrContext *swr_;
105
106 DecoderContext( QByteArray const & audioData, QAtomicInt & isCancelled );
107 ~DecoderContext();
108
109 bool openCodec( QString & errorString );
110 void closeCodec();
111 bool openOutputDevice( QString & errorString );
112 void closeOutputDevice();
113 bool play( QString & errorString );
114 bool normalizeAudio( AVFrame * frame, vector<char> & samples );
115 void playFrame( AVFrame * frame );
116 };
117
DecoderContext(QByteArray const & audioData,QAtomicInt & isCancelled)118 DecoderContext::DecoderContext( QByteArray const & audioData, QAtomicInt & isCancelled ):
119 isCancelled_( isCancelled ),
120 audioData_( audioData ),
121 audioDataStream_( audioData_ ),
122 formatContext_( NULL ),
123 codec_( NULL ),
124 codecContext_( NULL ),
125 avioContext_( NULL ),
126 audioStream_( NULL ),
127 aoDevice_( NULL ),
128 avformatOpened_( false ),
129 swr_( NULL )
130 {
131 }
132
~DecoderContext()133 DecoderContext::~DecoderContext()
134 {
135 closeOutputDevice();
136 closeCodec();
137 }
138
readAudioData(void * opaque,unsigned char * buffer,int bufferSize)139 static int readAudioData( void * opaque, unsigned char * buffer, int bufferSize )
140 {
141 QDataStream * pStream = ( QDataStream * )opaque;
142 return pStream->readRawData( ( char * )buffer, bufferSize );
143 }
144
openCodec(QString & errorString)145 bool DecoderContext::openCodec( QString & errorString )
146 {
147 formatContext_ = avformat_alloc_context();
148 if ( !formatContext_ )
149 {
150 errorString = QObject::tr( "avformat_alloc_context() failed." );
151 return false;
152 }
153
154 #if LIBAVCODEC_VERSION_MAJOR < 56 || ( LIBAVCODEC_VERSION_MAJOR == 56 && LIBAVCODEC_VERSION_MINOR < 56 )
155 unsigned char * avioBuffer = ( unsigned char * )av_malloc( kBufferSize + FF_INPUT_BUFFER_PADDING_SIZE );
156 #else
157 unsigned char * avioBuffer = ( unsigned char * )av_malloc( kBufferSize + AV_INPUT_BUFFER_PADDING_SIZE );
158 #endif
159 if ( !avioBuffer )
160 {
161 errorString = QObject::tr( "av_malloc() failed." );
162 return false;
163 }
164
165 // Don't free buffer allocated here (if succeeded), it will be cleaned up automatically.
166 avioContext_ = avio_alloc_context( avioBuffer, kBufferSize, 0, &audioDataStream_, readAudioData, NULL, NULL );
167 if ( !avioContext_ )
168 {
169 av_free( avioBuffer );
170 errorString = QObject::tr( "avio_alloc_context() failed." );
171 return false;
172 }
173
174 avioContext_->seekable = 0;
175 avioContext_->write_flag = 0;
176
177 // If pb not set, avformat_open_input() simply crash.
178 formatContext_->pb = avioContext_;
179 formatContext_->flags |= AVFMT_FLAG_CUSTOM_IO;
180
181 int ret = 0;
182 avformatOpened_ = true;
183
184 ret = avformat_open_input( &formatContext_, "_STREAM_", NULL, NULL );
185 if ( ret < 0 )
186 {
187 errorString = QObject::tr( "avformat_open_input() failed: %1." ).arg( avErrorString( ret ) );
188 return false;
189 }
190
191 ret = avformat_find_stream_info( formatContext_, NULL );
192 if ( ret < 0 )
193 {
194 errorString = QObject::tr( "avformat_find_stream_info() failed: %1." ).arg( avErrorString( ret ) );
195 return false;
196 }
197
198 // Find audio stream, use the first audio stream if available
199 for ( unsigned i = 0; i < formatContext_->nb_streams; i++ )
200 {
201 #if LIBAVCODEC_VERSION_MAJOR < 57 || ( LIBAVCODEC_VERSION_MAJOR == 57 && LIBAVCODEC_VERSION_MINOR < 33 )
202 if ( formatContext_->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO )
203 #else
204 if ( formatContext_->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO )
205 #endif
206 {
207 audioStream_ = formatContext_->streams[i];
208 break;
209 }
210 }
211 if ( !audioStream_ )
212 {
213 errorString = QObject::tr( "Could not find audio stream." );
214 return false;
215 }
216
217 #if LIBAVCODEC_VERSION_MAJOR < 57 || ( LIBAVCODEC_VERSION_MAJOR == 57 && LIBAVCODEC_VERSION_MINOR < 33 )
218 codecContext_ = audioStream_->codec;
219 codec_ = avcodec_find_decoder( codecContext_->codec_id );
220 if ( !codec_ )
221 {
222 errorString = QObject::tr( "Codec [id: %1] not found." ).arg( codecContext_->codec_id );
223 return false;
224 }
225 #else
226 codec_ = avcodec_find_decoder( audioStream_->codecpar->codec_id );
227 if ( !codec_ )
228 {
229 errorString = QObject::tr( "Codec [id: %1] not found." ).arg( audioStream_->codecpar->codec_id );
230 return false;
231 }
232 codecContext_ = avcodec_alloc_context3( codec_ );
233 if ( !codecContext_ )
234 {
235 errorString = QObject::tr( "avcodec_alloc_context3() failed." );
236 return false;
237 }
238 avcodec_parameters_to_context( codecContext_, audioStream_->codecpar );
239 #endif
240
241 ret = avcodec_open2( codecContext_, codec_, NULL );
242 if ( ret < 0 )
243 {
244 errorString = QObject::tr( "avcodec_open2() failed: %1." ).arg( avErrorString( ret ) );
245 return false;
246 }
247
248 av_log( NULL, AV_LOG_INFO, "Codec open: %s: channels: %d, rate: %d, format: %s\n", codec_->long_name,
249 codecContext_->channels, codecContext_->sample_rate, av_get_sample_fmt_name( codecContext_->sample_fmt ) );
250
251 if ( codecContext_->sample_fmt == AV_SAMPLE_FMT_S32 ||
252 codecContext_->sample_fmt == AV_SAMPLE_FMT_S32P ||
253 codecContext_->sample_fmt == AV_SAMPLE_FMT_FLT ||
254 codecContext_->sample_fmt == AV_SAMPLE_FMT_FLTP ||
255 codecContext_->sample_fmt == AV_SAMPLE_FMT_DBL ||
256 codecContext_->sample_fmt == AV_SAMPLE_FMT_DBLP )
257 {
258 swr_ = swr_alloc_set_opts( NULL,
259 codecContext_->channel_layout,
260 AV_SAMPLE_FMT_S16,
261 codecContext_->sample_rate,
262 codecContext_->channel_layout,
263 codecContext_->sample_fmt,
264 codecContext_->sample_rate,
265 0,
266 NULL );
267 swr_init( swr_ );
268 }
269
270 return true;
271 }
272
closeCodec()273 void DecoderContext::closeCodec()
274 {
275 if ( swr_ )
276 {
277 swr_free( &swr_ );
278 }
279
280 if ( !formatContext_ )
281 {
282 if ( avioContext_ )
283 {
284 av_free( avioContext_->buffer );
285 avioContext_ = NULL;
286 }
287 return;
288 }
289
290 // avformat_open_input() is not called, just free the buffer associated with
291 // the AVIOContext, and the AVFormatContext
292 if ( !avformatOpened_ )
293 {
294 if ( formatContext_ )
295 {
296 avformat_free_context( formatContext_ );
297 formatContext_ = NULL;
298 }
299
300 if ( avioContext_ )
301 {
302 av_free( avioContext_->buffer );
303 avioContext_ = NULL;
304 }
305 return;
306 }
307
308 avformatOpened_ = false;
309
310 // Closing a codec context without prior avcodec_open2() will result in
311 // a crash in ffmpeg
312 if ( audioStream_ && codecContext_ && codec_ )
313 {
314 audioStream_->discard = AVDISCARD_ALL;
315 avcodec_close( codecContext_ );
316 #if LIBAVCODEC_VERSION_MAJOR > 57 || ( LIBAVCODEC_VERSION_MAJOR == 57 && LIBAVCODEC_VERSION_MINOR >= 33 )
317 avcodec_free_context( &codecContext_ );
318 #endif
319 }
320
321 avformat_close_input( &formatContext_ );
322 av_free( avioContext_->buffer );
323 }
324
openOutputDevice(QString & errorString)325 bool DecoderContext::openOutputDevice( QString & errorString )
326 {
327 // Prepare for audio output
328 int aoDriverId = ao_default_driver_id();
329 ao_info * aoDrvInfo = ao_driver_info( aoDriverId );
330
331 if ( aoDriverId < 0 || !aoDrvInfo )
332 {
333 errorString = QObject::tr( "Cannot find usable audio output device." );
334 return false;
335 }
336
337 ao_sample_format aoSampleFormat;
338 memset (&aoSampleFormat, 0, sizeof(aoSampleFormat) );
339 aoSampleFormat.channels = codecContext_->channels;
340 aoSampleFormat.rate = codecContext_->sample_rate;
341 aoSampleFormat.byte_format = AO_FMT_NATIVE;
342 aoSampleFormat.matrix = 0;
343 aoSampleFormat.bits = qMin( 16, av_get_bytes_per_sample( codecContext_->sample_fmt ) << 3 );
344
345 if ( aoSampleFormat.bits == 0 )
346 {
347 errorString = QObject::tr( "Unsupported sample format." );
348 return false;
349 }
350
351 av_log( NULL, AV_LOG_INFO, "ao_open_live(): %s: channels: %d, rate: %d, bits: %d\n",
352 aoDrvInfo->name, aoSampleFormat.channels, aoSampleFormat.rate, aoSampleFormat.bits );
353
354 aoDevice_ = ao_open_live( aoDriverId, &aoSampleFormat, NULL );
355 if ( !aoDevice_ )
356 {
357 errorString = QObject::tr( "ao_open_live() failed: " );
358
359 switch ( errno )
360 {
361 case AO_ENODRIVER:
362 errorString += QObject::tr( "No driver." );
363 break;
364 case AO_ENOTLIVE:
365 errorString += QObject::tr( "This driver is not a live output device." );
366 break;
367 case AO_EBADOPTION:
368 errorString += QObject::tr( "A valid option key has an invalid value." );
369 break;
370 case AO_EOPENDEVICE:
371 errorString += QObject::tr( "Cannot open the device: %1, channels: %2, rate: %3, bits: %4." )
372 .arg( aoDrvInfo->short_name )
373 .arg( aoSampleFormat.channels )
374 .arg( aoSampleFormat.rate )
375 .arg( aoSampleFormat.bits );
376 break;
377 default:
378 errorString += QObject::tr( "Unknown error." );
379 break;
380 }
381
382 return false;
383 }
384
385 return true;
386 }
387
closeOutputDevice()388 void DecoderContext::closeOutputDevice()
389 {
390 // ao_close() is synchronous, it will wait until all audio streams flushed
391 if ( aoDevice_ )
392 {
393 ao_close( aoDevice_ );
394 aoDevice_ = NULL;
395 }
396 }
397
play(QString & errorString)398 bool DecoderContext::play( QString & errorString )
399 {
400 #if LIBAVCODEC_VERSION_MAJOR < 55 || ( LIBAVCODEC_VERSION_MAJOR == 55 && LIBAVCODEC_VERSION_MINOR < 28 )
401 AVFrame * frame = avcodec_alloc_frame();
402 #else
403 AVFrame * frame = av_frame_alloc();
404 #endif
405 if ( !frame )
406 {
407 errorString = QObject::tr( "avcodec_alloc_frame() failed." );
408 return false;
409 }
410
411 AVPacket packet;
412 av_init_packet( &packet );
413
414 while ( !Qt4x5::AtomicInt::loadAcquire( isCancelled_ ) &&
415 av_read_frame( formatContext_, &packet ) >= 0 )
416 {
417 if ( packet.stream_index == audioStream_->index )
418 {
419 AVPacket pack = packet;
420 #if LIBAVCODEC_VERSION_MAJOR < 57 || ( LIBAVCODEC_VERSION_MAJOR == 57 && LIBAVCODEC_VERSION_MINOR < 37 )
421 int gotFrame = 0;
422 do
423 {
424 int len = avcodec_decode_audio4( codecContext_, frame, &gotFrame, &pack );
425 if ( !Qt4x5::AtomicInt::loadAcquire( isCancelled_ ) && gotFrame )
426 {
427 playFrame( frame );
428 }
429 if( len <= 0 || Qt4x5::AtomicInt::loadAcquire( isCancelled_ ) )
430 break;
431 pack.size -= len;
432 pack.data += len;
433 }
434 while( pack.size > 0 );
435 #else
436 int ret = avcodec_send_packet( codecContext_, &pack );
437 /* read all the output frames (in general there may be any number of them) */
438 while( ret >= 0 )
439 {
440 ret = avcodec_receive_frame( codecContext_, frame);
441
442 if ( Qt4x5::AtomicInt::loadAcquire( isCancelled_ ) || ret < 0 )
443 break;
444
445 playFrame( frame );
446 }
447 #endif
448 }
449 // av_free_packet() must be called after each call to av_read_frame()
450 #if LIBAVCODEC_VERSION_MAJOR < 57 || ( LIBAVCODEC_VERSION_MAJOR == 57 && LIBAVCODEC_VERSION_MINOR < 7 )
451 av_free_packet( &packet );
452 #else
453 av_packet_unref( &packet );
454 #endif
455 }
456
457 #if LIBAVCODEC_VERSION_MAJOR < 57 || ( LIBAVCODEC_VERSION_MAJOR == 57 && LIBAVCODEC_VERSION_MINOR < 37 )
458 if ( !Qt4x5::AtomicInt::loadAcquire( isCancelled_ ) &&
459 codecContext_->codec->capabilities & CODEC_CAP_DELAY )
460 {
461 av_init_packet( &packet );
462 int gotFrame = 0;
463 while ( avcodec_decode_audio4( codecContext_, frame, &gotFrame, &packet ) >= 0 && gotFrame )
464 {
465 if ( Qt4x5::AtomicInt::loadAcquire( isCancelled_ ) )
466 break;
467 playFrame( frame );
468 }
469 }
470 #else
471 /* flush the decoder */
472 av_init_packet( &packet );
473 packet.data = NULL;
474 packet.size = 0;
475 int ret = avcodec_send_packet(codecContext_, &packet );
476 while( ret >= 0 )
477 {
478 ret = avcodec_receive_frame(codecContext_, frame);
479 if ( Qt4x5::AtomicInt::loadAcquire( isCancelled_ ) || ret < 0 )
480 break;
481 playFrame( frame );
482 }
483 #endif
484
485 #if LIBAVCODEC_VERSION_MAJOR < 54
486 av_free( frame );
487 #elif LIBAVCODEC_VERSION_MAJOR < 55 || ( LIBAVCODEC_VERSION_MAJOR == 55 && LIBAVCODEC_VERSION_MINOR < 28 )
488 avcodec_free_frame( &frame );
489 #else
490 av_frame_free( &frame );
491 #endif
492
493 return true;
494 }
495
toInt32(double v)496 static inline int32_t toInt32( double v )
497 {
498 if ( v >= 1.0 )
499 return 0x7fffffffL;
500 else if ( v <= -1.0 )
501 return 0x80000000L;
502 return floor( v * 2147483648.0 );
503 }
504
normalizeAudio(AVFrame * frame,vector<char> & samples)505 bool DecoderContext::normalizeAudio( AVFrame * frame, vector<char> & samples )
506 {
507 int lineSize = 0;
508 int dataSize = av_samples_get_buffer_size( &lineSize, codecContext_->channels,
509 frame->nb_samples, codecContext_->sample_fmt, 1 );
510
511 // Portions from: https://code.google.com/p/lavfilters/source/browse/decoder/LAVAudio/LAVAudio.cpp
512 // But this one use 8, 16, 32 bits integer, respectively.
513 switch ( codecContext_->sample_fmt )
514 {
515 case AV_SAMPLE_FMT_U8:
516 case AV_SAMPLE_FMT_S16:
517 {
518 samples.resize( dataSize );
519 memcpy( &samples.front(), frame->data[0], lineSize );
520 }
521 break;
522 // Planar
523 case AV_SAMPLE_FMT_U8P:
524 {
525 samples.resize( dataSize );
526
527 uint8_t * out = ( uint8_t * )&samples.front();
528 for ( int i = 0; i < frame->nb_samples; i++ )
529 {
530 for ( int ch = 0; ch < codecContext_->channels; ch++ )
531 {
532 *out++ = ( ( uint8_t * )frame->extended_data[ch] )[i];
533 }
534 }
535 }
536 break;
537 case AV_SAMPLE_FMT_S16P:
538 {
539 samples.resize( dataSize );
540
541 int16_t * out = ( int16_t * )&samples.front();
542 for ( int i = 0; i < frame->nb_samples; i++ )
543 {
544 for ( int ch = 0; ch < codecContext_->channels; ch++ )
545 {
546 *out++ = ( ( int16_t * )frame->extended_data[ch] )[i];
547 }
548 }
549 }
550 break;
551 case AV_SAMPLE_FMT_S32:
552 /* Pass through */
553 case AV_SAMPLE_FMT_S32P:
554 /* Pass through */
555 case AV_SAMPLE_FMT_FLT:
556 /* Pass through */
557 case AV_SAMPLE_FMT_FLTP:
558 /* Pass through */
559 {
560 samples.resize( dataSize / 2 );
561
562 uint8_t *out = ( uint8_t * )&samples.front();
563 swr_convert( swr_, &out, frame->nb_samples, (const uint8_t**)frame->extended_data, frame->nb_samples );
564 }
565 break;
566 case AV_SAMPLE_FMT_DBL:
567 case AV_SAMPLE_FMT_DBLP:
568 {
569 samples.resize( dataSize / 4 );
570
571 uint8_t *out = ( uint8_t * )&samples.front();
572 swr_convert( swr_, &out, frame->nb_samples, (const uint8_t**)frame->extended_data, frame->nb_samples );
573 }
574 break;
575 default:
576 return false;
577 }
578
579 return true;
580 }
581
playFrame(AVFrame * frame)582 void DecoderContext::playFrame( AVFrame * frame )
583 {
584 if ( !frame )
585 return;
586
587 vector<char> samples;
588 if ( normalizeAudio( frame, samples ) )
589 ao_play( aoDevice_, &samples.front(), samples.size() );
590 }
591
DecoderThread(QByteArray const & audioData,QObject * parent)592 DecoderThread::DecoderThread( QByteArray const & audioData, QObject * parent ) :
593 QThread( parent ),
594 isCancelled_( 0 ),
595 audioData_( audioData )
596 {
597 }
598
~DecoderThread()599 DecoderThread::~DecoderThread()
600 {
601 isCancelled_.ref();
602 }
603
run()604 void DecoderThread::run()
605 {
606 QString errorString;
607 DecoderContext d( audioData_, isCancelled_ );
608
609 if ( !d.openCodec( errorString ) )
610 {
611 emit error( errorString );
612 return;
613 }
614
615 while ( !deviceMutex_.tryLock( 100 ) )
616 {
617 if ( Qt4x5::AtomicInt::loadAcquire( isCancelled_ ) )
618 return;
619 }
620
621 if ( !d.openOutputDevice( errorString ) )
622 emit error( errorString );
623 else if ( !d.play( errorString ) )
624 emit error( errorString );
625
626 d.closeOutputDevice();
627 deviceMutex_.unlock();
628 }
629
cancel(bool waitUntilFinished)630 void DecoderThread::cancel( bool waitUntilFinished )
631 {
632 isCancelled_.ref();
633 if ( waitUntilFinished )
634 this->wait();
635 }
636
637 }
638
639 #endif // MAKE_FFMPEG_PLAYER
640