Combine two function calls into one
[openal-soft.git] / examples / alffplay.cpp
blob655ffc963b47ec1a5208be6e01224202c7850563
1 /*
2 * An example showing how to play a stream sync'd to video, using ffmpeg.
4 * Requires C++11.
5 */
7 #include <condition_variable>
8 #include <functional>
9 #include <algorithm>
10 #include <iostream>
11 #include <utility>
12 #include <iomanip>
13 #include <cstdint>
14 #include <cstring>
15 #include <cstdlib>
16 #include <atomic>
17 #include <cerrno>
18 #include <chrono>
19 #include <cstdio>
20 #include <memory>
21 #include <string>
22 #include <thread>
23 #include <vector>
24 #include <array>
25 #include <cmath>
26 #include <deque>
27 #include <mutex>
28 #include <ratio>
30 extern "C" {
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"
48 struct SwsContext;
51 #include "SDL.h"
53 #include "AL/alc.h"
54 #include "AL/al.h"
55 #include "AL/alext.h"
57 #include "common/alhelpers.h"
59 extern "C" {
60 /* Undefine this to disable use of experimental extensions. Don't use for
61 * production code! Interfaces and behavior may change prior to being
62 * finalized.
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);
78 #endif
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,
92 void *userParam);
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);
97 #endif
98 #endif /* ALLOW_EXPERIMENTAL_EXTS */
101 namespace {
103 inline constexpr int64_t operator "" _i64(unsigned long long int n) noexcept { return static_cast<int64_t>(n); }
105 #ifndef M_PI
106 #define M_PI (3.14159265358979323846)
107 #endif
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;
128 #endif
130 #ifdef AL_SOFT_events
131 LPALEVENTCONTROLSOFT alEventControlSOFT;
132 LPALEVENTCALLBACKSOFT alEventCallbackSOFT;
133 #endif
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};
150 enum {
151 FF_MOVIE_DONE_EVENT = SDL_USEREVENT
154 enum class SyncMaster {
155 Audio,
156 Video,
157 External,
159 Default = External
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>
199 class PacketQueue {
200 std::mutex mMutex;
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)
209 mCondVar.wait(lock);
210 return mPackets.empty() ? nullptr : &mPackets.front();
213 void pop()
215 AVPacket *pkt = &mPackets.front();
216 mTotalSize -= static_cast<unsigned int>(pkt->size);
217 av_packet_unref(pkt);
218 mPackets.pop_front();
221 public:
222 ~PacketQueue()
224 for(AVPacket &pkt : mPackets)
225 av_packet_unref(&pkt);
226 mPackets.clear();
227 mTotalSize = 0;
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))
240 if(ret < 0)
241 std::cerr<< "Failed to send packet: "<<ret <<std::endl;
242 pop();
244 return ret;
247 void setFinished()
250 std::lock_guard<std::mutex> _{mMutex};
251 mFinished = true;
253 mCondVar.notify_one();
256 bool put(const AVPacket *pkt)
259 std::unique_lock<std::mutex> lock{mMutex};
260 if(mTotalSize >= SizeLimit)
261 return false;
263 mPackets.push_back(AVPacket{});
264 if(av_packet_ref(&mPackets.back(), pkt) != 0)
266 mPackets.pop_back();
267 return true;
270 mTotalSize += static_cast<unsigned int>(mPackets.back().size);
272 mCondVar.notify_one();
273 return true;
278 struct MovieState;
280 struct AudioState {
281 MovieState &mMovie;
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 */
308 int mSamplesPos{0};
309 int mSamplesMax{0};
311 /* OpenAL format */
312 ALenum mFormat{AL_NONE};
313 ALuint mFrameSize{0};
315 std::mutex mSrcMutex;
316 std::condition_variable mSrcCond;
317 std::atomic_flag mConnected;
318 ALuint mSource{0};
319 std::vector<ALuint> mBuffers;
320 ALuint mBufferIdx{0};
322 AudioState(MovieState &movie) : mMovie(movie)
323 { mConnected.test_and_set(std::memory_order_relaxed); }
324 ~AudioState()
326 if(mSource)
327 alDeleteSources(1, &mSource);
328 if(!mBuffers.empty())
329 alDeleteBuffers(static_cast<ALsizei>(mBuffers.size()), mBuffers.data());
331 av_freep(&mSamples);
334 #ifdef AL_SOFT_events
335 static void AL_APIENTRY EventCallback(ALenum eventType, ALuint object, ALuint param,
336 ALsizei length, const ALchar *message,
337 void *userParam);
338 #endif
340 nanoseconds getClockNoLock();
341 nanoseconds getClock()
343 std::lock_guard<std::mutex> lock{mSrcMutex};
344 return getClockNoLock();
347 void startPlayback();
349 int getSync();
350 int decodeFrame();
351 bool readAudio(uint8_t *samples, unsigned int length);
353 int handler();
356 struct VideoState {
357 MovieState &mMovie;
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;
374 struct Picture {
375 AVFramePtr mFrame{};
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) { }
391 ~VideoState()
393 if(mImage)
394 SDL_DestroyTexture(mImage);
395 mImage = nullptr;
398 nanoseconds getClock();
400 void display(SDL_Window *screen, SDL_Renderer *renderer);
401 void updateVideo(SDL_Window *screen, SDL_Renderer *renderer, bool redraw);
402 int handler();
405 struct MovieState {
406 AVIOContextPtr mIOContext;
407 AVFormatCtxPtr mFormatCtx;
409 SyncMaster mAVSyncType{SyncMaster::Default};
411 microseconds mClockBase{microseconds::min()};
413 std::atomic<bool> mQuit{false};
415 AudioState mAudio;
416 VideoState mVideo;
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))
427 ~MovieState()
429 mQuit = true;
430 if(mParseThread.joinable())
431 mParseThread.join();
434 static int decode_interrupt_cb(void *ctx);
435 bool prepare();
436 void setTitle(SDL_Window *window);
438 nanoseconds getClock();
440 nanoseconds getMasterClock();
442 nanoseconds getDuration();
444 int streamComponentOpen(unsigned int stream_index);
445 int parse_handler();
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};
487 if(mSource)
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);
496 else
498 ALint ioffset;
499 alGetSourcei(mSource, AL_SAMPLE_OFFSET, &ioffset);
500 offset[0] = ALint64SOFT{ioffset} << 32;
501 offset[1] = 0;
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
543 // is now.
544 mDeviceStartTime = device_time - src_offset - startpts;
548 int AudioState::getSync()
550 if(mMovie.mAVSyncType == SyncMaster::Audio)
551 return 0;
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();
560 return 0;
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)
567 return 0;
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))
578 int ret;
579 while((ret=avcodec_receive_frame(mCodecCtx.get(), mDecodedFrame.get())) == AVERROR(EAGAIN))
580 mPackets.sendTo(mCodecCtx.get());
581 if(ret != 0)
583 if(ret == AVERROR_EOF) break;
584 std::cerr<< "Failed to receive frame: "<<ret <<std::endl;
585 continue;
588 if(mDecodedFrame->nb_samples <= 0)
589 continue;
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)
599 av_freep(&mSamples);
600 av_samples_alloc(
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());
611 return data_size;
614 return 0;
617 /* Duplicates the sample at in to out, count times. The frame size is a
618 * multiple of the template type size.
620 template<typename T>
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);
627 else
629 /* NOTE: frame_size is a multiple of sizeof(T). */
630 size_t type_mult{frame_size / sizeof(T)};
631 size_t i{0};
632 std::generate_n(dst, count*type_mult,
633 [sample,type_mult,&i]() -> T
635 T ret = sample[i];
636 i = (i+1)%type_mult;
637 return ret;
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
650 * on the source */
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;
668 mCurrentPts += skip;
669 continue;
672 unsigned int rem{length - audio_size};
673 if(mSamplesPos >= 0)
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);
680 else
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);
691 else
692 sample_dup<uint8_t>(samples, mSamples, rem, mFrameSize);
695 mSamplesPos += rem;
696 mCurrentPts += nanoseconds{seconds{rem}} / mCodecCtx->sample_rate;
697 samples += rem*mFrameSize;
698 audio_size += rem;
700 if(audio_size <= 0)
701 return false;
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;
709 audio_size += rem;
711 return true;
715 #ifdef AL_SOFT_events
716 void AL_APIENTRY AudioState::EventCallback(ALenum eventType, ALuint object, ALuint param,
717 ALsizei length, const ALchar *message,
718 void *userParam)
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();
729 return;
732 std::cout<< "\n---- AL Event on AudioState "<<self<<" ----\nEvent: ";
733 switch(eventType)
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;
744 std::cout<< "\n"
745 "Object ID: "<<object<<"\n"
746 "Parameter: "<<param<<"\n"
747 "Message: "<<std::string{message, static_cast<ALuint>(length)}<<"\n----"<<
748 std::endl;
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();
759 #endif
761 int AudioState::handler()
763 std::unique_lock<std::mutex> srclock{mSrcMutex, std::defer_lock};
764 milliseconds sleep_time{AudioBufferTime / 3};
765 ALenum fmt;
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;
779 #endif
781 /* Find a suitable format for OpenAL. */
782 mDstChanLayout = 0;
783 mFormat = AL_NONE;
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;
788 mFrameSize = 4;
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;
794 mFrameSize *= 8;
795 mFormat = fmt;
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;
803 mFrameSize *= 6;
804 mFormat = fmt;
806 if(mCodecCtx->channel_layout == AV_CH_LAYOUT_MONO)
808 mDstChanLayout = mCodecCtx->channel_layout;
809 mFrameSize *= 1;
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
815 * if it's 2D or 3D).
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.
828 mFrameSize *= 4;
829 mFormat = fmt;
832 if(!mFormat)
834 mDstChanLayout = AV_CH_LAYOUT_STEREO;
835 mFrameSize *= 2;
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;
842 mFrameSize = 1;
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;
848 mFrameSize *= 8;
849 mFormat = fmt;
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;
857 mFrameSize *= 6;
858 mFormat = fmt;
860 if(mCodecCtx->channel_layout == AV_CH_LAYOUT_MONO)
862 mDstChanLayout = mCodecCtx->channel_layout;
863 mFrameSize *= 1;
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)
874 mFrameSize *= 4;
875 mFormat = fmt;
878 if(!mFormat)
880 mDstChanLayout = AV_CH_LAYOUT_STEREO;
881 mFrameSize *= 2;
882 mFormat = AL_FORMAT_STEREO8;
885 if(!mFormat)
887 mDstSampleFmt = AV_SAMPLE_FMT_S16;
888 mFrameSize = 2;
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;
894 mFrameSize *= 8;
895 mFormat = fmt;
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;
903 mFrameSize *= 6;
904 mFormat = fmt;
906 if(mCodecCtx->channel_layout == AV_CH_LAYOUT_MONO)
908 mDstChanLayout = mCodecCtx->channel_layout;
909 mFrameSize *= 1;
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)
920 mFrameSize *= 4;
921 mFormat = fmt;
924 if(!mFormat)
926 mDstChanLayout = AV_CH_LAYOUT_STEREO;
927 mFrameSize *= 2;
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);
935 mSamples = nullptr;
936 mSamplesMax = 0;
937 mSamplesPos = 0;
938 mSamplesLen = 0;
940 mDecodedFrame.reset(av_frame_alloc());
941 if(!mDecodedFrame)
943 std::cerr<< "Failed to allocate audio frame" <<std::endl;
944 goto finish;
947 if(!mDstChanLayout)
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,
957 0, nullptr));
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);
965 mtx[3 + 1*64] = 1.0;
966 mtx[1 + 2*64] = 1.0;
967 mtx[2 + 3*64] = 1.0;
968 swr_set_matrix(mSwresCtx.get(), mtx.data(), 64);
970 else
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,
976 0, nullptr));
977 if(!mSwresCtx || swr_init(mSwresCtx.get()) != 0)
979 std::cerr<< "Failed to initialize audio converter" <<std::endl;
980 goto finish;
983 mBuffers.assign(AudioBufferTotalTime / AudioBufferTime, 0);
984 alGenBuffers(static_cast<ALsizei>(mBuffers.size()), mBuffers.data());
985 alGenSources(1, &mSource);
987 if(EnableDirectOut)
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)
996 goto finish;
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));
1010 else
1011 #endif
1012 samples = av_malloc(static_cast<ALuint>(buffer_len));
1014 /* Prefill the codec buffer. */
1015 do {
1016 const int ret{mPackets.sendTo(mCodecCtx.get())};
1017 if(ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
1018 break;
1019 } while(1);
1021 srclock.lock();
1022 if(alcGetInteger64vSOFT)
1024 int64_t devtime{};
1025 alcGetInteger64vSOFT(alcGetContextsDevice(alcGetCurrentContext()), ALC_DEVICE_CLOCK_SOFT,
1026 1, &devtime);
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. */
1033 ALint processed;
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());
1040 processed -= todq;
1043 /* Refill the buffer queue. */
1044 ALint queued;
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
1050 * the source.
1052 #ifdef AL_SOFT_map_buffer
1053 if(!samples)
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;
1061 else
1062 #endif
1064 auto ptr = static_cast<uint8_t*>(samples);
1065 if(!readAudio(ptr, static_cast<unsigned int>(buffer_len)))
1066 break;
1067 alBufferData(bufid, mFormat, samples, buffer_len, mCodecCtx->sample_rate);
1070 alSourceQueueBuffers(mSource, 1, &bufid);
1071 mBufferIdx = (mBufferIdx+1) % mBuffers.size();
1072 ++queued;
1074 if(queued == 0)
1075 break;
1077 /* Check that the source is playing. */
1078 ALint state;
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.
1093 int64_t devtime{};
1094 alcGetInteger64vSOFT(alcGetContextsDevice(alcGetCurrentContext()),
1095 ALC_DEVICE_CLOCK_SOFT, 1, &devtime);
1096 mDeviceStartTime = nanoseconds{devtime} - mCurrentPts;
1098 continue;
1101 /* (re)start the source if needed, and wait for a buffer to finish */
1102 if(state != AL_PLAYING && state != AL_PAUSED)
1103 startPlayback();
1105 mSrcCond.wait_for(srclock, sleep_time);
1108 alSourceRewind(mSource);
1109 alSourcei(mSource, AL_BUFFER, 0);
1110 srclock.unlock();
1112 finish:
1113 av_freep(&samples);
1115 #ifdef AL_SOFT_events
1116 if(alEventControlSOFT)
1118 alEventControlSOFT(evt_types.size(), evt_types.data(), AL_FALSE);
1119 alEventCallbackSOFT(nullptr, nullptr);
1121 #endif
1123 return 0;
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)
1140 if(!mImage)
1141 return;
1143 double aspect_ratio;
1144 int win_w, win_h;
1145 int w, h, x, y;
1147 if(mCodecCtx->sample_aspect_ratio.num == 0)
1148 aspect_ratio = 0.0;
1149 else
1151 aspect_ratio = av_q2d(mCodecCtx->sample_aspect_ratio) * mCodecCtx->width /
1152 mCodecCtx->height;
1154 if(aspect_ratio <= 0.0)
1155 aspect_ratio = static_cast<double>(mCodecCtx->width) / mCodecCtx->height;
1157 SDL_GetWindowSize(screen, &win_w, &win_h);
1158 h = win_h;
1159 w = (static_cast<int>(std::rint(h * aspect_ratio)) + 3) & ~3;
1160 if(w > win_w)
1162 w = win_w;
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
1176 * frame.
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};
1185 while(1)
1187 size_t next_idx{(read_idx+1)%mPictQ.size()};
1188 if(next_idx == mPictQWrite.load(std::memory_order_acquire))
1189 break;
1190 Picture *nextvp{&mPictQ[next_idx]};
1191 if(clocktime < nextvp->mPts)
1192 break;
1194 vp = nextvp;
1195 updated = true;
1196 read_idx = next_idx;
1198 if(mMovie.mQuit.load(std::memory_order_relaxed))
1200 if(mEOS)
1201 mFinalUpdate = true;
1202 mPictQRead.store(read_idx, std::memory_order_release);
1203 std::unique_lock<std::mutex>{mPictQMutex}.unlock();
1204 mPictQCond.notify_one();
1205 return;
1208 if(updated)
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)
1218 fmt_updated = true;
1219 if(mImage)
1220 SDL_DestroyTexture(mImage);
1221 mImage = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_IYUV, SDL_TEXTUREACCESS_STREAMING,
1222 mCodecCtx->coded_width, mCodecCtx->coded_height);
1223 if(!mImage)
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;
1233 int w{mWidth};
1234 int h{mHeight};
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);
1247 if(mImage)
1249 AVFrame *frame{vp->mFrame.get()};
1250 void *pixels{nullptr};
1251 int pitch{0};
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;
1261 else
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);
1294 redraw = true;
1297 if(redraw)
1299 /* Show the picture! */
1300 display(screen, renderer);
1303 if(updated)
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. */
1329 do {
1330 const int ret{mPackets.sendTo(mCodecCtx.get())};
1331 if(ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
1332 break;
1333 } while(1);
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()};
1348 int ret;
1349 while((ret=avcodec_receive_frame(mCodecCtx.get(), decoded_frame)) == AVERROR(EAGAIN))
1350 mPackets.sendTo(mCodecCtx.get());
1351 if(ret != 0)
1353 if(ret == AVERROR_EOF) break;
1354 std::cerr<< "Failed to receive frame: "<<ret <<std::endl;
1355 continue;
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);
1387 mEOS = true;
1389 std::unique_lock<std::mutex> lock{mPictQMutex};
1390 while(!mFinalUpdate) mPictQCond.wait(lock);
1392 return 0;
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;
1408 return false;
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;
1421 return false;
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;
1429 return false;
1432 mParseThread = std::thread{std::mem_fn(&MovieState::parse_handler), this};
1433 return true;
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();
1459 return 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)
1468 return -1;
1470 /* Get a pointer to the codec context for the stream, and open the
1471 * associated codec.
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))
1477 return -1;
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;
1484 return -1;
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);
1493 break;
1495 case AVMEDIA_TYPE_VIDEO:
1496 mVideo.mStream = mFormatCtx->streams[stream_index];
1497 mVideo.mCodecCtx = std::move(avctx);
1498 break;
1500 default:
1501 return -1;
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;
1531 mQuit = true;
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))
1545 AVPacket packet;
1546 if(av_read_frame(mFormatCtx.get(), &packet) < 0)
1547 break;
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();
1573 mVideo.mEOS = true;
1574 std::unique_lock<std::mutex> lock{mVideo.mPictQMutex};
1575 while(!mVideo.mFinalUpdate)
1576 mVideo.mPictQCond.wait(lock);
1577 lock.unlock();
1579 SDL_Event evt{};
1580 evt.user.type = FF_MOVIE_DONE_EVENT;
1581 SDL_PushEvent(&evt);
1583 return 0;
1587 // Helper class+method to print the time with human-readable formatting.
1588 struct PrettyTime {
1589 seconds mTime;
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};
1598 if(t.count() < 0)
1600 os << '-';
1601 t *= -1;
1604 // Only handle up to hour formatting
1605 if(t >= hours{1})
1606 os << duration_cast<hours>(t).count() << 'h' << std::setfill('0') << std::setw(2)
1607 << (duration_cast<minutes>(t).count() % 60) << 'm';
1608 else
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(' ');
1612 return os;
1615 } // namespace
1618 int main(int argc, char *argv[])
1620 std::unique_ptr<MovieState> movState;
1622 if(argc < 2)
1624 std::cerr<< "Usage: "<<argv[0]<<" [-device <device name>] [-direct] <files...>" <<std::endl;
1625 return 1;
1627 /* Register all formats and codecs */
1628 #if !(LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(58, 9, 100))
1629 av_register_all();
1630 #endif
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;
1637 return 1;
1640 /* Make a window to put our video */
1641 SDL_Window *screen{SDL_CreateWindow(AppName.c_str(), 0, 0, 640, 480, SDL_WINDOW_RESIZABLE)};
1642 if(!screen)
1644 std::cerr<< "SDL: could not set video mode - exiting" <<std::endl;
1645 return 1;
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)};
1650 if(renderer)
1652 SDL_RendererInfo rinf{};
1653 bool ok{false};
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);
1662 if(!ok)
1664 std::cerr<< "IYUV pixelformat textures not supported on renderer "<<rinf.name <<std::endl;
1665 SDL_DestroyRenderer(renderer);
1666 renderer = nullptr;
1669 if(!renderer)
1671 render_flags = SDL_RENDERER_SOFTWARE | SDL_RENDERER_PRESENTVSYNC;
1672 renderer = SDL_CreateRenderer(screen, -1, render_flags);
1674 if(!renderer)
1676 std::cerr<< "SDL: could not create renderer - exiting" <<std::endl;
1677 return 1;
1679 SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
1680 SDL_RenderFillRect(renderer, nullptr);
1681 SDL_RenderPresent(renderer);
1683 /* Open an audio device */
1684 ++argv; --argc;
1685 if(InitAL(&argv, &argc))
1687 std::cerr<< "Failed to set up audio device" <<std::endl;
1688 return 1;
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"));
1720 #endif
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"));
1730 #endif
1732 int fileidx{0};
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;
1739 else
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;
1749 else
1751 std::cout<< "Found AL_EXT_STEREO_ANGLES" <<std::endl;
1752 EnableWideStereo = true;
1755 else if(strcmp(argv[fileidx], "-novideo") == 0)
1756 DisableVideo = true;
1757 else
1758 break;
1761 while(fileidx < argc && !movState)
1763 movState = std::unique_ptr<MovieState>{new MovieState{argv[fileidx++]}};
1764 if(!movState->prepare()) movState = nullptr;
1766 if(!movState)
1768 std::cerr<< "Could not start a video" <<std::endl;
1769 return 1;
1771 movState->setTitle(screen);
1773 /* Default to going to the next movie at the end of one. */
1774 enum class EomAction {
1775 Next, Quit
1776 } eom_action{EomAction::Next};
1777 seconds last_time{seconds::min()};
1778 while(1)
1780 SDL_Event event{};
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};
1792 if(have_evt) do {
1793 switch(event.type)
1795 case SDL_KEYDOWN:
1796 switch(event.key.keysym.sym)
1798 case SDLK_ESCAPE:
1799 movState->mQuit = true;
1800 eom_action = EomAction::Quit;
1801 break;
1803 case SDLK_n:
1804 movState->mQuit = true;
1805 eom_action = EomAction::Next;
1806 break;
1808 default:
1809 break;
1811 break;
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;
1820 break;
1822 case SDL_WINDOWEVENT_EXPOSED:
1823 force_redraw = true;
1824 break;
1826 default:
1827 break;
1829 break;
1831 case SDL_QUIT:
1832 movState->mQuit = true;
1833 eom_action = EomAction::Quit;
1834 break;
1836 case FF_MOVIE_DONE_EVENT:
1837 std::cout<<'\n';
1838 last_time = seconds::min();
1839 if(eom_action != EomAction::Quit)
1841 movState = nullptr;
1842 while(fileidx < argc && !movState)
1844 movState = std::unique_ptr<MovieState>{new MovieState{argv[fileidx++]}};
1845 if(!movState->prepare()) movState = nullptr;
1847 if(movState)
1849 movState->setTitle(screen);
1850 break;
1854 /* Nothing more to play. Shut everything down and quit. */
1855 movState = nullptr;
1857 CloseAL();
1859 SDL_DestroyRenderer(renderer);
1860 renderer = nullptr;
1861 SDL_DestroyWindow(screen);
1862 screen = nullptr;
1864 SDL_Quit();
1865 exit(0);
1867 default:
1868 break;
1870 } while(SDL_PollEvent(&event));
1872 movState->mVideo.updateVideo(screen, renderer, force_redraw);
1875 std::cerr<< "SDL_WaitEvent error - "<<SDL_GetError() <<std::endl;
1876 return 1;