blob: 36177d77db13cdea7af0d680ea0302736763bfa5
1 | /************************************************** |
2 | * example based on amcodec |
3 | **************************************************/ |
4 | #include <sys/types.h> |
5 | #include <sys/stat.h> |
6 | #include <fcntl.h> |
7 | #include <stdio.h> |
8 | #include <stdlib.h> |
9 | #include <string.h> |
10 | #include <signal.h> |
11 | #include <errno.h> |
12 | #include <codec.h> |
13 | #include <stdbool.h> |
14 | #include <ctype.h> |
15 | #include <unistd.h> |
16 | |
17 | |
18 | |
19 | #define READ_SIZE (64 * 1024) |
20 | #define EXTERNAL_PTS (1) |
21 | #define SYNC_OUTSIDE (2) |
22 | #define UNIT_FREQ 96000 |
23 | #define PTS_FREQ 90000 |
24 | #define AV_SYNC_THRESH PTS_FREQ*30 |
25 | |
26 | static codec_para_t v_codec_para; |
27 | static codec_para_t a_codec_para; |
28 | static codec_para_t *pcodec, *apcodec, *vpcodec; |
29 | static char *filename; |
30 | FILE* fp = NULL; |
31 | static int axis[8] = {0}; |
32 | |
33 | int osd_blank(char *path, int cmd) |
34 | { |
35 | int fd; |
36 | char bcmd[16]; |
37 | fd = open(path, O_CREAT | O_RDWR | O_TRUNC, 0644); |
38 | |
39 | if (fd >= 0) { |
40 | sprintf(bcmd, "%d", cmd); |
41 | write(fd, bcmd, strlen(bcmd)); |
42 | close(fd); |
43 | return 0; |
44 | } |
45 | |
46 | return -1; |
47 | } |
48 | |
49 | int set_tsync_enable(int enable) |
50 | { |
51 | int fd; |
52 | char *path = "/sys/class/tsync/enable"; |
53 | char bcmd[16]; |
54 | fd = open(path, O_CREAT | O_RDWR | O_TRUNC, 0644); |
55 | if (fd >= 0) { |
56 | sprintf(bcmd, "%d", enable); |
57 | write(fd, bcmd, strlen(bcmd)); |
58 | close(fd); |
59 | return 0; |
60 | } |
61 | |
62 | return -1; |
63 | } |
64 | |
65 | int parse_para(const char *para, int para_num, int *result) |
66 | { |
67 | char *endp; |
68 | const char *startp = para; |
69 | int *out = result; |
70 | int len = 0, count = 0; |
71 | |
72 | if (!startp) { |
73 | return 0; |
74 | } |
75 | |
76 | len = strlen(startp); |
77 | |
78 | do { |
79 | //filter space out |
80 | while (startp && (isspace(*startp) || !isgraph(*startp)) && len) { |
81 | startp++; |
82 | len--; |
83 | } |
84 | |
85 | if (len == 0) { |
86 | break; |
87 | } |
88 | |
89 | *out++ = strtol(startp, &endp, 0); |
90 | |
91 | len -= endp - startp; |
92 | startp = endp; |
93 | count++; |
94 | |
95 | } while ((endp) && (count < para_num) && (len > 0)); |
96 | |
97 | return count; |
98 | } |
99 | |
100 | int set_display_axis(int recovery) |
101 | { |
102 | int fd; |
103 | char *path = "/sys/class/display/axis"; |
104 | char str[128]; |
105 | int count, i; |
106 | fd = open(path, O_CREAT | O_RDWR | O_TRUNC, 0644); |
107 | if (fd >= 0) { |
108 | if (!recovery) { |
109 | read(fd, str, 128); |
110 | printf("read axis %s, length %zu\n", str, strlen(str)); |
111 | count = parse_para(str, 8, axis); |
112 | } |
113 | if (recovery) { |
114 | sprintf(str, "%d %d %d %d %d %d %d %d", |
115 | axis[0], axis[1], axis[2], axis[3], axis[4], axis[5], axis[6], axis[7]); |
116 | } else { |
117 | sprintf(str, "2048 %d %d %d %d %d %d %d", |
118 | axis[1], axis[2], axis[3], axis[4], axis[5], axis[6], axis[7]); |
119 | } |
120 | write(fd, str, strlen(str)); |
121 | close(fd); |
122 | return 0; |
123 | } |
124 | |
125 | return -1; |
126 | } |
127 | |
128 | static void signal_handler(int signum) |
129 | { |
130 | printf("Get signum=%x\n", signum); |
131 | codec_close(apcodec); |
132 | codec_close(vpcodec); |
133 | fclose(fp); |
134 | set_display_axis(1); |
135 | signal(signum, SIG_DFL); |
136 | raise(signum); |
137 | } |
138 | |
139 | int main(int argc, char *argv[]) |
140 | { |
141 | int ret = CODEC_ERROR_NONE; |
142 | char buffer[READ_SIZE]; |
143 | |
144 | int len = 0; |
145 | int size = READ_SIZE; |
146 | uint32_t Readlen; |
147 | uint32_t isize; |
148 | struct buf_status vbuf; |
149 | |
150 | if (argc < 6) { |
151 | printf("Corret command: esplayer <filename> <width> <height> <fps> <format(1:mpeg4 2:h264 6:vc1)> [subformat for mpeg4/vc1]\n"); |
152 | return -1; |
153 | } |
154 | osd_blank("/sys/class/graphics/fb0/blank", 1); |
155 | osd_blank("/sys/class/graphics/fb1/blank", 0); |
156 | set_display_axis(0); |
157 | #ifdef AUDIO_ES |
158 | apcodec = &a_codec_para; |
159 | memset(apcodec, 0, sizeof(codec_para_t)); |
160 | #endif |
161 | |
162 | vpcodec = &v_codec_para; |
163 | memset(vpcodec, 0, sizeof(codec_para_t)); |
164 | |
165 | vpcodec->has_video = 1; |
166 | vpcodec->video_type = atoi(argv[5]); |
167 | if (vpcodec->video_type == VFORMAT_H264) { |
168 | vpcodec->am_sysinfo.format = VIDEO_DEC_FORMAT_H264; |
169 | vpcodec->am_sysinfo.param = (void *)(EXTERNAL_PTS | SYNC_OUTSIDE); |
170 | } else if (vpcodec->video_type == VFORMAT_VC1) { |
171 | if (argc < 7) { |
172 | printf("No subformat for vc1, take the default VIDEO_DEC_FORMAT_WVC1\n"); |
173 | vpcodec->am_sysinfo.format = VIDEO_DEC_FORMAT_WVC1; |
174 | } else { |
175 | vpcodec->am_sysinfo.format = atoi(argv[6]); |
176 | } |
177 | } else if (vpcodec->video_type == VFORMAT_MPEG4) { |
178 | if (argc < 7) { |
179 | printf("No subformat for mpeg4, take the default VIDEO_DEC_FORMAT_MPEG4_5\n"); |
180 | vpcodec->am_sysinfo.format = VIDEO_DEC_FORMAT_MPEG4_5; |
181 | } else { |
182 | vpcodec->am_sysinfo.format = atoi(argv[6]); |
183 | } |
184 | } |
185 | |
186 | vpcodec->stream_type = STREAM_TYPE_ES_VIDEO; |
187 | vpcodec->am_sysinfo.rate = 96000 / atoi(argv[4]); |
188 | vpcodec->am_sysinfo.height = atoi(argv[3]); |
189 | vpcodec->am_sysinfo.width = atoi(argv[2]); |
190 | vpcodec->has_audio = 0; |
191 | vpcodec->noblock = 0; |
192 | |
193 | #ifdef AUDIO_ES |
194 | apcodec->audio_type = AFORMAT_MPEG; |
195 | apcodec->stream_type = STREAM_TYPE_ES_AUDIO; |
196 | apcodec->audio_pid = 0x1023; |
197 | apcodec->has_audio = 1; |
198 | apcodec->audio_channels = 2; |
199 | apcodec->audio_samplerate = 48000; |
200 | apcodec->noblock = 0; |
201 | apcodec->audio_info.channels = 2; |
202 | apcodec->audio_info.sample_rate = 48000; |
203 | #endif |
204 | |
205 | printf("\n*********CODEC PLAYER DEMO************\n\n"); |
206 | filename = argv[1]; |
207 | printf("file %s to be played\n", filename); |
208 | |
209 | if ((fp = fopen(filename, "rb")) == NULL) { |
210 | printf("open file error!\n"); |
211 | return -1; |
212 | } |
213 | |
214 | #ifdef AUDIO_ES |
215 | ret = codec_init(apcodec); |
216 | if (ret != CODEC_ERROR_NONE) { |
217 | printf("codec init failed, ret=-0x%x", -ret); |
218 | return -1; |
219 | } |
220 | #endif |
221 | |
222 | ret = codec_init(vpcodec); |
223 | if (ret != CODEC_ERROR_NONE) { |
224 | printf("codec init failed, ret=-0x%x", -ret); |
225 | return -1; |
226 | } |
227 | printf("video codec ok!\n"); |
228 | |
229 | //codec_set_cntl_avthresh(vpcodec, AV_SYNC_THRESH); |
230 | //codec_set_cntl_syncthresh(vpcodec, 0); |
231 | |
232 | set_tsync_enable(0); |
233 | |
234 | pcodec = vpcodec; |
235 | while (!feof(fp)) { |
236 | Readlen = fread(buffer, 1, READ_SIZE, fp); |
237 | //printf("Readlen %d\n", Readlen); |
238 | if (Readlen <= 0) { |
239 | printf("read file error!\n"); |
240 | rewind(fp); |
241 | } |
242 | |
243 | isize = 0; |
244 | do { |
245 | ret = codec_write(pcodec, buffer + isize, Readlen); |
246 | if (ret < 0) { |
247 | if (errno != EAGAIN) { |
248 | printf("write data failed, errno %d\n", errno); |
249 | goto error; |
250 | } else { |
251 | continue; |
252 | } |
253 | } else { |
254 | isize += ret; |
255 | } |
256 | //printf("ret %d, isize %d\n", ret, isize); |
257 | } while (isize < Readlen); |
258 | |
259 | signal(SIGCHLD, SIG_IGN); |
260 | signal(SIGTSTP, SIG_IGN); |
261 | signal(SIGTTOU, SIG_IGN); |
262 | signal(SIGTTIN, SIG_IGN); |
263 | signal(SIGHUP, signal_handler); |
264 | signal(SIGTERM, signal_handler); |
265 | signal(SIGSEGV, signal_handler); |
266 | signal(SIGINT, signal_handler); |
267 | signal(SIGQUIT, signal_handler); |
268 | } |
269 | |
270 | do { |
271 | ret = codec_get_vbuf_state(pcodec, &vbuf); |
272 | if (ret != 0) { |
273 | printf("codec_get_vbuf_state error: %x\n", -ret); |
274 | goto error; |
275 | } |
276 | } while (vbuf.data_len > 0x100); |
277 | |
278 | error: |
279 | #ifdef AUDIO_ES |
280 | codec_close(apcodec); |
281 | #endif |
282 | codec_close(vpcodec); |
283 | fclose(fp); |
284 | set_display_axis(1); |
285 | |
286 | return 0; |
287 | } |
288 | |
289 |