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