xine-lib  1.2.9
metronom.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2000-2017 the xine project
3  *
4  * This file is part of xine, a free video player.
5  *
6  * xine is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * xine is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
19  *
20  * metronom: general pts => virtual calculation/assoc
21  *
22  * virtual pts: unit 1/90000 sec, always increasing
23  * can be used for synchronization
24  * video/audio frame with same pts also have same vpts
25  * but pts is likely to differ from vpts
26  *
27  * the basic idea is:
28  * video_pts + video_wrap_offset = video_vpts
29  * audio_pts + audio_wrap_offset = audio_vpts
30  *
31  * - video_wrap_offset should be equal to audio_wrap_offset as to have
32  * perfect audio and video sync. They will differ on brief periods due
33  * discontinuity correction.
34  * - metronom should also interpolate vpts values most of the time as
35  * video_pts and audio_vpts are not given for every frame.
36  * - corrections to the frame rate may be needed to cope with bad
37  * encoded streams.
38  */
39 
40 #ifndef HAVE_METRONOM_H
41 #define HAVE_METRONOM_H
42 
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46 
47 #include <pthread.h>
48 
49 #include <xine/video_out.h>
50 #include <xine.h>
51 
52 typedef struct metronom_s metronom_t ;
54 typedef struct scr_plugin_s scr_plugin_t;
55 
56 /* metronom prebuffer can be adjusted with XINE_PARAM_METRONOM_PREBUFFER.
57  * it sets how much the first video/audio frame should be delayed to
58  * have some prebuffering at the output layers. reducing this value (about
59  * 1/8 sec) may result in faster seeking (good to simulate play backwards,
60  * for example).
61  */
62 #define PREBUFFER_PTS_OFFSET 12000
63 
64  /* see below */
65 #define DISC_STREAMSTART 0
66 #define DISC_RELATIVE 1
67 #define DISC_ABSOLUTE 2
68 #define DISC_STREAMSEEK 3
69 #define DISC_GAPLESS 4
70 
71 struct metronom_s {
72 
73  /*
74  * called by audio output driver to inform metronom about current audio
75  * samplerate
76  *
77  * parameter pts_per_smpls : 1/90000 sec per 65536 samples
78  */
79  void (*set_audio_rate) (metronom_t *self, int64_t pts_per_smpls);
80 
81  /*
82  * called by video output driver for *every* frame
83  *
84  * parameter frame containing pts, scr, ... information
85  *
86  * will set vpts field in frame
87  *
88  * this function will also update video_wrap_offset if a discontinuity
89  * is detected (read the comentaries below about discontinuities).
90  *
91  */
92 
93  void (*got_video_frame) (metronom_t *self, vo_frame_t *frame);
94 
95  /*
96  * called by audio output driver whenever audio samples are delivered to it
97  *
98  * parameter pts : pts for audio data if known, 0 otherwise
99  * nsamples : number of samples delivered
100  *
101  * return value: virtual pts for audio data
102  *
103  * this function will also update audio_wrap_offset if a discontinuity
104  * is detected (read the comentaries below about discontinuities).
105  *
106  */
107 
108  int64_t (*got_audio_samples) (metronom_t *self, int64_t pts,
109  int nsamples);
110 
111  /*
112  * called by SPU decoder whenever a packet is delivered to it
113  *
114  * parameter pts : pts for SPU packet if known, 0 otherwise
115  *
116  * return value: virtual pts for SPU packet
117  * (this is the only pts to vpts function that cannot update the wrap_offset
118  * due to the lack of regularity on spu packets)
119  */
120 
121  int64_t (*got_spu_packet) (metronom_t *self, int64_t pts);
122 
123  /*
124  * tell metronom about discontinuities.
125  *
126  * these functions are called due to a discontinuity detected at
127  * demux stage.
128  *
129  * there are different types of discontinuities:
130  *
131  * DISC_STREAMSTART : new stream starts, expect pts values to start
132  * from zero immediately
133  * DISC_RELATIVE : typically a wrap-around, expect pts with
134  * a specified offset from the former ones soon
135  * DISC_ABSOLUTE : typically a new menu stream (nav packets)
136  * pts will start from given value soon
137  * DISC_STREAMSEEK : used by video and audio decoder loop,
138  * when a buffer with BUF_FLAG_SEEK set is encountered;
139  * applies the necessary vpts offset for the seek in
140  * metronom, but keeps the vpts difference between
141  * audio and video, so that metronom doesn't cough
142  * DISC_GAPLESS : wait until it is safe to start next gapless stream.
143  * used internally by xine_play ().
144  */
145  void (*handle_audio_discontinuity) (metronom_t *self, int type, int64_t disc_off);
146  void (*handle_video_discontinuity) (metronom_t *self, int type, int64_t disc_off);
147 
148  /*
149  * set/get options for metronom, constants see below
150  */
151  void (*set_option) (metronom_t *self, int option, int64_t value);
152  int64_t (*get_option) (metronom_t *self, int option);
153 
154  /*
155  * set a master metronom
156  * this is currently useful to sync independently generated streams
157  * (e.g. by post plugins) to the discontinuity domain of another
158  * metronom
159  */
160  void (*set_master) (metronom_t *self, metronom_t *master);
161 
162  void (*exit) (metronom_t *self);
163 
164 #ifdef METRONOM_INTERNAL
165  /*
166  * metronom internal stuff
167  */
168  xine_t *xine;
169 
170  metronom_t *master;
171 
172  int64_t pts_per_smpls;
173 
174  int64_t video_vpts;
175  int64_t spu_vpts;
176  int64_t audio_vpts;
177  int64_t audio_vpts_rmndr; /* the remainder for integer division */
178 
179  int64_t vpts_offset;
180 
181  int64_t video_drift;
182  int64_t video_drift_step;
183 
184  int audio_samples;
185  int64_t audio_drift_step;
186 
187  int64_t prebuffer;
188  int64_t av_offset;
189  int64_t spu_offset;
190 
191  pthread_mutex_t lock;
192 
193  int have_video;
194  int have_audio;
195  int video_discontinuity_count;
196  int audio_discontinuity_count;
197  int discontinuity_handled_count;
198  pthread_cond_t video_discontinuity_reached;
199  pthread_cond_t audio_discontinuity_reached;
200 
201  int force_video_jump;
202  int force_audio_jump;
203 
204  int64_t img_duration;
205  int img_cpt;
206  int64_t last_video_pts;
207  int64_t last_audio_pts;
208 
209  int video_mode;
210 #endif
211 };
212 
213 /*
214  * metronom options
215  */
216 
217 #define METRONOM_AV_OFFSET 2
218 #define METRONOM_ADJ_VPTS_OFFSET 3
219 #define METRONOM_FRAME_DURATION 4
220 #define METRONOM_SPU_OFFSET 5
221 #define METRONOM_VPTS_OFFSET 6
222 #define METRONOM_PREBUFFER 7
223 #define METRONOM_VPTS 8
224 /* METRONOM_LOCK can be used to lock metronom when multiple options needs to be fetched atomically (ex. VPTS_OFFSET and AV_OFFSET).
225  * example:
226  * metronom->set_option(metronom, METRONOM_LOCK, 1);
227  * vpts_offset = metronom->get_option(metronom, METRONOM_VPTS_OFFSET|METRONOM_NO_LOCK);
228  * av_offset = metronom->get_option(metronom, METRONOM_AV_OFFSET|METRONOM_NO_LOCK);
229  * metronom->set_option(metronom, METRONOM_LOCK, 0);
230  */
231 #define METRONOM_LOCK 9
232 #define METRONOM_NO_LOCK 0x8000
233 
234 metronom_t *_x_metronom_init (int have_video, int have_audio, xine_t *xine) XINE_MALLOC XINE_PROTECTED;
235 
236 /* FIXME: reorder this structure on the next cleanup to remove the dummies */
238 
239  /*
240  * set/get options for clock, constants see below
241  */
242  void (*set_option) (metronom_clock_t *self, int option, int64_t value);
243  int64_t (*get_option) (metronom_clock_t *self, int option);
244 
245  /*
246  * system clock reference (SCR) functions
247  */
248 
249 #ifdef METRONOM_CLOCK_INTERNAL
250  /*
251  * start clock (no clock reset)
252  * at given pts
253  */
254  void (*start_clock) (metronom_clock_t *self, int64_t pts);
255 
256 
257  /*
258  * stop metronom clock
259  */
260  void (*stop_clock) (metronom_clock_t *self);
261 
262 
263  /*
264  * resume clock from where it was stopped
265  */
266  void (*resume_clock) (metronom_clock_t *self);
267 #else
268  void *dummy1;
269  void *dummy2;
270  void *dummy3;
271 #endif
272 
273 
274  /*
275  * get current clock value in vpts
276  */
277  int64_t (*get_current_time) (metronom_clock_t *self);
278 
279 
280  /*
281  * adjust master clock to external timer (e.g. audio hardware)
282  */
283  void (*adjust_clock) (metronom_clock_t *self, int64_t desired_pts);
284 
285 #ifdef METRONOM_CLOCK_INTERNAL
286  /*
287  * set clock speed
288  * for constants see xine_internal.h
289  */
290 
291  int (*set_fine_speed) (metronom_clock_t *self, int speed);
292 #else
293  void *dummy4;
294 #endif
295 
296  /*
297  * (un)register a System Clock Reference provider at the metronom
298  */
301 
302 #ifdef METRONOM_CLOCK_INTERNAL
303  void (*exit) (metronom_clock_t *self);
304 
305  xine_t *xine;
306 
307  scr_plugin_t *scr_master;
308  scr_plugin_t **scr_list;
309  pthread_t sync_thread;
310  int thread_running;
311  int scr_adjustable;
312 #else
313  void *dummy5;
314  void *dummy6;
315  void *dummy7;
316  void *dummy8;
317  pthread_t dummy9;
318  int dummy10;
319  int dummy11;
320 #endif
321 
322  int speed;
323 
324 #ifdef METRONOM_CLOCK_INTERNAL
325  pthread_mutex_t lock;
326  pthread_cond_t cancel;
327 #endif
328 };
329 
331 
332 /*
333  * clock options
334  */
335 
336 #define CLOCK_SCR_ADJUSTABLE 1
337 
338 /*
339  * SCR (system clock reference) plugins
340  */
341 
343 {
344  int (*get_priority) (scr_plugin_t *self);
345 
346  /*
347  * set/get clock speed
348  *
349  * for speed constants see xine_internal.h
350  * returns actual speed
351  */
352 
353  int (*set_fine_speed) (scr_plugin_t *self, int speed);
354 
355  void (*adjust) (scr_plugin_t *self, int64_t vpts);
356 
357  void (*start) (scr_plugin_t *self, int64_t start_vpts);
358 
359  int64_t (*get_current) (scr_plugin_t *self);
360 
361  void (*exit) (scr_plugin_t *self);
362 
364 
366 };
367 
368 #ifdef __cplusplus
369 }
370 #endif
371 
372 #endif
int dummy11
Definition: metronom.h:319
void(* start)(scr_plugin_t *self, int64_t start_vpts)
Definition: metronom.h:357
metronom_clock_t * clock
Definition: metronom.h:363
void * dummy3
Definition: metronom.h:270
void(* exit)(metronom_t *self)
Definition: metronom.h:162
int speed
Definition: metronom.h:322
#define XINE_MALLOC
Definition: attributes.h:119
int(* set_fine_speed)(scr_plugin_t *self, int speed)
Definition: metronom.h:353
void(* handle_video_discontinuity)(metronom_t *self, int type, int64_t disc_off)
Definition: metronom.h:146
int64_t(* get_current)(scr_plugin_t *self)
Definition: metronom.h:359
void(* adjust)(scr_plugin_t *self, int64_t vpts)
Definition: metronom.h:355
int interface_version
Definition: metronom.h:365
metronom_t * _x_metronom_init(int have_video, int have_audio, xine_t *xine)
Definition: metronom.c:1152
void(* set_audio_rate)(metronom_t *self, int64_t pts_per_smpls)
Definition: metronom.h:79
void * dummy1
Definition: metronom.h:268
void(* exit)(scr_plugin_t *self)
Definition: metronom.h:361
void * dummy7
Definition: metronom.h:315
int64_t(* get_option)(metronom_clock_t *self, int option)
Definition: metronom.h:243
metronom_clock_t * _x_metronom_clock_init(xine_t *xine)
Definition: metronom.c:1230
void * dummy8
Definition: metronom.h:316
int64_t(* get_current_time)(metronom_clock_t *self)
Definition: metronom.h:277
int64_t(* got_audio_samples)(metronom_t *self, int64_t pts, int nsamples)
Definition: metronom.h:108
Definition: metronom.h:71
void(* unregister_scr)(metronom_clock_t *self, scr_plugin_t *scr)
Definition: metronom.h:300
void(* handle_audio_discontinuity)(metronom_t *self, int type, int64_t disc_off)
Definition: metronom.h:145
void(* got_video_frame)(metronom_t *self, vo_frame_t *frame)
Definition: metronom.h:93
Definition: video_out.h:60
void * dummy4
Definition: metronom.h:293
void(* set_master)(metronom_t *self, metronom_t *master)
Definition: metronom.h:160
int(* get_priority)(scr_plugin_t *self)
Definition: metronom.h:344
void * dummy5
Definition: metronom.h:313
void(* set_option)(metronom_t *self, int option, int64_t value)
Definition: metronom.h:151
int64_t(* get_option)(metronom_t *self, int option)
Definition: metronom.h:152
#define XINE_PROTECTED
Definition: attributes.h:73
void(* set_option)(metronom_clock_t *self, int option, int64_t value)
Definition: metronom.h:242
Definition: metronom.h:237
Definition: xine_internal.h:81
pthread_t dummy9
Definition: metronom.h:317
int dummy10
Definition: metronom.h:318
void * dummy2
Definition: metronom.h:269
int(* register_scr)(metronom_clock_t *self, scr_plugin_t *scr)
Definition: metronom.h:299
void(* adjust_clock)(metronom_clock_t *self, int64_t desired_pts)
Definition: metronom.h:283
int64_t(* got_spu_packet)(metronom_t *self, int64_t pts)
Definition: metronom.h:121
void * dummy6
Definition: metronom.h:314
Definition: metronom.h:342