blob: 09c36262211a399d4ed16363205e1c09491b3a21
1 | /************************************************************************************* |
2 | * @file pcmenc_api.c |
3 | * @brief |
4 | * |
5 | * Detailed description starts here. |
6 | * |
7 | * @author jian.xu, jian.xu@amlogic.com |
8 | * |
9 | * @internal |
10 | * Created 20124-26 |
11 | * Revision v1.0 |
12 | * Compiler gcc/g++ |
13 | * Company Amlogic Inc. |
14 | * Copyright Copyright (c) 2011, herbert.hu |
15 | * |
16 | * This source code is released for free distribution under the terms of the |
17 | * GNU General Public License as published by the Free Software Foundation. |
18 | ************************************************************************************* |
19 | */ |
20 | #include <stdio.h> |
21 | #include <unistd.h> |
22 | #include <stdlib.h> |
23 | #include <sys/types.h> |
24 | #include <sys/stat.h> |
25 | #include <sys/ioctl.h> |
26 | #include <signal.h> |
27 | #include <errno.h> |
28 | #include <fcntl.h> |
29 | #include <string.h> |
30 | #include <sys/mman.h> |
31 | #include "pcmenc_api.h" |
32 | #define AUDIODSP_PCMENC_DEV_NAME "/dev/audiodsp_pcmenc" |
33 | |
34 | static char *map_buf = (void *)-1L; |
35 | static unsigned read_offset = 0; |
36 | static unsigned buffer_size = 0; |
37 | static int dev_fd = -1; |
38 | #include <log-print.h> |
39 | int pcmenc_init() |
40 | { |
41 | buffer_size = 0; |
42 | read_offset = 0; |
43 | dev_fd = -1; |
44 | dev_fd = open(AUDIODSP_PCMENC_DEV_NAME, O_RDONLY); |
45 | if (dev_fd < 0) { |
46 | //printf("can not open %s\n", AUDIODSP_PCMENC_DEV_NAME); |
47 | adec_print("can not open %s\n", AUDIODSP_PCMENC_DEV_NAME); |
48 | return -1; |
49 | } |
50 | ioctl(dev_fd, AUDIODSP_PCMENC_GET_RING_BUF_SIZE, &buffer_size); |
51 | /* mapping the kernel buffer to user space to acess */ |
52 | map_buf = mmap(0, buffer_size, PROT_READ , MAP_PRIVATE, dev_fd, 0); |
53 | if (map_buf == (void*)-1L) { |
54 | //printf("pcmenc:mmap failed,err id %d \n",errno); |
55 | adec_print("pcmenc:mmap failed,err id %d \n", errno); |
56 | close(dev_fd); |
57 | return -1; |
58 | } |
59 | return 0; |
60 | } |
61 | static unsigned pcm_read_num = 0; |
62 | static int pcmenc_skip_pcm(int size) |
63 | { |
64 | int ring_buf_content = 0; |
65 | int len = 0; |
66 | int tail = 0; |
67 | ioctl(dev_fd, AUDIODSP_PCMENC_GET_RING_BUF_CONTENT, &ring_buf_content); |
68 | if (ring_buf_content > size) { |
69 | if (read_offset + size > buffer_size) { |
70 | //tail = size - read_offset; |
71 | tail = buffer_size - read_offset; |
72 | // memcpy(inputbuf,map_buf+read_offset,tail); |
73 | read_offset = 0; |
74 | // memcpy(inputbuf+tail,map_buf+read_offset,size-tail); |
75 | read_offset = size - tail; |
76 | } else { |
77 | // memcpy(inputbuf,map_buf+read_offset,size); |
78 | read_offset += size; |
79 | |
80 | } |
81 | pcm_read_num += size; |
82 | ioctl(dev_fd, AUDIODSP_PCMENC_SET_RING_BUF_RPTR, read_offset); |
83 | return size; |
84 | } else { |
85 | return 0; |
86 | } |
87 | |
88 | } |
89 | int pcmenc_read_pcm(char *inputbuf, uint size) |
90 | { |
91 | unsigned int ring_buf_content = 0; |
92 | int len = 0; |
93 | int tail = 0; |
94 | ioctl(dev_fd, AUDIODSP_PCMENC_GET_RING_BUF_CONTENT, &ring_buf_content); |
95 | if (ring_buf_content > buffer_size * 4 / 5) { |
96 | pcmenc_skip_pcm(size * 4); |
97 | memset(inputbuf, 0, size); |
98 | adec_print("pcmenc buffer full,skip %d bytes \n", 4 * size); |
99 | return size; |
100 | |
101 | //ioctl(dev_fd, AUDIODSP_PCMENC_GET_RING_BUF_CONTENT, &ring_buf_content); |
102 | } |
103 | //adec_print("read num %d,countent %d,total %d\n",pcm_read_num,ring_buf_content,pcm_read_num+ring_buf_content); |
104 | if (ring_buf_content > size) { |
105 | if (read_offset + size > buffer_size) { |
106 | //tail = size - read_offset; |
107 | tail = buffer_size - read_offset; |
108 | memcpy(inputbuf, map_buf + read_offset, tail); |
109 | read_offset = 0; |
110 | memcpy(inputbuf + tail, map_buf + read_offset, size - tail); |
111 | read_offset = size - tail; |
112 | } else { |
113 | memcpy(inputbuf, map_buf + read_offset, size); |
114 | read_offset += size; |
115 | |
116 | } |
117 | pcm_read_num += size; |
118 | ioctl(dev_fd, AUDIODSP_PCMENC_SET_RING_BUF_RPTR, read_offset); |
119 | return size; |
120 | } else { |
121 | return 0; |
122 | } |
123 | } |
124 | int pcmenc_get_pcm_info(pcm51_encoded_info_t *info) |
125 | { |
126 | int ret; |
127 | ret = ioctl(dev_fd, AUDIODSP_PCMENC_GET_PCMINFO, info); |
128 | if (ret) { |
129 | return ret; |
130 | } |
131 | adec_print("InfoValidFlag %d,SampFs %d,NumCh %d,AcMode %d,LFEFlag %d,BitsPerSamp %d \n", \ |
132 | info->InfoValidFlag, info->SampFs, info->NumCh, info->AcMode, info->LFEFlag, info->BitsPerSamp); |
133 | return 0; |
134 | |
135 | } |
136 | int pcmenc_deinit() |
137 | { |
138 | pcm_read_num = 0; |
139 | |
140 | if (map_buf != (void *)-1L) { |
141 | munmap(map_buf, buffer_size); |
142 | } |
143 | if (dev_fd >= 0) { |
144 | close(dev_fd); |
145 | } |
146 | return 0; |
147 | } |