Get rid of nonsensical limits on -geometry x, y,w and h values, they only
[mplayer/glamo.git] / stream / stream_pvr.c
blob8cc0eef6a03a6ddfa5fce6ff9def66c436bb65d9
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"
44 #include "help_mp.h"
46 #include "stream.h"
47 #include "pvr.h"
49 #include "frequencies.h"
50 #include "libavutil/common.h"
51 #include "libavutil/avstring.h"
53 #define PVR_DEFAULT_DEVICE "/dev/video0"
54 #define PVR_MAX_CONTROLS 10
56 /* logging mechanisms */
57 #define LOG_LEVEL_PVR "[pvr]"
58 #define LOG_LEVEL_V4L2 "[v4l2]"
59 #define LOG_LEVEL_ENCODER "[encoder]"
61 /* audio codec mode */
62 #define PVR_AUDIO_MODE_ARG_STEREO "stereo"
63 #define PVR_AUDIO_MODE_ARG_JOINT_STEREO "joint_stereo"
64 #define PVR_AUDIO_MODE_ARG_DUAL "dual"
65 #define PVR_AUDIO_MODE_ARG_MONO "mono"
67 /* video codec bitrate mode */
68 #define PVR_VIDEO_BITRATE_MODE_ARG_VBR "vbr"
69 #define PVR_VIDEO_BITRATE_MODE_ARG_CBR "cbr"
71 /* video codec stream type */
72 #define PVR_VIDEO_STREAM_TYPE_PS "ps"
73 #define PVR_VIDEO_STREAM_TYPE_TS "ts"
74 #define PVR_VIDEO_STREAM_TYPE_MPEG1 "mpeg1"
75 #define PVR_VIDEO_STREAM_TYPE_DVD "dvd"
76 #define PVR_VIDEO_STREAM_TYPE_VCD "vcd"
77 #define PVR_VIDEO_STREAM_TYPE_SVCD "svcd"
79 #define PVR_STATION_NAME_SIZE 256
81 /* command line arguments */
82 int pvr_param_aspect_ratio = 0;
83 int pvr_param_sample_rate = 0;
84 int pvr_param_audio_layer = 0;
85 int pvr_param_audio_bitrate = 0;
86 char *pvr_param_audio_mode = NULL;
87 int pvr_param_bitrate = 0;
88 char *pvr_param_bitrate_mode = NULL;
89 int pvr_param_bitrate_peak = 0;
90 char *pvr_param_stream_type = NULL;
92 typedef struct station_elem_s {
93 char name[8];
94 int freq;
95 char station[PVR_STATION_NAME_SIZE];
96 int enabled;
97 } station_elem_t;
99 typedef struct stationlist_s {
100 char name[PVR_STATION_NAME_SIZE];
101 station_elem_t *list;
102 int total; /* total number */
103 int used; /* used number */
104 int enabled; /* enabled number */
105 } stationlist_t;
107 struct pvr_t {
108 int dev_fd;
109 char *video_dev;
111 /* v4l2 params */
112 int mute;
113 int input;
114 int normid;
115 int brightness;
116 int contrast;
117 int hue;
118 int saturation;
119 int width;
120 int height;
121 int freq;
122 int chan_idx;
123 int chan_idx_last;
124 stationlist_t stationlist;
125 /* dups the tv_param_channel, or the url's channel param */
126 char *param_channel;
128 /* encoder params */
129 int aspect;
130 int samplerate;
131 int layer;
132 int audio_rate;
133 int audio_mode;
134 int bitrate;
135 int bitrate_mode;
136 int bitrate_peak;
137 int stream_type;
140 static struct pvr_t *
141 pvr_init (void)
143 struct pvr_t *pvr = NULL;
145 pvr = calloc (1, sizeof (struct pvr_t));
146 pvr->dev_fd = -1;
147 pvr->video_dev = strdup (PVR_DEFAULT_DEVICE);
149 /* v4l2 params */
150 pvr->mute = 0;
151 pvr->input = 0;
152 pvr->normid = -1;
153 pvr->brightness = 0;
154 pvr->contrast = 0;
155 pvr->hue = 0;
156 pvr->saturation = 0;
157 pvr->width = -1;
158 pvr->height = -1;
159 pvr->freq = -1;
160 pvr->chan_idx = -1;
161 pvr->chan_idx_last = -1;
163 /* set default encoding settings
164 * may be overlapped by user parameters
165 * Use VBR MPEG_PS encoding at 6 Mbps (peak at 9.6 Mbps)
166 * with 48 KHz L2 384 kbps audio.
168 pvr->aspect = V4L2_MPEG_VIDEO_ASPECT_4x3;
169 pvr->samplerate = V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000;
170 pvr->layer = V4L2_MPEG_AUDIO_ENCODING_LAYER_2;
171 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_384K;
172 pvr->audio_mode = V4L2_MPEG_AUDIO_MODE_STEREO;
173 pvr->bitrate = 6000000;
174 pvr->bitrate_mode = V4L2_MPEG_VIDEO_BITRATE_MODE_VBR;
175 pvr->bitrate_peak = 9600000;
176 pvr->stream_type = V4L2_MPEG_STREAM_TYPE_MPEG2_PS;
178 return pvr;
181 static void
182 pvr_uninit (struct pvr_t *pvr)
184 if (!pvr)
185 return;
187 /* close device */
188 if (pvr->dev_fd)
189 close (pvr->dev_fd);
191 if (pvr->video_dev)
192 free (pvr->video_dev);
194 if (pvr->stationlist.list)
195 free (pvr->stationlist.list);
197 if (pvr->param_channel)
198 free (pvr->param_channel);
200 free (pvr);
204 * @brief Copy Constructor for stationlist
206 * @see parse_setup_stationlist
208 static int
209 copycreate_stationlist (stationlist_t *stationlist, int num)
211 int i;
213 if (chantab < 0 || !stationlist)
214 return -1;
216 num = FFMAX (num, chanlists[chantab].count);
218 if (stationlist->list)
220 free (stationlist->list);
221 stationlist->list = NULL;
224 stationlist->total = 0;
225 stationlist->enabled = 0;
226 stationlist->used = 0;
227 stationlist->list = calloc (num, sizeof (station_elem_t));
229 if (!stationlist->list)
231 mp_msg (MSGT_OPEN, MSGL_ERR,
232 "%s No memory allocated for station list, giving up\n",
233 LOG_LEVEL_V4L2);
234 return -1;
237 /* transport the channel list data to our extented struct */
238 stationlist->total = num;
239 av_strlcpy (stationlist->name, chanlists[chantab].name, PVR_STATION_NAME_SIZE);
241 for (i = 0; i < chanlists[chantab].count; i++)
243 stationlist->list[i].station[0]= '\0'; /* no station name yet */
244 av_strlcpy (stationlist->list[i].name,
245 chanlists[chantab].list[i].name, PVR_STATION_NAME_SIZE);
246 stationlist->list[i].freq = chanlists[chantab].list[i].freq;
247 stationlist->list[i].enabled = 1; /* default enabled */
248 stationlist->enabled++;
249 stationlist->used++;
252 return 0;
255 static int
256 print_all_stations (struct pvr_t *pvr)
258 int i;
260 if (!pvr || !pvr->stationlist.list)
261 return -1;
263 for (i = 0; i < pvr->stationlist.total; i++)
265 mp_msg (MSGT_OPEN, MSGL_V,
266 "%s %3d: [%c] channel: %8s - freq: %8d - station: %s\n",
267 LOG_LEVEL_V4L2, i, (pvr->stationlist.list[i].enabled) ? 'X' : ' ',
268 pvr->stationlist.list[i].name, pvr->stationlist.list[i].freq,
269 pvr->stationlist.list[i].station);
272 return 0;
276 * Disables all stations
278 * @see parse_setup_stationlist
280 static void
281 disable_all_stations (struct pvr_t *pvr)
283 int i;
285 for (i = 0; i < pvr->stationlist.total; i++)
286 pvr->stationlist.list[i].enabled = 0;
287 pvr->stationlist.enabled = 0;
291 * Update or add a station
293 * @see parse_setup_stationlist
295 static int
296 set_station (struct pvr_t *pvr, const char *station,
297 const char *channel, int freq)
299 int i;
301 if (!pvr || !pvr->stationlist.list)
302 return -1;
304 if (0 >= pvr->stationlist.total || (!channel && !freq))
305 return -1;
307 /* select channel */
308 for (i = 0; i < pvr->stationlist.used; i++)
310 if (channel && !strcasecmp (pvr->stationlist.list[i].name, channel))
311 break; /* found existing channel entry */
313 if (freq > 0 && pvr->stationlist.list[i].freq == freq)
314 break; /* found existing frequency entry */
317 if (i < pvr->stationlist.used)
320 * found an existing entry,
321 * which is about to change with the user data.
322 * it is also enabled ..
324 if (!pvr->stationlist.list[i].enabled)
326 pvr->stationlist.list[i].enabled = 1;
327 pvr->stationlist.enabled++;
330 if (station)
331 av_strlcpy (pvr->stationlist.list[i].station,
332 station, PVR_STATION_NAME_SIZE);
333 else if (channel)
334 av_strlcpy (pvr->stationlist.list[i].station,
335 channel, PVR_STATION_NAME_SIZE);
336 else
337 snprintf (pvr->stationlist.list[i].station,
338 PVR_STATION_NAME_SIZE, "F %d", freq);
340 mp_msg (MSGT_OPEN, MSGL_DBG2,
341 "%s Set user station channel: %8s - freq: %8d - station: %s\n",
342 LOG_LEVEL_V4L2, pvr->stationlist.list[i].name,
343 pvr->stationlist.list[i].freq,
344 pvr->stationlist.list[i].station);
345 return 0;
348 /* from here on, we have to create a new entry, frequency is mandatory */
349 if (freq < 0)
351 mp_msg (MSGT_OPEN, MSGL_ERR,
352 "%s Cannot add new station/channel without frequency\n",
353 LOG_LEVEL_V4L2);
354 return -1;
357 if (pvr->stationlist.total < i)
360 * we have to extend the stationlist about
361 * an arbitrary size, even though this path is not performance critical
363 pvr->stationlist.total += 10;
364 pvr->stationlist.list =
365 realloc (pvr->stationlist.list,
366 pvr->stationlist.total * sizeof (station_elem_t));
368 if (!pvr->stationlist.list)
370 mp_msg (MSGT_OPEN, MSGL_ERR,
371 "%s No memory allocated for station list, giving up\n",
372 LOG_LEVEL_V4L2);
373 return -1;
376 /* clear the new space ..*/
377 memset (&(pvr->stationlist.list[pvr->stationlist.used]), 0,
378 (pvr->stationlist.total - pvr->stationlist.used)
379 * sizeof (station_elem_t));
382 /* here we go, our actual new entry */
383 pvr->stationlist.used++;
384 pvr->stationlist.list[i].enabled = 1;
385 pvr->stationlist.enabled++;
387 if (station)
388 av_strlcpy (pvr->stationlist.list[i].station,
389 station, PVR_STATION_NAME_SIZE);
390 if (channel)
391 av_strlcpy (pvr->stationlist.list[i].name, channel, PVR_STATION_NAME_SIZE);
392 else
393 snprintf (pvr->stationlist.list[i].name,
394 PVR_STATION_NAME_SIZE, "F %d", freq);
396 pvr->stationlist.list[i].freq = freq;
398 mp_msg (MSGT_OPEN, MSGL_DBG2,
399 "%s Add user station channel: %8s - freq: %8d - station: %s\n",
400 LOG_LEVEL_V4L2, pvr->stationlist.list[i].name,
401 pvr->stationlist.list[i].freq,
402 pvr->stationlist.list[i].station);
404 return 0;
408 * Here we set our stationlist, as follow
409 * - choose the frequency channel table, e.g. ntsc-cable
410 * - create our stationlist, same element size as the channellist
411 * - copy the channellist content to our stationlist
412 * - IF the user provides his channel-mapping, THEN:
413 * - disable all stations
414 * - update and/or create entries in the stationlist and enable them
416 static int
417 parse_setup_stationlist (struct pvr_t *pvr)
419 int i;
421 if (!pvr)
422 return -1;
424 /* Create our station/channel list */
425 if (stream_tv_defaults.chanlist)
427 /* select channel list */
428 for (i = 0; chanlists[i].name != NULL; i++)
430 if (!strcasecmp (chanlists[i].name, stream_tv_defaults.chanlist))
432 chantab = i;
433 break;
436 if (!chanlists[i].name)
438 mp_msg (MSGT_OPEN, MSGL_ERR,
439 "%s unable to find channel list %s, using default %s\n",
440 LOG_LEVEL_V4L2, stream_tv_defaults.chanlist, chanlists[chantab].name);
442 else
444 mp_msg (MSGT_OPEN, MSGL_INFO,
445 "%s select channel list %s, entries %d\n", LOG_LEVEL_V4L2,
446 chanlists[chantab].name, chanlists[chantab].count);
450 if (0 > chantab)
452 mp_msg (MSGT_OPEN, MSGL_FATAL,
453 "%s No channel list selected, giving up\n", LOG_LEVEL_V4L2);
454 return -1;
457 if (copycreate_stationlist (&(pvr->stationlist), -1) < 0)
459 mp_msg (MSGT_OPEN, MSGL_FATAL,
460 "%s No memory allocated for station list, giving up\n",
461 LOG_LEVEL_V4L2);
462 return -1;
465 /* Handle user channel mappings */
466 if (stream_tv_defaults.channels)
468 char channel[PVR_STATION_NAME_SIZE];
469 char station[PVR_STATION_NAME_SIZE];
470 char **channels = stream_tv_defaults.channels;
472 disable_all_stations (pvr);
474 while (*channels)
476 char *tmp = *(channels++);
477 char *sep = strchr (tmp, '-');
478 int freq=-1;
480 if (!sep)
481 continue; /* Wrong syntax, but mplayer should not crash */
483 av_strlcpy (station, sep + 1, PVR_STATION_NAME_SIZE);
485 sep[0] = '\0';
486 av_strlcpy (channel, tmp, PVR_STATION_NAME_SIZE);
488 while ((sep = strchr (station, '_')))
489 sep[0] = ' ';
491 /* if channel number is a number and larger than 1000 treat it as
492 * frequency tmp still contain pointer to null-terminated string with
493 * channel number here
495 if ((freq = atoi (channel)) <= 1000)
496 freq = -1;
498 if (set_station (pvr, station, (freq <= 0) ? channel : NULL, freq) < 0)
500 mp_msg (MSGT_OPEN, MSGL_ERR,
501 "%s Unable to set user station channel: %8s - freq: %8d - station: %s\n", LOG_LEVEL_V4L2,
502 channel, freq, station);
507 return print_all_stations (pvr);
510 static int
511 get_v4l2_freq (struct pvr_t *pvr)
513 int freq;
514 struct v4l2_frequency vf;
515 struct v4l2_tuner vt;
517 if (!pvr)
518 return -1;
520 if (pvr->dev_fd < 0)
521 return -1;
523 memset (&vt, 0, sizeof (vt));
524 memset (&vf, 0, sizeof (vf));
526 if (ioctl (pvr->dev_fd, VIDIOC_G_TUNER, &vt) < 0)
528 mp_msg (MSGT_OPEN, MSGL_ERR, "%s can't set tuner (%s).\n",
529 LOG_LEVEL_V4L2, strerror (errno));
530 return -1;
533 if (ioctl (pvr->dev_fd, VIDIOC_G_FREQUENCY, &vf) < 0)
535 mp_msg (MSGT_OPEN, MSGL_ERR, "%s can't get frequency %d.\n",
536 LOG_LEVEL_V4L2, errno);
537 return -1;
539 freq = vf.frequency;
540 if (!(vt.capability & V4L2_TUNER_CAP_LOW))
541 freq *= 1000;
542 freq /= 16;
544 return freq;
547 static int
548 set_v4l2_freq (struct pvr_t *pvr)
550 struct v4l2_frequency vf;
551 struct v4l2_tuner vt;
553 if (!pvr)
554 return -1;
556 if (0 >= pvr->freq)
558 mp_msg (MSGT_OPEN, MSGL_ERR,
559 "%s Frequency invalid %d !!!\n", LOG_LEVEL_V4L2, pvr->freq);
560 return -1;
563 /* don't set the frequency, if it's already set.
564 * setting it here would interrupt the stream.
566 if (get_v4l2_freq (pvr) == pvr->freq)
568 mp_msg (MSGT_OPEN, MSGL_STATUS,
569 "%s Frequency %d already set.\n", LOG_LEVEL_V4L2, pvr->freq);
570 return 0;
573 if (pvr->dev_fd < 0)
574 return -1;
576 memset (&vf, 0, sizeof (vf));
577 memset (&vt, 0, sizeof (vt));
579 if (ioctl (pvr->dev_fd, VIDIOC_G_TUNER, &vt) < 0)
581 mp_msg (MSGT_OPEN, MSGL_ERR, "%s can't get tuner (%s).\n",
582 LOG_LEVEL_V4L2, strerror (errno));
583 return -1;
586 vf.type = vt.type;
587 vf.frequency = pvr->freq * 16;
589 if (!(vt.capability & V4L2_TUNER_CAP_LOW))
590 vf.frequency /= 1000;
592 if (ioctl (pvr->dev_fd, VIDIOC_S_FREQUENCY, &vf) < 0)
594 mp_msg (MSGT_OPEN, MSGL_ERR, "%s can't set frequency (%s).\n",
595 LOG_LEVEL_V4L2, strerror (errno));
596 return -1;
599 memset (&vt, 0, sizeof(vt));
600 if (ioctl (pvr->dev_fd, VIDIOC_G_TUNER, &vt) < 0)
602 mp_msg (MSGT_OPEN, MSGL_ERR, "%s can't set tuner (%s).\n",
603 LOG_LEVEL_V4L2, strerror (errno));
604 return -1;
607 /* just a notification */
608 if (!vt.signal)
609 mp_msg (MSGT_OPEN, MSGL_ERR, "%s NO SIGNAL at frequency %d (%d)\n",
610 LOG_LEVEL_V4L2, pvr->freq, vf.frequency);
611 else
612 mp_msg (MSGT_OPEN, MSGL_STATUS, "%s Got signal at frequency %d (%d)\n",
613 LOG_LEVEL_V4L2, pvr->freq, vf.frequency);
615 return 0;
618 static int
619 set_station_by_step (struct pvr_t *pvr, int step, int v4lAction)
621 if (!pvr || !pvr->stationlist.list)
622 return -1;
624 if (pvr->stationlist.enabled >= abs (step))
626 int gotcha = 0;
627 int chidx = pvr->chan_idx + step;
629 while (!gotcha)
631 chidx = (chidx + pvr->stationlist.used) % pvr->stationlist.used;
633 mp_msg (MSGT_OPEN, MSGL_DBG2,
634 "%s Offset switch: current %d, enabled %d, step %d -> %d\n",
635 LOG_LEVEL_V4L2, pvr->chan_idx,
636 pvr->stationlist.enabled, step, chidx);
638 if (!pvr->stationlist.list[chidx].enabled)
640 mp_msg (MSGT_OPEN, MSGL_DBG2,
641 "%s Switch disabled to user station channel: %8s - freq: %8d - station: %s\n", LOG_LEVEL_V4L2,
642 pvr->stationlist.list[chidx].name,
643 pvr->stationlist.list[chidx].freq,
644 pvr->stationlist.list[chidx].station);
645 chidx += FFSIGN (step);
647 else
648 gotcha = 1;
651 pvr->freq = pvr->stationlist.list[chidx].freq;
652 pvr->chan_idx_last = pvr->chan_idx;
653 pvr->chan_idx = chidx;
655 mp_msg (MSGT_OPEN, MSGL_INFO,
656 "%s Switch to user station channel: %8s - freq: %8d - station: %s\n", LOG_LEVEL_V4L2,
657 pvr->stationlist.list[chidx].name,
658 pvr->stationlist.list[chidx].freq,
659 pvr->stationlist.list[chidx].station);
661 if (v4lAction)
662 return set_v4l2_freq (pvr);
664 return (pvr->freq > 0) ? 0 : -1;
667 mp_msg (MSGT_OPEN, MSGL_ERR,
668 "%s Ooops couldn't set freq by channel entry step %d to current %d, enabled %d\n", LOG_LEVEL_V4L2,
669 step, pvr->chan_idx, pvr->stationlist.enabled);
671 return -1;
674 static int
675 set_station_by_channelname_or_freq (struct pvr_t *pvr, const char *channel,
676 int freq, int v4lAction)
678 int i = 0;
680 if (!pvr || !pvr->stationlist.list)
681 return -1;
683 if (0 >= pvr->stationlist.enabled)
685 mp_msg (MSGT_OPEN, MSGL_WARN,
686 "%s No enabled station, cannot switch channel/frequency\n",
687 LOG_LEVEL_V4L2);
688 return -1;
691 if (channel)
693 /* select by channel */
694 for (i = 0; i < pvr->stationlist.used ; i++)
696 if (!strcasecmp (pvr->stationlist.list[i].name, channel))
698 if (!pvr->stationlist.list[i].enabled)
700 mp_msg (MSGT_OPEN, MSGL_WARN,
701 "%s Switch disabled to user station channel: %8s - freq: %8d - station: %s\n", LOG_LEVEL_V4L2,
702 pvr->stationlist.list[i].name,
703 pvr->stationlist.list[i].freq,
704 pvr->stationlist.list[i].station);
706 return -1;
709 pvr->freq = pvr->stationlist.list[i].freq;
710 pvr->chan_idx_last = pvr->chan_idx;
711 pvr->chan_idx = i;
712 break;
716 else if (freq >= 0)
718 /* select by freq */
719 for (i = 0; i < pvr->stationlist.used; i++)
721 if (pvr->stationlist.list[i].freq == freq)
723 if (!pvr->stationlist.list[i].enabled)
725 mp_msg (MSGT_OPEN, MSGL_WARN,
726 "%s Switch disabled to user station channel: %8s - freq: %8d - station: %s\n", LOG_LEVEL_V4L2,
727 pvr->stationlist.list[i].name,
728 pvr->stationlist.list[i].freq,
729 pvr->stationlist.list[i].station);
731 return -1;
734 pvr->freq = pvr->stationlist.list[i].freq;
735 pvr->chan_idx_last = pvr->chan_idx;
736 pvr->chan_idx = i;
737 break;
742 if (i >= pvr->stationlist.used)
744 if (channel)
745 mp_msg (MSGT_OPEN, MSGL_WARN,
746 "%s unable to find channel %s\n", LOG_LEVEL_V4L2, channel);
747 else
748 mp_msg (MSGT_OPEN, MSGL_WARN,
749 "%s unable to find frequency %d\n", LOG_LEVEL_V4L2, freq);
750 return -1;
753 mp_msg (MSGT_OPEN, MSGL_INFO,
754 "%s Switch to user station channel: %8s - freq: %8d - station: %s\n", LOG_LEVEL_V4L2,
755 pvr->stationlist.list[i].name,
756 pvr->stationlist.list[i].freq,
757 pvr->stationlist.list[i].station);
759 if (v4lAction)
760 return set_v4l2_freq (pvr);
762 return (pvr->freq > 0) ? 0 : -1;
765 static int
766 force_freq_step (struct pvr_t *pvr, int step)
768 int freq;
770 if (!pvr)
771 return -1;
773 freq = pvr->freq+step;
775 if (freq)
777 mp_msg (MSGT_OPEN, MSGL_INFO,
778 "%s Force Frequency %d + %d = %d \n", LOG_LEVEL_V4L2,
779 pvr->freq, step, freq);
781 pvr->freq = freq;
783 return set_v4l2_freq (pvr);
786 return -1;
789 static void
790 parse_encoder_options (struct pvr_t *pvr)
792 if (!pvr)
793 return;
795 /* -pvr aspect=digit */
796 if (pvr_param_aspect_ratio >= 0 && pvr_param_aspect_ratio <= 3)
797 pvr->aspect = pvr_param_aspect_ratio;
799 /* -pvr arate=x */
800 if (pvr_param_sample_rate != 0)
802 switch (pvr_param_sample_rate)
804 case 32000:
805 pvr->samplerate = V4L2_MPEG_AUDIO_SAMPLING_FREQ_32000;
806 break;
807 case 44100:
808 pvr->samplerate = V4L2_MPEG_AUDIO_SAMPLING_FREQ_44100;
809 break;
810 case 48000:
811 pvr->samplerate = V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000;
812 break;
813 default:
814 break;
818 /* -pvr alayer=x */
819 if (pvr_param_audio_layer == 1)
820 pvr->layer = V4L2_MPEG_AUDIO_ENCODING_LAYER_1;
821 else if (pvr_param_audio_layer == 2)
822 pvr->layer = V4L2_MPEG_AUDIO_ENCODING_LAYER_2;
823 else if (pvr_param_audio_layer == 3)
824 pvr->layer = V4L2_MPEG_AUDIO_ENCODING_LAYER_3;
826 /* -pvr abitrate=x */
827 if (pvr_param_audio_bitrate != 0)
829 if (pvr->layer == V4L2_MPEG_AUDIO_ENCODING_LAYER_1)
831 switch (pvr_param_audio_bitrate)
833 case 32:
834 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_32K;
835 break;
836 case 64:
837 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_64K;
838 break;
839 case 96:
840 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_96K;
841 break;
842 case 128:
843 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_128K;
844 break;
845 case 160:
846 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_160K;
847 break;
848 case 192:
849 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_192K;
850 break;
851 case 224:
852 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_224K;
853 break;
854 case 256:
855 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_256K;
856 break;
857 case 288:
858 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_288K;
859 break;
860 case 320:
861 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_320K;
862 break;
863 case 352:
864 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_352K;
865 break;
866 case 384:
867 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_384K;
868 break;
869 case 416:
870 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_416K;
871 break;
872 case 448:
873 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_448K;
874 break;
875 default:
876 break;
880 else if (pvr->layer == V4L2_MPEG_AUDIO_ENCODING_LAYER_2)
882 switch (pvr_param_audio_bitrate)
884 case 32:
885 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_32K;
886 break;
887 case 48:
888 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_48K;
889 break;
890 case 56:
891 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_56K;
892 break;
893 case 64:
894 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_64K;
895 break;
896 case 80:
897 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_80K;
898 break;
899 case 96:
900 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_96K;
901 break;
902 case 112:
903 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_112K;
904 break;
905 case 128:
906 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_128K;
907 break;
908 case 160:
909 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_160K;
910 break;
911 case 192:
912 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_192K;
913 break;
914 case 224:
915 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_224K;
916 break;
917 case 256:
918 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_256K;
919 break;
920 case 320:
921 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_320K;
922 break;
923 case 384:
924 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_384K;
925 break;
926 default:
927 break;
931 else if (pvr->layer == V4L2_MPEG_AUDIO_ENCODING_LAYER_3)
933 switch (pvr_param_audio_bitrate)
935 case 32:
936 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_32K;
937 break;
938 case 40:
939 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_40K;
940 break;
941 case 48:
942 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_48K;
943 break;
944 case 56:
945 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_56K;
946 break;
947 case 64:
948 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_64K;
949 break;
950 case 80:
951 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_80K;
952 break;
953 case 96:
954 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_96K;
955 break;
956 case 112:
957 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_112K;
958 break;
959 case 128:
960 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_128K;
961 break;
962 case 160:
963 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_160K;
964 break;
965 case 192:
966 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_192K;
967 break;
968 case 224:
969 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_224K;
970 break;
971 case 256:
972 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_256K;
973 break;
974 case 320:
975 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_320K;
976 break;
977 default:
978 break;
983 /* -pvr amode=x */
984 if (pvr_param_audio_mode)
986 if (!strcmp (pvr_param_audio_mode, PVR_AUDIO_MODE_ARG_STEREO))
987 pvr->audio_mode = V4L2_MPEG_AUDIO_MODE_STEREO;
988 else if (!strcmp (pvr_param_audio_mode, PVR_AUDIO_MODE_ARG_JOINT_STEREO))
989 pvr->audio_mode = V4L2_MPEG_AUDIO_MODE_JOINT_STEREO;
990 else if (!strcmp (pvr_param_audio_mode, PVR_AUDIO_MODE_ARG_DUAL))
991 pvr->audio_mode = V4L2_MPEG_AUDIO_MODE_DUAL;
992 else if (!strcmp (pvr_param_audio_mode, PVR_AUDIO_MODE_ARG_MONO))
993 pvr->audio_mode = V4L2_MPEG_AUDIO_MODE_MONO;
996 /* -pvr vbitrate=x */
997 if (pvr_param_bitrate)
998 pvr->bitrate = pvr_param_bitrate;
1000 /* -pvr vmode=x */
1001 if (pvr_param_bitrate_mode)
1003 if (!strcmp (pvr_param_bitrate_mode, PVR_VIDEO_BITRATE_MODE_ARG_VBR))
1004 pvr->bitrate_mode = V4L2_MPEG_VIDEO_BITRATE_MODE_VBR;
1005 else if (!strcmp (pvr_param_bitrate_mode, PVR_VIDEO_BITRATE_MODE_ARG_CBR))
1006 pvr->bitrate_mode = V4L2_MPEG_VIDEO_BITRATE_MODE_CBR;
1009 /* -pvr vpeak=x */
1010 if (pvr_param_bitrate_peak)
1011 pvr->bitrate_peak = pvr_param_bitrate_peak;
1013 /* -pvr fmt=x */
1014 if (pvr_param_stream_type)
1016 if (!strcmp (pvr_param_stream_type, PVR_VIDEO_STREAM_TYPE_PS))
1017 pvr->stream_type = V4L2_MPEG_STREAM_TYPE_MPEG2_PS;
1018 else if (!strcmp (pvr_param_stream_type, PVR_VIDEO_STREAM_TYPE_TS))
1019 pvr->stream_type = V4L2_MPEG_STREAM_TYPE_MPEG2_TS;
1020 else if (!strcmp (pvr_param_stream_type, PVR_VIDEO_STREAM_TYPE_MPEG1))
1021 pvr->stream_type = V4L2_MPEG_STREAM_TYPE_MPEG1_SS;
1022 else if (!strcmp (pvr_param_stream_type, PVR_VIDEO_STREAM_TYPE_DVD))
1023 pvr->stream_type = V4L2_MPEG_STREAM_TYPE_MPEG2_DVD;
1024 else if (!strcmp (pvr_param_stream_type, PVR_VIDEO_STREAM_TYPE_VCD))
1025 pvr->stream_type = V4L2_MPEG_STREAM_TYPE_MPEG1_VCD;
1026 else if (!strcmp (pvr_param_stream_type, PVR_VIDEO_STREAM_TYPE_SVCD))
1027 pvr->stream_type = V4L2_MPEG_STREAM_TYPE_MPEG2_SVCD;
1031 static void
1032 add_v4l2_ext_control (struct v4l2_ext_control *ctrl,
1033 uint32_t id, int32_t value)
1035 ctrl->id = id;
1036 ctrl->value = value;
1039 static int
1040 set_encoder_settings (struct pvr_t *pvr)
1042 struct v4l2_ext_control *ext_ctrl = NULL;
1043 struct v4l2_ext_controls ctrls;
1044 uint32_t count = 0;
1046 if (!pvr)
1047 return -1;
1049 if (pvr->dev_fd < 0)
1050 return -1;
1052 ext_ctrl = (struct v4l2_ext_control *)
1053 malloc (PVR_MAX_CONTROLS * sizeof (struct v4l2_ext_control));
1055 add_v4l2_ext_control (&ext_ctrl[count++], V4L2_CID_MPEG_VIDEO_ASPECT,
1056 pvr->aspect);
1058 add_v4l2_ext_control (&ext_ctrl[count++], V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
1059 pvr->samplerate);
1061 add_v4l2_ext_control (&ext_ctrl[count++], V4L2_CID_MPEG_AUDIO_ENCODING,
1062 pvr->layer);
1064 switch (pvr->layer)
1066 case V4L2_MPEG_AUDIO_ENCODING_LAYER_1:
1067 add_v4l2_ext_control (&ext_ctrl[count++], V4L2_CID_MPEG_AUDIO_L1_BITRATE,
1068 pvr->audio_rate);
1069 break;
1070 case V4L2_MPEG_AUDIO_ENCODING_LAYER_2:
1071 add_v4l2_ext_control (&ext_ctrl[count++], V4L2_CID_MPEG_AUDIO_L2_BITRATE,
1072 pvr->audio_rate);
1073 break;
1074 case V4L2_MPEG_AUDIO_ENCODING_LAYER_3:
1075 add_v4l2_ext_control (&ext_ctrl[count++], V4L2_CID_MPEG_AUDIO_L3_BITRATE,
1076 pvr->audio_rate);
1077 break;
1078 default:
1079 break;
1082 add_v4l2_ext_control (&ext_ctrl[count++], V4L2_CID_MPEG_AUDIO_MODE,
1083 pvr->audio_mode);
1085 add_v4l2_ext_control (&ext_ctrl[count++], V4L2_CID_MPEG_VIDEO_BITRATE,
1086 pvr->bitrate);
1088 add_v4l2_ext_control (&ext_ctrl[count++], V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
1089 pvr->bitrate_peak);
1091 add_v4l2_ext_control (&ext_ctrl[count++], V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
1092 pvr->bitrate_mode);
1094 add_v4l2_ext_control (&ext_ctrl[count++], V4L2_CID_MPEG_STREAM_TYPE,
1095 pvr->stream_type);
1097 /* set new encoding settings */
1098 ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
1099 ctrls.count = count;
1100 ctrls.controls = ext_ctrl;
1102 if (ioctl (pvr->dev_fd, VIDIOC_S_EXT_CTRLS, &ctrls) < 0)
1104 mp_msg (MSGT_OPEN, MSGL_ERR, "%s Error setting MPEG controls (%s).\n",
1105 LOG_LEVEL_ENCODER, strerror (errno));
1106 free (ext_ctrl);
1107 return -1;
1110 free (ext_ctrl);
1112 return 0;
1115 static void
1116 parse_v4l2_tv_options (struct pvr_t *pvr)
1118 if (!pvr)
1119 return;
1121 /* Create our station/channel list */
1122 parse_setup_stationlist (pvr);
1124 if (pvr->param_channel)
1126 if (set_station_by_channelname_or_freq (pvr, pvr->param_channel,
1127 -1, 0) >= 0)
1129 if (stream_tv_defaults.freq)
1131 mp_msg (MSGT_OPEN, MSGL_HINT,
1132 "%s tv param freq %s is overwritten by channel setting freq %d\n", LOG_LEVEL_V4L2,
1133 stream_tv_defaults.freq, pvr->freq);
1138 if (pvr->freq < 0 && stream_tv_defaults.freq)
1140 mp_msg (MSGT_OPEN, MSGL_HINT, "%s tv param freq %s is used directly\n",
1141 LOG_LEVEL_V4L2, stream_tv_defaults.freq);
1143 if (set_station_by_channelname_or_freq (pvr, NULL,
1144 atoi (stream_tv_defaults.freq), 0)<0)
1146 mp_msg (MSGT_OPEN, MSGL_WARN,
1147 "%s tv param freq %s invalid to set station\n",
1148 LOG_LEVEL_V4L2, stream_tv_defaults.freq);
1152 if (stream_tv_defaults.device)
1154 if (pvr->video_dev)
1155 free (pvr->video_dev);
1156 pvr->video_dev = strdup (stream_tv_defaults.device);
1159 if (stream_tv_defaults.noaudio)
1160 pvr->mute = stream_tv_defaults.noaudio;
1162 if (stream_tv_defaults.input)
1163 pvr->input = stream_tv_defaults.input;
1165 if (stream_tv_defaults.normid)
1166 pvr->normid = stream_tv_defaults.normid;
1168 if (stream_tv_defaults.brightness)
1169 pvr->brightness = stream_tv_defaults.brightness;
1171 if (stream_tv_defaults.contrast)
1172 pvr->contrast = stream_tv_defaults.contrast;
1174 if (stream_tv_defaults.hue)
1175 pvr->hue = stream_tv_defaults.hue;
1177 if (stream_tv_defaults.saturation)
1178 pvr->saturation = stream_tv_defaults.saturation;
1180 if (stream_tv_defaults.width)
1181 pvr->width = stream_tv_defaults.width;
1183 if (stream_tv_defaults.height)
1184 pvr->height = stream_tv_defaults.height;
1187 static int
1188 set_v4l2_settings (struct pvr_t *pvr)
1190 if (!pvr)
1191 return -1;
1193 if (pvr->dev_fd < 0)
1194 return -1;
1196 /* -tv noaudio */
1197 if (pvr->mute)
1199 struct v4l2_control ctrl;
1200 ctrl.id = V4L2_CID_AUDIO_MUTE;
1201 ctrl.value = 1;
1202 if (ioctl (pvr->dev_fd, VIDIOC_S_CTRL, &ctrl) < 0)
1204 mp_msg (MSGT_OPEN, MSGL_ERR,
1205 "%s can't mute (%s).\n", LOG_LEVEL_V4L2, strerror (errno));
1206 return -1;
1210 /* -tv input=x */
1211 if (pvr->input != 0)
1213 if (ioctl (pvr->dev_fd, VIDIOC_S_INPUT, &pvr->input) < 0)
1215 mp_msg (MSGT_OPEN, MSGL_ERR,
1216 "%s can't set input (%s)\n", LOG_LEVEL_V4L2, strerror (errno));
1217 return -1;
1221 /* -tv normid=x */
1222 if (pvr->normid != -1)
1224 struct v4l2_standard std;
1225 std.index = pvr->normid;
1227 if (ioctl (pvr->dev_fd, VIDIOC_ENUMSTD, &std) < 0)
1229 mp_msg (MSGT_OPEN, MSGL_ERR,
1230 "%s can't set norm (%s)\n", LOG_LEVEL_V4L2, strerror (errno));
1231 return -1;
1234 mp_msg (MSGT_OPEN, MSGL_V,
1235 "%s set norm to %s\n", LOG_LEVEL_V4L2, std.name);
1237 if (ioctl (pvr->dev_fd, VIDIOC_S_STD, &std.id) < 0)
1239 mp_msg (MSGT_OPEN, MSGL_ERR,
1240 "%s can't set norm (%s)\n", LOG_LEVEL_V4L2, strerror (errno));
1241 return -1;
1245 /* -tv brightness=x */
1246 if (pvr->brightness != 0)
1248 struct v4l2_control ctrl;
1249 ctrl.id = V4L2_CID_BRIGHTNESS;
1250 ctrl.value = pvr->brightness;
1252 if (ctrl.value < 0)
1253 ctrl.value = 0;
1254 if (ctrl.value > 255)
1255 ctrl.value = 255;
1257 if (ioctl (pvr->dev_fd, VIDIOC_S_CTRL, &ctrl) < 0)
1259 mp_msg (MSGT_OPEN, MSGL_ERR,
1260 "%s can't set brightness to %d (%s).\n",
1261 LOG_LEVEL_V4L2, ctrl.value, strerror (errno));
1262 return -1;
1266 /* -tv contrast=x */
1267 if (pvr->contrast != 0)
1269 struct v4l2_control ctrl;
1270 ctrl.id = V4L2_CID_CONTRAST;
1271 ctrl.value = pvr->contrast;
1273 if (ctrl.value < 0)
1274 ctrl.value = 0;
1275 if (ctrl.value > 127)
1276 ctrl.value = 127;
1278 if (ioctl (pvr->dev_fd, VIDIOC_S_CTRL, &ctrl) < 0)
1280 mp_msg (MSGT_OPEN, MSGL_ERR,
1281 "%s can't set contrast to %d (%s).\n",
1282 LOG_LEVEL_V4L2, ctrl.value, strerror (errno));
1283 return -1;
1287 /* -tv hue=x */
1288 if (pvr->hue != 0)
1290 struct v4l2_control ctrl;
1291 ctrl.id = V4L2_CID_HUE;
1292 ctrl.value = pvr->hue;
1294 if (ctrl.value < -128)
1295 ctrl.value = -128;
1296 if (ctrl.value > 127)
1297 ctrl.value = 127;
1299 if (ioctl (pvr->dev_fd, VIDIOC_S_CTRL, &ctrl) < 0)
1301 mp_msg (MSGT_OPEN, MSGL_ERR,
1302 "%s can't set hue to %d (%s).\n",
1303 LOG_LEVEL_V4L2, ctrl.value, strerror (errno));
1304 return -1;
1308 /* -tv saturation=x */
1309 if (pvr->saturation != 0)
1311 struct v4l2_control ctrl;
1312 ctrl.id = V4L2_CID_SATURATION;
1313 ctrl.value = pvr->saturation;
1315 if (ctrl.value < 0)
1316 ctrl.value = 0;
1317 if (ctrl.value > 127)
1318 ctrl.value = 127;
1320 if (ioctl (pvr->dev_fd, VIDIOC_S_CTRL, &ctrl) < 0)
1322 mp_msg (MSGT_OPEN, MSGL_ERR,
1323 "%s can't set saturation to %d (%s).\n",
1324 LOG_LEVEL_V4L2, ctrl.value, strerror (errno));
1325 return -1;
1329 /* -tv width=x:height=y */
1330 if (pvr->width && pvr->height)
1332 struct v4l2_format vfmt;
1333 vfmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1334 vfmt.fmt.pix.width = pvr->width;
1335 vfmt.fmt.pix.height = pvr->height;
1337 if (ioctl (pvr->dev_fd, VIDIOC_S_FMT, &vfmt) < 0)
1339 mp_msg (MSGT_OPEN, MSGL_ERR,
1340 "%s can't set resolution to %dx%d (%s).\n",
1341 LOG_LEVEL_V4L2, pvr->width, pvr->height, strerror (errno));
1342 return -1;
1346 if (pvr->freq < 0)
1348 int freq = get_v4l2_freq (pvr);
1349 mp_msg (MSGT_OPEN, MSGL_INFO,
1350 "%s Using current set frequency %d, to set channel\n",
1351 LOG_LEVEL_V4L2, freq);
1353 if (0 < freq)
1354 return set_station_by_channelname_or_freq (pvr, NULL, freq, 1);
1357 if (0 < pvr->freq)
1358 return set_v4l2_freq (pvr) ;
1360 return 0;
1363 static int
1364 v4l2_list_capabilities (struct pvr_t *pvr)
1366 struct v4l2_audio vaudio;
1367 struct v4l2_standard vs;
1368 struct v4l2_input vin;
1369 int err = 0;
1371 if (!pvr)
1372 return -1;
1374 if (pvr->dev_fd < 0)
1375 return -1;
1377 /* list available video inputs */
1378 vin.index = 0;
1379 err = 1;
1380 mp_msg (MSGT_OPEN, MSGL_INFO,
1381 "%s Available video inputs: ", LOG_LEVEL_V4L2);
1382 while (ioctl (pvr->dev_fd, VIDIOC_ENUMINPUT, &vin) >= 0)
1384 err = 0;
1385 mp_msg (MSGT_OPEN, MSGL_INFO, "'#%d, %s' ", vin.index, vin.name);
1386 vin.index++;
1388 if (err)
1390 mp_msg (MSGT_OPEN, MSGL_INFO, "none\n");
1391 return -1;
1393 else
1394 mp_msg (MSGT_OPEN, MSGL_INFO, "\n");
1396 /* list available audio inputs */
1397 vaudio.index = 0;
1398 err = 1;
1399 mp_msg (MSGT_OPEN, MSGL_INFO,
1400 "%s Available audio inputs: ", LOG_LEVEL_V4L2);
1401 while (ioctl (pvr->dev_fd, VIDIOC_ENUMAUDIO, &vaudio) >= 0)
1403 err = 0;
1404 mp_msg (MSGT_OPEN, MSGL_INFO, "'#%d, %s' ", vaudio.index, vaudio.name);
1405 vaudio.index++;
1407 if (err)
1409 mp_msg (MSGT_OPEN, MSGL_INFO, "none\n");
1410 return -1;
1412 else
1413 mp_msg (MSGT_OPEN, MSGL_INFO, "\n");
1415 /* list available norms */
1416 vs.index = 0;
1417 mp_msg (MSGT_OPEN, MSGL_INFO, "%s Available norms: ", LOG_LEVEL_V4L2);
1418 while (ioctl (pvr->dev_fd, VIDIOC_ENUMSTD, &vs) >= 0)
1420 err = 0;
1421 mp_msg (MSGT_OPEN, MSGL_INFO, "'#%d, %s' ", vs.index, vs.name);
1422 vs.index++;
1424 if (err)
1426 mp_msg (MSGT_OPEN, MSGL_INFO, "none\n");
1427 return -1;
1429 else
1430 mp_msg (MSGT_OPEN, MSGL_INFO, "\n");
1432 return 0;
1435 static int
1436 v4l2_display_settings (struct pvr_t *pvr)
1438 struct v4l2_audio vaudio;
1439 struct v4l2_standard vs;
1440 struct v4l2_input vin;
1441 v4l2_std_id std;
1442 int input;
1444 if (!pvr)
1445 return -1;
1447 if (pvr->dev_fd < 0)
1448 return -1;
1450 /* get current video input */
1451 if (ioctl (pvr->dev_fd, VIDIOC_G_INPUT, &input) == 0)
1453 vin.index = input;
1454 if (ioctl (pvr->dev_fd, VIDIOC_ENUMINPUT, &vin) < 0)
1456 mp_msg (MSGT_OPEN, MSGL_ERR,
1457 "%s can't get input (%s).\n", LOG_LEVEL_V4L2, strerror (errno));
1458 return -1;
1460 else
1461 mp_msg (MSGT_OPEN, MSGL_INFO,
1462 "%s Video input: %s\n", LOG_LEVEL_V4L2, vin.name);
1464 else
1466 mp_msg (MSGT_OPEN, MSGL_ERR,
1467 "%s can't get input (%s).\n", LOG_LEVEL_V4L2, strerror (errno));
1468 return -1;
1471 /* get current audio input */
1472 if (ioctl (pvr->dev_fd, VIDIOC_G_AUDIO, &vaudio) == 0)
1474 mp_msg (MSGT_OPEN, MSGL_INFO,
1475 "%s Audio input: %s\n", LOG_LEVEL_V4L2, vaudio.name);
1477 else
1479 mp_msg (MSGT_OPEN, MSGL_ERR,
1480 "%s can't get input (%s).\n", LOG_LEVEL_V4L2, strerror (errno));
1481 return -1;
1484 /* get current video format */
1485 if (ioctl (pvr->dev_fd, VIDIOC_G_STD, &std) == 0)
1487 vs.index = 0;
1489 while (ioctl (pvr->dev_fd, VIDIOC_ENUMSTD, &vs) >= 0)
1491 if (vs.id == std)
1493 mp_msg (MSGT_OPEN, MSGL_INFO,
1494 "%s Norm: %s.\n", LOG_LEVEL_V4L2, vs.name);
1495 break;
1497 vs.index++;
1500 else
1502 mp_msg (MSGT_OPEN, MSGL_ERR,
1503 "%s can't get norm (%s)\n", LOG_LEVEL_V4L2, strerror (errno));
1504 return -1;
1507 return 0;
1510 /* stream layer */
1512 static void
1513 pvr_stream_close (stream_t *stream)
1515 struct pvr_t *pvr;
1517 if (!stream)
1518 return;
1520 pvr = (struct pvr_t *) stream->priv;
1521 pvr_uninit (pvr);
1524 static int
1525 pvr_stream_read (stream_t *stream, char *buffer, int size)
1527 struct pollfd pfds[1];
1528 struct pvr_t *pvr;
1529 int rk, fd, pos;
1531 if (!stream || !buffer)
1532 return 0;
1534 pvr = (struct pvr_t *) stream->priv;
1535 fd = pvr->dev_fd;
1536 pos = 0;
1538 if (fd < 0)
1539 return 0;
1541 while (pos < size)
1543 pfds[0].fd = fd;
1544 pfds[0].events = POLLIN | POLLPRI;
1546 rk = size - pos;
1548 if (poll (pfds, 1, 500) <= 0)
1550 mp_msg (MSGT_OPEN, MSGL_ERR,
1551 "%s failed with errno %d when reading %d bytes\n",
1552 LOG_LEVEL_PVR, errno, size-pos);
1553 break;
1556 rk = read (fd, &buffer[pos], rk);
1557 if (rk > 0)
1559 pos += rk;
1560 mp_msg (MSGT_OPEN, MSGL_DBG3,
1561 "%s read (%d) bytes\n", LOG_LEVEL_PVR, pos);
1565 if (!pos)
1566 mp_msg (MSGT_OPEN, MSGL_ERR, "%s read %d bytes\n", LOG_LEVEL_PVR, pos);
1568 return pos;
1571 static int
1572 pvr_stream_open (stream_t *stream, int mode, void *opts, int *file_format)
1574 struct v4l2_capability vcap;
1575 struct v4l2_ext_controls ctrls;
1576 struct pvr_t *pvr = NULL;
1578 if (mode != STREAM_READ)
1579 return STREAM_UNSUPPORTED;
1581 pvr = pvr_init ();
1584 * if the url, i.e. 'pvr://8', contains the channel, use it,
1585 * else use the tv parameter.
1587 if (stream->url && strlen (stream->url) > 6 && stream->url[6] != '\0')
1588 pvr->param_channel = strdup (stream->url + 6);
1589 else if (stream_tv_defaults.channel && strlen (stream_tv_defaults.channel))
1590 pvr->param_channel = strdup (stream_tv_defaults.channel);
1592 parse_v4l2_tv_options (pvr);
1593 parse_encoder_options (pvr);
1595 /* open device */
1596 pvr->dev_fd = open (pvr->video_dev, O_RDWR);
1597 mp_msg (MSGT_OPEN, MSGL_INFO,
1598 "%s Using device %s\n", LOG_LEVEL_PVR, pvr->video_dev);
1599 if (pvr->dev_fd == -1)
1601 mp_msg (MSGT_OPEN, MSGL_ERR,
1602 "%s error opening device %s\n", LOG_LEVEL_PVR, pvr->video_dev);
1603 pvr_uninit (pvr);
1604 return STREAM_ERROR;
1607 /* query capabilities (i.e test V4L2 support) */
1608 if (ioctl (pvr->dev_fd, VIDIOC_QUERYCAP, &vcap) < 0)
1610 mp_msg (MSGT_OPEN, MSGL_ERR,
1611 "%s device is not V4L2 compliant (%s).\n",
1612 LOG_LEVEL_PVR, strerror (errno));
1613 pvr_uninit (pvr);
1614 return STREAM_ERROR;
1616 else
1617 mp_msg (MSGT_OPEN, MSGL_INFO,
1618 "%s Detected %s\n", LOG_LEVEL_PVR, vcap.card);
1620 /* check for a valid V4L2 capture device */
1621 if (!(vcap.capabilities & V4L2_CAP_VIDEO_CAPTURE))
1623 mp_msg (MSGT_OPEN, MSGL_ERR,
1624 "%s device is not a valid V4L2 capture device.\n",
1625 LOG_LEVEL_PVR);
1626 pvr_uninit (pvr);
1627 return STREAM_ERROR;
1630 /* check for device hardware MPEG encoding capability */
1631 ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
1632 ctrls.count = 0;
1633 ctrls.controls = NULL;
1635 if (ioctl (pvr->dev_fd, VIDIOC_G_EXT_CTRLS, &ctrls) < 0)
1637 mp_msg (MSGT_OPEN, MSGL_ERR,
1638 "%s device do not support MPEG input.\n", LOG_LEVEL_ENCODER);
1639 return STREAM_ERROR;
1642 /* list V4L2 capabilities */
1643 if (v4l2_list_capabilities (pvr) == -1)
1645 mp_msg (MSGT_OPEN, MSGL_ERR,
1646 "%s can't get v4l2 capabilities\n", LOG_LEVEL_PVR);
1647 pvr_uninit (pvr);
1648 return STREAM_ERROR;
1651 /* apply V4L2 settings */
1652 if (set_v4l2_settings (pvr) == -1)
1654 mp_msg (MSGT_OPEN, MSGL_ERR,
1655 "%s can't set v4l2 settings\n", LOG_LEVEL_PVR);
1656 pvr_uninit (pvr);
1657 return STREAM_ERROR;
1660 /* apply encoder settings */
1661 if (set_encoder_settings (pvr) == -1)
1663 mp_msg (MSGT_OPEN, MSGL_ERR,
1664 "%s can't set encoder settings\n", LOG_LEVEL_PVR);
1665 pvr_uninit (pvr);
1666 return STREAM_ERROR;
1669 /* display current V4L2 settings */
1670 if (v4l2_display_settings (pvr) == -1)
1672 mp_msg (MSGT_OPEN, MSGL_ERR,
1673 "%s can't get v4l2 settings\n", LOG_LEVEL_PVR);
1674 pvr_uninit (pvr);
1675 return STREAM_ERROR;
1678 stream->priv = pvr;
1679 stream->type = STREAMTYPE_PVR;
1680 stream->fill_buffer = pvr_stream_read;
1681 stream->close = pvr_stream_close;
1683 return STREAM_OK;
1686 /* PVR Public API access */
1688 const char *
1689 pvr_get_current_stationname (stream_t *stream)
1691 struct pvr_t *pvr;
1693 if (!stream || stream->type != STREAMTYPE_PVR)
1694 return NULL;
1696 pvr = (struct pvr_t *) stream->priv;
1698 if (pvr->stationlist.list &&
1699 pvr->stationlist.used > pvr->chan_idx &&
1700 pvr->chan_idx >= 0)
1701 return pvr->stationlist.list[pvr->chan_idx].station;
1703 return NULL;
1706 const char *
1707 pvr_get_current_channelname (stream_t *stream)
1709 struct pvr_t *pvr = (struct pvr_t *) stream->priv;
1711 if (pvr->stationlist.list &&
1712 pvr->stationlist.used > pvr->chan_idx &&
1713 pvr->chan_idx >= 0)
1714 return pvr->stationlist.list[pvr->chan_idx].name;
1716 return NULL;
1720 pvr_get_current_frequency (stream_t *stream)
1722 struct pvr_t *pvr = (struct pvr_t *) stream->priv;
1724 return pvr->freq;
1728 pvr_set_channel (stream_t *stream, const char * channel)
1730 struct pvr_t *pvr = (struct pvr_t *) stream->priv;
1732 return set_station_by_channelname_or_freq (pvr, channel, -1, 1);
1736 pvr_set_lastchannel (stream_t *stream)
1738 struct pvr_t *pvr = (struct pvr_t *) stream->priv;
1740 if (pvr->stationlist.list &&
1741 pvr->stationlist.used > pvr->chan_idx_last &&
1742 pvr->chan_idx_last >= 0)
1743 return set_station_by_channelname_or_freq (pvr, pvr->stationlist.list[pvr->chan_idx_last].name, -1, 1);
1745 return -1;
1749 pvr_set_freq (stream_t *stream, int freq)
1751 struct pvr_t *pvr = (struct pvr_t *) stream->priv;
1753 return set_station_by_channelname_or_freq (pvr, NULL, freq, 1);
1757 pvr_set_channel_step (stream_t *stream, int step)
1759 struct pvr_t *pvr = (struct pvr_t *) stream->priv;
1761 return set_station_by_step (pvr, step, 1);
1765 pvr_force_freq_step (stream_t *stream, int step)
1767 struct pvr_t *pvr = (struct pvr_t *) stream->priv;
1769 return force_freq_step (pvr, step);
1772 const stream_info_t stream_info_pvr = {
1773 "V4L2 MPEG Input (a.k.a PVR)",
1774 "pvr",
1775 "Benjamin Zores",
1777 pvr_stream_open,
1778 { "pvr", NULL },
1779 NULL,