Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 19 | enum sync_mode { |
| 20 | AV_SYNC_MODE_VMASTER = 0, |
| 21 | AV_SYNC_MODE_AMASTER = 1, |
| 22 | AV_SYNC_MODE_PCR_MASTER = 2, |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 23 | AV_SYNC_MODE_IPTV = 3, |
| 24 | AV_SYNC_MODE_FREE_RUN = 4, |
| 25 | AV_SYNC_MODE_MAX |
| 26 | }; |
| 27 | |
| 28 | enum sync_type { |
| 29 | AV_SYNC_TYPE_AUDIO, |
| 30 | AV_SYNC_TYPE_VIDEO, |
| 31 | AV_SYNC_TYPE_PCR, |
| 32 | AV_SYNC_TYPE_MAX |
| 33 | }; |
| 34 | |
| 35 | enum 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 Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | #define AV_SYNC_INVALID_PAUSE_PTS 0xFFFFFFFF |
| 46 | #define AV_SYNC_STEP_PAUSE_PTS 0xFFFFFFFE |
| 47 | |
| 48 | typedef uint32_t pts90K; |
| 49 | struct vframe; |
| 50 | typedef void (*free_frame)(struct vframe * frame); |
| 51 | typedef void (*pause_pts_done)(uint32_t pts, void* priv); |
| 52 | |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 53 | typedef 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 | |
| 60 | typedef int (*audio_start_cb)(void *priv, avs_ascb_reason reason); |
| 61 | |
| 62 | typedef enum { |
| 63 | AV_SYNC_ASTART_SYNC = 0, |
| 64 | AV_SYNC_ASTART_ASYNC, |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 65 | AV_SYNC_ASTART_AGAIN, |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 66 | AV_SYNC_ASTART_ERR |
| 67 | } avs_start_ret; |
| 68 | |
| 69 | typedef enum { |
| 70 | /* render ASAP */ |
| 71 | AV_SYNC_AA_RENDER, |
| 72 | /* late, drop to catch up */ |
| 73 | AV_SYNC_AA_DROP, |
| 74 | /* early, insert to hold */ |
| 75 | AV_SYNC_AA_INSERT, |
| 76 | AA_SYNC_AA_MAX |
| 77 | } avs_audio_action; |
| 78 | |
| 79 | struct audio_policy { |
| 80 | avs_audio_action action; |
| 81 | /* delta between apts and ideal render position |
| 82 | * positive means apts is behind wall clock |
| 83 | * negative means apts is ahead of wall clock |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 84 | * 0 is only valid for AV_SYNC_AA_DROP, drop current chunk |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 85 | */ |
| 86 | int delta; |
| 87 | }; |
| 88 | |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 89 | struct vframe { |
| 90 | /* private user data */ |
| 91 | void *private; |
| 92 | pts90K pts; |
| 93 | /* duration of this frame. 0 for invalid value */ |
| 94 | pts90K duration; |
| 95 | /* free function, will be called when multi frames are |
| 96 | * toggled in a single VSYNC, on frames not for display. |
| 97 | * For the last toggled frame, free won't be called. Caller |
| 98 | * of av_sync_pop_frame() are responsible for free poped frame. |
| 99 | * For example, if frame 1/2/3 are toggled in a single VSYCN, |
| 100 | * free() of 1/2 will be called, but free() of 3 won't. |
| 101 | */ |
| 102 | free_frame free; |
| 103 | |
| 104 | //For internal usage under this line |
| 105 | /*holding period */ |
| 106 | int hold_period; |
| 107 | }; |
| 108 | |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 109 | struct video_config { |
| 110 | /* AV sync delay. The delay of render. |
| 111 | * For video in VSYNC number unit: |
| 112 | * 2 for video planes |
| 113 | * 1 for osd planes |
| 114 | */ |
| 115 | int delay; |
| 116 | }; |
| 117 | |
| 118 | /* Open a new session and create the ID |
| 119 | * Params: |
| 120 | * @session_id: session ID allocated if success |
| 121 | * Return: |
| 122 | * >= 0 session handle. session_id is valid |
| 123 | * < 0 error, session_id is invalid |
| 124 | */ |
| 125 | int av_sync_open_session(int *session_id); |
| 126 | |
| 127 | /* Close session, will also decrease the ref cnt of session ID. |
| 128 | * Params: |
| 129 | * @session: Av sync session handle |
| 130 | */ |
| 131 | void av_sync_close_session(int session); |
| 132 | |
| 133 | /* Attach to kernel session. The returned avsync module will |
| 134 | * associated with @session_id. It is valid to create multi instance |
| 135 | * with the same session_id. For example, one for video, one for audio, |
| 136 | * one for PCR. But it is NOT valid to create 2 video session with the |
| 137 | * same session_id. Create session will increase the ref count of session |
| 138 | * ID. |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 139 | * Params: |
| 140 | * @session_id: unique AV sync session ID to bind audio and video |
| 141 | * usually get from kernel driver. |
| 142 | * @mode: AV sync mode of enum sync_mode |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 143 | * @type: AV sync type of enum sync_type. For a stream with both |
| 144 | * video and audio, two instances with each type should be created. |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 145 | * @start_thres: The start threshold of AV sync module. Set it to 0 for |
| 146 | * a default value. For low latency mode, set it to 1. Bigger |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 147 | * value will increase the delay of the first frame rendered. |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 148 | * AV sync will start when frame number reached threshold. |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 149 | * Return: |
| 150 | * null for failure, or handle for avsync module. |
| 151 | */ |
| 152 | void* av_sync_create(int session_id, |
| 153 | enum sync_mode mode, |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 154 | enum sync_type type, |
| 155 | int start_thres); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 156 | |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 157 | |
| 158 | /* Attach to an existed session. The returned avsync module will |
| 159 | * associated with @session_id. use av_sync_destroy to destroy it. |
| 160 | * Designed for audio path for now. Session created by audio client, |
| 161 | * and attached by audio HAL. Attach sesson will increase the ref count of |
| 162 | * session ID. |
| 163 | * Params: |
| 164 | * @session_id: unique AV sync session ID to bind audio and video |
| 165 | * usually get from kernel driver. |
| 166 | * @type: AV sync type of enum sync_type. For a stream with both |
| 167 | * video and audio, two instances with each type should be created. |
| 168 | * Return: |
| 169 | * null for failure, or handle for avsync module. |
| 170 | */ |
| 171 | |
| 172 | void* av_sync_attach(int session_id, |
| 173 | enum sync_type type); |
| 174 | |
| 175 | /* Dettach from existed session. Decrease the ref count of session ID |
| 176 | * Params: |
| 177 | * @sync: handle for avsync module |
| 178 | */ |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 179 | void av_sync_destroy(void *sync); |
| 180 | |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 181 | int av_sync_video_config(void *sync, struct video_config* config); |
| 182 | |
| 183 | /* Set start policy |
| 184 | * Params: |
| 185 | * @sync: AV sync module handle |
| 186 | * @policy: start policy |
| 187 | * Return: |
| 188 | * 0 for OK, or error code |
| 189 | */ |
| 190 | int avs_sync_set_start_policy(void *sync, enum sync_start_policy policy); |
| 191 | |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 192 | /* Pause/Resume AV sync module. |
| 193 | * It will return last frame in @av_sync_pop_frame() in pause state |
| 194 | * Params: |
| 195 | * @sync: AV sync module handle |
| 196 | * @pause: pause for true, or resume. |
| 197 | * Return: |
| 198 | * 0 for OK, or error code |
| 199 | */ |
| 200 | int av_sync_pause(void *sync, bool pause); |
| 201 | |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 202 | /* Push a new video frame to AV sync module |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 203 | * Params: |
| 204 | * @sync: AV sync module handle |
| 205 | * Return: |
| 206 | * 0 for OK, or error code |
| 207 | */ |
| 208 | int av_sync_push_frame(void *sync , struct vframe *frame); |
| 209 | |
| 210 | /* Pop video frame for next VSYNC. This API should be VSYNC triggerd. |
| 211 | * Params: |
| 212 | * @sync: AV sync module handle |
| 213 | * Return: |
| 214 | * Old frame if current frame is hold |
| 215 | * New frame if it is time for a frame toggle. |
| 216 | * null if there is no frame to pop out (underrun). |
| 217 | * */ |
| 218 | struct vframe *av_sync_pop_frame(void *sync); |
| 219 | |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 220 | /* Audio start control. Audio render need to check return value and |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 221 | * do sync or async start. NOT thread safe. |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 222 | * Params: |
| 223 | * @sync: AV sync module handle |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 224 | * @pts: first audio pts |
| 225 | * @delay: rendering delay |
| 226 | * @cb: callback to notify rendering start. |
| 227 | * @priv: parameter for cb |
| 228 | * Return: |
| 229 | * AV_SYNC_ASTART_SYNC, audio can start render ASAP. No need to wait for callback. |
| 230 | * AV_SYNC_ASTART_ASYNC, audio need to block until callback is triggered. |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 231 | * AV_SYNC_ASTART_AGAIN, discard current audio chunk and call this API again. |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 232 | * AV_SYNC_ASTART_ERR, something bad happens |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 233 | */ |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 234 | avs_start_ret av_sync_audio_start( |
| 235 | void *sync, |
| 236 | pts90K pts, |
| 237 | pts90K delay, |
| 238 | audio_start_cb cb, |
| 239 | void *priv); |
| 240 | |
| 241 | /* Audio render policy. Indicate render action and the difference from ideal position |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 242 | * NOT thread safe. |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 243 | * Params: |
| 244 | * @sync: AV sync module handle |
| 245 | * @pts: curent audio pts (considering delay) |
| 246 | * @policy: action field indicates the action. |
| 247 | * delta field indicates the diff from ideal position |
| 248 | * Return: |
| 249 | * 0 for OK, or error code |
| 250 | */ |
| 251 | int av_sync_audio_render( |
| 252 | void *sync, |
| 253 | pts90K pts, |
| 254 | struct audio_policy *policy); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 255 | |
| 256 | /* set playback speed |
| 257 | * Currently only work for VMASTER mode |
| 258 | * Params: |
| 259 | * @speed: 1.0 is normal speed. 2.0 is 2x faster. 0.1 is 10x slower |
| 260 | * Minimium speed is 0.001 |
| 261 | * Max speed is 100 |
| 262 | * Return: |
| 263 | * 0 for OK, or error code |
| 264 | */ |
| 265 | int av_sync_set_speed(void *sync, float speed); |
| 266 | |
| 267 | /* switch avsync mode |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 268 | * PCR master/IPTV handle mode change automatically |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 269 | * Params: |
| 270 | * @sync: AV sync module handle |
| 271 | * @sync_mode: new mode |
| 272 | * Return: |
| 273 | * 0 for OK, or error code |
| 274 | */ |
| 275 | int av_sync_change_mode(void *sync, enum sync_mode mode); |
| 276 | |
Song Zhao | 01031bb | 2021-05-13 21:23:20 -0700 | [diff] [blame^] | 277 | /* get avsync mode |
| 278 | * Params: |
| 279 | * @sync: AV sync module handle |
| 280 | * @mode: pointer to return current mode |
| 281 | * Return: |
| 282 | * 0 for OK, or error code |
| 283 | */ |
| 284 | int av_sync_get_mode(void *sync, enum sync_mode *mode); |
| 285 | |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 286 | /* set pause PTS |
| 287 | * av sync will pause after reaching the assigned PTS or do step. |
| 288 | * Need to call @av_sync_pause(false) to resume a the playback. |
| 289 | * Params: |
| 290 | * @sync: AV sync module handle |
| 291 | * @pts: pause pts in uint of 90K. |
| 292 | * AV_SYNC_STEP_PAUSE_PTS: step one frame, and the callback |
| 293 | * will report pts value of the stepped frame. |
| 294 | * AV_SYNC_INVALID_PAUSE_PTS: disable this feature. |
| 295 | * Other value: play until the assigned PTS, and the callback |
| 296 | * will report pts value of the paused frame. |
| 297 | * Return: |
| 298 | * 0 for OK, or error code |
| 299 | */ |
| 300 | int av_sync_set_pause_pts(void *sync, pts90K pts); |
| 301 | |
| 302 | /* set pause PTS call back |
| 303 | * av sync will callback when pause PTS is reached with assigned PTS from |
| 304 | * @av_sync_set_pause_pts. |
| 305 | * Params: |
| 306 | * @sync: AV sync module handle |
| 307 | * @cb: callback function |
| 308 | * @priv: callback function parameter |
| 309 | * Return: |
| 310 | * 0 for OK, or error code |
| 311 | */ |
| 312 | int av_sync_set_pause_pts_cb(void *sync, pause_pts_done cb, void *priv); |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 313 | |
| 314 | /* Update PCR clock. |
| 315 | * Use by AV_SYNC_TYPE_PCR only. |
| 316 | * Params: |
| 317 | * @sync: AV sync module handle |
| 318 | * @pts: PCR clock |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 319 | * @mono_clock: system monotonic clock receiving the pts |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 320 | * Return: |
| 321 | * 0 for OK, or error code |
| 322 | */ |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 323 | int av_sync_set_pcr_clock(void *sync, pts90K pts, uint64_t mono_clock); |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 324 | |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 325 | /* Get PCR clock pair. |
| 326 | * Use for clock recovery |
| 327 | * Params: |
| 328 | * @sync: AV sync module handle |
| 329 | * @pts: PCR clock pointer |
| 330 | * @mono_clock: system monotonic clock pointer receiving the pts |
| 331 | * Return: |
| 332 | * 0 for OK, or error code |
| 333 | */ |
| 334 | int av_sync_get_pcr_clock(void *sync, pts90K *pts, uint64_t *mono_clock); |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 335 | |
| 336 | /* get wall clock |
| 337 | * Params: |
| 338 | * @sync: AV sync module handle |
| 339 | * @pts: return wall clock |
| 340 | * Return: |
| 341 | * 0 for OK, or error code |
| 342 | */ |
| 343 | int av_sync_get_clock(void *sync, pts90K *pts); |
| 344 | |
| 345 | /* set session name for debugging purpose |
| 346 | * The session name will be listed from /sys/class/aml_msync/list_session |
| 347 | * Params: |
| 348 | * @sync: AV sync module handle |
| 349 | * @name: name of current session. |
| 350 | * Return: |
| 351 | * 0 for OK, or error code |
| 352 | */ |
| 353 | int av_sync_set_session_name(void *sync, const char *name); |
yongchun.li | 107a616 | 2021-05-05 02:38:57 -0700 | [diff] [blame] | 354 | |
| 355 | /* Start/finish audio seamless switch |
| 356 | * It declaims the coming audio seamless switch will happen. |
| 357 | * After removing the audio, the video will still play normally. |
| 358 | * After adding back the audio, it will sync with pipe-line without break the playing, |
| 359 | * only short audio break/mute on the switch is expected. |
| 360 | * Params: |
| 361 | * @sync: AV sync module handle |
| 362 | * @start: start the switch for true, or finish/reset for false |
| 363 | * Return: |
| 364 | * 0 for OK, or error code |
| 365 | */ |
| 366 | int av_sync_set_audio_switch(void *sync, bool start); |
| 367 | |
| 368 | /* Get the current audio seamless switch status. |
| 369 | * After setting the seamless switch, upper level can get the result by polling. |
| 370 | * The status will become false when the switching has finished or being termintated |
| 371 | * Params: |
| 372 | * @sync: AV sync module handle |
| 373 | * @status: start the switch for true, or finish/reset for false |
| 374 | * Return: |
| 375 | * 0 for OK, or error code |
| 376 | */ |
| 377 | int av_sync_get_audio_switch(void *sync, bool *status); |
| 378 | |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 379 | #endif |