vo_corevideo.m: remote useless parentheses and casts
[mplayer/glamo.git] / stream / stream_pvr.c
blob3271ce974202a471b941b8bd8a03f0c304b6284a
1 /*
2 * stream layer for hardware MPEG 1/2/4 encoders a.k.a PVR
3 * (such as WinTV PVR-150/250/350/500 (a.k.a IVTV), pvrusb2 and cx88)
4 * See http://ivtvdriver.org/index.php/Main_Page for more details on the
5 * cards supported by the ivtv driver.
7 * Copyright (C) 2006 Benjamin Zores
8 * Copyright (C) 2007 Sven Gothel (channel navigation)
10 * This file is part of MPlayer.
12 * MPlayer is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * MPlayer is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "config.h"
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <string.h>
33 #include <ctype.h>
34 #include <sys/time.h>
35 #include <errno.h>
36 #include <sys/ioctl.h>
37 #include <sys/fcntl.h>
38 #include <inttypes.h>
39 #include <poll.h>
40 #include <linux/types.h>
41 #include <linux/videodev2.h>
43 #include "mp_msg.h"
45 #include "stream.h"
46 #include "pvr.h"
48 #include "frequencies.h"
49 #include "libavutil/common.h"
50 #include "libavutil/avstring.h"
52 #define PVR_DEFAULT_DEVICE "/dev/video0"
53 #define PVR_MAX_CONTROLS 10
55 /* logging mechanisms */
56 #define LOG_LEVEL_PVR "[pvr]"
57 #define LOG_LEVEL_V4L2 "[v4l2]"
58 #define LOG_LEVEL_ENCODER "[encoder]"
60 /* audio codec mode */
61 #define PVR_AUDIO_MODE_ARG_STEREO "stereo"
62 #define PVR_AUDIO_MODE_ARG_JOINT_STEREO "joint_stereo"
63 #define PVR_AUDIO_MODE_ARG_DUAL "dual"
64 #define PVR_AUDIO_MODE_ARG_MONO "mono"
66 /* video codec bitrate mode */
67 #define PVR_VIDEO_BITRATE_MODE_ARG_VBR "vbr"
68 #define PVR_VIDEO_BITRATE_MODE_ARG_CBR "cbr"
70 /* video codec stream type */
71 #define PVR_VIDEO_STREAM_TYPE_PS "ps"
72 #define PVR_VIDEO_STREAM_TYPE_TS "ts"
73 #define PVR_VIDEO_STREAM_TYPE_MPEG1 "mpeg1"
74 #define PVR_VIDEO_STREAM_TYPE_DVD "dvd"
75 #define PVR_VIDEO_STREAM_TYPE_VCD "vcd"
76 #define PVR_VIDEO_STREAM_TYPE_SVCD "svcd"
78 #define PVR_STATION_NAME_SIZE 256
80 /* command line arguments */
81 int pvr_param_aspect_ratio = 0;
82 int pvr_param_sample_rate = 0;
83 int pvr_param_audio_layer = 0;
84 int pvr_param_audio_bitrate = 0;
85 char *pvr_param_audio_mode = NULL;
86 int pvr_param_bitrate = 0;
87 char *pvr_param_bitrate_mode = NULL;
88 int pvr_param_bitrate_peak = 0;
89 char *pvr_param_stream_type = NULL;
91 typedef struct station_elem_s {
92 char name[8];
93 int freq;
94 char station[PVR_STATION_NAME_SIZE];
95 int enabled;
96 } station_elem_t;
98 typedef struct stationlist_s {
99 char name[PVR_STATION_NAME_SIZE];
100 station_elem_t *list;
101 int total; /* total number */
102 int used; /* used number */
103 int enabled; /* enabled number */
104 } stationlist_t;
106 struct pvr_t {
107 int dev_fd;
108 char *video_dev;
110 /* v4l2 params */
111 int mute;
112 int input;
113 int normid;
114 int brightness;
115 int contrast;
116 int hue;
117 int saturation;
118 int width;
119 int height;
120 int freq;
121 int chan_idx;
122 int chan_idx_last;
123 stationlist_t stationlist;
124 /* dups the tv_param_channel, or the url's channel param */
125 char *param_channel;
127 /* encoder params */
128 int aspect;
129 int samplerate;
130 int layer;
131 int audio_rate;
132 int audio_mode;
133 int bitrate;
134 int bitrate_mode;
135 int bitrate_peak;
136 int stream_type;
139 static struct pvr_t *
140 pvr_init (void)
142 struct pvr_t *pvr = NULL;
144 pvr = calloc (1, sizeof (struct pvr_t));
145 pvr->dev_fd = -1;
146 pvr->video_dev = strdup (PVR_DEFAULT_DEVICE);
148 /* v4l2 params */
149 pvr->mute = 0;
150 pvr->input = 0;
151 pvr->normid = -1;
152 pvr->brightness = 0;
153 pvr->contrast = 0;
154 pvr->hue = 0;
155 pvr->saturation = 0;
156 pvr->width = -1;
157 pvr->height = -1;
158 pvr->freq = -1;
159 pvr->chan_idx = -1;
160 pvr->chan_idx_last = -1;
162 /* set default encoding settings
163 * may be overlapped by user parameters
164 * Use VBR MPEG_PS encoding at 6 Mbps (peak at 9.6 Mbps)
165 * with 48 KHz L2 384 kbps audio.
167 pvr->aspect = V4L2_MPEG_VIDEO_ASPECT_4x3;
168 pvr->samplerate = V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000;
169 pvr->layer = V4L2_MPEG_AUDIO_ENCODING_LAYER_2;
170 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_384K;
171 pvr->audio_mode = V4L2_MPEG_AUDIO_MODE_STEREO;
172 pvr->bitrate = 6000000;
173 pvr->bitrate_mode = V4L2_MPEG_VIDEO_BITRATE_MODE_VBR;
174 pvr->bitrate_peak = 9600000;
175 pvr->stream_type = V4L2_MPEG_STREAM_TYPE_MPEG2_PS;
177 return pvr;
180 static void
181 pvr_uninit (struct pvr_t *pvr)
183 if (!pvr)
184 return;
186 /* close device */
187 if (pvr->dev_fd)
188 close (pvr->dev_fd);
190 if (pvr->video_dev)
191 free (pvr->video_dev);
193 if (pvr->stationlist.list)
194 free (pvr->stationlist.list);
196 if (pvr->param_channel)
197 free (pvr->param_channel);
199 free (pvr);
203 * @brief Copy Constructor for stationlist
205 * @see parse_setup_stationlist
207 static int
208 copycreate_stationlist (stationlist_t *stationlist, int num)
210 int i;
212 if (chantab < 0 || !stationlist)
213 return -1;
215 num = FFMAX (num, chanlists[chantab].count);
217 if (stationlist->list)
219 free (stationlist->list);
220 stationlist->list = NULL;
223 stationlist->total = 0;
224 stationlist->enabled = 0;
225 stationlist->used = 0;
226 stationlist->list = calloc (num, sizeof (station_elem_t));
228 if (!stationlist->list)
230 mp_msg (MSGT_OPEN, MSGL_ERR,
231 "%s No memory allocated for station list, giving up\n",
232 LOG_LEVEL_V4L2);
233 return -1;
236 /* transport the channel list data to our extented struct */
237 stationlist->total = num;
238 av_strlcpy (stationlist->name, chanlists[chantab].name, PVR_STATION_NAME_SIZE);
240 for (i = 0; i < chanlists[chantab].count; i++)
242 stationlist->list[i].station[0]= '\0'; /* no station name yet */
243 av_strlcpy (stationlist->list[i].name,
244 chanlists[chantab].list[i].name, PVR_STATION_NAME_SIZE);
245 stationlist->list[i].freq = chanlists[chantab].list[i].freq;
246 stationlist->list[i].enabled = 1; /* default enabled */
247 stationlist->enabled++;
248 stationlist->used++;
251 return 0;
254 static int
255 print_all_stations (struct pvr_t *pvr)
257 int i;
259 if (!pvr || !pvr->stationlist.list)
260 return -1;
262 for (i = 0; i < pvr->stationlist.total; i++)
264 mp_msg (MSGT_OPEN, MSGL_V,
265 "%s %3d: [%c] channel: %8s - freq: %8d - station: %s\n",
266 LOG_LEVEL_V4L2, i, (pvr->stationlist.list[i].enabled) ? 'X' : ' ',
267 pvr->stationlist.list[i].name, pvr->stationlist.list[i].freq,
268 pvr->stationlist.list[i].station);
271 return 0;
275 * Disables all stations
277 * @see parse_setup_stationlist
279 static void
280 disable_all_stations (struct pvr_t *pvr)
282 int i;
284 for (i = 0; i < pvr->stationlist.total; i++)
285 pvr->stationlist.list[i].enabled = 0;
286 pvr->stationlist.enabled = 0;
290 * Update or add a station
292 * @see parse_setup_stationlist
294 static int
295 set_station (struct pvr_t *pvr, const char *station,
296 const char *channel, int freq)
298 int i;
300 if (!pvr || !pvr->stationlist.list)
301 return -1;
303 if (0 >= pvr->stationlist.total || (!channel && !freq))
304 return -1;
306 /* select channel */
307 for (i = 0; i < pvr->stationlist.used; i++)
309 if (channel && !strcasecmp (pvr->stationlist.list[i].name, channel))
310 break; /* found existing channel entry */
312 if (freq > 0 && pvr->stationlist.list[i].freq == freq)
313 break; /* found existing frequency entry */
316 if (i < pvr->stationlist.used)
319 * found an existing entry,
320 * which is about to change with the user data.
321 * it is also enabled ..
323 if (!pvr->stationlist.list[i].enabled)
325 pvr->stationlist.list[i].enabled = 1;
326 pvr->stationlist.enabled++;
329 if (station)
330 av_strlcpy (pvr->stationlist.list[i].station,
331 station, PVR_STATION_NAME_SIZE);
332 else if (channel)
333 av_strlcpy (pvr->stationlist.list[i].station,
334 channel, PVR_STATION_NAME_SIZE);
335 else
336 snprintf (pvr->stationlist.list[i].station,
337 PVR_STATION_NAME_SIZE, "F %d", freq);
339 mp_msg (MSGT_OPEN, MSGL_DBG2,
340 "%s Set user station channel: %8s - freq: %8d - station: %s\n",
341 LOG_LEVEL_V4L2, pvr->stationlist.list[i].name,
342 pvr->stationlist.list[i].freq,
343 pvr->stationlist.list[i].station);
344 return 0;
347 /* from here on, we have to create a new entry, frequency is mandatory */
348 if (freq < 0)
350 mp_msg (MSGT_OPEN, MSGL_ERR,
351 "%s Cannot add new station/channel without frequency\n",
352 LOG_LEVEL_V4L2);
353 return -1;
356 if (pvr->stationlist.total < i)
359 * we have to extend the stationlist about
360 * an arbitrary size, even though this path is not performance critical
362 pvr->stationlist.total += 10;
363 pvr->stationlist.list =
364 realloc (pvr->stationlist.list,
365 pvr->stationlist.total * sizeof (station_elem_t));
367 if (!pvr->stationlist.list)
369 mp_msg (MSGT_OPEN, MSGL_ERR,
370 "%s No memory allocated for station list, giving up\n",
371 LOG_LEVEL_V4L2);
372 return -1;
375 /* clear the new space ..*/
376 memset (&(pvr->stationlist.list[pvr->stationlist.used]), 0,
377 (pvr->stationlist.total - pvr->stationlist.used)
378 * sizeof (station_elem_t));
381 /* here we go, our actual new entry */
382 pvr->stationlist.used++;
383 pvr->stationlist.list[i].enabled = 1;
384 pvr->stationlist.enabled++;
386 if (station)
387 av_strlcpy (pvr->stationlist.list[i].station,
388 station, PVR_STATION_NAME_SIZE);
389 if (channel)
390 av_strlcpy (pvr->stationlist.list[i].name, channel, PVR_STATION_NAME_SIZE);
391 else
392 snprintf (pvr->stationlist.list[i].name,
393 PVR_STATION_NAME_SIZE, "F %d", freq);
395 pvr->stationlist.list[i].freq = freq;
397 mp_msg (MSGT_OPEN, MSGL_DBG2,
398 "%s Add user station channel: %8s - freq: %8d - station: %s\n",
399 LOG_LEVEL_V4L2, pvr->stationlist.list[i].name,
400 pvr->stationlist.list[i].freq,
401 pvr->stationlist.list[i].station);
403 return 0;
407 * Here we set our stationlist, as follow
408 * - choose the frequency channel table, e.g. ntsc-cable
409 * - create our stationlist, same element size as the channellist
410 * - copy the channellist content to our stationlist
411 * - IF the user provides his channel-mapping, THEN:
412 * - disable all stations
413 * - update and/or create entries in the stationlist and enable them
415 static int
416 parse_setup_stationlist (struct pvr_t *pvr)
418 int i;
420 if (!pvr)
421 return -1;
423 /* Create our station/channel list */
424 if (stream_tv_defaults.chanlist)
426 /* select channel list */
427 for (i = 0; chanlists[i].name != NULL; i++)
429 if (!strcasecmp (chanlists[i].name, stream_tv_defaults.chanlist))
431 chantab = i;
432 break;
435 if (!chanlists[i].name)
437 mp_msg (MSGT_OPEN, MSGL_ERR,
438 "%s unable to find channel list %s, using default %s\n",
439 LOG_LEVEL_V4L2, stream_tv_defaults.chanlist, chanlists[chantab].name);
441 else
443 mp_msg (MSGT_OPEN, MSGL_INFO,
444 "%s select channel list %s, entries %d\n", LOG_LEVEL_V4L2,
445 chanlists[chantab].name, chanlists[chantab].count);
449 if (0 > chantab)
451 mp_msg (MSGT_OPEN, MSGL_FATAL,
452 "%s No channel list selected, giving up\n", LOG_LEVEL_V4L2);
453 return -1;
456 if (copycreate_stationlist (&(pvr->stationlist), -1) < 0)
458 mp_msg (MSGT_OPEN, MSGL_FATAL,
459 "%s No memory allocated for station list, giving up\n",
460 LOG_LEVEL_V4L2);
461 return -1;
464 /* Handle user channel mappings */
465 if (stream_tv_defaults.channels)
467 char channel[PVR_STATION_NAME_SIZE];
468 char station[PVR_STATION_NAME_SIZE];
469 char **channels = stream_tv_defaults.channels;
471 disable_all_stations (pvr);
473 while (*channels)
475 char *tmp = *(channels++);
476 char *sep = strchr (tmp, '-');
477 int freq=-1;
479 if (!sep)
480 continue; /* Wrong syntax, but mplayer should not crash */
482 av_strlcpy (station, sep + 1, PVR_STATION_NAME_SIZE);
484 sep[0] = '\0';
485 av_strlcpy (channel, tmp, PVR_STATION_NAME_SIZE);
487 while ((sep = strchr (station, '_')))
488 sep[0] = ' ';
490 /* if channel number is a number and larger than 1000 treat it as
491 * frequency tmp still contain pointer to null-terminated string with
492 * channel number here
494 if ((freq = atoi (channel)) <= 1000)
495 freq = -1;
497 if (set_station (pvr, station, (freq <= 0) ? channel : NULL, freq) < 0)
499 mp_msg (MSGT_OPEN, MSGL_ERR,
500 "%s Unable to set user station channel: %8s - freq: %8d - station: %s\n", LOG_LEVEL_V4L2,
501 channel, freq, station);
506 return print_all_stations (pvr);
509 static int
510 get_v4l2_freq (struct pvr_t *pvr)
512 int freq;
513 struct v4l2_frequency vf;
514 struct v4l2_tuner vt;
516 if (!pvr)
517 return -1;
519 if (pvr->dev_fd < 0)
520 return -1;
522 memset (&vt, 0, sizeof (vt));
523 memset (&vf, 0, sizeof (vf));
525 if (ioctl (pvr->dev_fd, VIDIOC_G_TUNER, &vt) < 0)
527 mp_msg (MSGT_OPEN, MSGL_ERR, "%s can't set tuner (%s).\n",
528 LOG_LEVEL_V4L2, strerror (errno));
529 return -1;
532 if (ioctl (pvr->dev_fd, VIDIOC_G_FREQUENCY, &vf) < 0)
534 mp_msg (MSGT_OPEN, MSGL_ERR, "%s can't get frequency %d.\n",
535 LOG_LEVEL_V4L2, errno);
536 return -1;
538 freq = vf.frequency;
539 if (!(vt.capability & V4L2_TUNER_CAP_LOW))
540 freq *= 1000;
541 freq /= 16;
543 return freq;
546 static int
547 set_v4l2_freq (struct pvr_t *pvr)
549 struct v4l2_frequency vf;
550 struct v4l2_tuner vt;
552 if (!pvr)
553 return -1;
555 if (0 >= pvr->freq)
557 mp_msg (MSGT_OPEN, MSGL_ERR,
558 "%s Frequency invalid %d !!!\n", LOG_LEVEL_V4L2, pvr->freq);
559 return -1;
562 /* don't set the frequency, if it's already set.
563 * setting it here would interrupt the stream.
565 if (get_v4l2_freq (pvr) == pvr->freq)
567 mp_msg (MSGT_OPEN, MSGL_STATUS,
568 "%s Frequency %d already set.\n", LOG_LEVEL_V4L2, pvr->freq);
569 return 0;
572 if (pvr->dev_fd < 0)
573 return -1;
575 memset (&vf, 0, sizeof (vf));
576 memset (&vt, 0, sizeof (vt));
578 if (ioctl (pvr->dev_fd, VIDIOC_G_TUNER, &vt) < 0)
580 mp_msg (MSGT_OPEN, MSGL_ERR, "%s can't get tuner (%s).\n",
581 LOG_LEVEL_V4L2, strerror (errno));
582 return -1;
585 vf.type = vt.type;
586 vf.frequency = pvr->freq * 16;
588 if (!(vt.capability & V4L2_TUNER_CAP_LOW))
589 vf.frequency /= 1000;
591 if (ioctl (pvr->dev_fd, VIDIOC_S_FREQUENCY, &vf) < 0)
593 mp_msg (MSGT_OPEN, MSGL_ERR, "%s can't set frequency (%s).\n",
594 LOG_LEVEL_V4L2, strerror (errno));
595 return -1;
598 memset (&vt, 0, sizeof(vt));
599 if (ioctl (pvr->dev_fd, VIDIOC_G_TUNER, &vt) < 0)
601 mp_msg (MSGT_OPEN, MSGL_ERR, "%s can't set tuner (%s).\n",
602 LOG_LEVEL_V4L2, strerror (errno));
603 return -1;
606 /* just a notification */
607 if (!vt.signal)
608 mp_msg (MSGT_OPEN, MSGL_ERR, "%s NO SIGNAL at frequency %d (%d)\n",
609 LOG_LEVEL_V4L2, pvr->freq, vf.frequency);
610 else
611 mp_msg (MSGT_OPEN, MSGL_STATUS, "%s Got signal at frequency %d (%d)\n",
612 LOG_LEVEL_V4L2, pvr->freq, vf.frequency);
614 return 0;
617 static int
618 set_station_by_step (struct pvr_t *pvr, int step, int v4lAction)
620 if (!pvr || !pvr->stationlist.list)
621 return -1;
623 if (pvr->stationlist.enabled >= abs (step))
625 int gotcha = 0;
626 int chidx = pvr->chan_idx + step;
628 while (!gotcha)
630 chidx = (chidx + pvr->stationlist.used) % pvr->stationlist.used;
632 mp_msg (MSGT_OPEN, MSGL_DBG2,
633 "%s Offset switch: current %d, enabled %d, step %d -> %d\n",
634 LOG_LEVEL_V4L2, pvr->chan_idx,
635 pvr->stationlist.enabled, step, chidx);
637 if (!pvr->stationlist.list[chidx].enabled)
639 mp_msg (MSGT_OPEN, MSGL_DBG2,
640 "%s Switch disabled to user station channel: %8s - freq: %8d - station: %s\n", LOG_LEVEL_V4L2,
641 pvr->stationlist.list[chidx].name,
642 pvr->stationlist.list[chidx].freq,
643 pvr->stationlist.list[chidx].station);
644 chidx += FFSIGN (step);
646 else
647 gotcha = 1;
650 pvr->freq = pvr->stationlist.list[chidx].freq;
651 pvr->chan_idx_last = pvr->chan_idx;
652 pvr->chan_idx = chidx;
654 mp_msg (MSGT_OPEN, MSGL_INFO,
655 "%s Switch to user station channel: %8s - freq: %8d - station: %s\n", LOG_LEVEL_V4L2,
656 pvr->stationlist.list[chidx].name,
657 pvr->stationlist.list[chidx].freq,
658 pvr->stationlist.list[chidx].station);
660 if (v4lAction)
661 return set_v4l2_freq (pvr);
663 return (pvr->freq > 0) ? 0 : -1;
666 mp_msg (MSGT_OPEN, MSGL_ERR,
667 "%s Ooops couldn't set freq by channel entry step %d to current %d, enabled %d\n", LOG_LEVEL_V4L2,
668 step, pvr->chan_idx, pvr->stationlist.enabled);
670 return -1;
673 static int
674 set_station_by_channelname_or_freq (struct pvr_t *pvr, const char *channel,
675 int freq, int v4lAction)
677 int i = 0;
679 if (!pvr || !pvr->stationlist.list)
680 return -1;
682 if (0 >= pvr->stationlist.enabled)
684 mp_msg (MSGT_OPEN, MSGL_WARN,
685 "%s No enabled station, cannot switch channel/frequency\n",
686 LOG_LEVEL_V4L2);
687 return -1;
690 if (channel)
692 /* select by channel */
693 for (i = 0; i < pvr->stationlist.used ; i++)
695 if (!strcasecmp (pvr->stationlist.list[i].name, channel))
697 if (!pvr->stationlist.list[i].enabled)
699 mp_msg (MSGT_OPEN, MSGL_WARN,
700 "%s Switch disabled to user station channel: %8s - freq: %8d - station: %s\n", LOG_LEVEL_V4L2,
701 pvr->stationlist.list[i].name,
702 pvr->stationlist.list[i].freq,
703 pvr->stationlist.list[i].station);
705 return -1;
708 pvr->freq = pvr->stationlist.list[i].freq;
709 pvr->chan_idx_last = pvr->chan_idx;
710 pvr->chan_idx = i;
711 break;
715 else if (freq >= 0)
717 /* select by freq */
718 for (i = 0; i < pvr->stationlist.used; i++)
720 if (pvr->stationlist.list[i].freq == freq)
722 if (!pvr->stationlist.list[i].enabled)
724 mp_msg (MSGT_OPEN, MSGL_WARN,
725 "%s Switch disabled to user station channel: %8s - freq: %8d - station: %s\n", LOG_LEVEL_V4L2,
726 pvr->stationlist.list[i].name,
727 pvr->stationlist.list[i].freq,
728 pvr->stationlist.list[i].station);
730 return -1;
733 pvr->freq = pvr->stationlist.list[i].freq;
734 pvr->chan_idx_last = pvr->chan_idx;
735 pvr->chan_idx = i;
736 break;
741 if (i >= pvr->stationlist.used)
743 if (channel)
744 mp_msg (MSGT_OPEN, MSGL_WARN,
745 "%s unable to find channel %s\n", LOG_LEVEL_V4L2, channel);
746 else
747 mp_msg (MSGT_OPEN, MSGL_WARN,
748 "%s unable to find frequency %d\n", LOG_LEVEL_V4L2, freq);
749 return -1;
752 mp_msg (MSGT_OPEN, MSGL_INFO,
753 "%s Switch to user station channel: %8s - freq: %8d - station: %s\n", LOG_LEVEL_V4L2,
754 pvr->stationlist.list[i].name,
755 pvr->stationlist.list[i].freq,
756 pvr->stationlist.list[i].station);
758 if (v4lAction)
759 return set_v4l2_freq (pvr);
761 return (pvr->freq > 0) ? 0 : -1;
764 static int
765 force_freq_step (struct pvr_t *pvr, int step)
767 int freq;
769 if (!pvr)
770 return -1;
772 freq = pvr->freq+step;
774 if (freq)
776 mp_msg (MSGT_OPEN, MSGL_INFO,
777 "%s Force Frequency %d + %d = %d \n", LOG_LEVEL_V4L2,
778 pvr->freq, step, freq);
780 pvr->freq = freq;
782 return set_v4l2_freq (pvr);
785 return -1;
788 static void
789 parse_encoder_options (struct pvr_t *pvr)
791 if (!pvr)
792 return;
794 /* -pvr aspect=digit */
795 if (pvr_param_aspect_ratio >= 0 && pvr_param_aspect_ratio <= 3)
796 pvr->aspect = pvr_param_aspect_ratio;
798 /* -pvr arate=x */
799 if (pvr_param_sample_rate != 0)
801 switch (pvr_param_sample_rate)
803 case 32000:
804 pvr->samplerate = V4L2_MPEG_AUDIO_SAMPLING_FREQ_32000;
805 break;
806 case 44100:
807 pvr->samplerate = V4L2_MPEG_AUDIO_SAMPLING_FREQ_44100;
808 break;
809 case 48000:
810 pvr->samplerate = V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000;
811 break;
812 default:
813 break;
817 /* -pvr alayer=x */
818 if (pvr_param_audio_layer == 1)
819 pvr->layer = V4L2_MPEG_AUDIO_ENCODING_LAYER_1;
820 else if (pvr_param_audio_layer == 2)
821 pvr->layer = V4L2_MPEG_AUDIO_ENCODING_LAYER_2;
822 else if (pvr_param_audio_layer == 3)
823 pvr->layer = V4L2_MPEG_AUDIO_ENCODING_LAYER_3;
825 /* -pvr abitrate=x */
826 if (pvr_param_audio_bitrate != 0)
828 if (pvr->layer == V4L2_MPEG_AUDIO_ENCODING_LAYER_1)
830 switch (pvr_param_audio_bitrate)
832 case 32:
833 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_32K;
834 break;
835 case 64:
836 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_64K;
837 break;
838 case 96:
839 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_96K;
840 break;
841 case 128:
842 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_128K;
843 break;
844 case 160:
845 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_160K;
846 break;
847 case 192:
848 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_192K;
849 break;
850 case 224:
851 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_224K;
852 break;
853 case 256:
854 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_256K;
855 break;
856 case 288:
857 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_288K;
858 break;
859 case 320:
860 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_320K;
861 break;
862 case 352:
863 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_352K;
864 break;
865 case 384:
866 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_384K;
867 break;
868 case 416:
869 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_416K;
870 break;
871 case 448:
872 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_448K;
873 break;
874 default:
875 break;
879 else if (pvr->layer == V4L2_MPEG_AUDIO_ENCODING_LAYER_2)
881 switch (pvr_param_audio_bitrate)
883 case 32:
884 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_32K;
885 break;
886 case 48:
887 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_48K;
888 break;
889 case 56:
890 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_56K;
891 break;
892 case 64:
893 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_64K;
894 break;
895 case 80:
896 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_80K;
897 break;
898 case 96:
899 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_96K;
900 break;
901 case 112:
902 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_112K;
903 break;
904 case 128:
905 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_128K;
906 break;
907 case 160:
908 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_160K;
909 break;
910 case 192:
911 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_192K;
912 break;
913 case 224:
914 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_224K;
915 break;
916 case 256:
917 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_256K;
918 break;
919 case 320:
920 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_320K;
921 break;
922 case 384:
923 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_384K;
924 break;
925 default:
926 break;
930 else if (pvr->layer == V4L2_MPEG_AUDIO_ENCODING_LAYER_3)
932 switch (pvr_param_audio_bitrate)
934 case 32:
935 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_32K;
936 break;
937 case 40:
938 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_40K;
939 break;
940 case 48:
941 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_48K;
942 break;
943 case 56:
944 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_56K;
945 break;
946 case 64:
947 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_64K;
948 break;
949 case 80:
950 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_80K;
951 break;
952 case 96:
953 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_96K;
954 break;
955 case 112:
956 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_112K;
957 break;
958 case 128:
959 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_128K;
960 break;
961 case 160:
962 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_160K;
963 break;
964 case 192:
965 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_192K;
966 break;
967 case 224:
968 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_224K;
969 break;
970 case 256:
971 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_256K;
972 break;
973 case 320:
974 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_320K;
975 break;
976 default:
977 break;
982 /* -pvr amode=x */
983 if (pvr_param_audio_mode)
985 if (!strcmp (pvr_param_audio_mode, PVR_AUDIO_MODE_ARG_STEREO))
986 pvr->audio_mode = V4L2_MPEG_AUDIO_MODE_STEREO;
987 else if (!strcmp (pvr_param_audio_mode, PVR_AUDIO_MODE_ARG_JOINT_STEREO))
988 pvr->audio_mode = V4L2_MPEG_AUDIO_MODE_JOINT_STEREO;
989 else if (!strcmp (pvr_param_audio_mode, PVR_AUDIO_MODE_ARG_DUAL))
990 pvr->audio_mode = V4L2_MPEG_AUDIO_MODE_DUAL;
991 else if (!strcmp (pvr_param_audio_mode, PVR_AUDIO_MODE_ARG_MONO))
992 pvr->audio_mode = V4L2_MPEG_AUDIO_MODE_MONO;
995 /* -pvr vbitrate=x */
996 if (pvr_param_bitrate)
997 pvr->bitrate = pvr_param_bitrate;
999 /* -pvr vmode=x */
1000 if (pvr_param_bitrate_mode)
1002 if (!strcmp (pvr_param_bitrate_mode, PVR_VIDEO_BITRATE_MODE_ARG_VBR))
1003 pvr->bitrate_mode = V4L2_MPEG_VIDEO_BITRATE_MODE_VBR;
1004 else if (!strcmp (pvr_param_bitrate_mode, PVR_VIDEO_BITRATE_MODE_ARG_CBR))
1005 pvr->bitrate_mode = V4L2_MPEG_VIDEO_BITRATE_MODE_CBR;
1008 /* -pvr vpeak=x */
1009 if (pvr_param_bitrate_peak)
1010 pvr->bitrate_peak = pvr_param_bitrate_peak;
1012 /* -pvr fmt=x */
1013 if (pvr_param_stream_type)
1015 if (!strcmp (pvr_param_stream_type, PVR_VIDEO_STREAM_TYPE_PS))
1016 pvr->stream_type = V4L2_MPEG_STREAM_TYPE_MPEG2_PS;
1017 else if (!strcmp (pvr_param_stream_type, PVR_VIDEO_STREAM_TYPE_TS))
1018 pvr->stream_type = V4L2_MPEG_STREAM_TYPE_MPEG2_TS;
1019 else if (!strcmp (pvr_param_stream_type, PVR_VIDEO_STREAM_TYPE_MPEG1))
1020 pvr->stream_type = V4L2_MPEG_STREAM_TYPE_MPEG1_SS;
1021 else if (!strcmp (pvr_param_stream_type, PVR_VIDEO_STREAM_TYPE_DVD))
1022 pvr->stream_type = V4L2_MPEG_STREAM_TYPE_MPEG2_DVD;
1023 else if (!strcmp (pvr_param_stream_type, PVR_VIDEO_STREAM_TYPE_VCD))
1024 pvr->stream_type = V4L2_MPEG_STREAM_TYPE_MPEG1_VCD;
1025 else if (!strcmp (pvr_param_stream_type, PVR_VIDEO_STREAM_TYPE_SVCD))
1026 pvr->stream_type = V4L2_MPEG_STREAM_TYPE_MPEG2_SVCD;
1030 static void
1031 add_v4l2_ext_control (struct v4l2_ext_control *ctrl,
1032 uint32_t id, int32_t value)
1034 ctrl->id = id;
1035 ctrl->value = value;
1038 static int
1039 set_encoder_settings (struct pvr_t *pvr)
1041 struct v4l2_ext_control *ext_ctrl = NULL;
1042 struct v4l2_ext_controls ctrls;
1043 uint32_t count = 0;
1045 if (!pvr)
1046 return -1;
1048 if (pvr->dev_fd < 0)
1049 return -1;
1051 ext_ctrl = (struct v4l2_ext_control *)
1052 malloc (PVR_MAX_CONTROLS * sizeof (struct v4l2_ext_control));
1054 add_v4l2_ext_control (&ext_ctrl[count++], V4L2_CID_MPEG_VIDEO_ASPECT,
1055 pvr->aspect);
1057 add_v4l2_ext_control (&ext_ctrl[count++], V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
1058 pvr->samplerate);
1060 add_v4l2_ext_control (&ext_ctrl[count++], V4L2_CID_MPEG_AUDIO_ENCODING,
1061 pvr->layer);
1063 switch (pvr->layer)
1065 case V4L2_MPEG_AUDIO_ENCODING_LAYER_1:
1066 add_v4l2_ext_control (&ext_ctrl[count++], V4L2_CID_MPEG_AUDIO_L1_BITRATE,
1067 pvr->audio_rate);
1068 break;
1069 case V4L2_MPEG_AUDIO_ENCODING_LAYER_2:
1070 add_v4l2_ext_control (&ext_ctrl[count++], V4L2_CID_MPEG_AUDIO_L2_BITRATE,
1071 pvr->audio_rate);
1072 break;
1073 case V4L2_MPEG_AUDIO_ENCODING_LAYER_3:
1074 add_v4l2_ext_control (&ext_ctrl[count++], V4L2_CID_MPEG_AUDIO_L3_BITRATE,
1075 pvr->audio_rate);
1076 break;
1077 default:
1078 break;
1081 add_v4l2_ext_control (&ext_ctrl[count++], V4L2_CID_MPEG_AUDIO_MODE,
1082 pvr->audio_mode);
1084 add_v4l2_ext_control (&ext_ctrl[count++], V4L2_CID_MPEG_VIDEO_BITRATE,
1085 pvr->bitrate);
1087 add_v4l2_ext_control (&ext_ctrl[count++], V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
1088 pvr->bitrate_peak);
1090 add_v4l2_ext_control (&ext_ctrl[count++], V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
1091 pvr->bitrate_mode);
1093 add_v4l2_ext_control (&ext_ctrl[count++], V4L2_CID_MPEG_STREAM_TYPE,
1094 pvr->stream_type);
1096 /* set new encoding settings */
1097 ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
1098 ctrls.count = count;
1099 ctrls.controls = ext_ctrl;
1101 if (ioctl (pvr->dev_fd, VIDIOC_S_EXT_CTRLS, &ctrls) < 0)
1103 mp_msg (MSGT_OPEN, MSGL_ERR, "%s Error setting MPEG controls (%s).\n",
1104 LOG_LEVEL_ENCODER, strerror (errno));
1105 free (ext_ctrl);
1106 return -1;
1109 free (ext_ctrl);
1111 return 0;
1114 static void
1115 parse_v4l2_tv_options (struct pvr_t *pvr)
1117 if (!pvr)
1118 return;
1120 /* Create our station/channel list */
1121 parse_setup_stationlist (pvr);
1123 if (pvr->param_channel)
1125 if (set_station_by_channelname_or_freq (pvr, pvr->param_channel,
1126 -1, 0) >= 0)
1128 if (stream_tv_defaults.freq)
1130 mp_msg (MSGT_OPEN, MSGL_HINT,
1131 "%s tv param freq %s is overwritten by channel setting freq %d\n", LOG_LEVEL_V4L2,
1132 stream_tv_defaults.freq, pvr->freq);
1137 if (pvr->freq < 0 && stream_tv_defaults.freq)
1139 mp_msg (MSGT_OPEN, MSGL_HINT, "%s tv param freq %s is used directly\n",
1140 LOG_LEVEL_V4L2, stream_tv_defaults.freq);
1142 if (set_station_by_channelname_or_freq (pvr, NULL,
1143 atoi (stream_tv_defaults.freq), 0)<0)
1145 mp_msg (MSGT_OPEN, MSGL_WARN,
1146 "%s tv param freq %s invalid to set station\n",
1147 LOG_LEVEL_V4L2, stream_tv_defaults.freq);
1151 if (stream_tv_defaults.device)
1153 if (pvr->video_dev)
1154 free (pvr->video_dev);
1155 pvr->video_dev = strdup (stream_tv_defaults.device);
1158 if (stream_tv_defaults.noaudio)
1159 pvr->mute = stream_tv_defaults.noaudio;
1161 if (stream_tv_defaults.input)
1162 pvr->input = stream_tv_defaults.input;
1164 if (stream_tv_defaults.normid)
1165 pvr->normid = stream_tv_defaults.normid;
1167 if (stream_tv_defaults.brightness)
1168 pvr->brightness = stream_tv_defaults.brightness;
1170 if (stream_tv_defaults.contrast)
1171 pvr->contrast = stream_tv_defaults.contrast;
1173 if (stream_tv_defaults.hue)
1174 pvr->hue = stream_tv_defaults.hue;
1176 if (stream_tv_defaults.saturation)
1177 pvr->saturation = stream_tv_defaults.saturation;
1179 if (stream_tv_defaults.width)
1180 pvr->width = stream_tv_defaults.width;
1182 if (stream_tv_defaults.height)
1183 pvr->height = stream_tv_defaults.height;
1186 static int
1187 set_v4l2_settings (struct pvr_t *pvr)
1189 if (!pvr)
1190 return -1;
1192 if (pvr->dev_fd < 0)
1193 return -1;
1195 /* -tv noaudio */
1196 if (pvr->mute)
1198 struct v4l2_control ctrl;
1199 ctrl.id = V4L2_CID_AUDIO_MUTE;
1200 ctrl.value = 1;
1201 if (ioctl (pvr->dev_fd, VIDIOC_S_CTRL, &ctrl) < 0)
1203 mp_msg (MSGT_OPEN, MSGL_ERR,
1204 "%s can't mute (%s).\n", LOG_LEVEL_V4L2, strerror (errno));
1205 return -1;
1209 /* -tv input=x */
1210 if (pvr->input != 0)
1212 if (ioctl (pvr->dev_fd, VIDIOC_S_INPUT, &pvr->input) < 0)
1214 mp_msg (MSGT_OPEN, MSGL_ERR,
1215 "%s can't set input (%s)\n", LOG_LEVEL_V4L2, strerror (errno));
1216 return -1;
1220 /* -tv normid=x */
1221 if (pvr->normid != -1)
1223 struct v4l2_standard std;
1224 std.index = pvr->normid;
1226 if (ioctl (pvr->dev_fd, VIDIOC_ENUMSTD, &std) < 0)
1228 mp_msg (MSGT_OPEN, MSGL_ERR,
1229 "%s can't set norm (%s)\n", LOG_LEVEL_V4L2, strerror (errno));
1230 return -1;
1233 mp_msg (MSGT_OPEN, MSGL_V,
1234 "%s set norm to %s\n", LOG_LEVEL_V4L2, std.name);
1236 if (ioctl (pvr->dev_fd, VIDIOC_S_STD, &std.id) < 0)
1238 mp_msg (MSGT_OPEN, MSGL_ERR,
1239 "%s can't set norm (%s)\n", LOG_LEVEL_V4L2, strerror (errno));
1240 return -1;
1244 /* -tv brightness=x */
1245 if (pvr->brightness != 0)
1247 struct v4l2_control ctrl;
1248 ctrl.id = V4L2_CID_BRIGHTNESS;
1249 ctrl.value = pvr->brightness;
1251 if (ctrl.value < 0)
1252 ctrl.value = 0;
1253 if (ctrl.value > 255)
1254 ctrl.value = 255;
1256 if (ioctl (pvr->dev_fd, VIDIOC_S_CTRL, &ctrl) < 0)
1258 mp_msg (MSGT_OPEN, MSGL_ERR,
1259 "%s can't set brightness to %d (%s).\n",
1260 LOG_LEVEL_V4L2, ctrl.value, strerror (errno));
1261 return -1;
1265 /* -tv contrast=x */
1266 if (pvr->contrast != 0)
1268 struct v4l2_control ctrl;
1269 ctrl.id = V4L2_CID_CONTRAST;
1270 ctrl.value = pvr->contrast;
1272 if (ctrl.value < 0)
1273 ctrl.value = 0;
1274 if (ctrl.value > 127)
1275 ctrl.value = 127;
1277 if (ioctl (pvr->dev_fd, VIDIOC_S_CTRL, &ctrl) < 0)
1279 mp_msg (MSGT_OPEN, MSGL_ERR,
1280 "%s can't set contrast to %d (%s).\n",
1281 LOG_LEVEL_V4L2, ctrl.value, strerror (errno));
1282 return -1;
1286 /* -tv hue=x */
1287 if (pvr->hue != 0)
1289 struct v4l2_control ctrl;
1290 ctrl.id = V4L2_CID_HUE;
1291 ctrl.value = pvr->hue;
1293 if (ctrl.value < -128)
1294 ctrl.value = -128;
1295 if (ctrl.value > 127)
1296 ctrl.value = 127;
1298 if (ioctl (pvr->dev_fd, VIDIOC_S_CTRL, &ctrl) < 0)
1300 mp_msg (MSGT_OPEN, MSGL_ERR,
1301 "%s can't set hue to %d (%s).\n",
1302 LOG_LEVEL_V4L2, ctrl.value, strerror (errno));
1303 return -1;
1307 /* -tv saturation=x */
1308 if (pvr->saturation != 0)
1310 struct v4l2_control ctrl;
1311 ctrl.id = V4L2_CID_SATURATION;
1312 ctrl.value = pvr->saturation;
1314 if (ctrl.value < 0)
1315 ctrl.value = 0;
1316 if (ctrl.value > 127)
1317 ctrl.value = 127;
1319 if (ioctl (pvr->dev_fd, VIDIOC_S_CTRL, &ctrl) < 0)
1321 mp_msg (MSGT_OPEN, MSGL_ERR,
1322 "%s can't set saturation to %d (%s).\n",
1323 LOG_LEVEL_V4L2, ctrl.value, strerror (errno));
1324 return -1;
1328 /* -tv width=x:height=y */
1329 if (pvr->width && pvr->height)
1331 struct v4l2_format vfmt;
1332 vfmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1333 vfmt.fmt.pix.width = pvr->width;
1334 vfmt.fmt.pix.height = pvr->height;
1336 if (ioctl (pvr->dev_fd, VIDIOC_S_FMT, &vfmt) < 0)
1338 mp_msg (MSGT_OPEN, MSGL_ERR,
1339 "%s can't set resolution to %dx%d (%s).\n",
1340 LOG_LEVEL_V4L2, pvr->width, pvr->height, strerror (errno));
1341 return -1;
1345 if (pvr->freq < 0)
1347 int freq = get_v4l2_freq (pvr);
1348 mp_msg (MSGT_OPEN, MSGL_INFO,
1349 "%s Using current set frequency %d, to set channel\n",
1350 LOG_LEVEL_V4L2, freq);
1352 if (0 < freq)
1353 return set_station_by_channelname_or_freq (pvr, NULL, freq, 1);
1356 if (0 < pvr->freq)
1357 return set_v4l2_freq (pvr) ;
1359 return 0;
1362 static int
1363 v4l2_list_capabilities (struct pvr_t *pvr)
1365 struct v4l2_audio vaudio;
1366 struct v4l2_standard vs;
1367 struct v4l2_input vin;
1368 int err = 0;
1370 if (!pvr)
1371 return -1;
1373 if (pvr->dev_fd < 0)
1374 return -1;
1376 /* list available video inputs */
1377 vin.index = 0;
1378 err = 1;
1379 mp_msg (MSGT_OPEN, MSGL_INFO,
1380 "%s Available video inputs: ", LOG_LEVEL_V4L2);
1381 while (ioctl (pvr->dev_fd, VIDIOC_ENUMINPUT, &vin) >= 0)
1383 err = 0;
1384 mp_msg (MSGT_OPEN, MSGL_INFO, "'#%d, %s' ", vin.index, vin.name);
1385 vin.index++;
1387 if (err)
1389 mp_msg (MSGT_OPEN, MSGL_INFO, "none\n");
1390 return -1;
1392 else
1393 mp_msg (MSGT_OPEN, MSGL_INFO, "\n");
1395 /* list available audio inputs */
1396 vaudio.index = 0;
1397 err = 1;
1398 mp_msg (MSGT_OPEN, MSGL_INFO,
1399 "%s Available audio inputs: ", LOG_LEVEL_V4L2);
1400 while (ioctl (pvr->dev_fd, VIDIOC_ENUMAUDIO, &vaudio) >= 0)
1402 err = 0;
1403 mp_msg (MSGT_OPEN, MSGL_INFO, "'#%d, %s' ", vaudio.index, vaudio.name);
1404 vaudio.index++;
1406 if (err)
1408 mp_msg (MSGT_OPEN, MSGL_INFO, "none\n");
1409 return -1;
1411 else
1412 mp_msg (MSGT_OPEN, MSGL_INFO, "\n");
1414 /* list available norms */
1415 vs.index = 0;
1416 mp_msg (MSGT_OPEN, MSGL_INFO, "%s Available norms: ", LOG_LEVEL_V4L2);
1417 while (ioctl (pvr->dev_fd, VIDIOC_ENUMSTD, &vs) >= 0)
1419 err = 0;
1420 mp_msg (MSGT_OPEN, MSGL_INFO, "'#%d, %s' ", vs.index, vs.name);
1421 vs.index++;
1423 if (err)
1425 mp_msg (MSGT_OPEN, MSGL_INFO, "none\n");
1426 return -1;
1428 else
1429 mp_msg (MSGT_OPEN, MSGL_INFO, "\n");
1431 return 0;
1434 static int
1435 v4l2_display_settings (struct pvr_t *pvr)
1437 struct v4l2_audio vaudio;
1438 struct v4l2_standard vs;
1439 struct v4l2_input vin;
1440 v4l2_std_id std;
1441 int input;
1443 if (!pvr)
1444 return -1;
1446 if (pvr->dev_fd < 0)
1447 return -1;
1449 /* get current video input */
1450 if (ioctl (pvr->dev_fd, VIDIOC_G_INPUT, &input) == 0)
1452 vin.index = input;
1453 if (ioctl (pvr->dev_fd, VIDIOC_ENUMINPUT, &vin) < 0)
1455 mp_msg (MSGT_OPEN, MSGL_ERR,
1456 "%s can't get input (%s).\n", LOG_LEVEL_V4L2, strerror (errno));
1457 return -1;
1459 else
1460 mp_msg (MSGT_OPEN, MSGL_INFO,
1461 "%s Video input: %s\n", LOG_LEVEL_V4L2, vin.name);
1463 else
1465 mp_msg (MSGT_OPEN, MSGL_ERR,
1466 "%s can't get input (%s).\n", LOG_LEVEL_V4L2, strerror (errno));
1467 return -1;
1470 /* get current audio input */
1471 if (ioctl (pvr->dev_fd, VIDIOC_G_AUDIO, &vaudio) == 0)
1473 mp_msg (MSGT_OPEN, MSGL_INFO,
1474 "%s Audio input: %s\n", LOG_LEVEL_V4L2, vaudio.name);
1476 else
1478 mp_msg (MSGT_OPEN, MSGL_ERR,
1479 "%s can't get input (%s).\n", LOG_LEVEL_V4L2, strerror (errno));
1480 return -1;
1483 /* get current video format */
1484 if (ioctl (pvr->dev_fd, VIDIOC_G_STD, &std) == 0)
1486 vs.index = 0;
1488 while (ioctl (pvr->dev_fd, VIDIOC_ENUMSTD, &vs) >= 0)
1490 if (vs.id == std)
1492 mp_msg (MSGT_OPEN, MSGL_INFO,
1493 "%s Norm: %s.\n", LOG_LEVEL_V4L2, vs.name);
1494 break;
1496 vs.index++;
1499 else
1501 mp_msg (MSGT_OPEN, MSGL_ERR,
1502 "%s can't get norm (%s)\n", LOG_LEVEL_V4L2, strerror (errno));
1503 return -1;
1506 return 0;
1509 /* stream layer */
1511 static void
1512 pvr_stream_close (stream_t *stream)
1514 struct pvr_t *pvr;
1516 if (!stream)
1517 return;
1519 pvr = (struct pvr_t *) stream->priv;
1520 pvr_uninit (pvr);
1523 static int
1524 pvr_stream_read (stream_t *stream, char *buffer, int size)
1526 struct pollfd pfds[1];
1527 struct pvr_t *pvr;
1528 int rk, fd, pos;
1530 if (!stream || !buffer)
1531 return 0;
1533 pvr = (struct pvr_t *) stream->priv;
1534 fd = pvr->dev_fd;
1535 pos = 0;
1537 if (fd < 0)
1538 return 0;
1540 while (pos < size)
1542 pfds[0].fd = fd;
1543 pfds[0].events = POLLIN | POLLPRI;
1545 rk = size - pos;
1547 if (poll (pfds, 1, 500) <= 0)
1549 mp_msg (MSGT_OPEN, MSGL_ERR,
1550 "%s failed with errno %d when reading %d bytes\n",
1551 LOG_LEVEL_PVR, errno, size-pos);
1552 break;
1555 rk = read (fd, &buffer[pos], rk);
1556 if (rk > 0)
1558 pos += rk;
1559 mp_msg (MSGT_OPEN, MSGL_DBG3,
1560 "%s read (%d) bytes\n", LOG_LEVEL_PVR, pos);
1564 if (!pos)
1565 mp_msg (MSGT_OPEN, MSGL_ERR, "%s read %d bytes\n", LOG_LEVEL_PVR, pos);
1567 return pos;
1570 static int
1571 pvr_stream_open (stream_t *stream, int mode, void *opts, int *file_format)
1573 struct v4l2_capability vcap;
1574 struct v4l2_ext_controls ctrls;
1575 struct pvr_t *pvr = NULL;
1577 if (mode != STREAM_READ)
1578 return STREAM_UNSUPPORTED;
1580 pvr = pvr_init ();
1583 * if the url, i.e. 'pvr://8', contains the channel, use it,
1584 * else use the tv parameter.
1586 if (stream->url && strlen (stream->url) > 6 && stream->url[6] != '\0')
1587 pvr->param_channel = strdup (stream->url + 6);
1588 else if (stream_tv_defaults.channel && strlen (stream_tv_defaults.channel))
1589 pvr->param_channel = strdup (stream_tv_defaults.channel);
1591 parse_v4l2_tv_options (pvr);
1592 parse_encoder_options (pvr);
1594 /* open device */
1595 pvr->dev_fd = open (pvr->video_dev, O_RDWR);
1596 mp_msg (MSGT_OPEN, MSGL_INFO,
1597 "%s Using device %s\n", LOG_LEVEL_PVR, pvr->video_dev);
1598 if (pvr->dev_fd == -1)
1600 mp_msg (MSGT_OPEN, MSGL_ERR,
1601 "%s error opening device %s\n", LOG_LEVEL_PVR, pvr->video_dev);
1602 pvr_uninit (pvr);
1603 return STREAM_ERROR;
1606 /* query capabilities (i.e test V4L2 support) */
1607 if (ioctl (pvr->dev_fd, VIDIOC_QUERYCAP, &vcap) < 0)
1609 mp_msg (MSGT_OPEN, MSGL_ERR,
1610 "%s device is not V4L2 compliant (%s).\n",
1611 LOG_LEVEL_PVR, strerror (errno));
1612 pvr_uninit (pvr);
1613 return STREAM_ERROR;
1615 else
1616 mp_msg (MSGT_OPEN, MSGL_INFO,
1617 "%s Detected %s\n", LOG_LEVEL_PVR, vcap.card);
1619 /* check for a valid V4L2 capture device */
1620 if (!(vcap.capabilities & V4L2_CAP_VIDEO_CAPTURE))
1622 mp_msg (MSGT_OPEN, MSGL_ERR,
1623 "%s device is not a valid V4L2 capture device.\n",
1624 LOG_LEVEL_PVR);
1625 pvr_uninit (pvr);
1626 return STREAM_ERROR;
1629 /* check for device hardware MPEG encoding capability */
1630 ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
1631 ctrls.count = 0;
1632 ctrls.controls = NULL;
1634 if (ioctl (pvr->dev_fd, VIDIOC_G_EXT_CTRLS, &ctrls) < 0)
1636 mp_msg (MSGT_OPEN, MSGL_ERR,
1637 "%s device do not support MPEG input.\n", LOG_LEVEL_ENCODER);
1638 return STREAM_ERROR;
1641 /* list V4L2 capabilities */
1642 if (v4l2_list_capabilities (pvr) == -1)
1644 mp_msg (MSGT_OPEN, MSGL_ERR,
1645 "%s can't get v4l2 capabilities\n", LOG_LEVEL_PVR);
1646 pvr_uninit (pvr);
1647 return STREAM_ERROR;
1650 /* apply V4L2 settings */
1651 if (set_v4l2_settings (pvr) == -1)
1653 mp_msg (MSGT_OPEN, MSGL_ERR,
1654 "%s can't set v4l2 settings\n", LOG_LEVEL_PVR);
1655 pvr_uninit (pvr);
1656 return STREAM_ERROR;
1659 /* apply encoder settings */
1660 if (set_encoder_settings (pvr) == -1)
1662 mp_msg (MSGT_OPEN, MSGL_ERR,
1663 "%s can't set encoder settings\n", LOG_LEVEL_PVR);
1664 pvr_uninit (pvr);
1665 return STREAM_ERROR;
1668 /* display current V4L2 settings */
1669 if (v4l2_display_settings (pvr) == -1)
1671 mp_msg (MSGT_OPEN, MSGL_ERR,
1672 "%s can't get v4l2 settings\n", LOG_LEVEL_PVR);
1673 pvr_uninit (pvr);
1674 return STREAM_ERROR;
1677 stream->priv = pvr;
1678 stream->type = STREAMTYPE_PVR;
1679 stream->fill_buffer = pvr_stream_read;
1680 stream->close = pvr_stream_close;
1682 return STREAM_OK;
1685 /* PVR Public API access */
1687 const char *
1688 pvr_get_current_stationname (stream_t *stream)
1690 struct pvr_t *pvr;
1692 if (!stream || stream->type != STREAMTYPE_PVR)
1693 return NULL;
1695 pvr = (struct pvr_t *) stream->priv;
1697 if (pvr->stationlist.list &&
1698 pvr->stationlist.used > pvr->chan_idx &&
1699 pvr->chan_idx >= 0)
1700 return pvr->stationlist.list[pvr->chan_idx].station;
1702 return NULL;
1705 const char *
1706 pvr_get_current_channelname (stream_t *stream)
1708 struct pvr_t *pvr = (struct pvr_t *) stream->priv;
1710 if (pvr->stationlist.list &&
1711 pvr->stationlist.used > pvr->chan_idx &&
1712 pvr->chan_idx >= 0)
1713 return pvr->stationlist.list[pvr->chan_idx].name;
1715 return NULL;
1719 pvr_get_current_frequency (stream_t *stream)
1721 struct pvr_t *pvr = (struct pvr_t *) stream->priv;
1723 return pvr->freq;
1727 pvr_set_channel (stream_t *stream, const char * channel)
1729 struct pvr_t *pvr = (struct pvr_t *) stream->priv;
1731 return set_station_by_channelname_or_freq (pvr, channel, -1, 1);
1735 pvr_set_lastchannel (stream_t *stream)
1737 struct pvr_t *pvr = (struct pvr_t *) stream->priv;
1739 if (pvr->stationlist.list &&
1740 pvr->stationlist.used > pvr->chan_idx_last &&
1741 pvr->chan_idx_last >= 0)
1742 return set_station_by_channelname_or_freq (pvr, pvr->stationlist.list[pvr->chan_idx_last].name, -1, 1);
1744 return -1;
1748 pvr_set_freq (stream_t *stream, int freq)
1750 struct pvr_t *pvr = (struct pvr_t *) stream->priv;
1752 return set_station_by_channelname_or_freq (pvr, NULL, freq, 1);
1756 pvr_set_channel_step (stream_t *stream, int step)
1758 struct pvr_t *pvr = (struct pvr_t *) stream->priv;
1760 return set_station_by_step (pvr, step, 1);
1764 pvr_force_freq_step (stream_t *stream, int step)
1766 struct pvr_t *pvr = (struct pvr_t *) stream->priv;
1768 return force_freq_step (pvr, step);
1771 const stream_info_t stream_info_pvr = {
1772 "V4L2 MPEG Input (a.k.a PVR)",
1773 "pvr",
1774 "Benjamin Zores",
1776 pvr_stream_open,
1777 { "pvr", NULL },
1778 NULL,