2014-10-22 20:11:03 +00:00
# ifndef VIDEOPLAYER_VIDEOSTATE_H
# define VIDEOPLAYER_VIDEOSTATE_H
2022-01-05 08:26:02 +00:00
# include <cstdint>
2018-12-24 07:49:36 +00:00
# include <atomic>
2022-01-05 08:26:02 +00:00
# include <array>
2016-10-10 14:06:22 +00:00
# include <vector>
# include <memory>
2018-12-07 03:23:52 +00:00
# include <string>
2020-06-25 19:46:07 +00:00
# include <mutex>
# include <condition_variable>
2014-10-22 20:11:03 +00:00
2015-04-19 18:07:18 +00:00
# include <osg/ref_ptr>
namespace osg
{
class Texture2D ;
}
2014-10-22 20:11:03 +00:00
2021-05-02 08:59:22 +00:00
# if defined(_MSC_VER)
# pragma warning (push)
# pragma warning (disable : 4244)
# endif
2018-10-20 07:09:52 +00:00
extern " C "
{
# include <libavcodec/avcodec.h>
# include <libavformat/avformat.h>
# include <libavutil/imgutils.h>
# include <libavutil/channel_layout.h>
// From version 54.56 binkaudio encoding format changed from S16 to FLTP. See:
// https://gitorious.org/ffmpeg/ffmpeg/commit/7bfd1766d1c18f07b0a2dd042418a874d49ea60d
// https://ffmpeg.zeranoe.com/forum/viewtopic.php?f=15&t=872
# include <libswresample/swresample.h>
}
2021-05-02 08:59:22 +00:00
# if defined(_MSC_VER)
# pragma warning (pop)
# endif
2014-10-22 20:11:03 +00:00
# include "videodefs.hpp"
# define VIDEO_PICTURE_QUEUE_SIZE 50
extern " C "
{
struct SwsContext ;
struct AVPacket ;
struct AVFormatContext ;
struct AVStream ;
struct AVFrame ;
}
namespace Video
{
struct VideoState ;
class MovieAudioFactory ;
class MovieAudioDecoder ;
2016-10-10 14:06:22 +00:00
class VideoThread ;
class ParseThread ;
2014-10-22 20:11:03 +00:00
2014-10-24 19:19:17 +00:00
struct ExternalClock
{
ExternalClock ( ) ;
uint64_t mTimeBase ;
uint64_t mPausedAt ;
bool mPaused ;
2020-06-25 19:46:07 +00:00
std : : mutex mMutex ;
2014-10-24 19:19:17 +00:00
void setPaused ( bool paused ) ;
uint64_t get ( ) ;
void set ( uint64_t time ) ;
} ;
2021-12-29 12:01:51 +00:00
class PacketList
{
public :
AVPacket * pkt = nullptr ;
PacketList * next = nullptr ;
} ;
2014-10-22 20:11:03 +00:00
struct PacketQueue {
PacketQueue ( )
2020-11-13 07:39:47 +00:00
: first_pkt ( nullptr ) , last_pkt ( nullptr ) , flushing ( false ) , nb_packets ( 0 ) , size ( 0 )
2014-10-22 20:11:03 +00:00
{ }
~ PacketQueue ( )
{ clear ( ) ; }
2021-12-29 12:01:51 +00:00
PacketList * first_pkt , * last_pkt ;
2018-12-24 07:49:36 +00:00
std : : atomic < bool > flushing ;
2019-05-10 10:22:52 +00:00
std : : atomic < int > nb_packets ;
std : : atomic < int > size ;
2014-10-22 20:11:03 +00:00
2020-06-25 19:46:07 +00:00
std : : mutex mutex ;
std : : condition_variable cond ;
2014-10-22 20:11:03 +00:00
void put ( AVPacket * pkt ) ;
int get ( AVPacket * pkt , VideoState * is ) ;
void flush ( ) ;
void clear ( ) ;
} ;
struct VideoPicture {
VideoPicture ( ) : pts ( 0.0 )
{ }
2021-03-07 03:57:54 +00:00
struct AVFrameDeleter {
void operator ( ) ( AVFrame * frame ) const ;
} ;
2021-03-08 04:00:11 +00:00
// Sets frame dimensions.
// Must be called before writing to `rgbaFrame`.
// Return -1 on error.
int set_dimensions ( int w , int h ) ;
2021-03-07 03:57:54 +00:00
std : : unique_ptr < AVFrame , AVFrameDeleter > rgbaFrame ;
2014-10-22 20:11:03 +00:00
double pts ;
} ;
struct VideoState {
VideoState ( ) ;
~ VideoState ( ) ;
void setAudioFactory ( MovieAudioFactory * factory ) ;
2022-04-15 00:15:39 +00:00
void init ( std : : unique_ptr < std : : istream > & & inputstream , const std : : string & name ) ;
2014-10-22 20:11:03 +00:00
void deinit ( ) ;
2014-10-24 19:19:17 +00:00
void setPaused ( bool isPaused ) ;
void seekTo ( double time ) ;
2021-12-29 12:01:51 +00:00
double getDuration ( ) const ;
2014-10-24 19:19:17 +00:00
2014-10-22 20:11:03 +00:00
int stream_open ( int stream_index , AVFormatContext * pFormatCtx ) ;
bool update ( ) ;
static void video_thread_loop ( VideoState * is ) ;
static void decode_thread_loop ( VideoState * is ) ;
void video_display ( VideoPicture * vp ) ;
void video_refresh ( ) ;
2021-05-13 12:45:13 +00:00
int queue_picture ( const AVFrame & pFrame , double pts ) ;
double synchronize_video ( const AVFrame & src_frame , double pts ) ;
2014-10-22 20:11:03 +00:00
double get_audio_clock ( ) ;
2021-12-29 12:01:51 +00:00
double get_video_clock ( ) const ;
2014-10-22 20:11:03 +00:00
double get_external_clock ( ) ;
double get_master_clock ( ) ;
2015-04-19 18:07:18 +00:00
static int istream_read ( void * user_data , uint8_t * buf , int buf_size ) ;
static int istream_write ( void * user_data , uint8_t * buf , int buf_size ) ;
static int64_t istream_seek ( void * user_data , int64_t offset , int whence ) ;
2014-10-22 20:11:03 +00:00
2015-04-19 18:07:18 +00:00
osg : : ref_ptr < osg : : Texture2D > mTexture ;
2014-10-22 20:11:03 +00:00
MovieAudioFactory * mAudioFactory ;
2022-04-08 20:04:32 +00:00
std : : unique_ptr < MovieAudioDecoder > mAudioDecoder ;
2014-10-22 20:11:03 +00:00
2014-10-24 19:19:17 +00:00
ExternalClock mExternalClock ;
2022-04-15 00:15:39 +00:00
std : : unique_ptr < std : : istream > stream ;
2014-10-22 20:11:03 +00:00
AVFormatContext * format_ctx ;
2018-10-20 07:09:52 +00:00
AVCodecContext * video_ctx ;
AVCodecContext * audio_ctx ;
2014-10-22 20:11:03 +00:00
int av_sync_type ;
AVStream * * audio_st ;
PacketQueue audioq ;
2014-10-24 19:19:17 +00:00
uint8_t * mFlushPktData ;
2014-10-22 20:11:03 +00:00
AVStream * * video_st ;
double frame_last_pts ;
double video_clock ; ///<pts of last decoded frame / predicted pts of next decoded frame
PacketQueue videoq ;
SwsContext * sws_context ;
2021-03-08 04:00:11 +00:00
int sws_context_w , sws_context_h ;
2022-01-05 08:26:02 +00:00
std : : array < VideoPicture , VIDEO_PICTURE_QUEUE_SIZE + 1 > pictq ; // allocate one extra to make sure we do not overwrite the osg::Image currently set on the texture
2022-01-05 11:19:22 +00:00
int pictq_size ;
unsigned long pictq_rindex , pictq_windex ;
2020-06-25 19:46:07 +00:00
std : : mutex pictq_mutex ;
std : : condition_variable pictq_cond ;
2014-10-22 20:11:03 +00:00
2017-04-28 15:30:26 +00:00
std : : unique_ptr < ParseThread > parse_thread ;
std : : unique_ptr < VideoThread > video_thread ;
2014-10-22 20:11:03 +00:00
2018-12-24 07:49:36 +00:00
std : : atomic < bool > mSeekRequested ;
2014-10-24 19:19:17 +00:00
uint64_t mSeekPos ;
2018-12-24 07:49:36 +00:00
std : : atomic < bool > mVideoEnded ;
std : : atomic < bool > mPaused ;
std : : atomic < bool > mQuit ;
2014-10-22 20:11:03 +00:00
} ;
}
# endif