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