blob: 4527f2299ccb3b51d7760e036a44f145ae0dc398 [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':
134 secure_mode = 1;
135 break;
136
Ao Xua3a929a2020-04-16 18:29:51 +0800137 case 'm':
138 p = strchr(optarg, '-');
139 if (p == NULL) {
140 len = strlen(optarg);
141 } else {
142 vfresh = strtoul(p + 1, NULL, 0);
143 len = p - optarg;
144 }
145
146 strncpy(mode_str, optarg, len);
147 printf("mode:%s, vfresh:%d.\n", mode_str, vfresh);
148 break;
149
Song Zhao1b237602020-02-13 10:58:57 -0800150 default:
151 usage(argc, argv);
152 return -1;
153 }
154 }
155 return 0;
156}
157
158static void sig_handler(int signo)
159{
160 dump_v4l2_decode_state();
161 decode_finish();
162}
163
164int main(int argc, char *argv[])
165{
166 int rc;
167
168 if (parse_para(argc, argv))
169 goto error;
170
171 if (!media_file) {
172 printf("no file name assigned\n");
173 goto error;
174 }
175
176 sem_init(&wait_for_end, 0, 0);
177
178 display_engine_register_cb(capture_buffer_recycle);
Song Zhao6c7d1f82020-04-06 23:22:49 -0700179 rc = display_engine_start(secure_mode);
Song Zhao1b237602020-02-13 10:58:57 -0800180 if (rc < 0) {
181 printf("Unable to start display engine\n");
182 goto error;
183 }
184 printf("\ndisplay started\n");
185
186 struct dmx_cb cb = {
187 .write = v4l2_dec_write_es,
188 .frame_done = v4l2_dec_frame_done,
189 .meta_done = start_decoder,
190 .eos = v4l2_dec_eos,
191 };
192
193 signal(SIGINT, sig_handler);
194
195 rc = demux_init(media_file, &cb);
196 if (rc) {
197 printf("demux_init fail %d\n",rc);
198 goto error;
199 }
200 printf("\ndemux started\n");
201
202 sem_wait(&wait_for_end);
203
204 dmx_destroy();
205 v4l2_dec_destroy();
206 display_engine_stop();
207 sem_destroy(&wait_for_end);
208
209 return 0;
210error:
211 return 1;
212}