blob: 3b872d21079191e0cd5649a24f923d276dc8713d [file] [log] [blame]
Song Zhaoc03ba122020-12-23 21:54:02 -08001/*
bo.xiao00387eb2023-02-24 18:44:29 +08002 * Copyright (C) 2021 Amlogic Corporation.
Song Zhaoc03ba122020-12-23 21:54:02 -08003 *
bo.xiao00387eb2023-02-24 18:44:29 +08004 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Song Zhaoc03ba122020-12-23 21:54:02 -080015 *
16 * Description:
17 * User space AV sync module.
18 *
19 * Author: song.zhao@amlogic.com
20 */
21#ifndef AML_AVSYNC_H__
22#define AML_AVSYNC_H__
23
24#include <stdint.h>
25#include <stdbool.h>
26#include <time.h>
27
28enum sync_mode {
29 AV_SYNC_MODE_VMASTER = 0,
30 AV_SYNC_MODE_AMASTER = 1,
31 AV_SYNC_MODE_PCR_MASTER = 2,
Song Zhaoea5a0412021-01-18 16:40:08 -080032 AV_SYNC_MODE_IPTV = 3,
33 AV_SYNC_MODE_FREE_RUN = 4,
Song Zhao95bd0922021-09-21 14:07:46 -070034 AV_SYNC_MODE_VIDEO_MONO = 5, /* video render by system mono time */
Song Zhaoea5a0412021-01-18 16:40:08 -080035 AV_SYNC_MODE_MAX
36};
37
38enum sync_type {
39 AV_SYNC_TYPE_AUDIO,
40 AV_SYNC_TYPE_VIDEO,
41 AV_SYNC_TYPE_PCR,
42 AV_SYNC_TYPE_MAX
43};
44
45enum sync_start_policy {
46 AV_SYNC_START_NONE = 0,
47 AV_SYNC_START_V_FIRST = 1,
48 AV_SYNC_START_A_FIRST = 2,
49 AV_SYNC_START_ASAP = 3,
50 AV_SYNC_START_ALIGN = 4,
51 AV_SYNC_START_V_PEEK = 5,
52 AV_SYNC_START_MAX
Song Zhaoc03ba122020-12-23 21:54:02 -080053};
54
Song Zhao8039f562021-05-18 18:11:25 -070055enum clock_recovery_stat {
56 CLK_RECOVERY_NOT_RUNNING = 0,
57 CLK_RECOVERY_ONGOING = 1,
58 CLK_RECOVERY_READY = 2,
wei.dubcc2ed22021-05-19 07:16:10 -040059 CLK_RECOVERY_ERR,
Song Zhao8039f562021-05-18 18:11:25 -070060 CLK_RECOVERY_MAX
61};
62
Song Zhao08fa16b2021-12-08 14:30:17 -080063#define AV_SYNC_INVALID_PTS 0xFFFFFFFF
64#define AV_SYNC_INVALID_PAUSE_PTS AV_SYNC_INVALID_PTS
Song Zhaoc03ba122020-12-23 21:54:02 -080065#define AV_SYNC_STEP_PAUSE_PTS 0xFFFFFFFE
Song Zhao95bd0922021-09-21 14:07:46 -070066#define AV_SYNC_SESSION_V_MONO 64
Song Zhaoc03ba122020-12-23 21:54:02 -080067
68typedef uint32_t pts90K;
69struct vframe;
70typedef void (*free_frame)(struct vframe * frame);
71typedef void (*pause_pts_done)(uint32_t pts, void* priv);
yongchun.li428390d2022-02-17 17:15:40 -080072typedef void (*underflow_detected)(uint32_t pts, void* priv);
Song Zhaoc03ba122020-12-23 21:54:02 -080073
Song Zhaoea5a0412021-01-18 16:40:08 -080074typedef enum {
75 /* good to render */
76 AV_SYNC_ASCB_OK,
77 /* triggered by av_sync_destroy */
78 AV_SYNC_ASCB_STOP,
79} avs_ascb_reason;
80
81typedef int (*audio_start_cb)(void *priv, avs_ascb_reason reason);
82
83typedef enum {
84 AV_SYNC_ASTART_SYNC = 0,
85 AV_SYNC_ASTART_ASYNC,
Song Zhaod62bb392021-04-23 12:25:49 -070086 AV_SYNC_ASTART_AGAIN,
Song Zhaoea5a0412021-01-18 16:40:08 -080087 AV_SYNC_ASTART_ERR
88} avs_start_ret;
89
90typedef enum {
91 /* render ASAP */
92 AV_SYNC_AA_RENDER,
93 /* late, drop to catch up */
94 AV_SYNC_AA_DROP,
95 /* early, insert to hold */
96 AV_SYNC_AA_INSERT,
97 AA_SYNC_AA_MAX
98} avs_audio_action;
99
100struct audio_policy {
101 avs_audio_action action;
102 /* delta between apts and ideal render position
103 * positive means apts is behind wall clock
104 * negative means apts is ahead of wall clock
Song Zhaod62bb392021-04-23 12:25:49 -0700105 * 0 is only valid for AV_SYNC_AA_DROP, drop current chunk
Song Zhaoea5a0412021-01-18 16:40:08 -0800106 */
107 int delta;
108};
109
Song Zhaoc03ba122020-12-23 21:54:02 -0800110struct vframe {
111 /* private user data */
112 void *private;
113 pts90K pts;
Song Zhao95bd0922021-09-21 14:07:46 -0700114 /* mono render time in nanosecdons only use in VIDEO_MONO */
115 uint64_t mts;
Song Zhaoc03ba122020-12-23 21:54:02 -0800116 /* duration of this frame. 0 for invalid value */
117 pts90K duration;
118 /* free function, will be called when multi frames are
119 * toggled in a single VSYNC, on frames not for display.
120 * For the last toggled frame, free won't be called. Caller
121 * of av_sync_pop_frame() are responsible for free poped frame.
122 * For example, if frame 1/2/3 are toggled in a single VSYCN,
123 * free() of 1/2 will be called, but free() of 3 won't.
124 */
125 free_frame free;
126
127 //For internal usage under this line
128 /*holding period */
129 int hold_period;
130};
131
Song Zhaoea5a0412021-01-18 16:40:08 -0800132struct video_config {
133 /* AV sync delay. The delay of render.
134 * For video in VSYNC number unit:
135 * 2 for video planes
136 * 1 for osd planes
137 */
138 int delay;
Song Zhao6dce2672022-01-13 11:32:44 -0800139 /* delayed video for slow audio path, in ms */
140 int extra_delay;
Song Zhaoea5a0412021-01-18 16:40:08 -0800141};
142
bo.xiao5821fc72021-07-11 22:47:00 -0400143struct start_policy {
144 enum sync_start_policy policy;
145 /*timeout in ms */
146 int timeout;
147};
yongchun.li428390d2022-02-17 17:15:40 -0800148struct underflow_config {
149 int time_thresh; /* underflow check time threshold in ms */
150};
bo.xiao5821fc72021-07-11 22:47:00 -0400151
Song Zhaoea5a0412021-01-18 16:40:08 -0800152/* Open a new session and create the ID
153 * Params:
154 * @session_id: session ID allocated if success
155 * Return:
156 * >= 0 session handle. session_id is valid
157 * < 0 error, session_id is invalid
158 */
159int av_sync_open_session(int *session_id);
160
161/* Close session, will also decrease the ref cnt of session ID.
162 * Params:
163 * @session: Av sync session handle
164 */
165void av_sync_close_session(int session);
166
167/* Attach to kernel session. The returned avsync module will
168 * associated with @session_id. It is valid to create multi instance
169 * with the same session_id. For example, one for video, one for audio,
170 * one for PCR. But it is NOT valid to create 2 video session with the
171 * same session_id. Create session will increase the ref count of session
172 * ID.
Song Zhaoc03ba122020-12-23 21:54:02 -0800173 * Params:
174 * @session_id: unique AV sync session ID to bind audio and video
175 * usually get from kernel driver.
Song Zhao95bd0922021-09-21 14:07:46 -0700176 * If ID AV_SYNC_SESSION_V_MONO or bigger is assigned, only
177 * AV_SYNC_MODE_VIDEO_MONO is accepted.
Song Zhaoc03ba122020-12-23 21:54:02 -0800178 * @mode: AV sync mode of enum sync_mode
Song Zhaoea5a0412021-01-18 16:40:08 -0800179 * @type: AV sync type of enum sync_type. For a stream with both
180 * video and audio, two instances with each type should be created.
Song Zhaoc03ba122020-12-23 21:54:02 -0800181 * @start_thres: The start threshold of AV sync module. Set it to 0 for
182 * a default value. For low latency mode, set it to 1. Bigger
Song Zhaoea5a0412021-01-18 16:40:08 -0800183 * value will increase the delay of the first frame rendered.
Song Zhaoc03ba122020-12-23 21:54:02 -0800184 * AV sync will start when frame number reached threshold.
Song Zhaoc03ba122020-12-23 21:54:02 -0800185 * Return:
186 * null for failure, or handle for avsync module.
187 */
188void* av_sync_create(int session_id,
189 enum sync_mode mode,
Song Zhaoea5a0412021-01-18 16:40:08 -0800190 enum sync_type type,
191 int start_thres);
Song Zhaoc03ba122020-12-23 21:54:02 -0800192
Song Zhaoea5a0412021-01-18 16:40:08 -0800193
194/* Attach to an existed session. The returned avsync module will
195 * associated with @session_id. use av_sync_destroy to destroy it.
196 * Designed for audio path for now. Session created by audio client,
197 * and attached by audio HAL. Attach sesson will increase the ref count of
198 * session ID.
199 * Params:
200 * @session_id: unique AV sync session ID to bind audio and video
201 * usually get from kernel driver.
202 * @type: AV sync type of enum sync_type. For a stream with both
203 * video and audio, two instances with each type should be created.
204 * Return:
205 * null for failure, or handle for avsync module.
206 */
207
208void* av_sync_attach(int session_id,
209 enum sync_type type);
210
211/* Dettach from existed session. Decrease the ref count of session ID
212 * Params:
213 * @sync: handle for avsync module
214 */
Song Zhaoc03ba122020-12-23 21:54:02 -0800215void av_sync_destroy(void *sync);
216
Song Zhaoea5a0412021-01-18 16:40:08 -0800217int av_sync_video_config(void *sync, struct video_config* config);
218
219/* Set start policy
220 * Params:
221 * @sync: AV sync module handle
222 * @policy: start policy
223 * Return:
224 * 0 for OK, or error code
225 */
bo.xiao5821fc72021-07-11 22:47:00 -0400226int avs_sync_set_start_policy(void *sync, struct start_policy* st_policy);
Song Zhaoea5a0412021-01-18 16:40:08 -0800227
Song Zhaoc03ba122020-12-23 21:54:02 -0800228/* Pause/Resume AV sync module.
229 * It will return last frame in @av_sync_pop_frame() in pause state
230 * Params:
231 * @sync: AV sync module handle
232 * @pause: pause for true, or resume.
233 * Return:
234 * 0 for OK, or error code
235 */
236int av_sync_pause(void *sync, bool pause);
237
Song Zhaoea5a0412021-01-18 16:40:08 -0800238/* Push a new video frame to AV sync module
Song Zhaoc03ba122020-12-23 21:54:02 -0800239 * Params:
240 * @sync: AV sync module handle
241 * Return:
242 * 0 for OK, or error code
243 */
244int av_sync_push_frame(void *sync , struct vframe *frame);
245
Song Zhao95bd0922021-09-21 14:07:46 -0700246/* notify current system mono time for current VSYNC.
247 * This API should be VSYNC triggerd. Used only in VIDEO_MONO mode.
248 * Params:
249 * @sync: AV sync module handle
250 * @msys: system mono time of current VSYNC interrupt in nanosecond
251 * Usually returned by drmWaitVBlank.
252 * Return:
253 * 0 for OK, or error code
254 * */
255int av_sync_set_vsync_mono_time(void *sync , uint64_t msys);
256
Song Zhaoc03ba122020-12-23 21:54:02 -0800257/* Pop video frame for next VSYNC. This API should be VSYNC triggerd.
258 * Params:
259 * @sync: AV sync module handle
260 * Return:
261 * Old frame if current frame is hold
262 * New frame if it is time for a frame toggle.
263 * null if there is no frame to pop out (underrun).
264 * */
265struct vframe *av_sync_pop_frame(void *sync);
266
Song Zhaoea5a0412021-01-18 16:40:08 -0800267/* Audio start control. Audio render need to check return value and
Song Zhaod62bb392021-04-23 12:25:49 -0700268 * do sync or async start. NOT thread safe.
Song Zhaoc03ba122020-12-23 21:54:02 -0800269 * Params:
270 * @sync: AV sync module handle
Song Zhaoea5a0412021-01-18 16:40:08 -0800271 * @pts: first audio pts
272 * @delay: rendering delay
273 * @cb: callback to notify rendering start.
274 * @priv: parameter for cb
275 * Return:
276 * AV_SYNC_ASTART_SYNC, audio can start render ASAP. No need to wait for callback.
277 * AV_SYNC_ASTART_ASYNC, audio need to block until callback is triggered.
Song Zhaod62bb392021-04-23 12:25:49 -0700278 * AV_SYNC_ASTART_AGAIN, discard current audio chunk and call this API again.
Song Zhaoea5a0412021-01-18 16:40:08 -0800279 * AV_SYNC_ASTART_ERR, something bad happens
Song Zhaoc03ba122020-12-23 21:54:02 -0800280 */
Song Zhaoea5a0412021-01-18 16:40:08 -0800281avs_start_ret av_sync_audio_start(
282 void *sync,
283 pts90K pts,
284 pts90K delay,
285 audio_start_cb cb,
286 void *priv);
287
288/* Audio render policy. Indicate render action and the difference from ideal position
Song Zhaod62bb392021-04-23 12:25:49 -0700289 * NOT thread safe.
Song Zhaoea5a0412021-01-18 16:40:08 -0800290 * Params:
291 * @sync: AV sync module handle
292 * @pts: curent audio pts (considering delay)
293 * @policy: action field indicates the action.
294 * delta field indicates the diff from ideal position
295 * Return:
296 * 0 for OK, or error code
297 */
298int av_sync_audio_render(
299 void *sync,
300 pts90K pts,
301 struct audio_policy *policy);
Song Zhaoc03ba122020-12-23 21:54:02 -0800302
303/* set playback speed
304 * Currently only work for VMASTER mode
305 * Params:
306 * @speed: 1.0 is normal speed. 2.0 is 2x faster. 0.1 is 10x slower
307 * Minimium speed is 0.001
308 * Max speed is 100
309 * Return:
310 * 0 for OK, or error code
311 */
312int av_sync_set_speed(void *sync, float speed);
313
314/* switch avsync mode
Song Zhaoea5a0412021-01-18 16:40:08 -0800315 * PCR master/IPTV handle mode change automatically
Song Zhaoc03ba122020-12-23 21:54:02 -0800316 * Params:
317 * @sync: AV sync module handle
318 * @sync_mode: new mode
319 * Return:
320 * 0 for OK, or error code
321 */
322int av_sync_change_mode(void *sync, enum sync_mode mode);
323
Song Zhaoebc92012024-03-12 18:36:44 +0000324/* switch avsync mode by session id
325 * For Ad audio, it won't create avsync session, mode can be changed
326 * by session id.
327 * Params:
328 * @sync: AV sync module handle
329 * @sync_mode: new mode
330 * Return:
331 * 0 for OK, or error code
332 */
333int av_sync_change_mode_by_id(int id, enum sync_mode mode);
334
Song Zhao01031bb2021-05-13 21:23:20 -0700335/* get avsync mode
336 * Params:
337 * @sync: AV sync module handle
338 * @mode: pointer to return current mode
339 * Return:
340 * 0 for OK, or error code
341 */
342int av_sync_get_mode(void *sync, enum sync_mode *mode);
343
Song Zhaoc03ba122020-12-23 21:54:02 -0800344/* set pause PTS
345 * av sync will pause after reaching the assigned PTS or do step.
346 * Need to call @av_sync_pause(false) to resume a the playback.
347 * Params:
348 * @sync: AV sync module handle
349 * @pts: pause pts in uint of 90K.
350 * AV_SYNC_STEP_PAUSE_PTS: step one frame, and the callback
351 * will report pts value of the stepped frame.
352 * AV_SYNC_INVALID_PAUSE_PTS: disable this feature.
353 * Other value: play until the assigned PTS, and the callback
354 * will report pts value of the paused frame.
355 * Return:
356 * 0 for OK, or error code
357 */
358int av_sync_set_pause_pts(void *sync, pts90K pts);
359
360/* set pause PTS call back
361 * av sync will callback when pause PTS is reached with assigned PTS from
362 * @av_sync_set_pause_pts.
363 * Params:
364 * @sync: AV sync module handle
365 * @cb: callback function
366 * @priv: callback function parameter
367 * Return:
368 * 0 for OK, or error code
369 */
370int av_sync_set_pause_pts_cb(void *sync, pause_pts_done cb, void *priv);
Song Zhaoea5a0412021-01-18 16:40:08 -0800371
372/* Update PCR clock.
373 * Use by AV_SYNC_TYPE_PCR only.
374 * Params:
375 * @sync: AV sync module handle
376 * @pts: PCR clock
Song Zhaod62bb392021-04-23 12:25:49 -0700377 * @mono_clock: system monotonic clock receiving the pts
Song Zhaoea5a0412021-01-18 16:40:08 -0800378 * Return:
379 * 0 for OK, or error code
380 */
Song Zhaod62bb392021-04-23 12:25:49 -0700381int av_sync_set_pcr_clock(void *sync, pts90K pts, uint64_t mono_clock);
Song Zhaoea5a0412021-01-18 16:40:08 -0800382
Song Zhaod62bb392021-04-23 12:25:49 -0700383/* Get PCR clock pair.
384 * Use for clock recovery
385 * Params:
386 * @sync: AV sync module handle
387 * @pts: PCR clock pointer
388 * @mono_clock: system monotonic clock pointer receiving the pts
389 * Return:
390 * 0 for OK, or error code
391 */
392int av_sync_get_pcr_clock(void *sync, pts90K *pts, uint64_t *mono_clock);
Song Zhaoea5a0412021-01-18 16:40:08 -0800393
394/* get wall clock
395 * Params:
396 * @sync: AV sync module handle
397 * @pts: return wall clock
398 * Return:
399 * 0 for OK, or error code
400 */
401int av_sync_get_clock(void *sync, pts90K *pts);
402
Song Zhaof3350d32023-08-18 16:27:31 -0700403/* get render position and system mono raw time pair.
404 * pts type depends on sync_type of av_sync_create/av_sync_attach
405 * Params:
406 * @sync: AV sync module handle
407 * @pts: last rendered pts
408 * @mono_clock: CLOCK_MONOTONIC_RAW in nanosecond of the last rendered
409 * frame. 0 is invalid.
410 * Return:
411 * 0 for OK, or error code
412 */
413int av_sync_get_pos(void *sync, pts90K *pts, uint64_t *mono_clock);
414
Song Zhaoea5a0412021-01-18 16:40:08 -0800415/* set session name for debugging purpose
416 * The session name will be listed from /sys/class/aml_msync/list_session
417 * Params:
418 * @sync: AV sync module handle
419 * @name: name of current session.
420 * Return:
421 * 0 for OK, or error code
422 */
423int av_sync_set_session_name(void *sync, const char *name);
yongchun.li107a6162021-05-05 02:38:57 -0700424
425/* Start/finish audio seamless switch
426 * It declaims the coming audio seamless switch will happen.
427 * After removing the audio, the video will still play normally.
428 * After adding back the audio, it will sync with pipe-line without break the playing,
429 * only short audio break/mute on the switch is expected.
430 * Params:
431 * @sync: AV sync module handle
432 * @start: start the switch for true, or finish/reset for false
433 * Return:
434 * 0 for OK, or error code
435 */
436int av_sync_set_audio_switch(void *sync, bool start);
437
438/* Get the current audio seamless switch status.
439 * After setting the seamless switch, upper level can get the result by polling.
440 * The status will become false when the switching has finished or being termintated
441 * Params:
442 * @sync: AV sync module handle
443 * @status: start the switch for true, or finish/reset for false
444 * Return:
445 * 0 for OK, or error code
446 */
447int av_sync_get_audio_switch(void *sync, bool *status);
448
Song Zhao623e2f12021-09-03 15:54:04 -0700449/* Get the current clock deviation between PCR clock and system monotonic clock.
Song Zhao8039f562021-05-18 18:11:25 -0700450 * Params:
451 * @sync: AV sync module handle
452 * @ppm: part per million. Bigger than 0 is PCR master clock is faster. Less than 0
453 * if PCR master clock is slower.
454 * Return:
455 * CLK_RECOVERY_NOT_RUNNING: current mode doesn't support clock recovery
456 * CLK_RECOVERY_ONGOING: still ongoing, need more time to converge.
457 * CLK_RECOVERY_READY: clock recovery result is ready to be used. The result might
458 * be updated later for better accuracy.
wei.dubcc2ed22021-05-19 07:16:10 -0400459 * CLK_RECOVERY_ERR: error happens
Song Zhao8039f562021-05-18 18:11:25 -0700460 */
Song Zhao623e2f12021-09-03 15:54:04 -0700461enum clock_recovery_stat av_sync_get_clock_deviation(void *sync, int32_t *ppm);
yongchun.li428390d2022-02-17 17:15:40 -0800462
463/* set underflow detect call back
464 * av sync will callback when a buffer underflow detected when normal play
465 * Params:
466 * @sync: AV sync module handle
467 * @cb: callback function
468 * @priv: callback function parameter
469 * @cfg: the configuration of the call back, NULL use default value.
470 * Return:
471 * 0 for OK, or error code
472 */
473int av_sync_set_underflow_check_cb(void *sync, underflow_detected cb, void *priv, struct underflow_config *cfg);
Song Zhao6183ca92022-07-29 09:57:03 -0700474
475/* Cancel audio waiting.
476 * When AV_SYNC_ASTART_ASYNC blocks a thread, use this API to unblock it.
477 * audio_start_cb will be triggered with AV_SYNC_ASCB_STOP.
478 * Params:
479 * @sync: AV sync module handle
480 * Return:
481 * 0 for OK, or error code
482 */
483int avs_sync_stop_audio(void *sync);
bo.xiao05ba1ac2022-12-13 14:38:13 +0800484
485/* eos from video decoder.
486 * when video decoder eos, use this API to set eos to msync.
487 * Only effect video.
488 * Params:
489 * @sync: AV sync module handle
490 * Return:
491 * 0 for OK, or error code
492 */
493int avs_sync_set_eos(void *sync);
Song Zhaoc03ba122020-12-23 21:54:02 -0800494#endif