1 /*
2 * video output for V4L2 hardware MPEG decoders
3 *
4 * Copyright (C) 2007 Benjamin Zores
5 *
6 * This file is part of MPlayer.
7 *
8 * MPlayer is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * MPlayer is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23 #include "config.h"
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <sys/ioctl.h>
32 #include <fcntl.h>
33 #include <unistd.h>
34 #include <stdlib.h>
35 #include <inttypes.h>
36 #include <linux/types.h>
37 #include <linux/videodev2.h>
38 #include <linux/ioctl.h>
39
40 #include "mp_msg.h"
41 #include "subopt-helper.h"
42 #include "video_out.h"
43 #define NO_DRAW_SLICE
44 #include "video_out_internal.h"
45 #include "libmpdemux/mpeg_packetizer.h"
46 #include "vo_v4l2.h"
47
48 #define DEFAULT_MPEG_DECODER "/dev/video16"
49 #define V4L2_VO_HDR "VO: [v4l2]"
50
51 int v4l2_fd = -1;
52 static vo_mpegpes_t *pes;
53
54 /* suboptions */
55 static int output = -1;
56 static char *device = NULL;
57
58 static const opt_t subopts[] = {
59 {"output", OPT_ARG_INT, &output, int_non_neg},
60 {"device", OPT_ARG_MSTRZ, &device, NULL},
61 {NULL}
62 };
63
64 static const vo_info_t info =
65 {
66 "V4L2 MPEG Video Decoder Output",
67 "v4l2",
68 "Benjamin Zores",
69 ""
70 };
LIBVO_EXTERN(v4l2)71 const LIBVO_EXTERN (v4l2)
72
73 int
74 v4l2_write (const unsigned char *data, int len)
75 {
76 if (v4l2_fd < 0)
77 return 0;
78
79 return write (v4l2_fd, data, len);
80 }
81
82 /* video out functions */
83
84 static int
config(uint32_t width,uint32_t height,uint32_t d_width,uint32_t d_height,uint32_t fullscreen,char * title,uint32_t format)85 config (uint32_t width, uint32_t height,
86 uint32_t d_width, uint32_t d_height,
87 uint32_t fullscreen, char *title, uint32_t format)
88 {
89 return 0;
90 }
91
92 static int
preinit(const char * arg)93 preinit (const char *arg)
94 {
95 struct v4l2_output vout;
96 struct v4l2_ext_controls ctrls;
97 int err;
98
99 if (subopt_parse (arg, subopts) != 0)
100 {
101 mp_msg (MSGT_VO, MSGL_FATAL,
102 "\n-vo v4l2 command line help:\n"
103 "Example: mplayer -vo v4l2:device=/dev/video16:output=2\n"
104 "\nOptions:\n"
105 " device=/dev/videoX\n"
106 " Name of the MPEG decoder device file.\n"
107 " output=<0-...>\n"
108 " V4L2 id of the TV output.\n"
109 "\n" );
110 return -1;
111 }
112
113 if (!device)
114 device = strdup (DEFAULT_MPEG_DECODER);
115
116 v4l2_fd = open (device, O_RDWR);
117 if (v4l2_fd < 0)
118 {
119 free (device);
120 mp_msg (MSGT_VO, MSGL_FATAL, "%s %s\n", V4L2_VO_HDR, strerror (errno));
121 return -1;
122 }
123
124 /* check for device hardware MPEG decoding capability */
125 ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
126 ctrls.count = 0;
127 ctrls.controls = NULL;
128
129 if (ioctl (v4l2_fd, VIDIOC_G_EXT_CTRLS, &ctrls) < 0)
130 {
131 free (device);
132 mp_msg (MSGT_OPEN, MSGL_FATAL, "%s %s\n", V4L2_VO_HDR, strerror (errno));
133 return -1;
134 }
135
136 /* list available outputs */
137 vout.index = 0;
138 err = 1;
139 mp_msg (MSGT_VO, MSGL_INFO, "%s Available video outputs: ", V4L2_VO_HDR);
140 while (ioctl (v4l2_fd, VIDIOC_ENUMOUTPUT, &vout) >= 0)
141 {
142 err = 0;
143 mp_msg (MSGT_VO, MSGL_INFO, "'#%d, %s' ", vout.index, vout.name);
144 vout.index++;
145 }
146 if (err)
147 {
148 mp_msg (MSGT_VO, MSGL_INFO, "none\n");
149 free (device);
150 return -1;
151 }
152 else
153 mp_msg (MSGT_VO, MSGL_INFO, "\n");
154
155 /* set user specified output */
156 if (output != -1)
157 {
158 if (ioctl (v4l2_fd, VIDIOC_S_OUTPUT, &output) < 0)
159 {
160 mp_msg (MSGT_VO, MSGL_ERR,
161 "%s can't set output (%s)\n", V4L2_VO_HDR, strerror (errno));
162 free (device);
163 return -1;
164 }
165 }
166
167 /* display device name */
168 mp_msg (MSGT_VO, MSGL_INFO, "%s using %s\n", V4L2_VO_HDR, device);
169 free (device);
170
171 /* display current video output */
172 if (ioctl (v4l2_fd, VIDIOC_G_OUTPUT, &output) == 0)
173 {
174 vout.index = output;
175 if (ioctl (v4l2_fd, VIDIOC_ENUMOUTPUT, &vout) < 0)
176 {
177 mp_msg (MSGT_VO, MSGL_ERR,
178 "%s can't get output (%s).\n", V4L2_VO_HDR, strerror (errno));
179 return -1;
180 }
181 else
182 mp_msg (MSGT_VO, MSGL_INFO,
183 "%s video output: %s\n", V4L2_VO_HDR, vout.name);
184 }
185 else
186 {
187 mp_msg (MSGT_VO, MSGL_ERR,
188 "%s can't get output (%s).\n", V4L2_VO_HDR, strerror (errno));
189 return -1;
190 }
191
192 return 0;
193 }
194
195 static void
draw_osd(void)196 draw_osd (void)
197 {
198 /* do nothing */
199 }
200
201 static int
draw_frame(uint8_t * src[])202 draw_frame (uint8_t * src[])
203 {
204 pes = (vo_mpegpes_t *) src[0];
205 return 0;
206 }
207
208 static void
flip_page(void)209 flip_page (void)
210 {
211 if (v4l2_fd < 0)
212 return;
213
214 if (!pes)
215 return;
216
217 send_mpeg_pes_packet (pes->data, pes->size, pes->id,
218 pes->timestamp ? pes->timestamp : vo_pts, 2,
219 v4l2_write);
220
221 /* ensure flip_page() won't be called twice */
222 pes = NULL;
223 }
224
225 static void
uninit(void)226 uninit (void)
227 {
228 if (v4l2_fd < 0)
229 return;
230
231 /* close device */
232 close (v4l2_fd);
233 v4l2_fd = -1;
234 }
235
236 static void
check_events(void)237 check_events (void)
238 {
239 /* do nothing */
240 }
241
242 static int
query_format(uint32_t format)243 query_format (uint32_t format)
244 {
245 if (format != IMGFMT_MPEGPES)
246 return 0;
247
248 return VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_TIMER;
249 }
250
251 static int
control(uint32_t request,void * data)252 control (uint32_t request, void *data)
253 {
254 switch (request)
255 {
256 case VOCTRL_QUERY_FORMAT:
257 return query_format (*((uint32_t*) data));
258 }
259
260 return VO_NOTIMPL;
261 }
262