Add explanatory comments to the #endif part of multiple inclusion guards.
[mplayer/greg.git] / stream / stream_pvr.c
blobeeedbc1b759e4d7e890e0ab5e8a129d9e3345fd9
1 /*
2 * Copyright (C) 2006 Benjamin Zores
3 * Copyright (C) 2007 Sven Gothel (Channel Navigation)
4 * Stream layer for hardware MPEG 1/2/4 encoders a.k.a PVR
5 * (such as WinTV PVR-150/250/350/500 (a.k.a IVTV), pvrusb2 and cx88).
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 <unistd.h>
29 #include <string.h>
30 #include <ctype.h>
31 #include <sys/time.h>
32 #include <errno.h>
33 #include <sys/ioctl.h>
34 #include <sys/fcntl.h>
35 #include <inttypes.h>
36 #include <sys/poll.h>
37 #include <linux/types.h>
38 #include <linux/videodev2.h>
40 #include "mp_msg.h"
41 #include "help_mp.h"
43 #include "stream.h"
44 #include "pvr.h"
46 #include "frequencies.h"
47 #include "libavutil/common.h"
48 #include "libavutil/avstring.h"
50 #define PVR_DEFAULT_DEVICE "/dev/video0"
51 #define PVR_MAX_CONTROLS 10
53 /* logging mechanisms */
54 #define LOG_LEVEL_PVR "[pvr]"
55 #define LOG_LEVEL_V4L2 "[v4l2]"
56 #define LOG_LEVEL_ENCODER "[encoder]"
58 /* audio codec mode */
59 #define PVR_AUDIO_MODE_ARG_STEREO "stereo"
60 #define PVR_AUDIO_MODE_ARG_JOINT_STEREO "joint_stereo"
61 #define PVR_AUDIO_MODE_ARG_DUAL "dual"
62 #define PVR_AUDIO_MODE_ARG_MONO "mono"
64 /* video codec bitrate mode */
65 #define PVR_VIDEO_BITRATE_MODE_ARG_VBR "vbr"
66 #define PVR_VIDEO_BITRATE_MODE_ARG_CBR "cbr"
68 /* video codec stream type */
69 #define PVR_VIDEO_STREAM_TYPE_PS "ps"
70 #define PVR_VIDEO_STREAM_TYPE_TS "ts"
71 #define PVR_VIDEO_STREAM_TYPE_MPEG1 "mpeg1"
72 #define PVR_VIDEO_STREAM_TYPE_DVD "dvd"
73 #define PVR_VIDEO_STREAM_TYPE_VCD "vcd"
74 #define PVR_VIDEO_STREAM_TYPE_SVCD "svcd"
76 #define PVR_STATION_NAME_SIZE 256
78 /* command line arguments */
79 int pvr_param_aspect_ratio = 0;
80 int pvr_param_sample_rate = 0;
81 int pvr_param_audio_layer = 0;
82 int pvr_param_audio_bitrate = 0;
83 char *pvr_param_audio_mode = NULL;
84 int pvr_param_bitrate = 0;
85 char *pvr_param_bitrate_mode = NULL;
86 int pvr_param_bitrate_peak = 0;
87 char *pvr_param_stream_type = NULL;
89 typedef struct station_elem_s {
90 char name[8];
91 int freq;
92 char station[PVR_STATION_NAME_SIZE];
93 int enabled;
94 } station_elem_t;
96 typedef struct stationlist_s {
97 char name[PVR_STATION_NAME_SIZE];
98 station_elem_t *list;
99 int total; /* total number */
100 int used; /* used number */
101 int enabled; /* enabled number */
102 } stationlist_t;
104 struct pvr_t {
105 int dev_fd;
106 char *video_dev;
108 /* v4l2 params */
109 int mute;
110 int input;
111 int normid;
112 int brightness;
113 int contrast;
114 int hue;
115 int saturation;
116 int width;
117 int height;
118 int freq;
119 int chan_idx;
120 int chan_idx_last;
121 stationlist_t stationlist;
122 /* dups the tv_param_channel, or the url's channel param */
123 char *param_channel;
125 /* encoder params */
126 int aspect;
127 int samplerate;
128 int layer;
129 int audio_rate;
130 int audio_mode;
131 int bitrate;
132 int bitrate_mode;
133 int bitrate_peak;
134 int stream_type;
137 static struct pvr_t *
138 pvr_init (void)
140 struct pvr_t *pvr = NULL;
142 pvr = calloc (1, sizeof (struct pvr_t));
143 pvr->dev_fd = -1;
144 pvr->video_dev = strdup (PVR_DEFAULT_DEVICE);
146 /* v4l2 params */
147 pvr->mute = 0;
148 pvr->input = 0;
149 pvr->normid = -1;
150 pvr->brightness = 0;
151 pvr->contrast = 0;
152 pvr->hue = 0;
153 pvr->saturation = 0;
154 pvr->width = -1;
155 pvr->height = -1;
156 pvr->freq = -1;
157 pvr->chan_idx = -1;
158 pvr->chan_idx_last = -1;
160 /* set default encoding settings
161 * may be overlapped by user parameters
162 * Use VBR MPEG_PS encoding at 6 Mbps (peak at 9.6 Mbps)
163 * with 48 KHz L2 384 kbps audio.
165 pvr->aspect = V4L2_MPEG_VIDEO_ASPECT_4x3;
166 pvr->samplerate = V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000;
167 pvr->layer = V4L2_MPEG_AUDIO_ENCODING_LAYER_2;
168 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_384K;
169 pvr->audio_mode = V4L2_MPEG_AUDIO_MODE_STEREO;
170 pvr->bitrate = 6000000;
171 pvr->bitrate_mode = V4L2_MPEG_VIDEO_BITRATE_MODE_VBR;
172 pvr->bitrate_peak = 9600000;
173 pvr->stream_type = V4L2_MPEG_STREAM_TYPE_MPEG2_PS;
175 return pvr;
178 static void
179 pvr_uninit (struct pvr_t *pvr)
181 if (!pvr)
182 return;
184 /* close device */
185 if (pvr->dev_fd)
186 close (pvr->dev_fd);
188 if (pvr->video_dev)
189 free (pvr->video_dev);
191 if (pvr->stationlist.list)
192 free (pvr->stationlist.list);
194 if (pvr->param_channel)
195 free (pvr->param_channel);
197 free (pvr);
201 * @brief Copy Constructor for stationlist
203 * @see parse_setup_stationlist
205 static int
206 copycreate_stationlist (stationlist_t *stationlist, int num)
208 int i;
210 if (chantab < 0 || !stationlist)
211 return -1;
213 num = FFMAX (num, chanlists[chantab].count);
215 if (stationlist->list)
217 free (stationlist->list);
218 stationlist->list = NULL;
221 stationlist->total = 0;
222 stationlist->enabled = 0;
223 stationlist->used = 0;
224 stationlist->list = calloc (num, sizeof (station_elem_t));
226 if (!stationlist->list)
228 mp_msg (MSGT_OPEN, MSGL_ERR,
229 "%s No memory allocated for station list, giving up\n",
230 LOG_LEVEL_V4L2);
231 return -1;
234 /* transport the channel list data to our extented struct */
235 stationlist->total = num;
236 av_strlcpy (stationlist->name, chanlists[chantab].name, PVR_STATION_NAME_SIZE);
238 for (i = 0; i < chanlists[chantab].count; i++)
240 stationlist->list[i].station[0]= '\0'; /* no station name yet */
241 av_strlcpy (stationlist->list[i].name,
242 chanlists[chantab].list[i].name, PVR_STATION_NAME_SIZE);
243 stationlist->list[i].freq = chanlists[chantab].list[i].freq;
244 stationlist->list[i].enabled = 1; /* default enabled */
245 stationlist->enabled++;
246 stationlist->used++;
249 return 0;
252 static int
253 print_all_stations (struct pvr_t *pvr)
255 int i;
257 if (!pvr || !pvr->stationlist.list)
258 return -1;
260 for (i = 0; i < pvr->stationlist.total; i++)
262 mp_msg (MSGT_OPEN, MSGL_V,
263 "%s %3d: [%c] channel: %8s - freq: %8d - station: %s\n",
264 LOG_LEVEL_V4L2, i, (pvr->stationlist.list[i].enabled) ? 'X' : ' ',
265 pvr->stationlist.list[i].name, pvr->stationlist.list[i].freq,
266 pvr->stationlist.list[i].station);
269 return 0;
273 * Disables all stations
275 * @see parse_setup_stationlist
277 static void
278 disable_all_stations (struct pvr_t *pvr)
280 int i;
282 for (i = 0; i < pvr->stationlist.total; i++)
283 pvr->stationlist.list[i].enabled = 0;
284 pvr->stationlist.enabled = 0;
288 * Update or add a station
290 * @see parse_setup_stationlist
292 static int
293 set_station (struct pvr_t *pvr, const char *station,
294 const char *channel, int freq)
296 int i;
298 if (!pvr || !pvr->stationlist.list)
299 return -1;
301 if (0 >= pvr->stationlist.total || (!channel && !freq))
302 return -1;
304 /* select channel */
305 for (i = 0; i < pvr->stationlist.used; i++)
307 if (channel && !strcasecmp (pvr->stationlist.list[i].name, channel))
308 break; /* found existing channel entry */
310 if (freq > 0 && pvr->stationlist.list[i].freq == freq)
311 break; /* found existing frequency entry */
314 if (i < pvr->stationlist.used)
317 * found an existing entry,
318 * which is about to change with the user data.
319 * it is also enabled ..
321 if (!pvr->stationlist.list[i].enabled)
323 pvr->stationlist.list[i].enabled = 1;
324 pvr->stationlist.enabled++;
327 if (station)
328 av_strlcpy (pvr->stationlist.list[i].station,
329 station, PVR_STATION_NAME_SIZE);
330 else if (channel)
331 av_strlcpy (pvr->stationlist.list[i].station,
332 channel, PVR_STATION_NAME_SIZE);
333 else
334 snprintf (pvr->stationlist.list[i].station,
335 PVR_STATION_NAME_SIZE, "F %d", freq);
337 mp_msg (MSGT_OPEN, MSGL_DBG2,
338 "%s Set user station channel: %8s - freq: %8d - station: %s\n",
339 LOG_LEVEL_V4L2, pvr->stationlist.list[i].name,
340 pvr->stationlist.list[i].freq,
341 pvr->stationlist.list[i].station);
342 return 0;
345 /* from here on, we have to create a new entry, frequency is mandatory */
346 if (freq < 0)
348 mp_msg (MSGT_OPEN, MSGL_ERR,
349 "%s Cannot add new station/channel without frequency\n",
350 LOG_LEVEL_V4L2);
351 return -1;
354 if (pvr->stationlist.total < i)
357 * we have to extend the stationlist about
358 * an arbitrary size, even though this path is not performance critical
360 pvr->stationlist.total += 10;
361 pvr->stationlist.list =
362 realloc (pvr->stationlist.list,
363 pvr->stationlist.total * sizeof (station_elem_t));
365 if (!pvr->stationlist.list)
367 mp_msg (MSGT_OPEN, MSGL_ERR,
368 "%s No memory allocated for station list, giving up\n",
369 LOG_LEVEL_V4L2);
370 return -1;
373 /* clear the new space ..*/
374 memset (&(pvr->stationlist.list[pvr->stationlist.used]), 0,
375 (pvr->stationlist.total - pvr->stationlist.used)
376 * sizeof (station_elem_t));
379 /* here we go, our actual new entry */
380 pvr->stationlist.used++;
381 pvr->stationlist.list[i].enabled = 1;
382 pvr->stationlist.enabled++;
384 if (station)
385 av_strlcpy (pvr->stationlist.list[i].station,
386 station, PVR_STATION_NAME_SIZE);
387 if (channel)
388 av_strlcpy (pvr->stationlist.list[i].name, channel, PVR_STATION_NAME_SIZE);
389 else
390 snprintf (pvr->stationlist.list[i].name,
391 PVR_STATION_NAME_SIZE, "F %d", freq);
393 pvr->stationlist.list[i].freq = freq;
395 mp_msg (MSGT_OPEN, MSGL_DBG2,
396 "%s Add user station channel: %8s - freq: %8d - station: %s\n",
397 LOG_LEVEL_V4L2, pvr->stationlist.list[i].name,
398 pvr->stationlist.list[i].freq,
399 pvr->stationlist.list[i].station);
401 return 0;
405 * Here we set our stationlist, as follow
406 * - choose the frequency channel table, e.g. ntsc-cable
407 * - create our stationlist, same element size as the channellist
408 * - copy the channellist content to our stationlist
409 * - IF the user provides his channel-mapping, THEN:
410 * - disable all stations
411 * - update and/or create entries in the stationlist and enable them
413 static int
414 parse_setup_stationlist (struct pvr_t *pvr)
416 int i;
418 if (!pvr)
419 return -1;
421 /* Create our station/channel list */
422 if (stream_tv_defaults.chanlist)
424 /* select channel list */
425 for (i = 0; chanlists[i].name != NULL; i++)
427 if (!strcasecmp (chanlists[i].name, stream_tv_defaults.chanlist))
429 chantab = i;
430 break;
433 if (!chanlists[i].name)
435 mp_msg (MSGT_OPEN, MSGL_ERR,
436 "%s unable to find channel list %s, using default %s\n",
437 LOG_LEVEL_V4L2, stream_tv_defaults.chanlist, chanlists[chantab].name);
439 else
441 mp_msg (MSGT_OPEN, MSGL_INFO,
442 "%s select channel list %s, entries %d\n", LOG_LEVEL_V4L2,
443 chanlists[chantab].name, chanlists[chantab].count);
447 if (0 > chantab)
449 mp_msg (MSGT_OPEN, MSGL_FATAL,
450 "%s No channel list selected, giving up\n", LOG_LEVEL_V4L2);
451 return -1;
454 if (copycreate_stationlist (&(pvr->stationlist), -1) < 0)
456 mp_msg (MSGT_OPEN, MSGL_FATAL,
457 "%s No memory allocated for station list, giving up\n",
458 LOG_LEVEL_V4L2);
459 return -1;
462 /* Handle user channel mappings */
463 if (stream_tv_defaults.channels)
465 char channel[PVR_STATION_NAME_SIZE];
466 char station[PVR_STATION_NAME_SIZE];
467 char **channels = stream_tv_defaults.channels;
469 disable_all_stations (pvr);
471 while (*channels)
473 char *tmp = *(channels++);
474 char *sep = strchr (tmp, '-');
475 int freq=-1;
477 if (!sep)
478 continue; /* Wrong syntax, but mplayer should not crash */
480 av_strlcpy (station, sep + 1, PVR_STATION_NAME_SIZE);
482 sep[0] = '\0';
483 av_strlcpy (channel, tmp, PVR_STATION_NAME_SIZE);
485 while ((sep = strchr (station, '_')))
486 sep[0] = ' ';
488 /* if channel number is a number and larger than 1000 treat it as
489 * frequency tmp still contain pointer to null-terminated string with
490 * channel number here
492 if ((freq = atoi (channel)) <= 1000)
493 freq = -1;
495 if (set_station (pvr, station, (freq <= 0) ? channel : NULL, freq) < 0)
497 mp_msg (MSGT_OPEN, MSGL_ERR,
498 "%s Unable to set user station channel: %8s - freq: %8d - station: %s\n", LOG_LEVEL_V4L2,
499 channel, freq, station);
504 return print_all_stations (pvr);
507 static int
508 get_v4l2_freq (struct pvr_t *pvr)
510 int freq;
511 struct v4l2_frequency vf;
512 struct v4l2_tuner vt;
514 if (!pvr)
515 return -1;
517 if (pvr->dev_fd < 0)
518 return -1;
520 memset (&vt, 0, sizeof (vt));
521 memset (&vf, 0, sizeof (vf));
523 if (ioctl (pvr->dev_fd, VIDIOC_G_TUNER, &vt) < 0)
525 mp_msg (MSGT_OPEN, MSGL_ERR, "%s can't set tuner (%s).\n",
526 LOG_LEVEL_V4L2, strerror (errno));
527 return -1;
530 if (ioctl (pvr->dev_fd, VIDIOC_G_FREQUENCY, &vf) < 0)
532 mp_msg (MSGT_OPEN, MSGL_ERR, "%s can't get frequency %d.\n",
533 LOG_LEVEL_V4L2, errno);
534 return -1;
536 freq = vf.frequency;
537 if (!(vt.capability & V4L2_TUNER_CAP_LOW))
538 freq *= 1000;
539 freq /= 16;
541 return freq;
544 static int
545 set_v4l2_freq (struct pvr_t *pvr)
547 struct v4l2_frequency vf;
548 struct v4l2_tuner vt;
550 if (!pvr)
551 return -1;
553 if (0 >= pvr->freq)
555 mp_msg (MSGT_OPEN, MSGL_ERR,
556 "%s Frequency invalid %d !!!\n", LOG_LEVEL_V4L2, pvr->freq);
557 return -1;
560 /* don't set the frequency, if it's already set.
561 * setting it here would interrupt the stream.
563 if (get_v4l2_freq (pvr) == pvr->freq)
565 mp_msg (MSGT_OPEN, MSGL_STATUS,
566 "%s Frequency %d already set.\n", LOG_LEVEL_V4L2, pvr->freq);
567 return 0;
570 if (pvr->dev_fd < 0)
571 return -1;
573 memset (&vf, 0, sizeof (vf));
574 memset (&vt, 0, sizeof (vt));
576 if (ioctl (pvr->dev_fd, VIDIOC_G_TUNER, &vt) < 0)
578 mp_msg (MSGT_OPEN, MSGL_ERR, "%s can't get tuner (%s).\n",
579 LOG_LEVEL_V4L2, strerror (errno));
580 return -1;
583 vf.type = vt.type;
584 vf.frequency = pvr->freq * 16;
586 if (!(vt.capability & V4L2_TUNER_CAP_LOW))
587 vf.frequency /= 1000;
589 if (ioctl (pvr->dev_fd, VIDIOC_S_FREQUENCY, &vf) < 0)
591 mp_msg (MSGT_OPEN, MSGL_ERR, "%s can't set frequency (%s).\n",
592 LOG_LEVEL_V4L2, strerror (errno));
593 return -1;
596 memset (&vt, 0, sizeof(vt));
597 if (ioctl (pvr->dev_fd, VIDIOC_G_TUNER, &vt) < 0)
599 mp_msg (MSGT_OPEN, MSGL_ERR, "%s can't set tuner (%s).\n",
600 LOG_LEVEL_V4L2, strerror (errno));
601 return -1;
604 /* just a notification */
605 if (!vt.signal)
606 mp_msg (MSGT_OPEN, MSGL_ERR, "%s NO SIGNAL at frequency %d (%d)\n",
607 LOG_LEVEL_V4L2, pvr->freq, vf.frequency);
608 else
609 mp_msg (MSGT_OPEN, MSGL_STATUS, "%s Got signal at frequency %d (%d)\n",
610 LOG_LEVEL_V4L2, pvr->freq, vf.frequency);
612 return 0;
615 static int
616 set_station_by_step (struct pvr_t *pvr, int step, int v4lAction)
618 if (!pvr || !pvr->stationlist.list)
619 return -1;
621 if (pvr->stationlist.enabled >= abs (step))
623 int gotcha = 0;
624 int chidx = pvr->chan_idx + step;
626 while (!gotcha)
628 chidx = (chidx + pvr->stationlist.used) % pvr->stationlist.used;
630 mp_msg (MSGT_OPEN, MSGL_DBG2,
631 "%s Offset switch: current %d, enabled %d, step %d -> %d\n",
632 LOG_LEVEL_V4L2, pvr->chan_idx,
633 pvr->stationlist.enabled, step, chidx);
635 if (!pvr->stationlist.list[chidx].enabled)
637 mp_msg (MSGT_OPEN, MSGL_DBG2,
638 "%s Switch disabled to user station channel: %8s - freq: %8d - station: %s\n", LOG_LEVEL_V4L2,
639 pvr->stationlist.list[chidx].name,
640 pvr->stationlist.list[chidx].freq,
641 pvr->stationlist.list[chidx].station);
642 chidx += FFSIGN (step);
644 else
645 gotcha = 1;
648 pvr->freq = pvr->stationlist.list[chidx].freq;
649 pvr->chan_idx_last = pvr->chan_idx;
650 pvr->chan_idx = chidx;
652 mp_msg (MSGT_OPEN, MSGL_INFO,
653 "%s Switch to user station channel: %8s - freq: %8d - station: %s\n", LOG_LEVEL_V4L2,
654 pvr->stationlist.list[chidx].name,
655 pvr->stationlist.list[chidx].freq,
656 pvr->stationlist.list[chidx].station);
658 if (v4lAction)
659 return set_v4l2_freq (pvr);
661 return (pvr->freq > 0) ? 0 : -1;
664 mp_msg (MSGT_OPEN, MSGL_ERR,
665 "%s Ooops couldn't set freq by channel entry step %d to current %d, enabled %d\n", LOG_LEVEL_V4L2,
666 step, pvr->chan_idx, pvr->stationlist.enabled);
668 return -1;
671 static int
672 set_station_by_channelname_or_freq (struct pvr_t *pvr, const char *channel,
673 int freq, int v4lAction)
675 int i = 0;
677 if (!pvr || !pvr->stationlist.list)
678 return -1;
680 if (0 >= pvr->stationlist.enabled)
682 mp_msg (MSGT_OPEN, MSGL_WARN,
683 "%s No enabled station, cannot switch channel/frequency\n",
684 LOG_LEVEL_V4L2);
685 return -1;
688 if (channel)
690 /* select by channel */
691 for (i = 0; i < pvr->stationlist.used ; i++)
693 if (!strcasecmp (pvr->stationlist.list[i].name, channel))
695 if (!pvr->stationlist.list[i].enabled)
697 mp_msg (MSGT_OPEN, MSGL_WARN,
698 "%s Switch disabled to user station channel: %8s - freq: %8d - station: %s\n", LOG_LEVEL_V4L2,
699 pvr->stationlist.list[i].name,
700 pvr->stationlist.list[i].freq,
701 pvr->stationlist.list[i].station);
703 return -1;
706 pvr->freq = pvr->stationlist.list[i].freq;
707 pvr->chan_idx_last = pvr->chan_idx;
708 pvr->chan_idx = i;
709 break;
713 else if (freq >= 0)
715 /* select by freq */
716 for (i = 0; i < pvr->stationlist.used; i++)
718 if (pvr->stationlist.list[i].freq == freq)
720 if (!pvr->stationlist.list[i].enabled)
722 mp_msg (MSGT_OPEN, MSGL_WARN,
723 "%s Switch disabled to user station channel: %8s - freq: %8d - station: %s\n", LOG_LEVEL_V4L2,
724 pvr->stationlist.list[i].name,
725 pvr->stationlist.list[i].freq,
726 pvr->stationlist.list[i].station);
728 return -1;
731 pvr->freq = pvr->stationlist.list[i].freq;
732 pvr->chan_idx_last = pvr->chan_idx;
733 pvr->chan_idx = i;
734 break;
739 if (i >= pvr->stationlist.used)
741 if (channel)
742 mp_msg (MSGT_OPEN, MSGL_WARN,
743 "%s unable to find channel %s\n", LOG_LEVEL_V4L2, channel);
744 else
745 mp_msg (MSGT_OPEN, MSGL_WARN,
746 "%s unable to find frequency %d\n", LOG_LEVEL_V4L2, freq);
747 return -1;
750 mp_msg (MSGT_OPEN, MSGL_INFO,
751 "%s Switch to user station channel: %8s - freq: %8d - station: %s\n", LOG_LEVEL_V4L2,
752 pvr->stationlist.list[i].name,
753 pvr->stationlist.list[i].freq,
754 pvr->stationlist.list[i].station);
756 if (v4lAction)
757 return set_v4l2_freq (pvr);
759 return (pvr->freq > 0) ? 0 : -1;
762 static int
763 force_freq_step (struct pvr_t *pvr, int step)
765 int freq;
767 if (!pvr)
768 return -1;
770 freq = pvr->freq+step;
772 if (freq)
774 mp_msg (MSGT_OPEN, MSGL_INFO,
775 "%s Force Frequency %d + %d = %d \n", LOG_LEVEL_V4L2,
776 pvr->freq, step, freq);
778 pvr->freq = freq;
780 return set_v4l2_freq (pvr);
783 return -1;
786 static void
787 parse_encoder_options (struct pvr_t *pvr)
789 if (!pvr)
790 return;
792 /* -pvr aspect=digit */
793 if (pvr_param_aspect_ratio >= 0 && pvr_param_aspect_ratio <= 3)
794 pvr->aspect = pvr_param_aspect_ratio;
796 /* -pvr arate=x */
797 if (pvr_param_sample_rate != 0)
799 switch (pvr_param_sample_rate)
801 case 32000:
802 pvr->samplerate = V4L2_MPEG_AUDIO_SAMPLING_FREQ_32000;
803 break;
804 case 44100:
805 pvr->samplerate = V4L2_MPEG_AUDIO_SAMPLING_FREQ_44100;
806 break;
807 case 48000:
808 pvr->samplerate = V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000;
809 break;
810 default:
811 break;
815 /* -pvr alayer=x */
816 if (pvr_param_audio_layer == 1)
817 pvr->layer = V4L2_MPEG_AUDIO_ENCODING_LAYER_1;
818 else if (pvr_param_audio_layer == 2)
819 pvr->layer = V4L2_MPEG_AUDIO_ENCODING_LAYER_2;
820 else if (pvr_param_audio_layer == 3)
821 pvr->layer = V4L2_MPEG_AUDIO_ENCODING_LAYER_3;
823 /* -pvr abitrate=x */
824 if (pvr_param_audio_bitrate != 0)
826 if (pvr->layer == V4L2_MPEG_AUDIO_ENCODING_LAYER_1)
828 switch (pvr_param_audio_bitrate)
830 case 32:
831 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_32K;
832 break;
833 case 64:
834 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_64K;
835 break;
836 case 96:
837 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_96K;
838 break;
839 case 128:
840 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_128K;
841 break;
842 case 160:
843 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_160K;
844 break;
845 case 192:
846 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_192K;
847 break;
848 case 224:
849 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_224K;
850 break;
851 case 256:
852 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_256K;
853 break;
854 case 288:
855 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_288K;
856 break;
857 case 320:
858 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_320K;
859 break;
860 case 352:
861 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_352K;
862 break;
863 case 384:
864 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_384K;
865 break;
866 case 416:
867 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_416K;
868 break;
869 case 448:
870 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_448K;
871 break;
872 default:
873 break;
877 else if (pvr->layer == V4L2_MPEG_AUDIO_ENCODING_LAYER_2)
879 switch (pvr_param_audio_bitrate)
881 case 32:
882 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_32K;
883 break;
884 case 48:
885 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_48K;
886 break;
887 case 56:
888 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_56K;
889 break;
890 case 64:
891 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_64K;
892 break;
893 case 80:
894 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_80K;
895 break;
896 case 96:
897 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_96K;
898 break;
899 case 112:
900 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_112K;
901 break;
902 case 128:
903 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_128K;
904 break;
905 case 160:
906 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_160K;
907 break;
908 case 192:
909 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_192K;
910 break;
911 case 224:
912 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_224K;
913 break;
914 case 256:
915 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_256K;
916 break;
917 case 320:
918 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_320K;
919 break;
920 case 384:
921 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_384K;
922 break;
923 default:
924 break;
928 else if (pvr->layer == V4L2_MPEG_AUDIO_ENCODING_LAYER_3)
930 switch (pvr_param_audio_bitrate)
932 case 32:
933 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_32K;
934 break;
935 case 40:
936 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_40K;
937 break;
938 case 48:
939 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_48K;
940 break;
941 case 56:
942 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_56K;
943 break;
944 case 64:
945 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_64K;
946 break;
947 case 80:
948 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_80K;
949 break;
950 case 96:
951 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_96K;
952 break;
953 case 112:
954 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_112K;
955 break;
956 case 128:
957 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_128K;
958 break;
959 case 160:
960 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_160K;
961 break;
962 case 192:
963 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_192K;
964 break;
965 case 224:
966 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_224K;
967 break;
968 case 256:
969 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_256K;
970 break;
971 case 320:
972 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_320K;
973 break;
974 default:
975 break;
980 /* -pvr amode=x */
981 if (pvr_param_audio_mode)
983 if (!strcmp (pvr_param_audio_mode, PVR_AUDIO_MODE_ARG_STEREO))
984 pvr->audio_mode = V4L2_MPEG_AUDIO_MODE_STEREO;
985 else if (!strcmp (pvr_param_audio_mode, PVR_AUDIO_MODE_ARG_JOINT_STEREO))
986 pvr->audio_mode = V4L2_MPEG_AUDIO_MODE_JOINT_STEREO;
987 else if (!strcmp (pvr_param_audio_mode, PVR_AUDIO_MODE_ARG_DUAL))
988 pvr->audio_mode = V4L2_MPEG_AUDIO_MODE_DUAL;
989 else if (!strcmp (pvr_param_audio_mode, PVR_AUDIO_MODE_ARG_MONO))
990 pvr->audio_mode = V4L2_MPEG_AUDIO_MODE_MONO;
993 /* -pvr vbitrate=x */
994 if (pvr_param_bitrate)
995 pvr->bitrate = pvr_param_bitrate;
997 /* -pvr vmode=x */
998 if (pvr_param_bitrate_mode)
1000 if (!strcmp (pvr_param_bitrate_mode, PVR_VIDEO_BITRATE_MODE_ARG_VBR))
1001 pvr->bitrate_mode = V4L2_MPEG_VIDEO_BITRATE_MODE_VBR;
1002 else if (!strcmp (pvr_param_bitrate_mode, PVR_VIDEO_BITRATE_MODE_ARG_CBR))
1003 pvr->bitrate_mode = V4L2_MPEG_VIDEO_BITRATE_MODE_CBR;
1006 /* -pvr vpeak=x */
1007 if (pvr_param_bitrate_peak)
1008 pvr->bitrate_peak = pvr_param_bitrate_peak;
1010 /* -pvr fmt=x */
1011 if (pvr_param_stream_type)
1013 if (!strcmp (pvr_param_stream_type, PVR_VIDEO_STREAM_TYPE_PS))
1014 pvr->stream_type = V4L2_MPEG_STREAM_TYPE_MPEG2_PS;
1015 else if (!strcmp (pvr_param_stream_type, PVR_VIDEO_STREAM_TYPE_TS))
1016 pvr->stream_type = V4L2_MPEG_STREAM_TYPE_MPEG2_TS;
1017 else if (!strcmp (pvr_param_stream_type, PVR_VIDEO_STREAM_TYPE_MPEG1))
1018 pvr->stream_type = V4L2_MPEG_STREAM_TYPE_MPEG1_SS;
1019 else if (!strcmp (pvr_param_stream_type, PVR_VIDEO_STREAM_TYPE_DVD))
1020 pvr->stream_type = V4L2_MPEG_STREAM_TYPE_MPEG2_DVD;
1021 else if (!strcmp (pvr_param_stream_type, PVR_VIDEO_STREAM_TYPE_VCD))
1022 pvr->stream_type = V4L2_MPEG_STREAM_TYPE_MPEG1_VCD;
1023 else if (!strcmp (pvr_param_stream_type, PVR_VIDEO_STREAM_TYPE_SVCD))
1024 pvr->stream_type = V4L2_MPEG_STREAM_TYPE_MPEG2_SVCD;
1028 static void
1029 add_v4l2_ext_control (struct v4l2_ext_control *ctrl,
1030 uint32_t id, int32_t value)
1032 ctrl->id = id;
1033 ctrl->value = value;
1036 static int
1037 set_encoder_settings (struct pvr_t *pvr)
1039 struct v4l2_ext_control *ext_ctrl = NULL;
1040 struct v4l2_ext_controls ctrls;
1041 uint32_t count = 0;
1043 if (!pvr)
1044 return -1;
1046 if (pvr->dev_fd < 0)
1047 return -1;
1049 ext_ctrl = (struct v4l2_ext_control *)
1050 malloc (PVR_MAX_CONTROLS * sizeof (struct v4l2_ext_control));
1052 add_v4l2_ext_control (&ext_ctrl[count++], V4L2_CID_MPEG_VIDEO_ASPECT,
1053 pvr->aspect);
1055 add_v4l2_ext_control (&ext_ctrl[count++], V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
1056 pvr->samplerate);
1058 add_v4l2_ext_control (&ext_ctrl[count++], V4L2_CID_MPEG_AUDIO_ENCODING,
1059 pvr->layer);
1061 switch (pvr->layer)
1063 case V4L2_MPEG_AUDIO_ENCODING_LAYER_1:
1064 add_v4l2_ext_control (&ext_ctrl[count++], V4L2_CID_MPEG_AUDIO_L1_BITRATE,
1065 pvr->audio_rate);
1066 break;
1067 case V4L2_MPEG_AUDIO_ENCODING_LAYER_2:
1068 add_v4l2_ext_control (&ext_ctrl[count++], V4L2_CID_MPEG_AUDIO_L2_BITRATE,
1069 pvr->audio_rate);
1070 break;
1071 case V4L2_MPEG_AUDIO_ENCODING_LAYER_3:
1072 add_v4l2_ext_control (&ext_ctrl[count++], V4L2_CID_MPEG_AUDIO_L3_BITRATE,
1073 pvr->audio_rate);
1074 break;
1075 default:
1076 break;
1079 add_v4l2_ext_control (&ext_ctrl[count++], V4L2_CID_MPEG_AUDIO_MODE,
1080 pvr->audio_mode);
1082 add_v4l2_ext_control (&ext_ctrl[count++], V4L2_CID_MPEG_VIDEO_BITRATE,
1083 pvr->bitrate);
1085 add_v4l2_ext_control (&ext_ctrl[count++], V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
1086 pvr->bitrate_peak);
1088 add_v4l2_ext_control (&ext_ctrl[count++], V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
1089 pvr->bitrate_mode);
1091 add_v4l2_ext_control (&ext_ctrl[count++], V4L2_CID_MPEG_STREAM_TYPE,
1092 pvr->stream_type);
1094 /* set new encoding settings */
1095 ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
1096 ctrls.count = count;
1097 ctrls.controls = ext_ctrl;
1099 if (ioctl (pvr->dev_fd, VIDIOC_S_EXT_CTRLS, &ctrls) < 0)
1101 mp_msg (MSGT_OPEN, MSGL_ERR, "%s Error setting MPEG controls (%s).\n",
1102 LOG_LEVEL_ENCODER, strerror (errno));
1103 free (ext_ctrl);
1104 return -1;
1107 free (ext_ctrl);
1109 return 0;
1112 static void
1113 parse_v4l2_tv_options (struct pvr_t *pvr)
1115 if (!pvr)
1116 return;
1118 /* Create our station/channel list */
1119 parse_setup_stationlist (pvr);
1121 if (pvr->param_channel)
1123 if (set_station_by_channelname_or_freq (pvr, pvr->param_channel,
1124 -1, 0) >= 0)
1126 if (stream_tv_defaults.freq)
1128 mp_msg (MSGT_OPEN, MSGL_HINT,
1129 "%s tv param freq %s is overwritten by channel setting freq %d\n", LOG_LEVEL_V4L2,
1130 stream_tv_defaults.freq, pvr->freq);
1135 if (pvr->freq < 0 && stream_tv_defaults.freq)
1137 mp_msg (MSGT_OPEN, MSGL_HINT, "%s tv param freq %s is used directly\n",
1138 LOG_LEVEL_V4L2, stream_tv_defaults.freq);
1140 if (set_station_by_channelname_or_freq (pvr, NULL,
1141 atoi (stream_tv_defaults.freq), 0)<0)
1143 mp_msg (MSGT_OPEN, MSGL_WARN,
1144 "%s tv param freq %s invalid to set station\n",
1145 LOG_LEVEL_V4L2, stream_tv_defaults.freq);
1149 if (stream_tv_defaults.device)
1151 if (pvr->video_dev)
1152 free (pvr->video_dev);
1153 pvr->video_dev = strdup (stream_tv_defaults.device);
1156 if (stream_tv_defaults.noaudio)
1157 pvr->mute = stream_tv_defaults.noaudio;
1159 if (stream_tv_defaults.input)
1160 pvr->input = stream_tv_defaults.input;
1162 if (stream_tv_defaults.normid)
1163 pvr->normid = stream_tv_defaults.normid;
1165 if (stream_tv_defaults.brightness)
1166 pvr->brightness = stream_tv_defaults.brightness;
1168 if (stream_tv_defaults.contrast)
1169 pvr->contrast = stream_tv_defaults.contrast;
1171 if (stream_tv_defaults.hue)
1172 pvr->hue = stream_tv_defaults.hue;
1174 if (stream_tv_defaults.saturation)
1175 pvr->saturation = stream_tv_defaults.saturation;
1177 if (stream_tv_defaults.width)
1178 pvr->width = stream_tv_defaults.width;
1180 if (stream_tv_defaults.height)
1181 pvr->height = stream_tv_defaults.height;
1184 static int
1185 set_v4l2_settings (struct pvr_t *pvr)
1187 if (!pvr)
1188 return -1;
1190 if (pvr->dev_fd < 0)
1191 return -1;
1193 /* -tv noaudio */
1194 if (pvr->mute)
1196 struct v4l2_control ctrl;
1197 ctrl.id = V4L2_CID_AUDIO_MUTE;
1198 ctrl.value = 1;
1199 if (ioctl (pvr->dev_fd, VIDIOC_S_CTRL, &ctrl) < 0)
1201 mp_msg (MSGT_OPEN, MSGL_ERR,
1202 "%s can't mute (%s).\n", LOG_LEVEL_V4L2, strerror (errno));
1203 return -1;
1207 /* -tv input=x */
1208 if (pvr->input != 0)
1210 if (ioctl (pvr->dev_fd, VIDIOC_S_INPUT, &pvr->input) < 0)
1212 mp_msg (MSGT_OPEN, MSGL_ERR,
1213 "%s can't set input (%s)\n", LOG_LEVEL_V4L2, strerror (errno));
1214 return -1;
1218 /* -tv normid=x */
1219 if (pvr->normid != -1)
1221 struct v4l2_standard std;
1222 std.index = pvr->normid;
1224 if (ioctl (pvr->dev_fd, VIDIOC_ENUMSTD, &std) < 0)
1226 mp_msg (MSGT_OPEN, MSGL_ERR,
1227 "%s can't set norm (%s)\n", LOG_LEVEL_V4L2, strerror (errno));
1228 return -1;
1231 mp_msg (MSGT_OPEN, MSGL_V,
1232 "%s set norm to %s\n", LOG_LEVEL_V4L2, std.name);
1234 if (ioctl (pvr->dev_fd, VIDIOC_S_STD, &std.id) < 0)
1236 mp_msg (MSGT_OPEN, MSGL_ERR,
1237 "%s can't set norm (%s)\n", LOG_LEVEL_V4L2, strerror (errno));
1238 return -1;
1242 /* -tv brightness=x */
1243 if (pvr->brightness != 0)
1245 struct v4l2_control ctrl;
1246 ctrl.id = V4L2_CID_BRIGHTNESS;
1247 ctrl.value = pvr->brightness;
1249 if (ctrl.value < 0)
1250 ctrl.value = 0;
1251 if (ctrl.value > 255)
1252 ctrl.value = 255;
1254 if (ioctl (pvr->dev_fd, VIDIOC_S_CTRL, &ctrl) < 0)
1256 mp_msg (MSGT_OPEN, MSGL_ERR,
1257 "%s can't set brightness to %d (%s).\n",
1258 LOG_LEVEL_V4L2, ctrl.value, strerror (errno));
1259 return -1;
1263 /* -tv contrast=x */
1264 if (pvr->contrast != 0)
1266 struct v4l2_control ctrl;
1267 ctrl.id = V4L2_CID_CONTRAST;
1268 ctrl.value = pvr->contrast;
1270 if (ctrl.value < 0)
1271 ctrl.value = 0;
1272 if (ctrl.value > 127)
1273 ctrl.value = 127;
1275 if (ioctl (pvr->dev_fd, VIDIOC_S_CTRL, &ctrl) < 0)
1277 mp_msg (MSGT_OPEN, MSGL_ERR,
1278 "%s can't set contrast to %d (%s).\n",
1279 LOG_LEVEL_V4L2, ctrl.value, strerror (errno));
1280 return -1;
1284 /* -tv hue=x */
1285 if (pvr->hue != 0)
1287 struct v4l2_control ctrl;
1288 ctrl.id = V4L2_CID_HUE;
1289 ctrl.value = pvr->hue;
1291 if (ctrl.value < -128)
1292 ctrl.value = -128;
1293 if (ctrl.value > 127)
1294 ctrl.value = 127;
1296 if (ioctl (pvr->dev_fd, VIDIOC_S_CTRL, &ctrl) < 0)
1298 mp_msg (MSGT_OPEN, MSGL_ERR,
1299 "%s can't set hue to %d (%s).\n",
1300 LOG_LEVEL_V4L2, ctrl.value, strerror (errno));
1301 return -1;
1305 /* -tv saturation=x */
1306 if (pvr->saturation != 0)
1308 struct v4l2_control ctrl;
1309 ctrl.id = V4L2_CID_SATURATION;
1310 ctrl.value = pvr->saturation;
1312 if (ctrl.value < 0)
1313 ctrl.value = 0;
1314 if (ctrl.value > 127)
1315 ctrl.value = 127;
1317 if (ioctl (pvr->dev_fd, VIDIOC_S_CTRL, &ctrl) < 0)
1319 mp_msg (MSGT_OPEN, MSGL_ERR,
1320 "%s can't set saturation to %d (%s).\n",
1321 LOG_LEVEL_V4L2, ctrl.value, strerror (errno));
1322 return -1;
1326 /* -tv width=x:height=y */
1327 if (pvr->width && pvr->height)
1329 struct v4l2_format vfmt;
1330 vfmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1331 vfmt.fmt.pix.width = pvr->width;
1332 vfmt.fmt.pix.height = pvr->height;
1334 if (ioctl (pvr->dev_fd, VIDIOC_S_FMT, &vfmt) < 0)
1336 mp_msg (MSGT_OPEN, MSGL_ERR,
1337 "%s can't set resolution to %dx%d (%s).\n",
1338 LOG_LEVEL_V4L2, pvr->width, pvr->height, strerror (errno));
1339 return -1;
1343 if (pvr->freq < 0)
1345 int freq = get_v4l2_freq (pvr);
1346 mp_msg (MSGT_OPEN, MSGL_INFO,
1347 "%s Using current set frequency %d, to set channel\n",
1348 LOG_LEVEL_V4L2, freq);
1350 if (0 < freq)
1351 return set_station_by_channelname_or_freq (pvr, NULL, freq, 1);
1354 if (0 < pvr->freq)
1355 return set_v4l2_freq (pvr) ;
1357 return 0;
1360 static int
1361 v4l2_list_capabilities (struct pvr_t *pvr)
1363 struct v4l2_audio vaudio;
1364 struct v4l2_standard vs;
1365 struct v4l2_input vin;
1366 int err = 0;
1368 if (!pvr)
1369 return -1;
1371 if (pvr->dev_fd < 0)
1372 return -1;
1374 /* list available video inputs */
1375 vin.index = 0;
1376 err = 1;
1377 mp_msg (MSGT_OPEN, MSGL_INFO,
1378 "%s Available video inputs: ", LOG_LEVEL_V4L2);
1379 while (ioctl (pvr->dev_fd, VIDIOC_ENUMINPUT, &vin) >= 0)
1381 err = 0;
1382 mp_msg (MSGT_OPEN, MSGL_INFO, "'#%d, %s' ", vin.index, vin.name);
1383 vin.index++;
1385 if (err)
1387 mp_msg (MSGT_OPEN, MSGL_INFO, "none\n");
1388 return -1;
1390 else
1391 mp_msg (MSGT_OPEN, MSGL_INFO, "\n");
1393 /* list available audio inputs */
1394 vaudio.index = 0;
1395 err = 1;
1396 mp_msg (MSGT_OPEN, MSGL_INFO,
1397 "%s Available audio inputs: ", LOG_LEVEL_V4L2);
1398 while (ioctl (pvr->dev_fd, VIDIOC_ENUMAUDIO, &vaudio) >= 0)
1400 err = 0;
1401 mp_msg (MSGT_OPEN, MSGL_INFO, "'#%d, %s' ", vaudio.index, vaudio.name);
1402 vaudio.index++;
1404 if (err)
1406 mp_msg (MSGT_OPEN, MSGL_INFO, "none\n");
1407 return -1;
1409 else
1410 mp_msg (MSGT_OPEN, MSGL_INFO, "\n");
1412 /* list available norms */
1413 vs.index = 0;
1414 mp_msg (MSGT_OPEN, MSGL_INFO, "%s Available norms: ", LOG_LEVEL_V4L2);
1415 while (ioctl (pvr->dev_fd, VIDIOC_ENUMSTD, &vs) >= 0)
1417 err = 0;
1418 mp_msg (MSGT_OPEN, MSGL_INFO, "'#%d, %s' ", vs.index, vs.name);
1419 vs.index++;
1421 if (err)
1423 mp_msg (MSGT_OPEN, MSGL_INFO, "none\n");
1424 return -1;
1426 else
1427 mp_msg (MSGT_OPEN, MSGL_INFO, "\n");
1429 return 0;
1432 static int
1433 v4l2_display_settings (struct pvr_t *pvr)
1435 struct v4l2_audio vaudio;
1436 struct v4l2_standard vs;
1437 struct v4l2_input vin;
1438 v4l2_std_id std;
1439 int input;
1441 if (!pvr)
1442 return -1;
1444 if (pvr->dev_fd < 0)
1445 return -1;
1447 /* get current video input */
1448 if (ioctl (pvr->dev_fd, VIDIOC_G_INPUT, &input) == 0)
1450 vin.index = input;
1451 if (ioctl (pvr->dev_fd, VIDIOC_ENUMINPUT, &vin) < 0)
1453 mp_msg (MSGT_OPEN, MSGL_ERR,
1454 "%s can't get input (%s).\n", LOG_LEVEL_V4L2, strerror (errno));
1455 return -1;
1457 else
1458 mp_msg (MSGT_OPEN, MSGL_INFO,
1459 "%s Video input: %s\n", LOG_LEVEL_V4L2, vin.name);
1461 else
1463 mp_msg (MSGT_OPEN, MSGL_ERR,
1464 "%s can't get input (%s).\n", LOG_LEVEL_V4L2, strerror (errno));
1465 return -1;
1468 /* get current audio input */
1469 if (ioctl (pvr->dev_fd, VIDIOC_G_AUDIO, &vaudio) == 0)
1471 mp_msg (MSGT_OPEN, MSGL_INFO,
1472 "%s Audio input: %s\n", LOG_LEVEL_V4L2, vaudio.name);
1474 else
1476 mp_msg (MSGT_OPEN, MSGL_ERR,
1477 "%s can't get input (%s).\n", LOG_LEVEL_V4L2, strerror (errno));
1478 return -1;
1481 /* get current video format */
1482 if (ioctl (pvr->dev_fd, VIDIOC_G_STD, &std) == 0)
1484 vs.index = 0;
1486 while (ioctl (pvr->dev_fd, VIDIOC_ENUMSTD, &vs) >= 0)
1488 if (vs.id == std)
1490 mp_msg (MSGT_OPEN, MSGL_INFO,
1491 "%s Norm: %s.\n", LOG_LEVEL_V4L2, vs.name);
1492 break;
1494 vs.index++;
1497 else
1499 mp_msg (MSGT_OPEN, MSGL_ERR,
1500 "%s can't get norm (%s)\n", LOG_LEVEL_V4L2, strerror (errno));
1501 return -1;
1504 return 0;
1507 /* stream layer */
1509 static void
1510 pvr_stream_close (stream_t *stream)
1512 struct pvr_t *pvr;
1514 if (!stream)
1515 return;
1517 pvr = (struct pvr_t *) stream->priv;
1518 pvr_uninit (pvr);
1521 static int
1522 pvr_stream_read (stream_t *stream, char *buffer, int size)
1524 struct pollfd pfds[1];
1525 struct pvr_t *pvr;
1526 int rk, fd, pos;
1528 if (!stream || !buffer)
1529 return 0;
1531 pvr = (struct pvr_t *) stream->priv;
1532 fd = pvr->dev_fd;
1533 pos = 0;
1535 if (fd < 0)
1536 return 0;
1538 while (pos < size)
1540 pfds[0].fd = fd;
1541 pfds[0].events = POLLIN | POLLPRI;
1543 rk = size - pos;
1545 if (poll (pfds, 1, 500) <= 0)
1547 mp_msg (MSGT_OPEN, MSGL_ERR,
1548 "%s failed with errno %d when reading %d bytes\n",
1549 LOG_LEVEL_PVR, errno, size-pos);
1550 break;
1553 rk = read (fd, &buffer[pos], rk);
1554 if (rk > 0)
1556 pos += rk;
1557 mp_msg (MSGT_OPEN, MSGL_DBG3,
1558 "%s read (%d) bytes\n", LOG_LEVEL_PVR, pos);
1562 if (!pos)
1563 mp_msg (MSGT_OPEN, MSGL_ERR, "%s read %d bytes\n", LOG_LEVEL_PVR, pos);
1565 return pos;
1568 static int
1569 pvr_stream_open (stream_t *stream, int mode, void *opts, int *file_format)
1571 struct v4l2_capability vcap;
1572 struct v4l2_ext_controls ctrls;
1573 struct pvr_t *pvr = NULL;
1575 if (mode != STREAM_READ)
1576 return STREAM_UNSUPPORTED;
1578 pvr = pvr_init ();
1581 * if the url, i.e. 'pvr://8', contains the channel, use it,
1582 * else use the tv parameter.
1584 if (stream->url && strlen (stream->url) > 6 && stream->url[6] != '\0')
1585 pvr->param_channel = strdup (stream->url + 6);
1586 else if (stream_tv_defaults.channel && strlen (stream_tv_defaults.channel))
1587 pvr->param_channel = strdup (stream_tv_defaults.channel);
1589 parse_v4l2_tv_options (pvr);
1590 parse_encoder_options (pvr);
1592 /* open device */
1593 pvr->dev_fd = open (pvr->video_dev, O_RDWR);
1594 mp_msg (MSGT_OPEN, MSGL_INFO,
1595 "%s Using device %s\n", LOG_LEVEL_PVR, pvr->video_dev);
1596 if (pvr->dev_fd == -1)
1598 mp_msg (MSGT_OPEN, MSGL_ERR,
1599 "%s error opening device %s\n", LOG_LEVEL_PVR, pvr->video_dev);
1600 pvr_uninit (pvr);
1601 return STREAM_ERROR;
1604 /* query capabilities (i.e test V4L2 support) */
1605 if (ioctl (pvr->dev_fd, VIDIOC_QUERYCAP, &vcap) < 0)
1607 mp_msg (MSGT_OPEN, MSGL_ERR,
1608 "%s device is not V4L2 compliant (%s).\n",
1609 LOG_LEVEL_PVR, strerror (errno));
1610 pvr_uninit (pvr);
1611 return STREAM_ERROR;
1613 else
1614 mp_msg (MSGT_OPEN, MSGL_INFO,
1615 "%s Detected %s\n", LOG_LEVEL_PVR, vcap.card);
1617 /* check for a valid V4L2 capture device */
1618 if (!(vcap.capabilities & V4L2_CAP_VIDEO_CAPTURE))
1620 mp_msg (MSGT_OPEN, MSGL_ERR,
1621 "%s device is not a valid V4L2 capture device.\n",
1622 LOG_LEVEL_PVR);
1623 pvr_uninit (pvr);
1624 return STREAM_ERROR;
1627 /* check for device hardware MPEG encoding capability */
1628 ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
1629 ctrls.count = 0;
1630 ctrls.controls = NULL;
1632 if (ioctl (pvr->dev_fd, VIDIOC_G_EXT_CTRLS, &ctrls) < 0)
1634 mp_msg (MSGT_OPEN, MSGL_ERR,
1635 "%s device do not support MPEG input.\n", LOG_LEVEL_ENCODER);
1636 return STREAM_ERROR;
1639 /* list V4L2 capabilities */
1640 if (v4l2_list_capabilities (pvr) == -1)
1642 mp_msg (MSGT_OPEN, MSGL_ERR,
1643 "%s can't get v4l2 capabilities\n", LOG_LEVEL_PVR);
1644 pvr_uninit (pvr);
1645 return STREAM_ERROR;
1648 /* apply V4L2 settings */
1649 if (set_v4l2_settings (pvr) == -1)
1651 mp_msg (MSGT_OPEN, MSGL_ERR,
1652 "%s can't set v4l2 settings\n", LOG_LEVEL_PVR);
1653 pvr_uninit (pvr);
1654 return STREAM_ERROR;
1657 /* apply encoder settings */
1658 if (set_encoder_settings (pvr) == -1)
1660 mp_msg (MSGT_OPEN, MSGL_ERR,
1661 "%s can't set encoder settings\n", LOG_LEVEL_PVR);
1662 pvr_uninit (pvr);
1663 return STREAM_ERROR;
1666 /* display current V4L2 settings */
1667 if (v4l2_display_settings (pvr) == -1)
1669 mp_msg (MSGT_OPEN, MSGL_ERR,
1670 "%s can't get v4l2 settings\n", LOG_LEVEL_PVR);
1671 pvr_uninit (pvr);
1672 return STREAM_ERROR;
1675 stream->priv = pvr;
1676 stream->type = STREAMTYPE_PVR;
1677 stream->fill_buffer = pvr_stream_read;
1678 stream->close = pvr_stream_close;
1680 return STREAM_OK;
1683 /* PVR Public API access */
1685 const char *
1686 pvr_get_current_stationname (stream_t *stream)
1688 struct pvr_t *pvr;
1690 if (!stream || stream->type != STREAMTYPE_PVR)
1691 return NULL;
1693 pvr = (struct pvr_t *) stream->priv;
1695 if (pvr->stationlist.list &&
1696 pvr->stationlist.used > pvr->chan_idx &&
1697 pvr->chan_idx >= 0)
1698 return pvr->stationlist.list[pvr->chan_idx].station;
1700 return NULL;
1703 const char *
1704 pvr_get_current_channelname (stream_t *stream)
1706 struct pvr_t *pvr = (struct pvr_t *) stream->priv;
1708 if (pvr->stationlist.list &&
1709 pvr->stationlist.used > pvr->chan_idx &&
1710 pvr->chan_idx >= 0)
1711 return pvr->stationlist.list[pvr->chan_idx].name;
1713 return NULL;
1717 pvr_get_current_frequency (stream_t *stream)
1719 struct pvr_t *pvr = (struct pvr_t *) stream->priv;
1721 return pvr->freq;
1725 pvr_set_channel (stream_t *stream, const char * channel)
1727 struct pvr_t *pvr = (struct pvr_t *) stream->priv;
1729 return set_station_by_channelname_or_freq (pvr, channel, -1, 1);
1733 pvr_set_lastchannel (stream_t *stream)
1735 struct pvr_t *pvr = (struct pvr_t *) stream->priv;
1737 if (pvr->stationlist.list &&
1738 pvr->stationlist.used > pvr->chan_idx_last &&
1739 pvr->chan_idx_last >= 0)
1740 return set_station_by_channelname_or_freq (pvr, pvr->stationlist.list[pvr->chan_idx_last].name, -1, 1);
1742 return -1;
1746 pvr_set_freq (stream_t *stream, int freq)
1748 struct pvr_t *pvr = (struct pvr_t *) stream->priv;
1750 return set_station_by_channelname_or_freq (pvr, NULL, freq, 1);
1754 pvr_set_channel_step (stream_t *stream, int step)
1756 struct pvr_t *pvr = (struct pvr_t *) stream->priv;
1758 return set_station_by_step (pvr, step, 1);
1762 pvr_force_freq_step (stream_t *stream, int step)
1764 struct pvr_t *pvr = (struct pvr_t *) stream->priv;
1766 return force_freq_step (pvr, step);
1769 const stream_info_t stream_info_pvr = {
1770 "V4L2 MPEG Input (a.k.a PVR)",
1771 "pvr",
1772 "Benjamin Zores",
1774 pvr_stream_open,
1775 { "pvr", NULL },
1776 NULL,