├── Makefile ├── README.md └── esplayer.c /Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -o esplayer esplayer.c -lamcodec -lamadec -lamavutils -lpthread 3 | 4 | clean: 5 | rm -rf esplayer 6 | 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Amlogic Video Hardware Deocder Demo 2 | 3 | Decode and display on framebuffer. 4 | 5 | Usage: 6 | ``` 7 | esplayer [subformat for mpeg4/vc1] 8 | ``` 9 | 10 | Example: 11 | 12 | H265: 13 | 14 | ``` 15 | # ./esplayer 1080p.h265 1920 1080 25 11 16 | ``` 17 | -------------------------------------------------------------------------------- /esplayer.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014 Amlogic, Inc. All rights reserved. 3 | * Copyright (c) 2020 Wesion, Inc. All rights reserved. 4 | * 5 | * This source code is subject to the terms and conditions defined in the 6 | * file 'LICENSE' which is part of this source code package. 7 | * 8 | * Description: 9 | */ 10 | 11 | 12 | /************************************************** 13 | * example based on amcodec 14 | **************************************************/ 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | #define READ_SIZE (64 * 1024) 29 | #define EXTERNAL_PTS (1) 30 | #define SYNC_OUTSIDE (2) 31 | #define UNIT_FREQ 96000 32 | #define PTS_FREQ 90000 33 | #define AV_SYNC_THRESH PTS_FREQ*30 34 | 35 | static codec_para_t v_codec_para; 36 | static codec_para_t a_codec_para; 37 | static codec_para_t *pcodec, *apcodec, *vpcodec; 38 | static char *filename; 39 | FILE* fp = NULL; 40 | 41 | int osd_blank(char *path, int cmd) 42 | { 43 | int fd; 44 | char bcmd[16]; 45 | fd = open(path, O_CREAT | O_RDWR | O_TRUNC, 0644); 46 | 47 | if (fd >= 0) { 48 | sprintf(bcmd, "%d", cmd); 49 | write(fd, bcmd, strlen(bcmd)); 50 | close(fd); 51 | return 0; 52 | } 53 | 54 | return -1; 55 | } 56 | 57 | void init_display(void) 58 | { 59 | osd_blank("/sys/class/graphics/fb0/blank", 1); 60 | osd_blank("/sys/class/graphics/fb1/blank", 1); 61 | } 62 | 63 | void restore_display(void) 64 | { 65 | osd_blank("/sys/class/graphics/fb0/blank", 0); 66 | osd_blank("/sys/class/graphics/fb1/blank", 1); 67 | } 68 | 69 | int set_tsync_enable(int enable) 70 | { 71 | int fd; 72 | char *path = "/sys/class/tsync/enable"; 73 | char bcmd[16]; 74 | fd = open(path, O_CREAT | O_RDWR | O_TRUNC, 0644); 75 | if (fd >= 0) { 76 | sprintf(bcmd, "%d", enable); 77 | write(fd, bcmd, strlen(bcmd)); 78 | close(fd); 79 | return 0; 80 | } 81 | 82 | return -1; 83 | } 84 | 85 | static void signal_handler(int signum) 86 | { 87 | printf("Get signum=%x\n", signum); 88 | #ifdef AUDIO_ES 89 | codec_close(apcodec); 90 | #endif 91 | codec_close(vpcodec); 92 | fclose(fp); 93 | restore_display(); 94 | signal(signum, SIG_DFL); 95 | raise(signum); 96 | } 97 | 98 | int main(int argc, char *argv[]) 99 | { 100 | int ret = CODEC_ERROR_NONE; 101 | char buffer[READ_SIZE]; 102 | 103 | int len = 0; 104 | int size = READ_SIZE; 105 | int Readlen; 106 | int isize; 107 | struct buf_status vbuf; 108 | 109 | if (argc < 6) { 110 | printf("Corret command: esplayer [subformat for mpeg4/vc1]\n"); 111 | return -1; 112 | } 113 | 114 | #ifdef AUDIO_ES 115 | apcodec = &a_codec_para; 116 | memset(apcodec, 0, sizeof(codec_para_t)); 117 | #endif 118 | 119 | vpcodec = &v_codec_para; 120 | memset(vpcodec, 0, sizeof(codec_para_t)); 121 | 122 | vpcodec->has_video = 1; 123 | vpcodec->video_type = atoi(argv[5]); 124 | if (vpcodec->video_type == VFORMAT_H264) { 125 | vpcodec->am_sysinfo.format = VIDEO_DEC_FORMAT_H264; 126 | vpcodec->am_sysinfo.param = (void *)(EXTERNAL_PTS | SYNC_OUTSIDE); 127 | } else if (vpcodec->video_type == VFORMAT_VC1) { 128 | if (argc < 7) { 129 | printf("No subformat for vc1, take the default VIDEO_DEC_FORMAT_WVC1\n"); 130 | vpcodec->am_sysinfo.format = VIDEO_DEC_FORMAT_WVC1; 131 | } else { 132 | vpcodec->am_sysinfo.format = atoi(argv[6]); 133 | } 134 | } else if (vpcodec->video_type == VFORMAT_MPEG4) { 135 | if (argc < 7) { 136 | printf("No subformat for mpeg4, take the default VIDEO_DEC_FORMAT_MPEG4_5\n"); 137 | vpcodec->am_sysinfo.format = VIDEO_DEC_FORMAT_MPEG4_5; 138 | } else { 139 | vpcodec->am_sysinfo.format = atoi(argv[6]); 140 | } 141 | } else if (vpcodec->video_type == VFORMAT_HEVC) { 142 | vpcodec->am_sysinfo.format = VIDEO_DEC_FORMAT_HEVC; 143 | vpcodec->am_sysinfo.param = (void *)(EXTERNAL_PTS | SYNC_OUTSIDE); 144 | } else { 145 | printf("unsupported video type: %d\n", vpcodec->video_type); 146 | return -1; 147 | } 148 | 149 | vpcodec->stream_type = STREAM_TYPE_ES_VIDEO; 150 | vpcodec->am_sysinfo.rate = 96000 / atoi(argv[4]); 151 | vpcodec->am_sysinfo.height = atoi(argv[3]); 152 | vpcodec->am_sysinfo.width = atoi(argv[2]); 153 | vpcodec->has_audio = 0; 154 | vpcodec->noblock = 0; 155 | 156 | #ifdef AUDIO_ES 157 | apcodec->audio_type = AFORMAT_MPEG; 158 | apcodec->stream_type = STREAM_TYPE_ES_AUDIO; 159 | apcodec->audio_pid = 0x1023; 160 | apcodec->has_audio = 1; 161 | apcodec->audio_channels = 2; 162 | apcodec->audio_samplerate = 48000; 163 | apcodec->noblock = 0; 164 | apcodec->audio_info.channels = 2; 165 | apcodec->audio_info.sample_rate = 48000; 166 | #endif 167 | 168 | printf("\n*********CODEC PLAYER DEMO************\n\n"); 169 | filename = argv[1]; 170 | printf("file %s to be played\n", filename); 171 | 172 | if ((fp = fopen(filename, "rb")) == NULL) { 173 | printf("open file error!\n"); 174 | return -1; 175 | } 176 | 177 | init_display(); 178 | 179 | #ifdef AUDIO_ES 180 | ret = codec_init(apcodec); 181 | if (ret != CODEC_ERROR_NONE) { 182 | printf("codec init failed, ret=-0x%x", -ret); 183 | goto close_fp; 184 | } 185 | #endif 186 | 187 | ret = codec_init(vpcodec); 188 | if (ret != CODEC_ERROR_NONE) { 189 | printf("codec init failed, ret=-0x%x", -ret); 190 | #ifdef AUDIO_ES 191 | goto close_audio_fp; 192 | #else 193 | goto close_fp; 194 | #endif 195 | } 196 | printf("video codec ok!\n"); 197 | 198 | //codec_set_cntl_avthresh(vpcodec, AV_SYNC_THRESH); 199 | //codec_set_cntl_syncthresh(vpcodec, 0); 200 | 201 | set_tsync_enable(0); 202 | 203 | signal(SIGCHLD, SIG_IGN); 204 | signal(SIGTSTP, SIG_IGN); 205 | signal(SIGTTOU, SIG_IGN); 206 | signal(SIGTTIN, SIG_IGN); 207 | signal(SIGHUP, signal_handler); 208 | signal(SIGTERM, signal_handler); 209 | signal(SIGSEGV, signal_handler); 210 | signal(SIGINT, signal_handler); 211 | signal(SIGQUIT, signal_handler); 212 | 213 | pcodec = vpcodec; 214 | while (!feof(fp)) { 215 | Readlen = fread(buffer, 1, READ_SIZE, fp); 216 | if (Readlen <= 0) { 217 | printf("read file error!\n"); 218 | rewind(fp); 219 | } 220 | 221 | isize = 0; 222 | do { 223 | ret = codec_write(pcodec, buffer + isize, Readlen); 224 | if (ret < 0) { 225 | if (errno != EAGAIN) { 226 | printf("write data failed, errno %d\n", errno); 227 | goto error; 228 | } else { 229 | continue; 230 | } 231 | } else { 232 | isize += ret; 233 | } 234 | } while (isize < Readlen); 235 | } 236 | 237 | do { 238 | ret = codec_get_vbuf_state(pcodec, &vbuf); 239 | if (ret != 0) { 240 | printf("codec_get_vbuf_state error: %x\n", -ret); 241 | goto error; 242 | } 243 | } while (vbuf.data_len > 0x100); 244 | 245 | error: 246 | codec_close(vpcodec); 247 | #ifdef AUDIO_ES 248 | close_audio_fp: 249 | codec_close(apcodec); 250 | #endif 251 | close_fp: 252 | fclose(fp); 253 | 254 | restore_display(); 255 | 256 | return 0; 257 | } 258 | --------------------------------------------------------------------------------