packets_sync.h 13 KB

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