blob: 7510c0b12cdb516fc7a0968e7a75785534934d66 [file] [log] [blame]
Song Zhao1b237602020-02-13 10:58:57 -08001/*
2 * Copyright (c) 2019 Amlogic, Inc. All rights reserved.
3 *
4 * This source code is subject to the terms and conditions defined in the
5 * file 'LICENSE' which is part of this source code package.
6 *
7 * Description:
8 */
9#include <getopt.h>
10#include <stdbool.h>
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include <sys/ioctl.h>
15#include <sys/stat.h>
16#include <sys/types.h>
17#include <semaphore.h>
18#include <signal.h>
19#include <time.h>
20#include <unistd.h>
21
22#include "drm.h"
23#include "demux.h"
24#include "v4l2-dec.h"
25
26static char* media_file;
27static sem_t wait_for_end;
28int global_plane_id;
Ao Xua3a929a2020-04-16 18:29:51 +080029char mode_str[16];
30unsigned int vfresh;
Song Zhao1b237602020-02-13 10:58:57 -080031int g_dw_mode = 16;
Song Zhao6c7d1f82020-04-06 23:22:49 -070032static int secure_mode = 0;
Song Zhao1b237602020-02-13 10:58:57 -080033int ffmpeg_log = 0;
34
35static void usage(int argc, char **argv)
36{
37 printf( "Usage: %s [options]\n\n"
38 "Version 0.1\n"
39 "Options:\n"
40 "-d | --double_write double write mode.\n"
41 " 0: no DW compressed only\n"
42 " 1: DW with 1:1 ratio\n"
43 " 2: DW with 1:4 down sample\n"
44 " 4: DW with 1:2 down sample\n"
45 " 16: DW only\n"
46 "-f | --file name media file\n"
47 "-h | --help Print this message\n"
48 "-p | --plane=id select display plane. 26[pri] 28[overlay 1] 30[overlay 2] 32[video]\n"
Ao Xua3a929a2020-04-16 18:29:51 +080049 "-m | --mode str set display mode. such as 3840x2160 or 3840x2160-60\n"
50 " 1920x1080 or 1920x1080-60\n"
Song Zhao1b237602020-02-13 10:58:57 -080051 "-l | --log enable more ffmpeg demux log.\n"
Song Zhao6c7d1f82020-04-06 23:22:49 -070052 "-s | --secure secure video path.\n"
Song Zhao1b237602020-02-13 10:58:57 -080053 "",
54 argv[0]);
55}
56
Ao Xua3a929a2020-04-16 18:29:51 +080057static const char short_options[] = "d:hf:p:m:ls";
Song Zhao1b237602020-02-13 10:58:57 -080058
59static const struct option
60long_options[] = {
61 { "double_write", required_argument, NULL, 'd' },
62 { "file", required_argument, NULL, 'f' },
63 { "help", no_argument, NULL, 'h' },
64 { "plane", required_argument, NULL, 'p' },
Ao Xua3a929a2020-04-16 18:29:51 +080065 { "mode", required_argument, NULL, 'm' },
Song Zhao1b237602020-02-13 10:58:57 -080066 { "log", no_argument, NULL, 'l' },
Song Zhao6c7d1f82020-04-06 23:22:49 -070067 { "secure", no_argument, NULL, 's' },
Song Zhao1b237602020-02-13 10:58:57 -080068 { 0, 0, 0, 0 }
69};
70
71void decode_finish()
72{
73 sem_post(&wait_for_end);
74}
75
76int start_decoder(struct dmx_v_data *v_data)
77{
78 int ret;
Song Zhao6c7d1f82020-04-06 23:22:49 -070079 ret = v4l2_dec_init(v_data->type, secure_mode, decode_finish);
Song Zhao1b237602020-02-13 10:58:57 -080080 if (ret) {
81 printf("FATAL: start_decoder error:%d\n",ret);
82 exit(1);
83 }
84 return 0;
85}
86
87static int parse_para(int argc, char *argv[])
88{
Ao Xua3a929a2020-04-16 18:29:51 +080089 char *p;
90 unsigned int len;
Song Zhao1b237602020-02-13 10:58:57 -080091 for (;;) {
92 int idx;
93 int c;
94
95 c = getopt_long(argc, argv,
96 short_options, long_options, &idx);
97
98 if (-1 == c)
99 break;
100
101 switch (c) {
102 case 0: /* getopt_long() flag */
103 break;
104
105 case 'l':
106 ffmpeg_log = 1;
107 break;
108
109 case 'd':
110 g_dw_mode = atoi(optarg);
111 if (g_dw_mode != 0 &&
112 g_dw_mode != 1 &&
113 g_dw_mode != 2 &&
114 g_dw_mode != 4 &&
115 g_dw_mode != 16) {
116 printf("invalide dw_mode %d\n", g_dw_mode);
117 exit(1);
118 }
119 break;
120
121 case 'f':
122 media_file = strdup(optarg);
123 break;
124
125 case 'h':
126 usage(argc, argv);
127 return -1;
128
129 case 'p':
130 global_plane_id = atoi(optarg);
131 break;
132
Song Zhao6c7d1f82020-04-06 23:22:49 -0700133 case 's':
Song Zhaoec315042020-06-12 00:51:42 -0700134#ifndef CONFIG_SECMEM
135 printf("secure mode can not be enabled, check compile option\n");
136 exit(1);
137#endif
Song Zhao6c7d1f82020-04-06 23:22:49 -0700138 secure_mode = 1;
139 break;
140
Ao Xua3a929a2020-04-16 18:29:51 +0800141 case 'm':
142 p = strchr(optarg, '-');
143 if (p == NULL) {
144 len = strlen(optarg);
145 } else {
146 vfresh = strtoul(p + 1, NULL, 0);
147 len = p - optarg;
148 }
149
150 strncpy(mode_str, optarg, len);
151 printf("mode:%s, vfresh:%d.\n", mode_str, vfresh);
152 break;
153
Song Zhao1b237602020-02-13 10:58:57 -0800154 default:
155 usage(argc, argv);
156 return -1;
157 }
158 }
159 return 0;
160}
161
162static void sig_handler(int signo)
163{
164 dump_v4l2_decode_state();
165 decode_finish();
166}
167
168int main(int argc, char *argv[])
169{
170 int rc;
171
172 if (parse_para(argc, argv))
173 goto error;
174
175 if (!media_file) {
176 printf("no file name assigned\n");
177 goto error;
178 }
179
180 sem_init(&wait_for_end, 0, 0);
181
182 display_engine_register_cb(capture_buffer_recycle);
Song Zhao6c7d1f82020-04-06 23:22:49 -0700183 rc = display_engine_start(secure_mode);
Song Zhao1b237602020-02-13 10:58:57 -0800184 if (rc < 0) {
185 printf("Unable to start display engine\n");
186 goto error;
187 }
188 printf("\ndisplay started\n");
189
190 struct dmx_cb cb = {
191 .write = v4l2_dec_write_es,
192 .frame_done = v4l2_dec_frame_done,
193 .meta_done = start_decoder,
194 .eos = v4l2_dec_eos,
195 };
196
197 signal(SIGINT, sig_handler);
198
199 rc = demux_init(media_file, &cb);
200 if (rc) {
201 printf("demux_init fail %d\n",rc);
202 goto error;
203 }
204 printf("\ndemux started\n");
205
206 sem_wait(&wait_for_end);
207
208 dmx_destroy();
209 v4l2_dec_destroy();
210 display_engine_stop();
211 sem_destroy(&wait_for_end);
212
213 return 0;
214error:
215 return 1;
216}