blob: 36f5c5e1cb7a5bcd9d38b9032ec32e5e925e5de0 [file] [log] [blame]
Song Zhaoc03ba122020-12-23 21:54:02 -08001/*
2 * Copyright (c) 2020 Amlogic, Inc. All rights reserved.
3 *
4 * This source code is subject to the terms and conditions defined in the
5 * file 'LICENSE' which is part of this source code package.
6 *
7 * Description:
8 * User space AV sync module.
9 *
10 * Author: song.zhao@amlogic.com
11 */
12#ifndef AML_AVSYNC_H__
13#define AML_AVSYNC_H__
14
15#include <stdint.h>
16#include <stdbool.h>
17#include <time.h>
18
19enum sync_mode {
20 AV_SYNC_MODE_VMASTER = 0,
21 AV_SYNC_MODE_AMASTER = 1,
22 AV_SYNC_MODE_PCR_MASTER = 2,
Song Zhaoea5a0412021-01-18 16:40:08 -080023 AV_SYNC_MODE_IPTV = 3,
24 AV_SYNC_MODE_FREE_RUN = 4,
25 AV_SYNC_MODE_MAX
26};
27
28enum sync_type {
29 AV_SYNC_TYPE_AUDIO,
30 AV_SYNC_TYPE_VIDEO,
31 AV_SYNC_TYPE_PCR,
32 AV_SYNC_TYPE_MAX
33};
34
35enum sync_start_policy {
36 AV_SYNC_START_NONE = 0,
37 AV_SYNC_START_V_FIRST = 1,
38 AV_SYNC_START_A_FIRST = 2,
39 AV_SYNC_START_ASAP = 3,
40 AV_SYNC_START_ALIGN = 4,
41 AV_SYNC_START_V_PEEK = 5,
42 AV_SYNC_START_MAX
Song Zhaoc03ba122020-12-23 21:54:02 -080043};
44
Song Zhao8039f562021-05-18 18:11:25 -070045enum clock_recovery_stat {
46 CLK_RECOVERY_NOT_RUNNING = 0,
47 CLK_RECOVERY_ONGOING = 1,
48 CLK_RECOVERY_READY = 2,
wei.dubcc2ed22021-05-19 07:16:10 -040049 CLK_RECOVERY_ERR,
Song Zhao8039f562021-05-18 18:11:25 -070050 CLK_RECOVERY_MAX
51};
52
Song Zhaoc03ba122020-12-23 21:54:02 -080053#define AV_SYNC_INVALID_PAUSE_PTS 0xFFFFFFFF
54#define AV_SYNC_STEP_PAUSE_PTS 0xFFFFFFFE
55
56typedef uint32_t pts90K;
57struct vframe;
58typedef void (*free_frame)(struct vframe * frame);
59typedef void (*pause_pts_done)(uint32_t pts, void* priv);
60
Song Zhaoea5a0412021-01-18 16:40:08 -080061typedef enum {
62 /* good to render */
63 AV_SYNC_ASCB_OK,
64 /* triggered by av_sync_destroy */
65 AV_SYNC_ASCB_STOP,
66} avs_ascb_reason;
67
68typedef int (*audio_start_cb)(void *priv, avs_ascb_reason reason);
69
70typedef enum {
71 AV_SYNC_ASTART_SYNC = 0,
72 AV_SYNC_ASTART_ASYNC,
Song Zhaod62bb392021-04-23 12:25:49 -070073 AV_SYNC_ASTART_AGAIN,
Song Zhaoea5a0412021-01-18 16:40:08 -080074 AV_SYNC_ASTART_ERR
75} avs_start_ret;
76
77typedef enum {
78 /* render ASAP */
79 AV_SYNC_AA_RENDER,
80 /* late, drop to catch up */
81 AV_SYNC_AA_DROP,
82 /* early, insert to hold */
83 AV_SYNC_AA_INSERT,
84 AA_SYNC_AA_MAX
85} avs_audio_action;
86
87struct audio_policy {
88 avs_audio_action action;
89 /* delta between apts and ideal render position
90 * positive means apts is behind wall clock
91 * negative means apts is ahead of wall clock
Song Zhaod62bb392021-04-23 12:25:49 -070092 * 0 is only valid for AV_SYNC_AA_DROP, drop current chunk
Song Zhaoea5a0412021-01-18 16:40:08 -080093 */
94 int delta;
95};
96
Song Zhaoc03ba122020-12-23 21:54:02 -080097struct vframe {
98 /* private user data */
99 void *private;
100 pts90K pts;
101 /* duration of this frame. 0 for invalid value */
102 pts90K duration;
103 /* free function, will be called when multi frames are
104 * toggled in a single VSYNC, on frames not for display.
105 * For the last toggled frame, free won't be called. Caller
106 * of av_sync_pop_frame() are responsible for free poped frame.
107 * For example, if frame 1/2/3 are toggled in a single VSYCN,
108 * free() of 1/2 will be called, but free() of 3 won't.
109 */
110 free_frame free;
111
112 //For internal usage under this line
113 /*holding period */
114 int hold_period;
115};
116
Song Zhaoea5a0412021-01-18 16:40:08 -0800117struct video_config {
118 /* AV sync delay. The delay of render.
119 * For video in VSYNC number unit:
120 * 2 for video planes
121 * 1 for osd planes
122 */
123 int delay;
124};
125
126/* Open a new session and create the ID
127 * Params:
128 * @session_id: session ID allocated if success
129 * Return:
130 * >= 0 session handle. session_id is valid
131 * < 0 error, session_id is invalid
132 */
133int av_sync_open_session(int *session_id);
134
135/* Close session, will also decrease the ref cnt of session ID.
136 * Params:
137 * @session: Av sync session handle
138 */
139void av_sync_close_session(int session);
140
141/* Attach to kernel session. The returned avsync module will
142 * associated with @session_id. It is valid to create multi instance
143 * with the same session_id. For example, one for video, one for audio,
144 * one for PCR. But it is NOT valid to create 2 video session with the
145 * same session_id. Create session will increase the ref count of session
146 * ID.
Song Zhaoc03ba122020-12-23 21:54:02 -0800147 * Params:
148 * @session_id: unique AV sync session ID to bind audio and video
149 * usually get from kernel driver.
150 * @mode: AV sync mode of enum sync_mode
Song Zhaoea5a0412021-01-18 16:40:08 -0800151 * @type: AV sync type of enum sync_type. For a stream with both
152 * video and audio, two instances with each type should be created.
Song Zhaoc03ba122020-12-23 21:54:02 -0800153 * @start_thres: The start threshold of AV sync module. Set it to 0 for
154 * a default value. For low latency mode, set it to 1. Bigger
Song Zhaoea5a0412021-01-18 16:40:08 -0800155 * value will increase the delay of the first frame rendered.
Song Zhaoc03ba122020-12-23 21:54:02 -0800156 * AV sync will start when frame number reached threshold.
Song Zhaoc03ba122020-12-23 21:54:02 -0800157 * Return:
158 * null for failure, or handle for avsync module.
159 */
160void* av_sync_create(int session_id,
161 enum sync_mode mode,
Song Zhaoea5a0412021-01-18 16:40:08 -0800162 enum sync_type type,
163 int start_thres);
Song Zhaoc03ba122020-12-23 21:54:02 -0800164
Song Zhaoea5a0412021-01-18 16:40:08 -0800165
166/* Attach to an existed session. The returned avsync module will
167 * associated with @session_id. use av_sync_destroy to destroy it.
168 * Designed for audio path for now. Session created by audio client,
169 * and attached by audio HAL. Attach sesson will increase the ref count of
170 * session ID.
171 * Params:
172 * @session_id: unique AV sync session ID to bind audio and video
173 * usually get from kernel driver.
174 * @type: AV sync type of enum sync_type. For a stream with both
175 * video and audio, two instances with each type should be created.
176 * Return:
177 * null for failure, or handle for avsync module.
178 */
179
180void* av_sync_attach(int session_id,
181 enum sync_type type);
182
183/* Dettach from existed session. Decrease the ref count of session ID
184 * Params:
185 * @sync: handle for avsync module
186 */
Song Zhaoc03ba122020-12-23 21:54:02 -0800187void av_sync_destroy(void *sync);
188
Song Zhaoea5a0412021-01-18 16:40:08 -0800189int av_sync_video_config(void *sync, struct video_config* config);
190
191/* Set start policy
192 * Params:
193 * @sync: AV sync module handle
194 * @policy: start policy
195 * Return:
196 * 0 for OK, or error code
197 */
198int avs_sync_set_start_policy(void *sync, enum sync_start_policy policy);
199
Song Zhaoc03ba122020-12-23 21:54:02 -0800200/* Pause/Resume AV sync module.
201 * It will return last frame in @av_sync_pop_frame() in pause state
202 * Params:
203 * @sync: AV sync module handle
204 * @pause: pause for true, or resume.
205 * Return:
206 * 0 for OK, or error code
207 */
208int av_sync_pause(void *sync, bool pause);
209
Song Zhaoea5a0412021-01-18 16:40:08 -0800210/* Push a new video frame to AV sync module
Song Zhaoc03ba122020-12-23 21:54:02 -0800211 * Params:
212 * @sync: AV sync module handle
213 * Return:
214 * 0 for OK, or error code
215 */
216int av_sync_push_frame(void *sync , struct vframe *frame);
217
218/* Pop video frame for next VSYNC. This API should be VSYNC triggerd.
219 * Params:
220 * @sync: AV sync module handle
221 * Return:
222 * Old frame if current frame is hold
223 * New frame if it is time for a frame toggle.
224 * null if there is no frame to pop out (underrun).
225 * */
226struct vframe *av_sync_pop_frame(void *sync);
227
Song Zhaoea5a0412021-01-18 16:40:08 -0800228/* Audio start control. Audio render need to check return value and
Song Zhaod62bb392021-04-23 12:25:49 -0700229 * do sync or async start. NOT thread safe.
Song Zhaoc03ba122020-12-23 21:54:02 -0800230 * Params:
231 * @sync: AV sync module handle
Song Zhaoea5a0412021-01-18 16:40:08 -0800232 * @pts: first audio pts
233 * @delay: rendering delay
234 * @cb: callback to notify rendering start.
235 * @priv: parameter for cb
236 * Return:
237 * AV_SYNC_ASTART_SYNC, audio can start render ASAP. No need to wait for callback.
238 * AV_SYNC_ASTART_ASYNC, audio need to block until callback is triggered.
Song Zhaod62bb392021-04-23 12:25:49 -0700239 * AV_SYNC_ASTART_AGAIN, discard current audio chunk and call this API again.
Song Zhaoea5a0412021-01-18 16:40:08 -0800240 * AV_SYNC_ASTART_ERR, something bad happens
Song Zhaoc03ba122020-12-23 21:54:02 -0800241 */
Song Zhaoea5a0412021-01-18 16:40:08 -0800242avs_start_ret av_sync_audio_start(
243 void *sync,
244 pts90K pts,
245 pts90K delay,
246 audio_start_cb cb,
247 void *priv);
248
249/* Audio render policy. Indicate render action and the difference from ideal position
Song Zhaod62bb392021-04-23 12:25:49 -0700250 * NOT thread safe.
Song Zhaoea5a0412021-01-18 16:40:08 -0800251 * Params:
252 * @sync: AV sync module handle
253 * @pts: curent audio pts (considering delay)
254 * @policy: action field indicates the action.
255 * delta field indicates the diff from ideal position
256 * Return:
257 * 0 for OK, or error code
258 */
259int av_sync_audio_render(
260 void *sync,
261 pts90K pts,
262 struct audio_policy *policy);
Song Zhaoc03ba122020-12-23 21:54:02 -0800263
264/* set playback speed
265 * Currently only work for VMASTER mode
266 * Params:
267 * @speed: 1.0 is normal speed. 2.0 is 2x faster. 0.1 is 10x slower
268 * Minimium speed is 0.001
269 * Max speed is 100
270 * Return:
271 * 0 for OK, or error code
272 */
273int av_sync_set_speed(void *sync, float speed);
274
275/* switch avsync mode
Song Zhaoea5a0412021-01-18 16:40:08 -0800276 * PCR master/IPTV handle mode change automatically
Song Zhaoc03ba122020-12-23 21:54:02 -0800277 * Params:
278 * @sync: AV sync module handle
279 * @sync_mode: new mode
280 * Return:
281 * 0 for OK, or error code
282 */
283int av_sync_change_mode(void *sync, enum sync_mode mode);
284
Song Zhao01031bb2021-05-13 21:23:20 -0700285/* get avsync mode
286 * Params:
287 * @sync: AV sync module handle
288 * @mode: pointer to return current mode
289 * Return:
290 * 0 for OK, or error code
291 */
292int av_sync_get_mode(void *sync, enum sync_mode *mode);
293
Song Zhaoc03ba122020-12-23 21:54:02 -0800294/* set pause PTS
295 * av sync will pause after reaching the assigned PTS or do step.
296 * Need to call @av_sync_pause(false) to resume a the playback.
297 * Params:
298 * @sync: AV sync module handle
299 * @pts: pause pts in uint of 90K.
300 * AV_SYNC_STEP_PAUSE_PTS: step one frame, and the callback
301 * will report pts value of the stepped frame.
302 * AV_SYNC_INVALID_PAUSE_PTS: disable this feature.
303 * Other value: play until the assigned PTS, and the callback
304 * will report pts value of the paused frame.
305 * Return:
306 * 0 for OK, or error code
307 */
308int av_sync_set_pause_pts(void *sync, pts90K pts);
309
310/* set pause PTS call back
311 * av sync will callback when pause PTS is reached with assigned PTS from
312 * @av_sync_set_pause_pts.
313 * Params:
314 * @sync: AV sync module handle
315 * @cb: callback function
316 * @priv: callback function parameter
317 * Return:
318 * 0 for OK, or error code
319 */
320int av_sync_set_pause_pts_cb(void *sync, pause_pts_done cb, void *priv);
Song Zhaoea5a0412021-01-18 16:40:08 -0800321
322/* Update PCR clock.
323 * Use by AV_SYNC_TYPE_PCR only.
324 * Params:
325 * @sync: AV sync module handle
326 * @pts: PCR clock
Song Zhaod62bb392021-04-23 12:25:49 -0700327 * @mono_clock: system monotonic clock receiving the pts
Song Zhaoea5a0412021-01-18 16:40:08 -0800328 * Return:
329 * 0 for OK, or error code
330 */
Song Zhaod62bb392021-04-23 12:25:49 -0700331int av_sync_set_pcr_clock(void *sync, pts90K pts, uint64_t mono_clock);
Song Zhaoea5a0412021-01-18 16:40:08 -0800332
Song Zhaod62bb392021-04-23 12:25:49 -0700333/* Get PCR clock pair.
334 * Use for clock recovery
335 * Params:
336 * @sync: AV sync module handle
337 * @pts: PCR clock pointer
338 * @mono_clock: system monotonic clock pointer receiving the pts
339 * Return:
340 * 0 for OK, or error code
341 */
342int av_sync_get_pcr_clock(void *sync, pts90K *pts, uint64_t *mono_clock);
Song Zhaoea5a0412021-01-18 16:40:08 -0800343
344/* get wall clock
345 * Params:
346 * @sync: AV sync module handle
347 * @pts: return wall clock
348 * Return:
349 * 0 for OK, or error code
350 */
351int av_sync_get_clock(void *sync, pts90K *pts);
352
353/* set session name for debugging purpose
354 * The session name will be listed from /sys/class/aml_msync/list_session
355 * Params:
356 * @sync: AV sync module handle
357 * @name: name of current session.
358 * Return:
359 * 0 for OK, or error code
360 */
361int av_sync_set_session_name(void *sync, const char *name);
yongchun.li107a6162021-05-05 02:38:57 -0700362
363/* Start/finish audio seamless switch
364 * It declaims the coming audio seamless switch will happen.
365 * After removing the audio, the video will still play normally.
366 * After adding back the audio, it will sync with pipe-line without break the playing,
367 * only short audio break/mute on the switch is expected.
368 * Params:
369 * @sync: AV sync module handle
370 * @start: start the switch for true, or finish/reset for false
371 * Return:
372 * 0 for OK, or error code
373 */
374int av_sync_set_audio_switch(void *sync, bool start);
375
376/* Get the current audio seamless switch status.
377 * After setting the seamless switch, upper level can get the result by polling.
378 * The status will become false when the switching has finished or being termintated
379 * Params:
380 * @sync: AV sync module handle
381 * @status: start the switch for true, or finish/reset for false
382 * Return:
383 * 0 for OK, or error code
384 */
385int av_sync_get_audio_switch(void *sync, bool *status);
386
Song Zhao8039f562021-05-18 18:11:25 -0700387/* Get the current clock devication between PCR clock and system monotonic clock.
388 * Params:
389 * @sync: AV sync module handle
390 * @ppm: part per million. Bigger than 0 is PCR master clock is faster. Less than 0
391 * if PCR master clock is slower.
392 * Return:
393 * CLK_RECOVERY_NOT_RUNNING: current mode doesn't support clock recovery
394 * CLK_RECOVERY_ONGOING: still ongoing, need more time to converge.
395 * CLK_RECOVERY_READY: clock recovery result is ready to be used. The result might
396 * be updated later for better accuracy.
wei.dubcc2ed22021-05-19 07:16:10 -0400397 * CLK_RECOVERY_ERR: error happens
Song Zhao8039f562021-05-18 18:11:25 -0700398 */
399enum clock_recovery_stat av_sync_get_clock_devication(void *sync, int32_t *ppm);
Song Zhaoc03ba122020-12-23 21:54:02 -0800400#endif