stream_file: Simplify and document MinGW stdin hack
[mplayer.git] / libvo / vo_v4l2.c
blobd1ee15ca3b8f868e4440c01f5ca0f6850998a05f
1 /*
2 * video output for V4L2 hardware MPEG decoders
4 * Copyright (C) 2007 Benjamin Zores
6 * This file is part of MPlayer.
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.
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.
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.
23 #include "config.h"
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>
40 #include "mp_msg.h"
41 #include "subopt-helper.h"
42 #include "video_out.h"
43 #include "video_out_internal.h"
44 #include "libmpdemux/mpeg_packetizer.h"
45 #include "vo_v4l2.h"
47 #define DEFAULT_MPEG_DECODER "/dev/video16"
48 #define V4L2_VO_HDR "VO: [v4l2]"
50 int v4l2_fd = -1;
51 static vo_mpegpes_t *pes;
53 /* suboptions */
54 static int output = -1;
55 static char *device = NULL;
57 static const opt_t subopts[] = {
58 {"output", OPT_ARG_INT, &output, int_non_neg},
59 {"device", OPT_ARG_MSTRZ, &device, NULL},
60 {NULL}
63 static const vo_info_t info =
65 "V4L2 MPEG Video Decoder Output",
66 "v4l2",
67 "Benjamin Zores",
70 const LIBVO_EXTERN (v4l2)
72 int
73 v4l2_write (const unsigned char *data, int len)
75 if (v4l2_fd < 0)
76 return 0;
78 return write (v4l2_fd, data, len);
81 /* video out functions */
83 static int
84 config (uint32_t width, uint32_t height,
85 uint32_t d_width, uint32_t d_height,
86 uint32_t fullscreen, char *title, uint32_t format)
88 return 0;
91 static int
92 preinit (const char *arg)
94 struct v4l2_output vout;
95 struct v4l2_ext_controls ctrls;
96 int err;
98 if (subopt_parse (arg, subopts) != 0)
100 mp_msg (MSGT_VO, MSGL_FATAL,
101 "\n-vo v4l2 command line help:\n"
102 "Example: mplayer -vo v4l2:device=/dev/video16:output=2\n"
103 "\nOptions:\n"
104 " device=/dev/videoX\n"
105 " Name of the MPEG decoder device file.\n"
106 " output=<0-...>\n"
107 " V4L2 id of the TV output.\n"
108 "\n" );
109 return -1;
112 if (!device)
113 device = strdup (DEFAULT_MPEG_DECODER);
115 v4l2_fd = open (device, O_RDWR);
116 if (v4l2_fd < 0)
118 free (device);
119 mp_msg (MSGT_VO, MSGL_FATAL, "%s %s\n", V4L2_VO_HDR, strerror (errno));
120 return -1;
123 /* check for device hardware MPEG decoding capability */
124 ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
125 ctrls.count = 0;
126 ctrls.controls = NULL;
128 if (ioctl (v4l2_fd, VIDIOC_G_EXT_CTRLS, &ctrls) < 0)
130 free (device);
131 mp_msg (MSGT_OPEN, MSGL_FATAL, "%s %s\n", V4L2_VO_HDR, strerror (errno));
132 return -1;
135 /* list available outputs */
136 vout.index = 0;
137 err = 1;
138 mp_msg (MSGT_VO, MSGL_INFO, "%s Available video outputs: ", V4L2_VO_HDR);
139 while (ioctl (v4l2_fd, VIDIOC_ENUMOUTPUT, &vout) >= 0)
141 err = 0;
142 mp_msg (MSGT_VO, MSGL_INFO, "'#%d, %s' ", vout.index, vout.name);
143 vout.index++;
145 if (err)
147 mp_msg (MSGT_VO, MSGL_INFO, "none\n");
148 free (device);
149 return -1;
151 else
152 mp_msg (MSGT_VO, MSGL_INFO, "\n");
154 /* set user specified output */
155 if (output != -1)
157 if (ioctl (v4l2_fd, VIDIOC_S_OUTPUT, &output) < 0)
159 mp_msg (MSGT_VO, MSGL_ERR,
160 "%s can't set output (%s)\n", V4L2_VO_HDR, strerror (errno));
161 free (device);
162 return -1;
166 /* display device name */
167 mp_msg (MSGT_VO, MSGL_INFO, "%s using %s\n", V4L2_VO_HDR, device);
168 free (device);
170 /* display current video output */
171 if (ioctl (v4l2_fd, VIDIOC_G_OUTPUT, &output) == 0)
173 vout.index = output;
174 if (ioctl (v4l2_fd, VIDIOC_ENUMOUTPUT, &vout) < 0)
176 mp_msg (MSGT_VO, MSGL_ERR,
177 "%s can't get output (%s).\n", V4L2_VO_HDR, strerror (errno));
178 return -1;
180 else
181 mp_msg (MSGT_VO, MSGL_INFO,
182 "%s video output: %s\n", V4L2_VO_HDR, vout.name);
184 else
186 mp_msg (MSGT_VO, MSGL_ERR,
187 "%s can't get output (%s).\n", V4L2_VO_HDR, strerror (errno));
188 return -1;
191 return 0;
194 static void
195 draw_osd (void)
197 /* do nothing */
200 static int
201 draw_frame (uint8_t * src[])
203 pes = (vo_mpegpes_t *) src[0];
204 return 0;
207 static void
208 flip_page (void)
210 if (v4l2_fd < 0)
211 return;
213 if (!pes)
214 return;
216 send_mpeg_pes_packet (pes->data, pes->size, pes->id,
217 pes->timestamp ? pes->timestamp : vo_pts, 2,
218 v4l2_write);
220 /* ensure flip_page() won't be called twice */
221 pes = NULL;
224 static int
225 draw_slice (uint8_t *image[], int stride[], int w, int h, int x, int y)
227 return 0;
230 static void
231 uninit (void)
233 if (v4l2_fd < 0)
234 return;
236 /* close device */
237 close (v4l2_fd);
238 v4l2_fd = -1;
241 static void
242 check_events (void)
244 /* do nothing */
247 static int
248 query_format (uint32_t format)
250 if (format != IMGFMT_MPEGPES)
251 return 0;
253 return VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_TIMER;
256 static int
257 control (uint32_t request, void *data)
259 switch (request)
261 case VOCTRL_QUERY_FORMAT:
262 return query_format (*((uint32_t*) data));
265 return VO_NOTIMPL;