summaryrefslogtreecommitdiff
path: root/audio_hwsync.h (plain)
blob: c78cab026603fe46d9c2b656924133114dc68c58
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 APTS_DISCONTINUE_THRESHOLD (90000)
34#define APTS_DISCONTINUE_THRESHOLD_MIN (90000/1000*100)
35#define APTS_DISCONTINUE_THRESHOLD_MAX (5*90000)
36
37typedef struct audio_hwsync {
38 uint8_t hw_sync_header[16];
39 size_t hw_sync_header_cnt;
40 int hw_sync_state;
41 size_t hw_sync_body_cnt;
42 int hw_sync_frame_size;
43 uint8_t hw_sync_body_buf[4096];
44 uint8_t body_align[64];
45 uint8_t body_align_cnt;
46 bool first_apts_flag;//flag to indicate set first apts
47 uint64_t first_apts;
48 uint64_t last_apts_from_header;
49} audio_hwsync_t;
50static inline bool hwsync_header_valid(uint8_t *header)
51{
52 return (header[0] == 0x55) &&
53 (header[1] == 0x55) &&
54 (header[2] == 0x00) &&
55 (header[3] == 0x01);
56}
57
58static inline uint64_t hwsync_header_get_pts(uint8_t *header)
59{
60 return (((uint64_t)header[8]) << 56) |
61 (((uint64_t)header[9]) << 48) |
62 (((uint64_t)header[10]) << 40) |
63 (((uint64_t)header[11]) << 32) |
64 (((uint64_t)header[12]) << 24) |
65 (((uint64_t)header[13]) << 16) |
66 (((uint64_t)header[14]) << 8) |
67 ((uint64_t)header[15]);
68}
69
70static inline uint32_t hwsync_header_get_size(uint8_t *header)
71{
72 return (((uint32_t)header[4]) << 24) |
73 (((uint32_t)header[5]) << 16) |
74 (((uint32_t)header[6]) << 8) |
75 ((uint32_t)header[7]);
76}
77
78static inline uint64_t get_pts_gap(uint64_t a, uint64_t b)
79{
80 if (a >= b)
81 return (a - b);
82 else
83 return (b - a);
84}
85
86void aml_audio_hwsync_init(audio_hwsync_t *p_hwsync);
87int aml_audio_hwsync_find_frame(audio_hwsync_t *p_hwsync,
88 const void *in_buffer, size_t in_bytes, uint64_t *cur_pts, int *outsize);
89int aml_audio_hwsync_set_first_pts(audio_hwsync_t *p_hwsync, uint64_t pts);
90#endif
91