packets_sync.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. #ifndef AVPLAYER2_PACKETS_SYNC_H
  2. #define AVPLAYER2_PACKETS_SYNC_H
  3. #pragma once
  4. #include <QMutex>
  5. #include <QThread>
  6. #include <QWaitCondition>
  7. #include "AVPlayer2/ThreadBase.h"
  8. // only need to open audio filter, video will be synced
  9. #define USE_AVFILTER_AUDIO 1
  10. #define USE_AVFILTER_VIDEO 1
  11. extern "C" {
  12. #include <libavcodec/avcodec.h>
  13. #include <libavcodec/avfft.h>
  14. #include <libavformat/avformat.h>
  15. #include <libavutil/bprint.h>
  16. #include <libavutil/fifo.h>
  17. #include <libavutil/imgutils.h>
  18. #include <libavutil/samplefmt.h>
  19. #include <libavutil/time.h>
  20. #include <libswresample/swresample.h>
  21. #include <libswscale/swscale.h>
  22. #if USE_AVFILTER_AUDIO
  23. #include <libavfilter/avfilter.h>
  24. #include <libavfilter/buffersink.h>
  25. #include <libavfilter/buffersrc.h>
  26. #include <libavutil/avstring.h>
  27. #include <libavutil/macros.h>
  28. #include <libavutil/opt.h>
  29. #endif
  30. }
  31. #define MAX_QUEUE_SIZE (15 * 1024 * 1024)
  32. #define MIN_FRAMES 25
  33. #define EXTERNAL_CLOCK_MIN_FRAMES 2
  34. #define EXTERNAL_CLOCK_MAX_FRAMES 10
  35. #define SDL_MIX_MAXVOLUME 128
  36. /* Minimum SDL audio buffer size, in samples. */
  37. #define SDL_AUDIO_MIN_BUFFER_SIZE 512
  38. /* Calculate actual buffer size keeping in mind not cause too frequent audio
  39. * callbacks */
  40. #define SDL_AUDIO_MAX_CALLBACKS_PER_SEC 30
  41. /* Step size for volume control in dB */
  42. #define SDL_VOLUME_STEP (0.75)
  43. /* no AV sync correction is done if below the minimum AV sync threshold */
  44. #define AV_SYNC_THRESHOLD_MIN 0.04
  45. /* AV sync correction is done if above the maximum AV sync threshold */
  46. #define AV_SYNC_THRESHOLD_MAX 0.1
  47. /* If a frame duration is longer than this, it will not be duplicated to
  48. * compensate AV sync */
  49. #define AV_SYNC_FRAMEDUP_THRESHOLD 0.1
  50. /* no AV correction is done if too big error */
  51. #define AV_NOSYNC_THRESHOLD 10.0
  52. /* maximum audio speed change to get correct sync */
  53. #define SAMPLE_CORRECTION_PERCENT_MAX 10
  54. /* external clock speed adjustment constants for realtime sources based on
  55. * buffer fullness */
  56. #define EXTERNAL_CLOCK_SPEED_MIN 0.900
  57. #define EXTERNAL_CLOCK_SPEED_MAX 1.010
  58. #define EXTERNAL_CLOCK_SPEED_STEP 0.001
  59. /* we use about AUDIO_DIFF_AVG_NB A-V differences to make the average */
  60. #define AUDIO_DIFF_AVG_NB 20
  61. /* polls for possible required screen refresh at least this often, should be
  62. * less than 1/fps */
  63. #define REFRESH_RATE 0.01
  64. /* NOTE: the size must be big enough to compensate the hardware audio buffersize
  65. * size */
  66. /* TODO: We assume that a decoded and resampled frame fits into this buffer */
  67. #define SAMPLE_ARRAY_SIZE (8 * 65536)
  68. #define CURSOR_HIDE_DELAY 1000000
  69. #define USE_ONEPASS_SUBTITLE_RENDER 1
  70. typedef struct MyAVPacketList
  71. {
  72. AVPacket* pkt;
  73. int serial;
  74. } MyAVPacketList;
  75. typedef struct PacketQueue
  76. {
  77. AVFifo* pkt_list;
  78. int nb_packets;
  79. int size;
  80. int64_t duration;
  81. int abort_request;
  82. int serial;
  83. QMutex* mutex;
  84. QWaitCondition* cond;
  85. } PacketQueue;
  86. #define VIDEO_PICTURE_QUEUE_SIZE 3
  87. #define SUBPICTURE_QUEUE_SIZE 16
  88. #define SAMPLE_QUEUE_SIZE 20
  89. #define FRAME_QUEUE_SIZE \
  90. FFMAX(SAMPLE_QUEUE_SIZE, FFMAX(VIDEO_PICTURE_QUEUE_SIZE, SUBPICTURE_QUEUE_SIZE))
  91. typedef struct AudioParams
  92. {
  93. int freq;
  94. AVChannelLayout ch_layout;
  95. enum AVSampleFormat fmt;
  96. int frame_size;
  97. int bytes_per_sec;
  98. } AudioParams;
  99. typedef struct Clock
  100. {
  101. double pts; /* clock base */
  102. double pts_drift; /* clock base minus time at which we updated the clock */
  103. double last_updated;
  104. double speed;
  105. int serial; /* clock is based on a packet with this serial */
  106. int paused;
  107. int* queue_serial; /* pointer to the current packet queue serial, used for
  108. obsolete clock detection */
  109. } Clock;
  110. typedef struct FrameData
  111. {
  112. int64_t pkt_pos;
  113. } FrameData;
  114. /* Common struct for handling all types of decoded data and allocated render
  115. * buffers. */
  116. typedef struct Frame
  117. {
  118. AVFrame* frame;
  119. AVSubtitle sub;
  120. int serial;
  121. double pts; /* presentation timestamp for the frame */
  122. double duration; /* estimated duration of the frame */
  123. int64_t pos; /* byte position of the frame in the input file */
  124. int width;
  125. int height;
  126. int format;
  127. AVRational sar;
  128. int uploaded;
  129. int flip_v;
  130. } Frame;
  131. typedef struct FrameQueue
  132. {
  133. Frame queue[FRAME_QUEUE_SIZE]; // array queue model, loop queue
  134. int rindex; // read pointer
  135. int windex; // write pointer
  136. int size; // current frame num
  137. int max_size; // max frame num
  138. int keep_last; // keep last frame
  139. int rindex_shown; // current frame is shown
  140. QMutex* mutex;
  141. QWaitCondition* cond;
  142. PacketQueue* pktq;
  143. } FrameQueue;
  144. enum {
  145. AV_SYNC_AUDIO_MASTER, /* default choice */
  146. AV_SYNC_VIDEO_MASTER,
  147. AV_SYNC_EXTERNAL_CLOCK, /* synchronize to an external clock */
  148. };
  149. typedef struct Decoder
  150. {
  151. AVPacket* pkt;
  152. PacketQueue* queue;
  153. AVCodecContext* avctx;
  154. int pkt_serial;
  155. int finished;
  156. int packet_pending;
  157. QWaitCondition* empty_queue_cond; // SDL_cond* empty_queue_cond;
  158. int64_t start_pts;
  159. AVRational start_pts_tb;
  160. int64_t next_pts;
  161. AVRational next_pts_tb;
  162. // SDL_Thread* decoder_tid;
  163. void* decoder_tid; // thread pointer
  164. char* decoder_name;
  165. } Decoder;
  166. typedef struct Threads
  167. {
  168. ThreadBase* read_tid{nullptr};
  169. ThreadBase* video_decode_tid{nullptr};
  170. ThreadBase* audio_decode_tid{nullptr};
  171. ThreadBase* video_play_tid{nullptr};
  172. ThreadBase* audio_play_tid{nullptr};
  173. ThreadBase* subtitle_decode_tid{nullptr};
  174. Threads() = default;
  175. Threads(const Threads&) = delete;
  176. Threads& operator=(const Threads&) = delete;
  177. } Threads;
  178. typedef struct VideoState
  179. {
  180. const AVInputFormat* iformat;
  181. int abort_request;
  182. int force_refresh;
  183. int paused;
  184. int last_paused;
  185. int queue_attachments_req;
  186. int seek_req;
  187. int seek_flags;
  188. int64_t seek_pos;
  189. int64_t seek_rel;
  190. int read_pause_return;
  191. AVFormatContext* ic;
  192. int realtime;
  193. Clock vidclk;
  194. Clock audclk;
  195. Clock extclk;
  196. PacketQueue videoq;
  197. PacketQueue audioq;
  198. PacketQueue subtitleq;
  199. FrameQueue pictq;
  200. FrameQueue sampq;
  201. FrameQueue subpq;
  202. Decoder viddec;
  203. Decoder auddec;
  204. Decoder subdec;
  205. int audio_stream;
  206. int av_sync_type;
  207. double audio_clock;
  208. int audio_clock_serial;
  209. double audio_clock_old; // add for renew clock after speed changing
  210. AVStream* audio_st;
  211. int audio_volume;
  212. int muted;
  213. int frame_drops_early;
  214. int frame_drops_late;
  215. #if 0
  216. double audio_diff_avg_coef;
  217. int audio_diff_avg_count;
  218. double audio_diff_cum; /* used for AV difference average computation */
  219. double audio_diff_threshold;
  220. int16_t sample_array[SAMPLE_ARRAY_SIZE];
  221. int sample_array_index;
  222. uint8_t* audio_buf;
  223. uint8_t* audio_buf1;
  224. unsigned int audio_buf_size; /* in bytes */
  225. unsigned int audio_buf1_size;
  226. int audio_buf_index; /* in bytes */
  227. int audio_write_buf_size;
  228. int audio_hw_buf_size;
  229. struct SwrContext* swr_ctx;
  230. enum ShowMode {
  231. SHOW_MODE_NONE = -1, SHOW_MODE_VIDEO = 0, SHOW_MODE_WAVES, SHOW_MODE_RDFT, SHOW_MODE_NB
  232. } show_mode;
  233. #endif
  234. int last_i_start;
  235. // RDFTContext* rdft;
  236. int rdft_bits;
  237. // FFTSample* rdft_data;
  238. int xpos;
  239. double last_vis_time;
  240. // SDL_Texture* vis_texture;
  241. // SDL_Texture* sub_texture;
  242. // SDL_Texture* vid_texture;
  243. int subtitle_stream;
  244. AVStream* subtitle_st;
  245. double frame_timer;
  246. double frame_last_returned_time;
  247. double frame_last_filter_delay;
  248. int video_stream;
  249. AVStream* video_st;
  250. double max_frame_duration; // maximum duration of a frame - above this, we
  251. // consider the jump a timestamp discontinuity
  252. struct SwsContext* img_convert_ctx;
  253. struct SwsContext* sub_convert_ctx;
  254. int eof;
  255. int loop;
  256. char* filename;
  257. int width, height, xleft, ytop;
  258. int step;
  259. #if USE_AVFILTER_AUDIO
  260. struct AudioParams audio_src;
  261. struct AudioParams audio_tgt;
  262. double audio_speed;
  263. char* afilters;
  264. int req_afilter_reconfigure;
  265. char* vfilters;
  266. int req_vfilter_reconfigure;
  267. struct AudioParams audio_filter_src;
  268. int vfilter_idx;
  269. AVFilterContext* in_video_filter; // the first filter in the video chain
  270. AVFilterContext* out_video_filter; // the last filter in the video chain
  271. AVFilterContext* in_audio_filter; // the first filter in the audio chain
  272. AVFilterContext* out_audio_filter; // the last filter in the audio chain
  273. AVFilterGraph* agraph;
  274. AVFilterGraph* vgraph;
  275. #endif
  276. int last_video_stream, last_audio_stream, last_subtitle_stream;
  277. QWaitCondition* continue_read_thread;
  278. int read_thread_exit;
  279. // void* read_tid; //read thread pointer
  280. Threads threads; // all thread would access VideoState
  281. } VideoState;
  282. #if !NDEBUG
  283. #define PRINT_PACKETQUEUE_INFO 0
  284. #define PRINT_PACKETQUEUE_AUDIO_INFO 0
  285. #else
  286. #define PRINT_PACKETQUEUE_INFO 0
  287. #define PRINT_PACKETQUEUE_AUDIO_INFO 0
  288. #endif
  289. /***************PacketQueue operations*****************/
  290. int packet_queue_init(PacketQueue* q);
  291. void packet_queue_destroy(PacketQueue* q);
  292. void packet_queue_flush(PacketQueue* q);
  293. void packet_queue_start(PacketQueue* q);
  294. void packet_queue_abort(PacketQueue* q);
  295. int packet_queue_get(PacketQueue* q, AVPacket* pkt, int block, int* serial);
  296. int packet_queue_put(PacketQueue* q, AVPacket* pkt);
  297. int packet_queue_put_nullpacket(PacketQueue* q, AVPacket* pkt, int stream_index);
  298. int packet_queue_put_private(PacketQueue* q, AVPacket* pkt);
  299. void packet_queue_print(const PacketQueue* q, const AVPacket* pkt, const QString& prefix);
  300. /***************FrameQueue operations*****************/
  301. int frame_queue_init(FrameQueue* f, PacketQueue* pktq, int max_size, int keep_last);
  302. void frame_queue_destory(FrameQueue* f);
  303. void frame_queue_unref_item(Frame* vp);
  304. void frame_queue_signal(FrameQueue* f);
  305. Frame* frame_queue_peek_writable(FrameQueue* f);
  306. Frame* frame_queue_peek(FrameQueue* f);
  307. Frame* frame_queue_peek_next(FrameQueue* f);
  308. Frame* frame_queue_peek_last(FrameQueue* f);
  309. Frame* frame_queue_peek_readable(FrameQueue* f);
  310. void frame_queue_push(FrameQueue* f);
  311. void frame_queue_next(FrameQueue* f);
  312. int frame_queue_nb_remaining(FrameQueue* f);
  313. int64_t frame_queue_last_pos(FrameQueue* f);
  314. int queue_picture(
  315. VideoState* is, AVFrame* src_frame, double pts, double duration, int64_t pos, int serial);
  316. int get_video_frame(VideoState* is, AVFrame* frame);
  317. /***************Decoder operations*****************/
  318. int decoder_init(Decoder* d,
  319. AVCodecContext* avctx,
  320. PacketQueue* queue,
  321. QWaitCondition* empty_queue_cond);
  322. int decoder_decode_frame(Decoder* d, AVFrame* frame, AVSubtitle* sub);
  323. void decoder_destroy(Decoder* d);
  324. int decoder_start(Decoder* d, void* thread, const char* thread_name);
  325. void decoder_abort(Decoder* d, FrameQueue* fq);
  326. void get_file_info(const char* filename, int64_t& duration);
  327. void get_duration_time(
  328. const int64_t duration_us, int64_t& hours, int64_t& mins, int64_t& secs, int64_t& us);
  329. /***************Clock operations*****************/
  330. double get_clock(Clock* c);
  331. void set_clock_at(Clock* c, double pts, int serial, double time);
  332. void set_clock(Clock* c, double pts, int serial);
  333. void set_clock_speed(Clock* c, double speed);
  334. void init_clock(Clock* c, int* queue_serial);
  335. void sync_clock_to_slave(Clock* c, Clock* slave);
  336. /***************VideoState operations*****************/
  337. int get_master_sync_type(VideoState* is);
  338. double get_master_clock(VideoState* is);
  339. void check_external_clock_speed(VideoState* is);
  340. void stream_seek(VideoState* is, int64_t pos, int64_t rel, int seek_by_bytes);
  341. // void stream_toggle_pause(VideoState* is, bool pause = true);
  342. void toggle_pause(VideoState* is, bool pause = true);
  343. void toggle_mute(VideoState* is, bool mute = true);
  344. void update_volume(VideoState* is, int sign, double step);
  345. void step_to_next_frame(VideoState* is);
  346. double compute_target_delay(double delay, VideoState* is);
  347. double vp_duration(VideoState* is, Frame* vp, Frame* nextvp);
  348. void update_video_pts(VideoState* is, double pts, int64_t pos, int serial);
  349. #if PRINT_PACKETQUEUE_INFO
  350. void print_state_info(VideoState* is);
  351. #endif
  352. /****************************************/
  353. int is_realtime(AVFormatContext* s);
  354. int stream_has_enough_packets(AVStream* st, int stream_id, PacketQueue* queue);
  355. #if USE_AVFILTER_AUDIO
  356. void set_audio_playspeed(VideoState* is, double value);
  357. int cmp_audio_fmts(enum AVSampleFormat fmt1,
  358. int64_t channel_count1,
  359. enum AVSampleFormat fmt2,
  360. int64_t channel_count2);
  361. // int64_t get_valid_channel_layout(int64_t channel_layout, int channels);
  362. int configure_audio_filters(VideoState* is, const char* afilters, int force_output_format);
  363. int configure_filtergraph(AVFilterGraph* graph,
  364. const char* filtergraph,
  365. AVFilterContext* source_ctx,
  366. AVFilterContext* sink_ctx);
  367. // int audio_open(void* opaque, int64_t wanted_channel_layout, int
  368. // wanted_nb_channels, int wanted_sample_rate, struct AudioParams*
  369. // audio_hw_params);
  370. void set_video_watermark(VideoState* is, const char* watermark_text);
  371. void set_video_playspeed(VideoState* is);
  372. int configure_video_filters(AVFilterGraph* graph,
  373. VideoState* is,
  374. const char* vfilters,
  375. AVFrame* frame);
  376. #endif
  377. #endif // AVPLAYER2_PACKETS_SYNC_H