Fix crash during early exit
[mplayer.git] / libvo / vo_v4l2.c
blobfb4a66659342887f2bb9e4e0ec9be44d1a37810a
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"
46 #define DEFAULT_MPEG_DECODER "/dev/video16"
47 #define V4L2_VO_HDR "VO: [v4l2]"
49 int v4l2_fd = -1;
50 static vo_mpegpes_t *pes;
52 /* suboptions */
53 static int output = -1;
54 static char *device = NULL;
56 static opt_t subopts[] = {
57 {"output", OPT_ARG_INT, &output, (opt_test_f)int_non_neg},
58 {"device", OPT_ARG_MSTRZ, &device, NULL},
59 {NULL}
62 static const vo_info_t info =
64 "V4L2 MPEG Video Decoder Output",
65 "v4l2",
66 "Benjamin Zores",
69 const LIBVO_EXTERN (v4l2)
71 int
72 v4l2_write (unsigned char *data, int len)
74 if (v4l2_fd < 0)
75 return 0;
77 return write (v4l2_fd, data, len);
80 /* video out functions */
82 static int
83 config (uint32_t width, uint32_t height,
84 uint32_t d_width, uint32_t d_height,
85 uint32_t fullscreen, char *title, uint32_t format)
87 return 0;
90 static int
91 preinit (const char *arg)
93 struct v4l2_output vout;
94 struct v4l2_ext_controls ctrls;
95 int err;
97 if (subopt_parse (arg, subopts) != 0)
99 mp_msg (MSGT_VO, MSGL_FATAL,
100 "\n-vo v4l2 command line help:\n"
101 "Example: mplayer -vo v4l2:device=/dev/video16:output=2\n"
102 "\nOptions:\n"
103 " device=/dev/videoX\n"
104 " Name of the MPEG decoder device file.\n"
105 " output=<0-...>\n"
106 " V4L2 id of the TV output.\n"
107 "\n" );
108 return -1;
111 if (!device)
112 device = strdup (DEFAULT_MPEG_DECODER);
114 v4l2_fd = open (device, O_RDWR);
115 if (v4l2_fd < 0)
117 free (device);
118 mp_msg (MSGT_VO, MSGL_FATAL, "%s %s\n", V4L2_VO_HDR, strerror (errno));
119 return -1;
122 /* check for device hardware MPEG decoding capability */
123 ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
124 ctrls.count = 0;
125 ctrls.controls = NULL;
127 if (ioctl (v4l2_fd, VIDIOC_G_EXT_CTRLS, &ctrls) < 0)
129 free (device);
130 mp_msg (MSGT_OPEN, MSGL_FATAL, "%s %s\n", V4L2_VO_HDR, strerror (errno));
131 return -1;
134 /* list available outputs */
135 vout.index = 0;
136 err = 1;
137 mp_msg (MSGT_VO, MSGL_INFO, "%s Available video outputs: ", V4L2_VO_HDR);
138 while (ioctl (v4l2_fd, VIDIOC_ENUMOUTPUT, &vout) >= 0)
140 err = 0;
141 mp_msg (MSGT_VO, MSGL_INFO, "'#%d, %s' ", vout.index, vout.name);
142 vout.index++;
144 if (err)
146 mp_msg (MSGT_VO, MSGL_INFO, "none\n");
147 free (device);
148 return -1;
150 else
151 mp_msg (MSGT_VO, MSGL_INFO, "\n");
153 /* set user specified output */
154 if (output != -1)
156 if (ioctl (v4l2_fd, VIDIOC_S_OUTPUT, &output) < 0)
158 mp_msg (MSGT_VO, MSGL_ERR,
159 "%s can't set output (%s)\n", V4L2_VO_HDR, strerror (errno));
160 free (device);
161 return -1;
165 /* display device name */
166 mp_msg (MSGT_VO, MSGL_INFO, "%s using %s\n", V4L2_VO_HDR, device);
167 free (device);
169 /* display current video output */
170 if (ioctl (v4l2_fd, VIDIOC_G_OUTPUT, &output) == 0)
172 vout.index = output;
173 if (ioctl (v4l2_fd, VIDIOC_ENUMOUTPUT, &vout) < 0)
175 mp_msg (MSGT_VO, MSGL_ERR,
176 "%s can't get output (%s).\n", V4L2_VO_HDR, strerror (errno));
177 return -1;
179 else
180 mp_msg (MSGT_VO, MSGL_INFO,
181 "%s video output: %s\n", V4L2_VO_HDR, vout.name);
183 else
185 mp_msg (MSGT_VO, MSGL_ERR,
186 "%s can't get output (%s).\n", V4L2_VO_HDR, strerror (errno));
187 return -1;
190 return 0;
193 static void
194 draw_osd (void)
196 /* do nothing */
199 static int
200 draw_frame (uint8_t * src[])
202 pes = (vo_mpegpes_t *) src[0];
203 return 0;
206 static void
207 flip_page (void)
209 if (v4l2_fd < 0)
210 return;
212 if (!pes)
213 return;
215 send_mpeg_pes_packet (pes->data, pes->size, pes->id,
216 pes->timestamp ? pes->timestamp : vo_pts, 2,
217 v4l2_write);
219 /* ensure flip_page() won't be called twice */
220 pes = NULL;
223 static int
224 draw_slice (uint8_t *image[], int stride[], int w, int h, int x, int y)
226 return 0;
229 static void
230 uninit (void)
232 if (v4l2_fd < 0)
233 return;
235 /* close device */
236 close (v4l2_fd);
237 v4l2_fd = -1;
240 static void
241 check_events (void)
243 /* do nothing */
246 static int
247 query_format (uint32_t format)
249 if (format != IMGFMT_MPEGPES)
250 return 0;
252 return VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_TIMER;
255 static int
256 control (uint32_t request, void *data)
258 switch (request)
260 case VOCTRL_QUERY_FORMAT:
261 return query_format (*((uint32_t*) data));
264 return VO_NOTIMPL;