2 * An example showing how to play a stream sync'd to video, using ffmpeg.
7 #include <condition_variable>
31 #include "libavcodec/avcodec.h"
32 #include "libavformat/avformat.h"
33 #include "libavformat/avio.h"
34 #include "libavformat/version.h"
35 #include "libavutil/avutil.h"
36 #include "libavutil/error.h"
37 #include "libavutil/frame.h"
38 #include "libavutil/mem.h"
39 #include "libavutil/pixfmt.h"
40 #include "libavutil/rational.h"
41 #include "libavutil/samplefmt.h"
42 #include "libavutil/time.h"
43 #include "libavutil/version.h"
44 #include "libavutil/channel_layout.h"
45 #include "libswscale/swscale.h"
46 #include "libswresample/swresample.h"
57 #include "common/alhelpers.h"
60 /* Undefine this to disable use of experimental extensions. Don't use for
61 * production code! Interfaces and behavior may change prior to being
64 #define ALLOW_EXPERIMENTAL_EXTS
66 #ifdef ALLOW_EXPERIMENTAL_EXTS
67 #ifndef AL_SOFT_map_buffer
68 #define AL_SOFT_map_buffer 1
69 typedef unsigned int ALbitfieldSOFT
;
70 #define AL_MAP_READ_BIT_SOFT 0x00000001
71 #define AL_MAP_WRITE_BIT_SOFT 0x00000002
72 #define AL_MAP_PERSISTENT_BIT_SOFT 0x00000004
73 #define AL_PRESERVE_DATA_BIT_SOFT 0x00000008
74 typedef void (AL_APIENTRY
*LPALBUFFERSTORAGESOFT
)(ALuint buffer
, ALenum format
, const ALvoid
*data
, ALsizei size
, ALsizei freq
, ALbitfieldSOFT flags
);
75 typedef void* (AL_APIENTRY
*LPALMAPBUFFERSOFT
)(ALuint buffer
, ALsizei offset
, ALsizei length
, ALbitfieldSOFT access
);
76 typedef void (AL_APIENTRY
*LPALUNMAPBUFFERSOFT
)(ALuint buffer
);
77 typedef void (AL_APIENTRY
*LPALFLUSHMAPPEDBUFFERSOFT
)(ALuint buffer
, ALsizei offset
, ALsizei length
);
80 #ifndef AL_SOFT_events
81 #define AL_SOFT_events 1
82 #define AL_EVENT_CALLBACK_FUNCTION_SOFT 0x1220
83 #define AL_EVENT_CALLBACK_USER_PARAM_SOFT 0x1221
84 #define AL_EVENT_TYPE_BUFFER_COMPLETED_SOFT 0x1222
85 #define AL_EVENT_TYPE_SOURCE_STATE_CHANGED_SOFT 0x1223
86 #define AL_EVENT_TYPE_ERROR_SOFT 0x1224
87 #define AL_EVENT_TYPE_PERFORMANCE_SOFT 0x1225
88 #define AL_EVENT_TYPE_DEPRECATED_SOFT 0x1226
89 #define AL_EVENT_TYPE_DISCONNECTED_SOFT 0x1227
90 typedef void (AL_APIENTRY
*ALEVENTPROCSOFT
)(ALenum eventType
, ALuint object
, ALuint param
,
91 ALsizei length
, const ALchar
*message
,
93 typedef void (AL_APIENTRY
*LPALEVENTCONTROLSOFT
)(ALsizei count
, const ALenum
*types
, ALboolean enable
);
94 typedef void (AL_APIENTRY
*LPALEVENTCALLBACKSOFT
)(ALEVENTPROCSOFT callback
, void *userParam
);
95 typedef void* (AL_APIENTRY
*LPALGETPOINTERSOFT
)(ALenum pname
);
96 typedef void (AL_APIENTRY
*LPALGETPOINTERVSOFT
)(ALenum pname
, void **values
);
98 #endif /* ALLOW_EXPERIMENTAL_EXTS */
103 inline constexpr int64_t operator "" _i64(unsigned long long int n
) noexcept
{ return static_cast<int64_t>(n
); }
106 #define M_PI (3.14159265358979323846)
109 using fixed32
= std::chrono::duration
<int64_t,std::ratio
<1,(1_i64
<<32)>>;
110 using nanoseconds
= std::chrono::nanoseconds
;
111 using microseconds
= std::chrono::microseconds
;
112 using milliseconds
= std::chrono::milliseconds
;
113 using seconds
= std::chrono::seconds
;
114 using seconds_d64
= std::chrono::duration
<double>;
116 const std::string AppName
{"alffplay"};
118 bool EnableDirectOut
{false};
119 bool EnableWideStereo
{false};
120 bool DisableVideo
{false};
121 LPALGETSOURCEI64VSOFT alGetSourcei64vSOFT
;
122 LPALCGETINTEGER64VSOFT alcGetInteger64vSOFT
;
124 #ifdef AL_SOFT_map_buffer
125 LPALBUFFERSTORAGESOFT alBufferStorageSOFT
;
126 LPALMAPBUFFERSOFT alMapBufferSOFT
;
127 LPALUNMAPBUFFERSOFT alUnmapBufferSOFT
;
130 #ifdef AL_SOFT_events
131 LPALEVENTCONTROLSOFT alEventControlSOFT
;
132 LPALEVENTCALLBACKSOFT alEventCallbackSOFT
;
135 const seconds AVNoSyncThreshold
{10};
137 const milliseconds VideoSyncThreshold
{10};
138 #define VIDEO_PICTURE_QUEUE_SIZE 24
140 const seconds_d64 AudioSyncThreshold
{0.03};
141 const milliseconds AudioSampleCorrectionMax
{50};
142 /* Averaging filter coefficient for audio sync. */
143 #define AUDIO_DIFF_AVG_NB 20
144 const double AudioAvgFilterCoeff
{std::pow(0.01, 1.0/AUDIO_DIFF_AVG_NB
)};
145 /* Per-buffer size, in time */
146 const milliseconds AudioBufferTime
{20};
147 /* Buffer total size, in time (should be divisible by the buffer time) */
148 const milliseconds AudioBufferTotalTime
{800};
151 FF_MOVIE_DONE_EVENT
= SDL_USEREVENT
154 enum class SyncMaster
{
163 inline microseconds
get_avtime()
164 { return microseconds
{av_gettime()}; }
166 /* Define unique_ptrs to auto-cleanup associated ffmpeg objects. */
167 struct AVIOContextDeleter
{
168 void operator()(AVIOContext
*ptr
) { avio_closep(&ptr
); }
170 using AVIOContextPtr
= std::unique_ptr
<AVIOContext
,AVIOContextDeleter
>;
172 struct AVFormatCtxDeleter
{
173 void operator()(AVFormatContext
*ptr
) { avformat_close_input(&ptr
); }
175 using AVFormatCtxPtr
= std::unique_ptr
<AVFormatContext
,AVFormatCtxDeleter
>;
177 struct AVCodecCtxDeleter
{
178 void operator()(AVCodecContext
*ptr
) { avcodec_free_context(&ptr
); }
180 using AVCodecCtxPtr
= std::unique_ptr
<AVCodecContext
,AVCodecCtxDeleter
>;
182 struct AVFrameDeleter
{
183 void operator()(AVFrame
*ptr
) { av_frame_free(&ptr
); }
185 using AVFramePtr
= std::unique_ptr
<AVFrame
,AVFrameDeleter
>;
187 struct SwrContextDeleter
{
188 void operator()(SwrContext
*ptr
) { swr_free(&ptr
); }
190 using SwrContextPtr
= std::unique_ptr
<SwrContext
,SwrContextDeleter
>;
192 struct SwsContextDeleter
{
193 void operator()(SwsContext
*ptr
) { sws_freeContext(ptr
); }
195 using SwsContextPtr
= std::unique_ptr
<SwsContext
,SwsContextDeleter
>;
198 template<size_t SizeLimit
>
201 std::condition_variable mCondVar
;
202 std::deque
<AVPacket
> mPackets
;
203 size_t mTotalSize
{0};
204 bool mFinished
{false};
206 AVPacket
*getPacket(std::unique_lock
<std::mutex
> &lock
)
208 while(mPackets
.empty() && !mFinished
)
210 return mPackets
.empty() ? nullptr : &mPackets
.front();
215 AVPacket
*pkt
= &mPackets
.front();
216 mTotalSize
-= static_cast<unsigned int>(pkt
->size
);
217 av_packet_unref(pkt
);
218 mPackets
.pop_front();
224 for(AVPacket
&pkt
: mPackets
)
225 av_packet_unref(&pkt
);
230 int sendTo(AVCodecContext
*codecctx
)
232 std::unique_lock
<std::mutex
> lock
{mMutex
};
234 AVPacket
*pkt
{getPacket(lock
)};
235 if(!pkt
) return avcodec_send_packet(codecctx
, nullptr);
237 const int ret
{avcodec_send_packet(codecctx
, pkt
)};
238 if(ret
!= AVERROR(EAGAIN
))
241 std::cerr
<< "Failed to send packet: "<<ret
<<std::endl
;
250 std::lock_guard
<std::mutex
> _
{mMutex
};
253 mCondVar
.notify_one();
256 bool put(const AVPacket
*pkt
)
259 std::unique_lock
<std::mutex
> lock
{mMutex
};
260 if(mTotalSize
>= SizeLimit
)
263 mPackets
.push_back(AVPacket
{});
264 if(av_packet_ref(&mPackets
.back(), pkt
) != 0)
270 mTotalSize
+= static_cast<unsigned int>(mPackets
.back().size
);
272 mCondVar
.notify_one();
283 AVStream
*mStream
{nullptr};
284 AVCodecCtxPtr mCodecCtx
;
286 PacketQueue
<2*1024*1024> mPackets
;
288 /* Used for clock difference average computation */
289 seconds_d64 mClockDiffAvg
{0};
291 /* Time of the next sample to be buffered */
292 nanoseconds mCurrentPts
{0};
294 /* Device clock time that the stream started at. */
295 nanoseconds mDeviceStartTime
{nanoseconds::min()};
297 /* Decompressed sample frame, and swresample context for conversion */
298 AVFramePtr mDecodedFrame
;
299 SwrContextPtr mSwresCtx
;
301 /* Conversion format, for what gets fed to OpenAL */
302 uint64_t mDstChanLayout
{0};
303 AVSampleFormat mDstSampleFmt
{AV_SAMPLE_FMT_NONE
};
305 /* Storage of converted samples */
306 uint8_t *mSamples
{nullptr};
307 int mSamplesLen
{0}; /* In samples */
312 ALenum mFormat
{AL_NONE
};
313 ALuint mFrameSize
{0};
315 std::mutex mSrcMutex
;
316 std::condition_variable mSrcCond
;
317 std::atomic_flag mConnected
;
319 std::vector
<ALuint
> mBuffers
;
320 ALuint mBufferIdx
{0};
322 AudioState(MovieState
&movie
) : mMovie(movie
)
323 { mConnected
.test_and_set(std::memory_order_relaxed
); }
327 alDeleteSources(1, &mSource
);
328 if(!mBuffers
.empty())
329 alDeleteBuffers(static_cast<ALsizei
>(mBuffers
.size()), mBuffers
.data());
334 #ifdef AL_SOFT_events
335 static void AL_APIENTRY
EventCallback(ALenum eventType
, ALuint object
, ALuint param
,
336 ALsizei length
, const ALchar
*message
,
340 nanoseconds
getClockNoLock();
341 nanoseconds
getClock()
343 std::lock_guard
<std::mutex
> lock
{mSrcMutex
};
344 return getClockNoLock();
347 void startPlayback();
351 bool readAudio(uint8_t *samples
, unsigned int length
);
359 AVStream
*mStream
{nullptr};
360 AVCodecCtxPtr mCodecCtx
;
362 PacketQueue
<14*1024*1024> mPackets
;
364 /* The pts of the currently displayed frame, and the time (av_gettime) it
365 * was last updated - used to have running video pts
367 nanoseconds mDisplayPts
{0};
368 microseconds mDisplayPtsTime
{microseconds::min()};
369 std::mutex mDispPtsMutex
;
371 /* Swscale context for format conversion */
372 SwsContextPtr mSwscaleCtx
;
376 nanoseconds mPts
{nanoseconds::min()};
378 std::array
<Picture
,VIDEO_PICTURE_QUEUE_SIZE
> mPictQ
;
379 std::atomic
<size_t> mPictQRead
{0u}, mPictQWrite
{1u};
380 std::mutex mPictQMutex
;
381 std::condition_variable mPictQCond
;
383 SDL_Texture
*mImage
{nullptr};
384 int mWidth
{0}, mHeight
{0}; /* Logical image size (actual size may be larger) */
385 bool mFirstUpdate
{true};
387 std::atomic
<bool> mEOS
{false};
388 std::atomic
<bool> mFinalUpdate
{false};
390 VideoState(MovieState
&movie
) : mMovie(movie
) { }
394 SDL_DestroyTexture(mImage
);
398 nanoseconds
getClock();
400 void display(SDL_Window
*screen
, SDL_Renderer
*renderer
);
401 void updateVideo(SDL_Window
*screen
, SDL_Renderer
*renderer
, bool redraw
);
406 AVIOContextPtr mIOContext
;
407 AVFormatCtxPtr mFormatCtx
;
409 SyncMaster mAVSyncType
{SyncMaster::Default
};
411 microseconds mClockBase
{microseconds::min()};
413 std::atomic
<bool> mQuit
{false};
418 std::thread mParseThread
;
419 std::thread mAudioThread
;
420 std::thread mVideoThread
;
422 std::string mFilename
;
424 MovieState(std::string fname
)
425 : mAudio(*this), mVideo(*this), mFilename(std::move(fname
))
430 if(mParseThread
.joinable())
434 static int decode_interrupt_cb(void *ctx
);
436 void setTitle(SDL_Window
*window
);
438 nanoseconds
getClock();
440 nanoseconds
getMasterClock();
442 nanoseconds
getDuration();
444 int streamComponentOpen(unsigned int stream_index
);
449 nanoseconds
AudioState::getClockNoLock()
451 // The audio clock is the timestamp of the sample currently being heard.
452 if(alcGetInteger64vSOFT
)
454 // If device start time = min, we aren't playing yet.
455 if(mDeviceStartTime
== nanoseconds::min())
456 return nanoseconds::zero();
458 // Get the current device clock time and latency.
459 auto device
= alcGetContextsDevice(alcGetCurrentContext());
460 ALCint64SOFT devtimes
[2]{0,0};
461 alcGetInteger64vSOFT(device
, ALC_DEVICE_CLOCK_LATENCY_SOFT
, 2, devtimes
);
462 auto latency
= nanoseconds
{devtimes
[1]};
463 auto device_time
= nanoseconds
{devtimes
[0]};
465 // The clock is simply the current device time relative to the recorded
466 // start time. We can also subtract the latency to get more a accurate
467 // position of where the audio device actually is in the output stream.
468 return device_time
- mDeviceStartTime
- latency
;
471 /* The source-based clock is based on 4 components:
472 * 1 - The timestamp of the next sample to buffer (mCurrentPts)
473 * 2 - The length of the source's buffer queue
474 * (AudioBufferTime*AL_BUFFERS_QUEUED)
475 * 3 - The offset OpenAL is currently at in the source (the first value
476 * from AL_SAMPLE_OFFSET_LATENCY_SOFT)
477 * 4 - The latency between OpenAL and the DAC (the second value from
478 * AL_SAMPLE_OFFSET_LATENCY_SOFT)
480 * Subtracting the length of the source queue from the next sample's
481 * timestamp gives the timestamp of the sample at the start of the source
482 * queue. Adding the source offset to that results in the timestamp for the
483 * sample at OpenAL's current position, and subtracting the source latency
484 * from that gives the timestamp of the sample currently at the DAC.
486 nanoseconds pts
{mCurrentPts
};
489 ALint64SOFT offset
[2];
491 /* NOTE: The source state must be checked last, in case an underrun
492 * occurs and the source stops between retrieving the offset+latency
493 * and getting the state. */
494 if(alGetSourcei64vSOFT
)
495 alGetSourcei64vSOFT(mSource
, AL_SAMPLE_OFFSET_LATENCY_SOFT
, offset
);
499 alGetSourcei(mSource
, AL_SAMPLE_OFFSET
, &ioffset
);
500 offset
[0] = ALint64SOFT
{ioffset
} << 32;
503 ALint queued
, status
;
504 alGetSourcei(mSource
, AL_BUFFERS_QUEUED
, &queued
);
505 alGetSourcei(mSource
, AL_SOURCE_STATE
, &status
);
507 /* If the source is AL_STOPPED, then there was an underrun and all
508 * buffers are processed, so ignore the source queue. The audio thread
509 * will put the source into an AL_INITIAL state and clear the queue
510 * when it starts recovery. */
511 if(status
!= AL_STOPPED
)
513 pts
-= AudioBufferTime
*queued
;
514 pts
+= std::chrono::duration_cast
<nanoseconds
>(
515 fixed32
{offset
[0] / mCodecCtx
->sample_rate
});
517 /* Don't offset by the latency if the source isn't playing. */
518 if(status
== AL_PLAYING
)
519 pts
-= nanoseconds
{offset
[1]};
522 return std::max(pts
, nanoseconds::zero());
525 void AudioState::startPlayback()
527 alSourcePlay(mSource
);
528 if(alcGetInteger64vSOFT
)
530 // Subtract the total buffer queue time from the current pts to get the
531 // pts of the start of the queue.
532 nanoseconds startpts
{mCurrentPts
- AudioBufferTotalTime
};
533 int64_t srctimes
[2]{0,0};
534 alGetSourcei64vSOFT(mSource
, AL_SAMPLE_OFFSET_CLOCK_SOFT
, srctimes
);
535 auto device_time
= nanoseconds
{srctimes
[1]};
536 auto src_offset
= std::chrono::duration_cast
<nanoseconds
>(fixed32
{srctimes
[0]}) /
537 mCodecCtx
->sample_rate
;
539 // The mixer may have ticked and incremented the device time and sample
540 // offset, so subtract the source offset from the device time to get
541 // the device time the source started at. Also subtract startpts to get
542 // the device time the stream would have started at to reach where it
544 mDeviceStartTime
= device_time
- src_offset
- startpts
;
548 int AudioState::getSync()
550 if(mMovie
.mAVSyncType
== SyncMaster::Audio
)
553 auto ref_clock
= mMovie
.getMasterClock();
554 auto diff
= ref_clock
- getClockNoLock();
556 if(!(diff
< AVNoSyncThreshold
&& diff
> -AVNoSyncThreshold
))
558 /* Difference is TOO big; reset accumulated average */
559 mClockDiffAvg
= seconds_d64::zero();
563 /* Accumulate the diffs */
564 mClockDiffAvg
= mClockDiffAvg
*AudioAvgFilterCoeff
+ diff
;
565 auto avg_diff
= mClockDiffAvg
*(1.0 - AudioAvgFilterCoeff
);
566 if(avg_diff
< AudioSyncThreshold
/2.0 && avg_diff
> -AudioSyncThreshold
)
569 /* Constrain the per-update difference to avoid exceedingly large skips */
570 diff
= std::min
<nanoseconds
>(diff
, AudioSampleCorrectionMax
);
571 return static_cast<int>(std::chrono::duration_cast
<seconds
>(diff
*mCodecCtx
->sample_rate
).count());
574 int AudioState::decodeFrame()
576 while(!mMovie
.mQuit
.load(std::memory_order_relaxed
))
579 while((ret
=avcodec_receive_frame(mCodecCtx
.get(), mDecodedFrame
.get())) == AVERROR(EAGAIN
))
580 mPackets
.sendTo(mCodecCtx
.get());
583 if(ret
== AVERROR_EOF
) break;
584 std::cerr
<< "Failed to receive frame: "<<ret
<<std::endl
;
588 if(mDecodedFrame
->nb_samples
<= 0)
591 /* If provided, update w/ pts */
592 if(mDecodedFrame
->best_effort_timestamp
!= AV_NOPTS_VALUE
)
593 mCurrentPts
= std::chrono::duration_cast
<nanoseconds
>(
594 seconds_d64
{av_q2d(mStream
->time_base
)*mDecodedFrame
->best_effort_timestamp
}
597 if(mDecodedFrame
->nb_samples
> mSamplesMax
)
601 &mSamples
, nullptr, mCodecCtx
->channels
,
602 mDecodedFrame
->nb_samples
, mDstSampleFmt
, 0
604 mSamplesMax
= mDecodedFrame
->nb_samples
;
606 /* Return the amount of sample frames converted */
607 int data_size
{swr_convert(mSwresCtx
.get(), &mSamples
, mDecodedFrame
->nb_samples
,
608 const_cast<const uint8_t**>(mDecodedFrame
->data
), mDecodedFrame
->nb_samples
)};
610 av_frame_unref(mDecodedFrame
.get());
617 /* Duplicates the sample at in to out, count times. The frame size is a
618 * multiple of the template type size.
621 static void sample_dup(uint8_t *out
, const uint8_t *in
, unsigned int count
, size_t frame_size
)
623 auto *sample
= reinterpret_cast<const T
*>(in
);
624 auto *dst
= reinterpret_cast<T
*>(out
);
625 if(frame_size
== sizeof(T
))
626 std::fill_n(dst
, count
, *sample
);
629 /* NOTE: frame_size is a multiple of sizeof(T). */
630 size_t type_mult
{frame_size
/ sizeof(T
)};
632 std::generate_n(dst
, count
*type_mult
,
633 [sample
,type_mult
,&i
]() -> T
644 bool AudioState::readAudio(uint8_t *samples
, unsigned int length
)
646 int sample_skip
{getSync()};
647 unsigned int audio_size
{0};
649 /* Read the next chunk of data, refill the buffer, and queue it
651 length
/= mFrameSize
;
652 while(audio_size
< length
)
654 if(mSamplesLen
<= 0 || mSamplesPos
>= mSamplesLen
)
656 int frame_len
= decodeFrame();
657 if(frame_len
<= 0) break;
659 mSamplesLen
= frame_len
;
660 mSamplesPos
= std::min(mSamplesLen
, sample_skip
);
661 sample_skip
-= mSamplesPos
;
663 // Adjust the device start time and current pts by the amount we're
664 // skipping/duplicating, so that the clock remains correct for the
665 // current stream position.
666 auto skip
= nanoseconds
{seconds
{mSamplesPos
}} / mCodecCtx
->sample_rate
;
667 mDeviceStartTime
-= skip
;
672 unsigned int rem
{length
- audio_size
};
675 const auto len
= static_cast<unsigned int>(mSamplesLen
- mSamplesPos
);
676 if(rem
> len
) rem
= len
;
677 std::copy_n(mSamples
+ static_cast<unsigned int>(mSamplesPos
)*mFrameSize
,
678 rem
*mFrameSize
, samples
);
682 rem
= std::min(rem
, static_cast<unsigned int>(-mSamplesPos
));
684 /* Add samples by copying the first sample */
685 if((mFrameSize
&7) == 0)
686 sample_dup
<uint64_t>(samples
, mSamples
, rem
, mFrameSize
);
687 else if((mFrameSize
&3) == 0)
688 sample_dup
<uint32_t>(samples
, mSamples
, rem
, mFrameSize
);
689 else if((mFrameSize
&1) == 0)
690 sample_dup
<uint16_t>(samples
, mSamples
, rem
, mFrameSize
);
692 sample_dup
<uint8_t>(samples
, mSamples
, rem
, mFrameSize
);
696 mCurrentPts
+= nanoseconds
{seconds
{rem
}} / mCodecCtx
->sample_rate
;
697 samples
+= rem
*mFrameSize
;
703 if(audio_size
< length
)
705 const unsigned int rem
{length
- audio_size
};
706 std::fill_n(samples
, rem
*mFrameSize
,
707 (mDstSampleFmt
== AV_SAMPLE_FMT_U8
) ? 0x80 : 0x00);
708 mCurrentPts
+= nanoseconds
{seconds
{rem
}} / mCodecCtx
->sample_rate
;
715 #ifdef AL_SOFT_events
716 void AL_APIENTRY
AudioState::EventCallback(ALenum eventType
, ALuint object
, ALuint param
,
717 ALsizei length
, const ALchar
*message
,
720 auto self
= static_cast<AudioState
*>(userParam
);
722 if(eventType
== AL_EVENT_TYPE_BUFFER_COMPLETED_SOFT
)
724 /* Temporarily lock the source mutex to ensure it's not between
725 * checking the processed count and going to sleep.
727 std::unique_lock
<std::mutex
>{self
->mSrcMutex
}.unlock();
728 self
->mSrcCond
.notify_one();
732 std::cout
<< "\n---- AL Event on AudioState "<<self
<<" ----\nEvent: ";
735 case AL_EVENT_TYPE_BUFFER_COMPLETED_SOFT
: std::cout
<< "Buffer completed"; break;
736 case AL_EVENT_TYPE_SOURCE_STATE_CHANGED_SOFT
: std::cout
<< "Source state changed"; break;
737 case AL_EVENT_TYPE_ERROR_SOFT
: std::cout
<< "API error"; break;
738 case AL_EVENT_TYPE_PERFORMANCE_SOFT
: std::cout
<< "Performance"; break;
739 case AL_EVENT_TYPE_DEPRECATED_SOFT
: std::cout
<< "Deprecated"; break;
740 case AL_EVENT_TYPE_DISCONNECTED_SOFT
: std::cout
<< "Disconnected"; break;
741 default: std::cout
<< "0x"<<std::hex
<<std::setw(4)<<std::setfill('0')<<eventType
<<
742 std::dec
<<std::setw(0)<<std::setfill(' '); break;
745 "Object ID: "<<object
<<"\n"
746 "Parameter: "<<param
<<"\n"
747 "Message: "<<std::string
{message
, static_cast<ALuint
>(length
)}<<"\n----"<<
750 if(eventType
== AL_EVENT_TYPE_DISCONNECTED_SOFT
)
753 std::lock_guard
<std::mutex
> lock
{self
->mSrcMutex
};
754 self
->mConnected
.clear(std::memory_order_release
);
756 self
->mSrcCond
.notify_one();
761 int AudioState::handler()
763 std::unique_lock
<std::mutex
> srclock
{mSrcMutex
, std::defer_lock
};
764 milliseconds sleep_time
{AudioBufferTime
/ 3};
767 #ifdef AL_SOFT_events
768 const std::array
<ALenum
,6> evt_types
{{
769 AL_EVENT_TYPE_BUFFER_COMPLETED_SOFT
, AL_EVENT_TYPE_SOURCE_STATE_CHANGED_SOFT
,
770 AL_EVENT_TYPE_ERROR_SOFT
, AL_EVENT_TYPE_PERFORMANCE_SOFT
, AL_EVENT_TYPE_DEPRECATED_SOFT
,
771 AL_EVENT_TYPE_DISCONNECTED_SOFT
773 if(alEventControlSOFT
)
775 alEventControlSOFT(evt_types
.size(), evt_types
.data(), AL_TRUE
);
776 alEventCallbackSOFT(EventCallback
, this);
777 sleep_time
= AudioBufferTotalTime
;
781 /* Find a suitable format for OpenAL. */
784 if((mCodecCtx
->sample_fmt
== AV_SAMPLE_FMT_FLT
|| mCodecCtx
->sample_fmt
== AV_SAMPLE_FMT_FLTP
) &&
785 alIsExtensionPresent("AL_EXT_FLOAT32"))
787 mDstSampleFmt
= AV_SAMPLE_FMT_FLT
;
789 if(mCodecCtx
->channel_layout
== AV_CH_LAYOUT_7POINT1
&&
790 alIsExtensionPresent("AL_EXT_MCFORMATS") &&
791 (fmt
=alGetEnumValue("AL_FORMAT_71CHN32")) != AL_NONE
&& fmt
!= -1)
793 mDstChanLayout
= mCodecCtx
->channel_layout
;
797 if((mCodecCtx
->channel_layout
== AV_CH_LAYOUT_5POINT1
||
798 mCodecCtx
->channel_layout
== AV_CH_LAYOUT_5POINT1_BACK
) &&
799 alIsExtensionPresent("AL_EXT_MCFORMATS") &&
800 (fmt
=alGetEnumValue("AL_FORMAT_51CHN32")) != AL_NONE
&& fmt
!= -1)
802 mDstChanLayout
= mCodecCtx
->channel_layout
;
806 if(mCodecCtx
->channel_layout
== AV_CH_LAYOUT_MONO
)
808 mDstChanLayout
= mCodecCtx
->channel_layout
;
810 mFormat
= AL_FORMAT_MONO_FLOAT32
;
812 /* Assume 3D B-Format (ambisonics) if the channel layout is blank and
813 * there's 4 or more channels. FFmpeg/libavcodec otherwise seems to
814 * have no way to specify if the source is actually B-Format (let alone
817 if(mCodecCtx
->channel_layout
== 0 && mCodecCtx
->channels
>= 4 &&
818 alIsExtensionPresent("AL_EXT_BFORMAT") &&
819 (fmt
=alGetEnumValue("AL_FORMAT_BFORMAT3D_FLOAT32")) != AL_NONE
&& fmt
!= -1)
821 int order
{static_cast<int>(std::sqrt(mCodecCtx
->channels
)) - 1};
822 if((order
+1)*(order
+1) == mCodecCtx
->channels
||
823 (order
+1)*(order
+1) + 2 == mCodecCtx
->channels
)
825 /* OpenAL only supports first-order with AL_EXT_BFORMAT, which
826 * is 4 channels for 3D buffers.
834 mDstChanLayout
= AV_CH_LAYOUT_STEREO
;
836 mFormat
= AL_FORMAT_STEREO_FLOAT32
;
839 if(mCodecCtx
->sample_fmt
== AV_SAMPLE_FMT_U8
|| mCodecCtx
->sample_fmt
== AV_SAMPLE_FMT_U8P
)
841 mDstSampleFmt
= AV_SAMPLE_FMT_U8
;
843 if(mCodecCtx
->channel_layout
== AV_CH_LAYOUT_7POINT1
&&
844 alIsExtensionPresent("AL_EXT_MCFORMATS") &&
845 (fmt
=alGetEnumValue("AL_FORMAT_71CHN8")) != AL_NONE
&& fmt
!= -1)
847 mDstChanLayout
= mCodecCtx
->channel_layout
;
851 if((mCodecCtx
->channel_layout
== AV_CH_LAYOUT_5POINT1
||
852 mCodecCtx
->channel_layout
== AV_CH_LAYOUT_5POINT1_BACK
) &&
853 alIsExtensionPresent("AL_EXT_MCFORMATS") &&
854 (fmt
=alGetEnumValue("AL_FORMAT_51CHN8")) != AL_NONE
&& fmt
!= -1)
856 mDstChanLayout
= mCodecCtx
->channel_layout
;
860 if(mCodecCtx
->channel_layout
== AV_CH_LAYOUT_MONO
)
862 mDstChanLayout
= mCodecCtx
->channel_layout
;
864 mFormat
= AL_FORMAT_MONO8
;
866 if(mCodecCtx
->channel_layout
== 0 && mCodecCtx
->channels
>= 4 &&
867 alIsExtensionPresent("AL_EXT_BFORMAT") &&
868 (fmt
=alGetEnumValue("AL_FORMAT_BFORMAT3D8")) != AL_NONE
&& fmt
!= -1)
870 int order
{static_cast<int>(std::sqrt(mCodecCtx
->channels
)) - 1};
871 if((order
+1)*(order
+1) == mCodecCtx
->channels
||
872 (order
+1)*(order
+1) + 2 == mCodecCtx
->channels
)
880 mDstChanLayout
= AV_CH_LAYOUT_STEREO
;
882 mFormat
= AL_FORMAT_STEREO8
;
887 mDstSampleFmt
= AV_SAMPLE_FMT_S16
;
889 if(mCodecCtx
->channel_layout
== AV_CH_LAYOUT_7POINT1
&&
890 alIsExtensionPresent("AL_EXT_MCFORMATS") &&
891 (fmt
=alGetEnumValue("AL_FORMAT_71CHN16")) != AL_NONE
&& fmt
!= -1)
893 mDstChanLayout
= mCodecCtx
->channel_layout
;
897 if((mCodecCtx
->channel_layout
== AV_CH_LAYOUT_5POINT1
||
898 mCodecCtx
->channel_layout
== AV_CH_LAYOUT_5POINT1_BACK
) &&
899 alIsExtensionPresent("AL_EXT_MCFORMATS") &&
900 (fmt
=alGetEnumValue("AL_FORMAT_51CHN16")) != AL_NONE
&& fmt
!= -1)
902 mDstChanLayout
= mCodecCtx
->channel_layout
;
906 if(mCodecCtx
->channel_layout
== AV_CH_LAYOUT_MONO
)
908 mDstChanLayout
= mCodecCtx
->channel_layout
;
910 mFormat
= AL_FORMAT_MONO16
;
912 if(mCodecCtx
->channel_layout
== 0 && mCodecCtx
->channels
>= 4 &&
913 alIsExtensionPresent("AL_EXT_BFORMAT") &&
914 (fmt
=alGetEnumValue("AL_FORMAT_BFORMAT3D16")) != AL_NONE
&& fmt
!= -1)
916 int order
{static_cast<int>(std::sqrt(mCodecCtx
->channels
)) - 1};
917 if((order
+1)*(order
+1) == mCodecCtx
->channels
||
918 (order
+1)*(order
+1) + 2 == mCodecCtx
->channels
)
926 mDstChanLayout
= AV_CH_LAYOUT_STEREO
;
928 mFormat
= AL_FORMAT_STEREO16
;
931 void *samples
{nullptr};
932 ALsizei buffer_len
= static_cast<int>(std::chrono::duration_cast
<seconds
>(
933 mCodecCtx
->sample_rate
* AudioBufferTime
).count() * mFrameSize
);
940 mDecodedFrame
.reset(av_frame_alloc());
943 std::cerr
<< "Failed to allocate audio frame" <<std::endl
;
949 /* OpenAL only supports first-order ambisonics with AL_EXT_BFORMAT, so
950 * we have to drop any extra channels. It also only supports FuMa
951 * channel ordering and normalization, so a custom matrix is needed to
952 * scale and reorder the source from AmbiX.
954 mSwresCtx
.reset(swr_alloc_set_opts(nullptr,
955 (1_i64
<<4)-1, mDstSampleFmt
, mCodecCtx
->sample_rate
,
956 (1_i64
<<mCodecCtx
->channels
)-1, mCodecCtx
->sample_fmt
, mCodecCtx
->sample_rate
,
959 /* Note that ffmpeg/libavcodec has no method to check the ambisonic
960 * channel order and normalization, so we can only assume AmbiX as the
961 * defacto-standard. This is not true for .amb files, which use FuMa.
963 std::vector
<double> mtx(64*64, 0.0);
964 mtx
[0 + 0*64] = std::sqrt(0.5);
968 swr_set_matrix(mSwresCtx
.get(), mtx
.data(), 64);
971 mSwresCtx
.reset(swr_alloc_set_opts(nullptr,
972 static_cast<int64_t>(mDstChanLayout
), mDstSampleFmt
, mCodecCtx
->sample_rate
,
973 mCodecCtx
->channel_layout
? static_cast<int64_t>(mCodecCtx
->channel_layout
) :
974 av_get_default_channel_layout(mCodecCtx
->channels
),
975 mCodecCtx
->sample_fmt
, mCodecCtx
->sample_rate
,
977 if(!mSwresCtx
|| swr_init(mSwresCtx
.get()) != 0)
979 std::cerr
<< "Failed to initialize audio converter" <<std::endl
;
983 mBuffers
.assign(AudioBufferTotalTime
/ AudioBufferTime
, 0);
984 alGenBuffers(static_cast<ALsizei
>(mBuffers
.size()), mBuffers
.data());
985 alGenSources(1, &mSource
);
988 alSourcei(mSource
, AL_DIRECT_CHANNELS_SOFT
, AL_TRUE
);
989 if (EnableWideStereo
) {
990 ALfloat angles
[2] = {static_cast<ALfloat
>(M_PI
/ 3.0),
991 static_cast<ALfloat
>(-M_PI
/ 3.0)};
992 alSourcefv(mSource
, AL_STEREO_ANGLES
, angles
);
995 if(alGetError() != AL_NO_ERROR
)
998 #ifdef AL_SOFT_map_buffer
999 if(alBufferStorageSOFT
)
1001 for(ALuint bufid
: mBuffers
)
1002 alBufferStorageSOFT(bufid
, mFormat
, nullptr, buffer_len
, mCodecCtx
->sample_rate
,
1003 AL_MAP_WRITE_BIT_SOFT
);
1004 if(alGetError() != AL_NO_ERROR
)
1006 fprintf(stderr
, "Failed to use mapped buffers\n");
1007 samples
= av_malloc(static_cast<ALuint
>(buffer_len
));
1012 samples
= av_malloc(static_cast<ALuint
>(buffer_len
));
1014 /* Prefill the codec buffer. */
1016 const int ret
{mPackets
.sendTo(mCodecCtx
.get())};
1017 if(ret
== AVERROR(EAGAIN
) || ret
== AVERROR_EOF
)
1022 if(alcGetInteger64vSOFT
)
1025 alcGetInteger64vSOFT(alcGetContextsDevice(alcGetCurrentContext()), ALC_DEVICE_CLOCK_SOFT
,
1027 mDeviceStartTime
= nanoseconds
{devtime
} - mCurrentPts
;
1029 while(alGetError() == AL_NO_ERROR
&& !mMovie
.mQuit
.load(std::memory_order_relaxed
) &&
1030 mConnected
.test_and_set(std::memory_order_relaxed
))
1032 /* First remove any processed buffers. */
1034 alGetSourcei(mSource
, AL_BUFFERS_PROCESSED
, &processed
);
1035 while(processed
> 0)
1037 std::array
<ALuint
,4> bids
;
1038 const ALsizei todq
{std::min
<ALsizei
>(bids
.size(), processed
)};
1039 alSourceUnqueueBuffers(mSource
, todq
, bids
.data());
1043 /* Refill the buffer queue. */
1045 alGetSourcei(mSource
, AL_BUFFERS_QUEUED
, &queued
);
1046 while(static_cast<ALuint
>(queued
) < mBuffers
.size())
1048 const ALuint bufid
{mBuffers
[mBufferIdx
]};
1049 /* Read the next chunk of data, filling the buffer, and queue it on
1052 #ifdef AL_SOFT_map_buffer
1055 auto ptr
= static_cast<uint8_t*>(alMapBufferSOFT(bufid
, 0, buffer_len
,
1056 AL_MAP_WRITE_BIT_SOFT
));
1057 bool got_audio
{readAudio(ptr
, static_cast<unsigned int>(buffer_len
))};
1058 alUnmapBufferSOFT(bufid
);
1059 if(!got_audio
) break;
1064 auto ptr
= static_cast<uint8_t*>(samples
);
1065 if(!readAudio(ptr
, static_cast<unsigned int>(buffer_len
)))
1067 alBufferData(bufid
, mFormat
, samples
, buffer_len
, mCodecCtx
->sample_rate
);
1070 alSourceQueueBuffers(mSource
, 1, &bufid
);
1071 mBufferIdx
= (mBufferIdx
+1) % mBuffers
.size();
1077 /* Check that the source is playing. */
1079 alGetSourcei(mSource
, AL_SOURCE_STATE
, &state
);
1080 if(state
== AL_STOPPED
)
1082 /* AL_STOPPED means there was an underrun. Clear the buffer queue
1083 * since this likely means we're late, and rewind the source to get
1084 * it back into an AL_INITIAL state.
1086 alSourceRewind(mSource
);
1087 alSourcei(mSource
, AL_BUFFER
, 0);
1088 if(alcGetInteger64vSOFT
)
1090 /* Also update the device start time with the current device
1091 * clock, so the decoder knows we're running behind.
1094 alcGetInteger64vSOFT(alcGetContextsDevice(alcGetCurrentContext()),
1095 ALC_DEVICE_CLOCK_SOFT
, 1, &devtime
);
1096 mDeviceStartTime
= nanoseconds
{devtime
} - mCurrentPts
;
1101 /* (re)start the source if needed, and wait for a buffer to finish */
1102 if(state
!= AL_PLAYING
&& state
!= AL_PAUSED
)
1105 mSrcCond
.wait_for(srclock
, sleep_time
);
1108 alSourceRewind(mSource
);
1109 alSourcei(mSource
, AL_BUFFER
, 0);
1115 #ifdef AL_SOFT_events
1116 if(alEventControlSOFT
)
1118 alEventControlSOFT(evt_types
.size(), evt_types
.data(), AL_FALSE
);
1119 alEventCallbackSOFT(nullptr, nullptr);
1127 nanoseconds
VideoState::getClock()
1129 /* NOTE: This returns incorrect times while not playing. */
1130 std::lock_guard
<std::mutex
> _
{mDispPtsMutex
};
1131 if(mDisplayPtsTime
== microseconds::min())
1132 return nanoseconds::zero();
1133 auto delta
= get_avtime() - mDisplayPtsTime
;
1134 return mDisplayPts
+ delta
;
1137 /* Called by VideoState::updateVideo to display the next video frame. */
1138 void VideoState::display(SDL_Window
*screen
, SDL_Renderer
*renderer
)
1143 double aspect_ratio
;
1147 if(mCodecCtx
->sample_aspect_ratio
.num
== 0)
1151 aspect_ratio
= av_q2d(mCodecCtx
->sample_aspect_ratio
) * mCodecCtx
->width
/
1154 if(aspect_ratio
<= 0.0)
1155 aspect_ratio
= static_cast<double>(mCodecCtx
->width
) / mCodecCtx
->height
;
1157 SDL_GetWindowSize(screen
, &win_w
, &win_h
);
1159 w
= (static_cast<int>(std::rint(h
* aspect_ratio
)) + 3) & ~3;
1163 h
= (static_cast<int>(std::rint(w
/ aspect_ratio
)) + 3) & ~3;
1165 x
= (win_w
- w
) / 2;
1166 y
= (win_h
- h
) / 2;
1168 SDL_Rect src_rect
{ 0, 0, mWidth
, mHeight
};
1169 SDL_Rect dst_rect
{ x
, y
, w
, h
};
1170 SDL_RenderCopy(renderer
, mImage
, &src_rect
, &dst_rect
);
1171 SDL_RenderPresent(renderer
);
1174 /* Called regularly on the main thread where the SDL_Renderer was created. It
1175 * handles updating the textures of decoded frames and displaying the latest
1178 void VideoState::updateVideo(SDL_Window
*screen
, SDL_Renderer
*renderer
, bool redraw
)
1180 size_t read_idx
{mPictQRead
.load(std::memory_order_relaxed
)};
1181 Picture
*vp
{&mPictQ
[read_idx
]};
1183 auto clocktime
= mMovie
.getMasterClock();
1184 bool updated
{false};
1187 size_t next_idx
{(read_idx
+1)%mPictQ
.size()};
1188 if(next_idx
== mPictQWrite
.load(std::memory_order_acquire
))
1190 Picture
*nextvp
{&mPictQ
[next_idx
]};
1191 if(clocktime
< nextvp
->mPts
)
1196 read_idx
= next_idx
;
1198 if(mMovie
.mQuit
.load(std::memory_order_relaxed
))
1201 mFinalUpdate
= true;
1202 mPictQRead
.store(read_idx
, std::memory_order_release
);
1203 std::unique_lock
<std::mutex
>{mPictQMutex
}.unlock();
1204 mPictQCond
.notify_one();
1210 mPictQRead
.store(read_idx
, std::memory_order_release
);
1211 std::unique_lock
<std::mutex
>{mPictQMutex
}.unlock();
1212 mPictQCond
.notify_one();
1214 /* allocate or resize the buffer! */
1215 bool fmt_updated
{false};
1216 if(!mImage
|| mWidth
!= mCodecCtx
->width
|| mHeight
!= mCodecCtx
->height
)
1220 SDL_DestroyTexture(mImage
);
1221 mImage
= SDL_CreateTexture(renderer
, SDL_PIXELFORMAT_IYUV
, SDL_TEXTUREACCESS_STREAMING
,
1222 mCodecCtx
->coded_width
, mCodecCtx
->coded_height
);
1224 std::cerr
<< "Failed to create YV12 texture!" <<std::endl
;
1225 mWidth
= mCodecCtx
->width
;
1226 mHeight
= mCodecCtx
->height
;
1228 if(mFirstUpdate
&& mWidth
> 0 && mHeight
> 0)
1230 /* For the first update, set the window size to the video size. */
1231 mFirstUpdate
= false;
1235 if(mCodecCtx
->sample_aspect_ratio
.den
!= 0)
1237 double aspect_ratio
= av_q2d(mCodecCtx
->sample_aspect_ratio
);
1238 if(aspect_ratio
>= 1.0)
1239 w
= static_cast<int>(w
*aspect_ratio
+ 0.5);
1240 else if(aspect_ratio
> 0.0)
1241 h
= static_cast<int>(h
/aspect_ratio
+ 0.5);
1243 SDL_SetWindowSize(screen
, w
, h
);
1249 AVFrame
*frame
{vp
->mFrame
.get()};
1250 void *pixels
{nullptr};
1253 if(mCodecCtx
->pix_fmt
== AV_PIX_FMT_YUV420P
)
1254 SDL_UpdateYUVTexture(mImage
, nullptr,
1255 frame
->data
[0], frame
->linesize
[0],
1256 frame
->data
[1], frame
->linesize
[1],
1257 frame
->data
[2], frame
->linesize
[2]
1259 else if(SDL_LockTexture(mImage
, nullptr, &pixels
, &pitch
) != 0)
1260 std::cerr
<< "Failed to lock texture" <<std::endl
;
1263 // Convert the image into YUV format that SDL uses
1264 int coded_w
{mCodecCtx
->coded_width
};
1265 int coded_h
{mCodecCtx
->coded_height
};
1266 int w
{mCodecCtx
->width
};
1267 int h
{mCodecCtx
->height
};
1268 if(!mSwscaleCtx
|| fmt_updated
)
1270 mSwscaleCtx
.reset(sws_getContext(
1271 w
, h
, mCodecCtx
->pix_fmt
,
1272 w
, h
, AV_PIX_FMT_YUV420P
, 0,
1273 nullptr, nullptr, nullptr
1277 /* point pict at the queue */
1278 uint8_t *pict_data
[3];
1279 pict_data
[0] = static_cast<uint8_t*>(pixels
);
1280 pict_data
[1] = pict_data
[0] + coded_w
*coded_h
;
1281 pict_data
[2] = pict_data
[1] + coded_w
*coded_h
/4;
1283 int pict_linesize
[3];
1284 pict_linesize
[0] = pitch
;
1285 pict_linesize
[1] = pitch
/ 2;
1286 pict_linesize
[2] = pitch
/ 2;
1288 sws_scale(mSwscaleCtx
.get(), reinterpret_cast<uint8_t**>(frame
->data
), frame
->linesize
,
1289 0, h
, pict_data
, pict_linesize
);
1290 SDL_UnlockTexture(mImage
);
1299 /* Show the picture! */
1300 display(screen
, renderer
);
1305 auto disp_time
= get_avtime();
1307 std::lock_guard
<std::mutex
> _
{mDispPtsMutex
};
1308 mDisplayPts
= vp
->mPts
;
1309 mDisplayPtsTime
= disp_time
;
1311 if(mEOS
.load(std::memory_order_acquire
))
1313 if((read_idx
+1)%mPictQ
.size() == mPictQWrite
.load(std::memory_order_acquire
))
1315 mFinalUpdate
= true;
1316 std::unique_lock
<std::mutex
>{mPictQMutex
}.unlock();
1317 mPictQCond
.notify_one();
1322 int VideoState::handler()
1324 std::for_each(mPictQ
.begin(), mPictQ
.end(),
1325 [](Picture
&pict
) -> void
1326 { pict
.mFrame
= AVFramePtr
{av_frame_alloc()}; });
1328 /* Prefill the codec buffer. */
1330 const int ret
{mPackets
.sendTo(mCodecCtx
.get())};
1331 if(ret
== AVERROR(EAGAIN
) || ret
== AVERROR_EOF
)
1336 std::lock_guard
<std::mutex
> _
{mDispPtsMutex
};
1337 mDisplayPtsTime
= get_avtime();
1340 auto current_pts
= nanoseconds::zero();
1341 while(!mMovie
.mQuit
.load(std::memory_order_relaxed
))
1343 size_t write_idx
{mPictQWrite
.load(std::memory_order_relaxed
)};
1344 Picture
*vp
{&mPictQ
[write_idx
]};
1346 /* Retrieve video frame. */
1347 AVFrame
*decoded_frame
{vp
->mFrame
.get()};
1349 while((ret
=avcodec_receive_frame(mCodecCtx
.get(), decoded_frame
)) == AVERROR(EAGAIN
))
1350 mPackets
.sendTo(mCodecCtx
.get());
1353 if(ret
== AVERROR_EOF
) break;
1354 std::cerr
<< "Failed to receive frame: "<<ret
<<std::endl
;
1358 /* Get the PTS for this frame. */
1359 if(decoded_frame
->best_effort_timestamp
!= AV_NOPTS_VALUE
)
1360 current_pts
= std::chrono::duration_cast
<nanoseconds
>(
1361 seconds_d64
{av_q2d(mStream
->time_base
)*decoded_frame
->best_effort_timestamp
});
1362 vp
->mPts
= current_pts
;
1364 /* Update the video clock to the next expected PTS. */
1365 auto frame_delay
= av_q2d(mCodecCtx
->time_base
);
1366 frame_delay
+= decoded_frame
->repeat_pict
* (frame_delay
* 0.5);
1367 current_pts
+= std::chrono::duration_cast
<nanoseconds
>(seconds_d64
{frame_delay
});
1369 /* Put the frame in the queue to be loaded into a texture and displayed
1370 * by the rendering thread.
1372 write_idx
= (write_idx
+1)%mPictQ
.size();
1373 mPictQWrite
.store(write_idx
, std::memory_order_release
);
1375 /* Send a packet now so it's hopefully ready by the time it's needed. */
1376 mPackets
.sendTo(mCodecCtx
.get());
1378 if(write_idx
== mPictQRead
.load(std::memory_order_acquire
))
1380 /* Wait until we have space for a new pic */
1381 std::unique_lock
<std::mutex
> lock
{mPictQMutex
};
1382 while(write_idx
== mPictQRead
.load(std::memory_order_acquire
) &&
1383 !mMovie
.mQuit
.load(std::memory_order_relaxed
))
1384 mPictQCond
.wait(lock
);
1389 std::unique_lock
<std::mutex
> lock
{mPictQMutex
};
1390 while(!mFinalUpdate
) mPictQCond
.wait(lock
);
1396 int MovieState::decode_interrupt_cb(void *ctx
)
1398 return static_cast<MovieState
*>(ctx
)->mQuit
.load(std::memory_order_relaxed
);
1401 bool MovieState::prepare()
1403 AVIOContext
*avioctx
{nullptr};
1404 AVIOInterruptCB intcb
{decode_interrupt_cb
, this};
1405 if(avio_open2(&avioctx
, mFilename
.c_str(), AVIO_FLAG_READ
, &intcb
, nullptr))
1407 std::cerr
<< "Failed to open "<<mFilename
<<std::endl
;
1410 mIOContext
.reset(avioctx
);
1412 /* Open movie file. If avformat_open_input fails it will automatically free
1413 * this context, so don't set it onto a smart pointer yet.
1415 AVFormatContext
*fmtctx
{avformat_alloc_context()};
1416 fmtctx
->pb
= mIOContext
.get();
1417 fmtctx
->interrupt_callback
= intcb
;
1418 if(avformat_open_input(&fmtctx
, mFilename
.c_str(), nullptr, nullptr) != 0)
1420 std::cerr
<< "Failed to open "<<mFilename
<<std::endl
;
1423 mFormatCtx
.reset(fmtctx
);
1425 /* Retrieve stream information */
1426 if(avformat_find_stream_info(mFormatCtx
.get(), nullptr) < 0)
1428 std::cerr
<< mFilename
<<": failed to find stream info" <<std::endl
;
1432 mParseThread
= std::thread
{std::mem_fn(&MovieState::parse_handler
), this};
1436 void MovieState::setTitle(SDL_Window
*window
)
1438 auto pos1
= mFilename
.rfind('/');
1439 auto pos2
= mFilename
.rfind('\\');
1440 auto fpos
= ((pos1
== std::string::npos
) ? pos2
:
1441 (pos2
== std::string::npos
) ? pos1
:
1442 std::max(pos1
, pos2
)) + 1;
1443 SDL_SetWindowTitle(window
, (mFilename
.substr(fpos
)+" - "+AppName
).c_str());
1446 nanoseconds
MovieState::getClock()
1448 if(mClockBase
== microseconds::min())
1449 return nanoseconds::zero();
1450 return get_avtime() - mClockBase
;
1453 nanoseconds
MovieState::getMasterClock()
1455 if(mAVSyncType
== SyncMaster::Video
)
1456 return mVideo
.getClock();
1457 if(mAVSyncType
== SyncMaster::Audio
)
1458 return mAudio
.getClock();
1462 nanoseconds
MovieState::getDuration()
1463 { return std::chrono::duration
<int64_t,std::ratio
<1,AV_TIME_BASE
>>(mFormatCtx
->duration
); }
1465 int MovieState::streamComponentOpen(unsigned int stream_index
)
1467 if(stream_index
>= mFormatCtx
->nb_streams
)
1470 /* Get a pointer to the codec context for the stream, and open the
1473 AVCodecCtxPtr avctx
{avcodec_alloc_context3(nullptr)};
1474 if(!avctx
) return -1;
1476 if(avcodec_parameters_to_context(avctx
.get(), mFormatCtx
->streams
[stream_index
]->codecpar
))
1479 AVCodec
*codec
{avcodec_find_decoder(avctx
->codec_id
)};
1480 if(!codec
|| avcodec_open2(avctx
.get(), codec
, nullptr) < 0)
1482 std::cerr
<< "Unsupported codec: "<<avcodec_get_name(avctx
->codec_id
)
1483 << " (0x"<<std::hex
<<avctx
->codec_id
<<std::dec
<<")" <<std::endl
;
1487 /* Initialize and start the media type handler */
1488 switch(avctx
->codec_type
)
1490 case AVMEDIA_TYPE_AUDIO
:
1491 mAudio
.mStream
= mFormatCtx
->streams
[stream_index
];
1492 mAudio
.mCodecCtx
= std::move(avctx
);
1495 case AVMEDIA_TYPE_VIDEO
:
1496 mVideo
.mStream
= mFormatCtx
->streams
[stream_index
];
1497 mVideo
.mCodecCtx
= std::move(avctx
);
1504 return static_cast<int>(stream_index
);
1507 int MovieState::parse_handler()
1509 auto &audio_queue
= mAudio
.mPackets
;
1510 auto &video_queue
= mVideo
.mPackets
;
1512 int video_index
{-1};
1513 int audio_index
{-1};
1515 /* Dump information about file onto standard error */
1516 av_dump_format(mFormatCtx
.get(), 0, mFilename
.c_str(), 0);
1518 /* Find the first video and audio streams */
1519 for(unsigned int i
{0u};i
< mFormatCtx
->nb_streams
;i
++)
1521 auto codecpar
= mFormatCtx
->streams
[i
]->codecpar
;
1522 if(codecpar
->codec_type
== AVMEDIA_TYPE_VIDEO
&& !DisableVideo
&& video_index
< 0)
1523 video_index
= streamComponentOpen(i
);
1524 else if(codecpar
->codec_type
== AVMEDIA_TYPE_AUDIO
&& audio_index
< 0)
1525 audio_index
= streamComponentOpen(i
);
1528 if(video_index
< 0 && audio_index
< 0)
1530 std::cerr
<< mFilename
<<": could not open codecs" <<std::endl
;
1534 /* Set the base time 750ms ahead of the current av time. */
1535 mClockBase
= get_avtime() + milliseconds
{750};
1537 if(audio_index
>= 0)
1538 mAudioThread
= std::thread
{std::mem_fn(&AudioState::handler
), &mAudio
};
1539 if(video_index
>= 0)
1540 mVideoThread
= std::thread
{std::mem_fn(&VideoState::handler
), &mVideo
};
1542 /* Main packet reading/dispatching loop */
1543 while(!mQuit
.load(std::memory_order_relaxed
))
1546 if(av_read_frame(mFormatCtx
.get(), &packet
) < 0)
1549 /* Copy the packet into the queue it's meant for. */
1550 if(packet
.stream_index
== video_index
)
1552 while(!mQuit
.load(std::memory_order_acquire
) && !video_queue
.put(&packet
))
1553 std::this_thread::sleep_for(milliseconds
{100});
1555 else if(packet
.stream_index
== audio_index
)
1557 while(!mQuit
.load(std::memory_order_acquire
) && !audio_queue
.put(&packet
))
1558 std::this_thread::sleep_for(milliseconds
{100});
1561 av_packet_unref(&packet
);
1563 /* Finish the queues so the receivers know nothing more is coming. */
1564 if(mVideo
.mCodecCtx
) video_queue
.setFinished();
1565 if(mAudio
.mCodecCtx
) audio_queue
.setFinished();
1567 /* all done - wait for it */
1568 if(mVideoThread
.joinable())
1569 mVideoThread
.join();
1570 if(mAudioThread
.joinable())
1571 mAudioThread
.join();
1574 std::unique_lock
<std::mutex
> lock
{mVideo
.mPictQMutex
};
1575 while(!mVideo
.mFinalUpdate
)
1576 mVideo
.mPictQCond
.wait(lock
);
1580 evt
.user
.type
= FF_MOVIE_DONE_EVENT
;
1581 SDL_PushEvent(&evt
);
1587 // Helper class+method to print the time with human-readable formatting.
1591 std::ostream
&operator<<(std::ostream
&os
, const PrettyTime
&rhs
)
1593 using hours
= std::chrono::hours
;
1594 using minutes
= std::chrono::minutes
;
1595 using std::chrono::duration_cast
;
1597 seconds t
{rhs
.mTime
};
1604 // Only handle up to hour formatting
1606 os
<< duration_cast
<hours
>(t
).count() << 'h' << std::setfill('0') << std::setw(2)
1607 << (duration_cast
<minutes
>(t
).count() % 60) << 'm';
1609 os
<< duration_cast
<minutes
>(t
).count() << 'm' << std::setfill('0');
1610 os
<< std::setw(2) << (duration_cast
<seconds
>(t
).count() % 60) << 's' << std::setw(0)
1611 << std::setfill(' ');
1618 int main(int argc
, char *argv
[])
1620 std::unique_ptr
<MovieState
> movState
;
1624 std::cerr
<< "Usage: "<<argv
[0]<<" [-device <device name>] [-direct] <files...>" <<std::endl
;
1627 /* Register all formats and codecs */
1628 #if !(LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(58, 9, 100))
1631 /* Initialize networking protocols */
1632 avformat_network_init();
1634 if(SDL_Init(SDL_INIT_VIDEO
| SDL_INIT_EVENTS
))
1636 std::cerr
<< "Could not initialize SDL - <<"<<SDL_GetError() <<std::endl
;
1640 /* Make a window to put our video */
1641 SDL_Window
*screen
{SDL_CreateWindow(AppName
.c_str(), 0, 0, 640, 480, SDL_WINDOW_RESIZABLE
)};
1644 std::cerr
<< "SDL: could not set video mode - exiting" <<std::endl
;
1647 /* Make a renderer to handle the texture image surface and rendering. */
1648 Uint32 render_flags
{SDL_RENDERER_ACCELERATED
| SDL_RENDERER_PRESENTVSYNC
};
1649 SDL_Renderer
*renderer
{SDL_CreateRenderer(screen
, -1, render_flags
)};
1652 SDL_RendererInfo rinf
{};
1655 /* Make sure the renderer supports IYUV textures. If not, fallback to a
1656 * software renderer. */
1657 if(SDL_GetRendererInfo(renderer
, &rinf
) == 0)
1659 for(Uint32 i
{0u};!ok
&& i
< rinf
.num_texture_formats
;i
++)
1660 ok
= (rinf
.texture_formats
[i
] == SDL_PIXELFORMAT_IYUV
);
1664 std::cerr
<< "IYUV pixelformat textures not supported on renderer "<<rinf
.name
<<std::endl
;
1665 SDL_DestroyRenderer(renderer
);
1671 render_flags
= SDL_RENDERER_SOFTWARE
| SDL_RENDERER_PRESENTVSYNC
;
1672 renderer
= SDL_CreateRenderer(screen
, -1, render_flags
);
1676 std::cerr
<< "SDL: could not create renderer - exiting" <<std::endl
;
1679 SDL_SetRenderDrawColor(renderer
, 0, 0, 0, 255);
1680 SDL_RenderFillRect(renderer
, nullptr);
1681 SDL_RenderPresent(renderer
);
1683 /* Open an audio device */
1685 if(InitAL(&argv
, &argc
))
1687 std::cerr
<< "Failed to set up audio device" <<std::endl
;
1692 auto device
= alcGetContextsDevice(alcGetCurrentContext());
1693 if(alcIsExtensionPresent(device
, "ALC_SOFT_device_clock"))
1695 std::cout
<< "Found ALC_SOFT_device_clock" <<std::endl
;
1696 alcGetInteger64vSOFT
= reinterpret_cast<LPALCGETINTEGER64VSOFT
>(
1697 alcGetProcAddress(device
, "alcGetInteger64vSOFT")
1702 if(alIsExtensionPresent("AL_SOFT_source_latency"))
1704 std::cout
<< "Found AL_SOFT_source_latency" <<std::endl
;
1705 alGetSourcei64vSOFT
= reinterpret_cast<LPALGETSOURCEI64VSOFT
>(
1706 alGetProcAddress("alGetSourcei64vSOFT")
1709 #ifdef AL_SOFT_map_buffer
1710 if(alIsExtensionPresent("AL_SOFTX_map_buffer"))
1712 std::cout
<< "Found AL_SOFT_map_buffer" <<std::endl
;
1713 alBufferStorageSOFT
= reinterpret_cast<LPALBUFFERSTORAGESOFT
>(
1714 alGetProcAddress("alBufferStorageSOFT"));
1715 alMapBufferSOFT
= reinterpret_cast<LPALMAPBUFFERSOFT
>(
1716 alGetProcAddress("alMapBufferSOFT"));
1717 alUnmapBufferSOFT
= reinterpret_cast<LPALUNMAPBUFFERSOFT
>(
1718 alGetProcAddress("alUnmapBufferSOFT"));
1721 #ifdef AL_SOFT_events
1722 if(alIsExtensionPresent("AL_SOFTX_events"))
1724 std::cout
<< "Found AL_SOFT_events" <<std::endl
;
1725 alEventControlSOFT
= reinterpret_cast<LPALEVENTCONTROLSOFT
>(
1726 alGetProcAddress("alEventControlSOFT"));
1727 alEventCallbackSOFT
= reinterpret_cast<LPALEVENTCALLBACKSOFT
>(
1728 alGetProcAddress("alEventCallbackSOFT"));
1733 for(;fileidx
< argc
;++fileidx
)
1735 if(strcmp(argv
[fileidx
], "-direct") == 0)
1737 if(!alIsExtensionPresent("AL_SOFT_direct_channels"))
1738 std::cerr
<< "AL_SOFT_direct_channels not supported for direct output" <<std::endl
;
1741 std::cout
<< "Found AL_SOFT_direct_channels" <<std::endl
;
1742 EnableDirectOut
= true;
1745 else if(strcmp(argv
[fileidx
], "-wide") == 0)
1747 if(!alIsExtensionPresent("AL_EXT_STEREO_ANGLES"))
1748 std::cerr
<< "AL_EXT_STEREO_ANGLES not supported for wide stereo" <<std::endl
;
1751 std::cout
<< "Found AL_EXT_STEREO_ANGLES" <<std::endl
;
1752 EnableWideStereo
= true;
1755 else if(strcmp(argv
[fileidx
], "-novideo") == 0)
1756 DisableVideo
= true;
1761 while(fileidx
< argc
&& !movState
)
1763 movState
= std::unique_ptr
<MovieState
>{new MovieState
{argv
[fileidx
++]}};
1764 if(!movState
->prepare()) movState
= nullptr;
1768 std::cerr
<< "Could not start a video" <<std::endl
;
1771 movState
->setTitle(screen
);
1773 /* Default to going to the next movie at the end of one. */
1774 enum class EomAction
{
1776 } eom_action
{EomAction::Next
};
1777 seconds last_time
{seconds::min()};
1781 int have_evt
{SDL_WaitEventTimeout(&event
, 10)};
1783 auto cur_time
= std::chrono::duration_cast
<seconds
>(movState
->getMasterClock());
1784 if(cur_time
!= last_time
)
1786 auto end_time
= std::chrono::duration_cast
<seconds
>(movState
->getDuration());
1787 std::cout
<< " \r "<<PrettyTime
{cur_time
}<<" / "<<PrettyTime
{end_time
} <<std::flush
;
1788 last_time
= cur_time
;
1791 bool force_redraw
{false};
1796 switch(event
.key
.keysym
.sym
)
1799 movState
->mQuit
= true;
1800 eom_action
= EomAction::Quit
;
1804 movState
->mQuit
= true;
1805 eom_action
= EomAction::Next
;
1813 case SDL_WINDOWEVENT
:
1814 switch(event
.window
.event
)
1816 case SDL_WINDOWEVENT_RESIZED
:
1817 SDL_SetRenderDrawColor(renderer
, 0, 0, 0, 255);
1818 SDL_RenderFillRect(renderer
, nullptr);
1819 force_redraw
= true;
1822 case SDL_WINDOWEVENT_EXPOSED
:
1823 force_redraw
= true;
1832 movState
->mQuit
= true;
1833 eom_action
= EomAction::Quit
;
1836 case FF_MOVIE_DONE_EVENT
:
1838 last_time
= seconds::min();
1839 if(eom_action
!= EomAction::Quit
)
1842 while(fileidx
< argc
&& !movState
)
1844 movState
= std::unique_ptr
<MovieState
>{new MovieState
{argv
[fileidx
++]}};
1845 if(!movState
->prepare()) movState
= nullptr;
1849 movState
->setTitle(screen
);
1854 /* Nothing more to play. Shut everything down and quit. */
1859 SDL_DestroyRenderer(renderer
);
1861 SDL_DestroyWindow(screen
);
1870 } while(SDL_PollEvent(&event
));
1872 movState
->mVideo
.updateVideo(screen
, renderer
, force_redraw
);
1875 std::cerr
<< "SDL_WaitEvent error - "<<SDL_GetError() <<std::endl
;