vo_xv.c: Make reconfig logic more robust
[mplayer.git] / libvo / vo_ivtv.c
blob36efe18f5876d1d0d436d1a0b0802b382ec736ee
1 /*
2 * Copyright (C) 2006 Benjamin Zores
3 * Video output for WinTV PVR-150/250/350 (a.k.a IVTV) cards.
4 * TV-Out through hardware MPEG decoder.
5 * Based on some old code from ivtv driver authors.
6 * See http://ivtvdriver.org/index.php/Main_Page for more details on the
7 * cards supported by the ivtv driver.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "config.h"
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <sys/ioctl.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 #include <stdlib.h>
36 #include <inttypes.h>
37 #include <linux/types.h>
38 #include <linux/videodev2.h>
39 #include <linux/ivtv.h>
40 #include <linux/ioctl.h>
42 #include "mp_msg.h"
43 #include "subopt-helper.h"
44 #include "video_out.h"
45 #include "video_out_internal.h"
46 #include "libmpdemux/mpeg_packetizer.h"
48 #define DEFAULT_MPEG_DECODER "/dev/video16"
49 #define IVTV_VO_HDR "VO: [ivtv]"
51 /* ivtv private */
52 int ivtv_fd = -1;
53 static vo_mpegpes_t *pes;
55 /* suboptions */
56 static int output = -1;
57 static char *device = NULL;
59 static opt_t subopts[] = {
60 {"output", OPT_ARG_INT, &output, (opt_test_f)int_non_neg},
61 {"device", OPT_ARG_MSTRZ, &device, NULL},
62 {NULL}
65 static const vo_info_t info =
67 "IVTV MPEG Video Decoder TV-Out",
68 "ivtv",
69 "Benjamin Zores",
72 const LIBVO_EXTERN (ivtv)
74 /* ivtv internals */
76 static uint32_t
77 ivtv_reset (int blank_screen)
79 struct ivtv_cfg_stop_decode sd;
80 struct ivtv_cfg_start_decode sd1;
81 int flags = 0;
83 if (blank_screen)
84 flags |= IVTV_STOP_FL_HIDE_FRAME;
85 sd.flags = flags;
87 if (ioctl (ivtv_fd, IVTV_IOC_STOP_DECODE, &sd) < 0)
89 mp_msg (MSGT_VO, MSGL_ERR,
90 "IVTV_IOC_STOP_DECODE: %s\n", strerror (errno));
91 return 1;
94 sd1.gop_offset = 0;
95 sd1.muted_audio_frames = 0;
97 if (ioctl (ivtv_fd, IVTV_IOC_START_DECODE, &sd1) < 0)
99 mp_msg (MSGT_VO, MSGL_ERR,
100 "IVTV_IOC_START_DECODE: %s\n", strerror (errno));
101 return 1;
104 return 0;
108 ivtv_write (unsigned char *data, int len)
110 if (ivtv_fd < 0)
111 return 0;
113 return write (ivtv_fd, data, len);
116 /* video out functions */
118 static int
119 config (uint32_t width, uint32_t height,
120 uint32_t d_width, uint32_t d_height,
121 uint32_t fullscreen, char *title, uint32_t format)
123 return 0;
126 static int
127 preinit (const char *arg)
129 struct v4l2_output vout;
130 int err;
132 if (subopt_parse (arg, subopts) != 0)
134 mp_msg (MSGT_VO, MSGL_FATAL,
135 "\n-vo ivtv command line help:\n"
136 "Example: mplayer -vo ivtv:device=/dev/video16:output=2\n"
137 "\nOptions:\n"
138 " device=/dev/videoX\n"
139 " Name of the MPEG decoder device file.\n"
140 " output=<0-...>\n"
141 " V4L2 id of the TV output.\n"
142 "\n" );
143 return -1;
146 if (!device)
147 device = strdup (DEFAULT_MPEG_DECODER);
149 ivtv_fd = open (device, O_RDWR);
150 if (ivtv_fd < 0)
152 free (device);
153 mp_msg (MSGT_VO, MSGL_FATAL, "%s %s\n", IVTV_VO_HDR, strerror (errno));
154 return -1;
157 /* list available outputs */
158 vout.index = 0;
159 err = 1;
160 mp_msg (MSGT_VO, MSGL_INFO, "%s Available video outputs: ", IVTV_VO_HDR);
161 while (ioctl (ivtv_fd, VIDIOC_ENUMOUTPUT, &vout) >= 0)
163 err = 0;
164 mp_msg (MSGT_VO, MSGL_INFO, "'#%d, %s' ", vout.index, vout.name);
165 vout.index++;
167 if (err)
169 mp_msg (MSGT_VO, MSGL_INFO, "none\n");
170 free (device);
171 return -1;
173 else
174 mp_msg (MSGT_VO, MSGL_INFO, "\n");
176 /* set user specified output */
177 if (output != -1)
179 if (ioctl (ivtv_fd, VIDIOC_S_OUTPUT, &output) < 0)
181 mp_msg (MSGT_VO, MSGL_ERR,
182 "%s can't set output (%s)\n", IVTV_VO_HDR, strerror (errno));
183 free (device);
184 return -1;
188 /* display device name */
189 mp_msg (MSGT_VO, MSGL_INFO, "%s using %s\n", IVTV_VO_HDR, device);
190 free (device);
192 /* display current video output */
193 if (ioctl (ivtv_fd, VIDIOC_G_OUTPUT, &output) == 0)
195 vout.index = output;
196 if (ioctl (ivtv_fd, VIDIOC_ENUMOUTPUT, &vout) < 0)
198 mp_msg (MSGT_VO, MSGL_ERR,
199 "%s can't get output (%s).\n", IVTV_VO_HDR, strerror (errno));
200 return -1;
202 else
203 mp_msg (MSGT_VO, MSGL_INFO,
204 "%s video output: %s\n", IVTV_VO_HDR, vout.name);
206 else
208 mp_msg (MSGT_VO, MSGL_ERR,
209 "%s can't get output (%s).\n", IVTV_VO_HDR, strerror (errno));
210 return -1;
213 /* clear output */
214 ivtv_reset (1);
216 return 0;
219 static void
220 draw_osd (void)
222 /* do nothing */
225 static int
226 draw_frame (uint8_t * src[])
228 pes = (vo_mpegpes_t *) src[0];
229 return 0;
232 static void
233 flip_page (void)
235 if (ivtv_fd < 0)
236 return;
238 if (!pes)
239 return;
241 send_mpeg_pes_packet (pes->data, pes->size, pes->id,
242 pes->timestamp ? pes->timestamp : vo_pts, 2,
243 ivtv_write);
245 /* ensure flip_page() won't be called twice */
246 pes = NULL;
249 static int
250 draw_slice (uint8_t *image[], int stride[], int w, int h, int x, int y)
252 return 0;
255 static void
256 uninit (void)
258 if (ivtv_fd < 0)
259 return;
261 /* clear output */
262 ivtv_reset (1);
264 /* close device */
265 close (ivtv_fd);
266 ivtv_fd = -1;
269 static void
270 check_events (void)
272 /* do nothing */
275 static int
276 query_format (uint32_t format)
278 if (format != IMGFMT_MPEGPES)
279 return 0;
281 return VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_TIMER;
284 static int
285 control (uint32_t request, void *data)
287 switch (request)
289 case VOCTRL_PAUSE:
290 case VOCTRL_RESUME:
291 return ivtv_reset (0);
293 case VOCTRL_RESET:
294 return ivtv_reset (1);
296 case VOCTRL_QUERY_FORMAT:
297 return query_format (*((uint32_t*) data));
300 return VO_NOTIMPL;