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
33 #include <sys/ioctl.h>
34 #include <sys/fcntl.h>
37 #include <linux/types.h>
38 #include <linux/videodev2.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
{
92 char station
[PVR_STATION_NAME_SIZE
];
96 typedef struct stationlist_s
{
97 char name
[PVR_STATION_NAME_SIZE
];
99 int total
; /* total number */
100 int used
; /* used number */
101 int enabled
; /* enabled number */
121 stationlist_t stationlist
;
122 /* dups the tv_param_channel, or the url's channel param */
137 static struct pvr_t
*
140 struct pvr_t
*pvr
= NULL
;
142 pvr
= calloc (1, sizeof (struct pvr_t
));
144 pvr
->video_dev
= strdup (PVR_DEFAULT_DEVICE
);
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
;
179 pvr_uninit (struct pvr_t
*pvr
)
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
);
201 * @brief Copy Constructor for stationlist
203 * @see parse_setup_stationlist
206 copycreate_stationlist (stationlist_t
*stationlist
, int num
)
210 if (chantab
< 0 || !stationlist
)
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",
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
++;
253 print_all_stations (struct pvr_t
*pvr
)
257 if (!pvr
|| !pvr
->stationlist
.list
)
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
);
273 * Disables all stations
275 * @see parse_setup_stationlist
278 disable_all_stations (struct pvr_t
*pvr
)
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
293 set_station (struct pvr_t
*pvr
, const char *station
,
294 const char *channel
, int freq
)
298 if (!pvr
|| !pvr
->stationlist
.list
)
301 if (0 >= pvr
->stationlist
.total
|| (!channel
&& !freq
))
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
++;
328 av_strlcpy (pvr
->stationlist
.list
[i
].station
,
329 station
, PVR_STATION_NAME_SIZE
);
331 av_strlcpy (pvr
->stationlist
.list
[i
].station
,
332 channel
, PVR_STATION_NAME_SIZE
);
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
);
345 /* from here on, we have to create a new entry, frequency is mandatory */
348 mp_msg (MSGT_OPEN
, MSGL_ERR
,
349 "%s Cannot add new station/channel without frequency\n",
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",
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
++;
385 av_strlcpy (pvr
->stationlist
.list
[i
].station
,
386 station
, PVR_STATION_NAME_SIZE
);
388 av_strlcpy (pvr
->stationlist
.list
[i
].name
, channel
, PVR_STATION_NAME_SIZE
);
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
);
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
414 parse_setup_stationlist (struct pvr_t
*pvr
)
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
))
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
);
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
);
449 mp_msg (MSGT_OPEN
, MSGL_FATAL
,
450 "%s No channel list selected, giving up\n", LOG_LEVEL_V4L2
);
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",
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
);
473 char *tmp
= *(channels
++);
474 char *sep
= strchr (tmp
, '-');
478 continue; /* Wrong syntax, but mplayer should not crash */
480 av_strlcpy (station
, sep
+ 1, PVR_STATION_NAME_SIZE
);
483 av_strlcpy (channel
, tmp
, PVR_STATION_NAME_SIZE
);
485 while ((sep
= strchr (station
, '_')))
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)
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
);
508 get_v4l2_freq (struct pvr_t
*pvr
)
511 struct v4l2_frequency vf
;
512 struct v4l2_tuner vt
;
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
));
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
);
537 if (!(vt
.capability
& V4L2_TUNER_CAP_LOW
))
545 set_v4l2_freq (struct pvr_t
*pvr
)
547 struct v4l2_frequency vf
;
548 struct v4l2_tuner vt
;
555 mp_msg (MSGT_OPEN
, MSGL_ERR
,
556 "%s Frequency invalid %d !!!\n", LOG_LEVEL_V4L2
, pvr
->freq
);
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
);
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
));
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
));
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
));
604 /* just a notification */
606 mp_msg (MSGT_OPEN
, MSGL_ERR
, "%s NO SIGNAL at frequency %d (%d)\n",
607 LOG_LEVEL_V4L2
, pvr
->freq
, vf
.frequency
);
609 mp_msg (MSGT_OPEN
, MSGL_STATUS
, "%s Got signal at frequency %d (%d)\n",
610 LOG_LEVEL_V4L2
, pvr
->freq
, vf
.frequency
);
616 set_station_by_step (struct pvr_t
*pvr
, int step
, int v4lAction
)
618 if (!pvr
|| !pvr
->stationlist
.list
)
621 if (pvr
->stationlist
.enabled
>= abs (step
))
624 int chidx
= pvr
->chan_idx
+ step
;
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
);
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
);
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
);
672 set_station_by_channelname_or_freq (struct pvr_t
*pvr
, const char *channel
,
673 int freq
, int v4lAction
)
677 if (!pvr
|| !pvr
->stationlist
.list
)
680 if (0 >= pvr
->stationlist
.enabled
)
682 mp_msg (MSGT_OPEN
, MSGL_WARN
,
683 "%s No enabled station, cannot switch channel/frequency\n",
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
);
706 pvr
->freq
= pvr
->stationlist
.list
[i
].freq
;
707 pvr
->chan_idx_last
= pvr
->chan_idx
;
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
);
731 pvr
->freq
= pvr
->stationlist
.list
[i
].freq
;
732 pvr
->chan_idx_last
= pvr
->chan_idx
;
739 if (i
>= pvr
->stationlist
.used
)
742 mp_msg (MSGT_OPEN
, MSGL_WARN
,
743 "%s unable to find channel %s\n", LOG_LEVEL_V4L2
, channel
);
745 mp_msg (MSGT_OPEN
, MSGL_WARN
,
746 "%s unable to find frequency %d\n", LOG_LEVEL_V4L2
, freq
);
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
);
757 return set_v4l2_freq (pvr
);
759 return (pvr
->freq
> 0) ? 0 : -1;
763 force_freq_step (struct pvr_t
*pvr
, int step
)
770 freq
= pvr
->freq
+step
;
774 mp_msg (MSGT_OPEN
, MSGL_INFO
,
775 "%s Force Frequency %d + %d = %d \n", LOG_LEVEL_V4L2
,
776 pvr
->freq
, step
, freq
);
780 return set_v4l2_freq (pvr
);
787 parse_encoder_options (struct pvr_t
*pvr
)
792 /* -pvr aspect=digit */
793 if (pvr_param_aspect_ratio
>= 0 && pvr_param_aspect_ratio
<= 3)
794 pvr
->aspect
= pvr_param_aspect_ratio
;
797 if (pvr_param_sample_rate
!= 0)
799 switch (pvr_param_sample_rate
)
802 pvr
->samplerate
= V4L2_MPEG_AUDIO_SAMPLING_FREQ_32000
;
805 pvr
->samplerate
= V4L2_MPEG_AUDIO_SAMPLING_FREQ_44100
;
808 pvr
->samplerate
= V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000
;
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
)
831 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L1_BITRATE_32K
;
834 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L1_BITRATE_64K
;
837 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L1_BITRATE_96K
;
840 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L1_BITRATE_128K
;
843 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L1_BITRATE_160K
;
846 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L1_BITRATE_192K
;
849 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L1_BITRATE_224K
;
852 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L1_BITRATE_256K
;
855 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L1_BITRATE_288K
;
858 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L1_BITRATE_320K
;
861 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L1_BITRATE_352K
;
864 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L1_BITRATE_384K
;
867 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L1_BITRATE_416K
;
870 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L1_BITRATE_448K
;
877 else if (pvr
->layer
== V4L2_MPEG_AUDIO_ENCODING_LAYER_2
)
879 switch (pvr_param_audio_bitrate
)
882 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L2_BITRATE_32K
;
885 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L2_BITRATE_48K
;
888 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L2_BITRATE_56K
;
891 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L2_BITRATE_64K
;
894 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L2_BITRATE_80K
;
897 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L2_BITRATE_96K
;
900 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L2_BITRATE_112K
;
903 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L2_BITRATE_128K
;
906 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L2_BITRATE_160K
;
909 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L2_BITRATE_192K
;
912 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L2_BITRATE_224K
;
915 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L2_BITRATE_256K
;
918 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L2_BITRATE_320K
;
921 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L2_BITRATE_384K
;
928 else if (pvr
->layer
== V4L2_MPEG_AUDIO_ENCODING_LAYER_3
)
930 switch (pvr_param_audio_bitrate
)
933 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L3_BITRATE_32K
;
936 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L3_BITRATE_40K
;
939 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L3_BITRATE_48K
;
942 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L3_BITRATE_56K
;
945 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L3_BITRATE_64K
;
948 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L3_BITRATE_80K
;
951 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L3_BITRATE_96K
;
954 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L3_BITRATE_112K
;
957 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L3_BITRATE_128K
;
960 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L3_BITRATE_160K
;
963 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L3_BITRATE_192K
;
966 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L3_BITRATE_224K
;
969 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L3_BITRATE_256K
;
972 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L3_BITRATE_320K
;
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
;
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
;
1007 if (pvr_param_bitrate_peak
)
1008 pvr
->bitrate_peak
= pvr_param_bitrate_peak
;
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
;
1029 add_v4l2_ext_control (struct v4l2_ext_control
*ctrl
,
1030 uint32_t id
, int32_t value
)
1033 ctrl
->value
= value
;
1037 set_encoder_settings (struct pvr_t
*pvr
)
1039 struct v4l2_ext_control
*ext_ctrl
= NULL
;
1040 struct v4l2_ext_controls ctrls
;
1046 if (pvr
->dev_fd
< 0)
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
,
1055 add_v4l2_ext_control (&ext_ctrl
[count
++], V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ
,
1058 add_v4l2_ext_control (&ext_ctrl
[count
++], V4L2_CID_MPEG_AUDIO_ENCODING
,
1063 case V4L2_MPEG_AUDIO_ENCODING_LAYER_1
:
1064 add_v4l2_ext_control (&ext_ctrl
[count
++], V4L2_CID_MPEG_AUDIO_L1_BITRATE
,
1067 case V4L2_MPEG_AUDIO_ENCODING_LAYER_2
:
1068 add_v4l2_ext_control (&ext_ctrl
[count
++], V4L2_CID_MPEG_AUDIO_L2_BITRATE
,
1071 case V4L2_MPEG_AUDIO_ENCODING_LAYER_3
:
1072 add_v4l2_ext_control (&ext_ctrl
[count
++], V4L2_CID_MPEG_AUDIO_L3_BITRATE
,
1079 add_v4l2_ext_control (&ext_ctrl
[count
++], V4L2_CID_MPEG_AUDIO_MODE
,
1082 add_v4l2_ext_control (&ext_ctrl
[count
++], V4L2_CID_MPEG_VIDEO_BITRATE
,
1085 add_v4l2_ext_control (&ext_ctrl
[count
++], V4L2_CID_MPEG_VIDEO_BITRATE_PEAK
,
1088 add_v4l2_ext_control (&ext_ctrl
[count
++], V4L2_CID_MPEG_VIDEO_BITRATE_MODE
,
1091 add_v4l2_ext_control (&ext_ctrl
[count
++], V4L2_CID_MPEG_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
));
1113 parse_v4l2_tv_options (struct pvr_t
*pvr
)
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
,
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
)
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
;
1185 set_v4l2_settings (struct pvr_t
*pvr
)
1190 if (pvr
->dev_fd
< 0)
1196 struct v4l2_control ctrl
;
1197 ctrl
.id
= V4L2_CID_AUDIO_MUTE
;
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
));
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
));
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
));
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
));
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
;
1251 if (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
));
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
;
1272 if (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
));
1287 struct v4l2_control ctrl
;
1288 ctrl
.id
= V4L2_CID_HUE
;
1289 ctrl
.value
= pvr
->hue
;
1291 if (ctrl
.value
< -128)
1293 if (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
));
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
;
1314 if (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
));
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
));
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
);
1351 return set_station_by_channelname_or_freq (pvr
, NULL
, freq
, 1);
1355 return set_v4l2_freq (pvr
) ;
1361 v4l2_list_capabilities (struct pvr_t
*pvr
)
1363 struct v4l2_audio vaudio
;
1364 struct v4l2_standard vs
;
1365 struct v4l2_input vin
;
1371 if (pvr
->dev_fd
< 0)
1374 /* list available video inputs */
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)
1382 mp_msg (MSGT_OPEN
, MSGL_INFO
, "'#%d, %s' ", vin
.index
, vin
.name
);
1387 mp_msg (MSGT_OPEN
, MSGL_INFO
, "none\n");
1391 mp_msg (MSGT_OPEN
, MSGL_INFO
, "\n");
1393 /* list available audio inputs */
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)
1401 mp_msg (MSGT_OPEN
, MSGL_INFO
, "'#%d, %s' ", vaudio
.index
, vaudio
.name
);
1406 mp_msg (MSGT_OPEN
, MSGL_INFO
, "none\n");
1410 mp_msg (MSGT_OPEN
, MSGL_INFO
, "\n");
1412 /* list available norms */
1414 mp_msg (MSGT_OPEN
, MSGL_INFO
, "%s Available norms: ", LOG_LEVEL_V4L2
);
1415 while (ioctl (pvr
->dev_fd
, VIDIOC_ENUMSTD
, &vs
) >= 0)
1418 mp_msg (MSGT_OPEN
, MSGL_INFO
, "'#%d, %s' ", vs
.index
, vs
.name
);
1423 mp_msg (MSGT_OPEN
, MSGL_INFO
, "none\n");
1427 mp_msg (MSGT_OPEN
, MSGL_INFO
, "\n");
1433 v4l2_display_settings (struct pvr_t
*pvr
)
1435 struct v4l2_audio vaudio
;
1436 struct v4l2_standard vs
;
1437 struct v4l2_input vin
;
1444 if (pvr
->dev_fd
< 0)
1447 /* get current video input */
1448 if (ioctl (pvr
->dev_fd
, VIDIOC_G_INPUT
, &input
) == 0)
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
));
1458 mp_msg (MSGT_OPEN
, MSGL_INFO
,
1459 "%s Video input: %s\n", LOG_LEVEL_V4L2
, vin
.name
);
1463 mp_msg (MSGT_OPEN
, MSGL_ERR
,
1464 "%s can't get input (%s).\n", LOG_LEVEL_V4L2
, strerror (errno
));
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
);
1476 mp_msg (MSGT_OPEN
, MSGL_ERR
,
1477 "%s can't get input (%s).\n", LOG_LEVEL_V4L2
, strerror (errno
));
1481 /* get current video format */
1482 if (ioctl (pvr
->dev_fd
, VIDIOC_G_STD
, &std
) == 0)
1486 while (ioctl (pvr
->dev_fd
, VIDIOC_ENUMSTD
, &vs
) >= 0)
1490 mp_msg (MSGT_OPEN
, MSGL_INFO
,
1491 "%s Norm: %s.\n", LOG_LEVEL_V4L2
, vs
.name
);
1499 mp_msg (MSGT_OPEN
, MSGL_ERR
,
1500 "%s can't get norm (%s)\n", LOG_LEVEL_V4L2
, strerror (errno
));
1510 pvr_stream_close (stream_t
*stream
)
1517 pvr
= (struct pvr_t
*) stream
->priv
;
1522 pvr_stream_read (stream_t
*stream
, char *buffer
, int size
)
1524 struct pollfd pfds
[1];
1528 if (!stream
|| !buffer
)
1531 pvr
= (struct pvr_t
*) stream
->priv
;
1541 pfds
[0].events
= POLLIN
| POLLPRI
;
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
);
1553 rk
= read (fd
, &buffer
[pos
], rk
);
1557 mp_msg (MSGT_OPEN
, MSGL_DBG3
,
1558 "%s read (%d) bytes\n", LOG_LEVEL_PVR
, pos
);
1563 mp_msg (MSGT_OPEN
, MSGL_ERR
, "%s read %d bytes\n", LOG_LEVEL_PVR
, pos
);
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
;
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
);
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
);
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
));
1611 return STREAM_ERROR
;
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",
1624 return STREAM_ERROR
;
1627 /* check for device hardware MPEG encoding capability */
1628 ctrls
.ctrl_class
= V4L2_CTRL_CLASS_MPEG
;
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
);
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
);
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
);
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
);
1672 return STREAM_ERROR
;
1676 stream
->type
= STREAMTYPE_PVR
;
1677 stream
->fill_buffer
= pvr_stream_read
;
1678 stream
->close
= pvr_stream_close
;
1683 /* PVR Public API access */
1686 pvr_get_current_stationname (stream_t
*stream
)
1690 if (!stream
|| stream
->type
!= STREAMTYPE_PVR
)
1693 pvr
= (struct pvr_t
*) stream
->priv
;
1695 if (pvr
->stationlist
.list
&&
1696 pvr
->stationlist
.used
> pvr
->chan_idx
&&
1698 return pvr
->stationlist
.list
[pvr
->chan_idx
].station
;
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
&&
1711 return pvr
->stationlist
.list
[pvr
->chan_idx
].name
;
1717 pvr_get_current_frequency (stream_t
*stream
)
1719 struct pvr_t
*pvr
= (struct pvr_t
*) stream
->priv
;
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);
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)",