mp_msg: print messages to stdout, statusline to stderr
[mplayer.git] / stream / stream_pvr.c
blob7fd3fe3a347f745719e8b33c2edb60b3ce852607
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 <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[PVR_STATION_NAME_SIZE];
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 free (pvr->video_dev);
191 free (pvr->stationlist.list);
192 free (pvr->param_channel);
193 free (pvr);
197 * @brief Copy Constructor for stationlist
199 * @see parse_setup_stationlist
201 static int
202 copycreate_stationlist (stationlist_t *stationlist, int num)
204 int i;
206 if (chantab < 0 || !stationlist)
207 return -1;
209 num = FFMAX (num, chanlists[chantab].count);
211 free (stationlist->list);
212 stationlist->list = NULL;
214 stationlist->total = 0;
215 stationlist->enabled = 0;
216 stationlist->used = 0;
217 stationlist->list = calloc (num, sizeof (station_elem_t));
219 if (!stationlist->list)
221 mp_msg (MSGT_OPEN, MSGL_ERR,
222 "%s No memory allocated for station list, giving up\n",
223 LOG_LEVEL_V4L2);
224 return -1;
227 /* transport the channel list data to our extented struct */
228 stationlist->total = num;
229 av_strlcpy (stationlist->name, chanlists[chantab].name, PVR_STATION_NAME_SIZE);
231 for (i = 0; i < chanlists[chantab].count; i++)
233 stationlist->list[i].station[0]= '\0'; /* no station name yet */
234 av_strlcpy (stationlist->list[i].name,
235 chanlists[chantab].list[i].name, PVR_STATION_NAME_SIZE);
236 stationlist->list[i].freq = chanlists[chantab].list[i].freq;
237 stationlist->list[i].enabled = 1; /* default enabled */
238 stationlist->enabled++;
239 stationlist->used++;
242 return 0;
245 static int
246 print_all_stations (struct pvr_t *pvr)
248 int i;
250 if (!pvr || !pvr->stationlist.list)
251 return -1;
253 for (i = 0; i < pvr->stationlist.total; i++)
255 mp_msg (MSGT_OPEN, MSGL_V,
256 "%s %3d: [%c] channel: %8s - freq: %8d - station: %s\n",
257 LOG_LEVEL_V4L2, i, (pvr->stationlist.list[i].enabled) ? 'X' : ' ',
258 pvr->stationlist.list[i].name, pvr->stationlist.list[i].freq,
259 pvr->stationlist.list[i].station);
262 return 0;
266 * Disables all stations
268 * @see parse_setup_stationlist
270 static void
271 disable_all_stations (struct pvr_t *pvr)
273 int i;
275 for (i = 0; i < pvr->stationlist.total; i++)
276 pvr->stationlist.list[i].enabled = 0;
277 pvr->stationlist.enabled = 0;
281 * Update or add a station
283 * @see parse_setup_stationlist
285 static int
286 set_station (struct pvr_t *pvr, const char *station,
287 const char *channel, int freq)
289 int i;
291 if (!pvr || !pvr->stationlist.list)
292 return -1;
294 if (0 >= pvr->stationlist.total || (!channel && !freq))
295 return -1;
297 /* select channel */
298 for (i = 0; i < pvr->stationlist.used; i++)
300 if (channel && !strcasecmp (pvr->stationlist.list[i].name, channel))
301 break; /* found existing channel entry */
303 if (freq > 0 && pvr->stationlist.list[i].freq == freq)
304 break; /* found existing frequency entry */
307 if (i < pvr->stationlist.used)
310 * found an existing entry,
311 * which is about to change with the user data.
312 * it is also enabled ..
314 if (!pvr->stationlist.list[i].enabled)
316 pvr->stationlist.list[i].enabled = 1;
317 pvr->stationlist.enabled++;
320 if (station)
321 av_strlcpy (pvr->stationlist.list[i].station,
322 station, PVR_STATION_NAME_SIZE);
323 else if (channel)
324 av_strlcpy (pvr->stationlist.list[i].station,
325 channel, PVR_STATION_NAME_SIZE);
326 else
327 snprintf (pvr->stationlist.list[i].station,
328 PVR_STATION_NAME_SIZE, "F %d", freq);
330 mp_msg (MSGT_OPEN, MSGL_DBG2,
331 "%s Set user station channel: %8s - freq: %8d - station: %s\n",
332 LOG_LEVEL_V4L2, pvr->stationlist.list[i].name,
333 pvr->stationlist.list[i].freq,
334 pvr->stationlist.list[i].station);
335 return 0;
338 /* from here on, we have to create a new entry, frequency is mandatory */
339 if (freq < 0)
341 mp_msg (MSGT_OPEN, MSGL_ERR,
342 "%s Cannot add new station/channel without frequency\n",
343 LOG_LEVEL_V4L2);
344 return -1;
347 if (pvr->stationlist.total < i)
350 * we have to extend the stationlist about
351 * an arbitrary size, even though this path is not performance critical
353 pvr->stationlist.total += 10;
354 pvr->stationlist.list =
355 realloc (pvr->stationlist.list,
356 pvr->stationlist.total * sizeof (station_elem_t));
358 if (!pvr->stationlist.list)
360 mp_msg (MSGT_OPEN, MSGL_ERR,
361 "%s No memory allocated for station list, giving up\n",
362 LOG_LEVEL_V4L2);
363 return -1;
366 /* clear the new space ..*/
367 memset (&(pvr->stationlist.list[pvr->stationlist.used]), 0,
368 (pvr->stationlist.total - pvr->stationlist.used)
369 * sizeof (station_elem_t));
372 /* here we go, our actual new entry */
373 pvr->stationlist.used++;
374 pvr->stationlist.list[i].enabled = 1;
375 pvr->stationlist.enabled++;
377 if (station)
378 av_strlcpy (pvr->stationlist.list[i].station,
379 station, PVR_STATION_NAME_SIZE);
380 if (channel)
381 av_strlcpy (pvr->stationlist.list[i].name, channel, PVR_STATION_NAME_SIZE);
382 else
383 snprintf (pvr->stationlist.list[i].name,
384 PVR_STATION_NAME_SIZE, "F %d", freq);
386 pvr->stationlist.list[i].freq = freq;
388 mp_msg (MSGT_OPEN, MSGL_DBG2,
389 "%s Add user station channel: %8s - freq: %8d - station: %s\n",
390 LOG_LEVEL_V4L2, pvr->stationlist.list[i].name,
391 pvr->stationlist.list[i].freq,
392 pvr->stationlist.list[i].station);
394 return 0;
398 * Here we set our stationlist, as follow
399 * - choose the frequency channel table, e.g. ntsc-cable
400 * - create our stationlist, same element size as the channellist
401 * - copy the channellist content to our stationlist
402 * - IF the user provides his channel-mapping, THEN:
403 * - disable all stations
404 * - update and/or create entries in the stationlist and enable them
406 static int
407 parse_setup_stationlist (struct pvr_t *pvr)
409 int i;
411 if (!pvr)
412 return -1;
414 /* Create our station/channel list */
415 if (stream_tv_defaults.chanlist)
417 /* select channel list */
418 for (i = 0; chanlists[i].name != NULL; i++)
420 if (!strcasecmp (chanlists[i].name, stream_tv_defaults.chanlist))
422 chantab = i;
423 break;
426 if (!chanlists[i].name)
428 mp_msg (MSGT_OPEN, MSGL_ERR,
429 "%s unable to find channel list %s, using default %s\n",
430 LOG_LEVEL_V4L2, stream_tv_defaults.chanlist, chanlists[chantab].name);
432 else
434 mp_msg (MSGT_OPEN, MSGL_INFO,
435 "%s select channel list %s, entries %d\n", LOG_LEVEL_V4L2,
436 chanlists[chantab].name, chanlists[chantab].count);
440 if (0 > chantab)
442 mp_msg (MSGT_OPEN, MSGL_FATAL,
443 "%s No channel list selected, giving up\n", LOG_LEVEL_V4L2);
444 return -1;
447 if (copycreate_stationlist (&(pvr->stationlist), -1) < 0)
449 mp_msg (MSGT_OPEN, MSGL_FATAL,
450 "%s No memory allocated for station list, giving up\n",
451 LOG_LEVEL_V4L2);
452 return -1;
455 /* Handle user channel mappings */
456 if (stream_tv_defaults.channels)
458 char channel[PVR_STATION_NAME_SIZE];
459 char station[PVR_STATION_NAME_SIZE];
460 char **channels = stream_tv_defaults.channels;
462 disable_all_stations (pvr);
464 while (*channels)
466 char *tmp = *(channels++);
467 char *sep = strchr (tmp, '-');
468 int freq=-1;
470 if (!sep)
471 continue; /* Wrong syntax, but mplayer should not crash */
473 av_strlcpy (station, sep + 1, PVR_STATION_NAME_SIZE);
475 sep[0] = '\0';
476 av_strlcpy (channel, tmp, PVR_STATION_NAME_SIZE);
478 while ((sep = strchr (station, '_')))
479 sep[0] = ' ';
481 /* if channel number is a number and larger than 1000 treat it as
482 * frequency tmp still contain pointer to null-terminated string with
483 * channel number here
485 if ((freq = atoi (channel)) <= 1000)
486 freq = -1;
488 if (set_station (pvr, station, (freq <= 0) ? channel : NULL, freq) < 0)
490 mp_msg (MSGT_OPEN, MSGL_ERR,
491 "%s Unable to set user station channel: %8s - freq: %8d - station: %s\n", LOG_LEVEL_V4L2,
492 channel, freq, station);
497 return print_all_stations (pvr);
500 static int
501 get_v4l2_freq (struct pvr_t *pvr)
503 int freq;
504 struct v4l2_frequency vf;
505 struct v4l2_tuner vt;
507 if (!pvr)
508 return -1;
510 if (pvr->dev_fd < 0)
511 return -1;
513 memset (&vt, 0, sizeof (vt));
514 memset (&vf, 0, sizeof (vf));
516 if (ioctl (pvr->dev_fd, VIDIOC_G_TUNER, &vt) < 0)
518 mp_msg (MSGT_OPEN, MSGL_ERR, "%s can't set tuner (%s).\n",
519 LOG_LEVEL_V4L2, strerror (errno));
520 return -1;
523 if (ioctl (pvr->dev_fd, VIDIOC_G_FREQUENCY, &vf) < 0)
525 mp_msg (MSGT_OPEN, MSGL_ERR, "%s can't get frequency %d.\n",
526 LOG_LEVEL_V4L2, errno);
527 return -1;
529 freq = vf.frequency;
530 if (!(vt.capability & V4L2_TUNER_CAP_LOW))
531 freq *= 1000;
532 freq /= 16;
534 return freq;
537 static int
538 set_v4l2_freq (struct pvr_t *pvr)
540 struct v4l2_frequency vf;
541 struct v4l2_tuner vt;
543 if (!pvr)
544 return -1;
546 if (0 >= pvr->freq)
548 mp_msg (MSGT_OPEN, MSGL_ERR,
549 "%s Frequency invalid %d !!!\n", LOG_LEVEL_V4L2, pvr->freq);
550 return -1;
553 /* don't set the frequency, if it's already set.
554 * setting it here would interrupt the stream.
556 if (get_v4l2_freq (pvr) == pvr->freq)
558 mp_msg (MSGT_OPEN, MSGL_STATUS,
559 "%s Frequency %d already set.\n", LOG_LEVEL_V4L2, pvr->freq);
560 return 0;
563 if (pvr->dev_fd < 0)
564 return -1;
566 memset (&vf, 0, sizeof (vf));
567 memset (&vt, 0, sizeof (vt));
569 if (ioctl (pvr->dev_fd, VIDIOC_G_TUNER, &vt) < 0)
571 mp_msg (MSGT_OPEN, MSGL_ERR, "%s can't get tuner (%s).\n",
572 LOG_LEVEL_V4L2, strerror (errno));
573 return -1;
576 vf.type = vt.type;
577 vf.frequency = pvr->freq * 16;
579 if (!(vt.capability & V4L2_TUNER_CAP_LOW))
580 vf.frequency /= 1000;
582 if (ioctl (pvr->dev_fd, VIDIOC_S_FREQUENCY, &vf) < 0)
584 mp_msg (MSGT_OPEN, MSGL_ERR, "%s can't set frequency (%s).\n",
585 LOG_LEVEL_V4L2, strerror (errno));
586 return -1;
589 memset (&vt, 0, sizeof(vt));
590 if (ioctl (pvr->dev_fd, VIDIOC_G_TUNER, &vt) < 0)
592 mp_msg (MSGT_OPEN, MSGL_ERR, "%s can't set tuner (%s).\n",
593 LOG_LEVEL_V4L2, strerror (errno));
594 return -1;
597 /* just a notification */
598 if (!vt.signal)
599 mp_msg (MSGT_OPEN, MSGL_ERR, "%s NO SIGNAL at frequency %d (%d)\n",
600 LOG_LEVEL_V4L2, pvr->freq, vf.frequency);
601 else
602 mp_msg (MSGT_OPEN, MSGL_STATUS, "%s Got signal at frequency %d (%d)\n",
603 LOG_LEVEL_V4L2, pvr->freq, vf.frequency);
605 return 0;
608 static int
609 set_station_by_step (struct pvr_t *pvr, int step, int v4lAction)
611 if (!pvr || !pvr->stationlist.list)
612 return -1;
614 if (pvr->stationlist.enabled >= abs (step))
616 int gotcha = 0;
617 int chidx = pvr->chan_idx + step;
619 while (!gotcha)
621 chidx = (chidx + pvr->stationlist.used) % pvr->stationlist.used;
623 mp_msg (MSGT_OPEN, MSGL_DBG2,
624 "%s Offset switch: current %d, enabled %d, step %d -> %d\n",
625 LOG_LEVEL_V4L2, pvr->chan_idx,
626 pvr->stationlist.enabled, step, chidx);
628 if (!pvr->stationlist.list[chidx].enabled)
630 mp_msg (MSGT_OPEN, MSGL_DBG2,
631 "%s Switch disabled to user station channel: %8s - freq: %8d - station: %s\n", LOG_LEVEL_V4L2,
632 pvr->stationlist.list[chidx].name,
633 pvr->stationlist.list[chidx].freq,
634 pvr->stationlist.list[chidx].station);
635 chidx += FFSIGN (step);
637 else
638 gotcha = 1;
641 pvr->freq = pvr->stationlist.list[chidx].freq;
642 pvr->chan_idx_last = pvr->chan_idx;
643 pvr->chan_idx = chidx;
645 mp_msg (MSGT_OPEN, MSGL_INFO,
646 "%s Switch to user station channel: %8s - freq: %8d - station: %s\n", LOG_LEVEL_V4L2,
647 pvr->stationlist.list[chidx].name,
648 pvr->stationlist.list[chidx].freq,
649 pvr->stationlist.list[chidx].station);
651 if (v4lAction)
652 return set_v4l2_freq (pvr);
654 return (pvr->freq > 0) ? 0 : -1;
657 mp_msg (MSGT_OPEN, MSGL_ERR,
658 "%s Ooops couldn't set freq by channel entry step %d to current %d, enabled %d\n", LOG_LEVEL_V4L2,
659 step, pvr->chan_idx, pvr->stationlist.enabled);
661 return -1;
664 static int
665 set_station_by_channelname_or_freq (struct pvr_t *pvr, const char *channel,
666 int freq, int v4lAction)
668 int i = 0;
670 if (!pvr || !pvr->stationlist.list)
671 return -1;
673 if (0 >= pvr->stationlist.enabled)
675 mp_msg (MSGT_OPEN, MSGL_WARN,
676 "%s No enabled station, cannot switch channel/frequency\n",
677 LOG_LEVEL_V4L2);
678 return -1;
681 if (channel)
683 /* select by channel */
684 for (i = 0; i < pvr->stationlist.used ; i++)
686 if (!strcasecmp (pvr->stationlist.list[i].name, channel))
688 if (!pvr->stationlist.list[i].enabled)
690 mp_msg (MSGT_OPEN, MSGL_WARN,
691 "%s Switch disabled to user station channel: %8s - freq: %8d - station: %s\n", LOG_LEVEL_V4L2,
692 pvr->stationlist.list[i].name,
693 pvr->stationlist.list[i].freq,
694 pvr->stationlist.list[i].station);
696 return -1;
699 pvr->freq = pvr->stationlist.list[i].freq;
700 pvr->chan_idx_last = pvr->chan_idx;
701 pvr->chan_idx = i;
702 break;
706 else if (freq >= 0)
708 /* select by freq */
709 for (i = 0; i < pvr->stationlist.used; i++)
711 if (pvr->stationlist.list[i].freq == freq)
713 if (!pvr->stationlist.list[i].enabled)
715 mp_msg (MSGT_OPEN, MSGL_WARN,
716 "%s Switch disabled to user station channel: %8s - freq: %8d - station: %s\n", LOG_LEVEL_V4L2,
717 pvr->stationlist.list[i].name,
718 pvr->stationlist.list[i].freq,
719 pvr->stationlist.list[i].station);
721 return -1;
724 pvr->freq = pvr->stationlist.list[i].freq;
725 pvr->chan_idx_last = pvr->chan_idx;
726 pvr->chan_idx = i;
727 break;
732 if (i >= pvr->stationlist.used)
734 if (channel)
735 mp_msg (MSGT_OPEN, MSGL_WARN,
736 "%s unable to find channel %s\n", LOG_LEVEL_V4L2, channel);
737 else
738 mp_msg (MSGT_OPEN, MSGL_WARN,
739 "%s unable to find frequency %d\n", LOG_LEVEL_V4L2, freq);
740 return -1;
743 mp_msg (MSGT_OPEN, MSGL_INFO,
744 "%s Switch to user station channel: %8s - freq: %8d - station: %s\n", LOG_LEVEL_V4L2,
745 pvr->stationlist.list[i].name,
746 pvr->stationlist.list[i].freq,
747 pvr->stationlist.list[i].station);
749 if (v4lAction)
750 return set_v4l2_freq (pvr);
752 return (pvr->freq > 0) ? 0 : -1;
755 static int
756 force_freq_step (struct pvr_t *pvr, int step)
758 int freq;
760 if (!pvr)
761 return -1;
763 freq = pvr->freq+step;
765 if (freq)
767 mp_msg (MSGT_OPEN, MSGL_INFO,
768 "%s Force Frequency %d + %d = %d \n", LOG_LEVEL_V4L2,
769 pvr->freq, step, freq);
771 pvr->freq = freq;
773 return set_v4l2_freq (pvr);
776 return -1;
779 static void
780 parse_encoder_options (struct pvr_t *pvr)
782 if (!pvr)
783 return;
785 /* -pvr aspect=digit */
786 if (pvr_param_aspect_ratio >= 0 && pvr_param_aspect_ratio <= 3)
787 pvr->aspect = pvr_param_aspect_ratio;
789 /* -pvr arate=x */
790 if (pvr_param_sample_rate != 0)
792 switch (pvr_param_sample_rate)
794 case 32000:
795 pvr->samplerate = V4L2_MPEG_AUDIO_SAMPLING_FREQ_32000;
796 break;
797 case 44100:
798 pvr->samplerate = V4L2_MPEG_AUDIO_SAMPLING_FREQ_44100;
799 break;
800 case 48000:
801 pvr->samplerate = V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000;
802 break;
803 default:
804 break;
808 /* -pvr alayer=x */
809 if (pvr_param_audio_layer == 1)
810 pvr->layer = V4L2_MPEG_AUDIO_ENCODING_LAYER_1;
811 else if (pvr_param_audio_layer == 2)
812 pvr->layer = V4L2_MPEG_AUDIO_ENCODING_LAYER_2;
813 else if (pvr_param_audio_layer == 3)
814 pvr->layer = V4L2_MPEG_AUDIO_ENCODING_LAYER_3;
816 /* -pvr abitrate=x */
817 if (pvr_param_audio_bitrate != 0)
819 if (pvr->layer == V4L2_MPEG_AUDIO_ENCODING_LAYER_1)
821 switch (pvr_param_audio_bitrate)
823 case 32:
824 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_32K;
825 break;
826 case 64:
827 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_64K;
828 break;
829 case 96:
830 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_96K;
831 break;
832 case 128:
833 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_128K;
834 break;
835 case 160:
836 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_160K;
837 break;
838 case 192:
839 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_192K;
840 break;
841 case 224:
842 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_224K;
843 break;
844 case 256:
845 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_256K;
846 break;
847 case 288:
848 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_288K;
849 break;
850 case 320:
851 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_320K;
852 break;
853 case 352:
854 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_352K;
855 break;
856 case 384:
857 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_384K;
858 break;
859 case 416:
860 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_416K;
861 break;
862 case 448:
863 pvr->audio_rate = V4L2_MPEG_AUDIO_L1_BITRATE_448K;
864 break;
865 default:
866 break;
870 else if (pvr->layer == V4L2_MPEG_AUDIO_ENCODING_LAYER_2)
872 switch (pvr_param_audio_bitrate)
874 case 32:
875 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_32K;
876 break;
877 case 48:
878 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_48K;
879 break;
880 case 56:
881 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_56K;
882 break;
883 case 64:
884 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_64K;
885 break;
886 case 80:
887 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_80K;
888 break;
889 case 96:
890 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_96K;
891 break;
892 case 112:
893 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_112K;
894 break;
895 case 128:
896 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_128K;
897 break;
898 case 160:
899 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_160K;
900 break;
901 case 192:
902 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_192K;
903 break;
904 case 224:
905 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_224K;
906 break;
907 case 256:
908 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_256K;
909 break;
910 case 320:
911 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_320K;
912 break;
913 case 384:
914 pvr->audio_rate = V4L2_MPEG_AUDIO_L2_BITRATE_384K;
915 break;
916 default:
917 break;
921 else if (pvr->layer == V4L2_MPEG_AUDIO_ENCODING_LAYER_3)
923 switch (pvr_param_audio_bitrate)
925 case 32:
926 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_32K;
927 break;
928 case 40:
929 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_40K;
930 break;
931 case 48:
932 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_48K;
933 break;
934 case 56:
935 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_56K;
936 break;
937 case 64:
938 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_64K;
939 break;
940 case 80:
941 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_80K;
942 break;
943 case 96:
944 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_96K;
945 break;
946 case 112:
947 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_112K;
948 break;
949 case 128:
950 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_128K;
951 break;
952 case 160:
953 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_160K;
954 break;
955 case 192:
956 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_192K;
957 break;
958 case 224:
959 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_224K;
960 break;
961 case 256:
962 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_256K;
963 break;
964 case 320:
965 pvr->audio_rate = V4L2_MPEG_AUDIO_L3_BITRATE_320K;
966 break;
967 default:
968 break;
973 /* -pvr amode=x */
974 if (pvr_param_audio_mode)
976 if (!strcmp (pvr_param_audio_mode, PVR_AUDIO_MODE_ARG_STEREO))
977 pvr->audio_mode = V4L2_MPEG_AUDIO_MODE_STEREO;
978 else if (!strcmp (pvr_param_audio_mode, PVR_AUDIO_MODE_ARG_JOINT_STEREO))
979 pvr->audio_mode = V4L2_MPEG_AUDIO_MODE_JOINT_STEREO;
980 else if (!strcmp (pvr_param_audio_mode, PVR_AUDIO_MODE_ARG_DUAL))
981 pvr->audio_mode = V4L2_MPEG_AUDIO_MODE_DUAL;
982 else if (!strcmp (pvr_param_audio_mode, PVR_AUDIO_MODE_ARG_MONO))
983 pvr->audio_mode = V4L2_MPEG_AUDIO_MODE_MONO;
986 /* -pvr vbitrate=x */
987 if (pvr_param_bitrate)
988 pvr->bitrate = pvr_param_bitrate;
990 /* -pvr vmode=x */
991 if (pvr_param_bitrate_mode)
993 if (!strcmp (pvr_param_bitrate_mode, PVR_VIDEO_BITRATE_MODE_ARG_VBR))
994 pvr->bitrate_mode = V4L2_MPEG_VIDEO_BITRATE_MODE_VBR;
995 else if (!strcmp (pvr_param_bitrate_mode, PVR_VIDEO_BITRATE_MODE_ARG_CBR))
996 pvr->bitrate_mode = V4L2_MPEG_VIDEO_BITRATE_MODE_CBR;
999 /* -pvr vpeak=x */
1000 if (pvr_param_bitrate_peak)
1001 pvr->bitrate_peak = pvr_param_bitrate_peak;
1003 /* -pvr fmt=x */
1004 if (pvr_param_stream_type)
1006 if (!strcmp (pvr_param_stream_type, PVR_VIDEO_STREAM_TYPE_PS))
1007 pvr->stream_type = V4L2_MPEG_STREAM_TYPE_MPEG2_PS;
1008 else if (!strcmp (pvr_param_stream_type, PVR_VIDEO_STREAM_TYPE_TS))
1009 pvr->stream_type = V4L2_MPEG_STREAM_TYPE_MPEG2_TS;
1010 else if (!strcmp (pvr_param_stream_type, PVR_VIDEO_STREAM_TYPE_MPEG1))
1011 pvr->stream_type = V4L2_MPEG_STREAM_TYPE_MPEG1_SS;
1012 else if (!strcmp (pvr_param_stream_type, PVR_VIDEO_STREAM_TYPE_DVD))
1013 pvr->stream_type = V4L2_MPEG_STREAM_TYPE_MPEG2_DVD;
1014 else if (!strcmp (pvr_param_stream_type, PVR_VIDEO_STREAM_TYPE_VCD))
1015 pvr->stream_type = V4L2_MPEG_STREAM_TYPE_MPEG1_VCD;
1016 else if (!strcmp (pvr_param_stream_type, PVR_VIDEO_STREAM_TYPE_SVCD))
1017 pvr->stream_type = V4L2_MPEG_STREAM_TYPE_MPEG2_SVCD;
1021 static void
1022 add_v4l2_ext_control (struct v4l2_ext_control *ctrl,
1023 uint32_t id, int32_t value)
1025 ctrl->id = id;
1026 ctrl->value = value;
1029 static int
1030 set_encoder_settings (struct pvr_t *pvr)
1032 struct v4l2_ext_control *ext_ctrl = NULL;
1033 struct v4l2_ext_controls ctrls;
1034 uint32_t count = 0;
1036 if (!pvr)
1037 return -1;
1039 if (pvr->dev_fd < 0)
1040 return -1;
1042 ext_ctrl = (struct v4l2_ext_control *)
1043 malloc (PVR_MAX_CONTROLS * sizeof (struct v4l2_ext_control));
1045 add_v4l2_ext_control (&ext_ctrl[count++], V4L2_CID_MPEG_VIDEO_ASPECT,
1046 pvr->aspect);
1048 add_v4l2_ext_control (&ext_ctrl[count++], V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
1049 pvr->samplerate);
1051 add_v4l2_ext_control (&ext_ctrl[count++], V4L2_CID_MPEG_AUDIO_ENCODING,
1052 pvr->layer);
1054 switch (pvr->layer)
1056 case V4L2_MPEG_AUDIO_ENCODING_LAYER_1:
1057 add_v4l2_ext_control (&ext_ctrl[count++], V4L2_CID_MPEG_AUDIO_L1_BITRATE,
1058 pvr->audio_rate);
1059 break;
1060 case V4L2_MPEG_AUDIO_ENCODING_LAYER_2:
1061 add_v4l2_ext_control (&ext_ctrl[count++], V4L2_CID_MPEG_AUDIO_L2_BITRATE,
1062 pvr->audio_rate);
1063 break;
1064 case V4L2_MPEG_AUDIO_ENCODING_LAYER_3:
1065 add_v4l2_ext_control (&ext_ctrl[count++], V4L2_CID_MPEG_AUDIO_L3_BITRATE,
1066 pvr->audio_rate);
1067 break;
1068 default:
1069 break;
1072 add_v4l2_ext_control (&ext_ctrl[count++], V4L2_CID_MPEG_AUDIO_MODE,
1073 pvr->audio_mode);
1075 add_v4l2_ext_control (&ext_ctrl[count++], V4L2_CID_MPEG_VIDEO_BITRATE,
1076 pvr->bitrate);
1078 add_v4l2_ext_control (&ext_ctrl[count++], V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
1079 pvr->bitrate_peak);
1081 add_v4l2_ext_control (&ext_ctrl[count++], V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
1082 pvr->bitrate_mode);
1084 add_v4l2_ext_control (&ext_ctrl[count++], V4L2_CID_MPEG_STREAM_TYPE,
1085 pvr->stream_type);
1087 /* set new encoding settings */
1088 ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
1089 ctrls.count = count;
1090 ctrls.controls = ext_ctrl;
1092 if (ioctl (pvr->dev_fd, VIDIOC_S_EXT_CTRLS, &ctrls) < 0)
1094 mp_msg (MSGT_OPEN, MSGL_ERR, "%s Error setting MPEG controls (%s).\n",
1095 LOG_LEVEL_ENCODER, strerror (errno));
1096 free (ext_ctrl);
1097 return -1;
1100 free (ext_ctrl);
1102 return 0;
1105 static void
1106 parse_v4l2_tv_options (struct pvr_t *pvr)
1108 if (!pvr)
1109 return;
1111 /* Create our station/channel list */
1112 parse_setup_stationlist (pvr);
1114 if (pvr->param_channel)
1116 if (set_station_by_channelname_or_freq (pvr, pvr->param_channel,
1117 -1, 0) >= 0)
1119 if (stream_tv_defaults.freq)
1121 mp_msg (MSGT_OPEN, MSGL_HINT,
1122 "%s tv param freq %s is overwritten by channel setting freq %d\n", LOG_LEVEL_V4L2,
1123 stream_tv_defaults.freq, pvr->freq);
1128 if (pvr->freq < 0 && stream_tv_defaults.freq)
1130 mp_msg (MSGT_OPEN, MSGL_HINT, "%s tv param freq %s is used directly\n",
1131 LOG_LEVEL_V4L2, stream_tv_defaults.freq);
1133 if (set_station_by_channelname_or_freq (pvr, NULL,
1134 atoi (stream_tv_defaults.freq), 0)<0)
1136 mp_msg (MSGT_OPEN, MSGL_WARN,
1137 "%s tv param freq %s invalid to set station\n",
1138 LOG_LEVEL_V4L2, stream_tv_defaults.freq);
1142 if (stream_tv_defaults.device)
1144 free (pvr->video_dev);
1145 pvr->video_dev = strdup (stream_tv_defaults.device);
1148 if (stream_tv_defaults.noaudio)
1149 pvr->mute = stream_tv_defaults.noaudio;
1151 if (stream_tv_defaults.input)
1152 pvr->input = stream_tv_defaults.input;
1154 if (stream_tv_defaults.normid)
1155 pvr->normid = stream_tv_defaults.normid;
1157 if (stream_tv_defaults.brightness)
1158 pvr->brightness = stream_tv_defaults.brightness;
1160 if (stream_tv_defaults.contrast)
1161 pvr->contrast = stream_tv_defaults.contrast;
1163 if (stream_tv_defaults.hue)
1164 pvr->hue = stream_tv_defaults.hue;
1166 if (stream_tv_defaults.saturation)
1167 pvr->saturation = stream_tv_defaults.saturation;
1169 if (stream_tv_defaults.width)
1170 pvr->width = stream_tv_defaults.width;
1172 if (stream_tv_defaults.height)
1173 pvr->height = stream_tv_defaults.height;
1176 static int
1177 set_v4l2_settings (struct pvr_t *pvr)
1179 if (!pvr)
1180 return -1;
1182 if (pvr->dev_fd < 0)
1183 return -1;
1185 /* -tv noaudio */
1186 if (pvr->mute)
1188 struct v4l2_control ctrl;
1189 ctrl.id = V4L2_CID_AUDIO_MUTE;
1190 ctrl.value = 1;
1191 if (ioctl (pvr->dev_fd, VIDIOC_S_CTRL, &ctrl) < 0)
1193 mp_msg (MSGT_OPEN, MSGL_ERR,
1194 "%s can't mute (%s).\n", LOG_LEVEL_V4L2, strerror (errno));
1195 return -1;
1199 /* -tv input=x */
1200 if (pvr->input != 0)
1202 if (ioctl (pvr->dev_fd, VIDIOC_S_INPUT, &pvr->input) < 0)
1204 mp_msg (MSGT_OPEN, MSGL_ERR,
1205 "%s can't set input (%s)\n", LOG_LEVEL_V4L2, strerror (errno));
1206 return -1;
1210 /* -tv normid=x */
1211 if (pvr->normid != -1)
1213 struct v4l2_standard std;
1214 std.index = pvr->normid;
1216 if (ioctl (pvr->dev_fd, VIDIOC_ENUMSTD, &std) < 0)
1218 mp_msg (MSGT_OPEN, MSGL_ERR,
1219 "%s can't set norm (%s)\n", LOG_LEVEL_V4L2, strerror (errno));
1220 return -1;
1223 mp_msg (MSGT_OPEN, MSGL_V,
1224 "%s set norm to %s\n", LOG_LEVEL_V4L2, std.name);
1226 if (ioctl (pvr->dev_fd, VIDIOC_S_STD, &std.id) < 0)
1228 mp_msg (MSGT_OPEN, MSGL_ERR,
1229 "%s can't set norm (%s)\n", LOG_LEVEL_V4L2, strerror (errno));
1230 return -1;
1234 /* -tv brightness=x */
1235 if (pvr->brightness != 0)
1237 struct v4l2_control ctrl;
1238 ctrl.id = V4L2_CID_BRIGHTNESS;
1239 ctrl.value = pvr->brightness;
1241 if (ctrl.value < 0)
1242 ctrl.value = 0;
1243 if (ctrl.value > 255)
1244 ctrl.value = 255;
1246 if (ioctl (pvr->dev_fd, VIDIOC_S_CTRL, &ctrl) < 0)
1248 mp_msg (MSGT_OPEN, MSGL_ERR,
1249 "%s can't set brightness to %d (%s).\n",
1250 LOG_LEVEL_V4L2, ctrl.value, strerror (errno));
1251 return -1;
1255 /* -tv contrast=x */
1256 if (pvr->contrast != 0)
1258 struct v4l2_control ctrl;
1259 ctrl.id = V4L2_CID_CONTRAST;
1260 ctrl.value = pvr->contrast;
1262 if (ctrl.value < 0)
1263 ctrl.value = 0;
1264 if (ctrl.value > 127)
1265 ctrl.value = 127;
1267 if (ioctl (pvr->dev_fd, VIDIOC_S_CTRL, &ctrl) < 0)
1269 mp_msg (MSGT_OPEN, MSGL_ERR,
1270 "%s can't set contrast to %d (%s).\n",
1271 LOG_LEVEL_V4L2, ctrl.value, strerror (errno));
1272 return -1;
1276 /* -tv hue=x */
1277 if (pvr->hue != 0)
1279 struct v4l2_control ctrl;
1280 ctrl.id = V4L2_CID_HUE;
1281 ctrl.value = pvr->hue;
1283 if (ctrl.value < -128)
1284 ctrl.value = -128;
1285 if (ctrl.value > 127)
1286 ctrl.value = 127;
1288 if (ioctl (pvr->dev_fd, VIDIOC_S_CTRL, &ctrl) < 0)
1290 mp_msg (MSGT_OPEN, MSGL_ERR,
1291 "%s can't set hue to %d (%s).\n",
1292 LOG_LEVEL_V4L2, ctrl.value, strerror (errno));
1293 return -1;
1297 /* -tv saturation=x */
1298 if (pvr->saturation != 0)
1300 struct v4l2_control ctrl;
1301 ctrl.id = V4L2_CID_SATURATION;
1302 ctrl.value = pvr->saturation;
1304 if (ctrl.value < 0)
1305 ctrl.value = 0;
1306 if (ctrl.value > 127)
1307 ctrl.value = 127;
1309 if (ioctl (pvr->dev_fd, VIDIOC_S_CTRL, &ctrl) < 0)
1311 mp_msg (MSGT_OPEN, MSGL_ERR,
1312 "%s can't set saturation to %d (%s).\n",
1313 LOG_LEVEL_V4L2, ctrl.value, strerror (errno));
1314 return -1;
1318 /* -tv width=x:height=y */
1319 if (pvr->width && pvr->height)
1321 struct v4l2_format vfmt;
1322 vfmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1323 vfmt.fmt.pix.width = pvr->width;
1324 vfmt.fmt.pix.height = pvr->height;
1326 if (ioctl (pvr->dev_fd, VIDIOC_S_FMT, &vfmt) < 0)
1328 mp_msg (MSGT_OPEN, MSGL_ERR,
1329 "%s can't set resolution to %dx%d (%s).\n",
1330 LOG_LEVEL_V4L2, pvr->width, pvr->height, strerror (errno));
1331 return -1;
1335 if (pvr->freq < 0)
1337 int freq = get_v4l2_freq (pvr);
1338 mp_msg (MSGT_OPEN, MSGL_INFO,
1339 "%s Using current set frequency %d, to set channel\n",
1340 LOG_LEVEL_V4L2, freq);
1342 if (0 < freq)
1343 return set_station_by_channelname_or_freq (pvr, NULL, freq, 1);
1346 if (0 < pvr->freq)
1347 return set_v4l2_freq (pvr) ;
1349 return 0;
1352 static int
1353 v4l2_list_capabilities (struct pvr_t *pvr)
1355 struct v4l2_audio vaudio;
1356 struct v4l2_standard vs;
1357 struct v4l2_input vin;
1358 int err = 0;
1360 if (!pvr)
1361 return -1;
1363 if (pvr->dev_fd < 0)
1364 return -1;
1366 /* list available video inputs */
1367 vin.index = 0;
1368 err = 1;
1369 mp_msg (MSGT_OPEN, MSGL_INFO,
1370 "%s Available video inputs: ", LOG_LEVEL_V4L2);
1371 while (ioctl (pvr->dev_fd, VIDIOC_ENUMINPUT, &vin) >= 0)
1373 err = 0;
1374 mp_msg (MSGT_OPEN, MSGL_INFO, "'#%d, %s' ", vin.index, vin.name);
1375 vin.index++;
1377 if (err)
1379 mp_msg (MSGT_OPEN, MSGL_INFO, "none\n");
1380 return -1;
1382 else
1383 mp_msg (MSGT_OPEN, MSGL_INFO, "\n");
1385 /* list available audio inputs */
1386 vaudio.index = 0;
1387 err = 1;
1388 mp_msg (MSGT_OPEN, MSGL_INFO,
1389 "%s Available audio inputs: ", LOG_LEVEL_V4L2);
1390 while (ioctl (pvr->dev_fd, VIDIOC_ENUMAUDIO, &vaudio) >= 0)
1392 err = 0;
1393 mp_msg (MSGT_OPEN, MSGL_INFO, "'#%d, %s' ", vaudio.index, vaudio.name);
1394 vaudio.index++;
1396 if (err)
1398 mp_msg (MSGT_OPEN, MSGL_INFO, "none\n");
1399 return -1;
1401 else
1402 mp_msg (MSGT_OPEN, MSGL_INFO, "\n");
1404 /* list available norms */
1405 vs.index = 0;
1406 mp_msg (MSGT_OPEN, MSGL_INFO, "%s Available norms: ", LOG_LEVEL_V4L2);
1407 while (ioctl (pvr->dev_fd, VIDIOC_ENUMSTD, &vs) >= 0)
1409 err = 0;
1410 mp_msg (MSGT_OPEN, MSGL_INFO, "'#%d, %s' ", vs.index, vs.name);
1411 vs.index++;
1413 if (err)
1415 mp_msg (MSGT_OPEN, MSGL_INFO, "none\n");
1416 return -1;
1418 else
1419 mp_msg (MSGT_OPEN, MSGL_INFO, "\n");
1421 return 0;
1424 static int
1425 v4l2_display_settings (struct pvr_t *pvr)
1427 struct v4l2_audio vaudio;
1428 struct v4l2_standard vs;
1429 struct v4l2_input vin;
1430 v4l2_std_id std;
1431 int input;
1433 if (!pvr)
1434 return -1;
1436 if (pvr->dev_fd < 0)
1437 return -1;
1439 /* get current video input */
1440 if (ioctl (pvr->dev_fd, VIDIOC_G_INPUT, &input) == 0)
1442 vin.index = input;
1443 if (ioctl (pvr->dev_fd, VIDIOC_ENUMINPUT, &vin) < 0)
1445 mp_msg (MSGT_OPEN, MSGL_ERR,
1446 "%s can't get input (%s).\n", LOG_LEVEL_V4L2, strerror (errno));
1447 return -1;
1449 else
1450 mp_msg (MSGT_OPEN, MSGL_INFO,
1451 "%s Video input: %s\n", LOG_LEVEL_V4L2, vin.name);
1453 else
1455 mp_msg (MSGT_OPEN, MSGL_ERR,
1456 "%s can't get input (%s).\n", LOG_LEVEL_V4L2, strerror (errno));
1457 return -1;
1460 /* get current audio input */
1461 if (ioctl (pvr->dev_fd, VIDIOC_G_AUDIO, &vaudio) == 0)
1463 mp_msg (MSGT_OPEN, MSGL_INFO,
1464 "%s Audio input: %s\n", LOG_LEVEL_V4L2, vaudio.name);
1466 else
1468 mp_msg (MSGT_OPEN, MSGL_ERR,
1469 "%s can't get input (%s).\n", LOG_LEVEL_V4L2, strerror (errno));
1470 return -1;
1473 /* get current video format */
1474 if (ioctl (pvr->dev_fd, VIDIOC_G_STD, &std) == 0)
1476 vs.index = 0;
1478 while (ioctl (pvr->dev_fd, VIDIOC_ENUMSTD, &vs) >= 0)
1480 if (vs.id == std)
1482 mp_msg (MSGT_OPEN, MSGL_INFO,
1483 "%s Norm: %s.\n", LOG_LEVEL_V4L2, vs.name);
1484 break;
1486 vs.index++;
1489 else
1491 mp_msg (MSGT_OPEN, MSGL_ERR,
1492 "%s can't get norm (%s)\n", LOG_LEVEL_V4L2, strerror (errno));
1493 return -1;
1496 return 0;
1499 /* stream layer */
1501 static void
1502 pvr_stream_close (stream_t *stream)
1504 struct pvr_t *pvr;
1506 if (!stream)
1507 return;
1509 pvr = (struct pvr_t *) stream->priv;
1510 pvr_uninit (pvr);
1513 static int
1514 pvr_stream_read (stream_t *stream, char *buffer, int size)
1516 struct pollfd pfds[1];
1517 struct pvr_t *pvr;
1518 int rk, fd, pos;
1520 if (!stream || !buffer)
1521 return 0;
1523 pvr = (struct pvr_t *) stream->priv;
1524 fd = pvr->dev_fd;
1525 pos = 0;
1527 if (fd < 0)
1528 return 0;
1530 while (pos < size)
1532 pfds[0].fd = fd;
1533 pfds[0].events = POLLIN | POLLPRI;
1535 rk = size - pos;
1537 if (poll (pfds, 1, 500) <= 0)
1539 mp_msg (MSGT_OPEN, MSGL_ERR,
1540 "%s failed with errno %d when reading %d bytes\n",
1541 LOG_LEVEL_PVR, errno, size-pos);
1542 break;
1545 rk = read (fd, &buffer[pos], rk);
1546 if (rk > 0)
1548 pos += rk;
1549 mp_msg (MSGT_OPEN, MSGL_DBG3,
1550 "%s read (%d) bytes\n", LOG_LEVEL_PVR, pos);
1554 if (!pos)
1555 mp_msg (MSGT_OPEN, MSGL_ERR, "%s read %d bytes\n", LOG_LEVEL_PVR, pos);
1557 return pos;
1560 static int
1561 pvr_stream_open (stream_t *stream, int mode, void *opts, int *file_format)
1563 struct v4l2_capability vcap;
1564 struct v4l2_ext_controls ctrls;
1565 struct pvr_t *pvr = NULL;
1567 if (mode != STREAM_READ)
1568 return STREAM_UNSUPPORTED;
1570 pvr = pvr_init ();
1573 * if the url, i.e. 'pvr://8', contains the channel, use it,
1574 * else use the tv parameter.
1576 if (stream->url && strlen (stream->url) > 6 && stream->url[6] != '\0')
1577 pvr->param_channel = strdup (stream->url + 6);
1578 else if (stream_tv_defaults.channel && strlen (stream_tv_defaults.channel))
1579 pvr->param_channel = strdup (stream_tv_defaults.channel);
1581 parse_v4l2_tv_options (pvr);
1582 parse_encoder_options (pvr);
1584 /* open device */
1585 pvr->dev_fd = open (pvr->video_dev, O_RDWR);
1586 mp_msg (MSGT_OPEN, MSGL_INFO,
1587 "%s Using device %s\n", LOG_LEVEL_PVR, pvr->video_dev);
1588 if (pvr->dev_fd == -1)
1590 mp_msg (MSGT_OPEN, MSGL_ERR,
1591 "%s error opening device %s\n", LOG_LEVEL_PVR, pvr->video_dev);
1592 pvr_uninit (pvr);
1593 return STREAM_ERROR;
1596 /* query capabilities (i.e test V4L2 support) */
1597 if (ioctl (pvr->dev_fd, VIDIOC_QUERYCAP, &vcap) < 0)
1599 mp_msg (MSGT_OPEN, MSGL_ERR,
1600 "%s device is not V4L2 compliant (%s).\n",
1601 LOG_LEVEL_PVR, strerror (errno));
1602 pvr_uninit (pvr);
1603 return STREAM_ERROR;
1605 else
1606 mp_msg (MSGT_OPEN, MSGL_INFO,
1607 "%s Detected %s\n", LOG_LEVEL_PVR, vcap.card);
1609 /* check for a valid V4L2 capture device */
1610 if (!(vcap.capabilities & V4L2_CAP_VIDEO_CAPTURE))
1612 mp_msg (MSGT_OPEN, MSGL_ERR,
1613 "%s device is not a valid V4L2 capture device.\n",
1614 LOG_LEVEL_PVR);
1615 pvr_uninit (pvr);
1616 return STREAM_ERROR;
1619 /* check for device hardware MPEG encoding capability */
1620 ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
1621 ctrls.count = 0;
1622 ctrls.controls = NULL;
1624 if (ioctl (pvr->dev_fd, VIDIOC_G_EXT_CTRLS, &ctrls) < 0)
1626 mp_msg (MSGT_OPEN, MSGL_ERR,
1627 "%s device do not support MPEG input.\n", LOG_LEVEL_ENCODER);
1628 return STREAM_ERROR;
1631 /* list V4L2 capabilities */
1632 if (v4l2_list_capabilities (pvr) == -1)
1634 mp_msg (MSGT_OPEN, MSGL_ERR,
1635 "%s can't get v4l2 capabilities\n", LOG_LEVEL_PVR);
1636 pvr_uninit (pvr);
1637 return STREAM_ERROR;
1640 /* apply V4L2 settings */
1641 if (set_v4l2_settings (pvr) == -1)
1643 mp_msg (MSGT_OPEN, MSGL_ERR,
1644 "%s can't set v4l2 settings\n", LOG_LEVEL_PVR);
1645 pvr_uninit (pvr);
1646 return STREAM_ERROR;
1649 /* apply encoder settings */
1650 if (set_encoder_settings (pvr) == -1)
1652 mp_msg (MSGT_OPEN, MSGL_ERR,
1653 "%s can't set encoder settings\n", LOG_LEVEL_PVR);
1654 pvr_uninit (pvr);
1655 return STREAM_ERROR;
1658 /* display current V4L2 settings */
1659 if (v4l2_display_settings (pvr) == -1)
1661 mp_msg (MSGT_OPEN, MSGL_ERR,
1662 "%s can't get v4l2 settings\n", LOG_LEVEL_PVR);
1663 pvr_uninit (pvr);
1664 return STREAM_ERROR;
1667 stream->priv = pvr;
1668 stream->type = STREAMTYPE_PVR;
1669 stream->fill_buffer = pvr_stream_read;
1670 stream->close = pvr_stream_close;
1672 return STREAM_OK;
1675 /* PVR Public API access */
1677 const char *
1678 pvr_get_current_stationname (stream_t *stream)
1680 struct pvr_t *pvr;
1682 if (!stream || stream->type != STREAMTYPE_PVR)
1683 return NULL;
1685 pvr = (struct pvr_t *) stream->priv;
1687 if (pvr->stationlist.list &&
1688 pvr->stationlist.used > pvr->chan_idx &&
1689 pvr->chan_idx >= 0)
1690 return pvr->stationlist.list[pvr->chan_idx].station;
1692 return NULL;
1695 const char *
1696 pvr_get_current_channelname (stream_t *stream)
1698 struct pvr_t *pvr = (struct pvr_t *) stream->priv;
1700 if (pvr->stationlist.list &&
1701 pvr->stationlist.used > pvr->chan_idx &&
1702 pvr->chan_idx >= 0)
1703 return pvr->stationlist.list[pvr->chan_idx].name;
1705 return NULL;
1709 pvr_get_current_frequency (stream_t *stream)
1711 struct pvr_t *pvr = (struct pvr_t *) stream->priv;
1713 return pvr->freq;
1717 pvr_set_channel (stream_t *stream, const char * channel)
1719 struct pvr_t *pvr = (struct pvr_t *) stream->priv;
1721 return set_station_by_channelname_or_freq (pvr, channel, -1, 1);
1725 pvr_set_lastchannel (stream_t *stream)
1727 struct pvr_t *pvr = (struct pvr_t *) stream->priv;
1729 if (pvr->stationlist.list &&
1730 pvr->stationlist.used > pvr->chan_idx_last &&
1731 pvr->chan_idx_last >= 0)
1732 return set_station_by_channelname_or_freq (pvr, pvr->stationlist.list[pvr->chan_idx_last].name, -1, 1);
1734 return -1;
1738 pvr_set_freq (stream_t *stream, int freq)
1740 struct pvr_t *pvr = (struct pvr_t *) stream->priv;
1742 return set_station_by_channelname_or_freq (pvr, NULL, freq, 1);
1746 pvr_set_channel_step (stream_t *stream, int step)
1748 struct pvr_t *pvr = (struct pvr_t *) stream->priv;
1750 return set_station_by_step (pvr, step, 1);
1754 pvr_force_freq_step (stream_t *stream, int step)
1756 struct pvr_t *pvr = (struct pvr_t *) stream->priv;
1758 return force_freq_step (pvr, step);
1761 const stream_info_t stream_info_pvr = {
1762 "V4L2 MPEG Input (a.k.a PVR)",
1763 "pvr",
1764 "Benjamin Zores",
1766 pvr_stream_open,
1767 { "pvr", NULL },
1768 NULL,