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.
36 #include <sys/ioctl.h>
37 #include <sys/fcntl.h>
40 #include <linux/types.h>
41 #include <linux/videodev2.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
{
95 char station
[PVR_STATION_NAME_SIZE
];
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 */
124 stationlist_t stationlist
;
125 /* dups the tv_param_channel, or the url's channel param */
140 static struct pvr_t
*
143 struct pvr_t
*pvr
= NULL
;
145 pvr
= calloc (1, sizeof (struct pvr_t
));
147 pvr
->video_dev
= strdup (PVR_DEFAULT_DEVICE
);
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
;
182 pvr_uninit (struct pvr_t
*pvr
)
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
);
204 * @brief Copy Constructor for stationlist
206 * @see parse_setup_stationlist
209 copycreate_stationlist (stationlist_t
*stationlist
, int num
)
213 if (chantab
< 0 || !stationlist
)
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",
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
++;
256 print_all_stations (struct pvr_t
*pvr
)
260 if (!pvr
|| !pvr
->stationlist
.list
)
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
);
276 * Disables all stations
278 * @see parse_setup_stationlist
281 disable_all_stations (struct pvr_t
*pvr
)
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
296 set_station (struct pvr_t
*pvr
, const char *station
,
297 const char *channel
, int freq
)
301 if (!pvr
|| !pvr
->stationlist
.list
)
304 if (0 >= pvr
->stationlist
.total
|| (!channel
&& !freq
))
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
++;
331 av_strlcpy (pvr
->stationlist
.list
[i
].station
,
332 station
, PVR_STATION_NAME_SIZE
);
334 av_strlcpy (pvr
->stationlist
.list
[i
].station
,
335 channel
, PVR_STATION_NAME_SIZE
);
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
);
348 /* from here on, we have to create a new entry, frequency is mandatory */
351 mp_msg (MSGT_OPEN
, MSGL_ERR
,
352 "%s Cannot add new station/channel without frequency\n",
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",
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
++;
388 av_strlcpy (pvr
->stationlist
.list
[i
].station
,
389 station
, PVR_STATION_NAME_SIZE
);
391 av_strlcpy (pvr
->stationlist
.list
[i
].name
, channel
, PVR_STATION_NAME_SIZE
);
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
);
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
417 parse_setup_stationlist (struct pvr_t
*pvr
)
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
))
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
);
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
);
452 mp_msg (MSGT_OPEN
, MSGL_FATAL
,
453 "%s No channel list selected, giving up\n", LOG_LEVEL_V4L2
);
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",
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
);
476 char *tmp
= *(channels
++);
477 char *sep
= strchr (tmp
, '-');
481 continue; /* Wrong syntax, but mplayer should not crash */
483 av_strlcpy (station
, sep
+ 1, PVR_STATION_NAME_SIZE
);
486 av_strlcpy (channel
, tmp
, PVR_STATION_NAME_SIZE
);
488 while ((sep
= strchr (station
, '_')))
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)
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
);
511 get_v4l2_freq (struct pvr_t
*pvr
)
514 struct v4l2_frequency vf
;
515 struct v4l2_tuner vt
;
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
));
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
);
540 if (!(vt
.capability
& V4L2_TUNER_CAP_LOW
))
548 set_v4l2_freq (struct pvr_t
*pvr
)
550 struct v4l2_frequency vf
;
551 struct v4l2_tuner vt
;
558 mp_msg (MSGT_OPEN
, MSGL_ERR
,
559 "%s Frequency invalid %d !!!\n", LOG_LEVEL_V4L2
, pvr
->freq
);
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
);
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
));
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
));
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
));
607 /* just a notification */
609 mp_msg (MSGT_OPEN
, MSGL_ERR
, "%s NO SIGNAL at frequency %d (%d)\n",
610 LOG_LEVEL_V4L2
, pvr
->freq
, vf
.frequency
);
612 mp_msg (MSGT_OPEN
, MSGL_STATUS
, "%s Got signal at frequency %d (%d)\n",
613 LOG_LEVEL_V4L2
, pvr
->freq
, vf
.frequency
);
619 set_station_by_step (struct pvr_t
*pvr
, int step
, int v4lAction
)
621 if (!pvr
|| !pvr
->stationlist
.list
)
624 if (pvr
->stationlist
.enabled
>= abs (step
))
627 int chidx
= pvr
->chan_idx
+ step
;
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
);
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
);
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
);
675 set_station_by_channelname_or_freq (struct pvr_t
*pvr
, const char *channel
,
676 int freq
, int v4lAction
)
680 if (!pvr
|| !pvr
->stationlist
.list
)
683 if (0 >= pvr
->stationlist
.enabled
)
685 mp_msg (MSGT_OPEN
, MSGL_WARN
,
686 "%s No enabled station, cannot switch channel/frequency\n",
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
);
709 pvr
->freq
= pvr
->stationlist
.list
[i
].freq
;
710 pvr
->chan_idx_last
= pvr
->chan_idx
;
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
);
734 pvr
->freq
= pvr
->stationlist
.list
[i
].freq
;
735 pvr
->chan_idx_last
= pvr
->chan_idx
;
742 if (i
>= pvr
->stationlist
.used
)
745 mp_msg (MSGT_OPEN
, MSGL_WARN
,
746 "%s unable to find channel %s\n", LOG_LEVEL_V4L2
, channel
);
748 mp_msg (MSGT_OPEN
, MSGL_WARN
,
749 "%s unable to find frequency %d\n", LOG_LEVEL_V4L2
, freq
);
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
);
760 return set_v4l2_freq (pvr
);
762 return (pvr
->freq
> 0) ? 0 : -1;
766 force_freq_step (struct pvr_t
*pvr
, int step
)
773 freq
= pvr
->freq
+step
;
777 mp_msg (MSGT_OPEN
, MSGL_INFO
,
778 "%s Force Frequency %d + %d = %d \n", LOG_LEVEL_V4L2
,
779 pvr
->freq
, step
, freq
);
783 return set_v4l2_freq (pvr
);
790 parse_encoder_options (struct pvr_t
*pvr
)
795 /* -pvr aspect=digit */
796 if (pvr_param_aspect_ratio
>= 0 && pvr_param_aspect_ratio
<= 3)
797 pvr
->aspect
= pvr_param_aspect_ratio
;
800 if (pvr_param_sample_rate
!= 0)
802 switch (pvr_param_sample_rate
)
805 pvr
->samplerate
= V4L2_MPEG_AUDIO_SAMPLING_FREQ_32000
;
808 pvr
->samplerate
= V4L2_MPEG_AUDIO_SAMPLING_FREQ_44100
;
811 pvr
->samplerate
= V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000
;
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
)
834 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L1_BITRATE_32K
;
837 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L1_BITRATE_64K
;
840 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L1_BITRATE_96K
;
843 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L1_BITRATE_128K
;
846 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L1_BITRATE_160K
;
849 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L1_BITRATE_192K
;
852 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L1_BITRATE_224K
;
855 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L1_BITRATE_256K
;
858 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L1_BITRATE_288K
;
861 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L1_BITRATE_320K
;
864 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L1_BITRATE_352K
;
867 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L1_BITRATE_384K
;
870 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L1_BITRATE_416K
;
873 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L1_BITRATE_448K
;
880 else if (pvr
->layer
== V4L2_MPEG_AUDIO_ENCODING_LAYER_2
)
882 switch (pvr_param_audio_bitrate
)
885 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L2_BITRATE_32K
;
888 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L2_BITRATE_48K
;
891 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L2_BITRATE_56K
;
894 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L2_BITRATE_64K
;
897 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L2_BITRATE_80K
;
900 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L2_BITRATE_96K
;
903 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L2_BITRATE_112K
;
906 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L2_BITRATE_128K
;
909 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L2_BITRATE_160K
;
912 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L2_BITRATE_192K
;
915 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L2_BITRATE_224K
;
918 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L2_BITRATE_256K
;
921 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L2_BITRATE_320K
;
924 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L2_BITRATE_384K
;
931 else if (pvr
->layer
== V4L2_MPEG_AUDIO_ENCODING_LAYER_3
)
933 switch (pvr_param_audio_bitrate
)
936 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L3_BITRATE_32K
;
939 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L3_BITRATE_40K
;
942 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L3_BITRATE_48K
;
945 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L3_BITRATE_56K
;
948 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L3_BITRATE_64K
;
951 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L3_BITRATE_80K
;
954 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L3_BITRATE_96K
;
957 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L3_BITRATE_112K
;
960 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L3_BITRATE_128K
;
963 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L3_BITRATE_160K
;
966 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L3_BITRATE_192K
;
969 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L3_BITRATE_224K
;
972 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L3_BITRATE_256K
;
975 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L3_BITRATE_320K
;
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
;
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
;
1010 if (pvr_param_bitrate_peak
)
1011 pvr
->bitrate_peak
= pvr_param_bitrate_peak
;
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
;
1032 add_v4l2_ext_control (struct v4l2_ext_control
*ctrl
,
1033 uint32_t id
, int32_t value
)
1036 ctrl
->value
= value
;
1040 set_encoder_settings (struct pvr_t
*pvr
)
1042 struct v4l2_ext_control
*ext_ctrl
= NULL
;
1043 struct v4l2_ext_controls ctrls
;
1049 if (pvr
->dev_fd
< 0)
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
,
1058 add_v4l2_ext_control (&ext_ctrl
[count
++], V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ
,
1061 add_v4l2_ext_control (&ext_ctrl
[count
++], V4L2_CID_MPEG_AUDIO_ENCODING
,
1066 case V4L2_MPEG_AUDIO_ENCODING_LAYER_1
:
1067 add_v4l2_ext_control (&ext_ctrl
[count
++], V4L2_CID_MPEG_AUDIO_L1_BITRATE
,
1070 case V4L2_MPEG_AUDIO_ENCODING_LAYER_2
:
1071 add_v4l2_ext_control (&ext_ctrl
[count
++], V4L2_CID_MPEG_AUDIO_L2_BITRATE
,
1074 case V4L2_MPEG_AUDIO_ENCODING_LAYER_3
:
1075 add_v4l2_ext_control (&ext_ctrl
[count
++], V4L2_CID_MPEG_AUDIO_L3_BITRATE
,
1082 add_v4l2_ext_control (&ext_ctrl
[count
++], V4L2_CID_MPEG_AUDIO_MODE
,
1085 add_v4l2_ext_control (&ext_ctrl
[count
++], V4L2_CID_MPEG_VIDEO_BITRATE
,
1088 add_v4l2_ext_control (&ext_ctrl
[count
++], V4L2_CID_MPEG_VIDEO_BITRATE_PEAK
,
1091 add_v4l2_ext_control (&ext_ctrl
[count
++], V4L2_CID_MPEG_VIDEO_BITRATE_MODE
,
1094 add_v4l2_ext_control (&ext_ctrl
[count
++], V4L2_CID_MPEG_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
));
1116 parse_v4l2_tv_options (struct pvr_t
*pvr
)
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
,
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
)
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
;
1188 set_v4l2_settings (struct pvr_t
*pvr
)
1193 if (pvr
->dev_fd
< 0)
1199 struct v4l2_control ctrl
;
1200 ctrl
.id
= V4L2_CID_AUDIO_MUTE
;
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
));
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
));
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
));
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
));
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
;
1254 if (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
));
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
;
1275 if (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
));
1290 struct v4l2_control ctrl
;
1291 ctrl
.id
= V4L2_CID_HUE
;
1292 ctrl
.value
= pvr
->hue
;
1294 if (ctrl
.value
< -128)
1296 if (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
));
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
;
1317 if (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
));
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
));
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
);
1354 return set_station_by_channelname_or_freq (pvr
, NULL
, freq
, 1);
1358 return set_v4l2_freq (pvr
) ;
1364 v4l2_list_capabilities (struct pvr_t
*pvr
)
1366 struct v4l2_audio vaudio
;
1367 struct v4l2_standard vs
;
1368 struct v4l2_input vin
;
1374 if (pvr
->dev_fd
< 0)
1377 /* list available video inputs */
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)
1385 mp_msg (MSGT_OPEN
, MSGL_INFO
, "'#%d, %s' ", vin
.index
, vin
.name
);
1390 mp_msg (MSGT_OPEN
, MSGL_INFO
, "none\n");
1394 mp_msg (MSGT_OPEN
, MSGL_INFO
, "\n");
1396 /* list available audio inputs */
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)
1404 mp_msg (MSGT_OPEN
, MSGL_INFO
, "'#%d, %s' ", vaudio
.index
, vaudio
.name
);
1409 mp_msg (MSGT_OPEN
, MSGL_INFO
, "none\n");
1413 mp_msg (MSGT_OPEN
, MSGL_INFO
, "\n");
1415 /* list available norms */
1417 mp_msg (MSGT_OPEN
, MSGL_INFO
, "%s Available norms: ", LOG_LEVEL_V4L2
);
1418 while (ioctl (pvr
->dev_fd
, VIDIOC_ENUMSTD
, &vs
) >= 0)
1421 mp_msg (MSGT_OPEN
, MSGL_INFO
, "'#%d, %s' ", vs
.index
, vs
.name
);
1426 mp_msg (MSGT_OPEN
, MSGL_INFO
, "none\n");
1430 mp_msg (MSGT_OPEN
, MSGL_INFO
, "\n");
1436 v4l2_display_settings (struct pvr_t
*pvr
)
1438 struct v4l2_audio vaudio
;
1439 struct v4l2_standard vs
;
1440 struct v4l2_input vin
;
1447 if (pvr
->dev_fd
< 0)
1450 /* get current video input */
1451 if (ioctl (pvr
->dev_fd
, VIDIOC_G_INPUT
, &input
) == 0)
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
));
1461 mp_msg (MSGT_OPEN
, MSGL_INFO
,
1462 "%s Video input: %s\n", LOG_LEVEL_V4L2
, vin
.name
);
1466 mp_msg (MSGT_OPEN
, MSGL_ERR
,
1467 "%s can't get input (%s).\n", LOG_LEVEL_V4L2
, strerror (errno
));
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
);
1479 mp_msg (MSGT_OPEN
, MSGL_ERR
,
1480 "%s can't get input (%s).\n", LOG_LEVEL_V4L2
, strerror (errno
));
1484 /* get current video format */
1485 if (ioctl (pvr
->dev_fd
, VIDIOC_G_STD
, &std
) == 0)
1489 while (ioctl (pvr
->dev_fd
, VIDIOC_ENUMSTD
, &vs
) >= 0)
1493 mp_msg (MSGT_OPEN
, MSGL_INFO
,
1494 "%s Norm: %s.\n", LOG_LEVEL_V4L2
, vs
.name
);
1502 mp_msg (MSGT_OPEN
, MSGL_ERR
,
1503 "%s can't get norm (%s)\n", LOG_LEVEL_V4L2
, strerror (errno
));
1513 pvr_stream_close (stream_t
*stream
)
1520 pvr
= (struct pvr_t
*) stream
->priv
;
1525 pvr_stream_read (stream_t
*stream
, char *buffer
, int size
)
1527 struct pollfd pfds
[1];
1531 if (!stream
|| !buffer
)
1534 pvr
= (struct pvr_t
*) stream
->priv
;
1544 pfds
[0].events
= POLLIN
| POLLPRI
;
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
);
1556 rk
= read (fd
, &buffer
[pos
], rk
);
1560 mp_msg (MSGT_OPEN
, MSGL_DBG3
,
1561 "%s read (%d) bytes\n", LOG_LEVEL_PVR
, pos
);
1566 mp_msg (MSGT_OPEN
, MSGL_ERR
, "%s read %d bytes\n", LOG_LEVEL_PVR
, pos
);
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
;
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
);
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
);
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
));
1614 return STREAM_ERROR
;
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",
1627 return STREAM_ERROR
;
1630 /* check for device hardware MPEG encoding capability */
1631 ctrls
.ctrl_class
= V4L2_CTRL_CLASS_MPEG
;
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
);
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
);
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
);
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
);
1675 return STREAM_ERROR
;
1679 stream
->type
= STREAMTYPE_PVR
;
1680 stream
->fill_buffer
= pvr_stream_read
;
1681 stream
->close
= pvr_stream_close
;
1686 /* PVR Public API access */
1689 pvr_get_current_stationname (stream_t
*stream
)
1693 if (!stream
|| stream
->type
!= STREAMTYPE_PVR
)
1696 pvr
= (struct pvr_t
*) stream
->priv
;
1698 if (pvr
->stationlist
.list
&&
1699 pvr
->stationlist
.used
> pvr
->chan_idx
&&
1701 return pvr
->stationlist
.list
[pvr
->chan_idx
].station
;
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
&&
1714 return pvr
->stationlist
.list
[pvr
->chan_idx
].name
;
1720 pvr_get_current_frequency (stream_t
*stream
)
1722 struct pvr_t
*pvr
= (struct pvr_t
*) stream
->priv
;
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);
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)",