blob: 12bfa82fac220088321c4719fc8cb0b3525395b1 [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
45#define AV_SYNC_INVALID_PAUSE_PTS 0xFFFFFFFF
46#define AV_SYNC_STEP_PAUSE_PTS 0xFFFFFFFE
47
48typedef uint32_t pts90K;
49struct vframe;
50typedef void (*free_frame)(struct vframe * frame);
51typedef void (*pause_pts_done)(uint32_t pts, void* priv);
52
Song Zhaoea5a0412021-01-18 16:40:08 -080053typedef enum {
54 /* good to render */
55 AV_SYNC_ASCB_OK,
56 /* triggered by av_sync_destroy */
57 AV_SYNC_ASCB_STOP,
58} avs_ascb_reason;
59
60typedef int (*audio_start_cb)(void *priv, avs_ascb_reason reason);
61
62typedef enum {
63 AV_SYNC_ASTART_SYNC = 0,
64 AV_SYNC_ASTART_ASYNC,
65 AV_SYNC_ASTART_ERR
66} avs_start_ret;
67
68typedef enum {
69 /* render ASAP */
70 AV_SYNC_AA_RENDER,
71 /* late, drop to catch up */
72 AV_SYNC_AA_DROP,
73 /* early, insert to hold */
74 AV_SYNC_AA_INSERT,
75 AA_SYNC_AA_MAX
76} avs_audio_action;
77
78struct audio_policy {
79 avs_audio_action action;
80 /* delta between apts and ideal render position
81 * positive means apts is behind wall clock
82 * negative means apts is ahead of wall clock
83 */
84 int delta;
85};
86
Song Zhaoc03ba122020-12-23 21:54:02 -080087struct vframe {
88 /* private user data */
89 void *private;
90 pts90K pts;
91 /* duration of this frame. 0 for invalid value */
92 pts90K duration;
93 /* free function, will be called when multi frames are
94 * toggled in a single VSYNC, on frames not for display.
95 * For the last toggled frame, free won't be called. Caller
96 * of av_sync_pop_frame() are responsible for free poped frame.
97 * For example, if frame 1/2/3 are toggled in a single VSYCN,
98 * free() of 1/2 will be called, but free() of 3 won't.
99 */
100 free_frame free;
101
102 //For internal usage under this line
103 /*holding period */
104 int hold_period;
105};
106
Song Zhaoea5a0412021-01-18 16:40:08 -0800107struct video_config {
108 /* AV sync delay. The delay of render.
109 * For video in VSYNC number unit:
110 * 2 for video planes
111 * 1 for osd planes
112 */
113 int delay;
114};
115
116/* Open a new session and create the ID
117 * Params:
118 * @session_id: session ID allocated if success
119 * Return:
120 * >= 0 session handle. session_id is valid
121 * < 0 error, session_id is invalid
122 */
123int av_sync_open_session(int *session_id);
124
125/* Close session, will also decrease the ref cnt of session ID.
126 * Params:
127 * @session: Av sync session handle
128 */
129void av_sync_close_session(int session);
130
131/* Attach to kernel session. The returned avsync module will
132 * associated with @session_id. It is valid to create multi instance
133 * with the same session_id. For example, one for video, one for audio,
134 * one for PCR. But it is NOT valid to create 2 video session with the
135 * same session_id. Create session will increase the ref count of session
136 * ID.
Song Zhaoc03ba122020-12-23 21:54:02 -0800137 * Params:
138 * @session_id: unique AV sync session ID to bind audio and video
139 * usually get from kernel driver.
140 * @mode: AV sync mode of enum sync_mode
Song Zhaoea5a0412021-01-18 16:40:08 -0800141 * @type: AV sync type of enum sync_type. For a stream with both
142 * video and audio, two instances with each type should be created.
Song Zhaoc03ba122020-12-23 21:54:02 -0800143 * @start_thres: The start threshold of AV sync module. Set it to 0 for
144 * a default value. For low latency mode, set it to 1. Bigger
Song Zhaoea5a0412021-01-18 16:40:08 -0800145 * value will increase the delay of the first frame rendered.
Song Zhaoc03ba122020-12-23 21:54:02 -0800146 * AV sync will start when frame number reached threshold.
Song Zhaoc03ba122020-12-23 21:54:02 -0800147 * Return:
148 * null for failure, or handle for avsync module.
149 */
150void* av_sync_create(int session_id,
151 enum sync_mode mode,
Song Zhaoea5a0412021-01-18 16:40:08 -0800152 enum sync_type type,
153 int start_thres);
Song Zhaoc03ba122020-12-23 21:54:02 -0800154
Song Zhaoea5a0412021-01-18 16:40:08 -0800155
156/* Attach to an existed session. The returned avsync module will
157 * associated with @session_id. use av_sync_destroy to destroy it.
158 * Designed for audio path for now. Session created by audio client,
159 * and attached by audio HAL. Attach sesson will increase the ref count of
160 * session ID.
161 * Params:
162 * @session_id: unique AV sync session ID to bind audio and video
163 * usually get from kernel driver.
164 * @type: AV sync type of enum sync_type. For a stream with both
165 * video and audio, two instances with each type should be created.
166 * Return:
167 * null for failure, or handle for avsync module.
168 */
169
170void* av_sync_attach(int session_id,
171 enum sync_type type);
172
173/* Dettach from existed session. Decrease the ref count of session ID
174 * Params:
175 * @sync: handle for avsync module
176 */
Song Zhaoc03ba122020-12-23 21:54:02 -0800177void av_sync_destroy(void *sync);
178
Song Zhaoea5a0412021-01-18 16:40:08 -0800179int av_sync_video_config(void *sync, struct video_config* config);
180
181/* Set start policy
182 * Params:
183 * @sync: AV sync module handle
184 * @policy: start policy
185 * Return:
186 * 0 for OK, or error code
187 */
188int avs_sync_set_start_policy(void *sync, enum sync_start_policy policy);
189
Song Zhaoc03ba122020-12-23 21:54:02 -0800190/* Pause/Resume AV sync module.
191 * It will return last frame in @av_sync_pop_frame() in pause state
192 * Params:
193 * @sync: AV sync module handle
194 * @pause: pause for true, or resume.
195 * Return:
196 * 0 for OK, or error code
197 */
198int av_sync_pause(void *sync, bool pause);
199
Song Zhaoea5a0412021-01-18 16:40:08 -0800200/* Push a new video frame to AV sync module
Song Zhaoc03ba122020-12-23 21:54:02 -0800201 * Params:
202 * @sync: AV sync module handle
203 * Return:
204 * 0 for OK, or error code
205 */
206int av_sync_push_frame(void *sync , struct vframe *frame);
207
208/* Pop video frame for next VSYNC. This API should be VSYNC triggerd.
209 * Params:
210 * @sync: AV sync module handle
211 * Return:
212 * Old frame if current frame is hold
213 * New frame if it is time for a frame toggle.
214 * null if there is no frame to pop out (underrun).
215 * */
216struct vframe *av_sync_pop_frame(void *sync);
217
Song Zhaoea5a0412021-01-18 16:40:08 -0800218/* Audio start control. Audio render need to check return value and
219 * do sync or async start.
Song Zhaoc03ba122020-12-23 21:54:02 -0800220 * Params:
221 * @sync: AV sync module handle
Song Zhaoea5a0412021-01-18 16:40:08 -0800222 * @pts: first audio pts
223 * @delay: rendering delay
224 * @cb: callback to notify rendering start.
225 * @priv: parameter for cb
226 * Return:
227 * AV_SYNC_ASTART_SYNC, audio can start render ASAP. No need to wait for callback.
228 * AV_SYNC_ASTART_ASYNC, audio need to block until callback is triggered.
229 * AV_SYNC_ASTART_ERR, something bad happens
Song Zhaoc03ba122020-12-23 21:54:02 -0800230 */
Song Zhaoea5a0412021-01-18 16:40:08 -0800231avs_start_ret av_sync_audio_start(
232 void *sync,
233 pts90K pts,
234 pts90K delay,
235 audio_start_cb cb,
236 void *priv);
237
238/* Audio render policy. Indicate render action and the difference from ideal position
239 * Params:
240 * @sync: AV sync module handle
241 * @pts: curent audio pts (considering delay)
242 * @policy: action field indicates the action.
243 * delta field indicates the diff from ideal position
244 * Return:
245 * 0 for OK, or error code
246 */
247int av_sync_audio_render(
248 void *sync,
249 pts90K pts,
250 struct audio_policy *policy);
Song Zhaoc03ba122020-12-23 21:54:02 -0800251
252/* set playback speed
253 * Currently only work for VMASTER mode
254 * Params:
255 * @speed: 1.0 is normal speed. 2.0 is 2x faster. 0.1 is 10x slower
256 * Minimium speed is 0.001
257 * Max speed is 100
258 * Return:
259 * 0 for OK, or error code
260 */
261int av_sync_set_speed(void *sync, float speed);
262
263/* switch avsync mode
Song Zhaoea5a0412021-01-18 16:40:08 -0800264 * PCR master/IPTV handle mode change automatically
Song Zhaoc03ba122020-12-23 21:54:02 -0800265 * Params:
266 * @sync: AV sync module handle
267 * @sync_mode: new mode
268 * Return:
269 * 0 for OK, or error code
270 */
271int av_sync_change_mode(void *sync, enum sync_mode mode);
272
273/* set pause PTS
274 * av sync will pause after reaching the assigned PTS or do step.
275 * Need to call @av_sync_pause(false) to resume a the playback.
276 * Params:
277 * @sync: AV sync module handle
278 * @pts: pause pts in uint of 90K.
279 * AV_SYNC_STEP_PAUSE_PTS: step one frame, and the callback
280 * will report pts value of the stepped frame.
281 * AV_SYNC_INVALID_PAUSE_PTS: disable this feature.
282 * Other value: play until the assigned PTS, and the callback
283 * will report pts value of the paused frame.
284 * Return:
285 * 0 for OK, or error code
286 */
287int av_sync_set_pause_pts(void *sync, pts90K pts);
288
289/* set pause PTS call back
290 * av sync will callback when pause PTS is reached with assigned PTS from
291 * @av_sync_set_pause_pts.
292 * Params:
293 * @sync: AV sync module handle
294 * @cb: callback function
295 * @priv: callback function parameter
296 * Return:
297 * 0 for OK, or error code
298 */
299int av_sync_set_pause_pts_cb(void *sync, pause_pts_done cb, void *priv);
Song Zhaoea5a0412021-01-18 16:40:08 -0800300
301/* Update PCR clock.
302 * Use by AV_SYNC_TYPE_PCR only.
303 * Params:
304 * @sync: AV sync module handle
305 * @pts: PCR clock
306 * Return:
307 * 0 for OK, or error code
308 */
309int av_sync_set_pcr_clock(void *sync, pts90K pts);
310
311int av_sync_get_pcr_clock(void *sync, pts90K *pts);
312
313/* get wall clock
314 * Params:
315 * @sync: AV sync module handle
316 * @pts: return wall clock
317 * Return:
318 * 0 for OK, or error code
319 */
320int av_sync_get_clock(void *sync, pts90K *pts);
321
322/* set session name for debugging purpose
323 * The session name will be listed from /sys/class/aml_msync/list_session
324 * Params:
325 * @sync: AV sync module handle
326 * @name: name of current session.
327 * Return:
328 * 0 for OK, or error code
329 */
330int av_sync_set_session_name(void *sync, const char *name);
Song Zhaoc03ba122020-12-23 21:54:02 -0800331#endif