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