| 1 |
/* |
| 2 |
Copyright (c) 2012 Stephen Baker |
| 3 |
|
| 4 |
Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 5 |
this software and associated documentation files (the "Software"), to deal in |
| 6 |
the Software without restriction, including without limitation the rights to |
| 7 |
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
| 8 |
of the Software, and to permit persons to whom the Software is furnished to do |
| 9 |
so, subject to the following conditions: |
| 10 |
|
| 11 |
The above copyright notice and this permission notice shall be included in all |
| 12 |
copies or substantial portions of the Software. |
| 13 |
|
| 14 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 |
SOFTWARE. |
| 21 |
*/ |
| 22 |
|
| 23 |
#ifndef TH_VIDEO_H |
| 24 |
#define TH_VIDEO_H |
| 25 |
|
| 26 |
#include "config.h" |
| 27 |
|
| 28 |
#include <SDL_rect.h> |
| 29 |
#include <SDL_render.h> |
| 30 |
|
| 31 |
#include <array> |
| 32 |
#include <atomic> |
| 33 |
#include <condition_variable> |
| 34 |
#include <memory> |
| 35 |
#include <mutex> |
| 36 |
#include <queue> |
| 37 |
#include <string> |
| 38 |
#include <thread> |
| 39 |
|
| 40 |
#ifdef CORSIX_TH_USE_FFMPEG |
| 41 |
#include <SDL_mixer.h> |
| 42 |
|
| 43 |
extern "C" { |
| 44 |
#ifndef INT64_C |
| 45 |
#define INT64_C(c) (c##LL) |
| 46 |
#define UINT64_C(c) (c##ULL) |
| 47 |
#endif |
| 48 |
#include <libavcodec/avcodec.h> |
| 49 |
// IWYU pragma: no_include <libavcodec/codec.h> |
| 50 |
// IWYU pragma: no_include <libavcodec/packet.h> |
| 51 |
// IWYU pragma: no_include <libavcodec/version.h> |
| 52 |
#include <libavformat/avformat.h> |
| 53 |
#include <libavutil/avutil.h> // IWYU pragma: keep |
| 54 |
// IWYU pragma: no_include <libavutil/channel_layout.h> |
| 55 |
// IWYU pragma: no_include <libavutil/frame.h> |
| 56 |
// IWYU pragma: no_include <libavutil/mem.h> |
| 57 |
// IWYU pragma: no_include <libavutil/pixfmt.h> |
| 58 |
// IWYU pragma: no_include <libavutil/version.h> |
| 59 |
#include <libswresample/swresample.h> |
| 60 |
} |
| 61 |
|
| 62 |
struct SwsContext; |
| 63 |
|
| 64 |
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(59, 0, 100) |
| 65 |
using av_codec_ptr = AVCodec*; |
| 66 |
#else |
| 67 |
using av_codec_ptr = const AVCodec*; |
| 68 |
#endif |
| 69 |
|
| 70 |
//! \brief Functor for deleting AVPackets |
| 71 |
//! |
| 72 |
//! Deletes AVPacket pointers that are allocated with av_malloc |
| 73 |
class av_packet_deleter { |
| 74 |
public: |
| 75 |
void operator()(AVPacket* p) { |
| 76 |
av_packet_unref(p); |
| 77 |
av_free(p); |
| 78 |
} |
| 79 |
}; |
| 80 |
|
| 81 |
//! \brief unique_ptr for AVPackets |
| 82 |
using av_packet_unique_ptr = std::unique_ptr<AVPacket, av_packet_deleter>; |
| 83 |
|
| 84 |
class av_frame_deleter { |
| 85 |
public: |
| 86 |
void operator()(AVFrame* f) { av_frame_free(&f); } |
| 87 |
}; |
| 88 |
|
| 89 |
using av_frame_unique_ptr = std::unique_ptr<AVFrame, av_frame_deleter>; |
| 90 |
|
| 91 |
class av_codec_context_deleter { |
| 92 |
public: |
| 93 |
void operator()(AVCodecContext* c) { avcodec_free_context(&c); } |
| 94 |
}; |
| 95 |
|
| 96 |
using av_codec_context_unique_ptr = |
| 97 |
std::unique_ptr<AVCodecContext, av_codec_context_deleter>; |
| 98 |
|
| 99 |
//! \brief Functor for deleting Mix_Chunks |
| 100 |
//! |
| 101 |
//! Should be moved to a common mixer header when uses of Mix_Chunk outside of |
| 102 |
//! movies are converted to unique_ptr. |
| 103 |
class mix_chunk_deleter { |
| 104 |
public: |
| 105 |
void operator()(Mix_Chunk* c) { Mix_FreeChunk(c); } |
| 106 |
}; |
| 107 |
|
| 108 |
using mix_chunk_unique_ptr = std::unique_ptr<Mix_Chunk, mix_chunk_deleter>; |
| 109 |
|
| 110 |
#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 24, 100) |
| 111 |
//! \brief Functor for deleting AVChannelLayout |
| 112 |
class av_channel_layout_deleter { |
| 113 |
public: |
| 114 |
void operator()(AVChannelLayout* c) { |
| 115 |
av_channel_layout_uninit(c); |
| 116 |
delete c; |
| 117 |
} |
| 118 |
}; |
| 119 |
|
| 120 |
//! \brief unique_ptr for AVChannelLayout |
| 121 |
using av_channel_layout_unique_ptr = |
| 122 |
std::unique_ptr<AVChannelLayout, av_channel_layout_deleter>; |
| 123 |
#endif |
| 124 |
|
| 125 |
//! \brief A picture in movie_picture_buffer |
| 126 |
//! |
| 127 |
//! Stores the picture from a frame in the movie from the time that it is |
| 128 |
//! processed until it should be drawn. |
| 129 |
class movie_picture { |
| 130 |
public: |
| 131 |
movie_picture(); |
| 132 |
~movie_picture(); |
| 133 |
|
| 134 |
//! Allocate the buffer to hold a picture of the given size |
| 135 |
void allocate(int iWidth, int iHeight); |
| 136 |
|
| 137 |
//! Delete the buffer |
| 138 |
void deallocate(); |
| 139 |
|
| 140 |
uint8_t* buffer; ///< Pixel data in #m_pixelFormat |
| 141 |
const AVPixelFormat pixel_format; ///< The format of pixels to output |
| 142 |
int width; ///< Picture width |
| 143 |
int height; ///< Picture height |
| 144 |
double pts; ///< Presentation time stamp |
| 145 |
std::mutex mutex; ///< Mutex protecting this picture |
| 146 |
}; |
| 147 |
|
| 148 |
//! A buffer for holding movie pictures and drawing them to the renderer |
| 149 |
class movie_picture_buffer { |
| 150 |
public: |
| 151 |
movie_picture_buffer(); |
| 152 |
~movie_picture_buffer(); |
| 153 |
|
| 154 |
// NB: The following functions are called by the main program thread |
| 155 |
|
| 156 |
//! Indicate that processing should stop and the movie aborted |
| 157 |
void abort(); |
| 158 |
|
| 159 |
//! Resume after having aborted |
| 160 |
void reset(); |
| 161 |
|
| 162 |
//! Ready the picture buffer for a new renderer or new picture dimensions |
| 163 |
//! by allocating each movie_picture in the queue, resetting the read |
| 164 |
//! index and allocating a new texture. |
| 165 |
//! |
| 166 |
//! \remark Must be run on the program's graphics thread |
| 167 |
void allocate(SDL_Renderer* pRenderer, int iWidth, int iHeight); |
| 168 |
|
| 169 |
//! Destroy the associated texture and deallocate each of the |
| 170 |
//! movie_pictures in the queue so that the program can release |
| 171 |
//! the renderer |
| 172 |
//! |
| 173 |
//! \remark Must be run on the program's graphics thread |
| 174 |
void deallocate(); |
| 175 |
|
| 176 |
//! Advance the read index |
| 177 |
bool advance(); |
| 178 |
|
| 179 |
//! Draw the movie_picture at the current read index |
| 180 |
//! |
| 181 |
//! \param pRenderer The renderer to draw the picture to |
| 182 |
//! \param dstrect The rectangle on the renderer to draw to |
| 183 |
//! |
| 184 |
//! \remark Must be run on the program's graphics thread |
| 185 |
void draw(SDL_Renderer* pRenderer, const SDL_Rect& dstrect); |
| 186 |
|
| 187 |
//! Get the next presentation time stamp |
| 188 |
double get_next_pts(); |
| 189 |
|
| 190 |
//! Return whether there are any pictures left to draw in the picture queue |
| 191 |
//! |
| 192 |
//! \remark If the movie_picture_buffer is not allocated it cannot be read |
| 193 |
//! from or written to. Consequently it is both full and empty. |
| 194 |
bool empty(); |
| 195 |
|
| 196 |
// NB: These functions are called by a second thread |
| 197 |
|
| 198 |
//! Return whether there is space to add any more frame data to the queue |
| 199 |
//! |
| 200 |
//! \remark If the movie_picture_buffer is not allocated it cannot be read |
| 201 |
//! from or written to. Consequently it is both full and empty. |
| 202 |
bool full(); |
| 203 |
|
| 204 |
//! Write the given frame (and presentation time stamp) to the picture |
| 205 |
//! queue |
| 206 |
//! |
| 207 |
//! \retval 0 Success |
| 208 |
//! \retval -1 Abort is in progress |
| 209 |
//! \retval 1 An error writing the frame |
| 210 |
int write(AVFrame* pFrame, double dPts); |
| 211 |
|
| 212 |
private: |
| 213 |
//! Return whether there is space to add any more frame data to the queue |
| 214 |
//! |
| 215 |
//! \remark Requires external locking |
| 216 |
bool unsafe_full() const; |
| 217 |
|
| 218 |
//! The number of elements to allocate in the picture queue |
| 219 |
static constexpr std::size_t picture_buffer_size = 4; |
| 220 |
|
| 221 |
//! Whether we are in the process of aborting |
| 222 |
std::atomic<bool> aborting; |
| 223 |
|
| 224 |
//! Whether the picture buffer is currently allocated |
| 225 |
bool allocated; |
| 226 |
|
| 227 |
//! The number of elements currently written to the picture queue |
| 228 |
int picture_count; |
| 229 |
|
| 230 |
//! The position in the picture queue to be read next |
| 231 |
std::size_t read_index; |
| 232 |
|
| 233 |
//! The position in the picture queue to be written to next |
| 234 |
std::size_t write_index; |
| 235 |
|
| 236 |
//! The context for software scaling and pixel conversion when writing to the |
| 237 |
//! picture queue |
| 238 |
SwsContext* sws_context; |
| 239 |
|
| 240 |
//! The (potentially hardware) texture to draw the picture to. In OpenGL this |
| 241 |
//! should only be accessed on the main thread |
| 242 |
SDL_Texture* texture; |
| 243 |
|
| 244 |
//! A mutex for restricting access to the picture buffer to a single thread |
| 245 |
std::mutex mutex; |
| 246 |
|
| 247 |
//! A condition for indicating access to the picture buffer |
| 248 |
std::condition_variable cond; |
| 249 |
|
| 250 |
//! The picture queue, FIFO ring buffer of movie_pictures |
| 251 |
std::array<movie_picture, picture_buffer_size> picture_queue; |
| 252 |
}; |
| 253 |
|
| 254 |
//! The AVPacketQueue is a thread safe queue of movie packets |
| 255 |
class av_packet_queue { |
| 256 |
public: |
| 257 |
//! Construct a new empty packet queue |
| 258 |
av_packet_queue(); |
| 259 |
|
| 260 |
//! Destroy the packet queue. |
| 261 |
//! |
| 262 |
//! \remarks Does not free the included packets. The packet queue should be |
| 263 |
//! flushed before it is destroyed. |
| 264 |
~av_packet_queue() = default; |
| 265 |
|
| 266 |
//! Push a new packet on the back of the queue |
| 267 |
void push(av_packet_unique_ptr packet); |
| 268 |
|
| 269 |
//! Pull the packet from the front of the queue |
| 270 |
//! |
| 271 |
//! \param block Whether to block if the queue is empty or immediately |
| 272 |
//! return a nullptr |
| 273 |
av_packet_unique_ptr pull(bool block); |
| 274 |
|
| 275 |
//! Return the number of packets in the queue |
| 276 |
std::size_t get_count() const; |
| 277 |
|
| 278 |
//! Release a blocking pull without writing a new packet to the queue. |
| 279 |
void release(); |
| 280 |
|
| 281 |
//! Release and free the entire contents of the queue |
| 282 |
void clear(); |
| 283 |
|
| 284 |
private: |
| 285 |
std::queue<av_packet_unique_ptr> data; ///< The packets in the queue |
| 286 |
std::mutex mutex; ///< A mutex restricting access to the packet queue to a |
| 287 |
///< single thread |
| 288 |
std::condition_variable |
| 289 |
cond; ///< A condition to wait on for signaling the packet queue |
| 290 |
}; |
| 291 |
#endif // CORSIX_TH_USE_FFMPEG |
| 292 |
|
| 293 |
//! Movie player for CorsixTH |
| 294 |
//! |
| 295 |
//! The movie player is designed to be preinitialized and used for multiple |
| 296 |
//! movies. After initializing the movie player, call movie_player::set_renderer |
| 297 |
//! to assign the current SDL renderer to the movie player. Then |
| 298 |
//! movie_player::load the desired movie and finally movie_player::play it. |
| 299 |
class movie_player { |
| 300 |
public: |
| 301 |
//! Construct a new movie_player |
| 302 |
movie_player(); |
| 303 |
|
| 304 |
//! Destroy the movie_player |
| 305 |
~movie_player(); |
| 306 |
|
| 307 |
//! Assign the renderer on which to draw the movie. |
| 308 |
//! |
| 309 |
//! movie_player does not take ownership of the render, it is up to the |
| 310 |
//! caller to delete it, after deleting movie_player or setting a different |
| 311 |
//! renderer. |
| 312 |
void set_renderer(SDL_Renderer* pRenderer); |
| 313 |
|
| 314 |
//! Return whether movies were compiled into CorsixTH |
| 315 |
bool movies_enabled() const; |
| 316 |
|
| 317 |
//! Load the movie with the given file name |
| 318 |
bool load(const char* szFilepath); |
| 319 |
|
| 320 |
//! Unload and free the currently loaded movie. |
| 321 |
//! |
| 322 |
//! \remark This is called by load before loading a new movie so it is |
| 323 |
//! unnecessary to explicitly call this method. There is no harm either. |
| 324 |
void unload(); |
| 325 |
|
| 326 |
//! Play the currently loaded movie |
| 327 |
//! |
| 328 |
//! \param requested_audio_channel The audio channel to use |
| 329 |
void play(int requested_audio_channel); |
| 330 |
|
| 331 |
//! Stop the currently playing movie |
| 332 |
void stop(); |
| 333 |
|
| 334 |
//! Pause the currently playing movie |
| 335 |
//! |
| 336 |
//! This method is not thread safe, it should only be called from the event |
| 337 |
//! thread. |
| 338 |
void togglePause(); |
| 339 |
|
| 340 |
//! Return the original height of the movie |
| 341 |
int get_native_height() const; |
| 342 |
|
| 343 |
//! Return the original width of the movie |
| 344 |
int get_native_width() const; |
| 345 |
|
| 346 |
//! Return whether the movie has an audio stream |
| 347 |
bool has_audio_track() const; |
| 348 |
|
| 349 |
//! Return the length of the movie in milliseconds |
| 350 |
double get_movie_length() const; |
| 351 |
|
| 352 |
//! Return a text description of the last error encountered |
| 353 |
const char* get_last_error() const; |
| 354 |
|
| 355 |
//! Clear the last error so that if there is no more errors before the next |
| 356 |
//! call to movie_player::get_last_error() it will return an empty string. |
| 357 |
void clear_last_error(); |
| 358 |
|
| 359 |
//! Draw the next frame if it is time to do so |
| 360 |
//! |
| 361 |
//! \param destination_rect The location and dimensions in the renderer on |
| 362 |
//! which to draw the movie |
| 363 |
//! \returns The current presentation time stamp of the movie in milliseconds |
| 364 |
double refresh(const SDL_Rect& destination_rect); |
| 365 |
|
| 366 |
//! Deallocate the picture buffer and free any resources associated with it. |
| 367 |
//! |
| 368 |
//! \remark This destroys the textures and other resources that may lock |
| 369 |
//! the renderer from being deleted. If the target changes you would call |
| 370 |
//! this, then free and switch renderers in the outside program, then call |
| 371 |
//! movie_player::set_renderer and finally |
| 372 |
//! movie_player::allocate_picture_buffer. \remark Up to the size of the |
| 373 |
//! picture buffer frames may be lost during this process. |
| 374 |
void deallocate_picture_buffer(); |
| 375 |
|
| 376 |
//! Allocate the picture buffer for the current renderer |
| 377 |
void allocate_picture_buffer(); |
| 378 |
|
| 379 |
//! Read packets from the movie and allocate them to the appropriate stream |
| 380 |
//! packet queues. Signal if we have reached the end of the movie. |
| 381 |
//! |
| 382 |
//! \remark This should not be called externally. It is public as it is the |
| 383 |
//! entry point of a thread. |
| 384 |
void read_streams(); |
| 385 |
|
| 386 |
//! Read video frames from the video packet queue and write them to the |
| 387 |
//! picture queue. |
| 388 |
//! |
| 389 |
//! \remark This should not be called externally. It is public as it is the |
| 390 |
//! entry point of a thread. |
| 391 |
void run_video(); |
| 392 |
|
| 393 |
//! Read audio from the audio packet queue, and copy it into the audio |
| 394 |
//! buffer for playback |
| 395 |
void copy_audio_to_stream(uint8_t* pbStream, int iStreamSize); |
| 396 |
|
| 397 |
private: |
| 398 |
#ifdef CORSIX_TH_USE_FFMPEG |
| 399 |
static constexpr size_t movie_error_buffer_capacity = |
| 400 |
128; ///< Buffer to hold last error description |
| 401 |
|
| 402 |
//! Get the AVCodecContext associated with a given stream |
| 403 |
av_codec_context_unique_ptr get_codec_context_for_stream( |
| 404 |
av_codec_ptr codec, AVStream* stream) const; |
| 405 |
|
| 406 |
void play_audio(int requested_audio_channel); |
| 407 |
|
| 408 |
//! Get the time the given frame should be played (from the start of the |
| 409 |
//! stream) |
| 410 |
//! |
| 411 |
//! \param frame The video or audio frame |
| 412 |
//! \param streamIndex The position of the stream in m_pFormatContexts |
| 413 |
//! streams array |
| 414 |
double get_presentation_time_for_frame(const AVFrame& frame, |
| 415 |
int streamIndex) const; |
| 416 |
|
| 417 |
//! Decode audio from the movie into a format suitable for playback |
| 418 |
int decode_audio_frame(uint8_t* stream, int stream_size); |
| 419 |
|
| 420 |
//! Convert packet data into frames |
| 421 |
//! |
| 422 |
//! \param stream The index of the stream to get the frame for |
| 423 |
//! \param frame An empty frame which gets populated by the data in the |
| 424 |
//! packet queue. |
| 425 |
//! \returns FFMPEG result of avcodec_receive_frame |
| 426 |
int populate_frame(int stream, AVFrame& frame); |
| 427 |
|
| 428 |
//! Convert packet data into frames |
| 429 |
//! |
| 430 |
//! \param ctx The AVCodecContext of the stream to populate |
| 431 |
//! \param pq The packet queue to pull packets from |
| 432 |
//! \param frame An empty frame which gets populated by the data in the |
| 433 |
//! packet queue. |
| 434 |
//! \returns FFMPEG result of avcodec_receive_frame |
| 435 |
int populate_frame(AVCodecContext& ctx, av_packet_queue& pq, AVFrame& frame); |
| 436 |
|
| 437 |
SDL_Renderer* renderer; ///< The renderer to draw to |
| 438 |
|
| 439 |
//! A description of the last error |
| 440 |
std::string last_error; |
| 441 |
|
| 442 |
//! A buffer for passing to ffmpeg to get error details |
| 443 |
char error_buffer[movie_error_buffer_capacity]; |
| 444 |
|
| 445 |
//! Indicate that we are in the process of aborting playback |
| 446 |
std::atomic<bool> aborting; |
| 447 |
|
| 448 |
//! Indicate that playback is paused |
| 449 |
std::atomic<bool> paused{}; |
| 450 |
uint32_t pause_start_time{}; ///< The time in SDL ticks at which we paused |
| 451 |
|
| 452 |
std::mutex decoding_audio_mutex; ///< Synchronize access to #m_pAudioBuffer |
| 453 |
|
| 454 |
AVFormatContext* format_context; ///< Information related to the loaded |
| 455 |
///< movie and all of its streams |
| 456 |
int video_stream_index; ///< The index of the video stream |
| 457 |
int audio_stream_index; ///< The index of the audio stream |
| 458 |
av_codec_context_unique_ptr |
| 459 |
video_codec_context; ///< The video codec and information |
| 460 |
///< related to video |
| 461 |
av_codec_context_unique_ptr |
| 462 |
audio_codec_context; ///< The audio codec and information |
| 463 |
///< related to audio |
| 464 |
|
| 465 |
// queues for transferring data between threads |
| 466 |
av_packet_queue video_queue; ///< Packets from the video stream |
| 467 |
av_packet_queue audio_queue; ///< Packets from the audio stream |
| 468 |
::movie_picture_buffer movie_picture_buffer; ///< Buffer of processed video |
| 469 |
|
| 470 |
// clock sync parameters |
| 471 |
std::uint32_t current_sync_pts_system_time; ///< System time matching |
| 472 |
///< #current_sync_pts |
| 473 |
double current_sync_pts; ///< The current presentation time stamp (from the |
| 474 |
///< audio stream) |
| 475 |
|
| 476 |
SwrContext* audio_resample_context; ///< Context for resampling audio for |
| 477 |
///< playback with ffmpeg |
| 478 |
|
| 479 |
mix_chunk_unique_ptr empty_audio_chunk; ///< Empty chunk needed for SDL_mixer |
| 480 |
std::array<std::uint8_t, 1024> |
| 481 |
audio_chunk_buffer; ///< 0'd out buffer for the SDL_mixer chunk |
| 482 |
|
| 483 |
int audio_channel; ///< The channel to play audio on, -1 for none |
| 484 |
int mixer_channels; ///< How many channels to play on (1 - mono, 2 - |
| 485 |
///< stereo) |
| 486 |
int mixer_frequency; ///< The frequency of audio expected by SDL_mixer |
| 487 |
|
| 488 |
std::thread stream_thread; ///< The thread responsible for reading the |
| 489 |
///< movie streams |
| 490 |
std::thread video_thread; ///< The thread responsible for decoding the |
| 491 |
///< video stream |
| 492 |
#endif // CORSIX_TH_USE_FFMPEG |
| 493 |
}; |
| 494 |
|
| 495 |
#endif // TH_VIDEO_H |