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>
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
{
94 char station
[PVR_STATION_NAME_SIZE
];
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 */
123 stationlist_t stationlist
;
124 /* dups the tv_param_channel, or the url's channel param */
139 static struct pvr_t
*
142 struct pvr_t
*pvr
= NULL
;
144 pvr
= calloc (1, sizeof (struct pvr_t
));
146 pvr
->video_dev
= strdup (PVR_DEFAULT_DEVICE
);
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
;
181 pvr_uninit (struct pvr_t
*pvr
)
191 free (pvr
->video_dev
);
193 if (pvr
->stationlist
.list
)
194 free (pvr
->stationlist
.list
);
196 if (pvr
->param_channel
)
197 free (pvr
->param_channel
);
203 * @brief Copy Constructor for stationlist
205 * @see parse_setup_stationlist
208 copycreate_stationlist (stationlist_t
*stationlist
, int num
)
212 if (chantab
< 0 || !stationlist
)
215 num
= FFMAX (num
, chanlists
[chantab
].count
);
217 if (stationlist
->list
)
219 free (stationlist
->list
);
220 stationlist
->list
= NULL
;
223 stationlist
->total
= 0;
224 stationlist
->enabled
= 0;
225 stationlist
->used
= 0;
226 stationlist
->list
= calloc (num
, sizeof (station_elem_t
));
228 if (!stationlist
->list
)
230 mp_msg (MSGT_OPEN
, MSGL_ERR
,
231 "%s No memory allocated for station list, giving up\n",
236 /* transport the channel list data to our extented struct */
237 stationlist
->total
= num
;
238 av_strlcpy (stationlist
->name
, chanlists
[chantab
].name
, PVR_STATION_NAME_SIZE
);
240 for (i
= 0; i
< chanlists
[chantab
].count
; i
++)
242 stationlist
->list
[i
].station
[0]= '\0'; /* no station name yet */
243 av_strlcpy (stationlist
->list
[i
].name
,
244 chanlists
[chantab
].list
[i
].name
, PVR_STATION_NAME_SIZE
);
245 stationlist
->list
[i
].freq
= chanlists
[chantab
].list
[i
].freq
;
246 stationlist
->list
[i
].enabled
= 1; /* default enabled */
247 stationlist
->enabled
++;
255 print_all_stations (struct pvr_t
*pvr
)
259 if (!pvr
|| !pvr
->stationlist
.list
)
262 for (i
= 0; i
< pvr
->stationlist
.total
; i
++)
264 mp_msg (MSGT_OPEN
, MSGL_V
,
265 "%s %3d: [%c] channel: %8s - freq: %8d - station: %s\n",
266 LOG_LEVEL_V4L2
, i
, (pvr
->stationlist
.list
[i
].enabled
) ? 'X' : ' ',
267 pvr
->stationlist
.list
[i
].name
, pvr
->stationlist
.list
[i
].freq
,
268 pvr
->stationlist
.list
[i
].station
);
275 * Disables all stations
277 * @see parse_setup_stationlist
280 disable_all_stations (struct pvr_t
*pvr
)
284 for (i
= 0; i
< pvr
->stationlist
.total
; i
++)
285 pvr
->stationlist
.list
[i
].enabled
= 0;
286 pvr
->stationlist
.enabled
= 0;
290 * Update or add a station
292 * @see parse_setup_stationlist
295 set_station (struct pvr_t
*pvr
, const char *station
,
296 const char *channel
, int freq
)
300 if (!pvr
|| !pvr
->stationlist
.list
)
303 if (0 >= pvr
->stationlist
.total
|| (!channel
&& !freq
))
307 for (i
= 0; i
< pvr
->stationlist
.used
; i
++)
309 if (channel
&& !strcasecmp (pvr
->stationlist
.list
[i
].name
, channel
))
310 break; /* found existing channel entry */
312 if (freq
> 0 && pvr
->stationlist
.list
[i
].freq
== freq
)
313 break; /* found existing frequency entry */
316 if (i
< pvr
->stationlist
.used
)
319 * found an existing entry,
320 * which is about to change with the user data.
321 * it is also enabled ..
323 if (!pvr
->stationlist
.list
[i
].enabled
)
325 pvr
->stationlist
.list
[i
].enabled
= 1;
326 pvr
->stationlist
.enabled
++;
330 av_strlcpy (pvr
->stationlist
.list
[i
].station
,
331 station
, PVR_STATION_NAME_SIZE
);
333 av_strlcpy (pvr
->stationlist
.list
[i
].station
,
334 channel
, PVR_STATION_NAME_SIZE
);
336 snprintf (pvr
->stationlist
.list
[i
].station
,
337 PVR_STATION_NAME_SIZE
, "F %d", freq
);
339 mp_msg (MSGT_OPEN
, MSGL_DBG2
,
340 "%s Set user station channel: %8s - freq: %8d - station: %s\n",
341 LOG_LEVEL_V4L2
, pvr
->stationlist
.list
[i
].name
,
342 pvr
->stationlist
.list
[i
].freq
,
343 pvr
->stationlist
.list
[i
].station
);
347 /* from here on, we have to create a new entry, frequency is mandatory */
350 mp_msg (MSGT_OPEN
, MSGL_ERR
,
351 "%s Cannot add new station/channel without frequency\n",
356 if (pvr
->stationlist
.total
< i
)
359 * we have to extend the stationlist about
360 * an arbitrary size, even though this path is not performance critical
362 pvr
->stationlist
.total
+= 10;
363 pvr
->stationlist
.list
=
364 realloc (pvr
->stationlist
.list
,
365 pvr
->stationlist
.total
* sizeof (station_elem_t
));
367 if (!pvr
->stationlist
.list
)
369 mp_msg (MSGT_OPEN
, MSGL_ERR
,
370 "%s No memory allocated for station list, giving up\n",
375 /* clear the new space ..*/
376 memset (&(pvr
->stationlist
.list
[pvr
->stationlist
.used
]), 0,
377 (pvr
->stationlist
.total
- pvr
->stationlist
.used
)
378 * sizeof (station_elem_t
));
381 /* here we go, our actual new entry */
382 pvr
->stationlist
.used
++;
383 pvr
->stationlist
.list
[i
].enabled
= 1;
384 pvr
->stationlist
.enabled
++;
387 av_strlcpy (pvr
->stationlist
.list
[i
].station
,
388 station
, PVR_STATION_NAME_SIZE
);
390 av_strlcpy (pvr
->stationlist
.list
[i
].name
, channel
, PVR_STATION_NAME_SIZE
);
392 snprintf (pvr
->stationlist
.list
[i
].name
,
393 PVR_STATION_NAME_SIZE
, "F %d", freq
);
395 pvr
->stationlist
.list
[i
].freq
= freq
;
397 mp_msg (MSGT_OPEN
, MSGL_DBG2
,
398 "%s Add user station channel: %8s - freq: %8d - station: %s\n",
399 LOG_LEVEL_V4L2
, pvr
->stationlist
.list
[i
].name
,
400 pvr
->stationlist
.list
[i
].freq
,
401 pvr
->stationlist
.list
[i
].station
);
407 * Here we set our stationlist, as follow
408 * - choose the frequency channel table, e.g. ntsc-cable
409 * - create our stationlist, same element size as the channellist
410 * - copy the channellist content to our stationlist
411 * - IF the user provides his channel-mapping, THEN:
412 * - disable all stations
413 * - update and/or create entries in the stationlist and enable them
416 parse_setup_stationlist (struct pvr_t
*pvr
)
423 /* Create our station/channel list */
424 if (stream_tv_defaults
.chanlist
)
426 /* select channel list */
427 for (i
= 0; chanlists
[i
].name
!= NULL
; i
++)
429 if (!strcasecmp (chanlists
[i
].name
, stream_tv_defaults
.chanlist
))
435 if (!chanlists
[i
].name
)
437 mp_msg (MSGT_OPEN
, MSGL_ERR
,
438 "%s unable to find channel list %s, using default %s\n",
439 LOG_LEVEL_V4L2
, stream_tv_defaults
.chanlist
, chanlists
[chantab
].name
);
443 mp_msg (MSGT_OPEN
, MSGL_INFO
,
444 "%s select channel list %s, entries %d\n", LOG_LEVEL_V4L2
,
445 chanlists
[chantab
].name
, chanlists
[chantab
].count
);
451 mp_msg (MSGT_OPEN
, MSGL_FATAL
,
452 "%s No channel list selected, giving up\n", LOG_LEVEL_V4L2
);
456 if (copycreate_stationlist (&(pvr
->stationlist
), -1) < 0)
458 mp_msg (MSGT_OPEN
, MSGL_FATAL
,
459 "%s No memory allocated for station list, giving up\n",
464 /* Handle user channel mappings */
465 if (stream_tv_defaults
.channels
)
467 char channel
[PVR_STATION_NAME_SIZE
];
468 char station
[PVR_STATION_NAME_SIZE
];
469 char **channels
= stream_tv_defaults
.channels
;
471 disable_all_stations (pvr
);
475 char *tmp
= *(channels
++);
476 char *sep
= strchr (tmp
, '-');
480 continue; /* Wrong syntax, but mplayer should not crash */
482 av_strlcpy (station
, sep
+ 1, PVR_STATION_NAME_SIZE
);
485 av_strlcpy (channel
, tmp
, PVR_STATION_NAME_SIZE
);
487 while ((sep
= strchr (station
, '_')))
490 /* if channel number is a number and larger than 1000 treat it as
491 * frequency tmp still contain pointer to null-terminated string with
492 * channel number here
494 if ((freq
= atoi (channel
)) <= 1000)
497 if (set_station (pvr
, station
, (freq
<= 0) ? channel
: NULL
, freq
) < 0)
499 mp_msg (MSGT_OPEN
, MSGL_ERR
,
500 "%s Unable to set user station channel: %8s - freq: %8d - station: %s\n", LOG_LEVEL_V4L2
,
501 channel
, freq
, station
);
506 return print_all_stations (pvr
);
510 get_v4l2_freq (struct pvr_t
*pvr
)
513 struct v4l2_frequency vf
;
514 struct v4l2_tuner vt
;
522 memset (&vt
, 0, sizeof (vt
));
523 memset (&vf
, 0, sizeof (vf
));
525 if (ioctl (pvr
->dev_fd
, VIDIOC_G_TUNER
, &vt
) < 0)
527 mp_msg (MSGT_OPEN
, MSGL_ERR
, "%s can't set tuner (%s).\n",
528 LOG_LEVEL_V4L2
, strerror (errno
));
532 if (ioctl (pvr
->dev_fd
, VIDIOC_G_FREQUENCY
, &vf
) < 0)
534 mp_msg (MSGT_OPEN
, MSGL_ERR
, "%s can't get frequency %d.\n",
535 LOG_LEVEL_V4L2
, errno
);
539 if (!(vt
.capability
& V4L2_TUNER_CAP_LOW
))
547 set_v4l2_freq (struct pvr_t
*pvr
)
549 struct v4l2_frequency vf
;
550 struct v4l2_tuner vt
;
557 mp_msg (MSGT_OPEN
, MSGL_ERR
,
558 "%s Frequency invalid %d !!!\n", LOG_LEVEL_V4L2
, pvr
->freq
);
562 /* don't set the frequency, if it's already set.
563 * setting it here would interrupt the stream.
565 if (get_v4l2_freq (pvr
) == pvr
->freq
)
567 mp_msg (MSGT_OPEN
, MSGL_STATUS
,
568 "%s Frequency %d already set.\n", LOG_LEVEL_V4L2
, pvr
->freq
);
575 memset (&vf
, 0, sizeof (vf
));
576 memset (&vt
, 0, sizeof (vt
));
578 if (ioctl (pvr
->dev_fd
, VIDIOC_G_TUNER
, &vt
) < 0)
580 mp_msg (MSGT_OPEN
, MSGL_ERR
, "%s can't get tuner (%s).\n",
581 LOG_LEVEL_V4L2
, strerror (errno
));
586 vf
.frequency
= pvr
->freq
* 16;
588 if (!(vt
.capability
& V4L2_TUNER_CAP_LOW
))
589 vf
.frequency
/= 1000;
591 if (ioctl (pvr
->dev_fd
, VIDIOC_S_FREQUENCY
, &vf
) < 0)
593 mp_msg (MSGT_OPEN
, MSGL_ERR
, "%s can't set frequency (%s).\n",
594 LOG_LEVEL_V4L2
, strerror (errno
));
598 memset (&vt
, 0, sizeof(vt
));
599 if (ioctl (pvr
->dev_fd
, VIDIOC_G_TUNER
, &vt
) < 0)
601 mp_msg (MSGT_OPEN
, MSGL_ERR
, "%s can't set tuner (%s).\n",
602 LOG_LEVEL_V4L2
, strerror (errno
));
606 /* just a notification */
608 mp_msg (MSGT_OPEN
, MSGL_ERR
, "%s NO SIGNAL at frequency %d (%d)\n",
609 LOG_LEVEL_V4L2
, pvr
->freq
, vf
.frequency
);
611 mp_msg (MSGT_OPEN
, MSGL_STATUS
, "%s Got signal at frequency %d (%d)\n",
612 LOG_LEVEL_V4L2
, pvr
->freq
, vf
.frequency
);
618 set_station_by_step (struct pvr_t
*pvr
, int step
, int v4lAction
)
620 if (!pvr
|| !pvr
->stationlist
.list
)
623 if (pvr
->stationlist
.enabled
>= abs (step
))
626 int chidx
= pvr
->chan_idx
+ step
;
630 chidx
= (chidx
+ pvr
->stationlist
.used
) % pvr
->stationlist
.used
;
632 mp_msg (MSGT_OPEN
, MSGL_DBG2
,
633 "%s Offset switch: current %d, enabled %d, step %d -> %d\n",
634 LOG_LEVEL_V4L2
, pvr
->chan_idx
,
635 pvr
->stationlist
.enabled
, step
, chidx
);
637 if (!pvr
->stationlist
.list
[chidx
].enabled
)
639 mp_msg (MSGT_OPEN
, MSGL_DBG2
,
640 "%s Switch disabled to user station channel: %8s - freq: %8d - station: %s\n", LOG_LEVEL_V4L2
,
641 pvr
->stationlist
.list
[chidx
].name
,
642 pvr
->stationlist
.list
[chidx
].freq
,
643 pvr
->stationlist
.list
[chidx
].station
);
644 chidx
+= FFSIGN (step
);
650 pvr
->freq
= pvr
->stationlist
.list
[chidx
].freq
;
651 pvr
->chan_idx_last
= pvr
->chan_idx
;
652 pvr
->chan_idx
= chidx
;
654 mp_msg (MSGT_OPEN
, MSGL_INFO
,
655 "%s Switch to user station channel: %8s - freq: %8d - station: %s\n", LOG_LEVEL_V4L2
,
656 pvr
->stationlist
.list
[chidx
].name
,
657 pvr
->stationlist
.list
[chidx
].freq
,
658 pvr
->stationlist
.list
[chidx
].station
);
661 return set_v4l2_freq (pvr
);
663 return (pvr
->freq
> 0) ? 0 : -1;
666 mp_msg (MSGT_OPEN
, MSGL_ERR
,
667 "%s Ooops couldn't set freq by channel entry step %d to current %d, enabled %d\n", LOG_LEVEL_V4L2
,
668 step
, pvr
->chan_idx
, pvr
->stationlist
.enabled
);
674 set_station_by_channelname_or_freq (struct pvr_t
*pvr
, const char *channel
,
675 int freq
, int v4lAction
)
679 if (!pvr
|| !pvr
->stationlist
.list
)
682 if (0 >= pvr
->stationlist
.enabled
)
684 mp_msg (MSGT_OPEN
, MSGL_WARN
,
685 "%s No enabled station, cannot switch channel/frequency\n",
692 /* select by channel */
693 for (i
= 0; i
< pvr
->stationlist
.used
; i
++)
695 if (!strcasecmp (pvr
->stationlist
.list
[i
].name
, channel
))
697 if (!pvr
->stationlist
.list
[i
].enabled
)
699 mp_msg (MSGT_OPEN
, MSGL_WARN
,
700 "%s Switch disabled to user station channel: %8s - freq: %8d - station: %s\n", LOG_LEVEL_V4L2
,
701 pvr
->stationlist
.list
[i
].name
,
702 pvr
->stationlist
.list
[i
].freq
,
703 pvr
->stationlist
.list
[i
].station
);
708 pvr
->freq
= pvr
->stationlist
.list
[i
].freq
;
709 pvr
->chan_idx_last
= pvr
->chan_idx
;
718 for (i
= 0; i
< pvr
->stationlist
.used
; i
++)
720 if (pvr
->stationlist
.list
[i
].freq
== freq
)
722 if (!pvr
->stationlist
.list
[i
].enabled
)
724 mp_msg (MSGT_OPEN
, MSGL_WARN
,
725 "%s Switch disabled to user station channel: %8s - freq: %8d - station: %s\n", LOG_LEVEL_V4L2
,
726 pvr
->stationlist
.list
[i
].name
,
727 pvr
->stationlist
.list
[i
].freq
,
728 pvr
->stationlist
.list
[i
].station
);
733 pvr
->freq
= pvr
->stationlist
.list
[i
].freq
;
734 pvr
->chan_idx_last
= pvr
->chan_idx
;
741 if (i
>= pvr
->stationlist
.used
)
744 mp_msg (MSGT_OPEN
, MSGL_WARN
,
745 "%s unable to find channel %s\n", LOG_LEVEL_V4L2
, channel
);
747 mp_msg (MSGT_OPEN
, MSGL_WARN
,
748 "%s unable to find frequency %d\n", LOG_LEVEL_V4L2
, freq
);
752 mp_msg (MSGT_OPEN
, MSGL_INFO
,
753 "%s Switch to user station channel: %8s - freq: %8d - station: %s\n", LOG_LEVEL_V4L2
,
754 pvr
->stationlist
.list
[i
].name
,
755 pvr
->stationlist
.list
[i
].freq
,
756 pvr
->stationlist
.list
[i
].station
);
759 return set_v4l2_freq (pvr
);
761 return (pvr
->freq
> 0) ? 0 : -1;
765 force_freq_step (struct pvr_t
*pvr
, int step
)
772 freq
= pvr
->freq
+step
;
776 mp_msg (MSGT_OPEN
, MSGL_INFO
,
777 "%s Force Frequency %d + %d = %d \n", LOG_LEVEL_V4L2
,
778 pvr
->freq
, step
, freq
);
782 return set_v4l2_freq (pvr
);
789 parse_encoder_options (struct pvr_t
*pvr
)
794 /* -pvr aspect=digit */
795 if (pvr_param_aspect_ratio
>= 0 && pvr_param_aspect_ratio
<= 3)
796 pvr
->aspect
= pvr_param_aspect_ratio
;
799 if (pvr_param_sample_rate
!= 0)
801 switch (pvr_param_sample_rate
)
804 pvr
->samplerate
= V4L2_MPEG_AUDIO_SAMPLING_FREQ_32000
;
807 pvr
->samplerate
= V4L2_MPEG_AUDIO_SAMPLING_FREQ_44100
;
810 pvr
->samplerate
= V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000
;
818 if (pvr_param_audio_layer
== 1)
819 pvr
->layer
= V4L2_MPEG_AUDIO_ENCODING_LAYER_1
;
820 else if (pvr_param_audio_layer
== 2)
821 pvr
->layer
= V4L2_MPEG_AUDIO_ENCODING_LAYER_2
;
822 else if (pvr_param_audio_layer
== 3)
823 pvr
->layer
= V4L2_MPEG_AUDIO_ENCODING_LAYER_3
;
825 /* -pvr abitrate=x */
826 if (pvr_param_audio_bitrate
!= 0)
828 if (pvr
->layer
== V4L2_MPEG_AUDIO_ENCODING_LAYER_1
)
830 switch (pvr_param_audio_bitrate
)
833 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L1_BITRATE_32K
;
836 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L1_BITRATE_64K
;
839 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L1_BITRATE_96K
;
842 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L1_BITRATE_128K
;
845 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L1_BITRATE_160K
;
848 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L1_BITRATE_192K
;
851 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L1_BITRATE_224K
;
854 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L1_BITRATE_256K
;
857 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L1_BITRATE_288K
;
860 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L1_BITRATE_320K
;
863 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L1_BITRATE_352K
;
866 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L1_BITRATE_384K
;
869 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L1_BITRATE_416K
;
872 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L1_BITRATE_448K
;
879 else if (pvr
->layer
== V4L2_MPEG_AUDIO_ENCODING_LAYER_2
)
881 switch (pvr_param_audio_bitrate
)
884 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L2_BITRATE_32K
;
887 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L2_BITRATE_48K
;
890 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L2_BITRATE_56K
;
893 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L2_BITRATE_64K
;
896 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L2_BITRATE_80K
;
899 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L2_BITRATE_96K
;
902 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L2_BITRATE_112K
;
905 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L2_BITRATE_128K
;
908 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L2_BITRATE_160K
;
911 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L2_BITRATE_192K
;
914 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L2_BITRATE_224K
;
917 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L2_BITRATE_256K
;
920 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L2_BITRATE_320K
;
923 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L2_BITRATE_384K
;
930 else if (pvr
->layer
== V4L2_MPEG_AUDIO_ENCODING_LAYER_3
)
932 switch (pvr_param_audio_bitrate
)
935 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L3_BITRATE_32K
;
938 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L3_BITRATE_40K
;
941 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L3_BITRATE_48K
;
944 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L3_BITRATE_56K
;
947 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L3_BITRATE_64K
;
950 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L3_BITRATE_80K
;
953 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L3_BITRATE_96K
;
956 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L3_BITRATE_112K
;
959 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L3_BITRATE_128K
;
962 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L3_BITRATE_160K
;
965 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L3_BITRATE_192K
;
968 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L3_BITRATE_224K
;
971 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L3_BITRATE_256K
;
974 pvr
->audio_rate
= V4L2_MPEG_AUDIO_L3_BITRATE_320K
;
983 if (pvr_param_audio_mode
)
985 if (!strcmp (pvr_param_audio_mode
, PVR_AUDIO_MODE_ARG_STEREO
))
986 pvr
->audio_mode
= V4L2_MPEG_AUDIO_MODE_STEREO
;
987 else if (!strcmp (pvr_param_audio_mode
, PVR_AUDIO_MODE_ARG_JOINT_STEREO
))
988 pvr
->audio_mode
= V4L2_MPEG_AUDIO_MODE_JOINT_STEREO
;
989 else if (!strcmp (pvr_param_audio_mode
, PVR_AUDIO_MODE_ARG_DUAL
))
990 pvr
->audio_mode
= V4L2_MPEG_AUDIO_MODE_DUAL
;
991 else if (!strcmp (pvr_param_audio_mode
, PVR_AUDIO_MODE_ARG_MONO
))
992 pvr
->audio_mode
= V4L2_MPEG_AUDIO_MODE_MONO
;
995 /* -pvr vbitrate=x */
996 if (pvr_param_bitrate
)
997 pvr
->bitrate
= pvr_param_bitrate
;
1000 if (pvr_param_bitrate_mode
)
1002 if (!strcmp (pvr_param_bitrate_mode
, PVR_VIDEO_BITRATE_MODE_ARG_VBR
))
1003 pvr
->bitrate_mode
= V4L2_MPEG_VIDEO_BITRATE_MODE_VBR
;
1004 else if (!strcmp (pvr_param_bitrate_mode
, PVR_VIDEO_BITRATE_MODE_ARG_CBR
))
1005 pvr
->bitrate_mode
= V4L2_MPEG_VIDEO_BITRATE_MODE_CBR
;
1009 if (pvr_param_bitrate_peak
)
1010 pvr
->bitrate_peak
= pvr_param_bitrate_peak
;
1013 if (pvr_param_stream_type
)
1015 if (!strcmp (pvr_param_stream_type
, PVR_VIDEO_STREAM_TYPE_PS
))
1016 pvr
->stream_type
= V4L2_MPEG_STREAM_TYPE_MPEG2_PS
;
1017 else if (!strcmp (pvr_param_stream_type
, PVR_VIDEO_STREAM_TYPE_TS
))
1018 pvr
->stream_type
= V4L2_MPEG_STREAM_TYPE_MPEG2_TS
;
1019 else if (!strcmp (pvr_param_stream_type
, PVR_VIDEO_STREAM_TYPE_MPEG1
))
1020 pvr
->stream_type
= V4L2_MPEG_STREAM_TYPE_MPEG1_SS
;
1021 else if (!strcmp (pvr_param_stream_type
, PVR_VIDEO_STREAM_TYPE_DVD
))
1022 pvr
->stream_type
= V4L2_MPEG_STREAM_TYPE_MPEG2_DVD
;
1023 else if (!strcmp (pvr_param_stream_type
, PVR_VIDEO_STREAM_TYPE_VCD
))
1024 pvr
->stream_type
= V4L2_MPEG_STREAM_TYPE_MPEG1_VCD
;
1025 else if (!strcmp (pvr_param_stream_type
, PVR_VIDEO_STREAM_TYPE_SVCD
))
1026 pvr
->stream_type
= V4L2_MPEG_STREAM_TYPE_MPEG2_SVCD
;
1031 add_v4l2_ext_control (struct v4l2_ext_control
*ctrl
,
1032 uint32_t id
, int32_t value
)
1035 ctrl
->value
= value
;
1039 set_encoder_settings (struct pvr_t
*pvr
)
1041 struct v4l2_ext_control
*ext_ctrl
= NULL
;
1042 struct v4l2_ext_controls ctrls
;
1048 if (pvr
->dev_fd
< 0)
1051 ext_ctrl
= (struct v4l2_ext_control
*)
1052 malloc (PVR_MAX_CONTROLS
* sizeof (struct v4l2_ext_control
));
1054 add_v4l2_ext_control (&ext_ctrl
[count
++], V4L2_CID_MPEG_VIDEO_ASPECT
,
1057 add_v4l2_ext_control (&ext_ctrl
[count
++], V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ
,
1060 add_v4l2_ext_control (&ext_ctrl
[count
++], V4L2_CID_MPEG_AUDIO_ENCODING
,
1065 case V4L2_MPEG_AUDIO_ENCODING_LAYER_1
:
1066 add_v4l2_ext_control (&ext_ctrl
[count
++], V4L2_CID_MPEG_AUDIO_L1_BITRATE
,
1069 case V4L2_MPEG_AUDIO_ENCODING_LAYER_2
:
1070 add_v4l2_ext_control (&ext_ctrl
[count
++], V4L2_CID_MPEG_AUDIO_L2_BITRATE
,
1073 case V4L2_MPEG_AUDIO_ENCODING_LAYER_3
:
1074 add_v4l2_ext_control (&ext_ctrl
[count
++], V4L2_CID_MPEG_AUDIO_L3_BITRATE
,
1081 add_v4l2_ext_control (&ext_ctrl
[count
++], V4L2_CID_MPEG_AUDIO_MODE
,
1084 add_v4l2_ext_control (&ext_ctrl
[count
++], V4L2_CID_MPEG_VIDEO_BITRATE
,
1087 add_v4l2_ext_control (&ext_ctrl
[count
++], V4L2_CID_MPEG_VIDEO_BITRATE_PEAK
,
1090 add_v4l2_ext_control (&ext_ctrl
[count
++], V4L2_CID_MPEG_VIDEO_BITRATE_MODE
,
1093 add_v4l2_ext_control (&ext_ctrl
[count
++], V4L2_CID_MPEG_STREAM_TYPE
,
1096 /* set new encoding settings */
1097 ctrls
.ctrl_class
= V4L2_CTRL_CLASS_MPEG
;
1098 ctrls
.count
= count
;
1099 ctrls
.controls
= ext_ctrl
;
1101 if (ioctl (pvr
->dev_fd
, VIDIOC_S_EXT_CTRLS
, &ctrls
) < 0)
1103 mp_msg (MSGT_OPEN
, MSGL_ERR
, "%s Error setting MPEG controls (%s).\n",
1104 LOG_LEVEL_ENCODER
, strerror (errno
));
1115 parse_v4l2_tv_options (struct pvr_t
*pvr
)
1120 /* Create our station/channel list */
1121 parse_setup_stationlist (pvr
);
1123 if (pvr
->param_channel
)
1125 if (set_station_by_channelname_or_freq (pvr
, pvr
->param_channel
,
1128 if (stream_tv_defaults
.freq
)
1130 mp_msg (MSGT_OPEN
, MSGL_HINT
,
1131 "%s tv param freq %s is overwritten by channel setting freq %d\n", LOG_LEVEL_V4L2
,
1132 stream_tv_defaults
.freq
, pvr
->freq
);
1137 if (pvr
->freq
< 0 && stream_tv_defaults
.freq
)
1139 mp_msg (MSGT_OPEN
, MSGL_HINT
, "%s tv param freq %s is used directly\n",
1140 LOG_LEVEL_V4L2
, stream_tv_defaults
.freq
);
1142 if (set_station_by_channelname_or_freq (pvr
, NULL
,
1143 atoi (stream_tv_defaults
.freq
), 0)<0)
1145 mp_msg (MSGT_OPEN
, MSGL_WARN
,
1146 "%s tv param freq %s invalid to set station\n",
1147 LOG_LEVEL_V4L2
, stream_tv_defaults
.freq
);
1151 if (stream_tv_defaults
.device
)
1154 free (pvr
->video_dev
);
1155 pvr
->video_dev
= strdup (stream_tv_defaults
.device
);
1158 if (stream_tv_defaults
.noaudio
)
1159 pvr
->mute
= stream_tv_defaults
.noaudio
;
1161 if (stream_tv_defaults
.input
)
1162 pvr
->input
= stream_tv_defaults
.input
;
1164 if (stream_tv_defaults
.normid
)
1165 pvr
->normid
= stream_tv_defaults
.normid
;
1167 if (stream_tv_defaults
.brightness
)
1168 pvr
->brightness
= stream_tv_defaults
.brightness
;
1170 if (stream_tv_defaults
.contrast
)
1171 pvr
->contrast
= stream_tv_defaults
.contrast
;
1173 if (stream_tv_defaults
.hue
)
1174 pvr
->hue
= stream_tv_defaults
.hue
;
1176 if (stream_tv_defaults
.saturation
)
1177 pvr
->saturation
= stream_tv_defaults
.saturation
;
1179 if (stream_tv_defaults
.width
)
1180 pvr
->width
= stream_tv_defaults
.width
;
1182 if (stream_tv_defaults
.height
)
1183 pvr
->height
= stream_tv_defaults
.height
;
1187 set_v4l2_settings (struct pvr_t
*pvr
)
1192 if (pvr
->dev_fd
< 0)
1198 struct v4l2_control ctrl
;
1199 ctrl
.id
= V4L2_CID_AUDIO_MUTE
;
1201 if (ioctl (pvr
->dev_fd
, VIDIOC_S_CTRL
, &ctrl
) < 0)
1203 mp_msg (MSGT_OPEN
, MSGL_ERR
,
1204 "%s can't mute (%s).\n", LOG_LEVEL_V4L2
, strerror (errno
));
1210 if (pvr
->input
!= 0)
1212 if (ioctl (pvr
->dev_fd
, VIDIOC_S_INPUT
, &pvr
->input
) < 0)
1214 mp_msg (MSGT_OPEN
, MSGL_ERR
,
1215 "%s can't set input (%s)\n", LOG_LEVEL_V4L2
, strerror (errno
));
1221 if (pvr
->normid
!= -1)
1223 struct v4l2_standard std
;
1224 std
.index
= pvr
->normid
;
1226 if (ioctl (pvr
->dev_fd
, VIDIOC_ENUMSTD
, &std
) < 0)
1228 mp_msg (MSGT_OPEN
, MSGL_ERR
,
1229 "%s can't set norm (%s)\n", LOG_LEVEL_V4L2
, strerror (errno
));
1233 mp_msg (MSGT_OPEN
, MSGL_V
,
1234 "%s set norm to %s\n", LOG_LEVEL_V4L2
, std
.name
);
1236 if (ioctl (pvr
->dev_fd
, VIDIOC_S_STD
, &std
.id
) < 0)
1238 mp_msg (MSGT_OPEN
, MSGL_ERR
,
1239 "%s can't set norm (%s)\n", LOG_LEVEL_V4L2
, strerror (errno
));
1244 /* -tv brightness=x */
1245 if (pvr
->brightness
!= 0)
1247 struct v4l2_control ctrl
;
1248 ctrl
.id
= V4L2_CID_BRIGHTNESS
;
1249 ctrl
.value
= pvr
->brightness
;
1253 if (ctrl
.value
> 255)
1256 if (ioctl (pvr
->dev_fd
, VIDIOC_S_CTRL
, &ctrl
) < 0)
1258 mp_msg (MSGT_OPEN
, MSGL_ERR
,
1259 "%s can't set brightness to %d (%s).\n",
1260 LOG_LEVEL_V4L2
, ctrl
.value
, strerror (errno
));
1265 /* -tv contrast=x */
1266 if (pvr
->contrast
!= 0)
1268 struct v4l2_control ctrl
;
1269 ctrl
.id
= V4L2_CID_CONTRAST
;
1270 ctrl
.value
= pvr
->contrast
;
1274 if (ctrl
.value
> 127)
1277 if (ioctl (pvr
->dev_fd
, VIDIOC_S_CTRL
, &ctrl
) < 0)
1279 mp_msg (MSGT_OPEN
, MSGL_ERR
,
1280 "%s can't set contrast to %d (%s).\n",
1281 LOG_LEVEL_V4L2
, ctrl
.value
, strerror (errno
));
1289 struct v4l2_control ctrl
;
1290 ctrl
.id
= V4L2_CID_HUE
;
1291 ctrl
.value
= pvr
->hue
;
1293 if (ctrl
.value
< -128)
1295 if (ctrl
.value
> 127)
1298 if (ioctl (pvr
->dev_fd
, VIDIOC_S_CTRL
, &ctrl
) < 0)
1300 mp_msg (MSGT_OPEN
, MSGL_ERR
,
1301 "%s can't set hue to %d (%s).\n",
1302 LOG_LEVEL_V4L2
, ctrl
.value
, strerror (errno
));
1307 /* -tv saturation=x */
1308 if (pvr
->saturation
!= 0)
1310 struct v4l2_control ctrl
;
1311 ctrl
.id
= V4L2_CID_SATURATION
;
1312 ctrl
.value
= pvr
->saturation
;
1316 if (ctrl
.value
> 127)
1319 if (ioctl (pvr
->dev_fd
, VIDIOC_S_CTRL
, &ctrl
) < 0)
1321 mp_msg (MSGT_OPEN
, MSGL_ERR
,
1322 "%s can't set saturation to %d (%s).\n",
1323 LOG_LEVEL_V4L2
, ctrl
.value
, strerror (errno
));
1328 /* -tv width=x:height=y */
1329 if (pvr
->width
&& pvr
->height
)
1331 struct v4l2_format vfmt
;
1332 vfmt
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1333 vfmt
.fmt
.pix
.width
= pvr
->width
;
1334 vfmt
.fmt
.pix
.height
= pvr
->height
;
1336 if (ioctl (pvr
->dev_fd
, VIDIOC_S_FMT
, &vfmt
) < 0)
1338 mp_msg (MSGT_OPEN
, MSGL_ERR
,
1339 "%s can't set resolution to %dx%d (%s).\n",
1340 LOG_LEVEL_V4L2
, pvr
->width
, pvr
->height
, strerror (errno
));
1347 int freq
= get_v4l2_freq (pvr
);
1348 mp_msg (MSGT_OPEN
, MSGL_INFO
,
1349 "%s Using current set frequency %d, to set channel\n",
1350 LOG_LEVEL_V4L2
, freq
);
1353 return set_station_by_channelname_or_freq (pvr
, NULL
, freq
, 1);
1357 return set_v4l2_freq (pvr
) ;
1363 v4l2_list_capabilities (struct pvr_t
*pvr
)
1365 struct v4l2_audio vaudio
;
1366 struct v4l2_standard vs
;
1367 struct v4l2_input vin
;
1373 if (pvr
->dev_fd
< 0)
1376 /* list available video inputs */
1379 mp_msg (MSGT_OPEN
, MSGL_INFO
,
1380 "%s Available video inputs: ", LOG_LEVEL_V4L2
);
1381 while (ioctl (pvr
->dev_fd
, VIDIOC_ENUMINPUT
, &vin
) >= 0)
1384 mp_msg (MSGT_OPEN
, MSGL_INFO
, "'#%d, %s' ", vin
.index
, vin
.name
);
1389 mp_msg (MSGT_OPEN
, MSGL_INFO
, "none\n");
1393 mp_msg (MSGT_OPEN
, MSGL_INFO
, "\n");
1395 /* list available audio inputs */
1398 mp_msg (MSGT_OPEN
, MSGL_INFO
,
1399 "%s Available audio inputs: ", LOG_LEVEL_V4L2
);
1400 while (ioctl (pvr
->dev_fd
, VIDIOC_ENUMAUDIO
, &vaudio
) >= 0)
1403 mp_msg (MSGT_OPEN
, MSGL_INFO
, "'#%d, %s' ", vaudio
.index
, vaudio
.name
);
1408 mp_msg (MSGT_OPEN
, MSGL_INFO
, "none\n");
1412 mp_msg (MSGT_OPEN
, MSGL_INFO
, "\n");
1414 /* list available norms */
1416 mp_msg (MSGT_OPEN
, MSGL_INFO
, "%s Available norms: ", LOG_LEVEL_V4L2
);
1417 while (ioctl (pvr
->dev_fd
, VIDIOC_ENUMSTD
, &vs
) >= 0)
1420 mp_msg (MSGT_OPEN
, MSGL_INFO
, "'#%d, %s' ", vs
.index
, vs
.name
);
1425 mp_msg (MSGT_OPEN
, MSGL_INFO
, "none\n");
1429 mp_msg (MSGT_OPEN
, MSGL_INFO
, "\n");
1435 v4l2_display_settings (struct pvr_t
*pvr
)
1437 struct v4l2_audio vaudio
;
1438 struct v4l2_standard vs
;
1439 struct v4l2_input vin
;
1446 if (pvr
->dev_fd
< 0)
1449 /* get current video input */
1450 if (ioctl (pvr
->dev_fd
, VIDIOC_G_INPUT
, &input
) == 0)
1453 if (ioctl (pvr
->dev_fd
, VIDIOC_ENUMINPUT
, &vin
) < 0)
1455 mp_msg (MSGT_OPEN
, MSGL_ERR
,
1456 "%s can't get input (%s).\n", LOG_LEVEL_V4L2
, strerror (errno
));
1460 mp_msg (MSGT_OPEN
, MSGL_INFO
,
1461 "%s Video input: %s\n", LOG_LEVEL_V4L2
, vin
.name
);
1465 mp_msg (MSGT_OPEN
, MSGL_ERR
,
1466 "%s can't get input (%s).\n", LOG_LEVEL_V4L2
, strerror (errno
));
1470 /* get current audio input */
1471 if (ioctl (pvr
->dev_fd
, VIDIOC_G_AUDIO
, &vaudio
) == 0)
1473 mp_msg (MSGT_OPEN
, MSGL_INFO
,
1474 "%s Audio input: %s\n", LOG_LEVEL_V4L2
, vaudio
.name
);
1478 mp_msg (MSGT_OPEN
, MSGL_ERR
,
1479 "%s can't get input (%s).\n", LOG_LEVEL_V4L2
, strerror (errno
));
1483 /* get current video format */
1484 if (ioctl (pvr
->dev_fd
, VIDIOC_G_STD
, &std
) == 0)
1488 while (ioctl (pvr
->dev_fd
, VIDIOC_ENUMSTD
, &vs
) >= 0)
1492 mp_msg (MSGT_OPEN
, MSGL_INFO
,
1493 "%s Norm: %s.\n", LOG_LEVEL_V4L2
, vs
.name
);
1501 mp_msg (MSGT_OPEN
, MSGL_ERR
,
1502 "%s can't get norm (%s)\n", LOG_LEVEL_V4L2
, strerror (errno
));
1512 pvr_stream_close (stream_t
*stream
)
1519 pvr
= (struct pvr_t
*) stream
->priv
;
1524 pvr_stream_read (stream_t
*stream
, char *buffer
, int size
)
1526 struct pollfd pfds
[1];
1530 if (!stream
|| !buffer
)
1533 pvr
= (struct pvr_t
*) stream
->priv
;
1543 pfds
[0].events
= POLLIN
| POLLPRI
;
1547 if (poll (pfds
, 1, 500) <= 0)
1549 mp_msg (MSGT_OPEN
, MSGL_ERR
,
1550 "%s failed with errno %d when reading %d bytes\n",
1551 LOG_LEVEL_PVR
, errno
, size
-pos
);
1555 rk
= read (fd
, &buffer
[pos
], rk
);
1559 mp_msg (MSGT_OPEN
, MSGL_DBG3
,
1560 "%s read (%d) bytes\n", LOG_LEVEL_PVR
, pos
);
1565 mp_msg (MSGT_OPEN
, MSGL_ERR
, "%s read %d bytes\n", LOG_LEVEL_PVR
, pos
);
1571 pvr_stream_open (stream_t
*stream
, int mode
, void *opts
, int *file_format
)
1573 struct v4l2_capability vcap
;
1574 struct v4l2_ext_controls ctrls
;
1575 struct pvr_t
*pvr
= NULL
;
1577 if (mode
!= STREAM_READ
)
1578 return STREAM_UNSUPPORTED
;
1583 * if the url, i.e. 'pvr://8', contains the channel, use it,
1584 * else use the tv parameter.
1586 if (stream
->url
&& strlen (stream
->url
) > 6 && stream
->url
[6] != '\0')
1587 pvr
->param_channel
= strdup (stream
->url
+ 6);
1588 else if (stream_tv_defaults
.channel
&& strlen (stream_tv_defaults
.channel
))
1589 pvr
->param_channel
= strdup (stream_tv_defaults
.channel
);
1591 parse_v4l2_tv_options (pvr
);
1592 parse_encoder_options (pvr
);
1595 pvr
->dev_fd
= open (pvr
->video_dev
, O_RDWR
);
1596 mp_msg (MSGT_OPEN
, MSGL_INFO
,
1597 "%s Using device %s\n", LOG_LEVEL_PVR
, pvr
->video_dev
);
1598 if (pvr
->dev_fd
== -1)
1600 mp_msg (MSGT_OPEN
, MSGL_ERR
,
1601 "%s error opening device %s\n", LOG_LEVEL_PVR
, pvr
->video_dev
);
1603 return STREAM_ERROR
;
1606 /* query capabilities (i.e test V4L2 support) */
1607 if (ioctl (pvr
->dev_fd
, VIDIOC_QUERYCAP
, &vcap
) < 0)
1609 mp_msg (MSGT_OPEN
, MSGL_ERR
,
1610 "%s device is not V4L2 compliant (%s).\n",
1611 LOG_LEVEL_PVR
, strerror (errno
));
1613 return STREAM_ERROR
;
1616 mp_msg (MSGT_OPEN
, MSGL_INFO
,
1617 "%s Detected %s\n", LOG_LEVEL_PVR
, vcap
.card
);
1619 /* check for a valid V4L2 capture device */
1620 if (!(vcap
.capabilities
& V4L2_CAP_VIDEO_CAPTURE
))
1622 mp_msg (MSGT_OPEN
, MSGL_ERR
,
1623 "%s device is not a valid V4L2 capture device.\n",
1626 return STREAM_ERROR
;
1629 /* check for device hardware MPEG encoding capability */
1630 ctrls
.ctrl_class
= V4L2_CTRL_CLASS_MPEG
;
1632 ctrls
.controls
= NULL
;
1634 if (ioctl (pvr
->dev_fd
, VIDIOC_G_EXT_CTRLS
, &ctrls
) < 0)
1636 mp_msg (MSGT_OPEN
, MSGL_ERR
,
1637 "%s device do not support MPEG input.\n", LOG_LEVEL_ENCODER
);
1638 return STREAM_ERROR
;
1641 /* list V4L2 capabilities */
1642 if (v4l2_list_capabilities (pvr
) == -1)
1644 mp_msg (MSGT_OPEN
, MSGL_ERR
,
1645 "%s can't get v4l2 capabilities\n", LOG_LEVEL_PVR
);
1647 return STREAM_ERROR
;
1650 /* apply V4L2 settings */
1651 if (set_v4l2_settings (pvr
) == -1)
1653 mp_msg (MSGT_OPEN
, MSGL_ERR
,
1654 "%s can't set v4l2 settings\n", LOG_LEVEL_PVR
);
1656 return STREAM_ERROR
;
1659 /* apply encoder settings */
1660 if (set_encoder_settings (pvr
) == -1)
1662 mp_msg (MSGT_OPEN
, MSGL_ERR
,
1663 "%s can't set encoder settings\n", LOG_LEVEL_PVR
);
1665 return STREAM_ERROR
;
1668 /* display current V4L2 settings */
1669 if (v4l2_display_settings (pvr
) == -1)
1671 mp_msg (MSGT_OPEN
, MSGL_ERR
,
1672 "%s can't get v4l2 settings\n", LOG_LEVEL_PVR
);
1674 return STREAM_ERROR
;
1678 stream
->type
= STREAMTYPE_PVR
;
1679 stream
->fill_buffer
= pvr_stream_read
;
1680 stream
->close
= pvr_stream_close
;
1685 /* PVR Public API access */
1688 pvr_get_current_stationname (stream_t
*stream
)
1692 if (!stream
|| stream
->type
!= STREAMTYPE_PVR
)
1695 pvr
= (struct pvr_t
*) stream
->priv
;
1697 if (pvr
->stationlist
.list
&&
1698 pvr
->stationlist
.used
> pvr
->chan_idx
&&
1700 return pvr
->stationlist
.list
[pvr
->chan_idx
].station
;
1706 pvr_get_current_channelname (stream_t
*stream
)
1708 struct pvr_t
*pvr
= (struct pvr_t
*) stream
->priv
;
1710 if (pvr
->stationlist
.list
&&
1711 pvr
->stationlist
.used
> pvr
->chan_idx
&&
1713 return pvr
->stationlist
.list
[pvr
->chan_idx
].name
;
1719 pvr_get_current_frequency (stream_t
*stream
)
1721 struct pvr_t
*pvr
= (struct pvr_t
*) stream
->priv
;
1727 pvr_set_channel (stream_t
*stream
, const char * channel
)
1729 struct pvr_t
*pvr
= (struct pvr_t
*) stream
->priv
;
1731 return set_station_by_channelname_or_freq (pvr
, channel
, -1, 1);
1735 pvr_set_lastchannel (stream_t
*stream
)
1737 struct pvr_t
*pvr
= (struct pvr_t
*) stream
->priv
;
1739 if (pvr
->stationlist
.list
&&
1740 pvr
->stationlist
.used
> pvr
->chan_idx_last
&&
1741 pvr
->chan_idx_last
>= 0)
1742 return set_station_by_channelname_or_freq (pvr
, pvr
->stationlist
.list
[pvr
->chan_idx_last
].name
, -1, 1);
1748 pvr_set_freq (stream_t
*stream
, int freq
)
1750 struct pvr_t
*pvr
= (struct pvr_t
*) stream
->priv
;
1752 return set_station_by_channelname_or_freq (pvr
, NULL
, freq
, 1);
1756 pvr_set_channel_step (stream_t
*stream
, int step
)
1758 struct pvr_t
*pvr
= (struct pvr_t
*) stream
->priv
;
1760 return set_station_by_step (pvr
, step
, 1);
1764 pvr_force_freq_step (stream_t
*stream
, int step
)
1766 struct pvr_t
*pvr
= (struct pvr_t
*) stream
->priv
;
1768 return force_freq_step (pvr
, step
);
1771 const stream_info_t stream_info_pvr
= {
1772 "V4L2 MPEG Input (a.k.a PVR)",