summaryrefslogtreecommitdiff
path: root/audio_hwsync.h (plain)
blob: 9330c6b4e92c3e9eb447a289e830478318c9f066
1#ifndef _AUDIO_HWSYNC_H_
2#define _AUDIO_HWSYNC_H_
3
4#include <stdbool.h>
5
6#define TSYNC_FIRSTAPTS "/sys/class/tsync/firstapts"
7#define TSYNC_PCRSCR "/sys/class/tsync/pts_pcrscr"
8#define TSYNC_EVENT "/sys/class/tsync/event"
9#define TSYNC_APTS "/sys/class/tsync/pts_audio"
10#define SYSTIME_CORRECTION_THRESHOLD (90000*10/100)
11#define NSEC_PER_SECOND 1000000000ULL
12#define HW_SYNC_STATE_HEADER 0
13#define HW_SYNC_STATE_BODY 1
14#define HW_SYNC_STATE_RESYNC 2
15#define APTS_DISCONTINUE_THRESHOLD (90000)
16#define APTS_DISCONTINUE_THRESHOLD_MIN (90000/1000*100)
17#define APTS_DISCONTINUE_THRESHOLD_MAX (5*90000)
18
19typedef struct audio_hwsync {
20 uint8_t hw_sync_header[16];
21 size_t hw_sync_header_cnt;
22 int hw_sync_state;
23 size_t hw_sync_body_cnt;
24 int hw_sync_frame_size;
25 uint8_t hw_sync_body_buf[4096];
26 uint8_t body_align[64];
27 uint8_t body_align_cnt;
28 bool first_apts_flag;//flag to indicate set first apts
29 uint64_t first_apts;
30 uint64_t last_apts_from_header;
31} audio_hwsync_t;
32static inline bool hwsync_header_valid(uint8_t *header)
33{
34 return (header[0] == 0x55) &&
35 (header[1] == 0x55) &&
36 (header[2] == 0x00) &&
37 (header[3] == 0x01);
38}
39
40static inline uint64_t hwsync_header_get_pts(uint8_t *header)
41{
42 return (((uint64_t)header[8]) << 56) |
43 (((uint64_t)header[9]) << 48) |
44 (((uint64_t)header[10]) << 40) |
45 (((uint64_t)header[11]) << 32) |
46 (((uint64_t)header[12]) << 24) |
47 (((uint64_t)header[13]) << 16) |
48 (((uint64_t)header[14]) << 8) |
49 ((uint64_t)header[15]);
50}
51
52static inline uint32_t hwsync_header_get_size(uint8_t *header)
53{
54 return (((uint32_t)header[4]) << 24) |
55 (((uint32_t)header[5]) << 16) |
56 (((uint32_t)header[6]) << 8) |
57 ((uint32_t)header[7]);
58}
59
60static inline uint64_t get_pts_gap(uint64_t a, uint64_t b)
61{
62 if (a >= b)
63 return (a - b);
64 else
65 return (b - a);
66}
67
68void aml_audio_hwsync_init(audio_hwsync_t *p_hwsync);
69int aml_audio_hwsync_find_frame(audio_hwsync_t *p_hwsync,
70 const void *in_buffer, size_t in_bytes, uint64_t *cur_pts, int *outsize);
71int aml_audio_hwsync_set_first_pts(audio_hwsync_t *p_hwsync, uint64_t pts);
72#endif
73